1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2007-2009 Google Inc. 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/types.h> 67 #include <sys/param.h> 68 #include <sys/module.h> 69 #include <sys/systm.h> 70 #include <sys/errno.h> 71 #include <sys/param.h> 72 #include <sys/kernel.h> 73 #include <sys/conf.h> 74 #include <sys/uio.h> 75 #include <sys/malloc.h> 76 #include <sys/queue.h> 77 #include <sys/lock.h> 78 #include <sys/sx.h> 79 #include <sys/mutex.h> 80 #include <sys/proc.h> 81 #include <sys/mount.h> 82 #include <sys/sdt.h> 83 #include <sys/stat.h> 84 #include <sys/fcntl.h> 85 #include <sys/sysctl.h> 86 #include <sys/poll.h> 87 #include <sys/selinfo.h> 88 89 #include "fuse.h" 90 #include "fuse_internal.h" 91 #include "fuse_ipc.h" 92 93 #include <compat/linux/linux_errno.h> 94 #include <compat/linux/linux_errno.inc> 95 96 SDT_PROVIDER_DECLARE(fusefs); 97 /* 98 * Fuse trace probe: 99 * arg0: verbosity. Higher numbers give more verbose messages 100 * arg1: Textual message 101 */ 102 SDT_PROBE_DEFINE2(fusefs, , device, trace, "int", "char*"); 103 104 static struct cdev *fuse_dev; 105 106 static d_kqfilter_t fuse_device_filter; 107 static d_open_t fuse_device_open; 108 static d_poll_t fuse_device_poll; 109 static d_read_t fuse_device_read; 110 static d_write_t fuse_device_write; 111 112 static struct cdevsw fuse_device_cdevsw = { 113 .d_kqfilter = fuse_device_filter, 114 .d_open = fuse_device_open, 115 .d_name = "fuse", 116 .d_poll = fuse_device_poll, 117 .d_read = fuse_device_read, 118 .d_write = fuse_device_write, 119 .d_version = D_VERSION, 120 }; 121 122 static int fuse_device_filt_read(struct knote *kn, long hint); 123 static int fuse_device_filt_write(struct knote *kn, long hint); 124 static void fuse_device_filt_detach(struct knote *kn); 125 126 struct filterops fuse_device_rfiltops = { 127 .f_isfd = 1, 128 .f_detach = fuse_device_filt_detach, 129 .f_event = fuse_device_filt_read, 130 }; 131 132 struct filterops fuse_device_wfiltops = { 133 .f_isfd = 1, 134 .f_event = fuse_device_filt_write, 135 }; 136 137 /**************************** 138 * 139 * >>> Fuse device op defs 140 * 141 ****************************/ 142 143 static void 144 fdata_dtor(void *arg) 145 { 146 struct fuse_data *fdata; 147 struct fuse_ticket *tick; 148 149 fdata = arg; 150 if (fdata == NULL) 151 return; 152 153 fdata_set_dead(fdata); 154 155 FUSE_LOCK(); 156 fuse_lck_mtx_lock(fdata->aw_mtx); 157 /* wakup poll()ers */ 158 selwakeuppri(&fdata->ks_rsel, PZERO + 1); 159 /* Don't let syscall handlers wait in vain */ 160 while ((tick = fuse_aw_pop(fdata))) { 161 fuse_lck_mtx_lock(tick->tk_aw_mtx); 162 fticket_set_answered(tick); 163 tick->tk_aw_errno = ENOTCONN; 164 wakeup(tick); 165 fuse_lck_mtx_unlock(tick->tk_aw_mtx); 166 FUSE_ASSERT_AW_DONE(tick); 167 fuse_ticket_drop(tick); 168 } 169 fuse_lck_mtx_unlock(fdata->aw_mtx); 170 171 /* Cleanup unsent operations */ 172 fuse_lck_mtx_lock(fdata->ms_mtx); 173 while ((tick = fuse_ms_pop(fdata))) { 174 fuse_ticket_drop(tick); 175 } 176 fuse_lck_mtx_unlock(fdata->ms_mtx); 177 FUSE_UNLOCK(); 178 179 fdata_trydestroy(fdata); 180 } 181 182 static int 183 fuse_device_filter(struct cdev *dev, struct knote *kn) 184 { 185 struct fuse_data *data; 186 int error; 187 188 error = devfs_get_cdevpriv((void **)&data); 189 190 if (error == 0 && kn->kn_filter == EVFILT_READ) { 191 kn->kn_fop = &fuse_device_rfiltops; 192 kn->kn_hook = data; 193 knlist_add(&data->ks_rsel.si_note, kn, 0); 194 error = 0; 195 } else if (error == 0 && kn->kn_filter == EVFILT_WRITE) { 196 kn->kn_fop = &fuse_device_wfiltops; 197 error = 0; 198 } else if (error == 0) { 199 error = EINVAL; 200 kn->kn_data = error; 201 } 202 203 return (error); 204 } 205 206 static void 207 fuse_device_filt_detach(struct knote *kn) 208 { 209 struct fuse_data *data; 210 211 data = (struct fuse_data*)kn->kn_hook; 212 MPASS(data != NULL); 213 knlist_remove(&data->ks_rsel.si_note, kn, 0); 214 kn->kn_hook = NULL; 215 } 216 217 static int 218 fuse_device_filt_read(struct knote *kn, long hint) 219 { 220 struct fuse_data *data; 221 int ready; 222 223 data = (struct fuse_data*)kn->kn_hook; 224 MPASS(data != NULL); 225 226 mtx_assert(&data->ms_mtx, MA_OWNED); 227 if (fdata_get_dead(data)) { 228 kn->kn_flags |= EV_EOF; 229 kn->kn_fflags = ENODEV; 230 kn->kn_data = 1; 231 ready = 1; 232 } else if (STAILQ_FIRST(&data->ms_head)) { 233 MPASS(data->ms_count >= 1); 234 kn->kn_data = data->ms_count; 235 ready = 1; 236 } else { 237 ready = 0; 238 } 239 240 return (ready); 241 } 242 243 static int 244 fuse_device_filt_write(struct knote *kn, long hint) 245 { 246 247 kn->kn_data = 0; 248 249 /* The device is always ready to write, so we return 1*/ 250 return (1); 251 } 252 253 /* 254 * Resources are set up on a per-open basis 255 */ 256 static int 257 fuse_device_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 258 { 259 struct fuse_data *fdata; 260 int error; 261 262 SDT_PROBE2(fusefs, , device, trace, 1, "device open"); 263 264 fdata = fdata_alloc(dev, td->td_ucred); 265 error = devfs_set_cdevpriv(fdata, fdata_dtor); 266 if (error != 0) 267 fdata_trydestroy(fdata); 268 else 269 SDT_PROBE2(fusefs, , device, trace, 1, "device open success"); 270 return (error); 271 } 272 273 int 274 fuse_device_poll(struct cdev *dev, int events, struct thread *td) 275 { 276 struct fuse_data *data; 277 int error, revents = 0; 278 279 error = devfs_get_cdevpriv((void **)&data); 280 if (error != 0) 281 return (events & 282 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM)); 283 284 if (events & (POLLIN | POLLRDNORM)) { 285 fuse_lck_mtx_lock(data->ms_mtx); 286 if (fdata_get_dead(data) || STAILQ_FIRST(&data->ms_head)) 287 revents |= events & (POLLIN | POLLRDNORM); 288 else 289 selrecord(td, &data->ks_rsel); 290 fuse_lck_mtx_unlock(data->ms_mtx); 291 } 292 if (events & (POLLOUT | POLLWRNORM)) { 293 revents |= events & (POLLOUT | POLLWRNORM); 294 } 295 return (revents); 296 } 297 298 /* 299 * fuse_device_read hangs on the queue of VFS messages. 300 * When it's notified that there is a new one, it picks that and 301 * passes up to the daemon 302 */ 303 int 304 fuse_device_read(struct cdev *dev, struct uio *uio, int ioflag) 305 { 306 int err; 307 struct fuse_data *data; 308 struct fuse_ticket *tick; 309 void *buf; 310 int buflen; 311 312 SDT_PROBE2(fusefs, , device, trace, 1, "fuse device read"); 313 314 err = devfs_get_cdevpriv((void **)&data); 315 if (err != 0) 316 return (err); 317 318 fuse_lck_mtx_lock(data->ms_mtx); 319 again: 320 if (fdata_get_dead(data)) { 321 SDT_PROBE2(fusefs, , device, trace, 2, 322 "we know early on that reader should be kicked so we " 323 "don't wait for news"); 324 fuse_lck_mtx_unlock(data->ms_mtx); 325 return (ENODEV); 326 } 327 if (!(tick = fuse_ms_pop(data))) { 328 /* check if we may block */ 329 if (ioflag & O_NONBLOCK) { 330 /* get outa here soon */ 331 fuse_lck_mtx_unlock(data->ms_mtx); 332 return (EAGAIN); 333 } else { 334 err = msleep(data, &data->ms_mtx, PCATCH, "fu_msg", 0); 335 if (err != 0) { 336 fuse_lck_mtx_unlock(data->ms_mtx); 337 return (fdata_get_dead(data) ? ENODEV : err); 338 } 339 tick = fuse_ms_pop(data); 340 } 341 } 342 if (!tick) { 343 /* 344 * We can get here if fuse daemon suddenly terminates, 345 * eg, by being hit by a SIGKILL 346 * -- and some other cases, too, tho not totally clear, when 347 * (cv_signal/wakeup_one signals the whole process ?) 348 */ 349 SDT_PROBE2(fusefs, , device, trace, 1, "no message on thread"); 350 goto again; 351 } 352 fuse_lck_mtx_unlock(data->ms_mtx); 353 354 if (fdata_get_dead(data)) { 355 /* 356 * somebody somewhere -- eg., umount routine -- 357 * wants this liaison finished off 358 */ 359 SDT_PROBE2(fusefs, , device, trace, 2, 360 "reader is to be sacked"); 361 if (tick) { 362 SDT_PROBE2(fusefs, , device, trace, 2, "weird -- " 363 "\"kick\" is set tho there is message"); 364 FUSE_ASSERT_MS_DONE(tick); 365 fuse_ticket_drop(tick); 366 } 367 return (ENODEV); /* This should make the daemon get off 368 * of us */ 369 } 370 SDT_PROBE2(fusefs, , device, trace, 1, 371 "fuse device read message successfully"); 372 373 buf = tick->tk_ms_fiov.base; 374 buflen = tick->tk_ms_fiov.len; 375 376 /* 377 * Why not ban mercilessly stupid daemons who can't keep up 378 * with us? (There is no much use of a partial read here...) 379 */ 380 /* 381 * XXX note that in such cases Linux FUSE throws EIO at the 382 * syscall invoker and stands back to the message queue. The 383 * rationale should be made clear (and possibly adopt that 384 * behaviour). Keeping the current scheme at least makes 385 * fallacy as loud as possible... 386 */ 387 if (uio->uio_resid < buflen) { 388 fdata_set_dead(data); 389 SDT_PROBE2(fusefs, , device, trace, 2, 390 "daemon is stupid, kick it off..."); 391 err = ENODEV; 392 } else { 393 err = uiomove(buf, buflen, uio); 394 } 395 396 FUSE_ASSERT_MS_DONE(tick); 397 fuse_ticket_drop(tick); 398 399 return (err); 400 } 401 402 static inline int 403 fuse_ohead_audit(struct fuse_out_header *ohead, struct uio *uio) 404 { 405 if (uio->uio_resid + sizeof(struct fuse_out_header) != ohead->len) { 406 SDT_PROBE2(fusefs, , device, trace, 1, 407 "Format error: body size " 408 "differs from size claimed by header"); 409 return (EINVAL); 410 } 411 if (uio->uio_resid && ohead->unique != 0 && ohead->error) { 412 SDT_PROBE2(fusefs, , device, trace, 1, 413 "Format error: non zero error but message had a body"); 414 return (EINVAL); 415 } 416 417 return (0); 418 } 419 420 SDT_PROBE_DEFINE1(fusefs, , device, fuse_device_write_notify, 421 "struct fuse_out_header*"); 422 SDT_PROBE_DEFINE1(fusefs, , device, fuse_device_write_missing_ticket, 423 "uint64_t"); 424 SDT_PROBE_DEFINE1(fusefs, , device, fuse_device_write_found, 425 "struct fuse_ticket*"); 426 /* 427 * fuse_device_write first reads the header sent by the daemon. 428 * If that's OK, looks up ticket/callback node by the unique id seen in header. 429 * If the callback node contains a handler function, the uio is passed over 430 * that. 431 */ 432 static int 433 fuse_device_write(struct cdev *dev, struct uio *uio, int ioflag) 434 { 435 struct fuse_out_header ohead; 436 int err = 0; 437 struct fuse_data *data; 438 struct mount *mp; 439 struct fuse_ticket *tick, *itick, *x_tick; 440 int found = 0; 441 442 err = devfs_get_cdevpriv((void **)&data); 443 if (err != 0) 444 return (err); 445 mp = data->mp; 446 447 if (uio->uio_resid < sizeof(struct fuse_out_header)) { 448 SDT_PROBE2(fusefs, , device, trace, 1, 449 "fuse_device_write got less than a header!"); 450 fdata_set_dead(data); 451 return (EINVAL); 452 } 453 if ((err = uiomove(&ohead, sizeof(struct fuse_out_header), uio)) != 0) 454 return (err); 455 456 if (data->linux_errnos != 0 && ohead.error != 0) { 457 err = -ohead.error; 458 if (err < 0 || err >= nitems(linux_to_bsd_errtbl)) 459 return (EINVAL); 460 461 /* '-', because it will get flipped again below */ 462 ohead.error = -linux_to_bsd_errtbl[err]; 463 } 464 465 /* 466 * We check header information (which is redundant) and compare it 467 * with what we see. If we see some inconsistency we discard the 468 * whole answer and proceed on as if it had never existed. In 469 * particular, no pretender will be woken up, regardless the 470 * "unique" value in the header. 471 */ 472 if ((err = fuse_ohead_audit(&ohead, uio))) { 473 fdata_set_dead(data); 474 return (err); 475 } 476 /* Pass stuff over to callback if there is one installed */ 477 478 /* Looking for ticket with the unique id of header */ 479 fuse_lck_mtx_lock(data->aw_mtx); 480 TAILQ_FOREACH_SAFE(tick, &data->aw_head, tk_aw_link, 481 x_tick) { 482 if (tick->tk_unique == ohead.unique) { 483 SDT_PROBE1(fusefs, , device, fuse_device_write_found, 484 tick); 485 found = 1; 486 fuse_aw_remove(tick); 487 break; 488 } 489 } 490 if (found && tick->irq_unique > 0) { 491 /* 492 * Discard the FUSE_INTERRUPT ticket that tried to interrupt 493 * this operation 494 */ 495 TAILQ_FOREACH_SAFE(itick, &data->aw_head, tk_aw_link, 496 x_tick) { 497 if (itick->tk_unique == tick->irq_unique) { 498 fuse_aw_remove(itick); 499 fuse_ticket_drop(itick); 500 break; 501 } 502 } 503 tick->irq_unique = 0; 504 } 505 fuse_lck_mtx_unlock(data->aw_mtx); 506 507 if (found) { 508 if (tick->tk_aw_handler) { 509 /* 510 * We found a callback with proper handler. In this 511 * case the out header will be 0wnd by the callback, 512 * so the fun of freeing that is left for her. 513 * (Then, by all chance, she'll just get that's done 514 * via ticket_drop(), so no manual mucking 515 * around...) 516 */ 517 SDT_PROBE2(fusefs, , device, trace, 1, 518 "pass ticket to a callback"); 519 /* Sanitize the linuxism of negative errnos */ 520 ohead.error *= -1; 521 if (ohead.error < 0 || ohead.error > ELAST) { 522 /* Illegal error code */ 523 ohead.error = EIO; 524 memcpy(&tick->tk_aw_ohead, &ohead, 525 sizeof(ohead)); 526 tick->tk_aw_handler(tick, uio); 527 err = EINVAL; 528 } else { 529 memcpy(&tick->tk_aw_ohead, &ohead, 530 sizeof(ohead)); 531 err = tick->tk_aw_handler(tick, uio); 532 } 533 } else { 534 /* pretender doesn't wanna do anything with answer */ 535 SDT_PROBE2(fusefs, , device, trace, 1, 536 "stuff devalidated, so we drop it"); 537 } 538 539 /* 540 * As aw_mtx was not held during the callback execution the 541 * ticket may have been inserted again. However, this is safe 542 * because fuse_ticket_drop() will deal with refcount anyway. 543 */ 544 fuse_ticket_drop(tick); 545 } else if (ohead.unique == 0){ 546 /* unique == 0 means asynchronous notification */ 547 SDT_PROBE1(fusefs, , device, fuse_device_write_notify, &ohead); 548 switch (ohead.error) { 549 case FUSE_NOTIFY_INVAL_ENTRY: 550 err = fuse_internal_invalidate_entry(mp, uio); 551 break; 552 case FUSE_NOTIFY_INVAL_INODE: 553 err = fuse_internal_invalidate_inode(mp, uio); 554 break; 555 case FUSE_NOTIFY_RETRIEVE: 556 case FUSE_NOTIFY_STORE: 557 /* 558 * Unimplemented. I don't know of any file systems 559 * that use them, and the protocol isn't sound anyway, 560 * since the notification messages don't include the 561 * inode's generation number. Without that, it's 562 * possible to manipulate the cache of the wrong vnode. 563 * Finally, it's not defined what this message should 564 * do for a file with dirty cache. 565 */ 566 case FUSE_NOTIFY_POLL: 567 /* Unimplemented. See comments in fuse_vnops */ 568 default: 569 /* Not implemented */ 570 err = ENOSYS; 571 } 572 } else { 573 /* no callback at all! */ 574 SDT_PROBE1(fusefs, , device, fuse_device_write_missing_ticket, 575 ohead.unique); 576 if (ohead.error == -EAGAIN) { 577 /* 578 * This was probably a response to a FUSE_INTERRUPT 579 * operation whose original operation is already 580 * complete. We can't store FUSE_INTERRUPT tickets 581 * indefinitely because their responses are optional. 582 * So we delete them when the original operation 583 * completes. And sadly the fuse_header_out doesn't 584 * identify the opcode, so we have to guess. 585 */ 586 err = 0; 587 } else { 588 err = EINVAL; 589 } 590 } 591 592 return (err); 593 } 594 595 int 596 fuse_device_init(void) 597 { 598 599 fuse_dev = make_dev(&fuse_device_cdevsw, 0, UID_ROOT, GID_OPERATOR, 600 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, "fuse"); 601 if (fuse_dev == NULL) 602 return (ENOMEM); 603 return (0); 604 } 605 606 void 607 fuse_device_destroy(void) 608 { 609 610 MPASS(fuse_dev != NULL); 611 destroy_dev(fuse_dev); 612 } 613