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