1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/slab.h>
4 #include <linux/spinlock.h>
5 #include <linux/hardirq.h>
7 #include "extent_map.h"
8 #include "compression.h"
11 static struct kmem_cache
*extent_map_cache
;
13 int __init
extent_map_init(void)
15 extent_map_cache
= kmem_cache_create("btrfs_extent_map",
16 sizeof(struct extent_map
), 0,
17 SLAB_MEM_SPREAD
, NULL
);
18 if (!extent_map_cache
)
23 void extent_map_exit(void)
25 kmem_cache_destroy(extent_map_cache
);
29 * extent_map_tree_init - initialize extent map tree
30 * @tree: tree to initialize
32 * Initialize the extent tree @tree. Should be called for each new inode
33 * or other user of the extent_map interface.
35 void extent_map_tree_init(struct extent_map_tree
*tree
)
38 INIT_LIST_HEAD(&tree
->modified_extents
);
39 rwlock_init(&tree
->lock
);
43 * alloc_extent_map - allocate new extent map structure
45 * Allocate a new extent_map structure. The new structure is
46 * returned with a reference count of one and needs to be
47 * freed using free_extent_map()
49 struct extent_map
*alloc_extent_map(void)
51 struct extent_map
*em
;
52 em
= kmem_cache_zalloc(extent_map_cache
, GFP_NOFS
);
55 RB_CLEAR_NODE(&em
->rb_node
);
57 em
->compress_type
= BTRFS_COMPRESS_NONE
;
59 refcount_set(&em
->refs
, 1);
60 INIT_LIST_HEAD(&em
->list
);
65 * free_extent_map - drop reference count of an extent_map
66 * @em: extent map being released
68 * Drops the reference out on @em by one and free the structure
69 * if the reference count hits zero.
71 void free_extent_map(struct extent_map
*em
)
75 WARN_ON(refcount_read(&em
->refs
) == 0);
76 if (refcount_dec_and_test(&em
->refs
)) {
77 WARN_ON(extent_map_in_tree(em
));
78 WARN_ON(!list_empty(&em
->list
));
79 if (test_bit(EXTENT_FLAG_FS_MAPPING
, &em
->flags
))
80 kfree(em
->map_lookup
);
81 kmem_cache_free(extent_map_cache
, em
);
85 /* simple helper to do math around the end of an extent, handling wrap */
86 static u64
range_end(u64 start
, u64 len
)
88 if (start
+ len
< start
)
93 static int tree_insert(struct rb_root
*root
, struct extent_map
*em
)
95 struct rb_node
**p
= &root
->rb_node
;
96 struct rb_node
*parent
= NULL
;
97 struct extent_map
*entry
= NULL
;
98 struct rb_node
*orig_parent
= NULL
;
99 u64 end
= range_end(em
->start
, em
->len
);
103 entry
= rb_entry(parent
, struct extent_map
, rb_node
);
105 if (em
->start
< entry
->start
)
107 else if (em
->start
>= extent_map_end(entry
))
113 orig_parent
= parent
;
114 while (parent
&& em
->start
>= extent_map_end(entry
)) {
115 parent
= rb_next(parent
);
116 entry
= rb_entry(parent
, struct extent_map
, rb_node
);
119 if (end
> entry
->start
&& em
->start
< extent_map_end(entry
))
122 parent
= orig_parent
;
123 entry
= rb_entry(parent
, struct extent_map
, rb_node
);
124 while (parent
&& em
->start
< entry
->start
) {
125 parent
= rb_prev(parent
);
126 entry
= rb_entry(parent
, struct extent_map
, rb_node
);
129 if (end
> entry
->start
&& em
->start
< extent_map_end(entry
))
132 rb_link_node(&em
->rb_node
, orig_parent
, p
);
133 rb_insert_color(&em
->rb_node
, root
);
138 * search through the tree for an extent_map with a given offset. If
139 * it can't be found, try to find some neighboring extents
141 static struct rb_node
*__tree_search(struct rb_root
*root
, u64 offset
,
142 struct rb_node
**prev_ret
,
143 struct rb_node
**next_ret
)
145 struct rb_node
*n
= root
->rb_node
;
146 struct rb_node
*prev
= NULL
;
147 struct rb_node
*orig_prev
= NULL
;
148 struct extent_map
*entry
;
149 struct extent_map
*prev_entry
= NULL
;
152 entry
= rb_entry(n
, struct extent_map
, rb_node
);
156 if (offset
< entry
->start
)
158 else if (offset
>= extent_map_end(entry
))
166 while (prev
&& offset
>= extent_map_end(prev_entry
)) {
167 prev
= rb_next(prev
);
168 prev_entry
= rb_entry(prev
, struct extent_map
, rb_node
);
175 prev_entry
= rb_entry(prev
, struct extent_map
, rb_node
);
176 while (prev
&& offset
< prev_entry
->start
) {
177 prev
= rb_prev(prev
);
178 prev_entry
= rb_entry(prev
, struct extent_map
, rb_node
);
185 /* check to see if two extent_map structs are adjacent and safe to merge */
186 static int mergable_maps(struct extent_map
*prev
, struct extent_map
*next
)
188 if (test_bit(EXTENT_FLAG_PINNED
, &prev
->flags
))
192 * don't merge compressed extents, we need to know their
195 if (test_bit(EXTENT_FLAG_COMPRESSED
, &prev
->flags
))
198 if (test_bit(EXTENT_FLAG_LOGGING
, &prev
->flags
) ||
199 test_bit(EXTENT_FLAG_LOGGING
, &next
->flags
))
203 * We don't want to merge stuff that hasn't been written to the log yet
204 * since it may not reflect exactly what is on disk, and that would be
207 if (!list_empty(&prev
->list
) || !list_empty(&next
->list
))
210 if (extent_map_end(prev
) == next
->start
&&
211 prev
->flags
== next
->flags
&&
212 prev
->bdev
== next
->bdev
&&
213 ((next
->block_start
== EXTENT_MAP_HOLE
&&
214 prev
->block_start
== EXTENT_MAP_HOLE
) ||
215 (next
->block_start
== EXTENT_MAP_INLINE
&&
216 prev
->block_start
== EXTENT_MAP_INLINE
) ||
217 (next
->block_start
== EXTENT_MAP_DELALLOC
&&
218 prev
->block_start
== EXTENT_MAP_DELALLOC
) ||
219 (next
->block_start
< EXTENT_MAP_LAST_BYTE
- 1 &&
220 next
->block_start
== extent_map_block_end(prev
)))) {
226 static void try_merge_map(struct extent_map_tree
*tree
, struct extent_map
*em
)
228 struct extent_map
*merge
= NULL
;
232 * We can't modify an extent map that is in the tree and that is being
233 * used by another task, as it can cause that other task to see it in
234 * inconsistent state during the merging. We always have 1 reference for
235 * the tree and 1 for this task (which is unpinning the extent map or
236 * clearing the logging flag), so anything > 2 means it's being used by
239 if (refcount_read(&em
->refs
) > 2)
242 if (em
->start
!= 0) {
243 rb
= rb_prev(&em
->rb_node
);
245 merge
= rb_entry(rb
, struct extent_map
, rb_node
);
246 if (rb
&& mergable_maps(merge
, em
)) {
247 em
->start
= merge
->start
;
248 em
->orig_start
= merge
->orig_start
;
249 em
->len
+= merge
->len
;
250 em
->block_len
+= merge
->block_len
;
251 em
->block_start
= merge
->block_start
;
252 em
->mod_len
= (em
->mod_len
+ em
->mod_start
) - merge
->mod_start
;
253 em
->mod_start
= merge
->mod_start
;
254 em
->generation
= max(em
->generation
, merge
->generation
);
256 rb_erase(&merge
->rb_node
, &tree
->map
);
257 RB_CLEAR_NODE(&merge
->rb_node
);
258 free_extent_map(merge
);
262 rb
= rb_next(&em
->rb_node
);
264 merge
= rb_entry(rb
, struct extent_map
, rb_node
);
265 if (rb
&& mergable_maps(em
, merge
)) {
266 em
->len
+= merge
->len
;
267 em
->block_len
+= merge
->block_len
;
268 rb_erase(&merge
->rb_node
, &tree
->map
);
269 RB_CLEAR_NODE(&merge
->rb_node
);
270 em
->mod_len
= (merge
->mod_start
+ merge
->mod_len
) - em
->mod_start
;
271 em
->generation
= max(em
->generation
, merge
->generation
);
272 free_extent_map(merge
);
277 * unpin_extent_cache - unpin an extent from the cache
278 * @tree: tree to unpin the extent in
279 * @start: logical offset in the file
280 * @len: length of the extent
281 * @gen: generation that this extent has been modified in
283 * Called after an extent has been written to disk properly. Set the generation
284 * to the generation that actually added the file item to the inode so we know
285 * we need to sync this extent when we call fsync().
287 int unpin_extent_cache(struct extent_map_tree
*tree
, u64 start
, u64 len
,
291 struct extent_map
*em
;
292 bool prealloc
= false;
294 write_lock(&tree
->lock
);
295 em
= lookup_extent_mapping(tree
, start
, len
);
297 WARN_ON(!em
|| em
->start
!= start
);
302 em
->generation
= gen
;
303 clear_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
304 em
->mod_start
= em
->start
;
305 em
->mod_len
= em
->len
;
307 if (test_bit(EXTENT_FLAG_FILLING
, &em
->flags
)) {
309 clear_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
312 try_merge_map(tree
, em
);
315 em
->mod_start
= em
->start
;
316 em
->mod_len
= em
->len
;
321 write_unlock(&tree
->lock
);
326 void clear_em_logging(struct extent_map_tree
*tree
, struct extent_map
*em
)
328 clear_bit(EXTENT_FLAG_LOGGING
, &em
->flags
);
329 if (extent_map_in_tree(em
))
330 try_merge_map(tree
, em
);
333 static inline void setup_extent_mapping(struct extent_map_tree
*tree
,
334 struct extent_map
*em
,
337 refcount_inc(&em
->refs
);
338 em
->mod_start
= em
->start
;
339 em
->mod_len
= em
->len
;
342 list_move(&em
->list
, &tree
->modified_extents
);
344 try_merge_map(tree
, em
);
348 * add_extent_mapping - add new extent map to the extent tree
349 * @tree: tree to insert new map in
352 * Insert @em into @tree or perform a simple forward/backward merge with
353 * existing mappings. The extent_map struct passed in will be inserted
354 * into the tree directly, with an additional reference taken, or a
355 * reference dropped if the merge attempt was successful.
357 int add_extent_mapping(struct extent_map_tree
*tree
,
358 struct extent_map
*em
, int modified
)
362 ret
= tree_insert(&tree
->map
, em
);
366 setup_extent_mapping(tree
, em
, modified
);
371 static struct extent_map
*
372 __lookup_extent_mapping(struct extent_map_tree
*tree
,
373 u64 start
, u64 len
, int strict
)
375 struct extent_map
*em
;
376 struct rb_node
*rb_node
;
377 struct rb_node
*prev
= NULL
;
378 struct rb_node
*next
= NULL
;
379 u64 end
= range_end(start
, len
);
381 rb_node
= __tree_search(&tree
->map
, start
, &prev
, &next
);
391 em
= rb_entry(rb_node
, struct extent_map
, rb_node
);
393 if (strict
&& !(end
> em
->start
&& start
< extent_map_end(em
)))
396 refcount_inc(&em
->refs
);
401 * lookup_extent_mapping - lookup extent_map
402 * @tree: tree to lookup in
403 * @start: byte offset to start the search
404 * @len: length of the lookup range
406 * Find and return the first extent_map struct in @tree that intersects the
407 * [start, len] range. There may be additional objects in the tree that
408 * intersect, so check the object returned carefully to make sure that no
409 * additional lookups are needed.
411 struct extent_map
*lookup_extent_mapping(struct extent_map_tree
*tree
,
414 return __lookup_extent_mapping(tree
, start
, len
, 1);
418 * search_extent_mapping - find a nearby extent map
419 * @tree: tree to lookup in
420 * @start: byte offset to start the search
421 * @len: length of the lookup range
423 * Find and return the first extent_map struct in @tree that intersects the
424 * [start, len] range.
426 * If one can't be found, any nearby extent may be returned
428 struct extent_map
*search_extent_mapping(struct extent_map_tree
*tree
,
431 return __lookup_extent_mapping(tree
, start
, len
, 0);
435 * remove_extent_mapping - removes an extent_map from the extent tree
436 * @tree: extent tree to remove from
437 * @em: extent map being removed
439 * Removes @em from @tree. No reference counts are dropped, and no checks
440 * are done to see if the range is in use
442 int remove_extent_mapping(struct extent_map_tree
*tree
, struct extent_map
*em
)
446 WARN_ON(test_bit(EXTENT_FLAG_PINNED
, &em
->flags
));
447 rb_erase(&em
->rb_node
, &tree
->map
);
448 if (!test_bit(EXTENT_FLAG_LOGGING
, &em
->flags
))
449 list_del_init(&em
->list
);
450 RB_CLEAR_NODE(&em
->rb_node
);
454 void replace_extent_mapping(struct extent_map_tree
*tree
,
455 struct extent_map
*cur
,
456 struct extent_map
*new,
459 WARN_ON(test_bit(EXTENT_FLAG_PINNED
, &cur
->flags
));
460 ASSERT(extent_map_in_tree(cur
));
461 if (!test_bit(EXTENT_FLAG_LOGGING
, &cur
->flags
))
462 list_del_init(&cur
->list
);
463 rb_replace_node(&cur
->rb_node
, &new->rb_node
, &tree
->map
);
464 RB_CLEAR_NODE(&cur
->rb_node
);
466 setup_extent_mapping(tree
, new, modified
);