Home | History | Annotate | Line # | Download | only in v7fs
v7fs_vfsops.c revision 1.10
      1  1.10      maxv /*	$NetBSD: v7fs_vfsops.c,v 1.10 2014/04/16 18:55:19 maxv Exp $	*/
      2   1.1       uch 
      3   1.1       uch /*-
      4   1.1       uch  * Copyright (c) 2004, 2011 The NetBSD Foundation, Inc.
      5   1.1       uch  * All rights reserved.
      6   1.1       uch  *
      7   1.1       uch  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1       uch  * by UCHIYAMA Yasushi.
      9   1.1       uch  *
     10   1.1       uch  * Redistribution and use in source and binary forms, with or without
     11   1.1       uch  * modification, are permitted provided that the following conditions
     12   1.1       uch  * are met:
     13   1.1       uch  * 1. Redistributions of source code must retain the above copyright
     14   1.1       uch  *    notice, this list of conditions and the following disclaimer.
     15   1.1       uch  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       uch  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       uch  *    documentation and/or other materials provided with the distribution.
     18   1.1       uch  *
     19   1.1       uch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1       uch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1       uch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1       uch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1       uch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1       uch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1       uch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1       uch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1       uch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1       uch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1       uch  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1       uch  */
     31   1.1       uch 
     32   1.1       uch #include <sys/cdefs.h>
     33  1.10      maxv __KERNEL_RCSID(0, "$NetBSD: v7fs_vfsops.c,v 1.10 2014/04/16 18:55:19 maxv Exp $");
     34   1.1       uch #if defined _KERNEL_OPT
     35   1.1       uch #include "opt_v7fs.h"
     36   1.1       uch #endif
     37   1.1       uch 
     38   1.1       uch #include <sys/types.h>
     39   1.1       uch #include <sys/param.h>
     40   1.1       uch #include <sys/systm.h>
     41   1.1       uch #include <sys/pool.h>
     42   1.1       uch #include <sys/time.h>
     43   1.1       uch #include <sys/ucred.h>
     44   1.1       uch #include <sys/mount.h>
     45   1.5  christos #include <sys/disk.h>
     46   1.5  christos #include <sys/device.h>
     47   1.1       uch #include <sys/fcntl.h>
     48   1.8     rmind #include <sys/kmem.h>
     49   1.1       uch #include <sys/kauth.h>
     50   1.1       uch #include <sys/proc.h>
     51   1.1       uch 
     52   1.1       uch /* v-node */
     53   1.1       uch #include <sys/namei.h>
     54   1.1       uch #include <sys/vnode.h>
     55   1.1       uch /* devsw */
     56   1.1       uch #include <sys/conf.h>
     57   1.1       uch 
     58   1.1       uch #include "v7fs_extern.h"
     59   1.1       uch #include "v7fs.h"
     60   1.1       uch #include "v7fs_impl.h"
     61   1.1       uch #include "v7fs_inode.h"
     62   1.1       uch #include "v7fs_superblock.h"
     63   1.1       uch 
     64   1.1       uch #ifdef V7FS_VFSOPS_DEBUG
     65   1.1       uch #define	DPRINTF(fmt, args...)	printf("%s: " fmt, __func__, ##args)
     66   1.1       uch #else
     67   1.1       uch #define	DPRINTF(arg...)		((void)0)
     68   1.1       uch #endif
     69   1.1       uch 
     70   1.1       uch struct pool v7fs_node_pool;
     71   1.1       uch 
     72   1.1       uch static int v7fs_mountfs(struct vnode *, struct mount *, int);
     73   1.1       uch static int v7fs_openfs(struct vnode *, struct mount *, struct lwp *);
     74   1.1       uch static void v7fs_closefs(struct vnode *, struct mount *);
     75   1.2       uch static int is_v7fs_partition(struct vnode *);
     76   1.1       uch static enum vtype v7fs_mode_to_vtype(v7fs_mode_t mode);
     77   1.4       uch int v7fs_vnode_reload(struct mount *, struct vnode *);
     78   1.1       uch 
     79   1.1       uch int
     80   1.1       uch v7fs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
     81   1.1       uch {
     82   1.1       uch 	struct lwp *l = curlwp;
     83   1.1       uch 	struct v7fs_args *args = data;
     84   1.1       uch 	struct v7fs_mount *v7fsmount = (void *)mp->mnt_data;
     85   1.1       uch 	struct vnode *devvp = NULL;
     86   1.7     joerg 	int error = 0;
     87   1.1       uch 	bool update = mp->mnt_flag & MNT_UPDATE;
     88   1.1       uch 
     89   1.1       uch 	DPRINTF("mnt_flag=%x %s\n", mp->mnt_flag, update ? "update" : "");
     90   1.1       uch 
     91  1.10      maxv 	if (args == NULL)
     92  1.10      maxv 		return EINVAL;
     93   1.1       uch 	if (*data_len < sizeof(*args))
     94   1.1       uch 		return EINVAL;
     95   1.1       uch 
     96   1.1       uch 	if (mp->mnt_flag & MNT_GETARGS) {
     97   1.1       uch 		if (!v7fsmount)
     98   1.1       uch 			return EIO;
     99   1.1       uch 		args->fspec = NULL;
    100   1.1       uch 		args->endian = v7fsmount->core->endian;
    101   1.1       uch 		*data_len = sizeof(*args);
    102   1.1       uch 		return 0;
    103   1.1       uch 	}
    104   1.1       uch 
    105   1.1       uch 	DPRINTF("args->fspec=%s endian=%d\n", args->fspec, args->endian);
    106   1.1       uch 	if (args->fspec == NULL) {
    107   1.1       uch 		/* nothing to do. */
    108   1.1       uch 		return EINVAL;
    109   1.1       uch 	}
    110   1.1       uch 
    111   1.1       uch 	if (args->fspec != NULL) {
    112   1.1       uch 		/* Look up the name and verify that it's sane. */
    113   1.1       uch 		error = namei_simple_user(args->fspec,
    114   1.1       uch 		    NSM_FOLLOW_NOEMULROOT, &devvp);
    115   1.1       uch 		if (error != 0)
    116   1.1       uch 			return (error);
    117   1.1       uch 		DPRINTF("mount device=%lx\n", (long)devvp->v_rdev);
    118   1.1       uch 
    119   1.1       uch 		if (!update) {
    120   1.1       uch 			/*
    121   1.1       uch 			 * Be sure this is a valid block device
    122   1.1       uch 			 */
    123   1.1       uch 			if (devvp->v_type != VBLK)
    124   1.1       uch 				error = ENOTBLK;
    125   1.1       uch 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
    126   1.1       uch 				error = ENXIO;
    127   1.1       uch 		} else {
    128   1.1       uch 			KDASSERT(v7fsmount);
    129   1.1       uch 			/*
    130   1.1       uch 			 * Be sure we're still naming the same device
    131   1.1       uch 			 * used for our initial mount
    132   1.1       uch 			 */
    133   1.1       uch 			if (devvp != v7fsmount->devvp) {
    134   1.1       uch 				DPRINTF("devvp %p != %p rootvp=%p\n", devvp,
    135   1.1       uch 				    v7fsmount->devvp, rootvp);
    136   1.1       uch 				if (rootvp == v7fsmount->devvp) {
    137   1.1       uch 					vrele(devvp);
    138   1.1       uch 					devvp = rootvp;
    139   1.1       uch 					vref(devvp);
    140   1.1       uch 				} else {
    141   1.1       uch 					error = EINVAL;
    142   1.1       uch 				}
    143   1.1       uch 			}
    144   1.1       uch 		}
    145   1.1       uch 	}
    146   1.1       uch 
    147   1.1       uch 	/*
    148   1.1       uch 	 * If mount by non-root, then verify that user has necessary
    149   1.1       uch 	 * permissions on the device.
    150   1.1       uch 	 *
    151   1.1       uch 	 * Permission to update a mount is checked higher, so here we presume
    152   1.1       uch 	 * updating the mount is okay (for example, as far as securelevel goes)
    153   1.1       uch 	 * which leaves us with the normal check.
    154   1.1       uch 	 */
    155   1.1       uch 	if (error == 0) {
    156   1.1       uch 		int accessmode = VREAD;
    157   1.1       uch 		if (update ?
    158   1.1       uch 		    (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
    159   1.1       uch 		    (mp->mnt_flag & MNT_RDONLY) == 0)
    160   1.1       uch 			accessmode |= VWRITE;
    161   1.6      elad 		error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    162   1.6      elad 		    KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp,
    163   1.6      elad 		    KAUTH_ARG(accessmode));
    164   1.1       uch 	}
    165   1.1       uch 
    166   1.1       uch 	if (error) {
    167   1.1       uch 		vrele(devvp);
    168   1.1       uch 		return error;
    169   1.1       uch 	}
    170   1.1       uch 
    171   1.1       uch 	if (!update) {
    172   1.1       uch 		if ((error = v7fs_openfs(devvp, mp, l))) {
    173   1.1       uch 			vrele(devvp);
    174   1.1       uch 			return error;
    175   1.1       uch 		}
    176   1.1       uch 
    177   1.1       uch 		if ((error = v7fs_mountfs(devvp, mp, args->endian))) {
    178   1.1       uch 			v7fs_closefs(devvp, mp);
    179   1.1       uch 			VOP_UNLOCK(devvp);
    180   1.1       uch 			vrele(devvp);
    181   1.1       uch 			return error;
    182   1.1       uch 		}
    183   1.1       uch 		VOP_UNLOCK(devvp);
    184   1.1       uch 	} else 	if (mp->mnt_flag & MNT_RDONLY) {
    185   1.1       uch 		/* XXX: r/w -> read only */
    186   1.1       uch 	}
    187   1.1       uch 
    188   1.1       uch 	return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
    189   1.1       uch 	    mp->mnt_op->vfs_name, mp, l);
    190   1.1       uch }
    191   1.1       uch 
    192   1.2       uch static int
    193   1.1       uch is_v7fs_partition(struct vnode *devvp)
    194   1.1       uch {
    195   1.5  christos 	struct dkwedge_info dkw;
    196   1.1       uch 	int error;
    197   1.1       uch 
    198   1.5  christos 	if ((error = getdiskinfo(devvp, &dkw)) != 0) {
    199   1.5  christos 		DPRINTF("getdiskinfo=%d\n", error);
    200   1.2       uch 		return error;
    201   1.1       uch 	}
    202   1.5  christos 	DPRINTF("ptype=%s size=%" PRIu64 "\n", dkw.dkw_ptype, dkw->dkw_size);
    203   1.1       uch 
    204   1.5  christos 	return strcmp(dkw.dkw_ptype, DKW_PTYPE_V7) == 0 ? 0 : EINVAL;
    205   1.1       uch }
    206   1.1       uch 
    207   1.1       uch static int
    208   1.1       uch v7fs_openfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
    209   1.1       uch {
    210   1.1       uch 	kauth_cred_t cred = l->l_cred;
    211   1.1       uch 	int oflags;
    212   1.1       uch 	int error;
    213   1.1       uch 
    214   1.1       uch 	/* Flush buffer */
    215   1.1       uch 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    216   1.1       uch 	if ((error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0)))
    217   1.1       uch 		goto unlock_exit;
    218   1.1       uch 
    219   1.1       uch 	/* Open block device */
    220   1.1       uch 	oflags = FREAD;
    221   1.1       uch 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
    222   1.1       uch 		oflags |= FWRITE;
    223   1.1       uch 
    224   1.1       uch 	if ((error = VOP_OPEN(devvp, oflags, NOCRED)) != 0) {
    225   1.1       uch 		DPRINTF("VOP_OPEN=%d\n", error);
    226   1.1       uch 		goto unlock_exit;
    227   1.1       uch 	}
    228   1.1       uch 
    229   1.1       uch 	return 0; /* lock held */
    230   1.1       uch 
    231   1.1       uch unlock_exit:
    232   1.1       uch 	VOP_UNLOCK(devvp);
    233   1.1       uch 
    234   1.1       uch 	return error;
    235   1.1       uch }
    236   1.1       uch 
    237   1.1       uch static void
    238   1.1       uch v7fs_closefs(struct vnode *devvp, struct mount *mp)
    239   1.1       uch {
    240   1.1       uch 	int oflags = FREAD;
    241   1.1       uch 
    242   1.1       uch 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
    243   1.1       uch 		oflags |= FWRITE;
    244   1.1       uch 
    245   1.1       uch 	VOP_CLOSE(devvp, oflags, NOCRED);
    246   1.1       uch }
    247   1.1       uch 
    248   1.1       uch static int
    249   1.1       uch v7fs_mountfs(struct vnode *devvp, struct mount *mp, int endian)
    250   1.1       uch {
    251   1.1       uch 	struct v7fs_mount *v7fsmount;
    252   1.1       uch 	int error;
    253   1.1       uch 	struct v7fs_mount_device mount;
    254   1.1       uch 
    255   1.1       uch 	DPRINTF("%d\n",endian);
    256   1.1       uch 
    257   1.8     rmind 	v7fsmount = kmem_zalloc(sizeof(*v7fsmount), KM_SLEEP);
    258   1.1       uch 	if (v7fsmount == NULL) {
    259   1.1       uch 		return ENOMEM;
    260   1.1       uch 	}
    261   1.1       uch 	v7fsmount->devvp = devvp;
    262   1.1       uch 	v7fsmount->mountp = mp;
    263   1.1       uch 
    264   1.1       uch 	mount.device.vnode = devvp;
    265   1.1       uch 	mount.endian = endian;
    266   1.1       uch 
    267   1.1       uch 	if ((error = v7fs_io_init(&v7fsmount->core, &mount, V7FS_BSIZE))) {
    268   1.1       uch 		goto err_exit;
    269   1.1       uch 	}
    270   1.1       uch 	struct v7fs_self *fs = v7fsmount->core;
    271   1.1       uch 
    272   1.1       uch 	if ((error = v7fs_superblock_load(fs))) {
    273   1.1       uch 		v7fs_io_fini(fs);
    274   1.1       uch 		goto err_exit;
    275   1.1       uch 	}
    276   1.1       uch 
    277   1.1       uch 	LIST_INIT(&v7fsmount->v7fs_node_head);
    278   1.1       uch 
    279   1.1       uch 	mp->mnt_data = v7fsmount;
    280   1.1       uch 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)devvp->v_rdev;
    281   1.1       uch 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_V7FS);
    282   1.1       uch 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    283   1.1       uch 	mp->mnt_stat.f_namemax = V7FS_NAME_MAX;
    284   1.1       uch 	mp->mnt_flag |= MNT_LOCAL;
    285   1.1       uch 	mp->mnt_dev_bshift = V7FS_BSHIFT;
    286   1.1       uch 	mp->mnt_fs_bshift = V7FS_BSHIFT;
    287   1.1       uch 
    288   1.1       uch 	return 0;
    289   1.1       uch 
    290   1.1       uch err_exit:
    291   1.8     rmind 	kmem_free(v7fsmount, sizeof(*v7fsmount));
    292   1.1       uch 	return error;
    293   1.1       uch }
    294   1.1       uch 
    295   1.1       uch int
    296   1.1       uch v7fs_start(struct mount *mp, int flags)
    297   1.1       uch {
    298   1.1       uch 
    299   1.1       uch 	DPRINTF("\n");
    300   1.1       uch 	/* Nothing to do. */
    301   1.1       uch 	return 0;
    302   1.1       uch }
    303   1.1       uch 
    304   1.1       uch int
    305   1.1       uch v7fs_unmount(struct mount *mp, int mntflags)
    306   1.1       uch {
    307   1.1       uch 	struct v7fs_mount *v7fsmount = (void *)mp->mnt_data;
    308   1.1       uch 	int error;
    309   1.1       uch 
    310   1.1       uch 	DPRINTF("%p\n", v7fsmount);
    311   1.1       uch 
    312   1.1       uch 	if ((error = vflush(mp, NULLVP,
    313   1.1       uch 		    mntflags & MNT_FORCE ? FORCECLOSE : 0)) != 0)
    314   1.1       uch 		return error;
    315   1.1       uch 
    316   1.1       uch 	vn_lock(v7fsmount->devvp, LK_EXCLUSIVE | LK_RETRY);
    317   1.1       uch 	error = VOP_CLOSE(v7fsmount->devvp, FREAD, NOCRED);
    318   1.1       uch 	vput(v7fsmount->devvp);
    319   1.1       uch 
    320   1.1       uch 	v7fs_io_fini(v7fsmount->core);
    321   1.1       uch 
    322   1.8     rmind 	kmem_free(v7fsmount, sizeof(*v7fsmount));
    323   1.1       uch 	mp->mnt_data = NULL;
    324   1.1       uch 	mp->mnt_flag &= ~MNT_LOCAL;
    325   1.1       uch 
    326   1.1       uch 	return 0;
    327   1.1       uch }
    328   1.1       uch 
    329   1.1       uch int
    330   1.1       uch v7fs_root(struct mount *mp, struct vnode **vpp)
    331   1.1       uch {
    332   1.1       uch 	struct vnode *vp;
    333   1.1       uch 	int error;
    334   1.1       uch 
    335   1.1       uch 	DPRINTF("\n");
    336   1.1       uch 	if ((error = VFS_VGET(mp, V7FS_ROOT_INODE, &vp)) != 0) {
    337   1.1       uch 		DPRINTF("error=%d\n", error);
    338   1.1       uch 		return error;
    339   1.1       uch 	}
    340   1.1       uch 	*vpp = vp;
    341   1.1       uch 	DPRINTF("done.\n");
    342   1.1       uch 
    343   1.1       uch 	return 0;
    344   1.1       uch }
    345   1.1       uch 
    346   1.1       uch int
    347   1.1       uch v7fs_statvfs(struct mount *mp, struct statvfs *f)
    348   1.1       uch {
    349   1.1       uch 	struct v7fs_mount *v7fsmount = mp->mnt_data;
    350   1.1       uch 	struct v7fs_self *fs = v7fsmount->core;
    351   1.1       uch 
    352   1.1       uch 	DPRINTF("scratch remain=%d\n", fs->scratch_remain);
    353   1.1       uch 
    354   1.1       uch 	v7fs_superblock_status(fs);
    355   1.1       uch 
    356   1.1       uch 	f->f_bsize = V7FS_BSIZE;
    357   1.1       uch 	f->f_frsize = V7FS_BSIZE;
    358   1.1       uch 	f->f_iosize = V7FS_BSIZE;
    359   1.1       uch 	f->f_blocks = fs->stat.total_blocks;
    360   1.1       uch 	f->f_bfree = fs->stat.free_blocks;
    361   1.1       uch 	f->f_bavail = fs->stat.free_blocks;
    362   1.1       uch 	f->f_bresvd = 0;
    363   1.1       uch 	f->f_files = fs->stat.total_files;
    364   1.1       uch 	f->f_ffree = fs->stat.free_inode;
    365   1.1       uch 	f->f_favail = f->f_ffree;
    366   1.1       uch 	f->f_fresvd = 0;
    367   1.1       uch 	copy_statvfs_info(f, mp);
    368   1.1       uch 
    369   1.1       uch 	return 0;
    370   1.1       uch }
    371   1.1       uch 
    372   1.1       uch int
    373   1.1       uch v7fs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
    374   1.1       uch {
    375   1.1       uch 	struct v7fs_mount *v7fsmount = mp->mnt_data;
    376   1.1       uch 	struct v7fs_self *fs = v7fsmount->core;
    377   1.1       uch 	struct v7fs_node *v7fs_node;
    378   1.1       uch 	struct v7fs_inode *inode;
    379   1.1       uch 	struct vnode *v;
    380   1.1       uch 	int err, error;
    381   1.1       uch 	int retry_cnt;
    382   1.1       uch 
    383   1.1       uch 	DPRINTF("\n");
    384   1.1       uch 
    385   1.1       uch 	v7fs_superblock_writeback(fs);
    386   1.1       uch 	for (retry_cnt = 0; retry_cnt < 2; retry_cnt++) {
    387   1.1       uch 		error = 0;
    388   1.1       uch 
    389   1.1       uch 		mutex_enter(&mntvnode_lock);
    390   1.1       uch 		for (v7fs_node = LIST_FIRST(&v7fsmount->v7fs_node_head);
    391   1.1       uch 		    v7fs_node != NULL; v7fs_node = LIST_NEXT(v7fs_node, link)) {
    392   1.1       uch 			inode = &v7fs_node->inode;
    393   1.1       uch 			if (!v7fs_inode_allocated(inode)) {
    394   1.1       uch 				continue;
    395   1.1       uch 			}
    396   1.1       uch 			v = v7fs_node->vnode;
    397   1.1       uch 			mutex_enter(v->v_interlock);
    398   1.1       uch 			mutex_exit(&mntvnode_lock);
    399   1.1       uch 			err = vget(v, LK_EXCLUSIVE | LK_NOWAIT);
    400   1.1       uch 			if (err == 0) {
    401   1.1       uch 				err = VOP_FSYNC(v, cred, FSYNC_WAIT, 0, 0);
    402   1.1       uch 				vput(v);
    403   1.1       uch 			}
    404   1.1       uch 			if (err != 0)
    405   1.1       uch 				error = err;
    406   1.1       uch 			mutex_enter(&mntvnode_lock);
    407   1.1       uch 		}
    408   1.1       uch 		mutex_exit(&mntvnode_lock);
    409   1.1       uch 
    410   1.1       uch 		if (error == 0)
    411   1.1       uch 			break;
    412   1.1       uch 	}
    413   1.1       uch 
    414   1.1       uch 	return error;
    415   1.1       uch }
    416   1.1       uch 
    417   1.1       uch static enum vtype
    418   1.1       uch v7fs_mode_to_vtype (v7fs_mode_t mode)
    419   1.1       uch {
    420   1.1       uch 	enum vtype table[] = { VCHR, VDIR, VBLK, VREG, VLNK, VSOCK };
    421   1.1       uch 
    422   1.1       uch 	if ((mode & V7FS_IFMT) == V7FSBSD_IFFIFO)
    423   1.1       uch 		return VFIFO;
    424   1.1       uch 
    425   1.1       uch 	return table[((mode >> 13) & 7) - 1];
    426   1.1       uch }
    427   1.1       uch 
    428   1.1       uch int
    429   1.1       uch v7fs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
    430   1.1       uch {
    431   1.1       uch 	struct v7fs_mount *v7fsmount = mp->mnt_data;
    432   1.1       uch 	struct v7fs_self *fs = v7fsmount->core;
    433   1.1       uch 	struct vnode *vp;
    434   1.1       uch 	struct v7fs_node *v7fs_node;
    435   1.1       uch 	struct v7fs_inode inode;
    436   1.1       uch 	int error;
    437   1.1       uch 
    438   1.1       uch 	/* Lookup requested i-node */
    439   1.1       uch 	if ((error = v7fs_inode_load(fs, &inode, ino))) {
    440   1.1       uch 		DPRINTF("v7fs_inode_load failed.\n");
    441   1.1       uch 		return error;
    442   1.1       uch 	}
    443   1.1       uch 
    444   1.1       uch retry:
    445   1.1       uch 	mutex_enter(&mntvnode_lock);
    446   1.1       uch 	for (v7fs_node = LIST_FIRST(&v7fsmount->v7fs_node_head);
    447   1.1       uch 	    v7fs_node != NULL; v7fs_node = LIST_NEXT(v7fs_node, link)) {
    448   1.1       uch 		if (v7fs_node->inode.inode_number == ino) {
    449   1.1       uch 			vp = v7fs_node->vnode;
    450   1.1       uch 			mutex_enter(vp->v_interlock);
    451   1.1       uch 			mutex_exit(&mntvnode_lock);
    452   1.1       uch 			if (vget(vp, LK_EXCLUSIVE) == 0) {
    453   1.1       uch 				*vpp = vp;
    454   1.1       uch 				return 0;
    455   1.1       uch 			} else {
    456   1.1       uch 				DPRINTF("retry!\n");
    457   1.1       uch 				goto retry;
    458   1.1       uch 			}
    459   1.1       uch 		}
    460   1.1       uch 	}
    461   1.1       uch 	mutex_exit(&mntvnode_lock);
    462   1.1       uch 
    463   1.1       uch 	/* Allocate v-node. */
    464   1.1       uch 	if ((error = getnewvnode(VT_V7FS, mp, v7fs_vnodeop_p, NULL, &vp))) {
    465   1.1       uch 		DPRINTF("getnewvnode error.\n");
    466   1.1       uch 		return error;
    467   1.1       uch 	}
    468   1.1       uch 	/* Lock vnode here */
    469   1.1       uch 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    470   1.1       uch 
    471   1.1       uch 	/* Allocate i-node */
    472   1.1       uch 	vp->v_data = pool_get(&v7fs_node_pool, PR_WAITOK);
    473   1.1       uch 	memset(vp->v_data, 0, sizeof(*v7fs_node));
    474   1.1       uch 	v7fs_node = vp->v_data;
    475   1.1       uch 	mutex_enter(&mntvnode_lock);
    476   1.1       uch 	LIST_INSERT_HEAD(&v7fsmount->v7fs_node_head, v7fs_node, link);
    477   1.1       uch 	mutex_exit(&mntvnode_lock);
    478   1.1       uch 	v7fs_node->vnode = vp;
    479   1.1       uch 	v7fs_node->v7fsmount = v7fsmount;
    480   1.1       uch 	v7fs_node->inode = inode;/*structure copy */
    481   1.1       uch 	v7fs_node->lockf = NULL; /* advlock */
    482   1.1       uch 
    483   1.1       uch 	genfs_node_init(vp, &v7fs_genfsops);
    484   1.1       uch 	uvm_vnp_setsize(vp, v7fs_inode_filesize(&inode));
    485   1.1       uch 
    486   1.1       uch 	if (ino == V7FS_ROOT_INODE) {
    487   1.1       uch 		vp->v_type = VDIR;
    488   1.1       uch 		vp->v_vflag |= VV_ROOT;
    489   1.1       uch 	} else {
    490   1.1       uch 		vp->v_type = v7fs_mode_to_vtype(inode.mode);
    491   1.1       uch 
    492   1.1       uch 		if (vp->v_type == VBLK || vp->v_type == VCHR) {
    493   1.1       uch 			dev_t rdev = inode.device;
    494   1.1       uch 			vp->v_op = v7fs_specop_p;
    495   1.1       uch 			spec_node_init(vp, rdev);
    496   1.1       uch 		} else if (vp->v_type == VFIFO) {
    497   1.1       uch 			vp->v_op = v7fs_fifoop_p;
    498   1.1       uch 		}
    499   1.1       uch 	}
    500   1.1       uch 
    501   1.1       uch 	*vpp = vp;
    502   1.1       uch 
    503   1.1       uch 	return 0;
    504   1.1       uch }
    505   1.1       uch 
    506   1.1       uch 
    507   1.1       uch int
    508   1.1       uch v7fs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
    509   1.1       uch {
    510   1.1       uch 
    511   1.1       uch 	DPRINTF("\n");
    512   1.1       uch 	/* notyet */
    513   1.1       uch 	return EOPNOTSUPP;
    514   1.1       uch }
    515   1.1       uch 
    516   1.1       uch int
    517   1.1       uch v7fs_vptofh(struct vnode *vpp, struct fid *fid, size_t *fh_size)
    518   1.1       uch {
    519   1.1       uch 
    520   1.1       uch 	DPRINTF("\n");
    521   1.1       uch 	/* notyet */
    522   1.1       uch 	return EOPNOTSUPP;
    523   1.1       uch }
    524   1.1       uch 
    525   1.1       uch void
    526   1.1       uch v7fs_init(void)
    527   1.1       uch {
    528   1.1       uch 
    529   1.1       uch 	DPRINTF("\n");
    530   1.1       uch 	pool_init(&v7fs_node_pool, sizeof(struct v7fs_node), 0, 0, 0,
    531   1.1       uch 	    "v7fs_node_pool", &pool_allocator_nointr, IPL_NONE);
    532   1.1       uch }
    533   1.1       uch 
    534   1.1       uch void
    535   1.1       uch v7fs_reinit(void)
    536   1.1       uch {
    537   1.1       uch 
    538   1.1       uch 	/* Nothing to do. */
    539   1.1       uch 	DPRINTF("\n");
    540   1.1       uch }
    541   1.1       uch 
    542   1.1       uch void
    543   1.1       uch v7fs_done(void)
    544   1.1       uch {
    545   1.1       uch 
    546   1.1       uch 	DPRINTF("\n");
    547   1.1       uch 	pool_destroy(&v7fs_node_pool);
    548   1.1       uch }
    549   1.1       uch 
    550   1.1       uch int
    551   1.1       uch v7fs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
    552   1.1       uch     kauth_cred_t cred)
    553   1.1       uch {
    554   1.1       uch 
    555   1.1       uch 	DPRINTF("\n");
    556   1.1       uch 	return 0;
    557   1.1       uch }
    558   1.1       uch 
    559   1.1       uch int
    560   1.1       uch v7fs_mountroot(void)
    561   1.1       uch {
    562   1.1       uch 	struct mount *mp;
    563   1.1       uch 	int error;
    564   1.1       uch 
    565   1.1       uch 	DPRINTF("");
    566   1.1       uch 	/* On mountroot, devvp (rootdev) is opened by vfs_mountroot */
    567   1.2       uch 	if ((error = is_v7fs_partition (rootvp)))
    568   1.2       uch 		return error;
    569   1.1       uch 
    570   1.1       uch 	if ((error = vfs_rootmountalloc(MOUNT_V7FS, "root_device", &mp))) {
    571   1.1       uch 		DPRINTF("mountalloc error=%d\n", error);
    572   1.1       uch 		vrele(rootvp);
    573   1.1       uch 		return error;
    574   1.1       uch 	}
    575   1.1       uch 
    576   1.1       uch 	if ((error = v7fs_mountfs(rootvp, mp, _BYTE_ORDER))) {
    577   1.1       uch 		DPRINTF("mountfs error=%d\n", error);
    578   1.1       uch 		vfs_unbusy(mp, false, NULL);
    579   1.1       uch 		vfs_destroy(mp);
    580   1.1       uch 		return error;
    581   1.1       uch 	}
    582   1.1       uch 
    583   1.9  christos 	mountlist_append(mp);
    584   1.1       uch 
    585   1.1       uch 	vfs_unbusy(mp, false, NULL);
    586   1.1       uch 
    587   1.1       uch 	return 0;
    588   1.1       uch }
    589   1.4       uch 
    590   1.4       uch /* Reload disk inode information */
    591   1.4       uch int
    592   1.4       uch v7fs_vnode_reload(struct mount *mp, struct vnode *vp)
    593   1.4       uch {
    594   1.4       uch 	struct v7fs_mount *v7fsmount = mp->mnt_data;
    595   1.4       uch 	struct v7fs_self *fs = v7fsmount->core;
    596   1.4       uch 	struct v7fs_node *v7fs_node;
    597   1.4       uch 	struct v7fs_inode *inode = &((struct v7fs_node *)vp->v_data)->inode;
    598   1.4       uch 	int target_ino = inode->inode_number;
    599   1.4       uch 	int error = 0;
    600   1.4       uch 
    601   1.4       uch 	DPRINTF("#%d\n", target_ino);
    602   1.4       uch 	mutex_enter(&mntvnode_lock);
    603   1.4       uch 	for (v7fs_node = LIST_FIRST(&v7fsmount->v7fs_node_head);
    604   1.4       uch 	     v7fs_node != NULL; v7fs_node = LIST_NEXT(v7fs_node, link)) {
    605   1.4       uch 		inode = &v7fs_node->inode;
    606   1.4       uch 		if (!v7fs_inode_allocated(inode)) {
    607   1.4       uch 			continue;
    608   1.4       uch 		}
    609   1.4       uch 		if (inode->inode_number == target_ino) {
    610   1.4       uch 			error = v7fs_inode_load(fs, &v7fs_node->inode,
    611   1.4       uch 			    target_ino);
    612   1.4       uch 			DPRINTF("sync #%d error=%d\n", target_ino, error);
    613   1.4       uch 			break;
    614   1.4       uch 		}
    615   1.4       uch 	}
    616   1.4       uch 	mutex_exit(&mntvnode_lock);
    617   1.4       uch 
    618   1.4       uch 	return error;
    619   1.4       uch }
    620