xref: /netbsd-src/sbin/mount_portal/puffs_portal.c (revision 16dce51364ebe8aeafbae46bc5aa167b8115bc45)
1 /*	$NetBSD: puffs_portal.c,v 1.9 2017/05/10 16:35:18 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2007  Antti Kantee.  All Rights Reserved.
5  * Development was supported by the Finnish Cultural Foundation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #ifndef lint
31 __RCSID("$NetBSD: puffs_portal.c,v 1.9 2017/05/10 16:35:18 christos Exp $");
32 #endif /* !lint */
33 
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <sys/socket.h>
37 
38 #include <assert.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <mntopts.h>
42 #include <paths.h>
43 #include <poll.h>
44 #include <puffs.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <util.h>
50 
51 #include "portald.h"
52 
53 struct portal_node {
54 	char *path;
55 	int fd;
56 };
57 
58 __dead static void usage(void);
59 
60 PUFFSOP_PROTOS(portal);
61 
62 #define PORTAL_ROOT NULL
63 #define METADATASIZE (sizeof(int) + sizeof(size_t))
64 
65 qelem q;
66 int readcfg, sigchild;
67 const char *cfg;
68 
69 static void
70 usage(void)
71 {
72 
73 	errx(1, "usage: %s [-o options] /path/portal.conf mount_point",
74 	    getprogname());
75 }
76 
77 static void
78 sighup(int sig)
79 {
80 
81 	readcfg = 1;
82 }
83 
84 static void
85 sigcry(int sig)
86 {
87 
88 	sigchild = 1;
89 }
90 
91 static void
92 portal_loopfn(struct puffs_usermount *pu)
93 {
94 
95 	if (readcfg)
96 		conf_read(&q, cfg);
97 	readcfg = 0;
98 
99 	if (sigchild) {
100 		sigchild = 0;
101 		while (waitpid(-1, NULL, WNOHANG) != -1)
102 			continue;
103 	}
104 }
105 
106 #define PUFBUF_FD	1
107 #define PUFBUF_DATA	2
108 
109 #define CMSIZE (sizeof(struct cmsghdr) + sizeof(int))
110 
111 /* receive file descriptor produced by our child process */
112 static int
113 readfd(struct puffs_framebuf *pufbuf, int fd, int *done)
114 {
115 	struct cmsghdr *cmp;
116 	struct msghdr msg;
117 	struct iovec iov;
118 	ssize_t n;
119 	int error, rv;
120 
121 	rv = 0;
122 	cmp = emalloc(CMSG_SPACE(sizeof(int)));
123 
124 	iov.iov_base = &error;
125 	iov.iov_len = sizeof(int);
126 	msg.msg_iov = &iov;
127 	msg.msg_iovlen = 1;
128 	msg.msg_name = NULL;
129 	msg.msg_namelen = 0;
130 	msg.msg_control = cmp;
131 	msg.msg_controllen = CMSG_SPACE(sizeof(int));
132 
133 	n = recvmsg(fd, &msg, 0);
134 	if (n == -1) {
135 		rv = errno;
136 		goto out;
137 	}
138 	if (n == 0) {
139 		rv = ECONNRESET;
140 		goto out;
141 	}
142 
143 	/* the data for the server */
144 	puffs_framebuf_putdata_atoff(pufbuf, 0, &error, sizeof(int));
145 	if (error) {
146 		rv = error;
147 		goto out;
148 	}
149 	puffs_framebuf_putdata_atoff(pufbuf, sizeof(int),
150 	    CMSG_DATA(cmp), sizeof(int));
151 	*done = 1;
152 
153  out:
154 	free(cmp);
155 	return rv;
156 }
157 
158 /*
159  * receive data from provider
160  *
161  * XXX: should read directly into the buffer and adjust offsets
162  * instead of doing memcpy
163  */
164 static int
165 readdata(struct puffs_framebuf *pufbuf, int fd, int *done)
166 {
167 	char buf[1024];
168 	size_t max;
169 	ssize_t n;
170 	size_t moved;
171 
172 	/* don't override metadata */
173 	if (puffs_framebuf_telloff(pufbuf) == 0)
174 		puffs_framebuf_seekset(pufbuf, METADATASIZE);
175 	puffs_framebuf_getdata_atoff(pufbuf, sizeof(int), &max, sizeof(size_t));
176 	moved = puffs_framebuf_tellsize(pufbuf) - METADATASIZE;
177 	assert(max >= moved);
178 	max -= moved;
179 
180 	do {
181 		n = read(fd, buf, MIN(sizeof(buf), max));
182 		if (n == 0) {
183 			/*
184 			 * Deal with EOF here by closing the file descriptor
185 			 * and thus causing an error on subsequent accesses.
186 			 * This is the last kevent notification we are going
187 			 * to be getting for regular files.
188 			 */
189 			close(fd);
190 			if (moved)
191 				break;
192 			else
193 				return -1; /* caught by read */
194 		}
195 		if (n < 0) {
196 			if (moved)
197 				return 0;
198 
199 			if (errno != EAGAIN)
200 				return errno;
201 			else
202 				return 0;
203 		}
204 
205 		puffs_framebuf_putdata(pufbuf, buf, n);
206 		moved += n;
207 		max -= n;
208 	} while (max > 0);
209 
210 	*done = 1;
211 
212 	return 0;
213 }
214 
215 static int
216 portal_frame_rf(struct puffs_usermount *pu, struct puffs_framebuf *pufbuf,
217 	int fd, int *done)
218 {
219 	int type;
220 
221 	if (puffs_framebuf_getdata_atoff(pufbuf, 0, &type, sizeof(int)) == -1)
222 		return EINVAL;
223 
224 	if (type == PUFBUF_FD)
225 		return readfd(pufbuf, fd, done);
226 	else if (type == PUFBUF_DATA)
227 		return readdata(pufbuf, fd, done);
228 	else
229 		abort();
230 }
231 
232 static int
233 portal_frame_wf(struct puffs_usermount *pu, struct puffs_framebuf *pufbuf,
234 	int fd, int *done)
235 {
236 	void *win;
237 	size_t pbsize, pboff, winlen;
238 	ssize_t n;
239 	int error;
240 
241 	pboff = puffs_framebuf_telloff(pufbuf);
242 	pbsize = puffs_framebuf_tellsize(pufbuf);
243 	error = 0;
244 
245 	do {
246 		assert(pbsize > pboff);
247 		winlen = pbsize - pboff;
248 		if (puffs_framebuf_getwindow(pufbuf, pboff, &win, &winlen)==-1)
249 			return errno;
250 		n = write(fd, win, winlen);
251 		if (n == 0) {
252 			if (pboff != 0)
253 				break;
254 			else
255 				return -1; /* caught by node_write */
256 		}
257 		if (n < 0) {
258 			if (pboff != 0)
259 				break;
260 
261 			if (errno != EAGAIN)
262 				return errno;
263 			return 0;
264 		}
265 
266 		pboff += n;
267 		puffs_framebuf_seekset(pufbuf, pboff);
268 	} while (pboff != pbsize);
269 
270 	*done = 1;
271 	puffs_framebuf_putdata_atoff(pufbuf, 0, &pboff, sizeof(size_t));
272 	return error;
273 }
274 
275 /* transfer file descriptor to master file server */
276 static int
277 sendfd(int s, int fd, int error)
278 {
279 	struct cmsghdr *cmp;
280 	struct msghdr msg;
281 	struct iovec iov;
282 	ssize_t n;
283 	int rv;
284 
285 	rv = 0;
286 	cmp = emalloc(CMSG_LEN(sizeof(int)));
287 
288 	iov.iov_base = &error;
289 	iov.iov_len = sizeof(int);
290 
291 	msg.msg_iov = &iov;
292 	msg.msg_iovlen = 1;
293 	msg.msg_name = NULL;
294 	msg.msg_namelen = 0;
295 	if (error == 0) {
296 		cmp->cmsg_level = SOL_SOCKET;
297 		cmp->cmsg_type = SCM_RIGHTS;
298 		cmp->cmsg_len = CMSG_LEN(sizeof(int));
299 
300 		msg.msg_control = cmp;
301 		msg.msg_controllen = CMSG_LEN(sizeof(int));
302 		*(int *)CMSG_DATA(cmp) = fd;
303 	} else {
304 		msg.msg_control = NULL;
305 		msg.msg_controllen = 0;
306 	}
307 
308 	n = sendmsg(s, &msg, 0);
309 	if (n == -1)
310 		rv = errno;
311 	else if (n < (ssize_t)sizeof(int))
312 		rv = EPROTO;
313 
314 	free(cmp);
315 	return rv;
316 }
317 
318 /*
319  * Produce I/O file descriptor by forking (like original portald).
320  *
321  * child: run provider and transfer produced fd to parent
322  * parent: yield until child produces fd.  receive it and store it.
323  */
324 static int
325 provide(struct puffs_usermount *pu, struct portal_node *portn,
326 	struct portal_cred *portc, char **v)
327 {
328 	struct puffs_cc *pcc = puffs_cc_getcc(pu);
329 	struct puffs_framebuf *pufbuf;
330 	int s[2];
331 	int fd, error;
332 	int data;
333 
334 	pufbuf = puffs_framebuf_make();
335 	if (pufbuf == NULL)
336 		return ENOMEM;
337 
338 	data = PUFBUF_FD;
339 	if (puffs_framebuf_putdata(pufbuf, &data, sizeof(int)) == -1)
340 		goto bad;
341 
342 	if (socketpair(AF_LOCAL, SOCK_STREAM, 0, s) == -1)
343 		goto bad;
344 
345 	switch (fork()) {
346 	case -1:
347 		goto bad;
348 	case 0:
349 		error = activate_argv(portc, portn->path, v, &fd);
350 		sendfd(s[1], fd, error);
351 		exit(0);
352 	default:
353 		puffs_framev_addfd(pu, s[0], PUFFS_FBIO_READ);
354 		puffs_framev_enqueue_directreceive(pcc, s[0], pufbuf, 0);
355 		puffs_framev_removefd(pu, s[0], 0);
356 		close(s[0]);
357 		close(s[1]);
358 
359 		if (puffs_framebuf_tellsize(pufbuf) < sizeof(int)) {
360 			errno = EIO;
361 			goto bad;
362 		}
363 		puffs_framebuf_getdata_atoff(pufbuf, 0, &error, sizeof(int));
364 		if (error) {
365 			errno = error;
366 			goto bad;
367 		}
368 
369 		if (puffs_framebuf_tellsize(pufbuf) != 2*sizeof(int)) {
370 			errno = EIO;
371 			goto bad;
372 		}
373 
374 		puffs_framebuf_getdata_atoff(pufbuf, sizeof(int),
375 		    &fd, sizeof(int));
376 		puffs_framebuf_destroy(pufbuf);
377 
378 		data = 1;
379 		if (ioctl(fd, FIONBIO, &data) == -1)
380 			return errno;
381 
382 		if (puffs_framev_addfd(pu, fd, PUFFS_FBIO_WRITE) == -1)
383 			return errno;
384 
385 		portn->fd = fd;
386 		return 0;
387 	}
388 
389  bad:
390 	puffs_framebuf_destroy(pufbuf);
391 	return errno;
392 }
393 
394 int
395 main(int argc, char *argv[])
396 {
397 	extern char *optarg;
398 	extern int optind;
399 	struct puffs_usermount *pu;
400 	struct puffs_ops *pops;
401 	mntoptparse_t mp;
402 	int pflags, mntflags;
403 	int detach;
404 	int ch;
405 
406 	setprogname(argv[0]);
407 
408 	mntflags = pflags = 0;
409 	detach = 1;
410 	while ((ch = getopt(argc, argv, "o:s")) != -1) {
411 		switch (ch) {
412 		case 'o':
413 			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
414 			if (mp == NULL)
415 				err(1, "getmntopts");
416 			freemntopts(mp);
417 			break;
418 		case 's': /* stay on top */
419 			detach = 0;
420 			break;
421 		default:
422 			usage();
423 			/*NOTREACHED*/
424 		}
425 	}
426 	pflags |= PUFFS_KFLAG_NOCACHE | PUFFS_KFLAG_LOOKUP_FULLPNBUF;
427 	if (pflags & PUFFS_FLAG_OPDUMP)
428 		detach = 0;
429 	argc -= optind;
430 	argv += optind;
431 
432 	if (argc != 2)
433 		usage();
434 
435 	PUFFSOP_INIT(pops);
436 
437 	PUFFSOP_SETFSNOP(pops, unmount);
438 	PUFFSOP_SETFSNOP(pops, sync);
439 	PUFFSOP_SETFSNOP(pops, statvfs);
440 
441 	PUFFSOP_SET(pops, portal, node, lookup);
442 	PUFFSOP_SET(pops, portal, node, getattr);
443 	PUFFSOP_SET(pops, portal, node, setattr);
444 	PUFFSOP_SET(pops, portal, node, open);
445 	PUFFSOP_SET(pops, portal, node, read);
446 	PUFFSOP_SET(pops, portal, node, write);
447 	PUFFSOP_SET(pops, portal, node, seek);
448 	PUFFSOP_SET(pops, portal, node, poll);
449 	PUFFSOP_SET(pops, portal, node, inactive);
450 	PUFFSOP_SET(pops, portal, node, reclaim);
451 
452 	pu = puffs_init(pops, _PATH_PUFFS, "portal", NULL, pflags);
453 	if (pu == NULL)
454 		err(1, "init");
455 
456 	if (signal(SIGHUP, sighup) == SIG_ERR)
457 		warn("cannot set sighup handler");
458 	if (signal(SIGCHLD, sigcry) == SIG_ERR)
459 		err(1, "cannot set sigchild handler");
460 	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
461 		err(1, "cannot ignore sigpipe");
462 
463 	readcfg = 0;
464 	cfg = argv[0];
465 	if (*cfg != '/')
466 		errx(1, "need absolute path for config");
467 	q.q_forw = q.q_back = &q;
468 	if (conf_read(&q, cfg) == -1)
469 		err(1, "cannot read cfg \"%s\"", cfg);
470 
471 	puffs_ml_setloopfn(pu, portal_loopfn);
472 	puffs_framev_init(pu, portal_frame_rf, portal_frame_wf, NULL,NULL,NULL);
473 
474 	if (detach)
475 		if (puffs_daemon(pu, 1, 1) == -1)
476 			err(1, "puffs_daemon");
477 
478 	if (puffs_mount(pu,  argv[1], mntflags, PORTAL_ROOT) == -1)
479 		err(1, "mount");
480 	if (puffs_mainloop(pu) == -1)
481 		err(1, "mainloop");
482 
483 	return 0;
484 }
485 
486 static struct portal_node *
487 makenode(const char *path)
488 {
489 	struct portal_node *portn;
490 
491 	portn = emalloc(sizeof(struct portal_node));
492 	portn->path = estrdup(path);
493 	portn->fd = -1;
494 
495 	return portn;
496 }
497 
498 static void
499 credtr(struct portal_cred *portc, const struct puffs_cred *puffc, int mode)
500 {
501 	memset(portc, 0, sizeof(struct portal_cred));
502 
503 	portc->pcr_flag = mode;
504 	puffs_cred_getuid(puffc, &portc->pcr_uid);
505 	puffs_cred_getgid(puffc, &portc->pcr_gid);
506 	puffs_cred_getgroups(puffc, portc->pcr_groups,
507 	    (short *)&portc->pcr_ngroups);
508 }
509 
510 /*
511  * XXX: we could also simply already resolve the name at this stage
512  * instead of deferring it to open.  But doing it in open is how the
513  * original portald does it, and I don't want to introduce any funny
514  * incompatibilities.
515  */
516 int
517 portal_node_lookup(struct puffs_usermount *pu, puffs_cookie_t opc,
518 	struct puffs_newinfo *pni, const struct puffs_cn *pcn)
519 {
520 	struct portal_node *portn;
521 
522 	assert(opc == PORTAL_ROOT);
523 
524 	if (pcn->pcn_nameiop != NAMEI_LOOKUP
525 	    && pcn->pcn_nameiop != NAMEI_CREATE)
526 		return EOPNOTSUPP;
527 
528 	portn = makenode(pcn->pcn_name);
529 	puffs_newinfo_setcookie(pni, portn);
530 	puffs_newinfo_setvtype(pni, VREG);
531 
532 	pcn->pcn_flags &= ~NAMEI_REQUIREDIR;
533 	pcn->pcn_consume = strlen(pcn->pcn_name) - pcn->pcn_namelen;
534 
535 	return 0;
536 }
537 
538 int fakeid = 3;
539 
540 /* XXX: libpuffs'ize */
541 int
542 portal_node_getattr(struct puffs_usermount *pu, puffs_cookie_t opc,
543 	struct vattr *va, const struct puffs_cred *pcr)
544 {
545 	struct timeval tv;
546 	struct timespec ts;
547 
548 	puffs_vattr_null(va);
549 	if (opc == PORTAL_ROOT) {
550 		va->va_type = VDIR;
551 		va->va_mode = 0777;
552 		va->va_nlink = 2;
553 		va->va_uid = va->va_gid = 0;
554 		va->va_fileid = fakeid++;
555 		va->va_size = va->va_bytes = 0;
556 		va->va_gen = 0;
557 		va->va_rdev = PUFFS_VNOVAL;
558 		va->va_blocksize = DEV_BSIZE;
559 
560 		gettimeofday(&tv, NULL);
561 		TIMEVAL_TO_TIMESPEC(&tv, &ts);
562 		va->va_atime = va->va_ctime = va->va_mtime =
563 		    va->va_birthtime = ts;
564 	} else {
565 		/* cheat for now */
566 		int error;
567 		struct stat st;
568 		struct portal_node *portn = opc;
569 		struct portal_cred portc;
570 		char **v = conf_match(&q, portn->path);
571 		if (v == NULL)
572 			return ENOENT;
573 		credtr(&portc, pcr, 0777);
574 		error = provide(pu, portn, &portc, v);
575 		if (error)
576 			return error;
577 		if (fstat(portn->fd, &st) == -1)
578 			return errno;
579 		va->va_type = S_ISDIR(st.st_mode) ? VDIR : VREG; /* XXX */
580 		va->va_mode = st.st_mode;
581 		va->va_nlink = st.st_nlink;
582 		va->va_uid = st.st_uid;
583 		va->va_gid = st.st_gid;
584 		va->va_fileid = st.st_ino;
585 		va->va_size = va->va_bytes = st.st_size;
586 		va->va_gen = 0;
587 		va->va_rdev = st.st_rdev;
588 		va->va_blocksize = st.st_blksize;
589 		va->va_atime = st.st_atim;
590 		va->va_ctime = st.st_ctim;
591 		va->va_mtime = st.st_mtim;
592 		va->va_birthtime = st.st_birthtim;
593 		portal_node_reclaim(pu, opc);
594 	}
595 
596 	return 0;
597 }
598 
599 /* for writing, just pretend we care */
600 int
601 portal_node_setattr(struct puffs_usermount *pu, puffs_cookie_t opc,
602 	const struct vattr *va, const struct puffs_cred *pcr)
603 {
604 
605 	return 0;
606 }
607 
608 int
609 portal_node_open(struct puffs_usermount *pu, puffs_cookie_t opc, int mode,
610 	const struct puffs_cred *pcr)
611 {
612 	struct portal_node *portn = opc;
613 	struct portal_cred portc;
614 	char **v;
615 
616 	if (opc == PORTAL_ROOT)
617 		return 0;
618 
619 	if (mode & O_NONBLOCK)
620 		return EOPNOTSUPP;
621 
622 	v = conf_match(&q, portn->path);
623 	if (v == NULL)
624 		return ENOENT;
625 
626 	credtr(&portc, pcr, mode);
627 	return provide(pu, portn, &portc, v);
628 }
629 
630 int
631 portal_node_read(struct puffs_usermount *pu, puffs_cookie_t opc,
632 	uint8_t *buf, off_t offset, size_t *resid,
633 	const struct puffs_cred *pcr, int ioflag)
634 {
635 	struct puffs_cc *pcc = puffs_cc_getcc(pu);
636 	struct portal_node *portn = opc;
637 	struct puffs_framebuf *pufbuf;
638 	size_t xfersize, winsize, boff;
639 	void *win;
640 	int rv, error;
641 	int data, dummy;
642 
643 	assert(opc != PORTAL_ROOT);
644 	error = 0;
645 
646 	/* if we can't (re-)enable it, treat it as EOF */
647 	rv = puffs_framev_enablefd(pu, portn->fd, PUFFS_FBIO_READ);
648 	if (rv == -1)
649 		return 0;
650 
651 	pufbuf = puffs_framebuf_make();
652 	data = PUFBUF_DATA;
653 	puffs_framebuf_putdata(pufbuf, &data, sizeof(int));
654 	puffs_framebuf_putdata(pufbuf, resid, sizeof(size_t));
655 
656 	/* if we are doing nodelay, do read directly */
657 	if (ioflag & PUFFS_IO_NDELAY) {
658 		rv = readdata(pufbuf, portn->fd, &dummy);
659 		if (rv != 0) {
660 			error = rv;
661 			goto out;
662 		}
663 	} else {
664 		rv = puffs_framev_enqueue_directreceive(pcc,
665 		    portn->fd, pufbuf, 0);
666 
667 		if (rv == -1) {
668 			error = errno;
669 			goto out;
670 		}
671 	}
672 
673 	xfersize = puffs_framebuf_tellsize(pufbuf) - METADATASIZE;
674 	if (xfersize == 0) {
675 		assert(ioflag & PUFFS_IO_NDELAY);
676 		error = EAGAIN;
677 		goto out;
678 	}
679 
680 	*resid -= xfersize;
681 	boff = 0;
682 	while (xfersize > 0) {
683 		winsize = xfersize;
684 		rv = puffs_framebuf_getwindow(pufbuf, METADATASIZE,
685 		    &win, &winsize);
686 		assert(rv == 0);
687 		assert(winsize > 0);
688 
689 		memcpy(buf + boff, win, winsize);
690 		xfersize -= winsize;
691 		boff += winsize;
692 	}
693 
694  out:
695 	puffs_framev_disablefd(pu, portn->fd, PUFFS_FBIO_READ);
696 	puffs_framebuf_destroy(pufbuf);
697 
698 	/* a trickery, from readdata() */
699 	if (error == -1)
700 		return 0;
701 	return error;
702 }
703 
704 int
705 portal_node_write(struct puffs_usermount *pu, puffs_cookie_t opc,
706 	uint8_t *buf, off_t offset, size_t *resid,
707 	const struct puffs_cred *pcr, int ioflag)
708 {
709 	struct puffs_cc *pcc = puffs_cc_getcc(pu);
710 	struct portal_node *portn = opc;
711 	struct puffs_framebuf *pufbuf;
712 	size_t written;
713 	int error, rv, dummy;
714 
715 	assert(opc != PORTAL_ROOT);
716 
717 	pufbuf = puffs_framebuf_make();
718 	puffs_framebuf_putdata(pufbuf, buf, *resid);
719 
720 	error = 0;
721 	if (ioflag & PUFFS_IO_NDELAY) {
722 		rv = portal_frame_wf(pu, pufbuf, portn->fd, &dummy);
723 		if (rv) {
724 			error = rv;
725 			goto out;
726 		}
727 	} else  {
728 		rv = puffs_framev_enqueue_directsend(pcc, portn->fd, pufbuf, 0);
729 		if (rv == -1) {
730 			error = errno;
731 			goto out;
732 		}
733 	}
734 
735 	rv = puffs_framebuf_getdata_atoff(pufbuf, 0, &written, sizeof(size_t));
736 	assert(rv == 0);
737 	assert(written <= *resid);
738 	*resid -= written;
739 
740  out:
741 	puffs_framebuf_destroy(pufbuf);
742 	if (error == -1)
743 		error = 0;
744 	return 0;
745 }
746 
747 int
748 portal_node_seek(struct puffs_usermount *pu, puffs_cookie_t opc,
749 	off_t oldoff, off_t newoff, const struct puffs_cred *pcr)
750 {
751 	struct portal_node *portn = opc;
752 
753 	if (opc == PORTAL_ROOT || portn->fd == -1)
754 		return EOPNOTSUPP;
755 
756 	if (lseek(portn->fd, newoff, SEEK_SET) == -1)
757 		return errno;
758 	return 0;
759 }
760 
761 int
762 portal_node_poll(struct puffs_usermount *pu, puffs_cookie_t opc, int *events)
763 {
764 	struct puffs_cc *pcc = puffs_cc_getcc(pu);
765 	struct portal_node *portn = opc;
766 	int what;
767 
768 	what = 0;
769 	if (*events & POLLIN)
770 		what |= PUFFS_FBIO_READ;
771 	if (*events & POLLOUT)
772 		what |= PUFFS_FBIO_WRITE;
773 	if (*events & POLLERR)
774 		what |= PUFFS_FBIO_ERROR;
775 
776 	if (puffs_framev_enqueue_waitevent(pcc, portn->fd, &what) == -1) {
777 		*events = POLLERR;
778 		return errno;
779 	}
780 
781 	*events = 0;
782 	if (what & PUFFS_FBIO_READ)
783 		*events |= POLLIN;
784 	if (what & PUFFS_FBIO_WRITE)
785 		*events |= POLLOUT;
786 	if (what & PUFFS_FBIO_ERROR)
787 		*events |= POLLERR;
788 
789 	return 0;
790 }
791 
792 int
793 portal_node_inactive(struct puffs_usermount *pu, puffs_cookie_t opc)
794 {
795 
796 	if (opc == PORTAL_ROOT)
797 		return 0;
798 
799 	puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N1);
800 	return 0;
801 }
802 
803 int
804 portal_node_reclaim(struct puffs_usermount *pu, puffs_cookie_t opc)
805 {
806 	struct portal_node *portn = opc;
807 
808 	if (portn->fd != -1) {
809 		puffs_framev_removefd(pu, portn->fd, 0);
810 		close(portn->fd);
811 	}
812 	free(portn->path);
813 	free(portn);
814 
815 	return 0;
816 }
817