xref: /netbsd-src/sys/fs/v7fs/v7fs_vfsops.c (revision 61d03fed13c552b8bcef4e787891c88ef80eb2e0)
1*61d03fedShannken /*	$NetBSD: v7fs_vfsops.c,v 1.19 2022/05/03 07:34:38 hannken Exp $	*/
29255b46fSuch 
39255b46fSuch /*-
49255b46fSuch  * Copyright (c) 2004, 2011 The NetBSD Foundation, Inc.
59255b46fSuch  * All rights reserved.
69255b46fSuch  *
79255b46fSuch  * This code is derived from software contributed to The NetBSD Foundation
89255b46fSuch  * by UCHIYAMA Yasushi.
99255b46fSuch  *
109255b46fSuch  * Redistribution and use in source and binary forms, with or without
119255b46fSuch  * modification, are permitted provided that the following conditions
129255b46fSuch  * are met:
139255b46fSuch  * 1. Redistributions of source code must retain the above copyright
149255b46fSuch  *    notice, this list of conditions and the following disclaimer.
159255b46fSuch  * 2. Redistributions in binary form must reproduce the above copyright
169255b46fSuch  *    notice, this list of conditions and the following disclaimer in the
179255b46fSuch  *    documentation and/or other materials provided with the distribution.
189255b46fSuch  *
199255b46fSuch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
209255b46fSuch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
219255b46fSuch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229255b46fSuch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239255b46fSuch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
249255b46fSuch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
259255b46fSuch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
269255b46fSuch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
279255b46fSuch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
289255b46fSuch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
299255b46fSuch  * POSSIBILITY OF SUCH DAMAGE.
309255b46fSuch  */
319255b46fSuch 
329255b46fSuch #include <sys/cdefs.h>
33*61d03fedShannken __KERNEL_RCSID(0, "$NetBSD: v7fs_vfsops.c,v 1.19 2022/05/03 07:34:38 hannken Exp $");
349255b46fSuch #if defined _KERNEL_OPT
359255b46fSuch #include "opt_v7fs.h"
369255b46fSuch #endif
379255b46fSuch 
389255b46fSuch #include <sys/types.h>
399255b46fSuch #include <sys/param.h>
409255b46fSuch #include <sys/systm.h>
419255b46fSuch #include <sys/pool.h>
429255b46fSuch #include <sys/time.h>
439255b46fSuch #include <sys/ucred.h>
449255b46fSuch #include <sys/mount.h>
451058c21fSchristos #include <sys/disk.h>
461058c21fSchristos #include <sys/device.h>
479255b46fSuch #include <sys/fcntl.h>
48884c21d5Srmind #include <sys/kmem.h>
499255b46fSuch #include <sys/kauth.h>
509255b46fSuch #include <sys/proc.h>
519255b46fSuch 
529255b46fSuch /* v-node */
539255b46fSuch #include <sys/namei.h>
549255b46fSuch #include <sys/vnode.h>
559255b46fSuch /* devsw */
569255b46fSuch #include <sys/conf.h>
579255b46fSuch 
589255b46fSuch #include "v7fs_extern.h"
599255b46fSuch #include "v7fs.h"
609255b46fSuch #include "v7fs_impl.h"
619255b46fSuch #include "v7fs_inode.h"
629255b46fSuch #include "v7fs_superblock.h"
639255b46fSuch 
649255b46fSuch #ifdef V7FS_VFSOPS_DEBUG
659255b46fSuch #define	DPRINTF(fmt, args...)	printf("%s: " fmt, __func__, ##args)
669255b46fSuch #else
679255b46fSuch #define	DPRINTF(arg...)		((void)0)
689255b46fSuch #endif
699255b46fSuch 
709255b46fSuch struct pool v7fs_node_pool;
719255b46fSuch 
729255b46fSuch static int v7fs_mountfs(struct vnode *, struct mount *, int);
739255b46fSuch static int v7fs_openfs(struct vnode *, struct mount *, struct lwp *);
749255b46fSuch static void v7fs_closefs(struct vnode *, struct mount *);
75e8e84f5aSuch static int is_v7fs_partition(struct vnode *);
769255b46fSuch static enum vtype v7fs_mode_to_vtype(v7fs_mode_t mode);
779255b46fSuch 
789255b46fSuch int
v7fs_mount(struct mount * mp,const char * path,void * data,size_t * data_len)799255b46fSuch v7fs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
809255b46fSuch {
819255b46fSuch 	struct lwp *l = curlwp;
829255b46fSuch 	struct v7fs_args *args = data;
839255b46fSuch 	struct v7fs_mount *v7fsmount = (void *)mp->mnt_data;
849255b46fSuch 	struct vnode *devvp = NULL;
8515a60c04Sjoerg 	int error = 0;
869255b46fSuch 	bool update = mp->mnt_flag & MNT_UPDATE;
879255b46fSuch 
889255b46fSuch 	DPRINTF("mnt_flag=%x %s\n", mp->mnt_flag, update ? "update" : "");
899255b46fSuch 
9023f76b6dSmaxv 	if (args == NULL)
9123f76b6dSmaxv 		return EINVAL;
929255b46fSuch 	if (*data_len < sizeof(*args))
939255b46fSuch 		return EINVAL;
949255b46fSuch 
959255b46fSuch 	if (mp->mnt_flag & MNT_GETARGS) {
969255b46fSuch 		if (!v7fsmount)
979255b46fSuch 			return EIO;
989255b46fSuch 		args->fspec = NULL;
999255b46fSuch 		args->endian = v7fsmount->core->endian;
1009255b46fSuch 		*data_len = sizeof(*args);
1019255b46fSuch 		return 0;
1029255b46fSuch 	}
1039255b46fSuch 
1049255b46fSuch 	DPRINTF("args->fspec=%s endian=%d\n", args->fspec, args->endian);
1059255b46fSuch 	if (args->fspec == NULL) {
1069255b46fSuch 		/* nothing to do. */
1079255b46fSuch 		return EINVAL;
1089255b46fSuch 	}
1099255b46fSuch 
1109255b46fSuch 	if (args->fspec != NULL) {
1119255b46fSuch 		/* Look up the name and verify that it's sane. */
1129255b46fSuch 		error = namei_simple_user(args->fspec,
1139255b46fSuch 		    NSM_FOLLOW_NOEMULROOT, &devvp);
1149255b46fSuch 		if (error != 0)
1159255b46fSuch 			return (error);
1169255b46fSuch 		DPRINTF("mount device=%lx\n", (long)devvp->v_rdev);
1179255b46fSuch 
1189255b46fSuch 		if (!update) {
1199255b46fSuch 			/*
1209255b46fSuch 			 * Be sure this is a valid block device
1219255b46fSuch 			 */
1229255b46fSuch 			if (devvp->v_type != VBLK)
1239255b46fSuch 				error = ENOTBLK;
1249255b46fSuch 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
1259255b46fSuch 				error = ENXIO;
1269255b46fSuch 		} else {
1279255b46fSuch 			KDASSERT(v7fsmount);
1289255b46fSuch 			/*
1299255b46fSuch 			 * Be sure we're still naming the same device
1309255b46fSuch 			 * used for our initial mount
1319255b46fSuch 			 */
1329255b46fSuch 			if (devvp != v7fsmount->devvp) {
1339255b46fSuch 				DPRINTF("devvp %p != %p rootvp=%p\n", devvp,
1349255b46fSuch 				    v7fsmount->devvp, rootvp);
1359255b46fSuch 				if (rootvp == v7fsmount->devvp) {
1369255b46fSuch 					vrele(devvp);
1379255b46fSuch 					devvp = rootvp;
1389255b46fSuch 					vref(devvp);
1399255b46fSuch 				} else {
1409255b46fSuch 					error = EINVAL;
1419255b46fSuch 				}
1429255b46fSuch 			}
1439255b46fSuch 		}
1449255b46fSuch 	}
1459255b46fSuch 
1469255b46fSuch 	/*
1479255b46fSuch 	 * If mount by non-root, then verify that user has necessary
1489255b46fSuch 	 * permissions on the device.
1499255b46fSuch 	 *
1509255b46fSuch 	 * Permission to update a mount is checked higher, so here we presume
1519255b46fSuch 	 * updating the mount is okay (for example, as far as securelevel goes)
1529255b46fSuch 	 * which leaves us with the normal check.
1539255b46fSuch 	 */
1549255b46fSuch 	if (error == 0) {
1559255b46fSuch 		int accessmode = VREAD;
1569255b46fSuch 		if (update ?
1579255b46fSuch 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
1589255b46fSuch 		    (mp->mnt_flag & MNT_RDONLY) == 0)
1599255b46fSuch 			accessmode |= VWRITE;
160*61d03fedShannken 
161*61d03fedShannken 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1620c9d8d15Selad 		error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
1630c9d8d15Selad 		    KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp,
1640c9d8d15Selad 		    KAUTH_ARG(accessmode));
165*61d03fedShannken 		VOP_UNLOCK(devvp);
1669255b46fSuch 	}
1679255b46fSuch 
1689255b46fSuch 	if (error) {
1699255b46fSuch 		vrele(devvp);
1709255b46fSuch 		return error;
1719255b46fSuch 	}
1729255b46fSuch 
1739255b46fSuch 	if (!update) {
1749255b46fSuch 		if ((error = v7fs_openfs(devvp, mp, l))) {
1759255b46fSuch 			vrele(devvp);
1769255b46fSuch 			return error;
1779255b46fSuch 		}
1789255b46fSuch 
1799255b46fSuch 		if ((error = v7fs_mountfs(devvp, mp, args->endian))) {
1809255b46fSuch 			v7fs_closefs(devvp, mp);
1819255b46fSuch 			VOP_UNLOCK(devvp);
1829255b46fSuch 			vrele(devvp);
1839255b46fSuch 			return error;
1849255b46fSuch 		}
1859255b46fSuch 		VOP_UNLOCK(devvp);
1869255b46fSuch 	} else 	if (mp->mnt_flag & MNT_RDONLY) {
1879255b46fSuch 		/* XXX: r/w -> read only */
1889255b46fSuch 	}
1899255b46fSuch 
1909255b46fSuch 	return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
1919255b46fSuch 	    mp->mnt_op->vfs_name, mp, l);
1929255b46fSuch }
1939255b46fSuch 
194e8e84f5aSuch static int
is_v7fs_partition(struct vnode * devvp)1959255b46fSuch is_v7fs_partition(struct vnode *devvp)
1969255b46fSuch {
1971058c21fSchristos 	struct dkwedge_info dkw;
1989255b46fSuch 	int error;
1999255b46fSuch 
2001058c21fSchristos 	if ((error = getdiskinfo(devvp, &dkw)) != 0) {
2011058c21fSchristos 		DPRINTF("getdiskinfo=%d\n", error);
202e8e84f5aSuch 		return error;
2039255b46fSuch 	}
204e49cf0feSzafer 	DPRINTF("ptype=%s size=%" PRIu64 "\n", dkw.dkw_ptype, dkw.dkw_size);
2059255b46fSuch 
2061058c21fSchristos 	return strcmp(dkw.dkw_ptype, DKW_PTYPE_V7) == 0 ? 0 : EINVAL;
2079255b46fSuch }
2089255b46fSuch 
2099255b46fSuch static int
v7fs_openfs(struct vnode * devvp,struct mount * mp,struct lwp * l)2109255b46fSuch v7fs_openfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
2119255b46fSuch {
2129255b46fSuch 	kauth_cred_t cred = l->l_cred;
2139255b46fSuch 	int oflags;
2149255b46fSuch 	int error;
2159255b46fSuch 
2169255b46fSuch 	/* Flush buffer */
2179255b46fSuch 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
2189255b46fSuch 	if ((error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0)))
2199255b46fSuch 		goto unlock_exit;
2209255b46fSuch 
2219255b46fSuch 	/* Open block device */
2229255b46fSuch 	oflags = FREAD;
2239255b46fSuch 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
2249255b46fSuch 		oflags |= FWRITE;
2259255b46fSuch 
2269255b46fSuch 	if ((error = VOP_OPEN(devvp, oflags, NOCRED)) != 0) {
2279255b46fSuch 		DPRINTF("VOP_OPEN=%d\n", error);
2289255b46fSuch 		goto unlock_exit;
2299255b46fSuch 	}
2309255b46fSuch 
2319255b46fSuch 	return 0; /* lock held */
2329255b46fSuch 
2339255b46fSuch unlock_exit:
2349255b46fSuch 	VOP_UNLOCK(devvp);
2359255b46fSuch 
2369255b46fSuch 	return error;
2379255b46fSuch }
2389255b46fSuch 
2399255b46fSuch static void
v7fs_closefs(struct vnode * devvp,struct mount * mp)2409255b46fSuch v7fs_closefs(struct vnode *devvp, struct mount *mp)
2419255b46fSuch {
2429255b46fSuch 	int oflags = FREAD;
2439255b46fSuch 
2449255b46fSuch 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
2459255b46fSuch 		oflags |= FWRITE;
2469255b46fSuch 
2479255b46fSuch 	VOP_CLOSE(devvp, oflags, NOCRED);
2489255b46fSuch }
2499255b46fSuch 
2509255b46fSuch static int
v7fs_mountfs(struct vnode * devvp,struct mount * mp,int endian)2519255b46fSuch v7fs_mountfs(struct vnode *devvp, struct mount *mp, int endian)
2529255b46fSuch {
2539255b46fSuch 	struct v7fs_mount *v7fsmount;
2549255b46fSuch 	int error;
2559255b46fSuch 	struct v7fs_mount_device mount;
2569255b46fSuch 
2579255b46fSuch 	DPRINTF("%d\n",endian);
2589255b46fSuch 
259884c21d5Srmind 	v7fsmount = kmem_zalloc(sizeof(*v7fsmount), KM_SLEEP);
2609255b46fSuch 	v7fsmount->devvp = devvp;
2619255b46fSuch 	v7fsmount->mountp = mp;
2629255b46fSuch 
2639255b46fSuch 	mount.device.vnode = devvp;
2649255b46fSuch 	mount.endian = endian;
2659255b46fSuch 
2669255b46fSuch 	if ((error = v7fs_io_init(&v7fsmount->core, &mount, V7FS_BSIZE))) {
2679255b46fSuch 		goto err_exit;
2689255b46fSuch 	}
2699255b46fSuch 	struct v7fs_self *fs = v7fsmount->core;
2709255b46fSuch 
2719255b46fSuch 	if ((error = v7fs_superblock_load(fs))) {
2729255b46fSuch 		v7fs_io_fini(fs);
2739255b46fSuch 		goto err_exit;
2749255b46fSuch 	}
2759255b46fSuch 
2769255b46fSuch 	mp->mnt_data = v7fsmount;
2779255b46fSuch 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)devvp->v_rdev;
2789255b46fSuch 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_V7FS);
2799255b46fSuch 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
2809255b46fSuch 	mp->mnt_stat.f_namemax = V7FS_NAME_MAX;
2819255b46fSuch 	mp->mnt_flag |= MNT_LOCAL;
2829255b46fSuch 	mp->mnt_dev_bshift = V7FS_BSHIFT;
2839255b46fSuch 	mp->mnt_fs_bshift = V7FS_BSHIFT;
2849255b46fSuch 
2859255b46fSuch 	return 0;
2869255b46fSuch 
2879255b46fSuch err_exit:
288884c21d5Srmind 	kmem_free(v7fsmount, sizeof(*v7fsmount));
2899255b46fSuch 	return error;
2909255b46fSuch }
2919255b46fSuch 
2929255b46fSuch int
v7fs_start(struct mount * mp,int flags)2939255b46fSuch v7fs_start(struct mount *mp, int flags)
2949255b46fSuch {
2959255b46fSuch 
2969255b46fSuch 	DPRINTF("\n");
2979255b46fSuch 	/* Nothing to do. */
2989255b46fSuch 	return 0;
2999255b46fSuch }
3009255b46fSuch 
3019255b46fSuch int
v7fs_unmount(struct mount * mp,int mntflags)3029255b46fSuch v7fs_unmount(struct mount *mp, int mntflags)
3039255b46fSuch {
3049255b46fSuch 	struct v7fs_mount *v7fsmount = (void *)mp->mnt_data;
3059255b46fSuch 	int error;
3069255b46fSuch 
3079255b46fSuch 	DPRINTF("%p\n", v7fsmount);
3089255b46fSuch 
3099255b46fSuch 	if ((error = vflush(mp, NULLVP,
3109255b46fSuch 		    mntflags & MNT_FORCE ? FORCECLOSE : 0)) != 0)
3119255b46fSuch 		return error;
3129255b46fSuch 
3139255b46fSuch 	vn_lock(v7fsmount->devvp, LK_EXCLUSIVE | LK_RETRY);
3149255b46fSuch 	error = VOP_CLOSE(v7fsmount->devvp, FREAD, NOCRED);
3159255b46fSuch 	vput(v7fsmount->devvp);
3169255b46fSuch 
3179255b46fSuch 	v7fs_io_fini(v7fsmount->core);
3189255b46fSuch 
319884c21d5Srmind 	kmem_free(v7fsmount, sizeof(*v7fsmount));
3209255b46fSuch 	mp->mnt_data = NULL;
3219255b46fSuch 	mp->mnt_flag &= ~MNT_LOCAL;
3229255b46fSuch 
3239255b46fSuch 	return 0;
3249255b46fSuch }
3259255b46fSuch 
3269255b46fSuch int
v7fs_root(struct mount * mp,int lktype,struct vnode ** vpp)327c2e9cb94Sad v7fs_root(struct mount *mp, int lktype, struct vnode **vpp)
3289255b46fSuch {
3299255b46fSuch 	struct vnode *vp;
3309255b46fSuch 	int error;
3319255b46fSuch 
3329255b46fSuch 	DPRINTF("\n");
333c2e9cb94Sad 	if ((error = VFS_VGET(mp, V7FS_ROOT_INODE, lktype, &vp)) != 0) {
3349255b46fSuch 		DPRINTF("error=%d\n", error);
3359255b46fSuch 		return error;
3369255b46fSuch 	}
3379255b46fSuch 	*vpp = vp;
3389255b46fSuch 	DPRINTF("done.\n");
3399255b46fSuch 
3409255b46fSuch 	return 0;
3419255b46fSuch }
3429255b46fSuch 
3439255b46fSuch int
v7fs_statvfs(struct mount * mp,struct statvfs * f)3449255b46fSuch v7fs_statvfs(struct mount *mp, struct statvfs *f)
3459255b46fSuch {
3469255b46fSuch 	struct v7fs_mount *v7fsmount = mp->mnt_data;
3479255b46fSuch 	struct v7fs_self *fs = v7fsmount->core;
3489255b46fSuch 
3499255b46fSuch 	DPRINTF("scratch remain=%d\n", fs->scratch_remain);
3509255b46fSuch 
3519255b46fSuch 	v7fs_superblock_status(fs);
3529255b46fSuch 
3539255b46fSuch 	f->f_bsize = V7FS_BSIZE;
3549255b46fSuch 	f->f_frsize = V7FS_BSIZE;
3559255b46fSuch 	f->f_iosize = V7FS_BSIZE;
3569255b46fSuch 	f->f_blocks = fs->stat.total_blocks;
3579255b46fSuch 	f->f_bfree = fs->stat.free_blocks;
3589255b46fSuch 	f->f_bavail = fs->stat.free_blocks;
3599255b46fSuch 	f->f_bresvd = 0;
3609255b46fSuch 	f->f_files = fs->stat.total_files;
3619255b46fSuch 	f->f_ffree = fs->stat.free_inode;
3629255b46fSuch 	f->f_favail = f->f_ffree;
3639255b46fSuch 	f->f_fresvd = 0;
3649255b46fSuch 	copy_statvfs_info(f, mp);
3659255b46fSuch 
3669255b46fSuch 	return 0;
3679255b46fSuch }
3689255b46fSuch 
3699837a8c2Shannken static bool
v7fs_sync_selector(void * cl,struct vnode * vp)3709837a8c2Shannken v7fs_sync_selector(void *cl, struct vnode *vp)
3719837a8c2Shannken {
37230509f80Sriastradh 	struct v7fs_node *v7fs_node;
3739837a8c2Shannken 
37430509f80Sriastradh 	KASSERT(mutex_owned(vp->v_interlock));
37530509f80Sriastradh 
37630509f80Sriastradh 	v7fs_node = vp->v_data;
3779837a8c2Shannken 	if (v7fs_node == NULL)
3789837a8c2Shannken 		return false;
3799837a8c2Shannken 	if (!v7fs_inode_allocated(&v7fs_node->inode))
3809837a8c2Shannken 		return false;
3819837a8c2Shannken 
3829837a8c2Shannken 	return true;
3839837a8c2Shannken }
3849837a8c2Shannken 
3859255b46fSuch int
v7fs_sync(struct mount * mp,int waitfor,kauth_cred_t cred)3869255b46fSuch v7fs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
3879255b46fSuch {
3889255b46fSuch 	struct v7fs_mount *v7fsmount = mp->mnt_data;
3899255b46fSuch 	struct v7fs_self *fs = v7fsmount->core;
3909837a8c2Shannken 	struct vnode_iterator *marker;
3919837a8c2Shannken 	struct vnode *vp;
3929255b46fSuch 	int err, error;
3939255b46fSuch 
3949255b46fSuch 	DPRINTF("\n");
3959255b46fSuch 
3969255b46fSuch 	v7fs_superblock_writeback(fs);
3979255b46fSuch 	error = 0;
3989837a8c2Shannken 	vfs_vnode_iterator_init(mp, &marker);
3999837a8c2Shannken 	while ((vp = vfs_vnode_iterator_next(marker,
4009837a8c2Shannken 	    v7fs_sync_selector, NULL)) != NULL) {
4019837a8c2Shannken 		err = vn_lock(vp, LK_EXCLUSIVE);
4029837a8c2Shannken 		if (err) {
4039837a8c2Shannken 			vrele(vp);
4049255b46fSuch 			continue;
4059255b46fSuch 		}
4069837a8c2Shannken 		err = VOP_FSYNC(vp, cred, FSYNC_WAIT, 0, 0);
4079837a8c2Shannken 		vput(vp);
4089255b46fSuch 		if (err != 0)
4099255b46fSuch 			error = err;
4109255b46fSuch 	}
4119837a8c2Shannken 	vfs_vnode_iterator_destroy(marker);
4129255b46fSuch 
4139255b46fSuch 	return error;
4149255b46fSuch }
4159255b46fSuch 
4169255b46fSuch static enum vtype
v7fs_mode_to_vtype(v7fs_mode_t mode)4179255b46fSuch v7fs_mode_to_vtype (v7fs_mode_t mode)
4189255b46fSuch {
4199255b46fSuch 	enum vtype table[] = { VCHR, VDIR, VBLK, VREG, VLNK, VSOCK };
4209255b46fSuch 
4219255b46fSuch 	if ((mode & V7FS_IFMT) == V7FSBSD_IFFIFO)
4229255b46fSuch 		return VFIFO;
4239255b46fSuch 
4249255b46fSuch 	return table[((mode >> 13) & 7) - 1];
4259255b46fSuch }
4269255b46fSuch 
4279255b46fSuch int
v7fs_loadvnode(struct mount * mp,struct vnode * vp,const void * key,size_t key_len,const void ** new_key)4289837a8c2Shannken v7fs_loadvnode(struct mount *mp, struct vnode *vp,
4299837a8c2Shannken     const void *key, size_t key_len, const void **new_key)
4309255b46fSuch {
4319837a8c2Shannken 	struct v7fs_mount *v7fsmount;
4329837a8c2Shannken 	struct v7fs_self *fs;
4339255b46fSuch 	struct v7fs_node *v7fs_node;
4349255b46fSuch 	struct v7fs_inode inode;
4359837a8c2Shannken 	v7fs_ino_t number;
4369255b46fSuch 	int error;
4379255b46fSuch 
4389837a8c2Shannken 	KASSERT(key_len == sizeof(number));
4399837a8c2Shannken 	memcpy(&number, key, key_len);
4409837a8c2Shannken 
4419837a8c2Shannken 	v7fsmount = mp->mnt_data;
4429837a8c2Shannken 	fs = v7fsmount->core;
4439837a8c2Shannken 
4449255b46fSuch 	/* Lookup requested i-node */
4459837a8c2Shannken 	if ((error = v7fs_inode_load(fs, &inode, number))) {
4469255b46fSuch 		DPRINTF("v7fs_inode_load failed.\n");
4479255b46fSuch 		return error;
4489255b46fSuch 	}
4499255b46fSuch 
4509837a8c2Shannken 	v7fs_node = pool_get(&v7fs_node_pool, PR_WAITOK);
4519837a8c2Shannken 	memset(v7fs_node, 0, sizeof(*v7fs_node));
4529255b46fSuch 
4539837a8c2Shannken 	vp->v_tag = VT_V7FS;
4549837a8c2Shannken 	vp->v_data = v7fs_node;
4559255b46fSuch 	v7fs_node->vnode = vp;
4569255b46fSuch 	v7fs_node->v7fsmount = v7fsmount;
4579255b46fSuch 	v7fs_node->inode = inode;/*structure copy */
4589255b46fSuch 	v7fs_node->lockf = NULL; /* advlock */
4599255b46fSuch 
4609255b46fSuch 	genfs_node_init(vp, &v7fs_genfsops);
4619255b46fSuch 	uvm_vnp_setsize(vp, v7fs_inode_filesize(&inode));
4629255b46fSuch 
4639837a8c2Shannken 	if (number == V7FS_ROOT_INODE) {
4649255b46fSuch 		vp->v_type = VDIR;
4659255b46fSuch 		vp->v_vflag |= VV_ROOT;
4669837a8c2Shannken 		vp->v_op = v7fs_vnodeop_p;
4679255b46fSuch 	} else {
4689255b46fSuch 		vp->v_type = v7fs_mode_to_vtype(inode.mode);
4699255b46fSuch 
4709255b46fSuch 		if (vp->v_type == VBLK || vp->v_type == VCHR) {
4719255b46fSuch 			dev_t rdev = inode.device;
4729255b46fSuch 			vp->v_op = v7fs_specop_p;
4739255b46fSuch 			spec_node_init(vp, rdev);
4749255b46fSuch 		} else if (vp->v_type == VFIFO) {
4759255b46fSuch 			vp->v_op = v7fs_fifoop_p;
4769837a8c2Shannken 		} else {
4779837a8c2Shannken 			vp->v_op = v7fs_vnodeop_p;
4789255b46fSuch 		}
4799255b46fSuch 	}
4809255b46fSuch 
4819837a8c2Shannken 	*new_key = &v7fs_node->inode.inode_number;
4829837a8c2Shannken 
4839837a8c2Shannken 	return 0;
4849837a8c2Shannken }
4859837a8c2Shannken 
4869837a8c2Shannken 
4879837a8c2Shannken int
v7fs_vget(struct mount * mp,ino_t ino,int lktype,struct vnode ** vpp)488c2e9cb94Sad v7fs_vget(struct mount *mp, ino_t ino, int lktype, struct vnode **vpp)
4899837a8c2Shannken {
4909837a8c2Shannken 	int error;
4919837a8c2Shannken 	v7fs_ino_t number;
4929837a8c2Shannken 	struct vnode *vp;
4939837a8c2Shannken 
4949837a8c2Shannken 	KASSERT(ino <= UINT16_MAX);
4959837a8c2Shannken 	number = ino;
4969837a8c2Shannken 
4979837a8c2Shannken 	error = vcache_get(mp, &number, sizeof(number), &vp);
4989837a8c2Shannken 	if (error)
4999837a8c2Shannken 		return error;
500c2e9cb94Sad 	error = vn_lock(vp, lktype);
5019837a8c2Shannken 	if (error) {
5029837a8c2Shannken 		vrele(vp);
5039837a8c2Shannken 		return error;
5049837a8c2Shannken 	}
5059837a8c2Shannken 
5069255b46fSuch 	*vpp = vp;
5079255b46fSuch 
5089255b46fSuch 	return 0;
5099255b46fSuch }
5109255b46fSuch 
5119255b46fSuch int
v7fs_fhtovp(struct mount * mp,struct fid * fid,int lktype,struct vnode ** vpp)512c2e9cb94Sad v7fs_fhtovp(struct mount *mp, struct fid *fid, int lktype, struct vnode **vpp)
5139255b46fSuch {
5149255b46fSuch 
5159255b46fSuch 	DPRINTF("\n");
5169255b46fSuch 	/* notyet */
5179255b46fSuch 	return EOPNOTSUPP;
5189255b46fSuch }
5199255b46fSuch 
5209255b46fSuch int
v7fs_vptofh(struct vnode * vpp,struct fid * fid,size_t * fh_size)5219255b46fSuch v7fs_vptofh(struct vnode *vpp, struct fid *fid, size_t *fh_size)
5229255b46fSuch {
5239255b46fSuch 
5249255b46fSuch 	DPRINTF("\n");
5259255b46fSuch 	/* notyet */
5269255b46fSuch 	return EOPNOTSUPP;
5279255b46fSuch }
5289255b46fSuch 
5299255b46fSuch void
v7fs_init(void)5309255b46fSuch v7fs_init(void)
5319255b46fSuch {
5329255b46fSuch 
5339255b46fSuch 	DPRINTF("\n");
5349255b46fSuch 	pool_init(&v7fs_node_pool, sizeof(struct v7fs_node), 0, 0, 0,
5359255b46fSuch 	    "v7fs_node_pool", &pool_allocator_nointr, IPL_NONE);
5369255b46fSuch }
5379255b46fSuch 
5389255b46fSuch void
v7fs_reinit(void)5399255b46fSuch v7fs_reinit(void)
5409255b46fSuch {
5419255b46fSuch 
5429255b46fSuch 	/* Nothing to do. */
5439255b46fSuch 	DPRINTF("\n");
5449255b46fSuch }
5459255b46fSuch 
5469255b46fSuch void
v7fs_done(void)5479255b46fSuch v7fs_done(void)
5489255b46fSuch {
5499255b46fSuch 
5509255b46fSuch 	DPRINTF("\n");
5519255b46fSuch 	pool_destroy(&v7fs_node_pool);
5529255b46fSuch }
5539255b46fSuch 
5549255b46fSuch int
v7fs_gop_alloc(struct vnode * vp,off_t off,off_t len,int flags,kauth_cred_t cred)5559255b46fSuch v7fs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
5569255b46fSuch     kauth_cred_t cred)
5579255b46fSuch {
5589255b46fSuch 
5599255b46fSuch 	DPRINTF("\n");
5609255b46fSuch 	return 0;
5619255b46fSuch }
5629255b46fSuch 
5639255b46fSuch int
v7fs_mountroot(void)5649255b46fSuch v7fs_mountroot(void)
5659255b46fSuch {
5669255b46fSuch 	struct mount *mp;
5679255b46fSuch 	int error;
5689255b46fSuch 
5699255b46fSuch 	DPRINTF("");
5709255b46fSuch 	/* On mountroot, devvp (rootdev) is opened by vfs_mountroot */
571e8e84f5aSuch 	if ((error = is_v7fs_partition (rootvp)))
572e8e84f5aSuch 		return error;
5739255b46fSuch 
5749255b46fSuch 	if ((error = vfs_rootmountalloc(MOUNT_V7FS, "root_device", &mp))) {
5759255b46fSuch 		DPRINTF("mountalloc error=%d\n", error);
5769255b46fSuch 		vrele(rootvp);
5779255b46fSuch 		return error;
5789255b46fSuch 	}
5799255b46fSuch 
5809255b46fSuch 	if ((error = v7fs_mountfs(rootvp, mp, _BYTE_ORDER))) {
5819255b46fSuch 		DPRINTF("mountfs error=%d\n", error);
58220bb034fShannken 		vfs_unbusy(mp);
583ebb8f73bShannken 		vfs_rele(mp);
5849255b46fSuch 		return error;
5859255b46fSuch 	}
5869255b46fSuch 
5870b725b63Schristos 	mountlist_append(mp);
5889255b46fSuch 
58920bb034fShannken 	vfs_unbusy(mp);
5909255b46fSuch 
5919255b46fSuch 	return 0;
5929255b46fSuch }
593