fuse_i.h 17 KB

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