• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/android_fuse.h>
17 #include <linux/filter.h>
18 #include <linux/pagemap.h>
19 #include <linux/fuse.h>
20 #include <linux/fs.h>
21 #include <linux/mount.h>
22 #include <linux/wait.h>
23 #include <linux/list.h>
24 #include <linux/spinlock.h>
25 #include <linux/mm.h>
26 #include <linux/backing-dev.h>
27 #include <linux/mutex.h>
28 #include <linux/rwsem.h>
29 #include <linux/rbtree.h>
30 #include <linux/poll.h>
31 #include <linux/workqueue.h>
32 #include <linux/kref.h>
33 #include <linux/xattr.h>
34 #include <linux/pid_namespace.h>
35 #include <linux/refcount.h>
36 #include <linux/user_namespace.h>
37 #include <linux/statfs.h>
38 
39 #define FUSE_SUPER_MAGIC 0x65735546
40 
41 /** Default max number of pages that can be used in a single read request */
42 #define FUSE_DEFAULT_MAX_PAGES_PER_REQ 32
43 
44 /** Maximum of max_pages received in init_out */
45 #define FUSE_MAX_MAX_PAGES 256
46 
47 /** Bias for fi->writectr, meaning new writepages must not be sent */
48 #define FUSE_NOWRITE INT_MIN
49 
50 /** It could be as large as PATH_MAX, but would that have any uses? */
51 #define FUSE_NAME_MAX 1024
52 
53 /** Number of dentries for each connection in the control filesystem */
54 #define FUSE_CTL_NUM_DENTRIES 5
55 
56 /** List of active connections */
57 extern struct list_head fuse_conn_list;
58 
59 /** Global mutex protecting fuse_conn_list and the control filesystem */
60 extern struct mutex fuse_mutex;
61 
62 /** Module parameters */
63 extern unsigned max_user_bgreq;
64 extern unsigned max_user_congthresh;
65 
66 /* One forget request */
67 struct fuse_forget_link {
68 	struct fuse_forget_one forget_one;
69 	struct fuse_forget_link *next;
70 };
71 
72 
73 /* Submount lookup tracking */
74 struct fuse_submount_lookup {
75 	/** Refcount */
76 	refcount_t count;
77 
78 	/** Unique ID, which identifies the inode between userspace
79 	 * and kernel */
80 	u64 nodeid;
81 
82 	/** The request used for sending the FORGET message */
83 	struct fuse_forget_link *forget;
84 };
85 
86 /** FUSE specific dentry data */
87 #if BITS_PER_LONG < 64 || defined(CONFIG_FUSE_BPF)
88 struct fuse_dentry {
89 	union {
90 		u64 time;
91 		struct rcu_head rcu;
92 	};
93 
94 #ifdef CONFIG_FUSE_BPF
95 	struct path backing_path;
96 
97 	/* bpf program *only* set for negative dentries */
98 	struct bpf_prog *bpf;
99 #endif
100 };
101 
get_fuse_dentry(const struct dentry * entry)102 static inline struct fuse_dentry *get_fuse_dentry(const struct dentry *entry)
103 {
104 	return entry->d_fsdata;
105 }
106 #endif
107 
108 #ifdef CONFIG_FUSE_BPF
get_fuse_backing_path(const struct dentry * d,struct path * path)109 static inline void get_fuse_backing_path(const struct dentry *d,
110 					  struct path *path)
111 {
112 	struct fuse_dentry *di = get_fuse_dentry(d);
113 
114 	if (!di) {
115 		*path = (struct path) {};
116 		return;
117 	}
118 
119 	*path = di->backing_path;
120 	path_get(path);
121 }
122 #endif
123 
124 /** FUSE inode */
125 struct fuse_inode {
126 	/** Inode data */
127 	struct inode inode;
128 
129 #ifdef CONFIG_FUSE_BPF
130 	/**
131 	 * Backing inode, if this inode is from a backing file system.
132 	 * If this is set, nodeid is 0.
133 	 */
134 	struct inode *backing_inode;
135 
136 	/**
137 	 * bpf_prog, run on all operations to determine whether to pass through
138 	 * or handle in place
139 	 */
140 	struct bpf_prog *bpf;
141 #endif
142 
143 	/** Unique ID, which identifies the inode between userspace
144 	 * and kernel */
145 	u64 nodeid;
146 
147 	/** Number of lookups on this inode */
148 	u64 nlookup;
149 
150 	/** The request used for sending the FORGET message */
151 	struct fuse_forget_link *forget;
152 
153 	/** Time in jiffies until the file attributes are valid */
154 	u64 i_time;
155 
156 	/* Which attributes are invalid */
157 	u32 inval_mask;
158 
159 	/** The sticky bit in inode->i_mode may have been removed, so
160 	    preserve the original mode */
161 	umode_t orig_i_mode;
162 
163 	/* Cache birthtime */
164 	struct timespec64 i_btime;
165 
166 	/** 64 bit inode number */
167 	u64 orig_ino;
168 
169 	/** Version of last attribute change */
170 	u64 attr_version;
171 
172 	union {
173 		/* Write related fields (regular file only) */
174 		struct {
175 			/* Files usable in writepage.  Protected by fi->lock */
176 			struct list_head write_files;
177 
178 			/* Writepages pending on truncate or fsync */
179 			struct list_head queued_writes;
180 
181 			/* Number of sent writes, a negative bias
182 			 * (FUSE_NOWRITE) means more writes are blocked */
183 			int writectr;
184 
185 			/* Waitq for writepage completion */
186 			wait_queue_head_t page_waitq;
187 
188 			/* List of writepage requestst (pending or sent) */
189 			struct rb_root writepages;
190 		};
191 
192 		/* readdir cache (directory only) */
193 		struct {
194 			/* true if fully cached */
195 			bool cached;
196 
197 			/* size of cache */
198 			loff_t size;
199 
200 			/* position at end of cache (position of next entry) */
201 			loff_t pos;
202 
203 			/* version of the cache */
204 			u64 version;
205 
206 			/* modification time of directory when cache was
207 			 * started */
208 			struct timespec64 mtime;
209 
210 			/* iversion of directory when cache was started */
211 			u64 iversion;
212 
213 			/* protects above fields */
214 			spinlock_t lock;
215 		} rdc;
216 	};
217 
218 	/** Miscellaneous bits describing inode state */
219 	unsigned long state;
220 
221 	/** Lock for serializing lookup and readdir for back compatibility*/
222 	struct mutex mutex;
223 
224 	/** Lock to protect write related fields */
225 	spinlock_t lock;
226 
227 #ifdef CONFIG_FUSE_DAX
228 	/*
229 	 * Dax specific inode data
230 	 */
231 	struct fuse_inode_dax *dax;
232 #endif
233 	/** Submount specific lookup tracking */
234 	struct fuse_submount_lookup *submount_lookup;
235 };
236 
237 /** FUSE inode state bits */
238 enum {
239 	/** Advise readdirplus  */
240 	FUSE_I_ADVISE_RDPLUS,
241 	/** Initialized with readdirplus */
242 	FUSE_I_INIT_RDPLUS,
243 	/** An operation changing file size is in progress  */
244 	FUSE_I_SIZE_UNSTABLE,
245 	/* Bad inode */
246 	FUSE_I_BAD,
247 	/* Has btime */
248 	FUSE_I_BTIME,
249 };
250 
251 struct fuse_conn;
252 struct fuse_mount;
253 struct fuse_release_args;
254 
255 /**
256  * Reference to lower filesystem file for read/write operations handled in
257  * passthrough mode.
258  * This struct also tracks the credentials to be used for handling read/write
259  * operations.
260  */
261 struct fuse_passthrough {
262 	struct file *filp;
263 	struct cred *cred;
264 };
265 
266 /** FUSE specific file data */
267 struct fuse_file {
268 	/** Fuse connection for this file */
269 	struct fuse_mount *fm;
270 
271 	/* Argument space reserved for release */
272 	struct fuse_release_args *release_args;
273 
274 	/** Kernel file handle guaranteed to be unique */
275 	u64 kh;
276 
277 	/** File handle used by userspace */
278 	u64 fh;
279 
280 	/** Node id of this file */
281 	u64 nodeid;
282 
283 	/** Refcount */
284 	refcount_t count;
285 
286 	/** FOPEN_* flags returned by open */
287 	u32 open_flags;
288 
289 	/** Entry on inode's write_files list */
290 	struct list_head write_entry;
291 
292 	/* Readdir related */
293 	struct {
294 		/*
295 		 * Protects below fields against (crazy) parallel readdir on
296 		 * same open file.  Uncontended in the normal case.
297 		 */
298 		struct mutex lock;
299 
300 		/* Dir stream position */
301 		loff_t pos;
302 
303 		/* Offset in cache */
304 		loff_t cache_off;
305 
306 		/* Version of cache we are reading */
307 		u64 version;
308 
309 	} readdir;
310 
311 	/** Container for data related to the passthrough functionality */
312 	struct fuse_passthrough passthrough;
313 
314 #ifdef CONFIG_FUSE_BPF
315 	/**
316 	 * TODO: Reconcile with passthrough file
317 	 * backing file when in bpf mode
318 	 */
319 	struct file *backing_file;
320 #endif
321 
322 	/** RB node to be linked on fuse_conn->polled_files */
323 	struct rb_node polled_node;
324 
325 	/** Wait queue head for poll */
326 	wait_queue_head_t poll_wait;
327 
328 	/** Has flock been performed on this file? */
329 	bool flock:1;
330 };
331 
332 /** One input argument of a request */
333 struct fuse_in_arg {
334 	unsigned size;
335 	const void *value;
336 };
337 
338 /** One output argument of a request */
339 struct fuse_arg {
340 	unsigned size;
341 	void *value;
342 };
343 
344 /** FUSE page descriptor */
345 struct fuse_page_desc {
346 	unsigned int length;
347 	unsigned int offset;
348 };
349 
350 struct fuse_args {
351 	uint64_t nodeid;
352 	uint32_t opcode;
353 	uint32_t error_in;
354 	uint8_t in_numargs;
355 	uint8_t out_numargs;
356 	uint8_t ext_idx;
357 	bool force:1;
358 	bool noreply:1;
359 	bool nocreds:1;
360 	bool in_pages:1;
361 	bool out_pages:1;
362 	bool user_pages:1;
363 	bool out_argvar:1;
364 	bool page_zeroing:1;
365 	bool page_replace:1;
366 	bool may_block:1;
367 	bool is_ext:1;
368 	struct fuse_in_arg in_args[FUSE_MAX_IN_ARGS];
369 	struct fuse_arg out_args[FUSE_MAX_OUT_ARGS];
370 	void (*end)(struct fuse_mount *fm, struct fuse_args *args, int error);
371 
372 	/* Path used for completing d_canonical_path */
373 	struct path *canonical_path;
374 };
375 
376 struct fuse_args_pages {
377 	struct fuse_args args;
378 	struct page **pages;
379 	struct fuse_page_desc *descs;
380 	unsigned int num_pages;
381 };
382 
383 #define FUSE_ARGS(args) struct fuse_args args = {}
384 
385 /** The request IO state (for asynchronous processing) */
386 struct fuse_io_priv {
387 	struct kref refcnt;
388 	int async;
389 	spinlock_t lock;
390 	unsigned reqs;
391 	ssize_t bytes;
392 	size_t size;
393 	__u64 offset;
394 	bool write;
395 	bool should_dirty;
396 	int err;
397 	struct kiocb *iocb;
398 	struct completion *done;
399 	bool blocking;
400 };
401 
402 #define FUSE_IO_PRIV_SYNC(i) \
403 {					\
404 	.refcnt = KREF_INIT(1),		\
405 	.async = 0,			\
406 	.iocb = i,			\
407 }
408 
409 /**
410  * Request flags
411  *
412  * FR_ISREPLY:		set if the request has reply
413  * FR_FORCE:		force sending of the request even if interrupted
414  * FR_BACKGROUND:	request is sent in the background
415  * FR_WAITING:		request is counted as "waiting"
416  * FR_ABORTED:		the request was aborted
417  * FR_INTERRUPTED:	the request has been interrupted
418  * FR_LOCKED:		data is being copied to/from the request
419  * FR_PENDING:		request is not yet in userspace
420  * FR_SENT:		request is in userspace, waiting for an answer
421  * FR_FINISHED:		request is finished
422  * FR_PRIVATE:		request is on private list
423  * FR_ASYNC:		request is asynchronous
424  */
425 enum fuse_req_flag {
426 	FR_ISREPLY,
427 	FR_FORCE,
428 	FR_BACKGROUND,
429 	FR_WAITING,
430 	FR_ABORTED,
431 	FR_INTERRUPTED,
432 	FR_LOCKED,
433 	FR_PENDING,
434 	FR_SENT,
435 	FR_FINISHED,
436 	FR_PRIVATE,
437 	FR_ASYNC,
438 };
439 
440 /**
441  * A request to the client
442  *
443  * .waitq.lock protects the following fields:
444  *   - FR_ABORTED
445  *   - FR_LOCKED (may also be modified under fc->lock, tested under both)
446  */
447 struct fuse_req {
448 	/** This can be on either pending processing or io lists in
449 	    fuse_conn */
450 	struct list_head list;
451 
452 	/** Entry on the interrupts list  */
453 	struct list_head intr_entry;
454 
455 	/* Input/output arguments */
456 	struct fuse_args *args;
457 
458 	/** refcount */
459 	refcount_t count;
460 
461 	/* Request flags, updated with test/set/clear_bit() */
462 	unsigned long flags;
463 
464 	/* The request input header */
465 	struct {
466 		struct fuse_in_header h;
467 	} in;
468 
469 	/* The request output header */
470 	struct {
471 		struct fuse_out_header h;
472 	} out;
473 
474 	/** Used to wake up the task waiting for completion of request*/
475 	wait_queue_head_t waitq;
476 
477 	/** virtio-fs's physically contiguous buffer for in and out args */
478 	void *argbuf;
479 
480 	/** fuse_mount this request belongs to */
481 	struct fuse_mount *fm;
482 };
483 
484 struct fuse_iqueue;
485 
486 /**
487  * Input queue callbacks
488  *
489  * Input queue signalling is device-specific.  For example, the /dev/fuse file
490  * uses fiq->waitq and fasync to wake processes that are waiting on queue
491  * readiness.  These callbacks allow other device types to respond to input
492  * queue activity.
493  */
494 struct fuse_iqueue_ops {
495 	/**
496 	 * Signal that a forget has been queued
497 	 */
498 	void (*wake_forget_and_unlock)(struct fuse_iqueue *fiq, bool sync)
499 		__releases(fiq->lock);
500 
501 	/**
502 	 * Signal that an INTERRUPT request has been queued
503 	 */
504 	void (*wake_interrupt_and_unlock)(struct fuse_iqueue *fiq, bool sync)
505 		__releases(fiq->lock);
506 
507 	/**
508 	 * Signal that a request has been queued
509 	 */
510 	void (*wake_pending_and_unlock)(struct fuse_iqueue *fiq, bool sync)
511 		__releases(fiq->lock);
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 
fuse_is_inode_dax_mode(enum fuse_dax_mode mode)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 	bool no_daemon:1;
620 	unsigned int max_read;
621 	unsigned int blksize;
622 	const char *subtype;
623 	struct bpf_prog *root_bpf;
624 	struct file *root_dir;
625 
626 	/* DAX device, may be NULL */
627 	struct dax_device *dax_dev;
628 
629 	/* fuse_dev pointer to fill in, should contain NULL on entry */
630 	void **fudptr;
631 };
632 
633 struct fuse_sync_bucket {
634 	/* count is a possible scalability bottleneck */
635 	atomic_t count;
636 	wait_queue_head_t waitq;
637 	struct rcu_head rcu;
638 };
639 
640 /**
641  * A Fuse connection.
642  *
643  * This structure is created, when the root filesystem is mounted, and
644  * is destroyed, when the client device is closed and the last
645  * fuse_mount is destroyed.
646  */
647 struct fuse_conn {
648 	/** Lock protecting accessess to  members of this structure */
649 	spinlock_t lock;
650 
651 	/** Refcount */
652 	refcount_t count;
653 
654 	/** Number of fuse_dev's */
655 	atomic_t dev_count;
656 
657 	struct rcu_head rcu;
658 
659 	/** The user id for this mount */
660 	kuid_t user_id;
661 
662 	/** The group id for this mount */
663 	kgid_t group_id;
664 
665 	/** The pid namespace for this mount */
666 	struct pid_namespace *pid_ns;
667 
668 	/** The user namespace for this mount */
669 	struct user_namespace *user_ns;
670 
671 	/** Maximum read size */
672 	unsigned max_read;
673 
674 	/** Maximum write size */
675 	unsigned max_write;
676 
677 	/** Maximum number of pages that can be used in a single request */
678 	unsigned int max_pages;
679 
680 	/** Constrain ->max_pages to this value during feature negotiation */
681 	unsigned int max_pages_limit;
682 
683 	/** Input queue */
684 	struct fuse_iqueue iq;
685 
686 	/** The next unique kernel file handle */
687 	atomic64_t khctr;
688 
689 	/** rbtree of fuse_files waiting for poll events indexed by ph */
690 	struct rb_root polled_files;
691 
692 	/** Maximum number of outstanding background requests */
693 	unsigned max_background;
694 
695 	/** Number of background requests at which congestion starts */
696 	unsigned congestion_threshold;
697 
698 	/** Number of requests currently in the background */
699 	unsigned num_background;
700 
701 	/** Number of background requests currently queued for userspace */
702 	unsigned active_background;
703 
704 	/** The list of background requests set aside for later queuing */
705 	struct list_head bg_queue;
706 
707 	/** Protects: max_background, congestion_threshold, num_background,
708 	 * active_background, bg_queue, blocked */
709 	spinlock_t bg_lock;
710 
711 	/** Flag indicating that INIT reply has been received. Allocating
712 	 * any fuse request will be suspended until the flag is set */
713 	int initialized;
714 
715 	/** Flag indicating if connection is blocked.  This will be
716 	    the case before the INIT reply is received, and if there
717 	    are too many outstading backgrounds requests */
718 	int blocked;
719 
720 	/** waitq for blocked connection */
721 	wait_queue_head_t blocked_waitq;
722 
723 	/** Connection established, cleared on umount, connection
724 	    abort and device release */
725 	unsigned connected;
726 
727 	/** Connection aborted via sysfs */
728 	bool aborted;
729 
730 	/** Connection failed (version mismatch).  Cannot race with
731 	    setting other bitfields since it is only set once in INIT
732 	    reply, before any other request, and never cleared */
733 	unsigned conn_error:1;
734 
735 	/** Connection successful.  Only set in INIT */
736 	unsigned conn_init:1;
737 
738 	/** Do readahead asynchronously?  Only set in INIT */
739 	unsigned async_read:1;
740 
741 	/** Return an unique read error after abort.  Only set in INIT */
742 	unsigned abort_err:1;
743 
744 	/** Do not send separate SETATTR request before open(O_TRUNC)  */
745 	unsigned atomic_o_trunc:1;
746 
747 	/** Filesystem supports NFS exporting.  Only set in INIT */
748 	unsigned export_support:1;
749 
750 	/** write-back cache policy (default is write-through) */
751 	unsigned writeback_cache:1;
752 
753 	/** allow parallel lookups and readdir (default is serialized) */
754 	unsigned parallel_dirops:1;
755 
756 	/** handle fs handles killing suid/sgid/cap on write/chown/trunc */
757 	unsigned handle_killpriv:1;
758 
759 	/** cache READLINK responses in page cache */
760 	unsigned cache_symlinks:1;
761 
762 	/* show legacy mount options */
763 	unsigned int legacy_opts_show:1;
764 
765 	/*
766 	 * fs kills suid/sgid/cap on write/chown/trunc. suid is killed on
767 	 * write/trunc only if caller did not have CAP_FSETID.  sgid is killed
768 	 * on write/truncate only if caller did not have CAP_FSETID as well as
769 	 * file has group execute permission.
770 	 */
771 	unsigned handle_killpriv_v2:1;
772 
773 	/*
774 	 * The following bitfields are only for optimization purposes
775 	 * and hence races in setting them will not cause malfunction
776 	 */
777 
778 	/** Is open/release not implemented by fs? */
779 	unsigned no_open:1;
780 
781 	/** Is opendir/releasedir not implemented by fs? */
782 	unsigned no_opendir:1;
783 
784 	/** Is fsync not implemented by fs? */
785 	unsigned no_fsync:1;
786 
787 	/** Is fsyncdir not implemented by fs? */
788 	unsigned no_fsyncdir:1;
789 
790 	/** Is flush not implemented by fs? */
791 	unsigned no_flush:1;
792 
793 	/** Is setxattr not implemented by fs? */
794 	unsigned no_setxattr:1;
795 
796 	/** Does file server support extended setxattr */
797 	unsigned setxattr_ext:1;
798 
799 	/** Is getxattr not implemented by fs? */
800 	unsigned no_getxattr:1;
801 
802 	/** Is listxattr not implemented by fs? */
803 	unsigned no_listxattr:1;
804 
805 	/** Is removexattr not implemented by fs? */
806 	unsigned no_removexattr:1;
807 
808 	/** Are posix file locking primitives not implemented by fs? */
809 	unsigned no_lock:1;
810 
811 	/** Is access not implemented by fs? */
812 	unsigned no_access:1;
813 
814 	/** Is create not implemented by fs? */
815 	unsigned no_create:1;
816 
817 	/** Is interrupt not implemented by fs? */
818 	unsigned no_interrupt:1;
819 
820 	/** Is bmap not implemented by fs? */
821 	unsigned no_bmap:1;
822 
823 	/** Is poll not implemented by fs? */
824 	unsigned no_poll:1;
825 
826 	/** Do multi-page cached writes */
827 	unsigned big_writes:1;
828 
829 	/** Don't apply umask to creation modes */
830 	unsigned dont_mask:1;
831 
832 	/** Are BSD file locking primitives not implemented by fs? */
833 	unsigned no_flock:1;
834 
835 	/** Is fallocate not implemented by fs? */
836 	unsigned no_fallocate:1;
837 
838 	/** Is rename with flags implemented by fs? */
839 	unsigned no_rename2:1;
840 
841 	/** Use enhanced/automatic page cache invalidation. */
842 	unsigned auto_inval_data:1;
843 
844 	/** Filesystem is fully responsible for page cache invalidation. */
845 	unsigned explicit_inval_data:1;
846 
847 	/** Does the filesystem support readdirplus? */
848 	unsigned do_readdirplus:1;
849 
850 	/** Does the filesystem want adaptive readdirplus? */
851 	unsigned readdirplus_auto:1;
852 
853 	/** Does the filesystem support asynchronous direct-IO submission? */
854 	unsigned async_dio:1;
855 
856 	/** Is lseek not implemented by fs? */
857 	unsigned no_lseek:1;
858 
859 	/** Does the filesystem support posix acls? */
860 	unsigned posix_acl:1;
861 
862 	/** Check permissions based on the file mode or not? */
863 	unsigned default_permissions:1;
864 
865 	/** Allow other than the mounter user to access the filesystem ? */
866 	unsigned allow_other:1;
867 
868 	/** Does the filesystem support copy_file_range? */
869 	unsigned no_copy_file_range:1;
870 
871 	/* Send DESTROY request */
872 	unsigned int destroy:1;
873 
874 	/* Delete dentries that have gone stale */
875 	unsigned int delete_stale:1;
876 
877 	/** Do not create entry in fusectl fs */
878 	unsigned int no_control:1;
879 
880 	/** Do not allow MNT_FORCE umount */
881 	unsigned int no_force_umount:1;
882 
883 	/* Auto-mount submounts announced by the server */
884 	unsigned int auto_submounts:1;
885 
886 	/** Passthrough mode for read/write IO */
887 	unsigned int passthrough:1;
888 
889 	/* Propagate syncfs() to server */
890 	unsigned int sync_fs:1;
891 
892 	/* Initialize security xattrs when creating a new inode */
893 	unsigned int init_security:1;
894 
895 	/* Add supplementary group info when creating a new inode */
896 	unsigned int create_supp_group:1;
897 
898 	/* Does the filesystem support per inode DAX? */
899 	unsigned int inode_dax:1;
900 
901 	/* Is tmpfile not implemented by fs? */
902 	unsigned int no_tmpfile:1;
903 
904 	/* Relax restrictions to allow shared mmap in FOPEN_DIRECT_IO mode */
905 	unsigned int direct_io_allow_mmap:1;
906 
907 	/* Is statx not implemented by fs? */
908 	unsigned int no_statx:1;
909 
910 	/** BPF Only, no Daemon running */
911 	unsigned int no_daemon:1;
912 
913 	/** The number of requests waiting for completion */
914 	atomic_t num_waiting;
915 
916 	/** Negotiated minor version */
917 	unsigned minor;
918 
919 	/** Entry on the fuse_mount_list */
920 	struct list_head entry;
921 
922 	/** Device ID from the root super block */
923 	dev_t dev;
924 
925 	/** Dentries in the control filesystem */
926 	struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES];
927 
928 	/** number of dentries used in the above array */
929 	int ctl_ndents;
930 
931 	/** Key for lock owner ID scrambling */
932 	u32 scramble_key[4];
933 
934 	/** Version counter for attribute changes */
935 	atomic64_t attr_version;
936 
937 	/** Called on final put */
938 	void (*release)(struct fuse_conn *);
939 
940 	/**
941 	 * Read/write semaphore to hold when accessing the sb of any
942 	 * fuse_mount belonging to this connection
943 	 */
944 	struct rw_semaphore killsb;
945 
946 	/** List of device instances belonging to this connection */
947 	struct list_head devices;
948 
949 #ifdef CONFIG_FUSE_DAX
950 	/* Dax mode */
951 	enum fuse_dax_mode dax_mode;
952 
953 	/* Dax specific conn data, non-NULL if DAX is enabled */
954 	struct fuse_conn_dax *dax;
955 #endif
956 
957 	/** List of filesystems using this connection */
958 	struct list_head mounts;
959 
960 	/* New writepages go into this bucket */
961 	struct fuse_sync_bucket __rcu *curr_bucket;
962 
963 	/** IDR for passthrough requests */
964 	struct idr passthrough_req;
965 
966 	/** Protects passthrough_req */
967 	spinlock_t passthrough_req_lock;
968 };
969 
970 /*
971  * Represents a mounted filesystem, potentially a submount.
972  *
973  * This object allows sharing a fuse_conn between separate mounts to
974  * allow submounts with dedicated superblocks and thus separate device
975  * IDs.
976  */
977 struct fuse_mount {
978 	/* Underlying (potentially shared) connection to the FUSE server */
979 	struct fuse_conn *fc;
980 
981 	/*
982 	 * Super block for this connection (fc->killsb must be held when
983 	 * accessing this).
984 	 */
985 	struct super_block *sb;
986 
987 	/* Entry on fc->mounts */
988 	struct list_head fc_entry;
989 	struct rcu_head rcu;
990 };
991 
get_fuse_mount_super(struct super_block * sb)992 static inline struct fuse_mount *get_fuse_mount_super(struct super_block *sb)
993 {
994 	return sb->s_fs_info;
995 }
996 
get_fuse_conn_super(struct super_block * sb)997 static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
998 {
999 	return get_fuse_mount_super(sb)->fc;
1000 }
1001 
get_fuse_mount(struct inode * inode)1002 static inline struct fuse_mount *get_fuse_mount(struct inode *inode)
1003 {
1004 	return get_fuse_mount_super(inode->i_sb);
1005 }
1006 
get_fuse_conn(struct inode * inode)1007 static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
1008 {
1009 	return get_fuse_mount_super(inode->i_sb)->fc;
1010 }
1011 
get_fuse_inode(struct inode * inode)1012 static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
1013 {
1014 	return container_of(inode, struct fuse_inode, inode);
1015 }
1016 
get_node_id(struct inode * inode)1017 static inline u64 get_node_id(struct inode *inode)
1018 {
1019 	return get_fuse_inode(inode)->nodeid;
1020 }
1021 
invalid_nodeid(u64 nodeid)1022 static inline int invalid_nodeid(u64 nodeid)
1023 {
1024 	return !nodeid || nodeid == FUSE_ROOT_ID;
1025 }
1026 
fuse_get_attr_version(struct fuse_conn * fc)1027 static inline u64 fuse_get_attr_version(struct fuse_conn *fc)
1028 {
1029 	return atomic64_read(&fc->attr_version);
1030 }
1031 
fuse_stale_inode(const struct inode * inode,int generation,struct fuse_attr * attr)1032 static inline bool fuse_stale_inode(const struct inode *inode, int generation,
1033 				    struct fuse_attr *attr)
1034 {
1035 	return inode->i_generation != generation ||
1036 		inode_wrong_type(inode, attr->mode);
1037 }
1038 
fuse_make_bad(struct inode * inode)1039 static inline void fuse_make_bad(struct inode *inode)
1040 {
1041 	set_bit(FUSE_I_BAD, &get_fuse_inode(inode)->state);
1042 }
1043 
fuse_is_bad(struct inode * inode)1044 static inline bool fuse_is_bad(struct inode *inode)
1045 {
1046 	return unlikely(test_bit(FUSE_I_BAD, &get_fuse_inode(inode)->state));
1047 }
1048 
fuse_pages_alloc(unsigned int npages,gfp_t flags,struct fuse_page_desc ** desc)1049 static inline struct page **fuse_pages_alloc(unsigned int npages, gfp_t flags,
1050 					     struct fuse_page_desc **desc)
1051 {
1052 	struct page **pages;
1053 
1054 	pages = kzalloc(npages * (sizeof(struct page *) +
1055 				  sizeof(struct fuse_page_desc)), flags);
1056 	*desc = (void *) (pages + npages);
1057 
1058 	return pages;
1059 }
1060 
fuse_page_descs_length_init(struct fuse_page_desc * descs,unsigned int index,unsigned int nr_pages)1061 static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs,
1062 					       unsigned int index,
1063 					       unsigned int nr_pages)
1064 {
1065 	int i;
1066 
1067 	for (i = index; i < index + nr_pages; i++)
1068 		descs[i].length = PAGE_SIZE - descs[i].offset;
1069 }
1070 
fuse_sync_bucket_dec(struct fuse_sync_bucket * bucket)1071 static inline void fuse_sync_bucket_dec(struct fuse_sync_bucket *bucket)
1072 {
1073 	/* Need RCU protection to prevent use after free after the decrement */
1074 	rcu_read_lock();
1075 	if (atomic_dec_and_test(&bucket->count))
1076 		wake_up(&bucket->waitq);
1077 	rcu_read_unlock();
1078 }
1079 
1080 /** Device operations */
1081 extern const struct file_operations fuse_dev_operations;
1082 
1083 extern const struct dentry_operations fuse_dentry_operations;
1084 extern const struct dentry_operations fuse_root_dentry_operations;
1085 
1086 /**
1087  * Get a filled-in inode
1088  */
1089 struct inode *fuse_iget_backing(struct super_block *sb,
1090 				u64 nodeid,
1091 				struct inode *backing_inode);
1092 struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
1093 			int generation, struct fuse_attr *attr,
1094 			u64 attr_valid, u64 attr_version);
1095 
1096 int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name,
1097 		     struct fuse_entry_out *outarg,
1098 		     struct dentry *entry, struct inode **inode);
1099 
1100 /**
1101  * Send FORGET command
1102  */
1103 void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
1104 		       u64 nodeid, u64 nlookup);
1105 
1106 struct fuse_forget_link *fuse_alloc_forget(void);
1107 
1108 struct fuse_forget_link *fuse_dequeue_forget(struct fuse_iqueue *fiq,
1109 					     unsigned int max,
1110 					     unsigned int *countp);
1111 
1112 /*
1113  * Initialize READ or READDIR request
1114  */
1115 struct fuse_io_args {
1116 	union {
1117 		struct {
1118 			struct fuse_read_in in;
1119 			u64 attr_ver;
1120 		} read;
1121 		struct {
1122 			struct fuse_write_in in;
1123 			struct fuse_write_out out;
1124 			bool page_locked;
1125 		} write;
1126 	};
1127 	struct fuse_args_pages ap;
1128 	struct fuse_io_priv *io;
1129 	struct fuse_file *ff;
1130 };
1131 
1132 void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
1133 			 size_t count, int opcode);
1134 
1135 /**
1136  * Send OPEN or OPENDIR request
1137  */
1138 int fuse_open_common(struct inode *inode, struct file *file, bool isdir);
1139 
1140 struct fuse_file *fuse_file_alloc(struct fuse_mount *fm);
1141 void fuse_file_free(struct fuse_file *ff);
1142 void fuse_finish_open(struct inode *inode, struct file *file);
1143 
1144 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff,
1145 		       unsigned int flags);
1146 
1147 /**
1148  * Send RELEASE or RELEASEDIR request
1149  */
1150 void fuse_release_common(struct file *file, bool isdir);
1151 
1152 /**
1153  * Send FSYNC or FSYNCDIR request
1154  */
1155 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
1156 		      int datasync, int opcode);
1157 
1158 /**
1159  * Notify poll wakeup
1160  */
1161 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
1162 			    struct fuse_notify_poll_wakeup_out *outarg);
1163 
1164 /**
1165  * Initialize file operations on a regular file
1166  */
1167 void fuse_init_file_inode(struct inode *inode, unsigned int flags);
1168 
1169 /**
1170  * Initialize inode operations on regular files and special files
1171  */
1172 void fuse_init_common(struct inode *inode);
1173 
1174 /**
1175  * Initialize inode and file operations on a directory
1176  */
1177 void fuse_init_dir(struct inode *inode);
1178 
1179 /**
1180  * Initialize inode operations on a symlink
1181  */
1182 void fuse_init_symlink(struct inode *inode);
1183 
1184 /**
1185  * Change attributes of an inode
1186  */
1187 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
1188 			    struct fuse_statx *sx,
1189 			    u64 attr_valid, u64 attr_version);
1190 
1191 void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
1192 				   struct fuse_statx *sx,
1193 				   u64 attr_valid, u32 cache_mask);
1194 
1195 u32 fuse_get_cache_mask(struct inode *inode);
1196 
1197 /**
1198  * Initialize the client device
1199  */
1200 int fuse_dev_init(void);
1201 
1202 /**
1203  * Cleanup the client device
1204  */
1205 void fuse_dev_cleanup(void);
1206 
1207 int fuse_ctl_init(void);
1208 void fuse_ctl_cleanup(void);
1209 
1210 /**
1211  * Simple request sending that does request allocation and freeing
1212  */
1213 ssize_t fuse_simple_request(struct fuse_mount *fm, struct fuse_args *args);
1214 int fuse_simple_background(struct fuse_mount *fm, struct fuse_args *args,
1215 			   gfp_t gfp_flags);
1216 
1217 /**
1218  * End a finished request
1219  */
1220 void fuse_request_end(struct fuse_req *req);
1221 
1222 /* Abort all requests */
1223 void fuse_abort_conn(struct fuse_conn *fc);
1224 void fuse_wait_aborted(struct fuse_conn *fc);
1225 
1226 /**
1227  * Invalidate inode attributes
1228  */
1229 
1230 /* Attributes possibly changed on data modification */
1231 #define FUSE_STATX_MODIFY	(STATX_MTIME | STATX_CTIME | STATX_BLOCKS)
1232 
1233 /* Attributes possibly changed on data and/or size modification */
1234 #define FUSE_STATX_MODSIZE	(FUSE_STATX_MODIFY | STATX_SIZE)
1235 
1236 void fuse_invalidate_attr(struct inode *inode);
1237 void fuse_invalidate_attr_mask(struct inode *inode, u32 mask);
1238 
1239 void fuse_invalidate_entry_cache(struct dentry *entry);
1240 
1241 void fuse_invalidate_atime(struct inode *inode);
1242 
1243 static u64 fuse_time_to_jiffies(u64 sec, u32 nsec);
1244 #define ATTR_TIMEOUT(o) \
1245 	fuse_time_to_jiffies((o)->attr_valid, (o)->attr_valid_nsec)
1246 
1247 
1248 u64 entry_attr_timeout(struct fuse_entry_out *o);
1249 void fuse_init_dentry_root(struct dentry *root, struct file *backing_dir);
1250 
1251 void fuse_change_entry_timeout(struct dentry *entry, struct fuse_entry_out *o);
1252 
1253 /**
1254  * Acquire reference to fuse_conn
1255  */
1256 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
1257 
1258 /**
1259  * Initialize fuse_conn
1260  */
1261 void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm,
1262 		    struct user_namespace *user_ns,
1263 		    const struct fuse_iqueue_ops *fiq_ops, void *fiq_priv);
1264 
1265 /**
1266  * Release reference to fuse_conn
1267  */
1268 void fuse_conn_put(struct fuse_conn *fc);
1269 
1270 struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc);
1271 struct fuse_dev *fuse_dev_alloc(void);
1272 void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc);
1273 void fuse_dev_free(struct fuse_dev *fud);
1274 void fuse_send_init(struct fuse_mount *fm);
1275 
1276 /**
1277  * Fill in superblock and initialize fuse connection
1278  * @sb: partially-initialized superblock to fill in
1279  * @ctx: mount context
1280  */
1281 int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx);
1282 
1283 /*
1284  * Remove the mount from the connection
1285  *
1286  * Returns whether this was the last mount
1287  */
1288 bool fuse_mount_remove(struct fuse_mount *fm);
1289 
1290 /*
1291  * Setup context ops for submounts
1292  */
1293 int fuse_init_fs_context_submount(struct fs_context *fsc);
1294 
1295 /*
1296  * Shut down the connection (possibly sending DESTROY request).
1297  */
1298 void fuse_conn_destroy(struct fuse_mount *fm);
1299 
1300 /* Drop the connection and free the fuse mount */
1301 void fuse_mount_destroy(struct fuse_mount *fm);
1302 
1303 /**
1304  * Add connection to control filesystem
1305  */
1306 int fuse_ctl_add_conn(struct fuse_conn *fc);
1307 
1308 /**
1309  * Remove connection from control filesystem
1310  */
1311 void fuse_ctl_remove_conn(struct fuse_conn *fc);
1312 
1313 /**
1314  * Is file type valid?
1315  */
1316 int fuse_valid_type(int m);
1317 
1318 bool fuse_invalid_attr(struct fuse_attr *attr);
1319 
1320 /**
1321  * Is current process allowed to perform filesystem operation?
1322  */
1323 bool fuse_allow_current_process(struct fuse_conn *fc);
1324 
1325 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id);
1326 
1327 void fuse_flush_time_update(struct inode *inode);
1328 void fuse_update_ctime(struct inode *inode);
1329 
1330 int fuse_update_attributes(struct inode *inode, struct file *file, u32 mask);
1331 
1332 void fuse_flush_writepages(struct inode *inode);
1333 
1334 void fuse_set_nowrite(struct inode *inode);
1335 void fuse_release_nowrite(struct inode *inode);
1336 
1337 /**
1338  * Scan all fuse_mounts belonging to fc to find the first where
1339  * ilookup5() returns a result.  Return that result and the
1340  * respective fuse_mount in *fm (unless fm is NULL).
1341  *
1342  * The caller must hold fc->killsb.
1343  */
1344 struct inode *fuse_ilookup(struct fuse_conn *fc, u64 nodeid,
1345 			   struct fuse_mount **fm);
1346 
1347 /**
1348  * File-system tells the kernel to invalidate cache for the given node id.
1349  */
1350 int fuse_reverse_inval_inode(struct fuse_conn *fc, u64 nodeid,
1351 			     loff_t offset, loff_t len);
1352 
1353 /**
1354  * File-system tells the kernel to invalidate parent attributes and
1355  * the dentry matching parent/name.
1356  *
1357  * If the child_nodeid is non-zero and:
1358  *    - matches the inode number for the dentry matching parent/name,
1359  *    - is not a mount point
1360  *    - is a file or oan empty directory
1361  * then the dentry is unhashed (d_delete()).
1362  */
1363 int fuse_reverse_inval_entry(struct fuse_conn *fc, u64 parent_nodeid,
1364 			     u64 child_nodeid, struct qstr *name, u32 flags);
1365 
1366 int fuse_do_open(struct fuse_mount *fm, u64 nodeid, struct file *file,
1367 		 bool isdir);
1368 
1369 /**
1370  * fuse_direct_io() flags
1371  */
1372 
1373 /** If set, it is WRITE; otherwise - READ */
1374 #define FUSE_DIO_WRITE (1 << 0)
1375 
1376 /** CUSE pass fuse_direct_io() a file which f_mapping->host is not from FUSE */
1377 #define FUSE_DIO_CUSE  (1 << 1)
1378 
1379 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1380 		       loff_t *ppos, int flags);
1381 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
1382 		   unsigned int flags);
1383 long fuse_ioctl_common(struct file *file, unsigned int cmd,
1384 		       unsigned long arg, unsigned int flags);
1385 __poll_t fuse_file_poll(struct file *file, poll_table *wait);
1386 int fuse_dev_release(struct inode *inode, struct file *file);
1387 
1388 bool fuse_write_update_attr(struct inode *inode, loff_t pos, ssize_t written);
1389 
1390 int fuse_flush_times(struct inode *inode, struct fuse_file *ff);
1391 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc);
1392 
1393 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
1394 		    struct file *file);
1395 
1396 void fuse_set_initialized(struct fuse_conn *fc);
1397 
1398 void fuse_unlock_inode(struct inode *inode, bool locked);
1399 bool fuse_lock_inode(struct inode *inode);
1400 
1401 int fuse_setxattr(struct inode *inode, const char *name, const void *value,
1402 		  size_t size, int flags, unsigned int extra_flags);
1403 ssize_t fuse_getxattr(struct inode *inode, const char *name, void *value,
1404 		      size_t size);
1405 ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size);
1406 int fuse_removexattr(struct inode *inode, const char *name);
1407 extern const struct xattr_handler *fuse_xattr_handlers[];
1408 
1409 struct posix_acl;
1410 struct posix_acl *fuse_get_inode_acl(struct inode *inode, int type, bool rcu);
1411 struct posix_acl *fuse_get_acl(struct mnt_idmap *idmap,
1412 			       struct dentry *dentry, int type);
1413 int fuse_set_acl(struct mnt_idmap *, struct dentry *dentry,
1414 		 struct posix_acl *acl, int type);
1415 
1416 /* readdir.c */
1417 int fuse_readdir(struct file *file, struct dir_context *ctx);
1418 
1419 /**
1420  * Return the number of bytes in an arguments list
1421  */
1422 unsigned int fuse_len_args(unsigned int numargs, struct fuse_arg *args);
1423 
1424 /**
1425  * Get the next unique ID for a request
1426  */
1427 u64 fuse_get_unique(struct fuse_iqueue *fiq);
1428 void fuse_free_conn(struct fuse_conn *fc);
1429 
1430 /* dax.c */
1431 
1432 #define FUSE_IS_DAX(inode) (IS_ENABLED(CONFIG_FUSE_DAX) && IS_DAX(inode))
1433 
1434 ssize_t fuse_dax_read_iter(struct kiocb *iocb, struct iov_iter *to);
1435 ssize_t fuse_dax_write_iter(struct kiocb *iocb, struct iov_iter *from);
1436 int fuse_dax_mmap(struct file *file, struct vm_area_struct *vma);
1437 int fuse_dax_break_layouts(struct inode *inode, u64 dmap_start, u64 dmap_end);
1438 int fuse_dax_conn_alloc(struct fuse_conn *fc, enum fuse_dax_mode mode,
1439 			struct dax_device *dax_dev);
1440 void fuse_dax_conn_free(struct fuse_conn *fc);
1441 bool fuse_dax_inode_alloc(struct super_block *sb, struct fuse_inode *fi);
1442 void fuse_dax_inode_init(struct inode *inode, unsigned int flags);
1443 void fuse_dax_inode_cleanup(struct inode *inode);
1444 void fuse_dax_dontcache(struct inode *inode, unsigned int flags);
1445 bool fuse_dax_check_alignment(struct fuse_conn *fc, unsigned int map_alignment);
1446 void fuse_dax_cancel_work(struct fuse_conn *fc);
1447 
1448 /* ioctl.c */
1449 long fuse_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
1450 long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
1451 			    unsigned long arg);
1452 int fuse_fileattr_get(struct dentry *dentry, struct fileattr *fa);
1453 int fuse_fileattr_set(struct mnt_idmap *idmap,
1454 		      struct dentry *dentry, struct fileattr *fa);
1455 
1456 /* file.c */
1457 
1458 struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid,
1459 				 unsigned int open_flags, bool isdir);
1460 void fuse_file_release(struct inode *inode, struct fuse_file *ff,
1461 		       unsigned int open_flags, fl_owner_t id, bool isdir);
1462 
1463 /* passthrough.c */
1464 void fuse_copyattr(struct file *dst_file, struct file *src_file);
1465 int fuse_passthrough_open(struct fuse_dev *fud, u32 lower_fd);
1466 int fuse_passthrough_setup(struct fuse_conn *fc, struct fuse_file *ff,
1467 			   struct fuse_open_out *openarg);
1468 void fuse_passthrough_release(struct fuse_passthrough *passthrough);
1469 ssize_t fuse_passthrough_read_iter(struct kiocb *iocb, struct iov_iter *to);
1470 ssize_t fuse_passthrough_write_iter(struct kiocb *iocb, struct iov_iter *from);
1471 ssize_t fuse_passthrough_splice_read(struct file *in, loff_t *ppos,
1472 				     struct pipe_inode_info *pipe,
1473 				     size_t len, unsigned int flags);
1474 ssize_t fuse_passthrough_splice_write(struct pipe_inode_info *pipe,
1475 		struct file *out, loff_t *ppos, size_t len, unsigned int flags);
1476 ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma);
1477 
1478 /* backing.c */
1479 
1480 /*
1481  * Dummy io passed to fuse_bpf_backing when io operation needs no scratch space
1482  */
1483 struct fuse_dummy_io {
1484 	int unused;
1485 };
1486 
1487 struct fuse_open_io {
1488 	struct fuse_open_in foi;
1489 	struct fuse_open_out foo;
1490 };
1491 
1492 int fuse_open_initialize(struct fuse_bpf_args *fa, struct fuse_open_io *foi,
1493 			 struct inode *inode, struct file *file, bool isdir);
1494 int fuse_open_backing(struct fuse_bpf_args *fa,
1495 		      struct inode *inode, struct file *file, bool isdir);
1496 void *fuse_open_finalize(struct fuse_bpf_args *fa,
1497 		       struct inode *inode, struct file *file, bool isdir);
1498 
1499 struct fuse_create_open_io {
1500 	struct fuse_create_in fci;
1501 	struct fuse_entry_out feo;
1502 	struct fuse_open_out foo;
1503 };
1504 
1505 int fuse_create_open_initialize(
1506 		struct fuse_bpf_args *fa, struct fuse_create_open_io *fcoi,
1507 		struct inode *dir, struct dentry *entry,
1508 		struct file *file, unsigned int flags, umode_t mode);
1509 int fuse_create_open_backing(
1510 		struct fuse_bpf_args *fa,
1511 		struct inode *dir, struct dentry *entry,
1512 		struct file *file, unsigned int flags, umode_t mode);
1513 void *fuse_create_open_finalize(
1514 		struct fuse_bpf_args *fa,
1515 		struct inode *dir, struct dentry *entry,
1516 		struct file *file, unsigned int flags, umode_t mode);
1517 
1518 int fuse_mknod_initialize(
1519 		struct fuse_bpf_args *fa, struct fuse_mknod_in *fmi,
1520 		struct inode *dir, struct dentry *entry, umode_t mode, dev_t rdev);
1521 int fuse_mknod_backing(
1522 		struct fuse_bpf_args *fa,
1523 		struct inode *dir, struct dentry *entry, umode_t mode, dev_t rdev);
1524 void *fuse_mknod_finalize(
1525 		struct fuse_bpf_args *fa,
1526 		struct inode *dir, struct dentry *entry, umode_t mode, dev_t rdev);
1527 
1528 int fuse_mkdir_initialize(
1529 		struct fuse_bpf_args *fa, struct fuse_mkdir_in *fmi,
1530 		struct inode *dir, struct dentry *entry, umode_t mode);
1531 int fuse_mkdir_backing(
1532 		struct fuse_bpf_args *fa,
1533 		struct inode *dir, struct dentry *entry, umode_t mode);
1534 void *fuse_mkdir_finalize(
1535 		struct fuse_bpf_args *fa,
1536 		struct inode *dir, struct dentry *entry, umode_t mode);
1537 
1538 int fuse_rmdir_initialize(
1539 		struct fuse_bpf_args *fa, struct fuse_dummy_io *fmi,
1540 		struct inode *dir, struct dentry *entry);
1541 int fuse_rmdir_backing(
1542 		struct fuse_bpf_args *fa,
1543 		struct inode *dir, struct dentry *entry);
1544 void *fuse_rmdir_finalize(
1545 		struct fuse_bpf_args *fa,
1546 		struct inode *dir, struct dentry *entry);
1547 
1548 int fuse_rename2_initialize(struct fuse_bpf_args *fa, struct fuse_rename2_in *fri,
1549 			    struct inode *olddir, struct dentry *oldent,
1550 			    struct inode *newdir, struct dentry *newent,
1551 			    unsigned int flags);
1552 int fuse_rename2_backing(struct fuse_bpf_args *fa,
1553 			 struct inode *olddir, struct dentry *oldent,
1554 			 struct inode *newdir, struct dentry *newent,
1555 			 unsigned int flags);
1556 void *fuse_rename2_finalize(struct fuse_bpf_args *fa,
1557 			    struct inode *olddir, struct dentry *oldent,
1558 			    struct inode *newdir, struct dentry *newent,
1559 			    unsigned int flags);
1560 
1561 int fuse_rename_initialize(struct fuse_bpf_args *fa, struct fuse_rename_in *fri,
1562 			   struct inode *olddir, struct dentry *oldent,
1563 			   struct inode *newdir, struct dentry *newent);
1564 int fuse_rename_backing(struct fuse_bpf_args *fa,
1565 			struct inode *olddir, struct dentry *oldent,
1566 			struct inode *newdir, struct dentry *newent);
1567 void *fuse_rename_finalize(struct fuse_bpf_args *fa,
1568 			   struct inode *olddir, struct dentry *oldent,
1569 			   struct inode *newdir, struct dentry *newent);
1570 
1571 int fuse_unlink_initialize(
1572 		struct fuse_bpf_args *fa, struct fuse_dummy_io *fmi,
1573 		struct inode *dir, struct dentry *entry);
1574 int fuse_unlink_backing(
1575 		struct fuse_bpf_args *fa,
1576 		struct inode *dir, struct dentry *entry);
1577 void *fuse_unlink_finalize(
1578 		struct fuse_bpf_args *fa,
1579 		struct inode *dir, struct dentry *entry);
1580 
1581 int fuse_link_initialize(struct fuse_bpf_args *fa, struct fuse_link_in *fli,
1582 			  struct dentry *entry, struct inode *dir,
1583 			  struct dentry *newent);
1584 int fuse_link_backing(struct fuse_bpf_args *fa, struct dentry *entry,
1585 		      struct inode *dir, struct dentry *newent);
1586 void *fuse_link_finalize(struct fuse_bpf_args *fa, struct dentry *entry,
1587 			 struct inode *dir, struct dentry *newent);
1588 
1589 int fuse_release_initialize(struct fuse_bpf_args *fa, struct fuse_release_in *fri,
1590 			    struct inode *inode, struct fuse_file *ff);
1591 int fuse_release_backing(struct fuse_bpf_args *fa,
1592 			 struct inode *inode, struct fuse_file *ff);
1593 void *fuse_release_finalize(struct fuse_bpf_args *fa,
1594 			    struct inode *inode, struct fuse_file *ff);
1595 
1596 int fuse_flush_initialize(struct fuse_bpf_args *fa, struct fuse_flush_in *ffi,
1597 			  struct file *file, fl_owner_t id);
1598 int fuse_flush_backing(struct fuse_bpf_args *fa, struct file *file, fl_owner_t id);
1599 void *fuse_flush_finalize(struct fuse_bpf_args *fa,
1600 			  struct file *file, fl_owner_t id);
1601 
1602 struct fuse_lseek_io {
1603 	struct fuse_lseek_in fli;
1604 	struct fuse_lseek_out flo;
1605 };
1606 
1607 int fuse_lseek_initialize(struct fuse_bpf_args *fa, struct fuse_lseek_io *fli,
1608 			  struct file *file, loff_t offset, int whence);
1609 int fuse_lseek_backing(struct fuse_bpf_args *fa, struct file *file, loff_t offset, int whence);
1610 void *fuse_lseek_finalize(struct fuse_bpf_args *fa, struct file *file, loff_t offset, int whence);
1611 
1612 struct fuse_copy_file_range_io {
1613 	struct fuse_copy_file_range_in fci;
1614 	struct fuse_write_out fwo;
1615 };
1616 
1617 int fuse_copy_file_range_initialize(struct fuse_bpf_args *fa,
1618 				   struct fuse_copy_file_range_io *fcf,
1619 				   struct file *file_in, loff_t pos_in,
1620 				   struct file *file_out, loff_t pos_out,
1621 				   size_t len, unsigned int flags);
1622 int fuse_copy_file_range_backing(struct fuse_bpf_args *fa,
1623 				 struct file *file_in, loff_t pos_in,
1624 				 struct file *file_out, loff_t pos_out,
1625 				 size_t len, unsigned int flags);
1626 void *fuse_copy_file_range_finalize(struct fuse_bpf_args *fa,
1627 				    struct file *file_in, loff_t pos_in,
1628 				    struct file *file_out, loff_t pos_out,
1629 				    size_t len, unsigned int flags);
1630 
1631 int fuse_fsync_initialize(struct fuse_bpf_args *fa, struct fuse_fsync_in *ffi,
1632 		   struct file *file, loff_t start, loff_t end, int datasync);
1633 int fuse_fsync_backing(struct fuse_bpf_args *fa,
1634 		   struct file *file, loff_t start, loff_t end, int datasync);
1635 void *fuse_fsync_finalize(struct fuse_bpf_args *fa,
1636 		   struct file *file, loff_t start, loff_t end, int datasync);
1637 int fuse_dir_fsync_initialize(struct fuse_bpf_args *fa, struct fuse_fsync_in *ffi,
1638 		   struct file *file, loff_t start, loff_t end, int datasync);
1639 
1640 struct fuse_getxattr_io {
1641 	struct fuse_getxattr_in fgi;
1642 	struct fuse_getxattr_out fgo;
1643 };
1644 
1645 int fuse_getxattr_initialize(
1646 		struct fuse_bpf_args *fa, struct fuse_getxattr_io *fgio,
1647 		struct dentry *dentry, const char *name, void *value,
1648 		size_t size);
1649 int fuse_getxattr_backing(
1650 		struct fuse_bpf_args *fa,
1651 		struct dentry *dentry, const char *name, void *value,
1652 		size_t size);
1653 void *fuse_getxattr_finalize(
1654 		struct fuse_bpf_args *fa,
1655 		struct dentry *dentry, const char *name, void *value,
1656 		size_t size);
1657 
1658 int fuse_listxattr_initialize(struct fuse_bpf_args *fa,
1659 			       struct fuse_getxattr_io *fgio,
1660 			       struct dentry *dentry, char *list, size_t size);
1661 int fuse_listxattr_backing(struct fuse_bpf_args *fa, struct dentry *dentry,
1662 			   char *list, size_t size);
1663 void *fuse_listxattr_finalize(struct fuse_bpf_args *fa, struct dentry *dentry,
1664 			      char *list, size_t size);
1665 
1666 int fuse_setxattr_initialize(struct fuse_bpf_args *fa,
1667 			     struct fuse_setxattr_in *fsxi,
1668 			     struct dentry *dentry, const char *name,
1669 			     const void *value, size_t size, int flags);
1670 int fuse_setxattr_backing(struct fuse_bpf_args *fa, struct dentry *dentry,
1671 			  const char *name, const void *value, size_t size,
1672 			  int flags);
1673 void *fuse_setxattr_finalize(struct fuse_bpf_args *fa, struct dentry *dentry,
1674 			     const char *name, const void *value, size_t size,
1675 			     int flags);
1676 
1677 int fuse_removexattr_initialize(struct fuse_bpf_args *fa,
1678 				struct fuse_dummy_io *unused,
1679 				struct dentry *dentry, const char *name);
1680 int fuse_removexattr_backing(struct fuse_bpf_args *fa,
1681 			     struct dentry *dentry, const char *name);
1682 void *fuse_removexattr_finalize(struct fuse_bpf_args *fa,
1683 				struct dentry *dentry, const char *name);
1684 
1685 struct fuse_read_iter_out {
1686 	uint64_t ret;
1687 };
1688 struct fuse_file_read_iter_io {
1689 	struct fuse_read_in fri;
1690 	struct fuse_read_iter_out frio;
1691 };
1692 
1693 int fuse_file_read_iter_initialize(
1694 		struct fuse_bpf_args *fa, struct fuse_file_read_iter_io *fri,
1695 		struct kiocb *iocb, struct iov_iter *to);
1696 int fuse_file_read_iter_backing(struct fuse_bpf_args *fa,
1697 		struct kiocb *iocb, struct iov_iter *to);
1698 void *fuse_file_read_iter_finalize(struct fuse_bpf_args *fa,
1699 		struct kiocb *iocb, struct iov_iter *to);
1700 
1701 struct fuse_write_iter_out {
1702 	uint64_t ret;
1703 };
1704 struct fuse_file_write_iter_io {
1705 	struct fuse_write_in fwi;
1706 	struct fuse_write_out fwo;
1707 	struct fuse_write_iter_out fwio;
1708 };
1709 
1710 int fuse_file_write_iter_initialize(
1711 		struct fuse_bpf_args *fa, struct fuse_file_write_iter_io *fwio,
1712 		struct kiocb *iocb, struct iov_iter *from);
1713 int fuse_file_write_iter_backing(struct fuse_bpf_args *fa,
1714 		struct kiocb *iocb, struct iov_iter *from);
1715 void *fuse_file_write_iter_finalize(struct fuse_bpf_args *fa,
1716 		struct kiocb *iocb, struct iov_iter *from);
1717 
1718 ssize_t fuse_splice_read_backing(struct file *in, loff_t *ppos,
1719 		struct pipe_inode_info *pipe, size_t len, unsigned long flags);
1720 ssize_t fuse_splice_write_backing(struct pipe_inode_info *pipe,
1721 		struct file *out, loff_t *ppos, size_t len, unsigned long flags);
1722 
1723 long fuse_backing_ioctl(struct file *file, unsigned int command, unsigned long arg, int flags);
1724 
1725 int fuse_file_flock_backing(struct file *file, int cmd, struct file_lock *fl);
1726 ssize_t fuse_backing_mmap(struct file *file, struct vm_area_struct *vma);
1727 
1728 int fuse_file_fallocate_initialize(struct fuse_bpf_args *fa,
1729 		struct fuse_fallocate_in *ffi,
1730 		struct file *file, int mode, loff_t offset, loff_t length);
1731 int fuse_file_fallocate_backing(struct fuse_bpf_args *fa,
1732 		struct file *file, int mode, loff_t offset, loff_t length);
1733 void *fuse_file_fallocate_finalize(struct fuse_bpf_args *fa,
1734 		struct file *file, int mode, loff_t offset, loff_t length);
1735 
1736 struct fuse_lookup_io {
1737 	struct fuse_entry_out feo;
1738 	struct fuse_entry_bpf feb;
1739 };
1740 
1741 int fuse_handle_backing(struct fuse_entry_bpf *feb, struct inode **backing_inode,
1742 			struct path *backing_path);
1743 int fuse_handle_bpf_prog(struct fuse_entry_bpf *feb, struct inode *parent,
1744 			 struct bpf_prog **bpf);
1745 
1746 int fuse_lookup_initialize(struct fuse_bpf_args *fa, struct fuse_lookup_io *feo,
1747 	       struct inode *dir, struct dentry *entry, unsigned int flags);
1748 int fuse_lookup_backing(struct fuse_bpf_args *fa, struct inode *dir,
1749 			  struct dentry *entry, unsigned int flags);
1750 struct dentry *fuse_lookup_finalize(struct fuse_bpf_args *fa, struct inode *dir,
1751 			   struct dentry *entry, unsigned int flags);
1752 int fuse_revalidate_backing(struct dentry *entry, unsigned int flags);
1753 
1754 int fuse_canonical_path_initialize(struct fuse_bpf_args *fa,
1755 				   struct fuse_dummy_io *fdi,
1756 				   const struct path *path,
1757 				   struct path *canonical_path);
1758 int fuse_canonical_path_backing(struct fuse_bpf_args *fa, const struct path *path,
1759 				struct path *canonical_path);
1760 void *fuse_canonical_path_finalize(struct fuse_bpf_args *fa,
1761 				   const struct path *path,
1762 				   struct path *canonical_path);
1763 
1764 struct fuse_getattr_io {
1765 	struct fuse_getattr_in fgi;
1766 	struct fuse_attr_out fao;
1767 };
1768 int fuse_getattr_initialize(struct fuse_bpf_args *fa, struct fuse_getattr_io *fgio,
1769 			const struct dentry *entry, struct kstat *stat,
1770 			u32 request_mask, unsigned int flags);
1771 int fuse_getattr_backing(struct fuse_bpf_args *fa,
1772 			const struct dentry *entry, struct kstat *stat,
1773 			u32 request_mask, unsigned int flags);
1774 void *fuse_getattr_finalize(struct fuse_bpf_args *fa,
1775 			const struct dentry *entry, struct kstat *stat,
1776 			u32 request_mask, unsigned int flags);
1777 
1778 struct fuse_setattr_io {
1779 	struct fuse_setattr_in fsi;
1780 	struct fuse_attr_out fao;
1781 };
1782 
1783 int fuse_setattr_initialize(struct fuse_bpf_args *fa, struct fuse_setattr_io *fsi,
1784 		struct dentry *dentry, struct iattr *attr, struct file *file);
1785 int fuse_setattr_backing(struct fuse_bpf_args *fa,
1786 		struct dentry *dentry, struct iattr *attr, struct file *file);
1787 void *fuse_setattr_finalize(struct fuse_bpf_args *fa,
1788 		struct dentry *dentry, struct iattr *attr, struct file *file);
1789 
1790 int fuse_statfs_initialize(struct fuse_bpf_args *fa, struct fuse_statfs_out *fso,
1791 		struct dentry *dentry, struct kstatfs *buf);
1792 int fuse_statfs_backing(struct fuse_bpf_args *fa,
1793 		struct dentry *dentry, struct kstatfs *buf);
1794 void *fuse_statfs_finalize(struct fuse_bpf_args *fa,
1795 		struct dentry *dentry, struct kstatfs *buf);
1796 
1797 int fuse_get_link_initialize(struct fuse_bpf_args *fa, struct fuse_dummy_io *dummy,
1798 		struct inode *inode, struct dentry *dentry,
1799 		struct delayed_call *callback, const char **out);
1800 int fuse_get_link_backing(struct fuse_bpf_args *fa,
1801 		struct inode *inode, struct dentry *dentry,
1802 		struct delayed_call *callback, const char **out);
1803 void *fuse_get_link_finalize(struct fuse_bpf_args *fa,
1804 		struct inode *inode, struct dentry *dentry,
1805 		struct delayed_call *callback, const char **out);
1806 
1807 int fuse_symlink_initialize(
1808 		struct fuse_bpf_args *fa, struct fuse_dummy_io *unused,
1809 		struct inode *dir, struct dentry *entry, const char *link, int len);
1810 int fuse_symlink_backing(
1811 		struct fuse_bpf_args *fa,
1812 		struct inode *dir, struct dentry *entry, const char *link, int len);
1813 void *fuse_symlink_finalize(
1814 		struct fuse_bpf_args *fa,
1815 		struct inode *dir, struct dentry *entry, const char *link, int len);
1816 
1817 struct fuse_read_io {
1818 	struct fuse_read_in fri;
1819 	struct fuse_read_out fro;
1820 };
1821 
1822 int fuse_readdir_initialize(struct fuse_bpf_args *fa, struct fuse_read_io *frio,
1823 			    struct file *file, struct dir_context *ctx,
1824 			    bool *force_again, bool *allow_force, bool is_continued);
1825 int fuse_readdir_backing(struct fuse_bpf_args *fa,
1826 			 struct file *file, struct dir_context *ctx,
1827 			 bool *force_again, bool *allow_force, bool is_continued);
1828 void *fuse_readdir_finalize(struct fuse_bpf_args *fa,
1829 			    struct file *file, struct dir_context *ctx,
1830 			    bool *force_again, bool *allow_force, bool is_continued);
1831 
1832 int fuse_access_initialize(struct fuse_bpf_args *fa, struct fuse_access_in *fai,
1833 			   struct inode *inode, int mask);
1834 int fuse_access_backing(struct fuse_bpf_args *fa, struct inode *inode, int mask);
1835 void *fuse_access_finalize(struct fuse_bpf_args *fa, struct inode *inode, int mask);
1836 
1837 /*
1838  * FUSE caches dentries and attributes with separate timeout.  The
1839  * time in jiffies until the dentry/attributes are valid is stored in
1840  * dentry->d_fsdata and fuse_inode->i_time respectively.
1841  */
1842 
1843 /*
1844  * Calculate the time in jiffies until a dentry/attributes are valid
1845  */
fuse_time_to_jiffies(u64 sec,u32 nsec)1846 inline u64 fuse_time_to_jiffies(u64 sec, u32 nsec)
1847 {
1848 	if (sec || nsec) {
1849 		struct timespec64 ts = {
1850 			sec,
1851 			min_t(u32, nsec, NSEC_PER_SEC - 1)
1852 		};
1853 
1854 		return get_jiffies_64() + timespec64_to_jiffies(&ts);
1855 	} else
1856 		return 0;
1857 }
1858 
attr_timeout(struct fuse_attr_out * o)1859 static inline u64 attr_timeout(struct fuse_attr_out *o)
1860 {
1861 	return fuse_time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
1862 }
1863 
update_mtime(unsigned int ivalid,bool trust_local_mtime)1864 static inline bool update_mtime(unsigned int ivalid, bool trust_local_mtime)
1865 {
1866 	/* Always update if mtime is explicitly set  */
1867 	if (ivalid & ATTR_MTIME_SET)
1868 		return true;
1869 
1870 	/* Or if kernel i_mtime is the official one */
1871 	if (trust_local_mtime)
1872 		return true;
1873 
1874 	/* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1875 	if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1876 		return false;
1877 
1878 	/* In all other cases update */
1879 	return true;
1880 }
1881 
1882 void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
1883 			  struct kstat *stat);
1884 
iattr_to_fattr(struct fuse_conn * fc,struct iattr * iattr,struct fuse_setattr_in * arg,bool trust_local_cmtime)1885 static inline void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr,
1886 			   struct fuse_setattr_in *arg, bool trust_local_cmtime)
1887 {
1888 	unsigned int ivalid = iattr->ia_valid;
1889 
1890 	if (ivalid & ATTR_MODE)
1891 		arg->valid |= FATTR_MODE,   arg->mode = iattr->ia_mode;
1892 	if (ivalid & ATTR_UID)
1893 		arg->valid |= FATTR_UID,    arg->uid = from_kuid(fc->user_ns, iattr->ia_uid);
1894 	if (ivalid & ATTR_GID)
1895 		arg->valid |= FATTR_GID,    arg->gid = from_kgid(fc->user_ns, iattr->ia_gid);
1896 	if (ivalid & ATTR_SIZE)
1897 		arg->valid |= FATTR_SIZE,   arg->size = iattr->ia_size;
1898 	if (ivalid & ATTR_ATIME) {
1899 		arg->valid |= FATTR_ATIME;
1900 		arg->atime = iattr->ia_atime.tv_sec;
1901 		arg->atimensec = iattr->ia_atime.tv_nsec;
1902 		if (!(ivalid & ATTR_ATIME_SET))
1903 			arg->valid |= FATTR_ATIME_NOW;
1904 	}
1905 	if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1906 		arg->valid |= FATTR_MTIME;
1907 		arg->mtime = iattr->ia_mtime.tv_sec;
1908 		arg->mtimensec = iattr->ia_mtime.tv_nsec;
1909 		if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1910 			arg->valid |= FATTR_MTIME_NOW;
1911 	}
1912 	if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1913 		arg->valid |= FATTR_CTIME;
1914 		arg->ctime = iattr->ia_ctime.tv_sec;
1915 		arg->ctimensec = iattr->ia_ctime.tv_nsec;
1916 	}
1917 }
1918 
finalize_attr(struct inode * inode,struct fuse_attr_out * outarg,u64 attr_version,struct kstat * stat)1919 static inline int finalize_attr(struct inode *inode, struct fuse_attr_out *outarg,
1920 				u64 attr_version, struct kstat *stat)
1921 {
1922 	int err = 0;
1923 
1924 	if (fuse_invalid_attr(&outarg->attr) ||
1925 	    ((inode->i_mode ^ outarg->attr.mode) & S_IFMT)) {
1926 		fuse_make_bad(inode);
1927 		err = -EIO;
1928 	} else {
1929 		fuse_change_attributes(inode, &outarg->attr, NULL,
1930 				       ATTR_TIMEOUT(outarg),
1931 				       attr_version);
1932 		if (stat)
1933 			fuse_fillattr(inode, &outarg->attr, stat);
1934 	}
1935 	return err;
1936 }
1937 
convert_statfs_to_fuse(struct fuse_kstatfs * attr,struct kstatfs * stbuf)1938 static inline void convert_statfs_to_fuse(struct fuse_kstatfs *attr, struct kstatfs *stbuf)
1939 {
1940 	attr->bsize   = stbuf->f_bsize;
1941 	attr->frsize  = stbuf->f_frsize;
1942 	attr->blocks  = stbuf->f_blocks;
1943 	attr->bfree   = stbuf->f_bfree;
1944 	attr->bavail  = stbuf->f_bavail;
1945 	attr->files   = stbuf->f_files;
1946 	attr->ffree   = stbuf->f_ffree;
1947 	attr->namelen = stbuf->f_namelen;
1948 	/* fsid is left zero */
1949 }
1950 
convert_fuse_statfs(struct kstatfs * stbuf,struct fuse_kstatfs * attr)1951 static inline void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
1952 {
1953 	stbuf->f_type    = FUSE_SUPER_MAGIC;
1954 	stbuf->f_bsize   = attr->bsize;
1955 	stbuf->f_frsize  = attr->frsize;
1956 	stbuf->f_blocks  = attr->blocks;
1957 	stbuf->f_bfree   = attr->bfree;
1958 	stbuf->f_bavail  = attr->bavail;
1959 	stbuf->f_files   = attr->files;
1960 	stbuf->f_ffree   = attr->ffree;
1961 	stbuf->f_namelen = attr->namelen;
1962 	/* fsid is left zero */
1963 }
1964 
1965 #ifdef CONFIG_FUSE_BPF
1966 struct fuse_err_ret {
1967 	void *result;
1968 	bool ret;
1969 };
1970 
1971 int __init fuse_bpf_init(void);
1972 void __exit fuse_bpf_cleanup(void);
1973 
1974 ssize_t fuse_bpf_simple_request(struct fuse_mount *fm, struct fuse_bpf_args *args);
1975 
fuse_bpf_run(struct bpf_prog * prog,struct fuse_bpf_args * fba)1976 static inline int fuse_bpf_run(struct bpf_prog *prog, struct fuse_bpf_args *fba)
1977 {
1978 	int ret;
1979 
1980 	migrate_disable();
1981 	ret = bpf_prog_run(prog, fba);
1982 	migrate_enable();
1983 	return ret;
1984 }
1985 
1986 /*
1987  * expression statement to wrap the backing filter logic
1988  * struct inode *inode: inode with bpf and backing inode
1989  * typedef io: (typically complex) type whose components fuse_args can point to.
1990  *	An instance of this type is created locally and passed to initialize
1991  * void initialize(struct fuse_bpf_args *fa, io *in_out, args...): function that sets
1992  *	up fa and io based on args
1993  * int backing(struct fuse_bpf_args *fa, args...): function that actually performs
1994  *	the backing io operation
1995  * void *finalize(struct fuse_bpf_args *, args...): function that performs any final
1996  *	work needed to commit the backing io
1997  */
1998 #define fuse_bpf_backing(inode, io, initialize, backing, finalize,	\
1999 			 args...)					\
2000 ({									\
2001 	struct fuse_err_ret fer = {0};					\
2002 	int ext_flags;							\
2003 	struct fuse_inode *fuse_inode = get_fuse_inode(inode);		\
2004 	struct fuse_mount *fm = get_fuse_mount(inode);			\
2005 	io feo = {0};							\
2006 	struct fuse_bpf_args fa = {0}, fa_backup = {0};			\
2007 	bool locked;							\
2008 	ssize_t res;							\
2009 	void *err;							\
2010 	int i;								\
2011 	bool initialized = false;					\
2012 									\
2013 	do {								\
2014 		if (!fuse_inode || !fuse_inode->backing_inode)		\
2015 			break;						\
2016 									\
2017 		err = ERR_PTR(initialize(&fa, &feo, args));		\
2018 		if (err) {						\
2019 			fer = (struct fuse_err_ret) {			\
2020 				err,					\
2021 				true,					\
2022 			};						\
2023 			break;						\
2024 		}							\
2025 		initialized = true;					\
2026 									\
2027 		fa_backup = fa;						\
2028 		fa.opcode |= FUSE_PREFILTER;				\
2029 		for (i = 0; i < fa.in_numargs; ++i)			\
2030 			fa.out_args[i] = (struct fuse_bpf_arg) {	\
2031 				.size = fa.in_args[i].size,		\
2032 				.value = (void *)fa.in_args[i].value,	\
2033 			};						\
2034 		fa.out_numargs = fa.in_numargs;				\
2035 									\
2036 		ext_flags = fuse_inode->bpf ?				\
2037 			fuse_bpf_run(fuse_inode->bpf, &fa) :		\
2038 			FUSE_BPF_BACKING;				\
2039 		if (ext_flags < 0) {					\
2040 			fer = (struct fuse_err_ret) {			\
2041 				ERR_PTR(ext_flags),			\
2042 				true,					\
2043 			};						\
2044 			break;						\
2045 		}							\
2046 									\
2047 		if (ext_flags & FUSE_BPF_USER_FILTER) {			\
2048 			locked = fuse_lock_inode(inode);		\
2049 			res = fuse_bpf_simple_request(fm, &fa);		\
2050 			fuse_unlock_inode(inode, locked);		\
2051 			if (res < 0) {					\
2052 				fer = (struct fuse_err_ret) {		\
2053 					ERR_PTR(res),			\
2054 					true,				\
2055 				};					\
2056 				break;					\
2057 			}						\
2058 		}							\
2059 									\
2060 		if (!(ext_flags & FUSE_BPF_BACKING))			\
2061 			break;						\
2062 									\
2063 		fa.opcode &= ~FUSE_PREFILTER;				\
2064 		for (i = 0; i < fa.in_numargs; ++i)			\
2065 			fa.in_args[i] = (struct fuse_bpf_in_arg) {	\
2066 				.size = fa.out_args[i].size,		\
2067 				.value = fa.out_args[i].value,		\
2068 			};						\
2069 		for (i = 0; i < fa_backup.out_numargs; ++i)		\
2070 			fa.out_args[i] = (struct fuse_bpf_arg) {	\
2071 				.size = fa_backup.out_args[i].size,	\
2072 				.value = fa_backup.out_args[i].value,	\
2073 			};						\
2074 		fa.out_numargs = fa_backup.out_numargs;			\
2075 									\
2076 		fer = (struct fuse_err_ret) {				\
2077 			ERR_PTR(backing(&fa, args)),			\
2078 			true,						\
2079 		};							\
2080 		if (IS_ERR(fer.result))					\
2081 			fa.error_in = PTR_ERR(fer.result);		\
2082 		if (!(ext_flags & FUSE_BPF_POST_FILTER))		\
2083 			break;						\
2084 									\
2085 		fa.opcode |= FUSE_POSTFILTER;				\
2086 		for (i = 0; i < fa.out_numargs; ++i)			\
2087 			fa.in_args[fa.in_numargs++] =			\
2088 				(struct fuse_bpf_in_arg) {		\
2089 					.size = fa.out_args[i].size,	\
2090 					.value = fa.out_args[i].value,	\
2091 				};					\
2092 		ext_flags = fuse_bpf_run(fuse_inode->bpf, &fa);		\
2093 		if (ext_flags < 0) {					\
2094 			fer = (struct fuse_err_ret) {			\
2095 				ERR_PTR(ext_flags),			\
2096 				true,					\
2097 			};						\
2098 			break;						\
2099 		}							\
2100 		if (!(ext_flags & FUSE_BPF_USER_FILTER))		\
2101 			break;						\
2102 									\
2103 		fa.out_args[0].size = fa_backup.out_args[0].size;	\
2104 		fa.out_args[1].size = fa_backup.out_args[1].size;	\
2105 		fa.out_numargs = fa_backup.out_numargs;			\
2106 		locked = fuse_lock_inode(inode);			\
2107 		res = fuse_bpf_simple_request(fm, &fa);			\
2108 		fuse_unlock_inode(inode, locked);			\
2109 		if (res < 0) {						\
2110 			fer.result = ERR_PTR(res);			\
2111 			break;						\
2112 		}							\
2113 	} while (false);						\
2114 									\
2115 	if (initialized && fer.ret) {					\
2116 		err = finalize(&fa, args);				\
2117 		if (err)						\
2118 			fer.result = err;				\
2119 	}								\
2120 									\
2121 	fer;								\
2122 })
2123 
2124 #endif /* CONFIG_FUSE_BPF */
2125 
2126 #endif /* _FS_FUSE_I_H */
2127