Home | History | Annotate | Line # | Download | only in lfs
lfs_vnops.c revision 1.183
      1 /*	$NetBSD: lfs_vnops.c,v 1.183 2006/07/13 22:08:00 martin Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 /*
     39  * Copyright (c) 1986, 1989, 1991, 1993, 1995
     40  *	The Regents of the University of California.  All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. Neither the name of the University nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  * SUCH DAMAGE.
     65  *
     66  *	@(#)lfs_vnops.c	8.13 (Berkeley) 6/10/95
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: lfs_vnops.c,v 1.183 2006/07/13 22:08:00 martin Exp $");
     71 
     72 #ifdef _KERNEL_OPT
     73 #include "opt_compat_netbsd.h"
     74 #endif
     75 
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/namei.h>
     79 #include <sys/resourcevar.h>
     80 #include <sys/kernel.h>
     81 #include <sys/file.h>
     82 #include <sys/stat.h>
     83 #include <sys/buf.h>
     84 #include <sys/proc.h>
     85 #include <sys/mount.h>
     86 #include <sys/vnode.h>
     87 #include <sys/pool.h>
     88 #include <sys/signalvar.h>
     89 #include <sys/kauth.h>
     90 #include <sys/syslog.h>
     91 
     92 #include <miscfs/fifofs/fifo.h>
     93 #include <miscfs/genfs/genfs.h>
     94 #include <miscfs/specfs/specdev.h>
     95 
     96 #include <ufs/ufs/inode.h>
     97 #include <ufs/ufs/dir.h>
     98 #include <ufs/ufs/ufsmount.h>
     99 #include <ufs/ufs/ufs_extern.h>
    100 
    101 #include <uvm/uvm.h>
    102 #include <uvm/uvm_pmap.h>
    103 #include <uvm/uvm_stat.h>
    104 #include <uvm/uvm_pager.h>
    105 
    106 #include <ufs/lfs/lfs.h>
    107 #include <ufs/lfs/lfs_extern.h>
    108 
    109 extern pid_t lfs_writer_daemon;
    110 
    111 /* Global vfs data structures for lfs. */
    112 int (**lfs_vnodeop_p)(void *);
    113 const struct vnodeopv_entry_desc lfs_vnodeop_entries[] = {
    114 	{ &vop_default_desc, vn_default_error },
    115 	{ &vop_lookup_desc, ufs_lookup },		/* lookup */
    116 	{ &vop_create_desc, lfs_create },		/* create */
    117 	{ &vop_whiteout_desc, ufs_whiteout },		/* whiteout */
    118 	{ &vop_mknod_desc, lfs_mknod },			/* mknod */
    119 	{ &vop_open_desc, ufs_open },			/* open */
    120 	{ &vop_close_desc, lfs_close },			/* close */
    121 	{ &vop_access_desc, ufs_access },		/* access */
    122 	{ &vop_getattr_desc, lfs_getattr },		/* getattr */
    123 	{ &vop_setattr_desc, lfs_setattr },		/* setattr */
    124 	{ &vop_read_desc, lfs_read },			/* read */
    125 	{ &vop_write_desc, lfs_write },			/* write */
    126 	{ &vop_lease_desc, ufs_lease_check },		/* lease */
    127 	{ &vop_ioctl_desc, ufs_ioctl },			/* ioctl */
    128 	{ &vop_fcntl_desc, lfs_fcntl },			/* fcntl */
    129 	{ &vop_poll_desc, ufs_poll },			/* poll */
    130 	{ &vop_kqfilter_desc, genfs_kqfilter },		/* kqfilter */
    131 	{ &vop_revoke_desc, ufs_revoke },		/* revoke */
    132 	{ &vop_mmap_desc, lfs_mmap },			/* mmap */
    133 	{ &vop_fsync_desc, lfs_fsync },			/* fsync */
    134 	{ &vop_seek_desc, ufs_seek },			/* seek */
    135 	{ &vop_remove_desc, lfs_remove },		/* remove */
    136 	{ &vop_link_desc, lfs_link },			/* link */
    137 	{ &vop_rename_desc, lfs_rename },		/* rename */
    138 	{ &vop_mkdir_desc, lfs_mkdir },			/* mkdir */
    139 	{ &vop_rmdir_desc, lfs_rmdir },			/* rmdir */
    140 	{ &vop_symlink_desc, lfs_symlink },		/* symlink */
    141 	{ &vop_readdir_desc, ufs_readdir },		/* readdir */
    142 	{ &vop_readlink_desc, ufs_readlink },		/* readlink */
    143 	{ &vop_abortop_desc, ufs_abortop },		/* abortop */
    144 	{ &vop_inactive_desc, lfs_inactive },		/* inactive */
    145 	{ &vop_reclaim_desc, lfs_reclaim },		/* reclaim */
    146 	{ &vop_lock_desc, ufs_lock },			/* lock */
    147 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
    148 	{ &vop_bmap_desc, ufs_bmap },			/* bmap */
    149 	{ &vop_strategy_desc, lfs_strategy },		/* strategy */
    150 	{ &vop_print_desc, ufs_print },			/* print */
    151 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
    152 	{ &vop_pathconf_desc, ufs_pathconf },		/* pathconf */
    153 	{ &vop_advlock_desc, ufs_advlock },		/* advlock */
    154 	{ &vop_bwrite_desc, lfs_bwrite },		/* bwrite */
    155 	{ &vop_getpages_desc, lfs_getpages },		/* getpages */
    156 	{ &vop_putpages_desc, lfs_putpages },		/* putpages */
    157 	{ NULL, NULL }
    158 };
    159 const struct vnodeopv_desc lfs_vnodeop_opv_desc =
    160 	{ &lfs_vnodeop_p, lfs_vnodeop_entries };
    161 
    162 int (**lfs_specop_p)(void *);
    163 const struct vnodeopv_entry_desc lfs_specop_entries[] = {
    164 	{ &vop_default_desc, vn_default_error },
    165 	{ &vop_lookup_desc, spec_lookup },		/* lookup */
    166 	{ &vop_create_desc, spec_create },		/* create */
    167 	{ &vop_mknod_desc, spec_mknod },		/* mknod */
    168 	{ &vop_open_desc, spec_open },			/* open */
    169 	{ &vop_close_desc, lfsspec_close },		/* close */
    170 	{ &vop_access_desc, ufs_access },		/* access */
    171 	{ &vop_getattr_desc, lfs_getattr },		/* getattr */
    172 	{ &vop_setattr_desc, lfs_setattr },		/* setattr */
    173 	{ &vop_read_desc, ufsspec_read },		/* read */
    174 	{ &vop_write_desc, ufsspec_write },		/* write */
    175 	{ &vop_lease_desc, spec_lease_check },		/* lease */
    176 	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */
    177 	{ &vop_fcntl_desc, ufs_fcntl },			/* fcntl */
    178 	{ &vop_poll_desc, spec_poll },			/* poll */
    179 	{ &vop_kqfilter_desc, spec_kqfilter },		/* kqfilter */
    180 	{ &vop_revoke_desc, spec_revoke },		/* revoke */
    181 	{ &vop_mmap_desc, spec_mmap },			/* mmap */
    182 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
    183 	{ &vop_seek_desc, spec_seek },			/* seek */
    184 	{ &vop_remove_desc, spec_remove },		/* remove */
    185 	{ &vop_link_desc, spec_link },			/* link */
    186 	{ &vop_rename_desc, spec_rename },		/* rename */
    187 	{ &vop_mkdir_desc, spec_mkdir },		/* mkdir */
    188 	{ &vop_rmdir_desc, spec_rmdir },		/* rmdir */
    189 	{ &vop_symlink_desc, spec_symlink },		/* symlink */
    190 	{ &vop_readdir_desc, spec_readdir },		/* readdir */
    191 	{ &vop_readlink_desc, spec_readlink },		/* readlink */
    192 	{ &vop_abortop_desc, spec_abortop },		/* abortop */
    193 	{ &vop_inactive_desc, lfs_inactive },		/* inactive */
    194 	{ &vop_reclaim_desc, lfs_reclaim },		/* reclaim */
    195 	{ &vop_lock_desc, ufs_lock },			/* lock */
    196 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
    197 	{ &vop_bmap_desc, spec_bmap },			/* bmap */
    198 	{ &vop_strategy_desc, spec_strategy },		/* strategy */
    199 	{ &vop_print_desc, ufs_print },			/* print */
    200 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
    201 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
    202 	{ &vop_advlock_desc, spec_advlock },		/* advlock */
    203 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    204 	{ &vop_getpages_desc, spec_getpages },		/* getpages */
    205 	{ &vop_putpages_desc, spec_putpages },		/* putpages */
    206 	{ NULL, NULL }
    207 };
    208 const struct vnodeopv_desc lfs_specop_opv_desc =
    209 	{ &lfs_specop_p, lfs_specop_entries };
    210 
    211 int (**lfs_fifoop_p)(void *);
    212 const struct vnodeopv_entry_desc lfs_fifoop_entries[] = {
    213 	{ &vop_default_desc, vn_default_error },
    214 	{ &vop_lookup_desc, fifo_lookup },		/* lookup */
    215 	{ &vop_create_desc, fifo_create },		/* create */
    216 	{ &vop_mknod_desc, fifo_mknod },		/* mknod */
    217 	{ &vop_open_desc, fifo_open },			/* open */
    218 	{ &vop_close_desc, lfsfifo_close },		/* close */
    219 	{ &vop_access_desc, ufs_access },		/* access */
    220 	{ &vop_getattr_desc, lfs_getattr },		/* getattr */
    221 	{ &vop_setattr_desc, lfs_setattr },		/* setattr */
    222 	{ &vop_read_desc, ufsfifo_read },		/* read */
    223 	{ &vop_write_desc, ufsfifo_write },		/* write */
    224 	{ &vop_lease_desc, fifo_lease_check },		/* lease */
    225 	{ &vop_ioctl_desc, fifo_ioctl },		/* ioctl */
    226 	{ &vop_fcntl_desc, ufs_fcntl },			/* fcntl */
    227 	{ &vop_poll_desc, fifo_poll },			/* poll */
    228 	{ &vop_kqfilter_desc, fifo_kqfilter },		/* kqfilter */
    229 	{ &vop_revoke_desc, fifo_revoke },		/* revoke */
    230 	{ &vop_mmap_desc, fifo_mmap },			/* mmap */
    231 	{ &vop_fsync_desc, fifo_fsync },		/* fsync */
    232 	{ &vop_seek_desc, fifo_seek },			/* seek */
    233 	{ &vop_remove_desc, fifo_remove },		/* remove */
    234 	{ &vop_link_desc, fifo_link },			/* link */
    235 	{ &vop_rename_desc, fifo_rename },		/* rename */
    236 	{ &vop_mkdir_desc, fifo_mkdir },		/* mkdir */
    237 	{ &vop_rmdir_desc, fifo_rmdir },		/* rmdir */
    238 	{ &vop_symlink_desc, fifo_symlink },		/* symlink */
    239 	{ &vop_readdir_desc, fifo_readdir },		/* readdir */
    240 	{ &vop_readlink_desc, fifo_readlink },		/* readlink */
    241 	{ &vop_abortop_desc, fifo_abortop },		/* abortop */
    242 	{ &vop_inactive_desc, lfs_inactive },		/* inactive */
    243 	{ &vop_reclaim_desc, lfs_reclaim },		/* reclaim */
    244 	{ &vop_lock_desc, ufs_lock },			/* lock */
    245 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
    246 	{ &vop_bmap_desc, fifo_bmap },			/* bmap */
    247 	{ &vop_strategy_desc, fifo_strategy },		/* strategy */
    248 	{ &vop_print_desc, ufs_print },			/* print */
    249 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
    250 	{ &vop_pathconf_desc, fifo_pathconf },		/* pathconf */
    251 	{ &vop_advlock_desc, fifo_advlock },		/* advlock */
    252 	{ &vop_bwrite_desc, lfs_bwrite },		/* bwrite */
    253 	{ &vop_putpages_desc, fifo_putpages },		/* putpages */
    254 	{ NULL, NULL }
    255 };
    256 const struct vnodeopv_desc lfs_fifoop_opv_desc =
    257 	{ &lfs_fifoop_p, lfs_fifoop_entries };
    258 
    259 static int check_dirty(struct lfs *, struct vnode *, off_t, off_t, off_t, int, int);
    260 
    261 #define	LFS_READWRITE
    262 #include <ufs/ufs/ufs_readwrite.c>
    263 #undef	LFS_READWRITE
    264 
    265 /*
    266  * Synch an open file.
    267  */
    268 /* ARGSUSED */
    269 int
    270 lfs_fsync(void *v)
    271 {
    272 	struct vop_fsync_args /* {
    273 		struct vnode *a_vp;
    274 		kauth_cred_t a_cred;
    275 		int a_flags;
    276 		off_t offlo;
    277 		off_t offhi;
    278 		struct lwp *a_l;
    279 	} */ *ap = v;
    280 	struct vnode *vp = ap->a_vp;
    281 	int error, wait;
    282 
    283 	/* If we're mounted read-only, don't try to sync. */
    284 	if (VTOI(vp)->i_lfs->lfs_ronly)
    285 		return 0;
    286 
    287 	/*
    288 	 * Trickle sync checks for need to do a checkpoint after possible
    289 	 * activity from the pagedaemon.
    290 	 */
    291 	if (ap->a_flags & FSYNC_LAZY) {
    292 		simple_lock(&lfs_subsys_lock);
    293 		wakeup(&lfs_writer_daemon);
    294 		simple_unlock(&lfs_subsys_lock);
    295 		return 0;
    296 	}
    297 
    298 	/*
    299 	 * Don't reclaim any vnodes that are being cleaned.
    300 	 * This prevents the cleaner from writing files twice
    301 	 * in the same partial segment, causing an accounting
    302 	 * underflow.
    303 	 */
    304 	if (ap->a_flags & FSYNC_RECLAIM) {
    305 		if (VTOI(vp)->i_flags & IN_CLEANING)
    306 			return EAGAIN;
    307 	}
    308 
    309 	wait = (ap->a_flags & FSYNC_WAIT);
    310 	simple_lock(&vp->v_interlock);
    311 	error = VOP_PUTPAGES(vp, trunc_page(ap->a_offlo),
    312 			round_page(ap->a_offhi),
    313 			PGO_CLEANIT | (wait ? PGO_SYNCIO : 0));
    314 	if (error)
    315 		return error;
    316 	error = lfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
    317 	if (error == 0 && ap->a_flags & FSYNC_CACHE) {
    318 		int l = 0;
    319 		error = VOP_IOCTL(VTOI(vp)->i_devvp, DIOCCACHESYNC, &l, FWRITE,
    320 				  ap->a_l->l_proc->p_cred, ap->a_l);
    321 	}
    322 	if (wait && !VPISEMPTY(vp))
    323 		LFS_SET_UINO(VTOI(vp), IN_MODIFIED);
    324 
    325 	return error;
    326 }
    327 
    328 /*
    329  * Take IN_ADIROP off, then call ufs_inactive.
    330  */
    331 int
    332 lfs_inactive(void *v)
    333 {
    334 	struct vop_inactive_args /* {
    335 		struct vnode *a_vp;
    336 		struct lwp *a_l;
    337 	} */ *ap = v;
    338 
    339 	KASSERT(VTOI(ap->a_vp)->i_nlink == VTOI(ap->a_vp)->i_ffs_effnlink);
    340 
    341 	lfs_unmark_vnode(ap->a_vp);
    342 
    343 	/*
    344 	 * The Ifile is only ever inactivated on unmount.
    345 	 * Streamline this process by not giving it more dirty blocks.
    346 	 */
    347 	if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM) {
    348 		LFS_CLR_UINO(VTOI(ap->a_vp), IN_ALLMOD);
    349 		VOP_UNLOCK(ap->a_vp, 0);
    350 		return 0;
    351 	}
    352 
    353 	return ufs_inactive(v);
    354 }
    355 
    356 /*
    357  * These macros are used to bracket UFS directory ops, so that we can
    358  * identify all the pages touched during directory ops which need to
    359  * be ordered and flushed atomically, so that they may be recovered.
    360  *
    361  * Because we have to mark nodes VDIROP in order to prevent
    362  * the cache from reclaiming them while a dirop is in progress, we must
    363  * also manage the number of nodes so marked (otherwise we can run out).
    364  * We do this by setting lfs_dirvcount to the number of marked vnodes; it
    365  * is decremented during segment write, when VDIROP is taken off.
    366  */
    367 #define	MARK_VNODE(vp)			lfs_mark_vnode(vp)
    368 #define	UNMARK_VNODE(vp)		lfs_unmark_vnode(vp)
    369 #define	SET_DIROP_CREATE(dvp, vpp)	lfs_set_dirop_create((dvp), (vpp))
    370 #define	SET_DIROP_REMOVE(dvp, vp)	lfs_set_dirop((dvp), (vp))
    371 static int lfs_set_dirop_create(struct vnode *, struct vnode **);
    372 static int lfs_set_dirop(struct vnode *, struct vnode *);
    373 
    374 static int
    375 lfs_set_dirop(struct vnode *dvp, struct vnode *vp)
    376 {
    377 	struct lfs *fs;
    378 	int error;
    379 
    380 	KASSERT(VOP_ISLOCKED(dvp));
    381 	KASSERT(vp == NULL || VOP_ISLOCKED(vp));
    382 
    383 	fs = VTOI(dvp)->i_lfs;
    384 
    385 	ASSERT_NO_SEGLOCK(fs);
    386 	/*
    387 	 * LFS_NRESERVE calculates direct and indirect blocks as well
    388 	 * as an inode block; an overestimate in most cases.
    389 	 */
    390 	if ((error = lfs_reserve(fs, dvp, vp, LFS_NRESERVE(fs))) != 0)
    391 		return (error);
    392 
    393     restart:
    394 	simple_lock(&fs->lfs_interlock);
    395 	if (fs->lfs_dirops == 0) {
    396 		simple_unlock(&fs->lfs_interlock);
    397 		lfs_check(dvp, LFS_UNUSED_LBN, 0);
    398 		simple_lock(&fs->lfs_interlock);
    399 	}
    400 	while (fs->lfs_writer)
    401 		ltsleep(&fs->lfs_dirops, (PRIBIO + 1), "lfs_sdirop", 0,
    402 			&fs->lfs_interlock);
    403 	simple_lock(&lfs_subsys_lock);
    404 	if (lfs_dirvcount > LFS_MAX_DIROP && fs->lfs_dirops == 0) {
    405 		wakeup(&lfs_writer_daemon);
    406 		simple_unlock(&lfs_subsys_lock);
    407 		simple_unlock(&fs->lfs_interlock);
    408 		preempt(1);
    409 		goto restart;
    410 	}
    411 
    412 	if (lfs_dirvcount > LFS_MAX_DIROP) {
    413 		simple_unlock(&fs->lfs_interlock);
    414 		DLOG((DLOG_DIROP, "lfs_set_dirop: sleeping with dirops=%d, "
    415 		      "dirvcount=%d\n", fs->lfs_dirops, lfs_dirvcount));
    416 		if ((error = ltsleep(&lfs_dirvcount,
    417 		    PCATCH | PUSER | PNORELOCK, "lfs_maxdirop", 0,
    418 		    &lfs_subsys_lock)) != 0) {
    419 			goto unreserve;
    420 		}
    421 		goto restart;
    422 	}
    423 	simple_unlock(&lfs_subsys_lock);
    424 
    425 	++fs->lfs_dirops;
    426 	fs->lfs_doifile = 1;
    427 	simple_unlock(&fs->lfs_interlock);
    428 
    429 	/* Hold a reference so SET_ENDOP will be happy */
    430 	vref(dvp);
    431 	if (vp) {
    432 		vref(vp);
    433 		MARK_VNODE(vp);
    434 	}
    435 
    436 	MARK_VNODE(dvp);
    437 	return 0;
    438 
    439 unreserve:
    440 	lfs_reserve(fs, dvp, vp, -LFS_NRESERVE(fs));
    441 	return error;
    442 }
    443 
    444 /*
    445  * Get a new vnode *before* adjusting the dirop count, to avoid a deadlock
    446  * in getnewvnode(), if we have a stacked filesystem mounted on top
    447  * of us.
    448  *
    449  * NB: this means we have to clear the new vnodes on error.  Fortunately
    450  * SET_ENDOP is there to do that for us.
    451  */
    452 static int
    453 lfs_set_dirop_create(struct vnode *dvp, struct vnode **vpp)
    454 {
    455 	int error;
    456 	struct lfs *fs;
    457 
    458 	fs = VFSTOUFS(dvp->v_mount)->um_lfs;
    459 	ASSERT_NO_SEGLOCK(fs);
    460 	if (fs->lfs_ronly)
    461 		return EROFS;
    462 	if (vpp && (error = getnewvnode(VT_LFS, dvp->v_mount, lfs_vnodeop_p, vpp))) {
    463 		DLOG((DLOG_ALLOC, "lfs_set_dirop_create: dvp %p error %d\n",
    464 		      dvp, error));
    465 		return error;
    466 	}
    467 	if ((error = lfs_set_dirop(dvp, NULL)) != 0) {
    468 		if (vpp) {
    469 			ungetnewvnode(*vpp);
    470 			*vpp = NULL;
    471 		}
    472 		return error;
    473 	}
    474 	return 0;
    475 }
    476 
    477 #define	SET_ENDOP_BASE(fs, dvp, str)					\
    478 	do {								\
    479 		simple_lock(&(fs)->lfs_interlock);			\
    480 		--(fs)->lfs_dirops;					\
    481 		if (!(fs)->lfs_dirops) {				\
    482 			if ((fs)->lfs_nadirop) {			\
    483 				panic("SET_ENDOP: %s: no dirops but "	\
    484 					" nadirop=%d", (str),		\
    485 					(fs)->lfs_nadirop);		\
    486 			}						\
    487 			wakeup(&(fs)->lfs_writer);			\
    488 			simple_unlock(&(fs)->lfs_interlock);		\
    489 			lfs_check((dvp), LFS_UNUSED_LBN, 0);		\
    490 		} else							\
    491 			simple_unlock(&(fs)->lfs_interlock);		\
    492 	} while(0)
    493 #define SET_ENDOP_CREATE(fs, dvp, nvpp, str)				\
    494 	do {								\
    495 		UNMARK_VNODE(dvp);					\
    496 		if (nvpp && *nvpp)					\
    497 			UNMARK_VNODE(*nvpp);				\
    498 		/* Check for error return to stem vnode leakage */	\
    499 		if (nvpp && *nvpp && !((*nvpp)->v_flag & VDIROP))	\
    500 			ungetnewvnode(*(nvpp));				\
    501 		SET_ENDOP_BASE((fs), (dvp), (str));			\
    502 		lfs_reserve((fs), (dvp), NULL, -LFS_NRESERVE(fs));	\
    503 		vrele(dvp);						\
    504 	} while(0)
    505 #define SET_ENDOP_CREATE_AP(ap, str)					\
    506 	SET_ENDOP_CREATE(VTOI((ap)->a_dvp)->i_lfs, (ap)->a_dvp,		\
    507 			 (ap)->a_vpp, (str))
    508 #define SET_ENDOP_REMOVE(fs, dvp, ovp, str)				\
    509 	do {								\
    510 		UNMARK_VNODE(dvp);					\
    511 		if (ovp)						\
    512 			UNMARK_VNODE(ovp);				\
    513 		SET_ENDOP_BASE((fs), (dvp), (str));			\
    514 		lfs_reserve((fs), (dvp), (ovp), -LFS_NRESERVE(fs));	\
    515 		vrele(dvp);						\
    516 		if (ovp)						\
    517 			vrele(ovp);					\
    518 	} while(0)
    519 
    520 void
    521 lfs_mark_vnode(struct vnode *vp)
    522 {
    523 	struct inode *ip = VTOI(vp);
    524 	struct lfs *fs = ip->i_lfs;
    525 
    526 	simple_lock(&fs->lfs_interlock);
    527 	if (!(ip->i_flag & IN_ADIROP)) {
    528 		if (!(vp->v_flag & VDIROP)) {
    529 			(void)lfs_vref(vp);
    530 			simple_lock(&lfs_subsys_lock);
    531 			++lfs_dirvcount;
    532 			++fs->lfs_dirvcount;
    533 			simple_unlock(&lfs_subsys_lock);
    534 			TAILQ_INSERT_TAIL(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    535 			vp->v_flag |= VDIROP;
    536 		}
    537 		++fs->lfs_nadirop;
    538 		ip->i_flag |= IN_ADIROP;
    539 	} else
    540 		KASSERT(vp->v_flag & VDIROP);
    541 	simple_unlock(&fs->lfs_interlock);
    542 }
    543 
    544 void
    545 lfs_unmark_vnode(struct vnode *vp)
    546 {
    547 	struct inode *ip = VTOI(vp);
    548 
    549 	if (ip && (ip->i_flag & IN_ADIROP)) {
    550 		KASSERT(vp->v_flag & VDIROP);
    551 		simple_lock(&ip->i_lfs->lfs_interlock);
    552 		--ip->i_lfs->lfs_nadirop;
    553 		simple_unlock(&ip->i_lfs->lfs_interlock);
    554 		ip->i_flag &= ~IN_ADIROP;
    555 	}
    556 }
    557 
    558 int
    559 lfs_symlink(void *v)
    560 {
    561 	struct vop_symlink_args /* {
    562 		struct vnode *a_dvp;
    563 		struct vnode **a_vpp;
    564 		struct componentname *a_cnp;
    565 		struct vattr *a_vap;
    566 		char *a_target;
    567 	} */ *ap = v;
    568 	int error;
    569 
    570 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    571 		vput(ap->a_dvp);
    572 		return error;
    573 	}
    574 	error = ufs_symlink(ap);
    575 	SET_ENDOP_CREATE_AP(ap, "symlink");
    576 	return (error);
    577 }
    578 
    579 int
    580 lfs_mknod(void *v)
    581 {
    582 	struct vop_mknod_args	/* {
    583 		struct vnode *a_dvp;
    584 		struct vnode **a_vpp;
    585 		struct componentname *a_cnp;
    586 		struct vattr *a_vap;
    587 		} */ *ap = v;
    588 	struct vattr *vap = ap->a_vap;
    589 	struct vnode **vpp = ap->a_vpp;
    590 	struct inode *ip;
    591 	int error;
    592 	struct mount	*mp;
    593 	ino_t		ino;
    594 
    595 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    596 		vput(ap->a_dvp);
    597 		return error;
    598 	}
    599 	error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
    600 	    ap->a_dvp, vpp, ap->a_cnp);
    601 
    602 	/* Either way we're done with the dirop at this point */
    603 	SET_ENDOP_CREATE_AP(ap, "mknod");
    604 
    605 	if (error)
    606 		return (error);
    607 
    608 	ip = VTOI(*vpp);
    609 	mp  = (*vpp)->v_mount;
    610 	ino = ip->i_number;
    611 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
    612 	if (vap->va_rdev != VNOVAL) {
    613 		/*
    614 		 * Want to be able to use this to make badblock
    615 		 * inodes, so don't truncate the dev number.
    616 		 */
    617 #if 0
    618 		ip->i_ffs1_rdev = ufs_rw32(vap->va_rdev,
    619 		    UFS_MPNEEDSWAP((*vpp)->v_mount));
    620 #else
    621 		ip->i_ffs1_rdev = vap->va_rdev;
    622 #endif
    623 	}
    624 
    625 	/*
    626 	 * Call fsync to write the vnode so that we don't have to deal with
    627 	 * flushing it when it's marked VDIROP|VXLOCK.
    628 	 *
    629 	 * XXX KS - If we can't flush we also can't call vgone(), so must
    630 	 * return.  But, that leaves this vnode in limbo, also not good.
    631 	 * Can this ever happen (barring hardware failure)?
    632 	 */
    633 	if ((error = VOP_FSYNC(*vpp, NOCRED, FSYNC_WAIT, 0, 0,
    634 	    curlwp)) != 0) {
    635 		panic("lfs_mknod: couldn't fsync (ino %llu)",
    636 		    (unsigned long long)ino);
    637 		/* return (error); */
    638 	}
    639 	/*
    640 	 * Remove vnode so that it will be reloaded by VFS_VGET and
    641 	 * checked to see if it is an alias of an existing entry in
    642 	 * the inode cache.
    643 	 */
    644 	/* Used to be vput, but that causes us to call VOP_INACTIVE twice. */
    645 
    646 	VOP_UNLOCK(*vpp, 0);
    647 	lfs_vunref(*vpp);
    648 	(*vpp)->v_type = VNON;
    649 	vgone(*vpp);
    650 	error = VFS_VGET(mp, ino, vpp);
    651 
    652 	if (error != 0) {
    653 		*vpp = NULL;
    654 		return (error);
    655 	}
    656 	return (0);
    657 }
    658 
    659 int
    660 lfs_create(void *v)
    661 {
    662 	struct vop_create_args	/* {
    663 		struct vnode *a_dvp;
    664 		struct vnode **a_vpp;
    665 		struct componentname *a_cnp;
    666 		struct vattr *a_vap;
    667 	} */ *ap = v;
    668 	int error;
    669 
    670 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    671 		vput(ap->a_dvp);
    672 		return error;
    673 	}
    674 	error = ufs_create(ap);
    675 	SET_ENDOP_CREATE_AP(ap, "create");
    676 	return (error);
    677 }
    678 
    679 int
    680 lfs_mkdir(void *v)
    681 {
    682 	struct vop_mkdir_args	/* {
    683 		struct vnode *a_dvp;
    684 		struct vnode **a_vpp;
    685 		struct componentname *a_cnp;
    686 		struct vattr *a_vap;
    687 	} */ *ap = v;
    688 	int error;
    689 
    690 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    691 		vput(ap->a_dvp);
    692 		return error;
    693 	}
    694 	error = ufs_mkdir(ap);
    695 	SET_ENDOP_CREATE_AP(ap, "mkdir");
    696 	return (error);
    697 }
    698 
    699 int
    700 lfs_remove(void *v)
    701 {
    702 	struct vop_remove_args	/* {
    703 		struct vnode *a_dvp;
    704 		struct vnode *a_vp;
    705 		struct componentname *a_cnp;
    706 	} */ *ap = v;
    707 	struct vnode *dvp, *vp;
    708 	int error;
    709 
    710 	dvp = ap->a_dvp;
    711 	vp = ap->a_vp;
    712 	if ((error = SET_DIROP_REMOVE(dvp, vp)) != 0) {
    713 		if (dvp == vp)
    714 			vrele(vp);
    715 		else
    716 			vput(vp);
    717 		vput(dvp);
    718 		return error;
    719 	}
    720 	error = ufs_remove(ap);
    721 	SET_ENDOP_REMOVE(VTOI(dvp)->i_lfs, dvp, ap->a_vp, "remove");
    722 	return (error);
    723 }
    724 
    725 int
    726 lfs_rmdir(void *v)
    727 {
    728 	struct vop_rmdir_args	/* {
    729 		struct vnodeop_desc *a_desc;
    730 		struct vnode *a_dvp;
    731 		struct vnode *a_vp;
    732 		struct componentname *a_cnp;
    733 	} */ *ap = v;
    734 	struct vnode *vp;
    735 	int error;
    736 
    737 	vp = ap->a_vp;
    738 	if ((error = SET_DIROP_REMOVE(ap->a_dvp, ap->a_vp)) != 0) {
    739 		vrele(ap->a_dvp);
    740 		if (ap->a_vp != ap->a_dvp)
    741 			VOP_UNLOCK(ap->a_dvp, 0);
    742 		vput(vp);
    743 		return error;
    744 	}
    745 	error = ufs_rmdir(ap);
    746 	SET_ENDOP_REMOVE(VTOI(ap->a_dvp)->i_lfs, ap->a_dvp, ap->a_vp, "rmdir");
    747 	return (error);
    748 }
    749 
    750 int
    751 lfs_link(void *v)
    752 {
    753 	struct vop_link_args	/* {
    754 		struct vnode *a_dvp;
    755 		struct vnode *a_vp;
    756 		struct componentname *a_cnp;
    757 	} */ *ap = v;
    758 	int error;
    759 	struct vnode **vpp = NULL;
    760 
    761 	if ((error = SET_DIROP_CREATE(ap->a_dvp, vpp)) != 0) {
    762 		vput(ap->a_dvp);
    763 		return error;
    764 	}
    765 	error = ufs_link(ap);
    766 	SET_ENDOP_CREATE(VTOI(ap->a_dvp)->i_lfs, ap->a_dvp, vpp, "link");
    767 	return (error);
    768 }
    769 
    770 int
    771 lfs_rename(void *v)
    772 {
    773 	struct vop_rename_args	/* {
    774 		struct vnode *a_fdvp;
    775 		struct vnode *a_fvp;
    776 		struct componentname *a_fcnp;
    777 		struct vnode *a_tdvp;
    778 		struct vnode *a_tvp;
    779 		struct componentname *a_tcnp;
    780 	} */ *ap = v;
    781 	struct vnode *tvp, *fvp, *tdvp, *fdvp;
    782 	struct componentname *tcnp, *fcnp;
    783 	int error;
    784 	struct lfs *fs;
    785 
    786 	fs = VTOI(ap->a_fdvp)->i_lfs;
    787 	tvp = ap->a_tvp;
    788 	tdvp = ap->a_tdvp;
    789 	tcnp = ap->a_tcnp;
    790 	fvp = ap->a_fvp;
    791 	fdvp = ap->a_fdvp;
    792 	fcnp = ap->a_fcnp;
    793 
    794 	/*
    795 	 * Check for cross-device rename.
    796 	 * If it is, we don't want to set dirops, just error out.
    797 	 * (In particular note that MARK_VNODE(tdvp) will DTWT on
    798 	 * a cross-device rename.)
    799 	 *
    800 	 * Copied from ufs_rename.
    801 	 */
    802 	if ((fvp->v_mount != tdvp->v_mount) ||
    803 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
    804 		error = EXDEV;
    805 		goto errout;
    806 	}
    807 
    808 	/*
    809 	 * Check to make sure we're not renaming a vnode onto itself
    810 	 * (deleting a hard link by renaming one name onto another);
    811 	 * if we are we can't recursively call VOP_REMOVE since that
    812 	 * would leave us with an unaccounted-for number of live dirops.
    813 	 *
    814 	 * Inline the relevant section of ufs_rename here, *before*
    815 	 * calling SET_DIROP_REMOVE.
    816 	 */
    817 	if (tvp && ((VTOI(tvp)->i_flags & (IMMUTABLE | APPEND)) ||
    818 	    (VTOI(tdvp)->i_flags & APPEND))) {
    819 		error = EPERM;
    820 		goto errout;
    821 	}
    822 	if (fvp == tvp) {
    823 		if (fvp->v_type == VDIR) {
    824 			error = EINVAL;
    825 			goto errout;
    826 		}
    827 
    828 		/* Release destination completely. */
    829 		VOP_ABORTOP(tdvp, tcnp);
    830 		vput(tdvp);
    831 		vput(tvp);
    832 
    833 		/* Delete source. */
    834 		vrele(fvp);
    835 		fcnp->cn_flags &= ~(MODMASK | SAVESTART);
    836 		fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
    837 		fcnp->cn_nameiop = DELETE;
    838 		if ((error = relookup(fdvp, &fvp, fcnp))){
    839 			/* relookup blew away fdvp */
    840 			return (error);
    841 		}
    842 		return (VOP_REMOVE(fdvp, fvp, fcnp));
    843 	}
    844 
    845 	if ((error = SET_DIROP_REMOVE(tdvp, tvp)) != 0)
    846 		goto errout;
    847 	MARK_VNODE(fdvp);
    848 	MARK_VNODE(fvp);
    849 
    850 	error = ufs_rename(ap);
    851 	UNMARK_VNODE(fdvp);
    852 	UNMARK_VNODE(fvp);
    853 	SET_ENDOP_REMOVE(fs, tdvp, tvp, "rename");
    854 	return (error);
    855 
    856     errout:
    857 	VOP_ABORTOP(tdvp, ap->a_tcnp); /* XXX, why not in NFS? */
    858 	if (tdvp == tvp)
    859 		vrele(tdvp);
    860 	else
    861 		vput(tdvp);
    862 	if (tvp)
    863 		vput(tvp);
    864 	VOP_ABORTOP(fdvp, ap->a_fcnp); /* XXX, why not in NFS? */
    865 	vrele(fdvp);
    866 	vrele(fvp);
    867 	return (error);
    868 }
    869 
    870 /* XXX hack to avoid calling ITIMES in getattr */
    871 int
    872 lfs_getattr(void *v)
    873 {
    874 	struct vop_getattr_args /* {
    875 		struct vnode *a_vp;
    876 		struct vattr *a_vap;
    877 		kauth_cred_t a_cred;
    878 		struct lwp *a_l;
    879 	} */ *ap = v;
    880 	struct vnode *vp = ap->a_vp;
    881 	struct inode *ip = VTOI(vp);
    882 	struct vattr *vap = ap->a_vap;
    883 	struct lfs *fs = ip->i_lfs;
    884 	/*
    885 	 * Copy from inode table
    886 	 */
    887 	vap->va_fsid = ip->i_dev;
    888 	vap->va_fileid = ip->i_number;
    889 	vap->va_mode = ip->i_mode & ~IFMT;
    890 	vap->va_nlink = ip->i_nlink;
    891 	vap->va_uid = ip->i_uid;
    892 	vap->va_gid = ip->i_gid;
    893 	vap->va_rdev = (dev_t)ip->i_ffs1_rdev;
    894 	vap->va_size = vp->v_size;
    895 	vap->va_atime.tv_sec = ip->i_ffs1_atime;
    896 	vap->va_atime.tv_nsec = ip->i_ffs1_atimensec;
    897 	vap->va_mtime.tv_sec = ip->i_ffs1_mtime;
    898 	vap->va_mtime.tv_nsec = ip->i_ffs1_mtimensec;
    899 	vap->va_ctime.tv_sec = ip->i_ffs1_ctime;
    900 	vap->va_ctime.tv_nsec = ip->i_ffs1_ctimensec;
    901 	vap->va_flags = ip->i_flags;
    902 	vap->va_gen = ip->i_gen;
    903 	/* this doesn't belong here */
    904 	if (vp->v_type == VBLK)
    905 		vap->va_blocksize = BLKDEV_IOSIZE;
    906 	else if (vp->v_type == VCHR)
    907 		vap->va_blocksize = MAXBSIZE;
    908 	else
    909 		vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
    910 	vap->va_bytes = fsbtob(fs, (u_quad_t)ip->i_lfs_effnblks);
    911 	vap->va_type = vp->v_type;
    912 	vap->va_filerev = ip->i_modrev;
    913 	return (0);
    914 }
    915 
    916 /*
    917  * Check to make sure the inode blocks won't choke the buffer
    918  * cache, then call ufs_setattr as usual.
    919  */
    920 int
    921 lfs_setattr(void *v)
    922 {
    923 	struct vop_setattr_args /* {
    924 		struct vnode *a_vp;
    925 		struct vattr *a_vap;
    926 		kauth_cred_t a_cred;
    927 		struct lwp *a_l;
    928 	} */ *ap = v;
    929 	struct vnode *vp = ap->a_vp;
    930 
    931 	lfs_check(vp, LFS_UNUSED_LBN, 0);
    932 	return ufs_setattr(v);
    933 }
    934 
    935 /*
    936  * Release the block we hold on lfs_newseg wrapping.  Called on file close,
    937  * or explicitly from LFCNWRAPGO.
    938  */
    939 static int
    940 lfs_wrapgo(struct lfs *fs, struct inode *ip, int waitfor)
    941 {
    942 	if ((ip->i_lfs_iflags & LFSI_WRAPBLOCK) == 0)
    943 		return EBUSY;
    944 
    945 	ip->i_lfs_iflags &= ~LFSI_WRAPBLOCK;
    946 
    947 	KASSERT(fs->lfs_nowrap > 0);
    948 	if (fs->lfs_nowrap <= 0) {
    949 		simple_unlock(&fs->lfs_interlock);
    950 		return 0;
    951 	}
    952 
    953 	if (--fs->lfs_nowrap == 0) {
    954 		log(LOG_NOTICE, "%s: re-enabled log wrap\n", fs->lfs_fsmnt);
    955 		wakeup(&fs->lfs_nowrap);
    956 		lfs_wakeup_cleaner(fs);
    957 	}
    958 	if (waitfor) {
    959 		ltsleep(&fs->lfs_nextseg, PCATCH | PUSER,
    960 			"segment", 0, &fs->lfs_interlock);
    961 	}
    962 
    963 	return 0;
    964 }
    965 
    966 /*
    967  * Close called
    968  */
    969 /* ARGSUSED */
    970 int
    971 lfs_close(void *v)
    972 {
    973 	struct vop_close_args /* {
    974 		struct vnode *a_vp;
    975 		int  a_fflag;
    976 		kauth_cred_t a_cred;
    977 		struct lwp *a_l;
    978 	} */ *ap = v;
    979 	struct vnode *vp = ap->a_vp;
    980 	struct inode *ip = VTOI(vp);
    981 	struct lfs *fs = ip->i_lfs;
    982 
    983 	if (ip->i_lfs_iflags & LFSI_WRAPBLOCK) {
    984 		simple_lock(&fs->lfs_interlock);
    985 		lfs_wrapgo(fs, ip, 0);
    986 		simple_unlock(&fs->lfs_interlock);
    987 	}
    988 
    989 	if (vp == ip->i_lfs->lfs_ivnode &&
    990 	    vp->v_mount->mnt_iflag & IMNT_UNMOUNT)
    991 		return 0;
    992 
    993 	if (vp->v_usecount > 1 && vp != ip->i_lfs->lfs_ivnode) {
    994 		LFS_ITIMES(ip, NULL, NULL, NULL);
    995 	}
    996 	return (0);
    997 }
    998 
    999 /*
   1000  * Close wrapper for special devices.
   1001  *
   1002  * Update the times on the inode then do device close.
   1003  */
   1004 int
   1005 lfsspec_close(void *v)
   1006 {
   1007 	struct vop_close_args /* {
   1008 		struct vnode	*a_vp;
   1009 		int		a_fflag;
   1010 		kauth_cred_t	a_cred;
   1011 		struct lwp	*a_l;
   1012 	} */ *ap = v;
   1013 	struct vnode	*vp;
   1014 	struct inode	*ip;
   1015 
   1016 	vp = ap->a_vp;
   1017 	ip = VTOI(vp);
   1018 	if (vp->v_usecount > 1) {
   1019 		LFS_ITIMES(ip, NULL, NULL, NULL);
   1020 	}
   1021 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
   1022 }
   1023 
   1024 /*
   1025  * Close wrapper for fifo's.
   1026  *
   1027  * Update the times on the inode then do device close.
   1028  */
   1029 int
   1030 lfsfifo_close(void *v)
   1031 {
   1032 	struct vop_close_args /* {
   1033 		struct vnode	*a_vp;
   1034 		int		a_fflag;
   1035 		kauth_cred_	a_cred;
   1036 		struct lwp	*a_l;
   1037 	} */ *ap = v;
   1038 	struct vnode	*vp;
   1039 	struct inode	*ip;
   1040 
   1041 	vp = ap->a_vp;
   1042 	ip = VTOI(vp);
   1043 	if (ap->a_vp->v_usecount > 1) {
   1044 		LFS_ITIMES(ip, NULL, NULL, NULL);
   1045 	}
   1046 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
   1047 }
   1048 
   1049 /*
   1050  * Reclaim an inode so that it can be used for other purposes.
   1051  */
   1052 
   1053 int
   1054 lfs_reclaim(void *v)
   1055 {
   1056 	struct vop_reclaim_args /* {
   1057 		struct vnode *a_vp;
   1058 		struct lwp *a_l;
   1059 	} */ *ap = v;
   1060 	struct vnode *vp = ap->a_vp;
   1061 	struct inode *ip = VTOI(vp);
   1062 	int error;
   1063 
   1064 	KASSERT(ip->i_nlink == ip->i_ffs_effnlink);
   1065 
   1066 	LFS_CLR_UINO(ip, IN_ALLMOD);
   1067 	if ((error = ufs_reclaim(vp, ap->a_l)))
   1068 		return (error);
   1069 	pool_put(&lfs_dinode_pool, ip->i_din.ffs1_din);
   1070 	lfs_deregister_all(vp);
   1071 	pool_put(&lfs_inoext_pool, ip->inode_ext.lfs);
   1072 	ip->inode_ext.lfs = NULL;
   1073 	pool_put(&lfs_inode_pool, vp->v_data);
   1074 	vp->v_data = NULL;
   1075 	return (0);
   1076 }
   1077 
   1078 /*
   1079  * Read a block from a storage device.
   1080  * In order to avoid reading blocks that are in the process of being
   1081  * written by the cleaner---and hence are not mutexed by the normal
   1082  * buffer cache / page cache mechanisms---check for collisions before
   1083  * reading.
   1084  *
   1085  * We inline ufs_strategy to make sure that the VOP_BMAP occurs *before*
   1086  * the active cleaner test.
   1087  *
   1088  * XXX This code assumes that lfs_markv makes synchronous checkpoints.
   1089  */
   1090 int
   1091 lfs_strategy(void *v)
   1092 {
   1093 	struct vop_strategy_args /* {
   1094 		struct vnode *a_vp;
   1095 		struct buf *a_bp;
   1096 	} */ *ap = v;
   1097 	struct buf	*bp;
   1098 	struct lfs	*fs;
   1099 	struct vnode	*vp;
   1100 	struct inode	*ip;
   1101 	daddr_t		tbn;
   1102 	int		i, sn, error, slept;
   1103 
   1104 	bp = ap->a_bp;
   1105 	vp = ap->a_vp;
   1106 	ip = VTOI(vp);
   1107 	fs = ip->i_lfs;
   1108 
   1109 	/* lfs uses its strategy routine only for read */
   1110 	KASSERT(bp->b_flags & B_READ);
   1111 
   1112 	if (vp->v_type == VBLK || vp->v_type == VCHR)
   1113 		panic("lfs_strategy: spec");
   1114 	KASSERT(bp->b_bcount != 0);
   1115 	if (bp->b_blkno == bp->b_lblkno) {
   1116 		error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
   1117 				 NULL);
   1118 		if (error) {
   1119 			bp->b_error = error;
   1120 			bp->b_flags |= B_ERROR;
   1121 			biodone(bp);
   1122 			return (error);
   1123 		}
   1124 		if ((long)bp->b_blkno == -1) /* no valid data */
   1125 			clrbuf(bp);
   1126 	}
   1127 	if ((long)bp->b_blkno < 0) { /* block is not on disk */
   1128 		biodone(bp);
   1129 		return (0);
   1130 	}
   1131 
   1132 	slept = 1;
   1133 	simple_lock(&fs->lfs_interlock);
   1134 	while (slept && fs->lfs_seglock) {
   1135 		simple_unlock(&fs->lfs_interlock);
   1136 		/*
   1137 		 * Look through list of intervals.
   1138 		 * There will only be intervals to look through
   1139 		 * if the cleaner holds the seglock.
   1140 		 * Since the cleaner is synchronous, we can trust
   1141 		 * the list of intervals to be current.
   1142 		 */
   1143 		tbn = dbtofsb(fs, bp->b_blkno);
   1144 		sn = dtosn(fs, tbn);
   1145 		slept = 0;
   1146 		for (i = 0; i < fs->lfs_cleanind; i++) {
   1147 			if (sn == dtosn(fs, fs->lfs_cleanint[i]) &&
   1148 			    tbn >= fs->lfs_cleanint[i]) {
   1149 				DLOG((DLOG_CLEAN,
   1150 				      "lfs_strategy: ino %d lbn %" PRId64
   1151 				       " ind %d sn %d fsb %" PRIx32
   1152 				       " given sn %d fsb %" PRIx64 "\n",
   1153 					ip->i_number, bp->b_lblkno, i,
   1154 					dtosn(fs, fs->lfs_cleanint[i]),
   1155 					fs->lfs_cleanint[i], sn, tbn));
   1156 				DLOG((DLOG_CLEAN,
   1157 				      "lfs_strategy: sleeping on ino %d lbn %"
   1158 				      PRId64 "\n", ip->i_number, bp->b_lblkno));
   1159 				simple_lock(&fs->lfs_interlock);
   1160 				if (LFS_SEGLOCK_HELD(fs) && fs->lfs_iocount) {
   1161 					/* Cleaner can't wait for itself */
   1162 					ltsleep(&fs->lfs_iocount,
   1163 						(PRIBIO + 1) | PNORELOCK,
   1164 						"clean2", 0,
   1165 						&fs->lfs_interlock);
   1166 					slept = 1;
   1167 					break;
   1168 				} else if (fs->lfs_seglock) {
   1169 					ltsleep(&fs->lfs_seglock,
   1170 						(PRIBIO + 1) | PNORELOCK,
   1171 						"clean1", 0,
   1172 						&fs->lfs_interlock);
   1173 					slept = 1;
   1174 					break;
   1175 				}
   1176 				simple_unlock(&fs->lfs_interlock);
   1177 			}
   1178 		}
   1179 		simple_lock(&fs->lfs_interlock);
   1180 	}
   1181 	simple_unlock(&fs->lfs_interlock);
   1182 
   1183 	vp = ip->i_devvp;
   1184 	VOP_STRATEGY(vp, bp);
   1185 	return (0);
   1186 }
   1187 
   1188 void
   1189 lfs_flush_dirops(struct lfs *fs)
   1190 {
   1191 	struct inode *ip, *nip;
   1192 	struct vnode *vp;
   1193 	extern int lfs_dostats;
   1194 	struct segment *sp;
   1195 	int waslocked;
   1196 
   1197 	ASSERT_MAYBE_SEGLOCK(fs);
   1198 	KASSERT(fs->lfs_nadirop == 0);
   1199 
   1200 	if (fs->lfs_ronly)
   1201 		return;
   1202 
   1203 	simple_lock(&fs->lfs_interlock);
   1204 	if (TAILQ_FIRST(&fs->lfs_dchainhd) == NULL) {
   1205 		simple_unlock(&fs->lfs_interlock);
   1206 		return;
   1207 	} else
   1208 		simple_unlock(&fs->lfs_interlock);
   1209 
   1210 	if (lfs_dostats)
   1211 		++lfs_stats.flush_invoked;
   1212 
   1213 	/*
   1214 	 * Inline lfs_segwrite/lfs_writevnodes, but just for dirops.
   1215 	 * Technically this is a checkpoint (the on-disk state is valid)
   1216 	 * even though we are leaving out all the file data.
   1217 	 */
   1218 	lfs_imtime(fs);
   1219 	lfs_seglock(fs, SEGM_CKP);
   1220 	sp = fs->lfs_sp;
   1221 
   1222 	/*
   1223 	 * lfs_writevnodes, optimized to get dirops out of the way.
   1224 	 * Only write dirops, and don't flush files' pages, only
   1225 	 * blocks from the directories.
   1226 	 *
   1227 	 * We don't need to vref these files because they are
   1228 	 * dirops and so hold an extra reference until the
   1229 	 * segunlock clears them of that status.
   1230 	 *
   1231 	 * We don't need to check for IN_ADIROP because we know that
   1232 	 * no dirops are active.
   1233 	 *
   1234 	 */
   1235 	simple_lock(&fs->lfs_interlock);
   1236 	for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
   1237 		nip = TAILQ_NEXT(ip, i_lfs_dchain);
   1238 		simple_unlock(&fs->lfs_interlock);
   1239 		vp = ITOV(ip);
   1240 
   1241 		KASSERT((ip->i_flag & IN_ADIROP) == 0);
   1242 
   1243 		/*
   1244 		 * All writes to directories come from dirops; all
   1245 		 * writes to files' direct blocks go through the page
   1246 		 * cache, which we're not touching.  Reads to files
   1247 		 * and/or directories will not be affected by writing
   1248 		 * directory blocks inodes and file inodes.  So we don't
   1249 		 * really need to lock.  If we don't lock, though,
   1250 		 * make sure that we don't clear IN_MODIFIED
   1251 		 * unnecessarily.
   1252 		 */
   1253 		if (vp->v_flag & (VXLOCK | VFREEING)) {
   1254 			simple_lock(&fs->lfs_interlock);
   1255 			continue;
   1256 		}
   1257 		waslocked = VOP_ISLOCKED(vp);
   1258 		if (vp->v_type != VREG &&
   1259 		    ((ip->i_flag & IN_ALLMOD) || !VPISEMPTY(vp))) {
   1260 			lfs_writefile(fs, sp, vp);
   1261 			if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
   1262 			    !(ip->i_flag & IN_ALLMOD)) {
   1263 				LFS_SET_UINO(ip, IN_MODIFIED);
   1264 			}
   1265 		}
   1266 		(void) lfs_writeinode(fs, sp, ip);
   1267 		if (waslocked)
   1268 			LFS_SET_UINO(ip, IN_MODIFIED);
   1269 		simple_lock(&fs->lfs_interlock);
   1270 	}
   1271 	simple_unlock(&fs->lfs_interlock);
   1272 	/* We've written all the dirops there are */
   1273 	((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
   1274 	lfs_finalize_fs_seguse(fs);
   1275 	(void) lfs_writeseg(fs, sp);
   1276 	lfs_segunlock(fs);
   1277 }
   1278 
   1279 /*
   1280  * Flush all vnodes for which the pagedaemon has requested pageouts.
   1281  * Skip over any files that are marked VDIROP (since lfs_flush_dirop()
   1282  * has just run, this would be an error).  If we have to skip a vnode
   1283  * for any reason, just skip it; if we have to wait for the cleaner,
   1284  * abort.  The writer daemon will call us again later.
   1285  */
   1286 void
   1287 lfs_flush_pchain(struct lfs *fs)
   1288 {
   1289 	struct inode *ip, *nip;
   1290 	struct vnode *vp;
   1291 	extern int lfs_dostats;
   1292 	struct segment *sp;
   1293 	int error;
   1294 
   1295 	ASSERT_NO_SEGLOCK(fs);
   1296 
   1297 	if (fs->lfs_ronly)
   1298 		return;
   1299 
   1300 	simple_lock(&fs->lfs_interlock);
   1301 	if (TAILQ_FIRST(&fs->lfs_pchainhd) == NULL) {
   1302 		simple_unlock(&fs->lfs_interlock);
   1303 		return;
   1304 	} else
   1305 		simple_unlock(&fs->lfs_interlock);
   1306 
   1307 	/* Get dirops out of the way */
   1308 	lfs_flush_dirops(fs);
   1309 
   1310 	if (lfs_dostats)
   1311 		++lfs_stats.flush_invoked;
   1312 
   1313 	/*
   1314 	 * Inline lfs_segwrite/lfs_writevnodes, but just for pageouts.
   1315 	 */
   1316 	lfs_imtime(fs);
   1317 	lfs_seglock(fs, 0);
   1318 	sp = fs->lfs_sp;
   1319 
   1320 	/*
   1321 	 * lfs_writevnodes, optimized to clear pageout requests.
   1322 	 * Only write non-dirop files that are in the pageout queue.
   1323 	 * We're very conservative about what we write; we want to be
   1324 	 * fast and async.
   1325 	 */
   1326 	simple_lock(&fs->lfs_interlock);
   1327     top:
   1328 	for (ip = TAILQ_FIRST(&fs->lfs_pchainhd); ip != NULL; ip = nip) {
   1329 		nip = TAILQ_NEXT(ip, i_lfs_pchain);
   1330 		vp = ITOV(ip);
   1331 
   1332 		if (!(ip->i_flags & IN_PAGING))
   1333 			goto top;
   1334 
   1335 		if (vp->v_flag & (VXLOCK|VDIROP))
   1336 			continue;
   1337 		if (vp->v_type != VREG)
   1338 			continue;
   1339 		if (lfs_vref(vp))
   1340 			continue;
   1341 		simple_unlock(&fs->lfs_interlock);
   1342 
   1343 		if (VOP_ISLOCKED(vp)) {
   1344 			lfs_vunref(vp);
   1345 			simple_lock(&fs->lfs_interlock);
   1346 			continue;
   1347 		}
   1348 
   1349 		error = lfs_writefile(fs, sp, vp);
   1350 		if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
   1351 		    !(ip->i_flag & IN_ALLMOD)) {
   1352 			LFS_SET_UINO(ip, IN_MODIFIED);
   1353 		}
   1354 		(void) lfs_writeinode(fs, sp, ip);
   1355 
   1356 		lfs_vunref(vp);
   1357 
   1358 		if (error == EAGAIN) {
   1359 			lfs_writeseg(fs, sp);
   1360 			simple_lock(&fs->lfs_interlock);
   1361 			break;
   1362 		}
   1363 		simple_lock(&fs->lfs_interlock);
   1364 	}
   1365 	simple_unlock(&fs->lfs_interlock);
   1366 	(void) lfs_writeseg(fs, sp);
   1367 	lfs_segunlock(fs);
   1368 }
   1369 
   1370 /*
   1371  * Provide a fcntl interface to sys_lfs_{segwait,bmapv,markv}.
   1372  */
   1373 int
   1374 lfs_fcntl(void *v)
   1375 {
   1376 	struct vop_fcntl_args /* {
   1377 		struct vnode *a_vp;
   1378 		u_long a_command;
   1379 		caddr_t  a_data;
   1380 		int  a_fflag;
   1381 		kauth_cred_t a_cred;
   1382 		struct lwp *a_l;
   1383 	} */ *ap = v;
   1384 	struct timeval *tvp;
   1385 	BLOCK_INFO *blkiov;
   1386 	CLEANERINFO *cip;
   1387 	SEGUSE *sup;
   1388 	int blkcnt, error, oclean;
   1389 	size_t fh_size;
   1390 	struct lfs_fcntl_markv blkvp;
   1391 	struct proc *p;
   1392 	fsid_t *fsidp;
   1393 	struct lfs *fs;
   1394 	struct buf *bp;
   1395 	fhandle_t *fhp;
   1396 	daddr_t off;
   1397 
   1398 	/* Only respect LFS fcntls on fs root or Ifile */
   1399 	if (VTOI(ap->a_vp)->i_number != ROOTINO &&
   1400 	    VTOI(ap->a_vp)->i_number != LFS_IFILE_INUM) {
   1401 		return ufs_fcntl(v);
   1402 	}
   1403 
   1404 	/* Avoid locking a draining lock */
   1405 	if (ap->a_vp->v_mount->mnt_iflag & IMNT_UNMOUNT) {
   1406 		return ESHUTDOWN;
   1407 	}
   1408 
   1409 	p = ap->a_l->l_proc;
   1410 	fs = VTOI(ap->a_vp)->i_lfs;
   1411 	fsidp = &ap->a_vp->v_mount->mnt_stat.f_fsidx;
   1412 
   1413 	switch (ap->a_command) {
   1414 	    case LFCNSEGWAITALL:
   1415 	    case LFCNSEGWAITALL_COMPAT:
   1416 		fsidp = NULL;
   1417 		/* FALLSTHROUGH */
   1418 	    case LFCNSEGWAIT:
   1419 	    case LFCNSEGWAIT_COMPAT:
   1420 		tvp = (struct timeval *)ap->a_data;
   1421 		simple_lock(&fs->lfs_interlock);
   1422 		++fs->lfs_sleepers;
   1423 		simple_unlock(&fs->lfs_interlock);
   1424 
   1425 		error = lfs_segwait(fsidp, tvp);
   1426 
   1427 		simple_lock(&fs->lfs_interlock);
   1428 		if (--fs->lfs_sleepers == 0)
   1429 			wakeup(&fs->lfs_sleepers);
   1430 		simple_unlock(&fs->lfs_interlock);
   1431 		return error;
   1432 
   1433 	    case LFCNBMAPV:
   1434 	    case LFCNMARKV:
   1435 		if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
   1436 					       &p->p_acflag)) != 0)
   1437 			return (error);
   1438 		blkvp = *(struct lfs_fcntl_markv *)ap->a_data;
   1439 
   1440 		blkcnt = blkvp.blkcnt;
   1441 		if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
   1442 			return (EINVAL);
   1443 		blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
   1444 		if ((error = copyin(blkvp.blkiov, blkiov,
   1445 		     blkcnt * sizeof(BLOCK_INFO))) != 0) {
   1446 			lfs_free(fs, blkiov, LFS_NB_BLKIOV);
   1447 			return error;
   1448 		}
   1449 
   1450 		simple_lock(&fs->lfs_interlock);
   1451 		++fs->lfs_sleepers;
   1452 		simple_unlock(&fs->lfs_interlock);
   1453 		if (ap->a_command == LFCNBMAPV)
   1454 			error = lfs_bmapv(p, fsidp, blkiov, blkcnt);
   1455 		else /* LFCNMARKV */
   1456 			error = lfs_markv(p, fsidp, blkiov, blkcnt);
   1457 		if (error == 0)
   1458 			error = copyout(blkiov, blkvp.blkiov,
   1459 					blkcnt * sizeof(BLOCK_INFO));
   1460 		simple_lock(&fs->lfs_interlock);
   1461 		if (--fs->lfs_sleepers == 0)
   1462 			wakeup(&fs->lfs_sleepers);
   1463 		simple_unlock(&fs->lfs_interlock);
   1464 		lfs_free(fs, blkiov, LFS_NB_BLKIOV);
   1465 		return error;
   1466 
   1467 	    case LFCNRECLAIM:
   1468 		/*
   1469 		 * Flush dirops and write Ifile, allowing empty segments
   1470 		 * to be immediately reclaimed.
   1471 		 */
   1472 		lfs_writer_enter(fs, "pndirop");
   1473 		off = fs->lfs_offset;
   1474 		lfs_seglock(fs, SEGM_FORCE_CKP | SEGM_CKP);
   1475 		lfs_flush_dirops(fs);
   1476 		LFS_CLEANERINFO(cip, fs, bp);
   1477 		oclean = cip->clean;
   1478 		LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
   1479 		lfs_segwrite(ap->a_vp->v_mount, SEGM_FORCE_CKP);
   1480 		fs->lfs_sp->seg_flags |= SEGM_PROT;
   1481 		lfs_segunlock(fs);
   1482 		lfs_writer_leave(fs);
   1483 
   1484 #ifdef DEBUG
   1485 		LFS_CLEANERINFO(cip, fs, bp);
   1486 		DLOG((DLOG_CLEAN, "lfs_fcntl: reclaim wrote %" PRId64
   1487 		      " blocks, cleaned %" PRId32 " segments (activesb %d)\n",
   1488 		      fs->lfs_offset - off, cip->clean - oclean,
   1489 		      fs->lfs_activesb));
   1490 		LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
   1491 #endif
   1492 
   1493 		return 0;
   1494 
   1495 #ifdef COMPAT_30
   1496 	    case LFCNIFILEFH_COMPAT:
   1497 		/* Return the filehandle of the Ifile */
   1498 		if ((error = kauth_authorize_generic(ap->a_l->l_proc->p_cred,
   1499 					       KAUTH_GENERIC_ISSUSER,
   1500 					       &ap->a_l->l_proc->p_acflag)) != 0)
   1501 			return (error);
   1502 		fhp = (struct fhandle *)ap->a_data;
   1503 		fhp->fh_fsid = *fsidp;
   1504 		fh_size = 16;	/* former VFS_MAXFIDSIZ */
   1505 		return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
   1506 #endif
   1507 
   1508 	    case LFCNIFILEFH:
   1509 		/* Return the filehandle of the Ifile */
   1510 		if ((error = kauth_authorize_generic(ap->a_l->l_proc->p_cred,
   1511 					       KAUTH_GENERIC_ISSUSER,
   1512 					       &ap->a_l->l_proc->p_acflag)) != 0)
   1513 			return (error);
   1514 		fhp = (struct fhandle *)ap->a_data;
   1515 		fhp->fh_fsid = *fsidp;
   1516 		fh_size = sizeof(union lfs_fhandle) -
   1517 		    offsetof(fhandle_t, fh_fid);
   1518 		return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
   1519 
   1520 	    case LFCNREWIND:
   1521 		/* Move lfs_offset to the lowest-numbered segment */
   1522 		return lfs_rewind(fs, *(int *)ap->a_data);
   1523 
   1524 	    case LFCNINVAL:
   1525 		/* Mark a segment SEGUSE_INVAL */
   1526 		LFS_SEGENTRY(sup, fs, *(int *)ap->a_data, bp);
   1527 		if (sup->su_nbytes > 0) {
   1528 			brelse(bp);
   1529 			lfs_unset_inval_all(fs);
   1530 			return EBUSY;
   1531 		}
   1532 		sup->su_flags |= SEGUSE_INVAL;
   1533 		VOP_BWRITE(bp);
   1534 		return 0;
   1535 
   1536 	    case LFCNRESIZE:
   1537 		/* Resize the filesystem */
   1538 		return lfs_resize_fs(fs, *(int *)ap->a_data);
   1539 
   1540 	    case LFCNWRAPSTOP:
   1541 	    case LFCNWRAPSTOP_COMPAT:
   1542 		/*
   1543 		 * Hold lfs_newseg at segment 0; if requested, sleep until
   1544 		 * the filesystem wraps around.  To support external agents
   1545 		 * (dump, fsck-based regression test) that need to look at
   1546 		 * a snapshot of the filesystem, without necessarily
   1547 		 * requiring that all fs activity stops.
   1548 		 */
   1549 		if (VTOI(ap->a_vp)->i_lfs_iflags & LFSI_WRAPBLOCK)
   1550 			return EALREADY;
   1551 
   1552 		simple_lock(&fs->lfs_interlock);
   1553 		VTOI(ap->a_vp)->i_lfs_iflags |= LFSI_WRAPBLOCK;
   1554 		if (fs->lfs_nowrap == 0)
   1555 			log(LOG_NOTICE, "%s: disabled log wrap\n", fs->lfs_fsmnt);
   1556 		++fs->lfs_nowrap;
   1557 		if (*(int *)ap->a_data == 1 ||
   1558 		    ap->a_command == LFCNWRAPSTOP_COMPAT) {
   1559 			error = ltsleep(&fs->lfs_nowrap, PCATCH | PUSER,
   1560 				"segwrap", 0, &fs->lfs_interlock);
   1561 			if (error) {
   1562 				lfs_wrapgo(fs, VTOI(ap->a_vp), 0);
   1563 			}
   1564 		}
   1565 		simple_unlock(&fs->lfs_interlock);
   1566 		return 0;
   1567 
   1568 	    case LFCNWRAPGO:
   1569 	    case LFCNWRAPGO_COMPAT:
   1570 		/*
   1571 		 * Having done its work, the agent wakes up the writer.
   1572 		 * If the argument is 1, it sleeps until a new segment
   1573 		 * is selected.
   1574 		 */
   1575 		simple_lock(&fs->lfs_interlock);
   1576 		error = lfs_wrapgo(fs, VTOI(ap->a_vp),
   1577 				   (ap->a_command == LFCNWRAPGO_COMPAT ? 1 :
   1578 				    *((int *)ap->a_data)));
   1579 		simple_unlock(&fs->lfs_interlock);
   1580 		return error;
   1581 
   1582 	    default:
   1583 		return ufs_fcntl(v);
   1584 	}
   1585 	return 0;
   1586 }
   1587 
   1588 int
   1589 lfs_getpages(void *v)
   1590 {
   1591 	struct vop_getpages_args /* {
   1592 		struct vnode *a_vp;
   1593 		voff_t a_offset;
   1594 		struct vm_page **a_m;
   1595 		int *a_count;
   1596 		int a_centeridx;
   1597 		vm_prot_t a_access_type;
   1598 		int a_advice;
   1599 		int a_flags;
   1600 	} */ *ap = v;
   1601 
   1602 	if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM &&
   1603 	    (ap->a_access_type & VM_PROT_WRITE) != 0) {
   1604 		return EPERM;
   1605 	}
   1606 	if ((ap->a_access_type & VM_PROT_WRITE) != 0) {
   1607 		LFS_SET_UINO(VTOI(ap->a_vp), IN_MODIFIED);
   1608 	}
   1609 
   1610 	/*
   1611 	 * we're relying on the fact that genfs_getpages() always read in
   1612 	 * entire filesystem blocks.
   1613 	 */
   1614 	return genfs_getpages(v);
   1615 }
   1616 
   1617 /*
   1618  * Make sure that for all pages in every block in the given range,
   1619  * either all are dirty or all are clean.  If any of the pages
   1620  * we've seen so far are dirty, put the vnode on the paging chain,
   1621  * and mark it IN_PAGING.
   1622  *
   1623  * If checkfirst != 0, don't check all the pages but return at the
   1624  * first dirty page.
   1625  */
   1626 static int
   1627 check_dirty(struct lfs *fs, struct vnode *vp,
   1628 	    off_t startoffset, off_t endoffset, off_t blkeof,
   1629 	    int flags, int checkfirst)
   1630 {
   1631 	int by_list;
   1632 	struct vm_page *curpg = NULL; /* XXX: gcc */
   1633 	struct vm_page *pgs[MAXBSIZE / PAGE_SIZE], *pg;
   1634 	off_t soff = 0; /* XXX: gcc */
   1635 	voff_t off;
   1636 	int i;
   1637 	int nonexistent;
   1638 	int any_dirty;	/* number of dirty pages */
   1639 	int dirty;	/* number of dirty pages in a block */
   1640 	int tdirty;
   1641 	int pages_per_block = fs->lfs_bsize >> PAGE_SHIFT;
   1642 	int pagedaemon = (curproc == uvm.pagedaemon_proc);
   1643 
   1644 	ASSERT_MAYBE_SEGLOCK(fs);
   1645   top:
   1646 	by_list = (vp->v_uobj.uo_npages <=
   1647 		   ((endoffset - startoffset) >> PAGE_SHIFT) *
   1648 		   UVM_PAGE_HASH_PENALTY);
   1649 	any_dirty = 0;
   1650 
   1651 	if (by_list) {
   1652 		curpg = TAILQ_FIRST(&vp->v_uobj.memq);
   1653 	} else {
   1654 		soff = startoffset;
   1655 	}
   1656 	while (by_list || soff < MIN(blkeof, endoffset)) {
   1657 		if (by_list) {
   1658 			/*
   1659 			 * Find the first page in a block.  Skip
   1660 			 * blocks outside our area of interest or beyond
   1661 			 * the end of file.
   1662 			 */
   1663 			if (pages_per_block > 1) {
   1664 				while (curpg &&
   1665 				       ((curpg->offset & fs->lfs_bmask) ||
   1666 					curpg->offset >= vp->v_size ||
   1667 					curpg->offset >= endoffset))
   1668 					curpg = TAILQ_NEXT(curpg, listq);
   1669 			}
   1670 			if (curpg == NULL)
   1671 				break;
   1672 			soff = curpg->offset;
   1673 		}
   1674 
   1675 		/*
   1676 		 * Mark all pages in extended range busy; find out if any
   1677 		 * of them are dirty.
   1678 		 */
   1679 		nonexistent = dirty = 0;
   1680 		for (i = 0; i == 0 || i < pages_per_block; i++) {
   1681 			if (by_list && pages_per_block <= 1) {
   1682 				pgs[i] = pg = curpg;
   1683 			} else {
   1684 				off = soff + (i << PAGE_SHIFT);
   1685 				pgs[i] = pg = uvm_pagelookup(&vp->v_uobj, off);
   1686 				if (pg == NULL) {
   1687 					++nonexistent;
   1688 					continue;
   1689 				}
   1690 			}
   1691 			KASSERT(pg != NULL);
   1692 
   1693 			/*
   1694 			 * If we're holding the segment lock, we can deadlock
   1695 			 * against a process that has our page and is waiting
   1696 			 * for the cleaner, while the cleaner waits for the
   1697 			 * segment lock.  Just bail in that case.
   1698 			 */
   1699 			if ((pg->flags & PG_BUSY) &&
   1700 			    (pagedaemon || LFS_SEGLOCK_HELD(fs))) {
   1701 				if (by_list && i > 0)
   1702 					uvm_page_unbusy(pgs, i);
   1703 				DLOG((DLOG_PAGE, "lfs_putpages: avoiding 3-way or pagedaemon deadlock\n"));
   1704 				return -1;
   1705 			}
   1706 
   1707 			while (pg->flags & PG_BUSY) {
   1708 				pg->flags |= PG_WANTED;
   1709 				UVM_UNLOCK_AND_WAIT(pg, &vp->v_interlock, 0,
   1710 						    "lfsput", 0);
   1711 				simple_lock(&vp->v_interlock);
   1712 				if (by_list) {
   1713 					if (i > 0)
   1714 						uvm_page_unbusy(pgs, i);
   1715 					goto top;
   1716 				}
   1717 			}
   1718 			pg->flags |= PG_BUSY;
   1719 			UVM_PAGE_OWN(pg, "lfs_putpages");
   1720 
   1721 			pmap_page_protect(pg, VM_PROT_NONE);
   1722 			tdirty = (pmap_clear_modify(pg) ||
   1723 				  (pg->flags & PG_CLEAN) == 0);
   1724 			dirty += tdirty;
   1725 		}
   1726 		if (pages_per_block > 0 && nonexistent >= pages_per_block) {
   1727 			if (by_list) {
   1728 				curpg = TAILQ_NEXT(curpg, listq);
   1729 			} else {
   1730 				soff += fs->lfs_bsize;
   1731 			}
   1732 			continue;
   1733 		}
   1734 
   1735 		any_dirty += dirty;
   1736 		KASSERT(nonexistent == 0);
   1737 
   1738 		/*
   1739 		 * If any are dirty make all dirty; unbusy them,
   1740 		 * but if we were asked to clean, wire them so that
   1741 		 * the pagedaemon doesn't bother us about them while
   1742 		 * they're on their way to disk.
   1743 		 */
   1744 		for (i = 0; i == 0 || i < pages_per_block; i++) {
   1745 			pg = pgs[i];
   1746 			KASSERT(!((pg->flags & PG_CLEAN) && (pg->flags & PG_DELWRI)));
   1747 			if (dirty) {
   1748 				pg->flags &= ~PG_CLEAN;
   1749 				if (flags & PGO_FREE) {
   1750 					/*
   1751 					 * Wire the page so that
   1752 					 * pdaemon doesn't see it again.
   1753 					 */
   1754 					uvm_lock_pageq();
   1755 					uvm_pagewire(pg);
   1756 					uvm_unlock_pageq();
   1757 
   1758 					/* Suspended write flag */
   1759 					pg->flags |= PG_DELWRI;
   1760 				}
   1761 			}
   1762 			if (pg->flags & PG_WANTED)
   1763 				wakeup(pg);
   1764 			pg->flags &= ~(PG_WANTED|PG_BUSY);
   1765 			UVM_PAGE_OWN(pg, NULL);
   1766 		}
   1767 
   1768 		if (checkfirst && any_dirty)
   1769 			break;
   1770 
   1771 		if (by_list) {
   1772 			curpg = TAILQ_NEXT(curpg, listq);
   1773 		} else {
   1774 			soff += MAX(PAGE_SIZE, fs->lfs_bsize);
   1775 		}
   1776 	}
   1777 
   1778 	return any_dirty;
   1779 }
   1780 
   1781 /*
   1782  * lfs_putpages functions like genfs_putpages except that
   1783  *
   1784  * (1) It needs to bounds-check the incoming requests to ensure that
   1785  *     they are block-aligned; if they are not, expand the range and
   1786  *     do the right thing in case, e.g., the requested range is clean
   1787  *     but the expanded range is dirty.
   1788  *
   1789  * (2) It needs to explicitly send blocks to be written when it is done.
   1790  *     VOP_PUTPAGES is not ever called with the seglock held, so
   1791  *     we simply take the seglock and let lfs_segunlock wait for us.
   1792  *     XXX Actually we can be called with the seglock held, if we have
   1793  *     XXX to flush a vnode while lfs_markv is in operation.  As of this
   1794  *     XXX writing we panic in this case.
   1795  *
   1796  * Assumptions:
   1797  *
   1798  * (1) The caller does not hold any pages in this vnode busy.  If it does,
   1799  *     there is a danger that when we expand the page range and busy the
   1800  *     pages we will deadlock.
   1801  *
   1802  * (2) We are called with vp->v_interlock held; we must return with it
   1803  *     released.
   1804  *
   1805  * (3) We don't absolutely have to free pages right away, provided that
   1806  *     the request does not have PGO_SYNCIO.  When the pagedaemon gives
   1807  *     us a request with PGO_FREE, we take the pages out of the paging
   1808  *     queue and wake up the writer, which will handle freeing them for us.
   1809  *
   1810  *     We ensure that for any filesystem block, all pages for that
   1811  *     block are either resident or not, even if those pages are higher
   1812  *     than EOF; that means that we will be getting requests to free
   1813  *     "unused" pages above EOF all the time, and should ignore them.
   1814  *
   1815  * (4) If we are called with PGO_LOCKED, the finfo array we are to write
   1816  *     into has been set up for us by lfs_writefile.  If not, we will
   1817  *     have to handle allocating and/or freeing an finfo entry.
   1818  *
   1819  * XXX note that we're (ab)using PGO_LOCKED as "seglock held".
   1820  */
   1821 
   1822 int
   1823 lfs_putpages(void *v)
   1824 {
   1825 	int error;
   1826 	struct vop_putpages_args /* {
   1827 		struct vnode *a_vp;
   1828 		voff_t a_offlo;
   1829 		voff_t a_offhi;
   1830 		int a_flags;
   1831 	} */ *ap = v;
   1832 	struct vnode *vp;
   1833 	struct inode *ip;
   1834 	struct lfs *fs;
   1835 	struct segment *sp;
   1836 	off_t origoffset, startoffset, endoffset, origendoffset, blkeof;
   1837 	off_t off, max_endoffset;
   1838 	int s;
   1839 	boolean_t seglocked, sync, pagedaemon;
   1840 	struct vm_page *pg;
   1841 	UVMHIST_FUNC("lfs_putpages"); UVMHIST_CALLED(ubchist);
   1842 
   1843 	vp = ap->a_vp;
   1844 	ip = VTOI(vp);
   1845 	fs = ip->i_lfs;
   1846 	sync = (ap->a_flags & PGO_SYNCIO) != 0;
   1847 	pagedaemon = (curproc == uvm.pagedaemon_proc);
   1848 
   1849 	/* Putpages does nothing for metadata. */
   1850 	if (vp == fs->lfs_ivnode || vp->v_type != VREG) {
   1851 		simple_unlock(&vp->v_interlock);
   1852 		return 0;
   1853 	}
   1854 
   1855 	/*
   1856 	 * If there are no pages, don't do anything.
   1857 	 */
   1858 	if (vp->v_uobj.uo_npages == 0) {
   1859 		s = splbio();
   1860 		if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL &&
   1861 		    (vp->v_flag & VONWORKLST)) {
   1862 			vp->v_flag &= ~VONWORKLST;
   1863 			LIST_REMOVE(vp, v_synclist);
   1864 		}
   1865 		splx(s);
   1866 		simple_unlock(&vp->v_interlock);
   1867 
   1868 		/* Remove us from paging queue, if we were on it */
   1869 		simple_lock(&fs->lfs_interlock);
   1870 		if (ip->i_flags & IN_PAGING) {
   1871 			ip->i_flags &= ~IN_PAGING;
   1872 			TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
   1873 		}
   1874 		simple_unlock(&fs->lfs_interlock);
   1875 		return 0;
   1876 	}
   1877 
   1878 	blkeof = blkroundup(fs, ip->i_size);
   1879 
   1880 	/*
   1881 	 * Ignore requests to free pages past EOF but in the same block
   1882 	 * as EOF, unless the request is synchronous.  (If the request is
   1883 	 * sync, it comes from lfs_truncate.)
   1884 	 * XXXUBC Make these pages look "active" so the pagedaemon won't
   1885 	 * XXXUBC bother us with them again.
   1886 	 */
   1887 	if (!sync && ap->a_offlo >= ip->i_size && ap->a_offlo < blkeof) {
   1888 		origoffset = ap->a_offlo;
   1889 		for (off = origoffset; off < blkeof; off += fs->lfs_bsize) {
   1890 			pg = uvm_pagelookup(&vp->v_uobj, off);
   1891 			KASSERT(pg != NULL);
   1892 			while (pg->flags & PG_BUSY) {
   1893 				pg->flags |= PG_WANTED;
   1894 				UVM_UNLOCK_AND_WAIT(pg, &vp->v_interlock, 0,
   1895 						    "lfsput2", 0);
   1896 				simple_lock(&vp->v_interlock);
   1897 			}
   1898 			uvm_lock_pageq();
   1899 			uvm_pageactivate(pg);
   1900 			uvm_unlock_pageq();
   1901 		}
   1902 		ap->a_offlo = blkeof;
   1903 		if (ap->a_offhi > 0 && ap->a_offhi <= ap->a_offlo) {
   1904 			simple_unlock(&vp->v_interlock);
   1905 			return 0;
   1906 		}
   1907 	}
   1908 
   1909 	/*
   1910 	 * Extend page range to start and end at block boundaries.
   1911 	 * (For the purposes of VOP_PUTPAGES, fragments don't exist.)
   1912 	 */
   1913 	origoffset = ap->a_offlo;
   1914 	origendoffset = ap->a_offhi;
   1915 	startoffset = origoffset & ~(fs->lfs_bmask);
   1916 	max_endoffset = (trunc_page(LLONG_MAX) >> fs->lfs_bshift)
   1917 					       << fs->lfs_bshift;
   1918 
   1919 	if (origendoffset == 0 || ap->a_flags & PGO_ALLPAGES) {
   1920 		endoffset = max_endoffset;
   1921 		origendoffset = endoffset;
   1922 	} else {
   1923 		origendoffset = round_page(ap->a_offhi);
   1924 		endoffset = round_page(blkroundup(fs, origendoffset));
   1925 	}
   1926 
   1927 	KASSERT(startoffset > 0 || endoffset >= startoffset);
   1928 	if (startoffset == endoffset) {
   1929 		/* Nothing to do, why were we called? */
   1930 		simple_unlock(&vp->v_interlock);
   1931 		DLOG((DLOG_PAGE, "lfs_putpages: startoffset = endoffset = %"
   1932 		      PRId64 "\n", startoffset));
   1933 		return 0;
   1934 	}
   1935 
   1936 	ap->a_offlo = startoffset;
   1937 	ap->a_offhi = endoffset;
   1938 
   1939 	if (!(ap->a_flags & PGO_CLEANIT))
   1940 		return genfs_putpages(v);
   1941 
   1942 	/*
   1943 	 * If there are more than one page per block, we don't want
   1944 	 * to get caught locking them backwards; so set PGO_BUSYFAIL
   1945 	 * to avoid deadlocks.
   1946 	 */
   1947 	ap->a_flags |= PGO_BUSYFAIL;
   1948 
   1949 	do {
   1950 		int r;
   1951 
   1952 		/* If no pages are dirty, we can just use genfs_putpages. */
   1953 		r = check_dirty(fs, vp, startoffset, endoffset, blkeof,
   1954 				ap->a_flags, 1);
   1955 		if (r < 0) {
   1956 			simple_unlock(&vp->v_interlock);
   1957 			return EDEADLK;
   1958 		}
   1959 		if (r > 0)
   1960 			break;
   1961 
   1962 		/*
   1963 		 * Sometimes pages are dirtied between the time that
   1964 		 * we check and the time we try to clean them.
   1965 		 * Instruct lfs_gop_write to return EDEADLK in this case
   1966 		 * so we can write them properly.
   1967 		 */
   1968 		ip->i_lfs_iflags |= LFSI_NO_GOP_WRITE;
   1969 		r = genfs_putpages(v);
   1970 		ip->i_lfs_iflags &= ~LFSI_NO_GOP_WRITE;
   1971 		if (r != EDEADLK)
   1972 			return r;
   1973 
   1974 		/* Start over. */
   1975 		preempt(1);
   1976 		simple_lock(&vp->v_interlock);
   1977 	} while(1);
   1978 
   1979 	/*
   1980 	 * Dirty and asked to clean.
   1981 	 *
   1982 	 * Pagedaemon can't actually write LFS pages; wake up
   1983 	 * the writer to take care of that.  The writer will
   1984 	 * notice the pager inode queue and act on that.
   1985 	 */
   1986 	if (pagedaemon) {
   1987 		simple_lock(&fs->lfs_interlock);
   1988 		if (!(ip->i_flags & IN_PAGING)) {
   1989 			ip->i_flags |= IN_PAGING;
   1990 			TAILQ_INSERT_TAIL(&fs->lfs_pchainhd, ip, i_lfs_pchain);
   1991 		}
   1992 		simple_lock(&lfs_subsys_lock);
   1993 		wakeup(&lfs_writer_daemon);
   1994 		simple_unlock(&lfs_subsys_lock);
   1995 		simple_unlock(&fs->lfs_interlock);
   1996 		simple_unlock(&vp->v_interlock);
   1997 		preempt(1);
   1998 		return EWOULDBLOCK;
   1999 	}
   2000 
   2001 	/*
   2002 	 * If this is a file created in a recent dirop, we can't flush its
   2003 	 * inode until the dirop is complete.  Drain dirops, then flush the
   2004 	 * filesystem (taking care of any other pending dirops while we're
   2005 	 * at it).
   2006 	 */
   2007 	if ((ap->a_flags & (PGO_CLEANIT|PGO_LOCKED)) == PGO_CLEANIT &&
   2008 	    (vp->v_flag & VDIROP)) {
   2009 		int locked;
   2010 
   2011 		DLOG((DLOG_PAGE, "lfs_putpages: flushing VDIROP\n"));
   2012 		locked = VOP_ISLOCKED(vp) && /* XXX */
   2013 			vp->v_lock.lk_lockholder == curproc->p_pid;
   2014 		simple_unlock(&vp->v_interlock);
   2015 		lfs_writer_enter(fs, "ppdirop");
   2016 		if (locked)
   2017 			VOP_UNLOCK(vp, 0);
   2018 
   2019 		simple_lock(&fs->lfs_interlock);
   2020 		lfs_flush_fs(fs, sync ? SEGM_SYNC : 0);
   2021 		simple_unlock(&fs->lfs_interlock);
   2022 
   2023 		simple_lock(&vp->v_interlock);
   2024 		if (locked) {
   2025 			VOP_LOCK(vp, LK_EXCLUSIVE | LK_INTERLOCK);
   2026 			simple_lock(&vp->v_interlock);
   2027 		}
   2028 		lfs_writer_leave(fs);
   2029 
   2030 		/* XXX the flush should have taken care of this one too! */
   2031 	}
   2032 
   2033 	/*
   2034 	 * This is it.	We are going to write some pages.  From here on
   2035 	 * down it's all just mechanics.
   2036 	 *
   2037 	 * Don't let genfs_putpages wait; lfs_segunlock will wait for us.
   2038 	 */
   2039 	ap->a_flags &= ~PGO_SYNCIO;
   2040 
   2041 	/*
   2042 	 * If we've already got the seglock, flush the node and return.
   2043 	 * The FIP has already been set up for us by lfs_writefile,
   2044 	 * and FIP cleanup and lfs_updatemeta will also be done there,
   2045 	 * unless genfs_putpages returns EDEADLK; then we must flush
   2046 	 * what we have, and correct FIP and segment header accounting.
   2047 	 */
   2048     get_seglock:
   2049 	seglocked = (ap->a_flags & PGO_LOCKED) != 0;
   2050 	if (!seglocked) {
   2051 		simple_unlock(&vp->v_interlock);
   2052 		/*
   2053 		 * Take the seglock, because we are going to be writing pages.
   2054 		 */
   2055 		error = lfs_seglock(fs, SEGM_PROT | (sync ? SEGM_SYNC : 0));
   2056 		if (error != 0)
   2057 			return error;
   2058 		simple_lock(&vp->v_interlock);
   2059 	}
   2060 
   2061 	/*
   2062 	 * VOP_PUTPAGES should not be called while holding the seglock.
   2063 	 * XXXUBC fix lfs_markv, or do this properly.
   2064 	 */
   2065 #ifdef notyet
   2066 	KASSERT(fs->lfs_seglock == 1);
   2067 #endif /* notyet */
   2068 
   2069 	/*
   2070 	 * We assume we're being called with sp->fip pointing at blank space.
   2071 	 * Account for a new FIP in the segment header, and set sp->vp.
   2072 	 * (This should duplicate the setup at the top of lfs_writefile().)
   2073 	 */
   2074 	sp = fs->lfs_sp;
   2075 	if (!seglocked)
   2076 		lfs_acquire_finfo(fs, ip->i_number, ip->i_gen);
   2077 	KASSERT(sp->vp == NULL);
   2078 	sp->vp = vp;
   2079 
   2080 	if (!seglocked) {
   2081 		if (vp->v_flag & VDIROP)
   2082 			((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
   2083 	}
   2084 
   2085 	/*
   2086 	 * Loop through genfs_putpages until all pages are gathered.
   2087 	 * genfs_putpages() drops the interlock, so reacquire it if necessary.
   2088 	 * Whenever we lose the interlock we have to rerun check_dirty, as
   2089 	 * well.
   2090 	 */
   2091 again:
   2092 	if (check_dirty(fs, vp, startoffset, endoffset, blkeof,
   2093 	    ap->a_flags, 0) < 0) {
   2094 		simple_unlock(&vp->v_interlock);
   2095 		sp->vp = NULL;
   2096 		if (!seglocked) {
   2097 			lfs_release_finfo(fs);
   2098 			lfs_segunlock(fs);
   2099 		}
   2100 		if (pagedaemon)
   2101 			return EDEADLK;
   2102 		/* else seglocked == 0 */
   2103 		preempt(1);
   2104 		simple_lock(&vp->v_interlock);
   2105 		goto get_seglock;
   2106 	}
   2107 
   2108 	error = genfs_putpages(v);
   2109 	if (error == EDEADLK || error == EAGAIN) {
   2110 		DLOG((DLOG_PAGE, "lfs_putpages: genfs_putpages returned"
   2111 		      " EDEADLK [2] ino %d off %x (seg %d)\n",
   2112 		      ip->i_number, fs->lfs_offset,
   2113 		      dtosn(fs, fs->lfs_offset)));
   2114 		/* If nothing to write, short-circuit */
   2115 		if (sp->cbpp - sp->bpp > 1) {
   2116 			/* Write gathered pages */
   2117 			lfs_updatemeta(sp);
   2118 			lfs_release_finfo(fs);
   2119 			(void) lfs_writeseg(fs, sp);
   2120 
   2121 			/*
   2122 			 * Reinitialize brand new FIP and add us to it.
   2123 			 */
   2124 			KASSERT(sp->vp == vp);
   2125 			lfs_acquire_finfo(fs, ip->i_number, ip->i_gen);
   2126 		}
   2127 
   2128 		/* Give the write a chance to complete */
   2129 		preempt(1);
   2130 
   2131 		/* We've lost the interlock.  Start over. */
   2132 		if (error == EDEADLK) {
   2133 			simple_lock(&vp->v_interlock);
   2134 			goto again;
   2135 		}
   2136 	}
   2137 
   2138 	KASSERT(sp->vp == vp);
   2139 	if (!seglocked) {
   2140 		sp->vp = NULL;
   2141 
   2142 		/* Write indirect blocks as well */
   2143 		lfs_gather(fs, fs->lfs_sp, vp, lfs_match_indir);
   2144 		lfs_gather(fs, fs->lfs_sp, vp, lfs_match_dindir);
   2145 		lfs_gather(fs, fs->lfs_sp, vp, lfs_match_tindir);
   2146 
   2147 		KASSERT(sp->vp == NULL);
   2148 		sp->vp = vp;
   2149 	}
   2150 
   2151 	/*
   2152 	 * Blocks are now gathered into a segment waiting to be written.
   2153 	 * All that's left to do is update metadata, and write them.
   2154 	 */
   2155 	lfs_updatemeta(sp);
   2156 	KASSERT(sp->vp == vp);
   2157 	sp->vp = NULL;
   2158 
   2159 	if (seglocked) {
   2160 		/* we're called by lfs_writefile. */
   2161 		return error;
   2162 	}
   2163 
   2164 	/* Clean up FIP and send it to disk. */
   2165 	lfs_release_finfo(fs);
   2166 	lfs_writeseg(fs, fs->lfs_sp);
   2167 
   2168 	/*
   2169 	 * Remove us from paging queue, since we've now written all our
   2170 	 * pages.
   2171 	 */
   2172 	simple_lock(&fs->lfs_interlock);
   2173 	if (ip->i_flags & IN_PAGING) {
   2174 		ip->i_flags &= ~IN_PAGING;
   2175 		TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
   2176 	}
   2177 	simple_unlock(&fs->lfs_interlock);
   2178 
   2179 	/*
   2180 	 * XXX - with the malloc/copy writeseg, the pages are freed by now
   2181 	 * even if we don't wait (e.g. if we hold a nested lock).  This
   2182 	 * will not be true if we stop using malloc/copy.
   2183 	 */
   2184 	KASSERT(fs->lfs_sp->seg_flags & SEGM_PROT);
   2185 	lfs_segunlock(fs);
   2186 
   2187 	/*
   2188 	 * Wait for v_numoutput to drop to zero.  The seglock should
   2189 	 * take care of this, but there is a slight possibility that
   2190 	 * aiodoned might not have got around to our buffers yet.
   2191 	 */
   2192 	if (sync) {
   2193 		s = splbio();
   2194 		simple_lock(&global_v_numoutput_slock);
   2195 		while (vp->v_numoutput > 0) {
   2196 			DLOG((DLOG_PAGE, "lfs_putpages: ino %d sleeping on"
   2197 			      " num %d\n", ip->i_number, vp->v_numoutput));
   2198 			vp->v_flag |= VBWAIT;
   2199 			ltsleep(&vp->v_numoutput, PRIBIO + 1, "lfs_vn", 0,
   2200 			    &global_v_numoutput_slock);
   2201 		}
   2202 		simple_unlock(&global_v_numoutput_slock);
   2203 		splx(s);
   2204 	}
   2205 	return error;
   2206 }
   2207 
   2208 /*
   2209  * Return the last logical file offset that should be written for this file
   2210  * if we're doing a write that ends at "size".	If writing, we need to know
   2211  * about sizes on disk, i.e. fragments if there are any; if reading, we need
   2212  * to know about entire blocks.
   2213  */
   2214 void
   2215 lfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
   2216 {
   2217 	struct inode *ip = VTOI(vp);
   2218 	struct lfs *fs = ip->i_lfs;
   2219 	daddr_t olbn, nlbn;
   2220 
   2221 	olbn = lblkno(fs, ip->i_size);
   2222 	nlbn = lblkno(fs, size);
   2223 	if (!(flags & GOP_SIZE_MEM) && nlbn < NDADDR && olbn <= nlbn) {
   2224 		*eobp = fragroundup(fs, size);
   2225 	} else {
   2226 		*eobp = blkroundup(fs, size);
   2227 	}
   2228 }
   2229 
   2230 #ifdef DEBUG
   2231 void lfs_dump_vop(void *);
   2232 
   2233 void
   2234 lfs_dump_vop(void *v)
   2235 {
   2236 	struct vop_putpages_args /* {
   2237 		struct vnode *a_vp;
   2238 		voff_t a_offlo;
   2239 		voff_t a_offhi;
   2240 		int a_flags;
   2241 	} */ *ap = v;
   2242 
   2243 #ifdef DDB
   2244 	vfs_vnode_print(ap->a_vp, 0, printf);
   2245 #endif
   2246 	lfs_dump_dinode(VTOI(ap->a_vp)->i_din.ffs1_din);
   2247 }
   2248 #endif
   2249 
   2250 int
   2251 lfs_mmap(void *v)
   2252 {
   2253 	struct vop_mmap_args /* {
   2254 		const struct vnodeop_desc *a_desc;
   2255 		struct vnode *a_vp;
   2256 		int a_fflags;
   2257 		kauth_cred_t a_cred;
   2258 		struct lwp *a_l;
   2259 	} */ *ap = v;
   2260 
   2261 	if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM)
   2262 		return EOPNOTSUPP;
   2263 	return ufs_mmap(v);
   2264 }
   2265