]> git.ipfire.org Git - thirdparty/kernel/linux.git/blob - fs/fuse/fuse_i.h
fuse: introduce FUSE_PASSTHROUGH capability
[thirdparty/kernel/linux.git] / fs / fuse / fuse_i.h
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/fuse.h>
17 #include <linux/fs.h>
18 #include <linux/mount.h>
19 #include <linux/wait.h>
20 #include <linux/list.h>
21 #include <linux/spinlock.h>
22 #include <linux/mm.h>
23 #include <linux/backing-dev.h>
24 #include <linux/mutex.h>
25 #include <linux/rwsem.h>
26 #include <linux/rbtree.h>
27 #include <linux/poll.h>
28 #include <linux/workqueue.h>
29 #include <linux/kref.h>
30 #include <linux/xattr.h>
31 #include <linux/pid_namespace.h>
32 #include <linux/refcount.h>
33 #include <linux/user_namespace.h>
34
35 /** Default max number of pages that can be used in a single read request */
36 #define FUSE_DEFAULT_MAX_PAGES_PER_REQ 32
37
38 /** Maximum of max_pages received in init_out */
39 #define FUSE_MAX_MAX_PAGES 256
40
41 /** Bias for fi->writectr, meaning new writepages must not be sent */
42 #define FUSE_NOWRITE INT_MIN
43
44 /** It could be as large as PATH_MAX, but would that have any uses? */
45 #define FUSE_NAME_MAX 1024
46
47 /** Number of dentries for each connection in the control filesystem */
48 #define FUSE_CTL_NUM_DENTRIES 5
49
50 /** List of active connections */
51 extern struct list_head fuse_conn_list;
52
53 /** Global mutex protecting fuse_conn_list and the control filesystem */
54 extern struct mutex fuse_mutex;
55
56 /** Module parameters */
57 extern unsigned max_user_bgreq;
58 extern unsigned max_user_congthresh;
59
60 /* One forget request */
61 struct fuse_forget_link {
62 struct fuse_forget_one forget_one;
63 struct fuse_forget_link *next;
64 };
65
66 /* Submount lookup tracking */
67 struct fuse_submount_lookup {
68 /** Refcount */
69 refcount_t count;
70
71 /** Unique ID, which identifies the inode between userspace
72 * and kernel */
73 u64 nodeid;
74
75 /** The request used for sending the FORGET message */
76 struct fuse_forget_link *forget;
77 };
78
79 /** Container for data related to mapping to backing file */
80 struct fuse_backing {
81 struct file *file;
82
83 /** refcount */
84 refcount_t count;
85 struct rcu_head rcu;
86 };
87
88 /** FUSE inode */
89 struct fuse_inode {
90 /** Inode data */
91 struct inode inode;
92
93 /** Unique ID, which identifies the inode between userspace
94 * and kernel */
95 u64 nodeid;
96
97 /** Number of lookups on this inode */
98 u64 nlookup;
99
100 /** The request used for sending the FORGET message */
101 struct fuse_forget_link *forget;
102
103 /** Time in jiffies until the file attributes are valid */
104 u64 i_time;
105
106 /* Which attributes are invalid */
107 u32 inval_mask;
108
109 /** The sticky bit in inode->i_mode may have been removed, so
110 preserve the original mode */
111 umode_t orig_i_mode;
112
113 /* Cache birthtime */
114 struct timespec64 i_btime;
115
116 /** 64 bit inode number */
117 u64 orig_ino;
118
119 /** Version of last attribute change */
120 u64 attr_version;
121
122 union {
123 /* read/write io cache (regular file only) */
124 struct {
125 /* Files usable in writepage. Protected by fi->lock */
126 struct list_head write_files;
127
128 /* Writepages pending on truncate or fsync */
129 struct list_head queued_writes;
130
131 /* Number of sent writes, a negative bias
132 * (FUSE_NOWRITE) means more writes are blocked */
133 int writectr;
134
135 /** Number of files/maps using page cache */
136 int iocachectr;
137
138 /* Waitq for writepage completion */
139 wait_queue_head_t page_waitq;
140
141 /* waitq for direct-io completion */
142 wait_queue_head_t direct_io_waitq;
143
144 /* List of writepage requestst (pending or sent) */
145 struct rb_root writepages;
146 };
147
148 /* readdir cache (directory only) */
149 struct {
150 /* true if fully cached */
151 bool cached;
152
153 /* size of cache */
154 loff_t size;
155
156 /* position at end of cache (position of next entry) */
157 loff_t pos;
158
159 /* version of the cache */
160 u64 version;
161
162 /* modification time of directory when cache was
163 * started */
164 struct timespec64 mtime;
165
166 /* iversion of directory when cache was started */
167 u64 iversion;
168
169 /* protects above fields */
170 spinlock_t lock;
171 } rdc;
172 };
173
174 /** Miscellaneous bits describing inode state */
175 unsigned long state;
176
177 /** Lock for serializing lookup and readdir for back compatibility*/
178 struct mutex mutex;
179
180 /** Lock to protect write related fields */
181 spinlock_t lock;
182
183 #ifdef CONFIG_FUSE_DAX
184 /*
185 * Dax specific inode data
186 */
187 struct fuse_inode_dax *dax;
188 #endif
189 /** Submount specific lookup tracking */
190 struct fuse_submount_lookup *submount_lookup;
191 #ifdef CONFIG_FUSE_PASSTHROUGH
192 /** Reference to backing file in passthrough mode */
193 struct fuse_backing *fb;
194 #endif
195 };
196
197 /** FUSE inode state bits */
198 enum {
199 /** Advise readdirplus */
200 FUSE_I_ADVISE_RDPLUS,
201 /** Initialized with readdirplus */
202 FUSE_I_INIT_RDPLUS,
203 /** An operation changing file size is in progress */
204 FUSE_I_SIZE_UNSTABLE,
205 /* Bad inode */
206 FUSE_I_BAD,
207 /* Has btime */
208 FUSE_I_BTIME,
209 /* Wants or already has page cache IO */
210 FUSE_I_CACHE_IO_MODE,
211 };
212
213 struct fuse_conn;
214 struct fuse_mount;
215 struct fuse_release_args;
216
217 /** FUSE specific file data */
218 struct fuse_file {
219 /** Fuse connection for this file */
220 struct fuse_mount *fm;
221
222 /* Argument space reserved for release */
223 struct fuse_release_args *release_args;
224
225 /** Kernel file handle guaranteed to be unique */
226 u64 kh;
227
228 /** File handle used by userspace */
229 u64 fh;
230
231 /** Node id of this file */
232 u64 nodeid;
233
234 /** Refcount */
235 refcount_t count;
236
237 /** FOPEN_* flags returned by open */
238 u32 open_flags;
239
240 /** Entry on inode's write_files list */
241 struct list_head write_entry;
242
243 /* Readdir related */
244 struct {
245 /*
246 * Protects below fields against (crazy) parallel readdir on
247 * same open file. Uncontended in the normal case.
248 */
249 struct mutex lock;
250
251 /* Dir stream position */
252 loff_t pos;
253
254 /* Offset in cache */
255 loff_t cache_off;
256
257 /* Version of cache we are reading */
258 u64 version;
259
260 } readdir;
261
262 /** RB node to be linked on fuse_conn->polled_files */
263 struct rb_node polled_node;
264
265 /** Wait queue head for poll */
266 wait_queue_head_t poll_wait;
267
268 /** Does file hold a fi->iocachectr refcount? */
269 enum { IOM_NONE, IOM_CACHED, IOM_UNCACHED } iomode;
270
271 /** Has flock been performed on this file? */
272 bool flock:1;
273 };
274
275 /** One input argument of a request */
276 struct fuse_in_arg {
277 unsigned size;
278 const void *value;
279 };
280
281 /** One output argument of a request */
282 struct fuse_arg {
283 unsigned size;
284 void *value;
285 };
286
287 /** FUSE page descriptor */
288 struct fuse_page_desc {
289 unsigned int length;
290 unsigned int offset;
291 };
292
293 struct fuse_args {
294 uint64_t nodeid;
295 uint32_t opcode;
296 uint8_t in_numargs;
297 uint8_t out_numargs;
298 uint8_t ext_idx;
299 bool force:1;
300 bool noreply:1;
301 bool nocreds:1;
302 bool in_pages:1;
303 bool out_pages:1;
304 bool user_pages:1;
305 bool out_argvar:1;
306 bool page_zeroing:1;
307 bool page_replace:1;
308 bool may_block:1;
309 bool is_ext:1;
310 struct fuse_in_arg in_args[3];
311 struct fuse_arg out_args[2];
312 void (*end)(struct fuse_mount *fm, struct fuse_args *args, int error);
313 };
314
315 struct fuse_args_pages {
316 struct fuse_args args;
317 struct page **pages;
318 struct fuse_page_desc *descs;
319 unsigned int num_pages;
320 };
321
322 #define FUSE_ARGS(args) struct fuse_args args = {}
323
324 /** The request IO state (for asynchronous processing) */
325 struct fuse_io_priv {
326 struct kref refcnt;
327 int async;
328 spinlock_t lock;
329 unsigned reqs;
330 ssize_t bytes;
331 size_t size;
332 __u64 offset;
333 bool write;
334 bool should_dirty;
335 int err;
336 struct kiocb *iocb;
337 struct completion *done;
338 bool blocking;
339 };
340
341 #define FUSE_IO_PRIV_SYNC(i) \
342 { \
343 .refcnt = KREF_INIT(1), \
344 .async = 0, \
345 .iocb = i, \
346 }
347
348 /**
349 * Request flags
350 *
351 * FR_ISREPLY: set if the request has reply
352 * FR_FORCE: force sending of the request even if interrupted
353 * FR_BACKGROUND: request is sent in the background
354 * FR_WAITING: request is counted as "waiting"
355 * FR_ABORTED: the request was aborted
356 * FR_INTERRUPTED: the request has been interrupted
357 * FR_LOCKED: data is being copied to/from the request
358 * FR_PENDING: request is not yet in userspace
359 * FR_SENT: request is in userspace, waiting for an answer
360 * FR_FINISHED: request is finished
361 * FR_PRIVATE: request is on private list
362 * FR_ASYNC: request is asynchronous
363 */
364 enum fuse_req_flag {
365 FR_ISREPLY,
366 FR_FORCE,
367 FR_BACKGROUND,
368 FR_WAITING,
369 FR_ABORTED,
370 FR_INTERRUPTED,
371 FR_LOCKED,
372 FR_PENDING,
373 FR_SENT,
374 FR_FINISHED,
375 FR_PRIVATE,
376 FR_ASYNC,
377 };
378
379 /**
380 * A request to the client
381 *
382 * .waitq.lock protects the following fields:
383 * - FR_ABORTED
384 * - FR_LOCKED (may also be modified under fc->lock, tested under both)
385 */
386 struct fuse_req {
387 /** This can be on either pending processing or io lists in
388 fuse_conn */
389 struct list_head list;
390
391 /** Entry on the interrupts list */
392 struct list_head intr_entry;
393
394 /* Input/output arguments */
395 struct fuse_args *args;
396
397 /** refcount */
398 refcount_t count;
399
400 /* Request flags, updated with test/set/clear_bit() */
401 unsigned long flags;
402
403 /* The request input header */
404 struct {
405 struct fuse_in_header h;
406 } in;
407
408 /* The request output header */
409 struct {
410 struct fuse_out_header h;
411 } out;
412
413 /** Used to wake up the task waiting for completion of request*/
414 wait_queue_head_t waitq;
415
416 #if IS_ENABLED(CONFIG_VIRTIO_FS)
417 /** virtio-fs's physically contiguous buffer for in and out args */
418 void *argbuf;
419 #endif
420
421 /** fuse_mount this request belongs to */
422 struct fuse_mount *fm;
423 };
424
425 struct fuse_iqueue;
426
427 /**
428 * Input queue callbacks
429 *
430 * Input queue signalling is device-specific. For example, the /dev/fuse file
431 * uses fiq->waitq and fasync to wake processes that are waiting on queue
432 * readiness. These callbacks allow other device types to respond to input
433 * queue activity.
434 */
435 struct fuse_iqueue_ops {
436 /**
437 * Signal that a forget has been queued
438 */
439 void (*wake_forget_and_unlock)(struct fuse_iqueue *fiq)
440 __releases(fiq->lock);
441
442 /**
443 * Signal that an INTERRUPT request has been queued
444 */
445 void (*wake_interrupt_and_unlock)(struct fuse_iqueue *fiq)
446 __releases(fiq->lock);
447
448 /**
449 * Signal that a request has been queued
450 */
451 void (*wake_pending_and_unlock)(struct fuse_iqueue *fiq)
452 __releases(fiq->lock);
453
454 /**
455 * Clean up when fuse_iqueue is destroyed
456 */
457 void (*release)(struct fuse_iqueue *fiq);
458 };
459
460 /** /dev/fuse input queue operations */
461 extern const struct fuse_iqueue_ops fuse_dev_fiq_ops;
462
463 struct fuse_iqueue {
464 /** Connection established */
465 unsigned connected;
466
467 /** Lock protecting accesses to members of this structure */
468 spinlock_t lock;
469
470 /** Readers of the connection are waiting on this */
471 wait_queue_head_t waitq;
472
473 /** The next unique request id */
474 u64 reqctr;
475
476 /** The list of pending requests */
477 struct list_head pending;
478
479 /** Pending interrupts */
480 struct list_head interrupts;
481
482 /** Queue of pending forgets */
483 struct fuse_forget_link forget_list_head;
484 struct fuse_forget_link *forget_list_tail;
485
486 /** Batching of FORGET requests (positive indicates FORGET batch) */
487 int forget_batch;
488
489 /** O_ASYNC requests */
490 struct fasync_struct *fasync;
491
492 /** Device-specific callbacks */
493 const struct fuse_iqueue_ops *ops;
494
495 /** Device-specific state */
496 void *priv;
497 };
498
499 #define FUSE_PQ_HASH_BITS 8
500 #define FUSE_PQ_HASH_SIZE (1 << FUSE_PQ_HASH_BITS)
501
502 struct fuse_pqueue {
503 /** Connection established */
504 unsigned connected;
505
506 /** Lock protecting accessess to members of this structure */
507 spinlock_t lock;
508
509 /** Hash table of requests being processed */
510 struct list_head *processing;
511
512 /** The list of requests under I/O */
513 struct list_head io;
514 };
515
516 /**
517 * Fuse device instance
518 */
519 struct fuse_dev {
520 /** Fuse connection for this device */
521 struct fuse_conn *fc;
522
523 /** Processing queue */
524 struct fuse_pqueue pq;
525
526 /** list entry on fc->devices */
527 struct list_head entry;
528 };
529
530 enum fuse_dax_mode {
531 FUSE_DAX_INODE_DEFAULT, /* default */
532 FUSE_DAX_ALWAYS, /* "-o dax=always" */
533 FUSE_DAX_NEVER, /* "-o dax=never" */
534 FUSE_DAX_INODE_USER, /* "-o dax=inode" */
535 };
536
537 static inline bool fuse_is_inode_dax_mode(enum fuse_dax_mode mode)
538 {
539 return mode == FUSE_DAX_INODE_DEFAULT || mode == FUSE_DAX_INODE_USER;
540 }
541
542 struct fuse_fs_context {
543 int fd;
544 struct file *file;
545 unsigned int rootmode;
546 kuid_t user_id;
547 kgid_t group_id;
548 bool is_bdev:1;
549 bool fd_present:1;
550 bool rootmode_present:1;
551 bool user_id_present:1;
552 bool group_id_present:1;
553 bool default_permissions:1;
554 bool allow_other:1;
555 bool destroy:1;
556 bool no_control:1;
557 bool no_force_umount:1;
558 bool legacy_opts_show:1;
559 enum fuse_dax_mode dax_mode;
560 unsigned int max_read;
561 unsigned int blksize;
562 const char *subtype;
563
564 /* DAX device, may be NULL */
565 struct dax_device *dax_dev;
566
567 /* fuse_dev pointer to fill in, should contain NULL on entry */
568 void **fudptr;
569 };
570
571 struct fuse_sync_bucket {
572 /* count is a possible scalability bottleneck */
573 atomic_t count;
574 wait_queue_head_t waitq;
575 struct rcu_head rcu;
576 };
577
578 /**
579 * A Fuse connection.
580 *
581 * This structure is created, when the root filesystem is mounted, and
582 * is destroyed, when the client device is closed and the last
583 * fuse_mount is destroyed.
584 */
585 struct fuse_conn {
586 /** Lock protecting accessess to members of this structure */
587 spinlock_t lock;
588
589 /** Refcount */
590 refcount_t count;
591
592 /** Number of fuse_dev's */
593 atomic_t dev_count;
594
595 struct rcu_head rcu;
596
597 /** The user id for this mount */
598 kuid_t user_id;
599
600 /** The group id for this mount */
601 kgid_t group_id;
602
603 /** The pid namespace for this mount */
604 struct pid_namespace *pid_ns;
605
606 /** The user namespace for this mount */
607 struct user_namespace *user_ns;
608
609 /** Maximum read size */
610 unsigned max_read;
611
612 /** Maximum write size */
613 unsigned max_write;
614
615 /** Maximum number of pages that can be used in a single request */
616 unsigned int max_pages;
617
618 /** Constrain ->max_pages to this value during feature negotiation */
619 unsigned int max_pages_limit;
620
621 /** Input queue */
622 struct fuse_iqueue iq;
623
624 /** The next unique kernel file handle */
625 atomic64_t khctr;
626
627 /** rbtree of fuse_files waiting for poll events indexed by ph */
628 struct rb_root polled_files;
629
630 /** Maximum number of outstanding background requests */
631 unsigned max_background;
632
633 /** Number of background requests at which congestion starts */
634 unsigned congestion_threshold;
635
636 /** Number of requests currently in the background */
637 unsigned num_background;
638
639 /** Number of background requests currently queued for userspace */
640 unsigned active_background;
641
642 /** The list of background requests set aside for later queuing */
643 struct list_head bg_queue;
644
645 /** Protects: max_background, congestion_threshold, num_background,
646 * active_background, bg_queue, blocked */
647 spinlock_t bg_lock;
648
649 /** Flag indicating that INIT reply has been received. Allocating
650 * any fuse request will be suspended until the flag is set */
651 int initialized;
652
653 /** Flag indicating if connection is blocked. This will be
654 the case before the INIT reply is received, and if there
655 are too many outstading backgrounds requests */
656 int blocked;
657
658 /** waitq for blocked connection */
659 wait_queue_head_t blocked_waitq;
660
661 /** Connection established, cleared on umount, connection
662 abort and device release */
663 unsigned connected;
664
665 /** Connection aborted via sysfs */
666 bool aborted;
667
668 /** Connection failed (version mismatch). Cannot race with
669 setting other bitfields since it is only set once in INIT
670 reply, before any other request, and never cleared */
671 unsigned conn_error:1;
672
673 /** Connection successful. Only set in INIT */
674 unsigned conn_init:1;
675
676 /** Do readahead asynchronously? Only set in INIT */
677 unsigned async_read:1;
678
679 /** Return an unique read error after abort. Only set in INIT */
680 unsigned abort_err:1;
681
682 /** Do not send separate SETATTR request before open(O_TRUNC) */
683 unsigned atomic_o_trunc:1;
684
685 /** Filesystem supports NFS exporting. Only set in INIT */
686 unsigned export_support:1;
687
688 /** write-back cache policy (default is write-through) */
689 unsigned writeback_cache:1;
690
691 /** allow parallel lookups and readdir (default is serialized) */
692 unsigned parallel_dirops:1;
693
694 /** handle fs handles killing suid/sgid/cap on write/chown/trunc */
695 unsigned handle_killpriv:1;
696
697 /** cache READLINK responses in page cache */
698 unsigned cache_symlinks:1;
699
700 /* show legacy mount options */
701 unsigned int legacy_opts_show:1;
702
703 /*
704 * fs kills suid/sgid/cap on write/chown/trunc. suid is killed on
705 * write/trunc only if caller did not have CAP_FSETID. sgid is killed
706 * on write/truncate only if caller did not have CAP_FSETID as well as
707 * file has group execute permission.
708 */
709 unsigned handle_killpriv_v2:1;
710
711 /*
712 * The following bitfields are only for optimization purposes
713 * and hence races in setting them will not cause malfunction
714 */
715
716 /** Is open/release not implemented by fs? */
717 unsigned no_open:1;
718
719 /** Is opendir/releasedir not implemented by fs? */
720 unsigned no_opendir:1;
721
722 /** Is fsync not implemented by fs? */
723 unsigned no_fsync:1;
724
725 /** Is fsyncdir not implemented by fs? */
726 unsigned no_fsyncdir:1;
727
728 /** Is flush not implemented by fs? */
729 unsigned no_flush:1;
730
731 /** Is setxattr not implemented by fs? */
732 unsigned no_setxattr:1;
733
734 /** Does file server support extended setxattr */
735 unsigned setxattr_ext:1;
736
737 /** Is getxattr not implemented by fs? */
738 unsigned no_getxattr:1;
739
740 /** Is listxattr not implemented by fs? */
741 unsigned no_listxattr:1;
742
743 /** Is removexattr not implemented by fs? */
744 unsigned no_removexattr:1;
745
746 /** Are posix file locking primitives not implemented by fs? */
747 unsigned no_lock:1;
748
749 /** Is access not implemented by fs? */
750 unsigned no_access:1;
751
752 /** Is create not implemented by fs? */
753 unsigned no_create:1;
754
755 /** Is interrupt not implemented by fs? */
756 unsigned no_interrupt:1;
757
758 /** Is bmap not implemented by fs? */
759 unsigned no_bmap:1;
760
761 /** Is poll not implemented by fs? */
762 unsigned no_poll:1;
763
764 /** Do multi-page cached writes */
765 unsigned big_writes:1;
766
767 /** Don't apply umask to creation modes */
768 unsigned dont_mask:1;
769
770 /** Are BSD file locking primitives not implemented by fs? */
771 unsigned no_flock:1;
772
773 /** Is fallocate not implemented by fs? */
774 unsigned no_fallocate:1;
775
776 /** Is rename with flags implemented by fs? */
777 unsigned no_rename2:1;
778
779 /** Use enhanced/automatic page cache invalidation. */
780 unsigned auto_inval_data:1;
781
782 /** Filesystem is fully responsible for page cache invalidation. */
783 unsigned explicit_inval_data:1;
784
785 /** Does the filesystem support readdirplus? */
786 unsigned do_readdirplus:1;
787
788 /** Does the filesystem want adaptive readdirplus? */
789 unsigned readdirplus_auto:1;
790
791 /** Does the filesystem support asynchronous direct-IO submission? */
792 unsigned async_dio:1;
793
794 /** Is lseek not implemented by fs? */
795 unsigned no_lseek:1;
796
797 /** Does the filesystem support posix acls? */
798 unsigned posix_acl:1;
799
800 /** Check permissions based on the file mode or not? */
801 unsigned default_permissions:1;
802
803 /** Allow other than the mounter user to access the filesystem ? */
804 unsigned allow_other:1;
805
806 /** Does the filesystem support copy_file_range? */
807 unsigned no_copy_file_range:1;
808
809 /* Send DESTROY request */
810 unsigned int destroy:1;
811
812 /* Delete dentries that have gone stale */
813 unsigned int delete_stale:1;
814
815 /** Do not create entry in fusectl fs */
816 unsigned int no_control:1;
817
818 /** Do not allow MNT_FORCE umount */
819 unsigned int no_force_umount:1;
820
821 /* Auto-mount submounts announced by the server */
822 unsigned int auto_submounts:1;
823
824 /* Propagate syncfs() to server */
825 unsigned int sync_fs:1;
826
827 /* Initialize security xattrs when creating a new inode */
828 unsigned int init_security:1;
829
830 /* Add supplementary group info when creating a new inode */
831 unsigned int create_supp_group:1;
832
833 /* Does the filesystem support per inode DAX? */
834 unsigned int inode_dax:1;
835
836 /* Is tmpfile not implemented by fs? */
837 unsigned int no_tmpfile:1;
838
839 /* Relax restrictions to allow shared mmap in FOPEN_DIRECT_IO mode */
840 unsigned int direct_io_allow_mmap:1;
841
842 /* Is statx not implemented by fs? */
843 unsigned int no_statx:1;
844
845 /** Passthrough support for read/write IO */
846 unsigned int passthrough:1;
847
848 /** Maximum stack depth for passthrough backing files */
849 int max_stack_depth;
850
851 /** The number of requests waiting for completion */
852 atomic_t num_waiting;
853
854 /** Negotiated minor version */
855 unsigned minor;
856
857 /** Entry on the fuse_mount_list */
858 struct list_head entry;
859
860 /** Device ID from the root super block */
861 dev_t dev;
862
863 /** Dentries in the control filesystem */
864 struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES];
865
866 /** number of dentries used in the above array */
867 int ctl_ndents;
868
869 /** Key for lock owner ID scrambling */
870 u32 scramble_key[4];
871
872 /** Version counter for attribute changes */
873 atomic64_t attr_version;
874
875 /** Called on final put */
876 void (*release)(struct fuse_conn *);
877
878 /**
879 * Read/write semaphore to hold when accessing the sb of any
880 * fuse_mount belonging to this connection
881 */
882 struct rw_semaphore killsb;
883
884 /** List of device instances belonging to this connection */
885 struct list_head devices;
886
887 #ifdef CONFIG_FUSE_DAX
888 /* Dax mode */
889 enum fuse_dax_mode dax_mode;
890
891 /* Dax specific conn data, non-NULL if DAX is enabled */
892 struct fuse_conn_dax *dax;
893 #endif
894
895 /** List of filesystems using this connection */
896 struct list_head mounts;
897
898 /* New writepages go into this bucket */
899 struct fuse_sync_bucket __rcu *curr_bucket;
900 };
901
902 /*
903 * Represents a mounted filesystem, potentially a submount.
904 *
905 * This object allows sharing a fuse_conn between separate mounts to
906 * allow submounts with dedicated superblocks and thus separate device
907 * IDs.
908 */
909 struct fuse_mount {
910 /* Underlying (potentially shared) connection to the FUSE server */
911 struct fuse_conn *fc;
912
913 /*
914 * Super block for this connection (fc->killsb must be held when
915 * accessing this).
916 */
917 struct super_block *sb;
918
919 /* Entry on fc->mounts */
920 struct list_head fc_entry;
921 };
922
923 static inline struct fuse_mount *get_fuse_mount_super(struct super_block *sb)
924 {
925 return sb->s_fs_info;
926 }
927
928 static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
929 {
930 return get_fuse_mount_super(sb)->fc;
931 }
932
933 static inline struct fuse_mount *get_fuse_mount(struct inode *inode)
934 {
935 return get_fuse_mount_super(inode->i_sb);
936 }
937
938 static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
939 {
940 return get_fuse_mount_super(inode->i_sb)->fc;
941 }
942
943 static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
944 {
945 return container_of(inode, struct fuse_inode, inode);
946 }
947
948 static inline u64 get_node_id(struct inode *inode)
949 {
950 return get_fuse_inode(inode)->nodeid;
951 }
952
953 static inline int invalid_nodeid(u64 nodeid)
954 {
955 return !nodeid || nodeid == FUSE_ROOT_ID;
956 }
957
958 static inline u64 fuse_get_attr_version(struct fuse_conn *fc)
959 {
960 return atomic64_read(&fc->attr_version);
961 }
962
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
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
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 static inline struct page **fuse_pages_alloc(unsigned int npages, gfp_t flags,
982 struct fuse_page_desc **desc)
983 {
984 struct page **pages;
985
986 pages = kzalloc(npages * (sizeof(struct page *) +
987 sizeof(struct fuse_page_desc)), flags);
988 *desc = (void *) (pages + npages);
989
990 return pages;
991 }
992
993 static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs,
994 unsigned int index,
995 unsigned int nr_pages)
996 {
997 int i;
998
999 for (i = index; i < index + nr_pages; i++)
1000 descs[i].length = PAGE_SIZE - descs[i].offset;
1001 }
1002
1003 static inline void fuse_sync_bucket_dec(struct fuse_sync_bucket *bucket)
1004 {
1005 /* Need RCU protection to prevent use after free after the decrement */
1006 rcu_read_lock();
1007 if (atomic_dec_and_test(&bucket->count))
1008 wake_up(&bucket->waitq);
1009 rcu_read_unlock();
1010 }
1011
1012 /** Device operations */
1013 extern const struct file_operations fuse_dev_operations;
1014
1015 extern const struct dentry_operations fuse_dentry_operations;
1016 extern const struct dentry_operations fuse_root_dentry_operations;
1017
1018 /**
1019 * Get a filled in inode
1020 */
1021 struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
1022 int generation, struct fuse_attr *attr,
1023 u64 attr_valid, u64 attr_version);
1024
1025 int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name,
1026 struct fuse_entry_out *outarg, struct inode **inode);
1027
1028 /**
1029 * Send FORGET command
1030 */
1031 void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
1032 u64 nodeid, u64 nlookup);
1033
1034 struct fuse_forget_link *fuse_alloc_forget(void);
1035
1036 struct fuse_forget_link *fuse_dequeue_forget(struct fuse_iqueue *fiq,
1037 unsigned int max,
1038 unsigned int *countp);
1039
1040 /*
1041 * Initialize READ or READDIR request
1042 */
1043 struct fuse_io_args {
1044 union {
1045 struct {
1046 struct fuse_read_in in;
1047 u64 attr_ver;
1048 } read;
1049 struct {
1050 struct fuse_write_in in;
1051 struct fuse_write_out out;
1052 bool page_locked;
1053 } write;
1054 };
1055 struct fuse_args_pages ap;
1056 struct fuse_io_priv *io;
1057 struct fuse_file *ff;
1058 };
1059
1060 void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
1061 size_t count, int opcode);
1062
1063
1064 struct fuse_file *fuse_file_alloc(struct fuse_mount *fm, bool release);
1065 void fuse_file_free(struct fuse_file *ff);
1066 int fuse_finish_open(struct inode *inode, struct file *file);
1067
1068 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff,
1069 unsigned int flags);
1070
1071 /**
1072 * Send RELEASE or RELEASEDIR request
1073 */
1074 void fuse_release_common(struct file *file, bool isdir);
1075
1076 /**
1077 * Send FSYNC or FSYNCDIR request
1078 */
1079 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
1080 int datasync, int opcode);
1081
1082 /**
1083 * Notify poll wakeup
1084 */
1085 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
1086 struct fuse_notify_poll_wakeup_out *outarg);
1087
1088 /**
1089 * Initialize file operations on a regular file
1090 */
1091 void fuse_init_file_inode(struct inode *inode, unsigned int flags);
1092
1093 /**
1094 * Initialize inode operations on regular files and special files
1095 */
1096 void fuse_init_common(struct inode *inode);
1097
1098 /**
1099 * Initialize inode and file operations on a directory
1100 */
1101 void fuse_init_dir(struct inode *inode);
1102
1103 /**
1104 * Initialize inode operations on a symlink
1105 */
1106 void fuse_init_symlink(struct inode *inode);
1107
1108 /**
1109 * Change attributes of an inode
1110 */
1111 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
1112 struct fuse_statx *sx,
1113 u64 attr_valid, u64 attr_version);
1114
1115 void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
1116 struct fuse_statx *sx,
1117 u64 attr_valid, u32 cache_mask);
1118
1119 u32 fuse_get_cache_mask(struct inode *inode);
1120
1121 /**
1122 * Initialize the client device
1123 */
1124 int fuse_dev_init(void);
1125
1126 /**
1127 * Cleanup the client device
1128 */
1129 void fuse_dev_cleanup(void);
1130
1131 int fuse_ctl_init(void);
1132 void __exit fuse_ctl_cleanup(void);
1133
1134 /**
1135 * Simple request sending that does request allocation and freeing
1136 */
1137 ssize_t fuse_simple_request(struct fuse_mount *fm, struct fuse_args *args);
1138 int fuse_simple_background(struct fuse_mount *fm, struct fuse_args *args,
1139 gfp_t gfp_flags);
1140
1141 /**
1142 * End a finished request
1143 */
1144 void fuse_request_end(struct fuse_req *req);
1145
1146 /* Abort all requests */
1147 void fuse_abort_conn(struct fuse_conn *fc);
1148 void fuse_wait_aborted(struct fuse_conn *fc);
1149
1150 /**
1151 * Invalidate inode attributes
1152 */
1153
1154 /* Attributes possibly changed on data modification */
1155 #define FUSE_STATX_MODIFY (STATX_MTIME | STATX_CTIME | STATX_BLOCKS)
1156
1157 /* Attributes possibly changed on data and/or size modification */
1158 #define FUSE_STATX_MODSIZE (FUSE_STATX_MODIFY | STATX_SIZE)
1159
1160 void fuse_invalidate_attr(struct inode *inode);
1161 void fuse_invalidate_attr_mask(struct inode *inode, u32 mask);
1162
1163 void fuse_invalidate_entry_cache(struct dentry *entry);
1164
1165 void fuse_invalidate_atime(struct inode *inode);
1166
1167 u64 fuse_time_to_jiffies(u64 sec, u32 nsec);
1168 #define ATTR_TIMEOUT(o) \
1169 fuse_time_to_jiffies((o)->attr_valid, (o)->attr_valid_nsec)
1170
1171 void fuse_change_entry_timeout(struct dentry *entry, struct fuse_entry_out *o);
1172
1173 /**
1174 * Acquire reference to fuse_conn
1175 */
1176 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
1177
1178 /**
1179 * Initialize fuse_conn
1180 */
1181 void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm,
1182 struct user_namespace *user_ns,
1183 const struct fuse_iqueue_ops *fiq_ops, void *fiq_priv);
1184
1185 /**
1186 * Release reference to fuse_conn
1187 */
1188 void fuse_conn_put(struct fuse_conn *fc);
1189
1190 struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc);
1191 struct fuse_dev *fuse_dev_alloc(void);
1192 void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc);
1193 void fuse_dev_free(struct fuse_dev *fud);
1194 void fuse_send_init(struct fuse_mount *fm);
1195
1196 /**
1197 * Fill in superblock and initialize fuse connection
1198 * @sb: partially-initialized superblock to fill in
1199 * @ctx: mount context
1200 */
1201 int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx);
1202
1203 /*
1204 * Remove the mount from the connection
1205 *
1206 * Returns whether this was the last mount
1207 */
1208 bool fuse_mount_remove(struct fuse_mount *fm);
1209
1210 /*
1211 * Setup context ops for submounts
1212 */
1213 int fuse_init_fs_context_submount(struct fs_context *fsc);
1214
1215 /*
1216 * Shut down the connection (possibly sending DESTROY request).
1217 */
1218 void fuse_conn_destroy(struct fuse_mount *fm);
1219
1220 /* Drop the connection and free the fuse mount */
1221 void fuse_mount_destroy(struct fuse_mount *fm);
1222
1223 /**
1224 * Add connection to control filesystem
1225 */
1226 int fuse_ctl_add_conn(struct fuse_conn *fc);
1227
1228 /**
1229 * Remove connection from control filesystem
1230 */
1231 void fuse_ctl_remove_conn(struct fuse_conn *fc);
1232
1233 /**
1234 * Is file type valid?
1235 */
1236 int fuse_valid_type(int m);
1237
1238 bool fuse_invalid_attr(struct fuse_attr *attr);
1239
1240 /**
1241 * Is current process allowed to perform filesystem operation?
1242 */
1243 bool fuse_allow_current_process(struct fuse_conn *fc);
1244
1245 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id);
1246
1247 void fuse_flush_time_update(struct inode *inode);
1248 void fuse_update_ctime(struct inode *inode);
1249
1250 int fuse_update_attributes(struct inode *inode, struct file *file, u32 mask);
1251
1252 void fuse_flush_writepages(struct inode *inode);
1253
1254 void fuse_set_nowrite(struct inode *inode);
1255 void fuse_release_nowrite(struct inode *inode);
1256
1257 /**
1258 * Scan all fuse_mounts belonging to fc to find the first where
1259 * ilookup5() returns a result. Return that result and the
1260 * respective fuse_mount in *fm (unless fm is NULL).
1261 *
1262 * The caller must hold fc->killsb.
1263 */
1264 struct inode *fuse_ilookup(struct fuse_conn *fc, u64 nodeid,
1265 struct fuse_mount **fm);
1266
1267 /**
1268 * File-system tells the kernel to invalidate cache for the given node id.
1269 */
1270 int fuse_reverse_inval_inode(struct fuse_conn *fc, u64 nodeid,
1271 loff_t offset, loff_t len);
1272
1273 /**
1274 * File-system tells the kernel to invalidate parent attributes and
1275 * the dentry matching parent/name.
1276 *
1277 * If the child_nodeid is non-zero and:
1278 * - matches the inode number for the dentry matching parent/name,
1279 * - is not a mount point
1280 * - is a file or oan empty directory
1281 * then the dentry is unhashed (d_delete()).
1282 */
1283 int fuse_reverse_inval_entry(struct fuse_conn *fc, u64 parent_nodeid,
1284 u64 child_nodeid, struct qstr *name, u32 flags);
1285
1286 int fuse_do_open(struct fuse_mount *fm, u64 nodeid, struct file *file,
1287 bool isdir);
1288
1289 /**
1290 * fuse_direct_io() flags
1291 */
1292
1293 /** If set, it is WRITE; otherwise - READ */
1294 #define FUSE_DIO_WRITE (1 << 0)
1295
1296 /** CUSE pass fuse_direct_io() a file which f_mapping->host is not from FUSE */
1297 #define FUSE_DIO_CUSE (1 << 1)
1298
1299 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1300 loff_t *ppos, int flags);
1301 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
1302 unsigned int flags);
1303 long fuse_ioctl_common(struct file *file, unsigned int cmd,
1304 unsigned long arg, unsigned int flags);
1305 __poll_t fuse_file_poll(struct file *file, poll_table *wait);
1306 int fuse_dev_release(struct inode *inode, struct file *file);
1307
1308 bool fuse_write_update_attr(struct inode *inode, loff_t pos, ssize_t written);
1309
1310 int fuse_flush_times(struct inode *inode, struct fuse_file *ff);
1311 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc);
1312
1313 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
1314 struct file *file);
1315
1316 void fuse_set_initialized(struct fuse_conn *fc);
1317
1318 void fuse_unlock_inode(struct inode *inode, bool locked);
1319 bool fuse_lock_inode(struct inode *inode);
1320
1321 int fuse_setxattr(struct inode *inode, const char *name, const void *value,
1322 size_t size, int flags, unsigned int extra_flags);
1323 ssize_t fuse_getxattr(struct inode *inode, const char *name, void *value,
1324 size_t size);
1325 ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size);
1326 int fuse_removexattr(struct inode *inode, const char *name);
1327 extern const struct xattr_handler * const fuse_xattr_handlers[];
1328
1329 struct posix_acl;
1330 struct posix_acl *fuse_get_inode_acl(struct inode *inode, int type, bool rcu);
1331 struct posix_acl *fuse_get_acl(struct mnt_idmap *idmap,
1332 struct dentry *dentry, int type);
1333 int fuse_set_acl(struct mnt_idmap *, struct dentry *dentry,
1334 struct posix_acl *acl, int type);
1335
1336 /* readdir.c */
1337 int fuse_readdir(struct file *file, struct dir_context *ctx);
1338
1339 /**
1340 * Return the number of bytes in an arguments list
1341 */
1342 unsigned int fuse_len_args(unsigned int numargs, struct fuse_arg *args);
1343
1344 /**
1345 * Get the next unique ID for a request
1346 */
1347 u64 fuse_get_unique(struct fuse_iqueue *fiq);
1348 void fuse_free_conn(struct fuse_conn *fc);
1349
1350 /* dax.c */
1351
1352 #define FUSE_IS_DAX(inode) (IS_ENABLED(CONFIG_FUSE_DAX) && IS_DAX(inode))
1353
1354 ssize_t fuse_dax_read_iter(struct kiocb *iocb, struct iov_iter *to);
1355 ssize_t fuse_dax_write_iter(struct kiocb *iocb, struct iov_iter *from);
1356 int fuse_dax_mmap(struct file *file, struct vm_area_struct *vma);
1357 int fuse_dax_break_layouts(struct inode *inode, u64 dmap_start, u64 dmap_end);
1358 int fuse_dax_conn_alloc(struct fuse_conn *fc, enum fuse_dax_mode mode,
1359 struct dax_device *dax_dev);
1360 void fuse_dax_conn_free(struct fuse_conn *fc);
1361 bool fuse_dax_inode_alloc(struct super_block *sb, struct fuse_inode *fi);
1362 void fuse_dax_inode_init(struct inode *inode, unsigned int flags);
1363 void fuse_dax_inode_cleanup(struct inode *inode);
1364 void fuse_dax_dontcache(struct inode *inode, unsigned int flags);
1365 bool fuse_dax_check_alignment(struct fuse_conn *fc, unsigned int map_alignment);
1366 void fuse_dax_cancel_work(struct fuse_conn *fc);
1367
1368 /* ioctl.c */
1369 long fuse_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
1370 long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
1371 unsigned long arg);
1372 int fuse_fileattr_get(struct dentry *dentry, struct fileattr *fa);
1373 int fuse_fileattr_set(struct mnt_idmap *idmap,
1374 struct dentry *dentry, struct fileattr *fa);
1375
1376 /* iomode.c */
1377 int fuse_file_cached_io_start(struct inode *inode, struct fuse_file *ff);
1378 int fuse_file_uncached_io_start(struct inode *inode, struct fuse_file *ff);
1379 void fuse_file_uncached_io_end(struct inode *inode, struct fuse_file *ff);
1380
1381 int fuse_file_io_open(struct file *file, struct inode *inode);
1382 void fuse_file_io_release(struct fuse_file *ff, struct inode *inode);
1383
1384 /* file.c */
1385 struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid,
1386 unsigned int open_flags, bool isdir);
1387 void fuse_file_release(struct inode *inode, struct fuse_file *ff,
1388 unsigned int open_flags, fl_owner_t id, bool isdir);
1389
1390 /* passthrough.c */
1391 static inline struct fuse_backing *fuse_inode_backing(struct fuse_inode *fi)
1392 {
1393 #ifdef CONFIG_FUSE_PASSTHROUGH
1394 return READ_ONCE(fi->fb);
1395 #else
1396 return NULL;
1397 #endif
1398 }
1399
1400 static inline struct fuse_backing *fuse_inode_backing_set(struct fuse_inode *fi,
1401 struct fuse_backing *fb)
1402 {
1403 #ifdef CONFIG_FUSE_PASSTHROUGH
1404 return xchg(&fi->fb, fb);
1405 #else
1406 return NULL;
1407 #endif
1408 }
1409
1410 struct fuse_backing *fuse_backing_get(struct fuse_backing *fb);
1411 void fuse_backing_put(struct fuse_backing *fb);
1412
1413 #endif /* _FS_FUSE_I_H */