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