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/wait.h>
12 #include <linux/list.h>
13 #include <linux/spinlock.h>
15 #include <linux/backing-dev.h>
16 #include <asm/semaphore.h>
18 /** Max number of pages that can be used in a single read request */
19 #define FUSE_MAX_PAGES_PER_REQ 32
21 /** It could be as large as PATH_MAX, but would that have any uses? */
22 #define FUSE_NAME_MAX 1024
24 /** If the FUSE_DEFAULT_PERMISSIONS flag is given, the filesystem
25 module will check permissions based on the file mode. Otherwise no
26 permission checking is done in the kernel */
27 #define FUSE_DEFAULT_PERMISSIONS (1 << 0)
29 /** If the FUSE_ALLOW_OTHER flag is given, then not only the user
30 doing the mount will be allowed to access the filesystem */
31 #define FUSE_ALLOW_OTHER (1 << 1)
39 /** Unique ID, which identifies the inode between userspace
43 /** Number of lookups on this inode */
46 /** The request used for sending the FORGET message */
47 struct fuse_req *forget_req;
49 /** Time in jiffies until the file attributes are valid */
53 /** FUSE specific file data */
55 /** Request reserved for flush and release */
56 struct fuse_req *release_req;
58 /** File handle used by userspace */
62 /** One input argument of a request */
68 /** The request input */
70 /** The request header */
71 struct fuse_in_header h;
73 /** True if the data for the last argument is in req->pages */
76 /** Number of arguments */
79 /** Array of arguments */
80 struct fuse_in_arg args[3];
83 /** One output argument of a request */
89 /** The request output */
91 /** Header returned from userspace */
92 struct fuse_out_header h;
95 * The following bitfields are not changed during the request
99 /** Last argument is variable length (can be shorter than
103 /** Last argument is a list of pages to copy data to */
106 /** Zero partially or not copied pages */
107 unsigned page_zeroing:1;
109 /** Number or arguments */
112 /** Array of arguments */
113 struct fuse_arg args[3];
116 /** The request state */
117 enum fuse_req_state {
128 * A request to the client
131 /** This can be on either pending processing or io lists in
133 struct list_head list;
135 /** Entry on the background list */
136 struct list_head bg_entry;
142 * The following bitfields are either set once before the
143 * request is queued or setting/clearing them is protected by
147 /** True if the request has reply */
150 /** The request was interrupted */
151 unsigned interrupted:1;
153 /** Request is sent in the background */
154 unsigned background:1;
156 /** Data is being copied to/from the request */
159 /** State of the request */
160 enum fuse_req_state state;
162 /** The request input */
165 /** The request output */
168 /** Used to wake up the task waiting for completion of request*/
169 wait_queue_head_t waitq;
171 /** Data for asynchronous requests */
173 struct fuse_forget_in forget_in;
174 struct fuse_release_in release_in;
175 struct fuse_init_in init_in;
176 struct fuse_init_out init_out;
177 struct fuse_read_in read_in;
181 struct page *pages[FUSE_MAX_PAGES_PER_REQ];
183 /** number of pages in vector */
186 /** offset of data on first page */
187 unsigned page_offset;
189 /** Inode used in the request */
192 /** Second inode used in the request (or NULL) */
193 struct inode *inode2;
195 /** File used in the request (or NULL) */
198 /** Request completion callback */
199 void (*end)(struct fuse_conn *, struct fuse_req *);
205 * This structure is created, when the filesystem is mounted, and is
206 * destroyed, when the client device is closed and the filesystem is
210 /** Lock protecting accessess to members of this structure */
213 /** The user id for this mount */
216 /** The group id for this mount */
219 /** The fuse mount flags for this mount */
222 /** Maximum read size */
225 /** Maximum write size */
228 /** Readers of the connection are waiting on this */
229 wait_queue_head_t waitq;
231 /** The list of pending requests */
232 struct list_head pending;
234 /** The list of requests being processed */
235 struct list_head processing;
237 /** The list of requests under I/O */
240 /** Requests put in the background (RELEASE or any other
241 interrupted request) */
242 struct list_head background;
244 /** RW semaphore for exclusion with fuse_put_super() */
245 struct rw_semaphore sbput_sem;
247 /** The next unique request id */
250 /** Mount is active */
253 /** Connection established, cleared on umount, connection
254 abort and device release */
257 /** Connection failed (version mismatch). Cannot race with
258 setting other bitfields since it is only set once in INIT
259 reply, before any other request, and never cleared */
260 unsigned conn_error : 1;
262 /** Do readpages asynchronously? Only set in INIT */
263 unsigned async_read : 1;
266 * The following bitfields are only for optimization purposes
267 * and hence races in setting them will not cause malfunction
270 /** Is fsync not implemented by fs? */
271 unsigned no_fsync : 1;
273 /** Is fsyncdir not implemented by fs? */
274 unsigned no_fsyncdir : 1;
276 /** Is flush not implemented by fs? */
277 unsigned no_flush : 1;
279 /** Is setxattr not implemented by fs? */
280 unsigned no_setxattr : 1;
282 /** Is getxattr not implemented by fs? */
283 unsigned no_getxattr : 1;
285 /** Is listxattr not implemented by fs? */
286 unsigned no_listxattr : 1;
288 /** Is removexattr not implemented by fs? */
289 unsigned no_removexattr : 1;
291 /** Is access not implemented by fs? */
292 unsigned no_access : 1;
294 /** Is create not implemented by fs? */
295 unsigned no_create : 1;
297 /** The number of requests waiting for completion */
298 atomic_t num_waiting;
300 /** Negotiated minor version */
303 /** Backing dev info */
304 struct backing_dev_info bdi;
309 /** O_ASYNC requests */
310 struct fasync_struct *fasync;
313 static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
315 return sb->s_fs_info;
318 static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
320 return get_fuse_conn_super(inode->i_sb);
323 static inline struct fuse_conn *get_fuse_conn_kobj(struct kobject *obj)
325 return container_of(obj, struct fuse_conn, kobj);
328 static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
330 return container_of(inode, struct fuse_inode, inode);
333 static inline u64 get_node_id(struct inode *inode)
335 return get_fuse_inode(inode)->nodeid;
338 /** Device operations */
339 extern const struct file_operations fuse_dev_operations;
342 * Get a filled in inode
344 struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
345 int generation, struct fuse_attr *attr);
348 * Send FORGET command
350 void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
351 unsigned long nodeid, u64 nlookup);
354 * Initialize READ or READDIR request
356 void fuse_read_fill(struct fuse_req *req, struct file *file,
357 struct inode *inode, loff_t pos, size_t count, int opcode);
360 * Send OPEN or OPENDIR request
362 int fuse_open_common(struct inode *inode, struct file *file, int isdir);
364 struct fuse_file *fuse_file_alloc(void);
365 void fuse_file_free(struct fuse_file *ff);
366 void fuse_finish_open(struct inode *inode, struct file *file,
367 struct fuse_file *ff, struct fuse_open_out *outarg);
370 * Send a RELEASE request
372 void fuse_send_release(struct fuse_conn *fc, struct fuse_file *ff,
373 u64 nodeid, struct inode *inode, int flags, int isdir);
376 * Send RELEASE or RELEASEDIR request
378 int fuse_release_common(struct inode *inode, struct file *file, int isdir);
381 * Send FSYNC or FSYNCDIR request
383 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
387 * Initialize file operations on a regular file
389 void fuse_init_file_inode(struct inode *inode);
392 * Initialize inode operations on regular files and special files
394 void fuse_init_common(struct inode *inode);
397 * Initialize inode and file operations on a directory
399 void fuse_init_dir(struct inode *inode);
402 * Initialize inode operations on a symlink
404 void fuse_init_symlink(struct inode *inode);
407 * Change attributes of an inode
409 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr);
412 * Initialize the client device
414 int fuse_dev_init(void);
417 * Cleanup the client device
419 void fuse_dev_cleanup(void);
424 struct fuse_req *fuse_request_alloc(void);
429 void fuse_request_free(struct fuse_req *req);
432 * Reinitialize a request, the preallocated flag is left unmodified
434 void fuse_reset_request(struct fuse_req *req);
437 * Reserve a preallocated request
439 struct fuse_req *fuse_get_req(struct fuse_conn *fc);
442 * Decrement reference count of a request. If count goes to zero free
445 void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
448 * Send a request (synchronous)
450 void request_send(struct fuse_conn *fc, struct fuse_req *req);
453 * Send a request with no reply
455 void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
458 * Send a request in the background
460 void request_send_background(struct fuse_conn *fc, struct fuse_req *req);
463 * Release inodes and file associated with background request
465 void fuse_release_background(struct fuse_conn *fc, struct fuse_req *req);
467 /* Abort all requests */
468 void fuse_abort_conn(struct fuse_conn *fc);
471 * Get the attributes of a file
473 int fuse_do_getattr(struct inode *inode);
476 * Invalidate inode attributes
478 void fuse_invalidate_attr(struct inode *inode);