puffs.c revision 1.53 1 /* $NetBSD: puffs.c,v 1.53 2007/06/11 06:13:34 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.53 2007/06/11 06:13:34 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 <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
302 preq->preq_setbacks |= whatback & PUFFS_SETBACK_MASK;
303 }
304
305 int
306 puffs_mount(struct puffs_usermount *pu, const char *dir, int mntflags,
307 void *cookie)
308 {
309
310 #if 1
311 /* XXXkludgehere */
312 /* kauth doesn't provide this service any longer */
313 if (geteuid() != 0)
314 mntflags |= MNT_NOSUID | MNT_NODEV;
315 #endif
316
317 pu->pu_kargs.pa_root_cookie = cookie;
318 if (mount(MOUNT_PUFFS, dir, mntflags, &pu->pu_kargs) == -1)
319 return -1;
320 PU_SETSTATE(pu, PUFFS_STATE_RUNNING);
321
322 return 0;
323 }
324
325 struct puffs_usermount *
326 _puffs_init(int develv, struct puffs_ops *pops, const char *puffsname,
327 void *priv, uint32_t pflags)
328 {
329 struct puffs_usermount *pu;
330 struct puffs_kargs *pargs;
331 int fd;
332
333 if (develv != PUFFS_DEVEL_LIBVERSION) {
334 warnx("puffs_init: mounting with lib version %d, need %d",
335 develv, PUFFS_DEVEL_LIBVERSION);
336 errno = EINVAL;
337 return NULL;
338 }
339
340 fd = open("/dev/puffs", O_RDONLY);
341 if (fd == -1) {
342 warnx("puffs_init: cannot open /dev/puffs");
343 return NULL;
344 }
345 if (fd <= 2)
346 warnx("puffs_init: device fd %d (<= 2), sure this is "
347 "what you want?", fd);
348
349 pu = malloc(sizeof(struct puffs_usermount));
350 if (pu == NULL)
351 goto failfree;
352 memset(pu, 0, sizeof(struct puffs_usermount));
353
354 pargs = &pu->pu_kargs;
355 pargs->pa_vers = PUFFSDEVELVERS | PUFFSVERSION;
356 pargs->pa_flags = PUFFS_FLAG_KERN(pflags);
357 pargs->pa_fd = fd;
358 fillvnopmask(pops, pargs->pa_vnopmask);
359 (void)strlcpy(pargs->pa_name, puffsname, sizeof(pargs->pa_name));
360
361 puffs_zerostatvfs(&pargs->pa_svfsb);
362 pargs->pa_root_cookie = NULL;
363 pargs->pa_root_vtype = VDIR;
364 pargs->pa_root_vsize = 0;
365 pargs->pa_root_rdev = 0;
366
367 pu->pu_flags = pflags;
368 pu->pu_ops = *pops;
369 free(pops); /* XXX */
370
371 pu->pu_privdata = priv;
372 pu->pu_cc_stacksize = PUFFS_CC_STACKSIZE_DEFAULT;
373 LIST_INIT(&pu->pu_pnodelst);
374 LIST_INIT(&pu->pu_framectrl.fb_ios);
375 LIST_INIT(&pu->pu_ccnukelst);
376
377 /* defaults for some user-settable translation functions */
378 pu->pu_cmap = NULL; /* identity translation */
379
380 pu->pu_pathbuild = puffs_stdpath_buildpath;
381 pu->pu_pathfree = puffs_stdpath_freepath;
382 pu->pu_pathcmp = puffs_stdpath_cmppath;
383 pu->pu_pathtransform = NULL;
384 pu->pu_namemod = NULL;
385
386 PU_SETSTATE(pu, PUFFS_STATE_BEFOREMOUNT);
387
388 return pu;
389
390 failfree:
391 /* can't unmount() from here for obvious reasons */
392 close(fd);
393 free(pu);
394 return NULL;
395 }
396
397 /*
398 * XXX: there's currently no clean way to request unmount from
399 * within the user server, so be very brutal about it.
400 */
401 /*ARGSUSED1*/
402 int
403 puffs_exit(struct puffs_usermount *pu, int force)
404 {
405 struct puffs_node *pn;
406
407 force = 1; /* currently */
408
409 if (pu->pu_kargs.pa_fd)
410 close(pu->pu_kargs.pa_fd);
411
412 while ((pn = LIST_FIRST(&pu->pu_pnodelst)) != NULL)
413 puffs_pn_put(pn);
414
415 puffs_framev_exit(pu);
416 if (pu->pu_haskq)
417 close(pu->pu_kq);
418 free(pu);
419
420 return 0; /* always succesful for now, WILL CHANGE */
421 }
422
423 int
424 puffs_mainloop(struct puffs_usermount *pu, int flags)
425 {
426 struct puffs_getreq *pgr = NULL;
427 struct puffs_putreq *ppr = NULL;
428 struct puffs_framectrl *pfctrl = &pu->pu_framectrl;
429 struct puffs_fctrl_io *fio;
430 struct kevent *curev, *newevs;
431 size_t nchanges;
432 int puffsfd, sverrno;
433 int ndone;
434
435 assert(puffs_getstate(pu) >= PUFFS_STATE_RUNNING);
436
437 pgr = puffs_req_makeget(pu, puffs_getmaxreqlen(pu), 0);
438 if (pgr == NULL)
439 goto out;
440
441 ppr = puffs_req_makeput(pu);
442 if (ppr == NULL)
443 goto out;
444
445 newevs = realloc(pfctrl->evs, (2*pfctrl->nfds+1)*sizeof(struct kevent));
446 if (newevs == NULL)
447 goto out;
448 pfctrl->evs = newevs;
449
450 if ((flags & PUFFSLOOP_NODAEMON) == 0)
451 if (daemon(1, 0) == -1)
452 goto out;
453 pu->pu_state |= PU_INLOOP;
454
455 pu->pu_kq = kqueue();
456 if (pu->pu_kq == -1)
457 goto out;
458 pu->pu_haskq = 1;
459
460 curev = pfctrl->evs;
461 LIST_FOREACH(fio, &pfctrl->fb_ios, fio_entries) {
462 EV_SET(curev, fio->io_fd, EVFILT_READ, EV_ADD,
463 0, 0, (uintptr_t)fio);
464 curev++;
465 EV_SET(curev, fio->io_fd, EVFILT_WRITE, EV_ADD | EV_DISABLE,
466 0, 0, (uintptr_t)fio);
467 curev++;
468 }
469 puffsfd = puffs_getselectable(pu);
470 EV_SET(curev, puffsfd, EVFILT_READ, EV_ADD, 0, 0, 0);
471 if (kevent(pu->pu_kq, pfctrl->evs, 2*pfctrl->nfds+1,
472 NULL, 0, NULL) == -1)
473 goto out;
474
475 while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
476 if (pu->pu_ml_lfn)
477 pu->pu_ml_lfn(pu);
478
479 /* micro optimization: skip kevent syscall if possible */
480 if (pfctrl->nfds == 0 && pu->pu_ml_timep == NULL
481 && (pu->pu_state & PU_ASYNCFD) == 0) {
482 if (puffs_req_handle(pgr, ppr, 0) == -1)
483 goto out;
484 if (puffs_req_putput(ppr) == -1)
485 goto out;
486 puffs_req_resetput(ppr);
487 continue;
488 }
489 /* else: do full processing */
490
491 /*
492 * Do this here, because:
493 * a) loopfunc might generate some results
494 * b) it's still "after" event handling (except for round 1)
495 */
496 if (puffs_req_putput(ppr) == -1)
497 goto out;
498 puffs_req_resetput(ppr);
499
500 /*
501 * Build list of which to enable/disable in writecheck.
502 * Don't bother worrying about O(n) for now.
503 */
504 nchanges = 0;
505 LIST_FOREACH(fio, &pfctrl->fb_ios, fio_entries) {
506 if (fio->stat & FIO_WRGONE)
507 continue;
508
509 /*
510 * Try to write out everything to avoid the
511 * need for enabling EVFILT_WRITE. The likely
512 * case is that we can fit everything into the
513 * socket buffer.
514 */
515 if (puffs_framev_output(pu, pfctrl, fio)) {
516 /* need kernel notify? (error condition) */
517 if (puffs_req_putput(ppr) == -1)
518 goto out;
519 puffs_req_resetput(ppr);
520 }
521
522 /* en/disable write checks for kqueue as needed */
523 assert((FIO_EN_WRITE(fio) && FIO_RM_WRITE(fio)) == 0);
524 if (FIO_EN_WRITE(fio)) {
525 EV_SET(&pfctrl->evs[nchanges], fio->io_fd,
526 EVFILT_WRITE, EV_ENABLE, 0, 0,
527 (uintptr_t)fio);
528 fio->stat |= FIO_WR;
529 nchanges++;
530 }
531 if (FIO_RM_WRITE(fio)) {
532 EV_SET(&pfctrl->evs[nchanges], fio->io_fd,
533 EVFILT_WRITE, EV_DISABLE, 0, 0,
534 (uintptr_t)fio);
535 fio->stat &= ~FIO_WR;
536 nchanges++;
537 }
538 assert(nchanges <= pfctrl->nfds);
539 }
540
541 ndone = kevent(pu->pu_kq, pfctrl->evs, nchanges,
542 pfctrl->evs, pfctrl->nfds+1, pu->pu_ml_timep);
543
544 if (ndone == -1) {
545 if (errno != EINTR)
546 goto out;
547 else
548 continue;
549 }
550
551 /* uoptimize */
552 if (ndone == 0)
553 continue;
554
555 /* iterate over the results */
556 for (curev = pfctrl->evs; ndone--; curev++) {
557 /* get & possibly dispatch events from kernel */
558 if (curev->ident == puffsfd) {
559 if (puffs_req_handle(pgr, ppr, 0) == -1)
560 goto out;
561 continue;
562 }
563
564 fio = (void *)curev->udata;
565 if (curev->flags & EV_ERROR) {
566 assert(curev->filter == EVFILT_WRITE);
567 fio->stat &= ~FIO_WR;
568
569 /* XXX: how to know if it's a transient error */
570 puffs_framev_writeclose(pu, fio,
571 (int)curev->data);
572 continue;
573 }
574
575 if (curev->filter == EVFILT_READ) {
576 if (curev->flags & EV_EOF)
577 puffs_framev_readclose(pu, fio,
578 ECONNRESET);
579 else
580 puffs_framev_input(pu, pfctrl,
581 fio, ppr);
582
583 } else if (curev->filter == EVFILT_WRITE) {
584 if (curev->flags & EV_EOF)
585 puffs_framev_writeclose(pu, fio,
586 ECONNRESET);
587 else
588 puffs_framev_output(pu, pfctrl, fio);
589 }
590 }
591
592 /*
593 * Really free fd's now that we don't have references
594 * to them.
595 */
596 while ((fio = LIST_FIRST(&pfctrl->fb_ios_rmlist)) != NULL) {
597 LIST_REMOVE(fio, fio_entries);
598 free(fio);
599 }
600 }
601 errno = 0;
602 puffs_req_putput(ppr);
603
604 out:
605 /* store the real error for a while */
606 sverrno = errno;
607
608 if (ppr)
609 puffs_req_destroyput(ppr);
610 if (pgr)
611 puffs_req_destroyget(pgr);
612
613 errno = sverrno;
614 if (errno)
615 return -1;
616 else
617 return 0;
618 }
619