1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * FUSE: Filesystem in Userspace 4 * Copyright (c) 2023-2024 DataDirect Networks. 5 */ 6 7 #include "fuse_i.h" 8 #include "dev_uring_i.h" 9 #include "fuse_dev_i.h" 10 11 #include <linux/fs.h> 12 #include <linux/io_uring/cmd.h> 13 14 static bool __read_mostly enable_uring; 15 module_param(enable_uring, bool, 0644); 16 MODULE_PARM_DESC(enable_uring, 17 "Enable userspace communication through io-uring"); 18 19 #define FUSE_URING_IOV_SEGS 2 /* header and payload */ 20 21 22 bool fuse_uring_enabled(void) 23 { 24 return enable_uring; 25 } 26 27 static void fuse_uring_req_end(struct fuse_ring_ent *ent, struct fuse_req *req, 28 int error) 29 { 30 ent->fuse_req = NULL; 31 if (error) 32 req->out.h.error = error; 33 34 clear_bit(FR_SENT, &req->flags); 35 fuse_request_end(req); 36 } 37 38 /* Abort all list queued request on the given ring queue */ 39 static void fuse_uring_abort_end_queue_requests(struct fuse_ring_queue *queue) 40 { 41 struct fuse_req *req; 42 LIST_HEAD(req_list); 43 44 spin_lock(&queue->lock); 45 list_for_each_entry(req, &queue->fuse_req_queue, list) 46 clear_bit(FR_PENDING, &req->flags); 47 list_splice_init(&queue->fuse_req_queue, &req_list); 48 spin_unlock(&queue->lock); 49 50 /* must not hold queue lock to avoid order issues with fi->lock */ 51 fuse_dev_end_requests(&req_list); 52 } 53 54 void fuse_uring_abort_end_requests(struct fuse_ring *ring) 55 { 56 int qid; 57 struct fuse_ring_queue *queue; 58 59 for (qid = 0; qid < ring->nr_queues; qid++) { 60 queue = READ_ONCE(ring->queues[qid]); 61 if (!queue) 62 continue; 63 64 queue->stopped = true; 65 fuse_uring_abort_end_queue_requests(queue); 66 } 67 } 68 69 void fuse_uring_destruct(struct fuse_conn *fc) 70 { 71 struct fuse_ring *ring = fc->ring; 72 int qid; 73 74 if (!ring) 75 return; 76 77 for (qid = 0; qid < ring->nr_queues; qid++) { 78 struct fuse_ring_queue *queue = ring->queues[qid]; 79 80 if (!queue) 81 continue; 82 83 WARN_ON(!list_empty(&queue->ent_avail_queue)); 84 WARN_ON(!list_empty(&queue->ent_w_req_queue)); 85 WARN_ON(!list_empty(&queue->ent_commit_queue)); 86 WARN_ON(!list_empty(&queue->ent_in_userspace)); 87 88 kfree(queue->fpq.processing); 89 kfree(queue); 90 ring->queues[qid] = NULL; 91 } 92 93 kfree(ring->queues); 94 kfree(ring); 95 fc->ring = NULL; 96 } 97 98 /* 99 * Basic ring setup for this connection based on the provided configuration 100 */ 101 static struct fuse_ring *fuse_uring_create(struct fuse_conn *fc) 102 { 103 struct fuse_ring *ring; 104 size_t nr_queues = num_possible_cpus(); 105 struct fuse_ring *res = NULL; 106 size_t max_payload_size; 107 108 ring = kzalloc(sizeof(*fc->ring), GFP_KERNEL_ACCOUNT); 109 if (!ring) 110 return NULL; 111 112 ring->queues = kcalloc(nr_queues, sizeof(struct fuse_ring_queue *), 113 GFP_KERNEL_ACCOUNT); 114 if (!ring->queues) 115 goto out_err; 116 117 max_payload_size = max(FUSE_MIN_READ_BUFFER, fc->max_write); 118 max_payload_size = max(max_payload_size, fc->max_pages * PAGE_SIZE); 119 120 spin_lock(&fc->lock); 121 if (fc->ring) { 122 /* race, another thread created the ring in the meantime */ 123 spin_unlock(&fc->lock); 124 res = fc->ring; 125 goto out_err; 126 } 127 128 init_waitqueue_head(&ring->stop_waitq); 129 130 fc->ring = ring; 131 ring->nr_queues = nr_queues; 132 ring->fc = fc; 133 ring->max_payload_sz = max_payload_size; 134 atomic_set(&ring->queue_refs, 0); 135 136 spin_unlock(&fc->lock); 137 return ring; 138 139 out_err: 140 kfree(ring->queues); 141 kfree(ring); 142 return res; 143 } 144 145 static struct fuse_ring_queue *fuse_uring_create_queue(struct fuse_ring *ring, 146 int qid) 147 { 148 struct fuse_conn *fc = ring->fc; 149 struct fuse_ring_queue *queue; 150 struct list_head *pq; 151 152 queue = kzalloc(sizeof(*queue), GFP_KERNEL_ACCOUNT); 153 if (!queue) 154 return NULL; 155 pq = kcalloc(FUSE_PQ_HASH_SIZE, sizeof(struct list_head), GFP_KERNEL); 156 if (!pq) { 157 kfree(queue); 158 return NULL; 159 } 160 161 queue->qid = qid; 162 queue->ring = ring; 163 spin_lock_init(&queue->lock); 164 165 INIT_LIST_HEAD(&queue->ent_avail_queue); 166 INIT_LIST_HEAD(&queue->ent_commit_queue); 167 INIT_LIST_HEAD(&queue->ent_w_req_queue); 168 INIT_LIST_HEAD(&queue->ent_in_userspace); 169 INIT_LIST_HEAD(&queue->fuse_req_queue); 170 171 queue->fpq.processing = pq; 172 fuse_pqueue_init(&queue->fpq); 173 174 spin_lock(&fc->lock); 175 if (ring->queues[qid]) { 176 spin_unlock(&fc->lock); 177 kfree(queue->fpq.processing); 178 kfree(queue); 179 return ring->queues[qid]; 180 } 181 182 /* 183 * write_once and lock as the caller mostly doesn't take the lock at all 184 */ 185 WRITE_ONCE(ring->queues[qid], queue); 186 spin_unlock(&fc->lock); 187 188 return queue; 189 } 190 191 static void fuse_uring_stop_fuse_req_end(struct fuse_req *req) 192 { 193 clear_bit(FR_SENT, &req->flags); 194 req->out.h.error = -ECONNABORTED; 195 fuse_request_end(req); 196 } 197 198 /* 199 * Release a request/entry on connection tear down 200 */ 201 static void fuse_uring_entry_teardown(struct fuse_ring_ent *ent) 202 { 203 struct fuse_req *req; 204 struct io_uring_cmd *cmd; 205 206 struct fuse_ring_queue *queue = ent->queue; 207 208 spin_lock(&queue->lock); 209 cmd = ent->cmd; 210 ent->cmd = NULL; 211 req = ent->fuse_req; 212 ent->fuse_req = NULL; 213 if (req) { 214 /* remove entry from queue->fpq->processing */ 215 list_del_init(&req->list); 216 } 217 spin_unlock(&queue->lock); 218 219 if (cmd) 220 io_uring_cmd_done(cmd, -ENOTCONN, 0, IO_URING_F_UNLOCKED); 221 222 if (req) 223 fuse_uring_stop_fuse_req_end(req); 224 225 list_del_init(&ent->list); 226 kfree(ent); 227 } 228 229 static void fuse_uring_stop_list_entries(struct list_head *head, 230 struct fuse_ring_queue *queue, 231 enum fuse_ring_req_state exp_state) 232 { 233 struct fuse_ring *ring = queue->ring; 234 struct fuse_ring_ent *ent, *next; 235 ssize_t queue_refs = SSIZE_MAX; 236 LIST_HEAD(to_teardown); 237 238 spin_lock(&queue->lock); 239 list_for_each_entry_safe(ent, next, head, list) { 240 if (ent->state != exp_state) { 241 pr_warn("entry teardown qid=%d state=%d expected=%d", 242 queue->qid, ent->state, exp_state); 243 continue; 244 } 245 246 list_move(&ent->list, &to_teardown); 247 } 248 spin_unlock(&queue->lock); 249 250 /* no queue lock to avoid lock order issues */ 251 list_for_each_entry_safe(ent, next, &to_teardown, list) { 252 fuse_uring_entry_teardown(ent); 253 queue_refs = atomic_dec_return(&ring->queue_refs); 254 WARN_ON_ONCE(queue_refs < 0); 255 } 256 } 257 258 static void fuse_uring_teardown_entries(struct fuse_ring_queue *queue) 259 { 260 fuse_uring_stop_list_entries(&queue->ent_in_userspace, queue, 261 FRRS_USERSPACE); 262 fuse_uring_stop_list_entries(&queue->ent_avail_queue, queue, 263 FRRS_AVAILABLE); 264 } 265 266 /* 267 * Log state debug info 268 */ 269 static void fuse_uring_log_ent_state(struct fuse_ring *ring) 270 { 271 int qid; 272 struct fuse_ring_ent *ent; 273 274 for (qid = 0; qid < ring->nr_queues; qid++) { 275 struct fuse_ring_queue *queue = ring->queues[qid]; 276 277 if (!queue) 278 continue; 279 280 spin_lock(&queue->lock); 281 /* 282 * Log entries from the intermediate queue, the other queues 283 * should be empty 284 */ 285 list_for_each_entry(ent, &queue->ent_w_req_queue, list) { 286 pr_info(" ent-req-queue ring=%p qid=%d ent=%p state=%d\n", 287 ring, qid, ent, ent->state); 288 } 289 list_for_each_entry(ent, &queue->ent_commit_queue, list) { 290 pr_info(" ent-commit-queue ring=%p qid=%d ent=%p state=%d\n", 291 ring, qid, ent, ent->state); 292 } 293 spin_unlock(&queue->lock); 294 } 295 ring->stop_debug_log = 1; 296 } 297 298 static void fuse_uring_async_stop_queues(struct work_struct *work) 299 { 300 int qid; 301 struct fuse_ring *ring = 302 container_of(work, struct fuse_ring, async_teardown_work.work); 303 304 /* XXX code dup */ 305 for (qid = 0; qid < ring->nr_queues; qid++) { 306 struct fuse_ring_queue *queue = READ_ONCE(ring->queues[qid]); 307 308 if (!queue) 309 continue; 310 311 fuse_uring_teardown_entries(queue); 312 } 313 314 /* 315 * Some ring entries might be in the middle of IO operations, 316 * i.e. in process to get handled by file_operations::uring_cmd 317 * or on the way to userspace - we could handle that with conditions in 318 * run time code, but easier/cleaner to have an async tear down handler 319 * If there are still queue references left 320 */ 321 if (atomic_read(&ring->queue_refs) > 0) { 322 if (time_after(jiffies, 323 ring->teardown_time + FUSE_URING_TEARDOWN_TIMEOUT)) 324 fuse_uring_log_ent_state(ring); 325 326 schedule_delayed_work(&ring->async_teardown_work, 327 FUSE_URING_TEARDOWN_INTERVAL); 328 } else { 329 wake_up_all(&ring->stop_waitq); 330 } 331 } 332 333 /* 334 * Stop the ring queues 335 */ 336 void fuse_uring_stop_queues(struct fuse_ring *ring) 337 { 338 int qid; 339 340 for (qid = 0; qid < ring->nr_queues; qid++) { 341 struct fuse_ring_queue *queue = READ_ONCE(ring->queues[qid]); 342 343 if (!queue) 344 continue; 345 346 fuse_uring_teardown_entries(queue); 347 } 348 349 if (atomic_read(&ring->queue_refs) > 0) { 350 ring->teardown_time = jiffies; 351 INIT_DELAYED_WORK(&ring->async_teardown_work, 352 fuse_uring_async_stop_queues); 353 schedule_delayed_work(&ring->async_teardown_work, 354 FUSE_URING_TEARDOWN_INTERVAL); 355 } else { 356 wake_up_all(&ring->stop_waitq); 357 } 358 } 359 360 /* 361 * Checks for errors and stores it into the request 362 */ 363 static int fuse_uring_out_header_has_err(struct fuse_out_header *oh, 364 struct fuse_req *req, 365 struct fuse_conn *fc) 366 { 367 int err; 368 369 err = -EINVAL; 370 if (oh->unique == 0) { 371 /* Not supported through io-uring yet */ 372 pr_warn_once("notify through fuse-io-uring not supported\n"); 373 goto err; 374 } 375 376 if (oh->error <= -ERESTARTSYS || oh->error > 0) 377 goto err; 378 379 if (oh->error) { 380 err = oh->error; 381 goto err; 382 } 383 384 err = -ENOENT; 385 if ((oh->unique & ~FUSE_INT_REQ_BIT) != req->in.h.unique) { 386 pr_warn_ratelimited("unique mismatch, expected: %llu got %llu\n", 387 req->in.h.unique, 388 oh->unique & ~FUSE_INT_REQ_BIT); 389 goto err; 390 } 391 392 /* 393 * Is it an interrupt reply ID? 394 * XXX: Not supported through fuse-io-uring yet, it should not even 395 * find the request - should not happen. 396 */ 397 WARN_ON_ONCE(oh->unique & FUSE_INT_REQ_BIT); 398 399 err = 0; 400 err: 401 return err; 402 } 403 404 static int fuse_uring_copy_from_ring(struct fuse_ring *ring, 405 struct fuse_req *req, 406 struct fuse_ring_ent *ent) 407 { 408 struct fuse_copy_state cs; 409 struct fuse_args *args = req->args; 410 struct iov_iter iter; 411 int err; 412 struct fuse_uring_ent_in_out ring_in_out; 413 414 err = copy_from_user(&ring_in_out, &ent->headers->ring_ent_in_out, 415 sizeof(ring_in_out)); 416 if (err) 417 return -EFAULT; 418 419 err = import_ubuf(ITER_SOURCE, ent->payload, ring->max_payload_sz, 420 &iter); 421 if (err) 422 return err; 423 424 fuse_copy_init(&cs, 0, &iter); 425 cs.is_uring = 1; 426 cs.req = req; 427 428 return fuse_copy_out_args(&cs, args, ring_in_out.payload_sz); 429 } 430 431 /* 432 * Copy data from the req to the ring buffer 433 */ 434 static int fuse_uring_args_to_ring(struct fuse_ring *ring, struct fuse_req *req, 435 struct fuse_ring_ent *ent) 436 { 437 struct fuse_copy_state cs; 438 struct fuse_args *args = req->args; 439 struct fuse_in_arg *in_args = args->in_args; 440 int num_args = args->in_numargs; 441 int err; 442 struct iov_iter iter; 443 struct fuse_uring_ent_in_out ent_in_out = { 444 .flags = 0, 445 .commit_id = req->in.h.unique, 446 }; 447 448 err = import_ubuf(ITER_DEST, ent->payload, ring->max_payload_sz, &iter); 449 if (err) { 450 pr_info_ratelimited("fuse: Import of user buffer failed\n"); 451 return err; 452 } 453 454 fuse_copy_init(&cs, 1, &iter); 455 cs.is_uring = 1; 456 cs.req = req; 457 458 if (num_args > 0) { 459 /* 460 * Expectation is that the first argument is the per op header. 461 * Some op code have that as zero size. 462 */ 463 if (args->in_args[0].size > 0) { 464 err = copy_to_user(&ent->headers->op_in, in_args->value, 465 in_args->size); 466 if (err) { 467 pr_info_ratelimited( 468 "Copying the header failed.\n"); 469 return -EFAULT; 470 } 471 } 472 in_args++; 473 num_args--; 474 } 475 476 /* copy the payload */ 477 err = fuse_copy_args(&cs, num_args, args->in_pages, 478 (struct fuse_arg *)in_args, 0); 479 if (err) { 480 pr_info_ratelimited("%s fuse_copy_args failed\n", __func__); 481 return err; 482 } 483 484 ent_in_out.payload_sz = cs.ring.copied_sz; 485 err = copy_to_user(&ent->headers->ring_ent_in_out, &ent_in_out, 486 sizeof(ent_in_out)); 487 return err ? -EFAULT : 0; 488 } 489 490 static int fuse_uring_copy_to_ring(struct fuse_ring_ent *ent, 491 struct fuse_req *req) 492 { 493 struct fuse_ring_queue *queue = ent->queue; 494 struct fuse_ring *ring = queue->ring; 495 int err; 496 497 err = -EIO; 498 if (WARN_ON(ent->state != FRRS_FUSE_REQ)) { 499 pr_err("qid=%d ring-req=%p invalid state %d on send\n", 500 queue->qid, ent, ent->state); 501 return err; 502 } 503 504 err = -EINVAL; 505 if (WARN_ON(req->in.h.unique == 0)) 506 return err; 507 508 /* copy the request */ 509 err = fuse_uring_args_to_ring(ring, req, ent); 510 if (unlikely(err)) { 511 pr_info_ratelimited("Copy to ring failed: %d\n", err); 512 return err; 513 } 514 515 /* copy fuse_in_header */ 516 err = copy_to_user(&ent->headers->in_out, &req->in.h, 517 sizeof(req->in.h)); 518 if (err) { 519 err = -EFAULT; 520 return err; 521 } 522 523 return 0; 524 } 525 526 static int fuse_uring_prepare_send(struct fuse_ring_ent *ent, 527 struct fuse_req *req) 528 { 529 int err; 530 531 err = fuse_uring_copy_to_ring(ent, req); 532 if (!err) 533 set_bit(FR_SENT, &req->flags); 534 else 535 fuse_uring_req_end(ent, req, err); 536 537 return err; 538 } 539 540 /* 541 * Write data to the ring buffer and send the request to userspace, 542 * userspace will read it 543 * This is comparable with classical read(/dev/fuse) 544 */ 545 static int fuse_uring_send_next_to_ring(struct fuse_ring_ent *ent, 546 struct fuse_req *req, 547 unsigned int issue_flags) 548 { 549 struct fuse_ring_queue *queue = ent->queue; 550 int err; 551 struct io_uring_cmd *cmd; 552 553 err = fuse_uring_prepare_send(ent, req); 554 if (err) 555 return err; 556 557 spin_lock(&queue->lock); 558 cmd = ent->cmd; 559 ent->cmd = NULL; 560 ent->state = FRRS_USERSPACE; 561 list_move(&ent->list, &queue->ent_in_userspace); 562 spin_unlock(&queue->lock); 563 564 io_uring_cmd_done(cmd, 0, 0, issue_flags); 565 return 0; 566 } 567 568 /* 569 * Make a ring entry available for fuse_req assignment 570 */ 571 static void fuse_uring_ent_avail(struct fuse_ring_ent *ent, 572 struct fuse_ring_queue *queue) 573 { 574 WARN_ON_ONCE(!ent->cmd); 575 list_move(&ent->list, &queue->ent_avail_queue); 576 ent->state = FRRS_AVAILABLE; 577 } 578 579 /* Used to find the request on SQE commit */ 580 static void fuse_uring_add_to_pq(struct fuse_ring_ent *ent, 581 struct fuse_req *req) 582 { 583 struct fuse_ring_queue *queue = ent->queue; 584 struct fuse_pqueue *fpq = &queue->fpq; 585 unsigned int hash; 586 587 req->ring_entry = ent; 588 hash = fuse_req_hash(req->in.h.unique); 589 list_move_tail(&req->list, &fpq->processing[hash]); 590 } 591 592 /* 593 * Assign a fuse queue entry to the given entry 594 */ 595 static void fuse_uring_add_req_to_ring_ent(struct fuse_ring_ent *ent, 596 struct fuse_req *req) 597 { 598 struct fuse_ring_queue *queue = ent->queue; 599 struct fuse_conn *fc = req->fm->fc; 600 struct fuse_iqueue *fiq = &fc->iq; 601 602 lockdep_assert_held(&queue->lock); 603 604 if (WARN_ON_ONCE(ent->state != FRRS_AVAILABLE && 605 ent->state != FRRS_COMMIT)) { 606 pr_warn("%s qid=%d state=%d\n", __func__, ent->queue->qid, 607 ent->state); 608 } 609 610 spin_lock(&fiq->lock); 611 clear_bit(FR_PENDING, &req->flags); 612 spin_unlock(&fiq->lock); 613 ent->fuse_req = req; 614 ent->state = FRRS_FUSE_REQ; 615 list_move(&ent->list, &queue->ent_w_req_queue); 616 fuse_uring_add_to_pq(ent, req); 617 } 618 619 /* Fetch the next fuse request if available */ 620 static struct fuse_req *fuse_uring_ent_assign_req(struct fuse_ring_ent *ent) 621 __must_hold(&queue->lock) 622 { 623 struct fuse_req *req; 624 struct fuse_ring_queue *queue = ent->queue; 625 struct list_head *req_queue = &queue->fuse_req_queue; 626 627 lockdep_assert_held(&queue->lock); 628 629 /* get and assign the next entry while it is still holding the lock */ 630 req = list_first_entry_or_null(req_queue, struct fuse_req, list); 631 if (req) 632 fuse_uring_add_req_to_ring_ent(ent, req); 633 634 return req; 635 } 636 637 /* 638 * Read data from the ring buffer, which user space has written to 639 * This is comparible with handling of classical write(/dev/fuse). 640 * Also make the ring request available again for new fuse requests. 641 */ 642 static void fuse_uring_commit(struct fuse_ring_ent *ent, struct fuse_req *req, 643 unsigned int issue_flags) 644 { 645 struct fuse_ring *ring = ent->queue->ring; 646 struct fuse_conn *fc = ring->fc; 647 ssize_t err = 0; 648 649 err = copy_from_user(&req->out.h, &ent->headers->in_out, 650 sizeof(req->out.h)); 651 if (err) { 652 req->out.h.error = -EFAULT; 653 goto out; 654 } 655 656 err = fuse_uring_out_header_has_err(&req->out.h, req, fc); 657 if (err) { 658 /* req->out.h.error already set */ 659 goto out; 660 } 661 662 err = fuse_uring_copy_from_ring(ring, req, ent); 663 out: 664 fuse_uring_req_end(ent, req, err); 665 } 666 667 /* 668 * Get the next fuse req and send it 669 */ 670 static void fuse_uring_next_fuse_req(struct fuse_ring_ent *ent, 671 struct fuse_ring_queue *queue, 672 unsigned int issue_flags) 673 { 674 int err; 675 struct fuse_req *req; 676 677 retry: 678 spin_lock(&queue->lock); 679 fuse_uring_ent_avail(ent, queue); 680 req = fuse_uring_ent_assign_req(ent); 681 spin_unlock(&queue->lock); 682 683 if (req) { 684 err = fuse_uring_send_next_to_ring(ent, req, issue_flags); 685 if (err) 686 goto retry; 687 } 688 } 689 690 static int fuse_ring_ent_set_commit(struct fuse_ring_ent *ent) 691 { 692 struct fuse_ring_queue *queue = ent->queue; 693 694 lockdep_assert_held(&queue->lock); 695 696 if (WARN_ON_ONCE(ent->state != FRRS_USERSPACE)) 697 return -EIO; 698 699 ent->state = FRRS_COMMIT; 700 list_move(&ent->list, &queue->ent_commit_queue); 701 702 return 0; 703 } 704 705 /* FUSE_URING_CMD_COMMIT_AND_FETCH handler */ 706 static int fuse_uring_commit_fetch(struct io_uring_cmd *cmd, int issue_flags, 707 struct fuse_conn *fc) 708 { 709 const struct fuse_uring_cmd_req *cmd_req = io_uring_sqe_cmd(cmd->sqe); 710 struct fuse_ring_ent *ent; 711 int err; 712 struct fuse_ring *ring = fc->ring; 713 struct fuse_ring_queue *queue; 714 uint64_t commit_id = READ_ONCE(cmd_req->commit_id); 715 unsigned int qid = READ_ONCE(cmd_req->qid); 716 struct fuse_pqueue *fpq; 717 struct fuse_req *req; 718 719 err = -ENOTCONN; 720 if (!ring) 721 return err; 722 723 if (qid >= ring->nr_queues) 724 return -EINVAL; 725 726 queue = ring->queues[qid]; 727 if (!queue) 728 return err; 729 fpq = &queue->fpq; 730 731 if (!READ_ONCE(fc->connected) || READ_ONCE(queue->stopped)) 732 return err; 733 734 spin_lock(&queue->lock); 735 /* Find a request based on the unique ID of the fuse request 736 * This should get revised, as it needs a hash calculation and list 737 * search. And full struct fuse_pqueue is needed (memory overhead). 738 * As well as the link from req to ring_ent. 739 */ 740 req = fuse_request_find(fpq, commit_id); 741 err = -ENOENT; 742 if (!req) { 743 pr_info("qid=%d commit_id %llu not found\n", queue->qid, 744 commit_id); 745 spin_unlock(&queue->lock); 746 return err; 747 } 748 list_del_init(&req->list); 749 ent = req->ring_entry; 750 req->ring_entry = NULL; 751 752 err = fuse_ring_ent_set_commit(ent); 753 if (err != 0) { 754 pr_info_ratelimited("qid=%d commit_id %llu state %d", 755 queue->qid, commit_id, ent->state); 756 spin_unlock(&queue->lock); 757 req->out.h.error = err; 758 clear_bit(FR_SENT, &req->flags); 759 fuse_request_end(req); 760 return err; 761 } 762 763 ent->cmd = cmd; 764 spin_unlock(&queue->lock); 765 766 /* without the queue lock, as other locks are taken */ 767 fuse_uring_commit(ent, req, issue_flags); 768 769 /* 770 * Fetching the next request is absolutely required as queued 771 * fuse requests would otherwise not get processed - committing 772 * and fetching is done in one step vs legacy fuse, which has separated 773 * read (fetch request) and write (commit result). 774 */ 775 fuse_uring_next_fuse_req(ent, queue, issue_flags); 776 return 0; 777 } 778 779 /* 780 * fuse_uring_req_fetch command handling 781 */ 782 static void fuse_uring_do_register(struct fuse_ring_ent *ent, 783 struct io_uring_cmd *cmd, 784 unsigned int issue_flags) 785 { 786 struct fuse_ring_queue *queue = ent->queue; 787 788 spin_lock(&queue->lock); 789 ent->cmd = cmd; 790 fuse_uring_ent_avail(ent, queue); 791 spin_unlock(&queue->lock); 792 } 793 794 /* 795 * sqe->addr is a ptr to an iovec array, iov[0] has the headers, iov[1] 796 * the payload 797 */ 798 static int fuse_uring_get_iovec_from_sqe(const struct io_uring_sqe *sqe, 799 struct iovec iov[FUSE_URING_IOV_SEGS]) 800 { 801 struct iovec __user *uiov = u64_to_user_ptr(READ_ONCE(sqe->addr)); 802 struct iov_iter iter; 803 ssize_t ret; 804 805 if (sqe->len != FUSE_URING_IOV_SEGS) 806 return -EINVAL; 807 808 /* 809 * Direction for buffer access will actually be READ and WRITE, 810 * using write for the import should include READ access as well. 811 */ 812 ret = import_iovec(WRITE, uiov, FUSE_URING_IOV_SEGS, 813 FUSE_URING_IOV_SEGS, &iov, &iter); 814 if (ret < 0) 815 return ret; 816 817 return 0; 818 } 819 820 static struct fuse_ring_ent * 821 fuse_uring_create_ring_ent(struct io_uring_cmd *cmd, 822 struct fuse_ring_queue *queue) 823 { 824 struct fuse_ring *ring = queue->ring; 825 struct fuse_ring_ent *ent; 826 size_t payload_size; 827 struct iovec iov[FUSE_URING_IOV_SEGS]; 828 int err; 829 830 err = fuse_uring_get_iovec_from_sqe(cmd->sqe, iov); 831 if (err) { 832 pr_info_ratelimited("Failed to get iovec from sqe, err=%d\n", 833 err); 834 return ERR_PTR(err); 835 } 836 837 err = -EINVAL; 838 if (iov[0].iov_len < sizeof(struct fuse_uring_req_header)) { 839 pr_info_ratelimited("Invalid header len %zu\n", iov[0].iov_len); 840 return ERR_PTR(err); 841 } 842 843 payload_size = iov[1].iov_len; 844 if (payload_size < ring->max_payload_sz) { 845 pr_info_ratelimited("Invalid req payload len %zu\n", 846 payload_size); 847 return ERR_PTR(err); 848 } 849 850 err = -ENOMEM; 851 ent = kzalloc(sizeof(*ent), GFP_KERNEL_ACCOUNT); 852 if (!ent) 853 return ERR_PTR(err); 854 855 INIT_LIST_HEAD(&ent->list); 856 857 ent->queue = queue; 858 ent->headers = iov[0].iov_base; 859 ent->payload = iov[1].iov_base; 860 861 atomic_inc(&ring->queue_refs); 862 return ent; 863 } 864 865 /* 866 * Register header and payload buffer with the kernel and puts the 867 * entry as "ready to get fuse requests" on the queue 868 */ 869 static int fuse_uring_register(struct io_uring_cmd *cmd, 870 unsigned int issue_flags, struct fuse_conn *fc) 871 { 872 const struct fuse_uring_cmd_req *cmd_req = io_uring_sqe_cmd(cmd->sqe); 873 struct fuse_ring *ring = fc->ring; 874 struct fuse_ring_queue *queue; 875 struct fuse_ring_ent *ent; 876 int err; 877 unsigned int qid = READ_ONCE(cmd_req->qid); 878 879 err = -ENOMEM; 880 if (!ring) { 881 ring = fuse_uring_create(fc); 882 if (!ring) 883 return err; 884 } 885 886 if (qid >= ring->nr_queues) { 887 pr_info_ratelimited("fuse: Invalid ring qid %u\n", qid); 888 return -EINVAL; 889 } 890 891 queue = ring->queues[qid]; 892 if (!queue) { 893 queue = fuse_uring_create_queue(ring, qid); 894 if (!queue) 895 return err; 896 } 897 898 /* 899 * The created queue above does not need to be destructed in 900 * case of entry errors below, will be done at ring destruction time. 901 */ 902 903 ent = fuse_uring_create_ring_ent(cmd, queue); 904 if (IS_ERR(ent)) 905 return PTR_ERR(ent); 906 907 fuse_uring_do_register(ent, cmd, issue_flags); 908 909 return 0; 910 } 911 912 /* 913 * Entry function from io_uring to handle the given passthrough command 914 * (op code IORING_OP_URING_CMD) 915 */ 916 int __maybe_unused fuse_uring_cmd(struct io_uring_cmd *cmd, 917 unsigned int issue_flags) 918 { 919 struct fuse_dev *fud; 920 struct fuse_conn *fc; 921 u32 cmd_op = cmd->cmd_op; 922 int err; 923 924 if (!enable_uring) { 925 pr_info_ratelimited("fuse-io-uring is disabled\n"); 926 return -EOPNOTSUPP; 927 } 928 929 /* This extra SQE size holds struct fuse_uring_cmd_req */ 930 if (!(issue_flags & IO_URING_F_SQE128)) 931 return -EINVAL; 932 933 fud = fuse_get_dev(cmd->file); 934 if (!fud) { 935 pr_info_ratelimited("No fuse device found\n"); 936 return -ENOTCONN; 937 } 938 fc = fud->fc; 939 940 if (fc->aborted) 941 return -ECONNABORTED; 942 if (!fc->connected) 943 return -ENOTCONN; 944 945 /* 946 * fuse_uring_register() needs the ring to be initialized, 947 * we need to know the max payload size 948 */ 949 if (!fc->initialized) 950 return -EAGAIN; 951 952 switch (cmd_op) { 953 case FUSE_IO_URING_CMD_REGISTER: 954 err = fuse_uring_register(cmd, issue_flags, fc); 955 if (err) { 956 pr_info_once("FUSE_IO_URING_CMD_REGISTER failed err=%d\n", 957 err); 958 return err; 959 } 960 break; 961 case FUSE_IO_URING_CMD_COMMIT_AND_FETCH: 962 err = fuse_uring_commit_fetch(cmd, issue_flags, fc); 963 if (err) { 964 pr_info_once("FUSE_IO_URING_COMMIT_AND_FETCH failed err=%d\n", 965 err); 966 return err; 967 } 968 break; 969 default: 970 return -EINVAL; 971 } 972 973 return -EIOCBQUEUED; 974 } 975