1*0a6a1f1dSLionel Sambuc /* $NetBSD: v7fs_vfsops.c,v 1.12 2014/12/29 15:29:38 hannken Exp $ */
29f988b79SJean-Baptiste Boric
39f988b79SJean-Baptiste Boric /*-
49f988b79SJean-Baptiste Boric * Copyright (c) 2004, 2011 The NetBSD Foundation, Inc.
59f988b79SJean-Baptiste Boric * All rights reserved.
69f988b79SJean-Baptiste Boric *
79f988b79SJean-Baptiste Boric * This code is derived from software contributed to The NetBSD Foundation
89f988b79SJean-Baptiste Boric * by UCHIYAMA Yasushi.
99f988b79SJean-Baptiste Boric *
109f988b79SJean-Baptiste Boric * Redistribution and use in source and binary forms, with or without
119f988b79SJean-Baptiste Boric * modification, are permitted provided that the following conditions
129f988b79SJean-Baptiste Boric * are met:
139f988b79SJean-Baptiste Boric * 1. Redistributions of source code must retain the above copyright
149f988b79SJean-Baptiste Boric * notice, this list of conditions and the following disclaimer.
159f988b79SJean-Baptiste Boric * 2. Redistributions in binary form must reproduce the above copyright
169f988b79SJean-Baptiste Boric * notice, this list of conditions and the following disclaimer in the
179f988b79SJean-Baptiste Boric * documentation and/or other materials provided with the distribution.
189f988b79SJean-Baptiste Boric *
199f988b79SJean-Baptiste Boric * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
209f988b79SJean-Baptiste Boric * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
219f988b79SJean-Baptiste Boric * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229f988b79SJean-Baptiste Boric * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239f988b79SJean-Baptiste Boric * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
249f988b79SJean-Baptiste Boric * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
259f988b79SJean-Baptiste Boric * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
269f988b79SJean-Baptiste Boric * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
279f988b79SJean-Baptiste Boric * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
289f988b79SJean-Baptiste Boric * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
299f988b79SJean-Baptiste Boric * POSSIBILITY OF SUCH DAMAGE.
309f988b79SJean-Baptiste Boric */
319f988b79SJean-Baptiste Boric
329f988b79SJean-Baptiste Boric #include <sys/cdefs.h>
33*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: v7fs_vfsops.c,v 1.12 2014/12/29 15:29:38 hannken Exp $");
349f988b79SJean-Baptiste Boric #if defined _KERNEL_OPT
359f988b79SJean-Baptiste Boric #include "opt_v7fs.h"
369f988b79SJean-Baptiste Boric #endif
379f988b79SJean-Baptiste Boric
389f988b79SJean-Baptiste Boric #include <sys/types.h>
399f988b79SJean-Baptiste Boric #include <sys/param.h>
409f988b79SJean-Baptiste Boric #include <sys/systm.h>
419f988b79SJean-Baptiste Boric #include <sys/pool.h>
429f988b79SJean-Baptiste Boric #include <sys/time.h>
439f988b79SJean-Baptiste Boric #include <sys/ucred.h>
449f988b79SJean-Baptiste Boric #include <sys/mount.h>
459f988b79SJean-Baptiste Boric #include <sys/disk.h>
469f988b79SJean-Baptiste Boric #include <sys/device.h>
479f988b79SJean-Baptiste Boric #include <sys/fcntl.h>
489f988b79SJean-Baptiste Boric #include <sys/kmem.h>
499f988b79SJean-Baptiste Boric #include <sys/kauth.h>
509f988b79SJean-Baptiste Boric #include <sys/proc.h>
519f988b79SJean-Baptiste Boric
529f988b79SJean-Baptiste Boric /* v-node */
539f988b79SJean-Baptiste Boric #include <sys/namei.h>
549f988b79SJean-Baptiste Boric #include <sys/vnode.h>
559f988b79SJean-Baptiste Boric /* devsw */
569f988b79SJean-Baptiste Boric #include <sys/conf.h>
579f988b79SJean-Baptiste Boric
589f988b79SJean-Baptiste Boric #include "v7fs_extern.h"
599f988b79SJean-Baptiste Boric #include "v7fs.h"
609f988b79SJean-Baptiste Boric #include "v7fs_impl.h"
619f988b79SJean-Baptiste Boric #include "v7fs_inode.h"
629f988b79SJean-Baptiste Boric #include "v7fs_superblock.h"
639f988b79SJean-Baptiste Boric
649f988b79SJean-Baptiste Boric #ifdef V7FS_VFSOPS_DEBUG
659f988b79SJean-Baptiste Boric #define DPRINTF(fmt, args...) printf("%s: " fmt, __func__, ##args)
669f988b79SJean-Baptiste Boric #else
679f988b79SJean-Baptiste Boric #define DPRINTF(arg...) ((void)0)
689f988b79SJean-Baptiste Boric #endif
699f988b79SJean-Baptiste Boric
709f988b79SJean-Baptiste Boric struct pool v7fs_node_pool;
719f988b79SJean-Baptiste Boric
729f988b79SJean-Baptiste Boric static int v7fs_mountfs(struct vnode *, struct mount *, int);
739f988b79SJean-Baptiste Boric static int v7fs_openfs(struct vnode *, struct mount *, struct lwp *);
749f988b79SJean-Baptiste Boric static void v7fs_closefs(struct vnode *, struct mount *);
759f988b79SJean-Baptiste Boric static int is_v7fs_partition(struct vnode *);
769f988b79SJean-Baptiste Boric static enum vtype v7fs_mode_to_vtype(v7fs_mode_t mode);
779f988b79SJean-Baptiste Boric
789f988b79SJean-Baptiste Boric int
v7fs_mount(struct mount * mp,const char * path,void * data,size_t * data_len)799f988b79SJean-Baptiste Boric v7fs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
809f988b79SJean-Baptiste Boric {
819f988b79SJean-Baptiste Boric struct lwp *l = curlwp;
829f988b79SJean-Baptiste Boric struct v7fs_args *args = data;
839f988b79SJean-Baptiste Boric struct v7fs_mount *v7fsmount = (void *)mp->mnt_data;
849f988b79SJean-Baptiste Boric struct vnode *devvp = NULL;
859f988b79SJean-Baptiste Boric int error = 0;
869f988b79SJean-Baptiste Boric bool update = mp->mnt_flag & MNT_UPDATE;
879f988b79SJean-Baptiste Boric
889f988b79SJean-Baptiste Boric DPRINTF("mnt_flag=%x %s\n", mp->mnt_flag, update ? "update" : "");
899f988b79SJean-Baptiste Boric
90*0a6a1f1dSLionel Sambuc if (args == NULL)
91*0a6a1f1dSLionel Sambuc return EINVAL;
929f988b79SJean-Baptiste Boric if (*data_len < sizeof(*args))
939f988b79SJean-Baptiste Boric return EINVAL;
949f988b79SJean-Baptiste Boric
959f988b79SJean-Baptiste Boric if (mp->mnt_flag & MNT_GETARGS) {
969f988b79SJean-Baptiste Boric if (!v7fsmount)
979f988b79SJean-Baptiste Boric return EIO;
989f988b79SJean-Baptiste Boric args->fspec = NULL;
999f988b79SJean-Baptiste Boric args->endian = v7fsmount->core->endian;
1009f988b79SJean-Baptiste Boric *data_len = sizeof(*args);
1019f988b79SJean-Baptiste Boric return 0;
1029f988b79SJean-Baptiste Boric }
1039f988b79SJean-Baptiste Boric
1049f988b79SJean-Baptiste Boric DPRINTF("args->fspec=%s endian=%d\n", args->fspec, args->endian);
1059f988b79SJean-Baptiste Boric if (args->fspec == NULL) {
1069f988b79SJean-Baptiste Boric /* nothing to do. */
1079f988b79SJean-Baptiste Boric return EINVAL;
1089f988b79SJean-Baptiste Boric }
1099f988b79SJean-Baptiste Boric
1109f988b79SJean-Baptiste Boric if (args->fspec != NULL) {
1119f988b79SJean-Baptiste Boric /* Look up the name and verify that it's sane. */
1129f988b79SJean-Baptiste Boric error = namei_simple_user(args->fspec,
1139f988b79SJean-Baptiste Boric NSM_FOLLOW_NOEMULROOT, &devvp);
1149f988b79SJean-Baptiste Boric if (error != 0)
1159f988b79SJean-Baptiste Boric return (error);
1169f988b79SJean-Baptiste Boric DPRINTF("mount device=%lx\n", (long)devvp->v_rdev);
1179f988b79SJean-Baptiste Boric
1189f988b79SJean-Baptiste Boric if (!update) {
1199f988b79SJean-Baptiste Boric /*
1209f988b79SJean-Baptiste Boric * Be sure this is a valid block device
1219f988b79SJean-Baptiste Boric */
1229f988b79SJean-Baptiste Boric if (devvp->v_type != VBLK)
1239f988b79SJean-Baptiste Boric error = ENOTBLK;
1249f988b79SJean-Baptiste Boric else if (bdevsw_lookup(devvp->v_rdev) == NULL)
1259f988b79SJean-Baptiste Boric error = ENXIO;
1269f988b79SJean-Baptiste Boric } else {
1279f988b79SJean-Baptiste Boric KDASSERT(v7fsmount);
1289f988b79SJean-Baptiste Boric /*
1299f988b79SJean-Baptiste Boric * Be sure we're still naming the same device
1309f988b79SJean-Baptiste Boric * used for our initial mount
1319f988b79SJean-Baptiste Boric */
1329f988b79SJean-Baptiste Boric if (devvp != v7fsmount->devvp) {
1339f988b79SJean-Baptiste Boric DPRINTF("devvp %p != %p rootvp=%p\n", devvp,
1349f988b79SJean-Baptiste Boric v7fsmount->devvp, rootvp);
1359f988b79SJean-Baptiste Boric if (rootvp == v7fsmount->devvp) {
1369f988b79SJean-Baptiste Boric vrele(devvp);
1379f988b79SJean-Baptiste Boric devvp = rootvp;
1389f988b79SJean-Baptiste Boric vref(devvp);
1399f988b79SJean-Baptiste Boric } else {
1409f988b79SJean-Baptiste Boric error = EINVAL;
1419f988b79SJean-Baptiste Boric }
1429f988b79SJean-Baptiste Boric }
1439f988b79SJean-Baptiste Boric }
1449f988b79SJean-Baptiste Boric }
1459f988b79SJean-Baptiste Boric
1469f988b79SJean-Baptiste Boric /*
1479f988b79SJean-Baptiste Boric * If mount by non-root, then verify that user has necessary
1489f988b79SJean-Baptiste Boric * permissions on the device.
1499f988b79SJean-Baptiste Boric *
1509f988b79SJean-Baptiste Boric * Permission to update a mount is checked higher, so here we presume
1519f988b79SJean-Baptiste Boric * updating the mount is okay (for example, as far as securelevel goes)
1529f988b79SJean-Baptiste Boric * which leaves us with the normal check.
1539f988b79SJean-Baptiste Boric */
1549f988b79SJean-Baptiste Boric if (error == 0) {
1559f988b79SJean-Baptiste Boric int accessmode = VREAD;
1569f988b79SJean-Baptiste Boric if (update ?
1579f988b79SJean-Baptiste Boric (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
1589f988b79SJean-Baptiste Boric (mp->mnt_flag & MNT_RDONLY) == 0)
1599f988b79SJean-Baptiste Boric accessmode |= VWRITE;
1609f988b79SJean-Baptiste Boric error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
1619f988b79SJean-Baptiste Boric KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp,
1629f988b79SJean-Baptiste Boric KAUTH_ARG(accessmode));
1639f988b79SJean-Baptiste Boric }
1649f988b79SJean-Baptiste Boric
1659f988b79SJean-Baptiste Boric if (error) {
1669f988b79SJean-Baptiste Boric vrele(devvp);
1679f988b79SJean-Baptiste Boric return error;
1689f988b79SJean-Baptiste Boric }
1699f988b79SJean-Baptiste Boric
1709f988b79SJean-Baptiste Boric if (!update) {
1719f988b79SJean-Baptiste Boric if ((error = v7fs_openfs(devvp, mp, l))) {
1729f988b79SJean-Baptiste Boric vrele(devvp);
1739f988b79SJean-Baptiste Boric return error;
1749f988b79SJean-Baptiste Boric }
1759f988b79SJean-Baptiste Boric
1769f988b79SJean-Baptiste Boric if ((error = v7fs_mountfs(devvp, mp, args->endian))) {
1779f988b79SJean-Baptiste Boric v7fs_closefs(devvp, mp);
1789f988b79SJean-Baptiste Boric VOP_UNLOCK(devvp);
1799f988b79SJean-Baptiste Boric vrele(devvp);
1809f988b79SJean-Baptiste Boric return error;
1819f988b79SJean-Baptiste Boric }
1829f988b79SJean-Baptiste Boric VOP_UNLOCK(devvp);
1839f988b79SJean-Baptiste Boric } else if (mp->mnt_flag & MNT_RDONLY) {
1849f988b79SJean-Baptiste Boric /* XXX: r/w -> read only */
1859f988b79SJean-Baptiste Boric }
1869f988b79SJean-Baptiste Boric
1879f988b79SJean-Baptiste Boric return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
1889f988b79SJean-Baptiste Boric mp->mnt_op->vfs_name, mp, l);
1899f988b79SJean-Baptiste Boric }
1909f988b79SJean-Baptiste Boric
1919f988b79SJean-Baptiste Boric static int
is_v7fs_partition(struct vnode * devvp)1929f988b79SJean-Baptiste Boric is_v7fs_partition(struct vnode *devvp)
1939f988b79SJean-Baptiste Boric {
1949f988b79SJean-Baptiste Boric struct dkwedge_info dkw;
1959f988b79SJean-Baptiste Boric int error;
1969f988b79SJean-Baptiste Boric
1979f988b79SJean-Baptiste Boric if ((error = getdiskinfo(devvp, &dkw)) != 0) {
1989f988b79SJean-Baptiste Boric DPRINTF("getdiskinfo=%d\n", error);
1999f988b79SJean-Baptiste Boric return error;
2009f988b79SJean-Baptiste Boric }
2019f988b79SJean-Baptiste Boric DPRINTF("ptype=%s size=%" PRIu64 "\n", dkw.dkw_ptype, dkw->dkw_size);
2029f988b79SJean-Baptiste Boric
2039f988b79SJean-Baptiste Boric return strcmp(dkw.dkw_ptype, DKW_PTYPE_V7) == 0 ? 0 : EINVAL;
2049f988b79SJean-Baptiste Boric }
2059f988b79SJean-Baptiste Boric
2069f988b79SJean-Baptiste Boric static int
v7fs_openfs(struct vnode * devvp,struct mount * mp,struct lwp * l)2079f988b79SJean-Baptiste Boric v7fs_openfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
2089f988b79SJean-Baptiste Boric {
2099f988b79SJean-Baptiste Boric kauth_cred_t cred = l->l_cred;
2109f988b79SJean-Baptiste Boric int oflags;
2119f988b79SJean-Baptiste Boric int error;
2129f988b79SJean-Baptiste Boric
2139f988b79SJean-Baptiste Boric /* Flush buffer */
2149f988b79SJean-Baptiste Boric vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
2159f988b79SJean-Baptiste Boric if ((error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0)))
2169f988b79SJean-Baptiste Boric goto unlock_exit;
2179f988b79SJean-Baptiste Boric
2189f988b79SJean-Baptiste Boric /* Open block device */
2199f988b79SJean-Baptiste Boric oflags = FREAD;
2209f988b79SJean-Baptiste Boric if ((mp->mnt_flag & MNT_RDONLY) == 0)
2219f988b79SJean-Baptiste Boric oflags |= FWRITE;
2229f988b79SJean-Baptiste Boric
2239f988b79SJean-Baptiste Boric if ((error = VOP_OPEN(devvp, oflags, NOCRED)) != 0) {
2249f988b79SJean-Baptiste Boric DPRINTF("VOP_OPEN=%d\n", error);
2259f988b79SJean-Baptiste Boric goto unlock_exit;
2269f988b79SJean-Baptiste Boric }
2279f988b79SJean-Baptiste Boric
2289f988b79SJean-Baptiste Boric return 0; /* lock held */
2299f988b79SJean-Baptiste Boric
2309f988b79SJean-Baptiste Boric unlock_exit:
2319f988b79SJean-Baptiste Boric VOP_UNLOCK(devvp);
2329f988b79SJean-Baptiste Boric
2339f988b79SJean-Baptiste Boric return error;
2349f988b79SJean-Baptiste Boric }
2359f988b79SJean-Baptiste Boric
2369f988b79SJean-Baptiste Boric static void
v7fs_closefs(struct vnode * devvp,struct mount * mp)2379f988b79SJean-Baptiste Boric v7fs_closefs(struct vnode *devvp, struct mount *mp)
2389f988b79SJean-Baptiste Boric {
2399f988b79SJean-Baptiste Boric int oflags = FREAD;
2409f988b79SJean-Baptiste Boric
2419f988b79SJean-Baptiste Boric if ((mp->mnt_flag & MNT_RDONLY) == 0)
2429f988b79SJean-Baptiste Boric oflags |= FWRITE;
2439f988b79SJean-Baptiste Boric
2449f988b79SJean-Baptiste Boric VOP_CLOSE(devvp, oflags, NOCRED);
2459f988b79SJean-Baptiste Boric }
2469f988b79SJean-Baptiste Boric
2479f988b79SJean-Baptiste Boric static int
v7fs_mountfs(struct vnode * devvp,struct mount * mp,int endian)2489f988b79SJean-Baptiste Boric v7fs_mountfs(struct vnode *devvp, struct mount *mp, int endian)
2499f988b79SJean-Baptiste Boric {
2509f988b79SJean-Baptiste Boric struct v7fs_mount *v7fsmount;
2519f988b79SJean-Baptiste Boric int error;
2529f988b79SJean-Baptiste Boric struct v7fs_mount_device mount;
2539f988b79SJean-Baptiste Boric
2549f988b79SJean-Baptiste Boric DPRINTF("%d\n",endian);
2559f988b79SJean-Baptiste Boric
2569f988b79SJean-Baptiste Boric v7fsmount = kmem_zalloc(sizeof(*v7fsmount), KM_SLEEP);
2579f988b79SJean-Baptiste Boric if (v7fsmount == NULL) {
2589f988b79SJean-Baptiste Boric return ENOMEM;
2599f988b79SJean-Baptiste Boric }
2609f988b79SJean-Baptiste Boric v7fsmount->devvp = devvp;
2619f988b79SJean-Baptiste Boric v7fsmount->mountp = mp;
2629f988b79SJean-Baptiste Boric
2639f988b79SJean-Baptiste Boric mount.device.vnode = devvp;
2649f988b79SJean-Baptiste Boric mount.endian = endian;
2659f988b79SJean-Baptiste Boric
2669f988b79SJean-Baptiste Boric if ((error = v7fs_io_init(&v7fsmount->core, &mount, V7FS_BSIZE))) {
2679f988b79SJean-Baptiste Boric goto err_exit;
2689f988b79SJean-Baptiste Boric }
2699f988b79SJean-Baptiste Boric struct v7fs_self *fs = v7fsmount->core;
2709f988b79SJean-Baptiste Boric
2719f988b79SJean-Baptiste Boric if ((error = v7fs_superblock_load(fs))) {
2729f988b79SJean-Baptiste Boric v7fs_io_fini(fs);
2739f988b79SJean-Baptiste Boric goto err_exit;
2749f988b79SJean-Baptiste Boric }
2759f988b79SJean-Baptiste Boric
2769f988b79SJean-Baptiste Boric mp->mnt_data = v7fsmount;
2779f988b79SJean-Baptiste Boric mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)devvp->v_rdev;
2789f988b79SJean-Baptiste Boric mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_V7FS);
2799f988b79SJean-Baptiste Boric mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
2809f988b79SJean-Baptiste Boric mp->mnt_stat.f_namemax = V7FS_NAME_MAX;
2819f988b79SJean-Baptiste Boric mp->mnt_flag |= MNT_LOCAL;
2829f988b79SJean-Baptiste Boric mp->mnt_dev_bshift = V7FS_BSHIFT;
2839f988b79SJean-Baptiste Boric mp->mnt_fs_bshift = V7FS_BSHIFT;
2849f988b79SJean-Baptiste Boric
2859f988b79SJean-Baptiste Boric return 0;
2869f988b79SJean-Baptiste Boric
2879f988b79SJean-Baptiste Boric err_exit:
2889f988b79SJean-Baptiste Boric kmem_free(v7fsmount, sizeof(*v7fsmount));
2899f988b79SJean-Baptiste Boric return error;
2909f988b79SJean-Baptiste Boric }
2919f988b79SJean-Baptiste Boric
2929f988b79SJean-Baptiste Boric int
v7fs_start(struct mount * mp,int flags)2939f988b79SJean-Baptiste Boric v7fs_start(struct mount *mp, int flags)
2949f988b79SJean-Baptiste Boric {
2959f988b79SJean-Baptiste Boric
2969f988b79SJean-Baptiste Boric DPRINTF("\n");
2979f988b79SJean-Baptiste Boric /* Nothing to do. */
2989f988b79SJean-Baptiste Boric return 0;
2999f988b79SJean-Baptiste Boric }
3009f988b79SJean-Baptiste Boric
3019f988b79SJean-Baptiste Boric int
v7fs_unmount(struct mount * mp,int mntflags)3029f988b79SJean-Baptiste Boric v7fs_unmount(struct mount *mp, int mntflags)
3039f988b79SJean-Baptiste Boric {
3049f988b79SJean-Baptiste Boric struct v7fs_mount *v7fsmount = (void *)mp->mnt_data;
3059f988b79SJean-Baptiste Boric int error;
3069f988b79SJean-Baptiste Boric
3079f988b79SJean-Baptiste Boric DPRINTF("%p\n", v7fsmount);
3089f988b79SJean-Baptiste Boric
3099f988b79SJean-Baptiste Boric if ((error = vflush(mp, NULLVP,
3109f988b79SJean-Baptiste Boric mntflags & MNT_FORCE ? FORCECLOSE : 0)) != 0)
3119f988b79SJean-Baptiste Boric return error;
3129f988b79SJean-Baptiste Boric
3139f988b79SJean-Baptiste Boric vn_lock(v7fsmount->devvp, LK_EXCLUSIVE | LK_RETRY);
3149f988b79SJean-Baptiste Boric error = VOP_CLOSE(v7fsmount->devvp, FREAD, NOCRED);
3159f988b79SJean-Baptiste Boric vput(v7fsmount->devvp);
3169f988b79SJean-Baptiste Boric
3179f988b79SJean-Baptiste Boric v7fs_io_fini(v7fsmount->core);
3189f988b79SJean-Baptiste Boric
3199f988b79SJean-Baptiste Boric kmem_free(v7fsmount, sizeof(*v7fsmount));
3209f988b79SJean-Baptiste Boric mp->mnt_data = NULL;
3219f988b79SJean-Baptiste Boric mp->mnt_flag &= ~MNT_LOCAL;
3229f988b79SJean-Baptiste Boric
3239f988b79SJean-Baptiste Boric return 0;
3249f988b79SJean-Baptiste Boric }
3259f988b79SJean-Baptiste Boric
3269f988b79SJean-Baptiste Boric int
v7fs_root(struct mount * mp,struct vnode ** vpp)3279f988b79SJean-Baptiste Boric v7fs_root(struct mount *mp, struct vnode **vpp)
3289f988b79SJean-Baptiste Boric {
3299f988b79SJean-Baptiste Boric struct vnode *vp;
3309f988b79SJean-Baptiste Boric int error;
3319f988b79SJean-Baptiste Boric
3329f988b79SJean-Baptiste Boric DPRINTF("\n");
3339f988b79SJean-Baptiste Boric if ((error = VFS_VGET(mp, V7FS_ROOT_INODE, &vp)) != 0) {
3349f988b79SJean-Baptiste Boric DPRINTF("error=%d\n", error);
3359f988b79SJean-Baptiste Boric return error;
3369f988b79SJean-Baptiste Boric }
3379f988b79SJean-Baptiste Boric *vpp = vp;
3389f988b79SJean-Baptiste Boric DPRINTF("done.\n");
3399f988b79SJean-Baptiste Boric
3409f988b79SJean-Baptiste Boric return 0;
3419f988b79SJean-Baptiste Boric }
3429f988b79SJean-Baptiste Boric
3439f988b79SJean-Baptiste Boric int
v7fs_statvfs(struct mount * mp,struct statvfs * f)3449f988b79SJean-Baptiste Boric v7fs_statvfs(struct mount *mp, struct statvfs *f)
3459f988b79SJean-Baptiste Boric {
3469f988b79SJean-Baptiste Boric struct v7fs_mount *v7fsmount = mp->mnt_data;
3479f988b79SJean-Baptiste Boric struct v7fs_self *fs = v7fsmount->core;
3489f988b79SJean-Baptiste Boric
3499f988b79SJean-Baptiste Boric DPRINTF("scratch remain=%d\n", fs->scratch_remain);
3509f988b79SJean-Baptiste Boric
3519f988b79SJean-Baptiste Boric v7fs_superblock_status(fs);
3529f988b79SJean-Baptiste Boric
3539f988b79SJean-Baptiste Boric f->f_bsize = V7FS_BSIZE;
3549f988b79SJean-Baptiste Boric f->f_frsize = V7FS_BSIZE;
3559f988b79SJean-Baptiste Boric f->f_iosize = V7FS_BSIZE;
3569f988b79SJean-Baptiste Boric f->f_blocks = fs->stat.total_blocks;
3579f988b79SJean-Baptiste Boric f->f_bfree = fs->stat.free_blocks;
3589f988b79SJean-Baptiste Boric f->f_bavail = fs->stat.free_blocks;
3599f988b79SJean-Baptiste Boric f->f_bresvd = 0;
3609f988b79SJean-Baptiste Boric f->f_files = fs->stat.total_files;
3619f988b79SJean-Baptiste Boric f->f_ffree = fs->stat.free_inode;
3629f988b79SJean-Baptiste Boric f->f_favail = f->f_ffree;
3639f988b79SJean-Baptiste Boric f->f_fresvd = 0;
3649f988b79SJean-Baptiste Boric copy_statvfs_info(f, mp);
3659f988b79SJean-Baptiste Boric
3669f988b79SJean-Baptiste Boric return 0;
3679f988b79SJean-Baptiste Boric }
3689f988b79SJean-Baptiste Boric
369*0a6a1f1dSLionel Sambuc static bool
v7fs_sync_selector(void * cl,struct vnode * vp)370*0a6a1f1dSLionel Sambuc v7fs_sync_selector(void *cl, struct vnode *vp)
371*0a6a1f1dSLionel Sambuc {
372*0a6a1f1dSLionel Sambuc struct v7fs_node *v7fs_node = vp->v_data;
373*0a6a1f1dSLionel Sambuc
374*0a6a1f1dSLionel Sambuc if (v7fs_node == NULL)
375*0a6a1f1dSLionel Sambuc return false;
376*0a6a1f1dSLionel Sambuc if (!v7fs_inode_allocated(&v7fs_node->inode))
377*0a6a1f1dSLionel Sambuc return false;
378*0a6a1f1dSLionel Sambuc
379*0a6a1f1dSLionel Sambuc return true;
380*0a6a1f1dSLionel Sambuc }
381*0a6a1f1dSLionel Sambuc
3829f988b79SJean-Baptiste Boric int
v7fs_sync(struct mount * mp,int waitfor,kauth_cred_t cred)3839f988b79SJean-Baptiste Boric v7fs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
3849f988b79SJean-Baptiste Boric {
3859f988b79SJean-Baptiste Boric struct v7fs_mount *v7fsmount = mp->mnt_data;
3869f988b79SJean-Baptiste Boric struct v7fs_self *fs = v7fsmount->core;
387*0a6a1f1dSLionel Sambuc struct vnode_iterator *marker;
388*0a6a1f1dSLionel Sambuc struct vnode *vp;
3899f988b79SJean-Baptiste Boric int err, error;
3909f988b79SJean-Baptiste Boric
3919f988b79SJean-Baptiste Boric DPRINTF("\n");
3929f988b79SJean-Baptiste Boric
3939f988b79SJean-Baptiste Boric v7fs_superblock_writeback(fs);
3949f988b79SJean-Baptiste Boric error = 0;
395*0a6a1f1dSLionel Sambuc vfs_vnode_iterator_init(mp, &marker);
396*0a6a1f1dSLionel Sambuc while ((vp = vfs_vnode_iterator_next(marker,
397*0a6a1f1dSLionel Sambuc v7fs_sync_selector, NULL)) != NULL) {
398*0a6a1f1dSLionel Sambuc err = vn_lock(vp, LK_EXCLUSIVE);
399*0a6a1f1dSLionel Sambuc if (err) {
400*0a6a1f1dSLionel Sambuc vrele(vp);
4019f988b79SJean-Baptiste Boric continue;
4029f988b79SJean-Baptiste Boric }
403*0a6a1f1dSLionel Sambuc err = VOP_FSYNC(vp, cred, FSYNC_WAIT, 0, 0);
404*0a6a1f1dSLionel Sambuc vput(vp);
4059f988b79SJean-Baptiste Boric if (err != 0)
4069f988b79SJean-Baptiste Boric error = err;
4079f988b79SJean-Baptiste Boric }
408*0a6a1f1dSLionel Sambuc vfs_vnode_iterator_destroy(marker);
4099f988b79SJean-Baptiste Boric
4109f988b79SJean-Baptiste Boric return error;
4119f988b79SJean-Baptiste Boric }
4129f988b79SJean-Baptiste Boric
4139f988b79SJean-Baptiste Boric static enum vtype
v7fs_mode_to_vtype(v7fs_mode_t mode)4149f988b79SJean-Baptiste Boric v7fs_mode_to_vtype (v7fs_mode_t mode)
4159f988b79SJean-Baptiste Boric {
4169f988b79SJean-Baptiste Boric enum vtype table[] = { VCHR, VDIR, VBLK, VREG, VLNK, VSOCK };
4179f988b79SJean-Baptiste Boric
4189f988b79SJean-Baptiste Boric if ((mode & V7FS_IFMT) == V7FSBSD_IFFIFO)
4199f988b79SJean-Baptiste Boric return VFIFO;
4209f988b79SJean-Baptiste Boric
4219f988b79SJean-Baptiste Boric return table[((mode >> 13) & 7) - 1];
4229f988b79SJean-Baptiste Boric }
4239f988b79SJean-Baptiste Boric
4249f988b79SJean-Baptiste Boric int
v7fs_loadvnode(struct mount * mp,struct vnode * vp,const void * key,size_t key_len,const void ** new_key)425*0a6a1f1dSLionel Sambuc v7fs_loadvnode(struct mount *mp, struct vnode *vp,
426*0a6a1f1dSLionel Sambuc const void *key, size_t key_len, const void **new_key)
4279f988b79SJean-Baptiste Boric {
428*0a6a1f1dSLionel Sambuc struct v7fs_mount *v7fsmount;
429*0a6a1f1dSLionel Sambuc struct v7fs_self *fs;
4309f988b79SJean-Baptiste Boric struct v7fs_node *v7fs_node;
4319f988b79SJean-Baptiste Boric struct v7fs_inode inode;
432*0a6a1f1dSLionel Sambuc v7fs_ino_t number;
4339f988b79SJean-Baptiste Boric int error;
4349f988b79SJean-Baptiste Boric
435*0a6a1f1dSLionel Sambuc KASSERT(key_len == sizeof(number));
436*0a6a1f1dSLionel Sambuc memcpy(&number, key, key_len);
437*0a6a1f1dSLionel Sambuc
438*0a6a1f1dSLionel Sambuc v7fsmount = mp->mnt_data;
439*0a6a1f1dSLionel Sambuc fs = v7fsmount->core;
440*0a6a1f1dSLionel Sambuc
4419f988b79SJean-Baptiste Boric /* Lookup requested i-node */
442*0a6a1f1dSLionel Sambuc if ((error = v7fs_inode_load(fs, &inode, number))) {
4439f988b79SJean-Baptiste Boric DPRINTF("v7fs_inode_load failed.\n");
4449f988b79SJean-Baptiste Boric return error;
4459f988b79SJean-Baptiste Boric }
4469f988b79SJean-Baptiste Boric
447*0a6a1f1dSLionel Sambuc v7fs_node = pool_get(&v7fs_node_pool, PR_WAITOK);
448*0a6a1f1dSLionel Sambuc memset(v7fs_node, 0, sizeof(*v7fs_node));
4499f988b79SJean-Baptiste Boric
450*0a6a1f1dSLionel Sambuc vp->v_tag = VT_V7FS;
451*0a6a1f1dSLionel Sambuc vp->v_data = v7fs_node;
4529f988b79SJean-Baptiste Boric v7fs_node->vnode = vp;
4539f988b79SJean-Baptiste Boric v7fs_node->v7fsmount = v7fsmount;
4549f988b79SJean-Baptiste Boric v7fs_node->inode = inode;/*structure copy */
4559f988b79SJean-Baptiste Boric v7fs_node->lockf = NULL; /* advlock */
4569f988b79SJean-Baptiste Boric
4579f988b79SJean-Baptiste Boric genfs_node_init(vp, &v7fs_genfsops);
4589f988b79SJean-Baptiste Boric uvm_vnp_setsize(vp, v7fs_inode_filesize(&inode));
4599f988b79SJean-Baptiste Boric
460*0a6a1f1dSLionel Sambuc if (number == V7FS_ROOT_INODE) {
4619f988b79SJean-Baptiste Boric vp->v_type = VDIR;
4629f988b79SJean-Baptiste Boric vp->v_vflag |= VV_ROOT;
463*0a6a1f1dSLionel Sambuc vp->v_op = v7fs_vnodeop_p;
4649f988b79SJean-Baptiste Boric } else {
4659f988b79SJean-Baptiste Boric vp->v_type = v7fs_mode_to_vtype(inode.mode);
4669f988b79SJean-Baptiste Boric
4679f988b79SJean-Baptiste Boric if (vp->v_type == VBLK || vp->v_type == VCHR) {
4689f988b79SJean-Baptiste Boric dev_t rdev = inode.device;
4699f988b79SJean-Baptiste Boric vp->v_op = v7fs_specop_p;
4709f988b79SJean-Baptiste Boric spec_node_init(vp, rdev);
4719f988b79SJean-Baptiste Boric } else if (vp->v_type == VFIFO) {
4729f988b79SJean-Baptiste Boric vp->v_op = v7fs_fifoop_p;
473*0a6a1f1dSLionel Sambuc } else {
474*0a6a1f1dSLionel Sambuc vp->v_op = v7fs_vnodeop_p;
4759f988b79SJean-Baptiste Boric }
4769f988b79SJean-Baptiste Boric }
4779f988b79SJean-Baptiste Boric
478*0a6a1f1dSLionel Sambuc *new_key = &v7fs_node->inode.inode_number;
479*0a6a1f1dSLionel Sambuc
480*0a6a1f1dSLionel Sambuc return 0;
481*0a6a1f1dSLionel Sambuc }
482*0a6a1f1dSLionel Sambuc
483*0a6a1f1dSLionel Sambuc
484*0a6a1f1dSLionel Sambuc int
v7fs_vget(struct mount * mp,ino_t ino,struct vnode ** vpp)485*0a6a1f1dSLionel Sambuc v7fs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
486*0a6a1f1dSLionel Sambuc {
487*0a6a1f1dSLionel Sambuc int error;
488*0a6a1f1dSLionel Sambuc v7fs_ino_t number;
489*0a6a1f1dSLionel Sambuc struct vnode *vp;
490*0a6a1f1dSLionel Sambuc
491*0a6a1f1dSLionel Sambuc KASSERT(ino <= UINT16_MAX);
492*0a6a1f1dSLionel Sambuc number = ino;
493*0a6a1f1dSLionel Sambuc
494*0a6a1f1dSLionel Sambuc error = vcache_get(mp, &number, sizeof(number), &vp);
495*0a6a1f1dSLionel Sambuc if (error)
496*0a6a1f1dSLionel Sambuc return error;
497*0a6a1f1dSLionel Sambuc error = vn_lock(vp, LK_EXCLUSIVE);
498*0a6a1f1dSLionel Sambuc if (error) {
499*0a6a1f1dSLionel Sambuc vrele(vp);
500*0a6a1f1dSLionel Sambuc return error;
501*0a6a1f1dSLionel Sambuc }
502*0a6a1f1dSLionel Sambuc
5039f988b79SJean-Baptiste Boric *vpp = vp;
5049f988b79SJean-Baptiste Boric
5059f988b79SJean-Baptiste Boric return 0;
5069f988b79SJean-Baptiste Boric }
5079f988b79SJean-Baptiste Boric
5089f988b79SJean-Baptiste Boric int
v7fs_fhtovp(struct mount * mp,struct fid * fid,struct vnode ** vpp)5099f988b79SJean-Baptiste Boric v7fs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
5109f988b79SJean-Baptiste Boric {
5119f988b79SJean-Baptiste Boric
5129f988b79SJean-Baptiste Boric DPRINTF("\n");
5139f988b79SJean-Baptiste Boric /* notyet */
5149f988b79SJean-Baptiste Boric return EOPNOTSUPP;
5159f988b79SJean-Baptiste Boric }
5169f988b79SJean-Baptiste Boric
5179f988b79SJean-Baptiste Boric int
v7fs_vptofh(struct vnode * vpp,struct fid * fid,size_t * fh_size)5189f988b79SJean-Baptiste Boric v7fs_vptofh(struct vnode *vpp, struct fid *fid, size_t *fh_size)
5199f988b79SJean-Baptiste Boric {
5209f988b79SJean-Baptiste Boric
5219f988b79SJean-Baptiste Boric DPRINTF("\n");
5229f988b79SJean-Baptiste Boric /* notyet */
5239f988b79SJean-Baptiste Boric return EOPNOTSUPP;
5249f988b79SJean-Baptiste Boric }
5259f988b79SJean-Baptiste Boric
5269f988b79SJean-Baptiste Boric void
v7fs_init(void)5279f988b79SJean-Baptiste Boric v7fs_init(void)
5289f988b79SJean-Baptiste Boric {
5299f988b79SJean-Baptiste Boric
5309f988b79SJean-Baptiste Boric DPRINTF("\n");
5319f988b79SJean-Baptiste Boric pool_init(&v7fs_node_pool, sizeof(struct v7fs_node), 0, 0, 0,
5329f988b79SJean-Baptiste Boric "v7fs_node_pool", &pool_allocator_nointr, IPL_NONE);
5339f988b79SJean-Baptiste Boric }
5349f988b79SJean-Baptiste Boric
5359f988b79SJean-Baptiste Boric void
v7fs_reinit(void)5369f988b79SJean-Baptiste Boric v7fs_reinit(void)
5379f988b79SJean-Baptiste Boric {
5389f988b79SJean-Baptiste Boric
5399f988b79SJean-Baptiste Boric /* Nothing to do. */
5409f988b79SJean-Baptiste Boric DPRINTF("\n");
5419f988b79SJean-Baptiste Boric }
5429f988b79SJean-Baptiste Boric
5439f988b79SJean-Baptiste Boric void
v7fs_done(void)5449f988b79SJean-Baptiste Boric v7fs_done(void)
5459f988b79SJean-Baptiste Boric {
5469f988b79SJean-Baptiste Boric
5479f988b79SJean-Baptiste Boric DPRINTF("\n");
5489f988b79SJean-Baptiste Boric pool_destroy(&v7fs_node_pool);
5499f988b79SJean-Baptiste Boric }
5509f988b79SJean-Baptiste Boric
5519f988b79SJean-Baptiste Boric int
v7fs_gop_alloc(struct vnode * vp,off_t off,off_t len,int flags,kauth_cred_t cred)5529f988b79SJean-Baptiste Boric v7fs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
5539f988b79SJean-Baptiste Boric kauth_cred_t cred)
5549f988b79SJean-Baptiste Boric {
5559f988b79SJean-Baptiste Boric
5569f988b79SJean-Baptiste Boric DPRINTF("\n");
5579f988b79SJean-Baptiste Boric return 0;
5589f988b79SJean-Baptiste Boric }
5599f988b79SJean-Baptiste Boric
5609f988b79SJean-Baptiste Boric int
v7fs_mountroot(void)5619f988b79SJean-Baptiste Boric v7fs_mountroot(void)
5629f988b79SJean-Baptiste Boric {
5639f988b79SJean-Baptiste Boric struct mount *mp;
5649f988b79SJean-Baptiste Boric int error;
5659f988b79SJean-Baptiste Boric
5669f988b79SJean-Baptiste Boric DPRINTF("");
5679f988b79SJean-Baptiste Boric /* On mountroot, devvp (rootdev) is opened by vfs_mountroot */
5689f988b79SJean-Baptiste Boric if ((error = is_v7fs_partition (rootvp)))
5699f988b79SJean-Baptiste Boric return error;
5709f988b79SJean-Baptiste Boric
5719f988b79SJean-Baptiste Boric if ((error = vfs_rootmountalloc(MOUNT_V7FS, "root_device", &mp))) {
5729f988b79SJean-Baptiste Boric DPRINTF("mountalloc error=%d\n", error);
5739f988b79SJean-Baptiste Boric vrele(rootvp);
5749f988b79SJean-Baptiste Boric return error;
5759f988b79SJean-Baptiste Boric }
5769f988b79SJean-Baptiste Boric
5779f988b79SJean-Baptiste Boric if ((error = v7fs_mountfs(rootvp, mp, _BYTE_ORDER))) {
5789f988b79SJean-Baptiste Boric DPRINTF("mountfs error=%d\n", error);
5799f988b79SJean-Baptiste Boric vfs_unbusy(mp, false, NULL);
5809f988b79SJean-Baptiste Boric vfs_destroy(mp);
5819f988b79SJean-Baptiste Boric return error;
5829f988b79SJean-Baptiste Boric }
5839f988b79SJean-Baptiste Boric
5849f988b79SJean-Baptiste Boric mountlist_append(mp);
5859f988b79SJean-Baptiste Boric
5869f988b79SJean-Baptiste Boric vfs_unbusy(mp, false, NULL);
5879f988b79SJean-Baptiste Boric
5889f988b79SJean-Baptiste Boric return 0;
5899f988b79SJean-Baptiste Boric }
590