blob: 4a8e02f7b6c7a143ab232c13d973d112e49d7fb9 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
David Sterbac1d7c512018-04-03 19:23:33 +02002
Chris Masond1310b22008-01-24 16:13:08 -05003#include <linux/err.h>
Chris Masond1310b22008-01-24 16:13:08 -05004#include <linux/slab.h>
Chris Masona52d9a82007-08-27 16:49:44 -04005#include <linux/spinlock.h>
Li Zefan261507a02010-12-17 14:21:50 +08006#include "ctree.h"
Jeff Mahoney1c11b632019-03-27 14:24:12 +02007#include "volumes.h"
Chris Masona52d9a82007-08-27 16:49:44 -04008#include "extent_map.h"
Anand Jainebb87652016-03-10 17:26:59 +08009#include "compression.h"
Chris Masona52d9a82007-08-27 16:49:44 -040010
Chris Mason86479a02007-09-10 19:58:16 -040011
Chris Masona52d9a82007-08-27 16:49:44 -040012static struct kmem_cache *extent_map_cache;
Chris Masonca664622007-11-27 11:16:35 -050013
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050014int __init extent_map_init(void)
Chris Masona52d9a82007-08-27 16:49:44 -040015{
David Sterba837e1972012-09-07 03:00:48 -060016 extent_map_cache = kmem_cache_create("btrfs_extent_map",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020017 sizeof(struct extent_map), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +030018 SLAB_MEM_SPREAD, NULL);
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050019 if (!extent_map_cache)
20 return -ENOMEM;
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050021 return 0;
Chris Masona52d9a82007-08-27 16:49:44 -040022}
23
David Sterbae67c7182018-02-19 17:24:18 +010024void __cold extent_map_exit(void)
Chris Masona52d9a82007-08-27 16:49:44 -040025{
Kinglong Mee5598e902016-01-29 21:36:35 +080026 kmem_cache_destroy(extent_map_cache);
Chris Masona52d9a82007-08-27 16:49:44 -040027}
28
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040029/**
30 * extent_map_tree_init - initialize extent map tree
31 * @tree: tree to initialize
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040032 *
33 * Initialize the extent tree @tree. Should be called for each new inode
34 * or other user of the extent_map interface.
35 */
David Sterbaa8067e02011-04-21 00:34:43 +020036void extent_map_tree_init(struct extent_map_tree *tree)
Chris Masona52d9a82007-08-27 16:49:44 -040037{
Liu Bo07e1ce02018-08-23 03:51:52 +080038 tree->map = RB_ROOT_CACHED;
Josef Bacik5dc562c2012-08-17 13:14:17 -040039 INIT_LIST_HEAD(&tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -040040 rwlock_init(&tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -040041}
Chris Masona52d9a82007-08-27 16:49:44 -040042
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040043/**
44 * alloc_extent_map - allocate new extent map structure
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040045 *
46 * Allocate a new extent_map structure. The new structure is
47 * returned with a reference count of one and needs to be
48 * freed using free_extent_map()
49 */
David Sterba172ddd62011-04-21 00:48:27 +020050struct extent_map *alloc_extent_map(void)
Chris Masona52d9a82007-08-27 16:49:44 -040051{
52 struct extent_map *em;
Josef Bacik70c8a912012-10-11 16:54:30 -040053 em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +000054 if (!em)
55 return NULL;
Filipe Mananacbc0e922014-02-25 14:15:12 +000056 RB_CLEAR_NODE(&em->rb_node);
Chris Masond1310b22008-01-24 16:13:08 -050057 em->flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +080058 em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik5dc562c2012-08-17 13:14:17 -040059 em->generation = 0;
Elena Reshetova490b54d2017-03-03 10:55:12 +020060 refcount_set(&em->refs, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040061 INIT_LIST_HEAD(&em->list);
Chris Masona52d9a82007-08-27 16:49:44 -040062 return em;
63}
Chris Masona52d9a82007-08-27 16:49:44 -040064
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040065/**
66 * free_extent_map - drop reference count of an extent_map
Nicholas D Steeves01327612016-05-19 21:18:45 -040067 * @em: extent map being released
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040068 *
69 * Drops the reference out on @em by one and free the structure
70 * if the reference count hits zero.
71 */
Chris Masona52d9a82007-08-27 16:49:44 -040072void free_extent_map(struct extent_map *em)
73{
Chris Mason2bf5a722007-08-30 11:54:02 -040074 if (!em)
75 return;
Elena Reshetova490b54d2017-03-03 10:55:12 +020076 WARN_ON(refcount_read(&em->refs) == 0);
77 if (refcount_dec_and_test(&em->refs)) {
Filipe Mananacbc0e922014-02-25 14:15:12 +000078 WARN_ON(extent_map_in_tree(em));
Josef Bacik5dc562c2012-08-17 13:14:17 -040079 WARN_ON(!list_empty(&em->list));
Wang Shilong298a8f92014-06-19 10:42:52 +080080 if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
Jeff Mahoney95617d62015-06-03 10:55:48 -040081 kfree(em->map_lookup);
Chris Masona52d9a82007-08-27 16:49:44 -040082 kmem_cache_free(extent_map_cache, em);
83 }
84}
Chris Masona52d9a82007-08-27 16:49:44 -040085
Filipe David Borba Manana32193c12013-11-25 03:23:51 +000086/* simple helper to do math around the end of an extent, handling wrap */
87static u64 range_end(u64 start, u64 len)
88{
89 if (start + len < start)
90 return (u64)-1;
91 return start + len;
92}
93
Liu Bo07e1ce02018-08-23 03:51:52 +080094static int tree_insert(struct rb_root_cached *root, struct extent_map *em)
Chris Masona52d9a82007-08-27 16:49:44 -040095{
Liu Bo07e1ce02018-08-23 03:51:52 +080096 struct rb_node **p = &root->rb_root.rb_node;
Chris Masond3977122009-01-05 21:25:51 -050097 struct rb_node *parent = NULL;
Filipe David Borba Manana32193c12013-11-25 03:23:51 +000098 struct extent_map *entry = NULL;
99 struct rb_node *orig_parent = NULL;
100 u64 end = range_end(em->start, em->len);
Liu Bo07e1ce02018-08-23 03:51:52 +0800101 bool leftmost = true;
Chris Masona52d9a82007-08-27 16:49:44 -0400102
Chris Masond3977122009-01-05 21:25:51 -0500103 while (*p) {
Chris Masona52d9a82007-08-27 16:49:44 -0400104 parent = *p;
Chris Masond1310b22008-01-24 16:13:08 -0500105 entry = rb_entry(parent, struct extent_map, rb_node);
106
Liu Bo07e1ce02018-08-23 03:51:52 +0800107 if (em->start < entry->start) {
Chris Masona52d9a82007-08-27 16:49:44 -0400108 p = &(*p)->rb_left;
Liu Bo07e1ce02018-08-23 03:51:52 +0800109 } else if (em->start >= extent_map_end(entry)) {
Chris Masona52d9a82007-08-27 16:49:44 -0400110 p = &(*p)->rb_right;
Liu Bo07e1ce02018-08-23 03:51:52 +0800111 leftmost = false;
112 } else {
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000113 return -EEXIST;
Liu Bo07e1ce02018-08-23 03:51:52 +0800114 }
Chris Masona52d9a82007-08-27 16:49:44 -0400115 }
116
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000117 orig_parent = parent;
118 while (parent && em->start >= extent_map_end(entry)) {
119 parent = rb_next(parent);
120 entry = rb_entry(parent, struct extent_map, rb_node);
121 }
122 if (parent)
123 if (end > entry->start && em->start < extent_map_end(entry))
124 return -EEXIST;
125
126 parent = orig_parent;
127 entry = rb_entry(parent, struct extent_map, rb_node);
128 while (parent && em->start < entry->start) {
129 parent = rb_prev(parent);
130 entry = rb_entry(parent, struct extent_map, rb_node);
131 }
132 if (parent)
133 if (end > entry->start && em->start < extent_map_end(entry))
134 return -EEXIST;
135
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000136 rb_link_node(&em->rb_node, orig_parent, p);
Liu Bo07e1ce02018-08-23 03:51:52 +0800137 rb_insert_color_cached(&em->rb_node, root, leftmost);
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000138 return 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400139}
140
Chris Masond352ac62008-09-29 15:18:18 -0400141/*
142 * search through the tree for an extent_map with a given offset. If
143 * it can't be found, try to find some neighboring extents
144 */
Chris Masona52d9a82007-08-27 16:49:44 -0400145static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
Chris Mason5f564062008-01-22 16:47:59 -0500146 struct rb_node **prev_ret,
147 struct rb_node **next_ret)
Chris Masona52d9a82007-08-27 16:49:44 -0400148{
Chris Masond3977122009-01-05 21:25:51 -0500149 struct rb_node *n = root->rb_node;
Chris Masona52d9a82007-08-27 16:49:44 -0400150 struct rb_node *prev = NULL;
Chris Mason5f564062008-01-22 16:47:59 -0500151 struct rb_node *orig_prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500152 struct extent_map *entry;
153 struct extent_map *prev_entry = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400154
Chris Masond3977122009-01-05 21:25:51 -0500155 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500156 entry = rb_entry(n, struct extent_map, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400157 prev = n;
158 prev_entry = entry;
159
160 if (offset < entry->start)
161 n = n->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500162 else if (offset >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -0400163 n = n->rb_right;
164 else
165 return n;
166 }
Chris Mason5f564062008-01-22 16:47:59 -0500167
168 if (prev_ret) {
169 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500170 while (prev && offset >= extent_map_end(prev_entry)) {
Chris Mason5f564062008-01-22 16:47:59 -0500171 prev = rb_next(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500172 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500173 }
174 *prev_ret = prev;
175 prev = orig_prev;
Chris Masona52d9a82007-08-27 16:49:44 -0400176 }
Chris Mason5f564062008-01-22 16:47:59 -0500177
178 if (next_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500179 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500180 while (prev && offset < prev_entry->start) {
Chris Mason5f564062008-01-22 16:47:59 -0500181 prev = rb_prev(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500182 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500183 }
184 *next_ret = prev;
185 }
Chris Masona52d9a82007-08-27 16:49:44 -0400186 return NULL;
187}
188
Chris Masond352ac62008-09-29 15:18:18 -0400189/* check to see if two extent_map structs are adjacent and safe to merge */
Chris Masond1310b22008-01-24 16:13:08 -0500190static int mergable_maps(struct extent_map *prev, struct extent_map *next)
Chris Masona52d9a82007-08-27 16:49:44 -0400191{
Chris Mason7f3c74f2008-07-18 12:01:11 -0400192 if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
193 return 0;
194
Chris Masonc8b97812008-10-29 14:49:59 -0400195 /*
196 * don't merge compressed extents, we need to know their
197 * actual size
198 */
199 if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
200 return 0;
201
Josef Bacik201a9032013-01-24 12:02:07 -0500202 if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
203 test_bit(EXTENT_FLAG_LOGGING, &next->flags))
204 return 0;
205
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400206 /*
207 * We don't want to merge stuff that hasn't been written to the log yet
208 * since it may not reflect exactly what is on disk, and that would be
209 * bad.
210 */
211 if (!list_empty(&prev->list) || !list_empty(&next->list))
212 return 0;
213
Nikolay Borisov951e05a2019-01-08 16:53:46 +0200214 ASSERT(next->block_start != EXTENT_MAP_DELALLOC &&
215 prev->block_start != EXTENT_MAP_DELALLOC);
216
David Sterbac3e14902019-05-10 17:48:30 +0200217 if (prev->map_lookup || next->map_lookup)
218 ASSERT(test_bit(EXTENT_FLAG_FS_MAPPING, &prev->flags) &&
219 test_bit(EXTENT_FLAG_FS_MAPPING, &next->flags));
220
Chris Masond1310b22008-01-24 16:13:08 -0500221 if (extent_map_end(prev) == next->start &&
222 prev->flags == next->flags &&
David Sterbac3e14902019-05-10 17:48:30 +0200223 prev->map_lookup == next->map_lookup &&
Chris Masond1310b22008-01-24 16:13:08 -0500224 ((next->block_start == EXTENT_MAP_HOLE &&
225 prev->block_start == EXTENT_MAP_HOLE) ||
226 (next->block_start == EXTENT_MAP_INLINE &&
227 prev->block_start == EXTENT_MAP_INLINE) ||
Chris Masond1310b22008-01-24 16:13:08 -0500228 (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
229 next->block_start == extent_map_block_end(prev)))) {
230 return 1;
231 }
Chris Masona52d9a82007-08-27 16:49:44 -0400232 return 0;
233}
234
Li Zefan4d2c8f622011-07-14 03:18:33 +0000235static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
Chris Masona1ed8352009-09-11 12:27:37 -0400236{
Chris Masona1ed8352009-09-11 12:27:37 -0400237 struct extent_map *merge = NULL;
238 struct rb_node *rb;
Chris Masona1ed8352009-09-11 12:27:37 -0400239
Filipe Mananaac05ca92020-01-31 14:06:07 +0000240 /*
241 * We can't modify an extent map that is in the tree and that is being
242 * used by another task, as it can cause that other task to see it in
243 * inconsistent state during the merging. We always have 1 reference for
244 * the tree and 1 for this task (which is unpinning the extent map or
245 * clearing the logging flag), so anything > 2 means it's being used by
246 * other tasks too.
247 */
248 if (refcount_read(&em->refs) > 2)
249 return;
250
Chris Masona1ed8352009-09-11 12:27:37 -0400251 if (em->start != 0) {
252 rb = rb_prev(&em->rb_node);
253 if (rb)
254 merge = rb_entry(rb, struct extent_map, rb_node);
255 if (rb && mergable_maps(merge, em)) {
256 em->start = merge->start;
Josef Bacik70c8a912012-10-11 16:54:30 -0400257 em->orig_start = merge->orig_start;
Chris Masona1ed8352009-09-11 12:27:37 -0400258 em->len += merge->len;
259 em->block_len += merge->block_len;
260 em->block_start = merge->block_start;
Josef Bacik70c8a912012-10-11 16:54:30 -0400261 em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
262 em->mod_start = merge->mod_start;
263 em->generation = max(em->generation, merge->generation);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400264
Liu Bo07e1ce02018-08-23 03:51:52 +0800265 rb_erase_cached(&merge->rb_node, &tree->map);
Filipe Mananacbc0e922014-02-25 14:15:12 +0000266 RB_CLEAR_NODE(&merge->rb_node);
Chris Masona1ed8352009-09-11 12:27:37 -0400267 free_extent_map(merge);
268 }
269 }
270
271 rb = rb_next(&em->rb_node);
272 if (rb)
273 merge = rb_entry(rb, struct extent_map, rb_node);
274 if (rb && mergable_maps(em, merge)) {
275 em->len += merge->len;
Filipe David Borba Mananad527afe2013-11-30 11:28:35 +0000276 em->block_len += merge->block_len;
Liu Bo07e1ce02018-08-23 03:51:52 +0800277 rb_erase_cached(&merge->rb_node, &tree->map);
Filipe Mananacbc0e922014-02-25 14:15:12 +0000278 RB_CLEAR_NODE(&merge->rb_node);
Josef Bacik70c8a912012-10-11 16:54:30 -0400279 em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
280 em->generation = max(em->generation, merge->generation);
Chris Masona1ed8352009-09-11 12:27:37 -0400281 free_extent_map(merge);
282 }
Li Zefan4d2c8f622011-07-14 03:18:33 +0000283}
284
Josef Bacik5dc562c2012-08-17 13:14:17 -0400285/**
Liu Bo52b1de92012-10-30 17:13:52 +0800286 * unpin_extent_cache - unpin an extent from the cache
Josef Bacik5dc562c2012-08-17 13:14:17 -0400287 * @tree: tree to unpin the extent in
288 * @start: logical offset in the file
289 * @len: length of the extent
290 * @gen: generation that this extent has been modified in
Josef Bacik5dc562c2012-08-17 13:14:17 -0400291 *
292 * Called after an extent has been written to disk properly. Set the generation
293 * to the generation that actually added the file item to the inode so we know
294 * we need to sync this extent when we call fsync().
295 */
296int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
297 u64 gen)
Li Zefan4d2c8f622011-07-14 03:18:33 +0000298{
299 int ret = 0;
300 struct extent_map *em;
Liu Bo4e2f84e2012-08-27 10:52:20 -0600301 bool prealloc = false;
Li Zefan4d2c8f622011-07-14 03:18:33 +0000302
303 write_lock(&tree->lock);
304 em = lookup_extent_mapping(tree, start, len);
305
306 WARN_ON(!em || em->start != start);
307
308 if (!em)
309 goto out;
310
Josef Bacik5dc562c2012-08-17 13:14:17 -0400311 em->generation = gen;
Li Zefan4d2c8f622011-07-14 03:18:33 +0000312 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo4e2f84e2012-08-27 10:52:20 -0600313 em->mod_start = em->start;
314 em->mod_len = em->len;
315
Josef Bacikb11e2342012-12-03 10:58:15 -0500316 if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
Liu Bo4e2f84e2012-08-27 10:52:20 -0600317 prealloc = true;
Josef Bacikb11e2342012-12-03 10:58:15 -0500318 clear_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo4e2f84e2012-08-27 10:52:20 -0600319 }
Li Zefan4d2c8f622011-07-14 03:18:33 +0000320
321 try_merge_map(tree, em);
Liu Bo4e2f84e2012-08-27 10:52:20 -0600322
323 if (prealloc) {
324 em->mod_start = em->start;
325 em->mod_len = em->len;
326 }
327
Chris Masona1ed8352009-09-11 12:27:37 -0400328 free_extent_map(em);
329out:
330 write_unlock(&tree->lock);
331 return ret;
332
333}
334
Josef Bacik201a9032013-01-24 12:02:07 -0500335void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
336{
337 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe Mananacbc0e922014-02-25 14:15:12 +0000338 if (extent_map_in_tree(em))
Josef Bacik222c81d2013-01-28 09:45:20 -0500339 try_merge_map(tree, em);
Josef Bacik201a9032013-01-24 12:02:07 -0500340}
341
Filipe Manana176840b2014-02-25 14:15:13 +0000342static inline void setup_extent_mapping(struct extent_map_tree *tree,
343 struct extent_map *em,
344 int modified)
345{
Elena Reshetova490b54d2017-03-03 10:55:12 +0200346 refcount_inc(&em->refs);
Filipe Manana176840b2014-02-25 14:15:13 +0000347 em->mod_start = em->start;
348 em->mod_len = em->len;
349
350 if (modified)
351 list_move(&em->list, &tree->modified_extents);
352 else
353 try_merge_map(tree, em);
354}
355
Jeff Mahoney1c11b632019-03-27 14:24:12 +0200356static void extent_map_device_set_bits(struct extent_map *em, unsigned bits)
357{
358 struct map_lookup *map = em->map_lookup;
359 u64 stripe_size = em->orig_block_len;
360 int i;
361
362 for (i = 0; i < map->num_stripes; i++) {
363 struct btrfs_bio_stripe *stripe = &map->stripes[i];
364 struct btrfs_device *device = stripe->dev;
365
366 set_extent_bits_nowait(&device->alloc_state, stripe->physical,
367 stripe->physical + stripe_size - 1, bits);
368 }
369}
370
371static void extent_map_device_clear_bits(struct extent_map *em, unsigned bits)
372{
373 struct map_lookup *map = em->map_lookup;
374 u64 stripe_size = em->orig_block_len;
375 int i;
376
377 for (i = 0; i < map->num_stripes; i++) {
378 struct btrfs_bio_stripe *stripe = &map->stripes[i];
379 struct btrfs_device *device = stripe->dev;
380
381 __clear_extent_bit(&device->alloc_state, stripe->physical,
382 stripe->physical + stripe_size - 1, bits,
383 0, 0, NULL, GFP_NOWAIT, NULL);
384 }
385}
386
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400387/**
Nikolay Borisov401bd2d2021-01-22 11:57:52 +0200388 * Add new extent map to the extent tree
389 *
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400390 * @tree: tree to insert new map in
391 * @em: map to insert
Nikolay Borisov401bd2d2021-01-22 11:57:52 +0200392 * @modified: indicate whether the given @em should be added to the
393 * modified list, which indicates the extent needs to be logged
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400394 *
395 * Insert @em into @tree or perform a simple forward/backward merge with
396 * existing mappings. The extent_map struct passed in will be inserted
397 * into the tree directly, with an additional reference taken, or a
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300398 * reference dropped if the merge attempt was successful.
Chris Masona52d9a82007-08-27 16:49:44 -0400399 */
400int add_extent_mapping(struct extent_map_tree *tree,
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400401 struct extent_map *em, int modified)
Chris Masona52d9a82007-08-27 16:49:44 -0400402{
403 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400404
David Sterbad23ea3f2019-03-27 16:19:55 +0100405 lockdep_assert_held_write(&tree->lock);
406
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000407 ret = tree_insert(&tree->map, em);
408 if (ret)
Chris Mason7c2fe322008-08-20 08:51:50 -0400409 goto out;
Filipe David Borba Manana32193c12013-11-25 03:23:51 +0000410
Filipe Manana176840b2014-02-25 14:15:13 +0000411 setup_extent_mapping(tree, em, modified);
Nikolay Borisov88111332019-03-27 14:24:16 +0200412 if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags)) {
Jeff Mahoney1c11b632019-03-27 14:24:12 +0200413 extent_map_device_set_bits(em, CHUNK_ALLOCATED);
Nikolay Borisov88111332019-03-27 14:24:16 +0200414 extent_map_device_clear_bits(em, CHUNK_TRIMMED);
415 }
Chris Masona52d9a82007-08-27 16:49:44 -0400416out:
Chris Masona52d9a82007-08-27 16:49:44 -0400417 return ret;
418}
Chris Masona52d9a82007-08-27 16:49:44 -0400419
Eric Sandeen48a3b632013-04-25 20:41:01 +0000420static struct extent_map *
421__lookup_extent_mapping(struct extent_map_tree *tree,
422 u64 start, u64 len, int strict)
Li Zefaned64f062011-07-14 03:18:15 +0000423{
424 struct extent_map *em;
425 struct rb_node *rb_node;
426 struct rb_node *prev = NULL;
427 struct rb_node *next = NULL;
428 u64 end = range_end(start, len);
429
Liu Bo07e1ce02018-08-23 03:51:52 +0800430 rb_node = __tree_search(&tree->map.rb_root, start, &prev, &next);
Li Zefaned64f062011-07-14 03:18:15 +0000431 if (!rb_node) {
432 if (prev)
433 rb_node = prev;
434 else if (next)
435 rb_node = next;
436 else
437 return NULL;
438 }
439
440 em = rb_entry(rb_node, struct extent_map, rb_node);
441
442 if (strict && !(end > em->start && start < extent_map_end(em)))
443 return NULL;
444
Elena Reshetova490b54d2017-03-03 10:55:12 +0200445 refcount_inc(&em->refs);
Li Zefaned64f062011-07-14 03:18:15 +0000446 return em;
447}
448
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400449/**
450 * lookup_extent_mapping - lookup extent_map
451 * @tree: tree to lookup in
452 * @start: byte offset to start the search
453 * @len: length of the lookup range
454 *
455 * Find and return the first extent_map struct in @tree that intersects the
456 * [start, len] range. There may be additional objects in the tree that
457 * intersect, so check the object returned carefully to make sure that no
458 * additional lookups are needed.
Chris Masona52d9a82007-08-27 16:49:44 -0400459 */
460struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500461 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -0400462{
Li Zefaned64f062011-07-14 03:18:15 +0000463 return __lookup_extent_mapping(tree, start, len, 1);
Chris Masona52d9a82007-08-27 16:49:44 -0400464}
Chris Masona52d9a82007-08-27 16:49:44 -0400465
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400466/**
Chris Masonb917b7c2009-09-18 16:07:03 -0400467 * search_extent_mapping - find a nearby extent map
468 * @tree: tree to lookup in
469 * @start: byte offset to start the search
470 * @len: length of the lookup range
471 *
472 * Find and return the first extent_map struct in @tree that intersects the
473 * [start, len] range.
474 *
475 * If one can't be found, any nearby extent may be returned
476 */
477struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
478 u64 start, u64 len)
479{
Li Zefaned64f062011-07-14 03:18:15 +0000480 return __lookup_extent_mapping(tree, start, len, 0);
Chris Masonb917b7c2009-09-18 16:07:03 -0400481}
482
483/**
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400484 * remove_extent_mapping - removes an extent_map from the extent tree
485 * @tree: extent tree to remove from
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -0800486 * @em: extent map being removed
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400487 *
488 * Removes @em from @tree. No reference counts are dropped, and no checks
489 * are done to see if the range is in use
Chris Masona52d9a82007-08-27 16:49:44 -0400490 */
zhong jiangc1766dd2018-09-13 11:01:15 +0800491void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
Chris Masona52d9a82007-08-27 16:49:44 -0400492{
Chris Mason7f3c74f2008-07-18 12:01:11 -0400493 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
Liu Bo07e1ce02018-08-23 03:51:52 +0800494 rb_erase_cached(&em->rb_node, &tree->map);
Josef Bacikff44c6e2012-09-14 12:59:20 -0400495 if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
496 list_del_init(&em->list);
Jeff Mahoney1c11b632019-03-27 14:24:12 +0200497 if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
498 extent_map_device_clear_bits(em, CHUNK_ALLOCATED);
Filipe Mananacbc0e922014-02-25 14:15:12 +0000499 RB_CLEAR_NODE(&em->rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400500}
Filipe Manana176840b2014-02-25 14:15:13 +0000501
502void replace_extent_mapping(struct extent_map_tree *tree,
503 struct extent_map *cur,
504 struct extent_map *new,
505 int modified)
506{
507 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));
508 ASSERT(extent_map_in_tree(cur));
509 if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))
510 list_del_init(&cur->list);
Liu Bo07e1ce02018-08-23 03:51:52 +0800511 rb_replace_node_cached(&cur->rb_node, &new->rb_node, &tree->map);
Filipe Manana176840b2014-02-25 14:15:13 +0000512 RB_CLEAR_NODE(&cur->rb_node);
513
514 setup_extent_mapping(tree, new, modified);
515}
Liu Boc04e61b2018-01-05 12:51:11 -0700516
517static struct extent_map *next_extent_map(struct extent_map *em)
518{
519 struct rb_node *next;
520
521 next = rb_next(&em->rb_node);
522 if (!next)
523 return NULL;
524 return container_of(next, struct extent_map, rb_node);
525}
526
527static struct extent_map *prev_extent_map(struct extent_map *em)
528{
529 struct rb_node *prev;
530
531 prev = rb_prev(&em->rb_node);
532 if (!prev)
533 return NULL;
534 return container_of(prev, struct extent_map, rb_node);
535}
536
Andrea Gelmini52042d82018-11-28 12:05:13 +0100537/*
538 * Helper for btrfs_get_extent. Given an existing extent in the tree,
Liu Boc04e61b2018-01-05 12:51:11 -0700539 * the existing extent is the nearest extent to map_start,
540 * and an extent that you want to insert, deal with overlap and insert
541 * the best fitted new extent into the tree.
542 */
Liu Bo5f4791f2018-01-05 12:51:17 -0700543static noinline int merge_extent_mapping(struct extent_map_tree *em_tree,
544 struct extent_map *existing,
545 struct extent_map *em,
546 u64 map_start)
Liu Boc04e61b2018-01-05 12:51:11 -0700547{
548 struct extent_map *prev;
549 struct extent_map *next;
550 u64 start;
551 u64 end;
552 u64 start_diff;
553
554 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
555
556 if (existing->start > map_start) {
557 next = existing;
558 prev = prev_extent_map(next);
559 } else {
560 prev = existing;
561 next = next_extent_map(prev);
562 }
563
564 start = prev ? extent_map_end(prev) : em->start;
565 start = max_t(u64, start, em->start);
566 end = next ? next->start : extent_map_end(em);
567 end = min_t(u64, end, extent_map_end(em));
568 start_diff = start - em->start;
569 em->start = start;
570 em->len = end - start;
571 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
572 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
573 em->block_start += start_diff;
574 em->block_len = em->len;
575 }
576 return add_extent_mapping(em_tree, em, 0);
577}
578
579/**
Nikolay Borisov9ad37bb2021-01-22 11:57:53 +0200580 * Add extent mapping into em_tree
581 *
582 * @fs_info: the filesystem
583 * @em_tree: extent tree into which we want to insert the extent mapping
584 * @em_in: extent we are inserting
585 * @start: start of the logical range btrfs_get_extent() is requesting
586 * @len: length of the logical range btrfs_get_extent() is requesting
Liu Boc04e61b2018-01-05 12:51:11 -0700587 *
588 * Note that @em_in's range may be different from [start, start+len),
589 * but they must be overlapped.
590 *
591 * Insert @em_in into @em_tree. In case there is an overlapping range, handle
592 * the -EEXIST by either:
593 * a) Returning the existing extent in @em_in if @start is within the
594 * existing em.
595 * b) Merge the existing extent with @em_in passed in.
596 *
597 * Return 0 on success, otherwise -EEXIST.
598 *
599 */
David Sterbaf46b24c2018-04-03 21:45:57 +0200600int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info,
601 struct extent_map_tree *em_tree,
Liu Boc04e61b2018-01-05 12:51:11 -0700602 struct extent_map **em_in, u64 start, u64 len)
603{
604 int ret;
605 struct extent_map *em = *em_in;
606
607 ret = add_extent_mapping(em_tree, em, 0);
608 /* it is possible that someone inserted the extent into the tree
609 * while we had the lock dropped. It is also possible that
610 * an overlapping map exists in the tree
611 */
612 if (ret == -EEXIST) {
613 struct extent_map *existing;
614
615 ret = 0;
616
617 existing = search_extent_mapping(em_tree, start, len);
Liu Bo393da912018-01-05 12:51:16 -0700618
David Sterbaf46b24c2018-04-03 21:45:57 +0200619 trace_btrfs_handle_em_exist(fs_info, existing, em, start, len);
Liu Bo393da912018-01-05 12:51:16 -0700620
Liu Boc04e61b2018-01-05 12:51:11 -0700621 /*
622 * existing will always be non-NULL, since there must be
623 * extent causing the -EEXIST.
624 */
625 if (start >= existing->start &&
626 start < extent_map_end(existing)) {
627 free_extent_map(em);
628 *em_in = existing;
629 ret = 0;
630 } else {
Liu Bo9a7e10e2018-01-05 12:51:15 -0700631 u64 orig_start = em->start;
632 u64 orig_len = em->len;
633
Liu Boc04e61b2018-01-05 12:51:11 -0700634 /*
635 * The existing extent map is the one nearest to
636 * the [start, start + len) range which overlaps
637 */
638 ret = merge_extent_mapping(em_tree, existing,
639 em, start);
Liu Boc04e61b2018-01-05 12:51:11 -0700640 if (ret) {
641 free_extent_map(em);
642 *em_in = NULL;
Liu Bo9a7e10e2018-01-05 12:51:15 -0700643 WARN_ONCE(ret,
644"unexpected error %d: merge existing(start %llu len %llu) with em(start %llu len %llu)\n",
645 ret, existing->start, existing->len,
646 orig_start, orig_len);
Liu Boc04e61b2018-01-05 12:51:11 -0700647 }
Liu Bo9a7e10e2018-01-05 12:51:15 -0700648 free_extent_map(existing);
Liu Boc04e61b2018-01-05 12:51:11 -0700649 }
650 }
651
652 ASSERT(ret == 0 || ret == -EEXIST);
653 return ret;
654}