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