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