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 10
25 /** It could be as large as PATH_MAX, but would that have any uses? */
26 #define FUSE_NAME_MAX 1024
28 /** Number of dentries for each connection in the control filesystem */
29 #define FUSE_CTL_NUM_DENTRIES 3
31 /** If the FUSE_DEFAULT_PERMISSIONS flag is given, the filesystem
32 module will check permissions based on the file mode. Otherwise no
33 permission checking is done in the kernel */
34 #define FUSE_DEFAULT_PERMISSIONS (1 << 0)
36 /** If the FUSE_ALLOW_OTHER flag is given, then not only the user
37 doing the mount will be allowed to access the filesystem */
38 #define FUSE_ALLOW_OTHER (1 << 1)
40 /** List of active connections */
41 extern struct list_head fuse_conn_list;
43 /** Global mutex protecting fuse_conn_list and the control filesystem */
44 extern struct mutex fuse_mutex;
51 /** Unique ID, which identifies the inode between userspace
55 /** Number of lookups on this inode */
58 /** The request used for sending the FORGET message */
59 struct fuse_req *forget_req;
61 /** Time in jiffies until the file attributes are valid */
65 /** FUSE specific file data */
67 /** Request reserved for flush and release */
68 struct fuse_req *release_req;
70 /** File handle used by userspace */
74 /** One input argument of a request */
80 /** The request input */
82 /** The request header */
83 struct fuse_in_header h;
85 /** True if the data for the last argument is in req->pages */
88 /** Number of arguments */
91 /** Array of arguments */
92 struct fuse_in_arg args[3];
95 /** One output argument of a request */
101 /** The request output */
103 /** Header returned from userspace */
104 struct fuse_out_header h;
107 * The following bitfields are not changed during the request
111 /** Last argument is variable length (can be shorter than
115 /** Last argument is a list of pages to copy data to */
118 /** Zero partially or not copied pages */
119 unsigned page_zeroing:1;
121 /** Number or arguments */
124 /** Array of arguments */
125 struct fuse_arg args[3];
128 /** The request state */
129 enum fuse_req_state {
140 * A request to the client
143 /** This can be on either pending processing or io lists in
145 struct list_head list;
151 * The following bitfields are either set once before the
152 * request is queued or setting/clearing them is protected by
156 /** True if the request has reply */
159 /** Force sending of the request even if interrupted */
162 /** The request was interrupted */
163 unsigned interrupted:1;
165 /** Request is sent in the background */
166 unsigned background:1;
168 /** Data is being copied to/from the request */
171 /** Request is counted as "waiting" */
174 /** State of the request */
175 enum fuse_req_state state;
177 /** The request input */
180 /** The request output */
183 /** Used to wake up the task waiting for completion of request*/
184 wait_queue_head_t waitq;
186 /** Data for asynchronous requests */
188 struct fuse_forget_in forget_in;
189 struct fuse_release_in release_in;
190 struct fuse_init_in init_in;
191 struct fuse_init_out init_out;
192 struct fuse_read_in read_in;
193 struct fuse_lk_in lk_in;
197 struct page *pages[FUSE_MAX_PAGES_PER_REQ];
199 /** number of pages in vector */
202 /** offset of data on first page */
203 unsigned page_offset;
205 /** File used in the request (or NULL) */
208 /** vfsmount used in release */
209 struct vfsmount *vfsmount;
211 /** dentry used in release */
212 struct dentry *dentry;
214 /** Request completion callback */
215 void (*end)(struct fuse_conn *, struct fuse_req *);
221 * This structure is created, when the filesystem is mounted, and is
222 * destroyed, when the client device is closed and the filesystem is
226 /** Lock protecting accessess to members of this structure */
232 /** The user id for this mount */
235 /** The group id for this mount */
238 /** The fuse mount flags for this mount */
241 /** Maximum read size */
244 /** Maximum write size */
247 /** Readers of the connection are waiting on this */
248 wait_queue_head_t waitq;
250 /** The list of pending requests */
251 struct list_head pending;
253 /** The list of requests being processed */
254 struct list_head processing;
256 /** The list of requests under I/O */
259 /** Number of requests currently in the background */
260 unsigned num_background;
262 /** Flag indicating if connection is blocked. This will be
263 the case before the INIT reply is received, and if there
264 are too many outstading backgrounds requests */
267 /** waitq for blocked connection */
268 wait_queue_head_t blocked_waitq;
270 /** The next unique request id */
273 /** Connection established, cleared on umount, connection
274 abort and device release */
277 /** Connection failed (version mismatch). Cannot race with
278 setting other bitfields since it is only set once in INIT
279 reply, before any other request, and never cleared */
280 unsigned conn_error : 1;
282 /** Do readpages asynchronously? Only set in INIT */
283 unsigned async_read : 1;
286 * The following bitfields are only for optimization purposes
287 * and hence races in setting them will not cause malfunction
290 /** Is fsync not implemented by fs? */
291 unsigned no_fsync : 1;
293 /** Is fsyncdir not implemented by fs? */
294 unsigned no_fsyncdir : 1;
296 /** Is flush not implemented by fs? */
297 unsigned no_flush : 1;
299 /** Is setxattr not implemented by fs? */
300 unsigned no_setxattr : 1;
302 /** Is getxattr not implemented by fs? */
303 unsigned no_getxattr : 1;
305 /** Is listxattr not implemented by fs? */
306 unsigned no_listxattr : 1;
308 /** Is removexattr not implemented by fs? */
309 unsigned no_removexattr : 1;
311 /** Are file locking primitives not implemented by fs? */
312 unsigned no_lock : 1;
314 /** Is access not implemented by fs? */
315 unsigned no_access : 1;
317 /** Is create not implemented by fs? */
318 unsigned no_create : 1;
320 /** The number of requests waiting for completion */
321 atomic_t num_waiting;
323 /** Negotiated minor version */
326 /** Backing dev info */
327 struct backing_dev_info bdi;
329 /** Entry on the fuse_conn_list */
330 struct list_head entry;
335 /** Dentries in the control filesystem */
336 struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES];
338 /** number of dentries used in the above array */
341 /** O_ASYNC requests */
342 struct fasync_struct *fasync;
345 static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
347 return sb->s_fs_info;
350 static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
352 return get_fuse_conn_super(inode->i_sb);
355 static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
357 return container_of(inode, struct fuse_inode, inode);
360 static inline u64 get_node_id(struct inode *inode)
362 return get_fuse_inode(inode)->nodeid;
365 /** Device operations */
366 extern const struct file_operations fuse_dev_operations;
369 * Get a filled in inode
371 struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
372 int generation, struct fuse_attr *attr);
375 * Send FORGET command
377 void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
378 unsigned long nodeid, u64 nlookup);
381 * Initialize READ or READDIR request
383 void fuse_read_fill(struct fuse_req *req, struct file *file,
384 struct inode *inode, loff_t pos, size_t count, int opcode);
387 * Send OPEN or OPENDIR request
389 int fuse_open_common(struct inode *inode, struct file *file, int isdir);
391 struct fuse_file *fuse_file_alloc(void);
392 void fuse_file_free(struct fuse_file *ff);
393 void fuse_finish_open(struct inode *inode, struct file *file,
394 struct fuse_file *ff, struct fuse_open_out *outarg);
397 struct fuse_req *fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags,
400 * Send RELEASE or RELEASEDIR request
402 int fuse_release_common(struct inode *inode, struct file *file, int isdir);
405 * Send FSYNC or FSYNCDIR request
407 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
411 * Initialize file operations on a regular file
413 void fuse_init_file_inode(struct inode *inode);
416 * Initialize inode operations on regular files and special files
418 void fuse_init_common(struct inode *inode);
421 * Initialize inode and file operations on a directory
423 void fuse_init_dir(struct inode *inode);
426 * Initialize inode operations on a symlink
428 void fuse_init_symlink(struct inode *inode);
431 * Change attributes of an inode
433 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr);
436 * Initialize the client device
438 int fuse_dev_init(void);
441 * Cleanup the client device
443 void fuse_dev_cleanup(void);
445 int fuse_ctl_init(void);
446 void fuse_ctl_cleanup(void);
451 struct fuse_req *fuse_request_alloc(void);
456 void fuse_request_free(struct fuse_req *req);
459 * Reserve a preallocated request
461 struct fuse_req *fuse_get_req(struct fuse_conn *fc);
464 * Decrement reference count of a request. If count goes to zero free
467 void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
470 * Send a request (synchronous)
472 void request_send(struct fuse_conn *fc, struct fuse_req *req);
475 * Send a request with no reply
477 void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
480 * Send a request in the background
482 void request_send_background(struct fuse_conn *fc, struct fuse_req *req);
484 /* Abort all requests */
485 void fuse_abort_conn(struct fuse_conn *fc);
488 * Get the attributes of a file
490 int fuse_do_getattr(struct inode *inode);
493 * Invalidate inode attributes
495 void fuse_invalidate_attr(struct inode *inode);
498 * Acquire reference to fuse_conn
500 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
503 * Release reference to fuse_conn
505 void fuse_conn_put(struct fuse_conn *fc);
508 * Add connection to control filesystem
510 int fuse_ctl_add_conn(struct fuse_conn *fc);
513 * Remove connection from control filesystem
515 void fuse_ctl_remove_conn(struct fuse_conn *fc);