fuse_i.h 7.9 KB

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