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