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