blob: 1874aee69c86391e6b8a70eb3931a8a824e3da65 [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{
Chris Masona52d9a82007-08-27 16:49:44 -040023 if (extent_map_cache)
24 kmem_cache_destroy(extent_map_cache);
Chris Masona52d9a82007-08-27 16:49:44 -040025}
26
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040027/**
28 * extent_map_tree_init - initialize extent map tree
29 * @tree: tree to initialize
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040030 *
31 * Initialize the extent tree @tree. Should be called for each new inode
32 * or other user of the extent_map interface.
33 */
David Sterbaa8067e02011-04-21 00:34:43 +020034void extent_map_tree_init(struct extent_map_tree *tree)
Chris Masona52d9a82007-08-27 16:49:44 -040035{
Eric Paris6bef4d32010-02-23 19:43:04 +000036 tree->map = RB_ROOT;
Josef Bacik5dc562c2012-08-17 13:14:17 -040037 INIT_LIST_HEAD(&tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -040038 rwlock_init(&tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -040039}
Chris Masona52d9a82007-08-27 16:49:44 -040040
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040041/**
42 * alloc_extent_map - allocate new extent map structure
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040043 *
44 * Allocate a new extent_map structure. The new structure is
45 * returned with a reference count of one and needs to be
46 * freed using free_extent_map()
47 */
David Sterba172ddd62011-04-21 00:48:27 +020048struct extent_map *alloc_extent_map(void)
Chris Masona52d9a82007-08-27 16:49:44 -040049{
50 struct extent_map *em;
Josef Bacik70c8a912012-10-11 16:54:30 -040051 em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +000052 if (!em)
53 return NULL;
Filipe Mananacbc0e922014-02-25 14:15:12 +000054 RB_CLEAR_NODE(&em->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -050055 em->flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +080056 em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik5dc562c2012-08-17 13:14:17 -040057 em->generation = 0;
Chris Masona52d9a82007-08-27 16:49:44 -040058 atomic_set(&em->refs, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040059 INIT_LIST_HEAD(&em->list);
Chris Masona52d9a82007-08-27 16:49:44 -040060 return em;
61}
Chris Masona52d9a82007-08-27 16:49:44 -040062
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040063/**
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 Masona52d9a82007-08-27 16:49:44 -040070void free_extent_map(struct extent_map *em)
71{
Chris Mason2bf5a722007-08-30 11:54:02 -040072 if (!em)
73 return;
Chris Masond1310b22008-01-24 16:13:08 -050074 WARN_ON(atomic_read(&em->refs) == 0);
Chris Masona52d9a82007-08-27 16:49:44 -040075 if (atomic_dec_and_test(&em->refs)) {
Filipe Mananacbc0e922014-02-25 14:15:12 +000076 WARN_ON(extent_map_in_tree(em));
Josef Bacik5dc562c2012-08-17 13:14:17 -040077 WARN_ON(!list_empty(&em->list));
Chris Masona52d9a82007-08-27 16:49:44 -040078 kmem_cache_free(extent_map_cache, em);
79 }
80}
Chris Masona52d9a82007-08-27 16:49:44 -040081
Filipe David Borba Manana32193c12013-11-25 03:23:51 +000082/* simple helper to do math around the end of an extent, handling wrap */
83static u64 range_end(u64 start, u64 len)
84{
85 if (start + len < start)
86 return (u64)-1;
87 return start + len;
88}
89
90static int tree_insert(struct rb_root *root, struct extent_map *em)
Chris Masona52d9a82007-08-27 16:49:44 -040091{
Chris Masond3977122009-01-05 21:25:51 -050092 struct rb_node **p = &root->rb_node;
93 struct rb_node *parent = NULL;
Filipe David Borba Manana32193c12013-11-25 03:23:51 +000094 struct extent_map *entry = NULL;
95 struct rb_node *orig_parent = NULL;
96 u64 end = range_end(em->start, em->len);
Chris Masona52d9a82007-08-27 16:49:44 -040097
Chris Masond3977122009-01-05 21:25:51 -050098 while (*p) {
Chris Masona52d9a82007-08-27 16:49:44 -040099 parent = *p;
Chris Masond1310b22008-01-24 16:13:08 -0500100 entry = rb_entry(parent, struct extent_map, rb_node);
101
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000102 if (em->start < entry->start)
Chris Masona52d9a82007-08-27 16:49:44 -0400103 p = &(*p)->rb_left;
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000104 else if (em->start >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -0400105 p = &(*p)->rb_right;
106 else
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000107 return -EEXIST;
Chris Masona52d9a82007-08-27 16:49:44 -0400108 }
109
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000110 orig_parent = parent;
111 while (parent && em->start >= extent_map_end(entry)) {
112 parent = rb_next(parent);
113 entry = rb_entry(parent, struct extent_map, rb_node);
114 }
115 if (parent)
116 if (end > entry->start && em->start < extent_map_end(entry))
117 return -EEXIST;
118
119 parent = orig_parent;
120 entry = rb_entry(parent, struct extent_map, rb_node);
121 while (parent && em->start < entry->start) {
122 parent = rb_prev(parent);
123 entry = rb_entry(parent, struct extent_map, rb_node);
124 }
125 if (parent)
126 if (end > entry->start && em->start < extent_map_end(entry))
127 return -EEXIST;
128
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000129 rb_link_node(&em->rb_node, orig_parent, p);
130 rb_insert_color(&em->rb_node, root);
131 return 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400132}
133
Chris Masond352ac62008-09-29 15:18:18 -0400134/*
135 * search through the tree for an extent_map with a given offset. If
136 * it can't be found, try to find some neighboring extents
137 */
Chris Masona52d9a82007-08-27 16:49:44 -0400138static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
Chris Mason5f564062008-01-22 16:47:59 -0500139 struct rb_node **prev_ret,
140 struct rb_node **next_ret)
Chris Masona52d9a82007-08-27 16:49:44 -0400141{
Chris Masond3977122009-01-05 21:25:51 -0500142 struct rb_node *n = root->rb_node;
Chris Masona52d9a82007-08-27 16:49:44 -0400143 struct rb_node *prev = NULL;
Chris Mason5f564062008-01-22 16:47:59 -0500144 struct rb_node *orig_prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500145 struct extent_map *entry;
146 struct extent_map *prev_entry = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400147
Chris Masond3977122009-01-05 21:25:51 -0500148 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500149 entry = rb_entry(n, struct extent_map, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400150 prev = n;
151 prev_entry = entry;
152
153 if (offset < entry->start)
154 n = n->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500155 else if (offset >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -0400156 n = n->rb_right;
157 else
158 return n;
159 }
Chris Mason5f564062008-01-22 16:47:59 -0500160
161 if (prev_ret) {
162 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500163 while (prev && offset >= extent_map_end(prev_entry)) {
Chris Mason5f564062008-01-22 16:47:59 -0500164 prev = rb_next(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500165 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500166 }
167 *prev_ret = prev;
168 prev = orig_prev;
Chris Masona52d9a82007-08-27 16:49:44 -0400169 }
Chris Mason5f564062008-01-22 16:47:59 -0500170
171 if (next_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500172 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500173 while (prev && offset < prev_entry->start) {
Chris Mason5f564062008-01-22 16:47:59 -0500174 prev = rb_prev(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500175 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500176 }
177 *next_ret = prev;
178 }
Chris Masona52d9a82007-08-27 16:49:44 -0400179 return NULL;
180}
181
Chris Masond352ac62008-09-29 15:18:18 -0400182/* check to see if two extent_map structs are adjacent and safe to merge */
Chris Masond1310b22008-01-24 16:13:08 -0500183static int mergable_maps(struct extent_map *prev, struct extent_map *next)
Chris Masona52d9a82007-08-27 16:49:44 -0400184{
Chris Mason7f3c74f2008-07-18 12:01:11 -0400185 if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
186 return 0;
187
Chris Masonc8b97812008-10-29 14:49:59 -0400188 /*
189 * don't merge compressed extents, we need to know their
190 * actual size
191 */
192 if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
193 return 0;
194
Josef Bacik201a9032013-01-24 12:02:07 -0500195 if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
196 test_bit(EXTENT_FLAG_LOGGING, &next->flags))
197 return 0;
198
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400199 /*
200 * We don't want to merge stuff that hasn't been written to the log yet
201 * since it may not reflect exactly what is on disk, and that would be
202 * bad.
203 */
204 if (!list_empty(&prev->list) || !list_empty(&next->list))
205 return 0;
206
Chris Masond1310b22008-01-24 16:13:08 -0500207 if (extent_map_end(prev) == next->start &&
208 prev->flags == next->flags &&
209 prev->bdev == next->bdev &&
210 ((next->block_start == EXTENT_MAP_HOLE &&
211 prev->block_start == EXTENT_MAP_HOLE) ||
212 (next->block_start == EXTENT_MAP_INLINE &&
213 prev->block_start == EXTENT_MAP_INLINE) ||
214 (next->block_start == EXTENT_MAP_DELALLOC &&
215 prev->block_start == EXTENT_MAP_DELALLOC) ||
216 (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
217 next->block_start == extent_map_block_end(prev)))) {
218 return 1;
219 }
Chris Masona52d9a82007-08-27 16:49:44 -0400220 return 0;
221}
222
Li Zefan4d2c8f622011-07-14 03:18:33 +0000223static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
Chris Masona1ed8352009-09-11 12:27:37 -0400224{
Chris Masona1ed8352009-09-11 12:27:37 -0400225 struct extent_map *merge = NULL;
226 struct rb_node *rb;
Chris Masona1ed8352009-09-11 12:27:37 -0400227
228 if (em->start != 0) {
229 rb = rb_prev(&em->rb_node);
230 if (rb)
231 merge = rb_entry(rb, struct extent_map, rb_node);
232 if (rb && mergable_maps(merge, em)) {
233 em->start = merge->start;
Josef Bacik70c8a912012-10-11 16:54:30 -0400234 em->orig_start = merge->orig_start;
Chris Masona1ed8352009-09-11 12:27:37 -0400235 em->len += merge->len;
236 em->block_len += merge->block_len;
237 em->block_start = merge->block_start;
Josef Bacik70c8a912012-10-11 16:54:30 -0400238 em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
239 em->mod_start = merge->mod_start;
240 em->generation = max(em->generation, merge->generation);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400241
Chris Masona1ed8352009-09-11 12:27:37 -0400242 rb_erase(&merge->rb_node, &tree->map);
Filipe Mananacbc0e922014-02-25 14:15:12 +0000243 RB_CLEAR_NODE(&merge->rb_node);
Chris Masona1ed8352009-09-11 12:27:37 -0400244 free_extent_map(merge);
245 }
246 }
247
248 rb = rb_next(&em->rb_node);
249 if (rb)
250 merge = rb_entry(rb, struct extent_map, rb_node);
251 if (rb && mergable_maps(em, merge)) {
252 em->len += merge->len;
Filipe David Borba Mananad527afe2013-11-30 11:28:35 +0000253 em->block_len += merge->block_len;
Chris Masona1ed8352009-09-11 12:27:37 -0400254 rb_erase(&merge->rb_node, &tree->map);
Filipe Mananacbc0e922014-02-25 14:15:12 +0000255 RB_CLEAR_NODE(&merge->rb_node);
Josef Bacik70c8a912012-10-11 16:54:30 -0400256 em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
257 em->generation = max(em->generation, merge->generation);
Chris Masona1ed8352009-09-11 12:27:37 -0400258 free_extent_map(merge);
259 }
Li Zefan4d2c8f622011-07-14 03:18:33 +0000260}
261
Josef Bacik5dc562c2012-08-17 13:14:17 -0400262/**
Liu Bo52b1de92012-10-30 17:13:52 +0800263 * unpin_extent_cache - unpin an extent from the cache
Josef Bacik5dc562c2012-08-17 13:14:17 -0400264 * @tree: tree to unpin the extent in
265 * @start: logical offset in the file
266 * @len: length of the extent
267 * @gen: generation that this extent has been modified in
Josef Bacik5dc562c2012-08-17 13:14:17 -0400268 *
269 * Called after an extent has been written to disk properly. Set the generation
270 * to the generation that actually added the file item to the inode so we know
271 * we need to sync this extent when we call fsync().
272 */
273int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
274 u64 gen)
Li Zefan4d2c8f622011-07-14 03:18:33 +0000275{
276 int ret = 0;
277 struct extent_map *em;
Liu Bo4e2f84e2012-08-27 10:52:20 -0600278 bool prealloc = false;
Li Zefan4d2c8f622011-07-14 03:18:33 +0000279
280 write_lock(&tree->lock);
281 em = lookup_extent_mapping(tree, start, len);
282
283 WARN_ON(!em || em->start != start);
284
285 if (!em)
286 goto out;
287
Josef Bacik201a9032013-01-24 12:02:07 -0500288 if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
289 list_move(&em->list, &tree->modified_extents);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400290 em->generation = gen;
Li Zefan4d2c8f622011-07-14 03:18:33 +0000291 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo4e2f84e2012-08-27 10:52:20 -0600292 em->mod_start = em->start;
293 em->mod_len = em->len;
294
Josef Bacikb11e2342012-12-03 10:58:15 -0500295 if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
Liu Bo4e2f84e2012-08-27 10:52:20 -0600296 prealloc = true;
Josef Bacikb11e2342012-12-03 10:58:15 -0500297 clear_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo4e2f84e2012-08-27 10:52:20 -0600298 }
Li Zefan4d2c8f622011-07-14 03:18:33 +0000299
300 try_merge_map(tree, em);
Liu Bo4e2f84e2012-08-27 10:52:20 -0600301
302 if (prealloc) {
303 em->mod_start = em->start;
304 em->mod_len = em->len;
305 }
306
Chris Masona1ed8352009-09-11 12:27:37 -0400307 free_extent_map(em);
308out:
309 write_unlock(&tree->lock);
310 return ret;
311
312}
313
Josef Bacik201a9032013-01-24 12:02:07 -0500314void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
315{
316 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe Mananacbc0e922014-02-25 14:15:12 +0000317 if (extent_map_in_tree(em))
Josef Bacik222c81d2013-01-28 09:45:20 -0500318 try_merge_map(tree, em);
Josef Bacik201a9032013-01-24 12:02:07 -0500319}
320
Filipe Manana176840b2014-02-25 14:15:13 +0000321static inline void setup_extent_mapping(struct extent_map_tree *tree,
322 struct extent_map *em,
323 int modified)
324{
325 atomic_inc(&em->refs);
326 em->mod_start = em->start;
327 em->mod_len = em->len;
328
329 if (modified)
330 list_move(&em->list, &tree->modified_extents);
331 else
332 try_merge_map(tree, em);
333}
334
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400335/**
336 * add_extent_mapping - add new extent map to the extent tree
337 * @tree: tree to insert new map in
338 * @em: map to insert
339 *
340 * Insert @em into @tree or perform a simple forward/backward merge with
341 * existing mappings. The extent_map struct passed in will be inserted
342 * into the tree directly, with an additional reference taken, or a
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300343 * reference dropped if the merge attempt was successful.
Chris Masona52d9a82007-08-27 16:49:44 -0400344 */
345int add_extent_mapping(struct extent_map_tree *tree,
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400346 struct extent_map *em, int modified)
Chris Masona52d9a82007-08-27 16:49:44 -0400347{
348 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400349
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000350 ret = tree_insert(&tree->map, em);
351 if (ret)
Chris Mason7c2fe322008-08-20 08:51:50 -0400352 goto out;
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000353
Filipe Manana176840b2014-02-25 14:15:13 +0000354 setup_extent_mapping(tree, em, modified);
Chris Masona52d9a82007-08-27 16:49:44 -0400355out:
Chris Masona52d9a82007-08-27 16:49:44 -0400356 return ret;
357}
Chris Masona52d9a82007-08-27 16:49:44 -0400358
Eric Sandeen48a3b632013-04-25 20:41:01 +0000359static struct extent_map *
360__lookup_extent_mapping(struct extent_map_tree *tree,
361 u64 start, u64 len, int strict)
Li Zefaned64f062011-07-14 03:18:15 +0000362{
363 struct extent_map *em;
364 struct rb_node *rb_node;
365 struct rb_node *prev = NULL;
366 struct rb_node *next = NULL;
367 u64 end = range_end(start, len);
368
369 rb_node = __tree_search(&tree->map, start, &prev, &next);
370 if (!rb_node) {
371 if (prev)
372 rb_node = prev;
373 else if (next)
374 rb_node = next;
375 else
376 return NULL;
377 }
378
379 em = rb_entry(rb_node, struct extent_map, rb_node);
380
381 if (strict && !(end > em->start && start < extent_map_end(em)))
382 return NULL;
383
384 atomic_inc(&em->refs);
385 return em;
386}
387
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400388/**
389 * lookup_extent_mapping - lookup extent_map
390 * @tree: tree to lookup in
391 * @start: byte offset to start the search
392 * @len: length of the lookup range
393 *
394 * Find and return the first extent_map struct in @tree that intersects the
395 * [start, len] range. There may be additional objects in the tree that
396 * intersect, so check the object returned carefully to make sure that no
397 * additional lookups are needed.
Chris Masona52d9a82007-08-27 16:49:44 -0400398 */
399struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500400 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -0400401{
Li Zefaned64f062011-07-14 03:18:15 +0000402 return __lookup_extent_mapping(tree, start, len, 1);
Chris Masona52d9a82007-08-27 16:49:44 -0400403}
Chris Masona52d9a82007-08-27 16:49:44 -0400404
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400405/**
Chris Masonb917b7c2009-09-18 16:07:03 -0400406 * search_extent_mapping - find a nearby extent map
407 * @tree: tree to lookup in
408 * @start: byte offset to start the search
409 * @len: length of the lookup range
410 *
411 * Find and return the first extent_map struct in @tree that intersects the
412 * [start, len] range.
413 *
414 * If one can't be found, any nearby extent may be returned
415 */
416struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
417 u64 start, u64 len)
418{
Li Zefaned64f062011-07-14 03:18:15 +0000419 return __lookup_extent_mapping(tree, start, len, 0);
Chris Masonb917b7c2009-09-18 16:07:03 -0400420}
421
422/**
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400423 * remove_extent_mapping - removes an extent_map from the extent tree
424 * @tree: extent tree to remove from
425 * @em: extent map beeing removed
426 *
427 * Removes @em from @tree. No reference counts are dropped, and no checks
428 * are done to see if the range is in use
Chris Masona52d9a82007-08-27 16:49:44 -0400429 */
430int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
431{
Chris Masond1310b22008-01-24 16:13:08 -0500432 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400433
Chris Mason7f3c74f2008-07-18 12:01:11 -0400434 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
Chris Masond1310b22008-01-24 16:13:08 -0500435 rb_erase(&em->rb_node, &tree->map);
Josef Bacikff44c6e2012-09-14 12:59:20 -0400436 if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
437 list_del_init(&em->list);
Filipe Mananacbc0e922014-02-25 14:15:12 +0000438 RB_CLEAR_NODE(&em->rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400439 return ret;
440}
Filipe Manana176840b2014-02-25 14:15:13 +0000441
442void replace_extent_mapping(struct extent_map_tree *tree,
443 struct extent_map *cur,
444 struct extent_map *new,
445 int modified)
446{
447 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));
448 ASSERT(extent_map_in_tree(cur));
449 if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))
450 list_del_init(&cur->list);
451 rb_replace_node(&cur->rb_node, &new->rb_node, &tree->map);
452 RB_CLEAR_NODE(&cur->rb_node);
453
454 setup_extent_mapping(tree, new, modified);
455}