blob: 74b2a29880d36a3db2e40efcf45b8784319998ee [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 spin_lock_init(&tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -040046}
47EXPORT_SYMBOL(extent_map_tree_init);
48
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040049/**
50 * alloc_extent_map - allocate new extent map structure
51 * @mask: memory allocation flags
52 *
53 * Allocate a new extent_map structure. The new structure is
54 * returned with a reference count of one and needs to be
55 * freed using free_extent_map()
56 */
Chris Masona52d9a82007-08-27 16:49:44 -040057struct extent_map *alloc_extent_map(gfp_t mask)
58{
59 struct extent_map *em;
60 em = kmem_cache_alloc(extent_map_cache, mask);
61 if (!em || IS_ERR(em))
62 return em;
63 em->in_tree = 0;
Chris Masond1310b22008-01-24 16:13:08 -050064 em->flags = 0;
Chris Masona52d9a82007-08-27 16:49:44 -040065 atomic_set(&em->refs, 1);
66 return em;
67}
68EXPORT_SYMBOL(alloc_extent_map);
69
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040070/**
71 * free_extent_map - drop reference count of an extent_map
72 * @em: extent map beeing releasead
73 *
74 * Drops the reference out on @em by one and free the structure
75 * if the reference count hits zero.
76 */
Chris Masona52d9a82007-08-27 16:49:44 -040077void free_extent_map(struct extent_map *em)
78{
Chris Mason2bf5a722007-08-30 11:54:02 -040079 if (!em)
80 return;
Chris Masond1310b22008-01-24 16:13:08 -050081 WARN_ON(atomic_read(&em->refs) == 0);
Chris Masona52d9a82007-08-27 16:49:44 -040082 if (atomic_dec_and_test(&em->refs)) {
83 WARN_ON(em->in_tree);
84 kmem_cache_free(extent_map_cache, em);
85 }
86}
87EXPORT_SYMBOL(free_extent_map);
88
Chris Masona52d9a82007-08-27 16:49:44 -040089static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
90 struct rb_node *node)
91{
92 struct rb_node ** p = &root->rb_node;
93 struct rb_node * parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -050094 struct extent_map *entry;
Chris Masona52d9a82007-08-27 16:49:44 -040095
96 while(*p) {
97 parent = *p;
Chris Masond1310b22008-01-24 16:13:08 -050098 entry = rb_entry(parent, struct extent_map, rb_node);
99
100 WARN_ON(!entry->in_tree);
Chris Masona52d9a82007-08-27 16:49:44 -0400101
102 if (offset < entry->start)
103 p = &(*p)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500104 else if (offset >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -0400105 p = &(*p)->rb_right;
106 else
107 return parent;
108 }
109
Chris Masond1310b22008-01-24 16:13:08 -0500110 entry = rb_entry(node, struct extent_map, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400111 entry->in_tree = 1;
112 rb_link_node(node, parent, p);
113 rb_insert_color(node, root);
114 return NULL;
115}
116
Chris Masond352ac62008-09-29 15:18:18 -0400117/*
118 * search through the tree for an extent_map with a given offset. If
119 * it can't be found, try to find some neighboring extents
120 */
Chris Masona52d9a82007-08-27 16:49:44 -0400121static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
Chris Mason5f564062008-01-22 16:47:59 -0500122 struct rb_node **prev_ret,
123 struct rb_node **next_ret)
Chris Masona52d9a82007-08-27 16:49:44 -0400124{
125 struct rb_node * n = root->rb_node;
126 struct rb_node *prev = NULL;
Chris Mason5f564062008-01-22 16:47:59 -0500127 struct rb_node *orig_prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500128 struct extent_map *entry;
129 struct extent_map *prev_entry = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400130
131 while(n) {
Chris Masond1310b22008-01-24 16:13:08 -0500132 entry = rb_entry(n, struct extent_map, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400133 prev = n;
134 prev_entry = entry;
135
Chris Masond1310b22008-01-24 16:13:08 -0500136 WARN_ON(!entry->in_tree);
137
Chris Masona52d9a82007-08-27 16:49:44 -0400138 if (offset < entry->start)
139 n = n->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500140 else if (offset >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -0400141 n = n->rb_right;
142 else
143 return n;
144 }
Chris Mason5f564062008-01-22 16:47:59 -0500145
146 if (prev_ret) {
147 orig_prev = prev;
Chris Masond1310b22008-01-24 16:13:08 -0500148 while(prev && offset >= extent_map_end(prev_entry)) {
Chris Mason5f564062008-01-22 16:47:59 -0500149 prev = rb_next(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500150 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500151 }
152 *prev_ret = prev;
153 prev = orig_prev;
Chris Masona52d9a82007-08-27 16:49:44 -0400154 }
Chris Mason5f564062008-01-22 16:47:59 -0500155
156 if (next_ret) {
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 while(prev && offset < prev_entry->start) {
159 prev = rb_prev(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500160 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500161 }
162 *next_ret = prev;
163 }
Chris Masona52d9a82007-08-27 16:49:44 -0400164 return NULL;
165}
166
Chris Masond352ac62008-09-29 15:18:18 -0400167/*
168 * look for an offset in the tree, and if it can't be found, return
169 * the first offset we can find smaller than 'offset'.
170 */
Chris Masona52d9a82007-08-27 16:49:44 -0400171static inline struct rb_node *tree_search(struct rb_root *root, u64 offset)
172{
173 struct rb_node *prev;
174 struct rb_node *ret;
Chris Mason5f564062008-01-22 16:47:59 -0500175 ret = __tree_search(root, offset, &prev, NULL);
Chris Masona52d9a82007-08-27 16:49:44 -0400176 if (!ret)
177 return prev;
178 return ret;
179}
180
Chris Masond352ac62008-09-29 15:18:18 -0400181/* check to see if two extent_map structs are adjacent and safe to merge */
Chris Masond1310b22008-01-24 16:13:08 -0500182static int mergable_maps(struct extent_map *prev, struct extent_map *next)
Chris Masona52d9a82007-08-27 16:49:44 -0400183{
Chris Mason7f3c74f2008-07-18 12:01:11 -0400184 if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
185 return 0;
186
Chris Masond1310b22008-01-24 16:13:08 -0500187 if (extent_map_end(prev) == next->start &&
188 prev->flags == next->flags &&
189 prev->bdev == next->bdev &&
190 ((next->block_start == EXTENT_MAP_HOLE &&
191 prev->block_start == EXTENT_MAP_HOLE) ||
192 (next->block_start == EXTENT_MAP_INLINE &&
193 prev->block_start == EXTENT_MAP_INLINE) ||
194 (next->block_start == EXTENT_MAP_DELALLOC &&
195 prev->block_start == EXTENT_MAP_DELALLOC) ||
196 (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
197 next->block_start == extent_map_block_end(prev)))) {
198 return 1;
199 }
Chris Masona52d9a82007-08-27 16:49:44 -0400200 return 0;
201}
202
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400203/**
204 * add_extent_mapping - add new extent map to the extent tree
205 * @tree: tree to insert new map in
206 * @em: map to insert
207 *
208 * Insert @em into @tree or perform a simple forward/backward merge with
209 * existing mappings. The extent_map struct passed in will be inserted
210 * into the tree directly, with an additional reference taken, or a
211 * reference dropped if the merge attempt was sucessfull.
Chris Masona52d9a82007-08-27 16:49:44 -0400212 */
213int add_extent_mapping(struct extent_map_tree *tree,
214 struct extent_map *em)
215{
216 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500217 struct extent_map *merge = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400218 struct rb_node *rb;
Chris Mason7c2fe322008-08-20 08:51:50 -0400219 struct extent_map *exist;
Chris Masona52d9a82007-08-27 16:49:44 -0400220
Chris Mason7c2fe322008-08-20 08:51:50 -0400221 exist = lookup_extent_mapping(tree, em->start, em->len);
222 if (exist) {
223 free_extent_map(exist);
224 ret = -EEXIST;
225 goto out;
226 }
David Woodhouse64f26f72008-07-24 10:09:43 -0400227 assert_spin_locked(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500228 rb = tree_insert(&tree->map, em->start, &em->rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400229 if (rb) {
Chris Masona52d9a82007-08-27 16:49:44 -0400230 ret = -EEXIST;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400231 free_extent_map(merge);
Chris Masona52d9a82007-08-27 16:49:44 -0400232 goto out;
233 }
234 atomic_inc(&em->refs);
235 if (em->start != 0) {
236 rb = rb_prev(&em->rb_node);
237 if (rb)
Chris Masond1310b22008-01-24 16:13:08 -0500238 merge = rb_entry(rb, struct extent_map, rb_node);
239 if (rb && mergable_maps(merge, em)) {
240 em->start = merge->start;
241 em->len += merge->len;
242 em->block_start = merge->block_start;
243 merge->in_tree = 0;
244 rb_erase(&merge->rb_node, &tree->map);
245 free_extent_map(merge);
Chris Masona52d9a82007-08-27 16:49:44 -0400246 }
247 }
Chris Masond1310b22008-01-24 16:13:08 -0500248 rb = rb_next(&em->rb_node);
249 if (rb)
250 merge = rb_entry(rb, struct extent_map, rb_node);
251 if (rb && mergable_maps(em, merge)) {
252 em->len += merge->len;
253 rb_erase(&merge->rb_node, &tree->map);
254 merge->in_tree = 0;
255 free_extent_map(merge);
256 }
Chris Masona52d9a82007-08-27 16:49:44 -0400257out:
Chris Masona52d9a82007-08-27 16:49:44 -0400258 return ret;
259}
260EXPORT_SYMBOL(add_extent_mapping);
261
Chris Masond352ac62008-09-29 15:18:18 -0400262/* simple helper to do math around the end of an extent, handling wrap */
Chris Masond1310b22008-01-24 16:13:08 -0500263static u64 range_end(u64 start, u64 len)
264{
265 if (start + len < start)
266 return (u64)-1;
267 return start + len;
268}
269
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400270/**
271 * lookup_extent_mapping - lookup extent_map
272 * @tree: tree to lookup in
273 * @start: byte offset to start the search
274 * @len: length of the lookup range
275 *
276 * Find and return the first extent_map struct in @tree that intersects the
277 * [start, len] range. There may be additional objects in the tree that
278 * intersect, so check the object returned carefully to make sure that no
279 * additional lookups are needed.
Chris Masona52d9a82007-08-27 16:49:44 -0400280 */
281struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500282 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -0400283{
284 struct extent_map *em;
285 struct rb_node *rb_node;
Christoph Hellwig306929f2008-06-10 10:21:04 -0400286 struct rb_node *prev = NULL;
287 struct rb_node *next = NULL;
288 u64 end = range_end(start, len);
289
David Woodhouse64f26f72008-07-24 10:09:43 -0400290 assert_spin_locked(&tree->lock);
Chris Mason5f564062008-01-22 16:47:59 -0500291 rb_node = __tree_search(&tree->map, start, &prev, &next);
292 if (!rb_node && prev) {
293 em = rb_entry(prev, struct extent_map, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500294 if (end > em->start && start < extent_map_end(em))
Chris Mason5f564062008-01-22 16:47:59 -0500295 goto found;
296 }
297 if (!rb_node && next) {
298 em = rb_entry(next, struct extent_map, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500299 if (end > em->start && start < extent_map_end(em))
Chris Mason5f564062008-01-22 16:47:59 -0500300 goto found;
301 }
Chris Masona52d9a82007-08-27 16:49:44 -0400302 if (!rb_node) {
303 em = NULL;
304 goto out;
305 }
306 if (IS_ERR(rb_node)) {
307 em = ERR_PTR(PTR_ERR(rb_node));
308 goto out;
309 }
310 em = rb_entry(rb_node, struct extent_map, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500311 if (end > em->start && start < extent_map_end(em))
312 goto found;
313
314 em = NULL;
315 goto out;
316
Chris Mason5f564062008-01-22 16:47:59 -0500317found:
Chris Masona52d9a82007-08-27 16:49:44 -0400318 atomic_inc(&em->refs);
319out:
Chris Masona52d9a82007-08-27 16:49:44 -0400320 return em;
321}
322EXPORT_SYMBOL(lookup_extent_mapping);
323
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400324/**
325 * remove_extent_mapping - removes an extent_map from the extent tree
326 * @tree: extent tree to remove from
327 * @em: extent map beeing removed
328 *
329 * Removes @em from @tree. No reference counts are dropped, and no checks
330 * are done to see if the range is in use
Chris Masona52d9a82007-08-27 16:49:44 -0400331 */
332int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
333{
Chris Masond1310b22008-01-24 16:13:08 -0500334 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400335
Chris Mason7f3c74f2008-07-18 12:01:11 -0400336 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
David Woodhouse64f26f72008-07-24 10:09:43 -0400337 assert_spin_locked(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500338 rb_erase(&em->rb_node, &tree->map);
339 em->in_tree = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400340 return ret;
341}
342EXPORT_SYMBOL(remove_extent_mapping);