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