Home | History | Annotate | Line # | Download | only in rumpvfs
rump_vfs.c revision 1.20.2.5
      1  1.20.2.5  yamt /*	$NetBSD: rump_vfs.c,v 1.20.2.5 2009/07/18 14:53:26 yamt Exp $	*/
      2  1.20.2.2  yamt 
      3  1.20.2.2  yamt /*
      4  1.20.2.2  yamt  * Copyright (c) 2008 Antti Kantee.  All Rights Reserved.
      5  1.20.2.2  yamt  *
      6  1.20.2.2  yamt  * Development of this software was supported by the
      7  1.20.2.2  yamt  * Finnish Cultural Foundation.
      8  1.20.2.2  yamt  *
      9  1.20.2.2  yamt  * Redistribution and use in source and binary forms, with or without
     10  1.20.2.2  yamt  * modification, are permitted provided that the following conditions
     11  1.20.2.2  yamt  * are met:
     12  1.20.2.2  yamt  * 1. Redistributions of source code must retain the above copyright
     13  1.20.2.2  yamt  *    notice, this list of conditions and the following disclaimer.
     14  1.20.2.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.20.2.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     16  1.20.2.2  yamt  *    documentation and/or other materials provided with the distribution.
     17  1.20.2.2  yamt  *
     18  1.20.2.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19  1.20.2.2  yamt  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  1.20.2.2  yamt  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  1.20.2.2  yamt  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  1.20.2.2  yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  1.20.2.2  yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  1.20.2.2  yamt  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  1.20.2.2  yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  1.20.2.2  yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.20.2.2  yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.20.2.2  yamt  * SUCH DAMAGE.
     29  1.20.2.2  yamt  */
     30  1.20.2.2  yamt 
     31  1.20.2.2  yamt #include <sys/cdefs.h>
     32  1.20.2.5  yamt __KERNEL_RCSID(0, "$NetBSD: rump_vfs.c,v 1.20.2.5 2009/07/18 14:53:26 yamt Exp $");
     33  1.20.2.2  yamt 
     34  1.20.2.2  yamt #include <sys/param.h>
     35  1.20.2.2  yamt #include <sys/buf.h>
     36  1.20.2.2  yamt #include <sys/conf.h>
     37  1.20.2.2  yamt #include <sys/evcnt.h>
     38  1.20.2.2  yamt #include <sys/filedesc.h>
     39  1.20.2.2  yamt #include <sys/lockf.h>
     40  1.20.2.2  yamt #include <sys/kthread.h>
     41  1.20.2.2  yamt #include <sys/module.h>
     42  1.20.2.2  yamt #include <sys/namei.h>
     43  1.20.2.2  yamt #include <sys/queue.h>
     44  1.20.2.2  yamt #include <sys/vfs_syscalls.h>
     45  1.20.2.2  yamt #include <sys/vnode.h>
     46  1.20.2.2  yamt #include <sys/wapbl.h>
     47  1.20.2.2  yamt 
     48  1.20.2.2  yamt #include <miscfs/specfs/specdev.h>
     49  1.20.2.2  yamt #include <miscfs/syncfs/syncfs.h>
     50  1.20.2.2  yamt 
     51  1.20.2.2  yamt #include <rump/rump.h>
     52  1.20.2.2  yamt #include <rump/rumpuser.h>
     53  1.20.2.2  yamt 
     54  1.20.2.2  yamt #include "rump_private.h"
     55  1.20.2.2  yamt #include "rump_vfs_private.h"
     56  1.20.2.2  yamt 
     57  1.20.2.2  yamt struct fakeblk {
     58  1.20.2.2  yamt 	char path[MAXPATHLEN];
     59  1.20.2.2  yamt 	LIST_ENTRY(fakeblk) entries;
     60  1.20.2.2  yamt };
     61  1.20.2.2  yamt static LIST_HEAD(, fakeblk) fakeblks = LIST_HEAD_INITIALIZER(fakeblks);
     62  1.20.2.2  yamt 
     63  1.20.2.2  yamt static struct cwdinfo rump_cwdi;
     64  1.20.2.2  yamt 
     65  1.20.2.2  yamt static void rump_rcvp_lwpset(struct vnode *, struct vnode *, struct lwp *);
     66  1.20.2.2  yamt 
     67  1.20.2.2  yamt static void
     68  1.20.2.2  yamt pvfs_init(struct proc *p)
     69  1.20.2.2  yamt {
     70  1.20.2.2  yamt 
     71  1.20.2.2  yamt 	p->p_cwdi = cwdinit();
     72  1.20.2.2  yamt }
     73  1.20.2.2  yamt 
     74  1.20.2.2  yamt static void
     75  1.20.2.2  yamt pvfs_rele(struct proc *p)
     76  1.20.2.2  yamt {
     77  1.20.2.2  yamt 
     78  1.20.2.2  yamt 	cwdfree(p->p_cwdi);
     79  1.20.2.2  yamt }
     80  1.20.2.2  yamt 
     81  1.20.2.2  yamt void
     82  1.20.2.2  yamt rump_vfs_init(void)
     83  1.20.2.2  yamt {
     84  1.20.2.2  yamt 	char buf[64];
     85  1.20.2.2  yamt 	int error;
     86  1.20.2.2  yamt 
     87  1.20.2.2  yamt 	dovfsusermount = 1;
     88  1.20.2.2  yamt 
     89  1.20.2.2  yamt 	if (rumpuser_getenv("RUMP_NVNODES", buf, sizeof(buf), &error) == 0) {
     90  1.20.2.2  yamt 		desiredvnodes = strtoul(buf, NULL, 10);
     91  1.20.2.2  yamt 	} else {
     92  1.20.2.2  yamt 		desiredvnodes = 1<<16;
     93  1.20.2.2  yamt 	}
     94  1.20.2.2  yamt 
     95  1.20.2.2  yamt 	rumpblk_init();
     96  1.20.2.2  yamt 
     97  1.20.2.2  yamt 	cache_cpu_init(&rump_cpu);
     98  1.20.2.2  yamt 	vfsinit();
     99  1.20.2.2  yamt 	bufinit();
    100  1.20.2.2  yamt 	wapbl_init();
    101  1.20.2.2  yamt 	cwd_sys_init();
    102  1.20.2.2  yamt 	lf_init();
    103  1.20.2.2  yamt 
    104  1.20.2.2  yamt 	rumpuser_bioinit(rump_biodone);
    105  1.20.2.2  yamt 	rumpfs_init();
    106  1.20.2.2  yamt 
    107  1.20.2.2  yamt 	rump_proc_vfs_init = pvfs_init;
    108  1.20.2.2  yamt 	rump_proc_vfs_release = pvfs_rele;
    109  1.20.2.2  yamt 
    110  1.20.2.2  yamt 	/* bootstrap cwdi */
    111  1.20.2.2  yamt 	rw_init(&rump_cwdi.cwdi_lock);
    112  1.20.2.2  yamt 	rump_cwdi.cwdi_cdir = rootvnode;
    113  1.20.2.2  yamt 	vref(rump_cwdi.cwdi_cdir);
    114  1.20.2.2  yamt 	proc0.p_cwdi = &rump_cwdi;
    115  1.20.2.2  yamt 	proc0.p_cwdi = cwdinit();
    116  1.20.2.2  yamt 
    117  1.20.2.2  yamt 	if (rump_threads) {
    118  1.20.2.2  yamt 		int rv;
    119  1.20.2.2  yamt 
    120  1.20.2.2  yamt 		if ((rv = kthread_create(PRI_IOFLUSH, KTHREAD_MPSAFE, NULL,
    121  1.20.2.2  yamt 		    sched_sync, NULL, NULL, "ioflush")) != 0)
    122  1.20.2.2  yamt 			panic("syncer thread create failed: %d", rv);
    123  1.20.2.2  yamt 	} else {
    124  1.20.2.2  yamt 		syncdelay = 0;
    125  1.20.2.2  yamt 	}
    126  1.20.2.2  yamt }
    127  1.20.2.2  yamt 
    128  1.20.2.2  yamt struct mount *
    129  1.20.2.2  yamt rump_mnt_init(struct vfsops *vfsops, int mntflags)
    130  1.20.2.2  yamt {
    131  1.20.2.2  yamt 	struct mount *mp;
    132  1.20.2.2  yamt 
    133  1.20.2.2  yamt 	mp = kmem_zalloc(sizeof(struct mount), KM_SLEEP);
    134  1.20.2.2  yamt 
    135  1.20.2.2  yamt 	mp->mnt_op = vfsops;
    136  1.20.2.2  yamt 	mp->mnt_flag = mntflags;
    137  1.20.2.2  yamt 	TAILQ_INIT(&mp->mnt_vnodelist);
    138  1.20.2.2  yamt 	rw_init(&mp->mnt_unmounting);
    139  1.20.2.2  yamt 	mutex_init(&mp->mnt_updating, MUTEX_DEFAULT, IPL_NONE);
    140  1.20.2.2  yamt 	mutex_init(&mp->mnt_renamelock, MUTEX_DEFAULT, IPL_NONE);
    141  1.20.2.2  yamt 	mp->mnt_refcnt = 1;
    142  1.20.2.2  yamt 	mp->mnt_vnodecovered = rootvnode;
    143  1.20.2.2  yamt 
    144  1.20.2.2  yamt 	mount_initspecific(mp);
    145  1.20.2.2  yamt 
    146  1.20.2.2  yamt 	return mp;
    147  1.20.2.2  yamt }
    148  1.20.2.2  yamt 
    149  1.20.2.2  yamt int
    150  1.20.2.2  yamt rump_mnt_mount(struct mount *mp, const char *path, void *data, size_t *dlen)
    151  1.20.2.2  yamt {
    152  1.20.2.2  yamt 	struct vnode *rvp;
    153  1.20.2.2  yamt 	int rv;
    154  1.20.2.2  yamt 
    155  1.20.2.2  yamt 	rv = VFS_MOUNT(mp, path, data, dlen);
    156  1.20.2.2  yamt 	if (rv)
    157  1.20.2.2  yamt 		return rv;
    158  1.20.2.2  yamt 
    159  1.20.2.2  yamt 	(void) VFS_STATVFS(mp, &mp->mnt_stat);
    160  1.20.2.2  yamt 	rv = VFS_START(mp, 0);
    161  1.20.2.2  yamt 	if (rv) {
    162  1.20.2.2  yamt 		VFS_UNMOUNT(mp, MNT_FORCE);
    163  1.20.2.2  yamt 		return rv;
    164  1.20.2.2  yamt 	}
    165  1.20.2.2  yamt 
    166  1.20.2.2  yamt 	/*
    167  1.20.2.2  yamt 	 * XXX: set a root for lwp0.  This is strictly not correct,
    168  1.20.2.2  yamt 	 * but makes things work for single fs case without having
    169  1.20.2.2  yamt 	 * to manually call rump_rcvp_set().
    170  1.20.2.2  yamt 	 */
    171  1.20.2.2  yamt 	VFS_ROOT(mp, &rvp);
    172  1.20.2.2  yamt 	rump_rcvp_lwpset(rvp, rvp, &lwp0);
    173  1.20.2.2  yamt 	vput(rvp);
    174  1.20.2.2  yamt 
    175  1.20.2.2  yamt 	return rv;
    176  1.20.2.2  yamt }
    177  1.20.2.2  yamt 
    178  1.20.2.2  yamt void
    179  1.20.2.2  yamt rump_mnt_destroy(struct mount *mp)
    180  1.20.2.2  yamt {
    181  1.20.2.2  yamt 
    182  1.20.2.2  yamt 	/* See rcvp XXX above */
    183  1.20.2.2  yamt 	rump_cwdi.cwdi_rdir = NULL;
    184  1.20.2.2  yamt 	vref(rootvnode);
    185  1.20.2.2  yamt 	rump_cwdi.cwdi_cdir = rootvnode;
    186  1.20.2.2  yamt 
    187  1.20.2.2  yamt 	mount_finispecific(mp);
    188  1.20.2.2  yamt 	kmem_free(mp, sizeof(*mp));
    189  1.20.2.2  yamt }
    190  1.20.2.2  yamt 
    191  1.20.2.2  yamt struct componentname *
    192  1.20.2.2  yamt rump_makecn(u_long nameiop, u_long flags, const char *name, size_t namelen,
    193  1.20.2.2  yamt 	kauth_cred_t creds, struct lwp *l)
    194  1.20.2.2  yamt {
    195  1.20.2.2  yamt 	struct componentname *cnp;
    196  1.20.2.2  yamt 	const char *cp = NULL;
    197  1.20.2.2  yamt 
    198  1.20.2.2  yamt 	cnp = kmem_zalloc(sizeof(struct componentname), KM_SLEEP);
    199  1.20.2.2  yamt 
    200  1.20.2.2  yamt 	cnp->cn_nameiop = nameiop;
    201  1.20.2.2  yamt 	cnp->cn_flags = flags | HASBUF;
    202  1.20.2.2  yamt 
    203  1.20.2.2  yamt 	cnp->cn_pnbuf = PNBUF_GET();
    204  1.20.2.2  yamt 	strcpy(cnp->cn_pnbuf, name);
    205  1.20.2.2  yamt 	cnp->cn_nameptr = cnp->cn_pnbuf;
    206  1.20.2.2  yamt 	cnp->cn_namelen = namelen;
    207  1.20.2.2  yamt 	cnp->cn_hash = namei_hash(name, &cp);
    208  1.20.2.2  yamt 
    209  1.20.2.2  yamt 	cnp->cn_cred = creds;
    210  1.20.2.2  yamt 
    211  1.20.2.2  yamt 	return cnp;
    212  1.20.2.2  yamt }
    213  1.20.2.2  yamt 
    214  1.20.2.2  yamt void
    215  1.20.2.2  yamt rump_freecn(struct componentname *cnp, int flags)
    216  1.20.2.2  yamt {
    217  1.20.2.2  yamt 
    218  1.20.2.2  yamt 	if (flags & RUMPCN_FREECRED)
    219  1.20.2.2  yamt 		rump_cred_put(cnp->cn_cred);
    220  1.20.2.2  yamt 
    221  1.20.2.3  yamt 	if (cnp->cn_flags & SAVENAME) {
    222  1.20.2.3  yamt 		if (flags & RUMPCN_ISLOOKUP || cnp->cn_flags & SAVESTART)
    223  1.20.2.2  yamt 			PNBUF_PUT(cnp->cn_pnbuf);
    224  1.20.2.3  yamt 	} else {
    225  1.20.2.3  yamt 		PNBUF_PUT(cnp->cn_pnbuf);
    226  1.20.2.2  yamt 	}
    227  1.20.2.2  yamt 	kmem_free(cnp, sizeof(*cnp));
    228  1.20.2.2  yamt }
    229  1.20.2.2  yamt 
    230  1.20.2.2  yamt /* hey baby, what's your namei? */
    231  1.20.2.2  yamt int
    232  1.20.2.2  yamt rump_namei(uint32_t op, uint32_t flags, const char *namep,
    233  1.20.2.2  yamt 	struct vnode **dvpp, struct vnode **vpp, struct componentname **cnpp)
    234  1.20.2.2  yamt {
    235  1.20.2.2  yamt 	struct nameidata nd;
    236  1.20.2.2  yamt 	int rv;
    237  1.20.2.2  yamt 
    238  1.20.2.2  yamt 	NDINIT(&nd, op, flags, UIO_SYSSPACE, namep);
    239  1.20.2.2  yamt 	rv = namei(&nd);
    240  1.20.2.2  yamt 	if (rv)
    241  1.20.2.2  yamt 		return rv;
    242  1.20.2.2  yamt 
    243  1.20.2.2  yamt 	if (dvpp) {
    244  1.20.2.2  yamt 		KASSERT(flags & LOCKPARENT);
    245  1.20.2.2  yamt 		*dvpp = nd.ni_dvp;
    246  1.20.2.2  yamt 	} else {
    247  1.20.2.2  yamt 		KASSERT((flags & LOCKPARENT) == 0);
    248  1.20.2.2  yamt 	}
    249  1.20.2.2  yamt 
    250  1.20.2.2  yamt 	if (vpp) {
    251  1.20.2.2  yamt 		*vpp = nd.ni_vp;
    252  1.20.2.2  yamt 	} else {
    253  1.20.2.2  yamt 		if (nd.ni_vp) {
    254  1.20.2.2  yamt 			if (flags & LOCKLEAF)
    255  1.20.2.2  yamt 				vput(nd.ni_vp);
    256  1.20.2.2  yamt 			else
    257  1.20.2.2  yamt 				vrele(nd.ni_vp);
    258  1.20.2.2  yamt 		}
    259  1.20.2.2  yamt 	}
    260  1.20.2.2  yamt 
    261  1.20.2.2  yamt 	if (cnpp) {
    262  1.20.2.2  yamt 		struct componentname *cnp;
    263  1.20.2.2  yamt 
    264  1.20.2.2  yamt 		cnp = kmem_alloc(sizeof(*cnp), KM_SLEEP);
    265  1.20.2.2  yamt 		memcpy(cnp, &nd.ni_cnd, sizeof(*cnp));
    266  1.20.2.2  yamt 		*cnpp = cnp;
    267  1.20.2.2  yamt 	} else if (nd.ni_cnd.cn_flags & HASBUF) {
    268  1.20.2.2  yamt 		panic("%s: pathbuf mismatch", __func__);
    269  1.20.2.2  yamt 	}
    270  1.20.2.2  yamt 
    271  1.20.2.2  yamt 	return rv;
    272  1.20.2.2  yamt }
    273  1.20.2.2  yamt 
    274  1.20.2.2  yamt static struct fakeblk *
    275  1.20.2.2  yamt _rump_fakeblk_find(const char *path)
    276  1.20.2.2  yamt {
    277  1.20.2.2  yamt 	char buf[MAXPATHLEN];
    278  1.20.2.2  yamt 	struct fakeblk *fblk;
    279  1.20.2.2  yamt 	int error;
    280  1.20.2.2  yamt 
    281  1.20.2.2  yamt 	if (rumpuser_realpath(path, buf, &error) == NULL)
    282  1.20.2.2  yamt 		return NULL;
    283  1.20.2.2  yamt 
    284  1.20.2.2  yamt 	LIST_FOREACH(fblk, &fakeblks, entries)
    285  1.20.2.2  yamt 		if (strcmp(fblk->path, buf) == 0)
    286  1.20.2.2  yamt 			return fblk;
    287  1.20.2.2  yamt 
    288  1.20.2.2  yamt 	return NULL;
    289  1.20.2.2  yamt }
    290  1.20.2.2  yamt 
    291  1.20.2.2  yamt int
    292  1.20.2.2  yamt rump_fakeblk_register(const char *path)
    293  1.20.2.2  yamt {
    294  1.20.2.2  yamt 	char buf[MAXPATHLEN];
    295  1.20.2.2  yamt 	struct fakeblk *fblk;
    296  1.20.2.2  yamt 	int error;
    297  1.20.2.2  yamt 
    298  1.20.2.2  yamt 	if (_rump_fakeblk_find(path))
    299  1.20.2.2  yamt 		return EEXIST;
    300  1.20.2.2  yamt 
    301  1.20.2.2  yamt 	if (rumpuser_realpath(path, buf, &error) == NULL)
    302  1.20.2.2  yamt 		return error;
    303  1.20.2.2  yamt 
    304  1.20.2.2  yamt 	fblk = kmem_alloc(sizeof(struct fakeblk), KM_NOSLEEP);
    305  1.20.2.2  yamt 	if (fblk == NULL)
    306  1.20.2.2  yamt 		return ENOMEM;
    307  1.20.2.2  yamt 
    308  1.20.2.2  yamt 	strlcpy(fblk->path, buf, MAXPATHLEN);
    309  1.20.2.2  yamt 	LIST_INSERT_HEAD(&fakeblks, fblk, entries);
    310  1.20.2.2  yamt 
    311  1.20.2.2  yamt 	return 0;
    312  1.20.2.2  yamt }
    313  1.20.2.2  yamt 
    314  1.20.2.2  yamt int
    315  1.20.2.2  yamt rump_fakeblk_find(const char *path)
    316  1.20.2.2  yamt {
    317  1.20.2.2  yamt 
    318  1.20.2.2  yamt 	return _rump_fakeblk_find(path) != NULL;
    319  1.20.2.2  yamt }
    320  1.20.2.2  yamt 
    321  1.20.2.2  yamt void
    322  1.20.2.2  yamt rump_fakeblk_deregister(const char *path)
    323  1.20.2.2  yamt {
    324  1.20.2.2  yamt 	struct fakeblk *fblk;
    325  1.20.2.2  yamt 
    326  1.20.2.2  yamt 	fblk = _rump_fakeblk_find(path);
    327  1.20.2.2  yamt 	if (fblk == NULL)
    328  1.20.2.2  yamt 		return;
    329  1.20.2.2  yamt 
    330  1.20.2.2  yamt 	LIST_REMOVE(fblk, entries);
    331  1.20.2.2  yamt 	kmem_free(fblk, sizeof(*fblk));
    332  1.20.2.2  yamt }
    333  1.20.2.2  yamt 
    334  1.20.2.2  yamt void
    335  1.20.2.2  yamt rump_getvninfo(struct vnode *vp, enum vtype *vtype, voff_t *vsize, dev_t *vdev)
    336  1.20.2.2  yamt {
    337  1.20.2.2  yamt 
    338  1.20.2.2  yamt 	*vtype = vp->v_type;
    339  1.20.2.2  yamt 	*vsize = vp->v_size;
    340  1.20.2.2  yamt 	if (vp->v_specnode)
    341  1.20.2.2  yamt 		*vdev = vp->v_rdev;
    342  1.20.2.2  yamt 	else
    343  1.20.2.2  yamt 		*vdev = 0;
    344  1.20.2.2  yamt }
    345  1.20.2.2  yamt 
    346  1.20.2.2  yamt struct vfsops *
    347  1.20.2.2  yamt rump_vfslist_iterate(struct vfsops *ops)
    348  1.20.2.2  yamt {
    349  1.20.2.2  yamt 
    350  1.20.2.2  yamt 	if (ops == NULL)
    351  1.20.2.2  yamt 		return LIST_FIRST(&vfs_list);
    352  1.20.2.2  yamt 	else
    353  1.20.2.2  yamt 		return LIST_NEXT(ops, vfs_list);
    354  1.20.2.2  yamt }
    355  1.20.2.2  yamt 
    356  1.20.2.2  yamt struct vfsops *
    357  1.20.2.2  yamt rump_vfs_getopsbyname(const char *name)
    358  1.20.2.2  yamt {
    359  1.20.2.2  yamt 
    360  1.20.2.2  yamt 	return vfs_getopsbyname(name);
    361  1.20.2.2  yamt }
    362  1.20.2.2  yamt 
    363  1.20.2.2  yamt int
    364  1.20.2.2  yamt rump_vfs_getmp(const char *path, struct mount **mpp)
    365  1.20.2.2  yamt {
    366  1.20.2.2  yamt 	struct vnode *vp;
    367  1.20.2.2  yamt 	int rv;
    368  1.20.2.2  yamt 
    369  1.20.2.5  yamt 	if ((rv = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp)) != 0)
    370  1.20.2.2  yamt 		return rv;
    371  1.20.2.2  yamt 
    372  1.20.2.2  yamt 	*mpp = vp->v_mount;
    373  1.20.2.2  yamt 	vrele(vp);
    374  1.20.2.2  yamt 	return 0;
    375  1.20.2.2  yamt }
    376  1.20.2.2  yamt 
    377  1.20.2.2  yamt struct vattr*
    378  1.20.2.2  yamt rump_vattr_init(void)
    379  1.20.2.2  yamt {
    380  1.20.2.2  yamt 	struct vattr *vap;
    381  1.20.2.2  yamt 
    382  1.20.2.2  yamt 	vap = kmem_alloc(sizeof(struct vattr), KM_SLEEP);
    383  1.20.2.2  yamt 	vattr_null(vap);
    384  1.20.2.2  yamt 
    385  1.20.2.2  yamt 	return vap;
    386  1.20.2.2  yamt }
    387  1.20.2.2  yamt 
    388  1.20.2.2  yamt void
    389  1.20.2.2  yamt rump_vattr_settype(struct vattr *vap, enum vtype vt)
    390  1.20.2.2  yamt {
    391  1.20.2.2  yamt 
    392  1.20.2.2  yamt 	vap->va_type = vt;
    393  1.20.2.2  yamt }
    394  1.20.2.2  yamt 
    395  1.20.2.2  yamt void
    396  1.20.2.2  yamt rump_vattr_setmode(struct vattr *vap, mode_t mode)
    397  1.20.2.2  yamt {
    398  1.20.2.2  yamt 
    399  1.20.2.2  yamt 	vap->va_mode = mode;
    400  1.20.2.2  yamt }
    401  1.20.2.2  yamt 
    402  1.20.2.2  yamt void
    403  1.20.2.2  yamt rump_vattr_setrdev(struct vattr *vap, dev_t dev)
    404  1.20.2.2  yamt {
    405  1.20.2.2  yamt 
    406  1.20.2.2  yamt 	vap->va_rdev = dev;
    407  1.20.2.2  yamt }
    408  1.20.2.2  yamt 
    409  1.20.2.2  yamt void
    410  1.20.2.2  yamt rump_vattr_free(struct vattr *vap)
    411  1.20.2.2  yamt {
    412  1.20.2.2  yamt 
    413  1.20.2.2  yamt 	kmem_free(vap, sizeof(*vap));
    414  1.20.2.2  yamt }
    415  1.20.2.2  yamt 
    416  1.20.2.2  yamt void
    417  1.20.2.2  yamt rump_vp_incref(struct vnode *vp)
    418  1.20.2.2  yamt {
    419  1.20.2.2  yamt 
    420  1.20.2.2  yamt 	mutex_enter(&vp->v_interlock);
    421  1.20.2.2  yamt 	++vp->v_usecount;
    422  1.20.2.2  yamt 	mutex_exit(&vp->v_interlock);
    423  1.20.2.2  yamt }
    424  1.20.2.2  yamt 
    425  1.20.2.2  yamt int
    426  1.20.2.2  yamt rump_vp_getref(struct vnode *vp)
    427  1.20.2.2  yamt {
    428  1.20.2.2  yamt 
    429  1.20.2.2  yamt 	return vp->v_usecount;
    430  1.20.2.2  yamt }
    431  1.20.2.2  yamt 
    432  1.20.2.2  yamt void
    433  1.20.2.2  yamt rump_vp_decref(struct vnode *vp)
    434  1.20.2.2  yamt {
    435  1.20.2.2  yamt 
    436  1.20.2.2  yamt 	mutex_enter(&vp->v_interlock);
    437  1.20.2.2  yamt 	--vp->v_usecount;
    438  1.20.2.2  yamt 	mutex_exit(&vp->v_interlock);
    439  1.20.2.2  yamt }
    440  1.20.2.2  yamt 
    441  1.20.2.2  yamt /*
    442  1.20.2.2  yamt  * Really really recycle with a cherry on top.  We should be
    443  1.20.2.2  yamt  * extra-sure we can do this.  For example with p2k there is
    444  1.20.2.2  yamt  * no problem, since puffs in the kernel takes care of refcounting
    445  1.20.2.2  yamt  * for us.
    446  1.20.2.2  yamt  */
    447  1.20.2.2  yamt void
    448  1.20.2.2  yamt rump_vp_recycle_nokidding(struct vnode *vp)
    449  1.20.2.2  yamt {
    450  1.20.2.2  yamt 
    451  1.20.2.2  yamt 	mutex_enter(&vp->v_interlock);
    452  1.20.2.2  yamt 	vp->v_usecount = 1;
    453  1.20.2.2  yamt 	/*
    454  1.20.2.2  yamt 	 * XXX: NFS holds a reference to the root vnode, so don't clean
    455  1.20.2.2  yamt 	 * it out.  This is very wrong, but fixing it properly would
    456  1.20.2.2  yamt 	 * take too much effort for now
    457  1.20.2.2  yamt 	 */
    458  1.20.2.2  yamt 	if (vp->v_tag == VT_NFS && vp->v_vflag & VV_ROOT) {
    459  1.20.2.2  yamt 		mutex_exit(&vp->v_interlock);
    460  1.20.2.2  yamt 		return;
    461  1.20.2.2  yamt 	}
    462  1.20.2.2  yamt 	vclean(vp, DOCLOSE);
    463  1.20.2.2  yamt 	vrelel(vp, 0);
    464  1.20.2.2  yamt }
    465  1.20.2.2  yamt 
    466  1.20.2.2  yamt void
    467  1.20.2.2  yamt rump_vp_rele(struct vnode *vp)
    468  1.20.2.2  yamt {
    469  1.20.2.2  yamt 
    470  1.20.2.2  yamt 	vrele(vp);
    471  1.20.2.2  yamt }
    472  1.20.2.2  yamt 
    473  1.20.2.2  yamt void
    474  1.20.2.2  yamt rump_vp_interlock(struct vnode *vp)
    475  1.20.2.2  yamt {
    476  1.20.2.2  yamt 
    477  1.20.2.2  yamt 	mutex_enter(&vp->v_interlock);
    478  1.20.2.2  yamt }
    479  1.20.2.2  yamt 
    480  1.20.2.2  yamt int
    481  1.20.2.2  yamt rump_vfs_unmount(struct mount *mp, int mntflags)
    482  1.20.2.2  yamt {
    483  1.20.2.2  yamt #if 0
    484  1.20.2.2  yamt 	struct evcnt *ev;
    485  1.20.2.2  yamt 
    486  1.20.2.2  yamt 	printf("event counters:\n");
    487  1.20.2.2  yamt 	TAILQ_FOREACH(ev, &allevents, ev_list)
    488  1.20.2.2  yamt 		printf("%s: %llu\n", ev->ev_name, ev->ev_count);
    489  1.20.2.2  yamt #endif
    490  1.20.2.2  yamt 
    491  1.20.2.2  yamt 	return VFS_UNMOUNT(mp, mntflags);
    492  1.20.2.2  yamt }
    493  1.20.2.2  yamt 
    494  1.20.2.2  yamt int
    495  1.20.2.2  yamt rump_vfs_root(struct mount *mp, struct vnode **vpp, int lock)
    496  1.20.2.2  yamt {
    497  1.20.2.2  yamt 	int rv;
    498  1.20.2.2  yamt 
    499  1.20.2.2  yamt 	rv = VFS_ROOT(mp, vpp);
    500  1.20.2.2  yamt 	if (rv)
    501  1.20.2.2  yamt 		return rv;
    502  1.20.2.2  yamt 
    503  1.20.2.2  yamt 	if (!lock)
    504  1.20.2.2  yamt 		VOP_UNLOCK(*vpp, 0);
    505  1.20.2.2  yamt 
    506  1.20.2.2  yamt 	return 0;
    507  1.20.2.2  yamt }
    508  1.20.2.2  yamt 
    509  1.20.2.2  yamt int
    510  1.20.2.2  yamt rump_vfs_statvfs(struct mount *mp, struct statvfs *sbp)
    511  1.20.2.2  yamt {
    512  1.20.2.2  yamt 
    513  1.20.2.2  yamt 	return VFS_STATVFS(mp, sbp);
    514  1.20.2.2  yamt }
    515  1.20.2.2  yamt 
    516  1.20.2.2  yamt int
    517  1.20.2.2  yamt rump_vfs_sync(struct mount *mp, int wait, kauth_cred_t cred)
    518  1.20.2.2  yamt {
    519  1.20.2.2  yamt 
    520  1.20.2.2  yamt 	return VFS_SYNC(mp, wait ? MNT_WAIT : MNT_NOWAIT, cred);
    521  1.20.2.2  yamt }
    522  1.20.2.2  yamt 
    523  1.20.2.2  yamt int
    524  1.20.2.2  yamt rump_vfs_fhtovp(struct mount *mp, struct fid *fid, struct vnode **vpp)
    525  1.20.2.2  yamt {
    526  1.20.2.2  yamt 
    527  1.20.2.2  yamt 	return VFS_FHTOVP(mp, fid, vpp);
    528  1.20.2.2  yamt }
    529  1.20.2.2  yamt 
    530  1.20.2.2  yamt int
    531  1.20.2.2  yamt rump_vfs_vptofh(struct vnode *vp, struct fid *fid, size_t *fidsize)
    532  1.20.2.2  yamt {
    533  1.20.2.2  yamt 
    534  1.20.2.2  yamt 	return VFS_VPTOFH(vp, fid, fidsize);
    535  1.20.2.2  yamt }
    536  1.20.2.2  yamt 
    537  1.20.2.2  yamt /*ARGSUSED*/
    538  1.20.2.2  yamt void
    539  1.20.2.2  yamt rump_vfs_syncwait(struct mount *mp)
    540  1.20.2.2  yamt {
    541  1.20.2.2  yamt 	int n;
    542  1.20.2.2  yamt 
    543  1.20.2.2  yamt 	n = buf_syncwait();
    544  1.20.2.2  yamt 	if (n)
    545  1.20.2.2  yamt 		printf("syncwait: unsynced buffers: %d\n", n);
    546  1.20.2.2  yamt }
    547  1.20.2.2  yamt 
    548  1.20.2.2  yamt void
    549  1.20.2.2  yamt rump_biodone(void *arg, size_t count, int error)
    550  1.20.2.2  yamt {
    551  1.20.2.2  yamt 	struct buf *bp = arg;
    552  1.20.2.2  yamt 
    553  1.20.2.2  yamt 	bp->b_resid = bp->b_bcount - count;
    554  1.20.2.2  yamt 	KASSERT(bp->b_resid >= 0);
    555  1.20.2.2  yamt 	bp->b_error = error;
    556  1.20.2.2  yamt 
    557  1.20.2.2  yamt 	biodone(bp);
    558  1.20.2.2  yamt }
    559  1.20.2.2  yamt 
    560  1.20.2.2  yamt static void
    561  1.20.2.2  yamt rump_rcvp_lwpset(struct vnode *rvp, struct vnode *cvp, struct lwp *l)
    562  1.20.2.2  yamt {
    563  1.20.2.2  yamt 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
    564  1.20.2.2  yamt 
    565  1.20.2.2  yamt 	KASSERT(cvp);
    566  1.20.2.2  yamt 
    567  1.20.2.2  yamt 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
    568  1.20.2.2  yamt 	if (cwdi->cwdi_rdir)
    569  1.20.2.2  yamt 		vrele(cwdi->cwdi_rdir);
    570  1.20.2.2  yamt 	if (rvp)
    571  1.20.2.2  yamt 		vref(rvp);
    572  1.20.2.2  yamt 	cwdi->cwdi_rdir = rvp;
    573  1.20.2.2  yamt 
    574  1.20.2.2  yamt 	vrele(cwdi->cwdi_cdir);
    575  1.20.2.2  yamt 	vref(cvp);
    576  1.20.2.2  yamt 	cwdi->cwdi_cdir = cvp;
    577  1.20.2.2  yamt 	rw_exit(&cwdi->cwdi_lock);
    578  1.20.2.2  yamt }
    579  1.20.2.2  yamt 
    580  1.20.2.2  yamt void
    581  1.20.2.2  yamt rump_rcvp_set(struct vnode *rvp, struct vnode *cvp)
    582  1.20.2.2  yamt {
    583  1.20.2.2  yamt 
    584  1.20.2.2  yamt 	rump_rcvp_lwpset(rvp, cvp, curlwp);
    585  1.20.2.2  yamt }
    586  1.20.2.2  yamt 
    587  1.20.2.2  yamt struct vnode *
    588  1.20.2.2  yamt rump_cdir_get(void)
    589  1.20.2.2  yamt {
    590  1.20.2.2  yamt 	struct vnode *vp;
    591  1.20.2.2  yamt 	struct cwdinfo *cwdi = curlwp->l_proc->p_cwdi;
    592  1.20.2.2  yamt 
    593  1.20.2.2  yamt 	rw_enter(&cwdi->cwdi_lock, RW_READER);
    594  1.20.2.2  yamt 	vp = cwdi->cwdi_cdir;
    595  1.20.2.2  yamt 	rw_exit(&cwdi->cwdi_lock);
    596  1.20.2.2  yamt 	vref(vp);
    597  1.20.2.2  yamt 
    598  1.20.2.2  yamt 	return vp;
    599  1.20.2.2  yamt }
    600