Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1 | #include <linux/err.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2 | #include <linux/slab.h> |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3 | #include <linux/module.h> |
| 4 | #include <linux/spinlock.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5 | #include <linux/hardirq.h> |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6 | #include "ctree.h" |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7 | #include "extent_map.h" |
| 8 | |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 9 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 10 | static struct kmem_cache *extent_map_cache; |
Chris Mason | ca66462 | 2007-11-27 11:16:35 -0500 | [diff] [blame] | 11 | |
Wyatt Banks | 2f4cbe6 | 2007-11-19 10:22:33 -0500 | [diff] [blame] | 12 | int __init extent_map_init(void) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 13 | { |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 14 | extent_map_cache = kmem_cache_create("extent_map", |
| 15 | sizeof(struct extent_map), 0, |
| 16 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Wyatt Banks | 2f4cbe6 | 2007-11-19 10:22:33 -0500 | [diff] [blame] | 17 | if (!extent_map_cache) |
| 18 | return -ENOMEM; |
Wyatt Banks | 2f4cbe6 | 2007-11-19 10:22:33 -0500 | [diff] [blame] | 19 | return 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 20 | } |
| 21 | |
Christian Hesse | 17636e0 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 22 | void extent_map_exit(void) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 23 | { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 24 | if (extent_map_cache) |
| 25 | kmem_cache_destroy(extent_map_cache); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 26 | } |
| 27 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 28 | /** |
| 29 | * extent_map_tree_init - initialize extent map tree |
| 30 | * @tree: tree to initialize |
| 31 | * @mask: flags for memory allocations during tree operations |
| 32 | * |
| 33 | * Initialize the extent tree @tree. Should be called for each new inode |
| 34 | * or other user of the extent_map interface. |
| 35 | */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 36 | void extent_map_tree_init(struct extent_map_tree *tree, gfp_t mask) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 37 | { |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 38 | tree->map = RB_ROOT; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 39 | rwlock_init(&tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 40 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 41 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 42 | /** |
| 43 | * alloc_extent_map - allocate new extent map structure |
| 44 | * @mask: memory allocation flags |
| 45 | * |
| 46 | * Allocate a new extent_map structure. The new structure is |
| 47 | * returned with a reference count of one and needs to be |
| 48 | * freed using free_extent_map() |
| 49 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 50 | struct extent_map *alloc_extent_map(gfp_t mask) |
| 51 | { |
| 52 | struct extent_map *em; |
| 53 | em = kmem_cache_alloc(extent_map_cache, mask); |
Tsutomu Itoh | c26a920 | 2011-02-14 00:45:29 +0000 | [diff] [blame] | 54 | if (!em) |
| 55 | return NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 56 | em->in_tree = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 57 | em->flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 58 | em->compress_type = BTRFS_COMPRESS_NONE; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 59 | atomic_set(&em->refs, 1); |
| 60 | return em; |
| 61 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 62 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 63 | /** |
| 64 | * free_extent_map - drop reference count of an extent_map |
| 65 | * @em: extent map beeing releasead |
| 66 | * |
| 67 | * Drops the reference out on @em by one and free the structure |
| 68 | * if the reference count hits zero. |
| 69 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 70 | void free_extent_map(struct extent_map *em) |
| 71 | { |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 72 | if (!em) |
| 73 | return; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 74 | WARN_ON(atomic_read(&em->refs) == 0); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 75 | if (atomic_dec_and_test(&em->refs)) { |
| 76 | WARN_ON(em->in_tree); |
| 77 | kmem_cache_free(extent_map_cache, em); |
| 78 | } |
| 79 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 80 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 81 | static struct rb_node *tree_insert(struct rb_root *root, u64 offset, |
| 82 | struct rb_node *node) |
| 83 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 84 | struct rb_node **p = &root->rb_node; |
| 85 | struct rb_node *parent = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 86 | struct extent_map *entry; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 87 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 88 | while (*p) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 89 | parent = *p; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 90 | entry = rb_entry(parent, struct extent_map, rb_node); |
| 91 | |
| 92 | WARN_ON(!entry->in_tree); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 93 | |
| 94 | if (offset < entry->start) |
| 95 | p = &(*p)->rb_left; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 96 | else if (offset >= extent_map_end(entry)) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 97 | p = &(*p)->rb_right; |
| 98 | else |
| 99 | return parent; |
| 100 | } |
| 101 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 102 | entry = rb_entry(node, struct extent_map, rb_node); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 103 | entry->in_tree = 1; |
| 104 | rb_link_node(node, parent, p); |
| 105 | rb_insert_color(node, root); |
| 106 | return NULL; |
| 107 | } |
| 108 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 109 | /* |
| 110 | * search through the tree for an extent_map with a given offset. If |
| 111 | * it can't be found, try to find some neighboring extents |
| 112 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 113 | static struct rb_node *__tree_search(struct rb_root *root, u64 offset, |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 114 | struct rb_node **prev_ret, |
| 115 | struct rb_node **next_ret) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 116 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 117 | struct rb_node *n = root->rb_node; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 118 | struct rb_node *prev = NULL; |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 119 | struct rb_node *orig_prev = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 120 | struct extent_map *entry; |
| 121 | struct extent_map *prev_entry = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 122 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 123 | while (n) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 124 | entry = rb_entry(n, struct extent_map, rb_node); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 125 | prev = n; |
| 126 | prev_entry = entry; |
| 127 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 128 | WARN_ON(!entry->in_tree); |
| 129 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 130 | if (offset < entry->start) |
| 131 | n = n->rb_left; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 132 | else if (offset >= extent_map_end(entry)) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 133 | n = n->rb_right; |
| 134 | else |
| 135 | return n; |
| 136 | } |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 137 | |
| 138 | if (prev_ret) { |
| 139 | orig_prev = prev; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 140 | while (prev && offset >= extent_map_end(prev_entry)) { |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 141 | prev = rb_next(prev); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 142 | prev_entry = rb_entry(prev, struct extent_map, rb_node); |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 143 | } |
| 144 | *prev_ret = prev; |
| 145 | prev = orig_prev; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 146 | } |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 147 | |
| 148 | if (next_ret) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 149 | prev_entry = rb_entry(prev, struct extent_map, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 150 | while (prev && offset < prev_entry->start) { |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 151 | prev = rb_prev(prev); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 152 | prev_entry = rb_entry(prev, struct extent_map, rb_node); |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 153 | } |
| 154 | *next_ret = prev; |
| 155 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 156 | return NULL; |
| 157 | } |
| 158 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 159 | /* check to see if two extent_map structs are adjacent and safe to merge */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 160 | static int mergable_maps(struct extent_map *prev, struct extent_map *next) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 161 | { |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 162 | if (test_bit(EXTENT_FLAG_PINNED, &prev->flags)) |
| 163 | return 0; |
| 164 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 165 | /* |
| 166 | * don't merge compressed extents, we need to know their |
| 167 | * actual size |
| 168 | */ |
| 169 | if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags)) |
| 170 | return 0; |
| 171 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 172 | if (extent_map_end(prev) == next->start && |
| 173 | prev->flags == next->flags && |
| 174 | prev->bdev == next->bdev && |
| 175 | ((next->block_start == EXTENT_MAP_HOLE && |
| 176 | prev->block_start == EXTENT_MAP_HOLE) || |
| 177 | (next->block_start == EXTENT_MAP_INLINE && |
| 178 | prev->block_start == EXTENT_MAP_INLINE) || |
| 179 | (next->block_start == EXTENT_MAP_DELALLOC && |
| 180 | prev->block_start == EXTENT_MAP_DELALLOC) || |
| 181 | (next->block_start < EXTENT_MAP_LAST_BYTE - 1 && |
| 182 | next->block_start == extent_map_block_end(prev)))) { |
| 183 | return 1; |
| 184 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 188 | int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len) |
| 189 | { |
| 190 | int ret = 0; |
| 191 | struct extent_map *merge = NULL; |
| 192 | struct rb_node *rb; |
| 193 | struct extent_map *em; |
| 194 | |
| 195 | write_lock(&tree->lock); |
| 196 | em = lookup_extent_mapping(tree, start, len); |
| 197 | |
Dan Carpenter | 4eb3991 | 2009-11-10 09:01:43 +0000 | [diff] [blame] | 198 | WARN_ON(!em || em->start != start); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 199 | |
| 200 | if (!em) |
| 201 | goto out; |
| 202 | |
| 203 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 204 | |
| 205 | if (em->start != 0) { |
| 206 | rb = rb_prev(&em->rb_node); |
| 207 | if (rb) |
| 208 | merge = rb_entry(rb, struct extent_map, rb_node); |
| 209 | if (rb && mergable_maps(merge, em)) { |
| 210 | em->start = merge->start; |
| 211 | em->len += merge->len; |
| 212 | em->block_len += merge->block_len; |
| 213 | em->block_start = merge->block_start; |
| 214 | merge->in_tree = 0; |
| 215 | rb_erase(&merge->rb_node, &tree->map); |
| 216 | free_extent_map(merge); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | rb = rb_next(&em->rb_node); |
| 221 | if (rb) |
| 222 | merge = rb_entry(rb, struct extent_map, rb_node); |
| 223 | if (rb && mergable_maps(em, merge)) { |
| 224 | em->len += merge->len; |
| 225 | em->block_len += merge->len; |
| 226 | rb_erase(&merge->rb_node, &tree->map); |
| 227 | merge->in_tree = 0; |
| 228 | free_extent_map(merge); |
| 229 | } |
| 230 | |
| 231 | free_extent_map(em); |
| 232 | out: |
| 233 | write_unlock(&tree->lock); |
| 234 | return ret; |
| 235 | |
| 236 | } |
| 237 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 238 | /** |
| 239 | * add_extent_mapping - add new extent map to the extent tree |
| 240 | * @tree: tree to insert new map in |
| 241 | * @em: map to insert |
| 242 | * |
| 243 | * Insert @em into @tree or perform a simple forward/backward merge with |
| 244 | * existing mappings. The extent_map struct passed in will be inserted |
| 245 | * into the tree directly, with an additional reference taken, or a |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 246 | * reference dropped if the merge attempt was successful. |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 247 | */ |
| 248 | int add_extent_mapping(struct extent_map_tree *tree, |
| 249 | struct extent_map *em) |
| 250 | { |
| 251 | int ret = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 252 | struct extent_map *merge = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 253 | struct rb_node *rb; |
Chris Mason | 7c2fe32 | 2008-08-20 08:51:50 -0400 | [diff] [blame] | 254 | struct extent_map *exist; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 255 | |
Chris Mason | 7c2fe32 | 2008-08-20 08:51:50 -0400 | [diff] [blame] | 256 | exist = lookup_extent_mapping(tree, em->start, em->len); |
| 257 | if (exist) { |
| 258 | free_extent_map(exist); |
| 259 | ret = -EEXIST; |
| 260 | goto out; |
| 261 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 262 | rb = tree_insert(&tree->map, em->start, &em->rb_node); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 263 | if (rb) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 264 | ret = -EEXIST; |
| 265 | goto out; |
| 266 | } |
| 267 | atomic_inc(&em->refs); |
| 268 | if (em->start != 0) { |
| 269 | rb = rb_prev(&em->rb_node); |
| 270 | if (rb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 271 | merge = rb_entry(rb, struct extent_map, rb_node); |
| 272 | if (rb && mergable_maps(merge, em)) { |
| 273 | em->start = merge->start; |
| 274 | em->len += merge->len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 275 | em->block_len += merge->block_len; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 276 | em->block_start = merge->block_start; |
| 277 | merge->in_tree = 0; |
| 278 | rb_erase(&merge->rb_node, &tree->map); |
| 279 | free_extent_map(merge); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 280 | } |
| 281 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 282 | rb = rb_next(&em->rb_node); |
| 283 | if (rb) |
| 284 | merge = rb_entry(rb, struct extent_map, rb_node); |
| 285 | if (rb && mergable_maps(em, merge)) { |
| 286 | em->len += merge->len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 287 | em->block_len += merge->len; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 288 | rb_erase(&merge->rb_node, &tree->map); |
| 289 | merge->in_tree = 0; |
| 290 | free_extent_map(merge); |
| 291 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 292 | out: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 293 | return ret; |
| 294 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 295 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 296 | /* simple helper to do math around the end of an extent, handling wrap */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 297 | static u64 range_end(u64 start, u64 len) |
| 298 | { |
| 299 | if (start + len < start) |
| 300 | return (u64)-1; |
| 301 | return start + len; |
| 302 | } |
| 303 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 304 | /** |
| 305 | * lookup_extent_mapping - lookup extent_map |
| 306 | * @tree: tree to lookup in |
| 307 | * @start: byte offset to start the search |
| 308 | * @len: length of the lookup range |
| 309 | * |
| 310 | * Find and return the first extent_map struct in @tree that intersects the |
| 311 | * [start, len] range. There may be additional objects in the tree that |
| 312 | * intersect, so check the object returned carefully to make sure that no |
| 313 | * additional lookups are needed. |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 314 | */ |
| 315 | struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 316 | u64 start, u64 len) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 317 | { |
| 318 | struct extent_map *em; |
| 319 | struct rb_node *rb_node; |
Christoph Hellwig | 306929f | 2008-06-10 10:21:04 -0400 | [diff] [blame] | 320 | struct rb_node *prev = NULL; |
| 321 | struct rb_node *next = NULL; |
| 322 | u64 end = range_end(start, len); |
| 323 | |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 324 | rb_node = __tree_search(&tree->map, start, &prev, &next); |
| 325 | if (!rb_node && prev) { |
| 326 | em = rb_entry(prev, struct extent_map, rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 327 | if (end > em->start && start < extent_map_end(em)) |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 328 | goto found; |
| 329 | } |
| 330 | if (!rb_node && next) { |
| 331 | em = rb_entry(next, struct extent_map, rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 332 | if (end > em->start && start < extent_map_end(em)) |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 333 | goto found; |
| 334 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 335 | if (!rb_node) { |
| 336 | em = NULL; |
| 337 | goto out; |
| 338 | } |
| 339 | if (IS_ERR(rb_node)) { |
Julia Lawall | d0b678c | 2010-10-29 15:14:23 -0400 | [diff] [blame] | 340 | em = ERR_CAST(rb_node); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 341 | goto out; |
| 342 | } |
| 343 | em = rb_entry(rb_node, struct extent_map, rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 344 | if (end > em->start && start < extent_map_end(em)) |
| 345 | goto found; |
| 346 | |
| 347 | em = NULL; |
| 348 | goto out; |
| 349 | |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 350 | found: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 351 | atomic_inc(&em->refs); |
| 352 | out: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 353 | return em; |
| 354 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 355 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 356 | /** |
Chris Mason | b917b7c | 2009-09-18 16:07:03 -0400 | [diff] [blame] | 357 | * search_extent_mapping - find a nearby extent map |
| 358 | * @tree: tree to lookup in |
| 359 | * @start: byte offset to start the search |
| 360 | * @len: length of the lookup range |
| 361 | * |
| 362 | * Find and return the first extent_map struct in @tree that intersects the |
| 363 | * [start, len] range. |
| 364 | * |
| 365 | * If one can't be found, any nearby extent may be returned |
| 366 | */ |
| 367 | struct extent_map *search_extent_mapping(struct extent_map_tree *tree, |
| 368 | u64 start, u64 len) |
| 369 | { |
| 370 | struct extent_map *em; |
| 371 | struct rb_node *rb_node; |
| 372 | struct rb_node *prev = NULL; |
| 373 | struct rb_node *next = NULL; |
| 374 | |
| 375 | rb_node = __tree_search(&tree->map, start, &prev, &next); |
| 376 | if (!rb_node && prev) { |
| 377 | em = rb_entry(prev, struct extent_map, rb_node); |
| 378 | goto found; |
| 379 | } |
| 380 | if (!rb_node && next) { |
| 381 | em = rb_entry(next, struct extent_map, rb_node); |
| 382 | goto found; |
| 383 | } |
| 384 | if (!rb_node) { |
| 385 | em = NULL; |
| 386 | goto out; |
| 387 | } |
| 388 | if (IS_ERR(rb_node)) { |
Julia Lawall | d0b678c | 2010-10-29 15:14:23 -0400 | [diff] [blame] | 389 | em = ERR_CAST(rb_node); |
Chris Mason | b917b7c | 2009-09-18 16:07:03 -0400 | [diff] [blame] | 390 | goto out; |
| 391 | } |
| 392 | em = rb_entry(rb_node, struct extent_map, rb_node); |
| 393 | goto found; |
| 394 | |
| 395 | em = NULL; |
| 396 | goto out; |
| 397 | |
| 398 | found: |
| 399 | atomic_inc(&em->refs); |
| 400 | out: |
| 401 | return em; |
| 402 | } |
| 403 | |
| 404 | /** |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 405 | * remove_extent_mapping - removes an extent_map from the extent tree |
| 406 | * @tree: extent tree to remove from |
| 407 | * @em: extent map beeing removed |
| 408 | * |
| 409 | * Removes @em from @tree. No reference counts are dropped, and no checks |
| 410 | * are done to see if the range is in use |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 411 | */ |
| 412 | int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em) |
| 413 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 414 | int ret = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 415 | |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 416 | WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 417 | rb_erase(&em->rb_node, &tree->map); |
| 418 | em->in_tree = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 419 | return ret; |
| 420 | } |