xref: /openbmc/linux/fs/btrfs/extent_map.c (revision 4e2f84e63dc138eca91e89ccbc34f37732ce58f7)
1d1310b2eSChris Mason #include <linux/err.h>
2d1310b2eSChris Mason #include <linux/slab.h>
3a52d9a80SChris Mason #include <linux/module.h>
4a52d9a80SChris Mason #include <linux/spinlock.h>
5d1310b2eSChris Mason #include <linux/hardirq.h>
6261507a0SLi Zefan #include "ctree.h"
7a52d9a80SChris Mason #include "extent_map.h"
8a52d9a80SChris Mason 
986479a04SChris Mason 
10a52d9a80SChris Mason static struct kmem_cache *extent_map_cache;
11ca664626SChris Mason 
122f4cbe64SWyatt Banks int __init extent_map_init(void)
13a52d9a80SChris Mason {
149601e3f6SChristoph Hellwig 	extent_map_cache = kmem_cache_create("extent_map",
156d36dcd4SChris Mason 			sizeof(struct extent_map), 0,
169601e3f6SChristoph Hellwig 			SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
172f4cbe64SWyatt Banks 	if (!extent_map_cache)
182f4cbe64SWyatt Banks 		return -ENOMEM;
192f4cbe64SWyatt Banks 	return 0;
20a52d9a80SChris Mason }
21a52d9a80SChris Mason 
2217636e03SChristian Hesse void extent_map_exit(void)
23a52d9a80SChris Mason {
24a52d9a80SChris Mason 	if (extent_map_cache)
25a52d9a80SChris Mason 		kmem_cache_destroy(extent_map_cache);
26a52d9a80SChris Mason }
27a52d9a80SChris Mason 
289d2423c5SChristoph Hellwig /**
299d2423c5SChristoph Hellwig  * extent_map_tree_init - initialize extent map tree
309d2423c5SChristoph Hellwig  * @tree:		tree to initialize
319d2423c5SChristoph Hellwig  *
329d2423c5SChristoph Hellwig  * Initialize the extent tree @tree.  Should be called for each new inode
339d2423c5SChristoph Hellwig  * or other user of the extent_map interface.
349d2423c5SChristoph Hellwig  */
35a8067e02SDavid Sterba void extent_map_tree_init(struct extent_map_tree *tree)
36a52d9a80SChris Mason {
376bef4d31SEric Paris 	tree->map = RB_ROOT;
385dc562c5SJosef Bacik 	INIT_LIST_HEAD(&tree->modified_extents);
39890871beSChris Mason 	rwlock_init(&tree->lock);
40a52d9a80SChris Mason }
41a52d9a80SChris Mason 
429d2423c5SChristoph Hellwig /**
439d2423c5SChristoph Hellwig  * alloc_extent_map - allocate new extent map structure
449d2423c5SChristoph Hellwig  *
459d2423c5SChristoph Hellwig  * Allocate a new extent_map structure.  The new structure is
469d2423c5SChristoph Hellwig  * returned with a reference count of one and needs to be
479d2423c5SChristoph Hellwig  * freed using free_extent_map()
489d2423c5SChristoph Hellwig  */
49172ddd60SDavid Sterba struct extent_map *alloc_extent_map(void)
50a52d9a80SChris Mason {
51a52d9a80SChris Mason 	struct extent_map *em;
52172ddd60SDavid Sterba 	em = kmem_cache_alloc(extent_map_cache, GFP_NOFS);
53c26a9203STsutomu Itoh 	if (!em)
54c26a9203STsutomu Itoh 		return NULL;
55a52d9a80SChris Mason 	em->in_tree = 0;
56d1310b2eSChris Mason 	em->flags = 0;
57261507a0SLi Zefan 	em->compress_type = BTRFS_COMPRESS_NONE;
585dc562c5SJosef Bacik 	em->generation = 0;
59a52d9a80SChris Mason 	atomic_set(&em->refs, 1);
605dc562c5SJosef Bacik 	INIT_LIST_HEAD(&em->list);
61a52d9a80SChris Mason 	return em;
62a52d9a80SChris Mason }
63a52d9a80SChris Mason 
649d2423c5SChristoph Hellwig /**
659d2423c5SChristoph Hellwig  * free_extent_map - drop reference count of an extent_map
669d2423c5SChristoph Hellwig  * @em:		extent map beeing releasead
679d2423c5SChristoph Hellwig  *
689d2423c5SChristoph Hellwig  * Drops the reference out on @em by one and free the structure
699d2423c5SChristoph Hellwig  * if the reference count hits zero.
709d2423c5SChristoph Hellwig  */
71a52d9a80SChris Mason void free_extent_map(struct extent_map *em)
72a52d9a80SChris Mason {
732bf5a725SChris Mason 	if (!em)
742bf5a725SChris Mason 		return;
75d1310b2eSChris Mason 	WARN_ON(atomic_read(&em->refs) == 0);
76a52d9a80SChris Mason 	if (atomic_dec_and_test(&em->refs)) {
77a52d9a80SChris Mason 		WARN_ON(em->in_tree);
785dc562c5SJosef Bacik 		WARN_ON(!list_empty(&em->list));
79a52d9a80SChris Mason 		kmem_cache_free(extent_map_cache, em);
80a52d9a80SChris Mason 	}
81a52d9a80SChris Mason }
82a52d9a80SChris Mason 
83a52d9a80SChris Mason static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
84a52d9a80SChris Mason 				   struct rb_node *node)
85a52d9a80SChris Mason {
86a52d9a80SChris Mason 	struct rb_node **p = &root->rb_node;
87a52d9a80SChris Mason 	struct rb_node *parent = NULL;
88d1310b2eSChris Mason 	struct extent_map *entry;
89a52d9a80SChris Mason 
90a52d9a80SChris Mason 	while (*p) {
91a52d9a80SChris Mason 		parent = *p;
92d1310b2eSChris Mason 		entry = rb_entry(parent, struct extent_map, rb_node);
93d1310b2eSChris Mason 
94d1310b2eSChris Mason 		WARN_ON(!entry->in_tree);
95a52d9a80SChris Mason 
96a52d9a80SChris Mason 		if (offset < entry->start)
97a52d9a80SChris Mason 			p = &(*p)->rb_left;
98d1310b2eSChris Mason 		else if (offset >= extent_map_end(entry))
99a52d9a80SChris Mason 			p = &(*p)->rb_right;
100a52d9a80SChris Mason 		else
101a52d9a80SChris Mason 			return parent;
102a52d9a80SChris Mason 	}
103a52d9a80SChris Mason 
104d1310b2eSChris Mason 	entry = rb_entry(node, struct extent_map, rb_node);
105a52d9a80SChris Mason 	entry->in_tree = 1;
106a52d9a80SChris Mason 	rb_link_node(node, parent, p);
107a52d9a80SChris Mason 	rb_insert_color(node, root);
108a52d9a80SChris Mason 	return NULL;
109a52d9a80SChris Mason }
110a52d9a80SChris Mason 
111d352ac68SChris Mason /*
112d352ac68SChris Mason  * search through the tree for an extent_map with a given offset.  If
113d352ac68SChris Mason  * it can't be found, try to find some neighboring extents
114d352ac68SChris Mason  */
115a52d9a80SChris Mason static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
1165f56406aSChris Mason 				     struct rb_node **prev_ret,
1175f56406aSChris Mason 				     struct rb_node **next_ret)
118a52d9a80SChris Mason {
119a52d9a80SChris Mason 	struct rb_node *n = root->rb_node;
120a52d9a80SChris Mason 	struct rb_node *prev = NULL;
1215f56406aSChris Mason 	struct rb_node *orig_prev = NULL;
122d1310b2eSChris Mason 	struct extent_map *entry;
123d1310b2eSChris Mason 	struct extent_map *prev_entry = NULL;
124a52d9a80SChris Mason 
125a52d9a80SChris Mason 	while (n) {
126d1310b2eSChris Mason 		entry = rb_entry(n, struct extent_map, rb_node);
127a52d9a80SChris Mason 		prev = n;
128a52d9a80SChris Mason 		prev_entry = entry;
129a52d9a80SChris Mason 
130d1310b2eSChris Mason 		WARN_ON(!entry->in_tree);
131d1310b2eSChris Mason 
132a52d9a80SChris Mason 		if (offset < entry->start)
133a52d9a80SChris Mason 			n = n->rb_left;
134d1310b2eSChris Mason 		else if (offset >= extent_map_end(entry))
135a52d9a80SChris Mason 			n = n->rb_right;
136a52d9a80SChris Mason 		else
137a52d9a80SChris Mason 			return n;
138a52d9a80SChris Mason 	}
1395f56406aSChris Mason 
1405f56406aSChris Mason 	if (prev_ret) {
1415f56406aSChris Mason 		orig_prev = prev;
142d1310b2eSChris Mason 		while (prev && offset >= extent_map_end(prev_entry)) {
143a52d9a80SChris Mason 			prev = rb_next(prev);
144d1310b2eSChris Mason 			prev_entry = rb_entry(prev, struct extent_map, rb_node);
145a52d9a80SChris Mason 		}
146a52d9a80SChris Mason 		*prev_ret = prev;
1475f56406aSChris Mason 		prev = orig_prev;
1485f56406aSChris Mason 	}
1495f56406aSChris Mason 
1505f56406aSChris Mason 	if (next_ret) {
151d1310b2eSChris Mason 		prev_entry = rb_entry(prev, struct extent_map, rb_node);
1525f56406aSChris Mason 		while (prev && offset < prev_entry->start) {
1535f56406aSChris Mason 			prev = rb_prev(prev);
154d1310b2eSChris Mason 			prev_entry = rb_entry(prev, struct extent_map, rb_node);
1555f56406aSChris Mason 		}
1565f56406aSChris Mason 		*next_ret = prev;
1575f56406aSChris Mason 	}
158a52d9a80SChris Mason 	return NULL;
159a52d9a80SChris Mason }
160a52d9a80SChris Mason 
161d352ac68SChris Mason /* check to see if two extent_map structs are adjacent and safe to merge */
162d1310b2eSChris Mason static int mergable_maps(struct extent_map *prev, struct extent_map *next)
163a52d9a80SChris Mason {
1647f3c74fbSChris Mason 	if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
1657f3c74fbSChris Mason 		return 0;
1667f3c74fbSChris Mason 
167c8b97818SChris Mason 	/*
168c8b97818SChris Mason 	 * don't merge compressed extents, we need to know their
169c8b97818SChris Mason 	 * actual size
170c8b97818SChris Mason 	 */
171c8b97818SChris Mason 	if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
172c8b97818SChris Mason 		return 0;
173c8b97818SChris Mason 
174d1310b2eSChris Mason 	if (extent_map_end(prev) == next->start &&
175d1310b2eSChris Mason 	    prev->flags == next->flags &&
176d1310b2eSChris Mason 	    prev->bdev == next->bdev &&
177d1310b2eSChris Mason 	    ((next->block_start == EXTENT_MAP_HOLE &&
178d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_HOLE) ||
179d1310b2eSChris Mason 	     (next->block_start == EXTENT_MAP_INLINE &&
180d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_INLINE) ||
181d1310b2eSChris Mason 	     (next->block_start == EXTENT_MAP_DELALLOC &&
182d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_DELALLOC) ||
183d1310b2eSChris Mason 	     (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
184d1310b2eSChris Mason 	      next->block_start == extent_map_block_end(prev)))) {
185d1310b2eSChris Mason 		return 1;
186d1310b2eSChris Mason 	}
187a52d9a80SChris Mason 	return 0;
188a52d9a80SChris Mason }
189a52d9a80SChris Mason 
1904d2c8f62SLi Zefan static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
191a1ed835eSChris Mason {
192a1ed835eSChris Mason 	struct extent_map *merge = NULL;
193a1ed835eSChris Mason 	struct rb_node *rb;
194a1ed835eSChris Mason 
195a1ed835eSChris Mason 	if (em->start != 0) {
196a1ed835eSChris Mason 		rb = rb_prev(&em->rb_node);
197a1ed835eSChris Mason 		if (rb)
198a1ed835eSChris Mason 			merge = rb_entry(rb, struct extent_map, rb_node);
199a1ed835eSChris Mason 		if (rb && mergable_maps(merge, em)) {
200a1ed835eSChris Mason 			em->start = merge->start;
201a1ed835eSChris Mason 			em->len += merge->len;
202a1ed835eSChris Mason 			em->block_len += merge->block_len;
203a1ed835eSChris Mason 			em->block_start = merge->block_start;
204a1ed835eSChris Mason 			merge->in_tree = 0;
2055dc562c5SJosef Bacik 			if (merge->generation > em->generation) {
206*4e2f84e6SLiu Bo 				em->mod_start = em->start;
207*4e2f84e6SLiu Bo 				em->mod_len = em->len;
2085dc562c5SJosef Bacik 				em->generation = merge->generation;
2095dc562c5SJosef Bacik 				list_move(&em->list, &tree->modified_extents);
2105dc562c5SJosef Bacik 			}
2115dc562c5SJosef Bacik 
2125dc562c5SJosef Bacik 			list_del_init(&merge->list);
213a1ed835eSChris Mason 			rb_erase(&merge->rb_node, &tree->map);
214a1ed835eSChris Mason 			free_extent_map(merge);
215a1ed835eSChris Mason 		}
216a1ed835eSChris Mason 	}
217a1ed835eSChris Mason 
218a1ed835eSChris Mason 	rb = rb_next(&em->rb_node);
219a1ed835eSChris Mason 	if (rb)
220a1ed835eSChris Mason 		merge = rb_entry(rb, struct extent_map, rb_node);
221a1ed835eSChris Mason 	if (rb && mergable_maps(em, merge)) {
222a1ed835eSChris Mason 		em->len += merge->len;
223a1ed835eSChris Mason 		em->block_len += merge->len;
224a1ed835eSChris Mason 		rb_erase(&merge->rb_node, &tree->map);
225a1ed835eSChris Mason 		merge->in_tree = 0;
2265dc562c5SJosef Bacik 		if (merge->generation > em->generation) {
227*4e2f84e6SLiu Bo 			em->mod_len = em->len;
2285dc562c5SJosef Bacik 			em->generation = merge->generation;
2295dc562c5SJosef Bacik 			list_move(&em->list, &tree->modified_extents);
2305dc562c5SJosef Bacik 		}
2315dc562c5SJosef Bacik 		list_del_init(&merge->list);
232a1ed835eSChris Mason 		free_extent_map(merge);
233a1ed835eSChris Mason 	}
2344d2c8f62SLi Zefan }
2354d2c8f62SLi Zefan 
2365dc562c5SJosef Bacik /**
2375dc562c5SJosef Bacik  * unpint_extent_cache - unpin an extent from the cache
2385dc562c5SJosef Bacik  * @tree:	tree to unpin the extent in
2395dc562c5SJosef Bacik  * @start:	logical offset in the file
2405dc562c5SJosef Bacik  * @len:	length of the extent
2415dc562c5SJosef Bacik  * @gen:	generation that this extent has been modified in
2425dc562c5SJosef Bacik  * @prealloc:	if this is set we need to clear the prealloc flag
2435dc562c5SJosef Bacik  *
2445dc562c5SJosef Bacik  * Called after an extent has been written to disk properly.  Set the generation
2455dc562c5SJosef Bacik  * to the generation that actually added the file item to the inode so we know
2465dc562c5SJosef Bacik  * we need to sync this extent when we call fsync().
2475dc562c5SJosef Bacik  */
2485dc562c5SJosef Bacik int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
2495dc562c5SJosef Bacik 		       u64 gen)
2504d2c8f62SLi Zefan {
2514d2c8f62SLi Zefan 	int ret = 0;
2524d2c8f62SLi Zefan 	struct extent_map *em;
253*4e2f84e6SLiu Bo 	bool prealloc = false;
2544d2c8f62SLi Zefan 
2554d2c8f62SLi Zefan 	write_lock(&tree->lock);
2564d2c8f62SLi Zefan 	em = lookup_extent_mapping(tree, start, len);
2574d2c8f62SLi Zefan 
2584d2c8f62SLi Zefan 	WARN_ON(!em || em->start != start);
2594d2c8f62SLi Zefan 
2604d2c8f62SLi Zefan 	if (!em)
2614d2c8f62SLi Zefan 		goto out;
2624d2c8f62SLi Zefan 
2635dc562c5SJosef Bacik 	list_move(&em->list, &tree->modified_extents);
2645dc562c5SJosef Bacik 	em->generation = gen;
2654d2c8f62SLi Zefan 	clear_bit(EXTENT_FLAG_PINNED, &em->flags);
266*4e2f84e6SLiu Bo 	em->mod_start = em->start;
267*4e2f84e6SLiu Bo 	em->mod_len = em->len;
268*4e2f84e6SLiu Bo 
269*4e2f84e6SLiu Bo 	if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
270*4e2f84e6SLiu Bo 		prealloc = true;
271*4e2f84e6SLiu Bo 		clear_bit(EXTENT_FLAG_PREALLOC, &em->flags);
272*4e2f84e6SLiu Bo 	}
2734d2c8f62SLi Zefan 
2744d2c8f62SLi Zefan 	try_merge_map(tree, em);
275*4e2f84e6SLiu Bo 
276*4e2f84e6SLiu Bo 	if (prealloc) {
277*4e2f84e6SLiu Bo 		em->mod_start = em->start;
278*4e2f84e6SLiu Bo 		em->mod_len = em->len;
279*4e2f84e6SLiu Bo 	}
280*4e2f84e6SLiu Bo 
281a1ed835eSChris Mason 	free_extent_map(em);
282a1ed835eSChris Mason out:
283a1ed835eSChris Mason 	write_unlock(&tree->lock);
284a1ed835eSChris Mason 	return ret;
285a1ed835eSChris Mason 
286a1ed835eSChris Mason }
287a1ed835eSChris Mason 
2889d2423c5SChristoph Hellwig /**
2899d2423c5SChristoph Hellwig  * add_extent_mapping - add new extent map to the extent tree
2909d2423c5SChristoph Hellwig  * @tree:	tree to insert new map in
2919d2423c5SChristoph Hellwig  * @em:		map to insert
2929d2423c5SChristoph Hellwig  *
2939d2423c5SChristoph Hellwig  * Insert @em into @tree or perform a simple forward/backward merge with
2949d2423c5SChristoph Hellwig  * existing mappings.  The extent_map struct passed in will be inserted
2959d2423c5SChristoph Hellwig  * into the tree directly, with an additional reference taken, or a
29625985edcSLucas De Marchi  * reference dropped if the merge attempt was successful.
297a52d9a80SChris Mason  */
298a52d9a80SChris Mason int add_extent_mapping(struct extent_map_tree *tree,
299a52d9a80SChris Mason 		       struct extent_map *em)
300a52d9a80SChris Mason {
301a52d9a80SChris Mason 	int ret = 0;
302a52d9a80SChris Mason 	struct rb_node *rb;
3037c2fe32aSChris Mason 	struct extent_map *exist;
304a52d9a80SChris Mason 
3057c2fe32aSChris Mason 	exist = lookup_extent_mapping(tree, em->start, em->len);
3067c2fe32aSChris Mason 	if (exist) {
3077c2fe32aSChris Mason 		free_extent_map(exist);
3087c2fe32aSChris Mason 		ret = -EEXIST;
3097c2fe32aSChris Mason 		goto out;
3107c2fe32aSChris Mason 	}
311d1310b2eSChris Mason 	rb = tree_insert(&tree->map, em->start, &em->rb_node);
312a52d9a80SChris Mason 	if (rb) {
313a52d9a80SChris Mason 		ret = -EEXIST;
314a52d9a80SChris Mason 		goto out;
315a52d9a80SChris Mason 	}
316a52d9a80SChris Mason 	atomic_inc(&em->refs);
3174d2c8f62SLi Zefan 
318*4e2f84e6SLiu Bo 	em->mod_start = em->start;
319*4e2f84e6SLiu Bo 	em->mod_len = em->len;
320*4e2f84e6SLiu Bo 
3214d2c8f62SLi Zefan 	try_merge_map(tree, em);
322a52d9a80SChris Mason out:
323a52d9a80SChris Mason 	return ret;
324a52d9a80SChris Mason }
325a52d9a80SChris Mason 
326d352ac68SChris Mason /* simple helper to do math around the end of an extent, handling wrap */
327d1310b2eSChris Mason static u64 range_end(u64 start, u64 len)
328d1310b2eSChris Mason {
329d1310b2eSChris Mason 	if (start + len < start)
330d1310b2eSChris Mason 		return (u64)-1;
331d1310b2eSChris Mason 	return start + len;
332d1310b2eSChris Mason }
333d1310b2eSChris Mason 
334ed64f066SLi Zefan struct extent_map *__lookup_extent_mapping(struct extent_map_tree *tree,
335ed64f066SLi Zefan 					   u64 start, u64 len, int strict)
336ed64f066SLi Zefan {
337ed64f066SLi Zefan 	struct extent_map *em;
338ed64f066SLi Zefan 	struct rb_node *rb_node;
339ed64f066SLi Zefan 	struct rb_node *prev = NULL;
340ed64f066SLi Zefan 	struct rb_node *next = NULL;
341ed64f066SLi Zefan 	u64 end = range_end(start, len);
342ed64f066SLi Zefan 
343ed64f066SLi Zefan 	rb_node = __tree_search(&tree->map, start, &prev, &next);
344ed64f066SLi Zefan 	if (!rb_node) {
345ed64f066SLi Zefan 		if (prev)
346ed64f066SLi Zefan 			rb_node = prev;
347ed64f066SLi Zefan 		else if (next)
348ed64f066SLi Zefan 			rb_node = next;
349ed64f066SLi Zefan 		else
350ed64f066SLi Zefan 			return NULL;
351ed64f066SLi Zefan 	}
352ed64f066SLi Zefan 
353ed64f066SLi Zefan 	em = rb_entry(rb_node, struct extent_map, rb_node);
354ed64f066SLi Zefan 
355ed64f066SLi Zefan 	if (strict && !(end > em->start && start < extent_map_end(em)))
356ed64f066SLi Zefan 		return NULL;
357ed64f066SLi Zefan 
358ed64f066SLi Zefan 	atomic_inc(&em->refs);
359ed64f066SLi Zefan 	return em;
360ed64f066SLi Zefan }
361ed64f066SLi Zefan 
3629d2423c5SChristoph Hellwig /**
3639d2423c5SChristoph Hellwig  * lookup_extent_mapping - lookup extent_map
3649d2423c5SChristoph Hellwig  * @tree:	tree to lookup in
3659d2423c5SChristoph Hellwig  * @start:	byte offset to start the search
3669d2423c5SChristoph Hellwig  * @len:	length of the lookup range
3679d2423c5SChristoph Hellwig  *
3689d2423c5SChristoph Hellwig  * Find and return the first extent_map struct in @tree that intersects the
3699d2423c5SChristoph Hellwig  * [start, len] range.  There may be additional objects in the tree that
3709d2423c5SChristoph Hellwig  * intersect, so check the object returned carefully to make sure that no
3719d2423c5SChristoph Hellwig  * additional lookups are needed.
372a52d9a80SChris Mason  */
373a52d9a80SChris Mason struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
374d1310b2eSChris Mason 					 u64 start, u64 len)
375a52d9a80SChris Mason {
376ed64f066SLi Zefan 	return __lookup_extent_mapping(tree, start, len, 1);
377a52d9a80SChris Mason }
378a52d9a80SChris Mason 
3799d2423c5SChristoph Hellwig /**
380b917b7c3SChris Mason  * search_extent_mapping - find a nearby extent map
381b917b7c3SChris Mason  * @tree:	tree to lookup in
382b917b7c3SChris Mason  * @start:	byte offset to start the search
383b917b7c3SChris Mason  * @len:	length of the lookup range
384b917b7c3SChris Mason  *
385b917b7c3SChris Mason  * Find and return the first extent_map struct in @tree that intersects the
386b917b7c3SChris Mason  * [start, len] range.
387b917b7c3SChris Mason  *
388b917b7c3SChris Mason  * If one can't be found, any nearby extent may be returned
389b917b7c3SChris Mason  */
390b917b7c3SChris Mason struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
391b917b7c3SChris Mason 					 u64 start, u64 len)
392b917b7c3SChris Mason {
393ed64f066SLi Zefan 	return __lookup_extent_mapping(tree, start, len, 0);
394b917b7c3SChris Mason }
395b917b7c3SChris Mason 
396b917b7c3SChris Mason /**
3979d2423c5SChristoph Hellwig  * remove_extent_mapping - removes an extent_map from the extent tree
3989d2423c5SChristoph Hellwig  * @tree:	extent tree to remove from
3999d2423c5SChristoph Hellwig  * @em:		extent map beeing removed
4009d2423c5SChristoph Hellwig  *
4019d2423c5SChristoph Hellwig  * Removes @em from @tree.  No reference counts are dropped, and no checks
4029d2423c5SChristoph Hellwig  * are done to see if the range is in use
403a52d9a80SChris Mason  */
404a52d9a80SChris Mason int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
405a52d9a80SChris Mason {
406d1310b2eSChris Mason 	int ret = 0;
407a52d9a80SChris Mason 
4087f3c74fbSChris Mason 	WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
409d1310b2eSChris Mason 	rb_erase(&em->rb_node, &tree->map);
4105dc562c5SJosef Bacik 	list_del_init(&em->list);
411d1310b2eSChris Mason 	em->in_tree = 0;
412a52d9a80SChris Mason 	return ret;
413a52d9a80SChris Mason }
414