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