puffs.c revision 1.125 1 /* $NetBSD: puffs.c,v 1.125 2021/10/30 10:34:18 nia 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.125 2021/10/30 10:34:18 nia 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 warnx("%s: type %d, error %d, cookie %p (%s)", __func__,
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, "%s: type %d, error %d, cookie %p (%s)", __func__,
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 warnx("%s: adjusting " "stacksize to minimum %ld",
217 __func__, 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 warnx("%s: using next power of two: %d", __func__,
232 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("%s: call has effect only before mount", __func__);
274 return;
275 }
276
277 pargs->pa_root_vtype = vt;
278 pargs->pa_root_vsize = vsize;
279 pargs->pa_root_rdev = rdev;
280 }
281
282 void *
283 puffs_getspecific(struct puffs_usermount *pu)
284 {
285
286 return pu->pu_privdata;
287 }
288
289 void
290 puffs_setspecific(struct puffs_usermount *pu, void *privdata)
291 {
292
293 pu->pu_privdata = privdata;
294 }
295
296 void
297 puffs_setmntinfo(struct puffs_usermount *pu,
298 const char *mntfromname, const char *puffsname)
299 {
300 struct puffs_kargs *pargs = pu->pu_kargp;
301
302 (void)strlcpy(pargs->pa_mntfromname, mntfromname,
303 sizeof(pargs->pa_mntfromname));
304 (void)strlcpy(pargs->pa_typename, puffsname,
305 sizeof(pargs->pa_typename));
306 }
307
308 size_t
309 puffs_getmaxreqlen(struct puffs_usermount *pu)
310 {
311
312 return pu->pu_maxreqlen;
313 }
314
315 void
316 puffs_setmaxreqlen(struct puffs_usermount *pu, size_t reqlen)
317 {
318
319 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
320 warnx("%s: call has effect only before mount", __func__);
321
322 pu->pu_kargp->pa_maxmsglen = reqlen;
323 }
324
325 void
326 puffs_setfhsize(struct puffs_usermount *pu, size_t fhsize, int flags)
327 {
328
329 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
330 warnx("%s: call has effect only before mount", __func__);
331
332 pu->pu_kargp->pa_fhsize = fhsize;
333 pu->pu_kargp->pa_fhflags = flags;
334 }
335
336 void
337 puffs_setncookiehash(struct puffs_usermount *pu, int nhash)
338 {
339
340 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
341 warnx("%s: call has effect only before mount", __func__);
342
343 pu->pu_kargp->pa_nhashbuckets = nhash;
344 }
345
346 void
347 puffs_set_pathbuild(struct puffs_usermount *pu, pu_pathbuild_fn fn)
348 {
349
350 pu->pu_pathbuild = fn;
351 }
352
353 void
354 puffs_set_pathtransform(struct puffs_usermount *pu, pu_pathtransform_fn fn)
355 {
356
357 pu->pu_pathtransform = fn;
358 }
359
360 void
361 puffs_set_pathcmp(struct puffs_usermount *pu, pu_pathcmp_fn fn)
362 {
363
364 pu->pu_pathcmp = fn;
365 }
366
367 void
368 puffs_set_pathfree(struct puffs_usermount *pu, pu_pathfree_fn fn)
369 {
370
371 pu->pu_pathfree = fn;
372 }
373
374 void
375 puffs_set_namemod(struct puffs_usermount *pu, pu_namemod_fn fn)
376 {
377
378 pu->pu_namemod = fn;
379 }
380
381 void
382 puffs_set_errnotify(struct puffs_usermount *pu, pu_errnotify_fn fn)
383 {
384
385 pu->pu_errnotify = fn;
386 }
387
388 void
389 puffs_set_cmap(struct puffs_usermount *pu, pu_cmap_fn fn)
390 {
391
392 pu->pu_cmap = fn;
393 }
394
395 void
396 puffs_ml_setloopfn(struct puffs_usermount *pu, puffs_ml_loop_fn lfn)
397 {
398
399 pu->pu_ml_lfn = lfn;
400 }
401
402 void
403 puffs_ml_settimeout(struct puffs_usermount *pu, struct timespec *ts)
404 {
405
406 if (ts == NULL) {
407 pu->pu_ml_timep = NULL;
408 } else {
409 pu->pu_ml_timeout = *ts;
410 pu->pu_ml_timep = &pu->pu_ml_timeout;
411 }
412 }
413
414 void
415 puffs_set_prepost(struct puffs_usermount *pu,
416 pu_prepost_fn pre, pu_prepost_fn pst)
417 {
418
419 pu->pu_oppre = pre;
420 pu->pu_oppost = pst;
421 }
422
423 void
424 puffs_setback(struct puffs_cc *pcc, int whatback)
425 {
426 struct puffs_req *preq = puffs__framebuf_getdataptr(pcc->pcc_pb);
427
428 assert(PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN && (
429 preq->preq_optype == PUFFS_VN_OPEN ||
430 preq->preq_optype == PUFFS_VN_MMAP ||
431 preq->preq_optype == PUFFS_VN_REMOVE ||
432 preq->preq_optype == PUFFS_VN_RMDIR ||
433 preq->preq_optype == PUFFS_VN_INACTIVE));
434
435 preq->preq_setbacks |= whatback & PUFFS_SETBACK_MASK;
436 }
437
438 int
439 puffs_daemon(struct puffs_usermount *pu, int nochdir, int noclose)
440 {
441 long int n;
442 int parent, value, fd;
443
444 if (pipe(pu->pu_dpipe) == -1)
445 return -1;
446
447 switch (fork()) {
448 case -1:
449 return -1;
450 case 0:
451 parent = 0;
452 break;
453 default:
454 parent = 1;
455 break;
456 }
457 pu->pu_state |= PU_PUFFSDAEMON;
458
459 if (parent) {
460 close(pu->pu_dpipe[1]);
461 n = read(pu->pu_dpipe[0], &value, sizeof(int));
462 if (n == -1)
463 err(1, "puffs_daemon");
464 if (n != sizeof(value))
465 errx(1, "puffs_daemon got %ld bytes", n);
466 if (value) {
467 errno = value;
468 err(1, "puffs_daemon");
469 }
470 exit(0);
471 } else {
472 if (setsid() == -1)
473 goto fail;
474
475 if (!nochdir)
476 chdir("/");
477
478 if (!noclose) {
479 fd = open(_PATH_DEVNULL, O_RDWR, 0);
480 if (fd == -1)
481 goto fail;
482 dup2(fd, STDIN_FILENO);
483 dup2(fd, STDOUT_FILENO);
484 dup2(fd, STDERR_FILENO);
485 if (fd > STDERR_FILENO)
486 close(fd);
487 }
488 return 0;
489 }
490
491 fail:
492 n = write(pu->pu_dpipe[1], &errno, sizeof(int));
493 assert(n == 4);
494 return -1;
495 }
496
497 static void
498 shutdaemon(struct puffs_usermount *pu, int error)
499 {
500 ssize_t n;
501
502 n = write(pu->pu_dpipe[1], &error, sizeof(int));
503 assert(n == 4);
504 close(pu->pu_dpipe[0]);
505 close(pu->pu_dpipe[1]);
506 pu->pu_state &= ~PU_PUFFSDAEMON;
507 }
508
509 int
510 puffs_mount(struct puffs_usermount *pu, const char *dir, int mntflags,
511 puffs_cookie_t cookie)
512 {
513 int rv, fd, sverrno;
514 char *comfd;
515
516 pu->pu_kargp->pa_root_cookie = cookie;
517
518 /* XXXkludgehere */
519 /* kauth doesn't provide this service any longer */
520 if (geteuid() != 0)
521 mntflags |= MNT_NOSUID | MNT_NODEV;
522
523 /*
524 * Undocumented... Well, documented only here.
525 *
526 * This is used for imaginative purposes. If the env variable is
527 * set, puffs_mount() doesn't do the regular mount procedure.
528 * Rather, it crams the mount data down the comfd and sets comfd as
529 * the puffs descriptor.
530 *
531 * This shouldn't be used unless you can read my mind ( ... or write
532 * it, not to mention execute it, but that's starting to get silly).
533 */
534 if ((comfd = getenv("PUFFS_COMFD")) != NULL) {
535 size_t len;
536
537 if (sscanf(comfd, "%d", &pu->pu_fd) != 1) {
538 errno = EINVAL;
539 rv = -1;
540 goto out;
541 }
542 /* check that what we got at least resembles an fd */
543 if (fcntl(pu->pu_fd, F_GETFL) == -1) {
544 rv = -1;
545 goto out;
546 }
547
548 #define allwrite(buf, len) \
549 do { \
550 ssize_t al_rv; \
551 al_rv = write(pu->pu_fd, buf, len); \
552 if ((size_t)al_rv != len) { \
553 if (al_rv != -1) \
554 errno = EIO; \
555 rv = -1; \
556 goto out; \
557 } \
558 } while (/*CONSTCOND*/0)
559 len = strlen(dir)+1;
560 allwrite(&len, sizeof(len));
561 allwrite(dir, len);
562 len = strlen(pu->pu_kargp->pa_mntfromname)+1;
563 allwrite(&len, sizeof(len));
564 allwrite(pu->pu_kargp->pa_mntfromname, len);
565 allwrite(&mntflags, sizeof(mntflags));
566 len = sizeof(*pu->pu_kargp);
567 allwrite(&len, sizeof(len));
568 allwrite(pu->pu_kargp, sizeof(*pu->pu_kargp));
569 allwrite(&pu->pu_flags, sizeof(pu->pu_flags));
570 #undef allwrite
571
572 rv = 0;
573 } else {
574 char rp[MAXPATHLEN];
575 size_t rplen,dirlen;
576
577 if (realpath(dir, rp) == NULL) {
578 rv = -1;
579 goto out;
580 }
581
582 rplen = strlen(rp);
583 dirlen = strlen(dir);
584 if (strncmp(dir, rp, rplen) != 0 ||
585 strspn(dir + rplen, "/") != dirlen - rplen) {
586 warnx("%s: `%s' is a %s path.", __func__, dir,
587 dir[0] != '/' ? "relative" : "non canonical");
588 warnx("%s: using `%s' instead.", __func__, rp);
589 }
590
591 fd = open(_PATH_PUFFS, O_RDWR);
592 if (fd == -1) {
593 warnx("%s: cannot open `%s'", __func__, _PATH_PUFFS);
594 rv = -1;
595 goto out;
596 }
597 if (fd <= 2)
598 warnx("%s: device fd %d (<= 2), sure this is "
599 "what you want?", __func__, fd);
600
601 pu->pu_kargp->pa_fd = pu->pu_fd = fd;
602 if ((rv = mount(MOUNT_PUFFS, rp, mntflags,
603 pu->pu_kargp, sizeof(struct puffs_kargs))) == -1)
604 goto out;
605 }
606
607 PU_SETSTATE(pu, PUFFS_STATE_RUNNING);
608
609 out:
610 if (rv != 0)
611 sverrno = errno;
612 else
613 sverrno = 0;
614 free(pu->pu_kargp);
615 pu->pu_kargp = NULL;
616
617 if (pu->pu_state & PU_PUFFSDAEMON)
618 shutdaemon(pu, sverrno);
619
620 errno = sverrno;
621 return rv;
622 }
623
624 struct puffs_usermount *
625 puffs_init(struct puffs_ops *pops, const char *mntfromname,
626 const char *puffsname, void *priv, uint32_t pflags)
627 {
628 struct puffs_usermount *pu;
629 struct puffs_kargs *pargs;
630 int sverrno;
631
632 if (puffsname == PUFFS_DEFER)
633 puffsname = "n/a";
634 if (mntfromname == PUFFS_DEFER)
635 mntfromname = "n/a";
636 if (priv == PUFFS_DEFER)
637 priv = NULL;
638
639 pu = malloc(sizeof(struct puffs_usermount));
640 if (pu == NULL)
641 goto failfree;
642 memset(pu, 0, sizeof(struct puffs_usermount));
643
644 pargs = pu->pu_kargp = malloc(sizeof(struct puffs_kargs));
645 if (pargs == NULL)
646 goto failfree;
647 memset(pargs, 0, sizeof(struct puffs_kargs));
648
649 pargs->pa_vers = PUFFSVERSION;
650 pargs->pa_flags = PUFFS_FLAG_KERN(pflags);
651 fillvnopmask(pops, pargs);
652 puffs_setmntinfo(pu, mntfromname, puffsname);
653
654 puffs_zerostatvfs(&pargs->pa_svfsb);
655 pargs->pa_root_cookie = NULL;
656 pargs->pa_root_vtype = VDIR;
657 pargs->pa_root_vsize = 0;
658 pargs->pa_root_rdev = 0;
659 pargs->pa_maxmsglen = 0;
660 if (/*CONSTCOND*/ sizeof(time_t) == 4)
661 pargs->pa_time32 = 1;
662 else
663 pargs->pa_time32 = 0;
664
665 pu->pu_flags = pflags;
666 pu->pu_ops = *pops;
667 free(pops); /* XXX */
668
669 pu->pu_privdata = priv;
670 pu->pu_cc_stackshift = PUFFS_CC_STACKSHIFT_DEFAULT;
671 LIST_INIT(&pu->pu_pnodelst);
672 LIST_INIT(&pu->pu_ios);
673 LIST_INIT(&pu->pu_ios_rmlist);
674 LIST_INIT(&pu->pu_ccmagazin);
675 TAILQ_INIT(&pu->pu_sched);
676
677 pu->pu_framectrl[PU_FRAMECTRL_FS].rfb = puffs__fsframe_read;
678 pu->pu_framectrl[PU_FRAMECTRL_FS].wfb = puffs__fsframe_write;
679 pu->pu_framectrl[PU_FRAMECTRL_FS].cmpfb = puffs__fsframe_cmp;
680 pu->pu_framectrl[PU_FRAMECTRL_FS].gotfb = puffs__fsframe_gotframe;
681 pu->pu_framectrl[PU_FRAMECTRL_FS].fdnotfn = puffs_framev_unmountonclose;
682
683 /* defaults for some user-settable translation functions */
684 pu->pu_cmap = NULL; /* identity translation */
685
686 pu->pu_pathbuild = puffs_stdpath_buildpath;
687 pu->pu_pathfree = puffs_stdpath_freepath;
688 pu->pu_pathcmp = puffs_stdpath_cmppath;
689 pu->pu_pathtransform = NULL;
690 pu->pu_namemod = NULL;
691
692 pu->pu_errnotify = puffs_kernerr_log;
693
694 PU_SETSTATE(pu, PUFFS_STATE_BEFOREMOUNT);
695
696 return pu;
697
698 failfree:
699 /* can't unmount() from here for obvious reasons */
700 sverrno = errno;
701 free(pu);
702 errno = sverrno;
703 return NULL;
704 }
705
706 void
707 puffs_cancel(struct puffs_usermount *pu, int error)
708 {
709
710 assert(puffs_getstate(pu) < PUFFS_STATE_RUNNING);
711 shutdaemon(pu, error);
712 free(pu);
713 }
714
715 /*ARGSUSED1*/
716 int
717 puffs_exit(struct puffs_usermount *pu, int unused /* strict compat */)
718 {
719 struct puffs_framebuf *pb;
720 struct puffs_req *preq;
721 void *winp;
722 size_t winlen;
723 int sverrno;
724
725 pb = puffs_framebuf_make();
726 if (pb == NULL) {
727 errno = ENOMEM;
728 return -1;
729 }
730
731 winlen = sizeof(struct puffs_req);
732 if (puffs_framebuf_getwindow(pb, 0, &winp, &winlen) == -1) {
733 sverrno = errno;
734 puffs_framebuf_destroy(pb);
735 errno = sverrno;
736 return -1;
737 }
738 preq = winp;
739
740 preq->preq_buflen = sizeof(struct puffs_req);
741 preq->preq_opclass = PUFFSOP_UNMOUNT;
742 preq->preq_id = puffs__nextreq(pu);
743
744 puffs_framev_enqueue_justsend(pu, puffs_getselectable(pu), pb, 1, 0);
745
746 return 0;
747 }
748
749 /* no sigset_t static initializer */
750 static int sigs[NSIG] = { 0, };
751 static int sigcatch = 0;
752
753 int
754 puffs_unmountonsignal(int sig, bool sigignore)
755 {
756
757 if (sig < 0 || sig >= (int)NSIG) {
758 errno = EINVAL;
759 return -1;
760 }
761 if (sigignore)
762 if (signal(sig, SIG_IGN) == SIG_ERR)
763 return -1;
764
765 if (!sigs[sig])
766 sigcatch++;
767 sigs[sig] = 1;
768
769 return 0;
770 }
771
772 /*
773 * Actual mainloop. This is called from a context which can block.
774 * It is called either from puffs_mainloop (indirectly, via
775 * puffs_cc_continue() or from puffs_cc_yield()).
776 */
777 void
778 puffs__theloop(struct puffs_cc *pcc)
779 {
780 struct puffs_usermount *pu = pcc->pcc_pu;
781 struct puffs_framectrl *pfctrl;
782 struct puffs_fctrl_io *fio;
783 struct kevent *curev;
784 size_t nchanges;
785 int ndone;
786
787 while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
788
789 /*
790 * Schedule existing requests.
791 */
792 while ((pcc = TAILQ_FIRST(&pu->pu_sched)) != NULL) {
793 TAILQ_REMOVE(&pu->pu_sched, pcc, pcc_schedent);
794 puffs__goto(pcc);
795 }
796
797 if (pu->pu_ml_lfn)
798 pu->pu_ml_lfn(pu);
799
800 /* XXX: can we still do these optimizations? */
801 #if 0
802 /*
803 * Do this here, because:
804 * a) loopfunc might generate some results
805 * b) it's still "after" event handling (except for round 1)
806 */
807 if (puffs_req_putput(ppr) == -1)
808 goto out;
809 puffs_req_resetput(ppr);
810
811 /* micro optimization: skip kevent syscall if possible */
812 if (pu->pu_nfds == 1 && pu->pu_ml_timep == NULL
813 && (pu->pu_state & PU_ASYNCFD) == 0) {
814 pfctrl = XXX->fctrl;
815 puffs_framev_input(pu, pfctrl, XXX);
816 continue;
817 }
818 #endif
819
820 /* else: do full processing */
821 /* Don't bother worrying about O(n) for now */
822 LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
823 if (fio->stat & FIO_WRGONE)
824 continue;
825
826 pfctrl = fio->fctrl;
827
828 /*
829 * Try to write out everything to avoid the
830 * need for enabling EVFILT_WRITE. The likely
831 * case is that we can fit everything into the
832 * socket buffer.
833 */
834 puffs__framev_output(pu, pfctrl, fio);
835 }
836
837 /*
838 * Build list of which to enable/disable in writecheck.
839 */
840 nchanges = 0;
841 LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
842 if (fio->stat & FIO_WRGONE)
843 continue;
844
845 /* en/disable write checks for kqueue as needed */
846 assert((FIO_EN_WRITE(fio) && FIO_RM_WRITE(fio)) == 0);
847 if (FIO_EN_WRITE(fio)) {
848 EV_SET(&pu->pu_evs[nchanges], fio->io_fd,
849 EVFILT_WRITE, EV_ENABLE, 0, 0,
850 (intptr_t)fio);
851 fio->stat |= FIO_WR;
852 nchanges++;
853 }
854 if (FIO_RM_WRITE(fio)) {
855 EV_SET(&pu->pu_evs[nchanges], fio->io_fd,
856 EVFILT_WRITE, EV_DISABLE, 0, 0,
857 (intptr_t)fio);
858 fio->stat &= ~FIO_WR;
859 nchanges++;
860 }
861 }
862
863 ndone = kevent(pu->pu_kq, pu->pu_evs, nchanges,
864 pu->pu_evs, pu->pu_nevs, pu->pu_ml_timep);
865
866 if (ndone == -1) {
867 if (errno != EINTR)
868 break;
869 else
870 continue;
871 }
872
873 /* uoptimize */
874 if (ndone == 0)
875 continue;
876
877 /* iterate over the results */
878 for (curev = pu->pu_evs; ndone--; curev++) {
879 int what;
880
881 #if 0
882 /* get & possibly dispatch events from kernel */
883 if (curev->ident == puffsfd) {
884 if (puffs_req_handle(pgr, ppr, 0) == -1)
885 goto out;
886 continue;
887 }
888 #endif
889
890 fio = (void *)curev->udata;
891 if (__predict_true(fio))
892 pfctrl = fio->fctrl;
893 else
894 pfctrl = NULL;
895 if (curev->flags & EV_ERROR) {
896 assert(curev->filter == EVFILT_WRITE);
897 fio->stat &= ~FIO_WR;
898
899 /* XXX: how to know if it's a transient error */
900 puffs__framev_writeclose(pu, fio,
901 (int)curev->data);
902 puffs__framev_notify(fio, PUFFS_FBIO_ERROR);
903 continue;
904 }
905
906 what = 0;
907 switch (curev->filter) {
908 case EVFILT_READ:
909 puffs__framev_input(pu, pfctrl, fio);
910 what |= PUFFS_FBIO_READ;
911 break;
912 case EVFILT_WRITE:
913 puffs__framev_output(pu, pfctrl, fio);
914 what |= PUFFS_FBIO_WRITE;
915 break;
916 case EVFILT_SIGNAL:
917 if ((pu->pu_state & PU_DONEXIT) == 0) {
918 PU_SETSFLAG(pu, PU_DONEXIT);
919 puffs_exit(pu, 0);
920 }
921 break;
922 default:
923 warn("unhandled filter %d", curev->filter);
924 }
925 if (what)
926 puffs__framev_notify(fio, what);
927 }
928
929 /*
930 * Really free fd's now that we don't have references
931 * to them.
932 */
933 while ((fio = LIST_FIRST(&pu->pu_ios_rmlist)) != NULL) {
934 LIST_REMOVE(fio, fio_entries);
935 free(fio);
936 }
937 }
938
939 if (puffs__cc_restoremain(pu) == -1)
940 warn("cannot restore main context. impending doom");
941 }
942 int
943 puffs_mainloop(struct puffs_usermount *pu)
944 {
945 struct puffs_fctrl_io *fio;
946 struct puffs_cc *pcc;
947 struct kevent *curev;
948 size_t nevs;
949 int sverrno, i;
950
951 assert(puffs_getstate(pu) >= PUFFS_STATE_RUNNING);
952
953 pu->pu_kq = kqueue();
954 if (pu->pu_kq == -1)
955 goto out;
956 pu->pu_state |= PU_HASKQ;
957
958 puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK);
959 if (puffs__framev_addfd_ctrl(pu, puffs_getselectable(pu),
960 PUFFS_FBIO_READ | PUFFS_FBIO_WRITE,
961 &pu->pu_framectrl[PU_FRAMECTRL_FS]) == -1)
962 goto out;
963
964 nevs = pu->pu_nevs + sigcatch;
965 if (reallocarr(&pu->pu_evs, nevs, sizeof(struct kevent)) != 0) {
966 errno = ENOMEM;
967 goto out;
968 }
969 pu->pu_nevs = nevs;
970
971 curev = pu->pu_evs;
972
973 LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
974 EV_SET(curev, fio->io_fd, EVFILT_READ, EV_ADD,
975 0, 0, (intptr_t)fio);
976 curev++;
977 EV_SET(curev, fio->io_fd, EVFILT_WRITE, EV_ADD | EV_DISABLE,
978 0, 0, (intptr_t)fio);
979 curev++;
980 }
981 for (i = 0; i < NSIG; i++) {
982 if (sigs[i]) {
983 EV_SET(curev, i, EVFILT_SIGNAL, EV_ADD | EV_ENABLE,
984 0, 0, 0);
985 curev++;
986 }
987 }
988 assert(curev - pu->pu_evs == (ssize_t)pu->pu_nevs);
989 if (kevent(pu->pu_kq, pu->pu_evs, pu->pu_nevs, NULL, 0, NULL) == -1)
990 goto out;
991
992 pu->pu_state |= PU_INLOOP;
993
994 /*
995 * Create alternate execution context and jump to it. Note
996 * that we come "out" of savemain twice. Where we come out
997 * of it depends on the architecture. If the return address is
998 * stored on the stack, we jump out from puffs_cc_continue(),
999 * for a register return address from puffs__cc_savemain().
1000 * PU_MAINRESTORE makes sure we DTRT in both cases.
1001 */
1002 if (puffs__cc_create(pu, puffs__theloop, &pcc) == -1) {
1003 goto out;
1004 }
1005
1006 #if 0
1007 if (puffs__cc_savemain(pu) == -1) {
1008 goto out;
1009 }
1010 #else
1011 /*
1012 * XXX
1013 * puffs__cc_savemain() uses getcontext() and then returns.
1014 * the caller (this function) may overwrite the stack frame
1015 * of puffs__cc_savemain(), so when we call setcontext() later and
1016 * return from puffs__cc_savemain() again, the return address or
1017 * saved stack pointer can be garbage.
1018 * avoid this by calling getcontext() directly here.
1019 */
1020 extern int puffs_fakecc;
1021 if (!puffs_fakecc) {
1022 PU_CLRSFLAG(pu, PU_MAINRESTORE);
1023 if (getcontext(&pu->pu_mainctx) == -1) {
1024 goto out;
1025 }
1026 }
1027 #endif
1028
1029 if ((pu->pu_state & PU_MAINRESTORE) == 0)
1030 puffs_cc_continue(pcc);
1031
1032 finalpush(pu);
1033 errno = 0;
1034
1035 out:
1036 /* store the real error for a while */
1037 sverrno = errno;
1038
1039 errno = sverrno;
1040 if (errno)
1041 return -1;
1042 else
1043 return 0;
1044 }
1045