fuse_i.h 9.4 KB

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