Home | History | Annotate | Line # | Download | only in rumpvfs
rumpfs.c revision 1.57
      1 /*	$NetBSD: rumpfs.c,v 1.57 2010/06/24 13:03:18 hannken Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2009  Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: rumpfs.c,v 1.57 2010/06/24 13:03:18 hannken Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/atomic.h>
     33 #include <sys/dirent.h>
     34 #include <sys/errno.h>
     35 #include <sys/filedesc.h>
     36 #include <sys/fcntl.h>
     37 #include <sys/kauth.h>
     38 #include <sys/malloc.h>
     39 #include <sys/module.h>
     40 #include <sys/mount.h>
     41 #include <sys/namei.h>
     42 #include <sys/lock.h>
     43 #include <sys/lockf.h>
     44 #include <sys/queue.h>
     45 #include <sys/stat.h>
     46 #include <sys/syscallargs.h>
     47 #include <sys/vnode.h>
     48 
     49 #include <miscfs/fifofs/fifo.h>
     50 #include <miscfs/specfs/specdev.h>
     51 #include <miscfs/genfs/genfs.h>
     52 
     53 #include <rump/rumpuser.h>
     54 
     55 #include "rump_private.h"
     56 #include "rump_vfs_private.h"
     57 
     58 static int rump_vop_lookup(void *);
     59 static int rump_vop_getattr(void *);
     60 static int rump_vop_mkdir(void *);
     61 static int rump_vop_rmdir(void *);
     62 static int rump_vop_mknod(void *);
     63 static int rump_vop_create(void *);
     64 static int rump_vop_inactive(void *);
     65 static int rump_vop_reclaim(void *);
     66 static int rump_vop_success(void *);
     67 static int rump_vop_readdir(void *);
     68 static int rump_vop_spec(void *);
     69 static int rump_vop_read(void *);
     70 static int rump_vop_write(void *);
     71 static int rump_vop_open(void *);
     72 
     73 int (**fifo_vnodeop_p)(void *);
     74 const struct vnodeopv_entry_desc fifo_vnodeop_entries[] = {
     75 	{ &vop_default_desc, vn_default_error },
     76 	{ NULL, NULL }
     77 };
     78 const struct vnodeopv_desc fifo_vnodeop_opv_desc =
     79 	{ &fifo_vnodeop_p, fifo_vnodeop_entries };
     80 
     81 int (**rump_vnodeop_p)(void *);
     82 const struct vnodeopv_entry_desc rump_vnodeop_entries[] = {
     83 	{ &vop_default_desc, vn_default_error },
     84 	{ &vop_lookup_desc, rump_vop_lookup },
     85 	{ &vop_getattr_desc, rump_vop_getattr },
     86 	{ &vop_mkdir_desc, rump_vop_mkdir },
     87 	{ &vop_rmdir_desc, rump_vop_rmdir },
     88 	{ &vop_mknod_desc, rump_vop_mknod },
     89 	{ &vop_create_desc, rump_vop_create },
     90 	{ &vop_symlink_desc, genfs_eopnotsupp },
     91 	{ &vop_access_desc, rump_vop_success },
     92 	{ &vop_readdir_desc, rump_vop_readdir },
     93 	{ &vop_read_desc, rump_vop_read },
     94 	{ &vop_write_desc, rump_vop_write },
     95 	{ &vop_open_desc, rump_vop_open },
     96 	{ &vop_seek_desc, genfs_seek },
     97 	{ &vop_putpages_desc, genfs_null_putpages },
     98 	{ &vop_fsync_desc, rump_vop_success },
     99 	{ &vop_lock_desc, genfs_lock },
    100 	{ &vop_unlock_desc, genfs_unlock },
    101 	{ &vop_islocked_desc, genfs_islocked },
    102 	{ &vop_inactive_desc, rump_vop_inactive },
    103 	{ &vop_reclaim_desc, rump_vop_reclaim },
    104 	{ NULL, NULL }
    105 };
    106 const struct vnodeopv_desc rump_vnodeop_opv_desc =
    107 	{ &rump_vnodeop_p, rump_vnodeop_entries };
    108 
    109 int (**rump_specop_p)(void *);
    110 const struct vnodeopv_entry_desc rump_specop_entries[] = {
    111 	{ &vop_default_desc, rump_vop_spec },
    112 	{ NULL, NULL }
    113 };
    114 const struct vnodeopv_desc rump_specop_opv_desc =
    115 	{ &rump_specop_p, rump_specop_entries };
    116 
    117 const struct vnodeopv_desc * const rump_opv_descs[] = {
    118 	&rump_vnodeop_opv_desc,
    119 	&rump_specop_opv_desc,
    120 	NULL
    121 };
    122 
    123 struct rumpfs_dent {
    124 	char *rd_name;
    125 	int rd_namelen;
    126 	struct rumpfs_node *rd_node;
    127 
    128 	LIST_ENTRY(rumpfs_dent) rd_entries;
    129 };
    130 
    131 struct rumpfs_node {
    132 	struct vattr rn_va;
    133 	struct vnode *rn_vp;
    134 	char *rn_hostpath;
    135 	int rn_flags;
    136 
    137 	union {
    138 		struct {		/* VREG */
    139 			int readfd;
    140 			int writefd;
    141 			uint64_t offset;
    142 		} reg;
    143 		struct {		/* VDIR */
    144 			LIST_HEAD(, rumpfs_dent) dents;
    145 			int flags;
    146 		} dir;
    147 	} rn_u;
    148 };
    149 #define rn_readfd	rn_u.reg.readfd
    150 #define rn_writefd	rn_u.reg.writefd
    151 #define rn_offset	rn_u.reg.offset
    152 #define rn_dir		rn_u.dir.dents
    153 
    154 #define RUMPNODE_CANRECLAIM	0x01
    155 #define RUMPNODE_DIR_ET		0x02
    156 #define RUMPNODE_DIR_ETSUBS	0x04
    157 
    158 struct rumpfs_mount {
    159 	struct vnode *rfsmp_rvp;
    160 };
    161 
    162 static struct rumpfs_node *makeprivate(enum vtype, dev_t, off_t);
    163 
    164 /*
    165  * Extra Terrestrial stuff.  We map a given key (pathname) to a file on
    166  * the host FS.  ET phones home only from the root node of rumpfs.
    167  *
    168  * When an etfs node is removed, a vnode potentially behind it is not
    169  * immediately recycled.
    170  */
    171 
    172 struct etfs {
    173 	char et_key[MAXPATHLEN];
    174 	size_t et_keylen;
    175 	bool et_prefixkey;
    176 	bool et_removing;
    177 	devminor_t et_blkmin;
    178 
    179 	LIST_ENTRY(etfs) et_entries;
    180 
    181 	struct rumpfs_node *et_rn;
    182 };
    183 static kmutex_t etfs_lock;
    184 static LIST_HEAD(, etfs) etfs_list = LIST_HEAD_INITIALIZER(etfs_list);
    185 
    186 static enum vtype
    187 ettype_to_vtype(enum rump_etfs_type et)
    188 {
    189 	enum vtype vt;
    190 
    191 	switch (et) {
    192 	case RUMP_ETFS_REG:
    193 		vt = VREG;
    194 		break;
    195 	case RUMP_ETFS_BLK:
    196 		vt = VBLK;
    197 		break;
    198 	case RUMP_ETFS_CHR:
    199 		vt = VCHR;
    200 		break;
    201 	case RUMP_ETFS_DIR:
    202 		vt = VDIR;
    203 		break;
    204 	case RUMP_ETFS_DIR_SUBDIRS:
    205 		vt = VDIR;
    206 		break;
    207 	default:
    208 		panic("invalid et type: %d", et);
    209 	}
    210 
    211 	return vt;
    212 }
    213 
    214 static enum vtype
    215 hft_to_vtype(int hft)
    216 {
    217 	enum vtype vt;
    218 
    219 	switch (hft) {
    220 	case RUMPUSER_FT_OTHER:
    221 		vt = VNON;
    222 		break;
    223 	case RUMPUSER_FT_DIR:
    224 		vt = VDIR;
    225 		break;
    226 	case RUMPUSER_FT_REG:
    227 		vt = VREG;
    228 		break;
    229 	case RUMPUSER_FT_BLK:
    230 		vt = VBLK;
    231 		break;
    232 	case RUMPUSER_FT_CHR:
    233 		vt = VCHR;
    234 		break;
    235 	default:
    236 		vt = VNON;
    237 		break;
    238 	}
    239 
    240 	return vt;
    241 }
    242 
    243 static bool
    244 etfs_find(const char *key, struct etfs **etp, bool forceprefix)
    245 {
    246 	struct etfs *et;
    247 	size_t keylen = strlen(key);
    248 
    249 	KASSERT(mutex_owned(&etfs_lock));
    250 
    251 	LIST_FOREACH(et, &etfs_list, et_entries) {
    252 		if ((keylen == et->et_keylen || et->et_prefixkey || forceprefix)
    253 		    && strncmp(key, et->et_key, et->et_keylen) == 0) {
    254 			if (etp)
    255 				*etp = et;
    256 			return true;
    257 		}
    258 	}
    259 
    260 	return false;
    261 }
    262 
    263 #define REGDIR(ftype) \
    264     ((ftype) == RUMP_ETFS_DIR || (ftype) == RUMP_ETFS_DIR_SUBDIRS)
    265 static int
    266 doregister(const char *key, const char *hostpath,
    267 	enum rump_etfs_type ftype, uint64_t begin, uint64_t size)
    268 {
    269 	struct etfs *et;
    270 	struct rumpfs_node *rn;
    271 	uint64_t fsize;
    272 	dev_t rdev = NODEV;
    273 	devminor_t dmin = -1;
    274 	int hft, error;
    275 
    276 	if (rumpuser_getfileinfo(hostpath, &fsize, &hft, &error))
    277 		return error;
    278 
    279 	/* etfs directory requires a directory on the host */
    280 	if (REGDIR(ftype)) {
    281 		if (hft != RUMPUSER_FT_DIR)
    282 			return ENOTDIR;
    283 		if (begin != 0)
    284 			return EISDIR;
    285 		if (size != RUMP_ETFS_SIZE_ENDOFF)
    286 			return EISDIR;
    287 		size = fsize;
    288 	} else {
    289 		if (begin > fsize)
    290 			return EINVAL;
    291 		if (size == RUMP_ETFS_SIZE_ENDOFF)
    292 			size = fsize - begin;
    293 		if (begin + size > fsize)
    294 			return EINVAL;
    295 	}
    296 
    297 	if (ftype == RUMP_ETFS_BLK || ftype == RUMP_ETFS_CHR) {
    298 		error = rumpblk_register(hostpath, &dmin, begin, size);
    299 		if (error != 0) {
    300 			return error;
    301 		}
    302 		rdev = makedev(RUMPBLK_DEVMAJOR, dmin);
    303 	}
    304 
    305 	et = kmem_alloc(sizeof(*et), KM_SLEEP);
    306 	strcpy(et->et_key, key);
    307 	et->et_keylen = strlen(et->et_key);
    308 	et->et_rn = rn = makeprivate(ettype_to_vtype(ftype), rdev, size);
    309 	et->et_removing = false;
    310 	et->et_blkmin = dmin;
    311 
    312 	if (ftype == RUMP_ETFS_REG || REGDIR(ftype) || et->et_blkmin != -1) {
    313 		size_t len = strlen(hostpath)+1;
    314 
    315 		rn->rn_hostpath = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
    316 		memcpy(rn->rn_hostpath, hostpath, len);
    317 		rn->rn_offset = begin;
    318 	}
    319 
    320 	if (REGDIR(ftype)) {
    321 		rn->rn_flags |= RUMPNODE_DIR_ET;
    322 		et->et_prefixkey = true;
    323 	} else {
    324 		et->et_prefixkey = false;
    325 	}
    326 
    327 	if (ftype == RUMP_ETFS_DIR_SUBDIRS)
    328 		rn->rn_flags |= RUMPNODE_DIR_ETSUBS;
    329 
    330 	mutex_enter(&etfs_lock);
    331 	if (etfs_find(key, NULL, REGDIR(ftype))) {
    332 		mutex_exit(&etfs_lock);
    333 		if (et->et_blkmin != -1)
    334 			rumpblk_deregister(hostpath);
    335 		if (et->et_rn->rn_hostpath != NULL)
    336 			free(et->et_rn->rn_hostpath, M_TEMP);
    337 		kmem_free(et->et_rn, sizeof(*et->et_rn));
    338 		kmem_free(et, sizeof(*et));
    339 		return EEXIST;
    340 	}
    341 	LIST_INSERT_HEAD(&etfs_list, et, et_entries);
    342 	mutex_exit(&etfs_lock);
    343 
    344 	return 0;
    345 }
    346 #undef REGDIR
    347 
    348 int
    349 rump_etfs_register(const char *key, const char *hostpath,
    350 	enum rump_etfs_type ftype)
    351 {
    352 
    353 	return doregister(key, hostpath, ftype, 0, RUMP_ETFS_SIZE_ENDOFF);
    354 }
    355 
    356 int
    357 rump_etfs_register_withsize(const char *key, const char *hostpath,
    358 	enum rump_etfs_type ftype, uint64_t begin, uint64_t size)
    359 {
    360 
    361 	return doregister(key, hostpath, ftype, begin, size);
    362 }
    363 
    364 /* remove etfs mapping.  caller's responsibility to make sure it's not in use */
    365 int
    366 rump_etfs_remove(const char *key)
    367 {
    368 	struct etfs *et;
    369 	size_t keylen = strlen(key);
    370 	int rv;
    371 
    372 	mutex_enter(&etfs_lock);
    373 	LIST_FOREACH(et, &etfs_list, et_entries) {
    374 		if (keylen == et->et_keylen && strcmp(et->et_key, key) == 0) {
    375 			if (et->et_removing)
    376 				et = NULL;
    377 			else
    378 				et->et_removing = true;
    379 			break;
    380 		}
    381 	}
    382 	mutex_exit(&etfs_lock);
    383 	if (!et)
    384 		return ENOENT;
    385 
    386 	/*
    387 	 * ok, we know what we want to remove and have signalled there
    388 	 * actually are men at work.  first, unregister from rumpblk
    389 	 */
    390 	if (et->et_blkmin != -1) {
    391 		rv = rumpblk_deregister(et->et_rn->rn_hostpath);
    392 	} else {
    393 		rv = 0;
    394 	}
    395 	KASSERT(rv == 0);
    396 
    397 	/* then do the actual removal */
    398 	mutex_enter(&etfs_lock);
    399 	LIST_REMOVE(et, et_entries);
    400 	mutex_exit(&etfs_lock);
    401 
    402 	/* node is unreachable, safe to nuke all device copies */
    403 	if (et->et_blkmin != -1)
    404 		vdevgone(RUMPBLK_DEVMAJOR, et->et_blkmin, et->et_blkmin, VBLK);
    405 
    406 	if (et->et_rn->rn_hostpath != NULL)
    407 		free(et->et_rn->rn_hostpath, M_TEMP);
    408 	kmem_free(et->et_rn, sizeof(*et->et_rn));
    409 	kmem_free(et, sizeof(*et));
    410 
    411 	return 0;
    412 }
    413 
    414 /*
    415  * rumpfs
    416  */
    417 
    418 static int lastino = 1;
    419 static kmutex_t reclock;
    420 
    421 static struct rumpfs_node *
    422 makeprivate(enum vtype vt, dev_t rdev, off_t size)
    423 {
    424 	struct rumpfs_node *rn;
    425 	struct vattr *va;
    426 	struct timespec ts;
    427 
    428 	rn = kmem_zalloc(sizeof(*rn), KM_SLEEP);
    429 
    430 	switch (vt) {
    431 	case VDIR:
    432 		LIST_INIT(&rn->rn_dir);
    433 		break;
    434 	case VREG:
    435 		rn->rn_readfd = -1;
    436 		rn->rn_writefd = -1;
    437 		break;
    438 	default:
    439 		break;
    440 	}
    441 
    442 	nanotime(&ts);
    443 
    444 	va = &rn->rn_va;
    445 	va->va_type = vt;
    446 	va->va_mode = 0755;
    447 	if (vt == VDIR)
    448 		va->va_nlink = 2;
    449 	else
    450 		va->va_nlink = 1;
    451 	va->va_uid = 0;
    452 	va->va_gid = 0;
    453 	va->va_fsid =
    454 	va->va_fileid = atomic_inc_uint_nv(&lastino);
    455 	va->va_size = size;
    456 	va->va_blocksize = 512;
    457 	va->va_atime = ts;
    458 	va->va_mtime = ts;
    459 	va->va_ctime = ts;
    460 	va->va_birthtime = ts;
    461 	va->va_gen = 0;
    462 	va->va_flags = 0;
    463 	va->va_rdev = rdev;
    464 	va->va_bytes = 512;
    465 	va->va_filerev = 0;
    466 	va->va_vaflags = 0;
    467 
    468 	return rn;
    469 }
    470 
    471 static int
    472 makevnode(struct mount *mp, struct rumpfs_node *rn, struct vnode **vpp)
    473 {
    474 	struct vnode *vp;
    475 	int (**vpops)(void *);
    476 	struct vattr *va = &rn->rn_va;
    477 	int rv;
    478 
    479 	KASSERT(!mutex_owned(&reclock));
    480 
    481 	if (va->va_type == VCHR || va->va_type == VBLK) {
    482 		vpops = rump_specop_p;
    483 	} else {
    484 		vpops = rump_vnodeop_p;
    485 	}
    486 	if (vpops != rump_specop_p && va->va_type != VDIR
    487 	    && !(va->va_type == VREG && rn->rn_hostpath != NULL)
    488 	    && va->va_type != VSOCK)
    489 		return EOPNOTSUPP;
    490 
    491 	rv = getnewvnode(VT_RUMP, mp, vpops, &vp);
    492 	if (rv)
    493 		return rv;
    494 
    495 	vp->v_size = vp->v_writesize = va->va_size;
    496 	vp->v_type = va->va_type;
    497 
    498 	if (vpops == rump_specop_p) {
    499 		spec_node_init(vp, va->va_rdev);
    500 	}
    501 	vp->v_data = rn;
    502 
    503 	vn_lock(vp, LK_RETRY | LK_EXCLUSIVE);
    504 	mutex_enter(&reclock);
    505 	rn->rn_vp = vp;
    506 	mutex_exit(&reclock);
    507 
    508 	*vpp = vp;
    509 
    510 	return 0;
    511 }
    512 
    513 
    514 static void
    515 makedir(struct rumpfs_node *rnd,
    516 	struct componentname *cnp, struct rumpfs_node *rn)
    517 {
    518 	struct rumpfs_dent *rdent;
    519 
    520 	rdent = kmem_alloc(sizeof(*rdent), KM_SLEEP);
    521 	rdent->rd_name = kmem_alloc(cnp->cn_namelen+1, KM_SLEEP);
    522 	rdent->rd_node = rn;
    523 	strlcpy(rdent->rd_name, cnp->cn_nameptr, cnp->cn_namelen+1);
    524 	rdent->rd_namelen = strlen(rdent->rd_name);
    525 
    526 	LIST_INSERT_HEAD(&rnd->rn_dir, rdent, rd_entries);
    527 }
    528 
    529 static void
    530 freedir(struct rumpfs_node *rnd, struct componentname *cnp)
    531 {
    532 	struct rumpfs_dent *rd = NULL;
    533 
    534 	LIST_FOREACH(rd, &rnd->rn_dir, rd_entries) {
    535 		if (rd->rd_namelen == cnp->cn_namelen &&
    536 		    strncmp(rd->rd_name, cnp->cn_nameptr,
    537 		            cnp->cn_namelen) == 0)
    538 			break;
    539 	}
    540 	if (rd == NULL)
    541 		panic("could not find directory entry: %s", cnp->cn_nameptr);
    542 
    543 	LIST_REMOVE(rd, rd_entries);
    544 	kmem_free(rd->rd_name, rd->rd_namelen+1);
    545 	kmem_free(rd, sizeof(*rd));
    546 }
    547 
    548 /*
    549  * Simple lookup for rump file systems.
    550  *
    551  * uhm, this is twisted.  C F C C, hope of C C F C looming
    552  */
    553 static int
    554 rump_vop_lookup(void *v)
    555 {
    556 	struct vop_lookup_args /* {
    557 		struct vnode *a_dvp;
    558 		struct vnode **a_vpp;
    559 		struct componentname *a_cnp;
    560 	}; */ *ap = v;
    561 	struct componentname *cnp = ap->a_cnp;
    562 	struct vnode *dvp = ap->a_dvp;
    563 	struct vnode **vpp = ap->a_vpp;
    564 	struct vnode *vp;
    565 	struct rumpfs_node *rnd = dvp->v_data, *rn;
    566 	struct rumpfs_dent *rd = NULL;
    567 	struct etfs *et;
    568 	int rv;
    569 
    570 	/* we handle only some "non-special" cases */
    571 	if (!(((cnp->cn_flags & ISLASTCN) == 0) || (cnp->cn_nameiop != RENAME)))
    572 		return EOPNOTSUPP;
    573 	if (!((cnp->cn_flags & ISDOTDOT) == 0))
    574 		return EOPNOTSUPP;
    575 
    576 	/* check for dot, return directly if the case */
    577 	if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
    578 		vref(dvp);
    579 		*vpp = dvp;
    580 		return 0;
    581 	}
    582 
    583 	/* check for etfs */
    584 	if (dvp == rootvnode && cnp->cn_nameiop == LOOKUP) {
    585 		bool found;
    586 		mutex_enter(&etfs_lock);
    587 		found = etfs_find(cnp->cn_pnbuf, &et, false);
    588 		mutex_exit(&etfs_lock);
    589 
    590 		if (found) {
    591 			char *offset;
    592 
    593 			offset = strstr(cnp->cn_pnbuf, et->et_key);
    594 			KASSERT(offset);
    595 
    596 			rn = et->et_rn;
    597 			cnp->cn_consume += et->et_keylen
    598 			    - (cnp->cn_nameptr - offset) - cnp->cn_namelen;
    599 			if (rn->rn_va.va_type != VDIR)
    600 				cnp->cn_flags &= ~REQUIREDIR;
    601 			goto getvnode;
    602 		}
    603 	}
    604 
    605 	if (rnd->rn_flags & RUMPNODE_DIR_ET) {
    606 		uint64_t fsize;
    607 		char *newpath;
    608 		size_t newpathlen;
    609 		int hft, error;
    610 
    611 		newpathlen = strlen(rnd->rn_hostpath) + 1 + cnp->cn_namelen + 1;
    612 		newpath = malloc(newpathlen, M_TEMP, M_WAITOK);
    613 
    614 		strlcpy(newpath, rnd->rn_hostpath, newpathlen);
    615 		strlcat(newpath, "/", newpathlen);
    616 		strlcat(newpath, cnp->cn_nameptr, newpathlen);
    617 
    618 		if (rumpuser_getfileinfo(newpath, &fsize, &hft, &error)) {
    619 			free(newpath, M_TEMP);
    620 			return error;
    621 		}
    622 
    623 		/* allow only dirs and regular files */
    624 		if (hft != RUMPUSER_FT_REG && hft != RUMPUSER_FT_DIR) {
    625 			free(newpath, M_TEMP);
    626 			return ENOENT;
    627 		}
    628 
    629 		rn = makeprivate(hft_to_vtype(hft), NODEV, fsize);
    630 		rn->rn_flags |= RUMPNODE_CANRECLAIM;
    631 		if (rnd->rn_flags & RUMPNODE_DIR_ETSUBS) {
    632 			rn->rn_flags |= RUMPNODE_DIR_ET | RUMPNODE_DIR_ETSUBS;
    633 		}
    634 		rn->rn_hostpath = newpath;
    635 
    636 		goto getvnode;
    637 	} else {
    638 		LIST_FOREACH(rd, &rnd->rn_dir, rd_entries) {
    639 			if (rd->rd_namelen == cnp->cn_namelen &&
    640 			    strncmp(rd->rd_name, cnp->cn_nameptr,
    641 			      cnp->cn_namelen) == 0)
    642 				break;
    643 		}
    644 	}
    645 
    646 	if (!rd && ((cnp->cn_flags & ISLASTCN) == 0||cnp->cn_nameiop != CREATE))
    647 		return ENOENT;
    648 
    649 	if (!rd && (cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == CREATE) {
    650 		cnp->cn_flags |= SAVENAME;
    651 		return EJUSTRETURN;
    652 	}
    653 	if ((cnp->cn_flags & ISLASTCN) && cnp->cn_nameiop == DELETE)
    654 		cnp->cn_flags |= SAVENAME;
    655 
    656 	rn = rd->rd_node;
    657 
    658  getvnode:
    659 	KASSERT(rn);
    660 	mutex_enter(&reclock);
    661 	if ((vp = rn->rn_vp)) {
    662 		mutex_enter(&vp->v_interlock);
    663 		mutex_exit(&reclock);
    664 		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    665 			goto getvnode;
    666 		*vpp = vp;
    667 	} else {
    668 		mutex_exit(&reclock);
    669 		rv = makevnode(dvp->v_mount, rn, vpp);
    670 		if (rv)
    671 			return rv;
    672 	}
    673 
    674 	return 0;
    675 }
    676 
    677 static int
    678 rump_vop_getattr(void *v)
    679 {
    680 	struct vop_getattr_args /* {
    681 		struct vnode *a_vp;
    682 		struct vattr *a_vap;
    683 		kauth_cred_t a_cred;
    684 	} */ *ap = v;
    685 	struct rumpfs_node *rn = ap->a_vp->v_data;
    686 
    687 	memcpy(ap->a_vap, &rn->rn_va, sizeof(struct vattr));
    688 	return 0;
    689 }
    690 
    691 static int
    692 rump_vop_mkdir(void *v)
    693 {
    694 	struct vop_mkdir_args /* {
    695 		struct vnode *a_dvp;
    696 		struct vnode **a_vpp;
    697 		struct componentname *a_cnp;
    698 		struct vattr *a_vap;
    699 	}; */ *ap = v;
    700 	struct vnode *dvp = ap->a_dvp;
    701 	struct vnode **vpp = ap->a_vpp;
    702 	struct componentname *cnp = ap->a_cnp;
    703 	struct rumpfs_node *rnd = dvp->v_data, *rn;
    704 	int rv = 0;
    705 
    706 	rn = makeprivate(VDIR, NODEV, DEV_BSIZE);
    707 	rv = makevnode(dvp->v_mount, rn, vpp);
    708 	if (rv)
    709 		goto out;
    710 
    711 	makedir(rnd, cnp, rn);
    712 
    713  out:
    714 	PNBUF_PUT(cnp->cn_pnbuf);
    715 	vput(dvp);
    716 	return rv;
    717 }
    718 
    719 static int
    720 rump_vop_rmdir(void *v)
    721 {
    722         struct vop_rmdir_args /* {
    723                 struct vnode *a_dvp;
    724                 struct vnode *a_vp;
    725                 struct componentname *a_cnp;
    726         }; */ *ap = v;
    727 	struct vnode *dvp = ap->a_dvp;
    728 	struct vnode *vp = ap->a_vp;
    729 	struct componentname *cnp = ap->a_cnp;
    730 	struct rumpfs_node *rnd = dvp->v_data;
    731 	struct rumpfs_node *rn = vp->v_data;
    732 	int rv = 0;
    733 
    734 	if (!LIST_EMPTY(&rn->rn_dir)) {
    735 		rv = ENOTEMPTY;
    736 		goto out;
    737 	}
    738 
    739 	freedir(rnd, cnp);
    740 	rn->rn_flags |= RUMPNODE_CANRECLAIM;
    741 
    742 out:
    743 	PNBUF_PUT(cnp->cn_pnbuf);
    744 	vput(dvp);
    745 	vput(vp);
    746 
    747 	return rv;
    748 }
    749 
    750 static int
    751 rump_vop_mknod(void *v)
    752 {
    753 	struct vop_mknod_args /* {
    754 		struct vnode *a_dvp;
    755 		struct vnode **a_vpp;
    756 		struct componentname *a_cnp;
    757 		struct vattr *a_vap;
    758 	}; */ *ap = v;
    759 	struct vnode *dvp = ap->a_dvp;
    760 	struct vnode **vpp = ap->a_vpp;
    761 	struct componentname *cnp = ap->a_cnp;
    762 	struct vattr *va = ap->a_vap;
    763 	struct rumpfs_node *rnd = dvp->v_data, *rn;
    764 	int rv;
    765 
    766 	rn = makeprivate(va->va_type, va->va_rdev, DEV_BSIZE);
    767 	rv = makevnode(dvp->v_mount, rn, vpp);
    768 	if (rv)
    769 		goto out;
    770 
    771 	makedir(rnd, cnp, rn);
    772 
    773  out:
    774 	vput(dvp);
    775 	return rv;
    776 }
    777 
    778 static int
    779 rump_vop_create(void *v)
    780 {
    781 	struct vop_create_args /* {
    782 		struct vnode *a_dvp;
    783 		struct vnode **a_vpp;
    784 		struct componentname *a_cnp;
    785 		struct vattr *a_vap;
    786 	}; */ *ap = v;
    787 	struct vnode *dvp = ap->a_dvp;
    788 	struct vnode **vpp = ap->a_vpp;
    789 	struct componentname *cnp = ap->a_cnp;
    790 	struct vattr *va = ap->a_vap;
    791 	struct rumpfs_node *rnd = dvp->v_data, *rn;
    792 	int rv;
    793 
    794 	if (va->va_type != VSOCK) {
    795 		rv = EOPNOTSUPP;
    796 		goto out;
    797 	}
    798 	rn = makeprivate(VSOCK, NODEV, DEV_BSIZE);
    799 	rv = makevnode(dvp->v_mount, rn, vpp);
    800 	if (rv)
    801 		goto out;
    802 
    803 	makedir(rnd, cnp, rn);
    804 
    805  out:
    806 	vput(dvp);
    807 	return rv;
    808 }
    809 
    810 static int
    811 rump_vop_open(void *v)
    812 {
    813 	struct vop_open_args /* {
    814 		struct vnode *a_vp;
    815 		int a_mode;
    816 		kauth_cred_t a_cred;
    817 	} */ *ap = v;
    818 	struct vnode *vp = ap->a_vp;
    819 	struct rumpfs_node *rn = vp->v_data;
    820 	int mode = ap->a_mode;
    821 	int error = EINVAL;
    822 
    823 	if (vp->v_type != VREG)
    824 		return 0;
    825 
    826 	if (mode & FREAD) {
    827 		if (rn->rn_readfd != -1)
    828 			return 0;
    829 		rn->rn_readfd = rumpuser_open(rn->rn_hostpath,
    830 		    O_RDONLY, &error);
    831 	}
    832 
    833 	if (mode & FWRITE) {
    834 		if (rn->rn_writefd != -1)
    835 			return 0;
    836 		rn->rn_writefd = rumpuser_open(rn->rn_hostpath,
    837 		    O_WRONLY, &error);
    838 	}
    839 
    840 	return error;
    841 }
    842 
    843 /* simple readdir.  event omits dotstuff and periods */
    844 static int
    845 rump_vop_readdir(void *v)
    846 {
    847 	struct vop_readdir_args /* {
    848 		struct vnode *a_vp;
    849 		struct uio *a_uio;
    850 		kauth_cred_t a_cred;
    851 		int *a_eofflag;
    852 		off_t **a_cookies;
    853 		int *a_ncookies;
    854 	} */ *ap = v;
    855 	struct vnode *vp = ap->a_vp;
    856 	struct uio *uio = ap->a_uio;
    857 	struct rumpfs_node *rnd = vp->v_data;
    858 	struct rumpfs_dent *rdent;
    859 	unsigned i;
    860 	int rv = 0;
    861 
    862 	/* seek to current entry */
    863 	for (i = 0, rdent = LIST_FIRST(&rnd->rn_dir);
    864 	    (i < uio->uio_offset) && rdent;
    865 	    i++, rdent = LIST_NEXT(rdent, rd_entries))
    866 		continue;
    867 	if (!rdent)
    868 		goto out;
    869 
    870 	/* copy entries */
    871 	for (; rdent && uio->uio_resid > 0;
    872 	    rdent = LIST_NEXT(rdent, rd_entries), i++) {
    873 		struct dirent dent;
    874 
    875 		dent.d_fileno = rdent->rd_node->rn_va.va_fileid;
    876 		strlcpy(dent.d_name, rdent->rd_name, sizeof(dent.d_name));
    877 		dent.d_namlen = strlen(dent.d_name);
    878 		dent.d_type = vtype2dt(rdent->rd_node->rn_va.va_type);
    879 		dent.d_reclen = _DIRENT_RECLEN(&dent, dent.d_namlen);
    880 
    881 		if (uio->uio_resid < dent.d_reclen) {
    882 			i--;
    883 			break;
    884 		}
    885 
    886 		rv = uiomove(&dent, dent.d_reclen, uio);
    887 		if (rv) {
    888 			i--;
    889 			break;
    890 		}
    891 	}
    892 
    893  out:
    894 	if (ap->a_cookies) {
    895 		*ap->a_ncookies = 0;
    896 		*ap->a_cookies = NULL;
    897 	}
    898 	if (rdent)
    899 		*ap->a_eofflag = 0;
    900 	else
    901 		*ap->a_eofflag = 1;
    902 	uio->uio_offset = i;
    903 
    904 	return rv;
    905 }
    906 
    907 static int
    908 rump_vop_read(void *v)
    909 {
    910 	struct vop_read_args /* {
    911 		struct vnode *a_vp;
    912 		struct uio *a_uio;
    913 		int ioflags a_ioflag;
    914 		kauth_cred_t a_cred;
    915 	}; */ *ap = v;
    916 	struct vnode *vp = ap->a_vp;
    917 	struct rumpfs_node *rn = vp->v_data;
    918 	struct uio *uio = ap->a_uio;
    919 	uint8_t *buf;
    920 	size_t bufsize;
    921 	ssize_t n;
    922 	int error = 0;
    923 
    924 	bufsize = uio->uio_resid;
    925 	buf = kmem_alloc(bufsize, KM_SLEEP);
    926 	if ((n = rumpuser_pread(rn->rn_readfd, buf, bufsize,
    927 	    uio->uio_offset + rn->rn_offset, &error)) == -1)
    928 		goto out;
    929 	KASSERT(n <= bufsize);
    930 	error = uiomove(buf, n, uio);
    931 
    932  out:
    933 	kmem_free(buf, bufsize);
    934 	return error;
    935 }
    936 
    937 static int
    938 rump_vop_write(void *v)
    939 {
    940 	struct vop_read_args /* {
    941 		struct vnode *a_vp;
    942 		struct uio *a_uio;
    943 		int ioflags a_ioflag;
    944 		kauth_cred_t a_cred;
    945 	}; */ *ap = v;
    946 	struct vnode *vp = ap->a_vp;
    947 	struct rumpfs_node *rn = vp->v_data;
    948 	struct uio *uio = ap->a_uio;
    949 	uint8_t *buf;
    950 	size_t bufsize;
    951 	ssize_t n;
    952 	int error = 0;
    953 
    954 	bufsize = uio->uio_resid;
    955 	buf = kmem_alloc(bufsize, KM_SLEEP);
    956 	error = uiomove(buf, bufsize, uio);
    957 	if (error)
    958 		goto out;
    959 	KASSERT(uio->uio_resid == 0);
    960 	n = rumpuser_pwrite(rn->rn_writefd, buf, bufsize,
    961 	    (uio->uio_offset-bufsize) + rn->rn_offset, &error);
    962 	if (n >= 0) {
    963 		KASSERT(n <= bufsize);
    964 		uio->uio_resid = bufsize - n;
    965 	}
    966 
    967  out:
    968 	kmem_free(buf, bufsize);
    969 	return error;
    970 }
    971 
    972 static int
    973 rump_vop_success(void *v)
    974 {
    975 
    976 	return 0;
    977 }
    978 
    979 static int
    980 rump_vop_inactive(void *v)
    981 {
    982 	struct vop_inactive_args /* {
    983 		struct vnode *a_vp;
    984 		bool *a_recycle;
    985 	} */ *ap = v;
    986 	struct vnode *vp = ap->a_vp;
    987 	struct rumpfs_node *rn = vp->v_data;
    988 	int error;
    989 
    990 	if (vp->v_type == VREG) {
    991 		if (rn->rn_readfd != -1) {
    992 			rumpuser_close(rn->rn_readfd, &error);
    993 			rn->rn_readfd = -1;
    994 		}
    995 		if (rn->rn_writefd != -1) {
    996 			rumpuser_close(rn->rn_writefd, &error);
    997 			rn->rn_writefd = -1;
    998 		}
    999 	}
   1000 	*ap->a_recycle = (rn->rn_flags & RUMPNODE_CANRECLAIM) ? true : false;
   1001 
   1002 	VOP_UNLOCK(vp);
   1003 	return 0;
   1004 }
   1005 
   1006 static int
   1007 rump_vop_reclaim(void *v)
   1008 {
   1009 	struct vop_reclaim_args /* {
   1010 		struct vnode *a_vp;
   1011 	} */ *ap = v;
   1012 	struct vnode *vp = ap->a_vp;
   1013 	struct rumpfs_node *rn = vp->v_data;
   1014 
   1015 	mutex_enter(&reclock);
   1016 	rn->rn_vp = NULL;
   1017 	mutex_exit(&reclock);
   1018 	vp->v_data = NULL;
   1019 
   1020 	if (rn->rn_flags & RUMPNODE_CANRECLAIM) {
   1021 		if (rn->rn_hostpath)
   1022 			free(rn->rn_hostpath, M_TEMP);
   1023 		kmem_free(rn, sizeof(*rn));
   1024 	}
   1025 
   1026 	return 0;
   1027 }
   1028 
   1029 static int
   1030 rump_vop_spec(void *v)
   1031 {
   1032 	struct vop_generic_args *ap = v;
   1033 	int (**opvec)(void *);
   1034 
   1035 	switch (ap->a_desc->vdesc_offset) {
   1036 	case VOP_ACCESS_DESCOFFSET:
   1037 	case VOP_GETATTR_DESCOFFSET:
   1038 	case VOP_LOCK_DESCOFFSET:
   1039 	case VOP_UNLOCK_DESCOFFSET:
   1040 	case VOP_RECLAIM_DESCOFFSET:
   1041 		opvec = rump_vnodeop_p;
   1042 		break;
   1043 	default:
   1044 		opvec = spec_vnodeop_p;
   1045 		break;
   1046 	}
   1047 
   1048 	return VOCALL(opvec, ap->a_desc->vdesc_offset, v);
   1049 }
   1050 
   1051 /*
   1052  * Begin vfs-level stuff
   1053  */
   1054 
   1055 VFS_PROTOS(rumpfs);
   1056 struct vfsops rumpfs_vfsops = {
   1057 	.vfs_name =		MOUNT_RUMPFS,
   1058 	.vfs_min_mount_data = 	0,
   1059 	.vfs_mount =		rumpfs_mount,
   1060 	.vfs_start =		(void *)nullop,
   1061 	.vfs_unmount = 		rumpfs_unmount,
   1062 	.vfs_root =		rumpfs_root,
   1063 	.vfs_quotactl =		(void *)eopnotsupp,
   1064 	.vfs_statvfs =		genfs_statvfs,
   1065 	.vfs_sync =		(void *)nullop,
   1066 	.vfs_vget =		rumpfs_vget,
   1067 	.vfs_fhtovp =		(void *)eopnotsupp,
   1068 	.vfs_vptofh =		(void *)eopnotsupp,
   1069 	.vfs_init =		rumpfs_init,
   1070 	.vfs_reinit =		NULL,
   1071 	.vfs_done =		rumpfs_done,
   1072 	.vfs_mountroot =	rumpfs_mountroot,
   1073 	.vfs_snapshot =		(void *)eopnotsupp,
   1074 	.vfs_extattrctl =	(void *)eopnotsupp,
   1075 	.vfs_suspendctl =	(void *)eopnotsupp,
   1076 	.vfs_opv_descs =	rump_opv_descs,
   1077 	/* vfs_refcount */
   1078 	/* vfs_list */
   1079 };
   1080 
   1081 int
   1082 rumpfs_mount(struct mount *mp, const char *mntpath, void *arg, size_t *alen)
   1083 {
   1084 
   1085 	return EOPNOTSUPP;
   1086 }
   1087 
   1088 int
   1089 rumpfs_unmount(struct mount *mp, int flags)
   1090 {
   1091 
   1092 	/* if going for it, just lie about it */
   1093 	if (panicstr)
   1094 		return 0;
   1095 
   1096 	return EOPNOTSUPP; /* ;) */
   1097 }
   1098 
   1099 int
   1100 rumpfs_root(struct mount *mp, struct vnode **vpp)
   1101 {
   1102 	struct rumpfs_mount *rfsmp = mp->mnt_data;
   1103 
   1104 	vget(rfsmp->rfsmp_rvp, LK_EXCLUSIVE | LK_RETRY);
   1105 	*vpp = rfsmp->rfsmp_rvp;
   1106 	return 0;
   1107 }
   1108 
   1109 int
   1110 rumpfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
   1111 {
   1112 
   1113 	return EOPNOTSUPP;
   1114 }
   1115 
   1116 void
   1117 rumpfs_init()
   1118 {
   1119 
   1120 	CTASSERT(RUMP_ETFS_SIZE_ENDOFF == RUMPBLK_SIZENOTSET);
   1121 
   1122 	mutex_init(&reclock, MUTEX_DEFAULT, IPL_NONE);
   1123 	mutex_init(&etfs_lock, MUTEX_DEFAULT, IPL_NONE);
   1124 }
   1125 
   1126 void
   1127 rumpfs_done()
   1128 {
   1129 
   1130 	mutex_destroy(&reclock);
   1131 	mutex_destroy(&etfs_lock);
   1132 }
   1133 
   1134 int
   1135 rumpfs_mountroot()
   1136 {
   1137 	struct mount *mp;
   1138 	struct rumpfs_mount *rfsmp;
   1139 	struct rumpfs_node *rn;
   1140 	int error;
   1141 
   1142 	if ((error = vfs_rootmountalloc(MOUNT_RUMPFS, "rootdev", &mp)) != 0) {
   1143 		vrele(rootvp);
   1144 		return error;
   1145 	}
   1146 
   1147 	rfsmp = kmem_alloc(sizeof(*rfsmp), KM_SLEEP);
   1148 
   1149 	rn = makeprivate(VDIR, NODEV, DEV_BSIZE);
   1150 	error = makevnode(mp, rn, &rfsmp->rfsmp_rvp);
   1151 	if (error)
   1152 		panic("could not create root vnode: %d", error);
   1153 	rfsmp->rfsmp_rvp->v_vflag |= VV_ROOT;
   1154 	VOP_UNLOCK(rfsmp->rfsmp_rvp);
   1155 
   1156 	mutex_enter(&mountlist_lock);
   1157 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
   1158 	mutex_exit(&mountlist_lock);
   1159 
   1160 	mp->mnt_data = rfsmp;
   1161 	mp->mnt_stat.f_namemax = MAXNAMLEN;
   1162 	mp->mnt_stat.f_iosize = 512;
   1163 	mp->mnt_flag |= MNT_LOCAL;
   1164 	mp->mnt_iflag |= IMNT_MPSAFE;
   1165 	vfs_getnewfsid(mp);
   1166 
   1167 	error = set_statvfs_info("/", UIO_SYSSPACE, "rumpfs", UIO_SYSSPACE,
   1168 	    mp->mnt_op->vfs_name, mp, curlwp);
   1169 	if (error)
   1170 		panic("set statvfsinfo for rootfs failed");
   1171 
   1172 	vfs_unbusy(mp, false, NULL);
   1173 
   1174 	return 0;
   1175 }
   1176