Home | History | Annotate | Line # | Download | only in ffs
ffs_vnops.c revision 1.130
      1 /*	$NetBSD: ffs_vnops.c,v 1.130 2020/02/23 15:46:42 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.130 2020/02/23 15:46:42 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 
     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 
    287 	flags = ap->a_flags;
    288 	uflags = UPDATE_CLOSE | ((flags & FSYNC_WAIT) ? UPDATE_WAIT : 0);
    289 	vp = ap->a_vp;
    290 
    291 	error = spec_fsync(v);
    292 	if (error)
    293 		goto out;
    294 
    295 #ifdef WAPBL
    296 	struct mount *mp = vp->v_mount;
    297 
    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 	rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
    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 
    464 	if (mp && mp->mnt_wapbl) {
    465 
    466 		/*
    467 		 * Flush all dirty data associated with the vnode.
    468 		 */
    469 		if (vp->v_type == VREG) {
    470 			int pflags = PGO_ALLPAGES | PGO_CLEANIT;
    471 
    472 			if ((flags & FSYNC_LAZY))
    473 				pflags |= PGO_LAZY;
    474 			if ((flags & FSYNC_WAIT))
    475 				pflags |= PGO_SYNCIO;
    476 			rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
    477 			error = VOP_PUTPAGES(vp, 0, 0, pflags);
    478 			if (error)
    479 				return error;
    480 		}
    481 
    482 		/*
    483 		 * Don't bother writing out metadata if the syncer is
    484 		 * making the request.  We will let the sync vnode
    485 		 * write it out in a single burst through a call to
    486 		 * VFS_SYNC().
    487 		 */
    488 		if ((flags & (FSYNC_DATAONLY | FSYNC_LAZY)) != 0)
    489 			return 0;
    490 
    491 		if ((VTOI(vp)->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE
    492 		    | IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) != 0) {
    493 			error = UFS_WAPBL_BEGIN(mp);
    494 			if (error)
    495 				return error;
    496 			error = ffs_update(vp, NULL, NULL, uflags);
    497 			UFS_WAPBL_END(mp);
    498 		} else {
    499 			error = 0;
    500 		}
    501 		if (error || (flags & FSYNC_NOLOG) != 0)
    502 			return error;
    503 
    504 		/*
    505 		 * Don't flush the log if the vnode being flushed
    506 		 * contains no dirty buffers that could be in the log.
    507 		 */
    508 		if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
    509 			error = wapbl_flush(mp->mnt_wapbl, 0);
    510 			if (error)
    511 				return error;
    512 		}
    513 
    514 		if ((flags & FSYNC_WAIT) != 0) {
    515 			mutex_enter(vp->v_interlock);
    516 			while (vp->v_numoutput != 0)
    517 				cv_wait(&vp->v_cv, vp->v_interlock);
    518 			mutex_exit(vp->v_interlock);
    519 		}
    520 
    521 		return error;
    522 	}
    523 #endif /* WAPBL */
    524 
    525 	error = vflushbuf(vp, flags);
    526 	if (error == 0)
    527 		error = ffs_update(vp, NULL, NULL, uflags);
    528 	if (error == 0 && (flags & FSYNC_CACHE) != 0) {
    529 		i = 1;
    530 		(void)VOP_IOCTL(VTOI(vp)->i_devvp, DIOCCACHESYNC, &i, FWRITE,
    531 		    kauth_cred_get());
    532 	}
    533 
    534 	return error;
    535 }
    536 
    537 /*
    538  * Reclaim an inode so that it can be used for other purposes.
    539  */
    540 int
    541 ffs_reclaim(void *v)
    542 {
    543 	struct vop_reclaim_v2_args /* {
    544 		struct vnode *a_vp;
    545 		struct lwp *a_l;
    546 	} */ *ap = v;
    547 	struct vnode *vp = ap->a_vp;
    548 	struct inode *ip = VTOI(vp);
    549 	struct mount *mp = vp->v_mount;
    550 	struct ufsmount *ump = ip->i_ump;
    551 	void *data;
    552 	int error;
    553 
    554 	VOP_UNLOCK(vp);
    555 
    556 	/*
    557 	 * The inode must be freed and updated before being removed
    558 	 * from its hash chain.  Other threads trying to gain a hold
    559 	 * or lock on the inode will be stalled.
    560 	 */
    561 	error = UFS_WAPBL_BEGIN(mp);
    562 	if (error) {
    563 		return error;
    564 	}
    565 	if (ip->i_nlink <= 0 && ip->i_omode != 0 &&
    566 	    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
    567 		ffs_vfree(vp, ip->i_number, ip->i_omode);
    568 	UFS_WAPBL_END(mp);
    569 	if ((error = ufs_reclaim(vp)) != 0) {
    570 		return (error);
    571 	}
    572 	if (ip->i_din.ffs1_din != NULL) {
    573 		if (ump->um_fstype == UFS1)
    574 			pool_cache_put(ffs_dinode1_cache, ip->i_din.ffs1_din);
    575 		else
    576 			pool_cache_put(ffs_dinode2_cache, ip->i_din.ffs2_din);
    577 	}
    578 	/*
    579 	 * To interlock with ffs_sync().
    580 	 */
    581 	genfs_node_destroy(vp);
    582 	mutex_enter(vp->v_interlock);
    583 	data = vp->v_data;
    584 	vp->v_data = NULL;
    585 	mutex_exit(vp->v_interlock);
    586 
    587 	/*
    588 	 * XXX MFS ends up here, too, to free an inode.  Should we create
    589 	 * XXX a separate pool for MFS inodes?
    590 	 */
    591 	pool_cache_put(ffs_inode_cache, data);
    592 	return (0);
    593 }
    594 
    595 /*
    596  * Return the last logical file offset that should be written for this file
    597  * if we're doing a write that ends at "size".
    598  */
    599 
    600 void
    601 ffs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
    602 {
    603 	struct inode *ip = VTOI(vp);
    604 	struct fs *fs = ip->i_fs;
    605 	daddr_t olbn, nlbn;
    606 
    607 	olbn = ffs_lblkno(fs, ip->i_size);
    608 	nlbn = ffs_lblkno(fs, size);
    609 	if (nlbn < UFS_NDADDR && olbn <= nlbn) {
    610 		*eobp = ffs_fragroundup(fs, size);
    611 	} else {
    612 		*eobp = ffs_blkroundup(fs, size);
    613 	}
    614 }
    615 
    616 int
    617 ffs_openextattr(void *v)
    618 {
    619 	struct vop_openextattr_args /* {
    620 		struct vnode *a_vp;
    621 		kauth_cred_t a_cred;
    622 		struct proc *a_p;
    623 	} */ *ap = v;
    624 	struct inode *ip = VTOI(ap->a_vp);
    625 	struct fs *fs = ip->i_fs;
    626 
    627 	/* Not supported for UFS1 file systems. */
    628 	if (fs->fs_magic == FS_UFS1_MAGIC)
    629 		return (EOPNOTSUPP);
    630 
    631 	/* XXX Not implemented for UFS2 file systems. */
    632 	return (EOPNOTSUPP);
    633 }
    634 
    635 int
    636 ffs_closeextattr(void *v)
    637 {
    638 	struct vop_closeextattr_args /* {
    639 		struct vnode *a_vp;
    640 		int a_commit;
    641 		kauth_cred_t a_cred;
    642 		struct proc *a_p;
    643 	} */ *ap = v;
    644 	struct inode *ip = VTOI(ap->a_vp);
    645 	struct fs *fs = ip->i_fs;
    646 
    647 	/* Not supported for UFS1 file systems. */
    648 	if (fs->fs_magic == FS_UFS1_MAGIC)
    649 		return (EOPNOTSUPP);
    650 
    651 	/* XXX Not implemented for UFS2 file systems. */
    652 	return (EOPNOTSUPP);
    653 }
    654 
    655 int
    656 ffs_getextattr(void *v)
    657 {
    658 	struct vop_getextattr_args /* {
    659 		struct vnode *a_vp;
    660 		int a_attrnamespace;
    661 		const char *a_name;
    662 		struct uio *a_uio;
    663 		size_t *a_size;
    664 		kauth_cred_t a_cred;
    665 		struct proc *a_p;
    666 	} */ *ap = v;
    667 	struct vnode *vp = ap->a_vp;
    668 	struct inode *ip = VTOI(vp);
    669 	struct fs *fs = ip->i_fs;
    670 
    671 	if (fs->fs_magic == FS_UFS1_MAGIC) {
    672 #ifdef UFS_EXTATTR
    673 		int error;
    674 
    675 		error = ufs_getextattr(ap);
    676 		return error;
    677 #else
    678 		return (EOPNOTSUPP);
    679 #endif
    680 	}
    681 
    682 	/* XXX Not implemented for UFS2 file systems. */
    683 	return (EOPNOTSUPP);
    684 }
    685 
    686 int
    687 ffs_setextattr(void *v)
    688 {
    689 	struct vop_setextattr_args /* {
    690 		struct vnode *a_vp;
    691 		int a_attrnamespace;
    692 		const char *a_name;
    693 		struct uio *a_uio;
    694 		kauth_cred_t a_cred;
    695 		struct proc *a_p;
    696 	} */ *ap = v;
    697 	struct vnode *vp = ap->a_vp;
    698 	struct inode *ip = VTOI(vp);
    699 	struct fs *fs = ip->i_fs;
    700 
    701 	if (fs->fs_magic == FS_UFS1_MAGIC) {
    702 #ifdef UFS_EXTATTR
    703 		int error;
    704 
    705 		error = ufs_setextattr(ap);
    706 		return error;
    707 #else
    708 		return (EOPNOTSUPP);
    709 #endif
    710 	}
    711 
    712 	/* XXX Not implemented for UFS2 file systems. */
    713 	return (EOPNOTSUPP);
    714 }
    715 
    716 int
    717 ffs_listextattr(void *v)
    718 {
    719 	struct vop_listextattr_args /* {
    720 		struct vnode *a_vp;
    721 		int a_attrnamespace;
    722 		struct uio *a_uio;
    723 		size_t *a_size;
    724 		kauth_cred_t a_cred;
    725 		struct proc *a_p;
    726 	} */ *ap = v;
    727 	struct inode *ip = VTOI(ap->a_vp);
    728 	struct fs *fs = ip->i_fs;
    729 
    730 	if (fs->fs_magic == FS_UFS1_MAGIC) {
    731 #ifdef UFS_EXTATTR
    732 		int error;
    733 
    734 		error = ufs_listextattr(ap);
    735 		return error;
    736 #else
    737 		return (EOPNOTSUPP);
    738 #endif
    739 	}
    740 
    741 	/* XXX Not implemented for UFS2 file systems. */
    742 	return (EOPNOTSUPP);
    743 }
    744 
    745 int
    746 ffs_deleteextattr(void *v)
    747 {
    748 	struct vop_deleteextattr_args /* {
    749 		struct vnode *a_vp;
    750 		int a_attrnamespace;
    751 		kauth_cred_t a_cred;
    752 		struct proc *a_p;
    753 	} */ *ap = v;
    754 	struct vnode *vp = ap->a_vp;
    755 	struct inode *ip = VTOI(vp);
    756 	struct fs *fs = ip->i_fs;
    757 
    758 	if (fs->fs_magic == FS_UFS1_MAGIC) {
    759 #ifdef UFS_EXTATTR
    760 		int error;
    761 
    762 		error = ufs_deleteextattr(ap);
    763 		return error;
    764 #else
    765 		return (EOPNOTSUPP);
    766 #endif
    767 	}
    768 
    769 	/* XXX Not implemented for UFS2 file systems. */
    770 	return (EOPNOTSUPP);
    771 }
    772