2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1982, 1986, 1989, 1993
28 * The Regents of the University of California. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * @(#)ffs_vnops.c 8.15 (Berkeley) 5/14/95
61 #include <rev_endian_fs.h>
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/resourcevar.h>
65 #include <sys/kernel.h>
71 #include <sys/mount.h>
72 #include <sys/vnode.h>
73 #include <sys/malloc.h>
75 #include <sys/quota.h>
78 #include <vfs/vfs_support.h>
80 #include <miscfs/specfs/specdev.h>
81 #include <miscfs/fifofs/fifo.h>
83 #include <ufs/ufs/lockf.h>
84 #include <ufs/ufs/quota.h>
85 #include <ufs/ufs/inode.h>
86 #include <ufs/ufs/dir.h>
87 #include <ufs/ufs/ufsmount.h>
88 #include <ufs/ufs/ufs_extern.h>
90 #include <ufs/ffs/fs.h>
91 #include <ufs/ffs/ffs_extern.h>
93 #include <ufs/ufs/ufs_byte_order.h>
94 #include <architecture/byte_order.h>
95 #endif /* REV_ENDIAN_FS */
97 #define VOPFUNC int (*)(void *)
99 /* Global vfs data structures for ufs. */
100 int (**ffs_vnodeop_p
)(void *);
101 struct vnodeopv_entry_desc ffs_vnodeop_entries
[] = {
102 { &vop_default_desc
, (VOPFUNC
)vn_default_error
},
103 { &vop_lookup_desc
, (VOPFUNC
)ufs_lookup
}, /* lookup */
104 { &vop_create_desc
, (VOPFUNC
)ufs_create
}, /* create */
105 { &vop_whiteout_desc
, (VOPFUNC
)ufs_whiteout
}, /* whiteout */
106 { &vop_mknod_desc
, (VOPFUNC
)ufs_mknod
}, /* mknod */
107 { &vop_open_desc
, (VOPFUNC
)ufs_open
}, /* open */
108 { &vop_close_desc
, (VOPFUNC
)ufs_close
}, /* close */
109 { &vop_access_desc
, (VOPFUNC
)ufs_access
}, /* access */
110 { &vop_getattr_desc
, (VOPFUNC
)ufs_getattr
}, /* getattr */
111 { &vop_setattr_desc
, (VOPFUNC
)ufs_setattr
}, /* setattr */
112 { &vop_read_desc
, (VOPFUNC
)ffs_read
}, /* read */
113 { &vop_write_desc
, (VOPFUNC
)ffs_write
}, /* write */
114 { &vop_lease_desc
, (VOPFUNC
)ufs_lease_check
}, /* lease */
115 { &vop_ioctl_desc
, (VOPFUNC
)ufs_ioctl
}, /* ioctl */
116 { &vop_select_desc
, (VOPFUNC
)ufs_select
}, /* select */
117 { &vop_revoke_desc
, (VOPFUNC
)ufs_revoke
}, /* revoke */
118 { &vop_mmap_desc
, (VOPFUNC
)ufs_mmap
}, /* mmap */
119 { &vop_fsync_desc
, (VOPFUNC
)ffs_fsync
}, /* fsync */
120 { &vop_seek_desc
, (VOPFUNC
)ufs_seek
}, /* seek */
121 { &vop_remove_desc
, (VOPFUNC
)ufs_remove
}, /* remove */
122 { &vop_link_desc
, (VOPFUNC
)ufs_link
}, /* link */
123 { &vop_rename_desc
, (VOPFUNC
)ufs_rename
}, /* rename */
124 { &vop_mkdir_desc
, (VOPFUNC
)ufs_mkdir
}, /* mkdir */
125 { &vop_rmdir_desc
, (VOPFUNC
)ufs_rmdir
}, /* rmdir */
126 { &vop_symlink_desc
, (VOPFUNC
)ufs_symlink
}, /* symlink */
127 { &vop_readdir_desc
, (VOPFUNC
)ufs_readdir
}, /* readdir */
128 { &vop_readlink_desc
, (VOPFUNC
)ufs_readlink
}, /* readlink */
129 { &vop_abortop_desc
, (VOPFUNC
)nop_abortop
}, /* abortop */
130 { &vop_inactive_desc
, (VOPFUNC
)ufs_inactive
}, /* inactive */
131 { &vop_reclaim_desc
, (VOPFUNC
)ffs_reclaim
}, /* reclaim */
132 { &vop_lock_desc
, (VOPFUNC
)ufs_lock
}, /* lock */
133 { &vop_unlock_desc
, (VOPFUNC
)ufs_unlock
}, /* unlock */
134 { &vop_bmap_desc
, (VOPFUNC
)ufs_bmap
}, /* bmap */
135 { &vop_strategy_desc
, (VOPFUNC
)ufs_strategy
}, /* strategy */
136 { &vop_print_desc
, (VOPFUNC
)ufs_print
}, /* print */
137 { &vop_islocked_desc
, (VOPFUNC
)ufs_islocked
}, /* islocked */
138 { &vop_pathconf_desc
, (VOPFUNC
)ufs_pathconf
}, /* pathconf */
139 { &vop_advlock_desc
, (VOPFUNC
)ufs_advlock
}, /* advlock */
140 { &vop_blkatoff_desc
, (VOPFUNC
)ffs_blkatoff
}, /* blkatoff */
141 { &vop_valloc_desc
, (VOPFUNC
)ffs_valloc
}, /* valloc */
142 { &vop_reallocblks_desc
, (VOPFUNC
)ffs_reallocblks
}, /* reallocblks */
143 { &vop_vfree_desc
, (VOPFUNC
)ffs_vfree
}, /* vfree */
144 { &vop_truncate_desc
, (VOPFUNC
)ffs_truncate
}, /* truncate */
145 { &vop_update_desc
, (VOPFUNC
)ffs_update
}, /* update */
146 { &vop_bwrite_desc
, (VOPFUNC
)vn_bwrite
},
147 { &vop_pagein_desc
, (VOPFUNC
)ffs_pagein
}, /* Pagein */
148 { &vop_pageout_desc
, (VOPFUNC
)ffs_pageout
}, /* Pageout */
149 { &vop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* Copy File */
150 { &vop_blktooff_desc
, (VOPFUNC
)ffs_blktooff
}, /* blktooff */
151 { &vop_offtoblk_desc
, (VOPFUNC
)ffs_offtoblk
}, /* offtoblk */
152 { &vop_cmap_desc
, (VOPFUNC
)ufs_cmap
}, /* cmap */
153 { &vop_getattrlist_desc
, (VOPFUNC
)ufs_getattrlist
}, /* getattrlist */
154 { &vop_setattrlist_desc
, (VOPFUNC
)ufs_setattrlist
}, /* setattrlist */
155 { &vop_kqfilt_add_desc
, (VOPFUNC
)ufs_kqfilt_add
}, /* kqfilt_add */
156 { (struct vnodeop_desc
*)NULL
, (int(*)())NULL
}
158 struct vnodeopv_desc ffs_vnodeop_opv_desc
=
159 { &ffs_vnodeop_p
, ffs_vnodeop_entries
};
161 int (**ffs_specop_p
)(void *);
162 struct vnodeopv_entry_desc ffs_specop_entries
[] = {
163 { &vop_default_desc
, (VOPFUNC
)vn_default_error
},
164 { &vop_lookup_desc
, (VOPFUNC
)spec_lookup
}, /* lookup */
165 { &vop_create_desc
, (VOPFUNC
)spec_create
}, /* create */
166 { &vop_mknod_desc
, (VOPFUNC
)spec_mknod
}, /* mknod */
167 { &vop_open_desc
, (VOPFUNC
)spec_open
}, /* open */
168 { &vop_close_desc
, (VOPFUNC
)ufsspec_close
}, /* close */
169 { &vop_access_desc
, (VOPFUNC
)ufs_access
}, /* access */
170 { &vop_getattr_desc
, (VOPFUNC
)ufs_getattr
}, /* getattr */
171 { &vop_setattr_desc
, (VOPFUNC
)ufs_setattr
}, /* setattr */
172 { &vop_read_desc
, (VOPFUNC
)ufsspec_read
}, /* read */
173 { &vop_write_desc
, (VOPFUNC
)ufsspec_write
}, /* write */
174 { &vop_lease_desc
, (VOPFUNC
)spec_lease_check
}, /* lease */
175 { &vop_ioctl_desc
, (VOPFUNC
)spec_ioctl
}, /* ioctl */
176 { &vop_select_desc
, (VOPFUNC
)spec_select
}, /* select */
177 { &vop_revoke_desc
, (VOPFUNC
)spec_revoke
}, /* revoke */
178 { &vop_mmap_desc
, (VOPFUNC
)spec_mmap
}, /* mmap */
179 { &vop_fsync_desc
, (VOPFUNC
)ffs_fsync
}, /* fsync */
180 { &vop_seek_desc
, (VOPFUNC
)spec_seek
}, /* seek */
181 { &vop_remove_desc
, (VOPFUNC
)spec_remove
}, /* remove */
182 { &vop_link_desc
, (VOPFUNC
)spec_link
}, /* link */
183 { &vop_rename_desc
, (VOPFUNC
)spec_rename
}, /* rename */
184 { &vop_mkdir_desc
, (VOPFUNC
)spec_mkdir
}, /* mkdir */
185 { &vop_rmdir_desc
, (VOPFUNC
)spec_rmdir
}, /* rmdir */
186 { &vop_symlink_desc
, (VOPFUNC
)spec_symlink
}, /* symlink */
187 { &vop_readdir_desc
, (VOPFUNC
)spec_readdir
}, /* readdir */
188 { &vop_readlink_desc
, (VOPFUNC
)spec_readlink
}, /* readlink */
189 { &vop_abortop_desc
, (VOPFUNC
)spec_abortop
}, /* abortop */
190 { &vop_inactive_desc
, (VOPFUNC
)ufs_inactive
}, /* inactive */
191 { &vop_reclaim_desc
, (VOPFUNC
)ffs_reclaim
}, /* reclaim */
192 { &vop_lock_desc
, (VOPFUNC
)ufs_lock
}, /* lock */
193 { &vop_unlock_desc
, (VOPFUNC
)ufs_unlock
}, /* unlock */
194 { &vop_bmap_desc
, (VOPFUNC
)spec_bmap
}, /* bmap */
195 { &vop_strategy_desc
, (VOPFUNC
)spec_strategy
}, /* strategy */
196 { &vop_print_desc
, (VOPFUNC
)ufs_print
}, /* print */
197 { &vop_islocked_desc
, (VOPFUNC
)ufs_islocked
}, /* islocked */
198 { &vop_pathconf_desc
, (VOPFUNC
)spec_pathconf
}, /* pathconf */
199 { &vop_advlock_desc
, (VOPFUNC
)spec_advlock
}, /* advlock */
200 { &vop_blkatoff_desc
, (VOPFUNC
)spec_blkatoff
}, /* blkatoff */
201 { &vop_valloc_desc
, (VOPFUNC
)spec_valloc
}, /* valloc */
202 { &vop_reallocblks_desc
, (VOPFUNC
)spec_reallocblks
}, /* reallocblks */
203 { &vop_vfree_desc
, (VOPFUNC
)ffs_vfree
}, /* vfree */
204 { &vop_truncate_desc
, (VOPFUNC
)spec_truncate
}, /* truncate */
205 { &vop_update_desc
, (VOPFUNC
)ffs_update
}, /* update */
206 { &vop_bwrite_desc
, (VOPFUNC
)vn_bwrite
},
207 { &vop_devblocksize_desc
, (VOPFUNC
)spec_devblocksize
}, /* devblocksize */
208 { &vop_pagein_desc
, (VOPFUNC
)ffs_pagein
}, /* Pagein */
209 { &vop_pageout_desc
, (VOPFUNC
)ffs_pageout
}, /* Pageout */
210 { &vop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* Copy File */
211 { &vop_blktooff_desc
, (VOPFUNC
)ffs_blktooff
}, /* blktooff */
212 { &vop_offtoblk_desc
, (VOPFUNC
)ffs_offtoblk
}, /* offtoblk */
213 { &vop_cmap_desc
, (VOPFUNC
)spec_cmap
}, /* cmap */
214 { (struct vnodeop_desc
*)NULL
, (int(*)())NULL
}
216 struct vnodeopv_desc ffs_specop_opv_desc
=
217 { &ffs_specop_p
, ffs_specop_entries
};
220 int (**ffs_fifoop_p
)(void *);
221 struct vnodeopv_entry_desc ffs_fifoop_entries
[] = {
222 { &vop_default_desc
, (VOPFUNC
)vn_default_error
},
223 { &vop_lookup_desc
, (VOPFUNC
)fifo_lookup
}, /* lookup */
224 { &vop_create_desc
, (VOPFUNC
)fifo_create
}, /* create */
225 { &vop_mknod_desc
, (VOPFUNC
)fifo_mknod
}, /* mknod */
226 { &vop_open_desc
, (VOPFUNC
)fifo_open
}, /* open */
227 { &vop_close_desc
, (VOPFUNC
)ufsfifo_close
}, /* close */
228 { &vop_access_desc
, (VOPFUNC
)ufs_access
}, /* access */
229 { &vop_getattr_desc
, (VOPFUNC
)ufs_getattr
}, /* getattr */
230 { &vop_setattr_desc
, (VOPFUNC
)ufs_setattr
}, /* setattr */
231 { &vop_read_desc
, (VOPFUNC
)ufsfifo_read
}, /* read */
232 { &vop_write_desc
, (VOPFUNC
)ufsfifo_write
}, /* write */
233 { &vop_lease_desc
, (VOPFUNC
)fifo_lease_check
}, /* lease */
234 { &vop_ioctl_desc
, (VOPFUNC
)fifo_ioctl
}, /* ioctl */
235 { &vop_select_desc
, (VOPFUNC
)fifo_select
}, /* select */
236 { &vop_revoke_desc
, (VOPFUNC
)fifo_revoke
}, /* revoke */
237 { &vop_mmap_desc
, (VOPFUNC
)fifo_mmap
}, /* mmap */
238 { &vop_fsync_desc
, (VOPFUNC
)ffs_fsync
}, /* fsync */
239 { &vop_seek_desc
, (VOPFUNC
)fifo_seek
}, /* seek */
240 { &vop_remove_desc
, (VOPFUNC
)fifo_remove
}, /* remove */
241 { &vop_link_desc
, (VOPFUNC
)fifo_link
}, /* link */
242 { &vop_rename_desc
, (VOPFUNC
)fifo_rename
}, /* rename */
243 { &vop_mkdir_desc
, (VOPFUNC
)fifo_mkdir
}, /* mkdir */
244 { &vop_rmdir_desc
, (VOPFUNC
)fifo_rmdir
}, /* rmdir */
245 { &vop_symlink_desc
, (VOPFUNC
)fifo_symlink
}, /* symlink */
246 { &vop_readdir_desc
, (VOPFUNC
)fifo_readdir
}, /* readdir */
247 { &vop_readlink_desc
, (VOPFUNC
)fifo_readlink
}, /* readlink */
248 { &vop_abortop_desc
, (VOPFUNC
)fifo_abortop
}, /* abortop */
249 { &vop_inactive_desc
, (VOPFUNC
)ufs_inactive
}, /* inactive */
250 { &vop_reclaim_desc
, (VOPFUNC
)ffs_reclaim
}, /* reclaim */
251 { &vop_lock_desc
, (VOPFUNC
)ufs_lock
}, /* lock */
252 { &vop_unlock_desc
, (VOPFUNC
)ufs_unlock
}, /* unlock */
253 { &vop_bmap_desc
, (VOPFUNC
)fifo_bmap
}, /* bmap */
254 { &vop_strategy_desc
, (VOPFUNC
)fifo_strategy
}, /* strategy */
255 { &vop_print_desc
, (VOPFUNC
)ufs_print
}, /* print */
256 { &vop_islocked_desc
, (VOPFUNC
)ufs_islocked
}, /* islocked */
257 { &vop_pathconf_desc
, (VOPFUNC
)fifo_pathconf
}, /* pathconf */
258 { &vop_advlock_desc
, (VOPFUNC
)fifo_advlock
}, /* advlock */
259 { &vop_blkatoff_desc
, (VOPFUNC
)fifo_blkatoff
}, /* blkatoff */
260 { &vop_valloc_desc
, (VOPFUNC
)fifo_valloc
}, /* valloc */
261 { &vop_reallocblks_desc
, (VOPFUNC
)fifo_reallocblks
}, /* reallocblks */
262 { &vop_vfree_desc
, (VOPFUNC
)ffs_vfree
}, /* vfree */
263 { &vop_truncate_desc
, (VOPFUNC
)fifo_truncate
}, /* truncate */
264 { &vop_update_desc
, (VOPFUNC
)ffs_update
}, /* update */
265 { &vop_bwrite_desc
, (VOPFUNC
)vn_bwrite
},
266 { &vop_pagein_desc
, (VOPFUNC
)ffs_pagein
}, /* Pagein */
267 { &vop_pageout_desc
, (VOPFUNC
)ffs_pageout
}, /* Pageout */
268 { &vop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* Copy File */
269 { &vop_blktooff_desc
, (VOPFUNC
)ffs_blktooff
}, /* blktooff */
270 { &vop_offtoblk_desc
, (VOPFUNC
)ffs_offtoblk
}, /* offtoblk */
271 { &vop_cmap_desc
, (VOPFUNC
)ufs_cmap
}, /* cmap */
272 { &vop_kqfilt_add_desc
, (VOPFUNC
)ufsfifo_kqfilt_add
}, /* kqfilt_add */
273 { (struct vnodeop_desc
*)NULL
, (int(*)())NULL
}
275 struct vnodeopv_desc ffs_fifoop_opv_desc
=
276 { &ffs_fifoop_p
, ffs_fifoop_entries
};
280 * Enabling cluster read/write operations.
282 int doclusterread
= 0;
283 int doclusterwrite
= 0;
285 #include <ufs/ufs/ufs_readwrite.c>
288 * Synch an open file.
293 struct vop_fsync_args
/* {
295 struct ucred *a_cred;
300 register struct vnode
*vp
= ap
->a_vp
;
301 register struct buf
*bp
;
305 struct inode
*ip
= VTOI(vp
);
309 * Write out any clusters.
314 * Flush all dirty buffers associated with a vnode.
318 for (bp
= vp
->v_dirtyblkhd
.lh_first
; bp
; bp
= nbp
) {
319 nbp
= bp
->b_vnbufs
.le_next
;
320 if ((bp
->b_flags
& B_BUSY
))
322 if ((bp
->b_flags
& B_DELWRI
) == 0)
323 panic("ffs_fsync: not dirty");
325 bp
->b_flags
|= B_BUSY
;
328 * Wait for I/O associated with indirect blocks to complete,
329 * since there is no way to quickly wait for them below.
331 if (bp
->b_vp
== vp
|| ap
->a_waitfor
== MNT_NOWAIT
)
338 if (ap
->a_waitfor
== MNT_WAIT
) {
339 while (vp
->v_numoutput
) {
340 vp
->v_flag
|= VBWAIT
;
341 tsleep((caddr_t
)&vp
->v_numoutput
, PRIBIO
+ 1, "ffs_fsync", 0);
344 if (vp
->v_dirtyblkhd
.lh_first
) {
345 /* still have some dirty buffers */
347 vprint("ffs_fsync: dirty", vp
);
350 * Looks like the requests are not
351 * getting queued to the driver.
352 * Retrying here causes a cpu bound loop.
353 * Yield to the other threads and hope
356 (void)tsleep((caddr_t
)&vp
->v_numoutput
,
357 PRIBIO
+ 1, "ffs_fsync", hz
/10);
368 return (VOP_UPDATE(ap
->a_vp
, &tv
, &tv
, ap
->a_waitfor
== MNT_WAIT
));
372 * Reclaim an inode so that it can be used for other purposes.
376 struct vop_reclaim_args
/* {
381 register struct vnode
*vp
= ap
->a_vp
;
384 if (error
= ufs_reclaim(vp
, ap
->a_p
))
386 FREE_ZONE(vp
->v_data
, sizeof (struct inode
),
387 VFSTOUFS(vp
->v_mount
)->um_devvp
->v_tag
== VT_MFS
?
388 M_MFSNODE
: M_FFSNODE
);
393 /* Blktooff converts a logical block number to a file offset */
396 struct vop_blktooff_args
/* {
402 register struct inode
*ip
;
407 if (ap
->a_vp
== NULL
)
415 panic("-ve blkno in ffs_blktooff");
419 *ap
->a_offset
= (off_t
)lblktosize(fs
, bn
);
423 /* Blktooff converts a logical block number to a file offset */
426 struct vop_offtoblk_args
/* {
432 register struct inode
*ip
;
435 if (ap
->a_vp
== NULL
)
441 *ap
->a_lblkno
= (daddr_t
)lblkno(fs
, ap
->a_offset
);