]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/fuse/fuse_i.h
5b21e6ab9e753fd23a7433e0f0af485ec89fedc9
[karo-tx-linux.git] / fs / fuse / fuse_i.h
1 /*
2   FUSE: Filesystem in Userspace
3   Copyright (C) 2001-2008  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 #ifndef _FS_FUSE_I_H
10 #define _FS_FUSE_I_H
11
12 #include <linux/fuse.h>
13 #include <linux/fs.h>
14 #include <linux/mount.h>
15 #include <linux/wait.h>
16 #include <linux/list.h>
17 #include <linux/spinlock.h>
18 #include <linux/mm.h>
19 #include <linux/backing-dev.h>
20 #include <linux/mutex.h>
21 #include <linux/rwsem.h>
22 #include <linux/rbtree.h>
23 #include <linux/poll.h>
24 #include <linux/workqueue.h>
25
26 /** Max number of pages that can be used in a single read request */
27 #define FUSE_MAX_PAGES_PER_REQ 32
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 5
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 /** Number of page pointers embedded in fuse_req */
48 #define FUSE_REQ_INLINE_PAGES 1
49
50 /** List of active connections */
51 extern struct list_head fuse_conn_list;
52
53 /** Global mutex protecting fuse_conn_list and the control filesystem */
54 extern struct mutex fuse_mutex;
55
56 /** Module parameters */
57 extern unsigned max_user_bgreq;
58 extern unsigned max_user_congthresh;
59
60 /* One forget request */
61 struct fuse_forget_link {
62         struct fuse_forget_one forget_one;
63         struct fuse_forget_link *next;
64 };
65
66 /** FUSE inode */
67 struct fuse_inode {
68         /** Inode data */
69         struct inode inode;
70
71         /** Unique ID, which identifies the inode between userspace
72          * and kernel */
73         u64 nodeid;
74
75         /** Number of lookups on this inode */
76         u64 nlookup;
77
78         /** The request used for sending the FORGET message */
79         struct fuse_forget_link *forget;
80
81         /** Time in jiffies until the file attributes are valid */
82         u64 i_time;
83
84         /** The sticky bit in inode->i_mode may have been removed, so
85             preserve the original mode */
86         umode_t orig_i_mode;
87
88         /** 64 bit inode number */
89         u64 orig_ino;
90
91         /** Version of last attribute change */
92         u64 attr_version;
93
94         /** Files usable in writepage.  Protected by fc->lock */
95         struct list_head write_files;
96
97         /** Writepages pending on truncate or fsync */
98         struct list_head queued_writes;
99
100         /** Number of sent writes, a negative bias (FUSE_NOWRITE)
101          * means more writes are blocked */
102         int writectr;
103
104         /** Waitq for writepage completion */
105         wait_queue_head_t page_waitq;
106
107         /** List of writepage requestst (pending or sent) */
108         struct list_head writepages;
109 };
110
111 struct fuse_conn;
112
113 /** FUSE specific file data */
114 struct fuse_file {
115         /** Fuse connection for this file */
116         struct fuse_conn *fc;
117
118         /** Request reserved for flush and release */
119         struct fuse_req *reserved_req;
120
121         /** Kernel file handle guaranteed to be unique */
122         u64 kh;
123
124         /** File handle used by userspace */
125         u64 fh;
126
127         /** Node id of this file */
128         u64 nodeid;
129
130         /** Refcount */
131         atomic_t count;
132
133         /** FOPEN_* flags returned by open */
134         u32 open_flags;
135
136         /** Entry on inode's write_files list */
137         struct list_head write_entry;
138
139         /** RB node to be linked on fuse_conn->polled_files */
140         struct rb_node polled_node;
141
142         /** Wait queue head for poll */
143         wait_queue_head_t poll_wait;
144
145         /** Has flock been performed on this file? */
146         bool flock:1;
147 };
148
149 /** One input argument of a request */
150 struct fuse_in_arg {
151         unsigned size;
152         const void *value;
153 };
154
155 /** The request input */
156 struct fuse_in {
157         /** The request header */
158         struct fuse_in_header h;
159
160         /** True if the data for the last argument is in req->pages */
161         unsigned argpages:1;
162
163         /** Number of arguments */
164         unsigned numargs;
165
166         /** Array of arguments */
167         struct fuse_in_arg args[3];
168 };
169
170 /** One output argument of a request */
171 struct fuse_arg {
172         unsigned size;
173         void *value;
174 };
175
176 /** The request output */
177 struct fuse_out {
178         /** Header returned from userspace */
179         struct fuse_out_header h;
180
181         /*
182          * The following bitfields are not changed during the request
183          * processing
184          */
185
186         /** Last argument is variable length (can be shorter than
187             arg->size) */
188         unsigned argvar:1;
189
190         /** Last argument is a list of pages to copy data to */
191         unsigned argpages:1;
192
193         /** Zero partially or not copied pages */
194         unsigned page_zeroing:1;
195
196         /** Pages may be replaced with new ones */
197         unsigned page_replace:1;
198
199         /** Number or arguments */
200         unsigned numargs;
201
202         /** Array of arguments */
203         struct fuse_arg args[3];
204 };
205
206 /** The request state */
207 enum fuse_req_state {
208         FUSE_REQ_INIT = 0,
209         FUSE_REQ_PENDING,
210         FUSE_REQ_READING,
211         FUSE_REQ_SENT,
212         FUSE_REQ_WRITING,
213         FUSE_REQ_FINISHED
214 };
215
216 /**
217  * A request to the client
218  */
219 struct fuse_req {
220         /** This can be on either pending processing or io lists in
221             fuse_conn */
222         struct list_head list;
223
224         /** Entry on the interrupts list  */
225         struct list_head intr_entry;
226
227         /** refcount */
228         atomic_t count;
229
230         /** Unique ID for the interrupt request */
231         u64 intr_unique;
232
233         /*
234          * The following bitfields are either set once before the
235          * request is queued or setting/clearing them is protected by
236          * fuse_conn->lock
237          */
238
239         /** True if the request has reply */
240         unsigned isreply:1;
241
242         /** Force sending of the request even if interrupted */
243         unsigned force:1;
244
245         /** The request was aborted */
246         unsigned aborted:1;
247
248         /** Request is sent in the background */
249         unsigned background:1;
250
251         /** The request has been interrupted */
252         unsigned interrupted:1;
253
254         /** Data is being copied to/from the request */
255         unsigned locked:1;
256
257         /** Request is counted as "waiting" */
258         unsigned waiting:1;
259
260         /** State of the request */
261         enum fuse_req_state state;
262
263         /** The request input */
264         struct fuse_in in;
265
266         /** The request output */
267         struct fuse_out out;
268
269         /** Used to wake up the task waiting for completion of request*/
270         wait_queue_head_t waitq;
271
272         /** Data for asynchronous requests */
273         union {
274                 struct {
275                         union {
276                                 struct fuse_release_in in;
277                                 struct work_struct work;
278                         };
279                         struct path path;
280                 } release;
281                 struct fuse_init_in init_in;
282                 struct fuse_init_out init_out;
283                 struct cuse_init_in cuse_init_in;
284                 struct {
285                         struct fuse_read_in in;
286                         u64 attr_ver;
287                 } read;
288                 struct {
289                         struct fuse_write_in in;
290                         struct fuse_write_out out;
291                 } write;
292                 struct fuse_notify_retrieve_in retrieve_in;
293                 struct fuse_lk_in lk_in;
294         } misc;
295
296         /** page vector */
297         struct page **pages;
298
299         /** size of the 'pages' array */
300         unsigned max_pages;
301
302         /** inline page vector */
303         struct page *inline_pages[FUSE_REQ_INLINE_PAGES];
304
305         /** number of pages in vector */
306         unsigned num_pages;
307
308         /** offset of data on first page */
309         unsigned page_offset;
310
311         /** File used in the request (or NULL) */
312         struct fuse_file *ff;
313
314         /** Inode used in the request or NULL */
315         struct inode *inode;
316
317         /** Link on fi->writepages */
318         struct list_head writepages_entry;
319
320         /** Request completion callback */
321         void (*end)(struct fuse_conn *, struct fuse_req *);
322
323         /** Request is stolen from fuse_file->reserved_req */
324         struct file *stolen_file;
325 };
326
327 /**
328  * A Fuse connection.
329  *
330  * This structure is created, when the filesystem is mounted, and is
331  * destroyed, when the client device is closed and the filesystem is
332  * unmounted.
333  */
334 struct fuse_conn {
335         /** Lock protecting accessess to  members of this structure */
336         spinlock_t lock;
337
338         /** Mutex protecting against directory alias creation */
339         struct mutex inst_mutex;
340
341         /** Refcount */
342         atomic_t count;
343
344         /** The user id for this mount */
345         kuid_t user_id;
346
347         /** The group id for this mount */
348         kgid_t group_id;
349
350         /** The fuse mount flags for this mount */
351         unsigned flags;
352
353         /** Maximum read size */
354         unsigned max_read;
355
356         /** Maximum write size */
357         unsigned max_write;
358
359         /** Readers of the connection are waiting on this */
360         wait_queue_head_t waitq;
361
362         /** The list of pending requests */
363         struct list_head pending;
364
365         /** The list of requests being processed */
366         struct list_head processing;
367
368         /** The list of requests under I/O */
369         struct list_head io;
370
371         /** The next unique kernel file handle */
372         u64 khctr;
373
374         /** rbtree of fuse_files waiting for poll events indexed by ph */
375         struct rb_root polled_files;
376
377         /** Maximum number of outstanding background requests */
378         unsigned max_background;
379
380         /** Number of background requests at which congestion starts */
381         unsigned congestion_threshold;
382
383         /** Number of requests currently in the background */
384         unsigned num_background;
385
386         /** Number of background requests currently queued for userspace */
387         unsigned active_background;
388
389         /** The list of background requests set aside for later queuing */
390         struct list_head bg_queue;
391
392         /** Pending interrupts */
393         struct list_head interrupts;
394
395         /** Queue of pending forgets */
396         struct fuse_forget_link forget_list_head;
397         struct fuse_forget_link *forget_list_tail;
398
399         /** Batching of FORGET requests (positive indicates FORGET batch) */
400         int forget_batch;
401
402         /** Flag indicating if connection is blocked.  This will be
403             the case before the INIT reply is received, and if there
404             are too many outstading backgrounds requests */
405         int blocked;
406
407         /** waitq for blocked connection */
408         wait_queue_head_t blocked_waitq;
409
410         /** waitq for reserved requests */
411         wait_queue_head_t reserved_req_waitq;
412
413         /** The next unique request id */
414         u64 reqctr;
415
416         /** Connection established, cleared on umount, connection
417             abort and device release */
418         unsigned connected;
419
420         /** Connection failed (version mismatch).  Cannot race with
421             setting other bitfields since it is only set once in INIT
422             reply, before any other request, and never cleared */
423         unsigned conn_error:1;
424
425         /** Connection successful.  Only set in INIT */
426         unsigned conn_init:1;
427
428         /** Do readpages asynchronously?  Only set in INIT */
429         unsigned async_read:1;
430
431         /** Do not send separate SETATTR request before open(O_TRUNC)  */
432         unsigned atomic_o_trunc:1;
433
434         /** Filesystem supports NFS exporting.  Only set in INIT */
435         unsigned export_support:1;
436
437         /** Set if bdi is valid */
438         unsigned bdi_initialized:1;
439
440         /*
441          * The following bitfields are only for optimization purposes
442          * and hence races in setting them will not cause malfunction
443          */
444
445         /** Is fsync not implemented by fs? */
446         unsigned no_fsync:1;
447
448         /** Is fsyncdir not implemented by fs? */
449         unsigned no_fsyncdir:1;
450
451         /** Is flush not implemented by fs? */
452         unsigned no_flush:1;
453
454         /** Is setxattr not implemented by fs? */
455         unsigned no_setxattr:1;
456
457         /** Is getxattr not implemented by fs? */
458         unsigned no_getxattr:1;
459
460         /** Is listxattr not implemented by fs? */
461         unsigned no_listxattr:1;
462
463         /** Is removexattr not implemented by fs? */
464         unsigned no_removexattr:1;
465
466         /** Are posix file locking primitives not implemented by fs? */
467         unsigned no_lock:1;
468
469         /** Is access not implemented by fs? */
470         unsigned no_access:1;
471
472         /** Is create not implemented by fs? */
473         unsigned no_create:1;
474
475         /** Is interrupt not implemented by fs? */
476         unsigned no_interrupt:1;
477
478         /** Is bmap not implemented by fs? */
479         unsigned no_bmap:1;
480
481         /** Is poll not implemented by fs? */
482         unsigned no_poll:1;
483
484         /** Do multi-page cached writes */
485         unsigned big_writes:1;
486
487         /** Don't apply umask to creation modes */
488         unsigned dont_mask:1;
489
490         /** Are BSD file locking primitives not implemented by fs? */
491         unsigned no_flock:1;
492
493         /** Is fallocate not implemented by fs? */
494         unsigned no_fallocate:1;
495
496         /** Use enhanced/automatic page cache invalidation. */
497         unsigned auto_inval_data:1;
498
499         /** Does the filesystem support readdir-plus? */
500         unsigned do_readdirplus:1;
501
502         /** The number of requests waiting for completion */
503         atomic_t num_waiting;
504
505         /** Negotiated minor version */
506         unsigned minor;
507
508         /** Backing dev info */
509         struct backing_dev_info bdi;
510
511         /** Entry on the fuse_conn_list */
512         struct list_head entry;
513
514         /** Device ID from super block */
515         dev_t dev;
516
517         /** Dentries in the control filesystem */
518         struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES];
519
520         /** number of dentries used in the above array */
521         int ctl_ndents;
522
523         /** O_ASYNC requests */
524         struct fasync_struct *fasync;
525
526         /** Key for lock owner ID scrambling */
527         u32 scramble_key[4];
528
529         /** Reserved request for the DESTROY message */
530         struct fuse_req *destroy_req;
531
532         /** Version counter for attribute changes */
533         u64 attr_version;
534
535         /** Called on final put */
536         void (*release)(struct fuse_conn *);
537
538         /** Super block for this connection. */
539         struct super_block *sb;
540
541         /** Read/write semaphore to hold when accessing sb. */
542         struct rw_semaphore killsb;
543 };
544
545 static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
546 {
547         return sb->s_fs_info;
548 }
549
550 static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
551 {
552         return get_fuse_conn_super(inode->i_sb);
553 }
554
555 static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
556 {
557         return container_of(inode, struct fuse_inode, inode);
558 }
559
560 static inline u64 get_node_id(struct inode *inode)
561 {
562         return get_fuse_inode(inode)->nodeid;
563 }
564
565 /** Device operations */
566 extern const struct file_operations fuse_dev_operations;
567
568 extern const struct dentry_operations fuse_dentry_operations;
569
570 /**
571  * Inode to nodeid comparison.
572  */
573 int fuse_inode_eq(struct inode *inode, void *_nodeidp);
574
575 /**
576  * Get a filled in inode
577  */
578 struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
579                         int generation, struct fuse_attr *attr,
580                         u64 attr_valid, u64 attr_version);
581
582 int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
583                      struct fuse_entry_out *outarg, struct inode **inode);
584
585 /**
586  * Send FORGET command
587  */
588 void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
589                        u64 nodeid, u64 nlookup);
590
591 struct fuse_forget_link *fuse_alloc_forget(void);
592
593 /* Used by READDIRPLUS */
594 void fuse_force_forget(struct file *file, u64 nodeid);
595
596 /**
597  * Initialize READ or READDIR request
598  */
599 void fuse_read_fill(struct fuse_req *req, struct file *file,
600                     loff_t pos, size_t count, int opcode);
601
602 /**
603  * Send OPEN or OPENDIR request
604  */
605 int fuse_open_common(struct inode *inode, struct file *file, bool isdir);
606
607 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc);
608 struct fuse_file *fuse_file_get(struct fuse_file *ff);
609 void fuse_file_free(struct fuse_file *ff);
610 void fuse_finish_open(struct inode *inode, struct file *file);
611
612 void fuse_sync_release(struct fuse_file *ff, int flags);
613
614 /**
615  * Send RELEASE or RELEASEDIR request
616  */
617 void fuse_release_common(struct file *file, int opcode);
618
619 /**
620  * Send FSYNC or FSYNCDIR request
621  */
622 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
623                       int datasync, int isdir);
624
625 /**
626  * Notify poll wakeup
627  */
628 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
629                             struct fuse_notify_poll_wakeup_out *outarg);
630
631 /**
632  * Initialize file operations on a regular file
633  */
634 void fuse_init_file_inode(struct inode *inode);
635
636 /**
637  * Initialize inode operations on regular files and special files
638  */
639 void fuse_init_common(struct inode *inode);
640
641 /**
642  * Initialize inode and file operations on a directory
643  */
644 void fuse_init_dir(struct inode *inode);
645
646 /**
647  * Initialize inode operations on a symlink
648  */
649 void fuse_init_symlink(struct inode *inode);
650
651 /**
652  * Change attributes of an inode
653  */
654 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
655                             u64 attr_valid, u64 attr_version);
656
657 void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
658                                    u64 attr_valid);
659
660 /**
661  * Initialize the client device
662  */
663 int fuse_dev_init(void);
664
665 /**
666  * Cleanup the client device
667  */
668 void fuse_dev_cleanup(void);
669
670 int fuse_ctl_init(void);
671 void fuse_ctl_cleanup(void);
672
673 /**
674  * Allocate a request
675  */
676 struct fuse_req *fuse_request_alloc(unsigned npages);
677
678 struct fuse_req *fuse_request_alloc_nofs(unsigned npages);
679
680 /**
681  * Free a request
682  */
683 void fuse_request_free(struct fuse_req *req);
684
685 /**
686  * Get a request, may fail with -ENOMEM,
687  * caller should specify # elements in req->pages[] explicitly
688  */
689 struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages);
690
691 /**
692  * Get a request, may fail with -ENOMEM,
693  * useful for callers who doesn't use req->pages[]
694  */
695 static inline struct fuse_req *fuse_get_req_nopages(struct fuse_conn *fc)
696 {
697         return fuse_get_req(fc, 0);
698 }
699
700 /**
701  * Gets a requests for a file operation, always succeeds
702  */
703 struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
704                                              struct file *file);
705
706 /**
707  * Decrement reference count of a request.  If count goes to zero free
708  * the request.
709  */
710 void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
711
712 /**
713  * Send a request (synchronous)
714  */
715 void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req);
716
717 /**
718  * Send a request in the background
719  */
720 void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req);
721
722 void fuse_request_send_background_locked(struct fuse_conn *fc,
723                                          struct fuse_req *req);
724
725 /* Abort all requests */
726 void fuse_abort_conn(struct fuse_conn *fc);
727
728 /**
729  * Invalidate inode attributes
730  */
731 void fuse_invalidate_attr(struct inode *inode);
732
733 void fuse_invalidate_entry_cache(struct dentry *entry);
734
735 /**
736  * Acquire reference to fuse_conn
737  */
738 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
739
740 void fuse_conn_kill(struct fuse_conn *fc);
741
742 /**
743  * Initialize fuse_conn
744  */
745 void fuse_conn_init(struct fuse_conn *fc);
746
747 /**
748  * Release reference to fuse_conn
749  */
750 void fuse_conn_put(struct fuse_conn *fc);
751
752 /**
753  * Add connection to control filesystem
754  */
755 int fuse_ctl_add_conn(struct fuse_conn *fc);
756
757 /**
758  * Remove connection from control filesystem
759  */
760 void fuse_ctl_remove_conn(struct fuse_conn *fc);
761
762 /**
763  * Is file type valid?
764  */
765 int fuse_valid_type(int m);
766
767 /**
768  * Is task allowed to perform filesystem operation?
769  */
770 int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task);
771
772 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id);
773
774 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
775                            struct file *file, bool *refreshed);
776
777 void fuse_flush_writepages(struct inode *inode);
778
779 void fuse_set_nowrite(struct inode *inode);
780 void fuse_release_nowrite(struct inode *inode);
781
782 u64 fuse_get_attr_version(struct fuse_conn *fc);
783
784 /**
785  * File-system tells the kernel to invalidate cache for the given node id.
786  */
787 int fuse_reverse_inval_inode(struct super_block *sb, u64 nodeid,
788                              loff_t offset, loff_t len);
789
790 /**
791  * File-system tells the kernel to invalidate parent attributes and
792  * the dentry matching parent/name.
793  *
794  * If the child_nodeid is non-zero and:
795  *    - matches the inode number for the dentry matching parent/name,
796  *    - is not a mount point
797  *    - is a file or oan empty directory
798  * then the dentry is unhashed (d_delete()).
799  */
800 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
801                              u64 child_nodeid, struct qstr *name);
802
803 int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
804                  bool isdir);
805 ssize_t fuse_direct_io(struct file *file, const char __user *buf,
806                        size_t count, loff_t *ppos, int write);
807 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
808                    unsigned int flags);
809 long fuse_ioctl_common(struct file *file, unsigned int cmd,
810                        unsigned long arg, unsigned int flags);
811 unsigned fuse_file_poll(struct file *file, poll_table *wait);
812 int fuse_dev_release(struct inode *inode, struct file *file);
813
814 void fuse_write_update_size(struct inode *inode, loff_t pos);
815
816 #endif /* _FS_FUSE_I_H */