xref: /openbmc/linux/fs/btrfs/extent_map.c (revision 393da91819e35af538ef97c7c6a04899e2fbfe0e)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2d1310b2eSChris Mason #include <linux/err.h>
3d1310b2eSChris Mason #include <linux/slab.h>
4a52d9a80SChris Mason #include <linux/spinlock.h>
5261507a0SLi Zefan #include "ctree.h"
6a52d9a80SChris Mason #include "extent_map.h"
7ebb8765bSAnand Jain #include "compression.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,
16fba4b697SNikolay Borisov 			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 	kmem_cache_destroy(extent_map_cache);
25a52d9a80SChris Mason }
26a52d9a80SChris Mason 
279d2423c5SChristoph Hellwig /**
289d2423c5SChristoph Hellwig  * extent_map_tree_init - initialize extent map tree
299d2423c5SChristoph Hellwig  * @tree:		tree to initialize
309d2423c5SChristoph Hellwig  *
319d2423c5SChristoph Hellwig  * Initialize the extent tree @tree.  Should be called for each new inode
329d2423c5SChristoph Hellwig  * or other user of the extent_map interface.
339d2423c5SChristoph Hellwig  */
34a8067e02SDavid Sterba void extent_map_tree_init(struct extent_map_tree *tree)
35a52d9a80SChris Mason {
366bef4d31SEric Paris 	tree->map = RB_ROOT;
375dc562c5SJosef Bacik 	INIT_LIST_HEAD(&tree->modified_extents);
38890871beSChris Mason 	rwlock_init(&tree->lock);
39a52d9a80SChris Mason }
40a52d9a80SChris Mason 
419d2423c5SChristoph Hellwig /**
429d2423c5SChristoph Hellwig  * alloc_extent_map - allocate new extent map structure
439d2423c5SChristoph Hellwig  *
449d2423c5SChristoph Hellwig  * Allocate a new extent_map structure.  The new structure is
459d2423c5SChristoph Hellwig  * returned with a reference count of one and needs to be
469d2423c5SChristoph Hellwig  * freed using free_extent_map()
479d2423c5SChristoph Hellwig  */
48172ddd60SDavid Sterba struct extent_map *alloc_extent_map(void)
49a52d9a80SChris Mason {
50a52d9a80SChris Mason 	struct extent_map *em;
5170c8a91cSJosef Bacik 	em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS);
52c26a9203STsutomu Itoh 	if (!em)
53c26a9203STsutomu Itoh 		return NULL;
54cbc0e928SFilipe Manana 	RB_CLEAR_NODE(&em->rb_node);
55d1310b2eSChris Mason 	em->flags = 0;
56261507a0SLi Zefan 	em->compress_type = BTRFS_COMPRESS_NONE;
575dc562c5SJosef Bacik 	em->generation = 0;
58490b54d6SElena Reshetova 	refcount_set(&em->refs, 1);
595dc562c5SJosef Bacik 	INIT_LIST_HEAD(&em->list);
60a52d9a80SChris Mason 	return em;
61a52d9a80SChris Mason }
62a52d9a80SChris Mason 
639d2423c5SChristoph Hellwig /**
649d2423c5SChristoph Hellwig  * free_extent_map - drop reference count of an extent_map
6501327610SNicholas D Steeves  * @em:		extent map being released
669d2423c5SChristoph Hellwig  *
679d2423c5SChristoph Hellwig  * Drops the reference out on @em by one and free the structure
689d2423c5SChristoph Hellwig  * if the reference count hits zero.
699d2423c5SChristoph Hellwig  */
70a52d9a80SChris Mason void free_extent_map(struct extent_map *em)
71a52d9a80SChris Mason {
722bf5a725SChris Mason 	if (!em)
732bf5a725SChris Mason 		return;
74490b54d6SElena Reshetova 	WARN_ON(refcount_read(&em->refs) == 0);
75490b54d6SElena Reshetova 	if (refcount_dec_and_test(&em->refs)) {
76cbc0e928SFilipe Manana 		WARN_ON(extent_map_in_tree(em));
775dc562c5SJosef Bacik 		WARN_ON(!list_empty(&em->list));
78298a8f9cSWang Shilong 		if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
7995617d69SJeff Mahoney 			kfree(em->map_lookup);
80a52d9a80SChris Mason 		kmem_cache_free(extent_map_cache, em);
81a52d9a80SChris Mason 	}
82a52d9a80SChris Mason }
83a52d9a80SChris Mason 
8432193c14SFilipe David Borba Manana /* simple helper to do math around the end of an extent, handling wrap */
8532193c14SFilipe David Borba Manana static u64 range_end(u64 start, u64 len)
8632193c14SFilipe David Borba Manana {
8732193c14SFilipe David Borba Manana 	if (start + len < start)
8832193c14SFilipe David Borba Manana 		return (u64)-1;
8932193c14SFilipe David Borba Manana 	return start + len;
9032193c14SFilipe David Borba Manana }
9132193c14SFilipe David Borba Manana 
9232193c14SFilipe David Borba Manana static int tree_insert(struct rb_root *root, struct extent_map *em)
93a52d9a80SChris Mason {
94a52d9a80SChris Mason 	struct rb_node **p = &root->rb_node;
95a52d9a80SChris Mason 	struct rb_node *parent = NULL;
9632193c14SFilipe David Borba Manana 	struct extent_map *entry = NULL;
9732193c14SFilipe David Borba Manana 	struct rb_node *orig_parent = NULL;
9832193c14SFilipe David Borba Manana 	u64 end = range_end(em->start, em->len);
99a52d9a80SChris Mason 
100a52d9a80SChris Mason 	while (*p) {
101a52d9a80SChris Mason 		parent = *p;
102d1310b2eSChris Mason 		entry = rb_entry(parent, struct extent_map, rb_node);
103d1310b2eSChris Mason 
10432193c14SFilipe David Borba Manana 		if (em->start < entry->start)
105a52d9a80SChris Mason 			p = &(*p)->rb_left;
10632193c14SFilipe David Borba Manana 		else if (em->start >= extent_map_end(entry))
107a52d9a80SChris Mason 			p = &(*p)->rb_right;
108a52d9a80SChris Mason 		else
10932193c14SFilipe David Borba Manana 			return -EEXIST;
110a52d9a80SChris Mason 	}
111a52d9a80SChris Mason 
11232193c14SFilipe David Borba Manana 	orig_parent = parent;
11332193c14SFilipe David Borba Manana 	while (parent && em->start >= extent_map_end(entry)) {
11432193c14SFilipe David Borba Manana 		parent = rb_next(parent);
11532193c14SFilipe David Borba Manana 		entry = rb_entry(parent, struct extent_map, rb_node);
11632193c14SFilipe David Borba Manana 	}
11732193c14SFilipe David Borba Manana 	if (parent)
11832193c14SFilipe David Borba Manana 		if (end > entry->start && em->start < extent_map_end(entry))
11932193c14SFilipe David Borba Manana 			return -EEXIST;
12032193c14SFilipe David Borba Manana 
12132193c14SFilipe David Borba Manana 	parent = orig_parent;
12232193c14SFilipe David Borba Manana 	entry = rb_entry(parent, struct extent_map, rb_node);
12332193c14SFilipe David Borba Manana 	while (parent && em->start < entry->start) {
12432193c14SFilipe David Borba Manana 		parent = rb_prev(parent);
12532193c14SFilipe David Borba Manana 		entry = rb_entry(parent, struct extent_map, rb_node);
12632193c14SFilipe David Borba Manana 	}
12732193c14SFilipe David Borba Manana 	if (parent)
12832193c14SFilipe David Borba Manana 		if (end > entry->start && em->start < extent_map_end(entry))
12932193c14SFilipe David Borba Manana 			return -EEXIST;
13032193c14SFilipe David Borba Manana 
13132193c14SFilipe David Borba Manana 	rb_link_node(&em->rb_node, orig_parent, p);
13232193c14SFilipe David Borba Manana 	rb_insert_color(&em->rb_node, root);
13332193c14SFilipe David Borba Manana 	return 0;
134a52d9a80SChris Mason }
135a52d9a80SChris Mason 
136d352ac68SChris Mason /*
137d352ac68SChris Mason  * search through the tree for an extent_map with a given offset.  If
138d352ac68SChris Mason  * it can't be found, try to find some neighboring extents
139d352ac68SChris Mason  */
140a52d9a80SChris Mason static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
1415f56406aSChris Mason 				     struct rb_node **prev_ret,
1425f56406aSChris Mason 				     struct rb_node **next_ret)
143a52d9a80SChris Mason {
144a52d9a80SChris Mason 	struct rb_node *n = root->rb_node;
145a52d9a80SChris Mason 	struct rb_node *prev = NULL;
1465f56406aSChris Mason 	struct rb_node *orig_prev = NULL;
147d1310b2eSChris Mason 	struct extent_map *entry;
148d1310b2eSChris Mason 	struct extent_map *prev_entry = NULL;
149a52d9a80SChris Mason 
150a52d9a80SChris Mason 	while (n) {
151d1310b2eSChris Mason 		entry = rb_entry(n, struct extent_map, rb_node);
152a52d9a80SChris Mason 		prev = n;
153a52d9a80SChris Mason 		prev_entry = entry;
154a52d9a80SChris Mason 
155a52d9a80SChris Mason 		if (offset < entry->start)
156a52d9a80SChris Mason 			n = n->rb_left;
157d1310b2eSChris Mason 		else if (offset >= extent_map_end(entry))
158a52d9a80SChris Mason 			n = n->rb_right;
159a52d9a80SChris Mason 		else
160a52d9a80SChris Mason 			return n;
161a52d9a80SChris Mason 	}
1625f56406aSChris Mason 
1635f56406aSChris Mason 	if (prev_ret) {
1645f56406aSChris Mason 		orig_prev = prev;
165d1310b2eSChris Mason 		while (prev && offset >= extent_map_end(prev_entry)) {
166a52d9a80SChris Mason 			prev = rb_next(prev);
167d1310b2eSChris Mason 			prev_entry = rb_entry(prev, struct extent_map, rb_node);
168a52d9a80SChris Mason 		}
169a52d9a80SChris Mason 		*prev_ret = prev;
1705f56406aSChris Mason 		prev = orig_prev;
1715f56406aSChris Mason 	}
1725f56406aSChris Mason 
1735f56406aSChris Mason 	if (next_ret) {
174d1310b2eSChris Mason 		prev_entry = rb_entry(prev, struct extent_map, rb_node);
1755f56406aSChris Mason 		while (prev && offset < prev_entry->start) {
1765f56406aSChris Mason 			prev = rb_prev(prev);
177d1310b2eSChris Mason 			prev_entry = rb_entry(prev, struct extent_map, rb_node);
1785f56406aSChris Mason 		}
1795f56406aSChris Mason 		*next_ret = prev;
1805f56406aSChris Mason 	}
181a52d9a80SChris Mason 	return NULL;
182a52d9a80SChris Mason }
183a52d9a80SChris Mason 
184d352ac68SChris Mason /* check to see if two extent_map structs are adjacent and safe to merge */
185d1310b2eSChris Mason static int mergable_maps(struct extent_map *prev, struct extent_map *next)
186a52d9a80SChris Mason {
1877f3c74fbSChris Mason 	if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
1887f3c74fbSChris Mason 		return 0;
1897f3c74fbSChris Mason 
190c8b97818SChris Mason 	/*
191c8b97818SChris Mason 	 * don't merge compressed extents, we need to know their
192c8b97818SChris Mason 	 * actual size
193c8b97818SChris Mason 	 */
194c8b97818SChris Mason 	if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
195c8b97818SChris Mason 		return 0;
196c8b97818SChris Mason 
197201a9038SJosef Bacik 	if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
198201a9038SJosef Bacik 	    test_bit(EXTENT_FLAG_LOGGING, &next->flags))
199201a9038SJosef Bacik 		return 0;
200201a9038SJosef Bacik 
20109a2a8f9SJosef Bacik 	/*
20209a2a8f9SJosef Bacik 	 * We don't want to merge stuff that hasn't been written to the log yet
20309a2a8f9SJosef Bacik 	 * since it may not reflect exactly what is on disk, and that would be
20409a2a8f9SJosef Bacik 	 * bad.
20509a2a8f9SJosef Bacik 	 */
20609a2a8f9SJosef Bacik 	if (!list_empty(&prev->list) || !list_empty(&next->list))
20709a2a8f9SJosef Bacik 		return 0;
20809a2a8f9SJosef Bacik 
209d1310b2eSChris Mason 	if (extent_map_end(prev) == next->start &&
210d1310b2eSChris Mason 	    prev->flags == next->flags &&
211d1310b2eSChris Mason 	    prev->bdev == next->bdev &&
212d1310b2eSChris Mason 	    ((next->block_start == EXTENT_MAP_HOLE &&
213d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_HOLE) ||
214d1310b2eSChris Mason 	     (next->block_start == EXTENT_MAP_INLINE &&
215d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_INLINE) ||
216d1310b2eSChris Mason 	     (next->block_start == EXTENT_MAP_DELALLOC &&
217d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_DELALLOC) ||
218d1310b2eSChris Mason 	     (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
219d1310b2eSChris Mason 	      next->block_start == extent_map_block_end(prev)))) {
220d1310b2eSChris Mason 		return 1;
221d1310b2eSChris Mason 	}
222a52d9a80SChris Mason 	return 0;
223a52d9a80SChris Mason }
224a52d9a80SChris Mason 
2254d2c8f62SLi Zefan static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
226a1ed835eSChris Mason {
227a1ed835eSChris Mason 	struct extent_map *merge = NULL;
228a1ed835eSChris Mason 	struct rb_node *rb;
229a1ed835eSChris Mason 
230a1ed835eSChris Mason 	if (em->start != 0) {
231a1ed835eSChris Mason 		rb = rb_prev(&em->rb_node);
232a1ed835eSChris Mason 		if (rb)
233a1ed835eSChris Mason 			merge = rb_entry(rb, struct extent_map, rb_node);
234a1ed835eSChris Mason 		if (rb && mergable_maps(merge, em)) {
235a1ed835eSChris Mason 			em->start = merge->start;
23670c8a91cSJosef Bacik 			em->orig_start = merge->orig_start;
237a1ed835eSChris Mason 			em->len += merge->len;
238a1ed835eSChris Mason 			em->block_len += merge->block_len;
239a1ed835eSChris Mason 			em->block_start = merge->block_start;
24070c8a91cSJosef Bacik 			em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
24170c8a91cSJosef Bacik 			em->mod_start = merge->mod_start;
24270c8a91cSJosef Bacik 			em->generation = max(em->generation, merge->generation);
2435dc562c5SJosef Bacik 
244a1ed835eSChris Mason 			rb_erase(&merge->rb_node, &tree->map);
245cbc0e928SFilipe Manana 			RB_CLEAR_NODE(&merge->rb_node);
246a1ed835eSChris Mason 			free_extent_map(merge);
247a1ed835eSChris Mason 		}
248a1ed835eSChris Mason 	}
249a1ed835eSChris Mason 
250a1ed835eSChris Mason 	rb = rb_next(&em->rb_node);
251a1ed835eSChris Mason 	if (rb)
252a1ed835eSChris Mason 		merge = rb_entry(rb, struct extent_map, rb_node);
253a1ed835eSChris Mason 	if (rb && mergable_maps(em, merge)) {
254a1ed835eSChris Mason 		em->len += merge->len;
255d527afe1SFilipe David Borba Manana 		em->block_len += merge->block_len;
256a1ed835eSChris Mason 		rb_erase(&merge->rb_node, &tree->map);
257cbc0e928SFilipe Manana 		RB_CLEAR_NODE(&merge->rb_node);
25870c8a91cSJosef Bacik 		em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
25970c8a91cSJosef Bacik 		em->generation = max(em->generation, merge->generation);
260a1ed835eSChris Mason 		free_extent_map(merge);
261a1ed835eSChris Mason 	}
2624d2c8f62SLi Zefan }
2634d2c8f62SLi Zefan 
2645dc562c5SJosef Bacik /**
26552b1de91SLiu Bo  * unpin_extent_cache - unpin an extent from the cache
2665dc562c5SJosef Bacik  * @tree:	tree to unpin the extent in
2675dc562c5SJosef Bacik  * @start:	logical offset in the file
2685dc562c5SJosef Bacik  * @len:	length of the extent
2695dc562c5SJosef Bacik  * @gen:	generation that this extent has been modified in
2705dc562c5SJosef Bacik  *
2715dc562c5SJosef Bacik  * Called after an extent has been written to disk properly.  Set the generation
2725dc562c5SJosef Bacik  * to the generation that actually added the file item to the inode so we know
2735dc562c5SJosef Bacik  * we need to sync this extent when we call fsync().
2745dc562c5SJosef Bacik  */
2755dc562c5SJosef Bacik int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
2765dc562c5SJosef Bacik 		       u64 gen)
2774d2c8f62SLi Zefan {
2784d2c8f62SLi Zefan 	int ret = 0;
2794d2c8f62SLi Zefan 	struct extent_map *em;
2804e2f84e6SLiu Bo 	bool prealloc = false;
2814d2c8f62SLi Zefan 
2824d2c8f62SLi Zefan 	write_lock(&tree->lock);
2834d2c8f62SLi Zefan 	em = lookup_extent_mapping(tree, start, len);
2844d2c8f62SLi Zefan 
2854d2c8f62SLi Zefan 	WARN_ON(!em || em->start != start);
2864d2c8f62SLi Zefan 
2874d2c8f62SLi Zefan 	if (!em)
2884d2c8f62SLi Zefan 		goto out;
2894d2c8f62SLi Zefan 
2905dc562c5SJosef Bacik 	em->generation = gen;
2914d2c8f62SLi Zefan 	clear_bit(EXTENT_FLAG_PINNED, &em->flags);
2924e2f84e6SLiu Bo 	em->mod_start = em->start;
2934e2f84e6SLiu Bo 	em->mod_len = em->len;
2944e2f84e6SLiu Bo 
295b11e234dSJosef Bacik 	if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
2964e2f84e6SLiu Bo 		prealloc = true;
297b11e234dSJosef Bacik 		clear_bit(EXTENT_FLAG_FILLING, &em->flags);
2984e2f84e6SLiu Bo 	}
2994d2c8f62SLi Zefan 
3004d2c8f62SLi Zefan 	try_merge_map(tree, em);
3014e2f84e6SLiu Bo 
3024e2f84e6SLiu Bo 	if (prealloc) {
3034e2f84e6SLiu Bo 		em->mod_start = em->start;
3044e2f84e6SLiu Bo 		em->mod_len = em->len;
3054e2f84e6SLiu Bo 	}
3064e2f84e6SLiu Bo 
307a1ed835eSChris Mason 	free_extent_map(em);
308a1ed835eSChris Mason out:
309a1ed835eSChris Mason 	write_unlock(&tree->lock);
310a1ed835eSChris Mason 	return ret;
311a1ed835eSChris Mason 
312a1ed835eSChris Mason }
313a1ed835eSChris Mason 
314201a9038SJosef Bacik void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
315201a9038SJosef Bacik {
316201a9038SJosef Bacik 	clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
317cbc0e928SFilipe Manana 	if (extent_map_in_tree(em))
318201a9038SJosef Bacik 		try_merge_map(tree, em);
319201a9038SJosef Bacik }
320201a9038SJosef Bacik 
321176840b3SFilipe Manana static inline void setup_extent_mapping(struct extent_map_tree *tree,
322176840b3SFilipe Manana 					struct extent_map *em,
323176840b3SFilipe Manana 					int modified)
324176840b3SFilipe Manana {
325490b54d6SElena Reshetova 	refcount_inc(&em->refs);
326176840b3SFilipe Manana 	em->mod_start = em->start;
327176840b3SFilipe Manana 	em->mod_len = em->len;
328176840b3SFilipe Manana 
329176840b3SFilipe Manana 	if (modified)
330176840b3SFilipe Manana 		list_move(&em->list, &tree->modified_extents);
331176840b3SFilipe Manana 	else
332176840b3SFilipe Manana 		try_merge_map(tree, em);
333176840b3SFilipe Manana }
334176840b3SFilipe Manana 
3359d2423c5SChristoph Hellwig /**
3369d2423c5SChristoph Hellwig  * add_extent_mapping - add new extent map to the extent tree
3379d2423c5SChristoph Hellwig  * @tree:	tree to insert new map in
3389d2423c5SChristoph Hellwig  * @em:		map to insert
3399d2423c5SChristoph Hellwig  *
3409d2423c5SChristoph Hellwig  * Insert @em into @tree or perform a simple forward/backward merge with
3419d2423c5SChristoph Hellwig  * existing mappings.  The extent_map struct passed in will be inserted
3429d2423c5SChristoph Hellwig  * into the tree directly, with an additional reference taken, or a
34325985edcSLucas De Marchi  * reference dropped if the merge attempt was successful.
344a52d9a80SChris Mason  */
345a52d9a80SChris Mason int add_extent_mapping(struct extent_map_tree *tree,
34609a2a8f9SJosef Bacik 		       struct extent_map *em, int modified)
347a52d9a80SChris Mason {
348a52d9a80SChris Mason 	int ret = 0;
349a52d9a80SChris Mason 
35032193c14SFilipe David Borba Manana 	ret = tree_insert(&tree->map, em);
35132193c14SFilipe David Borba Manana 	if (ret)
3527c2fe32aSChris Mason 		goto out;
35332193c14SFilipe David Borba Manana 
354176840b3SFilipe Manana 	setup_extent_mapping(tree, em, modified);
355a52d9a80SChris Mason out:
356a52d9a80SChris Mason 	return ret;
357a52d9a80SChris Mason }
358a52d9a80SChris Mason 
35948a3b636SEric Sandeen static struct extent_map *
36048a3b636SEric Sandeen __lookup_extent_mapping(struct extent_map_tree *tree,
361ed64f066SLi Zefan 			u64 start, u64 len, int strict)
362ed64f066SLi Zefan {
363ed64f066SLi Zefan 	struct extent_map *em;
364ed64f066SLi Zefan 	struct rb_node *rb_node;
365ed64f066SLi Zefan 	struct rb_node *prev = NULL;
366ed64f066SLi Zefan 	struct rb_node *next = NULL;
367ed64f066SLi Zefan 	u64 end = range_end(start, len);
368ed64f066SLi Zefan 
369ed64f066SLi Zefan 	rb_node = __tree_search(&tree->map, start, &prev, &next);
370ed64f066SLi Zefan 	if (!rb_node) {
371ed64f066SLi Zefan 		if (prev)
372ed64f066SLi Zefan 			rb_node = prev;
373ed64f066SLi Zefan 		else if (next)
374ed64f066SLi Zefan 			rb_node = next;
375ed64f066SLi Zefan 		else
376ed64f066SLi Zefan 			return NULL;
377ed64f066SLi Zefan 	}
378ed64f066SLi Zefan 
379ed64f066SLi Zefan 	em = rb_entry(rb_node, struct extent_map, rb_node);
380ed64f066SLi Zefan 
381ed64f066SLi Zefan 	if (strict && !(end > em->start && start < extent_map_end(em)))
382ed64f066SLi Zefan 		return NULL;
383ed64f066SLi Zefan 
384490b54d6SElena Reshetova 	refcount_inc(&em->refs);
385ed64f066SLi Zefan 	return em;
386ed64f066SLi Zefan }
387ed64f066SLi Zefan 
3889d2423c5SChristoph Hellwig /**
3899d2423c5SChristoph Hellwig  * lookup_extent_mapping - lookup extent_map
3909d2423c5SChristoph Hellwig  * @tree:	tree to lookup in
3919d2423c5SChristoph Hellwig  * @start:	byte offset to start the search
3929d2423c5SChristoph Hellwig  * @len:	length of the lookup range
3939d2423c5SChristoph Hellwig  *
3949d2423c5SChristoph Hellwig  * Find and return the first extent_map struct in @tree that intersects the
3959d2423c5SChristoph Hellwig  * [start, len] range.  There may be additional objects in the tree that
3969d2423c5SChristoph Hellwig  * intersect, so check the object returned carefully to make sure that no
3979d2423c5SChristoph Hellwig  * additional lookups are needed.
398a52d9a80SChris Mason  */
399a52d9a80SChris Mason struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
400d1310b2eSChris Mason 					 u64 start, u64 len)
401a52d9a80SChris Mason {
402ed64f066SLi Zefan 	return __lookup_extent_mapping(tree, start, len, 1);
403a52d9a80SChris Mason }
404a52d9a80SChris Mason 
4059d2423c5SChristoph Hellwig /**
406b917b7c3SChris Mason  * search_extent_mapping - find a nearby extent map
407b917b7c3SChris Mason  * @tree:	tree to lookup in
408b917b7c3SChris Mason  * @start:	byte offset to start the search
409b917b7c3SChris Mason  * @len:	length of the lookup range
410b917b7c3SChris Mason  *
411b917b7c3SChris Mason  * Find and return the first extent_map struct in @tree that intersects the
412b917b7c3SChris Mason  * [start, len] range.
413b917b7c3SChris Mason  *
414b917b7c3SChris Mason  * If one can't be found, any nearby extent may be returned
415b917b7c3SChris Mason  */
416b917b7c3SChris Mason struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
417b917b7c3SChris Mason 					 u64 start, u64 len)
418b917b7c3SChris Mason {
419ed64f066SLi Zefan 	return __lookup_extent_mapping(tree, start, len, 0);
420b917b7c3SChris Mason }
421b917b7c3SChris Mason 
422b917b7c3SChris Mason /**
4239d2423c5SChristoph Hellwig  * remove_extent_mapping - removes an extent_map from the extent tree
4249d2423c5SChristoph Hellwig  * @tree:	extent tree to remove from
425bb7ab3b9SAdam Buchbinder  * @em:		extent map being removed
4269d2423c5SChristoph Hellwig  *
4279d2423c5SChristoph Hellwig  * Removes @em from @tree.  No reference counts are dropped, and no checks
4289d2423c5SChristoph Hellwig  * are done to see if the range is in use
429a52d9a80SChris Mason  */
430a52d9a80SChris Mason int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
431a52d9a80SChris Mason {
432d1310b2eSChris Mason 	int ret = 0;
433a52d9a80SChris Mason 
4347f3c74fbSChris Mason 	WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
435d1310b2eSChris Mason 	rb_erase(&em->rb_node, &tree->map);
436ff44c6e3SJosef Bacik 	if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
4375dc562c5SJosef Bacik 		list_del_init(&em->list);
438cbc0e928SFilipe Manana 	RB_CLEAR_NODE(&em->rb_node);
439a52d9a80SChris Mason 	return ret;
440a52d9a80SChris Mason }
441176840b3SFilipe Manana 
442176840b3SFilipe Manana void replace_extent_mapping(struct extent_map_tree *tree,
443176840b3SFilipe Manana 			    struct extent_map *cur,
444176840b3SFilipe Manana 			    struct extent_map *new,
445176840b3SFilipe Manana 			    int modified)
446176840b3SFilipe Manana {
447176840b3SFilipe Manana 	WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));
448176840b3SFilipe Manana 	ASSERT(extent_map_in_tree(cur));
449176840b3SFilipe Manana 	if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))
450176840b3SFilipe Manana 		list_del_init(&cur->list);
451176840b3SFilipe Manana 	rb_replace_node(&cur->rb_node, &new->rb_node, &tree->map);
452176840b3SFilipe Manana 	RB_CLEAR_NODE(&cur->rb_node);
453176840b3SFilipe Manana 
454176840b3SFilipe Manana 	setup_extent_mapping(tree, new, modified);
455176840b3SFilipe Manana }
456c04e61b5SLiu Bo 
457c04e61b5SLiu Bo static struct extent_map *next_extent_map(struct extent_map *em)
458c04e61b5SLiu Bo {
459c04e61b5SLiu Bo 	struct rb_node *next;
460c04e61b5SLiu Bo 
461c04e61b5SLiu Bo 	next = rb_next(&em->rb_node);
462c04e61b5SLiu Bo 	if (!next)
463c04e61b5SLiu Bo 		return NULL;
464c04e61b5SLiu Bo 	return container_of(next, struct extent_map, rb_node);
465c04e61b5SLiu Bo }
466c04e61b5SLiu Bo 
467c04e61b5SLiu Bo static struct extent_map *prev_extent_map(struct extent_map *em)
468c04e61b5SLiu Bo {
469c04e61b5SLiu Bo 	struct rb_node *prev;
470c04e61b5SLiu Bo 
471c04e61b5SLiu Bo 	prev = rb_prev(&em->rb_node);
472c04e61b5SLiu Bo 	if (!prev)
473c04e61b5SLiu Bo 		return NULL;
474c04e61b5SLiu Bo 	return container_of(prev, struct extent_map, rb_node);
475c04e61b5SLiu Bo }
476c04e61b5SLiu Bo 
477c04e61b5SLiu Bo /* helper for btfs_get_extent.  Given an existing extent in the tree,
478c04e61b5SLiu Bo  * the existing extent is the nearest extent to map_start,
479c04e61b5SLiu Bo  * and an extent that you want to insert, deal with overlap and insert
480c04e61b5SLiu Bo  * the best fitted new extent into the tree.
481c04e61b5SLiu Bo  */
4825f4791f4SLiu Bo static noinline int merge_extent_mapping(struct extent_map_tree *em_tree,
483c04e61b5SLiu Bo 					 struct extent_map *existing,
484c04e61b5SLiu Bo 					 struct extent_map *em,
485c04e61b5SLiu Bo 					 u64 map_start)
486c04e61b5SLiu Bo {
487c04e61b5SLiu Bo 	struct extent_map *prev;
488c04e61b5SLiu Bo 	struct extent_map *next;
489c04e61b5SLiu Bo 	u64 start;
490c04e61b5SLiu Bo 	u64 end;
491c04e61b5SLiu Bo 	u64 start_diff;
492c04e61b5SLiu Bo 
493c04e61b5SLiu Bo 	BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
494c04e61b5SLiu Bo 
495c04e61b5SLiu Bo 	if (existing->start > map_start) {
496c04e61b5SLiu Bo 		next = existing;
497c04e61b5SLiu Bo 		prev = prev_extent_map(next);
498c04e61b5SLiu Bo 	} else {
499c04e61b5SLiu Bo 		prev = existing;
500c04e61b5SLiu Bo 		next = next_extent_map(prev);
501c04e61b5SLiu Bo 	}
502c04e61b5SLiu Bo 
503c04e61b5SLiu Bo 	start = prev ? extent_map_end(prev) : em->start;
504c04e61b5SLiu Bo 	start = max_t(u64, start, em->start);
505c04e61b5SLiu Bo 	end = next ? next->start : extent_map_end(em);
506c04e61b5SLiu Bo 	end = min_t(u64, end, extent_map_end(em));
507c04e61b5SLiu Bo 	start_diff = start - em->start;
508c04e61b5SLiu Bo 	em->start = start;
509c04e61b5SLiu Bo 	em->len = end - start;
510c04e61b5SLiu Bo 	if (em->block_start < EXTENT_MAP_LAST_BYTE &&
511c04e61b5SLiu Bo 	    !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
512c04e61b5SLiu Bo 		em->block_start += start_diff;
513c04e61b5SLiu Bo 		em->block_len = em->len;
514c04e61b5SLiu Bo 	}
515c04e61b5SLiu Bo 	return add_extent_mapping(em_tree, em, 0);
516c04e61b5SLiu Bo }
517c04e61b5SLiu Bo 
518c04e61b5SLiu Bo /**
519c04e61b5SLiu Bo  * btrfs_add_extent_mapping - add extent mapping into em_tree
520c04e61b5SLiu Bo  * @em_tree - the extent tree into which we want to insert the extent mapping
521c04e61b5SLiu Bo  * @em_in   - extent we are inserting
522c04e61b5SLiu Bo  * @start   - start of the logical range btrfs_get_extent() is requesting
523c04e61b5SLiu Bo  * @len     - length of the logical range btrfs_get_extent() is requesting
524c04e61b5SLiu Bo  *
525c04e61b5SLiu Bo  * Note that @em_in's range may be different from [start, start+len),
526c04e61b5SLiu Bo  * but they must be overlapped.
527c04e61b5SLiu Bo  *
528c04e61b5SLiu Bo  * Insert @em_in into @em_tree. In case there is an overlapping range, handle
529c04e61b5SLiu Bo  * the -EEXIST by either:
530c04e61b5SLiu Bo  * a) Returning the existing extent in @em_in if @start is within the
531c04e61b5SLiu Bo  *    existing em.
532c04e61b5SLiu Bo  * b) Merge the existing extent with @em_in passed in.
533c04e61b5SLiu Bo  *
534c04e61b5SLiu Bo  * Return 0 on success, otherwise -EEXIST.
535c04e61b5SLiu Bo  *
536c04e61b5SLiu Bo  */
537c04e61b5SLiu Bo int btrfs_add_extent_mapping(struct extent_map_tree *em_tree,
538c04e61b5SLiu Bo 			     struct extent_map **em_in, u64 start, u64 len)
539c04e61b5SLiu Bo {
540c04e61b5SLiu Bo 	int ret;
541c04e61b5SLiu Bo 	struct extent_map *em = *em_in;
542c04e61b5SLiu Bo 
543c04e61b5SLiu Bo 	ret = add_extent_mapping(em_tree, em, 0);
544c04e61b5SLiu Bo 	/* it is possible that someone inserted the extent into the tree
545c04e61b5SLiu Bo 	 * while we had the lock dropped.  It is also possible that
546c04e61b5SLiu Bo 	 * an overlapping map exists in the tree
547c04e61b5SLiu Bo 	 */
548c04e61b5SLiu Bo 	if (ret == -EEXIST) {
549c04e61b5SLiu Bo 		struct extent_map *existing;
550c04e61b5SLiu Bo 
551c04e61b5SLiu Bo 		ret = 0;
552c04e61b5SLiu Bo 
553c04e61b5SLiu Bo 		existing = search_extent_mapping(em_tree, start, len);
554*393da918SLiu Bo 
555*393da918SLiu Bo 		trace_btrfs_handle_em_exist(existing, em, start, len);
556*393da918SLiu Bo 
557c04e61b5SLiu Bo 		/*
558c04e61b5SLiu Bo 		 * existing will always be non-NULL, since there must be
559c04e61b5SLiu Bo 		 * extent causing the -EEXIST.
560c04e61b5SLiu Bo 		 */
561c04e61b5SLiu Bo 		if (start >= existing->start &&
562c04e61b5SLiu Bo 		    start < extent_map_end(existing)) {
563c04e61b5SLiu Bo 			free_extent_map(em);
564c04e61b5SLiu Bo 			*em_in = existing;
565c04e61b5SLiu Bo 			ret = 0;
566c04e61b5SLiu Bo 		} else {
5679a7e10e7SLiu Bo 			u64 orig_start = em->start;
5689a7e10e7SLiu Bo 			u64 orig_len = em->len;
5699a7e10e7SLiu Bo 
570c04e61b5SLiu Bo 			/*
571c04e61b5SLiu Bo 			 * The existing extent map is the one nearest to
572c04e61b5SLiu Bo 			 * the [start, start + len) range which overlaps
573c04e61b5SLiu Bo 			 */
574c04e61b5SLiu Bo 			ret = merge_extent_mapping(em_tree, existing,
575c04e61b5SLiu Bo 						   em, start);
576c04e61b5SLiu Bo 			if (ret) {
577c04e61b5SLiu Bo 				free_extent_map(em);
578c04e61b5SLiu Bo 				*em_in = NULL;
5799a7e10e7SLiu Bo 				WARN_ONCE(ret,
5809a7e10e7SLiu Bo "unexpected error %d: merge existing(start %llu len %llu) with em(start %llu len %llu)\n",
5819a7e10e7SLiu Bo 					  ret, existing->start, existing->len,
5829a7e10e7SLiu Bo 					  orig_start, orig_len);
583c04e61b5SLiu Bo 			}
5849a7e10e7SLiu Bo 			free_extent_map(existing);
585c04e61b5SLiu Bo 		}
586c04e61b5SLiu Bo 	}
587c04e61b5SLiu Bo 
588c04e61b5SLiu Bo 	ASSERT(ret == 0 || ret == -EEXIST);
589c04e61b5SLiu Bo 	return ret;
590c04e61b5SLiu Bo }
591