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