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