extent-io-tree.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef BTRFS_EXTENT_IO_TREE_H
  3. #define BTRFS_EXTENT_IO_TREE_H
  4. struct extent_changeset;
  5. struct io_failure_record;
  6. /* Bits for the extent state */
  7. #define EXTENT_DIRTY (1U << 0)
  8. #define EXTENT_UPTODATE (1U << 1)
  9. #define EXTENT_LOCKED (1U << 2)
  10. #define EXTENT_NEW (1U << 3)
  11. #define EXTENT_DELALLOC (1U << 4)
  12. #define EXTENT_DEFRAG (1U << 5)
  13. #define EXTENT_BOUNDARY (1U << 6)
  14. #define EXTENT_NODATASUM (1U << 7)
  15. #define EXTENT_CLEAR_META_RESV (1U << 8)
  16. #define EXTENT_NEED_WAIT (1U << 9)
  17. #define EXTENT_DAMAGED (1U << 10)
  18. #define EXTENT_NORESERVE (1U << 11)
  19. #define EXTENT_QGROUP_RESERVED (1U << 12)
  20. #define EXTENT_CLEAR_DATA_RESV (1U << 13)
  21. #define EXTENT_DELALLOC_NEW (1U << 14)
  22. #define EXTENT_DO_ACCOUNTING (EXTENT_CLEAR_META_RESV | \
  23. EXTENT_CLEAR_DATA_RESV)
  24. #define EXTENT_CTLBITS (EXTENT_DO_ACCOUNTING)
  25. /*
  26. * Redefined bits above which are used only in the device allocation tree,
  27. * shouldn't be using EXTENT_LOCKED / EXTENT_BOUNDARY / EXTENT_CLEAR_META_RESV
  28. * / EXTENT_CLEAR_DATA_RESV because they have special meaning to the bit
  29. * manipulation functions
  30. */
  31. #define CHUNK_ALLOCATED EXTENT_DIRTY
  32. #define CHUNK_TRIMMED EXTENT_DEFRAG
  33. #define CHUNK_STATE_MASK (CHUNK_ALLOCATED | \
  34. CHUNK_TRIMMED)
  35. enum {
  36. IO_TREE_FS_PINNED_EXTENTS,
  37. IO_TREE_FS_EXCLUDED_EXTENTS,
  38. IO_TREE_BTREE_INODE_IO,
  39. IO_TREE_INODE_IO,
  40. IO_TREE_INODE_IO_FAILURE,
  41. IO_TREE_RELOC_BLOCKS,
  42. IO_TREE_TRANS_DIRTY_PAGES,
  43. IO_TREE_ROOT_DIRTY_LOG_PAGES,
  44. IO_TREE_INODE_FILE_EXTENT,
  45. IO_TREE_LOG_CSUM_RANGE,
  46. IO_TREE_SELFTEST,
  47. IO_TREE_DEVICE_ALLOC_STATE,
  48. };
  49. struct extent_io_tree {
  50. struct rb_root state;
  51. struct btrfs_fs_info *fs_info;
  52. void *private_data;
  53. u64 dirty_bytes;
  54. bool track_uptodate;
  55. /* Who owns this io tree, should be one of IO_TREE_* */
  56. u8 owner;
  57. spinlock_t lock;
  58. };
  59. struct extent_state {
  60. u64 start;
  61. u64 end; /* inclusive */
  62. struct rb_node rb_node;
  63. /* ADD NEW ELEMENTS AFTER THIS */
  64. wait_queue_head_t wq;
  65. refcount_t refs;
  66. unsigned state;
  67. struct io_failure_record *failrec;
  68. #ifdef CONFIG_BTRFS_DEBUG
  69. struct list_head leak_list;
  70. #endif
  71. };
  72. int __init extent_state_cache_init(void);
  73. void __cold extent_state_cache_exit(void);
  74. void extent_io_tree_init(struct btrfs_fs_info *fs_info,
  75. struct extent_io_tree *tree, unsigned int owner,
  76. void *private_data);
  77. void extent_io_tree_release(struct extent_io_tree *tree);
  78. int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  79. struct extent_state **cached);
  80. static inline int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
  81. {
  82. return lock_extent_bits(tree, start, end, NULL);
  83. }
  84. int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end);
  85. int __init extent_io_init(void);
  86. void __cold extent_io_exit(void);
  87. u64 count_range_bits(struct extent_io_tree *tree,
  88. u64 *start, u64 search_end,
  89. u64 max_bytes, unsigned bits, int contig);
  90. void free_extent_state(struct extent_state *state);
  91. int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
  92. unsigned bits, int filled,
  93. struct extent_state *cached_state);
  94. int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  95. unsigned bits, struct extent_changeset *changeset);
  96. int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  97. unsigned bits, int wake, int delete,
  98. struct extent_state **cached);
  99. int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  100. unsigned bits, int wake, int delete,
  101. struct extent_state **cached, gfp_t mask,
  102. struct extent_changeset *changeset);
  103. static inline int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
  104. {
  105. return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL);
  106. }
  107. static inline int unlock_extent_cached(struct extent_io_tree *tree, u64 start,
  108. u64 end, struct extent_state **cached)
  109. {
  110. return __clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
  111. GFP_NOFS, NULL);
  112. }
  113. static inline int unlock_extent_cached_atomic(struct extent_io_tree *tree,
  114. u64 start, u64 end, struct extent_state **cached)
  115. {
  116. return __clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
  117. GFP_ATOMIC, NULL);
  118. }
  119. static inline int clear_extent_bits(struct extent_io_tree *tree, u64 start,
  120. u64 end, unsigned bits)
  121. {
  122. int wake = 0;
  123. if (bits & EXTENT_LOCKED)
  124. wake = 1;
  125. return clear_extent_bit(tree, start, end, bits, wake, 0, NULL);
  126. }
  127. int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  128. unsigned bits, struct extent_changeset *changeset);
  129. int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  130. unsigned bits, u64 *failed_start,
  131. struct extent_state **cached_state, gfp_t mask);
  132. int set_extent_bits_nowait(struct extent_io_tree *tree, u64 start, u64 end,
  133. unsigned bits);
  134. static inline int set_extent_bits(struct extent_io_tree *tree, u64 start,
  135. u64 end, unsigned bits)
  136. {
  137. return set_extent_bit(tree, start, end, bits, NULL, NULL, GFP_NOFS);
  138. }
  139. static inline int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
  140. u64 end, struct extent_state **cached_state)
  141. {
  142. return __clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
  143. cached_state, GFP_NOFS, NULL);
  144. }
  145. static inline int set_extent_dirty(struct extent_io_tree *tree, u64 start,
  146. u64 end, gfp_t mask)
  147. {
  148. return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
  149. NULL, mask);
  150. }
  151. static inline int clear_extent_dirty(struct extent_io_tree *tree, u64 start,
  152. u64 end, struct extent_state **cached)
  153. {
  154. return clear_extent_bit(tree, start, end,
  155. EXTENT_DIRTY | EXTENT_DELALLOC |
  156. EXTENT_DO_ACCOUNTING, 0, 0, cached);
  157. }
  158. int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  159. unsigned bits, unsigned clear_bits,
  160. struct extent_state **cached_state);
  161. static inline int set_extent_delalloc(struct extent_io_tree *tree, u64 start,
  162. u64 end, unsigned int extra_bits,
  163. struct extent_state **cached_state)
  164. {
  165. return set_extent_bit(tree, start, end,
  166. EXTENT_DELALLOC | EXTENT_UPTODATE | extra_bits,
  167. NULL, cached_state, GFP_NOFS);
  168. }
  169. static inline int set_extent_defrag(struct extent_io_tree *tree, u64 start,
  170. u64 end, struct extent_state **cached_state)
  171. {
  172. return set_extent_bit(tree, start, end,
  173. EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
  174. NULL, cached_state, GFP_NOFS);
  175. }
  176. static inline int set_extent_new(struct extent_io_tree *tree, u64 start,
  177. u64 end)
  178. {
  179. return set_extent_bit(tree, start, end, EXTENT_NEW, NULL, NULL,
  180. GFP_NOFS);
  181. }
  182. static inline int set_extent_uptodate(struct extent_io_tree *tree, u64 start,
  183. u64 end, struct extent_state **cached_state, gfp_t mask)
  184. {
  185. return set_extent_bit(tree, start, end, EXTENT_UPTODATE, NULL,
  186. cached_state, mask);
  187. }
  188. int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
  189. u64 *start_ret, u64 *end_ret, unsigned bits,
  190. struct extent_state **cached_state);
  191. void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
  192. u64 *start_ret, u64 *end_ret, unsigned bits);
  193. int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
  194. u64 *start_ret, u64 *end_ret, unsigned bits);
  195. int extent_invalidatepage(struct extent_io_tree *tree,
  196. struct page *page, unsigned long offset);
  197. bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
  198. u64 *end, u64 max_bytes,
  199. struct extent_state **cached_state);
  200. /* This should be reworked in the future and put elsewhere. */
  201. struct io_failure_record *get_state_failrec(struct extent_io_tree *tree, u64 start);
  202. int set_state_failrec(struct extent_io_tree *tree, u64 start,
  203. struct io_failure_record *failrec);
  204. void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start,
  205. u64 end);
  206. int free_io_failure(struct extent_io_tree *failure_tree,
  207. struct extent_io_tree *io_tree,
  208. struct io_failure_record *rec);
  209. int clean_io_failure(struct btrfs_fs_info *fs_info,
  210. struct extent_io_tree *failure_tree,
  211. struct extent_io_tree *io_tree, u64 start,
  212. struct page *page, u64 ino, unsigned int pg_offset);
  213. #endif /* BTRFS_EXTENT_IO_TREE_H */