blob: 6fe8b14e11cf0285152ff00f5fcf722dbc57fc23 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Chris Masond1310b22008-01-24 16:13:08 -05002#include <linux/err.h>
Chris Masond1310b22008-01-24 16:13:08 -05003#include <linux/slab.h>
Chris Masona52d9a82007-08-27 16:49:44 -04004#include <linux/spinlock.h>
Chris Masond1310b22008-01-24 16:13:08 -05005#include <linux/hardirq.h>
Li Zefan261507a02010-12-17 14:21:50 +08006#include "ctree.h"
Chris Masona52d9a82007-08-27 16:49:44 -04007#include "extent_map.h"
Anand Jainebb87652016-03-10 17:26:59 +08008#include "compression.h"
Chris Masona52d9a82007-08-27 16:49:44 -04009
Chris Mason86479a02007-09-10 19:58:16 -040010
Chris Masona52d9a82007-08-27 16:49:44 -040011static struct kmem_cache *extent_map_cache;
Chris Masonca664622007-11-27 11:16:35 -050012
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050013int __init extent_map_init(void)
Chris Masona52d9a82007-08-27 16:49:44 -040014{
David Sterba837e1972012-09-07 03:00:48 -060015 extent_map_cache = kmem_cache_create("btrfs_extent_map",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020016 sizeof(struct extent_map), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +030017 SLAB_MEM_SPREAD, NULL);
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050018 if (!extent_map_cache)
19 return -ENOMEM;
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050020 return 0;
Chris Masona52d9a82007-08-27 16:49:44 -040021}
22
Christian Hesse17636e02007-12-11 09:25:06 -050023void extent_map_exit(void)
Chris Masona52d9a82007-08-27 16:49:44 -040024{
Kinglong Mee5598e902016-01-29 21:36:35 +080025 kmem_cache_destroy(extent_map_cache);
Chris Masona52d9a82007-08-27 16:49:44 -040026}
27
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040028/**
29 * extent_map_tree_init - initialize extent map tree
30 * @tree: tree to initialize
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040031 *
32 * Initialize the extent tree @tree. Should be called for each new inode
33 * or other user of the extent_map interface.
34 */
David Sterbaa8067e02011-04-21 00:34:43 +020035void extent_map_tree_init(struct extent_map_tree *tree)
Chris Masona52d9a82007-08-27 16:49:44 -040036{
Eric Paris6bef4d32010-02-23 19:43:04 +000037 tree->map = RB_ROOT;
Josef Bacik5dc562c2012-08-17 13:14:17 -040038 INIT_LIST_HEAD(&tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -040039 rwlock_init(&tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -040040}
Chris Masona52d9a82007-08-27 16:49:44 -040041
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040042/**
43 * alloc_extent_map - allocate new extent map structure
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040044 *
45 * Allocate a new extent_map structure. The new structure is
46 * returned with a reference count of one and needs to be
47 * freed using free_extent_map()
48 */
David Sterba172ddd62011-04-21 00:48:27 +020049struct extent_map *alloc_extent_map(void)
Chris Masona52d9a82007-08-27 16:49:44 -040050{
51 struct extent_map *em;
Josef Bacik70c8a912012-10-11 16:54:30 -040052 em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +000053 if (!em)
54 return NULL;
Filipe Mananacbc0e922014-02-25 14:15:12 +000055 RB_CLEAR_NODE(&em->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -050056 em->flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +080057 em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik5dc562c2012-08-17 13:14:17 -040058 em->generation = 0;
Elena Reshetova490b54d2017-03-03 10:55:12 +020059 refcount_set(&em->refs, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040060 INIT_LIST_HEAD(&em->list);
Chris Masona52d9a82007-08-27 16:49:44 -040061 return em;
62}
Chris Masona52d9a82007-08-27 16:49:44 -040063
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040064/**
65 * free_extent_map - drop reference count of an extent_map
Nicholas D Steeves01327612016-05-19 21:18:45 -040066 * @em: extent map being released
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040067 *
68 * Drops the reference out on @em by one and free the structure
69 * if the reference count hits zero.
70 */
Chris Masona52d9a82007-08-27 16:49:44 -040071void free_extent_map(struct extent_map *em)
72{
Chris Mason2bf5a722007-08-30 11:54:02 -040073 if (!em)
74 return;
Elena Reshetova490b54d2017-03-03 10:55:12 +020075 WARN_ON(refcount_read(&em->refs) == 0);
76 if (refcount_dec_and_test(&em->refs)) {
Filipe Mananacbc0e922014-02-25 14:15:12 +000077 WARN_ON(extent_map_in_tree(em));
Josef Bacik5dc562c2012-08-17 13:14:17 -040078 WARN_ON(!list_empty(&em->list));
Wang Shilong298a8f92014-06-19 10:42:52 +080079 if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
Jeff Mahoney95617d62015-06-03 10:55:48 -040080 kfree(em->map_lookup);
Chris Masona52d9a82007-08-27 16:49:44 -040081 kmem_cache_free(extent_map_cache, em);
82 }
83}
Chris Masona52d9a82007-08-27 16:49:44 -040084
Filipe David Borba Manana32193c12013-11-25 03:23:51 +000085/* simple helper to do math around the end of an extent, handling wrap */
86static u64 range_end(u64 start, u64 len)
87{
88 if (start + len < start)
89 return (u64)-1;
90 return start + len;
91}
92
93static int tree_insert(struct rb_root *root, struct extent_map *em)
Chris Masona52d9a82007-08-27 16:49:44 -040094{
Chris Masond3977122009-01-05 21:25:51 -050095 struct rb_node **p = &root->rb_node;
96 struct rb_node *parent = NULL;
Filipe David Borba Manana32193c12013-11-25 03:23:51 +000097 struct extent_map *entry = NULL;
98 struct rb_node *orig_parent = NULL;
99 u64 end = range_end(em->start, em->len);
Chris Masona52d9a82007-08-27 16:49:44 -0400100
Chris Masond3977122009-01-05 21:25:51 -0500101 while (*p) {
Chris Masona52d9a82007-08-27 16:49:44 -0400102 parent = *p;
Chris Masond1310b22008-01-24 16:13:08 -0500103 entry = rb_entry(parent, struct extent_map, rb_node);
104
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000105 if (em->start < entry->start)
Chris Masona52d9a82007-08-27 16:49:44 -0400106 p = &(*p)->rb_left;
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000107 else if (em->start >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -0400108 p = &(*p)->rb_right;
109 else
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000110 return -EEXIST;
Chris Masona52d9a82007-08-27 16:49:44 -0400111 }
112
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000113 orig_parent = parent;
114 while (parent && em->start >= extent_map_end(entry)) {
115 parent = rb_next(parent);
116 entry = rb_entry(parent, struct extent_map, rb_node);
117 }
118 if (parent)
119 if (end > entry->start && em->start < extent_map_end(entry))
120 return -EEXIST;
121
122 parent = orig_parent;
123 entry = rb_entry(parent, struct extent_map, rb_node);
124 while (parent && em->start < entry->start) {
125 parent = rb_prev(parent);
126 entry = rb_entry(parent, struct extent_map, rb_node);
127 }
128 if (parent)
129 if (end > entry->start && em->start < extent_map_end(entry))
130 return -EEXIST;
131
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000132 rb_link_node(&em->rb_node, orig_parent, p);
133 rb_insert_color(&em->rb_node, root);
134 return 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400135}
136
Chris Masond352ac62008-09-29 15:18:18 -0400137/*
138 * search through the tree for an extent_map with a given offset. If
139 * it can't be found, try to find some neighboring extents
140 */
Chris Masona52d9a82007-08-27 16:49:44 -0400141static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
Chris Mason5f564062008-01-22 16:47:59 -0500142 struct rb_node **prev_ret,
143 struct rb_node **next_ret)
Chris Masona52d9a82007-08-27 16:49:44 -0400144{
Chris Masond3977122009-01-05 21:25:51 -0500145 struct rb_node *n = root->rb_node;
Chris Masona52d9a82007-08-27 16:49:44 -0400146 struct rb_node *prev = NULL;
Chris Mason5f564062008-01-22 16:47:59 -0500147 struct rb_node *orig_prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500148 struct extent_map *entry;
149 struct extent_map *prev_entry = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400150
Chris Masond3977122009-01-05 21:25:51 -0500151 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500152 entry = rb_entry(n, struct extent_map, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400153 prev = n;
154 prev_entry = entry;
155
156 if (offset < entry->start)
157 n = n->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500158 else if (offset >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -0400159 n = n->rb_right;
160 else
161 return n;
162 }
Chris Mason5f564062008-01-22 16:47:59 -0500163
164 if (prev_ret) {
165 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500166 while (prev && offset >= extent_map_end(prev_entry)) {
Chris Mason5f564062008-01-22 16:47:59 -0500167 prev = rb_next(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500168 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500169 }
170 *prev_ret = prev;
171 prev = orig_prev;
Chris Masona52d9a82007-08-27 16:49:44 -0400172 }
Chris Mason5f564062008-01-22 16:47:59 -0500173
174 if (next_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500175 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500176 while (prev && offset < prev_entry->start) {
Chris Mason5f564062008-01-22 16:47:59 -0500177 prev = rb_prev(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500178 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500179 }
180 *next_ret = prev;
181 }
Chris Masona52d9a82007-08-27 16:49:44 -0400182 return NULL;
183}
184
Chris Masond352ac62008-09-29 15:18:18 -0400185/* check to see if two extent_map structs are adjacent and safe to merge */
Chris Masond1310b22008-01-24 16:13:08 -0500186static int mergable_maps(struct extent_map *prev, struct extent_map *next)
Chris Masona52d9a82007-08-27 16:49:44 -0400187{
Chris Mason7f3c74f2008-07-18 12:01:11 -0400188 if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
189 return 0;
190
Chris Masonc8b97812008-10-29 14:49:59 -0400191 /*
192 * don't merge compressed extents, we need to know their
193 * actual size
194 */
195 if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
196 return 0;
197
Josef Bacik201a9032013-01-24 12:02:07 -0500198 if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
199 test_bit(EXTENT_FLAG_LOGGING, &next->flags))
200 return 0;
201
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400202 /*
203 * We don't want to merge stuff that hasn't been written to the log yet
204 * since it may not reflect exactly what is on disk, and that would be
205 * bad.
206 */
207 if (!list_empty(&prev->list) || !list_empty(&next->list))
208 return 0;
209
Chris Masond1310b22008-01-24 16:13:08 -0500210 if (extent_map_end(prev) == next->start &&
211 prev->flags == next->flags &&
212 prev->bdev == next->bdev &&
213 ((next->block_start == EXTENT_MAP_HOLE &&
214 prev->block_start == EXTENT_MAP_HOLE) ||
215 (next->block_start == EXTENT_MAP_INLINE &&
216 prev->block_start == EXTENT_MAP_INLINE) ||
217 (next->block_start == EXTENT_MAP_DELALLOC &&
218 prev->block_start == EXTENT_MAP_DELALLOC) ||
219 (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
220 next->block_start == extent_map_block_end(prev)))) {
221 return 1;
222 }
Chris Masona52d9a82007-08-27 16:49:44 -0400223 return 0;
224}
225
Li Zefan4d2c8f622011-07-14 03:18:33 +0000226static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
Chris Masona1ed8352009-09-11 12:27:37 -0400227{
Chris Masona1ed8352009-09-11 12:27:37 -0400228 struct extent_map *merge = NULL;
229 struct rb_node *rb;
Chris Masona1ed8352009-09-11 12:27:37 -0400230
231 if (em->start != 0) {
232 rb = rb_prev(&em->rb_node);
233 if (rb)
234 merge = rb_entry(rb, struct extent_map, rb_node);
235 if (rb && mergable_maps(merge, em)) {
236 em->start = merge->start;
Josef Bacik70c8a912012-10-11 16:54:30 -0400237 em->orig_start = merge->orig_start;
Chris Masona1ed8352009-09-11 12:27:37 -0400238 em->len += merge->len;
239 em->block_len += merge->block_len;
240 em->block_start = merge->block_start;
Josef Bacik70c8a912012-10-11 16:54:30 -0400241 em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
242 em->mod_start = merge->mod_start;
243 em->generation = max(em->generation, merge->generation);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400244
Chris Masona1ed8352009-09-11 12:27:37 -0400245 rb_erase(&merge->rb_node, &tree->map);
Filipe Mananacbc0e922014-02-25 14:15:12 +0000246 RB_CLEAR_NODE(&merge->rb_node);
Chris Masona1ed8352009-09-11 12:27:37 -0400247 free_extent_map(merge);
248 }
249 }
250
251 rb = rb_next(&em->rb_node);
252 if (rb)
253 merge = rb_entry(rb, struct extent_map, rb_node);
254 if (rb && mergable_maps(em, merge)) {
255 em->len += merge->len;
Filipe David Borba Mananad527afe2013-11-30 11:28:35 +0000256 em->block_len += merge->block_len;
Chris Masona1ed8352009-09-11 12:27:37 -0400257 rb_erase(&merge->rb_node, &tree->map);
Filipe Mananacbc0e922014-02-25 14:15:12 +0000258 RB_CLEAR_NODE(&merge->rb_node);
Josef Bacik70c8a912012-10-11 16:54:30 -0400259 em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
260 em->generation = max(em->generation, merge->generation);
Chris Masona1ed8352009-09-11 12:27:37 -0400261 free_extent_map(merge);
262 }
Li Zefan4d2c8f622011-07-14 03:18:33 +0000263}
264
Josef Bacik5dc562c2012-08-17 13:14:17 -0400265/**
Liu Bo52b1de92012-10-30 17:13:52 +0800266 * unpin_extent_cache - unpin an extent from the cache
Josef Bacik5dc562c2012-08-17 13:14:17 -0400267 * @tree: tree to unpin the extent in
268 * @start: logical offset in the file
269 * @len: length of the extent
270 * @gen: generation that this extent has been modified in
Josef Bacik5dc562c2012-08-17 13:14:17 -0400271 *
272 * Called after an extent has been written to disk properly. Set the generation
273 * to the generation that actually added the file item to the inode so we know
274 * we need to sync this extent when we call fsync().
275 */
276int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
277 u64 gen)
Li Zefan4d2c8f622011-07-14 03:18:33 +0000278{
279 int ret = 0;
280 struct extent_map *em;
Liu Bo4e2f84e2012-08-27 10:52:20 -0600281 bool prealloc = false;
Li Zefan4d2c8f622011-07-14 03:18:33 +0000282
283 write_lock(&tree->lock);
284 em = lookup_extent_mapping(tree, start, len);
285
286 WARN_ON(!em || em->start != start);
287
288 if (!em)
289 goto out;
290
Josef Bacik5dc562c2012-08-17 13:14:17 -0400291 em->generation = gen;
Li Zefan4d2c8f622011-07-14 03:18:33 +0000292 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo4e2f84e2012-08-27 10:52:20 -0600293 em->mod_start = em->start;
294 em->mod_len = em->len;
295
Josef Bacikb11e2342012-12-03 10:58:15 -0500296 if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
Liu Bo4e2f84e2012-08-27 10:52:20 -0600297 prealloc = true;
Josef Bacikb11e2342012-12-03 10:58:15 -0500298 clear_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo4e2f84e2012-08-27 10:52:20 -0600299 }
Li Zefan4d2c8f622011-07-14 03:18:33 +0000300
301 try_merge_map(tree, em);
Liu Bo4e2f84e2012-08-27 10:52:20 -0600302
303 if (prealloc) {
304 em->mod_start = em->start;
305 em->mod_len = em->len;
306 }
307
Chris Masona1ed8352009-09-11 12:27:37 -0400308 free_extent_map(em);
309out:
310 write_unlock(&tree->lock);
311 return ret;
312
313}
314
Josef Bacik201a9032013-01-24 12:02:07 -0500315void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
316{
317 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe Mananacbc0e922014-02-25 14:15:12 +0000318 if (extent_map_in_tree(em))
Josef Bacik222c81d2013-01-28 09:45:20 -0500319 try_merge_map(tree, em);
Josef Bacik201a9032013-01-24 12:02:07 -0500320}
321
Filipe Manana176840b2014-02-25 14:15:13 +0000322static inline void setup_extent_mapping(struct extent_map_tree *tree,
323 struct extent_map *em,
324 int modified)
325{
Elena Reshetova490b54d2017-03-03 10:55:12 +0200326 refcount_inc(&em->refs);
Filipe Manana176840b2014-02-25 14:15:13 +0000327 em->mod_start = em->start;
328 em->mod_len = em->len;
329
330 if (modified)
331 list_move(&em->list, &tree->modified_extents);
332 else
333 try_merge_map(tree, em);
334}
335
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400336/**
337 * add_extent_mapping - add new extent map to the extent tree
338 * @tree: tree to insert new map in
339 * @em: map to insert
340 *
341 * Insert @em into @tree or perform a simple forward/backward merge with
342 * existing mappings. The extent_map struct passed in will be inserted
343 * into the tree directly, with an additional reference taken, or a
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300344 * reference dropped if the merge attempt was successful.
Chris Masona52d9a82007-08-27 16:49:44 -0400345 */
346int add_extent_mapping(struct extent_map_tree *tree,
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400347 struct extent_map *em, int modified)
Chris Masona52d9a82007-08-27 16:49:44 -0400348{
349 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400350
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000351 ret = tree_insert(&tree->map, em);
352 if (ret)
Chris Mason7c2fe322008-08-20 08:51:50 -0400353 goto out;
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000354
Filipe Manana176840b2014-02-25 14:15:13 +0000355 setup_extent_mapping(tree, em, modified);
Chris Masona52d9a82007-08-27 16:49:44 -0400356out:
Chris Masona52d9a82007-08-27 16:49:44 -0400357 return ret;
358}
Chris Masona52d9a82007-08-27 16:49:44 -0400359
Eric Sandeen48a3b632013-04-25 20:41:01 +0000360static struct extent_map *
361__lookup_extent_mapping(struct extent_map_tree *tree,
362 u64 start, u64 len, int strict)
Li Zefaned64f062011-07-14 03:18:15 +0000363{
364 struct extent_map *em;
365 struct rb_node *rb_node;
366 struct rb_node *prev = NULL;
367 struct rb_node *next = NULL;
368 u64 end = range_end(start, len);
369
370 rb_node = __tree_search(&tree->map, start, &prev, &next);
371 if (!rb_node) {
372 if (prev)
373 rb_node = prev;
374 else if (next)
375 rb_node = next;
376 else
377 return NULL;
378 }
379
380 em = rb_entry(rb_node, struct extent_map, rb_node);
381
382 if (strict && !(end > em->start && start < extent_map_end(em)))
383 return NULL;
384
Elena Reshetova490b54d2017-03-03 10:55:12 +0200385 refcount_inc(&em->refs);
Li Zefaned64f062011-07-14 03:18:15 +0000386 return em;
387}
388
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400389/**
390 * lookup_extent_mapping - lookup extent_map
391 * @tree: tree to lookup in
392 * @start: byte offset to start the search
393 * @len: length of the lookup range
394 *
395 * Find and return the first extent_map struct in @tree that intersects the
396 * [start, len] range. There may be additional objects in the tree that
397 * intersect, so check the object returned carefully to make sure that no
398 * additional lookups are needed.
Chris Masona52d9a82007-08-27 16:49:44 -0400399 */
400struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500401 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -0400402{
Li Zefaned64f062011-07-14 03:18:15 +0000403 return __lookup_extent_mapping(tree, start, len, 1);
Chris Masona52d9a82007-08-27 16:49:44 -0400404}
Chris Masona52d9a82007-08-27 16:49:44 -0400405
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400406/**
Chris Masonb917b7c2009-09-18 16:07:03 -0400407 * search_extent_mapping - find a nearby extent map
408 * @tree: tree to lookup in
409 * @start: byte offset to start the search
410 * @len: length of the lookup range
411 *
412 * Find and return the first extent_map struct in @tree that intersects the
413 * [start, len] range.
414 *
415 * If one can't be found, any nearby extent may be returned
416 */
417struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
418 u64 start, u64 len)
419{
Li Zefaned64f062011-07-14 03:18:15 +0000420 return __lookup_extent_mapping(tree, start, len, 0);
Chris Masonb917b7c2009-09-18 16:07:03 -0400421}
422
423/**
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400424 * remove_extent_mapping - removes an extent_map from the extent tree
425 * @tree: extent tree to remove from
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -0800426 * @em: extent map being removed
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400427 *
428 * Removes @em from @tree. No reference counts are dropped, and no checks
429 * are done to see if the range is in use
Chris Masona52d9a82007-08-27 16:49:44 -0400430 */
431int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
432{
Chris Masond1310b22008-01-24 16:13:08 -0500433 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400434
Chris Mason7f3c74f2008-07-18 12:01:11 -0400435 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
Chris Masond1310b22008-01-24 16:13:08 -0500436 rb_erase(&em->rb_node, &tree->map);
Josef Bacikff44c6e2012-09-14 12:59:20 -0400437 if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
438 list_del_init(&em->list);
Filipe Mananacbc0e922014-02-25 14:15:12 +0000439 RB_CLEAR_NODE(&em->rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400440 return ret;
441}
Filipe Manana176840b2014-02-25 14:15:13 +0000442
443void replace_extent_mapping(struct extent_map_tree *tree,
444 struct extent_map *cur,
445 struct extent_map *new,
446 int modified)
447{
448 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));
449 ASSERT(extent_map_in_tree(cur));
450 if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))
451 list_del_init(&cur->list);
452 rb_replace_node(&cur->rb_node, &new->rb_node, &tree->map);
453 RB_CLEAR_NODE(&cur->rb_node);
454
455 setup_extent_mapping(tree, new, modified);
456}
Liu Boc04e61b2018-01-05 12:51:11 -0700457
458static struct extent_map *next_extent_map(struct extent_map *em)
459{
460 struct rb_node *next;
461
462 next = rb_next(&em->rb_node);
463 if (!next)
464 return NULL;
465 return container_of(next, struct extent_map, rb_node);
466}
467
468static struct extent_map *prev_extent_map(struct extent_map *em)
469{
470 struct rb_node *prev;
471
472 prev = rb_prev(&em->rb_node);
473 if (!prev)
474 return NULL;
475 return container_of(prev, struct extent_map, rb_node);
476}
477
478/* helper for btfs_get_extent. Given an existing extent in the tree,
479 * the existing extent is the nearest extent to map_start,
480 * and an extent that you want to insert, deal with overlap and insert
481 * the best fitted new extent into the tree.
482 */
483static int merge_extent_mapping(struct extent_map_tree *em_tree,
484 struct extent_map *existing,
485 struct extent_map *em,
486 u64 map_start)
487{
488 struct extent_map *prev;
489 struct extent_map *next;
490 u64 start;
491 u64 end;
492 u64 start_diff;
493
494 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
495
496 if (existing->start > map_start) {
497 next = existing;
498 prev = prev_extent_map(next);
499 } else {
500 prev = existing;
501 next = next_extent_map(prev);
502 }
503
504 start = prev ? extent_map_end(prev) : em->start;
505 start = max_t(u64, start, em->start);
506 end = next ? next->start : extent_map_end(em);
507 end = min_t(u64, end, extent_map_end(em));
508 start_diff = start - em->start;
509 em->start = start;
510 em->len = end - start;
511 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
512 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
513 em->block_start += start_diff;
514 em->block_len = em->len;
515 }
516 return add_extent_mapping(em_tree, em, 0);
517}
518
519/**
520 * btrfs_add_extent_mapping - add extent mapping into em_tree
521 * @em_tree - the extent tree into which we want to insert the extent mapping
522 * @em_in - extent we are inserting
523 * @start - start of the logical range btrfs_get_extent() is requesting
524 * @len - length of the logical range btrfs_get_extent() is requesting
525 *
526 * Note that @em_in's range may be different from [start, start+len),
527 * but they must be overlapped.
528 *
529 * Insert @em_in into @em_tree. In case there is an overlapping range, handle
530 * the -EEXIST by either:
531 * a) Returning the existing extent in @em_in if @start is within the
532 * existing em.
533 * b) Merge the existing extent with @em_in passed in.
534 *
535 * Return 0 on success, otherwise -EEXIST.
536 *
537 */
538int btrfs_add_extent_mapping(struct extent_map_tree *em_tree,
539 struct extent_map **em_in, u64 start, u64 len)
540{
541 int ret;
542 struct extent_map *em = *em_in;
543
544 ret = add_extent_mapping(em_tree, em, 0);
545 /* it is possible that someone inserted the extent into the tree
546 * while we had the lock dropped. It is also possible that
547 * an overlapping map exists in the tree
548 */
549 if (ret == -EEXIST) {
550 struct extent_map *existing;
551
552 ret = 0;
553
554 existing = search_extent_mapping(em_tree, start, len);
555 /*
556 * existing will always be non-NULL, since there must be
557 * extent causing the -EEXIST.
558 */
559 if (start >= existing->start &&
560 start < extent_map_end(existing)) {
561 free_extent_map(em);
562 *em_in = existing;
563 ret = 0;
564 } else {
565 /*
566 * The existing extent map is the one nearest to
567 * the [start, start + len) range which overlaps
568 */
569 ret = merge_extent_mapping(em_tree, existing,
570 em, start);
571 free_extent_map(existing);
572 if (ret) {
573 free_extent_map(em);
574 *em_in = NULL;
575 }
576 }
577 }
578
579 ASSERT(ret == 0 || ret == -EEXIST);
580 return ret;
581}