fuse_i.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. /** Is setxattr not implemented by fs? */
  184. unsigned no_setxattr : 1;
  185. /** Is getxattr not implemented by fs? */
  186. unsigned no_getxattr : 1;
  187. /** Is listxattr not implemented by fs? */
  188. unsigned no_listxattr : 1;
  189. /** Is removexattr not implemented by fs? */
  190. unsigned no_removexattr : 1;
  191. /** Backing dev info */
  192. struct backing_dev_info bdi;
  193. };
  194. struct fuse_getdir_out_i {
  195. int fd;
  196. void *file; /* Used by kernel only */
  197. };
  198. static inline struct fuse_conn **get_fuse_conn_super_p(struct super_block *sb)
  199. {
  200. return (struct fuse_conn **) &sb->s_fs_info;
  201. }
  202. static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
  203. {
  204. return *get_fuse_conn_super_p(sb);
  205. }
  206. static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
  207. {
  208. return get_fuse_conn_super(inode->i_sb);
  209. }
  210. static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
  211. {
  212. return container_of(inode, struct fuse_inode, inode);
  213. }
  214. static inline u64 get_node_id(struct inode *inode)
  215. {
  216. return get_fuse_inode(inode)->nodeid;
  217. }
  218. /** Device operations */
  219. extern struct file_operations fuse_dev_operations;
  220. /**
  221. * This is the single global spinlock which protects FUSE's structures
  222. *
  223. * The following data is protected by this lock:
  224. *
  225. * - the private_data field of the device file
  226. * - the s_fs_info field of the super block
  227. * - unused_list, pending, processing lists in fuse_conn
  228. * - background list in fuse_conn
  229. * - the unique request ID counter reqctr in fuse_conn
  230. * - the sb (super_block) field in fuse_conn
  231. * - the file (device file) field in fuse_conn
  232. */
  233. extern spinlock_t fuse_lock;
  234. /**
  235. * Get a filled in inode
  236. */
  237. struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
  238. int generation, struct fuse_attr *attr);
  239. /**
  240. * Send FORGET command
  241. */
  242. void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
  243. unsigned long nodeid, u64 nlookup);
  244. /**
  245. * Initialise file operations on a regular file
  246. */
  247. void fuse_init_file_inode(struct inode *inode);
  248. /**
  249. * Initialise inode operations on regular files and special files
  250. */
  251. void fuse_init_common(struct inode *inode);
  252. /**
  253. * Initialise inode and file operations on a directory
  254. */
  255. void fuse_init_dir(struct inode *inode);
  256. /**
  257. * Initialise inode operations on a symlink
  258. */
  259. void fuse_init_symlink(struct inode *inode);
  260. /**
  261. * Change attributes of an inode
  262. */
  263. void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr);
  264. /**
  265. * Check if the connection can be released, and if yes, then free the
  266. * connection structure
  267. */
  268. void fuse_release_conn(struct fuse_conn *fc);
  269. /**
  270. * Initialize the client device
  271. */
  272. int fuse_dev_init(void);
  273. /**
  274. * Cleanup the client device
  275. */
  276. void fuse_dev_cleanup(void);
  277. /**
  278. * Allocate a request
  279. */
  280. struct fuse_req *fuse_request_alloc(void);
  281. /**
  282. * Free a request
  283. */
  284. void fuse_request_free(struct fuse_req *req);
  285. /**
  286. * Reinitialize a request, the preallocated flag is left unmodified
  287. */
  288. void fuse_reset_request(struct fuse_req *req);
  289. /**
  290. * Reserve a preallocated request
  291. */
  292. struct fuse_req *fuse_get_request(struct fuse_conn *fc);
  293. /**
  294. * Reserve a preallocated request, only interruptible by SIGKILL
  295. */
  296. struct fuse_req *fuse_get_request_nonint(struct fuse_conn *fc);
  297. /**
  298. * Decrement reference count of a request. If count goes to zero put
  299. * on unused list (preallocated) or free reqest (not preallocated).
  300. */
  301. void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
  302. /**
  303. * Send a request (synchronous, interruptible)
  304. */
  305. void request_send(struct fuse_conn *fc, struct fuse_req *req);
  306. /**
  307. * Send a request (synchronous, non-interruptible except by SIGKILL)
  308. */
  309. void request_send_nonint(struct fuse_conn *fc, struct fuse_req *req);
  310. /**
  311. * Send a request with no reply
  312. */
  313. void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
  314. /**
  315. * Send a request in the background
  316. */
  317. void request_send_background(struct fuse_conn *fc, struct fuse_req *req);
  318. /**
  319. * Release inodes and file assiciated with background request
  320. */
  321. void fuse_release_background(struct fuse_req *req);
  322. /**
  323. * Get the attributes of a file
  324. */
  325. int fuse_do_getattr(struct inode *inode);
  326. /**
  327. * Invalidate inode attributes
  328. */
  329. void fuse_invalidate_attr(struct inode *inode);
  330. /**
  331. * Send the INIT message
  332. */
  333. void fuse_send_init(struct fuse_conn *fc);