fuse_i.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. /** The request used for sending the FORGET message */
  27. struct fuse_req *forget_req;
  28. /** Time in jiffies until the file attributes are valid */
  29. unsigned long i_time;
  30. };
  31. /** One input argument of a request */
  32. struct fuse_in_arg {
  33. unsigned size;
  34. const void *value;
  35. };
  36. /** The request input */
  37. struct fuse_in {
  38. /** The request header */
  39. struct fuse_in_header h;
  40. /** True if the data for the last argument is in req->pages */
  41. unsigned argpages:1;
  42. /** Number of arguments */
  43. unsigned numargs;
  44. /** Array of arguments */
  45. struct fuse_in_arg args[3];
  46. };
  47. /** One output argument of a request */
  48. struct fuse_arg {
  49. unsigned size;
  50. void *value;
  51. };
  52. /** The request output */
  53. struct fuse_out {
  54. /** Header returned from userspace */
  55. struct fuse_out_header h;
  56. /** Last argument is variable length (can be shorter than
  57. arg->size) */
  58. unsigned argvar:1;
  59. /** Last argument is a list of pages to copy data to */
  60. unsigned argpages:1;
  61. /** Zero partially or not copied pages */
  62. unsigned page_zeroing:1;
  63. /** Number or arguments */
  64. unsigned numargs;
  65. /** Array of arguments */
  66. struct fuse_arg args[3];
  67. };
  68. struct fuse_req;
  69. struct fuse_conn;
  70. /**
  71. * A request to the client
  72. */
  73. struct fuse_req {
  74. /** This can be on either unused_list, pending or processing
  75. lists in fuse_conn */
  76. struct list_head list;
  77. /** refcount */
  78. atomic_t count;
  79. /** True if the request has reply */
  80. unsigned isreply:1;
  81. /** The request is preallocated */
  82. unsigned preallocated:1;
  83. /** The request was interrupted */
  84. unsigned interrupted:1;
  85. /** Request is sent in the background */
  86. unsigned background:1;
  87. /** Data is being copied to/from the request */
  88. unsigned locked:1;
  89. /** Request has been sent to userspace */
  90. unsigned sent:1;
  91. /** The request is finished */
  92. unsigned finished:1;
  93. /** The request input */
  94. struct fuse_in in;
  95. /** The request output */
  96. struct fuse_out out;
  97. /** Used to wake up the task waiting for completion of request*/
  98. wait_queue_head_t waitq;
  99. /** Data for asynchronous requests */
  100. union {
  101. struct fuse_forget_in forget_in;
  102. struct fuse_init_in_out init_in_out;
  103. } misc;
  104. /** page vector */
  105. struct page *pages[FUSE_MAX_PAGES_PER_REQ];
  106. /** number of pages in vector */
  107. unsigned num_pages;
  108. /** offset of data on first page */
  109. unsigned page_offset;
  110. /** Inode used in the request */
  111. struct inode *inode;
  112. /** Second inode used in the request (or NULL) */
  113. struct inode *inode2;
  114. /** File used in the request (or NULL) */
  115. struct file *file;
  116. };
  117. /**
  118. * A Fuse connection.
  119. *
  120. * This structure is created, when the filesystem is mounted, and is
  121. * destroyed, when the client device is closed and the filesystem is
  122. * unmounted.
  123. */
  124. struct fuse_conn {
  125. /** The superblock of the mounted filesystem */
  126. struct super_block *sb;
  127. /** The opened client device */
  128. struct file *file;
  129. /** The user id for this mount */
  130. uid_t user_id;
  131. /** Readers of the connection are waiting on this */
  132. wait_queue_head_t waitq;
  133. /** The list of pending requests */
  134. struct list_head pending;
  135. /** The list of requests being processed */
  136. struct list_head processing;
  137. /** Controls the maximum number of outstanding requests */
  138. struct semaphore outstanding_sem;
  139. /** This counts the number of outstanding requests if
  140. outstanding_sem would go negative */
  141. unsigned outstanding_debt;
  142. /** The list of unused requests */
  143. struct list_head unused_list;
  144. /** The next unique request id */
  145. u64 reqctr;
  146. /** Connection failed (version mismatch) */
  147. unsigned conn_error : 1;
  148. /** Backing dev info */
  149. struct backing_dev_info bdi;
  150. };
  151. struct fuse_getdir_out_i {
  152. int fd;
  153. void *file; /* Used by kernel only */
  154. };
  155. static inline struct fuse_conn **get_fuse_conn_super_p(struct super_block *sb)
  156. {
  157. return (struct fuse_conn **) &sb->s_fs_info;
  158. }
  159. static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
  160. {
  161. return *get_fuse_conn_super_p(sb);
  162. }
  163. static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
  164. {
  165. return get_fuse_conn_super(inode->i_sb);
  166. }
  167. static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
  168. {
  169. return container_of(inode, struct fuse_inode, inode);
  170. }
  171. static inline u64 get_node_id(struct inode *inode)
  172. {
  173. return get_fuse_inode(inode)->nodeid;
  174. }
  175. /** Device operations */
  176. extern struct file_operations fuse_dev_operations;
  177. /**
  178. * This is the single global spinlock which protects FUSE's structures
  179. *
  180. * The following data is protected by this lock:
  181. *
  182. * - the private_data field of the device file
  183. * - the s_fs_info field of the super block
  184. * - unused_list, pending, processing lists in fuse_conn
  185. * - the unique request ID counter reqctr in fuse_conn
  186. * - the sb (super_block) field in fuse_conn
  187. * - the file (device file) field in fuse_conn
  188. */
  189. extern spinlock_t fuse_lock;
  190. /**
  191. * Get a filled in inode
  192. */
  193. struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
  194. int generation, struct fuse_attr *attr, int version);
  195. /**
  196. * Send FORGET command
  197. */
  198. void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
  199. unsigned long nodeid, int version);
  200. /**
  201. * Initialise inode operations on regular files and special files
  202. */
  203. void fuse_init_common(struct inode *inode);
  204. /**
  205. * Initialise inode and file operations on a directory
  206. */
  207. void fuse_init_dir(struct inode *inode);
  208. /**
  209. * Initialise inode operations on a symlink
  210. */
  211. void fuse_init_symlink(struct inode *inode);
  212. /**
  213. * Change attributes of an inode
  214. */
  215. void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr);
  216. /**
  217. * Check if the connection can be released, and if yes, then free the
  218. * connection structure
  219. */
  220. void fuse_release_conn(struct fuse_conn *fc);
  221. /**
  222. * Initialize the client device
  223. */
  224. int fuse_dev_init(void);
  225. /**
  226. * Cleanup the client device
  227. */
  228. void fuse_dev_cleanup(void);
  229. /**
  230. * Allocate a request
  231. */
  232. struct fuse_req *fuse_request_alloc(void);
  233. /**
  234. * Free a request
  235. */
  236. void fuse_request_free(struct fuse_req *req);
  237. /**
  238. * Reinitialize a request, the preallocated flag is left unmodified
  239. */
  240. void fuse_reset_request(struct fuse_req *req);
  241. /**
  242. * Reserve a preallocated request
  243. */
  244. struct fuse_req *fuse_get_request(struct fuse_conn *fc);
  245. /**
  246. * Reserve a preallocated request, only interruptible by SIGKILL
  247. */
  248. struct fuse_req *fuse_get_request_nonint(struct fuse_conn *fc);
  249. /**
  250. * Decrement reference count of a request. If count goes to zero put
  251. * on unused list (preallocated) or free reqest (not preallocated).
  252. */
  253. void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
  254. /**
  255. * Send a request (synchronous, interruptible)
  256. */
  257. void request_send(struct fuse_conn *fc, struct fuse_req *req);
  258. /**
  259. * Send a request (synchronous, non-interruptible except by SIGKILL)
  260. */
  261. void request_send_nonint(struct fuse_conn *fc, struct fuse_req *req);
  262. /**
  263. * Send a request with no reply
  264. */
  265. void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
  266. /**
  267. * Send a request in the background
  268. */
  269. void request_send_background(struct fuse_conn *fc, struct fuse_req *req);
  270. /**
  271. * Get the attributes of a file
  272. */
  273. int fuse_do_getattr(struct inode *inode);
  274. /**
  275. * Invalidate inode attributes
  276. */
  277. void fuse_invalidate_attr(struct inode *inode);
  278. /**
  279. * Send the INIT message
  280. */
  281. void fuse_send_init(struct fuse_conn *fc);