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