Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 2 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3 | #include <linux/err.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4 | #include <linux/slab.h> |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 5 | #include <linux/spinlock.h> |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 6 | #include "ctree.h" |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 7 | #include "volumes.h" |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 8 | #include "extent_map.h" |
Anand Jain | ebb8765 | 2016-03-10 17:26:59 +0800 | [diff] [blame] | 9 | #include "compression.h" |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 10 | |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 11 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 12 | static struct kmem_cache *extent_map_cache; |
Chris Mason | ca66462 | 2007-11-27 11:16:35 -0500 | [diff] [blame] | 13 | |
Wyatt Banks | 2f4cbe6 | 2007-11-19 10:22:33 -0500 | [diff] [blame] | 14 | int __init extent_map_init(void) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 15 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 16 | extent_map_cache = kmem_cache_create("btrfs_extent_map", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 17 | sizeof(struct extent_map), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 18 | SLAB_MEM_SPREAD, NULL); |
Wyatt Banks | 2f4cbe6 | 2007-11-19 10:22:33 -0500 | [diff] [blame] | 19 | if (!extent_map_cache) |
| 20 | return -ENOMEM; |
Wyatt Banks | 2f4cbe6 | 2007-11-19 10:22:33 -0500 | [diff] [blame] | 21 | return 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 22 | } |
| 23 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 24 | void __cold extent_map_exit(void) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 25 | { |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 26 | kmem_cache_destroy(extent_map_cache); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 27 | } |
| 28 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 29 | /** |
| 30 | * extent_map_tree_init - initialize extent map tree |
| 31 | * @tree: tree to initialize |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 32 | * |
| 33 | * Initialize the extent tree @tree. Should be called for each new inode |
| 34 | * or other user of the extent_map interface. |
| 35 | */ |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 36 | void extent_map_tree_init(struct extent_map_tree *tree) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 37 | { |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 38 | tree->map = RB_ROOT_CACHED; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 39 | INIT_LIST_HEAD(&tree->modified_extents); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 40 | rwlock_init(&tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 41 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 42 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 43 | /** |
| 44 | * alloc_extent_map - allocate new extent map structure |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 45 | * |
| 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 Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 50 | struct extent_map *alloc_extent_map(void) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 51 | { |
| 52 | struct extent_map *em; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 53 | em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS); |
Tsutomu Itoh | c26a920 | 2011-02-14 00:45:29 +0000 | [diff] [blame] | 54 | if (!em) |
| 55 | return NULL; |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 56 | RB_CLEAR_NODE(&em->rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 57 | em->flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 58 | em->compress_type = BTRFS_COMPRESS_NONE; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 59 | em->generation = 0; |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 60 | refcount_set(&em->refs, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 61 | INIT_LIST_HEAD(&em->list); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 62 | return em; |
| 63 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 64 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 65 | /** |
| 66 | * free_extent_map - drop reference count of an extent_map |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 67 | * @em: extent map being released |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 68 | * |
| 69 | * Drops the reference out on @em by one and free the structure |
| 70 | * if the reference count hits zero. |
| 71 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 72 | void free_extent_map(struct extent_map *em) |
| 73 | { |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 74 | if (!em) |
| 75 | return; |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 76 | WARN_ON(refcount_read(&em->refs) == 0); |
| 77 | if (refcount_dec_and_test(&em->refs)) { |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 78 | WARN_ON(extent_map_in_tree(em)); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 79 | WARN_ON(!list_empty(&em->list)); |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 80 | if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags)) |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 81 | kfree(em->map_lookup); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 82 | kmem_cache_free(extent_map_cache, em); |
| 83 | } |
| 84 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 85 | |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 86 | /* simple helper to do math around the end of an extent, handling wrap */ |
| 87 | static u64 range_end(u64 start, u64 len) |
| 88 | { |
| 89 | if (start + len < start) |
| 90 | return (u64)-1; |
| 91 | return start + len; |
| 92 | } |
| 93 | |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 94 | static int tree_insert(struct rb_root_cached *root, struct extent_map *em) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 95 | { |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 96 | struct rb_node **p = &root->rb_root.rb_node; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 97 | struct rb_node *parent = NULL; |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 98 | struct extent_map *entry = NULL; |
| 99 | struct rb_node *orig_parent = NULL; |
| 100 | u64 end = range_end(em->start, em->len); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 101 | bool leftmost = true; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 102 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 103 | while (*p) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 104 | parent = *p; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 105 | entry = rb_entry(parent, struct extent_map, rb_node); |
| 106 | |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 107 | if (em->start < entry->start) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 108 | p = &(*p)->rb_left; |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 109 | } else if (em->start >= extent_map_end(entry)) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 110 | p = &(*p)->rb_right; |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 111 | leftmost = false; |
| 112 | } else { |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 113 | return -EEXIST; |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 114 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 115 | } |
| 116 | |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 117 | 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 Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 136 | rb_link_node(&em->rb_node, orig_parent, p); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 137 | rb_insert_color_cached(&em->rb_node, root, leftmost); |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 138 | return 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 141 | /* |
| 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 Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 145 | static struct rb_node *__tree_search(struct rb_root *root, u64 offset, |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 146 | struct rb_node **prev_ret, |
| 147 | struct rb_node **next_ret) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 148 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 149 | struct rb_node *n = root->rb_node; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 150 | struct rb_node *prev = NULL; |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 151 | struct rb_node *orig_prev = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 152 | struct extent_map *entry; |
| 153 | struct extent_map *prev_entry = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 154 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 155 | while (n) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 156 | entry = rb_entry(n, struct extent_map, rb_node); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 157 | prev = n; |
| 158 | prev_entry = entry; |
| 159 | |
| 160 | if (offset < entry->start) |
| 161 | n = n->rb_left; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 162 | else if (offset >= extent_map_end(entry)) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 163 | n = n->rb_right; |
| 164 | else |
| 165 | return n; |
| 166 | } |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 167 | |
| 168 | if (prev_ret) { |
| 169 | orig_prev = prev; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 170 | while (prev && offset >= extent_map_end(prev_entry)) { |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 171 | prev = rb_next(prev); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 172 | prev_entry = rb_entry(prev, struct extent_map, rb_node); |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 173 | } |
| 174 | *prev_ret = prev; |
| 175 | prev = orig_prev; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 176 | } |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 177 | |
| 178 | if (next_ret) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 179 | prev_entry = rb_entry(prev, struct extent_map, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 180 | while (prev && offset < prev_entry->start) { |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 181 | prev = rb_prev(prev); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 182 | prev_entry = rb_entry(prev, struct extent_map, rb_node); |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 183 | } |
| 184 | *next_ret = prev; |
| 185 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 186 | return NULL; |
| 187 | } |
| 188 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 189 | /* check to see if two extent_map structs are adjacent and safe to merge */ |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 190 | static int mergable_maps(struct extent_map *prev, struct extent_map *next) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 191 | { |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 192 | if (test_bit(EXTENT_FLAG_PINNED, &prev->flags)) |
| 193 | return 0; |
| 194 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 195 | /* |
| 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 Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 202 | if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) || |
| 203 | test_bit(EXTENT_FLAG_LOGGING, &next->flags)) |
| 204 | return 0; |
| 205 | |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 206 | /* |
| 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 Borisov | 951e05a | 2019-01-08 16:53:46 +0200 | [diff] [blame] | 214 | ASSERT(next->block_start != EXTENT_MAP_DELALLOC && |
| 215 | prev->block_start != EXTENT_MAP_DELALLOC); |
| 216 | |
David Sterba | c3e1490 | 2019-05-10 17:48:30 +0200 | [diff] [blame] | 217 | 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 Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 221 | if (extent_map_end(prev) == next->start && |
| 222 | prev->flags == next->flags && |
David Sterba | c3e1490 | 2019-05-10 17:48:30 +0200 | [diff] [blame] | 223 | prev->map_lookup == next->map_lookup && |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 224 | ((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 Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 228 | (next->block_start < EXTENT_MAP_LAST_BYTE - 1 && |
| 229 | next->block_start == extent_map_block_end(prev)))) { |
| 230 | return 1; |
| 231 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 232 | return 0; |
| 233 | } |
| 234 | |
Li Zefan | 4d2c8f62 | 2011-07-14 03:18:33 +0000 | [diff] [blame] | 235 | static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em) |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 236 | { |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 237 | struct extent_map *merge = NULL; |
| 238 | struct rb_node *rb; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 239 | |
Filipe Manana | ac05ca9 | 2020-01-31 14:06:07 +0000 | [diff] [blame] | 240 | /* |
| 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 Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 251 | 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 Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 257 | em->orig_start = merge->orig_start; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 258 | em->len += merge->len; |
| 259 | em->block_len += merge->block_len; |
| 260 | em->block_start = merge->block_start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 261 | 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 Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 264 | |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 265 | rb_erase_cached(&merge->rb_node, &tree->map); |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 266 | RB_CLEAR_NODE(&merge->rb_node); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 267 | 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 Manana | d527afe | 2013-11-30 11:28:35 +0000 | [diff] [blame] | 276 | em->block_len += merge->block_len; |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 277 | rb_erase_cached(&merge->rb_node, &tree->map); |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 278 | RB_CLEAR_NODE(&merge->rb_node); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 279 | em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start; |
| 280 | em->generation = max(em->generation, merge->generation); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 281 | free_extent_map(merge); |
| 282 | } |
Li Zefan | 4d2c8f62 | 2011-07-14 03:18:33 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 285 | /** |
Liu Bo | 52b1de9 | 2012-10-30 17:13:52 +0800 | [diff] [blame] | 286 | * unpin_extent_cache - unpin an extent from the cache |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 287 | * @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 Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 291 | * |
| 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 | */ |
| 296 | int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len, |
| 297 | u64 gen) |
Li Zefan | 4d2c8f62 | 2011-07-14 03:18:33 +0000 | [diff] [blame] | 298 | { |
| 299 | int ret = 0; |
| 300 | struct extent_map *em; |
Liu Bo | 4e2f84e | 2012-08-27 10:52:20 -0600 | [diff] [blame] | 301 | bool prealloc = false; |
Li Zefan | 4d2c8f62 | 2011-07-14 03:18:33 +0000 | [diff] [blame] | 302 | |
| 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 Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 311 | em->generation = gen; |
Li Zefan | 4d2c8f62 | 2011-07-14 03:18:33 +0000 | [diff] [blame] | 312 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
Liu Bo | 4e2f84e | 2012-08-27 10:52:20 -0600 | [diff] [blame] | 313 | em->mod_start = em->start; |
| 314 | em->mod_len = em->len; |
| 315 | |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 316 | if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) { |
Liu Bo | 4e2f84e | 2012-08-27 10:52:20 -0600 | [diff] [blame] | 317 | prealloc = true; |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 318 | clear_bit(EXTENT_FLAG_FILLING, &em->flags); |
Liu Bo | 4e2f84e | 2012-08-27 10:52:20 -0600 | [diff] [blame] | 319 | } |
Li Zefan | 4d2c8f62 | 2011-07-14 03:18:33 +0000 | [diff] [blame] | 320 | |
| 321 | try_merge_map(tree, em); |
Liu Bo | 4e2f84e | 2012-08-27 10:52:20 -0600 | [diff] [blame] | 322 | |
| 323 | if (prealloc) { |
| 324 | em->mod_start = em->start; |
| 325 | em->mod_len = em->len; |
| 326 | } |
| 327 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 328 | free_extent_map(em); |
| 329 | out: |
| 330 | write_unlock(&tree->lock); |
| 331 | return ret; |
| 332 | |
| 333 | } |
| 334 | |
Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 335 | void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em) |
| 336 | { |
| 337 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 338 | if (extent_map_in_tree(em)) |
Josef Bacik | 222c81d | 2013-01-28 09:45:20 -0500 | [diff] [blame] | 339 | try_merge_map(tree, em); |
Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 340 | } |
| 341 | |
Filipe Manana | 176840b | 2014-02-25 14:15:13 +0000 | [diff] [blame] | 342 | static inline void setup_extent_mapping(struct extent_map_tree *tree, |
| 343 | struct extent_map *em, |
| 344 | int modified) |
| 345 | { |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 346 | refcount_inc(&em->refs); |
Filipe Manana | 176840b | 2014-02-25 14:15:13 +0000 | [diff] [blame] | 347 | 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 Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 356 | static 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 | |
| 371 | static 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 Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 387 | /** |
| 388 | * add_extent_mapping - add new extent map to the extent tree |
| 389 | * @tree: tree to insert new map in |
| 390 | * @em: map to insert |
| 391 | * |
| 392 | * Insert @em into @tree or perform a simple forward/backward merge with |
| 393 | * existing mappings. The extent_map struct passed in will be inserted |
| 394 | * into the tree directly, with an additional reference taken, or a |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 395 | * reference dropped if the merge attempt was successful. |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 396 | */ |
| 397 | int add_extent_mapping(struct extent_map_tree *tree, |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 398 | struct extent_map *em, int modified) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 399 | { |
| 400 | int ret = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 401 | |
David Sterba | d23ea3f | 2019-03-27 16:19:55 +0100 | [diff] [blame] | 402 | lockdep_assert_held_write(&tree->lock); |
| 403 | |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 404 | ret = tree_insert(&tree->map, em); |
| 405 | if (ret) |
Chris Mason | 7c2fe32 | 2008-08-20 08:51:50 -0400 | [diff] [blame] | 406 | goto out; |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 407 | |
Filipe Manana | 176840b | 2014-02-25 14:15:13 +0000 | [diff] [blame] | 408 | setup_extent_mapping(tree, em, modified); |
Nikolay Borisov | 8811133 | 2019-03-27 14:24:16 +0200 | [diff] [blame] | 409 | if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags)) { |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 410 | extent_map_device_set_bits(em, CHUNK_ALLOCATED); |
Nikolay Borisov | 8811133 | 2019-03-27 14:24:16 +0200 | [diff] [blame] | 411 | extent_map_device_clear_bits(em, CHUNK_TRIMMED); |
| 412 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 413 | out: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 414 | return ret; |
| 415 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 416 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 417 | static struct extent_map * |
| 418 | __lookup_extent_mapping(struct extent_map_tree *tree, |
| 419 | u64 start, u64 len, int strict) |
Li Zefan | ed64f06 | 2011-07-14 03:18:15 +0000 | [diff] [blame] | 420 | { |
| 421 | struct extent_map *em; |
| 422 | struct rb_node *rb_node; |
| 423 | struct rb_node *prev = NULL; |
| 424 | struct rb_node *next = NULL; |
| 425 | u64 end = range_end(start, len); |
| 426 | |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 427 | rb_node = __tree_search(&tree->map.rb_root, start, &prev, &next); |
Li Zefan | ed64f06 | 2011-07-14 03:18:15 +0000 | [diff] [blame] | 428 | if (!rb_node) { |
| 429 | if (prev) |
| 430 | rb_node = prev; |
| 431 | else if (next) |
| 432 | rb_node = next; |
| 433 | else |
| 434 | return NULL; |
| 435 | } |
| 436 | |
| 437 | em = rb_entry(rb_node, struct extent_map, rb_node); |
| 438 | |
| 439 | if (strict && !(end > em->start && start < extent_map_end(em))) |
| 440 | return NULL; |
| 441 | |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 442 | refcount_inc(&em->refs); |
Li Zefan | ed64f06 | 2011-07-14 03:18:15 +0000 | [diff] [blame] | 443 | return em; |
| 444 | } |
| 445 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 446 | /** |
| 447 | * lookup_extent_mapping - lookup extent_map |
| 448 | * @tree: tree to lookup in |
| 449 | * @start: byte offset to start the search |
| 450 | * @len: length of the lookup range |
| 451 | * |
| 452 | * Find and return the first extent_map struct in @tree that intersects the |
| 453 | * [start, len] range. There may be additional objects in the tree that |
| 454 | * intersect, so check the object returned carefully to make sure that no |
| 455 | * additional lookups are needed. |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 456 | */ |
| 457 | struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 458 | u64 start, u64 len) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 459 | { |
Li Zefan | ed64f06 | 2011-07-14 03:18:15 +0000 | [diff] [blame] | 460 | return __lookup_extent_mapping(tree, start, len, 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 461 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 462 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 463 | /** |
Chris Mason | b917b7c | 2009-09-18 16:07:03 -0400 | [diff] [blame] | 464 | * search_extent_mapping - find a nearby extent map |
| 465 | * @tree: tree to lookup in |
| 466 | * @start: byte offset to start the search |
| 467 | * @len: length of the lookup range |
| 468 | * |
| 469 | * Find and return the first extent_map struct in @tree that intersects the |
| 470 | * [start, len] range. |
| 471 | * |
| 472 | * If one can't be found, any nearby extent may be returned |
| 473 | */ |
| 474 | struct extent_map *search_extent_mapping(struct extent_map_tree *tree, |
| 475 | u64 start, u64 len) |
| 476 | { |
Li Zefan | ed64f06 | 2011-07-14 03:18:15 +0000 | [diff] [blame] | 477 | return __lookup_extent_mapping(tree, start, len, 0); |
Chris Mason | b917b7c | 2009-09-18 16:07:03 -0400 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /** |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 481 | * remove_extent_mapping - removes an extent_map from the extent tree |
| 482 | * @tree: extent tree to remove from |
Adam Buchbinder | bb7ab3b | 2016-03-04 11:23:12 -0800 | [diff] [blame] | 483 | * @em: extent map being removed |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 484 | * |
| 485 | * Removes @em from @tree. No reference counts are dropped, and no checks |
| 486 | * are done to see if the range is in use |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 487 | */ |
zhong jiang | c1766dd | 2018-09-13 11:01:15 +0800 | [diff] [blame] | 488 | void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 489 | { |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 490 | WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags)); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 491 | rb_erase_cached(&em->rb_node, &tree->map); |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 492 | if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags)) |
| 493 | list_del_init(&em->list); |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 494 | if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags)) |
| 495 | extent_map_device_clear_bits(em, CHUNK_ALLOCATED); |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 496 | RB_CLEAR_NODE(&em->rb_node); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 497 | } |
Filipe Manana | 176840b | 2014-02-25 14:15:13 +0000 | [diff] [blame] | 498 | |
| 499 | void replace_extent_mapping(struct extent_map_tree *tree, |
| 500 | struct extent_map *cur, |
| 501 | struct extent_map *new, |
| 502 | int modified) |
| 503 | { |
| 504 | WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags)); |
| 505 | ASSERT(extent_map_in_tree(cur)); |
| 506 | if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags)) |
| 507 | list_del_init(&cur->list); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 508 | rb_replace_node_cached(&cur->rb_node, &new->rb_node, &tree->map); |
Filipe Manana | 176840b | 2014-02-25 14:15:13 +0000 | [diff] [blame] | 509 | RB_CLEAR_NODE(&cur->rb_node); |
| 510 | |
| 511 | setup_extent_mapping(tree, new, modified); |
| 512 | } |
Liu Bo | c04e61b | 2018-01-05 12:51:11 -0700 | [diff] [blame] | 513 | |
| 514 | static struct extent_map *next_extent_map(struct extent_map *em) |
| 515 | { |
| 516 | struct rb_node *next; |
| 517 | |
| 518 | next = rb_next(&em->rb_node); |
| 519 | if (!next) |
| 520 | return NULL; |
| 521 | return container_of(next, struct extent_map, rb_node); |
| 522 | } |
| 523 | |
| 524 | static struct extent_map *prev_extent_map(struct extent_map *em) |
| 525 | { |
| 526 | struct rb_node *prev; |
| 527 | |
| 528 | prev = rb_prev(&em->rb_node); |
| 529 | if (!prev) |
| 530 | return NULL; |
| 531 | return container_of(prev, struct extent_map, rb_node); |
| 532 | } |
| 533 | |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 534 | /* |
| 535 | * Helper for btrfs_get_extent. Given an existing extent in the tree, |
Liu Bo | c04e61b | 2018-01-05 12:51:11 -0700 | [diff] [blame] | 536 | * the existing extent is the nearest extent to map_start, |
| 537 | * and an extent that you want to insert, deal with overlap and insert |
| 538 | * the best fitted new extent into the tree. |
| 539 | */ |
Liu Bo | 5f4791f | 2018-01-05 12:51:17 -0700 | [diff] [blame] | 540 | static noinline int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 541 | struct extent_map *existing, |
| 542 | struct extent_map *em, |
| 543 | u64 map_start) |
Liu Bo | c04e61b | 2018-01-05 12:51:11 -0700 | [diff] [blame] | 544 | { |
| 545 | struct extent_map *prev; |
| 546 | struct extent_map *next; |
| 547 | u64 start; |
| 548 | u64 end; |
| 549 | u64 start_diff; |
| 550 | |
| 551 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 552 | |
| 553 | if (existing->start > map_start) { |
| 554 | next = existing; |
| 555 | prev = prev_extent_map(next); |
| 556 | } else { |
| 557 | prev = existing; |
| 558 | next = next_extent_map(prev); |
| 559 | } |
| 560 | |
| 561 | start = prev ? extent_map_end(prev) : em->start; |
| 562 | start = max_t(u64, start, em->start); |
| 563 | end = next ? next->start : extent_map_end(em); |
| 564 | end = min_t(u64, end, extent_map_end(em)); |
| 565 | start_diff = start - em->start; |
| 566 | em->start = start; |
| 567 | em->len = end - start; |
| 568 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 569 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 570 | em->block_start += start_diff; |
| 571 | em->block_len = em->len; |
| 572 | } |
| 573 | return add_extent_mapping(em_tree, em, 0); |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * btrfs_add_extent_mapping - add extent mapping into em_tree |
David Sterba | f46b24c | 2018-04-03 21:45:57 +0200 | [diff] [blame] | 578 | * @fs_info - used for tracepoint |
Liu Bo | c04e61b | 2018-01-05 12:51:11 -0700 | [diff] [blame] | 579 | * @em_tree - the extent tree into which we want to insert the extent mapping |
| 580 | * @em_in - extent we are inserting |
| 581 | * @start - start of the logical range btrfs_get_extent() is requesting |
| 582 | * @len - length of the logical range btrfs_get_extent() is requesting |
| 583 | * |
| 584 | * Note that @em_in's range may be different from [start, start+len), |
| 585 | * but they must be overlapped. |
| 586 | * |
| 587 | * Insert @em_in into @em_tree. In case there is an overlapping range, handle |
| 588 | * the -EEXIST by either: |
| 589 | * a) Returning the existing extent in @em_in if @start is within the |
| 590 | * existing em. |
| 591 | * b) Merge the existing extent with @em_in passed in. |
| 592 | * |
| 593 | * Return 0 on success, otherwise -EEXIST. |
| 594 | * |
| 595 | */ |
David Sterba | f46b24c | 2018-04-03 21:45:57 +0200 | [diff] [blame] | 596 | int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info, |
| 597 | struct extent_map_tree *em_tree, |
Liu Bo | c04e61b | 2018-01-05 12:51:11 -0700 | [diff] [blame] | 598 | struct extent_map **em_in, u64 start, u64 len) |
| 599 | { |
| 600 | int ret; |
| 601 | struct extent_map *em = *em_in; |
| 602 | |
| 603 | ret = add_extent_mapping(em_tree, em, 0); |
| 604 | /* it is possible that someone inserted the extent into the tree |
| 605 | * while we had the lock dropped. It is also possible that |
| 606 | * an overlapping map exists in the tree |
| 607 | */ |
| 608 | if (ret == -EEXIST) { |
| 609 | struct extent_map *existing; |
| 610 | |
| 611 | ret = 0; |
| 612 | |
| 613 | existing = search_extent_mapping(em_tree, start, len); |
Liu Bo | 393da91 | 2018-01-05 12:51:16 -0700 | [diff] [blame] | 614 | |
David Sterba | f46b24c | 2018-04-03 21:45:57 +0200 | [diff] [blame] | 615 | trace_btrfs_handle_em_exist(fs_info, existing, em, start, len); |
Liu Bo | 393da91 | 2018-01-05 12:51:16 -0700 | [diff] [blame] | 616 | |
Liu Bo | c04e61b | 2018-01-05 12:51:11 -0700 | [diff] [blame] | 617 | /* |
| 618 | * existing will always be non-NULL, since there must be |
| 619 | * extent causing the -EEXIST. |
| 620 | */ |
| 621 | if (start >= existing->start && |
| 622 | start < extent_map_end(existing)) { |
| 623 | free_extent_map(em); |
| 624 | *em_in = existing; |
| 625 | ret = 0; |
| 626 | } else { |
Liu Bo | 9a7e10e | 2018-01-05 12:51:15 -0700 | [diff] [blame] | 627 | u64 orig_start = em->start; |
| 628 | u64 orig_len = em->len; |
| 629 | |
Liu Bo | c04e61b | 2018-01-05 12:51:11 -0700 | [diff] [blame] | 630 | /* |
| 631 | * The existing extent map is the one nearest to |
| 632 | * the [start, start + len) range which overlaps |
| 633 | */ |
| 634 | ret = merge_extent_mapping(em_tree, existing, |
| 635 | em, start); |
Liu Bo | c04e61b | 2018-01-05 12:51:11 -0700 | [diff] [blame] | 636 | if (ret) { |
| 637 | free_extent_map(em); |
| 638 | *em_in = NULL; |
Liu Bo | 9a7e10e | 2018-01-05 12:51:15 -0700 | [diff] [blame] | 639 | WARN_ONCE(ret, |
| 640 | "unexpected error %d: merge existing(start %llu len %llu) with em(start %llu len %llu)\n", |
| 641 | ret, existing->start, existing->len, |
| 642 | orig_start, orig_len); |
Liu Bo | c04e61b | 2018-01-05 12:51:11 -0700 | [diff] [blame] | 643 | } |
Liu Bo | 9a7e10e | 2018-01-05 12:51:15 -0700 | [diff] [blame] | 644 | free_extent_map(existing); |
Liu Bo | c04e61b | 2018-01-05 12:51:11 -0700 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
| 648 | ASSERT(ret == 0 || ret == -EEXIST); |
| 649 | return ret; |
| 650 | } |