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