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" |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 7 | #include "extent_map.h" |
Anand Jain | ebb8765 | 2016-03-10 17:26:59 +0800 | [diff] [blame] | 8 | #include "compression.h" |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 9 | |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 10 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 11 | static struct kmem_cache *extent_map_cache; |
Chris Mason | ca66462 | 2007-11-27 11:16:35 -0500 | [diff] [blame] | 12 | |
Wyatt Banks | 2f4cbe6 | 2007-11-19 10:22:33 -0500 | [diff] [blame] | 13 | int __init extent_map_init(void) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 14 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 15 | extent_map_cache = kmem_cache_create("btrfs_extent_map", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 16 | sizeof(struct extent_map), 0, |
Nikolay Borisov | fba4b69 | 2016-06-23 21:17:08 +0300 | [diff] [blame] | 17 | SLAB_MEM_SPREAD, NULL); |
Wyatt Banks | 2f4cbe6 | 2007-11-19 10:22:33 -0500 | [diff] [blame] | 18 | if (!extent_map_cache) |
| 19 | return -ENOMEM; |
Wyatt Banks | 2f4cbe6 | 2007-11-19 10:22:33 -0500 | [diff] [blame] | 20 | return 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 21 | } |
| 22 | |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 23 | void __cold extent_map_exit(void) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 24 | { |
Kinglong Mee | 5598e90 | 2016-01-29 21:36:35 +0800 | [diff] [blame] | 25 | kmem_cache_destroy(extent_map_cache); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 26 | } |
| 27 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 28 | /** |
| 29 | * extent_map_tree_init - initialize extent map tree |
| 30 | * @tree: tree to initialize |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 31 | * |
| 32 | * Initialize the extent tree @tree. Should be called for each new inode |
| 33 | * or other user of the extent_map interface. |
| 34 | */ |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 35 | void extent_map_tree_init(struct extent_map_tree *tree) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 36 | { |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 37 | tree->map = RB_ROOT_CACHED; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 38 | INIT_LIST_HEAD(&tree->modified_extents); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 39 | rwlock_init(&tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 40 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 41 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 42 | /** |
| 43 | * alloc_extent_map - allocate new extent map structure |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 44 | * |
| 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 Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 49 | struct extent_map *alloc_extent_map(void) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 50 | { |
| 51 | struct extent_map *em; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 52 | em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS); |
Tsutomu Itoh | c26a920 | 2011-02-14 00:45:29 +0000 | [diff] [blame] | 53 | if (!em) |
| 54 | return NULL; |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 55 | RB_CLEAR_NODE(&em->rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 56 | em->flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 57 | em->compress_type = BTRFS_COMPRESS_NONE; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 58 | em->generation = 0; |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 59 | refcount_set(&em->refs, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 60 | INIT_LIST_HEAD(&em->list); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 61 | return em; |
| 62 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 63 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 64 | /** |
| 65 | * free_extent_map - drop reference count of an extent_map |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 66 | * @em: extent map being released |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 67 | * |
| 68 | * Drops the reference out on @em by one and free the structure |
| 69 | * if the reference count hits zero. |
| 70 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 71 | void free_extent_map(struct extent_map *em) |
| 72 | { |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 73 | if (!em) |
| 74 | return; |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 75 | WARN_ON(refcount_read(&em->refs) == 0); |
| 76 | if (refcount_dec_and_test(&em->refs)) { |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 77 | WARN_ON(extent_map_in_tree(em)); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 78 | WARN_ON(!list_empty(&em->list)); |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 79 | if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags)) |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 80 | kfree(em->map_lookup); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 81 | kmem_cache_free(extent_map_cache, em); |
| 82 | } |
| 83 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 84 | |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 85 | /* simple helper to do math around the end of an extent, handling wrap */ |
| 86 | static u64 range_end(u64 start, u64 len) |
| 87 | { |
| 88 | if (start + len < start) |
| 89 | return (u64)-1; |
| 90 | return start + len; |
| 91 | } |
| 92 | |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 93 | 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] | 94 | { |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 95 | struct rb_node **p = &root->rb_root.rb_node; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 96 | struct rb_node *parent = NULL; |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 97 | struct extent_map *entry = NULL; |
| 98 | struct rb_node *orig_parent = NULL; |
| 99 | u64 end = range_end(em->start, em->len); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 100 | bool leftmost = true; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 101 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 102 | while (*p) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 103 | parent = *p; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 104 | entry = rb_entry(parent, struct extent_map, rb_node); |
| 105 | |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 106 | if (em->start < entry->start) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 107 | p = &(*p)->rb_left; |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 108 | } else if (em->start >= extent_map_end(entry)) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 109 | p = &(*p)->rb_right; |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 110 | leftmost = false; |
| 111 | } else { |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 112 | return -EEXIST; |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 113 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 114 | } |
| 115 | |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 116 | orig_parent = parent; |
| 117 | while (parent && em->start >= extent_map_end(entry)) { |
| 118 | parent = rb_next(parent); |
| 119 | entry = rb_entry(parent, struct extent_map, rb_node); |
| 120 | } |
| 121 | if (parent) |
| 122 | if (end > entry->start && em->start < extent_map_end(entry)) |
| 123 | return -EEXIST; |
| 124 | |
| 125 | parent = orig_parent; |
| 126 | entry = rb_entry(parent, struct extent_map, rb_node); |
| 127 | while (parent && em->start < entry->start) { |
| 128 | parent = rb_prev(parent); |
| 129 | entry = rb_entry(parent, struct extent_map, rb_node); |
| 130 | } |
| 131 | if (parent) |
| 132 | if (end > entry->start && em->start < extent_map_end(entry)) |
| 133 | return -EEXIST; |
| 134 | |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 135 | rb_link_node(&em->rb_node, orig_parent, p); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 136 | rb_insert_color_cached(&em->rb_node, root, leftmost); |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 137 | return 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 138 | } |
| 139 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 140 | /* |
| 141 | * search through the tree for an extent_map with a given offset. If |
| 142 | * it can't be found, try to find some neighboring extents |
| 143 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 144 | static struct rb_node *__tree_search(struct rb_root *root, u64 offset, |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 145 | struct rb_node **prev_ret, |
| 146 | struct rb_node **next_ret) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 147 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 148 | struct rb_node *n = root->rb_node; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 149 | struct rb_node *prev = NULL; |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 150 | struct rb_node *orig_prev = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 151 | struct extent_map *entry; |
| 152 | struct extent_map *prev_entry = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 153 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 154 | while (n) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 155 | entry = rb_entry(n, struct extent_map, rb_node); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 156 | prev = n; |
| 157 | prev_entry = entry; |
| 158 | |
| 159 | if (offset < entry->start) |
| 160 | n = n->rb_left; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 161 | else if (offset >= extent_map_end(entry)) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 162 | n = n->rb_right; |
| 163 | else |
| 164 | return n; |
| 165 | } |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 166 | |
| 167 | if (prev_ret) { |
| 168 | orig_prev = prev; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 169 | while (prev && offset >= extent_map_end(prev_entry)) { |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 170 | prev = rb_next(prev); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 171 | prev_entry = rb_entry(prev, struct extent_map, rb_node); |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 172 | } |
| 173 | *prev_ret = prev; |
| 174 | prev = orig_prev; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 175 | } |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 176 | |
| 177 | if (next_ret) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 178 | prev_entry = rb_entry(prev, struct extent_map, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 179 | while (prev && offset < prev_entry->start) { |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 180 | prev = rb_prev(prev); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 181 | prev_entry = rb_entry(prev, struct extent_map, rb_node); |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 182 | } |
| 183 | *next_ret = prev; |
| 184 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 185 | return NULL; |
| 186 | } |
| 187 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 188 | /* 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] | 189 | static int mergable_maps(struct extent_map *prev, struct extent_map *next) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 190 | { |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 191 | if (test_bit(EXTENT_FLAG_PINNED, &prev->flags)) |
| 192 | return 0; |
| 193 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 194 | /* |
| 195 | * don't merge compressed extents, we need to know their |
| 196 | * actual size |
| 197 | */ |
| 198 | if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags)) |
| 199 | return 0; |
| 200 | |
Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 201 | if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) || |
| 202 | test_bit(EXTENT_FLAG_LOGGING, &next->flags)) |
| 203 | return 0; |
| 204 | |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 205 | /* |
| 206 | * We don't want to merge stuff that hasn't been written to the log yet |
| 207 | * since it may not reflect exactly what is on disk, and that would be |
| 208 | * bad. |
| 209 | */ |
| 210 | if (!list_empty(&prev->list) || !list_empty(&next->list)) |
| 211 | return 0; |
| 212 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 213 | if (extent_map_end(prev) == next->start && |
| 214 | prev->flags == next->flags && |
| 215 | prev->bdev == next->bdev && |
| 216 | ((next->block_start == EXTENT_MAP_HOLE && |
| 217 | prev->block_start == EXTENT_MAP_HOLE) || |
| 218 | (next->block_start == EXTENT_MAP_INLINE && |
| 219 | prev->block_start == EXTENT_MAP_INLINE) || |
| 220 | (next->block_start == EXTENT_MAP_DELALLOC && |
| 221 | prev->block_start == EXTENT_MAP_DELALLOC) || |
| 222 | (next->block_start < EXTENT_MAP_LAST_BYTE - 1 && |
| 223 | next->block_start == extent_map_block_end(prev)))) { |
| 224 | return 1; |
| 225 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 226 | return 0; |
| 227 | } |
| 228 | |
Li Zefan | 4d2c8f62 | 2011-07-14 03:18:33 +0000 | [diff] [blame] | 229 | 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] | 230 | { |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 231 | struct extent_map *merge = NULL; |
| 232 | struct rb_node *rb; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 233 | |
| 234 | if (em->start != 0) { |
| 235 | rb = rb_prev(&em->rb_node); |
| 236 | if (rb) |
| 237 | merge = rb_entry(rb, struct extent_map, rb_node); |
| 238 | if (rb && mergable_maps(merge, em)) { |
| 239 | em->start = merge->start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 240 | em->orig_start = merge->orig_start; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 241 | em->len += merge->len; |
| 242 | em->block_len += merge->block_len; |
| 243 | em->block_start = merge->block_start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 244 | em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start; |
| 245 | em->mod_start = merge->mod_start; |
| 246 | em->generation = max(em->generation, merge->generation); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 247 | |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 248 | rb_erase_cached(&merge->rb_node, &tree->map); |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 249 | RB_CLEAR_NODE(&merge->rb_node); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 250 | free_extent_map(merge); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | rb = rb_next(&em->rb_node); |
| 255 | if (rb) |
| 256 | merge = rb_entry(rb, struct extent_map, rb_node); |
| 257 | if (rb && mergable_maps(em, merge)) { |
| 258 | em->len += merge->len; |
Filipe David Borba Manana | d527afe | 2013-11-30 11:28:35 +0000 | [diff] [blame] | 259 | em->block_len += merge->block_len; |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 260 | rb_erase_cached(&merge->rb_node, &tree->map); |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 261 | RB_CLEAR_NODE(&merge->rb_node); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 262 | em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start; |
| 263 | em->generation = max(em->generation, merge->generation); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 264 | free_extent_map(merge); |
| 265 | } |
Li Zefan | 4d2c8f62 | 2011-07-14 03:18:33 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 268 | /** |
Liu Bo | 52b1de9 | 2012-10-30 17:13:52 +0800 | [diff] [blame] | 269 | * unpin_extent_cache - unpin an extent from the cache |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 270 | * @tree: tree to unpin the extent in |
| 271 | * @start: logical offset in the file |
| 272 | * @len: length of the extent |
| 273 | * @gen: generation that this extent has been modified in |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 274 | * |
| 275 | * Called after an extent has been written to disk properly. Set the generation |
| 276 | * to the generation that actually added the file item to the inode so we know |
| 277 | * we need to sync this extent when we call fsync(). |
| 278 | */ |
| 279 | int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len, |
| 280 | u64 gen) |
Li Zefan | 4d2c8f62 | 2011-07-14 03:18:33 +0000 | [diff] [blame] | 281 | { |
| 282 | int ret = 0; |
| 283 | struct extent_map *em; |
Liu Bo | 4e2f84e | 2012-08-27 10:52:20 -0600 | [diff] [blame] | 284 | bool prealloc = false; |
Li Zefan | 4d2c8f62 | 2011-07-14 03:18:33 +0000 | [diff] [blame] | 285 | |
| 286 | write_lock(&tree->lock); |
| 287 | em = lookup_extent_mapping(tree, start, len); |
| 288 | |
| 289 | WARN_ON(!em || em->start != start); |
| 290 | |
| 291 | if (!em) |
| 292 | goto out; |
| 293 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 294 | em->generation = gen; |
Li Zefan | 4d2c8f62 | 2011-07-14 03:18:33 +0000 | [diff] [blame] | 295 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
Liu Bo | 4e2f84e | 2012-08-27 10:52:20 -0600 | [diff] [blame] | 296 | em->mod_start = em->start; |
| 297 | em->mod_len = em->len; |
| 298 | |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 299 | if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) { |
Liu Bo | 4e2f84e | 2012-08-27 10:52:20 -0600 | [diff] [blame] | 300 | prealloc = true; |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 301 | clear_bit(EXTENT_FLAG_FILLING, &em->flags); |
Liu Bo | 4e2f84e | 2012-08-27 10:52:20 -0600 | [diff] [blame] | 302 | } |
Li Zefan | 4d2c8f62 | 2011-07-14 03:18:33 +0000 | [diff] [blame] | 303 | |
| 304 | try_merge_map(tree, em); |
Liu Bo | 4e2f84e | 2012-08-27 10:52:20 -0600 | [diff] [blame] | 305 | |
| 306 | if (prealloc) { |
| 307 | em->mod_start = em->start; |
| 308 | em->mod_len = em->len; |
| 309 | } |
| 310 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 311 | free_extent_map(em); |
| 312 | out: |
| 313 | write_unlock(&tree->lock); |
| 314 | return ret; |
| 315 | |
| 316 | } |
| 317 | |
Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 318 | void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em) |
| 319 | { |
| 320 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 321 | if (extent_map_in_tree(em)) |
Josef Bacik | 222c81d | 2013-01-28 09:45:20 -0500 | [diff] [blame] | 322 | try_merge_map(tree, em); |
Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 323 | } |
| 324 | |
Filipe Manana | 176840b | 2014-02-25 14:15:13 +0000 | [diff] [blame] | 325 | static inline void setup_extent_mapping(struct extent_map_tree *tree, |
| 326 | struct extent_map *em, |
| 327 | int modified) |
| 328 | { |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 329 | refcount_inc(&em->refs); |
Filipe Manana | 176840b | 2014-02-25 14:15:13 +0000 | [diff] [blame] | 330 | em->mod_start = em->start; |
| 331 | em->mod_len = em->len; |
| 332 | |
| 333 | if (modified) |
| 334 | list_move(&em->list, &tree->modified_extents); |
| 335 | else |
| 336 | try_merge_map(tree, em); |
| 337 | } |
| 338 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 339 | /** |
| 340 | * add_extent_mapping - add new extent map to the extent tree |
| 341 | * @tree: tree to insert new map in |
| 342 | * @em: map to insert |
| 343 | * |
| 344 | * Insert @em into @tree or perform a simple forward/backward merge with |
| 345 | * existing mappings. The extent_map struct passed in will be inserted |
| 346 | * into the tree directly, with an additional reference taken, or a |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 347 | * reference dropped if the merge attempt was successful. |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 348 | */ |
| 349 | int add_extent_mapping(struct extent_map_tree *tree, |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 350 | struct extent_map *em, int modified) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 351 | { |
| 352 | int ret = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 353 | |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 354 | ret = tree_insert(&tree->map, em); |
| 355 | if (ret) |
Chris Mason | 7c2fe32 | 2008-08-20 08:51:50 -0400 | [diff] [blame] | 356 | goto out; |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 357 | |
Filipe Manana | 176840b | 2014-02-25 14:15:13 +0000 | [diff] [blame] | 358 | setup_extent_mapping(tree, em, modified); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 359 | out: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 360 | return ret; |
| 361 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 362 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 363 | static struct extent_map * |
| 364 | __lookup_extent_mapping(struct extent_map_tree *tree, |
| 365 | u64 start, u64 len, int strict) |
Li Zefan | ed64f06 | 2011-07-14 03:18:15 +0000 | [diff] [blame] | 366 | { |
| 367 | struct extent_map *em; |
| 368 | struct rb_node *rb_node; |
| 369 | struct rb_node *prev = NULL; |
| 370 | struct rb_node *next = NULL; |
| 371 | u64 end = range_end(start, len); |
| 372 | |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 373 | rb_node = __tree_search(&tree->map.rb_root, start, &prev, &next); |
Li Zefan | ed64f06 | 2011-07-14 03:18:15 +0000 | [diff] [blame] | 374 | if (!rb_node) { |
| 375 | if (prev) |
| 376 | rb_node = prev; |
| 377 | else if (next) |
| 378 | rb_node = next; |
| 379 | else |
| 380 | return NULL; |
| 381 | } |
| 382 | |
| 383 | em = rb_entry(rb_node, struct extent_map, rb_node); |
| 384 | |
| 385 | if (strict && !(end > em->start && start < extent_map_end(em))) |
| 386 | return NULL; |
| 387 | |
Elena Reshetova | 490b54d | 2017-03-03 10:55:12 +0200 | [diff] [blame] | 388 | refcount_inc(&em->refs); |
Li Zefan | ed64f06 | 2011-07-14 03:18:15 +0000 | [diff] [blame] | 389 | return em; |
| 390 | } |
| 391 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 392 | /** |
| 393 | * lookup_extent_mapping - lookup extent_map |
| 394 | * @tree: tree to lookup in |
| 395 | * @start: byte offset to start the search |
| 396 | * @len: length of the lookup range |
| 397 | * |
| 398 | * Find and return the first extent_map struct in @tree that intersects the |
| 399 | * [start, len] range. There may be additional objects in the tree that |
| 400 | * intersect, so check the object returned carefully to make sure that no |
| 401 | * additional lookups are needed. |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 402 | */ |
| 403 | struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 404 | u64 start, u64 len) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 405 | { |
Li Zefan | ed64f06 | 2011-07-14 03:18:15 +0000 | [diff] [blame] | 406 | return __lookup_extent_mapping(tree, start, len, 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 407 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 408 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 409 | /** |
Chris Mason | b917b7c | 2009-09-18 16:07:03 -0400 | [diff] [blame] | 410 | * search_extent_mapping - find a nearby extent map |
| 411 | * @tree: tree to lookup in |
| 412 | * @start: byte offset to start the search |
| 413 | * @len: length of the lookup range |
| 414 | * |
| 415 | * Find and return the first extent_map struct in @tree that intersects the |
| 416 | * [start, len] range. |
| 417 | * |
| 418 | * If one can't be found, any nearby extent may be returned |
| 419 | */ |
| 420 | struct extent_map *search_extent_mapping(struct extent_map_tree *tree, |
| 421 | u64 start, u64 len) |
| 422 | { |
Li Zefan | ed64f06 | 2011-07-14 03:18:15 +0000 | [diff] [blame] | 423 | return __lookup_extent_mapping(tree, start, len, 0); |
Chris Mason | b917b7c | 2009-09-18 16:07:03 -0400 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /** |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 427 | * remove_extent_mapping - removes an extent_map from the extent tree |
| 428 | * @tree: extent tree to remove from |
Adam Buchbinder | bb7ab3b | 2016-03-04 11:23:12 -0800 | [diff] [blame] | 429 | * @em: extent map being removed |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 430 | * |
| 431 | * Removes @em from @tree. No reference counts are dropped, and no checks |
| 432 | * are done to see if the range is in use |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 433 | */ |
zhong jiang | c1766dd | 2018-09-13 11:01:15 +0800 | [diff] [blame] | 434 | 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] | 435 | { |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 436 | WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags)); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 437 | rb_erase_cached(&em->rb_node, &tree->map); |
Josef Bacik | ff44c6e | 2012-09-14 12:59:20 -0400 | [diff] [blame] | 438 | if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags)) |
| 439 | list_del_init(&em->list); |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 440 | RB_CLEAR_NODE(&em->rb_node); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 441 | } |
Filipe Manana | 176840b | 2014-02-25 14:15:13 +0000 | [diff] [blame] | 442 | |
| 443 | void 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); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 452 | rb_replace_node_cached(&cur->rb_node, &new->rb_node, &tree->map); |
Filipe Manana | 176840b | 2014-02-25 14:15:13 +0000 | [diff] [blame] | 453 | RB_CLEAR_NODE(&cur->rb_node); |
| 454 | |
| 455 | setup_extent_mapping(tree, new, modified); |
| 456 | } |
Liu Bo | c04e61b | 2018-01-05 12:51:11 -0700 | [diff] [blame] | 457 | |
| 458 | static 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 | |
| 468 | static 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 | |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame^] | 478 | /* |
| 479 | * Helper for btrfs_get_extent. Given an existing extent in the tree, |
Liu Bo | c04e61b | 2018-01-05 12:51:11 -0700 | [diff] [blame] | 480 | * the existing extent is the nearest extent to map_start, |
| 481 | * and an extent that you want to insert, deal with overlap and insert |
| 482 | * the best fitted new extent into the tree. |
| 483 | */ |
Liu Bo | 5f4791f | 2018-01-05 12:51:17 -0700 | [diff] [blame] | 484 | static noinline int merge_extent_mapping(struct extent_map_tree *em_tree, |
| 485 | struct extent_map *existing, |
| 486 | struct extent_map *em, |
| 487 | u64 map_start) |
Liu Bo | c04e61b | 2018-01-05 12:51:11 -0700 | [diff] [blame] | 488 | { |
| 489 | struct extent_map *prev; |
| 490 | struct extent_map *next; |
| 491 | u64 start; |
| 492 | u64 end; |
| 493 | u64 start_diff; |
| 494 | |
| 495 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
| 496 | |
| 497 | if (existing->start > map_start) { |
| 498 | next = existing; |
| 499 | prev = prev_extent_map(next); |
| 500 | } else { |
| 501 | prev = existing; |
| 502 | next = next_extent_map(prev); |
| 503 | } |
| 504 | |
| 505 | start = prev ? extent_map_end(prev) : em->start; |
| 506 | start = max_t(u64, start, em->start); |
| 507 | end = next ? next->start : extent_map_end(em); |
| 508 | end = min_t(u64, end, extent_map_end(em)); |
| 509 | start_diff = start - em->start; |
| 510 | em->start = start; |
| 511 | em->len = end - start; |
| 512 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 513 | !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) { |
| 514 | em->block_start += start_diff; |
| 515 | em->block_len = em->len; |
| 516 | } |
| 517 | return add_extent_mapping(em_tree, em, 0); |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * btrfs_add_extent_mapping - add extent mapping into em_tree |
David Sterba | f46b24c | 2018-04-03 21:45:57 +0200 | [diff] [blame] | 522 | * @fs_info - used for tracepoint |
Liu Bo | c04e61b | 2018-01-05 12:51:11 -0700 | [diff] [blame] | 523 | * @em_tree - the extent tree into which we want to insert the extent mapping |
| 524 | * @em_in - extent we are inserting |
| 525 | * @start - start of the logical range btrfs_get_extent() is requesting |
| 526 | * @len - length of the logical range btrfs_get_extent() is requesting |
| 527 | * |
| 528 | * Note that @em_in's range may be different from [start, start+len), |
| 529 | * but they must be overlapped. |
| 530 | * |
| 531 | * Insert @em_in into @em_tree. In case there is an overlapping range, handle |
| 532 | * the -EEXIST by either: |
| 533 | * a) Returning the existing extent in @em_in if @start is within the |
| 534 | * existing em. |
| 535 | * b) Merge the existing extent with @em_in passed in. |
| 536 | * |
| 537 | * Return 0 on success, otherwise -EEXIST. |
| 538 | * |
| 539 | */ |
David Sterba | f46b24c | 2018-04-03 21:45:57 +0200 | [diff] [blame] | 540 | int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info, |
| 541 | struct extent_map_tree *em_tree, |
Liu Bo | c04e61b | 2018-01-05 12:51:11 -0700 | [diff] [blame] | 542 | struct extent_map **em_in, u64 start, u64 len) |
| 543 | { |
| 544 | int ret; |
| 545 | struct extent_map *em = *em_in; |
| 546 | |
| 547 | ret = add_extent_mapping(em_tree, em, 0); |
| 548 | /* it is possible that someone inserted the extent into the tree |
| 549 | * while we had the lock dropped. It is also possible that |
| 550 | * an overlapping map exists in the tree |
| 551 | */ |
| 552 | if (ret == -EEXIST) { |
| 553 | struct extent_map *existing; |
| 554 | |
| 555 | ret = 0; |
| 556 | |
| 557 | existing = search_extent_mapping(em_tree, start, len); |
Liu Bo | 393da91 | 2018-01-05 12:51:16 -0700 | [diff] [blame] | 558 | |
David Sterba | f46b24c | 2018-04-03 21:45:57 +0200 | [diff] [blame] | 559 | trace_btrfs_handle_em_exist(fs_info, existing, em, start, len); |
Liu Bo | 393da91 | 2018-01-05 12:51:16 -0700 | [diff] [blame] | 560 | |
Liu Bo | c04e61b | 2018-01-05 12:51:11 -0700 | [diff] [blame] | 561 | /* |
| 562 | * existing will always be non-NULL, since there must be |
| 563 | * extent causing the -EEXIST. |
| 564 | */ |
| 565 | if (start >= existing->start && |
| 566 | start < extent_map_end(existing)) { |
| 567 | free_extent_map(em); |
| 568 | *em_in = existing; |
| 569 | ret = 0; |
| 570 | } else { |
Liu Bo | 9a7e10e | 2018-01-05 12:51:15 -0700 | [diff] [blame] | 571 | u64 orig_start = em->start; |
| 572 | u64 orig_len = em->len; |
| 573 | |
Liu Bo | c04e61b | 2018-01-05 12:51:11 -0700 | [diff] [blame] | 574 | /* |
| 575 | * The existing extent map is the one nearest to |
| 576 | * the [start, start + len) range which overlaps |
| 577 | */ |
| 578 | ret = merge_extent_mapping(em_tree, existing, |
| 579 | em, start); |
Liu Bo | c04e61b | 2018-01-05 12:51:11 -0700 | [diff] [blame] | 580 | if (ret) { |
| 581 | free_extent_map(em); |
| 582 | *em_in = NULL; |
Liu Bo | 9a7e10e | 2018-01-05 12:51:15 -0700 | [diff] [blame] | 583 | WARN_ONCE(ret, |
| 584 | "unexpected error %d: merge existing(start %llu len %llu) with em(start %llu len %llu)\n", |
| 585 | ret, existing->start, existing->len, |
| 586 | orig_start, orig_len); |
Liu Bo | c04e61b | 2018-01-05 12:51:11 -0700 | [diff] [blame] | 587 | } |
Liu Bo | 9a7e10e | 2018-01-05 12:51:15 -0700 | [diff] [blame] | 588 | free_extent_map(existing); |
Liu Bo | c04e61b | 2018-01-05 12:51:11 -0700 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
| 592 | ASSERT(ret == 0 || ret == -EEXIST); |
| 593 | return ret; |
| 594 | } |