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