fuse_i.h 17 KB

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