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 no_req_timeout:1; 422 bool legacy_opts_show:1; 423 bool syncfs_capable:1; 424 bool is_virtiofs:1; 425 enum fuse_vdax_mode vdax_mode; 426 unsigned int max_read; 427 unsigned int blksize; 428 const char *subtype; 429 430 /* Virtiofs DAX device, may be NULL */ 431 struct dax_device *vdax_dev; 432 }; 433 434 struct fuse_sync_bucket { 435 /* count is a possible scalability bottleneck */ 436 atomic_t count; 437 wait_queue_head_t waitq; 438 struct rcu_head rcu; 439 }; 440 441 /** 442 * struct fuse_conn - A Fuse connection. 443 * 444 * This structure is created, when the root filesystem is mounted, and 445 * is destroyed, when the client device is closed and the last 446 * fuse_mount is destroyed. 447 */ 448 struct fuse_conn { 449 /** 450 * @lock: Lock protecting: 451 * - polled_files 452 * - backing_files_map 453 * - curr_bucket 454 */ 455 spinlock_t lock; 456 457 /** @count: Refcount */ 458 refcount_t count; 459 460 /** @epoch: Current epoch for up-to-date dentries */ 461 atomic_t epoch; 462 463 /** @epoch_work: Used to invalidate dentries from old epochs */ 464 struct work_struct epoch_work; 465 466 /** @rcu: Used to delay freeing fuse_conn, making it safe */ 467 struct rcu_head rcu; 468 469 /** @user_id: The user id for this mount */ 470 kuid_t user_id; 471 472 /** @group_id: The group id for this mount */ 473 kgid_t group_id; 474 475 /** @pid_ns: The pid namespace for this mount */ 476 struct pid_namespace *pid_ns; 477 478 /** @user_ns: The user namespace for this mount */ 479 struct user_namespace *user_ns; 480 481 /** @max_read: Maximum read size */ 482 unsigned max_read; 483 484 /** @max_write: Maximum write size */ 485 unsigned max_write; 486 487 /** 488 * @max_pages: Maximum number of pages that can be used in a 489 * single request 490 */ 491 unsigned int max_pages; 492 493 /** 494 * @max_pages_limit: Constrain ->max_pages to this value during 495 * feature negotiation 496 */ 497 unsigned int max_pages_limit; 498 499 /** @chan: transport layer object */ 500 struct fuse_chan *chan; 501 502 /** @khctr: The next unique kernel file handle */ 503 atomic64_t khctr; 504 505 /** 506 * @polled_files: rbtree of fuse_files waiting for poll events 507 * indexed by ph 508 */ 509 struct rb_root polled_files; 510 511 /** 512 * @congestion_threshold: Number of background requests at which 513 * congestion starts 514 */ 515 unsigned congestion_threshold; 516 517 /** 518 * @conn_error: Connection failed (version mismatch). Cannot race with 519 * setting other bitfields since it is only set once in INIT 520 * reply, before any other request, and never cleared 521 */ 522 unsigned conn_error:1; 523 524 /** @conn_init: Connection successful. Only set in INIT */ 525 unsigned conn_init:1; 526 527 /** @async_read: Do readahead asynchronously? Only set in INIT */ 528 unsigned async_read:1; 529 530 /** 531 * @abort_err: Return an unique read error after abort. 532 * Only set in INIT 533 */ 534 unsigned abort_err:1; 535 536 /** 537 * @atomic_o_trunc: Do not send separate SETATTR request before 538 * open(O_TRUNC) 539 */ 540 unsigned atomic_o_trunc:1; 541 542 /** 543 * @export_support: Filesystem supports NFS exporting. 544 * Only set in INIT 545 */ 546 unsigned export_support:1; 547 548 /** @writeback_cache: write-back cache policy (default is write-through) */ 549 unsigned writeback_cache:1; 550 551 /** 552 * @parallel_dirops: allow parallel lookups and readdir (default is 553 * serialized) 554 */ 555 unsigned parallel_dirops:1; 556 557 /** 558 * @handle_killpriv: handle fs handles killing suid/sgid/cap on 559 * write/chown/trunc 560 */ 561 unsigned handle_killpriv:1; 562 563 /** @cache_symlinks: cache READLINK responses in page cache */ 564 unsigned cache_symlinks:1; 565 566 /** @legacy_opts_show: show legacy mount options */ 567 unsigned int legacy_opts_show:1; 568 569 /** 570 * @handle_killpriv_v2: 571 * fs kills suid/sgid/cap on write/chown/trunc. suid is killed on 572 * write/trunc only if caller did not have CAP_FSETID. sgid is killed 573 * on write/truncate only if caller did not have CAP_FSETID as well as 574 * file has group execute permission. 575 */ 576 unsigned handle_killpriv_v2:1; 577 578 /* 579 * The following bitfields are only for optimization purposes 580 * and hence races in setting them will not cause malfunction 581 */ 582 583 /** @no_open: Is open/release not implemented by fs? */ 584 unsigned no_open:1; 585 586 /** @no_opendir: Is opendir/releasedir not implemented by fs? */ 587 unsigned no_opendir:1; 588 589 /** @no_fsync: Is fsync not implemented by fs? */ 590 unsigned no_fsync:1; 591 592 /** @no_fsyncdir: Is fsyncdir not implemented by fs? */ 593 unsigned no_fsyncdir:1; 594 595 /** @no_flush: Is flush not implemented by fs? */ 596 unsigned no_flush:1; 597 598 /** @no_setxattr: Is setxattr not implemented by fs? */ 599 unsigned no_setxattr:1; 600 601 /** @setxattr_ext: Does file server support extended setxattr */ 602 unsigned setxattr_ext:1; 603 604 /** @no_getxattr: Is getxattr not implemented by fs? */ 605 unsigned no_getxattr:1; 606 607 /** @no_listxattr: Is listxattr not implemented by fs? */ 608 unsigned no_listxattr:1; 609 610 /** @no_removexattr: Is removexattr not implemented by fs? */ 611 unsigned no_removexattr:1; 612 613 /** @no_lock: Are posix file locking primitives not implemented by fs? */ 614 unsigned no_lock:1; 615 616 /** @no_access: Is access not implemented by fs? */ 617 unsigned no_access:1; 618 619 /** @no_create: Is create not implemented by fs? */ 620 unsigned no_create:1; 621 622 /** @no_bmap: Is bmap not implemented by fs? */ 623 unsigned no_bmap:1; 624 625 /** @no_poll: Is poll not implemented by fs? */ 626 unsigned no_poll:1; 627 628 /** @big_writes: Do multi-page cached writes */ 629 unsigned big_writes:1; 630 631 /** @dont_mask: Don't apply umask to creation modes */ 632 unsigned dont_mask:1; 633 634 /** @no_flock: Are BSD file locking primitives not implemented by fs? */ 635 unsigned no_flock:1; 636 637 /** @no_fallocate: Is fallocate not implemented by fs? */ 638 unsigned no_fallocate:1; 639 640 /** @no_rename2: Is rename with flags implemented by fs? */ 641 unsigned no_rename2:1; 642 643 /** @auto_inval_data: Use enhanced/automatic page cache invalidation. */ 644 unsigned auto_inval_data:1; 645 646 /** 647 * @explicit_inval_data: Filesystem is fully responsible for page cache 648 * invalidation. 649 */ 650 unsigned explicit_inval_data:1; 651 652 /** @do_readdirplus: Does the filesystem support readdirplus? */ 653 unsigned do_readdirplus:1; 654 655 /** @readdirplus_auto: Does the filesystem want adaptive readdirplus? */ 656 unsigned readdirplus_auto:1; 657 658 /** 659 * @async_dio: Does the filesystem support asynchronous direct-IO 660 * submission? 661 */ 662 unsigned async_dio:1; 663 664 /** @no_lseek: Is lseek not implemented by fs? */ 665 unsigned no_lseek:1; 666 667 /** @posix_acl: Does the filesystem support posix acls? */ 668 unsigned posix_acl:1; 669 670 /** 671 * @default_permissions: Check permissions based on the file mode 672 * or not? 673 */ 674 unsigned default_permissions:1; 675 676 /** 677 * @allow_other: Allow other than the mounter user to access the 678 * filesystem ? 679 */ 680 unsigned allow_other:1; 681 682 /** @no_copy_file_range: Does the filesystem support copy_file_range? */ 683 unsigned no_copy_file_range:1; 684 685 /** 686 * @no_copy_file_range_64: Does the filesystem support 687 * copy_file_range_64? 688 */ 689 unsigned no_copy_file_range_64:1; 690 691 /** @destroy: Send DESTROY request */ 692 unsigned int destroy:1; 693 694 /** @delete_stale: Delete dentries that have gone stale */ 695 unsigned int delete_stale:1; 696 697 /** @no_control: Do not create entry in fusectl fs */ 698 unsigned int no_control:1; 699 700 /** @no_force_umount: Do not allow MNT_FORCE umount */ 701 unsigned int no_force_umount:1; 702 703 /** 704 * @no_req_timeout: Do not enforce a request timeout 705 * 706 * Set by transports that cannot revoke a request once it has been 707 * handed to the server, since the timeout is implemented by aborting 708 * the connection. 709 */ 710 unsigned int no_req_timeout:1; 711 712 /** @auto_submounts: Auto-mount submounts announced by the server */ 713 unsigned int auto_submounts:1; 714 715 /** @sync_fs: Propagate syncfs() to server */ 716 unsigned int sync_fs:1; 717 718 /** 719 * @syncfs_capable: the privilege required to honor FUSE_HAS_SYNCFS was 720 * present when /dev/fuse was opened (CAP_SYS_ADMIN in the initial user 721 * namespace), i.e. the same privilege that mounting virtiofs/fuseblk 722 * requires. 723 */ 724 unsigned int syncfs_capable:1; 725 726 /** @init_security: Initialize security xattrs when creating a new inode */ 727 unsigned int init_security:1; 728 729 /** 730 * @create_supp_group: Add supplementary group info when creating 731 * a new inode 732 */ 733 unsigned int create_supp_group:1; 734 735 /** @inode_vdax: Does the filesystem support per inode virtiofs DAX? */ 736 unsigned int inode_vdax:1; 737 738 /** @no_tmpfile: Is tmpfile not implemented by fs? */ 739 unsigned int no_tmpfile:1; 740 741 /** 742 * @direct_io_allow_mmap: Relax restrictions to allow shared mmap 743 * in FOPEN_DIRECT_IO mode 744 */ 745 unsigned int direct_io_allow_mmap:1; 746 747 /** @no_statx: Is statx not implemented by fs? */ 748 unsigned int no_statx:1; 749 750 /** @passthrough: Passthrough support for read/write IO */ 751 unsigned int passthrough:1; 752 753 /** @backing_id_64: Backing ID is 64 bit and allocated by the server */ 754 bool backing_id_64:1; 755 756 /** @use_pages_for_kvec_io: Use pages instead of pointer for kernel I/O */ 757 unsigned int use_pages_for_kvec_io:1; 758 759 /** @no_link: Is link not implemented by fs? */ 760 unsigned int no_link:1; 761 762 /** @sync_init: Is synchronous FUSE_INIT allowed? */ 763 unsigned int sync_init:1; 764 765 /** @is_virtiofs: Is this a virtio-fs connection? */ 766 unsigned int is_virtiofs:1; 767 768 /** @max_stack_depth: Maximum stack depth for passthrough backing files */ 769 int max_stack_depth; 770 771 /** @minor: Negotiated minor version */ 772 unsigned minor; 773 774 /** @entry: Entry on the fuse_conn_list */ 775 struct list_head entry; 776 777 /** @dev: Device ID from the root super block */ 778 dev_t dev; 779 780 /** @scramble_key: Key for lock owner ID scrambling */ 781 u32 scramble_key[4]; 782 783 /** @attr_version: Version counter for attribute changes */ 784 atomic64_t attr_version; 785 786 /** @evict_ctr: Version counter for evict inode */ 787 atomic64_t evict_ctr; 788 789 /** @name_max: maximum file name length */ 790 u32 name_max; 791 792 /** @release: Called on final put */ 793 void (*release)(struct fuse_conn *); 794 795 /** 796 * @killsb: Read/write semaphore to hold when accessing the sb of any 797 * fuse_mount belonging to this connection 798 */ 799 struct rw_semaphore killsb; 800 801 #ifdef CONFIG_FUSE_VDAX 802 /** @vdax_mode: Virtiofs DAX mode */ 803 enum fuse_vdax_mode vdax_mode; 804 805 /** @dax: Dax specific conn data, non-NULL if virtiofs DAX is enabled */ 806 struct fuse_conn_vdax *vdax; 807 #endif 808 809 /** @mounts: List of filesystems using this connection */ 810 struct list_head mounts; 811 812 /** @curr_bucket: New writepages go into this bucket */ 813 struct fuse_sync_bucket __rcu *curr_bucket; 814 815 #ifdef CONFIG_FUSE_PASSTHROUGH 816 /* Selected by backing_id_64 */ 817 union { 818 /** @backing_files_map: IDR for backing files ids */ 819 struct idr backing_files_map; 820 821 /** @backing_64_ht: 64 bit ID lookup hash table */ 822 struct rhashtable backing_64_ht; 823 }; 824 #endif 825 }; 826 827 /* 828 * Represents a mounted filesystem, potentially a submount. 829 * 830 * This object allows sharing a fuse_conn between separate mounts to 831 * allow submounts with dedicated superblocks and thus separate device 832 * IDs. 833 */ 834 struct fuse_mount { 835 /* Underlying (potentially shared) connection to the FUSE server */ 836 struct fuse_conn *fc; 837 838 /* 839 * Super block for this connection (fc->killsb must be held when 840 * accessing this). 841 */ 842 struct super_block *sb; 843 844 /* Entry on fc->mounts */ 845 struct list_head fc_entry; 846 struct rcu_head rcu; 847 }; 848 849 /* 850 * Empty header for FUSE opcodes without specific header needs. 851 * Used as a placeholder in args->in_args[0] for consistency 852 * across all FUSE operations, simplifying request handling. 853 */ 854 struct fuse_zero_header {}; 855 856 static inline void fuse_set_zero_arg0(struct fuse_args *args) 857 { 858 args->in_args[0].size = sizeof(struct fuse_zero_header); 859 args->in_args[0].value = NULL; 860 } 861 862 static inline struct fuse_mount *get_fuse_mount_super(struct super_block *sb) 863 { 864 return sb->s_fs_info; 865 } 866 867 static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb) 868 { 869 return get_fuse_mount_super(sb)->fc; 870 } 871 872 static inline struct fuse_mount *get_fuse_mount(struct inode *inode) 873 { 874 return get_fuse_mount_super(inode->i_sb); 875 } 876 877 static inline struct fuse_conn *get_fuse_conn(struct inode *inode) 878 { 879 return get_fuse_mount_super(inode->i_sb)->fc; 880 } 881 882 static inline struct fuse_inode *get_fuse_inode(const struct inode *inode) 883 { 884 return container_of(inode, struct fuse_inode, inode); 885 } 886 887 static inline u64 get_node_id(struct inode *inode) 888 { 889 return get_fuse_inode(inode)->nodeid; 890 } 891 892 static inline int invalid_nodeid(u64 nodeid) 893 { 894 return !nodeid || nodeid == FUSE_ROOT_ID; 895 } 896 897 static inline u64 fuse_get_attr_version(struct fuse_conn *fc) 898 { 899 return atomic64_read(&fc->attr_version); 900 } 901 902 static inline u64 fuse_get_evict_ctr(struct fuse_conn *fc) 903 { 904 return atomic64_read(&fc->evict_ctr); 905 } 906 907 static inline bool fuse_stale_inode(const struct inode *inode, int generation, 908 struct fuse_attr *attr) 909 { 910 return inode->i_generation != generation || 911 inode_wrong_type(inode, attr->mode); 912 } 913 914 static inline void fuse_make_bad(struct inode *inode) 915 { 916 set_bit(FUSE_I_BAD, &get_fuse_inode(inode)->state); 917 } 918 919 static inline bool fuse_is_bad(struct inode *inode) 920 { 921 return unlikely(test_bit(FUSE_I_BAD, &get_fuse_inode(inode)->state)); 922 } 923 924 static inline bool fuse_inode_is_exclusive(const struct inode *inode) 925 { 926 const struct fuse_inode *fi = get_fuse_inode(inode); 927 928 return test_bit(FUSE_I_EXCLUSIVE, &fi->state); 929 } 930 931 static inline struct folio **fuse_folios_alloc(unsigned int nfolios, gfp_t flags, 932 struct fuse_folio_desc **desc) 933 { 934 struct folio **folios; 935 936 folios = kzalloc(nfolios * (sizeof(struct folio *) + 937 sizeof(struct fuse_folio_desc)), flags); 938 *desc = (void *) (folios + nfolios); 939 940 return folios; 941 } 942 943 static inline void fuse_folio_descs_length_init(struct fuse_folio_desc *descs, 944 unsigned int index, 945 unsigned int nr_folios) 946 { 947 int i; 948 949 for (i = index; i < index + nr_folios; i++) 950 descs[i].length = PAGE_SIZE - descs[i].offset; 951 } 952 953 static inline void fuse_sync_bucket_dec(struct fuse_sync_bucket *bucket) 954 { 955 /* Need RCU protection to prevent use after free after the decrement */ 956 rcu_read_lock(); 957 if (atomic_dec_and_test(&bucket->count)) 958 wake_up(&bucket->waitq); 959 rcu_read_unlock(); 960 } 961 962 /** Device operations */ 963 extern const struct file_operations fuse_dev_operations; 964 965 extern const struct dentry_operations fuse_dentry_operations; 966 967 /* 968 * Get a filled in inode 969 */ 970 struct inode *fuse_iget(struct super_block *sb, u64 nodeid, 971 int generation, struct fuse_attr *attr, 972 u64 attr_valid, u64 attr_version, 973 u64 evict_ctr); 974 975 int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name, 976 struct fuse_entry_out *outarg, struct inode **inode); 977 978 /* 979 * Initialize READ or READDIR request 980 */ 981 struct fuse_io_args { 982 union { 983 struct { 984 struct fuse_read_in in; 985 u64 attr_ver; 986 } read; 987 struct { 988 struct fuse_write_in in; 989 struct fuse_write_out out; 990 bool folio_locked; 991 } write; 992 }; 993 struct fuse_args_pages ap; 994 struct fuse_io_priv *io; 995 struct fuse_file *ff; 996 }; 997 998 void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos, 999 size_t count, int opcode); 1000 1001 1002 struct fuse_file *fuse_file_alloc(struct fuse_mount *fm, bool release); 1003 void fuse_file_free(struct fuse_file *ff); 1004 int fuse_finish_open(struct inode *inode, struct file *file); 1005 1006 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, 1007 unsigned int flags); 1008 1009 /* 1010 * Send RELEASE or RELEASEDIR request 1011 */ 1012 void fuse_release_common(struct file *file, bool isdir); 1013 1014 /* 1015 * Send FSYNC or FSYNCDIR request 1016 */ 1017 int fuse_fsync_common(struct file *file, loff_t start, loff_t end, 1018 int datasync, int opcode); 1019 1020 /* 1021 * Notify poll wakeup 1022 */ 1023 int fuse_notify_poll_wakeup(struct fuse_conn *fc, 1024 struct fuse_notify_poll_wakeup_out *outarg); 1025 1026 /* 1027 * Initialize file operations on a regular file 1028 */ 1029 void fuse_init_file_inode(struct inode *inode, unsigned int flags); 1030 1031 /* 1032 * Initialize inode operations on regular files and special files 1033 */ 1034 void fuse_init_common(struct inode *inode); 1035 1036 /* 1037 * Initialize inode and file operations on a directory 1038 */ 1039 void fuse_init_dir(struct inode *inode); 1040 1041 /* 1042 * Initialize inode operations on a symlink 1043 */ 1044 void fuse_init_symlink(struct inode *inode); 1045 1046 /* 1047 * Change attributes of an inode 1048 */ 1049 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr, 1050 struct fuse_statx *sx, 1051 u64 attr_valid, u64 attr_version); 1052 1053 void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr, 1054 struct fuse_statx *sx, 1055 u64 attr_valid, u32 cache_mask, 1056 u64 evict_ctr); 1057 1058 u32 fuse_get_cache_mask(struct inode *inode); 1059 1060 int fuse_ctl_init(void); 1061 void __exit fuse_ctl_cleanup(void); 1062 1063 /* 1064 * Simple request sending that does request allocation and freeing 1065 */ 1066 ssize_t __fuse_simple_request(const struct mnt_idmap *idmap, 1067 struct fuse_mount *fm, 1068 struct fuse_args *args); 1069 1070 static inline ssize_t fuse_simple_request(struct fuse_mount *fm, struct fuse_args *args) 1071 { 1072 return __fuse_simple_request(&invalid_mnt_idmap, fm, args); 1073 } 1074 1075 static inline ssize_t fuse_simple_idmap_request(const struct mnt_idmap *idmap, 1076 struct fuse_mount *fm, 1077 struct fuse_args *args) 1078 { 1079 return __fuse_simple_request(idmap, fm, args); 1080 } 1081 1082 int fuse_simple_background(struct fuse_mount *fm, struct fuse_args *args, 1083 gfp_t gfp_flags); 1084 int fuse_simple_notify_reply(struct fuse_mount *fm, struct fuse_args *args, u64 unique); 1085 1086 void fuse_dentry_tree_init(void); 1087 void fuse_dentry_tree_cleanup(void); 1088 1089 void fuse_epoch_work(struct work_struct *work); 1090 1091 /* 1092 * Invalidate inode attributes 1093 */ 1094 1095 /* Attributes possibly changed on data modification */ 1096 #define FUSE_STATX_MODIFY (STATX_MTIME | STATX_CTIME | STATX_BLOCKS) 1097 1098 /* Attributes possibly changed on data and/or size modification */ 1099 #define FUSE_STATX_MODSIZE (FUSE_STATX_MODIFY | STATX_SIZE) 1100 1101 /* Attributes possibly changed on directory modification */ 1102 #define FUSE_STATX_MODDIR (FUSE_STATX_MODSIZE | STATX_NLINK) 1103 1104 void fuse_invalidate_attr(struct inode *inode); 1105 void fuse_invalidate_attr_mask(struct inode *inode, u32 mask); 1106 1107 void fuse_invalidate_entry_cache(struct dentry *entry); 1108 1109 void fuse_invalidate_atime(struct inode *inode); 1110 1111 u64 fuse_time_to_jiffies(u64 sec, u32 nsec); 1112 #define ATTR_TIMEOUT(o) \ 1113 fuse_time_to_jiffies((o)->attr_valid, (o)->attr_valid_nsec) 1114 1115 void fuse_change_entry_timeout(struct dentry *entry, struct fuse_entry_out *o); 1116 1117 void fuse_dentry_set_epoch(struct dentry *dentry, u64 epoch); 1118 1119 /* 1120 * Initialize fuse_conn 1121 */ 1122 void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm, 1123 struct user_namespace *user_ns, struct fuse_chan *fch); 1124 1125 int fuse_send_init(struct fuse_mount *fm); 1126 1127 /** 1128 * fuse_fill_super_common - Fill in superblock and initialize fuse connection 1129 * @sb: partially-initialized superblock to fill in 1130 * @ctx: mount context 1131 */ 1132 int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx); 1133 1134 /** 1135 * fuse_mount_remove - Remove the mount from the connection 1136 * @fm: fuse_mount to remove 1137 * 1138 * Returns: whether this was the last mount 1139 */ 1140 bool fuse_mount_remove(struct fuse_mount *fm); 1141 1142 /* 1143 * Setup context ops for submounts 1144 */ 1145 int fuse_init_fs_context_submount(struct fs_context *fsc); 1146 1147 /* 1148 * Shut down the connection (possibly sending DESTROY request). 1149 */ 1150 void fuse_conn_destroy(struct fuse_mount *fm); 1151 1152 /* Drop the connection and free the fuse mount */ 1153 void fuse_mount_destroy(struct fuse_mount *fm); 1154 1155 /** 1156 * fuse_ctl_add_conn - Add connection to control filesystem 1157 * @fc: Fuse connection to add 1158 */ 1159 int fuse_ctl_add_conn(struct fuse_conn *fc); 1160 1161 /** 1162 * fuse_ctl_remove_conn - Remove connection from control filesystem 1163 * @fc: Fuse connection to remove 1164 */ 1165 void fuse_ctl_remove_conn(struct fuse_conn *fc); 1166 1167 /* 1168 * Is file type valid? 1169 */ 1170 int fuse_valid_type(int m); 1171 1172 bool fuse_invalid_attr(struct fuse_attr *attr); 1173 1174 /* 1175 * Is current process allowed to perform filesystem operation? 1176 */ 1177 bool fuse_allow_current_process(struct fuse_conn *fc); 1178 1179 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id); 1180 1181 void fuse_flush_time_update(struct inode *inode); 1182 void fuse_update_ctime(struct inode *inode); 1183 1184 int fuse_update_attributes(struct inode *inode, struct file *file, u32 mask); 1185 1186 void fuse_flush_writepages(struct inode *inode); 1187 1188 void fuse_set_nowrite(struct inode *inode); 1189 void fuse_release_nowrite(struct inode *inode); 1190 1191 /* 1192 * Scan all fuse_mounts belonging to fc to find the first where 1193 * ilookup5() returns a result. Return that result and the 1194 * respective fuse_mount in *fm (unless fm is NULL). 1195 * 1196 * The caller must hold fc->killsb. 1197 */ 1198 struct inode *fuse_ilookup(struct fuse_conn *fc, u64 nodeid, 1199 struct fuse_mount **fm); 1200 1201 /* 1202 * File-system tells the kernel to invalidate cache for the given node id. 1203 */ 1204 int fuse_reverse_inval_inode(struct fuse_conn *fc, u64 nodeid, 1205 loff_t offset, loff_t len); 1206 1207 /* 1208 * File-system tells the kernel to invalidate parent attributes and 1209 * the dentry matching parent/name. 1210 * 1211 * If the child_nodeid is non-zero and: 1212 * - matches the inode number for the dentry matching parent/name, 1213 * - is not a mount point 1214 * - is a file or oan empty directory 1215 * then the dentry is unhashed (d_delete()). 1216 */ 1217 int fuse_reverse_inval_entry(struct fuse_conn *fc, u64 parent_nodeid, 1218 u64 child_nodeid, struct qstr *name, u32 flags); 1219 1220 /* 1221 * Try to prune this inode. If neither the inode itself nor dentries associated 1222 * with this inode have any external reference, then the inode can be freed. 1223 */ 1224 void fuse_try_prune_one_inode(struct fuse_conn *fc, u64 nodeid); 1225 1226 int fuse_do_open(struct fuse_mount *fm, u64 nodeid, struct file *file, 1227 bool isdir); 1228 1229 /* 1230 * fuse_do_direct_io() flags 1231 */ 1232 1233 /** If set, it is WRITE; otherwise - READ */ 1234 #define FUSE_DIO_WRITE (1 << 0) 1235 1236 /** CUSE pass fuse_do_direct_io() a file which f_mapping->host is not from FUSE */ 1237 #define FUSE_DIO_CUSE (1 << 1) 1238 1239 ssize_t fuse_do_direct_io(struct fuse_io_priv *io, struct iov_iter *iter, 1240 loff_t *ppos, int flags); 1241 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg, 1242 unsigned int flags); 1243 long fuse_ioctl_common(struct file *file, unsigned int cmd, 1244 unsigned long arg, unsigned int flags); 1245 __poll_t fuse_file_poll(struct file *file, poll_table *wait); 1246 1247 bool fuse_write_update_attr(struct inode *inode, loff_t pos, ssize_t written); 1248 1249 int fuse_flush_times(struct inode *inode, struct fuse_file *ff); 1250 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc); 1251 1252 int fuse_do_setattr(const struct mnt_idmap *idmap, struct dentry *dentry, 1253 struct iattr *attr, struct file *file); 1254 1255 void fuse_unlock_inode(struct inode *inode, bool locked); 1256 bool fuse_lock_inode(struct inode *inode); 1257 1258 int fuse_setxattr(struct inode *inode, const char *name, const void *value, 1259 size_t size, int flags, unsigned int extra_flags); 1260 ssize_t fuse_getxattr(struct inode *inode, const char *name, void *value, 1261 size_t size); 1262 ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size); 1263 int fuse_removexattr(struct inode *inode, const char *name); 1264 extern const struct xattr_handler * const fuse_xattr_handlers[]; 1265 1266 struct posix_acl; 1267 struct posix_acl *fuse_get_inode_acl(struct inode *inode, int type, bool rcu); 1268 struct posix_acl *fuse_get_acl(const struct mnt_idmap *idmap, 1269 struct dentry *dentry, int type); 1270 int fuse_set_acl(const struct mnt_idmap *, struct dentry *dentry, 1271 struct posix_acl *acl, int type); 1272 1273 /* readdir.c */ 1274 int fuse_readdir(struct file *file, struct dir_context *ctx); 1275 1276 void fuse_free_conn(struct fuse_conn *fc); 1277 1278 /* dax.c */ 1279 1280 static inline bool fuse_conn_vdax(struct fuse_conn *fc) 1281 { 1282 #ifdef CONFIG_FUSE_VDAX 1283 return fc->vdax; 1284 #else 1285 return false; 1286 #endif 1287 } 1288 1289 static inline bool fuse_inode_vdax(struct inode *inode) 1290 { 1291 #ifdef CONFIG_FUSE_VDAX 1292 return get_fuse_inode(inode)->vdax; 1293 #else 1294 return false; 1295 #endif 1296 } 1297 1298 static inline bool FUSE_IS_VDAX(struct inode *inode) 1299 { 1300 return fuse_inode_vdax(inode) && IS_DAX(inode); 1301 } 1302 1303 ssize_t fuse_vdax_read_iter(struct kiocb *iocb, struct iov_iter *to); 1304 ssize_t fuse_vdax_write_iter(struct kiocb *iocb, struct iov_iter *from); 1305 int fuse_vdax_mmap(struct file *file, struct vm_area_struct *vma); 1306 int fuse_vdax_break_layouts(struct inode *inode, u64 dmap_start, u64 dmap_end); 1307 int fuse_vdax_conn_alloc(struct fuse_conn *fc, enum fuse_vdax_mode mode, 1308 struct dax_device *vdax_dev); 1309 void fuse_vdax_conn_free(struct fuse_conn *fc); 1310 bool fuse_vdax_inode_alloc(struct super_block *sb, struct fuse_inode *fi); 1311 void fuse_vdax_inode_init(struct inode *inode, unsigned int flags); 1312 void fuse_vdax_inode_cleanup(struct inode *inode); 1313 void fuse_vdax_dontcache(struct inode *inode, unsigned int flags); 1314 bool fuse_vdax_check_alignment(struct fuse_conn *fc, unsigned int map_alignment); 1315 void fuse_vdax_cancel_work(struct fuse_conn *fc); 1316 1317 /* ioctl.c */ 1318 long fuse_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 1319 long fuse_file_compat_ioctl(struct file *file, unsigned int cmd, 1320 unsigned long arg); 1321 int fuse_fileattr_get(struct dentry *dentry, struct file_kattr *fa); 1322 int fuse_fileattr_set(const struct mnt_idmap *idmap, 1323 struct dentry *dentry, struct file_kattr *fa); 1324 1325 /* iomode.c */ 1326 bool fuse_file_cached_io_open(struct inode *inode, struct fuse_file *ff); 1327 int fuse_inode_uncached_io_start(struct fuse_inode *fi, 1328 struct fuse_backing *fb); 1329 void fuse_inode_uncached_io_end(struct fuse_inode *fi); 1330 1331 int fuse_file_io_open(struct file *file, struct inode *inode); 1332 void fuse_file_io_release(struct fuse_file *ff, struct inode *inode); 1333 1334 /* file.c */ 1335 struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid, 1336 unsigned int open_flags, bool isdir); 1337 void fuse_file_release(struct inode *inode, struct fuse_file *ff, 1338 unsigned int open_flags, fl_owner_t id, bool isdir); 1339 1340 /* backing.c */ 1341 #ifdef CONFIG_FUSE_PASSTHROUGH 1342 void fuse_backing_put(struct fuse_backing *fb); 1343 #else 1344 1345 static inline void fuse_backing_put(struct fuse_backing *fb) 1346 { 1347 } 1348 #endif 1349 1350 struct fuse_backing *fuse_backing_lookup(struct fuse_conn *fc, u64 backing_id); 1351 int fuse_backing_add_64(struct fuse_conn *fc, struct fuse_backing *fb); 1352 bool fuse_backing_is_dax(struct fuse_backing *fb); 1353 void fuse_backing_files_init(struct fuse_conn *fc); 1354 void fuse_backing_files_init_64(struct fuse_conn *fc); 1355 void fuse_backing_files_free(struct fuse_conn *fc); 1356 1357 /* passthrough.c */ 1358 static inline struct fuse_backing *fuse_inode_backing(struct fuse_inode *fi) 1359 { 1360 #ifdef CONFIG_FUSE_PASSTHROUGH 1361 return READ_ONCE(fi->fb); 1362 #else 1363 return NULL; 1364 #endif 1365 } 1366 1367 static inline struct fuse_backing *fuse_inode_backing_set(struct fuse_inode *fi, 1368 struct fuse_backing *fb) 1369 { 1370 #ifdef CONFIG_FUSE_PASSTHROUGH 1371 return xchg(&fi->fb, fb); 1372 #else 1373 return NULL; 1374 #endif 1375 } 1376 1377 int fuse_passthrough_open(struct file *file, struct fuse_backing *fb); 1378 void fuse_passthrough_release(struct fuse_file *ff, struct fuse_backing *fb); 1379 1380 static inline bool fuse_is_passthrough(struct fuse_file *ff) 1381 { 1382 return IS_ENABLED(CONFIG_FUSE_PASSTHROUGH) && (ff->open_flags & FOPEN_PASSTHROUGH); 1383 } 1384 1385 ssize_t fuse_passthrough_read_iter(struct kiocb *iocb, struct iov_iter *iter); 1386 ssize_t fuse_passthrough_write_iter(struct kiocb *iocb, struct iov_iter *iter); 1387 ssize_t fuse_passthrough_splice_read(struct file *in, loff_t *ppos, 1388 struct pipe_inode_info *pipe, 1389 size_t len, unsigned int flags); 1390 ssize_t fuse_passthrough_splice_write(struct pipe_inode_info *pipe, 1391 struct file *out, loff_t *ppos, 1392 size_t len, unsigned int flags); 1393 ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma); 1394 int fuse_passthrough_fadvise(struct file *file, loff_t offset, loff_t len, int advice); 1395 1396 #ifdef CONFIG_SYSCTL 1397 extern int fuse_sysctl_register(void); 1398 extern void fuse_sysctl_unregister(void); 1399 #else 1400 #define fuse_sysctl_register() (0) 1401 #define fuse_sysctl_unregister() do { } while (0) 1402 #endif /* CONFIG_SYSCTL */ 1403 1404 /* ext_map.c */ 1405 1406 void fuse_ext_map_destroy(struct fuse_backing *fb); 1407 ssize_t fuse_ext_map_write_iter(struct kiocb *iocb, struct iov_iter *from); 1408 ssize_t fuse_ext_map_read_iter(struct kiocb *iocb, struct iov_iter *to); 1409 int fuse_ext_map_mmap(struct file *file, struct vm_area_struct *vma); 1410 bool fuse_ext_map_is_dax(struct fuse_backing *fb); 1411 int fuse_ext_map_populate(struct fuse_conn *fc, struct fuse_notify_backing_map_out *arg, 1412 struct fuse_extent *ext); 1413 #endif /* _FS_FUSE_I_H */ 1414