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