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