blob: b187917b36fa8c19202b1e5049f3bbf9aaef451f [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;
237 goto out;
238 }
239 atomic_inc(&em->refs);
240 if (em->start != 0) {
241 rb = rb_prev(&em->rb_node);
242 if (rb)
Chris Masond1310b22008-01-24 16:13:08 -0500243 merge = rb_entry(rb, struct extent_map, rb_node);
244 if (rb && mergable_maps(merge, em)) {
245 em->start = merge->start;
246 em->len += merge->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400247 em->block_len += merge->block_len;
Chris Masond1310b22008-01-24 16:13:08 -0500248 em->block_start = merge->block_start;
249 merge->in_tree = 0;
250 rb_erase(&merge->rb_node, &tree->map);
251 free_extent_map(merge);
Chris Masona52d9a82007-08-27 16:49:44 -0400252 }
253 }
Chris Masond1310b22008-01-24 16:13:08 -0500254 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;
Chris Masonc8b97812008-10-29 14:49:59 -0400259 em->block_len += merge->len;
Chris Masond1310b22008-01-24 16:13:08 -0500260 rb_erase(&merge->rb_node, &tree->map);
261 merge->in_tree = 0;
262 free_extent_map(merge);
263 }
Chris Masona52d9a82007-08-27 16:49:44 -0400264out:
Chris Masona52d9a82007-08-27 16:49:44 -0400265 return ret;
266}
267EXPORT_SYMBOL(add_extent_mapping);
268
Chris Masond352ac62008-09-29 15:18:18 -0400269/* simple helper to do math around the end of an extent, handling wrap */
Chris Masond1310b22008-01-24 16:13:08 -0500270static u64 range_end(u64 start, u64 len)
271{
272 if (start + len < start)
273 return (u64)-1;
274 return start + len;
275}
276
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400277/**
278 * lookup_extent_mapping - lookup extent_map
279 * @tree: tree to lookup in
280 * @start: byte offset to start the search
281 * @len: length of the lookup range
282 *
283 * Find and return the first extent_map struct in @tree that intersects the
284 * [start, len] range. There may be additional objects in the tree that
285 * intersect, so check the object returned carefully to make sure that no
286 * additional lookups are needed.
Chris Masona52d9a82007-08-27 16:49:44 -0400287 */
288struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500289 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -0400290{
291 struct extent_map *em;
292 struct rb_node *rb_node;
Christoph Hellwig306929f2008-06-10 10:21:04 -0400293 struct rb_node *prev = NULL;
294 struct rb_node *next = NULL;
295 u64 end = range_end(start, len);
296
David Woodhouse64f26f72008-07-24 10:09:43 -0400297 assert_spin_locked(&tree->lock);
Chris Mason5f564062008-01-22 16:47:59 -0500298 rb_node = __tree_search(&tree->map, start, &prev, &next);
299 if (!rb_node && prev) {
300 em = rb_entry(prev, struct extent_map, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500301 if (end > em->start && start < extent_map_end(em))
Chris Mason5f564062008-01-22 16:47:59 -0500302 goto found;
303 }
304 if (!rb_node && next) {
305 em = rb_entry(next, struct extent_map, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500306 if (end > em->start && start < extent_map_end(em))
Chris Mason5f564062008-01-22 16:47:59 -0500307 goto found;
308 }
Chris Masona52d9a82007-08-27 16:49:44 -0400309 if (!rb_node) {
310 em = NULL;
311 goto out;
312 }
313 if (IS_ERR(rb_node)) {
314 em = ERR_PTR(PTR_ERR(rb_node));
315 goto out;
316 }
317 em = rb_entry(rb_node, struct extent_map, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500318 if (end > em->start && start < extent_map_end(em))
319 goto found;
320
321 em = NULL;
322 goto out;
323
Chris Mason5f564062008-01-22 16:47:59 -0500324found:
Chris Masona52d9a82007-08-27 16:49:44 -0400325 atomic_inc(&em->refs);
326out:
Chris Masona52d9a82007-08-27 16:49:44 -0400327 return em;
328}
329EXPORT_SYMBOL(lookup_extent_mapping);
330
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400331/**
332 * remove_extent_mapping - removes an extent_map from the extent tree
333 * @tree: extent tree to remove from
334 * @em: extent map beeing removed
335 *
336 * Removes @em from @tree. No reference counts are dropped, and no checks
337 * are done to see if the range is in use
Chris Masona52d9a82007-08-27 16:49:44 -0400338 */
339int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
340{
Chris Masond1310b22008-01-24 16:13:08 -0500341 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400342
Chris Mason7f3c74f2008-07-18 12:01:11 -0400343 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
David Woodhouse64f26f72008-07-24 10:09:43 -0400344 assert_spin_locked(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500345 rb_erase(&em->rb_node, &tree->map);
346 em->in_tree = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400347 return ret;
348}
349EXPORT_SYMBOL(remove_extent_mapping);