fuse_i.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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. /** The number of requests waiting for completion */
  330. atomic_t num_waiting;
  331. /** Negotiated minor version */
  332. unsigned minor;
  333. /** Backing dev info */
  334. struct backing_dev_info bdi;
  335. /** Entry on the fuse_conn_list */
  336. struct list_head entry;
  337. /** Device ID from super block */
  338. dev_t dev;
  339. /** Dentries in the control filesystem */
  340. struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES];
  341. /** number of dentries used in the above array */
  342. int ctl_ndents;
  343. /** O_ASYNC requests */
  344. struct fasync_struct *fasync;
  345. /** Key for lock owner ID scrambling */
  346. u32 scramble_key[4];
  347. /** Reserved request for the DESTROY message */
  348. struct fuse_req *destroy_req;
  349. /** Version counter for attribute changes */
  350. u64 attr_version;
  351. /** Called on final put */
  352. void (*release)(struct fuse_conn *);
  353. };
  354. static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
  355. {
  356. return sb->s_fs_info;
  357. }
  358. static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
  359. {
  360. return get_fuse_conn_super(inode->i_sb);
  361. }
  362. static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
  363. {
  364. return container_of(inode, struct fuse_inode, inode);
  365. }
  366. static inline u64 get_node_id(struct inode *inode)
  367. {
  368. return get_fuse_inode(inode)->nodeid;
  369. }
  370. /** Device operations */
  371. extern const struct file_operations fuse_dev_operations;
  372. extern const struct dentry_operations fuse_dentry_operations;
  373. /**
  374. * Get a filled in inode
  375. */
  376. struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
  377. int generation, struct fuse_attr *attr,
  378. u64 attr_valid, u64 attr_version);
  379. int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
  380. struct fuse_entry_out *outarg, struct inode **inode);
  381. /**
  382. * Send FORGET command
  383. */
  384. void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
  385. u64 nodeid, u64 nlookup);
  386. /**
  387. * Initialize READ or READDIR request
  388. */
  389. void fuse_read_fill(struct fuse_req *req, struct file *file,
  390. loff_t pos, size_t count, int opcode);
  391. /**
  392. * Send OPEN or OPENDIR request
  393. */
  394. int fuse_open_common(struct inode *inode, struct file *file, bool isdir);
  395. struct fuse_file *fuse_file_alloc(struct fuse_conn *fc);
  396. struct fuse_file *fuse_file_get(struct fuse_file *ff);
  397. void fuse_file_free(struct fuse_file *ff);
  398. void fuse_finish_open(struct inode *inode, struct file *file);
  399. void fuse_sync_release(struct fuse_file *ff, int flags);
  400. /**
  401. * Send RELEASE or RELEASEDIR request
  402. */
  403. void fuse_release_common(struct file *file, int opcode);
  404. /**
  405. * Send FSYNC or FSYNCDIR request
  406. */
  407. int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
  408. int isdir);
  409. /**
  410. * Notify poll wakeup
  411. */
  412. int fuse_notify_poll_wakeup(struct fuse_conn *fc,
  413. struct fuse_notify_poll_wakeup_out *outarg);
  414. /**
  415. * Initialize file operations on a regular file
  416. */
  417. void fuse_init_file_inode(struct inode *inode);
  418. /**
  419. * Initialize inode operations on regular files and special files
  420. */
  421. void fuse_init_common(struct inode *inode);
  422. /**
  423. * Initialize inode and file operations on a directory
  424. */
  425. void fuse_init_dir(struct inode *inode);
  426. /**
  427. * Initialize inode operations on a symlink
  428. */
  429. void fuse_init_symlink(struct inode *inode);
  430. /**
  431. * Change attributes of an inode
  432. */
  433. void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
  434. u64 attr_valid, u64 attr_version);
  435. void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
  436. u64 attr_valid);
  437. void fuse_truncate(struct address_space *mapping, loff_t offset);
  438. /**
  439. * Initialize the client device
  440. */
  441. int fuse_dev_init(void);
  442. /**
  443. * Cleanup the client device
  444. */
  445. void fuse_dev_cleanup(void);
  446. int fuse_ctl_init(void);
  447. void fuse_ctl_cleanup(void);
  448. /**
  449. * Allocate a request
  450. */
  451. struct fuse_req *fuse_request_alloc(void);
  452. struct fuse_req *fuse_request_alloc_nofs(void);
  453. /**
  454. * Free a request
  455. */
  456. void fuse_request_free(struct fuse_req *req);
  457. /**
  458. * Get a request, may fail with -ENOMEM
  459. */
  460. struct fuse_req *fuse_get_req(struct fuse_conn *fc);
  461. /**
  462. * Gets a requests for a file operation, always succeeds
  463. */
  464. struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file);
  465. /**
  466. * Decrement reference count of a request. If count goes to zero free
  467. * the request.
  468. */
  469. void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
  470. /**
  471. * Send a request (synchronous)
  472. */
  473. void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req);
  474. /**
  475. * Send a request with no reply
  476. */
  477. void fuse_request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
  478. /**
  479. * Send a request in the background
  480. */
  481. void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req);
  482. void fuse_request_send_background_locked(struct fuse_conn *fc,
  483. struct fuse_req *req);
  484. /* Abort all requests */
  485. void fuse_abort_conn(struct fuse_conn *fc);
  486. /**
  487. * Invalidate inode attributes
  488. */
  489. void fuse_invalidate_attr(struct inode *inode);
  490. void fuse_invalidate_entry_cache(struct dentry *entry);
  491. /**
  492. * Acquire reference to fuse_conn
  493. */
  494. struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
  495. void fuse_conn_kill(struct fuse_conn *fc);
  496. /**
  497. * Initialize fuse_conn
  498. */
  499. void fuse_conn_init(struct fuse_conn *fc);
  500. /**
  501. * Release reference to fuse_conn
  502. */
  503. void fuse_conn_put(struct fuse_conn *fc);
  504. /**
  505. * Add connection to control filesystem
  506. */
  507. int fuse_ctl_add_conn(struct fuse_conn *fc);
  508. /**
  509. * Remove connection from control filesystem
  510. */
  511. void fuse_ctl_remove_conn(struct fuse_conn *fc);
  512. /**
  513. * Is file type valid?
  514. */
  515. int fuse_valid_type(int m);
  516. /**
  517. * Is task allowed to perform filesystem operation?
  518. */
  519. int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task);
  520. u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id);
  521. int fuse_update_attributes(struct inode *inode, struct kstat *stat,
  522. struct file *file, bool *refreshed);
  523. void fuse_flush_writepages(struct inode *inode);
  524. void fuse_set_nowrite(struct inode *inode);
  525. void fuse_release_nowrite(struct inode *inode);
  526. u64 fuse_get_attr_version(struct fuse_conn *fc);
  527. int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
  528. bool isdir);
  529. ssize_t fuse_direct_io(struct file *file, const char __user *buf,
  530. size_t count, loff_t *ppos, int write);
  531. long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
  532. unsigned int flags);
  533. unsigned fuse_file_poll(struct file *file, poll_table *wait);
  534. int fuse_dev_release(struct inode *inode, struct file *file);
  535. #endif /* _FS_FUSE_I_H */