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