xref: /openbmc/linux/fs/btrfs/extent_map.c (revision 07e1ce096db3605f3e0c98695df66a51e2be9f05)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2c1d7c514SDavid Sterba 
3d1310b2eSChris Mason #include <linux/err.h>
4d1310b2eSChris Mason #include <linux/slab.h>
5a52d9a80SChris Mason #include <linux/spinlock.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 
23e67c718bSDavid Sterba void __cold 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 {
37*07e1ce09SLiu Bo 	tree->map = RB_ROOT_CACHED;
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 
93*07e1ce09SLiu Bo static int tree_insert(struct rb_root_cached *root, struct extent_map *em)
94a52d9a80SChris Mason {
95*07e1ce09SLiu Bo 	struct rb_node **p = &root->rb_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);
100*07e1ce09SLiu Bo 	bool leftmost = true;
101a52d9a80SChris Mason 
102a52d9a80SChris Mason 	while (*p) {
103a52d9a80SChris Mason 		parent = *p;
104d1310b2eSChris Mason 		entry = rb_entry(parent, struct extent_map, rb_node);
105d1310b2eSChris Mason 
106*07e1ce09SLiu Bo 		if (em->start < entry->start) {
107a52d9a80SChris Mason 			p = &(*p)->rb_left;
108*07e1ce09SLiu Bo 		} else if (em->start >= extent_map_end(entry)) {
109a52d9a80SChris Mason 			p = &(*p)->rb_right;
110*07e1ce09SLiu Bo 			leftmost = false;
111*07e1ce09SLiu Bo 		} else {
11232193c14SFilipe David Borba Manana 			return -EEXIST;
113a52d9a80SChris Mason 		}
114*07e1ce09SLiu Bo 	}
115a52d9a80SChris Mason 
11632193c14SFilipe David Borba Manana 	orig_parent = parent;
11732193c14SFilipe David Borba Manana 	while (parent && em->start >= extent_map_end(entry)) {
11832193c14SFilipe David Borba Manana 		parent = rb_next(parent);
11932193c14SFilipe David Borba Manana 		entry = rb_entry(parent, struct extent_map, rb_node);
12032193c14SFilipe David Borba Manana 	}
12132193c14SFilipe David Borba Manana 	if (parent)
12232193c14SFilipe David Borba Manana 		if (end > entry->start && em->start < extent_map_end(entry))
12332193c14SFilipe David Borba Manana 			return -EEXIST;
12432193c14SFilipe David Borba Manana 
12532193c14SFilipe David Borba Manana 	parent = orig_parent;
12632193c14SFilipe David Borba Manana 	entry = rb_entry(parent, struct extent_map, rb_node);
12732193c14SFilipe David Borba Manana 	while (parent && em->start < entry->start) {
12832193c14SFilipe David Borba Manana 		parent = rb_prev(parent);
12932193c14SFilipe David Borba Manana 		entry = rb_entry(parent, struct extent_map, rb_node);
13032193c14SFilipe David Borba Manana 	}
13132193c14SFilipe David Borba Manana 	if (parent)
13232193c14SFilipe David Borba Manana 		if (end > entry->start && em->start < extent_map_end(entry))
13332193c14SFilipe David Borba Manana 			return -EEXIST;
13432193c14SFilipe David Borba Manana 
13532193c14SFilipe David Borba Manana 	rb_link_node(&em->rb_node, orig_parent, p);
136*07e1ce09SLiu Bo 	rb_insert_color_cached(&em->rb_node, root, leftmost);
13732193c14SFilipe David Borba Manana 	return 0;
138a52d9a80SChris Mason }
139a52d9a80SChris Mason 
140d352ac68SChris Mason /*
141d352ac68SChris Mason  * search through the tree for an extent_map with a given offset.  If
142d352ac68SChris Mason  * it can't be found, try to find some neighboring extents
143d352ac68SChris Mason  */
144a52d9a80SChris Mason static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
1455f56406aSChris Mason 				     struct rb_node **prev_ret,
1465f56406aSChris Mason 				     struct rb_node **next_ret)
147a52d9a80SChris Mason {
148a52d9a80SChris Mason 	struct rb_node *n = root->rb_node;
149a52d9a80SChris Mason 	struct rb_node *prev = NULL;
1505f56406aSChris Mason 	struct rb_node *orig_prev = NULL;
151d1310b2eSChris Mason 	struct extent_map *entry;
152d1310b2eSChris Mason 	struct extent_map *prev_entry = NULL;
153a52d9a80SChris Mason 
154a52d9a80SChris Mason 	while (n) {
155d1310b2eSChris Mason 		entry = rb_entry(n, struct extent_map, rb_node);
156a52d9a80SChris Mason 		prev = n;
157a52d9a80SChris Mason 		prev_entry = entry;
158a52d9a80SChris Mason 
159a52d9a80SChris Mason 		if (offset < entry->start)
160a52d9a80SChris Mason 			n = n->rb_left;
161d1310b2eSChris Mason 		else if (offset >= extent_map_end(entry))
162a52d9a80SChris Mason 			n = n->rb_right;
163a52d9a80SChris Mason 		else
164a52d9a80SChris Mason 			return n;
165a52d9a80SChris Mason 	}
1665f56406aSChris Mason 
1675f56406aSChris Mason 	if (prev_ret) {
1685f56406aSChris Mason 		orig_prev = prev;
169d1310b2eSChris Mason 		while (prev && offset >= extent_map_end(prev_entry)) {
170a52d9a80SChris Mason 			prev = rb_next(prev);
171d1310b2eSChris Mason 			prev_entry = rb_entry(prev, struct extent_map, rb_node);
172a52d9a80SChris Mason 		}
173a52d9a80SChris Mason 		*prev_ret = prev;
1745f56406aSChris Mason 		prev = orig_prev;
1755f56406aSChris Mason 	}
1765f56406aSChris Mason 
1775f56406aSChris Mason 	if (next_ret) {
178d1310b2eSChris Mason 		prev_entry = rb_entry(prev, struct extent_map, rb_node);
1795f56406aSChris Mason 		while (prev && offset < prev_entry->start) {
1805f56406aSChris Mason 			prev = rb_prev(prev);
181d1310b2eSChris Mason 			prev_entry = rb_entry(prev, struct extent_map, rb_node);
1825f56406aSChris Mason 		}
1835f56406aSChris Mason 		*next_ret = prev;
1845f56406aSChris Mason 	}
185a52d9a80SChris Mason 	return NULL;
186a52d9a80SChris Mason }
187a52d9a80SChris Mason 
188d352ac68SChris Mason /* check to see if two extent_map structs are adjacent and safe to merge */
189d1310b2eSChris Mason static int mergable_maps(struct extent_map *prev, struct extent_map *next)
190a52d9a80SChris Mason {
1917f3c74fbSChris Mason 	if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
1927f3c74fbSChris Mason 		return 0;
1937f3c74fbSChris Mason 
194c8b97818SChris Mason 	/*
195c8b97818SChris Mason 	 * don't merge compressed extents, we need to know their
196c8b97818SChris Mason 	 * actual size
197c8b97818SChris Mason 	 */
198c8b97818SChris Mason 	if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
199c8b97818SChris Mason 		return 0;
200c8b97818SChris Mason 
201201a9038SJosef Bacik 	if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
202201a9038SJosef Bacik 	    test_bit(EXTENT_FLAG_LOGGING, &next->flags))
203201a9038SJosef Bacik 		return 0;
204201a9038SJosef Bacik 
20509a2a8f9SJosef Bacik 	/*
20609a2a8f9SJosef Bacik 	 * We don't want to merge stuff that hasn't been written to the log yet
20709a2a8f9SJosef Bacik 	 * since it may not reflect exactly what is on disk, and that would be
20809a2a8f9SJosef Bacik 	 * bad.
20909a2a8f9SJosef Bacik 	 */
21009a2a8f9SJosef Bacik 	if (!list_empty(&prev->list) || !list_empty(&next->list))
21109a2a8f9SJosef Bacik 		return 0;
21209a2a8f9SJosef Bacik 
213d1310b2eSChris Mason 	if (extent_map_end(prev) == next->start &&
214d1310b2eSChris Mason 	    prev->flags == next->flags &&
215d1310b2eSChris Mason 	    prev->bdev == next->bdev &&
216d1310b2eSChris Mason 	    ((next->block_start == EXTENT_MAP_HOLE &&
217d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_HOLE) ||
218d1310b2eSChris Mason 	     (next->block_start == EXTENT_MAP_INLINE &&
219d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_INLINE) ||
220d1310b2eSChris Mason 	     (next->block_start == EXTENT_MAP_DELALLOC &&
221d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_DELALLOC) ||
222d1310b2eSChris Mason 	     (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
223d1310b2eSChris Mason 	      next->block_start == extent_map_block_end(prev)))) {
224d1310b2eSChris Mason 		return 1;
225d1310b2eSChris Mason 	}
226a52d9a80SChris Mason 	return 0;
227a52d9a80SChris Mason }
228a52d9a80SChris Mason 
2294d2c8f62SLi Zefan static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
230a1ed835eSChris Mason {
231a1ed835eSChris Mason 	struct extent_map *merge = NULL;
232a1ed835eSChris Mason 	struct rb_node *rb;
233a1ed835eSChris Mason 
234a1ed835eSChris Mason 	if (em->start != 0) {
235a1ed835eSChris Mason 		rb = rb_prev(&em->rb_node);
236a1ed835eSChris Mason 		if (rb)
237a1ed835eSChris Mason 			merge = rb_entry(rb, struct extent_map, rb_node);
238a1ed835eSChris Mason 		if (rb && mergable_maps(merge, em)) {
239a1ed835eSChris Mason 			em->start = merge->start;
24070c8a91cSJosef Bacik 			em->orig_start = merge->orig_start;
241a1ed835eSChris Mason 			em->len += merge->len;
242a1ed835eSChris Mason 			em->block_len += merge->block_len;
243a1ed835eSChris Mason 			em->block_start = merge->block_start;
24470c8a91cSJosef Bacik 			em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
24570c8a91cSJosef Bacik 			em->mod_start = merge->mod_start;
24670c8a91cSJosef Bacik 			em->generation = max(em->generation, merge->generation);
2475dc562c5SJosef Bacik 
248*07e1ce09SLiu Bo 			rb_erase_cached(&merge->rb_node, &tree->map);
249cbc0e928SFilipe Manana 			RB_CLEAR_NODE(&merge->rb_node);
250a1ed835eSChris Mason 			free_extent_map(merge);
251a1ed835eSChris Mason 		}
252a1ed835eSChris Mason 	}
253a1ed835eSChris Mason 
254a1ed835eSChris Mason 	rb = rb_next(&em->rb_node);
255a1ed835eSChris Mason 	if (rb)
256a1ed835eSChris Mason 		merge = rb_entry(rb, struct extent_map, rb_node);
257a1ed835eSChris Mason 	if (rb && mergable_maps(em, merge)) {
258a1ed835eSChris Mason 		em->len += merge->len;
259d527afe1SFilipe David Borba Manana 		em->block_len += merge->block_len;
260*07e1ce09SLiu Bo 		rb_erase_cached(&merge->rb_node, &tree->map);
261cbc0e928SFilipe Manana 		RB_CLEAR_NODE(&merge->rb_node);
26270c8a91cSJosef Bacik 		em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
26370c8a91cSJosef Bacik 		em->generation = max(em->generation, merge->generation);
264a1ed835eSChris Mason 		free_extent_map(merge);
265a1ed835eSChris Mason 	}
2664d2c8f62SLi Zefan }
2674d2c8f62SLi Zefan 
2685dc562c5SJosef Bacik /**
26952b1de91SLiu Bo  * unpin_extent_cache - unpin an extent from the cache
2705dc562c5SJosef Bacik  * @tree:	tree to unpin the extent in
2715dc562c5SJosef Bacik  * @start:	logical offset in the file
2725dc562c5SJosef Bacik  * @len:	length of the extent
2735dc562c5SJosef Bacik  * @gen:	generation that this extent has been modified in
2745dc562c5SJosef Bacik  *
2755dc562c5SJosef Bacik  * Called after an extent has been written to disk properly.  Set the generation
2765dc562c5SJosef Bacik  * to the generation that actually added the file item to the inode so we know
2775dc562c5SJosef Bacik  * we need to sync this extent when we call fsync().
2785dc562c5SJosef Bacik  */
2795dc562c5SJosef Bacik int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
2805dc562c5SJosef Bacik 		       u64 gen)
2814d2c8f62SLi Zefan {
2824d2c8f62SLi Zefan 	int ret = 0;
2834d2c8f62SLi Zefan 	struct extent_map *em;
2844e2f84e6SLiu Bo 	bool prealloc = false;
2854d2c8f62SLi Zefan 
2864d2c8f62SLi Zefan 	write_lock(&tree->lock);
2874d2c8f62SLi Zefan 	em = lookup_extent_mapping(tree, start, len);
2884d2c8f62SLi Zefan 
2894d2c8f62SLi Zefan 	WARN_ON(!em || em->start != start);
2904d2c8f62SLi Zefan 
2914d2c8f62SLi Zefan 	if (!em)
2924d2c8f62SLi Zefan 		goto out;
2934d2c8f62SLi Zefan 
2945dc562c5SJosef Bacik 	em->generation = gen;
2954d2c8f62SLi Zefan 	clear_bit(EXTENT_FLAG_PINNED, &em->flags);
2964e2f84e6SLiu Bo 	em->mod_start = em->start;
2974e2f84e6SLiu Bo 	em->mod_len = em->len;
2984e2f84e6SLiu Bo 
299b11e234dSJosef Bacik 	if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
3004e2f84e6SLiu Bo 		prealloc = true;
301b11e234dSJosef Bacik 		clear_bit(EXTENT_FLAG_FILLING, &em->flags);
3024e2f84e6SLiu Bo 	}
3034d2c8f62SLi Zefan 
3044d2c8f62SLi Zefan 	try_merge_map(tree, em);
3054e2f84e6SLiu Bo 
3064e2f84e6SLiu Bo 	if (prealloc) {
3074e2f84e6SLiu Bo 		em->mod_start = em->start;
3084e2f84e6SLiu Bo 		em->mod_len = em->len;
3094e2f84e6SLiu Bo 	}
3104e2f84e6SLiu Bo 
311a1ed835eSChris Mason 	free_extent_map(em);
312a1ed835eSChris Mason out:
313a1ed835eSChris Mason 	write_unlock(&tree->lock);
314a1ed835eSChris Mason 	return ret;
315a1ed835eSChris Mason 
316a1ed835eSChris Mason }
317a1ed835eSChris Mason 
318201a9038SJosef Bacik void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
319201a9038SJosef Bacik {
320201a9038SJosef Bacik 	clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
321cbc0e928SFilipe Manana 	if (extent_map_in_tree(em))
322201a9038SJosef Bacik 		try_merge_map(tree, em);
323201a9038SJosef Bacik }
324201a9038SJosef Bacik 
325176840b3SFilipe Manana static inline void setup_extent_mapping(struct extent_map_tree *tree,
326176840b3SFilipe Manana 					struct extent_map *em,
327176840b3SFilipe Manana 					int modified)
328176840b3SFilipe Manana {
329490b54d6SElena Reshetova 	refcount_inc(&em->refs);
330176840b3SFilipe Manana 	em->mod_start = em->start;
331176840b3SFilipe Manana 	em->mod_len = em->len;
332176840b3SFilipe Manana 
333176840b3SFilipe Manana 	if (modified)
334176840b3SFilipe Manana 		list_move(&em->list, &tree->modified_extents);
335176840b3SFilipe Manana 	else
336176840b3SFilipe Manana 		try_merge_map(tree, em);
337176840b3SFilipe Manana }
338176840b3SFilipe Manana 
3399d2423c5SChristoph Hellwig /**
3409d2423c5SChristoph Hellwig  * add_extent_mapping - add new extent map to the extent tree
3419d2423c5SChristoph Hellwig  * @tree:	tree to insert new map in
3429d2423c5SChristoph Hellwig  * @em:		map to insert
3439d2423c5SChristoph Hellwig  *
3449d2423c5SChristoph Hellwig  * Insert @em into @tree or perform a simple forward/backward merge with
3459d2423c5SChristoph Hellwig  * existing mappings.  The extent_map struct passed in will be inserted
3469d2423c5SChristoph Hellwig  * into the tree directly, with an additional reference taken, or a
34725985edcSLucas De Marchi  * reference dropped if the merge attempt was successful.
348a52d9a80SChris Mason  */
349a52d9a80SChris Mason int add_extent_mapping(struct extent_map_tree *tree,
35009a2a8f9SJosef Bacik 		       struct extent_map *em, int modified)
351a52d9a80SChris Mason {
352a52d9a80SChris Mason 	int ret = 0;
353a52d9a80SChris Mason 
35432193c14SFilipe David Borba Manana 	ret = tree_insert(&tree->map, em);
35532193c14SFilipe David Borba Manana 	if (ret)
3567c2fe32aSChris Mason 		goto out;
35732193c14SFilipe David Borba Manana 
358176840b3SFilipe Manana 	setup_extent_mapping(tree, em, modified);
359a52d9a80SChris Mason out:
360a52d9a80SChris Mason 	return ret;
361a52d9a80SChris Mason }
362a52d9a80SChris Mason 
36348a3b636SEric Sandeen static struct extent_map *
36448a3b636SEric Sandeen __lookup_extent_mapping(struct extent_map_tree *tree,
365ed64f066SLi Zefan 			u64 start, u64 len, int strict)
366ed64f066SLi Zefan {
367ed64f066SLi Zefan 	struct extent_map *em;
368ed64f066SLi Zefan 	struct rb_node *rb_node;
369ed64f066SLi Zefan 	struct rb_node *prev = NULL;
370ed64f066SLi Zefan 	struct rb_node *next = NULL;
371ed64f066SLi Zefan 	u64 end = range_end(start, len);
372ed64f066SLi Zefan 
373*07e1ce09SLiu Bo 	rb_node = __tree_search(&tree->map.rb_root, start, &prev, &next);
374ed64f066SLi Zefan 	if (!rb_node) {
375ed64f066SLi Zefan 		if (prev)
376ed64f066SLi Zefan 			rb_node = prev;
377ed64f066SLi Zefan 		else if (next)
378ed64f066SLi Zefan 			rb_node = next;
379ed64f066SLi Zefan 		else
380ed64f066SLi Zefan 			return NULL;
381ed64f066SLi Zefan 	}
382ed64f066SLi Zefan 
383ed64f066SLi Zefan 	em = rb_entry(rb_node, struct extent_map, rb_node);
384ed64f066SLi Zefan 
385ed64f066SLi Zefan 	if (strict && !(end > em->start && start < extent_map_end(em)))
386ed64f066SLi Zefan 		return NULL;
387ed64f066SLi Zefan 
388490b54d6SElena Reshetova 	refcount_inc(&em->refs);
389ed64f066SLi Zefan 	return em;
390ed64f066SLi Zefan }
391ed64f066SLi Zefan 
3929d2423c5SChristoph Hellwig /**
3939d2423c5SChristoph Hellwig  * lookup_extent_mapping - lookup extent_map
3949d2423c5SChristoph Hellwig  * @tree:	tree to lookup in
3959d2423c5SChristoph Hellwig  * @start:	byte offset to start the search
3969d2423c5SChristoph Hellwig  * @len:	length of the lookup range
3979d2423c5SChristoph Hellwig  *
3989d2423c5SChristoph Hellwig  * Find and return the first extent_map struct in @tree that intersects the
3999d2423c5SChristoph Hellwig  * [start, len] range.  There may be additional objects in the tree that
4009d2423c5SChristoph Hellwig  * intersect, so check the object returned carefully to make sure that no
4019d2423c5SChristoph Hellwig  * additional lookups are needed.
402a52d9a80SChris Mason  */
403a52d9a80SChris Mason struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
404d1310b2eSChris Mason 					 u64 start, u64 len)
405a52d9a80SChris Mason {
406ed64f066SLi Zefan 	return __lookup_extent_mapping(tree, start, len, 1);
407a52d9a80SChris Mason }
408a52d9a80SChris Mason 
4099d2423c5SChristoph Hellwig /**
410b917b7c3SChris Mason  * search_extent_mapping - find a nearby extent map
411b917b7c3SChris Mason  * @tree:	tree to lookup in
412b917b7c3SChris Mason  * @start:	byte offset to start the search
413b917b7c3SChris Mason  * @len:	length of the lookup range
414b917b7c3SChris Mason  *
415b917b7c3SChris Mason  * Find and return the first extent_map struct in @tree that intersects the
416b917b7c3SChris Mason  * [start, len] range.
417b917b7c3SChris Mason  *
418b917b7c3SChris Mason  * If one can't be found, any nearby extent may be returned
419b917b7c3SChris Mason  */
420b917b7c3SChris Mason struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
421b917b7c3SChris Mason 					 u64 start, u64 len)
422b917b7c3SChris Mason {
423ed64f066SLi Zefan 	return __lookup_extent_mapping(tree, start, len, 0);
424b917b7c3SChris Mason }
425b917b7c3SChris Mason 
426b917b7c3SChris Mason /**
4279d2423c5SChristoph Hellwig  * remove_extent_mapping - removes an extent_map from the extent tree
4289d2423c5SChristoph Hellwig  * @tree:	extent tree to remove from
429bb7ab3b9SAdam Buchbinder  * @em:		extent map being removed
4309d2423c5SChristoph Hellwig  *
4319d2423c5SChristoph Hellwig  * Removes @em from @tree.  No reference counts are dropped, and no checks
4329d2423c5SChristoph Hellwig  * are done to see if the range is in use
433a52d9a80SChris Mason  */
434c1766dd7Szhong jiang void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
435a52d9a80SChris Mason {
4367f3c74fbSChris Mason 	WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
437*07e1ce09SLiu Bo 	rb_erase_cached(&em->rb_node, &tree->map);
438ff44c6e3SJosef Bacik 	if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
4395dc562c5SJosef Bacik 		list_del_init(&em->list);
440cbc0e928SFilipe Manana 	RB_CLEAR_NODE(&em->rb_node);
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);
452*07e1ce09SLiu Bo 	rb_replace_node_cached(&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 }
457c04e61b5SLiu Bo 
458c04e61b5SLiu Bo static struct extent_map *next_extent_map(struct extent_map *em)
459c04e61b5SLiu Bo {
460c04e61b5SLiu Bo 	struct rb_node *next;
461c04e61b5SLiu Bo 
462c04e61b5SLiu Bo 	next = rb_next(&em->rb_node);
463c04e61b5SLiu Bo 	if (!next)
464c04e61b5SLiu Bo 		return NULL;
465c04e61b5SLiu Bo 	return container_of(next, struct extent_map, rb_node);
466c04e61b5SLiu Bo }
467c04e61b5SLiu Bo 
468c04e61b5SLiu Bo static struct extent_map *prev_extent_map(struct extent_map *em)
469c04e61b5SLiu Bo {
470c04e61b5SLiu Bo 	struct rb_node *prev;
471c04e61b5SLiu Bo 
472c04e61b5SLiu Bo 	prev = rb_prev(&em->rb_node);
473c04e61b5SLiu Bo 	if (!prev)
474c04e61b5SLiu Bo 		return NULL;
475c04e61b5SLiu Bo 	return container_of(prev, struct extent_map, rb_node);
476c04e61b5SLiu Bo }
477c04e61b5SLiu Bo 
478c04e61b5SLiu Bo /* helper for btfs_get_extent.  Given an existing extent in the tree,
479c04e61b5SLiu Bo  * the existing extent is the nearest extent to map_start,
480c04e61b5SLiu Bo  * and an extent that you want to insert, deal with overlap and insert
481c04e61b5SLiu Bo  * the best fitted new extent into the tree.
482c04e61b5SLiu Bo  */
4835f4791f4SLiu Bo static noinline int merge_extent_mapping(struct extent_map_tree *em_tree,
484c04e61b5SLiu Bo 					 struct extent_map *existing,
485c04e61b5SLiu Bo 					 struct extent_map *em,
486c04e61b5SLiu Bo 					 u64 map_start)
487c04e61b5SLiu Bo {
488c04e61b5SLiu Bo 	struct extent_map *prev;
489c04e61b5SLiu Bo 	struct extent_map *next;
490c04e61b5SLiu Bo 	u64 start;
491c04e61b5SLiu Bo 	u64 end;
492c04e61b5SLiu Bo 	u64 start_diff;
493c04e61b5SLiu Bo 
494c04e61b5SLiu Bo 	BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
495c04e61b5SLiu Bo 
496c04e61b5SLiu Bo 	if (existing->start > map_start) {
497c04e61b5SLiu Bo 		next = existing;
498c04e61b5SLiu Bo 		prev = prev_extent_map(next);
499c04e61b5SLiu Bo 	} else {
500c04e61b5SLiu Bo 		prev = existing;
501c04e61b5SLiu Bo 		next = next_extent_map(prev);
502c04e61b5SLiu Bo 	}
503c04e61b5SLiu Bo 
504c04e61b5SLiu Bo 	start = prev ? extent_map_end(prev) : em->start;
505c04e61b5SLiu Bo 	start = max_t(u64, start, em->start);
506c04e61b5SLiu Bo 	end = next ? next->start : extent_map_end(em);
507c04e61b5SLiu Bo 	end = min_t(u64, end, extent_map_end(em));
508c04e61b5SLiu Bo 	start_diff = start - em->start;
509c04e61b5SLiu Bo 	em->start = start;
510c04e61b5SLiu Bo 	em->len = end - start;
511c04e61b5SLiu Bo 	if (em->block_start < EXTENT_MAP_LAST_BYTE &&
512c04e61b5SLiu Bo 	    !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
513c04e61b5SLiu Bo 		em->block_start += start_diff;
514c04e61b5SLiu Bo 		em->block_len = em->len;
515c04e61b5SLiu Bo 	}
516c04e61b5SLiu Bo 	return add_extent_mapping(em_tree, em, 0);
517c04e61b5SLiu Bo }
518c04e61b5SLiu Bo 
519c04e61b5SLiu Bo /**
520c04e61b5SLiu Bo  * btrfs_add_extent_mapping - add extent mapping into em_tree
521f46b24c9SDavid Sterba  * @fs_info - used for tracepoint
522c04e61b5SLiu Bo  * @em_tree - the extent tree into which we want to insert the extent mapping
523c04e61b5SLiu Bo  * @em_in   - extent we are inserting
524c04e61b5SLiu Bo  * @start   - start of the logical range btrfs_get_extent() is requesting
525c04e61b5SLiu Bo  * @len     - length of the logical range btrfs_get_extent() is requesting
526c04e61b5SLiu Bo  *
527c04e61b5SLiu Bo  * Note that @em_in's range may be different from [start, start+len),
528c04e61b5SLiu Bo  * but they must be overlapped.
529c04e61b5SLiu Bo  *
530c04e61b5SLiu Bo  * Insert @em_in into @em_tree. In case there is an overlapping range, handle
531c04e61b5SLiu Bo  * the -EEXIST by either:
532c04e61b5SLiu Bo  * a) Returning the existing extent in @em_in if @start is within the
533c04e61b5SLiu Bo  *    existing em.
534c04e61b5SLiu Bo  * b) Merge the existing extent with @em_in passed in.
535c04e61b5SLiu Bo  *
536c04e61b5SLiu Bo  * Return 0 on success, otherwise -EEXIST.
537c04e61b5SLiu Bo  *
538c04e61b5SLiu Bo  */
539f46b24c9SDavid Sterba int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info,
540f46b24c9SDavid Sterba 			     struct extent_map_tree *em_tree,
541c04e61b5SLiu Bo 			     struct extent_map **em_in, u64 start, u64 len)
542c04e61b5SLiu Bo {
543c04e61b5SLiu Bo 	int ret;
544c04e61b5SLiu Bo 	struct extent_map *em = *em_in;
545c04e61b5SLiu Bo 
546c04e61b5SLiu Bo 	ret = add_extent_mapping(em_tree, em, 0);
547c04e61b5SLiu Bo 	/* it is possible that someone inserted the extent into the tree
548c04e61b5SLiu Bo 	 * while we had the lock dropped.  It is also possible that
549c04e61b5SLiu Bo 	 * an overlapping map exists in the tree
550c04e61b5SLiu Bo 	 */
551c04e61b5SLiu Bo 	if (ret == -EEXIST) {
552c04e61b5SLiu Bo 		struct extent_map *existing;
553c04e61b5SLiu Bo 
554c04e61b5SLiu Bo 		ret = 0;
555c04e61b5SLiu Bo 
556c04e61b5SLiu Bo 		existing = search_extent_mapping(em_tree, start, len);
557393da918SLiu Bo 
558f46b24c9SDavid Sterba 		trace_btrfs_handle_em_exist(fs_info, existing, em, start, len);
559393da918SLiu Bo 
560c04e61b5SLiu Bo 		/*
561c04e61b5SLiu Bo 		 * existing will always be non-NULL, since there must be
562c04e61b5SLiu Bo 		 * extent causing the -EEXIST.
563c04e61b5SLiu Bo 		 */
564c04e61b5SLiu Bo 		if (start >= existing->start &&
565c04e61b5SLiu Bo 		    start < extent_map_end(existing)) {
566c04e61b5SLiu Bo 			free_extent_map(em);
567c04e61b5SLiu Bo 			*em_in = existing;
568c04e61b5SLiu Bo 			ret = 0;
569c04e61b5SLiu Bo 		} else {
5709a7e10e7SLiu Bo 			u64 orig_start = em->start;
5719a7e10e7SLiu Bo 			u64 orig_len = em->len;
5729a7e10e7SLiu Bo 
573c04e61b5SLiu Bo 			/*
574c04e61b5SLiu Bo 			 * The existing extent map is the one nearest to
575c04e61b5SLiu Bo 			 * the [start, start + len) range which overlaps
576c04e61b5SLiu Bo 			 */
577c04e61b5SLiu Bo 			ret = merge_extent_mapping(em_tree, existing,
578c04e61b5SLiu Bo 						   em, start);
579c04e61b5SLiu Bo 			if (ret) {
580c04e61b5SLiu Bo 				free_extent_map(em);
581c04e61b5SLiu Bo 				*em_in = NULL;
5829a7e10e7SLiu Bo 				WARN_ONCE(ret,
5839a7e10e7SLiu Bo "unexpected error %d: merge existing(start %llu len %llu) with em(start %llu len %llu)\n",
5849a7e10e7SLiu Bo 					  ret, existing->start, existing->len,
5859a7e10e7SLiu Bo 					  orig_start, orig_len);
586c04e61b5SLiu Bo 			}
5879a7e10e7SLiu Bo 			free_extent_map(existing);
588c04e61b5SLiu Bo 		}
589c04e61b5SLiu Bo 	}
590c04e61b5SLiu Bo 
591c04e61b5SLiu Bo 	ASSERT(ret == 0 || ret == -EEXIST);
592c04e61b5SLiu Bo 	return ret;
593c04e61b5SLiu Bo }
594