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>
19 /** Max number of pages that can be used in a single read request */
20 #define FUSE_MAX_PAGES_PER_REQ 32
22 /** Maximum number of outstanding background requests */
23 #define FUSE_MAX_BACKGROUND 12
25 /** Congestion starts at 75% of maximum */
26 #define FUSE_CONGESTION_THRESHOLD (FUSE_MAX_BACKGROUND * 75 / 100)
28 /** It could be as large as PATH_MAX, but would that have any uses? */
29 #define FUSE_NAME_MAX 1024
31 /** Number of dentries for each connection in the control filesystem */
32 #define FUSE_CTL_NUM_DENTRIES 3
34 /** If the FUSE_DEFAULT_PERMISSIONS flag is given, the filesystem
35 module will check permissions based on the file mode. Otherwise no
36 permission checking is done in the kernel */
37 #define FUSE_DEFAULT_PERMISSIONS (1 << 0)
39 /** If the FUSE_ALLOW_OTHER flag is given, then not only the user
40 doing the mount will be allowed to access the filesystem */
41 #define FUSE_ALLOW_OTHER (1 << 1)
43 /** List of active connections */
44 extern struct list_head fuse_conn_list;
46 /** Global mutex protecting fuse_conn_list and the control filesystem */
47 extern struct mutex fuse_mutex;
54 /** Unique ID, which identifies the inode between userspace
58 /** Number of lookups on this inode */
61 /** The request used for sending the FORGET message */
62 struct fuse_req *forget_req;
64 /** Time in jiffies until the file attributes are valid */
67 /** The sticky bit in inode->i_mode may have been removed, so
68 preserve the original mode */
71 /** Version of last attribute change */
74 /** Files usable in writepage. Protected by fc->lock */
75 struct list_head write_files;
78 /** FUSE specific file data */
80 /** Request reserved for flush and release */
81 struct fuse_req *reserved_req;
83 /** File handle used by userspace */
89 /** Entry on inode's write_files list */
90 struct list_head write_entry;
93 /** One input argument of a request */
99 /** The request input */
101 /** The request header */
102 struct fuse_in_header h;
104 /** True if the data for the last argument is in req->pages */
107 /** Number of arguments */
110 /** Array of arguments */
111 struct fuse_in_arg args[3];
114 /** One output argument of a request */
120 /** The request output */
122 /** Header returned from userspace */
123 struct fuse_out_header h;
126 * The following bitfields are not changed during the request
130 /** Last argument is variable length (can be shorter than
134 /** Last argument is a list of pages to copy data to */
137 /** Zero partially or not copied pages */
138 unsigned page_zeroing:1;
140 /** Number or arguments */
143 /** Array of arguments */
144 struct fuse_arg args[3];
147 /** The request state */
148 enum fuse_req_state {
160 * A request to the client
163 /** This can be on either pending processing or io lists in
165 struct list_head list;
167 /** Entry on the interrupts list */
168 struct list_head intr_entry;
173 /** Unique ID for the interrupt request */
177 * The following bitfields are either set once before the
178 * request is queued or setting/clearing them is protected by
182 /** True if the request has reply */
185 /** Force sending of the request even if interrupted */
188 /** The request was aborted */
191 /** Request is sent in the background */
192 unsigned background:1;
194 /** The request has been interrupted */
195 unsigned interrupted:1;
197 /** Data is being copied to/from the request */
200 /** Request is counted as "waiting" */
203 /** State of the request */
204 enum fuse_req_state state;
206 /** The request input */
209 /** The request output */
212 /** Used to wake up the task waiting for completion of request*/
213 wait_queue_head_t waitq;
215 /** Data for asynchronous requests */
217 struct fuse_forget_in forget_in;
218 struct fuse_release_in release_in;
219 struct fuse_init_in init_in;
220 struct fuse_init_out init_out;
221 struct fuse_read_in read_in;
222 struct fuse_lk_in lk_in;
226 struct page *pages[FUSE_MAX_PAGES_PER_REQ];
228 /** number of pages in vector */
231 /** offset of data on first page */
232 unsigned page_offset;
234 /** File used in the request (or NULL) */
235 struct fuse_file *ff;
237 /** vfsmount used in release */
238 struct vfsmount *vfsmount;
240 /** dentry used in release */
241 struct dentry *dentry;
243 /** Request completion callback */
244 void (*end)(struct fuse_conn *, struct fuse_req *);
246 /** Request is stolen from fuse_file->reserved_req */
247 struct file *stolen_file;
253 * This structure is created, when the filesystem is mounted, and is
254 * destroyed, when the client device is closed and the filesystem is
258 /** Lock protecting accessess to members of this structure */
261 /** Mutex protecting against directory alias creation */
262 struct mutex inst_mutex;
267 /** The user id for this mount */
270 /** The group id for this mount */
273 /** The fuse mount flags for this mount */
276 /** Maximum read size */
279 /** Maximum write size */
282 /** Readers of the connection are waiting on this */
283 wait_queue_head_t waitq;
285 /** The list of pending requests */
286 struct list_head pending;
288 /** The list of requests being processed */
289 struct list_head processing;
291 /** The list of requests under I/O */
294 /** Number of requests currently in the background */
295 unsigned num_background;
297 /** Pending interrupts */
298 struct list_head interrupts;
300 /** Flag indicating if connection is blocked. This will be
301 the case before the INIT reply is received, and if there
302 are too many outstading backgrounds requests */
305 /** waitq for blocked connection */
306 wait_queue_head_t blocked_waitq;
308 /** waitq for reserved requests */
309 wait_queue_head_t reserved_req_waitq;
311 /** The next unique request id */
314 /** Connection established, cleared on umount, connection
315 abort and device release */
318 /** Connection failed (version mismatch). Cannot race with
319 setting other bitfields since it is only set once in INIT
320 reply, before any other request, and never cleared */
321 unsigned conn_error : 1;
323 /** Connection successful. Only set in INIT */
324 unsigned conn_init : 1;
326 /** Do readpages asynchronously? Only set in INIT */
327 unsigned async_read : 1;
329 /** Do not send separate SETATTR request before open(O_TRUNC) */
330 unsigned atomic_o_trunc : 1;
333 * The following bitfields are only for optimization purposes
334 * and hence races in setting them will not cause malfunction
337 /** Is fsync not implemented by fs? */
338 unsigned no_fsync : 1;
340 /** Is fsyncdir not implemented by fs? */
341 unsigned no_fsyncdir : 1;
343 /** Is flush not implemented by fs? */
344 unsigned no_flush : 1;
346 /** Is setxattr not implemented by fs? */
347 unsigned no_setxattr : 1;
349 /** Is getxattr not implemented by fs? */
350 unsigned no_getxattr : 1;
352 /** Is listxattr not implemented by fs? */
353 unsigned no_listxattr : 1;
355 /** Is removexattr not implemented by fs? */
356 unsigned no_removexattr : 1;
358 /** Are file locking primitives not implemented by fs? */
359 unsigned no_lock : 1;
361 /** Is access not implemented by fs? */
362 unsigned no_access : 1;
364 /** Is create not implemented by fs? */
365 unsigned no_create : 1;
367 /** Is interrupt not implemented by fs? */
368 unsigned no_interrupt : 1;
370 /** Is bmap not implemented by fs? */
371 unsigned no_bmap : 1;
373 /** The number of requests waiting for completion */
374 atomic_t num_waiting;
376 /** Negotiated minor version */
379 /** Backing dev info */
380 struct backing_dev_info bdi;
382 /** Entry on the fuse_conn_list */
383 struct list_head entry;
388 /** Dentries in the control filesystem */
389 struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES];
391 /** number of dentries used in the above array */
394 /** O_ASYNC requests */
395 struct fasync_struct *fasync;
397 /** Key for lock owner ID scrambling */
400 /** Reserved request for the DESTROY message */
401 struct fuse_req *destroy_req;
403 /** Version counter for attribute changes */
407 static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
409 return sb->s_fs_info;
412 static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
414 return get_fuse_conn_super(inode->i_sb);
417 static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
419 return container_of(inode, struct fuse_inode, inode);
422 static inline u64 get_node_id(struct inode *inode)
424 return get_fuse_inode(inode)->nodeid;
427 /** Device operations */
428 extern const struct file_operations fuse_dev_operations;
431 * Get a filled in inode
433 struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
434 int generation, struct fuse_attr *attr,
435 u64 attr_valid, u64 attr_version);
438 * Send FORGET command
440 void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
441 unsigned long nodeid, u64 nlookup);
444 * Initialize READ or READDIR request
446 void fuse_read_fill(struct fuse_req *req, struct fuse_file *ff,
447 struct inode *inode, loff_t pos, size_t count, int opcode);
450 * Send OPEN or OPENDIR request
452 int fuse_open_common(struct inode *inode, struct file *file, int isdir);
454 struct fuse_file *fuse_file_alloc(void);
455 void fuse_file_free(struct fuse_file *ff);
456 void fuse_finish_open(struct inode *inode, struct file *file,
457 struct fuse_file *ff, struct fuse_open_out *outarg);
459 /** Fill in ff->reserved_req with a RELEASE request */
460 void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode);
463 * Send RELEASE or RELEASEDIR request
465 int fuse_release_common(struct inode *inode, struct file *file, int isdir);
468 * Send FSYNC or FSYNCDIR request
470 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
474 * Initialize file operations on a regular file
476 void fuse_init_file_inode(struct inode *inode);
479 * Initialize inode operations on regular files and special files
481 void fuse_init_common(struct inode *inode);
484 * Initialize inode and file operations on a directory
486 void fuse_init_dir(struct inode *inode);
489 * Initialize inode operations on a symlink
491 void fuse_init_symlink(struct inode *inode);
494 * Change attributes of an inode
496 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
497 u64 attr_valid, u64 attr_version);
500 * Initialize the client device
502 int fuse_dev_init(void);
505 * Cleanup the client device
507 void fuse_dev_cleanup(void);
509 int fuse_ctl_init(void);
510 void fuse_ctl_cleanup(void);
515 struct fuse_req *fuse_request_alloc(void);
520 void fuse_request_free(struct fuse_req *req);
523 * Get a request, may fail with -ENOMEM
525 struct fuse_req *fuse_get_req(struct fuse_conn *fc);
528 * Gets a requests for a file operation, always succeeds
530 struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file);
533 * Decrement reference count of a request. If count goes to zero free
536 void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
539 * Send a request (synchronous)
541 void request_send(struct fuse_conn *fc, struct fuse_req *req);
544 * Send a request with no reply
546 void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
549 * Send a request in the background
551 void request_send_background(struct fuse_conn *fc, struct fuse_req *req);
553 /* Abort all requests */
554 void fuse_abort_conn(struct fuse_conn *fc);
557 * Invalidate inode attributes
559 void fuse_invalidate_attr(struct inode *inode);
562 * Acquire reference to fuse_conn
564 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
567 * Release reference to fuse_conn
569 void fuse_conn_put(struct fuse_conn *fc);
572 * Add connection to control filesystem
574 int fuse_ctl_add_conn(struct fuse_conn *fc);
577 * Remove connection from control filesystem
579 void fuse_ctl_remove_conn(struct fuse_conn *fc);
582 * Is file type valid?
584 int fuse_valid_type(int m);
587 * Is task allowed to perform filesystem operation?
589 int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task);