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