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