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