fuse: add atomic open+truncate support
[linux-2.6/kvm.git] / fs / fuse / fuse_i.h
blobb24fc9b386f826b10220f39136f4eda8def271f4
1 /*
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.
6 See the file COPYING.
7 */
9 #include <linux/fuse.h>
10 #include <linux/fs.h>
11 #include <linux/mount.h>
12 #include <linux/wait.h>
13 #include <linux/list.h>
14 #include <linux/spinlock.h>
15 #include <linux/mm.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;
49 /** FUSE inode */
50 struct fuse_inode {
51 /** Inode data */
52 struct inode inode;
54 /** Unique ID, which identifies the inode between userspace
55 * and kernel */
56 u64 nodeid;
58 /** Number of lookups on this inode */
59 u64 nlookup;
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 */
65 u64 i_time;
67 /** The sticky bit in inode->i_mode may have been removed, so
68 preserve the original mode */
69 mode_t orig_i_mode;
71 /** Version of last attribute change */
72 u64 attr_version;
75 /** FUSE specific file data */
76 struct fuse_file {
77 /** Request reserved for flush and release */
78 struct fuse_req *reserved_req;
80 /** File handle used by userspace */
81 u64 fh;
83 /** Refcount */
84 atomic_t count;
87 /** One input argument of a request */
88 struct fuse_in_arg {
89 unsigned size;
90 const void *value;
93 /** The request input */
94 struct fuse_in {
95 /** The request header */
96 struct fuse_in_header h;
98 /** True if the data for the last argument is in req->pages */
99 unsigned argpages:1;
101 /** Number of arguments */
102 unsigned numargs;
104 /** Array of arguments */
105 struct fuse_in_arg args[3];
108 /** One output argument of a request */
109 struct fuse_arg {
110 unsigned size;
111 void *value;
114 /** The request output */
115 struct fuse_out {
116 /** Header returned from userspace */
117 struct fuse_out_header h;
120 * The following bitfields are not changed during the request
121 * processing
124 /** Last argument is variable length (can be shorter than
125 arg->size) */
126 unsigned argvar:1;
128 /** Last argument is a list of pages to copy data to */
129 unsigned argpages:1;
131 /** Zero partially or not copied pages */
132 unsigned page_zeroing:1;
134 /** Number or arguments */
135 unsigned numargs;
137 /** Array of arguments */
138 struct fuse_arg args[3];
141 /** The request state */
142 enum fuse_req_state {
143 FUSE_REQ_INIT = 0,
144 FUSE_REQ_PENDING,
145 FUSE_REQ_READING,
146 FUSE_REQ_SENT,
147 FUSE_REQ_WRITING,
148 FUSE_REQ_FINISHED
151 struct fuse_conn;
154 * A request to the client
156 struct fuse_req {
157 /** This can be on either pending processing or io lists in
158 fuse_conn */
159 struct list_head list;
161 /** Entry on the interrupts list */
162 struct list_head intr_entry;
164 /** refcount */
165 atomic_t count;
167 /** Unique ID for the interrupt request */
168 u64 intr_unique;
171 * The following bitfields are either set once before the
172 * request is queued or setting/clearing them is protected by
173 * fuse_conn->lock
176 /** True if the request has reply */
177 unsigned isreply:1;
179 /** Force sending of the request even if interrupted */
180 unsigned force:1;
182 /** The request was aborted */
183 unsigned aborted:1;
185 /** Request is sent in the background */
186 unsigned background:1;
188 /** The request has been interrupted */
189 unsigned interrupted:1;
191 /** Data is being copied to/from the request */
192 unsigned locked:1;
194 /** Request is counted as "waiting" */
195 unsigned waiting:1;
197 /** State of the request */
198 enum fuse_req_state state;
200 /** The request input */
201 struct fuse_in in;
203 /** The request output */
204 struct fuse_out out;
206 /** Used to wake up the task waiting for completion of request*/
207 wait_queue_head_t waitq;
209 /** Data for asynchronous requests */
210 union {
211 struct fuse_forget_in forget_in;
212 struct fuse_release_in release_in;
213 struct fuse_init_in init_in;
214 struct fuse_init_out init_out;
215 struct fuse_read_in read_in;
216 struct fuse_lk_in lk_in;
217 } misc;
219 /** page vector */
220 struct page *pages[FUSE_MAX_PAGES_PER_REQ];
222 /** number of pages in vector */
223 unsigned num_pages;
225 /** offset of data on first page */
226 unsigned page_offset;
228 /** File used in the request (or NULL) */
229 struct fuse_file *ff;
231 /** vfsmount used in release */
232 struct vfsmount *vfsmount;
234 /** dentry used in release */
235 struct dentry *dentry;
237 /** Request completion callback */
238 void (*end)(struct fuse_conn *, struct fuse_req *);
240 /** Request is stolen from fuse_file->reserved_req */
241 struct file *stolen_file;
245 * A Fuse connection.
247 * This structure is created, when the filesystem is mounted, and is
248 * destroyed, when the client device is closed and the filesystem is
249 * unmounted.
251 struct fuse_conn {
252 /** Lock protecting accessess to members of this structure */
253 spinlock_t lock;
255 /** Mutex protecting against directory alias creation */
256 struct mutex inst_mutex;
258 /** Refcount */
259 atomic_t count;
261 /** The user id for this mount */
262 uid_t user_id;
264 /** The group id for this mount */
265 gid_t group_id;
267 /** The fuse mount flags for this mount */
268 unsigned flags;
270 /** Maximum read size */
271 unsigned max_read;
273 /** Maximum write size */
274 unsigned max_write;
276 /** Readers of the connection are waiting on this */
277 wait_queue_head_t waitq;
279 /** The list of pending requests */
280 struct list_head pending;
282 /** The list of requests being processed */
283 struct list_head processing;
285 /** The list of requests under I/O */
286 struct list_head io;
288 /** Number of requests currently in the background */
289 unsigned num_background;
291 /** Pending interrupts */
292 struct list_head interrupts;
294 /** Flag indicating if connection is blocked. This will be
295 the case before the INIT reply is received, and if there
296 are too many outstading backgrounds requests */
297 int blocked;
299 /** waitq for blocked connection */
300 wait_queue_head_t blocked_waitq;
302 /** waitq for reserved requests */
303 wait_queue_head_t reserved_req_waitq;
305 /** The next unique request id */
306 u64 reqctr;
308 /** Connection established, cleared on umount, connection
309 abort and device release */
310 unsigned connected;
312 /** Connection failed (version mismatch). Cannot race with
313 setting other bitfields since it is only set once in INIT
314 reply, before any other request, and never cleared */
315 unsigned conn_error : 1;
317 /** Connection successful. Only set in INIT */
318 unsigned conn_init : 1;
320 /** Do readpages asynchronously? Only set in INIT */
321 unsigned async_read : 1;
323 /** Do not send separate SETATTR request before open(O_TRUNC) */
324 unsigned atomic_o_trunc : 1;
327 * The following bitfields are only for optimization purposes
328 * and hence races in setting them will not cause malfunction
331 /** Is fsync not implemented by fs? */
332 unsigned no_fsync : 1;
334 /** Is fsyncdir not implemented by fs? */
335 unsigned no_fsyncdir : 1;
337 /** Is flush not implemented by fs? */
338 unsigned no_flush : 1;
340 /** Is setxattr not implemented by fs? */
341 unsigned no_setxattr : 1;
343 /** Is getxattr not implemented by fs? */
344 unsigned no_getxattr : 1;
346 /** Is listxattr not implemented by fs? */
347 unsigned no_listxattr : 1;
349 /** Is removexattr not implemented by fs? */
350 unsigned no_removexattr : 1;
352 /** Are file locking primitives not implemented by fs? */
353 unsigned no_lock : 1;
355 /** Is access not implemented by fs? */
356 unsigned no_access : 1;
358 /** Is create not implemented by fs? */
359 unsigned no_create : 1;
361 /** Is interrupt not implemented by fs? */
362 unsigned no_interrupt : 1;
364 /** Is bmap not implemented by fs? */
365 unsigned no_bmap : 1;
367 /** The number of requests waiting for completion */
368 atomic_t num_waiting;
370 /** Negotiated minor version */
371 unsigned minor;
373 /** Backing dev info */
374 struct backing_dev_info bdi;
376 /** Entry on the fuse_conn_list */
377 struct list_head entry;
379 /** Unique ID */
380 u64 id;
382 /** Dentries in the control filesystem */
383 struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES];
385 /** number of dentries used in the above array */
386 int ctl_ndents;
388 /** O_ASYNC requests */
389 struct fasync_struct *fasync;
391 /** Key for lock owner ID scrambling */
392 u32 scramble_key[4];
394 /** Reserved request for the DESTROY message */
395 struct fuse_req *destroy_req;
397 /** Version counter for attribute changes */
398 u64 attr_version;
401 static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
403 return sb->s_fs_info;
406 static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
408 return get_fuse_conn_super(inode->i_sb);
411 static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
413 return container_of(inode, struct fuse_inode, inode);
416 static inline u64 get_node_id(struct inode *inode)
418 return get_fuse_inode(inode)->nodeid;
421 /** Device operations */
422 extern const struct file_operations fuse_dev_operations;
425 * Get a filled in inode
427 struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
428 int generation, struct fuse_attr *attr,
429 u64 attr_valid, u64 attr_version);
432 * Send FORGET command
434 void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
435 unsigned long nodeid, u64 nlookup);
438 * Initialize READ or READDIR request
440 void fuse_read_fill(struct fuse_req *req, struct fuse_file *ff,
441 struct inode *inode, loff_t pos, size_t count, int opcode);
444 * Send OPEN or OPENDIR request
446 int fuse_open_common(struct inode *inode, struct file *file, int isdir);
448 struct fuse_file *fuse_file_alloc(void);
449 void fuse_file_free(struct fuse_file *ff);
450 void fuse_finish_open(struct inode *inode, struct file *file,
451 struct fuse_file *ff, struct fuse_open_out *outarg);
453 /** Fill in ff->reserved_req with a RELEASE request */
454 void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode);
457 * Send RELEASE or RELEASEDIR request
459 int fuse_release_common(struct inode *inode, struct file *file, int isdir);
462 * Send FSYNC or FSYNCDIR request
464 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
465 int isdir);
468 * Initialize file operations on a regular file
470 void fuse_init_file_inode(struct inode *inode);
473 * Initialize inode operations on regular files and special files
475 void fuse_init_common(struct inode *inode);
478 * Initialize inode and file operations on a directory
480 void fuse_init_dir(struct inode *inode);
483 * Initialize inode operations on a symlink
485 void fuse_init_symlink(struct inode *inode);
488 * Change attributes of an inode
490 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
491 u64 attr_valid, u64 attr_version);
494 * Initialize the client device
496 int fuse_dev_init(void);
499 * Cleanup the client device
501 void fuse_dev_cleanup(void);
503 int fuse_ctl_init(void);
504 void fuse_ctl_cleanup(void);
507 * Allocate a request
509 struct fuse_req *fuse_request_alloc(void);
512 * Free a request
514 void fuse_request_free(struct fuse_req *req);
517 * Get a request, may fail with -ENOMEM
519 struct fuse_req *fuse_get_req(struct fuse_conn *fc);
522 * Gets a requests for a file operation, always succeeds
524 struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file);
527 * Decrement reference count of a request. If count goes to zero free
528 * the request.
530 void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
533 * Send a request (synchronous)
535 void request_send(struct fuse_conn *fc, struct fuse_req *req);
538 * Send a request with no reply
540 void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
543 * Send a request in the background
545 void request_send_background(struct fuse_conn *fc, struct fuse_req *req);
547 /* Abort all requests */
548 void fuse_abort_conn(struct fuse_conn *fc);
551 * Invalidate inode attributes
553 void fuse_invalidate_attr(struct inode *inode);
556 * Acquire reference to fuse_conn
558 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
561 * Release reference to fuse_conn
563 void fuse_conn_put(struct fuse_conn *fc);
566 * Add connection to control filesystem
568 int fuse_ctl_add_conn(struct fuse_conn *fc);
571 * Remove connection from control filesystem
573 void fuse_ctl_remove_conn(struct fuse_conn *fc);
576 * Is file type valid?
578 int fuse_valid_type(int m);
581 * Is task allowed to perform filesystem operation?
583 int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task);