fuse_i.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu>
  4. This program can be distributed under the terms of the GNU GPL.
  5. See the file COPYING.
  6. */
  7. #include <linux/fuse.h>
  8. #include <linux/fs.h>
  9. #include <linux/wait.h>
  10. #include <linux/list.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/mm.h>
  13. #include <linux/backing-dev.h>
  14. #include <asm/semaphore.h>
  15. /** Max number of pages that can be used in a single read request */
  16. #define FUSE_MAX_PAGES_PER_REQ 32
  17. /** If more requests are outstanding, then the operation will block */
  18. #define FUSE_MAX_OUTSTANDING 10
  19. /** FUSE inode */
  20. struct fuse_inode {
  21. /** Inode data */
  22. struct inode inode;
  23. /** Unique ID, which identifies the inode between userspace
  24. * and kernel */
  25. u64 nodeid;
  26. /** Time in jiffies until the file attributes are valid */
  27. unsigned long i_time;
  28. };
  29. /** One input argument of a request */
  30. struct fuse_in_arg {
  31. unsigned size;
  32. const void *value;
  33. };
  34. /** The request input */
  35. struct fuse_in {
  36. /** The request header */
  37. struct fuse_in_header h;
  38. /** True if the data for the last argument is in req->pages */
  39. unsigned argpages:1;
  40. /** Number of arguments */
  41. unsigned numargs;
  42. /** Array of arguments */
  43. struct fuse_in_arg args[3];
  44. };
  45. /** One output argument of a request */
  46. struct fuse_arg {
  47. unsigned size;
  48. void *value;
  49. };
  50. /** The request output */
  51. struct fuse_out {
  52. /** Header returned from userspace */
  53. struct fuse_out_header h;
  54. /** Last argument is variable length (can be shorter than
  55. arg->size) */
  56. unsigned argvar:1;
  57. /** Last argument is a list of pages to copy data to */
  58. unsigned argpages:1;
  59. /** Zero partially or not copied pages */
  60. unsigned page_zeroing:1;
  61. /** Number or arguments */
  62. unsigned numargs;
  63. /** Array of arguments */
  64. struct fuse_arg args[3];
  65. };
  66. struct fuse_req;
  67. struct fuse_conn;
  68. /**
  69. * A request to the client
  70. */
  71. struct fuse_req {
  72. /** This can be on either unused_list, pending or processing
  73. lists in fuse_conn */
  74. struct list_head list;
  75. /** refcount */
  76. atomic_t count;
  77. /** True if the request has reply */
  78. unsigned isreply:1;
  79. /** The request is preallocated */
  80. unsigned preallocated:1;
  81. /** The request was interrupted */
  82. unsigned interrupted:1;
  83. /** Request is sent in the background */
  84. unsigned background:1;
  85. /** Data is being copied to/from the request */
  86. unsigned locked:1;
  87. /** Request has been sent to userspace */
  88. unsigned sent:1;
  89. /** The request is finished */
  90. unsigned finished:1;
  91. /** The request input */
  92. struct fuse_in in;
  93. /** The request output */
  94. struct fuse_out out;
  95. /** Used to wake up the task waiting for completion of request*/
  96. wait_queue_head_t waitq;
  97. /** Data for asynchronous requests */
  98. union {
  99. struct fuse_init_in_out init_in_out;
  100. } misc;
  101. /** page vector */
  102. struct page *pages[FUSE_MAX_PAGES_PER_REQ];
  103. /** number of pages in vector */
  104. unsigned num_pages;
  105. /** offset of data on first page */
  106. unsigned page_offset;
  107. /** Inode used in the request */
  108. struct inode *inode;
  109. /** Second inode used in the request (or NULL) */
  110. struct inode *inode2;
  111. /** File used in the request (or NULL) */
  112. struct file *file;
  113. };
  114. /**
  115. * A Fuse connection.
  116. *
  117. * This structure is created, when the filesystem is mounted, and is
  118. * destroyed, when the client device is closed and the filesystem is
  119. * unmounted.
  120. */
  121. struct fuse_conn {
  122. /** The superblock of the mounted filesystem */
  123. struct super_block *sb;
  124. /** The opened client device */
  125. struct file *file;
  126. /** The user id for this mount */
  127. uid_t user_id;
  128. /** Readers of the connection are waiting on this */
  129. wait_queue_head_t waitq;
  130. /** The list of pending requests */
  131. struct list_head pending;
  132. /** The list of requests being processed */
  133. struct list_head processing;
  134. /** Controls the maximum number of outstanding requests */
  135. struct semaphore outstanding_sem;
  136. /** This counts the number of outstanding requests if
  137. outstanding_sem would go negative */
  138. unsigned outstanding_debt;
  139. /** The list of unused requests */
  140. struct list_head unused_list;
  141. /** The next unique request id */
  142. u64 reqctr;
  143. /** Connection failed (version mismatch) */
  144. unsigned conn_error : 1;
  145. /** Backing dev info */
  146. struct backing_dev_info bdi;
  147. };
  148. static inline struct fuse_conn **get_fuse_conn_super_p(struct super_block *sb)
  149. {
  150. return (struct fuse_conn **) &sb->s_fs_info;
  151. }
  152. static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
  153. {
  154. return *get_fuse_conn_super_p(sb);
  155. }
  156. static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
  157. {
  158. return get_fuse_conn_super(inode->i_sb);
  159. }
  160. static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
  161. {
  162. return container_of(inode, struct fuse_inode, inode);
  163. }
  164. static inline u64 get_node_id(struct inode *inode)
  165. {
  166. return get_fuse_inode(inode)->nodeid;
  167. }
  168. /** Device operations */
  169. extern struct file_operations fuse_dev_operations;
  170. /**
  171. * This is the single global spinlock which protects FUSE's structures
  172. *
  173. * The following data is protected by this lock:
  174. *
  175. * - the private_data field of the device file
  176. * - the s_fs_info field of the super block
  177. * - unused_list, pending, processing lists in fuse_conn
  178. * - the unique request ID counter reqctr in fuse_conn
  179. * - the sb (super_block) field in fuse_conn
  180. * - the file (device file) field in fuse_conn
  181. */
  182. extern spinlock_t fuse_lock;
  183. /**
  184. * Check if the connection can be released, and if yes, then free the
  185. * connection structure
  186. */
  187. void fuse_release_conn(struct fuse_conn *fc);
  188. /**
  189. * Initialize the client device
  190. */
  191. int fuse_dev_init(void);
  192. /**
  193. * Cleanup the client device
  194. */
  195. void fuse_dev_cleanup(void);
  196. /**
  197. * Allocate a request
  198. */
  199. struct fuse_req *fuse_request_alloc(void);
  200. /**
  201. * Free a request
  202. */
  203. void fuse_request_free(struct fuse_req *req);
  204. /**
  205. * Reinitialize a request, the preallocated flag is left unmodified
  206. */
  207. void fuse_reset_request(struct fuse_req *req);
  208. /**
  209. * Reserve a preallocated request
  210. */
  211. struct fuse_req *fuse_get_request(struct fuse_conn *fc);
  212. /**
  213. * Reserve a preallocated request, only interruptible by SIGKILL
  214. */
  215. struct fuse_req *fuse_get_request_nonint(struct fuse_conn *fc);
  216. /**
  217. * Decrement reference count of a request. If count goes to zero put
  218. * on unused list (preallocated) or free reqest (not preallocated).
  219. */
  220. void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
  221. /**
  222. * Send a request (synchronous, interruptible)
  223. */
  224. void request_send(struct fuse_conn *fc, struct fuse_req *req);
  225. /**
  226. * Send a request (synchronous, non-interruptible except by SIGKILL)
  227. */
  228. void request_send_nonint(struct fuse_conn *fc, struct fuse_req *req);
  229. /**
  230. * Send a request with no reply
  231. */
  232. void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
  233. /**
  234. * Send a request in the background
  235. */
  236. void request_send_background(struct fuse_conn *fc, struct fuse_req *req);
  237. /**
  238. * Send the INIT message
  239. */
  240. void fuse_send_init(struct fuse_conn *fc);