xref: /openbmc/linux/fs/btrfs/extent_map.c (revision 6c05813ebb5a634add71f942a21b29eb6ff09695)
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"
104c0c8cfcSFilipe Manana #include "btrfs_inode.h"
11a52d9a80SChris Mason 
1286479a04SChris Mason 
13a52d9a80SChris Mason static struct kmem_cache *extent_map_cache;
14ca664626SChris Mason 
152f4cbe64SWyatt Banks int __init extent_map_init(void)
16a52d9a80SChris Mason {
17837e1972SDavid Sterba 	extent_map_cache = kmem_cache_create("btrfs_extent_map",
186d36dcd4SChris Mason 			sizeof(struct extent_map), 0,
19fba4b697SNikolay Borisov 			SLAB_MEM_SPREAD, NULL);
202f4cbe64SWyatt Banks 	if (!extent_map_cache)
212f4cbe64SWyatt Banks 		return -ENOMEM;
222f4cbe64SWyatt Banks 	return 0;
23a52d9a80SChris Mason }
24a52d9a80SChris Mason 
25e67c718bSDavid Sterba void __cold extent_map_exit(void)
26a52d9a80SChris Mason {
27a52d9a80SChris Mason 	kmem_cache_destroy(extent_map_cache);
28a52d9a80SChris Mason }
29a52d9a80SChris Mason 
309d2423c5SChristoph Hellwig /**
319d2423c5SChristoph Hellwig  * extent_map_tree_init - initialize extent map tree
329d2423c5SChristoph Hellwig  * @tree:		tree to initialize
339d2423c5SChristoph Hellwig  *
349d2423c5SChristoph Hellwig  * Initialize the extent tree @tree.  Should be called for each new inode
359d2423c5SChristoph Hellwig  * or other user of the extent_map interface.
369d2423c5SChristoph Hellwig  */
37a8067e02SDavid Sterba void extent_map_tree_init(struct extent_map_tree *tree)
38a52d9a80SChris Mason {
3907e1ce09SLiu Bo 	tree->map = RB_ROOT_CACHED;
405dc562c5SJosef Bacik 	INIT_LIST_HEAD(&tree->modified_extents);
41890871beSChris Mason 	rwlock_init(&tree->lock);
42a52d9a80SChris Mason }
43a52d9a80SChris Mason 
449d2423c5SChristoph Hellwig /**
459d2423c5SChristoph Hellwig  * alloc_extent_map - allocate new extent map structure
469d2423c5SChristoph Hellwig  *
479d2423c5SChristoph Hellwig  * Allocate a new extent_map structure.  The new structure is
489d2423c5SChristoph Hellwig  * returned with a reference count of one and needs to be
499d2423c5SChristoph Hellwig  * freed using free_extent_map()
509d2423c5SChristoph Hellwig  */
51172ddd60SDavid Sterba struct extent_map *alloc_extent_map(void)
52a52d9a80SChris Mason {
53a52d9a80SChris Mason 	struct extent_map *em;
5470c8a91cSJosef Bacik 	em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS);
55c26a9203STsutomu Itoh 	if (!em)
56c26a9203STsutomu Itoh 		return NULL;
57cbc0e928SFilipe Manana 	RB_CLEAR_NODE(&em->rb_node);
58261507a0SLi Zefan 	em->compress_type = BTRFS_COMPRESS_NONE;
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 	if (refcount_dec_and_test(&em->refs)) {
76cbc0e928SFilipe Manana 		WARN_ON(extent_map_in_tree(em));
775dc562c5SJosef Bacik 		WARN_ON(!list_empty(&em->list));
78298a8f9cSWang Shilong 		if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
7995617d69SJeff Mahoney 			kfree(em->map_lookup);
80a52d9a80SChris Mason 		kmem_cache_free(extent_map_cache, em);
81a52d9a80SChris Mason 	}
82a52d9a80SChris Mason }
83a52d9a80SChris Mason 
8432193c14SFilipe David Borba Manana /* simple helper to do math around the end of an extent, handling wrap */
8532193c14SFilipe David Borba Manana static u64 range_end(u64 start, u64 len)
8632193c14SFilipe David Borba Manana {
8732193c14SFilipe David Borba Manana 	if (start + len < start)
8832193c14SFilipe David Borba Manana 		return (u64)-1;
8932193c14SFilipe David Borba Manana 	return start + len;
9032193c14SFilipe David Borba Manana }
9132193c14SFilipe David Borba Manana 
9207e1ce09SLiu Bo static int tree_insert(struct rb_root_cached *root, struct extent_map *em)
93a52d9a80SChris Mason {
9407e1ce09SLiu Bo 	struct rb_node **p = &root->rb_root.rb_node;
95a52d9a80SChris Mason 	struct rb_node *parent = NULL;
9632193c14SFilipe David Borba Manana 	struct extent_map *entry = NULL;
9732193c14SFilipe David Borba Manana 	struct rb_node *orig_parent = NULL;
9832193c14SFilipe David Borba Manana 	u64 end = range_end(em->start, em->len);
9907e1ce09SLiu Bo 	bool leftmost = true;
100a52d9a80SChris Mason 
101a52d9a80SChris Mason 	while (*p) {
102a52d9a80SChris Mason 		parent = *p;
103d1310b2eSChris Mason 		entry = rb_entry(parent, struct extent_map, rb_node);
104d1310b2eSChris Mason 
10507e1ce09SLiu Bo 		if (em->start < entry->start) {
106a52d9a80SChris Mason 			p = &(*p)->rb_left;
10707e1ce09SLiu Bo 		} else if (em->start >= extent_map_end(entry)) {
108a52d9a80SChris Mason 			p = &(*p)->rb_right;
10907e1ce09SLiu Bo 			leftmost = false;
11007e1ce09SLiu Bo 		} else {
11132193c14SFilipe David Borba Manana 			return -EEXIST;
112a52d9a80SChris Mason 		}
11307e1ce09SLiu Bo 	}
114a52d9a80SChris Mason 
11532193c14SFilipe David Borba Manana 	orig_parent = parent;
11632193c14SFilipe David Borba Manana 	while (parent && em->start >= extent_map_end(entry)) {
11732193c14SFilipe David Borba Manana 		parent = rb_next(parent);
11832193c14SFilipe David Borba Manana 		entry = rb_entry(parent, struct extent_map, rb_node);
11932193c14SFilipe David Borba Manana 	}
12032193c14SFilipe David Borba Manana 	if (parent)
12132193c14SFilipe David Borba Manana 		if (end > entry->start && em->start < extent_map_end(entry))
12232193c14SFilipe David Borba Manana 			return -EEXIST;
12332193c14SFilipe David Borba Manana 
12432193c14SFilipe David Borba Manana 	parent = orig_parent;
12532193c14SFilipe David Borba Manana 	entry = rb_entry(parent, struct extent_map, rb_node);
12632193c14SFilipe David Borba Manana 	while (parent && em->start < entry->start) {
12732193c14SFilipe David Borba Manana 		parent = rb_prev(parent);
12832193c14SFilipe David Borba Manana 		entry = rb_entry(parent, struct extent_map, rb_node);
12932193c14SFilipe David Borba Manana 	}
13032193c14SFilipe David Borba Manana 	if (parent)
13132193c14SFilipe David Borba Manana 		if (end > entry->start && em->start < extent_map_end(entry))
13232193c14SFilipe David Borba Manana 			return -EEXIST;
13332193c14SFilipe David Borba Manana 
13432193c14SFilipe David Borba Manana 	rb_link_node(&em->rb_node, orig_parent, p);
13507e1ce09SLiu Bo 	rb_insert_color_cached(&em->rb_node, root, leftmost);
13632193c14SFilipe David Borba Manana 	return 0;
137a52d9a80SChris Mason }
138a52d9a80SChris Mason 
139d352ac68SChris Mason /*
140d352ac68SChris Mason  * search through the tree for an extent_map with a given offset.  If
141d352ac68SChris Mason  * it can't be found, try to find some neighboring extents
142d352ac68SChris Mason  */
143a52d9a80SChris Mason static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
144*6c05813eSFilipe Manana 				     struct rb_node **prev_or_next_ret)
145a52d9a80SChris Mason {
146a52d9a80SChris Mason 	struct rb_node *n = root->rb_node;
147a52d9a80SChris Mason 	struct rb_node *prev = NULL;
1485f56406aSChris Mason 	struct rb_node *orig_prev = NULL;
149d1310b2eSChris Mason 	struct extent_map *entry;
150d1310b2eSChris Mason 	struct extent_map *prev_entry = NULL;
151a52d9a80SChris Mason 
152*6c05813eSFilipe Manana 	ASSERT(prev_or_next_ret);
15308f088ddSFilipe Manana 
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 	orig_prev = prev;
168d1310b2eSChris Mason 	while (prev && offset >= extent_map_end(prev_entry)) {
169a52d9a80SChris Mason 		prev = rb_next(prev);
170d1310b2eSChris Mason 		prev_entry = rb_entry(prev, struct extent_map, rb_node);
171a52d9a80SChris Mason 	}
1725f56406aSChris Mason 
173*6c05813eSFilipe Manana 	/*
174*6c05813eSFilipe Manana 	 * Previous extent map found, return as in this case the caller does not
175*6c05813eSFilipe Manana 	 * care about the next one.
176*6c05813eSFilipe Manana 	 */
177*6c05813eSFilipe Manana 	if (prev) {
178*6c05813eSFilipe Manana 		*prev_or_next_ret = prev;
179*6c05813eSFilipe Manana 		return NULL;
180*6c05813eSFilipe Manana 	}
181*6c05813eSFilipe Manana 
182*6c05813eSFilipe Manana 	prev = orig_prev;
183d1310b2eSChris Mason 	prev_entry = rb_entry(prev, struct extent_map, rb_node);
1845f56406aSChris Mason 	while (prev && offset < prev_entry->start) {
1855f56406aSChris Mason 		prev = rb_prev(prev);
186d1310b2eSChris Mason 		prev_entry = rb_entry(prev, struct extent_map, rb_node);
1875f56406aSChris Mason 	}
188*6c05813eSFilipe Manana 	*prev_or_next_ret = prev;
18908f088ddSFilipe Manana 
190a52d9a80SChris Mason 	return NULL;
191a52d9a80SChris Mason }
192a52d9a80SChris Mason 
193d352ac68SChris Mason /* check to see if two extent_map structs are adjacent and safe to merge */
194d1310b2eSChris Mason static int mergable_maps(struct extent_map *prev, struct extent_map *next)
195a52d9a80SChris Mason {
1967f3c74fbSChris Mason 	if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
1977f3c74fbSChris Mason 		return 0;
1987f3c74fbSChris Mason 
199c8b97818SChris Mason 	/*
200c8b97818SChris Mason 	 * don't merge compressed extents, we need to know their
201c8b97818SChris Mason 	 * actual size
202c8b97818SChris Mason 	 */
203c8b97818SChris Mason 	if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
204c8b97818SChris Mason 		return 0;
205c8b97818SChris Mason 
206201a9038SJosef Bacik 	if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
207201a9038SJosef Bacik 	    test_bit(EXTENT_FLAG_LOGGING, &next->flags))
208201a9038SJosef Bacik 		return 0;
209201a9038SJosef Bacik 
21009a2a8f9SJosef Bacik 	/*
21109a2a8f9SJosef Bacik 	 * We don't want to merge stuff that hasn't been written to the log yet
21209a2a8f9SJosef Bacik 	 * since it may not reflect exactly what is on disk, and that would be
21309a2a8f9SJosef Bacik 	 * bad.
21409a2a8f9SJosef Bacik 	 */
21509a2a8f9SJosef Bacik 	if (!list_empty(&prev->list) || !list_empty(&next->list))
21609a2a8f9SJosef Bacik 		return 0;
21709a2a8f9SJosef Bacik 
218951e05a9SNikolay Borisov 	ASSERT(next->block_start != EXTENT_MAP_DELALLOC &&
219951e05a9SNikolay Borisov 	       prev->block_start != EXTENT_MAP_DELALLOC);
220951e05a9SNikolay Borisov 
221c3e14909SDavid Sterba 	if (prev->map_lookup || next->map_lookup)
222c3e14909SDavid Sterba 		ASSERT(test_bit(EXTENT_FLAG_FS_MAPPING, &prev->flags) &&
223c3e14909SDavid Sterba 		       test_bit(EXTENT_FLAG_FS_MAPPING, &next->flags));
224c3e14909SDavid Sterba 
225d1310b2eSChris Mason 	if (extent_map_end(prev) == next->start &&
226d1310b2eSChris Mason 	    prev->flags == next->flags &&
227c3e14909SDavid Sterba 	    prev->map_lookup == next->map_lookup &&
228d1310b2eSChris Mason 	    ((next->block_start == EXTENT_MAP_HOLE &&
229d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_HOLE) ||
230d1310b2eSChris Mason 	     (next->block_start == EXTENT_MAP_INLINE &&
231d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_INLINE) ||
232d1310b2eSChris Mason 	     (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
233d1310b2eSChris Mason 	      next->block_start == extent_map_block_end(prev)))) {
234d1310b2eSChris Mason 		return 1;
235d1310b2eSChris Mason 	}
236a52d9a80SChris Mason 	return 0;
237a52d9a80SChris Mason }
238a52d9a80SChris Mason 
2394d2c8f62SLi Zefan static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
240a1ed835eSChris Mason {
241a1ed835eSChris Mason 	struct extent_map *merge = NULL;
242a1ed835eSChris Mason 	struct rb_node *rb;
243a1ed835eSChris Mason 
244ac05ca91SFilipe Manana 	/*
245ac05ca91SFilipe Manana 	 * We can't modify an extent map that is in the tree and that is being
246ac05ca91SFilipe Manana 	 * used by another task, as it can cause that other task to see it in
247ac05ca91SFilipe Manana 	 * inconsistent state during the merging. We always have 1 reference for
248ac05ca91SFilipe Manana 	 * the tree and 1 for this task (which is unpinning the extent map or
249ac05ca91SFilipe Manana 	 * clearing the logging flag), so anything > 2 means it's being used by
250ac05ca91SFilipe Manana 	 * other tasks too.
251ac05ca91SFilipe Manana 	 */
252ac05ca91SFilipe Manana 	if (refcount_read(&em->refs) > 2)
253ac05ca91SFilipe Manana 		return;
254ac05ca91SFilipe Manana 
255a1ed835eSChris Mason 	if (em->start != 0) {
256a1ed835eSChris Mason 		rb = rb_prev(&em->rb_node);
257a1ed835eSChris Mason 		if (rb)
258a1ed835eSChris Mason 			merge = rb_entry(rb, struct extent_map, rb_node);
259a1ed835eSChris Mason 		if (rb && mergable_maps(merge, em)) {
260a1ed835eSChris Mason 			em->start = merge->start;
26170c8a91cSJosef Bacik 			em->orig_start = merge->orig_start;
262a1ed835eSChris Mason 			em->len += merge->len;
263a1ed835eSChris Mason 			em->block_len += merge->block_len;
264a1ed835eSChris Mason 			em->block_start = merge->block_start;
26570c8a91cSJosef Bacik 			em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
26670c8a91cSJosef Bacik 			em->mod_start = merge->mod_start;
26770c8a91cSJosef Bacik 			em->generation = max(em->generation, merge->generation);
268199257a7SQu Wenruo 			set_bit(EXTENT_FLAG_MERGED, &em->flags);
2695dc562c5SJosef Bacik 
27007e1ce09SLiu Bo 			rb_erase_cached(&merge->rb_node, &tree->map);
271cbc0e928SFilipe Manana 			RB_CLEAR_NODE(&merge->rb_node);
272a1ed835eSChris Mason 			free_extent_map(merge);
273a1ed835eSChris Mason 		}
274a1ed835eSChris Mason 	}
275a1ed835eSChris Mason 
276a1ed835eSChris Mason 	rb = rb_next(&em->rb_node);
277a1ed835eSChris Mason 	if (rb)
278a1ed835eSChris Mason 		merge = rb_entry(rb, struct extent_map, rb_node);
279a1ed835eSChris Mason 	if (rb && mergable_maps(em, merge)) {
280a1ed835eSChris Mason 		em->len += merge->len;
281d527afe1SFilipe David Borba Manana 		em->block_len += merge->block_len;
28207e1ce09SLiu Bo 		rb_erase_cached(&merge->rb_node, &tree->map);
283cbc0e928SFilipe Manana 		RB_CLEAR_NODE(&merge->rb_node);
28470c8a91cSJosef Bacik 		em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
28570c8a91cSJosef Bacik 		em->generation = max(em->generation, merge->generation);
286199257a7SQu Wenruo 		set_bit(EXTENT_FLAG_MERGED, &em->flags);
287a1ed835eSChris Mason 		free_extent_map(merge);
288a1ed835eSChris Mason 	}
2894d2c8f62SLi Zefan }
2904d2c8f62SLi Zefan 
2915dc562c5SJosef Bacik /**
29252b1de91SLiu Bo  * unpin_extent_cache - unpin an extent from the cache
2935dc562c5SJosef Bacik  * @tree:	tree to unpin the extent in
2945dc562c5SJosef Bacik  * @start:	logical offset in the file
2955dc562c5SJosef Bacik  * @len:	length of the extent
2965dc562c5SJosef Bacik  * @gen:	generation that this extent has been modified in
2975dc562c5SJosef Bacik  *
2985dc562c5SJosef Bacik  * Called after an extent has been written to disk properly.  Set the generation
2995dc562c5SJosef Bacik  * to the generation that actually added the file item to the inode so we know
3005dc562c5SJosef Bacik  * we need to sync this extent when we call fsync().
3015dc562c5SJosef Bacik  */
3025dc562c5SJosef Bacik int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
3035dc562c5SJosef Bacik 		       u64 gen)
3044d2c8f62SLi Zefan {
3054d2c8f62SLi Zefan 	int ret = 0;
3064d2c8f62SLi Zefan 	struct extent_map *em;
3074e2f84e6SLiu Bo 	bool prealloc = false;
3084d2c8f62SLi Zefan 
3094d2c8f62SLi Zefan 	write_lock(&tree->lock);
3104d2c8f62SLi Zefan 	em = lookup_extent_mapping(tree, start, len);
3114d2c8f62SLi Zefan 
3124d2c8f62SLi Zefan 	WARN_ON(!em || em->start != start);
3134d2c8f62SLi Zefan 
3144d2c8f62SLi Zefan 	if (!em)
3154d2c8f62SLi Zefan 		goto out;
3164d2c8f62SLi Zefan 
3175dc562c5SJosef Bacik 	em->generation = gen;
3184d2c8f62SLi Zefan 	clear_bit(EXTENT_FLAG_PINNED, &em->flags);
3194e2f84e6SLiu Bo 	em->mod_start = em->start;
3204e2f84e6SLiu Bo 	em->mod_len = em->len;
3214e2f84e6SLiu Bo 
322b11e234dSJosef Bacik 	if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
3234e2f84e6SLiu Bo 		prealloc = true;
324b11e234dSJosef Bacik 		clear_bit(EXTENT_FLAG_FILLING, &em->flags);
3254e2f84e6SLiu Bo 	}
3264d2c8f62SLi Zefan 
3274d2c8f62SLi Zefan 	try_merge_map(tree, em);
3284e2f84e6SLiu Bo 
3294e2f84e6SLiu Bo 	if (prealloc) {
3304e2f84e6SLiu Bo 		em->mod_start = em->start;
3314e2f84e6SLiu Bo 		em->mod_len = em->len;
3324e2f84e6SLiu Bo 	}
3334e2f84e6SLiu Bo 
334a1ed835eSChris Mason 	free_extent_map(em);
335a1ed835eSChris Mason out:
336a1ed835eSChris Mason 	write_unlock(&tree->lock);
337a1ed835eSChris Mason 	return ret;
338a1ed835eSChris Mason 
339a1ed835eSChris Mason }
340a1ed835eSChris Mason 
341201a9038SJosef Bacik void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
342201a9038SJosef Bacik {
34374333c7dSFilipe Manana 	lockdep_assert_held_write(&tree->lock);
34474333c7dSFilipe Manana 
345201a9038SJosef Bacik 	clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
346cbc0e928SFilipe Manana 	if (extent_map_in_tree(em))
347201a9038SJosef Bacik 		try_merge_map(tree, em);
348201a9038SJosef Bacik }
349201a9038SJosef Bacik 
350176840b3SFilipe Manana static inline void setup_extent_mapping(struct extent_map_tree *tree,
351176840b3SFilipe Manana 					struct extent_map *em,
352176840b3SFilipe Manana 					int modified)
353176840b3SFilipe Manana {
354490b54d6SElena Reshetova 	refcount_inc(&em->refs);
355176840b3SFilipe Manana 	em->mod_start = em->start;
356176840b3SFilipe Manana 	em->mod_len = em->len;
357176840b3SFilipe Manana 
358176840b3SFilipe Manana 	if (modified)
359176840b3SFilipe Manana 		list_move(&em->list, &tree->modified_extents);
360176840b3SFilipe Manana 	else
361176840b3SFilipe Manana 		try_merge_map(tree, em);
362176840b3SFilipe Manana }
363176840b3SFilipe Manana 
3641c11b63eSJeff Mahoney static void extent_map_device_set_bits(struct extent_map *em, unsigned bits)
3651c11b63eSJeff Mahoney {
3661c11b63eSJeff Mahoney 	struct map_lookup *map = em->map_lookup;
3671c11b63eSJeff Mahoney 	u64 stripe_size = em->orig_block_len;
3681c11b63eSJeff Mahoney 	int i;
3691c11b63eSJeff Mahoney 
3701c11b63eSJeff Mahoney 	for (i = 0; i < map->num_stripes; i++) {
3714c664611SQu Wenruo 		struct btrfs_io_stripe *stripe = &map->stripes[i];
3721c11b63eSJeff Mahoney 		struct btrfs_device *device = stripe->dev;
3731c11b63eSJeff Mahoney 
3741c11b63eSJeff Mahoney 		set_extent_bits_nowait(&device->alloc_state, stripe->physical,
3751c11b63eSJeff Mahoney 				 stripe->physical + stripe_size - 1, bits);
3761c11b63eSJeff Mahoney 	}
3771c11b63eSJeff Mahoney }
3781c11b63eSJeff Mahoney 
3791c11b63eSJeff Mahoney static void extent_map_device_clear_bits(struct extent_map *em, unsigned bits)
3801c11b63eSJeff Mahoney {
3811c11b63eSJeff Mahoney 	struct map_lookup *map = em->map_lookup;
3821c11b63eSJeff Mahoney 	u64 stripe_size = em->orig_block_len;
3831c11b63eSJeff Mahoney 	int i;
3841c11b63eSJeff Mahoney 
3851c11b63eSJeff Mahoney 	for (i = 0; i < map->num_stripes; i++) {
3864c664611SQu Wenruo 		struct btrfs_io_stripe *stripe = &map->stripes[i];
3871c11b63eSJeff Mahoney 		struct btrfs_device *device = stripe->dev;
3881c11b63eSJeff Mahoney 
3891c11b63eSJeff Mahoney 		__clear_extent_bit(&device->alloc_state, stripe->physical,
3901c11b63eSJeff Mahoney 				   stripe->physical + stripe_size - 1, bits,
391bd015294SJosef Bacik 				   NULL, GFP_NOWAIT, NULL);
3921c11b63eSJeff Mahoney 	}
3931c11b63eSJeff Mahoney }
3941c11b63eSJeff Mahoney 
3959d2423c5SChristoph Hellwig /**
396401bd2ddSNikolay Borisov  * Add new extent map to the extent tree
397401bd2ddSNikolay Borisov  *
3989d2423c5SChristoph Hellwig  * @tree:	tree to insert new map in
3999d2423c5SChristoph Hellwig  * @em:		map to insert
400401bd2ddSNikolay Borisov  * @modified:	indicate whether the given @em should be added to the
401401bd2ddSNikolay Borisov  *	        modified list, which indicates the extent needs to be logged
4029d2423c5SChristoph Hellwig  *
4039d2423c5SChristoph Hellwig  * Insert @em into @tree or perform a simple forward/backward merge with
4049d2423c5SChristoph Hellwig  * existing mappings.  The extent_map struct passed in will be inserted
4059d2423c5SChristoph Hellwig  * into the tree directly, with an additional reference taken, or a
40625985edcSLucas De Marchi  * reference dropped if the merge attempt was successful.
407a52d9a80SChris Mason  */
408a52d9a80SChris Mason int add_extent_mapping(struct extent_map_tree *tree,
40909a2a8f9SJosef Bacik 		       struct extent_map *em, int modified)
410a52d9a80SChris Mason {
411a52d9a80SChris Mason 	int ret = 0;
412a52d9a80SChris Mason 
413d23ea3faSDavid Sterba 	lockdep_assert_held_write(&tree->lock);
414d23ea3faSDavid Sterba 
41532193c14SFilipe David Borba Manana 	ret = tree_insert(&tree->map, em);
41632193c14SFilipe David Borba Manana 	if (ret)
4177c2fe32aSChris Mason 		goto out;
41832193c14SFilipe David Borba Manana 
419176840b3SFilipe Manana 	setup_extent_mapping(tree, em, modified);
4208811133dSNikolay Borisov 	if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags)) {
4211c11b63eSJeff Mahoney 		extent_map_device_set_bits(em, CHUNK_ALLOCATED);
4228811133dSNikolay Borisov 		extent_map_device_clear_bits(em, CHUNK_TRIMMED);
4238811133dSNikolay Borisov 	}
424a52d9a80SChris Mason out:
425a52d9a80SChris Mason 	return ret;
426a52d9a80SChris Mason }
427a52d9a80SChris Mason 
42848a3b636SEric Sandeen static struct extent_map *
42948a3b636SEric Sandeen __lookup_extent_mapping(struct extent_map_tree *tree,
430ed64f066SLi Zefan 			u64 start, u64 len, int strict)
431ed64f066SLi Zefan {
432ed64f066SLi Zefan 	struct extent_map *em;
433ed64f066SLi Zefan 	struct rb_node *rb_node;
434*6c05813eSFilipe Manana 	struct rb_node *prev_or_next = NULL;
435ed64f066SLi Zefan 	u64 end = range_end(start, len);
436ed64f066SLi Zefan 
437*6c05813eSFilipe Manana 	rb_node = __tree_search(&tree->map.rb_root, start, &prev_or_next);
438ed64f066SLi Zefan 	if (!rb_node) {
439*6c05813eSFilipe Manana 		if (prev_or_next)
440*6c05813eSFilipe Manana 			rb_node = prev_or_next;
441ed64f066SLi Zefan 		else
442ed64f066SLi Zefan 			return NULL;
443ed64f066SLi Zefan 	}
444ed64f066SLi Zefan 
445ed64f066SLi Zefan 	em = rb_entry(rb_node, struct extent_map, rb_node);
446ed64f066SLi Zefan 
447ed64f066SLi Zefan 	if (strict && !(end > em->start && start < extent_map_end(em)))
448ed64f066SLi Zefan 		return NULL;
449ed64f066SLi Zefan 
450490b54d6SElena Reshetova 	refcount_inc(&em->refs);
451ed64f066SLi Zefan 	return em;
452ed64f066SLi Zefan }
453ed64f066SLi Zefan 
4549d2423c5SChristoph Hellwig /**
4559d2423c5SChristoph Hellwig  * lookup_extent_mapping - lookup extent_map
4569d2423c5SChristoph Hellwig  * @tree:	tree to lookup in
4579d2423c5SChristoph Hellwig  * @start:	byte offset to start the search
4589d2423c5SChristoph Hellwig  * @len:	length of the lookup range
4599d2423c5SChristoph Hellwig  *
4609d2423c5SChristoph Hellwig  * Find and return the first extent_map struct in @tree that intersects the
4619d2423c5SChristoph Hellwig  * [start, len] range.  There may be additional objects in the tree that
4629d2423c5SChristoph Hellwig  * intersect, so check the object returned carefully to make sure that no
4639d2423c5SChristoph Hellwig  * additional lookups are needed.
464a52d9a80SChris Mason  */
465a52d9a80SChris Mason struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
466d1310b2eSChris Mason 					 u64 start, u64 len)
467a52d9a80SChris Mason {
468ed64f066SLi Zefan 	return __lookup_extent_mapping(tree, start, len, 1);
469a52d9a80SChris Mason }
470a52d9a80SChris Mason 
4719d2423c5SChristoph Hellwig /**
472b917b7c3SChris Mason  * search_extent_mapping - find a nearby extent map
473b917b7c3SChris Mason  * @tree:	tree to lookup in
474b917b7c3SChris Mason  * @start:	byte offset to start the search
475b917b7c3SChris Mason  * @len:	length of the lookup range
476b917b7c3SChris Mason  *
477b917b7c3SChris Mason  * Find and return the first extent_map struct in @tree that intersects the
478b917b7c3SChris Mason  * [start, len] range.
479b917b7c3SChris Mason  *
480b917b7c3SChris Mason  * If one can't be found, any nearby extent may be returned
481b917b7c3SChris Mason  */
482b917b7c3SChris Mason struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
483b917b7c3SChris Mason 					 u64 start, u64 len)
484b917b7c3SChris Mason {
485ed64f066SLi Zefan 	return __lookup_extent_mapping(tree, start, len, 0);
486b917b7c3SChris Mason }
487b917b7c3SChris Mason 
488b917b7c3SChris Mason /**
4899d2423c5SChristoph Hellwig  * remove_extent_mapping - removes an extent_map from the extent tree
4909d2423c5SChristoph Hellwig  * @tree:	extent tree to remove from
491bb7ab3b9SAdam Buchbinder  * @em:		extent map being removed
4929d2423c5SChristoph Hellwig  *
4939d2423c5SChristoph Hellwig  * Removes @em from @tree.  No reference counts are dropped, and no checks
4949d2423c5SChristoph Hellwig  * are done to see if the range is in use
495a52d9a80SChris Mason  */
496c1766dd7Szhong jiang void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
497a52d9a80SChris Mason {
4986d3b050eSFilipe Manana 	lockdep_assert_held_write(&tree->lock);
4996d3b050eSFilipe Manana 
5007f3c74fbSChris Mason 	WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
50107e1ce09SLiu Bo 	rb_erase_cached(&em->rb_node, &tree->map);
502ff44c6e3SJosef Bacik 	if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
5035dc562c5SJosef Bacik 		list_del_init(&em->list);
5041c11b63eSJeff Mahoney 	if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
5051c11b63eSJeff Mahoney 		extent_map_device_clear_bits(em, CHUNK_ALLOCATED);
506cbc0e928SFilipe Manana 	RB_CLEAR_NODE(&em->rb_node);
507a52d9a80SChris Mason }
508176840b3SFilipe Manana 
509176840b3SFilipe Manana void replace_extent_mapping(struct extent_map_tree *tree,
510176840b3SFilipe Manana 			    struct extent_map *cur,
511176840b3SFilipe Manana 			    struct extent_map *new,
512176840b3SFilipe Manana 			    int modified)
513176840b3SFilipe Manana {
5146d3b050eSFilipe Manana 	lockdep_assert_held_write(&tree->lock);
5156d3b050eSFilipe Manana 
516176840b3SFilipe Manana 	WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));
517176840b3SFilipe Manana 	ASSERT(extent_map_in_tree(cur));
518176840b3SFilipe Manana 	if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))
519176840b3SFilipe Manana 		list_del_init(&cur->list);
52007e1ce09SLiu Bo 	rb_replace_node_cached(&cur->rb_node, &new->rb_node, &tree->map);
521176840b3SFilipe Manana 	RB_CLEAR_NODE(&cur->rb_node);
522176840b3SFilipe Manana 
523176840b3SFilipe Manana 	setup_extent_mapping(tree, new, modified);
524176840b3SFilipe Manana }
525c04e61b5SLiu Bo 
526c04e61b5SLiu Bo static struct extent_map *next_extent_map(struct extent_map *em)
527c04e61b5SLiu Bo {
528c04e61b5SLiu Bo 	struct rb_node *next;
529c04e61b5SLiu Bo 
530c04e61b5SLiu Bo 	next = rb_next(&em->rb_node);
531c04e61b5SLiu Bo 	if (!next)
532c04e61b5SLiu Bo 		return NULL;
533c04e61b5SLiu Bo 	return container_of(next, struct extent_map, rb_node);
534c04e61b5SLiu Bo }
535c04e61b5SLiu Bo 
536c04e61b5SLiu Bo static struct extent_map *prev_extent_map(struct extent_map *em)
537c04e61b5SLiu Bo {
538c04e61b5SLiu Bo 	struct rb_node *prev;
539c04e61b5SLiu Bo 
540c04e61b5SLiu Bo 	prev = rb_prev(&em->rb_node);
541c04e61b5SLiu Bo 	if (!prev)
542c04e61b5SLiu Bo 		return NULL;
543c04e61b5SLiu Bo 	return container_of(prev, struct extent_map, rb_node);
544c04e61b5SLiu Bo }
545c04e61b5SLiu Bo 
54652042d8eSAndrea Gelmini /*
54752042d8eSAndrea Gelmini  * Helper for btrfs_get_extent.  Given an existing extent in the tree,
548c04e61b5SLiu Bo  * the existing extent is the nearest extent to map_start,
549c04e61b5SLiu Bo  * and an extent that you want to insert, deal with overlap and insert
550c04e61b5SLiu Bo  * the best fitted new extent into the tree.
551c04e61b5SLiu Bo  */
5525f4791f4SLiu Bo static noinline int merge_extent_mapping(struct extent_map_tree *em_tree,
553c04e61b5SLiu Bo 					 struct extent_map *existing,
554c04e61b5SLiu Bo 					 struct extent_map *em,
555c04e61b5SLiu Bo 					 u64 map_start)
556c04e61b5SLiu Bo {
557c04e61b5SLiu Bo 	struct extent_map *prev;
558c04e61b5SLiu Bo 	struct extent_map *next;
559c04e61b5SLiu Bo 	u64 start;
560c04e61b5SLiu Bo 	u64 end;
561c04e61b5SLiu Bo 	u64 start_diff;
562c04e61b5SLiu Bo 
563c04e61b5SLiu Bo 	BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
564c04e61b5SLiu Bo 
565c04e61b5SLiu Bo 	if (existing->start > map_start) {
566c04e61b5SLiu Bo 		next = existing;
567c04e61b5SLiu Bo 		prev = prev_extent_map(next);
568c04e61b5SLiu Bo 	} else {
569c04e61b5SLiu Bo 		prev = existing;
570c04e61b5SLiu Bo 		next = next_extent_map(prev);
571c04e61b5SLiu Bo 	}
572c04e61b5SLiu Bo 
573c04e61b5SLiu Bo 	start = prev ? extent_map_end(prev) : em->start;
574c04e61b5SLiu Bo 	start = max_t(u64, start, em->start);
575c04e61b5SLiu Bo 	end = next ? next->start : extent_map_end(em);
576c04e61b5SLiu Bo 	end = min_t(u64, end, extent_map_end(em));
577c04e61b5SLiu Bo 	start_diff = start - em->start;
578c04e61b5SLiu Bo 	em->start = start;
579c04e61b5SLiu Bo 	em->len = end - start;
580c04e61b5SLiu Bo 	if (em->block_start < EXTENT_MAP_LAST_BYTE &&
581c04e61b5SLiu Bo 	    !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
582c04e61b5SLiu Bo 		em->block_start += start_diff;
583c04e61b5SLiu Bo 		em->block_len = em->len;
584c04e61b5SLiu Bo 	}
585c04e61b5SLiu Bo 	return add_extent_mapping(em_tree, em, 0);
586c04e61b5SLiu Bo }
587c04e61b5SLiu Bo 
588c04e61b5SLiu Bo /**
5899ad37bb3SNikolay Borisov  * Add extent mapping into em_tree
5909ad37bb3SNikolay Borisov  *
5919ad37bb3SNikolay Borisov  * @fs_info:  the filesystem
5929ad37bb3SNikolay Borisov  * @em_tree:  extent tree into which we want to insert the extent mapping
5939ad37bb3SNikolay Borisov  * @em_in:    extent we are inserting
5949ad37bb3SNikolay Borisov  * @start:    start of the logical range btrfs_get_extent() is requesting
5959ad37bb3SNikolay Borisov  * @len:      length of the logical range btrfs_get_extent() is requesting
596c04e61b5SLiu Bo  *
597c04e61b5SLiu Bo  * Note that @em_in's range may be different from [start, start+len),
598c04e61b5SLiu Bo  * but they must be overlapped.
599c04e61b5SLiu Bo  *
600c04e61b5SLiu Bo  * Insert @em_in into @em_tree. In case there is an overlapping range, handle
601c04e61b5SLiu Bo  * the -EEXIST by either:
602c04e61b5SLiu Bo  * a) Returning the existing extent in @em_in if @start is within the
603c04e61b5SLiu Bo  *    existing em.
604c04e61b5SLiu Bo  * b) Merge the existing extent with @em_in passed in.
605c04e61b5SLiu Bo  *
606c04e61b5SLiu Bo  * Return 0 on success, otherwise -EEXIST.
607c04e61b5SLiu Bo  *
608c04e61b5SLiu Bo  */
609f46b24c9SDavid Sterba int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info,
610f46b24c9SDavid Sterba 			     struct extent_map_tree *em_tree,
611c04e61b5SLiu Bo 			     struct extent_map **em_in, u64 start, u64 len)
612c04e61b5SLiu Bo {
613c04e61b5SLiu Bo 	int ret;
614c04e61b5SLiu Bo 	struct extent_map *em = *em_in;
615c04e61b5SLiu Bo 
616c04e61b5SLiu Bo 	ret = add_extent_mapping(em_tree, em, 0);
617c04e61b5SLiu Bo 	/* it is possible that someone inserted the extent into the tree
618c04e61b5SLiu Bo 	 * while we had the lock dropped.  It is also possible that
619c04e61b5SLiu Bo 	 * an overlapping map exists in the tree
620c04e61b5SLiu Bo 	 */
621c04e61b5SLiu Bo 	if (ret == -EEXIST) {
622c04e61b5SLiu Bo 		struct extent_map *existing;
623c04e61b5SLiu Bo 
624c04e61b5SLiu Bo 		ret = 0;
625c04e61b5SLiu Bo 
626c04e61b5SLiu Bo 		existing = search_extent_mapping(em_tree, start, len);
627393da918SLiu Bo 
628f46b24c9SDavid Sterba 		trace_btrfs_handle_em_exist(fs_info, existing, em, start, len);
629393da918SLiu Bo 
630c04e61b5SLiu Bo 		/*
631c04e61b5SLiu Bo 		 * existing will always be non-NULL, since there must be
632c04e61b5SLiu Bo 		 * extent causing the -EEXIST.
633c04e61b5SLiu Bo 		 */
634c04e61b5SLiu Bo 		if (start >= existing->start &&
635c04e61b5SLiu Bo 		    start < extent_map_end(existing)) {
636c04e61b5SLiu Bo 			free_extent_map(em);
637c04e61b5SLiu Bo 			*em_in = existing;
638c04e61b5SLiu Bo 			ret = 0;
639c04e61b5SLiu Bo 		} else {
6409a7e10e7SLiu Bo 			u64 orig_start = em->start;
6419a7e10e7SLiu Bo 			u64 orig_len = em->len;
6429a7e10e7SLiu Bo 
643c04e61b5SLiu Bo 			/*
644c04e61b5SLiu Bo 			 * The existing extent map is the one nearest to
645c04e61b5SLiu Bo 			 * the [start, start + len) range which overlaps
646c04e61b5SLiu Bo 			 */
647c04e61b5SLiu Bo 			ret = merge_extent_mapping(em_tree, existing,
648c04e61b5SLiu Bo 						   em, start);
649c04e61b5SLiu Bo 			if (ret) {
650c04e61b5SLiu Bo 				free_extent_map(em);
651c04e61b5SLiu Bo 				*em_in = NULL;
6529a7e10e7SLiu Bo 				WARN_ONCE(ret,
6539a7e10e7SLiu Bo "unexpected error %d: merge existing(start %llu len %llu) with em(start %llu len %llu)\n",
6549a7e10e7SLiu Bo 					  ret, existing->start, existing->len,
6559a7e10e7SLiu Bo 					  orig_start, orig_len);
656c04e61b5SLiu Bo 			}
6579a7e10e7SLiu Bo 			free_extent_map(existing);
658c04e61b5SLiu Bo 		}
659c04e61b5SLiu Bo 	}
660c04e61b5SLiu Bo 
661c04e61b5SLiu Bo 	ASSERT(ret == 0 || ret == -EEXIST);
662c04e61b5SLiu Bo 	return ret;
663c04e61b5SLiu Bo }
6644c0c8cfcSFilipe Manana 
6654c0c8cfcSFilipe Manana /*
6669c9d1b4fSFilipe Manana  * Drop all extent maps from a tree in the fastest possible way, rescheduling
6679c9d1b4fSFilipe Manana  * if needed. This avoids searching the tree, from the root down to the first
6689c9d1b4fSFilipe Manana  * extent map, before each deletion.
6699c9d1b4fSFilipe Manana  */
6709c9d1b4fSFilipe Manana static void drop_all_extent_maps_fast(struct extent_map_tree *tree)
6719c9d1b4fSFilipe Manana {
6729c9d1b4fSFilipe Manana 	write_lock(&tree->lock);
6739c9d1b4fSFilipe Manana 	while (!RB_EMPTY_ROOT(&tree->map.rb_root)) {
6749c9d1b4fSFilipe Manana 		struct extent_map *em;
6759c9d1b4fSFilipe Manana 		struct rb_node *node;
6769c9d1b4fSFilipe Manana 
6779c9d1b4fSFilipe Manana 		node = rb_first_cached(&tree->map);
6789c9d1b4fSFilipe Manana 		em = rb_entry(node, struct extent_map, rb_node);
6799c9d1b4fSFilipe Manana 		clear_bit(EXTENT_FLAG_PINNED, &em->flags);
6809c9d1b4fSFilipe Manana 		clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
6819c9d1b4fSFilipe Manana 		remove_extent_mapping(tree, em);
6829c9d1b4fSFilipe Manana 		free_extent_map(em);
6839c9d1b4fSFilipe Manana 		cond_resched_rwlock_write(&tree->lock);
6849c9d1b4fSFilipe Manana 	}
6859c9d1b4fSFilipe Manana 	write_unlock(&tree->lock);
6869c9d1b4fSFilipe Manana }
6879c9d1b4fSFilipe Manana 
6889c9d1b4fSFilipe Manana /*
6894c0c8cfcSFilipe Manana  * Drop all extent maps in a given range.
6904c0c8cfcSFilipe Manana  *
6914c0c8cfcSFilipe Manana  * @inode:       The target inode.
6924c0c8cfcSFilipe Manana  * @start:       Start offset of the range.
6934c0c8cfcSFilipe Manana  * @end:         End offset of the range (inclusive value).
6944c0c8cfcSFilipe Manana  * @skip_pinned: Indicate if pinned extent maps should be ignored or not.
6954c0c8cfcSFilipe Manana  *
6964c0c8cfcSFilipe Manana  * This drops all the extent maps that intersect the given range [@start, @end].
6974c0c8cfcSFilipe Manana  * Extent maps that partially overlap the range and extend behind or beyond it,
6984c0c8cfcSFilipe Manana  * are split.
6994c0c8cfcSFilipe Manana  * The caller should have locked an appropriate file range in the inode's io
7004c0c8cfcSFilipe Manana  * tree before calling this function.
7014c0c8cfcSFilipe Manana  */
7024c0c8cfcSFilipe Manana void btrfs_drop_extent_map_range(struct btrfs_inode *inode, u64 start, u64 end,
7034c0c8cfcSFilipe Manana 				 bool skip_pinned)
7044c0c8cfcSFilipe Manana {
7054c0c8cfcSFilipe Manana 	struct extent_map *split = NULL;
7064c0c8cfcSFilipe Manana 	struct extent_map *split2 = NULL;
7074c0c8cfcSFilipe Manana 	struct extent_map_tree *em_tree = &inode->extent_tree;
7084c0c8cfcSFilipe Manana 	u64 len = end - start + 1;
7094c0c8cfcSFilipe Manana 	bool testend = true;
7104c0c8cfcSFilipe Manana 
7114c0c8cfcSFilipe Manana 	WARN_ON(end < start);
7124c0c8cfcSFilipe Manana 	if (end == (u64)-1) {
7139c9d1b4fSFilipe Manana 		if (start == 0 && !skip_pinned) {
7149c9d1b4fSFilipe Manana 			drop_all_extent_maps_fast(em_tree);
7159c9d1b4fSFilipe Manana 			return;
7169c9d1b4fSFilipe Manana 		}
7174c0c8cfcSFilipe Manana 		len = (u64)-1;
7184c0c8cfcSFilipe Manana 		testend = false;
7194c0c8cfcSFilipe Manana 	}
7204c0c8cfcSFilipe Manana 	while (1) {
7214c0c8cfcSFilipe Manana 		struct extent_map *em;
722f3109e33SFilipe Manana 		u64 em_end;
7234c0c8cfcSFilipe Manana 		u64 gen;
7244c0c8cfcSFilipe Manana 		unsigned long flags;
7254c0c8cfcSFilipe Manana 		bool ends_after_range = false;
7264c0c8cfcSFilipe Manana 		bool no_splits = false;
7274c0c8cfcSFilipe Manana 		bool modified;
7284c0c8cfcSFilipe Manana 		bool compressed;
7294c0c8cfcSFilipe Manana 
7304c0c8cfcSFilipe Manana 		if (!split)
7314c0c8cfcSFilipe Manana 			split = alloc_extent_map();
7324c0c8cfcSFilipe Manana 		if (!split2)
7334c0c8cfcSFilipe Manana 			split2 = alloc_extent_map();
7344c0c8cfcSFilipe Manana 		if (!split || !split2)
7354c0c8cfcSFilipe Manana 			no_splits = true;
7364c0c8cfcSFilipe Manana 
7374c0c8cfcSFilipe Manana 		write_lock(&em_tree->lock);
7384c0c8cfcSFilipe Manana 		em = lookup_extent_mapping(em_tree, start, len);
7394c0c8cfcSFilipe Manana 		if (!em) {
7404c0c8cfcSFilipe Manana 			write_unlock(&em_tree->lock);
7414c0c8cfcSFilipe Manana 			break;
7424c0c8cfcSFilipe Manana 		}
743f3109e33SFilipe Manana 		em_end = extent_map_end(em);
744f3109e33SFilipe Manana 		if (testend && em_end > start + len)
7454c0c8cfcSFilipe Manana 			ends_after_range = true;
7464c0c8cfcSFilipe Manana 		if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
7474c0c8cfcSFilipe Manana 			if (ends_after_range) {
7484c0c8cfcSFilipe Manana 				free_extent_map(em);
7494c0c8cfcSFilipe Manana 				write_unlock(&em_tree->lock);
7504c0c8cfcSFilipe Manana 				break;
7514c0c8cfcSFilipe Manana 			}
752f3109e33SFilipe Manana 			start = em_end;
7534c0c8cfcSFilipe Manana 			if (testend)
754f3109e33SFilipe Manana 				len = start + len - em_end;
7554c0c8cfcSFilipe Manana 			free_extent_map(em);
7564c0c8cfcSFilipe Manana 			write_unlock(&em_tree->lock);
7574c0c8cfcSFilipe Manana 			continue;
7584c0c8cfcSFilipe Manana 		}
7594c0c8cfcSFilipe Manana 		flags = em->flags;
7604c0c8cfcSFilipe Manana 		gen = em->generation;
7614c0c8cfcSFilipe Manana 		compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7624c0c8cfcSFilipe Manana 		clear_bit(EXTENT_FLAG_PINNED, &em->flags);
7634c0c8cfcSFilipe Manana 		clear_bit(EXTENT_FLAG_LOGGING, &flags);
7644c0c8cfcSFilipe Manana 		modified = !list_empty(&em->list);
7654c0c8cfcSFilipe Manana 		if (no_splits)
7664c0c8cfcSFilipe Manana 			goto next;
7674c0c8cfcSFilipe Manana 
7684c0c8cfcSFilipe Manana 		if (em->start < start) {
7694c0c8cfcSFilipe Manana 			split->start = em->start;
7704c0c8cfcSFilipe Manana 			split->len = start - em->start;
7714c0c8cfcSFilipe Manana 
7724c0c8cfcSFilipe Manana 			if (em->block_start < EXTENT_MAP_LAST_BYTE) {
7734c0c8cfcSFilipe Manana 				split->orig_start = em->orig_start;
7744c0c8cfcSFilipe Manana 				split->block_start = em->block_start;
7754c0c8cfcSFilipe Manana 
7764c0c8cfcSFilipe Manana 				if (compressed)
7774c0c8cfcSFilipe Manana 					split->block_len = em->block_len;
7784c0c8cfcSFilipe Manana 				else
7794c0c8cfcSFilipe Manana 					split->block_len = split->len;
7804c0c8cfcSFilipe Manana 				split->orig_block_len = max(split->block_len,
7814c0c8cfcSFilipe Manana 						em->orig_block_len);
7824c0c8cfcSFilipe Manana 				split->ram_bytes = em->ram_bytes;
7834c0c8cfcSFilipe Manana 			} else {
7844c0c8cfcSFilipe Manana 				split->orig_start = split->start;
7854c0c8cfcSFilipe Manana 				split->block_len = 0;
7864c0c8cfcSFilipe Manana 				split->block_start = em->block_start;
7874c0c8cfcSFilipe Manana 				split->orig_block_len = 0;
7884c0c8cfcSFilipe Manana 				split->ram_bytes = split->len;
7894c0c8cfcSFilipe Manana 			}
7904c0c8cfcSFilipe Manana 
7914c0c8cfcSFilipe Manana 			split->generation = gen;
7924c0c8cfcSFilipe Manana 			split->flags = flags;
7934c0c8cfcSFilipe Manana 			split->compress_type = em->compress_type;
7944c0c8cfcSFilipe Manana 			replace_extent_mapping(em_tree, em, split, modified);
7954c0c8cfcSFilipe Manana 			free_extent_map(split);
7964c0c8cfcSFilipe Manana 			split = split2;
7974c0c8cfcSFilipe Manana 			split2 = NULL;
7984c0c8cfcSFilipe Manana 		}
7994c0c8cfcSFilipe Manana 		if (ends_after_range) {
8004c0c8cfcSFilipe Manana 			split->start = start + len;
801f3109e33SFilipe Manana 			split->len = em_end - (start + len);
8024c0c8cfcSFilipe Manana 			split->block_start = em->block_start;
8034c0c8cfcSFilipe Manana 			split->flags = flags;
8044c0c8cfcSFilipe Manana 			split->compress_type = em->compress_type;
8054c0c8cfcSFilipe Manana 			split->generation = gen;
8064c0c8cfcSFilipe Manana 
8074c0c8cfcSFilipe Manana 			if (em->block_start < EXTENT_MAP_LAST_BYTE) {
8084c0c8cfcSFilipe Manana 				split->orig_block_len = max(em->block_len,
8094c0c8cfcSFilipe Manana 						    em->orig_block_len);
8104c0c8cfcSFilipe Manana 
8114c0c8cfcSFilipe Manana 				split->ram_bytes = em->ram_bytes;
8124c0c8cfcSFilipe Manana 				if (compressed) {
8134c0c8cfcSFilipe Manana 					split->block_len = em->block_len;
8144c0c8cfcSFilipe Manana 					split->orig_start = em->orig_start;
8154c0c8cfcSFilipe Manana 				} else {
8164c0c8cfcSFilipe Manana 					const u64 diff = start + len - em->start;
8174c0c8cfcSFilipe Manana 
8184c0c8cfcSFilipe Manana 					split->block_len = split->len;
8194c0c8cfcSFilipe Manana 					split->block_start += diff;
8204c0c8cfcSFilipe Manana 					split->orig_start = em->orig_start;
8214c0c8cfcSFilipe Manana 				}
8224c0c8cfcSFilipe Manana 			} else {
8234c0c8cfcSFilipe Manana 				split->ram_bytes = split->len;
8244c0c8cfcSFilipe Manana 				split->orig_start = split->start;
8254c0c8cfcSFilipe Manana 				split->block_len = 0;
8264c0c8cfcSFilipe Manana 				split->orig_block_len = 0;
8274c0c8cfcSFilipe Manana 			}
8284c0c8cfcSFilipe Manana 
8294c0c8cfcSFilipe Manana 			if (extent_map_in_tree(em)) {
8304c0c8cfcSFilipe Manana 				replace_extent_mapping(em_tree, em, split,
8314c0c8cfcSFilipe Manana 						       modified);
8324c0c8cfcSFilipe Manana 			} else {
8334c0c8cfcSFilipe Manana 				int ret;
8344c0c8cfcSFilipe Manana 
8354c0c8cfcSFilipe Manana 				ret = add_extent_mapping(em_tree, split,
8364c0c8cfcSFilipe Manana 							 modified);
8374c0c8cfcSFilipe Manana 				/* Logic error, shouldn't happen. */
8384c0c8cfcSFilipe Manana 				ASSERT(ret == 0);
8394c0c8cfcSFilipe Manana 				if (WARN_ON(ret != 0) && modified)
8404c0c8cfcSFilipe Manana 					btrfs_set_inode_full_sync(inode);
8414c0c8cfcSFilipe Manana 			}
8424c0c8cfcSFilipe Manana 			free_extent_map(split);
8434c0c8cfcSFilipe Manana 			split = NULL;
8444c0c8cfcSFilipe Manana 		}
8454c0c8cfcSFilipe Manana next:
8464c0c8cfcSFilipe Manana 		if (extent_map_in_tree(em)) {
8474c0c8cfcSFilipe Manana 			/*
8484c0c8cfcSFilipe Manana 			 * If the extent map is still in the tree it means that
8494c0c8cfcSFilipe Manana 			 * either of the following is true:
8504c0c8cfcSFilipe Manana 			 *
8514c0c8cfcSFilipe Manana 			 * 1) It fits entirely in our range (doesn't end beyond
8524c0c8cfcSFilipe Manana 			 *    it or starts before it);
8534c0c8cfcSFilipe Manana 			 *
8544c0c8cfcSFilipe Manana 			 * 2) It starts before our range and/or ends after our
8554c0c8cfcSFilipe Manana 			 *    range, and we were not able to allocate the extent
8564c0c8cfcSFilipe Manana 			 *    maps for split operations, @split and @split2.
8574c0c8cfcSFilipe Manana 			 *
8584c0c8cfcSFilipe Manana 			 * If we are at case 2) then we just remove the entire
8594c0c8cfcSFilipe Manana 			 * extent map - this is fine since if anyone needs it to
8604c0c8cfcSFilipe Manana 			 * access the subranges outside our range, will just
8614c0c8cfcSFilipe Manana 			 * load it again from the subvolume tree's file extent
8624c0c8cfcSFilipe Manana 			 * item. However if the extent map was in the list of
8634c0c8cfcSFilipe Manana 			 * modified extents, then we must mark the inode for a
8644c0c8cfcSFilipe Manana 			 * full fsync, otherwise a fast fsync will miss this
8654c0c8cfcSFilipe Manana 			 * extent if it's new and needs to be logged.
8664c0c8cfcSFilipe Manana 			 */
8674c0c8cfcSFilipe Manana 			if ((em->start < start || ends_after_range) && modified) {
8684c0c8cfcSFilipe Manana 				ASSERT(no_splits);
8694c0c8cfcSFilipe Manana 				btrfs_set_inode_full_sync(inode);
8704c0c8cfcSFilipe Manana 			}
8714c0c8cfcSFilipe Manana 			remove_extent_mapping(em_tree, em);
8724c0c8cfcSFilipe Manana 		}
8734c0c8cfcSFilipe Manana 		write_unlock(&em_tree->lock);
8744c0c8cfcSFilipe Manana 
8754c0c8cfcSFilipe Manana 		/* Once for us. */
8764c0c8cfcSFilipe Manana 		free_extent_map(em);
8774c0c8cfcSFilipe Manana 		/* And once for the tree. */
8784c0c8cfcSFilipe Manana 		free_extent_map(em);
8794c0c8cfcSFilipe Manana 	}
8804c0c8cfcSFilipe Manana 
8814c0c8cfcSFilipe Manana 	free_extent_map(split);
8824c0c8cfcSFilipe Manana 	free_extent_map(split2);
8834c0c8cfcSFilipe Manana }
884a1ba4c08SFilipe Manana 
885a1ba4c08SFilipe Manana /*
886a1ba4c08SFilipe Manana  * Replace a range in the inode's extent map tree with a new extent map.
887a1ba4c08SFilipe Manana  *
888a1ba4c08SFilipe Manana  * @inode:      The target inode.
889a1ba4c08SFilipe Manana  * @new_em:     The new extent map to add to the inode's extent map tree.
890a1ba4c08SFilipe Manana  * @modified:   Indicate if the new extent map should be added to the list of
891a1ba4c08SFilipe Manana  *              modified extents (for fast fsync tracking).
892a1ba4c08SFilipe Manana  *
893a1ba4c08SFilipe Manana  * Drops all the extent maps in the inode's extent map tree that intersect the
894a1ba4c08SFilipe Manana  * range of the new extent map and adds the new extent map to the tree.
895a1ba4c08SFilipe Manana  * The caller should have locked an appropriate file range in the inode's io
896a1ba4c08SFilipe Manana  * tree before calling this function.
897a1ba4c08SFilipe Manana  */
898a1ba4c08SFilipe Manana int btrfs_replace_extent_map_range(struct btrfs_inode *inode,
899a1ba4c08SFilipe Manana 				   struct extent_map *new_em,
900a1ba4c08SFilipe Manana 				   bool modified)
901a1ba4c08SFilipe Manana {
902a1ba4c08SFilipe Manana 	const u64 end = new_em->start + new_em->len - 1;
903a1ba4c08SFilipe Manana 	struct extent_map_tree *tree = &inode->extent_tree;
904a1ba4c08SFilipe Manana 	int ret;
905a1ba4c08SFilipe Manana 
906a1ba4c08SFilipe Manana 	ASSERT(!extent_map_in_tree(new_em));
907a1ba4c08SFilipe Manana 
908a1ba4c08SFilipe Manana 	/*
909a1ba4c08SFilipe Manana 	 * The caller has locked an appropriate file range in the inode's io
910a1ba4c08SFilipe Manana 	 * tree, but getting -EEXIST when adding the new extent map can still
911a1ba4c08SFilipe Manana 	 * happen in case there are extents that partially cover the range, and
912a1ba4c08SFilipe Manana 	 * this is due to two tasks operating on different parts of the extent.
913a1ba4c08SFilipe Manana 	 * See commit 18e83ac75bfe67 ("Btrfs: fix unexpected EEXIST from
914a1ba4c08SFilipe Manana 	 * btrfs_get_extent") for an example and details.
915a1ba4c08SFilipe Manana 	 */
916a1ba4c08SFilipe Manana 	do {
917a1ba4c08SFilipe Manana 		btrfs_drop_extent_map_range(inode, new_em->start, end, false);
918a1ba4c08SFilipe Manana 		write_lock(&tree->lock);
919a1ba4c08SFilipe Manana 		ret = add_extent_mapping(tree, new_em, modified);
920a1ba4c08SFilipe Manana 		write_unlock(&tree->lock);
921a1ba4c08SFilipe Manana 	} while (ret == -EEXIST);
922a1ba4c08SFilipe Manana 
923a1ba4c08SFilipe Manana 	return ret;
924a1ba4c08SFilipe Manana }
925