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