Home | History | Annotate | Line # | Download | only in libpuffs
puffs.c revision 1.78
      1 /*	$NetBSD: puffs.c,v 1.78 2007/11/29 17:47:55 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.78 2007/11/29 17:47:55 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 	long psize;
    159 	int stackshift;
    160 
    161 	psize = sysconf(_SC_PAGESIZE);
    162 	if (ss < 2*psize) {
    163 		ss = 2*psize;
    164 		fprintf(stderr, "puffs_setstacksize: adjusting stacksize "
    165 		    "to minimum %zu\n", ss);
    166 	}
    167 
    168 	assert(puffs_getstate(pu) == PUFFS_STATE_BEFOREMOUNT);
    169 	stackshift = -1;
    170 	while (ss) {
    171 		ss >>= 1;
    172 		stackshift++;
    173 	}
    174 	pu->pu_cc_stackshift = stackshift;
    175 	assert(1<<stackshift == ss);
    176 }
    177 
    178 struct puffs_pathobj *
    179 puffs_getrootpathobj(struct puffs_usermount *pu)
    180 {
    181 	struct puffs_node *pnr;
    182 
    183 	pnr = pu->pu_pn_root;
    184 	if (pnr == NULL) {
    185 		errno = ENOENT;
    186 		return NULL;
    187 	}
    188 
    189 	return &pnr->pn_po;
    190 }
    191 
    192 void
    193 puffs_setroot(struct puffs_usermount *pu, struct puffs_node *pn)
    194 {
    195 
    196 	pu->pu_pn_root = pn;
    197 }
    198 
    199 struct puffs_node *
    200 puffs_getroot(struct puffs_usermount *pu)
    201 {
    202 
    203 	return pu->pu_pn_root;
    204 }
    205 
    206 void
    207 puffs_setrootinfo(struct puffs_usermount *pu, enum vtype vt,
    208 	vsize_t vsize, dev_t rdev)
    209 {
    210 	struct puffs_kargs *pargs = pu->pu_kargp;
    211 
    212 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT) {
    213 		warnx("puffs_setrootinfo: call has effect only "
    214 		    "before mount\n");
    215 		return;
    216 	}
    217 
    218 	pargs->pa_root_vtype = vt;
    219 	pargs->pa_root_vsize = vsize;
    220 	pargs->pa_root_rdev = rdev;
    221 }
    222 
    223 void *
    224 puffs_getspecific(struct puffs_usermount *pu)
    225 {
    226 
    227 	return pu->pu_privdata;
    228 }
    229 
    230 size_t
    231 puffs_getmaxreqlen(struct puffs_usermount *pu)
    232 {
    233 
    234 	return pu->pu_maxreqlen;
    235 }
    236 
    237 void
    238 puffs_setmaxreqlen(struct puffs_usermount *pu, size_t reqlen)
    239 {
    240 
    241 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
    242 		warnx("puffs_setmaxreqlen: call has effect only "
    243 		    "before mount\n");
    244 
    245 	pu->pu_kargp->pa_maxmsglen = reqlen;
    246 }
    247 
    248 void
    249 puffs_setfhsize(struct puffs_usermount *pu, size_t fhsize, int flags)
    250 {
    251 
    252 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
    253 		warnx("puffs_setfhsize: call has effect only before mount\n");
    254 
    255 	pu->pu_kargp->pa_fhsize = fhsize;
    256 	pu->pu_kargp->pa_fhflags = flags;
    257 }
    258 
    259 void
    260 puffs_setncookiehash(struct puffs_usermount *pu, int nhash)
    261 {
    262 
    263 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
    264 		warnx("puffs_setfhsize: call has effect only before mount\n");
    265 
    266 	pu->pu_kargp->pa_nhashbuckets = nhash;
    267 }
    268 
    269 void
    270 puffs_set_pathbuild(struct puffs_usermount *pu, pu_pathbuild_fn fn)
    271 {
    272 
    273 	pu->pu_pathbuild = fn;
    274 }
    275 
    276 void
    277 puffs_set_pathtransform(struct puffs_usermount *pu, pu_pathtransform_fn fn)
    278 {
    279 
    280 	pu->pu_pathtransform = fn;
    281 }
    282 
    283 void
    284 puffs_set_pathcmp(struct puffs_usermount *pu, pu_pathcmp_fn fn)
    285 {
    286 
    287 	pu->pu_pathcmp = fn;
    288 }
    289 
    290 void
    291 puffs_set_pathfree(struct puffs_usermount *pu, pu_pathfree_fn fn)
    292 {
    293 
    294 	pu->pu_pathfree = fn;
    295 }
    296 
    297 void
    298 puffs_set_namemod(struct puffs_usermount *pu, pu_namemod_fn fn)
    299 {
    300 
    301 	pu->pu_namemod = fn;
    302 }
    303 
    304 void
    305 puffs_set_errnotify(struct puffs_usermount *pu, pu_errnotify_fn fn)
    306 {
    307 
    308 	pu->pu_errnotify = fn;
    309 }
    310 
    311 void
    312 puffs_ml_setloopfn(struct puffs_usermount *pu, puffs_ml_loop_fn lfn)
    313 {
    314 
    315 	pu->pu_ml_lfn = lfn;
    316 }
    317 
    318 void
    319 puffs_ml_settimeout(struct puffs_usermount *pu, struct timespec *ts)
    320 {
    321 
    322 	if (ts == NULL) {
    323 		pu->pu_ml_timep = NULL;
    324 	} else {
    325 		pu->pu_ml_timeout = *ts;
    326 		pu->pu_ml_timep = &pu->pu_ml_timeout;
    327 	}
    328 }
    329 
    330 void
    331 puffs_set_prepost(struct puffs_usermount *pu,
    332 	pu_prepost_fn pre, pu_prepost_fn pst)
    333 {
    334 
    335 	pu->pu_oppre = pre;
    336 	pu->pu_oppost = pst;
    337 }
    338 
    339 void
    340 puffs_setback(struct puffs_cc *pcc, int whatback)
    341 {
    342 	struct puffs_req *preq = pcc->pcc_preq;
    343 
    344 	assert(PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN && (
    345 	    preq->preq_optype == PUFFS_VN_OPEN ||
    346 	    preq->preq_optype == PUFFS_VN_MMAP ||
    347 	    preq->preq_optype == PUFFS_VN_REMOVE ||
    348 	    preq->preq_optype == PUFFS_VN_RMDIR ||
    349 	    preq->preq_optype == PUFFS_VN_INACTIVE));
    350 
    351 	preq->preq_setbacks |= whatback & PUFFS_SETBACK_MASK;
    352 }
    353 
    354 int
    355 puffs_daemon(struct puffs_usermount *pu, int nochdir, int noclose)
    356 {
    357 	ssize_t n;
    358 	int parent, value, fd;
    359 
    360 	if (pipe(pu->pu_dpipe) == -1)
    361 		return -1;
    362 
    363 	switch (fork()) {
    364 	case -1:
    365 		return -1;
    366 	case 0:
    367 		parent = 0;
    368 		break;
    369 	default:
    370 		parent = 1;
    371 		break;
    372 	}
    373 	pu->pu_state |= PU_PUFFSDAEMON;
    374 
    375 	if (parent) {
    376 		n = read(pu->pu_dpipe[0], &value, sizeof(int));
    377 		if (n == -1)
    378 			err(1, "puffs_daemon");
    379 		assert(n == sizeof(value));
    380 		if (value) {
    381 			errno = value;
    382 			err(1, "puffs_daemon");
    383 		}
    384 		exit(0);
    385 	} else {
    386 		if (setsid() == -1)
    387 			goto fail;
    388 
    389 		if (!nochdir)
    390 			chdir("/");
    391 
    392 		if (!noclose) {
    393 			fd = open(_PATH_DEVNULL, O_RDWR, 0);
    394 			if (fd == -1)
    395 				goto fail;
    396 			dup2(fd, STDIN_FILENO);
    397 			dup2(fd, STDOUT_FILENO);
    398 			dup2(fd, STDERR_FILENO);
    399 			if (fd > STDERR_FILENO)
    400 				close(fd);
    401 		}
    402 		return 0;
    403 	}
    404 
    405  fail:
    406 	n = write(pu->pu_dpipe[1], &errno, sizeof(int));
    407 	assert(n == 4);
    408 	return -1;
    409 }
    410 
    411 int
    412 puffs_mount(struct puffs_usermount *pu, const char *dir, int mntflags,
    413 	void *cookie)
    414 {
    415 	char rp[MAXPATHLEN];
    416 	ssize_t n;
    417 	int rv, fd, sverrno;
    418 
    419 #if 1
    420 	/* XXXkludgehere */
    421 	/* kauth doesn't provide this service any longer */
    422 	if (geteuid() != 0)
    423 		mntflags |= MNT_NOSUID | MNT_NODEV;
    424 #endif
    425 
    426 	if (realpath(dir, rp) == NULL) {
    427 		rv = -1;
    428 		goto out;
    429 	}
    430 
    431 	if (strcmp(dir, rp) != 0) {
    432 		warnx("puffs_mount: \"%s\" is a relative path.", dir);
    433 		warnx("puffs_mount: using \"%s\" instead.", rp);
    434 	}
    435 
    436 	fd = open(_PATH_PUFFS, O_RDWR);
    437 	if (fd == -1) {
    438 		warnx("puffs_mount: cannot open %s", _PATH_PUFFS);
    439 		rv = -1;
    440 		goto out;
    441 	}
    442 	if (fd <= 2)
    443 		warnx("puffs_init: device fd %d (<= 2), sure this is "
    444 		    "what you want?", fd);
    445 
    446 	pu->pu_kargp->pa_fd = pu->pu_fd = fd;
    447 	pu->pu_kargp->pa_root_cookie = cookie;
    448 	if ((rv = mount(MOUNT_PUFFS, rp, mntflags,
    449 	    pu->pu_kargp, sizeof(struct puffs_kargs))) == -1)
    450 		goto out;
    451 #if 0
    452 	if ((rv = ioctl(pu->pu_fd, PUFFSREQSIZEOP, &pu->pu_maxreqlen)) == -1)
    453 		goto out;
    454 #endif
    455 	PU_SETSTATE(pu, PUFFS_STATE_RUNNING);
    456 
    457  out:
    458 	if (rv != 0)
    459 		sverrno = errno;
    460 	else
    461 		sverrno = 0;
    462 	free(pu->pu_kargp);
    463 	pu->pu_kargp = NULL;
    464 
    465 	if (pu->pu_state & PU_PUFFSDAEMON) {
    466 		n = write(pu->pu_dpipe[1], &sverrno, sizeof(int));
    467 		assert(n == 4);
    468 		close(pu->pu_dpipe[0]);
    469 		close(pu->pu_dpipe[1]);
    470 	}
    471 
    472 	errno = sverrno;
    473 	return rv;
    474 }
    475 
    476 struct puffs_usermount *
    477 _puffs_init(int develv, struct puffs_ops *pops, const char *mntfromname,
    478 	const char *puffsname, void *priv, uint32_t pflags)
    479 {
    480 	struct puffs_usermount *pu;
    481 	struct puffs_kargs *pargs;
    482 	int sverrno;
    483 
    484 	if (develv != PUFFS_DEVEL_LIBVERSION) {
    485 		warnx("puffs_init: mounting with lib version %d, need %d",
    486 		    develv, PUFFS_DEVEL_LIBVERSION);
    487 		errno = EINVAL;
    488 		return NULL;
    489 	}
    490 
    491 	pu = malloc(sizeof(struct puffs_usermount));
    492 	if (pu == NULL)
    493 		goto failfree;
    494 	memset(pu, 0, sizeof(struct puffs_usermount));
    495 
    496 	pargs = pu->pu_kargp = malloc(sizeof(struct puffs_kargs));
    497 	if (pargs == NULL)
    498 		goto failfree;
    499 	memset(pargs, 0, sizeof(struct puffs_kargs));
    500 
    501 	pargs->pa_vers = PUFFSDEVELVERS | PUFFSVERSION;
    502 	pargs->pa_flags = PUFFS_FLAG_KERN(pflags);
    503 	fillvnopmask(pops, pargs->pa_vnopmask);
    504 	(void)strlcpy(pargs->pa_typename, puffsname,
    505 	    sizeof(pargs->pa_typename));
    506 	(void)strlcpy(pargs->pa_mntfromname, mntfromname,
    507 	    sizeof(pargs->pa_mntfromname));
    508 
    509 	puffs_zerostatvfs(&pargs->pa_svfsb);
    510 	pargs->pa_root_cookie = NULL;
    511 	pargs->pa_root_vtype = VDIR;
    512 	pargs->pa_root_vsize = 0;
    513 	pargs->pa_root_rdev = 0;
    514 	pargs->pa_maxmsglen = 0;
    515 
    516 	pu->pu_flags = pflags;
    517 	pu->pu_ops = *pops;
    518 	free(pops); /* XXX */
    519 
    520 	pu->pu_privdata = priv;
    521 	pu->pu_cc_stackshift = PUFFS_CC_STACKSHIFT_DEFAULT;
    522 	LIST_INIT(&pu->pu_pnodelst);
    523 	LIST_INIT(&pu->pu_framectrl.fb_ios);
    524 	LIST_INIT(&pu->pu_ccnukelst);
    525 	TAILQ_INIT(&pu->pu_sched);
    526 	TAILQ_INIT(&pu->pu_exq);
    527 
    528 	/* defaults for some user-settable translation functions */
    529 	pu->pu_cmap = NULL; /* identity translation */
    530 
    531 	pu->pu_pathbuild = puffs_stdpath_buildpath;
    532 	pu->pu_pathfree = puffs_stdpath_freepath;
    533 	pu->pu_pathcmp = puffs_stdpath_cmppath;
    534 	pu->pu_pathtransform = NULL;
    535 	pu->pu_namemod = NULL;
    536 
    537 	pu->pu_errnotify = puffs_defaulterror;
    538 
    539 	PU_SETSTATE(pu, PUFFS_STATE_BEFOREMOUNT);
    540 
    541 	return pu;
    542 
    543  failfree:
    544 	/* can't unmount() from here for obvious reasons */
    545 	sverrno = errno;
    546 	free(pu);
    547 	errno = sverrno;
    548 	return NULL;
    549 }
    550 
    551 /*
    552  * XXX: there's currently no clean way to request unmount from
    553  * within the user server, so be very brutal about it.
    554  */
    555 /*ARGSUSED1*/
    556 int
    557 puffs_exit(struct puffs_usermount *pu, int force)
    558 {
    559 	struct puffs_node *pn;
    560 
    561 	force = 1; /* currently */
    562 
    563 	if (pu->pu_fd)
    564 		close(pu->pu_fd);
    565 
    566 	while ((pn = LIST_FIRST(&pu->pu_pnodelst)) != NULL)
    567 		puffs_pn_put(pn);
    568 
    569 	puffs_framev_exit(pu);
    570 	if (pu->pu_state & PU_HASKQ)
    571 		close(pu->pu_kq);
    572 	free(pu);
    573 
    574 	return 0; /* always succesful for now, WILL CHANGE */
    575 }
    576 
    577 int
    578 puffs_mainloop(struct puffs_usermount *pu)
    579 {
    580 	struct puffs_getreq *pgr = NULL;
    581 	struct puffs_putreq *ppr = NULL;
    582 	struct puffs_framectrl *pfctrl = &pu->pu_framectrl;
    583 	struct puffs_fctrl_io *fio;
    584 	struct puffs_cc *pcc;
    585 	struct kevent *curev, *newevs;
    586 	size_t nchanges;
    587 	int puffsfd, sverrno;
    588 	int ndone;
    589 
    590 	assert(puffs_getstate(pu) >= PUFFS_STATE_RUNNING);
    591 
    592 	pgr = puffs_req_makeget(pu, puffs_getmaxreqlen(pu), 0);
    593 	if (pgr == NULL)
    594 		goto out;
    595 
    596 	ppr = puffs_req_makeput(pu);
    597 	if (ppr == NULL)
    598 		goto out;
    599 
    600 	newevs = realloc(pfctrl->evs, (2*pfctrl->nfds+1)*sizeof(struct kevent));
    601 	if (newevs == NULL)
    602 		goto out;
    603 	pfctrl->evs = newevs;
    604 
    605 	pu->pu_state |= PU_INLOOP;
    606 
    607 	pu->pu_kq = kqueue();
    608 	if (pu->pu_kq == -1)
    609 		goto out;
    610 	pu->pu_state |= PU_HASKQ;
    611 
    612 	curev = pfctrl->evs;
    613 	LIST_FOREACH(fio, &pfctrl->fb_ios, fio_entries) {
    614 		EV_SET(curev, fio->io_fd, EVFILT_READ, EV_ADD,
    615 		    0, 0, (uintptr_t)fio);
    616 		curev++;
    617 		EV_SET(curev, fio->io_fd, EVFILT_WRITE, EV_ADD | EV_DISABLE,
    618 		    0, 0, (uintptr_t)fio);
    619 		curev++;
    620 	}
    621 	puffsfd = puffs_getselectable(pu);
    622 	EV_SET(curev, puffsfd, EVFILT_READ, EV_ADD, 0, 0, 0);
    623 	if (kevent(pu->pu_kq, pfctrl->evs, 2*pfctrl->nfds+1,
    624 	    NULL, 0, NULL) == -1)
    625 		goto out;
    626 
    627 	while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
    628 		if (pu->pu_ml_lfn)
    629 			pu->pu_ml_lfn(pu);
    630 
    631 		/*
    632 		 * Do this here, because:
    633 		 *  a) loopfunc might generate some results
    634 		 *  b) it's still "after" event handling (except for round 1)
    635 		 */
    636 		if (puffs_req_putput(ppr) == -1)
    637 			goto out;
    638 		puffs_req_resetput(ppr);
    639 
    640 		/* micro optimization: skip kevent syscall if possible */
    641 		if (pfctrl->nfds == 0 && pu->pu_ml_timep == NULL
    642 		    && (pu->pu_state & PU_ASYNCFD) == 0) {
    643 			if (puffs_req_handle(pgr, ppr, 0) == -1)
    644 				goto out;
    645 			continue;
    646 		}
    647 
    648 		/* else: do full processing */
    649 
    650 		/*
    651 		 * Build list of which to enable/disable in writecheck.
    652 		 * Don't bother worrying about O(n) for now.
    653 		 */
    654 		nchanges = 0;
    655 		LIST_FOREACH(fio, &pfctrl->fb_ios, fio_entries) {
    656 			if (fio->stat & FIO_WRGONE)
    657 				continue;
    658 
    659 			/*
    660 			 * Try to write out everything to avoid the
    661 			 * need for enabling EVFILT_WRITE.  The likely
    662 			 * case is that we can fit everything into the
    663 			 * socket buffer.
    664 			 */
    665 			if (puffs_framev_output(pu, pfctrl, fio, ppr)) {
    666 				/* need kernel notify? (error condition) */
    667 				if (puffs_req_putput(ppr) == -1)
    668 					goto out;
    669 				puffs_req_resetput(ppr);
    670 			}
    671 
    672 			/* en/disable write checks for kqueue as needed */
    673 			assert((FIO_EN_WRITE(fio) && FIO_RM_WRITE(fio)) == 0);
    674 			if (FIO_EN_WRITE(fio)) {
    675 				EV_SET(&pfctrl->evs[nchanges], fio->io_fd,
    676 				    EVFILT_WRITE, EV_ENABLE, 0, 0,
    677 				    (uintptr_t)fio);
    678 				fio->stat |= FIO_WR;
    679 				nchanges++;
    680 			}
    681 			if (FIO_RM_WRITE(fio)) {
    682 				EV_SET(&pfctrl->evs[nchanges], fio->io_fd,
    683 				    EVFILT_WRITE, EV_DISABLE, 0, 0,
    684 				    (uintptr_t)fio);
    685 				fio->stat &= ~FIO_WR;
    686 				nchanges++;
    687 			}
    688 			assert(nchanges <= pfctrl->nfds);
    689 		}
    690 
    691 		ndone = kevent(pu->pu_kq, pfctrl->evs, nchanges,
    692 		    pfctrl->evs, pfctrl->nfds+1, pu->pu_ml_timep);
    693 
    694 		if (ndone == -1) {
    695 			if (errno != EINTR)
    696 				goto out;
    697 			else
    698 				continue;
    699 		}
    700 
    701 		/* uoptimize */
    702 		if (ndone == 0)
    703 			continue;
    704 
    705 		/* iterate over the results */
    706 		for (curev = pfctrl->evs; ndone--; curev++) {
    707 			int what;
    708 
    709 			/* get & possibly dispatch events from kernel */
    710 			if (curev->ident == puffsfd) {
    711 				if (puffs_req_handle(pgr, ppr, 0) == -1)
    712 					goto out;
    713 				continue;
    714 			}
    715 
    716 			fio = (void *)curev->udata;
    717 			if (curev->flags & EV_ERROR) {
    718 				assert(curev->filter == EVFILT_WRITE);
    719 				fio->stat &= ~FIO_WR;
    720 
    721 				/* XXX: how to know if it's a transient error */
    722 				puffs_framev_writeclose(pu, fio,
    723 				    (int)curev->data);
    724 				puffs_framev_notify(fio, PUFFS_FBIO_ERROR);
    725 				continue;
    726 			}
    727 
    728 			what = 0;
    729 			if (curev->filter == EVFILT_READ) {
    730 				puffs_framev_input(pu, pfctrl, fio, ppr);
    731 				what |= PUFFS_FBIO_READ;
    732 			}
    733 
    734 			else if (curev->filter == EVFILT_WRITE) {
    735 				puffs_framev_output(pu, pfctrl, fio, ppr);
    736 				what |= PUFFS_FBIO_WRITE;
    737 			}
    738 			if (what)
    739 				puffs_framev_notify(fio, what);
    740 		}
    741 
    742 		/*
    743 		 * Schedule continuations.
    744 		 */
    745 		while ((pcc = TAILQ_FIRST(&pu->pu_sched)) != NULL) {
    746 			TAILQ_REMOVE(&pu->pu_sched, pcc, entries);
    747 			puffs_goto(pcc);
    748 		}
    749 
    750 		/*
    751 		 * Really free fd's now that we don't have references
    752 		 * to them.
    753 		 */
    754 		while ((fio = LIST_FIRST(&pfctrl->fb_ios_rmlist)) != NULL) {
    755 			LIST_REMOVE(fio, fio_entries);
    756 			free(fio);
    757 		}
    758 	}
    759 	errno = 0;
    760 	puffs_req_putput(ppr);
    761 
    762  out:
    763 	/* store the real error for a while */
    764 	sverrno = errno;
    765 
    766 	if (ppr)
    767 		puffs_req_destroyput(ppr);
    768 	if (pgr)
    769 		puffs_req_destroyget(pgr);
    770 
    771 	errno = sverrno;
    772 	if (errno)
    773 		return -1;
    774 	else
    775 		return 0;
    776 }
    777