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