Home | History | Annotate | Line # | Download | only in libpuffs
puffs.c revision 1.51
      1 /*	$NetBSD: puffs.c,v 1.51 2007/05/21 08:30:49 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  * 3. The name of the company nor the name of the author may be used to
     19  *    endorse or promote products derived from this software without specific
     20  *    prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     23  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     25  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 #if !defined(lint)
     37 __RCSID("$NetBSD: puffs.c,v 1.51 2007/05/21 08:30:49 pooka Exp $");
     38 #endif /* !lint */
     39 
     40 #include <sys/param.h>
     41 #include <sys/mount.h>
     42 
     43 #include <assert.h>
     44 #include <err.h>
     45 #include <errno.h>
     46 #include <fcntl.h>
     47 #include <mntopts.h>
     48 #include <puffs.h>
     49 #include <stdio.h>
     50 #include <stdlib.h>
     51 #include <string.h>
     52 #include <syslog.h>
     53 #include <unistd.h>
     54 
     55 #include "puffs_priv.h"
     56 
     57 /* Most file systems want this for opts, so just give it to them */
     58 const struct mntopt puffsmopts[] = {
     59 	MOPT_STDOPTS,
     60 	PUFFSMOPT_STD,
     61 	MOPT_NULL,
     62 };
     63 
     64 #define FILLOP(lower, upper)						\
     65 do {									\
     66 	if (pops->puffs_node_##lower)					\
     67 		opmask[PUFFS_VN_##upper] = 1;				\
     68 } while (/*CONSTCOND*/0)
     69 static void
     70 fillvnopmask(struct puffs_ops *pops, uint8_t *opmask)
     71 {
     72 
     73 	memset(opmask, 0, PUFFS_VN_MAX);
     74 
     75 	FILLOP(create,   CREATE);
     76 	FILLOP(mknod,    MKNOD);
     77 	FILLOP(open,     OPEN);
     78 	FILLOP(close,    CLOSE);
     79 	FILLOP(access,   ACCESS);
     80 	FILLOP(getattr,  GETATTR);
     81 	FILLOP(setattr,  SETATTR);
     82 	FILLOP(poll,     POLL); /* XXX: not ready in kernel */
     83 	FILLOP(mmap,     MMAP);
     84 	FILLOP(fsync,    FSYNC);
     85 	FILLOP(seek,     SEEK);
     86 	FILLOP(remove,   REMOVE);
     87 	FILLOP(link,     LINK);
     88 	FILLOP(rename,   RENAME);
     89 	FILLOP(mkdir,    MKDIR);
     90 	FILLOP(rmdir,    RMDIR);
     91 	FILLOP(symlink,  SYMLINK);
     92 	FILLOP(readdir,  READDIR);
     93 	FILLOP(readlink, READLINK);
     94 	FILLOP(reclaim,  RECLAIM);
     95 	FILLOP(inactive, INACTIVE);
     96 	FILLOP(print,    PRINT);
     97 	FILLOP(read,     READ);
     98 	FILLOP(write,    WRITE);
     99 
    100 	/* XXX: not implemented in the kernel */
    101 	FILLOP(getextattr, GETEXTATTR);
    102 	FILLOP(setextattr, SETEXTATTR);
    103 	FILLOP(listextattr, LISTEXTATTR);
    104 }
    105 #undef FILLOP
    106 
    107 int
    108 puffs_getselectable(struct puffs_usermount *pu)
    109 {
    110 
    111 	return pu->pu_kargs.pa_fd;
    112 }
    113 
    114 int
    115 puffs_setblockingmode(struct puffs_usermount *pu, int mode)
    116 {
    117 	int rv, x;
    118 
    119 	if (mode != PUFFSDEV_BLOCK && mode != PUFFSDEV_NONBLOCK) {
    120 		errno = EINVAL;
    121 		return -1;
    122 	}
    123 
    124 	x = mode;
    125 	rv = ioctl(pu->pu_kargs.pa_fd, FIONBIO, &x);
    126 
    127 	if (rv == 0) {
    128 		if (mode == PUFFSDEV_BLOCK)
    129 			pu->pu_state &= ~PU_ASYNCFD;
    130 		else
    131 			pu->pu_state |= PU_ASYNCFD;
    132 	}
    133 
    134 	return rv;
    135 }
    136 
    137 int
    138 puffs_getstate(struct puffs_usermount *pu)
    139 {
    140 
    141 	return pu->pu_state & PU_STATEMASK;
    142 }
    143 
    144 void
    145 puffs_setstacksize(struct puffs_usermount *pu, size_t ss)
    146 {
    147 
    148 	pu->pu_cc_stacksize = ss;
    149 }
    150 
    151 struct puffs_pathobj *
    152 puffs_getrootpathobj(struct puffs_usermount *pu)
    153 {
    154 	struct puffs_node *pnr;
    155 
    156 	pnr = pu->pu_pn_root;
    157 	if (pnr == NULL) {
    158 		errno = ENOENT;
    159 		return NULL;
    160 	}
    161 
    162 	return &pnr->pn_po;
    163 }
    164 
    165 void
    166 puffs_setroot(struct puffs_usermount *pu, struct puffs_node *pn)
    167 {
    168 
    169 	pu->pu_pn_root = pn;
    170 }
    171 
    172 struct puffs_node *
    173 puffs_getroot(struct puffs_usermount *pu)
    174 {
    175 
    176 	return pu->pu_pn_root;
    177 }
    178 
    179 void
    180 puffs_setrootinfo(struct puffs_usermount *pu, enum vtype vt,
    181 	vsize_t vsize, dev_t rdev)
    182 {
    183 	struct puffs_kargs *pargs = &pu->pu_kargs;
    184 
    185 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
    186 		warnx("puffs_setrootinfo: call has effect only "
    187 		    "before mount\n");
    188 
    189 	pargs->pa_root_vtype = vt;
    190 	pargs->pa_root_vsize = vsize;
    191 	pargs->pa_root_rdev = rdev;
    192 }
    193 
    194 void *
    195 puffs_getspecific(struct puffs_usermount *pu)
    196 {
    197 
    198 	return pu->pu_privdata;
    199 }
    200 
    201 size_t
    202 puffs_getmaxreqlen(struct puffs_usermount *pu)
    203 {
    204 
    205 	return pu->pu_kargs.pa_maxreqlen;
    206 }
    207 
    208 void
    209 puffs_setmaxreqlen(struct puffs_usermount *pu, size_t reqlen)
    210 {
    211 
    212 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
    213 		warnx("puffs_setmaxreqlen: call has effect only "
    214 		    "before mount\n");
    215 
    216 	pu->pu_kargs.pa_maxreqlen = reqlen;
    217 }
    218 
    219 void
    220 puffs_setfhsize(struct puffs_usermount *pu, size_t fhsize, int flags)
    221 {
    222 
    223 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
    224 		warnx("puffs_setfhsize: call has effect only before mount\n");
    225 
    226 	pu->pu_kargs.pa_fhsize = fhsize;
    227 	pu->pu_kargs.pa_fhflags = flags;
    228 }
    229 
    230 void
    231 puffs_setncookiehash(struct puffs_usermount *pu, int nhash)
    232 {
    233 
    234 	if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
    235 		warnx("puffs_setfhsize: call has effect only before mount\n");
    236 
    237 	pu->pu_kargs.pa_nhashbuckets = nhash;
    238 }
    239 
    240 void
    241 puffs_set_pathbuild(struct puffs_usermount *pu, pu_pathbuild_fn fn)
    242 {
    243 
    244 	pu->pu_pathbuild = fn;
    245 }
    246 
    247 void
    248 puffs_set_pathtransform(struct puffs_usermount *pu, pu_pathtransform_fn fn)
    249 {
    250 
    251 	pu->pu_pathtransform = fn;
    252 }
    253 
    254 void
    255 puffs_set_pathcmp(struct puffs_usermount *pu, pu_pathcmp_fn fn)
    256 {
    257 
    258 	pu->pu_pathcmp = fn;
    259 }
    260 
    261 void
    262 puffs_set_pathfree(struct puffs_usermount *pu, pu_pathfree_fn fn)
    263 {
    264 
    265 	pu->pu_pathfree = fn;
    266 }
    267 
    268 void
    269 puffs_set_namemod(struct puffs_usermount *pu, pu_namemod_fn fn)
    270 {
    271 
    272 	pu->pu_namemod = fn;
    273 }
    274 
    275 void
    276 puffs_ml_setloopfn(struct puffs_usermount *pu, puffs_ml_loop_fn lfn)
    277 {
    278 
    279 	pu->pu_ml_lfn = lfn;
    280 }
    281 
    282 void
    283 puffs_ml_settimeout(struct puffs_usermount *pu, struct timespec *ts)
    284 {
    285 
    286 	if (ts == NULL) {
    287 		pu->pu_ml_timep = NULL;
    288 	} else {
    289 		pu->pu_ml_timeout = *ts;
    290 		pu->pu_ml_timep = &pu->pu_ml_timeout;
    291 	}
    292 }
    293 
    294 void
    295 puffs_setback(struct puffs_cc *pcc, int whatback)
    296 {
    297 	struct puffs_req *preq = pcc->pcc_preq;
    298 
    299 	assert(PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN && (
    300 	    preq->preq_optype == PUFFS_VN_OPEN ||
    301 	    preq->preq_optype == PUFFS_VN_MMAP ||
    302 	    preq->preq_optype == PUFFS_VN_REMOVE ||
    303 	    preq->preq_optype == PUFFS_VN_RMDIR));
    304 
    305 	preq->preq_setbacks |= whatback & PUFFS_SETBACK_MASK;
    306 }
    307 
    308 int
    309 puffs_mount(struct puffs_usermount *pu, const char *dir, int mntflags,
    310 	void *cookie)
    311 {
    312 
    313 #if 1
    314 	/* XXXkludgehere */
    315 	/* kauth doesn't provide this service any longer */
    316 	if (geteuid() != 0)
    317 		mntflags |= MNT_NOSUID | MNT_NODEV;
    318 #endif
    319 
    320 	pu->pu_kargs.pa_root_cookie = cookie;
    321 	if (mount(MOUNT_PUFFS, dir, mntflags, &pu->pu_kargs) == -1)
    322 		return -1;
    323 	PU_SETSTATE(pu, PUFFS_STATE_RUNNING);
    324 
    325 	return 0;
    326 }
    327 
    328 struct puffs_usermount *
    329 _puffs_init(int develv, struct puffs_ops *pops, const char *puffsname,
    330 	void *priv, uint32_t pflags)
    331 {
    332 	struct puffs_usermount *pu;
    333 	struct puffs_kargs *pargs;
    334 	int fd;
    335 
    336 	if (develv != PUFFS_DEVEL_LIBVERSION) {
    337 		warnx("puffs_mount: mounting with lib version %d, need %d",
    338 		    develv, PUFFS_DEVEL_LIBVERSION);
    339 		errno = EINVAL;
    340 		return NULL;
    341 	}
    342 
    343 	fd = open("/dev/puffs", O_RDONLY);
    344 	if (fd == -1)
    345 		return NULL;
    346 	if (fd <= 2)
    347 		warnx("puffs_mount: 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 		/* micro optimization: skip kevent syscall if possible */
    481 		if (pfctrl->nfds == 0 && pu->pu_ml_timep == NULL
    482 		    && (pu->pu_state & PU_ASYNCFD) == 0) {
    483 			if (puffs_req_handle(pgr, ppr, 0) == -1)
    484 				goto out;
    485 			if (puffs_req_putput(ppr) == -1)
    486 				goto out;
    487 			puffs_req_resetput(ppr);
    488 			continue;
    489 		}
    490 		/* else: do full processing */
    491 
    492 		/*
    493 		 * Do this here, because:
    494 		 *  a) loopfunc might generate some results
    495 		 *  b) it's still "after" event handling (except for round 1)
    496 		 */
    497 		if (puffs_req_putput(ppr) == -1)
    498 			goto out;
    499 		puffs_req_resetput(ppr);
    500 
    501 		/*
    502 		 * Build list of which to enable/disable in writecheck.
    503 		 * Don't bother worrying about O(n) for now.
    504 		 */
    505 		nchanges = 0;
    506 		LIST_FOREACH(fio, &pfctrl->fb_ios, fio_entries) {
    507 			if (fio->stat & FIO_WRGONE)
    508 				continue;
    509 
    510 			/*
    511 			 * Try to write out everything to avoid the
    512 			 * need for enabling EVFILT_WRITE.  The likely
    513 			 * case is that we can fit everything into the
    514 			 * socket buffer.
    515 			 */
    516 			if (puffs_framev_output(pu, pfctrl, fio)) {
    517 				/* need kernel notify? (error condition) */
    518 				if (puffs_req_putput(ppr) == -1)
    519 					goto out;
    520 				puffs_req_resetput(ppr);
    521 			}
    522 
    523 			/* en/disable write checks for kqueue as needed */
    524 			assert((FIO_EN_WRITE(fio) && FIO_RM_WRITE(fio)) == 0);
    525 			if (FIO_EN_WRITE(fio)) {
    526 				EV_SET(&pfctrl->evs[nchanges], fio->io_fd,
    527 				    EVFILT_WRITE, EV_ENABLE, 0, 0,
    528 				    (uintptr_t)fio);
    529 				fio->stat |= FIO_WR;
    530 				nchanges++;
    531 			}
    532 			if (FIO_RM_WRITE(fio)) {
    533 				EV_SET(&pfctrl->evs[nchanges], fio->io_fd,
    534 				    EVFILT_WRITE, EV_DISABLE, 0, 0,
    535 				    (uintptr_t)fio);
    536 				fio->stat &= ~FIO_WR;
    537 				nchanges++;
    538 			}
    539 			assert(nchanges <= pfctrl->nfds);
    540 		}
    541 
    542 		ndone = kevent(pu->pu_kq, pfctrl->evs, nchanges,
    543 		    pfctrl->evs, pfctrl->nfds+1, pu->pu_ml_timep);
    544 
    545 		if (ndone == -1) {
    546 			if (errno != EINTR)
    547 				goto out;
    548 			else
    549 				continue;
    550 		}
    551 
    552 		/* uoptimize */
    553 		if (ndone == 0)
    554 			continue;
    555 
    556 		/* iterate over the results */
    557 		for (curev = pfctrl->evs; ndone--; curev++) {
    558 			/* get & possibly dispatch events from kernel */
    559 			if (curev->ident == puffsfd) {
    560 				if (puffs_req_handle(pgr, ppr, 0) == -1)
    561 					goto out;
    562 				continue;
    563 			}
    564 
    565 			fio = (void *)curev->udata;
    566 			if (curev->flags & EV_ERROR) {
    567 				assert(curev->filter == EVFILT_WRITE);
    568 				fio->stat &= ~FIO_WR;
    569 
    570 				/* XXX: how to know if it's a transient error */
    571 				puffs_framev_writeclose(pu, fio,
    572 				    (int)curev->data);
    573 				continue;
    574 			}
    575 
    576 			if (curev->filter == EVFILT_READ) {
    577 				if (curev->flags & EV_EOF)
    578 					puffs_framev_readclose(pu, fio,
    579 					    ECONNRESET);
    580 				else
    581 					puffs_framev_input(pu, pfctrl,
    582 					    fio, ppr);
    583 
    584 			} else if (curev->filter == EVFILT_WRITE) {
    585 				if (curev->flags & EV_EOF)
    586 					puffs_framev_writeclose(pu, fio,
    587 					    ECONNRESET);
    588 				else
    589 					puffs_framev_output(pu, pfctrl, fio);
    590 			}
    591 		}
    592 
    593 		/*
    594 		 * Really free fd's now that we don't have references
    595 		 * to them.
    596 		 */
    597 		while ((fio = LIST_FIRST(&pfctrl->fb_ios_rmlist)) != NULL) {
    598 			LIST_REMOVE(fio, fio_entries);
    599 			free(fio);
    600 		}
    601 	}
    602 	errno = 0;
    603 	puffs_req_putput(ppr);
    604 
    605  out:
    606 	/* store the real error for a while */
    607 	sverrno = errno;
    608 
    609 	if (ppr)
    610 		puffs_req_destroyput(ppr);
    611 	if (pgr)
    612 		puffs_req_destroyget(pgr);
    613 
    614 	errno = sverrno;
    615 	if (errno)
    616 		return -1;
    617 	else
    618 		return 0;
    619 }
    620