Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1 | #include <linux/err.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2 | #include <linux/slab.h> |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 3 | #include <linux/spinlock.h> |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4 | #include <linux/hardirq.h> |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 5 | #include "ctree.h" |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 6 | #include "extent_map.h" |
| 7 | |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 8 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 9 | static struct kmem_cache *extent_map_cache; |
Chris Mason | ca66462 | 2007-11-27 11:16:35 -0500 | [diff] [blame] | 10 | |
Wyatt Banks | 2f4cbe6 | 2007-11-19 10:22:33 -0500 | [diff] [blame] | 11 | int __init extent_map_init(void) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 12 | { |
David Sterba | 837e197 | 2012-09-07 03:00:48 -0600 | [diff] [blame] | 13 | extent_map_cache = kmem_cache_create("btrfs_extent_map", |
Christoph Hellwig | 9601e3f | 2009-04-13 15:33:09 +0200 | [diff] [blame] | 14 | sizeof(struct extent_map), 0, |
| 15 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
Wyatt Banks | 2f4cbe6 | 2007-11-19 10:22:33 -0500 | [diff] [blame] | 16 | if (!extent_map_cache) |
| 17 | return -ENOMEM; |
Wyatt Banks | 2f4cbe6 | 2007-11-19 10:22:33 -0500 | [diff] [blame] | 18 | return 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 19 | } |
| 20 | |
Christian Hesse | 17636e0 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 21 | void extent_map_exit(void) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 22 | { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 23 | if (extent_map_cache) |
| 24 | kmem_cache_destroy(extent_map_cache); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 25 | } |
| 26 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 27 | /** |
| 28 | * extent_map_tree_init - initialize extent map tree |
| 29 | * @tree: tree to initialize |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 30 | * |
| 31 | * Initialize the extent tree @tree. Should be called for each new inode |
| 32 | * or other user of the extent_map interface. |
| 33 | */ |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 34 | void extent_map_tree_init(struct extent_map_tree *tree) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 35 | { |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 36 | tree->map = RB_ROOT; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 37 | INIT_LIST_HEAD(&tree->modified_extents); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 38 | rwlock_init(&tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 39 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 40 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 41 | /** |
| 42 | * alloc_extent_map - allocate new extent map structure |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 43 | * |
| 44 | * Allocate a new extent_map structure. The new structure is |
| 45 | * returned with a reference count of one and needs to be |
| 46 | * freed using free_extent_map() |
| 47 | */ |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 48 | struct extent_map *alloc_extent_map(void) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 49 | { |
| 50 | struct extent_map *em; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 51 | em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS); |
Tsutomu Itoh | c26a920 | 2011-02-14 00:45:29 +0000 | [diff] [blame] | 52 | if (!em) |
| 53 | return NULL; |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 54 | RB_CLEAR_NODE(&em->rb_node); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 55 | em->flags = 0; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame] | 56 | em->compress_type = BTRFS_COMPRESS_NONE; |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 57 | em->generation = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 58 | atomic_set(&em->refs, 1); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 59 | INIT_LIST_HEAD(&em->list); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 60 | return em; |
| 61 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 62 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 63 | /** |
| 64 | * free_extent_map - drop reference count of an extent_map |
| 65 | * @em: extent map beeing releasead |
| 66 | * |
| 67 | * Drops the reference out on @em by one and free the structure |
| 68 | * if the reference count hits zero. |
| 69 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 70 | void free_extent_map(struct extent_map *em) |
| 71 | { |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 72 | if (!em) |
| 73 | return; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 74 | WARN_ON(atomic_read(&em->refs) == 0); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 75 | if (atomic_dec_and_test(&em->refs)) { |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 76 | WARN_ON(extent_map_in_tree(em)); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 77 | WARN_ON(!list_empty(&em->list)); |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 78 | if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags)) |
| 79 | kfree(em->bdev); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 80 | kmem_cache_free(extent_map_cache, em); |
| 81 | } |
| 82 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 83 | |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 84 | /* simple helper to do math around the end of an extent, handling wrap */ |
| 85 | static u64 range_end(u64 start, u64 len) |
| 86 | { |
| 87 | if (start + len < start) |
| 88 | return (u64)-1; |
| 89 | return start + len; |
| 90 | } |
| 91 | |
| 92 | static int tree_insert(struct rb_root *root, struct extent_map *em) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 93 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 94 | struct rb_node **p = &root->rb_node; |
| 95 | struct rb_node *parent = NULL; |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 96 | struct extent_map *entry = NULL; |
| 97 | struct rb_node *orig_parent = NULL; |
| 98 | u64 end = range_end(em->start, em->len); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 99 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 100 | while (*p) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 101 | parent = *p; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 102 | entry = rb_entry(parent, struct extent_map, rb_node); |
| 103 | |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 104 | if (em->start < entry->start) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 105 | p = &(*p)->rb_left; |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 106 | else if (em->start >= extent_map_end(entry)) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 107 | p = &(*p)->rb_right; |
| 108 | else |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 109 | return -EEXIST; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 110 | } |
| 111 | |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 112 | orig_parent = parent; |
| 113 | while (parent && em->start >= extent_map_end(entry)) { |
| 114 | parent = rb_next(parent); |
| 115 | entry = rb_entry(parent, struct extent_map, rb_node); |
| 116 | } |
| 117 | if (parent) |
| 118 | if (end > entry->start && em->start < extent_map_end(entry)) |
| 119 | return -EEXIST; |
| 120 | |
| 121 | parent = orig_parent; |
| 122 | entry = rb_entry(parent, struct extent_map, rb_node); |
| 123 | while (parent && em->start < entry->start) { |
| 124 | parent = rb_prev(parent); |
| 125 | entry = rb_entry(parent, struct extent_map, rb_node); |
| 126 | } |
| 127 | if (parent) |
| 128 | if (end > entry->start && em->start < extent_map_end(entry)) |
| 129 | return -EEXIST; |
| 130 | |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 131 | rb_link_node(&em->rb_node, orig_parent, p); |
| 132 | rb_insert_color(&em->rb_node, root); |
| 133 | return 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 134 | } |
| 135 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 136 | /* |
| 137 | * search through the tree for an extent_map with a given offset. If |
| 138 | * it can't be found, try to find some neighboring extents |
| 139 | */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 140 | static struct rb_node *__tree_search(struct rb_root *root, u64 offset, |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 141 | struct rb_node **prev_ret, |
| 142 | struct rb_node **next_ret) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 143 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 144 | struct rb_node *n = root->rb_node; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 145 | struct rb_node *prev = NULL; |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 146 | struct rb_node *orig_prev = NULL; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 147 | struct extent_map *entry; |
| 148 | struct extent_map *prev_entry = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 149 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 150 | while (n) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 151 | entry = rb_entry(n, struct extent_map, rb_node); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 152 | prev = n; |
| 153 | prev_entry = entry; |
| 154 | |
| 155 | if (offset < entry->start) |
| 156 | n = n->rb_left; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 157 | else if (offset >= extent_map_end(entry)) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 158 | n = n->rb_right; |
| 159 | else |
| 160 | return n; |
| 161 | } |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 162 | |
| 163 | if (prev_ret) { |
| 164 | orig_prev = prev; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 165 | while (prev && offset >= extent_map_end(prev_entry)) { |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 166 | prev = rb_next(prev); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 167 | prev_entry = rb_entry(prev, struct extent_map, rb_node); |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 168 | } |
| 169 | *prev_ret = prev; |
| 170 | prev = orig_prev; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 171 | } |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 172 | |
| 173 | if (next_ret) { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 174 | prev_entry = rb_entry(prev, struct extent_map, rb_node); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 175 | while (prev && offset < prev_entry->start) { |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 176 | prev = rb_prev(prev); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 177 | prev_entry = rb_entry(prev, struct extent_map, rb_node); |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 178 | } |
| 179 | *next_ret = prev; |
| 180 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 181 | return NULL; |
| 182 | } |
| 183 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 184 | /* 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] | 185 | static int mergable_maps(struct extent_map *prev, struct extent_map *next) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 186 | { |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 187 | if (test_bit(EXTENT_FLAG_PINNED, &prev->flags)) |
| 188 | return 0; |
| 189 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 190 | /* |
| 191 | * don't merge compressed extents, we need to know their |
| 192 | * actual size |
| 193 | */ |
| 194 | if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags)) |
| 195 | return 0; |
| 196 | |
Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 197 | if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) || |
| 198 | test_bit(EXTENT_FLAG_LOGGING, &next->flags)) |
| 199 | return 0; |
| 200 | |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 201 | /* |
| 202 | * We don't want to merge stuff that hasn't been written to the log yet |
| 203 | * since it may not reflect exactly what is on disk, and that would be |
| 204 | * bad. |
| 205 | */ |
| 206 | if (!list_empty(&prev->list) || !list_empty(&next->list)) |
| 207 | return 0; |
| 208 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 209 | if (extent_map_end(prev) == next->start && |
| 210 | prev->flags == next->flags && |
| 211 | prev->bdev == next->bdev && |
| 212 | ((next->block_start == EXTENT_MAP_HOLE && |
| 213 | prev->block_start == EXTENT_MAP_HOLE) || |
| 214 | (next->block_start == EXTENT_MAP_INLINE && |
| 215 | prev->block_start == EXTENT_MAP_INLINE) || |
| 216 | (next->block_start == EXTENT_MAP_DELALLOC && |
| 217 | prev->block_start == EXTENT_MAP_DELALLOC) || |
| 218 | (next->block_start < EXTENT_MAP_LAST_BYTE - 1 && |
| 219 | next->block_start == extent_map_block_end(prev)))) { |
| 220 | return 1; |
| 221 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 222 | return 0; |
| 223 | } |
| 224 | |
Li Zefan | 4d2c8f62 | 2011-07-14 03:18:33 +0000 | [diff] [blame] | 225 | 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] | 226 | { |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 227 | struct extent_map *merge = NULL; |
| 228 | struct rb_node *rb; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 229 | |
| 230 | if (em->start != 0) { |
| 231 | rb = rb_prev(&em->rb_node); |
| 232 | if (rb) |
| 233 | merge = rb_entry(rb, struct extent_map, rb_node); |
| 234 | if (rb && mergable_maps(merge, em)) { |
| 235 | em->start = merge->start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 236 | em->orig_start = merge->orig_start; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 237 | em->len += merge->len; |
| 238 | em->block_len += merge->block_len; |
| 239 | em->block_start = merge->block_start; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 240 | em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start; |
| 241 | em->mod_start = merge->mod_start; |
| 242 | em->generation = max(em->generation, merge->generation); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 243 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 244 | rb_erase(&merge->rb_node, &tree->map); |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 245 | RB_CLEAR_NODE(&merge->rb_node); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 246 | free_extent_map(merge); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | rb = rb_next(&em->rb_node); |
| 251 | if (rb) |
| 252 | merge = rb_entry(rb, struct extent_map, rb_node); |
| 253 | if (rb && mergable_maps(em, merge)) { |
| 254 | em->len += merge->len; |
Filipe David Borba Manana | d527afe | 2013-11-30 11:28:35 +0000 | [diff] [blame] | 255 | em->block_len += merge->block_len; |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 256 | rb_erase(&merge->rb_node, &tree->map); |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 257 | RB_CLEAR_NODE(&merge->rb_node); |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 258 | em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start; |
| 259 | em->generation = max(em->generation, merge->generation); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 260 | free_extent_map(merge); |
| 261 | } |
Li Zefan | 4d2c8f62 | 2011-07-14 03:18:33 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 264 | /** |
Liu Bo | 52b1de9 | 2012-10-30 17:13:52 +0800 | [diff] [blame] | 265 | * unpin_extent_cache - unpin an extent from the cache |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 266 | * @tree: tree to unpin the extent in |
| 267 | * @start: logical offset in the file |
| 268 | * @len: length of the extent |
| 269 | * @gen: generation that this extent has been modified in |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 270 | * |
| 271 | * Called after an extent has been written to disk properly. Set the generation |
| 272 | * to the generation that actually added the file item to the inode so we know |
| 273 | * we need to sync this extent when we call fsync(). |
| 274 | */ |
| 275 | int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len, |
| 276 | u64 gen) |
Li Zefan | 4d2c8f62 | 2011-07-14 03:18:33 +0000 | [diff] [blame] | 277 | { |
| 278 | int ret = 0; |
| 279 | struct extent_map *em; |
Liu Bo | 4e2f84e | 2012-08-27 10:52:20 -0600 | [diff] [blame] | 280 | bool prealloc = false; |
Li Zefan | 4d2c8f62 | 2011-07-14 03:18:33 +0000 | [diff] [blame] | 281 | |
| 282 | write_lock(&tree->lock); |
| 283 | em = lookup_extent_mapping(tree, start, len); |
| 284 | |
| 285 | WARN_ON(!em || em->start != start); |
| 286 | |
| 287 | if (!em) |
| 288 | goto out; |
| 289 | |
Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 290 | if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags)) |
| 291 | list_move(&em->list, &tree->modified_extents); |
Josef Bacik | 5dc562c | 2012-08-17 13:14:17 -0400 | [diff] [blame] | 292 | em->generation = gen; |
Li Zefan | 4d2c8f62 | 2011-07-14 03:18:33 +0000 | [diff] [blame] | 293 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
Liu Bo | 4e2f84e | 2012-08-27 10:52:20 -0600 | [diff] [blame] | 294 | em->mod_start = em->start; |
| 295 | em->mod_len = em->len; |
| 296 | |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 297 | if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) { |
Liu Bo | 4e2f84e | 2012-08-27 10:52:20 -0600 | [diff] [blame] | 298 | prealloc = true; |
Josef Bacik | b11e234 | 2012-12-03 10:58:15 -0500 | [diff] [blame] | 299 | clear_bit(EXTENT_FLAG_FILLING, &em->flags); |
Liu Bo | 4e2f84e | 2012-08-27 10:52:20 -0600 | [diff] [blame] | 300 | } |
Li Zefan | 4d2c8f62 | 2011-07-14 03:18:33 +0000 | [diff] [blame] | 301 | |
| 302 | try_merge_map(tree, em); |
Liu Bo | 4e2f84e | 2012-08-27 10:52:20 -0600 | [diff] [blame] | 303 | |
| 304 | if (prealloc) { |
| 305 | em->mod_start = em->start; |
| 306 | em->mod_len = em->len; |
| 307 | } |
| 308 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 309 | free_extent_map(em); |
| 310 | out: |
| 311 | write_unlock(&tree->lock); |
| 312 | return ret; |
| 313 | |
| 314 | } |
| 315 | |
Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 316 | void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em) |
| 317 | { |
| 318 | clear_bit(EXTENT_FLAG_LOGGING, &em->flags); |
Filipe Manana | cbc0e92 | 2014-02-25 14:15:12 +0000 | [diff] [blame] | 319 | if (extent_map_in_tree(em)) |
Josef Bacik | 222c81d | 2013-01-28 09:45:20 -0500 | [diff] [blame] | 320 | try_merge_map(tree, em); |
Josef Bacik | 201a903 | 2013-01-24 12:02:07 -0500 | [diff] [blame] | 321 | } |
| 322 | |
Filipe Manana | 176840b | 2014-02-25 14:15:13 +0000 | [diff] [blame] | 323 | static inline void setup_extent_mapping(struct extent_map_tree *tree, |
| 324 | struct extent_map *em, |
| 325 | int modified) |
| 326 | { |
| 327 | atomic_inc(&em->refs); |
| 328 | em->mod_start = em->start; |
| 329 | em->mod_len = em->len; |
| 330 | |
| 331 | if (modified) |
| 332 | list_move(&em->list, &tree->modified_extents); |
| 333 | else |
| 334 | try_merge_map(tree, em); |
| 335 | } |
| 336 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 337 | /** |
| 338 | * add_extent_mapping - add new extent map to the extent tree |
| 339 | * @tree: tree to insert new map in |
| 340 | * @em: map to insert |
| 341 | * |
| 342 | * Insert @em into @tree or perform a simple forward/backward merge with |
| 343 | * existing mappings. The extent_map struct passed in will be inserted |
| 344 | * into the tree directly, with an additional reference taken, or a |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 345 | * reference dropped if the merge attempt was successful. |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 346 | */ |
| 347 | int add_extent_mapping(struct extent_map_tree *tree, |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 348 | struct extent_map *em, int modified) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 349 | { |
| 350 | int ret = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 351 | |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 352 | ret = tree_insert(&tree->map, em); |
| 353 | if (ret) |
Chris Mason | 7c2fe32 | 2008-08-20 08:51:50 -0400 | [diff] [blame] | 354 | goto out; |
Filipe David Borba Manana | 32193c1 | 2013-11-25 03:23:51 +0000 | [diff] [blame] | 355 | |
Filipe Manana | 176840b | 2014-02-25 14:15:13 +0000 | [diff] [blame] | 356 | setup_extent_mapping(tree, em, modified); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 357 | out: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 358 | return ret; |
| 359 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 360 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 361 | static struct extent_map * |
| 362 | __lookup_extent_mapping(struct extent_map_tree *tree, |
| 363 | u64 start, u64 len, int strict) |
Li Zefan | ed64f06 | 2011-07-14 03:18:15 +0000 | [diff] [blame] | 364 | { |
| 365 | struct extent_map *em; |
| 366 | struct rb_node *rb_node; |
| 367 | struct rb_node *prev = NULL; |
| 368 | struct rb_node *next = NULL; |
| 369 | u64 end = range_end(start, len); |
| 370 | |
| 371 | rb_node = __tree_search(&tree->map, start, &prev, &next); |
| 372 | if (!rb_node) { |
| 373 | if (prev) |
| 374 | rb_node = prev; |
| 375 | else if (next) |
| 376 | rb_node = next; |
| 377 | else |
| 378 | return NULL; |
| 379 | } |
| 380 | |
| 381 | em = rb_entry(rb_node, struct extent_map, rb_node); |
| 382 | |
| 383 | if (strict && !(end > em->start && start < extent_map_end(em))) |
| 384 | return NULL; |
| 385 | |
| 386 | atomic_inc(&em->refs); |
| 387 | return em; |
| 388 | } |
| 389 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 390 | /** |
| 391 | * lookup_extent_mapping - lookup extent_map |
| 392 | * @tree: tree to lookup in |
| 393 | * @start: byte offset to start the search |
| 394 | * @len: length of the lookup range |
| 395 | * |
| 396 | * Find and return the first extent_map struct in @tree that intersects the |
| 397 | * [start, len] range. There may be additional objects in the tree that |
| 398 | * intersect, so check the object returned carefully to make sure that no |
| 399 | * additional lookups are needed. |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 400 | */ |
| 401 | struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 402 | u64 start, u64 len) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 403 | { |
Li Zefan | ed64f06 | 2011-07-14 03:18:15 +0000 | [diff] [blame] | 404 | return __lookup_extent_mapping(tree, start, len, 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 405 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 406 | |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 407 | /** |
Chris Mason | b917b7c | 2009-09-18 16:07:03 -0400 | [diff] [blame] | 408 | * search_extent_mapping - find a nearby extent map |
| 409 | * @tree: tree to lookup in |
| 410 | * @start: byte offset to start the search |
| 411 | * @len: length of the lookup range |
| 412 | * |
| 413 | * Find and return the first extent_map struct in @tree that intersects the |
| 414 | * [start, len] range. |
| 415 | * |
| 416 | * If one can't be found, any nearby extent may be returned |
| 417 | */ |
| 418 | struct extent_map *search_extent_mapping(struct extent_map_tree *tree, |
| 419 | u64 start, u64 len) |
| 420 | { |
Li Zefan | ed64f06 | 2011-07-14 03:18:15 +0000 | [diff] [blame] | 421 | return __lookup_extent_mapping(tree, start, len, 0); |
Chris Mason | b917b7c | 2009-09-18 16:07:03 -0400 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /** |
Christoph Hellwig | 9d2423c | 2008-06-11 21:52:17 -0400 | [diff] [blame] | 425 | * remove_extent_mapping - removes an extent_map from the extent tree |
| 426 | * @tree: extent tree to remove from |
| 427 | * @em: extent map beeing removed |
| 428 | * |
| 429 | * Removes @em from @tree. No reference counts are dropped, and no checks |
| 430 | * are done to see if the range is in use |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 431 | */ |
| 432 | int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em) |
| 433 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 434 | int ret = 0; |
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)); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 437 | rb_erase(&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 | return ret; |
| 442 | } |
Filipe Manana | 176840b | 2014-02-25 14:15:13 +0000 | [diff] [blame] | 443 | |
| 444 | void replace_extent_mapping(struct extent_map_tree *tree, |
| 445 | struct extent_map *cur, |
| 446 | struct extent_map *new, |
| 447 | int modified) |
| 448 | { |
| 449 | WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags)); |
| 450 | ASSERT(extent_map_in_tree(cur)); |
| 451 | if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags)) |
| 452 | list_del_init(&cur->list); |
| 453 | rb_replace_node(&cur->rb_node, &new->rb_node, &tree->map); |
| 454 | RB_CLEAR_NODE(&cur->rb_node); |
| 455 | |
| 456 | setup_extent_mapping(tree, new, modified); |
| 457 | } |