extent_map.h 5.5 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 "fs.h"
  10. struct btrfs_inode;
  11. struct btrfs_fs_info;
  12. #define EXTENT_MAP_LAST_BYTE ((u64)-4)
  13. #define EXTENT_MAP_HOLE ((u64)-3)
  14. #define EXTENT_MAP_INLINE ((u64)-2)
  15. /* bits for the extent_map::flags field */
  16. enum {
  17. /* this entry not yet on disk, don't free it */
  18. ENUM_BIT(EXTENT_FLAG_PINNED),
  19. ENUM_BIT(EXTENT_FLAG_COMPRESS_ZLIB),
  20. ENUM_BIT(EXTENT_FLAG_COMPRESS_LZO),
  21. ENUM_BIT(EXTENT_FLAG_COMPRESS_ZSTD),
  22. /* pre-allocated extent */
  23. ENUM_BIT(EXTENT_FLAG_PREALLOC),
  24. /* Logging this extent */
  25. ENUM_BIT(EXTENT_FLAG_LOGGING),
  26. /* This em is merged from two or more physically adjacent ems */
  27. ENUM_BIT(EXTENT_FLAG_MERGED),
  28. };
  29. /*
  30. * This structure represents file extents and holes.
  31. *
  32. * Unlike on-disk file extent items, extent maps can be merged to save memory.
  33. * This means members only match file extent items before any merging.
  34. *
  35. * Keep this structure as compact as possible, as we can have really large
  36. * amounts of allocated extent maps at any time.
  37. */
  38. struct extent_map {
  39. struct rb_node rb_node;
  40. /* All of these are in bytes. */
  41. /* File offset matching the offset of a BTRFS_EXTENT_ITEM_KEY key. */
  42. u64 start;
  43. /*
  44. * Length of the file extent.
  45. *
  46. * For non-inlined file extents it's btrfs_file_extent_item::num_bytes.
  47. * For inline extents it's sectorsize, since inline data starts at
  48. * offsetof(struct btrfs_file_extent_item, disk_bytenr) thus
  49. * btrfs_file_extent_item::num_bytes is not valid.
  50. */
  51. u64 len;
  52. /*
  53. * The bytenr of the full on-disk extent.
  54. *
  55. * For regular extents it's btrfs_file_extent_item::disk_bytenr.
  56. * For holes it's EXTENT_MAP_HOLE and for inline extents it's
  57. * EXTENT_MAP_INLINE.
  58. */
  59. u64 disk_bytenr;
  60. /*
  61. * The full on-disk extent length, matching
  62. * btrfs_file_extent_item::disk_num_bytes.
  63. */
  64. u64 disk_num_bytes;
  65. /*
  66. * Offset inside the decompressed extent.
  67. *
  68. * For regular extents it's btrfs_file_extent_item::offset.
  69. * For holes and inline extents it's 0.
  70. */
  71. u64 offset;
  72. /*
  73. * The decompressed size of the whole on-disk extent, matching
  74. * btrfs_file_extent_item::ram_bytes.
  75. */
  76. u64 ram_bytes;
  77. /*
  78. * Generation of the extent map, for merged em it's the highest
  79. * generation of all merged ems.
  80. * For non-merged extents, it's from btrfs_file_extent_item::generation.
  81. */
  82. u64 generation;
  83. u32 flags;
  84. refcount_t refs;
  85. struct list_head list;
  86. };
  87. struct extent_map_tree {
  88. struct rb_root root;
  89. struct list_head modified_extents;
  90. rwlock_t lock;
  91. };
  92. struct btrfs_inode;
  93. static inline void btrfs_extent_map_set_compression(struct extent_map *em,
  94. enum btrfs_compression_type type)
  95. {
  96. if (type == BTRFS_COMPRESS_ZLIB)
  97. em->flags |= EXTENT_FLAG_COMPRESS_ZLIB;
  98. else if (type == BTRFS_COMPRESS_LZO)
  99. em->flags |= EXTENT_FLAG_COMPRESS_LZO;
  100. else if (type == BTRFS_COMPRESS_ZSTD)
  101. em->flags |= EXTENT_FLAG_COMPRESS_ZSTD;
  102. }
  103. static inline enum btrfs_compression_type btrfs_extent_map_compression(
  104. 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 btrfs_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 btrfs_extent_map_in_tree(const struct extent_map *em)
  125. {
  126. return !RB_EMPTY_NODE(&em->rb_node);
  127. }
  128. static inline u64 btrfs_extent_map_block_start(const struct extent_map *em)
  129. {
  130. if (em->disk_bytenr < EXTENT_MAP_LAST_BYTE) {
  131. if (btrfs_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 btrfs_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 btrfs_extent_map_tree_init(struct extent_map_tree *tree);
  144. struct extent_map *btrfs_lookup_extent_mapping(struct extent_map_tree *tree,
  145. u64 start, u64 len);
  146. void btrfs_remove_extent_mapping(struct btrfs_inode *inode, struct extent_map *em);
  147. int btrfs_split_extent_map(struct btrfs_inode *inode, u64 start, u64 len, u64 pre,
  148. u64 new_logical);
  149. struct extent_map *btrfs_alloc_extent_map(void);
  150. void btrfs_free_extent_map(struct extent_map *em);
  151. int __init btrfs_extent_map_init(void);
  152. void __cold btrfs_extent_map_exit(void);
  153. int btrfs_unpin_extent_cache(struct btrfs_inode *inode, u64 start, u64 len, u64 gen);
  154. void btrfs_clear_em_logging(struct btrfs_inode *inode, struct extent_map *em);
  155. struct extent_map *btrfs_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