extent_io.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. #ifndef __EXTENTIO__
  2. #define __EXTENTIO__
  3. #include <linux/rbtree.h>
  4. #include "ulist.h"
  5. /* bits for the extent state */
  6. #define EXTENT_DIRTY (1U << 0)
  7. #define EXTENT_WRITEBACK (1U << 1)
  8. #define EXTENT_UPTODATE (1U << 2)
  9. #define EXTENT_LOCKED (1U << 3)
  10. #define EXTENT_NEW (1U << 4)
  11. #define EXTENT_DELALLOC (1U << 5)
  12. #define EXTENT_DEFRAG (1U << 6)
  13. #define EXTENT_BOUNDARY (1U << 9)
  14. #define EXTENT_NODATASUM (1U << 10)
  15. #define EXTENT_DO_ACCOUNTING (1U << 11)
  16. #define EXTENT_FIRST_DELALLOC (1U << 12)
  17. #define EXTENT_NEED_WAIT (1U << 13)
  18. #define EXTENT_DAMAGED (1U << 14)
  19. #define EXTENT_NORESERVE (1U << 15)
  20. #define EXTENT_QGROUP_RESERVED (1U << 16)
  21. #define EXTENT_IOBITS (EXTENT_LOCKED | EXTENT_WRITEBACK)
  22. #define EXTENT_CTLBITS (EXTENT_DO_ACCOUNTING | EXTENT_FIRST_DELALLOC)
  23. /*
  24. * flags for bio submission. The high bits indicate the compression
  25. * type for this bio
  26. */
  27. #define EXTENT_BIO_COMPRESSED 1
  28. #define EXTENT_BIO_TREE_LOG 2
  29. #define EXTENT_BIO_PARENT_LOCKED 4
  30. #define EXTENT_BIO_FLAG_SHIFT 16
  31. /* these are bit numbers for test/set bit */
  32. #define EXTENT_BUFFER_UPTODATE 0
  33. #define EXTENT_BUFFER_DIRTY 2
  34. #define EXTENT_BUFFER_CORRUPT 3
  35. #define EXTENT_BUFFER_READAHEAD 4 /* this got triggered by readahead */
  36. #define EXTENT_BUFFER_TREE_REF 5
  37. #define EXTENT_BUFFER_STALE 6
  38. #define EXTENT_BUFFER_WRITEBACK 7
  39. #define EXTENT_BUFFER_READ_ERR 8 /* read IO error */
  40. #define EXTENT_BUFFER_DUMMY 9
  41. #define EXTENT_BUFFER_IN_TREE 10
  42. #define EXTENT_BUFFER_WRITE_ERR 11 /* write IO error */
  43. /* these are flags for extent_clear_unlock_delalloc */
  44. #define PAGE_UNLOCK (1 << 0)
  45. #define PAGE_CLEAR_DIRTY (1 << 1)
  46. #define PAGE_SET_WRITEBACK (1 << 2)
  47. #define PAGE_END_WRITEBACK (1 << 3)
  48. #define PAGE_SET_PRIVATE2 (1 << 4)
  49. #define PAGE_SET_ERROR (1 << 5)
  50. /*
  51. * page->private values. Every page that is controlled by the extent
  52. * map has page->private set to one.
  53. */
  54. #define EXTENT_PAGE_PRIVATE 1
  55. struct extent_state;
  56. struct btrfs_root;
  57. struct btrfs_io_bio;
  58. typedef int (extent_submit_bio_hook_t)(struct inode *inode, int rw,
  59. struct bio *bio, int mirror_num,
  60. unsigned long bio_flags, u64 bio_offset);
  61. struct extent_io_ops {
  62. int (*fill_delalloc)(struct inode *inode, struct page *locked_page,
  63. u64 start, u64 end, int *page_started,
  64. unsigned long *nr_written);
  65. int (*writepage_start_hook)(struct page *page, u64 start, u64 end);
  66. int (*writepage_io_hook)(struct page *page, u64 start, u64 end);
  67. extent_submit_bio_hook_t *submit_bio_hook;
  68. int (*merge_bio_hook)(int rw, struct page *page, unsigned long offset,
  69. size_t size, struct bio *bio,
  70. unsigned long bio_flags);
  71. int (*readpage_io_failed_hook)(struct page *page, int failed_mirror);
  72. int (*readpage_end_io_hook)(struct btrfs_io_bio *io_bio, u64 phy_offset,
  73. struct page *page, u64 start, u64 end,
  74. int mirror);
  75. int (*writepage_end_io_hook)(struct page *page, u64 start, u64 end,
  76. struct extent_state *state, int uptodate);
  77. void (*set_bit_hook)(struct inode *inode, struct extent_state *state,
  78. unsigned *bits);
  79. void (*clear_bit_hook)(struct inode *inode, struct extent_state *state,
  80. unsigned *bits);
  81. void (*merge_extent_hook)(struct inode *inode,
  82. struct extent_state *new,
  83. struct extent_state *other);
  84. void (*split_extent_hook)(struct inode *inode,
  85. struct extent_state *orig, u64 split);
  86. };
  87. struct extent_io_tree {
  88. struct rb_root state;
  89. struct address_space *mapping;
  90. u64 dirty_bytes;
  91. int track_uptodate;
  92. spinlock_t lock;
  93. const struct extent_io_ops *ops;
  94. };
  95. struct extent_state {
  96. u64 start;
  97. u64 end; /* inclusive */
  98. struct rb_node rb_node;
  99. /* ADD NEW ELEMENTS AFTER THIS */
  100. wait_queue_head_t wq;
  101. atomic_t refs;
  102. unsigned state;
  103. /* for use by the FS */
  104. u64 private;
  105. #ifdef CONFIG_BTRFS_DEBUG
  106. struct list_head leak_list;
  107. #endif
  108. };
  109. #define INLINE_EXTENT_BUFFER_PAGES 16
  110. #define MAX_INLINE_EXTENT_BUFFER_SIZE (INLINE_EXTENT_BUFFER_PAGES * PAGE_CACHE_SIZE)
  111. struct extent_buffer {
  112. u64 start;
  113. unsigned long len;
  114. unsigned long bflags;
  115. struct btrfs_fs_info *fs_info;
  116. spinlock_t refs_lock;
  117. atomic_t refs;
  118. atomic_t io_pages;
  119. int read_mirror;
  120. struct rcu_head rcu_head;
  121. pid_t lock_owner;
  122. /* count of read lock holders on the extent buffer */
  123. atomic_t write_locks;
  124. atomic_t read_locks;
  125. atomic_t blocking_writers;
  126. atomic_t blocking_readers;
  127. atomic_t spinning_readers;
  128. atomic_t spinning_writers;
  129. short lock_nested;
  130. /* >= 0 if eb belongs to a log tree, -1 otherwise */
  131. short log_index;
  132. /* protects write locks */
  133. rwlock_t lock;
  134. /* readers use lock_wq while they wait for the write
  135. * lock holders to unlock
  136. */
  137. wait_queue_head_t write_lock_wq;
  138. /* writers use read_lock_wq while they wait for readers
  139. * to unlock
  140. */
  141. wait_queue_head_t read_lock_wq;
  142. struct page *pages[INLINE_EXTENT_BUFFER_PAGES];
  143. #ifdef CONFIG_BTRFS_DEBUG
  144. struct list_head leak_list;
  145. #endif
  146. };
  147. /*
  148. * Structure to record how many bytes and which ranges are set/cleared
  149. */
  150. struct extent_changeset {
  151. /* How many bytes are set/cleared in this operation */
  152. u64 bytes_changed;
  153. /* Changed ranges */
  154. struct ulist *range_changed;
  155. };
  156. static inline void extent_set_compress_type(unsigned long *bio_flags,
  157. int compress_type)
  158. {
  159. *bio_flags |= compress_type << EXTENT_BIO_FLAG_SHIFT;
  160. }
  161. static inline int extent_compress_type(unsigned long bio_flags)
  162. {
  163. return bio_flags >> EXTENT_BIO_FLAG_SHIFT;
  164. }
  165. struct extent_map_tree;
  166. typedef struct extent_map *(get_extent_t)(struct inode *inode,
  167. struct page *page,
  168. size_t pg_offset,
  169. u64 start, u64 len,
  170. int create);
  171. void extent_io_tree_init(struct extent_io_tree *tree,
  172. struct address_space *mapping);
  173. int try_release_extent_mapping(struct extent_map_tree *map,
  174. struct extent_io_tree *tree, struct page *page,
  175. gfp_t mask);
  176. int try_release_extent_buffer(struct page *page);
  177. int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  178. struct extent_state **cached);
  179. static inline int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
  180. {
  181. return lock_extent_bits(tree, start, end, NULL);
  182. }
  183. int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end);
  184. int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
  185. get_extent_t *get_extent, int mirror_num);
  186. int extent_read_full_page_nolock(struct extent_io_tree *tree, struct page *page,
  187. get_extent_t *get_extent, int mirror_num);
  188. int __init extent_io_init(void);
  189. void extent_io_exit(void);
  190. u64 count_range_bits(struct extent_io_tree *tree,
  191. u64 *start, u64 search_end,
  192. u64 max_bytes, unsigned bits, int contig);
  193. void free_extent_state(struct extent_state *state);
  194. int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
  195. unsigned bits, int filled,
  196. struct extent_state *cached_state);
  197. int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  198. unsigned bits, gfp_t mask,
  199. struct extent_changeset *changeset);
  200. int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  201. unsigned bits, int wake, int delete,
  202. struct extent_state **cached, gfp_t mask);
  203. static inline int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
  204. {
  205. return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
  206. GFP_NOFS);
  207. }
  208. static inline int unlock_extent_cached(struct extent_io_tree *tree, u64 start,
  209. u64 end, struct extent_state **cached, gfp_t mask)
  210. {
  211. return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
  212. mask);
  213. }
  214. static inline int clear_extent_bits(struct extent_io_tree *tree, u64 start,
  215. u64 end, unsigned bits, gfp_t mask)
  216. {
  217. int wake = 0;
  218. if (bits & EXTENT_LOCKED)
  219. wake = 1;
  220. return clear_extent_bit(tree, start, end, bits, wake, 0, NULL, mask);
  221. }
  222. int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  223. unsigned bits, gfp_t mask,
  224. struct extent_changeset *changeset);
  225. int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  226. unsigned bits, u64 *failed_start,
  227. struct extent_state **cached_state, gfp_t mask);
  228. static inline int set_extent_bits(struct extent_io_tree *tree, u64 start,
  229. u64 end, unsigned bits, gfp_t mask)
  230. {
  231. return set_extent_bit(tree, start, end, bits, NULL, NULL, mask);
  232. }
  233. static inline int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
  234. u64 end, struct extent_state **cached_state, gfp_t mask)
  235. {
  236. return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
  237. cached_state, mask);
  238. }
  239. static inline int set_extent_dirty(struct extent_io_tree *tree, u64 start,
  240. u64 end, gfp_t mask)
  241. {
  242. return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
  243. NULL, mask);
  244. }
  245. static inline int clear_extent_dirty(struct extent_io_tree *tree, u64 start,
  246. u64 end, gfp_t mask)
  247. {
  248. return clear_extent_bit(tree, start, end,
  249. EXTENT_DIRTY | EXTENT_DELALLOC |
  250. EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
  251. }
  252. int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  253. unsigned bits, unsigned clear_bits,
  254. struct extent_state **cached_state, gfp_t mask);
  255. static inline int set_extent_delalloc(struct extent_io_tree *tree, u64 start,
  256. u64 end, struct extent_state **cached_state, gfp_t mask)
  257. {
  258. return set_extent_bit(tree, start, end,
  259. EXTENT_DELALLOC | EXTENT_UPTODATE,
  260. NULL, cached_state, mask);
  261. }
  262. static inline int set_extent_defrag(struct extent_io_tree *tree, u64 start,
  263. u64 end, struct extent_state **cached_state, gfp_t mask)
  264. {
  265. return set_extent_bit(tree, start, end,
  266. EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
  267. NULL, cached_state, mask);
  268. }
  269. static inline int set_extent_new(struct extent_io_tree *tree, u64 start,
  270. u64 end, gfp_t mask)
  271. {
  272. return set_extent_bit(tree, start, end, EXTENT_NEW, NULL, NULL, mask);
  273. }
  274. static inline int set_extent_uptodate(struct extent_io_tree *tree, u64 start,
  275. u64 end, struct extent_state **cached_state, gfp_t mask)
  276. {
  277. return set_extent_bit(tree, start, end, EXTENT_UPTODATE, NULL,
  278. cached_state, mask);
  279. }
  280. int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
  281. u64 *start_ret, u64 *end_ret, unsigned bits,
  282. struct extent_state **cached_state);
  283. int extent_invalidatepage(struct extent_io_tree *tree,
  284. struct page *page, unsigned long offset);
  285. int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
  286. get_extent_t *get_extent,
  287. struct writeback_control *wbc);
  288. int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
  289. u64 start, u64 end, get_extent_t *get_extent,
  290. int mode);
  291. int extent_writepages(struct extent_io_tree *tree,
  292. struct address_space *mapping,
  293. get_extent_t *get_extent,
  294. struct writeback_control *wbc);
  295. int btree_write_cache_pages(struct address_space *mapping,
  296. struct writeback_control *wbc);
  297. int extent_readpages(struct extent_io_tree *tree,
  298. struct address_space *mapping,
  299. struct list_head *pages, unsigned nr_pages,
  300. get_extent_t get_extent);
  301. int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  302. __u64 start, __u64 len, get_extent_t *get_extent);
  303. int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private);
  304. void set_page_extent_mapped(struct page *page);
  305. struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
  306. u64 start);
  307. struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
  308. u64 start, unsigned long len);
  309. struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
  310. u64 start);
  311. struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src);
  312. struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info,
  313. u64 start);
  314. void free_extent_buffer(struct extent_buffer *eb);
  315. void free_extent_buffer_stale(struct extent_buffer *eb);
  316. #define WAIT_NONE 0
  317. #define WAIT_COMPLETE 1
  318. #define WAIT_PAGE_LOCK 2
  319. int read_extent_buffer_pages(struct extent_io_tree *tree,
  320. struct extent_buffer *eb, u64 start, int wait,
  321. get_extent_t *get_extent, int mirror_num);
  322. void wait_on_extent_buffer_writeback(struct extent_buffer *eb);
  323. static inline unsigned long num_extent_pages(u64 start, u64 len)
  324. {
  325. return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
  326. (start >> PAGE_CACHE_SHIFT);
  327. }
  328. static inline void extent_buffer_get(struct extent_buffer *eb)
  329. {
  330. atomic_inc(&eb->refs);
  331. }
  332. int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
  333. unsigned long start,
  334. unsigned long len);
  335. void read_extent_buffer(struct extent_buffer *eb, void *dst,
  336. unsigned long start,
  337. unsigned long len);
  338. int read_extent_buffer_to_user(struct extent_buffer *eb, void __user *dst,
  339. unsigned long start,
  340. unsigned long len);
  341. void write_extent_buffer(struct extent_buffer *eb, const void *src,
  342. unsigned long start, unsigned long len);
  343. void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
  344. unsigned long dst_offset, unsigned long src_offset,
  345. unsigned long len);
  346. void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  347. unsigned long src_offset, unsigned long len);
  348. void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
  349. unsigned long src_offset, unsigned long len);
  350. void memset_extent_buffer(struct extent_buffer *eb, char c,
  351. unsigned long start, unsigned long len);
  352. int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
  353. unsigned long pos);
  354. void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
  355. unsigned long pos, unsigned long len);
  356. void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
  357. unsigned long pos, unsigned long len);
  358. void clear_extent_buffer_dirty(struct extent_buffer *eb);
  359. int set_extent_buffer_dirty(struct extent_buffer *eb);
  360. void set_extent_buffer_uptodate(struct extent_buffer *eb);
  361. void clear_extent_buffer_uptodate(struct extent_buffer *eb);
  362. int extent_buffer_uptodate(struct extent_buffer *eb);
  363. int extent_buffer_under_io(struct extent_buffer *eb);
  364. int map_private_extent_buffer(struct extent_buffer *eb, unsigned long offset,
  365. unsigned long min_len, char **map,
  366. unsigned long *map_start,
  367. unsigned long *map_len);
  368. void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end);
  369. void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end);
  370. void extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
  371. struct page *locked_page,
  372. unsigned bits_to_clear,
  373. unsigned long page_ops);
  374. struct bio *
  375. btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
  376. gfp_t gfp_flags);
  377. struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs);
  378. struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask);
  379. struct btrfs_fs_info;
  380. int repair_io_failure(struct inode *inode, u64 start, u64 length, u64 logical,
  381. struct page *page, unsigned int pg_offset,
  382. int mirror_num);
  383. int clean_io_failure(struct inode *inode, u64 start, struct page *page,
  384. unsigned int pg_offset);
  385. void end_extent_writepage(struct page *page, int err, u64 start, u64 end);
  386. int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
  387. int mirror_num);
  388. /*
  389. * When IO fails, either with EIO or csum verification fails, we
  390. * try other mirrors that might have a good copy of the data. This
  391. * io_failure_record is used to record state as we go through all the
  392. * mirrors. If another mirror has good data, the page is set up to date
  393. * and things continue. If a good mirror can't be found, the original
  394. * bio end_io callback is called to indicate things have failed.
  395. */
  396. struct io_failure_record {
  397. struct page *page;
  398. u64 start;
  399. u64 len;
  400. u64 logical;
  401. unsigned long bio_flags;
  402. int this_mirror;
  403. int failed_mirror;
  404. int in_validation;
  405. };
  406. void btrfs_free_io_failure_record(struct inode *inode, u64 start, u64 end);
  407. int btrfs_get_io_failure_record(struct inode *inode, u64 start, u64 end,
  408. struct io_failure_record **failrec_ret);
  409. int btrfs_check_repairable(struct inode *inode, struct bio *failed_bio,
  410. struct io_failure_record *failrec, int fail_mirror);
  411. struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
  412. struct io_failure_record *failrec,
  413. struct page *page, int pg_offset, int icsum,
  414. bio_end_io_t *endio_func, void *data);
  415. int free_io_failure(struct inode *inode, struct io_failure_record *rec);
  416. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  417. noinline u64 find_lock_delalloc_range(struct inode *inode,
  418. struct extent_io_tree *tree,
  419. struct page *locked_page, u64 *start,
  420. u64 *end, u64 max_bytes);
  421. #endif
  422. struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info,
  423. u64 start);
  424. #endif