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