usched: Allow process to change self cpu affinity
[dragonfly.git] / lib / libpuffs / puffs.c
blobb9e0fffde1e46025fc09737a037c2b77478a2474
1 /* $NetBSD: puffs.c,v 1.116 2011/05/03 13:16:47 manu Exp $ */
3 /*
4 * Copyright (c) 2005, 2006, 2007 Antti Kantee. All Rights Reserved.
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.
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.
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.
32 #include <sys/param.h>
33 #include <sys/event.h>
34 #include <sys/filio.h>
35 #include <sys/mount.h>
37 #include <assert.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <paths.h>
42 #include <pthread.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <syslog.h>
47 #include <unistd.h>
49 #include <vfs/puffs/puffs_msgif.h>
51 #include "puffs.h"
52 #include "puffs_priv.h"
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,
61 pthread_mutex_t pu_lock = PTHREAD_MUTEX_INITIALIZER;
63 #define FILLOP(lower, upper) \
64 do { \
65 if (pops->puffs_node_##lower) \
66 opmask[PUFFS_VN_##upper] = 1; \
67 } while (/*CONSTCOND*/0)
68 static void
69 fillvnopmask(struct puffs_ops *pops, struct puffs_kargs *pa)
71 uint8_t *opmask = pa->pa_vnopmask;
73 memset(opmask, 0, sizeof(pa->pa_vnopmask));
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);
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 FILLOP(advlock, ADVLOCK);
100 FILLOP(abortop, ABORTOP);
101 FILLOP(pathconf, PATHCONF);
103 FILLOP(getextattr, GETEXTATTR);
104 FILLOP(setextattr, SETEXTATTR);
105 FILLOP(listextattr, LISTEXTATTR);
106 FILLOP(deleteextattr, DELETEEXTATTR);
108 #undef FILLOP
111 * Go over all framev entries and write everything we can. This is
112 * mostly for the benefit of delivering "unmount" to the kernel.
114 static void
115 finalpush(struct puffs_usermount *pu)
117 struct puffs_fctrl_io *fio;
119 LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
120 if (fio->stat & FIO_WRGONE)
121 continue;
123 puffs__framev_output(pu, fio->fctrl, fio);
127 /*ARGSUSED*/
128 void
129 puffs_kernerr_abort(struct puffs_usermount *pu, uint8_t type,
130 int error, const char *str, puffs_cookie_t cookie)
133 fprintf(stderr, "abort: type %d, error %d, cookie %p (%s)\n",
134 type, error, cookie, str);
135 abort();
138 /*ARGSUSED*/
139 void
140 puffs_kernerr_log(struct puffs_usermount *pu, uint8_t type,
141 int error, const char *str, puffs_cookie_t cookie)
144 syslog(LOG_WARNING, "kernel: type %d, error %d, cookie %p (%s)\n",
145 type, error, cookie, str);
149 puffs_getselectable(struct puffs_usermount *pu)
152 return pu->pu_fd;
155 uint64_t
156 puffs__nextreq(struct puffs_usermount *pu)
158 uint64_t rv;
160 PU_LOCK();
161 rv = pu->pu_nextreq++ | (uint64_t)1<<63;
162 PU_UNLOCK();
164 return rv;
168 puffs_setblockingmode(struct puffs_usermount *pu, int mode)
170 int rv, x;
172 assert(puffs_getstate(pu) == PUFFS_STATE_RUNNING);
174 if (mode != PUFFSDEV_BLOCK && mode != PUFFSDEV_NONBLOCK) {
175 errno = EINVAL;
176 return -1;
179 x = mode;
180 rv = ioctl(pu->pu_fd, FIONBIO, &x);
182 if (rv == 0) {
183 if (mode == PUFFSDEV_BLOCK)
184 pu->pu_state &= ~PU_ASYNCFD;
185 else
186 pu->pu_state |= PU_ASYNCFD;
189 return rv;
193 puffs_getstate(struct puffs_usermount *pu)
196 return pu->pu_state & PU_STATEMASK;
199 void
200 puffs_setstacksize(struct puffs_usermount *pu, size_t ss)
202 long psize, minsize;
203 int stackshift;
204 int bonus;
206 assert(puffs_getstate(pu) == PUFFS_STATE_BEFOREMOUNT);
208 psize = sysconf(_SC_PAGESIZE);
209 minsize = 4*psize;
210 if (ss < (size_t)minsize || ss == PUFFS_STACKSIZE_MIN) {
211 if (ss != PUFFS_STACKSIZE_MIN)
212 fprintf(stderr, "puffs_setstacksize: adjusting "
213 "stacksize to minimum %ld\n", minsize);
214 ss = 4*psize;
217 stackshift = -1;
218 bonus = 0;
219 while (ss) {
220 if (ss & 0x1)
221 bonus++;
222 ss >>= 1;
223 stackshift++;
225 if (bonus > 1) {
226 stackshift++;
227 fprintf(stderr, "puffs_setstacksize: using next power of two: "
228 "%d\n", 1<<stackshift);
231 pu->pu_cc_stackshift = stackshift;
234 struct puffs_pathobj *
235 puffs_getrootpathobj(struct puffs_usermount *pu)
237 struct puffs_node *pnr;
239 pnr = pu->pu_pn_root;
240 if (pnr == NULL) {
241 errno = ENOENT;
242 return NULL;
245 return &pnr->pn_po;
248 void
249 puffs_setroot(struct puffs_usermount *pu, struct puffs_node *pn)
252 pu->pu_pn_root = pn;
255 struct puffs_node *
256 puffs_getroot(struct puffs_usermount *pu)
259 return pu->pu_pn_root;
262 void
263 puffs_setrootinfo(struct puffs_usermount *pu, enum vtype vt,
264 vsize_t vsize, dev_t rdev)
266 struct puffs_kargs *pargs = pu->pu_kargp;
268 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT) {
269 warnx("puffs_setrootinfo: call has effect only "
270 "before mount\n");
271 return;
274 pargs->pa_root_vtype = vt;
275 pargs->pa_root_vsize = vsize;
276 pargs->pa_root_rdev = rdev;
279 void *
280 puffs_getspecific(struct puffs_usermount *pu)
283 return pu->pu_privdata;
286 void
287 puffs_setspecific(struct puffs_usermount *pu, void *privdata)
290 pu->pu_privdata = privdata;
293 void
294 puffs_setmntinfo(struct puffs_usermount *pu,
295 const char *mntfromname, const char *puffsname)
297 struct puffs_kargs *pargs = pu->pu_kargp;
299 (void)strlcpy(pargs->pa_mntfromname, mntfromname,
300 sizeof(pargs->pa_mntfromname));
301 (void)strlcpy(pargs->pa_typename, puffsname,
302 sizeof(pargs->pa_typename));
305 size_t
306 puffs_getmaxreqlen(struct puffs_usermount *pu)
309 return pu->pu_maxreqlen;
312 void
313 puffs_setmaxreqlen(struct puffs_usermount *pu, size_t reqlen)
316 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
317 warnx("puffs_setmaxreqlen: call has effect only "
318 "before mount\n");
320 pu->pu_kargp->pa_maxmsglen = reqlen;
323 void
324 puffs_setfhsize(struct puffs_usermount *pu, size_t fhsize, int flags)
327 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
328 warnx("puffs_setfhsize: call has effect only before mount\n");
330 pu->pu_kargp->pa_fhsize = fhsize;
331 pu->pu_kargp->pa_fhflags = flags;
334 void
335 puffs_setncookiehash(struct puffs_usermount *pu, int nhash)
338 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
339 warnx("puffs_setfhsize: call has effect only before mount\n");
341 pu->pu_kargp->pa_nhashbuckets = nhash;
344 void
345 puffs_set_pathbuild(struct puffs_usermount *pu, pu_pathbuild_fn fn)
348 pu->pu_pathbuild = fn;
351 void
352 puffs_set_pathtransform(struct puffs_usermount *pu, pu_pathtransform_fn fn)
355 pu->pu_pathtransform = fn;
358 void
359 puffs_set_pathcmp(struct puffs_usermount *pu, pu_pathcmp_fn fn)
362 pu->pu_pathcmp = fn;
365 void
366 puffs_set_pathfree(struct puffs_usermount *pu, pu_pathfree_fn fn)
369 pu->pu_pathfree = fn;
372 void
373 puffs_set_namemod(struct puffs_usermount *pu, pu_namemod_fn fn)
376 pu->pu_namemod = fn;
379 void
380 puffs_set_errnotify(struct puffs_usermount *pu, pu_errnotify_fn fn)
383 pu->pu_errnotify = fn;
386 void
387 puffs_set_cmap(struct puffs_usermount *pu, pu_cmap_fn fn)
390 pu->pu_cmap = fn;
393 void
394 puffs_ml_setloopfn(struct puffs_usermount *pu, puffs_ml_loop_fn lfn)
397 pu->pu_ml_lfn = lfn;
400 void
401 puffs_ml_settimeout(struct puffs_usermount *pu, struct timespec *ts)
404 if (ts == NULL) {
405 pu->pu_ml_timep = NULL;
406 } else {
407 pu->pu_ml_timeout = *ts;
408 pu->pu_ml_timep = &pu->pu_ml_timeout;
412 void
413 puffs_set_prepost(struct puffs_usermount *pu,
414 pu_prepost_fn pre, pu_prepost_fn pst)
417 pu->pu_oppre = pre;
418 pu->pu_oppost = pst;
421 void
422 puffs_setback(struct puffs_cc *pcc, int whatback)
424 struct puffs_req *preq = puffs__framebuf_getdataptr(pcc->pcc_pb);
426 assert(PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN && (
427 preq->preq_optype == PUFFS_VN_OPEN ||
428 preq->preq_optype == PUFFS_VN_MMAP ||
429 preq->preq_optype == PUFFS_VN_REMOVE ||
430 preq->preq_optype == PUFFS_VN_RMDIR ||
431 preq->preq_optype == PUFFS_VN_INACTIVE));
433 preq->preq_setbacks |= whatback & PUFFS_SETBACK_MASK;
437 puffs_daemon(struct puffs_usermount *pu, int nochdir, int noclose)
439 long int n;
440 int parent, value, fd;
442 if (pipe(pu->pu_dpipe) == -1)
443 return -1;
445 switch (fork()) {
446 case -1:
447 return -1;
448 case 0:
449 parent = 0;
450 break;
451 default:
452 parent = 1;
453 break;
455 pu->pu_state |= PU_PUFFSDAEMON;
457 if (parent) {
458 close(pu->pu_dpipe[1]);
459 n = read(pu->pu_dpipe[0], &value, sizeof(int));
460 if (n == -1)
461 err(1, "puffs_daemon");
462 if (n != sizeof(value))
463 errx(1, "puffs_daemon got %ld bytes", n);
464 if (value) {
465 errno = value;
466 err(1, "puffs_daemon");
468 exit(0);
469 } else {
470 if (setsid() == -1)
471 goto fail;
473 if (!nochdir)
474 chdir("/");
476 if (!noclose) {
477 fd = open(_PATH_DEVNULL, O_RDWR, 0);
478 if (fd == -1)
479 goto fail;
480 dup2(fd, STDIN_FILENO);
481 dup2(fd, STDOUT_FILENO);
482 dup2(fd, STDERR_FILENO);
483 if (fd > STDERR_FILENO)
484 close(fd);
486 return 0;
489 fail:
490 n = write(pu->pu_dpipe[1], &errno, sizeof(int));
491 assert(n == 4);
492 return -1;
495 static void
496 shutdaemon(struct puffs_usermount *pu, int error)
498 ssize_t n;
500 n = write(pu->pu_dpipe[1], &error, sizeof(int));
501 assert(n == 4);
502 close(pu->pu_dpipe[0]);
503 close(pu->pu_dpipe[1]);
504 pu->pu_state &= ~PU_PUFFSDAEMON;
508 puffs_mount(struct puffs_usermount *pu, const char *dir, int mntflags,
509 puffs_cookie_t cookie)
511 struct stat sb;
512 int rv, fd, sverrno;
513 char *comfd;
515 pu->pu_kargp->pa_root_cookie = cookie;
517 /* XXXkludgehere */
518 /* kauth doesn't provide this service any longer */
519 if (geteuid() != 0)
520 mntflags |= MNT_NOSUID | MNT_NODEV;
523 * Undocumented... Well, documented only here.
525 * This is used for imaginative purposes. If the env variable is
526 * set, puffs_mount() doesn't do the regular mount procedure.
527 * Rather, it crams the mount data down the comfd and sets comfd as
528 * the puffs descriptor.
530 * This shouldn't be used unless you can read my mind ( ... or write
531 * it, not to mention execute it, but that's starting to get silly).
533 if ((comfd = getenv("PUFFS_COMFD")) != NULL) {
534 size_t len;
536 if (sscanf(comfd, "%d", &pu->pu_fd) != 1) {
537 errno = EINVAL;
538 rv = -1;
539 goto out;
541 /* check that what we got at least resembles an fd */
542 if (fcntl(pu->pu_fd, F_GETFL) == -1) {
543 rv = -1;
544 goto out;
547 #define allwrite(buf, len) \
548 do { \
549 ssize_t al_rv; \
550 al_rv = write(pu->pu_fd, buf, len); \
551 if ((size_t)al_rv != len) { \
552 if (al_rv != -1) \
553 errno = EIO; \
554 rv = -1; \
555 goto out; \
557 } while (/*CONSTCOND*/0)
558 len = strlen(dir)+1;
559 allwrite(&len, sizeof(len));
560 allwrite(dir, len);
561 len = strlen(pu->pu_kargp->pa_mntfromname)+1;
562 allwrite(&len, sizeof(len));
563 allwrite(pu->pu_kargp->pa_mntfromname, len);
564 allwrite(&mntflags, sizeof(mntflags));
565 len = sizeof(*pu->pu_kargp);
566 allwrite(&len, sizeof(len));
567 allwrite(pu->pu_kargp, sizeof(*pu->pu_kargp));
568 allwrite(&pu->pu_flags, sizeof(pu->pu_flags));
569 #undef allwrite
571 rv = 0;
572 } else {
573 char rp[MAXPATHLEN];
575 if (realpath(dir, rp) == NULL) {
576 rv = -1;
577 goto out;
580 if (strcmp(dir, rp) != 0) {
581 warnx("puffs_mount: \"%s\" is a relative path.", dir);
582 warnx("puffs_mount: using \"%s\" instead.", rp);
585 fd = open(_PATH_PUFFS, O_RDWR);
586 if (fd == -1) {
587 warnx("puffs_mount: cannot open %s", _PATH_PUFFS);
588 rv = -1;
589 goto out;
591 if (fd <= 2)
592 warnx("puffs_mount: device fd %d (<= 2), sure this is "
593 "what you want?", fd);
595 pu->pu_fd = fd;
596 rv = fstat(fd, &sb);
597 if (rv == -1) {
598 warnx("puffs_mount: putter device stat failed");
599 goto out;
601 pu->pu_kargp->pa_minor = minor(sb.st_rdev);
603 if ((rv = mount(MOUNT_PUFFS, rp, mntflags,
604 pu->pu_kargp)) == -1)
605 goto out;
608 PU_SETSTATE(pu, PUFFS_STATE_RUNNING);
610 out:
611 if (rv != 0)
612 sverrno = errno;
613 else
614 sverrno = 0;
615 free(pu->pu_kargp);
616 pu->pu_kargp = NULL;
618 if (pu->pu_state & PU_PUFFSDAEMON)
619 shutdaemon(pu, sverrno);
621 errno = sverrno;
622 return rv;
625 struct puffs_usermount *
626 puffs_init(struct puffs_ops *pops, const char *mntfromname,
627 const char *puffsname, void *priv, uint32_t pflags)
629 struct puffs_usermount *pu;
630 struct puffs_kargs *pargs;
631 int sverrno;
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;
640 pu = malloc(sizeof(struct puffs_usermount));
641 if (pu == NULL)
642 goto failfree;
643 memset(pu, 0, sizeof(struct puffs_usermount));
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));
650 pargs->pa_vers = PUFFSVERSION;
651 pargs->pa_flags = PUFFS_FLAG_KERN(pflags);
652 fillvnopmask(pops, pargs);
653 puffs_setmntinfo(pu, mntfromname, puffsname);
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;
666 pu->pu_flags = pflags;
667 pu->pu_ops = *pops;
668 free(pops); /* XXX */
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);
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;
684 /* defaults for some user-settable translation functions */
685 pu->pu_cmap = NULL; /* identity translation */
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;
693 pu->pu_errnotify = puffs_kernerr_log;
695 PU_SETSTATE(pu, PUFFS_STATE_BEFOREMOUNT);
697 return pu;
699 failfree:
700 /* can't unmount() from here for obvious reasons */
701 sverrno = errno;
702 free(pu);
703 errno = sverrno;
704 return NULL;
707 void
708 puffs_cancel(struct puffs_usermount *pu, int error)
711 assert(puffs_getstate(pu) < PUFFS_STATE_RUNNING);
712 shutdaemon(pu, error);
713 free(pu);
716 /*ARGSUSED1*/
718 puffs_exit(struct puffs_usermount *pu, int unused /* strict compat */)
720 struct puffs_framebuf *pb;
721 struct puffs_req *preq;
722 void *winp;
723 size_t winlen;
724 int sverrno;
726 pb = puffs_framebuf_make();
727 if (pb == NULL) {
728 errno = ENOMEM;
729 return -1;
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;
739 preq = winp;
741 preq->preq_buflen = sizeof(struct puffs_req);
742 preq->preq_opclass = PUFFSOP_UNMOUNT;
743 preq->preq_id = puffs__nextreq(pu);
745 puffs_framev_enqueue_justsend(pu, puffs_getselectable(pu), pb, 1, 0);
747 return 0;
750 /* no sigset_t static intializer */
751 static int sigs[NSIG] = { 0, };
752 static int sigcatch = 0;
755 puffs_unmountonsignal(int sig, bool sigignore)
758 if (sig < 0 || sig >= (int)NSIG) {
759 errno = EINVAL;
760 return -1;
762 if (sigignore)
763 if (signal(sig, SIG_IGN) == SIG_ERR)
764 return -1;
766 if (!sigs[sig])
767 sigcatch++;
768 sigs[sig] = 1;
770 return 0;
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()).
778 void
779 puffs__theloop(struct puffs_cc *pcc)
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;
788 while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
791 * Schedule existing requests.
793 while ((pcc = TAILQ_FIRST(&pu->pu_sched)) != NULL) {
794 TAILQ_REMOVE(&pu->pu_sched, pcc, pcc_schedent);
795 puffs__goto(pcc);
798 if (pu->pu_ml_lfn)
799 pu->pu_ml_lfn(pu);
801 /* XXX: can we still do these optimizations? */
802 #if 0
804 * Do this here, because:
805 * a) loopfunc might generate some results
806 * b) it's still "after" event handling (except for round 1)
808 if (puffs_req_putput(ppr) == -1)
809 goto out;
810 puffs_req_resetput(ppr);
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;
819 #endif
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;
827 pfctrl = fio->fctrl;
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.
835 puffs__framev_output(pu, pfctrl, fio);
839 * Build list of which to enable/disable in writecheck.
841 nchanges = 0;
842 LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
843 if (fio->stat & FIO_WRGONE)
844 continue;
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 fio);
852 fio->stat |= FIO_WR;
853 nchanges++;
855 if (FIO_RM_WRITE(fio)) {
856 EV_SET(&pu->pu_evs[nchanges], fio->io_fd,
857 EVFILT_WRITE, EV_DISABLE, 0, 0,
858 fio);
859 fio->stat &= ~FIO_WR;
860 nchanges++;
864 ndone = kevent(pu->pu_kq, pu->pu_evs, nchanges,
865 pu->pu_evs, pu->pu_nevs, pu->pu_ml_timep);
867 if (ndone == -1) {
868 if (errno != EINTR)
869 break;
870 else
871 continue;
874 /* uoptimize */
875 if (ndone == 0)
876 continue;
878 /* iterate over the results */
879 for (curev = pu->pu_evs; ndone--; curev++) {
880 int what;
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;
889 #endif
891 fio = (void *)curev->udata;
892 if (__predict_true(fio != NULL))
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;
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;
907 what = 0;
908 if (curev->filter == EVFILT_READ) {
909 puffs__framev_input(pu, pfctrl, fio);
910 what |= PUFFS_FBIO_READ;
913 else if (curev->filter == EVFILT_WRITE) {
914 puffs__framev_output(pu, pfctrl, fio);
915 what |= PUFFS_FBIO_WRITE;
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);
924 if (what)
925 puffs__framev_notify(fio, what);
929 * Really free fd's now that we don't have references
930 * to them.
932 while ((fio = LIST_FIRST(&pu->pu_ios_rmlist)) != NULL) {
933 LIST_REMOVE(fio, fio_entries);
934 free(fio);
938 if (puffs__cc_restoremain(pu) == -1)
939 warn("cannot restore main context. impending doom");
942 puffs_mainloop(struct puffs_usermount *pu)
944 struct puffs_fctrl_io *fio;
945 struct puffs_cc *pcc;
946 struct kevent *curev;
947 size_t nevs;
948 int sverrno, i;
950 assert(puffs_getstate(pu) >= PUFFS_STATE_RUNNING);
952 pu->pu_kq = kqueue();
953 if (pu->pu_kq == -1)
954 goto out;
955 pu->pu_state |= PU_HASKQ;
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;
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;
970 LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
971 EV_SET(curev, fio->io_fd, EVFILT_READ, EV_ADD,
972 0, 0, fio);
973 curev++;
974 EV_SET(curev, fio->io_fd, EVFILT_WRITE, EV_ADD | EV_DISABLE,
975 0, 0, fio);
976 curev++;
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++;
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;
989 pu->pu_state |= PU_INLOOP;
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.
999 if (puffs__cc_create(pu, puffs__theloop, &pcc) == -1) {
1000 goto out;
1002 if (puffs__cc_savemain(pu) == -1) {
1003 goto out;
1005 if ((pu->pu_state & PU_MAINRESTORE) == 0)
1006 puffs_cc_continue(pcc);
1008 finalpush(pu);
1009 errno = 0;
1011 out:
1012 /* store the real error for a while */
1013 sverrno = errno;
1015 errno = sverrno;
1016 if (errno)
1017 return -1;
1018 else
1019 return 0;