xref: /netbsd-src/lib/libp2k/p2k.c (revision 179b12252ecaf3553d9c2b7458ce62b6a2203d0c)
1 /*	$NetBSD: p2k.c,v 1.41 2010/06/24 13:03:05 hannken 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 	PUFFSOP_SET(pops, p2k, fs, extattrctl);
287 
288 	PUFFSOP_SET(pops, p2k, node, lookup);
289 	PUFFSOP_SET(pops, p2k, node, create);
290 	PUFFSOP_SET(pops, p2k, node, mknod);
291 	PUFFSOP_SET(pops, p2k, node, open);
292 	PUFFSOP_SET(pops, p2k, node, close);
293 	PUFFSOP_SET(pops, p2k, node, access);
294 	PUFFSOP_SET(pops, p2k, node, getattr);
295 	PUFFSOP_SET(pops, p2k, node, setattr);
296 #if 0
297 	PUFFSOP_SET(pops, p2k, node, poll);
298 #endif
299 	PUFFSOP_SET(pops, p2k, node, mmap);
300 	PUFFSOP_SET(pops, p2k, node, fsync);
301 	PUFFSOP_SET(pops, p2k, node, seek);
302 	PUFFSOP_SET(pops, p2k, node, remove);
303 	PUFFSOP_SET(pops, p2k, node, link);
304 	PUFFSOP_SET(pops, p2k, node, rename);
305 	PUFFSOP_SET(pops, p2k, node, mkdir);
306 	PUFFSOP_SET(pops, p2k, node, rmdir);
307 	PUFFSOP_SET(pops, p2k, node, symlink);
308 	PUFFSOP_SET(pops, p2k, node, readdir);
309 	PUFFSOP_SET(pops, p2k, node, readlink);
310 	PUFFSOP_SET(pops, p2k, node, read);
311 	PUFFSOP_SET(pops, p2k, node, write);
312 
313 	PUFFSOP_SET(pops, p2k, node, pathconf);
314 
315 	PUFFSOP_SET(pops, p2k, node, inactive);
316 	PUFFSOP_SET(pops, p2k, node, reclaim);
317 	PUFFSOP_SET(pops, p2k, node, abortop);
318 
319 	PUFFSOP_SET(pops, p2k, node, getextattr);
320 	PUFFSOP_SET(pops, p2k, node, setextattr);
321 	PUFFSOP_SET(pops, p2k, node, listextattr);
322 	PUFFSOP_SET(pops, p2k, node, deleteextattr);
323 
324 	dodaemon = true;
325 	hasdebug = false;
326 
327 	if (getenv("P2K_DEBUG") != NULL) {
328 		puffs_flags |= PUFFS_FLAG_OPDUMP;
329 		dodaemon = false;
330 		hasdebug = true;
331 	}
332 	if (getenv("P2K_NODETACH") != NULL) {
333 		dodaemon = false;
334 	}
335 	if (getenv("P2K_NOCACHE_PAGE") != NULL) {
336 		puffs_flags |= PUFFS_KFLAG_NOCACHE_PAGE;
337 	}
338 	if (getenv("P2K_NOCACHE_NAME") != NULL) {
339 		puffs_flags |= PUFFS_KFLAG_NOCACHE_NAME;
340 	}
341 	if (getenv("P2K_NOCACHE") != NULL) {
342 		puffs_flags |= PUFFS_KFLAG_NOCACHE;
343 	}
344 	if ((envbuf = getenv("P2K_WIZARDUID")) != NULL) {
345 		/* default to 0 in error cases */
346 		wizarduid = atoi(envbuf);
347 		haswizard = 1;
348 		printf("P2K WIZARD MODE: using uid %d\n", wizarduid);
349 	}
350 
351 	p2m = allocp2m();
352 	if (p2m == NULL)
353 		return NULL;
354 	p2m->p2m_pu = puffs_init(pops, PUFFS_DEFER, PUFFS_DEFER,
355 	    PUFFS_DEFER, puffs_flags);
356 	if (p2m->p2m_pu == NULL) {
357 		int sverrno = errno;
358 		free(p2m);
359 		errno = sverrno;
360 		return NULL;
361 	}
362 	p2m->p2m_hasdebug = hasdebug;
363 
364 	if (dodaemon) {
365 		if (puffs_daemon(p2m->p2m_pu, 1, 1) == -1) {
366 			int sverrno = errno;
367 			p2k_cancel(p2m, sverrno);
368 			errno = sverrno;
369 			p2m = NULL;
370 		}
371 	}
372 	if (p2m)
373 		rump_init();
374 
375 	return p2m;
376 }
377 
378 void
379 p2k_cancel(struct p2k_mount *p2m, int error)
380 {
381 
382 	puffs_cancel(p2m->p2m_pu, error);
383 	free(p2m);
384 }
385 
386 static int
387 setupfs(struct p2k_mount *p2m, const char *vfsname, const char *devpath,
388 	struct ukfs_part *part, const char *mountpath, int mntflags,
389 	void *arg, size_t alen)
390 {
391 	char partpath[UKFS_DEVICE_MAXPATHLEN];
392 	char partbuf[UKFS_DEVICE_MAXSTR];
393 	char typebuf[PUFFS_TYPELEN];
394 	struct puffs_usermount *pu = p2m->p2m_pu;
395 	struct p2k_node *p2n_root;
396 	struct ukfs *ukfs = NULL;
397 	extern int puffs_fakecc;
398 	int rv = -1, sverrno;
399 
400 	strcpy(typebuf, "p2k|");
401 	if (strcmp(vfsname, "puffs") == 0) { /* XXX */
402 		struct puffs_kargs *args = arg;
403 		strlcat(typebuf, args->pa_typename, sizeof(typebuf));
404 	} else {
405 		strlcat(typebuf, vfsname, sizeof(typebuf));
406 	}
407 
408 	strlcpy(partpath, devpath, sizeof(partpath));
409 	if (ukfs_part_tostring(part, partbuf, sizeof(partbuf))) {
410 		strlcat(partpath, partbuf, sizeof(partpath));
411 	}
412 	puffs_setmntinfo(pu, partpath, typebuf);
413 
414 	if (ukfs_init() == -1)
415 		goto out;
416 
417 	/*
418 	 * If we're mounting rumpfs, actually do no mount and redirect
419 	 * requests to rump fs namespace root.  Strictly speaking, this
420 	 * is not correct, but considering rumpfs doesn't currently
421 	 * support VFS_MOUNT(), I don't think anyone will notice.
422 	 */
423 	if (strcmp(vfsname, MOUNT_RUMPFS) == 0) {
424 		if ((rv = rump_pub_vfs_getmp("/", &p2m->p2m_mp)) != 0) {
425 			errno = rv;
426 			rv = -1;
427 			goto out;
428 		}
429 		if ((rv = rump_pub_vfs_root(p2m->p2m_mp,
430 		    &p2m->p2m_rvp, 0)) != 0) {
431 			errno = rv;
432 			rv = -1;
433 			goto out;
434 		}
435 	} else {
436 		if (part != ukfs_part_na)
437 			ukfs = ukfs_mount_disk(vfsname, devpath, part,
438 			    mountpath, mntflags, arg, alen);
439 		else
440 			ukfs = ukfs_mount(vfsname, devpath, mountpath, mntflags,
441 			    arg, alen);
442 		if (ukfs == NULL)
443 			goto out;
444 		ukfs_setspecific(ukfs, p2m);
445 		p2m->p2m_ukfs = ukfs;
446 		p2m->p2m_mp = ukfs_getmp(ukfs);
447 		p2m->p2m_rvp = ukfs_getrvp(ukfs);
448 	}
449 
450 	p2m->p2m_pu = pu;
451 
452 	/*
453 	 * Detect tmpfs.  XXX: this is a kludge.  See inactive().
454 	 *
455 	 * In reality we'd want "does file system use anon objects
456 	 * for storage?".  But since tmpfs hides the anon object from
457 	 * the public interface, we can't actually detect it sanely.
458 	 * Therefore, use this kludge.
459 	 */
460 	p2m->p2m_imtmpfsman = strcmp(vfsname, MOUNT_TMPFS) == 0;
461 
462 	p2n_root = getp2n(p2m, p2m->p2m_rvp, true, NULL);
463 	puffs_setfhsize(pu, 0, PUFFS_FHFLAG_PASSTHROUGH);
464 	puffs_setstacksize(pu, PUFFS_STACKSIZE_MIN);
465 	puffs_fakecc = 1;
466 	puffs_set_prepost(pu, makelwp, clearlwp);
467 	puffs_set_errnotify(pu, p2k_errcatcher);
468 
469 	puffs_setspecific(pu, p2m);
470 	rv = puffs_mount(pu, mountpath, mntflags, p2n_root);
471 
472  out:
473 	if (rv == -1) {
474 		sverrno = errno;
475 		puffs_cancel(pu, sverrno);
476 		if (ukfs)
477 			ukfs_release(p2m->p2m_ukfs, UKFS_RELFLAG_FORCE);
478 		free(p2m);
479 		errno = sverrno;
480 	}
481 
482 	return rv;
483 }
484 
485 int
486 p2k_mainloop(struct p2k_mount *p2m)
487 {
488 	int rv, sverrno;
489 
490 	rv = puffs_mainloop(p2m->p2m_pu);
491 	sverrno = errno;
492 	puffs_exit(p2m->p2m_pu, 1);
493 	if (p2m->p2m_ukfs)
494 		ukfs_release(p2m->p2m_ukfs, UKFS_RELFLAG_FORCE);
495 	free(p2m);
496 
497 	if (rv == -1)
498 		errno = sverrno;
499 	return rv;
500 }
501 
502 int
503 p2k_run_fs(const char *vfsname, const char *devpath, const char *mountpath,
504 	int mntflags, void *arg, size_t alen, uint32_t puffs_flags)
505 {
506 	struct p2k_mount *p2m;
507 	int rv;
508 
509 	p2m = p2k_init(puffs_flags);
510 	if (p2m == NULL)
511 		return -1;
512 	rv = setupfs(p2m, vfsname, devpath, ukfs_part_na, mountpath,
513 	    mntflags, arg, alen);
514 	if (rv == -1)
515 		return rv;
516 	return p2k_mainloop(p2m);
517 }
518 
519 int
520 p2k_run_diskfs(const char *vfsname, const char *devpath, struct ukfs_part *part,
521 	const char *mountpath, int mntflags, void *arg, size_t alen,
522 	uint32_t puffs_flags)
523 {
524 	struct p2k_mount *p2m;
525 	int rv;
526 
527 	p2m = p2k_init(puffs_flags);
528 	if (p2m == NULL)
529 		return -1;
530 	rv = setupfs(p2m, vfsname, devpath, part, mountpath, mntflags,
531 	    arg, alen);
532 	if (rv == -1)
533 		return rv;
534 	return p2k_mainloop(p2m);
535 }
536 
537 int
538 p2k_setup_fs(struct p2k_mount *p2m, const char *vfsname, const char *devpath,
539 	const char *mountpath, int mntflags, void *arg, size_t alen)
540 {
541 
542 	return setupfs(p2m, vfsname, devpath, ukfs_part_na, mountpath,
543 	    mntflags, arg, alen);
544 }
545 
546 int
547 p2k_setup_diskfs(struct p2k_mount *p2m, const char *vfsname,
548 	const char *devpath, struct ukfs_part *part, const char *mountpath,
549 	int mntflags, void *arg, size_t alen)
550 {
551 
552 	return setupfs(p2m, vfsname, devpath, part, mountpath, mntflags,
553 	    arg, alen);
554 }
555 
556 int
557 p2k_fs_statvfs(struct puffs_usermount *pu, struct statvfs *sbp)
558 {
559 	struct p2k_mount *p2m = puffs_getspecific(pu);
560 	struct mount *mp = p2m->p2m_mp;
561 
562 	return rump_pub_vfs_statvfs(mp, sbp);
563 }
564 
565 /*ARGSUSED*/
566 int
567 p2k_fs_unmount(struct puffs_usermount *pu, int flags)
568 {
569 	struct p2k_mount *p2m = puffs_getspecific(pu);
570 	struct ukfs *fs = p2m->p2m_ukfs;
571 	int error = 0;
572 
573 	rump_pub_lwp_release(rump_pub_lwp_curlwp()); /* ukfs & curlwp tricks */
574 
575 	rump_pub_vp_rele(p2m->p2m_rvp);
576 	if (fs) {
577 		if (ukfs_release(fs, 0) != 0) {
578 			ukfs_release(fs, UKFS_RELFLAG_FORCE);
579 			error = 0;
580 		}
581 	}
582 	p2m->p2m_ukfs = NULL;
583 
584 	if (p2m->p2m_hasdebug) {
585 		printf("-- rump kernel event counters --\n");
586 		rump_printevcnts();
587 		printf("-- end of event counters --\n");
588 	}
589 
590 	rump_pub_lwp_alloc_and_switch(0, 0);
591 	return error;
592 }
593 
594 int
595 p2k_fs_sync(struct puffs_usermount *pu, int waitfor,
596 	const struct puffs_cred *pcr)
597 {
598 	struct p2k_mount *p2m = puffs_getspecific(pu);
599 	struct mount *mp = p2m->p2m_mp;
600 	struct kauth_cred *cred;
601 	int rv;
602 
603 	cred = cred_create(pcr);
604 	rv = rump_pub_vfs_sync(mp, waitfor, cred);
605 	cred_destroy(cred);
606 
607 	return rv;
608 }
609 
610 /*ARGSUSED*/
611 int
612 p2k_fs_fhtonode(struct puffs_usermount *pu, void *fid, size_t fidsize,
613 	struct puffs_newinfo *pni)
614 {
615 	struct p2k_mount *p2m = puffs_getspecific(pu);
616 	struct mount *mp = p2m->p2m_mp;
617 	struct p2k_node *p2n;
618 	struct vnode *vp;
619 	enum vtype vtype;
620 	voff_t vsize;
621 	uint64_t rdev; /* XXX: allows running this on NetBSD 5.0 */
622 	int rv;
623 
624 	rv = rump_pub_vfs_fhtovp(mp, fid, &vp);
625 	if (rv)
626 		return rv;
627 	RUMP_VOP_UNLOCK(vp);
628 
629 	p2n = getp2n(p2m, vp, false, NULL);
630 	if (p2n == NULL)
631 		return ENOMEM;
632 
633 	puffs_newinfo_setcookie(pni, p2n);
634 	rump_pub_getvninfo(vp, &vtype, &vsize, (void *)&rdev);
635 	puffs_newinfo_setvtype(pni, vtype);
636 	puffs_newinfo_setsize(pni, vsize);
637 	/* LINTED: yea, it'll lose accuracy, but that's life */
638 	puffs_newinfo_setrdev(pni, rdev);
639 
640 	return 0;
641 }
642 
643 /*ARGSUSED*/
644 int
645 p2k_fs_nodetofh(struct puffs_usermount *pu, puffs_cookie_t cookie, void *fid,
646 	size_t *fidsize)
647 {
648 	struct vnode *vp = cookie;
649 
650 	return rump_pub_vfs_vptofh(vp, fid, fidsize);
651 }
652 
653 int
654 p2k_fs_extattrctl(struct puffs_usermount *pu, int cmd,
655 	puffs_cookie_t cookie, int flags,
656 	int attrnamespace, const char *attrname)
657 {
658 	struct p2k_mount *p2m = puffs_getspecific(pu);
659 	struct mount *mp = p2m->p2m_mp;
660 	struct vnode *vp;
661 
662 	if (flags & PUFFS_EXTATTRCTL_HASNODE) {
663 		vp = OPC2VP(cookie);
664 		RUMP_VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);
665 	} else {
666 		vp = NULL;
667 	}
668 
669 	/* vfsop unlocks (but doesn't release) vnode, so we're done here */
670 	return rump_pub_vfs_extattrctl(mp, cmd, vp, attrnamespace, attrname);
671 }
672 
673 /*ARGSUSED*/
674 int
675 p2k_node_lookup(struct puffs_usermount *pu, puffs_cookie_t opc,
676 	struct puffs_newinfo *pni, const struct puffs_cn *pcn)
677 {
678 	struct p2k_mount *p2m = puffs_getspecific(pu);
679 	struct p2k_node *p2n_dir = opc, *p2n;
680 	struct componentname *cn;
681 	struct vnode *dvp = p2n_dir->p2n_vp, *vp;
682 	enum vtype vtype;
683 	voff_t vsize;
684 	uint64_t rdev; /* XXX: uint64_t because of stack overwrite in compat */
685 	int rv;
686 
687 	cn = makecn(pcn, 0);
688 	RUMP_VOP_LOCK(dvp, LK_EXCLUSIVE);
689 	rv = RUMP_VOP_LOOKUP(dvp, &vp, cn);
690 	RUMP_VOP_UNLOCK(dvp);
691 	if (rump_pub_checksavecn(cn)) {
692 		/*
693 		 * XXX the rename lookup protocol is currently horribly
694 		 * broken.  We get 1) DELETE with SAVESTART 2) DELETE
695 		 * without SAVESTART 3) RENAME.  Hold on to this like
696 		 * it were the absolute truth for now.  However, do
697 		 * not sprinkle asserts based on this due to abovementioned
698 		 * brokenness -- some file system drivers might not
699 		 * even issue ABORT properly, so just free resources
700 		 * on the fly and hope for the best.  PR kern/42348
701 		 */
702 		if (pcn->pcn_flags & RUMP_NAMEI_INRENAME) {
703 			if (pcn->pcn_nameiop == RUMP_NAMEI_DELETE) {
704 				/* save path from the first lookup */
705 				if (pcn->pcn_flags & RUMP_NAMEI_SAVESTART) {
706 					if (p2n_dir->p2n_cn_ren_src)
707 						freecn(p2n_dir->p2n_cn_ren_src,
708 						    RUMPCN_FORCEFREE);
709 					p2n_dir->p2n_cn_ren_src = cn;
710 				} else {
711 					freecn(cn, RUMPCN_FORCEFREE);
712 					cn = NULL;
713 				}
714 			} else {
715 				assert(pcn->pcn_nameiop == RUMP_NAMEI_RENAME);
716 				if (p2n_dir->p2n_cn_ren_targ)
717 					freecn(p2n_dir->p2n_cn_ren_targ,
718 					    RUMPCN_FORCEFREE);
719 				p2n_dir->p2n_cn_ren_targ = cn;
720 			}
721 		} else {
722 			assert(p2n_dir->p2n_cn == NULL);
723 			p2n_dir->p2n_cn = cn;
724 		}
725 	} else {
726 		freecn(cn, 0);
727 		cn = NULL;
728 	}
729 	if (rv) {
730 		if (rv == EJUSTRETURN) {
731 			rv = ENOENT;
732 		}
733 		return rv;
734 	}
735 	RUMP_VOP_UNLOCK(vp);
736 
737 	p2n = getp2n(p2m, vp, false, NULL);
738 	if (p2n == NULL) {
739 		if (pcn->pcn_flags & RUMP_NAMEI_INRENAME) {
740 			if (pcn->pcn_nameiop == RUMP_NAMEI_DELETE) {
741 				p2n_dir->p2n_cn_ren_src = NULL;
742 			} else {
743 				p2n_dir->p2n_cn_ren_targ = NULL;
744 			}
745 		} else {
746 			p2n_dir->p2n_cn = NULL;
747 		}
748 		/* XXX: what in the world should happen with SAVESTART? */
749 		RUMP_VOP_ABORTOP(dvp, cn);
750 		return ENOMEM;
751 	}
752 
753 	puffs_newinfo_setcookie(pni, p2n);
754 	rump_pub_getvninfo(vp, &vtype, &vsize, (void *)&rdev);
755 	puffs_newinfo_setvtype(pni, vtype);
756 	puffs_newinfo_setsize(pni, vsize);
757 	/* LINTED: yea, it'll lose accuracy, but that's life */
758 	puffs_newinfo_setrdev(pni, rdev);
759 
760 	return 0;
761 }
762 
763 #define VERS_TIMECHANGE 599000700
764 static int
765 needcompat(void)
766 {
767 
768 	/*LINTED*/
769 	return __NetBSD_Version__ < VERS_TIMECHANGE
770 	    && rump_pub_getversion() >= VERS_TIMECHANGE;
771 }
772 
773 #define DOCOMPAT(va, va_compat)						\
774 do {									\
775 	if (needcompat()) {						\
776 		va_compat = rump_pub_vattr_init();			\
777 		rump_pub_vattr50_to_vattr(va, va_compat);		\
778 	} else {							\
779 		va_compat = __UNCONST(va);				\
780 	}								\
781 } while (/*CONSTCOND*/0)
782 
783 #define UNDOCOMPAT(va_compat)						\
784 do {									\
785 	if (needcompat())						\
786 		rump_pub_vattr_free(va_compat);				\
787 } while (/*CONSTCOND*/0)
788 
789 static int
790 do_makenode(struct puffs_usermount *pu, struct p2k_node *p2n_dir,
791 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
792 	const struct vattr *vap, char *link_target,
793 	int (*makefn)(struct vnode *, struct vnode **, struct componentname *,
794 		      struct vattr *),
795 	int (*symfn)(struct vnode *, struct vnode **, struct componentname *,
796 		      struct vattr *, char *))
797 {
798 	struct p2k_mount *p2m = puffs_getspecific(pu);
799 	struct vnode *dvp = p2n_dir->p2n_vp;
800 	struct p2k_node *p2n;
801 	struct componentname *cn;
802 	struct vattr *va_x;
803 	struct vnode *vp;
804 	int rv;
805 
806 	p2n = malloc(sizeof(*p2n));
807 	if (p2n == NULL)
808 		return ENOMEM;
809 	DOCOMPAT(vap, va_x);
810 
811 	if (p2n_dir->p2n_cn) {
812 		cn = p2n_dir->p2n_cn;
813 		p2n_dir->p2n_cn = NULL;
814 	} else {
815 		cn = makecn(pcn, RUMP_NAMEI_HASBUF);
816 	}
817 
818 	RUMP_VOP_LOCK(dvp, LK_EXCLUSIVE);
819 	rump_pub_vp_incref(dvp);
820 	if (makefn) {
821 		rv = makefn(dvp, &vp, cn, va_x);
822 	} else {
823 		rv = symfn(dvp, &vp, cn, va_x, link_target);
824 	}
825 	assert(RUMP_VOP_ISLOCKED(dvp) == 0);
826 	freecn(cn, 0);
827 
828 	if (rv == 0) {
829 		RUMP_VOP_UNLOCK(vp);
830 		p2n = getp2n(p2m, vp, true, p2n);
831 		puffs_newinfo_setcookie(pni, p2n);
832 	} else {
833 		free(p2n);
834 	}
835 
836 	UNDOCOMPAT(va_x);
837 
838 	return rv;
839 
840 }
841 
842 /*ARGSUSED*/
843 int
844 p2k_node_create(struct puffs_usermount *pu, puffs_cookie_t opc,
845 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
846 	const struct vattr *vap)
847 {
848 
849 	return do_makenode(pu, opc, pni, pcn, vap, NULL, RUMP_VOP_CREATE, NULL);
850 }
851 
852 /*ARGSUSED*/
853 int
854 p2k_node_mknod(struct puffs_usermount *pu, puffs_cookie_t opc,
855 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
856 	const struct vattr *vap)
857 {
858 
859 	return do_makenode(pu, opc, pni, pcn, vap, NULL, RUMP_VOP_MKNOD, NULL);
860 }
861 
862 /*ARGSUSED*/
863 int
864 p2k_node_open(struct puffs_usermount *pu, puffs_cookie_t opc, int mode,
865 	const struct puffs_cred *pcr)
866 {
867 	struct vnode *vp = OPC2VP(opc);
868 	struct kauth_cred *cred;
869 	int rv;
870 
871 	cred = cred_create(pcr);
872 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
873 	rv = RUMP_VOP_OPEN(vp, mode, cred);
874 	RUMP_VOP_UNLOCK(vp);
875 	cred_destroy(cred);
876 
877 	return rv;
878 }
879 
880 /*ARGSUSED*/
881 int
882 p2k_node_close(struct puffs_usermount *pu, puffs_cookie_t opc, int flags,
883 	const struct puffs_cred *pcr)
884 {
885 	struct vnode *vp = OPC2VP(opc);
886 	struct kauth_cred *cred;
887 
888 	cred = cred_create(pcr);
889 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
890 	RUMP_VOP_CLOSE(vp, flags, cred);
891 	RUMP_VOP_UNLOCK(vp);
892 	cred_destroy(cred);
893 
894 	return 0;
895 }
896 
897 /*ARGSUSED*/
898 int
899 p2k_node_access(struct puffs_usermount *pu, puffs_cookie_t opc, int mode,
900 	const struct puffs_cred *pcr)
901 {
902 	struct vnode *vp = OPC2VP(opc);
903 	struct kauth_cred *cred;
904 	int rv;
905 
906 	cred = cred_create(pcr);
907 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
908 	rv = RUMP_VOP_ACCESS(vp, mode, cred);
909 	RUMP_VOP_UNLOCK(vp);
910 	cred_destroy(cred);
911 
912 	return rv;
913 }
914 
915 /*ARGSUSED*/
916 int
917 p2k_node_getattr(struct puffs_usermount *pu, puffs_cookie_t opc,
918 	struct vattr *vap, const struct puffs_cred *pcr)
919 {
920 	struct vnode *vp = OPC2VP(opc);
921 	struct kauth_cred *cred;
922 	struct vattr *va_x;
923 	int rv;
924 
925 	/* "deadfs" */
926 	if (!vp)
927 		return 0;
928 
929 	if (needcompat()) {
930 		va_x = rump_pub_vattr_init();
931 	} else {
932 		va_x = vap;
933 	}
934 
935 	cred = cred_create(pcr);
936 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
937 	rv = RUMP_VOP_GETATTR(vp, va_x, cred);
938 	RUMP_VOP_UNLOCK(vp);
939 	cred_destroy(cred);
940 
941 	if (needcompat()) {
942 		rump_pub_vattr_to_vattr50(va_x, vap);
943 		rump_pub_vattr_free(va_x);
944 	}
945 
946 	return rv;
947 }
948 
949 /*ARGSUSED*/
950 int
951 p2k_node_setattr(struct puffs_usermount *pu, puffs_cookie_t opc,
952 	const struct vattr *vap, const struct puffs_cred *pcr)
953 {
954 	struct vnode *vp = OPC2VP(opc);
955 	struct kauth_cred *cred;
956 	struct vattr *va_x;
957 	int rv;
958 
959 	/* "deadfs" */
960 	if (!vp)
961 		return 0;
962 
963 	DOCOMPAT(vap, va_x);
964 
965 	cred = cred_create(pcr);
966 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
967 	rv = RUMP_VOP_SETATTR(vp, va_x, cred);
968 	RUMP_VOP_UNLOCK(vp);
969 	cred_destroy(cred);
970 
971 	UNDOCOMPAT(va_x);
972 
973 	return rv;
974 }
975 
976 /*ARGSUSED*/
977 int
978 p2k_node_fsync(struct puffs_usermount *pu, puffs_cookie_t opc,
979 	const struct puffs_cred *pcr, int flags, off_t offlo, off_t offhi)
980 {
981 	struct vnode *vp = OPC2VP(opc);
982 	struct kauth_cred *cred;
983 	int rv;
984 
985 	/* "deadfs" */
986 	if (!vp)
987 		return 0;
988 
989 	cred = cred_create(pcr);
990 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
991 	rv = RUMP_VOP_FSYNC(vp, cred, flags, offlo, offhi);
992 	RUMP_VOP_UNLOCK(vp);
993 	cred_destroy(cred);
994 
995 	return rv;
996 }
997 
998 /*ARGSUSED*/
999 int
1000 p2k_node_mmap(struct puffs_usermount *pu, puffs_cookie_t opc, vm_prot_t flags,
1001 	const struct puffs_cred *pcr)
1002 {
1003 	struct kauth_cred *cred;
1004 	int rv;
1005 
1006 	cred = cred_create(pcr);
1007 	rv = RUMP_VOP_MMAP(OPC2VP(opc), flags, cred);
1008 	cred_destroy(cred);
1009 
1010 	return rv;
1011 }
1012 
1013 /*ARGSUSED*/
1014 int
1015 p2k_node_seek(struct puffs_usermount *pu, puffs_cookie_t opc,
1016 	off_t oldoff, off_t newoff, const struct puffs_cred *pcr)
1017 {
1018 	struct vnode *vp = OPC2VP(opc);
1019 	struct kauth_cred *cred;
1020 	int rv;
1021 
1022 	cred = cred_create(pcr);
1023 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
1024 	rv = RUMP_VOP_SEEK(vp, oldoff, newoff, cred);
1025 	RUMP_VOP_UNLOCK(vp);
1026 	cred_destroy(cred);
1027 
1028 	return rv;
1029 }
1030 
1031 /*ARGSUSED*/
1032 int
1033 p2k_node_abortop(struct puffs_usermount *pu, puffs_cookie_t opc,
1034 	const struct puffs_cn *pcn)
1035 {
1036 	struct p2k_node *p2n_dir = opc;
1037 	struct componentname *cnp;
1038 
1039 	if ((cnp = p2n_dir->p2n_cn) != NULL) {
1040 		freecn(cnp, 0);
1041 		p2n_dir->p2n_cn = NULL;
1042 	}
1043 	if ((cnp = p2n_dir->p2n_cn_ren_src) != NULL) {
1044 		freecn(cnp, RUMPCN_FORCEFREE);
1045 		p2n_dir->p2n_cn_ren_src = NULL;
1046 	}
1047 	if ((cnp = p2n_dir->p2n_cn_ren_targ) != NULL) {
1048 		freecn(cnp, RUMPCN_FORCEFREE);
1049 		p2n_dir->p2n_cn_ren_targ = NULL;
1050 	}
1051 
1052 	return 0;
1053 }
1054 
1055 static int
1056 do_nukenode(struct p2k_node *p2n_dir, struct p2k_node *p2n,
1057 	const struct puffs_cn *pcn,
1058 	int (*nukefn)(struct vnode *, struct vnode *, struct componentname *))
1059 {
1060 	struct vnode *dvp = p2n_dir->p2n_vp, *vp = p2n->p2n_vp;
1061 	struct componentname *cn;
1062 	int rv;
1063 
1064 	if (p2n_dir->p2n_cn) {
1065 		cn = p2n_dir->p2n_cn;
1066 		p2n_dir->p2n_cn = NULL;
1067 	} else {
1068 		cn = makecn(pcn, RUMP_NAMEI_HASBUF);
1069 	}
1070 
1071 	RUMP_VOP_LOCK(dvp, LK_EXCLUSIVE);
1072 	rump_pub_vp_incref(dvp);
1073 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
1074 	rump_pub_vp_incref(vp);
1075 	rv = nukefn(dvp, vp, cn);
1076 	assert(RUMP_VOP_ISLOCKED(dvp) == 0);
1077 	assert(RUMP_VOP_ISLOCKED(vp) == 0);
1078 	freecn(cn, 0);
1079 
1080 	return rv;
1081 
1082 }
1083 
1084 /*ARGSUSED*/
1085 int
1086 p2k_node_remove(struct puffs_usermount *pu, puffs_cookie_t opc,
1087 	puffs_cookie_t targ, const struct puffs_cn *pcn)
1088 {
1089 
1090 	return do_nukenode(opc, targ, pcn, RUMP_VOP_REMOVE);
1091 }
1092 
1093 /*ARGSUSED*/
1094 int
1095 p2k_node_link(struct puffs_usermount *pu, puffs_cookie_t opc,
1096 	puffs_cookie_t targ, const struct puffs_cn *pcn)
1097 {
1098 	struct vnode *dvp = OPC2VP(opc);
1099 	struct p2k_node *p2n_dir = opc;
1100 	struct componentname *cn;
1101 	int rv;
1102 
1103 	if (p2n_dir->p2n_cn) {
1104 		cn = p2n_dir->p2n_cn;
1105 		p2n_dir->p2n_cn = NULL;
1106 	} else {
1107 		cn = makecn(pcn, RUMP_NAMEI_HASBUF);
1108 	}
1109 
1110 	RUMP_VOP_LOCK(dvp, LK_EXCLUSIVE);
1111 	rump_pub_vp_incref(dvp);
1112 	rv = RUMP_VOP_LINK(dvp, OPC2VP(targ), cn);
1113 	freecn(cn, 0);
1114 
1115 	return rv;
1116 }
1117 
1118 /*ARGSUSED*/
1119 int
1120 p2k_node_rename(struct puffs_usermount *pu,
1121 	puffs_cookie_t src_dir, puffs_cookie_t src,
1122 	const struct puffs_cn *pcn_src,
1123 	puffs_cookie_t targ_dir, puffs_cookie_t targ,
1124 	const struct puffs_cn *pcn_targ)
1125 {
1126 	struct p2k_node *p2n_srcdir = src_dir, *p2n_targdir = targ_dir;
1127 	struct vnode *dvp, *vp, *tdvp, *tvp = NULL;
1128 	struct componentname *cn_src, *cn_targ;
1129 	int rv;
1130 
1131 	if (p2n_srcdir->p2n_cn_ren_src) {
1132 		cn_src = p2n_srcdir->p2n_cn_ren_src;
1133 		p2n_srcdir->p2n_cn_ren_src = NULL;
1134 	} else {
1135 		cn_src = makecn(pcn_src, RUMP_NAMEI_HASBUF);
1136 	}
1137 
1138 	if (p2n_targdir->p2n_cn_ren_targ) {
1139 		cn_targ = p2n_targdir->p2n_cn_ren_targ;
1140 		p2n_targdir->p2n_cn_ren_targ = NULL;
1141 	} else {
1142 		cn_targ = makecn(pcn_targ, RUMP_NAMEI_HASBUF);
1143 	}
1144 
1145 	dvp = OPC2VP(src_dir);
1146 	vp = OPC2VP(src);
1147 	tdvp = OPC2VP(targ_dir);
1148 	if (targ) {
1149 		tvp = OPC2VP(targ);
1150 	}
1151 
1152 	rump_pub_vp_incref(dvp);
1153 	rump_pub_vp_incref(vp);
1154 	RUMP_VOP_LOCK(tdvp, LK_EXCLUSIVE);
1155 	rump_pub_vp_incref(tdvp);
1156 	if (tvp) {
1157 		RUMP_VOP_LOCK(tvp, LK_EXCLUSIVE);
1158 		rump_pub_vp_incref(tvp);
1159 	}
1160 	rv = RUMP_VOP_RENAME(dvp, vp, cn_src, tdvp, tvp, cn_targ);
1161 	assert(RUMP_VOP_ISLOCKED(tdvp) == 0);
1162 	if (tvp) {
1163 		assert(RUMP_VOP_ISLOCKED(tvp) == 0);
1164 	}
1165 	freecn(cn_src, RUMPCN_FORCEFREE);
1166 	freecn(cn_targ, RUMPCN_FORCEFREE);
1167 
1168 	return rv;
1169 }
1170 
1171 /*ARGSUSED*/
1172 int
1173 p2k_node_mkdir(struct puffs_usermount *pu, puffs_cookie_t opc,
1174 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
1175 	const struct vattr *vap)
1176 {
1177 
1178 	return do_makenode(pu, opc, pni, pcn, vap, NULL, RUMP_VOP_MKDIR, NULL);
1179 }
1180 
1181 /*ARGSUSED*/
1182 int
1183 p2k_node_rmdir(struct puffs_usermount *pu, puffs_cookie_t opc,
1184 	puffs_cookie_t targ, const struct puffs_cn *pcn)
1185 {
1186 
1187 	return do_nukenode(opc, targ, pcn, RUMP_VOP_RMDIR);
1188 }
1189 
1190 /*ARGSUSED*/
1191 int
1192 p2k_node_symlink(struct puffs_usermount *pu, puffs_cookie_t opc,
1193 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
1194 	const struct vattr *vap, const char *link_target)
1195 {
1196 
1197 	return do_makenode(pu, opc, pni, pcn, vap,
1198 	    __UNCONST(link_target), NULL, RUMP_VOP_SYMLINK);
1199 }
1200 
1201 /*ARGSUSED*/
1202 int
1203 p2k_node_readdir(struct puffs_usermount *pu, puffs_cookie_t opc,
1204 	struct dirent *dent, off_t *readoff, size_t *reslen,
1205 	const struct puffs_cred *pcr, int *eofflag,
1206 	off_t *cookies, size_t *ncookies)
1207 {
1208 	struct vnode *vp = OPC2VP(opc);
1209 	struct kauth_cred *cred;
1210 	struct uio *uio;
1211 	off_t *vop_cookies;
1212 	int vop_ncookies;
1213 	int rv;
1214 
1215 	cred = cred_create(pcr);
1216 	uio = rump_pub_uio_setup(dent, *reslen, *readoff, RUMPUIO_READ);
1217 	RUMP_VOP_LOCK(vp, LK_SHARED);
1218 	if (cookies) {
1219 		rv = RUMP_VOP_READDIR(vp, uio, cred, eofflag,
1220 		    &vop_cookies, &vop_ncookies);
1221 		memcpy(cookies, vop_cookies, vop_ncookies * sizeof(*cookies));
1222 		*ncookies = vop_ncookies;
1223 		free(vop_cookies);
1224 	} else {
1225 		rv = RUMP_VOP_READDIR(vp, uio, cred, eofflag, NULL, NULL);
1226 	}
1227 	RUMP_VOP_UNLOCK(vp);
1228 	if (rv == 0) {
1229 		*reslen = rump_pub_uio_getresid(uio);
1230 		*readoff = rump_pub_uio_getoff(uio);
1231 	}
1232 	rump_pub_uio_free(uio);
1233 	cred_destroy(cred);
1234 
1235 	return rv;
1236 }
1237 
1238 /*ARGSUSED*/
1239 int
1240 p2k_node_readlink(struct puffs_usermount *pu, puffs_cookie_t opc,
1241 	const struct puffs_cred *pcr, char *linkname, size_t *linklen)
1242 {
1243 	struct vnode *vp = OPC2VP(opc);
1244 	struct kauth_cred *cred;
1245 	struct uio *uio;
1246 	int rv;
1247 
1248 	cred = cred_create(pcr);
1249 	uio = rump_pub_uio_setup(linkname, *linklen, 0, RUMPUIO_READ);
1250 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
1251 	rv = RUMP_VOP_READLINK(vp, uio, cred);
1252 	RUMP_VOP_UNLOCK(vp);
1253 	*linklen -= rump_pub_uio_free(uio);
1254 	cred_destroy(cred);
1255 
1256 	return rv;
1257 }
1258 
1259 /*ARGSUSED*/
1260 int
1261 p2k_node_read(struct puffs_usermount *pu, puffs_cookie_t opc,
1262 	uint8_t *buf, off_t offset, size_t *resid,
1263 	const struct puffs_cred *pcr, int ioflag)
1264 {
1265 	struct vnode *vp = OPC2VP(opc);
1266 	struct kauth_cred *cred;
1267 	struct uio *uio;
1268 	int rv;
1269 
1270 	cred = cred_create(pcr);
1271 	uio = rump_pub_uio_setup(buf, *resid, offset, RUMPUIO_READ);
1272 	RUMP_VOP_LOCK(vp, LK_SHARED);
1273 	rv = RUMP_VOP_READ(vp, uio, ioflag, cred);
1274 	RUMP_VOP_UNLOCK(vp);
1275 	*resid = rump_pub_uio_free(uio);
1276 	cred_destroy(cred);
1277 
1278 	return rv;
1279 }
1280 
1281 /*ARGSUSED*/
1282 int
1283 p2k_node_write(struct puffs_usermount *pu, puffs_cookie_t opc,
1284 	uint8_t *buf, off_t offset, size_t *resid,
1285 	const struct puffs_cred *pcr, int ioflag)
1286 {
1287 	struct vnode *vp = OPC2VP(opc);
1288 	struct kauth_cred *cred;
1289 	struct uio *uio;
1290 	int rv;
1291 
1292 	/* "deadfs" */
1293 	if (!vp)
1294 		return 0;
1295 
1296 	cred = cred_create(pcr);
1297 	uio = rump_pub_uio_setup(buf, *resid, offset, RUMPUIO_WRITE);
1298 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
1299 	rv = RUMP_VOP_WRITE(vp, uio, ioflag, cred);
1300 	RUMP_VOP_UNLOCK(vp);
1301 	*resid = rump_pub_uio_free(uio);
1302 	cred_destroy(cred);
1303 
1304 	return rv;
1305 }
1306 
1307 /*ARGSUSED*/
1308 int
1309 p2k_node_pathconf(struct puffs_usermount *pu, puffs_cookie_t opc,
1310 	int name, register_t *retval)
1311 {
1312 	struct vnode *vp = OPC2VP(opc);
1313 	int rv;
1314 
1315 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
1316 	rv = RUMP_VOP_PATHCONF(vp, name, retval);
1317 	RUMP_VOP_UNLOCK(vp);
1318 
1319 	return rv;
1320 }
1321 
1322 /*ARGSUSED*/
1323 int
1324 p2k_node_getextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
1325 	int attrnamespace, const char *attrname, size_t *attrsize,
1326 	uint8_t *attr, size_t *resid, const struct puffs_cred *pcr)
1327 {
1328 	struct vnode *vp = OPC2VP(opc);
1329 	struct kauth_cred *cred;
1330 	struct uio *uio;
1331 	int rv;
1332 
1333 	if (attr)
1334 		uio = rump_pub_uio_setup(attr, *resid, 0, RUMPUIO_READ);
1335 	else
1336 		uio = NULL;
1337 
1338 	cred = cred_create(pcr);
1339 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
1340 	rv = RUMP_VOP_GETEXTATTR(vp, attrnamespace, attrname, uio,
1341 	    attrsize, cred);
1342 	RUMP_VOP_UNLOCK(vp);
1343 	cred_destroy(cred);
1344 
1345 	if (uio)
1346 		*resid = rump_pub_uio_free(uio);
1347 
1348 	return rv;
1349 }
1350 
1351 /*ARGSUSED*/
1352 int
1353 p2k_node_setextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
1354 	int attrnamespace, const char *attrname,
1355 	uint8_t *attr, size_t *resid, const struct puffs_cred *pcr)
1356 {
1357 	struct vnode *vp = OPC2VP(opc);
1358 	struct kauth_cred *cred;
1359 	struct uio *uio;
1360 	int rv;
1361 
1362 	if (attr)
1363 		uio = rump_pub_uio_setup(attr, *resid, 0, RUMPUIO_READ);
1364 	else
1365 		uio = NULL;
1366 
1367 	cred = cred_create(pcr);
1368 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
1369 	rv = RUMP_VOP_SETEXTATTR(vp, attrnamespace, attrname, uio, cred);
1370 	RUMP_VOP_UNLOCK(vp);
1371 	cred_destroy(cred);
1372 
1373 	if (uio)
1374 		*resid = rump_pub_uio_free(uio);
1375 
1376 	return rv;
1377 }
1378 
1379 /*ARGSUSED*/
1380 int
1381 p2k_node_listextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
1382 	int attrnamespace, size_t *attrsize,
1383 	uint8_t *attrs, size_t *resid, const struct puffs_cred *pcr)
1384 {
1385 	struct vnode *vp = OPC2VP(opc);
1386 	struct kauth_cred *cred;
1387 	struct uio *uio;
1388 	int rv;
1389 
1390 	if (attrs)
1391 		uio = rump_pub_uio_setup(attrs, *resid, 0, RUMPUIO_READ);
1392 	else
1393 		uio = NULL;
1394 
1395 	cred = cred_create(pcr);
1396 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
1397 	rv = RUMP_VOP_LISTEXTATTR(vp, attrnamespace, uio, attrsize, cred);
1398 	RUMP_VOP_UNLOCK(vp);
1399 	cred_destroy(cred);
1400 
1401 	if (uio)
1402 		*resid = rump_pub_uio_free(uio);
1403 
1404 	return rv;
1405 }
1406 
1407 /*ARGSUSED*/
1408 int
1409 p2k_node_deleteextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
1410 	int attrnamespace, const char *attrname, const struct puffs_cred *pcr)
1411 {
1412 	struct vnode *vp = OPC2VP(opc);
1413 	struct kauth_cred *cred;
1414 	int rv;
1415 
1416 	cred = cred_create(pcr);
1417 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
1418 	rv = RUMP_VOP_DELETEEXTATTR(vp, attrnamespace, attrname, cred);
1419 	RUMP_VOP_UNLOCK(vp);
1420 	cred_destroy(cred);
1421 
1422 	return rv;
1423 }
1424 
1425 /* the kernel releases its last reference here */
1426 int
1427 p2k_node_inactive(struct puffs_usermount *pu, puffs_cookie_t opc)
1428 {
1429 	struct p2k_mount *p2m = puffs_getspecific(pu);
1430 	struct p2k_node *p2n = opc;
1431 	struct vnode *vp = OPC2VP(opc);
1432 	bool recycle = false;
1433 	int rv;
1434 
1435 	/* deadfs */
1436 	if (!vp)
1437 		return 0;
1438 
1439 	/*
1440 	 * Flush all cached vnode pages from the rump kernel -- they
1441 	 * are kept in puffs for all things that matter.  However,
1442 	 * don't do this for tmpfs (vnodes are backed by an aobj), since that
1443 	 * would cause us to clear the backing storage leaving us without
1444 	 * a way to regain the data from "stable storage".
1445 	 */
1446 	if (!p2m->p2m_imtmpfsman) {
1447 		rump_pub_vp_interlock(vp);
1448 		RUMP_VOP_PUTPAGES(vp, 0, 0,
1449 		    PGO_ALLPAGES|PGO_CLEANIT|PGO_FREE);
1450 	}
1451 
1452 	/*
1453 	 * Ok, this is where we get nasty.  We pretend the vnode is
1454 	 * inactive and already tell the file system that.  However,
1455 	 * we are allowed to pretend it also grows a reference immediately
1456 	 * after per vget(), so this does not do harm.  Cheap trick, but ...
1457 	 *
1458 	 * If the file system thinks the inode is done for, we release
1459 	 * our reference and clear all knowledge of the vnode.  If,
1460 	 * however, the inode is still active, we retain our reference
1461 	 * until reclaim, since puffs might be flushing out some data
1462 	 * later.
1463 	 */
1464 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
1465 	rv = RUMP_VOP_INACTIVE(vp, &recycle);
1466 	if (recycle) {
1467 		puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N1);
1468 		rump_pub_vp_rele(p2n->p2n_vp);
1469 		p2n->p2n_vp = NULL;
1470 	}
1471 
1472 	return rv;
1473 }
1474 
1475 /*ARGSUSED*/
1476 int
1477 p2k_node_reclaim(struct puffs_usermount *pu, puffs_croissant_t opc)
1478 {
1479 	struct p2k_node *p2n = opc;
1480 
1481 	if (p2n->p2n_vp) {
1482 		rump_pub_vp_rele(p2n->p2n_vp);
1483 		p2n->p2n_vp = NULL;
1484 	}
1485 
1486 	freep2n(p2n);
1487 	return 0;
1488 }
1489