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