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