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