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 #ifndef _FS_FUSE_I_H 10 #define _FS_FUSE_I_H 11 12 #ifndef pr_fmt 13 # define pr_fmt(fmt) "fuse: " fmt 14 #endif 15 16 #include <linux/fuse.h> 17 #include <linux/fs.h> 18 #include <linux/mount.h> 19 #include <linux/wait.h> 20 #include <linux/list.h> 21 #include <linux/spinlock.h> 22 #include <linux/mm.h> 23 #include <linux/backing-dev.h> 24 #include <linux/mutex.h> 25 #include <linux/rwsem.h> 26 #include <linux/rbtree.h> 27 #include <linux/poll.h> 28 #include <linux/workqueue.h> 29 #include <linux/kref.h> 30 #include <linux/xattr.h> 31 #include <linux/pid_namespace.h> 32 #include <linux/refcount.h> 33 #include <linux/user_namespace.h> 34 35 /** Default max number of pages that can be used in a single read request */ 36 #define FUSE_DEFAULT_MAX_PAGES_PER_REQ 32 37 38 /** Maximum of max_pages received in init_out */ 39 #define FUSE_MAX_MAX_PAGES 256 40 41 /** Bias for fi->writectr, meaning new writepages must not be sent */ 42 #define FUSE_NOWRITE INT_MIN 43 44 /** It could be as large as PATH_MAX, but would that have any uses? */ 45 #define FUSE_NAME_MAX 1024 46 47 /** Number of dentries for each connection in the control filesystem */ 48 #define FUSE_CTL_NUM_DENTRIES 5 49 50 /** List of active connections */ 51 extern struct list_head fuse_conn_list; 52 53 /** Global mutex protecting fuse_conn_list and the control filesystem */ 54 extern struct mutex fuse_mutex; 55 56 /** Module parameters */ 57 extern unsigned max_user_bgreq; 58 extern unsigned max_user_congthresh; 59 60 /* One forget request */ 61 struct fuse_forget_link { 62 struct fuse_forget_one forget_one; 63 struct fuse_forget_link *next; 64 }; 65 66 /* Submount lookup tracking */ 67 struct fuse_submount_lookup { 68 /** Refcount */ 69 refcount_t count; 70 71 /** Unique ID, which identifies the inode between userspace 72 * and kernel */ 73 u64 nodeid; 74 75 /** The request used for sending the FORGET message */ 76 struct fuse_forget_link *forget; 77 }; 78 79 /** Container for data related to mapping to backing file */ 80 struct fuse_backing { 81 struct file *file; 82 struct cred *cred; 83 84 /** refcount */ 85 refcount_t count; 86 struct rcu_head rcu; 87 }; 88 89 /** FUSE inode */ 90 struct fuse_inode { 91 /** Inode data */ 92 struct inode inode; 93 94 /** Unique ID, which identifies the inode between userspace 95 * and kernel */ 96 u64 nodeid; 97 98 /** Number of lookups on this inode */ 99 u64 nlookup; 100 101 /** The request used for sending the FORGET message */ 102 struct fuse_forget_link *forget; 103 104 /** Time in jiffies until the file attributes are valid */ 105 u64 i_time; 106 107 /* Which attributes are invalid */ 108 u32 inval_mask; 109 110 /** The sticky bit in inode->i_mode may have been removed, so 111 preserve the original mode */ 112 umode_t orig_i_mode; 113 114 /* Cache birthtime */ 115 struct timespec64 i_btime; 116 117 /** 64 bit inode number */ 118 u64 orig_ino; 119 120 /** Version of last attribute change */ 121 u64 attr_version; 122 123 union { 124 /* read/write io cache (regular file only) */ 125 struct { 126 /* Files usable in writepage. Protected by fi->lock */ 127 struct list_head write_files; 128 129 /* Writepages pending on truncate or fsync */ 130 struct list_head queued_writes; 131 132 /* Number of sent writes, a negative bias 133 * (FUSE_NOWRITE) means more writes are blocked */ 134 int writectr; 135 136 /** Number of files/maps using page cache */ 137 int iocachectr; 138 139 /* Waitq for writepage completion */ 140 wait_queue_head_t page_waitq; 141 142 /* waitq for direct-io completion */ 143 wait_queue_head_t direct_io_waitq; 144 145 /* List of writepage requestst (pending or sent) */ 146 struct rb_root writepages; 147 }; 148 149 /* readdir cache (directory only) */ 150 struct { 151 /* true if fully cached */ 152 bool cached; 153 154 /* size of cache */ 155 loff_t size; 156 157 /* position at end of cache (position of next entry) */ 158 loff_t pos; 159 160 /* version of the cache */ 161 u64 version; 162 163 /* modification time of directory when cache was 164 * started */ 165 struct timespec64 mtime; 166 167 /* iversion of directory when cache was started */ 168 u64 iversion; 169 170 /* protects above fields */ 171 spinlock_t lock; 172 } rdc; 173 }; 174 175 /** Miscellaneous bits describing inode state */ 176 unsigned long state; 177 178 /** Lock for serializing lookup and readdir for back compatibility*/ 179 struct mutex mutex; 180 181 /** Lock to protect write related fields */ 182 spinlock_t lock; 183 184 #ifdef CONFIG_FUSE_DAX 185 /* 186 * Dax specific inode data 187 */ 188 struct fuse_inode_dax *dax; 189 #endif 190 /** Submount specific lookup tracking */ 191 struct fuse_submount_lookup *submount_lookup; 192 #ifdef CONFIG_FUSE_PASSTHROUGH 193 /** Reference to backing file in passthrough mode */ 194 struct fuse_backing *fb; 195 #endif 196 }; 197 198 /** FUSE inode state bits */ 199 enum { 200 /** Advise readdirplus */ 201 FUSE_I_ADVISE_RDPLUS, 202 /** Initialized with readdirplus */ 203 FUSE_I_INIT_RDPLUS, 204 /** An operation changing file size is in progress */ 205 FUSE_I_SIZE_UNSTABLE, 206 /* Bad inode */ 207 FUSE_I_BAD, 208 /* Has btime */ 209 FUSE_I_BTIME, 210 /* Wants or already has page cache IO */ 211 FUSE_I_CACHE_IO_MODE, 212 }; 213 214 struct fuse_conn; 215 struct fuse_mount; 216 union fuse_file_args; 217 218 /** FUSE specific file data */ 219 struct fuse_file { 220 /** Fuse connection for this file */ 221 struct fuse_mount *fm; 222 223 /* Argument space reserved for open/release */ 224 union fuse_file_args *args; 225 226 /** Kernel file handle guaranteed to be unique */ 227 u64 kh; 228 229 /** File handle used by userspace */ 230 u64 fh; 231 232 /** Node id of this file */ 233 u64 nodeid; 234 235 /** Refcount */ 236 refcount_t count; 237 238 /** FOPEN_* flags returned by open */ 239 u32 open_flags; 240 241 /** Entry on inode's write_files list */ 242 struct list_head write_entry; 243 244 /* Readdir related */ 245 struct { 246 /* Dir stream position */ 247 loff_t pos; 248 249 /* Offset in cache */ 250 loff_t cache_off; 251 252 /* Version of cache we are reading */ 253 u64 version; 254 255 } readdir; 256 257 /** RB node to be linked on fuse_conn->polled_files */ 258 struct rb_node polled_node; 259 260 /** Wait queue head for poll */ 261 wait_queue_head_t poll_wait; 262 263 /** Does file hold a fi->iocachectr refcount? */ 264 enum { IOM_NONE, IOM_CACHED, IOM_UNCACHED } iomode; 265 266 #ifdef CONFIG_FUSE_PASSTHROUGH 267 /** Reference to backing file in passthrough mode */ 268 struct file *passthrough; 269 const struct cred *cred; 270 #endif 271 272 /** Has flock been performed on this file? */ 273 bool flock:1; 274 }; 275 276 /** One input argument of a request */ 277 struct fuse_in_arg { 278 unsigned size; 279 const void *value; 280 }; 281 282 /** One output argument of a request */ 283 struct fuse_arg { 284 unsigned size; 285 void *value; 286 }; 287 288 /** FUSE page descriptor */ 289 struct fuse_page_desc { 290 unsigned int length; 291 unsigned int offset; 292 }; 293 294 struct fuse_args { 295 uint64_t nodeid; 296 uint32_t opcode; 297 uint8_t in_numargs; 298 uint8_t out_numargs; 299 uint8_t ext_idx; 300 bool force:1; 301 bool noreply:1; 302 bool nocreds:1; 303 bool in_pages:1; 304 bool out_pages:1; 305 bool user_pages:1; 306 bool out_argvar:1; 307 bool page_zeroing:1; 308 bool page_replace:1; 309 bool may_block:1; 310 bool is_ext:1; 311 bool is_pinned:1; 312 struct fuse_in_arg in_args[3]; 313 struct fuse_arg out_args[2]; 314 void (*end)(struct fuse_mount *fm, struct fuse_args *args, int error); 315 }; 316 317 struct fuse_args_pages { 318 struct fuse_args args; 319 struct page **pages; 320 struct fuse_page_desc *descs; 321 unsigned int num_pages; 322 }; 323 324 struct fuse_release_args { 325 struct fuse_args args; 326 struct fuse_release_in inarg; 327 struct inode *inode; 328 }; 329 330 union fuse_file_args { 331 /* Used during open() */ 332 struct fuse_open_out open_outarg; 333 /* Used during release() */ 334 struct fuse_release_args release_args; 335 }; 336 337 #define FUSE_ARGS(args) struct fuse_args args = {} 338 339 /** The request IO state (for asynchronous processing) */ 340 struct fuse_io_priv { 341 struct kref refcnt; 342 int async; 343 spinlock_t lock; 344 unsigned reqs; 345 ssize_t bytes; 346 size_t size; 347 __u64 offset; 348 bool write; 349 bool should_dirty; 350 int err; 351 struct kiocb *iocb; 352 struct completion *done; 353 bool blocking; 354 }; 355 356 #define FUSE_IO_PRIV_SYNC(i) \ 357 { \ 358 .refcnt = KREF_INIT(1), \ 359 .async = 0, \ 360 .iocb = i, \ 361 } 362 363 /** 364 * Request flags 365 * 366 * FR_ISREPLY: set if the request has reply 367 * FR_FORCE: force sending of the request even if interrupted 368 * FR_BACKGROUND: request is sent in the background 369 * FR_WAITING: request is counted as "waiting" 370 * FR_ABORTED: the request was aborted 371 * FR_INTERRUPTED: the request has been interrupted 372 * FR_LOCKED: data is being copied to/from the request 373 * FR_PENDING: request is not yet in userspace 374 * FR_SENT: request is in userspace, waiting for an answer 375 * FR_FINISHED: request is finished 376 * FR_PRIVATE: request is on private list 377 * FR_ASYNC: request is asynchronous 378 */ 379 enum fuse_req_flag { 380 FR_ISREPLY, 381 FR_FORCE, 382 FR_BACKGROUND, 383 FR_WAITING, 384 FR_ABORTED, 385 FR_INTERRUPTED, 386 FR_LOCKED, 387 FR_PENDING, 388 FR_SENT, 389 FR_FINISHED, 390 FR_PRIVATE, 391 FR_ASYNC, 392 }; 393 394 /** 395 * A request to the client 396 * 397 * .waitq.lock protects the following fields: 398 * - FR_ABORTED 399 * - FR_LOCKED (may also be modified under fc->lock, tested under both) 400 */ 401 struct fuse_req { 402 /** This can be on either pending processing or io lists in 403 fuse_conn */ 404 struct list_head list; 405 406 /** Entry on the interrupts list */ 407 struct list_head intr_entry; 408 409 /* Input/output arguments */ 410 struct fuse_args *args; 411 412 /** refcount */ 413 refcount_t count; 414 415 /* Request flags, updated with test/set/clear_bit() */ 416 unsigned long flags; 417 418 /* The request input header */ 419 struct { 420 struct fuse_in_header h; 421 } in; 422 423 /* The request output header */ 424 struct { 425 struct fuse_out_header h; 426 } out; 427 428 /** Used to wake up the task waiting for completion of request*/ 429 wait_queue_head_t waitq; 430 431 #if IS_ENABLED(CONFIG_VIRTIO_FS) 432 /** virtio-fs's physically contiguous buffer for in and out args */ 433 void *argbuf; 434 #endif 435 436 /** fuse_mount this request belongs to */ 437 struct fuse_mount *fm; 438 }; 439 440 struct fuse_iqueue; 441 442 /** 443 * Input queue callbacks 444 * 445 * Input queue signalling is device-specific. For example, the /dev/fuse file 446 * uses fiq->waitq and fasync to wake processes that are waiting on queue 447 * readiness. These callbacks allow other device types to respond to input 448 * queue activity. 449 */ 450 struct fuse_iqueue_ops { 451 /** 452 * Send one forget 453 */ 454 void (*send_forget)(struct fuse_iqueue *fiq, struct fuse_forget_link *link); 455 456 /** 457 * Send interrupt for request 458 */ 459 void (*send_interrupt)(struct fuse_iqueue *fiq, struct fuse_req *req); 460 461 /** 462 * Send one request 463 */ 464 void (*send_req)(struct fuse_iqueue *fiq, struct fuse_req *req); 465 466 /** 467 * Clean up when fuse_iqueue is destroyed 468 */ 469 void (*release)(struct fuse_iqueue *fiq); 470 }; 471 472 /** /dev/fuse input queue operations */ 473 extern const struct fuse_iqueue_ops fuse_dev_fiq_ops; 474 475 struct fuse_iqueue { 476 /** Connection established */ 477 unsigned connected; 478 479 /** Lock protecting accesses to members of this structure */ 480 spinlock_t lock; 481 482 /** Readers of the connection are waiting on this */ 483 wait_queue_head_t waitq; 484 485 /** The next unique request id */ 486 u64 reqctr; 487 488 /** The list of pending requests */ 489 struct list_head pending; 490 491 /** Pending interrupts */ 492 struct list_head interrupts; 493 494 /** Queue of pending forgets */ 495 struct fuse_forget_link forget_list_head; 496 struct fuse_forget_link *forget_list_tail; 497 498 /** Batching of FORGET requests (positive indicates FORGET batch) */ 499 int forget_batch; 500 501 /** O_ASYNC requests */ 502 struct fasync_struct *fasync; 503 504 /** Device-specific callbacks */ 505 const struct fuse_iqueue_ops *ops; 506 507 /** Device-specific state */ 508 void *priv; 509 }; 510 511 #define FUSE_PQ_HASH_BITS 8 512 #define FUSE_PQ_HASH_SIZE (1 << FUSE_PQ_HASH_BITS) 513 514 struct fuse_pqueue { 515 /** Connection established */ 516 unsigned connected; 517 518 /** Lock protecting accessess to members of this structure */ 519 spinlock_t lock; 520 521 /** Hash table of requests being processed */ 522 struct list_head *processing; 523 524 /** The list of requests under I/O */ 525 struct list_head io; 526 }; 527 528 /** 529 * Fuse device instance 530 */ 531 struct fuse_dev { 532 /** Fuse connection for this device */ 533 struct fuse_conn *fc; 534 535 /** Processing queue */ 536 struct fuse_pqueue pq; 537 538 /** list entry on fc->devices */ 539 struct list_head entry; 540 }; 541 542 enum fuse_dax_mode { 543 FUSE_DAX_INODE_DEFAULT, /* default */ 544 FUSE_DAX_ALWAYS, /* "-o dax=always" */ 545 FUSE_DAX_NEVER, /* "-o dax=never" */ 546 FUSE_DAX_INODE_USER, /* "-o dax=inode" */ 547 }; 548 549 static inline bool fuse_is_inode_dax_mode(enum fuse_dax_mode mode) 550 { 551 return mode == FUSE_DAX_INODE_DEFAULT || mode == FUSE_DAX_INODE_USER; 552 } 553 554 struct fuse_fs_context { 555 int fd; 556 struct file *file; 557 unsigned int rootmode; 558 kuid_t user_id; 559 kgid_t group_id; 560 bool is_bdev:1; 561 bool fd_present:1; 562 bool rootmode_present:1; 563 bool user_id_present:1; 564 bool group_id_present:1; 565 bool default_permissions:1; 566 bool allow_other:1; 567 bool destroy:1; 568 bool no_control:1; 569 bool no_force_umount:1; 570 bool legacy_opts_show:1; 571 enum fuse_dax_mode dax_mode; 572 unsigned int max_read; 573 unsigned int blksize; 574 const char *subtype; 575 576 /* DAX device, may be NULL */ 577 struct dax_device *dax_dev; 578 579 /* fuse_dev pointer to fill in, should contain NULL on entry */ 580 void **fudptr; 581 }; 582 583 struct fuse_sync_bucket { 584 /* count is a possible scalability bottleneck */ 585 atomic_t count; 586 wait_queue_head_t waitq; 587 struct rcu_head rcu; 588 }; 589 590 /** 591 * A Fuse connection. 592 * 593 * This structure is created, when the root filesystem is mounted, and 594 * is destroyed, when the client device is closed and the last 595 * fuse_mount is destroyed. 596 */ 597 struct fuse_conn { 598 /** Lock protecting accessess to members of this structure */ 599 spinlock_t lock; 600 601 /** Refcount */ 602 refcount_t count; 603 604 /** Number of fuse_dev's */ 605 atomic_t dev_count; 606 607 struct rcu_head rcu; 608 609 /** The user id for this mount */ 610 kuid_t user_id; 611 612 /** The group id for this mount */ 613 kgid_t group_id; 614 615 /** The pid namespace for this mount */ 616 struct pid_namespace *pid_ns; 617 618 /** The user namespace for this mount */ 619 struct user_namespace *user_ns; 620 621 /** Maximum read size */ 622 unsigned max_read; 623 624 /** Maximum write size */ 625 unsigned max_write; 626 627 /** Maximum number of pages that can be used in a single request */ 628 unsigned int max_pages; 629 630 /** Constrain ->max_pages to this value during feature negotiation */ 631 unsigned int max_pages_limit; 632 633 /** Input queue */ 634 struct fuse_iqueue iq; 635 636 /** The next unique kernel file handle */ 637 atomic64_t khctr; 638 639 /** rbtree of fuse_files waiting for poll events indexed by ph */ 640 struct rb_root polled_files; 641 642 /** Maximum number of outstanding background requests */ 643 unsigned max_background; 644 645 /** Number of background requests at which congestion starts */ 646 unsigned congestion_threshold; 647 648 /** Number of requests currently in the background */ 649 unsigned num_background; 650 651 /** Number of background requests currently queued for userspace */ 652 unsigned active_background; 653 654 /** The list of background requests set aside for later queuing */ 655 struct list_head bg_queue; 656 657 /** Protects: max_background, congestion_threshold, num_background, 658 * active_background, bg_queue, blocked */ 659 spinlock_t bg_lock; 660 661 /** Flag indicating that INIT reply has been received. Allocating 662 * any fuse request will be suspended until the flag is set */ 663 int initialized; 664 665 /** Flag indicating if connection is blocked. This will be 666 the case before the INIT reply is received, and if there 667 are too many outstading backgrounds requests */ 668 int blocked; 669 670 /** waitq for blocked connection */ 671 wait_queue_head_t blocked_waitq; 672 673 /** Connection established, cleared on umount, connection 674 abort and device release */ 675 unsigned connected; 676 677 /** Connection aborted via sysfs */ 678 bool aborted; 679 680 /** Connection failed (version mismatch). Cannot race with 681 setting other bitfields since it is only set once in INIT 682 reply, before any other request, and never cleared */ 683 unsigned conn_error:1; 684 685 /** Connection successful. Only set in INIT */ 686 unsigned conn_init:1; 687 688 /** Do readahead asynchronously? Only set in INIT */ 689 unsigned async_read:1; 690 691 /** Return an unique read error after abort. Only set in INIT */ 692 unsigned abort_err:1; 693 694 /** Do not send separate SETATTR request before open(O_TRUNC) */ 695 unsigned atomic_o_trunc:1; 696 697 /** Filesystem supports NFS exporting. Only set in INIT */ 698 unsigned export_support:1; 699 700 /** write-back cache policy (default is write-through) */ 701 unsigned writeback_cache:1; 702 703 /** allow parallel lookups and readdir (default is serialized) */ 704 unsigned parallel_dirops:1; 705 706 /** handle fs handles killing suid/sgid/cap on write/chown/trunc */ 707 unsigned handle_killpriv:1; 708 709 /** cache READLINK responses in page cache */ 710 unsigned cache_symlinks:1; 711 712 /* show legacy mount options */ 713 unsigned int legacy_opts_show:1; 714 715 /* 716 * fs kills suid/sgid/cap on write/chown/trunc. suid is killed on 717 * write/trunc only if caller did not have CAP_FSETID. sgid is killed 718 * on write/truncate only if caller did not have CAP_FSETID as well as 719 * file has group execute permission. 720 */ 721 unsigned handle_killpriv_v2:1; 722 723 /* 724 * The following bitfields are only for optimization purposes 725 * and hence races in setting them will not cause malfunction 726 */ 727 728 /** Is open/release not implemented by fs? */ 729 unsigned no_open:1; 730 731 /** Is opendir/releasedir not implemented by fs? */ 732 unsigned no_opendir:1; 733 734 /** Is fsync not implemented by fs? */ 735 unsigned no_fsync:1; 736 737 /** Is fsyncdir not implemented by fs? */ 738 unsigned no_fsyncdir:1; 739 740 /** Is flush not implemented by fs? */ 741 unsigned no_flush:1; 742 743 /** Is setxattr not implemented by fs? */ 744 unsigned no_setxattr:1; 745 746 /** Does file server support extended setxattr */ 747 unsigned setxattr_ext:1; 748 749 /** Is getxattr not implemented by fs? */ 750 unsigned no_getxattr:1; 751 752 /** Is listxattr not implemented by fs? */ 753 unsigned no_listxattr:1; 754 755 /** Is removexattr not implemented by fs? */ 756 unsigned no_removexattr:1; 757 758 /** Are posix file locking primitives not implemented by fs? */ 759 unsigned no_lock:1; 760 761 /** Is access not implemented by fs? */ 762 unsigned no_access:1; 763 764 /** Is create not implemented by fs? */ 765 unsigned no_create:1; 766 767 /** Is interrupt not implemented by fs? */ 768 unsigned no_interrupt:1; 769 770 /** Is bmap not implemented by fs? */ 771 unsigned no_bmap:1; 772 773 /** Is poll not implemented by fs? */ 774 unsigned no_poll:1; 775 776 /** Do multi-page cached writes */ 777 unsigned big_writes:1; 778 779 /** Don't apply umask to creation modes */ 780 unsigned dont_mask:1; 781 782 /** Are BSD file locking primitives not implemented by fs? */ 783 unsigned no_flock:1; 784 785 /** Is fallocate not implemented by fs? */ 786 unsigned no_fallocate:1; 787 788 /** Is rename with flags implemented by fs? */ 789 unsigned no_rename2:1; 790 791 /** Use enhanced/automatic page cache invalidation. */ 792 unsigned auto_inval_data:1; 793 794 /** Filesystem is fully responsible for page cache invalidation. */ 795 unsigned explicit_inval_data:1; 796 797 /** Does the filesystem support readdirplus? */ 798 unsigned do_readdirplus:1; 799 800 /** Does the filesystem want adaptive readdirplus? */ 801 unsigned readdirplus_auto:1; 802 803 /** Does the filesystem support asynchronous direct-IO submission? */ 804 unsigned async_dio:1; 805 806 /** Is lseek not implemented by fs? */ 807 unsigned no_lseek:1; 808 809 /** Does the filesystem support posix acls? */ 810 unsigned posix_acl:1; 811 812 /** Check permissions based on the file mode or not? */ 813 unsigned default_permissions:1; 814 815 /** Allow other than the mounter user to access the filesystem ? */ 816 unsigned allow_other:1; 817 818 /** Does the filesystem support copy_file_range? */ 819 unsigned no_copy_file_range:1; 820 821 /* Send DESTROY request */ 822 unsigned int destroy:1; 823 824 /* Delete dentries that have gone stale */ 825 unsigned int delete_stale:1; 826 827 /** Do not create entry in fusectl fs */ 828 unsigned int no_control:1; 829 830 /** Do not allow MNT_FORCE umount */ 831 unsigned int no_force_umount:1; 832 833 /* Auto-mount submounts announced by the server */ 834 unsigned int auto_submounts:1; 835 836 /* Propagate syncfs() to server */ 837 unsigned int sync_fs:1; 838 839 /* Initialize security xattrs when creating a new inode */ 840 unsigned int init_security:1; 841 842 /* Add supplementary group info when creating a new inode */ 843 unsigned int create_supp_group:1; 844 845 /* Does the filesystem support per inode DAX? */ 846 unsigned int inode_dax:1; 847 848 /* Is tmpfile not implemented by fs? */ 849 unsigned int no_tmpfile:1; 850 851 /* Relax restrictions to allow shared mmap in FOPEN_DIRECT_IO mode */ 852 unsigned int direct_io_allow_mmap:1; 853 854 /* Is statx not implemented by fs? */ 855 unsigned int no_statx:1; 856 857 /** Passthrough support for read/write IO */ 858 unsigned int passthrough:1; 859 860 /** Maximum stack depth for passthrough backing files */ 861 int max_stack_depth; 862 863 /** The number of requests waiting for completion */ 864 atomic_t num_waiting; 865 866 /** Negotiated minor version */ 867 unsigned minor; 868 869 /** Entry on the fuse_conn_list */ 870 struct list_head entry; 871 872 /** Device ID from the root super block */ 873 dev_t dev; 874 875 /** Dentries in the control filesystem */ 876 struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES]; 877 878 /** number of dentries used in the above array */ 879 int ctl_ndents; 880 881 /** Key for lock owner ID scrambling */ 882 u32 scramble_key[4]; 883 884 /** Version counter for attribute changes */ 885 atomic64_t attr_version; 886 887 /** Called on final put */ 888 void (*release)(struct fuse_conn *); 889 890 /** 891 * Read/write semaphore to hold when accessing the sb of any 892 * fuse_mount belonging to this connection 893 */ 894 struct rw_semaphore killsb; 895 896 /** List of device instances belonging to this connection */ 897 struct list_head devices; 898 899 #ifdef CONFIG_FUSE_DAX 900 /* Dax mode */ 901 enum fuse_dax_mode dax_mode; 902 903 /* Dax specific conn data, non-NULL if DAX is enabled */ 904 struct fuse_conn_dax *dax; 905 #endif 906 907 /** List of filesystems using this connection */ 908 struct list_head mounts; 909 910 /* New writepages go into this bucket */ 911 struct fuse_sync_bucket __rcu *curr_bucket; 912 913 #ifdef CONFIG_FUSE_PASSTHROUGH 914 /** IDR for backing files ids */ 915 struct idr backing_files_map; 916 #endif 917 }; 918 919 /* 920 * Represents a mounted filesystem, potentially a submount. 921 * 922 * This object allows sharing a fuse_conn between separate mounts to 923 * allow submounts with dedicated superblocks and thus separate device 924 * IDs. 925 */ 926 struct fuse_mount { 927 /* Underlying (potentially shared) connection to the FUSE server */ 928 struct fuse_conn *fc; 929 930 /* 931 * Super block for this connection (fc->killsb must be held when 932 * accessing this). 933 */ 934 struct super_block *sb; 935 936 /* Entry on fc->mounts */ 937 struct list_head fc_entry; 938 struct rcu_head rcu; 939 }; 940 941 static inline struct fuse_mount *get_fuse_mount_super(struct super_block *sb) 942 { 943 return sb->s_fs_info; 944 } 945 946 static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb) 947 { 948 return get_fuse_mount_super(sb)->fc; 949 } 950 951 static inline struct fuse_mount *get_fuse_mount(struct inode *inode) 952 { 953 return get_fuse_mount_super(inode->i_sb); 954 } 955 956 static inline struct fuse_conn *get_fuse_conn(struct inode *inode) 957 { 958 return get_fuse_mount_super(inode->i_sb)->fc; 959 } 960 961 static inline struct fuse_inode *get_fuse_inode(struct inode *inode) 962 { 963 return container_of(inode, struct fuse_inode, inode); 964 } 965 966 static inline u64 get_node_id(struct inode *inode) 967 { 968 return get_fuse_inode(inode)->nodeid; 969 } 970 971 static inline int invalid_nodeid(u64 nodeid) 972 { 973 return !nodeid || nodeid == FUSE_ROOT_ID; 974 } 975 976 static inline u64 fuse_get_attr_version(struct fuse_conn *fc) 977 { 978 return atomic64_read(&fc->attr_version); 979 } 980 981 static inline bool fuse_stale_inode(const struct inode *inode, int generation, 982 struct fuse_attr *attr) 983 { 984 return inode->i_generation != generation || 985 inode_wrong_type(inode, attr->mode); 986 } 987 988 static inline void fuse_make_bad(struct inode *inode) 989 { 990 set_bit(FUSE_I_BAD, &get_fuse_inode(inode)->state); 991 } 992 993 static inline bool fuse_is_bad(struct inode *inode) 994 { 995 return unlikely(test_bit(FUSE_I_BAD, &get_fuse_inode(inode)->state)); 996 } 997 998 static inline struct page **fuse_pages_alloc(unsigned int npages, gfp_t flags, 999 struct fuse_page_desc **desc) 1000 { 1001 struct page **pages; 1002 1003 pages = kzalloc(npages * (sizeof(struct page *) + 1004 sizeof(struct fuse_page_desc)), flags); 1005 *desc = (void *) (pages + npages); 1006 1007 return pages; 1008 } 1009 1010 static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs, 1011 unsigned int index, 1012 unsigned int nr_pages) 1013 { 1014 int i; 1015 1016 for (i = index; i < index + nr_pages; i++) 1017 descs[i].length = PAGE_SIZE - descs[i].offset; 1018 } 1019 1020 static inline void fuse_sync_bucket_dec(struct fuse_sync_bucket *bucket) 1021 { 1022 /* Need RCU protection to prevent use after free after the decrement */ 1023 rcu_read_lock(); 1024 if (atomic_dec_and_test(&bucket->count)) 1025 wake_up(&bucket->waitq); 1026 rcu_read_unlock(); 1027 } 1028 1029 /** Device operations */ 1030 extern const struct file_operations fuse_dev_operations; 1031 1032 extern const struct dentry_operations fuse_dentry_operations; 1033 extern const struct dentry_operations fuse_root_dentry_operations; 1034 1035 /** 1036 * Get a filled in inode 1037 */ 1038 struct inode *fuse_iget(struct super_block *sb, u64 nodeid, 1039 int generation, struct fuse_attr *attr, 1040 u64 attr_valid, u64 attr_version); 1041 1042 int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name, 1043 struct fuse_entry_out *outarg, struct inode **inode); 1044 1045 /** 1046 * Send FORGET command 1047 */ 1048 void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget, 1049 u64 nodeid, u64 nlookup); 1050 1051 struct fuse_forget_link *fuse_alloc_forget(void); 1052 1053 /* 1054 * Initialize READ or READDIR request 1055 */ 1056 struct fuse_io_args { 1057 union { 1058 struct { 1059 struct fuse_read_in in; 1060 u64 attr_ver; 1061 } read; 1062 struct { 1063 struct fuse_write_in in; 1064 struct fuse_write_out out; 1065 bool page_locked; 1066 } write; 1067 }; 1068 struct fuse_args_pages ap; 1069 struct fuse_io_priv *io; 1070 struct fuse_file *ff; 1071 }; 1072 1073 void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos, 1074 size_t count, int opcode); 1075 1076 1077 struct fuse_file *fuse_file_alloc(struct fuse_mount *fm, bool release); 1078 void fuse_file_free(struct fuse_file *ff); 1079 int fuse_finish_open(struct inode *inode, struct file *file); 1080 1081 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, 1082 unsigned int flags); 1083 1084 /** 1085 * Send RELEASE or RELEASEDIR request 1086 */ 1087 void fuse_release_common(struct file *file, bool isdir); 1088 1089 /** 1090 * Send FSYNC or FSYNCDIR request 1091 */ 1092 int fuse_fsync_common(struct file *file, loff_t start, loff_t end, 1093 int datasync, int opcode); 1094 1095 /** 1096 * Notify poll wakeup 1097 */ 1098 int fuse_notify_poll_wakeup(struct fuse_conn *fc, 1099 struct fuse_notify_poll_wakeup_out *outarg); 1100 1101 /** 1102 * Initialize file operations on a regular file 1103 */ 1104 void fuse_init_file_inode(struct inode *inode, unsigned int flags); 1105 1106 /** 1107 * Initialize inode operations on regular files and special files 1108 */ 1109 void fuse_init_common(struct inode *inode); 1110 1111 /** 1112 * Initialize inode and file operations on a directory 1113 */ 1114 void fuse_init_dir(struct inode *inode); 1115 1116 /** 1117 * Initialize inode operations on a symlink 1118 */ 1119 void fuse_init_symlink(struct inode *inode); 1120 1121 /** 1122 * Change attributes of an inode 1123 */ 1124 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr, 1125 struct fuse_statx *sx, 1126 u64 attr_valid, u64 attr_version); 1127 1128 void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr, 1129 struct fuse_statx *sx, 1130 u64 attr_valid, u32 cache_mask); 1131 1132 u32 fuse_get_cache_mask(struct inode *inode); 1133 1134 /** 1135 * Initialize the client device 1136 */ 1137 int fuse_dev_init(void); 1138 1139 /** 1140 * Cleanup the client device 1141 */ 1142 void fuse_dev_cleanup(void); 1143 1144 int fuse_ctl_init(void); 1145 void __exit fuse_ctl_cleanup(void); 1146 1147 /** 1148 * Simple request sending that does request allocation and freeing 1149 */ 1150 ssize_t __fuse_simple_request(struct mnt_idmap *idmap, 1151 struct fuse_mount *fm, 1152 struct fuse_args *args); 1153 1154 static inline ssize_t fuse_simple_request(struct fuse_mount *fm, struct fuse_args *args) 1155 { 1156 return __fuse_simple_request(&invalid_mnt_idmap, fm, args); 1157 } 1158 1159 static inline ssize_t fuse_simple_idmap_request(struct mnt_idmap *idmap, 1160 struct fuse_mount *fm, 1161 struct fuse_args *args) 1162 { 1163 return __fuse_simple_request(idmap, fm, args); 1164 } 1165 1166 int fuse_simple_background(struct fuse_mount *fm, struct fuse_args *args, 1167 gfp_t gfp_flags); 1168 1169 /** 1170 * End a finished request 1171 */ 1172 void fuse_request_end(struct fuse_req *req); 1173 1174 /* Abort all requests */ 1175 void fuse_abort_conn(struct fuse_conn *fc); 1176 void fuse_wait_aborted(struct fuse_conn *fc); 1177 1178 /** 1179 * Invalidate inode attributes 1180 */ 1181 1182 /* Attributes possibly changed on data modification */ 1183 #define FUSE_STATX_MODIFY (STATX_MTIME | STATX_CTIME | STATX_BLOCKS) 1184 1185 /* Attributes possibly changed on data and/or size modification */ 1186 #define FUSE_STATX_MODSIZE (FUSE_STATX_MODIFY | STATX_SIZE) 1187 1188 void fuse_invalidate_attr(struct inode *inode); 1189 void fuse_invalidate_attr_mask(struct inode *inode, u32 mask); 1190 1191 void fuse_invalidate_entry_cache(struct dentry *entry); 1192 1193 void fuse_invalidate_atime(struct inode *inode); 1194 1195 u64 fuse_time_to_jiffies(u64 sec, u32 nsec); 1196 #define ATTR_TIMEOUT(o) \ 1197 fuse_time_to_jiffies((o)->attr_valid, (o)->attr_valid_nsec) 1198 1199 void fuse_change_entry_timeout(struct dentry *entry, struct fuse_entry_out *o); 1200 1201 /** 1202 * Acquire reference to fuse_conn 1203 */ 1204 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc); 1205 1206 /** 1207 * Initialize fuse_conn 1208 */ 1209 void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm, 1210 struct user_namespace *user_ns, 1211 const struct fuse_iqueue_ops *fiq_ops, void *fiq_priv); 1212 1213 /** 1214 * Release reference to fuse_conn 1215 */ 1216 void fuse_conn_put(struct fuse_conn *fc); 1217 1218 struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc); 1219 struct fuse_dev *fuse_dev_alloc(void); 1220 void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc); 1221 void fuse_dev_free(struct fuse_dev *fud); 1222 void fuse_send_init(struct fuse_mount *fm); 1223 1224 /** 1225 * Fill in superblock and initialize fuse connection 1226 * @sb: partially-initialized superblock to fill in 1227 * @ctx: mount context 1228 */ 1229 int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx); 1230 1231 /* 1232 * Remove the mount from the connection 1233 * 1234 * Returns whether this was the last mount 1235 */ 1236 bool fuse_mount_remove(struct fuse_mount *fm); 1237 1238 /* 1239 * Setup context ops for submounts 1240 */ 1241 int fuse_init_fs_context_submount(struct fs_context *fsc); 1242 1243 /* 1244 * Shut down the connection (possibly sending DESTROY request). 1245 */ 1246 void fuse_conn_destroy(struct fuse_mount *fm); 1247 1248 /* Drop the connection and free the fuse mount */ 1249 void fuse_mount_destroy(struct fuse_mount *fm); 1250 1251 /** 1252 * Add connection to control filesystem 1253 */ 1254 int fuse_ctl_add_conn(struct fuse_conn *fc); 1255 1256 /** 1257 * Remove connection from control filesystem 1258 */ 1259 void fuse_ctl_remove_conn(struct fuse_conn *fc); 1260 1261 /** 1262 * Is file type valid? 1263 */ 1264 int fuse_valid_type(int m); 1265 1266 bool fuse_invalid_attr(struct fuse_attr *attr); 1267 1268 /** 1269 * Is current process allowed to perform filesystem operation? 1270 */ 1271 bool fuse_allow_current_process(struct fuse_conn *fc); 1272 1273 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id); 1274 1275 void fuse_flush_time_update(struct inode *inode); 1276 void fuse_update_ctime(struct inode *inode); 1277 1278 int fuse_update_attributes(struct inode *inode, struct file *file, u32 mask); 1279 1280 void fuse_flush_writepages(struct inode *inode); 1281 1282 void fuse_set_nowrite(struct inode *inode); 1283 void fuse_release_nowrite(struct inode *inode); 1284 1285 /** 1286 * Scan all fuse_mounts belonging to fc to find the first where 1287 * ilookup5() returns a result. Return that result and the 1288 * respective fuse_mount in *fm (unless fm is NULL). 1289 * 1290 * The caller must hold fc->killsb. 1291 */ 1292 struct inode *fuse_ilookup(struct fuse_conn *fc, u64 nodeid, 1293 struct fuse_mount **fm); 1294 1295 /** 1296 * File-system tells the kernel to invalidate cache for the given node id. 1297 */ 1298 int fuse_reverse_inval_inode(struct fuse_conn *fc, u64 nodeid, 1299 loff_t offset, loff_t len); 1300 1301 /** 1302 * File-system tells the kernel to invalidate parent attributes and 1303 * the dentry matching parent/name. 1304 * 1305 * If the child_nodeid is non-zero and: 1306 * - matches the inode number for the dentry matching parent/name, 1307 * - is not a mount point 1308 * - is a file or oan empty directory 1309 * then the dentry is unhashed (d_delete()). 1310 */ 1311 int fuse_reverse_inval_entry(struct fuse_conn *fc, u64 parent_nodeid, 1312 u64 child_nodeid, struct qstr *name, u32 flags); 1313 1314 int fuse_do_open(struct fuse_mount *fm, u64 nodeid, struct file *file, 1315 bool isdir); 1316 1317 /** 1318 * fuse_direct_io() flags 1319 */ 1320 1321 /** If set, it is WRITE; otherwise - READ */ 1322 #define FUSE_DIO_WRITE (1 << 0) 1323 1324 /** CUSE pass fuse_direct_io() a file which f_mapping->host is not from FUSE */ 1325 #define FUSE_DIO_CUSE (1 << 1) 1326 1327 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter, 1328 loff_t *ppos, int flags); 1329 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg, 1330 unsigned int flags); 1331 long fuse_ioctl_common(struct file *file, unsigned int cmd, 1332 unsigned long arg, unsigned int flags); 1333 __poll_t fuse_file_poll(struct file *file, poll_table *wait); 1334 int fuse_dev_release(struct inode *inode, struct file *file); 1335 1336 bool fuse_write_update_attr(struct inode *inode, loff_t pos, ssize_t written); 1337 1338 int fuse_flush_times(struct inode *inode, struct fuse_file *ff); 1339 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc); 1340 1341 int fuse_do_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 1342 struct iattr *attr, struct file *file); 1343 1344 void fuse_set_initialized(struct fuse_conn *fc); 1345 1346 void fuse_unlock_inode(struct inode *inode, bool locked); 1347 bool fuse_lock_inode(struct inode *inode); 1348 1349 int fuse_setxattr(struct inode *inode, const char *name, const void *value, 1350 size_t size, int flags, unsigned int extra_flags); 1351 ssize_t fuse_getxattr(struct inode *inode, const char *name, void *value, 1352 size_t size); 1353 ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size); 1354 int fuse_removexattr(struct inode *inode, const char *name); 1355 extern const struct xattr_handler * const fuse_xattr_handlers[]; 1356 1357 struct posix_acl; 1358 struct posix_acl *fuse_get_inode_acl(struct inode *inode, int type, bool rcu); 1359 struct posix_acl *fuse_get_acl(struct mnt_idmap *idmap, 1360 struct dentry *dentry, int type); 1361 int fuse_set_acl(struct mnt_idmap *, struct dentry *dentry, 1362 struct posix_acl *acl, int type); 1363 1364 /* readdir.c */ 1365 int fuse_readdir(struct file *file, struct dir_context *ctx); 1366 1367 /** 1368 * Return the number of bytes in an arguments list 1369 */ 1370 unsigned int fuse_len_args(unsigned int numargs, struct fuse_arg *args); 1371 1372 /** 1373 * Get the next unique ID for a request 1374 */ 1375 u64 fuse_get_unique(struct fuse_iqueue *fiq); 1376 void fuse_free_conn(struct fuse_conn *fc); 1377 1378 /* dax.c */ 1379 1380 #define FUSE_IS_DAX(inode) (IS_ENABLED(CONFIG_FUSE_DAX) && IS_DAX(inode)) 1381 1382 ssize_t fuse_dax_read_iter(struct kiocb *iocb, struct iov_iter *to); 1383 ssize_t fuse_dax_write_iter(struct kiocb *iocb, struct iov_iter *from); 1384 int fuse_dax_mmap(struct file *file, struct vm_area_struct *vma); 1385 int fuse_dax_break_layouts(struct inode *inode, u64 dmap_start, u64 dmap_end); 1386 int fuse_dax_conn_alloc(struct fuse_conn *fc, enum fuse_dax_mode mode, 1387 struct dax_device *dax_dev); 1388 void fuse_dax_conn_free(struct fuse_conn *fc); 1389 bool fuse_dax_inode_alloc(struct super_block *sb, struct fuse_inode *fi); 1390 void fuse_dax_inode_init(struct inode *inode, unsigned int flags); 1391 void fuse_dax_inode_cleanup(struct inode *inode); 1392 void fuse_dax_dontcache(struct inode *inode, unsigned int flags); 1393 bool fuse_dax_check_alignment(struct fuse_conn *fc, unsigned int map_alignment); 1394 void fuse_dax_cancel_work(struct fuse_conn *fc); 1395 1396 /* ioctl.c */ 1397 long fuse_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 1398 long fuse_file_compat_ioctl(struct file *file, unsigned int cmd, 1399 unsigned long arg); 1400 int fuse_fileattr_get(struct dentry *dentry, struct fileattr *fa); 1401 int fuse_fileattr_set(struct mnt_idmap *idmap, 1402 struct dentry *dentry, struct fileattr *fa); 1403 1404 /* iomode.c */ 1405 int fuse_file_cached_io_open(struct inode *inode, struct fuse_file *ff); 1406 int fuse_inode_uncached_io_start(struct fuse_inode *fi, 1407 struct fuse_backing *fb); 1408 void fuse_inode_uncached_io_end(struct fuse_inode *fi); 1409 1410 int fuse_file_io_open(struct file *file, struct inode *inode); 1411 void fuse_file_io_release(struct fuse_file *ff, struct inode *inode); 1412 1413 /* file.c */ 1414 struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid, 1415 unsigned int open_flags, bool isdir); 1416 void fuse_file_release(struct inode *inode, struct fuse_file *ff, 1417 unsigned int open_flags, fl_owner_t id, bool isdir); 1418 1419 /* passthrough.c */ 1420 static inline struct fuse_backing *fuse_inode_backing(struct fuse_inode *fi) 1421 { 1422 #ifdef CONFIG_FUSE_PASSTHROUGH 1423 return READ_ONCE(fi->fb); 1424 #else 1425 return NULL; 1426 #endif 1427 } 1428 1429 static inline struct fuse_backing *fuse_inode_backing_set(struct fuse_inode *fi, 1430 struct fuse_backing *fb) 1431 { 1432 #ifdef CONFIG_FUSE_PASSTHROUGH 1433 return xchg(&fi->fb, fb); 1434 #else 1435 return NULL; 1436 #endif 1437 } 1438 1439 #ifdef CONFIG_FUSE_PASSTHROUGH 1440 struct fuse_backing *fuse_backing_get(struct fuse_backing *fb); 1441 void fuse_backing_put(struct fuse_backing *fb); 1442 #else 1443 1444 static inline struct fuse_backing *fuse_backing_get(struct fuse_backing *fb) 1445 { 1446 return NULL; 1447 } 1448 1449 static inline void fuse_backing_put(struct fuse_backing *fb) 1450 { 1451 } 1452 #endif 1453 1454 void fuse_backing_files_init(struct fuse_conn *fc); 1455 void fuse_backing_files_free(struct fuse_conn *fc); 1456 int fuse_backing_open(struct fuse_conn *fc, struct fuse_backing_map *map); 1457 int fuse_backing_close(struct fuse_conn *fc, int backing_id); 1458 1459 struct fuse_backing *fuse_passthrough_open(struct file *file, 1460 struct inode *inode, 1461 int backing_id); 1462 void fuse_passthrough_release(struct fuse_file *ff, struct fuse_backing *fb); 1463 1464 static inline struct file *fuse_file_passthrough(struct fuse_file *ff) 1465 { 1466 #ifdef CONFIG_FUSE_PASSTHROUGH 1467 return ff->passthrough; 1468 #else 1469 return NULL; 1470 #endif 1471 } 1472 1473 ssize_t fuse_passthrough_read_iter(struct kiocb *iocb, struct iov_iter *iter); 1474 ssize_t fuse_passthrough_write_iter(struct kiocb *iocb, struct iov_iter *iter); 1475 ssize_t fuse_passthrough_splice_read(struct file *in, loff_t *ppos, 1476 struct pipe_inode_info *pipe, 1477 size_t len, unsigned int flags); 1478 ssize_t fuse_passthrough_splice_write(struct pipe_inode_info *pipe, 1479 struct file *out, loff_t *ppos, 1480 size_t len, unsigned int flags); 1481 ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma); 1482 1483 #endif /* _FS_FUSE_I_H */ 1484