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