extent-io-tree.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef BTRFS_EXTENT_IO_TREE_H
  3. #define BTRFS_EXTENT_IO_TREE_H
  4. #include <linux/rbtree.h>
  5. #include <linux/spinlock.h>
  6. #include <linux/refcount.h>
  7. #include <linux/list.h>
  8. #include <linux/wait.h>
  9. #include "misc.h"
  10. struct extent_changeset;
  11. struct btrfs_fs_info;
  12. struct btrfs_inode;
  13. /* Bits for the extent state */
  14. enum {
  15. ENUM_BIT(EXTENT_DIRTY),
  16. ENUM_BIT(EXTENT_LOCKED),
  17. ENUM_BIT(EXTENT_DIO_LOCKED),
  18. ENUM_BIT(EXTENT_DIRTY_LOG1),
  19. ENUM_BIT(EXTENT_DIRTY_LOG2),
  20. ENUM_BIT(EXTENT_DELALLOC),
  21. ENUM_BIT(EXTENT_DEFRAG),
  22. ENUM_BIT(EXTENT_BOUNDARY),
  23. ENUM_BIT(EXTENT_NODATASUM),
  24. ENUM_BIT(EXTENT_CLEAR_META_RESV),
  25. ENUM_BIT(EXTENT_NEED_WAIT),
  26. ENUM_BIT(EXTENT_NORESERVE),
  27. ENUM_BIT(EXTENT_QGROUP_RESERVED),
  28. ENUM_BIT(EXTENT_CLEAR_DATA_RESV),
  29. /*
  30. * Must be cleared only during ordered extent completion or on error
  31. * paths if we did not manage to submit bios and create the ordered
  32. * extents for the range. Should not be cleared during page release
  33. * and page invalidation (if there is an ordered extent in flight),
  34. * that is left for the ordered extent completion.
  35. */
  36. ENUM_BIT(EXTENT_DELALLOC_NEW),
  37. /*
  38. * Mark that a range is being locked for finishing an ordered extent.
  39. * Used together with EXTENT_LOCKED.
  40. */
  41. ENUM_BIT(EXTENT_FINISHING_ORDERED),
  42. /*
  43. * When an ordered extent successfully completes for a region marked as
  44. * a new delalloc range, use this flag when clearing a new delalloc
  45. * range to indicate that the VFS' inode number of bytes should be
  46. * incremented and the inode's new delalloc bytes decremented, in an
  47. * atomic way to prevent races with stat(2).
  48. */
  49. ENUM_BIT(EXTENT_ADD_INODE_BYTES),
  50. /*
  51. * Set during truncate when we're clearing an entire range and we just
  52. * want the extent states to go away.
  53. */
  54. ENUM_BIT(EXTENT_CLEAR_ALL_BITS),
  55. /*
  56. * This must be last.
  57. *
  58. * Bit not representing a state but a request for NOWAIT semantics,
  59. * e.g. when allocating memory, and must be masked out from the other
  60. * bits.
  61. */
  62. ENUM_BIT(EXTENT_NOWAIT)
  63. };
  64. #define EXTENT_DO_ACCOUNTING (EXTENT_CLEAR_META_RESV | \
  65. EXTENT_CLEAR_DATA_RESV)
  66. #define EXTENT_CTLBITS (EXTENT_DO_ACCOUNTING | \
  67. EXTENT_ADD_INODE_BYTES | \
  68. EXTENT_CLEAR_ALL_BITS)
  69. #define EXTENT_LOCK_BITS (EXTENT_LOCKED | EXTENT_DIO_LOCKED)
  70. /*
  71. * Redefined bits above which are used only in the device allocation tree,
  72. * shouldn't be using EXTENT_LOCKED / EXTENT_BOUNDARY / EXTENT_CLEAR_META_RESV
  73. * / EXTENT_CLEAR_DATA_RESV because they have special meaning to the bit
  74. * manipulation functions
  75. */
  76. #define CHUNK_ALLOCATED EXTENT_DIRTY
  77. #define CHUNK_TRIMMED EXTENT_DEFRAG
  78. #define CHUNK_STATE_MASK (CHUNK_ALLOCATED | \
  79. CHUNK_TRIMMED)
  80. enum {
  81. IO_TREE_FS_PINNED_EXTENTS,
  82. IO_TREE_FS_EXCLUDED_EXTENTS,
  83. IO_TREE_BTREE_INODE_IO,
  84. IO_TREE_INODE_IO,
  85. IO_TREE_RELOC_BLOCKS,
  86. IO_TREE_TRANS_DIRTY_PAGES,
  87. IO_TREE_ROOT_DIRTY_LOG_PAGES,
  88. IO_TREE_INODE_FILE_EXTENT,
  89. IO_TREE_LOG_CSUM_RANGE,
  90. IO_TREE_SELFTEST,
  91. IO_TREE_DEVICE_ALLOC_STATE,
  92. };
  93. struct extent_io_tree {
  94. struct rb_root state;
  95. /*
  96. * The fs_info is needed for trace points, a tree attached to an inode
  97. * needs the inode.
  98. *
  99. * owner == IO_TREE_INODE_IO - then inode is valid and fs_info can be
  100. * accessed as inode->root->fs_info
  101. */
  102. union {
  103. struct btrfs_fs_info *fs_info;
  104. struct btrfs_inode *inode;
  105. };
  106. /* Who owns this io tree, should be one of IO_TREE_* */
  107. u8 owner;
  108. spinlock_t lock;
  109. };
  110. struct extent_state {
  111. u64 start;
  112. u64 end; /* inclusive */
  113. struct rb_node rb_node;
  114. /* ADD NEW ELEMENTS AFTER THIS */
  115. wait_queue_head_t wq;
  116. refcount_t refs;
  117. u32 state;
  118. #ifdef CONFIG_BTRFS_DEBUG
  119. struct list_head leak_list;
  120. #endif
  121. };
  122. const struct btrfs_inode *btrfs_extent_io_tree_to_inode(const struct extent_io_tree *tree);
  123. const struct btrfs_fs_info *btrfs_extent_io_tree_to_fs_info(const struct extent_io_tree *tree);
  124. void btrfs_extent_io_tree_init(struct btrfs_fs_info *fs_info,
  125. struct extent_io_tree *tree, unsigned int owner);
  126. void btrfs_extent_io_tree_release(struct extent_io_tree *tree);
  127. int btrfs_lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, u32 bits,
  128. struct extent_state **cached);
  129. bool btrfs_try_lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  130. u32 bits, struct extent_state **cached);
  131. static inline int btrfs_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
  132. struct extent_state **cached)
  133. {
  134. return btrfs_lock_extent_bits(tree, start, end, EXTENT_LOCKED, cached);
  135. }
  136. static inline bool btrfs_try_lock_extent(struct extent_io_tree *tree, u64 start,
  137. u64 end, struct extent_state **cached)
  138. {
  139. return btrfs_try_lock_extent_bits(tree, start, end, EXTENT_LOCKED, cached);
  140. }
  141. int __init btrfs_extent_state_init_cachep(void);
  142. void __cold btrfs_extent_state_free_cachep(void);
  143. u64 btrfs_count_range_bits(struct extent_io_tree *tree,
  144. u64 *start, u64 search_end,
  145. u64 max_bytes, u32 bits, bool contig,
  146. struct extent_state **cached_state);
  147. void btrfs_free_extent_state(struct extent_state *state);
  148. bool btrfs_test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bit,
  149. struct extent_state *cached_state);
  150. bool btrfs_test_range_bit_exists(struct extent_io_tree *tree, u64 start, u64 end, u32 bit);
  151. void btrfs_get_range_bits(struct extent_io_tree *tree, u64 start, u64 end, u32 *bits,
  152. struct extent_state **cached_state);
  153. int btrfs_clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  154. u32 bits, struct extent_changeset *changeset);
  155. int btrfs_clear_extent_bit_changeset(struct extent_io_tree *tree, u64 start, u64 end,
  156. u32 bits, struct extent_state **cached,
  157. struct extent_changeset *changeset);
  158. static inline int btrfs_clear_extent_bit(struct extent_io_tree *tree, u64 start,
  159. u64 end, u32 bits,
  160. struct extent_state **cached)
  161. {
  162. return btrfs_clear_extent_bit_changeset(tree, start, end, bits, cached, NULL);
  163. }
  164. static inline int btrfs_unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
  165. struct extent_state **cached)
  166. {
  167. return btrfs_clear_extent_bit_changeset(tree, start, end, EXTENT_LOCKED,
  168. cached, NULL);
  169. }
  170. int btrfs_set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
  171. u32 bits, struct extent_changeset *changeset);
  172. int btrfs_set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  173. u32 bits, struct extent_state **cached_state);
  174. static inline int btrfs_clear_extent_dirty(struct extent_io_tree *tree, u64 start,
  175. u64 end, struct extent_state **cached)
  176. {
  177. return btrfs_clear_extent_bit(tree, start, end,
  178. EXTENT_DIRTY | EXTENT_DELALLOC |
  179. EXTENT_DO_ACCOUNTING, cached);
  180. }
  181. int btrfs_convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
  182. u32 bits, u32 clear_bits,
  183. struct extent_state **cached_state);
  184. bool btrfs_find_first_extent_bit(struct extent_io_tree *tree, u64 start,
  185. u64 *start_ret, u64 *end_ret, u32 bits,
  186. struct extent_state **cached_state);
  187. void btrfs_find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
  188. u64 *start_ret, u64 *end_ret, u32 bits);
  189. bool btrfs_find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
  190. u64 *start_ret, u64 *end_ret, u32 bits);
  191. bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
  192. u64 *end, u64 max_bytes,
  193. struct extent_state **cached_state);
  194. static inline int btrfs_lock_dio_extent(struct extent_io_tree *tree, u64 start,
  195. u64 end, struct extent_state **cached)
  196. {
  197. return btrfs_lock_extent_bits(tree, start, end, EXTENT_DIO_LOCKED, cached);
  198. }
  199. static inline bool btrfs_try_lock_dio_extent(struct extent_io_tree *tree, u64 start,
  200. u64 end, struct extent_state **cached)
  201. {
  202. return btrfs_try_lock_extent_bits(tree, start, end, EXTENT_DIO_LOCKED, cached);
  203. }
  204. static inline int btrfs_unlock_dio_extent(struct extent_io_tree *tree, u64 start,
  205. u64 end, struct extent_state **cached)
  206. {
  207. return btrfs_clear_extent_bit_changeset(tree, start, end, EXTENT_DIO_LOCKED,
  208. cached, NULL);
  209. }
  210. struct extent_state *btrfs_next_extent_state(struct extent_io_tree *tree,
  211. struct extent_state *state);
  212. #endif /* BTRFS_EXTENT_IO_TREE_H */