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