Home | History | Annotate | Line # | Download | only in lfs
lfs_vfsops.c revision 1.50
      1 /*	$NetBSD: lfs_vfsops.c,v 1.50 2000/04/29 00:23:00 perseant Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 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 <vm/vm.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 /*
    143  * Initialize the filesystem, most work done by ufs_init.
    144  */
    145 void
    146 lfs_init()
    147 {
    148 	ufs_init();
    149 
    150 	/*
    151 	 * XXX Same structure as FFS inodes?  Should we share a common pool?
    152 	 */
    153 	pool_init(&lfs_inode_pool, sizeof(struct inode), 0, 0, 0,
    154 		  "lfsinopl", 0, pool_page_alloc_nointr, pool_page_free_nointr,
    155 		  M_LFSNODE);
    156 }
    157 
    158 void
    159 lfs_done()
    160 {
    161 	ufs_done();
    162 	pool_destroy(&lfs_inode_pool);
    163 }
    164 
    165 /*
    166  * Called by main() when ufs is going to be mounted as root.
    167  */
    168 int
    169 lfs_mountroot()
    170 {
    171 	extern struct vnode *rootvp;
    172 	struct mount *mp;
    173 	struct proc *p = curproc;	/* XXX */
    174 	int error;
    175 
    176 	if (root_device->dv_class != DV_DISK)
    177 		return (ENODEV);
    178 
    179 	if (rootdev == NODEV)
    180 	  	return (ENODEV);
    181 	/*
    182 	 * Get vnodes for swapdev and rootdev.
    183 	 */
    184 	if ((error = bdevvp(rootdev, &rootvp))) {
    185 		printf("lfs_mountroot: can't setup bdevvp's");
    186 		return (error);
    187 	}
    188 	if ((error = vfs_rootmountalloc(MOUNT_LFS, "root_device", &mp))) {
    189 		vrele(rootvp);
    190 		return (error);
    191 	}
    192 	if ((error = lfs_mountfs(rootvp, mp, p))) {
    193 		mp->mnt_op->vfs_refcount--;
    194 		vfs_unbusy(mp);
    195 		free(mp, M_MOUNT);
    196 		vrele(rootvp);
    197 		return (error);
    198 	}
    199 	simple_lock(&mountlist_slock);
    200 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    201 	simple_unlock(&mountlist_slock);
    202 	(void)lfs_statfs(mp, &mp->mnt_stat, p);
    203 	vfs_unbusy(mp);
    204 	return (0);
    205 }
    206 
    207 /*
    208  * VFS Operations.
    209  *
    210  * mount system call
    211  */
    212 int
    213 lfs_mount(mp, path, data, ndp, p)
    214 	struct mount *mp;
    215 	const char *path;
    216 	void *data;
    217 	struct nameidata *ndp;
    218 	struct proc *p;
    219 {
    220 	struct vnode *devvp;
    221 	struct ufs_args args;
    222 	struct ufsmount *ump = NULL;
    223 	struct lfs *fs = NULL;				/* LFS */
    224 	size_t size;
    225 	int error;
    226 	mode_t accessmode;
    227 
    228 	error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
    229 	if (error)
    230 		return (error);
    231 
    232 #if 0
    233 	/* Until LFS can do NFS right.		XXX */
    234 	if (args.export.ex_flags & MNT_EXPORTED)
    235 		return (EINVAL);
    236 #endif
    237 
    238 	/*
    239 	 * If updating, check whether changing from read-only to
    240 	 * read/write; if there is no device name, that's all we do.
    241 	 */
    242 	if (mp->mnt_flag & MNT_UPDATE) {
    243 		ump = VFSTOUFS(mp);
    244 		fs = ump->um_lfs;
    245 		if (fs->lfs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
    246 			/*
    247 			 * If upgrade to read-write by non-root, then verify
    248 			 * that user has necessary permissions on the device.
    249 			 */
    250 			if (p->p_ucred->cr_uid != 0) {
    251 				vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    252 				error = VOP_ACCESS(ump->um_devvp, VREAD|VWRITE,
    253 						   p->p_ucred, p);
    254 				VOP_UNLOCK(ump->um_devvp, 0);
    255 				if (error)
    256 					return (error);
    257 			}
    258 			fs->lfs_ronly = 0;
    259 		}
    260 		if (args.fspec == 0) {
    261 			/*
    262 			 * Process export requests.
    263 			 */
    264 			return (vfs_export(mp, &ump->um_export, &args.export));
    265 		}
    266 	}
    267 	/*
    268 	 * Not an update, or updating the name: look up the name
    269 	 * and verify that it refers to a sensible block device.
    270 	 */
    271 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
    272 	if ((error = namei(ndp)) != 0)
    273 		return (error);
    274 	devvp = ndp->ni_vp;
    275 	if (devvp->v_type != VBLK) {
    276 		vrele(devvp);
    277 		return (ENOTBLK);
    278 	}
    279 	if (major(devvp->v_rdev) >= nblkdev) {
    280 		vrele(devvp);
    281 		return (ENXIO);
    282 	}
    283 	/*
    284 	 * If mount by non-root, then verify that user has necessary
    285 	 * permissions on the device.
    286 	 */
    287 	if (p->p_ucred->cr_uid != 0) {
    288 		accessmode = VREAD;
    289 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
    290 			accessmode |= VWRITE;
    291 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    292 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
    293 		if (error) {
    294 			vput(devvp);
    295 			return (error);
    296 		}
    297 		VOP_UNLOCK(devvp, 0);
    298 	}
    299 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
    300 		error = lfs_mountfs(devvp, mp, p);		/* LFS */
    301 	else {
    302 		if (devvp != ump->um_devvp)
    303 			error = EINVAL;	/* needs translation */
    304 		else
    305 			vrele(devvp);
    306 	}
    307 	if (error) {
    308 		vrele(devvp);
    309 		return (error);
    310 	}
    311 	ump = VFSTOUFS(mp);
    312 	fs = ump->um_lfs;					/* LFS */
    313 	(void)copyinstr(path, fs->lfs_fsmnt, sizeof(fs->lfs_fsmnt) - 1, &size);
    314 	bzero(fs->lfs_fsmnt + size, sizeof(fs->lfs_fsmnt) - size);
    315 	bcopy(fs->lfs_fsmnt, mp->mnt_stat.f_mntonname, MNAMELEN);
    316 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
    317 			 &size);
    318 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
    319 	return (0);
    320 }
    321 
    322 /*
    323  * Common code for mount and mountroot
    324  * LFS specific
    325  */
    326 int
    327 lfs_mountfs(devvp, mp, p)
    328 	struct vnode *devvp;
    329 	struct mount *mp;
    330 	struct proc *p;
    331 {
    332 	extern struct vnode *rootvp;
    333 	struct dlfs *dfs, *adfs;
    334 	struct lfs *fs;
    335 	struct ufsmount *ump;
    336 	struct vnode *vp;
    337 	struct buf *bp, *abp;
    338 	struct partinfo dpart;
    339 	dev_t dev;
    340 	int error, i, ronly, size;
    341 	struct ucred *cred;
    342         SEGUSE *sup;
    343 
    344 	cred = p ? p->p_ucred : NOCRED;
    345 	/*
    346 	 * Disallow multiple mounts of the same device.
    347 	 * Disallow mounting of a device that is currently in use
    348 	 * (except for root, which might share swap device for miniroot).
    349 	 * Flush out any old buffers remaining from a previous use.
    350 	 */
    351 	if ((error = vfs_mountedon(devvp)) != 0)
    352 		return (error);
    353 	if (vcount(devvp) > 1 && devvp != rootvp)
    354 		return (EBUSY);
    355 	if ((error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) != 0)
    356 		return (error);
    357 
    358 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    359 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
    360 	if (error)
    361 		return (error);
    362 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
    363 		size = DEV_BSIZE;
    364 	else
    365 		size = dpart.disklab->d_secsize;
    366 
    367 	/* Don't free random space on error. */
    368 	bp = NULL;
    369 	abp = NULL;
    370 	ump = NULL;
    371 
    372 	/* Read in the superblock. */
    373 	error = bread(devvp, LFS_LABELPAD / size, LFS_SBPAD, cred, &bp);
    374 	if (error)
    375 		goto out;
    376 	dfs = (struct dlfs *)bp->b_data;
    377 
    378 	/* Check the basics. */
    379 	if (dfs->dlfs_magic != LFS_MAGIC || dfs->dlfs_bsize > MAXBSIZE ||
    380 	    dfs->dlfs_version > LFS_VERSION ||
    381 	    dfs->dlfs_bsize < sizeof(struct dlfs)) {
    382 		error = EINVAL;		/* XXX needs translation */
    383 		goto out;
    384 	}
    385 
    386 	/*
    387 	 * Check the second superblock to see which is newer; then mount
    388 	 * using the older of the two.  This is necessary to ensure that
    389 	 * the filesystem is valid if it was not unmounted cleanly.
    390 	 */
    391 	if (dfs->dlfs_sboffs[1] &&
    392 	    dfs->dlfs_sboffs[1]-(LFS_LABELPAD/size) > LFS_SBPAD/size)
    393 	{
    394 		error = bread(devvp, dfs->dlfs_sboffs[1], LFS_SBPAD, cred, &abp);
    395 		if (error)
    396 			goto out;
    397 		adfs = (struct dlfs *)abp->b_data;
    398 
    399 		if (adfs->dlfs_tstamp < dfs->dlfs_tstamp) /* XXX 1s? */
    400 			dfs = adfs;
    401 	} else {
    402 		printf("lfs_mountfs: invalid alt superblock daddr=0x%x\n",
    403 			dfs->dlfs_sboffs[1]);
    404 		error = EINVAL;
    405 		goto out;
    406 	}
    407 
    408 	/* Allocate the mount structure, copy the superblock into it. */
    409 	fs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK);
    410 	memcpy(&fs->lfs_dlfs, dfs, sizeof(struct dlfs));
    411 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
    412 	memset((caddr_t)ump, 0, sizeof *ump);
    413 	ump->um_lfs = fs;
    414 	if (sizeof(struct lfs) < LFS_SBPAD)			/* XXX why? */
    415 		bp->b_flags |= B_INVAL;
    416 	brelse(bp);
    417 	bp = NULL;
    418 	brelse(abp);
    419 	abp = NULL;
    420 
    421 	/* Set up the I/O information */
    422 	fs->lfs_iocount = 0;
    423 	fs->lfs_diropwait = 0;
    424 	fs->lfs_activesb = 0;
    425 #ifdef LFS_CANNOT_ROLLFW
    426 	fs->lfs_sbactive = NULL;
    427 #endif
    428 #ifdef LFS_TRACK_IOS
    429 	for (i=0;i<LFS_THROTTLE;i++)
    430 		fs->lfs_pending[i] = LFS_UNUSED_DADDR;
    431 #endif
    432 
    433 	/* Set up the ifile and lock aflags */
    434 	fs->lfs_doifile = 0;
    435 	fs->lfs_writer = 0;
    436 	fs->lfs_dirops = 0;
    437 	fs->lfs_seglock = 0;
    438 	lockinit(&fs->lfs_freelock, PINOD, "lfs_freelock", 0, 0);
    439 
    440 	/* Set the file system readonly/modify bits. */
    441 	fs->lfs_ronly = ronly;
    442 	if (ronly == 0)
    443 		fs->lfs_fmod = 1;
    444 
    445 	/* Initialize the mount structure. */
    446 	dev = devvp->v_rdev;
    447 	mp->mnt_data = (qaddr_t)ump;
    448 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
    449 	mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_LFS);
    450 	mp->mnt_stat.f_iosize = fs->lfs_bsize;
    451 	mp->mnt_maxsymlinklen = fs->lfs_maxsymlinklen;
    452 	mp->mnt_flag |= MNT_LOCAL;
    453 	ump->um_flags = 0;
    454 	ump->um_mountp = mp;
    455 	ump->um_dev = dev;
    456 	ump->um_devvp = devvp;
    457 	ump->um_bptrtodb = 0;
    458 	ump->um_seqinc = 1 << fs->lfs_fsbtodb;
    459 	ump->um_nindir = fs->lfs_nindir;
    460 	for (i = 0; i < MAXQUOTAS; i++)
    461 		ump->um_quotas[i] = NULLVP;
    462 	devvp->v_specmountpoint = mp;
    463 
    464 	/*
    465 	 * We use the ifile vnode for almost every operation.  Instead of
    466 	 * retrieving it from the hash table each time we retrieve it here,
    467 	 * artificially increment the reference count and keep a pointer
    468 	 * to it in the incore copy of the superblock.
    469 	 */
    470 	if ((error = VFS_VGET(mp, LFS_IFILE_INUM, &vp)) != 0)
    471 		goto out;
    472 	fs->lfs_ivnode = vp;
    473 	VREF(vp);
    474 	vput(vp);
    475 
    476 	/*
    477 	 * Mark the current segment as ACTIVE, since we're going to
    478 	 * be writing to it.
    479 	 */
    480         LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_offset), bp);
    481         sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
    482         (void) VOP_BWRITE(bp);
    483 
    484 	return (0);
    485 out:
    486 	if (bp)
    487 		brelse(bp);
    488 	if (abp)
    489 		brelse(abp);
    490 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    491 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
    492 	VOP_UNLOCK(devvp, 0);
    493 	if (ump) {
    494 		free(ump->um_lfs, M_UFSMNT);
    495 		free(ump, M_UFSMNT);
    496 		mp->mnt_data = (qaddr_t)0;
    497 	}
    498 	return (error);
    499 }
    500 
    501 /*
    502  * unmount system call
    503  */
    504 int
    505 lfs_unmount(mp, mntflags, p)
    506 	struct mount *mp;
    507 	int mntflags;
    508 	struct proc *p;
    509 {
    510 	struct ufsmount *ump;
    511 	struct lfs *fs;
    512 	int error, flags, ronly;
    513 	extern int lfs_allclean_wakeup;
    514 
    515 	flags = 0;
    516 	if (mntflags & MNT_FORCE)
    517 		flags |= FORCECLOSE;
    518 
    519 	ump = VFSTOUFS(mp);
    520 	fs = ump->um_lfs;
    521 #ifdef QUOTA
    522 	if (mp->mnt_flag & MNT_QUOTA) {
    523 		int i;
    524 		error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags);
    525 		if (error)
    526 			return (error);
    527 		for (i = 0; i < MAXQUOTAS; i++) {
    528 			if (ump->um_quotas[i] == NULLVP)
    529 				continue;
    530 			quotaoff(p, mp, i);
    531 		}
    532 		/*
    533 		 * Here we fall through to vflush again to ensure
    534 		 * that we have gotten rid of all the system vnodes.
    535 		 */
    536 	}
    537 #endif
    538 	if ((error = vflush(mp, fs->lfs_ivnode, flags)) != 0)
    539 		return (error);
    540 	fs->lfs_clean = 1;
    541 	if ((error = VFS_SYNC(mp, 1, p->p_ucred, p)) != 0)
    542 		return (error);
    543 	if (fs->lfs_ivnode->v_dirtyblkhd.lh_first)
    544 		panic("lfs_unmount: still dirty blocks on ifile vnode\n");
    545 	vrele(fs->lfs_ivnode);
    546 	vgone(fs->lfs_ivnode);
    547 
    548 	ronly = !fs->lfs_ronly;
    549 	if (ump->um_devvp->v_type != VBAD)
    550 		ump->um_devvp->v_specmountpoint = NULL;
    551 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
    552 	error = VOP_CLOSE(ump->um_devvp,
    553 	    ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
    554 	vput(ump->um_devvp);
    555 
    556 	/* XXX KS - wake up the cleaner so it can die */
    557 	wakeup(&fs->lfs_nextseg);
    558 	wakeup(&lfs_allclean_wakeup);
    559 
    560 	free(fs, M_UFSMNT);
    561 	free(ump, M_UFSMNT);
    562 	mp->mnt_data = (qaddr_t)0;
    563 	mp->mnt_flag &= ~MNT_LOCAL;
    564 	return (error);
    565 }
    566 
    567 /*
    568  * Get file system statistics.
    569  */
    570 int
    571 lfs_statfs(mp, sbp, p)
    572 	struct mount *mp;
    573 	struct statfs *sbp;
    574 	struct proc *p;
    575 {
    576 	struct lfs *fs;
    577 	struct ufsmount *ump;
    578 
    579 	ump = VFSTOUFS(mp);
    580 	fs = ump->um_lfs;
    581 	if (fs->lfs_magic != LFS_MAGIC)
    582 		panic("lfs_statfs: magic");
    583 	sbp->f_type = 0;
    584 	sbp->f_bsize = fs->lfs_fsize;
    585 	sbp->f_iosize = fs->lfs_bsize;
    586 	sbp->f_blocks = dbtofrags(fs, fs->lfs_dsize);
    587 	sbp->f_bfree = dbtofrags(fs, fs->lfs_bfree);
    588 	/*
    589 	 * To compute the available space.  Subtract the minimum free
    590 	 * from the total number of blocks in the file system.	Set avail
    591 	 * to the smaller of this number and fs->lfs_bfree.
    592 	 *
    593 	 * XXX KS - is my modification below what is desired?  (This
    594 	 * will, e.g., change the report when the cleaner runs.)
    595 	 */
    596 #if 0
    597         sbp->f_bavail = (long) ((u_int64_t) fs->lfs_dsize * (u_int64_t)
    598 		(100 - fs->lfs_minfree) / (u_int64_t) 100);
    599 	sbp->f_bavail =
    600 	    sbp->f_bavail > fs->lfs_bfree ? fs->lfs_bfree : sbp->f_bavail;
    601 #else
    602 	sbp->f_bavail = (long) ((u_int64_t) fs->lfs_dsize * (u_int64_t)
    603 				(100 - fs->lfs_minfree) / (u_int64_t) 100)
    604 		- (u_int64_t)(fs->lfs_dsize - fs->lfs_bfree);
    605 #endif
    606 	sbp->f_bavail = dbtofrags(fs, sbp->f_bavail);
    607 	sbp->f_files = dbtofsb(fs,fs->lfs_bfree) * INOPB(fs);
    608 	sbp->f_ffree = sbp->f_files - fs->lfs_nfiles;
    609 	if (sbp != &mp->mnt_stat) {
    610 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
    611 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
    612 	}
    613 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
    614 	return (0);
    615 }
    616 
    617 /*
    618  * Go through the disk queues to initiate sandbagged IO;
    619  * go through the inodes to write those that have been modified;
    620  * initiate the writing of the super block if it has been modified.
    621  *
    622  * Note: we are always called with the filesystem marked `MPBUSY'.
    623  */
    624 int
    625 lfs_sync(mp, waitfor, cred, p)
    626 	struct mount *mp;
    627 	int waitfor;
    628 	struct ucred *cred;
    629 	struct proc *p;
    630 {
    631 	int error;
    632 	struct lfs *fs;
    633 
    634 	fs = ((struct ufsmount *)mp->mnt_data)->ufsmount_u.lfs;
    635 	while(fs->lfs_dirops)
    636 		error = tsleep(&fs->lfs_dirops, PRIBIO + 1, "lfs_dirops", 0);
    637 	fs->lfs_writer++;
    638 
    639 	/* All syncs must be checkpoints until roll-forward is implemented. */
    640 	error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
    641 	if(--fs->lfs_writer==0)
    642 		wakeup(&fs->lfs_dirops);
    643 #ifdef QUOTA
    644 	qsync(mp);
    645 #endif
    646 	return (error);
    647 }
    648 
    649 extern struct lock ufs_hashlock;
    650 
    651 /*
    652  * Look up an LFS dinode number to find its incore vnode.  If not already
    653  * in core, read it in from the specified device.  Return the inode locked.
    654  * Detection and handling of mount points must be done by the calling routine.
    655  */
    656 int
    657 lfs_vget(mp, ino, vpp)
    658 	struct mount *mp;
    659 	ino_t ino;
    660 	struct vnode **vpp;
    661 {
    662 	struct lfs *fs;
    663 	struct inode *ip;
    664 	struct buf *bp;
    665 	struct ifile *ifp;
    666 	struct vnode *vp;
    667 	struct ufsmount *ump;
    668 	ufs_daddr_t daddr;
    669 	dev_t dev;
    670 	int error;
    671 #ifdef LFS_ATIME_IFILE
    672 	struct timespec ts;
    673 #endif
    674 
    675 	ump = VFSTOUFS(mp);
    676 	dev = ump->um_dev;
    677 
    678 	do {
    679 		if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
    680 			return (0);
    681 	} while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
    682 
    683 	/* Translate the inode number to a disk address. */
    684 	fs = ump->um_lfs;
    685 	if (ino == LFS_IFILE_INUM)
    686 		daddr = fs->lfs_idaddr;
    687 	else {
    688 		LFS_IENTRY(ifp, fs, ino, bp);
    689 		daddr = ifp->if_daddr;
    690 #ifdef LFS_ATIME_IFILE
    691 		ts = ifp->if_atime; /* structure copy */
    692 #endif
    693 		brelse(bp);
    694 		if (daddr == LFS_UNUSED_DADDR) {
    695 			lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    696 			return (ENOENT);
    697 		}
    698 	}
    699 
    700 	/* Allocate new vnode/inode. */
    701 	if ((error = lfs_vcreate(mp, ino, &vp)) != 0) {
    702 		*vpp = NULL;
    703 		lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    704 		return (error);
    705 	}
    706 
    707 	/*
    708 	 * Put it onto its hash chain and lock it so that other requests for
    709 	 * this inode will block if they arrive while we are sleeping waiting
    710 	 * for old data structures to be purged or for the contents of the
    711 	 * disk portion of this inode to be read.
    712 	 */
    713 	ip = VTOI(vp);
    714 	ufs_ihashins(ip);
    715 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    716 
    717 	/*
    718 	 * XXX
    719 	 * This may not need to be here, logically it should go down with
    720 	 * the i_devvp initialization.
    721 	 * Ask Kirk.
    722 	 */
    723 	ip->i_lfs = ump->um_lfs;
    724 
    725 	/* Read in the disk contents for the inode, copy into the inode. */
    726 	error = bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp);
    727 	if (error) {
    728 		/*
    729 		 * The inode does not contain anything useful, so it would
    730 		 * be misleading to leave it on its hash chain. With mode
    731 		 * still zero, it will be unlinked and returned to the free
    732 		 * list by vput().
    733 		 */
    734 		vput(vp);
    735 		brelse(bp);
    736 		*vpp = NULL;
    737 		return (error);
    738 	}
    739 	ip->i_din.ffs_din = *lfs_ifind(fs, ino, bp);
    740 	ip->i_ffs_effnlink = ip->i_ffs_nlink;
    741 #ifdef LFS_ATIME_IFILE
    742 	ip->i_ffs_atime = ts.tv_sec;
    743 	ip->i_ffs_atimensec = ts.tv_nsec;
    744 #endif
    745 	brelse(bp);
    746 
    747 	/*
    748 	 * Initialize the vnode from the inode, check for aliases.  In all
    749 	 * cases re-init ip, the underlying vnode/inode may have changed.
    750 	 */
    751 	error = ufs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp);
    752 	if (error) {
    753 		vput(vp);
    754 		*vpp = NULL;
    755 		return (error);
    756 	}
    757 	if(vp->v_type == VNON) {
    758 		printf("lfs_vget: ino %d is type VNON! (ifmt %o)\n", ip->i_number, (ip->i_ffs_mode&IFMT)>>12);
    759 #ifdef DDB
    760 		Debugger();
    761 #endif
    762 	}
    763 	/*
    764 	 * Finish inode initialization now that aliasing has been resolved.
    765 	 */
    766 	ip->i_devvp = ump->um_devvp;
    767 	VREF(ip->i_devvp);
    768 	*vpp = vp;
    769 
    770 	return (0);
    771 }
    772 
    773 /*
    774  * File handle to vnode
    775  *
    776  * Have to be really careful about stale file handles:
    777  * - check that the inode number is valid
    778  * - call lfs_vget() to get the locked inode
    779  * - check for an unallocated inode (i_mode == 0)
    780  *
    781  * XXX
    782  * use ifile to see if inode is allocated instead of reading off disk
    783  * what is the relationship between my generational number and the NFS
    784  * generational number.
    785  */
    786 int
    787 lfs_fhtovp(mp, fhp, vpp)
    788 	struct mount *mp;
    789 	struct fid *fhp;
    790 	struct vnode **vpp;
    791 {
    792 	struct ufid *ufhp;
    793 
    794 	ufhp = (struct ufid *)fhp;
    795 	if (ufhp->ufid_ino < ROOTINO)
    796 		return (ESTALE);
    797 	return (ufs_fhtovp(mp, ufhp, vpp));
    798 }
    799 
    800 /*
    801  * Vnode pointer to File handle
    802  */
    803 /* ARGSUSED */
    804 int
    805 lfs_vptofh(vp, fhp)
    806 	struct vnode *vp;
    807 	struct fid *fhp;
    808 {
    809 	struct inode *ip;
    810 	struct ufid *ufhp;
    811 
    812 	ip = VTOI(vp);
    813 	ufhp = (struct ufid *)fhp;
    814 	ufhp->ufid_len = sizeof(struct ufid);
    815 	ufhp->ufid_ino = ip->i_number;
    816 	ufhp->ufid_gen = ip->i_ffs_gen;
    817 	return (0);
    818 }
    819 
    820 int
    821 lfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    822 	int *name;
    823 	u_int namelen;
    824 	void *oldp;
    825 	size_t *oldlenp;
    826 	void *newp;
    827 	size_t newlen;
    828 	struct proc *p;
    829 {
    830 	extern int lfs_writeindir, lfs_dostats, lfs_clean_vnhead;
    831 	extern struct lfs_stats lfs_stats;
    832 	int error;
    833 
    834 	/* all sysctl names at this level are terminal */
    835 	if (namelen != 1)
    836 		return (ENOTDIR);
    837 
    838 	switch (name[0]) {
    839 	case LFS_WRITEINDIR:
    840 		return (sysctl_int(oldp, oldlenp, newp, newlen,
    841 				   &lfs_writeindir));
    842 	case LFS_CLEAN_VNHEAD:
    843 		return (sysctl_int(oldp, oldlenp, newp, newlen,
    844 				   &lfs_clean_vnhead));
    845 	case LFS_DOSTATS:
    846 		if((error = sysctl_int(oldp, oldlenp, newp, newlen,
    847 				       &lfs_dostats)))
    848 			return error;
    849 		if(lfs_dostats == 0)
    850 			memset(&lfs_stats,0,sizeof(lfs_stats));
    851 		return 0;
    852 	case LFS_STATS:
    853 		return (sysctl_rdstruct(oldp, oldlenp, newp,
    854 					&lfs_stats, sizeof(lfs_stats)));
    855 	default:
    856 		return (EOPNOTSUPP);
    857 	}
    858 	/* NOTREACHED */
    859 }
    860