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