Home | History | Annotate | Line # | Download | only in libpuffs
framebuf.c revision 1.5
      1  1.5  pooka /*	$NetBSD: framebuf.c,v 1.5 2007/05/11 16:22:38 pooka Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*
      4  1.1  pooka  * Copyright (c) 2007  Antti Kantee.  All Rights Reserved.
      5  1.1  pooka  *
      6  1.1  pooka  * Development of this software was supported by the
      7  1.1  pooka  * Finnish Cultural Foundation.
      8  1.1  pooka  *
      9  1.1  pooka  * Redistribution and use in source and binary forms, with or without
     10  1.1  pooka  * modification, are permitted provided that the following conditions
     11  1.1  pooka  * are met:
     12  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     13  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     14  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     17  1.1  pooka  *
     18  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19  1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.1  pooka  * SUCH DAMAGE.
     29  1.1  pooka  */
     30  1.1  pooka 
     31  1.1  pooka #include <sys/cdefs.h>
     32  1.1  pooka #if !defined(lint)
     33  1.5  pooka __RCSID("$NetBSD: framebuf.c,v 1.5 2007/05/11 16:22:38 pooka Exp $");
     34  1.1  pooka #endif /* !lint */
     35  1.1  pooka 
     36  1.1  pooka #include <sys/types.h>
     37  1.1  pooka #include <sys/queue.h>
     38  1.1  pooka 
     39  1.1  pooka #include <assert.h>
     40  1.1  pooka #include <errno.h>
     41  1.1  pooka #include <poll.h>
     42  1.1  pooka #include <puffs.h>
     43  1.1  pooka #include <stdlib.h>
     44  1.5  pooka #include <unistd.h>
     45  1.1  pooka 
     46  1.1  pooka #include "puffs_priv.h"
     47  1.1  pooka 
     48  1.1  pooka struct puffs_framebuf {
     49  1.1  pooka 	struct puffs_cc *pcc;	/* pcc to continue with */
     50  1.1  pooka 	/* OR */
     51  1.1  pooka 	puffs_framebuf_cb fcb;	/* non-blocking callback */
     52  1.1  pooka 	void *fcb_arg;		/* argument for previous */
     53  1.1  pooka 
     54  1.1  pooka 	uint8_t *buf;		/* buffer base */
     55  1.1  pooka 	size_t len;		/* total length */
     56  1.1  pooka 
     57  1.1  pooka 	size_t offset;		/* cursor, telloff() */
     58  1.1  pooka 	size_t maxoff;		/* maximum offset for data, tellsize() */
     59  1.1  pooka 
     60  1.1  pooka 	volatile int rv;	/* errno value for pcc framebufs */
     61  1.1  pooka 
     62  1.1  pooka 	int	istat;
     63  1.1  pooka 
     64  1.1  pooka 	TAILQ_ENTRY(puffs_framebuf) pfb_entries;
     65  1.1  pooka };
     66  1.1  pooka #define ISTAT_NODESTROY	0x01	/* indestructible by framebuf_destroy() */
     67  1.1  pooka #define ISTAT_INTERNAL	0x02	/* never leaves library			*/
     68  1.1  pooka #define ISTAT_NOREPLY	0x04	/* nuke after sending 			*/
     69  1.1  pooka 
     70  1.5  pooka struct puffs_fctrl_io {
     71  1.5  pooka 	int io_fd;
     72  1.5  pooka 	int wrstat;
     73  1.5  pooka 
     74  1.5  pooka 	struct puffs_framebuf *cur_in;
     75  1.5  pooka 
     76  1.5  pooka 	TAILQ_HEAD(, puffs_framebuf) snd_qing;	/* queueing to be sent */
     77  1.5  pooka 	TAILQ_HEAD(, puffs_framebuf) res_qing;	/* q'ing for rescue */
     78  1.5  pooka 
     79  1.5  pooka 	LIST_ENTRY(puffs_fctrl_io) fio_entries;
     80  1.5  pooka };
     81  1.5  pooka #define EN_WRITE(fio) (fio->wrstat == 0 && !TAILQ_EMPTY(&fio->snd_qing))
     82  1.5  pooka #define RM_WRITE(fio) (fio->wrstat == 1 && TAILQ_EMPTY(&fio->snd_qing))
     83  1.5  pooka 
     84  1.1  pooka struct puffs_framectrl {
     85  1.1  pooka 	puffs_framebuf_readframe_fn rfb;
     86  1.1  pooka 	puffs_framebuf_writeframe_fn wfb;
     87  1.1  pooka 	puffs_framebuf_respcmp_fn cmpfb;
     88  1.1  pooka 
     89  1.5  pooka 	struct kevent *evs;
     90  1.5  pooka 	struct kevent *ch_evs;
     91  1.5  pooka 	size_t nevs;
     92  1.5  pooka 	int kq;
     93  1.1  pooka 
     94  1.5  pooka 	LIST_HEAD(, puffs_fctrl_io) fb_ios;
     95  1.1  pooka };
     96  1.1  pooka 
     97  1.1  pooka #define PUFBUF_INCRALLOC 65536	/* 64k ought to be enough for anyone */
     98  1.1  pooka #define PUFBUF_REMAIN(p) (p->len - p->offset)
     99  1.1  pooka 
    100  1.5  pooka static struct puffs_fctrl_io *
    101  1.5  pooka getfiobyfd(struct puffs_usermount *pu, int fd)
    102  1.5  pooka {
    103  1.5  pooka 	struct puffs_fctrl_io *fio;
    104  1.5  pooka 
    105  1.5  pooka 	LIST_FOREACH(fio, &pu->pu_framectrl->fb_ios, fio_entries)
    106  1.5  pooka 		if (fio->io_fd == fd)
    107  1.5  pooka 			return fio;
    108  1.5  pooka 	return NULL;
    109  1.5  pooka }
    110  1.5  pooka 
    111  1.1  pooka struct puffs_framebuf *
    112  1.1  pooka puffs_framebuf_make()
    113  1.1  pooka {
    114  1.1  pooka 	struct puffs_framebuf *pufbuf;
    115  1.1  pooka 
    116  1.1  pooka 	pufbuf = malloc(sizeof(struct puffs_framebuf));
    117  1.1  pooka 	if (pufbuf == NULL)
    118  1.1  pooka 		return NULL;
    119  1.1  pooka 	memset(pufbuf, 0, sizeof(struct puffs_framebuf));
    120  1.1  pooka 
    121  1.1  pooka 	pufbuf->buf = malloc(PUFBUF_INCRALLOC);
    122  1.1  pooka 	pufbuf->len = PUFBUF_INCRALLOC;
    123  1.1  pooka 	if (pufbuf->buf == NULL) {
    124  1.1  pooka 		free(pufbuf);
    125  1.1  pooka 		return NULL;
    126  1.1  pooka 	}
    127  1.1  pooka 
    128  1.1  pooka 	puffs_framebuf_recycle(pufbuf);
    129  1.1  pooka 	return pufbuf;
    130  1.1  pooka }
    131  1.1  pooka 
    132  1.1  pooka void
    133  1.1  pooka puffs_framebuf_destroy(struct puffs_framebuf *pufbuf)
    134  1.1  pooka {
    135  1.1  pooka 
    136  1.1  pooka 	assert((pufbuf->istat & ISTAT_NODESTROY) == 0);
    137  1.1  pooka 
    138  1.1  pooka 	free(pufbuf->buf);
    139  1.1  pooka 	free(pufbuf);
    140  1.1  pooka }
    141  1.1  pooka 
    142  1.1  pooka void
    143  1.1  pooka puffs_framebuf_recycle(struct puffs_framebuf *pufbuf)
    144  1.1  pooka {
    145  1.1  pooka 
    146  1.1  pooka 	assert((pufbuf->istat & ISTAT_NODESTROY) == 0);
    147  1.1  pooka 
    148  1.1  pooka 	pufbuf->offset = 0;
    149  1.1  pooka 	pufbuf->maxoff = 0;
    150  1.1  pooka 	pufbuf->istat = 0;
    151  1.1  pooka }
    152  1.1  pooka 
    153  1.1  pooka static int
    154  1.1  pooka reservespace(struct puffs_framebuf *pufbuf, size_t off, size_t wantsize)
    155  1.1  pooka {
    156  1.1  pooka 	size_t incr;
    157  1.1  pooka 	void *nd;
    158  1.1  pooka 
    159  1.1  pooka 	if (off <= pufbuf->len && pufbuf->len - off >= wantsize)
    160  1.1  pooka 		return 0;
    161  1.1  pooka 
    162  1.1  pooka 	for (incr = PUFBUF_INCRALLOC;
    163  1.1  pooka 	    pufbuf->len + incr < off + wantsize;
    164  1.1  pooka 	    incr += PUFBUF_INCRALLOC)
    165  1.1  pooka 		continue;
    166  1.1  pooka 
    167  1.1  pooka 	nd = realloc(pufbuf->buf, pufbuf->offset + incr);
    168  1.1  pooka 	if (nd == NULL)
    169  1.1  pooka 		return -1;
    170  1.1  pooka 
    171  1.1  pooka 	pufbuf->buf = nd;
    172  1.1  pooka 	pufbuf->len += incr;
    173  1.1  pooka 
    174  1.1  pooka 	return 0;
    175  1.1  pooka }
    176  1.1  pooka 
    177  1.1  pooka int
    178  1.1  pooka puffs_framebuf_reserve_space(struct puffs_framebuf *pufbuf, size_t wantsize)
    179  1.1  pooka {
    180  1.1  pooka 
    181  1.1  pooka 	return reservespace(pufbuf, pufbuf->offset, wantsize);
    182  1.1  pooka }
    183  1.1  pooka 
    184  1.1  pooka int
    185  1.1  pooka puffs_framebuf_putdata(struct puffs_framebuf *pufbuf,
    186  1.1  pooka 	const void *data, size_t dlen)
    187  1.1  pooka {
    188  1.1  pooka 
    189  1.1  pooka 	if (PUFBUF_REMAIN(pufbuf) < dlen)
    190  1.1  pooka 		if (puffs_framebuf_reserve_space(pufbuf, dlen) == -1)
    191  1.1  pooka 			return -1;
    192  1.1  pooka 
    193  1.1  pooka 	memcpy(pufbuf->buf + pufbuf->offset, data, dlen);
    194  1.1  pooka 	pufbuf->offset += dlen;
    195  1.1  pooka 
    196  1.1  pooka 	if (pufbuf->offset > pufbuf->maxoff)
    197  1.1  pooka 		pufbuf->maxoff = pufbuf->offset;
    198  1.1  pooka 
    199  1.1  pooka 	return 0;
    200  1.1  pooka }
    201  1.1  pooka 
    202  1.1  pooka int
    203  1.1  pooka puffs_framebuf_putdata_atoff(struct puffs_framebuf *pufbuf, size_t offset,
    204  1.1  pooka 	const void *data, size_t dlen)
    205  1.1  pooka {
    206  1.1  pooka 
    207  1.1  pooka 	if (reservespace(pufbuf, offset, dlen) == -1)
    208  1.1  pooka 		return -1;
    209  1.1  pooka 
    210  1.1  pooka 	memcpy(pufbuf->buf + offset, data, dlen);
    211  1.1  pooka 
    212  1.1  pooka 	if (offset + dlen > pufbuf->maxoff)
    213  1.1  pooka 		pufbuf->maxoff = offset + dlen;
    214  1.1  pooka 
    215  1.1  pooka 	return 0;
    216  1.1  pooka }
    217  1.1  pooka 
    218  1.1  pooka int
    219  1.1  pooka puffs_framebuf_getdata(struct puffs_framebuf *pufbuf, void *data, size_t dlen)
    220  1.1  pooka {
    221  1.1  pooka 
    222  1.1  pooka 	if (pufbuf->maxoff < pufbuf->offset + dlen) {
    223  1.1  pooka 		errno = ENOBUFS;
    224  1.1  pooka 		return -1;
    225  1.1  pooka 	}
    226  1.1  pooka 
    227  1.1  pooka 	memcpy(data, pufbuf->buf + pufbuf->offset, dlen);
    228  1.1  pooka 	pufbuf->offset += dlen;
    229  1.1  pooka 
    230  1.1  pooka 	return 0;
    231  1.1  pooka }
    232  1.1  pooka 
    233  1.1  pooka int
    234  1.1  pooka puffs_framebuf_getdata_atoff(struct puffs_framebuf *pufbuf, size_t offset,
    235  1.1  pooka 	void *data, size_t dlen)
    236  1.1  pooka {
    237  1.1  pooka 
    238  1.1  pooka 	if (pufbuf->maxoff < offset + dlen) {
    239  1.1  pooka 		errno = ENOBUFS;
    240  1.1  pooka 		return -1;
    241  1.1  pooka 	}
    242  1.1  pooka 
    243  1.1  pooka 	memcpy(data, pufbuf->buf + offset, dlen);
    244  1.1  pooka 	return 0;
    245  1.1  pooka }
    246  1.1  pooka 
    247  1.1  pooka size_t
    248  1.1  pooka puffs_framebuf_telloff(struct puffs_framebuf *pufbuf)
    249  1.1  pooka {
    250  1.1  pooka 
    251  1.1  pooka 	return pufbuf->offset;
    252  1.1  pooka }
    253  1.1  pooka 
    254  1.1  pooka size_t
    255  1.1  pooka puffs_framebuf_tellsize(struct puffs_framebuf *pufbuf)
    256  1.1  pooka {
    257  1.1  pooka 
    258  1.1  pooka 	return pufbuf->maxoff;
    259  1.1  pooka }
    260  1.1  pooka 
    261  1.3  pooka size_t
    262  1.3  pooka puffs_framebuf_remaining(struct puffs_framebuf *pufbuf)
    263  1.3  pooka {
    264  1.3  pooka 
    265  1.3  pooka 	return puffs_framebuf_tellsize(pufbuf) - puffs_framebuf_telloff(pufbuf);
    266  1.3  pooka }
    267  1.3  pooka 
    268  1.1  pooka int
    269  1.1  pooka puffs_framebuf_seekset(struct puffs_framebuf *pufbuf, size_t newoff)
    270  1.1  pooka {
    271  1.1  pooka 
    272  1.1  pooka 	if (reservespace(pufbuf, newoff, 0) == -1)
    273  1.1  pooka 		return -1;
    274  1.1  pooka 
    275  1.1  pooka 	pufbuf->offset = newoff;
    276  1.1  pooka 	return 0;
    277  1.1  pooka }
    278  1.1  pooka 
    279  1.1  pooka int
    280  1.1  pooka puffs_framebuf_getwindow(struct puffs_framebuf *pufbuf, size_t winoff,
    281  1.1  pooka 	void **data, size_t *dlen)
    282  1.1  pooka {
    283  1.1  pooka 	size_t winlen;
    284  1.1  pooka 
    285  1.1  pooka #ifdef WINTESTING
    286  1.1  pooka 	winlen = MIN(*dlen, 32);
    287  1.1  pooka #else
    288  1.1  pooka 	winlen = *dlen;
    289  1.1  pooka #endif
    290  1.1  pooka 
    291  1.1  pooka 	if (reservespace(pufbuf, winoff, winlen) == -1)
    292  1.1  pooka 		return -1;
    293  1.1  pooka 
    294  1.1  pooka 	*data = pufbuf->buf + winoff;
    295  1.1  pooka 	if (pufbuf->maxoff < winoff + winlen)
    296  1.1  pooka 		pufbuf->maxoff = winoff + winlen;
    297  1.1  pooka 
    298  1.1  pooka 	return 0;
    299  1.1  pooka }
    300  1.1  pooka 
    301  1.1  pooka int
    302  1.5  pooka puffs_framebuf_enqueue_cc(struct puffs_cc *pcc, int fd,
    303  1.5  pooka 	struct puffs_framebuf *pufbuf)
    304  1.1  pooka {
    305  1.1  pooka 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    306  1.5  pooka 	struct puffs_fctrl_io *fio;
    307  1.5  pooka 
    308  1.5  pooka 	fio = getfiobyfd(pu, fd);
    309  1.5  pooka 	if (fio == NULL) {
    310  1.5  pooka 		errno = EINVAL;
    311  1.5  pooka 		return -1;
    312  1.5  pooka 	}
    313  1.1  pooka 
    314  1.1  pooka 	pufbuf->pcc = pcc;
    315  1.1  pooka 	pufbuf->fcb = NULL;
    316  1.1  pooka 	pufbuf->fcb_arg = NULL;
    317  1.1  pooka 
    318  1.1  pooka 	pufbuf->offset = 0;
    319  1.1  pooka 	pufbuf->istat |= ISTAT_NODESTROY;
    320  1.1  pooka 
    321  1.5  pooka 	TAILQ_INSERT_TAIL(&fio->snd_qing, pufbuf, pfb_entries);
    322  1.1  pooka 
    323  1.1  pooka 	puffs_cc_yield(pcc);
    324  1.1  pooka 	if (pufbuf->rv) {
    325  1.1  pooka 		errno = pufbuf->rv;
    326  1.1  pooka 		return -1;
    327  1.1  pooka 	}
    328  1.1  pooka 
    329  1.1  pooka 	return 0;
    330  1.1  pooka }
    331  1.1  pooka 
    332  1.5  pooka int
    333  1.5  pooka puffs_framebuf_enqueue_cb(struct puffs_usermount *pu, int fd,
    334  1.1  pooka 	struct puffs_framebuf *pufbuf, puffs_framebuf_cb fcb, void *arg)
    335  1.1  pooka {
    336  1.5  pooka 	struct puffs_fctrl_io *fio;
    337  1.5  pooka 
    338  1.5  pooka 	fio = getfiobyfd(pu, fd);
    339  1.5  pooka 	if (fio == NULL) {
    340  1.5  pooka 		errno = EINVAL;
    341  1.5  pooka 		return -1;
    342  1.5  pooka 	}
    343  1.1  pooka 
    344  1.1  pooka 	pufbuf->pcc = NULL;
    345  1.1  pooka 	pufbuf->fcb = fcb;
    346  1.1  pooka 	pufbuf->fcb_arg = arg;
    347  1.1  pooka 
    348  1.1  pooka 	pufbuf->offset = 0;
    349  1.1  pooka 	pufbuf->istat |= ISTAT_NODESTROY;
    350  1.1  pooka 
    351  1.5  pooka 	TAILQ_INSERT_TAIL(&fio->snd_qing, pufbuf, pfb_entries);
    352  1.5  pooka 
    353  1.5  pooka 	return 0;
    354  1.1  pooka }
    355  1.1  pooka 
    356  1.5  pooka int
    357  1.5  pooka puffs_framebuf_enqueue_justsend(struct puffs_usermount *pu, int fd,
    358  1.1  pooka 	struct puffs_framebuf *pufbuf, int reply)
    359  1.1  pooka {
    360  1.5  pooka 	struct puffs_fctrl_io *fio;
    361  1.5  pooka 
    362  1.5  pooka 	fio = getfiobyfd(pu, fd);
    363  1.5  pooka 	if (fio == NULL) {
    364  1.5  pooka 		errno = EINVAL;
    365  1.5  pooka 		return -1;
    366  1.5  pooka 	}
    367  1.1  pooka 
    368  1.1  pooka 	pufbuf->pcc = NULL;
    369  1.1  pooka 	pufbuf->fcb = NULL;
    370  1.1  pooka 	pufbuf->fcb_arg = NULL;
    371  1.1  pooka 
    372  1.1  pooka 	pufbuf->offset = 0;
    373  1.1  pooka 	pufbuf->istat |= ISTAT_NODESTROY;
    374  1.1  pooka 	if (!reply)
    375  1.1  pooka 		pufbuf->istat |= ISTAT_NOREPLY;
    376  1.1  pooka 
    377  1.5  pooka 	TAILQ_INSERT_TAIL(&fio->snd_qing, pufbuf, pfb_entries);
    378  1.5  pooka 
    379  1.5  pooka 	return 0;
    380  1.1  pooka }
    381  1.1  pooka 
    382  1.1  pooka static struct puffs_framebuf *
    383  1.1  pooka findbuf(struct puffs_usermount *pu, struct puffs_framectrl *fctrl,
    384  1.5  pooka 	struct puffs_fctrl_io *fio, struct puffs_framebuf *findme)
    385  1.1  pooka {
    386  1.1  pooka 	struct puffs_framebuf *cand;
    387  1.1  pooka 
    388  1.5  pooka 	TAILQ_FOREACH(cand, &fio->res_qing, pfb_entries)
    389  1.1  pooka 		if (fctrl->cmpfb(pu, findme, cand))
    390  1.1  pooka 			break;
    391  1.1  pooka 
    392  1.1  pooka 	if (cand == NULL)
    393  1.1  pooka 		return NULL;
    394  1.1  pooka 
    395  1.5  pooka 	TAILQ_REMOVE(&fio->res_qing, cand, pfb_entries);
    396  1.1  pooka 	return cand;
    397  1.1  pooka }
    398  1.1  pooka 
    399  1.1  pooka static void
    400  1.1  pooka moveinfo(struct puffs_framebuf *from, struct puffs_framebuf *to)
    401  1.1  pooka {
    402  1.1  pooka 
    403  1.1  pooka 	assert(from->istat & ISTAT_INTERNAL);
    404  1.1  pooka 
    405  1.1  pooka 	/* migrate buffer */
    406  1.1  pooka 	free(to->buf);
    407  1.1  pooka 	to->buf = from->buf;
    408  1.1  pooka 	from->buf = NULL;
    409  1.1  pooka 
    410  1.1  pooka 	/* migrate buffer info */
    411  1.1  pooka 	to->len = from->len;
    412  1.1  pooka 	to->offset = from->offset;
    413  1.1  pooka 	to->maxoff = from->maxoff;
    414  1.1  pooka }
    415  1.1  pooka 
    416  1.1  pooka static int
    417  1.1  pooka handle_input(struct puffs_usermount *pu, struct puffs_framectrl *fctrl,
    418  1.5  pooka 	struct puffs_fctrl_io *fio, struct puffs_putreq *ppr)
    419  1.1  pooka {
    420  1.1  pooka 	struct puffs_framebuf *pufbuf, *appbuf;
    421  1.1  pooka 	int rv, complete;
    422  1.1  pooka 
    423  1.1  pooka 	for (;;) {
    424  1.5  pooka 		if ((pufbuf = fio->cur_in) == NULL) {
    425  1.1  pooka 			pufbuf = puffs_framebuf_make();
    426  1.1  pooka 			if (pufbuf == NULL)
    427  1.1  pooka 				return -1;
    428  1.1  pooka 			pufbuf->istat |= ISTAT_INTERNAL;
    429  1.5  pooka 			fio->cur_in = pufbuf;
    430  1.1  pooka 		}
    431  1.1  pooka 
    432  1.1  pooka 		complete = 0;
    433  1.5  pooka 		rv = fctrl->rfb(pu, pufbuf, fio->io_fd, &complete);
    434  1.1  pooka 
    435  1.1  pooka 		/* error */
    436  1.1  pooka 		if (rv) {
    437  1.1  pooka 			errno = rv;
    438  1.1  pooka 			return -1;
    439  1.1  pooka 		}
    440  1.1  pooka 
    441  1.1  pooka 		/* partial read, come back to fight another day */
    442  1.1  pooka 		if (complete == 0)
    443  1.1  pooka 			break;
    444  1.1  pooka 
    445  1.1  pooka 		/* else: full read, process */
    446  1.1  pooka 
    447  1.5  pooka 		appbuf = findbuf(pu, fctrl, fio, pufbuf);
    448  1.1  pooka 		if (appbuf == NULL) {
    449  1.1  pooka 			errno = ENOMSG;
    450  1.1  pooka 			return -1;
    451  1.1  pooka 		}
    452  1.1  pooka 
    453  1.1  pooka 		appbuf->istat &= ~ISTAT_NODESTROY;
    454  1.1  pooka 		moveinfo(pufbuf, appbuf);
    455  1.1  pooka 		if (appbuf->pcc) {
    456  1.1  pooka 			puffs_docc(appbuf->pcc, ppr);
    457  1.1  pooka 		} else if (appbuf->fcb) {
    458  1.1  pooka 			appbuf->fcb(pu, appbuf, appbuf->fcb_arg);
    459  1.1  pooka 		} else {
    460  1.1  pooka 			puffs_framebuf_destroy(appbuf);
    461  1.1  pooka 		}
    462  1.1  pooka 		puffs_framebuf_destroy(pufbuf);
    463  1.1  pooka 
    464  1.1  pooka 		/* hopeless romantics, here we go again */
    465  1.5  pooka 		fio->cur_in = NULL;
    466  1.1  pooka 	}
    467  1.1  pooka 
    468  1.1  pooka 	return rv;
    469  1.1  pooka }
    470  1.1  pooka 
    471  1.1  pooka static int
    472  1.5  pooka handle_output(struct puffs_usermount *pu, struct puffs_framectrl *fctrl,
    473  1.5  pooka 	struct puffs_fctrl_io *fio)
    474  1.1  pooka {
    475  1.1  pooka 	struct puffs_framebuf *pufbuf, *pufbuf_next;
    476  1.1  pooka 	int rv, complete;
    477  1.1  pooka 
    478  1.5  pooka 	for (pufbuf = TAILQ_FIRST(&fio->snd_qing);
    479  1.1  pooka 	    pufbuf;
    480  1.1  pooka 	    pufbuf = pufbuf_next) {
    481  1.1  pooka 		complete = 0;
    482  1.1  pooka 		pufbuf_next = TAILQ_NEXT(pufbuf, pfb_entries);
    483  1.5  pooka 		rv = fctrl->wfb(pu, pufbuf, fio->io_fd, &complete);
    484  1.1  pooka 
    485  1.1  pooka 		if (rv) {
    486  1.5  pooka 			TAILQ_REMOVE(&fio->snd_qing, pufbuf, pfb_entries);
    487  1.1  pooka 			pufbuf->rv = rv;
    488  1.1  pooka 			errno = rv;
    489  1.1  pooka 			return -1;
    490  1.1  pooka 		}
    491  1.1  pooka 
    492  1.1  pooka 		/* partial write */
    493  1.1  pooka 		if (complete == 0)
    494  1.1  pooka 			return 0;
    495  1.1  pooka 
    496  1.1  pooka 		/* else, complete write */
    497  1.5  pooka 		TAILQ_REMOVE(&fio->snd_qing, pufbuf, pfb_entries);
    498  1.1  pooka 
    499  1.1  pooka 		if ((pufbuf->istat & ISTAT_NOREPLY) == 0) {
    500  1.5  pooka 			TAILQ_INSERT_TAIL(&fio->res_qing, pufbuf,
    501  1.1  pooka 			    pfb_entries);
    502  1.1  pooka 		} else {
    503  1.1  pooka 			pufbuf->istat &= ~ISTAT_NODESTROY;
    504  1.1  pooka 			puffs_framebuf_destroy(pufbuf);
    505  1.1  pooka 		}
    506  1.1  pooka 	}
    507  1.1  pooka 
    508  1.1  pooka 	return 0;
    509  1.1  pooka }
    510  1.1  pooka 
    511  1.1  pooka int
    512  1.5  pooka puffs_framebuf_addfd(struct puffs_usermount *pu, int fd)
    513  1.5  pooka {
    514  1.5  pooka 	struct puffs_framectrl *pfctrl = pu->pu_framectrl;
    515  1.5  pooka 	struct puffs_fctrl_io *fio;
    516  1.5  pooka 	struct kevent kev[2];
    517  1.5  pooka 	struct kevent *newevs;
    518  1.5  pooka 	int rv;
    519  1.5  pooka 
    520  1.5  pooka 	newevs = realloc(pfctrl->evs, (2*pfctrl->nevs+1)*sizeof(struct kevent));
    521  1.5  pooka 	if (newevs == NULL)
    522  1.5  pooka 		return -1;
    523  1.5  pooka 	pfctrl->evs = newevs;
    524  1.5  pooka 
    525  1.5  pooka 	newevs = realloc(pfctrl->ch_evs, pfctrl->nevs * sizeof(struct kevent));
    526  1.5  pooka 	if (newevs == NULL)
    527  1.5  pooka 		return -1;
    528  1.5  pooka 	pfctrl->ch_evs = newevs;
    529  1.5  pooka 
    530  1.5  pooka 	fio = malloc(sizeof(struct puffs_fctrl_io));
    531  1.5  pooka 	if (fio == NULL)
    532  1.5  pooka 		return -1;
    533  1.5  pooka 
    534  1.5  pooka 	EV_SET(&kev[0], fd, EVFILT_READ, EV_ADD, 0, 0, (intptr_t)fio);
    535  1.5  pooka 	EV_SET(&kev[1], fd, EVFILT_WRITE, EV_ADD|EV_DISABLE, 0,0,(intptr_t)fio);
    536  1.5  pooka 	rv = kevent(pfctrl->kq, kev, 2, NULL, 0, NULL);
    537  1.5  pooka 	if (rv == -1) {
    538  1.5  pooka 		free(fio);
    539  1.5  pooka 		return -1;
    540  1.5  pooka 	}
    541  1.5  pooka 
    542  1.5  pooka 	fio->io_fd = fd;
    543  1.5  pooka 	fio->cur_in = NULL;
    544  1.5  pooka 	fio->wrstat = 0;
    545  1.5  pooka 	TAILQ_INIT(&fio->snd_qing);
    546  1.5  pooka 	TAILQ_INIT(&fio->res_qing);
    547  1.5  pooka 
    548  1.5  pooka 	LIST_INSERT_HEAD(&pfctrl->fb_ios, fio, fio_entries);
    549  1.5  pooka 	pfctrl->nevs++;
    550  1.5  pooka 
    551  1.5  pooka 	return 0;
    552  1.5  pooka }
    553  1.5  pooka 
    554  1.5  pooka static int
    555  1.5  pooka removefio(struct puffs_usermount *pu, struct puffs_fctrl_io *fio)
    556  1.5  pooka {
    557  1.5  pooka 	struct puffs_framectrl *pfctrl = pu->pu_framectrl;
    558  1.5  pooka 	struct puffs_framebuf *pufbuf;
    559  1.5  pooka 	struct kevent kev[2];
    560  1.5  pooka 
    561  1.5  pooka 	/* found, now remove */
    562  1.5  pooka 	EV_SET(&kev[0], fio->io_fd, EVFILT_READ, EV_DELETE, 0, 0, 0);
    563  1.5  pooka 	EV_SET(&kev[1], fio->io_fd, EVFILT_WRITE, EV_DELETE, 0, 0, 0);
    564  1.5  pooka 	(void) kevent(pfctrl->kq, kev, 2, NULL, 0, NULL);
    565  1.5  pooka 
    566  1.5  pooka 	LIST_REMOVE(fio, fio_entries);
    567  1.5  pooka 
    568  1.5  pooka 	/* free buffers */
    569  1.5  pooka 	while ((pufbuf = TAILQ_FIRST(&fio->snd_qing)) != NULL) {
    570  1.5  pooka 		TAILQ_REMOVE(&fio->snd_qing, pufbuf, pfb_entries);
    571  1.5  pooka 		puffs_framebuf_destroy(pufbuf);
    572  1.5  pooka 	}
    573  1.5  pooka 	while ((pufbuf = TAILQ_FIRST(&fio->res_qing)) != NULL) {
    574  1.5  pooka 		TAILQ_REMOVE(&fio->res_qing, pufbuf, pfb_entries);
    575  1.5  pooka 		puffs_framebuf_destroy(pufbuf);
    576  1.5  pooka 	}
    577  1.5  pooka 	if (fio->cur_in)
    578  1.5  pooka 		puffs_framebuf_destroy(fio->cur_in);
    579  1.5  pooka 	free(fio);
    580  1.5  pooka 
    581  1.5  pooka 	/* don't bother with realloc */
    582  1.5  pooka 	pfctrl->nevs--;
    583  1.5  pooka 
    584  1.5  pooka 	return 0;
    585  1.5  pooka 
    586  1.5  pooka }
    587  1.5  pooka 
    588  1.5  pooka int
    589  1.5  pooka puffs_framebuf_removefd(struct puffs_usermount *pu, int fd)
    590  1.5  pooka {
    591  1.5  pooka 	struct puffs_fctrl_io *fio;
    592  1.5  pooka 
    593  1.5  pooka 	fio = getfiobyfd(pu, fd);
    594  1.5  pooka 	if (fio == NULL) {
    595  1.5  pooka 		errno = ENXIO;
    596  1.5  pooka 		return -1;
    597  1.5  pooka 	}
    598  1.5  pooka 
    599  1.5  pooka 	return removefio(pu, fio);
    600  1.5  pooka }
    601  1.5  pooka 
    602  1.5  pooka int
    603  1.5  pooka puffs_framebuf_eventloop(struct puffs_usermount *pu, int *io_fds, size_t nfds,
    604  1.1  pooka 	puffs_framebuf_readframe_fn rfb, puffs_framebuf_writeframe_fn wfb,
    605  1.1  pooka 	puffs_framebuf_respcmp_fn cmpfb,
    606  1.2  pooka 	puffs_framebuf_loop_fn lfb)
    607  1.1  pooka {
    608  1.1  pooka 	struct puffs_getreq *pgr = NULL;
    609  1.1  pooka 	struct puffs_putreq *ppr = NULL;
    610  1.1  pooka 	struct puffs_framectrl *pfctrl = NULL;
    611  1.5  pooka 	struct puffs_fctrl_io *fio;
    612  1.5  pooka 	struct kevent kev;
    613  1.5  pooka 	struct kevent *curev;
    614  1.5  pooka 	size_t nchanges;
    615  1.5  pooka 	int puffsfd, sverrno;
    616  1.5  pooka 	int ndone;
    617  1.1  pooka 
    618  1.1  pooka 	assert(puffs_getstate(pu) >= PUFFS_STATE_RUNNING);
    619  1.1  pooka 
    620  1.1  pooka 	pgr = puffs_req_makeget(pu, puffs_getmaxreqlen(pu), 0);
    621  1.1  pooka 	if (pgr == NULL)
    622  1.1  pooka 		goto out;
    623  1.1  pooka 
    624  1.1  pooka 	ppr = puffs_req_makeput(pu);
    625  1.1  pooka 	if (ppr == NULL)
    626  1.1  pooka 		goto out;
    627  1.1  pooka 
    628  1.1  pooka 	pfctrl = malloc(sizeof(struct puffs_framectrl));
    629  1.1  pooka 	if (pfctrl == NULL)
    630  1.1  pooka 		goto out;
    631  1.1  pooka 	pfctrl->rfb = rfb;
    632  1.1  pooka 	pfctrl->wfb = wfb;
    633  1.1  pooka 	pfctrl->cmpfb = cmpfb;
    634  1.5  pooka 	pfctrl->evs = malloc(sizeof(struct kevent));
    635  1.5  pooka 	if (pfctrl->evs == NULL)
    636  1.5  pooka 		goto out;
    637  1.5  pooka 	pfctrl->ch_evs = NULL;
    638  1.5  pooka 	pfctrl->nevs = 1;
    639  1.5  pooka 	pfctrl->kq = kqueue();
    640  1.5  pooka 	if (pfctrl->kq == -1)
    641  1.5  pooka 		goto out;
    642  1.5  pooka 	LIST_INIT(&pfctrl->fb_ios);
    643  1.1  pooka 	pu->pu_framectrl = pfctrl;
    644  1.1  pooka 
    645  1.5  pooka 	for (; nfds--; io_fds++)
    646  1.5  pooka 		if (puffs_framebuf_addfd(pu, *io_fds) == -1)
    647  1.5  pooka 			goto out;
    648  1.5  pooka 
    649  1.1  pooka 	puffsfd = puffs_getselectable(pu);
    650  1.5  pooka 	EV_SET(&kev, puffsfd, EVFILT_READ, EV_ADD, 0, 0, 0);
    651  1.5  pooka 	if (kevent(pfctrl->kq, &kev, 1, NULL, 0, NULL) == -1)
    652  1.5  pooka 		goto out;
    653  1.5  pooka 
    654  1.1  pooka 	while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
    655  1.1  pooka 		if (lfb)
    656  1.2  pooka 			lfb(pu);
    657  1.1  pooka 
    658  1.5  pooka 		/*
    659  1.5  pooka 		 * Build list of which to enable/disable in writecheck.
    660  1.5  pooka 		 * Don't bother worrying about O(n) for now.
    661  1.5  pooka 		 */
    662  1.5  pooka 		nchanges = 0;
    663  1.5  pooka 		LIST_FOREACH(fio, &pfctrl->fb_ios, fio_entries) {
    664  1.5  pooka 			/*
    665  1.5  pooka 			 * Try to write out everything to avoid the
    666  1.5  pooka 			 * need for enabling EVFILT_WRITE.  The likely
    667  1.5  pooka 			 * case is that we can fit everything into the
    668  1.5  pooka 			 * socket buffer.
    669  1.5  pooka 			 */
    670  1.5  pooka 			if (handle_output(pu, pfctrl, fio) == -1)
    671  1.5  pooka 				goto out;
    672  1.5  pooka 
    673  1.5  pooka 			assert((EN_WRITE(fio) && RM_WRITE(fio)) == 0);
    674  1.5  pooka 			if (EN_WRITE(fio)) {
    675  1.5  pooka 				EV_SET(&pfctrl->ch_evs[nchanges], fio->io_fd,
    676  1.5  pooka 				    EVFILT_WRITE, EV_ENABLE, 0, 0,
    677  1.5  pooka 				    (uintptr_t)fio);
    678  1.5  pooka 				fio->wrstat = 1; /* XXX: not before call */
    679  1.5  pooka 				nchanges++;
    680  1.5  pooka 			}
    681  1.5  pooka 			if (RM_WRITE(fio)) {
    682  1.5  pooka 				EV_SET(&pfctrl->ch_evs[nchanges], fio->io_fd,
    683  1.5  pooka 				    EVFILT_WRITE, EV_DISABLE, 0, 0,
    684  1.5  pooka 				    (uintptr_t)fio);
    685  1.5  pooka 				fio->wrstat = 0; /* XXX: not before call */
    686  1.5  pooka 				nchanges++;
    687  1.5  pooka 			}
    688  1.5  pooka 		}
    689  1.1  pooka 
    690  1.5  pooka 		ndone = kevent(pfctrl->kq, pfctrl->ch_evs, nchanges,
    691  1.5  pooka 		    pfctrl->evs, pfctrl->nevs, NULL);
    692  1.5  pooka 		if (ndone == -1)
    693  1.1  pooka 			goto out;
    694  1.1  pooka 
    695  1.5  pooka 		/* XXX: handle errors */
    696  1.1  pooka 
    697  1.5  pooka 		/* iterate over the results */
    698  1.5  pooka 		for (curev = pfctrl->evs; ndone--; curev++) {
    699  1.5  pooka 			/* get & possibly dispatch events from kernel */
    700  1.5  pooka 			if (curev->ident == puffsfd) {
    701  1.5  pooka 				if (puffs_req_handle(pgr, ppr, 0) == -1)
    702  1.5  pooka 					goto out;
    703  1.5  pooka 				continue;
    704  1.5  pooka 			}
    705  1.5  pooka 
    706  1.5  pooka 			if (curev->filter == EVFILT_READ) {
    707  1.5  pooka 				if (handle_input(pu, pfctrl,
    708  1.5  pooka 				    (void *)curev->udata, ppr) == -1)
    709  1.5  pooka 					goto out;
    710  1.5  pooka 			}
    711  1.5  pooka 
    712  1.5  pooka 			if (curev->filter == EVFILT_WRITE) {
    713  1.5  pooka 				if (handle_output(pu, pfctrl,
    714  1.5  pooka 				    (void *)curev->udata) == -1)
    715  1.5  pooka 					goto out;
    716  1.5  pooka 			}
    717  1.5  pooka 		}
    718  1.1  pooka 
    719  1.1  pooka 		/* stuff all replies from both of the above into kernel */
    720  1.1  pooka 		if (puffs_req_putput(ppr) == -1)
    721  1.1  pooka 			goto out;
    722  1.1  pooka 		puffs_req_resetput(ppr);
    723  1.1  pooka 	}
    724  1.1  pooka 	errno = 0;
    725  1.1  pooka 
    726  1.1  pooka  out:
    727  1.5  pooka 	/* store the real error for a while */
    728  1.5  pooka 	sverrno = errno;
    729  1.5  pooka 
    730  1.1  pooka 	if (pfctrl) {
    731  1.5  pooka 		while ((fio = LIST_FIRST(&pfctrl->fb_ios)) != NULL)
    732  1.5  pooka 			removefio(pu, fio);
    733  1.1  pooka 		pu->pu_framectrl = NULL;
    734  1.5  pooka 		free(pfctrl->evs);
    735  1.5  pooka 		close(pfctrl->kq); /* takes care of puffsfd */
    736  1.1  pooka 		free(pfctrl);
    737  1.1  pooka 	}
    738  1.1  pooka 
    739  1.1  pooka 	if (ppr)
    740  1.1  pooka 		puffs_req_destroyput(ppr);
    741  1.1  pooka 	if (pgr)
    742  1.1  pooka 		puffs_req_destroyget(pgr);
    743  1.1  pooka 
    744  1.5  pooka 	errno = sverrno;
    745  1.5  pooka 	if (errno)
    746  1.5  pooka 		return -1;
    747  1.5  pooka 	else
    748  1.5  pooka 		return 0;
    749  1.1  pooka }
    750