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