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>
26 /** Max number of pages that can be used in a single read request */
27 #define FUSE_MAX_PAGES_PER_REQ 32
29 /** Bias for fi->writectr, meaning new writepages must not be sent */
30 #define FUSE_NOWRITE INT_MIN
32 /** It could be as large as PATH_MAX, but would that have any uses? */
33 #define FUSE_NAME_MAX 1024
35 /** Number of dentries for each connection in the control filesystem */
36 #define FUSE_CTL_NUM_DENTRIES 5
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)
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)
47 /** Number of page pointers embedded in fuse_req */
48 #define FUSE_REQ_INLINE_PAGES 1
50 /** List of active connections */
51 extern struct list_head fuse_conn_list;
53 /** Global mutex protecting fuse_conn_list and the control filesystem */
54 extern struct mutex fuse_mutex;
56 /** Module parameters */
57 extern unsigned max_user_bgreq;
58 extern unsigned max_user_congthresh;
60 /* One forget request */
61 struct fuse_forget_link {
62 struct fuse_forget_one forget_one;
63 struct fuse_forget_link *next;
71 /** Unique ID, which identifies the inode between userspace
75 /** Number of lookups on this inode */
78 /** The request used for sending the FORGET message */
79 struct fuse_forget_link *forget;
81 /** Time in jiffies until the file attributes are valid */
84 /** The sticky bit in inode->i_mode may have been removed, so
85 preserve the original mode */
88 /** 64 bit inode number */
91 /** Version of last attribute change */
94 /** Files usable in writepage. Protected by fc->lock */
95 struct list_head write_files;
97 /** Writepages pending on truncate or fsync */
98 struct list_head queued_writes;
100 /** Number of sent writes, a negative bias (FUSE_NOWRITE)
101 * means more writes are blocked */
104 /** Waitq for writepage completion */
105 wait_queue_head_t page_waitq;
107 /** List of writepage requestst (pending or sent) */
108 struct list_head writepages;
113 /** FUSE specific file data */
115 /** Fuse connection for this file */
116 struct fuse_conn *fc;
118 /** Request reserved for flush and release */
119 struct fuse_req *reserved_req;
121 /** Kernel file handle guaranteed to be unique */
124 /** File handle used by userspace */
127 /** Node id of this file */
133 /** FOPEN_* flags returned by open */
136 /** Entry on inode's write_files list */
137 struct list_head write_entry;
139 /** RB node to be linked on fuse_conn->polled_files */
140 struct rb_node polled_node;
142 /** Wait queue head for poll */
143 wait_queue_head_t poll_wait;
145 /** Has flock been performed on this file? */
149 /** One input argument of a request */
155 /** The request input */
157 /** The request header */
158 struct fuse_in_header h;
160 /** True if the data for the last argument is in req->pages */
163 /** Number of arguments */
166 /** Array of arguments */
167 struct fuse_in_arg args[3];
170 /** One output argument of a request */
176 /** The request output */
178 /** Header returned from userspace */
179 struct fuse_out_header h;
182 * The following bitfields are not changed during the request
186 /** Last argument is variable length (can be shorter than
190 /** Last argument is a list of pages to copy data to */
193 /** Zero partially or not copied pages */
194 unsigned page_zeroing:1;
196 /** Pages may be replaced with new ones */
197 unsigned page_replace:1;
199 /** Number or arguments */
202 /** Array of arguments */
203 struct fuse_arg args[3];
206 /** The request state */
207 enum fuse_req_state {
217 * A request to the client
220 /** This can be on either pending processing or io lists in
222 struct list_head list;
224 /** Entry on the interrupts list */
225 struct list_head intr_entry;
230 /** Unique ID for the interrupt request */
234 * The following bitfields are either set once before the
235 * request is queued or setting/clearing them is protected by
239 /** True if the request has reply */
242 /** Force sending of the request even if interrupted */
245 /** The request was aborted */
248 /** Request is sent in the background */
249 unsigned background:1;
251 /** The request has been interrupted */
252 unsigned interrupted:1;
254 /** Data is being copied to/from the request */
257 /** Request is counted as "waiting" */
260 /** State of the request */
261 enum fuse_req_state state;
263 /** The request input */
266 /** The request output */
269 /** Used to wake up the task waiting for completion of request*/
270 wait_queue_head_t waitq;
272 /** Data for asynchronous requests */
276 struct fuse_release_in in;
277 struct work_struct work;
281 struct fuse_init_in init_in;
282 struct fuse_init_out init_out;
283 struct cuse_init_in cuse_init_in;
285 struct fuse_read_in in;
289 struct fuse_write_in in;
290 struct fuse_write_out out;
292 struct fuse_notify_retrieve_in retrieve_in;
293 struct fuse_lk_in lk_in;
299 /** size of the 'pages' array */
302 /** inline page vector */
303 struct page *inline_pages[FUSE_REQ_INLINE_PAGES];
305 /** number of pages in vector */
308 /** offset of data on first page */
309 unsigned page_offset;
311 /** File used in the request (or NULL) */
312 struct fuse_file *ff;
314 /** Inode used in the request or NULL */
317 /** Link on fi->writepages */
318 struct list_head writepages_entry;
320 /** Request completion callback */
321 void (*end)(struct fuse_conn *, struct fuse_req *);
323 /** Request is stolen from fuse_file->reserved_req */
324 struct file *stolen_file;
330 * This structure is created, when the filesystem is mounted, and is
331 * destroyed, when the client device is closed and the filesystem is
335 /** Lock protecting accessess to members of this structure */
338 /** Mutex protecting against directory alias creation */
339 struct mutex inst_mutex;
344 /** The user id for this mount */
347 /** The group id for this mount */
350 /** The fuse mount flags for this mount */
353 /** Maximum read size */
356 /** Maximum write size */
359 /** Readers of the connection are waiting on this */
360 wait_queue_head_t waitq;
362 /** The list of pending requests */
363 struct list_head pending;
365 /** The list of requests being processed */
366 struct list_head processing;
368 /** The list of requests under I/O */
371 /** The next unique kernel file handle */
374 /** rbtree of fuse_files waiting for poll events indexed by ph */
375 struct rb_root polled_files;
377 /** Maximum number of outstanding background requests */
378 unsigned max_background;
380 /** Number of background requests at which congestion starts */
381 unsigned congestion_threshold;
383 /** Number of requests currently in the background */
384 unsigned num_background;
386 /** Number of background requests currently queued for userspace */
387 unsigned active_background;
389 /** The list of background requests set aside for later queuing */
390 struct list_head bg_queue;
392 /** Pending interrupts */
393 struct list_head interrupts;
395 /** Queue of pending forgets */
396 struct fuse_forget_link forget_list_head;
397 struct fuse_forget_link *forget_list_tail;
399 /** Batching of FORGET requests (positive indicates FORGET batch) */
402 /** Flag indicating if connection is blocked. This will be
403 the case before the INIT reply is received, and if there
404 are too many outstading backgrounds requests */
407 /** waitq for blocked connection */
408 wait_queue_head_t blocked_waitq;
410 /** waitq for reserved requests */
411 wait_queue_head_t reserved_req_waitq;
413 /** The next unique request id */
416 /** Connection established, cleared on umount, connection
417 abort and device release */
420 /** Connection failed (version mismatch). Cannot race with
421 setting other bitfields since it is only set once in INIT
422 reply, before any other request, and never cleared */
423 unsigned conn_error:1;
425 /** Connection successful. Only set in INIT */
426 unsigned conn_init:1;
428 /** Do readpages asynchronously? Only set in INIT */
429 unsigned async_read:1;
431 /** Do not send separate SETATTR request before open(O_TRUNC) */
432 unsigned atomic_o_trunc:1;
434 /** Filesystem supports NFS exporting. Only set in INIT */
435 unsigned export_support:1;
437 /** Set if bdi is valid */
438 unsigned bdi_initialized:1;
441 * The following bitfields are only for optimization purposes
442 * and hence races in setting them will not cause malfunction
445 /** Is fsync not implemented by fs? */
448 /** Is fsyncdir not implemented by fs? */
449 unsigned no_fsyncdir:1;
451 /** Is flush not implemented by fs? */
454 /** Is setxattr not implemented by fs? */
455 unsigned no_setxattr:1;
457 /** Is getxattr not implemented by fs? */
458 unsigned no_getxattr:1;
460 /** Is listxattr not implemented by fs? */
461 unsigned no_listxattr:1;
463 /** Is removexattr not implemented by fs? */
464 unsigned no_removexattr:1;
466 /** Are posix file locking primitives not implemented by fs? */
469 /** Is access not implemented by fs? */
470 unsigned no_access:1;
472 /** Is create not implemented by fs? */
473 unsigned no_create:1;
475 /** Is interrupt not implemented by fs? */
476 unsigned no_interrupt:1;
478 /** Is bmap not implemented by fs? */
481 /** Is poll not implemented by fs? */
484 /** Do multi-page cached writes */
485 unsigned big_writes:1;
487 /** Don't apply umask to creation modes */
488 unsigned dont_mask:1;
490 /** Are BSD file locking primitives not implemented by fs? */
493 /** Is fallocate not implemented by fs? */
494 unsigned no_fallocate:1;
496 /** Use enhanced/automatic page cache invalidation. */
497 unsigned auto_inval_data:1;
499 /** Does the filesystem support readdir-plus? */
500 unsigned do_readdirplus:1;
502 /** The number of requests waiting for completion */
503 atomic_t num_waiting;
505 /** Negotiated minor version */
508 /** Backing dev info */
509 struct backing_dev_info bdi;
511 /** Entry on the fuse_conn_list */
512 struct list_head entry;
514 /** Device ID from super block */
517 /** Dentries in the control filesystem */
518 struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES];
520 /** number of dentries used in the above array */
523 /** O_ASYNC requests */
524 struct fasync_struct *fasync;
526 /** Key for lock owner ID scrambling */
529 /** Reserved request for the DESTROY message */
530 struct fuse_req *destroy_req;
532 /** Version counter for attribute changes */
535 /** Called on final put */
536 void (*release)(struct fuse_conn *);
538 /** Super block for this connection. */
539 struct super_block *sb;
541 /** Read/write semaphore to hold when accessing sb. */
542 struct rw_semaphore killsb;
545 static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
547 return sb->s_fs_info;
550 static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
552 return get_fuse_conn_super(inode->i_sb);
555 static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
557 return container_of(inode, struct fuse_inode, inode);
560 static inline u64 get_node_id(struct inode *inode)
562 return get_fuse_inode(inode)->nodeid;
565 /** Device operations */
566 extern const struct file_operations fuse_dev_operations;
568 extern const struct dentry_operations fuse_dentry_operations;
571 * Inode to nodeid comparison.
573 int fuse_inode_eq(struct inode *inode, void *_nodeidp);
576 * Get a filled in inode
578 struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
579 int generation, struct fuse_attr *attr,
580 u64 attr_valid, u64 attr_version);
582 int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
583 struct fuse_entry_out *outarg, struct inode **inode);
586 * Send FORGET command
588 void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
589 u64 nodeid, u64 nlookup);
591 struct fuse_forget_link *fuse_alloc_forget(void);
593 /* Used by READDIRPLUS */
594 void fuse_force_forget(struct file *file, u64 nodeid);
597 * Initialize READ or READDIR request
599 void fuse_read_fill(struct fuse_req *req, struct file *file,
600 loff_t pos, size_t count, int opcode);
603 * Send OPEN or OPENDIR request
605 int fuse_open_common(struct inode *inode, struct file *file, bool isdir);
607 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc);
608 struct fuse_file *fuse_file_get(struct fuse_file *ff);
609 void fuse_file_free(struct fuse_file *ff);
610 void fuse_finish_open(struct inode *inode, struct file *file);
612 void fuse_sync_release(struct fuse_file *ff, int flags);
615 * Send RELEASE or RELEASEDIR request
617 void fuse_release_common(struct file *file, int opcode);
620 * Send FSYNC or FSYNCDIR request
622 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
623 int datasync, int isdir);
628 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
629 struct fuse_notify_poll_wakeup_out *outarg);
632 * Initialize file operations on a regular file
634 void fuse_init_file_inode(struct inode *inode);
637 * Initialize inode operations on regular files and special files
639 void fuse_init_common(struct inode *inode);
642 * Initialize inode and file operations on a directory
644 void fuse_init_dir(struct inode *inode);
647 * Initialize inode operations on a symlink
649 void fuse_init_symlink(struct inode *inode);
652 * Change attributes of an inode
654 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
655 u64 attr_valid, u64 attr_version);
657 void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
661 * Initialize the client device
663 int fuse_dev_init(void);
666 * Cleanup the client device
668 void fuse_dev_cleanup(void);
670 int fuse_ctl_init(void);
671 void fuse_ctl_cleanup(void);
676 struct fuse_req *fuse_request_alloc(unsigned npages);
678 struct fuse_req *fuse_request_alloc_nofs(unsigned npages);
683 void fuse_request_free(struct fuse_req *req);
686 * Get a request, may fail with -ENOMEM,
687 * caller should specify # elements in req->pages[] explicitly
689 struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages);
692 * Get a request, may fail with -ENOMEM,
693 * useful for callers who doesn't use req->pages[]
695 static inline struct fuse_req *fuse_get_req_nopages(struct fuse_conn *fc)
697 return fuse_get_req(fc, 0);
701 * Gets a requests for a file operation, always succeeds
703 struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
707 * Decrement reference count of a request. If count goes to zero free
710 void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
713 * Send a request (synchronous)
715 void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req);
718 * Send a request in the background
720 void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req);
722 void fuse_request_send_background_locked(struct fuse_conn *fc,
723 struct fuse_req *req);
725 /* Abort all requests */
726 void fuse_abort_conn(struct fuse_conn *fc);
729 * Invalidate inode attributes
731 void fuse_invalidate_attr(struct inode *inode);
733 void fuse_invalidate_entry_cache(struct dentry *entry);
736 * Acquire reference to fuse_conn
738 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
740 void fuse_conn_kill(struct fuse_conn *fc);
743 * Initialize fuse_conn
745 void fuse_conn_init(struct fuse_conn *fc);
748 * Release reference to fuse_conn
750 void fuse_conn_put(struct fuse_conn *fc);
753 * Add connection to control filesystem
755 int fuse_ctl_add_conn(struct fuse_conn *fc);
758 * Remove connection from control filesystem
760 void fuse_ctl_remove_conn(struct fuse_conn *fc);
763 * Is file type valid?
765 int fuse_valid_type(int m);
768 * Is task allowed to perform filesystem operation?
770 int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task);
772 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id);
774 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
775 struct file *file, bool *refreshed);
777 void fuse_flush_writepages(struct inode *inode);
779 void fuse_set_nowrite(struct inode *inode);
780 void fuse_release_nowrite(struct inode *inode);
782 u64 fuse_get_attr_version(struct fuse_conn *fc);
785 * File-system tells the kernel to invalidate cache for the given node id.
787 int fuse_reverse_inval_inode(struct super_block *sb, u64 nodeid,
788 loff_t offset, loff_t len);
791 * File-system tells the kernel to invalidate parent attributes and
792 * the dentry matching parent/name.
794 * If the child_nodeid is non-zero and:
795 * - matches the inode number for the dentry matching parent/name,
796 * - is not a mount point
797 * - is a file or oan empty directory
798 * then the dentry is unhashed (d_delete()).
800 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
801 u64 child_nodeid, struct qstr *name);
803 int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
805 ssize_t fuse_direct_io(struct file *file, const char __user *buf,
806 size_t count, loff_t *ppos, int write);
807 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
809 long fuse_ioctl_common(struct file *file, unsigned int cmd,
810 unsigned long arg, unsigned int flags);
811 unsigned fuse_file_poll(struct file *file, poll_table *wait);
812 int fuse_dev_release(struct inode *inode, struct file *file);
814 void fuse_write_update_size(struct inode *inode, loff_t pos);
816 #endif /* _FS_FUSE_I_H */