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