xref: /csrg-svn/sys/ufs/lfs/lfs_vfsops.c (revision 67949)
123400Smckusick /*
266805Sbostic  * Copyright (c) 1989, 1991, 1993, 1994
363375Sbostic  *	The Regents of the University of California.  All rights reserved.
423400Smckusick  *
544539Sbostic  * %sccs.include.redist.c%
637737Smckusick  *
7*67949Smckusick  *	@(#)lfs_vfsops.c	8.10 (Berkeley) 11/21/94
823400Smckusick  */
912795Ssam 
1051483Sbostic #include <sys/param.h>
1151483Sbostic #include <sys/systm.h>
1251483Sbostic #include <sys/namei.h>
1351483Sbostic #include <sys/proc.h>
1451483Sbostic #include <sys/kernel.h>
1551483Sbostic #include <sys/vnode.h>
1651483Sbostic #include <sys/mount.h>
1751483Sbostic #include <sys/buf.h>
1854734Smckusick #include <sys/mbuf.h>
1951483Sbostic #include <sys/file.h>
2051483Sbostic #include <sys/disklabel.h>
2151483Sbostic #include <sys/ioctl.h>
2251483Sbostic #include <sys/errno.h>
2351483Sbostic #include <sys/malloc.h>
2454734Smckusick #include <sys/socket.h>
2512795Ssam 
2655047Smckusick #include <miscfs/specfs/specdev.h>
2755047Smckusick 
2851501Sbostic #include <ufs/ufs/quota.h>
2951501Sbostic #include <ufs/ufs/inode.h>
3051501Sbostic #include <ufs/ufs/ufsmount.h>
3151501Sbostic #include <ufs/ufs/ufs_extern.h>
3247571Skarels 
3351501Sbostic #include <ufs/lfs/lfs.h>
3451501Sbostic #include <ufs/lfs/lfs_extern.h>
3551155Sbostic 
3651991Sbostic int lfs_mountfs __P((struct vnode *, struct mount *, struct proc *));
3751215Sbostic 
3851155Sbostic struct vfsops lfs_vfsops = {
3951155Sbostic 	lfs_mount,
4039043Smckusick 	ufs_start,
4151155Sbostic 	lfs_unmount,
4266805Sbostic 	ufs_root,
4341314Smckusick 	ufs_quotactl,
4451155Sbostic 	lfs_statfs,
4551155Sbostic 	lfs_sync,
4654693Sbostic 	lfs_vget,
4751559Smckusick 	lfs_fhtovp,
4851559Smckusick 	lfs_vptofh,
4951483Sbostic 	lfs_init,
5037737Smckusick };
5137737Smckusick 
5251483Sbostic int
5351155Sbostic lfs_mountroot()
5412795Ssam {
5551483Sbostic 	panic("lfs_mountroot");		/* XXX -- implement */
5637737Smckusick }
5737737Smckusick 
5837737Smckusick /*
5937737Smckusick  * VFS Operations.
6037737Smckusick  *
6137737Smckusick  * mount system call
6237737Smckusick  */
6351155Sbostic lfs_mount(mp, path, data, ndp, p)
6440346Smckusick 	register struct mount *mp;
6537737Smckusick 	char *path;
6637737Smckusick 	caddr_t data;
6737737Smckusick 	struct nameidata *ndp;
6848036Smckusick 	struct proc *p;
6937737Smckusick {
7037737Smckusick 	struct vnode *devvp;
7137737Smckusick 	struct ufs_args args;
7237737Smckusick 	struct ufsmount *ump;
7351501Sbostic 	register struct lfs *fs;				/* LFS */
7437737Smckusick 	u_int size;
7537737Smckusick 	int error;
7667533Smckusick 	mode_t accessmode;
7737737Smckusick 
7837737Smckusick 	if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args)))
7937737Smckusick 		return (error);
8051483Sbostic 
8151483Sbostic 	/* Until LFS can do NFS right.		XXX */
8265674Shibler 	if (args.export.ex_flags & MNT_EXPORTED)
8351483Sbostic 		return (EINVAL);
8465674Shibler 
8540371Smckusick 	/*
8650264Skarels 	 * If updating, check whether changing from read-only to
8750264Skarels 	 * read/write; if there is no device name, that's all we do.
8850264Skarels 	 */
8950264Skarels 	if (mp->mnt_flag & MNT_UPDATE) {
9039336Smckusick 		ump = VFSTOUFS(mp);
9167533Smckusick 		if (fs->lfs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
9267533Smckusick 			/*
9367533Smckusick 			 * If upgrade to read-write by non-root, then verify
9467533Smckusick 			 * that user has necessary permissions on the device.
9567533Smckusick 			 */
9667533Smckusick 			if (p->p_ucred->cr_uid != 0) {
9767533Smckusick 				VOP_LOCK(ump->um_devvp);
9867533Smckusick 				if (error = VOP_ACCESS(ump->um_devvp,
9967533Smckusick 				    VREAD | VWRITE, p->p_ucred, p)) {
10067533Smckusick 					VOP_UNLOCK(ump->um_devvp);
10167533Smckusick 					return (error);
10267533Smckusick 				}
10367533Smckusick 				VOP_UNLOCK(ump->um_devvp);
10467533Smckusick 			}
10551155Sbostic 			fs->lfs_ronly = 0;
10667533Smckusick 		}
10752175Smckusick 		if (args.fspec == 0) {
10852175Smckusick 			/*
10952175Smckusick 			 * Process export requests.
11052175Smckusick 			 */
11165674Shibler 			return (vfs_export(mp, &ump->um_export, &args.export));
11252175Smckusick 		}
11350264Skarels 	}
11450264Skarels 	/*
11550264Skarels 	 * Not an update, or updating the name: look up the name
11650264Skarels 	 * and verify that it refers to a sensible block device.
11750264Skarels 	 */
11852333Smckusick 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
11952333Smckusick 	if (error = namei(ndp))
12050264Skarels 		return (error);
12150264Skarels 	devvp = ndp->ni_vp;
12250264Skarels 	if (devvp->v_type != VBLK) {
12350264Skarels 		vrele(devvp);
12450264Skarels 		return (ENOTBLK);
12550264Skarels 	}
12650264Skarels 	if (major(devvp->v_rdev) >= nblkdev) {
12750264Skarels 		vrele(devvp);
12850264Skarels 		return (ENXIO);
12950264Skarels 	}
13067533Smckusick 	/*
13167533Smckusick 	 * If mount by non-root, then verify that user has necessary
13267533Smckusick 	 * permissions on the device.
13367533Smckusick 	 */
13467533Smckusick 	if (p->p_ucred->cr_uid != 0) {
13567533Smckusick 		accessmode = VREAD;
13667533Smckusick 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
13767533Smckusick 			accessmode |= VWRITE;
13867533Smckusick 		VOP_LOCK(devvp);
13967533Smckusick 		if (error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p)) {
14067533Smckusick 			vput(devvp);
14167533Smckusick 			return (error);
14267533Smckusick 		}
14367533Smckusick 		VOP_UNLOCK(devvp);
14467533Smckusick 	}
14550264Skarels 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
14651155Sbostic 		error = lfs_mountfs(devvp, mp, p);		/* LFS */
14750264Skarels 	else {
14839336Smckusick 		if (devvp != ump->um_devvp)
14939336Smckusick 			error = EINVAL;	/* needs translation */
15042858Smckusick 		else
15142858Smckusick 			vrele(devvp);
15239336Smckusick 	}
15337737Smckusick 	if (error) {
15437737Smckusick 		vrele(devvp);
15537737Smckusick 		return (error);
15632721Smckusick 	}
15737737Smckusick 	ump = VFSTOUFS(mp);
15851155Sbostic 	fs = ump->um_lfs;					/* LFS */
15951155Sbostic #ifdef NOTLFS							/* LFS */
16037737Smckusick 	(void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
16137737Smckusick 	bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
16241397Smckusick 	bcopy((caddr_t)fs->fs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
16341397Smckusick 	    MNAMELEN);
16451991Sbostic 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
16541397Smckusick 	    &size);
16641397Smckusick 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
16748036Smckusick 	(void) ufs_statfs(mp, &mp->mnt_stat, p);
16851155Sbostic #else
16951155Sbostic 	(void)copyinstr(path, fs->lfs_fsmnt, sizeof(fs->lfs_fsmnt) - 1, &size);
17051155Sbostic 	bzero(fs->lfs_fsmnt + size, sizeof(fs->lfs_fsmnt) - size);
17151155Sbostic 	bcopy((caddr_t)fs->lfs_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
17251155Sbostic 	    MNAMELEN);
17351991Sbostic 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
17451155Sbostic 	    &size);
17551155Sbostic 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
17651155Sbostic 	(void) lfs_statfs(mp, &mp->mnt_stat, p);
17751155Sbostic #endif
17837737Smckusick 	return (0);
17912795Ssam }
18012795Ssam 
18137737Smckusick /*
18237737Smckusick  * Common code for mount and mountroot
18351155Sbostic  * LFS specific
18437737Smckusick  */
18551991Sbostic int
18651155Sbostic lfs_mountfs(devvp, mp, p)
18740376Smckusick 	register struct vnode *devvp;
18837737Smckusick 	struct mount *mp;
18948036Smckusick 	struct proc *p;
19012795Ssam {
19151155Sbostic 	extern struct vnode *rootvp;
19251501Sbostic 	register struct lfs *fs;
19351155Sbostic 	register struct ufsmount *ump;
19451155Sbostic 	struct vnode *vp;
19551155Sbostic 	struct buf *bp;
19630749Skarels 	struct partinfo dpart;
19751155Sbostic 	dev_t dev;
19851155Sbostic 	int error, i, ronly, size;
199*67949Smckusick 	struct ucred *cred;
20012795Ssam 
201*67949Smckusick 	cred = p ? p->p_ucred : NOCRED;
20240376Smckusick 	/*
20340376Smckusick 	 * Disallow multiple mounts of the same device.
20445652Smckusick 	 * Disallow mounting of a device that is currently in use
20545652Smckusick 	 * (except for root, which might share swap device for miniroot).
20640376Smckusick 	 * Flush out any old buffers remaining from a previous use.
20740376Smckusick 	 */
20865674Shibler 	if (error = vfs_mountedon(devvp))
20940376Smckusick 		return (error);
21045652Smckusick 	if (vcount(devvp) > 1 && devvp != rootvp)
21140376Smckusick 		return (EBUSY);
212*67949Smckusick 	if (error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0))
21354693Sbostic 		return (error);
21451155Sbostic 
21551155Sbostic 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
21659473Smckusick 	if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p))
21737737Smckusick 		return (error);
21851155Sbostic 
219*67949Smckusick 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
22037737Smckusick 		size = DEV_BSIZE;
22148036Smckusick 	else {
22230749Skarels 		size = dpart.disklab->d_secsize;
22351155Sbostic #ifdef NEVER_USED
22451155Sbostic 		dpart.part->p_fstype = FS_LFS;
22551155Sbostic 		dpart.part->p_fsize = fs->lfs_fsize;	/* frag size */
22651155Sbostic 		dpart.part->p_frag = fs->lfs_frag;	/* frags per block */
22751155Sbostic 		dpart.part->p_cpg = fs->lfs_segshift;	/* segment shift */
22851155Sbostic #endif
22937737Smckusick 	}
23051155Sbostic 
23151155Sbostic 	/* Don't free random space on error. */
23251155Sbostic 	bp = NULL;
23351155Sbostic 	ump = NULL;
23451155Sbostic 
23551155Sbostic 	/* Read in the superblock. */
236*67949Smckusick 	if (error = bread(devvp, LFS_LABELPAD / size, LFS_SBPAD, cred, &bp))
23712795Ssam 		goto out;
23864523Sbostic 	fs = (struct lfs *)bp->b_data;
23951155Sbostic 
24051155Sbostic 	/* Check the basics. */
24151155Sbostic 	if (fs->lfs_magic != LFS_MAGIC || fs->lfs_bsize > MAXBSIZE ||
24251501Sbostic 	    fs->lfs_bsize < sizeof(struct lfs)) {
24341314Smckusick 		error = EINVAL;		/* XXX needs translation */
24416639Skarels 		goto out;
24516639Skarels 	}
24651155Sbostic 
24751155Sbostic 	/* Allocate the mount structure, copy the superblock into it. */
24841314Smckusick 	ump = (struct ufsmount *)malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
24955548Sbostic 	fs = ump->um_lfs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK);
25064523Sbostic 	bcopy(bp->b_data, fs, sizeof(struct lfs));
25151501Sbostic 	if (sizeof(struct lfs) < LFS_SBPAD)			/* XXX why? */
25239675Smckusick 		bp->b_flags |= B_INVAL;
25334421Skarels 	brelse(bp);
25434421Skarels 	bp = NULL;
25551155Sbostic 
25651183Sbostic 	/* Set up the I/O information */
25751183Sbostic 	fs->lfs_iocount = 0;
25851183Sbostic 
25955548Sbostic 	/* Set up the ifile and lock aflags */
26054264Sbostic 	fs->lfs_doifile = 0;
26154264Sbostic 	fs->lfs_writer = 0;
26254264Sbostic 	fs->lfs_dirops = 0;
26355548Sbostic 	fs->lfs_seglock = 0;
26454264Sbostic 
26551155Sbostic 	/* Set the file system readonly/modify bits. */
26651155Sbostic 	fs->lfs_ronly = ronly;
26712795Ssam 	if (ronly == 0)
26851155Sbostic 		fs->lfs_fmod = 1;
26951155Sbostic 
27051155Sbostic 	/* Initialize the mount structure. */
27151155Sbostic 	dev = devvp->v_rdev;
27241397Smckusick 	mp->mnt_data = (qaddr_t)ump;
27341397Smckusick 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
27451991Sbostic 	mp->mnt_stat.f_fsid.val[1] = MOUNT_LFS;
27567395Smkm 	mp->mnt_maxsymlinklen = fs->lfs_maxsymlinklen;
27641397Smckusick 	mp->mnt_flag |= MNT_LOCAL;
27737737Smckusick 	ump->um_mountp = mp;
27837737Smckusick 	ump->um_dev = dev;
27937737Smckusick 	ump->um_devvp = devvp;
28056475Smargo 	ump->um_bptrtodb = 0;
28156475Smargo 	ump->um_seqinc = 1 << fs->lfs_fsbtodb;
28256475Smargo 	ump->um_nindir = fs->lfs_nindir;
28341314Smckusick 	for (i = 0; i < MAXQUOTAS; i++)
28441314Smckusick 		ump->um_quotas[i] = NULLVP;
28552221Sbostic 	devvp->v_specflags |= SI_MOUNTEDON;
28651155Sbostic 
28752221Sbostic 	/*
28852221Sbostic 	 * We use the ifile vnode for almost every operation.  Instead of
28952221Sbostic 	 * retrieving it from the hash table each time we retrieve it here,
29052221Sbostic 	 * artificially increment the reference count and keep a pointer
29152221Sbostic 	 * to it in the incore copy of the superblock.
29252221Sbostic 	 */
29354693Sbostic 	if (error = VFS_VGET(mp, LFS_IFILE_INUM, &vp))
29451155Sbostic 		goto out;
29551155Sbostic 	fs->lfs_ivnode = vp;
29652221Sbostic 	VREF(vp);
29752221Sbostic 	vput(vp);
29851155Sbostic 
29937737Smckusick 	return (0);
30012795Ssam out:
30140872Smckusick 	if (bp)
30240872Smckusick 		brelse(bp);
303*67949Smckusick 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
30441314Smckusick 	if (ump) {
30551989Smckusick 		free(ump->um_lfs, M_UFSMNT);
30651501Sbostic 		free(ump, M_UFSMNT);
30741397Smckusick 		mp->mnt_data = (qaddr_t)0;
30832721Smckusick 	}
30937737Smckusick 	return (error);
31012795Ssam }
31112795Ssam 
31239043Smckusick /*
31337737Smckusick  * unmount system call
31437737Smckusick  */
31551155Sbostic lfs_unmount(mp, mntflags, p)
31637737Smckusick 	struct mount *mp;
31741314Smckusick 	int mntflags;
31848036Smckusick 	struct proc *p;
31912795Ssam {
32051501Sbostic 	extern int doforce;
32137737Smckusick 	register struct ufsmount *ump;
32255548Sbostic 	register struct lfs *fs;
32354693Sbostic 	int i, error, flags, ronly;
32412795Ssam 
32554693Sbostic 	flags = 0;
32648065Smckusick 	if (mntflags & MNT_FORCE) {
32765240Smckusick 		if (!doforce || (mp->mnt_flag & MNT_ROOTFS))
32848065Smckusick 			return (EINVAL);
32941314Smckusick 		flags |= FORCECLOSE;
33048065Smckusick 	}
33154264Sbostic 
33254264Sbostic 	ump = VFSTOUFS(mp);
33354264Sbostic 	fs = ump->um_lfs;
33412795Ssam #ifdef QUOTA
33541397Smckusick 	if (mp->mnt_flag & MNT_QUOTA) {
33654264Sbostic 		if (error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags))
33739898Smckusick 			return (error);
33841314Smckusick 		for (i = 0; i < MAXQUOTAS; i++) {
33941314Smckusick 			if (ump->um_quotas[i] == NULLVP)
34041314Smckusick 				continue;
34150114Smckusick 			quotaoff(p, mp, i);
34241314Smckusick 		}
34339898Smckusick 		/*
34441314Smckusick 		 * Here we fall through to vflush again to ensure
34541314Smckusick 		 * that we have gotten rid of all the system vnodes.
34639898Smckusick 		 */
34741314Smckusick 	}
34812795Ssam #endif
34954693Sbostic 	if (error = vflush(mp, fs->lfs_ivnode, flags))
35039898Smckusick 		return (error);
35155548Sbostic 	fs->lfs_clean = 1;
35254693Sbostic 	if (error = VFS_SYNC(mp, 1, p->p_ucred, p))
35354693Sbostic 		return (error);
35465240Smckusick 	if (fs->lfs_ivnode->v_dirtyblkhd.lh_first)
35554693Sbostic 		panic("lfs_unmount: still dirty blocks on ifile vnode\n");
35655806Sbostic 	vrele(fs->lfs_ivnode);
35754693Sbostic 	vgone(fs->lfs_ivnode);
35854693Sbostic 
35951155Sbostic 	ronly = !fs->lfs_ronly;
36040653Smckusick 	ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
36154693Sbostic 	error = VOP_CLOSE(ump->um_devvp,
36254693Sbostic 	    ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
36337737Smckusick 	vrele(ump->um_devvp);
36451989Smckusick 	free(fs, M_UFSMNT);
36551501Sbostic 	free(ump, M_UFSMNT);
36641397Smckusick 	mp->mnt_data = (qaddr_t)0;
36741397Smckusick 	mp->mnt_flag &= ~MNT_LOCAL;
36830749Skarels 	return (error);
36912795Ssam }
37012795Ssam 
37137737Smckusick /*
37237737Smckusick  * Get file system statistics.
37337737Smckusick  */
37451155Sbostic lfs_statfs(mp, sbp, p)
37537737Smckusick 	struct mount *mp;
37637737Smckusick 	register struct statfs *sbp;
37748036Smckusick 	struct proc *p;
37837737Smckusick {
37951501Sbostic 	register struct lfs *fs;
38037737Smckusick 	register struct ufsmount *ump;
38137737Smckusick 
38237737Smckusick 	ump = VFSTOUFS(mp);
38351155Sbostic 	fs = ump->um_lfs;
38451155Sbostic 	if (fs->lfs_magic != LFS_MAGIC)
38551155Sbostic 		panic("lfs_statfs: magic");
38651155Sbostic 	sbp->f_type = MOUNT_LFS;
38751155Sbostic 	sbp->f_bsize = fs->lfs_bsize;
38851943Smckusick 	sbp->f_iosize = fs->lfs_bsize;
38956156Smargo 	sbp->f_blocks = dbtofsb(fs,fs->lfs_dsize);
39055588Sbostic 	sbp->f_bfree = dbtofsb(fs, fs->lfs_bfree);
39151155Sbostic 	sbp->f_bavail = (fs->lfs_dsize * (100 - fs->lfs_minfree) / 100) -
39256368Smargo 		(fs->lfs_dsize - fs->lfs_bfree);
39356156Smargo 	sbp->f_bavail = dbtofsb(fs, sbp->f_bavail);
39451155Sbostic 	sbp->f_files = fs->lfs_nfiles;
39555588Sbostic 	sbp->f_ffree = sbp->f_bfree * INOPB(fs);
39641397Smckusick 	if (sbp != &mp->mnt_stat) {
39741397Smckusick 		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
39840346Smckusick 			(caddr_t)&sbp->f_mntonname[0], MNAMELEN);
39941397Smckusick 		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
40040346Smckusick 			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
40140346Smckusick 	}
40237737Smckusick 	return (0);
40337737Smckusick }
40437737Smckusick 
40537737Smckusick /*
40637737Smckusick  * Go through the disk queues to initiate sandbagged IO;
40737737Smckusick  * go through the inodes to write those that have been modified;
40837737Smckusick  * initiate the writing of the super block if it has been modified.
40941314Smckusick  *
41041314Smckusick  * Note: we are always called with the filesystem marked `MPBUSY'.
41137737Smckusick  */
41254693Sbostic lfs_sync(mp, waitfor, cred, p)
41337737Smckusick 	struct mount *mp;
41437737Smckusick 	int waitfor;
41554693Sbostic 	struct ucred *cred;
41654693Sbostic 	struct proc *p;
41737737Smckusick {
41851215Sbostic 	int error;
41937737Smckusick 
42051310Sbostic 	/* All syncs must be checkpoints until roll-forward is implemented. */
42157068Smargo 	error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
42241314Smckusick #ifdef QUOTA
42341314Smckusick 	qsync(mp);
42441314Smckusick #endif
42551215Sbostic 	return (error);
42637737Smckusick }
42751559Smckusick 
42851559Smckusick /*
42954693Sbostic  * Look up an LFS dinode number to find its incore vnode.  If not already
43054693Sbostic  * in core, read it in from the specified device.  Return the inode locked.
43154693Sbostic  * Detection and handling of mount points must be done by the calling routine.
43254693Sbostic  */
43354693Sbostic int
43454693Sbostic lfs_vget(mp, ino, vpp)
43554693Sbostic 	struct mount *mp;
43654693Sbostic 	ino_t ino;
43754693Sbostic 	struct vnode **vpp;
43854693Sbostic {
43954693Sbostic 	register struct lfs *fs;
44054693Sbostic 	register struct inode *ip;
44154693Sbostic 	struct buf *bp;
44254693Sbostic 	struct ifile *ifp;
44354693Sbostic 	struct vnode *vp;
44454693Sbostic 	struct ufsmount *ump;
44554693Sbostic 	daddr_t daddr;
44654693Sbostic 	dev_t dev;
44754693Sbostic 	int error;
44854693Sbostic 
44954693Sbostic 	ump = VFSTOUFS(mp);
45054693Sbostic 	dev = ump->um_dev;
45154693Sbostic 	if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
45254693Sbostic 		return (0);
45354693Sbostic 
45454693Sbostic 	/* Translate the inode number to a disk address. */
45554693Sbostic 	fs = ump->um_lfs;
45654693Sbostic 	if (ino == LFS_IFILE_INUM)
45754693Sbostic 		daddr = fs->lfs_idaddr;
45854693Sbostic 	else {
45954693Sbostic 		LFS_IENTRY(ifp, fs, ino, bp);
46054693Sbostic 		daddr = ifp->if_daddr;
46154693Sbostic 		brelse(bp);
46254693Sbostic 		if (daddr == LFS_UNUSED_DADDR)
46354693Sbostic 			return (ENOENT);
46454693Sbostic 	}
46554693Sbostic 
46654693Sbostic 	/* Allocate new vnode/inode. */
46754693Sbostic 	if (error = lfs_vcreate(mp, ino, &vp)) {
46854693Sbostic 		*vpp = NULL;
46954693Sbostic 		return (error);
47054693Sbostic 	}
47154693Sbostic 
47254693Sbostic 	/*
47354693Sbostic 	 * Put it onto its hash chain and lock it so that other requests for
47454693Sbostic 	 * this inode will block if they arrive while we are sleeping waiting
47554693Sbostic 	 * for old data structures to be purged or for the contents of the
47654693Sbostic 	 * disk portion of this inode to be read.
47754693Sbostic 	 */
47854693Sbostic 	ip = VTOI(vp);
47954693Sbostic 	ufs_ihashins(ip);
48054693Sbostic 
48154693Sbostic 	/*
48254693Sbostic 	 * XXX
48354693Sbostic 	 * This may not need to be here, logically it should go down with
48454693Sbostic 	 * the i_devvp initialization.
48554693Sbostic 	 * Ask Kirk.
48654693Sbostic 	 */
48754693Sbostic 	ip->i_lfs = ump->um_lfs;
48854693Sbostic 
48954693Sbostic 	/* Read in the disk contents for the inode, copy into the inode. */
49054693Sbostic 	if (error =
49154693Sbostic 	    bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp)) {
49254693Sbostic 		/*
49359820Smckusick 		 * The inode does not contain anything useful, so it would
49459820Smckusick 		 * be misleading to leave it on its hash chain. With mode
49559820Smckusick 		 * still zero, it will be unlinked and returned to the free
49659820Smckusick 		 * list by vput().
49754693Sbostic 		 */
49856799Smckusick 		vput(vp);
49954693Sbostic 		brelse(bp);
50054693Sbostic 		*vpp = NULL;
50154693Sbostic 		return (error);
50254693Sbostic 	}
50364523Sbostic 	ip->i_din = *lfs_ifind(fs, ino, (struct dinode *)bp->b_data);
50454693Sbostic 	brelse(bp);
50554693Sbostic 
50654693Sbostic 	/*
50754693Sbostic 	 * Initialize the vnode from the inode, check for aliases.  In all
50854693Sbostic 	 * cases re-init ip, the underlying vnode/inode may have changed.
50954693Sbostic 	 */
51054693Sbostic 	if (error = ufs_vinit(mp, lfs_specop_p, LFS_FIFOOPS, &vp)) {
51156799Smckusick 		vput(vp);
51254693Sbostic 		*vpp = NULL;
51354693Sbostic 		return (error);
51454693Sbostic 	}
51554693Sbostic 	/*
51654693Sbostic 	 * Finish inode initialization now that aliasing has been resolved.
51754693Sbostic 	 */
51854693Sbostic 	ip->i_devvp = ump->um_devvp;
51954693Sbostic 	VREF(ip->i_devvp);
52054693Sbostic 	*vpp = vp;
52154693Sbostic 	return (0);
52254693Sbostic }
52354693Sbostic 
52454693Sbostic /*
52551559Smckusick  * File handle to vnode
52651559Smckusick  *
52751559Smckusick  * Have to be really careful about stale file handles:
52851559Smckusick  * - check that the inode number is valid
52951559Smckusick  * - call lfs_vget() to get the locked inode
53051559Smckusick  * - check for an unallocated inode (i_mode == 0)
53155891Smckusick  * - check that the given client host has export rights and return
53255891Smckusick  *   those rights via. exflagsp and credanonp
53351559Smckusick  *
53451559Smckusick  * XXX
53551559Smckusick  * use ifile to see if inode is allocated instead of reading off disk
53651559Smckusick  * what is the relationship between my generational number and the NFS
53751559Smckusick  * generational number.
53851559Smckusick  */
53951559Smckusick int
54054734Smckusick lfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
54151559Smckusick 	register struct mount *mp;
54251559Smckusick 	struct fid *fhp;
54354734Smckusick 	struct mbuf *nam;
54451559Smckusick 	struct vnode **vpp;
54554734Smckusick 	int *exflagsp;
54654734Smckusick 	struct ucred **credanonp;
54751559Smckusick {
54851559Smckusick 	register struct ufid *ufhp;
54951559Smckusick 
55051559Smckusick 	ufhp = (struct ufid *)fhp;
55151559Smckusick 	if (ufhp->ufid_ino < ROOTINO)
55254734Smckusick 		return (ESTALE);
55356246Smckusick 	return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
55451559Smckusick }
55551559Smckusick 
55651559Smckusick /*
55751559Smckusick  * Vnode pointer to File handle
55851559Smckusick  */
55951559Smckusick /* ARGSUSED */
56051559Smckusick lfs_vptofh(vp, fhp)
56151559Smckusick 	struct vnode *vp;
56251559Smckusick 	struct fid *fhp;
56351559Smckusick {
56451559Smckusick 	register struct inode *ip;
56551559Smckusick 	register struct ufid *ufhp;
56651559Smckusick 
56751559Smckusick 	ip = VTOI(vp);
56851559Smckusick 	ufhp = (struct ufid *)fhp;
56951559Smckusick 	ufhp->ufid_len = sizeof(struct ufid);
57051559Smckusick 	ufhp->ufid_ino = ip->i_number;
57151559Smckusick 	ufhp->ufid_gen = ip->i_gen;
57251559Smckusick 	return (0);
57351559Smckusick }
574