Home | History | Annotate | Line # | Download | only in filecorefs
filecore_vfsops.c revision 1.31
      1 /*	$NetBSD: filecore_vfsops.c,v 1.31 2007/01/19 14:49:09 hannken Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1994 The Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	filecore_vfsops.c	1.1	1998/6/26
     32  */
     33 
     34 /*-
     35  * Copyright (c) 1998 Andrew McMurry
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  * 3. All advertising materials mentioning features or use of this software
     46  *    must display the following acknowledgement:
     47  *	This product includes software developed by the University of
     48  *	California, Berkeley and its contributors.
     49  * 4. Neither the name of the University nor the names of its contributors
     50  *    may be used to endorse or promote products derived from this software
     51  *    without specific prior written permission.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  * SUCH DAMAGE.
     64  *
     65  *	filecore_vfsops.c	1.1	1998/6/26
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: filecore_vfsops.c,v 1.31 2007/01/19 14:49:09 hannken Exp $");
     70 
     71 #if defined(_KERNEL_OPT)
     72 #include "opt_compat_netbsd.h"
     73 #endif
     74 
     75 #include <sys/param.h>
     76 #include <sys/systm.h>
     77 #include <sys/namei.h>
     78 #include <sys/proc.h>
     79 #include <sys/vnode.h>
     80 #include <miscfs/specfs/specdev.h>
     81 #include <sys/mount.h>
     82 #include <sys/buf.h>
     83 #include <sys/file.h>
     84 #include <sys/device.h>
     85 #include <sys/errno.h>
     86 #include <sys/malloc.h>
     87 #include <sys/pool.h>
     88 #include <sys/conf.h>
     89 #include <sys/sysctl.h>
     90 #include <sys/kauth.h>
     91 
     92 #include <fs/filecorefs/filecore.h>
     93 #include <fs/filecorefs/filecore_extern.h>
     94 #include <fs/filecorefs/filecore_node.h>
     95 #include <fs/filecorefs/filecore_mount.h>
     96 
     97 MALLOC_DEFINE(M_FILECOREMNT, "filecore mount", "Filecore FS mount structures");
     98 
     99 extern const struct vnodeopv_desc filecore_vnodeop_opv_desc;
    100 
    101 const struct vnodeopv_desc * const filecore_vnodeopv_descs[] = {
    102 	&filecore_vnodeop_opv_desc,
    103 	NULL,
    104 };
    105 
    106 struct vfsops filecore_vfsops = {
    107 	MOUNT_FILECORE,
    108 	filecore_mount,
    109 	filecore_start,
    110 	filecore_unmount,
    111 	filecore_root,
    112 	filecore_quotactl,
    113 	filecore_statvfs,
    114 	filecore_sync,
    115 	filecore_vget,
    116 	filecore_fhtovp,
    117 	filecore_vptofh,
    118 	filecore_init,
    119 	filecore_reinit,
    120 	filecore_done,
    121 	NULL,				/* filecore_mountroot */
    122 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    123 	vfs_stdextattrctl,
    124 	vfs_stdsuspendctl,
    125 	filecore_vnodeopv_descs,
    126 	0,
    127 	{ NULL, NULL }
    128 };
    129 VFS_ATTACH(filecore_vfsops);
    130 
    131 static const struct genfs_ops filecore_genfsops = {
    132 	.gop_size = genfs_size,
    133 };
    134 
    135 /*
    136  * Called by vfs_mountroot when iso is going to be mounted as root.
    137  *
    138  * Name is updated by mount(8) after booting.
    139  */
    140 
    141 static int filecore_mountfs __P((struct vnode *devvp, struct mount *mp,
    142 		struct lwp *l, struct filecore_args *argp));
    143 
    144 #if 0
    145 int
    146 filecore_mountroot()
    147 {
    148 	struct mount *mp;
    149 	extern struct vnode *rootvp;
    150 	struct proc *p = curproc;	/* XXX */
    151 	int error;
    152 	struct filecore_args args;
    153 
    154 	if (device_class(root_device) != DV_DISK)
    155 		return (ENODEV);
    156 
    157 	/*
    158 	 * Get vnodes for swapdev and rootdev.
    159 	 */
    160 	if (bdevvp(rootdev, &rootvp))
    161 		panic("filecore_mountroot: can't setup rootvp");
    162 
    163 	if ((error = vfs_rootmountalloc(MOUNT_FILECORE, "root_device", &mp)) != 0)
    164 		return (error);
    165 
    166 	args.flags = FILECOREMNT_ROOT;
    167 	if ((error = filecore_mountfs(rootvp, mp, p, &args)) != 0) {
    168 		mp->mnt_op->vfs_refcount--;
    169 		vfs_unbusy(mp);
    170 		free(mp, M_MOUNT);
    171 		return (error);
    172 	}
    173 	simple_lock(&mountlist_slock);
    174 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    175 	simple_unlock(&mountlist_slock);
    176 	(void)filecore_statvfs(mp, &mp->mnt_stat, p);
    177 	vfs_unbusy(mp);
    178 	return (0);
    179 }
    180 #endif
    181 
    182 /*
    183  * VFS Operations.
    184  *
    185  * mount system call
    186  */
    187 int
    188 filecore_mount(mp, path, data, ndp, l)
    189 	struct mount *mp;
    190 	const char *path;
    191 	void *data;
    192 	struct nameidata *ndp;
    193 	struct lwp *l;
    194 {
    195 	struct vnode *devvp;
    196 	struct filecore_args args;
    197 	int error;
    198 	struct filecore_mnt *fcmp = NULL;
    199 
    200 	if (mp->mnt_flag & MNT_GETARGS) {
    201 		fcmp = VFSTOFILECORE(mp);
    202 		if (fcmp == NULL)
    203 			return EIO;
    204 		args.flags = fcmp->fc_mntflags;
    205 		args.uid = fcmp->fc_uid;
    206 		args.gid = fcmp->fc_gid;
    207 		args.fspec = NULL;
    208 		return copyout(&args, data, sizeof(args));
    209 	}
    210 	error = copyin(data, &args, sizeof (struct filecore_args));
    211 	if (error)
    212 		return (error);
    213 
    214 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
    215 		return (EROFS);
    216 
    217 	if ((mp->mnt_flag & MNT_UPDATE) && args.fspec == NULL)
    218 		return EINVAL;
    219 
    220 	/*
    221 	 * Not an update, or updating the name: look up the name
    222 	 * and verify that it refers to a sensible block device.
    223 	 */
    224 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, l);
    225 	if ((error = namei(ndp)) != 0)
    226 		return (error);
    227 	devvp = ndp->ni_vp;
    228 
    229 	if (devvp->v_type != VBLK) {
    230 		vrele(devvp);
    231 		return ENOTBLK;
    232 	}
    233 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
    234 		vrele(devvp);
    235 		return ENXIO;
    236 	}
    237 	/*
    238 	 * If mount by non-root, then verify that user has necessary
    239 	 * permissions on the device.
    240 	 */
    241 	if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
    242 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    243 		error = VOP_ACCESS(devvp, VREAD, l->l_cred, l);
    244 		VOP_UNLOCK(devvp, 0);
    245 		if (error) {
    246 			vrele(devvp);
    247 			return (error);
    248 		}
    249 	}
    250 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
    251 		error = filecore_mountfs(devvp, mp, l, &args);
    252 	else {
    253 		if (devvp != fcmp->fc_devvp)
    254 			error = EINVAL;	/* needs translation */
    255 		else
    256 			vrele(devvp);
    257 	}
    258 	if (error) {
    259 		vrele(devvp);
    260 		return error;
    261 	}
    262 	fcmp = VFSTOFILECORE(mp);
    263 	return set_statvfs_info(path, UIO_USERSPACE, args.fspec, UIO_USERSPACE,
    264 	    mp, l);
    265 }
    266 
    267 /*
    268  * Common code for mount and mountroot
    269  */
    270 static int
    271 filecore_mountfs(devvp, mp, l, argp)
    272 	struct vnode *devvp;
    273 	struct mount *mp;
    274 	struct lwp *l;
    275 	struct filecore_args *argp;
    276 {
    277 	struct filecore_mnt *fcmp = (struct filecore_mnt *)0;
    278 	struct buf *bp = NULL;
    279 	dev_t dev = devvp->v_rdev;
    280 	int error = EINVAL;
    281 	int ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    282 	extern struct vnode *rootvp;
    283 	struct filecore_disc_record *fcdr;
    284 	unsigned map;
    285 	unsigned log2secsize;
    286 
    287 	if (!ronly)
    288 		return EROFS;
    289 
    290 	/*
    291 	 * Disallow multiple mounts of the same device.
    292 	 * Disallow mounting of a device that is currently in use
    293 	 * (except for root, which might share swap device for miniroot).
    294 	 * Flush out any old buffers remaining from a previous use.
    295 	 */
    296 	if ((error = vfs_mountedon(devvp)) != 0)
    297 		return error;
    298 	if (vcount(devvp) > 1 && devvp != rootvp)
    299 		return EBUSY;
    300 	if ((error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0))
    301 	    != 0)
    302 		return (error);
    303 
    304 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, l);
    305 	if (error)
    306 		return error;
    307 
    308 	/* Read the filecore boot block to check FS validity and to find the map */
    309 	error = bread(devvp, FILECORE_BOOTBLOCK_BLKN,
    310 			   FILECORE_BOOTBLOCK_SIZE, NOCRED, &bp);
    311 #ifdef FILECORE_DEBUG_BR
    312 		printf("bread(%p, %x, %d, CRED, %p)=%d\n", devvp,
    313 		       FILECORE_BOOTBLOCK_BLKN, FILECORE_BOOTBLOCK_SIZE,
    314 		       bp, error);
    315 #endif
    316 	if (error != 0)
    317 		goto out;
    318 
    319 	KASSERT(bp != NULL);
    320 	if (filecore_bbchecksum(bp->b_data) != 0) {
    321 		error = EINVAL;
    322 		goto out;
    323 	}
    324 	fcdr = (struct filecore_disc_record *)(bp->b_data+FILECORE_BB_DISCREC);
    325 	map = ((((8 << fcdr->log2secsize) - fcdr->zone_spare)
    326 	    * (fcdr->nzones / 2) - 8 * FILECORE_DISCREC_SIZE)
    327 	    << fcdr->log2bpmb) >> fcdr->log2secsize;
    328 	log2secsize = fcdr->log2secsize;
    329 	bp->b_flags |= B_AGE;
    330 #ifdef FILECORE_DEBUG_BR
    331 	printf("brelse(%p) vf1\n", bp);
    332 #endif
    333 	brelse(bp);
    334 	bp = NULL;
    335 
    336 	/* Read the bootblock in the map */
    337 	error = bread(devvp, map, 1 << log2secsize, NOCRED, &bp);
    338 #ifdef FILECORE_DEBUG_BR
    339 		printf("bread(%p, %x, %d, CRED, %p)=%d\n", devvp,
    340 		       map, 1 << log2secsize, bp, error);
    341 #endif
    342 	if (error != 0)
    343 		goto out;
    344        	fcdr = (struct filecore_disc_record *)(bp->b_data + 4);
    345 	fcmp = malloc(sizeof *fcmp, M_FILECOREMNT, M_WAITOK);
    346 	memset(fcmp, 0, sizeof *fcmp);
    347 	if (fcdr->log2bpmb > fcdr->log2secsize)
    348 		fcmp->log2bsize = fcdr->log2bpmb;
    349 	else	fcmp->log2bsize = fcdr->log2secsize;
    350 	fcmp->blksize = 1 << fcmp->log2bsize;
    351 	memcpy(&fcmp->drec, fcdr, sizeof(*fcdr));
    352 	fcmp->map = map;
    353 	fcmp->idspz = ((8 << fcdr->log2secsize) - fcdr->zone_spare)
    354 	    / (fcdr->idlen + 1);
    355 	fcmp->mask = (1 << fcdr->idlen) - 1;
    356 	if (fcdr->big_flag & 1) {
    357 		fcmp->nblks = ((((u_int64_t)fcdr->disc_size_2) << 32)
    358 		    + fcdr->disc_size) / fcmp->blksize;
    359 	} else {
    360 		fcmp->nblks=fcdr->disc_size / fcmp->blksize;
    361 	}
    362 
    363 	bp->b_flags |= B_AGE;
    364 #ifdef FILECORE_DEBUG_BR
    365 	printf("brelse(%p) vf2\n", bp);
    366 #endif
    367 	brelse(bp);
    368 	bp = NULL;
    369 
    370 	mp->mnt_data = fcmp;
    371 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
    372 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FILECORE);
    373 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    374 	mp->mnt_stat.f_namemax = 10;
    375 	mp->mnt_flag |= MNT_LOCAL;
    376 	mp->mnt_dev_bshift = fcdr->log2secsize;
    377 	mp->mnt_fs_bshift = fcmp->log2bsize;
    378 
    379 	fcmp->fc_mountp = mp;
    380 	fcmp->fc_dev = dev;
    381 	fcmp->fc_devvp = devvp;
    382 	fcmp->fc_mntflags = argp->flags;
    383 	if (argp->flags & FILECOREMNT_USEUID) {
    384 		fcmp->fc_uid = kauth_cred_getuid(l->l_cred);
    385 		fcmp->fc_gid = kauth_cred_getgid(l->l_cred);
    386 	} else {
    387 		fcmp->fc_uid = argp->uid;
    388 		fcmp->fc_gid = argp->gid;
    389 	}
    390 
    391 	return 0;
    392 out:
    393 	if (bp) {
    394 #ifdef FILECORE_DEBUG_BR
    395 		printf("brelse(%p) vf3\n", bp);
    396 #endif
    397 		brelse(bp);
    398 	}
    399 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    400 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, l);
    401 	VOP_UNLOCK(devvp, 0);
    402 	if (fcmp) {
    403 		free(fcmp, M_FILECOREMNT);
    404 		mp->mnt_data = NULL;
    405 	}
    406 	return error;
    407 }
    408 
    409 /*
    410  * Make a filesystem operational.
    411  * Nothing to do at the moment.
    412  */
    413 /* ARGSUSED */
    414 int
    415 filecore_start(mp, flags, l)
    416 	struct mount *mp;
    417 	int flags;
    418 	struct lwp *l;
    419 {
    420 	return 0;
    421 }
    422 
    423 /*
    424  * unmount system call
    425  */
    426 int
    427 filecore_unmount(mp, mntflags, l)
    428 	struct mount *mp;
    429 	int mntflags;
    430 	struct lwp *l;
    431 {
    432 	struct filecore_mnt *fcmp;
    433 	int error, flags = 0;
    434 
    435 	if (mntflags & MNT_FORCE)
    436 		flags |= FORCECLOSE;
    437 #if 0
    438 	mntflushbuf(mp, 0);
    439 	if (mntinvalbuf(mp))
    440 		return EBUSY;
    441 #endif
    442 	if ((error = vflush(mp, NULLVP, flags)) != 0)
    443 		return (error);
    444 
    445 	fcmp = VFSTOFILECORE(mp);
    446 
    447 	if (fcmp->fc_devvp->v_type != VBAD)
    448 		fcmp->fc_devvp->v_specmountpoint = NULL;
    449 	vn_lock(fcmp->fc_devvp, LK_EXCLUSIVE | LK_RETRY);
    450 	error = VOP_CLOSE(fcmp->fc_devvp, FREAD, NOCRED, l);
    451 	vput(fcmp->fc_devvp);
    452 	free(fcmp, M_FILECOREMNT);
    453 	mp->mnt_data = NULL;
    454 	mp->mnt_flag &= ~MNT_LOCAL;
    455 	return (error);
    456 }
    457 
    458 /*
    459  * Return root of a filesystem
    460  */
    461 int
    462 filecore_root(mp, vpp)
    463 	struct mount *mp;
    464 	struct vnode **vpp;
    465 {
    466 	struct vnode *nvp;
    467         int error;
    468 
    469         if ((error = VFS_VGET(mp, FILECORE_ROOTINO, &nvp)) != 0)
    470                 return (error);
    471         *vpp = nvp;
    472         return (0);
    473 }
    474 
    475 /*
    476  * Do operations associated with quotas, not supported
    477  */
    478 /* ARGSUSED */
    479 int
    480 filecore_quotactl(mp, cmd, uid, arg, l)
    481 	struct mount *mp;
    482 	int cmd;
    483 	uid_t uid;
    484 	void *arg;
    485 	struct lwp *l;
    486 {
    487 
    488 	return (EOPNOTSUPP);
    489 }
    490 
    491 /*
    492  * Get file system statistics.
    493  */
    494 int
    495 filecore_statvfs(mp, sbp, l)
    496 	struct mount *mp;
    497 	struct statvfs *sbp;
    498 	struct lwp *l;
    499 {
    500 	struct filecore_mnt *fcmp = VFSTOFILECORE(mp);
    501 
    502 	sbp->f_bsize = fcmp->blksize;
    503 	sbp->f_frsize = sbp->f_bsize; /* XXX */
    504 	sbp->f_iosize = sbp->f_bsize;	/* XXX */
    505 	sbp->f_blocks = fcmp->nblks;
    506 	sbp->f_bfree = 0; /* total free blocks */
    507 	sbp->f_bavail = 0; /* blocks free for non superuser */
    508 	sbp->f_bresvd = 0; /* reserved blocks */
    509 	sbp->f_files =  0; /* total files */
    510 	sbp->f_ffree = 0; /* free file nodes for non superuser */
    511 	sbp->f_favail = 0; /* free file nodes */
    512 	sbp->f_fresvd = 0; /* reserved file nodes */
    513 	copy_statvfs_info(sbp, mp);
    514 	return 0;
    515 }
    516 
    517 /* ARGSUSED */
    518 int
    519 filecore_sync(mp, waitfor, cred, l)
    520 	struct mount *mp;
    521 	int waitfor;
    522 	kauth_cred_t cred;
    523 	struct lwp *l;
    524 {
    525 	return (0);
    526 }
    527 
    528 /*
    529  * File handle to vnode
    530  *
    531  * Have to be really careful about stale file handles:
    532  * - check that the inode number is in range
    533  * - call iget() to get the locked inode
    534  * - check for an unallocated inode (i_mode == 0)
    535  * - check that the generation number matches
    536  */
    537 
    538 struct ifid {
    539 	ushort		ifid_len;
    540 	ushort		ifid_pad;
    541 	u_int32_t	ifid_ino;
    542 };
    543 
    544 /* ARGSUSED */
    545 int
    546 filecore_fhtovp(mp, fhp, vpp)
    547 	struct mount *mp;
    548 	struct fid *fhp;
    549 	struct vnode **vpp;
    550 {
    551 	struct ifid ifh;
    552 	struct vnode *nvp;
    553 	struct filecore_node *ip;
    554 	int error;
    555 
    556 	if (fhp->fid_len != sizeof(struct ifid))
    557 		return EINVAL;
    558 
    559 	memcpy(&ifh, fhp, sizeof(ifh));
    560 	if ((error = VFS_VGET(mp, ifh.ifid_ino, &nvp)) != 0) {
    561 		*vpp = NULLVP;
    562 		return (error);
    563 	}
    564         ip = VTOI(nvp);
    565         if (filecore_staleinode(ip)) {
    566                 vput(nvp);
    567                 *vpp = NULLVP;
    568                 return (ESTALE);
    569         }
    570 	*vpp = nvp;
    571 	return (0);
    572 }
    573 
    574 /* This looks complicated. Look at other vgets as well as the iso9660 one.
    575  *
    576  * The filecore inode number is made up of 1 byte directory entry index and
    577  * 3 bytes of the internal disc address of the directory. On a read-only
    578  * filesystem this is unique. For a read-write version we may not be able to
    579  * do vget, see msdosfs.
    580  */
    581 
    582 int
    583 filecore_vget(mp, ino, vpp)
    584 	struct mount *mp;
    585 	ino_t ino;
    586 	struct vnode **vpp;
    587 {
    588 	struct filecore_mnt *fcmp;
    589 	struct filecore_node *ip;
    590 	struct buf *bp;
    591 	struct vnode *vp;
    592 	dev_t dev;
    593 	int error;
    594 
    595 	fcmp = VFSTOFILECORE(mp);
    596 	dev = fcmp->fc_dev;
    597 	if ((*vpp = filecore_ihashget(dev, ino)) != NULLVP)
    598 		return (0);
    599 
    600 	/* Allocate a new vnode/filecore_node. */
    601 	if ((error = getnewvnode(VT_FILECORE, mp, filecore_vnodeop_p, &vp))
    602 	    != 0) {
    603 		*vpp = NULLVP;
    604 		return (error);
    605 	}
    606 	ip = pool_get(&filecore_node_pool, PR_WAITOK);
    607 	memset(ip, 0, sizeof(struct filecore_node));
    608 	vp->v_data = ip;
    609 	ip->i_vnode = vp;
    610 	ip->i_dev = dev;
    611 	ip->i_number = ino;
    612 	ip->i_block = -1;
    613 	ip->i_parent = -2;
    614 
    615 	/*
    616 	 * Put it onto its hash chain and lock it so that other requests for
    617 	 * this inode will block if they arrive while we are sleeping waiting
    618 	 * for old data structures to be purged or for the contents of the
    619 	 * disk portion of this inode to be read.
    620 	 */
    621 	filecore_ihashins(ip);
    622 
    623 	if (ino == FILECORE_ROOTINO) {
    624 		/* Here we need to construct a root directory inode */
    625 		memcpy(ip->i_dirent.name, "root", 4);
    626 		ip->i_dirent.load = 0;
    627 		ip->i_dirent.exec = 0;
    628 		ip->i_dirent.len = FILECORE_DIR_SIZE;
    629 		ip->i_dirent.addr = fcmp->drec.root;
    630 		ip->i_dirent.attr = FILECORE_ATTR_DIR | FILECORE_ATTR_READ;
    631 
    632 	} else {
    633 		/* Read in Data from Directory Entry */
    634 		if ((error = filecore_bread(fcmp, ino & FILECORE_INO_MASK,
    635 		    FILECORE_DIR_SIZE, NOCRED, &bp)) != 0) {
    636 			vput(vp);
    637 #ifdef FILECORE_DEBUG_BR
    638 			printf("brelse(%p) vf4\n", bp);
    639 #endif
    640 			brelse(bp);
    641 			*vpp = NULL;
    642 			return (error);
    643 		}
    644 
    645 		memcpy(&ip->i_dirent,
    646 		    fcdirentry(bp->b_data, ino >> FILECORE_INO_INDEX),
    647 		    sizeof(struct filecore_direntry));
    648 #ifdef FILECORE_DEBUG_BR
    649 		printf("brelse(%p) vf5\n", bp);
    650 #endif
    651 		brelse(bp);
    652 	}
    653 
    654 	ip->i_mnt = fcmp;
    655 	ip->i_devvp = fcmp->fc_devvp;
    656 	ip->i_diroff = 0;
    657 	VREF(ip->i_devvp);
    658 
    659 	/*
    660 	 * Setup type
    661 	 */
    662 	vp->v_type = VREG;
    663 	if (ip->i_dirent.attr & FILECORE_ATTR_DIR)
    664 		vp->v_type = VDIR;
    665 
    666 	/*
    667 	 * Initialize the associated vnode
    668 	 */
    669 	switch (vp->v_type) {
    670 	case VFIFO:
    671 	case VCHR:
    672 	case VBLK:
    673 		/*
    674 		 * Devices not supported.
    675 		 */
    676 		vput(vp);
    677 		return (EOPNOTSUPP);
    678 	case VLNK:
    679 	case VNON:
    680 	case VSOCK:
    681 	case VDIR:
    682 	case VBAD:
    683 	case VREG:
    684 		break;
    685 	}
    686 
    687 	if (ino == FILECORE_ROOTINO)
    688 		vp->v_flag |= VROOT;
    689 
    690 	/*
    691 	 * XXX need generation number?
    692 	 */
    693 
    694 	genfs_node_init(vp, &filecore_genfsops);
    695 	vp->v_size = ip->i_size;
    696 	*vpp = vp;
    697 	return (0);
    698 }
    699 
    700 /*
    701  * Vnode pointer to File handle
    702  */
    703 /* ARGSUSED */
    704 int
    705 filecore_vptofh(vp, fhp, fh_size)
    706 	struct vnode *vp;
    707 	struct fid *fhp;
    708 	size_t *fh_size;
    709 {
    710 	struct filecore_node *ip = VTOI(vp);
    711 	struct ifid ifh;
    712 
    713 	if (*fh_size < sizeof(struct ifid)) {
    714 		*fh_size = sizeof(struct ifid);
    715 		return E2BIG;
    716 	}
    717 	memset(&ifh, 0, sizeof(ifh));
    718 	ifh.ifid_len = sizeof(struct ifid);
    719 	ifh.ifid_ino = ip->i_number;
    720 	memcpy(fhp, &ifh, sizeof(ifh));
    721        	return 0;
    722 }
    723 
    724 SYSCTL_SETUP(sysctl_vfs_filecore_setup, "sysctl vfs.filecore subtree setup")
    725 {
    726 
    727 	sysctl_createv(clog, 0, NULL, NULL,
    728 		       CTLFLAG_PERMANENT,
    729 		       CTLTYPE_NODE, "vfs", NULL,
    730 		       NULL, 0, NULL, 0,
    731 		       CTL_VFS, CTL_EOL);
    732 	sysctl_createv(clog, 0, NULL, NULL,
    733 		       CTLFLAG_PERMANENT,
    734 		       CTLTYPE_NODE, "filecore",
    735 		       SYSCTL_DESCR("Acorn FILECORE file system"),
    736 		       NULL, 0, NULL, 0,
    737 		       CTL_VFS, 19, CTL_EOL);
    738 	/*
    739 	 * XXX the "19" above could be dynamic, thereby eliminating
    740 	 * one more instance of the "number to vfs" mapping problem,
    741 	 * but "19" is the order as taken from sys/mount.h
    742 	 */
    743 }
    744