blob: 50da69da20cec141fca63ffba2a4e92b8f93f875 [file] [log] [blame]
Chris Masond1310b22008-01-24 16:13:08 -05001#include <linux/err.h>
Chris Masona52d9a82007-08-27 16:49:44 -04002#include <linux/gfp.h>
Chris Masond1310b22008-01-24 16:13:08 -05003#include <linux/slab.h>
Chris Masona52d9a82007-08-27 16:49:44 -04004#include <linux/module.h>
5#include <linux/spinlock.h>
Chris Masond1310b22008-01-24 16:13:08 -05006#include <linux/hardirq.h>
Chris Masona52d9a82007-08-27 16:49:44 -04007#include "extent_map.h"
8
Chris Mason86479a02007-09-10 19:58:16 -04009/* temporary define until extent_map moves out of btrfs */
10struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
11 unsigned long extra_flags,
12 void (*ctor)(void *, struct kmem_cache *,
13 unsigned long));
14
Chris Masona52d9a82007-08-27 16:49:44 -040015static struct kmem_cache *extent_map_cache;
Chris Masonca664622007-11-27 11:16:35 -050016
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050017int __init extent_map_init(void)
Chris Masona52d9a82007-08-27 16:49:44 -040018{
Chris Mason86479a02007-09-10 19:58:16 -040019 extent_map_cache = btrfs_cache_create("extent_map",
Chris Mason6d36dcd2007-10-15 16:14:37 -040020 sizeof(struct extent_map), 0,
Chris Masona52d9a82007-08-27 16:49:44 -040021 NULL);
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050022 if (!extent_map_cache)
23 return -ENOMEM;
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050024 return 0;
Chris Masona52d9a82007-08-27 16:49:44 -040025}
26
Christian Hesse17636e02007-12-11 09:25:06 -050027void extent_map_exit(void)
Chris Masona52d9a82007-08-27 16:49:44 -040028{
Chris Masona52d9a82007-08-27 16:49:44 -040029 if (extent_map_cache)
30 kmem_cache_destroy(extent_map_cache);
Chris Masona52d9a82007-08-27 16:49:44 -040031}
32
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040033/**
34 * extent_map_tree_init - initialize extent map tree
35 * @tree: tree to initialize
36 * @mask: flags for memory allocations during tree operations
37 *
38 * Initialize the extent tree @tree. Should be called for each new inode
39 * or other user of the extent_map interface.
40 */
Chris Masond1310b22008-01-24 16:13:08 -050041void extent_map_tree_init(struct extent_map_tree *tree, gfp_t mask)
Chris Masona52d9a82007-08-27 16:49:44 -040042{
43 tree->map.rb_node = NULL;
Chris Masond1310b22008-01-24 16:13:08 -050044 spin_lock_init(&tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -040045}
46EXPORT_SYMBOL(extent_map_tree_init);
47
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040048/**
49 * alloc_extent_map - allocate new extent map structure
50 * @mask: memory allocation flags
51 *
52 * Allocate a new extent_map structure. The new structure is
53 * returned with a reference count of one and needs to be
54 * freed using free_extent_map()
55 */
Chris Masona52d9a82007-08-27 16:49:44 -040056struct extent_map *alloc_extent_map(gfp_t mask)
57{
58 struct extent_map *em;
59 em = kmem_cache_alloc(extent_map_cache, mask);
60 if (!em || IS_ERR(em))
61 return em;
62 em->in_tree = 0;
Chris Masond1310b22008-01-24 16:13:08 -050063 em->flags = 0;
Chris Masona52d9a82007-08-27 16:49:44 -040064 atomic_set(&em->refs, 1);
65 return em;
66}
67EXPORT_SYMBOL(alloc_extent_map);
68
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040069/**
70 * free_extent_map - drop reference count of an extent_map
71 * @em: extent map beeing releasead
72 *
73 * Drops the reference out on @em by one and free the structure
74 * if the reference count hits zero.
75 */
Chris Masona52d9a82007-08-27 16:49:44 -040076void free_extent_map(struct extent_map *em)
77{
Chris Mason2bf5a722007-08-30 11:54:02 -040078 if (!em)
79 return;
Chris Masond1310b22008-01-24 16:13:08 -050080 WARN_ON(atomic_read(&em->refs) == 0);
Chris Masona52d9a82007-08-27 16:49:44 -040081 if (atomic_dec_and_test(&em->refs)) {
82 WARN_ON(em->in_tree);
83 kmem_cache_free(extent_map_cache, em);
84 }
85}
86EXPORT_SYMBOL(free_extent_map);
87
Chris Masona52d9a82007-08-27 16:49:44 -040088static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
89 struct rb_node *node)
90{
Chris Masond3977122009-01-05 21:25:51 -050091 struct rb_node **p = &root->rb_node;
92 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -050093 struct extent_map *entry;
Chris Masona52d9a82007-08-27 16:49:44 -040094
Chris Masond3977122009-01-05 21:25:51 -050095 while (*p) {
Chris Masona52d9a82007-08-27 16:49:44 -040096 parent = *p;
Chris Masond1310b22008-01-24 16:13:08 -050097 entry = rb_entry(parent, struct extent_map, rb_node);
98
99 WARN_ON(!entry->in_tree);
Chris Masona52d9a82007-08-27 16:49:44 -0400100
101 if (offset < entry->start)
102 p = &(*p)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500103 else if (offset >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -0400104 p = &(*p)->rb_right;
105 else
106 return parent;
107 }
108
Chris Masond1310b22008-01-24 16:13:08 -0500109 entry = rb_entry(node, struct extent_map, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400110 entry->in_tree = 1;
111 rb_link_node(node, parent, p);
112 rb_insert_color(node, root);
113 return NULL;
114}
115
Chris Masond352ac62008-09-29 15:18:18 -0400116/*
117 * search through the tree for an extent_map with a given offset. If
118 * it can't be found, try to find some neighboring extents
119 */
Chris Masona52d9a82007-08-27 16:49:44 -0400120static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
Chris Mason5f564062008-01-22 16:47:59 -0500121 struct rb_node **prev_ret,
122 struct rb_node **next_ret)
Chris Masona52d9a82007-08-27 16:49:44 -0400123{
Chris Masond3977122009-01-05 21:25:51 -0500124 struct rb_node *n = root->rb_node;
Chris Masona52d9a82007-08-27 16:49:44 -0400125 struct rb_node *prev = NULL;
Chris Mason5f564062008-01-22 16:47:59 -0500126 struct rb_node *orig_prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500127 struct extent_map *entry;
128 struct extent_map *prev_entry = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400129
Chris Masond3977122009-01-05 21:25:51 -0500130 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500131 entry = rb_entry(n, struct extent_map, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400132 prev = n;
133 prev_entry = entry;
134
Chris Masond1310b22008-01-24 16:13:08 -0500135 WARN_ON(!entry->in_tree);
136
Chris Masona52d9a82007-08-27 16:49:44 -0400137 if (offset < entry->start)
138 n = n->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500139 else if (offset >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -0400140 n = n->rb_right;
141 else
142 return n;
143 }
Chris Mason5f564062008-01-22 16:47:59 -0500144
145 if (prev_ret) {
146 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500147 while (prev && offset >= extent_map_end(prev_entry)) {
Chris Mason5f564062008-01-22 16:47:59 -0500148 prev = rb_next(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500149 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500150 }
151 *prev_ret = prev;
152 prev = orig_prev;
Chris Masona52d9a82007-08-27 16:49:44 -0400153 }
Chris Mason5f564062008-01-22 16:47:59 -0500154
155 if (next_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500156 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500157 while (prev && offset < prev_entry->start) {
Chris Mason5f564062008-01-22 16:47:59 -0500158 prev = rb_prev(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500159 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500160 }
161 *next_ret = prev;
162 }
Chris Masona52d9a82007-08-27 16:49:44 -0400163 return NULL;
164}
165
Chris Masond352ac62008-09-29 15:18:18 -0400166/*
167 * look for an offset in the tree, and if it can't be found, return
168 * the first offset we can find smaller than 'offset'.
169 */
Chris Masona52d9a82007-08-27 16:49:44 -0400170static inline struct rb_node *tree_search(struct rb_root *root, u64 offset)
171{
172 struct rb_node *prev;
173 struct rb_node *ret;
Chris Mason5f564062008-01-22 16:47:59 -0500174 ret = __tree_search(root, offset, &prev, NULL);
Chris Masona52d9a82007-08-27 16:49:44 -0400175 if (!ret)
176 return prev;
177 return ret;
178}
179
Chris Masond352ac62008-09-29 15:18:18 -0400180/* check to see if two extent_map structs are adjacent and safe to merge */
Chris Masond1310b22008-01-24 16:13:08 -0500181static int mergable_maps(struct extent_map *prev, struct extent_map *next)
Chris Masona52d9a82007-08-27 16:49:44 -0400182{
Chris Mason7f3c74f2008-07-18 12:01:11 -0400183 if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
184 return 0;
185
Chris Masonc8b97812008-10-29 14:49:59 -0400186 /*
187 * don't merge compressed extents, we need to know their
188 * actual size
189 */
190 if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
191 return 0;
192
Chris Masond1310b22008-01-24 16:13:08 -0500193 if (extent_map_end(prev) == next->start &&
194 prev->flags == next->flags &&
195 prev->bdev == next->bdev &&
196 ((next->block_start == EXTENT_MAP_HOLE &&
197 prev->block_start == EXTENT_MAP_HOLE) ||
198 (next->block_start == EXTENT_MAP_INLINE &&
199 prev->block_start == EXTENT_MAP_INLINE) ||
200 (next->block_start == EXTENT_MAP_DELALLOC &&
201 prev->block_start == EXTENT_MAP_DELALLOC) ||
202 (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
203 next->block_start == extent_map_block_end(prev)))) {
204 return 1;
205 }
Chris Masona52d9a82007-08-27 16:49:44 -0400206 return 0;
207}
208
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400209/**
210 * add_extent_mapping - add new extent map to the extent tree
211 * @tree: tree to insert new map in
212 * @em: map to insert
213 *
214 * Insert @em into @tree or perform a simple forward/backward merge with
215 * existing mappings. The extent_map struct passed in will be inserted
216 * into the tree directly, with an additional reference taken, or a
217 * reference dropped if the merge attempt was sucessfull.
Chris Masona52d9a82007-08-27 16:49:44 -0400218 */
219int add_extent_mapping(struct extent_map_tree *tree,
220 struct extent_map *em)
221{
222 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500223 struct extent_map *merge = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400224 struct rb_node *rb;
Chris Mason7c2fe322008-08-20 08:51:50 -0400225 struct extent_map *exist;
Chris Masona52d9a82007-08-27 16:49:44 -0400226
Chris Mason7c2fe322008-08-20 08:51:50 -0400227 exist = lookup_extent_mapping(tree, em->start, em->len);
228 if (exist) {
229 free_extent_map(exist);
230 ret = -EEXIST;
231 goto out;
232 }
David Woodhouse64f26f72008-07-24 10:09:43 -0400233 assert_spin_locked(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500234 rb = tree_insert(&tree->map, em->start, &em->rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400235 if (rb) {
Chris Masona52d9a82007-08-27 16:49:44 -0400236 ret = -EEXIST;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400237 free_extent_map(merge);
Chris Masona52d9a82007-08-27 16:49:44 -0400238 goto out;
239 }
240 atomic_inc(&em->refs);
241 if (em->start != 0) {
242 rb = rb_prev(&em->rb_node);
243 if (rb)
Chris Masond1310b22008-01-24 16:13:08 -0500244 merge = rb_entry(rb, struct extent_map, rb_node);
245 if (rb && mergable_maps(merge, em)) {
246 em->start = merge->start;
247 em->len += merge->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400248 em->block_len += merge->block_len;
Chris Masond1310b22008-01-24 16:13:08 -0500249 em->block_start = merge->block_start;
250 merge->in_tree = 0;
251 rb_erase(&merge->rb_node, &tree->map);
252 free_extent_map(merge);
Chris Masona52d9a82007-08-27 16:49:44 -0400253 }
254 }
Chris Masond1310b22008-01-24 16:13:08 -0500255 rb = rb_next(&em->rb_node);
256 if (rb)
257 merge = rb_entry(rb, struct extent_map, rb_node);
258 if (rb && mergable_maps(em, merge)) {
259 em->len += merge->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400260 em->block_len += merge->len;
Chris Masond1310b22008-01-24 16:13:08 -0500261 rb_erase(&merge->rb_node, &tree->map);
262 merge->in_tree = 0;
263 free_extent_map(merge);
264 }
Chris Masona52d9a82007-08-27 16:49:44 -0400265out:
Chris Masona52d9a82007-08-27 16:49:44 -0400266 return ret;
267}
268EXPORT_SYMBOL(add_extent_mapping);
269
Chris Masond352ac62008-09-29 15:18:18 -0400270/* simple helper to do math around the end of an extent, handling wrap */
Chris Masond1310b22008-01-24 16:13:08 -0500271static u64 range_end(u64 start, u64 len)
272{
273 if (start + len < start)
274 return (u64)-1;
275 return start + len;
276}
277
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400278/**
279 * lookup_extent_mapping - lookup extent_map
280 * @tree: tree to lookup in
281 * @start: byte offset to start the search
282 * @len: length of the lookup range
283 *
284 * Find and return the first extent_map struct in @tree that intersects the
285 * [start, len] range. There may be additional objects in the tree that
286 * intersect, so check the object returned carefully to make sure that no
287 * additional lookups are needed.
Chris Masona52d9a82007-08-27 16:49:44 -0400288 */
289struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500290 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -0400291{
292 struct extent_map *em;
293 struct rb_node *rb_node;
Christoph Hellwig306929f2008-06-10 10:21:04 -0400294 struct rb_node *prev = NULL;
295 struct rb_node *next = NULL;
296 u64 end = range_end(start, len);
297
David Woodhouse64f26f72008-07-24 10:09:43 -0400298 assert_spin_locked(&tree->lock);
Chris Mason5f564062008-01-22 16:47:59 -0500299 rb_node = __tree_search(&tree->map, start, &prev, &next);
300 if (!rb_node && prev) {
301 em = rb_entry(prev, struct extent_map, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500302 if (end > em->start && start < extent_map_end(em))
Chris Mason5f564062008-01-22 16:47:59 -0500303 goto found;
304 }
305 if (!rb_node && next) {
306 em = rb_entry(next, struct extent_map, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500307 if (end > em->start && start < extent_map_end(em))
Chris Mason5f564062008-01-22 16:47:59 -0500308 goto found;
309 }
Chris Masona52d9a82007-08-27 16:49:44 -0400310 if (!rb_node) {
311 em = NULL;
312 goto out;
313 }
314 if (IS_ERR(rb_node)) {
315 em = ERR_PTR(PTR_ERR(rb_node));
316 goto out;
317 }
318 em = rb_entry(rb_node, struct extent_map, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500319 if (end > em->start && start < extent_map_end(em))
320 goto found;
321
322 em = NULL;
323 goto out;
324
Chris Mason5f564062008-01-22 16:47:59 -0500325found:
Chris Masona52d9a82007-08-27 16:49:44 -0400326 atomic_inc(&em->refs);
327out:
Chris Masona52d9a82007-08-27 16:49:44 -0400328 return em;
329}
330EXPORT_SYMBOL(lookup_extent_mapping);
331
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400332/**
333 * remove_extent_mapping - removes an extent_map from the extent tree
334 * @tree: extent tree to remove from
335 * @em: extent map beeing removed
336 *
337 * Removes @em from @tree. No reference counts are dropped, and no checks
338 * are done to see if the range is in use
Chris Masona52d9a82007-08-27 16:49:44 -0400339 */
340int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
341{
Chris Masond1310b22008-01-24 16:13:08 -0500342 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400343
Chris Mason7f3c74f2008-07-18 12:01:11 -0400344 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
David Woodhouse64f26f72008-07-24 10:09:43 -0400345 assert_spin_locked(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500346 rb_erase(&em->rb_node, &tree->map);
347 em->in_tree = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400348 return ret;
349}
350EXPORT_SYMBOL(remove_extent_mapping);