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