1 // SPDX-License-Identifier: GPL-2.0
4 #include <linux/slab.h>
5 #include <linux/spinlock.h>
8 #include "extent_map.h"
9 #include "compression.h"
10 #include "btrfs_inode.h"
14 static struct kmem_cache *extent_map_cache;
16 int __init btrfs_extent_map_init(void)
18 extent_map_cache = kmem_cache_create("btrfs_extent_map",
19 sizeof(struct extent_map), 0, 0, NULL);
20 if (!extent_map_cache)
25 void __cold btrfs_extent_map_exit(void)
27 kmem_cache_destroy(extent_map_cache);
31 * Initialize the extent tree @tree. Should be called for each new inode or
32 * other user of the extent_map interface.
34 void btrfs_extent_map_tree_init(struct extent_map_tree *tree)
37 INIT_LIST_HEAD(&tree->modified_extents);
38 rwlock_init(&tree->lock);
42 * Allocate a new extent_map structure. The new structure is returned with a
43 * reference count of one and needs to be freed using free_extent_map()
45 struct extent_map *btrfs_alloc_extent_map(void)
47 struct extent_map *em;
48 em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS);
51 RB_CLEAR_NODE(&em->rb_node);
52 refcount_set(&em->refs, 1);
53 INIT_LIST_HEAD(&em->list);
58 * Drop the reference out on @em by one and free the structure if the reference
61 void btrfs_free_extent_map(struct extent_map *em)
65 if (refcount_dec_and_test(&em->refs)) {
66 WARN_ON(btrfs_extent_map_in_tree(em));
67 WARN_ON(!list_empty(&em->list));
68 kmem_cache_free(extent_map_cache, em);
72 /* Do the math around the end of an extent, handling wrapping. */
73 static u64 range_end(u64 start, u64 len)
75 if (start + len < start)
80 static void remove_em(struct btrfs_inode *inode, struct extent_map *em)
82 struct btrfs_fs_info *fs_info = inode->root->fs_info;
84 rb_erase(&em->rb_node, &inode->extent_tree.root);
85 RB_CLEAR_NODE(&em->rb_node);
87 if (!btrfs_is_testing(fs_info) && is_fstree(btrfs_root_id(inode->root)))
88 percpu_counter_dec(&fs_info->evictable_extent_maps);
91 static int tree_insert(struct rb_root *root, struct extent_map *em)
93 struct rb_node **p = &root->rb_node;
94 struct rb_node *parent = NULL;
95 struct extent_map *entry = NULL;
96 struct rb_node *orig_parent = NULL;
97 u64 end = range_end(em->start, em->len);
101 entry = rb_entry(parent, struct extent_map, rb_node);
103 if (em->start < entry->start)
105 else if (em->start >= btrfs_extent_map_end(entry))
111 orig_parent = parent;
112 while (parent && em->start >= btrfs_extent_map_end(entry)) {
113 parent = rb_next(parent);
114 entry = rb_entry(parent, struct extent_map, rb_node);
117 if (end > entry->start && em->start < btrfs_extent_map_end(entry))
120 parent = orig_parent;
121 entry = rb_entry(parent, struct extent_map, rb_node);
122 while (parent && em->start < entry->start) {
123 parent = rb_prev(parent);
124 entry = rb_entry(parent, struct extent_map, rb_node);
127 if (end > entry->start && em->start < btrfs_extent_map_end(entry))
130 rb_link_node(&em->rb_node, orig_parent, p);
131 rb_insert_color(&em->rb_node, root);
136 * Search through the tree for an extent_map with a given offset. If it can't
137 * be found, try to find some neighboring extents
139 static struct rb_node *tree_search(struct rb_root *root, u64 offset,
140 struct rb_node **prev_or_next_ret)
142 struct rb_node *n = root->rb_node;
143 struct rb_node *prev = NULL;
144 struct rb_node *orig_prev = NULL;
145 struct extent_map *entry;
146 struct extent_map *prev_entry = NULL;
148 ASSERT(prev_or_next_ret);
151 entry = rb_entry(n, struct extent_map, rb_node);
155 if (offset < entry->start)
157 else if (offset >= btrfs_extent_map_end(entry))
164 while (prev && offset >= btrfs_extent_map_end(prev_entry)) {
165 prev = rb_next(prev);
166 prev_entry = rb_entry(prev, struct extent_map, rb_node);
170 * Previous extent map found, return as in this case the caller does not
171 * care about the next one.
174 *prev_or_next_ret = prev;
179 prev_entry = rb_entry(prev, struct extent_map, rb_node);
180 while (prev && offset < prev_entry->start) {
181 prev = rb_prev(prev);
182 prev_entry = rb_entry(prev, struct extent_map, rb_node);
184 *prev_or_next_ret = prev;
189 static inline u64 extent_map_block_len(const struct extent_map *em)
191 if (btrfs_extent_map_is_compressed(em))
192 return em->disk_num_bytes;
196 static inline u64 extent_map_block_end(const struct extent_map *em)
198 const u64 block_start = btrfs_extent_map_block_start(em);
199 const u64 block_end = block_start + extent_map_block_len(em);
201 if (block_end < block_start)
207 static bool can_merge_extent_map(const struct extent_map *em)
209 if (em->flags & EXTENT_FLAG_PINNED)
212 /* Don't merge compressed extents, we need to know their actual size. */
213 if (btrfs_extent_map_is_compressed(em))
216 if (em->flags & EXTENT_FLAG_LOGGING)
220 * We don't want to merge stuff that hasn't been written to the log yet
221 * since it may not reflect exactly what is on disk, and that would be
224 if (!list_empty(&em->list))
230 /* Check to see if two extent_map structs are adjacent and safe to merge. */
231 static bool mergeable_maps(const struct extent_map *prev, const struct extent_map *next)
233 if (btrfs_extent_map_end(prev) != next->start)
237 * The merged flag is not an on-disk flag, it just indicates we had the
238 * extent maps of 2 (or more) adjacent extents merged, so factor it out.
240 if ((prev->flags & ~EXTENT_FLAG_MERGED) !=
241 (next->flags & ~EXTENT_FLAG_MERGED))
244 if (next->disk_bytenr < EXTENT_MAP_LAST_BYTE - 1)
245 return btrfs_extent_map_block_start(next) == extent_map_block_end(prev);
247 /* HOLES and INLINE extents. */
248 return next->disk_bytenr == prev->disk_bytenr;
252 * Handle the on-disk data extents merge for @prev and @next.
254 * @prev: left extent to merge
255 * @next: right extent to merge
256 * @merged: the extent we will not discard after the merge; updated with new values
258 * After this, one of the two extents is the new merged extent and the other is
259 * removed from the tree and likely freed. Note that @merged is one of @prev/@next
260 * so there is const/non-const aliasing occurring here.
262 * Only touches disk_bytenr/disk_num_bytes/offset/ram_bytes.
263 * For now only uncompressed regular extent can be merged.
265 static void merge_ondisk_extents(const struct extent_map *prev, const struct extent_map *next,
266 struct extent_map *merged)
269 u64 new_disk_num_bytes;
272 /* @prev and @next should not be compressed. */
273 ASSERT(!btrfs_extent_map_is_compressed(prev));
274 ASSERT(!btrfs_extent_map_is_compressed(next));
277 * There are two different cases where @prev and @next can be merged.
279 * 1) They are referring to the same data extent:
281 * |<----- data extent A ----->|
282 * |<- prev ->|<- next ->|
284 * 2) They are referring to different data extents but still adjacent:
286 * |<-- data extent A -->|<-- data extent B -->|
287 * |<- prev ->|<- next ->|
289 * The calculation here always merges the data extents first, then updates
290 * @offset using the new data extents.
292 * For case 1), the merged data extent would be the same.
293 * For case 2), we just merge the two data extents into one.
295 new_disk_bytenr = min(prev->disk_bytenr, next->disk_bytenr);
296 new_disk_num_bytes = max(prev->disk_bytenr + prev->disk_num_bytes,
297 next->disk_bytenr + next->disk_num_bytes) -
299 new_offset = prev->disk_bytenr + prev->offset - new_disk_bytenr;
301 merged->disk_bytenr = new_disk_bytenr;
302 merged->disk_num_bytes = new_disk_num_bytes;
303 merged->ram_bytes = new_disk_num_bytes;
304 merged->offset = new_offset;
307 static void dump_extent_map(struct btrfs_fs_info *fs_info, const char *prefix,
308 struct extent_map *em)
310 if (!IS_ENABLED(CONFIG_BTRFS_DEBUG))
313 "%s, start=%llu len=%llu disk_bytenr=%llu disk_num_bytes=%llu ram_bytes=%llu offset=%llu flags=0x%x",
314 prefix, em->start, em->len, em->disk_bytenr, em->disk_num_bytes,
315 em->ram_bytes, em->offset, em->flags);
319 /* Internal sanity checks for btrfs debug builds. */
320 static void validate_extent_map(struct btrfs_fs_info *fs_info, struct extent_map *em)
322 if (!IS_ENABLED(CONFIG_BTRFS_DEBUG))
324 if (em->disk_bytenr < EXTENT_MAP_LAST_BYTE) {
325 if (em->disk_num_bytes == 0)
326 dump_extent_map(fs_info, "zero disk_num_bytes", em);
327 if (em->offset + em->len > em->ram_bytes)
328 dump_extent_map(fs_info, "ram_bytes too small", em);
329 if (em->offset + em->len > em->disk_num_bytes &&
330 !btrfs_extent_map_is_compressed(em))
331 dump_extent_map(fs_info, "disk_num_bytes too small", em);
332 if (!btrfs_extent_map_is_compressed(em) &&
333 em->ram_bytes != em->disk_num_bytes)
334 dump_extent_map(fs_info,
335 "ram_bytes mismatch with disk_num_bytes for non-compressed em",
337 } else if (em->offset) {
338 dump_extent_map(fs_info, "non-zero offset for hole/inline", em);
342 static void try_merge_map(struct btrfs_inode *inode, struct extent_map *em)
344 struct btrfs_fs_info *fs_info = inode->root->fs_info;
345 struct extent_map *merge = NULL;
349 * We can't modify an extent map that is in the tree and that is being
350 * used by another task, as it can cause that other task to see it in
351 * inconsistent state during the merging. We always have 1 reference for
352 * the tree and 1 for this task (which is unpinning the extent map or
353 * clearing the logging flag), so anything > 2 means it's being used by
356 if (refcount_read(&em->refs) > 2)
359 if (!can_merge_extent_map(em))
362 if (em->start != 0) {
363 rb = rb_prev(&em->rb_node);
364 merge = rb_entry_safe(rb, struct extent_map, rb_node);
366 if (rb && can_merge_extent_map(merge) && mergeable_maps(merge, em)) {
367 em->start = merge->start;
368 em->len += merge->len;
369 em->generation = max(em->generation, merge->generation);
371 if (em->disk_bytenr < EXTENT_MAP_LAST_BYTE)
372 merge_ondisk_extents(merge, em, em);
373 em->flags |= EXTENT_FLAG_MERGED;
375 validate_extent_map(fs_info, em);
376 remove_em(inode, merge);
377 btrfs_free_extent_map(merge);
381 rb = rb_next(&em->rb_node);
382 merge = rb_entry_safe(rb, struct extent_map, rb_node);
384 if (rb && can_merge_extent_map(merge) && mergeable_maps(em, merge)) {
385 em->len += merge->len;
386 if (em->disk_bytenr < EXTENT_MAP_LAST_BYTE)
387 merge_ondisk_extents(em, merge, em);
388 validate_extent_map(fs_info, em);
389 em->generation = max(em->generation, merge->generation);
390 em->flags |= EXTENT_FLAG_MERGED;
391 remove_em(inode, merge);
392 btrfs_free_extent_map(merge);
397 * Unpin an extent from the cache.
399 * @inode: the inode from which we are unpinning an extent range
400 * @start: logical offset in the file
401 * @len: length of the extent
402 * @gen: generation that this extent has been modified in
404 * Called after an extent has been written to disk properly. Set the generation
405 * to the generation that actually added the file item to the inode so we know
406 * we need to sync this extent when we call fsync().
408 * Returns: 0 on success
409 * -ENOENT when the extent is not found in the tree
410 * -EUCLEAN if the found extent does not match the expected start
412 int btrfs_unpin_extent_cache(struct btrfs_inode *inode, u64 start, u64 len, u64 gen)
414 struct btrfs_fs_info *fs_info = inode->root->fs_info;
415 struct extent_map_tree *tree = &inode->extent_tree;
417 struct extent_map *em;
419 write_lock(&tree->lock);
420 em = btrfs_lookup_extent_mapping(tree, start, len);
424 "no extent map found for inode %llu (root %lld) when unpinning extent range [%llu, %llu), generation %llu",
425 btrfs_ino(inode), btrfs_root_id(inode->root),
426 start, start + len, gen);
431 if (WARN_ON(em->start != start)) {
433 "found extent map for inode %llu (root %lld) with unexpected start offset %llu when unpinning extent range [%llu, %llu), generation %llu",
434 btrfs_ino(inode), btrfs_root_id(inode->root),
435 em->start, start, start + len, gen);
440 em->generation = gen;
441 em->flags &= ~EXTENT_FLAG_PINNED;
443 try_merge_map(inode, em);
446 write_unlock(&tree->lock);
447 btrfs_free_extent_map(em);
452 void btrfs_clear_em_logging(struct btrfs_inode *inode, struct extent_map *em)
454 lockdep_assert_held_write(&inode->extent_tree.lock);
456 em->flags &= ~EXTENT_FLAG_LOGGING;
457 if (btrfs_extent_map_in_tree(em))
458 try_merge_map(inode, em);
461 static inline void setup_extent_mapping(struct btrfs_inode *inode,
462 struct extent_map *em,
465 refcount_inc(&em->refs);
467 ASSERT(list_empty(&em->list));
470 list_add(&em->list, &inode->extent_tree.modified_extents);
472 try_merge_map(inode, em);
476 * Add a new extent map to an inode's extent map tree.
478 * @inode: the target inode
480 * @modified: indicate whether the given @em should be added to the
481 * modified list, which indicates the extent needs to be logged
483 * Insert @em into the @inode's extent map tree or perform a simple
484 * forward/backward merge with existing mappings. The extent_map struct passed
485 * in will be inserted into the tree directly, with an additional reference
486 * taken, or a reference dropped if the merge attempt was successful.
488 static int add_extent_mapping(struct btrfs_inode *inode,
489 struct extent_map *em, int modified)
491 struct extent_map_tree *tree = &inode->extent_tree;
492 struct btrfs_root *root = inode->root;
493 struct btrfs_fs_info *fs_info = root->fs_info;
496 lockdep_assert_held_write(&tree->lock);
498 validate_extent_map(fs_info, em);
499 ret = tree_insert(&tree->root, em);
503 setup_extent_mapping(inode, em, modified);
505 if (!btrfs_is_testing(fs_info) && is_fstree(btrfs_root_id(root)))
506 percpu_counter_inc(&fs_info->evictable_extent_maps);
511 static struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
512 u64 start, u64 len, int strict)
514 struct extent_map *em;
515 struct rb_node *rb_node;
516 struct rb_node *prev_or_next = NULL;
517 u64 end = range_end(start, len);
519 rb_node = tree_search(&tree->root, start, &prev_or_next);
522 rb_node = prev_or_next;
527 em = rb_entry(rb_node, struct extent_map, rb_node);
529 if (strict && !(end > em->start && start < btrfs_extent_map_end(em)))
532 refcount_inc(&em->refs);
537 * Lookup extent_map that intersects @start + @len range.
539 * @tree: tree to lookup in
540 * @start: byte offset to start the search
541 * @len: length of the lookup range
543 * Find and return the first extent_map struct in @tree that intersects the
544 * [start, len] range. There may be additional objects in the tree that
545 * intersect, so check the object returned carefully to make sure that no
546 * additional lookups are needed.
548 struct extent_map *btrfs_lookup_extent_mapping(struct extent_map_tree *tree,
551 return lookup_extent_mapping(tree, start, len, 1);
555 * Find a nearby extent map intersecting @start + @len (not an exact search).
557 * @tree: tree to lookup in
558 * @start: byte offset to start the search
559 * @len: length of the lookup range
561 * Find and return the first extent_map struct in @tree that intersects the
562 * [start, len] range.
564 * If one can't be found, any nearby extent may be returned
566 struct extent_map *btrfs_search_extent_mapping(struct extent_map_tree *tree,
569 return lookup_extent_mapping(tree, start, len, 0);
573 * Remove an extent_map from its inode's extent tree.
575 * @inode: the inode the extent map belongs to
576 * @em: extent map being removed
578 * Remove @em from the extent tree of @inode. No reference counts are dropped,
579 * and no checks are done to see if the range is in use.
581 void btrfs_remove_extent_mapping(struct btrfs_inode *inode, struct extent_map *em)
583 struct extent_map_tree *tree = &inode->extent_tree;
585 lockdep_assert_held_write(&tree->lock);
587 WARN_ON(em->flags & EXTENT_FLAG_PINNED);
588 if (!(em->flags & EXTENT_FLAG_LOGGING))
589 list_del_init(&em->list);
591 remove_em(inode, em);
594 static void replace_extent_mapping(struct btrfs_inode *inode,
595 struct extent_map *cur,
596 struct extent_map *new,
599 struct btrfs_fs_info *fs_info = inode->root->fs_info;
600 struct extent_map_tree *tree = &inode->extent_tree;
602 lockdep_assert_held_write(&tree->lock);
604 validate_extent_map(fs_info, new);
606 WARN_ON(cur->flags & EXTENT_FLAG_PINNED);
607 ASSERT(btrfs_extent_map_in_tree(cur));
608 if (!(cur->flags & EXTENT_FLAG_LOGGING))
609 list_del_init(&cur->list);
610 rb_replace_node(&cur->rb_node, &new->rb_node, &tree->root);
611 RB_CLEAR_NODE(&cur->rb_node);
613 setup_extent_mapping(inode, new, modified);
616 static struct extent_map *next_extent_map(const struct extent_map *em)
618 struct rb_node *next;
620 next = rb_next(&em->rb_node);
623 return container_of(next, struct extent_map, rb_node);
626 static struct extent_map *prev_extent_map(struct extent_map *em)
628 struct rb_node *prev;
630 prev = rb_prev(&em->rb_node);
633 return container_of(prev, struct extent_map, rb_node);
637 * Helper for btrfs_get_extent. Given an existing extent in the tree,
638 * the existing extent is the nearest extent to map_start,
639 * and an extent that you want to insert, deal with overlap and insert
640 * the best fitted new extent into the tree.
642 static noinline int merge_extent_mapping(struct btrfs_inode *inode,
643 struct extent_map *existing,
644 struct extent_map *em,
647 struct extent_map *prev;
648 struct extent_map *next;
653 if (map_start < em->start || map_start >= btrfs_extent_map_end(em))
656 if (existing->start > map_start) {
658 prev = prev_extent_map(next);
661 next = next_extent_map(prev);
664 start = prev ? btrfs_extent_map_end(prev) : em->start;
665 start = max_t(u64, start, em->start);
666 end = next ? next->start : btrfs_extent_map_end(em);
667 end = min_t(u64, end, btrfs_extent_map_end(em));
668 start_diff = start - em->start;
670 em->len = end - start;
671 if (em->disk_bytenr < EXTENT_MAP_LAST_BYTE)
672 em->offset += start_diff;
673 return add_extent_mapping(inode, em, 0);
677 * Add extent mapping into an inode's extent map tree.
679 * @inode: target inode
680 * @em_in: extent we are inserting
681 * @start: start of the logical range btrfs_get_extent() is requesting
682 * @len: length of the logical range btrfs_get_extent() is requesting
684 * Note that @em_in's range may be different from [start, start+len),
685 * but they must be overlapped.
687 * Insert @em_in into the inode's extent map tree. In case there is an
688 * overlapping range, handle the -EEXIST by either:
689 * a) Returning the existing extent in @em_in if @start is within the
691 * b) Merge the existing extent with @em_in passed in.
693 * Return 0 on success, otherwise -EEXIST.
696 int btrfs_add_extent_mapping(struct btrfs_inode *inode,
697 struct extent_map **em_in, u64 start, u64 len)
700 struct extent_map *em = *em_in;
701 struct btrfs_fs_info *fs_info = inode->root->fs_info;
704 * Tree-checker should have rejected any inline extent with non-zero
705 * file offset. Here just do a sanity check.
707 if (em->disk_bytenr == EXTENT_MAP_INLINE)
708 ASSERT(em->start == 0);
710 ret = add_extent_mapping(inode, em, 0);
711 /* it is possible that someone inserted the extent into the tree
712 * while we had the lock dropped. It is also possible that
713 * an overlapping map exists in the tree
715 if (ret == -EEXIST) {
716 struct extent_map *existing;
718 existing = btrfs_search_extent_mapping(&inode->extent_tree, start, len);
720 trace_btrfs_handle_em_exist(fs_info, existing, em, start, len);
723 * existing will always be non-NULL, since there must be
724 * extent causing the -EEXIST.
726 if (start >= existing->start &&
727 start < btrfs_extent_map_end(existing)) {
728 btrfs_free_extent_map(em);
732 u64 orig_start = em->start;
733 u64 orig_len = em->len;
736 * The existing extent map is the one nearest to
737 * the [start, start + len) range which overlaps
739 ret = merge_extent_mapping(inode, existing, em, start);
741 btrfs_free_extent_map(em);
744 "extent map merge error existing [%llu, %llu) with em [%llu, %llu) start %llu",
745 existing->start, btrfs_extent_map_end(existing),
746 orig_start, orig_start + orig_len, start);
748 btrfs_free_extent_map(existing);
752 ASSERT(ret == 0 || ret == -EEXIST);
757 * Drop all extent maps from a tree in the fastest possible way, rescheduling
758 * if needed. This avoids searching the tree, from the root down to the first
759 * extent map, before each deletion.
761 static void drop_all_extent_maps_fast(struct btrfs_inode *inode)
763 struct extent_map_tree *tree = &inode->extent_tree;
764 struct rb_node *node;
766 write_lock(&tree->lock);
767 node = rb_first(&tree->root);
769 struct extent_map *em;
770 struct rb_node *next = rb_next(node);
772 em = rb_entry(node, struct extent_map, rb_node);
773 em->flags &= ~(EXTENT_FLAG_PINNED | EXTENT_FLAG_LOGGING);
774 btrfs_remove_extent_mapping(inode, em);
775 btrfs_free_extent_map(em);
777 if (cond_resched_rwlock_write(&tree->lock))
778 node = rb_first(&tree->root);
782 write_unlock(&tree->lock);
786 * Drop all extent maps in a given range.
788 * @inode: The target inode.
789 * @start: Start offset of the range.
790 * @end: End offset of the range (inclusive value).
791 * @skip_pinned: Indicate if pinned extent maps should be ignored or not.
793 * This drops all the extent maps that intersect the given range [@start, @end].
794 * Extent maps that partially overlap the range and extend behind or beyond it,
796 * The caller should have locked an appropriate file range in the inode's io
797 * tree before calling this function.
799 void btrfs_drop_extent_map_range(struct btrfs_inode *inode, u64 start, u64 end,
802 struct extent_map *split;
803 struct extent_map *split2;
804 struct extent_map *em;
805 struct extent_map_tree *em_tree = &inode->extent_tree;
806 u64 len = end - start + 1;
808 WARN_ON(end < start);
809 if (end == (u64)-1) {
810 if (start == 0 && !skip_pinned) {
811 drop_all_extent_maps_fast(inode);
816 /* Make end offset exclusive for use in the loop below. */
821 * It's ok if we fail to allocate the extent maps, see the comment near
822 * the bottom of the loop below. We only need two spare extent maps in
823 * the worst case, where the first extent map that intersects our range
824 * starts before the range and the last extent map that intersects our
825 * range ends after our range (and they might be the same extent map),
826 * because we need to split those two extent maps at the boundaries.
828 split = btrfs_alloc_extent_map();
829 split2 = btrfs_alloc_extent_map();
831 write_lock(&em_tree->lock);
832 em = btrfs_lookup_extent_mapping(em_tree, start, len);
835 /* extent_map_end() returns exclusive value (last byte + 1). */
836 const u64 em_end = btrfs_extent_map_end(em);
837 struct extent_map *next_em = NULL;
843 next_em = next_extent_map(em);
845 if (next_em->start < end)
846 refcount_inc(&next_em->refs);
852 if (skip_pinned && (em->flags & EXTENT_FLAG_PINNED)) {
859 * In case we split the extent map, we want to preserve the
860 * EXTENT_FLAG_LOGGING flag on our extent map, but we don't want
861 * it on the new extent maps.
863 em->flags &= ~(EXTENT_FLAG_PINNED | EXTENT_FLAG_LOGGING);
864 modified = !list_empty(&em->list);
867 * The extent map does not cross our target range, so no need to
868 * split it, we can remove it directly.
870 if (em->start >= start && em_end <= end)
873 gen = em->generation;
875 if (em->start < start) {
882 split->start = em->start;
883 split->len = start - em->start;
885 if (em->disk_bytenr < EXTENT_MAP_LAST_BYTE) {
886 split->disk_bytenr = em->disk_bytenr;
887 split->disk_num_bytes = em->disk_num_bytes;
888 split->offset = em->offset;
889 split->ram_bytes = em->ram_bytes;
891 split->disk_bytenr = em->disk_bytenr;
892 split->disk_num_bytes = 0;
894 split->ram_bytes = split->len;
897 split->generation = gen;
898 split->flags = flags;
899 replace_extent_mapping(inode, em, split, modified);
900 btrfs_free_extent_map(split);
912 split->len = em_end - end;
913 split->disk_bytenr = em->disk_bytenr;
914 split->flags = flags;
915 split->generation = gen;
917 if (em->disk_bytenr < EXTENT_MAP_LAST_BYTE) {
918 split->disk_num_bytes = em->disk_num_bytes;
919 split->offset = em->offset + end - em->start;
920 split->ram_bytes = em->ram_bytes;
922 split->disk_num_bytes = 0;
924 split->ram_bytes = split->len;
927 if (btrfs_extent_map_in_tree(em)) {
928 replace_extent_mapping(inode, em, split, modified);
932 ret = add_extent_mapping(inode, split, modified);
933 /* Logic error, shouldn't happen. */
935 if (WARN_ON(ret != 0) && modified)
936 btrfs_set_inode_full_sync(inode);
938 btrfs_free_extent_map(split);
942 if (btrfs_extent_map_in_tree(em)) {
944 * If the extent map is still in the tree it means that
945 * either of the following is true:
947 * 1) It fits entirely in our range (doesn't end beyond
948 * it or starts before it);
950 * 2) It starts before our range and/or ends after our
951 * range, and we were not able to allocate the extent
952 * maps for split operations, @split and @split2.
954 * If we are at case 2) then we just remove the entire
955 * extent map - this is fine since if anyone needs it to
956 * access the subranges outside our range, will just
957 * load it again from the subvolume tree's file extent
958 * item. However if the extent map was in the list of
959 * modified extents, then we must mark the inode for a
960 * full fsync, otherwise a fast fsync will miss this
961 * extent if it's new and needs to be logged.
963 if ((em->start < start || em_end > end) && modified) {
965 btrfs_set_inode_full_sync(inode);
967 btrfs_remove_extent_mapping(inode, em);
971 * Once for the tree reference (we replaced or removed the
972 * extent map from the tree).
974 btrfs_free_extent_map(em);
976 /* Once for us (for our lookup reference). */
977 btrfs_free_extent_map(em);
982 write_unlock(&em_tree->lock);
984 btrfs_free_extent_map(split);
985 btrfs_free_extent_map(split2);
989 * Replace a range in the inode's extent map tree with a new extent map.
991 * @inode: The target inode.
992 * @new_em: The new extent map to add to the inode's extent map tree.
993 * @modified: Indicate if the new extent map should be added to the list of
994 * modified extents (for fast fsync tracking).
996 * Drops all the extent maps in the inode's extent map tree that intersect the
997 * range of the new extent map and adds the new extent map to the tree.
998 * The caller should have locked an appropriate file range in the inode's io
999 * tree before calling this function.
1001 int btrfs_replace_extent_map_range(struct btrfs_inode *inode,
1002 struct extent_map *new_em,
1005 const u64 end = new_em->start + new_em->len - 1;
1006 struct extent_map_tree *tree = &inode->extent_tree;
1009 ASSERT(!btrfs_extent_map_in_tree(new_em));
1012 * The caller has locked an appropriate file range in the inode's io
1013 * tree, but getting -EEXIST when adding the new extent map can still
1014 * happen in case there are extents that partially cover the range, and
1015 * this is due to two tasks operating on different parts of the extent.
1016 * See commit 18e83ac75bfe67 ("Btrfs: fix unexpected EEXIST from
1017 * btrfs_get_extent") for an example and details.
1020 btrfs_drop_extent_map_range(inode, new_em->start, end, false);
1021 write_lock(&tree->lock);
1022 ret = add_extent_mapping(inode, new_em, modified);
1023 write_unlock(&tree->lock);
1024 } while (ret == -EEXIST);
1030 * Split off the first pre bytes from the extent_map at [start, start + len],
1031 * and set the block_start for it to new_logical.
1033 * This function is used when an ordered_extent needs to be split.
1035 int btrfs_split_extent_map(struct btrfs_inode *inode, u64 start, u64 len, u64 pre,
1038 struct extent_map_tree *em_tree = &inode->extent_tree;
1039 struct extent_map *em;
1040 struct extent_map *split_pre = NULL;
1041 struct extent_map *split_mid = NULL;
1043 unsigned long flags;
1048 split_pre = btrfs_alloc_extent_map();
1051 split_mid = btrfs_alloc_extent_map();
1057 btrfs_lock_extent(&inode->io_tree, start, start + len - 1, NULL);
1058 write_lock(&em_tree->lock);
1059 em = btrfs_lookup_extent_mapping(em_tree, start, len);
1065 ASSERT(em->len == len);
1066 ASSERT(!btrfs_extent_map_is_compressed(em));
1067 ASSERT(em->disk_bytenr < EXTENT_MAP_LAST_BYTE);
1068 ASSERT(em->flags & EXTENT_FLAG_PINNED);
1069 ASSERT(!(em->flags & EXTENT_FLAG_LOGGING));
1070 ASSERT(!list_empty(&em->list));
1073 em->flags &= ~EXTENT_FLAG_PINNED;
1075 /* First, replace the em with a new extent_map starting from * em->start */
1076 split_pre->start = em->start;
1077 split_pre->len = pre;
1078 split_pre->disk_bytenr = new_logical;
1079 split_pre->disk_num_bytes = split_pre->len;
1080 split_pre->offset = 0;
1081 split_pre->ram_bytes = split_pre->len;
1082 split_pre->flags = flags;
1083 split_pre->generation = em->generation;
1085 replace_extent_mapping(inode, em, split_pre, 1);
1088 * Now we only have an extent_map at:
1089 * [em->start, em->start + pre]
1092 /* Insert the middle extent_map. */
1093 split_mid->start = em->start + pre;
1094 split_mid->len = em->len - pre;
1095 split_mid->disk_bytenr = btrfs_extent_map_block_start(em) + pre;
1096 split_mid->disk_num_bytes = split_mid->len;
1097 split_mid->offset = 0;
1098 split_mid->ram_bytes = split_mid->len;
1099 split_mid->flags = flags;
1100 split_mid->generation = em->generation;
1101 add_extent_mapping(inode, split_mid, 1);
1104 btrfs_free_extent_map(em);
1105 /* Once for the tree */
1106 btrfs_free_extent_map(em);
1109 write_unlock(&em_tree->lock);
1110 btrfs_unlock_extent(&inode->io_tree, start, start + len - 1, NULL);
1111 btrfs_free_extent_map(split_mid);
1113 btrfs_free_extent_map(split_pre);
1117 struct btrfs_em_shrink_ctx {
1122 static long btrfs_scan_inode(struct btrfs_inode *inode, struct btrfs_em_shrink_ctx *ctx)
1124 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1125 const u64 cur_fs_gen = btrfs_get_fs_generation(fs_info);
1126 struct extent_map_tree *tree = &inode->extent_tree;
1127 long nr_dropped = 0;
1128 struct rb_node *node;
1130 lockdep_assert_held_write(&tree->lock);
1133 * Take the mmap lock so that we serialize with the inode logging phase
1134 * of fsync because we may need to set the full sync flag on the inode,
1135 * in case we have to remove extent maps in the tree's list of modified
1136 * extents. If we set the full sync flag in the inode while an fsync is
1137 * in progress, we may risk missing new extents because before the flag
1138 * is set, fsync decides to only wait for writeback to complete and then
1139 * during inode logging it sees the flag set and uses the subvolume tree
1140 * to find new extents, which may not be there yet because ordered
1141 * extents haven't completed yet.
1143 * We also do a try lock because we don't want to block for too long and
1144 * we are holding the extent map tree's lock in write mode.
1146 if (!down_read_trylock(&inode->i_mmap_lock))
1149 node = rb_first(&tree->root);
1151 struct rb_node *next = rb_next(node);
1152 struct extent_map *em;
1154 em = rb_entry(node, struct extent_map, rb_node);
1157 if (em->flags & EXTENT_FLAG_PINNED)
1161 * If the inode is in the list of modified extents (new) and its
1162 * generation is the same (or is greater than) the current fs
1163 * generation, it means it was not yet persisted so we have to
1164 * set the full sync flag so that the next fsync will not miss
1167 if (!list_empty(&em->list) && em->generation >= cur_fs_gen)
1168 btrfs_set_inode_full_sync(inode);
1170 btrfs_remove_extent_mapping(inode, em);
1171 trace_btrfs_extent_map_shrinker_remove_em(inode, em);
1172 /* Drop the reference for the tree. */
1173 btrfs_free_extent_map(em);
1176 if (ctx->scanned >= ctx->nr_to_scan)
1180 * Stop if we need to reschedule or there's contention on the
1181 * lock. This is to avoid slowing other tasks trying to take the
1184 if (need_resched() || rwlock_needbreak(&tree->lock) ||
1185 btrfs_fs_closing(fs_info))
1189 up_read(&inode->i_mmap_lock);
1194 static struct btrfs_inode *find_first_inode_to_shrink(struct btrfs_root *root,
1197 struct btrfs_inode *inode;
1198 unsigned long from = min_ino;
1200 xa_lock(&root->inodes);
1202 struct extent_map_tree *tree;
1204 inode = xa_find(&root->inodes, &from, ULONG_MAX, XA_PRESENT);
1208 tree = &inode->extent_tree;
1211 * We want to be fast so if the lock is busy we don't want to
1212 * spend time waiting for it (some task is about to do IO for
1215 if (!write_trylock(&tree->lock))
1219 * Skip inode if it doesn't have loaded extent maps, so we avoid
1220 * getting a reference and doing an iput later. This includes
1221 * cases like files that were opened for things like stat(2), or
1222 * files with all extent maps previously released through the
1223 * release folio callback (btrfs_release_folio()) or released in
1224 * a previous run, or directories which never have extent maps.
1226 if (RB_EMPTY_ROOT(&tree->root)) {
1227 write_unlock(&tree->lock);
1231 if (igrab(&inode->vfs_inode))
1234 write_unlock(&tree->lock);
1236 from = btrfs_ino(inode) + 1;
1237 cond_resched_lock(&root->inodes.xa_lock);
1239 xa_unlock(&root->inodes);
1244 static long btrfs_scan_root(struct btrfs_root *root, struct btrfs_em_shrink_ctx *ctx)
1246 struct btrfs_fs_info *fs_info = root->fs_info;
1247 struct btrfs_inode *inode;
1248 long nr_dropped = 0;
1249 u64 min_ino = fs_info->em_shrinker_last_ino + 1;
1251 inode = find_first_inode_to_shrink(root, min_ino);
1253 nr_dropped += btrfs_scan_inode(inode, ctx);
1254 write_unlock(&inode->extent_tree.lock);
1256 min_ino = btrfs_ino(inode) + 1;
1257 fs_info->em_shrinker_last_ino = btrfs_ino(inode);
1258 iput(&inode->vfs_inode);
1260 if (ctx->scanned >= ctx->nr_to_scan || btrfs_fs_closing(fs_info))
1265 inode = find_first_inode_to_shrink(root, min_ino);
1270 * There are still inodes in this root or we happened to process
1271 * the last one and reached the scan limit. In either case set
1272 * the current root to this one, so we'll resume from the next
1273 * inode if there is one or we will find out this was the last
1274 * one and move to the next root.
1276 fs_info->em_shrinker_last_root = btrfs_root_id(root);
1279 * No more inodes in this root, set extent_map_shrinker_last_ino to 0 so
1280 * that when processing the next root we start from its first inode.
1282 fs_info->em_shrinker_last_ino = 0;
1283 fs_info->em_shrinker_last_root = btrfs_root_id(root) + 1;
1289 static void btrfs_extent_map_shrinker_worker(struct work_struct *work)
1291 struct btrfs_fs_info *fs_info;
1292 struct btrfs_em_shrink_ctx ctx;
1295 bool cycled = false;
1296 long nr_dropped = 0;
1298 fs_info = container_of(work, struct btrfs_fs_info, em_shrinker_work);
1301 ctx.nr_to_scan = atomic64_read(&fs_info->em_shrinker_nr_to_scan);
1303 start_root_id = fs_info->em_shrinker_last_root;
1304 next_root_id = fs_info->em_shrinker_last_root;
1306 if (trace_btrfs_extent_map_shrinker_scan_enter_enabled()) {
1307 s64 nr = percpu_counter_sum_positive(&fs_info->evictable_extent_maps);
1309 trace_btrfs_extent_map_shrinker_scan_enter(fs_info, nr);
1312 while (ctx.scanned < ctx.nr_to_scan && !btrfs_fs_closing(fs_info)) {
1313 struct btrfs_root *root;
1314 unsigned long count;
1318 spin_lock(&fs_info->fs_roots_radix_lock);
1319 count = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
1321 (unsigned long)next_root_id, 1);
1323 spin_unlock(&fs_info->fs_roots_radix_lock);
1324 if (start_root_id > 0 && !cycled) {
1326 fs_info->em_shrinker_last_root = 0;
1327 fs_info->em_shrinker_last_ino = 0;
1333 next_root_id = btrfs_root_id(root) + 1;
1334 root = btrfs_grab_root(root);
1335 spin_unlock(&fs_info->fs_roots_radix_lock);
1340 if (is_fstree(btrfs_root_id(root)))
1341 nr_dropped += btrfs_scan_root(root, &ctx);
1343 btrfs_put_root(root);
1346 if (trace_btrfs_extent_map_shrinker_scan_exit_enabled()) {
1347 s64 nr = percpu_counter_sum_positive(&fs_info->evictable_extent_maps);
1349 trace_btrfs_extent_map_shrinker_scan_exit(fs_info, nr_dropped, nr);
1352 atomic64_set(&fs_info->em_shrinker_nr_to_scan, 0);
1355 void btrfs_free_extent_maps(struct btrfs_fs_info *fs_info, long nr_to_scan)
1358 * Do nothing if the shrinker is already running. In case of high memory
1359 * pressure we can have a lot of tasks calling us and all passing the
1360 * same nr_to_scan value, but in reality we may need only to free
1361 * nr_to_scan extent maps (or less). In case we need to free more than
1362 * that, we will be called again by the fs shrinker, so no worries about
1363 * not doing enough work to reclaim memory from extent maps.
1364 * We can also be repeatedly called with the same nr_to_scan value
1365 * simply because the shrinker runs asynchronously and multiple calls
1366 * to this function are made before the shrinker does enough progress.
1368 * That's why we set the atomic counter to nr_to_scan only if its
1369 * current value is zero, instead of incrementing the counter by
1372 if (atomic64_cmpxchg(&fs_info->em_shrinker_nr_to_scan, 0, nr_to_scan) != 0)
1375 queue_work(system_unbound_wq, &fs_info->em_shrinker_work);
1378 void btrfs_init_extent_map_shrinker_work(struct btrfs_fs_info *fs_info)
1380 atomic64_set(&fs_info->em_shrinker_nr_to_scan, 0);
1381 INIT_WORK(&fs_info->em_shrinker_work, btrfs_extent_map_shrinker_worker);