Home | History | Annotate | Line # | Download | only in libpuffs
puffs.c revision 1.77
      1 /*	$NetBSD: puffs.c,v 1.77 2007/11/26 12:20:21 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005, 2006, 2007  Antti Kantee.  All Rights Reserved.
      5  *
      6  * Development of this software was supported by the
      7  * Google Summer of Code program and the Ulla Tuominen Foundation.
      8  * The Google SoC project was mentored by Bill Studenmund.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #if !defined(lint)
     34 __RCSID("$NetBSD: puffs.c,v 1.77 2007/11/26 12:20:21 pooka Exp $");
     35 #endif /* !lint */
     36 
     37 #include <sys/param.h>
     38 #include <sys/mount.h>
     39 
     40 #include <assert.h>
     41 #include <err.h>
     42 #include <errno.h>
     43 #include <fcntl.h>
     44 #include <mntopts.h>
     45 #include <paths.h>
     46 #include <puffs.h>
     47 #include <stdio.h>
     48 #include <stdlib.h>
     49 #include <string.h>
     50 #include <syslog.h>
     51 #include <unistd.h>
     52 
     53 #include "puffs_priv.h"
     54 
     55 /* Most file systems want this for opts, so just give it to them */
     56 const struct mntopt puffsmopts[] = {
     57 	MOPT_STDOPTS,
     58 	PUFFSMOPT_STD,
     59 	MOPT_NULL,
     60 };
     61 
     62 #ifdef PUFFS_WITH_THREADS
     63 #include <pthread.h>
     64 pthread_mutex_t pu_lock = PTHREAD_MUTEX_INITIALIZER;
     65 #endif
     66 
     67 #define FILLOP(lower, upper)						\
     68 do {									\
     69 	if (pops->puffs_node_##lower)					\
     70 		opmask[PUFFS_VN_##upper] = 1;				\
     71 } while (/*CONSTCOND*/0)
     72 static void
     73 fillvnopmask(struct puffs_ops *pops, uint8_t *opmask)
     74 {
     75 
     76 	memset(opmask, 0, PUFFS_VN_MAX);
     77 
     78 	FILLOP(create,   CREATE);
     79 	FILLOP(mknod,    MKNOD);
     80 	FILLOP(open,     OPEN);
     81 	FILLOP(close,    CLOSE);
     82 	FILLOP(access,   ACCESS);
     83 	FILLOP(getattr,  GETATTR);
     84 	FILLOP(setattr,  SETATTR);
     85 	FILLOP(poll,     POLL); /* XXX: not ready in kernel */
     86 	FILLOP(mmap,     MMAP);
     87 	FILLOP(fsync,    FSYNC);
     88 	FILLOP(seek,     SEEK);
     89 	FILLOP(remove,   REMOVE);
     90 	FILLOP(link,     LINK);
     91 	FILLOP(rename,   RENAME);
     92 	FILLOP(mkdir,    MKDIR);
     93 	FILLOP(rmdir,    RMDIR);
     94 	FILLOP(symlink,  SYMLINK);
     95 	FILLOP(readdir,  READDIR);
     96 	FILLOP(readlink, READLINK);
     97 	FILLOP(reclaim,  RECLAIM);
     98 	FILLOP(inactive, INACTIVE);
     99 	FILLOP(print,    PRINT);
    100 	FILLOP(read,     READ);
    101 	FILLOP(write,    WRITE);
    102 }
    103 #undef FILLOP
    104 
    105 /*ARGSUSED*/
    106 static void
    107 puffs_defaulterror(struct puffs_usermount *pu, uint8_t type,
    108 	int error, const char *str, void *cookie)
    109 {
    110 
    111 	fprintf(stderr, "abort: type %d, error %d, cookie %p (%s)\n",
    112 	    type, error, cookie, str);
    113 	abort();
    114 }
    115 
    116 int
    117 puffs_getselectable(struct puffs_usermount *pu)
    118 {
    119 
    120 	return pu->pu_fd;
    121 }
    122 
    123 int
    124 puffs_setblockingmode(struct puffs_usermount *pu, int mode)
    125 {
    126 	int rv, x;
    127 
    128 	assert(puffs_getstate(pu) == PUFFS_STATE_RUNNING);
    129 
    130 	if (mode != PUFFSDEV_BLOCK && mode != PUFFSDEV_NONBLOCK) {
    131 		errno = EINVAL;
    132 		return -1;
    133 	}
    134 
    135 	x = mode;
    136 	rv = ioctl(pu->pu_fd, FIONBIO, &x);
    137 
    138 	if (rv == 0) {
    139 		if (mode == PUFFSDEV_BLOCK)
    140 			pu->pu_state &= ~PU_ASYNCFD;
    141 		else
    142 			pu->pu_state |= PU_ASYNCFD;
    143 	}
    144 
    145 	return rv;
    146 }
    147 
    148 int
    149 puffs_getstate(struct puffs_usermount *pu)
    150 {
    151 
    152 	return pu->pu_state & PU_STATEMASK;
    153 }
    154 
    155 void
    156 puffs_setstacksize(struct puffs_usermount *pu, size_t ss)
    157 {
    158 	int stackshift;
    159 
    160 	assert(puffs_getstate(pu) == PUFFS_STATE_BEFOREMOUNT);
    161 	stackshift = -1;
    162 	while (ss) {
    163 		ss >>= 1;
    164 		stackshift++;
    165 	}
    166 	pu->pu_cc_stackshift = stackshift;
    167 	assert(1<<stackshift == ss);
    168 }
    169 
    170 struct puffs_pathobj *
    171 puffs_getrootpathobj(struct puffs_usermount *pu)
    172 {
    173 	struct puffs_node *pnr;
    174 
    175 	pnr = pu->pu_pn_root;
    176 	if (pnr == NULL) {
    177 		errno = ENOENT;
    178 		return NULL;
    179 	}
    180 
    181 	return &pnr->pn_po;
    182 }
    183 
    184 void
    185 puffs_setroot(struct puffs_usermount *pu, struct puffs_node *pn)
    186 {
    187 
    188 	pu->pu_pn_root = pn;
    189 }
    190 
    191 struct puffs_node *
    192 puffs_getroot(struct puffs_usermount *pu)
    193 {
    194 
    195 	return pu->pu_pn_root;
    196 }
    197 
    198 void
    199 puffs_setrootinfo(struct puffs_usermount *pu, enum vtype vt,
    200 	vsize_t vsize, dev_t rdev)
    201 {
    202 	struct puffs_kargs *pargs = pu->pu_kargp;
    203 
    204 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT) {
    205 		warnx("puffs_setrootinfo: call has effect only "
    206 		    "before mount\n");
    207 		return;
    208 	}
    209 
    210 	pargs->pa_root_vtype = vt;
    211 	pargs->pa_root_vsize = vsize;
    212 	pargs->pa_root_rdev = rdev;
    213 }
    214 
    215 void *
    216 puffs_getspecific(struct puffs_usermount *pu)
    217 {
    218 
    219 	return pu->pu_privdata;
    220 }
    221 
    222 size_t
    223 puffs_getmaxreqlen(struct puffs_usermount *pu)
    224 {
    225 
    226 	return pu->pu_maxreqlen;
    227 }
    228 
    229 void
    230 puffs_setmaxreqlen(struct puffs_usermount *pu, size_t reqlen)
    231 {
    232 
    233 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
    234 		warnx("puffs_setmaxreqlen: call has effect only "
    235 		    "before mount\n");
    236 
    237 	pu->pu_kargp->pa_maxmsglen = reqlen;
    238 }
    239 
    240 void
    241 puffs_setfhsize(struct puffs_usermount *pu, size_t fhsize, int flags)
    242 {
    243 
    244 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
    245 		warnx("puffs_setfhsize: call has effect only before mount\n");
    246 
    247 	pu->pu_kargp->pa_fhsize = fhsize;
    248 	pu->pu_kargp->pa_fhflags = flags;
    249 }
    250 
    251 void
    252 puffs_setncookiehash(struct puffs_usermount *pu, int nhash)
    253 {
    254 
    255 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
    256 		warnx("puffs_setfhsize: call has effect only before mount\n");
    257 
    258 	pu->pu_kargp->pa_nhashbuckets = nhash;
    259 }
    260 
    261 void
    262 puffs_set_pathbuild(struct puffs_usermount *pu, pu_pathbuild_fn fn)
    263 {
    264 
    265 	pu->pu_pathbuild = fn;
    266 }
    267 
    268 void
    269 puffs_set_pathtransform(struct puffs_usermount *pu, pu_pathtransform_fn fn)
    270 {
    271 
    272 	pu->pu_pathtransform = fn;
    273 }
    274 
    275 void
    276 puffs_set_pathcmp(struct puffs_usermount *pu, pu_pathcmp_fn fn)
    277 {
    278 
    279 	pu->pu_pathcmp = fn;
    280 }
    281 
    282 void
    283 puffs_set_pathfree(struct puffs_usermount *pu, pu_pathfree_fn fn)
    284 {
    285 
    286 	pu->pu_pathfree = fn;
    287 }
    288 
    289 void
    290 puffs_set_namemod(struct puffs_usermount *pu, pu_namemod_fn fn)
    291 {
    292 
    293 	pu->pu_namemod = fn;
    294 }
    295 
    296 void
    297 puffs_set_errnotify(struct puffs_usermount *pu, pu_errnotify_fn fn)
    298 {
    299 
    300 	pu->pu_errnotify = fn;
    301 }
    302 
    303 void
    304 puffs_ml_setloopfn(struct puffs_usermount *pu, puffs_ml_loop_fn lfn)
    305 {
    306 
    307 	pu->pu_ml_lfn = lfn;
    308 }
    309 
    310 void
    311 puffs_ml_settimeout(struct puffs_usermount *pu, struct timespec *ts)
    312 {
    313 
    314 	if (ts == NULL) {
    315 		pu->pu_ml_timep = NULL;
    316 	} else {
    317 		pu->pu_ml_timeout = *ts;
    318 		pu->pu_ml_timep = &pu->pu_ml_timeout;
    319 	}
    320 }
    321 
    322 void
    323 puffs_set_prepost(struct puffs_usermount *pu,
    324 	pu_prepost_fn pre, pu_prepost_fn pst)
    325 {
    326 
    327 	pu->pu_oppre = pre;
    328 	pu->pu_oppost = pst;
    329 }
    330 
    331 void
    332 puffs_setback(struct puffs_cc *pcc, int whatback)
    333 {
    334 	struct puffs_req *preq = pcc->pcc_preq;
    335 
    336 	assert(PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN && (
    337 	    preq->preq_optype == PUFFS_VN_OPEN ||
    338 	    preq->preq_optype == PUFFS_VN_MMAP ||
    339 	    preq->preq_optype == PUFFS_VN_REMOVE ||
    340 	    preq->preq_optype == PUFFS_VN_RMDIR ||
    341 	    preq->preq_optype == PUFFS_VN_INACTIVE));
    342 
    343 	preq->preq_setbacks |= whatback & PUFFS_SETBACK_MASK;
    344 }
    345 
    346 int
    347 puffs_daemon(struct puffs_usermount *pu, int nochdir, int noclose)
    348 {
    349 	ssize_t n;
    350 	int parent, value, fd;
    351 
    352 	if (pipe(pu->pu_dpipe) == -1)
    353 		return -1;
    354 
    355 	switch (fork()) {
    356 	case -1:
    357 		return -1;
    358 	case 0:
    359 		parent = 0;
    360 		break;
    361 	default:
    362 		parent = 1;
    363 		break;
    364 	}
    365 	pu->pu_state |= PU_PUFFSDAEMON;
    366 
    367 	if (parent) {
    368 		n = read(pu->pu_dpipe[0], &value, sizeof(int));
    369 		if (n == -1)
    370 			err(1, "puffs_daemon");
    371 		assert(n == sizeof(value));
    372 		if (value) {
    373 			errno = value;
    374 			err(1, "puffs_daemon");
    375 		}
    376 		exit(0);
    377 	} else {
    378 		if (setsid() == -1)
    379 			goto fail;
    380 
    381 		if (!nochdir)
    382 			chdir("/");
    383 
    384 		if (!noclose) {
    385 			fd = open(_PATH_DEVNULL, O_RDWR, 0);
    386 			if (fd == -1)
    387 				goto fail;
    388 			dup2(fd, STDIN_FILENO);
    389 			dup2(fd, STDOUT_FILENO);
    390 			dup2(fd, STDERR_FILENO);
    391 			if (fd > STDERR_FILENO)
    392 				close(fd);
    393 		}
    394 		return 0;
    395 	}
    396 
    397  fail:
    398 	n = write(pu->pu_dpipe[1], &errno, sizeof(int));
    399 	assert(n == 4);
    400 	return -1;
    401 }
    402 
    403 int
    404 puffs_mount(struct puffs_usermount *pu, const char *dir, int mntflags,
    405 	void *cookie)
    406 {
    407 	char rp[MAXPATHLEN];
    408 	ssize_t n;
    409 	int rv, fd, sverrno;
    410 
    411 #if 1
    412 	/* XXXkludgehere */
    413 	/* kauth doesn't provide this service any longer */
    414 	if (geteuid() != 0)
    415 		mntflags |= MNT_NOSUID | MNT_NODEV;
    416 #endif
    417 
    418 	if (realpath(dir, rp) == NULL) {
    419 		rv = -1;
    420 		goto out;
    421 	}
    422 
    423 	if (strcmp(dir, rp) != 0) {
    424 		warnx("puffs_mount: \"%s\" is a relative path.", dir);
    425 		warnx("puffs_mount: using \"%s\" instead.", rp);
    426 	}
    427 
    428 	fd = open(_PATH_PUFFS, O_RDWR);
    429 	if (fd == -1) {
    430 		warnx("puffs_mount: cannot open %s", _PATH_PUFFS);
    431 		rv = -1;
    432 		goto out;
    433 	}
    434 	if (fd <= 2)
    435 		warnx("puffs_init: device fd %d (<= 2), sure this is "
    436 		    "what you want?", fd);
    437 
    438 	pu->pu_kargp->pa_fd = pu->pu_fd = fd;
    439 	pu->pu_kargp->pa_root_cookie = cookie;
    440 	if ((rv = mount(MOUNT_PUFFS, rp, mntflags,
    441 	    pu->pu_kargp, sizeof(struct puffs_kargs))) == -1)
    442 		goto out;
    443 #if 0
    444 	if ((rv = ioctl(pu->pu_fd, PUFFSREQSIZEOP, &pu->pu_maxreqlen)) == -1)
    445 		goto out;
    446 #endif
    447 	PU_SETSTATE(pu, PUFFS_STATE_RUNNING);
    448 
    449  out:
    450 	if (rv != 0)
    451 		sverrno = errno;
    452 	else
    453 		sverrno = 0;
    454 	free(pu->pu_kargp);
    455 	pu->pu_kargp = NULL;
    456 
    457 	if (pu->pu_state & PU_PUFFSDAEMON) {
    458 		n = write(pu->pu_dpipe[1], &sverrno, sizeof(int));
    459 		assert(n == 4);
    460 		close(pu->pu_dpipe[0]);
    461 		close(pu->pu_dpipe[1]);
    462 	}
    463 
    464 	errno = sverrno;
    465 	return rv;
    466 }
    467 
    468 struct puffs_usermount *
    469 _puffs_init(int develv, struct puffs_ops *pops, const char *mntfromname,
    470 	const char *puffsname, void *priv, uint32_t pflags)
    471 {
    472 	struct puffs_usermount *pu;
    473 	struct puffs_kargs *pargs;
    474 	int sverrno;
    475 
    476 	if (develv != PUFFS_DEVEL_LIBVERSION) {
    477 		warnx("puffs_init: mounting with lib version %d, need %d",
    478 		    develv, PUFFS_DEVEL_LIBVERSION);
    479 		errno = EINVAL;
    480 		return NULL;
    481 	}
    482 
    483 	pu = malloc(sizeof(struct puffs_usermount));
    484 	if (pu == NULL)
    485 		goto failfree;
    486 	memset(pu, 0, sizeof(struct puffs_usermount));
    487 
    488 	pargs = pu->pu_kargp = malloc(sizeof(struct puffs_kargs));
    489 	if (pargs == NULL)
    490 		goto failfree;
    491 	memset(pargs, 0, sizeof(struct puffs_kargs));
    492 
    493 	pargs->pa_vers = PUFFSDEVELVERS | PUFFSVERSION;
    494 	pargs->pa_flags = PUFFS_FLAG_KERN(pflags);
    495 	fillvnopmask(pops, pargs->pa_vnopmask);
    496 	(void)strlcpy(pargs->pa_typename, puffsname,
    497 	    sizeof(pargs->pa_typename));
    498 	(void)strlcpy(pargs->pa_mntfromname, mntfromname,
    499 	    sizeof(pargs->pa_mntfromname));
    500 
    501 	puffs_zerostatvfs(&pargs->pa_svfsb);
    502 	pargs->pa_root_cookie = NULL;
    503 	pargs->pa_root_vtype = VDIR;
    504 	pargs->pa_root_vsize = 0;
    505 	pargs->pa_root_rdev = 0;
    506 	pargs->pa_maxmsglen = 0;
    507 
    508 	pu->pu_flags = pflags;
    509 	pu->pu_ops = *pops;
    510 	free(pops); /* XXX */
    511 
    512 	pu->pu_privdata = priv;
    513 	pu->pu_cc_stackshift = PUFFS_CC_STACKSHIFT_DEFAULT;
    514 	LIST_INIT(&pu->pu_pnodelst);
    515 	LIST_INIT(&pu->pu_framectrl.fb_ios);
    516 	LIST_INIT(&pu->pu_ccnukelst);
    517 	TAILQ_INIT(&pu->pu_sched);
    518 	TAILQ_INIT(&pu->pu_exq);
    519 
    520 	/* defaults for some user-settable translation functions */
    521 	pu->pu_cmap = NULL; /* identity translation */
    522 
    523 	pu->pu_pathbuild = puffs_stdpath_buildpath;
    524 	pu->pu_pathfree = puffs_stdpath_freepath;
    525 	pu->pu_pathcmp = puffs_stdpath_cmppath;
    526 	pu->pu_pathtransform = NULL;
    527 	pu->pu_namemod = NULL;
    528 
    529 	pu->pu_errnotify = puffs_defaulterror;
    530 
    531 	PU_SETSTATE(pu, PUFFS_STATE_BEFOREMOUNT);
    532 
    533 	return pu;
    534 
    535  failfree:
    536 	/* can't unmount() from here for obvious reasons */
    537 	sverrno = errno;
    538 	free(pu);
    539 	errno = sverrno;
    540 	return NULL;
    541 }
    542 
    543 /*
    544  * XXX: there's currently no clean way to request unmount from
    545  * within the user server, so be very brutal about it.
    546  */
    547 /*ARGSUSED1*/
    548 int
    549 puffs_exit(struct puffs_usermount *pu, int force)
    550 {
    551 	struct puffs_node *pn;
    552 
    553 	force = 1; /* currently */
    554 
    555 	if (pu->pu_fd)
    556 		close(pu->pu_fd);
    557 
    558 	while ((pn = LIST_FIRST(&pu->pu_pnodelst)) != NULL)
    559 		puffs_pn_put(pn);
    560 
    561 	puffs_framev_exit(pu);
    562 	if (pu->pu_state & PU_HASKQ)
    563 		close(pu->pu_kq);
    564 	free(pu);
    565 
    566 	return 0; /* always succesful for now, WILL CHANGE */
    567 }
    568 
    569 int
    570 puffs_mainloop(struct puffs_usermount *pu)
    571 {
    572 	struct puffs_getreq *pgr = NULL;
    573 	struct puffs_putreq *ppr = NULL;
    574 	struct puffs_framectrl *pfctrl = &pu->pu_framectrl;
    575 	struct puffs_fctrl_io *fio;
    576 	struct puffs_cc *pcc;
    577 	struct kevent *curev, *newevs;
    578 	size_t nchanges;
    579 	int puffsfd, sverrno;
    580 	int ndone;
    581 
    582 	assert(puffs_getstate(pu) >= PUFFS_STATE_RUNNING);
    583 
    584 	pgr = puffs_req_makeget(pu, puffs_getmaxreqlen(pu), 0);
    585 	if (pgr == NULL)
    586 		goto out;
    587 
    588 	ppr = puffs_req_makeput(pu);
    589 	if (ppr == NULL)
    590 		goto out;
    591 
    592 	newevs = realloc(pfctrl->evs, (2*pfctrl->nfds+1)*sizeof(struct kevent));
    593 	if (newevs == NULL)
    594 		goto out;
    595 	pfctrl->evs = newevs;
    596 
    597 	pu->pu_state |= PU_INLOOP;
    598 
    599 	pu->pu_kq = kqueue();
    600 	if (pu->pu_kq == -1)
    601 		goto out;
    602 	pu->pu_state |= PU_HASKQ;
    603 
    604 	curev = pfctrl->evs;
    605 	LIST_FOREACH(fio, &pfctrl->fb_ios, fio_entries) {
    606 		EV_SET(curev, fio->io_fd, EVFILT_READ, EV_ADD,
    607 		    0, 0, (uintptr_t)fio);
    608 		curev++;
    609 		EV_SET(curev, fio->io_fd, EVFILT_WRITE, EV_ADD | EV_DISABLE,
    610 		    0, 0, (uintptr_t)fio);
    611 		curev++;
    612 	}
    613 	puffsfd = puffs_getselectable(pu);
    614 	EV_SET(curev, puffsfd, EVFILT_READ, EV_ADD, 0, 0, 0);
    615 	if (kevent(pu->pu_kq, pfctrl->evs, 2*pfctrl->nfds+1,
    616 	    NULL, 0, NULL) == -1)
    617 		goto out;
    618 
    619 	while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
    620 		if (pu->pu_ml_lfn)
    621 			pu->pu_ml_lfn(pu);
    622 
    623 		/*
    624 		 * Do this here, because:
    625 		 *  a) loopfunc might generate some results
    626 		 *  b) it's still "after" event handling (except for round 1)
    627 		 */
    628 		if (puffs_req_putput(ppr) == -1)
    629 			goto out;
    630 		puffs_req_resetput(ppr);
    631 
    632 		/* micro optimization: skip kevent syscall if possible */
    633 		if (pfctrl->nfds == 0 && pu->pu_ml_timep == NULL
    634 		    && (pu->pu_state & PU_ASYNCFD) == 0) {
    635 			if (puffs_req_handle(pgr, ppr, 0) == -1)
    636 				goto out;
    637 			continue;
    638 		}
    639 
    640 		/* else: do full processing */
    641 
    642 		/*
    643 		 * Build list of which to enable/disable in writecheck.
    644 		 * Don't bother worrying about O(n) for now.
    645 		 */
    646 		nchanges = 0;
    647 		LIST_FOREACH(fio, &pfctrl->fb_ios, fio_entries) {
    648 			if (fio->stat & FIO_WRGONE)
    649 				continue;
    650 
    651 			/*
    652 			 * Try to write out everything to avoid the
    653 			 * need for enabling EVFILT_WRITE.  The likely
    654 			 * case is that we can fit everything into the
    655 			 * socket buffer.
    656 			 */
    657 			if (puffs_framev_output(pu, pfctrl, fio, ppr)) {
    658 				/* need kernel notify? (error condition) */
    659 				if (puffs_req_putput(ppr) == -1)
    660 					goto out;
    661 				puffs_req_resetput(ppr);
    662 			}
    663 
    664 			/* en/disable write checks for kqueue as needed */
    665 			assert((FIO_EN_WRITE(fio) && FIO_RM_WRITE(fio)) == 0);
    666 			if (FIO_EN_WRITE(fio)) {
    667 				EV_SET(&pfctrl->evs[nchanges], fio->io_fd,
    668 				    EVFILT_WRITE, EV_ENABLE, 0, 0,
    669 				    (uintptr_t)fio);
    670 				fio->stat |= FIO_WR;
    671 				nchanges++;
    672 			}
    673 			if (FIO_RM_WRITE(fio)) {
    674 				EV_SET(&pfctrl->evs[nchanges], fio->io_fd,
    675 				    EVFILT_WRITE, EV_DISABLE, 0, 0,
    676 				    (uintptr_t)fio);
    677 				fio->stat &= ~FIO_WR;
    678 				nchanges++;
    679 			}
    680 			assert(nchanges <= pfctrl->nfds);
    681 		}
    682 
    683 		ndone = kevent(pu->pu_kq, pfctrl->evs, nchanges,
    684 		    pfctrl->evs, pfctrl->nfds+1, pu->pu_ml_timep);
    685 
    686 		if (ndone == -1) {
    687 			if (errno != EINTR)
    688 				goto out;
    689 			else
    690 				continue;
    691 		}
    692 
    693 		/* uoptimize */
    694 		if (ndone == 0)
    695 			continue;
    696 
    697 		/* iterate over the results */
    698 		for (curev = pfctrl->evs; ndone--; curev++) {
    699 			int what;
    700 
    701 			/* get & possibly dispatch events from kernel */
    702 			if (curev->ident == puffsfd) {
    703 				if (puffs_req_handle(pgr, ppr, 0) == -1)
    704 					goto out;
    705 				continue;
    706 			}
    707 
    708 			fio = (void *)curev->udata;
    709 			if (curev->flags & EV_ERROR) {
    710 				assert(curev->filter == EVFILT_WRITE);
    711 				fio->stat &= ~FIO_WR;
    712 
    713 				/* XXX: how to know if it's a transient error */
    714 				puffs_framev_writeclose(pu, fio,
    715 				    (int)curev->data);
    716 				puffs_framev_notify(fio, PUFFS_FBIO_ERROR);
    717 				continue;
    718 			}
    719 
    720 			what = 0;
    721 			if (curev->filter == EVFILT_READ) {
    722 				puffs_framev_input(pu, pfctrl, fio, ppr);
    723 				what |= PUFFS_FBIO_READ;
    724 			}
    725 
    726 			else if (curev->filter == EVFILT_WRITE) {
    727 				puffs_framev_output(pu, pfctrl, fio, ppr);
    728 				what |= PUFFS_FBIO_WRITE;
    729 			}
    730 			if (what)
    731 				puffs_framev_notify(fio, what);
    732 		}
    733 
    734 		/*
    735 		 * Schedule continuations.
    736 		 */
    737 		while ((pcc = TAILQ_FIRST(&pu->pu_sched)) != NULL) {
    738 			TAILQ_REMOVE(&pu->pu_sched, pcc, entries);
    739 			puffs_goto(pcc);
    740 		}
    741 
    742 		/*
    743 		 * Really free fd's now that we don't have references
    744 		 * to them.
    745 		 */
    746 		while ((fio = LIST_FIRST(&pfctrl->fb_ios_rmlist)) != NULL) {
    747 			LIST_REMOVE(fio, fio_entries);
    748 			free(fio);
    749 		}
    750 	}
    751 	errno = 0;
    752 	puffs_req_putput(ppr);
    753 
    754  out:
    755 	/* store the real error for a while */
    756 	sverrno = errno;
    757 
    758 	if (ppr)
    759 		puffs_req_destroyput(ppr);
    760 	if (pgr)
    761 		puffs_req_destroyget(pgr);
    762 
    763 	errno = sverrno;
    764 	if (errno)
    765 		return -1;
    766 	else
    767 		return 0;
    768 }
    769