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