puffs.c revision 1.118 1 /* $NetBSD: puffs.c,v 1.118 2014/10/31 13:56:04 manu 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.118 2014/10/31 13:56:04 manu 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 <pthread.h>
47 #include <puffs.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <syslog.h>
52 #include <unistd.h>
53
54 #include "puffs_priv.h"
55
56 /* Most file systems want this for opts, so just give it to them */
57 const struct mntopt puffsmopts[] = {
58 MOPT_STDOPTS,
59 PUFFSMOPT_STD,
60 MOPT_NULL,
61 };
62
63 pthread_mutex_t pu_lock = PTHREAD_MUTEX_INITIALIZER;
64
65 #define FILLOP(lower, upper) \
66 do { \
67 if (pops->puffs_node_##lower) \
68 opmask[PUFFS_VN_##upper] = 1; \
69 } while (/*CONSTCOND*/0)
70 static void
71 fillvnopmask(struct puffs_ops *pops, struct puffs_kargs *pa)
72 {
73 uint8_t *opmask = pa->pa_vnopmask;
74
75 memset(opmask, 0, sizeof(pa->pa_vnopmask));
76
77 FILLOP(create, CREATE);
78 FILLOP(mknod, MKNOD);
79 FILLOP(open, OPEN);
80 FILLOP(close, CLOSE);
81 FILLOP(access, ACCESS);
82 FILLOP(getattr, GETATTR);
83 FILLOP(setattr, SETATTR);
84 FILLOP(poll, POLL);
85 FILLOP(mmap, MMAP);
86 FILLOP(fsync, FSYNC);
87 FILLOP(seek, SEEK);
88 FILLOP(remove, REMOVE);
89 FILLOP(link, LINK);
90 FILLOP(rename, RENAME);
91 FILLOP(mkdir, MKDIR);
92 FILLOP(rmdir, RMDIR);
93 FILLOP(symlink, SYMLINK);
94 FILLOP(readdir, READDIR);
95 FILLOP(readlink, READLINK);
96 FILLOP(reclaim, RECLAIM);
97 FILLOP(inactive, INACTIVE);
98 FILLOP(print, PRINT);
99 FILLOP(read, READ);
100 FILLOP(write, WRITE);
101 FILLOP(advlock, ADVLOCK);
102 FILLOP(abortop, ABORTOP);
103 FILLOP(pathconf, PATHCONF);
104
105 FILLOP(getextattr, GETEXTATTR);
106 FILLOP(setextattr, SETEXTATTR);
107 FILLOP(listextattr, LISTEXTATTR);
108 FILLOP(deleteextattr, DELETEEXTATTR);
109 FILLOP(fallocate, FALLOCATE);
110 FILLOP(fdiscard, FDISCARD);
111 }
112 #undef FILLOP
113
114 /*
115 * Go over all framev entries and write everything we can. This is
116 * mostly for the benefit of delivering "unmount" to the kernel.
117 */
118 static void
119 finalpush(struct puffs_usermount *pu)
120 {
121 struct puffs_fctrl_io *fio;
122
123 LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
124 if (fio->stat & FIO_WRGONE)
125 continue;
126
127 puffs__framev_output(pu, fio->fctrl, fio);
128 }
129 }
130
131 /*ARGSUSED*/
132 void
133 puffs_kernerr_abort(struct puffs_usermount *pu, uint8_t type,
134 int error, const char *str, puffs_cookie_t cookie)
135 {
136
137 fprintf(stderr, "abort: type %d, error %d, cookie %p (%s)\n",
138 type, error, cookie, str);
139 abort();
140 }
141
142 /*ARGSUSED*/
143 void
144 puffs_kernerr_log(struct puffs_usermount *pu, uint8_t type,
145 int error, const char *str, puffs_cookie_t cookie)
146 {
147
148 syslog(LOG_WARNING, "kernel: type %d, error %d, cookie %p (%s)\n",
149 type, error, cookie, str);
150 }
151
152 int
153 puffs_getselectable(struct puffs_usermount *pu)
154 {
155
156 return pu->pu_fd;
157 }
158
159 uint64_t
160 puffs__nextreq(struct puffs_usermount *pu)
161 {
162 uint64_t rv;
163
164 PU_LOCK();
165 rv = pu->pu_nextreq++ | (uint64_t)1<<63;
166 PU_UNLOCK();
167
168 return rv;
169 }
170
171 int
172 puffs_setblockingmode(struct puffs_usermount *pu, int mode)
173 {
174 int rv, x;
175
176 assert(puffs_getstate(pu) == PUFFS_STATE_RUNNING);
177
178 if (mode != PUFFSDEV_BLOCK && mode != PUFFSDEV_NONBLOCK) {
179 errno = EINVAL;
180 return -1;
181 }
182
183 x = mode;
184 rv = ioctl(pu->pu_fd, FIONBIO, &x);
185
186 if (rv == 0) {
187 if (mode == PUFFSDEV_BLOCK)
188 pu->pu_state &= ~PU_ASYNCFD;
189 else
190 pu->pu_state |= PU_ASYNCFD;
191 }
192
193 return rv;
194 }
195
196 int
197 puffs_getstate(struct puffs_usermount *pu)
198 {
199
200 return pu->pu_state & PU_STATEMASK;
201 }
202
203 void
204 puffs_setstacksize(struct puffs_usermount *pu, size_t ss)
205 {
206 long psize, minsize;
207 int stackshift;
208 int bonus;
209
210 assert(puffs_getstate(pu) == PUFFS_STATE_BEFOREMOUNT);
211
212 psize = sysconf(_SC_PAGESIZE);
213 minsize = 4*psize;
214 if (ss < (size_t)minsize || ss == PUFFS_STACKSIZE_MIN) {
215 if (ss != PUFFS_STACKSIZE_MIN)
216 fprintf(stderr, "puffs_setstacksize: adjusting "
217 "stacksize to minimum %ld\n", minsize);
218 ss = 4*psize;
219 }
220
221 stackshift = -1;
222 bonus = 0;
223 while (ss) {
224 if (ss & 0x1)
225 bonus++;
226 ss >>= 1;
227 stackshift++;
228 }
229 if (bonus > 1) {
230 stackshift++;
231 fprintf(stderr, "puffs_setstacksize: using next power of two: "
232 "%d\n", 1<<stackshift);
233 }
234
235 pu->pu_cc_stackshift = stackshift;
236 }
237
238 struct puffs_pathobj *
239 puffs_getrootpathobj(struct puffs_usermount *pu)
240 {
241 struct puffs_node *pnr;
242
243 pnr = pu->pu_pn_root;
244 if (pnr == NULL) {
245 errno = ENOENT;
246 return NULL;
247 }
248
249 return &pnr->pn_po;
250 }
251
252 void
253 puffs_setroot(struct puffs_usermount *pu, struct puffs_node *pn)
254 {
255
256 pu->pu_pn_root = pn;
257 }
258
259 struct puffs_node *
260 puffs_getroot(struct puffs_usermount *pu)
261 {
262
263 return pu->pu_pn_root;
264 }
265
266 void
267 puffs_setrootinfo(struct puffs_usermount *pu, enum vtype vt,
268 vsize_t vsize, dev_t rdev)
269 {
270 struct puffs_kargs *pargs = pu->pu_kargp;
271
272 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT) {
273 warnx("puffs_setrootinfo: call has effect only "
274 "before mount\n");
275 return;
276 }
277
278 pargs->pa_root_vtype = vt;
279 pargs->pa_root_vsize = vsize;
280 pargs->pa_root_rdev = rdev;
281 }
282
283 void *
284 puffs_getspecific(struct puffs_usermount *pu)
285 {
286
287 return pu->pu_privdata;
288 }
289
290 void
291 puffs_setspecific(struct puffs_usermount *pu, void *privdata)
292 {
293
294 pu->pu_privdata = privdata;
295 }
296
297 void
298 puffs_setmntinfo(struct puffs_usermount *pu,
299 const char *mntfromname, const char *puffsname)
300 {
301 struct puffs_kargs *pargs = pu->pu_kargp;
302
303 (void)strlcpy(pargs->pa_mntfromname, mntfromname,
304 sizeof(pargs->pa_mntfromname));
305 (void)strlcpy(pargs->pa_typename, puffsname,
306 sizeof(pargs->pa_typename));
307 }
308
309 size_t
310 puffs_getmaxreqlen(struct puffs_usermount *pu)
311 {
312
313 return pu->pu_maxreqlen;
314 }
315
316 void
317 puffs_setmaxreqlen(struct puffs_usermount *pu, size_t reqlen)
318 {
319
320 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
321 warnx("puffs_setmaxreqlen: call has effect only "
322 "before mount\n");
323
324 pu->pu_kargp->pa_maxmsglen = reqlen;
325 }
326
327 void
328 puffs_setfhsize(struct puffs_usermount *pu, size_t fhsize, int flags)
329 {
330
331 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
332 warnx("puffs_setfhsize: call has effect only before mount\n");
333
334 pu->pu_kargp->pa_fhsize = fhsize;
335 pu->pu_kargp->pa_fhflags = flags;
336 }
337
338 void
339 puffs_setncookiehash(struct puffs_usermount *pu, int nhash)
340 {
341
342 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
343 warnx("puffs_setfhsize: call has effect only before mount\n");
344
345 pu->pu_kargp->pa_nhashbuckets = nhash;
346 }
347
348 void
349 puffs_set_pathbuild(struct puffs_usermount *pu, pu_pathbuild_fn fn)
350 {
351
352 pu->pu_pathbuild = fn;
353 }
354
355 void
356 puffs_set_pathtransform(struct puffs_usermount *pu, pu_pathtransform_fn fn)
357 {
358
359 pu->pu_pathtransform = fn;
360 }
361
362 void
363 puffs_set_pathcmp(struct puffs_usermount *pu, pu_pathcmp_fn fn)
364 {
365
366 pu->pu_pathcmp = fn;
367 }
368
369 void
370 puffs_set_pathfree(struct puffs_usermount *pu, pu_pathfree_fn fn)
371 {
372
373 pu->pu_pathfree = fn;
374 }
375
376 void
377 puffs_set_namemod(struct puffs_usermount *pu, pu_namemod_fn fn)
378 {
379
380 pu->pu_namemod = fn;
381 }
382
383 void
384 puffs_set_errnotify(struct puffs_usermount *pu, pu_errnotify_fn fn)
385 {
386
387 pu->pu_errnotify = fn;
388 }
389
390 void
391 puffs_set_cmap(struct puffs_usermount *pu, pu_cmap_fn fn)
392 {
393
394 pu->pu_cmap = fn;
395 }
396
397 void
398 puffs_ml_setloopfn(struct puffs_usermount *pu, puffs_ml_loop_fn lfn)
399 {
400
401 pu->pu_ml_lfn = lfn;
402 }
403
404 void
405 puffs_ml_settimeout(struct puffs_usermount *pu, struct timespec *ts)
406 {
407
408 if (ts == NULL) {
409 pu->pu_ml_timep = NULL;
410 } else {
411 pu->pu_ml_timeout = *ts;
412 pu->pu_ml_timep = &pu->pu_ml_timeout;
413 }
414 }
415
416 void
417 puffs_set_prepost(struct puffs_usermount *pu,
418 pu_prepost_fn pre, pu_prepost_fn pst)
419 {
420
421 pu->pu_oppre = pre;
422 pu->pu_oppost = pst;
423 }
424
425 void
426 puffs_setback(struct puffs_cc *pcc, int whatback)
427 {
428 struct puffs_req *preq = puffs__framebuf_getdataptr(pcc->pcc_pb);
429
430 assert(PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN && (
431 preq->preq_optype == PUFFS_VN_OPEN ||
432 preq->preq_optype == PUFFS_VN_MMAP ||
433 preq->preq_optype == PUFFS_VN_REMOVE ||
434 preq->preq_optype == PUFFS_VN_RMDIR ||
435 preq->preq_optype == PUFFS_VN_INACTIVE));
436
437 preq->preq_setbacks |= whatback & PUFFS_SETBACK_MASK;
438 }
439
440 int
441 puffs_daemon(struct puffs_usermount *pu, int nochdir, int noclose)
442 {
443 long int n;
444 int parent, value, fd;
445
446 if (pipe(pu->pu_dpipe) == -1)
447 return -1;
448
449 switch (fork()) {
450 case -1:
451 return -1;
452 case 0:
453 parent = 0;
454 break;
455 default:
456 parent = 1;
457 break;
458 }
459 pu->pu_state |= PU_PUFFSDAEMON;
460
461 if (parent) {
462 close(pu->pu_dpipe[1]);
463 n = read(pu->pu_dpipe[0], &value, sizeof(int));
464 if (n == -1)
465 err(1, "puffs_daemon");
466 if (n != sizeof(value))
467 errx(1, "puffs_daemon got %ld bytes", n);
468 if (value) {
469 errno = value;
470 err(1, "puffs_daemon");
471 }
472 exit(0);
473 } else {
474 if (setsid() == -1)
475 goto fail;
476
477 if (!nochdir)
478 chdir("/");
479
480 if (!noclose) {
481 fd = open(_PATH_DEVNULL, O_RDWR, 0);
482 if (fd == -1)
483 goto fail;
484 dup2(fd, STDIN_FILENO);
485 dup2(fd, STDOUT_FILENO);
486 dup2(fd, STDERR_FILENO);
487 if (fd > STDERR_FILENO)
488 close(fd);
489 }
490 return 0;
491 }
492
493 fail:
494 n = write(pu->pu_dpipe[1], &errno, sizeof(int));
495 assert(n == 4);
496 return -1;
497 }
498
499 static void
500 shutdaemon(struct puffs_usermount *pu, int error)
501 {
502 ssize_t n;
503
504 n = write(pu->pu_dpipe[1], &error, sizeof(int));
505 assert(n == 4);
506 close(pu->pu_dpipe[0]);
507 close(pu->pu_dpipe[1]);
508 pu->pu_state &= ~PU_PUFFSDAEMON;
509 }
510
511 int
512 puffs_mount(struct puffs_usermount *pu, const char *dir, int mntflags,
513 puffs_cookie_t cookie)
514 {
515 int rv, fd, sverrno;
516 char *comfd;
517
518 pu->pu_kargp->pa_root_cookie = cookie;
519
520 /* XXXkludgehere */
521 /* kauth doesn't provide this service any longer */
522 if (geteuid() != 0)
523 mntflags |= MNT_NOSUID | MNT_NODEV;
524
525 /*
526 * Undocumented... Well, documented only here.
527 *
528 * This is used for imaginative purposes. If the env variable is
529 * set, puffs_mount() doesn't do the regular mount procedure.
530 * Rather, it crams the mount data down the comfd and sets comfd as
531 * the puffs descriptor.
532 *
533 * This shouldn't be used unless you can read my mind ( ... or write
534 * it, not to mention execute it, but that's starting to get silly).
535 */
536 if ((comfd = getenv("PUFFS_COMFD")) != NULL) {
537 size_t len;
538
539 if (sscanf(comfd, "%d", &pu->pu_fd) != 1) {
540 errno = EINVAL;
541 rv = -1;
542 goto out;
543 }
544 /* check that what we got at least resembles an fd */
545 if (fcntl(pu->pu_fd, F_GETFL) == -1) {
546 rv = -1;
547 goto out;
548 }
549
550 #define allwrite(buf, len) \
551 do { \
552 ssize_t al_rv; \
553 al_rv = write(pu->pu_fd, buf, len); \
554 if ((size_t)al_rv != len) { \
555 if (al_rv != -1) \
556 errno = EIO; \
557 rv = -1; \
558 goto out; \
559 } \
560 } while (/*CONSTCOND*/0)
561 len = strlen(dir)+1;
562 allwrite(&len, sizeof(len));
563 allwrite(dir, len);
564 len = strlen(pu->pu_kargp->pa_mntfromname)+1;
565 allwrite(&len, sizeof(len));
566 allwrite(pu->pu_kargp->pa_mntfromname, len);
567 allwrite(&mntflags, sizeof(mntflags));
568 len = sizeof(*pu->pu_kargp);
569 allwrite(&len, sizeof(len));
570 allwrite(pu->pu_kargp, sizeof(*pu->pu_kargp));
571 allwrite(&pu->pu_flags, sizeof(pu->pu_flags));
572 #undef allwrite
573
574 rv = 0;
575 } else {
576 char rp[MAXPATHLEN];
577
578 if (realpath(dir, rp) == NULL) {
579 rv = -1;
580 goto out;
581 }
582
583 if (strcmp(dir, rp) != 0) {
584 warnx("puffs_mount: \"%s\" is a relative path.", dir);
585 warnx("puffs_mount: using \"%s\" instead.", rp);
586 }
587
588 fd = open(_PATH_PUFFS, O_RDWR);
589 if (fd == -1) {
590 warnx("puffs_mount: cannot open %s", _PATH_PUFFS);
591 rv = -1;
592 goto out;
593 }
594 if (fd <= 2)
595 warnx("puffs_mount: device fd %d (<= 2), sure this is "
596 "what you want?", fd);
597
598 pu->pu_kargp->pa_fd = pu->pu_fd = fd;
599 if ((rv = mount(MOUNT_PUFFS, rp, mntflags,
600 pu->pu_kargp, sizeof(struct puffs_kargs))) == -1)
601 goto out;
602 }
603
604 PU_SETSTATE(pu, PUFFS_STATE_RUNNING);
605
606 out:
607 if (rv != 0)
608 sverrno = errno;
609 else
610 sverrno = 0;
611 free(pu->pu_kargp);
612 pu->pu_kargp = NULL;
613
614 if (pu->pu_state & PU_PUFFSDAEMON)
615 shutdaemon(pu, sverrno);
616
617 errno = sverrno;
618 return rv;
619 }
620
621 struct puffs_usermount *
622 puffs_init(struct puffs_ops *pops, const char *mntfromname,
623 const char *puffsname, void *priv, uint32_t pflags)
624 {
625 struct puffs_usermount *pu;
626 struct puffs_kargs *pargs;
627 int sverrno;
628
629 if (puffsname == PUFFS_DEFER)
630 puffsname = "n/a";
631 if (mntfromname == PUFFS_DEFER)
632 mntfromname = "n/a";
633 if (priv == PUFFS_DEFER)
634 priv = NULL;
635
636 pu = malloc(sizeof(struct puffs_usermount));
637 if (pu == NULL)
638 goto failfree;
639 memset(pu, 0, sizeof(struct puffs_usermount));
640
641 pargs = pu->pu_kargp = malloc(sizeof(struct puffs_kargs));
642 if (pargs == NULL)
643 goto failfree;
644 memset(pargs, 0, sizeof(struct puffs_kargs));
645
646 pargs->pa_vers = PUFFSVERSION;
647 pargs->pa_flags = PUFFS_FLAG_KERN(pflags);
648 fillvnopmask(pops, pargs);
649 puffs_setmntinfo(pu, mntfromname, puffsname);
650
651 puffs_zerostatvfs(&pargs->pa_svfsb);
652 pargs->pa_root_cookie = NULL;
653 pargs->pa_root_vtype = VDIR;
654 pargs->pa_root_vsize = 0;
655 pargs->pa_root_rdev = 0;
656 pargs->pa_maxmsglen = 0;
657 if (/*CONSTCOND*/ sizeof(time_t) == 4)
658 pargs->pa_time32 = 1;
659 else
660 pargs->pa_time32 = 0;
661
662 pu->pu_flags = pflags;
663 pu->pu_ops = *pops;
664 free(pops); /* XXX */
665
666 pu->pu_privdata = priv;
667 pu->pu_cc_stackshift = PUFFS_CC_STACKSHIFT_DEFAULT;
668 LIST_INIT(&pu->pu_pnodelst);
669 LIST_INIT(&pu->pu_ios);
670 LIST_INIT(&pu->pu_ios_rmlist);
671 LIST_INIT(&pu->pu_ccmagazin);
672 TAILQ_INIT(&pu->pu_sched);
673
674 pu->pu_framectrl[PU_FRAMECTRL_FS].rfb = puffs__fsframe_read;
675 pu->pu_framectrl[PU_FRAMECTRL_FS].wfb = puffs__fsframe_write;
676 pu->pu_framectrl[PU_FRAMECTRL_FS].cmpfb = puffs__fsframe_cmp;
677 pu->pu_framectrl[PU_FRAMECTRL_FS].gotfb = puffs__fsframe_gotframe;
678 pu->pu_framectrl[PU_FRAMECTRL_FS].fdnotfn = puffs_framev_unmountonclose;
679
680 /* defaults for some user-settable translation functions */
681 pu->pu_cmap = NULL; /* identity translation */
682
683 pu->pu_pathbuild = puffs_stdpath_buildpath;
684 pu->pu_pathfree = puffs_stdpath_freepath;
685 pu->pu_pathcmp = puffs_stdpath_cmppath;
686 pu->pu_pathtransform = NULL;
687 pu->pu_namemod = NULL;
688
689 pu->pu_errnotify = puffs_kernerr_log;
690
691 PU_SETSTATE(pu, PUFFS_STATE_BEFOREMOUNT);
692
693 return pu;
694
695 failfree:
696 /* can't unmount() from here for obvious reasons */
697 sverrno = errno;
698 free(pu);
699 errno = sverrno;
700 return NULL;
701 }
702
703 void
704 puffs_cancel(struct puffs_usermount *pu, int error)
705 {
706
707 assert(puffs_getstate(pu) < PUFFS_STATE_RUNNING);
708 shutdaemon(pu, error);
709 free(pu);
710 }
711
712 /*ARGSUSED1*/
713 int
714 puffs_exit(struct puffs_usermount *pu, int unused /* strict compat */)
715 {
716 struct puffs_framebuf *pb;
717 struct puffs_req *preq;
718 void *winp;
719 size_t winlen;
720 int sverrno;
721
722 pb = puffs_framebuf_make();
723 if (pb == NULL) {
724 errno = ENOMEM;
725 return -1;
726 }
727
728 winlen = sizeof(struct puffs_req);
729 if (puffs_framebuf_getwindow(pb, 0, &winp, &winlen) == -1) {
730 sverrno = errno;
731 puffs_framebuf_destroy(pb);
732 errno = sverrno;
733 return -1;
734 }
735 preq = winp;
736
737 preq->preq_buflen = sizeof(struct puffs_req);
738 preq->preq_opclass = PUFFSOP_UNMOUNT;
739 preq->preq_id = puffs__nextreq(pu);
740
741 puffs_framev_enqueue_justsend(pu, puffs_getselectable(pu), pb, 1, 0);
742
743 return 0;
744 }
745
746 /* no sigset_t static intializer */
747 static int sigs[NSIG] = { 0, };
748 static int sigcatch = 0;
749
750 int
751 puffs_unmountonsignal(int sig, bool sigignore)
752 {
753
754 if (sig < 0 || sig >= (int)NSIG) {
755 errno = EINVAL;
756 return -1;
757 }
758 if (sigignore)
759 if (signal(sig, SIG_IGN) == SIG_ERR)
760 return -1;
761
762 if (!sigs[sig])
763 sigcatch++;
764 sigs[sig] = 1;
765
766 return 0;
767 }
768
769 /*
770 * Actual mainloop. This is called from a context which can block.
771 * It is called either from puffs_mainloop (indirectly, via
772 * puffs_cc_continue() or from puffs_cc_yield()).
773 */
774 void
775 puffs__theloop(struct puffs_cc *pcc)
776 {
777 struct puffs_usermount *pu = pcc->pcc_pu;
778 struct puffs_framectrl *pfctrl;
779 struct puffs_fctrl_io *fio;
780 struct kevent *curev;
781 size_t nchanges;
782 int ndone;
783
784 while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
785
786 /*
787 * Schedule existing requests.
788 */
789 while ((pcc = TAILQ_FIRST(&pu->pu_sched)) != NULL) {
790 TAILQ_REMOVE(&pu->pu_sched, pcc, pcc_schedent);
791 puffs__goto(pcc);
792 }
793
794 if (pu->pu_ml_lfn)
795 pu->pu_ml_lfn(pu);
796
797 /* XXX: can we still do these optimizations? */
798 #if 0
799 /*
800 * Do this here, because:
801 * a) loopfunc might generate some results
802 * b) it's still "after" event handling (except for round 1)
803 */
804 if (puffs_req_putput(ppr) == -1)
805 goto out;
806 puffs_req_resetput(ppr);
807
808 /* micro optimization: skip kevent syscall if possible */
809 if (pu->pu_nfds == 1 && pu->pu_ml_timep == NULL
810 && (pu->pu_state & PU_ASYNCFD) == 0) {
811 pfctrl = XXX->fctrl;
812 puffs_framev_input(pu, pfctrl, XXX);
813 continue;
814 }
815 #endif
816
817 /* else: do full processing */
818 /* Don't bother worrying about O(n) for now */
819 LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
820 if (fio->stat & FIO_WRGONE)
821 continue;
822
823 pfctrl = fio->fctrl;
824
825 /*
826 * Try to write out everything to avoid the
827 * need for enabling EVFILT_WRITE. The likely
828 * case is that we can fit everything into the
829 * socket buffer.
830 */
831 puffs__framev_output(pu, pfctrl, fio);
832 }
833
834 /*
835 * Build list of which to enable/disable in writecheck.
836 */
837 nchanges = 0;
838 LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
839 if (fio->stat & FIO_WRGONE)
840 continue;
841
842 /* en/disable write checks for kqueue as needed */
843 assert((FIO_EN_WRITE(fio) && FIO_RM_WRITE(fio)) == 0);
844 if (FIO_EN_WRITE(fio)) {
845 EV_SET(&pu->pu_evs[nchanges], fio->io_fd,
846 EVFILT_WRITE, EV_ENABLE, 0, 0,
847 (uintptr_t)fio);
848 fio->stat |= FIO_WR;
849 nchanges++;
850 }
851 if (FIO_RM_WRITE(fio)) {
852 EV_SET(&pu->pu_evs[nchanges], fio->io_fd,
853 EVFILT_WRITE, EV_DISABLE, 0, 0,
854 (uintptr_t)fio);
855 fio->stat &= ~FIO_WR;
856 nchanges++;
857 }
858 }
859
860 ndone = kevent(pu->pu_kq, pu->pu_evs, nchanges,
861 pu->pu_evs, pu->pu_nevs, pu->pu_ml_timep);
862
863 if (ndone == -1) {
864 if (errno != EINTR)
865 break;
866 else
867 continue;
868 }
869
870 /* uoptimize */
871 if (ndone == 0)
872 continue;
873
874 /* iterate over the results */
875 for (curev = pu->pu_evs; ndone--; curev++) {
876 int what;
877
878 #if 0
879 /* get & possibly dispatch events from kernel */
880 if (curev->ident == puffsfd) {
881 if (puffs_req_handle(pgr, ppr, 0) == -1)
882 goto out;
883 continue;
884 }
885 #endif
886
887 fio = (void *)curev->udata;
888 if (__predict_true(fio))
889 pfctrl = fio->fctrl;
890 else
891 pfctrl = NULL;
892 if (curev->flags & EV_ERROR) {
893 assert(curev->filter == EVFILT_WRITE);
894 fio->stat &= ~FIO_WR;
895
896 /* XXX: how to know if it's a transient error */
897 puffs__framev_writeclose(pu, fio,
898 (int)curev->data);
899 puffs__framev_notify(fio, PUFFS_FBIO_ERROR);
900 continue;
901 }
902
903 what = 0;
904 if (curev->filter == EVFILT_READ) {
905 puffs__framev_input(pu, pfctrl, fio);
906 what |= PUFFS_FBIO_READ;
907 }
908
909 else if (curev->filter == EVFILT_WRITE) {
910 puffs__framev_output(pu, pfctrl, fio);
911 what |= PUFFS_FBIO_WRITE;
912 }
913
914 else if (__predict_false(curev->filter==EVFILT_SIGNAL)){
915 if ((pu->pu_state & PU_DONEXIT) == 0) {
916 PU_SETSFLAG(pu, PU_DONEXIT);
917 puffs_exit(pu, 0);
918 }
919 }
920 if (what)
921 puffs__framev_notify(fio, what);
922 }
923
924 /*
925 * Really free fd's now that we don't have references
926 * to them.
927 */
928 while ((fio = LIST_FIRST(&pu->pu_ios_rmlist)) != NULL) {
929 LIST_REMOVE(fio, fio_entries);
930 free(fio);
931 }
932 }
933
934 if (puffs__cc_restoremain(pu) == -1)
935 warn("cannot restore main context. impending doom");
936 }
937 int
938 puffs_mainloop(struct puffs_usermount *pu)
939 {
940 struct puffs_fctrl_io *fio;
941 struct puffs_cc *pcc;
942 struct kevent *curev;
943 size_t nevs;
944 int sverrno, i;
945
946 assert(puffs_getstate(pu) >= PUFFS_STATE_RUNNING);
947
948 pu->pu_kq = kqueue();
949 if (pu->pu_kq == -1)
950 goto out;
951 pu->pu_state |= PU_HASKQ;
952
953 puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK);
954 if (puffs__framev_addfd_ctrl(pu, puffs_getselectable(pu),
955 PUFFS_FBIO_READ | PUFFS_FBIO_WRITE,
956 &pu->pu_framectrl[PU_FRAMECTRL_FS]) == -1)
957 goto out;
958
959 nevs = pu->pu_nevs + sigcatch;
960 curev = realloc(pu->pu_evs, nevs * sizeof(struct kevent));
961 if (curev == NULL)
962 goto out;
963 pu->pu_evs = curev;
964 pu->pu_nevs = nevs;
965
966 LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
967 EV_SET(curev, fio->io_fd, EVFILT_READ, EV_ADD,
968 0, 0, (uintptr_t)fio);
969 curev++;
970 EV_SET(curev, fio->io_fd, EVFILT_WRITE, EV_ADD | EV_DISABLE,
971 0, 0, (uintptr_t)fio);
972 curev++;
973 }
974 for (i = 0; i < NSIG; i++) {
975 if (sigs[i]) {
976 EV_SET(curev, i, EVFILT_SIGNAL, EV_ADD | EV_ENABLE,
977 0, 0, 0);
978 curev++;
979 }
980 }
981 assert(curev - pu->pu_evs == (ssize_t)pu->pu_nevs);
982 if (kevent(pu->pu_kq, pu->pu_evs, pu->pu_nevs, NULL, 0, NULL) == -1)
983 goto out;
984
985 pu->pu_state |= PU_INLOOP;
986
987 /*
988 * Create alternate execution context and jump to it. Note
989 * that we come "out" of savemain twice. Where we come out
990 * of it depends on the architecture. If the return address is
991 * stored on the stack, we jump out from puffs_cc_continue(),
992 * for a register return address from puffs__cc_savemain().
993 * PU_MAINRESTORE makes sure we DTRT in both cases.
994 */
995 if (puffs__cc_create(pu, puffs__theloop, &pcc) == -1) {
996 goto out;
997 }
998
999 #if 0
1000 if (puffs__cc_savemain(pu) == -1) {
1001 goto out;
1002 }
1003 #else
1004 /*
1005 * XXX
1006 * puffs__cc_savemain() uses getcontext() and then returns.
1007 * the caller (this function) may overwrite the stack frame
1008 * of puffs__cc_savemain(), so when we call setcontext() later and
1009 * return from puffs__cc_savemain() again, the return address or
1010 * saved stack pointer can be garbage.
1011 * avoid this by calling getcontext() directly here.
1012 */
1013 extern int puffs_fakecc;
1014 if (!puffs_fakecc) {
1015 PU_CLRSFLAG(pu, PU_MAINRESTORE);
1016 if (getcontext(&pu->pu_mainctx) == -1) {
1017 goto out;
1018 }
1019 }
1020 #endif
1021
1022 if ((pu->pu_state & PU_MAINRESTORE) == 0)
1023 puffs_cc_continue(pcc);
1024
1025 finalpush(pu);
1026 errno = 0;
1027
1028 out:
1029 /* store the real error for a while */
1030 sverrno = errno;
1031
1032 errno = sverrno;
1033 if (errno)
1034 return -1;
1035 else
1036 return 0;
1037 }
1038