fuse_i.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2006 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. /** It could be as large as PATH_MAX, but would that have any uses? */
  18. #define FUSE_NAME_MAX 1024
  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. /*
  73. * The following bitfields are not changed during the request
  74. * processing
  75. */
  76. /** Last argument is variable length (can be shorter than
  77. arg->size) */
  78. unsigned argvar:1;
  79. /** Last argument is a list of pages to copy data to */
  80. unsigned argpages:1;
  81. /** Zero partially or not copied pages */
  82. unsigned page_zeroing:1;
  83. /** Number or arguments */
  84. unsigned numargs;
  85. /** Array of arguments */
  86. struct fuse_arg args[3];
  87. };
  88. /** The request state */
  89. enum fuse_req_state {
  90. FUSE_REQ_INIT = 0,
  91. FUSE_REQ_PENDING,
  92. FUSE_REQ_READING,
  93. FUSE_REQ_SENT,
  94. FUSE_REQ_FINISHED
  95. };
  96. struct fuse_conn;
  97. /**
  98. * A request to the client
  99. */
  100. struct fuse_req {
  101. /** This can be on either pending processing or io lists in
  102. fuse_conn */
  103. struct list_head list;
  104. /** Entry on the background list */
  105. struct list_head bg_entry;
  106. /** refcount */
  107. atomic_t count;
  108. /*
  109. * The following bitfields are either set once before the
  110. * request is queued or setting/clearing them is protected by
  111. * fuse_conn->lock
  112. */
  113. /** True if the request has reply */
  114. unsigned isreply:1;
  115. /** The request was interrupted */
  116. unsigned interrupted:1;
  117. /** Request is sent in the background */
  118. unsigned background:1;
  119. /** Data is being copied to/from the request */
  120. unsigned locked:1;
  121. /** State of the request */
  122. enum fuse_req_state state;
  123. /** The request input */
  124. struct fuse_in in;
  125. /** The request output */
  126. struct fuse_out out;
  127. /** Used to wake up the task waiting for completion of request*/
  128. wait_queue_head_t waitq;
  129. /** Data for asynchronous requests */
  130. union {
  131. struct fuse_forget_in forget_in;
  132. struct fuse_release_in release_in;
  133. struct fuse_init_in init_in;
  134. struct fuse_init_out init_out;
  135. struct fuse_read_in read_in;
  136. } misc;
  137. /** page vector */
  138. struct page *pages[FUSE_MAX_PAGES_PER_REQ];
  139. /** number of pages in vector */
  140. unsigned num_pages;
  141. /** offset of data on first page */
  142. unsigned page_offset;
  143. /** Inode used in the request */
  144. struct inode *inode;
  145. /** Second inode used in the request (or NULL) */
  146. struct inode *inode2;
  147. /** File used in the request (or NULL) */
  148. struct file *file;
  149. /** Request completion callback */
  150. void (*end)(struct fuse_conn *, struct fuse_req *);
  151. };
  152. /**
  153. * A Fuse connection.
  154. *
  155. * This structure is created, when the filesystem is mounted, and is
  156. * destroyed, when the client device is closed and the filesystem is
  157. * unmounted.
  158. */
  159. struct fuse_conn {
  160. /** Lock protecting accessess to members of this structure */
  161. spinlock_t lock;
  162. /** The user id for this mount */
  163. uid_t user_id;
  164. /** The group id for this mount */
  165. gid_t group_id;
  166. /** The fuse mount flags for this mount */
  167. unsigned flags;
  168. /** Maximum read size */
  169. unsigned max_read;
  170. /** Maximum write size */
  171. unsigned max_write;
  172. /** Readers of the connection are waiting on this */
  173. wait_queue_head_t waitq;
  174. /** The list of pending requests */
  175. struct list_head pending;
  176. /** The list of requests being processed */
  177. struct list_head processing;
  178. /** The list of requests under I/O */
  179. struct list_head io;
  180. /** Requests put in the background (RELEASE or any other
  181. interrupted request) */
  182. struct list_head background;
  183. /** RW semaphore for exclusion with fuse_put_super() */
  184. struct rw_semaphore sbput_sem;
  185. /** The next unique request id */
  186. u64 reqctr;
  187. /** Mount is active */
  188. unsigned mounted;
  189. /** Connection established, cleared on umount, connection
  190. abort and device release */
  191. unsigned connected;
  192. /** Connection failed (version mismatch). Cannot race with
  193. setting other bitfields since it is only set once in INIT
  194. reply, before any other request, and never cleared */
  195. unsigned conn_error : 1;
  196. /** Do readpages asynchronously? Only set in INIT */
  197. unsigned async_read : 1;
  198. /*
  199. * The following bitfields are only for optimization purposes
  200. * and hence races in setting them will not cause malfunction
  201. */
  202. /** Is fsync not implemented by fs? */
  203. unsigned no_fsync : 1;
  204. /** Is fsyncdir not implemented by fs? */
  205. unsigned no_fsyncdir : 1;
  206. /** Is flush not implemented by fs? */
  207. unsigned no_flush : 1;
  208. /** Is setxattr not implemented by fs? */
  209. unsigned no_setxattr : 1;
  210. /** Is getxattr not implemented by fs? */
  211. unsigned no_getxattr : 1;
  212. /** Is listxattr not implemented by fs? */
  213. unsigned no_listxattr : 1;
  214. /** Is removexattr not implemented by fs? */
  215. unsigned no_removexattr : 1;
  216. /** Is access not implemented by fs? */
  217. unsigned no_access : 1;
  218. /** Is create not implemented by fs? */
  219. unsigned no_create : 1;
  220. /** The number of requests waiting for completion */
  221. atomic_t num_waiting;
  222. /** Negotiated minor version */
  223. unsigned minor;
  224. /** Backing dev info */
  225. struct backing_dev_info bdi;
  226. /** kobject */
  227. struct kobject kobj;
  228. /** O_ASYNC requests */
  229. struct fasync_struct *fasync;
  230. };
  231. static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
  232. {
  233. return sb->s_fs_info;
  234. }
  235. static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
  236. {
  237. return get_fuse_conn_super(inode->i_sb);
  238. }
  239. static inline struct fuse_conn *get_fuse_conn_kobj(struct kobject *obj)
  240. {
  241. return container_of(obj, struct fuse_conn, kobj);
  242. }
  243. static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
  244. {
  245. return container_of(inode, struct fuse_inode, inode);
  246. }
  247. static inline u64 get_node_id(struct inode *inode)
  248. {
  249. return get_fuse_inode(inode)->nodeid;
  250. }
  251. /** Device operations */
  252. extern const struct file_operations fuse_dev_operations;
  253. /**
  254. * Get a filled in inode
  255. */
  256. struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
  257. int generation, struct fuse_attr *attr);
  258. /**
  259. * Send FORGET command
  260. */
  261. void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
  262. unsigned long nodeid, u64 nlookup);
  263. /**
  264. * Initialize READ or READDIR request
  265. */
  266. void fuse_read_fill(struct fuse_req *req, struct file *file,
  267. struct inode *inode, loff_t pos, size_t count, int opcode);
  268. /**
  269. * Send OPEN or OPENDIR request
  270. */
  271. int fuse_open_common(struct inode *inode, struct file *file, int isdir);
  272. struct fuse_file *fuse_file_alloc(void);
  273. void fuse_file_free(struct fuse_file *ff);
  274. void fuse_finish_open(struct inode *inode, struct file *file,
  275. struct fuse_file *ff, struct fuse_open_out *outarg);
  276. /**
  277. * Send a RELEASE request
  278. */
  279. void fuse_send_release(struct fuse_conn *fc, struct fuse_file *ff,
  280. u64 nodeid, struct inode *inode, int flags, int isdir);
  281. /**
  282. * Send RELEASE or RELEASEDIR request
  283. */
  284. int fuse_release_common(struct inode *inode, struct file *file, int isdir);
  285. /**
  286. * Send FSYNC or FSYNCDIR request
  287. */
  288. int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
  289. int isdir);
  290. /**
  291. * Initialize file operations on a regular file
  292. */
  293. void fuse_init_file_inode(struct inode *inode);
  294. /**
  295. * Initialize inode operations on regular files and special files
  296. */
  297. void fuse_init_common(struct inode *inode);
  298. /**
  299. * Initialize inode and file operations on a directory
  300. */
  301. void fuse_init_dir(struct inode *inode);
  302. /**
  303. * Initialize inode operations on a symlink
  304. */
  305. void fuse_init_symlink(struct inode *inode);
  306. /**
  307. * Change attributes of an inode
  308. */
  309. void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr);
  310. /**
  311. * Initialize the client device
  312. */
  313. int fuse_dev_init(void);
  314. /**
  315. * Cleanup the client device
  316. */
  317. void fuse_dev_cleanup(void);
  318. /**
  319. * Allocate a request
  320. */
  321. struct fuse_req *fuse_request_alloc(void);
  322. /**
  323. * Free a request
  324. */
  325. void fuse_request_free(struct fuse_req *req);
  326. /**
  327. * Reinitialize a request, the preallocated flag is left unmodified
  328. */
  329. void fuse_reset_request(struct fuse_req *req);
  330. /**
  331. * Reserve a preallocated request
  332. */
  333. struct fuse_req *fuse_get_req(struct fuse_conn *fc);
  334. /**
  335. * Decrement reference count of a request. If count goes to zero free
  336. * the request.
  337. */
  338. void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
  339. /**
  340. * Send a request (synchronous)
  341. */
  342. void request_send(struct fuse_conn *fc, struct fuse_req *req);
  343. /**
  344. * Send a request with no reply
  345. */
  346. void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
  347. /**
  348. * Send a request in the background
  349. */
  350. void request_send_background(struct fuse_conn *fc, struct fuse_req *req);
  351. /**
  352. * Release inodes and file associated with background request
  353. */
  354. void fuse_release_background(struct fuse_conn *fc, struct fuse_req *req);
  355. /* Abort all requests */
  356. void fuse_abort_conn(struct fuse_conn *fc);
  357. /**
  358. * Get the attributes of a file
  359. */
  360. int fuse_do_getattr(struct inode *inode);
  361. /**
  362. * Invalidate inode attributes
  363. */
  364. void fuse_invalidate_attr(struct inode *inode);