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