fuse_i.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2008 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. #ifndef _FS_FUSE_I_H
  8. #define _FS_FUSE_I_H
  9. #include <linux/fuse.h>
  10. #include <linux/fs.h>
  11. #include <linux/mount.h>
  12. #include <linux/wait.h>
  13. #include <linux/list.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/mm.h>
  16. #include <linux/backing-dev.h>
  17. #include <linux/mutex.h>
  18. #include <linux/rwsem.h>
  19. #include <linux/rbtree.h>
  20. #include <linux/poll.h>
  21. /** Max number of pages that can be used in a single read request */
  22. #define FUSE_MAX_PAGES_PER_REQ 32
  23. /** Maximum number of outstanding background requests */
  24. #define FUSE_MAX_BACKGROUND 12
  25. /** Congestion starts at 75% of maximum */
  26. #define FUSE_CONGESTION_THRESHOLD (FUSE_MAX_BACKGROUND * 75 / 100)
  27. /** Bias for fi->writectr, meaning new writepages must not be sent */
  28. #define FUSE_NOWRITE INT_MIN
  29. /** It could be as large as PATH_MAX, but would that have any uses? */
  30. #define FUSE_NAME_MAX 1024
  31. /** Number of dentries for each connection in the control filesystem */
  32. #define FUSE_CTL_NUM_DENTRIES 3
  33. /** If the FUSE_DEFAULT_PERMISSIONS flag is given, the filesystem
  34. module will check permissions based on the file mode. Otherwise no
  35. permission checking is done in the kernel */
  36. #define FUSE_DEFAULT_PERMISSIONS (1 << 0)
  37. /** If the FUSE_ALLOW_OTHER flag is given, then not only the user
  38. doing the mount will be allowed to access the filesystem */
  39. #define FUSE_ALLOW_OTHER (1 << 1)
  40. /** List of active connections */
  41. extern struct list_head fuse_conn_list;
  42. /** Global mutex protecting fuse_conn_list and the control filesystem */
  43. extern struct mutex fuse_mutex;
  44. /** FUSE inode */
  45. struct fuse_inode {
  46. /** Inode data */
  47. struct inode inode;
  48. /** Unique ID, which identifies the inode between userspace
  49. * and kernel */
  50. u64 nodeid;
  51. /** Number of lookups on this inode */
  52. u64 nlookup;
  53. /** The request used for sending the FORGET message */
  54. struct fuse_req *forget_req;
  55. /** Time in jiffies until the file attributes are valid */
  56. u64 i_time;
  57. /** The sticky bit in inode->i_mode may have been removed, so
  58. preserve the original mode */
  59. mode_t orig_i_mode;
  60. /** Version of last attribute change */
  61. u64 attr_version;
  62. /** Files usable in writepage. Protected by fc->lock */
  63. struct list_head write_files;
  64. /** Writepages pending on truncate or fsync */
  65. struct list_head queued_writes;
  66. /** Number of sent writes, a negative bias (FUSE_NOWRITE)
  67. * means more writes are blocked */
  68. int writectr;
  69. /** Waitq for writepage completion */
  70. wait_queue_head_t page_waitq;
  71. /** List of writepage requestst (pending or sent) */
  72. struct list_head writepages;
  73. };
  74. /** FUSE specific file data */
  75. struct fuse_file {
  76. /** Request reserved for flush and release */
  77. struct fuse_req *reserved_req;
  78. /** Kernel file handle guaranteed to be unique */
  79. u64 kh;
  80. /** File handle used by userspace */
  81. u64 fh;
  82. /** Refcount */
  83. atomic_t count;
  84. /** Entry on inode's write_files list */
  85. struct list_head write_entry;
  86. /** RB node to be linked on fuse_conn->polled_files */
  87. struct rb_node polled_node;
  88. /** Wait queue head for poll */
  89. wait_queue_head_t poll_wait;
  90. };
  91. /** One input argument of a request */
  92. struct fuse_in_arg {
  93. unsigned size;
  94. const void *value;
  95. };
  96. /** The request input */
  97. struct fuse_in {
  98. /** The request header */
  99. struct fuse_in_header h;
  100. /** True if the data for the last argument is in req->pages */
  101. unsigned argpages:1;
  102. /** Number of arguments */
  103. unsigned numargs;
  104. /** Array of arguments */
  105. struct fuse_in_arg args[3];
  106. };
  107. /** One output argument of a request */
  108. struct fuse_arg {
  109. unsigned size;
  110. void *value;
  111. };
  112. /** The request output */
  113. struct fuse_out {
  114. /** Header returned from userspace */
  115. struct fuse_out_header h;
  116. /*
  117. * The following bitfields are not changed during the request
  118. * processing
  119. */
  120. /** Last argument is variable length (can be shorter than
  121. arg->size) */
  122. unsigned argvar:1;
  123. /** Last argument is a list of pages to copy data to */
  124. unsigned argpages:1;
  125. /** Zero partially or not copied pages */
  126. unsigned page_zeroing:1;
  127. /** Number or arguments */
  128. unsigned numargs;
  129. /** Array of arguments */
  130. struct fuse_arg args[3];
  131. };
  132. /** The request state */
  133. enum fuse_req_state {
  134. FUSE_REQ_INIT = 0,
  135. FUSE_REQ_PENDING,
  136. FUSE_REQ_READING,
  137. FUSE_REQ_SENT,
  138. FUSE_REQ_WRITING,
  139. FUSE_REQ_FINISHED
  140. };
  141. struct fuse_conn;
  142. /**
  143. * A request to the client
  144. */
  145. struct fuse_req {
  146. /** This can be on either pending processing or io lists in
  147. fuse_conn */
  148. struct list_head list;
  149. /** Entry on the interrupts list */
  150. struct list_head intr_entry;
  151. /** refcount */
  152. atomic_t count;
  153. /** Unique ID for the interrupt request */
  154. u64 intr_unique;
  155. /*
  156. * The following bitfields are either set once before the
  157. * request is queued or setting/clearing them is protected by
  158. * fuse_conn->lock
  159. */
  160. /** True if the request has reply */
  161. unsigned isreply:1;
  162. /** Force sending of the request even if interrupted */
  163. unsigned force:1;
  164. /** The request was aborted */
  165. unsigned aborted:1;
  166. /** Request is sent in the background */
  167. unsigned background:1;
  168. /** The request has been interrupted */
  169. unsigned interrupted:1;
  170. /** Data is being copied to/from the request */
  171. unsigned locked:1;
  172. /** Request is counted as "waiting" */
  173. unsigned waiting:1;
  174. /** State of the request */
  175. enum fuse_req_state state;
  176. /** The request input */
  177. struct fuse_in in;
  178. /** The request output */
  179. struct fuse_out out;
  180. /** Used to wake up the task waiting for completion of request*/
  181. wait_queue_head_t waitq;
  182. /** Data for asynchronous requests */
  183. union {
  184. struct fuse_forget_in forget_in;
  185. struct {
  186. struct fuse_release_in in;
  187. struct vfsmount *vfsmount;
  188. struct dentry *dentry;
  189. } release;
  190. struct fuse_init_in init_in;
  191. struct fuse_init_out init_out;
  192. struct {
  193. struct fuse_read_in in;
  194. u64 attr_ver;
  195. } read;
  196. struct {
  197. struct fuse_write_in in;
  198. struct fuse_write_out out;
  199. } write;
  200. struct fuse_lk_in lk_in;
  201. } misc;
  202. /** page vector */
  203. struct page *pages[FUSE_MAX_PAGES_PER_REQ];
  204. /** number of pages in vector */
  205. unsigned num_pages;
  206. /** offset of data on first page */
  207. unsigned page_offset;
  208. /** File used in the request (or NULL) */
  209. struct fuse_file *ff;
  210. /** Inode used in the request or NULL */
  211. struct inode *inode;
  212. /** Link on fi->writepages */
  213. struct list_head writepages_entry;
  214. /** Request completion callback */
  215. void (*end)(struct fuse_conn *, struct fuse_req *);
  216. /** Request is stolen from fuse_file->reserved_req */
  217. struct file *stolen_file;
  218. };
  219. /**
  220. * A Fuse connection.
  221. *
  222. * This structure is created, when the filesystem is mounted, and is
  223. * destroyed, when the client device is closed and the filesystem is
  224. * unmounted.
  225. */
  226. struct fuse_conn {
  227. /** Lock protecting accessess to members of this structure */
  228. spinlock_t lock;
  229. /** Mutex protecting against directory alias creation */
  230. struct mutex inst_mutex;
  231. /** Refcount */
  232. atomic_t count;
  233. /** The user id for this mount */
  234. uid_t user_id;
  235. /** The group id for this mount */
  236. gid_t group_id;
  237. /** The fuse mount flags for this mount */
  238. unsigned flags;
  239. /** Maximum read size */
  240. unsigned max_read;
  241. /** Maximum write size */
  242. unsigned max_write;
  243. /** Readers of the connection are waiting on this */
  244. wait_queue_head_t waitq;
  245. /** The list of pending requests */
  246. struct list_head pending;
  247. /** The list of requests being processed */
  248. struct list_head processing;
  249. /** The list of requests under I/O */
  250. struct list_head io;
  251. /** The next unique kernel file handle */
  252. u64 khctr;
  253. /** rbtree of fuse_files waiting for poll events indexed by ph */
  254. struct rb_root polled_files;
  255. /** Number of requests currently in the background */
  256. unsigned num_background;
  257. /** Number of background requests currently queued for userspace */
  258. unsigned active_background;
  259. /** The list of background requests set aside for later queuing */
  260. struct list_head bg_queue;
  261. /** Pending interrupts */
  262. struct list_head interrupts;
  263. /** Flag indicating if connection is blocked. This will be
  264. the case before the INIT reply is received, and if there
  265. are too many outstading backgrounds requests */
  266. int blocked;
  267. /** waitq for blocked connection */
  268. wait_queue_head_t blocked_waitq;
  269. /** waitq for reserved requests */
  270. wait_queue_head_t reserved_req_waitq;
  271. /** The next unique request id */
  272. u64 reqctr;
  273. /** Connection established, cleared on umount, connection
  274. abort and device release */
  275. unsigned connected;
  276. /** Connection failed (version mismatch). Cannot race with
  277. setting other bitfields since it is only set once in INIT
  278. reply, before any other request, and never cleared */
  279. unsigned conn_error:1;
  280. /** Connection successful. Only set in INIT */
  281. unsigned conn_init:1;
  282. /** Do readpages asynchronously? Only set in INIT */
  283. unsigned async_read:1;
  284. /** Do not send separate SETATTR request before open(O_TRUNC) */
  285. unsigned atomic_o_trunc:1;
  286. /** Filesystem supports NFS exporting. Only set in INIT */
  287. unsigned export_support:1;
  288. /*
  289. * The following bitfields are only for optimization purposes
  290. * and hence races in setting them will not cause malfunction
  291. */
  292. /** Is fsync not implemented by fs? */
  293. unsigned no_fsync:1;
  294. /** Is fsyncdir not implemented by fs? */
  295. unsigned no_fsyncdir:1;
  296. /** Is flush not implemented by fs? */
  297. unsigned no_flush:1;
  298. /** Is setxattr not implemented by fs? */
  299. unsigned no_setxattr:1;
  300. /** Is getxattr not implemented by fs? */
  301. unsigned no_getxattr:1;
  302. /** Is listxattr not implemented by fs? */
  303. unsigned no_listxattr:1;
  304. /** Is removexattr not implemented by fs? */
  305. unsigned no_removexattr:1;
  306. /** Are file locking primitives not implemented by fs? */
  307. unsigned no_lock:1;
  308. /** Is access not implemented by fs? */
  309. unsigned no_access:1;
  310. /** Is create not implemented by fs? */
  311. unsigned no_create:1;
  312. /** Is interrupt not implemented by fs? */
  313. unsigned no_interrupt:1;
  314. /** Is bmap not implemented by fs? */
  315. unsigned no_bmap:1;
  316. /** Is poll not implemented by fs? */
  317. unsigned no_poll:1;
  318. /** Do multi-page cached writes */
  319. unsigned big_writes:1;
  320. /** The number of requests waiting for completion */
  321. atomic_t num_waiting;
  322. /** Negotiated minor version */
  323. unsigned minor;
  324. /** Backing dev info */
  325. struct backing_dev_info bdi;
  326. /** Entry on the fuse_conn_list */
  327. struct list_head entry;
  328. /** Device ID from super block */
  329. dev_t dev;
  330. /** Dentries in the control filesystem */
  331. struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES];
  332. /** number of dentries used in the above array */
  333. int ctl_ndents;
  334. /** O_ASYNC requests */
  335. struct fasync_struct *fasync;
  336. /** Key for lock owner ID scrambling */
  337. u32 scramble_key[4];
  338. /** Reserved request for the DESTROY message */
  339. struct fuse_req *destroy_req;
  340. /** Version counter for attribute changes */
  341. u64 attr_version;
  342. /** Called on final put */
  343. void (*release)(struct fuse_conn *);
  344. };
  345. static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
  346. {
  347. return sb->s_fs_info;
  348. }
  349. static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
  350. {
  351. return get_fuse_conn_super(inode->i_sb);
  352. }
  353. static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
  354. {
  355. return container_of(inode, struct fuse_inode, inode);
  356. }
  357. static inline u64 get_node_id(struct inode *inode)
  358. {
  359. return get_fuse_inode(inode)->nodeid;
  360. }
  361. /** Device operations */
  362. extern const struct file_operations fuse_dev_operations;
  363. extern const struct dentry_operations fuse_dentry_operations;
  364. /**
  365. * Get a filled in inode
  366. */
  367. struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
  368. int generation, struct fuse_attr *attr,
  369. u64 attr_valid, u64 attr_version);
  370. int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
  371. struct fuse_entry_out *outarg, struct inode **inode);
  372. /**
  373. * Send FORGET command
  374. */
  375. void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
  376. u64 nodeid, u64 nlookup);
  377. /**
  378. * Initialize READ or READDIR request
  379. */
  380. void fuse_read_fill(struct fuse_req *req, struct file *file,
  381. struct inode *inode, loff_t pos, size_t count, int opcode);
  382. /**
  383. * Send OPEN or OPENDIR request
  384. */
  385. int fuse_open_common(struct inode *inode, struct file *file, int isdir);
  386. struct fuse_file *fuse_file_alloc(struct fuse_conn *fc);
  387. void fuse_file_free(struct fuse_file *ff);
  388. void fuse_finish_open(struct inode *inode, struct file *file,
  389. struct fuse_file *ff, struct fuse_open_out *outarg);
  390. /** Fill in ff->reserved_req with a RELEASE request */
  391. void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode);
  392. /**
  393. * Send RELEASE or RELEASEDIR request
  394. */
  395. int fuse_release_common(struct inode *inode, struct file *file, int isdir);
  396. /**
  397. * Send FSYNC or FSYNCDIR request
  398. */
  399. int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
  400. int isdir);
  401. /**
  402. * Notify poll wakeup
  403. */
  404. int fuse_notify_poll_wakeup(struct fuse_conn *fc,
  405. struct fuse_notify_poll_wakeup_out *outarg);
  406. /**
  407. * Initialize file operations on a regular file
  408. */
  409. void fuse_init_file_inode(struct inode *inode);
  410. /**
  411. * Initialize inode operations on regular files and special files
  412. */
  413. void fuse_init_common(struct inode *inode);
  414. /**
  415. * Initialize inode and file operations on a directory
  416. */
  417. void fuse_init_dir(struct inode *inode);
  418. /**
  419. * Initialize inode operations on a symlink
  420. */
  421. void fuse_init_symlink(struct inode *inode);
  422. /**
  423. * Change attributes of an inode
  424. */
  425. void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
  426. u64 attr_valid, u64 attr_version);
  427. void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
  428. u64 attr_valid);
  429. void fuse_truncate(struct address_space *mapping, loff_t offset);
  430. /**
  431. * Initialize the client device
  432. */
  433. int fuse_dev_init(void);
  434. /**
  435. * Cleanup the client device
  436. */
  437. void fuse_dev_cleanup(void);
  438. int fuse_ctl_init(void);
  439. void fuse_ctl_cleanup(void);
  440. /**
  441. * Allocate a request
  442. */
  443. struct fuse_req *fuse_request_alloc(void);
  444. struct fuse_req *fuse_request_alloc_nofs(void);
  445. /**
  446. * Free a request
  447. */
  448. void fuse_request_free(struct fuse_req *req);
  449. /**
  450. * Get a request, may fail with -ENOMEM
  451. */
  452. struct fuse_req *fuse_get_req(struct fuse_conn *fc);
  453. /**
  454. * Gets a requests for a file operation, always succeeds
  455. */
  456. struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file);
  457. /**
  458. * Decrement reference count of a request. If count goes to zero free
  459. * the request.
  460. */
  461. void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
  462. /**
  463. * Send a request (synchronous)
  464. */
  465. void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req);
  466. /**
  467. * Send a request with no reply
  468. */
  469. void fuse_request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
  470. /**
  471. * Send a request in the background
  472. */
  473. void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req);
  474. void fuse_request_send_background_locked(struct fuse_conn *fc,
  475. struct fuse_req *req);
  476. /* Abort all requests */
  477. void fuse_abort_conn(struct fuse_conn *fc);
  478. /**
  479. * Invalidate inode attributes
  480. */
  481. void fuse_invalidate_attr(struct inode *inode);
  482. void fuse_invalidate_entry_cache(struct dentry *entry);
  483. /**
  484. * Acquire reference to fuse_conn
  485. */
  486. struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
  487. /**
  488. * Initialize fuse_conn
  489. */
  490. int fuse_conn_init(struct fuse_conn *fc, struct super_block *sb);
  491. /**
  492. * Release reference to fuse_conn
  493. */
  494. void fuse_conn_put(struct fuse_conn *fc);
  495. /**
  496. * Add connection to control filesystem
  497. */
  498. int fuse_ctl_add_conn(struct fuse_conn *fc);
  499. /**
  500. * Remove connection from control filesystem
  501. */
  502. void fuse_ctl_remove_conn(struct fuse_conn *fc);
  503. /**
  504. * Is file type valid?
  505. */
  506. int fuse_valid_type(int m);
  507. /**
  508. * Is task allowed to perform filesystem operation?
  509. */
  510. int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task);
  511. u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id);
  512. int fuse_update_attributes(struct inode *inode, struct kstat *stat,
  513. struct file *file, bool *refreshed);
  514. void fuse_flush_writepages(struct inode *inode);
  515. void fuse_set_nowrite(struct inode *inode);
  516. void fuse_release_nowrite(struct inode *inode);
  517. u64 fuse_get_attr_version(struct fuse_conn *fc);
  518. #endif /* _FS_FUSE_I_H */