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