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