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