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