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