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