blob: 2834ca5768ea8124cf9abd468906e4867f4d6dc2 [file] [log] [blame]
Chris Masond1310b22008-01-24 16:13:08 -05001#include <linux/err.h>
Chris Masond1310b22008-01-24 16:13:08 -05002#include <linux/slab.h>
Chris Masona52d9a82007-08-27 16:49:44 -04003#include <linux/spinlock.h>
Chris Masond1310b22008-01-24 16:13:08 -05004#include <linux/hardirq.h>
Li Zefan261507a02010-12-17 14:21:50 +08005#include "ctree.h"
Chris Masona52d9a82007-08-27 16:49:44 -04006#include "extent_map.h"
7
Chris Mason86479a02007-09-10 19:58:16 -04008
Chris Masona52d9a82007-08-27 16:49:44 -04009static struct kmem_cache *extent_map_cache;
Chris Masonca664622007-11-27 11:16:35 -050010
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050011int __init extent_map_init(void)
Chris Masona52d9a82007-08-27 16:49:44 -040012{
David Sterba837e1972012-09-07 03:00:48 -060013 extent_map_cache = kmem_cache_create("btrfs_extent_map",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020014 sizeof(struct extent_map), 0,
15 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050016 if (!extent_map_cache)
17 return -ENOMEM;
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050018 return 0;
Chris Masona52d9a82007-08-27 16:49:44 -040019}
20
Christian Hesse17636e02007-12-11 09:25:06 -050021void extent_map_exit(void)
Chris Masona52d9a82007-08-27 16:49:44 -040022{
Chris Masona52d9a82007-08-27 16:49:44 -040023 if (extent_map_cache)
24 kmem_cache_destroy(extent_map_cache);
Chris Masona52d9a82007-08-27 16:49:44 -040025}
26
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040027/**
28 * extent_map_tree_init - initialize extent map tree
29 * @tree: tree to initialize
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040030 *
31 * Initialize the extent tree @tree. Should be called for each new inode
32 * or other user of the extent_map interface.
33 */
David Sterbaa8067e02011-04-21 00:34:43 +020034void extent_map_tree_init(struct extent_map_tree *tree)
Chris Masona52d9a82007-08-27 16:49:44 -040035{
Eric Paris6bef4d32010-02-23 19:43:04 +000036 tree->map = RB_ROOT;
Josef Bacik5dc562c2012-08-17 13:14:17 -040037 INIT_LIST_HEAD(&tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -040038 rwlock_init(&tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -040039}
Chris Masona52d9a82007-08-27 16:49:44 -040040
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040041/**
42 * alloc_extent_map - allocate new extent map structure
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040043 *
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 Sterba172ddd62011-04-21 00:48:27 +020048struct extent_map *alloc_extent_map(void)
Chris Masona52d9a82007-08-27 16:49:44 -040049{
50 struct extent_map *em;
Josef Bacik70c8a912012-10-11 16:54:30 -040051 em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +000052 if (!em)
53 return NULL;
Chris Masona52d9a82007-08-27 16:49:44 -040054 em->in_tree = 0;
Chris Masond1310b22008-01-24 16:13:08 -050055 em->flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +080056 em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik5dc562c2012-08-17 13:14:17 -040057 em->generation = 0;
Chris Masona52d9a82007-08-27 16:49:44 -040058 atomic_set(&em->refs, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040059 INIT_LIST_HEAD(&em->list);
Chris Masona52d9a82007-08-27 16:49:44 -040060 return em;
61}
Chris Masona52d9a82007-08-27 16:49:44 -040062
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040063/**
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 Masona52d9a82007-08-27 16:49:44 -040070void free_extent_map(struct extent_map *em)
71{
Chris Mason2bf5a722007-08-30 11:54:02 -040072 if (!em)
73 return;
Chris Masond1310b22008-01-24 16:13:08 -050074 WARN_ON(atomic_read(&em->refs) == 0);
Chris Masona52d9a82007-08-27 16:49:44 -040075 if (atomic_dec_and_test(&em->refs)) {
76 WARN_ON(em->in_tree);
Josef Bacik5dc562c2012-08-17 13:14:17 -040077 WARN_ON(!list_empty(&em->list));
Chris Masona52d9a82007-08-27 16:49:44 -040078 kmem_cache_free(extent_map_cache, em);
79 }
80}
Chris Masona52d9a82007-08-27 16:49:44 -040081
Chris Masona52d9a82007-08-27 16:49:44 -040082static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
83 struct rb_node *node)
84{
Chris Masond3977122009-01-05 21:25:51 -050085 struct rb_node **p = &root->rb_node;
86 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -050087 struct extent_map *entry;
Chris Masona52d9a82007-08-27 16:49:44 -040088
Chris Masond3977122009-01-05 21:25:51 -050089 while (*p) {
Chris Masona52d9a82007-08-27 16:49:44 -040090 parent = *p;
Chris Masond1310b22008-01-24 16:13:08 -050091 entry = rb_entry(parent, struct extent_map, rb_node);
92
93 WARN_ON(!entry->in_tree);
Chris Masona52d9a82007-08-27 16:49:44 -040094
95 if (offset < entry->start)
96 p = &(*p)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -050097 else if (offset >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -040098 p = &(*p)->rb_right;
99 else
100 return parent;
101 }
102
Chris Masond1310b22008-01-24 16:13:08 -0500103 entry = rb_entry(node, struct extent_map, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400104 entry->in_tree = 1;
105 rb_link_node(node, parent, p);
106 rb_insert_color(node, root);
107 return NULL;
108}
109
Chris Masond352ac62008-09-29 15:18:18 -0400110/*
111 * search through the tree for an extent_map with a given offset. If
112 * it can't be found, try to find some neighboring extents
113 */
Chris Masona52d9a82007-08-27 16:49:44 -0400114static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
Chris Mason5f564062008-01-22 16:47:59 -0500115 struct rb_node **prev_ret,
116 struct rb_node **next_ret)
Chris Masona52d9a82007-08-27 16:49:44 -0400117{
Chris Masond3977122009-01-05 21:25:51 -0500118 struct rb_node *n = root->rb_node;
Chris Masona52d9a82007-08-27 16:49:44 -0400119 struct rb_node *prev = NULL;
Chris Mason5f564062008-01-22 16:47:59 -0500120 struct rb_node *orig_prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500121 struct extent_map *entry;
122 struct extent_map *prev_entry = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400123
Chris Masond3977122009-01-05 21:25:51 -0500124 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500125 entry = rb_entry(n, struct extent_map, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400126 prev = n;
127 prev_entry = entry;
128
Chris Masond1310b22008-01-24 16:13:08 -0500129 WARN_ON(!entry->in_tree);
130
Chris Masona52d9a82007-08-27 16:49:44 -0400131 if (offset < entry->start)
132 n = n->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500133 else if (offset >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -0400134 n = n->rb_right;
135 else
136 return n;
137 }
Chris Mason5f564062008-01-22 16:47:59 -0500138
139 if (prev_ret) {
140 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500141 while (prev && offset >= extent_map_end(prev_entry)) {
Chris Mason5f564062008-01-22 16:47:59 -0500142 prev = rb_next(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500143 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500144 }
145 *prev_ret = prev;
146 prev = orig_prev;
Chris Masona52d9a82007-08-27 16:49:44 -0400147 }
Chris Mason5f564062008-01-22 16:47:59 -0500148
149 if (next_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500150 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500151 while (prev && offset < prev_entry->start) {
Chris Mason5f564062008-01-22 16:47:59 -0500152 prev = rb_prev(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500153 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500154 }
155 *next_ret = prev;
156 }
Chris Masona52d9a82007-08-27 16:49:44 -0400157 return NULL;
158}
159
Chris Masond352ac62008-09-29 15:18:18 -0400160/* check to see if two extent_map structs are adjacent and safe to merge */
Chris Masond1310b22008-01-24 16:13:08 -0500161static int mergable_maps(struct extent_map *prev, struct extent_map *next)
Chris Masona52d9a82007-08-27 16:49:44 -0400162{
Chris Mason7f3c74f2008-07-18 12:01:11 -0400163 if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
164 return 0;
165
Chris Masonc8b97812008-10-29 14:49:59 -0400166 /*
167 * don't merge compressed extents, we need to know their
168 * actual size
169 */
170 if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
171 return 0;
172
Josef Bacik201a9032013-01-24 12:02:07 -0500173 if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
174 test_bit(EXTENT_FLAG_LOGGING, &next->flags))
175 return 0;
176
Chris Masond1310b22008-01-24 16:13:08 -0500177 if (extent_map_end(prev) == next->start &&
178 prev->flags == next->flags &&
179 prev->bdev == next->bdev &&
180 ((next->block_start == EXTENT_MAP_HOLE &&
181 prev->block_start == EXTENT_MAP_HOLE) ||
182 (next->block_start == EXTENT_MAP_INLINE &&
183 prev->block_start == EXTENT_MAP_INLINE) ||
184 (next->block_start == EXTENT_MAP_DELALLOC &&
185 prev->block_start == EXTENT_MAP_DELALLOC) ||
186 (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
187 next->block_start == extent_map_block_end(prev)))) {
188 return 1;
189 }
Chris Masona52d9a82007-08-27 16:49:44 -0400190 return 0;
191}
192
Li Zefan4d2c8f622011-07-14 03:18:33 +0000193static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
Chris Masona1ed8352009-09-11 12:27:37 -0400194{
Chris Masona1ed8352009-09-11 12:27:37 -0400195 struct extent_map *merge = NULL;
196 struct rb_node *rb;
Chris Masona1ed8352009-09-11 12:27:37 -0400197
198 if (em->start != 0) {
199 rb = rb_prev(&em->rb_node);
200 if (rb)
201 merge = rb_entry(rb, struct extent_map, rb_node);
202 if (rb && mergable_maps(merge, em)) {
203 em->start = merge->start;
Josef Bacik70c8a912012-10-11 16:54:30 -0400204 em->orig_start = merge->orig_start;
Chris Masona1ed8352009-09-11 12:27:37 -0400205 em->len += merge->len;
206 em->block_len += merge->block_len;
207 em->block_start = merge->block_start;
208 merge->in_tree = 0;
Josef Bacik70c8a912012-10-11 16:54:30 -0400209 em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
210 em->mod_start = merge->mod_start;
211 em->generation = max(em->generation, merge->generation);
212 list_move(&em->list, &tree->modified_extents);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400213
214 list_del_init(&merge->list);
Chris Masona1ed8352009-09-11 12:27:37 -0400215 rb_erase(&merge->rb_node, &tree->map);
216 free_extent_map(merge);
217 }
218 }
219
220 rb = rb_next(&em->rb_node);
221 if (rb)
222 merge = rb_entry(rb, struct extent_map, rb_node);
223 if (rb && mergable_maps(em, merge)) {
224 em->len += merge->len;
225 em->block_len += merge->len;
226 rb_erase(&merge->rb_node, &tree->map);
227 merge->in_tree = 0;
Josef Bacik70c8a912012-10-11 16:54:30 -0400228 em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
229 em->generation = max(em->generation, merge->generation);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400230 list_del_init(&merge->list);
Chris Masona1ed8352009-09-11 12:27:37 -0400231 free_extent_map(merge);
232 }
Li Zefan4d2c8f622011-07-14 03:18:33 +0000233}
234
Josef Bacik5dc562c2012-08-17 13:14:17 -0400235/**
Liu Bo52b1de92012-10-30 17:13:52 +0800236 * unpin_extent_cache - unpin an extent from the cache
Josef Bacik5dc562c2012-08-17 13:14:17 -0400237 * @tree: tree to unpin the extent in
238 * @start: logical offset in the file
239 * @len: length of the extent
240 * @gen: generation that this extent has been modified in
Josef Bacik5dc562c2012-08-17 13:14:17 -0400241 *
242 * Called after an extent has been written to disk properly. Set the generation
243 * to the generation that actually added the file item to the inode so we know
244 * we need to sync this extent when we call fsync().
245 */
246int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
247 u64 gen)
Li Zefan4d2c8f622011-07-14 03:18:33 +0000248{
249 int ret = 0;
250 struct extent_map *em;
Liu Bo4e2f84e2012-08-27 10:52:20 -0600251 bool prealloc = false;
Li Zefan4d2c8f622011-07-14 03:18:33 +0000252
253 write_lock(&tree->lock);
254 em = lookup_extent_mapping(tree, start, len);
255
256 WARN_ON(!em || em->start != start);
257
258 if (!em)
259 goto out;
260
Josef Bacik201a9032013-01-24 12:02:07 -0500261 if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
262 list_move(&em->list, &tree->modified_extents);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400263 em->generation = gen;
Li Zefan4d2c8f622011-07-14 03:18:33 +0000264 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo4e2f84e2012-08-27 10:52:20 -0600265 em->mod_start = em->start;
266 em->mod_len = em->len;
267
Josef Bacikb11e2342012-12-03 10:58:15 -0500268 if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
Liu Bo4e2f84e2012-08-27 10:52:20 -0600269 prealloc = true;
Josef Bacikb11e2342012-12-03 10:58:15 -0500270 clear_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo4e2f84e2012-08-27 10:52:20 -0600271 }
Li Zefan4d2c8f622011-07-14 03:18:33 +0000272
273 try_merge_map(tree, em);
Liu Bo4e2f84e2012-08-27 10:52:20 -0600274
275 if (prealloc) {
276 em->mod_start = em->start;
277 em->mod_len = em->len;
278 }
279
Chris Masona1ed8352009-09-11 12:27:37 -0400280 free_extent_map(em);
281out:
282 write_unlock(&tree->lock);
283 return ret;
284
285}
286
Josef Bacik201a9032013-01-24 12:02:07 -0500287void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
288{
289 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Josef Bacik222c81d2013-01-28 09:45:20 -0500290 if (em->in_tree)
291 try_merge_map(tree, em);
Josef Bacik201a9032013-01-24 12:02:07 -0500292}
293
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400294/**
295 * add_extent_mapping - add new extent map to the extent tree
296 * @tree: tree to insert new map in
297 * @em: map to insert
298 *
299 * Insert @em into @tree or perform a simple forward/backward merge with
300 * existing mappings. The extent_map struct passed in will be inserted
301 * into the tree directly, with an additional reference taken, or a
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300302 * reference dropped if the merge attempt was successful.
Chris Masona52d9a82007-08-27 16:49:44 -0400303 */
304int add_extent_mapping(struct extent_map_tree *tree,
305 struct extent_map *em)
306{
307 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400308 struct rb_node *rb;
Chris Mason7c2fe322008-08-20 08:51:50 -0400309 struct extent_map *exist;
Chris Masona52d9a82007-08-27 16:49:44 -0400310
Chris Mason7c2fe322008-08-20 08:51:50 -0400311 exist = lookup_extent_mapping(tree, em->start, em->len);
312 if (exist) {
313 free_extent_map(exist);
314 ret = -EEXIST;
315 goto out;
316 }
Chris Masond1310b22008-01-24 16:13:08 -0500317 rb = tree_insert(&tree->map, em->start, &em->rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400318 if (rb) {
Chris Masona52d9a82007-08-27 16:49:44 -0400319 ret = -EEXIST;
320 goto out;
321 }
322 atomic_inc(&em->refs);
Li Zefan4d2c8f622011-07-14 03:18:33 +0000323
Liu Bo4e2f84e2012-08-27 10:52:20 -0600324 em->mod_start = em->start;
325 em->mod_len = em->len;
326
Li Zefan4d2c8f622011-07-14 03:18:33 +0000327 try_merge_map(tree, em);
Chris Masona52d9a82007-08-27 16:49:44 -0400328out:
Chris Masona52d9a82007-08-27 16:49:44 -0400329 return ret;
330}
Chris Masona52d9a82007-08-27 16:49:44 -0400331
Chris Masond352ac62008-09-29 15:18:18 -0400332/* simple helper to do math around the end of an extent, handling wrap */
Chris Masond1310b22008-01-24 16:13:08 -0500333static u64 range_end(u64 start, u64 len)
334{
335 if (start + len < start)
336 return (u64)-1;
337 return start + len;
338}
339
Li Zefaned64f062011-07-14 03:18:15 +0000340struct extent_map *__lookup_extent_mapping(struct extent_map_tree *tree,
341 u64 start, u64 len, int strict)
342{
343 struct extent_map *em;
344 struct rb_node *rb_node;
345 struct rb_node *prev = NULL;
346 struct rb_node *next = NULL;
347 u64 end = range_end(start, len);
348
349 rb_node = __tree_search(&tree->map, start, &prev, &next);
350 if (!rb_node) {
351 if (prev)
352 rb_node = prev;
353 else if (next)
354 rb_node = next;
355 else
356 return NULL;
357 }
358
359 em = rb_entry(rb_node, struct extent_map, rb_node);
360
361 if (strict && !(end > em->start && start < extent_map_end(em)))
362 return NULL;
363
364 atomic_inc(&em->refs);
365 return em;
366}
367
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400368/**
369 * lookup_extent_mapping - lookup extent_map
370 * @tree: tree to lookup in
371 * @start: byte offset to start the search
372 * @len: length of the lookup range
373 *
374 * Find and return the first extent_map struct in @tree that intersects the
375 * [start, len] range. There may be additional objects in the tree that
376 * intersect, so check the object returned carefully to make sure that no
377 * additional lookups are needed.
Chris Masona52d9a82007-08-27 16:49:44 -0400378 */
379struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500380 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -0400381{
Li Zefaned64f062011-07-14 03:18:15 +0000382 return __lookup_extent_mapping(tree, start, len, 1);
Chris Masona52d9a82007-08-27 16:49:44 -0400383}
Chris Masona52d9a82007-08-27 16:49:44 -0400384
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400385/**
Chris Masonb917b7c2009-09-18 16:07:03 -0400386 * search_extent_mapping - find a nearby extent map
387 * @tree: tree to lookup in
388 * @start: byte offset to start the search
389 * @len: length of the lookup range
390 *
391 * Find and return the first extent_map struct in @tree that intersects the
392 * [start, len] range.
393 *
394 * If one can't be found, any nearby extent may be returned
395 */
396struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
397 u64 start, u64 len)
398{
Li Zefaned64f062011-07-14 03:18:15 +0000399 return __lookup_extent_mapping(tree, start, len, 0);
Chris Masonb917b7c2009-09-18 16:07:03 -0400400}
401
402/**
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400403 * remove_extent_mapping - removes an extent_map from the extent tree
404 * @tree: extent tree to remove from
405 * @em: extent map beeing removed
406 *
407 * Removes @em from @tree. No reference counts are dropped, and no checks
408 * are done to see if the range is in use
Chris Masona52d9a82007-08-27 16:49:44 -0400409 */
410int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
411{
Chris Masond1310b22008-01-24 16:13:08 -0500412 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400413
Chris Mason7f3c74f2008-07-18 12:01:11 -0400414 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
Chris Masond1310b22008-01-24 16:13:08 -0500415 rb_erase(&em->rb_node, &tree->map);
Josef Bacikff44c6e2012-09-14 12:59:20 -0400416 if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
417 list_del_init(&em->list);
Chris Masond1310b22008-01-24 16:13:08 -0500418 em->in_tree = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400419 return ret;
420}