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