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