ffs_vnops.c revision 1.116 1 /* $NetBSD: ffs_vnops.c,v 1.116 2010/08/12 07:41:49 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.116 2010/08/12 07:41:49 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 #ifdef WAPBL
286 struct mount *mp;
287 #endif
288
289 vp = ap->a_vp;
290
291 fstrans_start(vp->v_mount, FSTRANS_LAZY);
292 if ((ap->a_offlo == 0 && ap->a_offhi == 0) || (vp->v_type != VREG)) {
293 error = ffs_full_fsync(vp, ap->a_flags);
294 goto out;
295 }
296
297 bsize = vp->v_mount->mnt_stat.f_iosize;
298 blk_high = ap->a_offhi / bsize;
299 if (ap->a_offhi % bsize != 0)
300 blk_high++;
301
302 /*
303 * First, flush all pages in range.
304 */
305
306 mutex_enter(&vp->v_interlock);
307 error = VOP_PUTPAGES(vp, trunc_page(ap->a_offlo),
308 round_page(ap->a_offhi), PGO_CLEANIT |
309 ((ap->a_flags & FSYNC_WAIT) ? PGO_SYNCIO : 0));
310 if (error) {
311 goto out;
312 }
313
314 #ifdef WAPBL
315 mp = wapbl_vptomp(vp);
316 if (mp->mnt_wapbl) {
317 /*
318 * Don't bother writing out metadata if the syncer is
319 * making the request. We will let the sync vnode
320 * write it out in a single burst through a call to
321 * VFS_SYNC().
322 */
323 if ((ap->a_flags & (FSYNC_DATAONLY | FSYNC_LAZY)) != 0) {
324 fstrans_done(vp->v_mount);
325 return 0;
326 }
327 error = 0;
328 if (vp->v_tag == VT_UFS && VTOI(vp)->i_flag &
329 (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY |
330 IN_MODIFIED | IN_ACCESSED)) {
331 error = UFS_WAPBL_BEGIN(mp);
332 if (error) {
333 fstrans_done(vp->v_mount);
334 return error;
335 }
336 error = ffs_update(vp, NULL, NULL, UPDATE_CLOSE |
337 ((ap->a_flags & FSYNC_WAIT) ? UPDATE_WAIT : 0));
338 UFS_WAPBL_END(mp);
339 }
340 if (error || (ap->a_flags & FSYNC_NOLOG) != 0) {
341 fstrans_done(vp->v_mount);
342 return error;
343 }
344 error = wapbl_flush(mp->mnt_wapbl, 0);
345 fstrans_done(vp->v_mount);
346 return error;
347 }
348 #endif /* WAPBL */
349
350 /*
351 * Then, flush indirect blocks.
352 */
353
354 if (blk_high >= NDADDR) {
355 error = ufs_getlbns(vp, blk_high, ia, &num);
356 if (error)
357 goto out;
358
359 mutex_enter(&bufcache_lock);
360 for (i = 0; i < num; i++) {
361 if ((bp = incore(vp, ia[i].in_lbn)) == NULL)
362 continue;
363 if ((bp->b_cflags & BC_BUSY) != 0 ||
364 (bp->b_oflags & BO_DELWRI) == 0)
365 continue;
366 bp->b_cflags |= BC_BUSY | BC_VFLUSH;
367 mutex_exit(&bufcache_lock);
368 bawrite(bp);
369 mutex_enter(&bufcache_lock);
370 }
371 mutex_exit(&bufcache_lock);
372 }
373
374 if (ap->a_flags & FSYNC_WAIT) {
375 mutex_enter(&vp->v_interlock);
376 while (vp->v_numoutput > 0)
377 cv_wait(&vp->v_cv, &vp->v_interlock);
378 mutex_exit(&vp->v_interlock);
379 }
380
381 error = ffs_update(vp, NULL, NULL, UPDATE_CLOSE |
382 (((ap->a_flags & (FSYNC_WAIT | FSYNC_DATAONLY)) == FSYNC_WAIT)
383 ? UPDATE_WAIT : 0));
384
385 if (error == 0 && ap->a_flags & FSYNC_CACHE) {
386 int l = 0;
387 VOP_IOCTL(VTOI(vp)->i_devvp, DIOCCACHESYNC, &l, FWRITE,
388 curlwp->l_cred);
389 }
390
391 out:
392 fstrans_done(vp->v_mount);
393 return error;
394 }
395
396 /*
397 * Synch an open file. Called for VOP_FSYNC().
398 */
399 /* ARGSUSED */
400 int
401 ffs_full_fsync(struct vnode *vp, int flags)
402 {
403 struct buf *bp, *nbp;
404 int error, passes, skipmeta, waitfor, i;
405 struct mount *mp;
406
407 KASSERT(VTOI(vp) != NULL);
408 KASSERT(vp->v_tag == VT_UFS);
409
410 error = 0;
411
412 mp = vp->v_mount;
413 if (vp->v_type == VBLK && vp->v_specmountpoint != NULL) {
414 mp = vp->v_specmountpoint;
415 } else {
416 mp = vp->v_mount;
417 }
418
419 /*
420 * Flush all dirty data associated with the vnode.
421 */
422 if (vp->v_type == VREG || vp->v_type == VBLK) {
423 int pflags = PGO_ALLPAGES | PGO_CLEANIT;
424
425 if ((flags & FSYNC_WAIT))
426 pflags |= PGO_SYNCIO;
427 if (vp->v_type == VREG &&
428 fstrans_getstate(mp) == FSTRANS_SUSPENDING)
429 pflags |= PGO_FREE;
430 mutex_enter(&vp->v_interlock);
431 error = VOP_PUTPAGES(vp, 0, 0, pflags);
432 if (error)
433 return error;
434 }
435
436 #ifdef WAPBL
437 mp = wapbl_vptomp(vp);
438 if (mp && mp->mnt_wapbl) {
439 /*
440 * Don't bother writing out metadata if the syncer is
441 * making the request. We will let the sync vnode
442 * write it out in a single burst through a call to
443 * VFS_SYNC().
444 */
445 if ((flags & (FSYNC_DATAONLY | FSYNC_LAZY)) != 0)
446 return 0;
447
448 if ((VTOI(vp)->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE
449 | IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) != 0) {
450 error = UFS_WAPBL_BEGIN(mp);
451 if (error)
452 return error;
453 error = ffs_update(vp, NULL, NULL, UPDATE_CLOSE |
454 ((flags & FSYNC_WAIT) ? UPDATE_WAIT : 0));
455 UFS_WAPBL_END(mp);
456 }
457 if (error || (flags & FSYNC_NOLOG) != 0)
458 return error;
459
460 /*
461 * Don't flush the log if the vnode being flushed
462 * contains no dirty buffers that could be in the log.
463 */
464 if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
465 error = wapbl_flush(mp->mnt_wapbl, 0);
466 if (error)
467 return error;
468 }
469
470 if ((flags & FSYNC_WAIT) != 0) {
471 mutex_enter(&vp->v_interlock);
472 while (vp->v_numoutput != 0)
473 cv_wait(&vp->v_cv, &vp->v_interlock);
474 mutex_exit(&vp->v_interlock);
475 }
476
477 return error;
478 }
479 #endif /* WAPBL */
480
481 /*
482 * Write out metadata for non-logging file systems. XXX This block
483 * should be simplified now that softdep is gone.
484 */
485 passes = NIADDR + 1;
486 skipmeta = 0;
487 if (flags & FSYNC_WAIT)
488 skipmeta = 1;
489
490 loop:
491 mutex_enter(&bufcache_lock);
492 LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
493 bp->b_cflags &= ~BC_SCANNED;
494 }
495 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
496 nbp = LIST_NEXT(bp, b_vnbufs);
497 if (bp->b_cflags & (BC_BUSY | BC_SCANNED))
498 continue;
499 if ((bp->b_oflags & BO_DELWRI) == 0)
500 panic("ffs_fsync: not dirty");
501 if (skipmeta && bp->b_lblkno < 0)
502 continue;
503 bp->b_cflags |= BC_BUSY | BC_VFLUSH | BC_SCANNED;
504 mutex_exit(&bufcache_lock);
505 /*
506 * On our final pass through, do all I/O synchronously
507 * so that we can find out if our flush is failing
508 * because of write errors.
509 */
510 if (passes > 0 || !(flags & FSYNC_WAIT))
511 (void) bawrite(bp);
512 else if ((error = bwrite(bp)) != 0)
513 return (error);
514 /*
515 * Since we unlocked during the I/O, we need
516 * to start from a known point.
517 */
518 mutex_enter(&bufcache_lock);
519 nbp = LIST_FIRST(&vp->v_dirtyblkhd);
520 }
521 mutex_exit(&bufcache_lock);
522 if (skipmeta) {
523 skipmeta = 0;
524 goto loop;
525 }
526
527 if ((flags & FSYNC_WAIT) != 0) {
528 mutex_enter(&vp->v_interlock);
529 while (vp->v_numoutput) {
530 cv_wait(&vp->v_cv, &vp->v_interlock);
531 }
532 mutex_exit(&vp->v_interlock);
533
534 /*
535 * Ensure that any filesystem metadata associated
536 * with the vnode has been written.
537 */
538 if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
539 /*
540 * Block devices associated with filesystems may
541 * have new I/O requests posted for them even if
542 * the vnode is locked, so no amount of trying will
543 * get them clean. Thus we give block devices a
544 * good effort, then just give up. For all other file
545 * types, go around and try again until it is clean.
546 */
547 if (passes > 0) {
548 passes--;
549 goto loop;
550 }
551 #ifdef DIAGNOSTIC
552 if (vp->v_type != VBLK)
553 vprint("ffs_fsync: dirty", vp);
554 #endif
555 }
556 }
557
558 waitfor = (flags & FSYNC_WAIT) ? UPDATE_WAIT : 0;
559 error = ffs_update(vp, NULL, NULL, UPDATE_CLOSE | waitfor);
560
561 if (error == 0 && (flags & FSYNC_CACHE) != 0) {
562 (void)VOP_IOCTL(VTOI(vp)->i_devvp, DIOCCACHESYNC, &i, FWRITE,
563 kauth_cred_get());
564 }
565
566 return error;
567 }
568
569 /*
570 * Reclaim an inode so that it can be used for other purposes.
571 */
572 int
573 ffs_reclaim(void *v)
574 {
575 struct vop_reclaim_args /* {
576 struct vnode *a_vp;
577 struct lwp *a_l;
578 } */ *ap = v;
579 struct vnode *vp = ap->a_vp;
580 struct inode *ip = VTOI(vp);
581 struct mount *mp = vp->v_mount;
582 struct ufsmount *ump = ip->i_ump;
583 void *data;
584 int error;
585
586 fstrans_start(mp, FSTRANS_LAZY);
587 /*
588 * The inode must be freed and updated before being removed
589 * from its hash chain. Other threads trying to gain a hold
590 * on the inode will be stalled because it is locked (VI_XLOCK).
591 */
592 error = UFS_WAPBL_BEGIN(mp);
593 if (error) {
594 fstrans_done(mp);
595 return error;
596 }
597 if (ip->i_nlink <= 0 && ip->i_omode != 0 &&
598 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
599 ffs_vfree(vp, ip->i_number, ip->i_omode);
600 UFS_WAPBL_END(mp);
601 if ((error = ufs_reclaim(vp)) != 0) {
602 fstrans_done(mp);
603 return (error);
604 }
605 if (ip->i_din.ffs1_din != NULL) {
606 if (ump->um_fstype == UFS1)
607 pool_cache_put(ffs_dinode1_cache, ip->i_din.ffs1_din);
608 else
609 pool_cache_put(ffs_dinode2_cache, ip->i_din.ffs2_din);
610 }
611 /*
612 * To interlock with ffs_sync().
613 */
614 genfs_node_destroy(vp);
615 mutex_enter(&vp->v_interlock);
616 data = vp->v_data;
617 vp->v_data = NULL;
618 mutex_exit(&vp->v_interlock);
619
620 /*
621 * XXX MFS ends up here, too, to free an inode. Should we create
622 * XXX a separate pool for MFS inodes?
623 */
624 pool_cache_put(ffs_inode_cache, data);
625 fstrans_done(mp);
626 return (0);
627 }
628
629 /*
630 * Return the last logical file offset that should be written for this file
631 * if we're doing a write that ends at "size".
632 */
633
634 void
635 ffs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
636 {
637 struct inode *ip = VTOI(vp);
638 struct fs *fs = ip->i_fs;
639 daddr_t olbn, nlbn;
640
641 olbn = lblkno(fs, ip->i_size);
642 nlbn = lblkno(fs, size);
643 if (nlbn < NDADDR && olbn <= nlbn) {
644 *eobp = fragroundup(fs, size);
645 } else {
646 *eobp = blkroundup(fs, size);
647 }
648 }
649
650 int
651 ffs_openextattr(void *v)
652 {
653 struct vop_openextattr_args /* {
654 struct vnode *a_vp;
655 kauth_cred_t a_cred;
656 struct proc *a_p;
657 } */ *ap = v;
658 struct inode *ip = VTOI(ap->a_vp);
659 struct fs *fs = ip->i_fs;
660
661 /* Not supported for UFS1 file systems. */
662 if (fs->fs_magic == FS_UFS1_MAGIC)
663 return (EOPNOTSUPP);
664
665 /* XXX Not implemented for UFS2 file systems. */
666 return (EOPNOTSUPP);
667 }
668
669 int
670 ffs_closeextattr(void *v)
671 {
672 struct vop_closeextattr_args /* {
673 struct vnode *a_vp;
674 int a_commit;
675 kauth_cred_t a_cred;
676 struct proc *a_p;
677 } */ *ap = v;
678 struct inode *ip = VTOI(ap->a_vp);
679 struct fs *fs = ip->i_fs;
680
681 /* Not supported for UFS1 file systems. */
682 if (fs->fs_magic == FS_UFS1_MAGIC)
683 return (EOPNOTSUPP);
684
685 /* XXX Not implemented for UFS2 file systems. */
686 return (EOPNOTSUPP);
687 }
688
689 int
690 ffs_getextattr(void *v)
691 {
692 struct vop_getextattr_args /* {
693 struct vnode *a_vp;
694 int a_attrnamespace;
695 const char *a_name;
696 struct uio *a_uio;
697 size_t *a_size;
698 kauth_cred_t a_cred;
699 struct proc *a_p;
700 } */ *ap = v;
701 struct vnode *vp = ap->a_vp;
702 struct inode *ip = VTOI(vp);
703 struct fs *fs = ip->i_fs;
704
705 if (fs->fs_magic == FS_UFS1_MAGIC) {
706 #ifdef UFS_EXTATTR
707 int error;
708
709 fstrans_start(vp->v_mount, FSTRANS_SHARED);
710 error = ufs_getextattr(ap);
711 fstrans_done(vp->v_mount);
712 return error;
713 #else
714 return (EOPNOTSUPP);
715 #endif
716 }
717
718 /* XXX Not implemented for UFS2 file systems. */
719 return (EOPNOTSUPP);
720 }
721
722 int
723 ffs_setextattr(void *v)
724 {
725 struct vop_setextattr_args /* {
726 struct vnode *a_vp;
727 int a_attrnamespace;
728 const char *a_name;
729 struct uio *a_uio;
730 kauth_cred_t a_cred;
731 struct proc *a_p;
732 } */ *ap = v;
733 struct vnode *vp = ap->a_vp;
734 struct inode *ip = VTOI(vp);
735 struct fs *fs = ip->i_fs;
736
737 if (fs->fs_magic == FS_UFS1_MAGIC) {
738 #ifdef UFS_EXTATTR
739 int error;
740
741 fstrans_start(vp->v_mount, FSTRANS_SHARED);
742 error = ufs_setextattr(ap);
743 fstrans_done(vp->v_mount);
744 return error;
745 #else
746 return (EOPNOTSUPP);
747 #endif
748 }
749
750 /* XXX Not implemented for UFS2 file systems. */
751 return (EOPNOTSUPP);
752 }
753
754 int
755 ffs_listextattr(void *v)
756 {
757 struct vop_listextattr_args /* {
758 struct vnode *a_vp;
759 int a_attrnamespace;
760 struct uio *a_uio;
761 size_t *a_size;
762 kauth_cred_t a_cred;
763 struct proc *a_p;
764 } */ *ap = v;
765 struct inode *ip = VTOI(ap->a_vp);
766 struct fs *fs = ip->i_fs;
767
768 /* Not supported for UFS1 file systems. */
769 if (fs->fs_magic == FS_UFS1_MAGIC)
770 return (EOPNOTSUPP);
771
772 /* XXX Not implemented for UFS2 file systems. */
773 return (EOPNOTSUPP);
774 }
775
776 int
777 ffs_deleteextattr(void *v)
778 {
779 struct vop_deleteextattr_args /* {
780 struct vnode *a_vp;
781 int a_attrnamespace;
782 kauth_cred_t a_cred;
783 struct proc *a_p;
784 } */ *ap = v;
785 struct vnode *vp = ap->a_vp;
786 struct inode *ip = VTOI(vp);
787 struct fs *fs = ip->i_fs;
788
789 if (fs->fs_magic == FS_UFS1_MAGIC) {
790 #ifdef UFS_EXTATTR
791 int error;
792
793 fstrans_start(vp->v_mount, FSTRANS_SHARED);
794 error = ufs_deleteextattr(ap);
795 fstrans_done(vp->v_mount);
796 return error;
797 #else
798 return (EOPNOTSUPP);
799 #endif
800 }
801
802 /* XXX Not implemented for UFS2 file systems. */
803 return (EOPNOTSUPP);
804 }
805