xref: /openbmc/linux/fs/btrfs/extent_map.c (revision bd015294af4d919f94ede252e035f3c11638ea1f)
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"
71c11b63eSJeff Mahoney #include "volumes.h"
8a52d9a80SChris Mason #include "extent_map.h"
9ebb8765bSAnand Jain #include "compression.h"
10a52d9a80SChris Mason 
1186479a04SChris Mason 
12a52d9a80SChris Mason static struct kmem_cache *extent_map_cache;
13ca664626SChris Mason 
142f4cbe64SWyatt Banks int __init extent_map_init(void)
15a52d9a80SChris Mason {
16837e1972SDavid Sterba 	extent_map_cache = kmem_cache_create("btrfs_extent_map",
176d36dcd4SChris Mason 			sizeof(struct extent_map), 0,
18fba4b697SNikolay Borisov 			SLAB_MEM_SPREAD, NULL);
192f4cbe64SWyatt Banks 	if (!extent_map_cache)
202f4cbe64SWyatt Banks 		return -ENOMEM;
212f4cbe64SWyatt Banks 	return 0;
22a52d9a80SChris Mason }
23a52d9a80SChris Mason 
24e67c718bSDavid Sterba void __cold extent_map_exit(void)
25a52d9a80SChris Mason {
26a52d9a80SChris Mason 	kmem_cache_destroy(extent_map_cache);
27a52d9a80SChris Mason }
28a52d9a80SChris Mason 
299d2423c5SChristoph Hellwig /**
309d2423c5SChristoph Hellwig  * extent_map_tree_init - initialize extent map tree
319d2423c5SChristoph Hellwig  * @tree:		tree to initialize
329d2423c5SChristoph Hellwig  *
339d2423c5SChristoph Hellwig  * Initialize the extent tree @tree.  Should be called for each new inode
349d2423c5SChristoph Hellwig  * or other user of the extent_map interface.
359d2423c5SChristoph Hellwig  */
36a8067e02SDavid Sterba void extent_map_tree_init(struct extent_map_tree *tree)
37a52d9a80SChris Mason {
3807e1ce09SLiu Bo 	tree->map = RB_ROOT_CACHED;
395dc562c5SJosef Bacik 	INIT_LIST_HEAD(&tree->modified_extents);
40890871beSChris Mason 	rwlock_init(&tree->lock);
41a52d9a80SChris Mason }
42a52d9a80SChris Mason 
439d2423c5SChristoph Hellwig /**
449d2423c5SChristoph Hellwig  * alloc_extent_map - allocate new extent map structure
459d2423c5SChristoph Hellwig  *
469d2423c5SChristoph Hellwig  * Allocate a new extent_map structure.  The new structure is
479d2423c5SChristoph Hellwig  * returned with a reference count of one and needs to be
489d2423c5SChristoph Hellwig  * freed using free_extent_map()
499d2423c5SChristoph Hellwig  */
50172ddd60SDavid Sterba struct extent_map *alloc_extent_map(void)
51a52d9a80SChris Mason {
52a52d9a80SChris Mason 	struct extent_map *em;
5370c8a91cSJosef Bacik 	em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS);
54c26a9203STsutomu Itoh 	if (!em)
55c26a9203STsutomu Itoh 		return NULL;
56cbc0e928SFilipe Manana 	RB_CLEAR_NODE(&em->rb_node);
57d1310b2eSChris Mason 	em->flags = 0;
58261507a0SLi Zefan 	em->compress_type = BTRFS_COMPRESS_NONE;
595dc562c5SJosef Bacik 	em->generation = 0;
60490b54d6SElena Reshetova 	refcount_set(&em->refs, 1);
615dc562c5SJosef Bacik 	INIT_LIST_HEAD(&em->list);
62a52d9a80SChris Mason 	return em;
63a52d9a80SChris Mason }
64a52d9a80SChris Mason 
659d2423c5SChristoph Hellwig /**
669d2423c5SChristoph Hellwig  * free_extent_map - drop reference count of an extent_map
6701327610SNicholas D Steeves  * @em:		extent map being released
689d2423c5SChristoph Hellwig  *
699d2423c5SChristoph Hellwig  * Drops the reference out on @em by one and free the structure
709d2423c5SChristoph Hellwig  * if the reference count hits zero.
719d2423c5SChristoph Hellwig  */
72a52d9a80SChris Mason void free_extent_map(struct extent_map *em)
73a52d9a80SChris Mason {
742bf5a725SChris Mason 	if (!em)
752bf5a725SChris Mason 		return;
76490b54d6SElena Reshetova 	WARN_ON(refcount_read(&em->refs) == 0);
77490b54d6SElena Reshetova 	if (refcount_dec_and_test(&em->refs)) {
78cbc0e928SFilipe Manana 		WARN_ON(extent_map_in_tree(em));
795dc562c5SJosef Bacik 		WARN_ON(!list_empty(&em->list));
80298a8f9cSWang Shilong 		if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
8195617d69SJeff Mahoney 			kfree(em->map_lookup);
82a52d9a80SChris Mason 		kmem_cache_free(extent_map_cache, em);
83a52d9a80SChris Mason 	}
84a52d9a80SChris Mason }
85a52d9a80SChris Mason 
8632193c14SFilipe David Borba Manana /* simple helper to do math around the end of an extent, handling wrap */
8732193c14SFilipe David Borba Manana static u64 range_end(u64 start, u64 len)
8832193c14SFilipe David Borba Manana {
8932193c14SFilipe David Borba Manana 	if (start + len < start)
9032193c14SFilipe David Borba Manana 		return (u64)-1;
9132193c14SFilipe David Borba Manana 	return start + len;
9232193c14SFilipe David Borba Manana }
9332193c14SFilipe David Borba Manana 
9407e1ce09SLiu Bo static int tree_insert(struct rb_root_cached *root, struct extent_map *em)
95a52d9a80SChris Mason {
9607e1ce09SLiu Bo 	struct rb_node **p = &root->rb_root.rb_node;
97a52d9a80SChris Mason 	struct rb_node *parent = NULL;
9832193c14SFilipe David Borba Manana 	struct extent_map *entry = NULL;
9932193c14SFilipe David Borba Manana 	struct rb_node *orig_parent = NULL;
10032193c14SFilipe David Borba Manana 	u64 end = range_end(em->start, em->len);
10107e1ce09SLiu Bo 	bool leftmost = true;
102a52d9a80SChris Mason 
103a52d9a80SChris Mason 	while (*p) {
104a52d9a80SChris Mason 		parent = *p;
105d1310b2eSChris Mason 		entry = rb_entry(parent, struct extent_map, rb_node);
106d1310b2eSChris Mason 
10707e1ce09SLiu Bo 		if (em->start < entry->start) {
108a52d9a80SChris Mason 			p = &(*p)->rb_left;
10907e1ce09SLiu Bo 		} else if (em->start >= extent_map_end(entry)) {
110a52d9a80SChris Mason 			p = &(*p)->rb_right;
11107e1ce09SLiu Bo 			leftmost = false;
11207e1ce09SLiu Bo 		} else {
11332193c14SFilipe David Borba Manana 			return -EEXIST;
114a52d9a80SChris Mason 		}
11507e1ce09SLiu Bo 	}
116a52d9a80SChris Mason 
11732193c14SFilipe David Borba Manana 	orig_parent = parent;
11832193c14SFilipe David Borba Manana 	while (parent && em->start >= extent_map_end(entry)) {
11932193c14SFilipe David Borba Manana 		parent = rb_next(parent);
12032193c14SFilipe David Borba Manana 		entry = rb_entry(parent, struct extent_map, rb_node);
12132193c14SFilipe David Borba Manana 	}
12232193c14SFilipe David Borba Manana 	if (parent)
12332193c14SFilipe David Borba Manana 		if (end > entry->start && em->start < extent_map_end(entry))
12432193c14SFilipe David Borba Manana 			return -EEXIST;
12532193c14SFilipe David Borba Manana 
12632193c14SFilipe David Borba Manana 	parent = orig_parent;
12732193c14SFilipe David Borba Manana 	entry = rb_entry(parent, struct extent_map, rb_node);
12832193c14SFilipe David Borba Manana 	while (parent && em->start < entry->start) {
12932193c14SFilipe David Borba Manana 		parent = rb_prev(parent);
13032193c14SFilipe David Borba Manana 		entry = rb_entry(parent, struct extent_map, rb_node);
13132193c14SFilipe David Borba Manana 	}
13232193c14SFilipe David Borba Manana 	if (parent)
13332193c14SFilipe David Borba Manana 		if (end > entry->start && em->start < extent_map_end(entry))
13432193c14SFilipe David Borba Manana 			return -EEXIST;
13532193c14SFilipe David Borba Manana 
13632193c14SFilipe David Borba Manana 	rb_link_node(&em->rb_node, orig_parent, p);
13707e1ce09SLiu Bo 	rb_insert_color_cached(&em->rb_node, root, leftmost);
13832193c14SFilipe David Borba Manana 	return 0;
139a52d9a80SChris Mason }
140a52d9a80SChris Mason 
141d352ac68SChris Mason /*
142d352ac68SChris Mason  * search through the tree for an extent_map with a given offset.  If
143d352ac68SChris Mason  * it can't be found, try to find some neighboring extents
144d352ac68SChris Mason  */
145a52d9a80SChris Mason static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
1465f56406aSChris Mason 				     struct rb_node **prev_ret,
1475f56406aSChris Mason 				     struct rb_node **next_ret)
148a52d9a80SChris Mason {
149a52d9a80SChris Mason 	struct rb_node *n = root->rb_node;
150a52d9a80SChris Mason 	struct rb_node *prev = NULL;
1515f56406aSChris Mason 	struct rb_node *orig_prev = NULL;
152d1310b2eSChris Mason 	struct extent_map *entry;
153d1310b2eSChris Mason 	struct extent_map *prev_entry = NULL;
154a52d9a80SChris Mason 
155a52d9a80SChris Mason 	while (n) {
156d1310b2eSChris Mason 		entry = rb_entry(n, struct extent_map, rb_node);
157a52d9a80SChris Mason 		prev = n;
158a52d9a80SChris Mason 		prev_entry = entry;
159a52d9a80SChris Mason 
160a52d9a80SChris Mason 		if (offset < entry->start)
161a52d9a80SChris Mason 			n = n->rb_left;
162d1310b2eSChris Mason 		else if (offset >= extent_map_end(entry))
163a52d9a80SChris Mason 			n = n->rb_right;
164a52d9a80SChris Mason 		else
165a52d9a80SChris Mason 			return n;
166a52d9a80SChris Mason 	}
1675f56406aSChris Mason 
1685f56406aSChris Mason 	if (prev_ret) {
1695f56406aSChris Mason 		orig_prev = prev;
170d1310b2eSChris Mason 		while (prev && offset >= extent_map_end(prev_entry)) {
171a52d9a80SChris Mason 			prev = rb_next(prev);
172d1310b2eSChris Mason 			prev_entry = rb_entry(prev, struct extent_map, rb_node);
173a52d9a80SChris Mason 		}
174a52d9a80SChris Mason 		*prev_ret = prev;
1755f56406aSChris Mason 		prev = orig_prev;
1765f56406aSChris Mason 	}
1775f56406aSChris Mason 
1785f56406aSChris Mason 	if (next_ret) {
179d1310b2eSChris Mason 		prev_entry = rb_entry(prev, struct extent_map, rb_node);
1805f56406aSChris Mason 		while (prev && offset < prev_entry->start) {
1815f56406aSChris Mason 			prev = rb_prev(prev);
182d1310b2eSChris Mason 			prev_entry = rb_entry(prev, struct extent_map, rb_node);
1835f56406aSChris Mason 		}
1845f56406aSChris Mason 		*next_ret = prev;
1855f56406aSChris Mason 	}
186a52d9a80SChris Mason 	return NULL;
187a52d9a80SChris Mason }
188a52d9a80SChris Mason 
189d352ac68SChris Mason /* check to see if two extent_map structs are adjacent and safe to merge */
190d1310b2eSChris Mason static int mergable_maps(struct extent_map *prev, struct extent_map *next)
191a52d9a80SChris Mason {
1927f3c74fbSChris Mason 	if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
1937f3c74fbSChris Mason 		return 0;
1947f3c74fbSChris Mason 
195c8b97818SChris Mason 	/*
196c8b97818SChris Mason 	 * don't merge compressed extents, we need to know their
197c8b97818SChris Mason 	 * actual size
198c8b97818SChris Mason 	 */
199c8b97818SChris Mason 	if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
200c8b97818SChris Mason 		return 0;
201c8b97818SChris Mason 
202201a9038SJosef Bacik 	if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
203201a9038SJosef Bacik 	    test_bit(EXTENT_FLAG_LOGGING, &next->flags))
204201a9038SJosef Bacik 		return 0;
205201a9038SJosef Bacik 
20609a2a8f9SJosef Bacik 	/*
20709a2a8f9SJosef Bacik 	 * We don't want to merge stuff that hasn't been written to the log yet
20809a2a8f9SJosef Bacik 	 * since it may not reflect exactly what is on disk, and that would be
20909a2a8f9SJosef Bacik 	 * bad.
21009a2a8f9SJosef Bacik 	 */
21109a2a8f9SJosef Bacik 	if (!list_empty(&prev->list) || !list_empty(&next->list))
21209a2a8f9SJosef Bacik 		return 0;
21309a2a8f9SJosef Bacik 
214951e05a9SNikolay Borisov 	ASSERT(next->block_start != EXTENT_MAP_DELALLOC &&
215951e05a9SNikolay Borisov 	       prev->block_start != EXTENT_MAP_DELALLOC);
216951e05a9SNikolay Borisov 
217c3e14909SDavid Sterba 	if (prev->map_lookup || next->map_lookup)
218c3e14909SDavid Sterba 		ASSERT(test_bit(EXTENT_FLAG_FS_MAPPING, &prev->flags) &&
219c3e14909SDavid Sterba 		       test_bit(EXTENT_FLAG_FS_MAPPING, &next->flags));
220c3e14909SDavid Sterba 
221d1310b2eSChris Mason 	if (extent_map_end(prev) == next->start &&
222d1310b2eSChris Mason 	    prev->flags == next->flags &&
223c3e14909SDavid Sterba 	    prev->map_lookup == next->map_lookup &&
224d1310b2eSChris Mason 	    ((next->block_start == EXTENT_MAP_HOLE &&
225d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_HOLE) ||
226d1310b2eSChris Mason 	     (next->block_start == EXTENT_MAP_INLINE &&
227d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_INLINE) ||
228d1310b2eSChris Mason 	     (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
229d1310b2eSChris Mason 	      next->block_start == extent_map_block_end(prev)))) {
230d1310b2eSChris Mason 		return 1;
231d1310b2eSChris Mason 	}
232a52d9a80SChris Mason 	return 0;
233a52d9a80SChris Mason }
234a52d9a80SChris Mason 
2354d2c8f62SLi Zefan static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
236a1ed835eSChris Mason {
237a1ed835eSChris Mason 	struct extent_map *merge = NULL;
238a1ed835eSChris Mason 	struct rb_node *rb;
239a1ed835eSChris Mason 
240ac05ca91SFilipe Manana 	/*
241ac05ca91SFilipe Manana 	 * We can't modify an extent map that is in the tree and that is being
242ac05ca91SFilipe Manana 	 * used by another task, as it can cause that other task to see it in
243ac05ca91SFilipe Manana 	 * inconsistent state during the merging. We always have 1 reference for
244ac05ca91SFilipe Manana 	 * the tree and 1 for this task (which is unpinning the extent map or
245ac05ca91SFilipe Manana 	 * clearing the logging flag), so anything > 2 means it's being used by
246ac05ca91SFilipe Manana 	 * other tasks too.
247ac05ca91SFilipe Manana 	 */
248ac05ca91SFilipe Manana 	if (refcount_read(&em->refs) > 2)
249ac05ca91SFilipe Manana 		return;
250ac05ca91SFilipe Manana 
251a1ed835eSChris Mason 	if (em->start != 0) {
252a1ed835eSChris Mason 		rb = rb_prev(&em->rb_node);
253a1ed835eSChris Mason 		if (rb)
254a1ed835eSChris Mason 			merge = rb_entry(rb, struct extent_map, rb_node);
255a1ed835eSChris Mason 		if (rb && mergable_maps(merge, em)) {
256a1ed835eSChris Mason 			em->start = merge->start;
25770c8a91cSJosef Bacik 			em->orig_start = merge->orig_start;
258a1ed835eSChris Mason 			em->len += merge->len;
259a1ed835eSChris Mason 			em->block_len += merge->block_len;
260a1ed835eSChris Mason 			em->block_start = merge->block_start;
26170c8a91cSJosef Bacik 			em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
26270c8a91cSJosef Bacik 			em->mod_start = merge->mod_start;
26370c8a91cSJosef Bacik 			em->generation = max(em->generation, merge->generation);
264199257a7SQu Wenruo 			set_bit(EXTENT_FLAG_MERGED, &em->flags);
2655dc562c5SJosef Bacik 
26607e1ce09SLiu Bo 			rb_erase_cached(&merge->rb_node, &tree->map);
267cbc0e928SFilipe Manana 			RB_CLEAR_NODE(&merge->rb_node);
268a1ed835eSChris Mason 			free_extent_map(merge);
269a1ed835eSChris Mason 		}
270a1ed835eSChris Mason 	}
271a1ed835eSChris Mason 
272a1ed835eSChris Mason 	rb = rb_next(&em->rb_node);
273a1ed835eSChris Mason 	if (rb)
274a1ed835eSChris Mason 		merge = rb_entry(rb, struct extent_map, rb_node);
275a1ed835eSChris Mason 	if (rb && mergable_maps(em, merge)) {
276a1ed835eSChris Mason 		em->len += merge->len;
277d527afe1SFilipe David Borba Manana 		em->block_len += merge->block_len;
27807e1ce09SLiu Bo 		rb_erase_cached(&merge->rb_node, &tree->map);
279cbc0e928SFilipe Manana 		RB_CLEAR_NODE(&merge->rb_node);
28070c8a91cSJosef Bacik 		em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
28170c8a91cSJosef Bacik 		em->generation = max(em->generation, merge->generation);
282199257a7SQu Wenruo 		set_bit(EXTENT_FLAG_MERGED, &em->flags);
283a1ed835eSChris Mason 		free_extent_map(merge);
284a1ed835eSChris Mason 	}
2854d2c8f62SLi Zefan }
2864d2c8f62SLi Zefan 
2875dc562c5SJosef Bacik /**
28852b1de91SLiu Bo  * unpin_extent_cache - unpin an extent from the cache
2895dc562c5SJosef Bacik  * @tree:	tree to unpin the extent in
2905dc562c5SJosef Bacik  * @start:	logical offset in the file
2915dc562c5SJosef Bacik  * @len:	length of the extent
2925dc562c5SJosef Bacik  * @gen:	generation that this extent has been modified in
2935dc562c5SJosef Bacik  *
2945dc562c5SJosef Bacik  * Called after an extent has been written to disk properly.  Set the generation
2955dc562c5SJosef Bacik  * to the generation that actually added the file item to the inode so we know
2965dc562c5SJosef Bacik  * we need to sync this extent when we call fsync().
2975dc562c5SJosef Bacik  */
2985dc562c5SJosef Bacik int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
2995dc562c5SJosef Bacik 		       u64 gen)
3004d2c8f62SLi Zefan {
3014d2c8f62SLi Zefan 	int ret = 0;
3024d2c8f62SLi Zefan 	struct extent_map *em;
3034e2f84e6SLiu Bo 	bool prealloc = false;
3044d2c8f62SLi Zefan 
3054d2c8f62SLi Zefan 	write_lock(&tree->lock);
3064d2c8f62SLi Zefan 	em = lookup_extent_mapping(tree, start, len);
3074d2c8f62SLi Zefan 
3084d2c8f62SLi Zefan 	WARN_ON(!em || em->start != start);
3094d2c8f62SLi Zefan 
3104d2c8f62SLi Zefan 	if (!em)
3114d2c8f62SLi Zefan 		goto out;
3124d2c8f62SLi Zefan 
3135dc562c5SJosef Bacik 	em->generation = gen;
3144d2c8f62SLi Zefan 	clear_bit(EXTENT_FLAG_PINNED, &em->flags);
3154e2f84e6SLiu Bo 	em->mod_start = em->start;
3164e2f84e6SLiu Bo 	em->mod_len = em->len;
3174e2f84e6SLiu Bo 
318b11e234dSJosef Bacik 	if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
3194e2f84e6SLiu Bo 		prealloc = true;
320b11e234dSJosef Bacik 		clear_bit(EXTENT_FLAG_FILLING, &em->flags);
3214e2f84e6SLiu Bo 	}
3224d2c8f62SLi Zefan 
3234d2c8f62SLi Zefan 	try_merge_map(tree, em);
3244e2f84e6SLiu Bo 
3254e2f84e6SLiu Bo 	if (prealloc) {
3264e2f84e6SLiu Bo 		em->mod_start = em->start;
3274e2f84e6SLiu Bo 		em->mod_len = em->len;
3284e2f84e6SLiu Bo 	}
3294e2f84e6SLiu Bo 
330a1ed835eSChris Mason 	free_extent_map(em);
331a1ed835eSChris Mason out:
332a1ed835eSChris Mason 	write_unlock(&tree->lock);
333a1ed835eSChris Mason 	return ret;
334a1ed835eSChris Mason 
335a1ed835eSChris Mason }
336a1ed835eSChris Mason 
337201a9038SJosef Bacik void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
338201a9038SJosef Bacik {
339201a9038SJosef Bacik 	clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
340cbc0e928SFilipe Manana 	if (extent_map_in_tree(em))
341201a9038SJosef Bacik 		try_merge_map(tree, em);
342201a9038SJosef Bacik }
343201a9038SJosef Bacik 
344176840b3SFilipe Manana static inline void setup_extent_mapping(struct extent_map_tree *tree,
345176840b3SFilipe Manana 					struct extent_map *em,
346176840b3SFilipe Manana 					int modified)
347176840b3SFilipe Manana {
348490b54d6SElena Reshetova 	refcount_inc(&em->refs);
349176840b3SFilipe Manana 	em->mod_start = em->start;
350176840b3SFilipe Manana 	em->mod_len = em->len;
351176840b3SFilipe Manana 
352176840b3SFilipe Manana 	if (modified)
353176840b3SFilipe Manana 		list_move(&em->list, &tree->modified_extents);
354176840b3SFilipe Manana 	else
355176840b3SFilipe Manana 		try_merge_map(tree, em);
356176840b3SFilipe Manana }
357176840b3SFilipe Manana 
3581c11b63eSJeff Mahoney static void extent_map_device_set_bits(struct extent_map *em, unsigned bits)
3591c11b63eSJeff Mahoney {
3601c11b63eSJeff Mahoney 	struct map_lookup *map = em->map_lookup;
3611c11b63eSJeff Mahoney 	u64 stripe_size = em->orig_block_len;
3621c11b63eSJeff Mahoney 	int i;
3631c11b63eSJeff Mahoney 
3641c11b63eSJeff Mahoney 	for (i = 0; i < map->num_stripes; i++) {
3654c664611SQu Wenruo 		struct btrfs_io_stripe *stripe = &map->stripes[i];
3661c11b63eSJeff Mahoney 		struct btrfs_device *device = stripe->dev;
3671c11b63eSJeff Mahoney 
3681c11b63eSJeff Mahoney 		set_extent_bits_nowait(&device->alloc_state, stripe->physical,
3691c11b63eSJeff Mahoney 				 stripe->physical + stripe_size - 1, bits);
3701c11b63eSJeff Mahoney 	}
3711c11b63eSJeff Mahoney }
3721c11b63eSJeff Mahoney 
3731c11b63eSJeff Mahoney static void extent_map_device_clear_bits(struct extent_map *em, unsigned bits)
3741c11b63eSJeff Mahoney {
3751c11b63eSJeff Mahoney 	struct map_lookup *map = em->map_lookup;
3761c11b63eSJeff Mahoney 	u64 stripe_size = em->orig_block_len;
3771c11b63eSJeff Mahoney 	int i;
3781c11b63eSJeff Mahoney 
3791c11b63eSJeff Mahoney 	for (i = 0; i < map->num_stripes; i++) {
3804c664611SQu Wenruo 		struct btrfs_io_stripe *stripe = &map->stripes[i];
3811c11b63eSJeff Mahoney 		struct btrfs_device *device = stripe->dev;
3821c11b63eSJeff Mahoney 
3831c11b63eSJeff Mahoney 		__clear_extent_bit(&device->alloc_state, stripe->physical,
3841c11b63eSJeff Mahoney 				   stripe->physical + stripe_size - 1, bits,
385*bd015294SJosef Bacik 				   NULL, GFP_NOWAIT, NULL);
3861c11b63eSJeff Mahoney 	}
3871c11b63eSJeff Mahoney }
3881c11b63eSJeff Mahoney 
3899d2423c5SChristoph Hellwig /**
390401bd2ddSNikolay Borisov  * Add new extent map to the extent tree
391401bd2ddSNikolay Borisov  *
3929d2423c5SChristoph Hellwig  * @tree:	tree to insert new map in
3939d2423c5SChristoph Hellwig  * @em:		map to insert
394401bd2ddSNikolay Borisov  * @modified:	indicate whether the given @em should be added to the
395401bd2ddSNikolay Borisov  *	        modified list, which indicates the extent needs to be logged
3969d2423c5SChristoph Hellwig  *
3979d2423c5SChristoph Hellwig  * Insert @em into @tree or perform a simple forward/backward merge with
3989d2423c5SChristoph Hellwig  * existing mappings.  The extent_map struct passed in will be inserted
3999d2423c5SChristoph Hellwig  * into the tree directly, with an additional reference taken, or a
40025985edcSLucas De Marchi  * reference dropped if the merge attempt was successful.
401a52d9a80SChris Mason  */
402a52d9a80SChris Mason int add_extent_mapping(struct extent_map_tree *tree,
40309a2a8f9SJosef Bacik 		       struct extent_map *em, int modified)
404a52d9a80SChris Mason {
405a52d9a80SChris Mason 	int ret = 0;
406a52d9a80SChris Mason 
407d23ea3faSDavid Sterba 	lockdep_assert_held_write(&tree->lock);
408d23ea3faSDavid Sterba 
40932193c14SFilipe David Borba Manana 	ret = tree_insert(&tree->map, em);
41032193c14SFilipe David Borba Manana 	if (ret)
4117c2fe32aSChris Mason 		goto out;
41232193c14SFilipe David Borba Manana 
413176840b3SFilipe Manana 	setup_extent_mapping(tree, em, modified);
4148811133dSNikolay Borisov 	if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags)) {
4151c11b63eSJeff Mahoney 		extent_map_device_set_bits(em, CHUNK_ALLOCATED);
4168811133dSNikolay Borisov 		extent_map_device_clear_bits(em, CHUNK_TRIMMED);
4178811133dSNikolay Borisov 	}
418a52d9a80SChris Mason out:
419a52d9a80SChris Mason 	return ret;
420a52d9a80SChris Mason }
421a52d9a80SChris Mason 
42248a3b636SEric Sandeen static struct extent_map *
42348a3b636SEric Sandeen __lookup_extent_mapping(struct extent_map_tree *tree,
424ed64f066SLi Zefan 			u64 start, u64 len, int strict)
425ed64f066SLi Zefan {
426ed64f066SLi Zefan 	struct extent_map *em;
427ed64f066SLi Zefan 	struct rb_node *rb_node;
428ed64f066SLi Zefan 	struct rb_node *prev = NULL;
429ed64f066SLi Zefan 	struct rb_node *next = NULL;
430ed64f066SLi Zefan 	u64 end = range_end(start, len);
431ed64f066SLi Zefan 
43207e1ce09SLiu Bo 	rb_node = __tree_search(&tree->map.rb_root, start, &prev, &next);
433ed64f066SLi Zefan 	if (!rb_node) {
434ed64f066SLi Zefan 		if (prev)
435ed64f066SLi Zefan 			rb_node = prev;
436ed64f066SLi Zefan 		else if (next)
437ed64f066SLi Zefan 			rb_node = next;
438ed64f066SLi Zefan 		else
439ed64f066SLi Zefan 			return NULL;
440ed64f066SLi Zefan 	}
441ed64f066SLi Zefan 
442ed64f066SLi Zefan 	em = rb_entry(rb_node, struct extent_map, rb_node);
443ed64f066SLi Zefan 
444ed64f066SLi Zefan 	if (strict && !(end > em->start && start < extent_map_end(em)))
445ed64f066SLi Zefan 		return NULL;
446ed64f066SLi Zefan 
447490b54d6SElena Reshetova 	refcount_inc(&em->refs);
448ed64f066SLi Zefan 	return em;
449ed64f066SLi Zefan }
450ed64f066SLi Zefan 
4519d2423c5SChristoph Hellwig /**
4529d2423c5SChristoph Hellwig  * lookup_extent_mapping - lookup extent_map
4539d2423c5SChristoph Hellwig  * @tree:	tree to lookup in
4549d2423c5SChristoph Hellwig  * @start:	byte offset to start the search
4559d2423c5SChristoph Hellwig  * @len:	length of the lookup range
4569d2423c5SChristoph Hellwig  *
4579d2423c5SChristoph Hellwig  * Find and return the first extent_map struct in @tree that intersects the
4589d2423c5SChristoph Hellwig  * [start, len] range.  There may be additional objects in the tree that
4599d2423c5SChristoph Hellwig  * intersect, so check the object returned carefully to make sure that no
4609d2423c5SChristoph Hellwig  * additional lookups are needed.
461a52d9a80SChris Mason  */
462a52d9a80SChris Mason struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
463d1310b2eSChris Mason 					 u64 start, u64 len)
464a52d9a80SChris Mason {
465ed64f066SLi Zefan 	return __lookup_extent_mapping(tree, start, len, 1);
466a52d9a80SChris Mason }
467a52d9a80SChris Mason 
4689d2423c5SChristoph Hellwig /**
469b917b7c3SChris Mason  * search_extent_mapping - find a nearby extent map
470b917b7c3SChris Mason  * @tree:	tree to lookup in
471b917b7c3SChris Mason  * @start:	byte offset to start the search
472b917b7c3SChris Mason  * @len:	length of the lookup range
473b917b7c3SChris Mason  *
474b917b7c3SChris Mason  * Find and return the first extent_map struct in @tree that intersects the
475b917b7c3SChris Mason  * [start, len] range.
476b917b7c3SChris Mason  *
477b917b7c3SChris Mason  * If one can't be found, any nearby extent may be returned
478b917b7c3SChris Mason  */
479b917b7c3SChris Mason struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
480b917b7c3SChris Mason 					 u64 start, u64 len)
481b917b7c3SChris Mason {
482ed64f066SLi Zefan 	return __lookup_extent_mapping(tree, start, len, 0);
483b917b7c3SChris Mason }
484b917b7c3SChris Mason 
485b917b7c3SChris Mason /**
4869d2423c5SChristoph Hellwig  * remove_extent_mapping - removes an extent_map from the extent tree
4879d2423c5SChristoph Hellwig  * @tree:	extent tree to remove from
488bb7ab3b9SAdam Buchbinder  * @em:		extent map being removed
4899d2423c5SChristoph Hellwig  *
4909d2423c5SChristoph Hellwig  * Removes @em from @tree.  No reference counts are dropped, and no checks
4919d2423c5SChristoph Hellwig  * are done to see if the range is in use
492a52d9a80SChris Mason  */
493c1766dd7Szhong jiang void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
494a52d9a80SChris Mason {
4956d3b050eSFilipe Manana 	lockdep_assert_held_write(&tree->lock);
4966d3b050eSFilipe Manana 
4977f3c74fbSChris Mason 	WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
49807e1ce09SLiu Bo 	rb_erase_cached(&em->rb_node, &tree->map);
499ff44c6e3SJosef Bacik 	if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
5005dc562c5SJosef Bacik 		list_del_init(&em->list);
5011c11b63eSJeff Mahoney 	if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
5021c11b63eSJeff Mahoney 		extent_map_device_clear_bits(em, CHUNK_ALLOCATED);
503cbc0e928SFilipe Manana 	RB_CLEAR_NODE(&em->rb_node);
504a52d9a80SChris Mason }
505176840b3SFilipe Manana 
506176840b3SFilipe Manana void replace_extent_mapping(struct extent_map_tree *tree,
507176840b3SFilipe Manana 			    struct extent_map *cur,
508176840b3SFilipe Manana 			    struct extent_map *new,
509176840b3SFilipe Manana 			    int modified)
510176840b3SFilipe Manana {
5116d3b050eSFilipe Manana 	lockdep_assert_held_write(&tree->lock);
5126d3b050eSFilipe Manana 
513176840b3SFilipe Manana 	WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));
514176840b3SFilipe Manana 	ASSERT(extent_map_in_tree(cur));
515176840b3SFilipe Manana 	if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))
516176840b3SFilipe Manana 		list_del_init(&cur->list);
51707e1ce09SLiu Bo 	rb_replace_node_cached(&cur->rb_node, &new->rb_node, &tree->map);
518176840b3SFilipe Manana 	RB_CLEAR_NODE(&cur->rb_node);
519176840b3SFilipe Manana 
520176840b3SFilipe Manana 	setup_extent_mapping(tree, new, modified);
521176840b3SFilipe Manana }
522c04e61b5SLiu Bo 
523c04e61b5SLiu Bo static struct extent_map *next_extent_map(struct extent_map *em)
524c04e61b5SLiu Bo {
525c04e61b5SLiu Bo 	struct rb_node *next;
526c04e61b5SLiu Bo 
527c04e61b5SLiu Bo 	next = rb_next(&em->rb_node);
528c04e61b5SLiu Bo 	if (!next)
529c04e61b5SLiu Bo 		return NULL;
530c04e61b5SLiu Bo 	return container_of(next, struct extent_map, rb_node);
531c04e61b5SLiu Bo }
532c04e61b5SLiu Bo 
533c04e61b5SLiu Bo static struct extent_map *prev_extent_map(struct extent_map *em)
534c04e61b5SLiu Bo {
535c04e61b5SLiu Bo 	struct rb_node *prev;
536c04e61b5SLiu Bo 
537c04e61b5SLiu Bo 	prev = rb_prev(&em->rb_node);
538c04e61b5SLiu Bo 	if (!prev)
539c04e61b5SLiu Bo 		return NULL;
540c04e61b5SLiu Bo 	return container_of(prev, struct extent_map, rb_node);
541c04e61b5SLiu Bo }
542c04e61b5SLiu Bo 
54352042d8eSAndrea Gelmini /*
54452042d8eSAndrea Gelmini  * Helper for btrfs_get_extent.  Given an existing extent in the tree,
545c04e61b5SLiu Bo  * the existing extent is the nearest extent to map_start,
546c04e61b5SLiu Bo  * and an extent that you want to insert, deal with overlap and insert
547c04e61b5SLiu Bo  * the best fitted new extent into the tree.
548c04e61b5SLiu Bo  */
5495f4791f4SLiu Bo static noinline int merge_extent_mapping(struct extent_map_tree *em_tree,
550c04e61b5SLiu Bo 					 struct extent_map *existing,
551c04e61b5SLiu Bo 					 struct extent_map *em,
552c04e61b5SLiu Bo 					 u64 map_start)
553c04e61b5SLiu Bo {
554c04e61b5SLiu Bo 	struct extent_map *prev;
555c04e61b5SLiu Bo 	struct extent_map *next;
556c04e61b5SLiu Bo 	u64 start;
557c04e61b5SLiu Bo 	u64 end;
558c04e61b5SLiu Bo 	u64 start_diff;
559c04e61b5SLiu Bo 
560c04e61b5SLiu Bo 	BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
561c04e61b5SLiu Bo 
562c04e61b5SLiu Bo 	if (existing->start > map_start) {
563c04e61b5SLiu Bo 		next = existing;
564c04e61b5SLiu Bo 		prev = prev_extent_map(next);
565c04e61b5SLiu Bo 	} else {
566c04e61b5SLiu Bo 		prev = existing;
567c04e61b5SLiu Bo 		next = next_extent_map(prev);
568c04e61b5SLiu Bo 	}
569c04e61b5SLiu Bo 
570c04e61b5SLiu Bo 	start = prev ? extent_map_end(prev) : em->start;
571c04e61b5SLiu Bo 	start = max_t(u64, start, em->start);
572c04e61b5SLiu Bo 	end = next ? next->start : extent_map_end(em);
573c04e61b5SLiu Bo 	end = min_t(u64, end, extent_map_end(em));
574c04e61b5SLiu Bo 	start_diff = start - em->start;
575c04e61b5SLiu Bo 	em->start = start;
576c04e61b5SLiu Bo 	em->len = end - start;
577c04e61b5SLiu Bo 	if (em->block_start < EXTENT_MAP_LAST_BYTE &&
578c04e61b5SLiu Bo 	    !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
579c04e61b5SLiu Bo 		em->block_start += start_diff;
580c04e61b5SLiu Bo 		em->block_len = em->len;
581c04e61b5SLiu Bo 	}
582c04e61b5SLiu Bo 	return add_extent_mapping(em_tree, em, 0);
583c04e61b5SLiu Bo }
584c04e61b5SLiu Bo 
585c04e61b5SLiu Bo /**
5869ad37bb3SNikolay Borisov  * Add extent mapping into em_tree
5879ad37bb3SNikolay Borisov  *
5889ad37bb3SNikolay Borisov  * @fs_info:  the filesystem
5899ad37bb3SNikolay Borisov  * @em_tree:  extent tree into which we want to insert the extent mapping
5909ad37bb3SNikolay Borisov  * @em_in:    extent we are inserting
5919ad37bb3SNikolay Borisov  * @start:    start of the logical range btrfs_get_extent() is requesting
5929ad37bb3SNikolay Borisov  * @len:      length of the logical range btrfs_get_extent() is requesting
593c04e61b5SLiu Bo  *
594c04e61b5SLiu Bo  * Note that @em_in's range may be different from [start, start+len),
595c04e61b5SLiu Bo  * but they must be overlapped.
596c04e61b5SLiu Bo  *
597c04e61b5SLiu Bo  * Insert @em_in into @em_tree. In case there is an overlapping range, handle
598c04e61b5SLiu Bo  * the -EEXIST by either:
599c04e61b5SLiu Bo  * a) Returning the existing extent in @em_in if @start is within the
600c04e61b5SLiu Bo  *    existing em.
601c04e61b5SLiu Bo  * b) Merge the existing extent with @em_in passed in.
602c04e61b5SLiu Bo  *
603c04e61b5SLiu Bo  * Return 0 on success, otherwise -EEXIST.
604c04e61b5SLiu Bo  *
605c04e61b5SLiu Bo  */
606f46b24c9SDavid Sterba int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info,
607f46b24c9SDavid Sterba 			     struct extent_map_tree *em_tree,
608c04e61b5SLiu Bo 			     struct extent_map **em_in, u64 start, u64 len)
609c04e61b5SLiu Bo {
610c04e61b5SLiu Bo 	int ret;
611c04e61b5SLiu Bo 	struct extent_map *em = *em_in;
612c04e61b5SLiu Bo 
613c04e61b5SLiu Bo 	ret = add_extent_mapping(em_tree, em, 0);
614c04e61b5SLiu Bo 	/* it is possible that someone inserted the extent into the tree
615c04e61b5SLiu Bo 	 * while we had the lock dropped.  It is also possible that
616c04e61b5SLiu Bo 	 * an overlapping map exists in the tree
617c04e61b5SLiu Bo 	 */
618c04e61b5SLiu Bo 	if (ret == -EEXIST) {
619c04e61b5SLiu Bo 		struct extent_map *existing;
620c04e61b5SLiu Bo 
621c04e61b5SLiu Bo 		ret = 0;
622c04e61b5SLiu Bo 
623c04e61b5SLiu Bo 		existing = search_extent_mapping(em_tree, start, len);
624393da918SLiu Bo 
625f46b24c9SDavid Sterba 		trace_btrfs_handle_em_exist(fs_info, existing, em, start, len);
626393da918SLiu Bo 
627c04e61b5SLiu Bo 		/*
628c04e61b5SLiu Bo 		 * existing will always be non-NULL, since there must be
629c04e61b5SLiu Bo 		 * extent causing the -EEXIST.
630c04e61b5SLiu Bo 		 */
631c04e61b5SLiu Bo 		if (start >= existing->start &&
632c04e61b5SLiu Bo 		    start < extent_map_end(existing)) {
633c04e61b5SLiu Bo 			free_extent_map(em);
634c04e61b5SLiu Bo 			*em_in = existing;
635c04e61b5SLiu Bo 			ret = 0;
636c04e61b5SLiu Bo 		} else {
6379a7e10e7SLiu Bo 			u64 orig_start = em->start;
6389a7e10e7SLiu Bo 			u64 orig_len = em->len;
6399a7e10e7SLiu Bo 
640c04e61b5SLiu Bo 			/*
641c04e61b5SLiu Bo 			 * The existing extent map is the one nearest to
642c04e61b5SLiu Bo 			 * the [start, start + len) range which overlaps
643c04e61b5SLiu Bo 			 */
644c04e61b5SLiu Bo 			ret = merge_extent_mapping(em_tree, existing,
645c04e61b5SLiu Bo 						   em, start);
646c04e61b5SLiu Bo 			if (ret) {
647c04e61b5SLiu Bo 				free_extent_map(em);
648c04e61b5SLiu Bo 				*em_in = NULL;
6499a7e10e7SLiu Bo 				WARN_ONCE(ret,
6509a7e10e7SLiu Bo "unexpected error %d: merge existing(start %llu len %llu) with em(start %llu len %llu)\n",
6519a7e10e7SLiu Bo 					  ret, existing->start, existing->len,
6529a7e10e7SLiu Bo 					  orig_start, orig_len);
653c04e61b5SLiu Bo 			}
6549a7e10e7SLiu Bo 			free_extent_map(existing);
655c04e61b5SLiu Bo 		}
656c04e61b5SLiu Bo 	}
657c04e61b5SLiu Bo 
658c04e61b5SLiu Bo 	ASSERT(ret == 0 || ret == -EEXIST);
659c04e61b5SLiu Bo 	return ret;
660c04e61b5SLiu Bo }
661