xref: /openbmc/linux/fs/btrfs/extent_map.c (revision c04e61b5e41b0e8ace4aa4b67685fbe68ac37a46)
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>
5d1310b2eSChris Mason #include <linux/hardirq.h>
6261507a0SLi Zefan #include "ctree.h"
7a52d9a80SChris Mason #include "extent_map.h"
8ebb8765bSAnand Jain #include "compression.h"
9a52d9a80SChris Mason 
1086479a04SChris Mason 
11a52d9a80SChris Mason static struct kmem_cache *extent_map_cache;
12ca664626SChris Mason 
132f4cbe64SWyatt Banks int __init extent_map_init(void)
14a52d9a80SChris Mason {
15837e1972SDavid Sterba 	extent_map_cache = kmem_cache_create("btrfs_extent_map",
166d36dcd4SChris Mason 			sizeof(struct extent_map), 0,
17fba4b697SNikolay Borisov 			SLAB_MEM_SPREAD, NULL);
182f4cbe64SWyatt Banks 	if (!extent_map_cache)
192f4cbe64SWyatt Banks 		return -ENOMEM;
202f4cbe64SWyatt Banks 	return 0;
21a52d9a80SChris Mason }
22a52d9a80SChris Mason 
2317636e03SChristian Hesse void extent_map_exit(void)
24a52d9a80SChris Mason {
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;
5270c8a91cSJosef Bacik 	em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS);
53c26a9203STsutomu Itoh 	if (!em)
54c26a9203STsutomu Itoh 		return NULL;
55cbc0e928SFilipe Manana 	RB_CLEAR_NODE(&em->rb_node);
56d1310b2eSChris Mason 	em->flags = 0;
57261507a0SLi Zefan 	em->compress_type = BTRFS_COMPRESS_NONE;
585dc562c5SJosef Bacik 	em->generation = 0;
59490b54d6SElena Reshetova 	refcount_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
6601327610SNicholas D Steeves  * @em:		extent map being released
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;
75490b54d6SElena Reshetova 	WARN_ON(refcount_read(&em->refs) == 0);
76490b54d6SElena Reshetova 	if (refcount_dec_and_test(&em->refs)) {
77cbc0e928SFilipe Manana 		WARN_ON(extent_map_in_tree(em));
785dc562c5SJosef Bacik 		WARN_ON(!list_empty(&em->list));
79298a8f9cSWang Shilong 		if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
8095617d69SJeff Mahoney 			kfree(em->map_lookup);
81a52d9a80SChris Mason 		kmem_cache_free(extent_map_cache, em);
82a52d9a80SChris Mason 	}
83a52d9a80SChris Mason }
84a52d9a80SChris Mason 
8532193c14SFilipe David Borba Manana /* simple helper to do math around the end of an extent, handling wrap */
8632193c14SFilipe David Borba Manana static u64 range_end(u64 start, u64 len)
8732193c14SFilipe David Borba Manana {
8832193c14SFilipe David Borba Manana 	if (start + len < start)
8932193c14SFilipe David Borba Manana 		return (u64)-1;
9032193c14SFilipe David Borba Manana 	return start + len;
9132193c14SFilipe David Borba Manana }
9232193c14SFilipe David Borba Manana 
9332193c14SFilipe David Borba Manana static int tree_insert(struct rb_root *root, struct extent_map *em)
94a52d9a80SChris Mason {
95a52d9a80SChris Mason 	struct rb_node **p = &root->rb_node;
96a52d9a80SChris Mason 	struct rb_node *parent = NULL;
9732193c14SFilipe David Borba Manana 	struct extent_map *entry = NULL;
9832193c14SFilipe David Borba Manana 	struct rb_node *orig_parent = NULL;
9932193c14SFilipe David Borba Manana 	u64 end = range_end(em->start, em->len);
100a52d9a80SChris Mason 
101a52d9a80SChris Mason 	while (*p) {
102a52d9a80SChris Mason 		parent = *p;
103d1310b2eSChris Mason 		entry = rb_entry(parent, struct extent_map, rb_node);
104d1310b2eSChris Mason 
10532193c14SFilipe David Borba Manana 		if (em->start < entry->start)
106a52d9a80SChris Mason 			p = &(*p)->rb_left;
10732193c14SFilipe David Borba Manana 		else if (em->start >= extent_map_end(entry))
108a52d9a80SChris Mason 			p = &(*p)->rb_right;
109a52d9a80SChris Mason 		else
11032193c14SFilipe David Borba Manana 			return -EEXIST;
111a52d9a80SChris Mason 	}
112a52d9a80SChris Mason 
11332193c14SFilipe David Borba Manana 	orig_parent = parent;
11432193c14SFilipe David Borba Manana 	while (parent && em->start >= extent_map_end(entry)) {
11532193c14SFilipe David Borba Manana 		parent = rb_next(parent);
11632193c14SFilipe David Borba Manana 		entry = rb_entry(parent, struct extent_map, rb_node);
11732193c14SFilipe David Borba Manana 	}
11832193c14SFilipe David Borba Manana 	if (parent)
11932193c14SFilipe David Borba Manana 		if (end > entry->start && em->start < extent_map_end(entry))
12032193c14SFilipe David Borba Manana 			return -EEXIST;
12132193c14SFilipe David Borba Manana 
12232193c14SFilipe David Borba Manana 	parent = orig_parent;
12332193c14SFilipe David Borba Manana 	entry = rb_entry(parent, struct extent_map, rb_node);
12432193c14SFilipe David Borba Manana 	while (parent && em->start < entry->start) {
12532193c14SFilipe David Borba Manana 		parent = rb_prev(parent);
12632193c14SFilipe David Borba Manana 		entry = rb_entry(parent, struct extent_map, rb_node);
12732193c14SFilipe David Borba Manana 	}
12832193c14SFilipe David Borba Manana 	if (parent)
12932193c14SFilipe David Borba Manana 		if (end > entry->start && em->start < extent_map_end(entry))
13032193c14SFilipe David Borba Manana 			return -EEXIST;
13132193c14SFilipe David Borba Manana 
13232193c14SFilipe David Borba Manana 	rb_link_node(&em->rb_node, orig_parent, p);
13332193c14SFilipe David Borba Manana 	rb_insert_color(&em->rb_node, root);
13432193c14SFilipe David Borba Manana 	return 0;
135a52d9a80SChris Mason }
136a52d9a80SChris Mason 
137d352ac68SChris Mason /*
138d352ac68SChris Mason  * search through the tree for an extent_map with a given offset.  If
139d352ac68SChris Mason  * it can't be found, try to find some neighboring extents
140d352ac68SChris Mason  */
141a52d9a80SChris Mason static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
1425f56406aSChris Mason 				     struct rb_node **prev_ret,
1435f56406aSChris Mason 				     struct rb_node **next_ret)
144a52d9a80SChris Mason {
145a52d9a80SChris Mason 	struct rb_node *n = root->rb_node;
146a52d9a80SChris Mason 	struct rb_node *prev = NULL;
1475f56406aSChris Mason 	struct rb_node *orig_prev = NULL;
148d1310b2eSChris Mason 	struct extent_map *entry;
149d1310b2eSChris Mason 	struct extent_map *prev_entry = NULL;
150a52d9a80SChris Mason 
151a52d9a80SChris Mason 	while (n) {
152d1310b2eSChris Mason 		entry = rb_entry(n, struct extent_map, rb_node);
153a52d9a80SChris Mason 		prev = n;
154a52d9a80SChris Mason 		prev_entry = entry;
155a52d9a80SChris Mason 
156a52d9a80SChris Mason 		if (offset < entry->start)
157a52d9a80SChris Mason 			n = n->rb_left;
158d1310b2eSChris Mason 		else if (offset >= extent_map_end(entry))
159a52d9a80SChris Mason 			n = n->rb_right;
160a52d9a80SChris Mason 		else
161a52d9a80SChris Mason 			return n;
162a52d9a80SChris Mason 	}
1635f56406aSChris Mason 
1645f56406aSChris Mason 	if (prev_ret) {
1655f56406aSChris Mason 		orig_prev = prev;
166d1310b2eSChris Mason 		while (prev && offset >= extent_map_end(prev_entry)) {
167a52d9a80SChris Mason 			prev = rb_next(prev);
168d1310b2eSChris Mason 			prev_entry = rb_entry(prev, struct extent_map, rb_node);
169a52d9a80SChris Mason 		}
170a52d9a80SChris Mason 		*prev_ret = prev;
1715f56406aSChris Mason 		prev = orig_prev;
1725f56406aSChris Mason 	}
1735f56406aSChris Mason 
1745f56406aSChris Mason 	if (next_ret) {
175d1310b2eSChris Mason 		prev_entry = rb_entry(prev, struct extent_map, rb_node);
1765f56406aSChris Mason 		while (prev && offset < prev_entry->start) {
1775f56406aSChris Mason 			prev = rb_prev(prev);
178d1310b2eSChris Mason 			prev_entry = rb_entry(prev, struct extent_map, rb_node);
1795f56406aSChris Mason 		}
1805f56406aSChris Mason 		*next_ret = prev;
1815f56406aSChris Mason 	}
182a52d9a80SChris Mason 	return NULL;
183a52d9a80SChris Mason }
184a52d9a80SChris Mason 
185d352ac68SChris Mason /* check to see if two extent_map structs are adjacent and safe to merge */
186d1310b2eSChris Mason static int mergable_maps(struct extent_map *prev, struct extent_map *next)
187a52d9a80SChris Mason {
1887f3c74fbSChris Mason 	if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
1897f3c74fbSChris Mason 		return 0;
1907f3c74fbSChris Mason 
191c8b97818SChris Mason 	/*
192c8b97818SChris Mason 	 * don't merge compressed extents, we need to know their
193c8b97818SChris Mason 	 * actual size
194c8b97818SChris Mason 	 */
195c8b97818SChris Mason 	if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
196c8b97818SChris Mason 		return 0;
197c8b97818SChris Mason 
198201a9038SJosef Bacik 	if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
199201a9038SJosef Bacik 	    test_bit(EXTENT_FLAG_LOGGING, &next->flags))
200201a9038SJosef Bacik 		return 0;
201201a9038SJosef Bacik 
20209a2a8f9SJosef Bacik 	/*
20309a2a8f9SJosef Bacik 	 * We don't want to merge stuff that hasn't been written to the log yet
20409a2a8f9SJosef Bacik 	 * since it may not reflect exactly what is on disk, and that would be
20509a2a8f9SJosef Bacik 	 * bad.
20609a2a8f9SJosef Bacik 	 */
20709a2a8f9SJosef Bacik 	if (!list_empty(&prev->list) || !list_empty(&next->list))
20809a2a8f9SJosef Bacik 		return 0;
20909a2a8f9SJosef Bacik 
210d1310b2eSChris Mason 	if (extent_map_end(prev) == next->start &&
211d1310b2eSChris Mason 	    prev->flags == next->flags &&
212d1310b2eSChris Mason 	    prev->bdev == next->bdev &&
213d1310b2eSChris Mason 	    ((next->block_start == EXTENT_MAP_HOLE &&
214d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_HOLE) ||
215d1310b2eSChris Mason 	     (next->block_start == EXTENT_MAP_INLINE &&
216d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_INLINE) ||
217d1310b2eSChris Mason 	     (next->block_start == EXTENT_MAP_DELALLOC &&
218d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_DELALLOC) ||
219d1310b2eSChris Mason 	     (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
220d1310b2eSChris Mason 	      next->block_start == extent_map_block_end(prev)))) {
221d1310b2eSChris Mason 		return 1;
222d1310b2eSChris Mason 	}
223a52d9a80SChris Mason 	return 0;
224a52d9a80SChris Mason }
225a52d9a80SChris Mason 
2264d2c8f62SLi Zefan static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
227a1ed835eSChris Mason {
228a1ed835eSChris Mason 	struct extent_map *merge = NULL;
229a1ed835eSChris Mason 	struct rb_node *rb;
230a1ed835eSChris Mason 
231a1ed835eSChris Mason 	if (em->start != 0) {
232a1ed835eSChris Mason 		rb = rb_prev(&em->rb_node);
233a1ed835eSChris Mason 		if (rb)
234a1ed835eSChris Mason 			merge = rb_entry(rb, struct extent_map, rb_node);
235a1ed835eSChris Mason 		if (rb && mergable_maps(merge, em)) {
236a1ed835eSChris Mason 			em->start = merge->start;
23770c8a91cSJosef Bacik 			em->orig_start = merge->orig_start;
238a1ed835eSChris Mason 			em->len += merge->len;
239a1ed835eSChris Mason 			em->block_len += merge->block_len;
240a1ed835eSChris Mason 			em->block_start = merge->block_start;
24170c8a91cSJosef Bacik 			em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
24270c8a91cSJosef Bacik 			em->mod_start = merge->mod_start;
24370c8a91cSJosef Bacik 			em->generation = max(em->generation, merge->generation);
2445dc562c5SJosef Bacik 
245a1ed835eSChris Mason 			rb_erase(&merge->rb_node, &tree->map);
246cbc0e928SFilipe Manana 			RB_CLEAR_NODE(&merge->rb_node);
247a1ed835eSChris Mason 			free_extent_map(merge);
248a1ed835eSChris Mason 		}
249a1ed835eSChris Mason 	}
250a1ed835eSChris Mason 
251a1ed835eSChris Mason 	rb = rb_next(&em->rb_node);
252a1ed835eSChris Mason 	if (rb)
253a1ed835eSChris Mason 		merge = rb_entry(rb, struct extent_map, rb_node);
254a1ed835eSChris Mason 	if (rb && mergable_maps(em, merge)) {
255a1ed835eSChris Mason 		em->len += merge->len;
256d527afe1SFilipe David Borba Manana 		em->block_len += merge->block_len;
257a1ed835eSChris Mason 		rb_erase(&merge->rb_node, &tree->map);
258cbc0e928SFilipe Manana 		RB_CLEAR_NODE(&merge->rb_node);
25970c8a91cSJosef Bacik 		em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
26070c8a91cSJosef Bacik 		em->generation = max(em->generation, merge->generation);
261a1ed835eSChris Mason 		free_extent_map(merge);
262a1ed835eSChris Mason 	}
2634d2c8f62SLi Zefan }
2644d2c8f62SLi Zefan 
2655dc562c5SJosef Bacik /**
26652b1de91SLiu Bo  * unpin_extent_cache - unpin an extent from the cache
2675dc562c5SJosef Bacik  * @tree:	tree to unpin the extent in
2685dc562c5SJosef Bacik  * @start:	logical offset in the file
2695dc562c5SJosef Bacik  * @len:	length of the extent
2705dc562c5SJosef Bacik  * @gen:	generation that this extent has been modified in
2715dc562c5SJosef Bacik  *
2725dc562c5SJosef Bacik  * Called after an extent has been written to disk properly.  Set the generation
2735dc562c5SJosef Bacik  * to the generation that actually added the file item to the inode so we know
2745dc562c5SJosef Bacik  * we need to sync this extent when we call fsync().
2755dc562c5SJosef Bacik  */
2765dc562c5SJosef Bacik int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
2775dc562c5SJosef Bacik 		       u64 gen)
2784d2c8f62SLi Zefan {
2794d2c8f62SLi Zefan 	int ret = 0;
2804d2c8f62SLi Zefan 	struct extent_map *em;
2814e2f84e6SLiu Bo 	bool prealloc = false;
2824d2c8f62SLi Zefan 
2834d2c8f62SLi Zefan 	write_lock(&tree->lock);
2844d2c8f62SLi Zefan 	em = lookup_extent_mapping(tree, start, len);
2854d2c8f62SLi Zefan 
2864d2c8f62SLi Zefan 	WARN_ON(!em || em->start != start);
2874d2c8f62SLi Zefan 
2884d2c8f62SLi Zefan 	if (!em)
2894d2c8f62SLi Zefan 		goto out;
2904d2c8f62SLi Zefan 
2915dc562c5SJosef Bacik 	em->generation = gen;
2924d2c8f62SLi Zefan 	clear_bit(EXTENT_FLAG_PINNED, &em->flags);
2934e2f84e6SLiu Bo 	em->mod_start = em->start;
2944e2f84e6SLiu Bo 	em->mod_len = em->len;
2954e2f84e6SLiu Bo 
296b11e234dSJosef Bacik 	if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
2974e2f84e6SLiu Bo 		prealloc = true;
298b11e234dSJosef Bacik 		clear_bit(EXTENT_FLAG_FILLING, &em->flags);
2994e2f84e6SLiu Bo 	}
3004d2c8f62SLi Zefan 
3014d2c8f62SLi Zefan 	try_merge_map(tree, em);
3024e2f84e6SLiu Bo 
3034e2f84e6SLiu Bo 	if (prealloc) {
3044e2f84e6SLiu Bo 		em->mod_start = em->start;
3054e2f84e6SLiu Bo 		em->mod_len = em->len;
3064e2f84e6SLiu Bo 	}
3074e2f84e6SLiu Bo 
308a1ed835eSChris Mason 	free_extent_map(em);
309a1ed835eSChris Mason out:
310a1ed835eSChris Mason 	write_unlock(&tree->lock);
311a1ed835eSChris Mason 	return ret;
312a1ed835eSChris Mason 
313a1ed835eSChris Mason }
314a1ed835eSChris Mason 
315201a9038SJosef Bacik void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
316201a9038SJosef Bacik {
317201a9038SJosef Bacik 	clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
318cbc0e928SFilipe Manana 	if (extent_map_in_tree(em))
319201a9038SJosef Bacik 		try_merge_map(tree, em);
320201a9038SJosef Bacik }
321201a9038SJosef Bacik 
322176840b3SFilipe Manana static inline void setup_extent_mapping(struct extent_map_tree *tree,
323176840b3SFilipe Manana 					struct extent_map *em,
324176840b3SFilipe Manana 					int modified)
325176840b3SFilipe Manana {
326490b54d6SElena Reshetova 	refcount_inc(&em->refs);
327176840b3SFilipe Manana 	em->mod_start = em->start;
328176840b3SFilipe Manana 	em->mod_len = em->len;
329176840b3SFilipe Manana 
330176840b3SFilipe Manana 	if (modified)
331176840b3SFilipe Manana 		list_move(&em->list, &tree->modified_extents);
332176840b3SFilipe Manana 	else
333176840b3SFilipe Manana 		try_merge_map(tree, em);
334176840b3SFilipe Manana }
335176840b3SFilipe Manana 
3369d2423c5SChristoph Hellwig /**
3379d2423c5SChristoph Hellwig  * add_extent_mapping - add new extent map to the extent tree
3389d2423c5SChristoph Hellwig  * @tree:	tree to insert new map in
3399d2423c5SChristoph Hellwig  * @em:		map to insert
3409d2423c5SChristoph Hellwig  *
3419d2423c5SChristoph Hellwig  * Insert @em into @tree or perform a simple forward/backward merge with
3429d2423c5SChristoph Hellwig  * existing mappings.  The extent_map struct passed in will be inserted
3439d2423c5SChristoph Hellwig  * into the tree directly, with an additional reference taken, or a
34425985edcSLucas De Marchi  * reference dropped if the merge attempt was successful.
345a52d9a80SChris Mason  */
346a52d9a80SChris Mason int add_extent_mapping(struct extent_map_tree *tree,
34709a2a8f9SJosef Bacik 		       struct extent_map *em, int modified)
348a52d9a80SChris Mason {
349a52d9a80SChris Mason 	int ret = 0;
350a52d9a80SChris Mason 
35132193c14SFilipe David Borba Manana 	ret = tree_insert(&tree->map, em);
35232193c14SFilipe David Borba Manana 	if (ret)
3537c2fe32aSChris Mason 		goto out;
35432193c14SFilipe David Borba Manana 
355176840b3SFilipe Manana 	setup_extent_mapping(tree, em, modified);
356a52d9a80SChris Mason out:
357a52d9a80SChris Mason 	return ret;
358a52d9a80SChris Mason }
359a52d9a80SChris Mason 
36048a3b636SEric Sandeen static struct extent_map *
36148a3b636SEric Sandeen __lookup_extent_mapping(struct extent_map_tree *tree,
362ed64f066SLi Zefan 			u64 start, u64 len, int strict)
363ed64f066SLi Zefan {
364ed64f066SLi Zefan 	struct extent_map *em;
365ed64f066SLi Zefan 	struct rb_node *rb_node;
366ed64f066SLi Zefan 	struct rb_node *prev = NULL;
367ed64f066SLi Zefan 	struct rb_node *next = NULL;
368ed64f066SLi Zefan 	u64 end = range_end(start, len);
369ed64f066SLi Zefan 
370ed64f066SLi Zefan 	rb_node = __tree_search(&tree->map, start, &prev, &next);
371ed64f066SLi Zefan 	if (!rb_node) {
372ed64f066SLi Zefan 		if (prev)
373ed64f066SLi Zefan 			rb_node = prev;
374ed64f066SLi Zefan 		else if (next)
375ed64f066SLi Zefan 			rb_node = next;
376ed64f066SLi Zefan 		else
377ed64f066SLi Zefan 			return NULL;
378ed64f066SLi Zefan 	}
379ed64f066SLi Zefan 
380ed64f066SLi Zefan 	em = rb_entry(rb_node, struct extent_map, rb_node);
381ed64f066SLi Zefan 
382ed64f066SLi Zefan 	if (strict && !(end > em->start && start < extent_map_end(em)))
383ed64f066SLi Zefan 		return NULL;
384ed64f066SLi Zefan 
385490b54d6SElena Reshetova 	refcount_inc(&em->refs);
386ed64f066SLi Zefan 	return em;
387ed64f066SLi Zefan }
388ed64f066SLi Zefan 
3899d2423c5SChristoph Hellwig /**
3909d2423c5SChristoph Hellwig  * lookup_extent_mapping - lookup extent_map
3919d2423c5SChristoph Hellwig  * @tree:	tree to lookup in
3929d2423c5SChristoph Hellwig  * @start:	byte offset to start the search
3939d2423c5SChristoph Hellwig  * @len:	length of the lookup range
3949d2423c5SChristoph Hellwig  *
3959d2423c5SChristoph Hellwig  * Find and return the first extent_map struct in @tree that intersects the
3969d2423c5SChristoph Hellwig  * [start, len] range.  There may be additional objects in the tree that
3979d2423c5SChristoph Hellwig  * intersect, so check the object returned carefully to make sure that no
3989d2423c5SChristoph Hellwig  * additional lookups are needed.
399a52d9a80SChris Mason  */
400a52d9a80SChris Mason struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
401d1310b2eSChris Mason 					 u64 start, u64 len)
402a52d9a80SChris Mason {
403ed64f066SLi Zefan 	return __lookup_extent_mapping(tree, start, len, 1);
404a52d9a80SChris Mason }
405a52d9a80SChris Mason 
4069d2423c5SChristoph Hellwig /**
407b917b7c3SChris Mason  * search_extent_mapping - find a nearby extent map
408b917b7c3SChris Mason  * @tree:	tree to lookup in
409b917b7c3SChris Mason  * @start:	byte offset to start the search
410b917b7c3SChris Mason  * @len:	length of the lookup range
411b917b7c3SChris Mason  *
412b917b7c3SChris Mason  * Find and return the first extent_map struct in @tree that intersects the
413b917b7c3SChris Mason  * [start, len] range.
414b917b7c3SChris Mason  *
415b917b7c3SChris Mason  * If one can't be found, any nearby extent may be returned
416b917b7c3SChris Mason  */
417b917b7c3SChris Mason struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
418b917b7c3SChris Mason 					 u64 start, u64 len)
419b917b7c3SChris Mason {
420ed64f066SLi Zefan 	return __lookup_extent_mapping(tree, start, len, 0);
421b917b7c3SChris Mason }
422b917b7c3SChris Mason 
423b917b7c3SChris Mason /**
4249d2423c5SChristoph Hellwig  * remove_extent_mapping - removes an extent_map from the extent tree
4259d2423c5SChristoph Hellwig  * @tree:	extent tree to remove from
426bb7ab3b9SAdam Buchbinder  * @em:		extent map being removed
4279d2423c5SChristoph Hellwig  *
4289d2423c5SChristoph Hellwig  * Removes @em from @tree.  No reference counts are dropped, and no checks
4299d2423c5SChristoph Hellwig  * are done to see if the range is in use
430a52d9a80SChris Mason  */
431a52d9a80SChris Mason int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
432a52d9a80SChris Mason {
433d1310b2eSChris Mason 	int ret = 0;
434a52d9a80SChris Mason 
4357f3c74fbSChris Mason 	WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
436d1310b2eSChris Mason 	rb_erase(&em->rb_node, &tree->map);
437ff44c6e3SJosef Bacik 	if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
4385dc562c5SJosef Bacik 		list_del_init(&em->list);
439cbc0e928SFilipe Manana 	RB_CLEAR_NODE(&em->rb_node);
440a52d9a80SChris Mason 	return ret;
441a52d9a80SChris Mason }
442176840b3SFilipe Manana 
443176840b3SFilipe Manana void replace_extent_mapping(struct extent_map_tree *tree,
444176840b3SFilipe Manana 			    struct extent_map *cur,
445176840b3SFilipe Manana 			    struct extent_map *new,
446176840b3SFilipe Manana 			    int modified)
447176840b3SFilipe Manana {
448176840b3SFilipe Manana 	WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));
449176840b3SFilipe Manana 	ASSERT(extent_map_in_tree(cur));
450176840b3SFilipe Manana 	if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))
451176840b3SFilipe Manana 		list_del_init(&cur->list);
452176840b3SFilipe Manana 	rb_replace_node(&cur->rb_node, &new->rb_node, &tree->map);
453176840b3SFilipe Manana 	RB_CLEAR_NODE(&cur->rb_node);
454176840b3SFilipe Manana 
455176840b3SFilipe Manana 	setup_extent_mapping(tree, new, modified);
456176840b3SFilipe Manana }
457*c04e61b5SLiu Bo 
458*c04e61b5SLiu Bo static struct extent_map *next_extent_map(struct extent_map *em)
459*c04e61b5SLiu Bo {
460*c04e61b5SLiu Bo 	struct rb_node *next;
461*c04e61b5SLiu Bo 
462*c04e61b5SLiu Bo 	next = rb_next(&em->rb_node);
463*c04e61b5SLiu Bo 	if (!next)
464*c04e61b5SLiu Bo 		return NULL;
465*c04e61b5SLiu Bo 	return container_of(next, struct extent_map, rb_node);
466*c04e61b5SLiu Bo }
467*c04e61b5SLiu Bo 
468*c04e61b5SLiu Bo static struct extent_map *prev_extent_map(struct extent_map *em)
469*c04e61b5SLiu Bo {
470*c04e61b5SLiu Bo 	struct rb_node *prev;
471*c04e61b5SLiu Bo 
472*c04e61b5SLiu Bo 	prev = rb_prev(&em->rb_node);
473*c04e61b5SLiu Bo 	if (!prev)
474*c04e61b5SLiu Bo 		return NULL;
475*c04e61b5SLiu Bo 	return container_of(prev, struct extent_map, rb_node);
476*c04e61b5SLiu Bo }
477*c04e61b5SLiu Bo 
478*c04e61b5SLiu Bo /* helper for btfs_get_extent.  Given an existing extent in the tree,
479*c04e61b5SLiu Bo  * the existing extent is the nearest extent to map_start,
480*c04e61b5SLiu Bo  * and an extent that you want to insert, deal with overlap and insert
481*c04e61b5SLiu Bo  * the best fitted new extent into the tree.
482*c04e61b5SLiu Bo  */
483*c04e61b5SLiu Bo static int merge_extent_mapping(struct extent_map_tree *em_tree,
484*c04e61b5SLiu Bo 				struct extent_map *existing,
485*c04e61b5SLiu Bo 				struct extent_map *em,
486*c04e61b5SLiu Bo 				u64 map_start)
487*c04e61b5SLiu Bo {
488*c04e61b5SLiu Bo 	struct extent_map *prev;
489*c04e61b5SLiu Bo 	struct extent_map *next;
490*c04e61b5SLiu Bo 	u64 start;
491*c04e61b5SLiu Bo 	u64 end;
492*c04e61b5SLiu Bo 	u64 start_diff;
493*c04e61b5SLiu Bo 
494*c04e61b5SLiu Bo 	BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
495*c04e61b5SLiu Bo 
496*c04e61b5SLiu Bo 	if (existing->start > map_start) {
497*c04e61b5SLiu Bo 		next = existing;
498*c04e61b5SLiu Bo 		prev = prev_extent_map(next);
499*c04e61b5SLiu Bo 	} else {
500*c04e61b5SLiu Bo 		prev = existing;
501*c04e61b5SLiu Bo 		next = next_extent_map(prev);
502*c04e61b5SLiu Bo 	}
503*c04e61b5SLiu Bo 
504*c04e61b5SLiu Bo 	start = prev ? extent_map_end(prev) : em->start;
505*c04e61b5SLiu Bo 	start = max_t(u64, start, em->start);
506*c04e61b5SLiu Bo 	end = next ? next->start : extent_map_end(em);
507*c04e61b5SLiu Bo 	end = min_t(u64, end, extent_map_end(em));
508*c04e61b5SLiu Bo 	start_diff = start - em->start;
509*c04e61b5SLiu Bo 	em->start = start;
510*c04e61b5SLiu Bo 	em->len = end - start;
511*c04e61b5SLiu Bo 	if (em->block_start < EXTENT_MAP_LAST_BYTE &&
512*c04e61b5SLiu Bo 	    !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
513*c04e61b5SLiu Bo 		em->block_start += start_diff;
514*c04e61b5SLiu Bo 		em->block_len = em->len;
515*c04e61b5SLiu Bo 	}
516*c04e61b5SLiu Bo 	return add_extent_mapping(em_tree, em, 0);
517*c04e61b5SLiu Bo }
518*c04e61b5SLiu Bo 
519*c04e61b5SLiu Bo /**
520*c04e61b5SLiu Bo  * btrfs_add_extent_mapping - add extent mapping into em_tree
521*c04e61b5SLiu Bo  * @em_tree - the extent tree into which we want to insert the extent mapping
522*c04e61b5SLiu Bo  * @em_in   - extent we are inserting
523*c04e61b5SLiu Bo  * @start   - start of the logical range btrfs_get_extent() is requesting
524*c04e61b5SLiu Bo  * @len     - length of the logical range btrfs_get_extent() is requesting
525*c04e61b5SLiu Bo  *
526*c04e61b5SLiu Bo  * Note that @em_in's range may be different from [start, start+len),
527*c04e61b5SLiu Bo  * but they must be overlapped.
528*c04e61b5SLiu Bo  *
529*c04e61b5SLiu Bo  * Insert @em_in into @em_tree. In case there is an overlapping range, handle
530*c04e61b5SLiu Bo  * the -EEXIST by either:
531*c04e61b5SLiu Bo  * a) Returning the existing extent in @em_in if @start is within the
532*c04e61b5SLiu Bo  *    existing em.
533*c04e61b5SLiu Bo  * b) Merge the existing extent with @em_in passed in.
534*c04e61b5SLiu Bo  *
535*c04e61b5SLiu Bo  * Return 0 on success, otherwise -EEXIST.
536*c04e61b5SLiu Bo  *
537*c04e61b5SLiu Bo  */
538*c04e61b5SLiu Bo int btrfs_add_extent_mapping(struct extent_map_tree *em_tree,
539*c04e61b5SLiu Bo 			     struct extent_map **em_in, u64 start, u64 len)
540*c04e61b5SLiu Bo {
541*c04e61b5SLiu Bo 	int ret;
542*c04e61b5SLiu Bo 	struct extent_map *em = *em_in;
543*c04e61b5SLiu Bo 
544*c04e61b5SLiu Bo 	ret = add_extent_mapping(em_tree, em, 0);
545*c04e61b5SLiu Bo 	/* it is possible that someone inserted the extent into the tree
546*c04e61b5SLiu Bo 	 * while we had the lock dropped.  It is also possible that
547*c04e61b5SLiu Bo 	 * an overlapping map exists in the tree
548*c04e61b5SLiu Bo 	 */
549*c04e61b5SLiu Bo 	if (ret == -EEXIST) {
550*c04e61b5SLiu Bo 		struct extent_map *existing;
551*c04e61b5SLiu Bo 
552*c04e61b5SLiu Bo 		ret = 0;
553*c04e61b5SLiu Bo 
554*c04e61b5SLiu Bo 		existing = search_extent_mapping(em_tree, start, len);
555*c04e61b5SLiu Bo 		/*
556*c04e61b5SLiu Bo 		 * existing will always be non-NULL, since there must be
557*c04e61b5SLiu Bo 		 * extent causing the -EEXIST.
558*c04e61b5SLiu Bo 		 */
559*c04e61b5SLiu Bo 		if (start >= existing->start &&
560*c04e61b5SLiu Bo 		    start < extent_map_end(existing)) {
561*c04e61b5SLiu Bo 			free_extent_map(em);
562*c04e61b5SLiu Bo 			*em_in = existing;
563*c04e61b5SLiu Bo 			ret = 0;
564*c04e61b5SLiu Bo 		} else {
565*c04e61b5SLiu Bo 			/*
566*c04e61b5SLiu Bo 			 * The existing extent map is the one nearest to
567*c04e61b5SLiu Bo 			 * the [start, start + len) range which overlaps
568*c04e61b5SLiu Bo 			 */
569*c04e61b5SLiu Bo 			ret = merge_extent_mapping(em_tree, existing,
570*c04e61b5SLiu Bo 						   em, start);
571*c04e61b5SLiu Bo 			free_extent_map(existing);
572*c04e61b5SLiu Bo 			if (ret) {
573*c04e61b5SLiu Bo 				free_extent_map(em);
574*c04e61b5SLiu Bo 				*em_in = NULL;
575*c04e61b5SLiu Bo 			}
576*c04e61b5SLiu Bo 		}
577*c04e61b5SLiu Bo 	}
578*c04e61b5SLiu Bo 
579*c04e61b5SLiu Bo 	ASSERT(ret == 0 || ret == -EEXIST);
580*c04e61b5SLiu Bo 	return ret;
581*c04e61b5SLiu Bo }
582