blob: 4f59b4089eb01a3f870992d1e516550019e0f1a1 [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"
Anand Jainebb87652016-03-10 17:26:59 +08007#include "compression.h"
Chris Masona52d9a82007-08-27 16:49:44 -04008
Chris Mason86479a02007-09-10 19:58:16 -04009
Chris Masona52d9a82007-08-27 16:49:44 -040010static struct kmem_cache *extent_map_cache;
Chris Masonca664622007-11-27 11:16:35 -050011
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050012int __init extent_map_init(void)
Chris Masona52d9a82007-08-27 16:49:44 -040013{
David Sterba837e1972012-09-07 03:00:48 -060014 extent_map_cache = kmem_cache_create("btrfs_extent_map",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020015 sizeof(struct extent_map), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +030016 SLAB_MEM_SPREAD, NULL);
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050017 if (!extent_map_cache)
18 return -ENOMEM;
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050019 return 0;
Chris Masona52d9a82007-08-27 16:49:44 -040020}
21
Christian Hesse17636e02007-12-11 09:25:06 -050022void extent_map_exit(void)
Chris Masona52d9a82007-08-27 16:49:44 -040023{
Kinglong Mee5598e902016-01-29 21:36:35 +080024 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
Nicholas D Steeves01327612016-05-19 21:18:45 -040065 * @em: extent map being released
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040066 *
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));
Wang Shilong298a8f92014-06-19 10:42:52 +080078 if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
Jeff Mahoney95617d62015-06-03 10:55:48 -040079 kfree(em->map_lookup);
Chris Masona52d9a82007-08-27 16:49:44 -040080 kmem_cache_free(extent_map_cache, em);
81 }
82}
Chris Masona52d9a82007-08-27 16:49:44 -040083
Filipe David Borba Manana32193c12013-11-25 03:23:51 +000084/* simple helper to do math around the end of an extent, handling wrap */
85static u64 range_end(u64 start, u64 len)
86{
87 if (start + len < start)
88 return (u64)-1;
89 return start + len;
90}
91
92static int tree_insert(struct rb_root *root, struct extent_map *em)
Chris Masona52d9a82007-08-27 16:49:44 -040093{
Chris Masond3977122009-01-05 21:25:51 -050094 struct rb_node **p = &root->rb_node;
95 struct rb_node *parent = NULL;
Filipe David Borba Manana32193c12013-11-25 03:23:51 +000096 struct extent_map *entry = NULL;
97 struct rb_node *orig_parent = NULL;
98 u64 end = range_end(em->start, em->len);
Chris Masona52d9a82007-08-27 16:49:44 -040099
Chris Masond3977122009-01-05 21:25:51 -0500100 while (*p) {
Chris Masona52d9a82007-08-27 16:49:44 -0400101 parent = *p;
Chris Masond1310b22008-01-24 16:13:08 -0500102 entry = rb_entry(parent, struct extent_map, rb_node);
103
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000104 if (em->start < entry->start)
Chris Masona52d9a82007-08-27 16:49:44 -0400105 p = &(*p)->rb_left;
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000106 else if (em->start >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -0400107 p = &(*p)->rb_right;
108 else
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000109 return -EEXIST;
Chris Masona52d9a82007-08-27 16:49:44 -0400110 }
111
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000112 orig_parent = parent;
113 while (parent && em->start >= extent_map_end(entry)) {
114 parent = rb_next(parent);
115 entry = rb_entry(parent, struct extent_map, rb_node);
116 }
117 if (parent)
118 if (end > entry->start && em->start < extent_map_end(entry))
119 return -EEXIST;
120
121 parent = orig_parent;
122 entry = rb_entry(parent, struct extent_map, rb_node);
123 while (parent && em->start < entry->start) {
124 parent = rb_prev(parent);
125 entry = rb_entry(parent, struct extent_map, rb_node);
126 }
127 if (parent)
128 if (end > entry->start && em->start < extent_map_end(entry))
129 return -EEXIST;
130
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000131 rb_link_node(&em->rb_node, orig_parent, p);
132 rb_insert_color(&em->rb_node, root);
133 return 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400134}
135
Chris Masond352ac62008-09-29 15:18:18 -0400136/*
137 * search through the tree for an extent_map with a given offset. If
138 * it can't be found, try to find some neighboring extents
139 */
Chris Masona52d9a82007-08-27 16:49:44 -0400140static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
Chris Mason5f564062008-01-22 16:47:59 -0500141 struct rb_node **prev_ret,
142 struct rb_node **next_ret)
Chris Masona52d9a82007-08-27 16:49:44 -0400143{
Chris Masond3977122009-01-05 21:25:51 -0500144 struct rb_node *n = root->rb_node;
Chris Masona52d9a82007-08-27 16:49:44 -0400145 struct rb_node *prev = NULL;
Chris Mason5f564062008-01-22 16:47:59 -0500146 struct rb_node *orig_prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500147 struct extent_map *entry;
148 struct extent_map *prev_entry = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400149
Chris Masond3977122009-01-05 21:25:51 -0500150 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500151 entry = rb_entry(n, struct extent_map, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400152 prev = n;
153 prev_entry = entry;
154
155 if (offset < entry->start)
156 n = n->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500157 else if (offset >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -0400158 n = n->rb_right;
159 else
160 return n;
161 }
Chris Mason5f564062008-01-22 16:47:59 -0500162
163 if (prev_ret) {
164 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500165 while (prev && offset >= extent_map_end(prev_entry)) {
Chris Mason5f564062008-01-22 16:47:59 -0500166 prev = rb_next(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500167 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500168 }
169 *prev_ret = prev;
170 prev = orig_prev;
Chris Masona52d9a82007-08-27 16:49:44 -0400171 }
Chris Mason5f564062008-01-22 16:47:59 -0500172
173 if (next_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500174 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500175 while (prev && offset < prev_entry->start) {
Chris Mason5f564062008-01-22 16:47:59 -0500176 prev = rb_prev(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500177 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500178 }
179 *next_ret = prev;
180 }
Chris Masona52d9a82007-08-27 16:49:44 -0400181 return NULL;
182}
183
Chris Masond352ac62008-09-29 15:18:18 -0400184/* check to see if two extent_map structs are adjacent and safe to merge */
Chris Masond1310b22008-01-24 16:13:08 -0500185static int mergable_maps(struct extent_map *prev, struct extent_map *next)
Chris Masona52d9a82007-08-27 16:49:44 -0400186{
Chris Mason7f3c74f2008-07-18 12:01:11 -0400187 if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
188 return 0;
189
Chris Masonc8b97812008-10-29 14:49:59 -0400190 /*
191 * don't merge compressed extents, we need to know their
192 * actual size
193 */
194 if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
195 return 0;
196
Josef Bacik201a9032013-01-24 12:02:07 -0500197 if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
198 test_bit(EXTENT_FLAG_LOGGING, &next->flags))
199 return 0;
200
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400201 /*
202 * We don't want to merge stuff that hasn't been written to the log yet
203 * since it may not reflect exactly what is on disk, and that would be
204 * bad.
205 */
206 if (!list_empty(&prev->list) || !list_empty(&next->list))
207 return 0;
208
Chris Masond1310b22008-01-24 16:13:08 -0500209 if (extent_map_end(prev) == next->start &&
210 prev->flags == next->flags &&
211 prev->bdev == next->bdev &&
212 ((next->block_start == EXTENT_MAP_HOLE &&
213 prev->block_start == EXTENT_MAP_HOLE) ||
214 (next->block_start == EXTENT_MAP_INLINE &&
215 prev->block_start == EXTENT_MAP_INLINE) ||
216 (next->block_start == EXTENT_MAP_DELALLOC &&
217 prev->block_start == EXTENT_MAP_DELALLOC) ||
218 (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
219 next->block_start == extent_map_block_end(prev)))) {
220 return 1;
221 }
Chris Masona52d9a82007-08-27 16:49:44 -0400222 return 0;
223}
224
Li Zefan4d2c8f622011-07-14 03:18:33 +0000225static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
Chris Masona1ed8352009-09-11 12:27:37 -0400226{
Chris Masona1ed8352009-09-11 12:27:37 -0400227 struct extent_map *merge = NULL;
228 struct rb_node *rb;
Chris Masona1ed8352009-09-11 12:27:37 -0400229
Filipe Manana3e507152020-01-31 14:06:07 +0000230 /*
231 * We can't modify an extent map that is in the tree and that is being
232 * used by another task, as it can cause that other task to see it in
233 * inconsistent state during the merging. We always have 1 reference for
234 * the tree and 1 for this task (which is unpinning the extent map or
235 * clearing the logging flag), so anything > 2 means it's being used by
236 * other tasks too.
237 */
238 if (atomic_read(&em->refs) > 2)
239 return;
240
Chris Masona1ed8352009-09-11 12:27:37 -0400241 if (em->start != 0) {
242 rb = rb_prev(&em->rb_node);
243 if (rb)
244 merge = rb_entry(rb, struct extent_map, rb_node);
245 if (rb && mergable_maps(merge, em)) {
246 em->start = merge->start;
Josef Bacik70c8a912012-10-11 16:54:30 -0400247 em->orig_start = merge->orig_start;
Chris Masona1ed8352009-09-11 12:27:37 -0400248 em->len += merge->len;
249 em->block_len += merge->block_len;
250 em->block_start = merge->block_start;
Josef Bacik70c8a912012-10-11 16:54:30 -0400251 em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
252 em->mod_start = merge->mod_start;
253 em->generation = max(em->generation, merge->generation);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400254
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);
Chris Masona1ed8352009-09-11 12:27:37 -0400257 free_extent_map(merge);
258 }
259 }
260
261 rb = rb_next(&em->rb_node);
262 if (rb)
263 merge = rb_entry(rb, struct extent_map, rb_node);
264 if (rb && mergable_maps(em, merge)) {
265 em->len += merge->len;
Filipe David Borba Mananad527afe2013-11-30 11:28:35 +0000266 em->block_len += merge->block_len;
Chris Masona1ed8352009-09-11 12:27:37 -0400267 rb_erase(&merge->rb_node, &tree->map);
Filipe Mananacbc0e922014-02-25 14:15:12 +0000268 RB_CLEAR_NODE(&merge->rb_node);
Josef Bacik70c8a912012-10-11 16:54:30 -0400269 em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
270 em->generation = max(em->generation, merge->generation);
Chris Masona1ed8352009-09-11 12:27:37 -0400271 free_extent_map(merge);
272 }
Li Zefan4d2c8f622011-07-14 03:18:33 +0000273}
274
Josef Bacik5dc562c2012-08-17 13:14:17 -0400275/**
Liu Bo52b1de92012-10-30 17:13:52 +0800276 * unpin_extent_cache - unpin an extent from the cache
Josef Bacik5dc562c2012-08-17 13:14:17 -0400277 * @tree: tree to unpin the extent in
278 * @start: logical offset in the file
279 * @len: length of the extent
280 * @gen: generation that this extent has been modified in
Josef Bacik5dc562c2012-08-17 13:14:17 -0400281 *
282 * Called after an extent has been written to disk properly. Set the generation
283 * to the generation that actually added the file item to the inode so we know
284 * we need to sync this extent when we call fsync().
285 */
286int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
287 u64 gen)
Li Zefan4d2c8f622011-07-14 03:18:33 +0000288{
289 int ret = 0;
290 struct extent_map *em;
Liu Bo4e2f84e2012-08-27 10:52:20 -0600291 bool prealloc = false;
Li Zefan4d2c8f622011-07-14 03:18:33 +0000292
293 write_lock(&tree->lock);
294 em = lookup_extent_mapping(tree, start, len);
295
296 WARN_ON(!em || em->start != start);
297
298 if (!em)
299 goto out;
300
Josef Bacik5dc562c2012-08-17 13:14:17 -0400301 em->generation = gen;
Li Zefan4d2c8f622011-07-14 03:18:33 +0000302 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo4e2f84e2012-08-27 10:52:20 -0600303 em->mod_start = em->start;
304 em->mod_len = em->len;
305
Josef Bacikb11e2342012-12-03 10:58:15 -0500306 if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
Liu Bo4e2f84e2012-08-27 10:52:20 -0600307 prealloc = true;
Josef Bacikb11e2342012-12-03 10:58:15 -0500308 clear_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo4e2f84e2012-08-27 10:52:20 -0600309 }
Li Zefan4d2c8f622011-07-14 03:18:33 +0000310
311 try_merge_map(tree, em);
Liu Bo4e2f84e2012-08-27 10:52:20 -0600312
313 if (prealloc) {
314 em->mod_start = em->start;
315 em->mod_len = em->len;
316 }
317
Chris Masona1ed8352009-09-11 12:27:37 -0400318 free_extent_map(em);
319out:
320 write_unlock(&tree->lock);
321 return ret;
322
323}
324
Josef Bacik201a9032013-01-24 12:02:07 -0500325void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
326{
327 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe Mananacbc0e922014-02-25 14:15:12 +0000328 if (extent_map_in_tree(em))
Josef Bacik222c81d2013-01-28 09:45:20 -0500329 try_merge_map(tree, em);
Josef Bacik201a9032013-01-24 12:02:07 -0500330}
331
Filipe Manana176840b2014-02-25 14:15:13 +0000332static inline void setup_extent_mapping(struct extent_map_tree *tree,
333 struct extent_map *em,
334 int modified)
335{
336 atomic_inc(&em->refs);
337 em->mod_start = em->start;
338 em->mod_len = em->len;
339
340 if (modified)
341 list_move(&em->list, &tree->modified_extents);
342 else
343 try_merge_map(tree, em);
344}
345
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400346/**
347 * add_extent_mapping - add new extent map to the extent tree
348 * @tree: tree to insert new map in
349 * @em: map to insert
350 *
351 * Insert @em into @tree or perform a simple forward/backward merge with
352 * existing mappings. The extent_map struct passed in will be inserted
353 * into the tree directly, with an additional reference taken, or a
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300354 * reference dropped if the merge attempt was successful.
Chris Masona52d9a82007-08-27 16:49:44 -0400355 */
356int add_extent_mapping(struct extent_map_tree *tree,
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400357 struct extent_map *em, int modified)
Chris Masona52d9a82007-08-27 16:49:44 -0400358{
359 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400360
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000361 ret = tree_insert(&tree->map, em);
362 if (ret)
Chris Mason7c2fe322008-08-20 08:51:50 -0400363 goto out;
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000364
Filipe Manana176840b2014-02-25 14:15:13 +0000365 setup_extent_mapping(tree, em, modified);
Chris Masona52d9a82007-08-27 16:49:44 -0400366out:
Chris Masona52d9a82007-08-27 16:49:44 -0400367 return ret;
368}
Chris Masona52d9a82007-08-27 16:49:44 -0400369
Eric Sandeen48a3b632013-04-25 20:41:01 +0000370static struct extent_map *
371__lookup_extent_mapping(struct extent_map_tree *tree,
372 u64 start, u64 len, int strict)
Li Zefaned64f062011-07-14 03:18:15 +0000373{
374 struct extent_map *em;
375 struct rb_node *rb_node;
376 struct rb_node *prev = NULL;
377 struct rb_node *next = NULL;
378 u64 end = range_end(start, len);
379
380 rb_node = __tree_search(&tree->map, start, &prev, &next);
381 if (!rb_node) {
382 if (prev)
383 rb_node = prev;
384 else if (next)
385 rb_node = next;
386 else
387 return NULL;
388 }
389
390 em = rb_entry(rb_node, struct extent_map, rb_node);
391
392 if (strict && !(end > em->start && start < extent_map_end(em)))
393 return NULL;
394
395 atomic_inc(&em->refs);
396 return em;
397}
398
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400399/**
400 * lookup_extent_mapping - lookup extent_map
401 * @tree: tree to lookup in
402 * @start: byte offset to start the search
403 * @len: length of the lookup range
404 *
405 * Find and return the first extent_map struct in @tree that intersects the
406 * [start, len] range. There may be additional objects in the tree that
407 * intersect, so check the object returned carefully to make sure that no
408 * additional lookups are needed.
Chris Masona52d9a82007-08-27 16:49:44 -0400409 */
410struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500411 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -0400412{
Li Zefaned64f062011-07-14 03:18:15 +0000413 return __lookup_extent_mapping(tree, start, len, 1);
Chris Masona52d9a82007-08-27 16:49:44 -0400414}
Chris Masona52d9a82007-08-27 16:49:44 -0400415
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400416/**
Chris Masonb917b7c2009-09-18 16:07:03 -0400417 * search_extent_mapping - find a nearby extent map
418 * @tree: tree to lookup in
419 * @start: byte offset to start the search
420 * @len: length of the lookup range
421 *
422 * Find and return the first extent_map struct in @tree that intersects the
423 * [start, len] range.
424 *
425 * If one can't be found, any nearby extent may be returned
426 */
427struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
428 u64 start, u64 len)
429{
Li Zefaned64f062011-07-14 03:18:15 +0000430 return __lookup_extent_mapping(tree, start, len, 0);
Chris Masonb917b7c2009-09-18 16:07:03 -0400431}
432
433/**
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400434 * remove_extent_mapping - removes an extent_map from the extent tree
435 * @tree: extent tree to remove from
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -0800436 * @em: extent map being removed
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400437 *
438 * Removes @em from @tree. No reference counts are dropped, and no checks
439 * are done to see if the range is in use
Chris Masona52d9a82007-08-27 16:49:44 -0400440 */
441int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
442{
Chris Masond1310b22008-01-24 16:13:08 -0500443 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400444
Chris Mason7f3c74f2008-07-18 12:01:11 -0400445 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
Chris Masond1310b22008-01-24 16:13:08 -0500446 rb_erase(&em->rb_node, &tree->map);
Josef Bacikff44c6e2012-09-14 12:59:20 -0400447 if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
448 list_del_init(&em->list);
Filipe Mananacbc0e922014-02-25 14:15:12 +0000449 RB_CLEAR_NODE(&em->rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400450 return ret;
451}
Filipe Manana176840b2014-02-25 14:15:13 +0000452
453void replace_extent_mapping(struct extent_map_tree *tree,
454 struct extent_map *cur,
455 struct extent_map *new,
456 int modified)
457{
458 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));
459 ASSERT(extent_map_in_tree(cur));
460 if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))
461 list_del_init(&cur->list);
462 rb_replace_node(&cur->rb_node, &new->rb_node, &tree->map);
463 RB_CLEAR_NODE(&cur->rb_node);
464
465 setup_extent_mapping(tree, new, modified);
466}