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