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