Home | History | Annotate | Line # | Download | only in ffs
ffs_vnops.c revision 1.126
      1 /*	$NetBSD: ffs_vnops.c,v 1.126 2017/03/01 10:42:45 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.126 2017/03/01 10:42:45 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 
     87 #include <miscfs/fifofs/fifo.h>
     88 #include <miscfs/genfs/genfs.h>
     89 #include <miscfs/specfs/specdev.h>
     90 
     91 #include <ufs/ufs/inode.h>
     92 #include <ufs/ufs/dir.h>
     93 #include <ufs/ufs/ufs_extern.h>
     94 #include <ufs/ufs/ufsmount.h>
     95 #include <ufs/ufs/ufs_wapbl.h>
     96 
     97 #include <ufs/ffs/fs.h>
     98 #include <ufs/ffs/ffs_extern.h>
     99 
    100 #include <uvm/uvm.h>
    101 
    102 /* Global vfs data structures for ufs. */
    103 int (**ffs_vnodeop_p)(void *);
    104 const struct vnodeopv_entry_desc ffs_vnodeop_entries[] = {
    105 	{ &vop_default_desc, vn_default_error },
    106 	{ &vop_lookup_desc, ufs_lookup },		/* lookup */
    107 	{ &vop_create_desc, ufs_create },		/* create */
    108 	{ &vop_whiteout_desc, ufs_whiteout },		/* whiteout */
    109 	{ &vop_mknod_desc, ufs_mknod },			/* mknod */
    110 	{ &vop_open_desc, ufs_open },			/* open */
    111 	{ &vop_close_desc, ufs_close },			/* close */
    112 	{ &vop_access_desc, ufs_access },		/* access */
    113 	{ &vop_getattr_desc, ufs_getattr },		/* getattr */
    114 	{ &vop_setattr_desc, ufs_setattr },		/* setattr */
    115 	{ &vop_read_desc, ffs_read },			/* read */
    116 	{ &vop_write_desc, ffs_write },			/* write */
    117 	{ &vop_fallocate_desc, genfs_eopnotsupp },	/* fallocate */
    118 	{ &vop_fdiscard_desc, genfs_eopnotsupp },	/* fdiscard */
    119 	{ &vop_ioctl_desc, ufs_ioctl },			/* ioctl */
    120 	{ &vop_fcntl_desc, ufs_fcntl },			/* fcntl */
    121 	{ &vop_poll_desc, ufs_poll },			/* poll */
    122 	{ &vop_kqfilter_desc, genfs_kqfilter },		/* kqfilter */
    123 	{ &vop_revoke_desc, ufs_revoke },		/* revoke */
    124 	{ &vop_mmap_desc, ufs_mmap },			/* mmap */
    125 	{ &vop_fsync_desc, ffs_fsync },			/* fsync */
    126 	{ &vop_seek_desc, ufs_seek },			/* seek */
    127 	{ &vop_remove_desc, ufs_remove },		/* remove */
    128 	{ &vop_link_desc, ufs_link },			/* link */
    129 	{ &vop_rename_desc, ufs_rename },		/* rename */
    130 	{ &vop_mkdir_desc, ufs_mkdir },			/* mkdir */
    131 	{ &vop_rmdir_desc, ufs_rmdir },			/* rmdir */
    132 	{ &vop_symlink_desc, ufs_symlink },		/* symlink */
    133 	{ &vop_readdir_desc, ufs_readdir },		/* readdir */
    134 	{ &vop_readlink_desc, ufs_readlink },		/* readlink */
    135 	{ &vop_abortop_desc, ufs_abortop },		/* abortop */
    136 	{ &vop_inactive_desc, ufs_inactive },		/* inactive */
    137 	{ &vop_reclaim_desc, ffs_reclaim },		/* reclaim */
    138 	{ &vop_lock_desc, ufs_lock },			/* lock */
    139 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
    140 	{ &vop_bmap_desc, ufs_bmap },			/* bmap */
    141 	{ &vop_strategy_desc, ufs_strategy },		/* strategy */
    142 	{ &vop_print_desc, ufs_print },			/* print */
    143 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
    144 	{ &vop_pathconf_desc, ufs_pathconf },		/* pathconf */
    145 	{ &vop_advlock_desc, ufs_advlock },		/* advlock */
    146 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    147 	{ &vop_getpages_desc, genfs_getpages },		/* getpages */
    148 	{ &vop_putpages_desc, genfs_putpages },		/* putpages */
    149 	{ &vop_openextattr_desc, ffs_openextattr },	/* openextattr */
    150 	{ &vop_closeextattr_desc, ffs_closeextattr },	/* closeextattr */
    151 	{ &vop_getextattr_desc, ffs_getextattr },	/* getextattr */
    152 	{ &vop_setextattr_desc, ffs_setextattr },	/* setextattr */
    153 	{ &vop_listextattr_desc, ffs_listextattr },	/* listextattr */
    154 	{ &vop_deleteextattr_desc, ffs_deleteextattr },	/* deleteextattr */
    155 	{ NULL, NULL }
    156 };
    157 const struct vnodeopv_desc ffs_vnodeop_opv_desc =
    158 	{ &ffs_vnodeop_p, ffs_vnodeop_entries };
    159 
    160 int (**ffs_specop_p)(void *);
    161 const struct vnodeopv_entry_desc ffs_specop_entries[] = {
    162 	{ &vop_default_desc, vn_default_error },
    163 	{ &vop_lookup_desc, spec_lookup },		/* lookup */
    164 	{ &vop_create_desc, spec_create },		/* create */
    165 	{ &vop_mknod_desc, spec_mknod },		/* mknod */
    166 	{ &vop_open_desc, spec_open },			/* open */
    167 	{ &vop_close_desc, ufsspec_close },		/* close */
    168 	{ &vop_access_desc, ufs_access },		/* access */
    169 	{ &vop_getattr_desc, ufs_getattr },		/* getattr */
    170 	{ &vop_setattr_desc, ufs_setattr },		/* setattr */
    171 	{ &vop_read_desc, ufsspec_read },		/* read */
    172 	{ &vop_write_desc, ufsspec_write },		/* write */
    173 	{ &vop_fallocate_desc, spec_fallocate },	/* fallocate */
    174 	{ &vop_fdiscard_desc, spec_fdiscard },		/* fdiscard */
    175 	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */
    176 	{ &vop_fcntl_desc, ufs_fcntl },			/* fcntl */
    177 	{ &vop_poll_desc, spec_poll },			/* poll */
    178 	{ &vop_kqfilter_desc, spec_kqfilter },		/* kqfilter */
    179 	{ &vop_revoke_desc, spec_revoke },		/* revoke */
    180 	{ &vop_mmap_desc, spec_mmap },			/* mmap */
    181 	{ &vop_fsync_desc, ffs_spec_fsync },		/* fsync */
    182 	{ &vop_seek_desc, spec_seek },			/* seek */
    183 	{ &vop_remove_desc, spec_remove },		/* remove */
    184 	{ &vop_link_desc, spec_link },			/* link */
    185 	{ &vop_rename_desc, spec_rename },		/* rename */
    186 	{ &vop_mkdir_desc, spec_mkdir },		/* mkdir */
    187 	{ &vop_rmdir_desc, spec_rmdir },		/* rmdir */
    188 	{ &vop_symlink_desc, spec_symlink },		/* symlink */
    189 	{ &vop_readdir_desc, spec_readdir },		/* readdir */
    190 	{ &vop_readlink_desc, spec_readlink },		/* readlink */
    191 	{ &vop_abortop_desc, spec_abortop },		/* abortop */
    192 	{ &vop_inactive_desc, ufs_inactive },		/* inactive */
    193 	{ &vop_reclaim_desc, ffs_reclaim },		/* reclaim */
    194 	{ &vop_lock_desc, ufs_lock },			/* lock */
    195 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
    196 	{ &vop_bmap_desc, spec_bmap },			/* bmap */
    197 	{ &vop_strategy_desc, spec_strategy },		/* strategy */
    198 	{ &vop_print_desc, ufs_print },			/* print */
    199 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
    200 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
    201 	{ &vop_advlock_desc, spec_advlock },		/* advlock */
    202 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    203 	{ &vop_getpages_desc, spec_getpages },		/* getpages */
    204 	{ &vop_putpages_desc, spec_putpages },		/* putpages */
    205 	{ &vop_openextattr_desc, ffs_openextattr },	/* openextattr */
    206 	{ &vop_closeextattr_desc, ffs_closeextattr },	/* closeextattr */
    207 	{ &vop_getextattr_desc, ffs_getextattr },	/* getextattr */
    208 	{ &vop_setextattr_desc, ffs_setextattr },	/* setextattr */
    209 	{ &vop_listextattr_desc, ffs_listextattr },	/* listextattr */
    210 	{ &vop_deleteextattr_desc, ffs_deleteextattr },	/* deleteextattr */
    211 	{ NULL, NULL }
    212 };
    213 const struct vnodeopv_desc ffs_specop_opv_desc =
    214 	{ &ffs_specop_p, ffs_specop_entries };
    215 
    216 int (**ffs_fifoop_p)(void *);
    217 const struct vnodeopv_entry_desc ffs_fifoop_entries[] = {
    218 	{ &vop_default_desc, vn_default_error },
    219 	{ &vop_lookup_desc, vn_fifo_bypass },		/* lookup */
    220 	{ &vop_create_desc, vn_fifo_bypass },		/* create */
    221 	{ &vop_mknod_desc, vn_fifo_bypass },		/* mknod */
    222 	{ &vop_open_desc, vn_fifo_bypass },		/* open */
    223 	{ &vop_close_desc, ufsfifo_close },		/* close */
    224 	{ &vop_access_desc, ufs_access },		/* access */
    225 	{ &vop_getattr_desc, ufs_getattr },		/* getattr */
    226 	{ &vop_setattr_desc, ufs_setattr },		/* setattr */
    227 	{ &vop_read_desc, ufsfifo_read },		/* read */
    228 	{ &vop_write_desc, ufsfifo_write },		/* write */
    229 	{ &vop_fallocate_desc, vn_fifo_bypass },	/* fallocate */
    230 	{ &vop_fdiscard_desc, vn_fifo_bypass },		/* fdiscard */
    231 	{ &vop_ioctl_desc, vn_fifo_bypass },		/* ioctl */
    232 	{ &vop_fcntl_desc, ufs_fcntl },			/* fcntl */
    233 	{ &vop_poll_desc, vn_fifo_bypass },		/* poll */
    234 	{ &vop_kqfilter_desc, vn_fifo_bypass },		/* kqfilter */
    235 	{ &vop_revoke_desc, vn_fifo_bypass },		/* revoke */
    236 	{ &vop_mmap_desc, vn_fifo_bypass },		/* mmap */
    237 	{ &vop_fsync_desc, ffs_fsync },			/* fsync */
    238 	{ &vop_seek_desc, vn_fifo_bypass },		/* seek */
    239 	{ &vop_remove_desc, vn_fifo_bypass },		/* remove */
    240 	{ &vop_link_desc, vn_fifo_bypass },		/* link */
    241 	{ &vop_rename_desc, vn_fifo_bypass },		/* rename */
    242 	{ &vop_mkdir_desc, vn_fifo_bypass },		/* mkdir */
    243 	{ &vop_rmdir_desc, vn_fifo_bypass },		/* rmdir */
    244 	{ &vop_symlink_desc, vn_fifo_bypass },		/* symlink */
    245 	{ &vop_readdir_desc, vn_fifo_bypass },		/* readdir */
    246 	{ &vop_readlink_desc, vn_fifo_bypass },		/* readlink */
    247 	{ &vop_abortop_desc, vn_fifo_bypass },		/* abortop */
    248 	{ &vop_inactive_desc, ufs_inactive },		/* inactive */
    249 	{ &vop_reclaim_desc, ffs_reclaim },		/* reclaim */
    250 	{ &vop_lock_desc, ufs_lock },			/* lock */
    251 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
    252 	{ &vop_bmap_desc, vn_fifo_bypass },		/* bmap */
    253 	{ &vop_strategy_desc, vn_fifo_bypass },		/* strategy */
    254 	{ &vop_print_desc, ufs_print },			/* print */
    255 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
    256 	{ &vop_pathconf_desc, vn_fifo_bypass },		/* pathconf */
    257 	{ &vop_advlock_desc, vn_fifo_bypass },		/* advlock */
    258 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    259 	{ &vop_putpages_desc, vn_fifo_bypass }, 	/* putpages */
    260 	{ &vop_openextattr_desc, ffs_openextattr },	/* openextattr */
    261 	{ &vop_closeextattr_desc, ffs_closeextattr },	/* closeextattr */
    262 	{ &vop_getextattr_desc, ffs_getextattr },	/* getextattr */
    263 	{ &vop_setextattr_desc, ffs_setextattr },	/* setextattr */
    264 	{ &vop_listextattr_desc, ffs_listextattr },	/* listextattr */
    265 	{ &vop_deleteextattr_desc, ffs_deleteextattr },	/* deleteextattr */
    266 	{ NULL, NULL }
    267 };
    268 const struct vnodeopv_desc ffs_fifoop_opv_desc =
    269 	{ &ffs_fifoop_p, ffs_fifoop_entries };
    270 
    271 #include <ufs/ufs/ufs_readwrite.c>
    272 
    273 int
    274 ffs_spec_fsync(void *v)
    275 {
    276 	struct vop_fsync_args /* {
    277 		struct vnode *a_vp;
    278 		kauth_cred_t a_cred;
    279 		int a_flags;
    280 		off_t a_offlo;
    281 		off_t a_offhi;
    282 		struct lwp *a_l;
    283 	} */ *ap = v;
    284 	int error, flags, uflags;
    285 	struct vnode *vp;
    286 	struct mount *mp;
    287 
    288 	flags = ap->a_flags;
    289 	uflags = UPDATE_CLOSE | ((flags & FSYNC_WAIT) ? UPDATE_WAIT : 0);
    290 	vp = ap->a_vp;
    291 	mp = vp->v_mount;
    292 
    293 	error = spec_fsync(v);
    294 	if (error)
    295 		goto out;
    296 
    297 #ifdef WAPBL
    298 	if (mp && mp->mnt_wapbl) {
    299 		/*
    300 		 * Don't bother writing out metadata if the syncer is
    301 		 * making the request.  We will let the sync vnode
    302 		 * write it out in a single burst through a call to
    303 		 * VFS_SYNC().
    304 		 */
    305 		if ((flags & (FSYNC_DATAONLY | FSYNC_LAZY)) != 0)
    306 			goto out;
    307 		if ((VTOI(vp)->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE
    308 		    | IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) != 0) {
    309 			error = UFS_WAPBL_BEGIN(mp);
    310 			if (error != 0)
    311 				goto out;
    312 			error = ffs_update(vp, NULL, NULL, uflags);
    313 			UFS_WAPBL_END(mp);
    314 		}
    315 		goto out;
    316 	}
    317 #endif /* WAPBL */
    318 
    319 	error = ffs_update(vp, NULL, NULL, uflags);
    320 
    321 out:
    322 	return error;
    323 }
    324 
    325 int
    326 ffs_fsync(void *v)
    327 {
    328 	struct vop_fsync_args /* {
    329 		struct vnode *a_vp;
    330 		kauth_cred_t a_cred;
    331 		int a_flags;
    332 		off_t a_offlo;
    333 		off_t a_offhi;
    334 		struct lwp *a_l;
    335 	} */ *ap = v;
    336 	struct buf *bp;
    337 	int num, error, i;
    338 	struct indir ia[UFS_NIADDR + 1];
    339 	int bsize;
    340 	daddr_t blk_high;
    341 	struct vnode *vp;
    342 	struct mount *mp;
    343 
    344 	vp = ap->a_vp;
    345 	mp = vp->v_mount;
    346 
    347 	if ((ap->a_offlo == 0 && ap->a_offhi == 0) || (vp->v_type != VREG)) {
    348 		error = ffs_full_fsync(vp, ap->a_flags);
    349 		goto out;
    350 	}
    351 
    352 	bsize = mp->mnt_stat.f_iosize;
    353 	blk_high = ap->a_offhi / bsize;
    354 	if (ap->a_offhi % bsize != 0)
    355 		blk_high++;
    356 
    357 	/*
    358 	 * First, flush all pages in range.
    359 	 */
    360 
    361 	mutex_enter(vp->v_interlock);
    362 	error = VOP_PUTPAGES(vp, trunc_page(ap->a_offlo),
    363 	    round_page(ap->a_offhi), PGO_CLEANIT |
    364 	    ((ap->a_flags & FSYNC_WAIT) ? PGO_SYNCIO : 0));
    365 	if (error) {
    366 		goto out;
    367 	}
    368 
    369 #ifdef WAPBL
    370 	KASSERT(vp->v_type == VREG);
    371 	if (mp->mnt_wapbl) {
    372 		/*
    373 		 * Don't bother writing out metadata if the syncer is
    374 		 * making the request.  We will let the sync vnode
    375 		 * write it out in a single burst through a call to
    376 		 * VFS_SYNC().
    377 		 */
    378 		if ((ap->a_flags & (FSYNC_DATAONLY | FSYNC_LAZY)) != 0) {
    379 			return 0;
    380 		}
    381 		error = 0;
    382 		if (vp->v_tag == VT_UFS && VTOI(vp)->i_flag &
    383 		    (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY |
    384 				 IN_MODIFIED | IN_ACCESSED)) {
    385 			error = UFS_WAPBL_BEGIN(mp);
    386 			if (error) {
    387 				return error;
    388 			}
    389 			error = ffs_update(vp, NULL, NULL, UPDATE_CLOSE |
    390 			    ((ap->a_flags & FSYNC_WAIT) ? UPDATE_WAIT : 0));
    391 			UFS_WAPBL_END(mp);
    392 		}
    393 		if (error || (ap->a_flags & FSYNC_NOLOG) != 0) {
    394 			return error;
    395 		}
    396 		error = wapbl_flush(mp->mnt_wapbl, 0);
    397 		return error;
    398 	}
    399 #endif /* WAPBL */
    400 
    401 	/*
    402 	 * Then, flush indirect blocks.
    403 	 */
    404 
    405 	if (blk_high >= UFS_NDADDR) {
    406 		error = ufs_getlbns(vp, blk_high, ia, &num);
    407 		if (error)
    408 			goto out;
    409 
    410 		mutex_enter(&bufcache_lock);
    411 		for (i = 0; i < num; i++) {
    412 			if ((bp = incore(vp, ia[i].in_lbn)) == NULL)
    413 				continue;
    414 			if ((bp->b_cflags & BC_BUSY) != 0 ||
    415 			    (bp->b_oflags & BO_DELWRI) == 0)
    416 				continue;
    417 			bp->b_cflags |= BC_BUSY | BC_VFLUSH;
    418 			mutex_exit(&bufcache_lock);
    419 			bawrite(bp);
    420 			mutex_enter(&bufcache_lock);
    421 		}
    422 		mutex_exit(&bufcache_lock);
    423 	}
    424 
    425 	if (ap->a_flags & FSYNC_WAIT) {
    426 		mutex_enter(vp->v_interlock);
    427 		while (vp->v_numoutput > 0)
    428 			cv_wait(&vp->v_cv, vp->v_interlock);
    429 		mutex_exit(vp->v_interlock);
    430 	}
    431 
    432 	error = ffs_update(vp, NULL, NULL, UPDATE_CLOSE |
    433 	    (((ap->a_flags & (FSYNC_WAIT | FSYNC_DATAONLY)) == FSYNC_WAIT)
    434 	    ? UPDATE_WAIT : 0));
    435 
    436 	if (error == 0 && ap->a_flags & FSYNC_CACHE) {
    437 		int l = 0;
    438 		VOP_IOCTL(VTOI(vp)->i_devvp, DIOCCACHESYNC, &l, FWRITE,
    439 			curlwp->l_cred);
    440 	}
    441 
    442 out:
    443 	return error;
    444 }
    445 
    446 /*
    447  * Synch an open file.  Called for VOP_FSYNC().
    448  */
    449 /* ARGSUSED */
    450 int
    451 ffs_full_fsync(struct vnode *vp, int flags)
    452 {
    453 	int error, i, uflags;
    454 
    455 	KASSERT(vp->v_tag == VT_UFS);
    456 	KASSERT(VTOI(vp) != NULL);
    457 	KASSERT(vp->v_type != VCHR && vp->v_type != VBLK);
    458 
    459 	uflags = UPDATE_CLOSE | ((flags & FSYNC_WAIT) ? UPDATE_WAIT : 0);
    460 
    461 #ifdef WAPBL
    462 	struct mount *mp = vp->v_mount;
    463 	if (mp && mp->mnt_wapbl) {
    464 
    465 		/*
    466 		 * Flush all dirty data associated with the vnode.
    467 		 */
    468 		if (vp->v_type == VREG) {
    469 			int pflags = PGO_ALLPAGES | PGO_CLEANIT;
    470 
    471 			if ((flags & FSYNC_LAZY))
    472 				pflags |= PGO_LAZY;
    473 			if ((flags & FSYNC_WAIT))
    474 				pflags |= PGO_SYNCIO;
    475 			mutex_enter(vp->v_interlock);
    476 			error = VOP_PUTPAGES(vp, 0, 0, pflags);
    477 			if (error)
    478 				return error;
    479 		}
    480 
    481 		/*
    482 		 * Don't bother writing out metadata if the syncer is
    483 		 * making the request.  We will let the sync vnode
    484 		 * write it out in a single burst through a call to
    485 		 * VFS_SYNC().
    486 		 */
    487 		if ((flags & (FSYNC_DATAONLY | FSYNC_LAZY)) != 0)
    488 			return 0;
    489 
    490 		if ((VTOI(vp)->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE
    491 		    | IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) != 0) {
    492 			error = UFS_WAPBL_BEGIN(mp);
    493 			if (error)
    494 				return error;
    495 			error = ffs_update(vp, NULL, NULL, uflags);
    496 			UFS_WAPBL_END(mp);
    497 		} else {
    498 			error = 0;
    499 		}
    500 		if (error || (flags & FSYNC_NOLOG) != 0)
    501 			return error;
    502 
    503 		/*
    504 		 * Don't flush the log if the vnode being flushed
    505 		 * contains no dirty buffers that could be in the log.
    506 		 */
    507 		if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
    508 			error = wapbl_flush(mp->mnt_wapbl, 0);
    509 			if (error)
    510 				return error;
    511 		}
    512 
    513 		if ((flags & FSYNC_WAIT) != 0) {
    514 			mutex_enter(vp->v_interlock);
    515 			while (vp->v_numoutput != 0)
    516 				cv_wait(&vp->v_cv, vp->v_interlock);
    517 			mutex_exit(vp->v_interlock);
    518 		}
    519 
    520 		return error;
    521 	}
    522 #endif /* WAPBL */
    523 
    524 	error = vflushbuf(vp, flags);
    525 	if (error == 0)
    526 		error = ffs_update(vp, NULL, NULL, uflags);
    527 	if (error == 0 && (flags & FSYNC_CACHE) != 0) {
    528 		i = 1;
    529 		(void)VOP_IOCTL(VTOI(vp)->i_devvp, DIOCCACHESYNC, &i, FWRITE,
    530 		    kauth_cred_get());
    531 	}
    532 
    533 	return error;
    534 }
    535 
    536 /*
    537  * Reclaim an inode so that it can be used for other purposes.
    538  */
    539 int
    540 ffs_reclaim(void *v)
    541 {
    542 	struct vop_reclaim_args /* {
    543 		struct vnode *a_vp;
    544 		struct lwp *a_l;
    545 	} */ *ap = v;
    546 	struct vnode *vp = ap->a_vp;
    547 	struct inode *ip = VTOI(vp);
    548 	struct mount *mp = vp->v_mount;
    549 	struct ufsmount *ump = ip->i_ump;
    550 	void *data;
    551 	int error;
    552 
    553 	/*
    554 	 * The inode must be freed and updated before being removed
    555 	 * from its hash chain.  Other threads trying to gain a hold
    556 	 * or lock on the inode will be stalled.
    557 	 */
    558 	error = UFS_WAPBL_BEGIN(mp);
    559 	if (error) {
    560 		return error;
    561 	}
    562 	if (ip->i_nlink <= 0 && ip->i_omode != 0 &&
    563 	    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
    564 		ffs_vfree(vp, ip->i_number, ip->i_omode);
    565 	UFS_WAPBL_END(mp);
    566 	if ((error = ufs_reclaim(vp)) != 0) {
    567 		return (error);
    568 	}
    569 	if (ip->i_din.ffs1_din != NULL) {
    570 		if (ump->um_fstype == UFS1)
    571 			pool_cache_put(ffs_dinode1_cache, ip->i_din.ffs1_din);
    572 		else
    573 			pool_cache_put(ffs_dinode2_cache, ip->i_din.ffs2_din);
    574 	}
    575 	/*
    576 	 * To interlock with ffs_sync().
    577 	 */
    578 	genfs_node_destroy(vp);
    579 	mutex_enter(vp->v_interlock);
    580 	data = vp->v_data;
    581 	vp->v_data = NULL;
    582 	mutex_exit(vp->v_interlock);
    583 
    584 	/*
    585 	 * XXX MFS ends up here, too, to free an inode.  Should we create
    586 	 * XXX a separate pool for MFS inodes?
    587 	 */
    588 	pool_cache_put(ffs_inode_cache, data);
    589 	return (0);
    590 }
    591 
    592 /*
    593  * Return the last logical file offset that should be written for this file
    594  * if we're doing a write that ends at "size".
    595  */
    596 
    597 void
    598 ffs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
    599 {
    600 	struct inode *ip = VTOI(vp);
    601 	struct fs *fs = ip->i_fs;
    602 	daddr_t olbn, nlbn;
    603 
    604 	olbn = ffs_lblkno(fs, ip->i_size);
    605 	nlbn = ffs_lblkno(fs, size);
    606 	if (nlbn < UFS_NDADDR && olbn <= nlbn) {
    607 		*eobp = ffs_fragroundup(fs, size);
    608 	} else {
    609 		*eobp = ffs_blkroundup(fs, size);
    610 	}
    611 }
    612 
    613 int
    614 ffs_openextattr(void *v)
    615 {
    616 	struct vop_openextattr_args /* {
    617 		struct vnode *a_vp;
    618 		kauth_cred_t a_cred;
    619 		struct proc *a_p;
    620 	} */ *ap = v;
    621 	struct inode *ip = VTOI(ap->a_vp);
    622 	struct fs *fs = ip->i_fs;
    623 
    624 	/* Not supported for UFS1 file systems. */
    625 	if (fs->fs_magic == FS_UFS1_MAGIC)
    626 		return (EOPNOTSUPP);
    627 
    628 	/* XXX Not implemented for UFS2 file systems. */
    629 	return (EOPNOTSUPP);
    630 }
    631 
    632 int
    633 ffs_closeextattr(void *v)
    634 {
    635 	struct vop_closeextattr_args /* {
    636 		struct vnode *a_vp;
    637 		int a_commit;
    638 		kauth_cred_t a_cred;
    639 		struct proc *a_p;
    640 	} */ *ap = v;
    641 	struct inode *ip = VTOI(ap->a_vp);
    642 	struct fs *fs = ip->i_fs;
    643 
    644 	/* Not supported for UFS1 file systems. */
    645 	if (fs->fs_magic == FS_UFS1_MAGIC)
    646 		return (EOPNOTSUPP);
    647 
    648 	/* XXX Not implemented for UFS2 file systems. */
    649 	return (EOPNOTSUPP);
    650 }
    651 
    652 int
    653 ffs_getextattr(void *v)
    654 {
    655 	struct vop_getextattr_args /* {
    656 		struct vnode *a_vp;
    657 		int a_attrnamespace;
    658 		const char *a_name;
    659 		struct uio *a_uio;
    660 		size_t *a_size;
    661 		kauth_cred_t a_cred;
    662 		struct proc *a_p;
    663 	} */ *ap = v;
    664 	struct vnode *vp = ap->a_vp;
    665 	struct inode *ip = VTOI(vp);
    666 	struct fs *fs = ip->i_fs;
    667 
    668 	if (fs->fs_magic == FS_UFS1_MAGIC) {
    669 #ifdef UFS_EXTATTR
    670 		int error;
    671 
    672 		error = ufs_getextattr(ap);
    673 		return error;
    674 #else
    675 		return (EOPNOTSUPP);
    676 #endif
    677 	}
    678 
    679 	/* XXX Not implemented for UFS2 file systems. */
    680 	return (EOPNOTSUPP);
    681 }
    682 
    683 int
    684 ffs_setextattr(void *v)
    685 {
    686 	struct vop_setextattr_args /* {
    687 		struct vnode *a_vp;
    688 		int a_attrnamespace;
    689 		const char *a_name;
    690 		struct uio *a_uio;
    691 		kauth_cred_t a_cred;
    692 		struct proc *a_p;
    693 	} */ *ap = v;
    694 	struct vnode *vp = ap->a_vp;
    695 	struct inode *ip = VTOI(vp);
    696 	struct fs *fs = ip->i_fs;
    697 
    698 	if (fs->fs_magic == FS_UFS1_MAGIC) {
    699 #ifdef UFS_EXTATTR
    700 		int error;
    701 
    702 		error = ufs_setextattr(ap);
    703 		return error;
    704 #else
    705 		return (EOPNOTSUPP);
    706 #endif
    707 	}
    708 
    709 	/* XXX Not implemented for UFS2 file systems. */
    710 	return (EOPNOTSUPP);
    711 }
    712 
    713 int
    714 ffs_listextattr(void *v)
    715 {
    716 	struct vop_listextattr_args /* {
    717 		struct vnode *a_vp;
    718 		int a_attrnamespace;
    719 		struct uio *a_uio;
    720 		size_t *a_size;
    721 		kauth_cred_t a_cred;
    722 		struct proc *a_p;
    723 	} */ *ap = v;
    724 	struct inode *ip = VTOI(ap->a_vp);
    725 	struct fs *fs = ip->i_fs;
    726 
    727 	if (fs->fs_magic == FS_UFS1_MAGIC) {
    728 #ifdef UFS_EXTATTR
    729 		int error;
    730 
    731 		error = ufs_listextattr(ap);
    732 		return error;
    733 #else
    734 		return (EOPNOTSUPP);
    735 #endif
    736 	}
    737 
    738 	/* XXX Not implemented for UFS2 file systems. */
    739 	return (EOPNOTSUPP);
    740 }
    741 
    742 int
    743 ffs_deleteextattr(void *v)
    744 {
    745 	struct vop_deleteextattr_args /* {
    746 		struct vnode *a_vp;
    747 		int a_attrnamespace;
    748 		kauth_cred_t a_cred;
    749 		struct proc *a_p;
    750 	} */ *ap = v;
    751 	struct vnode *vp = ap->a_vp;
    752 	struct inode *ip = VTOI(vp);
    753 	struct fs *fs = ip->i_fs;
    754 
    755 	if (fs->fs_magic == FS_UFS1_MAGIC) {
    756 #ifdef UFS_EXTATTR
    757 		int error;
    758 
    759 		error = ufs_deleteextattr(ap);
    760 		return error;
    761 #else
    762 		return (EOPNOTSUPP);
    763 #endif
    764 	}
    765 
    766 	/* XXX Not implemented for UFS2 file systems. */
    767 	return (EOPNOTSUPP);
    768 }
    769