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