1 /* SPDX-License-Identifier: GPL-2.0 */
3 #ifndef BTRFS_EXTENT_MAP_H
4 #define BTRFS_EXTENT_MAP_H
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>
12 #include "extent_map.h"
13 #include "compression.h"
18 #define EXTENT_MAP_LAST_BYTE ((u64)-4)
19 #define EXTENT_MAP_HOLE ((u64)-3)
20 #define EXTENT_MAP_INLINE ((u64)-2)
22 /* bits for the extent_map::flags field */
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),
38 * This structure represents file extents and holes.
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.
43 * Keep this structure as compact as possible, as we can have really large
44 * amounts of allocated extent maps at any time.
47 struct rb_node rb_node;
49 /* All of these are in bytes. */
51 /* File offset matching the offset of a BTRFS_EXTENT_ITEM_KEY key. */
55 * Length of the file extent.
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.
65 * The file offset of the original file extent before splitting.
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.
74 * The full on-disk extent length, matching
75 * btrfs_file_extent_item::disk_num_bytes.
80 * The decompressed size of the whole on-disk extent, matching
81 * btrfs_file_extent_item::ram_bytes.
86 * The on-disk logical bytenr for the file extent.
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
92 * For holes it is EXTENT_MAP_HOLE and for inline extents it is
98 * The on-disk length for the file extent.
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.
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.
114 struct list_head list;
117 struct extent_map_tree {
118 struct rb_root_cached map;
119 struct list_head modified_extents;
125 static inline void extent_map_set_compression(struct extent_map *em,
126 enum btrfs_compression_type type)
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;
136 static inline enum btrfs_compression_type extent_map_compression(const struct extent_map *em)
138 if (em->flags & EXTENT_FLAG_COMPRESS_ZLIB)
139 return BTRFS_COMPRESS_ZLIB;
141 if (em->flags & EXTENT_FLAG_COMPRESS_LZO)
142 return BTRFS_COMPRESS_LZO;
144 if (em->flags & EXTENT_FLAG_COMPRESS_ZSTD)
145 return BTRFS_COMPRESS_ZSTD;
147 return BTRFS_COMPRESS_NONE;
151 * More efficient way to determine if extent is compressed, instead of using
152 * 'extent_map_compression() != BTRFS_COMPRESS_NONE'.
154 static inline bool extent_map_is_compressed(const struct extent_map *em)
156 return (em->flags & (EXTENT_FLAG_COMPRESS_ZLIB |
157 EXTENT_FLAG_COMPRESS_LZO |
158 EXTENT_FLAG_COMPRESS_ZSTD)) != 0;
161 static inline int extent_map_in_tree(const struct extent_map *em)
163 return !RB_EMPTY_NODE(&em->rb_node);
166 static inline u64 extent_map_end(const struct extent_map *em)
168 if (em->start + em->len < em->start)
170 return em->start + em->len;
173 void extent_map_tree_init(struct extent_map_tree *tree);
174 struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
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,
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,
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,
193 int btrfs_replace_extent_map_range(struct btrfs_inode *inode,
194 struct extent_map *new_em,
196 long btrfs_free_extent_maps(struct btrfs_fs_info *fs_info, long nr_to_scan);