2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1982, 1986, 1989, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
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.
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
55 * @(#)ffs_vnops.c 8.15 (Berkeley) 5/14/95
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>
68 #include <sys/mount.h>
69 #include <sys/vnode.h>
70 #include <sys/malloc.h>
74 #include <vfs/vfs_support.h>
76 #include <miscfs/specfs/specdev.h>
77 #include <miscfs/fifofs/fifo.h>
79 #include <ufs/ufs/lockf.h>
80 #include <ufs/ufs/quota.h>
81 #include <ufs/ufs/inode.h>
82 #include <ufs/ufs/dir.h>
83 #include <ufs/ufs/ufsmount.h>
84 #include <ufs/ufs/ufs_extern.h>
86 #include <ufs/ffs/fs.h>
87 #include <ufs/ffs/ffs_extern.h>
89 #include <ufs/ufs/ufs_byte_order.h>
90 #include <architecture/byte_order.h>
91 #endif /* REV_ENDIAN_FS */
93 #define VOPFUNC int (*)(void *)
95 /* Global vfs data structures for ufs. */
96 int (**ffs_vnodeop_p
)(void *);
97 struct vnodeopv_entry_desc ffs_vnodeop_entries
[] = {
98 { &vop_default_desc
, (VOPFUNC
)vn_default_error
},
99 { &vop_lookup_desc
, (VOPFUNC
)ufs_lookup
}, /* lookup */
100 { &vop_create_desc
, (VOPFUNC
)ufs_create
}, /* create */
101 { &vop_whiteout_desc
, (VOPFUNC
)ufs_whiteout
}, /* whiteout */
102 { &vop_mknod_desc
, (VOPFUNC
)ufs_mknod
}, /* mknod */
103 { &vop_open_desc
, (VOPFUNC
)ufs_open
}, /* open */
104 { &vop_close_desc
, (VOPFUNC
)ufs_close
}, /* close */
105 { &vop_access_desc
, (VOPFUNC
)ufs_access
}, /* access */
106 { &vop_getattr_desc
, (VOPFUNC
)ufs_getattr
}, /* getattr */
107 { &vop_setattr_desc
, (VOPFUNC
)ufs_setattr
}, /* setattr */
108 { &vop_read_desc
, (VOPFUNC
)ffs_read
}, /* read */
109 { &vop_write_desc
, (VOPFUNC
)ffs_write
}, /* write */
110 { &vop_lease_desc
, (VOPFUNC
)ufs_lease_check
}, /* lease */
111 { &vop_ioctl_desc
, (VOPFUNC
)ufs_ioctl
}, /* ioctl */
112 { &vop_select_desc
, (VOPFUNC
)ufs_select
}, /* select */
113 { &vop_revoke_desc
, (VOPFUNC
)ufs_revoke
}, /* revoke */
114 { &vop_mmap_desc
, (VOPFUNC
)ufs_mmap
}, /* mmap */
115 { &vop_fsync_desc
, (VOPFUNC
)ffs_fsync
}, /* fsync */
116 { &vop_seek_desc
, (VOPFUNC
)ufs_seek
}, /* seek */
117 { &vop_remove_desc
, (VOPFUNC
)ufs_remove
}, /* remove */
118 { &vop_link_desc
, (VOPFUNC
)ufs_link
}, /* link */
119 { &vop_rename_desc
, (VOPFUNC
)ufs_rename
}, /* rename */
120 { &vop_mkdir_desc
, (VOPFUNC
)ufs_mkdir
}, /* mkdir */
121 { &vop_rmdir_desc
, (VOPFUNC
)ufs_rmdir
}, /* rmdir */
122 { &vop_symlink_desc
, (VOPFUNC
)ufs_symlink
}, /* symlink */
123 { &vop_readdir_desc
, (VOPFUNC
)ufs_readdir
}, /* readdir */
124 { &vop_readlink_desc
, (VOPFUNC
)ufs_readlink
}, /* readlink */
125 { &vop_abortop_desc
, (VOPFUNC
)ufs_abortop
}, /* abortop */
126 { &vop_inactive_desc
, (VOPFUNC
)ufs_inactive
}, /* inactive */
127 { &vop_reclaim_desc
, (VOPFUNC
)ffs_reclaim
}, /* reclaim */
128 { &vop_lock_desc
, (VOPFUNC
)ufs_lock
}, /* lock */
129 { &vop_unlock_desc
, (VOPFUNC
)ufs_unlock
}, /* unlock */
130 { &vop_bmap_desc
, (VOPFUNC
)ufs_bmap
}, /* bmap */
131 { &vop_strategy_desc
, (VOPFUNC
)ufs_strategy
}, /* strategy */
132 { &vop_print_desc
, (VOPFUNC
)ufs_print
}, /* print */
133 { &vop_islocked_desc
, (VOPFUNC
)ufs_islocked
}, /* islocked */
134 { &vop_pathconf_desc
, (VOPFUNC
)ufs_pathconf
}, /* pathconf */
135 { &vop_advlock_desc
, (VOPFUNC
)ufs_advlock
}, /* advlock */
136 { &vop_blkatoff_desc
, (VOPFUNC
)ffs_blkatoff
}, /* blkatoff */
137 { &vop_valloc_desc
, (VOPFUNC
)ffs_valloc
}, /* valloc */
138 { &vop_reallocblks_desc
, (VOPFUNC
)ffs_reallocblks
}, /* reallocblks */
139 { &vop_vfree_desc
, (VOPFUNC
)ffs_vfree
}, /* vfree */
140 { &vop_truncate_desc
, (VOPFUNC
)ffs_truncate
}, /* truncate */
141 { &vop_update_desc
, (VOPFUNC
)ffs_update
}, /* update */
142 { &vop_bwrite_desc
, (VOPFUNC
)vn_bwrite
},
143 { &vop_pagein_desc
, (VOPFUNC
)ffs_pagein
}, /* Pagein */
144 { &vop_pageout_desc
, (VOPFUNC
)ffs_pageout
}, /* Pageout */
145 { &vop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* Copy File */
146 { &vop_blktooff_desc
, (VOPFUNC
)ffs_blktooff
}, /* blktooff */
147 { &vop_offtoblk_desc
, (VOPFUNC
)ffs_offtoblk
}, /* offtoblk */
148 { &vop_cmap_desc
, (VOPFUNC
)ufs_cmap
}, /* cmap */
149 { (struct vnodeop_desc
*)NULL
, (int(*)())NULL
}
151 struct vnodeopv_desc ffs_vnodeop_opv_desc
=
152 { &ffs_vnodeop_p
, ffs_vnodeop_entries
};
154 int (**ffs_specop_p
)(void *);
155 struct vnodeopv_entry_desc ffs_specop_entries
[] = {
156 { &vop_default_desc
, (VOPFUNC
)vn_default_error
},
157 { &vop_lookup_desc
, (VOPFUNC
)spec_lookup
}, /* lookup */
158 { &vop_create_desc
, (VOPFUNC
)spec_create
}, /* create */
159 { &vop_mknod_desc
, (VOPFUNC
)spec_mknod
}, /* mknod */
160 { &vop_open_desc
, (VOPFUNC
)spec_open
}, /* open */
161 { &vop_close_desc
, (VOPFUNC
)ufsspec_close
}, /* close */
162 { &vop_access_desc
, (VOPFUNC
)ufs_access
}, /* access */
163 { &vop_getattr_desc
, (VOPFUNC
)ufs_getattr
}, /* getattr */
164 { &vop_setattr_desc
, (VOPFUNC
)ufs_setattr
}, /* setattr */
165 { &vop_read_desc
, (VOPFUNC
)ufsspec_read
}, /* read */
166 { &vop_write_desc
, (VOPFUNC
)ufsspec_write
}, /* write */
167 { &vop_lease_desc
, (VOPFUNC
)spec_lease_check
}, /* lease */
168 { &vop_ioctl_desc
, (VOPFUNC
)spec_ioctl
}, /* ioctl */
169 { &vop_select_desc
, (VOPFUNC
)spec_select
}, /* select */
170 { &vop_revoke_desc
, (VOPFUNC
)spec_revoke
}, /* revoke */
171 { &vop_mmap_desc
, (VOPFUNC
)spec_mmap
}, /* mmap */
172 { &vop_fsync_desc
, (VOPFUNC
)ffs_fsync
}, /* fsync */
173 { &vop_seek_desc
, (VOPFUNC
)spec_seek
}, /* seek */
174 { &vop_remove_desc
, (VOPFUNC
)spec_remove
}, /* remove */
175 { &vop_link_desc
, (VOPFUNC
)spec_link
}, /* link */
176 { &vop_rename_desc
, (VOPFUNC
)spec_rename
}, /* rename */
177 { &vop_mkdir_desc
, (VOPFUNC
)spec_mkdir
}, /* mkdir */
178 { &vop_rmdir_desc
, (VOPFUNC
)spec_rmdir
}, /* rmdir */
179 { &vop_symlink_desc
, (VOPFUNC
)spec_symlink
}, /* symlink */
180 { &vop_readdir_desc
, (VOPFUNC
)spec_readdir
}, /* readdir */
181 { &vop_readlink_desc
, (VOPFUNC
)spec_readlink
}, /* readlink */
182 { &vop_abortop_desc
, (VOPFUNC
)spec_abortop
}, /* abortop */
183 { &vop_inactive_desc
, (VOPFUNC
)ufs_inactive
}, /* inactive */
184 { &vop_reclaim_desc
, (VOPFUNC
)ffs_reclaim
}, /* reclaim */
185 { &vop_lock_desc
, (VOPFUNC
)ufs_lock
}, /* lock */
186 { &vop_unlock_desc
, (VOPFUNC
)ufs_unlock
}, /* unlock */
187 { &vop_bmap_desc
, (VOPFUNC
)spec_bmap
}, /* bmap */
188 { &vop_strategy_desc
, (VOPFUNC
)spec_strategy
}, /* strategy */
189 { &vop_print_desc
, (VOPFUNC
)ufs_print
}, /* print */
190 { &vop_islocked_desc
, (VOPFUNC
)ufs_islocked
}, /* islocked */
191 { &vop_pathconf_desc
, (VOPFUNC
)spec_pathconf
}, /* pathconf */
192 { &vop_advlock_desc
, (VOPFUNC
)spec_advlock
}, /* advlock */
193 { &vop_blkatoff_desc
, (VOPFUNC
)spec_blkatoff
}, /* blkatoff */
194 { &vop_valloc_desc
, (VOPFUNC
)spec_valloc
}, /* valloc */
195 { &vop_reallocblks_desc
, (VOPFUNC
)spec_reallocblks
}, /* reallocblks */
196 { &vop_vfree_desc
, (VOPFUNC
)ffs_vfree
}, /* vfree */
197 { &vop_truncate_desc
, (VOPFUNC
)spec_truncate
}, /* truncate */
198 { &vop_update_desc
, (VOPFUNC
)ffs_update
}, /* update */
199 { &vop_bwrite_desc
, (VOPFUNC
)vn_bwrite
},
200 { &vop_devblocksize_desc
, (VOPFUNC
)spec_devblocksize
}, /* devblocksize */
201 { &vop_pagein_desc
, (VOPFUNC
)ffs_pagein
}, /* Pagein */
202 { &vop_pageout_desc
, (VOPFUNC
)ffs_pageout
}, /* Pageout */
203 { &vop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* Copy File */
204 { &vop_blktooff_desc
, (VOPFUNC
)ffs_blktooff
}, /* blktooff */
205 { &vop_offtoblk_desc
, (VOPFUNC
)ffs_offtoblk
}, /* offtoblk */
206 { &vop_cmap_desc
, (VOPFUNC
)spec_cmap
}, /* cmap */
207 { (struct vnodeop_desc
*)NULL
, (int(*)())NULL
}
209 struct vnodeopv_desc ffs_specop_opv_desc
=
210 { &ffs_specop_p
, ffs_specop_entries
};
213 int (**ffs_fifoop_p
)(void *);
214 struct vnodeopv_entry_desc ffs_fifoop_entries
[] = {
215 { &vop_default_desc
, (VOPFUNC
)vn_default_error
},
216 { &vop_lookup_desc
, (VOPFUNC
)fifo_lookup
}, /* lookup */
217 { &vop_create_desc
, (VOPFUNC
)fifo_create
}, /* create */
218 { &vop_mknod_desc
, (VOPFUNC
)fifo_mknod
}, /* mknod */
219 { &vop_open_desc
, (VOPFUNC
)fifo_open
}, /* open */
220 { &vop_close_desc
, (VOPFUNC
)ufsfifo_close
}, /* close */
221 { &vop_access_desc
, (VOPFUNC
)ufs_access
}, /* access */
222 { &vop_getattr_desc
, (VOPFUNC
)ufs_getattr
}, /* getattr */
223 { &vop_setattr_desc
, (VOPFUNC
)ufs_setattr
}, /* setattr */
224 { &vop_read_desc
, (VOPFUNC
)ufsfifo_read
}, /* read */
225 { &vop_write_desc
, (VOPFUNC
)ufsfifo_write
}, /* write */
226 { &vop_lease_desc
, (VOPFUNC
)fifo_lease_check
}, /* lease */
227 { &vop_ioctl_desc
, (VOPFUNC
)fifo_ioctl
}, /* ioctl */
228 { &vop_select_desc
, (VOPFUNC
)fifo_select
}, /* select */
229 { &vop_revoke_desc
, (VOPFUNC
)fifo_revoke
}, /* revoke */
230 { &vop_mmap_desc
, (VOPFUNC
)fifo_mmap
}, /* mmap */
231 { &vop_fsync_desc
, (VOPFUNC
)ffs_fsync
}, /* fsync */
232 { &vop_seek_desc
, (VOPFUNC
)fifo_seek
}, /* seek */
233 { &vop_remove_desc
, (VOPFUNC
)fifo_remove
}, /* remove */
234 { &vop_link_desc
, (VOPFUNC
)fifo_link
}, /* link */
235 { &vop_rename_desc
, (VOPFUNC
)fifo_rename
}, /* rename */
236 { &vop_mkdir_desc
, (VOPFUNC
)fifo_mkdir
}, /* mkdir */
237 { &vop_rmdir_desc
, (VOPFUNC
)fifo_rmdir
}, /* rmdir */
238 { &vop_symlink_desc
, (VOPFUNC
)fifo_symlink
}, /* symlink */
239 { &vop_readdir_desc
, (VOPFUNC
)fifo_readdir
}, /* readdir */
240 { &vop_readlink_desc
, (VOPFUNC
)fifo_readlink
}, /* readlink */
241 { &vop_abortop_desc
, (VOPFUNC
)fifo_abortop
}, /* abortop */
242 { &vop_inactive_desc
, (VOPFUNC
)ufs_inactive
}, /* inactive */
243 { &vop_reclaim_desc
, (VOPFUNC
)ffs_reclaim
}, /* reclaim */
244 { &vop_lock_desc
, (VOPFUNC
)ufs_lock
}, /* lock */
245 { &vop_unlock_desc
, (VOPFUNC
)ufs_unlock
}, /* unlock */
246 { &vop_bmap_desc
, (VOPFUNC
)fifo_bmap
}, /* bmap */
247 { &vop_strategy_desc
, (VOPFUNC
)fifo_strategy
}, /* strategy */
248 { &vop_print_desc
, (VOPFUNC
)ufs_print
}, /* print */
249 { &vop_islocked_desc
, (VOPFUNC
)ufs_islocked
}, /* islocked */
250 { &vop_pathconf_desc
, (VOPFUNC
)fifo_pathconf
}, /* pathconf */
251 { &vop_advlock_desc
, (VOPFUNC
)fifo_advlock
}, /* advlock */
252 { &vop_blkatoff_desc
, (VOPFUNC
)fifo_blkatoff
}, /* blkatoff */
253 { &vop_valloc_desc
, (VOPFUNC
)fifo_valloc
}, /* valloc */
254 { &vop_reallocblks_desc
, (VOPFUNC
)fifo_reallocblks
}, /* reallocblks */
255 { &vop_vfree_desc
, (VOPFUNC
)ffs_vfree
}, /* vfree */
256 { &vop_truncate_desc
, (VOPFUNC
)fifo_truncate
}, /* truncate */
257 { &vop_update_desc
, (VOPFUNC
)ffs_update
}, /* update */
258 { &vop_bwrite_desc
, (VOPFUNC
)vn_bwrite
},
259 { &vop_pagein_desc
, (VOPFUNC
)ffs_pagein
}, /* Pagein */
260 { &vop_pageout_desc
, (VOPFUNC
)ffs_pageout
}, /* Pageout */
261 { &vop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* Copy File */
262 { &vop_blktooff_desc
, (VOPFUNC
)ffs_blktooff
}, /* blktooff */
263 { &vop_offtoblk_desc
, (VOPFUNC
)ffs_offtoblk
}, /* offtoblk */
264 { &vop_cmap_desc
, (VOPFUNC
)ufs_cmap
}, /* cmap */
265 { (struct vnodeop_desc
*)NULL
, (int(*)())NULL
}
267 struct vnodeopv_desc ffs_fifoop_opv_desc
=
268 { &ffs_fifoop_p
, ffs_fifoop_entries
};
272 * Enabling cluster read/write operations.
274 int doclusterread
= 0;
275 int doclusterwrite
= 0;
277 #include <ufs/ufs/ufs_readwrite.c>
280 * Synch an open file.
285 struct vop_fsync_args
/* {
287 struct ucred *a_cred;
292 register struct vnode
*vp
= ap
->a_vp
;
293 register struct buf
*bp
;
297 struct inode
*ip
= VTOI(vp
);
300 * Write out any clusters.
305 * Flush all dirty buffers associated with a vnode.
309 for (bp
= vp
->v_dirtyblkhd
.lh_first
; bp
; bp
= nbp
) {
310 nbp
= bp
->b_vnbufs
.le_next
;
311 if ((bp
->b_flags
& B_BUSY
))
313 if ((bp
->b_flags
& B_DELWRI
) == 0)
314 panic("ffs_fsync: not dirty");
316 bp
->b_flags
|= B_BUSY
;
319 * Wait for I/O associated with indirect blocks to complete,
320 * since there is no way to quickly wait for them below.
322 if (bp
->b_vp
== vp
|| ap
->a_waitfor
== MNT_NOWAIT
)
328 if (vp
->v_flag
& VHASDIRTY
)
331 if (ap
->a_waitfor
== MNT_WAIT
) {
332 while (vp
->v_numoutput
) {
333 vp
->v_flag
|= VBWAIT
;
334 tsleep((caddr_t
)&vp
->v_numoutput
, PRIBIO
+ 1, "ffs_fsync", 0);
337 /* I have seen this happen for swapfile. So it is safer to
338 * check for dirty buffers again. --Umesh
340 if (vp
->v_dirtyblkhd
.lh_first
|| (vp
->v_flag
& VHASDIRTY
)) {
341 vprint("ffs_fsync: dirty", vp
);
348 return (VOP_UPDATE(ap
->a_vp
, &tv
, &tv
, ap
->a_waitfor
== MNT_WAIT
));
352 * Reclaim an inode so that it can be used for other purposes.
356 struct vop_reclaim_args
/* {
361 register struct vnode
*vp
= ap
->a_vp
;
364 if (error
= ufs_reclaim(vp
, ap
->a_p
))
366 FREE_ZONE(vp
->v_data
, sizeof (struct inode
),
367 VFSTOUFS(vp
->v_mount
)->um_devvp
->v_tag
== VT_MFS
?
368 M_MFSNODE
: M_FFSNODE
);
373 /* Blktooff converts a logical block number to a file offset */
376 struct vop_blktooff_args
/* {
382 register struct inode
*ip
;
387 if (ap
->a_vp
== NULL
)
395 panic("-ve blkno in ffs_blktooff");
399 *ap
->a_offset
= (off_t
)lblktosize(fs
, bn
);
403 /* Blktooff converts a logical block number to a file offset */
406 struct vop_offtoblk_args
/* {
412 register struct inode
*ip
;
415 if (ap
->a_vp
== NULL
)
421 *ap
->a_lblkno
= (daddr_t
)lblkno(fs
, ap
->a_offset
);