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