fuse_i.h 15 KB

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