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