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