blob: 1fe82cfc1d933986863608eb24dd586ba609a10b [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/module.h>
4#include <linux/spinlock.h>
Chris Masond1310b22008-01-24 16:13:08 -05005#include <linux/hardirq.h>
Li Zefan261507a02010-12-17 14:21:50 +08006#include "ctree.h"
Chris Masona52d9a82007-08-27 16:49:44 -04007#include "extent_map.h"
8
Chris Mason86479a02007-09-10 19:58:16 -04009
Chris Masona52d9a82007-08-27 16:49:44 -040010static struct kmem_cache *extent_map_cache;
Chris Masonca664622007-11-27 11:16:35 -050011
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050012int __init extent_map_init(void)
Chris Masona52d9a82007-08-27 16:49:44 -040013{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020014 extent_map_cache = kmem_cache_create("extent_map",
15 sizeof(struct extent_map), 0,
16 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050017 if (!extent_map_cache)
18 return -ENOMEM;
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050019 return 0;
Chris Masona52d9a82007-08-27 16:49:44 -040020}
21
Christian Hesse17636e02007-12-11 09:25:06 -050022void extent_map_exit(void)
Chris Masona52d9a82007-08-27 16:49:44 -040023{
Chris Masona52d9a82007-08-27 16:49:44 -040024 if (extent_map_cache)
25 kmem_cache_destroy(extent_map_cache);
Chris Masona52d9a82007-08-27 16:49:44 -040026}
27
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040028/**
29 * extent_map_tree_init - initialize extent map tree
30 * @tree: tree to initialize
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040031 *
32 * Initialize the extent tree @tree. Should be called for each new inode
33 * or other user of the extent_map interface.
34 */
David Sterbaa8067e02011-04-21 00:34:43 +020035void extent_map_tree_init(struct extent_map_tree *tree)
Chris Masona52d9a82007-08-27 16:49:44 -040036{
Eric Paris6bef4d32010-02-23 19:43:04 +000037 tree->map = RB_ROOT;
Josef Bacik5dc562c2012-08-17 13:14:17 -040038 INIT_LIST_HEAD(&tree->modified_extents);
Chris Mason890871b2009-09-02 16:24:52 -040039 rwlock_init(&tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -040040}
Chris Masona52d9a82007-08-27 16:49:44 -040041
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040042/**
43 * alloc_extent_map - allocate new extent map structure
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040044 *
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 Sterba172ddd62011-04-21 00:48:27 +020049struct extent_map *alloc_extent_map(void)
Chris Masona52d9a82007-08-27 16:49:44 -040050{
51 struct extent_map *em;
David Sterba172ddd62011-04-21 00:48:27 +020052 em = kmem_cache_alloc(extent_map_cache, GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +000053 if (!em)
54 return NULL;
Chris Masona52d9a82007-08-27 16:49:44 -040055 em->in_tree = 0;
Chris Masond1310b22008-01-24 16:13:08 -050056 em->flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +080057 em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik5dc562c2012-08-17 13:14:17 -040058 em->generation = 0;
Chris Masona52d9a82007-08-27 16:49:44 -040059 atomic_set(&em->refs, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040060 INIT_LIST_HEAD(&em->list);
Chris Masona52d9a82007-08-27 16:49:44 -040061 return em;
62}
Chris Masona52d9a82007-08-27 16:49:44 -040063
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040064/**
65 * free_extent_map - drop reference count of an extent_map
66 * @em: extent map beeing releasead
67 *
68 * Drops the reference out on @em by one and free the structure
69 * if the reference count hits zero.
70 */
Chris Masona52d9a82007-08-27 16:49:44 -040071void free_extent_map(struct extent_map *em)
72{
Chris Mason2bf5a722007-08-30 11:54:02 -040073 if (!em)
74 return;
Chris Masond1310b22008-01-24 16:13:08 -050075 WARN_ON(atomic_read(&em->refs) == 0);
Chris Masona52d9a82007-08-27 16:49:44 -040076 if (atomic_dec_and_test(&em->refs)) {
77 WARN_ON(em->in_tree);
Josef Bacik5dc562c2012-08-17 13:14:17 -040078 WARN_ON(!list_empty(&em->list));
Chris Masona52d9a82007-08-27 16:49:44 -040079 kmem_cache_free(extent_map_cache, em);
80 }
81}
Chris Masona52d9a82007-08-27 16:49:44 -040082
Chris Masona52d9a82007-08-27 16:49:44 -040083static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
84 struct rb_node *node)
85{
Chris Masond3977122009-01-05 21:25:51 -050086 struct rb_node **p = &root->rb_node;
87 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -050088 struct extent_map *entry;
Chris Masona52d9a82007-08-27 16:49:44 -040089
Chris Masond3977122009-01-05 21:25:51 -050090 while (*p) {
Chris Masona52d9a82007-08-27 16:49:44 -040091 parent = *p;
Chris Masond1310b22008-01-24 16:13:08 -050092 entry = rb_entry(parent, struct extent_map, rb_node);
93
94 WARN_ON(!entry->in_tree);
Chris Masona52d9a82007-08-27 16:49:44 -040095
96 if (offset < entry->start)
97 p = &(*p)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -050098 else if (offset >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -040099 p = &(*p)->rb_right;
100 else
101 return parent;
102 }
103
Chris Masond1310b22008-01-24 16:13:08 -0500104 entry = rb_entry(node, struct extent_map, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400105 entry->in_tree = 1;
106 rb_link_node(node, parent, p);
107 rb_insert_color(node, root);
108 return NULL;
109}
110
Chris Masond352ac62008-09-29 15:18:18 -0400111/*
112 * search through the tree for an extent_map with a given offset. If
113 * it can't be found, try to find some neighboring extents
114 */
Chris Masona52d9a82007-08-27 16:49:44 -0400115static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
Chris Mason5f564062008-01-22 16:47:59 -0500116 struct rb_node **prev_ret,
117 struct rb_node **next_ret)
Chris Masona52d9a82007-08-27 16:49:44 -0400118{
Chris Masond3977122009-01-05 21:25:51 -0500119 struct rb_node *n = root->rb_node;
Chris Masona52d9a82007-08-27 16:49:44 -0400120 struct rb_node *prev = NULL;
Chris Mason5f564062008-01-22 16:47:59 -0500121 struct rb_node *orig_prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500122 struct extent_map *entry;
123 struct extent_map *prev_entry = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400124
Chris Masond3977122009-01-05 21:25:51 -0500125 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500126 entry = rb_entry(n, struct extent_map, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400127 prev = n;
128 prev_entry = entry;
129
Chris Masond1310b22008-01-24 16:13:08 -0500130 WARN_ON(!entry->in_tree);
131
Chris Masona52d9a82007-08-27 16:49:44 -0400132 if (offset < entry->start)
133 n = n->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500134 else if (offset >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -0400135 n = n->rb_right;
136 else
137 return n;
138 }
Chris Mason5f564062008-01-22 16:47:59 -0500139
140 if (prev_ret) {
141 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500142 while (prev && offset >= extent_map_end(prev_entry)) {
Chris Mason5f564062008-01-22 16:47:59 -0500143 prev = rb_next(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500144 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500145 }
146 *prev_ret = prev;
147 prev = orig_prev;
Chris Masona52d9a82007-08-27 16:49:44 -0400148 }
Chris Mason5f564062008-01-22 16:47:59 -0500149
150 if (next_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500151 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500152 while (prev && offset < prev_entry->start) {
Chris Mason5f564062008-01-22 16:47:59 -0500153 prev = rb_prev(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500154 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500155 }
156 *next_ret = prev;
157 }
Chris Masona52d9a82007-08-27 16:49:44 -0400158 return NULL;
159}
160
Chris Masond352ac62008-09-29 15:18:18 -0400161/* check to see if two extent_map structs are adjacent and safe to merge */
Chris Masond1310b22008-01-24 16:13:08 -0500162static int mergable_maps(struct extent_map *prev, struct extent_map *next)
Chris Masona52d9a82007-08-27 16:49:44 -0400163{
Chris Mason7f3c74f2008-07-18 12:01:11 -0400164 if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
165 return 0;
166
Chris Masonc8b97812008-10-29 14:49:59 -0400167 /*
168 * don't merge compressed extents, we need to know their
169 * actual size
170 */
171 if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
172 return 0;
173
Chris Masond1310b22008-01-24 16:13:08 -0500174 if (extent_map_end(prev) == next->start &&
175 prev->flags == next->flags &&
176 prev->bdev == next->bdev &&
177 ((next->block_start == EXTENT_MAP_HOLE &&
178 prev->block_start == EXTENT_MAP_HOLE) ||
179 (next->block_start == EXTENT_MAP_INLINE &&
180 prev->block_start == EXTENT_MAP_INLINE) ||
181 (next->block_start == EXTENT_MAP_DELALLOC &&
182 prev->block_start == EXTENT_MAP_DELALLOC) ||
183 (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
184 next->block_start == extent_map_block_end(prev)))) {
185 return 1;
186 }
Chris Masona52d9a82007-08-27 16:49:44 -0400187 return 0;
188}
189
Li Zefan4d2c8f622011-07-14 03:18:33 +0000190static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
Chris Masona1ed8352009-09-11 12:27:37 -0400191{
Chris Masona1ed8352009-09-11 12:27:37 -0400192 struct extent_map *merge = NULL;
193 struct rb_node *rb;
Chris Masona1ed8352009-09-11 12:27:37 -0400194
195 if (em->start != 0) {
196 rb = rb_prev(&em->rb_node);
197 if (rb)
198 merge = rb_entry(rb, struct extent_map, rb_node);
199 if (rb && mergable_maps(merge, em)) {
200 em->start = merge->start;
201 em->len += merge->len;
202 em->block_len += merge->block_len;
203 em->block_start = merge->block_start;
204 merge->in_tree = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400205 if (merge->generation > em->generation) {
206 em->generation = merge->generation;
207 list_move(&em->list, &tree->modified_extents);
208 }
209
210 list_del_init(&merge->list);
Chris Masona1ed8352009-09-11 12:27:37 -0400211 rb_erase(&merge->rb_node, &tree->map);
212 free_extent_map(merge);
213 }
214 }
215
216 rb = rb_next(&em->rb_node);
217 if (rb)
218 merge = rb_entry(rb, struct extent_map, rb_node);
219 if (rb && mergable_maps(em, merge)) {
220 em->len += merge->len;
221 em->block_len += merge->len;
222 rb_erase(&merge->rb_node, &tree->map);
223 merge->in_tree = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400224 if (merge->generation > em->generation) {
225 em->generation = merge->generation;
226 list_move(&em->list, &tree->modified_extents);
227 }
228 list_del_init(&merge->list);
Chris Masona1ed8352009-09-11 12:27:37 -0400229 free_extent_map(merge);
230 }
Li Zefan4d2c8f622011-07-14 03:18:33 +0000231}
232
Josef Bacik5dc562c2012-08-17 13:14:17 -0400233/**
234 * unpint_extent_cache - unpin an extent from the cache
235 * @tree: tree to unpin the extent in
236 * @start: logical offset in the file
237 * @len: length of the extent
238 * @gen: generation that this extent has been modified in
239 * @prealloc: if this is set we need to clear the prealloc flag
240 *
241 * Called after an extent has been written to disk properly. Set the generation
242 * to the generation that actually added the file item to the inode so we know
243 * we need to sync this extent when we call fsync().
244 */
245int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
246 u64 gen)
Li Zefan4d2c8f622011-07-14 03:18:33 +0000247{
248 int ret = 0;
249 struct extent_map *em;
250
251 write_lock(&tree->lock);
252 em = lookup_extent_mapping(tree, start, len);
253
254 WARN_ON(!em || em->start != start);
255
256 if (!em)
257 goto out;
258
Josef Bacik5dc562c2012-08-17 13:14:17 -0400259 list_move(&em->list, &tree->modified_extents);
260 em->generation = gen;
Li Zefan4d2c8f622011-07-14 03:18:33 +0000261 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
262
263 try_merge_map(tree, em);
Chris Masona1ed8352009-09-11 12:27:37 -0400264 free_extent_map(em);
265out:
266 write_unlock(&tree->lock);
267 return ret;
268
269}
270
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400271/**
272 * add_extent_mapping - add new extent map to the extent tree
273 * @tree: tree to insert new map in
274 * @em: map to insert
275 *
276 * Insert @em into @tree or perform a simple forward/backward merge with
277 * existing mappings. The extent_map struct passed in will be inserted
278 * into the tree directly, with an additional reference taken, or a
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300279 * reference dropped if the merge attempt was successful.
Chris Masona52d9a82007-08-27 16:49:44 -0400280 */
281int add_extent_mapping(struct extent_map_tree *tree,
282 struct extent_map *em)
283{
284 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400285 struct rb_node *rb;
Chris Mason7c2fe322008-08-20 08:51:50 -0400286 struct extent_map *exist;
Chris Masona52d9a82007-08-27 16:49:44 -0400287
Chris Mason7c2fe322008-08-20 08:51:50 -0400288 exist = lookup_extent_mapping(tree, em->start, em->len);
289 if (exist) {
290 free_extent_map(exist);
291 ret = -EEXIST;
292 goto out;
293 }
Chris Masond1310b22008-01-24 16:13:08 -0500294 rb = tree_insert(&tree->map, em->start, &em->rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400295 if (rb) {
Chris Masona52d9a82007-08-27 16:49:44 -0400296 ret = -EEXIST;
297 goto out;
298 }
299 atomic_inc(&em->refs);
Li Zefan4d2c8f622011-07-14 03:18:33 +0000300
301 try_merge_map(tree, em);
Chris Masona52d9a82007-08-27 16:49:44 -0400302out:
Chris Masona52d9a82007-08-27 16:49:44 -0400303 return ret;
304}
Chris Masona52d9a82007-08-27 16:49:44 -0400305
Chris Masond352ac62008-09-29 15:18:18 -0400306/* simple helper to do math around the end of an extent, handling wrap */
Chris Masond1310b22008-01-24 16:13:08 -0500307static u64 range_end(u64 start, u64 len)
308{
309 if (start + len < start)
310 return (u64)-1;
311 return start + len;
312}
313
Li Zefaned64f062011-07-14 03:18:15 +0000314struct extent_map *__lookup_extent_mapping(struct extent_map_tree *tree,
315 u64 start, u64 len, int strict)
316{
317 struct extent_map *em;
318 struct rb_node *rb_node;
319 struct rb_node *prev = NULL;
320 struct rb_node *next = NULL;
321 u64 end = range_end(start, len);
322
323 rb_node = __tree_search(&tree->map, start, &prev, &next);
324 if (!rb_node) {
325 if (prev)
326 rb_node = prev;
327 else if (next)
328 rb_node = next;
329 else
330 return NULL;
331 }
332
333 em = rb_entry(rb_node, struct extent_map, rb_node);
334
335 if (strict && !(end > em->start && start < extent_map_end(em)))
336 return NULL;
337
338 atomic_inc(&em->refs);
339 return em;
340}
341
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400342/**
343 * lookup_extent_mapping - lookup extent_map
344 * @tree: tree to lookup in
345 * @start: byte offset to start the search
346 * @len: length of the lookup range
347 *
348 * Find and return the first extent_map struct in @tree that intersects the
349 * [start, len] range. There may be additional objects in the tree that
350 * intersect, so check the object returned carefully to make sure that no
351 * additional lookups are needed.
Chris Masona52d9a82007-08-27 16:49:44 -0400352 */
353struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500354 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -0400355{
Li Zefaned64f062011-07-14 03:18:15 +0000356 return __lookup_extent_mapping(tree, start, len, 1);
Chris Masona52d9a82007-08-27 16:49:44 -0400357}
Chris Masona52d9a82007-08-27 16:49:44 -0400358
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400359/**
Chris Masonb917b7c2009-09-18 16:07:03 -0400360 * search_extent_mapping - find a nearby extent map
361 * @tree: tree to lookup in
362 * @start: byte offset to start the search
363 * @len: length of the lookup range
364 *
365 * Find and return the first extent_map struct in @tree that intersects the
366 * [start, len] range.
367 *
368 * If one can't be found, any nearby extent may be returned
369 */
370struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
371 u64 start, u64 len)
372{
Li Zefaned64f062011-07-14 03:18:15 +0000373 return __lookup_extent_mapping(tree, start, len, 0);
Chris Masonb917b7c2009-09-18 16:07:03 -0400374}
375
376/**
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400377 * remove_extent_mapping - removes an extent_map from the extent tree
378 * @tree: extent tree to remove from
379 * @em: extent map beeing removed
380 *
381 * Removes @em from @tree. No reference counts are dropped, and no checks
382 * are done to see if the range is in use
Chris Masona52d9a82007-08-27 16:49:44 -0400383 */
384int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
385{
Chris Masond1310b22008-01-24 16:13:08 -0500386 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400387
Chris Mason7f3c74f2008-07-18 12:01:11 -0400388 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
Chris Masond1310b22008-01-24 16:13:08 -0500389 rb_erase(&em->rb_node, &tree->map);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400390 list_del_init(&em->list);
Chris Masond1310b22008-01-24 16:13:08 -0500391 em->in_tree = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400392 return ret;
393}