xref: /openbmc/linux/fs/btrfs/extent_map.c (revision 70c8a91ce21b83ccd2d9e7c968775430ead4353d)
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 {
14837e1972SDavid Sterba 	extent_map_cache = kmem_cache_create("btrfs_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;
52*70c8a91cSJosef Bacik 	em = kmem_cache_zalloc(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;
201*70c8a91cSJosef Bacik 			em->orig_start = merge->orig_start;
202a1ed835eSChris Mason 			em->len += merge->len;
203a1ed835eSChris Mason 			em->block_len += merge->block_len;
204a1ed835eSChris Mason 			em->block_start = merge->block_start;
205a1ed835eSChris Mason 			merge->in_tree = 0;
206*70c8a91cSJosef Bacik 			em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
207*70c8a91cSJosef Bacik 			em->mod_start = merge->mod_start;
208*70c8a91cSJosef Bacik 			em->generation = max(em->generation, merge->generation);
2095dc562c5SJosef Bacik 			list_move(&em->list, &tree->modified_extents);
2105dc562c5SJosef Bacik 
2115dc562c5SJosef Bacik 			list_del_init(&merge->list);
212a1ed835eSChris Mason 			rb_erase(&merge->rb_node, &tree->map);
213a1ed835eSChris Mason 			free_extent_map(merge);
214a1ed835eSChris Mason 		}
215a1ed835eSChris Mason 	}
216a1ed835eSChris Mason 
217a1ed835eSChris Mason 	rb = rb_next(&em->rb_node);
218a1ed835eSChris Mason 	if (rb)
219a1ed835eSChris Mason 		merge = rb_entry(rb, struct extent_map, rb_node);
220a1ed835eSChris Mason 	if (rb && mergable_maps(em, merge)) {
221a1ed835eSChris Mason 		em->len += merge->len;
222a1ed835eSChris Mason 		em->block_len += merge->len;
223a1ed835eSChris Mason 		rb_erase(&merge->rb_node, &tree->map);
224a1ed835eSChris Mason 		merge->in_tree = 0;
225*70c8a91cSJosef Bacik 		em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
226*70c8a91cSJosef Bacik 		em->generation = max(em->generation, merge->generation);
2275dc562c5SJosef Bacik 		list_del_init(&merge->list);
228a1ed835eSChris Mason 		free_extent_map(merge);
229a1ed835eSChris Mason 	}
2304d2c8f62SLi Zefan }
2314d2c8f62SLi Zefan 
2325dc562c5SJosef Bacik /**
2335dc562c5SJosef Bacik  * unpint_extent_cache - unpin an extent from the cache
2345dc562c5SJosef Bacik  * @tree:	tree to unpin the extent in
2355dc562c5SJosef Bacik  * @start:	logical offset in the file
2365dc562c5SJosef Bacik  * @len:	length of the extent
2375dc562c5SJosef Bacik  * @gen:	generation that this extent has been modified in
2385dc562c5SJosef Bacik  * @prealloc:	if this is set we need to clear the prealloc flag
2395dc562c5SJosef Bacik  *
2405dc562c5SJosef Bacik  * Called after an extent has been written to disk properly.  Set the generation
2415dc562c5SJosef Bacik  * to the generation that actually added the file item to the inode so we know
2425dc562c5SJosef Bacik  * we need to sync this extent when we call fsync().
2435dc562c5SJosef Bacik  */
2445dc562c5SJosef Bacik int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
2455dc562c5SJosef Bacik 		       u64 gen)
2464d2c8f62SLi Zefan {
2474d2c8f62SLi Zefan 	int ret = 0;
2484d2c8f62SLi Zefan 	struct extent_map *em;
2494e2f84e6SLiu Bo 	bool prealloc = false;
2504d2c8f62SLi Zefan 
2514d2c8f62SLi Zefan 	write_lock(&tree->lock);
2524d2c8f62SLi Zefan 	em = lookup_extent_mapping(tree, start, len);
2534d2c8f62SLi Zefan 
2544d2c8f62SLi Zefan 	WARN_ON(!em || em->start != start);
2554d2c8f62SLi Zefan 
2564d2c8f62SLi Zefan 	if (!em)
2574d2c8f62SLi Zefan 		goto out;
2584d2c8f62SLi Zefan 
2595dc562c5SJosef Bacik 	list_move(&em->list, &tree->modified_extents);
2605dc562c5SJosef Bacik 	em->generation = gen;
2614d2c8f62SLi Zefan 	clear_bit(EXTENT_FLAG_PINNED, &em->flags);
2624e2f84e6SLiu Bo 	em->mod_start = em->start;
2634e2f84e6SLiu Bo 	em->mod_len = em->len;
2644e2f84e6SLiu Bo 
265b11e234dSJosef Bacik 	if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
2664e2f84e6SLiu Bo 		prealloc = true;
267b11e234dSJosef Bacik 		clear_bit(EXTENT_FLAG_FILLING, &em->flags);
2684e2f84e6SLiu Bo 	}
2694d2c8f62SLi Zefan 
2704d2c8f62SLi Zefan 	try_merge_map(tree, em);
2714e2f84e6SLiu Bo 
2724e2f84e6SLiu Bo 	if (prealloc) {
2734e2f84e6SLiu Bo 		em->mod_start = em->start;
2744e2f84e6SLiu Bo 		em->mod_len = em->len;
2754e2f84e6SLiu Bo 	}
2764e2f84e6SLiu Bo 
277a1ed835eSChris Mason 	free_extent_map(em);
278a1ed835eSChris Mason out:
279a1ed835eSChris Mason 	write_unlock(&tree->lock);
280a1ed835eSChris Mason 	return ret;
281a1ed835eSChris Mason 
282a1ed835eSChris Mason }
283a1ed835eSChris Mason 
2849d2423c5SChristoph Hellwig /**
2859d2423c5SChristoph Hellwig  * add_extent_mapping - add new extent map to the extent tree
2869d2423c5SChristoph Hellwig  * @tree:	tree to insert new map in
2879d2423c5SChristoph Hellwig  * @em:		map to insert
2889d2423c5SChristoph Hellwig  *
2899d2423c5SChristoph Hellwig  * Insert @em into @tree or perform a simple forward/backward merge with
2909d2423c5SChristoph Hellwig  * existing mappings.  The extent_map struct passed in will be inserted
2919d2423c5SChristoph Hellwig  * into the tree directly, with an additional reference taken, or a
29225985edcSLucas De Marchi  * reference dropped if the merge attempt was successful.
293a52d9a80SChris Mason  */
294a52d9a80SChris Mason int add_extent_mapping(struct extent_map_tree *tree,
295a52d9a80SChris Mason 		       struct extent_map *em)
296a52d9a80SChris Mason {
297a52d9a80SChris Mason 	int ret = 0;
298a52d9a80SChris Mason 	struct rb_node *rb;
2997c2fe32aSChris Mason 	struct extent_map *exist;
300a52d9a80SChris Mason 
3017c2fe32aSChris Mason 	exist = lookup_extent_mapping(tree, em->start, em->len);
3027c2fe32aSChris Mason 	if (exist) {
3037c2fe32aSChris Mason 		free_extent_map(exist);
3047c2fe32aSChris Mason 		ret = -EEXIST;
3057c2fe32aSChris Mason 		goto out;
3067c2fe32aSChris Mason 	}
307d1310b2eSChris Mason 	rb = tree_insert(&tree->map, em->start, &em->rb_node);
308a52d9a80SChris Mason 	if (rb) {
309a52d9a80SChris Mason 		ret = -EEXIST;
310a52d9a80SChris Mason 		goto out;
311a52d9a80SChris Mason 	}
312a52d9a80SChris Mason 	atomic_inc(&em->refs);
3134d2c8f62SLi Zefan 
3144e2f84e6SLiu Bo 	em->mod_start = em->start;
3154e2f84e6SLiu Bo 	em->mod_len = em->len;
3164e2f84e6SLiu Bo 
3174d2c8f62SLi Zefan 	try_merge_map(tree, em);
318a52d9a80SChris Mason out:
319a52d9a80SChris Mason 	return ret;
320a52d9a80SChris Mason }
321a52d9a80SChris Mason 
322d352ac68SChris Mason /* simple helper to do math around the end of an extent, handling wrap */
323d1310b2eSChris Mason static u64 range_end(u64 start, u64 len)
324d1310b2eSChris Mason {
325d1310b2eSChris Mason 	if (start + len < start)
326d1310b2eSChris Mason 		return (u64)-1;
327d1310b2eSChris Mason 	return start + len;
328d1310b2eSChris Mason }
329d1310b2eSChris Mason 
330ed64f066SLi Zefan struct extent_map *__lookup_extent_mapping(struct extent_map_tree *tree,
331ed64f066SLi Zefan 					   u64 start, u64 len, int strict)
332ed64f066SLi Zefan {
333ed64f066SLi Zefan 	struct extent_map *em;
334ed64f066SLi Zefan 	struct rb_node *rb_node;
335ed64f066SLi Zefan 	struct rb_node *prev = NULL;
336ed64f066SLi Zefan 	struct rb_node *next = NULL;
337ed64f066SLi Zefan 	u64 end = range_end(start, len);
338ed64f066SLi Zefan 
339ed64f066SLi Zefan 	rb_node = __tree_search(&tree->map, start, &prev, &next);
340ed64f066SLi Zefan 	if (!rb_node) {
341ed64f066SLi Zefan 		if (prev)
342ed64f066SLi Zefan 			rb_node = prev;
343ed64f066SLi Zefan 		else if (next)
344ed64f066SLi Zefan 			rb_node = next;
345ed64f066SLi Zefan 		else
346ed64f066SLi Zefan 			return NULL;
347ed64f066SLi Zefan 	}
348ed64f066SLi Zefan 
349ed64f066SLi Zefan 	em = rb_entry(rb_node, struct extent_map, rb_node);
350ed64f066SLi Zefan 
351ed64f066SLi Zefan 	if (strict && !(end > em->start && start < extent_map_end(em)))
352ed64f066SLi Zefan 		return NULL;
353ed64f066SLi Zefan 
354ed64f066SLi Zefan 	atomic_inc(&em->refs);
355ed64f066SLi Zefan 	return em;
356ed64f066SLi Zefan }
357ed64f066SLi Zefan 
3589d2423c5SChristoph Hellwig /**
3599d2423c5SChristoph Hellwig  * lookup_extent_mapping - lookup extent_map
3609d2423c5SChristoph Hellwig  * @tree:	tree to lookup in
3619d2423c5SChristoph Hellwig  * @start:	byte offset to start the search
3629d2423c5SChristoph Hellwig  * @len:	length of the lookup range
3639d2423c5SChristoph Hellwig  *
3649d2423c5SChristoph Hellwig  * Find and return the first extent_map struct in @tree that intersects the
3659d2423c5SChristoph Hellwig  * [start, len] range.  There may be additional objects in the tree that
3669d2423c5SChristoph Hellwig  * intersect, so check the object returned carefully to make sure that no
3679d2423c5SChristoph Hellwig  * additional lookups are needed.
368a52d9a80SChris Mason  */
369a52d9a80SChris Mason struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
370d1310b2eSChris Mason 					 u64 start, u64 len)
371a52d9a80SChris Mason {
372ed64f066SLi Zefan 	return __lookup_extent_mapping(tree, start, len, 1);
373a52d9a80SChris Mason }
374a52d9a80SChris Mason 
3759d2423c5SChristoph Hellwig /**
376b917b7c3SChris Mason  * search_extent_mapping - find a nearby extent map
377b917b7c3SChris Mason  * @tree:	tree to lookup in
378b917b7c3SChris Mason  * @start:	byte offset to start the search
379b917b7c3SChris Mason  * @len:	length of the lookup range
380b917b7c3SChris Mason  *
381b917b7c3SChris Mason  * Find and return the first extent_map struct in @tree that intersects the
382b917b7c3SChris Mason  * [start, len] range.
383b917b7c3SChris Mason  *
384b917b7c3SChris Mason  * If one can't be found, any nearby extent may be returned
385b917b7c3SChris Mason  */
386b917b7c3SChris Mason struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
387b917b7c3SChris Mason 					 u64 start, u64 len)
388b917b7c3SChris Mason {
389ed64f066SLi Zefan 	return __lookup_extent_mapping(tree, start, len, 0);
390b917b7c3SChris Mason }
391b917b7c3SChris Mason 
392b917b7c3SChris Mason /**
3939d2423c5SChristoph Hellwig  * remove_extent_mapping - removes an extent_map from the extent tree
3949d2423c5SChristoph Hellwig  * @tree:	extent tree to remove from
3959d2423c5SChristoph Hellwig  * @em:		extent map beeing removed
3969d2423c5SChristoph Hellwig  *
3979d2423c5SChristoph Hellwig  * Removes @em from @tree.  No reference counts are dropped, and no checks
3989d2423c5SChristoph Hellwig  * are done to see if the range is in use
399a52d9a80SChris Mason  */
400a52d9a80SChris Mason int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
401a52d9a80SChris Mason {
402d1310b2eSChris Mason 	int ret = 0;
403a52d9a80SChris Mason 
4047f3c74fbSChris Mason 	WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
405d1310b2eSChris Mason 	rb_erase(&em->rb_node, &tree->map);
406ff44c6e3SJosef Bacik 	if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
4075dc562c5SJosef Bacik 		list_del_init(&em->list);
408d1310b2eSChris Mason 	em->in_tree = 0;
409a52d9a80SChris Mason 	return ret;
410a52d9a80SChris Mason }
411