puffs.c revision 1.92.4.6 1 /* $NetBSD: puffs.c,v 1.92.4.6 2011/07/17 15:36:03 riz 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.92.4.6 2011/07/17 15:36:03 riz 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 FILLOP(advlock, ADVLOCK);
103 FILLOP(abortop, ABORTOP);
104
105 FILLOP(getextattr, GETEXTATTR);
106 FILLOP(setextattr, SETEXTATTR);
107 FILLOP(listextattr, LISTEXTATTR);
108 FILLOP(deleteextattr, DELETEEXTATTR);
109 }
110 #undef FILLOP
111
112 /*
113 * Go over all framev entries and write everything we can. This is
114 * mostly for the benefit of delivering "unmount" to the kernel.
115 */
116 static void
117 finalpush(struct puffs_usermount *pu)
118 {
119 struct puffs_fctrl_io *fio;
120
121 LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
122 if (fio->stat & FIO_WRGONE)
123 continue;
124
125 puffs__framev_output(pu, fio->fctrl, fio);
126 }
127 }
128
129 /*ARGSUSED*/
130 static void
131 puffs_defaulterror(struct puffs_usermount *pu, uint8_t type,
132 int error, const char *str, puffs_cookie_t cookie)
133 {
134
135 fprintf(stderr, "abort: type %d, error %d, cookie %p (%s)\n",
136 type, error, cookie, str);
137 abort();
138 }
139
140 int
141 puffs_getselectable(struct puffs_usermount *pu)
142 {
143
144 return pu->pu_fd;
145 }
146
147 uint64_t
148 puffs__nextreq(struct puffs_usermount *pu)
149 {
150 uint64_t rv;
151
152 PU_LOCK();
153 rv = pu->pu_nextreq++;
154 PU_UNLOCK();
155
156 return rv;
157 }
158
159 int
160 puffs_setblockingmode(struct puffs_usermount *pu, int mode)
161 {
162 int rv, x;
163
164 assert(puffs_getstate(pu) == PUFFS_STATE_RUNNING);
165
166 if (mode != PUFFSDEV_BLOCK && mode != PUFFSDEV_NONBLOCK) {
167 errno = EINVAL;
168 return -1;
169 }
170
171 x = mode;
172 rv = ioctl(pu->pu_fd, FIONBIO, &x);
173
174 if (rv == 0) {
175 if (mode == PUFFSDEV_BLOCK)
176 pu->pu_state &= ~PU_ASYNCFD;
177 else
178 pu->pu_state |= PU_ASYNCFD;
179 }
180
181 return rv;
182 }
183
184 int
185 puffs_getstate(struct puffs_usermount *pu)
186 {
187
188 return pu->pu_state & PU_STATEMASK;
189 }
190
191 void
192 puffs_setstacksize(struct puffs_usermount *pu, size_t ss)
193 {
194 long psize, minsize;
195 int stackshift;
196 int bonus;
197
198 assert(puffs_getstate(pu) == PUFFS_STATE_BEFOREMOUNT);
199
200 psize = sysconf(_SC_PAGESIZE);
201 minsize = 4*psize;
202 if (ss < minsize || ss == PUFFS_STACKSIZE_MIN) {
203 if (ss != PUFFS_STACKSIZE_MIN)
204 fprintf(stderr, "puffs_setstacksize: adjusting "
205 "stacksize to minimum %ld\n", minsize);
206 ss = 4*psize;
207 }
208
209 stackshift = -1;
210 bonus = 0;
211 while (ss) {
212 if (ss & 0x1)
213 bonus++;
214 ss >>= 1;
215 stackshift++;
216 }
217 if (bonus > 1) {
218 stackshift++;
219 fprintf(stderr, "puffs_setstacksize: using next power of two: "
220 "%d\n", 1<<stackshift);
221 }
222
223 pu->pu_cc_stackshift = stackshift;
224 }
225
226 struct puffs_pathobj *
227 puffs_getrootpathobj(struct puffs_usermount *pu)
228 {
229 struct puffs_node *pnr;
230
231 pnr = pu->pu_pn_root;
232 if (pnr == NULL) {
233 errno = ENOENT;
234 return NULL;
235 }
236
237 return &pnr->pn_po;
238 }
239
240 void
241 puffs_setroot(struct puffs_usermount *pu, struct puffs_node *pn)
242 {
243
244 pu->pu_pn_root = pn;
245 }
246
247 struct puffs_node *
248 puffs_getroot(struct puffs_usermount *pu)
249 {
250
251 return pu->pu_pn_root;
252 }
253
254 void
255 puffs_setrootinfo(struct puffs_usermount *pu, enum vtype vt,
256 vsize_t vsize, dev_t rdev)
257 {
258 struct puffs_kargs *pargs = pu->pu_kargp;
259
260 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT) {
261 warnx("puffs_setrootinfo: call has effect only "
262 "before mount\n");
263 return;
264 }
265
266 pargs->pa_root_vtype = vt;
267 pargs->pa_root_vsize = vsize;
268 pargs->pa_root_rdev = rdev;
269 }
270
271 void *
272 puffs_getspecific(struct puffs_usermount *pu)
273 {
274
275 return pu->pu_privdata;
276 }
277
278 void
279 puffs_setspecific(struct puffs_usermount *pu, void *privdata)
280 {
281
282 pu->pu_privdata = privdata;
283 }
284
285 void
286 puffs_setmntinfo(struct puffs_usermount *pu,
287 const char *mntfromname, const char *puffsname)
288 {
289 struct puffs_kargs *pargs = pu->pu_kargp;
290
291 (void)strlcpy(pargs->pa_mntfromname, mntfromname,
292 sizeof(pargs->pa_mntfromname));
293 (void)strlcpy(pargs->pa_typename, puffsname,
294 sizeof(pargs->pa_typename));
295 }
296
297 size_t
298 puffs_getmaxreqlen(struct puffs_usermount *pu)
299 {
300
301 return pu->pu_maxreqlen;
302 }
303
304 void
305 puffs_setmaxreqlen(struct puffs_usermount *pu, size_t reqlen)
306 {
307
308 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
309 warnx("puffs_setmaxreqlen: call has effect only "
310 "before mount\n");
311
312 pu->pu_kargp->pa_maxmsglen = reqlen;
313 }
314
315 void
316 puffs_setfhsize(struct puffs_usermount *pu, size_t fhsize, int flags)
317 {
318
319 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
320 warnx("puffs_setfhsize: call has effect only before mount\n");
321
322 pu->pu_kargp->pa_fhsize = fhsize;
323 pu->pu_kargp->pa_fhflags = flags;
324 }
325
326 void
327 puffs_setncookiehash(struct puffs_usermount *pu, int nhash)
328 {
329
330 if (puffs_getstate(pu) != PUFFS_STATE_BEFOREMOUNT)
331 warnx("puffs_setfhsize: call has effect only before mount\n");
332
333 pu->pu_kargp->pa_nhashbuckets = nhash;
334 }
335
336 void
337 puffs_set_pathbuild(struct puffs_usermount *pu, pu_pathbuild_fn fn)
338 {
339
340 pu->pu_pathbuild = fn;
341 }
342
343 void
344 puffs_set_pathtransform(struct puffs_usermount *pu, pu_pathtransform_fn fn)
345 {
346
347 pu->pu_pathtransform = fn;
348 }
349
350 void
351 puffs_set_pathcmp(struct puffs_usermount *pu, pu_pathcmp_fn fn)
352 {
353
354 pu->pu_pathcmp = fn;
355 }
356
357 void
358 puffs_set_pathfree(struct puffs_usermount *pu, pu_pathfree_fn fn)
359 {
360
361 pu->pu_pathfree = fn;
362 }
363
364 void
365 puffs_set_namemod(struct puffs_usermount *pu, pu_namemod_fn fn)
366 {
367
368 pu->pu_namemod = fn;
369 }
370
371 void
372 puffs_set_errnotify(struct puffs_usermount *pu, pu_errnotify_fn fn)
373 {
374
375 pu->pu_errnotify = fn;
376 }
377
378 void
379 puffs_set_cmap(struct puffs_usermount *pu, pu_cmap_fn fn)
380 {
381
382 pu->pu_cmap = fn;
383 }
384
385 void
386 puffs_ml_setloopfn(struct puffs_usermount *pu, puffs_ml_loop_fn lfn)
387 {
388
389 pu->pu_ml_lfn = lfn;
390 }
391
392 void
393 puffs_ml_settimeout(struct puffs_usermount *pu, struct timespec *ts)
394 {
395
396 if (ts == NULL) {
397 pu->pu_ml_timep = NULL;
398 } else {
399 pu->pu_ml_timeout = *ts;
400 pu->pu_ml_timep = &pu->pu_ml_timeout;
401 }
402 }
403
404 void
405 puffs_set_prepost(struct puffs_usermount *pu,
406 pu_prepost_fn pre, pu_prepost_fn pst)
407 {
408
409 pu->pu_oppre = pre;
410 pu->pu_oppost = pst;
411 }
412
413 void
414 puffs_setback(struct puffs_cc *pcc, int whatback)
415 {
416 struct puffs_req *preq = puffs__framebuf_getdataptr(pcc->pcc_pb);
417
418 assert(PUFFSOP_OPCLASS(preq->preq_opclass) == PUFFSOP_VN && (
419 preq->preq_optype == PUFFS_VN_OPEN ||
420 preq->preq_optype == PUFFS_VN_MMAP ||
421 preq->preq_optype == PUFFS_VN_REMOVE ||
422 preq->preq_optype == PUFFS_VN_RMDIR ||
423 preq->preq_optype == PUFFS_VN_INACTIVE));
424
425 preq->preq_setbacks |= whatback & PUFFS_SETBACK_MASK;
426 }
427
428 int
429 puffs_daemon(struct puffs_usermount *pu, int nochdir, int noclose)
430 {
431 ssize_t n;
432 int parent, value, fd;
433
434 if (pipe(pu->pu_dpipe) == -1)
435 return -1;
436
437 switch (fork()) {
438 case -1:
439 return -1;
440 case 0:
441 parent = 0;
442 break;
443 default:
444 parent = 1;
445 break;
446 }
447 pu->pu_state |= PU_PUFFSDAEMON;
448
449 if (parent) {
450 n = read(pu->pu_dpipe[0], &value, sizeof(int));
451 if (n == -1)
452 err(1, "puffs_daemon");
453 assert(n == sizeof(value));
454 if (value) {
455 errno = value;
456 err(1, "puffs_daemon");
457 }
458 exit(0);
459 } else {
460 if (setsid() == -1)
461 goto fail;
462
463 if (!nochdir)
464 chdir("/");
465
466 if (!noclose) {
467 fd = open(_PATH_DEVNULL, O_RDWR, 0);
468 if (fd == -1)
469 goto fail;
470 dup2(fd, STDIN_FILENO);
471 dup2(fd, STDOUT_FILENO);
472 dup2(fd, STDERR_FILENO);
473 if (fd > STDERR_FILENO)
474 close(fd);
475 }
476 return 0;
477 }
478
479 fail:
480 n = write(pu->pu_dpipe[1], &errno, sizeof(int));
481 assert(n == 4);
482 return -1;
483 }
484
485 static void
486 shutdaemon(struct puffs_usermount *pu, int error)
487 {
488 ssize_t n;
489
490 n = write(pu->pu_dpipe[1], &error, sizeof(int));
491 assert(n == 4);
492 close(pu->pu_dpipe[0]);
493 close(pu->pu_dpipe[1]);
494 pu->pu_state &= ~PU_PUFFSDAEMON;
495 }
496
497 int
498 puffs_mount(struct puffs_usermount *pu, const char *dir, int mntflags,
499 puffs_cookie_t cookie)
500 {
501 char rp[MAXPATHLEN];
502 int rv, fd, sverrno;
503 char *comfd;
504
505 pu->pu_kargp->pa_root_cookie = cookie;
506
507 /* XXXkludgehere */
508 /* kauth doesn't provide this service any longer */
509 if (geteuid() != 0)
510 mntflags |= MNT_NOSUID | MNT_NODEV;
511
512 if (realpath(dir, rp) == NULL) {
513 rv = -1;
514 goto out;
515 }
516
517 if (strcmp(dir, rp) != 0) {
518 warnx("puffs_mount: \"%s\" is a relative path.", dir);
519 warnx("puffs_mount: using \"%s\" instead.", rp);
520 }
521
522 /*
523 * Undocumented... Well, documented only here.
524 *
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.
529 *
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).
532 */
533 if ((comfd = getenv("PUFFS_COMFD")) != NULL) {
534 size_t len;
535
536 if (sscanf(comfd, "%d", &pu->pu_fd) != 1) {
537 errno = EINVAL;
538 rv = -1;
539 goto out;
540 }
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;
545 }
546
547 len = strlen(dir)+1;
548
549 #define allwrite(buf, len) \
550 do { \
551 ssize_t al_rv; \
552 al_rv = write(pu->pu_fd, buf, len); \
553 if (al_rv != len) { \
554 if (al_rv != -1) \
555 errno = EIO; \
556 rv = -1; \
557 abort();\
558 goto out; \
559 } \
560 } while (/*CONSTCOND*/0)
561 allwrite(&len, sizeof(len));
562 allwrite(dir, len);
563 len = strlen(pu->pu_kargp->pa_mntfromname)+1;
564 allwrite(&len, sizeof(len));
565 allwrite(pu->pu_kargp->pa_mntfromname, len);
566 allwrite(&mntflags, sizeof(mntflags));
567 allwrite(pu->pu_kargp, sizeof(*pu->pu_kargp));
568 allwrite(&pu->pu_flags, sizeof(pu->pu_flags));
569 #undef allwrite
570
571 rv = 0;
572 } else {
573 fd = open(_PATH_PUFFS, O_RDWR);
574 if (fd == -1) {
575 warnx("puffs_mount: cannot open %s", _PATH_PUFFS);
576 rv = -1;
577 goto out;
578 }
579 if (fd <= 2)
580 warnx("puffs_mount: device fd %d (<= 2), sure this is "
581 "what you want?", fd);
582
583 pu->pu_kargp->pa_fd = pu->pu_fd = fd;
584 if ((rv = mount(MOUNT_PUFFS, rp, mntflags,
585 pu->pu_kargp, sizeof(struct puffs_kargs))) == -1)
586 goto out;
587 }
588
589 PU_SETSTATE(pu, PUFFS_STATE_RUNNING);
590
591 out:
592 if (rv != 0)
593 sverrno = errno;
594 else
595 sverrno = 0;
596 free(pu->pu_kargp);
597 pu->pu_kargp = NULL;
598
599 if (pu->pu_state & PU_PUFFSDAEMON)
600 shutdaemon(pu, sverrno);
601
602 errno = sverrno;
603 return rv;
604 }
605
606
607 /*ARGSUSED*/
608 struct puffs_usermount *
609 _puffs_init52(int dummy, struct puffs_ops *pops, const char *mntfromname,
610 const char *puffsname, void *priv, uint32_t pflags)
611 {
612 struct puffs_usermount *pu;
613 struct puffs_kargs *pargs;
614 int sverrno;
615
616 if (puffsname == PUFFS_DEFER)
617 puffsname = "n/a";
618 if (mntfromname == PUFFS_DEFER)
619 mntfromname = "n/a";
620 if (priv == PUFFS_DEFER)
621 priv = NULL;
622
623 pu = malloc(sizeof(struct puffs_usermount));
624 if (pu == NULL)
625 goto failfree;
626 memset(pu, 0, sizeof(struct puffs_usermount));
627
628 pargs = pu->pu_kargp = malloc(sizeof(struct puffs_kargs));
629 if (pargs == NULL)
630 goto failfree;
631 memset(pargs, 0, sizeof(struct puffs_kargs));
632
633 pargs->pa_vers = PUFFSDEVELVERS | PUFFSVERSION;
634 pargs->pa_flags = PUFFS_FLAG_KERN(pflags);
635 fillvnopmask(pops, pargs->pa_vnopmask);
636 puffs_setmntinfo(pu, mntfromname, puffsname);
637
638 puffs_zerostatvfs(&pargs->pa_svfsb);
639 pargs->pa_root_cookie = NULL;
640 pargs->pa_root_vtype = VDIR;
641 pargs->pa_root_vsize = 0;
642 pargs->pa_root_rdev = 0;
643 pargs->pa_maxmsglen = 0;
644
645 pu->pu_flags = pflags;
646 pu->pu_ops = *pops;
647 free(pops); /* XXX */
648
649 pu->pu_privdata = priv;
650 pu->pu_cc_stackshift = PUFFS_CC_STACKSHIFT_DEFAULT;
651 LIST_INIT(&pu->pu_pnodelst);
652 LIST_INIT(&pu->pu_ios);
653 LIST_INIT(&pu->pu_ios_rmlist);
654 LIST_INIT(&pu->pu_ccmagazin);
655 TAILQ_INIT(&pu->pu_sched);
656
657 pu->pu_framectrl[PU_FRAMECTRL_FS].rfb = puffs__fsframe_read;
658 pu->pu_framectrl[PU_FRAMECTRL_FS].wfb = puffs__fsframe_write;
659 pu->pu_framectrl[PU_FRAMECTRL_FS].cmpfb = puffs__fsframe_cmp;
660 pu->pu_framectrl[PU_FRAMECTRL_FS].gotfb = puffs__fsframe_gotframe;
661 pu->pu_framectrl[PU_FRAMECTRL_FS].fdnotfn = puffs_framev_unmountonclose;
662
663 /* defaults for some user-settable translation functions */
664 pu->pu_cmap = NULL; /* identity translation */
665
666 pu->pu_pathbuild = puffs_stdpath_buildpath;
667 pu->pu_pathfree = puffs_stdpath_freepath;
668 pu->pu_pathcmp = puffs_stdpath_cmppath;
669 pu->pu_pathtransform = NULL;
670 pu->pu_namemod = NULL;
671
672 pu->pu_errnotify = puffs_defaulterror;
673
674 PU_SETSTATE(pu, PUFFS_STATE_BEFOREMOUNT);
675
676 return pu;
677
678 failfree:
679 /* can't unmount() from here for obvious reasons */
680 sverrno = errno;
681 free(pu);
682 errno = sverrno;
683 return NULL;
684 }
685
686 struct puffs_usermount *
687 _puffs_init(int dummy, struct puffs_ops51 *pops51, const char *mntfromname,
688 const char *puffsname, void *priv, uint32_t pflags)
689 {
690 struct puffs_ops *pops;
691
692 PUFFSOP_INIT(pops);
693
694 pops->puffs_fs_unmount = pops51->puffs_fs_unmount;
695 pops->puffs_fs_statvfs = pops51->puffs_fs_statvfs;
696 pops->puffs_fs_sync = pops51->puffs_fs_sync;
697 pops->puffs_fs_fhtonode = pops51->puffs_fs_fhtonode;
698 pops->puffs_fs_nodetofh = pops51->puffs_fs_nodetofh;
699 pops->puffs_node_lookup = pops51->puffs_node_lookup;
700 pops->puffs_node_create = pops51->puffs_node_create;
701 pops->puffs_node_mknod = pops51->puffs_node_mknod;
702 pops->puffs_node_open = pops51->puffs_node_open;
703 pops->puffs_node_close = pops51->puffs_node_close;
704 pops->puffs_node_access = pops51->puffs_node_access;
705 pops->puffs_node_getattr = pops51->puffs_node_getattr;
706 pops->puffs_node_setattr = pops51->puffs_node_setattr;
707 pops->puffs_node_poll = pops51->puffs_node_poll;
708 pops->puffs_node_mmap = pops51->puffs_node_mmap;
709 pops->puffs_node_fsync = pops51->puffs_node_fsync;
710 pops->puffs_node_seek = pops51->puffs_node_seek;
711 pops->puffs_node_remove = pops51->puffs_node_remove;
712 pops->puffs_node_link = pops51->puffs_node_link;
713 pops->puffs_node_rename = pops51->puffs_node_rename;
714 pops->puffs_node_mkdir = pops51->puffs_node_mkdir;
715 pops->puffs_node_rmdir = pops51->puffs_node_rmdir;
716 pops->puffs_node_symlink = pops51->puffs_node_symlink;
717 pops->puffs_node_readdir = pops51->puffs_node_readdir;
718 pops->puffs_node_readlink = pops51->puffs_node_readlink;
719 pops->puffs_node_reclaim = pops51->puffs_node_reclaim;
720 pops->puffs_node_inactive = pops51->puffs_node_inactive;
721 pops->puffs_node_print = pops51->puffs_node_print;
722 pops->puffs_node_pathconf = pops51->puffs_node_pathconf;
723 pops->puffs_node_advlock = pops51->puffs_node_advlock;
724 pops->puffs_node_read = pops51->puffs_node_read;
725 pops->puffs_node_write = pops51->puffs_node_write;
726 pops->puffs_node_abortop = pops51->puffs_node_abortop;
727
728 free(pops51);
729
730 return _puffs_init52(dummy, pops, mntfromname,
731 puffsname, priv, pflags);
732 }
733
734
735 void
736 puffs_cancel(struct puffs_usermount *pu, int error)
737 {
738
739 assert(puffs_getstate(pu) < PUFFS_STATE_RUNNING);
740 shutdaemon(pu, error);
741 free(pu);
742 }
743
744 /*
745 * XXX: there's currently no clean way to request unmount from
746 * within the user server, so be very brutal about it.
747 */
748 /*ARGSUSED1*/
749 int
750 puffs_exit(struct puffs_usermount *pu, int force)
751 {
752 struct puffs_node *pn;
753
754 force = 1; /* currently */
755 assert((pu->pu_state & PU_PUFFSDAEMON) == 0);
756
757 if (pu->pu_fd)
758 close(pu->pu_fd);
759
760 while ((pn = LIST_FIRST(&pu->pu_pnodelst)) != NULL)
761 puffs_pn_put(pn);
762
763 finalpush(pu);
764 puffs__framev_exit(pu);
765 puffs__cc_exit(pu);
766 if (pu->pu_state & PU_HASKQ)
767 close(pu->pu_kq);
768 free(pu);
769
770 return 0; /* always succesful for now, WILL CHANGE */
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 * 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 (uintptr_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 (uintptr_t)fio);
858 fio->stat &= ~FIO_WR;
859 nchanges++;
860 }
861 assert(nchanges <= pu->pu_nfds);
862 }
863
864 ndone = kevent(pu->pu_kq, pu->pu_evs, nchanges,
865 pu->pu_evs, 2*pu->pu_nfds, 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 pfctrl = fio->fctrl;
893 if (curev->flags & EV_ERROR) {
894 assert(curev->filter == EVFILT_WRITE);
895 fio->stat &= ~FIO_WR;
896
897 /* XXX: how to know if it's a transient error */
898 puffs__framev_writeclose(pu, fio,
899 (int)curev->data);
900 puffs__framev_notify(fio, PUFFS_FBIO_ERROR);
901 continue;
902 }
903
904 what = 0;
905 if (curev->filter == EVFILT_READ) {
906 puffs__framev_input(pu, pfctrl, fio);
907 what |= PUFFS_FBIO_READ;
908 }
909
910 else if (curev->filter == EVFILT_WRITE) {
911 puffs__framev_output(pu, pfctrl, fio);
912 what |= PUFFS_FBIO_WRITE;
913 }
914 if (what)
915 puffs__framev_notify(fio, what);
916 }
917
918 /*
919 * Really free fd's now that we don't have references
920 * to them.
921 */
922 while ((fio = LIST_FIRST(&pu->pu_ios_rmlist)) != NULL) {
923 LIST_REMOVE(fio, fio_entries);
924 free(fio);
925 }
926 }
927
928 if (puffs__cc_restoremain(pu) == -1)
929 warn("cannot restore main context. impending doom");
930 }
931
932 int
933 puffs_mainloop(struct puffs_usermount *pu)
934 {
935 struct puffs_fctrl_io *fio;
936 struct puffs_cc *pcc;
937 struct kevent *curev;
938 int sverrno;
939
940 assert(puffs_getstate(pu) >= PUFFS_STATE_RUNNING);
941
942 pu->pu_kq = kqueue();
943 if (pu->pu_kq == -1)
944 goto out;
945 pu->pu_state |= PU_HASKQ;
946
947 puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK);
948 if (puffs__framev_addfd_ctrl(pu, puffs_getselectable(pu),
949 PUFFS_FBIO_READ | PUFFS_FBIO_WRITE,
950 &pu->pu_framectrl[PU_FRAMECTRL_FS]) == -1)
951 goto out;
952
953 curev = realloc(pu->pu_evs, (2*pu->pu_nfds)*sizeof(struct kevent));
954 if (curev == NULL)
955 goto out;
956 pu->pu_evs = curev;
957
958 LIST_FOREACH(fio, &pu->pu_ios, fio_entries) {
959 EV_SET(curev, fio->io_fd, EVFILT_READ, EV_ADD,
960 0, 0, (uintptr_t)fio);
961 curev++;
962 EV_SET(curev, fio->io_fd, EVFILT_WRITE, EV_ADD | EV_DISABLE,
963 0, 0, (uintptr_t)fio);
964 curev++;
965 }
966 if (kevent(pu->pu_kq, pu->pu_evs, 2*pu->pu_nfds, NULL, 0, NULL) == -1)
967 goto out;
968
969 pu->pu_state |= PU_INLOOP;
970
971 /*
972 * Create alternate execution context and jump to it. Note
973 * that we come "out" of savemain twice. Where we come out
974 * of it depends on the architecture. If the return address is
975 * stored on the stack, we jump out from puffs_cc_continue(),
976 * for a register return address from puffs__cc_savemain().
977 * PU_MAINRESTORE makes sure we DTRT in both cases.
978 */
979 if (puffs__cc_create(pu, puffs__theloop, &pcc) == -1) {
980 goto out;
981 }
982 if (puffs__cc_savemain(pu) == -1) {
983 goto out;
984 }
985 if ((pu->pu_state & PU_MAINRESTORE) == 0)
986 puffs_cc_continue(pcc);
987
988 finalpush(pu);
989 errno = 0;
990
991 out:
992 /* store the real error for a while */
993 sverrno = errno;
994
995 errno = sverrno;
996 if (errno)
997 return -1;
998 else
999 return 0;
1000 }
1001