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