xref: /openbmc/linux/fs/btrfs/extent_map.c (revision 8811133d8a982d3cef5d25eef54a8dca9e8e6ded)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2c1d7c514SDavid Sterba 
3d1310b2eSChris Mason #include <linux/err.h>
4d1310b2eSChris Mason #include <linux/slab.h>
5a52d9a80SChris Mason #include <linux/spinlock.h>
6261507a0SLi Zefan #include "ctree.h"
71c11b63eSJeff Mahoney #include "volumes.h"
8a52d9a80SChris Mason #include "extent_map.h"
9ebb8765bSAnand Jain #include "compression.h"
10a52d9a80SChris Mason 
1186479a04SChris Mason 
12a52d9a80SChris Mason static struct kmem_cache *extent_map_cache;
13ca664626SChris Mason 
142f4cbe64SWyatt Banks int __init extent_map_init(void)
15a52d9a80SChris Mason {
16837e1972SDavid Sterba 	extent_map_cache = kmem_cache_create("btrfs_extent_map",
176d36dcd4SChris Mason 			sizeof(struct extent_map), 0,
18fba4b697SNikolay Borisov 			SLAB_MEM_SPREAD, NULL);
192f4cbe64SWyatt Banks 	if (!extent_map_cache)
202f4cbe64SWyatt Banks 		return -ENOMEM;
212f4cbe64SWyatt Banks 	return 0;
22a52d9a80SChris Mason }
23a52d9a80SChris Mason 
24e67c718bSDavid Sterba void __cold extent_map_exit(void)
25a52d9a80SChris Mason {
26a52d9a80SChris Mason 	kmem_cache_destroy(extent_map_cache);
27a52d9a80SChris Mason }
28a52d9a80SChris Mason 
299d2423c5SChristoph Hellwig /**
309d2423c5SChristoph Hellwig  * extent_map_tree_init - initialize extent map tree
319d2423c5SChristoph Hellwig  * @tree:		tree to initialize
329d2423c5SChristoph Hellwig  *
339d2423c5SChristoph Hellwig  * Initialize the extent tree @tree.  Should be called for each new inode
349d2423c5SChristoph Hellwig  * or other user of the extent_map interface.
359d2423c5SChristoph Hellwig  */
36a8067e02SDavid Sterba void extent_map_tree_init(struct extent_map_tree *tree)
37a52d9a80SChris Mason {
3807e1ce09SLiu Bo 	tree->map = RB_ROOT_CACHED;
395dc562c5SJosef Bacik 	INIT_LIST_HEAD(&tree->modified_extents);
40890871beSChris Mason 	rwlock_init(&tree->lock);
41a52d9a80SChris Mason }
42a52d9a80SChris Mason 
439d2423c5SChristoph Hellwig /**
449d2423c5SChristoph Hellwig  * alloc_extent_map - allocate new extent map structure
459d2423c5SChristoph Hellwig  *
469d2423c5SChristoph Hellwig  * Allocate a new extent_map structure.  The new structure is
479d2423c5SChristoph Hellwig  * returned with a reference count of one and needs to be
489d2423c5SChristoph Hellwig  * freed using free_extent_map()
499d2423c5SChristoph Hellwig  */
50172ddd60SDavid Sterba struct extent_map *alloc_extent_map(void)
51a52d9a80SChris Mason {
52a52d9a80SChris Mason 	struct extent_map *em;
5370c8a91cSJosef Bacik 	em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS);
54c26a9203STsutomu Itoh 	if (!em)
55c26a9203STsutomu Itoh 		return NULL;
56cbc0e928SFilipe Manana 	RB_CLEAR_NODE(&em->rb_node);
57d1310b2eSChris Mason 	em->flags = 0;
58261507a0SLi Zefan 	em->compress_type = BTRFS_COMPRESS_NONE;
595dc562c5SJosef Bacik 	em->generation = 0;
60490b54d6SElena Reshetova 	refcount_set(&em->refs, 1);
615dc562c5SJosef Bacik 	INIT_LIST_HEAD(&em->list);
62a52d9a80SChris Mason 	return em;
63a52d9a80SChris Mason }
64a52d9a80SChris Mason 
659d2423c5SChristoph Hellwig /**
669d2423c5SChristoph Hellwig  * free_extent_map - drop reference count of an extent_map
6701327610SNicholas D Steeves  * @em:		extent map being released
689d2423c5SChristoph Hellwig  *
699d2423c5SChristoph Hellwig  * Drops the reference out on @em by one and free the structure
709d2423c5SChristoph Hellwig  * if the reference count hits zero.
719d2423c5SChristoph Hellwig  */
72a52d9a80SChris Mason void free_extent_map(struct extent_map *em)
73a52d9a80SChris Mason {
742bf5a725SChris Mason 	if (!em)
752bf5a725SChris Mason 		return;
76490b54d6SElena Reshetova 	WARN_ON(refcount_read(&em->refs) == 0);
77490b54d6SElena Reshetova 	if (refcount_dec_and_test(&em->refs)) {
78cbc0e928SFilipe Manana 		WARN_ON(extent_map_in_tree(em));
795dc562c5SJosef Bacik 		WARN_ON(!list_empty(&em->list));
80298a8f9cSWang Shilong 		if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
8195617d69SJeff Mahoney 			kfree(em->map_lookup);
82a52d9a80SChris Mason 		kmem_cache_free(extent_map_cache, em);
83a52d9a80SChris Mason 	}
84a52d9a80SChris Mason }
85a52d9a80SChris Mason 
8632193c14SFilipe David Borba Manana /* simple helper to do math around the end of an extent, handling wrap */
8732193c14SFilipe David Borba Manana static u64 range_end(u64 start, u64 len)
8832193c14SFilipe David Borba Manana {
8932193c14SFilipe David Borba Manana 	if (start + len < start)
9032193c14SFilipe David Borba Manana 		return (u64)-1;
9132193c14SFilipe David Borba Manana 	return start + len;
9232193c14SFilipe David Borba Manana }
9332193c14SFilipe David Borba Manana 
9407e1ce09SLiu Bo static int tree_insert(struct rb_root_cached *root, struct extent_map *em)
95a52d9a80SChris Mason {
9607e1ce09SLiu Bo 	struct rb_node **p = &root->rb_root.rb_node;
97a52d9a80SChris Mason 	struct rb_node *parent = NULL;
9832193c14SFilipe David Borba Manana 	struct extent_map *entry = NULL;
9932193c14SFilipe David Borba Manana 	struct rb_node *orig_parent = NULL;
10032193c14SFilipe David Borba Manana 	u64 end = range_end(em->start, em->len);
10107e1ce09SLiu Bo 	bool leftmost = true;
102a52d9a80SChris Mason 
103a52d9a80SChris Mason 	while (*p) {
104a52d9a80SChris Mason 		parent = *p;
105d1310b2eSChris Mason 		entry = rb_entry(parent, struct extent_map, rb_node);
106d1310b2eSChris Mason 
10707e1ce09SLiu Bo 		if (em->start < entry->start) {
108a52d9a80SChris Mason 			p = &(*p)->rb_left;
10907e1ce09SLiu Bo 		} else if (em->start >= extent_map_end(entry)) {
110a52d9a80SChris Mason 			p = &(*p)->rb_right;
11107e1ce09SLiu Bo 			leftmost = false;
11207e1ce09SLiu Bo 		} else {
11332193c14SFilipe David Borba Manana 			return -EEXIST;
114a52d9a80SChris Mason 		}
11507e1ce09SLiu Bo 	}
116a52d9a80SChris Mason 
11732193c14SFilipe David Borba Manana 	orig_parent = parent;
11832193c14SFilipe David Borba Manana 	while (parent && em->start >= extent_map_end(entry)) {
11932193c14SFilipe David Borba Manana 		parent = rb_next(parent);
12032193c14SFilipe David Borba Manana 		entry = rb_entry(parent, struct extent_map, rb_node);
12132193c14SFilipe David Borba Manana 	}
12232193c14SFilipe David Borba Manana 	if (parent)
12332193c14SFilipe David Borba Manana 		if (end > entry->start && em->start < extent_map_end(entry))
12432193c14SFilipe David Borba Manana 			return -EEXIST;
12532193c14SFilipe David Borba Manana 
12632193c14SFilipe David Borba Manana 	parent = orig_parent;
12732193c14SFilipe David Borba Manana 	entry = rb_entry(parent, struct extent_map, rb_node);
12832193c14SFilipe David Borba Manana 	while (parent && em->start < entry->start) {
12932193c14SFilipe David Borba Manana 		parent = rb_prev(parent);
13032193c14SFilipe David Borba Manana 		entry = rb_entry(parent, struct extent_map, rb_node);
13132193c14SFilipe David Borba Manana 	}
13232193c14SFilipe David Borba Manana 	if (parent)
13332193c14SFilipe David Borba Manana 		if (end > entry->start && em->start < extent_map_end(entry))
13432193c14SFilipe David Borba Manana 			return -EEXIST;
13532193c14SFilipe David Borba Manana 
13632193c14SFilipe David Borba Manana 	rb_link_node(&em->rb_node, orig_parent, p);
13707e1ce09SLiu Bo 	rb_insert_color_cached(&em->rb_node, root, leftmost);
13832193c14SFilipe David Borba Manana 	return 0;
139a52d9a80SChris Mason }
140a52d9a80SChris Mason 
141d352ac68SChris Mason /*
142d352ac68SChris Mason  * search through the tree for an extent_map with a given offset.  If
143d352ac68SChris Mason  * it can't be found, try to find some neighboring extents
144d352ac68SChris Mason  */
145a52d9a80SChris Mason static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
1465f56406aSChris Mason 				     struct rb_node **prev_ret,
1475f56406aSChris Mason 				     struct rb_node **next_ret)
148a52d9a80SChris Mason {
149a52d9a80SChris Mason 	struct rb_node *n = root->rb_node;
150a52d9a80SChris Mason 	struct rb_node *prev = NULL;
1515f56406aSChris Mason 	struct rb_node *orig_prev = NULL;
152d1310b2eSChris Mason 	struct extent_map *entry;
153d1310b2eSChris Mason 	struct extent_map *prev_entry = NULL;
154a52d9a80SChris Mason 
155a52d9a80SChris Mason 	while (n) {
156d1310b2eSChris Mason 		entry = rb_entry(n, struct extent_map, rb_node);
157a52d9a80SChris Mason 		prev = n;
158a52d9a80SChris Mason 		prev_entry = entry;
159a52d9a80SChris Mason 
160a52d9a80SChris Mason 		if (offset < entry->start)
161a52d9a80SChris Mason 			n = n->rb_left;
162d1310b2eSChris Mason 		else if (offset >= extent_map_end(entry))
163a52d9a80SChris Mason 			n = n->rb_right;
164a52d9a80SChris Mason 		else
165a52d9a80SChris Mason 			return n;
166a52d9a80SChris Mason 	}
1675f56406aSChris Mason 
1685f56406aSChris Mason 	if (prev_ret) {
1695f56406aSChris Mason 		orig_prev = prev;
170d1310b2eSChris Mason 		while (prev && offset >= extent_map_end(prev_entry)) {
171a52d9a80SChris Mason 			prev = rb_next(prev);
172d1310b2eSChris Mason 			prev_entry = rb_entry(prev, struct extent_map, rb_node);
173a52d9a80SChris Mason 		}
174a52d9a80SChris Mason 		*prev_ret = prev;
1755f56406aSChris Mason 		prev = orig_prev;
1765f56406aSChris Mason 	}
1775f56406aSChris Mason 
1785f56406aSChris Mason 	if (next_ret) {
179d1310b2eSChris Mason 		prev_entry = rb_entry(prev, struct extent_map, rb_node);
1805f56406aSChris Mason 		while (prev && offset < prev_entry->start) {
1815f56406aSChris Mason 			prev = rb_prev(prev);
182d1310b2eSChris Mason 			prev_entry = rb_entry(prev, struct extent_map, rb_node);
1835f56406aSChris Mason 		}
1845f56406aSChris Mason 		*next_ret = prev;
1855f56406aSChris Mason 	}
186a52d9a80SChris Mason 	return NULL;
187a52d9a80SChris Mason }
188a52d9a80SChris Mason 
189d352ac68SChris Mason /* check to see if two extent_map structs are adjacent and safe to merge */
190d1310b2eSChris Mason static int mergable_maps(struct extent_map *prev, struct extent_map *next)
191a52d9a80SChris Mason {
1927f3c74fbSChris Mason 	if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
1937f3c74fbSChris Mason 		return 0;
1947f3c74fbSChris Mason 
195c8b97818SChris Mason 	/*
196c8b97818SChris Mason 	 * don't merge compressed extents, we need to know their
197c8b97818SChris Mason 	 * actual size
198c8b97818SChris Mason 	 */
199c8b97818SChris Mason 	if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
200c8b97818SChris Mason 		return 0;
201c8b97818SChris Mason 
202201a9038SJosef Bacik 	if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
203201a9038SJosef Bacik 	    test_bit(EXTENT_FLAG_LOGGING, &next->flags))
204201a9038SJosef Bacik 		return 0;
205201a9038SJosef Bacik 
20609a2a8f9SJosef Bacik 	/*
20709a2a8f9SJosef Bacik 	 * We don't want to merge stuff that hasn't been written to the log yet
20809a2a8f9SJosef Bacik 	 * since it may not reflect exactly what is on disk, and that would be
20909a2a8f9SJosef Bacik 	 * bad.
21009a2a8f9SJosef Bacik 	 */
21109a2a8f9SJosef Bacik 	if (!list_empty(&prev->list) || !list_empty(&next->list))
21209a2a8f9SJosef Bacik 		return 0;
21309a2a8f9SJosef Bacik 
214951e05a9SNikolay Borisov 	ASSERT(next->block_start != EXTENT_MAP_DELALLOC &&
215951e05a9SNikolay Borisov 	       prev->block_start != EXTENT_MAP_DELALLOC);
216951e05a9SNikolay Borisov 
217d1310b2eSChris Mason 	if (extent_map_end(prev) == next->start &&
218d1310b2eSChris Mason 	    prev->flags == next->flags &&
219d1310b2eSChris Mason 	    prev->bdev == next->bdev &&
220d1310b2eSChris Mason 	    ((next->block_start == EXTENT_MAP_HOLE &&
221d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_HOLE) ||
222d1310b2eSChris Mason 	     (next->block_start == EXTENT_MAP_INLINE &&
223d1310b2eSChris Mason 	      prev->block_start == EXTENT_MAP_INLINE) ||
224d1310b2eSChris Mason 	     (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
225d1310b2eSChris Mason 	      next->block_start == extent_map_block_end(prev)))) {
226d1310b2eSChris Mason 		return 1;
227d1310b2eSChris Mason 	}
228a52d9a80SChris Mason 	return 0;
229a52d9a80SChris Mason }
230a52d9a80SChris Mason 
2314d2c8f62SLi Zefan static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
232a1ed835eSChris Mason {
233a1ed835eSChris Mason 	struct extent_map *merge = NULL;
234a1ed835eSChris Mason 	struct rb_node *rb;
235a1ed835eSChris Mason 
236a1ed835eSChris Mason 	if (em->start != 0) {
237a1ed835eSChris Mason 		rb = rb_prev(&em->rb_node);
238a1ed835eSChris Mason 		if (rb)
239a1ed835eSChris Mason 			merge = rb_entry(rb, struct extent_map, rb_node);
240a1ed835eSChris Mason 		if (rb && mergable_maps(merge, em)) {
241a1ed835eSChris Mason 			em->start = merge->start;
24270c8a91cSJosef Bacik 			em->orig_start = merge->orig_start;
243a1ed835eSChris Mason 			em->len += merge->len;
244a1ed835eSChris Mason 			em->block_len += merge->block_len;
245a1ed835eSChris Mason 			em->block_start = merge->block_start;
24670c8a91cSJosef Bacik 			em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
24770c8a91cSJosef Bacik 			em->mod_start = merge->mod_start;
24870c8a91cSJosef Bacik 			em->generation = max(em->generation, merge->generation);
2495dc562c5SJosef Bacik 
25007e1ce09SLiu Bo 			rb_erase_cached(&merge->rb_node, &tree->map);
251cbc0e928SFilipe Manana 			RB_CLEAR_NODE(&merge->rb_node);
252a1ed835eSChris Mason 			free_extent_map(merge);
253a1ed835eSChris Mason 		}
254a1ed835eSChris Mason 	}
255a1ed835eSChris Mason 
256a1ed835eSChris Mason 	rb = rb_next(&em->rb_node);
257a1ed835eSChris Mason 	if (rb)
258a1ed835eSChris Mason 		merge = rb_entry(rb, struct extent_map, rb_node);
259a1ed835eSChris Mason 	if (rb && mergable_maps(em, merge)) {
260a1ed835eSChris Mason 		em->len += merge->len;
261d527afe1SFilipe David Borba Manana 		em->block_len += merge->block_len;
26207e1ce09SLiu Bo 		rb_erase_cached(&merge->rb_node, &tree->map);
263cbc0e928SFilipe Manana 		RB_CLEAR_NODE(&merge->rb_node);
26470c8a91cSJosef Bacik 		em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
26570c8a91cSJosef Bacik 		em->generation = max(em->generation, merge->generation);
266a1ed835eSChris Mason 		free_extent_map(merge);
267a1ed835eSChris Mason 	}
2684d2c8f62SLi Zefan }
2694d2c8f62SLi Zefan 
2705dc562c5SJosef Bacik /**
27152b1de91SLiu Bo  * unpin_extent_cache - unpin an extent from the cache
2725dc562c5SJosef Bacik  * @tree:	tree to unpin the extent in
2735dc562c5SJosef Bacik  * @start:	logical offset in the file
2745dc562c5SJosef Bacik  * @len:	length of the extent
2755dc562c5SJosef Bacik  * @gen:	generation that this extent has been modified in
2765dc562c5SJosef Bacik  *
2775dc562c5SJosef Bacik  * Called after an extent has been written to disk properly.  Set the generation
2785dc562c5SJosef Bacik  * to the generation that actually added the file item to the inode so we know
2795dc562c5SJosef Bacik  * we need to sync this extent when we call fsync().
2805dc562c5SJosef Bacik  */
2815dc562c5SJosef Bacik int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
2825dc562c5SJosef Bacik 		       u64 gen)
2834d2c8f62SLi Zefan {
2844d2c8f62SLi Zefan 	int ret = 0;
2854d2c8f62SLi Zefan 	struct extent_map *em;
2864e2f84e6SLiu Bo 	bool prealloc = false;
2874d2c8f62SLi Zefan 
2884d2c8f62SLi Zefan 	write_lock(&tree->lock);
2894d2c8f62SLi Zefan 	em = lookup_extent_mapping(tree, start, len);
2904d2c8f62SLi Zefan 
2914d2c8f62SLi Zefan 	WARN_ON(!em || em->start != start);
2924d2c8f62SLi Zefan 
2934d2c8f62SLi Zefan 	if (!em)
2944d2c8f62SLi Zefan 		goto out;
2954d2c8f62SLi Zefan 
2965dc562c5SJosef Bacik 	em->generation = gen;
2974d2c8f62SLi Zefan 	clear_bit(EXTENT_FLAG_PINNED, &em->flags);
2984e2f84e6SLiu Bo 	em->mod_start = em->start;
2994e2f84e6SLiu Bo 	em->mod_len = em->len;
3004e2f84e6SLiu Bo 
301b11e234dSJosef Bacik 	if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
3024e2f84e6SLiu Bo 		prealloc = true;
303b11e234dSJosef Bacik 		clear_bit(EXTENT_FLAG_FILLING, &em->flags);
3044e2f84e6SLiu Bo 	}
3054d2c8f62SLi Zefan 
3064d2c8f62SLi Zefan 	try_merge_map(tree, em);
3074e2f84e6SLiu Bo 
3084e2f84e6SLiu Bo 	if (prealloc) {
3094e2f84e6SLiu Bo 		em->mod_start = em->start;
3104e2f84e6SLiu Bo 		em->mod_len = em->len;
3114e2f84e6SLiu Bo 	}
3124e2f84e6SLiu Bo 
313a1ed835eSChris Mason 	free_extent_map(em);
314a1ed835eSChris Mason out:
315a1ed835eSChris Mason 	write_unlock(&tree->lock);
316a1ed835eSChris Mason 	return ret;
317a1ed835eSChris Mason 
318a1ed835eSChris Mason }
319a1ed835eSChris Mason 
320201a9038SJosef Bacik void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
321201a9038SJosef Bacik {
322201a9038SJosef Bacik 	clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
323cbc0e928SFilipe Manana 	if (extent_map_in_tree(em))
324201a9038SJosef Bacik 		try_merge_map(tree, em);
325201a9038SJosef Bacik }
326201a9038SJosef Bacik 
327176840b3SFilipe Manana static inline void setup_extent_mapping(struct extent_map_tree *tree,
328176840b3SFilipe Manana 					struct extent_map *em,
329176840b3SFilipe Manana 					int modified)
330176840b3SFilipe Manana {
331490b54d6SElena Reshetova 	refcount_inc(&em->refs);
332176840b3SFilipe Manana 	em->mod_start = em->start;
333176840b3SFilipe Manana 	em->mod_len = em->len;
334176840b3SFilipe Manana 
335176840b3SFilipe Manana 	if (modified)
336176840b3SFilipe Manana 		list_move(&em->list, &tree->modified_extents);
337176840b3SFilipe Manana 	else
338176840b3SFilipe Manana 		try_merge_map(tree, em);
339176840b3SFilipe Manana }
340176840b3SFilipe Manana 
3411c11b63eSJeff Mahoney static void extent_map_device_set_bits(struct extent_map *em, unsigned bits)
3421c11b63eSJeff Mahoney {
3431c11b63eSJeff Mahoney 	struct map_lookup *map = em->map_lookup;
3441c11b63eSJeff Mahoney 	u64 stripe_size = em->orig_block_len;
3451c11b63eSJeff Mahoney 	int i;
3461c11b63eSJeff Mahoney 
3471c11b63eSJeff Mahoney 	for (i = 0; i < map->num_stripes; i++) {
3481c11b63eSJeff Mahoney 		struct btrfs_bio_stripe *stripe = &map->stripes[i];
3491c11b63eSJeff Mahoney 		struct btrfs_device *device = stripe->dev;
3501c11b63eSJeff Mahoney 
3511c11b63eSJeff Mahoney 		set_extent_bits_nowait(&device->alloc_state, stripe->physical,
3521c11b63eSJeff Mahoney 				 stripe->physical + stripe_size - 1, bits);
3531c11b63eSJeff Mahoney 	}
3541c11b63eSJeff Mahoney }
3551c11b63eSJeff Mahoney 
3561c11b63eSJeff Mahoney static void extent_map_device_clear_bits(struct extent_map *em, unsigned bits)
3571c11b63eSJeff Mahoney {
3581c11b63eSJeff Mahoney 	struct map_lookup *map = em->map_lookup;
3591c11b63eSJeff Mahoney 	u64 stripe_size = em->orig_block_len;
3601c11b63eSJeff Mahoney 	int i;
3611c11b63eSJeff Mahoney 
3621c11b63eSJeff Mahoney 	for (i = 0; i < map->num_stripes; i++) {
3631c11b63eSJeff Mahoney 		struct btrfs_bio_stripe *stripe = &map->stripes[i];
3641c11b63eSJeff Mahoney 		struct btrfs_device *device = stripe->dev;
3651c11b63eSJeff Mahoney 
3661c11b63eSJeff Mahoney 		__clear_extent_bit(&device->alloc_state, stripe->physical,
3671c11b63eSJeff Mahoney 				   stripe->physical + stripe_size - 1, bits,
3681c11b63eSJeff Mahoney 				   0, 0, NULL, GFP_NOWAIT, NULL);
3691c11b63eSJeff Mahoney 	}
3701c11b63eSJeff Mahoney }
3711c11b63eSJeff Mahoney 
3729d2423c5SChristoph Hellwig /**
3739d2423c5SChristoph Hellwig  * add_extent_mapping - add new extent map to the extent tree
3749d2423c5SChristoph Hellwig  * @tree:	tree to insert new map in
3759d2423c5SChristoph Hellwig  * @em:		map to insert
3769d2423c5SChristoph Hellwig  *
3779d2423c5SChristoph Hellwig  * Insert @em into @tree or perform a simple forward/backward merge with
3789d2423c5SChristoph Hellwig  * existing mappings.  The extent_map struct passed in will be inserted
3799d2423c5SChristoph Hellwig  * into the tree directly, with an additional reference taken, or a
38025985edcSLucas De Marchi  * reference dropped if the merge attempt was successful.
381a52d9a80SChris Mason  */
382a52d9a80SChris Mason int add_extent_mapping(struct extent_map_tree *tree,
38309a2a8f9SJosef Bacik 		       struct extent_map *em, int modified)
384a52d9a80SChris Mason {
385a52d9a80SChris Mason 	int ret = 0;
386a52d9a80SChris Mason 
38732193c14SFilipe David Borba Manana 	ret = tree_insert(&tree->map, em);
38832193c14SFilipe David Borba Manana 	if (ret)
3897c2fe32aSChris Mason 		goto out;
39032193c14SFilipe David Borba Manana 
391176840b3SFilipe Manana 	setup_extent_mapping(tree, em, modified);
392*8811133dSNikolay Borisov 	if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags)) {
3931c11b63eSJeff Mahoney 		extent_map_device_set_bits(em, CHUNK_ALLOCATED);
394*8811133dSNikolay Borisov 		extent_map_device_clear_bits(em, CHUNK_TRIMMED);
395*8811133dSNikolay Borisov 	}
396a52d9a80SChris Mason out:
397a52d9a80SChris Mason 	return ret;
398a52d9a80SChris Mason }
399a52d9a80SChris Mason 
40048a3b636SEric Sandeen static struct extent_map *
40148a3b636SEric Sandeen __lookup_extent_mapping(struct extent_map_tree *tree,
402ed64f066SLi Zefan 			u64 start, u64 len, int strict)
403ed64f066SLi Zefan {
404ed64f066SLi Zefan 	struct extent_map *em;
405ed64f066SLi Zefan 	struct rb_node *rb_node;
406ed64f066SLi Zefan 	struct rb_node *prev = NULL;
407ed64f066SLi Zefan 	struct rb_node *next = NULL;
408ed64f066SLi Zefan 	u64 end = range_end(start, len);
409ed64f066SLi Zefan 
41007e1ce09SLiu Bo 	rb_node = __tree_search(&tree->map.rb_root, start, &prev, &next);
411ed64f066SLi Zefan 	if (!rb_node) {
412ed64f066SLi Zefan 		if (prev)
413ed64f066SLi Zefan 			rb_node = prev;
414ed64f066SLi Zefan 		else if (next)
415ed64f066SLi Zefan 			rb_node = next;
416ed64f066SLi Zefan 		else
417ed64f066SLi Zefan 			return NULL;
418ed64f066SLi Zefan 	}
419ed64f066SLi Zefan 
420ed64f066SLi Zefan 	em = rb_entry(rb_node, struct extent_map, rb_node);
421ed64f066SLi Zefan 
422ed64f066SLi Zefan 	if (strict && !(end > em->start && start < extent_map_end(em)))
423ed64f066SLi Zefan 		return NULL;
424ed64f066SLi Zefan 
425490b54d6SElena Reshetova 	refcount_inc(&em->refs);
426ed64f066SLi Zefan 	return em;
427ed64f066SLi Zefan }
428ed64f066SLi Zefan 
4299d2423c5SChristoph Hellwig /**
4309d2423c5SChristoph Hellwig  * lookup_extent_mapping - lookup extent_map
4319d2423c5SChristoph Hellwig  * @tree:	tree to lookup in
4329d2423c5SChristoph Hellwig  * @start:	byte offset to start the search
4339d2423c5SChristoph Hellwig  * @len:	length of the lookup range
4349d2423c5SChristoph Hellwig  *
4359d2423c5SChristoph Hellwig  * Find and return the first extent_map struct in @tree that intersects the
4369d2423c5SChristoph Hellwig  * [start, len] range.  There may be additional objects in the tree that
4379d2423c5SChristoph Hellwig  * intersect, so check the object returned carefully to make sure that no
4389d2423c5SChristoph Hellwig  * additional lookups are needed.
439a52d9a80SChris Mason  */
440a52d9a80SChris Mason struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
441d1310b2eSChris Mason 					 u64 start, u64 len)
442a52d9a80SChris Mason {
443ed64f066SLi Zefan 	return __lookup_extent_mapping(tree, start, len, 1);
444a52d9a80SChris Mason }
445a52d9a80SChris Mason 
4469d2423c5SChristoph Hellwig /**
447b917b7c3SChris Mason  * search_extent_mapping - find a nearby extent map
448b917b7c3SChris Mason  * @tree:	tree to lookup in
449b917b7c3SChris Mason  * @start:	byte offset to start the search
450b917b7c3SChris Mason  * @len:	length of the lookup range
451b917b7c3SChris Mason  *
452b917b7c3SChris Mason  * Find and return the first extent_map struct in @tree that intersects the
453b917b7c3SChris Mason  * [start, len] range.
454b917b7c3SChris Mason  *
455b917b7c3SChris Mason  * If one can't be found, any nearby extent may be returned
456b917b7c3SChris Mason  */
457b917b7c3SChris Mason struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
458b917b7c3SChris Mason 					 u64 start, u64 len)
459b917b7c3SChris Mason {
460ed64f066SLi Zefan 	return __lookup_extent_mapping(tree, start, len, 0);
461b917b7c3SChris Mason }
462b917b7c3SChris Mason 
463b917b7c3SChris Mason /**
4649d2423c5SChristoph Hellwig  * remove_extent_mapping - removes an extent_map from the extent tree
4659d2423c5SChristoph Hellwig  * @tree:	extent tree to remove from
466bb7ab3b9SAdam Buchbinder  * @em:		extent map being removed
4679d2423c5SChristoph Hellwig  *
4689d2423c5SChristoph Hellwig  * Removes @em from @tree.  No reference counts are dropped, and no checks
4699d2423c5SChristoph Hellwig  * are done to see if the range is in use
470a52d9a80SChris Mason  */
471c1766dd7Szhong jiang void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
472a52d9a80SChris Mason {
4737f3c74fbSChris Mason 	WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
47407e1ce09SLiu Bo 	rb_erase_cached(&em->rb_node, &tree->map);
475ff44c6e3SJosef Bacik 	if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
4765dc562c5SJosef Bacik 		list_del_init(&em->list);
4771c11b63eSJeff Mahoney 	if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
4781c11b63eSJeff Mahoney 		extent_map_device_clear_bits(em, CHUNK_ALLOCATED);
479cbc0e928SFilipe Manana 	RB_CLEAR_NODE(&em->rb_node);
480a52d9a80SChris Mason }
481176840b3SFilipe Manana 
482176840b3SFilipe Manana void replace_extent_mapping(struct extent_map_tree *tree,
483176840b3SFilipe Manana 			    struct extent_map *cur,
484176840b3SFilipe Manana 			    struct extent_map *new,
485176840b3SFilipe Manana 			    int modified)
486176840b3SFilipe Manana {
487176840b3SFilipe Manana 	WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));
488176840b3SFilipe Manana 	ASSERT(extent_map_in_tree(cur));
489176840b3SFilipe Manana 	if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))
490176840b3SFilipe Manana 		list_del_init(&cur->list);
49107e1ce09SLiu Bo 	rb_replace_node_cached(&cur->rb_node, &new->rb_node, &tree->map);
492176840b3SFilipe Manana 	RB_CLEAR_NODE(&cur->rb_node);
493176840b3SFilipe Manana 
494176840b3SFilipe Manana 	setup_extent_mapping(tree, new, modified);
495176840b3SFilipe Manana }
496c04e61b5SLiu Bo 
497c04e61b5SLiu Bo static struct extent_map *next_extent_map(struct extent_map *em)
498c04e61b5SLiu Bo {
499c04e61b5SLiu Bo 	struct rb_node *next;
500c04e61b5SLiu Bo 
501c04e61b5SLiu Bo 	next = rb_next(&em->rb_node);
502c04e61b5SLiu Bo 	if (!next)
503c04e61b5SLiu Bo 		return NULL;
504c04e61b5SLiu Bo 	return container_of(next, struct extent_map, rb_node);
505c04e61b5SLiu Bo }
506c04e61b5SLiu Bo 
507c04e61b5SLiu Bo static struct extent_map *prev_extent_map(struct extent_map *em)
508c04e61b5SLiu Bo {
509c04e61b5SLiu Bo 	struct rb_node *prev;
510c04e61b5SLiu Bo 
511c04e61b5SLiu Bo 	prev = rb_prev(&em->rb_node);
512c04e61b5SLiu Bo 	if (!prev)
513c04e61b5SLiu Bo 		return NULL;
514c04e61b5SLiu Bo 	return container_of(prev, struct extent_map, rb_node);
515c04e61b5SLiu Bo }
516c04e61b5SLiu Bo 
51752042d8eSAndrea Gelmini /*
51852042d8eSAndrea Gelmini  * Helper for btrfs_get_extent.  Given an existing extent in the tree,
519c04e61b5SLiu Bo  * the existing extent is the nearest extent to map_start,
520c04e61b5SLiu Bo  * and an extent that you want to insert, deal with overlap and insert
521c04e61b5SLiu Bo  * the best fitted new extent into the tree.
522c04e61b5SLiu Bo  */
5235f4791f4SLiu Bo static noinline int merge_extent_mapping(struct extent_map_tree *em_tree,
524c04e61b5SLiu Bo 					 struct extent_map *existing,
525c04e61b5SLiu Bo 					 struct extent_map *em,
526c04e61b5SLiu Bo 					 u64 map_start)
527c04e61b5SLiu Bo {
528c04e61b5SLiu Bo 	struct extent_map *prev;
529c04e61b5SLiu Bo 	struct extent_map *next;
530c04e61b5SLiu Bo 	u64 start;
531c04e61b5SLiu Bo 	u64 end;
532c04e61b5SLiu Bo 	u64 start_diff;
533c04e61b5SLiu Bo 
534c04e61b5SLiu Bo 	BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
535c04e61b5SLiu Bo 
536c04e61b5SLiu Bo 	if (existing->start > map_start) {
537c04e61b5SLiu Bo 		next = existing;
538c04e61b5SLiu Bo 		prev = prev_extent_map(next);
539c04e61b5SLiu Bo 	} else {
540c04e61b5SLiu Bo 		prev = existing;
541c04e61b5SLiu Bo 		next = next_extent_map(prev);
542c04e61b5SLiu Bo 	}
543c04e61b5SLiu Bo 
544c04e61b5SLiu Bo 	start = prev ? extent_map_end(prev) : em->start;
545c04e61b5SLiu Bo 	start = max_t(u64, start, em->start);
546c04e61b5SLiu Bo 	end = next ? next->start : extent_map_end(em);
547c04e61b5SLiu Bo 	end = min_t(u64, end, extent_map_end(em));
548c04e61b5SLiu Bo 	start_diff = start - em->start;
549c04e61b5SLiu Bo 	em->start = start;
550c04e61b5SLiu Bo 	em->len = end - start;
551c04e61b5SLiu Bo 	if (em->block_start < EXTENT_MAP_LAST_BYTE &&
552c04e61b5SLiu Bo 	    !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
553c04e61b5SLiu Bo 		em->block_start += start_diff;
554c04e61b5SLiu Bo 		em->block_len = em->len;
555c04e61b5SLiu Bo 	}
556c04e61b5SLiu Bo 	return add_extent_mapping(em_tree, em, 0);
557c04e61b5SLiu Bo }
558c04e61b5SLiu Bo 
559c04e61b5SLiu Bo /**
560c04e61b5SLiu Bo  * btrfs_add_extent_mapping - add extent mapping into em_tree
561f46b24c9SDavid Sterba  * @fs_info - used for tracepoint
562c04e61b5SLiu Bo  * @em_tree - the extent tree into which we want to insert the extent mapping
563c04e61b5SLiu Bo  * @em_in   - extent we are inserting
564c04e61b5SLiu Bo  * @start   - start of the logical range btrfs_get_extent() is requesting
565c04e61b5SLiu Bo  * @len     - length of the logical range btrfs_get_extent() is requesting
566c04e61b5SLiu Bo  *
567c04e61b5SLiu Bo  * Note that @em_in's range may be different from [start, start+len),
568c04e61b5SLiu Bo  * but they must be overlapped.
569c04e61b5SLiu Bo  *
570c04e61b5SLiu Bo  * Insert @em_in into @em_tree. In case there is an overlapping range, handle
571c04e61b5SLiu Bo  * the -EEXIST by either:
572c04e61b5SLiu Bo  * a) Returning the existing extent in @em_in if @start is within the
573c04e61b5SLiu Bo  *    existing em.
574c04e61b5SLiu Bo  * b) Merge the existing extent with @em_in passed in.
575c04e61b5SLiu Bo  *
576c04e61b5SLiu Bo  * Return 0 on success, otherwise -EEXIST.
577c04e61b5SLiu Bo  *
578c04e61b5SLiu Bo  */
579f46b24c9SDavid Sterba int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info,
580f46b24c9SDavid Sterba 			     struct extent_map_tree *em_tree,
581c04e61b5SLiu Bo 			     struct extent_map **em_in, u64 start, u64 len)
582c04e61b5SLiu Bo {
583c04e61b5SLiu Bo 	int ret;
584c04e61b5SLiu Bo 	struct extent_map *em = *em_in;
585c04e61b5SLiu Bo 
586c04e61b5SLiu Bo 	ret = add_extent_mapping(em_tree, em, 0);
587c04e61b5SLiu Bo 	/* it is possible that someone inserted the extent into the tree
588c04e61b5SLiu Bo 	 * while we had the lock dropped.  It is also possible that
589c04e61b5SLiu Bo 	 * an overlapping map exists in the tree
590c04e61b5SLiu Bo 	 */
591c04e61b5SLiu Bo 	if (ret == -EEXIST) {
592c04e61b5SLiu Bo 		struct extent_map *existing;
593c04e61b5SLiu Bo 
594c04e61b5SLiu Bo 		ret = 0;
595c04e61b5SLiu Bo 
596c04e61b5SLiu Bo 		existing = search_extent_mapping(em_tree, start, len);
597393da918SLiu Bo 
598f46b24c9SDavid Sterba 		trace_btrfs_handle_em_exist(fs_info, existing, em, start, len);
599393da918SLiu Bo 
600c04e61b5SLiu Bo 		/*
601c04e61b5SLiu Bo 		 * existing will always be non-NULL, since there must be
602c04e61b5SLiu Bo 		 * extent causing the -EEXIST.
603c04e61b5SLiu Bo 		 */
604c04e61b5SLiu Bo 		if (start >= existing->start &&
605c04e61b5SLiu Bo 		    start < extent_map_end(existing)) {
606c04e61b5SLiu Bo 			free_extent_map(em);
607c04e61b5SLiu Bo 			*em_in = existing;
608c04e61b5SLiu Bo 			ret = 0;
609c04e61b5SLiu Bo 		} else {
6109a7e10e7SLiu Bo 			u64 orig_start = em->start;
6119a7e10e7SLiu Bo 			u64 orig_len = em->len;
6129a7e10e7SLiu Bo 
613c04e61b5SLiu Bo 			/*
614c04e61b5SLiu Bo 			 * The existing extent map is the one nearest to
615c04e61b5SLiu Bo 			 * the [start, start + len) range which overlaps
616c04e61b5SLiu Bo 			 */
617c04e61b5SLiu Bo 			ret = merge_extent_mapping(em_tree, existing,
618c04e61b5SLiu Bo 						   em, start);
619c04e61b5SLiu Bo 			if (ret) {
620c04e61b5SLiu Bo 				free_extent_map(em);
621c04e61b5SLiu Bo 				*em_in = NULL;
6229a7e10e7SLiu Bo 				WARN_ONCE(ret,
6239a7e10e7SLiu Bo "unexpected error %d: merge existing(start %llu len %llu) with em(start %llu len %llu)\n",
6249a7e10e7SLiu Bo 					  ret, existing->start, existing->len,
6259a7e10e7SLiu Bo 					  orig_start, orig_len);
626c04e61b5SLiu Bo 			}
6279a7e10e7SLiu Bo 			free_extent_map(existing);
628c04e61b5SLiu Bo 		}
629c04e61b5SLiu Bo 	}
630c04e61b5SLiu Bo 
631c04e61b5SLiu Bo 	ASSERT(ret == 0 || ret == -EEXIST);
632c04e61b5SLiu Bo 	return ret;
633c04e61b5SLiu Bo }
634