• 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 enum fuse_dax_mode {
582 	FUSE_DAX_INODE_DEFAULT,	/* default */
583 	FUSE_DAX_ALWAYS,	/* "-o dax=always" */
584 	FUSE_DAX_NEVER,		/* "-o dax=never" */
585 	FUSE_DAX_INODE_USER,	/* "-o dax=inode" */
586 };
587 
fuse_is_inode_dax_mode(enum fuse_dax_mode mode)588 static inline bool fuse_is_inode_dax_mode(enum fuse_dax_mode mode)
589 {
590 	return mode == FUSE_DAX_INODE_DEFAULT || mode == FUSE_DAX_INODE_USER;
591 }
592 
593 struct fuse_fs_context {
594 	int fd;
595 	struct file *file;
596 	unsigned int rootmode;
597 	kuid_t user_id;
598 	kgid_t group_id;
599 	bool is_bdev:1;
600 	bool fd_present:1;
601 	bool rootmode_present:1;
602 	bool user_id_present:1;
603 	bool group_id_present:1;
604 	bool default_permissions:1;
605 	bool allow_other:1;
606 	bool destroy:1;
607 	bool no_control:1;
608 	bool no_force_umount:1;
609 	bool legacy_opts_show:1;
610 	enum fuse_dax_mode dax_mode;
611 	bool no_daemon:1;
612 	unsigned int max_read;
613 	unsigned int blksize;
614 	const char *subtype;
615 	struct bpf_prog *root_bpf;
616 	struct file *root_dir;
617 
618 	/* DAX device, may be NULL */
619 	struct dax_device *dax_dev;
620 
621 	/* fuse_dev pointer to fill in, should contain NULL on entry */
622 	void **fudptr;
623 };
624 
625 struct fuse_sync_bucket {
626 	/* count is a possible scalability bottleneck */
627 	atomic_t count;
628 	wait_queue_head_t waitq;
629 	struct rcu_head rcu;
630 };
631 
632 /**
633  * A Fuse connection.
634  *
635  * This structure is created, when the root filesystem is mounted, and
636  * is destroyed, when the client device is closed and the last
637  * fuse_mount is destroyed.
638  */
639 struct fuse_conn {
640 	/** Lock protecting accessess to  members of this structure */
641 	spinlock_t lock;
642 
643 	/** Refcount */
644 	refcount_t count;
645 
646 	/** Number of fuse_dev's */
647 	atomic_t dev_count;
648 
649 	struct rcu_head rcu;
650 
651 	/** The user id for this mount */
652 	kuid_t user_id;
653 
654 	/** The group id for this mount */
655 	kgid_t group_id;
656 
657 	/** The pid namespace for this mount */
658 	struct pid_namespace *pid_ns;
659 
660 	/** The user namespace for this mount */
661 	struct user_namespace *user_ns;
662 
663 	/** Maximum read size */
664 	unsigned max_read;
665 
666 	/** Maximum write size */
667 	unsigned max_write;
668 
669 	/** Maximum number of pages that can be used in a single request */
670 	unsigned int max_pages;
671 
672 	/** Constrain ->max_pages to this value during feature negotiation */
673 	unsigned int max_pages_limit;
674 
675 	/** Input queue */
676 	struct fuse_iqueue iq;
677 
678 	/** The next unique kernel file handle */
679 	atomic64_t khctr;
680 
681 	/** rbtree of fuse_files waiting for poll events indexed by ph */
682 	struct rb_root polled_files;
683 
684 	/** Maximum number of outstanding background requests */
685 	unsigned max_background;
686 
687 	/** Number of background requests at which congestion starts */
688 	unsigned congestion_threshold;
689 
690 	/** Number of requests currently in the background */
691 	unsigned num_background;
692 
693 	/** Number of background requests currently queued for userspace */
694 	unsigned active_background;
695 
696 	/** The list of background requests set aside for later queuing */
697 	struct list_head bg_queue;
698 
699 	/** Protects: max_background, congestion_threshold, num_background,
700 	 * active_background, bg_queue, blocked */
701 	spinlock_t bg_lock;
702 
703 	/** Flag indicating that INIT reply has been received. Allocating
704 	 * any fuse request will be suspended until the flag is set */
705 	int initialized;
706 
707 	/** Flag indicating if connection is blocked.  This will be
708 	    the case before the INIT reply is received, and if there
709 	    are too many outstading backgrounds requests */
710 	int blocked;
711 
712 	/** waitq for blocked connection */
713 	wait_queue_head_t blocked_waitq;
714 
715 	/** Connection established, cleared on umount, connection
716 	    abort and device release */
717 	unsigned connected;
718 
719 	/** Connection aborted via sysfs */
720 	bool aborted;
721 
722 	/** Connection failed (version mismatch).  Cannot race with
723 	    setting other bitfields since it is only set once in INIT
724 	    reply, before any other request, and never cleared */
725 	unsigned conn_error:1;
726 
727 	/** Connection successful.  Only set in INIT */
728 	unsigned conn_init:1;
729 
730 	/** Do readahead asynchronously?  Only set in INIT */
731 	unsigned async_read:1;
732 
733 	/** Return an unique read error after abort.  Only set in INIT */
734 	unsigned abort_err:1;
735 
736 	/** Do not send separate SETATTR request before open(O_TRUNC)  */
737 	unsigned atomic_o_trunc:1;
738 
739 	/** Filesystem supports NFS exporting.  Only set in INIT */
740 	unsigned export_support:1;
741 
742 	/** write-back cache policy (default is write-through) */
743 	unsigned writeback_cache:1;
744 
745 	/** allow parallel lookups and readdir (default is serialized) */
746 	unsigned parallel_dirops:1;
747 
748 	/** handle fs handles killing suid/sgid/cap on write/chown/trunc */
749 	unsigned handle_killpriv:1;
750 
751 	/** cache READLINK responses in page cache */
752 	unsigned cache_symlinks:1;
753 
754 	/* show legacy mount options */
755 	unsigned int legacy_opts_show:1;
756 
757 	/*
758 	 * fs kills suid/sgid/cap on write/chown/trunc. suid is killed on
759 	 * write/trunc only if caller did not have CAP_FSETID.  sgid is killed
760 	 * on write/truncate only if caller did not have CAP_FSETID as well as
761 	 * file has group execute permission.
762 	 */
763 	unsigned handle_killpriv_v2:1;
764 
765 	/*
766 	 * The following bitfields are only for optimization purposes
767 	 * and hence races in setting them will not cause malfunction
768 	 */
769 
770 	/** Is open/release not implemented by fs? */
771 	unsigned no_open:1;
772 
773 	/** Is opendir/releasedir not implemented by fs? */
774 	unsigned no_opendir:1;
775 
776 	/** Is fsync not implemented by fs? */
777 	unsigned no_fsync:1;
778 
779 	/** Is fsyncdir not implemented by fs? */
780 	unsigned no_fsyncdir:1;
781 
782 	/** Is flush not implemented by fs? */
783 	unsigned no_flush:1;
784 
785 	/** Is setxattr not implemented by fs? */
786 	unsigned no_setxattr:1;
787 
788 	/** Does file server support extended setxattr */
789 	unsigned setxattr_ext:1;
790 
791 	/** Is getxattr not implemented by fs? */
792 	unsigned no_getxattr:1;
793 
794 	/** Is listxattr not implemented by fs? */
795 	unsigned no_listxattr:1;
796 
797 	/** Is removexattr not implemented by fs? */
798 	unsigned no_removexattr:1;
799 
800 	/** Are posix file locking primitives not implemented by fs? */
801 	unsigned no_lock:1;
802 
803 	/** Is access not implemented by fs? */
804 	unsigned no_access:1;
805 
806 	/** Is create not implemented by fs? */
807 	unsigned no_create:1;
808 
809 	/** Is interrupt not implemented by fs? */
810 	unsigned no_interrupt:1;
811 
812 	/** Is bmap not implemented by fs? */
813 	unsigned no_bmap:1;
814 
815 	/** Is dentry_canonical_path not implemented by fs? */
816 	unsigned no_dentry_canonical_path:1;
817 
818 	/** Is poll not implemented by fs? */
819 	unsigned no_poll:1;
820 
821 	/** Do multi-page cached writes */
822 	unsigned big_writes:1;
823 
824 	/** Don't apply umask to creation modes */
825 	unsigned dont_mask:1;
826 
827 	/** Are BSD file locking primitives not implemented by fs? */
828 	unsigned no_flock:1;
829 
830 	/** Is fallocate not implemented by fs? */
831 	unsigned no_fallocate:1;
832 
833 	/** Is rename with flags implemented by fs? */
834 	unsigned no_rename2:1;
835 
836 	/** Use enhanced/automatic page cache invalidation. */
837 	unsigned auto_inval_data:1;
838 
839 	/** Filesystem is fully responsible for page cache invalidation. */
840 	unsigned explicit_inval_data:1;
841 
842 	/** Does the filesystem support readdirplus? */
843 	unsigned do_readdirplus:1;
844 
845 	/** Does the filesystem want adaptive readdirplus? */
846 	unsigned readdirplus_auto:1;
847 
848 	/** Does the filesystem support asynchronous direct-IO submission? */
849 	unsigned async_dio:1;
850 
851 	/** Is lseek not implemented by fs? */
852 	unsigned no_lseek:1;
853 
854 	/** Does the filesystem support posix acls? */
855 	unsigned posix_acl:1;
856 
857 	/** Check permissions based on the file mode or not? */
858 	unsigned default_permissions:1;
859 
860 	/** Allow other than the mounter user to access the filesystem ? */
861 	unsigned allow_other:1;
862 
863 	/** Does the filesystem support copy_file_range? */
864 	unsigned no_copy_file_range:1;
865 
866 	/* Send DESTROY request */
867 	unsigned int destroy:1;
868 
869 	/* Delete dentries that have gone stale */
870 	unsigned int delete_stale:1;
871 
872 	/** Do not create entry in fusectl fs */
873 	unsigned int no_control:1;
874 
875 	/** Do not allow MNT_FORCE umount */
876 	unsigned int no_force_umount:1;
877 
878 	/* Auto-mount submounts announced by the server */
879 	unsigned int auto_submounts:1;
880 
881 	/** Passthrough mode for read/write IO */
882 	unsigned int passthrough:1;
883 
884 	/* Propagate syncfs() to server */
885 	unsigned int sync_fs:1;
886 
887 	/* Initialize security xattrs when creating a new inode */
888 	unsigned int init_security:1;
889 
890 	/* Does the filesystem support per inode DAX? */
891 	unsigned int inode_dax:1;
892 
893 	/* Is tmpfile not implemented by fs? */
894 	unsigned int no_tmpfile:1;
895 
896 	/** BPF Only, no Daemon running */
897 	unsigned int no_daemon:1;
898 
899 	/** The number of requests waiting for completion */
900 	atomic_t num_waiting;
901 
902 	/** Negotiated minor version */
903 	unsigned minor;
904 
905 	/** Entry on the fuse_mount_list */
906 	struct list_head entry;
907 
908 	/** Device ID from the root super block */
909 	dev_t dev;
910 
911 	/** Dentries in the control filesystem */
912 	struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES];
913 
914 	/** number of dentries used in the above array */
915 	int ctl_ndents;
916 
917 	/** Key for lock owner ID scrambling */
918 	u32 scramble_key[4];
919 
920 	/** Version counter for attribute changes */
921 	atomic64_t attr_version;
922 
923 	/** Called on final put */
924 	void (*release)(struct fuse_conn *);
925 
926 	/**
927 	 * Read/write semaphore to hold when accessing the sb of any
928 	 * fuse_mount belonging to this connection
929 	 */
930 	struct rw_semaphore killsb;
931 
932 	/** List of device instances belonging to this connection */
933 	struct list_head devices;
934 
935 #ifdef CONFIG_FUSE_DAX
936 	/* Dax mode */
937 	enum fuse_dax_mode dax_mode;
938 
939 	/* Dax specific conn data, non-NULL if DAX is enabled */
940 	struct fuse_conn_dax *dax;
941 #endif
942 
943 	/** List of filesystems using this connection */
944 	struct list_head mounts;
945 
946 	/* New writepages go into this bucket */
947 	struct fuse_sync_bucket __rcu *curr_bucket;
948 
949 	/** IDR for passthrough requests */
950 	struct idr passthrough_req;
951 
952 	/** Protects passthrough_req */
953 	spinlock_t passthrough_req_lock;
954 };
955 
956 /*
957  * Represents a mounted filesystem, potentially a submount.
958  *
959  * This object allows sharing a fuse_conn between separate mounts to
960  * allow submounts with dedicated superblocks and thus separate device
961  * IDs.
962  */
963 struct fuse_mount {
964 	/* Underlying (potentially shared) connection to the FUSE server */
965 	struct fuse_conn *fc;
966 
967 	/*
968 	 * Super block for this connection (fc->killsb must be held when
969 	 * accessing this).
970 	 */
971 	struct super_block *sb;
972 
973 	/* Entry on fc->mounts */
974 	struct list_head fc_entry;
975 };
976 
get_fuse_mount_super(struct super_block * sb)977 static inline struct fuse_mount *get_fuse_mount_super(struct super_block *sb)
978 {
979 	return sb->s_fs_info;
980 }
981 
get_fuse_conn_super(struct super_block * sb)982 static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
983 {
984 	return get_fuse_mount_super(sb)->fc;
985 }
986 
get_fuse_mount(struct inode * inode)987 static inline struct fuse_mount *get_fuse_mount(struct inode *inode)
988 {
989 	return get_fuse_mount_super(inode->i_sb);
990 }
991 
get_fuse_conn(struct inode * inode)992 static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
993 {
994 	return get_fuse_mount_super(inode->i_sb)->fc;
995 }
996 
get_fuse_inode(struct inode * inode)997 static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
998 {
999 	return container_of(inode, struct fuse_inode, inode);
1000 }
1001 
get_node_id(struct inode * inode)1002 static inline u64 get_node_id(struct inode *inode)
1003 {
1004 	return get_fuse_inode(inode)->nodeid;
1005 }
1006 
invalid_nodeid(u64 nodeid)1007 static inline int invalid_nodeid(u64 nodeid)
1008 {
1009 	return !nodeid || nodeid == FUSE_ROOT_ID;
1010 }
1011 
fuse_get_attr_version(struct fuse_conn * fc)1012 static inline u64 fuse_get_attr_version(struct fuse_conn *fc)
1013 {
1014 	return atomic64_read(&fc->attr_version);
1015 }
1016 
fuse_stale_inode(const struct inode * inode,int generation,struct fuse_attr * attr)1017 static inline bool fuse_stale_inode(const struct inode *inode, int generation,
1018 				    struct fuse_attr *attr)
1019 {
1020 	return inode->i_generation != generation ||
1021 		inode_wrong_type(inode, attr->mode);
1022 }
1023 
fuse_make_bad(struct inode * inode)1024 static inline void fuse_make_bad(struct inode *inode)
1025 {
1026 	remove_inode_hash(inode);
1027 	set_bit(FUSE_I_BAD, &get_fuse_inode(inode)->state);
1028 }
1029 
fuse_is_bad(struct inode * inode)1030 static inline bool fuse_is_bad(struct inode *inode)
1031 {
1032 	return unlikely(test_bit(FUSE_I_BAD, &get_fuse_inode(inode)->state));
1033 }
1034 
fuse_pages_alloc(unsigned int npages,gfp_t flags,struct fuse_page_desc ** desc)1035 static inline struct page **fuse_pages_alloc(unsigned int npages, gfp_t flags,
1036 					     struct fuse_page_desc **desc)
1037 {
1038 	struct page **pages;
1039 
1040 	pages = kzalloc(npages * (sizeof(struct page *) +
1041 				  sizeof(struct fuse_page_desc)), flags);
1042 	*desc = (void *) (pages + npages);
1043 
1044 	return pages;
1045 }
1046 
fuse_page_descs_length_init(struct fuse_page_desc * descs,unsigned int index,unsigned int nr_pages)1047 static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs,
1048 					       unsigned int index,
1049 					       unsigned int nr_pages)
1050 {
1051 	int i;
1052 
1053 	for (i = index; i < index + nr_pages; i++)
1054 		descs[i].length = PAGE_SIZE - descs[i].offset;
1055 }
1056 
fuse_sync_bucket_dec(struct fuse_sync_bucket * bucket)1057 static inline void fuse_sync_bucket_dec(struct fuse_sync_bucket *bucket)
1058 {
1059 	/* Need RCU protection to prevent use after free after the decrement */
1060 	rcu_read_lock();
1061 	if (atomic_dec_and_test(&bucket->count))
1062 		wake_up(&bucket->waitq);
1063 	rcu_read_unlock();
1064 }
1065 
1066 /** Device operations */
1067 extern const struct file_operations fuse_dev_operations;
1068 
1069 extern const struct dentry_operations fuse_dentry_operations;
1070 extern const struct dentry_operations fuse_root_dentry_operations;
1071 
1072 /**
1073  * Get a filled-in inode
1074  */
1075 struct inode *fuse_iget_backing(struct super_block *sb,
1076 				u64 nodeid,
1077 				struct inode *backing_inode);
1078 struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
1079 			int generation, struct fuse_attr *attr,
1080 			u64 attr_valid, u64 attr_version);
1081 
1082 int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name,
1083 		     struct fuse_entry_out *outarg,
1084 		     struct dentry *entry, struct inode **inode);
1085 
1086 /**
1087  * Send FORGET command
1088  */
1089 void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
1090 		       u64 nodeid, u64 nlookup);
1091 
1092 struct fuse_forget_link *fuse_alloc_forget(void);
1093 
1094 struct fuse_forget_link *fuse_dequeue_forget(struct fuse_iqueue *fiq,
1095 					     unsigned int max,
1096 					     unsigned int *countp);
1097 
1098 /*
1099  * Initialize READ or READDIR request
1100  */
1101 struct fuse_io_args {
1102 	union {
1103 		struct {
1104 			struct fuse_read_in in;
1105 			u64 attr_ver;
1106 		} read;
1107 		struct {
1108 			struct fuse_write_in in;
1109 			struct fuse_write_out out;
1110 			bool page_locked;
1111 		} write;
1112 	};
1113 	struct fuse_args_pages ap;
1114 	struct fuse_io_priv *io;
1115 	struct fuse_file *ff;
1116 };
1117 
1118 void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
1119 			 size_t count, int opcode);
1120 
1121 /**
1122  * Send OPEN or OPENDIR request
1123  */
1124 int fuse_open_common(struct inode *inode, struct file *file, bool isdir);
1125 
1126 struct fuse_file *fuse_file_alloc(struct fuse_mount *fm);
1127 void fuse_file_free(struct fuse_file *ff);
1128 void fuse_finish_open(struct inode *inode, struct file *file);
1129 
1130 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff,
1131 		       unsigned int flags);
1132 
1133 /**
1134  * Send RELEASE or RELEASEDIR request
1135  */
1136 void fuse_release_common(struct file *file, bool isdir);
1137 
1138 /**
1139  * Send FSYNC or FSYNCDIR request
1140  */
1141 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
1142 		      int datasync, int opcode);
1143 
1144 /**
1145  * Notify poll wakeup
1146  */
1147 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
1148 			    struct fuse_notify_poll_wakeup_out *outarg);
1149 
1150 /**
1151  * Initialize file operations on a regular file
1152  */
1153 void fuse_init_file_inode(struct inode *inode, unsigned int flags);
1154 
1155 /**
1156  * Initialize inode operations on regular files and special files
1157  */
1158 void fuse_init_common(struct inode *inode);
1159 
1160 /**
1161  * Initialize inode and file operations on a directory
1162  */
1163 void fuse_init_dir(struct inode *inode);
1164 
1165 /**
1166  * Initialize inode operations on a symlink
1167  */
1168 void fuse_init_symlink(struct inode *inode);
1169 
1170 /**
1171  * Change attributes of an inode
1172  */
1173 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
1174 			    u64 attr_valid, u64 attr_version);
1175 
1176 void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
1177 				   u64 attr_valid, u32 cache_mask);
1178 
1179 u32 fuse_get_cache_mask(struct inode *inode);
1180 
1181 /**
1182  * Initialize the client device
1183  */
1184 int fuse_dev_init(void);
1185 
1186 /**
1187  * Cleanup the client device
1188  */
1189 void fuse_dev_cleanup(void);
1190 
1191 int fuse_ctl_init(void);
1192 void fuse_ctl_cleanup(void);
1193 
1194 /**
1195  * Simple request sending that does request allocation and freeing
1196  */
1197 ssize_t fuse_simple_request(struct fuse_mount *fm, struct fuse_args *args);
1198 int fuse_simple_background(struct fuse_mount *fm, struct fuse_args *args,
1199 			   gfp_t gfp_flags);
1200 
1201 /**
1202  * End a finished request
1203  */
1204 void fuse_request_end(struct fuse_req *req);
1205 
1206 /* Abort all requests */
1207 void fuse_abort_conn(struct fuse_conn *fc);
1208 void fuse_wait_aborted(struct fuse_conn *fc);
1209 
1210 /**
1211  * Invalidate inode attributes
1212  */
1213 
1214 /* Attributes possibly changed on data modification */
1215 #define FUSE_STATX_MODIFY	(STATX_MTIME | STATX_CTIME | STATX_BLOCKS)
1216 
1217 /* Attributes possibly changed on data and/or size modification */
1218 #define FUSE_STATX_MODSIZE	(FUSE_STATX_MODIFY | STATX_SIZE)
1219 
1220 void fuse_invalidate_attr(struct inode *inode);
1221 void fuse_invalidate_attr_mask(struct inode *inode, u32 mask);
1222 
1223 void fuse_invalidate_entry_cache(struct dentry *entry);
1224 
1225 void fuse_invalidate_atime(struct inode *inode);
1226 
1227 u64 entry_attr_timeout(struct fuse_entry_out *o);
1228 void fuse_init_dentry_root(struct dentry *root, struct file *backing_dir);
1229 void fuse_change_entry_timeout(struct dentry *entry, struct fuse_entry_out *o);
1230 
1231 /**
1232  * Acquire reference to fuse_conn
1233  */
1234 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
1235 
1236 /**
1237  * Initialize fuse_conn
1238  */
1239 void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm,
1240 		    struct user_namespace *user_ns,
1241 		    const struct fuse_iqueue_ops *fiq_ops, void *fiq_priv);
1242 
1243 /**
1244  * Release reference to fuse_conn
1245  */
1246 void fuse_conn_put(struct fuse_conn *fc);
1247 
1248 struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc);
1249 struct fuse_dev *fuse_dev_alloc(void);
1250 void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc);
1251 void fuse_dev_free(struct fuse_dev *fud);
1252 void fuse_send_init(struct fuse_mount *fm);
1253 
1254 /**
1255  * Fill in superblock and initialize fuse connection
1256  * @sb: partially-initialized superblock to fill in
1257  * @ctx: mount context
1258  */
1259 int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx);
1260 
1261 /*
1262  * Remove the mount from the connection
1263  *
1264  * Returns whether this was the last mount
1265  */
1266 bool fuse_mount_remove(struct fuse_mount *fm);
1267 
1268 /*
1269  * Setup context ops for submounts
1270  */
1271 int fuse_init_fs_context_submount(struct fs_context *fsc);
1272 
1273 /*
1274  * Shut down the connection (possibly sending DESTROY request).
1275  */
1276 void fuse_conn_destroy(struct fuse_mount *fm);
1277 
1278 /* Drop the connection and free the fuse mount */
1279 void fuse_mount_destroy(struct fuse_mount *fm);
1280 
1281 /**
1282  * Add connection to control filesystem
1283  */
1284 int fuse_ctl_add_conn(struct fuse_conn *fc);
1285 
1286 /**
1287  * Remove connection from control filesystem
1288  */
1289 void fuse_ctl_remove_conn(struct fuse_conn *fc);
1290 
1291 /**
1292  * Is file type valid?
1293  */
1294 int fuse_valid_type(int m);
1295 
1296 bool fuse_invalid_attr(struct fuse_attr *attr);
1297 
1298 /**
1299  * Is current process allowed to perform filesystem operation?
1300  */
1301 int fuse_allow_current_process(struct fuse_conn *fc);
1302 
1303 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id);
1304 
1305 void fuse_flush_time_update(struct inode *inode);
1306 void fuse_update_ctime(struct inode *inode);
1307 
1308 int fuse_update_attributes(struct inode *inode, struct file *file, u32 mask);
1309 
1310 void fuse_flush_writepages(struct inode *inode);
1311 
1312 void fuse_set_nowrite(struct inode *inode);
1313 void fuse_release_nowrite(struct inode *inode);
1314 
1315 /**
1316  * Scan all fuse_mounts belonging to fc to find the first where
1317  * ilookup5() returns a result.  Return that result and the
1318  * respective fuse_mount in *fm (unless fm is NULL).
1319  *
1320  * The caller must hold fc->killsb.
1321  */
1322 struct inode *fuse_ilookup(struct fuse_conn *fc, u64 nodeid,
1323 			   struct fuse_mount **fm);
1324 
1325 /**
1326  * File-system tells the kernel to invalidate cache for the given node id.
1327  */
1328 int fuse_reverse_inval_inode(struct fuse_conn *fc, u64 nodeid,
1329 			     loff_t offset, loff_t len);
1330 
1331 /**
1332  * File-system tells the kernel to invalidate parent attributes and
1333  * the dentry matching parent/name.
1334  *
1335  * If the child_nodeid is non-zero and:
1336  *    - matches the inode number for the dentry matching parent/name,
1337  *    - is not a mount point
1338  *    - is a file or oan empty directory
1339  * then the dentry is unhashed (d_delete()).
1340  */
1341 int fuse_reverse_inval_entry(struct fuse_conn *fc, u64 parent_nodeid,
1342 			     u64 child_nodeid, struct qstr *name);
1343 
1344 int fuse_do_open(struct fuse_mount *fm, u64 nodeid, struct file *file,
1345 		 bool isdir);
1346 
1347 /**
1348  * fuse_direct_io() flags
1349  */
1350 
1351 /** If set, it is WRITE; otherwise - READ */
1352 #define FUSE_DIO_WRITE (1 << 0)
1353 
1354 /** CUSE pass fuse_direct_io() a file which f_mapping->host is not from FUSE */
1355 #define FUSE_DIO_CUSE  (1 << 1)
1356 
1357 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1358 		       loff_t *ppos, int flags);
1359 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
1360 		   unsigned int flags);
1361 long fuse_ioctl_common(struct file *file, unsigned int cmd,
1362 		       unsigned long arg, unsigned int flags);
1363 __poll_t fuse_file_poll(struct file *file, poll_table *wait);
1364 int fuse_dev_release(struct inode *inode, struct file *file);
1365 
1366 bool fuse_write_update_attr(struct inode *inode, loff_t pos, ssize_t written);
1367 
1368 int fuse_flush_times(struct inode *inode, struct fuse_file *ff);
1369 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc);
1370 
1371 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
1372 		    struct file *file);
1373 
1374 void fuse_set_initialized(struct fuse_conn *fc);
1375 
1376 void fuse_unlock_inode(struct inode *inode, bool locked);
1377 bool fuse_lock_inode(struct inode *inode);
1378 
1379 int fuse_setxattr(struct inode *inode, const char *name, const void *value,
1380 		  size_t size, int flags, unsigned int extra_flags);
1381 ssize_t fuse_getxattr(struct inode *inode, const char *name, void *value,
1382 		      size_t size);
1383 ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size);
1384 int fuse_removexattr(struct inode *inode, const char *name);
1385 extern const struct xattr_handler *fuse_xattr_handlers[];
1386 extern const struct xattr_handler *fuse_acl_xattr_handlers[];
1387 extern const struct xattr_handler *fuse_no_acl_xattr_handlers[];
1388 
1389 struct posix_acl;
1390 struct posix_acl *fuse_get_acl(struct inode *inode, int type, bool rcu);
1391 int fuse_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
1392 		 struct posix_acl *acl, int type);
1393 
1394 /* readdir.c */
1395 int fuse_readdir(struct file *file, struct dir_context *ctx);
1396 
1397 /**
1398  * Return the number of bytes in an arguments list
1399  */
1400 unsigned int fuse_len_args(unsigned int numargs, struct fuse_arg *args);
1401 
1402 /**
1403  * Get the next unique ID for a request
1404  */
1405 u64 fuse_get_unique(struct fuse_iqueue *fiq);
1406 void fuse_free_conn(struct fuse_conn *fc);
1407 
1408 /* dax.c */
1409 
1410 #define FUSE_IS_DAX(inode) (IS_ENABLED(CONFIG_FUSE_DAX) && IS_DAX(inode))
1411 
1412 ssize_t fuse_dax_read_iter(struct kiocb *iocb, struct iov_iter *to);
1413 ssize_t fuse_dax_write_iter(struct kiocb *iocb, struct iov_iter *from);
1414 int fuse_dax_mmap(struct file *file, struct vm_area_struct *vma);
1415 int fuse_dax_break_layouts(struct inode *inode, u64 dmap_start, u64 dmap_end);
1416 int fuse_dax_conn_alloc(struct fuse_conn *fc, enum fuse_dax_mode mode,
1417 			struct dax_device *dax_dev);
1418 void fuse_dax_conn_free(struct fuse_conn *fc);
1419 bool fuse_dax_inode_alloc(struct super_block *sb, struct fuse_inode *fi);
1420 void fuse_dax_inode_init(struct inode *inode, unsigned int flags);
1421 void fuse_dax_inode_cleanup(struct inode *inode);
1422 void fuse_dax_dontcache(struct inode *inode, unsigned int flags);
1423 bool fuse_dax_check_alignment(struct fuse_conn *fc, unsigned int map_alignment);
1424 void fuse_dax_cancel_work(struct fuse_conn *fc);
1425 
1426 /* ioctl.c */
1427 long fuse_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
1428 long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
1429 			    unsigned long arg);
1430 int fuse_fileattr_get(struct dentry *dentry, struct fileattr *fa);
1431 int fuse_fileattr_set(struct user_namespace *mnt_userns,
1432 		      struct dentry *dentry, struct fileattr *fa);
1433 
1434 /* file.c */
1435 
1436 struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid,
1437 				 unsigned int open_flags, bool isdir);
1438 void fuse_file_release(struct inode *inode, struct fuse_file *ff,
1439 		       unsigned int open_flags, fl_owner_t id, bool isdir);
1440 
1441 /* passthrough.c */
1442 void fuse_copyattr(struct file *dst_file, struct file *src_file);
1443 int fuse_passthrough_open(struct fuse_dev *fud, u32 lower_fd);
1444 int fuse_passthrough_setup(struct fuse_conn *fc, struct fuse_file *ff,
1445 			   struct fuse_open_out *openarg);
1446 void fuse_passthrough_release(struct fuse_passthrough *passthrough);
1447 ssize_t fuse_passthrough_read_iter(struct kiocb *iocb, struct iov_iter *to);
1448 ssize_t fuse_passthrough_write_iter(struct kiocb *iocb, struct iov_iter *from);
1449 ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma);
1450 
1451 /* backing.c */
1452 
1453 /*
1454  * Dummy io passed to fuse_bpf_backing when io operation needs no scratch space
1455  */
1456 struct fuse_dummy_io {
1457 	int unused;
1458 };
1459 
1460 struct fuse_open_io {
1461 	struct fuse_open_in foi;
1462 	struct fuse_open_out foo;
1463 };
1464 
1465 int fuse_open_initialize(struct fuse_bpf_args *fa, struct fuse_open_io *foi,
1466 			 struct inode *inode, struct file *file, bool isdir);
1467 int fuse_open_backing(struct fuse_bpf_args *fa,
1468 		      struct inode *inode, struct file *file, bool isdir);
1469 void *fuse_open_finalize(struct fuse_bpf_args *fa,
1470 		       struct inode *inode, struct file *file, bool isdir);
1471 
1472 struct fuse_create_open_io {
1473 	struct fuse_create_in fci;
1474 	struct fuse_entry_out feo;
1475 	struct fuse_open_out foo;
1476 };
1477 
1478 int fuse_create_open_initialize(
1479 		struct fuse_bpf_args *fa, struct fuse_create_open_io *fcoi,
1480 		struct inode *dir, struct dentry *entry,
1481 		struct file *file, unsigned int flags, umode_t mode);
1482 int fuse_create_open_backing(
1483 		struct fuse_bpf_args *fa,
1484 		struct inode *dir, struct dentry *entry,
1485 		struct file *file, unsigned int flags, umode_t mode);
1486 void *fuse_create_open_finalize(
1487 		struct fuse_bpf_args *fa,
1488 		struct inode *dir, struct dentry *entry,
1489 		struct file *file, unsigned int flags, umode_t mode);
1490 
1491 int fuse_mknod_initialize(
1492 		struct fuse_bpf_args *fa, struct fuse_mknod_in *fmi,
1493 		struct inode *dir, struct dentry *entry, umode_t mode, dev_t rdev);
1494 int fuse_mknod_backing(
1495 		struct fuse_bpf_args *fa,
1496 		struct inode *dir, struct dentry *entry, umode_t mode, dev_t rdev);
1497 void *fuse_mknod_finalize(
1498 		struct fuse_bpf_args *fa,
1499 		struct inode *dir, struct dentry *entry, umode_t mode, dev_t rdev);
1500 
1501 int fuse_mkdir_initialize(
1502 		struct fuse_bpf_args *fa, struct fuse_mkdir_in *fmi,
1503 		struct inode *dir, struct dentry *entry, umode_t mode);
1504 int fuse_mkdir_backing(
1505 		struct fuse_bpf_args *fa,
1506 		struct inode *dir, struct dentry *entry, umode_t mode);
1507 void *fuse_mkdir_finalize(
1508 		struct fuse_bpf_args *fa,
1509 		struct inode *dir, struct dentry *entry, umode_t mode);
1510 
1511 int fuse_rmdir_initialize(
1512 		struct fuse_bpf_args *fa, struct fuse_dummy_io *fmi,
1513 		struct inode *dir, struct dentry *entry);
1514 int fuse_rmdir_backing(
1515 		struct fuse_bpf_args *fa,
1516 		struct inode *dir, struct dentry *entry);
1517 void *fuse_rmdir_finalize(
1518 		struct fuse_bpf_args *fa,
1519 		struct inode *dir, struct dentry *entry);
1520 
1521 int fuse_rename2_initialize(struct fuse_bpf_args *fa, struct fuse_rename2_in *fri,
1522 			    struct inode *olddir, struct dentry *oldent,
1523 			    struct inode *newdir, struct dentry *newent,
1524 			    unsigned int flags);
1525 int fuse_rename2_backing(struct fuse_bpf_args *fa,
1526 			 struct inode *olddir, struct dentry *oldent,
1527 			 struct inode *newdir, struct dentry *newent,
1528 			 unsigned int flags);
1529 void *fuse_rename2_finalize(struct fuse_bpf_args *fa,
1530 			    struct inode *olddir, struct dentry *oldent,
1531 			    struct inode *newdir, struct dentry *newent,
1532 			    unsigned int flags);
1533 
1534 int fuse_rename_initialize(struct fuse_bpf_args *fa, struct fuse_rename_in *fri,
1535 			   struct inode *olddir, struct dentry *oldent,
1536 			   struct inode *newdir, struct dentry *newent);
1537 int fuse_rename_backing(struct fuse_bpf_args *fa,
1538 			struct inode *olddir, struct dentry *oldent,
1539 			struct inode *newdir, struct dentry *newent);
1540 void *fuse_rename_finalize(struct fuse_bpf_args *fa,
1541 			   struct inode *olddir, struct dentry *oldent,
1542 			   struct inode *newdir, struct dentry *newent);
1543 
1544 int fuse_unlink_initialize(
1545 		struct fuse_bpf_args *fa, struct fuse_dummy_io *fmi,
1546 		struct inode *dir, struct dentry *entry);
1547 int fuse_unlink_backing(
1548 		struct fuse_bpf_args *fa,
1549 		struct inode *dir, struct dentry *entry);
1550 void *fuse_unlink_finalize(
1551 		struct fuse_bpf_args *fa,
1552 		struct inode *dir, struct dentry *entry);
1553 
1554 int fuse_link_initialize(struct fuse_bpf_args *fa, struct fuse_link_in *fli,
1555 			  struct dentry *entry, struct inode *dir,
1556 			  struct dentry *newent);
1557 int fuse_link_backing(struct fuse_bpf_args *fa, struct dentry *entry,
1558 		      struct inode *dir, struct dentry *newent);
1559 void *fuse_link_finalize(struct fuse_bpf_args *fa, struct dentry *entry,
1560 			 struct inode *dir, struct dentry *newent);
1561 
1562 int fuse_release_initialize(struct fuse_bpf_args *fa, struct fuse_release_in *fri,
1563 			    struct inode *inode, struct fuse_file *ff);
1564 int fuse_release_backing(struct fuse_bpf_args *fa,
1565 			 struct inode *inode, struct fuse_file *ff);
1566 void *fuse_release_finalize(struct fuse_bpf_args *fa,
1567 			    struct inode *inode, struct fuse_file *ff);
1568 
1569 int fuse_flush_initialize(struct fuse_bpf_args *fa, struct fuse_flush_in *ffi,
1570 			  struct file *file, fl_owner_t id);
1571 int fuse_flush_backing(struct fuse_bpf_args *fa, struct file *file, fl_owner_t id);
1572 void *fuse_flush_finalize(struct fuse_bpf_args *fa,
1573 			  struct file *file, fl_owner_t id);
1574 
1575 struct fuse_lseek_io {
1576 	struct fuse_lseek_in fli;
1577 	struct fuse_lseek_out flo;
1578 };
1579 
1580 int fuse_lseek_initialize(struct fuse_bpf_args *fa, struct fuse_lseek_io *fli,
1581 			  struct file *file, loff_t offset, int whence);
1582 int fuse_lseek_backing(struct fuse_bpf_args *fa, struct file *file, loff_t offset, int whence);
1583 void *fuse_lseek_finalize(struct fuse_bpf_args *fa, struct file *file, loff_t offset, int whence);
1584 
1585 struct fuse_copy_file_range_io {
1586 	struct fuse_copy_file_range_in fci;
1587 	struct fuse_write_out fwo;
1588 };
1589 
1590 int fuse_copy_file_range_initialize(struct fuse_bpf_args *fa,
1591 				   struct fuse_copy_file_range_io *fcf,
1592 				   struct file *file_in, loff_t pos_in,
1593 				   struct file *file_out, loff_t pos_out,
1594 				   size_t len, unsigned int flags);
1595 int fuse_copy_file_range_backing(struct fuse_bpf_args *fa,
1596 				 struct file *file_in, loff_t pos_in,
1597 				 struct file *file_out, loff_t pos_out,
1598 				 size_t len, unsigned int flags);
1599 void *fuse_copy_file_range_finalize(struct fuse_bpf_args *fa,
1600 				    struct file *file_in, loff_t pos_in,
1601 				    struct file *file_out, loff_t pos_out,
1602 				    size_t len, unsigned int flags);
1603 
1604 int fuse_fsync_initialize(struct fuse_bpf_args *fa, struct fuse_fsync_in *ffi,
1605 		   struct file *file, loff_t start, loff_t end, int datasync);
1606 int fuse_fsync_backing(struct fuse_bpf_args *fa,
1607 		   struct file *file, loff_t start, loff_t end, int datasync);
1608 void *fuse_fsync_finalize(struct fuse_bpf_args *fa,
1609 		   struct file *file, loff_t start, loff_t end, int datasync);
1610 int fuse_dir_fsync_initialize(struct fuse_bpf_args *fa, struct fuse_fsync_in *ffi,
1611 		   struct file *file, loff_t start, loff_t end, int datasync);
1612 
1613 struct fuse_getxattr_io {
1614 	struct fuse_getxattr_in fgi;
1615 	struct fuse_getxattr_out fgo;
1616 };
1617 
1618 int fuse_getxattr_initialize(
1619 		struct fuse_bpf_args *fa, struct fuse_getxattr_io *fgio,
1620 		struct dentry *dentry, const char *name, void *value,
1621 		size_t size);
1622 int fuse_getxattr_backing(
1623 		struct fuse_bpf_args *fa,
1624 		struct dentry *dentry, const char *name, void *value,
1625 		size_t size);
1626 void *fuse_getxattr_finalize(
1627 		struct fuse_bpf_args *fa,
1628 		struct dentry *dentry, const char *name, void *value,
1629 		size_t size);
1630 
1631 int fuse_listxattr_initialize(struct fuse_bpf_args *fa,
1632 			       struct fuse_getxattr_io *fgio,
1633 			       struct dentry *dentry, char *list, size_t size);
1634 int fuse_listxattr_backing(struct fuse_bpf_args *fa, struct dentry *dentry,
1635 			   char *list, size_t size);
1636 void *fuse_listxattr_finalize(struct fuse_bpf_args *fa, struct dentry *dentry,
1637 			      char *list, size_t size);
1638 
1639 int fuse_setxattr_initialize(struct fuse_bpf_args *fa,
1640 			     struct fuse_setxattr_in *fsxi,
1641 			     struct dentry *dentry, const char *name,
1642 			     const void *value, size_t size, int flags);
1643 int fuse_setxattr_backing(struct fuse_bpf_args *fa, struct dentry *dentry,
1644 			  const char *name, const void *value, size_t size,
1645 			  int flags);
1646 void *fuse_setxattr_finalize(struct fuse_bpf_args *fa, struct dentry *dentry,
1647 			     const char *name, const void *value, size_t size,
1648 			     int flags);
1649 
1650 int fuse_removexattr_initialize(struct fuse_bpf_args *fa,
1651 				struct fuse_dummy_io *unused,
1652 				struct dentry *dentry, const char *name);
1653 int fuse_removexattr_backing(struct fuse_bpf_args *fa,
1654 			     struct dentry *dentry, const char *name);
1655 void *fuse_removexattr_finalize(struct fuse_bpf_args *fa,
1656 				struct dentry *dentry, const char *name);
1657 
1658 struct fuse_read_iter_out {
1659 	uint64_t ret;
1660 };
1661 struct fuse_file_read_iter_io {
1662 	struct fuse_read_in fri;
1663 	struct fuse_read_iter_out frio;
1664 };
1665 
1666 int fuse_file_read_iter_initialize(
1667 		struct fuse_bpf_args *fa, struct fuse_file_read_iter_io *fri,
1668 		struct kiocb *iocb, struct iov_iter *to);
1669 int fuse_file_read_iter_backing(struct fuse_bpf_args *fa,
1670 		struct kiocb *iocb, struct iov_iter *to);
1671 void *fuse_file_read_iter_finalize(struct fuse_bpf_args *fa,
1672 		struct kiocb *iocb, struct iov_iter *to);
1673 
1674 struct fuse_write_iter_out {
1675 	uint64_t ret;
1676 };
1677 struct fuse_file_write_iter_io {
1678 	struct fuse_write_in fwi;
1679 	struct fuse_write_out fwo;
1680 	struct fuse_write_iter_out fwio;
1681 };
1682 
1683 int fuse_file_write_iter_initialize(
1684 		struct fuse_bpf_args *fa, struct fuse_file_write_iter_io *fwio,
1685 		struct kiocb *iocb, struct iov_iter *from);
1686 int fuse_file_write_iter_backing(struct fuse_bpf_args *fa,
1687 		struct kiocb *iocb, struct iov_iter *from);
1688 void *fuse_file_write_iter_finalize(struct fuse_bpf_args *fa,
1689 		struct kiocb *iocb, struct iov_iter *from);
1690 
1691 long fuse_backing_ioctl(struct file *file, unsigned int command, unsigned long arg, int flags);
1692 
1693 int fuse_file_flock_backing(struct file *file, int cmd, struct file_lock *fl);
1694 ssize_t fuse_backing_mmap(struct file *file, struct vm_area_struct *vma);
1695 
1696 int fuse_file_fallocate_initialize(struct fuse_bpf_args *fa,
1697 		struct fuse_fallocate_in *ffi,
1698 		struct file *file, int mode, loff_t offset, loff_t length);
1699 int fuse_file_fallocate_backing(struct fuse_bpf_args *fa,
1700 		struct file *file, int mode, loff_t offset, loff_t length);
1701 void *fuse_file_fallocate_finalize(struct fuse_bpf_args *fa,
1702 		struct file *file, int mode, loff_t offset, loff_t length);
1703 
1704 struct fuse_lookup_io {
1705 	struct fuse_entry_out feo;
1706 	struct fuse_entry_bpf feb;
1707 };
1708 
1709 int fuse_handle_backing(struct fuse_entry_bpf *feb, struct inode **backing_inode,
1710 			struct path *backing_path);
1711 int fuse_handle_bpf_prog(struct fuse_entry_bpf *feb, struct inode *parent,
1712 			 struct bpf_prog **bpf);
1713 
1714 int fuse_lookup_initialize(struct fuse_bpf_args *fa, struct fuse_lookup_io *feo,
1715 	       struct inode *dir, struct dentry *entry, unsigned int flags);
1716 int fuse_lookup_backing(struct fuse_bpf_args *fa, struct inode *dir,
1717 			  struct dentry *entry, unsigned int flags);
1718 struct dentry *fuse_lookup_finalize(struct fuse_bpf_args *fa, struct inode *dir,
1719 			   struct dentry *entry, unsigned int flags);
1720 int fuse_revalidate_backing(struct dentry *entry, unsigned int flags);
1721 
1722 int fuse_canonical_path_initialize(struct fuse_bpf_args *fa,
1723 				   struct fuse_dummy_io *fdi,
1724 				   const struct path *path,
1725 				   struct path *canonical_path);
1726 int fuse_canonical_path_backing(struct fuse_bpf_args *fa, const struct path *path,
1727 				struct path *canonical_path);
1728 void *fuse_canonical_path_finalize(struct fuse_bpf_args *fa,
1729 				   const struct path *path,
1730 				   struct path *canonical_path);
1731 
1732 struct fuse_getattr_io {
1733 	struct fuse_getattr_in fgi;
1734 	struct fuse_attr_out fao;
1735 };
1736 int fuse_getattr_initialize(struct fuse_bpf_args *fa, struct fuse_getattr_io *fgio,
1737 			const struct dentry *entry, struct kstat *stat,
1738 			u32 request_mask, unsigned int flags);
1739 int fuse_getattr_backing(struct fuse_bpf_args *fa,
1740 			const struct dentry *entry, struct kstat *stat,
1741 			u32 request_mask, unsigned int flags);
1742 void *fuse_getattr_finalize(struct fuse_bpf_args *fa,
1743 			const struct dentry *entry, struct kstat *stat,
1744 			u32 request_mask, unsigned int flags);
1745 
1746 struct fuse_setattr_io {
1747 	struct fuse_setattr_in fsi;
1748 	struct fuse_attr_out fao;
1749 };
1750 
1751 int fuse_setattr_initialize(struct fuse_bpf_args *fa, struct fuse_setattr_io *fsi,
1752 		struct dentry *dentry, struct iattr *attr, struct file *file);
1753 int fuse_setattr_backing(struct fuse_bpf_args *fa,
1754 		struct dentry *dentry, struct iattr *attr, struct file *file);
1755 void *fuse_setattr_finalize(struct fuse_bpf_args *fa,
1756 		struct dentry *dentry, struct iattr *attr, struct file *file);
1757 
1758 int fuse_statfs_initialize(struct fuse_bpf_args *fa, struct fuse_statfs_out *fso,
1759 		struct dentry *dentry, struct kstatfs *buf);
1760 int fuse_statfs_backing(struct fuse_bpf_args *fa,
1761 		struct dentry *dentry, struct kstatfs *buf);
1762 void *fuse_statfs_finalize(struct fuse_bpf_args *fa,
1763 		struct dentry *dentry, struct kstatfs *buf);
1764 
1765 int fuse_get_link_initialize(struct fuse_bpf_args *fa, struct fuse_dummy_io *dummy,
1766 		struct inode *inode, struct dentry *dentry,
1767 		struct delayed_call *callback, const char **out);
1768 int fuse_get_link_backing(struct fuse_bpf_args *fa,
1769 		struct inode *inode, struct dentry *dentry,
1770 		struct delayed_call *callback, const char **out);
1771 void *fuse_get_link_finalize(struct fuse_bpf_args *fa,
1772 		struct inode *inode, struct dentry *dentry,
1773 		struct delayed_call *callback, const char **out);
1774 
1775 int fuse_symlink_initialize(
1776 		struct fuse_bpf_args *fa, struct fuse_dummy_io *unused,
1777 		struct inode *dir, struct dentry *entry, const char *link, int len);
1778 int fuse_symlink_backing(
1779 		struct fuse_bpf_args *fa,
1780 		struct inode *dir, struct dentry *entry, const char *link, int len);
1781 void *fuse_symlink_finalize(
1782 		struct fuse_bpf_args *fa,
1783 		struct inode *dir, struct dentry *entry, const char *link, int len);
1784 
1785 struct fuse_read_io {
1786 	struct fuse_read_in fri;
1787 	struct fuse_read_out fro;
1788 };
1789 
1790 int fuse_readdir_initialize(struct fuse_bpf_args *fa, struct fuse_read_io *frio,
1791 			    struct file *file, struct dir_context *ctx,
1792 			    bool *force_again, bool *allow_force, bool is_continued);
1793 int fuse_readdir_backing(struct fuse_bpf_args *fa,
1794 			 struct file *file, struct dir_context *ctx,
1795 			 bool *force_again, bool *allow_force, bool is_continued);
1796 void *fuse_readdir_finalize(struct fuse_bpf_args *fa,
1797 			    struct file *file, struct dir_context *ctx,
1798 			    bool *force_again, bool *allow_force, bool is_continued);
1799 
1800 int fuse_access_initialize(struct fuse_bpf_args *fa, struct fuse_access_in *fai,
1801 			   struct inode *inode, int mask);
1802 int fuse_access_backing(struct fuse_bpf_args *fa, struct inode *inode, int mask);
1803 void *fuse_access_finalize(struct fuse_bpf_args *fa, struct inode *inode, int mask);
1804 
1805 /*
1806  * FUSE caches dentries and attributes with separate timeout.  The
1807  * time in jiffies until the dentry/attributes are valid is stored in
1808  * dentry->d_fsdata and fuse_inode->i_time respectively.
1809  */
1810 
1811 /*
1812  * Calculate the time in jiffies until a dentry/attributes are valid
1813  */
time_to_jiffies(u64 sec,u32 nsec)1814 static inline u64 time_to_jiffies(u64 sec, u32 nsec)
1815 {
1816 	if (sec || nsec) {
1817 		struct timespec64 ts = {
1818 			sec,
1819 			min_t(u32, nsec, NSEC_PER_SEC - 1)
1820 		};
1821 
1822 		return get_jiffies_64() + timespec64_to_jiffies(&ts);
1823 	} else
1824 		return 0;
1825 }
1826 
attr_timeout(struct fuse_attr_out * o)1827 static inline u64 attr_timeout(struct fuse_attr_out *o)
1828 {
1829 	return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
1830 }
1831 
update_mtime(unsigned int ivalid,bool trust_local_mtime)1832 static inline bool update_mtime(unsigned int ivalid, bool trust_local_mtime)
1833 {
1834 	/* Always update if mtime is explicitly set  */
1835 	if (ivalid & ATTR_MTIME_SET)
1836 		return true;
1837 
1838 	/* Or if kernel i_mtime is the official one */
1839 	if (trust_local_mtime)
1840 		return true;
1841 
1842 	/* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1843 	if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1844 		return false;
1845 
1846 	/* In all other cases update */
1847 	return true;
1848 }
1849 
1850 void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
1851 			  struct kstat *stat);
1852 
iattr_to_fattr(struct fuse_conn * fc,struct iattr * iattr,struct fuse_setattr_in * arg,bool trust_local_cmtime)1853 static inline void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr,
1854 			   struct fuse_setattr_in *arg, bool trust_local_cmtime)
1855 {
1856 	unsigned int ivalid = iattr->ia_valid;
1857 
1858 	if (ivalid & ATTR_MODE)
1859 		arg->valid |= FATTR_MODE,   arg->mode = iattr->ia_mode;
1860 	if (ivalid & ATTR_UID)
1861 		arg->valid |= FATTR_UID,    arg->uid = from_kuid(fc->user_ns, iattr->ia_uid);
1862 	if (ivalid & ATTR_GID)
1863 		arg->valid |= FATTR_GID,    arg->gid = from_kgid(fc->user_ns, iattr->ia_gid);
1864 	if (ivalid & ATTR_SIZE)
1865 		arg->valid |= FATTR_SIZE,   arg->size = iattr->ia_size;
1866 	if (ivalid & ATTR_ATIME) {
1867 		arg->valid |= FATTR_ATIME;
1868 		arg->atime = iattr->ia_atime.tv_sec;
1869 		arg->atimensec = iattr->ia_atime.tv_nsec;
1870 		if (!(ivalid & ATTR_ATIME_SET))
1871 			arg->valid |= FATTR_ATIME_NOW;
1872 	}
1873 	if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1874 		arg->valid |= FATTR_MTIME;
1875 		arg->mtime = iattr->ia_mtime.tv_sec;
1876 		arg->mtimensec = iattr->ia_mtime.tv_nsec;
1877 		if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1878 			arg->valid |= FATTR_MTIME_NOW;
1879 	}
1880 	if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1881 		arg->valid |= FATTR_CTIME;
1882 		arg->ctime = iattr->ia_ctime.tv_sec;
1883 		arg->ctimensec = iattr->ia_ctime.tv_nsec;
1884 	}
1885 }
1886 
finalize_attr(struct inode * inode,struct fuse_attr_out * outarg,u64 attr_version,struct kstat * stat)1887 static inline int finalize_attr(struct inode *inode, struct fuse_attr_out *outarg,
1888 				u64 attr_version, struct kstat *stat)
1889 {
1890 	int err = 0;
1891 
1892 	if (fuse_invalid_attr(&outarg->attr) ||
1893 	    ((inode->i_mode ^ outarg->attr.mode) & S_IFMT)) {
1894 		fuse_make_bad(inode);
1895 		err = -EIO;
1896 	} else {
1897 		fuse_change_attributes(inode, &outarg->attr,
1898 				       attr_timeout(outarg),
1899 				       attr_version);
1900 		if (stat)
1901 			fuse_fillattr(inode, &outarg->attr, stat);
1902 	}
1903 	return err;
1904 }
1905 
convert_statfs_to_fuse(struct fuse_kstatfs * attr,struct kstatfs * stbuf)1906 static inline void convert_statfs_to_fuse(struct fuse_kstatfs *attr, struct kstatfs *stbuf)
1907 {
1908 	attr->bsize   = stbuf->f_bsize;
1909 	attr->frsize  = stbuf->f_frsize;
1910 	attr->blocks  = stbuf->f_blocks;
1911 	attr->bfree   = stbuf->f_bfree;
1912 	attr->bavail  = stbuf->f_bavail;
1913 	attr->files   = stbuf->f_files;
1914 	attr->ffree   = stbuf->f_ffree;
1915 	attr->namelen = stbuf->f_namelen;
1916 	/* fsid is left zero */
1917 }
1918 
convert_fuse_statfs(struct kstatfs * stbuf,struct fuse_kstatfs * attr)1919 static inline void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
1920 {
1921 	stbuf->f_type    = FUSE_SUPER_MAGIC;
1922 	stbuf->f_bsize   = attr->bsize;
1923 	stbuf->f_frsize  = attr->frsize;
1924 	stbuf->f_blocks  = attr->blocks;
1925 	stbuf->f_bfree   = attr->bfree;
1926 	stbuf->f_bavail  = attr->bavail;
1927 	stbuf->f_files   = attr->files;
1928 	stbuf->f_ffree   = attr->ffree;
1929 	stbuf->f_namelen = attr->namelen;
1930 	/* fsid is left zero */
1931 }
1932 
1933 #ifdef CONFIG_FUSE_BPF
1934 struct fuse_err_ret {
1935 	void *result;
1936 	bool ret;
1937 };
1938 
1939 int __init fuse_bpf_init(void);
1940 void __exit fuse_bpf_cleanup(void);
1941 
1942 ssize_t fuse_bpf_simple_request(struct fuse_mount *fm, struct fuse_bpf_args *args);
1943 
fuse_bpf_run(struct bpf_prog * prog,struct fuse_bpf_args * fba)1944 static inline int fuse_bpf_run(struct bpf_prog *prog, struct fuse_bpf_args *fba)
1945 {
1946 	int ret;
1947 
1948 	migrate_disable();
1949 	ret = bpf_prog_run(prog, fba);
1950 	migrate_enable();
1951 	return ret;
1952 }
1953 
1954 /*
1955  * expression statement to wrap the backing filter logic
1956  * struct inode *inode: inode with bpf and backing inode
1957  * typedef io: (typically complex) type whose components fuse_args can point to.
1958  *	An instance of this type is created locally and passed to initialize
1959  * void initialize(struct fuse_bpf_args *fa, io *in_out, args...): function that sets
1960  *	up fa and io based on args
1961  * int backing(struct fuse_bpf_args *fa, args...): function that actually performs
1962  *	the backing io operation
1963  * void *finalize(struct fuse_bpf_args *, args...): function that performs any final
1964  *	work needed to commit the backing io
1965  */
1966 #define fuse_bpf_backing(inode, io, initialize, backing, finalize,	\
1967 			 args...)					\
1968 ({									\
1969 	struct fuse_err_ret fer = {0};					\
1970 	int ext_flags;							\
1971 	struct fuse_inode *fuse_inode = get_fuse_inode(inode);		\
1972 	struct fuse_mount *fm = get_fuse_mount(inode);			\
1973 	io feo = {0};							\
1974 	struct fuse_bpf_args fa = {0}, fa_backup = {0};			\
1975 	bool locked;							\
1976 	ssize_t res;							\
1977 	void *err;							\
1978 	int i;								\
1979 	bool initialized = false;					\
1980 									\
1981 	do {								\
1982 		if (!fuse_inode || !fuse_inode->backing_inode)		\
1983 			break;						\
1984 									\
1985 		err = ERR_PTR(initialize(&fa, &feo, args));		\
1986 		if (err) {						\
1987 			fer = (struct fuse_err_ret) {			\
1988 				err,					\
1989 				true,					\
1990 			};						\
1991 			break;						\
1992 		}							\
1993 		initialized = true;					\
1994 									\
1995 		fa_backup = fa;						\
1996 		fa.opcode |= FUSE_PREFILTER;				\
1997 		for (i = 0; i < fa.in_numargs; ++i)			\
1998 			fa.out_args[i] = (struct fuse_bpf_arg) {	\
1999 				.size = fa.in_args[i].size,		\
2000 				.value = (void *)fa.in_args[i].value,	\
2001 			};						\
2002 		fa.out_numargs = fa.in_numargs;				\
2003 									\
2004 		ext_flags = fuse_inode->bpf ?				\
2005 			fuse_bpf_run(fuse_inode->bpf, &fa) :		\
2006 			FUSE_BPF_BACKING;				\
2007 		if (ext_flags < 0) {					\
2008 			fer = (struct fuse_err_ret) {			\
2009 				ERR_PTR(ext_flags),			\
2010 				true,					\
2011 			};						\
2012 			break;						\
2013 		}							\
2014 									\
2015 		if (ext_flags & FUSE_BPF_USER_FILTER) {			\
2016 			locked = fuse_lock_inode(inode);		\
2017 			res = fuse_bpf_simple_request(fm, &fa);		\
2018 			fuse_unlock_inode(inode, locked);		\
2019 			if (res < 0) {					\
2020 				fer = (struct fuse_err_ret) {		\
2021 					ERR_PTR(res),			\
2022 					true,				\
2023 				};					\
2024 				break;					\
2025 			}						\
2026 		}							\
2027 									\
2028 		if (!(ext_flags & FUSE_BPF_BACKING))			\
2029 			break;						\
2030 									\
2031 		fa.opcode &= ~FUSE_PREFILTER;				\
2032 		for (i = 0; i < fa.in_numargs; ++i)			\
2033 			fa.in_args[i] = (struct fuse_bpf_in_arg) {	\
2034 				.size = fa.out_args[i].size,		\
2035 				.value = fa.out_args[i].value,		\
2036 			};						\
2037 		for (i = 0; i < fa_backup.out_numargs; ++i)		\
2038 			fa.out_args[i] = (struct fuse_bpf_arg) {	\
2039 				.size = fa_backup.out_args[i].size,	\
2040 				.value = fa_backup.out_args[i].value,	\
2041 			};						\
2042 		fa.out_numargs = fa_backup.out_numargs;			\
2043 									\
2044 		fer = (struct fuse_err_ret) {				\
2045 			ERR_PTR(backing(&fa, args)),			\
2046 			true,						\
2047 		};							\
2048 		if (IS_ERR(fer.result))					\
2049 			fa.error_in = PTR_ERR(fer.result);		\
2050 		if (!(ext_flags & FUSE_BPF_POST_FILTER))		\
2051 			break;						\
2052 									\
2053 		fa.opcode |= FUSE_POSTFILTER;				\
2054 		for (i = 0; i < fa.out_numargs; ++i)			\
2055 			fa.in_args[fa.in_numargs++] =			\
2056 				(struct fuse_bpf_in_arg) {		\
2057 					.size = fa.out_args[i].size,	\
2058 					.value = fa.out_args[i].value,	\
2059 				};					\
2060 		ext_flags = fuse_bpf_run(fuse_inode->bpf, &fa);		\
2061 		if (ext_flags < 0) {					\
2062 			fer = (struct fuse_err_ret) {			\
2063 				ERR_PTR(ext_flags),			\
2064 				true,					\
2065 			};						\
2066 			break;						\
2067 		}							\
2068 		if (!(ext_flags & FUSE_BPF_USER_FILTER))		\
2069 			break;						\
2070 									\
2071 		fa.out_args[0].size = fa_backup.out_args[0].size;	\
2072 		fa.out_args[1].size = fa_backup.out_args[1].size;	\
2073 		fa.out_numargs = fa_backup.out_numargs;			\
2074 		locked = fuse_lock_inode(inode);			\
2075 		res = fuse_bpf_simple_request(fm, &fa);			\
2076 		fuse_unlock_inode(inode, locked);			\
2077 		if (res < 0) {						\
2078 			fer.result = ERR_PTR(res);			\
2079 			break;						\
2080 		}							\
2081 	} while (false);						\
2082 									\
2083 	if (initialized && fer.ret) {					\
2084 		err = finalize(&fa, args);				\
2085 		if (err)						\
2086 			fer.result = err;				\
2087 	}								\
2088 									\
2089 	fer;								\
2090 })
2091 
2092 #endif /* CONFIG_FUSE_BPF */
2093 
2094 #endif /* _FS_FUSE_I_H */
2095