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