fuse_i.h 18 KB

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