fuse: truncate on spontaneous size change
[linux-2.6] / fs / fuse / fuse_i.h
1 /*
2   FUSE: Filesystem in Userspace
3   Copyright (C) 2001-2006  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/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>
18
19 /** Max number of pages that can be used in a single read request */
20 #define FUSE_MAX_PAGES_PER_REQ 32
21
22 /** Maximum number of outstanding background requests */
23 #define FUSE_MAX_BACKGROUND 12
24
25 /** Congestion starts at 75% of maximum */
26 #define FUSE_CONGESTION_THRESHOLD (FUSE_MAX_BACKGROUND * 75 / 100)
27
28 /** It could be as large as PATH_MAX, but would that have any uses? */
29 #define FUSE_NAME_MAX 1024
30
31 /** Number of dentries for each connection in the control filesystem */
32 #define FUSE_CTL_NUM_DENTRIES 3
33
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)
38
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)
42
43 /** List of active connections */
44 extern struct list_head fuse_conn_list;
45
46 /** Global mutex protecting fuse_conn_list and the control filesystem */
47 extern struct mutex fuse_mutex;
48
49 /** FUSE inode */
50 struct fuse_inode {
51         /** Inode data */
52         struct inode inode;
53
54         /** Unique ID, which identifies the inode between userspace
55          * and kernel */
56         u64 nodeid;
57
58         /** Number of lookups on this inode */
59         u64 nlookup;
60
61         /** The request used for sending the FORGET message */
62         struct fuse_req *forget_req;
63
64         /** Time in jiffies until the file attributes are valid */
65         u64 i_time;
66 };
67
68 /** FUSE specific file data */
69 struct fuse_file {
70         /** Request reserved for flush and release */
71         struct fuse_req *reserved_req;
72
73         /** File handle used by userspace */
74         u64 fh;
75
76         /** Refcount */
77         atomic_t count;
78 };
79
80 /** One input argument of a request */
81 struct fuse_in_arg {
82         unsigned size;
83         const void *value;
84 };
85
86 /** The request input */
87 struct fuse_in {
88         /** The request header */
89         struct fuse_in_header h;
90
91         /** True if the data for the last argument is in req->pages */
92         unsigned argpages:1;
93
94         /** Number of arguments */
95         unsigned numargs;
96
97         /** Array of arguments */
98         struct fuse_in_arg args[3];
99 };
100
101 /** One output argument of a request */
102 struct fuse_arg {
103         unsigned size;
104         void *value;
105 };
106
107 /** The request output */
108 struct fuse_out {
109         /** Header returned from userspace */
110         struct fuse_out_header h;
111
112         /*
113          * The following bitfields are not changed during the request
114          * processing
115          */
116
117         /** Last argument is variable length (can be shorter than
118             arg->size) */
119         unsigned argvar:1;
120
121         /** Last argument is a list of pages to copy data to */
122         unsigned argpages:1;
123
124         /** Zero partially or not copied pages */
125         unsigned page_zeroing:1;
126
127         /** Number or arguments */
128         unsigned numargs;
129
130         /** Array of arguments */
131         struct fuse_arg args[3];
132 };
133
134 /** The request state */
135 enum fuse_req_state {
136         FUSE_REQ_INIT = 0,
137         FUSE_REQ_PENDING,
138         FUSE_REQ_READING,
139         FUSE_REQ_SENT,
140         FUSE_REQ_WRITING,
141         FUSE_REQ_FINISHED
142 };
143
144 struct fuse_conn;
145
146 /**
147  * A request to the client
148  */
149 struct fuse_req {
150         /** This can be on either pending processing or io lists in
151             fuse_conn */
152         struct list_head list;
153
154         /** Entry on the interrupts list  */
155         struct list_head intr_entry;
156
157         /** refcount */
158         atomic_t count;
159
160         /** Unique ID for the interrupt request */
161         u64 intr_unique;
162
163         /*
164          * The following bitfields are either set once before the
165          * request is queued or setting/clearing them is protected by
166          * fuse_conn->lock
167          */
168
169         /** True if the request has reply */
170         unsigned isreply:1;
171
172         /** Force sending of the request even if interrupted */
173         unsigned force:1;
174
175         /** The request was aborted */
176         unsigned aborted:1;
177
178         /** Request is sent in the background */
179         unsigned background:1;
180
181         /** The request has been interrupted */
182         unsigned interrupted:1;
183
184         /** Data is being copied to/from the request */
185         unsigned locked:1;
186
187         /** Request is counted as "waiting" */
188         unsigned waiting:1;
189
190         /** State of the request */
191         enum fuse_req_state state;
192
193         /** The request input */
194         struct fuse_in in;
195
196         /** The request output */
197         struct fuse_out out;
198
199         /** Used to wake up the task waiting for completion of request*/
200         wait_queue_head_t waitq;
201
202         /** Data for asynchronous requests */
203         union {
204                 struct fuse_forget_in forget_in;
205                 struct fuse_release_in release_in;
206                 struct fuse_init_in init_in;
207                 struct fuse_init_out init_out;
208                 struct fuse_read_in read_in;
209                 struct fuse_lk_in lk_in;
210         } misc;
211
212         /** page vector */
213         struct page *pages[FUSE_MAX_PAGES_PER_REQ];
214
215         /** number of pages in vector */
216         unsigned num_pages;
217
218         /** offset of data on first page */
219         unsigned page_offset;
220
221         /** File used in the request (or NULL) */
222         struct fuse_file *ff;
223
224         /** vfsmount used in release */
225         struct vfsmount *vfsmount;
226
227         /** dentry used in release */
228         struct dentry *dentry;
229
230         /** Request completion callback */
231         void (*end)(struct fuse_conn *, struct fuse_req *);
232
233         /** Request is stolen from fuse_file->reserved_req */
234         struct file *stolen_file;
235 };
236
237 /**
238  * A Fuse connection.
239  *
240  * This structure is created, when the filesystem is mounted, and is
241  * destroyed, when the client device is closed and the filesystem is
242  * unmounted.
243  */
244 struct fuse_conn {
245         /** Lock protecting accessess to  members of this structure */
246         spinlock_t lock;
247
248         /** Mutex protecting against directory alias creation */
249         struct mutex inst_mutex;
250
251         /** Refcount */
252         atomic_t count;
253
254         /** The user id for this mount */
255         uid_t user_id;
256
257         /** The group id for this mount */
258         gid_t group_id;
259
260         /** The fuse mount flags for this mount */
261         unsigned flags;
262
263         /** Maximum read size */
264         unsigned max_read;
265
266         /** Maximum write size */
267         unsigned max_write;
268
269         /** Readers of the connection are waiting on this */
270         wait_queue_head_t waitq;
271
272         /** The list of pending requests */
273         struct list_head pending;
274
275         /** The list of requests being processed */
276         struct list_head processing;
277
278         /** The list of requests under I/O */
279         struct list_head io;
280
281         /** Number of requests currently in the background */
282         unsigned num_background;
283
284         /** Pending interrupts */
285         struct list_head interrupts;
286
287         /** Flag indicating if connection is blocked.  This will be
288             the case before the INIT reply is received, and if there
289             are too many outstading backgrounds requests */
290         int blocked;
291
292         /** waitq for blocked connection */
293         wait_queue_head_t blocked_waitq;
294
295         /** waitq for reserved requests */
296         wait_queue_head_t reserved_req_waitq;
297
298         /** The next unique request id */
299         u64 reqctr;
300
301         /** Connection established, cleared on umount, connection
302             abort and device release */
303         unsigned connected;
304
305         /** Connection failed (version mismatch).  Cannot race with
306             setting other bitfields since it is only set once in INIT
307             reply, before any other request, and never cleared */
308         unsigned conn_error : 1;
309
310         /** Connection successful.  Only set in INIT */
311         unsigned conn_init : 1;
312
313         /** Do readpages asynchronously?  Only set in INIT */
314         unsigned async_read : 1;
315
316         /*
317          * The following bitfields are only for optimization purposes
318          * and hence races in setting them will not cause malfunction
319          */
320
321         /** Is fsync not implemented by fs? */
322         unsigned no_fsync : 1;
323
324         /** Is fsyncdir not implemented by fs? */
325         unsigned no_fsyncdir : 1;
326
327         /** Is flush not implemented by fs? */
328         unsigned no_flush : 1;
329
330         /** Is setxattr not implemented by fs? */
331         unsigned no_setxattr : 1;
332
333         /** Is getxattr not implemented by fs? */
334         unsigned no_getxattr : 1;
335
336         /** Is listxattr not implemented by fs? */
337         unsigned no_listxattr : 1;
338
339         /** Is removexattr not implemented by fs? */
340         unsigned no_removexattr : 1;
341
342         /** Are file locking primitives not implemented by fs? */
343         unsigned no_lock : 1;
344
345         /** Is access not implemented by fs? */
346         unsigned no_access : 1;
347
348         /** Is create not implemented by fs? */
349         unsigned no_create : 1;
350
351         /** Is interrupt not implemented by fs? */
352         unsigned no_interrupt : 1;
353
354         /** Is bmap not implemented by fs? */
355         unsigned no_bmap : 1;
356
357         /** The number of requests waiting for completion */
358         atomic_t num_waiting;
359
360         /** Negotiated minor version */
361         unsigned minor;
362
363         /** Backing dev info */
364         struct backing_dev_info bdi;
365
366         /** Entry on the fuse_conn_list */
367         struct list_head entry;
368
369         /** Unique ID */
370         u64 id;
371
372         /** Dentries in the control filesystem */
373         struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES];
374
375         /** number of dentries used in the above array */
376         int ctl_ndents;
377
378         /** O_ASYNC requests */
379         struct fasync_struct *fasync;
380
381         /** Key for lock owner ID scrambling */
382         u32 scramble_key[4];
383
384         /** Reserved request for the DESTROY message */
385         struct fuse_req *destroy_req;
386 };
387
388 static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
389 {
390         return sb->s_fs_info;
391 }
392
393 static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
394 {
395         return get_fuse_conn_super(inode->i_sb);
396 }
397
398 static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
399 {
400         return container_of(inode, struct fuse_inode, inode);
401 }
402
403 static inline u64 get_node_id(struct inode *inode)
404 {
405         return get_fuse_inode(inode)->nodeid;
406 }
407
408 /** Device operations */
409 extern const struct file_operations fuse_dev_operations;
410
411 /**
412  * Get a filled in inode
413  */
414 struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
415                         int generation, struct fuse_attr *attr);
416
417 /**
418  * Send FORGET command
419  */
420 void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
421                       unsigned long nodeid, u64 nlookup);
422
423 /**
424  * Initialize READ or READDIR request
425  */
426 void fuse_read_fill(struct fuse_req *req, struct fuse_file *ff,
427                     struct inode *inode, loff_t pos, size_t count, int opcode);
428
429 /**
430  * Send OPEN or OPENDIR request
431  */
432 int fuse_open_common(struct inode *inode, struct file *file, int isdir);
433
434 struct fuse_file *fuse_file_alloc(void);
435 void fuse_file_free(struct fuse_file *ff);
436 void fuse_finish_open(struct inode *inode, struct file *file,
437                       struct fuse_file *ff, struct fuse_open_out *outarg);
438
439 /** Fill in ff->reserved_req with a RELEASE request */
440 void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode);
441
442 /**
443  * Send RELEASE or RELEASEDIR request
444  */
445 int fuse_release_common(struct inode *inode, struct file *file, int isdir);
446
447 /**
448  * Send FSYNC or FSYNCDIR request
449  */
450 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
451                       int isdir);
452
453 /**
454  * Initialize file operations on a regular file
455  */
456 void fuse_init_file_inode(struct inode *inode);
457
458 /**
459  * Initialize inode operations on regular files and special files
460  */
461 void fuse_init_common(struct inode *inode);
462
463 /**
464  * Initialize inode and file operations on a directory
465  */
466 void fuse_init_dir(struct inode *inode);
467
468 /**
469  * Initialize inode operations on a symlink
470  */
471 void fuse_init_symlink(struct inode *inode);
472
473 /**
474  * Change attributes of an inode
475  */
476 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr);
477
478 /**
479  * Initialize the client device
480  */
481 int fuse_dev_init(void);
482
483 /**
484  * Cleanup the client device
485  */
486 void fuse_dev_cleanup(void);
487
488 int fuse_ctl_init(void);
489 void fuse_ctl_cleanup(void);
490
491 /**
492  * Allocate a request
493  */
494 struct fuse_req *fuse_request_alloc(void);
495
496 /**
497  * Free a request
498  */
499 void fuse_request_free(struct fuse_req *req);
500
501 /**
502  * Get a request, may fail with -ENOMEM
503  */
504 struct fuse_req *fuse_get_req(struct fuse_conn *fc);
505
506 /**
507  * Gets a requests for a file operation, always succeeds
508  */
509 struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file);
510
511 /**
512  * Decrement reference count of a request.  If count goes to zero free
513  * the request.
514  */
515 void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
516
517 /**
518  * Send a request (synchronous)
519  */
520 void request_send(struct fuse_conn *fc, struct fuse_req *req);
521
522 /**
523  * Send a request with no reply
524  */
525 void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
526
527 /**
528  * Send a request in the background
529  */
530 void request_send_background(struct fuse_conn *fc, struct fuse_req *req);
531
532 /* Abort all requests */
533 void fuse_abort_conn(struct fuse_conn *fc);
534
535 /**
536  * Get the attributes of a file
537  */
538 int fuse_do_getattr(struct inode *inode);
539
540 /**
541  * Invalidate inode attributes
542  */
543 void fuse_invalidate_attr(struct inode *inode);
544
545 /**
546  * Acquire reference to fuse_conn
547  */
548 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
549
550 /**
551  * Release reference to fuse_conn
552  */
553 void fuse_conn_put(struct fuse_conn *fc);
554
555 /**
556  * Add connection to control filesystem
557  */
558 int fuse_ctl_add_conn(struct fuse_conn *fc);
559
560 /**
561  * Remove connection from control filesystem
562  */
563 void fuse_ctl_remove_conn(struct fuse_conn *fc);
564
565 /**
566  * Is file type valid?
567  */
568 int fuse_valid_type(int m);