Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1 | #include <linux/err.h> |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 2 | #include <linux/gfp.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3 | #include <linux/slab.h> |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 4 | #include <linux/module.h> |
| 5 | #include <linux/spinlock.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6 | #include <linux/hardirq.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 | { |
| 38 | tree->map.rb_node = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 39 | spin_lock_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); |
| 54 | if (!em || IS_ERR(em)) |
| 55 | return em; |
| 56 | em->in_tree = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 57 | em->flags = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 58 | atomic_set(&em->refs, 1); |
| 59 | return em; |
| 60 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 61 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 62 | /** |
| 63 | * free_extent_map - drop reference count of an extent_map |
| 64 | * @em: extent map beeing releasead |
| 65 | * |
| 66 | * Drops the reference out on @em by one and free the structure |
| 67 | * if the reference count hits zero. |
| 68 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 69 | void free_extent_map(struct extent_map *em) |
| 70 | { |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 71 | if (!em) |
| 72 | return; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 73 | WARN_ON(atomic_read(&em->refs) == 0); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 74 | if (atomic_dec_and_test(&em->refs)) { |
| 75 | WARN_ON(em->in_tree); |
| 76 | kmem_cache_free(extent_map_cache, em); |
| 77 | } |
| 78 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 79 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 80 | static struct rb_node *tree_insert(struct rb_root *root, u64 offset, |
| 81 | struct rb_node *node) |
| 82 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 83 | struct rb_node **p = &root->rb_node; |
| 84 | struct rb_node *parent = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 85 | struct extent_map *entry; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 86 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 87 | while (*p) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 88 | parent = *p; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 89 | entry = rb_entry(parent, struct extent_map, rb_node); |
| 90 | |
| 91 | WARN_ON(!entry->in_tree); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 92 | |
| 93 | if (offset < entry->start) |
| 94 | p = &(*p)->rb_left; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 95 | else if (offset >= extent_map_end(entry)) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 96 | p = &(*p)->rb_right; |
| 97 | else |
| 98 | return parent; |
| 99 | } |
| 100 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 101 | entry = rb_entry(node, struct extent_map, rb_node); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 102 | entry->in_tree = 1; |
| 103 | rb_link_node(node, parent, p); |
| 104 | rb_insert_color(node, root); |
| 105 | return NULL; |
| 106 | } |
| 107 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 108 | /* |
| 109 | * search through the tree for an extent_map with a given offset. If |
| 110 | * it can't be found, try to find some neighboring extents |
| 111 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 112 | static struct rb_node *__tree_search(struct rb_root *root, u64 offset, |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 113 | struct rb_node **prev_ret, |
| 114 | struct rb_node **next_ret) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 115 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 116 | struct rb_node *n = root->rb_node; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 117 | struct rb_node *prev = NULL; |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 118 | struct rb_node *orig_prev = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 119 | struct extent_map *entry; |
| 120 | struct extent_map *prev_entry = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 121 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 122 | while (n) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 123 | entry = rb_entry(n, struct extent_map, rb_node); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 124 | prev = n; |
| 125 | prev_entry = entry; |
| 126 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 127 | WARN_ON(!entry->in_tree); |
| 128 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 129 | if (offset < entry->start) |
| 130 | n = n->rb_left; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 131 | else if (offset >= extent_map_end(entry)) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 132 | n = n->rb_right; |
| 133 | else |
| 134 | return n; |
| 135 | } |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 136 | |
| 137 | if (prev_ret) { |
| 138 | orig_prev = prev; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 139 | while (prev && offset >= extent_map_end(prev_entry)) { |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 140 | prev = rb_next(prev); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 141 | prev_entry = rb_entry(prev, struct extent_map, rb_node); |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 142 | } |
| 143 | *prev_ret = prev; |
| 144 | prev = orig_prev; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 145 | } |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 146 | |
| 147 | if (next_ret) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 148 | prev_entry = rb_entry(prev, struct extent_map, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 149 | while (prev && offset < prev_entry->start) { |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 150 | prev = rb_prev(prev); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 151 | prev_entry = rb_entry(prev, struct extent_map, rb_node); |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 152 | } |
| 153 | *next_ret = prev; |
| 154 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 155 | return NULL; |
| 156 | } |
| 157 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 158 | /* |
| 159 | * look for an offset in the tree, and if it can't be found, return |
| 160 | * the first offset we can find smaller than 'offset'. |
| 161 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 162 | static inline struct rb_node *tree_search(struct rb_root *root, u64 offset) |
| 163 | { |
| 164 | struct rb_node *prev; |
| 165 | struct rb_node *ret; |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 166 | ret = __tree_search(root, offset, &prev, NULL); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 167 | if (!ret) |
| 168 | return prev; |
| 169 | return ret; |
| 170 | } |
| 171 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 172 | /* 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] | 173 | static int mergable_maps(struct extent_map *prev, struct extent_map *next) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 174 | { |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 175 | if (test_bit(EXTENT_FLAG_PINNED, &prev->flags)) |
| 176 | return 0; |
| 177 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 178 | /* |
| 179 | * don't merge compressed extents, we need to know their |
| 180 | * actual size |
| 181 | */ |
| 182 | if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags)) |
| 183 | return 0; |
| 184 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 185 | if (extent_map_end(prev) == next->start && |
| 186 | prev->flags == next->flags && |
| 187 | prev->bdev == next->bdev && |
| 188 | ((next->block_start == EXTENT_MAP_HOLE && |
| 189 | prev->block_start == EXTENT_MAP_HOLE) || |
| 190 | (next->block_start == EXTENT_MAP_INLINE && |
| 191 | prev->block_start == EXTENT_MAP_INLINE) || |
| 192 | (next->block_start == EXTENT_MAP_DELALLOC && |
| 193 | prev->block_start == EXTENT_MAP_DELALLOC) || |
| 194 | (next->block_start < EXTENT_MAP_LAST_BYTE - 1 && |
| 195 | next->block_start == extent_map_block_end(prev)))) { |
| 196 | return 1; |
| 197 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 201 | /** |
| 202 | * add_extent_mapping - add new extent map to the extent tree |
| 203 | * @tree: tree to insert new map in |
| 204 | * @em: map to insert |
| 205 | * |
| 206 | * Insert @em into @tree or perform a simple forward/backward merge with |
| 207 | * existing mappings. The extent_map struct passed in will be inserted |
| 208 | * into the tree directly, with an additional reference taken, or a |
| 209 | * reference dropped if the merge attempt was sucessfull. |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 210 | */ |
| 211 | int add_extent_mapping(struct extent_map_tree *tree, |
| 212 | struct extent_map *em) |
| 213 | { |
| 214 | int ret = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 215 | struct extent_map *merge = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 216 | struct rb_node *rb; |
Chris Mason | 7c2fe32 | 2008-08-20 08:51:50 -0400 | [diff] [blame] | 217 | struct extent_map *exist; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 218 | |
Chris Mason | 7c2fe32 | 2008-08-20 08:51:50 -0400 | [diff] [blame] | 219 | exist = lookup_extent_mapping(tree, em->start, em->len); |
| 220 | if (exist) { |
| 221 | free_extent_map(exist); |
| 222 | ret = -EEXIST; |
| 223 | goto out; |
| 224 | } |
David Woodhouse | 64f26f7 | 2008-07-24 10:09:43 -0400 | [diff] [blame] | 225 | assert_spin_locked(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 226 | rb = tree_insert(&tree->map, em->start, &em->rb_node); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 227 | if (rb) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 228 | ret = -EEXIST; |
| 229 | goto out; |
| 230 | } |
| 231 | atomic_inc(&em->refs); |
| 232 | if (em->start != 0) { |
| 233 | rb = rb_prev(&em->rb_node); |
| 234 | if (rb) |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 235 | merge = rb_entry(rb, struct extent_map, rb_node); |
| 236 | if (rb && mergable_maps(merge, em)) { |
| 237 | em->start = merge->start; |
| 238 | em->len += merge->len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 239 | em->block_len += merge->block_len; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 240 | em->block_start = merge->block_start; |
| 241 | merge->in_tree = 0; |
| 242 | rb_erase(&merge->rb_node, &tree->map); |
| 243 | free_extent_map(merge); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 244 | } |
| 245 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 246 | rb = rb_next(&em->rb_node); |
| 247 | if (rb) |
| 248 | merge = rb_entry(rb, struct extent_map, rb_node); |
| 249 | if (rb && mergable_maps(em, merge)) { |
| 250 | em->len += merge->len; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 251 | em->block_len += merge->len; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 252 | rb_erase(&merge->rb_node, &tree->map); |
| 253 | merge->in_tree = 0; |
| 254 | free_extent_map(merge); |
| 255 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 256 | out: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 257 | return ret; |
| 258 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 259 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 260 | /* 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] | 261 | static u64 range_end(u64 start, u64 len) |
| 262 | { |
| 263 | if (start + len < start) |
| 264 | return (u64)-1; |
| 265 | return start + len; |
| 266 | } |
| 267 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 268 | /** |
| 269 | * lookup_extent_mapping - lookup extent_map |
| 270 | * @tree: tree to lookup in |
| 271 | * @start: byte offset to start the search |
| 272 | * @len: length of the lookup range |
| 273 | * |
| 274 | * Find and return the first extent_map struct in @tree that intersects the |
| 275 | * [start, len] range. There may be additional objects in the tree that |
| 276 | * intersect, so check the object returned carefully to make sure that no |
| 277 | * additional lookups are needed. |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 278 | */ |
| 279 | struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 280 | u64 start, u64 len) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 281 | { |
| 282 | struct extent_map *em; |
| 283 | struct rb_node *rb_node; |
Christoph Hellwig | 306929f | 2008-06-10 10:21:04 -0400 | [diff] [blame] | 284 | struct rb_node *prev = NULL; |
| 285 | struct rb_node *next = NULL; |
| 286 | u64 end = range_end(start, len); |
| 287 | |
David Woodhouse | 64f26f7 | 2008-07-24 10:09:43 -0400 | [diff] [blame] | 288 | assert_spin_locked(&tree->lock); |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 289 | rb_node = __tree_search(&tree->map, start, &prev, &next); |
| 290 | if (!rb_node && prev) { |
| 291 | em = rb_entry(prev, struct extent_map, rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 292 | if (end > em->start && start < extent_map_end(em)) |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 293 | goto found; |
| 294 | } |
| 295 | if (!rb_node && next) { |
| 296 | em = rb_entry(next, struct extent_map, rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 297 | if (end > em->start && start < extent_map_end(em)) |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 298 | goto found; |
| 299 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 300 | if (!rb_node) { |
| 301 | em = NULL; |
| 302 | goto out; |
| 303 | } |
| 304 | if (IS_ERR(rb_node)) { |
| 305 | em = ERR_PTR(PTR_ERR(rb_node)); |
| 306 | goto out; |
| 307 | } |
| 308 | em = rb_entry(rb_node, struct extent_map, rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 309 | if (end > em->start && start < extent_map_end(em)) |
| 310 | goto found; |
| 311 | |
| 312 | em = NULL; |
| 313 | goto out; |
| 314 | |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 315 | found: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 316 | atomic_inc(&em->refs); |
| 317 | out: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 318 | return em; |
| 319 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 320 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 321 | /** |
| 322 | * remove_extent_mapping - removes an extent_map from the extent tree |
| 323 | * @tree: extent tree to remove from |
| 324 | * @em: extent map beeing removed |
| 325 | * |
| 326 | * Removes @em from @tree. No reference counts are dropped, and no checks |
| 327 | * are done to see if the range is in use |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 328 | */ |
| 329 | int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em) |
| 330 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 331 | int ret = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 332 | |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 333 | WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags)); |
David Woodhouse | 64f26f7 | 2008-07-24 10:09:43 -0400 | [diff] [blame] | 334 | assert_spin_locked(&tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 335 | rb_erase(&em->rb_node, &tree->map); |
| 336 | em->in_tree = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 337 | return ret; |
| 338 | } |