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