fuse_i.h 10 KB

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