xref: /openbmc/linux/fs/btrfs/extent_map.c (revision f3109e33bb0a523f70a3394b51c878dbedb60528)
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);
58d1310b2eSChris Mason 	em->flags = 0;
59261507a0SLi Zefan 	em->compress_type = BTRFS_COMPRESS_NONE;
605dc562c5SJosef Bacik 	em->generation = 0;
61490b54d6SElena Reshetova 	refcount_set(&em->refs, 1);
625dc562c5SJosef Bacik 	INIT_LIST_HEAD(&em->list);
63a52d9a80SChris Mason 	return em;
64a52d9a80SChris Mason }
65a52d9a80SChris Mason 
669d2423c5SChristoph Hellwig /**
679d2423c5SChristoph Hellwig  * free_extent_map - drop reference count of an extent_map
6801327610SNicholas D Steeves  * @em:		extent map being released
699d2423c5SChristoph Hellwig  *
709d2423c5SChristoph Hellwig  * Drops the reference out on @em by one and free the structure
719d2423c5SChristoph Hellwig  * if the reference count hits zero.
729d2423c5SChristoph Hellwig  */
73a52d9a80SChris Mason void free_extent_map(struct extent_map *em)
74a52d9a80SChris Mason {
752bf5a725SChris Mason 	if (!em)
762bf5a725SChris Mason 		return;
77490b54d6SElena Reshetova 	WARN_ON(refcount_read(&em->refs) == 0);
78490b54d6SElena Reshetova 	if (refcount_dec_and_test(&em->refs)) {
79cbc0e928SFilipe Manana 		WARN_ON(extent_map_in_tree(em));
805dc562c5SJosef Bacik 		WARN_ON(!list_empty(&em->list));
81298a8f9cSWang Shilong 		if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
8295617d69SJeff Mahoney 			kfree(em->map_lookup);
83a52d9a80SChris Mason 		kmem_cache_free(extent_map_cache, em);
84a52d9a80SChris Mason 	}
85a52d9a80SChris Mason }
86a52d9a80SChris Mason 
8732193c14SFilipe David Borba Manana /* simple helper to do math around the end of an extent, handling wrap */
8832193c14SFilipe David Borba Manana static u64 range_end(u64 start, u64 len)
8932193c14SFilipe David Borba Manana {
9032193c14SFilipe David Borba Manana 	if (start + len < start)
9132193c14SFilipe David Borba Manana 		return (u64)-1;
9232193c14SFilipe David Borba Manana 	return start + len;
9332193c14SFilipe David Borba Manana }
9432193c14SFilipe David Borba Manana 
9507e1ce09SLiu Bo static int tree_insert(struct rb_root_cached *root, struct extent_map *em)
96a52d9a80SChris Mason {
9707e1ce09SLiu Bo 	struct rb_node **p = &root->rb_root.rb_node;
98a52d9a80SChris Mason 	struct rb_node *parent = NULL;
9932193c14SFilipe David Borba Manana 	struct extent_map *entry = NULL;
10032193c14SFilipe David Borba Manana 	struct rb_node *orig_parent = NULL;
10132193c14SFilipe David Borba Manana 	u64 end = range_end(em->start, em->len);
10207e1ce09SLiu Bo 	bool leftmost = true;
103a52d9a80SChris Mason 
104a52d9a80SChris Mason 	while (*p) {
105a52d9a80SChris Mason 		parent = *p;
106d1310b2eSChris Mason 		entry = rb_entry(parent, struct extent_map, rb_node);
107d1310b2eSChris Mason 
10807e1ce09SLiu Bo 		if (em->start < entry->start) {
109a52d9a80SChris Mason 			p = &(*p)->rb_left;
11007e1ce09SLiu Bo 		} else if (em->start >= extent_map_end(entry)) {
111a52d9a80SChris Mason 			p = &(*p)->rb_right;
11207e1ce09SLiu Bo 			leftmost = false;
11307e1ce09SLiu Bo 		} else {
11432193c14SFilipe David Borba Manana 			return -EEXIST;
115a52d9a80SChris Mason 		}
11607e1ce09SLiu Bo 	}
117a52d9a80SChris Mason 
11832193c14SFilipe David Borba Manana 	orig_parent = parent;
11932193c14SFilipe David Borba Manana 	while (parent && em->start >= extent_map_end(entry)) {
12032193c14SFilipe David Borba Manana 		parent = rb_next(parent);
12132193c14SFilipe David Borba Manana 		entry = rb_entry(parent, struct extent_map, rb_node);
12232193c14SFilipe David Borba Manana 	}
12332193c14SFilipe David Borba Manana 	if (parent)
12432193c14SFilipe David Borba Manana 		if (end > entry->start && em->start < extent_map_end(entry))
12532193c14SFilipe David Borba Manana 			return -EEXIST;
12632193c14SFilipe David Borba Manana 
12732193c14SFilipe David Borba Manana 	parent = orig_parent;
12832193c14SFilipe David Borba Manana 	entry = rb_entry(parent, struct extent_map, rb_node);
12932193c14SFilipe David Borba Manana 	while (parent && em->start < entry->start) {
13032193c14SFilipe David Borba Manana 		parent = rb_prev(parent);
13132193c14SFilipe David Borba Manana 		entry = rb_entry(parent, struct extent_map, rb_node);
13232193c14SFilipe David Borba Manana 	}
13332193c14SFilipe David Borba Manana 	if (parent)
13432193c14SFilipe David Borba Manana 		if (end > entry->start && em->start < extent_map_end(entry))
13532193c14SFilipe David Borba Manana 			return -EEXIST;
13632193c14SFilipe David Borba Manana 
13732193c14SFilipe David Borba Manana 	rb_link_node(&em->rb_node, orig_parent, p);
13807e1ce09SLiu Bo 	rb_insert_color_cached(&em->rb_node, root, leftmost);
13932193c14SFilipe David Borba Manana 	return 0;
140a52d9a80SChris Mason }
141a52d9a80SChris Mason 
142d352ac68SChris Mason /*
143d352ac68SChris Mason  * search through the tree for an extent_map with a given offset.  If
144d352ac68SChris Mason  * it can't be found, try to find some neighboring extents
145d352ac68SChris Mason  */
146a52d9a80SChris Mason static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
1475f56406aSChris Mason 				     struct rb_node **prev_ret,
1485f56406aSChris Mason 				     struct rb_node **next_ret)
149a52d9a80SChris Mason {
150a52d9a80SChris Mason 	struct rb_node *n = root->rb_node;
151a52d9a80SChris Mason 	struct rb_node *prev = NULL;
1525f56406aSChris Mason 	struct rb_node *orig_prev = NULL;
153d1310b2eSChris Mason 	struct extent_map *entry;
154d1310b2eSChris Mason 	struct extent_map *prev_entry = NULL;
155a52d9a80SChris Mason 
156a52d9a80SChris Mason 	while (n) {
157d1310b2eSChris Mason 		entry = rb_entry(n, struct extent_map, rb_node);
158a52d9a80SChris Mason 		prev = n;
159a52d9a80SChris Mason 		prev_entry = entry;
160a52d9a80SChris Mason 
161a52d9a80SChris Mason 		if (offset < entry->start)
162a52d9a80SChris Mason 			n = n->rb_left;
163d1310b2eSChris Mason 		else if (offset >= extent_map_end(entry))
164a52d9a80SChris Mason 			n = n->rb_right;
165a52d9a80SChris Mason 		else
166a52d9a80SChris Mason 			return n;
167a52d9a80SChris Mason 	}
1685f56406aSChris Mason 
1695f56406aSChris Mason 	if (prev_ret) {
1705f56406aSChris Mason 		orig_prev = prev;
171d1310b2eSChris Mason 		while (prev && offset >= extent_map_end(prev_entry)) {
172a52d9a80SChris Mason 			prev = rb_next(prev);
173d1310b2eSChris Mason 			prev_entry = rb_entry(prev, struct extent_map, rb_node);
174a52d9a80SChris Mason 		}
175a52d9a80SChris Mason 		*prev_ret = prev;
1765f56406aSChris Mason 		prev = orig_prev;
1775f56406aSChris Mason 	}
1785f56406aSChris Mason 
1795f56406aSChris Mason 	if (next_ret) {
180d1310b2eSChris Mason 		prev_entry = rb_entry(prev, struct extent_map, rb_node);
1815f56406aSChris Mason 		while (prev && offset < prev_entry->start) {
1825f56406aSChris Mason 			prev = rb_prev(prev);
183d1310b2eSChris Mason 			prev_entry = rb_entry(prev, struct extent_map, rb_node);
1845f56406aSChris Mason 		}
1855f56406aSChris Mason 		*next_ret = prev;
1865f56406aSChris Mason 	}
187a52d9a80SChris Mason 	return NULL;
188a52d9a80SChris Mason }
189a52d9a80SChris Mason 
190d352ac68SChris Mason /* check to see if two extent_map structs are adjacent and safe to merge */
191d1310b2eSChris Mason static int mergable_maps(struct extent_map *prev, struct extent_map *next)
192a52d9a80SChris Mason {
1937f3c74fbSChris Mason 	if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
1947f3c74fbSChris Mason 		return 0;
1957f3c74fbSChris Mason 
196c8b97818SChris Mason 	/*
197c8b97818SChris Mason 	 * don't merge compressed extents, we need to know their
198c8b97818SChris Mason 	 * actual size
199c8b97818SChris Mason 	 */
200c8b97818SChris Mason 	if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
201c8b97818SChris Mason 		return 0;
202c8b97818SChris Mason 
203201a9038SJosef Bacik 	if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
204201a9038SJosef Bacik 	    test_bit(EXTENT_FLAG_LOGGING, &next->flags))
205201a9038SJosef Bacik 		return 0;
206201a9038SJosef Bacik 
20709a2a8f9SJosef Bacik 	/*
20809a2a8f9SJosef Bacik 	 * We don't want to merge stuff that hasn't been written to the log yet
20909a2a8f9SJosef Bacik 	 * since it may not reflect exactly what is on disk, and that would be
21009a2a8f9SJosef Bacik 	 * bad.
21109a2a8f9SJosef Bacik 	 */
21209a2a8f9SJosef Bacik 	if (!list_empty(&prev->list) || !list_empty(&next->list))
21309a2a8f9SJosef Bacik 		return 0;
21409a2a8f9SJosef Bacik 
215951e05a9SNikolay Borisov 	ASSERT(next->block_start != EXTENT_MAP_DELALLOC &&
216951e05a9SNikolay Borisov 	       prev->block_start != EXTENT_MAP_DELALLOC);
217951e05a9SNikolay Borisov 
218c3e14909SDavid Sterba 	if (prev->map_lookup || next->map_lookup)
219c3e14909SDavid Sterba 		ASSERT(test_bit(EXTENT_FLAG_FS_MAPPING, &prev->flags) &&
220c3e14909SDavid Sterba 		       test_bit(EXTENT_FLAG_FS_MAPPING, &next->flags));
221c3e14909SDavid Sterba 
222d1310b2eSChris Mason 	if (extent_map_end(prev) == next->start &&
223d1310b2eSChris Mason 	    prev->flags == next->flags &&
224c3e14909SDavid Sterba 	    prev->map_lookup == next->map_lookup &&
225d1310b2eSChris Mason 	    ((next->block_start == EXTENT_MAP_HOLE &&
226d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_HOLE) ||
227d1310b2eSChris Mason 	     (next->block_start == EXTENT_MAP_INLINE &&
228d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_INLINE) ||
229d1310b2eSChris Mason 	     (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
230d1310b2eSChris Mason 	      next->block_start == extent_map_block_end(prev)))) {
231d1310b2eSChris Mason 		return 1;
232d1310b2eSChris Mason 	}
233a52d9a80SChris Mason 	return 0;
234a52d9a80SChris Mason }
235a52d9a80SChris Mason 
2364d2c8f62SLi Zefan static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
237a1ed835eSChris Mason {
238a1ed835eSChris Mason 	struct extent_map *merge = NULL;
239a1ed835eSChris Mason 	struct rb_node *rb;
240a1ed835eSChris Mason 
241ac05ca91SFilipe Manana 	/*
242ac05ca91SFilipe Manana 	 * We can't modify an extent map that is in the tree and that is being
243ac05ca91SFilipe Manana 	 * used by another task, as it can cause that other task to see it in
244ac05ca91SFilipe Manana 	 * inconsistent state during the merging. We always have 1 reference for
245ac05ca91SFilipe Manana 	 * the tree and 1 for this task (which is unpinning the extent map or
246ac05ca91SFilipe Manana 	 * clearing the logging flag), so anything > 2 means it's being used by
247ac05ca91SFilipe Manana 	 * other tasks too.
248ac05ca91SFilipe Manana 	 */
249ac05ca91SFilipe Manana 	if (refcount_read(&em->refs) > 2)
250ac05ca91SFilipe Manana 		return;
251ac05ca91SFilipe Manana 
252a1ed835eSChris Mason 	if (em->start != 0) {
253a1ed835eSChris Mason 		rb = rb_prev(&em->rb_node);
254a1ed835eSChris Mason 		if (rb)
255a1ed835eSChris Mason 			merge = rb_entry(rb, struct extent_map, rb_node);
256a1ed835eSChris Mason 		if (rb && mergable_maps(merge, em)) {
257a1ed835eSChris Mason 			em->start = merge->start;
25870c8a91cSJosef Bacik 			em->orig_start = merge->orig_start;
259a1ed835eSChris Mason 			em->len += merge->len;
260a1ed835eSChris Mason 			em->block_len += merge->block_len;
261a1ed835eSChris Mason 			em->block_start = merge->block_start;
26270c8a91cSJosef Bacik 			em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
26370c8a91cSJosef Bacik 			em->mod_start = merge->mod_start;
26470c8a91cSJosef Bacik 			em->generation = max(em->generation, merge->generation);
265199257a7SQu Wenruo 			set_bit(EXTENT_FLAG_MERGED, &em->flags);
2665dc562c5SJosef Bacik 
26707e1ce09SLiu Bo 			rb_erase_cached(&merge->rb_node, &tree->map);
268cbc0e928SFilipe Manana 			RB_CLEAR_NODE(&merge->rb_node);
269a1ed835eSChris Mason 			free_extent_map(merge);
270a1ed835eSChris Mason 		}
271a1ed835eSChris Mason 	}
272a1ed835eSChris Mason 
273a1ed835eSChris Mason 	rb = rb_next(&em->rb_node);
274a1ed835eSChris Mason 	if (rb)
275a1ed835eSChris Mason 		merge = rb_entry(rb, struct extent_map, rb_node);
276a1ed835eSChris Mason 	if (rb && mergable_maps(em, merge)) {
277a1ed835eSChris Mason 		em->len += merge->len;
278d527afe1SFilipe David Borba Manana 		em->block_len += merge->block_len;
27907e1ce09SLiu Bo 		rb_erase_cached(&merge->rb_node, &tree->map);
280cbc0e928SFilipe Manana 		RB_CLEAR_NODE(&merge->rb_node);
28170c8a91cSJosef Bacik 		em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
28270c8a91cSJosef Bacik 		em->generation = max(em->generation, merge->generation);
283199257a7SQu Wenruo 		set_bit(EXTENT_FLAG_MERGED, &em->flags);
284a1ed835eSChris Mason 		free_extent_map(merge);
285a1ed835eSChris Mason 	}
2864d2c8f62SLi Zefan }
2874d2c8f62SLi Zefan 
2885dc562c5SJosef Bacik /**
28952b1de91SLiu Bo  * unpin_extent_cache - unpin an extent from the cache
2905dc562c5SJosef Bacik  * @tree:	tree to unpin the extent in
2915dc562c5SJosef Bacik  * @start:	logical offset in the file
2925dc562c5SJosef Bacik  * @len:	length of the extent
2935dc562c5SJosef Bacik  * @gen:	generation that this extent has been modified in
2945dc562c5SJosef Bacik  *
2955dc562c5SJosef Bacik  * Called after an extent has been written to disk properly.  Set the generation
2965dc562c5SJosef Bacik  * to the generation that actually added the file item to the inode so we know
2975dc562c5SJosef Bacik  * we need to sync this extent when we call fsync().
2985dc562c5SJosef Bacik  */
2995dc562c5SJosef Bacik int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
3005dc562c5SJosef Bacik 		       u64 gen)
3014d2c8f62SLi Zefan {
3024d2c8f62SLi Zefan 	int ret = 0;
3034d2c8f62SLi Zefan 	struct extent_map *em;
3044e2f84e6SLiu Bo 	bool prealloc = false;
3054d2c8f62SLi Zefan 
3064d2c8f62SLi Zefan 	write_lock(&tree->lock);
3074d2c8f62SLi Zefan 	em = lookup_extent_mapping(tree, start, len);
3084d2c8f62SLi Zefan 
3094d2c8f62SLi Zefan 	WARN_ON(!em || em->start != start);
3104d2c8f62SLi Zefan 
3114d2c8f62SLi Zefan 	if (!em)
3124d2c8f62SLi Zefan 		goto out;
3134d2c8f62SLi Zefan 
3145dc562c5SJosef Bacik 	em->generation = gen;
3154d2c8f62SLi Zefan 	clear_bit(EXTENT_FLAG_PINNED, &em->flags);
3164e2f84e6SLiu Bo 	em->mod_start = em->start;
3174e2f84e6SLiu Bo 	em->mod_len = em->len;
3184e2f84e6SLiu Bo 
319b11e234dSJosef Bacik 	if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
3204e2f84e6SLiu Bo 		prealloc = true;
321b11e234dSJosef Bacik 		clear_bit(EXTENT_FLAG_FILLING, &em->flags);
3224e2f84e6SLiu Bo 	}
3234d2c8f62SLi Zefan 
3244d2c8f62SLi Zefan 	try_merge_map(tree, em);
3254e2f84e6SLiu Bo 
3264e2f84e6SLiu Bo 	if (prealloc) {
3274e2f84e6SLiu Bo 		em->mod_start = em->start;
3284e2f84e6SLiu Bo 		em->mod_len = em->len;
3294e2f84e6SLiu Bo 	}
3304e2f84e6SLiu Bo 
331a1ed835eSChris Mason 	free_extent_map(em);
332a1ed835eSChris Mason out:
333a1ed835eSChris Mason 	write_unlock(&tree->lock);
334a1ed835eSChris Mason 	return ret;
335a1ed835eSChris Mason 
336a1ed835eSChris Mason }
337a1ed835eSChris Mason 
338201a9038SJosef Bacik void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
339201a9038SJosef Bacik {
340201a9038SJosef Bacik 	clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
341cbc0e928SFilipe Manana 	if (extent_map_in_tree(em))
342201a9038SJosef Bacik 		try_merge_map(tree, em);
343201a9038SJosef Bacik }
344201a9038SJosef Bacik 
345176840b3SFilipe Manana static inline void setup_extent_mapping(struct extent_map_tree *tree,
346176840b3SFilipe Manana 					struct extent_map *em,
347176840b3SFilipe Manana 					int modified)
348176840b3SFilipe Manana {
349490b54d6SElena Reshetova 	refcount_inc(&em->refs);
350176840b3SFilipe Manana 	em->mod_start = em->start;
351176840b3SFilipe Manana 	em->mod_len = em->len;
352176840b3SFilipe Manana 
353176840b3SFilipe Manana 	if (modified)
354176840b3SFilipe Manana 		list_move(&em->list, &tree->modified_extents);
355176840b3SFilipe Manana 	else
356176840b3SFilipe Manana 		try_merge_map(tree, em);
357176840b3SFilipe Manana }
358176840b3SFilipe Manana 
3591c11b63eSJeff Mahoney static void extent_map_device_set_bits(struct extent_map *em, unsigned bits)
3601c11b63eSJeff Mahoney {
3611c11b63eSJeff Mahoney 	struct map_lookup *map = em->map_lookup;
3621c11b63eSJeff Mahoney 	u64 stripe_size = em->orig_block_len;
3631c11b63eSJeff Mahoney 	int i;
3641c11b63eSJeff Mahoney 
3651c11b63eSJeff Mahoney 	for (i = 0; i < map->num_stripes; i++) {
3664c664611SQu Wenruo 		struct btrfs_io_stripe *stripe = &map->stripes[i];
3671c11b63eSJeff Mahoney 		struct btrfs_device *device = stripe->dev;
3681c11b63eSJeff Mahoney 
3691c11b63eSJeff Mahoney 		set_extent_bits_nowait(&device->alloc_state, stripe->physical,
3701c11b63eSJeff Mahoney 				 stripe->physical + stripe_size - 1, bits);
3711c11b63eSJeff Mahoney 	}
3721c11b63eSJeff Mahoney }
3731c11b63eSJeff Mahoney 
3741c11b63eSJeff Mahoney static void extent_map_device_clear_bits(struct extent_map *em, unsigned bits)
3751c11b63eSJeff Mahoney {
3761c11b63eSJeff Mahoney 	struct map_lookup *map = em->map_lookup;
3771c11b63eSJeff Mahoney 	u64 stripe_size = em->orig_block_len;
3781c11b63eSJeff Mahoney 	int i;
3791c11b63eSJeff Mahoney 
3801c11b63eSJeff Mahoney 	for (i = 0; i < map->num_stripes; i++) {
3814c664611SQu Wenruo 		struct btrfs_io_stripe *stripe = &map->stripes[i];
3821c11b63eSJeff Mahoney 		struct btrfs_device *device = stripe->dev;
3831c11b63eSJeff Mahoney 
3841c11b63eSJeff Mahoney 		__clear_extent_bit(&device->alloc_state, stripe->physical,
3851c11b63eSJeff Mahoney 				   stripe->physical + stripe_size - 1, bits,
386bd015294SJosef Bacik 				   NULL, GFP_NOWAIT, NULL);
3871c11b63eSJeff Mahoney 	}
3881c11b63eSJeff Mahoney }
3891c11b63eSJeff Mahoney 
3909d2423c5SChristoph Hellwig /**
391401bd2ddSNikolay Borisov  * Add new extent map to the extent tree
392401bd2ddSNikolay Borisov  *
3939d2423c5SChristoph Hellwig  * @tree:	tree to insert new map in
3949d2423c5SChristoph Hellwig  * @em:		map to insert
395401bd2ddSNikolay Borisov  * @modified:	indicate whether the given @em should be added to the
396401bd2ddSNikolay Borisov  *	        modified list, which indicates the extent needs to be logged
3979d2423c5SChristoph Hellwig  *
3989d2423c5SChristoph Hellwig  * Insert @em into @tree or perform a simple forward/backward merge with
3999d2423c5SChristoph Hellwig  * existing mappings.  The extent_map struct passed in will be inserted
4009d2423c5SChristoph Hellwig  * into the tree directly, with an additional reference taken, or a
40125985edcSLucas De Marchi  * reference dropped if the merge attempt was successful.
402a52d9a80SChris Mason  */
403a52d9a80SChris Mason int add_extent_mapping(struct extent_map_tree *tree,
40409a2a8f9SJosef Bacik 		       struct extent_map *em, int modified)
405a52d9a80SChris Mason {
406a52d9a80SChris Mason 	int ret = 0;
407a52d9a80SChris Mason 
408d23ea3faSDavid Sterba 	lockdep_assert_held_write(&tree->lock);
409d23ea3faSDavid Sterba 
41032193c14SFilipe David Borba Manana 	ret = tree_insert(&tree->map, em);
41132193c14SFilipe David Borba Manana 	if (ret)
4127c2fe32aSChris Mason 		goto out;
41332193c14SFilipe David Borba Manana 
414176840b3SFilipe Manana 	setup_extent_mapping(tree, em, modified);
4158811133dSNikolay Borisov 	if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags)) {
4161c11b63eSJeff Mahoney 		extent_map_device_set_bits(em, CHUNK_ALLOCATED);
4178811133dSNikolay Borisov 		extent_map_device_clear_bits(em, CHUNK_TRIMMED);
4188811133dSNikolay Borisov 	}
419a52d9a80SChris Mason out:
420a52d9a80SChris Mason 	return ret;
421a52d9a80SChris Mason }
422a52d9a80SChris Mason 
42348a3b636SEric Sandeen static struct extent_map *
42448a3b636SEric Sandeen __lookup_extent_mapping(struct extent_map_tree *tree,
425ed64f066SLi Zefan 			u64 start, u64 len, int strict)
426ed64f066SLi Zefan {
427ed64f066SLi Zefan 	struct extent_map *em;
428ed64f066SLi Zefan 	struct rb_node *rb_node;
429ed64f066SLi Zefan 	struct rb_node *prev = NULL;
430ed64f066SLi Zefan 	struct rb_node *next = NULL;
431ed64f066SLi Zefan 	u64 end = range_end(start, len);
432ed64f066SLi Zefan 
43307e1ce09SLiu Bo 	rb_node = __tree_search(&tree->map.rb_root, start, &prev, &next);
434ed64f066SLi Zefan 	if (!rb_node) {
435ed64f066SLi Zefan 		if (prev)
436ed64f066SLi Zefan 			rb_node = prev;
437ed64f066SLi Zefan 		else if (next)
438ed64f066SLi Zefan 			rb_node = next;
439ed64f066SLi Zefan 		else
440ed64f066SLi Zefan 			return NULL;
441ed64f066SLi Zefan 	}
442ed64f066SLi Zefan 
443ed64f066SLi Zefan 	em = rb_entry(rb_node, struct extent_map, rb_node);
444ed64f066SLi Zefan 
445ed64f066SLi Zefan 	if (strict && !(end > em->start && start < extent_map_end(em)))
446ed64f066SLi Zefan 		return NULL;
447ed64f066SLi Zefan 
448490b54d6SElena Reshetova 	refcount_inc(&em->refs);
449ed64f066SLi Zefan 	return em;
450ed64f066SLi Zefan }
451ed64f066SLi Zefan 
4529d2423c5SChristoph Hellwig /**
4539d2423c5SChristoph Hellwig  * lookup_extent_mapping - lookup extent_map
4549d2423c5SChristoph Hellwig  * @tree:	tree to lookup in
4559d2423c5SChristoph Hellwig  * @start:	byte offset to start the search
4569d2423c5SChristoph Hellwig  * @len:	length of the lookup range
4579d2423c5SChristoph Hellwig  *
4589d2423c5SChristoph Hellwig  * Find and return the first extent_map struct in @tree that intersects the
4599d2423c5SChristoph Hellwig  * [start, len] range.  There may be additional objects in the tree that
4609d2423c5SChristoph Hellwig  * intersect, so check the object returned carefully to make sure that no
4619d2423c5SChristoph Hellwig  * additional lookups are needed.
462a52d9a80SChris Mason  */
463a52d9a80SChris Mason struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
464d1310b2eSChris Mason 					 u64 start, u64 len)
465a52d9a80SChris Mason {
466ed64f066SLi Zefan 	return __lookup_extent_mapping(tree, start, len, 1);
467a52d9a80SChris Mason }
468a52d9a80SChris Mason 
4699d2423c5SChristoph Hellwig /**
470b917b7c3SChris Mason  * search_extent_mapping - find a nearby extent map
471b917b7c3SChris Mason  * @tree:	tree to lookup in
472b917b7c3SChris Mason  * @start:	byte offset to start the search
473b917b7c3SChris Mason  * @len:	length of the lookup range
474b917b7c3SChris Mason  *
475b917b7c3SChris Mason  * Find and return the first extent_map struct in @tree that intersects the
476b917b7c3SChris Mason  * [start, len] range.
477b917b7c3SChris Mason  *
478b917b7c3SChris Mason  * If one can't be found, any nearby extent may be returned
479b917b7c3SChris Mason  */
480b917b7c3SChris Mason struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
481b917b7c3SChris Mason 					 u64 start, u64 len)
482b917b7c3SChris Mason {
483ed64f066SLi Zefan 	return __lookup_extent_mapping(tree, start, len, 0);
484b917b7c3SChris Mason }
485b917b7c3SChris Mason 
486b917b7c3SChris Mason /**
4879d2423c5SChristoph Hellwig  * remove_extent_mapping - removes an extent_map from the extent tree
4889d2423c5SChristoph Hellwig  * @tree:	extent tree to remove from
489bb7ab3b9SAdam Buchbinder  * @em:		extent map being removed
4909d2423c5SChristoph Hellwig  *
4919d2423c5SChristoph Hellwig  * Removes @em from @tree.  No reference counts are dropped, and no checks
4929d2423c5SChristoph Hellwig  * are done to see if the range is in use
493a52d9a80SChris Mason  */
494c1766dd7Szhong jiang void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
495a52d9a80SChris Mason {
4966d3b050eSFilipe Manana 	lockdep_assert_held_write(&tree->lock);
4976d3b050eSFilipe Manana 
4987f3c74fbSChris Mason 	WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
49907e1ce09SLiu Bo 	rb_erase_cached(&em->rb_node, &tree->map);
500ff44c6e3SJosef Bacik 	if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
5015dc562c5SJosef Bacik 		list_del_init(&em->list);
5021c11b63eSJeff Mahoney 	if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
5031c11b63eSJeff Mahoney 		extent_map_device_clear_bits(em, CHUNK_ALLOCATED);
504cbc0e928SFilipe Manana 	RB_CLEAR_NODE(&em->rb_node);
505a52d9a80SChris Mason }
506176840b3SFilipe Manana 
507176840b3SFilipe Manana void replace_extent_mapping(struct extent_map_tree *tree,
508176840b3SFilipe Manana 			    struct extent_map *cur,
509176840b3SFilipe Manana 			    struct extent_map *new,
510176840b3SFilipe Manana 			    int modified)
511176840b3SFilipe Manana {
5126d3b050eSFilipe Manana 	lockdep_assert_held_write(&tree->lock);
5136d3b050eSFilipe Manana 
514176840b3SFilipe Manana 	WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));
515176840b3SFilipe Manana 	ASSERT(extent_map_in_tree(cur));
516176840b3SFilipe Manana 	if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))
517176840b3SFilipe Manana 		list_del_init(&cur->list);
51807e1ce09SLiu Bo 	rb_replace_node_cached(&cur->rb_node, &new->rb_node, &tree->map);
519176840b3SFilipe Manana 	RB_CLEAR_NODE(&cur->rb_node);
520176840b3SFilipe Manana 
521176840b3SFilipe Manana 	setup_extent_mapping(tree, new, modified);
522176840b3SFilipe Manana }
523c04e61b5SLiu Bo 
524c04e61b5SLiu Bo static struct extent_map *next_extent_map(struct extent_map *em)
525c04e61b5SLiu Bo {
526c04e61b5SLiu Bo 	struct rb_node *next;
527c04e61b5SLiu Bo 
528c04e61b5SLiu Bo 	next = rb_next(&em->rb_node);
529c04e61b5SLiu Bo 	if (!next)
530c04e61b5SLiu Bo 		return NULL;
531c04e61b5SLiu Bo 	return container_of(next, struct extent_map, rb_node);
532c04e61b5SLiu Bo }
533c04e61b5SLiu Bo 
534c04e61b5SLiu Bo static struct extent_map *prev_extent_map(struct extent_map *em)
535c04e61b5SLiu Bo {
536c04e61b5SLiu Bo 	struct rb_node *prev;
537c04e61b5SLiu Bo 
538c04e61b5SLiu Bo 	prev = rb_prev(&em->rb_node);
539c04e61b5SLiu Bo 	if (!prev)
540c04e61b5SLiu Bo 		return NULL;
541c04e61b5SLiu Bo 	return container_of(prev, struct extent_map, rb_node);
542c04e61b5SLiu Bo }
543c04e61b5SLiu Bo 
54452042d8eSAndrea Gelmini /*
54552042d8eSAndrea Gelmini  * Helper for btrfs_get_extent.  Given an existing extent in the tree,
546c04e61b5SLiu Bo  * the existing extent is the nearest extent to map_start,
547c04e61b5SLiu Bo  * and an extent that you want to insert, deal with overlap and insert
548c04e61b5SLiu Bo  * the best fitted new extent into the tree.
549c04e61b5SLiu Bo  */
5505f4791f4SLiu Bo static noinline int merge_extent_mapping(struct extent_map_tree *em_tree,
551c04e61b5SLiu Bo 					 struct extent_map *existing,
552c04e61b5SLiu Bo 					 struct extent_map *em,
553c04e61b5SLiu Bo 					 u64 map_start)
554c04e61b5SLiu Bo {
555c04e61b5SLiu Bo 	struct extent_map *prev;
556c04e61b5SLiu Bo 	struct extent_map *next;
557c04e61b5SLiu Bo 	u64 start;
558c04e61b5SLiu Bo 	u64 end;
559c04e61b5SLiu Bo 	u64 start_diff;
560c04e61b5SLiu Bo 
561c04e61b5SLiu Bo 	BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
562c04e61b5SLiu Bo 
563c04e61b5SLiu Bo 	if (existing->start > map_start) {
564c04e61b5SLiu Bo 		next = existing;
565c04e61b5SLiu Bo 		prev = prev_extent_map(next);
566c04e61b5SLiu Bo 	} else {
567c04e61b5SLiu Bo 		prev = existing;
568c04e61b5SLiu Bo 		next = next_extent_map(prev);
569c04e61b5SLiu Bo 	}
570c04e61b5SLiu Bo 
571c04e61b5SLiu Bo 	start = prev ? extent_map_end(prev) : em->start;
572c04e61b5SLiu Bo 	start = max_t(u64, start, em->start);
573c04e61b5SLiu Bo 	end = next ? next->start : extent_map_end(em);
574c04e61b5SLiu Bo 	end = min_t(u64, end, extent_map_end(em));
575c04e61b5SLiu Bo 	start_diff = start - em->start;
576c04e61b5SLiu Bo 	em->start = start;
577c04e61b5SLiu Bo 	em->len = end - start;
578c04e61b5SLiu Bo 	if (em->block_start < EXTENT_MAP_LAST_BYTE &&
579c04e61b5SLiu Bo 	    !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
580c04e61b5SLiu Bo 		em->block_start += start_diff;
581c04e61b5SLiu Bo 		em->block_len = em->len;
582c04e61b5SLiu Bo 	}
583c04e61b5SLiu Bo 	return add_extent_mapping(em_tree, em, 0);
584c04e61b5SLiu Bo }
585c04e61b5SLiu Bo 
586c04e61b5SLiu Bo /**
5879ad37bb3SNikolay Borisov  * Add extent mapping into em_tree
5889ad37bb3SNikolay Borisov  *
5899ad37bb3SNikolay Borisov  * @fs_info:  the filesystem
5909ad37bb3SNikolay Borisov  * @em_tree:  extent tree into which we want to insert the extent mapping
5919ad37bb3SNikolay Borisov  * @em_in:    extent we are inserting
5929ad37bb3SNikolay Borisov  * @start:    start of the logical range btrfs_get_extent() is requesting
5939ad37bb3SNikolay Borisov  * @len:      length of the logical range btrfs_get_extent() is requesting
594c04e61b5SLiu Bo  *
595c04e61b5SLiu Bo  * Note that @em_in's range may be different from [start, start+len),
596c04e61b5SLiu Bo  * but they must be overlapped.
597c04e61b5SLiu Bo  *
598c04e61b5SLiu Bo  * Insert @em_in into @em_tree. In case there is an overlapping range, handle
599c04e61b5SLiu Bo  * the -EEXIST by either:
600c04e61b5SLiu Bo  * a) Returning the existing extent in @em_in if @start is within the
601c04e61b5SLiu Bo  *    existing em.
602c04e61b5SLiu Bo  * b) Merge the existing extent with @em_in passed in.
603c04e61b5SLiu Bo  *
604c04e61b5SLiu Bo  * Return 0 on success, otherwise -EEXIST.
605c04e61b5SLiu Bo  *
606c04e61b5SLiu Bo  */
607f46b24c9SDavid Sterba int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info,
608f46b24c9SDavid Sterba 			     struct extent_map_tree *em_tree,
609c04e61b5SLiu Bo 			     struct extent_map **em_in, u64 start, u64 len)
610c04e61b5SLiu Bo {
611c04e61b5SLiu Bo 	int ret;
612c04e61b5SLiu Bo 	struct extent_map *em = *em_in;
613c04e61b5SLiu Bo 
614c04e61b5SLiu Bo 	ret = add_extent_mapping(em_tree, em, 0);
615c04e61b5SLiu Bo 	/* it is possible that someone inserted the extent into the tree
616c04e61b5SLiu Bo 	 * while we had the lock dropped.  It is also possible that
617c04e61b5SLiu Bo 	 * an overlapping map exists in the tree
618c04e61b5SLiu Bo 	 */
619c04e61b5SLiu Bo 	if (ret == -EEXIST) {
620c04e61b5SLiu Bo 		struct extent_map *existing;
621c04e61b5SLiu Bo 
622c04e61b5SLiu Bo 		ret = 0;
623c04e61b5SLiu Bo 
624c04e61b5SLiu Bo 		existing = search_extent_mapping(em_tree, start, len);
625393da918SLiu Bo 
626f46b24c9SDavid Sterba 		trace_btrfs_handle_em_exist(fs_info, existing, em, start, len);
627393da918SLiu Bo 
628c04e61b5SLiu Bo 		/*
629c04e61b5SLiu Bo 		 * existing will always be non-NULL, since there must be
630c04e61b5SLiu Bo 		 * extent causing the -EEXIST.
631c04e61b5SLiu Bo 		 */
632c04e61b5SLiu Bo 		if (start >= existing->start &&
633c04e61b5SLiu Bo 		    start < extent_map_end(existing)) {
634c04e61b5SLiu Bo 			free_extent_map(em);
635c04e61b5SLiu Bo 			*em_in = existing;
636c04e61b5SLiu Bo 			ret = 0;
637c04e61b5SLiu Bo 		} else {
6389a7e10e7SLiu Bo 			u64 orig_start = em->start;
6399a7e10e7SLiu Bo 			u64 orig_len = em->len;
6409a7e10e7SLiu Bo 
641c04e61b5SLiu Bo 			/*
642c04e61b5SLiu Bo 			 * The existing extent map is the one nearest to
643c04e61b5SLiu Bo 			 * the [start, start + len) range which overlaps
644c04e61b5SLiu Bo 			 */
645c04e61b5SLiu Bo 			ret = merge_extent_mapping(em_tree, existing,
646c04e61b5SLiu Bo 						   em, start);
647c04e61b5SLiu Bo 			if (ret) {
648c04e61b5SLiu Bo 				free_extent_map(em);
649c04e61b5SLiu Bo 				*em_in = NULL;
6509a7e10e7SLiu Bo 				WARN_ONCE(ret,
6519a7e10e7SLiu Bo "unexpected error %d: merge existing(start %llu len %llu) with em(start %llu len %llu)\n",
6529a7e10e7SLiu Bo 					  ret, existing->start, existing->len,
6539a7e10e7SLiu Bo 					  orig_start, orig_len);
654c04e61b5SLiu Bo 			}
6559a7e10e7SLiu Bo 			free_extent_map(existing);
656c04e61b5SLiu Bo 		}
657c04e61b5SLiu Bo 	}
658c04e61b5SLiu Bo 
659c04e61b5SLiu Bo 	ASSERT(ret == 0 || ret == -EEXIST);
660c04e61b5SLiu Bo 	return ret;
661c04e61b5SLiu Bo }
6624c0c8cfcSFilipe Manana 
6634c0c8cfcSFilipe Manana /*
6644c0c8cfcSFilipe Manana  * Drop all extent maps in a given range.
6654c0c8cfcSFilipe Manana  *
6664c0c8cfcSFilipe Manana  * @inode:       The target inode.
6674c0c8cfcSFilipe Manana  * @start:       Start offset of the range.
6684c0c8cfcSFilipe Manana  * @end:         End offset of the range (inclusive value).
6694c0c8cfcSFilipe Manana  * @skip_pinned: Indicate if pinned extent maps should be ignored or not.
6704c0c8cfcSFilipe Manana  *
6714c0c8cfcSFilipe Manana  * This drops all the extent maps that intersect the given range [@start, @end].
6724c0c8cfcSFilipe Manana  * Extent maps that partially overlap the range and extend behind or beyond it,
6734c0c8cfcSFilipe Manana  * are split.
6744c0c8cfcSFilipe Manana  * The caller should have locked an appropriate file range in the inode's io
6754c0c8cfcSFilipe Manana  * tree before calling this function.
6764c0c8cfcSFilipe Manana  */
6774c0c8cfcSFilipe Manana void btrfs_drop_extent_map_range(struct btrfs_inode *inode, u64 start, u64 end,
6784c0c8cfcSFilipe Manana 				 bool skip_pinned)
6794c0c8cfcSFilipe Manana {
6804c0c8cfcSFilipe Manana 	struct extent_map *split = NULL;
6814c0c8cfcSFilipe Manana 	struct extent_map *split2 = NULL;
6824c0c8cfcSFilipe Manana 	struct extent_map_tree *em_tree = &inode->extent_tree;
6834c0c8cfcSFilipe Manana 	u64 len = end - start + 1;
6844c0c8cfcSFilipe Manana 	bool testend = true;
6854c0c8cfcSFilipe Manana 
6864c0c8cfcSFilipe Manana 	WARN_ON(end < start);
6874c0c8cfcSFilipe Manana 	if (end == (u64)-1) {
6884c0c8cfcSFilipe Manana 		len = (u64)-1;
6894c0c8cfcSFilipe Manana 		testend = false;
6904c0c8cfcSFilipe Manana 	}
6914c0c8cfcSFilipe Manana 	while (1) {
6924c0c8cfcSFilipe Manana 		struct extent_map *em;
693*f3109e33SFilipe Manana 		u64 em_end;
6944c0c8cfcSFilipe Manana 		u64 gen;
6954c0c8cfcSFilipe Manana 		unsigned long flags;
6964c0c8cfcSFilipe Manana 		bool ends_after_range = false;
6974c0c8cfcSFilipe Manana 		bool no_splits = false;
6984c0c8cfcSFilipe Manana 		bool modified;
6994c0c8cfcSFilipe Manana 		bool compressed;
7004c0c8cfcSFilipe Manana 
7014c0c8cfcSFilipe Manana 		if (!split)
7024c0c8cfcSFilipe Manana 			split = alloc_extent_map();
7034c0c8cfcSFilipe Manana 		if (!split2)
7044c0c8cfcSFilipe Manana 			split2 = alloc_extent_map();
7054c0c8cfcSFilipe Manana 		if (!split || !split2)
7064c0c8cfcSFilipe Manana 			no_splits = true;
7074c0c8cfcSFilipe Manana 
7084c0c8cfcSFilipe Manana 		write_lock(&em_tree->lock);
7094c0c8cfcSFilipe Manana 		em = lookup_extent_mapping(em_tree, start, len);
7104c0c8cfcSFilipe Manana 		if (!em) {
7114c0c8cfcSFilipe Manana 			write_unlock(&em_tree->lock);
7124c0c8cfcSFilipe Manana 			break;
7134c0c8cfcSFilipe Manana 		}
714*f3109e33SFilipe Manana 		em_end = extent_map_end(em);
715*f3109e33SFilipe Manana 		if (testend && em_end > start + len)
7164c0c8cfcSFilipe Manana 			ends_after_range = true;
7174c0c8cfcSFilipe Manana 		if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
7184c0c8cfcSFilipe Manana 			if (ends_after_range) {
7194c0c8cfcSFilipe Manana 				free_extent_map(em);
7204c0c8cfcSFilipe Manana 				write_unlock(&em_tree->lock);
7214c0c8cfcSFilipe Manana 				break;
7224c0c8cfcSFilipe Manana 			}
723*f3109e33SFilipe Manana 			start = em_end;
7244c0c8cfcSFilipe Manana 			if (testend)
725*f3109e33SFilipe Manana 				len = start + len - em_end;
7264c0c8cfcSFilipe Manana 			free_extent_map(em);
7274c0c8cfcSFilipe Manana 			write_unlock(&em_tree->lock);
7284c0c8cfcSFilipe Manana 			continue;
7294c0c8cfcSFilipe Manana 		}
7304c0c8cfcSFilipe Manana 		flags = em->flags;
7314c0c8cfcSFilipe Manana 		gen = em->generation;
7324c0c8cfcSFilipe Manana 		compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7334c0c8cfcSFilipe Manana 		clear_bit(EXTENT_FLAG_PINNED, &em->flags);
7344c0c8cfcSFilipe Manana 		clear_bit(EXTENT_FLAG_LOGGING, &flags);
7354c0c8cfcSFilipe Manana 		modified = !list_empty(&em->list);
7364c0c8cfcSFilipe Manana 		if (no_splits)
7374c0c8cfcSFilipe Manana 			goto next;
7384c0c8cfcSFilipe Manana 
7394c0c8cfcSFilipe Manana 		if (em->start < start) {
7404c0c8cfcSFilipe Manana 			split->start = em->start;
7414c0c8cfcSFilipe Manana 			split->len = start - em->start;
7424c0c8cfcSFilipe Manana 
7434c0c8cfcSFilipe Manana 			if (em->block_start < EXTENT_MAP_LAST_BYTE) {
7444c0c8cfcSFilipe Manana 				split->orig_start = em->orig_start;
7454c0c8cfcSFilipe Manana 				split->block_start = em->block_start;
7464c0c8cfcSFilipe Manana 
7474c0c8cfcSFilipe Manana 				if (compressed)
7484c0c8cfcSFilipe Manana 					split->block_len = em->block_len;
7494c0c8cfcSFilipe Manana 				else
7504c0c8cfcSFilipe Manana 					split->block_len = split->len;
7514c0c8cfcSFilipe Manana 				split->orig_block_len = max(split->block_len,
7524c0c8cfcSFilipe Manana 						em->orig_block_len);
7534c0c8cfcSFilipe Manana 				split->ram_bytes = em->ram_bytes;
7544c0c8cfcSFilipe Manana 			} else {
7554c0c8cfcSFilipe Manana 				split->orig_start = split->start;
7564c0c8cfcSFilipe Manana 				split->block_len = 0;
7574c0c8cfcSFilipe Manana 				split->block_start = em->block_start;
7584c0c8cfcSFilipe Manana 				split->orig_block_len = 0;
7594c0c8cfcSFilipe Manana 				split->ram_bytes = split->len;
7604c0c8cfcSFilipe Manana 			}
7614c0c8cfcSFilipe Manana 
7624c0c8cfcSFilipe Manana 			split->generation = gen;
7634c0c8cfcSFilipe Manana 			split->flags = flags;
7644c0c8cfcSFilipe Manana 			split->compress_type = em->compress_type;
7654c0c8cfcSFilipe Manana 			replace_extent_mapping(em_tree, em, split, modified);
7664c0c8cfcSFilipe Manana 			free_extent_map(split);
7674c0c8cfcSFilipe Manana 			split = split2;
7684c0c8cfcSFilipe Manana 			split2 = NULL;
7694c0c8cfcSFilipe Manana 		}
7704c0c8cfcSFilipe Manana 		if (ends_after_range) {
7714c0c8cfcSFilipe Manana 			split->start = start + len;
772*f3109e33SFilipe Manana 			split->len = em_end - (start + len);
7734c0c8cfcSFilipe Manana 			split->block_start = em->block_start;
7744c0c8cfcSFilipe Manana 			split->flags = flags;
7754c0c8cfcSFilipe Manana 			split->compress_type = em->compress_type;
7764c0c8cfcSFilipe Manana 			split->generation = gen;
7774c0c8cfcSFilipe Manana 
7784c0c8cfcSFilipe Manana 			if (em->block_start < EXTENT_MAP_LAST_BYTE) {
7794c0c8cfcSFilipe Manana 				split->orig_block_len = max(em->block_len,
7804c0c8cfcSFilipe Manana 						    em->orig_block_len);
7814c0c8cfcSFilipe Manana 
7824c0c8cfcSFilipe Manana 				split->ram_bytes = em->ram_bytes;
7834c0c8cfcSFilipe Manana 				if (compressed) {
7844c0c8cfcSFilipe Manana 					split->block_len = em->block_len;
7854c0c8cfcSFilipe Manana 					split->orig_start = em->orig_start;
7864c0c8cfcSFilipe Manana 				} else {
7874c0c8cfcSFilipe Manana 					const u64 diff = start + len - em->start;
7884c0c8cfcSFilipe Manana 
7894c0c8cfcSFilipe Manana 					split->block_len = split->len;
7904c0c8cfcSFilipe Manana 					split->block_start += diff;
7914c0c8cfcSFilipe Manana 					split->orig_start = em->orig_start;
7924c0c8cfcSFilipe Manana 				}
7934c0c8cfcSFilipe Manana 			} else {
7944c0c8cfcSFilipe Manana 				split->ram_bytes = split->len;
7954c0c8cfcSFilipe Manana 				split->orig_start = split->start;
7964c0c8cfcSFilipe Manana 				split->block_len = 0;
7974c0c8cfcSFilipe Manana 				split->orig_block_len = 0;
7984c0c8cfcSFilipe Manana 			}
7994c0c8cfcSFilipe Manana 
8004c0c8cfcSFilipe Manana 			if (extent_map_in_tree(em)) {
8014c0c8cfcSFilipe Manana 				replace_extent_mapping(em_tree, em, split,
8024c0c8cfcSFilipe Manana 						       modified);
8034c0c8cfcSFilipe Manana 			} else {
8044c0c8cfcSFilipe Manana 				int ret;
8054c0c8cfcSFilipe Manana 
8064c0c8cfcSFilipe Manana 				ret = add_extent_mapping(em_tree, split,
8074c0c8cfcSFilipe Manana 							 modified);
8084c0c8cfcSFilipe Manana 				/* Logic error, shouldn't happen. */
8094c0c8cfcSFilipe Manana 				ASSERT(ret == 0);
8104c0c8cfcSFilipe Manana 				if (WARN_ON(ret != 0) && modified)
8114c0c8cfcSFilipe Manana 					btrfs_set_inode_full_sync(inode);
8124c0c8cfcSFilipe Manana 			}
8134c0c8cfcSFilipe Manana 			free_extent_map(split);
8144c0c8cfcSFilipe Manana 			split = NULL;
8154c0c8cfcSFilipe Manana 		}
8164c0c8cfcSFilipe Manana next:
8174c0c8cfcSFilipe Manana 		if (extent_map_in_tree(em)) {
8184c0c8cfcSFilipe Manana 			/*
8194c0c8cfcSFilipe Manana 			 * If the extent map is still in the tree it means that
8204c0c8cfcSFilipe Manana 			 * either of the following is true:
8214c0c8cfcSFilipe Manana 			 *
8224c0c8cfcSFilipe Manana 			 * 1) It fits entirely in our range (doesn't end beyond
8234c0c8cfcSFilipe Manana 			 *    it or starts before it);
8244c0c8cfcSFilipe Manana 			 *
8254c0c8cfcSFilipe Manana 			 * 2) It starts before our range and/or ends after our
8264c0c8cfcSFilipe Manana 			 *    range, and we were not able to allocate the extent
8274c0c8cfcSFilipe Manana 			 *    maps for split operations, @split and @split2.
8284c0c8cfcSFilipe Manana 			 *
8294c0c8cfcSFilipe Manana 			 * If we are at case 2) then we just remove the entire
8304c0c8cfcSFilipe Manana 			 * extent map - this is fine since if anyone needs it to
8314c0c8cfcSFilipe Manana 			 * access the subranges outside our range, will just
8324c0c8cfcSFilipe Manana 			 * load it again from the subvolume tree's file extent
8334c0c8cfcSFilipe Manana 			 * item. However if the extent map was in the list of
8344c0c8cfcSFilipe Manana 			 * modified extents, then we must mark the inode for a
8354c0c8cfcSFilipe Manana 			 * full fsync, otherwise a fast fsync will miss this
8364c0c8cfcSFilipe Manana 			 * extent if it's new and needs to be logged.
8374c0c8cfcSFilipe Manana 			 */
8384c0c8cfcSFilipe Manana 			if ((em->start < start || ends_after_range) && modified) {
8394c0c8cfcSFilipe Manana 				ASSERT(no_splits);
8404c0c8cfcSFilipe Manana 				btrfs_set_inode_full_sync(inode);
8414c0c8cfcSFilipe Manana 			}
8424c0c8cfcSFilipe Manana 			remove_extent_mapping(em_tree, em);
8434c0c8cfcSFilipe Manana 		}
8444c0c8cfcSFilipe Manana 		write_unlock(&em_tree->lock);
8454c0c8cfcSFilipe Manana 
8464c0c8cfcSFilipe Manana 		/* Once for us. */
8474c0c8cfcSFilipe Manana 		free_extent_map(em);
8484c0c8cfcSFilipe Manana 		/* And once for the tree. */
8494c0c8cfcSFilipe Manana 		free_extent_map(em);
8504c0c8cfcSFilipe Manana 	}
8514c0c8cfcSFilipe Manana 
8524c0c8cfcSFilipe Manana 	free_extent_map(split);
8534c0c8cfcSFilipe Manana 	free_extent_map(split2);
8544c0c8cfcSFilipe Manana }
855