xref: /netbsd-src/lib/libp2k/p2k.c (revision 63aea4bd5b445e491ff0389fe27ec78b3099dba3)
1 /*	$NetBSD: p2k.c,v 1.66 2015/04/20 23:03:07 riastradh 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_pub_vp_rele(dvp);
807 	RUMP_VOP_UNLOCK(dvp);
808 	freecn(cn);
809 
810 	if (rv == 0) {
811 		p2n = getp2n(p2m, vp, true, p2n);
812 		puffs_newinfo_setcookie(pni, p2n);
813 	} else {
814 		free(p2n);
815 	}
816 
817 	UNDOCOMPAT(va_x);
818 
819 	return rv;
820 
821 }
822 
823 /*ARGSUSED*/
824 int
825 p2k_node_create(struct puffs_usermount *pu, puffs_cookie_t opc,
826 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
827 	const struct vattr *vap)
828 {
829 
830 	return do_makenode(pu, opc, pni, pcn, vap, NULL, RUMP_VOP_CREATE, NULL);
831 }
832 
833 /*ARGSUSED*/
834 int
835 p2k_node_mknod(struct puffs_usermount *pu, puffs_cookie_t opc,
836 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
837 	const struct vattr *vap)
838 {
839 
840 	return do_makenode(pu, opc, pni, pcn, vap, NULL, RUMP_VOP_MKNOD, NULL);
841 }
842 
843 /*ARGSUSED*/
844 int
845 p2k_node_open(struct puffs_usermount *pu, puffs_cookie_t opc, int mode,
846 	const struct puffs_cred *pcr)
847 {
848 	struct vnode *vp = OPC2VP(opc);
849 	struct kauth_cred *cred;
850 	int rv;
851 
852 	cred = cred_create(pcr);
853 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
854 	rv = RUMP_VOP_OPEN(vp, mode, cred);
855 	RUMP_VOP_UNLOCK(vp);
856 	cred_destroy(cred);
857 
858 	return rv;
859 }
860 
861 /*ARGSUSED*/
862 int
863 p2k_node_close(struct puffs_usermount *pu, puffs_cookie_t opc, int flags,
864 	const struct puffs_cred *pcr)
865 {
866 	struct vnode *vp = OPC2VP(opc);
867 	struct kauth_cred *cred;
868 
869 	cred = cred_create(pcr);
870 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
871 	RUMP_VOP_CLOSE(vp, flags, cred);
872 	RUMP_VOP_UNLOCK(vp);
873 	cred_destroy(cred);
874 
875 	return 0;
876 }
877 
878 /*ARGSUSED*/
879 int
880 p2k_node_access(struct puffs_usermount *pu, puffs_cookie_t opc, int mode,
881 	const struct puffs_cred *pcr)
882 {
883 	struct vnode *vp = OPC2VP(opc);
884 	struct kauth_cred *cred;
885 	int rv;
886 
887 	cred = cred_create(pcr);
888 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
889 	rv = RUMP_VOP_ACCESS(vp, mode, cred);
890 	RUMP_VOP_UNLOCK(vp);
891 	cred_destroy(cred);
892 
893 	return rv;
894 }
895 
896 /*ARGSUSED*/
897 int
898 p2k_node_getattr(struct puffs_usermount *pu, puffs_cookie_t opc,
899 	struct vattr *vap, const struct puffs_cred *pcr)
900 {
901 	struct vnode *vp = OPC2VP(opc);
902 	struct kauth_cred *cred;
903 	struct vattr *va_x;
904 	int rv;
905 
906 	/* "deadfs" */
907 	if (!vp)
908 		return 0;
909 
910 	if (needcompat()) {
911 		va_x = rump_pub_vattr_init();
912 	} else {
913 		va_x = vap;
914 	}
915 
916 	cred = cred_create(pcr);
917 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
918 	rv = RUMP_VOP_GETATTR(vp, va_x, cred);
919 	RUMP_VOP_UNLOCK(vp);
920 	cred_destroy(cred);
921 
922 	if (needcompat()) {
923 		rump_pub_vattr_to_vattr50(va_x, vap);
924 		rump_pub_vattr_free(va_x);
925 	}
926 
927 	return rv;
928 }
929 
930 /*ARGSUSED*/
931 int
932 p2k_node_setattr(struct puffs_usermount *pu, puffs_cookie_t opc,
933 	const struct vattr *vap, const struct puffs_cred *pcr)
934 {
935 	struct vnode *vp = OPC2VP(opc);
936 	struct kauth_cred *cred;
937 	struct vattr *va_x;
938 	int rv;
939 
940 	/* "deadfs" */
941 	if (!vp)
942 		return 0;
943 
944 	DOCOMPAT(vap, va_x);
945 
946 	cred = cred_create(pcr);
947 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
948 	rv = RUMP_VOP_SETATTR(vp, va_x, cred);
949 	RUMP_VOP_UNLOCK(vp);
950 	cred_destroy(cred);
951 
952 	UNDOCOMPAT(va_x);
953 
954 	return rv;
955 }
956 
957 /*ARGSUSED*/
958 int
959 p2k_node_fsync(struct puffs_usermount *pu, puffs_cookie_t opc,
960 	const struct puffs_cred *pcr, int flags, off_t offlo, off_t offhi)
961 {
962 	struct vnode *vp = OPC2VP(opc);
963 	struct kauth_cred *cred;
964 	int rv;
965 
966 	/* "deadfs" */
967 	if (!vp)
968 		return 0;
969 
970 	cred = cred_create(pcr);
971 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
972 	rv = RUMP_VOP_FSYNC(vp, cred, flags, offlo, offhi);
973 	RUMP_VOP_UNLOCK(vp);
974 	cred_destroy(cred);
975 
976 	return rv;
977 }
978 
979 /*ARGSUSED*/
980 int
981 p2k_node_mmap(struct puffs_usermount *pu, puffs_cookie_t opc, vm_prot_t flags,
982 	const struct puffs_cred *pcr)
983 {
984 	struct kauth_cred *cred;
985 	int rv;
986 
987 	cred = cred_create(pcr);
988 	rv = RUMP_VOP_MMAP(OPC2VP(opc), flags, cred);
989 	cred_destroy(cred);
990 
991 	return rv;
992 }
993 
994 /*ARGSUSED*/
995 int
996 p2k_node_seek(struct puffs_usermount *pu, puffs_cookie_t opc,
997 	off_t oldoff, off_t newoff, const struct puffs_cred *pcr)
998 {
999 	struct vnode *vp = OPC2VP(opc);
1000 	struct kauth_cred *cred;
1001 	int rv;
1002 
1003 	cred = cred_create(pcr);
1004 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
1005 	rv = RUMP_VOP_SEEK(vp, oldoff, newoff, cred);
1006 	RUMP_VOP_UNLOCK(vp);
1007 	cred_destroy(cred);
1008 
1009 	return rv;
1010 }
1011 
1012 static int
1013 do_nukenode(struct p2k_node *p2n_dir, struct p2k_node *p2n,
1014 	const struct puffs_cn *pcn,
1015 	int (*nukefn)(struct vnode *, struct vnode *, struct componentname *))
1016 {
1017 	struct vnode *dvp = p2n_dir->p2n_vp, *vp = p2n->p2n_vp;
1018 	struct componentname *cn;
1019 	int rv;
1020 
1021 	cn = makecn(pcn);
1022 	RUMP_VOP_LOCK(dvp, LK_EXCLUSIVE);
1023 	rump_pub_vp_incref(dvp);
1024 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
1025 	rump_pub_vp_incref(vp);
1026 	rv = nukefn(dvp, vp, cn);
1027 	assert(RUMP_VOP_ISLOCKED(dvp) == 0);
1028 	assert(RUMP_VOP_ISLOCKED(vp) == 0);
1029 	freecn(cn);
1030 
1031 	return rv;
1032 
1033 }
1034 
1035 /*ARGSUSED*/
1036 int
1037 p2k_node_remove(struct puffs_usermount *pu, puffs_cookie_t opc,
1038 	puffs_cookie_t targ, const struct puffs_cn *pcn)
1039 {
1040 
1041 	return do_nukenode(opc, targ, pcn, RUMP_VOP_REMOVE);
1042 }
1043 
1044 /*ARGSUSED*/
1045 int
1046 p2k_node_link(struct puffs_usermount *pu, puffs_cookie_t opc,
1047 	puffs_cookie_t targ, const struct puffs_cn *pcn)
1048 {
1049 	struct vnode *dvp = OPC2VP(opc);
1050 	struct componentname *cn;
1051 	int rv;
1052 
1053 	cn = makecn(pcn);
1054 	RUMP_VOP_LOCK(dvp, LK_EXCLUSIVE);
1055 	rump_pub_vp_incref(dvp);
1056 	rv = RUMP_VOP_LINK(dvp, OPC2VP(targ), cn);
1057 	rump_pub_vp_rele(dvp);
1058 	RUMP_VOP_UNLOCK(dvp);
1059 	freecn(cn);
1060 
1061 	return rv;
1062 }
1063 
1064 /*ARGSUSED*/
1065 int
1066 p2k_node_rename(struct puffs_usermount *pu,
1067 	puffs_cookie_t src_dir, puffs_cookie_t src,
1068 	const struct puffs_cn *pcn_src,
1069 	puffs_cookie_t targ_dir, puffs_cookie_t targ,
1070 	const struct puffs_cn *pcn_targ)
1071 {
1072 	struct vnode *dvp, *vp, *tdvp, *tvp = NULL;
1073 	struct componentname *cn_src, *cn_targ;
1074 	int rv;
1075 
1076 	cn_src = makecn(pcn_src);
1077 	cn_targ = makecn(pcn_targ);
1078 
1079 	dvp = OPC2VP(src_dir);
1080 	vp = OPC2VP(src);
1081 	tdvp = OPC2VP(targ_dir);
1082 	if (targ) {
1083 		tvp = OPC2VP(targ);
1084 	}
1085 
1086 	rump_pub_vp_incref(dvp);
1087 	rump_pub_vp_incref(vp);
1088 	RUMP_VOP_LOCK(tdvp, LK_EXCLUSIVE);
1089 	rump_pub_vp_incref(tdvp);
1090 	if (tvp) {
1091 		RUMP_VOP_LOCK(tvp, LK_EXCLUSIVE);
1092 		rump_pub_vp_incref(tvp);
1093 	}
1094 	rv = RUMP_VOP_RENAME(dvp, vp, cn_src, tdvp, tvp, cn_targ);
1095 	assert(RUMP_VOP_ISLOCKED(tdvp) == 0);
1096 	if (tvp) {
1097 		assert(RUMP_VOP_ISLOCKED(tvp) == 0);
1098 	}
1099 	freecn(cn_src);
1100 	freecn(cn_targ);
1101 
1102 	return rv;
1103 }
1104 
1105 /*ARGSUSED*/
1106 int
1107 p2k_node_mkdir(struct puffs_usermount *pu, puffs_cookie_t opc,
1108 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
1109 	const struct vattr *vap)
1110 {
1111 
1112 	return do_makenode(pu, opc, pni, pcn, vap, NULL, RUMP_VOP_MKDIR, NULL);
1113 }
1114 
1115 /*ARGSUSED*/
1116 int
1117 p2k_node_rmdir(struct puffs_usermount *pu, puffs_cookie_t opc,
1118 	puffs_cookie_t targ, const struct puffs_cn *pcn)
1119 {
1120 
1121 	return do_nukenode(opc, targ, pcn, RUMP_VOP_RMDIR);
1122 }
1123 
1124 /*ARGSUSED*/
1125 int
1126 p2k_node_symlink(struct puffs_usermount *pu, puffs_cookie_t opc,
1127 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
1128 	const struct vattr *vap, const char *link_target)
1129 {
1130 
1131 	return do_makenode(pu, opc, pni, pcn, vap,
1132 	    __UNCONST(link_target), NULL, RUMP_VOP_SYMLINK);
1133 }
1134 
1135 /*ARGSUSED*/
1136 int
1137 p2k_node_readdir(struct puffs_usermount *pu, puffs_cookie_t opc,
1138 	struct dirent *dent, off_t *readoff, size_t *reslen,
1139 	const struct puffs_cred *pcr, int *eofflag,
1140 	off_t *cookies, size_t *ncookies)
1141 {
1142 	struct vnode *vp = OPC2VP(opc);
1143 	struct kauth_cred *cred;
1144 	struct uio *uio;
1145 	off_t *vop_cookies;
1146 	int vop_ncookies;
1147 	int rv;
1148 
1149 	cred = cred_create(pcr);
1150 	uio = rump_pub_uio_setup(dent, *reslen, *readoff, RUMPUIO_READ);
1151 	RUMP_VOP_LOCK(vp, LK_SHARED);
1152 	if (cookies) {
1153 		rv = RUMP_VOP_READDIR(vp, uio, cred, eofflag,
1154 		    &vop_cookies, &vop_ncookies);
1155 		memcpy(cookies, vop_cookies, vop_ncookies * sizeof(*cookies));
1156 		*ncookies = vop_ncookies;
1157 		free(vop_cookies);
1158 	} else {
1159 		rv = RUMP_VOP_READDIR(vp, uio, cred, eofflag, NULL, NULL);
1160 	}
1161 	RUMP_VOP_UNLOCK(vp);
1162 	if (rv == 0) {
1163 		*reslen = rump_pub_uio_getresid(uio);
1164 		*readoff = rump_pub_uio_getoff(uio);
1165 	}
1166 	rump_pub_uio_free(uio);
1167 	cred_destroy(cred);
1168 
1169 	return rv;
1170 }
1171 
1172 /*ARGSUSED*/
1173 int
1174 p2k_node_readlink(struct puffs_usermount *pu, puffs_cookie_t opc,
1175 	const struct puffs_cred *pcr, char *linkname, size_t *linklen)
1176 {
1177 	struct vnode *vp = OPC2VP(opc);
1178 	struct kauth_cred *cred;
1179 	struct uio *uio;
1180 	int rv;
1181 
1182 	cred = cred_create(pcr);
1183 	uio = rump_pub_uio_setup(linkname, *linklen, 0, RUMPUIO_READ);
1184 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
1185 	rv = RUMP_VOP_READLINK(vp, uio, cred);
1186 	RUMP_VOP_UNLOCK(vp);
1187 	*linklen -= rump_pub_uio_free(uio);
1188 	cred_destroy(cred);
1189 
1190 	return rv;
1191 }
1192 
1193 /*ARGSUSED*/
1194 int
1195 p2k_node_read(struct puffs_usermount *pu, puffs_cookie_t opc,
1196 	uint8_t *buf, off_t offset, size_t *resid,
1197 	const struct puffs_cred *pcr, int ioflag)
1198 {
1199 	struct vnode *vp = OPC2VP(opc);
1200 	struct kauth_cred *cred;
1201 	struct uio *uio;
1202 	int rv;
1203 
1204 	cred = cred_create(pcr);
1205 	uio = rump_pub_uio_setup(buf, *resid, offset, RUMPUIO_READ);
1206 	RUMP_VOP_LOCK(vp, LK_SHARED);
1207 	rv = RUMP_VOP_READ(vp, uio, ioflag, cred);
1208 	RUMP_VOP_UNLOCK(vp);
1209 	*resid = rump_pub_uio_free(uio);
1210 	cred_destroy(cred);
1211 
1212 	return rv;
1213 }
1214 
1215 /*ARGSUSED*/
1216 int
1217 p2k_node_write(struct puffs_usermount *pu, puffs_cookie_t opc,
1218 	uint8_t *buf, off_t offset, size_t *resid,
1219 	const struct puffs_cred *pcr, int ioflag)
1220 {
1221 	struct vnode *vp = OPC2VP(opc);
1222 	struct kauth_cred *cred;
1223 	struct uio *uio;
1224 	int rv;
1225 
1226 	/* "deadfs" */
1227 	if (!vp)
1228 		return 0;
1229 
1230 	cred = cred_create(pcr);
1231 	uio = rump_pub_uio_setup(buf, *resid, offset, RUMPUIO_WRITE);
1232 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
1233 	rv = RUMP_VOP_WRITE(vp, uio, ioflag, cred);
1234 	RUMP_VOP_UNLOCK(vp);
1235 	*resid = rump_pub_uio_free(uio);
1236 	cred_destroy(cred);
1237 
1238 	return rv;
1239 }
1240 
1241 /*ARGSUSED*/
1242 int
1243 p2k_node_pathconf(struct puffs_usermount *pu, puffs_cookie_t opc,
1244 	int name, register_t *retval)
1245 {
1246 	struct vnode *vp = OPC2VP(opc);
1247 	int rv;
1248 
1249 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
1250 	rv = RUMP_VOP_PATHCONF(vp, name, retval);
1251 	RUMP_VOP_UNLOCK(vp);
1252 
1253 	return rv;
1254 }
1255 
1256 /*ARGSUSED*/
1257 int
1258 p2k_node_getextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
1259 	int attrnamespace, const char *attrname, size_t *attrsize,
1260 	uint8_t *attr, size_t *resid, const struct puffs_cred *pcr)
1261 {
1262 	struct vnode *vp = OPC2VP(opc);
1263 	struct kauth_cred *cred;
1264 	struct uio *uio;
1265 	int rv;
1266 
1267 	if (attr)
1268 		uio = rump_pub_uio_setup(attr, *resid, 0, RUMPUIO_READ);
1269 	else
1270 		uio = NULL;
1271 
1272 	cred = cred_create(pcr);
1273 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
1274 	rv = RUMP_VOP_GETEXTATTR(vp, attrnamespace, attrname, uio,
1275 	    attrsize, cred);
1276 	RUMP_VOP_UNLOCK(vp);
1277 	cred_destroy(cred);
1278 
1279 	if (uio)
1280 		*resid = rump_pub_uio_free(uio);
1281 
1282 	return rv;
1283 }
1284 
1285 /*ARGSUSED*/
1286 int
1287 p2k_node_setextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
1288 	int attrnamespace, const char *attrname,
1289 	uint8_t *attr, size_t *resid, const struct puffs_cred *pcr)
1290 {
1291 	struct vnode *vp = OPC2VP(opc);
1292 	struct kauth_cred *cred;
1293 	struct uio *uio;
1294 	int rv;
1295 
1296 	if (attr)
1297 		uio = rump_pub_uio_setup(attr, *resid, 0, RUMPUIO_READ);
1298 	else
1299 		uio = NULL;
1300 
1301 	cred = cred_create(pcr);
1302 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
1303 	rv = RUMP_VOP_SETEXTATTR(vp, attrnamespace, attrname, uio, cred);
1304 	RUMP_VOP_UNLOCK(vp);
1305 	cred_destroy(cred);
1306 
1307 	if (uio)
1308 		*resid = rump_pub_uio_free(uio);
1309 
1310 	return rv;
1311 }
1312 
1313 /*ARGSUSED*/
1314 int
1315 p2k_node_listextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
1316 	int attrnamespace, size_t *attrsize, uint8_t *attrs,
1317 	size_t *resid, int flags, const struct puffs_cred *pcr)
1318 {
1319 	struct vnode *vp = OPC2VP(opc);
1320 	struct kauth_cred *cred;
1321 	struct uio *uio;
1322 	int rv;
1323 
1324 	if (attrs)
1325 		uio = rump_pub_uio_setup(attrs, *resid, 0, RUMPUIO_READ);
1326 	else
1327 		uio = NULL;
1328 
1329 	cred = cred_create(pcr);
1330 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
1331 	rv = RUMP_VOP_LISTEXTATTR(vp, attrnamespace, uio, attrsize,
1332 	    flags, cred);
1333 	RUMP_VOP_UNLOCK(vp);
1334 	cred_destroy(cred);
1335 
1336 	if (uio)
1337 		*resid = rump_pub_uio_free(uio);
1338 
1339 	return rv;
1340 }
1341 
1342 /*ARGSUSED*/
1343 int
1344 p2k_node_deleteextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
1345 	int attrnamespace, const char *attrname, const struct puffs_cred *pcr)
1346 {
1347 	struct vnode *vp = OPC2VP(opc);
1348 	struct kauth_cred *cred;
1349 	int rv;
1350 
1351 	cred = cred_create(pcr);
1352 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
1353 	rv = RUMP_VOP_DELETEEXTATTR(vp, attrnamespace, attrname, cred);
1354 	RUMP_VOP_UNLOCK(vp);
1355 	cred_destroy(cred);
1356 
1357 	return rv;
1358 }
1359 
1360 /* the kernel releases its last reference here */
1361 int
1362 p2k_node_inactive(struct puffs_usermount *pu, puffs_cookie_t opc)
1363 {
1364 	struct p2k_mount *p2m = puffs_getspecific(pu);
1365 	struct p2k_node *p2n = opc;
1366 	struct vnode *vp = OPC2VP(opc);
1367 	bool recycle = false;
1368 	int rv;
1369 
1370 	/* deadfs */
1371 	if (!vp)
1372 		return 0;
1373 
1374 	/*
1375 	 * Flush all cached vnode pages from the rump kernel -- they
1376 	 * are kept in puffs for all things that matter.  However,
1377 	 * don't do this for tmpfs (vnodes are backed by an aobj), since that
1378 	 * would cause us to clear the backing storage leaving us without
1379 	 * a way to regain the data from "stable storage".
1380 	 */
1381 	if (!p2m->p2m_imtmpfsman) {
1382 		rump_pub_vp_interlock(vp);
1383 		RUMP_VOP_PUTPAGES(vp, 0, 0,
1384 		    PGO_ALLPAGES|PGO_CLEANIT|PGO_FREE);
1385 	}
1386 
1387 	/*
1388 	 * Ok, this is where we get nasty.  We pretend the vnode is
1389 	 * inactive and already tell the file system that.  However,
1390 	 * we are allowed to pretend it also grows a reference immediately
1391 	 * after per vget(), so this does not do harm.  Cheap trick, but ...
1392 	 *
1393 	 * If the file system thinks the inode is done for, we release
1394 	 * our reference and clear all knowledge of the vnode.  If,
1395 	 * however, the inode is still active, we retain our reference
1396 	 * until reclaim, since puffs might be flushing out some data
1397 	 * later.
1398 	 */
1399 	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
1400 	rv = RUMP_VOP_INACTIVE(vp, &recycle);
1401 	if (recycle) {
1402 		puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N1);
1403 		rump_pub_vp_rele(p2n->p2n_vp);
1404 		p2n->p2n_vp = NULL;
1405 	}
1406 
1407 	return rv;
1408 }
1409 
1410 /*ARGSUSED*/
1411 int
1412 p2k_node_reclaim(struct puffs_usermount *pu, puffs_croissant_t opc)
1413 {
1414 	struct p2k_node *p2n = opc;
1415 
1416 	if (p2n->p2n_vp) {
1417 		rump_pub_vp_rele(p2n->p2n_vp);
1418 		p2n->p2n_vp = NULL;
1419 	}
1420 
1421 	freep2n(p2n);
1422 	return 0;
1423 }
1424