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