2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
10 #include <linux/fuse.h>
12 #include "fuse_kernel.h"
13 #include <linux/version.h>
14 #include <linux/utsname.h>
16 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) && LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
17 #error Kernel version 2.5.* not supported
20 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
22 # if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,6)
23 # define KERNEL_2_6_6_PLUS
25 # if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,8)
26 # define KERNEL_2_6_8_PLUS
28 # if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,9)
29 # define KERNEL_2_6_9_PLUS
31 # if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10)
32 # define KERNEL_2_6_10_PLUS
34 # if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
35 # define KERNEL_2_6_12_PLUS
37 # if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
38 # define KERNEL_2_6_13_PLUS
44 # include <linux/config.h>
45 # ifdef CONFIG_MODVERSIONS
47 # include <linux/modversions.h>
49 # ifndef HAVE_I_SIZE_FUNC
50 # define i_size_read(inode) ((inode)->i_size)
51 # define i_size_write(inode, size) do { (inode)->i_size = size; } while(0)
53 # define new_decode_dev(x) (x)
54 # define new_encode_dev(x) (x)
55 #endif /* KERNEL_2_6 */
56 #endif /* FUSE_MAINLINE */
58 #include <linux/wait.h>
59 #include <linux/list.h>
60 #include <linux/spinlock.h>
63 #include <linux/backing-dev.h>
65 #include <asm/semaphore.h>
71 #define container_of(ptr, type, member) ({ \
72 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
73 (type *)( (char *)__mptr - offsetof(type,member) );})
79 #include <linux/pagemap.h>
80 static inline void set_page_dirty_lock(struct page
*page
)
87 /** Max number of pages that can be used in a single read request */
88 #define FUSE_MAX_PAGES_PER_REQ 32
90 /** If more requests are outstanding, then the operation will block */
91 #define FUSE_MAX_OUTSTANDING 10
93 /** If the FUSE_DEFAULT_PERMISSIONS flag is given, the filesystem
94 module will check permissions based on the file mode. Otherwise no
95 permission checking is done in the kernel */
96 #define FUSE_DEFAULT_PERMISSIONS (1 << 0)
98 /** If the FUSE_ALLOW_OTHER flag is given, then not only the user
99 doing the mount will be allowed to access the filesystem */
100 #define FUSE_ALLOW_OTHER (1 << 1)
103 /** Allow FUSE to combine reads into 64k chunks. This is useful if
104 the filesystem is better at handling large chunks */
105 #define FUSE_LARGE_READ (1 << 31)
113 /** Unique ID, which identifies the inode between userspace
117 /** Number of lookups on this inode */
120 /** The request used for sending the FORGET message */
121 struct fuse_req
*forget_req
;
123 /** Time in jiffies until the file attributes are valid */
124 unsigned long i_time
;
127 /** FUSE specific file data */
129 /** Request reserved for flush and release */
130 struct fuse_req
*release_req
;
132 /** File handle used by userspace */
136 /** One input argument of a request */
142 /** The request input */
144 /** The request header */
145 struct fuse_in_header h
;
147 /** True if the data for the last argument is in req->pages */
150 /** Number of arguments */
153 /** Array of arguments */
154 struct fuse_in_arg args
[3];
157 /** One output argument of a request */
163 /** The request output */
165 /** Header returned from userspace */
166 struct fuse_out_header h
;
168 /** Last argument is variable length (can be shorter than
172 /** Last argument is a list of pages to copy data to */
175 /** Zero partially or not copied pages */
176 unsigned page_zeroing
:1;
178 /** Number or arguments */
181 /** Array of arguments */
182 struct fuse_arg args
[3];
189 * A request to the client
192 /** This can be on either unused_list, pending or processing
193 lists in fuse_conn */
194 struct list_head list
;
196 /** Entry on the background list */
197 struct list_head bg_entry
;
202 /** True if the request has reply */
205 /** The request is preallocated */
206 unsigned preallocated
:1;
208 /** The request was interrupted */
209 unsigned interrupted
:1;
211 /** Request is sent in the background */
212 unsigned background
:1;
214 /** Data is being copied to/from the request */
217 /** Request has been sent to userspace */
220 /** The request is finished */
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
;
235 struct fuse_release_in release_in
;
236 struct fuse_init_in_out init_in_out
;
240 struct page
*pages
[FUSE_MAX_PAGES_PER_REQ
];
242 /** number of pages in vector */
245 /** offset of data on first page */
246 unsigned page_offset
;
248 /** Inode used in the request */
251 /** Second inode used in the request (or NULL) */
252 struct inode
*inode2
;
254 /** File used in the request (or NULL) */
261 * This structure is created, when the filesystem is mounted, and is
262 * destroyed, when the client device is closed and the filesystem is
266 /** Reference count */
269 /** The user id for this mount */
272 /** The group id for this mount */
275 /** The fuse mount flags for this mount */
278 /** Maximum read size */
281 /** Maximum write size */
284 /** Readers of the connection are waiting on this */
285 wait_queue_head_t waitq
;
287 /** The list of pending requests */
288 struct list_head pending
;
290 /** The list of requests being processed */
291 struct list_head processing
;
293 /** Requests put in the background (RELEASE or any other
294 interrupted request) */
295 struct list_head background
;
297 /** Controls the maximum number of outstanding requests */
298 struct semaphore outstanding_sem
;
300 /** This counts the number of outstanding requests if
301 outstanding_sem would go negative */
302 unsigned outstanding_debt
;
304 /** RW semaphore for exclusion with fuse_put_super() */
305 struct rw_semaphore sbput_sem
;
307 /** The list of unused requests */
308 struct list_head unused_list
;
310 /** The next unique request id */
313 /** Mount is active */
314 unsigned mounted
: 1;
316 /** Connection established */
317 unsigned connected
: 1;
319 /** Connection failed (version mismatch) */
320 unsigned conn_error
: 1;
322 /** Is fsync not implemented by fs? */
323 unsigned no_fsync
: 1;
325 /** Is fsyncdir not implemented by fs? */
326 unsigned no_fsyncdir
: 1;
328 /** Is flush not implemented by fs? */
329 unsigned no_flush
: 1;
331 /** Is setxattr not implemented by fs? */
332 unsigned no_setxattr
: 1;
334 /** Is getxattr not implemented by fs? */
335 unsigned no_getxattr
: 1;
337 /** Is listxattr not implemented by fs? */
338 unsigned no_listxattr
: 1;
340 /** Is removexattr not implemented by fs? */
341 unsigned no_removexattr
: 1;
344 /** Backing dev info */
345 struct backing_dev_info bdi
;
349 static inline struct fuse_conn
**get_fuse_conn_super_p(struct super_block
*sb
)
352 return (struct fuse_conn
**) &sb
->s_fs_info
;
354 return (struct fuse_conn
**) &sb
->u
.generic_sbp
;
358 static inline struct fuse_conn
*get_fuse_conn_super(struct super_block
*sb
)
360 return *get_fuse_conn_super_p(sb
);
363 static inline struct fuse_conn
*get_fuse_conn(struct inode
*inode
)
365 return get_fuse_conn_super(inode
->i_sb
);
368 static inline struct fuse_inode
*get_fuse_inode(struct inode
*inode
)
370 return container_of(inode
, struct fuse_inode
, inode
);
373 static inline u64
get_node_id(struct inode
*inode
)
375 return get_fuse_inode(inode
)->nodeid
;
378 /** Device operations */
379 extern struct file_operations fuse_dev_operations
;
382 * This is the single global spinlock which protects FUSE's structures
384 * The following data is protected by this lock:
386 * - the private_data field of the device file
387 * - the s_fs_info field of the super block
388 * - unused_list, pending, processing lists in fuse_conn
389 * - background list in fuse_conn
390 * - the unique request ID counter reqctr in fuse_conn
391 * - the sb (super_block) field in fuse_conn
392 * - the file (device file) field in fuse_conn
394 extern spinlock_t fuse_lock
;
397 * Get a filled in inode
399 struct inode
*fuse_iget(struct super_block
*sb
, unsigned long nodeid
,
400 int generation
, struct fuse_attr
*attr
);
403 * Send FORGET command
405 void fuse_send_forget(struct fuse_conn
*fc
, struct fuse_req
*req
,
406 unsigned long nodeid
, u64 nlookup
);
409 * Send READ or READDIR request
411 size_t fuse_send_read_common(struct fuse_req
*req
, struct file
*file
,
412 struct inode
*inode
, loff_t pos
, size_t count
,
416 * Send OPEN or OPENDIR request
418 int fuse_open_common(struct inode
*inode
, struct file
*file
, int isdir
);
421 * Send RELEASE or RELEASEDIR request
423 int fuse_release_common(struct inode
*inode
, struct file
*file
, int isdir
);
426 * Send FSYNC or FSYNCDIR request
428 int fuse_fsync_common(struct file
*file
, struct dentry
*de
, int datasync
,
432 * Initialise file operations on a regular file
434 void fuse_init_file_inode(struct inode
*inode
);
437 * Initialise inode operations on regular files and special files
439 void fuse_init_common(struct inode
*inode
);
442 * Initialise inode and file operations on a directory
444 void fuse_init_dir(struct inode
*inode
);
447 * Initialise inode operations on a symlink
449 void fuse_init_symlink(struct inode
*inode
);
452 * Change attributes of an inode
454 void fuse_change_attributes(struct inode
*inode
, struct fuse_attr
*attr
);
457 * Check if the connection can be released, and if yes, then free the
458 * connection structure
460 void fuse_release_conn(struct fuse_conn
*fc
);
463 * Initialize the client device
465 int fuse_dev_init(void);
468 * Cleanup the client device
470 void fuse_dev_cleanup(void);
475 struct fuse_req
*fuse_request_alloc(void);
480 void fuse_request_free(struct fuse_req
*req
);
483 * Reinitialize a request, the preallocated flag is left unmodified
485 void fuse_reset_request(struct fuse_req
*req
);
488 * Reserve a preallocated request
490 struct fuse_req
*fuse_get_request(struct fuse_conn
*fc
);
493 * Decrement reference count of a request. If count goes to zero put
494 * on unused list (preallocated) or free reqest (not preallocated).
496 void fuse_put_request(struct fuse_conn
*fc
, struct fuse_req
*req
);
499 * Send a request (synchronous)
501 void request_send(struct fuse_conn
*fc
, struct fuse_req
*req
);
504 * Send a request with no reply
506 void request_send_noreply(struct fuse_conn
*fc
, struct fuse_req
*req
);
509 * Send a request in the background
511 void request_send_background(struct fuse_conn
*fc
, struct fuse_req
*req
);
514 * Release inodes and file assiciated with background request
516 void fuse_release_background(struct fuse_req
*req
);
519 * Get the attributes of a file
521 int fuse_do_getattr(struct inode
*inode
);
524 * Invalidate inode attributes
526 void fuse_invalidate_attr(struct inode
*inode
);
529 * Send the INIT message
531 void fuse_send_init(struct fuse_conn
*fc
);