blob: 46bea0f4dc7bdd4e5f0193ec852aa5354d9d87d7 [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
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
31 * @mask: flags for memory allocations during tree operations
32 *
33 * Initialize the extent tree @tree. Should be called for each new inode
34 * or other user of the extent_map interface.
35 */
Chris Masond1310b22008-01-24 16:13:08 -050036void extent_map_tree_init(struct extent_map_tree *tree, gfp_t mask)
Chris Masona52d9a82007-08-27 16:49:44 -040037{
38 tree->map.rb_node = NULL;
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
44 * @mask: memory allocation flags
45 *
46 * Allocate a new extent_map structure. The new structure is
47 * returned with a reference count of one and needs to be
48 * freed using free_extent_map()
49 */
Chris Masona52d9a82007-08-27 16:49:44 -040050struct extent_map *alloc_extent_map(gfp_t mask)
51{
52 struct extent_map *em;
53 em = kmem_cache_alloc(extent_map_cache, mask);
54 if (!em || IS_ERR(em))
55 return em;
56 em->in_tree = 0;
Chris Masond1310b22008-01-24 16:13:08 -050057 em->flags = 0;
Chris Masona52d9a82007-08-27 16:49:44 -040058 atomic_set(&em->refs, 1);
59 return em;
60}
Chris Masona52d9a82007-08-27 16:49:44 -040061
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040062/**
63 * free_extent_map - drop reference count of an extent_map
64 * @em: extent map beeing releasead
65 *
66 * Drops the reference out on @em by one and free the structure
67 * if the reference count hits zero.
68 */
Chris Masona52d9a82007-08-27 16:49:44 -040069void free_extent_map(struct extent_map *em)
70{
Chris Mason2bf5a722007-08-30 11:54:02 -040071 if (!em)
72 return;
Chris Masond1310b22008-01-24 16:13:08 -050073 WARN_ON(atomic_read(&em->refs) == 0);
Chris Masona52d9a82007-08-27 16:49:44 -040074 if (atomic_dec_and_test(&em->refs)) {
75 WARN_ON(em->in_tree);
76 kmem_cache_free(extent_map_cache, em);
77 }
78}
Chris Masona52d9a82007-08-27 16:49:44 -040079
Chris Masona52d9a82007-08-27 16:49:44 -040080static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
81 struct rb_node *node)
82{
Chris Masond3977122009-01-05 21:25:51 -050083 struct rb_node **p = &root->rb_node;
84 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -050085 struct extent_map *entry;
Chris Masona52d9a82007-08-27 16:49:44 -040086
Chris Masond3977122009-01-05 21:25:51 -050087 while (*p) {
Chris Masona52d9a82007-08-27 16:49:44 -040088 parent = *p;
Chris Masond1310b22008-01-24 16:13:08 -050089 entry = rb_entry(parent, struct extent_map, rb_node);
90
91 WARN_ON(!entry->in_tree);
Chris Masona52d9a82007-08-27 16:49:44 -040092
93 if (offset < entry->start)
94 p = &(*p)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -050095 else if (offset >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -040096 p = &(*p)->rb_right;
97 else
98 return parent;
99 }
100
Chris Masond1310b22008-01-24 16:13:08 -0500101 entry = rb_entry(node, struct extent_map, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400102 entry->in_tree = 1;
103 rb_link_node(node, parent, p);
104 rb_insert_color(node, root);
105 return NULL;
106}
107
Chris Masond352ac62008-09-29 15:18:18 -0400108/*
109 * search through the tree for an extent_map with a given offset. If
110 * it can't be found, try to find some neighboring extents
111 */
Chris Masona52d9a82007-08-27 16:49:44 -0400112static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
Chris Mason5f564062008-01-22 16:47:59 -0500113 struct rb_node **prev_ret,
114 struct rb_node **next_ret)
Chris Masona52d9a82007-08-27 16:49:44 -0400115{
Chris Masond3977122009-01-05 21:25:51 -0500116 struct rb_node *n = root->rb_node;
Chris Masona52d9a82007-08-27 16:49:44 -0400117 struct rb_node *prev = NULL;
Chris Mason5f564062008-01-22 16:47:59 -0500118 struct rb_node *orig_prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500119 struct extent_map *entry;
120 struct extent_map *prev_entry = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400121
Chris Masond3977122009-01-05 21:25:51 -0500122 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500123 entry = rb_entry(n, struct extent_map, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400124 prev = n;
125 prev_entry = entry;
126
Chris Masond1310b22008-01-24 16:13:08 -0500127 WARN_ON(!entry->in_tree);
128
Chris Masona52d9a82007-08-27 16:49:44 -0400129 if (offset < entry->start)
130 n = n->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500131 else if (offset >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -0400132 n = n->rb_right;
133 else
134 return n;
135 }
Chris Mason5f564062008-01-22 16:47:59 -0500136
137 if (prev_ret) {
138 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500139 while (prev && offset >= extent_map_end(prev_entry)) {
Chris Mason5f564062008-01-22 16:47:59 -0500140 prev = rb_next(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500141 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500142 }
143 *prev_ret = prev;
144 prev = orig_prev;
Chris Masona52d9a82007-08-27 16:49:44 -0400145 }
Chris Mason5f564062008-01-22 16:47:59 -0500146
147 if (next_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500148 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500149 while (prev && offset < prev_entry->start) {
Chris Mason5f564062008-01-22 16:47:59 -0500150 prev = rb_prev(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500151 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500152 }
153 *next_ret = prev;
154 }
Chris Masona52d9a82007-08-27 16:49:44 -0400155 return NULL;
156}
157
Chris Masond352ac62008-09-29 15:18:18 -0400158/*
159 * look for an offset in the tree, and if it can't be found, return
160 * the first offset we can find smaller than 'offset'.
161 */
Chris Masona52d9a82007-08-27 16:49:44 -0400162static inline struct rb_node *tree_search(struct rb_root *root, u64 offset)
163{
164 struct rb_node *prev;
165 struct rb_node *ret;
Chris Mason5f564062008-01-22 16:47:59 -0500166 ret = __tree_search(root, offset, &prev, NULL);
Chris Masona52d9a82007-08-27 16:49:44 -0400167 if (!ret)
168 return prev;
169 return ret;
170}
171
Chris Masond352ac62008-09-29 15:18:18 -0400172/* check to see if two extent_map structs are adjacent and safe to merge */
Chris Masond1310b22008-01-24 16:13:08 -0500173static int mergable_maps(struct extent_map *prev, struct extent_map *next)
Chris Masona52d9a82007-08-27 16:49:44 -0400174{
Chris Mason7f3c74f2008-07-18 12:01:11 -0400175 if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
176 return 0;
177
Chris Masonc8b97812008-10-29 14:49:59 -0400178 /*
179 * don't merge compressed extents, we need to know their
180 * actual size
181 */
182 if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
183 return 0;
184
Chris Masond1310b22008-01-24 16:13:08 -0500185 if (extent_map_end(prev) == next->start &&
186 prev->flags == next->flags &&
187 prev->bdev == next->bdev &&
188 ((next->block_start == EXTENT_MAP_HOLE &&
189 prev->block_start == EXTENT_MAP_HOLE) ||
190 (next->block_start == EXTENT_MAP_INLINE &&
191 prev->block_start == EXTENT_MAP_INLINE) ||
192 (next->block_start == EXTENT_MAP_DELALLOC &&
193 prev->block_start == EXTENT_MAP_DELALLOC) ||
194 (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
195 next->block_start == extent_map_block_end(prev)))) {
196 return 1;
197 }
Chris Masona52d9a82007-08-27 16:49:44 -0400198 return 0;
199}
200
Chris Masona1ed8352009-09-11 12:27:37 -0400201int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len)
202{
203 int ret = 0;
204 struct extent_map *merge = NULL;
205 struct rb_node *rb;
206 struct extent_map *em;
207
208 write_lock(&tree->lock);
209 em = lookup_extent_mapping(tree, start, len);
210
Dan Carpenter4eb39912009-11-10 09:01:43 +0000211 WARN_ON(!em || em->start != start);
Chris Masona1ed8352009-09-11 12:27:37 -0400212
213 if (!em)
214 goto out;
215
216 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
217
218 if (em->start != 0) {
219 rb = rb_prev(&em->rb_node);
220 if (rb)
221 merge = rb_entry(rb, struct extent_map, rb_node);
222 if (rb && mergable_maps(merge, em)) {
223 em->start = merge->start;
224 em->len += merge->len;
225 em->block_len += merge->block_len;
226 em->block_start = merge->block_start;
227 merge->in_tree = 0;
228 rb_erase(&merge->rb_node, &tree->map);
229 free_extent_map(merge);
230 }
231 }
232
233 rb = rb_next(&em->rb_node);
234 if (rb)
235 merge = rb_entry(rb, struct extent_map, rb_node);
236 if (rb && mergable_maps(em, merge)) {
237 em->len += merge->len;
238 em->block_len += merge->len;
239 rb_erase(&merge->rb_node, &tree->map);
240 merge->in_tree = 0;
241 free_extent_map(merge);
242 }
243
244 free_extent_map(em);
245out:
246 write_unlock(&tree->lock);
247 return ret;
248
249}
250
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400251/**
252 * add_extent_mapping - add new extent map to the extent tree
253 * @tree: tree to insert new map in
254 * @em: map to insert
255 *
256 * Insert @em into @tree or perform a simple forward/backward merge with
257 * existing mappings. The extent_map struct passed in will be inserted
258 * into the tree directly, with an additional reference taken, or a
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200259 * reference dropped if the merge attempt was successfull.
Chris Masona52d9a82007-08-27 16:49:44 -0400260 */
261int add_extent_mapping(struct extent_map_tree *tree,
262 struct extent_map *em)
263{
264 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500265 struct extent_map *merge = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400266 struct rb_node *rb;
Chris Mason7c2fe322008-08-20 08:51:50 -0400267 struct extent_map *exist;
Chris Masona52d9a82007-08-27 16:49:44 -0400268
Chris Mason7c2fe322008-08-20 08:51:50 -0400269 exist = lookup_extent_mapping(tree, em->start, em->len);
270 if (exist) {
271 free_extent_map(exist);
272 ret = -EEXIST;
273 goto out;
274 }
Chris Masond1310b22008-01-24 16:13:08 -0500275 rb = tree_insert(&tree->map, em->start, &em->rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400276 if (rb) {
Chris Masona52d9a82007-08-27 16:49:44 -0400277 ret = -EEXIST;
278 goto out;
279 }
280 atomic_inc(&em->refs);
281 if (em->start != 0) {
282 rb = rb_prev(&em->rb_node);
283 if (rb)
Chris Masond1310b22008-01-24 16:13:08 -0500284 merge = rb_entry(rb, struct extent_map, rb_node);
285 if (rb && mergable_maps(merge, em)) {
286 em->start = merge->start;
287 em->len += merge->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400288 em->block_len += merge->block_len;
Chris Masond1310b22008-01-24 16:13:08 -0500289 em->block_start = merge->block_start;
290 merge->in_tree = 0;
291 rb_erase(&merge->rb_node, &tree->map);
292 free_extent_map(merge);
Chris Masona52d9a82007-08-27 16:49:44 -0400293 }
294 }
Chris Masond1310b22008-01-24 16:13:08 -0500295 rb = rb_next(&em->rb_node);
296 if (rb)
297 merge = rb_entry(rb, struct extent_map, rb_node);
298 if (rb && mergable_maps(em, merge)) {
299 em->len += merge->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400300 em->block_len += merge->len;
Chris Masond1310b22008-01-24 16:13:08 -0500301 rb_erase(&merge->rb_node, &tree->map);
302 merge->in_tree = 0;
303 free_extent_map(merge);
304 }
Chris Masona52d9a82007-08-27 16:49:44 -0400305out:
Chris Masona52d9a82007-08-27 16:49:44 -0400306 return ret;
307}
Chris Masona52d9a82007-08-27 16:49:44 -0400308
Chris Masond352ac62008-09-29 15:18:18 -0400309/* simple helper to do math around the end of an extent, handling wrap */
Chris Masond1310b22008-01-24 16:13:08 -0500310static u64 range_end(u64 start, u64 len)
311{
312 if (start + len < start)
313 return (u64)-1;
314 return start + len;
315}
316
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400317/**
318 * lookup_extent_mapping - lookup extent_map
319 * @tree: tree to lookup in
320 * @start: byte offset to start the search
321 * @len: length of the lookup range
322 *
323 * Find and return the first extent_map struct in @tree that intersects the
324 * [start, len] range. There may be additional objects in the tree that
325 * intersect, so check the object returned carefully to make sure that no
326 * additional lookups are needed.
Chris Masona52d9a82007-08-27 16:49:44 -0400327 */
328struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500329 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -0400330{
331 struct extent_map *em;
332 struct rb_node *rb_node;
Christoph Hellwig306929f2008-06-10 10:21:04 -0400333 struct rb_node *prev = NULL;
334 struct rb_node *next = NULL;
335 u64 end = range_end(start, len);
336
Chris Mason5f564062008-01-22 16:47:59 -0500337 rb_node = __tree_search(&tree->map, start, &prev, &next);
338 if (!rb_node && prev) {
339 em = rb_entry(prev, struct extent_map, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500340 if (end > em->start && start < extent_map_end(em))
Chris Mason5f564062008-01-22 16:47:59 -0500341 goto found;
342 }
343 if (!rb_node && next) {
344 em = rb_entry(next, struct extent_map, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500345 if (end > em->start && start < extent_map_end(em))
Chris Mason5f564062008-01-22 16:47:59 -0500346 goto found;
347 }
Chris Masona52d9a82007-08-27 16:49:44 -0400348 if (!rb_node) {
349 em = NULL;
350 goto out;
351 }
352 if (IS_ERR(rb_node)) {
353 em = ERR_PTR(PTR_ERR(rb_node));
354 goto out;
355 }
356 em = rb_entry(rb_node, struct extent_map, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500357 if (end > em->start && start < extent_map_end(em))
358 goto found;
359
360 em = NULL;
361 goto out;
362
Chris Mason5f564062008-01-22 16:47:59 -0500363found:
Chris Masona52d9a82007-08-27 16:49:44 -0400364 atomic_inc(&em->refs);
365out:
Chris Masona52d9a82007-08-27 16:49:44 -0400366 return em;
367}
Chris Masona52d9a82007-08-27 16:49:44 -0400368
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400369/**
Chris Masonb917b7c2009-09-18 16:07:03 -0400370 * search_extent_mapping - find a nearby extent map
371 * @tree: tree to lookup in
372 * @start: byte offset to start the search
373 * @len: length of the lookup range
374 *
375 * Find and return the first extent_map struct in @tree that intersects the
376 * [start, len] range.
377 *
378 * If one can't be found, any nearby extent may be returned
379 */
380struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
381 u64 start, u64 len)
382{
383 struct extent_map *em;
384 struct rb_node *rb_node;
385 struct rb_node *prev = NULL;
386 struct rb_node *next = NULL;
387
388 rb_node = __tree_search(&tree->map, start, &prev, &next);
389 if (!rb_node && prev) {
390 em = rb_entry(prev, struct extent_map, rb_node);
391 goto found;
392 }
393 if (!rb_node && next) {
394 em = rb_entry(next, struct extent_map, rb_node);
395 goto found;
396 }
397 if (!rb_node) {
398 em = NULL;
399 goto out;
400 }
401 if (IS_ERR(rb_node)) {
402 em = ERR_PTR(PTR_ERR(rb_node));
403 goto out;
404 }
405 em = rb_entry(rb_node, struct extent_map, rb_node);
406 goto found;
407
408 em = NULL;
409 goto out;
410
411found:
412 atomic_inc(&em->refs);
413out:
414 return em;
415}
416
417/**
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400418 * remove_extent_mapping - removes an extent_map from the extent tree
419 * @tree: extent tree to remove from
420 * @em: extent map beeing removed
421 *
422 * Removes @em from @tree. No reference counts are dropped, and no checks
423 * are done to see if the range is in use
Chris Masona52d9a82007-08-27 16:49:44 -0400424 */
425int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
426{
Chris Masond1310b22008-01-24 16:13:08 -0500427 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400428
Chris Mason7f3c74f2008-07-18 12:01:11 -0400429 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
Chris Masond1310b22008-01-24 16:13:08 -0500430 rb_erase(&em->rb_node, &tree->map);
431 em->in_tree = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400432 return ret;
433}