ffs_vnops.c revision 1.117 1 /* $NetBSD: ffs_vnops.c,v 1.117 2011/04/15 15:54:11 hannken Exp $ */
2
3 /*-
4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Wasabi Systems, Inc, and by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1989, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)ffs_vnops.c 8.15 (Berkeley) 5/14/95
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: ffs_vnops.c,v 1.117 2011/04/15 15:54:11 hannken Exp $");
65
66 #if defined(_KERNEL_OPT)
67 #include "opt_ffs.h"
68 #include "opt_wapbl.h"
69 #endif
70
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/resourcevar.h>
74 #include <sys/kernel.h>
75 #include <sys/file.h>
76 #include <sys/stat.h>
77 #include <sys/buf.h>
78 #include <sys/event.h>
79 #include <sys/proc.h>
80 #include <sys/mount.h>
81 #include <sys/vnode.h>
82 #include <sys/pool.h>
83 #include <sys/signalvar.h>
84 #include <sys/kauth.h>
85 #include <sys/wapbl.h>
86 #include <sys/fstrans.h>
87
88 #include <miscfs/fifofs/fifo.h>
89 #include <miscfs/genfs/genfs.h>
90 #include <miscfs/specfs/specdev.h>
91
92 #include <ufs/ufs/inode.h>
93 #include <ufs/ufs/dir.h>
94 #include <ufs/ufs/ufs_extern.h>
95 #include <ufs/ufs/ufsmount.h>
96 #include <ufs/ufs/ufs_wapbl.h>
97
98 #include <ufs/ffs/fs.h>
99 #include <ufs/ffs/ffs_extern.h>
100
101 #include <uvm/uvm.h>
102
103 /* Global vfs data structures for ufs. */
104 int (**ffs_vnodeop_p)(void *);
105 const struct vnodeopv_entry_desc ffs_vnodeop_entries[] = {
106 { &vop_default_desc, vn_default_error },
107 { &vop_lookup_desc, ufs_lookup }, /* lookup */
108 { &vop_create_desc, ufs_create }, /* create */
109 { &vop_whiteout_desc, ufs_whiteout }, /* whiteout */
110 { &vop_mknod_desc, ufs_mknod }, /* mknod */
111 { &vop_open_desc, ufs_open }, /* open */
112 { &vop_close_desc, ufs_close }, /* close */
113 { &vop_access_desc, ufs_access }, /* access */
114 { &vop_getattr_desc, ufs_getattr }, /* getattr */
115 { &vop_setattr_desc, ufs_setattr }, /* setattr */
116 { &vop_read_desc, ffs_read }, /* read */
117 { &vop_write_desc, ffs_write }, /* write */
118 { &vop_ioctl_desc, ufs_ioctl }, /* ioctl */
119 { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
120 { &vop_poll_desc, ufs_poll }, /* poll */
121 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */
122 { &vop_revoke_desc, ufs_revoke }, /* revoke */
123 { &vop_mmap_desc, ufs_mmap }, /* mmap */
124 { &vop_fsync_desc, ffs_fsync }, /* fsync */
125 { &vop_seek_desc, ufs_seek }, /* seek */
126 { &vop_remove_desc, ufs_remove }, /* remove */
127 { &vop_link_desc, ufs_link }, /* link */
128 { &vop_rename_desc, ufs_rename }, /* rename */
129 { &vop_mkdir_desc, ufs_mkdir }, /* mkdir */
130 { &vop_rmdir_desc, ufs_rmdir }, /* rmdir */
131 { &vop_symlink_desc, ufs_symlink }, /* symlink */
132 { &vop_readdir_desc, ufs_readdir }, /* readdir */
133 { &vop_readlink_desc, ufs_readlink }, /* readlink */
134 { &vop_abortop_desc, ufs_abortop }, /* abortop */
135 { &vop_inactive_desc, ufs_inactive }, /* inactive */
136 { &vop_reclaim_desc, ffs_reclaim }, /* reclaim */
137 { &vop_lock_desc, ufs_lock }, /* lock */
138 { &vop_unlock_desc, ufs_unlock }, /* unlock */
139 { &vop_bmap_desc, ufs_bmap }, /* bmap */
140 { &vop_strategy_desc, ufs_strategy }, /* strategy */
141 { &vop_print_desc, ufs_print }, /* print */
142 { &vop_islocked_desc, ufs_islocked }, /* islocked */
143 { &vop_pathconf_desc, ufs_pathconf }, /* pathconf */
144 { &vop_advlock_desc, ufs_advlock }, /* advlock */
145 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
146 { &vop_getpages_desc, genfs_getpages }, /* getpages */
147 { &vop_putpages_desc, genfs_putpages }, /* putpages */
148 { &vop_openextattr_desc, ffs_openextattr }, /* openextattr */
149 { &vop_closeextattr_desc, ffs_closeextattr }, /* closeextattr */
150 { &vop_getextattr_desc, ffs_getextattr }, /* getextattr */
151 { &vop_setextattr_desc, ffs_setextattr }, /* setextattr */
152 { &vop_listextattr_desc, ffs_listextattr }, /* listextattr */
153 { &vop_deleteextattr_desc, ffs_deleteextattr }, /* deleteextattr */
154 { NULL, NULL }
155 };
156 const struct vnodeopv_desc ffs_vnodeop_opv_desc =
157 { &ffs_vnodeop_p, ffs_vnodeop_entries };
158
159 int (**ffs_specop_p)(void *);
160 const struct vnodeopv_entry_desc ffs_specop_entries[] = {
161 { &vop_default_desc, vn_default_error },
162 { &vop_lookup_desc, spec_lookup }, /* lookup */
163 { &vop_create_desc, spec_create }, /* create */
164 { &vop_mknod_desc, spec_mknod }, /* mknod */
165 { &vop_open_desc, spec_open }, /* open */
166 { &vop_close_desc, ufsspec_close }, /* close */
167 { &vop_access_desc, ufs_access }, /* access */
168 { &vop_getattr_desc, ufs_getattr }, /* getattr */
169 { &vop_setattr_desc, ufs_setattr }, /* setattr */
170 { &vop_read_desc, ufsspec_read }, /* read */
171 { &vop_write_desc, ufsspec_write }, /* write */
172 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
173 { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
174 { &vop_poll_desc, spec_poll }, /* poll */
175 { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */
176 { &vop_revoke_desc, spec_revoke }, /* revoke */
177 { &vop_mmap_desc, spec_mmap }, /* mmap */
178 { &vop_fsync_desc, ffs_fsync }, /* fsync */
179 { &vop_seek_desc, spec_seek }, /* seek */
180 { &vop_remove_desc, spec_remove }, /* remove */
181 { &vop_link_desc, spec_link }, /* link */
182 { &vop_rename_desc, spec_rename }, /* rename */
183 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
184 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
185 { &vop_symlink_desc, spec_symlink }, /* symlink */
186 { &vop_readdir_desc, spec_readdir }, /* readdir */
187 { &vop_readlink_desc, spec_readlink }, /* readlink */
188 { &vop_abortop_desc, spec_abortop }, /* abortop */
189 { &vop_inactive_desc, ufs_inactive }, /* inactive */
190 { &vop_reclaim_desc, ffs_reclaim }, /* reclaim */
191 { &vop_lock_desc, ufs_lock }, /* lock */
192 { &vop_unlock_desc, ufs_unlock }, /* unlock */
193 { &vop_bmap_desc, spec_bmap }, /* bmap */
194 { &vop_strategy_desc, spec_strategy }, /* strategy */
195 { &vop_print_desc, ufs_print }, /* print */
196 { &vop_islocked_desc, ufs_islocked }, /* islocked */
197 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
198 { &vop_advlock_desc, spec_advlock }, /* advlock */
199 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
200 { &vop_getpages_desc, spec_getpages }, /* getpages */
201 { &vop_putpages_desc, spec_putpages }, /* putpages */
202 { &vop_openextattr_desc, ffs_openextattr }, /* openextattr */
203 { &vop_closeextattr_desc, ffs_closeextattr }, /* closeextattr */
204 { &vop_getextattr_desc, ffs_getextattr }, /* getextattr */
205 { &vop_setextattr_desc, ffs_setextattr }, /* setextattr */
206 { &vop_listextattr_desc, ffs_listextattr }, /* listextattr */
207 { &vop_deleteextattr_desc, ffs_deleteextattr }, /* deleteextattr */
208 { NULL, NULL }
209 };
210 const struct vnodeopv_desc ffs_specop_opv_desc =
211 { &ffs_specop_p, ffs_specop_entries };
212
213 int (**ffs_fifoop_p)(void *);
214 const struct vnodeopv_entry_desc ffs_fifoop_entries[] = {
215 { &vop_default_desc, vn_default_error },
216 { &vop_lookup_desc, vn_fifo_bypass }, /* lookup */
217 { &vop_create_desc, vn_fifo_bypass }, /* create */
218 { &vop_mknod_desc, vn_fifo_bypass }, /* mknod */
219 { &vop_open_desc, vn_fifo_bypass }, /* open */
220 { &vop_close_desc, ufsfifo_close }, /* close */
221 { &vop_access_desc, ufs_access }, /* access */
222 { &vop_getattr_desc, ufs_getattr }, /* getattr */
223 { &vop_setattr_desc, ufs_setattr }, /* setattr */
224 { &vop_read_desc, ufsfifo_read }, /* read */
225 { &vop_write_desc, ufsfifo_write }, /* write */
226 { &vop_ioctl_desc, vn_fifo_bypass }, /* ioctl */
227 { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
228 { &vop_poll_desc, vn_fifo_bypass }, /* poll */
229 { &vop_kqfilter_desc, vn_fifo_bypass }, /* kqfilter */
230 { &vop_revoke_desc, vn_fifo_bypass }, /* revoke */
231 { &vop_mmap_desc, vn_fifo_bypass }, /* mmap */
232 { &vop_fsync_desc, ffs_fsync }, /* fsync */
233 { &vop_seek_desc, vn_fifo_bypass }, /* seek */
234 { &vop_remove_desc, vn_fifo_bypass }, /* remove */
235 { &vop_link_desc, vn_fifo_bypass }, /* link */
236 { &vop_rename_desc, vn_fifo_bypass }, /* rename */
237 { &vop_mkdir_desc, vn_fifo_bypass }, /* mkdir */
238 { &vop_rmdir_desc, vn_fifo_bypass }, /* rmdir */
239 { &vop_symlink_desc, vn_fifo_bypass }, /* symlink */
240 { &vop_readdir_desc, vn_fifo_bypass }, /* readdir */
241 { &vop_readlink_desc, vn_fifo_bypass }, /* readlink */
242 { &vop_abortop_desc, vn_fifo_bypass }, /* abortop */
243 { &vop_inactive_desc, ufs_inactive }, /* inactive */
244 { &vop_reclaim_desc, ffs_reclaim }, /* reclaim */
245 { &vop_lock_desc, ufs_lock }, /* lock */
246 { &vop_unlock_desc, ufs_unlock }, /* unlock */
247 { &vop_bmap_desc, vn_fifo_bypass }, /* bmap */
248 { &vop_strategy_desc, vn_fifo_bypass }, /* strategy */
249 { &vop_print_desc, ufs_print }, /* print */
250 { &vop_islocked_desc, ufs_islocked }, /* islocked */
251 { &vop_pathconf_desc, vn_fifo_bypass }, /* pathconf */
252 { &vop_advlock_desc, vn_fifo_bypass }, /* advlock */
253 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
254 { &vop_putpages_desc, vn_fifo_bypass }, /* putpages */
255 { &vop_openextattr_desc, ffs_openextattr }, /* openextattr */
256 { &vop_closeextattr_desc, ffs_closeextattr }, /* closeextattr */
257 { &vop_getextattr_desc, ffs_getextattr }, /* getextattr */
258 { &vop_setextattr_desc, ffs_setextattr }, /* setextattr */
259 { &vop_listextattr_desc, ffs_listextattr }, /* listextattr */
260 { &vop_deleteextattr_desc, ffs_deleteextattr }, /* deleteextattr */
261 { NULL, NULL }
262 };
263 const struct vnodeopv_desc ffs_fifoop_opv_desc =
264 { &ffs_fifoop_p, ffs_fifoop_entries };
265
266 #include <ufs/ufs/ufs_readwrite.c>
267
268 int
269 ffs_fsync(void *v)
270 {
271 struct vop_fsync_args /* {
272 struct vnode *a_vp;
273 kauth_cred_t a_cred;
274 int a_flags;
275 off_t a_offlo;
276 off_t a_offhi;
277 struct lwp *a_l;
278 } */ *ap = v;
279 struct buf *bp;
280 int num, error, i;
281 struct indir ia[NIADDR + 1];
282 int bsize;
283 daddr_t blk_high;
284 struct vnode *vp;
285 struct mount *mp;
286
287 vp = ap->a_vp;
288 mp = vp->v_mount;
289
290 fstrans_start(mp, FSTRANS_LAZY);
291 if ((ap->a_offlo == 0 && ap->a_offhi == 0) || (vp->v_type != VREG)) {
292 error = ffs_full_fsync(vp, ap->a_flags);
293 goto out;
294 }
295
296 bsize = mp->mnt_stat.f_iosize;
297 blk_high = ap->a_offhi / bsize;
298 if (ap->a_offhi % bsize != 0)
299 blk_high++;
300
301 /*
302 * First, flush all pages in range.
303 */
304
305 mutex_enter(&vp->v_interlock);
306 error = VOP_PUTPAGES(vp, trunc_page(ap->a_offlo),
307 round_page(ap->a_offhi), PGO_CLEANIT |
308 ((ap->a_flags & FSYNC_WAIT) ? PGO_SYNCIO : 0));
309 if (error) {
310 goto out;
311 }
312
313 #ifdef WAPBL
314 KASSERT(vp->v_type == VREG);
315 if (mp->mnt_wapbl) {
316 /*
317 * Don't bother writing out metadata if the syncer is
318 * making the request. We will let the sync vnode
319 * write it out in a single burst through a call to
320 * VFS_SYNC().
321 */
322 if ((ap->a_flags & (FSYNC_DATAONLY | FSYNC_LAZY)) != 0) {
323 fstrans_done(mp);
324 return 0;
325 }
326 error = 0;
327 if (vp->v_tag == VT_UFS && VTOI(vp)->i_flag &
328 (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY |
329 IN_MODIFIED | IN_ACCESSED)) {
330 error = UFS_WAPBL_BEGIN(mp);
331 if (error) {
332 fstrans_done(mp);
333 return error;
334 }
335 error = ffs_update(vp, NULL, NULL, UPDATE_CLOSE |
336 ((ap->a_flags & FSYNC_WAIT) ? UPDATE_WAIT : 0));
337 UFS_WAPBL_END(mp);
338 }
339 if (error || (ap->a_flags & FSYNC_NOLOG) != 0) {
340 fstrans_done(mp);
341 return error;
342 }
343 error = wapbl_flush(mp->mnt_wapbl, 0);
344 fstrans_done(mp);
345 return error;
346 }
347 #endif /* WAPBL */
348
349 /*
350 * Then, flush indirect blocks.
351 */
352
353 if (blk_high >= NDADDR) {
354 error = ufs_getlbns(vp, blk_high, ia, &num);
355 if (error)
356 goto out;
357
358 mutex_enter(&bufcache_lock);
359 for (i = 0; i < num; i++) {
360 if ((bp = incore(vp, ia[i].in_lbn)) == NULL)
361 continue;
362 if ((bp->b_cflags & BC_BUSY) != 0 ||
363 (bp->b_oflags & BO_DELWRI) == 0)
364 continue;
365 bp->b_cflags |= BC_BUSY | BC_VFLUSH;
366 mutex_exit(&bufcache_lock);
367 bawrite(bp);
368 mutex_enter(&bufcache_lock);
369 }
370 mutex_exit(&bufcache_lock);
371 }
372
373 if (ap->a_flags & FSYNC_WAIT) {
374 mutex_enter(&vp->v_interlock);
375 while (vp->v_numoutput > 0)
376 cv_wait(&vp->v_cv, &vp->v_interlock);
377 mutex_exit(&vp->v_interlock);
378 }
379
380 error = ffs_update(vp, NULL, NULL, UPDATE_CLOSE |
381 (((ap->a_flags & (FSYNC_WAIT | FSYNC_DATAONLY)) == FSYNC_WAIT)
382 ? UPDATE_WAIT : 0));
383
384 if (error == 0 && ap->a_flags & FSYNC_CACHE) {
385 int l = 0;
386 VOP_IOCTL(VTOI(vp)->i_devvp, DIOCCACHESYNC, &l, FWRITE,
387 curlwp->l_cred);
388 }
389
390 out:
391 fstrans_done(mp);
392 return error;
393 }
394
395 /*
396 * Synch an open file. Called for VOP_FSYNC().
397 */
398 /* ARGSUSED */
399 int
400 ffs_full_fsync(struct vnode *vp, int flags)
401 {
402 struct buf *bp, *nbp;
403 int error, passes, skipmeta, waitfor, i;
404 struct mount *mp;
405
406 KASSERT(VTOI(vp) != NULL);
407 KASSERT(vp->v_tag == VT_UFS);
408
409 error = 0;
410
411 mp = vp->v_mount;
412 if (vp->v_type == VBLK && vp->v_specmountpoint != NULL) {
413 mp = vp->v_specmountpoint;
414 } else {
415 mp = vp->v_mount;
416 }
417
418 /*
419 * Flush all dirty data associated with the vnode.
420 */
421 if (vp->v_type == VREG || vp->v_type == VBLK) {
422 int pflags = PGO_ALLPAGES | PGO_CLEANIT;
423
424 if ((flags & FSYNC_WAIT))
425 pflags |= PGO_SYNCIO;
426 if (vp->v_type == VREG &&
427 fstrans_getstate(mp) == FSTRANS_SUSPENDING)
428 pflags |= PGO_FREE;
429 mutex_enter(&vp->v_interlock);
430 error = VOP_PUTPAGES(vp, 0, 0, pflags);
431 if (error)
432 return error;
433 }
434
435 #ifdef WAPBL
436 mp = wapbl_vptomp(vp);
437 if (mp && mp->mnt_wapbl) {
438 /*
439 * Don't bother writing out metadata if the syncer is
440 * making the request. We will let the sync vnode
441 * write it out in a single burst through a call to
442 * VFS_SYNC().
443 */
444 if ((flags & (FSYNC_DATAONLY | FSYNC_LAZY)) != 0)
445 return 0;
446
447 if ((VTOI(vp)->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE
448 | IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) != 0) {
449 error = UFS_WAPBL_BEGIN(mp);
450 if (error)
451 return error;
452 error = ffs_update(vp, NULL, NULL, UPDATE_CLOSE |
453 ((flags & FSYNC_WAIT) ? UPDATE_WAIT : 0));
454 UFS_WAPBL_END(mp);
455 }
456 if (error || (flags & FSYNC_NOLOG) != 0)
457 return error;
458
459 /*
460 * Don't flush the log if the vnode being flushed
461 * contains no dirty buffers that could be in the log.
462 */
463 if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
464 error = wapbl_flush(mp->mnt_wapbl, 0);
465 if (error)
466 return error;
467 }
468
469 if ((flags & FSYNC_WAIT) != 0) {
470 mutex_enter(&vp->v_interlock);
471 while (vp->v_numoutput != 0)
472 cv_wait(&vp->v_cv, &vp->v_interlock);
473 mutex_exit(&vp->v_interlock);
474 }
475
476 return error;
477 }
478 #endif /* WAPBL */
479
480 /*
481 * Write out metadata for non-logging file systems. XXX This block
482 * should be simplified now that softdep is gone.
483 */
484 passes = NIADDR + 1;
485 skipmeta = 0;
486 if (flags & FSYNC_WAIT)
487 skipmeta = 1;
488
489 loop:
490 mutex_enter(&bufcache_lock);
491 LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
492 bp->b_cflags &= ~BC_SCANNED;
493 }
494 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
495 nbp = LIST_NEXT(bp, b_vnbufs);
496 if (bp->b_cflags & (BC_BUSY | BC_SCANNED))
497 continue;
498 if ((bp->b_oflags & BO_DELWRI) == 0)
499 panic("ffs_fsync: not dirty");
500 if (skipmeta && bp->b_lblkno < 0)
501 continue;
502 bp->b_cflags |= BC_BUSY | BC_VFLUSH | BC_SCANNED;
503 mutex_exit(&bufcache_lock);
504 /*
505 * On our final pass through, do all I/O synchronously
506 * so that we can find out if our flush is failing
507 * because of write errors.
508 */
509 if (passes > 0 || !(flags & FSYNC_WAIT))
510 (void) bawrite(bp);
511 else if ((error = bwrite(bp)) != 0)
512 return (error);
513 /*
514 * Since we unlocked during the I/O, we need
515 * to start from a known point.
516 */
517 mutex_enter(&bufcache_lock);
518 nbp = LIST_FIRST(&vp->v_dirtyblkhd);
519 }
520 mutex_exit(&bufcache_lock);
521 if (skipmeta) {
522 skipmeta = 0;
523 goto loop;
524 }
525
526 if ((flags & FSYNC_WAIT) != 0) {
527 mutex_enter(&vp->v_interlock);
528 while (vp->v_numoutput) {
529 cv_wait(&vp->v_cv, &vp->v_interlock);
530 }
531 mutex_exit(&vp->v_interlock);
532
533 /*
534 * Ensure that any filesystem metadata associated
535 * with the vnode has been written.
536 */
537 if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
538 /*
539 * Block devices associated with filesystems may
540 * have new I/O requests posted for them even if
541 * the vnode is locked, so no amount of trying will
542 * get them clean. Thus we give block devices a
543 * good effort, then just give up. For all other file
544 * types, go around and try again until it is clean.
545 */
546 if (passes > 0) {
547 passes--;
548 goto loop;
549 }
550 #ifdef DIAGNOSTIC
551 if (vp->v_type != VBLK)
552 vprint("ffs_fsync: dirty", vp);
553 #endif
554 }
555 }
556
557 waitfor = (flags & FSYNC_WAIT) ? UPDATE_WAIT : 0;
558 error = ffs_update(vp, NULL, NULL, UPDATE_CLOSE | waitfor);
559
560 if (error == 0 && (flags & FSYNC_CACHE) != 0) {
561 (void)VOP_IOCTL(VTOI(vp)->i_devvp, DIOCCACHESYNC, &i, FWRITE,
562 kauth_cred_get());
563 }
564
565 return error;
566 }
567
568 /*
569 * Reclaim an inode so that it can be used for other purposes.
570 */
571 int
572 ffs_reclaim(void *v)
573 {
574 struct vop_reclaim_args /* {
575 struct vnode *a_vp;
576 struct lwp *a_l;
577 } */ *ap = v;
578 struct vnode *vp = ap->a_vp;
579 struct inode *ip = VTOI(vp);
580 struct mount *mp = vp->v_mount;
581 struct ufsmount *ump = ip->i_ump;
582 void *data;
583 int error;
584
585 fstrans_start(mp, FSTRANS_LAZY);
586 /*
587 * The inode must be freed and updated before being removed
588 * from its hash chain. Other threads trying to gain a hold
589 * on the inode will be stalled because it is locked (VI_XLOCK).
590 */
591 error = UFS_WAPBL_BEGIN(mp);
592 if (error) {
593 fstrans_done(mp);
594 return error;
595 }
596 if (ip->i_nlink <= 0 && ip->i_omode != 0 &&
597 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
598 ffs_vfree(vp, ip->i_number, ip->i_omode);
599 UFS_WAPBL_END(mp);
600 if ((error = ufs_reclaim(vp)) != 0) {
601 fstrans_done(mp);
602 return (error);
603 }
604 if (ip->i_din.ffs1_din != NULL) {
605 if (ump->um_fstype == UFS1)
606 pool_cache_put(ffs_dinode1_cache, ip->i_din.ffs1_din);
607 else
608 pool_cache_put(ffs_dinode2_cache, ip->i_din.ffs2_din);
609 }
610 /*
611 * To interlock with ffs_sync().
612 */
613 genfs_node_destroy(vp);
614 mutex_enter(&vp->v_interlock);
615 data = vp->v_data;
616 vp->v_data = NULL;
617 mutex_exit(&vp->v_interlock);
618
619 /*
620 * XXX MFS ends up here, too, to free an inode. Should we create
621 * XXX a separate pool for MFS inodes?
622 */
623 pool_cache_put(ffs_inode_cache, data);
624 fstrans_done(mp);
625 return (0);
626 }
627
628 /*
629 * Return the last logical file offset that should be written for this file
630 * if we're doing a write that ends at "size".
631 */
632
633 void
634 ffs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
635 {
636 struct inode *ip = VTOI(vp);
637 struct fs *fs = ip->i_fs;
638 daddr_t olbn, nlbn;
639
640 olbn = lblkno(fs, ip->i_size);
641 nlbn = lblkno(fs, size);
642 if (nlbn < NDADDR && olbn <= nlbn) {
643 *eobp = fragroundup(fs, size);
644 } else {
645 *eobp = blkroundup(fs, size);
646 }
647 }
648
649 int
650 ffs_openextattr(void *v)
651 {
652 struct vop_openextattr_args /* {
653 struct vnode *a_vp;
654 kauth_cred_t a_cred;
655 struct proc *a_p;
656 } */ *ap = v;
657 struct inode *ip = VTOI(ap->a_vp);
658 struct fs *fs = ip->i_fs;
659
660 /* Not supported for UFS1 file systems. */
661 if (fs->fs_magic == FS_UFS1_MAGIC)
662 return (EOPNOTSUPP);
663
664 /* XXX Not implemented for UFS2 file systems. */
665 return (EOPNOTSUPP);
666 }
667
668 int
669 ffs_closeextattr(void *v)
670 {
671 struct vop_closeextattr_args /* {
672 struct vnode *a_vp;
673 int a_commit;
674 kauth_cred_t a_cred;
675 struct proc *a_p;
676 } */ *ap = v;
677 struct inode *ip = VTOI(ap->a_vp);
678 struct fs *fs = ip->i_fs;
679
680 /* Not supported for UFS1 file systems. */
681 if (fs->fs_magic == FS_UFS1_MAGIC)
682 return (EOPNOTSUPP);
683
684 /* XXX Not implemented for UFS2 file systems. */
685 return (EOPNOTSUPP);
686 }
687
688 int
689 ffs_getextattr(void *v)
690 {
691 struct vop_getextattr_args /* {
692 struct vnode *a_vp;
693 int a_attrnamespace;
694 const char *a_name;
695 struct uio *a_uio;
696 size_t *a_size;
697 kauth_cred_t a_cred;
698 struct proc *a_p;
699 } */ *ap = v;
700 struct vnode *vp = ap->a_vp;
701 struct inode *ip = VTOI(vp);
702 struct fs *fs = ip->i_fs;
703
704 if (fs->fs_magic == FS_UFS1_MAGIC) {
705 #ifdef UFS_EXTATTR
706 int error;
707
708 fstrans_start(vp->v_mount, FSTRANS_SHARED);
709 error = ufs_getextattr(ap);
710 fstrans_done(vp->v_mount);
711 return error;
712 #else
713 return (EOPNOTSUPP);
714 #endif
715 }
716
717 /* XXX Not implemented for UFS2 file systems. */
718 return (EOPNOTSUPP);
719 }
720
721 int
722 ffs_setextattr(void *v)
723 {
724 struct vop_setextattr_args /* {
725 struct vnode *a_vp;
726 int a_attrnamespace;
727 const char *a_name;
728 struct uio *a_uio;
729 kauth_cred_t a_cred;
730 struct proc *a_p;
731 } */ *ap = v;
732 struct vnode *vp = ap->a_vp;
733 struct inode *ip = VTOI(vp);
734 struct fs *fs = ip->i_fs;
735
736 if (fs->fs_magic == FS_UFS1_MAGIC) {
737 #ifdef UFS_EXTATTR
738 int error;
739
740 fstrans_start(vp->v_mount, FSTRANS_SHARED);
741 error = ufs_setextattr(ap);
742 fstrans_done(vp->v_mount);
743 return error;
744 #else
745 return (EOPNOTSUPP);
746 #endif
747 }
748
749 /* XXX Not implemented for UFS2 file systems. */
750 return (EOPNOTSUPP);
751 }
752
753 int
754 ffs_listextattr(void *v)
755 {
756 struct vop_listextattr_args /* {
757 struct vnode *a_vp;
758 int a_attrnamespace;
759 struct uio *a_uio;
760 size_t *a_size;
761 kauth_cred_t a_cred;
762 struct proc *a_p;
763 } */ *ap = v;
764 struct inode *ip = VTOI(ap->a_vp);
765 struct fs *fs = ip->i_fs;
766
767 /* Not supported for UFS1 file systems. */
768 if (fs->fs_magic == FS_UFS1_MAGIC)
769 return (EOPNOTSUPP);
770
771 /* XXX Not implemented for UFS2 file systems. */
772 return (EOPNOTSUPP);
773 }
774
775 int
776 ffs_deleteextattr(void *v)
777 {
778 struct vop_deleteextattr_args /* {
779 struct vnode *a_vp;
780 int a_attrnamespace;
781 kauth_cred_t a_cred;
782 struct proc *a_p;
783 } */ *ap = v;
784 struct vnode *vp = ap->a_vp;
785 struct inode *ip = VTOI(vp);
786 struct fs *fs = ip->i_fs;
787
788 if (fs->fs_magic == FS_UFS1_MAGIC) {
789 #ifdef UFS_EXTATTR
790 int error;
791
792 fstrans_start(vp->v_mount, FSTRANS_SHARED);
793 error = ufs_deleteextattr(ap);
794 fstrans_done(vp->v_mount);
795 return error;
796 #else
797 return (EOPNOTSUPP);
798 #endif
799 }
800
801 /* XXX Not implemented for UFS2 file systems. */
802 return (EOPNOTSUPP);
803 }
804