fuse_i.h 11 KB

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