fuse_i.h 10 KB

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