Home | History | Annotate | Line # | Download | only in libpuffs
puffs.c revision 1.55
      1 /*	$NetBSD: puffs.c,v 1.55 2007/07/05 12:27:39 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.55 2007/07/05 12:27:39 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 <puffs.h>
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 #include <string.h>
     49 #include <syslog.h>
     50 #include <unistd.h>
     51 
     52 #include "puffs_priv.h"
     53 
     54 /* Most file systems want this for opts, so just give it to them */
     55 const struct mntopt puffsmopts[] = {
     56 	MOPT_STDOPTS,
     57 	PUFFSMOPT_STD,
     58 	MOPT_NULL,
     59 };
     60 
     61 #define FILLOP(lower, upper)						\
     62 do {									\
     63 	if (pops->puffs_node_##lower)					\
     64 		opmask[PUFFS_VN_##upper] = 1;				\
     65 } while (/*CONSTCOND*/0)
     66 static void
     67 fillvnopmask(struct puffs_ops *pops, uint8_t *opmask)
     68 {
     69 
     70 	memset(opmask, 0, PUFFS_VN_MAX);
     71 
     72 	FILLOP(create,   CREATE);
     73 	FILLOP(mknod,    MKNOD);
     74 	FILLOP(open,     OPEN);
     75 	FILLOP(close,    CLOSE);
     76 	FILLOP(access,   ACCESS);
     77 	FILLOP(getattr,  GETATTR);
     78 	FILLOP(setattr,  SETATTR);
     79 	FILLOP(poll,     POLL); /* XXX: not ready in kernel */
     80 	FILLOP(mmap,     MMAP);
     81 	FILLOP(fsync,    FSYNC);
     82 	FILLOP(seek,     SEEK);
     83 	FILLOP(remove,   REMOVE);
     84 	FILLOP(link,     LINK);
     85 	FILLOP(rename,   RENAME);
     86 	FILLOP(mkdir,    MKDIR);
     87 	FILLOP(rmdir,    RMDIR);
     88 	FILLOP(symlink,  SYMLINK);
     89 	FILLOP(readdir,  READDIR);
     90 	FILLOP(readlink, READLINK);
     91 	FILLOP(reclaim,  RECLAIM);
     92 	FILLOP(inactive, INACTIVE);
     93 	FILLOP(print,    PRINT);
     94 	FILLOP(read,     READ);
     95 	FILLOP(write,    WRITE);
     96 
     97 	/* XXX: not implemented in the kernel */
     98 	FILLOP(getextattr, GETEXTATTR);
     99 	FILLOP(setextattr, SETEXTATTR);
    100 	FILLOP(listextattr, LISTEXTATTR);
    101 }
    102 #undef FILLOP
    103 
    104 int
    105 puffs_getselectable(struct puffs_usermount *pu)
    106 {
    107 
    108 	return pu->pu_kargs.pa_fd;
    109 }
    110 
    111 int
    112 puffs_setblockingmode(struct puffs_usermount *pu, int mode)
    113 {
    114 	int rv, x;
    115 
    116 	if (mode != PUFFSDEV_BLOCK && mode != PUFFSDEV_NONBLOCK) {
    117 		errno = EINVAL;
    118 		return -1;
    119 	}
    120 
    121 	x = mode;
    122 	rv = ioctl(pu->pu_kargs.pa_fd, FIONBIO, &x);
    123 
    124 	if (rv == 0) {
    125 		if (mode == PUFFSDEV_BLOCK)
    126 			pu->pu_state &= ~PU_ASYNCFD;
    127 		else
    128 			pu->pu_state |= PU_ASYNCFD;
    129 	}
    130 
    131 	return rv;
    132 }
    133 
    134 int
    135 puffs_getstate(struct puffs_usermount *pu)
    136 {
    137 
    138 	return pu->pu_state & PU_STATEMASK;
    139 }
    140 
    141 void
    142 puffs_setstacksize(struct puffs_usermount *pu, size_t ss)
    143 {
    144 
    145 	pu->pu_cc_stacksize = ss;
    146 }
    147 
    148 struct puffs_pathobj *
    149 puffs_getrootpathobj(struct puffs_usermount *pu)
    150 {
    151 	struct puffs_node *pnr;
    152 
    153 	pnr = pu->pu_pn_root;
    154 	if (pnr == NULL) {
    155 		errno = ENOENT;
    156 		return NULL;
    157 	}
    158 
    159 	return &pnr->pn_po;
    160 }
    161 
    162 void
    163 puffs_setroot(struct puffs_usermount *pu, struct puffs_node *pn)
    164 {
    165 
    166 	pu->pu_pn_root = pn;
    167 }
    168 
    169 struct puffs_node *
    170 puffs_getroot(struct puffs_usermount *pu)
    171 {
    172 
    173 	return pu->pu_pn_root;
    174 }
    175 
    176 void
    177 puffs_setrootinfo(struct puffs_usermount *pu, enum vtype vt,
    178 	vsize_t vsize, dev_t rdev)
    179 {
    180 	struct puffs_kargs *pargs = &pu->pu_kargs;
    181 
    182 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
    183 		warnx("puffs_setrootinfo: call has effect only "
    184 		    "before mount\n");
    185 
    186 	pargs->pa_root_vtype = vt;
    187 	pargs->pa_root_vsize = vsize;
    188 	pargs->pa_root_rdev = rdev;
    189 }
    190 
    191 void *
    192 puffs_getspecific(struct puffs_usermount *pu)
    193 {
    194 
    195 	return pu->pu_privdata;
    196 }
    197 
    198 size_t
    199 puffs_getmaxreqlen(struct puffs_usermount *pu)
    200 {
    201 
    202 	return pu->pu_kargs.pa_maxreqlen;
    203 }
    204 
    205 void
    206 puffs_setmaxreqlen(struct puffs_usermount *pu, size_t reqlen)
    207 {
    208 
    209 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
    210 		warnx("puffs_setmaxreqlen: call has effect only "
    211 		    "before mount\n");
    212 
    213 	pu->pu_kargs.pa_maxreqlen = reqlen;
    214 }
    215 
    216 void
    217 puffs_setfhsize(struct puffs_usermount *pu, size_t fhsize, int flags)
    218 {
    219 
    220 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
    221 		warnx("puffs_setfhsize: call has effect only before mount\n");
    222 
    223 	pu->pu_kargs.pa_fhsize = fhsize;
    224 	pu->pu_kargs.pa_fhflags = flags;
    225 }
    226 
    227 void
    228 puffs_setncookiehash(struct puffs_usermount *pu, int nhash)
    229 {
    230 
    231 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
    232 		warnx("puffs_setfhsize: call has effect only before mount\n");
    233 
    234 	pu->pu_kargs.pa_nhashbuckets = nhash;
    235 }
    236 
    237 void
    238 puffs_set_pathbuild(struct puffs_usermount *pu, pu_pathbuild_fn fn)
    239 {
    240 
    241 	pu->pu_pathbuild = fn;
    242 }
    243 
    244 void
    245 puffs_set_pathtransform(struct puffs_usermount *pu, pu_pathtransform_fn fn)
    246 {
    247 
    248 	pu->pu_pathtransform = fn;
    249 }
    250 
    251 void
    252 puffs_set_pathcmp(struct puffs_usermount *pu, pu_pathcmp_fn fn)
    253 {
    254 
    255 	pu->pu_pathcmp = fn;
    256 }
    257 
    258 void
    259 puffs_set_pathfree(struct puffs_usermount *pu, pu_pathfree_fn fn)
    260 {
    261 
    262 	pu->pu_pathfree = fn;
    263 }
    264 
    265 void
    266 puffs_set_namemod(struct puffs_usermount *pu, pu_namemod_fn fn)
    267 {
    268 
    269 	pu->pu_namemod = fn;
    270 }
    271 
    272 void
    273 puffs_ml_setloopfn(struct puffs_usermount *pu, puffs_ml_loop_fn lfn)
    274 {
    275 
    276 	pu->pu_ml_lfn = lfn;
    277 }
    278 
    279 void
    280 puffs_ml_settimeout(struct puffs_usermount *pu, struct timespec *ts)
    281 {
    282 
    283 	if (ts == NULL) {
    284 		pu->pu_ml_timep = NULL;
    285 	} else {
    286 		pu->pu_ml_timeout = *ts;
    287 		pu->pu_ml_timep = &pu->pu_ml_timeout;
    288 	}
    289 }
    290 
    291 void
    292 puffs_setback(struct puffs_cc *pcc, int whatback)
    293 {
    294 	struct puffs_req *preq = pcc->pcc_preq;
    295 
    296 	assert(PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN && (
    297 	    preq->preq_optype == PUFFS_VN_OPEN ||
    298 	    preq->preq_optype == PUFFS_VN_MMAP ||
    299 	    preq->preq_optype == PUFFS_VN_REMOVE ||
    300 	    preq->preq_optype == PUFFS_VN_RMDIR ||
    301 	    preq->preq_optype == PUFFS_VN_INACTIVE));
    302 
    303 	preq->preq_setbacks |= whatback & PUFFS_SETBACK_MASK;
    304 }
    305 
    306 int
    307 puffs_mount(struct puffs_usermount *pu, const char *dir, int mntflags,
    308 	void *cookie)
    309 {
    310 
    311 #if 1
    312 	/* XXXkludgehere */
    313 	/* kauth doesn't provide this service any longer */
    314 	if (geteuid() != 0)
    315 		mntflags |= MNT_NOSUID | MNT_NODEV;
    316 #endif
    317 
    318 	pu->pu_kargs.pa_root_cookie = cookie;
    319 	if (mount(MOUNT_PUFFS, dir, mntflags, &pu->pu_kargs) == -1)
    320 		return -1;
    321 	PU_SETSTATE(pu, PUFFS_STATE_RUNNING);
    322 
    323 	return 0;
    324 }
    325 
    326 struct puffs_usermount *
    327 _puffs_init(int develv, struct puffs_ops *pops, const char *puffsname,
    328 	void *priv, uint32_t pflags)
    329 {
    330 	struct puffs_usermount *pu;
    331 	struct puffs_kargs *pargs;
    332 	int fd;
    333 
    334 	if (develv != PUFFS_DEVEL_LIBVERSION) {
    335 		warnx("puffs_init: mounting with lib version %d, need %d",
    336 		    develv, PUFFS_DEVEL_LIBVERSION);
    337 		errno = EINVAL;
    338 		return NULL;
    339 	}
    340 
    341 	fd = open("/dev/puffs", O_RDONLY);
    342 	if (fd == -1) {
    343 		warnx("puffs_init: cannot open /dev/puffs");
    344 		return NULL;
    345 	}
    346 	if (fd <= 2)
    347 		warnx("puffs_init: device fd %d (<= 2), sure this is "
    348 		    "what you want?", fd);
    349 
    350 	pu = malloc(sizeof(struct puffs_usermount));
    351 	if (pu == NULL)
    352 		goto failfree;
    353 	memset(pu, 0, sizeof(struct puffs_usermount));
    354 
    355 	pargs = &pu->pu_kargs;
    356 	pargs->pa_vers = PUFFSDEVELVERS | PUFFSVERSION;
    357 	pargs->pa_flags = PUFFS_FLAG_KERN(pflags);
    358 	pargs->pa_fd = fd;
    359 	fillvnopmask(pops, pargs->pa_vnopmask);
    360 	(void)strlcpy(pargs->pa_name, puffsname, sizeof(pargs->pa_name));
    361 
    362 	puffs_zerostatvfs(&pargs->pa_svfsb);
    363 	pargs->pa_root_cookie = NULL;
    364 	pargs->pa_root_vtype = VDIR;
    365 	pargs->pa_root_vsize = 0;
    366 	pargs->pa_root_rdev = 0;
    367 
    368 	pu->pu_flags = pflags;
    369 	pu->pu_ops = *pops;
    370 	free(pops); /* XXX */
    371 
    372 	pu->pu_privdata = priv;
    373 	pu->pu_cc_stacksize = PUFFS_CC_STACKSIZE_DEFAULT;
    374 	LIST_INIT(&pu->pu_pnodelst);
    375 	LIST_INIT(&pu->pu_framectrl.fb_ios);
    376 	LIST_INIT(&pu->pu_ccnukelst);
    377 
    378 	/* defaults for some user-settable translation functions */
    379 	pu->pu_cmap = NULL; /* identity translation */
    380 
    381 	pu->pu_pathbuild = puffs_stdpath_buildpath;
    382 	pu->pu_pathfree = puffs_stdpath_freepath;
    383 	pu->pu_pathcmp = puffs_stdpath_cmppath;
    384 	pu->pu_pathtransform = NULL;
    385 	pu->pu_namemod = NULL;
    386 
    387 	PU_SETSTATE(pu, PUFFS_STATE_BEFOREMOUNT);
    388 
    389 	return pu;
    390 
    391  failfree:
    392 	/* can't unmount() from here for obvious reasons */
    393 	close(fd);
    394 	free(pu);
    395 	return NULL;
    396 }
    397 
    398 /*
    399  * XXX: there's currently no clean way to request unmount from
    400  * within the user server, so be very brutal about it.
    401  */
    402 /*ARGSUSED1*/
    403 int
    404 puffs_exit(struct puffs_usermount *pu, int force)
    405 {
    406 	struct puffs_node *pn;
    407 
    408 	force = 1; /* currently */
    409 
    410 	if (pu->pu_kargs.pa_fd)
    411 		close(pu->pu_kargs.pa_fd);
    412 
    413 	while ((pn = LIST_FIRST(&pu->pu_pnodelst)) != NULL)
    414 		puffs_pn_put(pn);
    415 
    416 	puffs_framev_exit(pu);
    417 	if (pu->pu_haskq)
    418 		close(pu->pu_kq);
    419 	free(pu);
    420 
    421 	return 0; /* always succesful for now, WILL CHANGE */
    422 }
    423 
    424 int
    425 puffs_mainloop(struct puffs_usermount *pu, int flags)
    426 {
    427 	struct puffs_getreq *pgr = NULL;
    428 	struct puffs_putreq *ppr = NULL;
    429 	struct puffs_framectrl *pfctrl = &pu->pu_framectrl;
    430 	struct puffs_fctrl_io *fio;
    431 	struct kevent *curev, *newevs;
    432 	size_t nchanges;
    433 	int puffsfd, sverrno;
    434 	int ndone;
    435 
    436 	assert(puffs_getstate(pu) >= PUFFS_STATE_RUNNING);
    437 
    438 	pgr = puffs_req_makeget(pu, puffs_getmaxreqlen(pu), 0);
    439 	if (pgr == NULL)
    440 		goto out;
    441 
    442 	ppr = puffs_req_makeput(pu);
    443 	if (ppr == NULL)
    444 		goto out;
    445 
    446 	newevs = realloc(pfctrl->evs, (2*pfctrl->nfds+1)*sizeof(struct kevent));
    447 	if (newevs == NULL)
    448 		goto out;
    449 	pfctrl->evs = newevs;
    450 
    451 	if ((flags & PUFFSLOOP_NODAEMON) == 0)
    452 		if (daemon(1, 0) == -1)
    453 			goto out;
    454 	pu->pu_state |= PU_INLOOP;
    455 
    456 	pu->pu_kq = kqueue();
    457 	if (pu->pu_kq == -1)
    458 		goto out;
    459 	pu->pu_haskq = 1;
    460 
    461 	curev = pfctrl->evs;
    462 	LIST_FOREACH(fio, &pfctrl->fb_ios, fio_entries) {
    463 		EV_SET(curev, fio->io_fd, EVFILT_READ, EV_ADD,
    464 		    0, 0, (uintptr_t)fio);
    465 		curev++;
    466 		EV_SET(curev, fio->io_fd, EVFILT_WRITE, EV_ADD | EV_DISABLE,
    467 		    0, 0, (uintptr_t)fio);
    468 		curev++;
    469 	}
    470 	puffsfd = puffs_getselectable(pu);
    471 	EV_SET(curev, puffsfd, EVFILT_READ, EV_ADD, 0, 0, 0);
    472 	if (kevent(pu->pu_kq, pfctrl->evs, 2*pfctrl->nfds+1,
    473 	    NULL, 0, NULL) == -1)
    474 		goto out;
    475 
    476 	while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
    477 		if (pu->pu_ml_lfn)
    478 			pu->pu_ml_lfn(pu);
    479 
    480 		/*
    481 		 * Do this here, because:
    482 		 *  a) loopfunc might generate some results
    483 		 *  b) it's still "after" event handling (except for round 1)
    484 		 */
    485 		if (puffs_req_putput(ppr) == -1)
    486 			goto out;
    487 		puffs_req_resetput(ppr);
    488 
    489 		/* micro optimization: skip kevent syscall if possible */
    490 		if (pfctrl->nfds == 0 && pu->pu_ml_timep == NULL
    491 		    && (pu->pu_state & PU_ASYNCFD) == 0) {
    492 			if (puffs_req_handle(pgr, ppr, 0) == -1)
    493 				goto out;
    494 			continue;
    495 		}
    496 
    497 		/* else: do full processing */
    498 
    499 		/*
    500 		 * Build list of which to enable/disable in writecheck.
    501 		 * Don't bother worrying about O(n) for now.
    502 		 */
    503 		nchanges = 0;
    504 		LIST_FOREACH(fio, &pfctrl->fb_ios, fio_entries) {
    505 			if (fio->stat & FIO_WRGONE)
    506 				continue;
    507 
    508 			/*
    509 			 * Try to write out everything to avoid the
    510 			 * need for enabling EVFILT_WRITE.  The likely
    511 			 * case is that we can fit everything into the
    512 			 * socket buffer.
    513 			 */
    514 			if (puffs_framev_output(pu, pfctrl, fio)) {
    515 				/* need kernel notify? (error condition) */
    516 				if (puffs_req_putput(ppr) == -1)
    517 					goto out;
    518 				puffs_req_resetput(ppr);
    519 			}
    520 
    521 			/* en/disable write checks for kqueue as needed */
    522 			assert((FIO_EN_WRITE(fio) && FIO_RM_WRITE(fio)) == 0);
    523 			if (FIO_EN_WRITE(fio)) {
    524 				EV_SET(&pfctrl->evs[nchanges], fio->io_fd,
    525 				    EVFILT_WRITE, EV_ENABLE, 0, 0,
    526 				    (uintptr_t)fio);
    527 				fio->stat |= FIO_WR;
    528 				nchanges++;
    529 			}
    530 			if (FIO_RM_WRITE(fio)) {
    531 				EV_SET(&pfctrl->evs[nchanges], fio->io_fd,
    532 				    EVFILT_WRITE, EV_DISABLE, 0, 0,
    533 				    (uintptr_t)fio);
    534 				fio->stat &= ~FIO_WR;
    535 				nchanges++;
    536 			}
    537 			assert(nchanges <= pfctrl->nfds);
    538 		}
    539 
    540 		ndone = kevent(pu->pu_kq, pfctrl->evs, nchanges,
    541 		    pfctrl->evs, pfctrl->nfds+1, pu->pu_ml_timep);
    542 
    543 		if (ndone == -1) {
    544 			if (errno != EINTR)
    545 				goto out;
    546 			else
    547 				continue;
    548 		}
    549 
    550 		/* uoptimize */
    551 		if (ndone == 0)
    552 			continue;
    553 
    554 		/* iterate over the results */
    555 		for (curev = pfctrl->evs; ndone--; curev++) {
    556 			/* get & possibly dispatch events from kernel */
    557 			if (curev->ident == puffsfd) {
    558 				if (puffs_req_handle(pgr, ppr, 0) == -1)
    559 					goto out;
    560 				continue;
    561 			}
    562 
    563 			fio = (void *)curev->udata;
    564 			if (curev->flags & EV_ERROR) {
    565 				assert(curev->filter == EVFILT_WRITE);
    566 				fio->stat &= ~FIO_WR;
    567 
    568 				/* XXX: how to know if it's a transient error */
    569 				puffs_framev_writeclose(pu, fio,
    570 				    (int)curev->data);
    571 				continue;
    572 			}
    573 
    574 			if (curev->filter == EVFILT_READ) {
    575 				if (curev->flags & EV_EOF && curev->data == 0)
    576 					puffs_framev_readclose(pu, fio,
    577 					    ECONNRESET);
    578 				else
    579 					puffs_framev_input(pu, pfctrl,
    580 					    fio, ppr);
    581 
    582 			} else if (curev->filter == EVFILT_WRITE) {
    583 				if (curev->flags & EV_EOF)
    584 					puffs_framev_writeclose(pu, fio,
    585 					    ECONNRESET);
    586 				else
    587 					puffs_framev_output(pu, pfctrl, fio);
    588 			}
    589 		}
    590 
    591 		/*
    592 		 * Really free fd's now that we don't have references
    593 		 * to them.
    594 		 */
    595 		while ((fio = LIST_FIRST(&pfctrl->fb_ios_rmlist)) != NULL) {
    596 			LIST_REMOVE(fio, fio_entries);
    597 			free(fio);
    598 		}
    599 	}
    600 	errno = 0;
    601 	puffs_req_putput(ppr);
    602 
    603  out:
    604 	/* store the real error for a while */
    605 	sverrno = errno;
    606 
    607 	if (ppr)
    608 		puffs_req_destroyput(ppr);
    609 	if (pgr)
    610 		puffs_req_destroyget(pgr);
    611 
    612 	errno = sverrno;
    613 	if (errno)
    614 		return -1;
    615 	else
    616 		return 0;
    617 }
    618