1 // SPDX-License-Identifier: GPL-2.0
4 #include <linux/slab.h>
5 #include <linux/spinlock.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 __cold
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
;
231 if (em
->start
!= 0) {
232 rb
= rb_prev(&em
->rb_node
);
234 merge
= rb_entry(rb
, struct extent_map
, rb_node
);
235 if (rb
&& mergable_maps(merge
, em
)) {
236 em
->start
= merge
->start
;
237 em
->orig_start
= merge
->orig_start
;
238 em
->len
+= merge
->len
;
239 em
->block_len
+= merge
->block_len
;
240 em
->block_start
= merge
->block_start
;
241 em
->mod_len
= (em
->mod_len
+ em
->mod_start
) - merge
->mod_start
;
242 em
->mod_start
= merge
->mod_start
;
243 em
->generation
= max(em
->generation
, merge
->generation
);
245 rb_erase(&merge
->rb_node
, &tree
->map
);
246 RB_CLEAR_NODE(&merge
->rb_node
);
247 free_extent_map(merge
);
251 rb
= rb_next(&em
->rb_node
);
253 merge
= rb_entry(rb
, struct extent_map
, rb_node
);
254 if (rb
&& mergable_maps(em
, merge
)) {
255 em
->len
+= merge
->len
;
256 em
->block_len
+= merge
->block_len
;
257 rb_erase(&merge
->rb_node
, &tree
->map
);
258 RB_CLEAR_NODE(&merge
->rb_node
);
259 em
->mod_len
= (merge
->mod_start
+ merge
->mod_len
) - em
->mod_start
;
260 em
->generation
= max(em
->generation
, merge
->generation
);
261 free_extent_map(merge
);
266 * unpin_extent_cache - unpin an extent from the cache
267 * @tree: tree to unpin the extent in
268 * @start: logical offset in the file
269 * @len: length of the extent
270 * @gen: generation that this extent has been modified in
272 * Called after an extent has been written to disk properly. Set the generation
273 * to the generation that actually added the file item to the inode so we know
274 * we need to sync this extent when we call fsync().
276 int unpin_extent_cache(struct extent_map_tree
*tree
, u64 start
, u64 len
,
280 struct extent_map
*em
;
281 bool prealloc
= false;
283 write_lock(&tree
->lock
);
284 em
= lookup_extent_mapping(tree
, start
, len
);
286 WARN_ON(!em
|| em
->start
!= start
);
291 em
->generation
= gen
;
292 clear_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
293 em
->mod_start
= em
->start
;
294 em
->mod_len
= em
->len
;
296 if (test_bit(EXTENT_FLAG_FILLING
, &em
->flags
)) {
298 clear_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
301 try_merge_map(tree
, em
);
304 em
->mod_start
= em
->start
;
305 em
->mod_len
= em
->len
;
310 write_unlock(&tree
->lock
);
315 void clear_em_logging(struct extent_map_tree
*tree
, struct extent_map
*em
)
317 clear_bit(EXTENT_FLAG_LOGGING
, &em
->flags
);
318 if (extent_map_in_tree(em
))
319 try_merge_map(tree
, em
);
322 static inline void setup_extent_mapping(struct extent_map_tree
*tree
,
323 struct extent_map
*em
,
326 refcount_inc(&em
->refs
);
327 em
->mod_start
= em
->start
;
328 em
->mod_len
= em
->len
;
331 list_move(&em
->list
, &tree
->modified_extents
);
333 try_merge_map(tree
, em
);
337 * add_extent_mapping - add new extent map to the extent tree
338 * @tree: tree to insert new map in
341 * Insert @em into @tree or perform a simple forward/backward merge with
342 * existing mappings. The extent_map struct passed in will be inserted
343 * into the tree directly, with an additional reference taken, or a
344 * reference dropped if the merge attempt was successful.
346 int add_extent_mapping(struct extent_map_tree
*tree
,
347 struct extent_map
*em
, int modified
)
351 ret
= tree_insert(&tree
->map
, em
);
355 setup_extent_mapping(tree
, em
, modified
);
360 static struct extent_map
*
361 __lookup_extent_mapping(struct extent_map_tree
*tree
,
362 u64 start
, u64 len
, int strict
)
364 struct extent_map
*em
;
365 struct rb_node
*rb_node
;
366 struct rb_node
*prev
= NULL
;
367 struct rb_node
*next
= NULL
;
368 u64 end
= range_end(start
, len
);
370 rb_node
= __tree_search(&tree
->map
, start
, &prev
, &next
);
380 em
= rb_entry(rb_node
, struct extent_map
, rb_node
);
382 if (strict
&& !(end
> em
->start
&& start
< extent_map_end(em
)))
385 refcount_inc(&em
->refs
);
390 * lookup_extent_mapping - lookup extent_map
391 * @tree: tree to lookup in
392 * @start: byte offset to start the search
393 * @len: length of the lookup range
395 * Find and return the first extent_map struct in @tree that intersects the
396 * [start, len] range. There may be additional objects in the tree that
397 * intersect, so check the object returned carefully to make sure that no
398 * additional lookups are needed.
400 struct extent_map
*lookup_extent_mapping(struct extent_map_tree
*tree
,
403 return __lookup_extent_mapping(tree
, start
, len
, 1);
407 * search_extent_mapping - find a nearby extent map
408 * @tree: tree to lookup in
409 * @start: byte offset to start the search
410 * @len: length of the lookup range
412 * Find and return the first extent_map struct in @tree that intersects the
413 * [start, len] range.
415 * If one can't be found, any nearby extent may be returned
417 struct extent_map
*search_extent_mapping(struct extent_map_tree
*tree
,
420 return __lookup_extent_mapping(tree
, start
, len
, 0);
424 * remove_extent_mapping - removes an extent_map from the extent tree
425 * @tree: extent tree to remove from
426 * @em: extent map being removed
428 * Removes @em from @tree. No reference counts are dropped, and no checks
429 * are done to see if the range is in use
431 int remove_extent_mapping(struct extent_map_tree
*tree
, struct extent_map
*em
)
435 WARN_ON(test_bit(EXTENT_FLAG_PINNED
, &em
->flags
));
436 rb_erase(&em
->rb_node
, &tree
->map
);
437 if (!test_bit(EXTENT_FLAG_LOGGING
, &em
->flags
))
438 list_del_init(&em
->list
);
439 RB_CLEAR_NODE(&em
->rb_node
);
443 void replace_extent_mapping(struct extent_map_tree
*tree
,
444 struct extent_map
*cur
,
445 struct extent_map
*new,
448 WARN_ON(test_bit(EXTENT_FLAG_PINNED
, &cur
->flags
));
449 ASSERT(extent_map_in_tree(cur
));
450 if (!test_bit(EXTENT_FLAG_LOGGING
, &cur
->flags
))
451 list_del_init(&cur
->list
);
452 rb_replace_node(&cur
->rb_node
, &new->rb_node
, &tree
->map
);
453 RB_CLEAR_NODE(&cur
->rb_node
);
455 setup_extent_mapping(tree
, new, modified
);
458 static struct extent_map
*next_extent_map(struct extent_map
*em
)
460 struct rb_node
*next
;
462 next
= rb_next(&em
->rb_node
);
465 return container_of(next
, struct extent_map
, rb_node
);
468 static struct extent_map
*prev_extent_map(struct extent_map
*em
)
470 struct rb_node
*prev
;
472 prev
= rb_prev(&em
->rb_node
);
475 return container_of(prev
, struct extent_map
, rb_node
);
478 /* helper for btfs_get_extent. Given an existing extent in the tree,
479 * the existing extent is the nearest extent to map_start,
480 * and an extent that you want to insert, deal with overlap and insert
481 * the best fitted new extent into the tree.
483 static noinline
int merge_extent_mapping(struct extent_map_tree
*em_tree
,
484 struct extent_map
*existing
,
485 struct extent_map
*em
,
488 struct extent_map
*prev
;
489 struct extent_map
*next
;
494 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
496 if (existing
->start
> map_start
) {
498 prev
= prev_extent_map(next
);
501 next
= next_extent_map(prev
);
504 start
= prev
? extent_map_end(prev
) : em
->start
;
505 start
= max_t(u64
, start
, em
->start
);
506 end
= next
? next
->start
: extent_map_end(em
);
507 end
= min_t(u64
, end
, extent_map_end(em
));
508 start_diff
= start
- em
->start
;
510 em
->len
= end
- start
;
511 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
512 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
513 em
->block_start
+= start_diff
;
514 em
->block_len
= em
->len
;
516 return add_extent_mapping(em_tree
, em
, 0);
520 * btrfs_add_extent_mapping - add extent mapping into em_tree
521 * @em_tree - the extent tree into which we want to insert the extent mapping
522 * @em_in - extent we are inserting
523 * @start - start of the logical range btrfs_get_extent() is requesting
524 * @len - length of the logical range btrfs_get_extent() is requesting
526 * Note that @em_in's range may be different from [start, start+len),
527 * but they must be overlapped.
529 * Insert @em_in into @em_tree. In case there is an overlapping range, handle
530 * the -EEXIST by either:
531 * a) Returning the existing extent in @em_in if @start is within the
533 * b) Merge the existing extent with @em_in passed in.
535 * Return 0 on success, otherwise -EEXIST.
538 int btrfs_add_extent_mapping(struct extent_map_tree
*em_tree
,
539 struct extent_map
**em_in
, u64 start
, u64 len
)
542 struct extent_map
*em
= *em_in
;
544 ret
= add_extent_mapping(em_tree
, em
, 0);
545 /* it is possible that someone inserted the extent into the tree
546 * while we had the lock dropped. It is also possible that
547 * an overlapping map exists in the tree
549 if (ret
== -EEXIST
) {
550 struct extent_map
*existing
;
554 existing
= search_extent_mapping(em_tree
, start
, len
);
556 trace_btrfs_handle_em_exist(existing
, em
, start
, len
);
559 * existing will always be non-NULL, since there must be
560 * extent causing the -EEXIST.
562 if (start
>= existing
->start
&&
563 start
< extent_map_end(existing
)) {
568 u64 orig_start
= em
->start
;
569 u64 orig_len
= em
->len
;
572 * The existing extent map is the one nearest to
573 * the [start, start + len) range which overlaps
575 ret
= merge_extent_mapping(em_tree
, existing
,
581 "unexpected error %d: merge existing(start %llu len %llu) with em(start %llu len %llu)\n",
582 ret
, existing
->start
, existing
->len
,
583 orig_start
, orig_len
);
585 free_extent_map(existing
);
589 ASSERT(ret
== 0 || ret
== -EEXIST
);