Home | History | Annotate | Line # | Download | only in lfs
lfs_vnops.c revision 1.203
      1 /*	$NetBSD: lfs_vnops.c,v 1.203 2007/04/17 01:16:47 perseant 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.203 2007/04/17 01:16:47 perseant 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 int lfs_ignore_lazy_sync = 1;
    112 
    113 /* Forward declaration for hackish genfs_putpages */
    114 static int
    115 lfs_genfs_putpages(struct vnode *, off_t, off_t, int, struct vm_page **);
    116 
    117 /* Global vfs data structures for lfs. */
    118 int (**lfs_vnodeop_p)(void *);
    119 const struct vnodeopv_entry_desc lfs_vnodeop_entries[] = {
    120 	{ &vop_default_desc, vn_default_error },
    121 	{ &vop_lookup_desc, ufs_lookup },		/* lookup */
    122 	{ &vop_create_desc, lfs_create },		/* create */
    123 	{ &vop_whiteout_desc, ufs_whiteout },		/* whiteout */
    124 	{ &vop_mknod_desc, lfs_mknod },			/* mknod */
    125 	{ &vop_open_desc, ufs_open },			/* open */
    126 	{ &vop_close_desc, lfs_close },			/* close */
    127 	{ &vop_access_desc, ufs_access },		/* access */
    128 	{ &vop_getattr_desc, lfs_getattr },		/* getattr */
    129 	{ &vop_setattr_desc, lfs_setattr },		/* setattr */
    130 	{ &vop_read_desc, lfs_read },			/* read */
    131 	{ &vop_write_desc, lfs_write },			/* write */
    132 	{ &vop_lease_desc, ufs_lease_check },		/* lease */
    133 	{ &vop_ioctl_desc, ufs_ioctl },			/* ioctl */
    134 	{ &vop_fcntl_desc, lfs_fcntl },			/* fcntl */
    135 	{ &vop_poll_desc, ufs_poll },			/* poll */
    136 	{ &vop_kqfilter_desc, genfs_kqfilter },		/* kqfilter */
    137 	{ &vop_revoke_desc, ufs_revoke },		/* revoke */
    138 	{ &vop_mmap_desc, lfs_mmap },			/* mmap */
    139 	{ &vop_fsync_desc, lfs_fsync },			/* fsync */
    140 	{ &vop_seek_desc, ufs_seek },			/* seek */
    141 	{ &vop_remove_desc, lfs_remove },		/* remove */
    142 	{ &vop_link_desc, lfs_link },			/* link */
    143 	{ &vop_rename_desc, lfs_rename },		/* rename */
    144 	{ &vop_mkdir_desc, lfs_mkdir },			/* mkdir */
    145 	{ &vop_rmdir_desc, lfs_rmdir },			/* rmdir */
    146 	{ &vop_symlink_desc, lfs_symlink },		/* symlink */
    147 	{ &vop_readdir_desc, ufs_readdir },		/* readdir */
    148 	{ &vop_readlink_desc, ufs_readlink },		/* readlink */
    149 	{ &vop_abortop_desc, ufs_abortop },		/* abortop */
    150 	{ &vop_inactive_desc, lfs_inactive },		/* inactive */
    151 	{ &vop_reclaim_desc, lfs_reclaim },		/* reclaim */
    152 	{ &vop_lock_desc, ufs_lock },			/* lock */
    153 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
    154 	{ &vop_bmap_desc, ufs_bmap },			/* bmap */
    155 	{ &vop_strategy_desc, lfs_strategy },		/* strategy */
    156 	{ &vop_print_desc, ufs_print },			/* print */
    157 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
    158 	{ &vop_pathconf_desc, ufs_pathconf },		/* pathconf */
    159 	{ &vop_advlock_desc, ufs_advlock },		/* advlock */
    160 	{ &vop_bwrite_desc, lfs_bwrite },		/* bwrite */
    161 	{ &vop_getpages_desc, lfs_getpages },		/* getpages */
    162 	{ &vop_putpages_desc, lfs_putpages },		/* putpages */
    163 	{ NULL, NULL }
    164 };
    165 const struct vnodeopv_desc lfs_vnodeop_opv_desc =
    166 	{ &lfs_vnodeop_p, lfs_vnodeop_entries };
    167 
    168 int (**lfs_specop_p)(void *);
    169 const struct vnodeopv_entry_desc lfs_specop_entries[] = {
    170 	{ &vop_default_desc, vn_default_error },
    171 	{ &vop_lookup_desc, spec_lookup },		/* lookup */
    172 	{ &vop_create_desc, spec_create },		/* create */
    173 	{ &vop_mknod_desc, spec_mknod },		/* mknod */
    174 	{ &vop_open_desc, spec_open },			/* open */
    175 	{ &vop_close_desc, lfsspec_close },		/* close */
    176 	{ &vop_access_desc, ufs_access },		/* access */
    177 	{ &vop_getattr_desc, lfs_getattr },		/* getattr */
    178 	{ &vop_setattr_desc, lfs_setattr },		/* setattr */
    179 	{ &vop_read_desc, ufsspec_read },		/* read */
    180 	{ &vop_write_desc, ufsspec_write },		/* write */
    181 	{ &vop_lease_desc, spec_lease_check },		/* lease */
    182 	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */
    183 	{ &vop_fcntl_desc, ufs_fcntl },			/* fcntl */
    184 	{ &vop_poll_desc, spec_poll },			/* poll */
    185 	{ &vop_kqfilter_desc, spec_kqfilter },		/* kqfilter */
    186 	{ &vop_revoke_desc, spec_revoke },		/* revoke */
    187 	{ &vop_mmap_desc, spec_mmap },			/* mmap */
    188 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
    189 	{ &vop_seek_desc, spec_seek },			/* seek */
    190 	{ &vop_remove_desc, spec_remove },		/* remove */
    191 	{ &vop_link_desc, spec_link },			/* link */
    192 	{ &vop_rename_desc, spec_rename },		/* rename */
    193 	{ &vop_mkdir_desc, spec_mkdir },		/* mkdir */
    194 	{ &vop_rmdir_desc, spec_rmdir },		/* rmdir */
    195 	{ &vop_symlink_desc, spec_symlink },		/* symlink */
    196 	{ &vop_readdir_desc, spec_readdir },		/* readdir */
    197 	{ &vop_readlink_desc, spec_readlink },		/* readlink */
    198 	{ &vop_abortop_desc, spec_abortop },		/* abortop */
    199 	{ &vop_inactive_desc, lfs_inactive },		/* inactive */
    200 	{ &vop_reclaim_desc, lfs_reclaim },		/* reclaim */
    201 	{ &vop_lock_desc, ufs_lock },			/* lock */
    202 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
    203 	{ &vop_bmap_desc, spec_bmap },			/* bmap */
    204 	{ &vop_strategy_desc, spec_strategy },		/* strategy */
    205 	{ &vop_print_desc, ufs_print },			/* print */
    206 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
    207 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
    208 	{ &vop_advlock_desc, spec_advlock },		/* advlock */
    209 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    210 	{ &vop_getpages_desc, spec_getpages },		/* getpages */
    211 	{ &vop_putpages_desc, spec_putpages },		/* putpages */
    212 	{ NULL, NULL }
    213 };
    214 const struct vnodeopv_desc lfs_specop_opv_desc =
    215 	{ &lfs_specop_p, lfs_specop_entries };
    216 
    217 int (**lfs_fifoop_p)(void *);
    218 const struct vnodeopv_entry_desc lfs_fifoop_entries[] = {
    219 	{ &vop_default_desc, vn_default_error },
    220 	{ &vop_lookup_desc, fifo_lookup },		/* lookup */
    221 	{ &vop_create_desc, fifo_create },		/* create */
    222 	{ &vop_mknod_desc, fifo_mknod },		/* mknod */
    223 	{ &vop_open_desc, fifo_open },			/* open */
    224 	{ &vop_close_desc, lfsfifo_close },		/* close */
    225 	{ &vop_access_desc, ufs_access },		/* access */
    226 	{ &vop_getattr_desc, lfs_getattr },		/* getattr */
    227 	{ &vop_setattr_desc, lfs_setattr },		/* setattr */
    228 	{ &vop_read_desc, ufsfifo_read },		/* read */
    229 	{ &vop_write_desc, ufsfifo_write },		/* write */
    230 	{ &vop_lease_desc, fifo_lease_check },		/* lease */
    231 	{ &vop_ioctl_desc, fifo_ioctl },		/* ioctl */
    232 	{ &vop_fcntl_desc, ufs_fcntl },			/* fcntl */
    233 	{ &vop_poll_desc, fifo_poll },			/* poll */
    234 	{ &vop_kqfilter_desc, fifo_kqfilter },		/* kqfilter */
    235 	{ &vop_revoke_desc, fifo_revoke },		/* revoke */
    236 	{ &vop_mmap_desc, fifo_mmap },			/* mmap */
    237 	{ &vop_fsync_desc, fifo_fsync },		/* fsync */
    238 	{ &vop_seek_desc, fifo_seek },			/* seek */
    239 	{ &vop_remove_desc, fifo_remove },		/* remove */
    240 	{ &vop_link_desc, fifo_link },			/* link */
    241 	{ &vop_rename_desc, fifo_rename },		/* rename */
    242 	{ &vop_mkdir_desc, fifo_mkdir },		/* mkdir */
    243 	{ &vop_rmdir_desc, fifo_rmdir },		/* rmdir */
    244 	{ &vop_symlink_desc, fifo_symlink },		/* symlink */
    245 	{ &vop_readdir_desc, fifo_readdir },		/* readdir */
    246 	{ &vop_readlink_desc, fifo_readlink },		/* readlink */
    247 	{ &vop_abortop_desc, fifo_abortop },		/* abortop */
    248 	{ &vop_inactive_desc, lfs_inactive },		/* inactive */
    249 	{ &vop_reclaim_desc, lfs_reclaim },		/* reclaim */
    250 	{ &vop_lock_desc, ufs_lock },			/* lock */
    251 	{ &vop_unlock_desc, ufs_unlock },		/* unlock */
    252 	{ &vop_bmap_desc, fifo_bmap },			/* bmap */
    253 	{ &vop_strategy_desc, fifo_strategy },		/* strategy */
    254 	{ &vop_print_desc, ufs_print },			/* print */
    255 	{ &vop_islocked_desc, ufs_islocked },		/* islocked */
    256 	{ &vop_pathconf_desc, fifo_pathconf },		/* pathconf */
    257 	{ &vop_advlock_desc, fifo_advlock },		/* advlock */
    258 	{ &vop_bwrite_desc, lfs_bwrite },		/* bwrite */
    259 	{ &vop_putpages_desc, fifo_putpages },		/* putpages */
    260 	{ NULL, NULL }
    261 };
    262 const struct vnodeopv_desc lfs_fifoop_opv_desc =
    263 	{ &lfs_fifoop_p, lfs_fifoop_entries };
    264 
    265 static int check_dirty(struct lfs *, struct vnode *, off_t, off_t, off_t, int, int, struct vm_page **);
    266 
    267 #define	LFS_READWRITE
    268 #include <ufs/ufs/ufs_readwrite.c>
    269 #undef	LFS_READWRITE
    270 
    271 /*
    272  * Synch an open file.
    273  */
    274 /* ARGSUSED */
    275 int
    276 lfs_fsync(void *v)
    277 {
    278 	struct vop_fsync_args /* {
    279 		struct vnode *a_vp;
    280 		kauth_cred_t a_cred;
    281 		int a_flags;
    282 		off_t offlo;
    283 		off_t offhi;
    284 		struct lwp *a_l;
    285 	} */ *ap = v;
    286 	struct vnode *vp = ap->a_vp;
    287 	int error, wait;
    288 	struct inode *ip = VTOI(vp);
    289 	struct lfs *fs = ip->i_lfs;
    290 
    291 	/* If we're mounted read-only, don't try to sync. */
    292 	if (fs->lfs_ronly)
    293 		return 0;
    294 
    295 	/*
    296 	 * Trickle sync simply adds this vnode to the pager list, as if
    297 	 * the pagedaemon had requested a pageout.
    298 	 */
    299 	if (ap->a_flags & FSYNC_LAZY) {
    300 		if (lfs_ignore_lazy_sync == 0) {
    301 			simple_lock(&fs->lfs_interlock);
    302 			if (!(ip->i_flags & IN_PAGING)) {
    303 				ip->i_flags |= IN_PAGING;
    304 				TAILQ_INSERT_TAIL(&fs->lfs_pchainhd, ip,
    305 						  i_lfs_pchain);
    306 			}
    307 			simple_unlock(&fs->lfs_interlock);
    308 			simple_lock(&lfs_subsys_lock);
    309 			wakeup(&lfs_writer_daemon);
    310 			simple_unlock(&lfs_subsys_lock);
    311 		}
    312 		return 0;
    313 	}
    314 
    315 	/*
    316 	 * If a vnode is bring cleaned, flush it out before we try to
    317 	 * reuse it.  This prevents the cleaner from writing files twice
    318 	 * in the same partial segment, causing an accounting underflow.
    319 	 */
    320 	if (ap->a_flags & FSYNC_RECLAIM && ip->i_flags & IN_CLEANING) {
    321 		lfs_vflush(vp);
    322 	}
    323 
    324 	wait = (ap->a_flags & FSYNC_WAIT);
    325 	simple_lock(&vp->v_interlock);
    326 	do {
    327 		error = VOP_PUTPAGES(vp, trunc_page(ap->a_offlo),
    328 				     round_page(ap->a_offhi),
    329 				     PGO_CLEANIT | (wait ? PGO_SYNCIO : 0));
    330 		if (error == EAGAIN)
    331 			ltsleep(&fs->lfs_avail, PCATCH | PUSER, "lfs_fsync",
    332 				hz / 100 + 1, &fs->lfs_interlock);
    333 	} while (error == EAGAIN);
    334 	if (error)
    335 		return error;
    336 
    337 	if ((ap->a_flags & FSYNC_DATAONLY) == 0)
    338 		error = lfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
    339 
    340 	if (error == 0 && ap->a_flags & FSYNC_CACHE) {
    341 		int l = 0;
    342 		error = VOP_IOCTL(ip->i_devvp, DIOCCACHESYNC, &l, FWRITE,
    343 				  ap->a_l->l_cred, ap->a_l);
    344 	}
    345 	if (wait && !VPISEMPTY(vp))
    346 		LFS_SET_UINO(ip, IN_MODIFIED);
    347 
    348 	return error;
    349 }
    350 
    351 /*
    352  * Take IN_ADIROP off, then call ufs_inactive.
    353  */
    354 int
    355 lfs_inactive(void *v)
    356 {
    357 	struct vop_inactive_args /* {
    358 		struct vnode *a_vp;
    359 		struct lwp *a_l;
    360 	} */ *ap = v;
    361 
    362 	KASSERT(VTOI(ap->a_vp)->i_nlink == VTOI(ap->a_vp)->i_ffs_effnlink);
    363 
    364 	lfs_unmark_vnode(ap->a_vp);
    365 
    366 	/*
    367 	 * The Ifile is only ever inactivated on unmount.
    368 	 * Streamline this process by not giving it more dirty blocks.
    369 	 */
    370 	if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM) {
    371 		LFS_CLR_UINO(VTOI(ap->a_vp), IN_ALLMOD);
    372 		VOP_UNLOCK(ap->a_vp, 0);
    373 		return 0;
    374 	}
    375 
    376 	return ufs_inactive(v);
    377 }
    378 
    379 /*
    380  * These macros are used to bracket UFS directory ops, so that we can
    381  * identify all the pages touched during directory ops which need to
    382  * be ordered and flushed atomically, so that they may be recovered.
    383  *
    384  * Because we have to mark nodes VDIROP in order to prevent
    385  * the cache from reclaiming them while a dirop is in progress, we must
    386  * also manage the number of nodes so marked (otherwise we can run out).
    387  * We do this by setting lfs_dirvcount to the number of marked vnodes; it
    388  * is decremented during segment write, when VDIROP is taken off.
    389  */
    390 #define	MARK_VNODE(vp)			lfs_mark_vnode(vp)
    391 #define	UNMARK_VNODE(vp)		lfs_unmark_vnode(vp)
    392 #define	SET_DIROP_CREATE(dvp, vpp)	lfs_set_dirop_create((dvp), (vpp))
    393 #define	SET_DIROP_REMOVE(dvp, vp)	lfs_set_dirop((dvp), (vp))
    394 static int lfs_set_dirop_create(struct vnode *, struct vnode **);
    395 static int lfs_set_dirop(struct vnode *, struct vnode *);
    396 
    397 static int
    398 lfs_set_dirop(struct vnode *dvp, struct vnode *vp)
    399 {
    400 	struct lfs *fs;
    401 	int error;
    402 
    403 	KASSERT(VOP_ISLOCKED(dvp));
    404 	KASSERT(vp == NULL || VOP_ISLOCKED(vp));
    405 
    406 	fs = VTOI(dvp)->i_lfs;
    407 
    408 	ASSERT_NO_SEGLOCK(fs);
    409 	/*
    410 	 * LFS_NRESERVE calculates direct and indirect blocks as well
    411 	 * as an inode block; an overestimate in most cases.
    412 	 */
    413 	if ((error = lfs_reserve(fs, dvp, vp, LFS_NRESERVE(fs))) != 0)
    414 		return (error);
    415 
    416   restart:
    417 	simple_lock(&fs->lfs_interlock);
    418 	if (fs->lfs_dirops == 0) {
    419 		simple_unlock(&fs->lfs_interlock);
    420 		lfs_check(dvp, LFS_UNUSED_LBN, 0);
    421 		simple_lock(&fs->lfs_interlock);
    422 	}
    423 	while (fs->lfs_writer) {
    424 		error = ltsleep(&fs->lfs_dirops, (PRIBIO + 1) | PCATCH,
    425 				"lfs_sdirop", 0, &fs->lfs_interlock);
    426 		if (error == EINTR) {
    427 			simple_unlock(&fs->lfs_interlock);
    428 			goto unreserve;
    429 		}
    430 	}
    431 	simple_lock(&lfs_subsys_lock);
    432 	if (lfs_dirvcount > LFS_MAX_DIROP && fs->lfs_dirops == 0) {
    433 		wakeup(&lfs_writer_daemon);
    434 		simple_unlock(&lfs_subsys_lock);
    435 		simple_unlock(&fs->lfs_interlock);
    436 		preempt();
    437 		goto restart;
    438 	}
    439 
    440 	if (lfs_dirvcount > LFS_MAX_DIROP) {
    441 		simple_unlock(&fs->lfs_interlock);
    442 		DLOG((DLOG_DIROP, "lfs_set_dirop: sleeping with dirops=%d, "
    443 		      "dirvcount=%d\n", fs->lfs_dirops, lfs_dirvcount));
    444 		if ((error = ltsleep(&lfs_dirvcount,
    445 				     PCATCH | PUSER | PNORELOCK, "lfs_maxdirop", 0,
    446 				     &lfs_subsys_lock)) != 0) {
    447 			goto unreserve;
    448 		}
    449 		goto restart;
    450 	}
    451 	simple_unlock(&lfs_subsys_lock);
    452 
    453 	++fs->lfs_dirops;
    454 	fs->lfs_doifile = 1;
    455 	simple_unlock(&fs->lfs_interlock);
    456 
    457 	/* Hold a reference so SET_ENDOP will be happy */
    458 	vref(dvp);
    459 	if (vp) {
    460 		vref(vp);
    461 		MARK_VNODE(vp);
    462 	}
    463 
    464 	MARK_VNODE(dvp);
    465 	return 0;
    466 
    467   unreserve:
    468 	lfs_reserve(fs, dvp, vp, -LFS_NRESERVE(fs));
    469 	return error;
    470 }
    471 
    472 /*
    473  * Get a new vnode *before* adjusting the dirop count, to avoid a deadlock
    474  * in getnewvnode(), if we have a stacked filesystem mounted on top
    475  * of us.
    476  *
    477  * NB: this means we have to clear the new vnodes on error.  Fortunately
    478  * SET_ENDOP is there to do that for us.
    479  */
    480 static int
    481 lfs_set_dirop_create(struct vnode *dvp, struct vnode **vpp)
    482 {
    483 	int error;
    484 	struct lfs *fs;
    485 
    486 	fs = VFSTOUFS(dvp->v_mount)->um_lfs;
    487 	ASSERT_NO_SEGLOCK(fs);
    488 	if (fs->lfs_ronly)
    489 		return EROFS;
    490 	if (vpp && (error = getnewvnode(VT_LFS, dvp->v_mount, lfs_vnodeop_p, vpp))) {
    491 		DLOG((DLOG_ALLOC, "lfs_set_dirop_create: dvp %p error %d\n",
    492 		      dvp, error));
    493 		return error;
    494 	}
    495 	if ((error = lfs_set_dirop(dvp, NULL)) != 0) {
    496 		if (vpp) {
    497 			ungetnewvnode(*vpp);
    498 			*vpp = NULL;
    499 		}
    500 		return error;
    501 	}
    502 	return 0;
    503 }
    504 
    505 #define	SET_ENDOP_BASE(fs, dvp, str)					\
    506 	do {								\
    507 		simple_lock(&(fs)->lfs_interlock);			\
    508 		--(fs)->lfs_dirops;					\
    509 		if (!(fs)->lfs_dirops) {				\
    510 			if ((fs)->lfs_nadirop) {			\
    511 				panic("SET_ENDOP: %s: no dirops but "	\
    512 					" nadirop=%d", (str),		\
    513 					(fs)->lfs_nadirop);		\
    514 			}						\
    515 			wakeup(&(fs)->lfs_writer);			\
    516 			simple_unlock(&(fs)->lfs_interlock);		\
    517 			lfs_check((dvp), LFS_UNUSED_LBN, 0);		\
    518 		} else							\
    519 			simple_unlock(&(fs)->lfs_interlock);		\
    520 	} while(0)
    521 #define SET_ENDOP_CREATE(fs, dvp, nvpp, str)				\
    522 	do {								\
    523 		UNMARK_VNODE(dvp);					\
    524 		if (nvpp && *nvpp)					\
    525 			UNMARK_VNODE(*nvpp);				\
    526 		/* Check for error return to stem vnode leakage */	\
    527 		if (nvpp && *nvpp && !((*nvpp)->v_flag & VDIROP))	\
    528 			ungetnewvnode(*(nvpp));				\
    529 		SET_ENDOP_BASE((fs), (dvp), (str));			\
    530 		lfs_reserve((fs), (dvp), NULL, -LFS_NRESERVE(fs));	\
    531 		vrele(dvp);						\
    532 	} while(0)
    533 #define SET_ENDOP_CREATE_AP(ap, str)					\
    534 	SET_ENDOP_CREATE(VTOI((ap)->a_dvp)->i_lfs, (ap)->a_dvp,		\
    535 			 (ap)->a_vpp, (str))
    536 #define SET_ENDOP_REMOVE(fs, dvp, ovp, str)				\
    537 	do {								\
    538 		UNMARK_VNODE(dvp);					\
    539 		if (ovp)						\
    540 			UNMARK_VNODE(ovp);				\
    541 		SET_ENDOP_BASE((fs), (dvp), (str));			\
    542 		lfs_reserve((fs), (dvp), (ovp), -LFS_NRESERVE(fs));	\
    543 		vrele(dvp);						\
    544 		if (ovp)						\
    545 			vrele(ovp);					\
    546 	} while(0)
    547 
    548 void
    549 lfs_mark_vnode(struct vnode *vp)
    550 {
    551 	struct inode *ip = VTOI(vp);
    552 	struct lfs *fs = ip->i_lfs;
    553 
    554 	simple_lock(&fs->lfs_interlock);
    555 	if (!(ip->i_flag & IN_ADIROP)) {
    556 		if (!(vp->v_flag & VDIROP)) {
    557 			(void)lfs_vref(vp);
    558 			simple_lock(&lfs_subsys_lock);
    559 			++lfs_dirvcount;
    560 			++fs->lfs_dirvcount;
    561 			simple_unlock(&lfs_subsys_lock);
    562 			TAILQ_INSERT_TAIL(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    563 			vp->v_flag |= VDIROP;
    564 		}
    565 		++fs->lfs_nadirop;
    566 		ip->i_flag |= IN_ADIROP;
    567 	} else
    568 		KASSERT(vp->v_flag & VDIROP);
    569 	simple_unlock(&fs->lfs_interlock);
    570 }
    571 
    572 void
    573 lfs_unmark_vnode(struct vnode *vp)
    574 {
    575 	struct inode *ip = VTOI(vp);
    576 
    577 	if (ip && (ip->i_flag & IN_ADIROP)) {
    578 		KASSERT(vp->v_flag & VDIROP);
    579 		simple_lock(&ip->i_lfs->lfs_interlock);
    580 		--ip->i_lfs->lfs_nadirop;
    581 		simple_unlock(&ip->i_lfs->lfs_interlock);
    582 		ip->i_flag &= ~IN_ADIROP;
    583 	}
    584 }
    585 
    586 int
    587 lfs_symlink(void *v)
    588 {
    589 	struct vop_symlink_args /* {
    590 		struct vnode *a_dvp;
    591 		struct vnode **a_vpp;
    592 		struct componentname *a_cnp;
    593 		struct vattr *a_vap;
    594 		char *a_target;
    595 	} */ *ap = v;
    596 	int error;
    597 
    598 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    599 		vput(ap->a_dvp);
    600 		return error;
    601 	}
    602 	error = ufs_symlink(ap);
    603 	SET_ENDOP_CREATE_AP(ap, "symlink");
    604 	return (error);
    605 }
    606 
    607 int
    608 lfs_mknod(void *v)
    609 {
    610 	struct vop_mknod_args	/* {
    611 		struct vnode *a_dvp;
    612 		struct vnode **a_vpp;
    613 		struct componentname *a_cnp;
    614 		struct vattr *a_vap;
    615 	} */ *ap = v;
    616 	struct vattr *vap = ap->a_vap;
    617 	struct vnode **vpp = ap->a_vpp;
    618 	struct inode *ip;
    619 	int error;
    620 	struct mount	*mp;
    621 	ino_t		ino;
    622 
    623 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    624 		vput(ap->a_dvp);
    625 		return error;
    626 	}
    627 	error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
    628 			      ap->a_dvp, vpp, ap->a_cnp);
    629 
    630 	/* Either way we're done with the dirop at this point */
    631 	SET_ENDOP_CREATE_AP(ap, "mknod");
    632 
    633 	if (error)
    634 		return (error);
    635 
    636 	ip = VTOI(*vpp);
    637 	mp  = (*vpp)->v_mount;
    638 	ino = ip->i_number;
    639 	ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
    640 	if (vap->va_rdev != VNOVAL) {
    641 		/*
    642 		 * Want to be able to use this to make badblock
    643 		 * inodes, so don't truncate the dev number.
    644 		 */
    645 #if 0
    646 		ip->i_ffs1_rdev = ufs_rw32(vap->va_rdev,
    647 					   UFS_MPNEEDSWAP((*vpp)->v_mount));
    648 #else
    649 		ip->i_ffs1_rdev = vap->va_rdev;
    650 #endif
    651 	}
    652 
    653 	/*
    654 	 * Call fsync to write the vnode so that we don't have to deal with
    655 	 * flushing it when it's marked VDIROP|VXLOCK.
    656 	 *
    657 	 * XXX KS - If we can't flush we also can't call vgone(), so must
    658 	 * return.  But, that leaves this vnode in limbo, also not good.
    659 	 * Can this ever happen (barring hardware failure)?
    660 	 */
    661 	if ((error = VOP_FSYNC(*vpp, NOCRED, FSYNC_WAIT, 0, 0,
    662 			       curlwp)) != 0) {
    663 		panic("lfs_mknod: couldn't fsync (ino %llu)",
    664 		      (unsigned long long)ino);
    665 		/* return (error); */
    666 	}
    667 	/*
    668 	 * Remove vnode so that it will be reloaded by VFS_VGET and
    669 	 * checked to see if it is an alias of an existing entry in
    670 	 * the inode cache.
    671 	 */
    672 	/* Used to be vput, but that causes us to call VOP_INACTIVE twice. */
    673 
    674 	VOP_UNLOCK(*vpp, 0);
    675 	lfs_vunref(*vpp);
    676 	(*vpp)->v_type = VNON;
    677 	vgone(*vpp);
    678 	error = VFS_VGET(mp, ino, vpp);
    679 
    680 	if (error != 0) {
    681 		*vpp = NULL;
    682 		return (error);
    683 	}
    684 	return (0);
    685 }
    686 
    687 int
    688 lfs_create(void *v)
    689 {
    690 	struct vop_create_args	/* {
    691 		struct vnode *a_dvp;
    692 		struct vnode **a_vpp;
    693 		struct componentname *a_cnp;
    694 		struct vattr *a_vap;
    695 	} */ *ap = v;
    696 	int error;
    697 
    698 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    699 		vput(ap->a_dvp);
    700 		return error;
    701 	}
    702 	error = ufs_create(ap);
    703 	SET_ENDOP_CREATE_AP(ap, "create");
    704 	return (error);
    705 }
    706 
    707 int
    708 lfs_mkdir(void *v)
    709 {
    710 	struct vop_mkdir_args	/* {
    711 		struct vnode *a_dvp;
    712 		struct vnode **a_vpp;
    713 		struct componentname *a_cnp;
    714 		struct vattr *a_vap;
    715 	} */ *ap = v;
    716 	int error;
    717 
    718 	if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
    719 		vput(ap->a_dvp);
    720 		return error;
    721 	}
    722 	error = ufs_mkdir(ap);
    723 	SET_ENDOP_CREATE_AP(ap, "mkdir");
    724 	return (error);
    725 }
    726 
    727 int
    728 lfs_remove(void *v)
    729 {
    730 	struct vop_remove_args	/* {
    731 		struct vnode *a_dvp;
    732 		struct vnode *a_vp;
    733 		struct componentname *a_cnp;
    734 	} */ *ap = v;
    735 	struct vnode *dvp, *vp;
    736 	struct inode *ip;
    737 	int error;
    738 
    739 	dvp = ap->a_dvp;
    740 	vp = ap->a_vp;
    741 	ip = VTOI(vp);
    742 	if ((error = SET_DIROP_REMOVE(dvp, vp)) != 0) {
    743 		if (dvp == vp)
    744 			vrele(vp);
    745 		else
    746 			vput(vp);
    747 		vput(dvp);
    748 		return error;
    749 	}
    750 	error = ufs_remove(ap);
    751 	if (ip->i_nlink == 0)
    752 		lfs_orphan(ip->i_lfs, ip->i_number);
    753 	SET_ENDOP_REMOVE(ip->i_lfs, dvp, ap->a_vp, "remove");
    754 	return (error);
    755 }
    756 
    757 int
    758 lfs_rmdir(void *v)
    759 {
    760 	struct vop_rmdir_args	/* {
    761 		struct vnodeop_desc *a_desc;
    762 		struct vnode *a_dvp;
    763 		struct vnode *a_vp;
    764 		struct componentname *a_cnp;
    765 	} */ *ap = v;
    766 	struct vnode *vp;
    767 	struct inode *ip;
    768 	int error;
    769 
    770 	vp = ap->a_vp;
    771 	ip = VTOI(vp);
    772 	if ((error = SET_DIROP_REMOVE(ap->a_dvp, ap->a_vp)) != 0) {
    773 		if (ap->a_dvp == vp)
    774 			vrele(ap->a_dvp);
    775 		else
    776 			vput(ap->a_dvp);
    777 		vput(vp);
    778 		return error;
    779 	}
    780 	error = ufs_rmdir(ap);
    781 	if (ip->i_nlink == 0)
    782 		lfs_orphan(ip->i_lfs, ip->i_number);
    783 	SET_ENDOP_REMOVE(ip->i_lfs, ap->a_dvp, ap->a_vp, "rmdir");
    784 	return (error);
    785 }
    786 
    787 int
    788 lfs_link(void *v)
    789 {
    790 	struct vop_link_args	/* {
    791 		struct vnode *a_dvp;
    792 		struct vnode *a_vp;
    793 		struct componentname *a_cnp;
    794 	} */ *ap = v;
    795 	int error;
    796 	struct vnode **vpp = NULL;
    797 
    798 	if ((error = SET_DIROP_CREATE(ap->a_dvp, vpp)) != 0) {
    799 		vput(ap->a_dvp);
    800 		return error;
    801 	}
    802 	error = ufs_link(ap);
    803 	SET_ENDOP_CREATE(VTOI(ap->a_dvp)->i_lfs, ap->a_dvp, vpp, "link");
    804 	return (error);
    805 }
    806 
    807 int
    808 lfs_rename(void *v)
    809 {
    810 	struct vop_rename_args	/* {
    811 		struct vnode *a_fdvp;
    812 		struct vnode *a_fvp;
    813 		struct componentname *a_fcnp;
    814 		struct vnode *a_tdvp;
    815 		struct vnode *a_tvp;
    816 		struct componentname *a_tcnp;
    817 	} */ *ap = v;
    818 	struct vnode *tvp, *fvp, *tdvp, *fdvp;
    819 	struct componentname *tcnp, *fcnp;
    820 	int error;
    821 	struct lfs *fs;
    822 
    823 	fs = VTOI(ap->a_fdvp)->i_lfs;
    824 	tvp = ap->a_tvp;
    825 	tdvp = ap->a_tdvp;
    826 	tcnp = ap->a_tcnp;
    827 	fvp = ap->a_fvp;
    828 	fdvp = ap->a_fdvp;
    829 	fcnp = ap->a_fcnp;
    830 
    831 	/*
    832 	 * Check for cross-device rename.
    833 	 * If it is, we don't want to set dirops, just error out.
    834 	 * (In particular note that MARK_VNODE(tdvp) will DTWT on
    835 	 * a cross-device rename.)
    836 	 *
    837 	 * Copied from ufs_rename.
    838 	 */
    839 	if ((fvp->v_mount != tdvp->v_mount) ||
    840 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
    841 		error = EXDEV;
    842 		goto errout;
    843 	}
    844 
    845 	/*
    846 	 * Check to make sure we're not renaming a vnode onto itself
    847 	 * (deleting a hard link by renaming one name onto another);
    848 	 * if we are we can't recursively call VOP_REMOVE since that
    849 	 * would leave us with an unaccounted-for number of live dirops.
    850 	 *
    851 	 * Inline the relevant section of ufs_rename here, *before*
    852 	 * calling SET_DIROP_REMOVE.
    853 	 */
    854 	if (tvp && ((VTOI(tvp)->i_flags & (IMMUTABLE | APPEND)) ||
    855 		    (VTOI(tdvp)->i_flags & APPEND))) {
    856 		error = EPERM;
    857 		goto errout;
    858 	}
    859 	if (fvp == tvp) {
    860 		if (fvp->v_type == VDIR) {
    861 			error = EINVAL;
    862 			goto errout;
    863 		}
    864 
    865 		/* Release destination completely. */
    866 		VOP_ABORTOP(tdvp, tcnp);
    867 		vput(tdvp);
    868 		vput(tvp);
    869 
    870 		/* Delete source. */
    871 		vrele(fvp);
    872 		fcnp->cn_flags &= ~(MODMASK | SAVESTART);
    873 		fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
    874 		fcnp->cn_nameiop = DELETE;
    875 		vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
    876 		if ((error = relookup(fdvp, &fvp, fcnp))) {
    877 			vput(fdvp);
    878 			return (error);
    879 		}
    880 		return (VOP_REMOVE(fdvp, fvp, fcnp));
    881 	}
    882 
    883 	if ((error = SET_DIROP_REMOVE(tdvp, tvp)) != 0)
    884 		goto errout;
    885 	MARK_VNODE(fdvp);
    886 	MARK_VNODE(fvp);
    887 
    888 	error = ufs_rename(ap);
    889 	UNMARK_VNODE(fdvp);
    890 	UNMARK_VNODE(fvp);
    891 	SET_ENDOP_REMOVE(fs, tdvp, tvp, "rename");
    892 	return (error);
    893 
    894   errout:
    895 	VOP_ABORTOP(tdvp, ap->a_tcnp); /* XXX, why not in NFS? */
    896 	if (tdvp == tvp)
    897 		vrele(tdvp);
    898 	else
    899 		vput(tdvp);
    900 	if (tvp)
    901 		vput(tvp);
    902 	VOP_ABORTOP(fdvp, ap->a_fcnp); /* XXX, why not in NFS? */
    903 	vrele(fdvp);
    904 	vrele(fvp);
    905 	return (error);
    906 }
    907 
    908 /* XXX hack to avoid calling ITIMES in getattr */
    909 int
    910 lfs_getattr(void *v)
    911 {
    912 	struct vop_getattr_args /* {
    913 		struct vnode *a_vp;
    914 		struct vattr *a_vap;
    915 		kauth_cred_t a_cred;
    916 		struct lwp *a_l;
    917 	} */ *ap = v;
    918 	struct vnode *vp = ap->a_vp;
    919 	struct inode *ip = VTOI(vp);
    920 	struct vattr *vap = ap->a_vap;
    921 	struct lfs *fs = ip->i_lfs;
    922 	/*
    923 	 * Copy from inode table
    924 	 */
    925 	vap->va_fsid = ip->i_dev;
    926 	vap->va_fileid = ip->i_number;
    927 	vap->va_mode = ip->i_mode & ~IFMT;
    928 	vap->va_nlink = ip->i_nlink;
    929 	vap->va_uid = ip->i_uid;
    930 	vap->va_gid = ip->i_gid;
    931 	vap->va_rdev = (dev_t)ip->i_ffs1_rdev;
    932 	vap->va_size = vp->v_size;
    933 	vap->va_atime.tv_sec = ip->i_ffs1_atime;
    934 	vap->va_atime.tv_nsec = ip->i_ffs1_atimensec;
    935 	vap->va_mtime.tv_sec = ip->i_ffs1_mtime;
    936 	vap->va_mtime.tv_nsec = ip->i_ffs1_mtimensec;
    937 	vap->va_ctime.tv_sec = ip->i_ffs1_ctime;
    938 	vap->va_ctime.tv_nsec = ip->i_ffs1_ctimensec;
    939 	vap->va_flags = ip->i_flags;
    940 	vap->va_gen = ip->i_gen;
    941 	/* this doesn't belong here */
    942 	if (vp->v_type == VBLK)
    943 		vap->va_blocksize = BLKDEV_IOSIZE;
    944 	else if (vp->v_type == VCHR)
    945 		vap->va_blocksize = MAXBSIZE;
    946 	else
    947 		vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
    948 	vap->va_bytes = fsbtob(fs, (u_quad_t)ip->i_lfs_effnblks);
    949 	vap->va_type = vp->v_type;
    950 	vap->va_filerev = ip->i_modrev;
    951 	return (0);
    952 }
    953 
    954 /*
    955  * Check to make sure the inode blocks won't choke the buffer
    956  * cache, then call ufs_setattr as usual.
    957  */
    958 int
    959 lfs_setattr(void *v)
    960 {
    961 	struct vop_setattr_args /* {
    962 		struct vnode *a_vp;
    963 		struct vattr *a_vap;
    964 		kauth_cred_t a_cred;
    965 		struct lwp *a_l;
    966 	} */ *ap = v;
    967 	struct vnode *vp = ap->a_vp;
    968 
    969 	lfs_check(vp, LFS_UNUSED_LBN, 0);
    970 	return ufs_setattr(v);
    971 }
    972 
    973 /*
    974  * Release the block we hold on lfs_newseg wrapping.  Called on file close,
    975  * or explicitly from LFCNWRAPGO.  Called with the interlock held.
    976  */
    977 static int
    978 lfs_wrapgo(struct lfs *fs, struct inode *ip, int waitfor)
    979 {
    980 	if (lockstatus(&fs->lfs_stoplock) != LK_EXCLUSIVE)
    981 		return EBUSY;
    982 
    983 	lockmgr(&fs->lfs_stoplock, LK_RELEASE, &fs->lfs_interlock);
    984 
    985 	KASSERT(fs->lfs_nowrap > 0);
    986 	if (fs->lfs_nowrap <= 0) {
    987 		return 0;
    988 	}
    989 
    990 	if (--fs->lfs_nowrap == 0) {
    991 		log(LOG_NOTICE, "%s: re-enabled log wrap\n", fs->lfs_fsmnt);
    992 		wakeup(&fs->lfs_wrappass);
    993 		lfs_wakeup_cleaner(fs);
    994 	}
    995 	if (waitfor) {
    996 		ltsleep(&fs->lfs_nextseg, PCATCH | PUSER,
    997 			"segment", 0, &fs->lfs_interlock);
    998 	}
    999 
   1000 	return 0;
   1001 }
   1002 
   1003 /*
   1004  * Close called
   1005  */
   1006 /* ARGSUSED */
   1007 int
   1008 lfs_close(void *v)
   1009 {
   1010 	struct vop_close_args /* {
   1011 		struct vnode *a_vp;
   1012 		int  a_fflag;
   1013 		kauth_cred_t a_cred;
   1014 		struct lwp *a_l;
   1015 	} */ *ap = v;
   1016 	struct vnode *vp = ap->a_vp;
   1017 	struct inode *ip = VTOI(vp);
   1018 	struct lfs *fs = ip->i_lfs;
   1019 
   1020 	if ((ip->i_number == ROOTINO || ip->i_number == LFS_IFILE_INUM) &&
   1021 	    lockstatus(&fs->lfs_stoplock) == LK_EXCLUSIVE) {
   1022 		simple_lock(&fs->lfs_interlock);
   1023 		log(LOG_NOTICE, "lfs_close: releasing log wrap control\n");
   1024 		lfs_wrapgo(fs, ip, 0);
   1025 		simple_unlock(&fs->lfs_interlock);
   1026 	}
   1027 
   1028 	if (vp == ip->i_lfs->lfs_ivnode &&
   1029 	    vp->v_mount->mnt_iflag & IMNT_UNMOUNT)
   1030 		return 0;
   1031 
   1032 	if (vp->v_usecount > 1 && vp != ip->i_lfs->lfs_ivnode) {
   1033 		LFS_ITIMES(ip, NULL, NULL, NULL);
   1034 	}
   1035 	return (0);
   1036 }
   1037 
   1038 /*
   1039  * Close wrapper for special devices.
   1040  *
   1041  * Update the times on the inode then do device close.
   1042  */
   1043 int
   1044 lfsspec_close(void *v)
   1045 {
   1046 	struct vop_close_args /* {
   1047 		struct vnode	*a_vp;
   1048 		int		a_fflag;
   1049 		kauth_cred_t	a_cred;
   1050 		struct lwp	*a_l;
   1051 	} */ *ap = v;
   1052 	struct vnode	*vp;
   1053 	struct inode	*ip;
   1054 
   1055 	vp = ap->a_vp;
   1056 	ip = VTOI(vp);
   1057 	if (vp->v_usecount > 1) {
   1058 		LFS_ITIMES(ip, NULL, NULL, NULL);
   1059 	}
   1060 	return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
   1061 }
   1062 
   1063 /*
   1064  * Close wrapper for fifo's.
   1065  *
   1066  * Update the times on the inode then do device close.
   1067  */
   1068 int
   1069 lfsfifo_close(void *v)
   1070 {
   1071 	struct vop_close_args /* {
   1072 		struct vnode	*a_vp;
   1073 		int		a_fflag;
   1074 		kauth_cred_	a_cred;
   1075 		struct lwp	*a_l;
   1076 	} */ *ap = v;
   1077 	struct vnode	*vp;
   1078 	struct inode	*ip;
   1079 
   1080 	vp = ap->a_vp;
   1081 	ip = VTOI(vp);
   1082 	if (ap->a_vp->v_usecount > 1) {
   1083 		LFS_ITIMES(ip, NULL, NULL, NULL);
   1084 	}
   1085 	return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
   1086 }
   1087 
   1088 /*
   1089  * Reclaim an inode so that it can be used for other purposes.
   1090  */
   1091 
   1092 int
   1093 lfs_reclaim(void *v)
   1094 {
   1095 	struct vop_reclaim_args /* {
   1096 		struct vnode *a_vp;
   1097 		struct lwp *a_l;
   1098 	} */ *ap = v;
   1099 	struct vnode *vp = ap->a_vp;
   1100 	struct inode *ip = VTOI(vp);
   1101 	struct lfs *fs = ip->i_lfs;
   1102 	int error;
   1103 
   1104 	KASSERT(ip->i_nlink == ip->i_ffs_effnlink);
   1105 
   1106 	LFS_CLR_UINO(ip, IN_ALLMOD);
   1107 	if ((error = ufs_reclaim(vp, ap->a_l)))
   1108 		return (error);
   1109 
   1110 	/*
   1111 	 * Take us off the paging and/or dirop queues if we were on them.
   1112 	 * We shouldn't be on them.
   1113 	 */
   1114 	simple_lock(&fs->lfs_interlock);
   1115 	if (ip->i_flags & IN_PAGING) {
   1116 		log(LOG_WARNING, "%s: reclaimed vnode is IN_PAGING\n",
   1117 		    fs->lfs_fsmnt);
   1118 		ip->i_flags &= ~IN_PAGING;
   1119 		TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
   1120 	}
   1121 	if (vp->v_flag & VDIROP) {
   1122 		panic("reclaimed vnode is VDIROP");
   1123 		vp->v_flag &= ~VDIROP;
   1124 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
   1125 	}
   1126 	simple_unlock(&fs->lfs_interlock);
   1127 
   1128 	pool_put(&lfs_dinode_pool, ip->i_din.ffs1_din);
   1129 	lfs_deregister_all(vp);
   1130 	pool_put(&lfs_inoext_pool, ip->inode_ext.lfs);
   1131 	ip->inode_ext.lfs = NULL;
   1132 	genfs_node_destroy(vp);
   1133 	pool_put(&lfs_inode_pool, vp->v_data);
   1134 	vp->v_data = NULL;
   1135 	return (0);
   1136 }
   1137 
   1138 /*
   1139  * Read a block from a storage device.
   1140  * In order to avoid reading blocks that are in the process of being
   1141  * written by the cleaner---and hence are not mutexed by the normal
   1142  * buffer cache / page cache mechanisms---check for collisions before
   1143  * reading.
   1144  *
   1145  * We inline ufs_strategy to make sure that the VOP_BMAP occurs *before*
   1146  * the active cleaner test.
   1147  *
   1148  * XXX This code assumes that lfs_markv makes synchronous checkpoints.
   1149  */
   1150 int
   1151 lfs_strategy(void *v)
   1152 {
   1153 	struct vop_strategy_args /* {
   1154 		struct vnode *a_vp;
   1155 		struct buf *a_bp;
   1156 	} */ *ap = v;
   1157 	struct buf	*bp;
   1158 	struct lfs	*fs;
   1159 	struct vnode	*vp;
   1160 	struct inode	*ip;
   1161 	daddr_t		tbn;
   1162 	int		i, sn, error, slept;
   1163 
   1164 	bp = ap->a_bp;
   1165 	vp = ap->a_vp;
   1166 	ip = VTOI(vp);
   1167 	fs = ip->i_lfs;
   1168 
   1169 	/* lfs uses its strategy routine only for read */
   1170 	KASSERT(bp->b_flags & B_READ);
   1171 
   1172 	if (vp->v_type == VBLK || vp->v_type == VCHR)
   1173 		panic("lfs_strategy: spec");
   1174 	KASSERT(bp->b_bcount != 0);
   1175 	if (bp->b_blkno == bp->b_lblkno) {
   1176 		error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
   1177 				 NULL);
   1178 		if (error) {
   1179 			bp->b_error = error;
   1180 			bp->b_flags |= B_ERROR;
   1181 			biodone(bp);
   1182 			return (error);
   1183 		}
   1184 		if ((long)bp->b_blkno == -1) /* no valid data */
   1185 			clrbuf(bp);
   1186 	}
   1187 	if ((long)bp->b_blkno < 0) { /* block is not on disk */
   1188 		biodone(bp);
   1189 		return (0);
   1190 	}
   1191 
   1192 	slept = 1;
   1193 	simple_lock(&fs->lfs_interlock);
   1194 	while (slept && fs->lfs_seglock) {
   1195 		simple_unlock(&fs->lfs_interlock);
   1196 		/*
   1197 		 * Look through list of intervals.
   1198 		 * There will only be intervals to look through
   1199 		 * if the cleaner holds the seglock.
   1200 		 * Since the cleaner is synchronous, we can trust
   1201 		 * the list of intervals to be current.
   1202 		 */
   1203 		tbn = dbtofsb(fs, bp->b_blkno);
   1204 		sn = dtosn(fs, tbn);
   1205 		slept = 0;
   1206 		for (i = 0; i < fs->lfs_cleanind; i++) {
   1207 			if (sn == dtosn(fs, fs->lfs_cleanint[i]) &&
   1208 			    tbn >= fs->lfs_cleanint[i]) {
   1209 				DLOG((DLOG_CLEAN,
   1210 				      "lfs_strategy: ino %d lbn %" PRId64
   1211 				      " ind %d sn %d fsb %" PRIx32
   1212 				      " given sn %d fsb %" PRIx64 "\n",
   1213 				      ip->i_number, bp->b_lblkno, i,
   1214 				      dtosn(fs, fs->lfs_cleanint[i]),
   1215 				      fs->lfs_cleanint[i], sn, tbn));
   1216 				DLOG((DLOG_CLEAN,
   1217 				      "lfs_strategy: sleeping on ino %d lbn %"
   1218 				      PRId64 "\n", ip->i_number, bp->b_lblkno));
   1219 				simple_lock(&fs->lfs_interlock);
   1220 				if (LFS_SEGLOCK_HELD(fs) && fs->lfs_iocount) {
   1221 					/* Cleaner can't wait for itself */
   1222 					ltsleep(&fs->lfs_iocount,
   1223 						(PRIBIO + 1) | PNORELOCK,
   1224 						"clean2", 0,
   1225 						&fs->lfs_interlock);
   1226 					slept = 1;
   1227 					break;
   1228 				} else if (fs->lfs_seglock) {
   1229 					ltsleep(&fs->lfs_seglock,
   1230 						(PRIBIO + 1) | PNORELOCK,
   1231 						"clean1", 0,
   1232 						&fs->lfs_interlock);
   1233 					slept = 1;
   1234 					break;
   1235 				}
   1236 				simple_unlock(&fs->lfs_interlock);
   1237 			}
   1238 		}
   1239 		simple_lock(&fs->lfs_interlock);
   1240 	}
   1241 	simple_unlock(&fs->lfs_interlock);
   1242 
   1243 	vp = ip->i_devvp;
   1244 	VOP_STRATEGY(vp, bp);
   1245 	return (0);
   1246 }
   1247 
   1248 void
   1249 lfs_flush_dirops(struct lfs *fs)
   1250 {
   1251 	struct inode *ip, *nip;
   1252 	struct vnode *vp;
   1253 	extern int lfs_dostats;
   1254 	struct segment *sp;
   1255 	int waslocked;
   1256 
   1257 	ASSERT_MAYBE_SEGLOCK(fs);
   1258 	KASSERT(fs->lfs_nadirop == 0);
   1259 
   1260 	if (fs->lfs_ronly)
   1261 		return;
   1262 
   1263 	simple_lock(&fs->lfs_interlock);
   1264 	if (TAILQ_FIRST(&fs->lfs_dchainhd) == NULL) {
   1265 		simple_unlock(&fs->lfs_interlock);
   1266 		return;
   1267 	} else
   1268 		simple_unlock(&fs->lfs_interlock);
   1269 
   1270 	if (lfs_dostats)
   1271 		++lfs_stats.flush_invoked;
   1272 
   1273 	/*
   1274 	 * Inline lfs_segwrite/lfs_writevnodes, but just for dirops.
   1275 	 * Technically this is a checkpoint (the on-disk state is valid)
   1276 	 * even though we are leaving out all the file data.
   1277 	 */
   1278 	lfs_imtime(fs);
   1279 	lfs_seglock(fs, SEGM_CKP);
   1280 	sp = fs->lfs_sp;
   1281 
   1282 	/*
   1283 	 * lfs_writevnodes, optimized to get dirops out of the way.
   1284 	 * Only write dirops, and don't flush files' pages, only
   1285 	 * blocks from the directories.
   1286 	 *
   1287 	 * We don't need to vref these files because they are
   1288 	 * dirops and so hold an extra reference until the
   1289 	 * segunlock clears them of that status.
   1290 	 *
   1291 	 * We don't need to check for IN_ADIROP because we know that
   1292 	 * no dirops are active.
   1293 	 *
   1294 	 */
   1295 	simple_lock(&fs->lfs_interlock);
   1296 	for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
   1297 		nip = TAILQ_NEXT(ip, i_lfs_dchain);
   1298 		simple_unlock(&fs->lfs_interlock);
   1299 		vp = ITOV(ip);
   1300 
   1301 		KASSERT((ip->i_flag & IN_ADIROP) == 0);
   1302 
   1303 		/*
   1304 		 * All writes to directories come from dirops; all
   1305 		 * writes to files' direct blocks go through the page
   1306 		 * cache, which we're not touching.  Reads to files
   1307 		 * and/or directories will not be affected by writing
   1308 		 * directory blocks inodes and file inodes.  So we don't
   1309 		 * really need to lock.	 If we don't lock, though,
   1310 		 * make sure that we don't clear IN_MODIFIED
   1311 		 * unnecessarily.
   1312 		 */
   1313 		if (vp->v_flag & (VXLOCK | VFREEING)) {
   1314 			simple_lock(&fs->lfs_interlock);
   1315 			continue;
   1316 		}
   1317 		waslocked = VOP_ISLOCKED(vp);
   1318 		if (vp->v_type != VREG &&
   1319 		    ((ip->i_flag & IN_ALLMOD) || !VPISEMPTY(vp))) {
   1320 			lfs_writefile(fs, sp, vp);
   1321 			if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
   1322 			    !(ip->i_flag & IN_ALLMOD)) {
   1323 				LFS_SET_UINO(ip, IN_MODIFIED);
   1324 			}
   1325 		}
   1326 		KDASSERT(ip->i_number != LFS_IFILE_INUM);
   1327 		(void) lfs_writeinode(fs, sp, ip);
   1328 		if (waslocked == LK_EXCLOTHER)
   1329 			LFS_SET_UINO(ip, IN_MODIFIED);
   1330 		simple_lock(&fs->lfs_interlock);
   1331 	}
   1332 	simple_unlock(&fs->lfs_interlock);
   1333 	/* We've written all the dirops there are */
   1334 	((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
   1335 	lfs_finalize_fs_seguse(fs);
   1336 	(void) lfs_writeseg(fs, sp);
   1337 	lfs_segunlock(fs);
   1338 }
   1339 
   1340 /*
   1341  * Flush all vnodes for which the pagedaemon has requested pageouts.
   1342  * Skip over any files that are marked VDIROP (since lfs_flush_dirop()
   1343  * has just run, this would be an error).  If we have to skip a vnode
   1344  * for any reason, just skip it; if we have to wait for the cleaner,
   1345  * abort.  The writer daemon will call us again later.
   1346  */
   1347 void
   1348 lfs_flush_pchain(struct lfs *fs)
   1349 {
   1350 	struct inode *ip, *nip;
   1351 	struct vnode *vp;
   1352 	extern int lfs_dostats;
   1353 	struct segment *sp;
   1354 	int error;
   1355 
   1356 	ASSERT_NO_SEGLOCK(fs);
   1357 
   1358 	if (fs->lfs_ronly)
   1359 		return;
   1360 
   1361 	simple_lock(&fs->lfs_interlock);
   1362 	if (TAILQ_FIRST(&fs->lfs_pchainhd) == NULL) {
   1363 		simple_unlock(&fs->lfs_interlock);
   1364 		return;
   1365 	} else
   1366 		simple_unlock(&fs->lfs_interlock);
   1367 
   1368 	/* Get dirops out of the way */
   1369 	lfs_flush_dirops(fs);
   1370 
   1371 	if (lfs_dostats)
   1372 		++lfs_stats.flush_invoked;
   1373 
   1374 	/*
   1375 	 * Inline lfs_segwrite/lfs_writevnodes, but just for pageouts.
   1376 	 */
   1377 	lfs_imtime(fs);
   1378 	lfs_seglock(fs, 0);
   1379 	sp = fs->lfs_sp;
   1380 
   1381 	/*
   1382 	 * lfs_writevnodes, optimized to clear pageout requests.
   1383 	 * Only write non-dirop files that are in the pageout queue.
   1384 	 * We're very conservative about what we write; we want to be
   1385 	 * fast and async.
   1386 	 */
   1387 	simple_lock(&fs->lfs_interlock);
   1388   top:
   1389 	for (ip = TAILQ_FIRST(&fs->lfs_pchainhd); ip != NULL; ip = nip) {
   1390 		nip = TAILQ_NEXT(ip, i_lfs_pchain);
   1391 		vp = ITOV(ip);
   1392 
   1393 		if (!(ip->i_flags & IN_PAGING))
   1394 			goto top;
   1395 
   1396 		if (vp->v_flag & (VXLOCK|VDIROP))
   1397 			continue;
   1398 		if (vp->v_type != VREG)
   1399 			continue;
   1400 		if (lfs_vref(vp))
   1401 			continue;
   1402 		simple_unlock(&fs->lfs_interlock);
   1403 
   1404 		if (VOP_ISLOCKED(vp)) {
   1405 			lfs_vunref(vp);
   1406 			simple_lock(&fs->lfs_interlock);
   1407 			continue;
   1408 		}
   1409 
   1410 		error = lfs_writefile(fs, sp, vp);
   1411 		if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
   1412 		    !(ip->i_flag & IN_ALLMOD)) {
   1413 			LFS_SET_UINO(ip, IN_MODIFIED);
   1414 		}
   1415 		KDASSERT(ip->i_number != LFS_IFILE_INUM);
   1416 		(void) lfs_writeinode(fs, sp, ip);
   1417 
   1418 		lfs_vunref(vp);
   1419 
   1420 		if (error == EAGAIN) {
   1421 			lfs_writeseg(fs, sp);
   1422 			simple_lock(&fs->lfs_interlock);
   1423 			break;
   1424 		}
   1425 		simple_lock(&fs->lfs_interlock);
   1426 	}
   1427 	simple_unlock(&fs->lfs_interlock);
   1428 	(void) lfs_writeseg(fs, sp);
   1429 	lfs_segunlock(fs);
   1430 }
   1431 
   1432 /*
   1433  * Provide a fcntl interface to sys_lfs_{segwait,bmapv,markv}.
   1434  */
   1435 int
   1436 lfs_fcntl(void *v)
   1437 {
   1438 	struct vop_fcntl_args /* {
   1439 		struct vnode *a_vp;
   1440 		u_long a_command;
   1441 		void * a_data;
   1442 		int  a_fflag;
   1443 		kauth_cred_t a_cred;
   1444 		struct lwp *a_l;
   1445 	} */ *ap = v;
   1446 	struct timeval *tvp;
   1447 	BLOCK_INFO *blkiov;
   1448 	CLEANERINFO *cip;
   1449 	SEGUSE *sup;
   1450 	int blkcnt, error, oclean;
   1451 	size_t fh_size;
   1452 	struct lfs_fcntl_markv blkvp;
   1453 	struct lwp *l;
   1454 	fsid_t *fsidp;
   1455 	struct lfs *fs;
   1456 	struct buf *bp;
   1457 	fhandle_t *fhp;
   1458 	daddr_t off;
   1459 
   1460 	/* Only respect LFS fcntls on fs root or Ifile */
   1461 	if (VTOI(ap->a_vp)->i_number != ROOTINO &&
   1462 	    VTOI(ap->a_vp)->i_number != LFS_IFILE_INUM) {
   1463 		return ufs_fcntl(v);
   1464 	}
   1465 
   1466 	/* Avoid locking a draining lock */
   1467 	if (ap->a_vp->v_mount->mnt_iflag & IMNT_UNMOUNT) {
   1468 		return ESHUTDOWN;
   1469 	}
   1470 
   1471 	/* LFS control and monitoring fcntls are available only to root */
   1472 	l = ap->a_l;
   1473 	if (((ap->a_command & 0xff00) >> 8) == 'L' &&
   1474 	    (error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
   1475 					     NULL)) != 0)
   1476 		return (error);
   1477 
   1478 	fs = VTOI(ap->a_vp)->i_lfs;
   1479 	fsidp = &ap->a_vp->v_mount->mnt_stat.f_fsidx;
   1480 
   1481 	error = 0;
   1482 	switch (ap->a_command) {
   1483 	    case LFCNSEGWAITALL:
   1484 	    case LFCNSEGWAITALL_COMPAT:
   1485 		    fsidp = NULL;
   1486 		    /* FALLSTHROUGH */
   1487 	    case LFCNSEGWAIT:
   1488 	    case LFCNSEGWAIT_COMPAT:
   1489 		    tvp = (struct timeval *)ap->a_data;
   1490 		    simple_lock(&fs->lfs_interlock);
   1491 		    ++fs->lfs_sleepers;
   1492 		    simple_unlock(&fs->lfs_interlock);
   1493 
   1494 		    error = lfs_segwait(fsidp, tvp);
   1495 
   1496 		    simple_lock(&fs->lfs_interlock);
   1497 		    if (--fs->lfs_sleepers == 0)
   1498 			    wakeup(&fs->lfs_sleepers);
   1499 		    simple_unlock(&fs->lfs_interlock);
   1500 		    return error;
   1501 
   1502 	    case LFCNBMAPV:
   1503 	    case LFCNMARKV:
   1504 		    blkvp = *(struct lfs_fcntl_markv *)ap->a_data;
   1505 
   1506 		    blkcnt = blkvp.blkcnt;
   1507 		    if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
   1508 			    return (EINVAL);
   1509 		    blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
   1510 		    if ((error = copyin(blkvp.blkiov, blkiov,
   1511 					blkcnt * sizeof(BLOCK_INFO))) != 0) {
   1512 			    lfs_free(fs, blkiov, LFS_NB_BLKIOV);
   1513 			    return error;
   1514 		    }
   1515 
   1516 		    simple_lock(&fs->lfs_interlock);
   1517 		    ++fs->lfs_sleepers;
   1518 		    simple_unlock(&fs->lfs_interlock);
   1519 		    if (ap->a_command == LFCNBMAPV)
   1520 			    error = lfs_bmapv(l->l_proc, fsidp, blkiov, blkcnt);
   1521 		    else /* LFCNMARKV */
   1522 			    error = lfs_markv(l->l_proc, fsidp, blkiov, blkcnt);
   1523 		    if (error == 0)
   1524 			    error = copyout(blkiov, blkvp.blkiov,
   1525 					    blkcnt * sizeof(BLOCK_INFO));
   1526 		    simple_lock(&fs->lfs_interlock);
   1527 		    if (--fs->lfs_sleepers == 0)
   1528 			    wakeup(&fs->lfs_sleepers);
   1529 		    simple_unlock(&fs->lfs_interlock);
   1530 		    lfs_free(fs, blkiov, LFS_NB_BLKIOV);
   1531 		    return error;
   1532 
   1533 	    case LFCNRECLAIM:
   1534 		    /*
   1535 		     * Flush dirops and write Ifile, allowing empty segments
   1536 		     * to be immediately reclaimed.
   1537 		     */
   1538 		    lfs_writer_enter(fs, "pndirop");
   1539 		    off = fs->lfs_offset;
   1540 		    lfs_seglock(fs, SEGM_FORCE_CKP | SEGM_CKP);
   1541 		    lfs_flush_dirops(fs);
   1542 		    LFS_CLEANERINFO(cip, fs, bp);
   1543 		    oclean = cip->clean;
   1544 		    LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
   1545 		    lfs_segwrite(ap->a_vp->v_mount, SEGM_FORCE_CKP);
   1546 		    fs->lfs_sp->seg_flags |= SEGM_PROT;
   1547 		    lfs_segunlock(fs);
   1548 		    lfs_writer_leave(fs);
   1549 
   1550 #ifdef DEBUG
   1551 		    LFS_CLEANERINFO(cip, fs, bp);
   1552 		    DLOG((DLOG_CLEAN, "lfs_fcntl: reclaim wrote %" PRId64
   1553 			  " blocks, cleaned %" PRId32 " segments (activesb %d)\n",
   1554 			  fs->lfs_offset - off, cip->clean - oclean,
   1555 			  fs->lfs_activesb));
   1556 		    LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
   1557 #endif
   1558 
   1559 		    return 0;
   1560 
   1561 #ifdef COMPAT_30
   1562 	    case LFCNIFILEFH_COMPAT:
   1563 		    /* Return the filehandle of the Ifile */
   1564 		    if ((error = kauth_authorize_generic(l->l_cred,
   1565 							 KAUTH_GENERIC_ISSUSER, NULL)) != 0)
   1566 			    return (error);
   1567 		    fhp = (struct fhandle *)ap->a_data;
   1568 		    fhp->fh_fsid = *fsidp;
   1569 		    fh_size = 16;	/* former VFS_MAXFIDSIZ */
   1570 		    return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
   1571 #endif
   1572 
   1573 	    case LFCNIFILEFH_COMPAT2:
   1574 	    case LFCNIFILEFH:
   1575 		    /* Return the filehandle of the Ifile */
   1576 		    fhp = (struct fhandle *)ap->a_data;
   1577 		    fhp->fh_fsid = *fsidp;
   1578 		    fh_size = sizeof(struct lfs_fhandle) -
   1579 			    offsetof(fhandle_t, fh_fid);
   1580 		    return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
   1581 
   1582 	    case LFCNREWIND:
   1583 		    /* Move lfs_offset to the lowest-numbered segment */
   1584 		    return lfs_rewind(fs, *(int *)ap->a_data);
   1585 
   1586 	    case LFCNINVAL:
   1587 		    /* Mark a segment SEGUSE_INVAL */
   1588 		    LFS_SEGENTRY(sup, fs, *(int *)ap->a_data, bp);
   1589 		    if (sup->su_nbytes > 0) {
   1590 			    brelse(bp);
   1591 			    lfs_unset_inval_all(fs);
   1592 			    return EBUSY;
   1593 		    }
   1594 		    sup->su_flags |= SEGUSE_INVAL;
   1595 		    VOP_BWRITE(bp);
   1596 		    return 0;
   1597 
   1598 	    case LFCNRESIZE:
   1599 		    /* Resize the filesystem */
   1600 		    return lfs_resize_fs(fs, *(int *)ap->a_data);
   1601 
   1602 	    case LFCNWRAPSTOP:
   1603 	    case LFCNWRAPSTOP_COMPAT:
   1604 		    /*
   1605 		     * Hold lfs_newseg at segment 0; if requested, sleep until
   1606 		     * the filesystem wraps around.  To support external agents
   1607 		     * (dump, fsck-based regression test) that need to look at
   1608 		     * a snapshot of the filesystem, without necessarily
   1609 		     * requiring that all fs activity stops.
   1610 		     */
   1611 		    if (lockstatus(&fs->lfs_stoplock))
   1612 			    return EALREADY;
   1613 
   1614 		    simple_lock(&fs->lfs_interlock);
   1615 		    lockmgr(&fs->lfs_stoplock, LK_EXCLUSIVE, &fs->lfs_interlock);
   1616 		    if (fs->lfs_nowrap == 0)
   1617 			    log(LOG_NOTICE, "%s: disabled log wrap\n", fs->lfs_fsmnt);
   1618 		    ++fs->lfs_nowrap;
   1619 		    if (*(int *)ap->a_data == 1 ||
   1620 			ap->a_command == LFCNWRAPSTOP_COMPAT) {
   1621 			    log(LOG_NOTICE, "LFCNSTOPWRAP waiting for log wrap\n");
   1622 			    error = ltsleep(&fs->lfs_nowrap, PCATCH | PUSER,
   1623 					    "segwrap", 0, &fs->lfs_interlock);
   1624 			    log(LOG_NOTICE, "LFCNSTOPWRAP done waiting\n");
   1625 			    if (error) {
   1626 				    lfs_wrapgo(fs, VTOI(ap->a_vp), 0);
   1627 			    }
   1628 		    }
   1629 		    simple_unlock(&fs->lfs_interlock);
   1630 		    return 0;
   1631 
   1632 	    case LFCNWRAPGO:
   1633 	    case LFCNWRAPGO_COMPAT:
   1634 		    /*
   1635 		     * Having done its work, the agent wakes up the writer.
   1636 		     * If the argument is 1, it sleeps until a new segment
   1637 		     * is selected.
   1638 		     */
   1639 		    simple_lock(&fs->lfs_interlock);
   1640 		    error = lfs_wrapgo(fs, VTOI(ap->a_vp),
   1641 				       (ap->a_command == LFCNWRAPGO_COMPAT ? 1 :
   1642 					*((int *)ap->a_data)));
   1643 		    simple_unlock(&fs->lfs_interlock);
   1644 		    return error;
   1645 
   1646 	    case LFCNWRAPPASS:
   1647 		    if (lockstatus(&fs->lfs_stoplock) != LK_EXCLUSIVE)
   1648 			    return EALREADY;
   1649 		    if ((VTOI(ap->a_vp)->i_lfs_iflags & LFSI_WRAPWAIT))
   1650 			    return EALREADY;
   1651 		    simple_lock(&fs->lfs_interlock);
   1652 		    if (fs->lfs_nowrap == 0) {
   1653 			    simple_unlock(&fs->lfs_interlock);
   1654 			    return EBUSY;
   1655 		    }
   1656 		    fs->lfs_wrappass = 1;
   1657 		    wakeup(&fs->lfs_wrappass);
   1658 		    /* Wait for the log to wrap, if asked */
   1659 		    if (*(int *)ap->a_data) {
   1660 			    lfs_vref(ap->a_vp);
   1661 			    VTOI(ap->a_vp)->i_lfs_iflags |= LFSI_WRAPWAIT;
   1662 			    log(LOG_NOTICE, "LFCNPASS waiting for log wrap\n");
   1663 			    error = ltsleep(&fs->lfs_nowrap, PCATCH | PUSER,
   1664 					    "segwrap", 0, &fs->lfs_interlock);
   1665 			    log(LOG_NOTICE, "LFCNPASS done waiting\n");
   1666 			    VTOI(ap->a_vp)->i_lfs_iflags &= ~LFSI_WRAPWAIT;
   1667 			    lfs_vunref(ap->a_vp);
   1668 		    }
   1669 		    simple_unlock(&fs->lfs_interlock);
   1670 		    return error;
   1671 
   1672 	    case LFCNWRAPSTATUS:
   1673 		    simple_lock(&fs->lfs_interlock);
   1674 		    *(int *)ap->a_data = fs->lfs_wrapstatus;
   1675 		    simple_unlock(&fs->lfs_interlock);
   1676 		    return 0;
   1677 
   1678 	    default:
   1679 		    return ufs_fcntl(v);
   1680 	}
   1681 	return 0;
   1682 }
   1683 
   1684 int
   1685 lfs_getpages(void *v)
   1686 {
   1687 	struct vop_getpages_args /* {
   1688 		struct vnode *a_vp;
   1689 		voff_t a_offset;
   1690 		struct vm_page **a_m;
   1691 		int *a_count;
   1692 		int a_centeridx;
   1693 		vm_prot_t a_access_type;
   1694 		int a_advice;
   1695 		int a_flags;
   1696 	} */ *ap = v;
   1697 
   1698 	if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM &&
   1699 	    (ap->a_access_type & VM_PROT_WRITE) != 0) {
   1700 		return EPERM;
   1701 	}
   1702 	if ((ap->a_access_type & VM_PROT_WRITE) != 0) {
   1703 		LFS_SET_UINO(VTOI(ap->a_vp), IN_MODIFIED);
   1704 	}
   1705 
   1706 	/*
   1707 	 * we're relying on the fact that genfs_getpages() always read in
   1708 	 * entire filesystem blocks.
   1709 	 */
   1710 	return genfs_getpages(v);
   1711 }
   1712 
   1713 static void
   1714 wait_for_page(struct vnode *vp, struct vm_page *pg, const char *label)
   1715 {
   1716 	if ((pg->flags & PG_BUSY) == 0)
   1717 		return;		/* Nothing to wait for! */
   1718 
   1719 #if defined(UVM_PAGE_TRKOWN)
   1720 	static struct vm_page *lastpg;
   1721 
   1722 	if (label != NULL && pg != lastpg) {
   1723 		if (pg->owner_tag) {
   1724 			printf("lfs_putpages[%d.%d]: %s: page %p owner %d.%d [%s]\n",
   1725 			       curproc->p_pid, curlwp->l_lid, label,
   1726 			       pg, pg->owner, pg->lowner, pg->owner_tag);
   1727 		} else {
   1728 			printf("lfs_putpages[%d.%d]: %s: page %p unowned?!\n",
   1729 			       curproc->p_pid, curlwp->l_lid, label, pg);
   1730 		}
   1731 	}
   1732 	lastpg = pg;
   1733 #endif
   1734 
   1735 	pg->flags |= PG_WANTED;
   1736 	UVM_UNLOCK_AND_WAIT(pg, &vp->v_interlock, 0, "lfsput", 0);
   1737 	simple_lock(&vp->v_interlock);
   1738 }
   1739 
   1740 /*
   1741  * This routine is called by lfs_putpages() when it can't complete the
   1742  * write because a page is busy.  This means that either (1) someone,
   1743  * possibly the pagedaemon, is looking at this page, and will give it up
   1744  * presently; or (2) we ourselves are holding the page busy in the
   1745  * process of being written (either gathered or actually on its way to
   1746  * disk).  We don't need to give up the segment lock, but we might need
   1747  * to call lfs_writeseg() to expedite the page's journey to disk.
   1748  */
   1749 /* #define BUSYWAIT */
   1750 static void
   1751 write_and_wait(struct lfs *fs, struct vnode *vp, struct vm_page *pg,
   1752 	       int seglocked, const char *label)
   1753 {
   1754 #ifndef BUSYWAIT
   1755 	struct inode *ip = VTOI(vp);
   1756 	struct segment *sp = fs->lfs_sp;
   1757 	int count = 0;
   1758 
   1759 	if (pg == NULL)
   1760 		return;
   1761 
   1762 	while (pg->flags & PG_BUSY) {
   1763 		if (sp->cbpp - sp->bpp > 1) {
   1764 			/* Write gathered pages */
   1765 			lfs_updatemeta(sp);
   1766 			lfs_release_finfo(fs);
   1767 			(void) lfs_writeseg(fs, sp);
   1768 
   1769 			/*
   1770 			 * Reinitialize FIP
   1771 			 */
   1772 			KASSERT(sp->vp == vp);
   1773 			lfs_acquire_finfo(fs, ip->i_number,
   1774 					  ip->i_gen);
   1775 		}
   1776 		wait_for_page(vp, pg, label);
   1777 		++count;
   1778 	}
   1779 	if (label != NULL && count > 1)
   1780 		printf("lfs_putpages[%d]: %s: %sn = %d\n", curproc->p_pid,
   1781 		       label, (count > 0 ? "looping, " : ""), count);
   1782 #else
   1783 	preempt(1);
   1784 #endif
   1785 }
   1786 
   1787 /*
   1788  * Make sure that for all pages in every block in the given range,
   1789  * either all are dirty or all are clean.  If any of the pages
   1790  * we've seen so far are dirty, put the vnode on the paging chain,
   1791  * and mark it IN_PAGING.
   1792  *
   1793  * If checkfirst != 0, don't check all the pages but return at the
   1794  * first dirty page.
   1795  */
   1796 static int
   1797 check_dirty(struct lfs *fs, struct vnode *vp,
   1798 	    off_t startoffset, off_t endoffset, off_t blkeof,
   1799 	    int flags, int checkfirst, struct vm_page **pgp)
   1800 {
   1801 	int by_list;
   1802 	struct vm_page *curpg = NULL; /* XXX: gcc */
   1803 	struct vm_page *pgs[MAXBSIZE / PAGE_SIZE], *pg;
   1804 	off_t soff = 0; /* XXX: gcc */
   1805 	voff_t off;
   1806 	int i;
   1807 	int nonexistent;
   1808 	int any_dirty;	/* number of dirty pages */
   1809 	int dirty;	/* number of dirty pages in a block */
   1810 	int tdirty;
   1811 	int pages_per_block = fs->lfs_bsize >> PAGE_SHIFT;
   1812 	int pagedaemon = (curproc == uvm.pagedaemon_proc);
   1813 
   1814 	ASSERT_MAYBE_SEGLOCK(fs);
   1815   top:
   1816 	by_list = (vp->v_uobj.uo_npages <=
   1817 		   ((endoffset - startoffset) >> PAGE_SHIFT) *
   1818 		   UVM_PAGE_HASH_PENALTY);
   1819 	any_dirty = 0;
   1820 
   1821 	if (by_list) {
   1822 		curpg = TAILQ_FIRST(&vp->v_uobj.memq);
   1823 	} else {
   1824 		soff = startoffset;
   1825 	}
   1826 	while (by_list || soff < MIN(blkeof, endoffset)) {
   1827 		if (by_list) {
   1828 			/*
   1829 			 * Find the first page in a block.  Skip
   1830 			 * blocks outside our area of interest or beyond
   1831 			 * the end of file.
   1832 			 */
   1833 			if (pages_per_block > 1) {
   1834 				while (curpg &&
   1835 				       ((curpg->offset & fs->lfs_bmask) ||
   1836 					curpg->offset >= vp->v_size ||
   1837 					curpg->offset >= endoffset))
   1838 					curpg = TAILQ_NEXT(curpg, listq);
   1839 			}
   1840 			if (curpg == NULL)
   1841 				break;
   1842 			soff = curpg->offset;
   1843 		}
   1844 
   1845 		/*
   1846 		 * Mark all pages in extended range busy; find out if any
   1847 		 * of them are dirty.
   1848 		 */
   1849 		nonexistent = dirty = 0;
   1850 		for (i = 0; i == 0 || i < pages_per_block; i++) {
   1851 			if (by_list && pages_per_block <= 1) {
   1852 				pgs[i] = pg = curpg;
   1853 			} else {
   1854 				off = soff + (i << PAGE_SHIFT);
   1855 				pgs[i] = pg = uvm_pagelookup(&vp->v_uobj, off);
   1856 				if (pg == NULL) {
   1857 					++nonexistent;
   1858 					continue;
   1859 				}
   1860 			}
   1861 			KASSERT(pg != NULL);
   1862 
   1863 			/*
   1864 			 * If we're holding the segment lock, we can deadlock
   1865 			 * against a process that has our page and is waiting
   1866 			 * for the cleaner, while the cleaner waits for the
   1867 			 * segment lock.  Just bail in that case.
   1868 			 */
   1869 			if ((pg->flags & PG_BUSY) &&
   1870 			    (pagedaemon || LFS_SEGLOCK_HELD(fs))) {
   1871 				if (i > 0)
   1872 					uvm_page_unbusy(pgs, i);
   1873 				DLOG((DLOG_PAGE, "lfs_putpages: avoiding 3-way or pagedaemon deadlock\n"));
   1874 				if (pgp)
   1875 					*pgp = pg;
   1876 				return -1;
   1877 			}
   1878 
   1879 			while (pg->flags & PG_BUSY) {
   1880 				wait_for_page(vp, pg, NULL);
   1881 				if (i > 0)
   1882 					uvm_page_unbusy(pgs, i);
   1883 				goto top;
   1884 			}
   1885 			pg->flags |= PG_BUSY;
   1886 			UVM_PAGE_OWN(pg, "lfs_putpages");
   1887 
   1888 			pmap_page_protect(pg, VM_PROT_NONE);
   1889 			tdirty = (pmap_clear_modify(pg) ||
   1890 				  (pg->flags & PG_CLEAN) == 0);
   1891 			dirty += tdirty;
   1892 		}
   1893 		if (pages_per_block > 0 && nonexistent >= pages_per_block) {
   1894 			if (by_list) {
   1895 				curpg = TAILQ_NEXT(curpg, listq);
   1896 			} else {
   1897 				soff += fs->lfs_bsize;
   1898 			}
   1899 			continue;
   1900 		}
   1901 
   1902 		any_dirty += dirty;
   1903 		KASSERT(nonexistent == 0);
   1904 
   1905 		/*
   1906 		 * If any are dirty make all dirty; unbusy them,
   1907 		 * but if we were asked to clean, wire them so that
   1908 		 * the pagedaemon doesn't bother us about them while
   1909 		 * they're on their way to disk.
   1910 		 */
   1911 		for (i = 0; i == 0 || i < pages_per_block; i++) {
   1912 			pg = pgs[i];
   1913 			KASSERT(!((pg->flags & PG_CLEAN) && (pg->flags & PG_DELWRI)));
   1914 			if (dirty) {
   1915 				pg->flags &= ~PG_CLEAN;
   1916 				if (flags & PGO_FREE) {
   1917 					/*
   1918 					 * Wire the page so that
   1919 					 * pdaemon doesn't see it again.
   1920 					 */
   1921 					uvm_lock_pageq();
   1922 					uvm_pagewire(pg);
   1923 					uvm_unlock_pageq();
   1924 
   1925 					/* Suspended write flag */
   1926 					pg->flags |= PG_DELWRI;
   1927 				}
   1928 			}
   1929 			if (pg->flags & PG_WANTED)
   1930 				wakeup(pg);
   1931 			pg->flags &= ~(PG_WANTED|PG_BUSY);
   1932 			UVM_PAGE_OWN(pg, NULL);
   1933 		}
   1934 
   1935 		if (checkfirst && any_dirty)
   1936 			break;
   1937 
   1938 		if (by_list) {
   1939 			curpg = TAILQ_NEXT(curpg, listq);
   1940 		} else {
   1941 			soff += MAX(PAGE_SIZE, fs->lfs_bsize);
   1942 		}
   1943 	}
   1944 
   1945 	return any_dirty;
   1946 }
   1947 
   1948 /*
   1949  * lfs_putpages functions like genfs_putpages except that
   1950  *
   1951  * (1) It needs to bounds-check the incoming requests to ensure that
   1952  *     they are block-aligned; if they are not, expand the range and
   1953  *     do the right thing in case, e.g., the requested range is clean
   1954  *     but the expanded range is dirty.
   1955  *
   1956  * (2) It needs to explicitly send blocks to be written when it is done.
   1957  *     If VOP_PUTPAGES is called without the seglock held, we simply take
   1958  *     the seglock and let lfs_segunlock wait for us.
   1959  *     XXX There might be a bad situation if we have to flush a vnode while
   1960  *     XXX lfs_markv is in operation.  As of this writing we panic in this
   1961  *     XXX case.
   1962  *
   1963  * Assumptions:
   1964  *
   1965  * (1) The caller does not hold any pages in this vnode busy.  If it does,
   1966  *     there is a danger that when we expand the page range and busy the
   1967  *     pages we will deadlock.
   1968  *
   1969  * (2) We are called with vp->v_interlock held; we must return with it
   1970  *     released.
   1971  *
   1972  * (3) We don't absolutely have to free pages right away, provided that
   1973  *     the request does not have PGO_SYNCIO.  When the pagedaemon gives
   1974  *     us a request with PGO_FREE, we take the pages out of the paging
   1975  *     queue and wake up the writer, which will handle freeing them for us.
   1976  *
   1977  *     We ensure that for any filesystem block, all pages for that
   1978  *     block are either resident or not, even if those pages are higher
   1979  *     than EOF; that means that we will be getting requests to free
   1980  *     "unused" pages above EOF all the time, and should ignore them.
   1981  *
   1982  * (4) If we are called with PGO_LOCKED, the finfo array we are to write
   1983  *     into has been set up for us by lfs_writefile.  If not, we will
   1984  *     have to handle allocating and/or freeing an finfo entry.
   1985  *
   1986  * XXX note that we're (ab)using PGO_LOCKED as "seglock held".
   1987  */
   1988 
   1989 /* How many times to loop before we should start to worry */
   1990 #define TOOMANY 4
   1991 
   1992 int
   1993 lfs_putpages(void *v)
   1994 {
   1995 	int error;
   1996 	struct vop_putpages_args /* {
   1997 		struct vnode *a_vp;
   1998 		voff_t a_offlo;
   1999 		voff_t a_offhi;
   2000 		int a_flags;
   2001 	} */ *ap = v;
   2002 	struct vnode *vp;
   2003 	struct inode *ip;
   2004 	struct lfs *fs;
   2005 	struct segment *sp;
   2006 	off_t origoffset, startoffset, endoffset, origendoffset, blkeof;
   2007 	off_t off, max_endoffset;
   2008 	int s;
   2009 	bool seglocked, sync, pagedaemon;
   2010 	struct vm_page *pg, *busypg;
   2011 	UVMHIST_FUNC("lfs_putpages"); UVMHIST_CALLED(ubchist);
   2012 #ifdef DEBUG
   2013 	int debug_n_again, debug_n_dirtyclean;
   2014 #endif
   2015 
   2016 	vp = ap->a_vp;
   2017 	ip = VTOI(vp);
   2018 	fs = ip->i_lfs;
   2019 	sync = (ap->a_flags & PGO_SYNCIO) != 0;
   2020 	pagedaemon = (curproc == uvm.pagedaemon_proc);
   2021 
   2022 	/* Putpages does nothing for metadata. */
   2023 	if (vp == fs->lfs_ivnode || vp->v_type != VREG) {
   2024 		simple_unlock(&vp->v_interlock);
   2025 		return 0;
   2026 	}
   2027 
   2028 	/*
   2029 	 * If there are no pages, don't do anything.
   2030 	 */
   2031 	if (vp->v_uobj.uo_npages == 0) {
   2032 		s = splbio();
   2033 		if (TAILQ_EMPTY(&vp->v_uobj.memq) &&
   2034 		    (vp->v_flag & VONWORKLST) &&
   2035 		    LIST_FIRST(&vp->v_dirtyblkhd) == NULL) {
   2036 			vp->v_flag &= ~VWRITEMAPDIRTY;
   2037 			vn_syncer_remove_from_worklist(vp);
   2038 		}
   2039 		splx(s);
   2040 		simple_unlock(&vp->v_interlock);
   2041 
   2042 		/* Remove us from paging queue, if we were on it */
   2043 		simple_lock(&fs->lfs_interlock);
   2044 		if (ip->i_flags & IN_PAGING) {
   2045 			ip->i_flags &= ~IN_PAGING;
   2046 			TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
   2047 		}
   2048 		simple_unlock(&fs->lfs_interlock);
   2049 		return 0;
   2050 	}
   2051 
   2052 	blkeof = blkroundup(fs, ip->i_size);
   2053 
   2054 	/*
   2055 	 * Ignore requests to free pages past EOF but in the same block
   2056 	 * as EOF, unless the request is synchronous.  (If the request is
   2057 	 * sync, it comes from lfs_truncate.)
   2058 	 * XXXUBC Make these pages look "active" so the pagedaemon won't
   2059 	 * XXXUBC bother us with them again.
   2060 	 */
   2061 	if (!sync && ap->a_offlo >= ip->i_size && ap->a_offlo < blkeof) {
   2062 		origoffset = ap->a_offlo;
   2063 		for (off = origoffset; off < blkeof; off += fs->lfs_bsize) {
   2064 			pg = uvm_pagelookup(&vp->v_uobj, off);
   2065 			KASSERT(pg != NULL);
   2066 			while (pg->flags & PG_BUSY) {
   2067 				pg->flags |= PG_WANTED;
   2068 				UVM_UNLOCK_AND_WAIT(pg, &vp->v_interlock, 0,
   2069 						    "lfsput2", 0);
   2070 				simple_lock(&vp->v_interlock);
   2071 			}
   2072 			uvm_lock_pageq();
   2073 			uvm_pageactivate(pg);
   2074 			uvm_unlock_pageq();
   2075 		}
   2076 		ap->a_offlo = blkeof;
   2077 		if (ap->a_offhi > 0 && ap->a_offhi <= ap->a_offlo) {
   2078 			simple_unlock(&vp->v_interlock);
   2079 			return 0;
   2080 		}
   2081 	}
   2082 
   2083 	/*
   2084 	 * Extend page range to start and end at block boundaries.
   2085 	 * (For the purposes of VOP_PUTPAGES, fragments don't exist.)
   2086 	 */
   2087 	origoffset = ap->a_offlo;
   2088 	origendoffset = ap->a_offhi;
   2089 	startoffset = origoffset & ~(fs->lfs_bmask);
   2090 	max_endoffset = (trunc_page(LLONG_MAX) >> fs->lfs_bshift)
   2091 					       << fs->lfs_bshift;
   2092 
   2093 	if (origendoffset == 0 || ap->a_flags & PGO_ALLPAGES) {
   2094 		endoffset = max_endoffset;
   2095 		origendoffset = endoffset;
   2096 	} else {
   2097 		origendoffset = round_page(ap->a_offhi);
   2098 		endoffset = round_page(blkroundup(fs, origendoffset));
   2099 	}
   2100 
   2101 	KASSERT(startoffset > 0 || endoffset >= startoffset);
   2102 	if (startoffset == endoffset) {
   2103 		/* Nothing to do, why were we called? */
   2104 		simple_unlock(&vp->v_interlock);
   2105 		DLOG((DLOG_PAGE, "lfs_putpages: startoffset = endoffset = %"
   2106 		      PRId64 "\n", startoffset));
   2107 		return 0;
   2108 	}
   2109 
   2110 	ap->a_offlo = startoffset;
   2111 	ap->a_offhi = endoffset;
   2112 
   2113 	/*
   2114 	 * If not cleaning, just send the pages through genfs_putpages
   2115 	 * to be returned to the pool.
   2116 	 */
   2117 	if (!(ap->a_flags & PGO_CLEANIT))
   2118 		return genfs_putpages(v);
   2119 
   2120 	/* Set PGO_BUSYFAIL to avoid deadlocks */
   2121 	ap->a_flags |= PGO_BUSYFAIL;
   2122 
   2123 	/*
   2124 	 * Likewise, if we are asked to clean but the pages are not
   2125 	 * dirty, we can just free them using genfs_putpages.
   2126 	 */
   2127 #ifdef DEBUG
   2128 	debug_n_dirtyclean = 0;
   2129 #endif
   2130 	do {
   2131 		int r;
   2132 
   2133 		/* Count the number of dirty pages */
   2134 		r = check_dirty(fs, vp, startoffset, endoffset, blkeof,
   2135 				ap->a_flags, 1, NULL);
   2136 		if (r < 0) {
   2137 			/* Pages are busy with another process */
   2138 			simple_unlock(&vp->v_interlock);
   2139 			return EDEADLK;
   2140 		}
   2141 		if (r > 0) /* Some pages are dirty */
   2142 			break;
   2143 
   2144 		/*
   2145 		 * Sometimes pages are dirtied between the time that
   2146 		 * we check and the time we try to clean them.
   2147 		 * Instruct lfs_gop_write to return EDEADLK in this case
   2148 		 * so we can write them properly.
   2149 		 */
   2150 		ip->i_lfs_iflags |= LFSI_NO_GOP_WRITE;
   2151 		r = lfs_genfs_putpages(vp, startoffset, endoffset,
   2152 				       ap->a_flags, &busypg);
   2153 		ip->i_lfs_iflags &= ~LFSI_NO_GOP_WRITE;
   2154 		if (r != EDEADLK)
   2155 			return r;
   2156 
   2157 		/* One of the pages was busy.  Start over. */
   2158 		wait_for_page(vp, busypg, "dirtyclean");
   2159 		simple_lock(&vp->v_interlock);
   2160 #ifdef DEBUG
   2161 		++debug_n_dirtyclean;
   2162 #endif
   2163 	} while(1);
   2164 
   2165 #ifdef DEBUG
   2166 	if (debug_n_dirtyclean > TOOMANY)
   2167 		printf("lfs_putpages: dirtyclean: looping, n = %d\n",
   2168 		       debug_n_dirtyclean);
   2169 #endif
   2170 
   2171 	/*
   2172 	 * Dirty and asked to clean.
   2173 	 *
   2174 	 * Pagedaemon can't actually write LFS pages; wake up
   2175 	 * the writer to take care of that.  The writer will
   2176 	 * notice the pager inode queue and act on that.
   2177 	 */
   2178 	if (pagedaemon) {
   2179 		simple_lock(&fs->lfs_interlock);
   2180 		if (!(ip->i_flags & IN_PAGING)) {
   2181 			ip->i_flags |= IN_PAGING;
   2182 			TAILQ_INSERT_TAIL(&fs->lfs_pchainhd, ip, i_lfs_pchain);
   2183 		}
   2184 		simple_lock(&lfs_subsys_lock);
   2185 		wakeup(&lfs_writer_daemon);
   2186 		simple_unlock(&lfs_subsys_lock);
   2187 		simple_unlock(&fs->lfs_interlock);
   2188 		simple_unlock(&vp->v_interlock);
   2189 		preempt();
   2190 		return EWOULDBLOCK;
   2191 	}
   2192 
   2193 	/*
   2194 	 * If this is a file created in a recent dirop, we can't flush its
   2195 	 * inode until the dirop is complete.  Drain dirops, then flush the
   2196 	 * filesystem (taking care of any other pending dirops while we're
   2197 	 * at it).
   2198 	 */
   2199 	if ((ap->a_flags & (PGO_CLEANIT|PGO_LOCKED)) == PGO_CLEANIT &&
   2200 	    (vp->v_flag & VDIROP)) {
   2201 		int locked;
   2202 
   2203 		DLOG((DLOG_PAGE, "lfs_putpages: flushing VDIROP\n"));
   2204 		locked = (VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
   2205 		simple_unlock(&vp->v_interlock);
   2206 		lfs_writer_enter(fs, "ppdirop");
   2207 		if (locked)
   2208 			VOP_UNLOCK(vp, 0); /* XXX why? */
   2209 
   2210 		simple_lock(&fs->lfs_interlock);
   2211 		lfs_flush_fs(fs, sync ? SEGM_SYNC : 0);
   2212 		simple_unlock(&fs->lfs_interlock);
   2213 
   2214 		simple_lock(&vp->v_interlock);
   2215 		if (locked) {
   2216 			VOP_LOCK(vp, LK_EXCLUSIVE | LK_INTERLOCK);
   2217 			simple_lock(&vp->v_interlock);
   2218 		}
   2219 		lfs_writer_leave(fs);
   2220 
   2221 		/* XXX the flush should have taken care of this one too! */
   2222 	}
   2223 
   2224 	/*
   2225 	 * This is it.	We are going to write some pages.  From here on
   2226 	 * down it's all just mechanics.
   2227 	 *
   2228 	 * Don't let genfs_putpages wait; lfs_segunlock will wait for us.
   2229 	 */
   2230 	ap->a_flags &= ~PGO_SYNCIO;
   2231 
   2232 	/*
   2233 	 * If we've already got the seglock, flush the node and return.
   2234 	 * The FIP has already been set up for us by lfs_writefile,
   2235 	 * and FIP cleanup and lfs_updatemeta will also be done there,
   2236 	 * unless genfs_putpages returns EDEADLK; then we must flush
   2237 	 * what we have, and correct FIP and segment header accounting.
   2238 	 */
   2239   get_seglock:
   2240 	/*
   2241 	 * If we are not called with the segment locked, lock it.
   2242 	 * Account for a new FIP in the segment header, and set sp->vp.
   2243 	 * (This should duplicate the setup at the top of lfs_writefile().)
   2244 	 */
   2245 	seglocked = (ap->a_flags & PGO_LOCKED) != 0;
   2246 	if (!seglocked) {
   2247 		simple_unlock(&vp->v_interlock);
   2248 		error = lfs_seglock(fs, SEGM_PROT | (sync ? SEGM_SYNC : 0));
   2249 		if (error != 0)
   2250 			return error;
   2251 		simple_lock(&vp->v_interlock);
   2252 		lfs_acquire_finfo(fs, ip->i_number, ip->i_gen);
   2253 	}
   2254 	sp = fs->lfs_sp;
   2255 	KASSERT(sp->vp == NULL);
   2256 	sp->vp = vp;
   2257 
   2258 	/*
   2259 	 * Ensure that the partial segment is marked SS_DIROP if this
   2260 	 * vnode is a DIROP.
   2261 	 */
   2262 	if (!seglocked && vp->v_flag & VDIROP)
   2263 		((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
   2264 
   2265 	/*
   2266 	 * Loop over genfs_putpages until all pages are gathered.
   2267 	 * genfs_putpages() drops the interlock, so reacquire it if necessary.
   2268 	 * Whenever we lose the interlock we have to rerun check_dirty, as
   2269 	 * well, since more pages might have been dirtied in our absence.
   2270 	 */
   2271 #ifdef DEBUG
   2272 	debug_n_again = 0;
   2273 #endif
   2274 	do {
   2275 		busypg = NULL;
   2276 		if (check_dirty(fs, vp, startoffset, endoffset, blkeof,
   2277 				ap->a_flags, 0, &busypg) < 0) {
   2278 			simple_unlock(&vp->v_interlock);
   2279 			sp->vp = NULL;
   2280 
   2281 			write_and_wait(fs, vp, busypg, seglocked, NULL);
   2282 			if (!seglocked) {
   2283 				lfs_release_finfo(fs);
   2284 				lfs_segunlock(fs);
   2285 			}
   2286 			simple_lock(&vp->v_interlock);
   2287 			goto get_seglock;
   2288 		}
   2289 
   2290 		busypg = NULL;
   2291 		error = lfs_genfs_putpages(vp, startoffset, endoffset,
   2292 					   ap->a_flags, &busypg);
   2293 
   2294 		if (error == EDEADLK || error == EAGAIN) {
   2295 			DLOG((DLOG_PAGE, "lfs_putpages: genfs_putpages returned"
   2296 			      " %d ino %d off %x (seg %d)\n", error,
   2297 			      ip->i_number, fs->lfs_offset,
   2298 			      dtosn(fs, fs->lfs_offset)));
   2299 
   2300 			write_and_wait(fs, vp, busypg, seglocked, "again");
   2301 			if (error != EAGAIN) /* XXX - test on multiproc */
   2302 				simple_lock(&vp->v_interlock);
   2303 		}
   2304 #ifdef DEBUG
   2305 		++debug_n_again;
   2306 #endif
   2307 	} while (error == EDEADLK);
   2308 #ifdef DEBUG
   2309 	if (debug_n_again > TOOMANY)
   2310 		printf("lfs_putpages: again: looping, n = %d\n", debug_n_again);
   2311 #endif
   2312 
   2313 	KASSERT(sp != NULL && sp->vp == vp);
   2314 	if (!seglocked) {
   2315 		sp->vp = NULL;
   2316 
   2317 		/* Write indirect blocks as well */
   2318 		lfs_gather(fs, fs->lfs_sp, vp, lfs_match_indir);
   2319 		lfs_gather(fs, fs->lfs_sp, vp, lfs_match_dindir);
   2320 		lfs_gather(fs, fs->lfs_sp, vp, lfs_match_tindir);
   2321 
   2322 		KASSERT(sp->vp == NULL);
   2323 		sp->vp = vp;
   2324 	}
   2325 
   2326 	/*
   2327 	 * Blocks are now gathered into a segment waiting to be written.
   2328 	 * All that's left to do is update metadata, and write them.
   2329 	 */
   2330 	lfs_updatemeta(sp);
   2331 	KASSERT(sp->vp == vp);
   2332 	sp->vp = NULL;
   2333 
   2334 	/*
   2335 	 * If we were called from lfs_writefile, we don't need to clean up
   2336 	 * the FIP or unlock the segment lock.	We're done.
   2337 	 */
   2338 	if (seglocked)
   2339 		return error;
   2340 
   2341 	/* Clean up FIP and send it to disk. */
   2342 	lfs_release_finfo(fs);
   2343 	lfs_writeseg(fs, fs->lfs_sp);
   2344 
   2345 	/*
   2346 	 * Remove us from paging queue if we wrote all our pages.
   2347 	 */
   2348 	if (origendoffset == 0 || ap->a_flags & PGO_ALLPAGES) {
   2349 		simple_lock(&fs->lfs_interlock);
   2350 		if (ip->i_flags & IN_PAGING) {
   2351 			ip->i_flags &= ~IN_PAGING;
   2352 			TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
   2353 		}
   2354 		simple_unlock(&fs->lfs_interlock);
   2355 	}
   2356 
   2357 	/*
   2358 	 * XXX - with the malloc/copy writeseg, the pages are freed by now
   2359 	 * even if we don't wait (e.g. if we hold a nested lock).  This
   2360 	 * will not be true if we stop using malloc/copy.
   2361 	 */
   2362 	KASSERT(fs->lfs_sp->seg_flags & SEGM_PROT);
   2363 	lfs_segunlock(fs);
   2364 
   2365 	/*
   2366 	 * Wait for v_numoutput to drop to zero.  The seglock should
   2367 	 * take care of this, but there is a slight possibility that
   2368 	 * aiodoned might not have got around to our buffers yet.
   2369 	 */
   2370 	if (sync) {
   2371 		s = splbio();
   2372 		simple_lock(&global_v_numoutput_slock);
   2373 		while (vp->v_numoutput > 0) {
   2374 			DLOG((DLOG_PAGE, "lfs_putpages: ino %d sleeping on"
   2375 			      " num %d\n", ip->i_number, vp->v_numoutput));
   2376 			vp->v_flag |= VBWAIT;
   2377 			ltsleep(&vp->v_numoutput, PRIBIO + 1, "lfs_vn", 0,
   2378 				&global_v_numoutput_slock);
   2379 		}
   2380 		simple_unlock(&global_v_numoutput_slock);
   2381 		splx(s);
   2382 	}
   2383 	return error;
   2384 }
   2385 
   2386 /*
   2387  * Return the last logical file offset that should be written for this file
   2388  * if we're doing a write that ends at "size".	If writing, we need to know
   2389  * about sizes on disk, i.e. fragments if there are any; if reading, we need
   2390  * to know about entire blocks.
   2391  */
   2392 void
   2393 lfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
   2394 {
   2395 	struct inode *ip = VTOI(vp);
   2396 	struct lfs *fs = ip->i_lfs;
   2397 	daddr_t olbn, nlbn;
   2398 
   2399 	olbn = lblkno(fs, ip->i_size);
   2400 	nlbn = lblkno(fs, size);
   2401 	if (!(flags & GOP_SIZE_MEM) && nlbn < NDADDR && olbn <= nlbn) {
   2402 		*eobp = fragroundup(fs, size);
   2403 	} else {
   2404 		*eobp = blkroundup(fs, size);
   2405 	}
   2406 }
   2407 
   2408 #ifdef DEBUG
   2409 void lfs_dump_vop(void *);
   2410 
   2411 void
   2412 lfs_dump_vop(void *v)
   2413 {
   2414 	struct vop_putpages_args /* {
   2415 		struct vnode *a_vp;
   2416 		voff_t a_offlo;
   2417 		voff_t a_offhi;
   2418 		int a_flags;
   2419 	} */ *ap = v;
   2420 
   2421 #ifdef DDB
   2422 	vfs_vnode_print(ap->a_vp, 0, printf);
   2423 #endif
   2424 	lfs_dump_dinode(VTOI(ap->a_vp)->i_din.ffs1_din);
   2425 }
   2426 #endif
   2427 
   2428 int
   2429 lfs_mmap(void *v)
   2430 {
   2431 	struct vop_mmap_args /* {
   2432 		const struct vnodeop_desc *a_desc;
   2433 		struct vnode *a_vp;
   2434 		int a_fflags;
   2435 		kauth_cred_t a_cred;
   2436 		struct lwp *a_l;
   2437 	} */ *ap = v;
   2438 
   2439 	if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM)
   2440 		return EOPNOTSUPP;
   2441 	return ufs_mmap(v);
   2442 }
   2443 
   2444 /*
   2445  * Our own version of genfs_putpages.  This is never called by the pagedaemon
   2446  * or with PGO_SYNCIO, always called with PGO_CLEANIT and PGO_BUSYFAIL; and
   2447  * in the event of failure it returns the busy page in "busypg".
   2448  */
   2449 static int
   2450 lfs_genfs_putpages(struct vnode *vp, off_t startoff, off_t endoff, int flags,
   2451 		   struct vm_page **busypg)
   2452 {
   2453 	struct uvm_object *uobj = &vp->v_uobj;
   2454 	struct simplelock *slock = &uobj->vmobjlock;
   2455 	off_t off;
   2456 	/* Even for strange MAXPHYS, the shift rounds down to a page */
   2457 #define maxpages (MAXPHYS >> PAGE_SHIFT)
   2458 	int i, s, error, npages, nback;
   2459 	int freeflag;
   2460 	struct vm_page *pgs[maxpages], *pg, *nextpg, *tpg, curmp, endmp;
   2461 	bool wasclean, by_list, needs_clean, yld;
   2462 	bool async = (flags & PGO_SYNCIO) == 0;
   2463 	bool pagedaemon = curproc == uvm.pagedaemon_proc;
   2464 	struct lwp *l = curlwp ? curlwp : &lwp0;
   2465 	struct genfs_node *gp = VTOG(vp);
   2466 	int dirtygen;
   2467 	bool modified = false;
   2468 	bool has_trans = false;
   2469 	bool cleanall;
   2470 	bool writeinprog;
   2471 
   2472 	UVMHIST_FUNC("genfs_putpages"); UVMHIST_CALLED(ubchist);
   2473 
   2474         *busypg = NULL;
   2475 
   2476 	KASSERT(flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE));
   2477 	KASSERT((startoff & PAGE_MASK) == 0 && (endoff & PAGE_MASK) == 0);
   2478 	KASSERT(startoff < endoff || endoff == 0);
   2479 
   2480 	UVMHIST_LOG(ubchist, "vp %p pages %d off 0x%x len 0x%x",
   2481 	    vp, uobj->uo_npages, startoff, endoff - startoff);
   2482 
   2483 	KASSERT((vp->v_flag & VONWORKLST) != 0 ||
   2484 	    (vp->v_flag & VWRITEMAPDIRTY) == 0);
   2485 	if (uobj->uo_npages == 0) {
   2486 		s = splbio();
   2487 		if (vp->v_flag & VONWORKLST) {
   2488 			vp->v_flag &= ~VWRITEMAPDIRTY;
   2489 			if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL)
   2490 				vn_syncer_remove_from_worklist(vp);
   2491 		}
   2492 		splx(s);
   2493 		simple_unlock(slock);
   2494 		return (0);
   2495 	}
   2496 
   2497 	/*
   2498 	 * the vnode has pages, set up to process the request.
   2499 	 */
   2500 
   2501 	if ((flags & PGO_CLEANIT) != 0) {
   2502 		simple_unlock(slock);
   2503 		if (pagedaemon)
   2504 			error = fstrans_start_nowait(vp->v_mount, FSTRANS_LAZY);
   2505 		else
   2506 			error = fstrans_start(vp->v_mount, FSTRANS_LAZY);
   2507 		if (error)
   2508 			return error;
   2509 		has_trans = true;
   2510 		simple_lock(slock);
   2511 	}
   2512 
   2513 	error = 0;
   2514 	s = splbio();
   2515 	simple_lock(&global_v_numoutput_slock);
   2516 	wasclean = (vp->v_numoutput == 0);
   2517 	simple_unlock(&global_v_numoutput_slock);
   2518 	splx(s);
   2519 	off = startoff;
   2520 	if (endoff == 0 || flags & PGO_ALLPAGES) {
   2521 		endoff = trunc_page(LLONG_MAX);
   2522 	}
   2523 	by_list = (uobj->uo_npages <=
   2524 	    ((endoff - startoff) >> PAGE_SHIFT) * UVM_PAGE_HASH_PENALTY);
   2525 
   2526 #if !defined(DEBUG)
   2527 	/*
   2528 	 * if this vnode is known not to have dirty pages,
   2529 	 * don't bother to clean it out.
   2530 	 */
   2531 
   2532 	if ((vp->v_flag & VONWORKLST) == 0) {
   2533 		if ((flags & (PGO_FREE|PGO_DEACTIVATE)) == 0) {
   2534 			goto skip_scan;
   2535 		}
   2536 		flags &= ~PGO_CLEANIT;
   2537 	}
   2538 #endif /* !defined(DEBUG) */
   2539 
   2540 	/*
   2541 	 * start the loop.  when scanning by list, hold the last page
   2542 	 * in the list before we start.  pages allocated after we start
   2543 	 * will be added to the end of the list, so we can stop at the
   2544 	 * current last page.
   2545 	 */
   2546 
   2547 	cleanall = (flags & PGO_CLEANIT) != 0 && wasclean &&
   2548 	    startoff == 0 && endoff == trunc_page(LLONG_MAX) &&
   2549 	    (vp->v_flag & VONWORKLST) != 0;
   2550 	dirtygen = gp->g_dirtygen;
   2551 	freeflag = pagedaemon ? PG_PAGEOUT : PG_RELEASED;
   2552 	if (by_list) {
   2553 		curmp.uobject = uobj;
   2554 		curmp.offset = (voff_t)-1;
   2555 		curmp.flags = PG_BUSY;
   2556 		endmp.uobject = uobj;
   2557 		endmp.offset = (voff_t)-1;
   2558 		endmp.flags = PG_BUSY;
   2559 		pg = TAILQ_FIRST(&uobj->memq);
   2560 		TAILQ_INSERT_TAIL(&uobj->memq, &endmp, listq);
   2561 		PHOLD(l);
   2562 	} else {
   2563 		pg = uvm_pagelookup(uobj, off);
   2564 	}
   2565 	nextpg = NULL;
   2566 	while (by_list || off < endoff) {
   2567 
   2568 		/*
   2569 		 * if the current page is not interesting, move on to the next.
   2570 		 */
   2571 
   2572 		KASSERT(pg == NULL || pg->uobject == uobj);
   2573 		KASSERT(pg == NULL ||
   2574 		    (pg->flags & (PG_RELEASED|PG_PAGEOUT)) == 0 ||
   2575 		    (pg->flags & PG_BUSY) != 0);
   2576 		writeinprog = (pg->flags & PG_BUSY) &&
   2577 			      !(pg->flags & PG_CLEAN && !pmap_is_modified(pg));
   2578 		if (by_list) {
   2579 			if (pg == &endmp) {
   2580 				break;
   2581 			}
   2582 			if (pg->offset < startoff || pg->offset >= endoff ||
   2583 			    pg->flags & (PG_RELEASED|PG_PAGEOUT) ||
   2584 			    writeinprog) {
   2585 				if (pg->offset >= startoff &&
   2586 				    pg->offset < endoff) {
   2587 					wasclean = false;
   2588 				}
   2589 				pg = TAILQ_NEXT(pg, listq);
   2590 				continue;
   2591 			}
   2592 			off = pg->offset;
   2593 		} else if (pg == NULL || pg->flags & (PG_RELEASED|PG_PAGEOUT) ||
   2594 			   writeinprog) {
   2595 			if (pg != NULL) {
   2596 				wasclean = false;
   2597 			}
   2598 			off += PAGE_SIZE;
   2599 			if (off < endoff) {
   2600 				pg = uvm_pagelookup(uobj, off);
   2601 			}
   2602 			continue;
   2603 		}
   2604 
   2605 		/*
   2606 		 * if the current page needs to be cleaned and it's busy,
   2607 		 * wait for it to become unbusy.
   2608 		 */
   2609 
   2610 		yld = (l->l_cpu->ci_schedstate.spc_flags &
   2611 		    SPCF_SHOULDYIELD) && !pagedaemon;
   2612 		if (pg->flags & PG_BUSY || yld) {
   2613 			UVMHIST_LOG(ubchist, "busy %p", pg,0,0,0);
   2614 			if (pg->flags & PG_BUSY) {
   2615                                 *busypg = pg;
   2616 				UVMHIST_LOG(ubchist, "busyfail %p", pg, 0,0,0);
   2617 				error = EDEADLK;
   2618 				break;
   2619 			}
   2620 			KASSERT(!pagedaemon);
   2621 			if (by_list) {
   2622 				TAILQ_INSERT_BEFORE(pg, &curmp, listq);
   2623 				UVMHIST_LOG(ubchist, "curmp next %p",
   2624 				    TAILQ_NEXT(&curmp, listq), 0,0,0);
   2625 			}
   2626 			if (yld) {
   2627 				simple_unlock(slock);
   2628 				preempt();
   2629 				simple_lock(slock);
   2630 			} else {
   2631 				pg->flags |= PG_WANTED;
   2632 #ifdef UVM_PAGE_TRKOWN
   2633 				printf("sleeping on page %lld owned by pid %d.%d (%s)\n",
   2634 					(long long)pg->offset, pg->owner, pg->lowner, pg->owner_tag);
   2635 #endif
   2636 				UVM_UNLOCK_AND_WAIT(pg, slock, 0, "genput", 0);
   2637 				simple_lock(slock);
   2638 			}
   2639 			if (by_list) {
   2640 				UVMHIST_LOG(ubchist, "after next %p",
   2641 				    TAILQ_NEXT(&curmp, listq), 0,0,0);
   2642 				pg = TAILQ_NEXT(&curmp, listq);
   2643 				TAILQ_REMOVE(&uobj->memq, &curmp, listq);
   2644 			} else {
   2645 				pg = uvm_pagelookup(uobj, off);
   2646 			}
   2647 			continue;
   2648 		}
   2649 
   2650 		/*
   2651 		 * if we're freeing, remove all mappings of the page now.
   2652 		 * if we're cleaning, check if the page is needs to be cleaned.
   2653 		 */
   2654 
   2655 		if (flags & PGO_FREE) {
   2656 			pmap_page_protect(pg, VM_PROT_NONE);
   2657 		} else if (flags & PGO_CLEANIT) {
   2658 
   2659 			/*
   2660 			 * if we still have some hope to pull this vnode off
   2661 			 * from the syncer queue, write-protect the page.
   2662 			 */
   2663 
   2664 			if (cleanall && wasclean &&
   2665 			    gp->g_dirtygen == dirtygen) {
   2666 
   2667 				/*
   2668 				 * uobj pages get wired only by uvm_fault
   2669 				 * where uobj is locked.
   2670 				 */
   2671 
   2672 				if (pg->wire_count == 0) {
   2673 					pmap_page_protect(pg,
   2674 					    VM_PROT_READ|VM_PROT_EXECUTE);
   2675 				} else {
   2676 					cleanall = false;
   2677 				}
   2678 			}
   2679 		}
   2680 
   2681 		if (flags & PGO_CLEANIT) {
   2682 			needs_clean = pmap_clear_modify(pg) ||
   2683 			    (pg->flags & PG_CLEAN) == 0;
   2684 			pg->flags |= PG_CLEAN;
   2685 		} else {
   2686 			needs_clean = false;
   2687 		}
   2688 
   2689 		/*
   2690 		 * if we're cleaning, build a cluster.
   2691 		 * the cluster will consist of pages which are currently dirty,
   2692 		 * but they will be returned to us marked clean.
   2693 		 * if not cleaning, just operate on the one page.
   2694 		 */
   2695 
   2696 		if (needs_clean) {
   2697 			KDASSERT((vp->v_flag & VONWORKLST));
   2698 			wasclean = false;
   2699 			memset(pgs, 0, sizeof(pgs));
   2700 			pg->flags |= PG_BUSY;
   2701 			UVM_PAGE_OWN(pg, "genfs_putpages");
   2702 
   2703 			/*
   2704 			 * first look backward.
   2705 			 */
   2706 
   2707 			npages = MIN(maxpages >> 1, off >> PAGE_SHIFT);
   2708 			nback = npages;
   2709 			uvn_findpages(uobj, off - PAGE_SIZE, &nback, &pgs[0],
   2710 			    UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY|UFP_BACKWARD);
   2711 			if (nback) {
   2712 				memmove(&pgs[0], &pgs[npages - nback],
   2713 				    nback * sizeof(pgs[0]));
   2714 				if (npages - nback < nback)
   2715 					memset(&pgs[nback], 0,
   2716 					    (npages - nback) * sizeof(pgs[0]));
   2717 				else
   2718 					memset(&pgs[npages - nback], 0,
   2719 					    nback * sizeof(pgs[0]));
   2720 			}
   2721 
   2722 			/*
   2723 			 * then plug in our page of interest.
   2724 			 */
   2725 
   2726 			pgs[nback] = pg;
   2727 
   2728 			/*
   2729 			 * then look forward to fill in the remaining space in
   2730 			 * the array of pages.
   2731 			 */
   2732 
   2733 			npages = maxpages - nback - 1;
   2734 			uvn_findpages(uobj, off + PAGE_SIZE, &npages,
   2735 			    &pgs[nback + 1],
   2736 			    UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY);
   2737 			npages += nback + 1;
   2738 		} else {
   2739 			pgs[0] = pg;
   2740 			npages = 1;
   2741 			nback = 0;
   2742 		}
   2743 
   2744 		/*
   2745 		 * apply FREE or DEACTIVATE options if requested.
   2746 		 */
   2747 
   2748 		if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
   2749 			uvm_lock_pageq();
   2750 		}
   2751 		for (i = 0; i < npages; i++) {
   2752 			tpg = pgs[i];
   2753 			KASSERT(tpg->uobject == uobj);
   2754 			if (by_list && tpg == TAILQ_NEXT(pg, listq))
   2755 				pg = tpg;
   2756 			if (tpg->offset < startoff || tpg->offset >= endoff)
   2757 				continue;
   2758 			if (flags & PGO_DEACTIVATE && tpg->wire_count == 0) {
   2759 				(void) pmap_clear_reference(tpg);
   2760 				uvm_pagedeactivate(tpg);
   2761 			} else if (flags & PGO_FREE) {
   2762 				pmap_page_protect(tpg, VM_PROT_NONE);
   2763 				if (tpg->flags & PG_BUSY) {
   2764 					tpg->flags |= freeflag;
   2765 					if (pagedaemon) {
   2766 						uvmexp.paging++;
   2767 						uvm_pagedequeue(tpg);
   2768 					}
   2769 				} else {
   2770 
   2771 					/*
   2772 					 * ``page is not busy''
   2773 					 * implies that npages is 1
   2774 					 * and needs_clean is false.
   2775 					 */
   2776 
   2777 					nextpg = TAILQ_NEXT(tpg, listq);
   2778 					uvm_pagefree(tpg);
   2779 					if (pagedaemon)
   2780 						uvmexp.pdfreed++;
   2781 				}
   2782 			}
   2783 		}
   2784 		if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
   2785 			uvm_unlock_pageq();
   2786 		}
   2787 		if (needs_clean) {
   2788 			modified = true;
   2789 
   2790 			/*
   2791 			 * start the i/o.  if we're traversing by list,
   2792 			 * keep our place in the list with a marker page.
   2793 			 */
   2794 
   2795 			if (by_list) {
   2796 				TAILQ_INSERT_AFTER(&uobj->memq, pg, &curmp,
   2797 				    listq);
   2798 			}
   2799 			simple_unlock(slock);
   2800 			error = GOP_WRITE(vp, pgs, npages, flags);
   2801 			simple_lock(slock);
   2802 			if (by_list) {
   2803 				pg = TAILQ_NEXT(&curmp, listq);
   2804 				TAILQ_REMOVE(&uobj->memq, &curmp, listq);
   2805 			}
   2806 			if (error) {
   2807 				break;
   2808 			}
   2809 			if (by_list) {
   2810 				continue;
   2811 			}
   2812 		}
   2813 
   2814 		/*
   2815 		 * find the next page and continue if there was no error.
   2816 		 */
   2817 
   2818 		if (by_list) {
   2819 			if (nextpg) {
   2820 				pg = nextpg;
   2821 				nextpg = NULL;
   2822 			} else {
   2823 				pg = TAILQ_NEXT(pg, listq);
   2824 			}
   2825 		} else {
   2826 			off += (npages - nback) << PAGE_SHIFT;
   2827 			if (off < endoff) {
   2828 				pg = uvm_pagelookup(uobj, off);
   2829 			}
   2830 		}
   2831 	}
   2832 	if (by_list) {
   2833 		TAILQ_REMOVE(&uobj->memq, &endmp, listq);
   2834 		PRELE(l);
   2835 	}
   2836 
   2837 	if (modified && (vp->v_flag & VWRITEMAPDIRTY) != 0 &&
   2838 	    (vp->v_type != VBLK ||
   2839 	    (vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)) {
   2840 		GOP_MARKUPDATE(vp, GOP_UPDATE_MODIFIED);
   2841 	}
   2842 
   2843 	/*
   2844 	 * if we're cleaning and there was nothing to clean,
   2845 	 * take us off the syncer list.  if we started any i/o
   2846 	 * and we're doing sync i/o, wait for all writes to finish.
   2847 	 */
   2848 
   2849 	s = splbio();
   2850 	if (cleanall && wasclean && gp->g_dirtygen == dirtygen &&
   2851 	    (vp->v_flag & VONWORKLST) != 0) {
   2852 		vp->v_flag &= ~VWRITEMAPDIRTY;
   2853 		if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL)
   2854 			vn_syncer_remove_from_worklist(vp);
   2855 	}
   2856 	splx(s);
   2857 
   2858 #if !defined(DEBUG)
   2859 skip_scan:
   2860 #endif /* !defined(DEBUG) */
   2861 	if (!wasclean && !async) {
   2862 		s = splbio();
   2863 		/*
   2864 		 * XXX - we want simple_unlock(&global_v_numoutput_slock);
   2865 		 *	 but the slot in ltsleep() is taken!
   2866 		 * XXX - try to recover from missed wakeups with a timeout..
   2867 		 *	 must think of something better.
   2868 		 */
   2869 		while (vp->v_numoutput != 0) {
   2870 			vp->v_flag |= VBWAIT;
   2871 			UVM_UNLOCK_AND_WAIT(&vp->v_numoutput, slock, false,
   2872 			    "genput2", hz);
   2873 			simple_lock(slock);
   2874 		}
   2875 		splx(s);
   2876 	}
   2877 	simple_unlock(slock);
   2878 
   2879 	if (has_trans)
   2880 		fstrans_done(vp->v_mount);
   2881 
   2882 	return (error);
   2883 }
   2884