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