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