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