ffs_vnops.c revision 1.109 1 /* $NetBSD: ffs_vnops.c,v 1.109 2009/02/01 17:36:43 ad 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.109 2009/02/01 17:36:43 ad 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, ffs_lock }, /* lock */
138 { &vop_unlock_desc, ffs_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, ffs_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, ffs_lock }, /* lock */
192 { &vop_unlock_desc, ffs_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, ffs_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, fifo_lookup }, /* lookup */
217 { &vop_create_desc, fifo_create }, /* create */
218 { &vop_mknod_desc, fifo_mknod }, /* mknod */
219 { &vop_open_desc, fifo_open }, /* 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, fifo_ioctl }, /* ioctl */
227 { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
228 { &vop_poll_desc, fifo_poll }, /* poll */
229 { &vop_kqfilter_desc, fifo_kqfilter }, /* kqfilter */
230 { &vop_revoke_desc, fifo_revoke }, /* revoke */
231 { &vop_mmap_desc, fifo_mmap }, /* mmap */
232 { &vop_fsync_desc, ffs_fsync }, /* fsync */
233 { &vop_seek_desc, fifo_seek }, /* seek */
234 { &vop_remove_desc, fifo_remove }, /* remove */
235 { &vop_link_desc, fifo_link }, /* link */
236 { &vop_rename_desc, fifo_rename }, /* rename */
237 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */
238 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */
239 { &vop_symlink_desc, fifo_symlink }, /* symlink */
240 { &vop_readdir_desc, fifo_readdir }, /* readdir */
241 { &vop_readlink_desc, fifo_readlink }, /* readlink */
242 { &vop_abortop_desc, fifo_abortop }, /* abortop */
243 { &vop_inactive_desc, ufs_inactive }, /* inactive */
244 { &vop_reclaim_desc, ffs_reclaim }, /* reclaim */
245 { &vop_lock_desc, ffs_lock }, /* lock */
246 { &vop_unlock_desc, ffs_unlock }, /* unlock */
247 { &vop_bmap_desc, fifo_bmap }, /* bmap */
248 { &vop_strategy_desc, fifo_strategy }, /* strategy */
249 { &vop_print_desc, ufs_print }, /* print */
250 { &vop_islocked_desc, ffs_islocked }, /* islocked */
251 { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */
252 { &vop_advlock_desc, fifo_advlock }, /* advlock */
253 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
254 { &vop_putpages_desc, fifo_putpages }, /* 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 /*
293 * XXX no easy way to sync a range in a file with softdep.
294 */
295 if ((ap->a_offlo == 0 && ap->a_offhi == 0) || DOINGSOFTDEP(vp) ||
296 (vp->v_type != VREG)) {
297 int flags = ap->a_flags;
298 error = ffs_full_fsync(vp, flags);
299 goto out;
300 }
301
302 bsize = vp->v_mount->mnt_stat.f_iosize;
303 blk_high = ap->a_offhi / bsize;
304 if (ap->a_offhi % bsize != 0)
305 blk_high++;
306
307 /*
308 * First, flush all pages in range.
309 */
310
311 mutex_enter(&vp->v_interlock);
312 error = VOP_PUTPAGES(vp, trunc_page(ap->a_offlo),
313 round_page(ap->a_offhi), PGO_CLEANIT |
314 ((ap->a_flags & FSYNC_WAIT) ? PGO_SYNCIO : 0));
315 if (error) {
316 goto out;
317 }
318
319 #ifdef WAPBL
320 mp = wapbl_vptomp(vp);
321 if (mp->mnt_wapbl) {
322 if (ap->a_flags & FSYNC_DATAONLY) {
323 fstrans_done(vp->v_mount);
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(vp->v_mount);
333 return error;
334 }
335 error = ffs_update(vp, NULL, NULL,
336 (ap->a_flags & FSYNC_WAIT) ? UPDATE_WAIT : 0);
337 UFS_WAPBL_END(mp);
338 }
339 if (error || (ap->a_flags & FSYNC_NOLOG)) {
340 fstrans_done(vp->v_mount);
341 return error;
342 }
343 error = wapbl_flush(mp->mnt_wapbl, 0);
344 fstrans_done(vp->v_mount);
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,
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(vp->v_mount);
392 return error;
393 }
394
395 /*
396 * Synch an open file. Called for VOP_FSYNC() and VFS_FSYNC().
397 *
398 * BEWARE: THIS ROUTINE ACCEPTS BOTH FFS AND NON-FFS VNODES.
399 */
400 /* ARGSUSED */
401 int
402 ffs_full_fsync(struct vnode *vp, int flags)
403 {
404 struct buf *bp, *nbp;
405 int error, passes, skipmeta, inodedeps_only, waitfor;
406 struct mount *mp;
407
408 error = 0;
409
410 if ((flags & FSYNC_VFS) != 0) {
411 KASSERT(vp->v_specmountpoint != NULL);
412 mp = vp->v_specmountpoint;
413 KASSERT(vp->v_type == VBLK);
414 } else {
415 mp = vp->v_mount;
416 KASSERT(vp->v_tag == VT_UFS);
417 }
418
419 if (vp->v_type == VBLK &&
420 vp->v_specmountpoint != NULL &&
421 (vp->v_specmountpoint->mnt_flag & MNT_SOFTDEP))
422 softdep_fsync_mountdev(vp);
423
424 mutex_enter(&vp->v_interlock);
425
426 inodedeps_only = DOINGSOFTDEP(vp) && (flags & FSYNC_RECLAIM)
427 && UVM_OBJ_IS_CLEAN(&vp->v_uobj) && LIST_EMPTY(&vp->v_dirtyblkhd);
428
429 /*
430 * Flush all dirty data associated with a vnode.
431 */
432
433 if (vp->v_type == VREG || vp->v_type == VBLK) {
434 int pflags = PGO_ALLPAGES | PGO_CLEANIT;
435
436 if ((flags & FSYNC_WAIT))
437 pflags |= PGO_SYNCIO;
438 if (vp->v_type == VREG &&
439 fstrans_getstate(mp) == FSTRANS_SUSPENDING)
440 pflags |= PGO_FREE;
441 error = VOP_PUTPAGES(vp, 0, 0, pflags);
442 if (error)
443 return error;
444 } else {
445 mutex_exit(&vp->v_interlock);
446 }
447
448 #ifdef WAPBL
449 if (mp && mp->mnt_wapbl) {
450 error = 0;
451 if (flags & FSYNC_DATAONLY)
452 return error;
453
454 if ((flags & FSYNC_VFS) == 0 && VTOI(vp) != NULL &&
455 (VTOI(vp)->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE
456 | IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) != 0) {
457 error = UFS_WAPBL_BEGIN(mp);
458 if (error)
459 return error;
460 error = ffs_update(vp, NULL, NULL,
461 (flags & FSYNC_WAIT) ? UPDATE_WAIT : 0);
462 UFS_WAPBL_END(mp);
463 }
464 if (error || (flags & FSYNC_NOLOG))
465 return error;
466
467 /*
468 * Don't flush the log if the vnode being flushed
469 * contains no dirty buffers that could be in the log.
470 */
471 if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
472 error = wapbl_flush(mp->mnt_wapbl, 0);
473 if (error)
474 return error;
475 }
476
477 if ((flags & FSYNC_WAIT) != 0) {
478 mutex_enter(&vp->v_interlock);
479 while (vp->v_numoutput)
480 cv_wait(&vp->v_cv, &vp->v_interlock);
481 mutex_exit(&vp->v_interlock);
482 }
483
484 return error;
485 }
486 #endif /* WAPBL */
487
488 passes = NIADDR + 1;
489 skipmeta = 0;
490 if (flags & FSYNC_WAIT)
491 skipmeta = 1;
492
493 loop:
494 mutex_enter(&bufcache_lock);
495 LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
496 bp->b_cflags &= ~BC_SCANNED;
497 }
498 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
499 nbp = LIST_NEXT(bp, b_vnbufs);
500 if (bp->b_cflags & (BC_BUSY | BC_SCANNED))
501 continue;
502 if ((bp->b_oflags & BO_DELWRI) == 0)
503 panic("ffs_fsync: not dirty");
504 if (skipmeta && bp->b_lblkno < 0)
505 continue;
506 bp->b_cflags |= BC_BUSY | BC_VFLUSH | BC_SCANNED;
507 mutex_exit(&bufcache_lock);
508 /*
509 * On our final pass through, do all I/O synchronously
510 * so that we can find out if our flush is failing
511 * because of write errors.
512 */
513 if (passes > 0 || !(flags & FSYNC_WAIT))
514 (void) bawrite(bp);
515 else if ((error = bwrite(bp)) != 0)
516 return (error);
517 /*
518 * Since we unlocked during the I/O, we need
519 * to start from a known point.
520 */
521 mutex_enter(&bufcache_lock);
522 nbp = LIST_FIRST(&vp->v_dirtyblkhd);
523 }
524 mutex_exit(&bufcache_lock);
525 if (skipmeta) {
526 skipmeta = 0;
527 goto loop;
528 }
529
530 if ((flags & FSYNC_WAIT) != 0) {
531 mutex_enter(&vp->v_interlock);
532 while (vp->v_numoutput) {
533 cv_wait(&vp->v_cv, &vp->v_interlock);
534 }
535 mutex_exit(&vp->v_interlock);
536
537 /*
538 * Ensure that any filesystem metadata associated
539 * with the vnode has been written.
540 */
541 if ((error = softdep_sync_metadata(vp)) != 0)
542 return (error);
543
544 if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
545 /*
546 * Block devices associated with filesystems may
547 * have new I/O requests posted for them even if
548 * the vnode is locked, so no amount of trying will
549 * get them clean. Thus we give block devices a
550 * good effort, then just give up. For all other file
551 * types, go around and try again until it is clean.
552 */
553 if (passes > 0) {
554 passes--;
555 goto loop;
556 }
557 #ifdef DIAGNOSTIC
558 if (vp->v_type != VBLK)
559 vprint("ffs_fsync: dirty", vp);
560 #endif
561 }
562 }
563
564 if (inodedeps_only)
565 waitfor = 0;
566 else
567 waitfor = (flags & FSYNC_WAIT) != 0 ? UPDATE_WAIT : 0;
568
569 if ((flags & FSYNC_VFS) == 0)
570 error = ffs_update(vp, NULL, NULL, waitfor);
571
572 if (error == 0 && (flags & FSYNC_CACHE) != 0) {
573 int i = 0;
574 if ((flags & FSYNC_VFS) == 0) {
575 KASSERT(VTOI(vp) != NULL);
576 vp = VTOI(vp)->i_devvp;
577 }
578 VOP_IOCTL(vp, DIOCCACHESYNC, &i, FWRITE, curlwp->l_cred);
579 }
580
581 return error;
582 }
583
584 /*
585 * Reclaim an inode so that it can be used for other purposes.
586 */
587 int
588 ffs_reclaim(void *v)
589 {
590 struct vop_reclaim_args /* {
591 struct vnode *a_vp;
592 struct lwp *a_l;
593 } */ *ap = v;
594 struct vnode *vp = ap->a_vp;
595 struct inode *ip = VTOI(vp);
596 struct mount *mp = vp->v_mount;
597 struct ufsmount *ump = ip->i_ump;
598 void *data;
599 int error;
600
601 fstrans_start(mp, FSTRANS_LAZY);
602 if ((error = ufs_reclaim(vp)) != 0) {
603 fstrans_done(mp);
604 return (error);
605 }
606 if (ip->i_din.ffs1_din != NULL) {
607 if (ump->um_fstype == UFS1)
608 pool_cache_put(ffs_dinode1_cache, ip->i_din.ffs1_din);
609 else
610 pool_cache_put(ffs_dinode2_cache, ip->i_din.ffs2_din);
611 }
612 /*
613 * To interlock with ffs_sync().
614 */
615 genfs_node_destroy(vp);
616 mutex_enter(&vp->v_interlock);
617 data = vp->v_data;
618 vp->v_data = NULL;
619 mutex_exit(&vp->v_interlock);
620
621 /*
622 * XXX MFS ends up here, too, to free an inode. Should we create
623 * XXX a separate pool for MFS inodes?
624 */
625 pool_cache_put(ffs_inode_cache, data);
626 fstrans_done(mp);
627 return (0);
628 }
629
630 #if 0
631 int
632 ffs_getpages(void *v)
633 {
634 struct vop_getpages_args /* {
635 struct vnode *a_vp;
636 voff_t a_offset;
637 struct vm_page **a_m;
638 int *a_count;
639 int a_centeridx;
640 vm_prot_t a_access_type;
641 int a_advice;
642 int a_flags;
643 } */ *ap = v;
644 struct vnode *vp = ap->a_vp;
645 struct inode *ip = VTOI(vp);
646 struct fs *fs = ip->i_fs;
647
648 /*
649 * don't allow a softdep write to create pages for only part of a block.
650 * the dependency tracking requires that all pages be in memory for
651 * a block involved in a dependency.
652 */
653
654 if (ap->a_flags & PGO_OVERWRITE &&
655 (blkoff(fs, ap->a_offset) != 0 ||
656 blkoff(fs, *ap->a_count << PAGE_SHIFT) != 0) &&
657 DOINGSOFTDEP(ap->a_vp)) {
658 if ((ap->a_flags & PGO_LOCKED) == 0) {
659 mutex_exit(&vp->v_interlock);
660 }
661 return EINVAL;
662 }
663 return genfs_getpages(v);
664 }
665 #endif
666
667 /*
668 * Return the last logical file offset that should be written for this file
669 * if we're doing a write that ends at "size".
670 */
671
672 void
673 ffs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
674 {
675 struct inode *ip = VTOI(vp);
676 struct fs *fs = ip->i_fs;
677 daddr_t olbn, nlbn;
678
679 olbn = lblkno(fs, ip->i_size);
680 nlbn = lblkno(fs, size);
681 if (nlbn < NDADDR && olbn <= nlbn) {
682 *eobp = fragroundup(fs, size);
683 } else {
684 *eobp = blkroundup(fs, size);
685 }
686 }
687
688 int
689 ffs_openextattr(void *v)
690 {
691 struct vop_openextattr_args /* {
692 struct vnode *a_vp;
693 kauth_cred_t a_cred;
694 struct proc *a_p;
695 } */ *ap = v;
696 struct inode *ip = VTOI(ap->a_vp);
697 struct fs *fs = ip->i_fs;
698
699 /* Not supported for UFS1 file systems. */
700 if (fs->fs_magic == FS_UFS1_MAGIC)
701 return (EOPNOTSUPP);
702
703 /* XXX Not implemented for UFS2 file systems. */
704 return (EOPNOTSUPP);
705 }
706
707 int
708 ffs_closeextattr(void *v)
709 {
710 struct vop_closeextattr_args /* {
711 struct vnode *a_vp;
712 int a_commit;
713 kauth_cred_t a_cred;
714 struct proc *a_p;
715 } */ *ap = v;
716 struct inode *ip = VTOI(ap->a_vp);
717 struct fs *fs = ip->i_fs;
718
719 /* Not supported for UFS1 file systems. */
720 if (fs->fs_magic == FS_UFS1_MAGIC)
721 return (EOPNOTSUPP);
722
723 /* XXX Not implemented for UFS2 file systems. */
724 return (EOPNOTSUPP);
725 }
726
727 int
728 ffs_getextattr(void *v)
729 {
730 struct vop_getextattr_args /* {
731 struct vnode *a_vp;
732 int a_attrnamespace;
733 const char *a_name;
734 struct uio *a_uio;
735 size_t *a_size;
736 kauth_cred_t a_cred;
737 struct proc *a_p;
738 } */ *ap = v;
739 struct vnode *vp = ap->a_vp;
740 struct inode *ip = VTOI(vp);
741 struct fs *fs = ip->i_fs;
742
743 if (fs->fs_magic == FS_UFS1_MAGIC) {
744 #ifdef UFS_EXTATTR
745 int error;
746
747 fstrans_start(vp->v_mount, FSTRANS_SHARED);
748 error = ufs_getextattr(ap);
749 fstrans_done(vp->v_mount);
750 return error;
751 #else
752 return (EOPNOTSUPP);
753 #endif
754 }
755
756 /* XXX Not implemented for UFS2 file systems. */
757 return (EOPNOTSUPP);
758 }
759
760 int
761 ffs_setextattr(void *v)
762 {
763 struct vop_setextattr_args /* {
764 struct vnode *a_vp;
765 int a_attrnamespace;
766 const char *a_name;
767 struct uio *a_uio;
768 kauth_cred_t a_cred;
769 struct proc *a_p;
770 } */ *ap = v;
771 struct vnode *vp = ap->a_vp;
772 struct inode *ip = VTOI(vp);
773 struct fs *fs = ip->i_fs;
774
775 if (fs->fs_magic == FS_UFS1_MAGIC) {
776 #ifdef UFS_EXTATTR
777 int error;
778
779 fstrans_start(vp->v_mount, FSTRANS_SHARED);
780 error = ufs_setextattr(ap);
781 fstrans_done(vp->v_mount);
782 return error;
783 #else
784 return (EOPNOTSUPP);
785 #endif
786 }
787
788 /* XXX Not implemented for UFS2 file systems. */
789 return (EOPNOTSUPP);
790 }
791
792 int
793 ffs_listextattr(void *v)
794 {
795 struct vop_listextattr_args /* {
796 struct vnode *a_vp;
797 int a_attrnamespace;
798 struct uio *a_uio;
799 size_t *a_size;
800 kauth_cred_t a_cred;
801 struct proc *a_p;
802 } */ *ap = v;
803 struct inode *ip = VTOI(ap->a_vp);
804 struct fs *fs = ip->i_fs;
805
806 /* Not supported for UFS1 file systems. */
807 if (fs->fs_magic == FS_UFS1_MAGIC)
808 return (EOPNOTSUPP);
809
810 /* XXX Not implemented for UFS2 file systems. */
811 return (EOPNOTSUPP);
812 }
813
814 int
815 ffs_deleteextattr(void *v)
816 {
817 struct vop_deleteextattr_args /* {
818 struct vnode *a_vp;
819 int a_attrnamespace;
820 kauth_cred_t a_cred;
821 struct proc *a_p;
822 } */ *ap = v;
823 struct vnode *vp = ap->a_vp;
824 struct inode *ip = VTOI(vp);
825 struct fs *fs = ip->i_fs;
826
827 if (fs->fs_magic == FS_UFS1_MAGIC) {
828 #ifdef UFS_EXTATTR
829 int error;
830
831 fstrans_start(vp->v_mount, FSTRANS_SHARED);
832 error = ufs_deleteextattr(ap);
833 fstrans_done(vp->v_mount);
834 return error;
835 #else
836 return (EOPNOTSUPP);
837 #endif
838 }
839
840 /* XXX Not implemented for UFS2 file systems. */
841 return (EOPNOTSUPP);
842 }
843
844 /*
845 * Lock the node.
846 */
847 int
848 ffs_lock(void *v)
849 {
850 struct vop_lock_args /* {
851 struct vnode *a_vp;
852 int a_flags;
853 } */ *ap = v;
854 struct vnode *vp = ap->a_vp;
855 struct mount *mp = vp->v_mount;
856 int flags = ap->a_flags;
857
858 if ((flags & LK_INTERLOCK) != 0) {
859 mutex_exit(&vp->v_interlock);
860 flags &= ~LK_INTERLOCK;
861 }
862
863 /*
864 * Fake lock during file system suspension.
865 */
866 if ((vp->v_type == VREG || vp->v_type == VDIR) &&
867 fstrans_is_owner(mp) &&
868 fstrans_getstate(mp) == FSTRANS_SUSPENDING) {
869 return 0;
870 }
871
872 return (vlockmgr(vp->v_vnlock, flags));
873 }
874
875 /*
876 * Unlock the node.
877 */
878 int
879 ffs_unlock(void *v)
880 {
881 struct vop_unlock_args /* {
882 struct vnode *a_vp;
883 int a_flags;
884 } */ *ap = v;
885 struct vnode *vp = ap->a_vp;
886 struct mount *mp = vp->v_mount;
887
888 KASSERT(ap->a_flags == 0);
889
890 /*
891 * Fake unlock during file system suspension.
892 */
893 if ((vp->v_type == VREG || vp->v_type == VDIR) &&
894 fstrans_is_owner(mp) &&
895 fstrans_getstate(mp) == FSTRANS_SUSPENDING) {
896 return 0;
897 }
898 return (vlockmgr(vp->v_vnlock, LK_RELEASE));
899 }
900
901 /*
902 * Return whether or not the node is locked.
903 */
904 int
905 ffs_islocked(void *v)
906 {
907 struct vop_islocked_args /* {
908 struct vnode *a_vp;
909 } */ *ap = v;
910 struct vnode *vp = ap->a_vp;
911
912 return (vlockstatus(vp->v_vnlock));
913 }
914