2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
9 #include <linux/fuse.h>
11 #include <linux/mount.h>
12 #include <linux/wait.h>
13 #include <linux/list.h>
14 #include <linux/spinlock.h>
16 #include <linux/backing-dev.h>
17 #include <linux/mutex.h>
18 #include <linux/rwsem.h>
20 /** Max number of pages that can be used in a single read request */
21 #define FUSE_MAX_PAGES_PER_REQ 32
23 /** Maximum number of outstanding background requests */
24 #define FUSE_MAX_BACKGROUND 12
26 /** Congestion starts at 75% of maximum */
27 #define FUSE_CONGESTION_THRESHOLD (FUSE_MAX_BACKGROUND * 75 / 100)
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 3
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 /** List of active connections */
48 extern struct list_head fuse_conn_list;
50 /** Global mutex protecting fuse_conn_list and the control filesystem */
51 extern struct mutex fuse_mutex;
58 /** Unique ID, which identifies the inode between userspace
62 /** Number of lookups on this inode */
65 /** The request used for sending the FORGET message */
66 struct fuse_req *forget_req;
68 /** Time in jiffies until the file attributes are valid */
71 /** The sticky bit in inode->i_mode may have been removed, so
72 preserve the original mode */
75 /** Version of last attribute change */
78 /** Files usable in writepage. Protected by fc->lock */
79 struct list_head write_files;
81 /** Writepages pending on truncate or fsync */
82 struct list_head queued_writes;
84 /** Number of sent writes, a negative bias (FUSE_NOWRITE)
85 * means more writes are blocked */
88 /** Waitq for writepage completion */
89 wait_queue_head_t page_waitq;
91 /** List of writepage requestst (pending or sent) */
92 struct list_head writepages;
95 /** FUSE specific file data */
97 /** Request reserved for flush and release */
98 struct fuse_req *reserved_req;
100 /** File handle used by userspace */
106 /** Entry on inode's write_files list */
107 struct list_head write_entry;
110 /** One input argument of a request */
116 /** The request input */
118 /** The request header */
119 struct fuse_in_header h;
121 /** True if the data for the last argument is in req->pages */
124 /** Number of arguments */
127 /** Array of arguments */
128 struct fuse_in_arg args[3];
131 /** One output argument of a request */
137 /** The request output */
139 /** Header returned from userspace */
140 struct fuse_out_header h;
143 * The following bitfields are not changed during the request
147 /** Last argument is variable length (can be shorter than
151 /** Last argument is a list of pages to copy data to */
154 /** Zero partially or not copied pages */
155 unsigned page_zeroing:1;
157 /** Number or arguments */
160 /** Array of arguments */
161 struct fuse_arg args[3];
164 /** The request state */
165 enum fuse_req_state {
177 * A request to the client
180 /** This can be on either pending processing or io lists in
182 struct list_head list;
184 /** Entry on the interrupts list */
185 struct list_head intr_entry;
190 /** Unique ID for the interrupt request */
194 * The following bitfields are either set once before the
195 * request is queued or setting/clearing them is protected by
199 /** True if the request has reply */
202 /** Force sending of the request even if interrupted */
205 /** The request was aborted */
208 /** Request is sent in the background */
209 unsigned background:1;
211 /** The request has been interrupted */
212 unsigned interrupted:1;
214 /** Data is being copied to/from the request */
217 /** Request is counted as "waiting" */
220 /** State of the request */
221 enum fuse_req_state state;
223 /** The request input */
226 /** The request output */
229 /** Used to wake up the task waiting for completion of request*/
230 wait_queue_head_t waitq;
232 /** Data for asynchronous requests */
234 struct fuse_forget_in forget_in;
236 struct fuse_release_in in;
237 struct vfsmount *vfsmount;
238 struct dentry *dentry;
240 struct fuse_init_in init_in;
241 struct fuse_init_out init_out;
243 struct fuse_read_in in;
247 struct fuse_write_in in;
248 struct fuse_write_out out;
250 struct fuse_lk_in lk_in;
254 struct page *pages[FUSE_MAX_PAGES_PER_REQ];
256 /** number of pages in vector */
259 /** offset of data on first page */
260 unsigned page_offset;
262 /** File used in the request (or NULL) */
263 struct fuse_file *ff;
265 /** Inode used in the request or NULL */
268 /** Link on fi->writepages */
269 struct list_head writepages_entry;
271 /** Request completion callback */
272 void (*end)(struct fuse_conn *, struct fuse_req *);
274 /** Request is stolen from fuse_file->reserved_req */
275 struct file *stolen_file;
281 * This structure is created, when the filesystem is mounted, and is
282 * destroyed, when the client device is closed and the filesystem is
286 /** Lock protecting accessess to members of this structure */
289 /** Mutex protecting against directory alias creation */
290 struct mutex inst_mutex;
295 /** The user id for this mount */
298 /** The group id for this mount */
301 /** The fuse mount flags for this mount */
304 /** Maximum read size */
307 /** Maximum write size */
310 /** Readers of the connection are waiting on this */
311 wait_queue_head_t waitq;
313 /** The list of pending requests */
314 struct list_head pending;
316 /** The list of requests being processed */
317 struct list_head processing;
319 /** The list of requests under I/O */
322 /** Number of requests currently in the background */
323 unsigned num_background;
325 /** Number of background requests currently queued for userspace */
326 unsigned active_background;
328 /** The list of background requests set aside for later queuing */
329 struct list_head bg_queue;
331 /** Pending interrupts */
332 struct list_head interrupts;
334 /** Flag indicating if connection is blocked. This will be
335 the case before the INIT reply is received, and if there
336 are too many outstading backgrounds requests */
339 /** waitq for blocked connection */
340 wait_queue_head_t blocked_waitq;
342 /** waitq for reserved requests */
343 wait_queue_head_t reserved_req_waitq;
345 /** The next unique request id */
348 /** Connection established, cleared on umount, connection
349 abort and device release */
352 /** Connection failed (version mismatch). Cannot race with
353 setting other bitfields since it is only set once in INIT
354 reply, before any other request, and never cleared */
355 unsigned conn_error : 1;
357 /** Connection successful. Only set in INIT */
358 unsigned conn_init : 1;
360 /** Do readpages asynchronously? Only set in INIT */
361 unsigned async_read : 1;
363 /** Do not send separate SETATTR request before open(O_TRUNC) */
364 unsigned atomic_o_trunc : 1;
367 * The following bitfields are only for optimization purposes
368 * and hence races in setting them will not cause malfunction
371 /** Is fsync not implemented by fs? */
372 unsigned no_fsync : 1;
374 /** Is fsyncdir not implemented by fs? */
375 unsigned no_fsyncdir : 1;
377 /** Is flush not implemented by fs? */
378 unsigned no_flush : 1;
380 /** Is setxattr not implemented by fs? */
381 unsigned no_setxattr : 1;
383 /** Is getxattr not implemented by fs? */
384 unsigned no_getxattr : 1;
386 /** Is listxattr not implemented by fs? */
387 unsigned no_listxattr : 1;
389 /** Is removexattr not implemented by fs? */
390 unsigned no_removexattr : 1;
392 /** Are file locking primitives not implemented by fs? */
393 unsigned no_lock : 1;
395 /** Is access not implemented by fs? */
396 unsigned no_access : 1;
398 /** Is create not implemented by fs? */
399 unsigned no_create : 1;
401 /** Is interrupt not implemented by fs? */
402 unsigned no_interrupt : 1;
404 /** Is bmap not implemented by fs? */
405 unsigned no_bmap : 1;
407 /** The number of requests waiting for completion */
408 atomic_t num_waiting;
410 /** Negotiated minor version */
413 /** Backing dev info */
414 struct backing_dev_info bdi;
416 /** Entry on the fuse_conn_list */
417 struct list_head entry;
419 /** Device ID from super block */
422 /** Dentries in the control filesystem */
423 struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES];
425 /** number of dentries used in the above array */
428 /** O_ASYNC requests */
429 struct fasync_struct *fasync;
431 /** Key for lock owner ID scrambling */
434 /** Reserved request for the DESTROY message */
435 struct fuse_req *destroy_req;
437 /** Version counter for attribute changes */
441 static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
443 return sb->s_fs_info;
446 static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
448 return get_fuse_conn_super(inode->i_sb);
451 static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
453 return container_of(inode, struct fuse_inode, inode);
456 static inline u64 get_node_id(struct inode *inode)
458 return get_fuse_inode(inode)->nodeid;
461 /** Device operations */
462 extern const struct file_operations fuse_dev_operations;
465 * Get a filled in inode
467 struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
468 int generation, struct fuse_attr *attr,
469 u64 attr_valid, u64 attr_version);
472 * Send FORGET command
474 void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
475 u64 nodeid, u64 nlookup);
478 * Initialize READ or READDIR request
480 void fuse_read_fill(struct fuse_req *req, struct file *file,
481 struct inode *inode, loff_t pos, size_t count, int opcode);
484 * Send OPEN or OPENDIR request
486 int fuse_open_common(struct inode *inode, struct file *file, int isdir);
488 struct fuse_file *fuse_file_alloc(void);
489 void fuse_file_free(struct fuse_file *ff);
490 void fuse_finish_open(struct inode *inode, struct file *file,
491 struct fuse_file *ff, struct fuse_open_out *outarg);
493 /** Fill in ff->reserved_req with a RELEASE request */
494 void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode);
497 * Send RELEASE or RELEASEDIR request
499 int fuse_release_common(struct inode *inode, struct file *file, int isdir);
502 * Send FSYNC or FSYNCDIR request
504 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
508 * Initialize file operations on a regular file
510 void fuse_init_file_inode(struct inode *inode);
513 * Initialize inode operations on regular files and special files
515 void fuse_init_common(struct inode *inode);
518 * Initialize inode and file operations on a directory
520 void fuse_init_dir(struct inode *inode);
523 * Initialize inode operations on a symlink
525 void fuse_init_symlink(struct inode *inode);
528 * Change attributes of an inode
530 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
531 u64 attr_valid, u64 attr_version);
533 void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
536 void fuse_truncate(struct address_space *mapping, loff_t offset);
539 * Initialize the client device
541 int fuse_dev_init(void);
544 * Cleanup the client device
546 void fuse_dev_cleanup(void);
548 int fuse_ctl_init(void);
549 void fuse_ctl_cleanup(void);
554 struct fuse_req *fuse_request_alloc(void);
556 struct fuse_req *fuse_request_alloc_nofs(void);
561 void fuse_request_free(struct fuse_req *req);
564 * Get a request, may fail with -ENOMEM
566 struct fuse_req *fuse_get_req(struct fuse_conn *fc);
569 * Gets a requests for a file operation, always succeeds
571 struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file);
574 * Decrement reference count of a request. If count goes to zero free
577 void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
580 * Send a request (synchronous)
582 void request_send(struct fuse_conn *fc, struct fuse_req *req);
585 * Send a request with no reply
587 void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
590 * Send a request in the background
592 void request_send_background(struct fuse_conn *fc, struct fuse_req *req);
594 void request_send_background_locked(struct fuse_conn *fc, struct fuse_req *req);
596 /* Abort all requests */
597 void fuse_abort_conn(struct fuse_conn *fc);
600 * Invalidate inode attributes
602 void fuse_invalidate_attr(struct inode *inode);
605 * Acquire reference to fuse_conn
607 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
610 * Release reference to fuse_conn
612 void fuse_conn_put(struct fuse_conn *fc);
615 * Add connection to control filesystem
617 int fuse_ctl_add_conn(struct fuse_conn *fc);
620 * Remove connection from control filesystem
622 void fuse_ctl_remove_conn(struct fuse_conn *fc);
625 * Is file type valid?
627 int fuse_valid_type(int m);
630 * Is task allowed to perform filesystem operation?
632 int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task);
634 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id);
636 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
637 struct file *file, bool *refreshed);
639 void fuse_flush_writepages(struct inode *inode);
641 void fuse_set_nowrite(struct inode *inode);
642 void fuse_release_nowrite(struct inode *inode);
644 u64 fuse_get_attr_version(struct fuse_conn *fc);