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