Home | History | Annotate | Line # | Download | only in ffs
ffs_vnops.c revision 1.134
      1 /*	$NetBSD: ffs_vnops.c,v 1.134 2021/06/29 22:34:09 dholland 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.134 2021/06/29 22:34:09 dholland 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/acl.h>
     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 /* Global vfs data structures for ufs. */
    102 int (**ffs_vnodeop_p)(void *);
    103 const struct vnodeopv_entry_desc ffs_vnodeop_entries[] = {
    104 	{ &vop_default_desc, vn_default_error },
    105 	{ &vop_parsepath_desc, genfs_parsepath },	/* parsepath */
    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, genfs_access },		/* access */
    113 	{ &vop_accessx_desc, ufs_accessx },		/* accessx */
    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_fallocate_desc, genfs_eopnotsupp },	/* fallocate */
    119 	{ &vop_fdiscard_desc, genfs_eopnotsupp },	/* fdiscard */
    120 	{ &vop_ioctl_desc, ufs_ioctl },			/* ioctl */
    121 	{ &vop_fcntl_desc, ufs_fcntl },			/* fcntl */
    122 	{ &vop_poll_desc, ufs_poll },			/* poll */
    123 	{ &vop_kqfilter_desc, genfs_kqfilter },		/* kqfilter */
    124 	{ &vop_revoke_desc, ufs_revoke },		/* revoke */
    125 	{ &vop_mmap_desc, ufs_mmap },			/* mmap */
    126 	{ &vop_fsync_desc, ffs_fsync },			/* fsync */
    127 	{ &vop_seek_desc, ufs_seek },			/* seek */
    128 	{ &vop_remove_desc, ufs_remove },		/* remove */
    129 	{ &vop_link_desc, ufs_link },			/* link */
    130 	{ &vop_rename_desc, ufs_rename },		/* rename */
    131 	{ &vop_mkdir_desc, ufs_mkdir },			/* mkdir */
    132 	{ &vop_rmdir_desc, ufs_rmdir },			/* rmdir */
    133 	{ &vop_symlink_desc, ufs_symlink },		/* symlink */
    134 	{ &vop_readdir_desc, ufs_readdir },		/* readdir */
    135 	{ &vop_readlink_desc, ufs_readlink },		/* readlink */
    136 	{ &vop_abortop_desc, ufs_abortop },		/* abortop */
    137 	{ &vop_inactive_desc, ufs_inactive },		/* inactive */
    138 	{ &vop_reclaim_desc, ffs_reclaim },		/* reclaim */
    139 	{ &vop_lock_desc, ufs_lock },			/* lock */
    140 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
    141 	{ &vop_bmap_desc, ufs_bmap },			/* bmap */
    142 	{ &vop_strategy_desc, ufs_strategy },		/* strategy */
    143 	{ &vop_print_desc, ufs_print },			/* print */
    144 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
    145 	{ &vop_pathconf_desc, ufs_pathconf },		/* pathconf */
    146 	{ &vop_advlock_desc, ufs_advlock },		/* advlock */
    147 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    148 	{ &vop_getpages_desc, genfs_getpages },		/* getpages */
    149 	{ &vop_putpages_desc, genfs_putpages },		/* putpages */
    150 	{ &vop_openextattr_desc, ffs_openextattr },	/* openextattr */
    151 	{ &vop_closeextattr_desc, ffs_closeextattr },	/* closeextattr */
    152 	{ &vop_getextattr_desc, ffs_getextattr },	/* getextattr */
    153 	{ &vop_setextattr_desc, ffs_setextattr },	/* setextattr */
    154 	{ &vop_listextattr_desc, ffs_listextattr },	/* listextattr */
    155 	{ &vop_deleteextattr_desc, ffs_deleteextattr },	/* deleteextattr */
    156 	{ &vop_getacl_desc, ufs_getacl },		/* getacl */
    157 	{ &vop_setacl_desc, ufs_setacl },		/* setacl */
    158 	{ &vop_aclcheck_desc, ufs_aclcheck },		/* aclcheck */
    159 	{ NULL, NULL }
    160 };
    161 const struct vnodeopv_desc ffs_vnodeop_opv_desc =
    162 	{ &ffs_vnodeop_p, ffs_vnodeop_entries };
    163 
    164 int (**ffs_specop_p)(void *);
    165 const struct vnodeopv_entry_desc ffs_specop_entries[] = {
    166 	{ &vop_default_desc, vn_default_error },
    167 	{ &vop_parsepath_desc, genfs_parsepath },	/* parsepath */
    168 	{ &vop_lookup_desc, spec_lookup },		/* lookup */
    169 	{ &vop_create_desc, spec_create },		/* create */
    170 	{ &vop_mknod_desc, spec_mknod },		/* mknod */
    171 	{ &vop_open_desc, spec_open },			/* open */
    172 	{ &vop_close_desc, ufsspec_close },		/* close */
    173 	{ &vop_access_desc, genfs_access },		/* access */
    174 	{ &vop_accessx_desc, ufs_accessx },		/* accessx */
    175 	{ &vop_getattr_desc, ufs_getattr },		/* getattr */
    176 	{ &vop_setattr_desc, ufs_setattr },		/* setattr */
    177 	{ &vop_read_desc, ufsspec_read },		/* read */
    178 	{ &vop_write_desc, ufsspec_write },		/* write */
    179 	{ &vop_fallocate_desc, spec_fallocate },	/* fallocate */
    180 	{ &vop_fdiscard_desc, spec_fdiscard },		/* fdiscard */
    181 	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */
    182 	{ &vop_fcntl_desc, ufs_fcntl },			/* fcntl */
    183 	{ &vop_poll_desc, spec_poll },			/* poll */
    184 	{ &vop_kqfilter_desc, spec_kqfilter },		/* kqfilter */
    185 	{ &vop_revoke_desc, spec_revoke },		/* revoke */
    186 	{ &vop_mmap_desc, spec_mmap },			/* mmap */
    187 	{ &vop_fsync_desc, ffs_spec_fsync },		/* fsync */
    188 	{ &vop_seek_desc, spec_seek },			/* seek */
    189 	{ &vop_remove_desc, spec_remove },		/* remove */
    190 	{ &vop_link_desc, spec_link },			/* link */
    191 	{ &vop_rename_desc, spec_rename },		/* rename */
    192 	{ &vop_mkdir_desc, spec_mkdir },		/* mkdir */
    193 	{ &vop_rmdir_desc, spec_rmdir },		/* rmdir */
    194 	{ &vop_symlink_desc, spec_symlink },		/* symlink */
    195 	{ &vop_readdir_desc, spec_readdir },		/* readdir */
    196 	{ &vop_readlink_desc, spec_readlink },		/* readlink */
    197 	{ &vop_abortop_desc, spec_abortop },		/* abortop */
    198 	{ &vop_inactive_desc, ufs_inactive },		/* inactive */
    199 	{ &vop_reclaim_desc, ffs_reclaim },		/* reclaim */
    200 	{ &vop_lock_desc, ufs_lock },			/* lock */
    201 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
    202 	{ &vop_bmap_desc, spec_bmap },			/* bmap */
    203 	{ &vop_strategy_desc, spec_strategy },		/* strategy */
    204 	{ &vop_print_desc, ufs_print },			/* print */
    205 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
    206 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
    207 	{ &vop_advlock_desc, spec_advlock },		/* advlock */
    208 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    209 	{ &vop_getpages_desc, spec_getpages },		/* getpages */
    210 	{ &vop_putpages_desc, spec_putpages },		/* putpages */
    211 	{ &vop_openextattr_desc, ffs_openextattr },	/* openextattr */
    212 	{ &vop_closeextattr_desc, ffs_closeextattr },	/* closeextattr */
    213 	{ &vop_getextattr_desc, ffs_getextattr },	/* getextattr */
    214 	{ &vop_setextattr_desc, ffs_setextattr },	/* setextattr */
    215 	{ &vop_listextattr_desc, ffs_listextattr },	/* listextattr */
    216 	{ &vop_deleteextattr_desc, ffs_deleteextattr },	/* deleteextattr */
    217 	{ &vop_getacl_desc, ufs_getacl },		/* getacl */
    218 	{ &vop_setacl_desc, ufs_setacl },		/* setacl */
    219 	{ &vop_aclcheck_desc, ufs_aclcheck },		/* aclcheck */
    220 	{ NULL, NULL }
    221 };
    222 const struct vnodeopv_desc ffs_specop_opv_desc =
    223 	{ &ffs_specop_p, ffs_specop_entries };
    224 
    225 int (**ffs_fifoop_p)(void *);
    226 const struct vnodeopv_entry_desc ffs_fifoop_entries[] = {
    227 	{ &vop_default_desc, vn_default_error },
    228 	{ &vop_parsepath_desc, genfs_parsepath },	/* parsepath */
    229 	{ &vop_lookup_desc, vn_fifo_bypass },		/* lookup */
    230 	{ &vop_create_desc, vn_fifo_bypass },		/* create */
    231 	{ &vop_mknod_desc, vn_fifo_bypass },		/* mknod */
    232 	{ &vop_open_desc, vn_fifo_bypass },		/* open */
    233 	{ &vop_close_desc, ufsfifo_close },		/* close */
    234 	{ &vop_access_desc, genfs_access },		/* access */
    235 	{ &vop_accessx_desc, ufs_accessx },		/* accessx */
    236 	{ &vop_getattr_desc, ufs_getattr },		/* getattr */
    237 	{ &vop_setattr_desc, ufs_setattr },		/* setattr */
    238 	{ &vop_read_desc, ufsfifo_read },		/* read */
    239 	{ &vop_write_desc, ufsfifo_write },		/* write */
    240 	{ &vop_fallocate_desc, vn_fifo_bypass },	/* fallocate */
    241 	{ &vop_fdiscard_desc, vn_fifo_bypass },		/* fdiscard */
    242 	{ &vop_ioctl_desc, vn_fifo_bypass },		/* ioctl */
    243 	{ &vop_fcntl_desc, ufs_fcntl },			/* fcntl */
    244 	{ &vop_poll_desc, vn_fifo_bypass },		/* poll */
    245 	{ &vop_kqfilter_desc, vn_fifo_bypass },		/* kqfilter */
    246 	{ &vop_revoke_desc, vn_fifo_bypass },		/* revoke */
    247 	{ &vop_mmap_desc, vn_fifo_bypass },		/* mmap */
    248 	{ &vop_fsync_desc, ffs_fsync },			/* fsync */
    249 	{ &vop_seek_desc, vn_fifo_bypass },		/* seek */
    250 	{ &vop_remove_desc, vn_fifo_bypass },		/* remove */
    251 	{ &vop_link_desc, vn_fifo_bypass },		/* link */
    252 	{ &vop_rename_desc, vn_fifo_bypass },		/* rename */
    253 	{ &vop_mkdir_desc, vn_fifo_bypass },		/* mkdir */
    254 	{ &vop_rmdir_desc, vn_fifo_bypass },		/* rmdir */
    255 	{ &vop_symlink_desc, vn_fifo_bypass },		/* symlink */
    256 	{ &vop_readdir_desc, vn_fifo_bypass },		/* readdir */
    257 	{ &vop_readlink_desc, vn_fifo_bypass },		/* readlink */
    258 	{ &vop_abortop_desc, vn_fifo_bypass },		/* abortop */
    259 	{ &vop_inactive_desc, ufs_inactive },		/* inactive */
    260 	{ &vop_reclaim_desc, ffs_reclaim },		/* reclaim */
    261 	{ &vop_lock_desc, ufs_lock },			/* lock */
    262 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
    263 	{ &vop_bmap_desc, vn_fifo_bypass },		/* bmap */
    264 	{ &vop_strategy_desc, vn_fifo_bypass },		/* strategy */
    265 	{ &vop_print_desc, ufs_print },			/* print */
    266 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
    267 	{ &vop_pathconf_desc, vn_fifo_bypass },		/* pathconf */
    268 	{ &vop_advlock_desc, vn_fifo_bypass },		/* advlock */
    269 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    270 	{ &vop_putpages_desc, vn_fifo_bypass }, 	/* putpages */
    271 	{ &vop_openextattr_desc, ffs_openextattr },	/* openextattr */
    272 	{ &vop_closeextattr_desc, ffs_closeextattr },	/* closeextattr */
    273 	{ &vop_getextattr_desc, ffs_getextattr },	/* getextattr */
    274 	{ &vop_setextattr_desc, ffs_setextattr },	/* setextattr */
    275 	{ &vop_listextattr_desc, ffs_listextattr },	/* listextattr */
    276 	{ &vop_deleteextattr_desc, ffs_deleteextattr },	/* deleteextattr */
    277 	{ &vop_getacl_desc, ufs_getacl },		/* getacl */
    278 	{ &vop_setacl_desc, ufs_setacl },		/* setacl */
    279 	{ &vop_aclcheck_desc, ufs_aclcheck },		/* aclcheck */
    280 	{ NULL, NULL }
    281 };
    282 const struct vnodeopv_desc ffs_fifoop_opv_desc =
    283 	{ &ffs_fifoop_p, ffs_fifoop_entries };
    284 
    285 #include <ufs/ufs/ufs_readwrite.c>
    286 
    287 int
    288 ffs_spec_fsync(void *v)
    289 {
    290 	struct vop_fsync_args /* {
    291 		struct vnode *a_vp;
    292 		kauth_cred_t a_cred;
    293 		int a_flags;
    294 		off_t a_offlo;
    295 		off_t a_offhi;
    296 		struct lwp *a_l;
    297 	} */ *ap = v;
    298 	int error, flags, uflags;
    299 	struct vnode *vp;
    300 
    301 	flags = ap->a_flags;
    302 	uflags = UPDATE_CLOSE | ((flags & FSYNC_WAIT) ? UPDATE_WAIT : 0);
    303 	vp = ap->a_vp;
    304 
    305 	error = spec_fsync(v);
    306 	if (error)
    307 		goto out;
    308 
    309 #ifdef WAPBL
    310 	struct mount *mp = vp->v_mount;
    311 
    312 	if (mp && mp->mnt_wapbl) {
    313 		/*
    314 		 * Don't bother writing out metadata if the syncer is
    315 		 * making the request.  We will let the sync vnode
    316 		 * write it out in a single burst through a call to
    317 		 * VFS_SYNC().
    318 		 */
    319 		if ((flags & (FSYNC_DATAONLY | FSYNC_LAZY)) != 0)
    320 			goto out;
    321 		if ((VTOI(vp)->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE
    322 		    | IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) != 0) {
    323 			error = UFS_WAPBL_BEGIN(mp);
    324 			if (error != 0)
    325 				goto out;
    326 			error = ffs_update(vp, NULL, NULL, uflags);
    327 			UFS_WAPBL_END(mp);
    328 		}
    329 		goto out;
    330 	}
    331 #endif /* WAPBL */
    332 
    333 	error = ffs_update(vp, NULL, NULL, uflags);
    334 
    335 out:
    336 	return error;
    337 }
    338 
    339 int
    340 ffs_fsync(void *v)
    341 {
    342 	struct vop_fsync_args /* {
    343 		struct vnode *a_vp;
    344 		kauth_cred_t a_cred;
    345 		int a_flags;
    346 		off_t a_offlo;
    347 		off_t a_offhi;
    348 		struct lwp *a_l;
    349 	} */ *ap = v;
    350 	struct buf *bp;
    351 	int num, error, i;
    352 	struct indir ia[UFS_NIADDR + 1];
    353 	int bsize;
    354 	daddr_t blk_high;
    355 	struct vnode *vp;
    356 	struct mount *mp;
    357 
    358 	vp = ap->a_vp;
    359 	mp = vp->v_mount;
    360 
    361 	if ((ap->a_offlo == 0 && ap->a_offhi == 0) || (vp->v_type != VREG)) {
    362 		error = ffs_full_fsync(vp, ap->a_flags);
    363 		goto out;
    364 	}
    365 
    366 	bsize = mp->mnt_stat.f_iosize;
    367 	blk_high = ap->a_offhi / bsize;
    368 	if (ap->a_offhi % bsize != 0)
    369 		blk_high++;
    370 
    371 	/*
    372 	 * First, flush all pages in range.
    373 	 */
    374 
    375 	rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
    376 	error = VOP_PUTPAGES(vp, trunc_page(ap->a_offlo),
    377 	    round_page(ap->a_offhi), PGO_CLEANIT |
    378 	    ((ap->a_flags & FSYNC_WAIT) ? PGO_SYNCIO : 0));
    379 	if (error) {
    380 		goto out;
    381 	}
    382 
    383 #ifdef WAPBL
    384 	KASSERT(vp->v_type == VREG);
    385 	if (mp->mnt_wapbl) {
    386 		/*
    387 		 * Don't bother writing out metadata if the syncer is
    388 		 * making the request.  We will let the sync vnode
    389 		 * write it out in a single burst through a call to
    390 		 * VFS_SYNC().
    391 		 */
    392 		if ((ap->a_flags & (FSYNC_DATAONLY | FSYNC_LAZY)) != 0) {
    393 			return 0;
    394 		}
    395 		error = 0;
    396 		if (vp->v_tag == VT_UFS && VTOI(vp)->i_flag &
    397 		    (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY |
    398 				 IN_MODIFIED | IN_ACCESSED)) {
    399 			error = UFS_WAPBL_BEGIN(mp);
    400 			if (error) {
    401 				return error;
    402 			}
    403 			error = ffs_update(vp, NULL, NULL, UPDATE_CLOSE |
    404 			    ((ap->a_flags & FSYNC_WAIT) ? UPDATE_WAIT : 0));
    405 			UFS_WAPBL_END(mp);
    406 		}
    407 		if (error || (ap->a_flags & FSYNC_NOLOG) != 0) {
    408 			return error;
    409 		}
    410 		error = wapbl_flush(mp->mnt_wapbl, 0);
    411 		return error;
    412 	}
    413 #endif /* WAPBL */
    414 
    415 	/*
    416 	 * Then, flush indirect blocks.
    417 	 */
    418 
    419 	if (blk_high >= UFS_NDADDR) {
    420 		error = ufs_getlbns(vp, blk_high, ia, &num);
    421 		if (error)
    422 			goto out;
    423 
    424 		mutex_enter(&bufcache_lock);
    425 		for (i = 0; i < num; i++) {
    426 			if ((bp = incore(vp, ia[i].in_lbn)) == NULL)
    427 				continue;
    428 			if ((bp->b_cflags & BC_BUSY) != 0 ||
    429 			    (bp->b_oflags & BO_DELWRI) == 0)
    430 				continue;
    431 			bp->b_cflags |= BC_BUSY | BC_VFLUSH;
    432 			mutex_exit(&bufcache_lock);
    433 			bawrite(bp);
    434 			mutex_enter(&bufcache_lock);
    435 		}
    436 		mutex_exit(&bufcache_lock);
    437 	}
    438 
    439 	if (ap->a_flags & FSYNC_WAIT) {
    440 		mutex_enter(vp->v_interlock);
    441 		while (vp->v_numoutput > 0)
    442 			cv_wait(&vp->v_cv, vp->v_interlock);
    443 		mutex_exit(vp->v_interlock);
    444 	}
    445 
    446 	error = ffs_update(vp, NULL, NULL, UPDATE_CLOSE |
    447 	    (((ap->a_flags & (FSYNC_WAIT | FSYNC_DATAONLY)) == FSYNC_WAIT)
    448 	    ? UPDATE_WAIT : 0));
    449 
    450 	if (error == 0 && ap->a_flags & FSYNC_CACHE) {
    451 		int l = 0;
    452 		VOP_IOCTL(VTOI(vp)->i_devvp, DIOCCACHESYNC, &l, FWRITE,
    453 			curlwp->l_cred);
    454 	}
    455 
    456 out:
    457 	return error;
    458 }
    459 
    460 /*
    461  * Synch an open file.  Called for VOP_FSYNC().
    462  */
    463 /* ARGSUSED */
    464 int
    465 ffs_full_fsync(struct vnode *vp, int flags)
    466 {
    467 	int error, i, uflags;
    468 
    469 	KASSERT(vp->v_tag == VT_UFS);
    470 	KASSERT(VTOI(vp) != NULL);
    471 	KASSERT(vp->v_type != VCHR && vp->v_type != VBLK);
    472 
    473 	uflags = UPDATE_CLOSE | ((flags & FSYNC_WAIT) ? UPDATE_WAIT : 0);
    474 
    475 #ifdef WAPBL
    476 	struct mount *mp = vp->v_mount;
    477 
    478 	if (mp && mp->mnt_wapbl) {
    479 
    480 		/*
    481 		 * Flush all dirty data associated with the vnode.
    482 		 */
    483 		if (vp->v_type == VREG) {
    484 			int pflags = PGO_ALLPAGES | PGO_CLEANIT;
    485 
    486 			if ((flags & FSYNC_LAZY))
    487 				pflags |= PGO_LAZY;
    488 			if ((flags & FSYNC_WAIT))
    489 				pflags |= PGO_SYNCIO;
    490 			rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
    491 			error = VOP_PUTPAGES(vp, 0, 0, pflags);
    492 			if (error)
    493 				return error;
    494 		}
    495 
    496 		/*
    497 		 * Don't bother writing out metadata if the syncer is
    498 		 * making the request.  We will let the sync vnode
    499 		 * write it out in a single burst through a call to
    500 		 * VFS_SYNC().
    501 		 */
    502 		if ((flags & (FSYNC_DATAONLY | FSYNC_LAZY)) != 0)
    503 			return 0;
    504 
    505 		if ((VTOI(vp)->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE
    506 		    | IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) != 0) {
    507 			error = UFS_WAPBL_BEGIN(mp);
    508 			if (error)
    509 				return error;
    510 			error = ffs_update(vp, NULL, NULL, uflags);
    511 			UFS_WAPBL_END(mp);
    512 		} else {
    513 			error = 0;
    514 		}
    515 		if (error || (flags & FSYNC_NOLOG) != 0)
    516 			return error;
    517 
    518 		/*
    519 		 * Don't flush the log if the vnode being flushed
    520 		 * contains no dirty buffers that could be in the log.
    521 		 */
    522 		if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
    523 			error = wapbl_flush(mp->mnt_wapbl, 0);
    524 			if (error)
    525 				return error;
    526 		}
    527 
    528 		if ((flags & FSYNC_WAIT) != 0) {
    529 			mutex_enter(vp->v_interlock);
    530 			while (vp->v_numoutput != 0)
    531 				cv_wait(&vp->v_cv, vp->v_interlock);
    532 			mutex_exit(vp->v_interlock);
    533 		}
    534 
    535 		return error;
    536 	}
    537 #endif /* WAPBL */
    538 
    539 	error = vflushbuf(vp, flags);
    540 	if (error == 0)
    541 		error = ffs_update(vp, NULL, NULL, uflags);
    542 	if (error == 0 && (flags & FSYNC_CACHE) != 0) {
    543 		i = 1;
    544 		(void)VOP_IOCTL(VTOI(vp)->i_devvp, DIOCCACHESYNC, &i, FWRITE,
    545 		    kauth_cred_get());
    546 	}
    547 
    548 	return error;
    549 }
    550 
    551 /*
    552  * Reclaim an inode so that it can be used for other purposes.
    553  */
    554 int
    555 ffs_reclaim(void *v)
    556 {
    557 	struct vop_reclaim_v2_args /* {
    558 		struct vnode *a_vp;
    559 		struct lwp *a_l;
    560 	} */ *ap = v;
    561 	struct vnode *vp = ap->a_vp;
    562 	struct inode *ip = VTOI(vp);
    563 	struct mount *mp = vp->v_mount;
    564 	struct ufsmount *ump = ip->i_ump;
    565 	void *data;
    566 	int error;
    567 
    568 	VOP_UNLOCK(vp);
    569 
    570 	/*
    571 	 * The inode must be freed and updated before being removed
    572 	 * from its hash chain.  Other threads trying to gain a hold
    573 	 * or lock on the inode will be stalled.
    574 	 */
    575 	error = UFS_WAPBL_BEGIN(mp);
    576 	if (error) {
    577 		return error;
    578 	}
    579 	if (ip->i_nlink <= 0 && ip->i_omode != 0 &&
    580 	    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
    581 		ffs_vfree(vp, ip->i_number, ip->i_omode);
    582 	UFS_WAPBL_END(mp);
    583 	if ((error = ufs_reclaim(vp)) != 0) {
    584 		return (error);
    585 	}
    586 	if (ip->i_din.ffs1_din != NULL) {
    587 		if (ump->um_fstype == UFS1)
    588 			pool_cache_put(ffs_dinode1_cache, ip->i_din.ffs1_din);
    589 		else
    590 			pool_cache_put(ffs_dinode2_cache, ip->i_din.ffs2_din);
    591 	}
    592 	/*
    593 	 * To interlock with ffs_sync().
    594 	 */
    595 	genfs_node_destroy(vp);
    596 	mutex_enter(vp->v_interlock);
    597 	data = vp->v_data;
    598 	vp->v_data = NULL;
    599 	mutex_exit(vp->v_interlock);
    600 
    601 	/*
    602 	 * XXX MFS ends up here, too, to free an inode.  Should we create
    603 	 * XXX a separate pool for MFS inodes?
    604 	 */
    605 	pool_cache_put(ffs_inode_cache, data);
    606 	return (0);
    607 }
    608 
    609 /*
    610  * Return the last logical file offset that should be written for this file
    611  * if we're doing a write that ends at "size".
    612  */
    613 
    614 void
    615 ffs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
    616 {
    617 	struct inode *ip = VTOI(vp);
    618 	struct fs *fs = ip->i_fs;
    619 	daddr_t olbn, nlbn;
    620 
    621 	olbn = ffs_lblkno(fs, ip->i_size);
    622 	nlbn = ffs_lblkno(fs, size);
    623 	if (nlbn < UFS_NDADDR && olbn <= nlbn) {
    624 		*eobp = ffs_fragroundup(fs, size);
    625 	} else {
    626 		*eobp = ffs_blkroundup(fs, size);
    627 	}
    628 }
    629