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