1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2c1d7c514SDavid Sterba 3d1310b2eSChris Mason #include <linux/err.h> 4d1310b2eSChris Mason #include <linux/slab.h> 5a52d9a80SChris Mason #include <linux/spinlock.h> 6261507a0SLi Zefan #include "ctree.h" 71c11b63eSJeff Mahoney #include "volumes.h" 8a52d9a80SChris Mason #include "extent_map.h" 9ebb8765bSAnand Jain #include "compression.h" 10a52d9a80SChris Mason 1186479a04SChris Mason 12a52d9a80SChris Mason static struct kmem_cache *extent_map_cache; 13ca664626SChris Mason 142f4cbe64SWyatt Banks int __init extent_map_init(void) 15a52d9a80SChris Mason { 16837e1972SDavid Sterba extent_map_cache = kmem_cache_create("btrfs_extent_map", 176d36dcd4SChris Mason sizeof(struct extent_map), 0, 18fba4b697SNikolay Borisov SLAB_MEM_SPREAD, NULL); 192f4cbe64SWyatt Banks if (!extent_map_cache) 202f4cbe64SWyatt Banks return -ENOMEM; 212f4cbe64SWyatt Banks return 0; 22a52d9a80SChris Mason } 23a52d9a80SChris Mason 24e67c718bSDavid Sterba void __cold extent_map_exit(void) 25a52d9a80SChris Mason { 26a52d9a80SChris Mason kmem_cache_destroy(extent_map_cache); 27a52d9a80SChris Mason } 28a52d9a80SChris Mason 299d2423c5SChristoph Hellwig /** 309d2423c5SChristoph Hellwig * extent_map_tree_init - initialize extent map tree 319d2423c5SChristoph Hellwig * @tree: tree to initialize 329d2423c5SChristoph Hellwig * 339d2423c5SChristoph Hellwig * Initialize the extent tree @tree. Should be called for each new inode 349d2423c5SChristoph Hellwig * or other user of the extent_map interface. 359d2423c5SChristoph Hellwig */ 36a8067e02SDavid Sterba void extent_map_tree_init(struct extent_map_tree *tree) 37a52d9a80SChris Mason { 3807e1ce09SLiu Bo tree->map = RB_ROOT_CACHED; 395dc562c5SJosef Bacik INIT_LIST_HEAD(&tree->modified_extents); 40890871beSChris Mason rwlock_init(&tree->lock); 41a52d9a80SChris Mason } 42a52d9a80SChris Mason 439d2423c5SChristoph Hellwig /** 449d2423c5SChristoph Hellwig * alloc_extent_map - allocate new extent map structure 459d2423c5SChristoph Hellwig * 469d2423c5SChristoph Hellwig * Allocate a new extent_map structure. The new structure is 479d2423c5SChristoph Hellwig * returned with a reference count of one and needs to be 489d2423c5SChristoph Hellwig * freed using free_extent_map() 499d2423c5SChristoph Hellwig */ 50172ddd60SDavid Sterba struct extent_map *alloc_extent_map(void) 51a52d9a80SChris Mason { 52a52d9a80SChris Mason struct extent_map *em; 5370c8a91cSJosef Bacik em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS); 54c26a9203STsutomu Itoh if (!em) 55c26a9203STsutomu Itoh return NULL; 56cbc0e928SFilipe Manana RB_CLEAR_NODE(&em->rb_node); 57d1310b2eSChris Mason em->flags = 0; 58261507a0SLi Zefan em->compress_type = BTRFS_COMPRESS_NONE; 595dc562c5SJosef Bacik em->generation = 0; 60490b54d6SElena Reshetova refcount_set(&em->refs, 1); 615dc562c5SJosef Bacik INIT_LIST_HEAD(&em->list); 62a52d9a80SChris Mason return em; 63a52d9a80SChris Mason } 64a52d9a80SChris Mason 659d2423c5SChristoph Hellwig /** 669d2423c5SChristoph Hellwig * free_extent_map - drop reference count of an extent_map 6701327610SNicholas D Steeves * @em: extent map being released 689d2423c5SChristoph Hellwig * 699d2423c5SChristoph Hellwig * Drops the reference out on @em by one and free the structure 709d2423c5SChristoph Hellwig * if the reference count hits zero. 719d2423c5SChristoph Hellwig */ 72a52d9a80SChris Mason void free_extent_map(struct extent_map *em) 73a52d9a80SChris Mason { 742bf5a725SChris Mason if (!em) 752bf5a725SChris Mason return; 76490b54d6SElena Reshetova WARN_ON(refcount_read(&em->refs) == 0); 77490b54d6SElena Reshetova if (refcount_dec_and_test(&em->refs)) { 78cbc0e928SFilipe Manana WARN_ON(extent_map_in_tree(em)); 795dc562c5SJosef Bacik WARN_ON(!list_empty(&em->list)); 80298a8f9cSWang Shilong if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags)) 8195617d69SJeff Mahoney kfree(em->map_lookup); 82a52d9a80SChris Mason kmem_cache_free(extent_map_cache, em); 83a52d9a80SChris Mason } 84a52d9a80SChris Mason } 85a52d9a80SChris Mason 8632193c14SFilipe David Borba Manana /* simple helper to do math around the end of an extent, handling wrap */ 8732193c14SFilipe David Borba Manana static u64 range_end(u64 start, u64 len) 8832193c14SFilipe David Borba Manana { 8932193c14SFilipe David Borba Manana if (start + len < start) 9032193c14SFilipe David Borba Manana return (u64)-1; 9132193c14SFilipe David Borba Manana return start + len; 9232193c14SFilipe David Borba Manana } 9332193c14SFilipe David Borba Manana 9407e1ce09SLiu Bo static int tree_insert(struct rb_root_cached *root, struct extent_map *em) 95a52d9a80SChris Mason { 9607e1ce09SLiu Bo struct rb_node **p = &root->rb_root.rb_node; 97a52d9a80SChris Mason struct rb_node *parent = NULL; 9832193c14SFilipe David Borba Manana struct extent_map *entry = NULL; 9932193c14SFilipe David Borba Manana struct rb_node *orig_parent = NULL; 10032193c14SFilipe David Borba Manana u64 end = range_end(em->start, em->len); 10107e1ce09SLiu Bo bool leftmost = true; 102a52d9a80SChris Mason 103a52d9a80SChris Mason while (*p) { 104a52d9a80SChris Mason parent = *p; 105d1310b2eSChris Mason entry = rb_entry(parent, struct extent_map, rb_node); 106d1310b2eSChris Mason 10707e1ce09SLiu Bo if (em->start < entry->start) { 108a52d9a80SChris Mason p = &(*p)->rb_left; 10907e1ce09SLiu Bo } else if (em->start >= extent_map_end(entry)) { 110a52d9a80SChris Mason p = &(*p)->rb_right; 11107e1ce09SLiu Bo leftmost = false; 11207e1ce09SLiu Bo } else { 11332193c14SFilipe David Borba Manana return -EEXIST; 114a52d9a80SChris Mason } 11507e1ce09SLiu Bo } 116a52d9a80SChris Mason 11732193c14SFilipe David Borba Manana orig_parent = parent; 11832193c14SFilipe David Borba Manana while (parent && em->start >= extent_map_end(entry)) { 11932193c14SFilipe David Borba Manana parent = rb_next(parent); 12032193c14SFilipe David Borba Manana entry = rb_entry(parent, struct extent_map, rb_node); 12132193c14SFilipe David Borba Manana } 12232193c14SFilipe David Borba Manana if (parent) 12332193c14SFilipe David Borba Manana if (end > entry->start && em->start < extent_map_end(entry)) 12432193c14SFilipe David Borba Manana return -EEXIST; 12532193c14SFilipe David Borba Manana 12632193c14SFilipe David Borba Manana parent = orig_parent; 12732193c14SFilipe David Borba Manana entry = rb_entry(parent, struct extent_map, rb_node); 12832193c14SFilipe David Borba Manana while (parent && em->start < entry->start) { 12932193c14SFilipe David Borba Manana parent = rb_prev(parent); 13032193c14SFilipe David Borba Manana entry = rb_entry(parent, struct extent_map, rb_node); 13132193c14SFilipe David Borba Manana } 13232193c14SFilipe David Borba Manana if (parent) 13332193c14SFilipe David Borba Manana if (end > entry->start && em->start < extent_map_end(entry)) 13432193c14SFilipe David Borba Manana return -EEXIST; 13532193c14SFilipe David Borba Manana 13632193c14SFilipe David Borba Manana rb_link_node(&em->rb_node, orig_parent, p); 13707e1ce09SLiu Bo rb_insert_color_cached(&em->rb_node, root, leftmost); 13832193c14SFilipe David Borba Manana return 0; 139a52d9a80SChris Mason } 140a52d9a80SChris Mason 141d352ac68SChris Mason /* 142d352ac68SChris Mason * search through the tree for an extent_map with a given offset. If 143d352ac68SChris Mason * it can't be found, try to find some neighboring extents 144d352ac68SChris Mason */ 145a52d9a80SChris Mason static struct rb_node *__tree_search(struct rb_root *root, u64 offset, 1465f56406aSChris Mason struct rb_node **prev_ret, 1475f56406aSChris Mason struct rb_node **next_ret) 148a52d9a80SChris Mason { 149a52d9a80SChris Mason struct rb_node *n = root->rb_node; 150a52d9a80SChris Mason struct rb_node *prev = NULL; 1515f56406aSChris Mason struct rb_node *orig_prev = NULL; 152d1310b2eSChris Mason struct extent_map *entry; 153d1310b2eSChris Mason struct extent_map *prev_entry = NULL; 154a52d9a80SChris Mason 155a52d9a80SChris Mason while (n) { 156d1310b2eSChris Mason entry = rb_entry(n, struct extent_map, rb_node); 157a52d9a80SChris Mason prev = n; 158a52d9a80SChris Mason prev_entry = entry; 159a52d9a80SChris Mason 160a52d9a80SChris Mason if (offset < entry->start) 161a52d9a80SChris Mason n = n->rb_left; 162d1310b2eSChris Mason else if (offset >= extent_map_end(entry)) 163a52d9a80SChris Mason n = n->rb_right; 164a52d9a80SChris Mason else 165a52d9a80SChris Mason return n; 166a52d9a80SChris Mason } 1675f56406aSChris Mason 1685f56406aSChris Mason if (prev_ret) { 1695f56406aSChris Mason orig_prev = prev; 170d1310b2eSChris Mason while (prev && offset >= extent_map_end(prev_entry)) { 171a52d9a80SChris Mason prev = rb_next(prev); 172d1310b2eSChris Mason prev_entry = rb_entry(prev, struct extent_map, rb_node); 173a52d9a80SChris Mason } 174a52d9a80SChris Mason *prev_ret = prev; 1755f56406aSChris Mason prev = orig_prev; 1765f56406aSChris Mason } 1775f56406aSChris Mason 1785f56406aSChris Mason if (next_ret) { 179d1310b2eSChris Mason prev_entry = rb_entry(prev, struct extent_map, rb_node); 1805f56406aSChris Mason while (prev && offset < prev_entry->start) { 1815f56406aSChris Mason prev = rb_prev(prev); 182d1310b2eSChris Mason prev_entry = rb_entry(prev, struct extent_map, rb_node); 1835f56406aSChris Mason } 1845f56406aSChris Mason *next_ret = prev; 1855f56406aSChris Mason } 186a52d9a80SChris Mason return NULL; 187a52d9a80SChris Mason } 188a52d9a80SChris Mason 189d352ac68SChris Mason /* check to see if two extent_map structs are adjacent and safe to merge */ 190d1310b2eSChris Mason static int mergable_maps(struct extent_map *prev, struct extent_map *next) 191a52d9a80SChris Mason { 1927f3c74fbSChris Mason if (test_bit(EXTENT_FLAG_PINNED, &prev->flags)) 1937f3c74fbSChris Mason return 0; 1947f3c74fbSChris Mason 195c8b97818SChris Mason /* 196c8b97818SChris Mason * don't merge compressed extents, we need to know their 197c8b97818SChris Mason * actual size 198c8b97818SChris Mason */ 199c8b97818SChris Mason if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags)) 200c8b97818SChris Mason return 0; 201c8b97818SChris Mason 202201a9038SJosef Bacik if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) || 203201a9038SJosef Bacik test_bit(EXTENT_FLAG_LOGGING, &next->flags)) 204201a9038SJosef Bacik return 0; 205201a9038SJosef Bacik 20609a2a8f9SJosef Bacik /* 20709a2a8f9SJosef Bacik * We don't want to merge stuff that hasn't been written to the log yet 20809a2a8f9SJosef Bacik * since it may not reflect exactly what is on disk, and that would be 20909a2a8f9SJosef Bacik * bad. 21009a2a8f9SJosef Bacik */ 21109a2a8f9SJosef Bacik if (!list_empty(&prev->list) || !list_empty(&next->list)) 21209a2a8f9SJosef Bacik return 0; 21309a2a8f9SJosef Bacik 214951e05a9SNikolay Borisov ASSERT(next->block_start != EXTENT_MAP_DELALLOC && 215951e05a9SNikolay Borisov prev->block_start != EXTENT_MAP_DELALLOC); 216951e05a9SNikolay Borisov 217*c3e14909SDavid Sterba if (prev->map_lookup || next->map_lookup) 218*c3e14909SDavid Sterba ASSERT(test_bit(EXTENT_FLAG_FS_MAPPING, &prev->flags) && 219*c3e14909SDavid Sterba test_bit(EXTENT_FLAG_FS_MAPPING, &next->flags)); 220*c3e14909SDavid Sterba 221*c3e14909SDavid Sterba if (prev->bdev || next->bdev) 222*c3e14909SDavid Sterba ASSERT(prev->bdev == next->bdev); 223*c3e14909SDavid Sterba 224d1310b2eSChris Mason if (extent_map_end(prev) == next->start && 225d1310b2eSChris Mason prev->flags == next->flags && 226*c3e14909SDavid Sterba prev->map_lookup == next->map_lookup && 227d1310b2eSChris Mason ((next->block_start == EXTENT_MAP_HOLE && 228d1310b2eSChris Mason prev->block_start == EXTENT_MAP_HOLE) || 229d1310b2eSChris Mason (next->block_start == EXTENT_MAP_INLINE && 230d1310b2eSChris Mason prev->block_start == EXTENT_MAP_INLINE) || 231d1310b2eSChris Mason (next->block_start < EXTENT_MAP_LAST_BYTE - 1 && 232d1310b2eSChris Mason next->block_start == extent_map_block_end(prev)))) { 233d1310b2eSChris Mason return 1; 234d1310b2eSChris Mason } 235a52d9a80SChris Mason return 0; 236a52d9a80SChris Mason } 237a52d9a80SChris Mason 2384d2c8f62SLi Zefan static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em) 239a1ed835eSChris Mason { 240a1ed835eSChris Mason struct extent_map *merge = NULL; 241a1ed835eSChris Mason struct rb_node *rb; 242a1ed835eSChris Mason 243a1ed835eSChris Mason if (em->start != 0) { 244a1ed835eSChris Mason rb = rb_prev(&em->rb_node); 245a1ed835eSChris Mason if (rb) 246a1ed835eSChris Mason merge = rb_entry(rb, struct extent_map, rb_node); 247a1ed835eSChris Mason if (rb && mergable_maps(merge, em)) { 248a1ed835eSChris Mason em->start = merge->start; 24970c8a91cSJosef Bacik em->orig_start = merge->orig_start; 250a1ed835eSChris Mason em->len += merge->len; 251a1ed835eSChris Mason em->block_len += merge->block_len; 252a1ed835eSChris Mason em->block_start = merge->block_start; 25370c8a91cSJosef Bacik em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start; 25470c8a91cSJosef Bacik em->mod_start = merge->mod_start; 25570c8a91cSJosef Bacik em->generation = max(em->generation, merge->generation); 2565dc562c5SJosef Bacik 25707e1ce09SLiu Bo rb_erase_cached(&merge->rb_node, &tree->map); 258cbc0e928SFilipe Manana RB_CLEAR_NODE(&merge->rb_node); 259a1ed835eSChris Mason free_extent_map(merge); 260a1ed835eSChris Mason } 261a1ed835eSChris Mason } 262a1ed835eSChris Mason 263a1ed835eSChris Mason rb = rb_next(&em->rb_node); 264a1ed835eSChris Mason if (rb) 265a1ed835eSChris Mason merge = rb_entry(rb, struct extent_map, rb_node); 266a1ed835eSChris Mason if (rb && mergable_maps(em, merge)) { 267a1ed835eSChris Mason em->len += merge->len; 268d527afe1SFilipe David Borba Manana em->block_len += merge->block_len; 26907e1ce09SLiu Bo rb_erase_cached(&merge->rb_node, &tree->map); 270cbc0e928SFilipe Manana RB_CLEAR_NODE(&merge->rb_node); 27170c8a91cSJosef Bacik em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start; 27270c8a91cSJosef Bacik em->generation = max(em->generation, merge->generation); 273a1ed835eSChris Mason free_extent_map(merge); 274a1ed835eSChris Mason } 2754d2c8f62SLi Zefan } 2764d2c8f62SLi Zefan 2775dc562c5SJosef Bacik /** 27852b1de91SLiu Bo * unpin_extent_cache - unpin an extent from the cache 2795dc562c5SJosef Bacik * @tree: tree to unpin the extent in 2805dc562c5SJosef Bacik * @start: logical offset in the file 2815dc562c5SJosef Bacik * @len: length of the extent 2825dc562c5SJosef Bacik * @gen: generation that this extent has been modified in 2835dc562c5SJosef Bacik * 2845dc562c5SJosef Bacik * Called after an extent has been written to disk properly. Set the generation 2855dc562c5SJosef Bacik * to the generation that actually added the file item to the inode so we know 2865dc562c5SJosef Bacik * we need to sync this extent when we call fsync(). 2875dc562c5SJosef Bacik */ 2885dc562c5SJosef Bacik int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len, 2895dc562c5SJosef Bacik u64 gen) 2904d2c8f62SLi Zefan { 2914d2c8f62SLi Zefan int ret = 0; 2924d2c8f62SLi Zefan struct extent_map *em; 2934e2f84e6SLiu Bo bool prealloc = false; 2944d2c8f62SLi Zefan 2954d2c8f62SLi Zefan write_lock(&tree->lock); 2964d2c8f62SLi Zefan em = lookup_extent_mapping(tree, start, len); 2974d2c8f62SLi Zefan 2984d2c8f62SLi Zefan WARN_ON(!em || em->start != start); 2994d2c8f62SLi Zefan 3004d2c8f62SLi Zefan if (!em) 3014d2c8f62SLi Zefan goto out; 3024d2c8f62SLi Zefan 3035dc562c5SJosef Bacik em->generation = gen; 3044d2c8f62SLi Zefan clear_bit(EXTENT_FLAG_PINNED, &em->flags); 3054e2f84e6SLiu Bo em->mod_start = em->start; 3064e2f84e6SLiu Bo em->mod_len = em->len; 3074e2f84e6SLiu Bo 308b11e234dSJosef Bacik if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) { 3094e2f84e6SLiu Bo prealloc = true; 310b11e234dSJosef Bacik clear_bit(EXTENT_FLAG_FILLING, &em->flags); 3114e2f84e6SLiu Bo } 3124d2c8f62SLi Zefan 3134d2c8f62SLi Zefan try_merge_map(tree, em); 3144e2f84e6SLiu Bo 3154e2f84e6SLiu Bo if (prealloc) { 3164e2f84e6SLiu Bo em->mod_start = em->start; 3174e2f84e6SLiu Bo em->mod_len = em->len; 3184e2f84e6SLiu Bo } 3194e2f84e6SLiu Bo 320a1ed835eSChris Mason free_extent_map(em); 321a1ed835eSChris Mason out: 322a1ed835eSChris Mason write_unlock(&tree->lock); 323a1ed835eSChris Mason return ret; 324a1ed835eSChris Mason 325a1ed835eSChris Mason } 326a1ed835eSChris Mason 327201a9038SJosef Bacik void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em) 328201a9038SJosef Bacik { 329201a9038SJosef Bacik clear_bit(EXTENT_FLAG_LOGGING, &em->flags); 330cbc0e928SFilipe Manana if (extent_map_in_tree(em)) 331201a9038SJosef Bacik try_merge_map(tree, em); 332201a9038SJosef Bacik } 333201a9038SJosef Bacik 334176840b3SFilipe Manana static inline void setup_extent_mapping(struct extent_map_tree *tree, 335176840b3SFilipe Manana struct extent_map *em, 336176840b3SFilipe Manana int modified) 337176840b3SFilipe Manana { 338490b54d6SElena Reshetova refcount_inc(&em->refs); 339176840b3SFilipe Manana em->mod_start = em->start; 340176840b3SFilipe Manana em->mod_len = em->len; 341176840b3SFilipe Manana 342176840b3SFilipe Manana if (modified) 343176840b3SFilipe Manana list_move(&em->list, &tree->modified_extents); 344176840b3SFilipe Manana else 345176840b3SFilipe Manana try_merge_map(tree, em); 346176840b3SFilipe Manana } 347176840b3SFilipe Manana 3481c11b63eSJeff Mahoney static void extent_map_device_set_bits(struct extent_map *em, unsigned bits) 3491c11b63eSJeff Mahoney { 3501c11b63eSJeff Mahoney struct map_lookup *map = em->map_lookup; 3511c11b63eSJeff Mahoney u64 stripe_size = em->orig_block_len; 3521c11b63eSJeff Mahoney int i; 3531c11b63eSJeff Mahoney 3541c11b63eSJeff Mahoney for (i = 0; i < map->num_stripes; i++) { 3551c11b63eSJeff Mahoney struct btrfs_bio_stripe *stripe = &map->stripes[i]; 3561c11b63eSJeff Mahoney struct btrfs_device *device = stripe->dev; 3571c11b63eSJeff Mahoney 3581c11b63eSJeff Mahoney set_extent_bits_nowait(&device->alloc_state, stripe->physical, 3591c11b63eSJeff Mahoney stripe->physical + stripe_size - 1, bits); 3601c11b63eSJeff Mahoney } 3611c11b63eSJeff Mahoney } 3621c11b63eSJeff Mahoney 3631c11b63eSJeff Mahoney static void extent_map_device_clear_bits(struct extent_map *em, unsigned bits) 3641c11b63eSJeff Mahoney { 3651c11b63eSJeff Mahoney struct map_lookup *map = em->map_lookup; 3661c11b63eSJeff Mahoney u64 stripe_size = em->orig_block_len; 3671c11b63eSJeff Mahoney int i; 3681c11b63eSJeff Mahoney 3691c11b63eSJeff Mahoney for (i = 0; i < map->num_stripes; i++) { 3701c11b63eSJeff Mahoney struct btrfs_bio_stripe *stripe = &map->stripes[i]; 3711c11b63eSJeff Mahoney struct btrfs_device *device = stripe->dev; 3721c11b63eSJeff Mahoney 3731c11b63eSJeff Mahoney __clear_extent_bit(&device->alloc_state, stripe->physical, 3741c11b63eSJeff Mahoney stripe->physical + stripe_size - 1, bits, 3751c11b63eSJeff Mahoney 0, 0, NULL, GFP_NOWAIT, NULL); 3761c11b63eSJeff Mahoney } 3771c11b63eSJeff Mahoney } 3781c11b63eSJeff Mahoney 3799d2423c5SChristoph Hellwig /** 3809d2423c5SChristoph Hellwig * add_extent_mapping - add new extent map to the extent tree 3819d2423c5SChristoph Hellwig * @tree: tree to insert new map in 3829d2423c5SChristoph Hellwig * @em: map to insert 3839d2423c5SChristoph Hellwig * 3849d2423c5SChristoph Hellwig * Insert @em into @tree or perform a simple forward/backward merge with 3859d2423c5SChristoph Hellwig * existing mappings. The extent_map struct passed in will be inserted 3869d2423c5SChristoph Hellwig * into the tree directly, with an additional reference taken, or a 38725985edcSLucas De Marchi * reference dropped if the merge attempt was successful. 388a52d9a80SChris Mason */ 389a52d9a80SChris Mason int add_extent_mapping(struct extent_map_tree *tree, 39009a2a8f9SJosef Bacik struct extent_map *em, int modified) 391a52d9a80SChris Mason { 392a52d9a80SChris Mason int ret = 0; 393a52d9a80SChris Mason 394d23ea3faSDavid Sterba lockdep_assert_held_write(&tree->lock); 395d23ea3faSDavid Sterba 39632193c14SFilipe David Borba Manana ret = tree_insert(&tree->map, em); 39732193c14SFilipe David Borba Manana if (ret) 3987c2fe32aSChris Mason goto out; 39932193c14SFilipe David Borba Manana 400176840b3SFilipe Manana setup_extent_mapping(tree, em, modified); 4018811133dSNikolay Borisov if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags)) { 4021c11b63eSJeff Mahoney extent_map_device_set_bits(em, CHUNK_ALLOCATED); 4038811133dSNikolay Borisov extent_map_device_clear_bits(em, CHUNK_TRIMMED); 4048811133dSNikolay Borisov } 405a52d9a80SChris Mason out: 406a52d9a80SChris Mason return ret; 407a52d9a80SChris Mason } 408a52d9a80SChris Mason 40948a3b636SEric Sandeen static struct extent_map * 41048a3b636SEric Sandeen __lookup_extent_mapping(struct extent_map_tree *tree, 411ed64f066SLi Zefan u64 start, u64 len, int strict) 412ed64f066SLi Zefan { 413ed64f066SLi Zefan struct extent_map *em; 414ed64f066SLi Zefan struct rb_node *rb_node; 415ed64f066SLi Zefan struct rb_node *prev = NULL; 416ed64f066SLi Zefan struct rb_node *next = NULL; 417ed64f066SLi Zefan u64 end = range_end(start, len); 418ed64f066SLi Zefan 41907e1ce09SLiu Bo rb_node = __tree_search(&tree->map.rb_root, start, &prev, &next); 420ed64f066SLi Zefan if (!rb_node) { 421ed64f066SLi Zefan if (prev) 422ed64f066SLi Zefan rb_node = prev; 423ed64f066SLi Zefan else if (next) 424ed64f066SLi Zefan rb_node = next; 425ed64f066SLi Zefan else 426ed64f066SLi Zefan return NULL; 427ed64f066SLi Zefan } 428ed64f066SLi Zefan 429ed64f066SLi Zefan em = rb_entry(rb_node, struct extent_map, rb_node); 430ed64f066SLi Zefan 431ed64f066SLi Zefan if (strict && !(end > em->start && start < extent_map_end(em))) 432ed64f066SLi Zefan return NULL; 433ed64f066SLi Zefan 434490b54d6SElena Reshetova refcount_inc(&em->refs); 435ed64f066SLi Zefan return em; 436ed64f066SLi Zefan } 437ed64f066SLi Zefan 4389d2423c5SChristoph Hellwig /** 4399d2423c5SChristoph Hellwig * lookup_extent_mapping - lookup extent_map 4409d2423c5SChristoph Hellwig * @tree: tree to lookup in 4419d2423c5SChristoph Hellwig * @start: byte offset to start the search 4429d2423c5SChristoph Hellwig * @len: length of the lookup range 4439d2423c5SChristoph Hellwig * 4449d2423c5SChristoph Hellwig * Find and return the first extent_map struct in @tree that intersects the 4459d2423c5SChristoph Hellwig * [start, len] range. There may be additional objects in the tree that 4469d2423c5SChristoph Hellwig * intersect, so check the object returned carefully to make sure that no 4479d2423c5SChristoph Hellwig * additional lookups are needed. 448a52d9a80SChris Mason */ 449a52d9a80SChris Mason struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree, 450d1310b2eSChris Mason u64 start, u64 len) 451a52d9a80SChris Mason { 452ed64f066SLi Zefan return __lookup_extent_mapping(tree, start, len, 1); 453a52d9a80SChris Mason } 454a52d9a80SChris Mason 4559d2423c5SChristoph Hellwig /** 456b917b7c3SChris Mason * search_extent_mapping - find a nearby extent map 457b917b7c3SChris Mason * @tree: tree to lookup in 458b917b7c3SChris Mason * @start: byte offset to start the search 459b917b7c3SChris Mason * @len: length of the lookup range 460b917b7c3SChris Mason * 461b917b7c3SChris Mason * Find and return the first extent_map struct in @tree that intersects the 462b917b7c3SChris Mason * [start, len] range. 463b917b7c3SChris Mason * 464b917b7c3SChris Mason * If one can't be found, any nearby extent may be returned 465b917b7c3SChris Mason */ 466b917b7c3SChris Mason struct extent_map *search_extent_mapping(struct extent_map_tree *tree, 467b917b7c3SChris Mason u64 start, u64 len) 468b917b7c3SChris Mason { 469ed64f066SLi Zefan return __lookup_extent_mapping(tree, start, len, 0); 470b917b7c3SChris Mason } 471b917b7c3SChris Mason 472b917b7c3SChris Mason /** 4739d2423c5SChristoph Hellwig * remove_extent_mapping - removes an extent_map from the extent tree 4749d2423c5SChristoph Hellwig * @tree: extent tree to remove from 475bb7ab3b9SAdam Buchbinder * @em: extent map being removed 4769d2423c5SChristoph Hellwig * 4779d2423c5SChristoph Hellwig * Removes @em from @tree. No reference counts are dropped, and no checks 4789d2423c5SChristoph Hellwig * are done to see if the range is in use 479a52d9a80SChris Mason */ 480c1766dd7Szhong jiang void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em) 481a52d9a80SChris Mason { 4827f3c74fbSChris Mason WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags)); 48307e1ce09SLiu Bo rb_erase_cached(&em->rb_node, &tree->map); 484ff44c6e3SJosef Bacik if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags)) 4855dc562c5SJosef Bacik list_del_init(&em->list); 4861c11b63eSJeff Mahoney if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags)) 4871c11b63eSJeff Mahoney extent_map_device_clear_bits(em, CHUNK_ALLOCATED); 488cbc0e928SFilipe Manana RB_CLEAR_NODE(&em->rb_node); 489a52d9a80SChris Mason } 490176840b3SFilipe Manana 491176840b3SFilipe Manana void replace_extent_mapping(struct extent_map_tree *tree, 492176840b3SFilipe Manana struct extent_map *cur, 493176840b3SFilipe Manana struct extent_map *new, 494176840b3SFilipe Manana int modified) 495176840b3SFilipe Manana { 496176840b3SFilipe Manana WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags)); 497176840b3SFilipe Manana ASSERT(extent_map_in_tree(cur)); 498176840b3SFilipe Manana if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags)) 499176840b3SFilipe Manana list_del_init(&cur->list); 50007e1ce09SLiu Bo rb_replace_node_cached(&cur->rb_node, &new->rb_node, &tree->map); 501176840b3SFilipe Manana RB_CLEAR_NODE(&cur->rb_node); 502176840b3SFilipe Manana 503176840b3SFilipe Manana setup_extent_mapping(tree, new, modified); 504176840b3SFilipe Manana } 505c04e61b5SLiu Bo 506c04e61b5SLiu Bo static struct extent_map *next_extent_map(struct extent_map *em) 507c04e61b5SLiu Bo { 508c04e61b5SLiu Bo struct rb_node *next; 509c04e61b5SLiu Bo 510c04e61b5SLiu Bo next = rb_next(&em->rb_node); 511c04e61b5SLiu Bo if (!next) 512c04e61b5SLiu Bo return NULL; 513c04e61b5SLiu Bo return container_of(next, struct extent_map, rb_node); 514c04e61b5SLiu Bo } 515c04e61b5SLiu Bo 516c04e61b5SLiu Bo static struct extent_map *prev_extent_map(struct extent_map *em) 517c04e61b5SLiu Bo { 518c04e61b5SLiu Bo struct rb_node *prev; 519c04e61b5SLiu Bo 520c04e61b5SLiu Bo prev = rb_prev(&em->rb_node); 521c04e61b5SLiu Bo if (!prev) 522c04e61b5SLiu Bo return NULL; 523c04e61b5SLiu Bo return container_of(prev, struct extent_map, rb_node); 524c04e61b5SLiu Bo } 525c04e61b5SLiu Bo 52652042d8eSAndrea Gelmini /* 52752042d8eSAndrea Gelmini * Helper for btrfs_get_extent. Given an existing extent in the tree, 528c04e61b5SLiu Bo * the existing extent is the nearest extent to map_start, 529c04e61b5SLiu Bo * and an extent that you want to insert, deal with overlap and insert 530c04e61b5SLiu Bo * the best fitted new extent into the tree. 531c04e61b5SLiu Bo */ 5325f4791f4SLiu Bo static noinline int merge_extent_mapping(struct extent_map_tree *em_tree, 533c04e61b5SLiu Bo struct extent_map *existing, 534c04e61b5SLiu Bo struct extent_map *em, 535c04e61b5SLiu Bo u64 map_start) 536c04e61b5SLiu Bo { 537c04e61b5SLiu Bo struct extent_map *prev; 538c04e61b5SLiu Bo struct extent_map *next; 539c04e61b5SLiu Bo u64 start; 540c04e61b5SLiu Bo u64 end; 541c04e61b5SLiu Bo u64 start_diff; 542c04e61b5SLiu Bo 543c04e61b5SLiu Bo BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); 544c04e61b5SLiu Bo 545c04e61b5SLiu Bo if (existing->start > map_start) { 546c04e61b5SLiu Bo next = existing; 547c04e61b5SLiu Bo prev = prev_extent_map(next); 548c04e61b5SLiu Bo } else { 549c04e61b5SLiu Bo prev = existing; 550c04e61b5SLiu Bo next = next_extent_map(prev); 551c04e61b5SLiu Bo } 552c04e61b5SLiu Bo 553c04e61b5SLiu Bo start = prev ? extent_map_end(prev) : em->start; 554c04e61b5SLiu Bo start = max_t(u64, start, em->start); 555c04e61b5SLiu Bo end = next ? next->start : extent_map_end(em); 556c04e61b5SLiu Bo end = min_t(u64, end, extent_map_end(em)); 557c04e61b5SLiu Bo start_diff = start - em->start; 558c04e61b5SLiu Bo em->start = start; 559c04e61b5SLiu Bo em->len = end - start; 560c04e61b5SLiu Bo if (em->block_start < EXTENT_MAP_LAST_BYTE && 561c04e61b5SLiu Bo !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { 562c04e61b5SLiu Bo em->block_start += start_diff; 563c04e61b5SLiu Bo em->block_len = em->len; 564c04e61b5SLiu Bo } 565c04e61b5SLiu Bo return add_extent_mapping(em_tree, em, 0); 566c04e61b5SLiu Bo } 567c04e61b5SLiu Bo 568c04e61b5SLiu Bo /** 569c04e61b5SLiu Bo * btrfs_add_extent_mapping - add extent mapping into em_tree 570f46b24c9SDavid Sterba * @fs_info - used for tracepoint 571c04e61b5SLiu Bo * @em_tree - the extent tree into which we want to insert the extent mapping 572c04e61b5SLiu Bo * @em_in - extent we are inserting 573c04e61b5SLiu Bo * @start - start of the logical range btrfs_get_extent() is requesting 574c04e61b5SLiu Bo * @len - length of the logical range btrfs_get_extent() is requesting 575c04e61b5SLiu Bo * 576c04e61b5SLiu Bo * Note that @em_in's range may be different from [start, start+len), 577c04e61b5SLiu Bo * but they must be overlapped. 578c04e61b5SLiu Bo * 579c04e61b5SLiu Bo * Insert @em_in into @em_tree. In case there is an overlapping range, handle 580c04e61b5SLiu Bo * the -EEXIST by either: 581c04e61b5SLiu Bo * a) Returning the existing extent in @em_in if @start is within the 582c04e61b5SLiu Bo * existing em. 583c04e61b5SLiu Bo * b) Merge the existing extent with @em_in passed in. 584c04e61b5SLiu Bo * 585c04e61b5SLiu Bo * Return 0 on success, otherwise -EEXIST. 586c04e61b5SLiu Bo * 587c04e61b5SLiu Bo */ 588f46b24c9SDavid Sterba int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info, 589f46b24c9SDavid Sterba struct extent_map_tree *em_tree, 590c04e61b5SLiu Bo struct extent_map **em_in, u64 start, u64 len) 591c04e61b5SLiu Bo { 592c04e61b5SLiu Bo int ret; 593c04e61b5SLiu Bo struct extent_map *em = *em_in; 594c04e61b5SLiu Bo 595c04e61b5SLiu Bo ret = add_extent_mapping(em_tree, em, 0); 596c04e61b5SLiu Bo /* it is possible that someone inserted the extent into the tree 597c04e61b5SLiu Bo * while we had the lock dropped. It is also possible that 598c04e61b5SLiu Bo * an overlapping map exists in the tree 599c04e61b5SLiu Bo */ 600c04e61b5SLiu Bo if (ret == -EEXIST) { 601c04e61b5SLiu Bo struct extent_map *existing; 602c04e61b5SLiu Bo 603c04e61b5SLiu Bo ret = 0; 604c04e61b5SLiu Bo 605c04e61b5SLiu Bo existing = search_extent_mapping(em_tree, start, len); 606393da918SLiu Bo 607f46b24c9SDavid Sterba trace_btrfs_handle_em_exist(fs_info, existing, em, start, len); 608393da918SLiu Bo 609c04e61b5SLiu Bo /* 610c04e61b5SLiu Bo * existing will always be non-NULL, since there must be 611c04e61b5SLiu Bo * extent causing the -EEXIST. 612c04e61b5SLiu Bo */ 613c04e61b5SLiu Bo if (start >= existing->start && 614c04e61b5SLiu Bo start < extent_map_end(existing)) { 615c04e61b5SLiu Bo free_extent_map(em); 616c04e61b5SLiu Bo *em_in = existing; 617c04e61b5SLiu Bo ret = 0; 618c04e61b5SLiu Bo } else { 6199a7e10e7SLiu Bo u64 orig_start = em->start; 6209a7e10e7SLiu Bo u64 orig_len = em->len; 6219a7e10e7SLiu Bo 622c04e61b5SLiu Bo /* 623c04e61b5SLiu Bo * The existing extent map is the one nearest to 624c04e61b5SLiu Bo * the [start, start + len) range which overlaps 625c04e61b5SLiu Bo */ 626c04e61b5SLiu Bo ret = merge_extent_mapping(em_tree, existing, 627c04e61b5SLiu Bo em, start); 628c04e61b5SLiu Bo if (ret) { 629c04e61b5SLiu Bo free_extent_map(em); 630c04e61b5SLiu Bo *em_in = NULL; 6319a7e10e7SLiu Bo WARN_ONCE(ret, 6329a7e10e7SLiu Bo "unexpected error %d: merge existing(start %llu len %llu) with em(start %llu len %llu)\n", 6339a7e10e7SLiu Bo ret, existing->start, existing->len, 6349a7e10e7SLiu Bo orig_start, orig_len); 635c04e61b5SLiu Bo } 6369a7e10e7SLiu Bo free_extent_map(existing); 637c04e61b5SLiu Bo } 638c04e61b5SLiu Bo } 639c04e61b5SLiu Bo 640c04e61b5SLiu Bo ASSERT(ret == 0 || ret == -EEXIST); 641c04e61b5SLiu Bo return ret; 642c04e61b5SLiu Bo } 643