fuse_i.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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/mount.h>
  10. #include <linux/wait.h>
  11. #include <linux/list.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/mm.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/mutex.h>
  16. /** Max number of pages that can be used in a single read request */
  17. #define FUSE_MAX_PAGES_PER_REQ 32
  18. /** Maximum number of outstanding background requests */
  19. #define FUSE_MAX_BACKGROUND 10
  20. /** It could be as large as PATH_MAX, but would that have any uses? */
  21. #define FUSE_NAME_MAX 1024
  22. /** Number of dentries for each connection in the control filesystem */
  23. #define FUSE_CTL_NUM_DENTRIES 3
  24. /** If the FUSE_DEFAULT_PERMISSIONS flag is given, the filesystem
  25. module will check permissions based on the file mode. Otherwise no
  26. permission checking is done in the kernel */
  27. #define FUSE_DEFAULT_PERMISSIONS (1 << 0)
  28. /** If the FUSE_ALLOW_OTHER flag is given, then not only the user
  29. doing the mount will be allowed to access the filesystem */
  30. #define FUSE_ALLOW_OTHER (1 << 1)
  31. /** List of active connections */
  32. extern struct list_head fuse_conn_list;
  33. /** Global mutex protecting fuse_conn_list and the control filesystem */
  34. extern struct mutex fuse_mutex;
  35. /** FUSE inode */
  36. struct fuse_inode {
  37. /** Inode data */
  38. struct inode inode;
  39. /** Unique ID, which identifies the inode between userspace
  40. * and kernel */
  41. u64 nodeid;
  42. /** Number of lookups on this inode */
  43. u64 nlookup;
  44. /** The request used for sending the FORGET message */
  45. struct fuse_req *forget_req;
  46. /** Time in jiffies until the file attributes are valid */
  47. u64 i_time;
  48. };
  49. /** FUSE specific file data */
  50. struct fuse_file {
  51. /** Request reserved for flush and release */
  52. struct fuse_req *reserved_req;
  53. /** File handle used by userspace */
  54. u64 fh;
  55. };
  56. /** One input argument of a request */
  57. struct fuse_in_arg {
  58. unsigned size;
  59. const void *value;
  60. };
  61. /** The request input */
  62. struct fuse_in {
  63. /** The request header */
  64. struct fuse_in_header h;
  65. /** True if the data for the last argument is in req->pages */
  66. unsigned argpages:1;
  67. /** Number of arguments */
  68. unsigned numargs;
  69. /** Array of arguments */
  70. struct fuse_in_arg args[3];
  71. };
  72. /** One output argument of a request */
  73. struct fuse_arg {
  74. unsigned size;
  75. void *value;
  76. };
  77. /** The request output */
  78. struct fuse_out {
  79. /** Header returned from userspace */
  80. struct fuse_out_header h;
  81. /*
  82. * The following bitfields are not changed during the request
  83. * processing
  84. */
  85. /** Last argument is variable length (can be shorter than
  86. arg->size) */
  87. unsigned argvar:1;
  88. /** Last argument is a list of pages to copy data to */
  89. unsigned argpages:1;
  90. /** Zero partially or not copied pages */
  91. unsigned page_zeroing:1;
  92. /** Number or arguments */
  93. unsigned numargs;
  94. /** Array of arguments */
  95. struct fuse_arg args[3];
  96. };
  97. /** The request state */
  98. enum fuse_req_state {
  99. FUSE_REQ_INIT = 0,
  100. FUSE_REQ_PENDING,
  101. FUSE_REQ_READING,
  102. FUSE_REQ_SENT,
  103. FUSE_REQ_WRITING,
  104. FUSE_REQ_FINISHED
  105. };
  106. struct fuse_conn;
  107. /**
  108. * A request to the client
  109. */
  110. struct fuse_req {
  111. /** This can be on either pending processing or io lists in
  112. fuse_conn */
  113. struct list_head list;
  114. /** Entry on the interrupts list */
  115. struct list_head intr_entry;
  116. /** refcount */
  117. atomic_t count;
  118. /** Unique ID for the interrupt request */
  119. u64 intr_unique;
  120. /*
  121. * The following bitfields are either set once before the
  122. * request is queued or setting/clearing them is protected by
  123. * fuse_conn->lock
  124. */
  125. /** True if the request has reply */
  126. unsigned isreply:1;
  127. /** Force sending of the request even if interrupted */
  128. unsigned force:1;
  129. /** The request was aborted */
  130. unsigned aborted:1;
  131. /** Request is sent in the background */
  132. unsigned background:1;
  133. /** The request has been interrupted */
  134. unsigned interrupted:1;
  135. /** Data is being copied to/from the request */
  136. unsigned locked:1;
  137. /** Request is counted as "waiting" */
  138. unsigned waiting:1;
  139. /** State of the request */
  140. enum fuse_req_state state;
  141. /** The request input */
  142. struct fuse_in in;
  143. /** The request output */
  144. struct fuse_out out;
  145. /** Used to wake up the task waiting for completion of request*/
  146. wait_queue_head_t waitq;
  147. /** Data for asynchronous requests */
  148. union {
  149. struct fuse_forget_in forget_in;
  150. struct fuse_release_in release_in;
  151. struct fuse_init_in init_in;
  152. struct fuse_init_out init_out;
  153. struct fuse_read_in read_in;
  154. struct fuse_lk_in lk_in;
  155. } misc;
  156. /** page vector */
  157. struct page *pages[FUSE_MAX_PAGES_PER_REQ];
  158. /** number of pages in vector */
  159. unsigned num_pages;
  160. /** offset of data on first page */
  161. unsigned page_offset;
  162. /** File used in the request (or NULL) */
  163. struct file *file;
  164. /** vfsmount used in release */
  165. struct vfsmount *vfsmount;
  166. /** dentry used in release */
  167. struct dentry *dentry;
  168. /** Request completion callback */
  169. void (*end)(struct fuse_conn *, struct fuse_req *);
  170. /** Request is stolen from fuse_file->reserved_req */
  171. struct file *stolen_file;
  172. };
  173. /**
  174. * A Fuse connection.
  175. *
  176. * This structure is created, when the filesystem is mounted, and is
  177. * destroyed, when the client device is closed and the filesystem is
  178. * unmounted.
  179. */
  180. struct fuse_conn {
  181. /** Lock protecting accessess to members of this structure */
  182. spinlock_t lock;
  183. /** Mutex protecting against directory alias creation */
  184. struct mutex inst_mutex;
  185. /** Refcount */
  186. atomic_t count;
  187. /** The user id for this mount */
  188. uid_t user_id;
  189. /** The group id for this mount */
  190. gid_t group_id;
  191. /** The fuse mount flags for this mount */
  192. unsigned flags;
  193. /** Maximum read size */
  194. unsigned max_read;
  195. /** Maximum write size */
  196. unsigned max_write;
  197. /** Readers of the connection are waiting on this */
  198. wait_queue_head_t waitq;
  199. /** The list of pending requests */
  200. struct list_head pending;
  201. /** The list of requests being processed */
  202. struct list_head processing;
  203. /** The list of requests under I/O */
  204. struct list_head io;
  205. /** Number of requests currently in the background */
  206. unsigned num_background;
  207. /** Pending interrupts */
  208. struct list_head interrupts;
  209. /** Flag indicating if connection is blocked. This will be
  210. the case before the INIT reply is received, and if there
  211. are too many outstading backgrounds requests */
  212. int blocked;
  213. /** waitq for blocked connection */
  214. wait_queue_head_t blocked_waitq;
  215. /** The next unique request id */
  216. u64 reqctr;
  217. /** Connection established, cleared on umount, connection
  218. abort and device release */
  219. unsigned connected;
  220. /** Connection failed (version mismatch). Cannot race with
  221. setting other bitfields since it is only set once in INIT
  222. reply, before any other request, and never cleared */
  223. unsigned conn_error : 1;
  224. /** Do readpages asynchronously? Only set in INIT */
  225. unsigned async_read : 1;
  226. /*
  227. * The following bitfields are only for optimization purposes
  228. * and hence races in setting them will not cause malfunction
  229. */
  230. /** Is fsync not implemented by fs? */
  231. unsigned no_fsync : 1;
  232. /** Is fsyncdir not implemented by fs? */
  233. unsigned no_fsyncdir : 1;
  234. /** Is flush not implemented by fs? */
  235. unsigned no_flush : 1;
  236. /** Is setxattr not implemented by fs? */
  237. unsigned no_setxattr : 1;
  238. /** Is getxattr not implemented by fs? */
  239. unsigned no_getxattr : 1;
  240. /** Is listxattr not implemented by fs? */
  241. unsigned no_listxattr : 1;
  242. /** Is removexattr not implemented by fs? */
  243. unsigned no_removexattr : 1;
  244. /** Are file locking primitives not implemented by fs? */
  245. unsigned no_lock : 1;
  246. /** Is access not implemented by fs? */
  247. unsigned no_access : 1;
  248. /** Is create not implemented by fs? */
  249. unsigned no_create : 1;
  250. /** Is interrupt not implemented by fs? */
  251. unsigned no_interrupt : 1;
  252. /** The number of requests waiting for completion */
  253. atomic_t num_waiting;
  254. /** Negotiated minor version */
  255. unsigned minor;
  256. /** Backing dev info */
  257. struct backing_dev_info bdi;
  258. /** Entry on the fuse_conn_list */
  259. struct list_head entry;
  260. /** Unique ID */
  261. u64 id;
  262. /** Dentries in the control filesystem */
  263. struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES];
  264. /** number of dentries used in the above array */
  265. int ctl_ndents;
  266. /** O_ASYNC requests */
  267. struct fasync_struct *fasync;
  268. /** Key for lock owner ID scrambling */
  269. u32 scramble_key[4];
  270. };
  271. static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
  272. {
  273. return sb->s_fs_info;
  274. }
  275. static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
  276. {
  277. return get_fuse_conn_super(inode->i_sb);
  278. }
  279. static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
  280. {
  281. return container_of(inode, struct fuse_inode, inode);
  282. }
  283. static inline u64 get_node_id(struct inode *inode)
  284. {
  285. return get_fuse_inode(inode)->nodeid;
  286. }
  287. /** Device operations */
  288. extern const struct file_operations fuse_dev_operations;
  289. /**
  290. * Get a filled in inode
  291. */
  292. struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
  293. int generation, struct fuse_attr *attr);
  294. /**
  295. * Send FORGET command
  296. */
  297. void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
  298. unsigned long nodeid, u64 nlookup);
  299. /**
  300. * Initialize READ or READDIR request
  301. */
  302. void fuse_read_fill(struct fuse_req *req, struct file *file,
  303. struct inode *inode, loff_t pos, size_t count, int opcode);
  304. /**
  305. * Send OPEN or OPENDIR request
  306. */
  307. int fuse_open_common(struct inode *inode, struct file *file, int isdir);
  308. struct fuse_file *fuse_file_alloc(void);
  309. void fuse_file_free(struct fuse_file *ff);
  310. void fuse_finish_open(struct inode *inode, struct file *file,
  311. struct fuse_file *ff, struct fuse_open_out *outarg);
  312. /** */
  313. struct fuse_req *fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags,
  314. int opcode);
  315. /**
  316. * Send RELEASE or RELEASEDIR request
  317. */
  318. int fuse_release_common(struct inode *inode, struct file *file, int isdir);
  319. /**
  320. * Send FSYNC or FSYNCDIR request
  321. */
  322. int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
  323. int isdir);
  324. /**
  325. * Initialize file operations on a regular file
  326. */
  327. void fuse_init_file_inode(struct inode *inode);
  328. /**
  329. * Initialize inode operations on regular files and special files
  330. */
  331. void fuse_init_common(struct inode *inode);
  332. /**
  333. * Initialize inode and file operations on a directory
  334. */
  335. void fuse_init_dir(struct inode *inode);
  336. /**
  337. * Initialize inode operations on a symlink
  338. */
  339. void fuse_init_symlink(struct inode *inode);
  340. /**
  341. * Change attributes of an inode
  342. */
  343. void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr);
  344. /**
  345. * Initialize the client device
  346. */
  347. int fuse_dev_init(void);
  348. /**
  349. * Cleanup the client device
  350. */
  351. void fuse_dev_cleanup(void);
  352. int fuse_ctl_init(void);
  353. void fuse_ctl_cleanup(void);
  354. /**
  355. * Allocate a request
  356. */
  357. struct fuse_req *fuse_request_alloc(void);
  358. /**
  359. * Free a request
  360. */
  361. void fuse_request_free(struct fuse_req *req);
  362. /**
  363. * Get a request, may fail with -ENOMEM
  364. */
  365. struct fuse_req *fuse_get_req(struct fuse_conn *fc);
  366. /**
  367. * Gets a requests for a file operation, always succeeds
  368. */
  369. struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file);
  370. /**
  371. * Decrement reference count of a request. If count goes to zero free
  372. * the request.
  373. */
  374. void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
  375. /**
  376. * Send a request (synchronous)
  377. */
  378. void request_send(struct fuse_conn *fc, struct fuse_req *req);
  379. /**
  380. * Send a request with no reply
  381. */
  382. void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
  383. /**
  384. * Send a request in the background
  385. */
  386. void request_send_background(struct fuse_conn *fc, struct fuse_req *req);
  387. /* Abort all requests */
  388. void fuse_abort_conn(struct fuse_conn *fc);
  389. /**
  390. * Get the attributes of a file
  391. */
  392. int fuse_do_getattr(struct inode *inode);
  393. /**
  394. * Invalidate inode attributes
  395. */
  396. void fuse_invalidate_attr(struct inode *inode);
  397. /**
  398. * Acquire reference to fuse_conn
  399. */
  400. struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
  401. /**
  402. * Release reference to fuse_conn
  403. */
  404. void fuse_conn_put(struct fuse_conn *fc);
  405. /**
  406. * Add connection to control filesystem
  407. */
  408. int fuse_ctl_add_conn(struct fuse_conn *fc);
  409. /**
  410. * Remove connection from control filesystem
  411. */
  412. void fuse_ctl_remove_conn(struct fuse_conn *fc);