1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2007-2009 Google Inc. and Amit Singh 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are 9 * met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above 14 * copyright notice, this list of conditions and the following disclaimer 15 * in the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Google Inc. nor the names of its 18 * contributors may be used to endorse or promote products derived from 19 * this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 * Copyright (C) 2005 Csaba Henk. 34 * All rights reserved. 35 * 36 * Copyright (c) 2019 The FreeBSD Foundation 37 * 38 * Portions of this software were developed by BFF Storage Systems, LLC under 39 * sponsorship from the FreeBSD Foundation. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 50 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60 * SUCH DAMAGE. 61 */ 62 63 #include <sys/cdefs.h> 64 __FBSDID("$FreeBSD$"); 65 66 #include <sys/param.h> 67 #include <sys/module.h> 68 #include <sys/systm.h> 69 #include <sys/counter.h> 70 #include <sys/errno.h> 71 #include <sys/kernel.h> 72 #include <sys/conf.h> 73 #include <sys/uio.h> 74 #include <sys/malloc.h> 75 #include <sys/queue.h> 76 #include <sys/lock.h> 77 #include <sys/sx.h> 78 #include <sys/mutex.h> 79 #include <sys/proc.h> 80 #include <sys/mount.h> 81 #include <sys/sdt.h> 82 #include <sys/vnode.h> 83 #include <sys/signalvar.h> 84 #include <sys/syscallsubr.h> 85 #include <sys/sysctl.h> 86 #include <vm/uma.h> 87 88 #include "fuse.h" 89 #include "fuse_node.h" 90 #include "fuse_ipc.h" 91 #include "fuse_internal.h" 92 93 SDT_PROVIDER_DECLARE(fusefs); 94 /* 95 * Fuse trace probe: 96 * arg0: verbosity. Higher numbers give more verbose messages 97 * arg1: Textual message 98 */ 99 SDT_PROBE_DEFINE2(fusefs, , ipc, trace, "int", "char*"); 100 101 static void fdisp_make_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op, 102 struct fuse_data *data, uint64_t nid, pid_t pid, struct ucred *cred); 103 static void fuse_interrupt_send(struct fuse_ticket *otick, int err); 104 static struct fuse_ticket *fticket_alloc(struct fuse_data *data); 105 static void fticket_refresh(struct fuse_ticket *ftick); 106 static void fticket_destroy(struct fuse_ticket *ftick); 107 static int fticket_wait_answer(struct fuse_ticket *ftick); 108 static inline int 109 fticket_aw_pull_uio(struct fuse_ticket *ftick, 110 struct uio *uio); 111 112 static int fuse_body_audit(struct fuse_ticket *ftick, size_t blen); 113 114 static fuse_handler_t fuse_standard_handler; 115 116 static counter_u64_t fuse_ticket_count; 117 SYSCTL_COUNTER_U64(_vfs_fusefs_stats, OID_AUTO, ticket_count, CTLFLAG_RD, 118 &fuse_ticket_count, "Number of allocated tickets"); 119 120 static long fuse_iov_permanent_bufsize = 1 << 19; 121 122 SYSCTL_LONG(_vfs_fusefs, OID_AUTO, iov_permanent_bufsize, CTLFLAG_RW, 123 &fuse_iov_permanent_bufsize, 0, 124 "limit for permanently stored buffer size for fuse_iovs"); 125 static int fuse_iov_credit = 16; 126 127 SYSCTL_INT(_vfs_fusefs, OID_AUTO, iov_credit, CTLFLAG_RW, 128 &fuse_iov_credit, 0, 129 "how many times is an oversized fuse_iov tolerated"); 130 131 MALLOC_DEFINE(M_FUSEMSG, "fuse_msgbuf", "fuse message buffer"); 132 static uma_zone_t ticket_zone; 133 134 /* 135 * TODO: figure out how to timeout INTERRUPT requests, because the daemon may 136 * leagally never respond 137 */ 138 static int 139 fuse_interrupt_callback(struct fuse_ticket *tick, struct uio *uio) 140 { 141 struct fuse_ticket *otick, *x_tick; 142 struct fuse_interrupt_in *fii; 143 struct fuse_data *data = tick->tk_data; 144 bool found = false; 145 146 fii = (struct fuse_interrupt_in*)((char*)tick->tk_ms_fiov.base + 147 sizeof(struct fuse_in_header)); 148 149 fuse_lck_mtx_lock(data->aw_mtx); 150 TAILQ_FOREACH_SAFE(otick, &data->aw_head, tk_aw_link, x_tick) { 151 if (otick->tk_unique == fii->unique) { 152 found = true; 153 break; 154 } 155 } 156 fuse_lck_mtx_unlock(data->aw_mtx); 157 158 if (!found) { 159 /* Original is already complete. Just return */ 160 return 0; 161 } 162 163 /* Clear the original ticket's interrupt association */ 164 otick->irq_unique = 0; 165 166 if (tick->tk_aw_ohead.error == ENOSYS) { 167 fsess_set_notimpl(data->mp, FUSE_INTERRUPT); 168 return 0; 169 } else if (tick->tk_aw_ohead.error == EAGAIN) { 170 /* 171 * There are two reasons we might get this: 172 * 1) the daemon received the INTERRUPT request before the 173 * original, or 174 * 2) the daemon received the INTERRUPT request after it 175 * completed the original request. 176 * In the first case we should re-send the INTERRUPT. In the 177 * second, we should ignore it. 178 */ 179 /* Resend */ 180 fuse_interrupt_send(otick, EINTR); 181 return 0; 182 } else { 183 /* Illegal FUSE_INTERRUPT response */ 184 return EINVAL; 185 } 186 } 187 188 /* Interrupt the operation otick. Return err as its error code */ 189 void 190 fuse_interrupt_send(struct fuse_ticket *otick, int err) 191 { 192 struct fuse_dispatcher fdi; 193 struct fuse_interrupt_in *fii; 194 struct fuse_in_header *ftick_hdr; 195 struct fuse_data *data = otick->tk_data; 196 struct fuse_ticket *tick, *xtick; 197 struct ucred reused_creds; 198 gid_t reused_groups[1]; 199 200 if (otick->irq_unique == 0) { 201 /* 202 * If the daemon hasn't yet received otick, then we can answer 203 * it ourselves and return. 204 */ 205 fuse_lck_mtx_lock(data->ms_mtx); 206 STAILQ_FOREACH_SAFE(tick, &otick->tk_data->ms_head, tk_ms_link, 207 xtick) { 208 if (tick == otick) { 209 STAILQ_REMOVE(&otick->tk_data->ms_head, tick, 210 fuse_ticket, tk_ms_link); 211 otick->tk_data->ms_count--; 212 otick->tk_ms_link.stqe_next = NULL; 213 fuse_lck_mtx_unlock(data->ms_mtx); 214 215 fuse_lck_mtx_lock(otick->tk_aw_mtx); 216 if (!fticket_answered(otick)) { 217 fticket_set_answered(otick); 218 otick->tk_aw_errno = err; 219 wakeup(otick); 220 } 221 fuse_lck_mtx_unlock(otick->tk_aw_mtx); 222 223 fuse_ticket_drop(tick); 224 return; 225 } 226 } 227 fuse_lck_mtx_unlock(data->ms_mtx); 228 229 /* 230 * If the fuse daemon doesn't support interrupts, then there's 231 * nothing more that we can do 232 */ 233 if (fsess_not_impl(data->mp, FUSE_INTERRUPT)) 234 return; 235 236 /* 237 * If the fuse daemon has already received otick, then we must 238 * send FUSE_INTERRUPT. 239 */ 240 ftick_hdr = fticket_in_header(otick); 241 reused_creds.cr_uid = ftick_hdr->uid; 242 reused_groups[0] = ftick_hdr->gid; 243 reused_creds.cr_groups = reused_groups; 244 fdisp_init(&fdi, sizeof(*fii)); 245 fdisp_make_pid(&fdi, FUSE_INTERRUPT, data, ftick_hdr->nodeid, 246 ftick_hdr->pid, &reused_creds); 247 248 fii = fdi.indata; 249 fii->unique = otick->tk_unique; 250 fuse_insert_callback(fdi.tick, fuse_interrupt_callback); 251 252 otick->irq_unique = fdi.tick->tk_unique; 253 /* Interrupt ops should be delivered ASAP */ 254 fuse_insert_message(fdi.tick, true); 255 fdisp_destroy(&fdi); 256 } else { 257 /* This ticket has already been interrupted */ 258 } 259 } 260 261 void 262 fiov_init(struct fuse_iov *fiov, size_t size) 263 { 264 uint32_t msize = FU_AT_LEAST(size); 265 266 fiov->len = 0; 267 268 fiov->base = malloc(msize, M_FUSEMSG, M_WAITOK | M_ZERO); 269 270 fiov->allocated_size = msize; 271 fiov->credit = fuse_iov_credit; 272 } 273 274 void 275 fiov_teardown(struct fuse_iov *fiov) 276 { 277 MPASS(fiov->base != NULL); 278 free(fiov->base, M_FUSEMSG); 279 } 280 281 void 282 fiov_adjust(struct fuse_iov *fiov, size_t size) 283 { 284 if (fiov->allocated_size < size || 285 (fuse_iov_permanent_bufsize >= 0 && 286 fiov->allocated_size - size > fuse_iov_permanent_bufsize && 287 --fiov->credit < 0)) { 288 fiov->base = realloc(fiov->base, FU_AT_LEAST(size), M_FUSEMSG, 289 M_WAITOK | M_ZERO); 290 if (!fiov->base) { 291 panic("FUSE: realloc failed"); 292 } 293 fiov->allocated_size = FU_AT_LEAST(size); 294 fiov->credit = fuse_iov_credit; 295 /* Clear data buffer after reallocation */ 296 bzero(fiov->base, size); 297 } else if (size > fiov->len) { 298 /* Clear newly extended portion of data buffer */ 299 bzero((char*)fiov->base + fiov->len, size - fiov->len); 300 } 301 fiov->len = size; 302 } 303 304 /* Resize the fiov if needed, and clear it's buffer */ 305 void 306 fiov_refresh(struct fuse_iov *fiov) 307 { 308 fiov_adjust(fiov, 0); 309 } 310 311 static int 312 fticket_ctor(void *mem, int size, void *arg, int flags) 313 { 314 struct fuse_ticket *ftick = mem; 315 struct fuse_data *data = arg; 316 317 FUSE_ASSERT_MS_DONE(ftick); 318 FUSE_ASSERT_AW_DONE(ftick); 319 320 ftick->tk_data = data; 321 322 if (ftick->tk_unique != 0) 323 fticket_refresh(ftick); 324 325 /* May be truncated to 32 bits */ 326 ftick->tk_unique = atomic_fetchadd_long(&data->ticketer, 1); 327 if (ftick->tk_unique == 0) 328 ftick->tk_unique = atomic_fetchadd_long(&data->ticketer, 1); 329 330 ftick->irq_unique = 0; 331 332 refcount_init(&ftick->tk_refcount, 1); 333 counter_u64_add(fuse_ticket_count, 1); 334 335 return 0; 336 } 337 338 static void 339 fticket_dtor(void *mem, int size, void *arg) 340 { 341 #ifdef INVARIANTS 342 struct fuse_ticket *ftick = mem; 343 #endif 344 345 FUSE_ASSERT_MS_DONE(ftick); 346 FUSE_ASSERT_AW_DONE(ftick); 347 348 counter_u64_add(fuse_ticket_count, -1); 349 } 350 351 static int 352 fticket_init(void *mem, int size, int flags) 353 { 354 struct fuse_ticket *ftick = mem; 355 356 bzero(ftick, sizeof(struct fuse_ticket)); 357 358 fiov_init(&ftick->tk_ms_fiov, sizeof(struct fuse_in_header)); 359 360 mtx_init(&ftick->tk_aw_mtx, "fuse answer delivery mutex", NULL, MTX_DEF); 361 fiov_init(&ftick->tk_aw_fiov, 0); 362 363 return 0; 364 } 365 366 static void 367 fticket_fini(void *mem, int size) 368 { 369 struct fuse_ticket *ftick = mem; 370 371 fiov_teardown(&ftick->tk_ms_fiov); 372 fiov_teardown(&ftick->tk_aw_fiov); 373 mtx_destroy(&ftick->tk_aw_mtx); 374 } 375 376 static inline struct fuse_ticket * 377 fticket_alloc(struct fuse_data *data) 378 { 379 return uma_zalloc_arg(ticket_zone, data, M_WAITOK); 380 } 381 382 static inline void 383 fticket_destroy(struct fuse_ticket *ftick) 384 { 385 return uma_zfree(ticket_zone, ftick); 386 } 387 388 static inline 389 void 390 fticket_refresh(struct fuse_ticket *ftick) 391 { 392 FUSE_ASSERT_MS_DONE(ftick); 393 FUSE_ASSERT_AW_DONE(ftick); 394 395 fiov_refresh(&ftick->tk_ms_fiov); 396 397 bzero(&ftick->tk_aw_ohead, sizeof(struct fuse_out_header)); 398 399 fiov_refresh(&ftick->tk_aw_fiov); 400 ftick->tk_aw_errno = 0; 401 ftick->tk_flag = 0; 402 } 403 404 /* Prepar the ticket to be reused, but don't clear its data buffers */ 405 static inline void 406 fticket_reset(struct fuse_ticket *ftick) 407 { 408 FUSE_ASSERT_MS_DONE(ftick); 409 FUSE_ASSERT_AW_DONE(ftick); 410 411 bzero(&ftick->tk_aw_ohead, sizeof(struct fuse_out_header)); 412 413 ftick->tk_aw_errno = 0; 414 ftick->tk_flag = 0; 415 } 416 417 static int 418 fticket_wait_answer(struct fuse_ticket *ftick) 419 { 420 struct thread *td = curthread; 421 sigset_t blockedset, oldset; 422 int err = 0, stops_deferred; 423 struct fuse_data *data = ftick->tk_data; 424 bool interrupted = false; 425 426 if (fsess_maybe_impl(ftick->tk_data->mp, FUSE_INTERRUPT) && 427 data->dataflags & FSESS_INTR) { 428 SIGEMPTYSET(blockedset); 429 } else { 430 /* Block all signals except (implicitly) SIGKILL */ 431 SIGFILLSET(blockedset); 432 } 433 stops_deferred = sigdeferstop(SIGDEFERSTOP_SILENT); 434 kern_sigprocmask(td, SIG_BLOCK, NULL, &oldset, 0); 435 436 fuse_lck_mtx_lock(ftick->tk_aw_mtx); 437 438 retry: 439 if (fticket_answered(ftick)) { 440 goto out; 441 } 442 443 if (fdata_get_dead(data)) { 444 err = ENOTCONN; 445 fticket_set_answered(ftick); 446 goto out; 447 } 448 kern_sigprocmask(td, SIG_BLOCK, &blockedset, NULL, 0); 449 err = msleep(ftick, &ftick->tk_aw_mtx, PCATCH, "fu_ans", 450 data->daemon_timeout * hz); 451 kern_sigprocmask(td, SIG_SETMASK, &oldset, NULL, 0); 452 if (err == EWOULDBLOCK) { 453 SDT_PROBE2(fusefs, , ipc, trace, 3, 454 "fticket_wait_answer: EWOULDBLOCK"); 455 #ifdef XXXIP /* die conditionally */ 456 if (!fdata_get_dead(data)) { 457 fdata_set_dead(data); 458 } 459 #endif 460 err = ETIMEDOUT; 461 fticket_set_answered(ftick); 462 } else if ((err == EINTR || err == ERESTART)) { 463 /* 464 * Whether we get EINTR or ERESTART depends on whether 465 * SA_RESTART was set by sigaction(2). 466 * 467 * Try to interrupt the operation and wait for an EINTR response 468 * to the original operation. If the file system does not 469 * support FUSE_INTERRUPT, then we'll just wait for it to 470 * complete like normal. If it does support FUSE_INTERRUPT, 471 * then it will either respond EINTR to the original operation, 472 * or EAGAIN to the interrupt. 473 */ 474 sigset_t tmpset; 475 476 SDT_PROBE2(fusefs, , ipc, trace, 4, 477 "fticket_wait_answer: interrupt"); 478 fuse_lck_mtx_unlock(ftick->tk_aw_mtx); 479 fuse_interrupt_send(ftick, err); 480 481 PROC_LOCK(td->td_proc); 482 mtx_lock(&td->td_proc->p_sigacts->ps_mtx); 483 tmpset = td->td_proc->p_siglist; 484 SIGSETOR(tmpset, td->td_siglist); 485 mtx_unlock(&td->td_proc->p_sigacts->ps_mtx); 486 PROC_UNLOCK(td->td_proc); 487 488 fuse_lck_mtx_lock(ftick->tk_aw_mtx); 489 if (!interrupted && !SIGISMEMBER(tmpset, SIGKILL)) { 490 /* 491 * Block all signals while we wait for an interrupt 492 * response. The protocol doesn't discriminate between 493 * different signals. 494 */ 495 SIGFILLSET(blockedset); 496 interrupted = true; 497 goto retry; 498 } else { 499 /* 500 * Return immediately for fatal signals, or if this is 501 * the second interruption. We should only be 502 * interrupted twice if the thread is stopped, for 503 * example during sigexit. 504 */ 505 } 506 } else if (err) { 507 SDT_PROBE2(fusefs, , ipc, trace, 6, 508 "fticket_wait_answer: other error"); 509 } else { 510 SDT_PROBE2(fusefs, , ipc, trace, 7, "fticket_wait_answer: OK"); 511 } 512 out: 513 if (!(err || fticket_answered(ftick))) { 514 SDT_PROBE2(fusefs, , ipc, trace, 1, 515 "FUSE: requester was woken up but still no answer"); 516 err = ENXIO; 517 } 518 fuse_lck_mtx_unlock(ftick->tk_aw_mtx); 519 sigallowstop(stops_deferred); 520 521 return err; 522 } 523 524 static inline 525 int 526 fticket_aw_pull_uio(struct fuse_ticket *ftick, struct uio *uio) 527 { 528 int err = 0; 529 size_t len = uio_resid(uio); 530 531 if (len) { 532 fiov_adjust(fticket_resp(ftick), len); 533 err = uiomove(fticket_resp(ftick)->base, len, uio); 534 } 535 return err; 536 } 537 538 int 539 fticket_pull(struct fuse_ticket *ftick, struct uio *uio) 540 { 541 int err = 0; 542 543 if (ftick->tk_aw_ohead.error) { 544 return 0; 545 } 546 err = fuse_body_audit(ftick, uio_resid(uio)); 547 if (!err) { 548 err = fticket_aw_pull_uio(ftick, uio); 549 } 550 return err; 551 } 552 553 struct fuse_data * 554 fdata_alloc(struct cdev *fdev, struct ucred *cred) 555 { 556 struct fuse_data *data; 557 558 data = malloc(sizeof(struct fuse_data), M_FUSEMSG, M_WAITOK | M_ZERO); 559 560 data->fdev = fdev; 561 mtx_init(&data->ms_mtx, "fuse message list mutex", NULL, MTX_DEF); 562 STAILQ_INIT(&data->ms_head); 563 data->ms_count = 0; 564 knlist_init_mtx(&data->ks_rsel.si_note, &data->ms_mtx); 565 mtx_init(&data->aw_mtx, "fuse answer list mutex", NULL, MTX_DEF); 566 TAILQ_INIT(&data->aw_head); 567 data->daemoncred = crhold(cred); 568 data->daemon_timeout = FUSE_DEFAULT_DAEMON_TIMEOUT; 569 sx_init(&data->rename_lock, "fuse rename lock"); 570 data->ref = 1; 571 572 return data; 573 } 574 575 void 576 fdata_trydestroy(struct fuse_data *data) 577 { 578 data->ref--; 579 MPASS(data->ref >= 0); 580 if (data->ref != 0) 581 return; 582 583 /* Driving off stage all that stuff thrown at device... */ 584 sx_destroy(&data->rename_lock); 585 crfree(data->daemoncred); 586 mtx_destroy(&data->aw_mtx); 587 knlist_delete(&data->ks_rsel.si_note, curthread, 0); 588 knlist_destroy(&data->ks_rsel.si_note); 589 mtx_destroy(&data->ms_mtx); 590 591 free(data, M_FUSEMSG); 592 } 593 594 void 595 fdata_set_dead(struct fuse_data *data) 596 { 597 FUSE_LOCK(); 598 if (fdata_get_dead(data)) { 599 FUSE_UNLOCK(); 600 return; 601 } 602 fuse_lck_mtx_lock(data->ms_mtx); 603 data->dataflags |= FSESS_DEAD; 604 wakeup_one(data); 605 selwakeuppri(&data->ks_rsel, PZERO + 1); 606 wakeup(&data->ticketer); 607 fuse_lck_mtx_unlock(data->ms_mtx); 608 FUSE_UNLOCK(); 609 } 610 611 struct fuse_ticket * 612 fuse_ticket_fetch(struct fuse_data *data) 613 { 614 int err = 0; 615 struct fuse_ticket *ftick; 616 617 ftick = fticket_alloc(data); 618 619 if (!(data->dataflags & FSESS_INITED)) { 620 /* Sleep until get answer for INIT messsage */ 621 FUSE_LOCK(); 622 if (!(data->dataflags & FSESS_INITED) && data->ticketer > 2) { 623 err = msleep(&data->ticketer, &fuse_mtx, PCATCH | PDROP, 624 "fu_ini", 0); 625 if (err) 626 fdata_set_dead(data); 627 } else 628 FUSE_UNLOCK(); 629 } 630 return ftick; 631 } 632 633 int 634 fuse_ticket_drop(struct fuse_ticket *ftick) 635 { 636 int die; 637 638 die = refcount_release(&ftick->tk_refcount); 639 if (die) 640 fticket_destroy(ftick); 641 642 return die; 643 } 644 645 void 646 fuse_insert_callback(struct fuse_ticket *ftick, fuse_handler_t * handler) 647 { 648 if (fdata_get_dead(ftick->tk_data)) { 649 return; 650 } 651 ftick->tk_aw_handler = handler; 652 653 fuse_lck_mtx_lock(ftick->tk_data->aw_mtx); 654 fuse_aw_push(ftick); 655 fuse_lck_mtx_unlock(ftick->tk_data->aw_mtx); 656 } 657 658 /* 659 * Insert a new upgoing ticket into the message queue 660 * 661 * If urgent is true, insert at the front of the queue. Otherwise, insert in 662 * FIFO order. 663 */ 664 void 665 fuse_insert_message(struct fuse_ticket *ftick, bool urgent) 666 { 667 if (ftick->tk_flag & FT_DIRTY) { 668 panic("FUSE: ticket reused without being refreshed"); 669 } 670 ftick->tk_flag |= FT_DIRTY; 671 672 if (fdata_get_dead(ftick->tk_data)) { 673 return; 674 } 675 fuse_lck_mtx_lock(ftick->tk_data->ms_mtx); 676 if (urgent) 677 fuse_ms_push_head(ftick); 678 else 679 fuse_ms_push(ftick); 680 wakeup_one(ftick->tk_data); 681 selwakeuppri(&ftick->tk_data->ks_rsel, PZERO + 1); 682 KNOTE_LOCKED(&ftick->tk_data->ks_rsel.si_note, 0); 683 fuse_lck_mtx_unlock(ftick->tk_data->ms_mtx); 684 } 685 686 static int 687 fuse_body_audit(struct fuse_ticket *ftick, size_t blen) 688 { 689 int err = 0; 690 enum fuse_opcode opcode; 691 692 opcode = fticket_opcode(ftick); 693 694 switch (opcode) { 695 case FUSE_BMAP: 696 err = (blen == sizeof(struct fuse_bmap_out)) ? 0 : EINVAL; 697 break; 698 699 case FUSE_LINK: 700 case FUSE_LOOKUP: 701 case FUSE_MKDIR: 702 case FUSE_MKNOD: 703 case FUSE_SYMLINK: 704 if (fuse_libabi_geq(ftick->tk_data, 7, 9)) { 705 err = (blen == sizeof(struct fuse_entry_out)) ? 706 0 : EINVAL; 707 } else { 708 err = (blen == FUSE_COMPAT_ENTRY_OUT_SIZE) ? 0 : EINVAL; 709 } 710 break; 711 712 case FUSE_FORGET: 713 panic("FUSE: a handler has been intalled for FUSE_FORGET"); 714 break; 715 716 case FUSE_GETATTR: 717 case FUSE_SETATTR: 718 if (fuse_libabi_geq(ftick->tk_data, 7, 9)) { 719 err = (blen == sizeof(struct fuse_attr_out)) ? 720 0 : EINVAL; 721 } else { 722 err = (blen == FUSE_COMPAT_ATTR_OUT_SIZE) ? 0 : EINVAL; 723 } 724 break; 725 726 case FUSE_READLINK: 727 err = (PAGE_SIZE >= blen) ? 0 : EINVAL; 728 break; 729 730 case FUSE_UNLINK: 731 err = (blen == 0) ? 0 : EINVAL; 732 break; 733 734 case FUSE_RMDIR: 735 err = (blen == 0) ? 0 : EINVAL; 736 break; 737 738 case FUSE_RENAME: 739 err = (blen == 0) ? 0 : EINVAL; 740 break; 741 742 case FUSE_OPEN: 743 err = (blen == sizeof(struct fuse_open_out)) ? 0 : EINVAL; 744 break; 745 746 case FUSE_READ: 747 err = (((struct fuse_read_in *)( 748 (char *)ftick->tk_ms_fiov.base + 749 sizeof(struct fuse_in_header) 750 ))->size >= blen) ? 0 : EINVAL; 751 break; 752 753 case FUSE_WRITE: 754 err = (blen == sizeof(struct fuse_write_out)) ? 0 : EINVAL; 755 break; 756 757 case FUSE_STATFS: 758 if (fuse_libabi_geq(ftick->tk_data, 7, 4)) { 759 err = (blen == sizeof(struct fuse_statfs_out)) ? 760 0 : EINVAL; 761 } else { 762 err = (blen == FUSE_COMPAT_STATFS_SIZE) ? 0 : EINVAL; 763 } 764 break; 765 766 case FUSE_RELEASE: 767 err = (blen == 0) ? 0 : EINVAL; 768 break; 769 770 case FUSE_FSYNC: 771 err = (blen == 0) ? 0 : EINVAL; 772 break; 773 774 case FUSE_SETXATTR: 775 err = (blen == 0) ? 0 : EINVAL; 776 break; 777 778 case FUSE_GETXATTR: 779 case FUSE_LISTXATTR: 780 /* 781 * These can have varying response lengths, and 0 length 782 * isn't necessarily invalid. 783 */ 784 err = 0; 785 break; 786 787 case FUSE_REMOVEXATTR: 788 err = (blen == 0) ? 0 : EINVAL; 789 break; 790 791 case FUSE_FLUSH: 792 err = (blen == 0) ? 0 : EINVAL; 793 break; 794 795 case FUSE_INIT: 796 if (blen == sizeof(struct fuse_init_out) || 797 blen == FUSE_COMPAT_INIT_OUT_SIZE || 798 blen == FUSE_COMPAT_22_INIT_OUT_SIZE) { 799 err = 0; 800 } else { 801 err = EINVAL; 802 } 803 break; 804 805 case FUSE_OPENDIR: 806 err = (blen == sizeof(struct fuse_open_out)) ? 0 : EINVAL; 807 break; 808 809 case FUSE_READDIR: 810 err = (((struct fuse_read_in *)( 811 (char *)ftick->tk_ms_fiov.base + 812 sizeof(struct fuse_in_header) 813 ))->size >= blen) ? 0 : EINVAL; 814 break; 815 816 case FUSE_RELEASEDIR: 817 err = (blen == 0) ? 0 : EINVAL; 818 break; 819 820 case FUSE_FSYNCDIR: 821 err = (blen == 0) ? 0 : EINVAL; 822 break; 823 824 case FUSE_GETLK: 825 err = (blen == sizeof(struct fuse_lk_out)) ? 0 : EINVAL; 826 break; 827 828 case FUSE_SETLK: 829 err = (blen == 0) ? 0 : EINVAL; 830 break; 831 832 case FUSE_SETLKW: 833 err = (blen == 0) ? 0 : EINVAL; 834 break; 835 836 case FUSE_ACCESS: 837 err = (blen == 0) ? 0 : EINVAL; 838 break; 839 840 case FUSE_CREATE: 841 if (fuse_libabi_geq(ftick->tk_data, 7, 9)) { 842 err = (blen == sizeof(struct fuse_entry_out) + 843 sizeof(struct fuse_open_out)) ? 0 : EINVAL; 844 } else { 845 err = (blen == FUSE_COMPAT_ENTRY_OUT_SIZE + 846 sizeof(struct fuse_open_out)) ? 0 : EINVAL; 847 } 848 break; 849 850 case FUSE_DESTROY: 851 err = (blen == 0) ? 0 : EINVAL; 852 break; 853 854 case FUSE_LSEEK: 855 err = (blen == sizeof(struct fuse_lseek_out)) ? 0 : EINVAL; 856 break; 857 858 case FUSE_COPY_FILE_RANGE: 859 err = (blen == sizeof(struct fuse_write_out)) ? 0 : EINVAL; 860 break; 861 862 default: 863 panic("FUSE: opcodes out of sync (%d)\n", opcode); 864 } 865 866 return err; 867 } 868 869 static inline void 870 fuse_setup_ihead(struct fuse_in_header *ihead, struct fuse_ticket *ftick, 871 uint64_t nid, enum fuse_opcode op, size_t blen, pid_t pid, 872 struct ucred *cred) 873 { 874 ihead->len = sizeof(*ihead) + blen; 875 ihead->unique = ftick->tk_unique; 876 ihead->nodeid = nid; 877 ihead->opcode = op; 878 879 ihead->pid = pid; 880 ihead->uid = cred->cr_uid; 881 ihead->gid = cred->cr_groups[0]; 882 } 883 884 /* 885 * fuse_standard_handler just pulls indata and wakes up pretender. 886 * Doesn't try to interpret data, that's left for the pretender. 887 * Though might do a basic size verification before the pull-in takes place 888 */ 889 890 static int 891 fuse_standard_handler(struct fuse_ticket *ftick, struct uio *uio) 892 { 893 int err = 0; 894 895 err = fticket_pull(ftick, uio); 896 897 fuse_lck_mtx_lock(ftick->tk_aw_mtx); 898 899 if (!fticket_answered(ftick)) { 900 fticket_set_answered(ftick); 901 ftick->tk_aw_errno = err; 902 wakeup(ftick); 903 } 904 fuse_lck_mtx_unlock(ftick->tk_aw_mtx); 905 906 return err; 907 } 908 909 /* 910 * Reinitialize a dispatcher from a pid and node id, without resizing or 911 * clearing its data buffers 912 */ 913 static void 914 fdisp_refresh_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op, 915 struct mount *mp, uint64_t nid, pid_t pid, struct ucred *cred) 916 { 917 MPASS(fdip->tick); 918 MPASS2(sizeof(fdip->finh) + fdip->iosize <= fdip->tick->tk_ms_fiov.len, 919 "Must use fdisp_make_pid to increase the size of the fiov"); 920 fticket_reset(fdip->tick); 921 922 FUSE_DIMALLOC(&fdip->tick->tk_ms_fiov, fdip->finh, 923 fdip->indata, fdip->iosize); 924 925 fuse_setup_ihead(fdip->finh, fdip->tick, nid, op, fdip->iosize, pid, 926 cred); 927 } 928 929 /* Initialize a dispatcher from a pid and node id */ 930 static void 931 fdisp_make_pid(struct fuse_dispatcher *fdip, enum fuse_opcode op, 932 struct fuse_data *data, uint64_t nid, pid_t pid, struct ucred *cred) 933 { 934 if (fdip->tick) { 935 fticket_refresh(fdip->tick); 936 } else { 937 fdip->tick = fuse_ticket_fetch(data); 938 } 939 940 /* FUSE_DIMALLOC will bzero the fiovs when it enlarges them */ 941 FUSE_DIMALLOC(&fdip->tick->tk_ms_fiov, fdip->finh, 942 fdip->indata, fdip->iosize); 943 944 fuse_setup_ihead(fdip->finh, fdip->tick, nid, op, fdip->iosize, pid, cred); 945 } 946 947 void 948 fdisp_make(struct fuse_dispatcher *fdip, enum fuse_opcode op, struct mount *mp, 949 uint64_t nid, struct thread *td, struct ucred *cred) 950 { 951 struct fuse_data *data = fuse_get_mpdata(mp); 952 RECTIFY_TDCR(td, cred); 953 954 return fdisp_make_pid(fdip, op, data, nid, td->td_proc->p_pid, cred); 955 } 956 957 void 958 fdisp_make_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op, 959 struct vnode *vp, struct thread *td, struct ucred *cred) 960 { 961 struct mount *mp = vnode_mount(vp); 962 struct fuse_data *data = fuse_get_mpdata(mp); 963 964 RECTIFY_TDCR(td, cred); 965 return fdisp_make_pid(fdip, op, data, VTOI(vp), 966 td->td_proc->p_pid, cred); 967 } 968 969 /* Refresh a fuse_dispatcher so it can be reused, but don't zero its data */ 970 void 971 fdisp_refresh_vp(struct fuse_dispatcher *fdip, enum fuse_opcode op, 972 struct vnode *vp, struct thread *td, struct ucred *cred) 973 { 974 RECTIFY_TDCR(td, cred); 975 return fdisp_refresh_pid(fdip, op, vnode_mount(vp), VTOI(vp), 976 td->td_proc->p_pid, cred); 977 } 978 979 void 980 fdisp_refresh(struct fuse_dispatcher *fdip) 981 { 982 fticket_refresh(fdip->tick); 983 } 984 985 SDT_PROBE_DEFINE2(fusefs, , ipc, fdisp_wait_answ_error, "char*", "int"); 986 987 int 988 fdisp_wait_answ(struct fuse_dispatcher *fdip) 989 { 990 int err = 0; 991 992 fdip->answ_stat = 0; 993 fuse_insert_callback(fdip->tick, fuse_standard_handler); 994 fuse_insert_message(fdip->tick, false); 995 996 if ((err = fticket_wait_answer(fdip->tick))) { 997 fuse_lck_mtx_lock(fdip->tick->tk_aw_mtx); 998 999 if (fticket_answered(fdip->tick)) { 1000 /* 1001 * Just between noticing the interrupt and getting here, 1002 * the standard handler has completed his job. 1003 * So we drop the ticket and exit as usual. 1004 */ 1005 SDT_PROBE2(fusefs, , ipc, fdisp_wait_answ_error, 1006 "IPC: interrupted, already answered", err); 1007 fuse_lck_mtx_unlock(fdip->tick->tk_aw_mtx); 1008 goto out; 1009 } else { 1010 /* 1011 * So we were faster than the standard handler. 1012 * Then by setting the answered flag we get *him* 1013 * to drop the ticket. 1014 */ 1015 SDT_PROBE2(fusefs, , ipc, fdisp_wait_answ_error, 1016 "IPC: interrupted, setting to answered", err); 1017 fticket_set_answered(fdip->tick); 1018 fuse_lck_mtx_unlock(fdip->tick->tk_aw_mtx); 1019 return err; 1020 } 1021 } 1022 1023 if (fdip->tick->tk_aw_errno == ENOTCONN) { 1024 /* The daemon died while we were waiting for a response */ 1025 err = ENOTCONN; 1026 goto out; 1027 } else if (fdip->tick->tk_aw_errno) { 1028 /* 1029 * There was some sort of communication error with the daemon 1030 * that the client wouldn't understand. 1031 */ 1032 SDT_PROBE2(fusefs, , ipc, fdisp_wait_answ_error, 1033 "IPC: explicit EIO-ing", fdip->tick->tk_aw_errno); 1034 err = EIO; 1035 goto out; 1036 } 1037 if ((err = fdip->tick->tk_aw_ohead.error)) { 1038 SDT_PROBE2(fusefs, , ipc, fdisp_wait_answ_error, 1039 "IPC: setting status", fdip->tick->tk_aw_ohead.error); 1040 /* 1041 * This means a "proper" fuse syscall error. 1042 * We record this value so the caller will 1043 * be able to know it's not a boring messaging 1044 * failure, if she wishes so (and if not, she can 1045 * just simply propagate the return value of this routine). 1046 * [XXX Maybe a bitflag would do the job too, 1047 * if other flags needed, this will be converted thusly.] 1048 */ 1049 fdip->answ_stat = err; 1050 goto out; 1051 } 1052 fdip->answ = fticket_resp(fdip->tick)->base; 1053 fdip->iosize = fticket_resp(fdip->tick)->len; 1054 1055 return 0; 1056 1057 out: 1058 return err; 1059 } 1060 1061 void 1062 fuse_ipc_init(void) 1063 { 1064 ticket_zone = uma_zcreate("fuse_ticket", sizeof(struct fuse_ticket), 1065 fticket_ctor, fticket_dtor, fticket_init, fticket_fini, 1066 UMA_ALIGN_PTR, 0); 1067 fuse_ticket_count = counter_u64_alloc(M_WAITOK); 1068 } 1069 1070 void 1071 fuse_ipc_destroy(void) 1072 { 1073 counter_u64_free(fuse_ticket_count); 1074 uma_zdestroy(ticket_zone); 1075 } 1076