• 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 	/** BPF Only, no Daemon running */
873 	unsigned int no_daemon:1;
874 
875 	/** The number of requests waiting for completion */
876 	atomic_t num_waiting;
877 
878 	/** Negotiated minor version */
879 	unsigned minor;
880 
881 	/** Entry on the fuse_mount_list */
882 	struct list_head entry;
883 
884 	/** Device ID from the root super block */
885 	dev_t dev;
886 
887 	/** Dentries in the control filesystem */
888 	struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES];
889 
890 	/** number of dentries used in the above array */
891 	int ctl_ndents;
892 
893 	/** Key for lock owner ID scrambling */
894 	u32 scramble_key[4];
895 
896 	/** Version counter for attribute changes */
897 	atomic64_t attr_version;
898 
899 	/** Called on final put */
900 	void (*release)(struct fuse_conn *);
901 
902 	/**
903 	 * Read/write semaphore to hold when accessing the sb of any
904 	 * fuse_mount belonging to this connection
905 	 */
906 	struct rw_semaphore killsb;
907 
908 	/** List of device instances belonging to this connection */
909 	struct list_head devices;
910 
911 #ifdef CONFIG_FUSE_DAX
912 	/* Dax specific conn data, non-NULL if DAX is enabled */
913 	struct fuse_conn_dax *dax;
914 #endif
915 
916 	/** List of filesystems using this connection */
917 	struct list_head mounts;
918 
919 	/* New writepages go into this bucket */
920 	struct fuse_sync_bucket __rcu *curr_bucket;
921 
922 	/** IDR for passthrough requests */
923 	struct idr passthrough_req;
924 
925 	/** Protects passthrough_req */
926 	spinlock_t passthrough_req_lock;
927 };
928 
929 /*
930  * Represents a mounted filesystem, potentially a submount.
931  *
932  * This object allows sharing a fuse_conn between separate mounts to
933  * allow submounts with dedicated superblocks and thus separate device
934  * IDs.
935  */
936 struct fuse_mount {
937 	/* Underlying (potentially shared) connection to the FUSE server */
938 	struct fuse_conn *fc;
939 
940 	/*
941 	 * Super block for this connection (fc->killsb must be held when
942 	 * accessing this).
943 	 */
944 	struct super_block *sb;
945 
946 	/* Entry on fc->mounts */
947 	struct list_head fc_entry;
948 };
949 
get_fuse_mount_super(struct super_block * sb)950 static inline struct fuse_mount *get_fuse_mount_super(struct super_block *sb)
951 {
952 	return sb->s_fs_info;
953 }
954 
get_fuse_conn_super(struct super_block * sb)955 static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
956 {
957 	return get_fuse_mount_super(sb)->fc;
958 }
959 
get_fuse_mount(struct inode * inode)960 static inline struct fuse_mount *get_fuse_mount(struct inode *inode)
961 {
962 	return get_fuse_mount_super(inode->i_sb);
963 }
964 
get_fuse_conn(struct inode * inode)965 static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
966 {
967 	return get_fuse_mount_super(inode->i_sb)->fc;
968 }
969 
get_fuse_inode(struct inode * inode)970 static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
971 {
972 	return container_of(inode, struct fuse_inode, inode);
973 }
974 
get_node_id(struct inode * inode)975 static inline u64 get_node_id(struct inode *inode)
976 {
977 	return get_fuse_inode(inode)->nodeid;
978 }
979 
invalid_nodeid(u64 nodeid)980 static inline int invalid_nodeid(u64 nodeid)
981 {
982 	return !nodeid || nodeid == FUSE_ROOT_ID;
983 }
984 
fuse_get_attr_version(struct fuse_conn * fc)985 static inline u64 fuse_get_attr_version(struct fuse_conn *fc)
986 {
987 	return atomic64_read(&fc->attr_version);
988 }
989 
fuse_stale_inode(const struct inode * inode,int generation,struct fuse_attr * attr)990 static inline bool fuse_stale_inode(const struct inode *inode, int generation,
991 				    struct fuse_attr *attr)
992 {
993 	return inode->i_generation != generation ||
994 		inode_wrong_type(inode, attr->mode);
995 }
996 
fuse_make_bad(struct inode * inode)997 static inline void fuse_make_bad(struct inode *inode)
998 {
999 	remove_inode_hash(inode);
1000 	set_bit(FUSE_I_BAD, &get_fuse_inode(inode)->state);
1001 }
1002 
fuse_is_bad(struct inode * inode)1003 static inline bool fuse_is_bad(struct inode *inode)
1004 {
1005 	return unlikely(test_bit(FUSE_I_BAD, &get_fuse_inode(inode)->state));
1006 }
1007 
fuse_pages_alloc(unsigned int npages,gfp_t flags,struct fuse_page_desc ** desc)1008 static inline struct page **fuse_pages_alloc(unsigned int npages, gfp_t flags,
1009 					     struct fuse_page_desc **desc)
1010 {
1011 	struct page **pages;
1012 
1013 	pages = kzalloc(npages * (sizeof(struct page *) +
1014 				  sizeof(struct fuse_page_desc)), flags);
1015 	*desc = (void *) (pages + npages);
1016 
1017 	return pages;
1018 }
1019 
fuse_page_descs_length_init(struct fuse_page_desc * descs,unsigned int index,unsigned int nr_pages)1020 static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs,
1021 					       unsigned int index,
1022 					       unsigned int nr_pages)
1023 {
1024 	int i;
1025 
1026 	for (i = index; i < index + nr_pages; i++)
1027 		descs[i].length = PAGE_SIZE - descs[i].offset;
1028 }
1029 
fuse_sync_bucket_dec(struct fuse_sync_bucket * bucket)1030 static inline void fuse_sync_bucket_dec(struct fuse_sync_bucket *bucket)
1031 {
1032 	/* Need RCU protection to prevent use after free after the decrement */
1033 	rcu_read_lock();
1034 	if (atomic_dec_and_test(&bucket->count))
1035 		wake_up(&bucket->waitq);
1036 	rcu_read_unlock();
1037 }
1038 
1039 /** Device operations */
1040 extern const struct file_operations fuse_dev_operations;
1041 
1042 extern const struct dentry_operations fuse_dentry_operations;
1043 extern const struct dentry_operations fuse_root_dentry_operations;
1044 
1045 /**
1046  * Get a filled-in inode
1047  */
1048 struct inode *fuse_iget_backing(struct super_block *sb,
1049 				u64 nodeid,
1050 				struct inode *backing_inode);
1051 struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
1052 			int generation, struct fuse_attr *attr,
1053 			u64 attr_valid, u64 attr_version);
1054 
1055 int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name,
1056 		     struct fuse_entry_out *outarg,
1057 		     struct dentry *entry, struct inode **inode);
1058 
1059 /**
1060  * Send FORGET command
1061  */
1062 void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
1063 		       u64 nodeid, u64 nlookup);
1064 
1065 struct fuse_forget_link *fuse_alloc_forget(void);
1066 
1067 struct fuse_forget_link *fuse_dequeue_forget(struct fuse_iqueue *fiq,
1068 					     unsigned int max,
1069 					     unsigned int *countp);
1070 
1071 /*
1072  * Initialize READ or READDIR request
1073  */
1074 struct fuse_io_args {
1075 	union {
1076 		struct {
1077 			struct fuse_read_in in;
1078 			u64 attr_ver;
1079 		} read;
1080 		struct {
1081 			struct fuse_write_in in;
1082 			struct fuse_write_out out;
1083 			bool page_locked;
1084 		} write;
1085 	};
1086 	struct fuse_args_pages ap;
1087 	struct fuse_io_priv *io;
1088 	struct fuse_file *ff;
1089 };
1090 
1091 void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
1092 			 size_t count, int opcode);
1093 
1094 /**
1095  * Send OPEN or OPENDIR request
1096  */
1097 int fuse_open_common(struct inode *inode, struct file *file, bool isdir);
1098 
1099 struct fuse_file *fuse_file_alloc(struct fuse_mount *fm);
1100 void fuse_file_free(struct fuse_file *ff);
1101 void fuse_finish_open(struct inode *inode, struct file *file);
1102 
1103 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff,
1104 		       unsigned int flags);
1105 
1106 /**
1107  * Send RELEASE or RELEASEDIR request
1108  */
1109 void fuse_release_common(struct file *file, bool isdir);
1110 
1111 /**
1112  * Send FSYNC or FSYNCDIR request
1113  */
1114 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
1115 		      int datasync, int opcode);
1116 
1117 /**
1118  * Notify poll wakeup
1119  */
1120 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
1121 			    struct fuse_notify_poll_wakeup_out *outarg);
1122 
1123 /**
1124  * Initialize file operations on a regular file
1125  */
1126 void fuse_init_file_inode(struct inode *inode);
1127 
1128 /**
1129  * Initialize inode operations on regular files and special files
1130  */
1131 void fuse_init_common(struct inode *inode);
1132 
1133 /**
1134  * Initialize inode and file operations on a directory
1135  */
1136 void fuse_init_dir(struct inode *inode);
1137 
1138 /**
1139  * Initialize inode operations on a symlink
1140  */
1141 void fuse_init_symlink(struct inode *inode);
1142 
1143 /**
1144  * Change attributes of an inode
1145  */
1146 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
1147 			    u64 attr_valid, u64 attr_version);
1148 
1149 void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
1150 				   u64 attr_valid);
1151 
1152 /**
1153  * Initialize the client device
1154  */
1155 int fuse_dev_init(void);
1156 
1157 /**
1158  * Cleanup the client device
1159  */
1160 void fuse_dev_cleanup(void);
1161 
1162 int fuse_ctl_init(void);
1163 void fuse_ctl_cleanup(void);
1164 
1165 /**
1166  * Simple request sending that does request allocation and freeing
1167  */
1168 ssize_t fuse_simple_request(struct fuse_mount *fm, struct fuse_args *args);
1169 int fuse_simple_background(struct fuse_mount *fm, struct fuse_args *args,
1170 			   gfp_t gfp_flags);
1171 
1172 /**
1173  * End a finished request
1174  */
1175 void fuse_request_end(struct fuse_req *req);
1176 
1177 /* Abort all requests */
1178 void fuse_abort_conn(struct fuse_conn *fc);
1179 void fuse_wait_aborted(struct fuse_conn *fc);
1180 
1181 /**
1182  * Invalidate inode attributes
1183  */
1184 void fuse_invalidate_attr(struct inode *inode);
1185 
1186 void fuse_invalidate_entry_cache(struct dentry *entry);
1187 
1188 void fuse_invalidate_atime(struct inode *inode);
1189 
1190 u64 entry_attr_timeout(struct fuse_entry_out *o);
1191 void fuse_init_dentry_root(struct dentry *root, struct file *backing_dir);
1192 void fuse_change_entry_timeout(struct dentry *entry, struct fuse_entry_out *o);
1193 
1194 /**
1195  * Acquire reference to fuse_conn
1196  */
1197 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
1198 
1199 /**
1200  * Initialize fuse_conn
1201  */
1202 void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm,
1203 		    struct user_namespace *user_ns,
1204 		    const struct fuse_iqueue_ops *fiq_ops, void *fiq_priv);
1205 
1206 /**
1207  * Release reference to fuse_conn
1208  */
1209 void fuse_conn_put(struct fuse_conn *fc);
1210 
1211 struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc);
1212 struct fuse_dev *fuse_dev_alloc(void);
1213 void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc);
1214 void fuse_dev_free(struct fuse_dev *fud);
1215 void fuse_send_init(struct fuse_mount *fm);
1216 
1217 /**
1218  * Fill in superblock and initialize fuse connection
1219  * @sb: partially-initialized superblock to fill in
1220  * @ctx: mount context
1221  */
1222 int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx);
1223 
1224 /*
1225  * Remove the mount from the connection
1226  *
1227  * Returns whether this was the last mount
1228  */
1229 bool fuse_mount_remove(struct fuse_mount *fm);
1230 
1231 /*
1232  * Setup context ops for submounts
1233  */
1234 int fuse_init_fs_context_submount(struct fs_context *fsc);
1235 
1236 /*
1237  * Shut down the connection (possibly sending DESTROY request).
1238  */
1239 void fuse_conn_destroy(struct fuse_mount *fm);
1240 
1241 /* Drop the connection and free the fuse mount */
1242 void fuse_mount_destroy(struct fuse_mount *fm);
1243 
1244 /**
1245  * Add connection to control filesystem
1246  */
1247 int fuse_ctl_add_conn(struct fuse_conn *fc);
1248 
1249 /**
1250  * Remove connection from control filesystem
1251  */
1252 void fuse_ctl_remove_conn(struct fuse_conn *fc);
1253 
1254 /**
1255  * Is file type valid?
1256  */
1257 int fuse_valid_type(int m);
1258 
1259 bool fuse_invalid_attr(struct fuse_attr *attr);
1260 
1261 /**
1262  * Is current process allowed to perform filesystem operation?
1263  */
1264 int fuse_allow_current_process(struct fuse_conn *fc);
1265 
1266 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id);
1267 
1268 void fuse_flush_time_update(struct inode *inode);
1269 void fuse_update_ctime(struct inode *inode);
1270 
1271 int fuse_update_attributes(struct inode *inode, struct file *file);
1272 
1273 void fuse_flush_writepages(struct inode *inode);
1274 
1275 void fuse_set_nowrite(struct inode *inode);
1276 void fuse_release_nowrite(struct inode *inode);
1277 
1278 /**
1279  * Scan all fuse_mounts belonging to fc to find the first where
1280  * ilookup5() returns a result.  Return that result and the
1281  * respective fuse_mount in *fm (unless fm is NULL).
1282  *
1283  * The caller must hold fc->killsb.
1284  */
1285 struct inode *fuse_ilookup(struct fuse_conn *fc, u64 nodeid,
1286 			   struct fuse_mount **fm);
1287 
1288 /**
1289  * File-system tells the kernel to invalidate cache for the given node id.
1290  */
1291 int fuse_reverse_inval_inode(struct fuse_conn *fc, u64 nodeid,
1292 			     loff_t offset, loff_t len);
1293 
1294 /**
1295  * File-system tells the kernel to invalidate parent attributes and
1296  * the dentry matching parent/name.
1297  *
1298  * If the child_nodeid is non-zero and:
1299  *    - matches the inode number for the dentry matching parent/name,
1300  *    - is not a mount point
1301  *    - is a file or oan empty directory
1302  * then the dentry is unhashed (d_delete()).
1303  */
1304 int fuse_reverse_inval_entry(struct fuse_conn *fc, u64 parent_nodeid,
1305 			     u64 child_nodeid, struct qstr *name);
1306 
1307 int fuse_do_open(struct fuse_mount *fm, u64 nodeid, struct file *file,
1308 		 bool isdir);
1309 
1310 /**
1311  * fuse_direct_io() flags
1312  */
1313 
1314 /** If set, it is WRITE; otherwise - READ */
1315 #define FUSE_DIO_WRITE (1 << 0)
1316 
1317 /** CUSE pass fuse_direct_io() a file which f_mapping->host is not from FUSE */
1318 #define FUSE_DIO_CUSE  (1 << 1)
1319 
1320 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1321 		       loff_t *ppos, int flags);
1322 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
1323 		   unsigned int flags);
1324 long fuse_ioctl_common(struct file *file, unsigned int cmd,
1325 		       unsigned long arg, unsigned int flags);
1326 __poll_t fuse_file_poll(struct file *file, poll_table *wait);
1327 int fuse_dev_release(struct inode *inode, struct file *file);
1328 
1329 bool fuse_write_update_size(struct inode *inode, loff_t pos);
1330 
1331 int fuse_flush_times(struct inode *inode, struct fuse_file *ff);
1332 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc);
1333 
1334 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
1335 		    struct file *file);
1336 
1337 void fuse_set_initialized(struct fuse_conn *fc);
1338 
1339 void fuse_unlock_inode(struct inode *inode, bool locked);
1340 bool fuse_lock_inode(struct inode *inode);
1341 
1342 int fuse_setxattr(struct inode *inode, const char *name, const void *value,
1343 		  size_t size, int flags, unsigned int extra_flags);
1344 ssize_t fuse_getxattr(struct inode *inode, const char *name, void *value,
1345 		      size_t size);
1346 ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size);
1347 int fuse_removexattr(struct inode *inode, const char *name);
1348 extern const struct xattr_handler *fuse_xattr_handlers[];
1349 extern const struct xattr_handler *fuse_acl_xattr_handlers[];
1350 extern const struct xattr_handler *fuse_no_acl_xattr_handlers[];
1351 
1352 struct posix_acl;
1353 struct posix_acl *fuse_get_acl(struct inode *inode, int type, bool rcu);
1354 int fuse_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
1355 		 struct posix_acl *acl, int type);
1356 
1357 /* readdir.c */
1358 int fuse_readdir(struct file *file, struct dir_context *ctx);
1359 
1360 /**
1361  * Return the number of bytes in an arguments list
1362  */
1363 unsigned int fuse_len_args(unsigned int numargs, struct fuse_arg *args);
1364 
1365 /**
1366  * Get the next unique ID for a request
1367  */
1368 u64 fuse_get_unique(struct fuse_iqueue *fiq);
1369 void fuse_free_conn(struct fuse_conn *fc);
1370 
1371 /* dax.c */
1372 
1373 #define FUSE_IS_DAX(inode) (IS_ENABLED(CONFIG_FUSE_DAX) && IS_DAX(inode))
1374 
1375 ssize_t fuse_dax_read_iter(struct kiocb *iocb, struct iov_iter *to);
1376 ssize_t fuse_dax_write_iter(struct kiocb *iocb, struct iov_iter *from);
1377 int fuse_dax_mmap(struct file *file, struct vm_area_struct *vma);
1378 int fuse_dax_break_layouts(struct inode *inode, u64 dmap_start, u64 dmap_end);
1379 int fuse_dax_conn_alloc(struct fuse_conn *fc, struct dax_device *dax_dev);
1380 void fuse_dax_conn_free(struct fuse_conn *fc);
1381 bool fuse_dax_inode_alloc(struct super_block *sb, struct fuse_inode *fi);
1382 void fuse_dax_inode_init(struct inode *inode);
1383 void fuse_dax_inode_cleanup(struct inode *inode);
1384 bool fuse_dax_check_alignment(struct fuse_conn *fc, unsigned int map_alignment);
1385 void fuse_dax_cancel_work(struct fuse_conn *fc);
1386 
1387 /* ioctl.c */
1388 long fuse_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
1389 long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
1390 			    unsigned long arg);
1391 int fuse_fileattr_get(struct dentry *dentry, struct fileattr *fa);
1392 int fuse_fileattr_set(struct user_namespace *mnt_userns,
1393 		      struct dentry *dentry, struct fileattr *fa);
1394 
1395 /* file.c */
1396 
1397 struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid,
1398 				 unsigned int open_flags, bool isdir);
1399 void fuse_file_release(struct inode *inode, struct fuse_file *ff,
1400 		       unsigned int open_flags, fl_owner_t id, bool isdir);
1401 
1402 /* passthrough.c */
1403 void fuse_copyattr(struct file *dst_file, struct file *src_file);
1404 int fuse_passthrough_open(struct fuse_dev *fud, u32 lower_fd);
1405 int fuse_passthrough_setup(struct fuse_conn *fc, struct fuse_file *ff,
1406 			   struct fuse_open_out *openarg);
1407 void fuse_passthrough_release(struct fuse_passthrough *passthrough);
1408 ssize_t fuse_passthrough_read_iter(struct kiocb *iocb, struct iov_iter *to);
1409 ssize_t fuse_passthrough_write_iter(struct kiocb *iocb, struct iov_iter *from);
1410 ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma);
1411 
1412 /* backing.c */
1413 
1414 /*
1415  * Dummy io passed to fuse_bpf_backing when io operation needs no scratch space
1416  */
1417 struct fuse_dummy_io {
1418 	int unused;
1419 };
1420 
1421 struct fuse_open_io {
1422 	struct fuse_open_in foi;
1423 	struct fuse_open_out foo;
1424 };
1425 
1426 int fuse_open_initialize(struct fuse_bpf_args *fa, struct fuse_open_io *foi,
1427 			 struct inode *inode, struct file *file, bool isdir);
1428 int fuse_open_backing(struct fuse_bpf_args *fa,
1429 		      struct inode *inode, struct file *file, bool isdir);
1430 void *fuse_open_finalize(struct fuse_bpf_args *fa,
1431 		       struct inode *inode, struct file *file, bool isdir);
1432 
1433 struct fuse_create_open_io {
1434 	struct fuse_create_in fci;
1435 	struct fuse_entry_out feo;
1436 	struct fuse_open_out foo;
1437 };
1438 
1439 int fuse_create_open_initialize(
1440 		struct fuse_bpf_args *fa, struct fuse_create_open_io *fcoi,
1441 		struct inode *dir, struct dentry *entry,
1442 		struct file *file, unsigned int flags, umode_t mode);
1443 int fuse_create_open_backing(
1444 		struct fuse_bpf_args *fa,
1445 		struct inode *dir, struct dentry *entry,
1446 		struct file *file, unsigned int flags, umode_t mode);
1447 void *fuse_create_open_finalize(
1448 		struct fuse_bpf_args *fa,
1449 		struct inode *dir, struct dentry *entry,
1450 		struct file *file, unsigned int flags, umode_t mode);
1451 
1452 int fuse_mknod_initialize(
1453 		struct fuse_bpf_args *fa, struct fuse_mknod_in *fmi,
1454 		struct inode *dir, struct dentry *entry, umode_t mode, dev_t rdev);
1455 int fuse_mknod_backing(
1456 		struct fuse_bpf_args *fa,
1457 		struct inode *dir, struct dentry *entry, umode_t mode, dev_t rdev);
1458 void *fuse_mknod_finalize(
1459 		struct fuse_bpf_args *fa,
1460 		struct inode *dir, struct dentry *entry, umode_t mode, dev_t rdev);
1461 
1462 int fuse_mkdir_initialize(
1463 		struct fuse_bpf_args *fa, struct fuse_mkdir_in *fmi,
1464 		struct inode *dir, struct dentry *entry, umode_t mode);
1465 int fuse_mkdir_backing(
1466 		struct fuse_bpf_args *fa,
1467 		struct inode *dir, struct dentry *entry, umode_t mode);
1468 void *fuse_mkdir_finalize(
1469 		struct fuse_bpf_args *fa,
1470 		struct inode *dir, struct dentry *entry, umode_t mode);
1471 
1472 int fuse_rmdir_initialize(
1473 		struct fuse_bpf_args *fa, struct fuse_dummy_io *fmi,
1474 		struct inode *dir, struct dentry *entry);
1475 int fuse_rmdir_backing(
1476 		struct fuse_bpf_args *fa,
1477 		struct inode *dir, struct dentry *entry);
1478 void *fuse_rmdir_finalize(
1479 		struct fuse_bpf_args *fa,
1480 		struct inode *dir, struct dentry *entry);
1481 
1482 int fuse_rename2_initialize(struct fuse_bpf_args *fa, struct fuse_rename2_in *fri,
1483 			    struct inode *olddir, struct dentry *oldent,
1484 			    struct inode *newdir, struct dentry *newent,
1485 			    unsigned int flags);
1486 int fuse_rename2_backing(struct fuse_bpf_args *fa,
1487 			 struct inode *olddir, struct dentry *oldent,
1488 			 struct inode *newdir, struct dentry *newent,
1489 			 unsigned int flags);
1490 void *fuse_rename2_finalize(struct fuse_bpf_args *fa,
1491 			    struct inode *olddir, struct dentry *oldent,
1492 			    struct inode *newdir, struct dentry *newent,
1493 			    unsigned int flags);
1494 
1495 int fuse_rename_initialize(struct fuse_bpf_args *fa, struct fuse_rename_in *fri,
1496 			   struct inode *olddir, struct dentry *oldent,
1497 			   struct inode *newdir, struct dentry *newent);
1498 int fuse_rename_backing(struct fuse_bpf_args *fa,
1499 			struct inode *olddir, struct dentry *oldent,
1500 			struct inode *newdir, struct dentry *newent);
1501 void *fuse_rename_finalize(struct fuse_bpf_args *fa,
1502 			   struct inode *olddir, struct dentry *oldent,
1503 			   struct inode *newdir, struct dentry *newent);
1504 
1505 int fuse_unlink_initialize(
1506 		struct fuse_bpf_args *fa, struct fuse_dummy_io *fmi,
1507 		struct inode *dir, struct dentry *entry);
1508 int fuse_unlink_backing(
1509 		struct fuse_bpf_args *fa,
1510 		struct inode *dir, struct dentry *entry);
1511 void *fuse_unlink_finalize(
1512 		struct fuse_bpf_args *fa,
1513 		struct inode *dir, struct dentry *entry);
1514 
1515 int fuse_link_initialize(struct fuse_bpf_args *fa, struct fuse_link_in *fli,
1516 			  struct dentry *entry, struct inode *dir,
1517 			  struct dentry *newent);
1518 int fuse_link_backing(struct fuse_bpf_args *fa, struct dentry *entry,
1519 		      struct inode *dir, struct dentry *newent);
1520 void *fuse_link_finalize(struct fuse_bpf_args *fa, struct dentry *entry,
1521 			 struct inode *dir, struct dentry *newent);
1522 
1523 int fuse_release_initialize(struct fuse_bpf_args *fa, struct fuse_release_in *fri,
1524 			    struct inode *inode, struct fuse_file *ff);
1525 int fuse_release_backing(struct fuse_bpf_args *fa,
1526 			 struct inode *inode, struct fuse_file *ff);
1527 void *fuse_release_finalize(struct fuse_bpf_args *fa,
1528 			    struct inode *inode, struct fuse_file *ff);
1529 
1530 int fuse_flush_initialize(struct fuse_bpf_args *fa, struct fuse_flush_in *ffi,
1531 			  struct file *file, fl_owner_t id);
1532 int fuse_flush_backing(struct fuse_bpf_args *fa, struct file *file, fl_owner_t id);
1533 void *fuse_flush_finalize(struct fuse_bpf_args *fa,
1534 			  struct file *file, fl_owner_t id);
1535 
1536 struct fuse_lseek_io {
1537 	struct fuse_lseek_in fli;
1538 	struct fuse_lseek_out flo;
1539 };
1540 
1541 int fuse_lseek_initialize(struct fuse_bpf_args *fa, struct fuse_lseek_io *fli,
1542 			  struct file *file, loff_t offset, int whence);
1543 int fuse_lseek_backing(struct fuse_bpf_args *fa, struct file *file, loff_t offset, int whence);
1544 void *fuse_lseek_finalize(struct fuse_bpf_args *fa, struct file *file, loff_t offset, int whence);
1545 
1546 struct fuse_copy_file_range_io {
1547 	struct fuse_copy_file_range_in fci;
1548 	struct fuse_write_out fwo;
1549 };
1550 
1551 int fuse_copy_file_range_initialize(struct fuse_bpf_args *fa,
1552 				   struct fuse_copy_file_range_io *fcf,
1553 				   struct file *file_in, loff_t pos_in,
1554 				   struct file *file_out, loff_t pos_out,
1555 				   size_t len, unsigned int flags);
1556 int fuse_copy_file_range_backing(struct fuse_bpf_args *fa,
1557 				 struct file *file_in, loff_t pos_in,
1558 				 struct file *file_out, loff_t pos_out,
1559 				 size_t len, unsigned int flags);
1560 void *fuse_copy_file_range_finalize(struct fuse_bpf_args *fa,
1561 				    struct file *file_in, loff_t pos_in,
1562 				    struct file *file_out, loff_t pos_out,
1563 				    size_t len, unsigned int flags);
1564 
1565 int fuse_fsync_initialize(struct fuse_bpf_args *fa, struct fuse_fsync_in *ffi,
1566 		   struct file *file, loff_t start, loff_t end, int datasync);
1567 int fuse_fsync_backing(struct fuse_bpf_args *fa,
1568 		   struct file *file, loff_t start, loff_t end, int datasync);
1569 void *fuse_fsync_finalize(struct fuse_bpf_args *fa,
1570 		   struct file *file, loff_t start, loff_t end, int datasync);
1571 int fuse_dir_fsync_initialize(struct fuse_bpf_args *fa, struct fuse_fsync_in *ffi,
1572 		   struct file *file, loff_t start, loff_t end, int datasync);
1573 
1574 struct fuse_getxattr_io {
1575 	struct fuse_getxattr_in fgi;
1576 	struct fuse_getxattr_out fgo;
1577 };
1578 
1579 int fuse_getxattr_initialize(
1580 		struct fuse_bpf_args *fa, struct fuse_getxattr_io *fgio,
1581 		struct dentry *dentry, const char *name, void *value,
1582 		size_t size);
1583 int fuse_getxattr_backing(
1584 		struct fuse_bpf_args *fa,
1585 		struct dentry *dentry, const char *name, void *value,
1586 		size_t size);
1587 void *fuse_getxattr_finalize(
1588 		struct fuse_bpf_args *fa,
1589 		struct dentry *dentry, const char *name, void *value,
1590 		size_t size);
1591 
1592 int fuse_listxattr_initialize(struct fuse_bpf_args *fa,
1593 			       struct fuse_getxattr_io *fgio,
1594 			       struct dentry *dentry, char *list, size_t size);
1595 int fuse_listxattr_backing(struct fuse_bpf_args *fa, struct dentry *dentry,
1596 			   char *list, size_t size);
1597 void *fuse_listxattr_finalize(struct fuse_bpf_args *fa, struct dentry *dentry,
1598 			      char *list, size_t size);
1599 
1600 int fuse_setxattr_initialize(struct fuse_bpf_args *fa,
1601 			     struct fuse_setxattr_in *fsxi,
1602 			     struct dentry *dentry, const char *name,
1603 			     const void *value, size_t size, int flags);
1604 int fuse_setxattr_backing(struct fuse_bpf_args *fa, struct dentry *dentry,
1605 			  const char *name, const void *value, size_t size,
1606 			  int flags);
1607 void *fuse_setxattr_finalize(struct fuse_bpf_args *fa, struct dentry *dentry,
1608 			     const char *name, const void *value, size_t size,
1609 			     int flags);
1610 
1611 int fuse_removexattr_initialize(struct fuse_bpf_args *fa,
1612 				struct fuse_dummy_io *unused,
1613 				struct dentry *dentry, const char *name);
1614 int fuse_removexattr_backing(struct fuse_bpf_args *fa,
1615 			     struct dentry *dentry, const char *name);
1616 void *fuse_removexattr_finalize(struct fuse_bpf_args *fa,
1617 				struct dentry *dentry, const char *name);
1618 
1619 struct fuse_read_iter_out {
1620 	uint64_t ret;
1621 };
1622 struct fuse_file_read_iter_io {
1623 	struct fuse_read_in fri;
1624 	struct fuse_read_iter_out frio;
1625 };
1626 
1627 int fuse_file_read_iter_initialize(
1628 		struct fuse_bpf_args *fa, struct fuse_file_read_iter_io *fri,
1629 		struct kiocb *iocb, struct iov_iter *to);
1630 int fuse_file_read_iter_backing(struct fuse_bpf_args *fa,
1631 		struct kiocb *iocb, struct iov_iter *to);
1632 void *fuse_file_read_iter_finalize(struct fuse_bpf_args *fa,
1633 		struct kiocb *iocb, struct iov_iter *to);
1634 
1635 struct fuse_write_iter_out {
1636 	uint64_t ret;
1637 };
1638 struct fuse_file_write_iter_io {
1639 	struct fuse_write_in fwi;
1640 	struct fuse_write_out fwo;
1641 	struct fuse_write_iter_out fwio;
1642 };
1643 
1644 int fuse_file_write_iter_initialize(
1645 		struct fuse_bpf_args *fa, struct fuse_file_write_iter_io *fwio,
1646 		struct kiocb *iocb, struct iov_iter *from);
1647 int fuse_file_write_iter_backing(struct fuse_bpf_args *fa,
1648 		struct kiocb *iocb, struct iov_iter *from);
1649 void *fuse_file_write_iter_finalize(struct fuse_bpf_args *fa,
1650 		struct kiocb *iocb, struct iov_iter *from);
1651 
1652 long fuse_backing_ioctl(struct file *file, unsigned int command, unsigned long arg, int flags);
1653 
1654 int fuse_file_flock_backing(struct file *file, int cmd, struct file_lock *fl);
1655 ssize_t fuse_backing_mmap(struct file *file, struct vm_area_struct *vma);
1656 
1657 int fuse_file_fallocate_initialize(struct fuse_bpf_args *fa,
1658 		struct fuse_fallocate_in *ffi,
1659 		struct file *file, int mode, loff_t offset, loff_t length);
1660 int fuse_file_fallocate_backing(struct fuse_bpf_args *fa,
1661 		struct file *file, int mode, loff_t offset, loff_t length);
1662 void *fuse_file_fallocate_finalize(struct fuse_bpf_args *fa,
1663 		struct file *file, int mode, loff_t offset, loff_t length);
1664 
1665 struct fuse_lookup_io {
1666 	struct fuse_entry_out feo;
1667 	struct fuse_entry_bpf feb;
1668 };
1669 
1670 int fuse_handle_backing(struct fuse_entry_bpf *feb, struct inode **backing_inode,
1671 			struct path *backing_path);
1672 int fuse_handle_bpf_prog(struct fuse_entry_bpf *feb, struct inode *parent,
1673 			 struct bpf_prog **bpf);
1674 
1675 int fuse_lookup_initialize(struct fuse_bpf_args *fa, struct fuse_lookup_io *feo,
1676 	       struct inode *dir, struct dentry *entry, unsigned int flags);
1677 int fuse_lookup_backing(struct fuse_bpf_args *fa, struct inode *dir,
1678 			  struct dentry *entry, unsigned int flags);
1679 struct dentry *fuse_lookup_finalize(struct fuse_bpf_args *fa, struct inode *dir,
1680 			   struct dentry *entry, unsigned int flags);
1681 int fuse_revalidate_backing(struct dentry *entry, unsigned int flags);
1682 
1683 int fuse_canonical_path_initialize(struct fuse_bpf_args *fa,
1684 				   struct fuse_dummy_io *fdi,
1685 				   const struct path *path,
1686 				   struct path *canonical_path);
1687 int fuse_canonical_path_backing(struct fuse_bpf_args *fa, const struct path *path,
1688 				struct path *canonical_path);
1689 void *fuse_canonical_path_finalize(struct fuse_bpf_args *fa,
1690 				   const struct path *path,
1691 				   struct path *canonical_path);
1692 
1693 struct fuse_getattr_io {
1694 	struct fuse_getattr_in fgi;
1695 	struct fuse_attr_out fao;
1696 };
1697 int fuse_getattr_initialize(struct fuse_bpf_args *fa, struct fuse_getattr_io *fgio,
1698 			const struct dentry *entry, struct kstat *stat,
1699 			u32 request_mask, unsigned int flags);
1700 int fuse_getattr_backing(struct fuse_bpf_args *fa,
1701 			const struct dentry *entry, struct kstat *stat,
1702 			u32 request_mask, unsigned int flags);
1703 void *fuse_getattr_finalize(struct fuse_bpf_args *fa,
1704 			const struct dentry *entry, struct kstat *stat,
1705 			u32 request_mask, unsigned int flags);
1706 
1707 struct fuse_setattr_io {
1708 	struct fuse_setattr_in fsi;
1709 	struct fuse_attr_out fao;
1710 };
1711 
1712 int fuse_setattr_initialize(struct fuse_bpf_args *fa, struct fuse_setattr_io *fsi,
1713 		struct dentry *dentry, struct iattr *attr, struct file *file);
1714 int fuse_setattr_backing(struct fuse_bpf_args *fa,
1715 		struct dentry *dentry, struct iattr *attr, struct file *file);
1716 void *fuse_setattr_finalize(struct fuse_bpf_args *fa,
1717 		struct dentry *dentry, struct iattr *attr, struct file *file);
1718 
1719 int fuse_statfs_initialize(struct fuse_bpf_args *fa, struct fuse_statfs_out *fso,
1720 		struct dentry *dentry, struct kstatfs *buf);
1721 int fuse_statfs_backing(struct fuse_bpf_args *fa,
1722 		struct dentry *dentry, struct kstatfs *buf);
1723 void *fuse_statfs_finalize(struct fuse_bpf_args *fa,
1724 		struct dentry *dentry, struct kstatfs *buf);
1725 
1726 int fuse_get_link_initialize(struct fuse_bpf_args *fa, struct fuse_dummy_io *dummy,
1727 		struct inode *inode, struct dentry *dentry,
1728 		struct delayed_call *callback, const char **out);
1729 int fuse_get_link_backing(struct fuse_bpf_args *fa,
1730 		struct inode *inode, struct dentry *dentry,
1731 		struct delayed_call *callback, const char **out);
1732 void *fuse_get_link_finalize(struct fuse_bpf_args *fa,
1733 		struct inode *inode, struct dentry *dentry,
1734 		struct delayed_call *callback, const char **out);
1735 
1736 int fuse_symlink_initialize(
1737 		struct fuse_bpf_args *fa, struct fuse_dummy_io *unused,
1738 		struct inode *dir, struct dentry *entry, const char *link, int len);
1739 int fuse_symlink_backing(
1740 		struct fuse_bpf_args *fa,
1741 		struct inode *dir, struct dentry *entry, const char *link, int len);
1742 void *fuse_symlink_finalize(
1743 		struct fuse_bpf_args *fa,
1744 		struct inode *dir, struct dentry *entry, const char *link, int len);
1745 
1746 struct fuse_read_io {
1747 	struct fuse_read_in fri;
1748 	struct fuse_read_out fro;
1749 };
1750 
1751 int fuse_readdir_initialize(struct fuse_bpf_args *fa, struct fuse_read_io *frio,
1752 			    struct file *file, struct dir_context *ctx,
1753 			    bool *force_again, bool *allow_force, bool is_continued);
1754 int fuse_readdir_backing(struct fuse_bpf_args *fa,
1755 			 struct file *file, struct dir_context *ctx,
1756 			 bool *force_again, bool *allow_force, bool is_continued);
1757 void *fuse_readdir_finalize(struct fuse_bpf_args *fa,
1758 			    struct file *file, struct dir_context *ctx,
1759 			    bool *force_again, bool *allow_force, bool is_continued);
1760 
1761 int fuse_access_initialize(struct fuse_bpf_args *fa, struct fuse_access_in *fai,
1762 			   struct inode *inode, int mask);
1763 int fuse_access_backing(struct fuse_bpf_args *fa, struct inode *inode, int mask);
1764 void *fuse_access_finalize(struct fuse_bpf_args *fa, struct inode *inode, int mask);
1765 
1766 /*
1767  * FUSE caches dentries and attributes with separate timeout.  The
1768  * time in jiffies until the dentry/attributes are valid is stored in
1769  * dentry->d_fsdata and fuse_inode->i_time respectively.
1770  */
1771 
1772 /*
1773  * Calculate the time in jiffies until a dentry/attributes are valid
1774  */
time_to_jiffies(u64 sec,u32 nsec)1775 static inline u64 time_to_jiffies(u64 sec, u32 nsec)
1776 {
1777 	if (sec || nsec) {
1778 		struct timespec64 ts = {
1779 			sec,
1780 			min_t(u32, nsec, NSEC_PER_SEC - 1)
1781 		};
1782 
1783 		return get_jiffies_64() + timespec64_to_jiffies(&ts);
1784 	} else
1785 		return 0;
1786 }
1787 
attr_timeout(struct fuse_attr_out * o)1788 static inline u64 attr_timeout(struct fuse_attr_out *o)
1789 {
1790 	return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
1791 }
1792 
update_mtime(unsigned int ivalid,bool trust_local_mtime)1793 static inline bool update_mtime(unsigned int ivalid, bool trust_local_mtime)
1794 {
1795 	/* Always update if mtime is explicitly set  */
1796 	if (ivalid & ATTR_MTIME_SET)
1797 		return true;
1798 
1799 	/* Or if kernel i_mtime is the official one */
1800 	if (trust_local_mtime)
1801 		return true;
1802 
1803 	/* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1804 	if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1805 		return false;
1806 
1807 	/* In all other cases update */
1808 	return true;
1809 }
1810 
1811 void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
1812 			  struct kstat *stat);
1813 
iattr_to_fattr(struct fuse_conn * fc,struct iattr * iattr,struct fuse_setattr_in * arg,bool trust_local_cmtime)1814 static inline void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr,
1815 			   struct fuse_setattr_in *arg, bool trust_local_cmtime)
1816 {
1817 	unsigned int ivalid = iattr->ia_valid;
1818 
1819 	if (ivalid & ATTR_MODE)
1820 		arg->valid |= FATTR_MODE,   arg->mode = iattr->ia_mode;
1821 	if (ivalid & ATTR_UID)
1822 		arg->valid |= FATTR_UID,    arg->uid = from_kuid(fc->user_ns, iattr->ia_uid);
1823 	if (ivalid & ATTR_GID)
1824 		arg->valid |= FATTR_GID,    arg->gid = from_kgid(fc->user_ns, iattr->ia_gid);
1825 	if (ivalid & ATTR_SIZE)
1826 		arg->valid |= FATTR_SIZE,   arg->size = iattr->ia_size;
1827 	if (ivalid & ATTR_ATIME) {
1828 		arg->valid |= FATTR_ATIME;
1829 		arg->atime = iattr->ia_atime.tv_sec;
1830 		arg->atimensec = iattr->ia_atime.tv_nsec;
1831 		if (!(ivalid & ATTR_ATIME_SET))
1832 			arg->valid |= FATTR_ATIME_NOW;
1833 	}
1834 	if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1835 		arg->valid |= FATTR_MTIME;
1836 		arg->mtime = iattr->ia_mtime.tv_sec;
1837 		arg->mtimensec = iattr->ia_mtime.tv_nsec;
1838 		if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1839 			arg->valid |= FATTR_MTIME_NOW;
1840 	}
1841 	if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1842 		arg->valid |= FATTR_CTIME;
1843 		arg->ctime = iattr->ia_ctime.tv_sec;
1844 		arg->ctimensec = iattr->ia_ctime.tv_nsec;
1845 	}
1846 }
1847 
finalize_attr(struct inode * inode,struct fuse_attr_out * outarg,u64 attr_version,struct kstat * stat)1848 static inline int finalize_attr(struct inode *inode, struct fuse_attr_out *outarg,
1849 				u64 attr_version, struct kstat *stat)
1850 {
1851 	int err = 0;
1852 
1853 	if (fuse_invalid_attr(&outarg->attr) ||
1854 	    ((inode->i_mode ^ outarg->attr.mode) & S_IFMT)) {
1855 		fuse_make_bad(inode);
1856 		err = -EIO;
1857 	} else {
1858 		fuse_change_attributes(inode, &outarg->attr,
1859 				       attr_timeout(outarg),
1860 				       attr_version);
1861 		if (stat)
1862 			fuse_fillattr(inode, &outarg->attr, stat);
1863 	}
1864 	return err;
1865 }
1866 
convert_statfs_to_fuse(struct fuse_kstatfs * attr,struct kstatfs * stbuf)1867 static inline void convert_statfs_to_fuse(struct fuse_kstatfs *attr, struct kstatfs *stbuf)
1868 {
1869 	attr->bsize   = stbuf->f_bsize;
1870 	attr->frsize  = stbuf->f_frsize;
1871 	attr->blocks  = stbuf->f_blocks;
1872 	attr->bfree   = stbuf->f_bfree;
1873 	attr->bavail  = stbuf->f_bavail;
1874 	attr->files   = stbuf->f_files;
1875 	attr->ffree   = stbuf->f_ffree;
1876 	attr->namelen = stbuf->f_namelen;
1877 	/* fsid is left zero */
1878 }
1879 
convert_fuse_statfs(struct kstatfs * stbuf,struct fuse_kstatfs * attr)1880 static inline void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
1881 {
1882 	stbuf->f_type    = FUSE_SUPER_MAGIC;
1883 	stbuf->f_bsize   = attr->bsize;
1884 	stbuf->f_frsize  = attr->frsize;
1885 	stbuf->f_blocks  = attr->blocks;
1886 	stbuf->f_bfree   = attr->bfree;
1887 	stbuf->f_bavail  = attr->bavail;
1888 	stbuf->f_files   = attr->files;
1889 	stbuf->f_ffree   = attr->ffree;
1890 	stbuf->f_namelen = attr->namelen;
1891 	/* fsid is left zero */
1892 }
1893 
1894 #ifdef CONFIG_FUSE_BPF
1895 struct fuse_err_ret {
1896 	void *result;
1897 	bool ret;
1898 };
1899 
1900 int __init fuse_bpf_init(void);
1901 void __exit fuse_bpf_cleanup(void);
1902 
1903 ssize_t fuse_bpf_simple_request(struct fuse_mount *fm, struct fuse_bpf_args *args);
1904 
fuse_bpf_run(struct bpf_prog * prog,struct fuse_bpf_args * fba)1905 static inline int fuse_bpf_run(struct bpf_prog *prog, struct fuse_bpf_args *fba)
1906 {
1907 	int ret;
1908 
1909 	migrate_disable();
1910 	ret = bpf_prog_run(prog, fba);
1911 	migrate_enable();
1912 	return ret;
1913 }
1914 
1915 /*
1916  * expression statement to wrap the backing filter logic
1917  * struct inode *inode: inode with bpf and backing inode
1918  * typedef io: (typically complex) type whose components fuse_args can point to.
1919  *	An instance of this type is created locally and passed to initialize
1920  * void initialize(struct fuse_bpf_args *fa, io *in_out, args...): function that sets
1921  *	up fa and io based on args
1922  * int backing(struct fuse_bpf_args *fa, args...): function that actually performs
1923  *	the backing io operation
1924  * void *finalize(struct fuse_bpf_args *, args...): function that performs any final
1925  *	work needed to commit the backing io
1926  */
1927 #define fuse_bpf_backing(inode, io, initialize, backing, finalize,	\
1928 			 args...)					\
1929 ({									\
1930 	struct fuse_err_ret fer = {0};					\
1931 	int ext_flags;							\
1932 	struct fuse_inode *fuse_inode = get_fuse_inode(inode);		\
1933 	struct fuse_mount *fm = get_fuse_mount(inode);			\
1934 	io feo = {0};							\
1935 	struct fuse_bpf_args fa = {0}, fa_backup = {0};			\
1936 	bool locked;							\
1937 	ssize_t res;							\
1938 	void *err;							\
1939 	int i;								\
1940 	bool initialized = false;					\
1941 									\
1942 	do {								\
1943 		if (!fuse_inode || !fuse_inode->backing_inode)		\
1944 			break;						\
1945 									\
1946 		err = ERR_PTR(initialize(&fa, &feo, args));		\
1947 		if (err) {						\
1948 			fer = (struct fuse_err_ret) {			\
1949 				err,					\
1950 				true,					\
1951 			};						\
1952 			break;						\
1953 		}							\
1954 		initialized = true;					\
1955 									\
1956 		fa_backup = fa;						\
1957 		fa.opcode |= FUSE_PREFILTER;				\
1958 		for (i = 0; i < fa.in_numargs; ++i)			\
1959 			fa.out_args[i] = (struct fuse_bpf_arg) {	\
1960 				.size = fa.in_args[i].size,		\
1961 				.value = (void *)fa.in_args[i].value,	\
1962 			};						\
1963 		fa.out_numargs = fa.in_numargs;				\
1964 									\
1965 		ext_flags = fuse_inode->bpf ?				\
1966 			fuse_bpf_run(fuse_inode->bpf, &fa) :		\
1967 			FUSE_BPF_BACKING;				\
1968 		if (ext_flags < 0) {					\
1969 			fer = (struct fuse_err_ret) {			\
1970 				ERR_PTR(ext_flags),			\
1971 				true,					\
1972 			};						\
1973 			break;						\
1974 		}							\
1975 									\
1976 		if (ext_flags & FUSE_BPF_USER_FILTER) {			\
1977 			locked = fuse_lock_inode(inode);		\
1978 			res = fuse_bpf_simple_request(fm, &fa);		\
1979 			fuse_unlock_inode(inode, locked);		\
1980 			if (res < 0) {					\
1981 				fer = (struct fuse_err_ret) {		\
1982 					ERR_PTR(res),			\
1983 					true,				\
1984 				};					\
1985 				break;					\
1986 			}						\
1987 		}							\
1988 									\
1989 		if (!(ext_flags & FUSE_BPF_BACKING))			\
1990 			break;						\
1991 									\
1992 		fa.opcode &= ~FUSE_PREFILTER;				\
1993 		for (i = 0; i < fa.in_numargs; ++i)			\
1994 			fa.in_args[i] = (struct fuse_bpf_in_arg) {	\
1995 				.size = fa.out_args[i].size,		\
1996 				.value = fa.out_args[i].value,		\
1997 			};						\
1998 		for (i = 0; i < fa_backup.out_numargs; ++i)		\
1999 			fa.out_args[i] = (struct fuse_bpf_arg) {	\
2000 				.size = fa_backup.out_args[i].size,	\
2001 				.value = fa_backup.out_args[i].value,	\
2002 			};						\
2003 		fa.out_numargs = fa_backup.out_numargs;			\
2004 									\
2005 		fer = (struct fuse_err_ret) {				\
2006 			ERR_PTR(backing(&fa, args)),			\
2007 			true,						\
2008 		};							\
2009 		if (IS_ERR(fer.result))					\
2010 			fa.error_in = PTR_ERR(fer.result);		\
2011 		if (!(ext_flags & FUSE_BPF_POST_FILTER))		\
2012 			break;						\
2013 									\
2014 		fa.opcode |= FUSE_POSTFILTER;				\
2015 		for (i = 0; i < fa.out_numargs; ++i)			\
2016 			fa.in_args[fa.in_numargs++] =			\
2017 				(struct fuse_bpf_in_arg) {		\
2018 					.size = fa.out_args[i].size,	\
2019 					.value = fa.out_args[i].value,	\
2020 				};					\
2021 		ext_flags = fuse_bpf_run(fuse_inode->bpf, &fa);		\
2022 		if (ext_flags < 0) {					\
2023 			fer = (struct fuse_err_ret) {			\
2024 				ERR_PTR(ext_flags),			\
2025 				true,					\
2026 			};						\
2027 			break;						\
2028 		}							\
2029 		if (!(ext_flags & FUSE_BPF_USER_FILTER))		\
2030 			break;						\
2031 									\
2032 		fa.out_args[0].size = fa_backup.out_args[0].size;	\
2033 		fa.out_args[1].size = fa_backup.out_args[1].size;	\
2034 		fa.out_numargs = fa_backup.out_numargs;			\
2035 		locked = fuse_lock_inode(inode);			\
2036 		res = fuse_bpf_simple_request(fm, &fa);			\
2037 		fuse_unlock_inode(inode, locked);			\
2038 		if (res < 0) {						\
2039 			fer.result = ERR_PTR(res);			\
2040 			break;						\
2041 		}							\
2042 	} while (false);						\
2043 									\
2044 	if (initialized && fer.ret) {					\
2045 		err = finalize(&fa, args);				\
2046 		if (err)						\
2047 			fer.result = err;				\
2048 	}								\
2049 									\
2050 	fer;								\
2051 })
2052 
2053 #endif /* CONFIG_FUSE_BPF */
2054 
2055 #endif /* _FS_FUSE_I_H */
2056