puffs.c revision 1.119 1 /* $NetBSD: puffs.c,v 1.119 2014/12/22 08:16:21 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.119 2014/12/22 08:16:21 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 size_t rplen,dirlen;
578
579 if (realpath(dir, rp) == NULL) {
580 rv = -1;
581 goto out;
582 }
583
584 rplen = strlen(rp);
585 dirlen = strlen(dir);
586 if (strncmp(dir, rp, rplen) != 0 ||
587 strspn(dir + rplen, "/") != dirlen - rplen) {
588 warnx("puffs_mount: \"%s\" is a relative path.", dir);
589 warnx("puffs_mount: using \"%s\" instead.", rp);
590 }
591
592 fd = open(_PATH_PUFFS, O_RDWR);
593 if (fd == -1) {
594 warnx("puffs_mount: cannot open %s", _PATH_PUFFS);
595 rv = -1;
596 goto out;
597 }
598 if (fd <= 2)
599 warnx("puffs_mount: device fd %d (<= 2), sure this is "
600 "what you want?", fd);
601
602 pu->pu_kargp->pa_fd = pu->pu_fd = fd;
603 if ((rv = mount(MOUNT_PUFFS, rp, mntflags,
604 pu->pu_kargp, sizeof(struct puffs_kargs))) == -1)
605 goto out;
606 }
607
608 PU_SETSTATE(pu, PUFFS_STATE_RUNNING);
609
610 out:
611 if (rv != 0)
612 sverrno = errno;
613 else
614 sverrno = 0;
615 free(pu->pu_kargp);
616 pu->pu_kargp = NULL;
617
618 if (pu->pu_state & PU_PUFFSDAEMON)
619 shutdaemon(pu, sverrno);
620
621 errno = sverrno;
622 return rv;
623 }
624
625 struct puffs_usermount *
626 puffs_init(struct puffs_ops *pops, const char *mntfromname,
627 const char *puffsname, void *priv, uint32_t pflags)
628 {
629 struct puffs_usermount *pu;
630 struct puffs_kargs *pargs;
631 int sverrno;
632
633 if (puffsname == PUFFS_DEFER)
634 puffsname = "n/a";
635 if (mntfromname == PUFFS_DEFER)
636 mntfromname = "n/a";
637 if (priv == PUFFS_DEFER)
638 priv = NULL;
639
640 pu = malloc(sizeof(struct puffs_usermount));
641 if (pu == NULL)
642 goto failfree;
643 memset(pu, 0, sizeof(struct puffs_usermount));
644
645 pargs = pu->pu_kargp = malloc(sizeof(struct puffs_kargs));
646 if (pargs == NULL)
647 goto failfree;
648 memset(pargs, 0, sizeof(struct puffs_kargs));
649
650 pargs->pa_vers = PUFFSVERSION;
651 pargs->pa_flags = PUFFS_FLAG_KERN(pflags);
652 fillvnopmask(pops, pargs);
653 puffs_setmntinfo(pu, mntfromname, puffsname);
654
655 puffs_zerostatvfs(&pargs->pa_svfsb);
656 pargs->pa_root_cookie = NULL;
657 pargs->pa_root_vtype = VDIR;
658 pargs->pa_root_vsize = 0;
659 pargs->pa_root_rdev = 0;
660 pargs->pa_maxmsglen = 0;
661 if (/*CONSTCOND*/ sizeof(time_t) == 4)
662 pargs->pa_time32 = 1;
663 else
664 pargs->pa_time32 = 0;
665
666 pu->pu_flags = pflags;
667 pu->pu_ops = *pops;
668 free(pops); /* XXX */
669
670 pu->pu_privdata = priv;
671 pu->pu_cc_stackshift = PUFFS_CC_STACKSHIFT_DEFAULT;
672 LIST_INIT(&pu->pu_pnodelst);
673 LIST_INIT(&pu->pu_ios);
674 LIST_INIT(&pu->pu_ios_rmlist);
675 LIST_INIT(&pu->pu_ccmagazin);
676 TAILQ_INIT(&pu->pu_sched);
677
678 pu->pu_framectrl[PU_FRAMECTRL_FS].rfb = puffs__fsframe_read;
679 pu->pu_framectrl[PU_FRAMECTRL_FS].wfb = puffs__fsframe_write;
680 pu->pu_framectrl[PU_FRAMECTRL_FS].cmpfb = puffs__fsframe_cmp;
681 pu->pu_framectrl[PU_FRAMECTRL_FS].gotfb = puffs__fsframe_gotframe;
682 pu->pu_framectrl[PU_FRAMECTRL_FS].fdnotfn = puffs_framev_unmountonclose;
683
684 /* defaults for some user-settable translation functions */
685 pu->pu_cmap = NULL; /* identity translation */
686
687 pu->pu_pathbuild = puffs_stdpath_buildpath;
688 pu->pu_pathfree = puffs_stdpath_freepath;
689 pu->pu_pathcmp = puffs_stdpath_cmppath;
690 pu->pu_pathtransform = NULL;
691 pu->pu_namemod = NULL;
692
693 pu->pu_errnotify = puffs_kernerr_log;
694
695 PU_SETSTATE(pu, PUFFS_STATE_BEFOREMOUNT);
696
697 return pu;
698
699 failfree:
700 /* can't unmount() from here for obvious reasons */
701 sverrno = errno;
702 free(pu);
703 errno = sverrno;
704 return NULL;
705 }
706
707 void
708 puffs_cancel(struct puffs_usermount *pu, int error)
709 {
710
711 assert(puffs_getstate(pu) < PUFFS_STATE_RUNNING);
712 shutdaemon(pu, error);
713 free(pu);
714 }
715
716 /*ARGSUSED1*/
717 int
718 puffs_exit(struct puffs_usermount *pu, int unused /* strict compat */)
719 {
720 struct puffs_framebuf *pb;
721 struct puffs_req *preq;
722 void *winp;
723 size_t winlen;
724 int sverrno;
725
726 pb = puffs_framebuf_make();
727 if (pb == NULL) {
728 errno = ENOMEM;
729 return -1;
730 }
731
732 winlen = sizeof(struct puffs_req);
733 if (puffs_framebuf_getwindow(pb, 0, &winp, &winlen) == -1) {
734 sverrno = errno;
735 puffs_framebuf_destroy(pb);
736 errno = sverrno;
737 return -1;
738 }
739 preq = winp;
740
741 preq->preq_buflen = sizeof(struct puffs_req);
742 preq->preq_opclass = PUFFSOP_UNMOUNT;
743 preq->preq_id = puffs__nextreq(pu);
744
745 puffs_framev_enqueue_justsend(pu, puffs_getselectable(pu), pb, 1, 0);
746
747 return 0;
748 }
749
750 /* no sigset_t static intializer */
751 static int sigs[NSIG] = { 0, };
752 static int sigcatch = 0;
753
754 int
755 puffs_unmountonsignal(int sig, bool sigignore)
756 {
757
758 if (sig < 0 || sig >= (int)NSIG) {
759 errno = EINVAL;
760 return -1;
761 }
762 if (sigignore)
763 if (signal(sig, SIG_IGN) == SIG_ERR)
764 return -1;
765
766 if (!sigs[sig])
767 sigcatch++;
768 sigs[sig] = 1;
769
770 return 0;
771 }
772
773 /*
774 * Actual mainloop. This is called from a context which can block.
775 * It is called either from puffs_mainloop (indirectly, via
776 * puffs_cc_continue() or from puffs_cc_yield()).
777 */
778 void
779 puffs__theloop(struct puffs_cc *pcc)
780 {
781 struct puffs_usermount *pu = pcc->pcc_pu;
782 struct puffs_framectrl *pfctrl;
783 struct puffs_fctrl_io *fio;
784 struct kevent *curev;
785 size_t nchanges;
786 int ndone;
787
788 while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
789
790 /*
791 * Schedule existing requests.
792 */
793 while ((pcc = TAILQ_FIRST(&pu->pu_sched)) != NULL) {
794 TAILQ_REMOVE(&pu->pu_sched, pcc, pcc_schedent);
795 puffs__goto(pcc);
796 }
797
798 if (pu->pu_ml_lfn)
799 pu->pu_ml_lfn(pu);
800
801 /* XXX: can we still do these optimizations? */
802 #if 0
803 /*
804 * Do this here, because:
805 * a) loopfunc might generate some results
806 * b) it's still "after" event handling (except for round 1)
807 */
808 if (puffs_req_putput(ppr) == -1)
809 goto out;
810 puffs_req_resetput(ppr);
811
812 /* micro optimization: skip kevent syscall if possible */
813 if (pu->pu_nfds == 1 && pu->pu_ml_timep == NULL
814 && (pu->pu_state & PU_ASYNCFD) == 0) {
815 pfctrl = XXX->fctrl;
816 puffs_framev_input(pu, pfctrl, XXX);
817 continue;
818 }
819 #endif
820
821 /* else: do full processing */
822 /* Don't bother worrying about O(n) for now */
823 LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
824 if (fio->stat & FIO_WRGONE)
825 continue;
826
827 pfctrl = fio->fctrl;
828
829 /*
830 * Try to write out everything to avoid the
831 * need for enabling EVFILT_WRITE. The likely
832 * case is that we can fit everything into the
833 * socket buffer.
834 */
835 puffs__framev_output(pu, pfctrl, fio);
836 }
837
838 /*
839 * Build list of which to enable/disable in writecheck.
840 */
841 nchanges = 0;
842 LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
843 if (fio->stat & FIO_WRGONE)
844 continue;
845
846 /* en/disable write checks for kqueue as needed */
847 assert((FIO_EN_WRITE(fio) && FIO_RM_WRITE(fio)) == 0);
848 if (FIO_EN_WRITE(fio)) {
849 EV_SET(&pu->pu_evs[nchanges], fio->io_fd,
850 EVFILT_WRITE, EV_ENABLE, 0, 0,
851 (uintptr_t)fio);
852 fio->stat |= FIO_WR;
853 nchanges++;
854 }
855 if (FIO_RM_WRITE(fio)) {
856 EV_SET(&pu->pu_evs[nchanges], fio->io_fd,
857 EVFILT_WRITE, EV_DISABLE, 0, 0,
858 (uintptr_t)fio);
859 fio->stat &= ~FIO_WR;
860 nchanges++;
861 }
862 }
863
864 ndone = kevent(pu->pu_kq, pu->pu_evs, nchanges,
865 pu->pu_evs, pu->pu_nevs, pu->pu_ml_timep);
866
867 if (ndone == -1) {
868 if (errno != EINTR)
869 break;
870 else
871 continue;
872 }
873
874 /* uoptimize */
875 if (ndone == 0)
876 continue;
877
878 /* iterate over the results */
879 for (curev = pu->pu_evs; ndone--; curev++) {
880 int what;
881
882 #if 0
883 /* get & possibly dispatch events from kernel */
884 if (curev->ident == puffsfd) {
885 if (puffs_req_handle(pgr, ppr, 0) == -1)
886 goto out;
887 continue;
888 }
889 #endif
890
891 fio = (void *)curev->udata;
892 if (__predict_true(fio))
893 pfctrl = fio->fctrl;
894 else
895 pfctrl = NULL;
896 if (curev->flags & EV_ERROR) {
897 assert(curev->filter == EVFILT_WRITE);
898 fio->stat &= ~FIO_WR;
899
900 /* XXX: how to know if it's a transient error */
901 puffs__framev_writeclose(pu, fio,
902 (int)curev->data);
903 puffs__framev_notify(fio, PUFFS_FBIO_ERROR);
904 continue;
905 }
906
907 what = 0;
908 if (curev->filter == EVFILT_READ) {
909 puffs__framev_input(pu, pfctrl, fio);
910 what |= PUFFS_FBIO_READ;
911 }
912
913 else if (curev->filter == EVFILT_WRITE) {
914 puffs__framev_output(pu, pfctrl, fio);
915 what |= PUFFS_FBIO_WRITE;
916 }
917
918 else if (__predict_false(curev->filter==EVFILT_SIGNAL)){
919 if ((pu->pu_state & PU_DONEXIT) == 0) {
920 PU_SETSFLAG(pu, PU_DONEXIT);
921 puffs_exit(pu, 0);
922 }
923 }
924 if (what)
925 puffs__framev_notify(fio, what);
926 }
927
928 /*
929 * Really free fd's now that we don't have references
930 * to them.
931 */
932 while ((fio = LIST_FIRST(&pu->pu_ios_rmlist)) != NULL) {
933 LIST_REMOVE(fio, fio_entries);
934 free(fio);
935 }
936 }
937
938 if (puffs__cc_restoremain(pu) == -1)
939 warn("cannot restore main context. impending doom");
940 }
941 int
942 puffs_mainloop(struct puffs_usermount *pu)
943 {
944 struct puffs_fctrl_io *fio;
945 struct puffs_cc *pcc;
946 struct kevent *curev;
947 size_t nevs;
948 int sverrno, i;
949
950 assert(puffs_getstate(pu) >= PUFFS_STATE_RUNNING);
951
952 pu->pu_kq = kqueue();
953 if (pu->pu_kq == -1)
954 goto out;
955 pu->pu_state |= PU_HASKQ;
956
957 puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK);
958 if (puffs__framev_addfd_ctrl(pu, puffs_getselectable(pu),
959 PUFFS_FBIO_READ | PUFFS_FBIO_WRITE,
960 &pu->pu_framectrl[PU_FRAMECTRL_FS]) == -1)
961 goto out;
962
963 nevs = pu->pu_nevs + sigcatch;
964 curev = realloc(pu->pu_evs, nevs * sizeof(struct kevent));
965 if (curev == NULL)
966 goto out;
967 pu->pu_evs = curev;
968 pu->pu_nevs = nevs;
969
970 LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
971 EV_SET(curev, fio->io_fd, EVFILT_READ, EV_ADD,
972 0, 0, (uintptr_t)fio);
973 curev++;
974 EV_SET(curev, fio->io_fd, EVFILT_WRITE, EV_ADD | EV_DISABLE,
975 0, 0, (uintptr_t)fio);
976 curev++;
977 }
978 for (i = 0; i < NSIG; i++) {
979 if (sigs[i]) {
980 EV_SET(curev, i, EVFILT_SIGNAL, EV_ADD | EV_ENABLE,
981 0, 0, 0);
982 curev++;
983 }
984 }
985 assert(curev - pu->pu_evs == (ssize_t)pu->pu_nevs);
986 if (kevent(pu->pu_kq, pu->pu_evs, pu->pu_nevs, NULL, 0, NULL) == -1)
987 goto out;
988
989 pu->pu_state |= PU_INLOOP;
990
991 /*
992 * Create alternate execution context and jump to it. Note
993 * that we come "out" of savemain twice. Where we come out
994 * of it depends on the architecture. If the return address is
995 * stored on the stack, we jump out from puffs_cc_continue(),
996 * for a register return address from puffs__cc_savemain().
997 * PU_MAINRESTORE makes sure we DTRT in both cases.
998 */
999 if (puffs__cc_create(pu, puffs__theloop, &pcc) == -1) {
1000 goto out;
1001 }
1002
1003 #if 0
1004 if (puffs__cc_savemain(pu) == -1) {
1005 goto out;
1006 }
1007 #else
1008 /*
1009 * XXX
1010 * puffs__cc_savemain() uses getcontext() and then returns.
1011 * the caller (this function) may overwrite the stack frame
1012 * of puffs__cc_savemain(), so when we call setcontext() later and
1013 * return from puffs__cc_savemain() again, the return address or
1014 * saved stack pointer can be garbage.
1015 * avoid this by calling getcontext() directly here.
1016 */
1017 extern int puffs_fakecc;
1018 if (!puffs_fakecc) {
1019 PU_CLRSFLAG(pu, PU_MAINRESTORE);
1020 if (getcontext(&pu->pu_mainctx) == -1) {
1021 goto out;
1022 }
1023 }
1024 #endif
1025
1026 if ((pu->pu_state & PU_MAINRESTORE) == 0)
1027 puffs_cc_continue(pcc);
1028
1029 finalpush(pu);
1030 errno = 0;
1031
1032 out:
1033 /* store the real error for a while */
1034 sverrno = errno;
1035
1036 errno = sverrno;
1037 if (errno)
1038 return -1;
1039 else
1040 return 0;
1041 }
1042