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