2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
12 #include <linux/fuse.h>
14 #include <linux/mount.h>
15 #include <linux/wait.h>
16 #include <linux/list.h>
17 #include <linux/spinlock.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 #include <linux/kref.h>
26 #include <linux/xattr.h>
28 /** Max number of pages that can be used in a single read request */
29 #define FUSE_MAX_PAGES_PER_REQ 32
31 /** Bias for fi->writectr, meaning new writepages must not be sent */
32 #define FUSE_NOWRITE INT_MIN
34 /** It could be as large as PATH_MAX, but would that have any uses? */
35 #define FUSE_NAME_MAX 1024
37 /** Number of dentries for each connection in the control filesystem */
38 #define FUSE_CTL_NUM_DENTRIES 5
40 /** If the FUSE_DEFAULT_PERMISSIONS flag is given, the filesystem
41 module will check permissions based on the file mode. Otherwise no
42 permission checking is done in the kernel */
43 #define FUSE_DEFAULT_PERMISSIONS (1 << 0)
45 /** If the FUSE_ALLOW_OTHER flag is given, then not only the user
46 doing the mount will be allowed to access the filesystem */
47 #define FUSE_ALLOW_OTHER (1 << 1)
49 /** Number of page pointers embedded in fuse_req */
50 #define FUSE_REQ_INLINE_PAGES 1
52 /** List of active connections */
53 extern struct list_head fuse_conn_list
;
55 /** Global mutex protecting fuse_conn_list and the control filesystem */
56 extern struct mutex fuse_mutex
;
58 /** Module parameters */
59 extern unsigned max_user_bgreq
;
60 extern unsigned max_user_congthresh
;
62 /* One forget request */
63 struct fuse_forget_link
{
64 struct fuse_forget_one forget_one
;
65 struct fuse_forget_link
*next
;
73 /** Unique ID, which identifies the inode between userspace
77 /** Number of lookups on this inode */
80 /** The request used for sending the FORGET message */
81 struct fuse_forget_link
*forget
;
83 /** Time in jiffies until the file attributes are valid */
86 /** The sticky bit in inode->i_mode may have been removed, so
87 preserve the original mode */
90 /** 64 bit inode number */
93 /** Version of last attribute change */
96 /** Files usable in writepage. Protected by fc->lock */
97 struct list_head write_files
;
99 /** Writepages pending on truncate or fsync */
100 struct list_head queued_writes
;
102 /** Number of sent writes, a negative bias (FUSE_NOWRITE)
103 * means more writes are blocked */
106 /** Waitq for writepage completion */
107 wait_queue_head_t page_waitq
;
109 /** List of writepage requestst (pending or sent) */
110 struct list_head writepages
;
112 /** Miscellaneous bits describing inode state */
115 /** Lock for serializing lookup and readdir for back compatibility*/
119 /** FUSE inode state bits */
121 /** Advise readdirplus */
122 FUSE_I_ADVISE_RDPLUS
,
123 /** Initialized with readdirplus */
125 /** An operation changing file size is in progress */
126 FUSE_I_SIZE_UNSTABLE
,
131 /** FUSE specific file data */
133 /** Fuse connection for this file */
134 struct fuse_conn
*fc
;
136 /** Request reserved for flush and release */
137 struct fuse_req
*reserved_req
;
139 /** Kernel file handle guaranteed to be unique */
142 /** File handle used by userspace */
145 /** Node id of this file */
151 /** FOPEN_* flags returned by open */
154 /** Entry on inode's write_files list */
155 struct list_head write_entry
;
157 /** RB node to be linked on fuse_conn->polled_files */
158 struct rb_node polled_node
;
160 /** Wait queue head for poll */
161 wait_queue_head_t poll_wait
;
163 /** Has flock been performed on this file? */
167 /** One input argument of a request */
173 /** The request input */
175 /** The request header */
176 struct fuse_in_header h
;
178 /** True if the data for the last argument is in req->pages */
181 /** Number of arguments */
184 /** Array of arguments */
185 struct fuse_in_arg args
[3];
188 /** One output argument of a request */
194 /** The request output */
196 /** Header returned from userspace */
197 struct fuse_out_header h
;
200 * The following bitfields are not changed during the request
204 /** Last argument is variable length (can be shorter than
208 /** Last argument is a list of pages to copy data to */
211 /** Zero partially or not copied pages */
212 unsigned page_zeroing
:1;
214 /** Pages may be replaced with new ones */
215 unsigned page_replace
:1;
217 /** Number or arguments */
220 /** Array of arguments */
221 struct fuse_arg args
[2];
224 /** FUSE page descriptor */
225 struct fuse_page_desc
{
237 struct fuse_in_arg args
[3];
243 struct fuse_arg args
[2];
247 #define FUSE_ARGS(args) struct fuse_args args = {}
249 /** The request IO state (for asynchronous processing) */
250 struct fuse_io_priv
{
262 struct completion
*done
;
266 #define FUSE_IO_PRIV_SYNC(f) \
268 .refcnt = { ATOMIC_INIT(1) }, \
276 * FR_ISREPLY: set if the request has reply
277 * FR_FORCE: force sending of the request even if interrupted
278 * FR_BACKGROUND: request is sent in the background
279 * FR_WAITING: request is counted as "waiting"
280 * FR_ABORTED: the request was aborted
281 * FR_INTERRUPTED: the request has been interrupted
282 * FR_LOCKED: data is being copied to/from the request
283 * FR_PENDING: request is not yet in userspace
284 * FR_SENT: request is in userspace, waiting for an answer
285 * FR_FINISHED: request is finished
286 * FR_PRIVATE: request is on private list
303 * A request to the client
305 * .waitq.lock protects the following fields:
307 * - FR_LOCKED (may also be modified under fc->lock, tested under both)
310 /** This can be on either pending processing or io lists in
312 struct list_head list
;
314 /** Entry on the interrupts list */
315 struct list_head intr_entry
;
320 /** Unique ID for the interrupt request */
323 /* Request flags, updated with test/set/clear_bit() */
326 /** The request input */
329 /** The request output */
332 /** Used to wake up the task waiting for completion of request*/
333 wait_queue_head_t waitq
;
335 /** Data for asynchronous requests */
338 struct fuse_release_in in
;
341 struct fuse_init_in init_in
;
342 struct fuse_init_out init_out
;
343 struct cuse_init_in cuse_init_in
;
345 struct fuse_read_in in
;
349 struct fuse_write_in in
;
350 struct fuse_write_out out
;
351 struct fuse_req
*next
;
353 struct fuse_notify_retrieve_in retrieve_in
;
359 /** page-descriptor vector */
360 struct fuse_page_desc
*page_descs
;
362 /** size of the 'pages' array */
365 /** inline page vector */
366 struct page
*inline_pages
[FUSE_REQ_INLINE_PAGES
];
368 /** inline page-descriptor vector */
369 struct fuse_page_desc inline_page_descs
[FUSE_REQ_INLINE_PAGES
];
371 /** number of pages in vector */
374 /** File used in the request (or NULL) */
375 struct fuse_file
*ff
;
377 /** Inode used in the request or NULL */
380 /** AIO control block */
381 struct fuse_io_priv
*io
;
383 /** Link on fi->writepages */
384 struct list_head writepages_entry
;
386 /** Request completion callback */
387 void (*end
)(struct fuse_conn
*, struct fuse_req
*);
389 /** Request is stolen from fuse_file->reserved_req */
390 struct file
*stolen_file
;
394 /** Connection established */
397 /** Readers of the connection are waiting on this */
398 wait_queue_head_t waitq
;
400 /** The next unique request id */
403 /** The list of pending requests */
404 struct list_head pending
;
406 /** Pending interrupts */
407 struct list_head interrupts
;
409 /** Queue of pending forgets */
410 struct fuse_forget_link forget_list_head
;
411 struct fuse_forget_link
*forget_list_tail
;
413 /** Batching of FORGET requests (positive indicates FORGET batch) */
416 /** O_ASYNC requests */
417 struct fasync_struct
*fasync
;
421 /** Connection established */
424 /** Lock protecting accessess to members of this structure */
427 /** The list of requests being processed */
428 struct list_head processing
;
430 /** The list of requests under I/O */
435 * Fuse device instance
438 /** Fuse connection for this device */
439 struct fuse_conn
*fc
;
441 /** Processing queue */
442 struct fuse_pqueue pq
;
444 /** list entry on fc->devices */
445 struct list_head entry
;
451 * This structure is created, when the filesystem is mounted, and is
452 * destroyed, when the client device is closed and the filesystem is
456 /** Lock protecting accessess to members of this structure */
462 /** Number of fuse_dev's */
467 /** The user id for this mount */
470 /** The group id for this mount */
473 /** The fuse mount flags for this mount */
476 /** Maximum read size */
479 /** Maximum write size */
483 struct fuse_iqueue iq
;
485 /** The next unique kernel file handle */
488 /** rbtree of fuse_files waiting for poll events indexed by ph */
489 struct rb_root polled_files
;
491 /** Maximum number of outstanding background requests */
492 unsigned max_background
;
494 /** Number of background requests at which congestion starts */
495 unsigned congestion_threshold
;
497 /** Number of requests currently in the background */
498 unsigned num_background
;
500 /** Number of background requests currently queued for userspace */
501 unsigned active_background
;
503 /** The list of background requests set aside for later queuing */
504 struct list_head bg_queue
;
506 /** Flag indicating that INIT reply has been received. Allocating
507 * any fuse request will be suspended until the flag is set */
510 /** Flag indicating if connection is blocked. This will be
511 the case before the INIT reply is received, and if there
512 are too many outstading backgrounds requests */
515 /** waitq for blocked connection */
516 wait_queue_head_t blocked_waitq
;
518 /** waitq for reserved requests */
519 wait_queue_head_t reserved_req_waitq
;
521 /** Connection established, cleared on umount, connection
522 abort and device release */
525 /** Connection failed (version mismatch). Cannot race with
526 setting other bitfields since it is only set once in INIT
527 reply, before any other request, and never cleared */
528 unsigned conn_error
:1;
530 /** Connection successful. Only set in INIT */
531 unsigned conn_init
:1;
533 /** Do readpages asynchronously? Only set in INIT */
534 unsigned async_read
:1;
536 /** Do not send separate SETATTR request before open(O_TRUNC) */
537 unsigned atomic_o_trunc
:1;
539 /** Filesystem supports NFS exporting. Only set in INIT */
540 unsigned export_support
:1;
542 /** Set if bdi is valid */
543 unsigned bdi_initialized
:1;
545 /** write-back cache policy (default is write-through) */
546 unsigned writeback_cache
:1;
548 /** allow parallel lookups and readdir (default is serialized) */
549 unsigned parallel_dirops
:1;
551 /** handle fs handles killing suid/sgid/cap on write/chown/trunc */
552 unsigned handle_killpriv
:1;
555 * The following bitfields are only for optimization purposes
556 * and hence races in setting them will not cause malfunction
559 /** Is open/release not implemented by fs? */
562 /** Is fsync not implemented by fs? */
565 /** Is fsyncdir not implemented by fs? */
566 unsigned no_fsyncdir
:1;
568 /** Is flush not implemented by fs? */
571 /** Is setxattr not implemented by fs? */
572 unsigned no_setxattr
:1;
574 /** Is getxattr not implemented by fs? */
575 unsigned no_getxattr
:1;
577 /** Is listxattr not implemented by fs? */
578 unsigned no_listxattr
:1;
580 /** Is removexattr not implemented by fs? */
581 unsigned no_removexattr
:1;
583 /** Are posix file locking primitives not implemented by fs? */
586 /** Is access not implemented by fs? */
587 unsigned no_access
:1;
589 /** Is create not implemented by fs? */
590 unsigned no_create
:1;
592 /** Is interrupt not implemented by fs? */
593 unsigned no_interrupt
:1;
595 /** Is bmap not implemented by fs? */
598 /** Is poll not implemented by fs? */
601 /** Do multi-page cached writes */
602 unsigned big_writes
:1;
604 /** Don't apply umask to creation modes */
605 unsigned dont_mask
:1;
607 /** Are BSD file locking primitives not implemented by fs? */
610 /** Is fallocate not implemented by fs? */
611 unsigned no_fallocate
:1;
613 /** Is rename with flags implemented by fs? */
614 unsigned no_rename2
:1;
616 /** Use enhanced/automatic page cache invalidation. */
617 unsigned auto_inval_data
:1;
619 /** Does the filesystem support readdirplus? */
620 unsigned do_readdirplus
:1;
622 /** Does the filesystem want adaptive readdirplus? */
623 unsigned readdirplus_auto
:1;
625 /** Does the filesystem support asynchronous direct-IO submission? */
626 unsigned async_dio
:1;
628 /** Is lseek not implemented by fs? */
631 /** Does the filesystem support posix acls? */
632 unsigned posix_acl
:1;
634 /** The number of requests waiting for completion */
635 atomic_t num_waiting
;
637 /** Negotiated minor version */
640 /** Backing dev info */
641 struct backing_dev_info bdi
;
643 /** Entry on the fuse_conn_list */
644 struct list_head entry
;
646 /** Device ID from super block */
649 /** Dentries in the control filesystem */
650 struct dentry
*ctl_dentry
[FUSE_CTL_NUM_DENTRIES
];
652 /** number of dentries used in the above array */
655 /** Key for lock owner ID scrambling */
658 /** Reserved request for the DESTROY message */
659 struct fuse_req
*destroy_req
;
661 /** Version counter for attribute changes */
664 /** Called on final put */
665 void (*release
)(struct fuse_conn
*);
667 /** Super block for this connection. */
668 struct super_block
*sb
;
670 /** Read/write semaphore to hold when accessing sb. */
671 struct rw_semaphore killsb
;
673 /** List of device instances belonging to this connection */
674 struct list_head devices
;
677 static inline struct fuse_conn
*get_fuse_conn_super(struct super_block
*sb
)
679 return sb
->s_fs_info
;
682 static inline struct fuse_conn
*get_fuse_conn(struct inode
*inode
)
684 return get_fuse_conn_super(inode
->i_sb
);
687 static inline struct fuse_inode
*get_fuse_inode(struct inode
*inode
)
689 return container_of(inode
, struct fuse_inode
, inode
);
692 static inline u64
get_node_id(struct inode
*inode
)
694 return get_fuse_inode(inode
)->nodeid
;
697 /** Device operations */
698 extern const struct file_operations fuse_dev_operations
;
700 extern const struct dentry_operations fuse_dentry_operations
;
703 * Inode to nodeid comparison.
705 int fuse_inode_eq(struct inode
*inode
, void *_nodeidp
);
708 * Get a filled in inode
710 struct inode
*fuse_iget(struct super_block
*sb
, u64 nodeid
,
711 int generation
, struct fuse_attr
*attr
,
712 u64 attr_valid
, u64 attr_version
);
714 int fuse_lookup_name(struct super_block
*sb
, u64 nodeid
, const struct qstr
*name
,
715 struct fuse_entry_out
*outarg
, struct inode
**inode
);
718 * Send FORGET command
720 void fuse_queue_forget(struct fuse_conn
*fc
, struct fuse_forget_link
*forget
,
721 u64 nodeid
, u64 nlookup
);
723 struct fuse_forget_link
*fuse_alloc_forget(void);
725 /* Used by READDIRPLUS */
726 void fuse_force_forget(struct file
*file
, u64 nodeid
);
729 * Initialize READ or READDIR request
731 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
,
732 loff_t pos
, size_t count
, int opcode
);
735 * Send OPEN or OPENDIR request
737 int fuse_open_common(struct inode
*inode
, struct file
*file
, bool isdir
);
739 struct fuse_file
*fuse_file_alloc(struct fuse_conn
*fc
);
740 struct fuse_file
*fuse_file_get(struct fuse_file
*ff
);
741 void fuse_file_free(struct fuse_file
*ff
);
742 void fuse_finish_open(struct inode
*inode
, struct file
*file
);
744 void fuse_sync_release(struct fuse_file
*ff
, int flags
);
747 * Send RELEASE or RELEASEDIR request
749 void fuse_release_common(struct file
*file
, int opcode
);
752 * Send FSYNC or FSYNCDIR request
754 int fuse_fsync_common(struct file
*file
, loff_t start
, loff_t end
,
755 int datasync
, int isdir
);
760 int fuse_notify_poll_wakeup(struct fuse_conn
*fc
,
761 struct fuse_notify_poll_wakeup_out
*outarg
);
764 * Initialize file operations on a regular file
766 void fuse_init_file_inode(struct inode
*inode
);
769 * Initialize inode operations on regular files and special files
771 void fuse_init_common(struct inode
*inode
);
774 * Initialize inode and file operations on a directory
776 void fuse_init_dir(struct inode
*inode
);
779 * Initialize inode operations on a symlink
781 void fuse_init_symlink(struct inode
*inode
);
784 * Change attributes of an inode
786 void fuse_change_attributes(struct inode
*inode
, struct fuse_attr
*attr
,
787 u64 attr_valid
, u64 attr_version
);
789 void fuse_change_attributes_common(struct inode
*inode
, struct fuse_attr
*attr
,
793 * Initialize the client device
795 int fuse_dev_init(void);
798 * Cleanup the client device
800 void fuse_dev_cleanup(void);
802 int fuse_ctl_init(void);
803 void __exit
fuse_ctl_cleanup(void);
808 struct fuse_req
*fuse_request_alloc(unsigned npages
);
810 struct fuse_req
*fuse_request_alloc_nofs(unsigned npages
);
815 void fuse_request_free(struct fuse_req
*req
);
818 * Get a request, may fail with -ENOMEM,
819 * caller should specify # elements in req->pages[] explicitly
821 struct fuse_req
*fuse_get_req(struct fuse_conn
*fc
, unsigned npages
);
822 struct fuse_req
*fuse_get_req_for_background(struct fuse_conn
*fc
,
826 * Increment reference count on request
828 void __fuse_get_request(struct fuse_req
*req
);
831 * Gets a requests for a file operation, always succeeds
833 struct fuse_req
*fuse_get_req_nofail_nopages(struct fuse_conn
*fc
,
837 * Decrement reference count of a request. If count goes to zero free
840 void fuse_put_request(struct fuse_conn
*fc
, struct fuse_req
*req
);
843 * Send a request (synchronous)
845 void fuse_request_send(struct fuse_conn
*fc
, struct fuse_req
*req
);
848 * Simple request sending that does request allocation and freeing
850 ssize_t
fuse_simple_request(struct fuse_conn
*fc
, struct fuse_args
*args
);
853 * Send a request in the background
855 void fuse_request_send_background(struct fuse_conn
*fc
, struct fuse_req
*req
);
857 void fuse_request_send_background_locked(struct fuse_conn
*fc
,
858 struct fuse_req
*req
);
860 /* Abort all requests */
861 void fuse_abort_conn(struct fuse_conn
*fc
);
864 * Invalidate inode attributes
866 void fuse_invalidate_attr(struct inode
*inode
);
868 void fuse_invalidate_entry_cache(struct dentry
*entry
);
870 void fuse_invalidate_atime(struct inode
*inode
);
873 * Acquire reference to fuse_conn
875 struct fuse_conn
*fuse_conn_get(struct fuse_conn
*fc
);
878 * Initialize fuse_conn
880 void fuse_conn_init(struct fuse_conn
*fc
);
883 * Release reference to fuse_conn
885 void fuse_conn_put(struct fuse_conn
*fc
);
887 struct fuse_dev
*fuse_dev_alloc(struct fuse_conn
*fc
);
888 void fuse_dev_free(struct fuse_dev
*fud
);
891 * Add connection to control filesystem
893 int fuse_ctl_add_conn(struct fuse_conn
*fc
);
896 * Remove connection from control filesystem
898 void fuse_ctl_remove_conn(struct fuse_conn
*fc
);
901 * Is file type valid?
903 int fuse_valid_type(int m
);
906 * Is current process allowed to perform filesystem operation?
908 int fuse_allow_current_process(struct fuse_conn
*fc
);
910 u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
);
912 void fuse_update_ctime(struct inode
*inode
);
914 int fuse_update_attributes(struct inode
*inode
, struct kstat
*stat
,
915 struct file
*file
, bool *refreshed
);
917 void fuse_flush_writepages(struct inode
*inode
);
919 void fuse_set_nowrite(struct inode
*inode
);
920 void fuse_release_nowrite(struct inode
*inode
);
922 u64
fuse_get_attr_version(struct fuse_conn
*fc
);
925 * File-system tells the kernel to invalidate cache for the given node id.
927 int fuse_reverse_inval_inode(struct super_block
*sb
, u64 nodeid
,
928 loff_t offset
, loff_t len
);
931 * File-system tells the kernel to invalidate parent attributes and
932 * the dentry matching parent/name.
934 * If the child_nodeid is non-zero and:
935 * - matches the inode number for the dentry matching parent/name,
936 * - is not a mount point
937 * - is a file or oan empty directory
938 * then the dentry is unhashed (d_delete()).
940 int fuse_reverse_inval_entry(struct super_block
*sb
, u64 parent_nodeid
,
941 u64 child_nodeid
, struct qstr
*name
);
943 int fuse_do_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
947 * fuse_direct_io() flags
950 /** If set, it is WRITE; otherwise - READ */
951 #define FUSE_DIO_WRITE (1 << 0)
953 /** CUSE pass fuse_direct_io() a file which f_mapping->host is not from FUSE */
954 #define FUSE_DIO_CUSE (1 << 1)
956 ssize_t
fuse_direct_io(struct fuse_io_priv
*io
, struct iov_iter
*iter
,
957 loff_t
*ppos
, int flags
);
958 long fuse_do_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
,
960 long fuse_ioctl_common(struct file
*file
, unsigned int cmd
,
961 unsigned long arg
, unsigned int flags
);
962 unsigned fuse_file_poll(struct file
*file
, poll_table
*wait
);
963 int fuse_dev_release(struct inode
*inode
, struct file
*file
);
965 bool fuse_write_update_size(struct inode
*inode
, loff_t pos
);
967 int fuse_flush_times(struct inode
*inode
, struct fuse_file
*ff
);
968 int fuse_write_inode(struct inode
*inode
, struct writeback_control
*wbc
);
970 int fuse_do_setattr(struct inode
*inode
, struct iattr
*attr
,
973 void fuse_set_initialized(struct fuse_conn
*fc
);
975 void fuse_unlock_inode(struct inode
*inode
);
976 void fuse_lock_inode(struct inode
*inode
);
978 int fuse_setxattr(struct inode
*inode
, const char *name
, const void *value
,
979 size_t size
, int flags
);
980 ssize_t
fuse_getxattr(struct inode
*inode
, const char *name
, void *value
,
982 ssize_t
fuse_listxattr(struct dentry
*entry
, char *list
, size_t size
);
983 int fuse_removexattr(struct inode
*inode
, const char *name
);
984 extern const struct xattr_handler
*fuse_xattr_handlers
[];
985 extern const struct xattr_handler
*fuse_acl_xattr_handlers
[];
988 struct posix_acl
*fuse_get_acl(struct inode
*inode
, int type
);
989 int fuse_set_acl(struct inode
*inode
, struct posix_acl
*acl
, int type
);
991 #endif /* _FS_FUSE_I_H */