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