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