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