fix
[fuse.git] / kernel / fuse_i.h
blobf5afa01e6640bb4c093b7d89d555b8f293eea757
1 /*
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.
6 See the file COPYING.
7 */
9 #ifdef FUSE_MAINLINE
10 #include <linux/fuse.h>
11 #else
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
18 #endif
20 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
21 # define KERNEL_2_6
22 # if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,6)
23 # define KERNEL_2_6_6_PLUS
24 # endif
25 # if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,8)
26 # define KERNEL_2_6_8_PLUS
27 # endif
28 # if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,9)
29 # define KERNEL_2_6_9_PLUS
30 # endif
31 # if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10)
32 # define KERNEL_2_6_10_PLUS
33 # endif
34 # if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
35 # define KERNEL_2_6_12_PLUS
36 # endif
37 # if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,13)
38 # define KERNEL_2_6_13_PLUS
39 # endif
40 #endif
42 #include "config.h"
43 #ifndef KERNEL_2_6
44 # include <linux/config.h>
45 # ifdef CONFIG_MODVERSIONS
46 # define MODVERSIONS
47 # include <linux/modversions.h>
48 # endif
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)
52 # endif
53 # define new_decode_dev(x) (x)
54 # define new_encode_dev(x) (x)
55 #endif /* KERNEL_2_6 */
56 #endif /* FUSE_MAINLINE */
57 #include <linux/fs.h>
58 #include <linux/wait.h>
59 #include <linux/list.h>
60 #include <linux/spinlock.h>
61 #ifdef KERNEL_2_6
62 #include <linux/mm.h>
63 #include <linux/backing-dev.h>
64 #endif
65 #include <asm/semaphore.h>
67 #ifndef BUG_ON
68 #define BUG_ON(x)
69 #endif
70 #ifndef container_of
71 #define container_of(ptr, type, member) ({ \
72 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
73 (type *)( (char *)__mptr - offsetof(type,member) );})
74 #endif
75 #ifndef __user
76 #define __user
77 #endif
78 #ifndef KERNEL_2_6
79 #include <linux/pagemap.h>
80 static inline void set_page_dirty_lock(struct page *page)
82 lock_page(page);
83 set_page_dirty(page);
84 unlock_page(page);
86 #endif
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)
102 #ifndef KERNEL_2_6
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)
106 #endif
108 /** FUSE inode */
109 struct fuse_inode {
110 /** Inode data */
111 struct inode inode;
113 /** Unique ID, which identifies the inode between userspace
114 * and kernel */
115 u64 nodeid;
117 /** Number of lookups on this inode */
118 u64 nlookup;
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 */
128 struct fuse_file {
129 /** Request reserved for flush and release */
130 struct fuse_req *release_req;
132 /** File handle used by userspace */
133 u64 fh;
136 /** One input argument of a request */
137 struct fuse_in_arg {
138 unsigned size;
139 const void *value;
142 /** The request input */
143 struct fuse_in {
144 /** The request header */
145 struct fuse_in_header h;
147 /** True if the data for the last argument is in req->pages */
148 unsigned argpages:1;
150 /** Number of arguments */
151 unsigned numargs;
153 /** Array of arguments */
154 struct fuse_in_arg args[3];
157 /** One output argument of a request */
158 struct fuse_arg {
159 unsigned size;
160 void *value;
163 /** The request output */
164 struct fuse_out {
165 /** Header returned from userspace */
166 struct fuse_out_header h;
168 /** Last argument is variable length (can be shorter than
169 arg->size) */
170 unsigned argvar:1;
172 /** Last argument is a list of pages to copy data to */
173 unsigned argpages:1;
175 /** Zero partially or not copied pages */
176 unsigned page_zeroing:1;
178 /** Number or arguments */
179 unsigned numargs;
181 /** Array of arguments */
182 struct fuse_arg args[3];
185 struct fuse_req;
186 struct fuse_conn;
189 * A request to the client
191 struct fuse_req {
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;
199 /** refcount */
200 atomic_t count;
202 /** True if the request has reply */
203 unsigned isreply:1;
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 */
215 unsigned locked:1;
217 /** Request has been sent to userspace */
218 unsigned sent:1;
220 /** The request is finished */
221 unsigned finished:1;
223 /** The request input */
224 struct fuse_in in;
226 /** The request output */
227 struct fuse_out out;
229 /** Used to wake up the task waiting for completion of request*/
230 wait_queue_head_t waitq;
232 /** Data for asynchronous requests */
233 union {
234 struct fuse_forget_in forget_in;
235 struct fuse_release_in release_in;
236 struct fuse_init_in_out init_in_out;
237 } misc;
239 /** page vector */
240 struct page *pages[FUSE_MAX_PAGES_PER_REQ];
242 /** number of pages in vector */
243 unsigned num_pages;
245 /** offset of data on first page */
246 unsigned page_offset;
248 /** Inode used in the request */
249 struct inode *inode;
251 /** Second inode used in the request (or NULL) */
252 struct inode *inode2;
254 /** File used in the request (or NULL) */
255 struct file *file;
259 * A Fuse connection.
261 * This structure is created, when the filesystem is mounted, and is
262 * destroyed, when the client device is closed and the filesystem is
263 * unmounted.
265 struct fuse_conn {
266 /** Reference count */
267 int count;
269 /** The user id for this mount */
270 uid_t user_id;
272 /** The group id for this mount */
273 gid_t group_id;
275 /** The fuse mount flags for this mount */
276 unsigned flags;
278 /** Maximum read size */
279 unsigned max_read;
281 /** Maximum write size */
282 unsigned max_write;
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 */
311 u64 reqctr;
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;
343 #ifdef KERNEL_2_6
344 /** Backing dev info */
345 struct backing_dev_info bdi;
346 #endif
349 static inline struct fuse_conn **get_fuse_conn_super_p(struct super_block *sb)
351 #ifdef KERNEL_2_6
352 return (struct fuse_conn **) &sb->s_fs_info;
353 #else
354 return (struct fuse_conn **) &sb->u.generic_sbp;
355 #endif
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,
413 int isdir);
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,
429 int isdir);
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);
473 * Allocate a request
475 struct fuse_req *fuse_request_alloc(void);
478 * Free a request
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);