Home | History | Annotate | Line # | Download | only in libpuffs
framebuf.c revision 1.20
      1 /*	$NetBSD: framebuf.c,v 1.20 2007/08/25 09:30:41 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007  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  * The event portion of this code is a twisty maze of pointers,
     33  * flags, yields and continues.  Sincere aplogies.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #if !defined(lint)
     38 __RCSID("$NetBSD: framebuf.c,v 1.20 2007/08/25 09:30:41 pooka Exp $");
     39 #endif /* !lint */
     40 
     41 #include <sys/types.h>
     42 #include <sys/queue.h>
     43 
     44 #include <assert.h>
     45 #include <errno.h>
     46 #include <poll.h>
     47 #include <puffs.h>
     48 #include <stdlib.h>
     49 #include <unistd.h>
     50 
     51 #include "puffs_priv.h"
     52 
     53 struct puffs_framebuf {
     54 	struct puffs_cc *pcc;	/* pcc to continue with */
     55 	/* OR */
     56 	puffs_framev_cb fcb;	/* non-blocking callback */
     57 	void *fcb_arg;		/* argument for previous */
     58 
     59 	uint8_t *buf;		/* buffer base */
     60 	size_t len;		/* total length */
     61 
     62 	size_t offset;		/* cursor, telloff() */
     63 	size_t maxoff;		/* maximum offset for data, tellsize() */
     64 
     65 	volatile int rv;	/* errno value */
     66 
     67 	int	istat;
     68 
     69 	TAILQ_ENTRY(puffs_framebuf) pfb_entries;
     70 };
     71 #define ISTAT_NODESTROY	0x01	/* indestructible by framebuf_destroy() */
     72 #define ISTAT_INTERNAL	0x02	/* never leaves library			*/
     73 #define ISTAT_NOREPLY	0x04	/* nuke after sending 			*/
     74 #define ISTAT_DIRECT	0x08	/* receive directly, no moveinfo	*/
     75 
     76 #define ISTAT_ONQUEUE	ISTAT_NODESTROY	/* alias */
     77 
     78 #define PUFBUF_INCRALLOC 4096
     79 #define PUFBUF_REMAIN(p) (p->len - p->offset)
     80 
     81 /* for poll/kqueue */
     82 struct puffs_fbevent {
     83 	struct puffs_cc	*pcc;
     84 	int what;
     85 	volatile int rv;
     86 
     87 	LIST_ENTRY(puffs_fbevent) pfe_entries;
     88 };
     89 
     90 static struct puffs_fctrl_io *
     91 getfiobyfd(struct puffs_usermount *pu, int fd)
     92 {
     93 	struct puffs_framectrl *pfctrl = &pu->pu_framectrl;
     94 	struct puffs_fctrl_io *fio;
     95 
     96 	LIST_FOREACH(fio, &pfctrl->fb_ios, fio_entries)
     97 		if (fio->io_fd == fd)
     98 			return fio;
     99 	return NULL;
    100 }
    101 
    102 struct puffs_framebuf *
    103 puffs_framebuf_make()
    104 {
    105 	struct puffs_framebuf *pufbuf;
    106 
    107 	pufbuf = malloc(sizeof(struct puffs_framebuf));
    108 	if (pufbuf == NULL)
    109 		return NULL;
    110 	memset(pufbuf, 0, sizeof(struct puffs_framebuf));
    111 
    112 	pufbuf->buf = malloc(PUFBUF_INCRALLOC);
    113 	if (pufbuf->buf == NULL) {
    114 		free(pufbuf);
    115 		return NULL;
    116 	}
    117 	pufbuf->len = PUFBUF_INCRALLOC;
    118 
    119 	puffs_framebuf_recycle(pufbuf);
    120 	return pufbuf;
    121 }
    122 
    123 void
    124 puffs_framebuf_destroy(struct puffs_framebuf *pufbuf)
    125 {
    126 
    127 	assert((pufbuf->istat & ISTAT_NODESTROY) == 0);
    128 
    129 	free(pufbuf->buf);
    130 	free(pufbuf);
    131 }
    132 
    133 void
    134 puffs_framebuf_recycle(struct puffs_framebuf *pufbuf)
    135 {
    136 
    137 	assert((pufbuf->istat & ISTAT_NODESTROY) == 0);
    138 
    139 	pufbuf->offset = 0;
    140 	pufbuf->maxoff = 0;
    141 	pufbuf->istat = 0;
    142 }
    143 
    144 static int
    145 reservespace(struct puffs_framebuf *pufbuf, size_t off, size_t wantsize)
    146 {
    147 	size_t incr;
    148 	void *nd;
    149 
    150 	if (off <= pufbuf->len && pufbuf->len - off >= wantsize)
    151 		return 0;
    152 
    153 	for (incr = PUFBUF_INCRALLOC;
    154 	    pufbuf->len + incr < off + wantsize;
    155 	    incr += PUFBUF_INCRALLOC)
    156 		continue;
    157 
    158 	nd = realloc(pufbuf->buf, pufbuf->offset + incr);
    159 	if (nd == NULL)
    160 		return -1;
    161 
    162 	pufbuf->buf = nd;
    163 	pufbuf->len += incr;
    164 
    165 	return 0;
    166 }
    167 
    168 int
    169 puffs_framebuf_reserve_space(struct puffs_framebuf *pufbuf, size_t wantsize)
    170 {
    171 
    172 	return reservespace(pufbuf, pufbuf->offset, wantsize);
    173 }
    174 
    175 int
    176 puffs_framebuf_putdata(struct puffs_framebuf *pufbuf,
    177 	const void *data, size_t dlen)
    178 {
    179 
    180 	if (PUFBUF_REMAIN(pufbuf) < dlen)
    181 		if (puffs_framebuf_reserve_space(pufbuf, dlen) == -1)
    182 			return -1;
    183 
    184 	memcpy(pufbuf->buf + pufbuf->offset, data, dlen);
    185 	pufbuf->offset += dlen;
    186 
    187 	if (pufbuf->offset > pufbuf->maxoff)
    188 		pufbuf->maxoff = pufbuf->offset;
    189 
    190 	return 0;
    191 }
    192 
    193 int
    194 puffs_framebuf_putdata_atoff(struct puffs_framebuf *pufbuf, size_t offset,
    195 	const void *data, size_t dlen)
    196 {
    197 
    198 	if (reservespace(pufbuf, offset, dlen) == -1)
    199 		return -1;
    200 
    201 	memcpy(pufbuf->buf + offset, data, dlen);
    202 
    203 	if (offset + dlen > pufbuf->maxoff)
    204 		pufbuf->maxoff = offset + dlen;
    205 
    206 	return 0;
    207 }
    208 
    209 int
    210 puffs_framebuf_getdata(struct puffs_framebuf *pufbuf, void *data, size_t dlen)
    211 {
    212 
    213 	if (pufbuf->maxoff < pufbuf->offset + dlen) {
    214 		errno = ENOBUFS;
    215 		return -1;
    216 	}
    217 
    218 	memcpy(data, pufbuf->buf + pufbuf->offset, dlen);
    219 	pufbuf->offset += dlen;
    220 
    221 	return 0;
    222 }
    223 
    224 int
    225 puffs_framebuf_getdata_atoff(struct puffs_framebuf *pufbuf, size_t offset,
    226 	void *data, size_t dlen)
    227 {
    228 
    229 	if (pufbuf->maxoff < offset + dlen) {
    230 		errno = ENOBUFS;
    231 		return -1;
    232 	}
    233 
    234 	memcpy(data, pufbuf->buf + offset, dlen);
    235 	return 0;
    236 }
    237 
    238 size_t
    239 puffs_framebuf_telloff(struct puffs_framebuf *pufbuf)
    240 {
    241 
    242 	return pufbuf->offset;
    243 }
    244 
    245 size_t
    246 puffs_framebuf_tellsize(struct puffs_framebuf *pufbuf)
    247 {
    248 
    249 	return pufbuf->maxoff;
    250 }
    251 
    252 size_t
    253 puffs_framebuf_remaining(struct puffs_framebuf *pufbuf)
    254 {
    255 
    256 	return puffs_framebuf_tellsize(pufbuf) - puffs_framebuf_telloff(pufbuf);
    257 }
    258 
    259 int
    260 puffs_framebuf_seekset(struct puffs_framebuf *pufbuf, size_t newoff)
    261 {
    262 
    263 	if (reservespace(pufbuf, newoff, 0) == -1)
    264 		return -1;
    265 
    266 	pufbuf->offset = newoff;
    267 	return 0;
    268 }
    269 
    270 int
    271 puffs_framebuf_getwindow(struct puffs_framebuf *pufbuf, size_t winoff,
    272 	void **data, size_t *dlen)
    273 {
    274 	size_t winlen;
    275 
    276 #ifdef WINTESTING
    277 	winlen = MIN(*dlen, 32);
    278 #else
    279 	winlen = *dlen;
    280 #endif
    281 
    282 	if (reservespace(pufbuf, winoff, winlen) == -1)
    283 		return -1;
    284 
    285 	*data = pufbuf->buf + winoff;
    286 	if (pufbuf->maxoff < winoff + winlen)
    287 		pufbuf->maxoff = winoff + winlen;
    288 
    289 	return 0;
    290 }
    291 
    292 static void
    293 errnotify(struct puffs_usermount *pu, struct puffs_framebuf *pufbuf, int error)
    294 {
    295 
    296 	pufbuf->rv = error;
    297 	if (pufbuf->pcc) {
    298 		puffs_goto(pufbuf->pcc);
    299 	} else if (pufbuf->fcb) {
    300 		pufbuf->istat &= ~ISTAT_NODESTROY;
    301 		pufbuf->fcb(pu, pufbuf, pufbuf->fcb_arg, error);
    302 	} else {
    303 		pufbuf->istat &= ~ISTAT_NODESTROY;
    304 		puffs_framebuf_destroy(pufbuf);
    305 	}
    306 }
    307 
    308 #define GETFIO(fd)							\
    309 do {									\
    310 	fio = getfiobyfd(pu, fd);					\
    311 	if (fio == NULL) {						\
    312 		errno = EINVAL;						\
    313 		return -1;						\
    314 	}								\
    315 	if (fio->stat & FIO_WRGONE) {					\
    316 		errno = ESHUTDOWN;					\
    317 		return -1;						\
    318 	}								\
    319 } while (/*CONSTCOND*/0)
    320 
    321 int
    322 puffs_framev_enqueue_cc(struct puffs_cc *pcc, int fd,
    323 	struct puffs_framebuf *pufbuf, int flags)
    324 {
    325 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    326 	struct puffs_fctrl_io *fio;
    327 
    328 	/*
    329 	 * Technically we shouldn't allow this is RDGONE, but it's
    330 	 * difficult to trap write close without allowing writes.
    331 	 * And besides, there's probably a disconnect sequence in
    332 	 * the protocol, so unexpectedly getting a closed fd is
    333 	 * most likely an error condition.
    334 	 */
    335 	GETFIO(fd);
    336 
    337 	pufbuf->pcc = pcc;
    338 	pufbuf->fcb = NULL;
    339 	pufbuf->fcb_arg = NULL;
    340 
    341 	pufbuf->offset = 0;
    342 	pufbuf->istat |= ISTAT_NODESTROY;
    343 
    344 	if (flags & PUFFS_FBQUEUE_URGENT)
    345 		TAILQ_INSERT_HEAD(&fio->snd_qing, pufbuf, pfb_entries);
    346 	else
    347 		TAILQ_INSERT_TAIL(&fio->snd_qing, pufbuf, pfb_entries);
    348 
    349 	puffs_cc_yield(pcc);
    350 	if (pufbuf->rv) {
    351 		pufbuf->istat &= ~ISTAT_NODESTROY;
    352 		errno = pufbuf->rv;
    353 		return -1;
    354 	}
    355 
    356 	return 0;
    357 }
    358 
    359 int
    360 puffs_framev_enqueue_cb(struct puffs_usermount *pu, int fd,
    361 	struct puffs_framebuf *pufbuf, puffs_framev_cb fcb, void *arg,
    362 	int flags)
    363 {
    364 	struct puffs_fctrl_io *fio;
    365 
    366 	/* see enqueue_cc */
    367 	GETFIO(fd);
    368 
    369 	pufbuf->pcc = NULL;
    370 	pufbuf->fcb = fcb;
    371 	pufbuf->fcb_arg = arg;
    372 
    373 	pufbuf->offset = 0;
    374 	pufbuf->istat |= ISTAT_NODESTROY;
    375 
    376 	if (flags & PUFFS_FBQUEUE_URGENT)
    377 		TAILQ_INSERT_HEAD(&fio->snd_qing, pufbuf, pfb_entries);
    378 	else
    379 		TAILQ_INSERT_TAIL(&fio->snd_qing, pufbuf, pfb_entries);
    380 
    381 	return 0;
    382 }
    383 
    384 int
    385 puffs_framev_enqueue_justsend(struct puffs_usermount *pu, int fd,
    386 	struct puffs_framebuf *pufbuf, int reply, int flags)
    387 {
    388 	struct puffs_fctrl_io *fio;
    389 
    390 	GETFIO(fd);
    391 
    392 	pufbuf->pcc = NULL;
    393 	pufbuf->fcb = NULL;
    394 	pufbuf->fcb_arg = NULL;
    395 
    396 	pufbuf->offset = 0;
    397 	pufbuf->istat |= ISTAT_NODESTROY;
    398 	if (!reply)
    399 		pufbuf->istat |= ISTAT_NOREPLY;
    400 
    401 	if (flags & PUFFS_FBQUEUE_URGENT)
    402 		TAILQ_INSERT_HEAD(&fio->snd_qing, pufbuf, pfb_entries);
    403 	else
    404 		TAILQ_INSERT_TAIL(&fio->snd_qing, pufbuf, pfb_entries);
    405 
    406 	return 0;
    407 }
    408 
    409 /* ARGSUSED */
    410 int
    411 puffs_framev_enqueue_directreceive(struct puffs_cc *pcc, int fd,
    412 	struct puffs_framebuf *pufbuf, int flags /* used in the future */)
    413 {
    414 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    415 	struct puffs_fctrl_io *fio;
    416 
    417 	fio = getfiobyfd(pu, fd);
    418 	if (fio == NULL) {
    419 		errno = EINVAL;
    420 		return -1;
    421 	}
    422 
    423 	/* XXX: should have cur_in queue */
    424 	assert(fio->cur_in == NULL);
    425 	fio->cur_in = pufbuf;
    426 
    427 	pufbuf->pcc = pcc;
    428 	pufbuf->fcb = NULL;
    429 	pufbuf->fcb_arg = NULL;
    430 
    431 	pufbuf->offset = 0;
    432 	pufbuf->istat |= ISTAT_NODESTROY | ISTAT_DIRECT;
    433 
    434 	puffs_cc_yield(pcc);
    435 	pufbuf->istat &= ~ISTAT_NODESTROY; /* XXX: not the right place */
    436 	if (pufbuf->rv) {
    437 		errno = pufbuf->rv;
    438 		return -1;
    439 	}
    440 
    441 	return 0;
    442 }
    443 
    444 int
    445 puffs_framev_enqueue_directsend(struct puffs_cc *pcc, int fd,
    446 	struct puffs_framebuf *pufbuf, int flags)
    447 {
    448 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    449 	struct puffs_fctrl_io *fio;
    450 
    451 	if (flags & PUFFS_FBQUEUE_URGENT)
    452 		abort(); /* EOPNOTSUPP for now */
    453 
    454 	GETFIO(fd);
    455 
    456 	pufbuf->pcc = pcc;
    457 	pufbuf->fcb = NULL;
    458 	pufbuf->fcb_arg = NULL;
    459 
    460 	pufbuf->offset = 0;
    461 	pufbuf->istat |= ISTAT_NODESTROY | ISTAT_DIRECT;
    462 
    463 	TAILQ_INSERT_TAIL(&fio->snd_qing, pufbuf, pfb_entries);
    464 
    465 	puffs_cc_yield(pcc);
    466 	if (pufbuf->rv) {
    467 		pufbuf->istat &= ~ISTAT_NODESTROY;
    468 		errno = pufbuf->rv;
    469 		return -1;
    470 	}
    471 
    472 	return 0;
    473 }
    474 
    475 /*
    476  * this beauty shall remain undocumented for now
    477  */
    478 int
    479 puffs_framev_framebuf_ccpromote(struct puffs_framebuf *pufbuf,
    480 	struct puffs_cc *pcc)
    481 {
    482 
    483 	if ((pufbuf->istat & ISTAT_ONQUEUE) == 0) {
    484 		errno = EBUSY;
    485 		return -1;
    486 	}
    487 
    488 	pufbuf->pcc = pcc;
    489 	pufbuf->fcb = NULL;
    490 	pufbuf->fcb_arg = NULL;
    491 	pufbuf->istat &= ~ISTAT_NOREPLY;
    492 
    493 	puffs_cc_yield(pcc);
    494 
    495 	return 0;
    496 }
    497 
    498 int
    499 puffs_framev_enqueue_waitevent(struct puffs_cc *pcc, int fd, int *what)
    500 {
    501 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    502 	struct puffs_fctrl_io *fio;
    503 	struct puffs_fbevent feb;
    504 	struct kevent kev;
    505 	int rv, svwhat;
    506 
    507 	svwhat = *what;
    508 
    509 	if (*what == 0) {
    510 		errno = EINVAL;
    511 		return -1;
    512 	}
    513 
    514 	fio = getfiobyfd(pu, fd);
    515 	if (fio == NULL) {
    516 		errno = EINVAL;
    517 		return -1;
    518 	}
    519 
    520 	feb.pcc = pcc;
    521 	feb.what = *what & (PUFFS_FBIO_READ|PUFFS_FBIO_WRITE|PUFFS_FBIO_ERROR);
    522 
    523 	if (*what & PUFFS_FBIO_READ)
    524 		if ((fio->stat & FIO_ENABLE_R) == 0)
    525 			EV_SET(&kev, fd, EVFILT_READ, EV_ENABLE,
    526 			    0, 0, (uintptr_t)fio);
    527 
    528 	rv = kevent(pu->pu_kq, &kev, 1, NULL, 0, NULL);
    529 	if (rv != 0)
    530 		return errno;
    531 
    532 	if (*what & PUFFS_FBIO_READ)
    533 		fio->rwait++;
    534 	if (*what & PUFFS_FBIO_WRITE)
    535 		fio->wwait++;
    536 
    537 	LIST_INSERT_HEAD(&fio->ev_qing, &feb, pfe_entries);
    538 	puffs_cc_yield(pcc);
    539 
    540 	assert(svwhat == *what);
    541 
    542 	if (*what & PUFFS_FBIO_READ) {
    543 		fio->rwait--;
    544 		if (fio->rwait == 0 && (fio->stat & FIO_ENABLE_R) == 0) {
    545 			EV_SET(&kev, fd, EVFILT_READ, EV_DISABLE,
    546 			    0, 0, (uintptr_t)fio);
    547 			rv = kevent(pu->pu_kq, &kev, 1, NULL, 0, NULL);
    548 #if 0
    549 			if (rv != 0)
    550 				/* XXXXX oh dear */;
    551 #endif
    552 		}
    553 	}
    554 	if (*what & PUFFS_FBIO_WRITE)
    555 		fio->wwait--;
    556 
    557 	if (feb.rv == 0) {
    558 		*what = feb.what;
    559 		rv = 0;
    560 	} else {
    561 		*what = PUFFS_FBIO_ERROR;
    562 		errno = feb.rv;
    563 		rv = -1;
    564 	}
    565 
    566 	return rv;
    567 }
    568 
    569 void
    570 puffs_framev_notify(struct puffs_fctrl_io *fio, int what)
    571 {
    572 	struct puffs_fbevent *fbevp;
    573 
    574  restart:
    575 	LIST_FOREACH(fbevp, &fio->ev_qing, pfe_entries) {
    576 		if (fbevp->what & what) {
    577 			fbevp->what = what;
    578 			fbevp->rv = 0;
    579 			LIST_REMOVE(fbevp, pfe_entries);
    580 			puffs_cc_continue(fbevp->pcc);
    581 			goto restart;
    582 		}
    583 	}
    584 }
    585 
    586 static struct puffs_framebuf *
    587 findbuf(struct puffs_usermount *pu, struct puffs_framectrl *fctrl,
    588 	struct puffs_fctrl_io *fio, struct puffs_framebuf *findme)
    589 {
    590 	struct puffs_framebuf *cand;
    591 
    592 	TAILQ_FOREACH(cand, &fio->res_qing, pfb_entries)
    593 		if (fctrl->cmpfb(pu, findme, cand) == 0)
    594 			break;
    595 
    596 	if (cand == NULL)
    597 		return NULL;
    598 
    599 	TAILQ_REMOVE(&fio->res_qing, cand, pfb_entries);
    600 	return cand;
    601 }
    602 
    603 static void
    604 moveinfo(struct puffs_framebuf *from, struct puffs_framebuf *to)
    605 {
    606 
    607 	assert(from->istat & ISTAT_INTERNAL);
    608 
    609 	/* migrate buffer */
    610 	free(to->buf);
    611 	to->buf = from->buf;
    612 	from->buf = NULL;
    613 
    614 	/* migrate buffer info */
    615 	to->len = from->len;
    616 	to->offset = from->offset;
    617 	to->maxoff = from->maxoff;
    618 }
    619 
    620 void
    621 puffs_framev_input(struct puffs_usermount *pu, struct puffs_framectrl *fctrl,
    622 	struct puffs_fctrl_io *fio, struct puffs_putreq *ppr)
    623 {
    624 	struct puffs_framebuf *pufbuf, *appbuf;
    625 	int rv, complete;
    626 
    627 	while ((fio->stat & FIO_DEAD) == 0 && (fio->stat & FIO_ENABLE_R)) {
    628 		if ((pufbuf = fio->cur_in) == NULL) {
    629 			pufbuf = puffs_framebuf_make();
    630 			if (pufbuf == NULL)
    631 				return;
    632 			pufbuf->istat |= ISTAT_INTERNAL;
    633 			fio->cur_in = pufbuf;
    634 		}
    635 
    636 		complete = 0;
    637 		rv = fctrl->rfb(pu, pufbuf, fio->io_fd, &complete);
    638 
    639 		/* error */
    640 		if (rv) {
    641 			puffs_framev_readclose(pu, fio, rv);
    642 			fio->cur_in = NULL;
    643 			if ((pufbuf->istat & ISTAT_DIRECT) == 0) {
    644 				assert((pufbuf->istat & ISTAT_NODESTROY) == 0);
    645 				puffs_framebuf_destroy(pufbuf);
    646 			}
    647 			return;
    648 		}
    649 
    650 		/* partial read, come back to fight another day */
    651 		if (complete == 0)
    652 			break;
    653 
    654 		/* else: full read, process */
    655 		if ((pufbuf->istat & ISTAT_DIRECT) == 0) {
    656 			appbuf = findbuf(pu, fctrl, fio, pufbuf);
    657 
    658 			/* XXX: error delivery? */
    659 			if (appbuf == NULL) {
    660 				/* errno = ENOMSG; */
    661 				return;
    662 			}
    663 
    664 			moveinfo(pufbuf, appbuf);
    665 			puffs_framebuf_destroy(pufbuf);
    666 		} else {
    667 			appbuf = pufbuf;
    668 		}
    669 		appbuf->istat &= ~ISTAT_NODESTROY;
    670 		fio->cur_in = NULL;
    671 
    672 		if (appbuf->pcc) {
    673 			puffs_docc(appbuf->pcc, ppr);
    674 		} else if (appbuf->fcb) {
    675 			appbuf->fcb(pu, appbuf, appbuf->fcb_arg, 0);
    676 		} else {
    677 			puffs_framebuf_destroy(appbuf);
    678 		}
    679 
    680 		/* hopeless romantics, here we go again */
    681 	}
    682 }
    683 
    684 int
    685 puffs_framev_output(struct puffs_usermount *pu, struct puffs_framectrl *fctrl,
    686 	struct puffs_fctrl_io *fio, struct puffs_putreq *ppr)
    687 {
    688 	struct puffs_framebuf *pufbuf;
    689 	int rv, complete, done;
    690 
    691 	if (fio->stat & FIO_DEAD)
    692 		return 0;
    693 
    694 	for (pufbuf = TAILQ_FIRST(&fio->snd_qing), done = 0;
    695 	    pufbuf && (fio->stat & FIO_DEAD) == 0 && fio->stat & FIO_ENABLE_W;
    696 	    pufbuf = TAILQ_FIRST(&fio->snd_qing)) {
    697 		complete = 0;
    698 		rv = fctrl->wfb(pu, pufbuf, fio->io_fd, &complete);
    699 
    700 		if (rv) {
    701 			puffs_framev_writeclose(pu, fio, rv);
    702 			done = 1;
    703 			break;
    704 		}
    705 
    706 		/* partial write */
    707 		if (complete == 0)
    708 			return done;
    709 
    710 		/* else, complete write */
    711 		TAILQ_REMOVE(&fio->snd_qing, pufbuf, pfb_entries);
    712 
    713 		/* can't wait for result if we can't read */
    714 		if (fio->stat & FIO_RDGONE) {
    715 			errnotify(pu, pufbuf, ENXIO);
    716 			done = 1;
    717 		} else if ((pufbuf->istat & ISTAT_DIRECT)) {
    718 			pufbuf->istat &= ~ISTAT_NODESTROY;
    719 			puffs_docc(pufbuf->pcc, ppr);
    720 			done = 1;
    721 		} else if ((pufbuf->istat & ISTAT_NOREPLY) == 0) {
    722 			TAILQ_INSERT_TAIL(&fio->res_qing, pufbuf,
    723 			    pfb_entries);
    724 		} else {
    725 			pufbuf->istat &= ~ISTAT_NODESTROY;
    726 			puffs_framebuf_destroy(pufbuf);
    727 		}
    728 
    729 		/* omstart! */
    730 	}
    731 
    732 	return done;
    733 }
    734 
    735 int
    736 puffs_framev_addfd(struct puffs_usermount *pu, int fd, int what)
    737 {
    738 	struct puffs_framectrl *pfctrl = &pu->pu_framectrl;
    739 	struct puffs_fctrl_io *fio;
    740 	struct kevent *newevs;
    741 	struct kevent kev[2];
    742 	size_t nfds;
    743 	int rv, readenable;
    744 
    745 	nfds = pfctrl->nfds+1;
    746 	newevs = realloc(pfctrl->evs, (2*nfds+1) * sizeof(struct kevent));
    747 	if (newevs == NULL)
    748 		return -1;
    749 	pfctrl->evs = newevs;
    750 
    751 	fio = malloc(sizeof(struct puffs_fctrl_io));
    752 	if (fio == NULL)
    753 		return -1;
    754 	memset(fio, 0, sizeof(struct puffs_fctrl_io));
    755 	fio->io_fd = fd;
    756 	fio->cur_in = NULL;
    757 	TAILQ_INIT(&fio->snd_qing);
    758 	TAILQ_INIT(&fio->res_qing);
    759 	LIST_INIT(&fio->ev_qing);
    760 
    761 	readenable = 0;
    762 	if ((what & PUFFS_FBIO_READ) == 0)
    763 		readenable = EV_DISABLE;
    764 
    765 	if (pu->pu_state & PU_INLOOP) {
    766 		EV_SET(&kev[0], fd, EVFILT_READ,
    767 		    EV_ADD|readenable, 0, 0, (intptr_t)fio);
    768 		EV_SET(&kev[1], fd, EVFILT_WRITE,
    769 		    EV_ADD|EV_DISABLE, 0, 0, (intptr_t)fio);
    770 		rv = kevent(pu->pu_kq, kev, 2, NULL, 0, NULL);
    771 		if (rv == -1) {
    772 			free(fio);
    773 			return -1;
    774 		}
    775 	}
    776 	if (what & PUFFS_FBIO_READ)
    777 		fio->stat |= FIO_ENABLE_R;
    778 	if (what & PUFFS_FBIO_WRITE)
    779 		fio->stat |= FIO_ENABLE_W;
    780 
    781 	LIST_INSERT_HEAD(&pfctrl->fb_ios, fio, fio_entries);
    782 	pfctrl->nfds = nfds;
    783 
    784 	return 0;
    785 }
    786 
    787 /*
    788  * XXX: the following en/disable should be coalesced and executed
    789  * only during the actual kevent call.  So feel free to fix if
    790  * threatened by mindblowing boredom.
    791  */
    792 
    793 int
    794 puffs_framev_enablefd(struct puffs_usermount *pu, int fd, int what)
    795 {
    796 	struct kevent kev;
    797 	struct puffs_fctrl_io *fio;
    798 	int rv = 0;
    799 
    800 	assert((what & (PUFFS_FBIO_READ | PUFFS_FBIO_WRITE)) != 0);
    801 
    802 	fio = getfiobyfd(pu, fd);
    803 	if (fio == NULL) {
    804 		errno = ENXIO;
    805 		return -1;
    806 	}
    807 
    808 	/* write is enabled in the event loop if there is output */
    809 	if (what & PUFFS_FBIO_READ && fio->rwait == 0) {
    810 		EV_SET(&kev, fd, EVFILT_READ, EV_ENABLE, 0, 0, (uintptr_t)fio);
    811 		rv = kevent(pu->pu_kq, &kev, 1, NULL, 0, NULL);
    812 	}
    813 
    814 	if (rv == 0) {
    815 		if (what & PUFFS_FBIO_READ)
    816 			fio->stat |= FIO_ENABLE_R;
    817 		if (what & PUFFS_FBIO_WRITE)
    818 			fio->stat |= FIO_ENABLE_W;
    819 	}
    820 
    821 	return rv;
    822 }
    823 
    824 int
    825 puffs_framev_disablefd(struct puffs_usermount *pu, int fd, int what)
    826 {
    827 	struct kevent kev[2];
    828 	struct puffs_fctrl_io *fio;
    829 	size_t i;
    830 	int rv;
    831 
    832 	assert((what & (PUFFS_FBIO_READ | PUFFS_FBIO_WRITE)) != 0);
    833 
    834 	fio = getfiobyfd(pu, fd);
    835 	if (fio == NULL) {
    836 		errno = ENXIO;
    837 		return -1;
    838 	}
    839 
    840 	i = 0;
    841 	if (what & PUFFS_FBIO_READ && fio->rwait == 0) {
    842 		EV_SET(&kev[0], fd,
    843 		    EVFILT_READ, EV_DISABLE, 0, 0, (uintptr_t)fio);
    844 		i++;
    845 	}
    846 	if (what & PUFFS_FBIO_WRITE && fio->stat & FIO_WR && fio->wwait == 0) {
    847 		EV_SET(&kev[1], fd,
    848 		    EVFILT_WRITE, EV_DISABLE, 0, 0, (uintptr_t)fio);
    849 		i++;
    850 	}
    851 	if (i)
    852 		rv = kevent(pu->pu_kq, kev, i, NULL, 0, NULL);
    853 	else
    854 		rv = 0;
    855 
    856 	if (rv == 0) {
    857 		if (what & PUFFS_FBIO_READ)
    858 			fio->stat &= ~FIO_ENABLE_R;
    859 		if (what & PUFFS_FBIO_WRITE)
    860 			fio->stat &= ~FIO_ENABLE_W;
    861 	}
    862 
    863 	return rv;
    864 }
    865 
    866 void
    867 puffs_framev_readclose(struct puffs_usermount *pu,
    868 	struct puffs_fctrl_io *fio, int error)
    869 {
    870 	struct puffs_framebuf *pufbuf;
    871 	struct kevent kev;
    872 	int notflag;
    873 
    874 	if (fio->stat & FIO_RDGONE || fio->stat & FIO_DEAD)
    875 		return;
    876 	fio->stat |= FIO_RDGONE;
    877 
    878 	if (fio->cur_in) {
    879 		if ((fio->cur_in->istat & ISTAT_DIRECT) == 0) {
    880 			puffs_framebuf_destroy(fio->cur_in);
    881 			fio->cur_in = NULL;
    882 		} else {
    883 			errnotify(pu, fio->cur_in, error);
    884 		}
    885 	}
    886 
    887 	while ((pufbuf = TAILQ_FIRST(&fio->res_qing)) != NULL) {
    888 		TAILQ_REMOVE(&fio->res_qing, pufbuf, pfb_entries);
    889 		errnotify(pu, pufbuf, error);
    890 	}
    891 
    892 	EV_SET(&kev, fio->io_fd, EVFILT_READ, EV_DELETE, 0, 0, 0);
    893 	(void) kevent(pu->pu_kq, &kev, 1, NULL, 0, NULL);
    894 
    895 	notflag = PUFFS_FBIO_READ;
    896 	if (fio->stat & FIO_WRGONE)
    897 		notflag |= PUFFS_FBIO_WRITE;
    898 
    899 	if (pu->pu_framectrl.fdnotfn)
    900 		pu->pu_framectrl.fdnotfn(pu, fio->io_fd, notflag);
    901 }
    902 
    903 void
    904 puffs_framev_writeclose(struct puffs_usermount *pu,
    905 	struct puffs_fctrl_io *fio, int error)
    906 {
    907 	struct puffs_framebuf *pufbuf;
    908 	struct kevent kev;
    909 	int notflag;
    910 
    911 	if (fio->stat & FIO_WRGONE || fio->stat & FIO_DEAD)
    912 		return;
    913 	fio->stat |= FIO_WRGONE;
    914 
    915 	while ((pufbuf = TAILQ_FIRST(&fio->snd_qing)) != NULL) {
    916 		TAILQ_REMOVE(&fio->snd_qing, pufbuf, pfb_entries);
    917 		errnotify(pu, pufbuf, error);
    918 	}
    919 
    920 	EV_SET(&kev, fio->io_fd, EVFILT_WRITE, EV_DELETE, 0, 0, 0);
    921 	(void) kevent(pu->pu_kq, &kev, 1, NULL, 0, NULL);
    922 
    923 	notflag = PUFFS_FBIO_WRITE;
    924 	if (fio->stat & FIO_RDGONE)
    925 		notflag |= PUFFS_FBIO_READ;
    926 
    927 	if (pu->pu_framectrl.fdnotfn)
    928 		pu->pu_framectrl.fdnotfn(pu, fio->io_fd, notflag);
    929 }
    930 
    931 static int
    932 removefio(struct puffs_usermount *pu, struct puffs_fctrl_io *fio, int error)
    933 {
    934 	struct puffs_framectrl *pfctrl = &pu->pu_framectrl;
    935 	struct puffs_fbevent *fbevp;
    936 
    937 	LIST_REMOVE(fio, fio_entries);
    938 	if (pu->pu_state & PU_INLOOP) {
    939 		puffs_framev_readclose(pu, fio, error);
    940 		puffs_framev_writeclose(pu, fio, error);
    941 	}
    942 
    943 	while ((fbevp = LIST_FIRST(&fio->ev_qing)) != NULL) {
    944 		fbevp->rv = error;
    945 		LIST_REMOVE(fbevp, pfe_entries);
    946 		puffs_goto(fbevp->pcc);
    947 	}
    948 
    949 	/* don't bother with realloc */
    950 	pfctrl->nfds--;
    951 
    952 	/* don't free us yet, might have some references in event arrays */
    953 	fio->stat |= FIO_DEAD;
    954 	LIST_INSERT_HEAD(&pfctrl->fb_ios_rmlist, fio, fio_entries);
    955 
    956 	return 0;
    957 
    958 }
    959 
    960 int
    961 puffs_framev_removefd(struct puffs_usermount *pu, int fd, int error)
    962 {
    963 	struct puffs_fctrl_io *fio;
    964 
    965 	fio = getfiobyfd(pu, fd);
    966 	if (fio == NULL) {
    967 		errno = ENXIO;
    968 		return -1;
    969 	}
    970 
    971 	return removefio(pu, fio, error ? error : ECONNRESET);
    972 }
    973 
    974 void
    975 puffs_framev_removeonclose(struct puffs_usermount *pu, int fd, int what)
    976 {
    977 
    978 	if (what == (PUFFS_FBIO_READ | PUFFS_FBIO_WRITE))
    979 		(void) puffs_framev_removefd(pu, fd, ECONNRESET);
    980 }
    981 
    982 void
    983 puffs_framev_unmountonclose(struct puffs_usermount *pu, int fd, int what)
    984 {
    985 
    986 	/* XXX & X: unmount is non-sensible */
    987 	puffs_framev_removeonclose(pu, fd, what);
    988 	if (what == (PUFFS_FBIO_READ | PUFFS_FBIO_WRITE))
    989 		PU_SETSTATE(pu, PUFFS_STATE_UNMOUNTED);
    990 }
    991 
    992 void
    993 puffs_framev_init(struct puffs_usermount *pu,
    994 	puffs_framev_readframe_fn rfb, puffs_framev_writeframe_fn wfb,
    995 	puffs_framev_cmpframe_fn cmpfb, puffs_framev_fdnotify_fn fdnotfn)
    996 {
    997 	struct puffs_framectrl *pfctrl;
    998 
    999 	pfctrl = &pu->pu_framectrl;
   1000 	pfctrl->rfb = rfb;
   1001 	pfctrl->wfb = wfb;
   1002 	pfctrl->cmpfb = cmpfb;
   1003 	pfctrl->fdnotfn = fdnotfn;
   1004 }
   1005 
   1006 void
   1007 puffs_framev_exit(struct puffs_usermount *pu)
   1008 {
   1009 	struct puffs_framectrl *pfctrl = &pu->pu_framectrl;
   1010 	struct puffs_fctrl_io *fio;
   1011 
   1012 	while ((fio = LIST_FIRST(&pfctrl->fb_ios)) != NULL)
   1013 		removefio(pu, fio, ENXIO);
   1014 	free(pfctrl->evs);
   1015 
   1016 	/* closing pu->pu_kq takes care of puffsfd */
   1017 }
   1018