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