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" 7a52d9a80SChris Mason #include "extent_map.h" 8ebb8765bSAnand Jain #include "compression.h" 9a52d9a80SChris Mason 1086479a04SChris Mason 11a52d9a80SChris Mason static struct kmem_cache *extent_map_cache; 12ca664626SChris Mason 132f4cbe64SWyatt Banks int __init extent_map_init(void) 14a52d9a80SChris Mason { 15837e1972SDavid Sterba extent_map_cache = kmem_cache_create("btrfs_extent_map", 166d36dcd4SChris Mason sizeof(struct extent_map), 0, 17fba4b697SNikolay Borisov SLAB_MEM_SPREAD, NULL); 182f4cbe64SWyatt Banks if (!extent_map_cache) 192f4cbe64SWyatt Banks return -ENOMEM; 202f4cbe64SWyatt Banks return 0; 21a52d9a80SChris Mason } 22a52d9a80SChris Mason 23e67c718bSDavid Sterba void __cold extent_map_exit(void) 24a52d9a80SChris Mason { 25a52d9a80SChris Mason kmem_cache_destroy(extent_map_cache); 26a52d9a80SChris Mason } 27a52d9a80SChris Mason 289d2423c5SChristoph Hellwig /** 299d2423c5SChristoph Hellwig * extent_map_tree_init - initialize extent map tree 309d2423c5SChristoph Hellwig * @tree: tree to initialize 319d2423c5SChristoph Hellwig * 329d2423c5SChristoph Hellwig * Initialize the extent tree @tree. Should be called for each new inode 339d2423c5SChristoph Hellwig * or other user of the extent_map interface. 349d2423c5SChristoph Hellwig */ 35a8067e02SDavid Sterba void extent_map_tree_init(struct extent_map_tree *tree) 36a52d9a80SChris Mason { 3707e1ce09SLiu Bo tree->map = RB_ROOT_CACHED; 385dc562c5SJosef Bacik INIT_LIST_HEAD(&tree->modified_extents); 39890871beSChris Mason rwlock_init(&tree->lock); 40a52d9a80SChris Mason } 41a52d9a80SChris Mason 429d2423c5SChristoph Hellwig /** 439d2423c5SChristoph Hellwig * alloc_extent_map - allocate new extent map structure 449d2423c5SChristoph Hellwig * 459d2423c5SChristoph Hellwig * Allocate a new extent_map structure. The new structure is 469d2423c5SChristoph Hellwig * returned with a reference count of one and needs to be 479d2423c5SChristoph Hellwig * freed using free_extent_map() 489d2423c5SChristoph Hellwig */ 49172ddd60SDavid Sterba struct extent_map *alloc_extent_map(void) 50a52d9a80SChris Mason { 51a52d9a80SChris Mason struct extent_map *em; 5270c8a91cSJosef Bacik em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS); 53c26a9203STsutomu Itoh if (!em) 54c26a9203STsutomu Itoh return NULL; 55cbc0e928SFilipe Manana RB_CLEAR_NODE(&em->rb_node); 56d1310b2eSChris Mason em->flags = 0; 57261507a0SLi Zefan em->compress_type = BTRFS_COMPRESS_NONE; 585dc562c5SJosef Bacik em->generation = 0; 59490b54d6SElena Reshetova refcount_set(&em->refs, 1); 605dc562c5SJosef Bacik INIT_LIST_HEAD(&em->list); 61a52d9a80SChris Mason return em; 62a52d9a80SChris Mason } 63a52d9a80SChris Mason 649d2423c5SChristoph Hellwig /** 659d2423c5SChristoph Hellwig * free_extent_map - drop reference count of an extent_map 6601327610SNicholas D Steeves * @em: extent map being released 679d2423c5SChristoph Hellwig * 689d2423c5SChristoph Hellwig * Drops the reference out on @em by one and free the structure 699d2423c5SChristoph Hellwig * if the reference count hits zero. 709d2423c5SChristoph Hellwig */ 71a52d9a80SChris Mason void free_extent_map(struct extent_map *em) 72a52d9a80SChris Mason { 732bf5a725SChris Mason if (!em) 742bf5a725SChris Mason return; 75490b54d6SElena Reshetova WARN_ON(refcount_read(&em->refs) == 0); 76490b54d6SElena Reshetova if (refcount_dec_and_test(&em->refs)) { 77cbc0e928SFilipe Manana WARN_ON(extent_map_in_tree(em)); 785dc562c5SJosef Bacik WARN_ON(!list_empty(&em->list)); 79298a8f9cSWang Shilong if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags)) 8095617d69SJeff Mahoney kfree(em->map_lookup); 81a52d9a80SChris Mason kmem_cache_free(extent_map_cache, em); 82a52d9a80SChris Mason } 83a52d9a80SChris Mason } 84a52d9a80SChris Mason 8532193c14SFilipe David Borba Manana /* simple helper to do math around the end of an extent, handling wrap */ 8632193c14SFilipe David Borba Manana static u64 range_end(u64 start, u64 len) 8732193c14SFilipe David Borba Manana { 8832193c14SFilipe David Borba Manana if (start + len < start) 8932193c14SFilipe David Borba Manana return (u64)-1; 9032193c14SFilipe David Borba Manana return start + len; 9132193c14SFilipe David Borba Manana } 9232193c14SFilipe David Borba Manana 9307e1ce09SLiu Bo static int tree_insert(struct rb_root_cached *root, struct extent_map *em) 94a52d9a80SChris Mason { 9507e1ce09SLiu Bo struct rb_node **p = &root->rb_root.rb_node; 96a52d9a80SChris Mason struct rb_node *parent = NULL; 9732193c14SFilipe David Borba Manana struct extent_map *entry = NULL; 9832193c14SFilipe David Borba Manana struct rb_node *orig_parent = NULL; 9932193c14SFilipe David Borba Manana u64 end = range_end(em->start, em->len); 10007e1ce09SLiu Bo bool leftmost = true; 101a52d9a80SChris Mason 102a52d9a80SChris Mason while (*p) { 103a52d9a80SChris Mason parent = *p; 104d1310b2eSChris Mason entry = rb_entry(parent, struct extent_map, rb_node); 105d1310b2eSChris Mason 10607e1ce09SLiu Bo if (em->start < entry->start) { 107a52d9a80SChris Mason p = &(*p)->rb_left; 10807e1ce09SLiu Bo } else if (em->start >= extent_map_end(entry)) { 109a52d9a80SChris Mason p = &(*p)->rb_right; 11007e1ce09SLiu Bo leftmost = false; 11107e1ce09SLiu Bo } else { 11232193c14SFilipe David Borba Manana return -EEXIST; 113a52d9a80SChris Mason } 11407e1ce09SLiu Bo } 115a52d9a80SChris Mason 11632193c14SFilipe David Borba Manana orig_parent = parent; 11732193c14SFilipe David Borba Manana while (parent && em->start >= extent_map_end(entry)) { 11832193c14SFilipe David Borba Manana parent = rb_next(parent); 11932193c14SFilipe David Borba Manana entry = rb_entry(parent, struct extent_map, rb_node); 12032193c14SFilipe David Borba Manana } 12132193c14SFilipe David Borba Manana if (parent) 12232193c14SFilipe David Borba Manana if (end > entry->start && em->start < extent_map_end(entry)) 12332193c14SFilipe David Borba Manana return -EEXIST; 12432193c14SFilipe David Borba Manana 12532193c14SFilipe David Borba Manana parent = orig_parent; 12632193c14SFilipe David Borba Manana entry = rb_entry(parent, struct extent_map, rb_node); 12732193c14SFilipe David Borba Manana while (parent && em->start < entry->start) { 12832193c14SFilipe David Borba Manana parent = rb_prev(parent); 12932193c14SFilipe David Borba Manana entry = rb_entry(parent, struct extent_map, rb_node); 13032193c14SFilipe David Borba Manana } 13132193c14SFilipe David Borba Manana if (parent) 13232193c14SFilipe David Borba Manana if (end > entry->start && em->start < extent_map_end(entry)) 13332193c14SFilipe David Borba Manana return -EEXIST; 13432193c14SFilipe David Borba Manana 13532193c14SFilipe David Borba Manana rb_link_node(&em->rb_node, orig_parent, p); 13607e1ce09SLiu Bo rb_insert_color_cached(&em->rb_node, root, leftmost); 13732193c14SFilipe David Borba Manana return 0; 138a52d9a80SChris Mason } 139a52d9a80SChris Mason 140d352ac68SChris Mason /* 141d352ac68SChris Mason * search through the tree for an extent_map with a given offset. If 142d352ac68SChris Mason * it can't be found, try to find some neighboring extents 143d352ac68SChris Mason */ 144a52d9a80SChris Mason static struct rb_node *__tree_search(struct rb_root *root, u64 offset, 1455f56406aSChris Mason struct rb_node **prev_ret, 1465f56406aSChris Mason struct rb_node **next_ret) 147a52d9a80SChris Mason { 148a52d9a80SChris Mason struct rb_node *n = root->rb_node; 149a52d9a80SChris Mason struct rb_node *prev = NULL; 1505f56406aSChris Mason struct rb_node *orig_prev = NULL; 151d1310b2eSChris Mason struct extent_map *entry; 152d1310b2eSChris Mason struct extent_map *prev_entry = NULL; 153a52d9a80SChris Mason 154a52d9a80SChris Mason while (n) { 155d1310b2eSChris Mason entry = rb_entry(n, struct extent_map, rb_node); 156a52d9a80SChris Mason prev = n; 157a52d9a80SChris Mason prev_entry = entry; 158a52d9a80SChris Mason 159a52d9a80SChris Mason if (offset < entry->start) 160a52d9a80SChris Mason n = n->rb_left; 161d1310b2eSChris Mason else if (offset >= extent_map_end(entry)) 162a52d9a80SChris Mason n = n->rb_right; 163a52d9a80SChris Mason else 164a52d9a80SChris Mason return n; 165a52d9a80SChris Mason } 1665f56406aSChris Mason 1675f56406aSChris Mason if (prev_ret) { 1685f56406aSChris Mason orig_prev = prev; 169d1310b2eSChris Mason while (prev && offset >= extent_map_end(prev_entry)) { 170a52d9a80SChris Mason prev = rb_next(prev); 171d1310b2eSChris Mason prev_entry = rb_entry(prev, struct extent_map, rb_node); 172a52d9a80SChris Mason } 173a52d9a80SChris Mason *prev_ret = prev; 1745f56406aSChris Mason prev = orig_prev; 1755f56406aSChris Mason } 1765f56406aSChris Mason 1775f56406aSChris Mason if (next_ret) { 178d1310b2eSChris Mason prev_entry = rb_entry(prev, struct extent_map, rb_node); 1795f56406aSChris Mason while (prev && offset < prev_entry->start) { 1805f56406aSChris Mason prev = rb_prev(prev); 181d1310b2eSChris Mason prev_entry = rb_entry(prev, struct extent_map, rb_node); 1825f56406aSChris Mason } 1835f56406aSChris Mason *next_ret = prev; 1845f56406aSChris Mason } 185a52d9a80SChris Mason return NULL; 186a52d9a80SChris Mason } 187a52d9a80SChris Mason 188d352ac68SChris Mason /* check to see if two extent_map structs are adjacent and safe to merge */ 189d1310b2eSChris Mason static int mergable_maps(struct extent_map *prev, struct extent_map *next) 190a52d9a80SChris Mason { 1917f3c74fbSChris Mason if (test_bit(EXTENT_FLAG_PINNED, &prev->flags)) 1927f3c74fbSChris Mason return 0; 1937f3c74fbSChris Mason 194c8b97818SChris Mason /* 195c8b97818SChris Mason * don't merge compressed extents, we need to know their 196c8b97818SChris Mason * actual size 197c8b97818SChris Mason */ 198c8b97818SChris Mason if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags)) 199c8b97818SChris Mason return 0; 200c8b97818SChris Mason 201201a9038SJosef Bacik if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) || 202201a9038SJosef Bacik test_bit(EXTENT_FLAG_LOGGING, &next->flags)) 203201a9038SJosef Bacik return 0; 204201a9038SJosef Bacik 20509a2a8f9SJosef Bacik /* 20609a2a8f9SJosef Bacik * We don't want to merge stuff that hasn't been written to the log yet 20709a2a8f9SJosef Bacik * since it may not reflect exactly what is on disk, and that would be 20809a2a8f9SJosef Bacik * bad. 20909a2a8f9SJosef Bacik */ 21009a2a8f9SJosef Bacik if (!list_empty(&prev->list) || !list_empty(&next->list)) 21109a2a8f9SJosef Bacik return 0; 21209a2a8f9SJosef Bacik 213*951e05a9SNikolay Borisov ASSERT(next->block_start != EXTENT_MAP_DELALLOC && 214*951e05a9SNikolay Borisov prev->block_start != EXTENT_MAP_DELALLOC); 215*951e05a9SNikolay Borisov 216d1310b2eSChris Mason if (extent_map_end(prev) == next->start && 217d1310b2eSChris Mason prev->flags == next->flags && 218d1310b2eSChris Mason prev->bdev == next->bdev && 219d1310b2eSChris Mason ((next->block_start == EXTENT_MAP_HOLE && 220d1310b2eSChris Mason prev->block_start == EXTENT_MAP_HOLE) || 221d1310b2eSChris Mason (next->block_start == EXTENT_MAP_INLINE && 222d1310b2eSChris Mason prev->block_start == EXTENT_MAP_INLINE) || 223d1310b2eSChris Mason (next->block_start < EXTENT_MAP_LAST_BYTE - 1 && 224d1310b2eSChris Mason next->block_start == extent_map_block_end(prev)))) { 225d1310b2eSChris Mason return 1; 226d1310b2eSChris Mason } 227a52d9a80SChris Mason return 0; 228a52d9a80SChris Mason } 229a52d9a80SChris Mason 2304d2c8f62SLi Zefan static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em) 231a1ed835eSChris Mason { 232a1ed835eSChris Mason struct extent_map *merge = NULL; 233a1ed835eSChris Mason struct rb_node *rb; 234a1ed835eSChris Mason 235a1ed835eSChris Mason if (em->start != 0) { 236a1ed835eSChris Mason rb = rb_prev(&em->rb_node); 237a1ed835eSChris Mason if (rb) 238a1ed835eSChris Mason merge = rb_entry(rb, struct extent_map, rb_node); 239a1ed835eSChris Mason if (rb && mergable_maps(merge, em)) { 240a1ed835eSChris Mason em->start = merge->start; 24170c8a91cSJosef Bacik em->orig_start = merge->orig_start; 242a1ed835eSChris Mason em->len += merge->len; 243a1ed835eSChris Mason em->block_len += merge->block_len; 244a1ed835eSChris Mason em->block_start = merge->block_start; 24570c8a91cSJosef Bacik em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start; 24670c8a91cSJosef Bacik em->mod_start = merge->mod_start; 24770c8a91cSJosef Bacik em->generation = max(em->generation, merge->generation); 2485dc562c5SJosef Bacik 24907e1ce09SLiu Bo rb_erase_cached(&merge->rb_node, &tree->map); 250cbc0e928SFilipe Manana RB_CLEAR_NODE(&merge->rb_node); 251a1ed835eSChris Mason free_extent_map(merge); 252a1ed835eSChris Mason } 253a1ed835eSChris Mason } 254a1ed835eSChris Mason 255a1ed835eSChris Mason rb = rb_next(&em->rb_node); 256a1ed835eSChris Mason if (rb) 257a1ed835eSChris Mason merge = rb_entry(rb, struct extent_map, rb_node); 258a1ed835eSChris Mason if (rb && mergable_maps(em, merge)) { 259a1ed835eSChris Mason em->len += merge->len; 260d527afe1SFilipe David Borba Manana em->block_len += merge->block_len; 26107e1ce09SLiu Bo rb_erase_cached(&merge->rb_node, &tree->map); 262cbc0e928SFilipe Manana RB_CLEAR_NODE(&merge->rb_node); 26370c8a91cSJosef Bacik em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start; 26470c8a91cSJosef Bacik em->generation = max(em->generation, merge->generation); 265a1ed835eSChris Mason free_extent_map(merge); 266a1ed835eSChris Mason } 2674d2c8f62SLi Zefan } 2684d2c8f62SLi Zefan 2695dc562c5SJosef Bacik /** 27052b1de91SLiu Bo * unpin_extent_cache - unpin an extent from the cache 2715dc562c5SJosef Bacik * @tree: tree to unpin the extent in 2725dc562c5SJosef Bacik * @start: logical offset in the file 2735dc562c5SJosef Bacik * @len: length of the extent 2745dc562c5SJosef Bacik * @gen: generation that this extent has been modified in 2755dc562c5SJosef Bacik * 2765dc562c5SJosef Bacik * Called after an extent has been written to disk properly. Set the generation 2775dc562c5SJosef Bacik * to the generation that actually added the file item to the inode so we know 2785dc562c5SJosef Bacik * we need to sync this extent when we call fsync(). 2795dc562c5SJosef Bacik */ 2805dc562c5SJosef Bacik int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len, 2815dc562c5SJosef Bacik u64 gen) 2824d2c8f62SLi Zefan { 2834d2c8f62SLi Zefan int ret = 0; 2844d2c8f62SLi Zefan struct extent_map *em; 2854e2f84e6SLiu Bo bool prealloc = false; 2864d2c8f62SLi Zefan 2874d2c8f62SLi Zefan write_lock(&tree->lock); 2884d2c8f62SLi Zefan em = lookup_extent_mapping(tree, start, len); 2894d2c8f62SLi Zefan 2904d2c8f62SLi Zefan WARN_ON(!em || em->start != start); 2914d2c8f62SLi Zefan 2924d2c8f62SLi Zefan if (!em) 2934d2c8f62SLi Zefan goto out; 2944d2c8f62SLi Zefan 2955dc562c5SJosef Bacik em->generation = gen; 2964d2c8f62SLi Zefan clear_bit(EXTENT_FLAG_PINNED, &em->flags); 2974e2f84e6SLiu Bo em->mod_start = em->start; 2984e2f84e6SLiu Bo em->mod_len = em->len; 2994e2f84e6SLiu Bo 300b11e234dSJosef Bacik if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) { 3014e2f84e6SLiu Bo prealloc = true; 302b11e234dSJosef Bacik clear_bit(EXTENT_FLAG_FILLING, &em->flags); 3034e2f84e6SLiu Bo } 3044d2c8f62SLi Zefan 3054d2c8f62SLi Zefan try_merge_map(tree, em); 3064e2f84e6SLiu Bo 3074e2f84e6SLiu Bo if (prealloc) { 3084e2f84e6SLiu Bo em->mod_start = em->start; 3094e2f84e6SLiu Bo em->mod_len = em->len; 3104e2f84e6SLiu Bo } 3114e2f84e6SLiu Bo 312a1ed835eSChris Mason free_extent_map(em); 313a1ed835eSChris Mason out: 314a1ed835eSChris Mason write_unlock(&tree->lock); 315a1ed835eSChris Mason return ret; 316a1ed835eSChris Mason 317a1ed835eSChris Mason } 318a1ed835eSChris Mason 319201a9038SJosef Bacik void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em) 320201a9038SJosef Bacik { 321201a9038SJosef Bacik clear_bit(EXTENT_FLAG_LOGGING, &em->flags); 322cbc0e928SFilipe Manana if (extent_map_in_tree(em)) 323201a9038SJosef Bacik try_merge_map(tree, em); 324201a9038SJosef Bacik } 325201a9038SJosef Bacik 326176840b3SFilipe Manana static inline void setup_extent_mapping(struct extent_map_tree *tree, 327176840b3SFilipe Manana struct extent_map *em, 328176840b3SFilipe Manana int modified) 329176840b3SFilipe Manana { 330490b54d6SElena Reshetova refcount_inc(&em->refs); 331176840b3SFilipe Manana em->mod_start = em->start; 332176840b3SFilipe Manana em->mod_len = em->len; 333176840b3SFilipe Manana 334176840b3SFilipe Manana if (modified) 335176840b3SFilipe Manana list_move(&em->list, &tree->modified_extents); 336176840b3SFilipe Manana else 337176840b3SFilipe Manana try_merge_map(tree, em); 338176840b3SFilipe Manana } 339176840b3SFilipe Manana 3409d2423c5SChristoph Hellwig /** 3419d2423c5SChristoph Hellwig * add_extent_mapping - add new extent map to the extent tree 3429d2423c5SChristoph Hellwig * @tree: tree to insert new map in 3439d2423c5SChristoph Hellwig * @em: map to insert 3449d2423c5SChristoph Hellwig * 3459d2423c5SChristoph Hellwig * Insert @em into @tree or perform a simple forward/backward merge with 3469d2423c5SChristoph Hellwig * existing mappings. The extent_map struct passed in will be inserted 3479d2423c5SChristoph Hellwig * into the tree directly, with an additional reference taken, or a 34825985edcSLucas De Marchi * reference dropped if the merge attempt was successful. 349a52d9a80SChris Mason */ 350a52d9a80SChris Mason int add_extent_mapping(struct extent_map_tree *tree, 35109a2a8f9SJosef Bacik struct extent_map *em, int modified) 352a52d9a80SChris Mason { 353a52d9a80SChris Mason int ret = 0; 354a52d9a80SChris Mason 35532193c14SFilipe David Borba Manana ret = tree_insert(&tree->map, em); 35632193c14SFilipe David Borba Manana if (ret) 3577c2fe32aSChris Mason goto out; 35832193c14SFilipe David Borba Manana 359176840b3SFilipe Manana setup_extent_mapping(tree, em, modified); 360a52d9a80SChris Mason out: 361a52d9a80SChris Mason return ret; 362a52d9a80SChris Mason } 363a52d9a80SChris Mason 36448a3b636SEric Sandeen static struct extent_map * 36548a3b636SEric Sandeen __lookup_extent_mapping(struct extent_map_tree *tree, 366ed64f066SLi Zefan u64 start, u64 len, int strict) 367ed64f066SLi Zefan { 368ed64f066SLi Zefan struct extent_map *em; 369ed64f066SLi Zefan struct rb_node *rb_node; 370ed64f066SLi Zefan struct rb_node *prev = NULL; 371ed64f066SLi Zefan struct rb_node *next = NULL; 372ed64f066SLi Zefan u64 end = range_end(start, len); 373ed64f066SLi Zefan 37407e1ce09SLiu Bo rb_node = __tree_search(&tree->map.rb_root, start, &prev, &next); 375ed64f066SLi Zefan if (!rb_node) { 376ed64f066SLi Zefan if (prev) 377ed64f066SLi Zefan rb_node = prev; 378ed64f066SLi Zefan else if (next) 379ed64f066SLi Zefan rb_node = next; 380ed64f066SLi Zefan else 381ed64f066SLi Zefan return NULL; 382ed64f066SLi Zefan } 383ed64f066SLi Zefan 384ed64f066SLi Zefan em = rb_entry(rb_node, struct extent_map, rb_node); 385ed64f066SLi Zefan 386ed64f066SLi Zefan if (strict && !(end > em->start && start < extent_map_end(em))) 387ed64f066SLi Zefan return NULL; 388ed64f066SLi Zefan 389490b54d6SElena Reshetova refcount_inc(&em->refs); 390ed64f066SLi Zefan return em; 391ed64f066SLi Zefan } 392ed64f066SLi Zefan 3939d2423c5SChristoph Hellwig /** 3949d2423c5SChristoph Hellwig * lookup_extent_mapping - lookup extent_map 3959d2423c5SChristoph Hellwig * @tree: tree to lookup in 3969d2423c5SChristoph Hellwig * @start: byte offset to start the search 3979d2423c5SChristoph Hellwig * @len: length of the lookup range 3989d2423c5SChristoph Hellwig * 3999d2423c5SChristoph Hellwig * Find and return the first extent_map struct in @tree that intersects the 4009d2423c5SChristoph Hellwig * [start, len] range. There may be additional objects in the tree that 4019d2423c5SChristoph Hellwig * intersect, so check the object returned carefully to make sure that no 4029d2423c5SChristoph Hellwig * additional lookups are needed. 403a52d9a80SChris Mason */ 404a52d9a80SChris Mason struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree, 405d1310b2eSChris Mason u64 start, u64 len) 406a52d9a80SChris Mason { 407ed64f066SLi Zefan return __lookup_extent_mapping(tree, start, len, 1); 408a52d9a80SChris Mason } 409a52d9a80SChris Mason 4109d2423c5SChristoph Hellwig /** 411b917b7c3SChris Mason * search_extent_mapping - find a nearby extent map 412b917b7c3SChris Mason * @tree: tree to lookup in 413b917b7c3SChris Mason * @start: byte offset to start the search 414b917b7c3SChris Mason * @len: length of the lookup range 415b917b7c3SChris Mason * 416b917b7c3SChris Mason * Find and return the first extent_map struct in @tree that intersects the 417b917b7c3SChris Mason * [start, len] range. 418b917b7c3SChris Mason * 419b917b7c3SChris Mason * If one can't be found, any nearby extent may be returned 420b917b7c3SChris Mason */ 421b917b7c3SChris Mason struct extent_map *search_extent_mapping(struct extent_map_tree *tree, 422b917b7c3SChris Mason u64 start, u64 len) 423b917b7c3SChris Mason { 424ed64f066SLi Zefan return __lookup_extent_mapping(tree, start, len, 0); 425b917b7c3SChris Mason } 426b917b7c3SChris Mason 427b917b7c3SChris Mason /** 4289d2423c5SChristoph Hellwig * remove_extent_mapping - removes an extent_map from the extent tree 4299d2423c5SChristoph Hellwig * @tree: extent tree to remove from 430bb7ab3b9SAdam Buchbinder * @em: extent map being removed 4319d2423c5SChristoph Hellwig * 4329d2423c5SChristoph Hellwig * Removes @em from @tree. No reference counts are dropped, and no checks 4339d2423c5SChristoph Hellwig * are done to see if the range is in use 434a52d9a80SChris Mason */ 435c1766dd7Szhong jiang void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em) 436a52d9a80SChris Mason { 4377f3c74fbSChris Mason WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags)); 43807e1ce09SLiu Bo rb_erase_cached(&em->rb_node, &tree->map); 439ff44c6e3SJosef Bacik if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags)) 4405dc562c5SJosef Bacik list_del_init(&em->list); 441cbc0e928SFilipe Manana RB_CLEAR_NODE(&em->rb_node); 442a52d9a80SChris Mason } 443176840b3SFilipe Manana 444176840b3SFilipe Manana void replace_extent_mapping(struct extent_map_tree *tree, 445176840b3SFilipe Manana struct extent_map *cur, 446176840b3SFilipe Manana struct extent_map *new, 447176840b3SFilipe Manana int modified) 448176840b3SFilipe Manana { 449176840b3SFilipe Manana WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags)); 450176840b3SFilipe Manana ASSERT(extent_map_in_tree(cur)); 451176840b3SFilipe Manana if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags)) 452176840b3SFilipe Manana list_del_init(&cur->list); 45307e1ce09SLiu Bo rb_replace_node_cached(&cur->rb_node, &new->rb_node, &tree->map); 454176840b3SFilipe Manana RB_CLEAR_NODE(&cur->rb_node); 455176840b3SFilipe Manana 456176840b3SFilipe Manana setup_extent_mapping(tree, new, modified); 457176840b3SFilipe Manana } 458c04e61b5SLiu Bo 459c04e61b5SLiu Bo static struct extent_map *next_extent_map(struct extent_map *em) 460c04e61b5SLiu Bo { 461c04e61b5SLiu Bo struct rb_node *next; 462c04e61b5SLiu Bo 463c04e61b5SLiu Bo next = rb_next(&em->rb_node); 464c04e61b5SLiu Bo if (!next) 465c04e61b5SLiu Bo return NULL; 466c04e61b5SLiu Bo return container_of(next, struct extent_map, rb_node); 467c04e61b5SLiu Bo } 468c04e61b5SLiu Bo 469c04e61b5SLiu Bo static struct extent_map *prev_extent_map(struct extent_map *em) 470c04e61b5SLiu Bo { 471c04e61b5SLiu Bo struct rb_node *prev; 472c04e61b5SLiu Bo 473c04e61b5SLiu Bo prev = rb_prev(&em->rb_node); 474c04e61b5SLiu Bo if (!prev) 475c04e61b5SLiu Bo return NULL; 476c04e61b5SLiu Bo return container_of(prev, struct extent_map, rb_node); 477c04e61b5SLiu Bo } 478c04e61b5SLiu Bo 47952042d8eSAndrea Gelmini /* 48052042d8eSAndrea Gelmini * Helper for btrfs_get_extent. Given an existing extent in the tree, 481c04e61b5SLiu Bo * the existing extent is the nearest extent to map_start, 482c04e61b5SLiu Bo * and an extent that you want to insert, deal with overlap and insert 483c04e61b5SLiu Bo * the best fitted new extent into the tree. 484c04e61b5SLiu Bo */ 4855f4791f4SLiu Bo static noinline int merge_extent_mapping(struct extent_map_tree *em_tree, 486c04e61b5SLiu Bo struct extent_map *existing, 487c04e61b5SLiu Bo struct extent_map *em, 488c04e61b5SLiu Bo u64 map_start) 489c04e61b5SLiu Bo { 490c04e61b5SLiu Bo struct extent_map *prev; 491c04e61b5SLiu Bo struct extent_map *next; 492c04e61b5SLiu Bo u64 start; 493c04e61b5SLiu Bo u64 end; 494c04e61b5SLiu Bo u64 start_diff; 495c04e61b5SLiu Bo 496c04e61b5SLiu Bo BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); 497c04e61b5SLiu Bo 498c04e61b5SLiu Bo if (existing->start > map_start) { 499c04e61b5SLiu Bo next = existing; 500c04e61b5SLiu Bo prev = prev_extent_map(next); 501c04e61b5SLiu Bo } else { 502c04e61b5SLiu Bo prev = existing; 503c04e61b5SLiu Bo next = next_extent_map(prev); 504c04e61b5SLiu Bo } 505c04e61b5SLiu Bo 506c04e61b5SLiu Bo start = prev ? extent_map_end(prev) : em->start; 507c04e61b5SLiu Bo start = max_t(u64, start, em->start); 508c04e61b5SLiu Bo end = next ? next->start : extent_map_end(em); 509c04e61b5SLiu Bo end = min_t(u64, end, extent_map_end(em)); 510c04e61b5SLiu Bo start_diff = start - em->start; 511c04e61b5SLiu Bo em->start = start; 512c04e61b5SLiu Bo em->len = end - start; 513c04e61b5SLiu Bo if (em->block_start < EXTENT_MAP_LAST_BYTE && 514c04e61b5SLiu Bo !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { 515c04e61b5SLiu Bo em->block_start += start_diff; 516c04e61b5SLiu Bo em->block_len = em->len; 517c04e61b5SLiu Bo } 518c04e61b5SLiu Bo return add_extent_mapping(em_tree, em, 0); 519c04e61b5SLiu Bo } 520c04e61b5SLiu Bo 521c04e61b5SLiu Bo /** 522c04e61b5SLiu Bo * btrfs_add_extent_mapping - add extent mapping into em_tree 523f46b24c9SDavid Sterba * @fs_info - used for tracepoint 524c04e61b5SLiu Bo * @em_tree - the extent tree into which we want to insert the extent mapping 525c04e61b5SLiu Bo * @em_in - extent we are inserting 526c04e61b5SLiu Bo * @start - start of the logical range btrfs_get_extent() is requesting 527c04e61b5SLiu Bo * @len - length of the logical range btrfs_get_extent() is requesting 528c04e61b5SLiu Bo * 529c04e61b5SLiu Bo * Note that @em_in's range may be different from [start, start+len), 530c04e61b5SLiu Bo * but they must be overlapped. 531c04e61b5SLiu Bo * 532c04e61b5SLiu Bo * Insert @em_in into @em_tree. In case there is an overlapping range, handle 533c04e61b5SLiu Bo * the -EEXIST by either: 534c04e61b5SLiu Bo * a) Returning the existing extent in @em_in if @start is within the 535c04e61b5SLiu Bo * existing em. 536c04e61b5SLiu Bo * b) Merge the existing extent with @em_in passed in. 537c04e61b5SLiu Bo * 538c04e61b5SLiu Bo * Return 0 on success, otherwise -EEXIST. 539c04e61b5SLiu Bo * 540c04e61b5SLiu Bo */ 541f46b24c9SDavid Sterba int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info, 542f46b24c9SDavid Sterba struct extent_map_tree *em_tree, 543c04e61b5SLiu Bo struct extent_map **em_in, u64 start, u64 len) 544c04e61b5SLiu Bo { 545c04e61b5SLiu Bo int ret; 546c04e61b5SLiu Bo struct extent_map *em = *em_in; 547c04e61b5SLiu Bo 548c04e61b5SLiu Bo ret = add_extent_mapping(em_tree, em, 0); 549c04e61b5SLiu Bo /* it is possible that someone inserted the extent into the tree 550c04e61b5SLiu Bo * while we had the lock dropped. It is also possible that 551c04e61b5SLiu Bo * an overlapping map exists in the tree 552c04e61b5SLiu Bo */ 553c04e61b5SLiu Bo if (ret == -EEXIST) { 554c04e61b5SLiu Bo struct extent_map *existing; 555c04e61b5SLiu Bo 556c04e61b5SLiu Bo ret = 0; 557c04e61b5SLiu Bo 558c04e61b5SLiu Bo existing = search_extent_mapping(em_tree, start, len); 559393da918SLiu Bo 560f46b24c9SDavid Sterba trace_btrfs_handle_em_exist(fs_info, existing, em, start, len); 561393da918SLiu Bo 562c04e61b5SLiu Bo /* 563c04e61b5SLiu Bo * existing will always be non-NULL, since there must be 564c04e61b5SLiu Bo * extent causing the -EEXIST. 565c04e61b5SLiu Bo */ 566c04e61b5SLiu Bo if (start >= existing->start && 567c04e61b5SLiu Bo start < extent_map_end(existing)) { 568c04e61b5SLiu Bo free_extent_map(em); 569c04e61b5SLiu Bo *em_in = existing; 570c04e61b5SLiu Bo ret = 0; 571c04e61b5SLiu Bo } else { 5729a7e10e7SLiu Bo u64 orig_start = em->start; 5739a7e10e7SLiu Bo u64 orig_len = em->len; 5749a7e10e7SLiu Bo 575c04e61b5SLiu Bo /* 576c04e61b5SLiu Bo * The existing extent map is the one nearest to 577c04e61b5SLiu Bo * the [start, start + len) range which overlaps 578c04e61b5SLiu Bo */ 579c04e61b5SLiu Bo ret = merge_extent_mapping(em_tree, existing, 580c04e61b5SLiu Bo em, start); 581c04e61b5SLiu Bo if (ret) { 582c04e61b5SLiu Bo free_extent_map(em); 583c04e61b5SLiu Bo *em_in = NULL; 5849a7e10e7SLiu Bo WARN_ONCE(ret, 5859a7e10e7SLiu Bo "unexpected error %d: merge existing(start %llu len %llu) with em(start %llu len %llu)\n", 5869a7e10e7SLiu Bo ret, existing->start, existing->len, 5879a7e10e7SLiu Bo orig_start, orig_len); 588c04e61b5SLiu Bo } 5899a7e10e7SLiu Bo free_extent_map(existing); 590c04e61b5SLiu Bo } 591c04e61b5SLiu Bo } 592c04e61b5SLiu Bo 593c04e61b5SLiu Bo ASSERT(ret == 0 || ret == -EEXIST); 594c04e61b5SLiu Bo return ret; 595c04e61b5SLiu Bo } 596