^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include "ctree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "volumes.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "extent_map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "compression.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) static struct kmem_cache *extent_map_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) int __init extent_map_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) extent_map_cache = kmem_cache_create("btrfs_extent_map",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) sizeof(struct extent_map), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) SLAB_MEM_SPREAD, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) if (!extent_map_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) void __cold extent_map_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) kmem_cache_destroy(extent_map_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * extent_map_tree_init - initialize extent map tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * @tree: tree to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * Initialize the extent tree @tree. Should be called for each new inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * or other user of the extent_map interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) void extent_map_tree_init(struct extent_map_tree *tree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) tree->map = RB_ROOT_CACHED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) INIT_LIST_HEAD(&tree->modified_extents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) rwlock_init(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * alloc_extent_map - allocate new extent map structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * Allocate a new extent_map structure. The new structure is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * returned with a reference count of one and needs to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * freed using free_extent_map()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct extent_map *alloc_extent_map(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (!em)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) RB_CLEAR_NODE(&em->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) em->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) em->compress_type = BTRFS_COMPRESS_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) em->generation = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) refcount_set(&em->refs, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) INIT_LIST_HEAD(&em->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * free_extent_map - drop reference count of an extent_map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * @em: extent map being released
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * Drops the reference out on @em by one and free the structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * if the reference count hits zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) void free_extent_map(struct extent_map *em)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (!em)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) WARN_ON(refcount_read(&em->refs) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (refcount_dec_and_test(&em->refs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) WARN_ON(extent_map_in_tree(em));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) WARN_ON(!list_empty(&em->list));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) kfree(em->map_lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) kmem_cache_free(extent_map_cache, em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /* simple helper to do math around the end of an extent, handling wrap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static u64 range_end(u64 start, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (start + len < start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return (u64)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return start + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static int tree_insert(struct rb_root_cached *root, struct extent_map *em)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct rb_node **p = &root->rb_root.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct rb_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct extent_map *entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct rb_node *orig_parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) u64 end = range_end(em->start, em->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) bool leftmost = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) entry = rb_entry(parent, struct extent_map, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (em->start < entry->start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) p = &(*p)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) } else if (em->start >= extent_map_end(entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) p = &(*p)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) leftmost = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) orig_parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) while (parent && em->start >= extent_map_end(entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) parent = rb_next(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) entry = rb_entry(parent, struct extent_map, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (end > entry->start && em->start < extent_map_end(entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) parent = orig_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) entry = rb_entry(parent, struct extent_map, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) while (parent && em->start < entry->start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) parent = rb_prev(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) entry = rb_entry(parent, struct extent_map, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (end > entry->start && em->start < extent_map_end(entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) rb_link_node(&em->rb_node, orig_parent, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) rb_insert_color_cached(&em->rb_node, root, leftmost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * search through the tree for an extent_map with a given offset. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * it can't be found, try to find some neighboring extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct rb_node **prev_ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct rb_node **next_ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct rb_node *n = root->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct rb_node *prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct rb_node *orig_prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct extent_map *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct extent_map *prev_entry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) while (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) entry = rb_entry(n, struct extent_map, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) prev = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) prev_entry = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (offset < entry->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) n = n->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) else if (offset >= extent_map_end(entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) n = n->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (prev_ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) orig_prev = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) while (prev && offset >= extent_map_end(prev_entry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) prev = rb_next(prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) prev_entry = rb_entry(prev, struct extent_map, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) *prev_ret = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) prev = orig_prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (next_ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) prev_entry = rb_entry(prev, struct extent_map, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) while (prev && offset < prev_entry->start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) prev = rb_prev(prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) prev_entry = rb_entry(prev, struct extent_map, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) *next_ret = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) /* check to see if two extent_map structs are adjacent and safe to merge */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static int mergable_maps(struct extent_map *prev, struct extent_map *next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * don't merge compressed extents, we need to know their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * actual size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) test_bit(EXTENT_FLAG_LOGGING, &next->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * We don't want to merge stuff that hasn't been written to the log yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * since it may not reflect exactly what is on disk, and that would be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * bad.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (!list_empty(&prev->list) || !list_empty(&next->list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) ASSERT(next->block_start != EXTENT_MAP_DELALLOC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) prev->block_start != EXTENT_MAP_DELALLOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (prev->map_lookup || next->map_lookup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) ASSERT(test_bit(EXTENT_FLAG_FS_MAPPING, &prev->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) test_bit(EXTENT_FLAG_FS_MAPPING, &next->flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (extent_map_end(prev) == next->start &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) prev->flags == next->flags &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) prev->map_lookup == next->map_lookup &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) ((next->block_start == EXTENT_MAP_HOLE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) prev->block_start == EXTENT_MAP_HOLE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) (next->block_start == EXTENT_MAP_INLINE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) prev->block_start == EXTENT_MAP_INLINE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) next->block_start == extent_map_block_end(prev)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) struct extent_map *merge = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) struct rb_node *rb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * We can't modify an extent map that is in the tree and that is being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * used by another task, as it can cause that other task to see it in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * inconsistent state during the merging. We always have 1 reference for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * the tree and 1 for this task (which is unpinning the extent map or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * clearing the logging flag), so anything > 2 means it's being used by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * other tasks too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (refcount_read(&em->refs) > 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (em->start != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) rb = rb_prev(&em->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (rb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) merge = rb_entry(rb, struct extent_map, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (rb && mergable_maps(merge, em)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) em->start = merge->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) em->orig_start = merge->orig_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) em->len += merge->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) em->block_len += merge->block_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) em->block_start = merge->block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) em->mod_start = merge->mod_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) em->generation = max(em->generation, merge->generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) rb_erase_cached(&merge->rb_node, &tree->map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) RB_CLEAR_NODE(&merge->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) free_extent_map(merge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) rb = rb_next(&em->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (rb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) merge = rb_entry(rb, struct extent_map, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (rb && mergable_maps(em, merge)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) em->len += merge->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) em->block_len += merge->block_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) rb_erase_cached(&merge->rb_node, &tree->map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) RB_CLEAR_NODE(&merge->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) em->generation = max(em->generation, merge->generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) free_extent_map(merge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * unpin_extent_cache - unpin an extent from the cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * @tree: tree to unpin the extent in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * @start: logical offset in the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * @len: length of the extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * @gen: generation that this extent has been modified in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * Called after an extent has been written to disk properly. Set the generation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * to the generation that actually added the file item to the inode so we know
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * we need to sync this extent when we call fsync().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) u64 gen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) bool prealloc = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) write_lock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) em = lookup_extent_mapping(tree, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) WARN_ON(!em || em->start != start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (!em)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) em->generation = gen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) clear_bit(EXTENT_FLAG_PINNED, &em->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) em->mod_start = em->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) em->mod_len = em->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) prealloc = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) clear_bit(EXTENT_FLAG_FILLING, &em->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) try_merge_map(tree, em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (prealloc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) em->mod_start = em->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) em->mod_len = em->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) write_unlock(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (extent_map_in_tree(em))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) try_merge_map(tree, em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) static inline void setup_extent_mapping(struct extent_map_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) struct extent_map *em,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) int modified)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) refcount_inc(&em->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) em->mod_start = em->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) em->mod_len = em->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (modified)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) list_move(&em->list, &tree->modified_extents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) try_merge_map(tree, em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static void extent_map_device_set_bits(struct extent_map *em, unsigned bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) struct map_lookup *map = em->map_lookup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) u64 stripe_size = em->orig_block_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) for (i = 0; i < map->num_stripes; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) struct btrfs_bio_stripe *stripe = &map->stripes[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) struct btrfs_device *device = stripe->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) set_extent_bits_nowait(&device->alloc_state, stripe->physical,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) stripe->physical + stripe_size - 1, bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) static void extent_map_device_clear_bits(struct extent_map *em, unsigned bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) struct map_lookup *map = em->map_lookup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) u64 stripe_size = em->orig_block_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) for (i = 0; i < map->num_stripes; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) struct btrfs_bio_stripe *stripe = &map->stripes[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) struct btrfs_device *device = stripe->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) __clear_extent_bit(&device->alloc_state, stripe->physical,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) stripe->physical + stripe_size - 1, bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 0, 0, NULL, GFP_NOWAIT, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * add_extent_mapping - add new extent map to the extent tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * @tree: tree to insert new map in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * @em: map to insert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * Insert @em into @tree or perform a simple forward/backward merge with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * existing mappings. The extent_map struct passed in will be inserted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) * into the tree directly, with an additional reference taken, or a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * reference dropped if the merge attempt was successful.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) int add_extent_mapping(struct extent_map_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) struct extent_map *em, int modified)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) lockdep_assert_held_write(&tree->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) ret = tree_insert(&tree->map, em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) setup_extent_mapping(tree, em, modified);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) extent_map_device_set_bits(em, CHUNK_ALLOCATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) extent_map_device_clear_bits(em, CHUNK_TRIMMED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) static struct extent_map *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) __lookup_extent_mapping(struct extent_map_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) u64 start, u64 len, int strict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) struct extent_map *em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) struct rb_node *rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) struct rb_node *prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) struct rb_node *next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) u64 end = range_end(start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) rb_node = __tree_search(&tree->map.rb_root, start, &prev, &next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) if (!rb_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) if (prev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) rb_node = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) else if (next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) rb_node = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) em = rb_entry(rb_node, struct extent_map, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) if (strict && !(end > em->start && start < extent_map_end(em)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) refcount_inc(&em->refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) return em;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) * lookup_extent_mapping - lookup extent_map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) * @tree: tree to lookup in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) * @start: byte offset to start the search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * @len: length of the lookup range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * Find and return the first extent_map struct in @tree that intersects the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) * [start, len] range. There may be additional objects in the tree that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * intersect, so check the object returned carefully to make sure that no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * additional lookups are needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) u64 start, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) return __lookup_extent_mapping(tree, start, len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * search_extent_mapping - find a nearby extent map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * @tree: tree to lookup in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) * @start: byte offset to start the search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) * @len: length of the lookup range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * Find and return the first extent_map struct in @tree that intersects the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) * [start, len] range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * If one can't be found, any nearby extent may be returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) u64 start, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) return __lookup_extent_mapping(tree, start, len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * remove_extent_mapping - removes an extent_map from the extent tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * @tree: extent tree to remove from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * @em: extent map being removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) * Removes @em from @tree. No reference counts are dropped, and no checks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) * are done to see if the range is in use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) rb_erase_cached(&em->rb_node, &tree->map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) list_del_init(&em->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) extent_map_device_clear_bits(em, CHUNK_ALLOCATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) RB_CLEAR_NODE(&em->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) void replace_extent_mapping(struct extent_map_tree *tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) struct extent_map *cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) struct extent_map *new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) int modified)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) ASSERT(extent_map_in_tree(cur));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) list_del_init(&cur->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) rb_replace_node_cached(&cur->rb_node, &new->rb_node, &tree->map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) RB_CLEAR_NODE(&cur->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) setup_extent_mapping(tree, new, modified);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) static struct extent_map *next_extent_map(struct extent_map *em)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) struct rb_node *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) next = rb_next(&em->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) if (!next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) return container_of(next, struct extent_map, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) static struct extent_map *prev_extent_map(struct extent_map *em)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) struct rb_node *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) prev = rb_prev(&em->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) if (!prev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) return container_of(prev, struct extent_map, rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) * Helper for btrfs_get_extent. Given an existing extent in the tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) * the existing extent is the nearest extent to map_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) * and an extent that you want to insert, deal with overlap and insert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) * the best fitted new extent into the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) static noinline int merge_extent_mapping(struct extent_map_tree *em_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) struct extent_map *existing,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) struct extent_map *em,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) u64 map_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) struct extent_map *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) struct extent_map *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) u64 start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) u64 end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) u64 start_diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) if (existing->start > map_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) next = existing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) prev = prev_extent_map(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) prev = existing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) next = next_extent_map(prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) start = prev ? extent_map_end(prev) : em->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) start = max_t(u64, start, em->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) end = next ? next->start : extent_map_end(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) end = min_t(u64, end, extent_map_end(em));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) start_diff = start - em->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) em->start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) em->len = end - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) if (em->block_start < EXTENT_MAP_LAST_BYTE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) em->block_start += start_diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) em->block_len = em->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) return add_extent_mapping(em_tree, em, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) * btrfs_add_extent_mapping - add extent mapping into em_tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) * @fs_info - used for tracepoint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) * @em_tree - the extent tree into which we want to insert the extent mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) * @em_in - extent we are inserting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) * @start - start of the logical range btrfs_get_extent() is requesting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) * @len - length of the logical range btrfs_get_extent() is requesting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) * Note that @em_in's range may be different from [start, start+len),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) * but they must be overlapped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) * Insert @em_in into @em_tree. In case there is an overlapping range, handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) * the -EEXIST by either:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) * a) Returning the existing extent in @em_in if @start is within the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) * existing em.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) * b) Merge the existing extent with @em_in passed in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) * Return 0 on success, otherwise -EEXIST.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) struct extent_map_tree *em_tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) struct extent_map **em_in, u64 start, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) struct extent_map *em = *em_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) ret = add_extent_mapping(em_tree, em, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) /* it is possible that someone inserted the extent into the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) * while we had the lock dropped. It is also possible that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) * an overlapping map exists in the tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) if (ret == -EEXIST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) struct extent_map *existing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) existing = search_extent_mapping(em_tree, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) trace_btrfs_handle_em_exist(fs_info, existing, em, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) * existing will always be non-NULL, since there must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) * extent causing the -EEXIST.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) if (start >= existing->start &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) start < extent_map_end(existing)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) *em_in = existing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) u64 orig_start = em->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) u64 orig_len = em->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) * The existing extent map is the one nearest to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) * the [start, start + len) range which overlaps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) ret = merge_extent_mapping(em_tree, existing,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) em, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) free_extent_map(em);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) *em_in = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) WARN_ONCE(ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) "unexpected error %d: merge existing(start %llu len %llu) with em(start %llu len %llu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) ret, existing->start, existing->len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) orig_start, orig_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) free_extent_map(existing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) ASSERT(ret == 0 || ret == -EEXIST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) }