Home | History | Annotate | Line # | Download | only in libp2k
      1 /*	$NetBSD: p2k.c,v 1.75 2022/05/24 20:50:17 andvar Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007, 2008, 2009  Antti Kantee.  All Rights Reserved.
      5  *
      6  * Development of this software was supported by the
      7  * Finnish Cultural Foundation.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * puffs 2k, i.e. puffs 2 kernel.  Converts the puffs protocol to
     33  * the kernel vfs protocol and vice versa.
     34  *
     35  * A word about reference counting: puffs in the kernel is the king of
     36  * reference counting.  We must maintain a vnode alive and kicking
     37  * until the kernel tells us to reclaim it.  Therefore we make sure
     38  * we never accidentally lose a vnode.  Before calling operations which
     39  * decrease the refcount we always bump the refcount up to compensate.
     40  * Come inactive, if the file system thinks that the vnode should be
     41  * put out of its misery, it will set the recycle flag.  We use this
     42  * to tell the kernel to reclaim the vnode.  Only in reclaim do we
     43  * really nuke the last reference.
     44  */
     45 
     46 #include <sys/cdefs.h>
     47 #include <sys/mount.h>
     48 #include <sys/param.h>
     49 #include <sys/lock.h>
     50 #include <sys/namei.h>
     51 #include <sys/dirent.h>
     52 #include <sys/hash.h>
     53 
     54 #include <assert.h>
     55 #include <errno.h>
     56 #include <puffs.h>
     57 #include <stdlib.h>
     58 #include <stdio.h>
     59 
     60 #include <rump/rump.h>
     61 #include <rump/rumpvnode_if.h>
     62 #include <rump/p2k.h>
     63 #include <rump/ukfs.h>
     64 
     65 #include <uvm/uvm_pager.h>
     66 
     67 /* NetBSD-5 compat */
     68 #ifndef MOUNT_RUMPFS
     69 #define MOUNT_RUMPFS    "rumpfs"
     70 #endif
     71 
     72 PUFFSOP_PROTOS(p2k)
     73 
     74 LIST_HEAD(p2k_vp_hash, p2k_node);
     75 #define NHASHBUCK (1<<16)
     76 struct p2k_mount {
     77 	struct vnode *p2m_rvp;
     78 	struct puffs_usermount *p2m_pu;
     79 	struct ukfs *p2m_ukfs;
     80 	struct p2k_vp_hash p2m_vphash[NHASHBUCK];
     81 	struct mount *p2m_mp;
     82 	int p2m_nvnodes;
     83 	int p2m_imtmpfsman;
     84 	bool p2m_hasdebug;
     85 };
     86 
     87 struct p2k_node {
     88 	struct puffs_node p2n_pn;
     89 	struct vnode *p2n_vp;
     90 
     91 	LIST_ENTRY(p2k_node) p2n_entries;
     92 };
     93 
     94 #define OPC2VP(opc) (((struct p2k_node *)opc)->p2n_vp)
     95 
     96 static int haswizard;
     97 static uid_t wizarduid;
     98 
     99 static struct kauth_cred *
    100 cred_create(const struct puffs_cred *pcr)
    101 {
    102 	gid_t groups[NGROUPS];
    103 	uid_t uid;
    104 	gid_t gid;
    105 	short ngroups = __arraycount(groups);
    106 
    107 	if (haswizard) {
    108 		uid = wizarduid;
    109 	} else {
    110 		if (puffs_cred_getuid(pcr, &uid) == -1)
    111 			uid = 0;
    112 	}
    113 	if (puffs_cred_getgid(pcr, &gid) == -1)
    114 		gid = 0;
    115 	puffs_cred_getgroups(pcr, groups, &ngroups);
    116 
    117 	/* LINTED: ngroups is ok */
    118 	return rump_pub_cred_create(uid, gid, ngroups, groups);
    119 }
    120 
    121 static __inline void
    122 cred_destroy(struct kauth_cred *cred)
    123 {
    124 
    125 	rump_pub_cred_put(cred);
    126 }
    127 
    128 static struct componentname *
    129 makecn(const struct puffs_cn *pcn)
    130 {
    131 	struct kauth_cred *cred;
    132 
    133 	cred = cred_create(pcn->pcn_cred);
    134 	/* LINTED: prehistoric types in first two args */
    135 	return rump_pub_makecn(pcn->pcn_nameiop, pcn->pcn_flags,
    136 	    pcn->pcn_name, pcn->pcn_namelen, cred, rump_pub_lwproc_curlwp());
    137 }
    138 
    139 static __inline void
    140 freecn(struct componentname *cnp)
    141 {
    142 
    143 	rump_pub_freecn(cnp, RUMPCN_FREECRED);
    144 }
    145 
    146 static void
    147 makelwp(struct puffs_usermount *pu)
    148 {
    149 	pid_t pid;
    150 	lwpid_t lid;
    151 
    152 	puffs_cc_getcaller(puffs_cc_getcc(pu), &pid, &lid);
    153 	rump_pub_allbetsareoff_setid(pid, lid);
    154 }
    155 
    156 static volatile sig_atomic_t dodump;
    157 static void
    158 dumpmp(struct puffs_usermount *pu)
    159 {
    160 	struct puffs_statvfs svfsb;
    161 
    162 	if (dodump && p2k_fs_statvfs(pu, &svfsb) == 0) {
    163 		rump_pub_vfs_mount_print(svfsb.f_mntonname, dodump-1);
    164 	}
    165 
    166 	dodump = 0;
    167 }
    168 
    169 static void
    170 sighand(int sig)
    171 {
    172 
    173 	if (sig == SIGINFO)
    174 		dodump = 1;
    175 	else if (sig == SIGUSR1)
    176 		dodump = 2;
    177 }
    178 
    179 static __inline struct p2k_vp_hash *
    180 gethash(struct p2k_mount *p2m, struct vnode *vp)
    181 {
    182 	uint32_t hash;
    183 
    184 	hash = hash32_buf(&vp, sizeof(vp), HASH32_BUF_INIT);
    185 	return &p2m->p2m_vphash[hash % NHASHBUCK];
    186 }
    187 
    188 /*
    189  * Find node based on hash of vnode pointer.  If vnode is found,
    190  * releases one reference to vnode based on the fact that we just
    191  * performed a lookup for it.
    192  *
    193  * If the optinal p2n_storage parameter is passed, it is used instead
    194  * of allocating more memory.  This allows for easier error recovery.
    195  */
    196 static struct p2k_node *
    197 getp2n(struct p2k_mount *p2m, struct vnode *vp, bool initial,
    198 	struct p2k_node *p2n_storage)
    199 {
    200 	struct p2k_vp_hash *hl;
    201 	struct p2k_node *p2n = NULL;
    202 
    203 	/* p2n_storage => initial */
    204 	assert(!p2n_storage || initial);
    205 
    206 	hl = gethash(p2m, vp);
    207 	if (!initial)
    208 		LIST_FOREACH(p2n, hl, p2n_entries)
    209 			if (p2n->p2n_vp == vp)
    210 				break;
    211 
    212 	hl = gethash(p2m, vp);
    213 	if (p2n) {
    214 		rump_pub_vp_rele(vp);
    215 	} else {
    216 		if (p2n_storage)
    217 			p2n = p2n_storage;
    218 		else
    219 			p2n = malloc(sizeof(*p2n));
    220 		if (!p2n) {
    221 			rump_pub_vp_rele(vp);
    222 			return NULL;
    223 		}
    224 		memset(p2n, 0, sizeof(*p2n));
    225 		LIST_INSERT_HEAD(hl, p2n, p2n_entries);
    226 		p2n->p2n_vp = vp;
    227 	}
    228 	return p2n;
    229 }
    230 
    231 static void
    232 freep2n(struct p2k_node *p2n)
    233 {
    234 
    235 	assert(p2n->p2n_vp == NULL);
    236 	LIST_REMOVE(p2n, p2n_entries);
    237 	free(p2n);
    238 }
    239 
    240 /*ARGSUSED*/
    241 static void
    242 p2k_errcatcher(struct puffs_usermount *pu, uint8_t type, int error,
    243 	const char *str, puffs_cookie_t cook)
    244 {
    245 
    246 	fprintf(stderr, "type %d, error %d, cookie %p (%s)\n",
    247 	    type, error, cook, str);
    248 
    249 	/*
    250 	 * Trap all EINVAL responses to lookup.  It most likely means
    251 	 * that we supplied VNON/VBAD as the type.  The real kernel
    252 	 * doesn't panic from this either, but just handles it.
    253 	 */
    254 	if (type != PUFFS_VN_LOOKUP && error == EINVAL)
    255 		abort();
    256 }
    257 
    258 /* just to avoid annoying loop when singlestepping */
    259 static struct p2k_mount *
    260 allocp2m(void)
    261 {
    262 	struct p2k_mount *p2m;
    263 	int i;
    264 
    265 	p2m = malloc(sizeof(*p2m));
    266 	if (p2m == NULL)
    267 		return NULL;
    268 	memset(p2m, 0, sizeof(*p2m));
    269 
    270 	for (i = 0; i < NHASHBUCK; i++)
    271 		LIST_INIT(&p2m->p2m_vphash[i]);
    272 
    273 	return p2m;
    274 }
    275 
    276 struct p2k_mount *
    277 p2k_init(uint32_t puffs_flags)
    278 {
    279 	struct puffs_ops *pops;
    280 	struct p2k_mount *p2m;
    281 	char *envbuf;
    282 	bool dodaemon;
    283 	bool hasdebug;
    284 
    285 	PUFFSOP_INIT(pops);
    286 
    287 	PUFFSOP_SET(pops, p2k, fs, statvfs);
    288 	PUFFSOP_SET(pops, p2k, fs, unmount);
    289 	PUFFSOP_SET(pops, p2k, fs, sync);
    290 	PUFFSOP_SET(pops, p2k, fs, fhtonode);
    291 	PUFFSOP_SET(pops, p2k, fs, nodetofh);
    292 	PUFFSOP_SET(pops, p2k, fs, extattrctl);
    293 
    294 	PUFFSOP_SET(pops, p2k, node, lookup);
    295 	PUFFSOP_SET(pops, p2k, node, create);
    296 	PUFFSOP_SET(pops, p2k, node, mknod);
    297 	PUFFSOP_SET(pops, p2k, node, open);
    298 	PUFFSOP_SET(pops, p2k, node, close);
    299 	PUFFSOP_SET(pops, p2k, node, access);
    300 	PUFFSOP_SET(pops, p2k, node, getattr);
    301 	PUFFSOP_SET(pops, p2k, node, setattr);
    302 #if 0
    303 	PUFFSOP_SET(pops, p2k, node, poll);
    304 #endif
    305 	PUFFSOP_SET(pops, p2k, node, mmap);
    306 	PUFFSOP_SET(pops, p2k, node, fsync);
    307 	PUFFSOP_SET(pops, p2k, node, seek);
    308 	PUFFSOP_SET(pops, p2k, node, remove);
    309 	PUFFSOP_SET(pops, p2k, node, link);
    310 	PUFFSOP_SET(pops, p2k, node, rename);
    311 	PUFFSOP_SET(pops, p2k, node, mkdir);
    312 	PUFFSOP_SET(pops, p2k, node, rmdir);
    313 	PUFFSOP_SET(pops, p2k, node, symlink);
    314 	PUFFSOP_SET(pops, p2k, node, readdir);
    315 	PUFFSOP_SET(pops, p2k, node, readlink);
    316 	PUFFSOP_SET(pops, p2k, node, read);
    317 	PUFFSOP_SET(pops, p2k, node, write);
    318 
    319 	PUFFSOP_SET(pops, p2k, node, pathconf);
    320 
    321 	PUFFSOP_SET(pops, p2k, node, inactive);
    322 	PUFFSOP_SET(pops, p2k, node, reclaim);
    323 
    324 	PUFFSOP_SET(pops, p2k, node, getextattr);
    325 	PUFFSOP_SET(pops, p2k, node, setextattr);
    326 	PUFFSOP_SET(pops, p2k, node, listextattr);
    327 	PUFFSOP_SET(pops, p2k, node, deleteextattr);
    328 
    329 	dodaemon = true;
    330 	hasdebug = false;
    331 
    332 	if (getenv("P2K_DEBUG") != NULL) {
    333 		puffs_flags |= PUFFS_FLAG_OPDUMP;
    334 		dodaemon = false;
    335 		hasdebug = true;
    336 	}
    337 	if (getenv("P2K_NODETACH") != NULL) {
    338 		dodaemon = false;
    339 	}
    340 	if (getenv("P2K_NOCACHE_PAGE") != NULL) {
    341 		puffs_flags |= PUFFS_KFLAG_NOCACHE_PAGE;
    342 	}
    343 	if (getenv("P2K_NOCACHE_NAME") != NULL) {
    344 		puffs_flags |= PUFFS_KFLAG_NOCACHE_NAME;
    345 	}
    346 	if (getenv("P2K_NOCACHE") != NULL) {
    347 		puffs_flags |= PUFFS_KFLAG_NOCACHE;
    348 	}
    349 	if ((envbuf = getenv("P2K_WIZARDUID")) != NULL) {
    350 		char *ep;
    351 
    352 		wizarduid = strtoul(envbuf, &ep, 10);
    353 		if (envbuf[0] == '\0' || *ep != '\0') {
    354 			printf("P2K_WIZARDUID: invalid uid %s\n", envbuf);
    355 		} else if (wizarduid > UID_MAX) {
    356 			printf("P2K_WIZARDUID: uid %s out-of-range\n", envbuf);
    357 		} else {
    358 			haswizard = 1;
    359 			printf("P2K WIZARD MODE: using uid %d\n", wizarduid);
    360 		}
    361 	}
    362 
    363 	/*
    364 	 * Explicitly tell that our cookies can be treated as
    365 	 * puffs_node, since we never let libpuffs know by
    366 	 * calling  call puffs_pn_new()
    367 	 */
    368 	puffs_flags |= PUFFS_FLAG_PNCOOKIE;
    369 
    370 	p2m = allocp2m();
    371 	if (p2m == NULL)
    372 		return NULL;
    373 	p2m->p2m_pu = puffs_init(pops, PUFFS_DEFER, PUFFS_DEFER,
    374 	    PUFFS_DEFER, puffs_flags);
    375 	if (p2m->p2m_pu == NULL) {
    376 		int sverrno = errno;
    377 		free(p2m);
    378 		errno = sverrno;
    379 		return NULL;
    380 	}
    381 	p2m->p2m_hasdebug = hasdebug;
    382 
    383 	if (dodaemon) {
    384 		if (puffs_daemon(p2m->p2m_pu, 1, 1) == -1) {
    385 			int sverrno = errno;
    386 			p2k_cancel(p2m, sverrno);
    387 			errno = sverrno;
    388 			p2m = NULL;
    389 		}
    390 	}
    391 	if (p2m)
    392 		rump_init();
    393 
    394 	rump_pub_lwproc_rfork(RUMP_RFCFDG);
    395 
    396 	return p2m;
    397 }
    398 
    399 void
    400 p2k_cancel(struct p2k_mount *p2m, int error)
    401 {
    402 
    403 	puffs_cancel(p2m->p2m_pu, error);
    404 	free(p2m);
    405 }
    406 
    407 static int
    408 setupfs(struct p2k_mount *p2m, const char *vfsname, const char *devpath,
    409 	struct ukfs_part *part, const char *mountpath, int mntflags,
    410 	void *arg, size_t alen)
    411 {
    412 	char partpath[UKFS_DEVICE_MAXPATHLEN];
    413 	char partbuf[UKFS_DEVICE_MAXSTR];
    414 	char typebuf[PUFFS_TYPELEN];
    415 	struct puffs_usermount *pu = p2m->p2m_pu;
    416 	struct p2k_node *p2n_root;
    417 	struct ukfs *ukfs = NULL;
    418 	extern int puffs_fakecc;
    419 	int rv = -1, sverrno;
    420 
    421 	strcpy(typebuf, "p2k|");
    422 	if (strcmp(vfsname, "puffs") == 0) { /* XXX */
    423 		struct puffs_kargs *args = arg;
    424 		strlcat(typebuf, args->pa_typename, sizeof(typebuf));
    425 	} else {
    426 		strlcat(typebuf, vfsname, sizeof(typebuf));
    427 	}
    428 
    429 	strlcpy(partpath, devpath, sizeof(partpath));
    430 	if (ukfs_part_tostring(part, partbuf, sizeof(partbuf))) {
    431 		strlcat(partpath, partbuf, sizeof(partpath));
    432 	}
    433 	puffs_setmntinfo(pu, partpath, typebuf);
    434 
    435 	if (ukfs_init() == -1)
    436 		goto out;
    437 
    438 	/*
    439 	 * If we're mounting rumpfs, actually do no mount and redirect
    440 	 * requests to rump fs namespace root.  Strictly speaking, this
    441 	 * is not correct, but I don't think anyone will notice.
    442 	 * After all, we're mostly interested in things which reside
    443 	 * specifically on the rootfs, namely the contents of /dev
    444 	 */
    445 	if (strcmp(vfsname, MOUNT_RUMPFS) == 0) {
    446 		if ((rv = rump_pub_vfs_getmp("/", &p2m->p2m_mp)) != 0) {
    447 			errno = rv;
    448 			rv = -1;
    449 			goto out;
    450 		}
    451 	} else {
    452 		if (part != ukfs_part_na)
    453 			ukfs = ukfs_mount_disk(vfsname, devpath, part,
    454 			    mountpath, mntflags, arg, alen);
    455 		else
    456 			ukfs = ukfs_mount(vfsname, devpath, mountpath, mntflags,
    457 			    arg, alen);
    458 		if (ukfs == NULL)
    459 			goto out;
    460 		ukfs_setspecific(ukfs, p2m);
    461 		p2m->p2m_ukfs = ukfs;
    462 		p2m->p2m_mp = ukfs_getmp(ukfs);
    463 	}
    464 	if ((rv = rump_pub_vfs_root(p2m->p2m_mp, &p2m->p2m_rvp, 0)) != 0) {
    465 		errno = rv;
    466 		rv = -1;
    467 		goto out;
    468 	}
    469 
    470 	p2m->p2m_pu = pu;
    471 
    472 	/*
    473 	 * Detect tmpfs.  XXX: this is a kludge.  See inactive().
    474 	 *
    475 	 * In reality we'd want "does file system use anon objects
    476 	 * for storage?".  But since tmpfs hides the anon object from
    477 	 * the public interface, we can't actually detect it sanely.
    478 	 * Therefore, use this kludge.
    479 	 */
    480 	p2m->p2m_imtmpfsman = strcmp(vfsname, MOUNT_TMPFS) == 0;
    481 
    482 	p2n_root = getp2n(p2m, p2m->p2m_rvp, true, NULL);
    483 	puffs_setfhsize(pu, 0, PUFFS_FHFLAG_PASSTHROUGH);
    484 	puffs_setstacksize(pu, PUFFS_STACKSIZE_MIN);
    485 	puffs_fakecc = 1;
    486 	puffs_set_prepost(pu, makelwp, NULL);
    487 
    488 	if (p2m->p2m_hasdebug) {
    489 		struct timespec ts;
    490 
    491 		signal(SIGINFO, sighand);
    492 		signal(SIGUSR1, sighand);
    493 
    494 		ts.tv_sec = 0;
    495 		ts.tv_nsec = 1000*1000*10; /* 10ms */
    496 		puffs_ml_setloopfn(pu, dumpmp);
    497 		puffs_ml_settimeout(pu, &ts);
    498 	}
    499 	puffs_set_errnotify(pu, p2k_errcatcher);
    500 
    501 	puffs_setspecific(pu, p2m);
    502 	rv = puffs_mount(pu, mountpath, mntflags, p2n_root);
    503 
    504  out:
    505 	if (rv == -1) {
    506 		sverrno = errno;
    507 		puffs_cancel(pu, sverrno);
    508 		if (ukfs)
    509 			ukfs_release(p2m->p2m_ukfs, UKFS_RELFLAG_FORCE);
    510 		free(p2m);
    511 		errno = sverrno;
    512 	}
    513 
    514 	return rv;
    515 }
    516 
    517 int
    518 p2k_mainloop(struct p2k_mount *p2m)
    519 {
    520 	int rv, sverrno;
    521 
    522 	rv = puffs_mainloop(p2m->p2m_pu);
    523 	sverrno = errno;
    524 	puffs_exit(p2m->p2m_pu, 1);
    525 	if (p2m->p2m_ukfs)
    526 		ukfs_release(p2m->p2m_ukfs, UKFS_RELFLAG_FORCE);
    527 	free(p2m);
    528 
    529 	if (rv == -1)
    530 		errno = sverrno;
    531 	return rv;
    532 }
    533 
    534 int
    535 p2k_run_fs(const char *vfsname, const char *devpath, const char *mountpath,
    536 	int mntflags, void *arg, size_t alen, uint32_t puffs_flags)
    537 {
    538 	struct p2k_mount *p2m;
    539 	int rv;
    540 
    541 	p2m = p2k_init(puffs_flags);
    542 	if (p2m == NULL)
    543 		return -1;
    544 	rv = setupfs(p2m, vfsname, devpath, ukfs_part_na, mountpath,
    545 	    mntflags, arg, alen);
    546 	if (rv == -1)
    547 		return rv;
    548 	return p2k_mainloop(p2m);
    549 }
    550 
    551 int
    552 p2k_run_diskfs(const char *vfsname, const char *devpath, struct ukfs_part *part,
    553 	const char *mountpath, int mntflags, void *arg, size_t alen,
    554 	uint32_t puffs_flags)
    555 {
    556 	struct p2k_mount *p2m;
    557 	int rv;
    558 
    559 	p2m = p2k_init(puffs_flags);
    560 	if (p2m == NULL)
    561 		return -1;
    562 	rv = setupfs(p2m, vfsname, devpath, part, mountpath, mntflags,
    563 	    arg, alen);
    564 	if (rv == -1)
    565 		return rv;
    566 	return p2k_mainloop(p2m);
    567 }
    568 
    569 int
    570 p2k_setup_fs(struct p2k_mount *p2m, const char *vfsname, const char *devpath,
    571 	const char *mountpath, int mntflags, void *arg, size_t alen)
    572 {
    573 
    574 	return setupfs(p2m, vfsname, devpath, ukfs_part_na, mountpath,
    575 	    mntflags, arg, alen);
    576 }
    577 
    578 int
    579 p2k_setup_diskfs(struct p2k_mount *p2m, const char *vfsname,
    580 	const char *devpath, struct ukfs_part *part, const char *mountpath,
    581 	int mntflags, void *arg, size_t alen)
    582 {
    583 
    584 	return setupfs(p2m, vfsname, devpath, part, mountpath, mntflags,
    585 	    arg, alen);
    586 }
    587 
    588 int
    589 p2k_fs_statvfs(struct puffs_usermount *pu, struct puffs_statvfs *sbp)
    590 {
    591 	struct p2k_mount *p2m = puffs_getspecific(pu);
    592 	struct mount *mp = p2m->p2m_mp;
    593 	struct statvfs sb;
    594 	puffs_statvfs_to_statvfs(sbp, &sb);
    595 
    596 	return rump_pub_vfs_statvfs(mp, &sb);
    597 }
    598 
    599 /*ARGSUSED*/
    600 int
    601 p2k_fs_unmount(struct puffs_usermount *pu, int flags)
    602 {
    603 	struct p2k_mount *p2m = puffs_getspecific(pu);
    604 	struct ukfs *fs = p2m->p2m_ukfs;
    605 	int error = 0;
    606 
    607 	rump_pub_vp_rele(p2m->p2m_rvp);
    608 
    609 	if (fs) {
    610 		if (ukfs_release(fs, 0) != 0) {
    611 			struct puffs_statvfs svfsb;
    612 
    613 			if (p2m->p2m_hasdebug
    614 			    && p2k_fs_statvfs(pu, &svfsb) == 0) {
    615 				printf("\nSOFT UNMOUNT FAILED, MP INFO DUMP\n");
    616 				rump_pub_vfs_mount_print(svfsb.f_mntonname, 1);
    617 			}
    618 			ukfs_release(fs, UKFS_RELFLAG_FORCE);
    619 			error = 0;
    620 		}
    621 	}
    622 	p2m->p2m_ukfs = NULL;
    623 
    624 	if (p2m->p2m_hasdebug) {
    625 		printf("-- rump kernel event counters --\n");
    626 		rump_printevcnts();
    627 		printf("-- end of event counters --\n");
    628 	}
    629 
    630 	return error;
    631 }
    632 
    633 int
    634 p2k_fs_sync(struct puffs_usermount *pu, int waitfor,
    635 	const struct puffs_cred *pcr)
    636 {
    637 	struct p2k_mount *p2m = puffs_getspecific(pu);
    638 	struct mount *mp = p2m->p2m_mp;
    639 	struct kauth_cred *cred;
    640 	int rv;
    641 
    642 	cred = cred_create(pcr);
    643 	rv = rump_pub_vfs_sync(mp, waitfor, cred);
    644 	cred_destroy(cred);
    645 
    646 	return rv;
    647 }
    648 
    649 /*ARGSUSED*/
    650 int
    651 p2k_fs_fhtonode(struct puffs_usermount *pu, void *fid, size_t fidsize,
    652 	struct puffs_newinfo *pni)
    653 {
    654 	struct p2k_mount *p2m = puffs_getspecific(pu);
    655 	struct mount *mp = p2m->p2m_mp;
    656 	struct p2k_node *p2n;
    657 	struct vnode *vp;
    658 	enum rump_vtype vtype;
    659 	voff_t vsize;
    660 	uint64_t rdev; /* XXX: allows running this on NetBSD 5.0 */
    661 	int rv;
    662 
    663 	rv = rump_pub_vfs_fhtovp(mp, fid, &vp);
    664 	if (rv)
    665 		return rv;
    666 	RUMP_VOP_UNLOCK(vp);
    667 
    668 	p2n = getp2n(p2m, vp, false, NULL);
    669 	if (p2n == NULL)
    670 		return ENOMEM;
    671 
    672 	puffs_newinfo_setcookie(pni, p2n);
    673 	rump_pub_getvninfo(vp, &vtype, &vsize, (void *)&rdev);
    674 	puffs_newinfo_setvtype(pni, (enum vtype)vtype);
    675 	puffs_newinfo_setsize(pni, vsize);
    676 	/* LINTED: yea, it'll lose accuracy, but that's life */
    677 	puffs_newinfo_setrdev(pni, rdev);
    678 
    679 	return 0;
    680 }
    681 
    682 /*ARGSUSED*/
    683 int
    684 p2k_fs_nodetofh(struct puffs_usermount *pu, puffs_cookie_t cookie, void *fid,
    685 	size_t *fidsize)
    686 {
    687 	struct vnode *vp = OPC2VP(cookie);
    688 
    689 	return rump_pub_vfs_vptofh(vp, fid, fidsize);
    690 }
    691 
    692 int
    693 p2k_fs_extattrctl(struct puffs_usermount *pu, int cmd,
    694 	puffs_cookie_t cookie, int flags,
    695 	int attrnamespace, const char *attrname)
    696 {
    697 	struct p2k_mount *p2m = puffs_getspecific(pu);
    698 	struct mount *mp = p2m->p2m_mp;
    699 	struct vnode *vp;
    700 
    701 	if (flags & PUFFS_EXTATTRCTL_HASNODE) {
    702 		vp = OPC2VP(cookie);
    703 		RUMP_VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);
    704 	} else {
    705 		vp = NULL;
    706 	}
    707 
    708 	/* vfsop unlocks (but doesn't release) vnode, so we're done here */
    709 	return rump_pub_vfs_extattrctl(mp, cmd, vp, attrnamespace, attrname);
    710 }
    711 
    712 /*ARGSUSED*/
    713 int
    714 p2k_node_lookup(struct puffs_usermount *pu, puffs_cookie_t opc,
    715 	struct puffs_newinfo *pni, const struct puffs_cn *pcn)
    716 {
    717 	struct p2k_mount *p2m = puffs_getspecific(pu);
    718 	struct p2k_node *p2n_dir = opc, *p2n;
    719 	struct componentname *cn;
    720 	struct vnode *dvp = p2n_dir->p2n_vp, *vp;
    721 	enum rump_vtype vtype;
    722 	voff_t vsize;
    723 	uint64_t rdev; /* XXX: uint64_t because of stack overwrite in compat */
    724 	int rv;
    725 
    726 	cn = makecn(pcn);
    727 	RUMP_VOP_LOCK(dvp, LK_EXCLUSIVE);
    728 	rv = RUMP_VOP_LOOKUP(dvp, &vp, cn);
    729 	RUMP_VOP_UNLOCK(dvp);
    730 	freecn(cn);
    731 
    732 	if (rv) {
    733 		if (rv == RUMP_EJUSTRETURN) {
    734 			rv = ENOENT;
    735 		}
    736 		return rv;
    737 	}
    738 
    739 	p2n = getp2n(p2m, vp, false, NULL);
    740 	if (p2n == NULL) {
    741 		return ENOMEM;
    742 	}
    743 
    744 	puffs_newinfo_setcookie(pni, p2n);
    745 	rump_pub_getvninfo(vp, &vtype, &vsize, (void *)&rdev);
    746 	puffs_newinfo_setvtype(pni, (enum vtype)vtype);
    747 	puffs_newinfo_setsize(pni, vsize);
    748 	/* LINTED: yea, it'll lose accuracy, but that's life */
    749 	puffs_newinfo_setrdev(pni, rdev);
    750 
    751 	return 0;
    752 }
    753 
    754 #define VERS_TIMECHANGE 599000700
    755 static int
    756 needcompat(void)
    757 {
    758 
    759 	/*LINTED*/
    760 	return __NetBSD_Version__ < VERS_TIMECHANGE
    761 	    && rump_getversion() >= VERS_TIMECHANGE;
    762 }
    763 
    764 #define DOCOMPAT(va, va_compat)						\
    765 do {									\
    766 	if (needcompat()) {						\
    767 		va_compat = rump_pub_vattr_init();			\
    768 		rump_pub_vattr50_to_vattr(va, va_compat);		\
    769 	} else {							\
    770 		va_compat = __UNCONST(va);				\
    771 	}								\
    772 } while (0)
    773 
    774 #define UNDOCOMPAT(va_compat)						\
    775 do {									\
    776 	if (needcompat())						\
    777 		rump_pub_vattr_free(va_compat);				\
    778 } while (0)
    779 
    780 static int
    781 do_makenode(struct puffs_usermount *pu, struct p2k_node *p2n_dir,
    782 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
    783 	const struct vattr *vap, char *link_target,
    784 	int (*makefn)(struct vnode *, struct vnode **, struct componentname *,
    785 		      struct vattr *),
    786 	int (*symfn)(struct vnode *, struct vnode **, struct componentname *,
    787 		      struct vattr *, char *))
    788 {
    789 	struct p2k_mount *p2m = puffs_getspecific(pu);
    790 	struct vnode *dvp = p2n_dir->p2n_vp;
    791 	struct p2k_node *p2n;
    792 	struct componentname *cn;
    793 	struct vattr *va_x;
    794 	struct vnode *vp = NULL;
    795 	int rv;
    796 
    797 	p2n = malloc(sizeof(*p2n));
    798 	if (p2n == NULL)
    799 		return ENOMEM;
    800 	DOCOMPAT(vap, va_x);
    801 
    802 	cn = makecn(pcn);
    803 	RUMP_VOP_LOCK(dvp, LK_EXCLUSIVE);
    804 	rump_pub_vp_incref(dvp);
    805 	if (makefn) {
    806 		rv = makefn(dvp, &vp, cn, va_x);
    807 	} else {
    808 		rv = symfn(dvp, &vp, cn, va_x, link_target);
    809 	}
    810 	rump_pub_vp_rele(dvp);
    811 	RUMP_VOP_UNLOCK(dvp);
    812 	freecn(cn);
    813 
    814 	if (rv == 0) {
    815 		p2n = getp2n(p2m, vp, true, p2n);
    816 		puffs_newinfo_setcookie(pni, p2n);
    817 	} else {
    818 		free(p2n);
    819 	}
    820 
    821 	UNDOCOMPAT(va_x);
    822 
    823 	return rv;
    824 
    825 }
    826 
    827 /*ARGSUSED*/
    828 int
    829 p2k_node_create(struct puffs_usermount *pu, puffs_cookie_t opc,
    830 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
    831 	const struct vattr *vap)
    832 {
    833 
    834 	return do_makenode(pu, opc, pni, pcn, vap, NULL, RUMP_VOP_CREATE, NULL);
    835 }
    836 
    837 /*ARGSUSED*/
    838 int
    839 p2k_node_mknod(struct puffs_usermount *pu, puffs_cookie_t opc,
    840 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
    841 	const struct vattr *vap)
    842 {
    843 
    844 	return do_makenode(pu, opc, pni, pcn, vap, NULL, RUMP_VOP_MKNOD, NULL);
    845 }
    846 
    847 /*ARGSUSED*/
    848 int
    849 p2k_node_open(struct puffs_usermount *pu, puffs_cookie_t opc, int mode,
    850 	const struct puffs_cred *pcr)
    851 {
    852 	struct vnode *vp = OPC2VP(opc);
    853 	struct kauth_cred *cred;
    854 	int rv;
    855 
    856 	cred = cred_create(pcr);
    857 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
    858 	rv = RUMP_VOP_OPEN(vp, mode, cred);
    859 	RUMP_VOP_UNLOCK(vp);
    860 	cred_destroy(cred);
    861 
    862 	return rv;
    863 }
    864 
    865 /*ARGSUSED*/
    866 int
    867 p2k_node_close(struct puffs_usermount *pu, puffs_cookie_t opc, int flags,
    868 	const struct puffs_cred *pcr)
    869 {
    870 	struct vnode *vp = OPC2VP(opc);
    871 	struct kauth_cred *cred;
    872 
    873 	cred = cred_create(pcr);
    874 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
    875 	RUMP_VOP_CLOSE(vp, flags, cred);
    876 	RUMP_VOP_UNLOCK(vp);
    877 	cred_destroy(cred);
    878 
    879 	return 0;
    880 }
    881 
    882 /*ARGSUSED*/
    883 int
    884 p2k_node_access(struct puffs_usermount *pu, puffs_cookie_t opc, int mode,
    885 	const struct puffs_cred *pcr)
    886 {
    887 	struct vnode *vp = OPC2VP(opc);
    888 	struct kauth_cred *cred;
    889 	int rv;
    890 
    891 	cred = cred_create(pcr);
    892 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
    893 	rv = RUMP_VOP_ACCESS(vp, mode, cred);
    894 	RUMP_VOP_UNLOCK(vp);
    895 	cred_destroy(cred);
    896 
    897 	return rv;
    898 }
    899 
    900 /*ARGSUSED*/
    901 int
    902 p2k_node_getattr(struct puffs_usermount *pu, puffs_cookie_t opc,
    903 	struct vattr *vap, const struct puffs_cred *pcr)
    904 {
    905 	struct vnode *vp = OPC2VP(opc);
    906 	struct kauth_cred *cred;
    907 	struct vattr *va_x;
    908 	int rv;
    909 
    910 	/* "deadfs" */
    911 	if (!vp)
    912 		return 0;
    913 
    914 	if (needcompat()) {
    915 		va_x = rump_pub_vattr_init();
    916 	} else {
    917 		va_x = vap;
    918 	}
    919 
    920 	cred = cred_create(pcr);
    921 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
    922 	rv = RUMP_VOP_GETATTR(vp, va_x, cred);
    923 	RUMP_VOP_UNLOCK(vp);
    924 	cred_destroy(cred);
    925 
    926 	if (needcompat()) {
    927 		rump_pub_vattr_to_vattr50(va_x, vap);
    928 		rump_pub_vattr_free(va_x);
    929 	}
    930 
    931 	return rv;
    932 }
    933 
    934 /*ARGSUSED*/
    935 int
    936 p2k_node_setattr(struct puffs_usermount *pu, puffs_cookie_t opc,
    937 	const struct vattr *vap, const struct puffs_cred *pcr)
    938 {
    939 	struct vnode *vp = OPC2VP(opc);
    940 	struct kauth_cred *cred;
    941 	struct vattr *va_x;
    942 	int rv;
    943 
    944 	/* "deadfs" */
    945 	if (!vp)
    946 		return 0;
    947 
    948 	DOCOMPAT(vap, va_x);
    949 
    950 	cred = cred_create(pcr);
    951 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
    952 	rv = RUMP_VOP_SETATTR(vp, va_x, cred);
    953 	RUMP_VOP_UNLOCK(vp);
    954 	cred_destroy(cred);
    955 
    956 	UNDOCOMPAT(va_x);
    957 
    958 	return rv;
    959 }
    960 
    961 /*ARGSUSED*/
    962 int
    963 p2k_node_fsync(struct puffs_usermount *pu, puffs_cookie_t opc,
    964 	const struct puffs_cred *pcr, int flags, off_t offlo, off_t offhi)
    965 {
    966 	struct vnode *vp = OPC2VP(opc);
    967 	struct kauth_cred *cred;
    968 	int rv;
    969 
    970 	/* "deadfs" */
    971 	if (!vp)
    972 		return 0;
    973 
    974 	cred = cred_create(pcr);
    975 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
    976 	rv = RUMP_VOP_FSYNC(vp, cred, flags, offlo, offhi);
    977 	RUMP_VOP_UNLOCK(vp);
    978 	cred_destroy(cred);
    979 
    980 	return rv;
    981 }
    982 
    983 /*ARGSUSED*/
    984 int
    985 p2k_node_mmap(struct puffs_usermount *pu, puffs_cookie_t opc, vm_prot_t flags,
    986 	const struct puffs_cred *pcr)
    987 {
    988 	struct kauth_cred *cred;
    989 	int rv;
    990 
    991 	cred = cred_create(pcr);
    992 	rv = RUMP_VOP_MMAP(OPC2VP(opc), flags, cred);
    993 	cred_destroy(cred);
    994 
    995 	return rv;
    996 }
    997 
    998 /*ARGSUSED*/
    999 int
   1000 p2k_node_seek(struct puffs_usermount *pu, puffs_cookie_t opc,
   1001 	off_t oldoff, off_t newoff, const struct puffs_cred *pcr)
   1002 {
   1003 	struct vnode *vp = OPC2VP(opc);
   1004 	struct kauth_cred *cred;
   1005 	int rv;
   1006 
   1007 	cred = cred_create(pcr);
   1008 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
   1009 	rv = RUMP_VOP_SEEK(vp, oldoff, newoff, cred);
   1010 	RUMP_VOP_UNLOCK(vp);
   1011 	cred_destroy(cred);
   1012 
   1013 	return rv;
   1014 }
   1015 
   1016 static int
   1017 do_nukenode(struct p2k_node *p2n_dir, struct p2k_node *p2n,
   1018 	const struct puffs_cn *pcn,
   1019 	int (*nukefn)(struct vnode *, struct vnode *, struct componentname *))
   1020 {
   1021 	struct vnode *dvp = p2n_dir->p2n_vp, *vp = p2n->p2n_vp;
   1022 	struct componentname *cn;
   1023 	int rv;
   1024 
   1025 	cn = makecn(pcn);
   1026 	RUMP_VOP_LOCK(dvp, LK_EXCLUSIVE);
   1027 	rump_pub_vp_incref(dvp);
   1028 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
   1029 	rump_pub_vp_incref(vp);
   1030 	rv = nukefn(dvp, vp, cn);
   1031 	assert(dvp != vp);
   1032 	assert(RUMP_VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
   1033 	assert(RUMP_VOP_ISLOCKED(vp) == 0);
   1034 	rump_pub_vp_rele(dvp);
   1035 	RUMP_VOP_UNLOCK(dvp);
   1036 	freecn(cn);
   1037 
   1038 	return rv;
   1039 
   1040 }
   1041 
   1042 /*ARGSUSED*/
   1043 int
   1044 p2k_node_remove(struct puffs_usermount *pu, puffs_cookie_t opc,
   1045 	puffs_cookie_t targ, const struct puffs_cn *pcn)
   1046 {
   1047 
   1048 	return do_nukenode(opc, targ, pcn, RUMP_VOP_REMOVE);
   1049 }
   1050 
   1051 /*ARGSUSED*/
   1052 int
   1053 p2k_node_link(struct puffs_usermount *pu, puffs_cookie_t opc,
   1054 	puffs_cookie_t targ, const struct puffs_cn *pcn)
   1055 {
   1056 	struct vnode *dvp = OPC2VP(opc);
   1057 	struct componentname *cn;
   1058 	int rv;
   1059 
   1060 	cn = makecn(pcn);
   1061 	RUMP_VOP_LOCK(dvp, LK_EXCLUSIVE);
   1062 	rump_pub_vp_incref(dvp);
   1063 	rv = RUMP_VOP_LINK(dvp, OPC2VP(targ), cn);
   1064 	rump_pub_vp_rele(dvp);
   1065 	RUMP_VOP_UNLOCK(dvp);
   1066 	freecn(cn);
   1067 
   1068 	return rv;
   1069 }
   1070 
   1071 /*ARGSUSED*/
   1072 int
   1073 p2k_node_rename(struct puffs_usermount *pu,
   1074 	puffs_cookie_t src_dir, puffs_cookie_t src,
   1075 	const struct puffs_cn *pcn_src,
   1076 	puffs_cookie_t targ_dir, puffs_cookie_t targ,
   1077 	const struct puffs_cn *pcn_targ)
   1078 {
   1079 	struct vnode *dvp, *vp, *tdvp, *tvp = NULL;
   1080 	struct componentname *cn_src, *cn_targ;
   1081 	int rv;
   1082 
   1083 	cn_src = makecn(pcn_src);
   1084 	cn_targ = makecn(pcn_targ);
   1085 
   1086 	dvp = OPC2VP(src_dir);
   1087 	vp = OPC2VP(src);
   1088 	tdvp = OPC2VP(targ_dir);
   1089 	if (targ) {
   1090 		tvp = OPC2VP(targ);
   1091 	}
   1092 
   1093 	rump_pub_vp_incref(dvp);
   1094 	rump_pub_vp_incref(vp);
   1095 	RUMP_VOP_LOCK(tdvp, LK_EXCLUSIVE);
   1096 	rump_pub_vp_incref(tdvp);
   1097 	if (tvp) {
   1098 		RUMP_VOP_LOCK(tvp, LK_EXCLUSIVE);
   1099 		rump_pub_vp_incref(tvp);
   1100 	}
   1101 	rv = RUMP_VOP_RENAME(dvp, vp, cn_src, tdvp, tvp, cn_targ);
   1102 	assert(RUMP_VOP_ISLOCKED(tdvp) == 0);
   1103 	if (tvp) {
   1104 		assert(RUMP_VOP_ISLOCKED(tvp) == 0);
   1105 	}
   1106 	freecn(cn_src);
   1107 	freecn(cn_targ);
   1108 
   1109 	return rv;
   1110 }
   1111 
   1112 /*ARGSUSED*/
   1113 int
   1114 p2k_node_mkdir(struct puffs_usermount *pu, puffs_cookie_t opc,
   1115 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
   1116 	const struct vattr *vap)
   1117 {
   1118 
   1119 	return do_makenode(pu, opc, pni, pcn, vap, NULL, RUMP_VOP_MKDIR, NULL);
   1120 }
   1121 
   1122 /*ARGSUSED*/
   1123 int
   1124 p2k_node_rmdir(struct puffs_usermount *pu, puffs_cookie_t opc,
   1125 	puffs_cookie_t targ, const struct puffs_cn *pcn)
   1126 {
   1127 
   1128 	return do_nukenode(opc, targ, pcn, RUMP_VOP_RMDIR);
   1129 }
   1130 
   1131 /*ARGSUSED*/
   1132 int
   1133 p2k_node_symlink(struct puffs_usermount *pu, puffs_cookie_t opc,
   1134 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
   1135 	const struct vattr *vap, const char *link_target)
   1136 {
   1137 
   1138 	return do_makenode(pu, opc, pni, pcn, vap,
   1139 	    __UNCONST(link_target), NULL, RUMP_VOP_SYMLINK);
   1140 }
   1141 
   1142 /*ARGSUSED*/
   1143 int
   1144 p2k_node_readdir(struct puffs_usermount *pu, puffs_cookie_t opc,
   1145 	struct dirent *dent, off_t *readoff, size_t *reslen,
   1146 	const struct puffs_cred *pcr, int *eofflag,
   1147 	off_t *cookies, size_t *ncookies)
   1148 {
   1149 	struct vnode *vp = OPC2VP(opc);
   1150 	struct kauth_cred *cred;
   1151 	struct uio *uio;
   1152 	off_t *vop_cookies;
   1153 	int vop_ncookies;
   1154 	int rv;
   1155 
   1156 	cred = cred_create(pcr);
   1157 	uio = rump_pub_uio_setup(dent, *reslen, *readoff, RUMPUIO_READ);
   1158 	RUMP_VOP_LOCK(vp, LK_SHARED);
   1159 	if (cookies) {
   1160 		rv = RUMP_VOP_READDIR(vp, uio, cred, eofflag,
   1161 		    &vop_cookies, &vop_ncookies);
   1162 		memcpy(cookies, vop_cookies, vop_ncookies * sizeof(*cookies));
   1163 		*ncookies = vop_ncookies;
   1164 		free(vop_cookies);
   1165 	} else {
   1166 		rv = RUMP_VOP_READDIR(vp, uio, cred, eofflag, NULL, NULL);
   1167 	}
   1168 	RUMP_VOP_UNLOCK(vp);
   1169 	if (rv == 0) {
   1170 		*reslen = rump_pub_uio_getresid(uio);
   1171 		*readoff = rump_pub_uio_getoff(uio);
   1172 	}
   1173 	rump_pub_uio_free(uio);
   1174 	cred_destroy(cred);
   1175 
   1176 	return rv;
   1177 }
   1178 
   1179 /*ARGSUSED*/
   1180 int
   1181 p2k_node_readlink(struct puffs_usermount *pu, puffs_cookie_t opc,
   1182 	const struct puffs_cred *pcr, char *linkname, size_t *linklen)
   1183 {
   1184 	struct vnode *vp = OPC2VP(opc);
   1185 	struct kauth_cred *cred;
   1186 	struct uio *uio;
   1187 	int rv;
   1188 
   1189 	cred = cred_create(pcr);
   1190 	uio = rump_pub_uio_setup(linkname, *linklen, 0, RUMPUIO_READ);
   1191 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
   1192 	rv = RUMP_VOP_READLINK(vp, uio, cred);
   1193 	RUMP_VOP_UNLOCK(vp);
   1194 	*linklen -= rump_pub_uio_free(uio);
   1195 	cred_destroy(cred);
   1196 
   1197 	return rv;
   1198 }
   1199 
   1200 /*ARGSUSED*/
   1201 int
   1202 p2k_node_read(struct puffs_usermount *pu, puffs_cookie_t opc,
   1203 	uint8_t *buf, off_t offset, size_t *resid,
   1204 	const struct puffs_cred *pcr, int ioflag)
   1205 {
   1206 	struct vnode *vp = OPC2VP(opc);
   1207 	struct kauth_cred *cred;
   1208 	struct uio *uio;
   1209 	int rv;
   1210 
   1211 	cred = cred_create(pcr);
   1212 	uio = rump_pub_uio_setup(buf, *resid, offset, RUMPUIO_READ);
   1213 	RUMP_VOP_LOCK(vp, LK_SHARED);
   1214 	rv = RUMP_VOP_READ(vp, uio, ioflag, cred);
   1215 	RUMP_VOP_UNLOCK(vp);
   1216 	*resid = rump_pub_uio_free(uio);
   1217 	cred_destroy(cred);
   1218 
   1219 	return rv;
   1220 }
   1221 
   1222 /*ARGSUSED*/
   1223 int
   1224 p2k_node_write(struct puffs_usermount *pu, puffs_cookie_t opc,
   1225 	uint8_t *buf, off_t offset, size_t *resid,
   1226 	const struct puffs_cred *pcr, int ioflag)
   1227 {
   1228 	struct vnode *vp = OPC2VP(opc);
   1229 	struct kauth_cred *cred;
   1230 	struct uio *uio;
   1231 	int rv;
   1232 
   1233 	/* "deadfs" */
   1234 	if (!vp)
   1235 		return 0;
   1236 
   1237 	cred = cred_create(pcr);
   1238 	uio = rump_pub_uio_setup(buf, *resid, offset, RUMPUIO_WRITE);
   1239 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
   1240 	rv = RUMP_VOP_WRITE(vp, uio, ioflag, cred);
   1241 	RUMP_VOP_UNLOCK(vp);
   1242 	*resid = rump_pub_uio_free(uio);
   1243 	cred_destroy(cred);
   1244 
   1245 	return rv;
   1246 }
   1247 
   1248 /*ARGSUSED*/
   1249 int
   1250 p2k_node_pathconf(struct puffs_usermount *pu, puffs_cookie_t opc,
   1251 	int name, register_t *retval)
   1252 {
   1253 	struct vnode *vp = OPC2VP(opc);
   1254 	int rv;
   1255 
   1256 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
   1257 	rv = RUMP_VOP_PATHCONF(vp, name, retval);
   1258 	RUMP_VOP_UNLOCK(vp);
   1259 
   1260 	return rv;
   1261 }
   1262 
   1263 /*ARGSUSED*/
   1264 int
   1265 p2k_node_getextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
   1266 	int attrnamespace, const char *attrname, size_t *attrsize,
   1267 	uint8_t *attr, size_t *resid, const struct puffs_cred *pcr)
   1268 {
   1269 	struct vnode *vp = OPC2VP(opc);
   1270 	struct kauth_cred *cred;
   1271 	struct uio *uio;
   1272 	int rv;
   1273 
   1274 	if (attr)
   1275 		uio = rump_pub_uio_setup(attr, *resid, 0, RUMPUIO_READ);
   1276 	else
   1277 		uio = NULL;
   1278 
   1279 	cred = cred_create(pcr);
   1280 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
   1281 	rv = RUMP_VOP_GETEXTATTR(vp, attrnamespace, attrname, uio,
   1282 	    attrsize, cred);
   1283 	RUMP_VOP_UNLOCK(vp);
   1284 	cred_destroy(cred);
   1285 
   1286 	if (uio)
   1287 		*resid = rump_pub_uio_free(uio);
   1288 
   1289 	return rv;
   1290 }
   1291 
   1292 /*ARGSUSED*/
   1293 int
   1294 p2k_node_setextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
   1295 	int attrnamespace, const char *attrname,
   1296 	uint8_t *attr, size_t *resid, const struct puffs_cred *pcr)
   1297 {
   1298 	struct vnode *vp = OPC2VP(opc);
   1299 	struct kauth_cred *cred;
   1300 	struct uio *uio;
   1301 	int rv;
   1302 
   1303 	if (attr)
   1304 		uio = rump_pub_uio_setup(attr, *resid, 0, RUMPUIO_READ);
   1305 	else
   1306 		uio = NULL;
   1307 
   1308 	cred = cred_create(pcr);
   1309 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
   1310 	rv = RUMP_VOP_SETEXTATTR(vp, attrnamespace, attrname, uio, cred);
   1311 	RUMP_VOP_UNLOCK(vp);
   1312 	cred_destroy(cred);
   1313 
   1314 	if (uio)
   1315 		*resid = rump_pub_uio_free(uio);
   1316 
   1317 	return rv;
   1318 }
   1319 
   1320 /*ARGSUSED*/
   1321 int
   1322 p2k_node_listextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
   1323 	int attrnamespace, size_t *attrsize, uint8_t *attrs,
   1324 	size_t *resid, int flags, const struct puffs_cred *pcr)
   1325 {
   1326 	struct vnode *vp = OPC2VP(opc);
   1327 	struct kauth_cred *cred;
   1328 	struct uio *uio;
   1329 	int rv;
   1330 
   1331 	if (attrs)
   1332 		uio = rump_pub_uio_setup(attrs, *resid, 0, RUMPUIO_READ);
   1333 	else
   1334 		uio = NULL;
   1335 
   1336 	cred = cred_create(pcr);
   1337 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
   1338 	rv = RUMP_VOP_LISTEXTATTR(vp, attrnamespace, uio, attrsize,
   1339 	    flags, cred);
   1340 	RUMP_VOP_UNLOCK(vp);
   1341 	cred_destroy(cred);
   1342 
   1343 	if (uio)
   1344 		*resid = rump_pub_uio_free(uio);
   1345 
   1346 	return rv;
   1347 }
   1348 
   1349 /*ARGSUSED*/
   1350 int
   1351 p2k_node_deleteextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
   1352 	int attrnamespace, const char *attrname, const struct puffs_cred *pcr)
   1353 {
   1354 	struct vnode *vp = OPC2VP(opc);
   1355 	struct kauth_cred *cred;
   1356 	int rv;
   1357 
   1358 	cred = cred_create(pcr);
   1359 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
   1360 	rv = RUMP_VOP_DELETEEXTATTR(vp, attrnamespace, attrname, cred);
   1361 	RUMP_VOP_UNLOCK(vp);
   1362 	cred_destroy(cred);
   1363 
   1364 	return rv;
   1365 }
   1366 
   1367 /* the kernel releases its last reference here */
   1368 int
   1369 p2k_node_inactive(struct puffs_usermount *pu, puffs_cookie_t opc)
   1370 {
   1371 	struct p2k_mount *p2m = puffs_getspecific(pu);
   1372 	struct p2k_node *p2n = opc;
   1373 	struct vnode *vp = OPC2VP(opc);
   1374 	bool recycle = false;
   1375 	int rv;
   1376 
   1377 	/* deadfs */
   1378 	if (!vp)
   1379 		return 0;
   1380 
   1381 	/*
   1382 	 * Flush all cached vnode pages from the rump kernel -- they
   1383 	 * are kept in puffs for all things that matter.  However,
   1384 	 * don't do this for tmpfs (vnodes are backed by an aobj), since that
   1385 	 * would cause us to clear the backing storage leaving us without
   1386 	 * a way to regain the data from "stable storage".
   1387 	 */
   1388 	if (!p2m->p2m_imtmpfsman) {
   1389 		rump_pub_vp_vmobjlock(vp, 1);
   1390 		RUMP_VOP_PUTPAGES(vp, 0, 0,
   1391 		    PGO_ALLPAGES|PGO_CLEANIT|PGO_FREE);
   1392 	}
   1393 
   1394 	/*
   1395 	 * Ok, this is where we get nasty.  We pretend the vnode is
   1396 	 * inactive and already tell the file system that.  However,
   1397 	 * we are allowed to pretend it also grows a reference immediately
   1398 	 * after per vget(), so this does not do harm.  Cheap trick, but ...
   1399 	 *
   1400 	 * If the file system thinks the inode is done for, we release
   1401 	 * our reference and clear all knowledge of the vnode.  If,
   1402 	 * however, the inode is still active, we retain our reference
   1403 	 * until reclaim, since puffs might be flushing out some data
   1404 	 * later.
   1405 	 */
   1406 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
   1407 	rv = RUMP_VOP_INACTIVE(vp, &recycle);
   1408 	RUMP_VOP_UNLOCK(vp);
   1409 	if (recycle) {
   1410 		puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N1);
   1411 		rump_pub_vp_rele(p2n->p2n_vp);
   1412 		p2n->p2n_vp = NULL;
   1413 	}
   1414 
   1415 	return rv;
   1416 }
   1417 
   1418 /*ARGSUSED*/
   1419 int
   1420 p2k_node_reclaim(struct puffs_usermount *pu, puffs_croissant_t opc)
   1421 {
   1422 	struct p2k_node *p2n = opc;
   1423 
   1424 	if (p2n->p2n_vp) {
   1425 		rump_pub_vp_rele(p2n->p2n_vp);
   1426 		p2n->p2n_vp = NULL;
   1427 	}
   1428 
   1429 	freep2n(p2n);
   1430 	return 0;
   1431 }
   1432