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