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