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