extent_map.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef BTRFS_EXTENT_MAP_H
  3. #define BTRFS_EXTENT_MAP_H
  4. #include <linux/rbtree.h>
  5. #include <linux/refcount.h>
  6. #define EXTENT_MAP_LAST_BYTE ((u64)-4)
  7. #define EXTENT_MAP_HOLE ((u64)-3)
  8. #define EXTENT_MAP_INLINE ((u64)-2)
  9. #define EXTENT_MAP_DELALLOC ((u64)-1)
  10. /* bits for the flags field */
  11. #define EXTENT_FLAG_PINNED 0 /* this entry not yet on disk, don't free it */
  12. #define EXTENT_FLAG_COMPRESSED 1
  13. #define EXTENT_FLAG_PREALLOC 3 /* pre-allocated extent */
  14. #define EXTENT_FLAG_LOGGING 4 /* Logging this extent */
  15. #define EXTENT_FLAG_FILLING 5 /* Filling in a preallocated extent */
  16. #define EXTENT_FLAG_FS_MAPPING 6 /* filesystem extent mapping type */
  17. struct extent_map {
  18. struct rb_node rb_node;
  19. /* all of these are in bytes */
  20. u64 start;
  21. u64 len;
  22. u64 mod_start;
  23. u64 mod_len;
  24. u64 orig_start;
  25. u64 orig_block_len;
  26. u64 ram_bytes;
  27. u64 block_start;
  28. u64 block_len;
  29. u64 generation;
  30. unsigned long flags;
  31. union {
  32. struct block_device *bdev;
  33. /*
  34. * used for chunk mappings
  35. * flags & EXTENT_FLAG_FS_MAPPING must be set
  36. */
  37. struct map_lookup *map_lookup;
  38. };
  39. refcount_t refs;
  40. unsigned int compress_type;
  41. struct list_head list;
  42. };
  43. struct extent_map_tree {
  44. struct rb_root map;
  45. struct list_head modified_extents;
  46. rwlock_t lock;
  47. };
  48. static inline int extent_map_in_tree(const struct extent_map *em)
  49. {
  50. return !RB_EMPTY_NODE(&em->rb_node);
  51. }
  52. static inline u64 extent_map_end(struct extent_map *em)
  53. {
  54. if (em->start + em->len < em->start)
  55. return (u64)-1;
  56. return em->start + em->len;
  57. }
  58. static inline u64 extent_map_block_end(struct extent_map *em)
  59. {
  60. if (em->block_start + em->block_len < em->block_start)
  61. return (u64)-1;
  62. return em->block_start + em->block_len;
  63. }
  64. void extent_map_tree_init(struct extent_map_tree *tree);
  65. struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
  66. u64 start, u64 len);
  67. int add_extent_mapping(struct extent_map_tree *tree,
  68. struct extent_map *em, int modified);
  69. int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em);
  70. void replace_extent_mapping(struct extent_map_tree *tree,
  71. struct extent_map *cur,
  72. struct extent_map *new,
  73. int modified);
  74. struct extent_map *alloc_extent_map(void);
  75. void free_extent_map(struct extent_map *em);
  76. int __init extent_map_init(void);
  77. void __cold extent_map_exit(void);
  78. int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len, u64 gen);
  79. void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em);
  80. struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
  81. u64 start, u64 len);
  82. int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info,
  83. struct extent_map_tree *em_tree,
  84. struct extent_map **em_in, u64 start, u64 len);
  85. #endif