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