Home | History | Annotate | Line # | Download | only in filecorefs
filecore_vfsops.c revision 1.54
      1 /*	$NetBSD: filecore_vfsops.c,v 1.54 2008/05/16 09:21:59 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.54 2008/05/16 09:21:59 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/genfs/genfs.h>
     81 #include <miscfs/specfs/specdev.h>
     82 #include <sys/mount.h>
     83 #include <sys/buf.h>
     84 #include <sys/file.h>
     85 #include <sys/device.h>
     86 #include <sys/errno.h>
     87 #include <sys/malloc.h>
     88 #include <sys/pool.h>
     89 #include <sys/conf.h>
     90 #include <sys/sysctl.h>
     91 #include <sys/kauth.h>
     92 #include <sys/module.h>
     93 
     94 #include <fs/filecorefs/filecore.h>
     95 #include <fs/filecorefs/filecore_extern.h>
     96 #include <fs/filecorefs/filecore_node.h>
     97 #include <fs/filecorefs/filecore_mount.h>
     98 
     99 MODULE(MODULE_CLASS_VFS, filecorefs, NULL);
    100 
    101 MALLOC_JUSTDEFINE(M_FILECOREMNT,
    102     "filecore mount", "Filecore FS mount structures");
    103 MALLOC_JUSTDEFINE(M_FILECORETMP,
    104     "filecore temp", "Filecore FS temporary structures");
    105 
    106 extern const struct vnodeopv_desc filecore_vnodeop_opv_desc;
    107 
    108 const struct vnodeopv_desc * const filecore_vnodeopv_descs[] = {
    109 	&filecore_vnodeop_opv_desc,
    110 	NULL,
    111 };
    112 
    113 struct vfsops filecore_vfsops = {
    114 	MOUNT_FILECORE,
    115 	sizeof (struct filecore_args),
    116 	filecore_mount,
    117 	filecore_start,
    118 	filecore_unmount,
    119 	filecore_root,
    120 	(void *)eopnotsupp,		/* vfs_quotactl */
    121 	filecore_statvfs,
    122 	filecore_sync,
    123 	filecore_vget,
    124 	filecore_fhtovp,
    125 	filecore_vptofh,
    126 	filecore_init,
    127 	filecore_reinit,
    128 	filecore_done,
    129 	NULL,				/* filecore_mountroot */
    130 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    131 	vfs_stdextattrctl,
    132 	(void *)eopnotsupp,		/* vfs_suspendctl */
    133 	genfs_renamelock_enter,
    134 	genfs_renamelock_exit,
    135 	(void *)eopnotsupp,
    136 	filecore_vnodeopv_descs,
    137 	0,
    138 	{ NULL, NULL }
    139 };
    140 
    141 static const struct genfs_ops filecore_genfsops = {
    142 	.gop_size = genfs_size,
    143 };
    144 
    145 static int
    146 filecorefs_modcmd(modcmd_t cmd, void *arg)
    147 {
    148 
    149 	switch (cmd) {
    150 	case MODULE_CMD_INIT:
    151 		return vfs_attach(&filecore_vfsops);
    152 	case MODULE_CMD_FINI:
    153 		return vfs_detach(&filecore_vfsops);
    154 	default:
    155 		return ENOTTY;
    156 	}
    157 }
    158 
    159 /*
    160  * Called by vfs_mountroot when iso is going to be mounted as root.
    161  *
    162  * Name is updated by mount(8) after booting.
    163  */
    164 
    165 static int filecore_mountfs __P((struct vnode *devvp, struct mount *mp,
    166 		struct lwp *l, struct filecore_args *argp));
    167 
    168 #if 0
    169 int
    170 filecore_mountroot()
    171 {
    172 	struct mount *mp;
    173 	extern struct vnode *rootvp;
    174 	struct proc *p = curproc;	/* XXX */
    175 	int error;
    176 	struct filecore_args args;
    177 
    178 	if (device_class(root_device) != DV_DISK)
    179 		return (ENODEV);
    180 
    181 	/*
    182 	 * Get vnodes for swapdev and rootdev.
    183 	 */
    184 	if (bdevvp(rootdev, &rootvp))
    185 		panic("filecore_mountroot: can't setup rootvp");
    186 
    187 	if ((error = vfs_rootmountalloc(MOUNT_FILECORE, "root_device", &mp)) != 0)
    188 		return (error);
    189 
    190 	args.flags = FILECOREMNT_ROOT;
    191 	if ((error = filecore_mountfs(rootvp, mp, p, &args)) != 0) {
    192 		vfs_unbusy(mp, false, NULL);
    193 		vfs_destroy(mp);
    194 		return (error);
    195 	}
    196 	simple_lock(&mountlist_slock);
    197 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    198 	simple_unlock(&mountlist_slock);
    199 	(void)filecore_statvfs(mp, &mp->mnt_stat, p);
    200 	vfs_unbusy(mp, false, NULL);
    201 	return (0);
    202 }
    203 #endif
    204 
    205 /*
    206  * VFS Operations.
    207  *
    208  * mount system call
    209  */
    210 int
    211 filecore_mount(mp, path, data, data_len)
    212 	struct mount *mp;
    213 	const char *path;
    214 	void *data;
    215 	size_t *data_len;
    216 {
    217 	struct lwp *l = curlwp;
    218 	struct nameidata nd;
    219 	struct vnode *devvp;
    220 	struct filecore_args *args = data;
    221 	int error;
    222 	struct filecore_mnt *fcmp = NULL;
    223 
    224 	if (*data_len < sizeof *args)
    225 		return EINVAL;
    226 
    227 	if (mp->mnt_flag & MNT_GETARGS) {
    228 		fcmp = VFSTOFILECORE(mp);
    229 		if (fcmp == NULL)
    230 			return EIO;
    231 		args->flags = fcmp->fc_mntflags;
    232 		args->uid = fcmp->fc_uid;
    233 		args->gid = fcmp->fc_gid;
    234 		args->fspec = NULL;
    235 		*data_len = sizeof *args;
    236 		return 0;
    237 	}
    238 
    239 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
    240 		return (EROFS);
    241 
    242 	if ((mp->mnt_flag & MNT_UPDATE) && args->fspec == NULL)
    243 		return EINVAL;
    244 
    245 	/*
    246 	 * Not an update, or updating the name: look up the name
    247 	 * and verify that it refers to a sensible block device.
    248 	 */
    249 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec);
    250 	if ((error = namei(&nd)) != 0)
    251 		return (error);
    252 	devvp = nd.ni_vp;
    253 
    254 	if (devvp->v_type != VBLK) {
    255 		vrele(devvp);
    256 		return ENOTBLK;
    257 	}
    258 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
    259 		vrele(devvp);
    260 		return ENXIO;
    261 	}
    262 	/*
    263 	 * If mount by non-root, then verify that user has necessary
    264 	 * permissions on the device.
    265 	 */
    266 	if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
    267 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    268 		error = VOP_ACCESS(devvp, VREAD, l->l_cred);
    269 		VOP_UNLOCK(devvp, 0);
    270 		if (error) {
    271 			vrele(devvp);
    272 			return (error);
    273 		}
    274 	}
    275 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
    276 		error = filecore_mountfs(devvp, mp, l, args);
    277 	else {
    278 		if (devvp != fcmp->fc_devvp)
    279 			error = EINVAL;	/* needs translation */
    280 		else
    281 			vrele(devvp);
    282 	}
    283 	if (error) {
    284 		vrele(devvp);
    285 		return error;
    286 	}
    287 	fcmp = VFSTOFILECORE(mp);
    288 	return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
    289 	    mp->mnt_op->vfs_name, mp, l);
    290 }
    291 
    292 /*
    293  * Common code for mount and mountroot
    294  */
    295 static int
    296 filecore_mountfs(devvp, mp, l, argp)
    297 	struct vnode *devvp;
    298 	struct mount *mp;
    299 	struct lwp *l;
    300 	struct filecore_args *argp;
    301 {
    302 	struct filecore_mnt *fcmp = (struct filecore_mnt *)0;
    303 	struct buf *bp = NULL;
    304 	dev_t dev = devvp->v_rdev;
    305 	int error = EINVAL;
    306 	int ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    307 	struct filecore_disc_record *fcdr;
    308 	unsigned map;
    309 	unsigned log2secsize;
    310 
    311 	if (!ronly)
    312 		return EROFS;
    313 
    314 	if ((error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0)) != 0)
    315 		return (error);
    316 
    317 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED);
    318 	if (error)
    319 		return error;
    320 
    321 	/* Read the filecore boot block to check FS validity and to find the map */
    322 	error = bread(devvp, FILECORE_BOOTBLOCK_BLKN,
    323 			   FILECORE_BOOTBLOCK_SIZE, NOCRED, 0, &bp);
    324 #ifdef FILECORE_DEBUG_BR
    325 		printf("bread(%p, %x, %d, CRED, %p)=%d\n", devvp,
    326 		       FILECORE_BOOTBLOCK_BLKN, FILECORE_BOOTBLOCK_SIZE,
    327 		       bp, error);
    328 #endif
    329 	if (error != 0)
    330 		goto out;
    331 
    332 	KASSERT(bp != NULL);
    333 	if (filecore_bbchecksum(bp->b_data) != 0) {
    334 		error = EINVAL;
    335 		goto out;
    336 	}
    337 	fcdr = (struct filecore_disc_record *)((char *)(bp->b_data) +
    338 	    FILECORE_BB_DISCREC);
    339 	map = ((((8 << fcdr->log2secsize) - fcdr->zone_spare)
    340 	    * (fcdr->nzones / 2) - 8 * FILECORE_DISCREC_SIZE)
    341 	    << fcdr->log2bpmb) >> fcdr->log2secsize;
    342 	log2secsize = fcdr->log2secsize;
    343 #ifdef FILECORE_DEBUG_BR
    344 	printf("brelse(%p) vf1\n", bp);
    345 #endif
    346 	brelse(bp, BC_AGE);
    347 	bp = NULL;
    348 
    349 	/* Read the bootblock in the map */
    350 	error = bread(devvp, map, 1 << log2secsize, NOCRED, 0, &bp);
    351 #ifdef FILECORE_DEBUG_BR
    352 		printf("bread(%p, %x, %d, CRED, %p)=%d\n", devvp,
    353 		       map, 1 << log2secsize, bp, error);
    354 #endif
    355 	if (error != 0)
    356 		goto out;
    357        	fcdr = (struct filecore_disc_record *)((char *)(bp->b_data) + 4);
    358 	fcmp = malloc(sizeof *fcmp, M_FILECOREMNT, M_WAITOK);
    359 	memset(fcmp, 0, sizeof *fcmp);
    360 	if (fcdr->log2bpmb > fcdr->log2secsize)
    361 		fcmp->log2bsize = fcdr->log2bpmb;
    362 	else	fcmp->log2bsize = fcdr->log2secsize;
    363 	fcmp->blksize = 1 << fcmp->log2bsize;
    364 	memcpy(&fcmp->drec, fcdr, sizeof(*fcdr));
    365 	fcmp->map = map;
    366 	fcmp->idspz = ((8 << fcdr->log2secsize) - fcdr->zone_spare)
    367 	    / (fcdr->idlen + 1);
    368 	fcmp->mask = (1 << fcdr->idlen) - 1;
    369 	if (fcdr->big_flag & 1) {
    370 		fcmp->nblks = ((((u_int64_t)fcdr->disc_size_2) << 32)
    371 		    + fcdr->disc_size) / fcmp->blksize;
    372 	} else {
    373 		fcmp->nblks=fcdr->disc_size / fcmp->blksize;
    374 	}
    375 
    376 #ifdef FILECORE_DEBUG_BR
    377 	printf("brelse(%p) vf2\n", bp);
    378 #endif
    379 	brelse(bp, BC_AGE);
    380 	bp = NULL;
    381 
    382 	mp->mnt_data = fcmp;
    383 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
    384 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FILECORE);
    385 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    386 	mp->mnt_stat.f_namemax = 10;
    387 	mp->mnt_flag |= MNT_LOCAL;
    388 	mp->mnt_dev_bshift = fcdr->log2secsize;
    389 	mp->mnt_fs_bshift = fcmp->log2bsize;
    390 
    391 	fcmp->fc_mountp = mp;
    392 	fcmp->fc_dev = dev;
    393 	fcmp->fc_devvp = devvp;
    394 	fcmp->fc_mntflags = argp->flags;
    395 	if (argp->flags & FILECOREMNT_USEUID) {
    396 		fcmp->fc_uid = kauth_cred_getuid(l->l_cred);
    397 		fcmp->fc_gid = kauth_cred_getgid(l->l_cred);
    398 	} else {
    399 		fcmp->fc_uid = argp->uid;
    400 		fcmp->fc_gid = argp->gid;
    401 	}
    402 
    403 	return 0;
    404 out:
    405 	if (bp) {
    406 #ifdef FILECORE_DEBUG_BR
    407 		printf("brelse(%p) vf3\n", bp);
    408 #endif
    409 		brelse(bp, 0);
    410 	}
    411 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    412 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED);
    413 	VOP_UNLOCK(devvp, 0);
    414 	return error;
    415 }
    416 
    417 /*
    418  * Make a filesystem operational.
    419  * Nothing to do at the moment.
    420  */
    421 /* ARGSUSED */
    422 int
    423 filecore_start(mp, flags)
    424 	struct mount *mp;
    425 	int flags;
    426 {
    427 	return 0;
    428 }
    429 
    430 /*
    431  * unmount system call
    432  */
    433 int
    434 filecore_unmount(mp, mntflags)
    435 	struct mount *mp;
    436 	int mntflags;
    437 {
    438 	struct filecore_mnt *fcmp;
    439 	int error, flags = 0;
    440 
    441 	if (mntflags & MNT_FORCE)
    442 		flags |= FORCECLOSE;
    443 	if ((error = vflush(mp, NULLVP, flags)) != 0)
    444 		return (error);
    445 
    446 	fcmp = VFSTOFILECORE(mp);
    447 
    448 	if (fcmp->fc_devvp->v_type != VBAD)
    449 		fcmp->fc_devvp->v_specmountpoint = NULL;
    450 	vn_lock(fcmp->fc_devvp, LK_EXCLUSIVE | LK_RETRY);
    451 	error = VOP_CLOSE(fcmp->fc_devvp, FREAD, NOCRED);
    452 	vput(fcmp->fc_devvp);
    453 	free(fcmp, M_FILECOREMNT);
    454 	mp->mnt_data = NULL;
    455 	mp->mnt_flag &= ~MNT_LOCAL;
    456 	return (error);
    457 }
    458 
    459 /*
    460  * Return root of a filesystem
    461  */
    462 int
    463 filecore_root(mp, vpp)
    464 	struct mount *mp;
    465 	struct vnode **vpp;
    466 {
    467 	struct vnode *nvp;
    468         int error;
    469 
    470         if ((error = VFS_VGET(mp, FILECORE_ROOTINO, &nvp)) != 0)
    471                 return (error);
    472         *vpp = nvp;
    473         return (0);
    474 }
    475 
    476 /*
    477  * Get file system statistics.
    478  */
    479 int
    480 filecore_statvfs(mp, sbp)
    481 	struct mount *mp;
    482 	struct statvfs *sbp;
    483 {
    484 	struct filecore_mnt *fcmp = VFSTOFILECORE(mp);
    485 
    486 	sbp->f_bsize = fcmp->blksize;
    487 	sbp->f_frsize = sbp->f_bsize; /* XXX */
    488 	sbp->f_iosize = sbp->f_bsize;	/* XXX */
    489 	sbp->f_blocks = fcmp->nblks;
    490 	sbp->f_bfree = 0; /* total free blocks */
    491 	sbp->f_bavail = 0; /* blocks free for non superuser */
    492 	sbp->f_bresvd = 0; /* reserved blocks */
    493 	sbp->f_files =  0; /* total files */
    494 	sbp->f_ffree = 0; /* free file nodes for non superuser */
    495 	sbp->f_favail = 0; /* free file nodes */
    496 	sbp->f_fresvd = 0; /* reserved file nodes */
    497 	copy_statvfs_info(sbp, mp);
    498 	return 0;
    499 }
    500 
    501 /* ARGSUSED */
    502 int
    503 filecore_sync(mp, waitfor, cred)
    504 	struct mount *mp;
    505 	int waitfor;
    506 	kauth_cred_t cred;
    507 {
    508 	return (0);
    509 }
    510 
    511 /*
    512  * File handle to vnode
    513  *
    514  * Have to be really careful about stale file handles:
    515  * - check that the inode number is in range
    516  * - call iget() to get the locked inode
    517  * - check for an unallocated inode (i_mode == 0)
    518  * - check that the generation number matches
    519  */
    520 
    521 struct ifid {
    522 	ushort		ifid_len;
    523 	ushort		ifid_pad;
    524 	u_int32_t	ifid_ino;
    525 };
    526 
    527 /* ARGSUSED */
    528 int
    529 filecore_fhtovp(mp, fhp, vpp)
    530 	struct mount *mp;
    531 	struct fid *fhp;
    532 	struct vnode **vpp;
    533 {
    534 	struct ifid ifh;
    535 	struct vnode *nvp;
    536 	struct filecore_node *ip;
    537 	int error;
    538 
    539 	if (fhp->fid_len != sizeof(struct ifid))
    540 		return EINVAL;
    541 
    542 	memcpy(&ifh, fhp, sizeof(ifh));
    543 	if ((error = VFS_VGET(mp, ifh.ifid_ino, &nvp)) != 0) {
    544 		*vpp = NULLVP;
    545 		return (error);
    546 	}
    547         ip = VTOI(nvp);
    548         if (filecore_staleinode(ip)) {
    549                 vput(nvp);
    550                 *vpp = NULLVP;
    551                 return (ESTALE);
    552         }
    553 	*vpp = nvp;
    554 	return (0);
    555 }
    556 
    557 /* This looks complicated. Look at other vgets as well as the iso9660 one.
    558  *
    559  * The filecore inode number is made up of 1 byte directory entry index and
    560  * 3 bytes of the internal disc address of the directory. On a read-only
    561  * filesystem this is unique. For a read-write version we may not be able to
    562  * do vget, see msdosfs.
    563  */
    564 
    565 int
    566 filecore_vget(mp, ino, vpp)
    567 	struct mount *mp;
    568 	ino_t ino;
    569 	struct vnode **vpp;
    570 {
    571 	struct filecore_mnt *fcmp;
    572 	struct filecore_node *ip;
    573 	struct buf *bp;
    574 	struct vnode *vp;
    575 	dev_t dev;
    576 	int error;
    577 
    578 	fcmp = VFSTOFILECORE(mp);
    579 	dev = fcmp->fc_dev;
    580 	if ((*vpp = filecore_ihashget(dev, ino)) != NULLVP)
    581 		return (0);
    582 
    583 	/* Allocate a new vnode/filecore_node. */
    584 	if ((error = getnewvnode(VT_FILECORE, mp, filecore_vnodeop_p, &vp))
    585 	    != 0) {
    586 		*vpp = NULLVP;
    587 		return (error);
    588 	}
    589 	ip = pool_get(&filecore_node_pool, PR_WAITOK);
    590 	memset(ip, 0, sizeof(struct filecore_node));
    591 	vp->v_data = ip;
    592 	ip->i_vnode = vp;
    593 	ip->i_dev = dev;
    594 	ip->i_number = ino;
    595 	ip->i_block = -1;
    596 	ip->i_parent = -2;
    597 	genfs_node_init(vp, &filecore_genfsops);
    598 
    599 	/*
    600 	 * Put it onto its hash chain and lock it so that other requests for
    601 	 * this inode will block if they arrive while we are sleeping waiting
    602 	 * for old data structures to be purged or for the contents of the
    603 	 * disk portion of this inode to be read.
    604 	 */
    605 	filecore_ihashins(ip);
    606 
    607 	if (ino == FILECORE_ROOTINO) {
    608 		/* Here we need to construct a root directory inode */
    609 		memcpy(ip->i_dirent.name, "root", 4);
    610 		ip->i_dirent.load = 0;
    611 		ip->i_dirent.exec = 0;
    612 		ip->i_dirent.len = FILECORE_DIR_SIZE;
    613 		ip->i_dirent.addr = fcmp->drec.root;
    614 		ip->i_dirent.attr = FILECORE_ATTR_DIR | FILECORE_ATTR_READ;
    615 
    616 	} else {
    617 		/* Read in Data from Directory Entry */
    618 		if ((error = filecore_bread(fcmp, ino & FILECORE_INO_MASK,
    619 		    FILECORE_DIR_SIZE, NOCRED, &bp)) != 0) {
    620 			vput(vp);
    621 #ifdef FILECORE_DEBUG_BR
    622 			printf("brelse(%p) vf4\n", bp);
    623 #endif
    624 			brelse(bp, 0);
    625 			*vpp = NULL;
    626 			return (error);
    627 		}
    628 
    629 		memcpy(&ip->i_dirent,
    630 		    fcdirentry(bp->b_data, ino >> FILECORE_INO_INDEX),
    631 		    sizeof(struct filecore_direntry));
    632 #ifdef FILECORE_DEBUG_BR
    633 		printf("brelse(%p) vf5\n", bp);
    634 #endif
    635 		brelse(bp, 0);
    636 	}
    637 
    638 	ip->i_mnt = fcmp;
    639 	ip->i_devvp = fcmp->fc_devvp;
    640 	ip->i_diroff = 0;
    641 	VREF(ip->i_devvp);
    642 
    643 	/*
    644 	 * Setup type
    645 	 */
    646 	vp->v_type = VREG;
    647 	if (ip->i_dirent.attr & FILECORE_ATTR_DIR)
    648 		vp->v_type = VDIR;
    649 
    650 	/*
    651 	 * Initialize the associated vnode
    652 	 */
    653 	switch (vp->v_type) {
    654 	case VFIFO:
    655 	case VCHR:
    656 	case VBLK:
    657 		/*
    658 		 * Devices not supported.
    659 		 */
    660 		vput(vp);
    661 		return (EOPNOTSUPP);
    662 	case VLNK:
    663 	case VNON:
    664 	case VSOCK:
    665 	case VDIR:
    666 	case VBAD:
    667 	case VREG:
    668 		break;
    669 	}
    670 
    671 	if (ino == FILECORE_ROOTINO)
    672 		vp->v_vflag |= VV_ROOT;
    673 
    674 	/*
    675 	 * XXX need generation number?
    676 	 */
    677 
    678 	uvm_vnp_setsize(vp, ip->i_size);
    679 	*vpp = vp;
    680 	return (0);
    681 }
    682 
    683 /*
    684  * Vnode pointer to File handle
    685  */
    686 /* ARGSUSED */
    687 int
    688 filecore_vptofh(vp, fhp, fh_size)
    689 	struct vnode *vp;
    690 	struct fid *fhp;
    691 	size_t *fh_size;
    692 {
    693 	struct filecore_node *ip = VTOI(vp);
    694 	struct ifid ifh;
    695 
    696 	if (*fh_size < sizeof(struct ifid)) {
    697 		*fh_size = sizeof(struct ifid);
    698 		return E2BIG;
    699 	}
    700 	memset(&ifh, 0, sizeof(ifh));
    701 	ifh.ifid_len = sizeof(struct ifid);
    702 	ifh.ifid_ino = ip->i_number;
    703 	memcpy(fhp, &ifh, sizeof(ifh));
    704        	return 0;
    705 }
    706 
    707 SYSCTL_SETUP(sysctl_vfs_filecore_setup, "sysctl vfs.filecore subtree setup")
    708 {
    709 
    710 	sysctl_createv(clog, 0, NULL, NULL,
    711 		       CTLFLAG_PERMANENT,
    712 		       CTLTYPE_NODE, "vfs", NULL,
    713 		       NULL, 0, NULL, 0,
    714 		       CTL_VFS, CTL_EOL);
    715 	sysctl_createv(clog, 0, NULL, NULL,
    716 		       CTLFLAG_PERMANENT,
    717 		       CTLTYPE_NODE, "filecore",
    718 		       SYSCTL_DESCR("Acorn FILECORE file system"),
    719 		       NULL, 0, NULL, 0,
    720 		       CTL_VFS, 19, CTL_EOL);
    721 	/*
    722 	 * XXX the "19" above could be dynamic, thereby eliminating
    723 	 * one more instance of the "number to vfs" mapping problem,
    724 	 * but "19" is the order as taken from sys/mount.h
    725 	 */
    726 }
    727