puffs.c revision 1.79 1 /* $NetBSD: puffs.c,v 1.79 2007/12/04 21:24:11 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2005, 2006, 2007 Antti Kantee. All Rights Reserved.
5 *
6 * Development of this software was supported by the
7 * Google Summer of Code program and the Ulla Tuominen Foundation.
8 * The Google SoC project was mentored by Bill Studenmund.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
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.79 2007/12/04 21:24:11 pooka 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 <puffs.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <syslog.h>
51 #include <unistd.h>
52
53 #include "puffs_priv.h"
54
55 /* Most file systems want this for opts, so just give it to them */
56 const struct mntopt puffsmopts[] = {
57 MOPT_STDOPTS,
58 PUFFSMOPT_STD,
59 MOPT_NULL,
60 };
61
62 #ifdef PUFFS_WITH_THREADS
63 #include <pthread.h>
64 pthread_mutex_t pu_lock = PTHREAD_MUTEX_INITIALIZER;
65 #endif
66
67 #define FILLOP(lower, upper) \
68 do { \
69 if (pops->puffs_node_##lower) \
70 opmask[PUFFS_VN_##upper] = 1; \
71 } while (/*CONSTCOND*/0)
72 static void
73 fillvnopmask(struct puffs_ops *pops, uint8_t *opmask)
74 {
75
76 memset(opmask, 0, PUFFS_VN_MAX);
77
78 FILLOP(create, CREATE);
79 FILLOP(mknod, MKNOD);
80 FILLOP(open, OPEN);
81 FILLOP(close, CLOSE);
82 FILLOP(access, ACCESS);
83 FILLOP(getattr, GETATTR);
84 FILLOP(setattr, SETATTR);
85 FILLOP(poll, POLL); /* XXX: not ready in kernel */
86 FILLOP(mmap, MMAP);
87 FILLOP(fsync, FSYNC);
88 FILLOP(seek, SEEK);
89 FILLOP(remove, REMOVE);
90 FILLOP(link, LINK);
91 FILLOP(rename, RENAME);
92 FILLOP(mkdir, MKDIR);
93 FILLOP(rmdir, RMDIR);
94 FILLOP(symlink, SYMLINK);
95 FILLOP(readdir, READDIR);
96 FILLOP(readlink, READLINK);
97 FILLOP(reclaim, RECLAIM);
98 FILLOP(inactive, INACTIVE);
99 FILLOP(print, PRINT);
100 FILLOP(read, READ);
101 FILLOP(write, WRITE);
102 }
103 #undef FILLOP
104
105 /*
106 * Go over all framev entries and write everything we can. This is
107 * mostly for the benefit of delivering "unmount" to the kernel.
108 */
109 static void
110 finalpush(struct puffs_usermount *pu)
111 {
112 struct puffs_fctrl_io *fio;
113
114 LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
115 if (fio->stat & FIO_WRGONE)
116 continue;
117
118 puffs_framev_output(pu, fio->fctrl, fio);
119 }
120 }
121
122 /*ARGSUSED*/
123 static void
124 puffs_defaulterror(struct puffs_usermount *pu, uint8_t type,
125 int error, const char *str, void *cookie)
126 {
127
128 fprintf(stderr, "abort: type %d, error %d, cookie %p (%s)\n",
129 type, error, cookie, str);
130 abort();
131 }
132
133 int
134 puffs_getselectable(struct puffs_usermount *pu)
135 {
136
137 return pu->pu_fd;
138 }
139
140 int
141 puffs_setblockingmode(struct puffs_usermount *pu, int mode)
142 {
143 int rv, x;
144
145 assert(puffs_getstate(pu) == PUFFS_STATE_RUNNING);
146
147 if (mode != PUFFSDEV_BLOCK && mode != PUFFSDEV_NONBLOCK) {
148 errno = EINVAL;
149 return -1;
150 }
151
152 x = mode;
153 rv = ioctl(pu->pu_fd, FIONBIO, &x);
154
155 if (rv == 0) {
156 if (mode == PUFFSDEV_BLOCK)
157 pu->pu_state &= ~PU_ASYNCFD;
158 else
159 pu->pu_state |= PU_ASYNCFD;
160 }
161
162 return rv;
163 }
164
165 int
166 puffs_getstate(struct puffs_usermount *pu)
167 {
168
169 return pu->pu_state & PU_STATEMASK;
170 }
171
172 void
173 puffs_setstacksize(struct puffs_usermount *pu, size_t ss)
174 {
175 long psize;
176 int stackshift;
177
178 psize = sysconf(_SC_PAGESIZE);
179 if (ss < 2*psize) {
180 ss = 2*psize;
181 fprintf(stderr, "puffs_setstacksize: adjusting stacksize "
182 "to minimum %zu\n", ss);
183 }
184
185 assert(puffs_getstate(pu) == PUFFS_STATE_BEFOREMOUNT);
186 stackshift = -1;
187 while (ss) {
188 ss >>= 1;
189 stackshift++;
190 }
191 pu->pu_cc_stackshift = stackshift;
192 assert(1<<stackshift == ss);
193 }
194
195 struct puffs_pathobj *
196 puffs_getrootpathobj(struct puffs_usermount *pu)
197 {
198 struct puffs_node *pnr;
199
200 pnr = pu->pu_pn_root;
201 if (pnr == NULL) {
202 errno = ENOENT;
203 return NULL;
204 }
205
206 return &pnr->pn_po;
207 }
208
209 void
210 puffs_setroot(struct puffs_usermount *pu, struct puffs_node *pn)
211 {
212
213 pu->pu_pn_root = pn;
214 }
215
216 struct puffs_node *
217 puffs_getroot(struct puffs_usermount *pu)
218 {
219
220 return pu->pu_pn_root;
221 }
222
223 void
224 puffs_setrootinfo(struct puffs_usermount *pu, enum vtype vt,
225 vsize_t vsize, dev_t rdev)
226 {
227 struct puffs_kargs *pargs = pu->pu_kargp;
228
229 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT) {
230 warnx("puffs_setrootinfo: call has effect only "
231 "before mount\n");
232 return;
233 }
234
235 pargs->pa_root_vtype = vt;
236 pargs->pa_root_vsize = vsize;
237 pargs->pa_root_rdev = rdev;
238 }
239
240 void *
241 puffs_getspecific(struct puffs_usermount *pu)
242 {
243
244 return pu->pu_privdata;
245 }
246
247 size_t
248 puffs_getmaxreqlen(struct puffs_usermount *pu)
249 {
250
251 return pu->pu_maxreqlen;
252 }
253
254 void
255 puffs_setmaxreqlen(struct puffs_usermount *pu, size_t reqlen)
256 {
257
258 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
259 warnx("puffs_setmaxreqlen: call has effect only "
260 "before mount\n");
261
262 pu->pu_kargp->pa_maxmsglen = reqlen;
263 }
264
265 void
266 puffs_setfhsize(struct puffs_usermount *pu, size_t fhsize, int flags)
267 {
268
269 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
270 warnx("puffs_setfhsize: call has effect only before mount\n");
271
272 pu->pu_kargp->pa_fhsize = fhsize;
273 pu->pu_kargp->pa_fhflags = flags;
274 }
275
276 void
277 puffs_setncookiehash(struct puffs_usermount *pu, int nhash)
278 {
279
280 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
281 warnx("puffs_setfhsize: call has effect only before mount\n");
282
283 pu->pu_kargp->pa_nhashbuckets = nhash;
284 }
285
286 void
287 puffs_set_pathbuild(struct puffs_usermount *pu, pu_pathbuild_fn fn)
288 {
289
290 pu->pu_pathbuild = fn;
291 }
292
293 void
294 puffs_set_pathtransform(struct puffs_usermount *pu, pu_pathtransform_fn fn)
295 {
296
297 pu->pu_pathtransform = fn;
298 }
299
300 void
301 puffs_set_pathcmp(struct puffs_usermount *pu, pu_pathcmp_fn fn)
302 {
303
304 pu->pu_pathcmp = fn;
305 }
306
307 void
308 puffs_set_pathfree(struct puffs_usermount *pu, pu_pathfree_fn fn)
309 {
310
311 pu->pu_pathfree = fn;
312 }
313
314 void
315 puffs_set_namemod(struct puffs_usermount *pu, pu_namemod_fn fn)
316 {
317
318 pu->pu_namemod = fn;
319 }
320
321 void
322 puffs_set_errnotify(struct puffs_usermount *pu, pu_errnotify_fn fn)
323 {
324
325 pu->pu_errnotify = fn;
326 }
327
328 void
329 puffs_ml_setloopfn(struct puffs_usermount *pu, puffs_ml_loop_fn lfn)
330 {
331
332 pu->pu_ml_lfn = lfn;
333 }
334
335 void
336 puffs_ml_settimeout(struct puffs_usermount *pu, struct timespec *ts)
337 {
338
339 if (ts == NULL) {
340 pu->pu_ml_timep = NULL;
341 } else {
342 pu->pu_ml_timeout = *ts;
343 pu->pu_ml_timep = &pu->pu_ml_timeout;
344 }
345 }
346
347 void
348 puffs_set_prepost(struct puffs_usermount *pu,
349 pu_prepost_fn pre, pu_prepost_fn pst)
350 {
351
352 pu->pu_oppre = pre;
353 pu->pu_oppost = pst;
354 }
355
356 void
357 puffs_setback(struct puffs_cc *pcc, int whatback)
358 {
359 struct puffs_req *preq = puffs__framebuf_getdataptr(pcc->pcc_pb);
360
361 assert(PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN && (
362 preq->preq_optype == PUFFS_VN_OPEN ||
363 preq->preq_optype == PUFFS_VN_MMAP ||
364 preq->preq_optype == PUFFS_VN_REMOVE ||
365 preq->preq_optype == PUFFS_VN_RMDIR ||
366 preq->preq_optype == PUFFS_VN_INACTIVE));
367
368 preq->preq_setbacks |= whatback & PUFFS_SETBACK_MASK;
369 }
370
371 int
372 puffs_daemon(struct puffs_usermount *pu, int nochdir, int noclose)
373 {
374 ssize_t n;
375 int parent, value, fd;
376
377 if (pipe(pu->pu_dpipe) == -1)
378 return -1;
379
380 switch (fork()) {
381 case -1:
382 return -1;
383 case 0:
384 parent = 0;
385 break;
386 default:
387 parent = 1;
388 break;
389 }
390 pu->pu_state |= PU_PUFFSDAEMON;
391
392 if (parent) {
393 n = read(pu->pu_dpipe[0], &value, sizeof(int));
394 if (n == -1)
395 err(1, "puffs_daemon");
396 assert(n == sizeof(value));
397 if (value) {
398 errno = value;
399 err(1, "puffs_daemon");
400 }
401 exit(0);
402 } else {
403 if (setsid() == -1)
404 goto fail;
405
406 if (!nochdir)
407 chdir("/");
408
409 if (!noclose) {
410 fd = open(_PATH_DEVNULL, O_RDWR, 0);
411 if (fd == -1)
412 goto fail;
413 dup2(fd, STDIN_FILENO);
414 dup2(fd, STDOUT_FILENO);
415 dup2(fd, STDERR_FILENO);
416 if (fd > STDERR_FILENO)
417 close(fd);
418 }
419 return 0;
420 }
421
422 fail:
423 n = write(pu->pu_dpipe[1], &errno, sizeof(int));
424 assert(n == 4);
425 return -1;
426 }
427
428 int
429 puffs_mount(struct puffs_usermount *pu, const char *dir, int mntflags,
430 void *cookie)
431 {
432 char rp[MAXPATHLEN];
433 ssize_t n;
434 int rv, fd, sverrno;
435
436 #if 1
437 /* XXXkludgehere */
438 /* kauth doesn't provide this service any longer */
439 if (geteuid() != 0)
440 mntflags |= MNT_NOSUID | MNT_NODEV;
441 #endif
442
443 if (realpath(dir, rp) == NULL) {
444 rv = -1;
445 goto out;
446 }
447
448 if (strcmp(dir, rp) != 0) {
449 warnx("puffs_mount: \"%s\" is a relative path.", dir);
450 warnx("puffs_mount: using \"%s\" instead.", rp);
451 }
452
453 fd = open(_PATH_PUFFS, O_RDWR);
454 if (fd == -1) {
455 warnx("puffs_mount: cannot open %s", _PATH_PUFFS);
456 rv = -1;
457 goto out;
458 }
459 if (fd <= 2)
460 warnx("puffs_init: device fd %d (<= 2), sure this is "
461 "what you want?", fd);
462
463 pu->pu_kargp->pa_fd = pu->pu_fd = fd;
464 pu->pu_kargp->pa_root_cookie = cookie;
465 if ((rv = mount(MOUNT_PUFFS, rp, mntflags,
466 pu->pu_kargp, sizeof(struct puffs_kargs))) == -1)
467 goto out;
468 #if 0
469 if ((rv = ioctl(pu->pu_fd, PUFFSREQSIZEOP, &pu->pu_maxreqlen)) == -1)
470 goto out;
471 #endif
472 PU_SETSTATE(pu, PUFFS_STATE_RUNNING);
473
474 out:
475 if (rv != 0)
476 sverrno = errno;
477 else
478 sverrno = 0;
479 free(pu->pu_kargp);
480 pu->pu_kargp = NULL;
481
482 if (pu->pu_state & PU_PUFFSDAEMON) {
483 n = write(pu->pu_dpipe[1], &sverrno, sizeof(int));
484 assert(n == 4);
485 close(pu->pu_dpipe[0]);
486 close(pu->pu_dpipe[1]);
487 }
488
489 errno = sverrno;
490 return rv;
491 }
492
493 struct puffs_usermount *
494 _puffs_init(int develv, struct puffs_ops *pops, const char *mntfromname,
495 const char *puffsname, void *priv, uint32_t pflags)
496 {
497 struct puffs_usermount *pu;
498 struct puffs_kargs *pargs;
499 int sverrno;
500
501 if (develv != PUFFS_DEVEL_LIBVERSION) {
502 warnx("puffs_init: mounting with lib version %d, need %d",
503 develv, PUFFS_DEVEL_LIBVERSION);
504 errno = EINVAL;
505 return NULL;
506 }
507
508 pu = malloc(sizeof(struct puffs_usermount));
509 if (pu == NULL)
510 goto failfree;
511 memset(pu, 0, sizeof(struct puffs_usermount));
512
513 pargs = pu->pu_kargp = malloc(sizeof(struct puffs_kargs));
514 if (pargs == NULL)
515 goto failfree;
516 memset(pargs, 0, sizeof(struct puffs_kargs));
517
518 pargs->pa_vers = PUFFSDEVELVERS | PUFFSVERSION;
519 pargs->pa_flags = PUFFS_FLAG_KERN(pflags);
520 fillvnopmask(pops, pargs->pa_vnopmask);
521 (void)strlcpy(pargs->pa_typename, puffsname,
522 sizeof(pargs->pa_typename));
523 (void)strlcpy(pargs->pa_mntfromname, mntfromname,
524 sizeof(pargs->pa_mntfromname));
525
526 puffs_zerostatvfs(&pargs->pa_svfsb);
527 pargs->pa_root_cookie = NULL;
528 pargs->pa_root_vtype = VDIR;
529 pargs->pa_root_vsize = 0;
530 pargs->pa_root_rdev = 0;
531 pargs->pa_maxmsglen = 0;
532
533 pu->pu_flags = pflags;
534 pu->pu_ops = *pops;
535 free(pops); /* XXX */
536
537 pu->pu_privdata = priv;
538 pu->pu_cc_stackshift = PUFFS_CC_STACKSHIFT_DEFAULT;
539 LIST_INIT(&pu->pu_pnodelst);
540 LIST_INIT(&pu->pu_ios);
541 LIST_INIT(&pu->pu_ios_rmlist);
542 LIST_INIT(&pu->pu_ccnukelst);
543 TAILQ_INIT(&pu->pu_sched);
544 TAILQ_INIT(&pu->pu_exq);
545
546 pu->pu_framectrl[PU_FRAMECTRL_FS].rfb = puffs_fsframe_read;
547 pu->pu_framectrl[PU_FRAMECTRL_FS].wfb = puffs_fsframe_write;
548 pu->pu_framectrl[PU_FRAMECTRL_FS].cmpfb = puffs_fsframe_cmp;
549 pu->pu_framectrl[PU_FRAMECTRL_FS].gotfb = puffs_fsframe_gotframe;
550 pu->pu_framectrl[PU_FRAMECTRL_FS].fdnotfn = puffs_framev_unmountonclose;
551
552 /* defaults for some user-settable translation functions */
553 pu->pu_cmap = NULL; /* identity translation */
554
555 pu->pu_pathbuild = puffs_stdpath_buildpath;
556 pu->pu_pathfree = puffs_stdpath_freepath;
557 pu->pu_pathcmp = puffs_stdpath_cmppath;
558 pu->pu_pathtransform = NULL;
559 pu->pu_namemod = NULL;
560
561 pu->pu_errnotify = puffs_defaulterror;
562
563 PU_SETSTATE(pu, PUFFS_STATE_BEFOREMOUNT);
564
565 return pu;
566
567 failfree:
568 /* can't unmount() from here for obvious reasons */
569 sverrno = errno;
570 free(pu);
571 errno = sverrno;
572 return NULL;
573 }
574
575 /*
576 * XXX: there's currently no clean way to request unmount from
577 * within the user server, so be very brutal about it.
578 */
579 /*ARGSUSED1*/
580 int
581 puffs_exit(struct puffs_usermount *pu, int force)
582 {
583 struct puffs_node *pn;
584
585 force = 1; /* currently */
586
587 if (pu->pu_fd)
588 close(pu->pu_fd);
589
590 while ((pn = LIST_FIRST(&pu->pu_pnodelst)) != NULL)
591 puffs_pn_put(pn);
592
593 finalpush(pu);
594 puffs_framev_exit(pu);
595 if (pu->pu_state & PU_HASKQ)
596 close(pu->pu_kq);
597 free(pu);
598
599 return 0; /* always succesful for now, WILL CHANGE */
600 }
601
602 int
603 puffs_mainloop(struct puffs_usermount *pu)
604 {
605 struct puffs_framectrl *pfctrl;
606 struct puffs_fctrl_io *fio;
607 struct puffs_cc *pcc;
608 struct kevent *curev;
609 size_t nchanges;
610 int ndone, sverrno;
611
612 assert(puffs_getstate(pu) >= PUFFS_STATE_RUNNING);
613
614 pu->pu_kq = kqueue();
615 if (pu->pu_kq == -1)
616 goto out;
617 pu->pu_state |= PU_HASKQ;
618
619 puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK);
620 if (puffs__framev_addfd_ctrl(pu, puffs_getselectable(pu),
621 PUFFS_FBIO_READ | PUFFS_FBIO_WRITE,
622 &pu->pu_framectrl[PU_FRAMECTRL_FS]) == -1)
623 goto out;
624
625 curev = realloc(pu->pu_evs, (2*pu->pu_nfds)*sizeof(struct kevent));
626 if (curev == NULL)
627 goto out;
628 pu->pu_evs = curev;
629
630 LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
631 EV_SET(curev, fio->io_fd, EVFILT_READ, EV_ADD,
632 0, 0, (uintptr_t)fio);
633 curev++;
634 EV_SET(curev, fio->io_fd, EVFILT_WRITE, EV_ADD | EV_DISABLE,
635 0, 0, (uintptr_t)fio);
636 curev++;
637 }
638 if (kevent(pu->pu_kq, pu->pu_evs, 2*pu->pu_nfds, NULL, 0, NULL) == -1)
639 goto out;
640
641 pu->pu_state |= PU_INLOOP;
642
643 while (puffs_getstate(pu) != PUFFS_STATE_UNMOUNTED) {
644 if (pu->pu_ml_lfn)
645 pu->pu_ml_lfn(pu);
646
647 /* XXX: can we still do these optimizations? */
648 #if 0
649 /*
650 * Do this here, because:
651 * a) loopfunc might generate some results
652 * b) it's still "after" event handling (except for round 1)
653 */
654 if (puffs_req_putput(ppr) == -1)
655 goto out;
656 puffs_req_resetput(ppr);
657
658 /* micro optimization: skip kevent syscall if possible */
659 if (pu->pu_nfds == 1 && pu->pu_ml_timep == NULL
660 && (pu->pu_state & PU_ASYNCFD) == 0) {
661 pfctrl = XXX->fctrl;
662 puffs_framev_input(pu, pfctrl, XXX);
663 continue;
664 }
665 #endif
666
667 /* else: do full processing */
668 /* Don't bother worrying about O(n) for now */
669 nchanges = 0;
670
671 LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
672 if (fio->stat & FIO_WRGONE)
673 continue;
674
675 pfctrl = fio->fctrl;
676
677 /*
678 * Try to write out everything to avoid the
679 * need for enabling EVFILT_WRITE. The likely
680 * case is that we can fit everything into the
681 * socket buffer.
682 */
683 puffs_framev_output(pu, pfctrl, fio);
684 }
685
686 /*
687 * Build list of which to enable/disable in writecheck.
688 */
689 LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
690 if (fio->stat & FIO_WRGONE)
691 continue;
692
693 /* en/disable write checks for kqueue as needed */
694 assert((FIO_EN_WRITE(fio) && FIO_RM_WRITE(fio)) == 0);
695 if (FIO_EN_WRITE(fio)) {
696 EV_SET(&pu->pu_evs[nchanges], fio->io_fd,
697 EVFILT_WRITE, EV_ENABLE, 0, 0,
698 (uintptr_t)fio);
699 fio->stat |= FIO_WR;
700 nchanges++;
701 }
702 if (FIO_RM_WRITE(fio)) {
703 EV_SET(&pu->pu_evs[nchanges], fio->io_fd,
704 EVFILT_WRITE, EV_DISABLE, 0, 0,
705 (uintptr_t)fio);
706 fio->stat &= ~FIO_WR;
707 nchanges++;
708 }
709 assert(nchanges <= pu->pu_nfds);
710 }
711
712 ndone = kevent(pu->pu_kq, pu->pu_evs, nchanges,
713 pu->pu_evs, 2*pu->pu_nfds, pu->pu_ml_timep);
714
715 if (ndone == -1) {
716 if (errno != EINTR)
717 goto out;
718 else
719 continue;
720 }
721
722 /* uoptimize */
723 if (ndone == 0)
724 continue;
725
726 /* iterate over the results */
727 for (curev = pu->pu_evs; ndone--; curev++) {
728 int what;
729
730 #if 0
731 /* get & possibly dispatch events from kernel */
732 if (curev->ident == puffsfd) {
733 if (puffs_req_handle(pgr, ppr, 0) == -1)
734 goto out;
735 continue;
736 }
737 #endif
738
739 fio = (void *)curev->udata;
740 pfctrl = fio->fctrl;
741 if (curev->flags & EV_ERROR) {
742 assert(curev->filter == EVFILT_WRITE);
743 fio->stat &= ~FIO_WR;
744
745 /* XXX: how to know if it's a transient error */
746 puffs_framev_writeclose(pu, fio,
747 (int)curev->data);
748 puffs_framev_notify(fio, PUFFS_FBIO_ERROR);
749 continue;
750 }
751
752 what = 0;
753 if (curev->filter == EVFILT_READ) {
754 puffs_framev_input(pu, pfctrl, fio);
755 what |= PUFFS_FBIO_READ;
756 }
757
758 else if (curev->filter == EVFILT_WRITE) {
759 puffs_framev_output(pu, pfctrl, fio);
760 what |= PUFFS_FBIO_WRITE;
761 }
762 if (what)
763 puffs_framev_notify(fio, what);
764 }
765
766 /*
767 * Schedule continuations.
768 */
769 while ((pcc = TAILQ_FIRST(&pu->pu_sched)) != NULL) {
770 TAILQ_REMOVE(&pu->pu_sched, pcc, entries);
771 puffs_goto(pcc);
772 }
773
774 /*
775 * Really free fd's now that we don't have references
776 * to them.
777 */
778 while ((fio = LIST_FIRST(&pu->pu_ios_rmlist)) != NULL) {
779 LIST_REMOVE(fio, fio_entries);
780 free(fio);
781 }
782 }
783 finalpush(pu);
784 errno = 0;
785
786 out:
787 /* store the real error for a while */
788 sverrno = errno;
789
790 errno = sverrno;
791 if (errno)
792 return -1;
793 else
794 return 0;
795 }
796