blob: cdbadeaef2028351cbca7990234d71a1d4334c63 [file] [log] [blame]
Chris Masond1310b22008-01-24 16:13:08 -05001#include <linux/err.h>
Chris Masond1310b22008-01-24 16:13:08 -05002#include <linux/slab.h>
Chris Masona52d9a82007-08-27 16:49:44 -04003#include <linux/spinlock.h>
Chris Masond1310b22008-01-24 16:13:08 -05004#include <linux/hardirq.h>
Li Zefan261507a02010-12-17 14:21:50 +08005#include "ctree.h"
Chris Masona52d9a82007-08-27 16:49:44 -04006#include "extent_map.h"
7
Chris Mason86479a02007-09-10 19:58:16 -04008
Chris Masona52d9a82007-08-27 16:49:44 -04009static struct kmem_cache *extent_map_cache;
Chris Masonca664622007-11-27 11:16:35 -050010
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050011int __init extent_map_init(void)
Chris Masona52d9a82007-08-27 16:49:44 -040012{
David Sterba837e1972012-09-07 03:00:48 -060013 extent_map_cache = kmem_cache_create("btrfs_extent_map",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020014 sizeof(struct extent_map), 0,
15 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050016 if (!extent_map_cache)
17 return -ENOMEM;
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050018 return 0;
Chris Masona52d9a82007-08-27 16:49:44 -040019}
20
Christian Hesse17636e02007-12-11 09:25:06 -050021void extent_map_exit(void)
Chris Masona52d9a82007-08-27 16:49:44 -040022{
Kinglong Mee5598e902016-01-29 21:36:35 +080023 kmem_cache_destroy(extent_map_cache);
Chris Masona52d9a82007-08-27 16:49:44 -040024}
25
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040026/**
27 * extent_map_tree_init - initialize extent map tree
28 * @tree: tree to initialize
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040029 *
30 * Initialize the extent tree @tree. Should be called for each new inode
31 * or other user of the extent_map interface.
32 */
David Sterbaa8067e02011-04-21 00:34:43 +020033void extent_map_tree_init(struct extent_map_tree *tree)
Chris Masona52d9a82007-08-27 16:49:44 -040034{
Eric Paris6bef4d32010-02-23 19:43:04 +000035 tree->map = RB_ROOT;
Josef Bacik5dc562c2012-08-17 13:14:17 -040036 INIT_LIST_HEAD(&tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -040037 rwlock_init(&tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -040038}
Chris Masona52d9a82007-08-27 16:49:44 -040039
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040040/**
41 * alloc_extent_map - allocate new extent map structure
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040042 *
43 * Allocate a new extent_map structure. The new structure is
44 * returned with a reference count of one and needs to be
45 * freed using free_extent_map()
46 */
David Sterba172ddd62011-04-21 00:48:27 +020047struct extent_map *alloc_extent_map(void)
Chris Masona52d9a82007-08-27 16:49:44 -040048{
49 struct extent_map *em;
Josef Bacik70c8a912012-10-11 16:54:30 -040050 em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +000051 if (!em)
52 return NULL;
Filipe Mananacbc0e922014-02-25 14:15:12 +000053 RB_CLEAR_NODE(&em->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -050054 em->flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +080055 em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik5dc562c2012-08-17 13:14:17 -040056 em->generation = 0;
Chris Masona52d9a82007-08-27 16:49:44 -040057 atomic_set(&em->refs, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040058 INIT_LIST_HEAD(&em->list);
Chris Masona52d9a82007-08-27 16:49:44 -040059 return em;
60}
Chris Masona52d9a82007-08-27 16:49:44 -040061
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040062/**
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 Masona52d9a82007-08-27 16:49:44 -040069void free_extent_map(struct extent_map *em)
70{
Chris Mason2bf5a722007-08-30 11:54:02 -040071 if (!em)
72 return;
Chris Masond1310b22008-01-24 16:13:08 -050073 WARN_ON(atomic_read(&em->refs) == 0);
Chris Masona52d9a82007-08-27 16:49:44 -040074 if (atomic_dec_and_test(&em->refs)) {
Filipe Mananacbc0e922014-02-25 14:15:12 +000075 WARN_ON(extent_map_in_tree(em));
Josef Bacik5dc562c2012-08-17 13:14:17 -040076 WARN_ON(!list_empty(&em->list));
Wang Shilong298a8f92014-06-19 10:42:52 +080077 if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
Jeff Mahoney95617d62015-06-03 10:55:48 -040078 kfree(em->map_lookup);
Chris Masona52d9a82007-08-27 16:49:44 -040079 kmem_cache_free(extent_map_cache, em);
80 }
81}
Chris Masona52d9a82007-08-27 16:49:44 -040082
Filipe David Borba Manana32193c12013-11-25 03:23:51 +000083/* simple helper to do math around the end of an extent, handling wrap */
84static u64 range_end(u64 start, u64 len)
85{
86 if (start + len < start)
87 return (u64)-1;
88 return start + len;
89}
90
91static int tree_insert(struct rb_root *root, struct extent_map *em)
Chris Masona52d9a82007-08-27 16:49:44 -040092{
Chris Masond3977122009-01-05 21:25:51 -050093 struct rb_node **p = &root->rb_node;
94 struct rb_node *parent = NULL;
Filipe David Borba Manana32193c12013-11-25 03:23:51 +000095 struct extent_map *entry = NULL;
96 struct rb_node *orig_parent = NULL;
97 u64 end = range_end(em->start, em->len);
Chris Masona52d9a82007-08-27 16:49:44 -040098
Chris Masond3977122009-01-05 21:25:51 -050099 while (*p) {
Chris Masona52d9a82007-08-27 16:49:44 -0400100 parent = *p;
Chris Masond1310b22008-01-24 16:13:08 -0500101 entry = rb_entry(parent, struct extent_map, rb_node);
102
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000103 if (em->start < entry->start)
Chris Masona52d9a82007-08-27 16:49:44 -0400104 p = &(*p)->rb_left;
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000105 else if (em->start >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -0400106 p = &(*p)->rb_right;
107 else
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000108 return -EEXIST;
Chris Masona52d9a82007-08-27 16:49:44 -0400109 }
110
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000111 orig_parent = parent;
112 while (parent && em->start >= extent_map_end(entry)) {
113 parent = rb_next(parent);
114 entry = rb_entry(parent, struct extent_map, rb_node);
115 }
116 if (parent)
117 if (end > entry->start && em->start < extent_map_end(entry))
118 return -EEXIST;
119
120 parent = orig_parent;
121 entry = rb_entry(parent, struct extent_map, rb_node);
122 while (parent && em->start < entry->start) {
123 parent = rb_prev(parent);
124 entry = rb_entry(parent, struct extent_map, rb_node);
125 }
126 if (parent)
127 if (end > entry->start && em->start < extent_map_end(entry))
128 return -EEXIST;
129
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000130 rb_link_node(&em->rb_node, orig_parent, p);
131 rb_insert_color(&em->rb_node, root);
132 return 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400133}
134
Chris Masond352ac62008-09-29 15:18:18 -0400135/*
136 * search through the tree for an extent_map with a given offset. If
137 * it can't be found, try to find some neighboring extents
138 */
Chris Masona52d9a82007-08-27 16:49:44 -0400139static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
Chris Mason5f564062008-01-22 16:47:59 -0500140 struct rb_node **prev_ret,
141 struct rb_node **next_ret)
Chris Masona52d9a82007-08-27 16:49:44 -0400142{
Chris Masond3977122009-01-05 21:25:51 -0500143 struct rb_node *n = root->rb_node;
Chris Masona52d9a82007-08-27 16:49:44 -0400144 struct rb_node *prev = NULL;
Chris Mason5f564062008-01-22 16:47:59 -0500145 struct rb_node *orig_prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500146 struct extent_map *entry;
147 struct extent_map *prev_entry = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400148
Chris Masond3977122009-01-05 21:25:51 -0500149 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500150 entry = rb_entry(n, struct extent_map, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400151 prev = n;
152 prev_entry = entry;
153
154 if (offset < entry->start)
155 n = n->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500156 else if (offset >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -0400157 n = n->rb_right;
158 else
159 return n;
160 }
Chris Mason5f564062008-01-22 16:47:59 -0500161
162 if (prev_ret) {
163 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500164 while (prev && offset >= extent_map_end(prev_entry)) {
Chris Mason5f564062008-01-22 16:47:59 -0500165 prev = rb_next(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500166 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500167 }
168 *prev_ret = prev;
169 prev = orig_prev;
Chris Masona52d9a82007-08-27 16:49:44 -0400170 }
Chris Mason5f564062008-01-22 16:47:59 -0500171
172 if (next_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500173 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500174 while (prev && offset < prev_entry->start) {
Chris Mason5f564062008-01-22 16:47:59 -0500175 prev = rb_prev(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500176 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500177 }
178 *next_ret = prev;
179 }
Chris Masona52d9a82007-08-27 16:49:44 -0400180 return NULL;
181}
182
Chris Masond352ac62008-09-29 15:18:18 -0400183/* check to see if two extent_map structs are adjacent and safe to merge */
Chris Masond1310b22008-01-24 16:13:08 -0500184static int mergable_maps(struct extent_map *prev, struct extent_map *next)
Chris Masona52d9a82007-08-27 16:49:44 -0400185{
Chris Mason7f3c74f2008-07-18 12:01:11 -0400186 if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
187 return 0;
188
Chris Masonc8b97812008-10-29 14:49:59 -0400189 /*
190 * don't merge compressed extents, we need to know their
191 * actual size
192 */
193 if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
194 return 0;
195
Josef Bacik201a9032013-01-24 12:02:07 -0500196 if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
197 test_bit(EXTENT_FLAG_LOGGING, &next->flags))
198 return 0;
199
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400200 /*
201 * We don't want to merge stuff that hasn't been written to the log yet
202 * since it may not reflect exactly what is on disk, and that would be
203 * bad.
204 */
205 if (!list_empty(&prev->list) || !list_empty(&next->list))
206 return 0;
207
Chris Masond1310b22008-01-24 16:13:08 -0500208 if (extent_map_end(prev) == next->start &&
209 prev->flags == next->flags &&
210 prev->bdev == next->bdev &&
211 ((next->block_start == EXTENT_MAP_HOLE &&
212 prev->block_start == EXTENT_MAP_HOLE) ||
213 (next->block_start == EXTENT_MAP_INLINE &&
214 prev->block_start == EXTENT_MAP_INLINE) ||
215 (next->block_start == EXTENT_MAP_DELALLOC &&
216 prev->block_start == EXTENT_MAP_DELALLOC) ||
217 (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
218 next->block_start == extent_map_block_end(prev)))) {
219 return 1;
220 }
Chris Masona52d9a82007-08-27 16:49:44 -0400221 return 0;
222}
223
Li Zefan4d2c8f622011-07-14 03:18:33 +0000224static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
Chris Masona1ed8352009-09-11 12:27:37 -0400225{
Chris Masona1ed8352009-09-11 12:27:37 -0400226 struct extent_map *merge = NULL;
227 struct rb_node *rb;
Chris Masona1ed8352009-09-11 12:27:37 -0400228
229 if (em->start != 0) {
230 rb = rb_prev(&em->rb_node);
231 if (rb)
232 merge = rb_entry(rb, struct extent_map, rb_node);
233 if (rb && mergable_maps(merge, em)) {
234 em->start = merge->start;
Josef Bacik70c8a912012-10-11 16:54:30 -0400235 em->orig_start = merge->orig_start;
Chris Masona1ed8352009-09-11 12:27:37 -0400236 em->len += merge->len;
237 em->block_len += merge->block_len;
238 em->block_start = merge->block_start;
Josef Bacik70c8a912012-10-11 16:54:30 -0400239 em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
240 em->mod_start = merge->mod_start;
241 em->generation = max(em->generation, merge->generation);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400242
Chris Masona1ed8352009-09-11 12:27:37 -0400243 rb_erase(&merge->rb_node, &tree->map);
Filipe Mananacbc0e922014-02-25 14:15:12 +0000244 RB_CLEAR_NODE(&merge->rb_node);
Chris Masona1ed8352009-09-11 12:27:37 -0400245 free_extent_map(merge);
246 }
247 }
248
249 rb = rb_next(&em->rb_node);
250 if (rb)
251 merge = rb_entry(rb, struct extent_map, rb_node);
252 if (rb && mergable_maps(em, merge)) {
253 em->len += merge->len;
Filipe David Borba Mananad527afe2013-11-30 11:28:35 +0000254 em->block_len += merge->block_len;
Chris Masona1ed8352009-09-11 12:27:37 -0400255 rb_erase(&merge->rb_node, &tree->map);
Filipe Mananacbc0e922014-02-25 14:15:12 +0000256 RB_CLEAR_NODE(&merge->rb_node);
Josef Bacik70c8a912012-10-11 16:54:30 -0400257 em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
258 em->generation = max(em->generation, merge->generation);
Chris Masona1ed8352009-09-11 12:27:37 -0400259 free_extent_map(merge);
260 }
Li Zefan4d2c8f622011-07-14 03:18:33 +0000261}
262
Josef Bacik5dc562c2012-08-17 13:14:17 -0400263/**
Liu Bo52b1de92012-10-30 17:13:52 +0800264 * unpin_extent_cache - unpin an extent from the cache
Josef Bacik5dc562c2012-08-17 13:14:17 -0400265 * @tree: tree to unpin the extent in
266 * @start: logical offset in the file
267 * @len: length of the extent
268 * @gen: generation that this extent has been modified in
Josef Bacik5dc562c2012-08-17 13:14:17 -0400269 *
270 * Called after an extent has been written to disk properly. Set the generation
271 * to the generation that actually added the file item to the inode so we know
272 * we need to sync this extent when we call fsync().
273 */
274int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
275 u64 gen)
Li Zefan4d2c8f622011-07-14 03:18:33 +0000276{
277 int ret = 0;
278 struct extent_map *em;
Liu Bo4e2f84e2012-08-27 10:52:20 -0600279 bool prealloc = false;
Li Zefan4d2c8f622011-07-14 03:18:33 +0000280
281 write_lock(&tree->lock);
282 em = lookup_extent_mapping(tree, start, len);
283
284 WARN_ON(!em || em->start != start);
285
286 if (!em)
287 goto out;
288
Josef Bacik5dc562c2012-08-17 13:14:17 -0400289 em->generation = gen;
Li Zefan4d2c8f622011-07-14 03:18:33 +0000290 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo4e2f84e2012-08-27 10:52:20 -0600291 em->mod_start = em->start;
292 em->mod_len = em->len;
293
Josef Bacikb11e2342012-12-03 10:58:15 -0500294 if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
Liu Bo4e2f84e2012-08-27 10:52:20 -0600295 prealloc = true;
Josef Bacikb11e2342012-12-03 10:58:15 -0500296 clear_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo4e2f84e2012-08-27 10:52:20 -0600297 }
Li Zefan4d2c8f622011-07-14 03:18:33 +0000298
299 try_merge_map(tree, em);
Liu Bo4e2f84e2012-08-27 10:52:20 -0600300
301 if (prealloc) {
302 em->mod_start = em->start;
303 em->mod_len = em->len;
304 }
305
Chris Masona1ed8352009-09-11 12:27:37 -0400306 free_extent_map(em);
307out:
308 write_unlock(&tree->lock);
309 return ret;
310
311}
312
Josef Bacik201a9032013-01-24 12:02:07 -0500313void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
314{
315 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe Mananacbc0e922014-02-25 14:15:12 +0000316 if (extent_map_in_tree(em))
Josef Bacik222c81d2013-01-28 09:45:20 -0500317 try_merge_map(tree, em);
Josef Bacik201a9032013-01-24 12:02:07 -0500318}
319
Filipe Manana176840b2014-02-25 14:15:13 +0000320static inline void setup_extent_mapping(struct extent_map_tree *tree,
321 struct extent_map *em,
322 int modified)
323{
324 atomic_inc(&em->refs);
325 em->mod_start = em->start;
326 em->mod_len = em->len;
327
328 if (modified)
329 list_move(&em->list, &tree->modified_extents);
330 else
331 try_merge_map(tree, em);
332}
333
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400334/**
335 * add_extent_mapping - add new extent map to the extent tree
336 * @tree: tree to insert new map in
337 * @em: map to insert
338 *
339 * Insert @em into @tree or perform a simple forward/backward merge with
340 * existing mappings. The extent_map struct passed in will be inserted
341 * into the tree directly, with an additional reference taken, or a
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300342 * reference dropped if the merge attempt was successful.
Chris Masona52d9a82007-08-27 16:49:44 -0400343 */
344int add_extent_mapping(struct extent_map_tree *tree,
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400345 struct extent_map *em, int modified)
Chris Masona52d9a82007-08-27 16:49:44 -0400346{
347 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400348
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000349 ret = tree_insert(&tree->map, em);
350 if (ret)
Chris Mason7c2fe322008-08-20 08:51:50 -0400351 goto out;
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000352
Filipe Manana176840b2014-02-25 14:15:13 +0000353 setup_extent_mapping(tree, em, modified);
Chris Masona52d9a82007-08-27 16:49:44 -0400354out:
Chris Masona52d9a82007-08-27 16:49:44 -0400355 return ret;
356}
Chris Masona52d9a82007-08-27 16:49:44 -0400357
Eric Sandeen48a3b632013-04-25 20:41:01 +0000358static struct extent_map *
359__lookup_extent_mapping(struct extent_map_tree *tree,
360 u64 start, u64 len, int strict)
Li Zefaned64f062011-07-14 03:18:15 +0000361{
362 struct extent_map *em;
363 struct rb_node *rb_node;
364 struct rb_node *prev = NULL;
365 struct rb_node *next = NULL;
366 u64 end = range_end(start, len);
367
368 rb_node = __tree_search(&tree->map, start, &prev, &next);
369 if (!rb_node) {
370 if (prev)
371 rb_node = prev;
372 else if (next)
373 rb_node = next;
374 else
375 return NULL;
376 }
377
378 em = rb_entry(rb_node, struct extent_map, rb_node);
379
380 if (strict && !(end > em->start && start < extent_map_end(em)))
381 return NULL;
382
383 atomic_inc(&em->refs);
384 return em;
385}
386
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400387/**
388 * lookup_extent_mapping - lookup extent_map
389 * @tree: tree to lookup in
390 * @start: byte offset to start the search
391 * @len: length of the lookup range
392 *
393 * Find and return the first extent_map struct in @tree that intersects the
394 * [start, len] range. There may be additional objects in the tree that
395 * intersect, so check the object returned carefully to make sure that no
396 * additional lookups are needed.
Chris Masona52d9a82007-08-27 16:49:44 -0400397 */
398struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500399 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -0400400{
Li Zefaned64f062011-07-14 03:18:15 +0000401 return __lookup_extent_mapping(tree, start, len, 1);
Chris Masona52d9a82007-08-27 16:49:44 -0400402}
Chris Masona52d9a82007-08-27 16:49:44 -0400403
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400404/**
Chris Masonb917b7c2009-09-18 16:07:03 -0400405 * search_extent_mapping - find a nearby extent map
406 * @tree: tree to lookup in
407 * @start: byte offset to start the search
408 * @len: length of the lookup range
409 *
410 * Find and return the first extent_map struct in @tree that intersects the
411 * [start, len] range.
412 *
413 * If one can't be found, any nearby extent may be returned
414 */
415struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
416 u64 start, u64 len)
417{
Li Zefaned64f062011-07-14 03:18:15 +0000418 return __lookup_extent_mapping(tree, start, len, 0);
Chris Masonb917b7c2009-09-18 16:07:03 -0400419}
420
421/**
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400422 * remove_extent_mapping - removes an extent_map from the extent tree
423 * @tree: extent tree to remove from
424 * @em: extent map beeing removed
425 *
426 * Removes @em from @tree. No reference counts are dropped, and no checks
427 * are done to see if the range is in use
Chris Masona52d9a82007-08-27 16:49:44 -0400428 */
429int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
430{
Chris Masond1310b22008-01-24 16:13:08 -0500431 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400432
Chris Mason7f3c74f2008-07-18 12:01:11 -0400433 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
Chris Masond1310b22008-01-24 16:13:08 -0500434 rb_erase(&em->rb_node, &tree->map);
Josef Bacikff44c6e2012-09-14 12:59:20 -0400435 if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
436 list_del_init(&em->list);
Filipe Mananacbc0e922014-02-25 14:15:12 +0000437 RB_CLEAR_NODE(&em->rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400438 return ret;
439}
Filipe Manana176840b2014-02-25 14:15:13 +0000440
441void replace_extent_mapping(struct extent_map_tree *tree,
442 struct extent_map *cur,
443 struct extent_map *new,
444 int modified)
445{
446 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));
447 ASSERT(extent_map_in_tree(cur));
448 if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))
449 list_del_init(&cur->list);
450 rb_replace_node(&cur->rb_node, &new->rb_node, &tree->map);
451 RB_CLEAR_NODE(&cur->rb_node);
452
453 setup_extent_mapping(tree, new, modified);
454}