xref: /openbmc/linux/fs/btrfs/extent_map.c (revision 9b569ea0be6fb27a4985acc9325896a3edc95ede)
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>
6*9b569ea0SJosef Bacik #include "messages.h"
7261507a0SLi Zefan #include "ctree.h"
81c11b63eSJeff Mahoney #include "volumes.h"
9a52d9a80SChris Mason #include "extent_map.h"
10ebb8765bSAnand Jain #include "compression.h"
114c0c8cfcSFilipe Manana #include "btrfs_inode.h"
12a52d9a80SChris Mason 
1386479a04SChris Mason 
14a52d9a80SChris Mason static struct kmem_cache *extent_map_cache;
15ca664626SChris Mason 
162f4cbe64SWyatt Banks int __init extent_map_init(void)
17a52d9a80SChris Mason {
18837e1972SDavid Sterba 	extent_map_cache = kmem_cache_create("btrfs_extent_map",
196d36dcd4SChris Mason 			sizeof(struct extent_map), 0,
20fba4b697SNikolay Borisov 			SLAB_MEM_SPREAD, NULL);
212f4cbe64SWyatt Banks 	if (!extent_map_cache)
222f4cbe64SWyatt Banks 		return -ENOMEM;
232f4cbe64SWyatt Banks 	return 0;
24a52d9a80SChris Mason }
25a52d9a80SChris Mason 
26e67c718bSDavid Sterba void __cold extent_map_exit(void)
27a52d9a80SChris Mason {
28a52d9a80SChris Mason 	kmem_cache_destroy(extent_map_cache);
29a52d9a80SChris Mason }
30a52d9a80SChris Mason 
319d2423c5SChristoph Hellwig /**
329d2423c5SChristoph Hellwig  * extent_map_tree_init - initialize extent map tree
339d2423c5SChristoph Hellwig  * @tree:		tree to initialize
349d2423c5SChristoph Hellwig  *
359d2423c5SChristoph Hellwig  * Initialize the extent tree @tree.  Should be called for each new inode
369d2423c5SChristoph Hellwig  * or other user of the extent_map interface.
379d2423c5SChristoph Hellwig  */
38a8067e02SDavid Sterba void extent_map_tree_init(struct extent_map_tree *tree)
39a52d9a80SChris Mason {
4007e1ce09SLiu Bo 	tree->map = RB_ROOT_CACHED;
415dc562c5SJosef Bacik 	INIT_LIST_HEAD(&tree->modified_extents);
42890871beSChris Mason 	rwlock_init(&tree->lock);
43a52d9a80SChris Mason }
44a52d9a80SChris Mason 
459d2423c5SChristoph Hellwig /**
469d2423c5SChristoph Hellwig  * alloc_extent_map - allocate new extent map structure
479d2423c5SChristoph Hellwig  *
489d2423c5SChristoph Hellwig  * Allocate a new extent_map structure.  The new structure is
499d2423c5SChristoph Hellwig  * returned with a reference count of one and needs to be
509d2423c5SChristoph Hellwig  * freed using free_extent_map()
519d2423c5SChristoph Hellwig  */
52172ddd60SDavid Sterba struct extent_map *alloc_extent_map(void)
53a52d9a80SChris Mason {
54a52d9a80SChris Mason 	struct extent_map *em;
5570c8a91cSJosef Bacik 	em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS);
56c26a9203STsutomu Itoh 	if (!em)
57c26a9203STsutomu Itoh 		return NULL;
58cbc0e928SFilipe Manana 	RB_CLEAR_NODE(&em->rb_node);
59261507a0SLi Zefan 	em->compress_type = BTRFS_COMPRESS_NONE;
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 	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 
9307e1ce09SLiu Bo static int tree_insert(struct rb_root_cached *root, struct extent_map *em)
94a52d9a80SChris Mason {
9507e1ce09SLiu 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);
10007e1ce09SLiu 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 
10607e1ce09SLiu Bo 		if (em->start < entry->start) {
107a52d9a80SChris Mason 			p = &(*p)->rb_left;
10807e1ce09SLiu Bo 		} else if (em->start >= extent_map_end(entry)) {
109a52d9a80SChris Mason 			p = &(*p)->rb_right;
11007e1ce09SLiu Bo 			leftmost = false;
11107e1ce09SLiu Bo 		} else {
11232193c14SFilipe David Borba Manana 			return -EEXIST;
113a52d9a80SChris Mason 		}
11407e1ce09SLiu 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);
13607e1ce09SLiu 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,
1456c05813eSFilipe Manana 				     struct rb_node **prev_or_next_ret)
146a52d9a80SChris Mason {
147a52d9a80SChris Mason 	struct rb_node *n = root->rb_node;
148a52d9a80SChris Mason 	struct rb_node *prev = NULL;
1495f56406aSChris Mason 	struct rb_node *orig_prev = NULL;
150d1310b2eSChris Mason 	struct extent_map *entry;
151d1310b2eSChris Mason 	struct extent_map *prev_entry = NULL;
152a52d9a80SChris Mason 
1536c05813eSFilipe Manana 	ASSERT(prev_or_next_ret);
15408f088ddSFilipe Manana 
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 	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 	}
1735f56406aSChris Mason 
1746c05813eSFilipe Manana 	/*
1756c05813eSFilipe Manana 	 * Previous extent map found, return as in this case the caller does not
1766c05813eSFilipe Manana 	 * care about the next one.
1776c05813eSFilipe Manana 	 */
1786c05813eSFilipe Manana 	if (prev) {
1796c05813eSFilipe Manana 		*prev_or_next_ret = prev;
1806c05813eSFilipe Manana 		return NULL;
1816c05813eSFilipe Manana 	}
1826c05813eSFilipe Manana 
1836c05813eSFilipe Manana 	prev = orig_prev;
184d1310b2eSChris Mason 	prev_entry = rb_entry(prev, struct extent_map, rb_node);
1855f56406aSChris Mason 	while (prev && offset < prev_entry->start) {
1865f56406aSChris Mason 		prev = rb_prev(prev);
187d1310b2eSChris Mason 		prev_entry = rb_entry(prev, struct extent_map, rb_node);
1885f56406aSChris Mason 	}
1896c05813eSFilipe Manana 	*prev_or_next_ret = prev;
19008f088ddSFilipe Manana 
191a52d9a80SChris Mason 	return NULL;
192a52d9a80SChris Mason }
193a52d9a80SChris Mason 
194d352ac68SChris Mason /* check to see if two extent_map structs are adjacent and safe to merge */
195d1310b2eSChris Mason static int mergable_maps(struct extent_map *prev, struct extent_map *next)
196a52d9a80SChris Mason {
1977f3c74fbSChris Mason 	if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
1987f3c74fbSChris Mason 		return 0;
1997f3c74fbSChris Mason 
200c8b97818SChris Mason 	/*
201c8b97818SChris Mason 	 * don't merge compressed extents, we need to know their
202c8b97818SChris Mason 	 * actual size
203c8b97818SChris Mason 	 */
204c8b97818SChris Mason 	if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
205c8b97818SChris Mason 		return 0;
206c8b97818SChris Mason 
207201a9038SJosef Bacik 	if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
208201a9038SJosef Bacik 	    test_bit(EXTENT_FLAG_LOGGING, &next->flags))
209201a9038SJosef Bacik 		return 0;
210201a9038SJosef Bacik 
21109a2a8f9SJosef Bacik 	/*
21209a2a8f9SJosef Bacik 	 * We don't want to merge stuff that hasn't been written to the log yet
21309a2a8f9SJosef Bacik 	 * since it may not reflect exactly what is on disk, and that would be
21409a2a8f9SJosef Bacik 	 * bad.
21509a2a8f9SJosef Bacik 	 */
21609a2a8f9SJosef Bacik 	if (!list_empty(&prev->list) || !list_empty(&next->list))
21709a2a8f9SJosef Bacik 		return 0;
21809a2a8f9SJosef Bacik 
219951e05a9SNikolay Borisov 	ASSERT(next->block_start != EXTENT_MAP_DELALLOC &&
220951e05a9SNikolay Borisov 	       prev->block_start != EXTENT_MAP_DELALLOC);
221951e05a9SNikolay Borisov 
222c3e14909SDavid Sterba 	if (prev->map_lookup || next->map_lookup)
223c3e14909SDavid Sterba 		ASSERT(test_bit(EXTENT_FLAG_FS_MAPPING, &prev->flags) &&
224c3e14909SDavid Sterba 		       test_bit(EXTENT_FLAG_FS_MAPPING, &next->flags));
225c3e14909SDavid Sterba 
226d1310b2eSChris Mason 	if (extent_map_end(prev) == next->start &&
227d1310b2eSChris Mason 	    prev->flags == next->flags &&
228c3e14909SDavid Sterba 	    prev->map_lookup == next->map_lookup &&
229d1310b2eSChris Mason 	    ((next->block_start == EXTENT_MAP_HOLE &&
230d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_HOLE) ||
231d1310b2eSChris Mason 	     (next->block_start == EXTENT_MAP_INLINE &&
232d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_INLINE) ||
233d1310b2eSChris Mason 	     (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
234d1310b2eSChris Mason 	      next->block_start == extent_map_block_end(prev)))) {
235d1310b2eSChris Mason 		return 1;
236d1310b2eSChris Mason 	}
237a52d9a80SChris Mason 	return 0;
238a52d9a80SChris Mason }
239a52d9a80SChris Mason 
2404d2c8f62SLi Zefan static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
241a1ed835eSChris Mason {
242a1ed835eSChris Mason 	struct extent_map *merge = NULL;
243a1ed835eSChris Mason 	struct rb_node *rb;
244a1ed835eSChris Mason 
245ac05ca91SFilipe Manana 	/*
246ac05ca91SFilipe Manana 	 * We can't modify an extent map that is in the tree and that is being
247ac05ca91SFilipe Manana 	 * used by another task, as it can cause that other task to see it in
248ac05ca91SFilipe Manana 	 * inconsistent state during the merging. We always have 1 reference for
249ac05ca91SFilipe Manana 	 * the tree and 1 for this task (which is unpinning the extent map or
250ac05ca91SFilipe Manana 	 * clearing the logging flag), so anything > 2 means it's being used by
251ac05ca91SFilipe Manana 	 * other tasks too.
252ac05ca91SFilipe Manana 	 */
253ac05ca91SFilipe Manana 	if (refcount_read(&em->refs) > 2)
254ac05ca91SFilipe Manana 		return;
255ac05ca91SFilipe Manana 
256a1ed835eSChris Mason 	if (em->start != 0) {
257a1ed835eSChris Mason 		rb = rb_prev(&em->rb_node);
258a1ed835eSChris Mason 		if (rb)
259a1ed835eSChris Mason 			merge = rb_entry(rb, struct extent_map, rb_node);
260a1ed835eSChris Mason 		if (rb && mergable_maps(merge, em)) {
261a1ed835eSChris Mason 			em->start = merge->start;
26270c8a91cSJosef Bacik 			em->orig_start = merge->orig_start;
263a1ed835eSChris Mason 			em->len += merge->len;
264a1ed835eSChris Mason 			em->block_len += merge->block_len;
265a1ed835eSChris Mason 			em->block_start = merge->block_start;
26670c8a91cSJosef Bacik 			em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
26770c8a91cSJosef Bacik 			em->mod_start = merge->mod_start;
26870c8a91cSJosef Bacik 			em->generation = max(em->generation, merge->generation);
269199257a7SQu Wenruo 			set_bit(EXTENT_FLAG_MERGED, &em->flags);
2705dc562c5SJosef Bacik 
27107e1ce09SLiu Bo 			rb_erase_cached(&merge->rb_node, &tree->map);
272cbc0e928SFilipe Manana 			RB_CLEAR_NODE(&merge->rb_node);
273a1ed835eSChris Mason 			free_extent_map(merge);
274a1ed835eSChris Mason 		}
275a1ed835eSChris Mason 	}
276a1ed835eSChris Mason 
277a1ed835eSChris Mason 	rb = rb_next(&em->rb_node);
278a1ed835eSChris Mason 	if (rb)
279a1ed835eSChris Mason 		merge = rb_entry(rb, struct extent_map, rb_node);
280a1ed835eSChris Mason 	if (rb && mergable_maps(em, merge)) {
281a1ed835eSChris Mason 		em->len += merge->len;
282d527afe1SFilipe David Borba Manana 		em->block_len += merge->block_len;
28307e1ce09SLiu Bo 		rb_erase_cached(&merge->rb_node, &tree->map);
284cbc0e928SFilipe Manana 		RB_CLEAR_NODE(&merge->rb_node);
28570c8a91cSJosef Bacik 		em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
28670c8a91cSJosef Bacik 		em->generation = max(em->generation, merge->generation);
287199257a7SQu Wenruo 		set_bit(EXTENT_FLAG_MERGED, &em->flags);
288a1ed835eSChris Mason 		free_extent_map(merge);
289a1ed835eSChris Mason 	}
2904d2c8f62SLi Zefan }
2914d2c8f62SLi Zefan 
2925dc562c5SJosef Bacik /**
29352b1de91SLiu Bo  * unpin_extent_cache - unpin an extent from the cache
2945dc562c5SJosef Bacik  * @tree:	tree to unpin the extent in
2955dc562c5SJosef Bacik  * @start:	logical offset in the file
2965dc562c5SJosef Bacik  * @len:	length of the extent
2975dc562c5SJosef Bacik  * @gen:	generation that this extent has been modified in
2985dc562c5SJosef Bacik  *
2995dc562c5SJosef Bacik  * Called after an extent has been written to disk properly.  Set the generation
3005dc562c5SJosef Bacik  * to the generation that actually added the file item to the inode so we know
3015dc562c5SJosef Bacik  * we need to sync this extent when we call fsync().
3025dc562c5SJosef Bacik  */
3035dc562c5SJosef Bacik int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
3045dc562c5SJosef Bacik 		       u64 gen)
3054d2c8f62SLi Zefan {
3064d2c8f62SLi Zefan 	int ret = 0;
3074d2c8f62SLi Zefan 	struct extent_map *em;
3084e2f84e6SLiu Bo 	bool prealloc = false;
3094d2c8f62SLi Zefan 
3104d2c8f62SLi Zefan 	write_lock(&tree->lock);
3114d2c8f62SLi Zefan 	em = lookup_extent_mapping(tree, start, len);
3124d2c8f62SLi Zefan 
3134d2c8f62SLi Zefan 	WARN_ON(!em || em->start != start);
3144d2c8f62SLi Zefan 
3154d2c8f62SLi Zefan 	if (!em)
3164d2c8f62SLi Zefan 		goto out;
3174d2c8f62SLi Zefan 
3185dc562c5SJosef Bacik 	em->generation = gen;
3194d2c8f62SLi Zefan 	clear_bit(EXTENT_FLAG_PINNED, &em->flags);
3204e2f84e6SLiu Bo 	em->mod_start = em->start;
3214e2f84e6SLiu Bo 	em->mod_len = em->len;
3224e2f84e6SLiu Bo 
323b11e234dSJosef Bacik 	if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
3244e2f84e6SLiu Bo 		prealloc = true;
325b11e234dSJosef Bacik 		clear_bit(EXTENT_FLAG_FILLING, &em->flags);
3264e2f84e6SLiu Bo 	}
3274d2c8f62SLi Zefan 
3284d2c8f62SLi Zefan 	try_merge_map(tree, em);
3294e2f84e6SLiu Bo 
3304e2f84e6SLiu Bo 	if (prealloc) {
3314e2f84e6SLiu Bo 		em->mod_start = em->start;
3324e2f84e6SLiu Bo 		em->mod_len = em->len;
3334e2f84e6SLiu Bo 	}
3344e2f84e6SLiu Bo 
335a1ed835eSChris Mason 	free_extent_map(em);
336a1ed835eSChris Mason out:
337a1ed835eSChris Mason 	write_unlock(&tree->lock);
338a1ed835eSChris Mason 	return ret;
339a1ed835eSChris Mason 
340a1ed835eSChris Mason }
341a1ed835eSChris Mason 
342201a9038SJosef Bacik void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
343201a9038SJosef Bacik {
34474333c7dSFilipe Manana 	lockdep_assert_held_write(&tree->lock);
34574333c7dSFilipe Manana 
346201a9038SJosef Bacik 	clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
347cbc0e928SFilipe Manana 	if (extent_map_in_tree(em))
348201a9038SJosef Bacik 		try_merge_map(tree, em);
349201a9038SJosef Bacik }
350201a9038SJosef Bacik 
351176840b3SFilipe Manana static inline void setup_extent_mapping(struct extent_map_tree *tree,
352176840b3SFilipe Manana 					struct extent_map *em,
353176840b3SFilipe Manana 					int modified)
354176840b3SFilipe Manana {
355490b54d6SElena Reshetova 	refcount_inc(&em->refs);
356176840b3SFilipe Manana 	em->mod_start = em->start;
357176840b3SFilipe Manana 	em->mod_len = em->len;
358176840b3SFilipe Manana 
359176840b3SFilipe Manana 	if (modified)
360176840b3SFilipe Manana 		list_move(&em->list, &tree->modified_extents);
361176840b3SFilipe Manana 	else
362176840b3SFilipe Manana 		try_merge_map(tree, em);
363176840b3SFilipe Manana }
364176840b3SFilipe Manana 
3651c11b63eSJeff Mahoney static void extent_map_device_set_bits(struct extent_map *em, unsigned bits)
3661c11b63eSJeff Mahoney {
3671c11b63eSJeff Mahoney 	struct map_lookup *map = em->map_lookup;
3681c11b63eSJeff Mahoney 	u64 stripe_size = em->orig_block_len;
3691c11b63eSJeff Mahoney 	int i;
3701c11b63eSJeff Mahoney 
3711c11b63eSJeff Mahoney 	for (i = 0; i < map->num_stripes; i++) {
3724c664611SQu Wenruo 		struct btrfs_io_stripe *stripe = &map->stripes[i];
3731c11b63eSJeff Mahoney 		struct btrfs_device *device = stripe->dev;
3741c11b63eSJeff Mahoney 
3751c11b63eSJeff Mahoney 		set_extent_bits_nowait(&device->alloc_state, stripe->physical,
3761c11b63eSJeff Mahoney 				 stripe->physical + stripe_size - 1, bits);
3771c11b63eSJeff Mahoney 	}
3781c11b63eSJeff Mahoney }
3791c11b63eSJeff Mahoney 
3801c11b63eSJeff Mahoney static void extent_map_device_clear_bits(struct extent_map *em, unsigned bits)
3811c11b63eSJeff Mahoney {
3821c11b63eSJeff Mahoney 	struct map_lookup *map = em->map_lookup;
3831c11b63eSJeff Mahoney 	u64 stripe_size = em->orig_block_len;
3841c11b63eSJeff Mahoney 	int i;
3851c11b63eSJeff Mahoney 
3861c11b63eSJeff Mahoney 	for (i = 0; i < map->num_stripes; i++) {
3874c664611SQu Wenruo 		struct btrfs_io_stripe *stripe = &map->stripes[i];
3881c11b63eSJeff Mahoney 		struct btrfs_device *device = stripe->dev;
3891c11b63eSJeff Mahoney 
3901c11b63eSJeff Mahoney 		__clear_extent_bit(&device->alloc_state, stripe->physical,
3911c11b63eSJeff Mahoney 				   stripe->physical + stripe_size - 1, bits,
392bd015294SJosef Bacik 				   NULL, GFP_NOWAIT, NULL);
3931c11b63eSJeff Mahoney 	}
3941c11b63eSJeff Mahoney }
3951c11b63eSJeff Mahoney 
3969d2423c5SChristoph Hellwig /**
397401bd2ddSNikolay Borisov  * Add new extent map to the extent tree
398401bd2ddSNikolay Borisov  *
3999d2423c5SChristoph Hellwig  * @tree:	tree to insert new map in
4009d2423c5SChristoph Hellwig  * @em:		map to insert
401401bd2ddSNikolay Borisov  * @modified:	indicate whether the given @em should be added to the
402401bd2ddSNikolay Borisov  *	        modified list, which indicates the extent needs to be logged
4039d2423c5SChristoph Hellwig  *
4049d2423c5SChristoph Hellwig  * Insert @em into @tree or perform a simple forward/backward merge with
4059d2423c5SChristoph Hellwig  * existing mappings.  The extent_map struct passed in will be inserted
4069d2423c5SChristoph Hellwig  * into the tree directly, with an additional reference taken, or a
40725985edcSLucas De Marchi  * reference dropped if the merge attempt was successful.
408a52d9a80SChris Mason  */
409a52d9a80SChris Mason int add_extent_mapping(struct extent_map_tree *tree,
41009a2a8f9SJosef Bacik 		       struct extent_map *em, int modified)
411a52d9a80SChris Mason {
412a52d9a80SChris Mason 	int ret = 0;
413a52d9a80SChris Mason 
414d23ea3faSDavid Sterba 	lockdep_assert_held_write(&tree->lock);
415d23ea3faSDavid Sterba 
41632193c14SFilipe David Borba Manana 	ret = tree_insert(&tree->map, em);
41732193c14SFilipe David Borba Manana 	if (ret)
4187c2fe32aSChris Mason 		goto out;
41932193c14SFilipe David Borba Manana 
420176840b3SFilipe Manana 	setup_extent_mapping(tree, em, modified);
4218811133dSNikolay Borisov 	if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags)) {
4221c11b63eSJeff Mahoney 		extent_map_device_set_bits(em, CHUNK_ALLOCATED);
4238811133dSNikolay Borisov 		extent_map_device_clear_bits(em, CHUNK_TRIMMED);
4248811133dSNikolay Borisov 	}
425a52d9a80SChris Mason out:
426a52d9a80SChris Mason 	return ret;
427a52d9a80SChris Mason }
428a52d9a80SChris Mason 
42948a3b636SEric Sandeen static struct extent_map *
43048a3b636SEric Sandeen __lookup_extent_mapping(struct extent_map_tree *tree,
431ed64f066SLi Zefan 			u64 start, u64 len, int strict)
432ed64f066SLi Zefan {
433ed64f066SLi Zefan 	struct extent_map *em;
434ed64f066SLi Zefan 	struct rb_node *rb_node;
4356c05813eSFilipe Manana 	struct rb_node *prev_or_next = NULL;
436ed64f066SLi Zefan 	u64 end = range_end(start, len);
437ed64f066SLi Zefan 
4386c05813eSFilipe Manana 	rb_node = __tree_search(&tree->map.rb_root, start, &prev_or_next);
439ed64f066SLi Zefan 	if (!rb_node) {
4406c05813eSFilipe Manana 		if (prev_or_next)
4416c05813eSFilipe Manana 			rb_node = prev_or_next;
442ed64f066SLi Zefan 		else
443ed64f066SLi Zefan 			return NULL;
444ed64f066SLi Zefan 	}
445ed64f066SLi Zefan 
446ed64f066SLi Zefan 	em = rb_entry(rb_node, struct extent_map, rb_node);
447ed64f066SLi Zefan 
448ed64f066SLi Zefan 	if (strict && !(end > em->start && start < extent_map_end(em)))
449ed64f066SLi Zefan 		return NULL;
450ed64f066SLi Zefan 
451490b54d6SElena Reshetova 	refcount_inc(&em->refs);
452ed64f066SLi Zefan 	return em;
453ed64f066SLi Zefan }
454ed64f066SLi Zefan 
4559d2423c5SChristoph Hellwig /**
4569d2423c5SChristoph Hellwig  * lookup_extent_mapping - lookup extent_map
4579d2423c5SChristoph Hellwig  * @tree:	tree to lookup in
4589d2423c5SChristoph Hellwig  * @start:	byte offset to start the search
4599d2423c5SChristoph Hellwig  * @len:	length of the lookup range
4609d2423c5SChristoph Hellwig  *
4619d2423c5SChristoph Hellwig  * Find and return the first extent_map struct in @tree that intersects the
4629d2423c5SChristoph Hellwig  * [start, len] range.  There may be additional objects in the tree that
4639d2423c5SChristoph Hellwig  * intersect, so check the object returned carefully to make sure that no
4649d2423c5SChristoph Hellwig  * additional lookups are needed.
465a52d9a80SChris Mason  */
466a52d9a80SChris Mason struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
467d1310b2eSChris Mason 					 u64 start, u64 len)
468a52d9a80SChris Mason {
469ed64f066SLi Zefan 	return __lookup_extent_mapping(tree, start, len, 1);
470a52d9a80SChris Mason }
471a52d9a80SChris Mason 
4729d2423c5SChristoph Hellwig /**
473b917b7c3SChris Mason  * search_extent_mapping - find a nearby extent map
474b917b7c3SChris Mason  * @tree:	tree to lookup in
475b917b7c3SChris Mason  * @start:	byte offset to start the search
476b917b7c3SChris Mason  * @len:	length of the lookup range
477b917b7c3SChris Mason  *
478b917b7c3SChris Mason  * Find and return the first extent_map struct in @tree that intersects the
479b917b7c3SChris Mason  * [start, len] range.
480b917b7c3SChris Mason  *
481b917b7c3SChris Mason  * If one can't be found, any nearby extent may be returned
482b917b7c3SChris Mason  */
483b917b7c3SChris Mason struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
484b917b7c3SChris Mason 					 u64 start, u64 len)
485b917b7c3SChris Mason {
486ed64f066SLi Zefan 	return __lookup_extent_mapping(tree, start, len, 0);
487b917b7c3SChris Mason }
488b917b7c3SChris Mason 
489b917b7c3SChris Mason /**
4909d2423c5SChristoph Hellwig  * remove_extent_mapping - removes an extent_map from the extent tree
4919d2423c5SChristoph Hellwig  * @tree:	extent tree to remove from
492bb7ab3b9SAdam Buchbinder  * @em:		extent map being removed
4939d2423c5SChristoph Hellwig  *
4949d2423c5SChristoph Hellwig  * Removes @em from @tree.  No reference counts are dropped, and no checks
4959d2423c5SChristoph Hellwig  * are done to see if the range is in use
496a52d9a80SChris Mason  */
497c1766dd7Szhong jiang void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
498a52d9a80SChris Mason {
4996d3b050eSFilipe Manana 	lockdep_assert_held_write(&tree->lock);
5006d3b050eSFilipe Manana 
5017f3c74fbSChris Mason 	WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
50207e1ce09SLiu Bo 	rb_erase_cached(&em->rb_node, &tree->map);
503ff44c6e3SJosef Bacik 	if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
5045dc562c5SJosef Bacik 		list_del_init(&em->list);
5051c11b63eSJeff Mahoney 	if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
5061c11b63eSJeff Mahoney 		extent_map_device_clear_bits(em, CHUNK_ALLOCATED);
507cbc0e928SFilipe Manana 	RB_CLEAR_NODE(&em->rb_node);
508a52d9a80SChris Mason }
509176840b3SFilipe Manana 
510176840b3SFilipe Manana void replace_extent_mapping(struct extent_map_tree *tree,
511176840b3SFilipe Manana 			    struct extent_map *cur,
512176840b3SFilipe Manana 			    struct extent_map *new,
513176840b3SFilipe Manana 			    int modified)
514176840b3SFilipe Manana {
5156d3b050eSFilipe Manana 	lockdep_assert_held_write(&tree->lock);
5166d3b050eSFilipe Manana 
517176840b3SFilipe Manana 	WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));
518176840b3SFilipe Manana 	ASSERT(extent_map_in_tree(cur));
519176840b3SFilipe Manana 	if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))
520176840b3SFilipe Manana 		list_del_init(&cur->list);
52107e1ce09SLiu Bo 	rb_replace_node_cached(&cur->rb_node, &new->rb_node, &tree->map);
522176840b3SFilipe Manana 	RB_CLEAR_NODE(&cur->rb_node);
523176840b3SFilipe Manana 
524176840b3SFilipe Manana 	setup_extent_mapping(tree, new, modified);
525176840b3SFilipe Manana }
526c04e61b5SLiu Bo 
527d47704bdSFilipe Manana static struct extent_map *next_extent_map(const struct extent_map *em)
528c04e61b5SLiu Bo {
529c04e61b5SLiu Bo 	struct rb_node *next;
530c04e61b5SLiu Bo 
531c04e61b5SLiu Bo 	next = rb_next(&em->rb_node);
532c04e61b5SLiu Bo 	if (!next)
533c04e61b5SLiu Bo 		return NULL;
534c04e61b5SLiu Bo 	return container_of(next, struct extent_map, rb_node);
535c04e61b5SLiu Bo }
536c04e61b5SLiu Bo 
537d47704bdSFilipe Manana /*
538d47704bdSFilipe Manana  * Get the extent map that immediately follows another one.
539d47704bdSFilipe Manana  *
540d47704bdSFilipe Manana  * @tree:       The extent map tree that the extent map belong to.
541d47704bdSFilipe Manana  *              Holding read or write access on the tree's lock is required.
542d47704bdSFilipe Manana  * @em:         An extent map from the given tree. The caller must ensure that
543d47704bdSFilipe Manana  *              between getting @em and between calling this function, the
544d47704bdSFilipe Manana  *              extent map @em is not removed from the tree - for example, by
545d47704bdSFilipe Manana  *              holding the tree's lock for the duration of those 2 operations.
546d47704bdSFilipe Manana  *
547d47704bdSFilipe Manana  * Returns the extent map that immediately follows @em, or NULL if @em is the
548d47704bdSFilipe Manana  * last extent map in the tree.
549d47704bdSFilipe Manana  */
550d47704bdSFilipe Manana struct extent_map *btrfs_next_extent_map(const struct extent_map_tree *tree,
551d47704bdSFilipe Manana 					 const struct extent_map *em)
552d47704bdSFilipe Manana {
553d47704bdSFilipe Manana 	struct extent_map *next;
554d47704bdSFilipe Manana 
555d47704bdSFilipe Manana 	/* The lock must be acquired either in read mode or write mode. */
556d47704bdSFilipe Manana 	lockdep_assert_held(&tree->lock);
557d47704bdSFilipe Manana 	ASSERT(extent_map_in_tree(em));
558d47704bdSFilipe Manana 
559d47704bdSFilipe Manana 	next = next_extent_map(em);
560d47704bdSFilipe Manana 	if (next)
561d47704bdSFilipe Manana 		refcount_inc(&next->refs);
562d47704bdSFilipe Manana 
563d47704bdSFilipe Manana 	return next;
564d47704bdSFilipe Manana }
565d47704bdSFilipe Manana 
566c04e61b5SLiu Bo static struct extent_map *prev_extent_map(struct extent_map *em)
567c04e61b5SLiu Bo {
568c04e61b5SLiu Bo 	struct rb_node *prev;
569c04e61b5SLiu Bo 
570c04e61b5SLiu Bo 	prev = rb_prev(&em->rb_node);
571c04e61b5SLiu Bo 	if (!prev)
572c04e61b5SLiu Bo 		return NULL;
573c04e61b5SLiu Bo 	return container_of(prev, struct extent_map, rb_node);
574c04e61b5SLiu Bo }
575c04e61b5SLiu Bo 
57652042d8eSAndrea Gelmini /*
57752042d8eSAndrea Gelmini  * Helper for btrfs_get_extent.  Given an existing extent in the tree,
578c04e61b5SLiu Bo  * the existing extent is the nearest extent to map_start,
579c04e61b5SLiu Bo  * and an extent that you want to insert, deal with overlap and insert
580c04e61b5SLiu Bo  * the best fitted new extent into the tree.
581c04e61b5SLiu Bo  */
5825f4791f4SLiu Bo static noinline int merge_extent_mapping(struct extent_map_tree *em_tree,
583c04e61b5SLiu Bo 					 struct extent_map *existing,
584c04e61b5SLiu Bo 					 struct extent_map *em,
585c04e61b5SLiu Bo 					 u64 map_start)
586c04e61b5SLiu Bo {
587c04e61b5SLiu Bo 	struct extent_map *prev;
588c04e61b5SLiu Bo 	struct extent_map *next;
589c04e61b5SLiu Bo 	u64 start;
590c04e61b5SLiu Bo 	u64 end;
591c04e61b5SLiu Bo 	u64 start_diff;
592c04e61b5SLiu Bo 
593c04e61b5SLiu Bo 	BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
594c04e61b5SLiu Bo 
595c04e61b5SLiu Bo 	if (existing->start > map_start) {
596c04e61b5SLiu Bo 		next = existing;
597c04e61b5SLiu Bo 		prev = prev_extent_map(next);
598c04e61b5SLiu Bo 	} else {
599c04e61b5SLiu Bo 		prev = existing;
600c04e61b5SLiu Bo 		next = next_extent_map(prev);
601c04e61b5SLiu Bo 	}
602c04e61b5SLiu Bo 
603c04e61b5SLiu Bo 	start = prev ? extent_map_end(prev) : em->start;
604c04e61b5SLiu Bo 	start = max_t(u64, start, em->start);
605c04e61b5SLiu Bo 	end = next ? next->start : extent_map_end(em);
606c04e61b5SLiu Bo 	end = min_t(u64, end, extent_map_end(em));
607c04e61b5SLiu Bo 	start_diff = start - em->start;
608c04e61b5SLiu Bo 	em->start = start;
609c04e61b5SLiu Bo 	em->len = end - start;
610c04e61b5SLiu Bo 	if (em->block_start < EXTENT_MAP_LAST_BYTE &&
611c04e61b5SLiu Bo 	    !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
612c04e61b5SLiu Bo 		em->block_start += start_diff;
613c04e61b5SLiu Bo 		em->block_len = em->len;
614c04e61b5SLiu Bo 	}
615c04e61b5SLiu Bo 	return add_extent_mapping(em_tree, em, 0);
616c04e61b5SLiu Bo }
617c04e61b5SLiu Bo 
618c04e61b5SLiu Bo /**
6199ad37bb3SNikolay Borisov  * Add extent mapping into em_tree
6209ad37bb3SNikolay Borisov  *
6219ad37bb3SNikolay Borisov  * @fs_info:  the filesystem
6229ad37bb3SNikolay Borisov  * @em_tree:  extent tree into which we want to insert the extent mapping
6239ad37bb3SNikolay Borisov  * @em_in:    extent we are inserting
6249ad37bb3SNikolay Borisov  * @start:    start of the logical range btrfs_get_extent() is requesting
6259ad37bb3SNikolay Borisov  * @len:      length of the logical range btrfs_get_extent() is requesting
626c04e61b5SLiu Bo  *
627c04e61b5SLiu Bo  * Note that @em_in's range may be different from [start, start+len),
628c04e61b5SLiu Bo  * but they must be overlapped.
629c04e61b5SLiu Bo  *
630c04e61b5SLiu Bo  * Insert @em_in into @em_tree. In case there is an overlapping range, handle
631c04e61b5SLiu Bo  * the -EEXIST by either:
632c04e61b5SLiu Bo  * a) Returning the existing extent in @em_in if @start is within the
633c04e61b5SLiu Bo  *    existing em.
634c04e61b5SLiu Bo  * b) Merge the existing extent with @em_in passed in.
635c04e61b5SLiu Bo  *
636c04e61b5SLiu Bo  * Return 0 on success, otherwise -EEXIST.
637c04e61b5SLiu Bo  *
638c04e61b5SLiu Bo  */
639f46b24c9SDavid Sterba int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info,
640f46b24c9SDavid Sterba 			     struct extent_map_tree *em_tree,
641c04e61b5SLiu Bo 			     struct extent_map **em_in, u64 start, u64 len)
642c04e61b5SLiu Bo {
643c04e61b5SLiu Bo 	int ret;
644c04e61b5SLiu Bo 	struct extent_map *em = *em_in;
645c04e61b5SLiu Bo 
646c04e61b5SLiu Bo 	ret = add_extent_mapping(em_tree, em, 0);
647c04e61b5SLiu Bo 	/* it is possible that someone inserted the extent into the tree
648c04e61b5SLiu Bo 	 * while we had the lock dropped.  It is also possible that
649c04e61b5SLiu Bo 	 * an overlapping map exists in the tree
650c04e61b5SLiu Bo 	 */
651c04e61b5SLiu Bo 	if (ret == -EEXIST) {
652c04e61b5SLiu Bo 		struct extent_map *existing;
653c04e61b5SLiu Bo 
654c04e61b5SLiu Bo 		ret = 0;
655c04e61b5SLiu Bo 
656c04e61b5SLiu Bo 		existing = search_extent_mapping(em_tree, start, len);
657393da918SLiu Bo 
658f46b24c9SDavid Sterba 		trace_btrfs_handle_em_exist(fs_info, existing, em, start, len);
659393da918SLiu Bo 
660c04e61b5SLiu Bo 		/*
661c04e61b5SLiu Bo 		 * existing will always be non-NULL, since there must be
662c04e61b5SLiu Bo 		 * extent causing the -EEXIST.
663c04e61b5SLiu Bo 		 */
664c04e61b5SLiu Bo 		if (start >= existing->start &&
665c04e61b5SLiu Bo 		    start < extent_map_end(existing)) {
666c04e61b5SLiu Bo 			free_extent_map(em);
667c04e61b5SLiu Bo 			*em_in = existing;
668c04e61b5SLiu Bo 			ret = 0;
669c04e61b5SLiu Bo 		} else {
6709a7e10e7SLiu Bo 			u64 orig_start = em->start;
6719a7e10e7SLiu Bo 			u64 orig_len = em->len;
6729a7e10e7SLiu Bo 
673c04e61b5SLiu Bo 			/*
674c04e61b5SLiu Bo 			 * The existing extent map is the one nearest to
675c04e61b5SLiu Bo 			 * the [start, start + len) range which overlaps
676c04e61b5SLiu Bo 			 */
677c04e61b5SLiu Bo 			ret = merge_extent_mapping(em_tree, existing,
678c04e61b5SLiu Bo 						   em, start);
679c04e61b5SLiu Bo 			if (ret) {
680c04e61b5SLiu Bo 				free_extent_map(em);
681c04e61b5SLiu Bo 				*em_in = NULL;
6829a7e10e7SLiu Bo 				WARN_ONCE(ret,
6839a7e10e7SLiu Bo "unexpected error %d: merge existing(start %llu len %llu) with em(start %llu len %llu)\n",
6849a7e10e7SLiu Bo 					  ret, existing->start, existing->len,
6859a7e10e7SLiu Bo 					  orig_start, orig_len);
686c04e61b5SLiu Bo 			}
6879a7e10e7SLiu Bo 			free_extent_map(existing);
688c04e61b5SLiu Bo 		}
689c04e61b5SLiu Bo 	}
690c04e61b5SLiu Bo 
691c04e61b5SLiu Bo 	ASSERT(ret == 0 || ret == -EEXIST);
692c04e61b5SLiu Bo 	return ret;
693c04e61b5SLiu Bo }
6944c0c8cfcSFilipe Manana 
6954c0c8cfcSFilipe Manana /*
6969c9d1b4fSFilipe Manana  * Drop all extent maps from a tree in the fastest possible way, rescheduling
6979c9d1b4fSFilipe Manana  * if needed. This avoids searching the tree, from the root down to the first
6989c9d1b4fSFilipe Manana  * extent map, before each deletion.
6999c9d1b4fSFilipe Manana  */
7009c9d1b4fSFilipe Manana static void drop_all_extent_maps_fast(struct extent_map_tree *tree)
7019c9d1b4fSFilipe Manana {
7029c9d1b4fSFilipe Manana 	write_lock(&tree->lock);
7039c9d1b4fSFilipe Manana 	while (!RB_EMPTY_ROOT(&tree->map.rb_root)) {
7049c9d1b4fSFilipe Manana 		struct extent_map *em;
7059c9d1b4fSFilipe Manana 		struct rb_node *node;
7069c9d1b4fSFilipe Manana 
7079c9d1b4fSFilipe Manana 		node = rb_first_cached(&tree->map);
7089c9d1b4fSFilipe Manana 		em = rb_entry(node, struct extent_map, rb_node);
7099c9d1b4fSFilipe Manana 		clear_bit(EXTENT_FLAG_PINNED, &em->flags);
7109c9d1b4fSFilipe Manana 		clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
7119c9d1b4fSFilipe Manana 		remove_extent_mapping(tree, em);
7129c9d1b4fSFilipe Manana 		free_extent_map(em);
7139c9d1b4fSFilipe Manana 		cond_resched_rwlock_write(&tree->lock);
7149c9d1b4fSFilipe Manana 	}
7159c9d1b4fSFilipe Manana 	write_unlock(&tree->lock);
7169c9d1b4fSFilipe Manana }
7179c9d1b4fSFilipe Manana 
7189c9d1b4fSFilipe Manana /*
7194c0c8cfcSFilipe Manana  * Drop all extent maps in a given range.
7204c0c8cfcSFilipe Manana  *
7214c0c8cfcSFilipe Manana  * @inode:       The target inode.
7224c0c8cfcSFilipe Manana  * @start:       Start offset of the range.
7234c0c8cfcSFilipe Manana  * @end:         End offset of the range (inclusive value).
7244c0c8cfcSFilipe Manana  * @skip_pinned: Indicate if pinned extent maps should be ignored or not.
7254c0c8cfcSFilipe Manana  *
7264c0c8cfcSFilipe Manana  * This drops all the extent maps that intersect the given range [@start, @end].
7274c0c8cfcSFilipe Manana  * Extent maps that partially overlap the range and extend behind or beyond it,
7284c0c8cfcSFilipe Manana  * are split.
7294c0c8cfcSFilipe Manana  * The caller should have locked an appropriate file range in the inode's io
7304c0c8cfcSFilipe Manana  * tree before calling this function.
7314c0c8cfcSFilipe Manana  */
7324c0c8cfcSFilipe Manana void btrfs_drop_extent_map_range(struct btrfs_inode *inode, u64 start, u64 end,
7334c0c8cfcSFilipe Manana 				 bool skip_pinned)
7344c0c8cfcSFilipe Manana {
735db21370bSFilipe Manana 	struct extent_map *split;
736db21370bSFilipe Manana 	struct extent_map *split2;
737db21370bSFilipe Manana 	struct extent_map *em;
7384c0c8cfcSFilipe Manana 	struct extent_map_tree *em_tree = &inode->extent_tree;
7394c0c8cfcSFilipe Manana 	u64 len = end - start + 1;
7404c0c8cfcSFilipe Manana 
7414c0c8cfcSFilipe Manana 	WARN_ON(end < start);
7424c0c8cfcSFilipe Manana 	if (end == (u64)-1) {
7439c9d1b4fSFilipe Manana 		if (start == 0 && !skip_pinned) {
7449c9d1b4fSFilipe Manana 			drop_all_extent_maps_fast(em_tree);
7459c9d1b4fSFilipe Manana 			return;
7469c9d1b4fSFilipe Manana 		}
7474c0c8cfcSFilipe Manana 		len = (u64)-1;
748db21370bSFilipe Manana 	} else {
749db21370bSFilipe Manana 		/* Make end offset exclusive for use in the loop below. */
750db21370bSFilipe Manana 		end++;
7514c0c8cfcSFilipe Manana 	}
7524c0c8cfcSFilipe Manana 
753db21370bSFilipe Manana 	/*
754db21370bSFilipe Manana 	 * It's ok if we fail to allocate the extent maps, see the comment near
755db21370bSFilipe Manana 	 * the bottom of the loop below. We only need two spare extent maps in
756db21370bSFilipe Manana 	 * the worst case, where the first extent map that intersects our range
757db21370bSFilipe Manana 	 * starts before the range and the last extent map that intersects our
758db21370bSFilipe Manana 	 * range ends after our range (and they might be the same extent map),
759db21370bSFilipe Manana 	 * because we need to split those two extent maps at the boundaries.
760db21370bSFilipe Manana 	 */
7614c0c8cfcSFilipe Manana 	split = alloc_extent_map();
7624c0c8cfcSFilipe Manana 	split2 = alloc_extent_map();
7634c0c8cfcSFilipe Manana 
7644c0c8cfcSFilipe Manana 	write_lock(&em_tree->lock);
7654c0c8cfcSFilipe Manana 	em = lookup_extent_mapping(em_tree, start, len);
766db21370bSFilipe Manana 
767db21370bSFilipe Manana 	while (em) {
768db21370bSFilipe Manana 		/* extent_map_end() returns exclusive value (last byte + 1). */
769db21370bSFilipe Manana 		const u64 em_end = extent_map_end(em);
770db21370bSFilipe Manana 		struct extent_map *next_em = NULL;
771db21370bSFilipe Manana 		u64 gen;
772db21370bSFilipe Manana 		unsigned long flags;
773db21370bSFilipe Manana 		bool modified;
774db21370bSFilipe Manana 		bool compressed;
775db21370bSFilipe Manana 
776db21370bSFilipe Manana 		if (em_end < end) {
777db21370bSFilipe Manana 			next_em = next_extent_map(em);
778db21370bSFilipe Manana 			if (next_em) {
779db21370bSFilipe Manana 				if (next_em->start < end)
780db21370bSFilipe Manana 					refcount_inc(&next_em->refs);
781db21370bSFilipe Manana 				else
782db21370bSFilipe Manana 					next_em = NULL;
7834c0c8cfcSFilipe Manana 			}
784db21370bSFilipe Manana 		}
785db21370bSFilipe Manana 
7864c0c8cfcSFilipe Manana 		if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
787f3109e33SFilipe Manana 			start = em_end;
788db21370bSFilipe Manana 			if (end != (u64)-1)
789f3109e33SFilipe Manana 				len = start + len - em_end;
790db21370bSFilipe Manana 			goto next;
7914c0c8cfcSFilipe Manana 		}
792db21370bSFilipe Manana 
7934c0c8cfcSFilipe Manana 		clear_bit(EXTENT_FLAG_PINNED, &em->flags);
7944c0c8cfcSFilipe Manana 		clear_bit(EXTENT_FLAG_LOGGING, &flags);
7954c0c8cfcSFilipe Manana 		modified = !list_empty(&em->list);
796db21370bSFilipe Manana 
797db21370bSFilipe Manana 		/*
798db21370bSFilipe Manana 		 * The extent map does not cross our target range, so no need to
799db21370bSFilipe Manana 		 * split it, we can remove it directly.
800db21370bSFilipe Manana 		 */
801db21370bSFilipe Manana 		if (em->start >= start && em_end <= end)
802db21370bSFilipe Manana 			goto remove_em;
803db21370bSFilipe Manana 
804db21370bSFilipe Manana 		flags = em->flags;
805db21370bSFilipe Manana 		gen = em->generation;
806db21370bSFilipe Manana 		compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
8074c0c8cfcSFilipe Manana 
8084c0c8cfcSFilipe Manana 		if (em->start < start) {
809db21370bSFilipe Manana 			if (!split) {
810db21370bSFilipe Manana 				split = split2;
811db21370bSFilipe Manana 				split2 = NULL;
812db21370bSFilipe Manana 				if (!split)
813db21370bSFilipe Manana 					goto remove_em;
814db21370bSFilipe Manana 			}
8154c0c8cfcSFilipe Manana 			split->start = em->start;
8164c0c8cfcSFilipe Manana 			split->len = start - em->start;
8174c0c8cfcSFilipe Manana 
8184c0c8cfcSFilipe Manana 			if (em->block_start < EXTENT_MAP_LAST_BYTE) {
8194c0c8cfcSFilipe Manana 				split->orig_start = em->orig_start;
8204c0c8cfcSFilipe Manana 				split->block_start = em->block_start;
8214c0c8cfcSFilipe Manana 
8224c0c8cfcSFilipe Manana 				if (compressed)
8234c0c8cfcSFilipe Manana 					split->block_len = em->block_len;
8244c0c8cfcSFilipe Manana 				else
8254c0c8cfcSFilipe Manana 					split->block_len = split->len;
8264c0c8cfcSFilipe Manana 				split->orig_block_len = max(split->block_len,
8274c0c8cfcSFilipe Manana 						em->orig_block_len);
8284c0c8cfcSFilipe Manana 				split->ram_bytes = em->ram_bytes;
8294c0c8cfcSFilipe Manana 			} else {
8304c0c8cfcSFilipe Manana 				split->orig_start = split->start;
8314c0c8cfcSFilipe Manana 				split->block_len = 0;
8324c0c8cfcSFilipe Manana 				split->block_start = em->block_start;
8334c0c8cfcSFilipe Manana 				split->orig_block_len = 0;
8344c0c8cfcSFilipe Manana 				split->ram_bytes = split->len;
8354c0c8cfcSFilipe Manana 			}
8364c0c8cfcSFilipe Manana 
8374c0c8cfcSFilipe Manana 			split->generation = gen;
8384c0c8cfcSFilipe Manana 			split->flags = flags;
8394c0c8cfcSFilipe Manana 			split->compress_type = em->compress_type;
8404c0c8cfcSFilipe Manana 			replace_extent_mapping(em_tree, em, split, modified);
8414c0c8cfcSFilipe Manana 			free_extent_map(split);
8424c0c8cfcSFilipe Manana 			split = split2;
8434c0c8cfcSFilipe Manana 			split2 = NULL;
8444c0c8cfcSFilipe Manana 		}
845db21370bSFilipe Manana 		if (em_end > end) {
846db21370bSFilipe Manana 			if (!split) {
847db21370bSFilipe Manana 				split = split2;
848db21370bSFilipe Manana 				split2 = NULL;
849db21370bSFilipe Manana 				if (!split)
850db21370bSFilipe Manana 					goto remove_em;
851db21370bSFilipe Manana 			}
8524c0c8cfcSFilipe Manana 			split->start = start + len;
853f3109e33SFilipe Manana 			split->len = em_end - (start + len);
8544c0c8cfcSFilipe Manana 			split->block_start = em->block_start;
8554c0c8cfcSFilipe Manana 			split->flags = flags;
8564c0c8cfcSFilipe Manana 			split->compress_type = em->compress_type;
8574c0c8cfcSFilipe Manana 			split->generation = gen;
8584c0c8cfcSFilipe Manana 
8594c0c8cfcSFilipe Manana 			if (em->block_start < EXTENT_MAP_LAST_BYTE) {
8604c0c8cfcSFilipe Manana 				split->orig_block_len = max(em->block_len,
8614c0c8cfcSFilipe Manana 						    em->orig_block_len);
8624c0c8cfcSFilipe Manana 
8634c0c8cfcSFilipe Manana 				split->ram_bytes = em->ram_bytes;
8644c0c8cfcSFilipe Manana 				if (compressed) {
8654c0c8cfcSFilipe Manana 					split->block_len = em->block_len;
8664c0c8cfcSFilipe Manana 					split->orig_start = em->orig_start;
8674c0c8cfcSFilipe Manana 				} else {
8684c0c8cfcSFilipe Manana 					const u64 diff = start + len - em->start;
8694c0c8cfcSFilipe Manana 
8704c0c8cfcSFilipe Manana 					split->block_len = split->len;
8714c0c8cfcSFilipe Manana 					split->block_start += diff;
8724c0c8cfcSFilipe Manana 					split->orig_start = em->orig_start;
8734c0c8cfcSFilipe Manana 				}
8744c0c8cfcSFilipe Manana 			} else {
8754c0c8cfcSFilipe Manana 				split->ram_bytes = split->len;
8764c0c8cfcSFilipe Manana 				split->orig_start = split->start;
8774c0c8cfcSFilipe Manana 				split->block_len = 0;
8784c0c8cfcSFilipe Manana 				split->orig_block_len = 0;
8794c0c8cfcSFilipe Manana 			}
8804c0c8cfcSFilipe Manana 
8814c0c8cfcSFilipe Manana 			if (extent_map_in_tree(em)) {
8824c0c8cfcSFilipe Manana 				replace_extent_mapping(em_tree, em, split,
8834c0c8cfcSFilipe Manana 						       modified);
8844c0c8cfcSFilipe Manana 			} else {
8854c0c8cfcSFilipe Manana 				int ret;
8864c0c8cfcSFilipe Manana 
8874c0c8cfcSFilipe Manana 				ret = add_extent_mapping(em_tree, split,
8884c0c8cfcSFilipe Manana 							 modified);
8894c0c8cfcSFilipe Manana 				/* Logic error, shouldn't happen. */
8904c0c8cfcSFilipe Manana 				ASSERT(ret == 0);
8914c0c8cfcSFilipe Manana 				if (WARN_ON(ret != 0) && modified)
8924c0c8cfcSFilipe Manana 					btrfs_set_inode_full_sync(inode);
8934c0c8cfcSFilipe Manana 			}
8944c0c8cfcSFilipe Manana 			free_extent_map(split);
8954c0c8cfcSFilipe Manana 			split = NULL;
8964c0c8cfcSFilipe Manana 		}
897db21370bSFilipe Manana remove_em:
8984c0c8cfcSFilipe Manana 		if (extent_map_in_tree(em)) {
8994c0c8cfcSFilipe Manana 			/*
9004c0c8cfcSFilipe Manana 			 * If the extent map is still in the tree it means that
9014c0c8cfcSFilipe Manana 			 * either of the following is true:
9024c0c8cfcSFilipe Manana 			 *
9034c0c8cfcSFilipe Manana 			 * 1) It fits entirely in our range (doesn't end beyond
9044c0c8cfcSFilipe Manana 			 *    it or starts before it);
9054c0c8cfcSFilipe Manana 			 *
9064c0c8cfcSFilipe Manana 			 * 2) It starts before our range and/or ends after our
9074c0c8cfcSFilipe Manana 			 *    range, and we were not able to allocate the extent
9084c0c8cfcSFilipe Manana 			 *    maps for split operations, @split and @split2.
9094c0c8cfcSFilipe Manana 			 *
9104c0c8cfcSFilipe Manana 			 * If we are at case 2) then we just remove the entire
9114c0c8cfcSFilipe Manana 			 * extent map - this is fine since if anyone needs it to
9124c0c8cfcSFilipe Manana 			 * access the subranges outside our range, will just
9134c0c8cfcSFilipe Manana 			 * load it again from the subvolume tree's file extent
9144c0c8cfcSFilipe Manana 			 * item. However if the extent map was in the list of
9154c0c8cfcSFilipe Manana 			 * modified extents, then we must mark the inode for a
9164c0c8cfcSFilipe Manana 			 * full fsync, otherwise a fast fsync will miss this
9174c0c8cfcSFilipe Manana 			 * extent if it's new and needs to be logged.
9184c0c8cfcSFilipe Manana 			 */
919db21370bSFilipe Manana 			if ((em->start < start || em_end > end) && modified) {
920db21370bSFilipe Manana 				ASSERT(!split);
9214c0c8cfcSFilipe Manana 				btrfs_set_inode_full_sync(inode);
9224c0c8cfcSFilipe Manana 			}
9234c0c8cfcSFilipe Manana 			remove_extent_mapping(em_tree, em);
9244c0c8cfcSFilipe Manana 		}
9254c0c8cfcSFilipe Manana 
926db21370bSFilipe Manana 		/*
927db21370bSFilipe Manana 		 * Once for the tree reference (we replaced or removed the
928db21370bSFilipe Manana 		 * extent map from the tree).
929db21370bSFilipe Manana 		 */
9304c0c8cfcSFilipe Manana 		free_extent_map(em);
931db21370bSFilipe Manana next:
932db21370bSFilipe Manana 		/* Once for us (for our lookup reference). */
9334c0c8cfcSFilipe Manana 		free_extent_map(em);
934db21370bSFilipe Manana 
935db21370bSFilipe Manana 		em = next_em;
9364c0c8cfcSFilipe Manana 	}
9374c0c8cfcSFilipe Manana 
938db21370bSFilipe Manana 	write_unlock(&em_tree->lock);
939db21370bSFilipe Manana 
9404c0c8cfcSFilipe Manana 	free_extent_map(split);
9414c0c8cfcSFilipe Manana 	free_extent_map(split2);
9424c0c8cfcSFilipe Manana }
943a1ba4c08SFilipe Manana 
944a1ba4c08SFilipe Manana /*
945a1ba4c08SFilipe Manana  * Replace a range in the inode's extent map tree with a new extent map.
946a1ba4c08SFilipe Manana  *
947a1ba4c08SFilipe Manana  * @inode:      The target inode.
948a1ba4c08SFilipe Manana  * @new_em:     The new extent map to add to the inode's extent map tree.
949a1ba4c08SFilipe Manana  * @modified:   Indicate if the new extent map should be added to the list of
950a1ba4c08SFilipe Manana  *              modified extents (for fast fsync tracking).
951a1ba4c08SFilipe Manana  *
952a1ba4c08SFilipe Manana  * Drops all the extent maps in the inode's extent map tree that intersect the
953a1ba4c08SFilipe Manana  * range of the new extent map and adds the new extent map to the tree.
954a1ba4c08SFilipe Manana  * The caller should have locked an appropriate file range in the inode's io
955a1ba4c08SFilipe Manana  * tree before calling this function.
956a1ba4c08SFilipe Manana  */
957a1ba4c08SFilipe Manana int btrfs_replace_extent_map_range(struct btrfs_inode *inode,
958a1ba4c08SFilipe Manana 				   struct extent_map *new_em,
959a1ba4c08SFilipe Manana 				   bool modified)
960a1ba4c08SFilipe Manana {
961a1ba4c08SFilipe Manana 	const u64 end = new_em->start + new_em->len - 1;
962a1ba4c08SFilipe Manana 	struct extent_map_tree *tree = &inode->extent_tree;
963a1ba4c08SFilipe Manana 	int ret;
964a1ba4c08SFilipe Manana 
965a1ba4c08SFilipe Manana 	ASSERT(!extent_map_in_tree(new_em));
966a1ba4c08SFilipe Manana 
967a1ba4c08SFilipe Manana 	/*
968a1ba4c08SFilipe Manana 	 * The caller has locked an appropriate file range in the inode's io
969a1ba4c08SFilipe Manana 	 * tree, but getting -EEXIST when adding the new extent map can still
970a1ba4c08SFilipe Manana 	 * happen in case there are extents that partially cover the range, and
971a1ba4c08SFilipe Manana 	 * this is due to two tasks operating on different parts of the extent.
972a1ba4c08SFilipe Manana 	 * See commit 18e83ac75bfe67 ("Btrfs: fix unexpected EEXIST from
973a1ba4c08SFilipe Manana 	 * btrfs_get_extent") for an example and details.
974a1ba4c08SFilipe Manana 	 */
975a1ba4c08SFilipe Manana 	do {
976a1ba4c08SFilipe Manana 		btrfs_drop_extent_map_range(inode, new_em->start, end, false);
977a1ba4c08SFilipe Manana 		write_lock(&tree->lock);
978a1ba4c08SFilipe Manana 		ret = add_extent_mapping(tree, new_em, modified);
979a1ba4c08SFilipe Manana 		write_unlock(&tree->lock);
980a1ba4c08SFilipe Manana 	} while (ret == -EEXIST);
981a1ba4c08SFilipe Manana 
982a1ba4c08SFilipe Manana 	return ret;
983a1ba4c08SFilipe Manana }
984