fuse_i.h 15 KB

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