[PATCH] FUSE - read-write operations
[powerpc.git] / fs / fuse / fuse_i.h
1 /*
2   FUSE: Filesystem in Userspace
3   Copyright (C) 2001-2005  Miklos Szeredi <miklos@szeredi.hu>
4
5   This program can be distributed under the terms of the GNU GPL.
6   See the file COPYING.
7 */
8
9 #include <linux/fuse.h>
10 #include <linux/fs.h>
11 #include <linux/wait.h>
12 #include <linux/list.h>
13 #include <linux/spinlock.h>
14 #include <linux/mm.h>
15 #include <linux/backing-dev.h>
16 #include <asm/semaphore.h>
17
18 /** Max number of pages that can be used in a single read request */
19 #define FUSE_MAX_PAGES_PER_REQ 32
20
21 /** If more requests are outstanding, then the operation will block */
22 #define FUSE_MAX_OUTSTANDING 10
23
24 /** FUSE inode */
25 struct fuse_inode {
26         /** Inode data */
27         struct inode inode;
28
29         /** Unique ID, which identifies the inode between userspace
30          * and kernel */
31         u64 nodeid;
32
33         /** Number of lookups on this inode */
34         u64 nlookup;
35
36         /** The request used for sending the FORGET message */
37         struct fuse_req *forget_req;
38
39         /** Time in jiffies until the file attributes are valid */
40         unsigned long i_time;
41 };
42
43 /** One input argument of a request */
44 struct fuse_in_arg {
45         unsigned size;
46         const void *value;
47 };
48
49 /** The request input */
50 struct fuse_in {
51         /** The request header */
52         struct fuse_in_header h;
53
54         /** True if the data for the last argument is in req->pages */
55         unsigned argpages:1;
56
57         /** Number of arguments */
58         unsigned numargs;
59
60         /** Array of arguments */
61         struct fuse_in_arg args[3];
62 };
63
64 /** One output argument of a request */
65 struct fuse_arg {
66         unsigned size;
67         void *value;
68 };
69
70 /** The request output */
71 struct fuse_out {
72         /** Header returned from userspace */
73         struct fuse_out_header h;
74
75         /** Last argument is variable length (can be shorter than
76             arg->size) */
77         unsigned argvar:1;
78
79         /** Last argument is a list of pages to copy data to */
80         unsigned argpages:1;
81
82         /** Zero partially or not copied pages */
83         unsigned page_zeroing:1;
84
85         /** Number or arguments */
86         unsigned numargs;
87
88         /** Array of arguments */
89         struct fuse_arg args[3];
90 };
91
92 struct fuse_req;
93 struct fuse_conn;
94
95 /**
96  * A request to the client
97  */
98 struct fuse_req {
99         /** This can be on either unused_list, pending or processing
100             lists in fuse_conn */
101         struct list_head list;
102
103         /** refcount */
104         atomic_t count;
105
106         /** True if the request has reply */
107         unsigned isreply:1;
108
109         /** The request is preallocated */
110         unsigned preallocated:1;
111
112         /** The request was interrupted */
113         unsigned interrupted:1;
114
115         /** Request is sent in the background */
116         unsigned background:1;
117
118         /** Data is being copied to/from the request */
119         unsigned locked:1;
120
121         /** Request has been sent to userspace */
122         unsigned sent:1;
123
124         /** The request is finished */
125         unsigned finished:1;
126
127         /** The request input */
128         struct fuse_in in;
129
130         /** The request output */
131         struct fuse_out out;
132
133         /** Used to wake up the task waiting for completion of request*/
134         wait_queue_head_t waitq;
135
136         /** Data for asynchronous requests */
137         union {
138                 struct fuse_forget_in forget_in;
139                 struct fuse_init_in_out init_in_out;
140         } misc;
141
142         /** page vector */
143         struct page *pages[FUSE_MAX_PAGES_PER_REQ];
144
145         /** number of pages in vector */
146         unsigned num_pages;
147
148         /** offset of data on first page */
149         unsigned page_offset;
150
151         /** Inode used in the request */
152         struct inode *inode;
153
154         /** Second inode used in the request (or NULL) */
155         struct inode *inode2;
156
157         /** File used in the request (or NULL) */
158         struct file *file;
159 };
160
161 /**
162  * A Fuse connection.
163  *
164  * This structure is created, when the filesystem is mounted, and is
165  * destroyed, when the client device is closed and the filesystem is
166  * unmounted.
167  */
168 struct fuse_conn {
169         /** The superblock of the mounted filesystem */
170         struct super_block *sb;
171
172         /** The opened client device */
173         struct file *file;
174
175         /** The user id for this mount */
176         uid_t user_id;
177
178         /** Readers of the connection are waiting on this */
179         wait_queue_head_t waitq;
180
181         /** The list of pending requests */
182         struct list_head pending;
183
184         /** The list of requests being processed */
185         struct list_head processing;
186
187         /** Controls the maximum number of outstanding requests */
188         struct semaphore outstanding_sem;
189
190         /** This counts the number of outstanding requests if
191             outstanding_sem would go negative */
192         unsigned outstanding_debt;
193
194         /** The list of unused requests */
195         struct list_head unused_list;
196
197         /** The next unique request id */
198         u64 reqctr;
199
200         /** Connection failed (version mismatch) */
201         unsigned conn_error : 1;
202
203         /** Backing dev info */
204         struct backing_dev_info bdi;
205 };
206
207 struct fuse_getdir_out_i {
208         int fd;
209         void *file; /* Used by kernel only */
210 };
211
212 static inline struct fuse_conn **get_fuse_conn_super_p(struct super_block *sb)
213 {
214         return (struct fuse_conn **) &sb->s_fs_info;
215 }
216
217 static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
218 {
219         return *get_fuse_conn_super_p(sb);
220 }
221
222 static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
223 {
224         return get_fuse_conn_super(inode->i_sb);
225 }
226
227 static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
228 {
229         return container_of(inode, struct fuse_inode, inode);
230 }
231
232 static inline u64 get_node_id(struct inode *inode)
233 {
234         return get_fuse_inode(inode)->nodeid;
235 }
236
237 /** Device operations */
238 extern struct file_operations fuse_dev_operations;
239
240 /**
241  * This is the single global spinlock which protects FUSE's structures
242  *
243  * The following data is protected by this lock:
244  *
245  *  - the private_data field of the device file
246  *  - the s_fs_info field of the super block
247  *  - unused_list, pending, processing lists in fuse_conn
248  *  - the unique request ID counter reqctr in fuse_conn
249  *  - the sb (super_block) field in fuse_conn
250  *  - the file (device file) field in fuse_conn
251  */
252 extern spinlock_t fuse_lock;
253
254 /**
255  * Get a filled in inode
256  */
257 struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
258                         int generation, struct fuse_attr *attr);
259
260 /**
261  * Send FORGET command
262  */
263 void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
264                       unsigned long nodeid, u64 nlookup);
265
266 /**
267  * Initialise inode operations on regular files and special files
268  */
269 void fuse_init_common(struct inode *inode);
270
271 /**
272  * Initialise inode and file operations on a directory
273  */
274 void fuse_init_dir(struct inode *inode);
275
276 /**
277  * Initialise inode operations on a symlink
278  */
279 void fuse_init_symlink(struct inode *inode);
280
281 /**
282  * Change attributes of an inode
283  */
284 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr);
285
286 /**
287  * Check if the connection can be released, and if yes, then free the
288  * connection structure
289  */
290 void fuse_release_conn(struct fuse_conn *fc);
291
292 /**
293  * Initialize the client device
294  */
295 int fuse_dev_init(void);
296
297 /**
298  * Cleanup the client device
299  */
300 void fuse_dev_cleanup(void);
301
302 /**
303  * Allocate a request
304  */
305 struct fuse_req *fuse_request_alloc(void);
306
307 /**
308  * Free a request
309  */
310 void fuse_request_free(struct fuse_req *req);
311
312 /**
313  * Reinitialize a request, the preallocated flag is left unmodified
314  */
315 void fuse_reset_request(struct fuse_req *req);
316
317 /**
318  * Reserve a preallocated request
319  */
320 struct fuse_req *fuse_get_request(struct fuse_conn *fc);
321
322 /**
323  * Reserve a preallocated request, only interruptible by SIGKILL
324  */
325 struct fuse_req *fuse_get_request_nonint(struct fuse_conn *fc);
326
327 /**
328  * Decrement reference count of a request.  If count goes to zero put
329  * on unused list (preallocated) or free reqest (not preallocated).
330  */
331 void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
332
333 /**
334  * Send a request (synchronous, interruptible)
335  */
336 void request_send(struct fuse_conn *fc, struct fuse_req *req);
337
338 /**
339  * Send a request (synchronous, non-interruptible except by SIGKILL)
340  */
341 void request_send_nonint(struct fuse_conn *fc, struct fuse_req *req);
342
343 /**
344  * Send a request with no reply
345  */
346 void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
347
348 /**
349  * Send a request in the background
350  */
351 void request_send_background(struct fuse_conn *fc, struct fuse_req *req);
352
353 /**
354  * Get the attributes of a file
355  */
356 int fuse_do_getattr(struct inode *inode);
357
358 /**
359  * Invalidate inode attributes
360  */
361 void fuse_invalidate_attr(struct inode *inode);
362
363 /**
364  * Send the INIT message
365  */
366 void fuse_send_init(struct fuse_conn *fc);