fuse_i.h 10 KB

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