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