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