fuse_i.h 15 KB

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