puffs.c revision 1.71 1 /* $NetBSD: puffs.c,v 1.71 2007/10/28 18:40:30 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.71 2007/10/28 18:40:30 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_set_prepost(struct puffs_usermount *pu,
316 pu_prepost_fn pre, pu_prepost_fn pst)
317 {
318
319 pu->pu_oppre = pre;
320 pu->pu_oppost = pst;
321 }
322
323 void
324 puffs_setback(struct puffs_cc *pcc, int whatback)
325 {
326 struct puffs_req *preq = pcc->pcc_preq;
327
328 assert(PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN && (
329 preq->preq_optype == PUFFS_VN_OPEN ||
330 preq->preq_optype == PUFFS_VN_MMAP ||
331 preq->preq_optype == PUFFS_VN_REMOVE ||
332 preq->preq_optype == PUFFS_VN_RMDIR ||
333 preq->preq_optype == PUFFS_VN_INACTIVE));
334
335 preq->preq_setbacks |= whatback & PUFFS_SETBACK_MASK;
336 }
337
338 int
339 puffs_mount(struct puffs_usermount *pu, const char *dir, int mntflags,
340 void *cookie)
341 {
342 char rp[MAXPATHLEN];
343 int rv;
344
345 #if 1
346 /* XXXkludgehere */
347 /* kauth doesn't provide this service any longer */
348 if (geteuid() != 0)
349 mntflags |= MNT_NOSUID | MNT_NODEV;
350 #endif
351
352 if (realpath(dir, rp) == NULL)
353 return -1;
354
355 if (strcmp(dir, rp) != 0) {
356 warnx("puffs_mount: \"%s\" is a relative path.", dir);
357 warnx("puffs_mount: using \"%s\" instead.", rp);
358 }
359
360 pu->pu_kargp->pa_root_cookie = cookie;
361 if ((rv = mount(MOUNT_PUFFS, rp, mntflags,
362 pu->pu_kargp, sizeof(struct puffs_kargs))) == -1)
363 goto out;
364 #if 0
365 if ((rv = ioctl(pu->pu_fd, PUFFSREQSIZEOP, &pu->pu_maxreqlen)) == -1)
366 goto out;
367 #endif
368 PU_SETSTATE(pu, PUFFS_STATE_RUNNING);
369
370 out:
371 free(pu->pu_kargp);
372 pu->pu_kargp = NULL;
373
374 return rv;
375 }
376
377 struct puffs_usermount *
378 _puffs_init(int develv, struct puffs_ops *pops, const char *mntfromname,
379 const char *puffsname, void *priv, uint32_t pflags)
380 {
381 struct puffs_usermount *pu;
382 struct puffs_kargs *pargs;
383 int sverrno, fd;
384
385 if (develv != PUFFS_DEVEL_LIBVERSION) {
386 warnx("puffs_init: mounting with lib version %d, need %d",
387 develv, PUFFS_DEVEL_LIBVERSION);
388 errno = EINVAL;
389 return NULL;
390 }
391
392 fd = open(_PATH_PUFFS, O_RDONLY);
393 if (fd == -1) {
394 warnx("puffs_init: cannot open %s", _PATH_PUFFS);
395 return NULL;
396 }
397 if (fd <= 2)
398 warnx("puffs_init: device fd %d (<= 2), sure this is "
399 "what you want?", fd);
400
401 pu = malloc(sizeof(struct puffs_usermount));
402 if (pu == NULL)
403 goto failfree;
404 memset(pu, 0, sizeof(struct puffs_usermount));
405
406 pargs = pu->pu_kargp = malloc(sizeof(struct puffs_kargs));
407 if (pargs == NULL)
408 goto failfree;
409 memset(pargs, 0, sizeof(struct puffs_kargs));
410
411 pargs->pa_vers = PUFFSDEVELVERS | PUFFSVERSION;
412 pargs->pa_flags = PUFFS_FLAG_KERN(pflags);
413 pargs->pa_fd = pu->pu_fd = fd;
414 fillvnopmask(pops, pargs->pa_vnopmask);
415 (void)strlcpy(pargs->pa_typename, puffsname,
416 sizeof(pargs->pa_typename));
417 (void)strlcpy(pargs->pa_mntfromname, mntfromname,
418 sizeof(pargs->pa_mntfromname));
419
420 puffs_zerostatvfs(&pargs->pa_svfsb);
421 pargs->pa_root_cookie = NULL;
422 pargs->pa_root_vtype = VDIR;
423 pargs->pa_root_vsize = 0;
424 pargs->pa_root_rdev = 0;
425 pargs->pa_maxmsglen = 0;
426
427 pu->pu_flags = pflags;
428 pu->pu_ops = *pops;
429 free(pops); /* XXX */
430
431 pu->pu_privdata = priv;
432 pu->pu_cc_stackshift = PUFFS_CC_STACKSHIFT_DEFAULT;
433 LIST_INIT(&pu->pu_pnodelst);
434 LIST_INIT(&pu->pu_framectrl.fb_ios);
435 LIST_INIT(&pu->pu_ccnukelst);
436 TAILQ_INIT(&pu->pu_sched);
437 TAILQ_INIT(&pu->pu_exq);
438
439 /* defaults for some user-settable translation functions */
440 pu->pu_cmap = NULL; /* identity translation */
441
442 pu->pu_pathbuild = puffs_stdpath_buildpath;
443 pu->pu_pathfree = puffs_stdpath_freepath;
444 pu->pu_pathcmp = puffs_stdpath_cmppath;
445 pu->pu_pathtransform = NULL;
446 pu->pu_namemod = NULL;
447
448 pu->pu_errnotify = puffs_defaulterror;
449
450 PU_SETSTATE(pu, PUFFS_STATE_BEFOREMOUNT);
451
452 return pu;
453
454 failfree:
455 /* can't unmount() from here for obvious reasons */
456 sverrno = errno;
457 close(fd);
458 free(pu);
459 errno = sverrno;
460 return NULL;
461 }
462
463 /*
464 * XXX: there's currently no clean way to request unmount from
465 * within the user server, so be very brutal about it.
466 */
467 /*ARGSUSED1*/
468 int
469 puffs_exit(struct puffs_usermount *pu, int force)
470 {
471 struct puffs_node *pn;
472
473 force = 1; /* currently */
474
475 if (pu->pu_fd)
476 close(pu->pu_fd);
477
478 while ((pn = LIST_FIRST(&pu->pu_pnodelst)) != NULL)
479 puffs_pn_put(pn);
480
481 puffs_framev_exit(pu);
482 if (pu->pu_state & PU_HASKQ)
483 close(pu->pu_kq);
484 free(pu);
485
486 return 0; /* always succesful for now, WILL CHANGE */
487 }
488
489 int
490 puffs_mainloop(struct puffs_usermount *pu, int flags)
491 {
492 struct puffs_getreq *pgr = NULL;
493 struct puffs_putreq *ppr = NULL;
494 struct puffs_framectrl *pfctrl = &pu->pu_framectrl;
495 struct puffs_fctrl_io *fio;
496 struct puffs_cc *pcc;
497 struct kevent *curev, *newevs;
498 size_t nchanges;
499 int puffsfd, sverrno;
500 int ndone;
501
502 assert(puffs_getstate(pu) >= PUFFS_STATE_RUNNING);
503
504 pgr = puffs_req_makeget(pu, puffs_getmaxreqlen(pu), 0);
505 if (pgr == NULL)
506 goto out;
507
508 ppr = puffs_req_makeput(pu);
509 if (ppr == NULL)
510 goto out;
511
512 newevs = realloc(pfctrl->evs, (2*pfctrl->nfds+1)*sizeof(struct kevent));
513 if (newevs == NULL)
514 goto out;
515 pfctrl->evs = newevs;
516
517 if ((flags & PUFFSLOOP_NODAEMON) == 0)
518 if (daemon(1, 0) == -1)
519 goto out;
520 pu->pu_state |= PU_INLOOP;
521
522 pu->pu_kq = kqueue();
523 if (pu->pu_kq == -1)
524 goto out;
525 pu->pu_state |= PU_HASKQ;
526
527 curev = pfctrl->evs;
528 LIST_FOREACH(fio, &pfctrl->fb_ios, fio_entries) {
529 EV_SET(curev, fio->io_fd, EVFILT_READ, EV_ADD,
530 0, 0, (uintptr_t)fio);
531 curev++;
532 EV_SET(curev, fio->io_fd, EVFILT_WRITE, EV_ADD | EV_DISABLE,
533 0, 0, (uintptr_t)fio);
534 curev++;
535 }
536 puffsfd = puffs_getselectable(pu);
537 EV_SET(curev, puffsfd, EVFILT_READ, EV_ADD, 0, 0, 0);
538 if (kevent(pu->pu_kq, pfctrl->evs, 2*pfctrl->nfds+1,
539 NULL, 0, NULL) == -1)
540 goto out;
541
542 while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
543 if (pu->pu_ml_lfn)
544 pu->pu_ml_lfn(pu);
545
546 /*
547 * Do this here, because:
548 * a) loopfunc might generate some results
549 * b) it's still "after" event handling (except for round 1)
550 */
551 if (puffs_req_putput(ppr) == -1)
552 goto out;
553 puffs_req_resetput(ppr);
554
555 /* micro optimization: skip kevent syscall if possible */
556 if (pfctrl->nfds == 0 && pu->pu_ml_timep == NULL
557 && (pu->pu_state & PU_ASYNCFD) == 0) {
558 if (puffs_req_handle(pgr, ppr, 0) == -1)
559 goto out;
560 continue;
561 }
562
563 /* else: do full processing */
564
565 /*
566 * Build list of which to enable/disable in writecheck.
567 * Don't bother worrying about O(n) for now.
568 */
569 nchanges = 0;
570 LIST_FOREACH(fio, &pfctrl->fb_ios, fio_entries) {
571 if (fio->stat & FIO_WRGONE)
572 continue;
573
574 /*
575 * Try to write out everything to avoid the
576 * need for enabling EVFILT_WRITE. The likely
577 * case is that we can fit everything into the
578 * socket buffer.
579 */
580 if (puffs_framev_output(pu, pfctrl, fio, ppr)) {
581 /* need kernel notify? (error condition) */
582 if (puffs_req_putput(ppr) == -1)
583 goto out;
584 puffs_req_resetput(ppr);
585 }
586
587 /* en/disable write checks for kqueue as needed */
588 assert((FIO_EN_WRITE(fio) && FIO_RM_WRITE(fio)) == 0);
589 if (FIO_EN_WRITE(fio)) {
590 EV_SET(&pfctrl->evs[nchanges], fio->io_fd,
591 EVFILT_WRITE, EV_ENABLE, 0, 0,
592 (uintptr_t)fio);
593 fio->stat |= FIO_WR;
594 nchanges++;
595 }
596 if (FIO_RM_WRITE(fio)) {
597 EV_SET(&pfctrl->evs[nchanges], fio->io_fd,
598 EVFILT_WRITE, EV_DISABLE, 0, 0,
599 (uintptr_t)fio);
600 fio->stat &= ~FIO_WR;
601 nchanges++;
602 }
603 assert(nchanges <= pfctrl->nfds);
604 }
605
606 ndone = kevent(pu->pu_kq, pfctrl->evs, nchanges,
607 pfctrl->evs, pfctrl->nfds+1, pu->pu_ml_timep);
608
609 if (ndone == -1) {
610 if (errno != EINTR)
611 goto out;
612 else
613 continue;
614 }
615
616 /* uoptimize */
617 if (ndone == 0)
618 continue;
619
620 /* iterate over the results */
621 for (curev = pfctrl->evs; ndone--; curev++) {
622 int what;
623
624 /* get & possibly dispatch events from kernel */
625 if (curev->ident == puffsfd) {
626 if (puffs_req_handle(pgr, ppr, 0) == -1)
627 goto out;
628 continue;
629 }
630
631 fio = (void *)curev->udata;
632 if (curev->flags & EV_ERROR) {
633 assert(curev->filter == EVFILT_WRITE);
634 fio->stat &= ~FIO_WR;
635
636 /* XXX: how to know if it's a transient error */
637 puffs_framev_writeclose(pu, fio,
638 (int)curev->data);
639 puffs_framev_notify(fio, PUFFS_FBIO_ERROR);
640 continue;
641 }
642
643 what = 0;
644 if (curev->filter == EVFILT_READ) {
645 puffs_framev_input(pu, pfctrl, fio, ppr);
646 what |= PUFFS_FBIO_READ;
647 }
648
649 else if (curev->filter == EVFILT_WRITE) {
650 puffs_framev_output(pu, pfctrl, fio, ppr);
651 what |= PUFFS_FBIO_WRITE;
652 }
653 if (what)
654 puffs_framev_notify(fio, what);
655 }
656
657 /*
658 * Schedule continuations.
659 */
660 while ((pcc = TAILQ_FIRST(&pu->pu_sched)) != NULL) {
661 TAILQ_REMOVE(&pu->pu_sched, pcc, entries);
662 puffs_goto(pcc);
663 }
664
665 /*
666 * Really free fd's now that we don't have references
667 * to them.
668 */
669 while ((fio = LIST_FIRST(&pfctrl->fb_ios_rmlist)) != NULL) {
670 LIST_REMOVE(fio, fio_entries);
671 free(fio);
672 }
673 }
674 errno = 0;
675 puffs_req_putput(ppr);
676
677 out:
678 /* store the real error for a while */
679 sverrno = errno;
680
681 if (ppr)
682 puffs_req_destroyput(ppr);
683 if (pgr)
684 puffs_req_destroyget(pgr);
685
686 errno = sverrno;
687 if (errno)
688 return -1;
689 else
690 return 0;
691 }
692