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