Home | History | Annotate | Line # | Download | only in lfs
lfs_vfsops.c revision 1.88
      1 /*	$NetBSD: lfs_vfsops.c,v 1.88 2003/01/25 23:00:09 kleink Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000 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.88 2003/01/25 23:00:09 kleink 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/buf.h>
     88 #include <sys/device.h>
     89 #include <sys/mbuf.h>
     90 #include <sys/file.h>
     91 #include <sys/disklabel.h>
     92 #include <sys/ioctl.h>
     93 #include <sys/errno.h>
     94 #include <sys/malloc.h>
     95 #include <sys/pool.h>
     96 #include <sys/socket.h>
     97 #include <uvm/uvm_extern.h>
     98 #include <sys/sysctl.h>
     99 #include <sys/conf.h>
    100 
    101 #include <miscfs/specfs/specdev.h>
    102 
    103 #include <ufs/ufs/quota.h>
    104 #include <ufs/ufs/inode.h>
    105 #include <ufs/ufs/ufsmount.h>
    106 #include <ufs/ufs/ufs_extern.h>
    107 
    108 #include <ufs/lfs/lfs.h>
    109 #include <ufs/lfs/lfs_extern.h>
    110 
    111 int lfs_mountfs(struct vnode *, struct mount *, struct proc *);
    112 
    113 extern const struct vnodeopv_desc lfs_vnodeop_opv_desc;
    114 extern const struct vnodeopv_desc lfs_specop_opv_desc;
    115 extern const struct vnodeopv_desc lfs_fifoop_opv_desc;
    116 
    117 const struct vnodeopv_desc * const lfs_vnodeopv_descs[] = {
    118 	&lfs_vnodeop_opv_desc,
    119 	&lfs_specop_opv_desc,
    120 	&lfs_fifoop_opv_desc,
    121 	NULL,
    122 };
    123 
    124 struct vfsops lfs_vfsops = {
    125 	MOUNT_LFS,
    126 	lfs_mount,
    127 	ufs_start,
    128 	lfs_unmount,
    129 	ufs_root,
    130 	ufs_quotactl,
    131 	lfs_statfs,
    132 	lfs_sync,
    133 	lfs_vget,
    134 	lfs_fhtovp,
    135 	lfs_vptofh,
    136 	lfs_init,
    137 	lfs_reinit,
    138 	lfs_done,
    139 	lfs_sysctl,
    140 	lfs_mountroot,
    141 	ufs_check_export,
    142 	lfs_vnodeopv_descs,
    143 };
    144 
    145 struct genfs_ops lfs_genfsops = {
    146 	NULL,
    147 	NULL,
    148 	genfs_compat_gop_write,
    149 };
    150 
    151 struct pool lfs_inode_pool;
    152 
    153 extern int locked_queue_count;
    154 extern long locked_queue_bytes;
    155 
    156 /*
    157  * Initialize the filesystem, most work done by ufs_init.
    158  */
    159 void
    160 lfs_init()
    161 {
    162 	ufs_init();
    163 
    164 	/*
    165 	 * XXX Same structure as FFS inodes?  Should we share a common pool?
    166 	 */
    167 	pool_init(&lfs_inode_pool, sizeof(struct inode), 0, 0, 0,
    168 		  "lfsinopl", &pool_allocator_nointr);
    169 #ifdef DEBUG
    170 	memset(lfs_log, 0, sizeof(lfs_log));
    171 #endif
    172 }
    173 
    174 void
    175 lfs_reinit()
    176 {
    177 	ufs_reinit();
    178 }
    179 
    180 void
    181 lfs_done()
    182 {
    183 	ufs_done();
    184 	pool_destroy(&lfs_inode_pool);
    185 }
    186 
    187 /*
    188  * Called by main() when ufs is going to be mounted as root.
    189  */
    190 int
    191 lfs_mountroot()
    192 {
    193 	extern struct vnode *rootvp;
    194 	struct mount *mp;
    195 	struct proc *p = curproc;	/* XXX */
    196 	int error;
    197 
    198 	if (root_device->dv_class != DV_DISK)
    199 		return (ENODEV);
    200 
    201 	if (rootdev == NODEV)
    202 	  	return (ENODEV);
    203 	/*
    204 	 * Get vnodes for swapdev and rootdev.
    205 	 */
    206 	if ((error = bdevvp(rootdev, &rootvp))) {
    207 		printf("lfs_mountroot: can't setup bdevvp's");
    208 		return (error);
    209 	}
    210 	if ((error = vfs_rootmountalloc(MOUNT_LFS, "root_device", &mp))) {
    211 		vrele(rootvp);
    212 		return (error);
    213 	}
    214 	if ((error = lfs_mountfs(rootvp, mp, p))) {
    215 		mp->mnt_op->vfs_refcount--;
    216 		vfs_unbusy(mp);
    217 		free(mp, M_MOUNT);
    218 		vrele(rootvp);
    219 		return (error);
    220 	}
    221 	simple_lock(&mountlist_slock);
    222 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    223 	simple_unlock(&mountlist_slock);
    224 	(void)lfs_statfs(mp, &mp->mnt_stat, p);
    225 	vfs_unbusy(mp);
    226 	inittodr(VFSTOUFS(mp)->um_lfs->lfs_tstamp);
    227 	return (0);
    228 }
    229 
    230 /*
    231  * VFS Operations.
    232  *
    233  * mount system call
    234  */
    235 int
    236 lfs_mount(struct mount *mp, const char *path, void *data, struct nameidata *ndp, struct proc *p)
    237 {
    238 	struct vnode *devvp;
    239 	struct ufs_args args;
    240 	struct ufsmount *ump = NULL;
    241 	struct lfs *fs = NULL;				/* LFS */
    242 	size_t size;
    243 	int error;
    244 	mode_t accessmode;
    245 
    246 	if (mp->mnt_flag & MNT_GETARGS) {
    247 		ump = VFSTOUFS(mp);
    248 		if (ump == NULL)
    249 			return EIO;
    250 		args.fspec = NULL;
    251 		vfs_showexport(mp, &args.export, &ump->um_export);
    252 		return copyout(&args, data, sizeof(args));
    253 	}
    254 	error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
    255 	if (error)
    256 		return (error);
    257 
    258 #if 0
    259 	/* Until LFS can do NFS right.		XXX */
    260 	if (args.export.ex_flags & MNT_EXPORTED)
    261 		return (EINVAL);
    262 #endif
    263 
    264 	/*
    265 	 * If updating, check whether changing from read-only to
    266 	 * read/write; if there is no device name, that's all we do.
    267 	 */
    268 	if (mp->mnt_flag & MNT_UPDATE) {
    269 		ump = VFSTOUFS(mp);
    270 		fs = ump->um_lfs;
    271 		if (fs->lfs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
    272 			/*
    273 			 * If upgrade to read-write by non-root, then verify
    274 			 * that user has necessary permissions on the device.
    275 			 */
    276 			if (p->p_ucred->cr_uid != 0) {
    277 				vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    278 				error = VOP_ACCESS(ump->um_devvp, VREAD|VWRITE,
    279 						   p->p_ucred, p);
    280 				VOP_UNLOCK(ump->um_devvp, 0);
    281 				if (error)
    282 					return (error);
    283 			}
    284 			fs->lfs_ronly = 0;
    285 		}
    286 		if (args.fspec == 0) {
    287 			/*
    288 			 * Process export requests.
    289 			 */
    290 			return (vfs_export(mp, &ump->um_export, &args.export));
    291 		}
    292 	}
    293 	/*
    294 	 * Not an update, or updating the name: look up the name
    295 	 * and verify that it refers to a sensible block device.
    296 	 */
    297 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
    298 	if ((error = namei(ndp)) != 0)
    299 		return (error);
    300 	devvp = ndp->ni_vp;
    301 	if (devvp->v_type != VBLK) {
    302 		vrele(devvp);
    303 		return (ENOTBLK);
    304 	}
    305 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
    306 		vrele(devvp);
    307 		return (ENXIO);
    308 	}
    309 	/*
    310 	 * If mount by non-root, then verify that user has necessary
    311 	 * permissions on the device.
    312 	 */
    313 	if (p->p_ucred->cr_uid != 0) {
    314 		accessmode = VREAD;
    315 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
    316 			accessmode |= VWRITE;
    317 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    318 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
    319 		if (error) {
    320 			vput(devvp);
    321 			return (error);
    322 		}
    323 		VOP_UNLOCK(devvp, 0);
    324 	}
    325 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
    326 		error = lfs_mountfs(devvp, mp, p);		/* LFS */
    327 	else {
    328 		if (devvp != ump->um_devvp)
    329 			error = EINVAL;	/* needs translation */
    330 		else
    331 			vrele(devvp);
    332 	}
    333 	if (error) {
    334 		vrele(devvp);
    335 		return (error);
    336 	}
    337 	ump = VFSTOUFS(mp);
    338 	fs = ump->um_lfs;					/* LFS */
    339 	(void)copyinstr(path, fs->lfs_fsmnt, sizeof(fs->lfs_fsmnt) - 1, &size);
    340 	bzero(fs->lfs_fsmnt + size, sizeof(fs->lfs_fsmnt) - size);
    341 	bcopy(fs->lfs_fsmnt, mp->mnt_stat.f_mntonname, MNAMELEN);
    342 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
    343 			 &size);
    344 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
    345 	return (0);
    346 }
    347 
    348 /*
    349  * Roll-forward code.
    350  */
    351 
    352 /*
    353  * Load the appropriate indirect block, and change the appropriate pointer.
    354  * Mark the block dirty.  Do segment and avail accounting.
    355  */
    356 static int
    357 update_meta(struct lfs *fs, ino_t ino, int version, daddr_t lbn,
    358 	    daddr_t ndaddr, size_t size, struct proc *p)
    359 {
    360 	int error;
    361 	struct vnode *vp;
    362 	struct inode *ip;
    363 	daddr_t odaddr, ooff;
    364 	struct indir a[NIADDR], *ap;
    365 	struct buf *bp;
    366 	SEGUSE *sup;
    367 	int num;
    368 
    369 	if ((error = lfs_rf_valloc(fs, ino, version, p, &vp)) != 0) {
    370 #ifdef DEBUG_LFS_RFW
    371 		printf("update_meta: ino %d: lfs_rf_valloc returned %d\n", ino,
    372 		       error);
    373 #endif
    374 		return error;
    375 	}
    376 
    377 	if ((error = VOP_BALLOC(vp, (lbn << fs->lfs_bshift), size,
    378 				NOCRED, 0, &bp)) != 0) {
    379 		vput(vp);
    380 		return (error);
    381 	}
    382 	/* No need to write, the block is already on disk */
    383 	if (bp->b_flags & B_DELWRI) {
    384 		LFS_UNLOCK_BUF(bp);
    385 		fs->lfs_avail += btofsb(fs, bp->b_bcount);
    386 	}
    387 	bp->b_flags |= B_INVAL;
    388 	brelse(bp);
    389 
    390 	/*
    391 	 * Extend the file, if it is not large enough already.
    392 	 * XXX this is not exactly right, we don't know how much of the
    393 	 * XXX last block is actually used.  We hope that an inode will
    394 	 * XXX appear later to give the correct size.
    395 	 */
    396 	ip = VTOI(vp);
    397 	if (ip->i_ffs_size <= (lbn << fs->lfs_bshift)) {
    398 		if (lbn < NDADDR)
    399 			ip->i_ffs_size = (lbn << fs->lfs_bshift) +
    400 				(size - fs->lfs_fsize) + 1;
    401 		else
    402 			ip->i_ffs_size = (lbn << fs->lfs_bshift) + 1;
    403 	}
    404 
    405 	error = ufs_bmaparray(vp, lbn, &odaddr, &a[0], &num, NULL);
    406 	if (error) {
    407 #ifdef DEBUG_LFS_RFW
    408 		printf("update_meta: ufs_bmaparray returned %d\n", error);
    409 #endif
    410 		vput(vp);
    411 		return error;
    412 	}
    413 	switch (num) {
    414 	    case 0:
    415 		ooff = ip->i_ffs_db[lbn];
    416 		if (ooff == UNWRITTEN)
    417 			ip->i_ffs_blocks += btofsb(fs, size);
    418 		/* XXX what about fragment extension? */
    419 		ip->i_ffs_db[lbn] = ndaddr;
    420 		break;
    421 	    case 1:
    422 		ooff = ip->i_ffs_ib[a[0].in_off];
    423 		if (ooff == UNWRITTEN)
    424 			ip->i_ffs_blocks += btofsb(fs, size);
    425 		ip->i_ffs_ib[a[0].in_off] = ndaddr;
    426 		break;
    427 	    default:
    428 		ap = &a[num - 1];
    429 		if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
    430 			panic("update_meta: bread bno %lld",
    431 			    (long long)ap->in_lbn);
    432 
    433 		/* XXX ondisk32 */
    434 		ooff = ((int32_t *)bp->b_data)[ap->in_off];
    435 		if (ooff == UNWRITTEN)
    436 			ip->i_ffs_blocks += btofsb(fs, size);
    437 		/* XXX ondisk32 */
    438 		((int32_t *)bp->b_data)[ap->in_off] = ndaddr;
    439 		(void) VOP_BWRITE(bp);
    440 	}
    441 	LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED | IN_UPDATE);
    442 
    443 	/* Update segment usage information. */
    444 	if (odaddr > 0) {
    445 		LFS_SEGENTRY(sup, fs, dtosn(fs, dbtofsb(fs, odaddr)), bp);
    446 #ifdef DIAGNOSTIC
    447 		if (sup->su_nbytes < size) {
    448 			panic("update_meta: negative bytes "
    449 			      "(segment %" PRId64 " short by %ld)\n",
    450 			      dtosn(fs, dbtofsb(fs, odaddr)), (long)size - sup->su_nbytes);
    451 			sup->su_nbytes = size;
    452 		}
    453 #endif
    454 		sup->su_nbytes -= size;
    455 		LFS_BWRITE_LOG(bp);
    456 	}
    457 	LFS_SEGENTRY(sup, fs, dtosn(fs, ndaddr), bp);
    458 	sup->su_nbytes += size;
    459 	LFS_BWRITE_LOG(bp);
    460 
    461 	/* Fix this so it can be released */
    462 	/* ip->i_lfs_effnblks = ip->i_ffs_blocks; */
    463 
    464 #ifdef DEBUG_LFS_RFW
    465 	/* Now look again to make sure it worked */
    466 	ufs_bmaparray(vp, lbn, &odaddr, &a[0], &num, NULL );
    467 	if (dbtofsb(fs, odaddr) != ndaddr)
    468 		printf("update_meta: failed setting ino %d lbn %d to %x\n",
    469 		       ino, lbn, ndaddr);
    470 #endif
    471 	vput(vp);
    472 	return 0;
    473 }
    474 
    475 static int
    476 update_inoblk(struct lfs *fs, daddr_t offset, struct ucred *cred,
    477 	      struct proc *p)
    478 {
    479 	struct vnode *devvp, *vp;
    480 	struct inode *ip;
    481 	struct dinode *dip;
    482 	struct buf *dbp, *ibp;
    483 	int error;
    484 	daddr_t daddr;
    485 	IFILE *ifp;
    486 	SEGUSE *sup;
    487 
    488 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
    489 
    490 	/*
    491 	 * Get the inode, update times and perms.
    492 	 * DO NOT update disk blocks, we do that separately.
    493 	 */
    494 	error = bread(devvp, fsbtodb(fs, offset), fs->lfs_ibsize, cred, &dbp);
    495 	if (error) {
    496 #ifdef DEBUG_LFS_RFW
    497 		printf("update_inoblk: bread returned %d\n", error);
    498 #endif
    499 		return error;
    500 	}
    501 	dip = ((struct dinode *)(dbp->b_data)) + INOPB(fs);
    502 	while (--dip >= (struct dinode *)dbp->b_data) {
    503 		if (dip->di_inumber > LFS_IFILE_INUM) {
    504 			/* printf("ino %d version %d\n", dip->di_inumber,
    505 			       dip->di_gen); */
    506 			error = lfs_rf_valloc(fs, dip->di_inumber, dip->di_gen,
    507 					      p, &vp);
    508 			if (error) {
    509 #ifdef DEBUG_LFS_RFW
    510 				printf("update_inoblk: lfs_rf_valloc returned %d\n", error);
    511 #endif
    512 				continue;
    513 			}
    514 			ip = VTOI(vp);
    515 			if (dip->di_size != ip->i_ffs_size)
    516 				VOP_TRUNCATE(vp, dip->di_size, 0, NOCRED, p);
    517 			/* Get mode, link count, size, and times */
    518 			memcpy(&ip->i_din.ffs_din, dip,
    519 			       offsetof(struct dinode, di_db[0]));
    520 
    521 			/* Then the rest, except di_blocks */
    522 			ip->i_ffs_flags = dip->di_flags;
    523 			ip->i_ffs_gen = dip->di_gen;
    524 			ip->i_ffs_uid = dip->di_uid;
    525 			ip->i_ffs_gid = dip->di_gid;
    526 
    527 			ip->i_ffs_effnlink = dip->di_nlink;
    528 
    529 			LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED | IN_UPDATE);
    530 
    531 			/* Re-initialize to get type right */
    532 			ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p,
    533 				  &vp);
    534 			vput(vp);
    535 
    536 			/* Record change in location */
    537 			LFS_IENTRY(ifp, fs, dip->di_inumber, ibp);
    538 			daddr = ifp->if_daddr;
    539 			ifp->if_daddr = dbtofsb(fs, dbp->b_blkno);
    540 			error = LFS_BWRITE_LOG(ibp); /* Ifile */
    541 			/* And do segment accounting */
    542 			if (dtosn(fs, daddr) != dtosn(fs, dbtofsb(fs, dbp->b_blkno))) {
    543 				if (daddr > 0) {
    544 					LFS_SEGENTRY(sup, fs, dtosn(fs, daddr),
    545 						     ibp);
    546 					sup->su_nbytes -= DINODE_SIZE;
    547 					LFS_BWRITE_LOG(ibp);
    548 				}
    549 				LFS_SEGENTRY(sup, fs, dtosn(fs, dbtofsb(fs, dbp->b_blkno)),
    550 					     ibp);
    551 				sup->su_nbytes += DINODE_SIZE;
    552 				LFS_BWRITE_LOG(ibp);
    553 			}
    554 		}
    555 	}
    556 	dbp->b_flags |= B_AGE;
    557 	brelse(dbp);
    558 
    559 	return 0;
    560 }
    561 
    562 #define CHECK_CKSUM   0x0001  /* Check the checksum to make sure it's valid */
    563 #define CHECK_UPDATE  0x0002  /* Update Ifile for new data blocks / inodes */
    564 
    565 static daddr_t
    566 check_segsum(struct lfs *fs, daddr_t offset,
    567 	     struct ucred *cred, int flags, int *pseg_flags, struct proc *p)
    568 {
    569 	struct vnode *devvp;
    570 	struct buf *bp, *dbp;
    571 	int error, nblocks, ninos, i, j;
    572 	SEGSUM *ssp;
    573 	u_long *dp, *datap; /* XXX u_int32_t */
    574 	daddr_t oldoffset;
    575 	int32_t *iaddr;	/* XXX ondisk32 */
    576 	FINFO *fip;
    577 	SEGUSE *sup;
    578 	size_t size;
    579 	u_int64_t serial;
    580 
    581 	devvp = VTOI(fs->lfs_ivnode)->i_devvp;
    582 	/*
    583 	 * If the segment has a superblock and we're at the top
    584 	 * of the segment, skip the superblock.
    585 	 */
    586 	if (sntod(fs, dtosn(fs, offset)) == offset) {
    587        		LFS_SEGENTRY(sup, fs, dtosn(fs, offset), bp);
    588        		if (sup->su_flags & SEGUSE_SUPERBLOCK)
    589 			offset += btofsb(fs, LFS_SBPAD);
    590        		brelse(bp);
    591 	}
    592 
    593 	/* Read in the segment summary */
    594 	error = bread(devvp, offset, fs->lfs_sumsize, cred, &bp);
    595 	if (error)
    596 		return -1;
    597 
    598 	/* Check summary checksum */
    599 	ssp = (SEGSUM *)bp->b_data;
    600 	if (flags & CHECK_CKSUM) {
    601 		if (ssp->ss_sumsum != cksum(&ssp->ss_datasum,
    602 					   fs->lfs_sumsize -
    603 					   sizeof(ssp->ss_sumsum))) {
    604 #ifdef DEBUG_LFS_RFW
    605 			printf("Sumsum error at 0x%x\n", offset);
    606 #endif
    607 			offset = -1;
    608 			goto err1;
    609 		}
    610 		if (ssp->ss_nfinfo == 0 && ssp->ss_ninos == 0) {
    611 #ifdef DEBUG_LFS_RFW
    612 			printf("Empty pseg at 0x%x\n", offset);
    613 #endif
    614 			offset = -1;
    615 			goto err1;
    616 		}
    617 		if (ssp->ss_create < fs->lfs_tstamp) {
    618 #ifdef DEBUG_LFS_RFW
    619 			printf("Old data at 0x%x\n", offset);
    620 #endif
    621 			offset = -1;
    622 			goto err1;
    623 		}
    624 	}
    625 	if (fs->lfs_version > 1) {
    626 		serial = ssp->ss_serial;
    627 		if (serial != fs->lfs_serial + 1) {
    628 #ifdef DEBUG_LFS_RFW
    629 			printf("Unexpected serial number at 0x%x\n", offset);
    630 #endif
    631 			offset = -1;
    632 			goto err1;
    633 		}
    634 		if (ssp->ss_ident != fs->lfs_ident) {
    635 #ifdef DEBUG_LFS_RFW
    636 			printf("Incorrect fsid (0x%x vs 0x%x) at 0x%x\n",
    637 			       ssp->ss_ident, fs->lfs_ident, offset);
    638 #endif
    639 			offset = -1;
    640 			goto err1;
    641 		}
    642 	}
    643 	if (pseg_flags)
    644 		*pseg_flags = ssp->ss_flags;
    645 	oldoffset = offset;
    646 	offset += btofsb(fs, fs->lfs_sumsize);
    647 
    648 	ninos = howmany(ssp->ss_ninos, INOPB(fs));
    649 	/* XXX ondisk32 */
    650 	iaddr = (int32_t *)(bp->b_data + fs->lfs_sumsize - sizeof(int32_t));
    651 	if (flags & CHECK_CKSUM) {
    652 		/* Count blocks */
    653 		nblocks = 0;
    654 		fip = (FINFO *)(bp->b_data + SEGSUM_SIZE(fs));
    655 		for (i = 0; i < ssp->ss_nfinfo; ++i) {
    656 			nblocks += fip->fi_nblocks;
    657 			if (fip->fi_nblocks <= 0)
    658 				break;
    659 			/* XXX ondisk32 */
    660 			fip = (FINFO *)(((char *)fip) + sizeof(FINFO) +
    661 					(fip->fi_nblocks - 1) *
    662 					sizeof(int32_t));
    663 		}
    664 		nblocks += ninos;
    665 		/* Create the sum array */
    666 		datap = dp = (u_long *)malloc(nblocks * sizeof(u_long),
    667 					      M_SEGMENT, M_WAITOK);
    668 	}
    669 
    670 	/* Handle individual blocks */
    671 	fip = (FINFO *)(bp->b_data + SEGSUM_SIZE(fs));
    672 	for (i = 0; i < ssp->ss_nfinfo || ninos; ++i) {
    673 		/* Inode block? */
    674 		if (ninos && *iaddr == offset) {
    675 			if (flags & CHECK_CKSUM) {
    676 				/* Read in the head and add to the buffer */
    677 				error = bread(devvp, fsbtodb(fs, offset), fs->lfs_bsize,
    678 					      cred, &dbp);
    679 				if (error) {
    680 					offset = -1;
    681 					goto err2;
    682 				}
    683 				(*dp++) = ((u_long *)(dbp->b_data))[0];
    684 				dbp->b_flags |= B_AGE;
    685 				brelse(dbp);
    686 			}
    687 			if (flags & CHECK_UPDATE) {
    688 				if ((error = update_inoblk(fs, offset, cred, p))
    689 				    != 0) {
    690 					offset = -1;
    691 					goto err2;
    692 				}
    693 			}
    694 			offset += btofsb(fs, fs->lfs_ibsize);
    695 			--iaddr;
    696 			--ninos;
    697 			--i; /* compensate */
    698 			continue;
    699 		}
    700 		/* printf("check: blocks from ino %d version %d\n",
    701 		       fip->fi_ino, fip->fi_version); */
    702 		size = fs->lfs_bsize;
    703 		for (j = 0; j < fip->fi_nblocks; ++j) {
    704 			if (j == fip->fi_nblocks - 1)
    705 				size = fip->fi_lastlength;
    706 			if (flags & CHECK_CKSUM) {
    707 				error = bread(devvp, fsbtodb(fs, offset), size, cred, &dbp);
    708 				if (error) {
    709 					offset = -1;
    710 					goto err2;
    711 				}
    712 				(*dp++) = ((u_long *)(dbp->b_data))[0];
    713 				dbp->b_flags |= B_AGE;
    714 				brelse(dbp);
    715 			}
    716 			/* Account for and update any direct blocks */
    717 			if ((flags & CHECK_UPDATE) &&
    718 			   fip->fi_ino > LFS_IFILE_INUM &&
    719 			   fip->fi_blocks[j] >= 0) {
    720 				update_meta(fs, fip->fi_ino, fip->fi_version,
    721 					    fip->fi_blocks[j], offset, size, p);
    722 			}
    723 			offset += btofsb(fs, size);
    724 		}
    725 		/* XXX ondisk32 */
    726 		fip = (FINFO *)(((char *)fip) + sizeof(FINFO)
    727 				+ (fip->fi_nblocks - 1) * sizeof(int32_t));
    728 	}
    729 	/* Checksum the array, compare */
    730 	if ((flags & CHECK_CKSUM) &&
    731 	   ssp->ss_datasum != cksum(datap, nblocks * sizeof(u_long)))
    732 	{
    733 #ifdef DEBUG_LFS_RFW
    734 		printf("Datasum error at 0x%x (wanted %x got %x)\n", offset,
    735 		       ssp->ss_datasum, cksum(datap, nblocks *
    736 					      sizeof(u_long)));
    737 #endif
    738 		offset = -1;
    739 		goto err2;
    740 	}
    741 
    742 	/* If we're at the end of the segment, move to the next */
    743 	if (dtosn(fs, offset + btofsb(fs, fs->lfs_sumsize + fs->lfs_bsize)) !=
    744 	   dtosn(fs, offset)) {
    745 		if (dtosn(fs, offset) == dtosn(fs, ssp->ss_next)) {
    746 			offset = -1;
    747 			goto err2;
    748 		}
    749 		offset = ssp->ss_next;
    750 #ifdef DEBUG_LFS_RFW
    751 		printf("LFS roll forward: moving on to offset 0x%x "
    752 		       " -> segment %d\n", offset, dtosn(fs,offset));
    753 #endif
    754 	}
    755 
    756 	if (flags & CHECK_UPDATE) {
    757 		fs->lfs_avail -= (offset - oldoffset);
    758 		/* Don't clog the buffer queue */
    759 		if (locked_queue_count > LFS_MAX_BUFS ||
    760 		    locked_queue_bytes > LFS_MAX_BYTES) {
    761 			++fs->lfs_writer;
    762 			lfs_flush(fs, SEGM_CKP);
    763 			if (--fs->lfs_writer == 0)
    764 				wakeup(&fs->lfs_dirops);
    765 		}
    766 	}
    767 
    768     err2:
    769 	if (flags & CHECK_CKSUM)
    770 		free(datap, M_SEGMENT);
    771     err1:
    772 	bp->b_flags |= B_AGE;
    773 	brelse(bp);
    774 
    775 	/* XXX should we update the serial number even for bad psegs? */
    776 	if ((flags & CHECK_UPDATE) && offset > 0 && fs->lfs_version > 1)
    777 		fs->lfs_serial = serial;
    778 	return offset;
    779 }
    780 
    781 /*
    782  * Common code for mount and mountroot
    783  * LFS specific
    784  */
    785 int
    786 lfs_mountfs(struct vnode *devvp, struct mount *mp, struct proc *p)
    787 {
    788 	extern struct vnode *rootvp;
    789 	struct dlfs *tdfs, *dfs, *adfs;
    790 	struct lfs *fs;
    791 	struct ufsmount *ump;
    792 	struct vnode *vp;
    793 	struct buf *bp, *abp;
    794 	struct partinfo dpart;
    795 	dev_t dev;
    796 	int error, i, ronly, secsize, fsbsize;
    797 	struct ucred *cred;
    798 	CLEANERINFO *cip;
    799         SEGUSE *sup;
    800 	int flags, dirty, do_rollforward;
    801 	daddr_t offset, oldoffset, lastgoodpseg, sb_addr;
    802 	int sn, curseg;
    803 
    804 	cred = p ? p->p_ucred : NOCRED;
    805 	/*
    806 	 * Disallow multiple mounts of the same device.
    807 	 * Disallow mounting of a device that is currently in use
    808 	 * (except for root, which might share swap device for miniroot).
    809 	 * Flush out any old buffers remaining from a previous use.
    810 	 */
    811 	if ((error = vfs_mountedon(devvp)) != 0)
    812 		return (error);
    813 	if (vcount(devvp) > 1 && devvp != rootvp)
    814 		return (EBUSY);
    815 	if ((error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) != 0)
    816 		return (error);
    817 
    818 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    819 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
    820 	if (error)
    821 		return (error);
    822 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
    823 		secsize = DEV_BSIZE;
    824 	else
    825 		secsize = dpart.disklab->d_secsize;
    826 
    827 	/* Don't free random space on error. */
    828 	bp = NULL;
    829 	abp = NULL;
    830 	ump = NULL;
    831 
    832 	sb_addr = LFS_LABELPAD / secsize;
    833 	while (1) {
    834 		/* Read in the superblock. */
    835 		error = bread(devvp, sb_addr, LFS_SBPAD, cred, &bp);
    836 		if (error)
    837 			goto out;
    838 		dfs = (struct dlfs *)bp->b_data;
    839 
    840 		/* Check the basics. */
    841 		if (dfs->dlfs_magic != LFS_MAGIC || dfs->dlfs_bsize >= MAXBSIZE ||
    842 		    dfs->dlfs_version > LFS_VERSION ||
    843 		    dfs->dlfs_bsize < sizeof(struct dlfs)) {
    844 #ifdef DEBUG_LFS
    845 			printf("lfs_mountfs: primary superblock sanity failed\n");
    846 #endif
    847 			error = EINVAL;		/* XXX needs translation */
    848 			goto out;
    849 		}
    850 		if (dfs->dlfs_inodefmt > LFS_MAXINODEFMT)
    851 			printf("lfs_mountfs: warning: unknown inode format %d\n",
    852 			       dfs->dlfs_inodefmt);
    853 
    854 		if (dfs->dlfs_version == 1)
    855 			fsbsize = secsize;
    856 		else {
    857 			fsbsize = 1 << (dfs->dlfs_bshift - dfs->dlfs_blktodb +
    858 				dfs->dlfs_fsbtodb);
    859 			/*
    860 			 * Could be, if the frag size is large enough, that we
    861 			 * don't have the "real" primary superblock.  If that's
    862 			 * the case, get the real one, and try again.
    863 			 */
    864 			if (sb_addr != dfs->dlfs_sboffs[0] <<
    865                                        dfs->dlfs_fsbtodb) {
    866 /* #ifdef DEBUG_LFS */
    867 				printf("lfs_mountfs: sb daddr 0x%llx is not right, trying 0x%llx\n",
    868 					(long long)sb_addr, (long long)(dfs->dlfs_sboffs[0] <<
    869 						 dfs->dlfs_fsbtodb));
    870 /* #endif */
    871 				sb_addr = dfs->dlfs_sboffs[0] <<
    872 					  dfs->dlfs_fsbtodb;
    873 				brelse(bp);
    874 				continue;
    875 			}
    876 		}
    877 		break;
    878 	}
    879 
    880 	/*
    881 	 * Check the second superblock to see which is newer; then mount
    882 	 * using the older of the two.  This is necessary to ensure that
    883 	 * the filesystem is valid if it was not unmounted cleanly.
    884 	 */
    885 
    886 	if (dfs->dlfs_sboffs[1] &&
    887 	    dfs->dlfs_sboffs[1] - LFS_LABELPAD / fsbsize > LFS_SBPAD / fsbsize)
    888 	{
    889 		error = bread(devvp, dfs->dlfs_sboffs[1] * (fsbsize / secsize),
    890 			LFS_SBPAD, cred, &abp);
    891 		if (error)
    892 			goto out;
    893 		adfs = (struct dlfs *)abp->b_data;
    894 
    895 		if (dfs->dlfs_version == 1) {
    896 			/* 1s resolution comparison */
    897 			if (adfs->dlfs_tstamp < dfs->dlfs_tstamp)
    898 				tdfs = adfs;
    899 			else
    900 				tdfs = dfs;
    901 		} else {
    902 			/* monotonic infinite-resolution comparison */
    903 			if (adfs->dlfs_serial < dfs->dlfs_serial)
    904 				tdfs = adfs;
    905 			else
    906 				tdfs = dfs;
    907 		}
    908 
    909 		/* Check the basics. */
    910 		if (tdfs->dlfs_magic != LFS_MAGIC ||
    911 		    tdfs->dlfs_bsize > MAXBSIZE ||
    912 	    	    tdfs->dlfs_version > LFS_VERSION ||
    913 	    	    tdfs->dlfs_bsize < sizeof(struct dlfs)) {
    914 #ifdef DEBUG_LFS
    915 			printf("lfs_mountfs: alt superblock sanity failed\n");
    916 #endif
    917 			error = EINVAL;		/* XXX needs translation */
    918 			goto out;
    919 		}
    920 	} else {
    921 #ifdef DEBUG_LFS
    922 		printf("lfs_mountfs: invalid alt superblock daddr=0x%x\n",
    923 			dfs->dlfs_sboffs[1]);
    924 #endif
    925 		error = EINVAL;
    926 		goto out;
    927 	}
    928 
    929 	/* Allocate the mount structure, copy the superblock into it. */
    930 	fs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK | M_ZERO);
    931 	memcpy(&fs->lfs_dlfs, tdfs, sizeof(struct dlfs));
    932 
    933 	/* Compatibility */
    934 	if (fs->lfs_version < 2) {
    935 		fs->lfs_sumsize = LFS_V1_SUMMARY_SIZE;
    936 		fs->lfs_ibsize = fs->lfs_bsize;
    937 		fs->lfs_start = fs->lfs_sboffs[0];
    938 		fs->lfs_tstamp = fs->lfs_otstamp;
    939 		fs->lfs_fsbtodb = 0;
    940 	}
    941 
    942 	/* Before rolling forward, lock so vget will sleep for other procs */
    943 	fs->lfs_flags = LFS_NOTYET;
    944 	fs->lfs_rfpid = p->p_pid;
    945 
    946 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK | M_ZERO);
    947 	ump->um_lfs = fs;
    948 	if (sizeof(struct lfs) < LFS_SBPAD) {			/* XXX why? */
    949 		bp->b_flags |= B_INVAL;
    950 		abp->b_flags |= B_INVAL;
    951 	}
    952 	brelse(bp);
    953 	bp = NULL;
    954 	brelse(abp);
    955 	abp = NULL;
    956 
    957 	/* Set up the I/O information */
    958 	fs->lfs_devbsize = secsize;
    959 	fs->lfs_iocount = 0;
    960 	fs->lfs_diropwait = 0;
    961 	fs->lfs_activesb = 0;
    962 	fs->lfs_uinodes = 0;
    963 	fs->lfs_ravail = 0;
    964 	fs->lfs_sbactive = 0;
    965 
    966 	/* Set up the ifile and lock aflags */
    967 	fs->lfs_doifile = 0;
    968 	fs->lfs_writer = 0;
    969 	fs->lfs_dirops = 0;
    970 	fs->lfs_nadirop = 0;
    971 	fs->lfs_seglock = 0;
    972 	lockinit(&fs->lfs_freelock, PINOD, "lfs_freelock", 0, 0);
    973 	lockinit(&fs->lfs_fraglock, PINOD, "lfs_fraglock", 0, 0);
    974 
    975 	/* Set the file system readonly/modify bits. */
    976 	fs->lfs_ronly = ronly;
    977 	if (ronly == 0)
    978 		fs->lfs_fmod = 1;
    979 
    980 	/* Initialize the mount structure. */
    981 	dev = devvp->v_rdev;
    982 	mp->mnt_data = ump;
    983 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
    984 	mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_LFS);
    985 	mp->mnt_stat.f_iosize = fs->lfs_bsize;
    986 	mp->mnt_maxsymlinklen = fs->lfs_maxsymlinklen;
    987 	mp->mnt_flag |= MNT_LOCAL;
    988 	ump->um_flags = 0;
    989 	ump->um_mountp = mp;
    990 	ump->um_dev = dev;
    991 	ump->um_devvp = devvp;
    992 	ump->um_bptrtodb = fs->lfs_fsbtodb;
    993 	ump->um_seqinc = fragstofsb(fs, fs->lfs_frag);
    994 	ump->um_nindir = fs->lfs_nindir;
    995 	ump->um_lognindir = ffs(fs->lfs_nindir) - 1;
    996 	for (i = 0; i < MAXQUOTAS; i++)
    997 		ump->um_quotas[i] = NULLVP;
    998 	devvp->v_specmountpoint = mp;
    999 
   1000 	/*
   1001 	 * We use the ifile vnode for almost every operation.  Instead of
   1002 	 * retrieving it from the hash table each time we retrieve it here,
   1003 	 * artificially increment the reference count and keep a pointer
   1004 	 * to it in the incore copy of the superblock.
   1005 	 */
   1006 	if ((error = VFS_VGET(mp, LFS_IFILE_INUM, &vp)) != 0) {
   1007 #ifdef DEBUG
   1008 		printf("lfs_mountfs: ifile vget failed, error=%d\n", error);
   1009 #endif
   1010 		goto out;
   1011 	}
   1012 	fs->lfs_ivnode = vp;
   1013 	VREF(vp);
   1014 
   1015 	/*
   1016 	 * Roll forward.
   1017 	 *
   1018 	 * We don't automatically roll forward for v1 filesystems, because
   1019 	 * of the danger that the clock was turned back between the last
   1020 	 * checkpoint and crash.  This would roll forward garbage.
   1021 	 *
   1022 	 * v2 filesystems don't have this problem because they use a
   1023 	 * monotonically increasing serial number instead of a timestamp.
   1024 	 */
   1025 #ifdef LFS_DO_ROLLFORWARD
   1026 	do_rollforward = !fs->lfs_ronly;
   1027 #else
   1028 	do_rollforward = (fs->lfs_version > 1 && !fs->lfs_ronly &&
   1029 			  !(fs->lfs_pflags & LFS_PF_CLEAN));
   1030 #endif
   1031 	if (do_rollforward) {
   1032 		/*
   1033 		 * Phase I: Find the address of the last good partial
   1034 		 * segment that was written after the checkpoint.  Mark
   1035 		 * the segments in question dirty, so they won't be
   1036 		 * reallocated.
   1037 		 */
   1038 		lastgoodpseg = oldoffset = offset = fs->lfs_offset;
   1039 		flags = 0x0;
   1040 #ifdef DEBUG_LFS_RFW
   1041 		printf("LFS roll forward phase 1: starting at offset 0x%x\n",
   1042 		       offset);
   1043 #endif
   1044 		LFS_SEGENTRY(sup, fs, dtosn(fs, offset), bp);
   1045 		if (!(sup->su_flags & SEGUSE_DIRTY))
   1046 			--fs->lfs_nclean;
   1047 		sup->su_flags |= SEGUSE_DIRTY;
   1048 		(void) LFS_BWRITE_LOG(bp);
   1049 		while ((offset = check_segsum(fs, offset, cred, CHECK_CKSUM,
   1050 					      &flags, p)) > 0)
   1051 		{
   1052 			if (sntod(fs, oldoffset) != sntod(fs, offset)) {
   1053 				LFS_SEGENTRY(sup, fs, dtosn(fs, oldoffset),
   1054 					     bp);
   1055 				if (!(sup->su_flags & SEGUSE_DIRTY))
   1056 					--fs->lfs_nclean;
   1057 				sup->su_flags |= SEGUSE_DIRTY;
   1058 				(void) LFS_BWRITE_LOG(bp);
   1059 			}
   1060 
   1061 #ifdef DEBUG_LFS_RFW
   1062 			printf("LFS roll forward phase 1: offset=0x%x\n",
   1063 			       offset);
   1064 			if (flags & SS_DIROP) {
   1065 				printf("lfs_mountfs: dirops at 0x%x\n",
   1066 				       oldoffset);
   1067 				if (!(flags & SS_CONT))
   1068 					printf("lfs_mountfs: dirops end "
   1069 					       "at 0x%x\n", oldoffset);
   1070 			}
   1071 #endif
   1072 			if (!(flags & SS_CONT))
   1073 				lastgoodpseg = offset;
   1074 			oldoffset = offset;
   1075 		}
   1076 #ifdef DEBUG_LFS_RFW
   1077 		if (flags & SS_CONT) {
   1078 			printf("LFS roll forward: warning: incomplete "
   1079 			       "dirops discarded\n");
   1080 		}
   1081 		printf("LFS roll forward phase 1: completed: "
   1082 		       "lastgoodpseg=0x%x\n", lastgoodpseg);
   1083 #endif
   1084 		oldoffset = fs->lfs_offset;
   1085 		if (fs->lfs_offset != lastgoodpseg) {
   1086 			/* Don't overwrite what we're trying to preserve */
   1087 			offset = fs->lfs_offset;
   1088 			fs->lfs_offset = lastgoodpseg;
   1089 			fs->lfs_curseg = sntod(fs, dtosn(fs, fs->lfs_offset));
   1090 			for (sn = curseg = dtosn(fs, fs->lfs_curseg);;) {
   1091 				sn = (sn + 1) % fs->lfs_nseg;
   1092 				if (sn == curseg)
   1093 					panic("lfs_mountfs: no clean segments");
   1094 				LFS_SEGENTRY(sup, fs, sn, bp);
   1095 				dirty = (sup->su_flags & SEGUSE_DIRTY);
   1096 				brelse(bp);
   1097 				if (!dirty)
   1098 					break;
   1099 			}
   1100 			fs->lfs_nextseg = sntod(fs, sn);
   1101 
   1102 			/*
   1103 			 * Phase II: Roll forward from the first superblock.
   1104 			 */
   1105 			while (offset != lastgoodpseg) {
   1106 #ifdef DEBUG_LFS_RFW
   1107 				printf("LFS roll forward phase 2: 0x%x\n",
   1108 				       offset);
   1109 #endif
   1110 				offset = check_segsum(fs, offset, cred,
   1111 						      CHECK_UPDATE, NULL, p);
   1112 			}
   1113 
   1114 			/*
   1115 			 * Finish: flush our changes to disk.
   1116 			 */
   1117 			lfs_segwrite(mp, SEGM_CKP | SEGM_SYNC);
   1118 			printf("lfs_mountfs: roll forward recovered %lld blocks\n",
   1119 			       (long long)(lastgoodpseg - oldoffset));
   1120 		}
   1121 #ifdef DEBUG_LFS_RFW
   1122 		printf("LFS roll forward complete\n");
   1123 #endif
   1124 	}
   1125 	/* If writing, sb is not clean; record in case of immediate crash */
   1126 	if (!fs->lfs_ronly) {
   1127 		fs->lfs_pflags &= ~LFS_PF_CLEAN;
   1128 		lfs_writesuper(fs, fs->lfs_sboffs[0]);
   1129 	}
   1130 
   1131 	/* Allow vget now that roll-forward is complete */
   1132 	fs->lfs_flags &= ~(LFS_NOTYET);
   1133 	wakeup(&fs->lfs_flags);
   1134 
   1135 	/*
   1136 	 * Initialize the ifile cleaner info with information from
   1137 	 * the superblock.
   1138 	 */
   1139 	LFS_CLEANERINFO(cip, fs, bp);
   1140 	cip->clean = fs->lfs_nclean;
   1141 	cip->dirty = fs->lfs_nseg - fs->lfs_nclean;
   1142 	cip->avail = fs->lfs_avail;
   1143 	cip->bfree = fs->lfs_bfree;
   1144 	(void) LFS_BWRITE_LOG(bp); /* Ifile */
   1145 
   1146 	/*
   1147 	 * Mark the current segment as ACTIVE, since we're going to
   1148 	 * be writing to it.
   1149 	 */
   1150         LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_offset), bp);
   1151         sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
   1152         (void) LFS_BWRITE_LOG(bp); /* Ifile */
   1153 
   1154 	/* Now that roll-forward is done, unlock the Ifile */
   1155 	vput(vp);
   1156 
   1157 	/* Comment on ifile size if it is too large */
   1158 	if (fs->lfs_ivnode->v_size / fs->lfs_bsize > LFS_MAX_BUFS) {
   1159 		fs->lfs_flags |= LFS_WARNED;
   1160 		printf("lfs_mountfs: please consider increasing NBUF to at least %lld\n",
   1161 			(long long)(fs->lfs_ivnode->v_size / fs->lfs_bsize) * (nbuf / LFS_MAX_BUFS));
   1162 	}
   1163 	if (fs->lfs_ivnode->v_size > LFS_MAX_BYTES) {
   1164 		fs->lfs_flags |= LFS_WARNED;
   1165 		printf("lfs_mountfs: please consider increasing BUFPAGES to at least %lld\n",
   1166 			(long long)fs->lfs_ivnode->v_size * bufpages / LFS_MAX_BYTES);
   1167 	}
   1168 
   1169 	return (0);
   1170 out:
   1171 	if (bp)
   1172 		brelse(bp);
   1173 	if (abp)
   1174 		brelse(abp);
   1175 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
   1176 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
   1177 	VOP_UNLOCK(devvp, 0);
   1178 	if (ump) {
   1179 		free(ump->um_lfs, M_UFSMNT);
   1180 		free(ump, M_UFSMNT);
   1181 		mp->mnt_data = NULL;
   1182 	}
   1183 	return (error);
   1184 }
   1185 
   1186 /*
   1187  * unmount system call
   1188  */
   1189 int
   1190 lfs_unmount(struct mount *mp, int mntflags, struct proc *p)
   1191 {
   1192 	struct ufsmount *ump;
   1193 	struct lfs *fs;
   1194 	int error, flags, ronly;
   1195 
   1196 	flags = 0;
   1197 	if (mntflags & MNT_FORCE)
   1198 		flags |= FORCECLOSE;
   1199 
   1200 	ump = VFSTOUFS(mp);
   1201 	fs = ump->um_lfs;
   1202 #ifdef QUOTA
   1203 	if (mp->mnt_flag & MNT_QUOTA) {
   1204 		int i;
   1205 		error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags);
   1206 		if (error)
   1207 			return (error);
   1208 		for (i = 0; i < MAXQUOTAS; i++) {
   1209 			if (ump->um_quotas[i] == NULLVP)
   1210 				continue;
   1211 			quotaoff(p, mp, i);
   1212 		}
   1213 		/*
   1214 		 * Here we fall through to vflush again to ensure
   1215 		 * that we have gotten rid of all the system vnodes.
   1216 		 */
   1217 	}
   1218 #endif
   1219 	if ((error = vflush(mp, fs->lfs_ivnode, flags)) != 0)
   1220 		return (error);
   1221 	if ((error = VFS_SYNC(mp, 1, p->p_ucred, p)) != 0)
   1222 		return (error);
   1223 	if (LIST_FIRST(&fs->lfs_ivnode->v_dirtyblkhd))
   1224 		panic("lfs_unmount: still dirty blocks on ifile vnode");
   1225 
   1226 	/* Explicitly write the superblock, to update serial and pflags */
   1227 	fs->lfs_pflags |= LFS_PF_CLEAN;
   1228 	lfs_writesuper(fs, fs->lfs_sboffs[0]);
   1229 	lfs_writesuper(fs, fs->lfs_sboffs[1]);
   1230 
   1231 	/* Comment on ifile size if it has become too large */
   1232 	if (!(fs->lfs_flags & LFS_WARNED)) {
   1233 		if (fs->lfs_ivnode->v_size / fs->lfs_bsize > LFS_MAX_BUFS)
   1234 			printf("lfs_unmount: please consider increasing"
   1235 				" NBUF to at least %lld\n",
   1236 				(long long)(fs->lfs_ivnode->v_size /
   1237 					    fs->lfs_bsize) *
   1238 				(long long)(nbuf / LFS_MAX_BUFS));
   1239 		if (fs->lfs_ivnode->v_size > LFS_MAX_BYTES)
   1240 			printf("lfs_unmount: please consider increasing"
   1241 				" BUFPAGES to at least %lld\n",
   1242 				(long long)fs->lfs_ivnode->v_size *
   1243 				bufpages / LFS_MAX_BYTES);
   1244 	}
   1245 
   1246 	/* Finish with the Ifile, now that we're done with it */
   1247 	vrele(fs->lfs_ivnode);
   1248 	vgone(fs->lfs_ivnode);
   1249 
   1250 	/* Wait for superblock writes to complete */
   1251 	while (fs->lfs_iocount)
   1252 		tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs_umount", 0);
   1253 
   1254 	ronly = !fs->lfs_ronly;
   1255 	if (ump->um_devvp->v_type != VBAD)
   1256 		ump->um_devvp->v_specmountpoint = NULL;
   1257 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
   1258 	error = VOP_CLOSE(ump->um_devvp,
   1259 	    ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
   1260 	vput(ump->um_devvp);
   1261 
   1262 	/* XXX KS - wake up the cleaner so it can die */
   1263 	wakeup(&fs->lfs_nextseg);
   1264 	wakeup(&lfs_allclean_wakeup);
   1265 
   1266 	free(fs, M_UFSMNT);
   1267 	free(ump, M_UFSMNT);
   1268 	mp->mnt_data = NULL;
   1269 	mp->mnt_flag &= ~MNT_LOCAL;
   1270 	return (error);
   1271 }
   1272 
   1273 /*
   1274  * Get file system statistics.
   1275  */
   1276 int
   1277 lfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p)
   1278 {
   1279 	struct lfs *fs;
   1280 	struct ufsmount *ump;
   1281 
   1282 	ump = VFSTOUFS(mp);
   1283 	fs = ump->um_lfs;
   1284 	if (fs->lfs_magic != LFS_MAGIC)
   1285 		panic("lfs_statfs: magic");
   1286 
   1287 	sbp->f_type = 0;
   1288 	sbp->f_bsize = fs->lfs_fsize;
   1289 	sbp->f_iosize = fs->lfs_bsize;
   1290 	sbp->f_blocks = fsbtofrags(fs, LFS_EST_NONMETA(fs));
   1291 	sbp->f_bfree = fsbtofrags(fs, LFS_EST_BFREE(fs));
   1292 	sbp->f_bavail = fsbtofrags(fs, (long)LFS_EST_BFREE(fs) -
   1293 				  (long)LFS_EST_RSVD(fs));
   1294 
   1295 	sbp->f_files = fs->lfs_bfree / btofsb(fs, fs->lfs_ibsize) * INOPB(fs);
   1296 	sbp->f_ffree = sbp->f_files - fs->lfs_nfiles;
   1297 	if (sbp != &mp->mnt_stat) {
   1298 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
   1299 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
   1300 	}
   1301 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
   1302 	return (0);
   1303 }
   1304 
   1305 /*
   1306  * Go through the disk queues to initiate sandbagged IO;
   1307  * go through the inodes to write those that have been modified;
   1308  * initiate the writing of the super block if it has been modified.
   1309  *
   1310  * Note: we are always called with the filesystem marked `MPBUSY'.
   1311  */
   1312 int
   1313 lfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p)
   1314 {
   1315 	int error;
   1316 	struct lfs *fs;
   1317 
   1318 	fs = ((struct ufsmount *)mp->mnt_data)->ufsmount_u.lfs;
   1319 	if (fs->lfs_ronly)
   1320 		return 0;
   1321 	while (fs->lfs_dirops)
   1322 		error = tsleep(&fs->lfs_writer, PRIBIO + 1, "lfs_dirops", 0);
   1323 	fs->lfs_writer++;
   1324 
   1325 	/* All syncs must be checkpoints until roll-forward is implemented. */
   1326 	error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
   1327 	if (--fs->lfs_writer == 0)
   1328 		wakeup(&fs->lfs_dirops);
   1329 #ifdef QUOTA
   1330 	qsync(mp);
   1331 #endif
   1332 	return (error);
   1333 }
   1334 
   1335 extern struct lock ufs_hashlock;
   1336 
   1337 /*
   1338  * Look up an LFS dinode number to find its incore vnode.  If not already
   1339  * in core, read it in from the specified device.  Return the inode locked.
   1340  * Detection and handling of mount points must be done by the calling routine.
   1341  */
   1342 int
   1343 lfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
   1344 {
   1345 	struct lfs *fs;
   1346 	struct dinode *dip;
   1347 	struct inode *ip;
   1348 	struct buf *bp;
   1349 	struct ifile *ifp;
   1350 	struct vnode *vp;
   1351 	struct ufsmount *ump;
   1352 	daddr_t daddr;
   1353 	dev_t dev;
   1354 	int i, error, retries;
   1355 	struct timespec ts;
   1356 
   1357 	ump = VFSTOUFS(mp);
   1358 	dev = ump->um_dev;
   1359 	fs = ump->um_lfs;
   1360 
   1361 	/*
   1362 	 * If the filesystem is not completely mounted yet, suspend
   1363 	 * any access requests (wait for roll-forward to complete).
   1364 	 */
   1365 	while ((fs->lfs_flags & LFS_NOTYET) && curproc->p_pid != fs->lfs_rfpid)
   1366 		tsleep(&fs->lfs_flags, PRIBIO+1, "lfs_notyet", 0);
   1367 
   1368 	if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
   1369 		return (0);
   1370 
   1371 	if ((error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, &vp)) != 0) {
   1372 		*vpp = NULL;
   1373 		 return (error);
   1374 	}
   1375 
   1376 	do {
   1377 		if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
   1378 			ungetnewvnode(vp);
   1379 			return (0);
   1380 		}
   1381 	} while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
   1382 
   1383 	/* Translate the inode number to a disk address. */
   1384 	if (ino == LFS_IFILE_INUM)
   1385 		daddr = fs->lfs_idaddr;
   1386 	else {
   1387 		/* XXX bounds-check this too */
   1388 		LFS_IENTRY(ifp, fs, ino, bp);
   1389 		daddr = ifp->if_daddr;
   1390 		if (fs->lfs_version > 1) {
   1391 			ts.tv_sec = ifp->if_atime_sec;
   1392 			ts.tv_nsec = ifp->if_atime_nsec;
   1393 		}
   1394 
   1395 		brelse(bp);
   1396 		if (daddr == LFS_UNUSED_DADDR) {
   1397 			*vpp = NULLVP;
   1398 			ungetnewvnode(vp);
   1399 			lockmgr(&ufs_hashlock, LK_RELEASE, 0);
   1400 			return (ENOENT);
   1401 		}
   1402 	}
   1403 
   1404 	/* Allocate/init new vnode/inode. */
   1405 	lfs_vcreate(mp, ino, vp);
   1406 
   1407 	/*
   1408 	 * Put it onto its hash chain and lock it so that other requests for
   1409 	 * this inode will block if they arrive while we are sleeping waiting
   1410 	 * for old data structures to be purged or for the contents of the
   1411 	 * disk portion of this inode to be read.
   1412 	 */
   1413 	ip = VTOI(vp);
   1414 	ufs_ihashins(ip);
   1415 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
   1416 
   1417 	/*
   1418 	 * XXX
   1419 	 * This may not need to be here, logically it should go down with
   1420 	 * the i_devvp initialization.
   1421 	 * Ask Kirk.
   1422 	 */
   1423 	ip->i_lfs = ump->um_lfs;
   1424 
   1425 	/* Read in the disk contents for the inode, copy into the inode. */
   1426 	retries = 0;
   1427     again:
   1428 	error = bread(ump->um_devvp, fsbtodb(fs, daddr),
   1429 		(fs->lfs_version == 1 ? fs->lfs_bsize : fs->lfs_ibsize),
   1430 		NOCRED, &bp);
   1431 	if (error) {
   1432 		/*
   1433 		 * The inode does not contain anything useful, so it would
   1434 		 * be misleading to leave it on its hash chain. With mode
   1435 		 * still zero, it will be unlinked and returned to the free
   1436 		 * list by vput().
   1437 		 */
   1438 		vput(vp);
   1439 		brelse(bp);
   1440 		*vpp = NULL;
   1441 		return (error);
   1442 	}
   1443 
   1444 	dip = lfs_ifind(fs, ino, bp);
   1445 	if (dip == NULL) {
   1446 		/* Assume write has not completed yet; try again */
   1447 		bp->b_flags |= B_INVAL;
   1448 		brelse(bp);
   1449 		++retries;
   1450 		if (retries > LFS_IFIND_RETRIES) {
   1451 #ifdef DEBUG
   1452 			/* If the seglock is held look at the bpp to see
   1453 			   what is there anyway */
   1454 			if (fs->lfs_seglock > 0) {
   1455 				struct buf **bpp;
   1456 				struct dinode *dp;
   1457 				int i;
   1458 
   1459 				for (bpp = fs->lfs_sp->bpp;
   1460 				     bpp != fs->lfs_sp->cbpp; ++bpp) {
   1461 					if ((*bpp)->b_vp == fs->lfs_ivnode &&
   1462 					    bpp != fs->lfs_sp->bpp) {
   1463 						/* Inode block */
   1464 						printf("block 0x%" PRIx64 ": ",
   1465 						    (*bpp)->b_blkno);
   1466 						dp = (struct dinode *)(*bpp)->b_data;
   1467 						for (i = 0; i < INOPB(fs); i++)
   1468 							if (dp[i].di_u.inumber)
   1469 								printf("%d ", dp[i].di_u.inumber);
   1470 						printf("\n");
   1471 					}
   1472 				}
   1473 			}
   1474 #endif
   1475 			panic("lfs_vget: dinode not found");
   1476 		}
   1477 		printf("lfs_vget: dinode %d not found, retrying...\n", ino);
   1478 		(void)tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs ifind", 1);
   1479 		goto again;
   1480 	}
   1481 	ip->i_din.ffs_din = *dip;
   1482 
   1483 	ip->i_ffs_effnlink = ip->i_ffs_nlink;
   1484 	ip->i_lfs_effnblks = ip->i_ffs_blocks;
   1485 	ip->i_lfs_osize = ip->i_ffs_size;
   1486 	if (fs->lfs_version > 1) {
   1487 		ip->i_ffs_atime = ts.tv_sec;
   1488 		ip->i_ffs_atimensec = ts.tv_nsec;
   1489 	}
   1490 	brelse(bp);
   1491 
   1492         memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
   1493         for (i = 0; i < NDADDR; i++)
   1494                 if (ip->i_ffs_db[i] != 0)
   1495                         ip->i_lfs_fragsize[i] = blksize(fs, ip, i);
   1496 
   1497 	/*
   1498 	 * Initialize the vnode from the inode, check for aliases.  In all
   1499 	 * cases re-init ip, the underlying vnode/inode may have changed.
   1500 	 */
   1501 	ufs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp);
   1502 #ifdef DIAGNOSTIC
   1503 	if (vp->v_type == VNON) {
   1504 		panic("lfs_vget: ino %d is type VNON! (ifmt %o)",
   1505 		       ip->i_number, (ip->i_ffs_mode & IFMT) >> 12);
   1506 	}
   1507 #endif
   1508 	/*
   1509 	 * Finish inode initialization now that aliasing has been resolved.
   1510 	 */
   1511 
   1512 	genfs_node_init(vp, &lfs_genfsops);
   1513 	ip->i_devvp = ump->um_devvp;
   1514 	VREF(ip->i_devvp);
   1515 	*vpp = vp;
   1516 
   1517 	uvm_vnp_setsize(vp, ip->i_ffs_size);
   1518 
   1519 	return (0);
   1520 }
   1521 
   1522 /*
   1523  * File handle to vnode
   1524  *
   1525  * Have to be really careful about stale file handles:
   1526  * - check that the inode number is valid
   1527  * - call lfs_vget() to get the locked inode
   1528  * - check for an unallocated inode (i_mode == 0)
   1529  *
   1530  * XXX
   1531  * use ifile to see if inode is allocated instead of reading off disk
   1532  * what is the relationship between my generational number and the NFS
   1533  * generational number.
   1534  */
   1535 int
   1536 lfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
   1537 {
   1538 	struct ufid *ufhp;
   1539 
   1540 	ufhp = (struct ufid *)fhp;
   1541 	if (ufhp->ufid_ino < ROOTINO)
   1542 		return (ESTALE);
   1543 	return (ufs_fhtovp(mp, ufhp, vpp));
   1544 }
   1545 
   1546 /*
   1547  * Vnode pointer to File handle
   1548  */
   1549 /* ARGSUSED */
   1550 int
   1551 lfs_vptofh(struct vnode *vp, struct fid *fhp)
   1552 {
   1553 	struct inode *ip;
   1554 	struct ufid *ufhp;
   1555 
   1556 	ip = VTOI(vp);
   1557 	ufhp = (struct ufid *)fhp;
   1558 	ufhp->ufid_len = sizeof(struct ufid);
   1559 	ufhp->ufid_ino = ip->i_number;
   1560 	ufhp->ufid_gen = ip->i_ffs_gen;
   1561 	return (0);
   1562 }
   1563 
   1564 int
   1565 lfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen, struct proc *p)
   1566 {
   1567 	extern int lfs_writeindir, lfs_dostats, lfs_clean_vnhead;
   1568 	extern struct lfs_stats lfs_stats;
   1569 	int error;
   1570 
   1571 	/* all sysctl names at this level are terminal */
   1572 	if (namelen != 1)
   1573 		return (ENOTDIR);
   1574 
   1575 	switch (name[0]) {
   1576 	case LFS_WRITEINDIR:
   1577 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1578 				   &lfs_writeindir));
   1579 	case LFS_CLEAN_VNHEAD:
   1580 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1581 				   &lfs_clean_vnhead));
   1582 	case LFS_DOSTATS:
   1583 		if ((error = sysctl_int(oldp, oldlenp, newp, newlen,
   1584 				       &lfs_dostats)))
   1585 			return error;
   1586 		if (lfs_dostats == 0)
   1587 			memset(&lfs_stats,0,sizeof(lfs_stats));
   1588 		return 0;
   1589 	case LFS_STATS:
   1590 		return (sysctl_rdstruct(oldp, oldlenp, newp,
   1591 					&lfs_stats, sizeof(lfs_stats)));
   1592 	default:
   1593 		return (EOPNOTSUPP);
   1594 	}
   1595 	/* NOTREACHED */
   1596 }
   1597