Home | History | Annotate | Line # | Download | only in lfs
lfs_vfsops.c revision 1.163
      1 /*	$NetBSD: lfs_vfsops.c,v 1.163 2005/02/26 05:40:42 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) 1989, 1991, 1993, 1994
     40  *	The Regents of the University of California.  All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. Neither the name of the University nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  * SUCH DAMAGE.
     65  *
     66  *	@(#)lfs_vfsops.c	8.20 (Berkeley) 6/10/95
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: lfs_vfsops.c,v 1.163 2005/02/26 05:40:42 perseant Exp $");
     71 
     72 #if defined(_KERNEL_OPT)
     73 #include "opt_quota.h"
     74 #endif
     75 
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/namei.h>
     79 #include <sys/proc.h>
     80 #include <sys/kernel.h>
     81 #include <sys/vnode.h>
     82 #include <sys/mount.h>
     83 #include <sys/kthread.h>
     84 #include <sys/buf.h>
     85 #include <sys/device.h>
     86 #include <sys/mbuf.h>
     87 #include <sys/file.h>
     88 #include <sys/disklabel.h>
     89 #include <sys/ioctl.h>
     90 #include <sys/errno.h>
     91 #include <sys/malloc.h>
     92 #include <sys/pool.h>
     93 #include <sys/socket.h>
     94 #include <uvm/uvm_extern.h>
     95 #include <sys/sysctl.h>
     96 #include <sys/conf.h>
     97 
     98 #include <miscfs/specfs/specdev.h>
     99 
    100 #include <ufs/ufs/quota.h>
    101 #include <ufs/ufs/inode.h>
    102 #include <ufs/ufs/ufsmount.h>
    103 #include <ufs/ufs/ufs_extern.h>
    104 
    105 #include <uvm/uvm.h>
    106 #include <uvm/uvm_stat.h>
    107 #include <uvm/uvm_pager.h>
    108 #include <uvm/uvm_pdaemon.h>
    109 
    110 #include <ufs/lfs/lfs.h>
    111 #include <ufs/lfs/lfs_extern.h>
    112 
    113 #include <miscfs/genfs/genfs.h>
    114 #include <miscfs/genfs/genfs_node.h>
    115 
    116 static int lfs_gop_write(struct vnode *, struct vm_page **, int, int);
    117 static boolean_t lfs_issequential_hole(const struct ufsmount *,
    118     daddr_t, daddr_t);
    119 
    120 static int lfs_mountfs(struct vnode *, struct mount *, struct proc *);
    121 static daddr_t check_segsum(struct lfs *, daddr_t, u_int64_t,
    122     struct ucred *, int, int *, struct proc *);
    123 
    124 extern const struct vnodeopv_desc lfs_vnodeop_opv_desc;
    125 extern const struct vnodeopv_desc lfs_specop_opv_desc;
    126 extern const struct vnodeopv_desc lfs_fifoop_opv_desc;
    127 
    128 pid_t lfs_writer_daemon = 0;
    129 int lfs_do_flush = 0;
    130 
    131 const struct vnodeopv_desc * const lfs_vnodeopv_descs[] = {
    132 	&lfs_vnodeop_opv_desc,
    133 	&lfs_specop_opv_desc,
    134 	&lfs_fifoop_opv_desc,
    135 	NULL,
    136 };
    137 
    138 struct vfsops lfs_vfsops = {
    139 	MOUNT_LFS,
    140 	lfs_mount,
    141 	ufs_start,
    142 	lfs_unmount,
    143 	ufs_root,
    144 	ufs_quotactl,
    145 	lfs_statvfs,
    146 	lfs_sync,
    147 	lfs_vget,
    148 	lfs_fhtovp,
    149 	lfs_vptofh,
    150 	lfs_init,
    151 	lfs_reinit,
    152 	lfs_done,
    153 	NULL,
    154 	lfs_mountroot,
    155 	ufs_check_export,
    156 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    157 	vfs_stdextattrctl,
    158 	lfs_vnodeopv_descs,
    159 };
    160 
    161 struct genfs_ops lfs_genfsops = {
    162 	lfs_gop_size,
    163 	ufs_gop_alloc,
    164 	lfs_gop_write,
    165 };
    166 
    167 /*
    168  * XXX Same structure as FFS inodes?  Should we share a common pool?
    169  */
    170 POOL_INIT(lfs_inode_pool, sizeof(struct inode), 0, 0, 0, "lfsinopl",
    171     &pool_allocator_nointr);
    172 POOL_INIT(lfs_dinode_pool, sizeof(struct ufs1_dinode), 0, 0, 0, "lfsdinopl",
    173     &pool_allocator_nointr);
    174 POOL_INIT(lfs_inoext_pool, sizeof(struct lfs_inode_ext), 8, 0, 0, "lfsinoextpl",
    175     &pool_allocator_nointr);
    176 POOL_INIT(lfs_lbnentry_pool, sizeof(struct lbnentry), 0, 0, 0, "lfslbnpool",
    177     &pool_allocator_nointr);
    178 
    179 /*
    180  * The writer daemon.  UVM keeps track of how many dirty pages we are holding
    181  * in lfs_subsys_pages; the daemon flushes the filesystem when this value
    182  * crosses the (user-defined) threshhold LFS_MAX_PAGES.
    183  */
    184 static void
    185 lfs_writerd(void *arg)
    186 {
    187 #ifdef LFS_PD
    188 	struct mount *mp, *nmp;
    189 	struct lfs *fs;
    190 #endif
    191 
    192 	lfs_writer_daemon = curproc->p_pid;
    193 
    194 	simple_lock(&lfs_subsys_lock);
    195 	for (;;) {
    196 		ltsleep(&lfs_writer_daemon, PVM | PNORELOCK, "lfswriter", 0,
    197 		    &lfs_subsys_lock);
    198 
    199 #ifdef LFS_PD
    200 		/*
    201 		 * Look through the list of LFSs to see if any of them
    202 		 * have requested pageouts.
    203 		 */
    204 		simple_lock(&mountlist_slock);
    205 		for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
    206 		     mp = nmp) {
    207 			if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    208 				nmp = CIRCLEQ_NEXT(mp, mnt_list);
    209 				continue;
    210 			}
    211 			if (strncmp(&mp->mnt_stat.f_fstypename[0], MOUNT_LFS,
    212 				    MFSNAMELEN) == 0) {
    213 				fs = VFSTOUFS(mp)->um_lfs;
    214 				if (fs->lfs_pdflush ||
    215 				    !TAILQ_EMPTY(&fs->lfs_pchainhd)) {
    216 #ifdef DEBUG_LFS_FLUSH
    217 					printf("daemon: pdflush set\n");
    218 #endif
    219 					fs->lfs_pdflush = 0;
    220 					lfs_flush_fs(fs, 0);
    221 				}
    222 			}
    223 
    224 			simple_lock(&mountlist_slock);
    225 			nmp = CIRCLEQ_NEXT(mp, mnt_list);
    226 			vfs_unbusy(mp);
    227 		}
    228 		simple_unlock(&mountlist_slock);
    229 #endif /* LFS_PD */
    230 
    231 		/*
    232 		 * If global state wants a flush, flush everything.
    233 		 */
    234 		simple_lock(&lfs_subsys_lock);
    235 		while (lfs_do_flush || locked_queue_count > LFS_MAX_BUFS ||
    236 			locked_queue_bytes > LFS_MAX_BYTES ||
    237 			lfs_subsys_pages > LFS_MAX_PAGES) {
    238 
    239 #ifdef DEBUG_LFS_FLUSH
    240 			if (lfs_do_flush)
    241 				printf("daemon: lfs_do_flush\n");
    242 			if (locked_queue_count > LFS_MAX_BUFS)
    243 				printf("daemon: lqc = %d, max %d\n",
    244 					locked_queue_count, LFS_MAX_BUFS);
    245 			if (locked_queue_bytes > LFS_MAX_BYTES)
    246 				printf("daemon: lqb = %ld, max %ld\n",
    247 					locked_queue_bytes, LFS_MAX_BYTES);
    248 			if (lfs_subsys_pages > LFS_MAX_PAGES)
    249 				printf("daemon: lssp = %d, max %d\n",
    250 					lfs_subsys_pages, LFS_MAX_PAGES);
    251 #endif /* DEBUG_LFS_FLUSH */
    252 			lfs_flush(NULL, SEGM_WRITERD, 0);
    253 			lfs_do_flush = 0;
    254 		}
    255 	}
    256 	/* NOTREACHED */
    257 }
    258 
    259 /*
    260  * Initialize the filesystem, most work done by ufs_init.
    261  */
    262 void
    263 lfs_init()
    264 {
    265 #ifdef _LKM
    266 	malloc_type_attach(M_SEGMENT);
    267 	pool_init(&lfs_inode_pool, sizeof(struct inode), 0, 0, 0,
    268 	    "lfsinopl", &pool_allocator_nointr);
    269 	pool_init(&lfs_dinode_pool, sizeof(struct ufs1_dinode), 0, 0, 0,
    270 	    "lfsdinopl", &pool_allocator_nointr);
    271 	pool_init(&lfs_inoext_pool, sizeof(struct lfs_inode_ext), 8, 0, 0,
    272 	    "lfsinoextpl", &pool_allocator_nointr);
    273 	pool_init(&lfs_lbnentry_pool, sizeof(struct lbnentry), 0, 0, 0,
    274 	    "lfslbnpool", &pool_allocator_nointr);
    275 #endif
    276 	ufs_init();
    277 
    278 #ifdef DEBUG
    279 	memset(lfs_log, 0, sizeof(lfs_log));
    280 #endif
    281 	simple_lock_init(&lfs_subsys_lock);
    282 }
    283 
    284 void
    285 lfs_reinit()
    286 {
    287 	ufs_reinit();
    288 }
    289 
    290 void
    291 lfs_done()
    292 {
    293 	ufs_done();
    294 #ifdef _LKM
    295 	pool_destroy(&lfs_inode_pool);
    296 	pool_destroy(&lfs_dinode_pool);
    297 	pool_destroy(&lfs_inoext_pool);
    298 	malloc_type_detach(M_SEGMENT);
    299 #endif
    300 }
    301 
    302 /*
    303  * Called by main() when ufs is going to be mounted as root.
    304  */
    305 int
    306 lfs_mountroot()
    307 {
    308 	extern struct vnode *rootvp;
    309 	struct mount *mp;
    310 	struct proc *p = curproc;	/* XXX */
    311 	int error;
    312 
    313 	if (root_device->dv_class != DV_DISK)
    314 		return (ENODEV);
    315 
    316 	if (rootdev == NODEV)
    317 		return (ENODEV);
    318 	if ((error = vfs_rootmountalloc(MOUNT_LFS, "root_device", &mp))) {
    319 		vrele(rootvp);
    320 		return (error);
    321 	}
    322 	if ((error = lfs_mountfs(rootvp, mp, p))) {
    323 		mp->mnt_op->vfs_refcount--;
    324 		vfs_unbusy(mp);
    325 		free(mp, M_MOUNT);
    326 		return (error);
    327 	}
    328 	simple_lock(&mountlist_slock);
    329 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    330 	simple_unlock(&mountlist_slock);
    331 	(void)lfs_statvfs(mp, &mp->mnt_stat, p);
    332 	vfs_unbusy(mp);
    333 	setrootfstime((time_t)(VFSTOUFS(mp)->um_lfs->lfs_tstamp));
    334 	return (0);
    335 }
    336 
    337 /*
    338  * VFS Operations.
    339  *
    340  * mount system call
    341  */
    342 int
    343 lfs_mount(struct mount *mp, const char *path, void *data, struct nameidata *ndp, struct proc *p)
    344 {
    345 	struct vnode *devvp;
    346 	struct ufs_args args;
    347 	struct ufsmount *ump = NULL;
    348 	struct lfs *fs = NULL;				/* LFS */
    349 	int error, update;
    350 	mode_t accessmode;
    351 
    352 	if (mp->mnt_flag & MNT_GETARGS) {
    353 		ump = VFSTOUFS(mp);
    354 		if (ump == NULL)
    355 			return EIO;
    356 		args.fspec = NULL;
    357 		vfs_showexport(mp, &args.export, &ump->um_export);
    358 		return copyout(&args, data, sizeof(args));
    359 	}
    360 	error = copyin(data, &args, sizeof (struct ufs_args));
    361 	if (error)
    362 		return (error);
    363 
    364 	update = mp->mnt_flag & MNT_UPDATE;
    365 
    366 	/* Check arguments */
    367 	if (args.fspec != NULL) {
    368 		/*
    369 		 * Look up the name and verify that it's sane.
    370 		 */
    371 		NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
    372 		if ((error = namei(ndp)) != 0)
    373 			return (error);
    374 		devvp = ndp->ni_vp;
    375 
    376 		if (!update) {
    377 			/*
    378 			 * Be sure this is a valid block device
    379 			 */
    380 			if (devvp->v_type != VBLK)
    381 				error = ENOTBLK;
    382 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
    383 				error = ENXIO;
    384 		} else {
    385 			/*
    386 			 * Be sure we're still naming the same device
    387 			 * used for our initial mount
    388 			 */
    389 			ump = VFSTOUFS(mp);
    390 			if (devvp != ump->um_devvp)
    391 				error = EINVAL;
    392 		}
    393 	} else {
    394 		if (!update) {
    395 			/* New mounts must have a filename for the device */
    396 			return (EINVAL);
    397 		} else {
    398 			/* Use the extant mount */
    399 			ump = VFSTOUFS(mp);
    400 			devvp = ump->um_devvp;
    401 			vref(devvp);
    402 		}
    403 	}
    404 
    405 
    406 	/*
    407 	 * If mount by non-root, then verify that user has necessary
    408 	 * permissions on the device.
    409 	 */
    410 	if (error == 0 && p->p_ucred->cr_uid != 0) {
    411 		accessmode = VREAD;
    412 		if (update ?
    413 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
    414 		    (mp->mnt_flag & MNT_RDONLY) == 0)
    415 			accessmode |= VWRITE;
    416 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    417 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
    418 		VOP_UNLOCK(devvp, 0);
    419 	}
    420 
    421 	if (error) {
    422 		vrele(devvp);
    423 		return (error);
    424 	}
    425 
    426 	if (!update) {
    427 		int flags;
    428 
    429 		/*
    430 		 * Disallow multiple mounts of the same device.
    431 		 * Disallow mounting of a device that is currently in use
    432 		 * (except for root, which might share swap device for
    433 		 * miniroot).
    434 		 */
    435 		error = vfs_mountedon(devvp);
    436 		if (error)
    437 			goto fail;
    438 		if (vcount(devvp) > 1 && devvp != rootvp) {
    439 			error = EBUSY;
    440 			goto fail;
    441 		}
    442 		if (mp->mnt_flag & MNT_RDONLY)
    443 			flags = FREAD;
    444 		else
    445 			flags = FREAD|FWRITE;
    446 		error = VOP_OPEN(devvp, flags, FSCRED, p);
    447 		if (error)
    448 			goto fail;
    449 		error = lfs_mountfs(devvp, mp, p);		/* LFS */
    450 		if (error) {
    451 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    452 			(void)VOP_CLOSE(devvp, flags, NOCRED, p);
    453 			VOP_UNLOCK(devvp, 0);
    454 			goto fail;
    455 		}
    456 
    457 		ump = VFSTOUFS(mp);
    458 		fs = ump->um_lfs;
    459 	} else {
    460 		/*
    461 		 * Update the mount.
    462 		 */
    463 
    464 		/*
    465 		 * The initial mount got a reference on this
    466 		 * device, so drop the one obtained via
    467 		 * namei(), above.
    468 		 */
    469 		vrele(devvp);
    470 
    471 		ump = VFSTOUFS(mp);
    472 		fs = ump->um_lfs;
    473 		if (fs->lfs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
    474 			/*
    475 			 * Changing from read-only to read/write
    476 			 */
    477 			fs->lfs_ronly = 0;
    478 		}
    479 		if (args.fspec == 0) {
    480 			/*
    481 			 * Process export requests.
    482 			 */
    483 			return (vfs_export(mp, &ump->um_export, &args.export));
    484 		}
    485 	}
    486 
    487 	return set_statvfs_info(path, UIO_USERSPACE, args.fspec,
    488 	    UIO_USERSPACE, mp, p);
    489 
    490 fail:
    491 	vrele(devvp);
    492 	return (error);
    493 }
    494 
    495 /*
    496  * Roll-forward code.
    497  */
    498 
    499 /*
    500  * Load the appropriate indirect block, and change the appropriate pointer.
    501  * Mark the block dirty.  Do segment and avail accounting.
    502  */
    503 static int
    504 update_meta(struct lfs *fs, ino_t ino, int version, daddr_t lbn,
    505 	    daddr_t ndaddr, size_t size, struct proc *p)
    506 {
    507 	int error;
    508 	struct vnode *vp;
    509 	struct inode *ip;
    510 #ifdef DEBUG_LFS_RFW
    511 	daddr_t odaddr;
    512 	struct indir a[NIADDR];
    513 	int num;
    514 	int i;
    515 #endif /* DEBUG_LFS_RFW */
    516 	struct buf *bp;
    517 	SEGUSE *sup;
    518 
    519 	KASSERT(lbn >= 0);	/* no indirect blocks */
    520 
    521 	if ((error = lfs_rf_valloc(fs, ino, version, p, &vp)) != 0) {
    522 #ifdef DEBUG_LFS_RFW
    523 		printf("update_meta: ino %d: lfs_rf_valloc returned %d\n", ino,
    524 		       error);
    525 #endif /* DEBUG_LFS_RFW */
    526 		return error;
    527 	}
    528 
    529 	if ((error = VOP_BALLOC(vp, (lbn << fs->lfs_bshift), size,
    530 				NOCRED, 0, &bp)) != 0) {
    531 		vput(vp);
    532 		return (error);
    533 	}
    534 	/* No need to write, the block is already on disk */
    535 	if (bp->b_flags & B_DELWRI) {
    536 		LFS_UNLOCK_BUF(bp);
    537 		fs->lfs_avail += btofsb(fs, bp->b_bcount);
    538 	}
    539 	bp->b_flags |= B_INVAL;
    540 	brelse(bp);
    541 
    542 	/*
    543 	 * Extend the file, if it is not large enough already.
    544 	 * XXX this is not exactly right, we don't know how much of the
    545 	 * XXX last block is actually used.  We hope that an inode will
    546 	 * XXX appear later to give the correct size.
    547 	 */
    548 	ip = VTOI(vp);
    549 	if (ip->i_size <= (lbn << fs->lfs_bshift)) {
    550 		u_int64_t newsize;
    551 
    552 		if (lbn < NDADDR)
    553 			newsize = ip->i_ffs1_size = (lbn << fs->lfs_bshift) +
    554 				(size - fs->lfs_fsize) + 1;
    555 		else
    556 			newsize = ip->i_ffs1_size = (lbn << fs->lfs_bshift) + 1;
    557 
    558 		if (ip->i_size < newsize) {
    559 			ip->i_size = newsize;
    560 			/*
    561 			 * tell vm our new size for the case the inode won't
    562 			 * appear later.
    563 			 */
    564 			uvm_vnp_setsize(vp, newsize);
    565 		}
    566 	}
    567 
    568 	lfs_update_single(fs, NULL, vp, lbn, ndaddr, size);
    569 
    570 	LFS_SEGENTRY(sup, fs, dtosn(fs, ndaddr), bp);
    571 	sup->su_nbytes += size;
    572 	LFS_WRITESEGENTRY(sup, fs, dtosn(fs, ndaddr), bp);
    573 
    574 	/* differences here should be due to UNWRITTEN indirect blocks. */
    575 	KASSERT((lblkno(fs, ip->i_size) > NDADDR &&
    576 	    ip->i_lfs_effnblks == ip->i_ffs1_blocks) ||
    577 	    ip->i_lfs_effnblks >= ip->i_ffs1_blocks);
    578 
    579 #ifdef DEBUG_LFS_RFW
    580 	/* Now look again to make sure it worked */
    581 	ufs_bmaparray(vp, lbn, &odaddr, &a[0], &num, NULL, NULL);
    582 	for (i = num; i > 0; i--) {
    583 		if (!a[i].in_exists)
    584 			panic("update_meta: absent %d lv indirect block", i);
    585 	}
    586 	if (dbtofsb(fs, odaddr) != ndaddr)
    587 		printf("update_meta: failed setting ino %d lbn %" PRId64
    588 		    " to %" PRId64 "\n", ino, lbn, ndaddr);
    589 #endif /* DEBUG_LFS_RFW */
    590 	vput(vp);
    591 	return 0;
    592 }
    593 
    594 static int
    595 update_inoblk(struct lfs *fs, daddr_t offset, struct ucred *cred,
    596 	      struct proc *p)
    597 {
    598 	struct vnode *devvp, *vp;
    599 	struct inode *ip;
    600 	struct ufs1_dinode *dip;
    601 	struct buf *dbp, *ibp;
    602 	int error;
    603 	daddr_t daddr;
    604 	IFILE *ifp;
    605 	SEGUSE *sup;
    606 
    607 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
    608 
    609 	/*
    610 	 * Get the inode, update times and perms.
    611 	 * DO NOT update disk blocks, we do that separately.
    612 	 */
    613 	error = bread(devvp, fsbtodb(fs, offset), fs->lfs_ibsize, cred, &dbp);
    614 	if (error) {
    615 #ifdef DEBUG_LFS_RFW
    616 		printf("update_inoblk: bread returned %d\n", error);
    617 #endif
    618 		return error;
    619 	}
    620 	dip = ((struct ufs1_dinode *)(dbp->b_data)) + INOPB(fs);
    621 	while (--dip >= (struct ufs1_dinode *)dbp->b_data) {
    622 		if (dip->di_inumber > LFS_IFILE_INUM) {
    623 			/* printf("ino %d version %d\n", dip->di_inumber,
    624 			       dip->di_gen); */
    625 			error = lfs_rf_valloc(fs, dip->di_inumber, dip->di_gen,
    626 					      p, &vp);
    627 			if (error) {
    628 #ifdef DEBUG_LFS_RFW
    629 				printf("update_inoblk: lfs_rf_valloc returned %d\n", error);
    630 #endif
    631 				continue;
    632 			}
    633 			ip = VTOI(vp);
    634 			if (dip->di_size != ip->i_size)
    635 				VOP_TRUNCATE(vp, dip->di_size, 0, NOCRED, p);
    636 			/* Get mode, link count, size, and times */
    637 			memcpy(ip->i_din.ffs1_din, dip,
    638 			       offsetof(struct ufs1_dinode, di_db[0]));
    639 
    640 			/* Then the rest, except di_blocks */
    641 			ip->i_flags = ip->i_ffs1_flags = dip->di_flags;
    642 			ip->i_gen = ip->i_ffs1_gen = dip->di_gen;
    643 			ip->i_uid = ip->i_ffs1_uid = dip->di_uid;
    644 			ip->i_gid = ip->i_ffs1_gid = dip->di_gid;
    645 
    646 			ip->i_mode = ip->i_ffs1_mode;
    647 			ip->i_nlink = ip->i_ffs_effnlink = ip->i_ffs1_nlink;
    648 			ip->i_size = ip->i_ffs1_size;
    649 
    650 			LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
    651 
    652 			/* Re-initialize to get type right */
    653 			ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p,
    654 				  &vp);
    655 			vput(vp);
    656 
    657 			/* Record change in location */
    658 			LFS_IENTRY(ifp, fs, dip->di_inumber, ibp);
    659 			daddr = ifp->if_daddr;
    660 			ifp->if_daddr = dbtofsb(fs, dbp->b_blkno);
    661 			error = LFS_BWRITE_LOG(ibp); /* Ifile */
    662 			/* And do segment accounting */
    663 			if (dtosn(fs, daddr) != dtosn(fs, dbtofsb(fs, dbp->b_blkno))) {
    664 				if (daddr > 0) {
    665 					LFS_SEGENTRY(sup, fs, dtosn(fs, daddr),
    666 						     ibp);
    667 					sup->su_nbytes -= sizeof (struct ufs1_dinode);
    668 					LFS_WRITESEGENTRY(sup, fs,
    669 							  dtosn(fs, daddr),
    670 							  ibp);
    671 				}
    672 				LFS_SEGENTRY(sup, fs, dtosn(fs, dbtofsb(fs, dbp->b_blkno)),
    673 					     ibp);
    674 				sup->su_nbytes += sizeof (struct ufs1_dinode);
    675 				LFS_WRITESEGENTRY(sup, fs,
    676 						  dtosn(fs, dbtofsb(fs, dbp->b_blkno)),
    677 						  ibp);
    678 			}
    679 		}
    680 	}
    681 	dbp->b_flags |= B_AGE;
    682 	brelse(dbp);
    683 
    684 	return 0;
    685 }
    686 
    687 #define CHECK_CKSUM   0x0001  /* Check the checksum to make sure it's valid */
    688 #define CHECK_UPDATE  0x0002  /* Update Ifile for new data blocks / inodes */
    689 
    690 static daddr_t
    691 check_segsum(struct lfs *fs, daddr_t offset, u_int64_t nextserial,
    692 	     struct ucred *cred, int flags, int *pseg_flags, struct proc *p)
    693 {
    694 	struct vnode *devvp;
    695 	struct buf *bp, *dbp;
    696 	int error, nblocks = 0, ninos, i, j; /* XXX: gcc */
    697 	SEGSUM *ssp;
    698 	u_long *dp = NULL, *datap = NULL; /* XXX u_int32_t */
    699 	daddr_t oldoffset;
    700 	int32_t *iaddr;	/* XXX ondisk32 */
    701 	FINFO *fip;
    702 	SEGUSE *sup;
    703 	size_t size;
    704 
    705 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
    706 	/*
    707 	 * If the segment has a superblock and we're at the top
    708 	 * of the segment, skip the superblock.
    709 	 */
    710 	if (sntod(fs, dtosn(fs, offset)) == offset) {
    711 		LFS_SEGENTRY(sup, fs, dtosn(fs, offset), bp);
    712 		if (sup->su_flags & SEGUSE_SUPERBLOCK)
    713 			offset += btofsb(fs, LFS_SBPAD);
    714 		brelse(bp);
    715 	}
    716 
    717 	/* Read in the segment summary */
    718 	error = bread(devvp, fsbtodb(fs, offset), fs->lfs_sumsize, cred, &bp);
    719 	if (error)
    720 		return -1;
    721 
    722 	/* Check summary checksum */
    723 	ssp = (SEGSUM *)bp->b_data;
    724 	if (flags & CHECK_CKSUM) {
    725 		if (ssp->ss_sumsum != cksum(&ssp->ss_datasum,
    726 					   fs->lfs_sumsize -
    727 					   sizeof(ssp->ss_sumsum))) {
    728 #ifdef DEBUG_LFS_RFW
    729 			printf("Sumsum error at 0x%" PRIx64 "\n", offset);
    730 #endif
    731 			offset = -1;
    732 			goto err1;
    733 		}
    734 		if (ssp->ss_nfinfo == 0 && ssp->ss_ninos == 0) {
    735 #ifdef DEBUG_LFS_RFW
    736 			printf("Empty pseg at 0x%" PRIx64 "\n", offset);
    737 #endif
    738 			offset = -1;
    739 			goto err1;
    740 		}
    741 		if (ssp->ss_create < fs->lfs_tstamp) {
    742 #ifdef DEBUG_LFS_RFW
    743 			printf("Old data at 0x%" PRIx64 "\n", offset);
    744 #endif
    745 			offset = -1;
    746 			goto err1;
    747 		}
    748 	}
    749 	if (fs->lfs_version > 1) {
    750 		if (ssp->ss_serial != nextserial) {
    751 #ifdef DEBUG_LFS_RFW
    752 			printf("Unexpected serial number at 0x%" PRIx64
    753 			    "\n", offset);
    754 #endif
    755 			offset = -1;
    756 			goto err1;
    757 		}
    758 		if (ssp->ss_ident != fs->lfs_ident) {
    759 #ifdef DEBUG_LFS_RFW
    760 			printf("Incorrect fsid (0x%x vs 0x%x) at 0x%"
    761 			    PRIx64 "\n", ssp->ss_ident, fs->lfs_ident, offset);
    762 #endif
    763 			offset = -1;
    764 			goto err1;
    765 		}
    766 	}
    767 	if (pseg_flags)
    768 		*pseg_flags = ssp->ss_flags;
    769 	oldoffset = offset;
    770 	offset += btofsb(fs, fs->lfs_sumsize);
    771 
    772 	ninos = howmany(ssp->ss_ninos, INOPB(fs));
    773 	/* XXX ondisk32 */
    774 	iaddr = (int32_t *)(bp->b_data + fs->lfs_sumsize - sizeof(int32_t));
    775 	if (flags & CHECK_CKSUM) {
    776 		/* Count blocks */
    777 		nblocks = 0;
    778 		fip = (FINFO *)(bp->b_data + SEGSUM_SIZE(fs));
    779 		for (i = 0; i < ssp->ss_nfinfo; ++i) {
    780 			nblocks += fip->fi_nblocks;
    781 			if (fip->fi_nblocks <= 0)
    782 				break;
    783 			/* XXX ondisk32 */
    784 			fip = (FINFO *)(((char *)fip) + FINFOSIZE +
    785 					(fip->fi_nblocks * sizeof(int32_t)));
    786 		}
    787 		nblocks += ninos;
    788 		/* Create the sum array */
    789 		datap = dp = (u_long *)malloc(nblocks * sizeof(u_long),
    790 					      M_SEGMENT, M_WAITOK);
    791 	}
    792 
    793 	/* Handle individual blocks */
    794 	fip = (FINFO *)(bp->b_data + SEGSUM_SIZE(fs));
    795 	for (i = 0; i < ssp->ss_nfinfo || ninos; ++i) {
    796 		/* Inode block? */
    797 		if (ninos && *iaddr == offset) {
    798 			if (flags & CHECK_CKSUM) {
    799 				/* Read in the head and add to the buffer */
    800 				error = bread(devvp, fsbtodb(fs, offset), fs->lfs_bsize,
    801 					      cred, &dbp);
    802 				if (error) {
    803 					offset = -1;
    804 					goto err2;
    805 				}
    806 				(*dp++) = ((u_long *)(dbp->b_data))[0];
    807 				dbp->b_flags |= B_AGE;
    808 				brelse(dbp);
    809 			}
    810 			if (flags & CHECK_UPDATE) {
    811 				if ((error = update_inoblk(fs, offset, cred, p))
    812 				    != 0) {
    813 					offset = -1;
    814 					goto err2;
    815 				}
    816 			}
    817 			offset += btofsb(fs, fs->lfs_ibsize);
    818 			--iaddr;
    819 			--ninos;
    820 			--i; /* compensate */
    821 			continue;
    822 		}
    823 		/* printf("check: blocks from ino %d version %d\n",
    824 		       fip->fi_ino, fip->fi_version); */
    825 		size = fs->lfs_bsize;
    826 		for (j = 0; j < fip->fi_nblocks; ++j) {
    827 			if (j == fip->fi_nblocks - 1)
    828 				size = fip->fi_lastlength;
    829 			if (flags & CHECK_CKSUM) {
    830 				error = bread(devvp, fsbtodb(fs, offset), size, cred, &dbp);
    831 				if (error) {
    832 					offset = -1;
    833 					goto err2;
    834 				}
    835 				(*dp++) = ((u_long *)(dbp->b_data))[0];
    836 				dbp->b_flags |= B_AGE;
    837 				brelse(dbp);
    838 			}
    839 			/* Account for and update any direct blocks */
    840 			if ((flags & CHECK_UPDATE) &&
    841 			   fip->fi_ino > LFS_IFILE_INUM &&
    842 			   fip->fi_blocks[j] >= 0) {
    843 				update_meta(fs, fip->fi_ino, fip->fi_version,
    844 					    fip->fi_blocks[j], offset, size, p);
    845 			}
    846 			offset += btofsb(fs, size);
    847 		}
    848 		/* XXX ondisk32 */
    849 		fip = (FINFO *)(((char *)fip) + FINFOSIZE
    850 				+ fip->fi_nblocks * sizeof(int32_t));
    851 	}
    852 	/* Checksum the array, compare */
    853 	if ((flags & CHECK_CKSUM) &&
    854 	   ssp->ss_datasum != cksum(datap, nblocks * sizeof(u_long)))
    855 	{
    856 #ifdef DEBUG_LFS_RFW
    857 		printf("Datasum error at 0x%" PRIx64 " (wanted %x got %x)\n",
    858 		    offset, ssp->ss_datasum, cksum(datap, nblocks *
    859 					      sizeof(u_long)));
    860 #endif
    861 		offset = -1;
    862 		goto err2;
    863 	}
    864 
    865 	/* If we're at the end of the segment, move to the next */
    866 	if (dtosn(fs, offset + btofsb(fs, fs->lfs_sumsize + fs->lfs_bsize)) !=
    867 	   dtosn(fs, offset)) {
    868 		if (dtosn(fs, offset) == dtosn(fs, ssp->ss_next)) {
    869 			offset = -1;
    870 			goto err2;
    871 		}
    872 		offset = ssp->ss_next;
    873 #ifdef DEBUG_LFS_RFW
    874 		printf("LFS roll forward: moving on to offset 0x%" PRIx64
    875 		       " -> segment %d\n", offset, dtosn(fs,offset));
    876 #endif
    877 	}
    878 
    879 	if (flags & CHECK_UPDATE) {
    880 		fs->lfs_avail -= (offset - oldoffset);
    881 		/* Don't clog the buffer queue */
    882 		simple_lock(&lfs_subsys_lock);
    883 		if (locked_queue_count > LFS_MAX_BUFS ||
    884 		    locked_queue_bytes > LFS_MAX_BYTES) {
    885 			lfs_flush(fs, SEGM_CKP, 0);
    886 		}
    887 		simple_unlock(&lfs_subsys_lock);
    888 	}
    889 
    890     err2:
    891 	if (flags & CHECK_CKSUM)
    892 		free(datap, M_SEGMENT);
    893     err1:
    894 	bp->b_flags |= B_AGE;
    895 	brelse(bp);
    896 
    897 	/* XXX should we update the serial number even for bad psegs? */
    898 	if ((flags & CHECK_UPDATE) && offset > 0 && fs->lfs_version > 1)
    899 		fs->lfs_serial = nextserial;
    900 	return offset;
    901 }
    902 
    903 /*
    904  * Common code for mount and mountroot
    905  * LFS specific
    906  */
    907 int
    908 lfs_mountfs(struct vnode *devvp, struct mount *mp, struct proc *p)
    909 {
    910 	struct dlfs *tdfs, *dfs, *adfs;
    911 	struct lfs *fs;
    912 	struct ufsmount *ump;
    913 	struct vnode *vp;
    914 	struct buf *bp, *abp;
    915 	struct partinfo dpart;
    916 	dev_t dev;
    917 	int error, i, ronly, secsize, fsbsize;
    918 	struct ucred *cred;
    919 	CLEANERINFO *cip;
    920 	SEGUSE *sup;
    921 	int flags, dirty, do_rollforward;
    922 	daddr_t offset, oldoffset, lastgoodpseg, sb_addr;
    923 	int sn, curseg;
    924 
    925 	cred = p ? p->p_ucred : NOCRED;
    926 
    927 	/*
    928 	 * Flush out any old buffers remaining from a previous use.
    929 	 */
    930 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    931 	error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0);
    932 	VOP_UNLOCK(devvp, 0);
    933 	if (error)
    934 		return (error);
    935 
    936 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    937 	if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, p) != 0)
    938 		secsize = DEV_BSIZE;
    939 	else
    940 		secsize = dpart.disklab->d_secsize;
    941 
    942 	/* Don't free random space on error. */
    943 	bp = NULL;
    944 	abp = NULL;
    945 	ump = NULL;
    946 
    947 	sb_addr = LFS_LABELPAD / secsize;
    948 	while (1) {
    949 		/* Read in the superblock. */
    950 		error = bread(devvp, sb_addr, LFS_SBPAD, cred, &bp);
    951 		if (error)
    952 			goto out;
    953 		dfs = (struct dlfs *)bp->b_data;
    954 
    955 		/* Check the basics. */
    956 		if (dfs->dlfs_magic != LFS_MAGIC || dfs->dlfs_bsize > MAXBSIZE ||
    957 		    dfs->dlfs_version > LFS_VERSION ||
    958 		    dfs->dlfs_bsize < sizeof(struct dlfs)) {
    959 #ifdef DEBUG_LFS
    960 			printf("lfs_mountfs: primary superblock sanity failed\n");
    961 #endif
    962 			error = EINVAL;		/* XXX needs translation */
    963 			goto out;
    964 		}
    965 		if (dfs->dlfs_inodefmt > LFS_MAXINODEFMT)
    966 			printf("lfs_mountfs: warning: unknown inode format %d\n",
    967 			       dfs->dlfs_inodefmt);
    968 
    969 		if (dfs->dlfs_version == 1)
    970 			fsbsize = secsize;
    971 		else {
    972 			fsbsize = 1 << (dfs->dlfs_bshift - dfs->dlfs_blktodb +
    973 				dfs->dlfs_fsbtodb);
    974 			/*
    975 			 * Could be, if the frag size is large enough, that we
    976 			 * don't have the "real" primary superblock.  If that's
    977 			 * the case, get the real one, and try again.
    978 			 */
    979 			if (sb_addr != dfs->dlfs_sboffs[0] <<
    980 				       dfs->dlfs_fsbtodb) {
    981 /* #ifdef DEBUG_LFS */
    982 				printf("lfs_mountfs: sb daddr 0x%llx is not right, trying 0x%llx\n",
    983 					(long long)sb_addr, (long long)(dfs->dlfs_sboffs[0] <<
    984 						 dfs->dlfs_fsbtodb));
    985 /* #endif */
    986 				sb_addr = dfs->dlfs_sboffs[0] <<
    987 					  dfs->dlfs_fsbtodb;
    988 				brelse(bp);
    989 				continue;
    990 			}
    991 		}
    992 		break;
    993 	}
    994 
    995 	/*
    996 	 * Check the second superblock to see which is newer; then mount
    997 	 * using the older of the two.	This is necessary to ensure that
    998 	 * the filesystem is valid if it was not unmounted cleanly.
    999 	 */
   1000 
   1001 	if (dfs->dlfs_sboffs[1] &&
   1002 	    dfs->dlfs_sboffs[1] - LFS_LABELPAD / fsbsize > LFS_SBPAD / fsbsize)
   1003 	{
   1004 		error = bread(devvp, dfs->dlfs_sboffs[1] * (fsbsize / secsize),
   1005 			LFS_SBPAD, cred, &abp);
   1006 		if (error)
   1007 			goto out;
   1008 		adfs = (struct dlfs *)abp->b_data;
   1009 
   1010 		if (dfs->dlfs_version == 1) {
   1011 			/* 1s resolution comparison */
   1012 			if (adfs->dlfs_tstamp < dfs->dlfs_tstamp)
   1013 				tdfs = adfs;
   1014 			else
   1015 				tdfs = dfs;
   1016 		} else {
   1017 			/* monotonic infinite-resolution comparison */
   1018 			if (adfs->dlfs_serial < dfs->dlfs_serial)
   1019 				tdfs = adfs;
   1020 			else
   1021 				tdfs = dfs;
   1022 		}
   1023 
   1024 		/* Check the basics. */
   1025 		if (tdfs->dlfs_magic != LFS_MAGIC ||
   1026 		    tdfs->dlfs_bsize > MAXBSIZE ||
   1027 		    tdfs->dlfs_version > LFS_VERSION ||
   1028 		    tdfs->dlfs_bsize < sizeof(struct dlfs)) {
   1029 #ifdef DEBUG_LFS
   1030 			printf("lfs_mountfs: alt superblock sanity failed\n");
   1031 #endif
   1032 			error = EINVAL;		/* XXX needs translation */
   1033 			goto out;
   1034 		}
   1035 	} else {
   1036 #ifdef DEBUG_LFS
   1037 		printf("lfs_mountfs: invalid alt superblock daddr=0x%x\n",
   1038 			dfs->dlfs_sboffs[1]);
   1039 #endif
   1040 		error = EINVAL;
   1041 		goto out;
   1042 	}
   1043 
   1044 	/* Allocate the mount structure, copy the superblock into it. */
   1045 	fs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK | M_ZERO);
   1046 	memcpy(&fs->lfs_dlfs, tdfs, sizeof(struct dlfs));
   1047 
   1048 	/* Compatibility */
   1049 	if (fs->lfs_version < 2) {
   1050 		fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
   1051 		fs->lfs_ibsize = fs->lfs_bsize;
   1052 		fs->lfs_start = fs->lfs_sboffs[0];
   1053 		fs->lfs_tstamp = fs->lfs_otstamp;
   1054 		fs->lfs_fsbtodb = 0;
   1055 	}
   1056 
   1057 	/*
   1058 	 * If we aren't going to be able to write meaningfully to this
   1059 	 * filesystem, and were not mounted readonly, bomb out now.
   1060 	 */
   1061 	if (fsbtob(fs, LFS_NRESERVE(fs)) > LFS_MAX_BYTES && !ronly) {
   1062 		printf("lfs_mount: to mount this filesystem read/write,"
   1063 		       " we need BUFPAGES >= %lld\n",
   1064 			(long long)((bufmem_hiwater / bufmem_lowater) *
   1065 			LFS_INVERSE_MAX_BYTES(
   1066 				fsbtob(fs, LFS_NRESERVE(fs))) >> PAGE_SHIFT));
   1067 		free(fs, M_UFSMNT);
   1068 		error = EFBIG; /* XXX needs translation */
   1069 		goto out;
   1070 	}
   1071 
   1072 	/* Before rolling forward, lock so vget will sleep for other procs */
   1073 	fs->lfs_flags = LFS_NOTYET;
   1074 	fs->lfs_rfpid = p->p_pid;
   1075 
   1076 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
   1077 	ump->um_lfs = fs;
   1078 	ump->um_fstype = UFS1;
   1079 	if (sizeof(struct lfs) < LFS_SBPAD) {			/* XXX why? */
   1080 		bp->b_flags |= B_INVAL;
   1081 		abp->b_flags |= B_INVAL;
   1082 	}
   1083 	brelse(bp);
   1084 	bp = NULL;
   1085 	brelse(abp);
   1086 	abp = NULL;
   1087 
   1088 	/* Set up the I/O information */
   1089 	fs->lfs_devbsize = secsize;
   1090 	fs->lfs_iocount = 0;
   1091 	fs->lfs_diropwait = 0;
   1092 	fs->lfs_activesb = 0;
   1093 	fs->lfs_uinodes = 0;
   1094 	fs->lfs_ravail = 0;
   1095 	fs->lfs_favail = 0;
   1096 	fs->lfs_sbactive = 0;
   1097 
   1098 	/* Set up the ifile and lock aflags */
   1099 	fs->lfs_doifile = 0;
   1100 	fs->lfs_writer = 0;
   1101 	fs->lfs_dirops = 0;
   1102 	fs->lfs_nadirop = 0;
   1103 	fs->lfs_seglock = 0;
   1104 	fs->lfs_pdflush = 0;
   1105 	fs->lfs_sleepers = 0;
   1106 	fs->lfs_pages = 0;
   1107 	simple_lock_init(&fs->lfs_interlock);
   1108 	lockinit(&fs->lfs_fraglock, PINOD, "lfs_fraglock", 0, 0);
   1109 
   1110 	/* Set the file system readonly/modify bits. */
   1111 	fs->lfs_ronly = ronly;
   1112 	if (ronly == 0)
   1113 		fs->lfs_fmod = 1;
   1114 
   1115 	/* Initialize the mount structure. */
   1116 	dev = devvp->v_rdev;
   1117 	mp->mnt_data = ump;
   1118 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
   1119 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_LFS);
   1120 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
   1121 	mp->mnt_stat.f_namemax = MAXNAMLEN;
   1122 	mp->mnt_stat.f_iosize = fs->lfs_bsize;
   1123 	mp->mnt_flag |= MNT_LOCAL;
   1124 	mp->mnt_fs_bshift = fs->lfs_bshift;
   1125 	ump->um_flags = 0;
   1126 	ump->um_mountp = mp;
   1127 	ump->um_dev = dev;
   1128 	ump->um_devvp = devvp;
   1129 	ump->um_bptrtodb = fs->lfs_fsbtodb;
   1130 	ump->um_seqinc = fragstofsb(fs, fs->lfs_frag);
   1131 	ump->um_nindir = fs->lfs_nindir;
   1132 	ump->um_lognindir = ffs(fs->lfs_nindir) - 1;
   1133 	for (i = 0; i < MAXQUOTAS; i++)
   1134 		ump->um_quotas[i] = NULLVP;
   1135 	ump->um_maxsymlinklen = fs->lfs_maxsymlinklen;
   1136 	ump->um_dirblksiz = DIRBLKSIZ;
   1137 	ump->um_maxfilesize = fs->lfs_maxfilesize;
   1138 	if (ump->um_maxsymlinklen > 0)
   1139 		mp->mnt_iflag |= IMNT_DTYPE;
   1140 	devvp->v_specmountpoint = mp;
   1141 
   1142 	/* Set up reserved memory for pageout */
   1143 	lfs_setup_resblks(fs);
   1144 	/* Set up vdirop tailq */
   1145 	TAILQ_INIT(&fs->lfs_dchainhd);
   1146 	/* and paging tailq */
   1147 	TAILQ_INIT(&fs->lfs_pchainhd);
   1148 
   1149 	/*
   1150 	 * We use the ifile vnode for almost every operation.  Instead of
   1151 	 * retrieving it from the hash table each time we retrieve it here,
   1152 	 * artificially increment the reference count and keep a pointer
   1153 	 * to it in the incore copy of the superblock.
   1154 	 */
   1155 	if ((error = VFS_VGET(mp, LFS_IFILE_INUM, &vp)) != 0) {
   1156 #ifdef DEBUG
   1157 		printf("lfs_mountfs: ifile vget failed, error=%d\n", error);
   1158 #endif
   1159 		goto out;
   1160 	}
   1161 	fs->lfs_ivnode = vp;
   1162 	VREF(vp);
   1163 
   1164 	/* Set up segment usage flags for the autocleaner. */
   1165 	fs->lfs_nactive = 0;
   1166 	fs->lfs_suflags = (u_int32_t **)malloc(2 * sizeof(u_int32_t *),
   1167 						M_SEGMENT, M_WAITOK);
   1168 	fs->lfs_suflags[0] = (u_int32_t *)malloc(fs->lfs_nseg * sizeof(u_int32_t),
   1169 						 M_SEGMENT, M_WAITOK);
   1170 	fs->lfs_suflags[1] = (u_int32_t *)malloc(fs->lfs_nseg * sizeof(u_int32_t),
   1171 						 M_SEGMENT, M_WAITOK);
   1172 	memset(fs->lfs_suflags[1], 0, fs->lfs_nseg * sizeof(u_int32_t));
   1173 	for (i = 0; i < fs->lfs_nseg; i++) {
   1174 		int changed;
   1175 
   1176 		LFS_SEGENTRY(sup, fs, i, bp);
   1177 		changed = 0;
   1178 		if (!ronly) {
   1179 			if (sup->su_nbytes == 0 &&
   1180 			    !(sup->su_flags & SEGUSE_EMPTY)) {
   1181 				sup->su_flags |= SEGUSE_EMPTY;
   1182 				++changed;
   1183 			} else if (!(sup->su_nbytes == 0) &&
   1184 				   (sup->su_flags & SEGUSE_EMPTY)) {
   1185 				sup->su_flags &= ~SEGUSE_EMPTY;
   1186 				++changed;
   1187 			}
   1188 			if (sup->su_flags & SEGUSE_ACTIVE) {
   1189 				sup->su_flags &= ~SEGUSE_ACTIVE;
   1190 				++changed;
   1191 			}
   1192 		}
   1193 		fs->lfs_suflags[0][i] = sup->su_flags;
   1194 		if (changed)
   1195 			LFS_WRITESEGENTRY(sup, fs, i, bp);
   1196 		else
   1197 			brelse(bp);
   1198 	}
   1199 
   1200 	/*
   1201 	 * Roll forward.
   1202 	 *
   1203 	 * We don't automatically roll forward for v1 filesystems, because
   1204 	 * of the danger that the clock was turned back between the last
   1205 	 * checkpoint and crash.  This would roll forward garbage.
   1206 	 *
   1207 	 * v2 filesystems don't have this problem because they use a
   1208 	 * monotonically increasing serial number instead of a timestamp.
   1209 	 */
   1210 #ifdef LFS_DO_ROLLFORWARD
   1211 	do_rollforward = !fs->lfs_ronly;
   1212 #else
   1213 	do_rollforward = (fs->lfs_version > 1 && !fs->lfs_ronly &&
   1214 			  !(fs->lfs_pflags & LFS_PF_CLEAN));
   1215 #endif
   1216 	if (do_rollforward) {
   1217 		u_int64_t nextserial;
   1218 		/*
   1219 		 * Phase I: Find the address of the last good partial
   1220 		 * segment that was written after the checkpoint.  Mark
   1221 		 * the segments in question dirty, so they won't be
   1222 		 * reallocated.
   1223 		 */
   1224 		lastgoodpseg = oldoffset = offset = fs->lfs_offset;
   1225 		flags = 0x0;
   1226 #ifdef DEBUG_LFS_RFW
   1227 		printf("LFS roll forward phase 1: starting at offset 0x%"
   1228 		    PRIx64 "\n", offset);
   1229 #endif
   1230 		LFS_SEGENTRY(sup, fs, dtosn(fs, offset), bp);
   1231 		if (!(sup->su_flags & SEGUSE_DIRTY))
   1232 			--fs->lfs_nclean;
   1233 		sup->su_flags |= SEGUSE_DIRTY;
   1234 		LFS_WRITESEGENTRY(sup, fs, dtosn(fs, offset), bp);
   1235 		nextserial = fs->lfs_serial + 1;
   1236 		while ((offset = check_segsum(fs, offset, nextserial,
   1237 		    cred, CHECK_CKSUM, &flags, p)) > 0) {
   1238 			nextserial++;
   1239 			if (sntod(fs, oldoffset) != sntod(fs, offset)) {
   1240 				LFS_SEGENTRY(sup, fs, dtosn(fs, oldoffset),
   1241 					     bp);
   1242 				if (!(sup->su_flags & SEGUSE_DIRTY))
   1243 					--fs->lfs_nclean;
   1244 				sup->su_flags |= SEGUSE_DIRTY;
   1245 				LFS_WRITESEGENTRY(sup, fs, dtosn(fs, oldoffset),
   1246 					     bp);
   1247 			}
   1248 
   1249 #ifdef DEBUG_LFS_RFW
   1250 			printf("LFS roll forward phase 1: offset=0x%"
   1251 			    PRIx64 "\n", offset);
   1252 			if (flags & SS_DIROP) {
   1253 				printf("lfs_mountfs: dirops at 0x%" PRIx64 "\n",
   1254 				       oldoffset);
   1255 				if (!(flags & SS_CONT))
   1256 					printf("lfs_mountfs: dirops end "
   1257 					       "at 0x%" PRIx64 "\n", oldoffset);
   1258 			}
   1259 #endif
   1260 			if (!(flags & SS_CONT))
   1261 				lastgoodpseg = offset;
   1262 			oldoffset = offset;
   1263 		}
   1264 #ifdef DEBUG_LFS_RFW
   1265 		if (flags & SS_CONT) {
   1266 			printf("LFS roll forward: warning: incomplete "
   1267 			       "dirops discarded\n");
   1268 		}
   1269 		printf("LFS roll forward phase 1: completed: "
   1270 		       "lastgoodpseg=0x%" PRIx64 "\n", lastgoodpseg);
   1271 #endif
   1272 		oldoffset = fs->lfs_offset;
   1273 		if (fs->lfs_offset != lastgoodpseg) {
   1274 			/* Don't overwrite what we're trying to preserve */
   1275 			offset = fs->lfs_offset;
   1276 			fs->lfs_offset = lastgoodpseg;
   1277 			fs->lfs_curseg = sntod(fs, dtosn(fs, fs->lfs_offset));
   1278 			for (sn = curseg = dtosn(fs, fs->lfs_curseg);;) {
   1279 				sn = (sn + 1) % fs->lfs_nseg;
   1280 				if (sn == curseg)
   1281 					panic("lfs_mountfs: no clean segments");
   1282 				LFS_SEGENTRY(sup, fs, sn, bp);
   1283 				dirty = (sup->su_flags & SEGUSE_DIRTY);
   1284 				brelse(bp);
   1285 				if (!dirty)
   1286 					break;
   1287 			}
   1288 			fs->lfs_nextseg = sntod(fs, sn);
   1289 
   1290 			/*
   1291 			 * Phase II: Roll forward from the first superblock.
   1292 			 */
   1293 			while (offset != lastgoodpseg) {
   1294 #ifdef DEBUG_LFS_RFW
   1295 				printf("LFS roll forward phase 2: 0x%"
   1296 				    PRIx64 "\n", offset);
   1297 #endif
   1298 				offset = check_segsum(fs, offset,
   1299 				    fs->lfs_serial + 1, cred, CHECK_UPDATE,
   1300 				    NULL, p);
   1301 			}
   1302 
   1303 			/*
   1304 			 * Finish: flush our changes to disk.
   1305 			 */
   1306 			lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
   1307 			printf("lfs_mountfs: roll forward recovered %lld blocks\n",
   1308 			       (long long)(lastgoodpseg - oldoffset));
   1309 		}
   1310 #ifdef DEBUG_LFS_RFW
   1311 		printf("LFS roll forward complete\n");
   1312 #endif
   1313 	}
   1314 	/* If writing, sb is not clean; record in case of immediate crash */
   1315 	if (!fs->lfs_ronly) {
   1316 		fs->lfs_pflags &= ~LFS_PF_CLEAN;
   1317 		lfs_writesuper(fs, fs->lfs_sboffs[0]);
   1318 		lfs_writesuper(fs, fs->lfs_sboffs[1]);
   1319 	}
   1320 
   1321 	/* Allow vget now that roll-forward is complete */
   1322 	fs->lfs_flags &= ~(LFS_NOTYET);
   1323 	wakeup(&fs->lfs_flags);
   1324 
   1325 	/*
   1326 	 * Initialize the ifile cleaner info with information from
   1327 	 * the superblock.
   1328 	 */
   1329 	LFS_CLEANERINFO(cip, fs, bp);
   1330 	cip->clean = fs->lfs_nclean;
   1331 	cip->dirty = fs->lfs_nseg - fs->lfs_nclean;
   1332 	cip->avail = fs->lfs_avail;
   1333 	cip->bfree = fs->lfs_bfree;
   1334 	(void) LFS_BWRITE_LOG(bp); /* Ifile */
   1335 
   1336 	/*
   1337 	 * Mark the current segment as ACTIVE, since we're going to
   1338 	 * be writing to it.
   1339 	 */
   1340 	LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_offset), bp);
   1341 	sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
   1342 	fs->lfs_nactive++;
   1343 	LFS_WRITESEGENTRY(sup, fs, dtosn(fs, fs->lfs_offset), bp);  /* Ifile */
   1344 
   1345 	/* Now that roll-forward is done, unlock the Ifile */
   1346 	vput(vp);
   1347 
   1348 	/* Comment on ifile size if it is too large */
   1349 	if (fs->lfs_ivnode->v_size / fs->lfs_bsize > LFS_MAX_BUFS) {
   1350 		fs->lfs_flags |= LFS_WARNED;
   1351 		printf("lfs_mountfs: please consider increasing NBUF to at least %lld\n",
   1352 			(long long)LFS_INVERSE_MAX_BUFS((fs->lfs_ivnode->v_size / fs->lfs_bsize) - fs->lfs_segtabsz));
   1353 	}
   1354 	if (fs->lfs_ivnode->v_size > LFS_MAX_BYTES) {
   1355 		fs->lfs_flags |= LFS_WARNED;
   1356 		printf("lfs_mountfs: please consider increasing BUFPAGES to at least %lld\n",
   1357 			(long long)LFS_INVERSE_MAX_BYTES((fs->lfs_ivnode->v_size - fs->lfs_segtabsz * fs->lfs_bsize) >> PAGE_SHIFT));
   1358 	}
   1359 
   1360 	/* Start the pagedaemon-anticipating daemon */
   1361 	if (lfs_writer_daemon == 0 &&
   1362 	    kthread_create1(lfs_writerd, NULL, NULL, "lfs_writer") != 0)
   1363 		panic("fork lfs_writer");
   1364 
   1365 	return (0);
   1366 
   1367 out:
   1368 	if (bp)
   1369 		brelse(bp);
   1370 	if (abp)
   1371 		brelse(abp);
   1372 	if (ump) {
   1373 		free(ump->um_lfs, M_UFSMNT);
   1374 		free(ump, M_UFSMNT);
   1375 		mp->mnt_data = NULL;
   1376 	}
   1377 
   1378 	return (error);
   1379 }
   1380 
   1381 /*
   1382  * unmount system call
   1383  */
   1384 int
   1385 lfs_unmount(struct mount *mp, int mntflags, struct proc *p)
   1386 {
   1387 	struct ufsmount *ump;
   1388 	struct lfs *fs;
   1389 	int error, flags, ronly;
   1390 	int s;
   1391 
   1392 	flags = 0;
   1393 	if (mntflags & MNT_FORCE)
   1394 		flags |= FORCECLOSE;
   1395 
   1396 	ump = VFSTOUFS(mp);
   1397 	fs = ump->um_lfs;
   1398 
   1399 	/* wake up the cleaner so it can die */
   1400 	wakeup(&fs->lfs_nextseg);
   1401 	wakeup(&lfs_allclean_wakeup);
   1402 	simple_lock(&fs->lfs_interlock);
   1403 	while (fs->lfs_sleepers)
   1404 		ltsleep(&fs->lfs_sleepers, PRIBIO + 1, "lfs_sleepers", 0,
   1405 			&fs->lfs_interlock);
   1406 	simple_unlock(&fs->lfs_interlock);
   1407 
   1408 #ifdef QUOTA
   1409 	if (mp->mnt_flag & MNT_QUOTA) {
   1410 		int i;
   1411 		error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags);
   1412 		if (error)
   1413 			return (error);
   1414 		for (i = 0; i < MAXQUOTAS; i++) {
   1415 			if (ump->um_quotas[i] == NULLVP)
   1416 				continue;
   1417 			quotaoff(p, mp, i);
   1418 		}
   1419 		/*
   1420 		 * Here we fall through to vflush again to ensure
   1421 		 * that we have gotten rid of all the system vnodes.
   1422 		 */
   1423 	}
   1424 #endif
   1425 	if ((error = vflush(mp, fs->lfs_ivnode, flags)) != 0)
   1426 		return (error);
   1427 	if ((error = VFS_SYNC(mp, 1, p->p_ucred, p)) != 0)
   1428 		return (error);
   1429 	s = splbio();
   1430 	if (LIST_FIRST(&fs->lfs_ivnode->v_dirtyblkhd))
   1431 		panic("lfs_unmount: still dirty blocks on ifile vnode");
   1432 	splx(s);
   1433 
   1434 	/* Comment on ifile size if it has become too large */
   1435 	if (!(fs->lfs_flags & LFS_WARNED)) {
   1436 		if (fs->lfs_ivnode->v_size / fs->lfs_bsize > LFS_MAX_BUFS)
   1437 			printf("lfs_unmount: please consider increasing"
   1438 				" NBUF to at least %lld\n",
   1439 				(long long)(fs->lfs_ivnode->v_size /
   1440 					    fs->lfs_bsize) *
   1441 				(long long)(nbuf / LFS_MAX_BUFS));
   1442 		if (fs->lfs_ivnode->v_size > LFS_MAX_BYTES)
   1443 			printf("lfs_unmount: please consider increasing"
   1444 				" BUFPAGES to at least %lld\n",
   1445 				(long long)(fs->lfs_ivnode->v_size *
   1446 				bufpages / LFS_MAX_BYTES));
   1447 	}
   1448 
   1449 	/* Explicitly write the superblock, to update serial and pflags */
   1450 	fs->lfs_pflags |= LFS_PF_CLEAN;
   1451 	lfs_writesuper(fs, fs->lfs_sboffs[0]);
   1452 	lfs_writesuper(fs, fs->lfs_sboffs[1]);
   1453 	while (fs->lfs_iocount)
   1454 		tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs_umount", 0);
   1455 
   1456 	/* Finish with the Ifile, now that we're done with it */
   1457 	vrele(fs->lfs_ivnode);
   1458 	vgone(fs->lfs_ivnode);
   1459 
   1460 	ronly = !fs->lfs_ronly;
   1461 	if (ump->um_devvp->v_type != VBAD)
   1462 		ump->um_devvp->v_specmountpoint = NULL;
   1463 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1464 	error = VOP_CLOSE(ump->um_devvp,
   1465 	    ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
   1466 	vput(ump->um_devvp);
   1467 
   1468 	/* Free per-mount data structures */
   1469 	free(fs->lfs_suflags[0], M_SEGMENT);
   1470 	free(fs->lfs_suflags[1], M_SEGMENT);
   1471 	free(fs->lfs_suflags, M_SEGMENT);
   1472 	lfs_free_resblks(fs);
   1473 	free(fs, M_UFSMNT);
   1474 	free(ump, M_UFSMNT);
   1475 
   1476 	mp->mnt_data = NULL;
   1477 	mp->mnt_flag &= ~MNT_LOCAL;
   1478 	return (error);
   1479 }
   1480 
   1481 /*
   1482  * Get file system statistics.
   1483  */
   1484 int
   1485 lfs_statvfs(struct mount *mp, struct statvfs *sbp, struct proc *p)
   1486 {
   1487 	struct lfs *fs;
   1488 	struct ufsmount *ump;
   1489 
   1490 	ump = VFSTOUFS(mp);
   1491 	fs = ump->um_lfs;
   1492 	if (fs->lfs_magic != LFS_MAGIC)
   1493 		panic("lfs_statvfs: magic");
   1494 
   1495 	sbp->f_bsize = fs->lfs_bsize;
   1496 	sbp->f_frsize = fs->lfs_fsize;
   1497 	sbp->f_iosize = fs->lfs_bsize;
   1498 	sbp->f_blocks = fsbtofrags(fs, LFS_EST_NONMETA(fs));
   1499 
   1500 	sbp->f_bfree = fsbtofrags(fs, LFS_EST_BFREE(fs));
   1501 	KASSERT(sbp->f_bfree <= fs->lfs_dsize);
   1502 	if (sbp->f_bfree < 0)
   1503 		sbp->f_bfree = 0;
   1504 
   1505 	sbp->f_bresvd = fsbtofrags(fs, LFS_EST_RSVD(fs));
   1506 	if (sbp->f_bfree > sbp->f_bresvd)
   1507 		sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
   1508 	else
   1509 		sbp->f_bavail = 0;
   1510 
   1511 	sbp->f_files = fs->lfs_bfree / btofsb(fs, fs->lfs_ibsize) * INOPB(fs);
   1512 	sbp->f_ffree = sbp->f_files - fs->lfs_nfiles;
   1513 	sbp->f_favail = sbp->f_ffree;
   1514 	sbp->f_fresvd = 0;
   1515 	copy_statvfs_info(sbp, mp);
   1516 	return (0);
   1517 }
   1518 
   1519 /*
   1520  * Go through the disk queues to initiate sandbagged IO;
   1521  * go through the inodes to write those that have been modified;
   1522  * initiate the writing of the super block if it has been modified.
   1523  *
   1524  * Note: we are always called with the filesystem marked `MPBUSY'.
   1525  */
   1526 int
   1527 lfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p)
   1528 {
   1529 	int error;
   1530 	struct lfs *fs;
   1531 
   1532 	fs = VFSTOUFS(mp)->um_lfs;
   1533 	if (fs->lfs_ronly)
   1534 		return 0;
   1535 	lfs_writer_enter(fs, "lfs_dirops");
   1536 
   1537 	/* All syncs must be checkpoints until roll-forward is implemented. */
   1538 	error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
   1539 	lfs_writer_leave(fs);
   1540 #ifdef QUOTA
   1541 	qsync(mp);
   1542 #endif
   1543 	return (error);
   1544 }
   1545 
   1546 extern struct lock ufs_hashlock;
   1547 
   1548 /*
   1549  * Look up an LFS dinode number to find its incore vnode.  If not already
   1550  * in core, read it in from the specified device.  Return the inode locked.
   1551  * Detection and handling of mount points must be done by the calling routine.
   1552  */
   1553 int
   1554 lfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
   1555 {
   1556 	struct lfs *fs;
   1557 	struct ufs1_dinode *dip;
   1558 	struct inode *ip;
   1559 	struct buf *bp;
   1560 	struct ifile *ifp;
   1561 	struct vnode *vp;
   1562 	struct ufsmount *ump;
   1563 	daddr_t daddr;
   1564 	dev_t dev;
   1565 	int error, retries;
   1566 	struct timespec ts;
   1567 
   1568 	ump = VFSTOUFS(mp);
   1569 	dev = ump->um_dev;
   1570 	fs = ump->um_lfs;
   1571 
   1572 	/*
   1573 	 * If the filesystem is not completely mounted yet, suspend
   1574 	 * any access requests (wait for roll-forward to complete).
   1575 	 */
   1576 	while ((fs->lfs_flags & LFS_NOTYET) && curproc->p_pid != fs->lfs_rfpid)
   1577 		tsleep(&fs->lfs_flags, PRIBIO+1, "lfs_notyet", 0);
   1578 
   1579 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
   1580 		return (0);
   1581 
   1582 	if ((error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, &vp)) != 0) {
   1583 		*vpp = NULL;
   1584 		 return (error);
   1585 	}
   1586 
   1587 	do {
   1588 		if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
   1589 			ungetnewvnode(vp);
   1590 			return (0);
   1591 		}
   1592 	} while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
   1593 
   1594 	/* Translate the inode number to a disk address. */
   1595 	if (ino == LFS_IFILE_INUM)
   1596 		daddr = fs->lfs_idaddr;
   1597 	else {
   1598 		/* XXX bounds-check this too */
   1599 		LFS_IENTRY(ifp, fs, ino, bp);
   1600 		daddr = ifp->if_daddr;
   1601 		if (fs->lfs_version > 1) {
   1602 			ts.tv_sec = ifp->if_atime_sec;
   1603 			ts.tv_nsec = ifp->if_atime_nsec;
   1604 		}
   1605 
   1606 		brelse(bp);
   1607 		if (daddr == LFS_UNUSED_DADDR) {
   1608 			*vpp = NULLVP;
   1609 			ungetnewvnode(vp);
   1610 			lockmgr(&ufs_hashlock, LK_RELEASE, 0);
   1611 			return (ENOENT);
   1612 		}
   1613 	}
   1614 
   1615 	/* Allocate/init new vnode/inode. */
   1616 	lfs_vcreate(mp, ino, vp);
   1617 
   1618 	/*
   1619 	 * Put it onto its hash chain and lock it so that other requests for
   1620 	 * this inode will block if they arrive while we are sleeping waiting
   1621 	 * for old data structures to be purged or for the contents of the
   1622 	 * disk portion of this inode to be read.
   1623 	 */
   1624 	ip = VTOI(vp);
   1625 	ufs_ihashins(ip);
   1626 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
   1627 
   1628 	/*
   1629 	 * XXX
   1630 	 * This may not need to be here, logically it should go down with
   1631 	 * the i_devvp initialization.
   1632 	 * Ask Kirk.
   1633 	 */
   1634 	ip->i_lfs = ump->um_lfs;
   1635 
   1636 	/* Read in the disk contents for the inode, copy into the inode. */
   1637 	retries = 0;
   1638     again:
   1639 	error = bread(ump->um_devvp, fsbtodb(fs, daddr),
   1640 		(fs->lfs_version == 1 ? fs->lfs_bsize : fs->lfs_ibsize),
   1641 		NOCRED, &bp);
   1642 	if (error) {
   1643 		/*
   1644 		 * The inode does not contain anything useful, so it would
   1645 		 * be misleading to leave it on its hash chain. With mode
   1646 		 * still zero, it will be unlinked and returned to the free
   1647 		 * list by vput().
   1648 		 */
   1649 		vput(vp);
   1650 		brelse(bp);
   1651 		*vpp = NULL;
   1652 		return (error);
   1653 	}
   1654 
   1655 	dip = lfs_ifind(fs, ino, bp);
   1656 	if (dip == NULL) {
   1657 		/* Assume write has not completed yet; try again */
   1658 		bp->b_flags |= B_INVAL;
   1659 		brelse(bp);
   1660 		++retries;
   1661 		if (retries > LFS_IFIND_RETRIES) {
   1662 #ifdef DEBUG
   1663 			/* If the seglock is held look at the bpp to see
   1664 			   what is there anyway */
   1665 			if (fs->lfs_seglock > 0) {
   1666 				struct buf **bpp;
   1667 				struct ufs1_dinode *dp;
   1668 				int i;
   1669 
   1670 				for (bpp = fs->lfs_sp->bpp;
   1671 				     bpp != fs->lfs_sp->cbpp; ++bpp) {
   1672 					if ((*bpp)->b_vp == fs->lfs_ivnode &&
   1673 					    bpp != fs->lfs_sp->bpp) {
   1674 						/* Inode block */
   1675 						printf("block 0x%" PRIx64 ": ",
   1676 						    (*bpp)->b_blkno);
   1677 						dp = (struct ufs1_dinode *)(*bpp)->b_data;
   1678 						for (i = 0; i < INOPB(fs); i++)
   1679 							if (dp[i].di_u.inumber)
   1680 								printf("%d ", dp[i].di_u.inumber);
   1681 						printf("\n");
   1682 					}
   1683 				}
   1684 			}
   1685 #endif
   1686 			panic("lfs_vget: dinode not found");
   1687 		}
   1688 		printf("lfs_vget: dinode %d not found, retrying...\n", ino);
   1689 		(void)tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs ifind", 1);
   1690 		goto again;
   1691 	}
   1692 	*ip->i_din.ffs1_din = *dip;
   1693 	brelse(bp);
   1694 
   1695 	if (fs->lfs_version > 1) {
   1696 		ip->i_ffs1_atime = ts.tv_sec;
   1697 		ip->i_ffs1_atimensec = ts.tv_nsec;
   1698 	}
   1699 
   1700 	lfs_vinit(mp, &vp);
   1701 
   1702 	*vpp = vp;
   1703 
   1704 	KASSERT(VOP_ISLOCKED(vp));
   1705 
   1706 	return (0);
   1707 }
   1708 
   1709 /*
   1710  * File handle to vnode
   1711  */
   1712 int
   1713 lfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
   1714 {
   1715 	struct lfid *lfhp;
   1716 	struct buf *bp;
   1717 	IFILE *ifp;
   1718 	int32_t daddr;
   1719 	struct lfs *fs;
   1720 
   1721 	lfhp = (struct lfid *)fhp;
   1722 	if (lfhp->lfid_ino < LFS_IFILE_INUM)
   1723 		return ESTALE;
   1724 
   1725 	fs = VFSTOUFS(mp)->um_lfs;
   1726 	if (lfhp->lfid_ident != fs->lfs_ident)
   1727 		return ESTALE;
   1728 
   1729 	if (lfhp->lfid_ino >
   1730 	    ((VTOI(fs->lfs_ivnode)->i_ffs1_size >> fs->lfs_bshift) -
   1731 	     fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb)
   1732 		return ESTALE;
   1733 
   1734 	if (ufs_ihashlookup(VFSTOUFS(mp)->um_dev, lfhp->lfid_ino) == NULLVP) {
   1735 		LFS_IENTRY(ifp, fs, lfhp->lfid_ino, bp);
   1736 		daddr = ifp->if_daddr;
   1737 		brelse(bp);
   1738 		if (daddr == LFS_UNUSED_DADDR)
   1739 			return ESTALE;
   1740 	}
   1741 
   1742 	return (ufs_fhtovp(mp, &lfhp->lfid_ufid, vpp));
   1743 }
   1744 
   1745 /*
   1746  * Vnode pointer to File handle
   1747  */
   1748 /* ARGSUSED */
   1749 int
   1750 lfs_vptofh(struct vnode *vp, struct fid *fhp)
   1751 {
   1752 	struct inode *ip;
   1753 	struct lfid *lfhp;
   1754 
   1755 	ip = VTOI(vp);
   1756 	lfhp = (struct lfid *)fhp;
   1757 	lfhp->lfid_len = sizeof(struct lfid);
   1758 	lfhp->lfid_ino = ip->i_number;
   1759 	lfhp->lfid_gen = ip->i_gen;
   1760 	lfhp->lfid_ident = ip->i_lfs->lfs_ident;
   1761 	return (0);
   1762 }
   1763 
   1764 static int
   1765 sysctl_lfs_dostats(SYSCTLFN_ARGS)
   1766 {
   1767 	extern struct lfs_stats lfs_stats;
   1768 	extern int lfs_dostats;
   1769 	int error;
   1770 
   1771 	error = sysctl_lookup(SYSCTLFN_CALL(rnode));
   1772 	if (error || newp == NULL)
   1773 		return (error);
   1774 
   1775 	if (lfs_dostats == 0)
   1776 		memset(&lfs_stats,0,sizeof(lfs_stats));
   1777 
   1778 	return (0);
   1779 }
   1780 
   1781 SYSCTL_SETUP(sysctl_vfs_lfs_setup, "sysctl vfs.lfs subtree setup")
   1782 {
   1783 	extern int lfs_writeindir, lfs_dostats, lfs_clean_vnhead,
   1784 		   lfs_fs_pagetrip;
   1785 
   1786 	sysctl_createv(clog, 0, NULL, NULL,
   1787 		       CTLFLAG_PERMANENT,
   1788 		       CTLTYPE_NODE, "vfs", NULL,
   1789 		       NULL, 0, NULL, 0,
   1790 		       CTL_VFS, CTL_EOL);
   1791 	sysctl_createv(clog, 0, NULL, NULL,
   1792 		       CTLFLAG_PERMANENT,
   1793 		       CTLTYPE_NODE, "lfs",
   1794 		       SYSCTL_DESCR("Log-structured file system"),
   1795 		       NULL, 0, NULL, 0,
   1796 		       CTL_VFS, 5, CTL_EOL);
   1797 	/*
   1798 	 * XXX the "5" above could be dynamic, thereby eliminating one
   1799 	 * more instance of the "number to vfs" mapping problem, but
   1800 	 * "2" is the order as taken from sys/mount.h
   1801 	 */
   1802 
   1803 	sysctl_createv(clog, 0, NULL, NULL,
   1804 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1805 		       CTLTYPE_INT, "flushindir", NULL,
   1806 		       NULL, 0, &lfs_writeindir, 0,
   1807 		       CTL_VFS, 5, LFS_WRITEINDIR, CTL_EOL);
   1808 	sysctl_createv(clog, 0, NULL, NULL,
   1809 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1810 		       CTLTYPE_INT, "clean_vnhead", NULL,
   1811 		       NULL, 0, &lfs_clean_vnhead, 0,
   1812 		       CTL_VFS, 5, LFS_CLEAN_VNHEAD, CTL_EOL);
   1813 	sysctl_createv(clog, 0, NULL, NULL,
   1814 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1815 		       CTLTYPE_INT, "dostats",
   1816 		       SYSCTL_DESCR("Maintain statistics on LFS operations"),
   1817 		       sysctl_lfs_dostats, 0, &lfs_dostats, 0,
   1818 		       CTL_VFS, 5, LFS_DOSTATS, CTL_EOL);
   1819 	sysctl_createv(clog, 0, NULL, NULL,
   1820 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1821 		       CTLTYPE_INT, "pagetrip",
   1822 		       SYSCTL_DESCR("How many dirty segments of pages in fs trips write"),
   1823 		       sysctl_lfs_dostats, 0, &lfs_fs_pagetrip, 0,
   1824 		       CTL_VFS, 5, LFS_FS_PAGETRIP, CTL_EOL);
   1825 }
   1826 
   1827 /*
   1828  * ufs_bmaparray callback function for writing.
   1829  *
   1830  * Since blocks will be written to the new segment anyway,
   1831  * we don't care about current daddr of them.
   1832  */
   1833 static boolean_t
   1834 lfs_issequential_hole(const struct ufsmount *ump,
   1835     daddr_t daddr0, daddr_t daddr1)
   1836 {
   1837 	daddr0 = (daddr_t)((int32_t)daddr0); /* XXX ondisk32 */
   1838 	daddr1 = (daddr_t)((int32_t)daddr1); /* XXX ondisk32 */
   1839 
   1840 	KASSERT(daddr0 == UNWRITTEN ||
   1841 	    (0 <= daddr0 && daddr0 <= LFS_MAX_DADDR));
   1842 	KASSERT(daddr1 == UNWRITTEN ||
   1843 	    (0 <= daddr1 && daddr1 <= LFS_MAX_DADDR));
   1844 
   1845 	/* NOTE: all we want to know here is 'hole or not'. */
   1846 	/* NOTE: UNASSIGNED is converted to 0 by ufs_bmaparray. */
   1847 
   1848 	/*
   1849 	 * treat UNWRITTENs and all resident blocks as 'contiguous'
   1850 	 */
   1851 	if (daddr0 != 0 && daddr1 != 0)
   1852 		return TRUE;
   1853 
   1854 	/*
   1855 	 * both are in hole?
   1856 	 */
   1857 	if (daddr0 == 0 && daddr1 == 0)
   1858 		return TRUE; /* all holes are 'contiguous' for us. */
   1859 
   1860 	return FALSE;
   1861 }
   1862 
   1863 /*
   1864  * lfs_gop_write functions exactly like genfs_gop_write, except that
   1865  * (1) it requires the seglock to be held by its caller, and sp->fip
   1866  *     to be properly initialized (it will return without re-initializing
   1867  *     sp->fip, and without calling lfs_writeseg).
   1868  * (2) it uses the remaining space in the segment, rather than VOP_BMAP,
   1869  *     to determine how large a block it can write at once (though it does
   1870  *     still use VOP_BMAP to find holes in the file);
   1871  * (3) it calls lfs_gatherblock instead of VOP_STRATEGY on its blocks
   1872  *     (leaving lfs_writeseg to deal with the cluster blocks, so we might
   1873  *     now have clusters of clusters, ick.)
   1874  */
   1875 static int
   1876 lfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
   1877 {
   1878 	int i, s, error, run;
   1879 	int fs_bshift;
   1880 	vaddr_t kva;
   1881 	off_t eof, offset, startoffset;
   1882 	size_t bytes, iobytes, skipbytes;
   1883 	daddr_t lbn, blkno;
   1884 	struct vm_page *pg;
   1885 	struct buf *mbp, *bp;
   1886 	struct vnode *devvp = VTOI(vp)->i_devvp;
   1887 	struct inode *ip = VTOI(vp);
   1888 	struct lfs *fs = ip->i_lfs;
   1889 	struct segment *sp = fs->lfs_sp;
   1890 	UVMHIST_FUNC("lfs_gop_write"); UVMHIST_CALLED(ubchist);
   1891 
   1892 	/* The Ifile lives in the buffer cache */
   1893 	KASSERT(vp != fs->lfs_ivnode);
   1894 
   1895 	/*
   1896 	 * Sometimes things slip past the filters in lfs_putpages,
   1897 	 * and the pagedaemon tries to write pages---problem is
   1898 	 * that the pagedaemon never acquires the segment lock.
   1899 	 *
   1900 	 * Alternatively, pages that were clean when we called
   1901 	 * genfs_putpages may have become dirty in the meantime.  In this
   1902 	 * case the segment header is not properly set up for blocks
   1903 	 * to be added to it.
   1904 	 *
   1905 	 * Unbusy and unclean the pages, and put them on the ACTIVE
   1906 	 * queue under the hypothesis that they couldn't have got here
   1907 	 * unless they were modified *quite* recently.
   1908 	 *
   1909 	 * XXXUBC that last statement is an oversimplification of course.
   1910 	 */
   1911 	if (!(fs->lfs_seglock) || fs->lfs_lockpid != curproc->p_pid ||
   1912 		(ip->i_lfs_iflags & LFSI_NO_GOP_WRITE) ||
   1913 		(pgs[0]->offset & fs->lfs_bmask) != 0) {
   1914                 goto tryagain;
   1915 	}
   1916 
   1917 	UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x",
   1918 	    vp, pgs, npages, flags);
   1919 
   1920 	GOP_SIZE(vp, vp->v_size, &eof, GOP_SIZE_WRITE);
   1921 
   1922 	if (vp->v_type == VREG)
   1923 		fs_bshift = vp->v_mount->mnt_fs_bshift;
   1924 	else
   1925 		fs_bshift = DEV_BSHIFT;
   1926 	error = 0;
   1927 	pg = pgs[0];
   1928 	startoffset = pg->offset;
   1929 	bytes = MIN(npages << PAGE_SHIFT, eof - startoffset);
   1930 	skipbytes = 0;
   1931 
   1932 	/* KASSERT(bytes != 0); */
   1933 	if (bytes == 0)
   1934 		printf("ino %d bytes == 0 offset %" PRId64 "\n",
   1935 			VTOI(vp)->i_number, pgs[0]->offset);
   1936 
   1937 	/* Swap PG_DELWRI for PG_PAGEOUT */
   1938 	for (i = 0; i < npages; i++)
   1939 		if (pgs[i]->flags & PG_DELWRI) {
   1940 			KASSERT(!(pgs[i]->flags & PG_PAGEOUT));
   1941 			pgs[i]->flags &= ~PG_DELWRI;
   1942 			pgs[i]->flags |= PG_PAGEOUT;
   1943 			uvmexp.paging++;
   1944 			uvm_lock_pageq();
   1945 			uvm_pageunwire(pgs[i]);
   1946 			uvm_unlock_pageq();
   1947 		}
   1948 
   1949 	/*
   1950 	 * Check to make sure we're starting on a block boundary.
   1951 	 * We'll check later to make sure we always write entire
   1952 	 * blocks (or fragments).
   1953 	 */
   1954 	if (startoffset & fs->lfs_bmask)
   1955 		printf("%" PRId64 " & %" PRId64 " = %" PRId64 "\n",
   1956 			startoffset, fs->lfs_bmask,
   1957 			startoffset & fs->lfs_bmask);
   1958 	KASSERT((startoffset & fs->lfs_bmask) == 0);
   1959 	if (bytes & fs->lfs_ffmask) {
   1960 		printf("lfs_gop_write: asked to write %ld bytes\n", (long)bytes);
   1961 		panic("lfs_gop_write: non-integer blocks");
   1962 	}
   1963 
   1964 	/*
   1965 	 * XXX We can deadlock here on pager_map with UVMPAGER_MAPIN_WAITOK.
   1966 	 */
   1967 	kva = uvm_pagermapin(pgs, npages,
   1968 	    UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
   1969 
   1970 	s = splbio();
   1971 	simple_lock(&global_v_numoutput_slock);
   1972 	vp->v_numoutput += 2; /* one for biodone, one for aiodone */
   1973 	simple_unlock(&global_v_numoutput_slock);
   1974 	mbp = pool_get(&bufpool, PR_WAITOK);
   1975 	splx(s);
   1976 
   1977 	memset(mbp, 0, sizeof(*bp));
   1978 	BUF_INIT(mbp);
   1979 	UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
   1980 	    vp, mbp, vp->v_numoutput, bytes);
   1981 	mbp->b_bufsize = npages << PAGE_SHIFT;
   1982 	mbp->b_data = (void *)kva;
   1983 	mbp->b_resid = mbp->b_bcount = bytes;
   1984 	mbp->b_flags = B_BUSY|B_WRITE|B_AGE|B_CALL;
   1985 	mbp->b_iodone = uvm_aio_biodone;
   1986 	mbp->b_vp = vp;
   1987 
   1988 	bp = NULL;
   1989 	for (offset = startoffset;
   1990 	    bytes > 0;
   1991 	    offset += iobytes, bytes -= iobytes) {
   1992 		lbn = offset >> fs_bshift;
   1993 		error = ufs_bmaparray(vp, lbn, &blkno, NULL, NULL, &run,
   1994 		    lfs_issequential_hole);
   1995 		if (error) {
   1996 			UVMHIST_LOG(ubchist, "ufs_bmaparray() -> %d",
   1997 			    error,0,0,0);
   1998 			skipbytes += bytes;
   1999 			bytes = 0;
   2000 			break;
   2001 		}
   2002 
   2003 		iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
   2004 		    bytes);
   2005 		if (blkno == (daddr_t)-1) {
   2006 			skipbytes += iobytes;
   2007 			continue;
   2008 		}
   2009 
   2010 		/*
   2011 		 * Discover how much we can really pack into this buffer.
   2012 		 */
   2013 		/* If no room in the current segment, finish it up */
   2014 		if (sp->sum_bytes_left < sizeof(int32_t) ||
   2015 		    sp->seg_bytes_left < (1 << fs->lfs_bshift)) {
   2016 			int version;
   2017 
   2018 			lfs_updatemeta(sp);
   2019 
   2020 			version = sp->fip->fi_version;
   2021 			(void) lfs_writeseg(fs, sp);
   2022 
   2023 			sp->fip->fi_version = version;
   2024 			sp->fip->fi_ino = ip->i_number;
   2025 			/* Add the current file to the segment summary. */
   2026 			++((SEGSUM *)(sp->segsum))->ss_nfinfo;
   2027 			sp->sum_bytes_left -= FINFOSIZE;
   2028 		}
   2029 		/* Check both for space in segment and space in segsum */
   2030 		iobytes = MIN(iobytes, (sp->seg_bytes_left >> fs_bshift)
   2031 					<< fs_bshift);
   2032 		iobytes = MIN(iobytes, (sp->sum_bytes_left / sizeof(int32_t))
   2033 				       << fs_bshift);
   2034 		KASSERT(iobytes > 0);
   2035 
   2036 		/* if it's really one i/o, don't make a second buf */
   2037 		if (offset == startoffset && iobytes == bytes) {
   2038 			bp = mbp;
   2039 			/* printf("bp is mbp\n"); */
   2040 			/* correct overcount if there is no second buffer */
   2041 			s = splbio();
   2042 			simple_lock(&global_v_numoutput_slock);
   2043 			--vp->v_numoutput;
   2044 			simple_unlock(&global_v_numoutput_slock);
   2045 			splx(s);
   2046 		} else {
   2047 			/* printf("bp is not mbp\n"); */
   2048 			s = splbio();
   2049 			bp = pool_get(&bufpool, PR_WAITOK);
   2050 			UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
   2051 			    vp, bp, vp->v_numoutput, 0);
   2052 			splx(s);
   2053 			memset(bp, 0, sizeof(*bp));
   2054 			BUF_INIT(bp);
   2055 			bp->b_data = (char *)kva +
   2056 			    (vaddr_t)(offset - pg->offset);
   2057 			bp->b_resid = bp->b_bcount = iobytes;
   2058 			bp->b_flags = B_BUSY|B_WRITE|B_CALL;
   2059 			bp->b_iodone = uvm_aio_biodone1;
   2060 		}
   2061 
   2062 		/* XXX This is silly ... is this necessary? */
   2063 		bp->b_vp = NULL;
   2064 		s = splbio();
   2065 		bgetvp(vp, bp);
   2066 		splx(s);
   2067 
   2068 		bp->b_lblkno = lblkno(fs, offset);
   2069 		bp->b_private = mbp;
   2070 		if (devvp->v_type == VBLK) {
   2071 			bp->b_dev = devvp->v_rdev;
   2072 		}
   2073 		VOP_BWRITE(bp);
   2074 		while (lfs_gatherblock(sp, bp, NULL))
   2075 			continue;
   2076 	}
   2077 
   2078 	if (skipbytes) {
   2079 		UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0);
   2080 		s = splbio();
   2081 		if (error) {
   2082 			mbp->b_flags |= B_ERROR;
   2083 			mbp->b_error = error;
   2084 		}
   2085 		mbp->b_resid -= skipbytes;
   2086 		if (mbp->b_resid == 0) {
   2087 			biodone(mbp);
   2088 		}
   2089 		splx(s);
   2090 	}
   2091 	UVMHIST_LOG(ubchist, "returning 0", 0,0,0,0);
   2092 	return (0);
   2093 
   2094     tryagain:
   2095         /*
   2096          * We can't write the pages, for whatever reason.
   2097          * Clean up after ourselves, and make the caller try again.
   2098          */
   2099         simple_lock(&vp->v_interlock);
   2100 #ifdef DEBUG
   2101         if (ip->i_lfs_iflags & LFSI_NO_GOP_WRITE)
   2102                 printf("lfs_gop_write: clean pages dirtied\n");
   2103         else if ((pgs[0]->offset & fs->lfs_bmask) != 0)
   2104                 printf("lfs_gop_write: start not on block boundary\n");
   2105         else
   2106                 printf("lfs_gop_write: seglock not held\n");
   2107 #endif
   2108         uvm_lock_pageq();
   2109         for (i = 0; i < npages; i++) {
   2110                 pg = pgs[i];
   2111 
   2112                 if (pg->flags & PG_PAGEOUT)
   2113                         uvmexp.paging--;
   2114                 if (pg->flags & PG_DELWRI) {
   2115                         uvm_pageunwire(pg);
   2116                 }
   2117                 uvm_pageactivate(pg);
   2118                 pg->flags &= ~(PG_CLEAN|PG_DELWRI|PG_PAGEOUT|PG_RELEASED);
   2119 #ifdef DEBUG_LFS
   2120                 printf("pg[%d]->flags = %x\n", i, pg->flags);
   2121                 printf("pg[%d]->pqflags = %x\n", i, pg->pqflags);
   2122                 printf("pg[%d]->uanon = %p\n", i, pg->uanon);
   2123                 printf("pg[%d]->uobject = %p\n", i, pg->uobject);
   2124                 printf("pg[%d]->wire_count = %d\n", i, pg->wire_count);
   2125                 printf("pg[%d]->loan_count = %d\n", i, pg->loan_count);
   2126 #endif
   2127         }
   2128         /* uvm_pageunbusy takes care of PG_BUSY, PG_WANTED */
   2129         uvm_page_unbusy(pgs, npages);
   2130         uvm_unlock_pageq();
   2131         simple_unlock(&vp->v_interlock);
   2132         return EAGAIN;
   2133 }
   2134 
   2135 /*
   2136  * finish vnode/inode initialization.
   2137  * used by lfs_vget and lfs_fastvget.
   2138  */
   2139 void
   2140 lfs_vinit(struct mount *mp, struct vnode **vpp)
   2141 {
   2142 	struct vnode *vp = *vpp;
   2143 	struct inode *ip = VTOI(vp);
   2144 	struct ufsmount *ump = VFSTOUFS(mp);
   2145 	int i;
   2146 
   2147 	ip->i_mode = ip->i_ffs1_mode;
   2148 	ip->i_ffs_effnlink = ip->i_nlink = ip->i_ffs1_nlink;
   2149 	ip->i_lfs_osize = ip->i_size = ip->i_ffs1_size;
   2150 	ip->i_flags = ip->i_ffs1_flags;
   2151 	ip->i_gen = ip->i_ffs1_gen;
   2152 	ip->i_uid = ip->i_ffs1_uid;
   2153 	ip->i_gid = ip->i_ffs1_gid;
   2154 
   2155 	ip->i_lfs_effnblks = ip->i_ffs1_blocks;
   2156 
   2157 	/*
   2158 	 * Initialize the vnode from the inode, check for aliases.  In all
   2159 	 * cases re-init ip, the underlying vnode/inode may have changed.
   2160 	 */
   2161 	ufs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp);
   2162 	ip = VTOI(vp);
   2163 
   2164 	memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
   2165 	if (vp->v_type != VLNK || ip->i_size >= ip->i_ump->um_maxsymlinklen) {
   2166 		struct lfs *fs = ump->um_lfs;
   2167 #ifdef DEBUG
   2168 		for (i = (ip->i_size + fs->lfs_bsize - 1) >> fs->lfs_bshift;
   2169 		    i < NDADDR; i++) {
   2170 			if ((vp->v_type == VBLK || vp->v_type == VCHR) &&
   2171 			    i == 0)
   2172 				continue;
   2173 			if (ip->i_ffs1_db[i] != 0) {
   2174 inconsistent:
   2175 				lfs_dump_dinode(ip->i_din.ffs1_din);
   2176 				panic("inconsistent inode");
   2177 			}
   2178 		}
   2179 		for ( ; i < NDADDR + NIADDR; i++) {
   2180 			if (ip->i_ffs1_ib[i - NDADDR] != 0) {
   2181 				goto inconsistent;
   2182 			}
   2183 		}
   2184 #endif /* DEBUG */
   2185 		for (i = 0; i < NDADDR; i++)
   2186 			if (ip->i_ffs1_db[i] != 0)
   2187 				ip->i_lfs_fragsize[i] = blksize(fs, ip, i);
   2188 	}
   2189 
   2190 #ifdef DEBUG
   2191 	if (vp->v_type == VNON) {
   2192 		printf("lfs_vinit: ino %d is type VNON! (ifmt=%o)\n",
   2193 		       ip->i_number, (ip->i_mode & IFMT) >> 12);
   2194 		lfs_dump_dinode(ip->i_din.ffs1_din);
   2195 #ifdef DDB
   2196 		Debugger();
   2197 #endif /* DDB */
   2198 	}
   2199 #endif /* DEBUG */
   2200 
   2201 	/*
   2202 	 * Finish inode initialization now that aliasing has been resolved.
   2203 	 */
   2204 
   2205 	ip->i_devvp = ump->um_devvp;
   2206 	VREF(ip->i_devvp);
   2207 	genfs_node_init(vp, &lfs_genfsops);
   2208 	uvm_vnp_setsize(vp, ip->i_size);
   2209 
   2210 	*vpp = vp;
   2211 }
   2212