1d1310b2eSChris Mason #include <linux/err.h> 2a52d9a80SChris Mason #include <linux/gfp.h> 3d1310b2eSChris Mason #include <linux/slab.h> 4a52d9a80SChris Mason #include <linux/module.h> 5a52d9a80SChris Mason #include <linux/spinlock.h> 60a2118dfSJens Axboe #include <linux/version.h> 7d1310b2eSChris Mason #include <linux/hardirq.h> 8a52d9a80SChris Mason #include "extent_map.h" 9a52d9a80SChris Mason 1086479a04SChris Mason /* temporary define until extent_map moves out of btrfs */ 1186479a04SChris Mason struct kmem_cache *btrfs_cache_create(const char *name, size_t size, 1286479a04SChris Mason unsigned long extra_flags, 1386479a04SChris Mason void (*ctor)(void *, struct kmem_cache *, 1486479a04SChris Mason unsigned long)); 1586479a04SChris Mason 16a52d9a80SChris Mason static struct kmem_cache *extent_map_cache; 17ca664626SChris Mason 182f4cbe64SWyatt Banks int __init extent_map_init(void) 19a52d9a80SChris Mason { 2086479a04SChris Mason extent_map_cache = btrfs_cache_create("extent_map", 216d36dcd4SChris Mason sizeof(struct extent_map), 0, 22a52d9a80SChris Mason NULL); 232f4cbe64SWyatt Banks if (!extent_map_cache) 242f4cbe64SWyatt Banks return -ENOMEM; 252f4cbe64SWyatt Banks return 0; 26a52d9a80SChris Mason } 27a52d9a80SChris Mason 2817636e03SChristian Hesse void extent_map_exit(void) 29a52d9a80SChris Mason { 30a52d9a80SChris Mason if (extent_map_cache) 31a52d9a80SChris Mason kmem_cache_destroy(extent_map_cache); 32a52d9a80SChris Mason } 33a52d9a80SChris Mason 349d2423c5SChristoph Hellwig /** 359d2423c5SChristoph Hellwig * extent_map_tree_init - initialize extent map tree 369d2423c5SChristoph Hellwig * @tree: tree to initialize 379d2423c5SChristoph Hellwig * @mask: flags for memory allocations during tree operations 389d2423c5SChristoph Hellwig * 399d2423c5SChristoph Hellwig * Initialize the extent tree @tree. Should be called for each new inode 409d2423c5SChristoph Hellwig * or other user of the extent_map interface. 419d2423c5SChristoph Hellwig */ 42d1310b2eSChris Mason void extent_map_tree_init(struct extent_map_tree *tree, gfp_t mask) 43a52d9a80SChris Mason { 44a52d9a80SChris Mason tree->map.rb_node = NULL; 45d1310b2eSChris Mason spin_lock_init(&tree->lock); 46a52d9a80SChris Mason } 47a52d9a80SChris Mason EXPORT_SYMBOL(extent_map_tree_init); 48a52d9a80SChris Mason 499d2423c5SChristoph Hellwig /** 509d2423c5SChristoph Hellwig * alloc_extent_map - allocate new extent map structure 519d2423c5SChristoph Hellwig * @mask: memory allocation flags 529d2423c5SChristoph Hellwig * 539d2423c5SChristoph Hellwig * Allocate a new extent_map structure. The new structure is 549d2423c5SChristoph Hellwig * returned with a reference count of one and needs to be 559d2423c5SChristoph Hellwig * freed using free_extent_map() 569d2423c5SChristoph Hellwig */ 57a52d9a80SChris Mason struct extent_map *alloc_extent_map(gfp_t mask) 58a52d9a80SChris Mason { 59a52d9a80SChris Mason struct extent_map *em; 60a52d9a80SChris Mason em = kmem_cache_alloc(extent_map_cache, mask); 61a52d9a80SChris Mason if (!em || IS_ERR(em)) 62a52d9a80SChris Mason return em; 63a52d9a80SChris Mason em->in_tree = 0; 64d1310b2eSChris Mason em->flags = 0; 65a52d9a80SChris Mason atomic_set(&em->refs, 1); 66a52d9a80SChris Mason return em; 67a52d9a80SChris Mason } 68a52d9a80SChris Mason EXPORT_SYMBOL(alloc_extent_map); 69a52d9a80SChris Mason 709d2423c5SChristoph Hellwig /** 719d2423c5SChristoph Hellwig * free_extent_map - drop reference count of an extent_map 729d2423c5SChristoph Hellwig * @em: extent map beeing releasead 739d2423c5SChristoph Hellwig * 749d2423c5SChristoph Hellwig * Drops the reference out on @em by one and free the structure 759d2423c5SChristoph Hellwig * if the reference count hits zero. 769d2423c5SChristoph Hellwig */ 77a52d9a80SChris Mason void free_extent_map(struct extent_map *em) 78a52d9a80SChris Mason { 792bf5a725SChris Mason if (!em) 802bf5a725SChris Mason return; 81d1310b2eSChris Mason WARN_ON(atomic_read(&em->refs) == 0); 82a52d9a80SChris Mason if (atomic_dec_and_test(&em->refs)) { 83a52d9a80SChris Mason WARN_ON(em->in_tree); 84a52d9a80SChris Mason kmem_cache_free(extent_map_cache, em); 85a52d9a80SChris Mason } 86a52d9a80SChris Mason } 87a52d9a80SChris Mason EXPORT_SYMBOL(free_extent_map); 88a52d9a80SChris Mason 89a52d9a80SChris Mason static struct rb_node *tree_insert(struct rb_root *root, u64 offset, 90a52d9a80SChris Mason struct rb_node *node) 91a52d9a80SChris Mason { 92a52d9a80SChris Mason struct rb_node ** p = &root->rb_node; 93a52d9a80SChris Mason struct rb_node * parent = NULL; 94d1310b2eSChris Mason struct extent_map *entry; 95a52d9a80SChris Mason 96a52d9a80SChris Mason while(*p) { 97a52d9a80SChris Mason parent = *p; 98d1310b2eSChris Mason entry = rb_entry(parent, struct extent_map, rb_node); 99d1310b2eSChris Mason 100d1310b2eSChris Mason WARN_ON(!entry->in_tree); 101a52d9a80SChris Mason 102a52d9a80SChris Mason if (offset < entry->start) 103a52d9a80SChris Mason p = &(*p)->rb_left; 104d1310b2eSChris Mason else if (offset >= extent_map_end(entry)) 105a52d9a80SChris Mason p = &(*p)->rb_right; 106a52d9a80SChris Mason else 107a52d9a80SChris Mason return parent; 108a52d9a80SChris Mason } 109a52d9a80SChris Mason 110d1310b2eSChris Mason entry = rb_entry(node, struct extent_map, rb_node); 111a52d9a80SChris Mason entry->in_tree = 1; 112a52d9a80SChris Mason rb_link_node(node, parent, p); 113a52d9a80SChris Mason rb_insert_color(node, root); 114a52d9a80SChris Mason return NULL; 115a52d9a80SChris Mason } 116a52d9a80SChris Mason 117*d352ac68SChris Mason /* 118*d352ac68SChris Mason * search through the tree for an extent_map with a given offset. If 119*d352ac68SChris Mason * it can't be found, try to find some neighboring extents 120*d352ac68SChris Mason */ 121a52d9a80SChris Mason static struct rb_node *__tree_search(struct rb_root *root, u64 offset, 1225f56406aSChris Mason struct rb_node **prev_ret, 1235f56406aSChris Mason struct rb_node **next_ret) 124a52d9a80SChris Mason { 125a52d9a80SChris Mason struct rb_node * n = root->rb_node; 126a52d9a80SChris Mason struct rb_node *prev = NULL; 1275f56406aSChris Mason struct rb_node *orig_prev = NULL; 128d1310b2eSChris Mason struct extent_map *entry; 129d1310b2eSChris Mason struct extent_map *prev_entry = NULL; 130a52d9a80SChris Mason 131a52d9a80SChris Mason while(n) { 132d1310b2eSChris Mason entry = rb_entry(n, struct extent_map, rb_node); 133a52d9a80SChris Mason prev = n; 134a52d9a80SChris Mason prev_entry = entry; 135a52d9a80SChris Mason 136d1310b2eSChris Mason WARN_ON(!entry->in_tree); 137d1310b2eSChris Mason 138a52d9a80SChris Mason if (offset < entry->start) 139a52d9a80SChris Mason n = n->rb_left; 140d1310b2eSChris Mason else if (offset >= extent_map_end(entry)) 141a52d9a80SChris Mason n = n->rb_right; 142a52d9a80SChris Mason else 143a52d9a80SChris Mason return n; 144a52d9a80SChris Mason } 1455f56406aSChris Mason 1465f56406aSChris Mason if (prev_ret) { 1475f56406aSChris Mason orig_prev = prev; 148d1310b2eSChris Mason while(prev && offset >= extent_map_end(prev_entry)) { 149a52d9a80SChris Mason prev = rb_next(prev); 150d1310b2eSChris Mason prev_entry = rb_entry(prev, struct extent_map, rb_node); 151a52d9a80SChris Mason } 152a52d9a80SChris Mason *prev_ret = prev; 1535f56406aSChris Mason prev = orig_prev; 1545f56406aSChris Mason } 1555f56406aSChris Mason 1565f56406aSChris Mason if (next_ret) { 157d1310b2eSChris Mason prev_entry = rb_entry(prev, struct extent_map, rb_node); 1585f56406aSChris Mason while(prev && offset < prev_entry->start) { 1595f56406aSChris Mason prev = rb_prev(prev); 160d1310b2eSChris Mason prev_entry = rb_entry(prev, struct extent_map, rb_node); 1615f56406aSChris Mason } 1625f56406aSChris Mason *next_ret = prev; 1635f56406aSChris Mason } 164a52d9a80SChris Mason return NULL; 165a52d9a80SChris Mason } 166a52d9a80SChris Mason 167*d352ac68SChris Mason /* 168*d352ac68SChris Mason * look for an offset in the tree, and if it can't be found, return 169*d352ac68SChris Mason * the first offset we can find smaller than 'offset'. 170*d352ac68SChris Mason */ 171a52d9a80SChris Mason static inline struct rb_node *tree_search(struct rb_root *root, u64 offset) 172a52d9a80SChris Mason { 173a52d9a80SChris Mason struct rb_node *prev; 174a52d9a80SChris Mason struct rb_node *ret; 1755f56406aSChris Mason ret = __tree_search(root, offset, &prev, NULL); 176a52d9a80SChris Mason if (!ret) 177a52d9a80SChris Mason return prev; 178a52d9a80SChris Mason return ret; 179a52d9a80SChris Mason } 180a52d9a80SChris Mason 181*d352ac68SChris Mason /* check to see if two extent_map structs are adjacent and safe to merge */ 182d1310b2eSChris Mason static int mergable_maps(struct extent_map *prev, struct extent_map *next) 183a52d9a80SChris Mason { 1847f3c74fbSChris Mason if (test_bit(EXTENT_FLAG_PINNED, &prev->flags)) 1857f3c74fbSChris Mason return 0; 1867f3c74fbSChris Mason 187d1310b2eSChris Mason if (extent_map_end(prev) == next->start && 188d1310b2eSChris Mason prev->flags == next->flags && 189d1310b2eSChris Mason prev->bdev == next->bdev && 190d1310b2eSChris Mason ((next->block_start == EXTENT_MAP_HOLE && 191d1310b2eSChris Mason prev->block_start == EXTENT_MAP_HOLE) || 192d1310b2eSChris Mason (next->block_start == EXTENT_MAP_INLINE && 193d1310b2eSChris Mason prev->block_start == EXTENT_MAP_INLINE) || 194d1310b2eSChris Mason (next->block_start == EXTENT_MAP_DELALLOC && 195d1310b2eSChris Mason prev->block_start == EXTENT_MAP_DELALLOC) || 196d1310b2eSChris Mason (next->block_start < EXTENT_MAP_LAST_BYTE - 1 && 197d1310b2eSChris Mason next->block_start == extent_map_block_end(prev)))) { 198d1310b2eSChris Mason return 1; 199d1310b2eSChris Mason } 200a52d9a80SChris Mason return 0; 201a52d9a80SChris Mason } 202a52d9a80SChris Mason 2039d2423c5SChristoph Hellwig /** 2049d2423c5SChristoph Hellwig * add_extent_mapping - add new extent map to the extent tree 2059d2423c5SChristoph Hellwig * @tree: tree to insert new map in 2069d2423c5SChristoph Hellwig * @em: map to insert 2079d2423c5SChristoph Hellwig * 2089d2423c5SChristoph Hellwig * Insert @em into @tree or perform a simple forward/backward merge with 2099d2423c5SChristoph Hellwig * existing mappings. The extent_map struct passed in will be inserted 2109d2423c5SChristoph Hellwig * into the tree directly, with an additional reference taken, or a 2119d2423c5SChristoph Hellwig * reference dropped if the merge attempt was sucessfull. 212a52d9a80SChris Mason */ 213a52d9a80SChris Mason int add_extent_mapping(struct extent_map_tree *tree, 214a52d9a80SChris Mason struct extent_map *em) 215a52d9a80SChris Mason { 216a52d9a80SChris Mason int ret = 0; 217d1310b2eSChris Mason struct extent_map *merge = NULL; 218a52d9a80SChris Mason struct rb_node *rb; 2197c2fe32aSChris Mason struct extent_map *exist; 220a52d9a80SChris Mason 2217c2fe32aSChris Mason exist = lookup_extent_mapping(tree, em->start, em->len); 2227c2fe32aSChris Mason if (exist) { 2237c2fe32aSChris Mason free_extent_map(exist); 2247c2fe32aSChris Mason ret = -EEXIST; 2257c2fe32aSChris Mason goto out; 2267c2fe32aSChris Mason } 22764f26f74SDavid Woodhouse assert_spin_locked(&tree->lock); 228d1310b2eSChris Mason rb = tree_insert(&tree->map, em->start, &em->rb_node); 229a52d9a80SChris Mason if (rb) { 230a52d9a80SChris Mason ret = -EEXIST; 231e6dcd2dcSChris Mason free_extent_map(merge); 232a52d9a80SChris Mason goto out; 233a52d9a80SChris Mason } 234a52d9a80SChris Mason atomic_inc(&em->refs); 235a52d9a80SChris Mason if (em->start != 0) { 236a52d9a80SChris Mason rb = rb_prev(&em->rb_node); 237a52d9a80SChris Mason if (rb) 238d1310b2eSChris Mason merge = rb_entry(rb, struct extent_map, rb_node); 239d1310b2eSChris Mason if (rb && mergable_maps(merge, em)) { 240d1310b2eSChris Mason em->start = merge->start; 241d1310b2eSChris Mason em->len += merge->len; 242d1310b2eSChris Mason em->block_start = merge->block_start; 243d1310b2eSChris Mason merge->in_tree = 0; 244d1310b2eSChris Mason rb_erase(&merge->rb_node, &tree->map); 245d1310b2eSChris Mason free_extent_map(merge); 246a52d9a80SChris Mason } 247a52d9a80SChris Mason } 248d1310b2eSChris Mason rb = rb_next(&em->rb_node); 249d1310b2eSChris Mason if (rb) 250d1310b2eSChris Mason merge = rb_entry(rb, struct extent_map, rb_node); 251d1310b2eSChris Mason if (rb && mergable_maps(em, merge)) { 252d1310b2eSChris Mason em->len += merge->len; 253d1310b2eSChris Mason rb_erase(&merge->rb_node, &tree->map); 254d1310b2eSChris Mason merge->in_tree = 0; 255d1310b2eSChris Mason free_extent_map(merge); 256d1310b2eSChris Mason } 257a52d9a80SChris Mason out: 258a52d9a80SChris Mason return ret; 259a52d9a80SChris Mason } 260a52d9a80SChris Mason EXPORT_SYMBOL(add_extent_mapping); 261a52d9a80SChris Mason 262*d352ac68SChris Mason /* simple helper to do math around the end of an extent, handling wrap */ 263d1310b2eSChris Mason static u64 range_end(u64 start, u64 len) 264d1310b2eSChris Mason { 265d1310b2eSChris Mason if (start + len < start) 266d1310b2eSChris Mason return (u64)-1; 267d1310b2eSChris Mason return start + len; 268d1310b2eSChris Mason } 269d1310b2eSChris Mason 2709d2423c5SChristoph Hellwig /** 2719d2423c5SChristoph Hellwig * lookup_extent_mapping - lookup extent_map 2729d2423c5SChristoph Hellwig * @tree: tree to lookup in 2739d2423c5SChristoph Hellwig * @start: byte offset to start the search 2749d2423c5SChristoph Hellwig * @len: length of the lookup range 2759d2423c5SChristoph Hellwig * 2769d2423c5SChristoph Hellwig * Find and return the first extent_map struct in @tree that intersects the 2779d2423c5SChristoph Hellwig * [start, len] range. There may be additional objects in the tree that 2789d2423c5SChristoph Hellwig * intersect, so check the object returned carefully to make sure that no 2799d2423c5SChristoph Hellwig * additional lookups are needed. 280a52d9a80SChris Mason */ 281a52d9a80SChris Mason struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree, 282d1310b2eSChris Mason u64 start, u64 len) 283a52d9a80SChris Mason { 284a52d9a80SChris Mason struct extent_map *em; 285a52d9a80SChris Mason struct rb_node *rb_node; 286306929f3SChristoph Hellwig struct rb_node *prev = NULL; 287306929f3SChristoph Hellwig struct rb_node *next = NULL; 288306929f3SChristoph Hellwig u64 end = range_end(start, len); 289306929f3SChristoph Hellwig 29064f26f74SDavid Woodhouse assert_spin_locked(&tree->lock); 2915f56406aSChris Mason rb_node = __tree_search(&tree->map, start, &prev, &next); 2925f56406aSChris Mason if (!rb_node && prev) { 2935f56406aSChris Mason em = rb_entry(prev, struct extent_map, rb_node); 294d1310b2eSChris Mason if (end > em->start && start < extent_map_end(em)) 2955f56406aSChris Mason goto found; 2965f56406aSChris Mason } 2975f56406aSChris Mason if (!rb_node && next) { 2985f56406aSChris Mason em = rb_entry(next, struct extent_map, rb_node); 299d1310b2eSChris Mason if (end > em->start && start < extent_map_end(em)) 3005f56406aSChris Mason goto found; 3015f56406aSChris Mason } 302a52d9a80SChris Mason if (!rb_node) { 303a52d9a80SChris Mason em = NULL; 304a52d9a80SChris Mason goto out; 305a52d9a80SChris Mason } 306a52d9a80SChris Mason if (IS_ERR(rb_node)) { 307a52d9a80SChris Mason em = ERR_PTR(PTR_ERR(rb_node)); 308a52d9a80SChris Mason goto out; 309a52d9a80SChris Mason } 310a52d9a80SChris Mason em = rb_entry(rb_node, struct extent_map, rb_node); 311d1310b2eSChris Mason if (end > em->start && start < extent_map_end(em)) 312d1310b2eSChris Mason goto found; 313d1310b2eSChris Mason 314a52d9a80SChris Mason em = NULL; 315a52d9a80SChris Mason goto out; 316d1310b2eSChris Mason 3175f56406aSChris Mason found: 318a52d9a80SChris Mason atomic_inc(&em->refs); 319a52d9a80SChris Mason out: 320a52d9a80SChris Mason return em; 321a52d9a80SChris Mason } 322a52d9a80SChris Mason EXPORT_SYMBOL(lookup_extent_mapping); 323a52d9a80SChris Mason 3249d2423c5SChristoph Hellwig /** 3259d2423c5SChristoph Hellwig * remove_extent_mapping - removes an extent_map from the extent tree 3269d2423c5SChristoph Hellwig * @tree: extent tree to remove from 3279d2423c5SChristoph Hellwig * @em: extent map beeing removed 3289d2423c5SChristoph Hellwig * 3299d2423c5SChristoph Hellwig * Removes @em from @tree. No reference counts are dropped, and no checks 3309d2423c5SChristoph Hellwig * are done to see if the range is in use 331a52d9a80SChris Mason */ 332a52d9a80SChris Mason int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em) 333a52d9a80SChris Mason { 334d1310b2eSChris Mason int ret = 0; 335a52d9a80SChris Mason 3367f3c74fbSChris Mason WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags)); 33764f26f74SDavid Woodhouse assert_spin_locked(&tree->lock); 338d1310b2eSChris Mason rb_erase(&em->rb_node, &tree->map); 339d1310b2eSChris Mason em->in_tree = 0; 340a52d9a80SChris Mason return ret; 341a52d9a80SChris Mason } 342a52d9a80SChris Mason EXPORT_SYMBOL(remove_extent_mapping); 343