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 /** 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 /** Module parameters */
54 extern unsigned max_user_bgreq;
55 extern unsigned max_user_congthresh;
56 
57 /* One forget request */
58 struct fuse_forget_link {
59 	struct fuse_forget_one forget_one;
60 	struct fuse_forget_link *next;
61 };
62 
63 /** FUSE inode */
64 struct fuse_inode {
65 	/** Inode data */
66 	struct inode inode;
67 
68 	/** Unique ID, which identifies the inode between userspace
69 	 * and kernel */
70 	u64 nodeid;
71 
72 	/** Number of lookups on this inode */
73 	u64 nlookup;
74 
75 	/** The request used for sending the FORGET message */
76 	struct fuse_forget_link *forget;
77 
78 	/** Time in jiffies until the file attributes are valid */
79 	u64 i_time;
80 
81 	/** The sticky bit in inode->i_mode may have been removed, so
82 	    preserve the original mode */
83 	umode_t orig_i_mode;
84 
85 	/** 64 bit inode number */
86 	u64 orig_ino;
87 
88 	/** Version of last attribute change */
89 	u64 attr_version;
90 
91 	/** Files usable in writepage.  Protected by fc->lock */
92 	struct list_head write_files;
93 
94 	/** Writepages pending on truncate or fsync */
95 	struct list_head queued_writes;
96 
97 	/** Number of sent writes, a negative bias (FUSE_NOWRITE)
98 	 * means more writes are blocked */
99 	int writectr;
100 
101 	/** Waitq for writepage completion */
102 	wait_queue_head_t page_waitq;
103 
104 	/** List of writepage requestst (pending or sent) */
105 	struct list_head writepages;
106 
107 	/** Miscellaneous bits describing inode state */
108 	unsigned long state;
109 };
110 
111 /** FUSE inode state bits */
112 enum {
113 	/** An operation changing file size is in progress  */
114 	FUSE_I_SIZE_UNSTABLE,
115 };
116 
117 struct fuse_conn;
118 
119 /** FUSE specific file data */
120 struct fuse_file {
121 	/** Fuse connection for this file */
122 	struct fuse_conn *fc;
123 
124 	/** Request reserved for flush and release */
125 	struct fuse_req *reserved_req;
126 
127 	/** Kernel file handle guaranteed to be unique */
128 	u64 kh;
129 
130 	/** File handle used by userspace */
131 	u64 fh;
132 
133 	/** Node id of this file */
134 	u64 nodeid;
135 
136 	/** Refcount */
137 	atomic_t count;
138 
139 	/** FOPEN_* flags returned by open */
140 	u32 open_flags;
141 
142 	/** Entry on inode's write_files list */
143 	struct list_head write_entry;
144 
145 	/** RB node to be linked on fuse_conn->polled_files */
146 	struct rb_node polled_node;
147 
148 	/** Wait queue head for poll */
149 	wait_queue_head_t poll_wait;
150 
151 	/** Has flock been performed on this file? */
152 	bool flock:1;
153 };
154 
155 /** One input argument of a request */
156 struct fuse_in_arg {
157 	unsigned size;
158 	const void *value;
159 };
160 
161 /** The request input */
162 struct fuse_in {
163 	/** The request header */
164 	struct fuse_in_header h;
165 
166 	/** True if the data for the last argument is in req->pages */
167 	unsigned argpages:1;
168 
169 	/** Number of arguments */
170 	unsigned numargs;
171 
172 	/** Array of arguments */
173 	struct fuse_in_arg args[3];
174 };
175 
176 /** One output argument of a request */
177 struct fuse_arg {
178 	unsigned size;
179 	void *value;
180 };
181 
182 /** The request output */
183 struct fuse_out {
184 	/** Header returned from userspace */
185 	struct fuse_out_header h;
186 
187 	/*
188 	 * The following bitfields are not changed during the request
189 	 * processing
190 	 */
191 
192 	/** Last argument is variable length (can be shorter than
193 	    arg->size) */
194 	unsigned argvar:1;
195 
196 	/** Last argument is a list of pages to copy data to */
197 	unsigned argpages:1;
198 
199 	/** Zero partially or not copied pages */
200 	unsigned page_zeroing:1;
201 
202 	/** Pages may be replaced with new ones */
203 	unsigned page_replace:1;
204 
205 	/** Number or arguments */
206 	unsigned numargs;
207 
208 	/** Array of arguments */
209 	struct fuse_arg args[3];
210 };
211 
212 /** The request state */
213 enum fuse_req_state {
214 	FUSE_REQ_INIT = 0,
215 	FUSE_REQ_PENDING,
216 	FUSE_REQ_READING,
217 	FUSE_REQ_SENT,
218 	FUSE_REQ_WRITING,
219 	FUSE_REQ_FINISHED
220 };
221 
222 /**
223  * A request to the client
224  */
225 struct fuse_req {
226 	/** This can be on either pending processing or io lists in
227 	    fuse_conn */
228 	struct list_head list;
229 
230 	/** Entry on the interrupts list  */
231 	struct list_head intr_entry;
232 
233 	/** refcount */
234 	atomic_t count;
235 
236 	/** Unique ID for the interrupt request */
237 	u64 intr_unique;
238 
239 	/*
240 	 * The following bitfields are either set once before the
241 	 * request is queued or setting/clearing them is protected by
242 	 * fuse_conn->lock
243 	 */
244 
245 	/** True if the request has reply */
246 	unsigned isreply:1;
247 
248 	/** Force sending of the request even if interrupted */
249 	unsigned force:1;
250 
251 	/** The request was aborted */
252 	unsigned aborted:1;
253 
254 	/** Request is sent in the background */
255 	unsigned background:1;
256 
257 	/** The request has been interrupted */
258 	unsigned interrupted:1;
259 
260 	/** Data is being copied to/from the request */
261 	unsigned locked:1;
262 
263 	/** Request is counted as "waiting" */
264 	unsigned waiting:1;
265 
266 	/** State of the request */
267 	enum fuse_req_state state;
268 
269 	/** The request input */
270 	struct fuse_in in;
271 
272 	/** The request output */
273 	struct fuse_out out;
274 
275 	/** Used to wake up the task waiting for completion of request*/
276 	wait_queue_head_t waitq;
277 
278 	/** Data for asynchronous requests */
279 	union {
280 		struct {
281 			union {
282 				struct fuse_release_in in;
283 				struct work_struct work;
284 			};
285 			struct path path;
286 		} release;
287 		struct fuse_init_in init_in;
288 		struct fuse_init_out init_out;
289 		struct cuse_init_in cuse_init_in;
290 		struct {
291 			struct fuse_read_in in;
292 			u64 attr_ver;
293 		} read;
294 		struct {
295 			struct fuse_write_in in;
296 			struct fuse_write_out out;
297 		} write;
298 		struct fuse_notify_retrieve_in retrieve_in;
299 		struct fuse_lk_in lk_in;
300 	} misc;
301 
302 	/** page vector */
303 	struct page *pages[FUSE_MAX_PAGES_PER_REQ];
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 	uid_t user_id;
346 
347 	/** The group id for this mount */
348 	gid_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 	/** The number of requests waiting for completion */
494 	atomic_t num_waiting;
495 
496 	/** Negotiated minor version */
497 	unsigned minor;
498 
499 	/** Backing dev info */
500 	struct backing_dev_info bdi;
501 
502 	/** Entry on the fuse_conn_list */
503 	struct list_head entry;
504 
505 	/** Device ID from super block */
506 	dev_t dev;
507 
508 	/** Dentries in the control filesystem */
509 	struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES];
510 
511 	/** number of dentries used in the above array */
512 	int ctl_ndents;
513 
514 	/** O_ASYNC requests */
515 	struct fasync_struct *fasync;
516 
517 	/** Key for lock owner ID scrambling */
518 	u32 scramble_key[4];
519 
520 	/** Reserved request for the DESTROY message */
521 	struct fuse_req *destroy_req;
522 
523 	/** Version counter for attribute changes */
524 	u64 attr_version;
525 
526 	/** Called on final put */
527 	void (*release)(struct fuse_conn *);
528 
529 	/** Super block for this connection. */
530 	struct super_block *sb;
531 
532 	/** Read/write semaphore to hold when accessing sb. */
533 	struct rw_semaphore killsb;
534 };
535 
get_fuse_conn_super(struct super_block * sb)536 static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
537 {
538 	return sb->s_fs_info;
539 }
540 
get_fuse_conn(struct inode * inode)541 static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
542 {
543 	return get_fuse_conn_super(inode->i_sb);
544 }
545 
get_fuse_inode(struct inode * inode)546 static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
547 {
548 	return container_of(inode, struct fuse_inode, inode);
549 }
550 
get_node_id(struct inode * inode)551 static inline u64 get_node_id(struct inode *inode)
552 {
553 	return get_fuse_inode(inode)->nodeid;
554 }
555 
556 /** Device operations */
557 extern const struct file_operations fuse_dev_operations;
558 
559 extern const struct dentry_operations fuse_dentry_operations;
560 
561 /**
562  * Inode to nodeid comparison.
563  */
564 int fuse_inode_eq(struct inode *inode, void *_nodeidp);
565 
566 /**
567  * Get a filled in inode
568  */
569 struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
570 			int generation, struct fuse_attr *attr,
571 			u64 attr_valid, u64 attr_version);
572 
573 int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
574 		     struct fuse_entry_out *outarg, struct inode **inode);
575 
576 /**
577  * Send FORGET command
578  */
579 void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
580 		       u64 nodeid, u64 nlookup);
581 
582 struct fuse_forget_link *fuse_alloc_forget(void);
583 
584 /**
585  * Initialize READ or READDIR request
586  */
587 void fuse_read_fill(struct fuse_req *req, struct file *file,
588 		    loff_t pos, size_t count, int opcode);
589 
590 /**
591  * Send OPEN or OPENDIR request
592  */
593 int fuse_open_common(struct inode *inode, struct file *file, bool isdir);
594 
595 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc);
596 struct fuse_file *fuse_file_get(struct fuse_file *ff);
597 void fuse_file_free(struct fuse_file *ff);
598 void fuse_finish_open(struct inode *inode, struct file *file);
599 
600 void fuse_sync_release(struct fuse_file *ff, int flags);
601 
602 /**
603  * Send RELEASE or RELEASEDIR request
604  */
605 void fuse_release_common(struct file *file, int opcode);
606 
607 /**
608  * Send FSYNC or FSYNCDIR request
609  */
610 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
611 		      int datasync, int isdir);
612 
613 /**
614  * Notify poll wakeup
615  */
616 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
617 			    struct fuse_notify_poll_wakeup_out *outarg);
618 
619 /**
620  * Initialize file operations on a regular file
621  */
622 void fuse_init_file_inode(struct inode *inode);
623 
624 /**
625  * Initialize inode operations on regular files and special files
626  */
627 void fuse_init_common(struct inode *inode);
628 
629 /**
630  * Initialize inode and file operations on a directory
631  */
632 void fuse_init_dir(struct inode *inode);
633 
634 /**
635  * Initialize inode operations on a symlink
636  */
637 void fuse_init_symlink(struct inode *inode);
638 
639 /**
640  * Change attributes of an inode
641  */
642 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
643 			    u64 attr_valid, u64 attr_version);
644 
645 void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
646 				   u64 attr_valid);
647 
648 /**
649  * Initialize the client device
650  */
651 int fuse_dev_init(void);
652 
653 /**
654  * Cleanup the client device
655  */
656 void fuse_dev_cleanup(void);
657 
658 int fuse_ctl_init(void);
659 void fuse_ctl_cleanup(void);
660 
661 /**
662  * Allocate a request
663  */
664 struct fuse_req *fuse_request_alloc(void);
665 
666 struct fuse_req *fuse_request_alloc_nofs(void);
667 
668 /**
669  * Free a request
670  */
671 void fuse_request_free(struct fuse_req *req);
672 
673 /**
674  * Get a request, may fail with -ENOMEM
675  */
676 struct fuse_req *fuse_get_req(struct fuse_conn *fc);
677 
678 /**
679  * Gets a requests for a file operation, always succeeds
680  */
681 struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file);
682 
683 /**
684  * Decrement reference count of a request.  If count goes to zero free
685  * the request.
686  */
687 void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
688 
689 /**
690  * Send a request (synchronous)
691  */
692 void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req);
693 
694 /**
695  * Send a request in the background
696  */
697 void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req);
698 
699 void fuse_request_send_background_locked(struct fuse_conn *fc,
700 					 struct fuse_req *req);
701 
702 /* Abort all requests */
703 void fuse_abort_conn(struct fuse_conn *fc);
704 
705 /**
706  * Invalidate inode attributes
707  */
708 void fuse_invalidate_attr(struct inode *inode);
709 
710 void fuse_invalidate_entry_cache(struct dentry *entry);
711 
712 /**
713  * Acquire reference to fuse_conn
714  */
715 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
716 
717 void fuse_conn_kill(struct fuse_conn *fc);
718 
719 /**
720  * Initialize fuse_conn
721  */
722 void fuse_conn_init(struct fuse_conn *fc);
723 
724 /**
725  * Release reference to fuse_conn
726  */
727 void fuse_conn_put(struct fuse_conn *fc);
728 
729 /**
730  * Add connection to control filesystem
731  */
732 int fuse_ctl_add_conn(struct fuse_conn *fc);
733 
734 /**
735  * Remove connection from control filesystem
736  */
737 void fuse_ctl_remove_conn(struct fuse_conn *fc);
738 
739 /**
740  * Is file type valid?
741  */
742 int fuse_valid_type(int m);
743 
744 /**
745  * Is task allowed to perform filesystem operation?
746  */
747 int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task);
748 
749 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id);
750 
751 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
752 			   struct file *file, bool *refreshed);
753 
754 void fuse_flush_writepages(struct inode *inode);
755 
756 void fuse_set_nowrite(struct inode *inode);
757 void fuse_release_nowrite(struct inode *inode);
758 
759 u64 fuse_get_attr_version(struct fuse_conn *fc);
760 
761 /**
762  * File-system tells the kernel to invalidate cache for the given node id.
763  */
764 int fuse_reverse_inval_inode(struct super_block *sb, u64 nodeid,
765 			     loff_t offset, loff_t len);
766 
767 /**
768  * File-system tells the kernel to invalidate parent attributes and
769  * the dentry matching parent/name.
770  *
771  * If the child_nodeid is non-zero and:
772  *    - matches the inode number for the dentry matching parent/name,
773  *    - is not a mount point
774  *    - is a file or oan empty directory
775  * then the dentry is unhashed (d_delete()).
776  */
777 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
778 			     u64 child_nodeid, struct qstr *name);
779 
780 int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
781 		 bool isdir);
782 ssize_t fuse_direct_io(struct file *file, const char __user *buf,
783 		       size_t count, loff_t *ppos, int write);
784 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
785 		   unsigned int flags);
786 long fuse_ioctl_common(struct file *file, unsigned int cmd,
787 		       unsigned long arg, unsigned int flags);
788 unsigned fuse_file_poll(struct file *file, poll_table *wait);
789 int fuse_dev_release(struct inode *inode, struct file *file);
790 
791 void fuse_write_update_size(struct inode *inode, loff_t pos);
792 
793 #endif /* _FS_FUSE_I_H */
794