extent_io.h 17 KB

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