puffs.c revision 1.45 1 /* $NetBSD: puffs.c,v 1.45 2007/05/11 21:42:42 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 * 3. The name of the company nor the name of the author may be used to
19 * endorse or promote products derived from this software without specific
20 * prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
23 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #if !defined(lint)
37 __RCSID("$NetBSD: puffs.c,v 1.45 2007/05/11 21:42:42 pooka Exp $");
38 #endif /* !lint */
39
40 #include <sys/param.h>
41 #include <sys/mount.h>
42
43 #include <assert.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <mntopts.h>
48 #include <puffs.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <syslog.h>
53 #include <unistd.h>
54
55 #include "puffs_priv.h"
56
57 /* Most file systems want this for opts, so just give it to them */
58 const struct mntopt puffsmopts[] = {
59 MOPT_STDOPTS,
60 PUFFSMOPT_STD,
61 MOPT_NULL,
62 };
63
64 #define FILLOP(lower, upper) \
65 do { \
66 if (pops->puffs_node_##lower) \
67 opmask[PUFFS_VN_##upper] = 1; \
68 } while (/*CONSTCOND*/0)
69 static void
70 fillvnopmask(struct puffs_ops *pops, uint8_t *opmask)
71 {
72
73 memset(opmask, 0, PUFFS_VN_MAX);
74
75 FILLOP(create, CREATE);
76 FILLOP(mknod, MKNOD);
77 FILLOP(open, OPEN);
78 FILLOP(close, CLOSE);
79 FILLOP(access, ACCESS);
80 FILLOP(getattr, GETATTR);
81 FILLOP(setattr, SETATTR);
82 FILLOP(poll, POLL); /* XXX: not ready in kernel */
83 FILLOP(mmap, MMAP);
84 FILLOP(fsync, FSYNC);
85 FILLOP(seek, SEEK);
86 FILLOP(remove, REMOVE);
87 FILLOP(link, LINK);
88 FILLOP(rename, RENAME);
89 FILLOP(mkdir, MKDIR);
90 FILLOP(rmdir, RMDIR);
91 FILLOP(symlink, SYMLINK);
92 FILLOP(readdir, READDIR);
93 FILLOP(readlink, READLINK);
94 FILLOP(reclaim, RECLAIM);
95 FILLOP(inactive, INACTIVE);
96 FILLOP(print, PRINT);
97 FILLOP(read, READ);
98 FILLOP(write, WRITE);
99
100 /* XXX: not implemented in the kernel */
101 FILLOP(getextattr, GETEXTATTR);
102 FILLOP(setextattr, SETEXTATTR);
103 FILLOP(listextattr, LISTEXTATTR);
104 }
105 #undef FILLOP
106
107 int
108 puffs_getselectable(struct puffs_usermount *pu)
109 {
110
111 return pu->pu_kargs.pa_fd;
112 }
113
114 int
115 puffs_setblockingmode(struct puffs_usermount *pu, int mode)
116 {
117 int x;
118
119 x = mode;
120 return ioctl(pu->pu_kargs.pa_fd, FIONBIO, &x);
121 }
122
123 int
124 puffs_getstate(struct puffs_usermount *pu)
125 {
126
127 return pu->pu_state & PU_STATEMASK;
128 }
129
130 void
131 puffs_setstacksize(struct puffs_usermount *pu, size_t ss)
132 {
133
134 pu->pu_cc_stacksize = ss;
135 }
136
137 struct puffs_pathobj *
138 puffs_getrootpathobj(struct puffs_usermount *pu)
139 {
140 struct puffs_node *pnr;
141
142 pnr = pu->pu_pn_root;
143 if (pnr == NULL) {
144 errno = ENOENT;
145 return NULL;
146 }
147
148 return &pnr->pn_po;
149 }
150
151 void
152 puffs_setroot(struct puffs_usermount *pu, struct puffs_node *pn)
153 {
154
155 pu->pu_pn_root = pn;
156 }
157
158 struct puffs_node *
159 puffs_getroot(struct puffs_usermount *pu)
160 {
161
162 return pu->pu_pn_root;
163 }
164
165 void *
166 puffs_getspecific(struct puffs_usermount *pu)
167 {
168
169 return pu->pu_privdata;
170 }
171
172 size_t
173 puffs_getmaxreqlen(struct puffs_usermount *pu)
174 {
175
176 return pu->pu_kargs.pa_maxreqlen;
177 }
178
179 void
180 puffs_setmaxreqlen(struct puffs_usermount *pu, size_t reqlen)
181 {
182
183 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
184 warnx("puffs_setmaxreqlen: call has effect only "
185 "before mount\n");
186
187 pu->pu_kargs.pa_maxreqlen = reqlen;
188 }
189
190 void
191 puffs_setfhsize(struct puffs_usermount *pu, size_t fhsize, int flags)
192 {
193
194 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
195 warnx("puffs_setfhsize: call has effect only before mount\n");
196
197 pu->pu_kargs.pa_fhsize = fhsize;
198 pu->pu_kargs.pa_fhflags = flags;
199 }
200
201 void
202 puffs_setncookiehash(struct puffs_usermount *pu, int nhash)
203 {
204
205 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
206 warnx("puffs_setfhsize: call has effect only before mount\n");
207
208 pu->pu_kargs.pa_nhashbuckets = nhash;
209 }
210
211 void
212 puffs_set_pathbuild(struct puffs_usermount *pu, pu_pathbuild_fn fn)
213 {
214
215 pu->pu_pathbuild = fn;
216 }
217
218 void
219 puffs_set_pathtransform(struct puffs_usermount *pu, pu_pathtransform_fn fn)
220 {
221
222 pu->pu_pathtransform = fn;
223 }
224
225 void
226 puffs_set_pathcmp(struct puffs_usermount *pu, pu_pathcmp_fn fn)
227 {
228
229 pu->pu_pathcmp = fn;
230 }
231
232 void
233 puffs_set_pathfree(struct puffs_usermount *pu, pu_pathfree_fn fn)
234 {
235
236 pu->pu_pathfree = fn;
237 }
238
239 void
240 puffs_set_namemod(struct puffs_usermount *pu, pu_namemod_fn fn)
241 {
242
243 pu->pu_namemod = fn;
244 }
245
246 void
247 puffs_setback(struct puffs_cc *pcc, int whatback)
248 {
249 struct puffs_req *preq = pcc->pcc_preq;
250
251 assert(PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN && (
252 preq->preq_optype == PUFFS_VN_OPEN ||
253 preq->preq_optype == PUFFS_VN_MMAP ||
254 preq->preq_optype == PUFFS_VN_REMOVE ||
255 preq->preq_optype == PUFFS_VN_RMDIR));
256
257 preq->preq_setbacks |= whatback & PUFFS_SETBACK_MASK;
258 }
259
260 int
261 puffs_domount(struct puffs_usermount *pu, const char *dir, int mntflags)
262 {
263
264 #if 1
265 /* XXXkludgehere */
266 /* kauth doesn't provide this service any longer */
267 if (geteuid() != 0)
268 mntflags |= MNT_NOSUID | MNT_NODEV;
269 #endif
270
271 if (mount(MOUNT_PUFFS, dir, mntflags, &pu->pu_kargs) == -1)
272 return -1;
273 PU_SETSTATE(pu, PUFFS_STATE_MOUNTING);
274
275 return 0;
276 }
277
278 struct puffs_usermount *
279 _puffs_init(int develv, struct puffs_ops *pops, const char *puffsname,
280 void *priv, uint32_t pflags)
281 {
282 struct puffs_usermount *pu;
283 struct puffs_kargs *pargs;
284 int fd;
285
286 if (develv != PUFFS_DEVEL_LIBVERSION) {
287 warnx("puffs_mount: mounting with lib version %d, need %d",
288 develv, PUFFS_DEVEL_LIBVERSION);
289 errno = EINVAL;
290 return NULL;
291 }
292
293 fd = open("/dev/puffs", O_RDONLY);
294 if (fd == -1)
295 return NULL;
296 if (fd <= 2)
297 warnx("puffs_mount: device fd %d (<= 2), sure this is "
298 "what you want?", fd);
299
300 pu = malloc(sizeof(struct puffs_usermount));
301 if (pu == NULL)
302 goto failfree;
303
304 pargs = &pu->pu_kargs;
305 memset(pargs, 0, sizeof(struct puffs_kargs));
306 pargs->pa_vers = PUFFSDEVELVERS | PUFFSVERSION;
307 pargs->pa_flags = PUFFS_FLAG_KERN(pflags);
308 pargs->pa_fd = fd;
309 fillvnopmask(pops, pargs->pa_vnopmask);
310 (void)strlcpy(pargs->pa_name, puffsname, sizeof(pargs->pa_name));
311
312 pu->pu_flags = pflags;
313 pu->pu_ops = *pops;
314 free(pops); /* XXX */
315
316 pu->pu_privdata = priv;
317 pu->pu_cc_stacksize = PUFFS_CC_STACKSIZE_DEFAULT;
318 LIST_INIT(&pu->pu_pnodelst);
319 LIST_INIT(&pu->pu_framectrl.fb_ios);
320
321 /* defaults for some user-settable translation functions */
322 pu->pu_cmap = NULL; /* identity translation */
323
324 pu->pu_pathbuild = puffs_stdpath_buildpath;
325 pu->pu_pathfree = puffs_stdpath_freepath;
326 pu->pu_pathcmp = puffs_stdpath_cmppath;
327 pu->pu_pathtransform = NULL;
328 pu->pu_namemod = NULL;
329
330 PU_SETSTATE(pu, PUFFS_STATE_BEFOREMOUNT);
331
332 return pu;
333
334 failfree:
335 /* can't unmount() from here for obvious reasons */
336 close(fd);
337 free(pu);
338 return NULL;
339 }
340
341 struct puffs_usermount *
342 _puffs_mount(int develv, struct puffs_ops *pops, const char *dir, int mntflags,
343 const char *puffsname, void *priv, uint32_t pflags)
344 {
345 struct puffs_usermount *pu;
346 int sverrno;
347
348 pu = _puffs_init(develv, pops, puffsname, priv, pflags);
349 if (pu == NULL)
350 return NULL;
351
352 if (puffs_domount(pu, dir, mntflags) == -1) {
353 sverrno = errno;
354 puffs_exit(pu, 1);
355 errno = sverrno;
356 return NULL;
357 }
358
359 return pu;
360 }
361
362 int
363 puffs_start(struct puffs_usermount *pu, void *rootcookie, struct statvfs *sbp)
364 {
365 struct puffs_startreq sreq;
366
367 memset(&sreq, 0, sizeof(struct puffs_startreq));
368 sreq.psr_cookie = rootcookie;
369 sreq.psr_sb = *sbp;
370
371 /* tell kernel we're flying */
372 if (ioctl(pu->pu_kargs.pa_fd, PUFFSSTARTOP, &sreq) == -1)
373 return -1;
374
375 PU_SETSTATE(pu, PUFFS_STATE_RUNNING);
376
377 return 0;
378 }
379
380 /*
381 * XXX: there's currently no clean way to request unmount from
382 * within the user server, so be very brutal about it.
383 */
384 /*ARGSUSED1*/
385 int
386 puffs_exit(struct puffs_usermount *pu, int force)
387 {
388 struct puffs_node *pn;
389
390 force = 1; /* currently */
391
392 if (pu->pu_kargs.pa_fd)
393 close(pu->pu_kargs.pa_fd);
394
395 while ((pn = LIST_FIRST(&pu->pu_pnodelst)) != NULL)
396 puffs_pn_put(pn);
397
398 puffs_framebuf_exit(pu);
399 if (pu->pu_haskq)
400 close(pu->pu_kq);
401 free(pu);
402
403 return 0; /* always succesful for now, WILL CHANGE */
404 }
405
406 /*
407 * XXX: should deal with errors way way way better
408 */
409 int
410 puffs_mainloop(struct puffs_usermount *pu, int flags)
411 {
412 struct puffs_getreq *pgr = NULL;
413 struct puffs_putreq *ppr = NULL;
414 struct puffs_framectrl *pfctrl = &pu->pu_framectrl;
415 struct puffs_fctrl_io *fio;
416 struct kevent *curev, *newevs;
417 size_t nchanges;
418 int puffsfd, sverrno;
419 int ndone;
420
421 assert(puffs_getstate(pu) >= PUFFS_STATE_RUNNING);
422
423 pgr = puffs_req_makeget(pu, puffs_getmaxreqlen(pu), 0);
424 if (pgr == NULL)
425 goto out;
426
427 ppr = puffs_req_makeput(pu);
428 if (ppr == NULL)
429 goto out;
430
431 newevs = realloc(pfctrl->evs, (2*pfctrl->nfds+1)*sizeof(struct kevent));
432 if (newevs == NULL)
433 goto out;
434 pfctrl->evs = newevs;
435
436 if ((flags & PUFFSLOOP_NODAEMON) == 0)
437 if (daemon(1, 0) == -1)
438 goto out;
439 pu->pu_state |= PU_INLOOP;
440
441 pu->pu_kq = kqueue();
442 if (pu->pu_kq == -1)
443 goto out;
444 pu->pu_haskq = 1;
445
446 curev = pfctrl->evs;
447 LIST_FOREACH(fio, &pfctrl->fb_ios, fio_entries) {
448 EV_SET(curev, fio->io_fd, EVFILT_READ, EV_ADD,
449 0, 0, (uintptr_t)fio);
450 curev++;
451 EV_SET(curev, fio->io_fd, EVFILT_WRITE, EV_ADD | EV_DISABLE,
452 0, 0, (uintptr_t)fio);
453 curev++;
454 }
455 puffsfd = puffs_getselectable(pu);
456 EV_SET(curev, puffsfd, EVFILT_READ, EV_ADD, 0, 0, 0);
457 if (kevent(pu->pu_kq, pfctrl->evs, 2*pfctrl->nfds+1,
458 NULL, 0, NULL) == -1)
459 goto out;
460
461 while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
462 if (pfctrl->lfb)
463 pfctrl->lfb(pu);
464
465 /*
466 * Build list of which to enable/disable in writecheck.
467 * Don't bother worrying about O(n) for now.
468 */
469 nchanges = 0;
470 LIST_FOREACH(fio, &pfctrl->fb_ios, fio_entries) {
471 /*
472 * Try to write out everything to avoid the
473 * need for enabling EVFILT_WRITE. The likely
474 * case is that we can fit everything into the
475 * socket buffer.
476 */
477 if (puffs_framebuf_output(pu, pfctrl, fio) == -1)
478 goto out;
479
480 assert((FIO_EN_WRITE(fio) && FIO_RM_WRITE(fio)) == 0);
481 if (FIO_EN_WRITE(fio)) {
482 EV_SET(&pfctrl->ch_evs[nchanges], fio->io_fd,
483 EVFILT_WRITE, EV_ENABLE, 0, 0,
484 (uintptr_t)fio);
485 fio->wrstat = 1; /* XXX: not before call */
486 nchanges++;
487 }
488 if (FIO_RM_WRITE(fio)) {
489 EV_SET(&pfctrl->ch_evs[nchanges], fio->io_fd,
490 EVFILT_WRITE, EV_DISABLE, 0, 0,
491 (uintptr_t)fio);
492 fio->wrstat = 0; /* XXX: not before call */
493 nchanges++;
494 }
495 }
496
497 ndone = kevent(pu->pu_kq, pfctrl->ch_evs, nchanges,
498 pfctrl->evs, pfctrl->nfds+1, NULL);
499 if (ndone == -1)
500 goto out;
501
502 /* XXX: handle errors */
503
504 /* iterate over the results */
505 for (curev = pfctrl->evs; ndone--; curev++) {
506 /* get & possibly dispatch events from kernel */
507 if (curev->ident == puffsfd) {
508 if (puffs_req_handle(pgr, ppr, 0) == -1)
509 goto out;
510 continue;
511 }
512
513 if (curev->filter == EVFILT_READ) {
514 if (puffs_framebuf_input(pu, pfctrl,
515 (void *)curev->udata, ppr) == -1)
516 goto out;
517 }
518
519 if (curev->filter == EVFILT_WRITE) {
520 if (puffs_framebuf_output(pu, pfctrl,
521 (void *)curev->udata) == -1)
522 goto out;
523 }
524 }
525
526 /* stuff all replies from both of the above into kernel */
527 if (puffs_req_putput(ppr) == -1)
528 goto out;
529 puffs_req_resetput(ppr);
530 }
531 errno = 0;
532
533 out:
534 /* store the real error for a while */
535 sverrno = errno;
536
537 if (ppr)
538 puffs_req_destroyput(ppr);
539 if (pgr)
540 puffs_req_destroyget(pgr);
541
542 errno = sverrno;
543 if (errno)
544 return -1;
545 else
546 return 0;
547 }
548