Home | History | Annotate | Line # | Download | only in rumpvfs
rumpfs.c revision 1.40
      1 /*	$NetBSD: rumpfs.c,v 1.40 2010/04/21 07:35:12 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2009  Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: rumpfs.c,v 1.40 2010/04/21 07:35:12 pooka Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/atomic.h>
     33 #include <sys/dirent.h>
     34 #include <sys/errno.h>
     35 #include <sys/filedesc.h>
     36 #include <sys/fcntl.h>
     37 #include <sys/kauth.h>
     38 #include <sys/malloc.h>
     39 #include <sys/module.h>
     40 #include <sys/mount.h>
     41 #include <sys/namei.h>
     42 #include <sys/lock.h>
     43 #include <sys/lockf.h>
     44 #include <sys/queue.h>
     45 #include <sys/stat.h>
     46 #include <sys/syscallargs.h>
     47 #include <sys/vnode.h>
     48 
     49 #include <miscfs/fifofs/fifo.h>
     50 #include <miscfs/specfs/specdev.h>
     51 #include <miscfs/genfs/genfs.h>
     52 
     53 #include <rump/rumpuser.h>
     54 
     55 #include "rump_private.h"
     56 #include "rump_vfs_private.h"
     57 
     58 static int rump_vop_lookup(void *);
     59 static int rump_vop_getattr(void *);
     60 static int rump_vop_mkdir(void *);
     61 static int rump_vop_mknod(void *);
     62 static int rump_vop_create(void *);
     63 static int rump_vop_inactive(void *);
     64 static int rump_vop_reclaim(void *);
     65 static int rump_vop_success(void *);
     66 static int rump_vop_spec(void *);
     67 static int rump_vop_read(void *);
     68 static int rump_vop_write(void *);
     69 static int rump_vop_open(void *);
     70 
     71 int (**fifo_vnodeop_p)(void *);
     72 const struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
     73 	{ &vop_default_desc, vn_default_error },
     74 	{ NULL, NULL }
     75 };
     76 const struct vnodeopv_desc fifo_vnodeop_opv_desc =
     77 	{ &fifo_vnodeop_p, fifo_vnodeop_entries };
     78 
     79 int (**rump_vnodeop_p)(void *);
     80 const struct vnodeopv_entry_desc rump_vnodeop_entries[] = {
     81 	{ &vop_default_desc, vn_default_error },
     82 	{ &vop_lookup_desc, rump_vop_lookup },
     83 	{ &vop_getattr_desc, rump_vop_getattr },
     84 	{ &vop_mkdir_desc, rump_vop_mkdir },
     85 	{ &vop_mknod_desc, rump_vop_mknod },
     86 	{ &vop_create_desc, rump_vop_create },
     87 	{ &vop_access_desc, rump_vop_success },
     88 	{ &vop_read_desc, rump_vop_read },
     89 	{ &vop_write_desc, rump_vop_write },
     90 	{ &vop_open_desc, rump_vop_open },
     91 	{ &vop_putpages_desc, genfs_null_putpages },
     92 	{ &vop_fsync_desc, rump_vop_success },
     93 	{ &vop_lock_desc, genfs_lock },
     94 	{ &vop_unlock_desc, genfs_unlock },
     95 	{ &vop_inactive_desc, rump_vop_inactive },
     96 	{ &vop_reclaim_desc, rump_vop_reclaim },
     97 	{ NULL, NULL }
     98 };
     99 const struct vnodeopv_desc rump_vnodeop_opv_desc =
    100 	{ &rump_vnodeop_p, rump_vnodeop_entries };
    101 
    102 int (**rump_specop_p)(void *);
    103 const struct vnodeopv_entry_desc rump_specop_entries[] = {
    104 	{ &vop_default_desc, rump_vop_spec },
    105 	{ NULL, NULL }
    106 };
    107 const struct vnodeopv_desc rump_specop_opv_desc =
    108 	{ &rump_specop_p, rump_specop_entries };
    109 
    110 const struct vnodeopv_desc * const rump_opv_descs[] = {
    111 	&rump_vnodeop_opv_desc,
    112 	&rump_specop_opv_desc,
    113 	NULL
    114 };
    115 
    116 struct rumpfs_dent {
    117 	char *rd_name;
    118 	struct rumpfs_node *rd_node;
    119 
    120 	LIST_ENTRY(rumpfs_dent) rd_entries;
    121 };
    122 
    123 struct rumpfs_node {
    124 	struct vattr rn_va;
    125 	struct vnode *rn_vp;
    126 
    127 	union {
    128 		struct {
    129 			char *hostpath;		/* VREG */
    130 			int readfd;
    131 			int writefd;
    132 			uint64_t offset;
    133 		} reg;
    134 		LIST_HEAD(, rumpfs_dent) dir;	/* VDIR */
    135 	} rn_u;
    136 };
    137 #define rn_hostpath	rn_u.reg.hostpath
    138 #define rn_readfd	rn_u.reg.readfd
    139 #define rn_writefd	rn_u.reg.writefd
    140 #define rn_offset	rn_u.reg.offset
    141 #define rn_dir		rn_u.dir
    142 
    143 struct rumpfs_mount {
    144 	struct vnode *rfsmp_rvp;
    145 };
    146 
    147 static struct rumpfs_node *makeprivate(enum vtype, dev_t, off_t);
    148 
    149 /*
    150  * Extra Terrestrial stuff.  We map a given key (pathname) to a file on
    151  * the host FS.  ET phones home only from the root node of rumpfs.
    152  *
    153  * When an etfs node is removed, a vnode potentially behind it is not
    154  * immediately recycled.
    155  */
    156 
    157 struct etfs {
    158 	char et_key[MAXPATHLEN];
    159 	size_t et_keylen;
    160 
    161 	LIST_ENTRY(etfs) et_entries;
    162 
    163 	struct rumpfs_node *et_rn;
    164 };
    165 static kmutex_t etfs_lock;
    166 static LIST_HEAD(, etfs) etfs_list = LIST_HEAD_INITIALIZER(etfs_list);
    167 
    168 static enum vtype
    169 ettype_to_vtype(enum rump_etfs_type et)
    170 {
    171 	enum vtype vt;
    172 
    173 	switch (et) {
    174 	case RUMP_ETFS_REG:
    175 		vt = VREG;
    176 		break;
    177 	case RUMP_ETFS_BLK:
    178 		vt = VBLK;
    179 		break;
    180 	case RUMP_ETFS_CHR:
    181 		vt = VCHR;
    182 		break;
    183 	default:
    184 		panic("invalid et type: %d", et);
    185 	}
    186 
    187 	return vt;
    188 }
    189 
    190 static bool
    191 etfs_find(const char *key, struct rumpfs_node **rnp)
    192 {
    193 	struct etfs *et;
    194 	size_t keylen = strlen(key);
    195 	bool rv = false;
    196 
    197 	KASSERT(mutex_owned(&etfs_lock));
    198 
    199 	LIST_FOREACH(et, &etfs_list, et_entries) {
    200 		if (keylen == et->et_keylen && strcmp(key, et->et_key) == 0) {
    201 			*rnp = et->et_rn;
    202 			rv = true;
    203 			break;
    204 		}
    205 	}
    206 
    207 	return rv;
    208 }
    209 
    210 static int
    211 doregister(const char *key, const char *hostpath,
    212 	enum rump_etfs_type ftype, uint64_t begin, uint64_t size)
    213 {
    214 	struct etfs *et;
    215 	struct rumpfs_node *rn_dummy, *rn;
    216 	uint64_t fsize;
    217 	dev_t rdev = NODEV;
    218 	devminor_t dmin;
    219 	int hft, error;
    220 
    221 	if (rumpuser_getfileinfo(hostpath, &fsize, &hft, &error))
    222 		return error;
    223 
    224 	/* check that we give sensible arguments */
    225 	if (begin > fsize)
    226 		return EINVAL;
    227 	if (size == RUMP_ETFS_SIZE_ENDOFF)
    228 		size = fsize - begin;
    229 	if (begin + size > fsize)
    230 		return EINVAL;
    231 
    232 	if (ftype == RUMP_ETFS_BLK || ftype == RUMP_ETFS_CHR) {
    233 		error = rumpblk_register(hostpath, &dmin, begin, size);
    234 		if (error != 0) {
    235 			return error;
    236 		}
    237 		rdev = makedev(RUMPBLK_DEVMAJOR, dmin);
    238 	}
    239 
    240 	et = kmem_alloc(sizeof(*et), KM_SLEEP);
    241 	strcpy(et->et_key, key);
    242 	et->et_keylen = strlen(et->et_key);
    243 	et->et_rn = rn = makeprivate(ettype_to_vtype(ftype), rdev, size);
    244 	if (ftype == RUMP_ETFS_REG) {
    245 		size_t len = strlen(hostpath)+1;
    246 
    247 		rn->rn_hostpath = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
    248 		memcpy(rn->rn_hostpath, hostpath, len);
    249 		rn->rn_offset = begin;
    250 	}
    251 
    252 	mutex_enter(&etfs_lock);
    253 	if (etfs_find(key, &rn_dummy)) {
    254 		mutex_exit(&etfs_lock);
    255 		kmem_free(et, sizeof(*et));
    256 		/* XXX: rumpblk_deregister(hostpath); */
    257 		return EEXIST;
    258 	}
    259 	LIST_INSERT_HEAD(&etfs_list, et, et_entries);
    260 	mutex_exit(&etfs_lock);
    261 
    262 	return 0;
    263 }
    264 
    265 int
    266 rump_etfs_register(const char *key, const char *hostpath,
    267 	enum rump_etfs_type ftype)
    268 {
    269 
    270 	return doregister(key, hostpath, ftype, 0, RUMP_ETFS_SIZE_ENDOFF);
    271 }
    272 
    273 int
    274 rump_etfs_register_withsize(const char *key, const char *hostpath,
    275 	enum rump_etfs_type ftype, uint64_t begin, uint64_t size)
    276 {
    277 
    278 	/*
    279 	 * Check that we're mapping at block offsets.  I guess this
    280 	 * is not technically necessary except for BLK/CHR backends
    281 	 * (i.e. what getfileinfo() returns, not ftype) and can be
    282 	 * removed later if there are problems.
    283 	 */
    284 	if ((begin & (DEV_BSIZE-1)) != 0)
    285 		return EINVAL;
    286 	if (size != RUMP_ETFS_SIZE_ENDOFF && (size & (DEV_BSIZE-1)) != 0)
    287 		return EINVAL;
    288 
    289 	return doregister(key, hostpath, ftype, begin, size);
    290 }
    291 
    292 int
    293 rump_etfs_remove(const char *key)
    294 {
    295 	struct etfs *et;
    296 	size_t keylen = strlen(key);
    297 
    298 	mutex_enter(&etfs_lock);
    299 	LIST_FOREACH(et, &etfs_list, et_entries) {
    300 		if (keylen == et->et_keylen && strcmp(et->et_key, key) == 0) {
    301 			LIST_REMOVE(et, et_entries);
    302 			kmem_free(et, sizeof(*et));
    303 			break;
    304 		}
    305 	}
    306 	mutex_exit(&etfs_lock);
    307 
    308 	if (!et)
    309 		return ENOENT;
    310 	return 0;
    311 }
    312 
    313 /*
    314  * rumpfs
    315  */
    316 
    317 static int lastino = 1;
    318 static kmutex_t reclock;
    319 
    320 static struct rumpfs_node *
    321 makeprivate(enum vtype vt, dev_t rdev, off_t size)
    322 {
    323 	struct rumpfs_node *rn;
    324 	struct vattr *va;
    325 	struct timespec ts;
    326 
    327 	rn = kmem_zalloc(sizeof(*rn), KM_SLEEP);
    328 
    329 	switch (vt) {
    330 	case VDIR:
    331 		LIST_INIT(&rn->rn_dir);
    332 		break;
    333 	case VREG:
    334 		rn->rn_readfd = -1;
    335 		rn->rn_writefd = -1;
    336 		break;
    337 	default:
    338 		break;
    339 	}
    340 
    341 	nanotime(&ts);
    342 
    343 	va = &rn->rn_va;
    344 	va->va_type = vt;
    345 	va->va_mode = 0755;
    346 	if (vt == VDIR)
    347 		va->va_nlink = 2;
    348 	else
    349 		va->va_nlink = 1;
    350 	va->va_uid = 0;
    351 	va->va_gid = 0;
    352 	va->va_fsid =
    353 	va->va_fileid = atomic_inc_uint_nv(&lastino);
    354 	va->va_size = size;
    355 	va->va_blocksize = 512;
    356 	va->va_atime = ts;
    357 	va->va_mtime = ts;
    358 	va->va_ctime = ts;
    359 	va->va_birthtime = ts;
    360 	va->va_gen = 0;
    361 	va->va_flags = 0;
    362 	va->va_rdev = rdev;
    363 	va->va_bytes = 512;
    364 	va->va_filerev = 0;
    365 	va->va_vaflags = 0;
    366 
    367 	return rn;
    368 }
    369 
    370 static int
    371 makevnode(struct mount *mp, struct rumpfs_node *rn, struct vnode **vpp)
    372 {
    373 	struct vnode *vp;
    374 	int (**vpops)(void *);
    375 	struct vattr *va = &rn->rn_va;
    376 	int rv;
    377 
    378 	KASSERT(mutex_owned(&reclock));
    379 
    380 	if (va->va_type == VCHR || va->va_type == VBLK) {
    381 		vpops = rump_specop_p;
    382 	} else {
    383 		vpops = rump_vnodeop_p;
    384 	}
    385 	if (vpops != rump_specop_p && va->va_type != VDIR
    386 	    && !(va->va_type == VREG && rn->rn_hostpath != NULL)
    387 	    && va->va_type != VSOCK)
    388 		return EOPNOTSUPP;
    389 
    390 	rv = getnewvnode(VT_RUMP, mp, vpops, &vp);
    391 	if (rv)
    392 		return rv;
    393 
    394 	vp->v_size = vp->v_writesize = va->va_size;
    395 	vp->v_type = va->va_type;
    396 
    397 	if (vpops == rump_specop_p) {
    398 		spec_node_init(vp, va->va_rdev);
    399 	}
    400 	vp->v_data = rn;
    401 
    402 	vn_lock(vp, LK_RETRY | LK_EXCLUSIVE);
    403 	rn->rn_vp = vp;
    404 	*vpp = vp;
    405 
    406 	return 0;
    407 }
    408 
    409 /*
    410  * Simple lookup for faking lookup of device entry for rump file systems
    411  * and for locating/creating directories.  Yes, this will panic if you
    412  * call it with the wrong arguments.
    413  *
    414  * uhm, this is twisted.  C F C C, hope of C C F C looming
    415  */
    416 static int
    417 rump_vop_lookup(void *v)
    418 {
    419 	struct vop_lookup_args /* {
    420 		struct vnode *a_dvp;
    421 		struct vnode **a_vpp;
    422 		struct componentname *a_cnp;
    423 	}; */ *ap = v;
    424 	struct componentname *cnp = ap->a_cnp;
    425 	struct vnode *dvp = ap->a_dvp;
    426 	struct vnode **vpp = ap->a_vpp;
    427 	struct vnode *vp;
    428 	struct rumpfs_node *rnd = dvp->v_data, *rn;
    429 	struct rumpfs_dent *rd = NULL;
    430 	int rv;
    431 
    432 	/* we handle only some "non-special" cases */
    433 	if (!(((cnp->cn_flags & ISLASTCN) == 0)
    434 	    || (cnp->cn_nameiop == LOOKUP || cnp->cn_nameiop == CREATE)))
    435 		return EOPNOTSUPP;
    436 	if (!((cnp->cn_flags & ISDOTDOT) == 0))
    437 		return EOPNOTSUPP;
    438 
    439 	/* check for dot, return directly if the case */
    440 	if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
    441 		vref(dvp);
    442 		*vpp = dvp;
    443 		goto out;
    444 	}
    445 
    446 	/* check if we are returning a faked block device */
    447 	if (dvp == rootvnode && cnp->cn_nameiop == LOOKUP) {
    448 		mutex_enter(&etfs_lock);
    449 		if (etfs_find(cnp->cn_pnbuf, &rn)) {
    450 			mutex_exit(&etfs_lock);
    451 			cnp->cn_consume = strlen(cnp->cn_nameptr
    452 			    + cnp->cn_namelen);
    453 			cnp->cn_flags &= ~REQUIREDIR;
    454 			goto getvnode;
    455 		}
    456 		mutex_exit(&etfs_lock);
    457 	}
    458 
    459 	if (!rd) {
    460 		LIST_FOREACH(rd, &rnd->rn_dir, rd_entries) {
    461 			if (strlen(rd->rd_name) == cnp->cn_namelen &&
    462 			    strncmp(rd->rd_name, cnp->cn_nameptr,
    463 			      cnp->cn_namelen) == 0)
    464 				break;
    465 		}
    466 	}
    467 
    468 	if (!rd && ((cnp->cn_flags & ISLASTCN) == 0||cnp->cn_nameiop != CREATE))
    469 		return ENOENT;
    470 
    471 	if (!rd && (cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
    472 		cnp->cn_flags |= SAVENAME;
    473 		return EJUSTRETURN;
    474 	}
    475 	rn = rd->rd_node;
    476 	rd = NULL;
    477 
    478  getvnode:
    479 	KASSERT(rn);
    480 	mutex_enter(&reclock);
    481 	if ((vp = rn->rn_vp)) {
    482 		mutex_enter(&vp->v_interlock);
    483 		mutex_exit(&reclock);
    484 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    485 			goto getvnode;
    486 		*vpp = vp;
    487 	} else {
    488 		rv = makevnode(dvp->v_mount, rn, vpp);
    489 		rn->rn_vp = *vpp;
    490 		mutex_exit(&reclock);
    491 		if (rv)
    492 			return rv;
    493 	}
    494 
    495  out:
    496 	return 0;
    497 }
    498 
    499 static int
    500 rump_vop_getattr(void *v)
    501 {
    502 	struct vop_getattr_args /* {
    503 		struct vnode *a_vp;
    504 		struct vattr *a_vap;
    505 		kauth_cred_t a_cred;
    506 	} */ *ap = v;
    507 	struct rumpfs_node *rn = ap->a_vp->v_data;
    508 
    509 	memcpy(ap->a_vap, &rn->rn_va, sizeof(struct vattr));
    510 	return 0;
    511 }
    512 
    513 static int
    514 rump_vop_mkdir(void *v)
    515 {
    516 	struct vop_mkdir_args /* {
    517 		struct vnode *a_dvp;
    518 		struct vnode **a_vpp;
    519 		struct componentname *a_cnp;
    520 		struct vattr *a_vap;
    521 	}; */ *ap = v;
    522 	struct vnode *dvp = ap->a_dvp;
    523 	struct vnode **vpp = ap->a_vpp;
    524 	struct componentname *cnp = ap->a_cnp;
    525 	struct rumpfs_node *rnd = dvp->v_data, *rn;
    526 	struct rumpfs_dent *rdent;
    527 	int rv = 0;
    528 
    529 	rn = makeprivate(VDIR, NODEV, DEV_BSIZE);
    530 	mutex_enter(&reclock);
    531 	rv = makevnode(dvp->v_mount, rn, vpp);
    532 	mutex_exit(&reclock);
    533 	if (rv)
    534 		goto out;
    535 
    536 	rdent = kmem_alloc(sizeof(*rdent), KM_SLEEP);
    537 	rdent->rd_name = kmem_alloc(cnp->cn_namelen+1, KM_SLEEP);
    538 	rdent->rd_node = (*vpp)->v_data;
    539 	strlcpy(rdent->rd_name, cnp->cn_nameptr, cnp->cn_namelen+1);
    540 
    541 	LIST_INSERT_HEAD(&rnd->rn_dir, rdent, rd_entries);
    542 
    543  out:
    544 	vput(dvp);
    545 	return rv;
    546 }
    547 
    548 static int
    549 rump_vop_mknod(void *v)
    550 {
    551 	struct vop_mknod_args /* {
    552 		struct vnode *a_dvp;
    553 		struct vnode **a_vpp;
    554 		struct componentname *a_cnp;
    555 		struct vattr *a_vap;
    556 	}; */ *ap = v;
    557 	struct vnode *dvp = ap->a_dvp;
    558 	struct vnode **vpp = ap->a_vpp;
    559 	struct componentname *cnp = ap->a_cnp;
    560 	struct vattr *va = ap->a_vap;
    561 	struct rumpfs_node *rnd = dvp->v_data, *rn;
    562 	struct rumpfs_dent *rdent;
    563 	int rv;
    564 
    565 	rn = makeprivate(va->va_type, va->va_rdev, DEV_BSIZE);
    566 	mutex_enter(&reclock);
    567 	rv = makevnode(dvp->v_mount, rn, vpp);
    568 	mutex_exit(&reclock);
    569 	if (rv)
    570 		goto out;
    571 
    572 	rdent = kmem_alloc(sizeof(*rdent), KM_SLEEP);
    573 	rdent->rd_name = kmem_alloc(cnp->cn_namelen+1, KM_SLEEP);
    574 	rdent->rd_node = (*vpp)->v_data;
    575 	rdent->rd_node->rn_va.va_rdev = va->va_rdev;
    576 	strlcpy(rdent->rd_name, cnp->cn_nameptr, cnp->cn_namelen+1);
    577 
    578 	LIST_INSERT_HEAD(&rnd->rn_dir, rdent, rd_entries);
    579 
    580  out:
    581 	vput(dvp);
    582 	return rv;
    583 }
    584 
    585 static int
    586 rump_vop_create(void *v)
    587 {
    588 	struct vop_create_args /* {
    589 		struct vnode *a_dvp;
    590 		struct vnode **a_vpp;
    591 		struct componentname *a_cnp;
    592 		struct vattr *a_vap;
    593 	}; */ *ap = v;
    594 	struct vnode *dvp = ap->a_dvp;
    595 	struct vnode **vpp = ap->a_vpp;
    596 	struct componentname *cnp = ap->a_cnp;
    597 	struct vattr *va = ap->a_vap;
    598 	struct rumpfs_node *rnd = dvp->v_data, *rn;
    599 	struct rumpfs_dent *rdent;
    600 	int rv;
    601 
    602 	if (va->va_type != VSOCK) {
    603 		rv = EOPNOTSUPP;
    604 		goto out;
    605 	}
    606 	rn = makeprivate(VSOCK, NODEV, DEV_BSIZE);
    607 	mutex_enter(&reclock);
    608 	rv = makevnode(dvp->v_mount, rn, vpp);
    609 	mutex_exit(&reclock);
    610 	if (rv)
    611 		goto out;
    612 
    613 	rdent = kmem_alloc(sizeof(*rdent), KM_SLEEP);
    614 	rdent->rd_name = kmem_alloc(cnp->cn_namelen+1, KM_SLEEP);
    615 	rdent->rd_node = (*vpp)->v_data;
    616 	rdent->rd_node->rn_va.va_rdev = NODEV;
    617 	strlcpy(rdent->rd_name, cnp->cn_nameptr, cnp->cn_namelen+1);
    618 
    619 	LIST_INSERT_HEAD(&rnd->rn_dir, rdent, rd_entries);
    620 
    621  out:
    622 	vput(dvp);
    623 	return rv;
    624 }
    625 
    626 static int
    627 rump_vop_open(void *v)
    628 {
    629 	struct vop_open_args /* {
    630 		struct vnode *a_vp;
    631 		int a_mode;
    632 		kauth_cred_t a_cred;
    633 	} */ *ap = v;
    634 	struct vnode *vp = ap->a_vp;
    635 	struct rumpfs_node *rn = vp->v_data;
    636 	int mode = ap->a_mode;
    637 	int error = EINVAL;
    638 
    639 	if (vp->v_type != VREG)
    640 		return 0;
    641 
    642 	if (mode & FREAD) {
    643 		if (rn->rn_readfd != -1)
    644 			return 0;
    645 		rn->rn_readfd = rumpuser_open(rn->rn_hostpath,
    646 		    O_RDONLY, &error);
    647 	} else if (mode & FWRITE) {
    648 		if (rn->rn_writefd != -1)
    649 			return 0;
    650 		rn->rn_writefd = rumpuser_open(rn->rn_hostpath,
    651 		    O_WRONLY, &error);
    652 	}
    653 
    654 	return error;
    655 }
    656 
    657 static int
    658 rump_vop_read(void *v)
    659 {
    660 	struct vop_read_args /* {
    661 		struct vnode *a_vp;
    662 		struct uio *a_uio;
    663 		int ioflags a_ioflag;
    664 		kauth_cred_t a_cred;
    665 	}; */ *ap = v;
    666 	struct vnode *vp = ap->a_vp;
    667 	struct rumpfs_node *rn = vp->v_data;
    668 	struct uio *uio = ap->a_uio;
    669 	uint8_t *buf;
    670 	size_t bufsize;
    671 	int error = 0;
    672 
    673 	bufsize = uio->uio_resid;
    674 	buf = kmem_alloc(bufsize, KM_SLEEP);
    675 	if (rumpuser_pread(rn->rn_readfd, buf, bufsize,
    676 	    uio->uio_offset + rn->rn_offset, &error) == -1)
    677 		goto out;
    678 	error = uiomove(buf, bufsize, uio);
    679 
    680  out:
    681 	kmem_free(buf, bufsize);
    682 	return error;
    683 }
    684 
    685 static int
    686 rump_vop_write(void *v)
    687 {
    688 	struct vop_read_args /* {
    689 		struct vnode *a_vp;
    690 		struct uio *a_uio;
    691 		int ioflags a_ioflag;
    692 		kauth_cred_t a_cred;
    693 	}; */ *ap = v;
    694 	struct vnode *vp = ap->a_vp;
    695 	struct rumpfs_node *rn = vp->v_data;
    696 	struct uio *uio = ap->a_uio;
    697 	uint8_t *buf;
    698 	size_t bufsize;
    699 	int error = 0;
    700 
    701 	bufsize = uio->uio_resid;
    702 	buf = kmem_alloc(bufsize, KM_SLEEP);
    703 	error = uiomove(buf, bufsize, uio);
    704 	if (error)
    705 		goto out;
    706 	KASSERT(uio->uio_resid == 0);
    707 	rumpuser_pwrite(rn->rn_writefd, buf, bufsize,
    708 	    uio->uio_offset + rn->rn_offset, &error);
    709 
    710  out:
    711 	kmem_free(buf, bufsize);
    712 	return error;
    713 }
    714 
    715 static int
    716 rump_vop_success(void *v)
    717 {
    718 
    719 	return 0;
    720 }
    721 
    722 static int
    723 rump_vop_inactive(void *v)
    724 {
    725 	struct vop_inactive_args *ap = v;
    726 	struct vnode *vp = ap->a_vp;
    727 	struct rumpfs_node *rn = vp->v_data;
    728 	int error;
    729 
    730 	if (vp->v_type == VREG) {
    731 		if (rn->rn_readfd != -1) {
    732 			rumpuser_close(rn->rn_readfd, &error);
    733 			rn->rn_readfd = -1;
    734 		}
    735 		if (rn->rn_writefd != -1) {
    736 			rumpuser_close(rn->rn_writefd, &error);
    737 			rn->rn_writefd = -1;
    738 		}
    739 	}
    740 
    741 	VOP_UNLOCK(vp, 0);
    742 	return 0;
    743 }
    744 
    745 static int
    746 rump_vop_reclaim(void *v)
    747 {
    748 	struct vop_reclaim_args /* {
    749 		struct vnode *a_vp;
    750 	} */ *ap = v;
    751 	struct vnode *vp = ap->a_vp;
    752 	struct rumpfs_node *rn = vp->v_data;
    753 
    754 	mutex_enter(&reclock);
    755 	rn->rn_vp = NULL;
    756 	mutex_exit(&reclock);
    757 	vp->v_data = NULL;
    758 
    759 	return 0;
    760 }
    761 
    762 static int
    763 rump_vop_spec(void *v)
    764 {
    765 	struct vop_generic_args *ap = v;
    766 	int (**opvec)(void *);
    767 
    768 	switch (ap->a_desc->vdesc_offset) {
    769 	case VOP_ACCESS_DESCOFFSET:
    770 	case VOP_GETATTR_DESCOFFSET:
    771 	case VOP_LOCK_DESCOFFSET:
    772 	case VOP_UNLOCK_DESCOFFSET:
    773 		opvec = rump_vnodeop_p;
    774 		break;
    775 	default:
    776 		opvec = spec_vnodeop_p;
    777 		break;
    778 	}
    779 
    780 	return VOCALL(opvec, ap->a_desc->vdesc_offset, v);
    781 }
    782 
    783 /*
    784  * Begin vfs-level stuff
    785  */
    786 
    787 VFS_PROTOS(rumpfs);
    788 struct vfsops rumpfs_vfsops = {
    789 	.vfs_name =		MOUNT_RUMPFS,
    790 	.vfs_min_mount_data = 	0,
    791 	.vfs_mount =		rumpfs_mount,
    792 	.vfs_start =		(void *)nullop,
    793 	.vfs_unmount = 		rumpfs_unmount,
    794 	.vfs_root =		rumpfs_root,
    795 	.vfs_quotactl =		(void *)eopnotsupp,
    796 	.vfs_statvfs =		genfs_statvfs,
    797 	.vfs_sync =		(void *)nullop,
    798 	.vfs_vget =		rumpfs_vget,
    799 	.vfs_fhtovp =		(void *)eopnotsupp,
    800 	.vfs_vptofh =		(void *)eopnotsupp,
    801 	.vfs_init =		rumpfs_init,
    802 	.vfs_reinit =		NULL,
    803 	.vfs_done =		rumpfs_done,
    804 	.vfs_mountroot =	rumpfs_mountroot,
    805 	.vfs_snapshot =		(void *)eopnotsupp,
    806 	.vfs_extattrctl =	(void *)eopnotsupp,
    807 	.vfs_suspendctl =	(void *)eopnotsupp,
    808 	.vfs_opv_descs =	rump_opv_descs,
    809 	/* vfs_refcount */
    810 	/* vfs_list */
    811 };
    812 
    813 int
    814 rumpfs_mount(struct mount *mp, const char *mntpath, void *arg, size_t *alen)
    815 {
    816 
    817 	return EOPNOTSUPP;
    818 }
    819 
    820 int
    821 rumpfs_unmount(struct mount *mp, int flags)
    822 {
    823 
    824 	/* if going for it, just lie about it */
    825 	if (panicstr)
    826 		return 0;
    827 
    828 	return EOPNOTSUPP; /* ;) */
    829 }
    830 
    831 int
    832 rumpfs_root(struct mount *mp, struct vnode **vpp)
    833 {
    834 	struct rumpfs_mount *rfsmp = mp->mnt_data;
    835 
    836 	vget(rfsmp->rfsmp_rvp, LK_EXCLUSIVE | LK_RETRY);
    837 	*vpp = rfsmp->rfsmp_rvp;
    838 	return 0;
    839 }
    840 
    841 int
    842 rumpfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
    843 {
    844 
    845 	return EOPNOTSUPP;
    846 }
    847 
    848 void
    849 rumpfs_init()
    850 {
    851 
    852 	CTASSERT(RUMP_ETFS_SIZE_ENDOFF == RUMPBLK_SIZENOTSET);
    853 
    854 	mutex_init(&reclock, MUTEX_DEFAULT, IPL_NONE);
    855 	mutex_init(&etfs_lock, MUTEX_DEFAULT, IPL_NONE);
    856 }
    857 
    858 void
    859 rumpfs_done()
    860 {
    861 
    862 	mutex_destroy(&reclock);
    863 	mutex_destroy(&etfs_lock);
    864 }
    865 
    866 int
    867 rumpfs_mountroot()
    868 {
    869 	struct mount *mp;
    870 	struct rumpfs_mount *rfsmp;
    871 	struct rumpfs_node *rn;
    872 	int error;
    873 
    874 	if ((error = vfs_rootmountalloc(MOUNT_RUMPFS, "rootdev", &mp)) != 0) {
    875 		vrele(rootvp);
    876 		return error;
    877 	}
    878 
    879 	rfsmp = kmem_alloc(sizeof(*rfsmp), KM_SLEEP);
    880 
    881 	rn = makeprivate(VDIR, NODEV, DEV_BSIZE);
    882 	mutex_enter(&reclock);
    883 	error = makevnode(mp, rn, &rfsmp->rfsmp_rvp);
    884 	mutex_exit(&reclock);
    885 	if (error)
    886 		panic("could not create root vnode: %d", error);
    887 	rfsmp->rfsmp_rvp->v_vflag |= VV_ROOT;
    888 	VOP_UNLOCK(rfsmp->rfsmp_rvp, 0);
    889 
    890 	mutex_enter(&mountlist_lock);
    891 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    892 	mutex_exit(&mountlist_lock);
    893 
    894 	mp->mnt_data = rfsmp;
    895 	mp->mnt_stat.f_namemax = MAXNAMLEN;
    896 	mp->mnt_stat.f_iosize = 512;
    897 	mp->mnt_flag |= MNT_LOCAL;
    898 	mp->mnt_iflag |= IMNT_MPSAFE;
    899 	vfs_getnewfsid(mp);
    900 
    901 	error = set_statvfs_info("/", UIO_SYSSPACE, "rumpfs", UIO_SYSSPACE,
    902 	    mp->mnt_op->vfs_name, mp, curlwp);
    903 	if (error)
    904 		panic("set statvfsinfo for rootfs failed");
    905 
    906 	vfs_unbusy(mp, false, NULL);
    907 
    908 	return 0;
    909 }
    910