]> git.saurik.com Git - apple/xnu.git/blame - bsd/ufs/ffs/ffs_vnops.c
xnu-792.6.61.tar.gz
[apple/xnu.git] / bsd / ufs / ffs / ffs_vnops.c
CommitLineData
1c79356b 1/*
55e303ae 2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
37839358
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
1c79356b 11 *
37839358
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
37839358
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23/*
24 * Copyright (c) 1982, 1986, 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 *
55 * @(#)ffs_vnops.c 8.15 (Berkeley) 5/14/95
56 */
57
58#include <rev_endian_fs.h>
59#include <sys/param.h>
60#include <sys/systm.h>
61#include <sys/resourcevar.h>
62#include <sys/kernel.h>
63#include <sys/file.h>
64#include <sys/stat.h>
1c79356b
A
65#include <sys/proc.h>
66#include <sys/conf.h>
91447636
A
67#include <sys/mount_internal.h>
68#include <sys/vnode_internal.h>
1c79356b
A
69#include <sys/malloc.h>
70#include <sys/ubc.h>
9bccf70c 71#include <sys/quota.h>
1c79356b
A
72
73#include <sys/vm.h>
74#include <vfs/vfs_support.h>
75
76#include <miscfs/specfs/specdev.h>
77#include <miscfs/fifofs/fifo.h>
78
1c79356b
A
79#include <ufs/ufs/quota.h>
80#include <ufs/ufs/inode.h>
81#include <ufs/ufs/dir.h>
82#include <ufs/ufs/ufsmount.h>
83#include <ufs/ufs/ufs_extern.h>
84
85#include <ufs/ffs/fs.h>
86#include <ufs/ffs/ffs_extern.h>
87#if REV_ENDIAN_FS
88#include <ufs/ufs/ufs_byte_order.h>
89#include <architecture/byte_order.h>
90#endif /* REV_ENDIAN_FS */
91
92#define VOPFUNC int (*)(void *)
93
94/* Global vfs data structures for ufs. */
95int (**ffs_vnodeop_p)(void *);
96struct vnodeopv_entry_desc ffs_vnodeop_entries[] = {
91447636
A
97 { &vnop_default_desc, (VOPFUNC)vn_default_error },
98 { &vnop_lookup_desc, (VOPFUNC)ufs_lookup }, /* lookup */
99 { &vnop_create_desc, (VOPFUNC)ufs_create }, /* create */
100 { &vnop_whiteout_desc, (VOPFUNC)ufs_whiteout }, /* whiteout */
101 { &vnop_mknod_desc, (VOPFUNC)ufs_mknod }, /* mknod */
102 { &vnop_open_desc, (VOPFUNC)ufs_open }, /* open */
103 { &vnop_close_desc, (VOPFUNC)ufs_close }, /* close */
104 { &vnop_getattr_desc, (VOPFUNC)ufs_getattr }, /* getattr */
105 { &vnop_setattr_desc, (VOPFUNC)ufs_setattr }, /* setattr */
106 { &vnop_read_desc, (VOPFUNC)ffs_read }, /* read */
107 { &vnop_write_desc, (VOPFUNC)ffs_write }, /* write */
108 { &vnop_ioctl_desc, (VOPFUNC)ufs_ioctl }, /* ioctl */
109 { &vnop_select_desc, (VOPFUNC)ufs_select }, /* select */
110 { &vnop_revoke_desc, (VOPFUNC)ufs_revoke }, /* revoke */
111 { &vnop_mmap_desc, (VOPFUNC)ufs_mmap }, /* mmap */
112 { &vnop_fsync_desc, (VOPFUNC)ffs_fsync }, /* fsync */
113 { &vnop_remove_desc, (VOPFUNC)ufs_remove }, /* remove */
114 { &vnop_link_desc, (VOPFUNC)ufs_link }, /* link */
115 { &vnop_rename_desc, (VOPFUNC)ufs_rename }, /* rename */
116 { &vnop_mkdir_desc, (VOPFUNC)ufs_mkdir }, /* mkdir */
117 { &vnop_rmdir_desc, (VOPFUNC)ufs_rmdir }, /* rmdir */
118 { &vnop_symlink_desc, (VOPFUNC)ufs_symlink }, /* symlink */
119 { &vnop_readdir_desc, (VOPFUNC)ufs_readdir }, /* readdir */
120 { &vnop_readlink_desc, (VOPFUNC)ufs_readlink }, /* readlink */
121 { &vnop_inactive_desc, (VOPFUNC)ufs_inactive }, /* inactive */
122 { &vnop_reclaim_desc, (VOPFUNC)ffs_reclaim }, /* reclaim */
123 { &vnop_strategy_desc, (VOPFUNC)ufs_strategy }, /* strategy */
124 { &vnop_pathconf_desc, (VOPFUNC)ufs_pathconf }, /* pathconf */
125 { &vnop_advlock_desc, (VOPFUNC)err_advlock }, /* advlock */
126 { &vnop_bwrite_desc, (VOPFUNC)vn_bwrite },
127 { &vnop_pagein_desc, (VOPFUNC)ffs_pagein }, /* Pagein */
128 { &vnop_pageout_desc, (VOPFUNC)ffs_pageout }, /* Pageout */
129 { &vnop_copyfile_desc, (VOPFUNC)err_copyfile }, /* Copy File */
130 { &vnop_blktooff_desc, (VOPFUNC)ffs_blktooff }, /* blktooff */
131 { &vnop_offtoblk_desc, (VOPFUNC)ffs_offtoblk }, /* offtoblk */
132 { &vnop_blockmap_desc, (VOPFUNC)ufs_blockmap }, /* blockmap */
133 { &vnop_kqfilt_add_desc, (VOPFUNC)ufs_kqfilt_add }, /* kqfilt_add */
1c79356b
A
134 { (struct vnodeop_desc*)NULL, (int(*)())NULL }
135};
136struct vnodeopv_desc ffs_vnodeop_opv_desc =
137 { &ffs_vnodeop_p, ffs_vnodeop_entries };
138
139int (**ffs_specop_p)(void *);
140struct vnodeopv_entry_desc ffs_specop_entries[] = {
91447636
A
141 { &vnop_default_desc, (VOPFUNC)vn_default_error },
142 { &vnop_lookup_desc, (VOPFUNC)spec_lookup }, /* lookup */
143 { &vnop_create_desc, (VOPFUNC)spec_create }, /* create */
144 { &vnop_mknod_desc, (VOPFUNC)spec_mknod }, /* mknod */
145 { &vnop_open_desc, (VOPFUNC)spec_open }, /* open */
146 { &vnop_close_desc, (VOPFUNC)ufsspec_close }, /* close */
147 { &vnop_getattr_desc, (VOPFUNC)ufs_getattr }, /* getattr */
148 { &vnop_setattr_desc, (VOPFUNC)ufs_setattr }, /* setattr */
149 { &vnop_read_desc, (VOPFUNC)ufsspec_read }, /* read */
150 { &vnop_write_desc, (VOPFUNC)ufsspec_write }, /* write */
151 { &vnop_ioctl_desc, (VOPFUNC)spec_ioctl }, /* ioctl */
152 { &vnop_select_desc, (VOPFUNC)spec_select }, /* select */
153 { &vnop_revoke_desc, (VOPFUNC)spec_revoke }, /* revoke */
154 { &vnop_mmap_desc, (VOPFUNC)spec_mmap }, /* mmap */
155 { &vnop_fsync_desc, (VOPFUNC)ffs_fsync }, /* fsync */
156 { &vnop_remove_desc, (VOPFUNC)spec_remove }, /* remove */
157 { &vnop_link_desc, (VOPFUNC)spec_link }, /* link */
158 { &vnop_rename_desc, (VOPFUNC)spec_rename }, /* rename */
159 { &vnop_mkdir_desc, (VOPFUNC)spec_mkdir }, /* mkdir */
160 { &vnop_rmdir_desc, (VOPFUNC)spec_rmdir }, /* rmdir */
161 { &vnop_symlink_desc, (VOPFUNC)spec_symlink }, /* symlink */
162 { &vnop_readdir_desc, (VOPFUNC)spec_readdir }, /* readdir */
163 { &vnop_readlink_desc, (VOPFUNC)spec_readlink }, /* readlink */
164 { &vnop_inactive_desc, (VOPFUNC)ufs_inactive }, /* inactive */
165 { &vnop_reclaim_desc, (VOPFUNC)ffs_reclaim }, /* reclaim */
166 { &vnop_strategy_desc, (VOPFUNC)spec_strategy }, /* strategy */
167 { &vnop_pathconf_desc, (VOPFUNC)spec_pathconf }, /* pathconf */
168 { &vnop_advlock_desc, (VOPFUNC)err_advlock }, /* advlock */
169 { &vnop_bwrite_desc, (VOPFUNC)vn_bwrite },
170 { &vnop_devblocksize_desc, (VOPFUNC)spec_devblocksize }, /* devblocksize */
171 { &vnop_pagein_desc, (VOPFUNC)ffs_pagein }, /* Pagein */
172 { &vnop_pageout_desc, (VOPFUNC)ffs_pageout }, /* Pageout */
173 { &vnop_copyfile_desc, (VOPFUNC)err_copyfile }, /* Copy File */
174 { &vnop_blktooff_desc, (VOPFUNC)ffs_blktooff }, /* blktooff */
175 { &vnop_offtoblk_desc, (VOPFUNC)ffs_offtoblk }, /* offtoblk */
176 { &vnop_blockmap_desc, (VOPFUNC)spec_blockmap }, /* blockmap */
1c79356b
A
177 { (struct vnodeop_desc*)NULL, (int(*)())NULL }
178};
179struct vnodeopv_desc ffs_specop_opv_desc =
180 { &ffs_specop_p, ffs_specop_entries };
181
182#if FIFO
183int (**ffs_fifoop_p)(void *);
184struct vnodeopv_entry_desc ffs_fifoop_entries[] = {
91447636
A
185 { &vnop_default_desc, (VOPFUNC)vn_default_error },
186 { &vnop_lookup_desc, (VOPFUNC)fifo_lookup }, /* lookup */
187 { &vnop_create_desc, (VOPFUNC)fifo_create }, /* create */
188 { &vnop_mknod_desc, (VOPFUNC)fifo_mknod }, /* mknod */
189 { &vnop_open_desc, (VOPFUNC)fifo_open }, /* open */
190 { &vnop_close_desc, (VOPFUNC)ufsfifo_close }, /* close */
191 { &vnop_getattr_desc, (VOPFUNC)ufs_getattr }, /* getattr */
192 { &vnop_setattr_desc, (VOPFUNC)ufs_setattr }, /* setattr */
193 { &vnop_read_desc, (VOPFUNC)ufsfifo_read }, /* read */
194 { &vnop_write_desc, (VOPFUNC)ufsfifo_write }, /* write */
195 { &vnop_ioctl_desc, (VOPFUNC)fifo_ioctl }, /* ioctl */
196 { &vnop_select_desc, (VOPFUNC)fifo_select }, /* select */
197 { &vnop_revoke_desc, (VOPFUNC)fifo_revoke }, /* revoke */
198 { &vnop_mmap_desc, (VOPFUNC)fifo_mmap }, /* mmap */
199 { &vnop_fsync_desc, (VOPFUNC)ffs_fsync }, /* fsync */
200 { &vnop_remove_desc, (VOPFUNC)fifo_remove }, /* remove */
201 { &vnop_link_desc, (VOPFUNC)fifo_link }, /* link */
202 { &vnop_rename_desc, (VOPFUNC)fifo_rename }, /* rename */
203 { &vnop_mkdir_desc, (VOPFUNC)fifo_mkdir }, /* mkdir */
204 { &vnop_rmdir_desc, (VOPFUNC)fifo_rmdir }, /* rmdir */
205 { &vnop_symlink_desc, (VOPFUNC)fifo_symlink }, /* symlink */
206 { &vnop_readdir_desc, (VOPFUNC)fifo_readdir }, /* readdir */
207 { &vnop_readlink_desc, (VOPFUNC)fifo_readlink }, /* readlink */
208 { &vnop_inactive_desc, (VOPFUNC)ufs_inactive }, /* inactive */
209 { &vnop_reclaim_desc, (VOPFUNC)ffs_reclaim }, /* reclaim */
210 { &vnop_strategy_desc, (VOPFUNC)fifo_strategy }, /* strategy */
211 { &vnop_pathconf_desc, (VOPFUNC)fifo_pathconf }, /* pathconf */
212 { &vnop_advlock_desc, (VOPFUNC)err_advlock }, /* advlock */
213 { &vnop_bwrite_desc, (VOPFUNC)vn_bwrite },
214 { &vnop_pagein_desc, (VOPFUNC)ffs_pagein }, /* Pagein */
215 { &vnop_pageout_desc, (VOPFUNC)ffs_pageout }, /* Pageout */
216 { &vnop_copyfile_desc, (VOPFUNC)err_copyfile }, /* Copy File */
217 { &vnop_blktooff_desc, (VOPFUNC)ffs_blktooff }, /* blktooff */
218 { &vnop_offtoblk_desc, (VOPFUNC)ffs_offtoblk }, /* offtoblk */
219 { &vnop_blockmap_desc, (VOPFUNC)ufs_blockmap }, /* blockmap */
220 { &vnop_kqfilt_add_desc, (VOPFUNC)ufsfifo_kqfilt_add }, /* kqfilt_add */
1c79356b
A
221 { (struct vnodeop_desc*)NULL, (int(*)())NULL }
222};
223struct vnodeopv_desc ffs_fifoop_opv_desc =
224 { &ffs_fifoop_p, ffs_fifoop_entries };
225#endif /* FIFO */
226
227/*
228 * Enabling cluster read/write operations.
229 */
230int doclusterread = 0;
231int doclusterwrite = 0;
232
233#include <ufs/ufs/ufs_readwrite.c>
234
235/*
236 * Synch an open file.
237 */
238/* ARGSUSED */
239int
240ffs_fsync(ap)
91447636 241 struct vnop_fsync_args /* {
1c79356b 242 struct vnode *a_vp;
1c79356b 243 int a_waitfor;
91447636 244 vfs_context_t a_context;
1c79356b
A
245 } */ *ap;
246{
91447636
A
247 return(ffs_fsync_internal(ap->a_vp, ap->a_waitfor));
248}
249
250
251int
252ffs_fsync_internal(vnode_t vp, int waitfor)
253{
1c79356b 254 struct timeval tv;
91447636 255 int wait = (waitfor == MNT_WAIT);
1c79356b
A
256
257 /*
258 * Write out any clusters.
259 */
91447636 260 cluster_push(vp, 0);
1c79356b 261
1c79356b
A
262 /*
263 * Flush all dirty buffers associated with a vnode.
264 */
91447636
A
265 buf_flushdirtyblks(vp, wait, 0, (char *)"ffs_fsync");
266 microtime(&tv);
267
268 return (ffs_update(vp, &tv, &tv, wait));
1c79356b
A
269}
270
271/*
272 * Reclaim an inode so that it can be used for other purposes.
273 */
274int
275ffs_reclaim(ap)
91447636 276 struct vnop_reclaim_args /* {
1c79356b 277 struct vnode *a_vp;
91447636 278 vfs_context_t a_context;
1c79356b
A
279 } */ *ap;
280{
281 register struct vnode *vp = ap->a_vp;
282 int error;
283
91447636 284 if ( (error = ufs_reclaim(vp, vfs_context_proc(ap->a_context))) )
1c79356b 285 return (error);
91447636
A
286
287 FREE_ZONE(vnode_fsnode(vp), sizeof (struct inode), M_FFSNODE);
288
289 vnode_clearfsnode(vp);
290
1c79356b
A
291 return (0);
292}
293
294/* Blktooff converts a logical block number to a file offset */
295int
296ffs_blktooff(ap)
91447636 297 struct vnop_blktooff_args /* {
1c79356b 298 struct vnode *a_vp;
91447636 299 daddr64_t a_lblkno;
1c79356b
A
300 off_t *a_offset;
301 } */ *ap;
302{
303 register struct inode *ip;
304 register FS *fs;
1c79356b
A
305
306 if (ap->a_vp == NULL)
307 return (EINVAL);
308
91447636 309 fs = VTOI(ap->a_vp)->I_FS;
1c79356b 310
91447636 311 *ap->a_offset = (off_t)lblktosize(fs, ap->a_lblkno);
1c79356b 312
1c79356b
A
313 return (0);
314}
315
316/* Blktooff converts a logical block number to a file offset */
317int
318ffs_offtoblk(ap)
91447636 319 struct vnop_offtoblk_args /* {
1c79356b
A
320 struct vnode *a_vp;
321 off_t a_offset;
91447636 322 daddr64_t *a_lblkno;
1c79356b
A
323 } */ *ap;
324{
91447636 325 register FS *fs;
1c79356b
A
326
327 if (ap->a_vp == NULL)
328 return (EINVAL);
329
91447636
A
330 fs = VTOI(ap->a_vp)->I_FS;
331
332 *ap->a_lblkno = (daddr64_t)lblkno(fs, ap->a_offset);
1c79356b 333
1c79356b
A
334 return (0);
335}