]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/btrfs/extent_map.c
Btrfs: btrfs_mark_extent_written uses the wrong slot
[net-next-2.6.git] / fs / btrfs / extent_map.c
CommitLineData
d1310b2e 1#include <linux/err.h>
a52d9a80 2#include <linux/gfp.h>
d1310b2e 3#include <linux/slab.h>
a52d9a80
CM
4#include <linux/module.h>
5#include <linux/spinlock.h>
d1310b2e 6#include <linux/hardirq.h>
a52d9a80
CM
7#include "extent_map.h"
8
86479a04 9
a52d9a80 10static struct kmem_cache *extent_map_cache;
ca664626 11
2f4cbe64 12int __init extent_map_init(void)
a52d9a80 13{
9601e3f6
CH
14 extent_map_cache = kmem_cache_create("extent_map",
15 sizeof(struct extent_map), 0,
16 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
2f4cbe64
WB
17 if (!extent_map_cache)
18 return -ENOMEM;
2f4cbe64 19 return 0;
a52d9a80
CM
20}
21
17636e03 22void extent_map_exit(void)
a52d9a80 23{
a52d9a80
CM
24 if (extent_map_cache)
25 kmem_cache_destroy(extent_map_cache);
a52d9a80
CM
26}
27
9d2423c5
CH
28/**
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 */
d1310b2e 36void extent_map_tree_init(struct extent_map_tree *tree, gfp_t mask)
a52d9a80
CM
37{
38 tree->map.rb_node = NULL;
890871be 39 rwlock_init(&tree->lock);
a52d9a80 40}
a52d9a80 41
9d2423c5
CH
42/**
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 */
a52d9a80
CM
50struct 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;
d1310b2e 57 em->flags = 0;
a52d9a80
CM
58 atomic_set(&em->refs, 1);
59 return em;
60}
a52d9a80 61
9d2423c5
CH
62/**
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 */
a52d9a80
CM
69void free_extent_map(struct extent_map *em)
70{
2bf5a725
CM
71 if (!em)
72 return;
d1310b2e 73 WARN_ON(atomic_read(&em->refs) == 0);
a52d9a80
CM
74 if (atomic_dec_and_test(&em->refs)) {
75 WARN_ON(em->in_tree);
76 kmem_cache_free(extent_map_cache, em);
77 }
78}
a52d9a80 79
a52d9a80
CM
80static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
81 struct rb_node *node)
82{
d397712b
CM
83 struct rb_node **p = &root->rb_node;
84 struct rb_node *parent = NULL;
d1310b2e 85 struct extent_map *entry;
a52d9a80 86
d397712b 87 while (*p) {
a52d9a80 88 parent = *p;
d1310b2e
CM
89 entry = rb_entry(parent, struct extent_map, rb_node);
90
91 WARN_ON(!entry->in_tree);
a52d9a80
CM
92
93 if (offset < entry->start)
94 p = &(*p)->rb_left;
d1310b2e 95 else if (offset >= extent_map_end(entry))
a52d9a80
CM
96 p = &(*p)->rb_right;
97 else
98 return parent;
99 }
100
d1310b2e 101 entry = rb_entry(node, struct extent_map, rb_node);
a52d9a80
CM
102 entry->in_tree = 1;
103 rb_link_node(node, parent, p);
104 rb_insert_color(node, root);
105 return NULL;
106}
107
d352ac68
CM
108/*
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 */
a52d9a80 112static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
5f56406a
CM
113 struct rb_node **prev_ret,
114 struct rb_node **next_ret)
a52d9a80 115{
d397712b 116 struct rb_node *n = root->rb_node;
a52d9a80 117 struct rb_node *prev = NULL;
5f56406a 118 struct rb_node *orig_prev = NULL;
d1310b2e
CM
119 struct extent_map *entry;
120 struct extent_map *prev_entry = NULL;
a52d9a80 121
d397712b 122 while (n) {
d1310b2e 123 entry = rb_entry(n, struct extent_map, rb_node);
a52d9a80
CM
124 prev = n;
125 prev_entry = entry;
126
d1310b2e
CM
127 WARN_ON(!entry->in_tree);
128
a52d9a80
CM
129 if (offset < entry->start)
130 n = n->rb_left;
d1310b2e 131 else if (offset >= extent_map_end(entry))
a52d9a80
CM
132 n = n->rb_right;
133 else
134 return n;
135 }
5f56406a
CM
136
137 if (prev_ret) {
138 orig_prev = prev;
d397712b 139 while (prev && offset >= extent_map_end(prev_entry)) {
5f56406a 140 prev = rb_next(prev);
d1310b2e 141 prev_entry = rb_entry(prev, struct extent_map, rb_node);
5f56406a
CM
142 }
143 *prev_ret = prev;
144 prev = orig_prev;
145 }
146
147 if (next_ret) {
d1310b2e 148 prev_entry = rb_entry(prev, struct extent_map, rb_node);
d397712b 149 while (prev && offset < prev_entry->start) {
5f56406a 150 prev = rb_prev(prev);
d1310b2e 151 prev_entry = rb_entry(prev, struct extent_map, rb_node);
5f56406a
CM
152 }
153 *next_ret = prev;
a52d9a80 154 }
a52d9a80
CM
155 return NULL;
156}
157
d352ac68 158/* check to see if two extent_map structs are adjacent and safe to merge */
d1310b2e 159static int mergable_maps(struct extent_map *prev, struct extent_map *next)
a52d9a80 160{
7f3c74fb
CM
161 if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
162 return 0;
163
c8b97818
CM
164 /*
165 * don't merge compressed extents, we need to know their
166 * actual size
167 */
168 if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
169 return 0;
170
d1310b2e
CM
171 if (extent_map_end(prev) == next->start &&
172 prev->flags == next->flags &&
173 prev->bdev == next->bdev &&
174 ((next->block_start == EXTENT_MAP_HOLE &&
175 prev->block_start == EXTENT_MAP_HOLE) ||
176 (next->block_start == EXTENT_MAP_INLINE &&
177 prev->block_start == EXTENT_MAP_INLINE) ||
178 (next->block_start == EXTENT_MAP_DELALLOC &&
179 prev->block_start == EXTENT_MAP_DELALLOC) ||
180 (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
181 next->block_start == extent_map_block_end(prev)))) {
182 return 1;
183 }
a52d9a80
CM
184 return 0;
185}
186
a1ed835e
CM
187int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len)
188{
189 int ret = 0;
190 struct extent_map *merge = NULL;
191 struct rb_node *rb;
192 struct extent_map *em;
193
194 write_lock(&tree->lock);
195 em = lookup_extent_mapping(tree, start, len);
196
4eb3991c 197 WARN_ON(!em || em->start != start);
a1ed835e
CM
198
199 if (!em)
200 goto out;
201
202 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
203
204 if (em->start != 0) {
205 rb = rb_prev(&em->rb_node);
206 if (rb)
207 merge = rb_entry(rb, struct extent_map, rb_node);
208 if (rb && mergable_maps(merge, em)) {
209 em->start = merge->start;
210 em->len += merge->len;
211 em->block_len += merge->block_len;
212 em->block_start = merge->block_start;
213 merge->in_tree = 0;
214 rb_erase(&merge->rb_node, &tree->map);
215 free_extent_map(merge);
216 }
217 }
218
219 rb = rb_next(&em->rb_node);
220 if (rb)
221 merge = rb_entry(rb, struct extent_map, rb_node);
222 if (rb && mergable_maps(em, merge)) {
223 em->len += merge->len;
224 em->block_len += merge->len;
225 rb_erase(&merge->rb_node, &tree->map);
226 merge->in_tree = 0;
227 free_extent_map(merge);
228 }
229
230 free_extent_map(em);
231out:
232 write_unlock(&tree->lock);
233 return ret;
234
235}
236
9d2423c5
CH
237/**
238 * add_extent_mapping - add new extent map to the extent tree
239 * @tree: tree to insert new map in
240 * @em: map to insert
241 *
242 * Insert @em into @tree or perform a simple forward/backward merge with
243 * existing mappings. The extent_map struct passed in will be inserted
244 * into the tree directly, with an additional reference taken, or a
245 * reference dropped if the merge attempt was sucessfull.
a52d9a80
CM
246 */
247int add_extent_mapping(struct extent_map_tree *tree,
248 struct extent_map *em)
249{
250 int ret = 0;
d1310b2e 251 struct extent_map *merge = NULL;
a52d9a80 252 struct rb_node *rb;
7c2fe32a 253 struct extent_map *exist;
a52d9a80 254
7c2fe32a
CM
255 exist = lookup_extent_mapping(tree, em->start, em->len);
256 if (exist) {
257 free_extent_map(exist);
258 ret = -EEXIST;
259 goto out;
260 }
d1310b2e 261 rb = tree_insert(&tree->map, em->start, &em->rb_node);
a52d9a80 262 if (rb) {
a52d9a80
CM
263 ret = -EEXIST;
264 goto out;
265 }
266 atomic_inc(&em->refs);
267 if (em->start != 0) {
268 rb = rb_prev(&em->rb_node);
269 if (rb)
d1310b2e
CM
270 merge = rb_entry(rb, struct extent_map, rb_node);
271 if (rb && mergable_maps(merge, em)) {
272 em->start = merge->start;
273 em->len += merge->len;
c8b97818 274 em->block_len += merge->block_len;
d1310b2e
CM
275 em->block_start = merge->block_start;
276 merge->in_tree = 0;
277 rb_erase(&merge->rb_node, &tree->map);
278 free_extent_map(merge);
a52d9a80
CM
279 }
280 }
d1310b2e
CM
281 rb = rb_next(&em->rb_node);
282 if (rb)
283 merge = rb_entry(rb, struct extent_map, rb_node);
284 if (rb && mergable_maps(em, merge)) {
285 em->len += merge->len;
c8b97818 286 em->block_len += merge->len;
d1310b2e
CM
287 rb_erase(&merge->rb_node, &tree->map);
288 merge->in_tree = 0;
289 free_extent_map(merge);
290 }
a52d9a80 291out:
a52d9a80
CM
292 return ret;
293}
a52d9a80 294
d352ac68 295/* simple helper to do math around the end of an extent, handling wrap */
d1310b2e
CM
296static u64 range_end(u64 start, u64 len)
297{
298 if (start + len < start)
299 return (u64)-1;
300 return start + len;
301}
302
9d2423c5
CH
303/**
304 * lookup_extent_mapping - lookup extent_map
305 * @tree: tree to lookup in
306 * @start: byte offset to start the search
307 * @len: length of the lookup range
308 *
309 * Find and return the first extent_map struct in @tree that intersects the
310 * [start, len] range. There may be additional objects in the tree that
311 * intersect, so check the object returned carefully to make sure that no
312 * additional lookups are needed.
a52d9a80
CM
313 */
314struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
d1310b2e 315 u64 start, u64 len)
a52d9a80
CM
316{
317 struct extent_map *em;
318 struct rb_node *rb_node;
306929f3
CH
319 struct rb_node *prev = NULL;
320 struct rb_node *next = NULL;
321 u64 end = range_end(start, len);
322
5f56406a
CM
323 rb_node = __tree_search(&tree->map, start, &prev, &next);
324 if (!rb_node && prev) {
325 em = rb_entry(prev, struct extent_map, rb_node);
d1310b2e 326 if (end > em->start && start < extent_map_end(em))
5f56406a
CM
327 goto found;
328 }
329 if (!rb_node && next) {
330 em = rb_entry(next, struct extent_map, rb_node);
d1310b2e 331 if (end > em->start && start < extent_map_end(em))
5f56406a
CM
332 goto found;
333 }
a52d9a80
CM
334 if (!rb_node) {
335 em = NULL;
336 goto out;
337 }
338 if (IS_ERR(rb_node)) {
339 em = ERR_PTR(PTR_ERR(rb_node));
340 goto out;
341 }
342 em = rb_entry(rb_node, struct extent_map, rb_node);
d1310b2e
CM
343 if (end > em->start && start < extent_map_end(em))
344 goto found;
345
346 em = NULL;
347 goto out;
348
5f56406a 349found:
a52d9a80
CM
350 atomic_inc(&em->refs);
351out:
a52d9a80
CM
352 return em;
353}
a52d9a80 354
b917b7c3
CM
355/**
356 * search_extent_mapping - find a nearby extent map
357 * @tree: tree to lookup in
358 * @start: byte offset to start the search
359 * @len: length of the lookup range
360 *
361 * Find and return the first extent_map struct in @tree that intersects the
362 * [start, len] range.
363 *
364 * If one can't be found, any nearby extent may be returned
365 */
366struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
367 u64 start, u64 len)
368{
369 struct extent_map *em;
370 struct rb_node *rb_node;
371 struct rb_node *prev = NULL;
372 struct rb_node *next = NULL;
373
374 rb_node = __tree_search(&tree->map, start, &prev, &next);
375 if (!rb_node && prev) {
376 em = rb_entry(prev, struct extent_map, rb_node);
377 goto found;
378 }
379 if (!rb_node && next) {
380 em = rb_entry(next, struct extent_map, rb_node);
381 goto found;
382 }
383 if (!rb_node) {
384 em = NULL;
385 goto out;
386 }
387 if (IS_ERR(rb_node)) {
388 em = ERR_PTR(PTR_ERR(rb_node));
389 goto out;
390 }
391 em = rb_entry(rb_node, struct extent_map, rb_node);
392 goto found;
393
394 em = NULL;
395 goto out;
396
397found:
398 atomic_inc(&em->refs);
399out:
400 return em;
401}
402
9d2423c5
CH
403/**
404 * remove_extent_mapping - removes an extent_map from the extent tree
405 * @tree: extent tree to remove from
406 * @em: extent map beeing removed
407 *
408 * Removes @em from @tree. No reference counts are dropped, and no checks
409 * are done to see if the range is in use
a52d9a80
CM
410 */
411int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
412{
d1310b2e 413 int ret = 0;
a52d9a80 414
7f3c74fb 415 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
d1310b2e
CM
416 rb_erase(&em->rb_node, &tree->map);
417 em->in_tree = 0;
a52d9a80
CM
418 return ret;
419}