Home | History | Annotate | Line # | Download | only in rumpvfs
rumpfs.c revision 1.89
      1 /*	$NetBSD: rumpfs.c,v 1.89 2011/01/14 11:07:42 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2009, 2010 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.89 2011/01/14 11:07:42 pooka Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/atomic.h>
     33 #include <sys/buf.h>
     34 #include <sys/dirent.h>
     35 #include <sys/errno.h>
     36 #include <sys/filedesc.h>
     37 #include <sys/fcntl.h>
     38 #include <sys/kauth.h>
     39 #include <sys/malloc.h>
     40 #include <sys/module.h>
     41 #include <sys/mount.h>
     42 #include <sys/namei.h>
     43 #include <sys/lock.h>
     44 #include <sys/lockf.h>
     45 #include <sys/queue.h>
     46 #include <sys/stat.h>
     47 #include <sys/syscallargs.h>
     48 #include <sys/vnode.h>
     49 #include <sys/unistd.h>
     50 
     51 #include <miscfs/fifofs/fifo.h>
     52 #include <miscfs/specfs/specdev.h>
     53 #include <miscfs/genfs/genfs.h>
     54 #include <miscfs/genfs/genfs_node.h>
     55 
     56 #include <uvm/uvm_extern.h>
     57 
     58 #include <rump/rumpuser.h>
     59 
     60 #include "rump_private.h"
     61 #include "rump_vfs_private.h"
     62 
     63 static int rump_vop_lookup(void *);
     64 static int rump_vop_getattr(void *);
     65 static int rump_vop_setattr(void *);
     66 static int rump_vop_mkdir(void *);
     67 static int rump_vop_rmdir(void *);
     68 static int rump_vop_remove(void *);
     69 static int rump_vop_mknod(void *);
     70 static int rump_vop_create(void *);
     71 static int rump_vop_inactive(void *);
     72 static int rump_vop_reclaim(void *);
     73 static int rump_vop_success(void *);
     74 static int rump_vop_readdir(void *);
     75 static int rump_vop_spec(void *);
     76 static int rump_vop_read(void *);
     77 static int rump_vop_write(void *);
     78 static int rump_vop_open(void *);
     79 static int rump_vop_symlink(void *);
     80 static int rump_vop_readlink(void *);
     81 static int rump_vop_whiteout(void *);
     82 static int rump_vop_pathconf(void *);
     83 static int rump_vop_bmap(void *);
     84 static int rump_vop_strategy(void *);
     85 static int rump_vop_advlock(void *);
     86 static int rump_vop_access(void *);
     87 
     88 int (**fifo_vnodeop_p)(void *);
     89 const struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
     90 	{ &vop_default_desc, vn_default_error },
     91 	{ NULL, NULL }
     92 };
     93 const struct vnodeopv_desc fifo_vnodeop_opv_desc =
     94 	{ &fifo_vnodeop_p, fifo_vnodeop_entries };
     95 
     96 int (**rump_vnodeop_p)(void *);
     97 const struct vnodeopv_entry_desc rump_vnodeop_entries[] = {
     98 	{ &vop_default_desc, vn_default_error },
     99 	{ &vop_lookup_desc, rump_vop_lookup },
    100 	{ &vop_getattr_desc, rump_vop_getattr },
    101 	{ &vop_setattr_desc, rump_vop_setattr },
    102 	{ &vop_mkdir_desc, rump_vop_mkdir },
    103 	{ &vop_rmdir_desc, rump_vop_rmdir },
    104 	{ &vop_remove_desc, rump_vop_remove },
    105 	{ &vop_mknod_desc, rump_vop_mknod },
    106 	{ &vop_create_desc, rump_vop_create },
    107 	{ &vop_symlink_desc, rump_vop_symlink },
    108 	{ &vop_readlink_desc, rump_vop_readlink },
    109 	{ &vop_access_desc, rump_vop_access },
    110 	{ &vop_readdir_desc, rump_vop_readdir },
    111 	{ &vop_read_desc, rump_vop_read },
    112 	{ &vop_write_desc, rump_vop_write },
    113 	{ &vop_open_desc, rump_vop_open },
    114 	{ &vop_close_desc, genfs_nullop },
    115 	{ &vop_seek_desc, genfs_seek },
    116 	{ &vop_getpages_desc, genfs_getpages },
    117 	{ &vop_putpages_desc, genfs_putpages },
    118 	{ &vop_whiteout_desc, rump_vop_whiteout },
    119 	{ &vop_fsync_desc, rump_vop_success },
    120 	{ &vop_lock_desc, genfs_lock },
    121 	{ &vop_unlock_desc, genfs_unlock },
    122 	{ &vop_islocked_desc, genfs_islocked },
    123 	{ &vop_inactive_desc, rump_vop_inactive },
    124 	{ &vop_reclaim_desc, rump_vop_reclaim },
    125 	{ &vop_link_desc, genfs_eopnotsupp },
    126 	{ &vop_pathconf_desc, rump_vop_pathconf },
    127 	{ &vop_bmap_desc, rump_vop_bmap },
    128 	{ &vop_strategy_desc, rump_vop_strategy },
    129 	{ &vop_advlock_desc, rump_vop_advlock },
    130 	{ NULL, NULL }
    131 };
    132 const struct vnodeopv_desc rump_vnodeop_opv_desc =
    133 	{ &rump_vnodeop_p, rump_vnodeop_entries };
    134 
    135 int (**rump_specop_p)(void *);
    136 const struct vnodeopv_entry_desc rump_specop_entries[] = {
    137 	{ &vop_default_desc, rump_vop_spec },
    138 	{ NULL, NULL }
    139 };
    140 const struct vnodeopv_desc rump_specop_opv_desc =
    141 	{ &rump_specop_p, rump_specop_entries };
    142 
    143 const struct vnodeopv_desc * const rump_opv_descs[] = {
    144 	&rump_vnodeop_opv_desc,
    145 	&rump_specop_opv_desc,
    146 	NULL
    147 };
    148 
    149 #define RUMPFS_WHITEOUT ((void *)-1)
    150 #define RDENT_ISWHITEOUT(rdp) (rdp->rd_node == RUMPFS_WHITEOUT)
    151 struct rumpfs_dent {
    152 	char *rd_name;
    153 	int rd_namelen;
    154 	struct rumpfs_node *rd_node;
    155 
    156 	LIST_ENTRY(rumpfs_dent) rd_entries;
    157 };
    158 
    159 struct genfs_ops rumpfs_genfsops = {
    160 	.gop_size = genfs_size,
    161 	.gop_write = genfs_gop_write,
    162 
    163 	/* optional */
    164 	.gop_alloc = NULL,
    165 	.gop_markupdate = NULL,
    166 };
    167 
    168 struct rumpfs_node {
    169 	struct genfs_node rn_gn;
    170 	struct vattr rn_va;
    171 	struct vnode *rn_vp;
    172 	char *rn_hostpath;
    173 	int rn_flags;
    174 	struct lockf *rn_lockf;
    175 
    176 	union {
    177 		struct {		/* VREG */
    178 			int readfd;
    179 			int writefd;
    180 			uint64_t offset;
    181 		} reg;
    182 		struct {
    183 			void *data;
    184 			size_t dlen;
    185 		} reg_noet;
    186 		struct {		/* VDIR */
    187 			LIST_HEAD(, rumpfs_dent) dents;
    188 			struct rumpfs_node *parent;
    189 			int flags;
    190 		} dir;
    191 		struct {
    192 			char *target;
    193 			size_t len;
    194 		} link;
    195 	} rn_u;
    196 };
    197 #define rn_readfd	rn_u.reg.readfd
    198 #define rn_writefd	rn_u.reg.writefd
    199 #define rn_offset	rn_u.reg.offset
    200 #define rn_data		rn_u.reg_noet.data
    201 #define rn_dlen		rn_u.reg_noet.dlen
    202 #define rn_dir		rn_u.dir.dents
    203 #define rn_parent	rn_u.dir.parent
    204 #define rn_linktarg	rn_u.link.target
    205 #define rn_linklen	rn_u.link.len
    206 
    207 #define RUMPNODE_CANRECLAIM	0x01
    208 #define RUMPNODE_DIR_ET		0x02
    209 #define RUMPNODE_DIR_ETSUBS	0x04
    210 #define RUMPNODE_ET_PHONE_HOST	0x10
    211 
    212 struct rumpfs_mount {
    213 	struct vnode *rfsmp_rvp;
    214 };
    215 
    216 #define INO_WHITEOUT 1
    217 static int lastino = 2;
    218 static kmutex_t reclock;
    219 
    220 static struct rumpfs_node *makeprivate(enum vtype, dev_t, off_t, bool);
    221 
    222 /*
    223  * Extra Terrestrial stuff.  We map a given key (pathname) to a file on
    224  * the host FS.  ET phones home only from the root node of rumpfs.
    225  *
    226  * When an etfs node is removed, a vnode potentially behind it is not
    227  * immediately recycled.
    228  */
    229 
    230 struct etfs {
    231 	char et_key[MAXPATHLEN];
    232 	size_t et_keylen;
    233 	bool et_prefixkey;
    234 	bool et_removing;
    235 	devminor_t et_blkmin;
    236 
    237 	LIST_ENTRY(etfs) et_entries;
    238 
    239 	struct rumpfs_node *et_rn;
    240 };
    241 static kmutex_t etfs_lock;
    242 static LIST_HEAD(, etfs) etfs_list = LIST_HEAD_INITIALIZER(etfs_list);
    243 
    244 static enum vtype
    245 ettype_to_vtype(enum rump_etfs_type et)
    246 {
    247 	enum vtype vt;
    248 
    249 	switch (et) {
    250 	case RUMP_ETFS_REG:
    251 		vt = VREG;
    252 		break;
    253 	case RUMP_ETFS_BLK:
    254 		vt = VBLK;
    255 		break;
    256 	case RUMP_ETFS_CHR:
    257 		vt = VCHR;
    258 		break;
    259 	case RUMP_ETFS_DIR:
    260 		vt = VDIR;
    261 		break;
    262 	case RUMP_ETFS_DIR_SUBDIRS:
    263 		vt = VDIR;
    264 		break;
    265 	default:
    266 		panic("invalid et type: %d", et);
    267 	}
    268 
    269 	return vt;
    270 }
    271 
    272 static enum vtype
    273 hft_to_vtype(int hft)
    274 {
    275 	enum vtype vt;
    276 
    277 	switch (hft) {
    278 	case RUMPUSER_FT_OTHER:
    279 		vt = VNON;
    280 		break;
    281 	case RUMPUSER_FT_DIR:
    282 		vt = VDIR;
    283 		break;
    284 	case RUMPUSER_FT_REG:
    285 		vt = VREG;
    286 		break;
    287 	case RUMPUSER_FT_BLK:
    288 		vt = VBLK;
    289 		break;
    290 	case RUMPUSER_FT_CHR:
    291 		vt = VCHR;
    292 		break;
    293 	default:
    294 		vt = VNON;
    295 		break;
    296 	}
    297 
    298 	return vt;
    299 }
    300 
    301 static bool
    302 etfs_find(const char *key, struct etfs **etp, bool forceprefix)
    303 {
    304 	struct etfs *et;
    305 	size_t keylen = strlen(key);
    306 
    307 	KASSERT(mutex_owned(&etfs_lock));
    308 
    309 	LIST_FOREACH(et, &etfs_list, et_entries) {
    310 		if ((keylen == et->et_keylen || et->et_prefixkey || forceprefix)
    311 		    && strncmp(key, et->et_key, et->et_keylen) == 0) {
    312 			if (etp)
    313 				*etp = et;
    314 			return true;
    315 		}
    316 	}
    317 
    318 	return false;
    319 }
    320 
    321 #define REGDIR(ftype) \
    322     ((ftype) == RUMP_ETFS_DIR || (ftype) == RUMP_ETFS_DIR_SUBDIRS)
    323 static int
    324 doregister(const char *key, const char *hostpath,
    325 	enum rump_etfs_type ftype, uint64_t begin, uint64_t size)
    326 {
    327 	char buf[9];
    328 	struct etfs *et;
    329 	struct rumpfs_node *rn;
    330 	uint64_t fsize;
    331 	dev_t rdev = NODEV;
    332 	devminor_t dmin = -1;
    333 	int hft, error;
    334 
    335 	if (key[0] != '/') {
    336 		return EINVAL;
    337 	}
    338 	while (key[0] == '/') {
    339 		key++;
    340 	}
    341 
    342 	if (rumpuser_getfileinfo(hostpath, &fsize, &hft, &error))
    343 		return error;
    344 
    345 	/* etfs directory requires a directory on the host */
    346 	if (REGDIR(ftype)) {
    347 		if (hft != RUMPUSER_FT_DIR)
    348 			return ENOTDIR;
    349 		if (begin != 0)
    350 			return EISDIR;
    351 		if (size != RUMP_ETFS_SIZE_ENDOFF)
    352 			return EISDIR;
    353 		size = fsize;
    354 	} else {
    355 		if (begin > fsize)
    356 			return EINVAL;
    357 		if (size == RUMP_ETFS_SIZE_ENDOFF)
    358 			size = fsize - begin;
    359 		if (begin + size > fsize)
    360 			return EINVAL;
    361 	}
    362 
    363 	if (ftype == RUMP_ETFS_BLK || ftype == RUMP_ETFS_CHR) {
    364 		error = rumpblk_register(hostpath, &dmin, begin, size);
    365 		if (error != 0) {
    366 			return error;
    367 		}
    368 		rdev = makedev(RUMPBLK_DEVMAJOR, dmin);
    369 	}
    370 
    371 	et = kmem_alloc(sizeof(*et), KM_SLEEP);
    372 	strcpy(et->et_key, key);
    373 	et->et_keylen = strlen(et->et_key);
    374 	et->et_rn = rn = makeprivate(ettype_to_vtype(ftype), rdev, size, true);
    375 	et->et_removing = false;
    376 	et->et_blkmin = dmin;
    377 
    378 	rn->rn_flags |= RUMPNODE_ET_PHONE_HOST;
    379 
    380 	if (ftype == RUMP_ETFS_REG || REGDIR(ftype) || et->et_blkmin != -1) {
    381 		size_t len = strlen(hostpath)+1;
    382 
    383 		rn->rn_hostpath = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
    384 		memcpy(rn->rn_hostpath, hostpath, len);
    385 		rn->rn_offset = begin;
    386 	}
    387 
    388 	if (REGDIR(ftype)) {
    389 		rn->rn_flags |= RUMPNODE_DIR_ET;
    390 		et->et_prefixkey = true;
    391 	} else {
    392 		et->et_prefixkey = false;
    393 	}
    394 
    395 	if (ftype == RUMP_ETFS_DIR_SUBDIRS)
    396 		rn->rn_flags |= RUMPNODE_DIR_ETSUBS;
    397 
    398 	mutex_enter(&etfs_lock);
    399 	if (etfs_find(key, NULL, REGDIR(ftype))) {
    400 		mutex_exit(&etfs_lock);
    401 		if (et->et_blkmin != -1)
    402 			rumpblk_deregister(hostpath);
    403 		if (et->et_rn->rn_hostpath != NULL)
    404 			free(et->et_rn->rn_hostpath, M_TEMP);
    405 		kmem_free(et->et_rn, sizeof(*et->et_rn));
    406 		kmem_free(et, sizeof(*et));
    407 		return EEXIST;
    408 	}
    409 	LIST_INSERT_HEAD(&etfs_list, et, et_entries);
    410 	mutex_exit(&etfs_lock);
    411 
    412 	if (ftype == RUMP_ETFS_BLK) {
    413 		format_bytes(buf, sizeof(buf), size);
    414 		aprint_verbose("/%s: hostpath %s (%s)\n", key, hostpath, buf);
    415 	}
    416 
    417 	return 0;
    418 }
    419 #undef REGDIR
    420 
    421 int
    422 rump_etfs_register(const char *key, const char *hostpath,
    423 	enum rump_etfs_type ftype)
    424 {
    425 
    426 	return doregister(key, hostpath, ftype, 0, RUMP_ETFS_SIZE_ENDOFF);
    427 }
    428 
    429 int
    430 rump_etfs_register_withsize(const char *key, const char *hostpath,
    431 	enum rump_etfs_type ftype, uint64_t begin, uint64_t size)
    432 {
    433 
    434 	return doregister(key, hostpath, ftype, begin, size);
    435 }
    436 
    437 /* remove etfs mapping.  caller's responsibility to make sure it's not in use */
    438 int
    439 rump_etfs_remove(const char *key)
    440 {
    441 	struct etfs *et;
    442 	size_t keylen;
    443 	int rv;
    444 
    445 	if (key[0] != '/') {
    446 		return EINVAL;
    447 	}
    448 	while (key[0] == '/') {
    449 		key++;
    450 	}
    451 
    452 	keylen = strlen(key);
    453 
    454 	mutex_enter(&etfs_lock);
    455 	LIST_FOREACH(et, &etfs_list, et_entries) {
    456 		if (keylen == et->et_keylen && strcmp(et->et_key, key) == 0) {
    457 			if (et->et_removing)
    458 				et = NULL;
    459 			else
    460 				et->et_removing = true;
    461 			break;
    462 		}
    463 	}
    464 	mutex_exit(&etfs_lock);
    465 	if (!et)
    466 		return ENOENT;
    467 
    468 	/*
    469 	 * ok, we know what we want to remove and have signalled there
    470 	 * actually are men at work.  first, unregister from rumpblk
    471 	 */
    472 	if (et->et_blkmin != -1) {
    473 		rv = rumpblk_deregister(et->et_rn->rn_hostpath);
    474 	} else {
    475 		rv = 0;
    476 	}
    477 	KASSERT(rv == 0);
    478 
    479 	/* then do the actual removal */
    480 	mutex_enter(&etfs_lock);
    481 	LIST_REMOVE(et, et_entries);
    482 	mutex_exit(&etfs_lock);
    483 
    484 	/* node is unreachable, safe to nuke all device copies */
    485 	if (et->et_blkmin != -1) {
    486 		vdevgone(RUMPBLK_DEVMAJOR, et->et_blkmin, et->et_blkmin, VBLK);
    487 	} else {
    488 		struct vnode *vp;
    489 
    490 		mutex_enter(&reclock);
    491 		if ((vp = et->et_rn->rn_vp) != NULL)
    492 			mutex_enter(&vp->v_interlock);
    493 		mutex_exit(&reclock);
    494 		if (vp && vget(vp, 0) == 0)
    495 			vgone(vp);
    496 	}
    497 
    498 	if (et->et_rn->rn_hostpath != NULL)
    499 		free(et->et_rn->rn_hostpath, M_TEMP);
    500 	kmem_free(et->et_rn, sizeof(*et->et_rn));
    501 	kmem_free(et, sizeof(*et));
    502 
    503 	return 0;
    504 }
    505 
    506 /*
    507  * rumpfs
    508  */
    509 
    510 static struct rumpfs_node *
    511 makeprivate(enum vtype vt, dev_t rdev, off_t size, bool et)
    512 {
    513 	struct rumpfs_node *rn;
    514 	struct vattr *va;
    515 	struct timespec ts;
    516 
    517 	rn = kmem_zalloc(sizeof(*rn), KM_SLEEP);
    518 
    519 	switch (vt) {
    520 	case VDIR:
    521 		LIST_INIT(&rn->rn_dir);
    522 		break;
    523 	case VREG:
    524 		if (et) {
    525 			rn->rn_readfd = -1;
    526 			rn->rn_writefd = -1;
    527 		}
    528 		break;
    529 	default:
    530 		break;
    531 	}
    532 
    533 	nanotime(&ts);
    534 
    535 	va = &rn->rn_va;
    536 	va->va_type = vt;
    537 	va->va_mode = 0755;
    538 	if (vt == VDIR)
    539 		va->va_nlink = 2;
    540 	else
    541 		va->va_nlink = 1;
    542 	va->va_uid = 0;
    543 	va->va_gid = 0;
    544 	va->va_fsid =
    545 	va->va_fileid = atomic_inc_uint_nv(&lastino);
    546 	va->va_size = size;
    547 	va->va_blocksize = 512;
    548 	va->va_atime = ts;
    549 	va->va_mtime = ts;
    550 	va->va_ctime = ts;
    551 	va->va_birthtime = ts;
    552 	va->va_gen = 0;
    553 	va->va_flags = 0;
    554 	va->va_rdev = rdev;
    555 	va->va_bytes = 512;
    556 	va->va_filerev = 0;
    557 	va->va_vaflags = 0;
    558 
    559 	return rn;
    560 }
    561 
    562 static int
    563 makevnode(struct mount *mp, struct rumpfs_node *rn, struct vnode **vpp)
    564 {
    565 	struct vnode *vp;
    566 	int (**vpops)(void *);
    567 	struct vattr *va = &rn->rn_va;
    568 	int rv;
    569 
    570 	KASSERT(!mutex_owned(&reclock));
    571 
    572 	if (va->va_type == VCHR || va->va_type == VBLK) {
    573 		vpops = rump_specop_p;
    574 	} else {
    575 		vpops = rump_vnodeop_p;
    576 	}
    577 
    578 	rv = getnewvnode(VT_RUMP, mp, vpops, &vp);
    579 	if (rv)
    580 		return rv;
    581 
    582 	vp->v_size = vp->v_writesize = va->va_size;
    583 	vp->v_type = va->va_type;
    584 
    585 	if (vpops == rump_specop_p) {
    586 		spec_node_init(vp, va->va_rdev);
    587 	}
    588 	vp->v_data = rn;
    589 
    590 	genfs_node_init(vp, &rumpfs_genfsops);
    591 	vn_lock(vp, LK_RETRY | LK_EXCLUSIVE);
    592 	mutex_enter(&reclock);
    593 	rn->rn_vp = vp;
    594 	mutex_exit(&reclock);
    595 
    596 	*vpp = vp;
    597 
    598 	return 0;
    599 }
    600 
    601 
    602 static void
    603 makedir(struct rumpfs_node *rnd,
    604 	struct componentname *cnp, struct rumpfs_node *rn)
    605 {
    606 	struct rumpfs_dent *rdent;
    607 
    608 	rdent = kmem_alloc(sizeof(*rdent), KM_SLEEP);
    609 	rdent->rd_name = kmem_alloc(cnp->cn_namelen+1, KM_SLEEP);
    610 	rdent->rd_node = rn;
    611 	strlcpy(rdent->rd_name, cnp->cn_nameptr, cnp->cn_namelen+1);
    612 	rdent->rd_namelen = strlen(rdent->rd_name);
    613 
    614 	LIST_INSERT_HEAD(&rnd->rn_dir, rdent, rd_entries);
    615 }
    616 
    617 static void
    618 freedir(struct rumpfs_node *rnd, struct componentname *cnp)
    619 {
    620 	struct rumpfs_dent *rd = NULL;
    621 
    622 	LIST_FOREACH(rd, &rnd->rn_dir, rd_entries) {
    623 		if (rd->rd_namelen == cnp->cn_namelen &&
    624 		    strncmp(rd->rd_name, cnp->cn_nameptr,
    625 		            cnp->cn_namelen) == 0)
    626 			break;
    627 	}
    628 	if (rd == NULL)
    629 		panic("could not find directory entry: %s", cnp->cn_nameptr);
    630 
    631 	if (cnp->cn_flags & DOWHITEOUT) {
    632 		rd->rd_node = RUMPFS_WHITEOUT;
    633 	} else {
    634 		LIST_REMOVE(rd, rd_entries);
    635 		kmem_free(rd->rd_name, rd->rd_namelen+1);
    636 		kmem_free(rd, sizeof(*rd));
    637 	}
    638 }
    639 
    640 /*
    641  * Simple lookup for rump file systems.
    642  *
    643  * uhm, this is twisted.  C F C C, hope of C C F C looming
    644  */
    645 static int
    646 rump_vop_lookup(void *v)
    647 {
    648 	struct vop_lookup_args /* {
    649 		struct vnode *a_dvp;
    650 		struct vnode **a_vpp;
    651 		struct componentname *a_cnp;
    652 	}; */ *ap = v;
    653 	struct componentname *cnp = ap->a_cnp;
    654 	struct vnode *dvp = ap->a_dvp;
    655 	struct vnode **vpp = ap->a_vpp;
    656 	struct vnode *vp;
    657 	struct rumpfs_node *rnd = dvp->v_data, *rn;
    658 	struct rumpfs_dent *rd = NULL;
    659 	struct etfs *et;
    660 	bool dotdot = (cnp->cn_flags & ISDOTDOT) != 0;
    661 	int rv = 0;
    662 
    663 	*vpp = NULL;
    664 
    665 	if ((cnp->cn_flags & ISLASTCN)
    666 	    && (dvp->v_mount->mnt_flag & MNT_RDONLY)
    667 	    && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
    668 		return EROFS;
    669 
    670 	/* check for dot, return directly if the case */
    671 	if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
    672 		vref(dvp);
    673 		*vpp = dvp;
    674 		return 0;
    675 	}
    676 
    677 	/* we don't do rename */
    678 	if (!(((cnp->cn_flags & ISLASTCN) == 0) || (cnp->cn_nameiop != RENAME)))
    679 		return EOPNOTSUPP;
    680 
    681 	/* check for etfs */
    682 	if (dvp == rootvnode && cnp->cn_nameiop == LOOKUP) {
    683 		bool found;
    684 		mutex_enter(&etfs_lock);
    685 		found = etfs_find(cnp->cn_nameptr, &et, false);
    686 		mutex_exit(&etfs_lock);
    687 
    688 		if (found) {
    689 			rn = et->et_rn;
    690 			cnp->cn_consume += et->et_keylen - cnp->cn_namelen;
    691 			if (rn->rn_va.va_type != VDIR)
    692 				cnp->cn_flags &= ~REQUIREDIR;
    693 			goto getvnode;
    694 		}
    695 	}
    696 
    697 	if (rnd->rn_flags & RUMPNODE_DIR_ET) {
    698 		uint64_t fsize;
    699 		char *newpath;
    700 		size_t newpathlen;
    701 		int hft, error;
    702 
    703 		if (dotdot)
    704 			return EOPNOTSUPP;
    705 
    706 		newpathlen = strlen(rnd->rn_hostpath) + 1 + cnp->cn_namelen + 1;
    707 		newpath = malloc(newpathlen, M_TEMP, M_WAITOK);
    708 
    709 		strlcpy(newpath, rnd->rn_hostpath, newpathlen);
    710 		strlcat(newpath, "/", newpathlen);
    711 		strlcat(newpath, cnp->cn_nameptr, newpathlen);
    712 
    713 		if (rumpuser_getfileinfo(newpath, &fsize, &hft, &error)) {
    714 			free(newpath, M_TEMP);
    715 			return error;
    716 		}
    717 
    718 		/* allow only dirs and regular files */
    719 		if (hft != RUMPUSER_FT_REG && hft != RUMPUSER_FT_DIR) {
    720 			free(newpath, M_TEMP);
    721 			return ENOENT;
    722 		}
    723 
    724 		rn = makeprivate(hft_to_vtype(hft), NODEV, fsize, true);
    725 		rn->rn_flags |= RUMPNODE_CANRECLAIM;
    726 		if (rnd->rn_flags & RUMPNODE_DIR_ETSUBS) {
    727 			rn->rn_flags |= RUMPNODE_DIR_ET | RUMPNODE_DIR_ETSUBS;
    728 			rn->rn_flags |= RUMPNODE_ET_PHONE_HOST;
    729 		}
    730 		rn->rn_hostpath = newpath;
    731 
    732 		goto getvnode;
    733 	} else {
    734 		if (dotdot) {
    735 			rn = rnd->rn_parent;
    736 			goto getvnode;
    737 		} else {
    738 			LIST_FOREACH(rd, &rnd->rn_dir, rd_entries) {
    739 				if (rd->rd_namelen == cnp->cn_namelen &&
    740 				    strncmp(rd->rd_name, cnp->cn_nameptr,
    741 				      cnp->cn_namelen) == 0)
    742 					break;
    743 			}
    744 		}
    745 	}
    746 
    747 	if (!rd && ((cnp->cn_flags & ISLASTCN) == 0||cnp->cn_nameiop != CREATE))
    748 		return ENOENT;
    749 
    750 	if (!rd && (cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
    751 		if (dvp->v_mount->mnt_flag & MNT_RDONLY)
    752 			return EROFS;
    753 		return EJUSTRETURN;
    754 	}
    755 
    756 	if (RDENT_ISWHITEOUT(rd)) {
    757 		cnp->cn_flags |= ISWHITEOUT;
    758 		return ENOENT;
    759 	}
    760 
    761 	rn = rd->rd_node;
    762 
    763  getvnode:
    764 	KASSERT(rn);
    765 	if (dotdot)
    766 		VOP_UNLOCK(dvp);
    767 	mutex_enter(&reclock);
    768 	if ((vp = rn->rn_vp)) {
    769 		mutex_enter(&vp->v_interlock);
    770 		mutex_exit(&reclock);
    771 		if (vget(vp, LK_EXCLUSIVE)) {
    772 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    773 			goto getvnode;
    774 		}
    775 		*vpp = vp;
    776 	} else {
    777 		mutex_exit(&reclock);
    778 		rv = makevnode(dvp->v_mount, rn, vpp);
    779 	}
    780 	if (dotdot)
    781 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    782 
    783 	return rv;
    784 }
    785 
    786 int
    787 rump_vop_access(void *v)
    788 {
    789 	struct vop_access_args /* {
    790 		const struct vnodeop_desc *a_desc;
    791 		struct vnode *a_vp;
    792 		int a_mode;
    793 		kauth_cred_t a_cred;
    794 	} */ *ap = v;
    795 	struct vnode *vp = ap->a_vp;
    796 	int mode = ap->a_mode;
    797 
    798 	if (mode & VWRITE) {
    799 		switch (vp->v_type) {
    800 		case VDIR:
    801 		case VLNK:
    802 		case VREG:
    803 			if ((vp->v_mount->mnt_flag & MNT_RDONLY))
    804 				return EROFS;
    805 			break;
    806 		default:
    807 			break;
    808 		}
    809 	}
    810 
    811 	return 0;
    812 }
    813 
    814 static int
    815 rump_vop_getattr(void *v)
    816 {
    817 	struct vop_getattr_args /* {
    818 		struct vnode *a_vp;
    819 		struct vattr *a_vap;
    820 		kauth_cred_t a_cred;
    821 	} */ *ap = v;
    822 	struct vnode *vp = ap->a_vp;
    823 	struct rumpfs_node *rn = vp->v_data;
    824 	struct vattr *vap = ap->a_vap;
    825 
    826 	memcpy(vap, &rn->rn_va, sizeof(struct vattr));
    827 	vap->va_size = vp->v_size;
    828 	return 0;
    829 }
    830 
    831 static int
    832 rump_vop_setattr(void *v)
    833 {
    834 	struct vop_getattr_args /* {
    835 		struct vnode *a_vp;
    836 		struct vattr *a_vap;
    837 		kauth_cred_t a_cred;
    838 	} */ *ap = v;
    839 	struct vnode *vp = ap->a_vp;
    840 	struct vattr *vap = ap->a_vap;
    841 	struct rumpfs_node *rn = vp->v_data;
    842 
    843 #define SETIFVAL(a,t) if (vap->a != (t)VNOVAL) rn->rn_va.a = vap->a
    844 	SETIFVAL(va_mode, mode_t);
    845 	SETIFVAL(va_uid, uid_t);
    846 	SETIFVAL(va_gid, gid_t);
    847 	SETIFVAL(va_atime.tv_sec, time_t);
    848 	SETIFVAL(va_ctime.tv_sec, time_t);
    849 	SETIFVAL(va_mtime.tv_sec, time_t);
    850 	SETIFVAL(va_birthtime.tv_sec, time_t);
    851 	SETIFVAL(va_atime.tv_nsec, long);
    852 	SETIFVAL(va_ctime.tv_nsec, long);
    853 	SETIFVAL(va_mtime.tv_nsec, long);
    854 	SETIFVAL(va_birthtime.tv_nsec, long);
    855 	SETIFVAL(va_flags, u_long);
    856 #undef  SETIFVAL
    857 
    858 	if (vp->v_type == VREG && vap->va_size != VSIZENOTSET)
    859 		uvm_vnp_setsize(vp, vap->va_size);
    860 	return 0;
    861 }
    862 
    863 static int
    864 rump_vop_mkdir(void *v)
    865 {
    866 	struct vop_mkdir_args /* {
    867 		struct vnode *a_dvp;
    868 		struct vnode **a_vpp;
    869 		struct componentname *a_cnp;
    870 		struct vattr *a_vap;
    871 	}; */ *ap = v;
    872 	struct vnode *dvp = ap->a_dvp;
    873 	struct vnode **vpp = ap->a_vpp;
    874 	struct componentname *cnp = ap->a_cnp;
    875 	struct rumpfs_node *rnd = dvp->v_data, *rn;
    876 	int rv = 0;
    877 
    878 	rn = makeprivate(VDIR, NODEV, DEV_BSIZE, false);
    879 	rn->rn_parent = rnd;
    880 	rv = makevnode(dvp->v_mount, rn, vpp);
    881 	if (rv)
    882 		goto out;
    883 
    884 	makedir(rnd, cnp, rn);
    885 
    886  out:
    887 	vput(dvp);
    888 	return rv;
    889 }
    890 
    891 static int
    892 rump_vop_rmdir(void *v)
    893 {
    894         struct vop_rmdir_args /* {
    895                 struct vnode *a_dvp;
    896                 struct vnode *a_vp;
    897                 struct componentname *a_cnp;
    898         }; */ *ap = v;
    899 	struct vnode *dvp = ap->a_dvp;
    900 	struct vnode *vp = ap->a_vp;
    901 	struct componentname *cnp = ap->a_cnp;
    902 	struct rumpfs_node *rnd = dvp->v_data;
    903 	struct rumpfs_node *rn = vp->v_data;
    904 	int rv = 0;
    905 
    906 	if (!LIST_EMPTY(&rn->rn_dir)) {
    907 		rv = ENOTEMPTY;
    908 		goto out;
    909 	}
    910 
    911 	freedir(rnd, cnp);
    912 	rn->rn_flags |= RUMPNODE_CANRECLAIM;
    913 
    914 out:
    915 	vput(dvp);
    916 	vput(vp);
    917 
    918 	return rv;
    919 }
    920 
    921 static int
    922 rump_vop_remove(void *v)
    923 {
    924         struct vop_rmdir_args /* {
    925                 struct vnode *a_dvp;
    926                 struct vnode *a_vp;
    927                 struct componentname *a_cnp;
    928         }; */ *ap = v;
    929 	struct vnode *dvp = ap->a_dvp;
    930 	struct vnode *vp = ap->a_vp;
    931 	struct componentname *cnp = ap->a_cnp;
    932 	struct rumpfs_node *rnd = dvp->v_data;
    933 	struct rumpfs_node *rn = vp->v_data;
    934 	int rv = 0;
    935 
    936 	if (rn->rn_flags & RUMPNODE_ET_PHONE_HOST)
    937 		return EOPNOTSUPP;
    938 
    939 	if (vp->v_type == VREG) {
    940 		rump_hyperfree(rn->rn_data, rn->rn_dlen);
    941 	}
    942 
    943 	freedir(rnd, cnp);
    944 	rn->rn_flags |= RUMPNODE_CANRECLAIM;
    945 
    946 	vput(dvp);
    947 	vput(vp);
    948 
    949 	return rv;
    950 }
    951 
    952 static int
    953 rump_vop_mknod(void *v)
    954 {
    955 	struct vop_mknod_args /* {
    956 		struct vnode *a_dvp;
    957 		struct vnode **a_vpp;
    958 		struct componentname *a_cnp;
    959 		struct vattr *a_vap;
    960 	}; */ *ap = v;
    961 	struct vnode *dvp = ap->a_dvp;
    962 	struct vnode **vpp = ap->a_vpp;
    963 	struct componentname *cnp = ap->a_cnp;
    964 	struct vattr *va = ap->a_vap;
    965 	struct rumpfs_node *rnd = dvp->v_data, *rn;
    966 	int rv;
    967 
    968 	rn = makeprivate(va->va_type, va->va_rdev, DEV_BSIZE, false);
    969 	rv = makevnode(dvp->v_mount, rn, vpp);
    970 	if (rv)
    971 		goto out;
    972 
    973 	makedir(rnd, cnp, rn);
    974 
    975  out:
    976 	vput(dvp);
    977 	return rv;
    978 }
    979 
    980 static int
    981 rump_vop_create(void *v)
    982 {
    983 	struct vop_create_args /* {
    984 		struct vnode *a_dvp;
    985 		struct vnode **a_vpp;
    986 		struct componentname *a_cnp;
    987 		struct vattr *a_vap;
    988 	}; */ *ap = v;
    989 	struct vnode *dvp = ap->a_dvp;
    990 	struct vnode **vpp = ap->a_vpp;
    991 	struct componentname *cnp = ap->a_cnp;
    992 	struct vattr *va = ap->a_vap;
    993 	struct rumpfs_node *rnd = dvp->v_data, *rn;
    994 	off_t newsize;
    995 	int rv;
    996 
    997 	newsize = va->va_type == VSOCK ? DEV_BSIZE : 0;
    998 	rn = makeprivate(va->va_type, NODEV, newsize, false);
    999 	rv = makevnode(dvp->v_mount, rn, vpp);
   1000 	if (rv)
   1001 		goto out;
   1002 
   1003 	makedir(rnd, cnp, rn);
   1004 
   1005  out:
   1006 	vput(dvp);
   1007 	return rv;
   1008 }
   1009 
   1010 static int
   1011 rump_vop_symlink(void *v)
   1012 {
   1013 	struct vop_symlink_args /* {
   1014 		struct vnode *a_dvp;
   1015 		struct vnode **a_vpp;
   1016 		struct componentname *a_cnp;
   1017 		struct vattr *a_vap;
   1018 		char *a_target;
   1019 	}; */ *ap = v;
   1020 	struct vnode *dvp = ap->a_dvp;
   1021 	struct vnode **vpp = ap->a_vpp;
   1022 	struct componentname *cnp = ap->a_cnp;
   1023 	struct rumpfs_node *rnd = dvp->v_data, *rn;
   1024 	const char *target = ap->a_target;
   1025 	size_t linklen;
   1026 	int rv;
   1027 
   1028 	linklen = strlen(target);
   1029 	KASSERT(linklen < MAXPATHLEN);
   1030 	rn = makeprivate(VLNK, NODEV, linklen, false);
   1031 	rv = makevnode(dvp->v_mount, rn, vpp);
   1032 	if (rv)
   1033 		goto out;
   1034 
   1035 	makedir(rnd, cnp, rn);
   1036 
   1037 	KASSERT(linklen < MAXPATHLEN);
   1038 	rn->rn_linktarg = PNBUF_GET();
   1039 	rn->rn_linklen = linklen;
   1040 	strcpy(rn->rn_linktarg, target);
   1041 
   1042  out:
   1043 	vput(dvp);
   1044 	return rv;
   1045 }
   1046 
   1047 static int
   1048 rump_vop_readlink(void *v)
   1049 {
   1050 	struct vop_readlink_args /* {
   1051 		struct vnode *a_vp;
   1052 		struct uio *a_uio;
   1053 		kauth_cred_t a_cred;
   1054 	}; */ *ap = v;
   1055 	struct vnode *vp = ap->a_vp;
   1056 	struct rumpfs_node *rn = vp->v_data;
   1057 	struct uio *uio = ap->a_uio;
   1058 
   1059 	return uiomove(rn->rn_linktarg, rn->rn_linklen, uio);
   1060 }
   1061 
   1062 static int
   1063 rump_vop_whiteout(void *v)
   1064 {
   1065 	struct vop_whiteout_args /* {
   1066 		struct vnode            *a_dvp;
   1067 		struct componentname    *a_cnp;
   1068 		int                     a_flags;
   1069 	} */ *ap = v;
   1070 	struct vnode *dvp = ap->a_dvp;
   1071 	struct rumpfs_node *rnd = dvp->v_data;
   1072 	struct componentname *cnp = ap->a_cnp;
   1073 	int flags = ap->a_flags;
   1074 
   1075 	switch (flags) {
   1076 	case LOOKUP:
   1077 		break;
   1078 	case CREATE:
   1079 		makedir(rnd, cnp, RUMPFS_WHITEOUT);
   1080 		break;
   1081 	case DELETE:
   1082 		cnp->cn_flags &= ~DOWHITEOUT; /* cargo culting never fails ? */
   1083 		freedir(rnd, cnp);
   1084 		break;
   1085 	default:
   1086 		panic("unknown whiteout op %d", flags);
   1087 	}
   1088 
   1089 	return 0;
   1090 }
   1091 
   1092 static int
   1093 rump_vop_open(void *v)
   1094 {
   1095 	struct vop_open_args /* {
   1096 		struct vnode *a_vp;
   1097 		int a_mode;
   1098 		kauth_cred_t a_cred;
   1099 	} */ *ap = v;
   1100 	struct vnode *vp = ap->a_vp;
   1101 	struct rumpfs_node *rn = vp->v_data;
   1102 	int mode = ap->a_mode;
   1103 	int error = EINVAL;
   1104 
   1105 	if (vp->v_type != VREG || (rn->rn_flags & RUMPNODE_ET_PHONE_HOST) == 0)
   1106 		return 0;
   1107 
   1108 	if (mode & FREAD) {
   1109 		if (rn->rn_readfd != -1)
   1110 			return 0;
   1111 		rn->rn_readfd = rumpuser_open(rn->rn_hostpath,
   1112 		    O_RDONLY, &error);
   1113 	}
   1114 
   1115 	if (mode & FWRITE) {
   1116 		if (rn->rn_writefd != -1)
   1117 			return 0;
   1118 		rn->rn_writefd = rumpuser_open(rn->rn_hostpath,
   1119 		    O_WRONLY, &error);
   1120 	}
   1121 
   1122 	return error;
   1123 }
   1124 
   1125 /* simple readdir.  event omits dotstuff and periods */
   1126 static int
   1127 rump_vop_readdir(void *v)
   1128 {
   1129 	struct vop_readdir_args /* {
   1130 		struct vnode *a_vp;
   1131 		struct uio *a_uio;
   1132 		kauth_cred_t a_cred;
   1133 		int *a_eofflag;
   1134 		off_t **a_cookies;
   1135 		int *a_ncookies;
   1136 	} */ *ap = v;
   1137 	struct vnode *vp = ap->a_vp;
   1138 	struct uio *uio = ap->a_uio;
   1139 	struct rumpfs_node *rnd = vp->v_data;
   1140 	struct rumpfs_dent *rdent;
   1141 	unsigned i;
   1142 	int rv = 0;
   1143 
   1144 	/* seek to current entry */
   1145 	for (i = 0, rdent = LIST_FIRST(&rnd->rn_dir);
   1146 	    (i < uio->uio_offset) && rdent;
   1147 	    i++, rdent = LIST_NEXT(rdent, rd_entries))
   1148 		continue;
   1149 	if (!rdent)
   1150 		goto out;
   1151 
   1152 	/* copy entries */
   1153 	for (; rdent && uio->uio_resid > 0;
   1154 	    rdent = LIST_NEXT(rdent, rd_entries), i++) {
   1155 		struct dirent dent;
   1156 
   1157 		strlcpy(dent.d_name, rdent->rd_name, sizeof(dent.d_name));
   1158 		dent.d_namlen = strlen(dent.d_name);
   1159 		dent.d_reclen = _DIRENT_RECLEN(&dent, dent.d_namlen);
   1160 
   1161 		if (__predict_false(RDENT_ISWHITEOUT(rdent))) {
   1162 			dent.d_fileno = INO_WHITEOUT;
   1163 			dent.d_type = DT_WHT;
   1164 		} else {
   1165 			dent.d_fileno = rdent->rd_node->rn_va.va_fileid;
   1166 			dent.d_type = vtype2dt(rdent->rd_node->rn_va.va_type);
   1167 		}
   1168 
   1169 		if (uio->uio_resid < dent.d_reclen) {
   1170 			i--;
   1171 			break;
   1172 		}
   1173 
   1174 		rv = uiomove(&dent, dent.d_reclen, uio);
   1175 		if (rv) {
   1176 			i--;
   1177 			break;
   1178 		}
   1179 	}
   1180 
   1181  out:
   1182 	if (ap->a_cookies) {
   1183 		*ap->a_ncookies = 0;
   1184 		*ap->a_cookies = NULL;
   1185 	}
   1186 	if (rdent)
   1187 		*ap->a_eofflag = 0;
   1188 	else
   1189 		*ap->a_eofflag = 1;
   1190 	uio->uio_offset = i;
   1191 
   1192 	return rv;
   1193 }
   1194 
   1195 static int
   1196 etread(struct rumpfs_node *rn, struct uio *uio)
   1197 {
   1198 	uint8_t *buf;
   1199 	size_t bufsize;
   1200 	ssize_t n;
   1201 	int error = 0;
   1202 
   1203 	bufsize = uio->uio_resid;
   1204 	buf = kmem_alloc(bufsize, KM_SLEEP);
   1205 	if ((n = rumpuser_pread(rn->rn_readfd, buf, bufsize,
   1206 	    uio->uio_offset + rn->rn_offset, &error)) == -1)
   1207 		goto out;
   1208 	KASSERT(n <= bufsize);
   1209 	error = uiomove(buf, n, uio);
   1210 
   1211  out:
   1212 	kmem_free(buf, bufsize);
   1213 	return error;
   1214 
   1215 }
   1216 
   1217 static int
   1218 rump_vop_read(void *v)
   1219 {
   1220 	struct vop_read_args /* {
   1221 		struct vnode *a_vp;
   1222 		struct uio *a_uio;
   1223 		int ioflags a_ioflag;
   1224 		kauth_cred_t a_cred;
   1225 	}; */ *ap = v;
   1226 	struct vnode *vp = ap->a_vp;
   1227 	struct rumpfs_node *rn = vp->v_data;
   1228 	struct uio *uio = ap->a_uio;
   1229 	const int advice = IO_ADV_DECODE(ap->a_ioflag);
   1230 	off_t chunk;
   1231 	int error = 0;
   1232 
   1233 	/* et op? */
   1234 	if (rn->rn_flags & RUMPNODE_ET_PHONE_HOST)
   1235 		return etread(rn, uio);
   1236 
   1237 	/* otherwise, it's off to ubc with us */
   1238 	while (uio->uio_resid > 0) {
   1239 		chunk = MIN(uio->uio_resid, (off_t)rn->rn_dlen-uio->uio_offset);
   1240 		if (chunk == 0)
   1241 			break;
   1242 		error = ubc_uiomove(&vp->v_uobj, uio, chunk, advice,
   1243 		    UBC_READ | UBC_PARTIALOK | UBC_WANT_UNMAP(vp)?UBC_UNMAP:0);
   1244 		if (error)
   1245 			break;
   1246 	}
   1247 
   1248 	return error;
   1249 }
   1250 
   1251 static int
   1252 etwrite(struct rumpfs_node *rn, struct uio *uio)
   1253 {
   1254 	uint8_t *buf;
   1255 	size_t bufsize;
   1256 	ssize_t n;
   1257 	int error = 0;
   1258 
   1259 	bufsize = uio->uio_resid;
   1260 	buf = kmem_alloc(bufsize, KM_SLEEP);
   1261 	error = uiomove(buf, bufsize, uio);
   1262 	if (error)
   1263 		goto out;
   1264 	KASSERT(uio->uio_resid == 0);
   1265 	n = rumpuser_pwrite(rn->rn_writefd, buf, bufsize,
   1266 	    (uio->uio_offset-bufsize) + rn->rn_offset, &error);
   1267 	if (n >= 0) {
   1268 		KASSERT(n <= bufsize);
   1269 		uio->uio_resid = bufsize - n;
   1270 	}
   1271 
   1272  out:
   1273 	kmem_free(buf, bufsize);
   1274 	return error;
   1275 }
   1276 
   1277 static int
   1278 rump_vop_write(void *v)
   1279 {
   1280 	struct vop_read_args /* {
   1281 		struct vnode *a_vp;
   1282 		struct uio *a_uio;
   1283 		int ioflags a_ioflag;
   1284 		kauth_cred_t a_cred;
   1285 	}; */ *ap = v;
   1286 	struct vnode *vp = ap->a_vp;
   1287 	struct rumpfs_node *rn = vp->v_data;
   1288 	struct uio *uio = ap->a_uio;
   1289 	const int advice = IO_ADV_DECODE(ap->a_ioflag);
   1290 	void *olddata;
   1291 	size_t oldlen, newlen;
   1292 	off_t chunk;
   1293 	int error = 0;
   1294 	bool allocd = false;
   1295 
   1296 	if (ap->a_ioflag & IO_APPEND)
   1297 		uio->uio_offset = vp->v_size;
   1298 
   1299 	/* consult et? */
   1300 	if (rn->rn_flags & RUMPNODE_ET_PHONE_HOST)
   1301 		return etwrite(rn, uio);
   1302 
   1303 	/*
   1304 	 * Otherwise, it's a case of ubcmove.
   1305 	 */
   1306 
   1307 	/*
   1308 	 * First, make sure we have enough storage.
   1309 	 *
   1310 	 * No, you don't need to tell me it's not very efficient.
   1311 	 * No, it doesn't really support sparse files, just fakes it.
   1312 	 */
   1313 	newlen = uio->uio_offset + uio->uio_resid;
   1314 	oldlen = 0; /* XXXgcc */
   1315 	olddata = NULL;
   1316 	if (rn->rn_dlen < newlen) {
   1317 		oldlen = rn->rn_dlen;
   1318 		olddata = rn->rn_data;
   1319 
   1320 		rn->rn_data = rump_hypermalloc(newlen, 0, true, "rumpfs");
   1321 		rn->rn_dlen = newlen;
   1322 		memset(rn->rn_data, 0, newlen);
   1323 		memcpy(rn->rn_data, olddata, oldlen);
   1324 		allocd = true;
   1325 		uvm_vnp_setsize(vp, newlen);
   1326 	}
   1327 
   1328 	/* ok, we have enough stooorage.  write */
   1329 	while (uio->uio_resid > 0) {
   1330 		chunk = MIN(uio->uio_resid, (off_t)rn->rn_dlen-uio->uio_offset);
   1331 		if (chunk == 0)
   1332 			break;
   1333 		error = ubc_uiomove(&vp->v_uobj, uio, chunk, advice,
   1334 		    UBC_WRITE | UBC_PARTIALOK | UBC_WANT_UNMAP(vp)?UBC_UNMAP:0);
   1335 		if (error)
   1336 			break;
   1337 	}
   1338 
   1339 	if (allocd) {
   1340 		if (error) {
   1341 			rump_hyperfree(rn->rn_data, newlen);
   1342 			rn->rn_data = olddata;
   1343 			rn->rn_dlen = oldlen;
   1344 			uvm_vnp_setsize(vp, oldlen);
   1345 		} else {
   1346 			rump_hyperfree(olddata, oldlen);
   1347 		}
   1348 	}
   1349 
   1350 	return error;
   1351 }
   1352 
   1353 static int
   1354 rump_vop_bmap(void *v)
   1355 {
   1356 	struct vop_bmap_args /* {
   1357 		struct vnode *a_vp;
   1358 		daddr_t a_bn;
   1359 		struct vnode **a_vpp;
   1360 		daddr_t *a_bnp;
   1361 		int *a_runp;
   1362 	} */ *ap = v;
   1363 
   1364 	/* 1:1 mapping */
   1365 	if (ap->a_vpp)
   1366 		*ap->a_vpp = ap->a_vp;
   1367 	if (ap->a_bnp)
   1368 		*ap->a_bnp = ap->a_bn;
   1369 	if (ap->a_runp)
   1370 		*ap->a_runp = 16;
   1371 
   1372 	return 0;
   1373 }
   1374 
   1375 static int
   1376 rump_vop_strategy(void *v)
   1377 {
   1378 	struct vop_strategy_args /* {
   1379 		struct vnode *a_vp;
   1380 		struct buf *a_bp;
   1381 	} */ *ap = v;
   1382 	struct vnode *vp = ap->a_vp;
   1383 	struct rumpfs_node *rn = vp->v_data;
   1384 	struct buf *bp = ap->a_bp;
   1385 	off_t copylen, copyoff;
   1386 	int error;
   1387 
   1388 	if (vp->v_type != VREG || rn->rn_flags & RUMPNODE_ET_PHONE_HOST) {
   1389 		error = EINVAL;
   1390 		goto out;
   1391 	}
   1392 
   1393 	copyoff = bp->b_blkno << DEV_BSHIFT;
   1394 	copylen = MIN(rn->rn_dlen - copyoff, bp->b_bcount);
   1395 	if (BUF_ISWRITE(bp)) {
   1396 		memcpy((uint8_t *)rn->rn_data + copyoff, bp->b_data, copylen);
   1397 	} else {
   1398 		memset((uint8_t*)bp->b_data + copylen, 0, bp->b_bcount-copylen);
   1399 		memcpy(bp->b_data, (uint8_t *)rn->rn_data + copyoff, copylen);
   1400 	}
   1401 	bp->b_resid = 0;
   1402 	error = 0;
   1403 
   1404  out:
   1405 	bp->b_error = error;
   1406 	biodone(bp);
   1407 	return 0;
   1408 }
   1409 
   1410 static int
   1411 rump_vop_pathconf(void *v)
   1412 {
   1413 	struct vop_pathconf_args /* {
   1414 		struct vnode *a_vp;
   1415 		int a_name;
   1416 		register_t *a_retval;
   1417 	}; */ *ap = v;
   1418 	int name = ap->a_name;
   1419 	register_t *retval = ap->a_retval;
   1420 
   1421 	switch (name) {
   1422 	case _PC_LINK_MAX:
   1423 		*retval = LINK_MAX;
   1424 		return 0;
   1425 	case _PC_NAME_MAX:
   1426 		*retval = NAME_MAX;
   1427 		return 0;
   1428 	case _PC_PATH_MAX:
   1429 		*retval = PATH_MAX;
   1430 		return 0;
   1431 	case _PC_PIPE_BUF:
   1432 		*retval = PIPE_BUF;
   1433 		return 0;
   1434 	case _PC_CHOWN_RESTRICTED:
   1435 		*retval = 1;
   1436 		return 0;
   1437 	case _PC_NO_TRUNC:
   1438 		*retval = 1;
   1439 		return 0;
   1440 	case _PC_SYNC_IO:
   1441 		*retval = 1;
   1442 		return 0;
   1443 	case _PC_FILESIZEBITS:
   1444 		*retval = 43; /* this one goes to 11 */
   1445 		return 0;
   1446 	case _PC_SYMLINK_MAX:
   1447 		*retval = MAXPATHLEN;
   1448 		return 0;
   1449 	case _PC_2_SYMLINKS:
   1450 		*retval = 1;
   1451 		return 0;
   1452 	default:
   1453 		return EINVAL;
   1454 	}
   1455 }
   1456 
   1457 static int
   1458 rump_vop_success(void *v)
   1459 {
   1460 
   1461 	return 0;
   1462 }
   1463 
   1464 static int
   1465 rump_vop_inactive(void *v)
   1466 {
   1467 	struct vop_inactive_args /* {
   1468 		struct vnode *a_vp;
   1469 		bool *a_recycle;
   1470 	} */ *ap = v;
   1471 	struct vnode *vp = ap->a_vp;
   1472 	struct rumpfs_node *rn = vp->v_data;
   1473 	int error;
   1474 
   1475 	if (rn->rn_flags & RUMPNODE_ET_PHONE_HOST && vp->v_type == VREG) {
   1476 		if (rn->rn_readfd != -1) {
   1477 			rumpuser_close(rn->rn_readfd, &error);
   1478 			rn->rn_readfd = -1;
   1479 		}
   1480 		if (rn->rn_writefd != -1) {
   1481 			rumpuser_close(rn->rn_writefd, &error);
   1482 			rn->rn_writefd = -1;
   1483 		}
   1484 	}
   1485 	*ap->a_recycle = (rn->rn_flags & RUMPNODE_CANRECLAIM) ? true : false;
   1486 
   1487 	VOP_UNLOCK(vp);
   1488 	return 0;
   1489 }
   1490 
   1491 static int
   1492 rump_vop_reclaim(void *v)
   1493 {
   1494 	struct vop_reclaim_args /* {
   1495 		struct vnode *a_vp;
   1496 	} */ *ap = v;
   1497 	struct vnode *vp = ap->a_vp;
   1498 	struct rumpfs_node *rn = vp->v_data;
   1499 
   1500 	mutex_enter(&reclock);
   1501 	rn->rn_vp = NULL;
   1502 	mutex_exit(&reclock);
   1503 	genfs_node_destroy(vp);
   1504 	vp->v_data = NULL;
   1505 
   1506 	if (rn->rn_flags & RUMPNODE_CANRECLAIM) {
   1507 		if (vp->v_type == VLNK)
   1508 			PNBUF_PUT(rn->rn_linktarg);
   1509 		if (rn->rn_hostpath)
   1510 			free(rn->rn_hostpath, M_TEMP);
   1511 		kmem_free(rn, sizeof(*rn));
   1512 	}
   1513 
   1514 	return 0;
   1515 }
   1516 
   1517 static int
   1518 rump_vop_spec(void *v)
   1519 {
   1520 	struct vop_generic_args *ap = v;
   1521 	int (**opvec)(void *);
   1522 
   1523 	switch (ap->a_desc->vdesc_offset) {
   1524 	case VOP_ACCESS_DESCOFFSET:
   1525 	case VOP_GETATTR_DESCOFFSET:
   1526 	case VOP_SETATTR_DESCOFFSET:
   1527 	case VOP_LOCK_DESCOFFSET:
   1528 	case VOP_UNLOCK_DESCOFFSET:
   1529 	case VOP_ISLOCKED_DESCOFFSET:
   1530 	case VOP_RECLAIM_DESCOFFSET:
   1531 		opvec = rump_vnodeop_p;
   1532 		break;
   1533 	default:
   1534 		opvec = spec_vnodeop_p;
   1535 		break;
   1536 	}
   1537 
   1538 	return VOCALL(opvec, ap->a_desc->vdesc_offset, v);
   1539 }
   1540 
   1541 static int
   1542 rump_vop_advlock(void *v)
   1543 {
   1544 	struct vop_advlock_args /* {
   1545 		const struct vnodeop_desc *a_desc;
   1546 		struct vnode *a_vp;
   1547 		void *a_id;
   1548 		int a_op;
   1549 		struct flock *a_fl;
   1550 		int a_flags;
   1551 	} */ *ap = v;
   1552 	struct vnode *vp = ap->a_vp;
   1553 	struct rumpfs_node *rn = vp->v_data;
   1554 
   1555 	return lf_advlock(ap, &rn->rn_lockf, vp->v_size);
   1556 }
   1557 
   1558 /*
   1559  * Begin vfs-level stuff
   1560  */
   1561 
   1562 VFS_PROTOS(rumpfs);
   1563 struct vfsops rumpfs_vfsops = {
   1564 	.vfs_name =		MOUNT_RUMPFS,
   1565 	.vfs_min_mount_data = 	0,
   1566 	.vfs_mount =		rumpfs_mount,
   1567 	.vfs_start =		(void *)nullop,
   1568 	.vfs_unmount = 		rumpfs_unmount,
   1569 	.vfs_root =		rumpfs_root,
   1570 	.vfs_quotactl =		(void *)eopnotsupp,
   1571 	.vfs_statvfs =		genfs_statvfs,
   1572 	.vfs_sync =		(void *)nullop,
   1573 	.vfs_vget =		rumpfs_vget,
   1574 	.vfs_fhtovp =		(void *)eopnotsupp,
   1575 	.vfs_vptofh =		(void *)eopnotsupp,
   1576 	.vfs_init =		rumpfs_init,
   1577 	.vfs_reinit =		NULL,
   1578 	.vfs_done =		rumpfs_done,
   1579 	.vfs_mountroot =	rumpfs_mountroot,
   1580 	.vfs_snapshot =		(void *)eopnotsupp,
   1581 	.vfs_extattrctl =	(void *)eopnotsupp,
   1582 	.vfs_suspendctl =	(void *)eopnotsupp,
   1583 	.vfs_renamelock_enter =	genfs_renamelock_enter,
   1584 	.vfs_renamelock_exit =	genfs_renamelock_exit,
   1585 	.vfs_opv_descs =	rump_opv_descs,
   1586 	/* vfs_refcount */
   1587 	/* vfs_list */
   1588 };
   1589 
   1590 static int
   1591 rumpfs_mountfs(struct mount *mp)
   1592 {
   1593 	struct rumpfs_mount *rfsmp;
   1594 	struct rumpfs_node *rn;
   1595 	int error;
   1596 
   1597 	rfsmp = kmem_alloc(sizeof(*rfsmp), KM_SLEEP);
   1598 
   1599 	rn = makeprivate(VDIR, NODEV, DEV_BSIZE, false);
   1600 	rn->rn_parent = rn;
   1601 	if ((error = makevnode(mp, rn, &rfsmp->rfsmp_rvp)) != 0)
   1602 		return error;
   1603 
   1604 	rfsmp->rfsmp_rvp->v_vflag |= VV_ROOT;
   1605 	VOP_UNLOCK(rfsmp->rfsmp_rvp);
   1606 
   1607 	mp->mnt_data = rfsmp;
   1608 	mp->mnt_stat.f_namemax = MAXNAMLEN;
   1609 	mp->mnt_stat.f_iosize = 512;
   1610 	mp->mnt_flag |= MNT_LOCAL;
   1611 	mp->mnt_iflag |= IMNT_MPSAFE | IMNT_CAN_RWTORO;
   1612 	mp->mnt_fs_bshift = DEV_BSHIFT;
   1613 	vfs_getnewfsid(mp);
   1614 
   1615 	return 0;
   1616 }
   1617 
   1618 int
   1619 rumpfs_mount(struct mount *mp, const char *mntpath, void *arg, size_t *alen)
   1620 {
   1621 	int error;
   1622 
   1623 	if (mp->mnt_flag & MNT_UPDATE) {
   1624 		return 0;
   1625 	}
   1626 
   1627 	error = set_statvfs_info(mntpath, UIO_USERSPACE, "rumpfs", UIO_SYSSPACE,
   1628 	    mp->mnt_op->vfs_name, mp, curlwp);
   1629 	if (error)
   1630 		return error;
   1631 
   1632 	return rumpfs_mountfs(mp);
   1633 }
   1634 
   1635 int
   1636 rumpfs_unmount(struct mount *mp, int mntflags)
   1637 {
   1638 	struct rumpfs_mount *rfsmp = mp->mnt_data;
   1639 	int flags = 0, error;
   1640 
   1641 	if (panicstr || mntflags & MNT_FORCE)
   1642 		flags |= FORCECLOSE;
   1643 
   1644 	if ((error = vflush(mp, rfsmp->rfsmp_rvp, flags)) != 0)
   1645 		return error;
   1646 	vgone(rfsmp->rfsmp_rvp); /* XXX */
   1647 
   1648 	kmem_free(rfsmp, sizeof(*rfsmp));
   1649 
   1650 	return 0;
   1651 }
   1652 
   1653 int
   1654 rumpfs_root(struct mount *mp, struct vnode **vpp)
   1655 {
   1656 	struct rumpfs_mount *rfsmp = mp->mnt_data;
   1657 
   1658 	vref(rfsmp->rfsmp_rvp);
   1659 	vn_lock(rfsmp->rfsmp_rvp, LK_EXCLUSIVE | LK_RETRY);
   1660 	*vpp = rfsmp->rfsmp_rvp;
   1661 	return 0;
   1662 }
   1663 
   1664 int
   1665 rumpfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
   1666 {
   1667 
   1668 	return EOPNOTSUPP;
   1669 }
   1670 
   1671 void
   1672 rumpfs_init()
   1673 {
   1674 
   1675 	CTASSERT(RUMP_ETFS_SIZE_ENDOFF == RUMPBLK_SIZENOTSET);
   1676 
   1677 	mutex_init(&reclock, MUTEX_DEFAULT, IPL_NONE);
   1678 	mutex_init(&etfs_lock, MUTEX_DEFAULT, IPL_NONE);
   1679 }
   1680 
   1681 void
   1682 rumpfs_done()
   1683 {
   1684 
   1685 	mutex_destroy(&reclock);
   1686 	mutex_destroy(&etfs_lock);
   1687 }
   1688 
   1689 int
   1690 rumpfs_mountroot()
   1691 {
   1692 	struct mount *mp;
   1693 	int error;
   1694 
   1695 	if ((error = vfs_rootmountalloc(MOUNT_RUMPFS, "rootdev", &mp)) != 0) {
   1696 		vrele(rootvp);
   1697 		return error;
   1698 	}
   1699 
   1700 	if ((error = rumpfs_mountfs(mp)) != 0)
   1701 		panic("mounting rootfs failed: %d", error);
   1702 
   1703 	mutex_enter(&mountlist_lock);
   1704 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
   1705 	mutex_exit(&mountlist_lock);
   1706 
   1707 	error = set_statvfs_info("/", UIO_SYSSPACE, "rumpfs", UIO_SYSSPACE,
   1708 	    mp->mnt_op->vfs_name, mp, curlwp);
   1709 	if (error)
   1710 		panic("set_statvfs_info failed for rootfs: %d", error);
   1711 
   1712 	mp->mnt_flag &= ~MNT_RDONLY;
   1713 	vfs_unbusy(mp, false, NULL);
   1714 
   1715 	return 0;
   1716 }
   1717