1 /* 2 FUSE: Filesystem in Userspace 3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu> 4 5 This program can be distributed under the terms of the GNU GPL. 6 See the file COPYING. 7 */ 8 9 #include "dev_uring_i.h" 10 #include "fuse_i.h" 11 #include "fuse_dev_i.h" 12 13 #include <linux/init.h> 14 #include <linux/module.h> 15 #include <linux/poll.h> 16 #include <linux/sched/signal.h> 17 #include <linux/uio.h> 18 #include <linux/miscdevice.h> 19 #include <linux/pagemap.h> 20 #include <linux/file.h> 21 #include <linux/slab.h> 22 #include <linux/pipe_fs_i.h> 23 #include <linux/swap.h> 24 #include <linux/splice.h> 25 #include <linux/sched.h> 26 27 #define CREATE_TRACE_POINTS 28 #include "fuse_trace.h" 29 30 MODULE_ALIAS_MISCDEV(FUSE_MINOR); 31 MODULE_ALIAS("devname:fuse"); 32 33 static struct kmem_cache *fuse_req_cachep; 34 35 static void fuse_request_init(struct fuse_mount *fm, struct fuse_req *req) 36 { 37 INIT_LIST_HEAD(&req->list); 38 INIT_LIST_HEAD(&req->intr_entry); 39 init_waitqueue_head(&req->waitq); 40 refcount_set(&req->count, 1); 41 __set_bit(FR_PENDING, &req->flags); 42 req->fm = fm; 43 } 44 45 static struct fuse_req *fuse_request_alloc(struct fuse_mount *fm, gfp_t flags) 46 { 47 struct fuse_req *req = kmem_cache_zalloc(fuse_req_cachep, flags); 48 if (req) 49 fuse_request_init(fm, req); 50 51 return req; 52 } 53 54 static void fuse_request_free(struct fuse_req *req) 55 { 56 kmem_cache_free(fuse_req_cachep, req); 57 } 58 59 static void __fuse_get_request(struct fuse_req *req) 60 { 61 refcount_inc(&req->count); 62 } 63 64 /* Must be called with > 1 refcount */ 65 static void __fuse_put_request(struct fuse_req *req) 66 { 67 refcount_dec(&req->count); 68 } 69 70 void fuse_set_initialized(struct fuse_conn *fc) 71 { 72 /* Make sure stores before this are seen on another CPU */ 73 smp_wmb(); 74 fc->initialized = 1; 75 } 76 77 static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background) 78 { 79 return !fc->initialized || (for_background && fc->blocked) || 80 (fc->io_uring && fc->connected && !fuse_uring_ready(fc)); 81 } 82 83 static void fuse_drop_waiting(struct fuse_conn *fc) 84 { 85 /* 86 * lockess check of fc->connected is okay, because atomic_dec_and_test() 87 * provides a memory barrier matched with the one in fuse_wait_aborted() 88 * to ensure no wake-up is missed. 89 */ 90 if (atomic_dec_and_test(&fc->num_waiting) && 91 !READ_ONCE(fc->connected)) { 92 /* wake up aborters */ 93 wake_up_all(&fc->blocked_waitq); 94 } 95 } 96 97 static void fuse_put_request(struct fuse_req *req); 98 99 static struct fuse_req *fuse_get_req(struct mnt_idmap *idmap, 100 struct fuse_mount *fm, 101 bool for_background) 102 { 103 struct fuse_conn *fc = fm->fc; 104 struct fuse_req *req; 105 bool no_idmap = !fm->sb || (fm->sb->s_iflags & SB_I_NOIDMAP); 106 kuid_t fsuid; 107 kgid_t fsgid; 108 int err; 109 110 atomic_inc(&fc->num_waiting); 111 112 if (fuse_block_alloc(fc, for_background)) { 113 err = -EINTR; 114 if (wait_event_killable_exclusive(fc->blocked_waitq, 115 !fuse_block_alloc(fc, for_background))) 116 goto out; 117 } 118 /* Matches smp_wmb() in fuse_set_initialized() */ 119 smp_rmb(); 120 121 err = -ENOTCONN; 122 if (!fc->connected) 123 goto out; 124 125 err = -ECONNREFUSED; 126 if (fc->conn_error) 127 goto out; 128 129 req = fuse_request_alloc(fm, GFP_KERNEL); 130 err = -ENOMEM; 131 if (!req) { 132 if (for_background) 133 wake_up(&fc->blocked_waitq); 134 goto out; 135 } 136 137 req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns); 138 139 __set_bit(FR_WAITING, &req->flags); 140 if (for_background) 141 __set_bit(FR_BACKGROUND, &req->flags); 142 143 /* 144 * Keep the old behavior when idmappings support was not 145 * declared by a FUSE server. 146 * 147 * For those FUSE servers who support idmapped mounts, 148 * we send UID/GID only along with "inode creation" 149 * fuse requests, otherwise idmap == &invalid_mnt_idmap and 150 * req->in.h.{u,g}id will be equal to FUSE_INVALID_UIDGID. 151 */ 152 fsuid = no_idmap ? current_fsuid() : mapped_fsuid(idmap, fc->user_ns); 153 fsgid = no_idmap ? current_fsgid() : mapped_fsgid(idmap, fc->user_ns); 154 req->in.h.uid = from_kuid(fc->user_ns, fsuid); 155 req->in.h.gid = from_kgid(fc->user_ns, fsgid); 156 157 if (no_idmap && unlikely(req->in.h.uid == ((uid_t)-1) || 158 req->in.h.gid == ((gid_t)-1))) { 159 fuse_put_request(req); 160 return ERR_PTR(-EOVERFLOW); 161 } 162 163 return req; 164 165 out: 166 fuse_drop_waiting(fc); 167 return ERR_PTR(err); 168 } 169 170 static void fuse_put_request(struct fuse_req *req) 171 { 172 struct fuse_conn *fc = req->fm->fc; 173 174 if (refcount_dec_and_test(&req->count)) { 175 if (test_bit(FR_BACKGROUND, &req->flags)) { 176 /* 177 * We get here in the unlikely case that a background 178 * request was allocated but not sent 179 */ 180 spin_lock(&fc->bg_lock); 181 if (!fc->blocked) 182 wake_up(&fc->blocked_waitq); 183 spin_unlock(&fc->bg_lock); 184 } 185 186 if (test_bit(FR_WAITING, &req->flags)) { 187 __clear_bit(FR_WAITING, &req->flags); 188 fuse_drop_waiting(fc); 189 } 190 191 fuse_request_free(req); 192 } 193 } 194 195 unsigned int fuse_len_args(unsigned int numargs, struct fuse_arg *args) 196 { 197 unsigned nbytes = 0; 198 unsigned i; 199 200 for (i = 0; i < numargs; i++) 201 nbytes += args[i].size; 202 203 return nbytes; 204 } 205 EXPORT_SYMBOL_GPL(fuse_len_args); 206 207 static u64 fuse_get_unique_locked(struct fuse_iqueue *fiq) 208 { 209 fiq->reqctr += FUSE_REQ_ID_STEP; 210 return fiq->reqctr; 211 } 212 213 u64 fuse_get_unique(struct fuse_iqueue *fiq) 214 { 215 u64 ret; 216 217 spin_lock(&fiq->lock); 218 ret = fuse_get_unique_locked(fiq); 219 spin_unlock(&fiq->lock); 220 221 return ret; 222 } 223 EXPORT_SYMBOL_GPL(fuse_get_unique); 224 225 unsigned int fuse_req_hash(u64 unique) 226 { 227 return hash_long(unique & ~FUSE_INT_REQ_BIT, FUSE_PQ_HASH_BITS); 228 } 229 230 /* 231 * A new request is available, wake fiq->waitq 232 */ 233 static void fuse_dev_wake_and_unlock(struct fuse_iqueue *fiq) 234 __releases(fiq->lock) 235 { 236 wake_up(&fiq->waitq); 237 kill_fasync(&fiq->fasync, SIGIO, POLL_IN); 238 spin_unlock(&fiq->lock); 239 } 240 241 void fuse_dev_queue_forget(struct fuse_iqueue *fiq, 242 struct fuse_forget_link *forget) 243 { 244 spin_lock(&fiq->lock); 245 if (fiq->connected) { 246 fiq->forget_list_tail->next = forget; 247 fiq->forget_list_tail = forget; 248 fuse_dev_wake_and_unlock(fiq); 249 } else { 250 kfree(forget); 251 spin_unlock(&fiq->lock); 252 } 253 } 254 255 void fuse_dev_queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req) 256 { 257 spin_lock(&fiq->lock); 258 if (list_empty(&req->intr_entry)) { 259 list_add_tail(&req->intr_entry, &fiq->interrupts); 260 /* 261 * Pairs with smp_mb() implied by test_and_set_bit() 262 * from fuse_request_end(). 263 */ 264 smp_mb(); 265 if (test_bit(FR_FINISHED, &req->flags)) { 266 list_del_init(&req->intr_entry); 267 spin_unlock(&fiq->lock); 268 } else { 269 fuse_dev_wake_and_unlock(fiq); 270 } 271 } else { 272 spin_unlock(&fiq->lock); 273 } 274 } 275 276 static void fuse_dev_queue_req(struct fuse_iqueue *fiq, struct fuse_req *req) 277 { 278 spin_lock(&fiq->lock); 279 if (fiq->connected) { 280 if (req->in.h.opcode != FUSE_NOTIFY_REPLY) 281 req->in.h.unique = fuse_get_unique_locked(fiq); 282 list_add_tail(&req->list, &fiq->pending); 283 fuse_dev_wake_and_unlock(fiq); 284 } else { 285 spin_unlock(&fiq->lock); 286 req->out.h.error = -ENOTCONN; 287 clear_bit(FR_PENDING, &req->flags); 288 fuse_request_end(req); 289 } 290 } 291 292 const struct fuse_iqueue_ops fuse_dev_fiq_ops = { 293 .send_forget = fuse_dev_queue_forget, 294 .send_interrupt = fuse_dev_queue_interrupt, 295 .send_req = fuse_dev_queue_req, 296 }; 297 EXPORT_SYMBOL_GPL(fuse_dev_fiq_ops); 298 299 static void fuse_send_one(struct fuse_iqueue *fiq, struct fuse_req *req) 300 { 301 req->in.h.len = sizeof(struct fuse_in_header) + 302 fuse_len_args(req->args->in_numargs, 303 (struct fuse_arg *) req->args->in_args); 304 trace_fuse_request_send(req); 305 fiq->ops->send_req(fiq, req); 306 } 307 308 void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget, 309 u64 nodeid, u64 nlookup) 310 { 311 struct fuse_iqueue *fiq = &fc->iq; 312 313 forget->forget_one.nodeid = nodeid; 314 forget->forget_one.nlookup = nlookup; 315 316 fiq->ops->send_forget(fiq, forget); 317 } 318 319 static void flush_bg_queue(struct fuse_conn *fc) 320 { 321 struct fuse_iqueue *fiq = &fc->iq; 322 323 while (fc->active_background < fc->max_background && 324 !list_empty(&fc->bg_queue)) { 325 struct fuse_req *req; 326 327 req = list_first_entry(&fc->bg_queue, struct fuse_req, list); 328 list_del(&req->list); 329 fc->active_background++; 330 fuse_send_one(fiq, req); 331 } 332 } 333 334 /* 335 * This function is called when a request is finished. Either a reply 336 * has arrived or it was aborted (and not yet sent) or some error 337 * occurred during communication with userspace, or the device file 338 * was closed. The requester thread is woken up (if still waiting), 339 * the 'end' callback is called if given, else the reference to the 340 * request is released 341 */ 342 void fuse_request_end(struct fuse_req *req) 343 { 344 struct fuse_mount *fm = req->fm; 345 struct fuse_conn *fc = fm->fc; 346 struct fuse_iqueue *fiq = &fc->iq; 347 348 if (test_and_set_bit(FR_FINISHED, &req->flags)) 349 goto put_request; 350 351 trace_fuse_request_end(req); 352 /* 353 * test_and_set_bit() implies smp_mb() between bit 354 * changing and below FR_INTERRUPTED check. Pairs with 355 * smp_mb() from queue_interrupt(). 356 */ 357 if (test_bit(FR_INTERRUPTED, &req->flags)) { 358 spin_lock(&fiq->lock); 359 list_del_init(&req->intr_entry); 360 spin_unlock(&fiq->lock); 361 } 362 WARN_ON(test_bit(FR_PENDING, &req->flags)); 363 WARN_ON(test_bit(FR_SENT, &req->flags)); 364 if (test_bit(FR_BACKGROUND, &req->flags)) { 365 spin_lock(&fc->bg_lock); 366 clear_bit(FR_BACKGROUND, &req->flags); 367 if (fc->num_background == fc->max_background) { 368 fc->blocked = 0; 369 wake_up(&fc->blocked_waitq); 370 } else if (!fc->blocked) { 371 /* 372 * Wake up next waiter, if any. It's okay to use 373 * waitqueue_active(), as we've already synced up 374 * fc->blocked with waiters with the wake_up() call 375 * above. 376 */ 377 if (waitqueue_active(&fc->blocked_waitq)) 378 wake_up(&fc->blocked_waitq); 379 } 380 381 fc->num_background--; 382 fc->active_background--; 383 flush_bg_queue(fc); 384 spin_unlock(&fc->bg_lock); 385 } else { 386 /* Wake up waiter sleeping in request_wait_answer() */ 387 wake_up(&req->waitq); 388 } 389 390 if (test_bit(FR_ASYNC, &req->flags)) 391 req->args->end(fm, req->args, req->out.h.error); 392 put_request: 393 fuse_put_request(req); 394 } 395 EXPORT_SYMBOL_GPL(fuse_request_end); 396 397 static int queue_interrupt(struct fuse_req *req) 398 { 399 struct fuse_iqueue *fiq = &req->fm->fc->iq; 400 401 /* Check for we've sent request to interrupt this req */ 402 if (unlikely(!test_bit(FR_INTERRUPTED, &req->flags))) 403 return -EINVAL; 404 405 fiq->ops->send_interrupt(fiq, req); 406 407 return 0; 408 } 409 410 static void request_wait_answer(struct fuse_req *req) 411 { 412 struct fuse_conn *fc = req->fm->fc; 413 struct fuse_iqueue *fiq = &fc->iq; 414 int err; 415 416 if (!fc->no_interrupt) { 417 /* Any signal may interrupt this */ 418 err = wait_event_interruptible(req->waitq, 419 test_bit(FR_FINISHED, &req->flags)); 420 if (!err) 421 return; 422 423 set_bit(FR_INTERRUPTED, &req->flags); 424 /* matches barrier in fuse_dev_do_read() */ 425 smp_mb__after_atomic(); 426 if (test_bit(FR_SENT, &req->flags)) 427 queue_interrupt(req); 428 } 429 430 if (!test_bit(FR_FORCE, &req->flags)) { 431 /* Only fatal signals may interrupt this */ 432 err = wait_event_killable(req->waitq, 433 test_bit(FR_FINISHED, &req->flags)); 434 if (!err) 435 return; 436 437 spin_lock(&fiq->lock); 438 /* Request is not yet in userspace, bail out */ 439 if (test_bit(FR_PENDING, &req->flags)) { 440 list_del(&req->list); 441 spin_unlock(&fiq->lock); 442 __fuse_put_request(req); 443 req->out.h.error = -EINTR; 444 return; 445 } 446 spin_unlock(&fiq->lock); 447 } 448 449 /* 450 * Either request is already in userspace, or it was forced. 451 * Wait it out. 452 */ 453 wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags)); 454 } 455 456 static void __fuse_request_send(struct fuse_req *req) 457 { 458 struct fuse_iqueue *fiq = &req->fm->fc->iq; 459 460 BUG_ON(test_bit(FR_BACKGROUND, &req->flags)); 461 462 /* acquire extra reference, since request is still needed after 463 fuse_request_end() */ 464 __fuse_get_request(req); 465 fuse_send_one(fiq, req); 466 467 request_wait_answer(req); 468 /* Pairs with smp_wmb() in fuse_request_end() */ 469 smp_rmb(); 470 } 471 472 static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args) 473 { 474 if (fc->minor < 4 && args->opcode == FUSE_STATFS) 475 args->out_args[0].size = FUSE_COMPAT_STATFS_SIZE; 476 477 if (fc->minor < 9) { 478 switch (args->opcode) { 479 case FUSE_LOOKUP: 480 case FUSE_CREATE: 481 case FUSE_MKNOD: 482 case FUSE_MKDIR: 483 case FUSE_SYMLINK: 484 case FUSE_LINK: 485 args->out_args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE; 486 break; 487 case FUSE_GETATTR: 488 case FUSE_SETATTR: 489 args->out_args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE; 490 break; 491 } 492 } 493 if (fc->minor < 12) { 494 switch (args->opcode) { 495 case FUSE_CREATE: 496 args->in_args[0].size = sizeof(struct fuse_open_in); 497 break; 498 case FUSE_MKNOD: 499 args->in_args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE; 500 break; 501 } 502 } 503 } 504 505 static void fuse_force_creds(struct fuse_req *req) 506 { 507 struct fuse_conn *fc = req->fm->fc; 508 509 if (!req->fm->sb || req->fm->sb->s_iflags & SB_I_NOIDMAP) { 510 req->in.h.uid = from_kuid_munged(fc->user_ns, current_fsuid()); 511 req->in.h.gid = from_kgid_munged(fc->user_ns, current_fsgid()); 512 } else { 513 req->in.h.uid = FUSE_INVALID_UIDGID; 514 req->in.h.gid = FUSE_INVALID_UIDGID; 515 } 516 517 req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns); 518 } 519 520 static void fuse_args_to_req(struct fuse_req *req, struct fuse_args *args) 521 { 522 req->in.h.opcode = args->opcode; 523 req->in.h.nodeid = args->nodeid; 524 req->args = args; 525 if (args->is_ext) 526 req->in.h.total_extlen = args->in_args[args->ext_idx].size / 8; 527 if (args->end) 528 __set_bit(FR_ASYNC, &req->flags); 529 } 530 531 ssize_t __fuse_simple_request(struct mnt_idmap *idmap, 532 struct fuse_mount *fm, 533 struct fuse_args *args) 534 { 535 struct fuse_conn *fc = fm->fc; 536 struct fuse_req *req; 537 ssize_t ret; 538 539 if (args->force) { 540 atomic_inc(&fc->num_waiting); 541 req = fuse_request_alloc(fm, GFP_KERNEL | __GFP_NOFAIL); 542 543 if (!args->nocreds) 544 fuse_force_creds(req); 545 546 __set_bit(FR_WAITING, &req->flags); 547 __set_bit(FR_FORCE, &req->flags); 548 } else { 549 WARN_ON(args->nocreds); 550 req = fuse_get_req(idmap, fm, false); 551 if (IS_ERR(req)) 552 return PTR_ERR(req); 553 } 554 555 /* Needs to be done after fuse_get_req() so that fc->minor is valid */ 556 fuse_adjust_compat(fc, args); 557 fuse_args_to_req(req, args); 558 559 if (!args->noreply) 560 __set_bit(FR_ISREPLY, &req->flags); 561 __fuse_request_send(req); 562 ret = req->out.h.error; 563 if (!ret && args->out_argvar) { 564 BUG_ON(args->out_numargs == 0); 565 ret = args->out_args[args->out_numargs - 1].size; 566 } 567 fuse_put_request(req); 568 569 return ret; 570 } 571 572 #ifdef CONFIG_FUSE_IO_URING 573 static bool fuse_request_queue_background_uring(struct fuse_conn *fc, 574 struct fuse_req *req) 575 { 576 struct fuse_iqueue *fiq = &fc->iq; 577 578 req->in.h.unique = fuse_get_unique(fiq); 579 req->in.h.len = sizeof(struct fuse_in_header) + 580 fuse_len_args(req->args->in_numargs, 581 (struct fuse_arg *) req->args->in_args); 582 583 return fuse_uring_queue_bq_req(req); 584 } 585 #endif 586 587 /* 588 * @return true if queued 589 */ 590 static int fuse_request_queue_background(struct fuse_req *req) 591 { 592 struct fuse_mount *fm = req->fm; 593 struct fuse_conn *fc = fm->fc; 594 bool queued = false; 595 596 WARN_ON(!test_bit(FR_BACKGROUND, &req->flags)); 597 if (!test_bit(FR_WAITING, &req->flags)) { 598 __set_bit(FR_WAITING, &req->flags); 599 atomic_inc(&fc->num_waiting); 600 } 601 __set_bit(FR_ISREPLY, &req->flags); 602 603 #ifdef CONFIG_FUSE_IO_URING 604 if (fuse_uring_ready(fc)) 605 return fuse_request_queue_background_uring(fc, req); 606 #endif 607 608 spin_lock(&fc->bg_lock); 609 if (likely(fc->connected)) { 610 fc->num_background++; 611 if (fc->num_background == fc->max_background) 612 fc->blocked = 1; 613 list_add_tail(&req->list, &fc->bg_queue); 614 flush_bg_queue(fc); 615 queued = true; 616 } 617 spin_unlock(&fc->bg_lock); 618 619 return queued; 620 } 621 622 int fuse_simple_background(struct fuse_mount *fm, struct fuse_args *args, 623 gfp_t gfp_flags) 624 { 625 struct fuse_req *req; 626 627 if (args->force) { 628 WARN_ON(!args->nocreds); 629 req = fuse_request_alloc(fm, gfp_flags); 630 if (!req) 631 return -ENOMEM; 632 __set_bit(FR_BACKGROUND, &req->flags); 633 } else { 634 WARN_ON(args->nocreds); 635 req = fuse_get_req(&invalid_mnt_idmap, fm, true); 636 if (IS_ERR(req)) 637 return PTR_ERR(req); 638 } 639 640 fuse_args_to_req(req, args); 641 642 if (!fuse_request_queue_background(req)) { 643 fuse_put_request(req); 644 return -ENOTCONN; 645 } 646 647 return 0; 648 } 649 EXPORT_SYMBOL_GPL(fuse_simple_background); 650 651 static int fuse_simple_notify_reply(struct fuse_mount *fm, 652 struct fuse_args *args, u64 unique) 653 { 654 struct fuse_req *req; 655 struct fuse_iqueue *fiq = &fm->fc->iq; 656 657 req = fuse_get_req(&invalid_mnt_idmap, fm, false); 658 if (IS_ERR(req)) 659 return PTR_ERR(req); 660 661 __clear_bit(FR_ISREPLY, &req->flags); 662 req->in.h.unique = unique; 663 664 fuse_args_to_req(req, args); 665 666 fuse_send_one(fiq, req); 667 668 return 0; 669 } 670 671 /* 672 * Lock the request. Up to the next unlock_request() there mustn't be 673 * anything that could cause a page-fault. If the request was already 674 * aborted bail out. 675 */ 676 static int lock_request(struct fuse_req *req) 677 { 678 int err = 0; 679 if (req) { 680 spin_lock(&req->waitq.lock); 681 if (test_bit(FR_ABORTED, &req->flags)) 682 err = -ENOENT; 683 else 684 set_bit(FR_LOCKED, &req->flags); 685 spin_unlock(&req->waitq.lock); 686 } 687 return err; 688 } 689 690 /* 691 * Unlock request. If it was aborted while locked, caller is responsible 692 * for unlocking and ending the request. 693 */ 694 static int unlock_request(struct fuse_req *req) 695 { 696 int err = 0; 697 if (req) { 698 spin_lock(&req->waitq.lock); 699 if (test_bit(FR_ABORTED, &req->flags)) 700 err = -ENOENT; 701 else 702 clear_bit(FR_LOCKED, &req->flags); 703 spin_unlock(&req->waitq.lock); 704 } 705 return err; 706 } 707 708 void fuse_copy_init(struct fuse_copy_state *cs, int write, 709 struct iov_iter *iter) 710 { 711 memset(cs, 0, sizeof(*cs)); 712 cs->write = write; 713 cs->iter = iter; 714 } 715 716 /* Unmap and put previous page of userspace buffer */ 717 static void fuse_copy_finish(struct fuse_copy_state *cs) 718 { 719 if (cs->currbuf) { 720 struct pipe_buffer *buf = cs->currbuf; 721 722 if (cs->write) 723 buf->len = PAGE_SIZE - cs->len; 724 cs->currbuf = NULL; 725 } else if (cs->pg) { 726 if (cs->write) { 727 flush_dcache_page(cs->pg); 728 set_page_dirty_lock(cs->pg); 729 } 730 put_page(cs->pg); 731 } 732 cs->pg = NULL; 733 } 734 735 /* 736 * Get another pagefull of userspace buffer, and map it to kernel 737 * address space, and lock request 738 */ 739 static int fuse_copy_fill(struct fuse_copy_state *cs) 740 { 741 struct page *page; 742 int err; 743 744 err = unlock_request(cs->req); 745 if (err) 746 return err; 747 748 fuse_copy_finish(cs); 749 if (cs->pipebufs) { 750 struct pipe_buffer *buf = cs->pipebufs; 751 752 if (!cs->write) { 753 err = pipe_buf_confirm(cs->pipe, buf); 754 if (err) 755 return err; 756 757 BUG_ON(!cs->nr_segs); 758 cs->currbuf = buf; 759 cs->pg = buf->page; 760 cs->offset = buf->offset; 761 cs->len = buf->len; 762 cs->pipebufs++; 763 cs->nr_segs--; 764 } else { 765 if (cs->nr_segs >= cs->pipe->max_usage) 766 return -EIO; 767 768 page = alloc_page(GFP_HIGHUSER); 769 if (!page) 770 return -ENOMEM; 771 772 buf->page = page; 773 buf->offset = 0; 774 buf->len = 0; 775 776 cs->currbuf = buf; 777 cs->pg = page; 778 cs->offset = 0; 779 cs->len = PAGE_SIZE; 780 cs->pipebufs++; 781 cs->nr_segs++; 782 } 783 } else { 784 size_t off; 785 err = iov_iter_get_pages2(cs->iter, &page, PAGE_SIZE, 1, &off); 786 if (err < 0) 787 return err; 788 BUG_ON(!err); 789 cs->len = err; 790 cs->offset = off; 791 cs->pg = page; 792 } 793 794 return lock_request(cs->req); 795 } 796 797 /* Do as much copy to/from userspace buffer as we can */ 798 static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size) 799 { 800 unsigned ncpy = min(*size, cs->len); 801 if (val) { 802 void *pgaddr = kmap_local_page(cs->pg); 803 void *buf = pgaddr + cs->offset; 804 805 if (cs->write) 806 memcpy(buf, *val, ncpy); 807 else 808 memcpy(*val, buf, ncpy); 809 810 kunmap_local(pgaddr); 811 *val += ncpy; 812 } 813 *size -= ncpy; 814 cs->len -= ncpy; 815 cs->offset += ncpy; 816 if (cs->is_uring) 817 cs->ring.copied_sz += ncpy; 818 819 return ncpy; 820 } 821 822 static int fuse_check_folio(struct folio *folio) 823 { 824 if (folio_mapped(folio) || 825 folio->mapping != NULL || 826 (folio->flags & PAGE_FLAGS_CHECK_AT_PREP & 827 ~(1 << PG_locked | 828 1 << PG_referenced | 829 1 << PG_lru | 830 1 << PG_active | 831 1 << PG_workingset | 832 1 << PG_reclaim | 833 1 << PG_waiters | 834 LRU_GEN_MASK | LRU_REFS_MASK))) { 835 dump_page(&folio->page, "fuse: trying to steal weird page"); 836 return 1; 837 } 838 return 0; 839 } 840 841 /* 842 * Attempt to steal a page from the splice() pipe and move it into the 843 * pagecache. If successful, the pointer in @pagep will be updated. The 844 * folio that was originally in @pagep will lose a reference and the new 845 * folio returned in @pagep will carry a reference. 846 */ 847 static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep) 848 { 849 int err; 850 struct folio *oldfolio = page_folio(*pagep); 851 struct folio *newfolio; 852 struct pipe_buffer *buf = cs->pipebufs; 853 854 folio_get(oldfolio); 855 err = unlock_request(cs->req); 856 if (err) 857 goto out_put_old; 858 859 fuse_copy_finish(cs); 860 861 err = pipe_buf_confirm(cs->pipe, buf); 862 if (err) 863 goto out_put_old; 864 865 BUG_ON(!cs->nr_segs); 866 cs->currbuf = buf; 867 cs->len = buf->len; 868 cs->pipebufs++; 869 cs->nr_segs--; 870 871 if (cs->len != PAGE_SIZE) 872 goto out_fallback; 873 874 if (!pipe_buf_try_steal(cs->pipe, buf)) 875 goto out_fallback; 876 877 newfolio = page_folio(buf->page); 878 879 folio_clear_uptodate(newfolio); 880 folio_clear_mappedtodisk(newfolio); 881 882 if (fuse_check_folio(newfolio) != 0) 883 goto out_fallback_unlock; 884 885 /* 886 * This is a new and locked page, it shouldn't be mapped or 887 * have any special flags on it 888 */ 889 if (WARN_ON(folio_mapped(oldfolio))) 890 goto out_fallback_unlock; 891 if (WARN_ON(folio_has_private(oldfolio))) 892 goto out_fallback_unlock; 893 if (WARN_ON(folio_test_dirty(oldfolio) || 894 folio_test_writeback(oldfolio))) 895 goto out_fallback_unlock; 896 if (WARN_ON(folio_test_mlocked(oldfolio))) 897 goto out_fallback_unlock; 898 899 replace_page_cache_folio(oldfolio, newfolio); 900 901 folio_get(newfolio); 902 903 if (!(buf->flags & PIPE_BUF_FLAG_LRU)) 904 folio_add_lru(newfolio); 905 906 /* 907 * Release while we have extra ref on stolen page. Otherwise 908 * anon_pipe_buf_release() might think the page can be reused. 909 */ 910 pipe_buf_release(cs->pipe, buf); 911 912 err = 0; 913 spin_lock(&cs->req->waitq.lock); 914 if (test_bit(FR_ABORTED, &cs->req->flags)) 915 err = -ENOENT; 916 else 917 *pagep = &newfolio->page; 918 spin_unlock(&cs->req->waitq.lock); 919 920 if (err) { 921 folio_unlock(newfolio); 922 folio_put(newfolio); 923 goto out_put_old; 924 } 925 926 folio_unlock(oldfolio); 927 /* Drop ref for ap->pages[] array */ 928 folio_put(oldfolio); 929 cs->len = 0; 930 931 err = 0; 932 out_put_old: 933 /* Drop ref obtained in this function */ 934 folio_put(oldfolio); 935 return err; 936 937 out_fallback_unlock: 938 folio_unlock(newfolio); 939 out_fallback: 940 cs->pg = buf->page; 941 cs->offset = buf->offset; 942 943 err = lock_request(cs->req); 944 if (!err) 945 err = 1; 946 947 goto out_put_old; 948 } 949 950 static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page, 951 unsigned offset, unsigned count) 952 { 953 struct pipe_buffer *buf; 954 int err; 955 956 if (cs->nr_segs >= cs->pipe->max_usage) 957 return -EIO; 958 959 get_page(page); 960 err = unlock_request(cs->req); 961 if (err) { 962 put_page(page); 963 return err; 964 } 965 966 fuse_copy_finish(cs); 967 968 buf = cs->pipebufs; 969 buf->page = page; 970 buf->offset = offset; 971 buf->len = count; 972 973 cs->pipebufs++; 974 cs->nr_segs++; 975 cs->len = 0; 976 977 return 0; 978 } 979 980 /* 981 * Copy a page in the request to/from the userspace buffer. Must be 982 * done atomically 983 */ 984 static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep, 985 unsigned offset, unsigned count, int zeroing) 986 { 987 int err; 988 struct page *page = *pagep; 989 990 if (page && zeroing && count < PAGE_SIZE) 991 clear_highpage(page); 992 993 while (count) { 994 if (cs->write && cs->pipebufs && page) { 995 /* 996 * Can't control lifetime of pipe buffers, so always 997 * copy user pages. 998 */ 999 if (cs->req->args->user_pages) { 1000 err = fuse_copy_fill(cs); 1001 if (err) 1002 return err; 1003 } else { 1004 return fuse_ref_page(cs, page, offset, count); 1005 } 1006 } else if (!cs->len) { 1007 if (cs->move_pages && page && 1008 offset == 0 && count == PAGE_SIZE) { 1009 err = fuse_try_move_page(cs, pagep); 1010 if (err <= 0) 1011 return err; 1012 } else { 1013 err = fuse_copy_fill(cs); 1014 if (err) 1015 return err; 1016 } 1017 } 1018 if (page) { 1019 void *mapaddr = kmap_local_page(page); 1020 void *buf = mapaddr + offset; 1021 offset += fuse_copy_do(cs, &buf, &count); 1022 kunmap_local(mapaddr); 1023 } else 1024 offset += fuse_copy_do(cs, NULL, &count); 1025 } 1026 if (page && !cs->write) 1027 flush_dcache_page(page); 1028 return 0; 1029 } 1030 1031 /* Copy pages in the request to/from userspace buffer */ 1032 static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes, 1033 int zeroing) 1034 { 1035 unsigned i; 1036 struct fuse_req *req = cs->req; 1037 struct fuse_args_pages *ap = container_of(req->args, typeof(*ap), args); 1038 1039 for (i = 0; i < ap->num_folios && (nbytes || zeroing); i++) { 1040 int err; 1041 unsigned int offset = ap->descs[i].offset; 1042 unsigned int count = min(nbytes, ap->descs[i].length); 1043 struct page *orig, *pagep; 1044 1045 orig = pagep = &ap->folios[i]->page; 1046 1047 err = fuse_copy_page(cs, &pagep, offset, count, zeroing); 1048 if (err) 1049 return err; 1050 1051 nbytes -= count; 1052 1053 /* 1054 * fuse_copy_page may have moved a page from a pipe instead of 1055 * copying into our given page, so update the folios if it was 1056 * replaced. 1057 */ 1058 if (pagep != orig) 1059 ap->folios[i] = page_folio(pagep); 1060 } 1061 return 0; 1062 } 1063 1064 /* Copy a single argument in the request to/from userspace buffer */ 1065 static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size) 1066 { 1067 while (size) { 1068 if (!cs->len) { 1069 int err = fuse_copy_fill(cs); 1070 if (err) 1071 return err; 1072 } 1073 fuse_copy_do(cs, &val, &size); 1074 } 1075 return 0; 1076 } 1077 1078 /* Copy request arguments to/from userspace buffer */ 1079 int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs, 1080 unsigned argpages, struct fuse_arg *args, 1081 int zeroing) 1082 { 1083 int err = 0; 1084 unsigned i; 1085 1086 for (i = 0; !err && i < numargs; i++) { 1087 struct fuse_arg *arg = &args[i]; 1088 if (i == numargs - 1 && argpages) 1089 err = fuse_copy_pages(cs, arg->size, zeroing); 1090 else 1091 err = fuse_copy_one(cs, arg->value, arg->size); 1092 } 1093 return err; 1094 } 1095 1096 static int forget_pending(struct fuse_iqueue *fiq) 1097 { 1098 return fiq->forget_list_head.next != NULL; 1099 } 1100 1101 static int request_pending(struct fuse_iqueue *fiq) 1102 { 1103 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) || 1104 forget_pending(fiq); 1105 } 1106 1107 /* 1108 * Transfer an interrupt request to userspace 1109 * 1110 * Unlike other requests this is assembled on demand, without a need 1111 * to allocate a separate fuse_req structure. 1112 * 1113 * Called with fiq->lock held, releases it 1114 */ 1115 static int fuse_read_interrupt(struct fuse_iqueue *fiq, 1116 struct fuse_copy_state *cs, 1117 size_t nbytes, struct fuse_req *req) 1118 __releases(fiq->lock) 1119 { 1120 struct fuse_in_header ih; 1121 struct fuse_interrupt_in arg; 1122 unsigned reqsize = sizeof(ih) + sizeof(arg); 1123 int err; 1124 1125 list_del_init(&req->intr_entry); 1126 memset(&ih, 0, sizeof(ih)); 1127 memset(&arg, 0, sizeof(arg)); 1128 ih.len = reqsize; 1129 ih.opcode = FUSE_INTERRUPT; 1130 ih.unique = (req->in.h.unique | FUSE_INT_REQ_BIT); 1131 arg.unique = req->in.h.unique; 1132 1133 spin_unlock(&fiq->lock); 1134 if (nbytes < reqsize) 1135 return -EINVAL; 1136 1137 err = fuse_copy_one(cs, &ih, sizeof(ih)); 1138 if (!err) 1139 err = fuse_copy_one(cs, &arg, sizeof(arg)); 1140 fuse_copy_finish(cs); 1141 1142 return err ? err : reqsize; 1143 } 1144 1145 static struct fuse_forget_link *fuse_dequeue_forget(struct fuse_iqueue *fiq, 1146 unsigned int max, 1147 unsigned int *countp) 1148 { 1149 struct fuse_forget_link *head = fiq->forget_list_head.next; 1150 struct fuse_forget_link **newhead = &head; 1151 unsigned count; 1152 1153 for (count = 0; *newhead != NULL && count < max; count++) 1154 newhead = &(*newhead)->next; 1155 1156 fiq->forget_list_head.next = *newhead; 1157 *newhead = NULL; 1158 if (fiq->forget_list_head.next == NULL) 1159 fiq->forget_list_tail = &fiq->forget_list_head; 1160 1161 if (countp != NULL) 1162 *countp = count; 1163 1164 return head; 1165 } 1166 1167 static int fuse_read_single_forget(struct fuse_iqueue *fiq, 1168 struct fuse_copy_state *cs, 1169 size_t nbytes) 1170 __releases(fiq->lock) 1171 { 1172 int err; 1173 struct fuse_forget_link *forget = fuse_dequeue_forget(fiq, 1, NULL); 1174 struct fuse_forget_in arg = { 1175 .nlookup = forget->forget_one.nlookup, 1176 }; 1177 struct fuse_in_header ih = { 1178 .opcode = FUSE_FORGET, 1179 .nodeid = forget->forget_one.nodeid, 1180 .unique = fuse_get_unique_locked(fiq), 1181 .len = sizeof(ih) + sizeof(arg), 1182 }; 1183 1184 spin_unlock(&fiq->lock); 1185 kfree(forget); 1186 if (nbytes < ih.len) 1187 return -EINVAL; 1188 1189 err = fuse_copy_one(cs, &ih, sizeof(ih)); 1190 if (!err) 1191 err = fuse_copy_one(cs, &arg, sizeof(arg)); 1192 fuse_copy_finish(cs); 1193 1194 if (err) 1195 return err; 1196 1197 return ih.len; 1198 } 1199 1200 static int fuse_read_batch_forget(struct fuse_iqueue *fiq, 1201 struct fuse_copy_state *cs, size_t nbytes) 1202 __releases(fiq->lock) 1203 { 1204 int err; 1205 unsigned max_forgets; 1206 unsigned count; 1207 struct fuse_forget_link *head; 1208 struct fuse_batch_forget_in arg = { .count = 0 }; 1209 struct fuse_in_header ih = { 1210 .opcode = FUSE_BATCH_FORGET, 1211 .unique = fuse_get_unique_locked(fiq), 1212 .len = sizeof(ih) + sizeof(arg), 1213 }; 1214 1215 if (nbytes < ih.len) { 1216 spin_unlock(&fiq->lock); 1217 return -EINVAL; 1218 } 1219 1220 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one); 1221 head = fuse_dequeue_forget(fiq, max_forgets, &count); 1222 spin_unlock(&fiq->lock); 1223 1224 arg.count = count; 1225 ih.len += count * sizeof(struct fuse_forget_one); 1226 err = fuse_copy_one(cs, &ih, sizeof(ih)); 1227 if (!err) 1228 err = fuse_copy_one(cs, &arg, sizeof(arg)); 1229 1230 while (head) { 1231 struct fuse_forget_link *forget = head; 1232 1233 if (!err) { 1234 err = fuse_copy_one(cs, &forget->forget_one, 1235 sizeof(forget->forget_one)); 1236 } 1237 head = forget->next; 1238 kfree(forget); 1239 } 1240 1241 fuse_copy_finish(cs); 1242 1243 if (err) 1244 return err; 1245 1246 return ih.len; 1247 } 1248 1249 static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq, 1250 struct fuse_copy_state *cs, 1251 size_t nbytes) 1252 __releases(fiq->lock) 1253 { 1254 if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL) 1255 return fuse_read_single_forget(fiq, cs, nbytes); 1256 else 1257 return fuse_read_batch_forget(fiq, cs, nbytes); 1258 } 1259 1260 /* 1261 * Read a single request into the userspace filesystem's buffer. This 1262 * function waits until a request is available, then removes it from 1263 * the pending list and copies request data to userspace buffer. If 1264 * no reply is needed (FORGET) or request has been aborted or there 1265 * was an error during the copying then it's finished by calling 1266 * fuse_request_end(). Otherwise add it to the processing list, and set 1267 * the 'sent' flag. 1268 */ 1269 static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file, 1270 struct fuse_copy_state *cs, size_t nbytes) 1271 { 1272 ssize_t err; 1273 struct fuse_conn *fc = fud->fc; 1274 struct fuse_iqueue *fiq = &fc->iq; 1275 struct fuse_pqueue *fpq = &fud->pq; 1276 struct fuse_req *req; 1277 struct fuse_args *args; 1278 unsigned reqsize; 1279 unsigned int hash; 1280 1281 /* 1282 * Require sane minimum read buffer - that has capacity for fixed part 1283 * of any request header + negotiated max_write room for data. 1284 * 1285 * Historically libfuse reserves 4K for fixed header room, but e.g. 1286 * GlusterFS reserves only 80 bytes 1287 * 1288 * = `sizeof(fuse_in_header) + sizeof(fuse_write_in)` 1289 * 1290 * which is the absolute minimum any sane filesystem should be using 1291 * for header room. 1292 */ 1293 if (nbytes < max_t(size_t, FUSE_MIN_READ_BUFFER, 1294 sizeof(struct fuse_in_header) + 1295 sizeof(struct fuse_write_in) + 1296 fc->max_write)) 1297 return -EINVAL; 1298 1299 restart: 1300 for (;;) { 1301 spin_lock(&fiq->lock); 1302 if (!fiq->connected || request_pending(fiq)) 1303 break; 1304 spin_unlock(&fiq->lock); 1305 1306 if (file->f_flags & O_NONBLOCK) 1307 return -EAGAIN; 1308 err = wait_event_interruptible_exclusive(fiq->waitq, 1309 !fiq->connected || request_pending(fiq)); 1310 if (err) 1311 return err; 1312 } 1313 1314 if (!fiq->connected) { 1315 err = fc->aborted ? -ECONNABORTED : -ENODEV; 1316 goto err_unlock; 1317 } 1318 1319 if (!list_empty(&fiq->interrupts)) { 1320 req = list_entry(fiq->interrupts.next, struct fuse_req, 1321 intr_entry); 1322 return fuse_read_interrupt(fiq, cs, nbytes, req); 1323 } 1324 1325 if (forget_pending(fiq)) { 1326 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0) 1327 return fuse_read_forget(fc, fiq, cs, nbytes); 1328 1329 if (fiq->forget_batch <= -8) 1330 fiq->forget_batch = 16; 1331 } 1332 1333 req = list_entry(fiq->pending.next, struct fuse_req, list); 1334 clear_bit(FR_PENDING, &req->flags); 1335 list_del_init(&req->list); 1336 spin_unlock(&fiq->lock); 1337 1338 args = req->args; 1339 reqsize = req->in.h.len; 1340 1341 /* If request is too large, reply with an error and restart the read */ 1342 if (nbytes < reqsize) { 1343 req->out.h.error = -EIO; 1344 /* SETXATTR is special, since it may contain too large data */ 1345 if (args->opcode == FUSE_SETXATTR) 1346 req->out.h.error = -E2BIG; 1347 fuse_request_end(req); 1348 goto restart; 1349 } 1350 spin_lock(&fpq->lock); 1351 /* 1352 * Must not put request on fpq->io queue after having been shut down by 1353 * fuse_abort_conn() 1354 */ 1355 if (!fpq->connected) { 1356 req->out.h.error = err = -ECONNABORTED; 1357 goto out_end; 1358 1359 } 1360 list_add(&req->list, &fpq->io); 1361 spin_unlock(&fpq->lock); 1362 cs->req = req; 1363 err = fuse_copy_one(cs, &req->in.h, sizeof(req->in.h)); 1364 if (!err) 1365 err = fuse_copy_args(cs, args->in_numargs, args->in_pages, 1366 (struct fuse_arg *) args->in_args, 0); 1367 fuse_copy_finish(cs); 1368 spin_lock(&fpq->lock); 1369 clear_bit(FR_LOCKED, &req->flags); 1370 if (!fpq->connected) { 1371 err = fc->aborted ? -ECONNABORTED : -ENODEV; 1372 goto out_end; 1373 } 1374 if (err) { 1375 req->out.h.error = -EIO; 1376 goto out_end; 1377 } 1378 if (!test_bit(FR_ISREPLY, &req->flags)) { 1379 err = reqsize; 1380 goto out_end; 1381 } 1382 hash = fuse_req_hash(req->in.h.unique); 1383 list_move_tail(&req->list, &fpq->processing[hash]); 1384 __fuse_get_request(req); 1385 set_bit(FR_SENT, &req->flags); 1386 spin_unlock(&fpq->lock); 1387 /* matches barrier in request_wait_answer() */ 1388 smp_mb__after_atomic(); 1389 if (test_bit(FR_INTERRUPTED, &req->flags)) 1390 queue_interrupt(req); 1391 fuse_put_request(req); 1392 1393 return reqsize; 1394 1395 out_end: 1396 if (!test_bit(FR_PRIVATE, &req->flags)) 1397 list_del_init(&req->list); 1398 spin_unlock(&fpq->lock); 1399 fuse_request_end(req); 1400 return err; 1401 1402 err_unlock: 1403 spin_unlock(&fiq->lock); 1404 return err; 1405 } 1406 1407 static int fuse_dev_open(struct inode *inode, struct file *file) 1408 { 1409 /* 1410 * The fuse device's file's private_data is used to hold 1411 * the fuse_conn(ection) when it is mounted, and is used to 1412 * keep track of whether the file has been mounted already. 1413 */ 1414 file->private_data = NULL; 1415 return 0; 1416 } 1417 1418 static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to) 1419 { 1420 struct fuse_copy_state cs; 1421 struct file *file = iocb->ki_filp; 1422 struct fuse_dev *fud = fuse_get_dev(file); 1423 1424 if (!fud) 1425 return -EPERM; 1426 1427 if (!user_backed_iter(to)) 1428 return -EINVAL; 1429 1430 fuse_copy_init(&cs, 1, to); 1431 1432 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to)); 1433 } 1434 1435 static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos, 1436 struct pipe_inode_info *pipe, 1437 size_t len, unsigned int flags) 1438 { 1439 int total, ret; 1440 int page_nr = 0; 1441 struct pipe_buffer *bufs; 1442 struct fuse_copy_state cs; 1443 struct fuse_dev *fud = fuse_get_dev(in); 1444 1445 if (!fud) 1446 return -EPERM; 1447 1448 bufs = kvmalloc_array(pipe->max_usage, sizeof(struct pipe_buffer), 1449 GFP_KERNEL); 1450 if (!bufs) 1451 return -ENOMEM; 1452 1453 fuse_copy_init(&cs, 1, NULL); 1454 cs.pipebufs = bufs; 1455 cs.pipe = pipe; 1456 ret = fuse_dev_do_read(fud, in, &cs, len); 1457 if (ret < 0) 1458 goto out; 1459 1460 if (pipe_buf_usage(pipe) + cs.nr_segs > pipe->max_usage) { 1461 ret = -EIO; 1462 goto out; 1463 } 1464 1465 for (ret = total = 0; page_nr < cs.nr_segs; total += ret) { 1466 /* 1467 * Need to be careful about this. Having buf->ops in module 1468 * code can Oops if the buffer persists after module unload. 1469 */ 1470 bufs[page_nr].ops = &nosteal_pipe_buf_ops; 1471 bufs[page_nr].flags = 0; 1472 ret = add_to_pipe(pipe, &bufs[page_nr++]); 1473 if (unlikely(ret < 0)) 1474 break; 1475 } 1476 if (total) 1477 ret = total; 1478 out: 1479 for (; page_nr < cs.nr_segs; page_nr++) 1480 put_page(bufs[page_nr].page); 1481 1482 kvfree(bufs); 1483 return ret; 1484 } 1485 1486 static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size, 1487 struct fuse_copy_state *cs) 1488 { 1489 struct fuse_notify_poll_wakeup_out outarg; 1490 int err = -EINVAL; 1491 1492 if (size != sizeof(outarg)) 1493 goto err; 1494 1495 err = fuse_copy_one(cs, &outarg, sizeof(outarg)); 1496 if (err) 1497 goto err; 1498 1499 fuse_copy_finish(cs); 1500 return fuse_notify_poll_wakeup(fc, &outarg); 1501 1502 err: 1503 fuse_copy_finish(cs); 1504 return err; 1505 } 1506 1507 static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size, 1508 struct fuse_copy_state *cs) 1509 { 1510 struct fuse_notify_inval_inode_out outarg; 1511 int err = -EINVAL; 1512 1513 if (size != sizeof(outarg)) 1514 goto err; 1515 1516 err = fuse_copy_one(cs, &outarg, sizeof(outarg)); 1517 if (err) 1518 goto err; 1519 fuse_copy_finish(cs); 1520 1521 down_read(&fc->killsb); 1522 err = fuse_reverse_inval_inode(fc, outarg.ino, 1523 outarg.off, outarg.len); 1524 up_read(&fc->killsb); 1525 return err; 1526 1527 err: 1528 fuse_copy_finish(cs); 1529 return err; 1530 } 1531 1532 static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size, 1533 struct fuse_copy_state *cs) 1534 { 1535 struct fuse_notify_inval_entry_out outarg; 1536 int err = -ENOMEM; 1537 char *buf; 1538 struct qstr name; 1539 1540 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL); 1541 if (!buf) 1542 goto err; 1543 1544 err = -EINVAL; 1545 if (size < sizeof(outarg)) 1546 goto err; 1547 1548 err = fuse_copy_one(cs, &outarg, sizeof(outarg)); 1549 if (err) 1550 goto err; 1551 1552 err = -ENAMETOOLONG; 1553 if (outarg.namelen > FUSE_NAME_MAX) 1554 goto err; 1555 1556 err = -EINVAL; 1557 if (size != sizeof(outarg) + outarg.namelen + 1) 1558 goto err; 1559 1560 name.name = buf; 1561 name.len = outarg.namelen; 1562 err = fuse_copy_one(cs, buf, outarg.namelen + 1); 1563 if (err) 1564 goto err; 1565 fuse_copy_finish(cs); 1566 buf[outarg.namelen] = 0; 1567 1568 down_read(&fc->killsb); 1569 err = fuse_reverse_inval_entry(fc, outarg.parent, 0, &name, outarg.flags); 1570 up_read(&fc->killsb); 1571 kfree(buf); 1572 return err; 1573 1574 err: 1575 kfree(buf); 1576 fuse_copy_finish(cs); 1577 return err; 1578 } 1579 1580 static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size, 1581 struct fuse_copy_state *cs) 1582 { 1583 struct fuse_notify_delete_out outarg; 1584 int err = -ENOMEM; 1585 char *buf; 1586 struct qstr name; 1587 1588 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL); 1589 if (!buf) 1590 goto err; 1591 1592 err = -EINVAL; 1593 if (size < sizeof(outarg)) 1594 goto err; 1595 1596 err = fuse_copy_one(cs, &outarg, sizeof(outarg)); 1597 if (err) 1598 goto err; 1599 1600 err = -ENAMETOOLONG; 1601 if (outarg.namelen > FUSE_NAME_MAX) 1602 goto err; 1603 1604 err = -EINVAL; 1605 if (size != sizeof(outarg) + outarg.namelen + 1) 1606 goto err; 1607 1608 name.name = buf; 1609 name.len = outarg.namelen; 1610 err = fuse_copy_one(cs, buf, outarg.namelen + 1); 1611 if (err) 1612 goto err; 1613 fuse_copy_finish(cs); 1614 buf[outarg.namelen] = 0; 1615 1616 down_read(&fc->killsb); 1617 err = fuse_reverse_inval_entry(fc, outarg.parent, outarg.child, &name, 0); 1618 up_read(&fc->killsb); 1619 kfree(buf); 1620 return err; 1621 1622 err: 1623 kfree(buf); 1624 fuse_copy_finish(cs); 1625 return err; 1626 } 1627 1628 static int fuse_notify_store(struct fuse_conn *fc, unsigned int size, 1629 struct fuse_copy_state *cs) 1630 { 1631 struct fuse_notify_store_out outarg; 1632 struct inode *inode; 1633 struct address_space *mapping; 1634 u64 nodeid; 1635 int err; 1636 pgoff_t index; 1637 unsigned int offset; 1638 unsigned int num; 1639 loff_t file_size; 1640 loff_t end; 1641 1642 err = -EINVAL; 1643 if (size < sizeof(outarg)) 1644 goto out_finish; 1645 1646 err = fuse_copy_one(cs, &outarg, sizeof(outarg)); 1647 if (err) 1648 goto out_finish; 1649 1650 err = -EINVAL; 1651 if (size - sizeof(outarg) != outarg.size) 1652 goto out_finish; 1653 1654 nodeid = outarg.nodeid; 1655 1656 down_read(&fc->killsb); 1657 1658 err = -ENOENT; 1659 inode = fuse_ilookup(fc, nodeid, NULL); 1660 if (!inode) 1661 goto out_up_killsb; 1662 1663 mapping = inode->i_mapping; 1664 index = outarg.offset >> PAGE_SHIFT; 1665 offset = outarg.offset & ~PAGE_MASK; 1666 file_size = i_size_read(inode); 1667 end = outarg.offset + outarg.size; 1668 if (end > file_size) { 1669 file_size = end; 1670 fuse_write_update_attr(inode, file_size, outarg.size); 1671 } 1672 1673 num = outarg.size; 1674 while (num) { 1675 struct folio *folio; 1676 struct page *page; 1677 unsigned int this_num; 1678 1679 folio = filemap_grab_folio(mapping, index); 1680 err = PTR_ERR(folio); 1681 if (IS_ERR(folio)) 1682 goto out_iput; 1683 1684 page = &folio->page; 1685 this_num = min_t(unsigned, num, folio_size(folio) - offset); 1686 err = fuse_copy_page(cs, &page, offset, this_num, 0); 1687 if (!folio_test_uptodate(folio) && !err && offset == 0 && 1688 (this_num == folio_size(folio) || file_size == end)) { 1689 folio_zero_segment(folio, this_num, folio_size(folio)); 1690 folio_mark_uptodate(folio); 1691 } 1692 folio_unlock(folio); 1693 folio_put(folio); 1694 1695 if (err) 1696 goto out_iput; 1697 1698 num -= this_num; 1699 offset = 0; 1700 index++; 1701 } 1702 1703 err = 0; 1704 1705 out_iput: 1706 iput(inode); 1707 out_up_killsb: 1708 up_read(&fc->killsb); 1709 out_finish: 1710 fuse_copy_finish(cs); 1711 return err; 1712 } 1713 1714 struct fuse_retrieve_args { 1715 struct fuse_args_pages ap; 1716 struct fuse_notify_retrieve_in inarg; 1717 }; 1718 1719 static void fuse_retrieve_end(struct fuse_mount *fm, struct fuse_args *args, 1720 int error) 1721 { 1722 struct fuse_retrieve_args *ra = 1723 container_of(args, typeof(*ra), ap.args); 1724 1725 release_pages(ra->ap.folios, ra->ap.num_folios); 1726 kfree(ra); 1727 } 1728 1729 static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode, 1730 struct fuse_notify_retrieve_out *outarg) 1731 { 1732 int err; 1733 struct address_space *mapping = inode->i_mapping; 1734 pgoff_t index; 1735 loff_t file_size; 1736 unsigned int num; 1737 unsigned int offset; 1738 size_t total_len = 0; 1739 unsigned int num_pages, cur_pages = 0; 1740 struct fuse_conn *fc = fm->fc; 1741 struct fuse_retrieve_args *ra; 1742 size_t args_size = sizeof(*ra); 1743 struct fuse_args_pages *ap; 1744 struct fuse_args *args; 1745 1746 offset = outarg->offset & ~PAGE_MASK; 1747 file_size = i_size_read(inode); 1748 1749 num = min(outarg->size, fc->max_write); 1750 if (outarg->offset > file_size) 1751 num = 0; 1752 else if (outarg->offset + num > file_size) 1753 num = file_size - outarg->offset; 1754 1755 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; 1756 num_pages = min(num_pages, fc->max_pages); 1757 1758 args_size += num_pages * (sizeof(ap->folios[0]) + sizeof(ap->descs[0])); 1759 1760 ra = kzalloc(args_size, GFP_KERNEL); 1761 if (!ra) 1762 return -ENOMEM; 1763 1764 ap = &ra->ap; 1765 ap->folios = (void *) (ra + 1); 1766 ap->descs = (void *) (ap->folios + num_pages); 1767 1768 args = &ap->args; 1769 args->nodeid = outarg->nodeid; 1770 args->opcode = FUSE_NOTIFY_REPLY; 1771 args->in_numargs = 3; 1772 args->in_pages = true; 1773 args->end = fuse_retrieve_end; 1774 1775 index = outarg->offset >> PAGE_SHIFT; 1776 1777 while (num && cur_pages < num_pages) { 1778 struct folio *folio; 1779 unsigned int this_num; 1780 1781 folio = filemap_get_folio(mapping, index); 1782 if (IS_ERR(folio)) 1783 break; 1784 1785 this_num = min_t(unsigned, num, PAGE_SIZE - offset); 1786 ap->folios[ap->num_folios] = folio; 1787 ap->descs[ap->num_folios].offset = offset; 1788 ap->descs[ap->num_folios].length = this_num; 1789 ap->num_folios++; 1790 cur_pages++; 1791 1792 offset = 0; 1793 num -= this_num; 1794 total_len += this_num; 1795 index++; 1796 } 1797 ra->inarg.offset = outarg->offset; 1798 ra->inarg.size = total_len; 1799 fuse_set_zero_arg0(args); 1800 args->in_args[1].size = sizeof(ra->inarg); 1801 args->in_args[1].value = &ra->inarg; 1802 args->in_args[2].size = total_len; 1803 1804 err = fuse_simple_notify_reply(fm, args, outarg->notify_unique); 1805 if (err) 1806 fuse_retrieve_end(fm, args, err); 1807 1808 return err; 1809 } 1810 1811 static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size, 1812 struct fuse_copy_state *cs) 1813 { 1814 struct fuse_notify_retrieve_out outarg; 1815 struct fuse_mount *fm; 1816 struct inode *inode; 1817 u64 nodeid; 1818 int err; 1819 1820 err = -EINVAL; 1821 if (size != sizeof(outarg)) 1822 goto copy_finish; 1823 1824 err = fuse_copy_one(cs, &outarg, sizeof(outarg)); 1825 if (err) 1826 goto copy_finish; 1827 1828 fuse_copy_finish(cs); 1829 1830 down_read(&fc->killsb); 1831 err = -ENOENT; 1832 nodeid = outarg.nodeid; 1833 1834 inode = fuse_ilookup(fc, nodeid, &fm); 1835 if (inode) { 1836 err = fuse_retrieve(fm, inode, &outarg); 1837 iput(inode); 1838 } 1839 up_read(&fc->killsb); 1840 1841 return err; 1842 1843 copy_finish: 1844 fuse_copy_finish(cs); 1845 return err; 1846 } 1847 1848 /* 1849 * Resending all processing queue requests. 1850 * 1851 * During a FUSE daemon panics and failover, it is possible for some inflight 1852 * requests to be lost and never returned. As a result, applications awaiting 1853 * replies would become stuck forever. To address this, we can use notification 1854 * to trigger resending of these pending requests to the FUSE daemon, ensuring 1855 * they are properly processed again. 1856 * 1857 * Please note that this strategy is applicable only to idempotent requests or 1858 * if the FUSE daemon takes careful measures to avoid processing duplicated 1859 * non-idempotent requests. 1860 */ 1861 static void fuse_resend(struct fuse_conn *fc) 1862 { 1863 struct fuse_dev *fud; 1864 struct fuse_req *req, *next; 1865 struct fuse_iqueue *fiq = &fc->iq; 1866 LIST_HEAD(to_queue); 1867 unsigned int i; 1868 1869 spin_lock(&fc->lock); 1870 if (!fc->connected) { 1871 spin_unlock(&fc->lock); 1872 return; 1873 } 1874 1875 list_for_each_entry(fud, &fc->devices, entry) { 1876 struct fuse_pqueue *fpq = &fud->pq; 1877 1878 spin_lock(&fpq->lock); 1879 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++) 1880 list_splice_tail_init(&fpq->processing[i], &to_queue); 1881 spin_unlock(&fpq->lock); 1882 } 1883 spin_unlock(&fc->lock); 1884 1885 list_for_each_entry_safe(req, next, &to_queue, list) { 1886 set_bit(FR_PENDING, &req->flags); 1887 clear_bit(FR_SENT, &req->flags); 1888 /* mark the request as resend request */ 1889 req->in.h.unique |= FUSE_UNIQUE_RESEND; 1890 } 1891 1892 spin_lock(&fiq->lock); 1893 if (!fiq->connected) { 1894 spin_unlock(&fiq->lock); 1895 list_for_each_entry(req, &to_queue, list) 1896 clear_bit(FR_PENDING, &req->flags); 1897 fuse_dev_end_requests(&to_queue); 1898 return; 1899 } 1900 /* iq and pq requests are both oldest to newest */ 1901 list_splice(&to_queue, &fiq->pending); 1902 fuse_dev_wake_and_unlock(fiq); 1903 } 1904 1905 static int fuse_notify_resend(struct fuse_conn *fc) 1906 { 1907 fuse_resend(fc); 1908 return 0; 1909 } 1910 1911 static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code, 1912 unsigned int size, struct fuse_copy_state *cs) 1913 { 1914 /* Don't try to move pages (yet) */ 1915 cs->move_pages = 0; 1916 1917 switch (code) { 1918 case FUSE_NOTIFY_POLL: 1919 return fuse_notify_poll(fc, size, cs); 1920 1921 case FUSE_NOTIFY_INVAL_INODE: 1922 return fuse_notify_inval_inode(fc, size, cs); 1923 1924 case FUSE_NOTIFY_INVAL_ENTRY: 1925 return fuse_notify_inval_entry(fc, size, cs); 1926 1927 case FUSE_NOTIFY_STORE: 1928 return fuse_notify_store(fc, size, cs); 1929 1930 case FUSE_NOTIFY_RETRIEVE: 1931 return fuse_notify_retrieve(fc, size, cs); 1932 1933 case FUSE_NOTIFY_DELETE: 1934 return fuse_notify_delete(fc, size, cs); 1935 1936 case FUSE_NOTIFY_RESEND: 1937 return fuse_notify_resend(fc); 1938 1939 default: 1940 fuse_copy_finish(cs); 1941 return -EINVAL; 1942 } 1943 } 1944 1945 /* Look up request on processing list by unique ID */ 1946 struct fuse_req *fuse_request_find(struct fuse_pqueue *fpq, u64 unique) 1947 { 1948 unsigned int hash = fuse_req_hash(unique); 1949 struct fuse_req *req; 1950 1951 list_for_each_entry(req, &fpq->processing[hash], list) { 1952 if (req->in.h.unique == unique) 1953 return req; 1954 } 1955 return NULL; 1956 } 1957 1958 int fuse_copy_out_args(struct fuse_copy_state *cs, struct fuse_args *args, 1959 unsigned nbytes) 1960 { 1961 1962 unsigned int reqsize = 0; 1963 1964 /* 1965 * Uring has all headers separated from args - args is payload only 1966 */ 1967 if (!cs->is_uring) 1968 reqsize = sizeof(struct fuse_out_header); 1969 1970 reqsize += fuse_len_args(args->out_numargs, args->out_args); 1971 1972 if (reqsize < nbytes || (reqsize > nbytes && !args->out_argvar)) 1973 return -EINVAL; 1974 else if (reqsize > nbytes) { 1975 struct fuse_arg *lastarg = &args->out_args[args->out_numargs-1]; 1976 unsigned diffsize = reqsize - nbytes; 1977 1978 if (diffsize > lastarg->size) 1979 return -EINVAL; 1980 lastarg->size -= diffsize; 1981 } 1982 return fuse_copy_args(cs, args->out_numargs, args->out_pages, 1983 args->out_args, args->page_zeroing); 1984 } 1985 1986 /* 1987 * Write a single reply to a request. First the header is copied from 1988 * the write buffer. The request is then searched on the processing 1989 * list by the unique ID found in the header. If found, then remove 1990 * it from the list and copy the rest of the buffer to the request. 1991 * The request is finished by calling fuse_request_end(). 1992 */ 1993 static ssize_t fuse_dev_do_write(struct fuse_dev *fud, 1994 struct fuse_copy_state *cs, size_t nbytes) 1995 { 1996 int err; 1997 struct fuse_conn *fc = fud->fc; 1998 struct fuse_pqueue *fpq = &fud->pq; 1999 struct fuse_req *req; 2000 struct fuse_out_header oh; 2001 2002 err = -EINVAL; 2003 if (nbytes < sizeof(struct fuse_out_header)) 2004 goto out; 2005 2006 err = fuse_copy_one(cs, &oh, sizeof(oh)); 2007 if (err) 2008 goto copy_finish; 2009 2010 err = -EINVAL; 2011 if (oh.len != nbytes) 2012 goto copy_finish; 2013 2014 /* 2015 * Zero oh.unique indicates unsolicited notification message 2016 * and error contains notification code. 2017 */ 2018 if (!oh.unique) { 2019 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs); 2020 goto out; 2021 } 2022 2023 err = -EINVAL; 2024 if (oh.error <= -512 || oh.error > 0) 2025 goto copy_finish; 2026 2027 spin_lock(&fpq->lock); 2028 req = NULL; 2029 if (fpq->connected) 2030 req = fuse_request_find(fpq, oh.unique & ~FUSE_INT_REQ_BIT); 2031 2032 err = -ENOENT; 2033 if (!req) { 2034 spin_unlock(&fpq->lock); 2035 goto copy_finish; 2036 } 2037 2038 /* Is it an interrupt reply ID? */ 2039 if (oh.unique & FUSE_INT_REQ_BIT) { 2040 __fuse_get_request(req); 2041 spin_unlock(&fpq->lock); 2042 2043 err = 0; 2044 if (nbytes != sizeof(struct fuse_out_header)) 2045 err = -EINVAL; 2046 else if (oh.error == -ENOSYS) 2047 fc->no_interrupt = 1; 2048 else if (oh.error == -EAGAIN) 2049 err = queue_interrupt(req); 2050 2051 fuse_put_request(req); 2052 2053 goto copy_finish; 2054 } 2055 2056 clear_bit(FR_SENT, &req->flags); 2057 list_move(&req->list, &fpq->io); 2058 req->out.h = oh; 2059 set_bit(FR_LOCKED, &req->flags); 2060 spin_unlock(&fpq->lock); 2061 cs->req = req; 2062 if (!req->args->page_replace) 2063 cs->move_pages = 0; 2064 2065 if (oh.error) 2066 err = nbytes != sizeof(oh) ? -EINVAL : 0; 2067 else 2068 err = fuse_copy_out_args(cs, req->args, nbytes); 2069 fuse_copy_finish(cs); 2070 2071 spin_lock(&fpq->lock); 2072 clear_bit(FR_LOCKED, &req->flags); 2073 if (!fpq->connected) 2074 err = -ENOENT; 2075 else if (err) 2076 req->out.h.error = -EIO; 2077 if (!test_bit(FR_PRIVATE, &req->flags)) 2078 list_del_init(&req->list); 2079 spin_unlock(&fpq->lock); 2080 2081 fuse_request_end(req); 2082 out: 2083 return err ? err : nbytes; 2084 2085 copy_finish: 2086 fuse_copy_finish(cs); 2087 goto out; 2088 } 2089 2090 static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from) 2091 { 2092 struct fuse_copy_state cs; 2093 struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp); 2094 2095 if (!fud) 2096 return -EPERM; 2097 2098 if (!user_backed_iter(from)) 2099 return -EINVAL; 2100 2101 fuse_copy_init(&cs, 0, from); 2102 2103 return fuse_dev_do_write(fud, &cs, iov_iter_count(from)); 2104 } 2105 2106 static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe, 2107 struct file *out, loff_t *ppos, 2108 size_t len, unsigned int flags) 2109 { 2110 unsigned int head, tail, count; 2111 unsigned nbuf; 2112 unsigned idx; 2113 struct pipe_buffer *bufs; 2114 struct fuse_copy_state cs; 2115 struct fuse_dev *fud; 2116 size_t rem; 2117 ssize_t ret; 2118 2119 fud = fuse_get_dev(out); 2120 if (!fud) 2121 return -EPERM; 2122 2123 pipe_lock(pipe); 2124 2125 head = pipe->head; 2126 tail = pipe->tail; 2127 count = pipe_occupancy(head, tail); 2128 2129 bufs = kvmalloc_array(count, sizeof(struct pipe_buffer), GFP_KERNEL); 2130 if (!bufs) { 2131 pipe_unlock(pipe); 2132 return -ENOMEM; 2133 } 2134 2135 nbuf = 0; 2136 rem = 0; 2137 for (idx = tail; !pipe_empty(head, idx) && rem < len; idx++) 2138 rem += pipe_buf(pipe, idx)->len; 2139 2140 ret = -EINVAL; 2141 if (rem < len) 2142 goto out_free; 2143 2144 rem = len; 2145 while (rem) { 2146 struct pipe_buffer *ibuf; 2147 struct pipe_buffer *obuf; 2148 2149 if (WARN_ON(nbuf >= count || pipe_empty(head, tail))) 2150 goto out_free; 2151 2152 ibuf = pipe_buf(pipe, tail); 2153 obuf = &bufs[nbuf]; 2154 2155 if (rem >= ibuf->len) { 2156 *obuf = *ibuf; 2157 ibuf->ops = NULL; 2158 tail++; 2159 pipe->tail = tail; 2160 } else { 2161 if (!pipe_buf_get(pipe, ibuf)) 2162 goto out_free; 2163 2164 *obuf = *ibuf; 2165 obuf->flags &= ~PIPE_BUF_FLAG_GIFT; 2166 obuf->len = rem; 2167 ibuf->offset += obuf->len; 2168 ibuf->len -= obuf->len; 2169 } 2170 nbuf++; 2171 rem -= obuf->len; 2172 } 2173 pipe_unlock(pipe); 2174 2175 fuse_copy_init(&cs, 0, NULL); 2176 cs.pipebufs = bufs; 2177 cs.nr_segs = nbuf; 2178 cs.pipe = pipe; 2179 2180 if (flags & SPLICE_F_MOVE) 2181 cs.move_pages = 1; 2182 2183 ret = fuse_dev_do_write(fud, &cs, len); 2184 2185 pipe_lock(pipe); 2186 out_free: 2187 for (idx = 0; idx < nbuf; idx++) { 2188 struct pipe_buffer *buf = &bufs[idx]; 2189 2190 if (buf->ops) 2191 pipe_buf_release(pipe, buf); 2192 } 2193 pipe_unlock(pipe); 2194 2195 kvfree(bufs); 2196 return ret; 2197 } 2198 2199 static __poll_t fuse_dev_poll(struct file *file, poll_table *wait) 2200 { 2201 __poll_t mask = EPOLLOUT | EPOLLWRNORM; 2202 struct fuse_iqueue *fiq; 2203 struct fuse_dev *fud = fuse_get_dev(file); 2204 2205 if (!fud) 2206 return EPOLLERR; 2207 2208 fiq = &fud->fc->iq; 2209 poll_wait(file, &fiq->waitq, wait); 2210 2211 spin_lock(&fiq->lock); 2212 if (!fiq->connected) 2213 mask = EPOLLERR; 2214 else if (request_pending(fiq)) 2215 mask |= EPOLLIN | EPOLLRDNORM; 2216 spin_unlock(&fiq->lock); 2217 2218 return mask; 2219 } 2220 2221 /* Abort all requests on the given list (pending or processing) */ 2222 void fuse_dev_end_requests(struct list_head *head) 2223 { 2224 while (!list_empty(head)) { 2225 struct fuse_req *req; 2226 req = list_entry(head->next, struct fuse_req, list); 2227 req->out.h.error = -ECONNABORTED; 2228 clear_bit(FR_SENT, &req->flags); 2229 list_del_init(&req->list); 2230 fuse_request_end(req); 2231 } 2232 } 2233 2234 static void end_polls(struct fuse_conn *fc) 2235 { 2236 struct rb_node *p; 2237 2238 p = rb_first(&fc->polled_files); 2239 2240 while (p) { 2241 struct fuse_file *ff; 2242 ff = rb_entry(p, struct fuse_file, polled_node); 2243 wake_up_interruptible_all(&ff->poll_wait); 2244 2245 p = rb_next(p); 2246 } 2247 } 2248 2249 /* 2250 * Abort all requests. 2251 * 2252 * Emergency exit in case of a malicious or accidental deadlock, or just a hung 2253 * filesystem. 2254 * 2255 * The same effect is usually achievable through killing the filesystem daemon 2256 * and all users of the filesystem. The exception is the combination of an 2257 * asynchronous request and the tricky deadlock (see 2258 * Documentation/filesystems/fuse.rst). 2259 * 2260 * Aborting requests under I/O goes as follows: 1: Separate out unlocked 2261 * requests, they should be finished off immediately. Locked requests will be 2262 * finished after unlock; see unlock_request(). 2: Finish off the unlocked 2263 * requests. It is possible that some request will finish before we can. This 2264 * is OK, the request will in that case be removed from the list before we touch 2265 * it. 2266 */ 2267 void fuse_abort_conn(struct fuse_conn *fc) 2268 { 2269 struct fuse_iqueue *fiq = &fc->iq; 2270 2271 spin_lock(&fc->lock); 2272 if (fc->connected) { 2273 struct fuse_dev *fud; 2274 struct fuse_req *req, *next; 2275 LIST_HEAD(to_end); 2276 unsigned int i; 2277 2278 /* Background queuing checks fc->connected under bg_lock */ 2279 spin_lock(&fc->bg_lock); 2280 fc->connected = 0; 2281 spin_unlock(&fc->bg_lock); 2282 2283 fuse_set_initialized(fc); 2284 list_for_each_entry(fud, &fc->devices, entry) { 2285 struct fuse_pqueue *fpq = &fud->pq; 2286 2287 spin_lock(&fpq->lock); 2288 fpq->connected = 0; 2289 list_for_each_entry_safe(req, next, &fpq->io, list) { 2290 req->out.h.error = -ECONNABORTED; 2291 spin_lock(&req->waitq.lock); 2292 set_bit(FR_ABORTED, &req->flags); 2293 if (!test_bit(FR_LOCKED, &req->flags)) { 2294 set_bit(FR_PRIVATE, &req->flags); 2295 __fuse_get_request(req); 2296 list_move(&req->list, &to_end); 2297 } 2298 spin_unlock(&req->waitq.lock); 2299 } 2300 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++) 2301 list_splice_tail_init(&fpq->processing[i], 2302 &to_end); 2303 spin_unlock(&fpq->lock); 2304 } 2305 spin_lock(&fc->bg_lock); 2306 fc->blocked = 0; 2307 fc->max_background = UINT_MAX; 2308 flush_bg_queue(fc); 2309 spin_unlock(&fc->bg_lock); 2310 2311 spin_lock(&fiq->lock); 2312 fiq->connected = 0; 2313 list_for_each_entry(req, &fiq->pending, list) 2314 clear_bit(FR_PENDING, &req->flags); 2315 list_splice_tail_init(&fiq->pending, &to_end); 2316 while (forget_pending(fiq)) 2317 kfree(fuse_dequeue_forget(fiq, 1, NULL)); 2318 wake_up_all(&fiq->waitq); 2319 spin_unlock(&fiq->lock); 2320 kill_fasync(&fiq->fasync, SIGIO, POLL_IN); 2321 end_polls(fc); 2322 wake_up_all(&fc->blocked_waitq); 2323 spin_unlock(&fc->lock); 2324 2325 fuse_dev_end_requests(&to_end); 2326 2327 /* 2328 * fc->lock must not be taken to avoid conflicts with io-uring 2329 * locks 2330 */ 2331 fuse_uring_abort(fc); 2332 } else { 2333 spin_unlock(&fc->lock); 2334 } 2335 } 2336 EXPORT_SYMBOL_GPL(fuse_abort_conn); 2337 2338 void fuse_wait_aborted(struct fuse_conn *fc) 2339 { 2340 /* matches implicit memory barrier in fuse_drop_waiting() */ 2341 smp_mb(); 2342 wait_event(fc->blocked_waitq, atomic_read(&fc->num_waiting) == 0); 2343 2344 fuse_uring_wait_stopped_queues(fc); 2345 } 2346 2347 int fuse_dev_release(struct inode *inode, struct file *file) 2348 { 2349 struct fuse_dev *fud = fuse_get_dev(file); 2350 2351 if (fud) { 2352 struct fuse_conn *fc = fud->fc; 2353 struct fuse_pqueue *fpq = &fud->pq; 2354 LIST_HEAD(to_end); 2355 unsigned int i; 2356 2357 spin_lock(&fpq->lock); 2358 WARN_ON(!list_empty(&fpq->io)); 2359 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++) 2360 list_splice_init(&fpq->processing[i], &to_end); 2361 spin_unlock(&fpq->lock); 2362 2363 fuse_dev_end_requests(&to_end); 2364 2365 /* Are we the last open device? */ 2366 if (atomic_dec_and_test(&fc->dev_count)) { 2367 WARN_ON(fc->iq.fasync != NULL); 2368 fuse_abort_conn(fc); 2369 } 2370 fuse_dev_free(fud); 2371 } 2372 return 0; 2373 } 2374 EXPORT_SYMBOL_GPL(fuse_dev_release); 2375 2376 static int fuse_dev_fasync(int fd, struct file *file, int on) 2377 { 2378 struct fuse_dev *fud = fuse_get_dev(file); 2379 2380 if (!fud) 2381 return -EPERM; 2382 2383 /* No locking - fasync_helper does its own locking */ 2384 return fasync_helper(fd, file, on, &fud->fc->iq.fasync); 2385 } 2386 2387 static int fuse_device_clone(struct fuse_conn *fc, struct file *new) 2388 { 2389 struct fuse_dev *fud; 2390 2391 if (new->private_data) 2392 return -EINVAL; 2393 2394 fud = fuse_dev_alloc_install(fc); 2395 if (!fud) 2396 return -ENOMEM; 2397 2398 new->private_data = fud; 2399 atomic_inc(&fc->dev_count); 2400 2401 return 0; 2402 } 2403 2404 static long fuse_dev_ioctl_clone(struct file *file, __u32 __user *argp) 2405 { 2406 int res; 2407 int oldfd; 2408 struct fuse_dev *fud = NULL; 2409 2410 if (get_user(oldfd, argp)) 2411 return -EFAULT; 2412 2413 CLASS(fd, f)(oldfd); 2414 if (fd_empty(f)) 2415 return -EINVAL; 2416 2417 /* 2418 * Check against file->f_op because CUSE 2419 * uses the same ioctl handler. 2420 */ 2421 if (fd_file(f)->f_op == file->f_op) 2422 fud = fuse_get_dev(fd_file(f)); 2423 2424 res = -EINVAL; 2425 if (fud) { 2426 mutex_lock(&fuse_mutex); 2427 res = fuse_device_clone(fud->fc, file); 2428 mutex_unlock(&fuse_mutex); 2429 } 2430 2431 return res; 2432 } 2433 2434 static long fuse_dev_ioctl_backing_open(struct file *file, 2435 struct fuse_backing_map __user *argp) 2436 { 2437 struct fuse_dev *fud = fuse_get_dev(file); 2438 struct fuse_backing_map map; 2439 2440 if (!fud) 2441 return -EPERM; 2442 2443 if (!IS_ENABLED(CONFIG_FUSE_PASSTHROUGH)) 2444 return -EOPNOTSUPP; 2445 2446 if (copy_from_user(&map, argp, sizeof(map))) 2447 return -EFAULT; 2448 2449 return fuse_backing_open(fud->fc, &map); 2450 } 2451 2452 static long fuse_dev_ioctl_backing_close(struct file *file, __u32 __user *argp) 2453 { 2454 struct fuse_dev *fud = fuse_get_dev(file); 2455 int backing_id; 2456 2457 if (!fud) 2458 return -EPERM; 2459 2460 if (!IS_ENABLED(CONFIG_FUSE_PASSTHROUGH)) 2461 return -EOPNOTSUPP; 2462 2463 if (get_user(backing_id, argp)) 2464 return -EFAULT; 2465 2466 return fuse_backing_close(fud->fc, backing_id); 2467 } 2468 2469 static long fuse_dev_ioctl(struct file *file, unsigned int cmd, 2470 unsigned long arg) 2471 { 2472 void __user *argp = (void __user *)arg; 2473 2474 switch (cmd) { 2475 case FUSE_DEV_IOC_CLONE: 2476 return fuse_dev_ioctl_clone(file, argp); 2477 2478 case FUSE_DEV_IOC_BACKING_OPEN: 2479 return fuse_dev_ioctl_backing_open(file, argp); 2480 2481 case FUSE_DEV_IOC_BACKING_CLOSE: 2482 return fuse_dev_ioctl_backing_close(file, argp); 2483 2484 default: 2485 return -ENOTTY; 2486 } 2487 } 2488 2489 const struct file_operations fuse_dev_operations = { 2490 .owner = THIS_MODULE, 2491 .open = fuse_dev_open, 2492 .read_iter = fuse_dev_read, 2493 .splice_read = fuse_dev_splice_read, 2494 .write_iter = fuse_dev_write, 2495 .splice_write = fuse_dev_splice_write, 2496 .poll = fuse_dev_poll, 2497 .release = fuse_dev_release, 2498 .fasync = fuse_dev_fasync, 2499 .unlocked_ioctl = fuse_dev_ioctl, 2500 .compat_ioctl = compat_ptr_ioctl, 2501 #ifdef CONFIG_FUSE_IO_URING 2502 .uring_cmd = fuse_uring_cmd, 2503 #endif 2504 }; 2505 EXPORT_SYMBOL_GPL(fuse_dev_operations); 2506 2507 static struct miscdevice fuse_miscdevice = { 2508 .minor = FUSE_MINOR, 2509 .name = "fuse", 2510 .fops = &fuse_dev_operations, 2511 }; 2512 2513 int __init fuse_dev_init(void) 2514 { 2515 int err = -ENOMEM; 2516 fuse_req_cachep = kmem_cache_create("fuse_request", 2517 sizeof(struct fuse_req), 2518 0, 0, NULL); 2519 if (!fuse_req_cachep) 2520 goto out; 2521 2522 err = misc_register(&fuse_miscdevice); 2523 if (err) 2524 goto out_cache_clean; 2525 2526 return 0; 2527 2528 out_cache_clean: 2529 kmem_cache_destroy(fuse_req_cachep); 2530 out: 2531 return err; 2532 } 2533 2534 void fuse_dev_cleanup(void) 2535 { 2536 misc_deregister(&fuse_miscdevice); 2537 kmem_cache_destroy(fuse_req_cachep); 2538 } 2539