blob: 81123277c2b8f7854d2e3ba9c2d03440b957a507 [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>
Jens Axboe0a2118d2007-10-19 09:23:05 -04006#include <linux/version.h>
Chris Masond1310b22008-01-24 16:13:08 -05007#include <linux/hardirq.h>
Chris Masona52d9a82007-08-27 16:49:44 -04008#include "extent_map.h"
9
Chris Mason86479a02007-09-10 19:58:16 -040010/* temporary define until extent_map moves out of btrfs */
11struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
12 unsigned long extra_flags,
13 void (*ctor)(void *, struct kmem_cache *,
14 unsigned long));
15
Chris Masona52d9a82007-08-27 16:49:44 -040016static struct kmem_cache *extent_map_cache;
Chris Masonca664622007-11-27 11:16:35 -050017
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050018int __init extent_map_init(void)
Chris Masona52d9a82007-08-27 16:49:44 -040019{
Chris Mason86479a02007-09-10 19:58:16 -040020 extent_map_cache = btrfs_cache_create("extent_map",
Chris Mason6d36dcd2007-10-15 16:14:37 -040021 sizeof(struct extent_map), 0,
Chris Masona52d9a82007-08-27 16:49:44 -040022 NULL);
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050023 if (!extent_map_cache)
24 return -ENOMEM;
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050025 return 0;
Chris Masona52d9a82007-08-27 16:49:44 -040026}
27
Christian Hesse17636e02007-12-11 09:25:06 -050028void extent_map_exit(void)
Chris Masona52d9a82007-08-27 16:49:44 -040029{
Chris Masona52d9a82007-08-27 16:49:44 -040030 if (extent_map_cache)
31 kmem_cache_destroy(extent_map_cache);
Chris Masona52d9a82007-08-27 16:49:44 -040032}
33
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040034/**
35 * extent_map_tree_init - initialize extent map tree
36 * @tree: tree to initialize
37 * @mask: flags for memory allocations during tree operations
38 *
39 * Initialize the extent tree @tree. Should be called for each new inode
40 * or other user of the extent_map interface.
41 */
Chris Masond1310b22008-01-24 16:13:08 -050042void extent_map_tree_init(struct extent_map_tree *tree, gfp_t mask)
Chris Masona52d9a82007-08-27 16:49:44 -040043{
44 tree->map.rb_node = NULL;
Chris Masond1310b22008-01-24 16:13:08 -050045 tree->last = NULL;
46 spin_lock_init(&tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -040047}
48EXPORT_SYMBOL(extent_map_tree_init);
49
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040050/**
51 * alloc_extent_map - allocate new extent map structure
52 * @mask: memory allocation flags
53 *
54 * Allocate a new extent_map structure. The new structure is
55 * returned with a reference count of one and needs to be
56 * freed using free_extent_map()
57 */
Chris Masona52d9a82007-08-27 16:49:44 -040058struct extent_map *alloc_extent_map(gfp_t mask)
59{
60 struct extent_map *em;
61 em = kmem_cache_alloc(extent_map_cache, mask);
62 if (!em || IS_ERR(em))
63 return em;
64 em->in_tree = 0;
Chris Masond1310b22008-01-24 16:13:08 -050065 em->flags = 0;
Chris Masona52d9a82007-08-27 16:49:44 -040066 atomic_set(&em->refs, 1);
67 return em;
68}
69EXPORT_SYMBOL(alloc_extent_map);
70
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040071/**
72 * free_extent_map - drop reference count of an extent_map
73 * @em: extent map beeing releasead
74 *
75 * Drops the reference out on @em by one and free the structure
76 * if the reference count hits zero.
77 */
Chris Masona52d9a82007-08-27 16:49:44 -040078void free_extent_map(struct extent_map *em)
79{
Chris Mason2bf5a722007-08-30 11:54:02 -040080 if (!em)
81 return;
Chris Masond1310b22008-01-24 16:13:08 -050082 WARN_ON(atomic_read(&em->refs) == 0);
Chris Masona52d9a82007-08-27 16:49:44 -040083 if (atomic_dec_and_test(&em->refs)) {
84 WARN_ON(em->in_tree);
85 kmem_cache_free(extent_map_cache, em);
86 }
87}
88EXPORT_SYMBOL(free_extent_map);
89
Chris Masona52d9a82007-08-27 16:49:44 -040090static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
91 struct rb_node *node)
92{
93 struct rb_node ** p = &root->rb_node;
94 struct rb_node * parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -050095 struct extent_map *entry;
Chris Masona52d9a82007-08-27 16:49:44 -040096
97 while(*p) {
98 parent = *p;
Chris Masond1310b22008-01-24 16:13:08 -050099 entry = rb_entry(parent, struct extent_map, rb_node);
100
101 WARN_ON(!entry->in_tree);
Chris Masona52d9a82007-08-27 16:49:44 -0400102
103 if (offset < entry->start)
104 p = &(*p)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500105 else if (offset >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -0400106 p = &(*p)->rb_right;
107 else
108 return parent;
109 }
110
Chris Masond1310b22008-01-24 16:13:08 -0500111 entry = rb_entry(node, struct extent_map, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400112 entry->in_tree = 1;
113 rb_link_node(node, parent, p);
114 rb_insert_color(node, root);
115 return NULL;
116}
117
118static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
Chris Mason5f564062008-01-22 16:47:59 -0500119 struct rb_node **prev_ret,
120 struct rb_node **next_ret)
Chris Masona52d9a82007-08-27 16:49:44 -0400121{
122 struct rb_node * n = root->rb_node;
123 struct rb_node *prev = NULL;
Chris Mason5f564062008-01-22 16:47:59 -0500124 struct rb_node *orig_prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500125 struct extent_map *entry;
126 struct extent_map *prev_entry = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400127
128 while(n) {
Chris Masond1310b22008-01-24 16:13:08 -0500129 entry = rb_entry(n, struct extent_map, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400130 prev = n;
131 prev_entry = entry;
132
Chris Masond1310b22008-01-24 16:13:08 -0500133 WARN_ON(!entry->in_tree);
134
Chris Masona52d9a82007-08-27 16:49:44 -0400135 if (offset < entry->start)
136 n = n->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500137 else if (offset >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -0400138 n = n->rb_right;
139 else
140 return n;
141 }
Chris Mason5f564062008-01-22 16:47:59 -0500142
143 if (prev_ret) {
144 orig_prev = prev;
Chris Masond1310b22008-01-24 16:13:08 -0500145 while(prev && offset >= extent_map_end(prev_entry)) {
Chris Mason5f564062008-01-22 16:47:59 -0500146 prev = rb_next(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500147 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500148 }
149 *prev_ret = prev;
150 prev = orig_prev;
Chris Masona52d9a82007-08-27 16:49:44 -0400151 }
Chris Mason5f564062008-01-22 16:47:59 -0500152
153 if (next_ret) {
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 while(prev && offset < prev_entry->start) {
156 prev = rb_prev(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500157 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500158 }
159 *next_ret = prev;
160 }
Chris Masona52d9a82007-08-27 16:49:44 -0400161 return NULL;
162}
163
164static inline struct rb_node *tree_search(struct rb_root *root, u64 offset)
165{
166 struct rb_node *prev;
167 struct rb_node *ret;
Chris Mason5f564062008-01-22 16:47:59 -0500168 ret = __tree_search(root, offset, &prev, NULL);
Chris Masona52d9a82007-08-27 16:49:44 -0400169 if (!ret)
170 return prev;
171 return ret;
172}
173
Chris Masond1310b22008-01-24 16:13:08 -0500174static int mergable_maps(struct extent_map *prev, struct extent_map *next)
Chris Masona52d9a82007-08-27 16:49:44 -0400175{
Chris Masond1310b22008-01-24 16:13:08 -0500176 if (extent_map_end(prev) == next->start &&
177 prev->flags == next->flags &&
178 prev->bdev == next->bdev &&
179 ((next->block_start == EXTENT_MAP_HOLE &&
180 prev->block_start == EXTENT_MAP_HOLE) ||
181 (next->block_start == EXTENT_MAP_INLINE &&
182 prev->block_start == EXTENT_MAP_INLINE) ||
183 (next->block_start == EXTENT_MAP_DELALLOC &&
184 prev->block_start == EXTENT_MAP_DELALLOC) ||
185 (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
186 next->block_start == extent_map_block_end(prev)))) {
187 return 1;
188 }
Chris Masona52d9a82007-08-27 16:49:44 -0400189 return 0;
190}
191
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400192/**
193 * add_extent_mapping - add new extent map to the extent tree
194 * @tree: tree to insert new map in
195 * @em: map to insert
196 *
197 * Insert @em into @tree or perform a simple forward/backward merge with
198 * existing mappings. The extent_map struct passed in will be inserted
199 * into the tree directly, with an additional reference taken, or a
200 * reference dropped if the merge attempt was sucessfull.
Chris Masona52d9a82007-08-27 16:49:44 -0400201 */
202int add_extent_mapping(struct extent_map_tree *tree,
203 struct extent_map *em)
204{
205 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500206 struct extent_map *merge = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400207 struct rb_node *rb;
208
Chris Masone6dcd2d2008-07-17 12:53:50 -0400209 BUG_ON(spin_trylock(&tree->lock));
Chris Masond1310b22008-01-24 16:13:08 -0500210 rb = tree_insert(&tree->map, em->start, &em->rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400211 if (rb) {
Chris Masona52d9a82007-08-27 16:49:44 -0400212 ret = -EEXIST;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400213 free_extent_map(merge);
Chris Masona52d9a82007-08-27 16:49:44 -0400214 goto out;
215 }
216 atomic_inc(&em->refs);
217 if (em->start != 0) {
218 rb = rb_prev(&em->rb_node);
219 if (rb)
Chris Masond1310b22008-01-24 16:13:08 -0500220 merge = rb_entry(rb, struct extent_map, rb_node);
221 if (rb && mergable_maps(merge, em)) {
222 em->start = merge->start;
223 em->len += merge->len;
224 em->block_start = merge->block_start;
225 merge->in_tree = 0;
226 rb_erase(&merge->rb_node, &tree->map);
227 free_extent_map(merge);
Chris Masona52d9a82007-08-27 16:49:44 -0400228 }
229 }
Chris Masond1310b22008-01-24 16:13:08 -0500230 rb = rb_next(&em->rb_node);
231 if (rb)
232 merge = rb_entry(rb, struct extent_map, rb_node);
233 if (rb && mergable_maps(em, merge)) {
234 em->len += merge->len;
235 rb_erase(&merge->rb_node, &tree->map);
236 merge->in_tree = 0;
237 free_extent_map(merge);
238 }
239 tree->last = em;
Chris Masona52d9a82007-08-27 16:49:44 -0400240out:
Chris Masona52d9a82007-08-27 16:49:44 -0400241 return ret;
242}
243EXPORT_SYMBOL(add_extent_mapping);
244
Chris Masond1310b22008-01-24 16:13:08 -0500245static u64 range_end(u64 start, u64 len)
246{
247 if (start + len < start)
248 return (u64)-1;
249 return start + len;
250}
251
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400252/**
253 * lookup_extent_mapping - lookup extent_map
254 * @tree: tree to lookup in
255 * @start: byte offset to start the search
256 * @len: length of the lookup range
257 *
258 * Find and return the first extent_map struct in @tree that intersects the
259 * [start, len] range. There may be additional objects in the tree that
260 * intersect, so check the object returned carefully to make sure that no
261 * additional lookups are needed.
Chris Masona52d9a82007-08-27 16:49:44 -0400262 */
263struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500264 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -0400265{
266 struct extent_map *em;
267 struct rb_node *rb_node;
Christoph Hellwig306929f2008-06-10 10:21:04 -0400268 struct rb_node *prev = NULL;
269 struct rb_node *next = NULL;
270 u64 end = range_end(start, len);
271
Chris Masone6dcd2d2008-07-17 12:53:50 -0400272 BUG_ON(spin_trylock(&tree->lock));
Christoph Hellwig306929f2008-06-10 10:21:04 -0400273 em = tree->last;
274 if (em && end > em->start && start < extent_map_end(em))
275 goto found;
Chris Masona52d9a82007-08-27 16:49:44 -0400276
Chris Mason5f564062008-01-22 16:47:59 -0500277 rb_node = __tree_search(&tree->map, start, &prev, &next);
278 if (!rb_node && prev) {
279 em = rb_entry(prev, struct extent_map, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500280 if (end > em->start && start < extent_map_end(em))
Chris Mason5f564062008-01-22 16:47:59 -0500281 goto found;
282 }
283 if (!rb_node && next) {
284 em = rb_entry(next, struct extent_map, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500285 if (end > em->start && start < extent_map_end(em))
Chris Mason5f564062008-01-22 16:47:59 -0500286 goto found;
287 }
Chris Masona52d9a82007-08-27 16:49:44 -0400288 if (!rb_node) {
289 em = NULL;
290 goto out;
291 }
292 if (IS_ERR(rb_node)) {
293 em = ERR_PTR(PTR_ERR(rb_node));
294 goto out;
295 }
296 em = rb_entry(rb_node, struct extent_map, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500297 if (end > em->start && start < extent_map_end(em))
298 goto found;
299
300 em = NULL;
301 goto out;
302
Chris Mason5f564062008-01-22 16:47:59 -0500303found:
Chris Masona52d9a82007-08-27 16:49:44 -0400304 atomic_inc(&em->refs);
Chris Masond1310b22008-01-24 16:13:08 -0500305 tree->last = em;
Chris Masona52d9a82007-08-27 16:49:44 -0400306out:
Chris Masona52d9a82007-08-27 16:49:44 -0400307 return em;
308}
309EXPORT_SYMBOL(lookup_extent_mapping);
310
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400311/**
312 * remove_extent_mapping - removes an extent_map from the extent tree
313 * @tree: extent tree to remove from
314 * @em: extent map beeing removed
315 *
316 * Removes @em from @tree. No reference counts are dropped, and no checks
317 * are done to see if the range is in use
Chris Masona52d9a82007-08-27 16:49:44 -0400318 */
319int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
320{
Chris Masond1310b22008-01-24 16:13:08 -0500321 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400322
Chris Masone6dcd2d2008-07-17 12:53:50 -0400323 BUG_ON(spin_trylock(&tree->lock));
Chris Masond1310b22008-01-24 16:13:08 -0500324 rb_erase(&em->rb_node, &tree->map);
325 em->in_tree = 0;
326 if (tree->last == em)
327 tree->last = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400328 return ret;
329}
330EXPORT_SYMBOL(remove_extent_mapping);