extent_map.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef BTRFS_EXTENT_MAP_H
  3. #define BTRFS_EXTENT_MAP_H
  4. #include <linux/compiler_types.h>
  5. #include <linux/spinlock_types.h>
  6. #include <linux/rbtree.h>
  7. #include <linux/list.h>
  8. #include <linux/refcount.h>
  9. #include "misc.h"
  10. #include "compression.h"
  11. struct btrfs_inode;
  12. struct btrfs_fs_info;
  13. #define EXTENT_MAP_LAST_BYTE ((u64)-4)
  14. #define EXTENT_MAP_HOLE ((u64)-3)
  15. #define EXTENT_MAP_INLINE ((u64)-2)
  16. /* bits for the extent_map::flags field */
  17. enum {
  18. /* this entry not yet on disk, don't free it */
  19. ENUM_BIT(EXTENT_FLAG_PINNED),
  20. ENUM_BIT(EXTENT_FLAG_COMPRESS_ZLIB),
  21. ENUM_BIT(EXTENT_FLAG_COMPRESS_LZO),
  22. ENUM_BIT(EXTENT_FLAG_COMPRESS_ZSTD),
  23. /* pre-allocated extent */
  24. ENUM_BIT(EXTENT_FLAG_PREALLOC),
  25. /* Logging this extent */
  26. ENUM_BIT(EXTENT_FLAG_LOGGING),
  27. /* This em is merged from two or more physically adjacent ems */
  28. ENUM_BIT(EXTENT_FLAG_MERGED),
  29. };
  30. /*
  31. * This structure represents file extents and holes.
  32. *
  33. * Unlike on-disk file extent items, extent maps can be merged to save memory.
  34. * This means members only match file extent items before any merging.
  35. *
  36. * Keep this structure as compact as possible, as we can have really large
  37. * amounts of allocated extent maps at any time.
  38. */
  39. struct extent_map {
  40. struct rb_node rb_node;
  41. /* All of these are in bytes. */
  42. /* File offset matching the offset of a BTRFS_EXTENT_ITEM_KEY key. */
  43. u64 start;
  44. /*
  45. * Length of the file extent.
  46. *
  47. * For non-inlined file extents it's btrfs_file_extent_item::num_bytes.
  48. * For inline extents it's sectorsize, since inline data starts at
  49. * offsetof(struct btrfs_file_extent_item, disk_bytenr) thus
  50. * btrfs_file_extent_item::num_bytes is not valid.
  51. */
  52. u64 len;
  53. /*
  54. * The bytenr of the full on-disk extent.
  55. *
  56. * For regular extents it's btrfs_file_extent_item::disk_bytenr.
  57. * For holes it's EXTENT_MAP_HOLE and for inline extents it's
  58. * EXTENT_MAP_INLINE.
  59. */
  60. u64 disk_bytenr;
  61. /*
  62. * The full on-disk extent length, matching
  63. * btrfs_file_extent_item::disk_num_bytes.
  64. */
  65. u64 disk_num_bytes;
  66. /*
  67. * Offset inside the decompressed extent.
  68. *
  69. * For regular extents it's btrfs_file_extent_item::offset.
  70. * For holes and inline extents it's 0.
  71. */
  72. u64 offset;
  73. /*
  74. * The decompressed size of the whole on-disk extent, matching
  75. * btrfs_file_extent_item::ram_bytes.
  76. */
  77. u64 ram_bytes;
  78. /*
  79. * Generation of the extent map, for merged em it's the highest
  80. * generation of all merged ems.
  81. * For non-merged extents, it's from btrfs_file_extent_item::generation.
  82. */
  83. u64 generation;
  84. u32 flags;
  85. refcount_t refs;
  86. struct list_head list;
  87. };
  88. struct extent_map_tree {
  89. struct rb_root root;
  90. struct list_head modified_extents;
  91. rwlock_t lock;
  92. };
  93. struct btrfs_inode;
  94. static inline void extent_map_set_compression(struct extent_map *em,
  95. enum btrfs_compression_type type)
  96. {
  97. if (type == BTRFS_COMPRESS_ZLIB)
  98. em->flags |= EXTENT_FLAG_COMPRESS_ZLIB;
  99. else if (type == BTRFS_COMPRESS_LZO)
  100. em->flags |= EXTENT_FLAG_COMPRESS_LZO;
  101. else if (type == BTRFS_COMPRESS_ZSTD)
  102. em->flags |= EXTENT_FLAG_COMPRESS_ZSTD;
  103. }
  104. static inline enum btrfs_compression_type extent_map_compression(const struct extent_map *em)
  105. {
  106. if (em->flags & EXTENT_FLAG_COMPRESS_ZLIB)
  107. return BTRFS_COMPRESS_ZLIB;
  108. if (em->flags & EXTENT_FLAG_COMPRESS_LZO)
  109. return BTRFS_COMPRESS_LZO;
  110. if (em->flags & EXTENT_FLAG_COMPRESS_ZSTD)
  111. return BTRFS_COMPRESS_ZSTD;
  112. return BTRFS_COMPRESS_NONE;
  113. }
  114. /*
  115. * More efficient way to determine if extent is compressed, instead of using
  116. * 'extent_map_compression() != BTRFS_COMPRESS_NONE'.
  117. */
  118. static inline bool extent_map_is_compressed(const struct extent_map *em)
  119. {
  120. return (em->flags & (EXTENT_FLAG_COMPRESS_ZLIB |
  121. EXTENT_FLAG_COMPRESS_LZO |
  122. EXTENT_FLAG_COMPRESS_ZSTD)) != 0;
  123. }
  124. static inline int extent_map_in_tree(const struct extent_map *em)
  125. {
  126. return !RB_EMPTY_NODE(&em->rb_node);
  127. }
  128. static inline u64 extent_map_block_start(const struct extent_map *em)
  129. {
  130. if (em->disk_bytenr < EXTENT_MAP_LAST_BYTE) {
  131. if (extent_map_is_compressed(em))
  132. return em->disk_bytenr;
  133. return em->disk_bytenr + em->offset;
  134. }
  135. return em->disk_bytenr;
  136. }
  137. static inline u64 extent_map_end(const struct extent_map *em)
  138. {
  139. if (em->start + em->len < em->start)
  140. return (u64)-1;
  141. return em->start + em->len;
  142. }
  143. void extent_map_tree_init(struct extent_map_tree *tree);
  144. struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
  145. u64 start, u64 len);
  146. void remove_extent_mapping(struct btrfs_inode *inode, struct extent_map *em);
  147. int split_extent_map(struct btrfs_inode *inode, u64 start, u64 len, u64 pre,
  148. u64 new_logical);
  149. struct extent_map *alloc_extent_map(void);
  150. void free_extent_map(struct extent_map *em);
  151. int __init extent_map_init(void);
  152. void __cold extent_map_exit(void);
  153. int unpin_extent_cache(struct btrfs_inode *inode, u64 start, u64 len, u64 gen);
  154. void clear_em_logging(struct btrfs_inode *inode, struct extent_map *em);
  155. struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
  156. u64 start, u64 len);
  157. int btrfs_add_extent_mapping(struct btrfs_inode *inode,
  158. struct extent_map **em_in, u64 start, u64 len);
  159. void btrfs_drop_extent_map_range(struct btrfs_inode *inode,
  160. u64 start, u64 end,
  161. bool skip_pinned);
  162. int btrfs_replace_extent_map_range(struct btrfs_inode *inode,
  163. struct extent_map *new_em,
  164. bool modified);
  165. void btrfs_free_extent_maps(struct btrfs_fs_info *fs_info, long nr_to_scan);
  166. void btrfs_init_extent_map_shrinker_work(struct btrfs_fs_info *fs_info);
  167. #endif