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