omap_hsmmc: Flush posted write to IRQ
[linux-ginger.git] / fs / btrfs / extent_map.c
blob50da69da20cec141fca63ffba2a4e92b8f93f875
1 #include <linux/err.h>
2 #include <linux/gfp.h>
3 #include <linux/slab.h>
4 #include <linux/module.h>
5 #include <linux/spinlock.h>
6 #include <linux/hardirq.h>
7 #include "extent_map.h"
9 /* temporary define until extent_map moves out of btrfs */
10 struct 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));
15 static struct kmem_cache *extent_map_cache;
17 int __init extent_map_init(void)
19 extent_map_cache = btrfs_cache_create("extent_map",
20 sizeof(struct extent_map), 0,
21 NULL);
22 if (!extent_map_cache)
23 return -ENOMEM;
24 return 0;
27 void extent_map_exit(void)
29 if (extent_map_cache)
30 kmem_cache_destroy(extent_map_cache);
33 /**
34 * extent_map_tree_init - initialize extent map tree
35 * @tree: tree to initialize
36 * @mask: flags for memory allocations during tree operations
38 * Initialize the extent tree @tree. Should be called for each new inode
39 * or other user of the extent_map interface.
41 void extent_map_tree_init(struct extent_map_tree *tree, gfp_t mask)
43 tree->map.rb_node = NULL;
44 spin_lock_init(&tree->lock);
46 EXPORT_SYMBOL(extent_map_tree_init);
48 /**
49 * alloc_extent_map - allocate new extent map structure
50 * @mask: memory allocation flags
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()
56 struct extent_map *alloc_extent_map(gfp_t mask)
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;
63 em->flags = 0;
64 atomic_set(&em->refs, 1);
65 return em;
67 EXPORT_SYMBOL(alloc_extent_map);
69 /**
70 * free_extent_map - drop reference count of an extent_map
71 * @em: extent map beeing releasead
73 * Drops the reference out on @em by one and free the structure
74 * if the reference count hits zero.
76 void free_extent_map(struct extent_map *em)
78 if (!em)
79 return;
80 WARN_ON(atomic_read(&em->refs) == 0);
81 if (atomic_dec_and_test(&em->refs)) {
82 WARN_ON(em->in_tree);
83 kmem_cache_free(extent_map_cache, em);
86 EXPORT_SYMBOL(free_extent_map);
88 static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
89 struct rb_node *node)
91 struct rb_node **p = &root->rb_node;
92 struct rb_node *parent = NULL;
93 struct extent_map *entry;
95 while (*p) {
96 parent = *p;
97 entry = rb_entry(parent, struct extent_map, rb_node);
99 WARN_ON(!entry->in_tree);
101 if (offset < entry->start)
102 p = &(*p)->rb_left;
103 else if (offset >= extent_map_end(entry))
104 p = &(*p)->rb_right;
105 else
106 return parent;
109 entry = rb_entry(node, struct extent_map, rb_node);
110 entry->in_tree = 1;
111 rb_link_node(node, parent, p);
112 rb_insert_color(node, root);
113 return NULL;
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
120 static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
121 struct rb_node **prev_ret,
122 struct rb_node **next_ret)
124 struct rb_node *n = root->rb_node;
125 struct rb_node *prev = NULL;
126 struct rb_node *orig_prev = NULL;
127 struct extent_map *entry;
128 struct extent_map *prev_entry = NULL;
130 while (n) {
131 entry = rb_entry(n, struct extent_map, rb_node);
132 prev = n;
133 prev_entry = entry;
135 WARN_ON(!entry->in_tree);
137 if (offset < entry->start)
138 n = n->rb_left;
139 else if (offset >= extent_map_end(entry))
140 n = n->rb_right;
141 else
142 return n;
145 if (prev_ret) {
146 orig_prev = prev;
147 while (prev && offset >= extent_map_end(prev_entry)) {
148 prev = rb_next(prev);
149 prev_entry = rb_entry(prev, struct extent_map, rb_node);
151 *prev_ret = prev;
152 prev = orig_prev;
155 if (next_ret) {
156 prev_entry = rb_entry(prev, struct extent_map, rb_node);
157 while (prev && offset < prev_entry->start) {
158 prev = rb_prev(prev);
159 prev_entry = rb_entry(prev, struct extent_map, rb_node);
161 *next_ret = prev;
163 return NULL;
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'.
170 static inline struct rb_node *tree_search(struct rb_root *root, u64 offset)
172 struct rb_node *prev;
173 struct rb_node *ret;
174 ret = __tree_search(root, offset, &prev, NULL);
175 if (!ret)
176 return prev;
177 return ret;
180 /* check to see if two extent_map structs are adjacent and safe to merge */
181 static int mergable_maps(struct extent_map *prev, struct extent_map *next)
183 if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
184 return 0;
187 * don't merge compressed extents, we need to know their
188 * actual size
190 if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
191 return 0;
193 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;
206 return 0;
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
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.
219 int add_extent_mapping(struct extent_map_tree *tree,
220 struct extent_map *em)
222 int ret = 0;
223 struct extent_map *merge = NULL;
224 struct rb_node *rb;
225 struct extent_map *exist;
227 exist = lookup_extent_mapping(tree, em->start, em->len);
228 if (exist) {
229 free_extent_map(exist);
230 ret = -EEXIST;
231 goto out;
233 assert_spin_locked(&tree->lock);
234 rb = tree_insert(&tree->map, em->start, &em->rb_node);
235 if (rb) {
236 ret = -EEXIST;
237 free_extent_map(merge);
238 goto out;
240 atomic_inc(&em->refs);
241 if (em->start != 0) {
242 rb = rb_prev(&em->rb_node);
243 if (rb)
244 merge = rb_entry(rb, struct extent_map, rb_node);
245 if (rb && mergable_maps(merge, em)) {
246 em->start = merge->start;
247 em->len += merge->len;
248 em->block_len += merge->block_len;
249 em->block_start = merge->block_start;
250 merge->in_tree = 0;
251 rb_erase(&merge->rb_node, &tree->map);
252 free_extent_map(merge);
255 rb = rb_next(&em->rb_node);
256 if (rb)
257 merge = rb_entry(rb, struct extent_map, rb_node);
258 if (rb && mergable_maps(em, merge)) {
259 em->len += merge->len;
260 em->block_len += merge->len;
261 rb_erase(&merge->rb_node, &tree->map);
262 merge->in_tree = 0;
263 free_extent_map(merge);
265 out:
266 return ret;
268 EXPORT_SYMBOL(add_extent_mapping);
270 /* simple helper to do math around the end of an extent, handling wrap */
271 static u64 range_end(u64 start, u64 len)
273 if (start + len < start)
274 return (u64)-1;
275 return start + len;
279 * lookup_extent_mapping - lookup extent_map
280 * @tree: tree to lookup in
281 * @start: byte offset to start the search
282 * @len: length of the lookup range
284 * Find and return the first extent_map struct in @tree that intersects the
285 * [start, len] range. There may be additional objects in the tree that
286 * intersect, so check the object returned carefully to make sure that no
287 * additional lookups are needed.
289 struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
290 u64 start, u64 len)
292 struct extent_map *em;
293 struct rb_node *rb_node;
294 struct rb_node *prev = NULL;
295 struct rb_node *next = NULL;
296 u64 end = range_end(start, len);
298 assert_spin_locked(&tree->lock);
299 rb_node = __tree_search(&tree->map, start, &prev, &next);
300 if (!rb_node && prev) {
301 em = rb_entry(prev, struct extent_map, rb_node);
302 if (end > em->start && start < extent_map_end(em))
303 goto found;
305 if (!rb_node && next) {
306 em = rb_entry(next, struct extent_map, rb_node);
307 if (end > em->start && start < extent_map_end(em))
308 goto found;
310 if (!rb_node) {
311 em = NULL;
312 goto out;
314 if (IS_ERR(rb_node)) {
315 em = ERR_PTR(PTR_ERR(rb_node));
316 goto out;
318 em = rb_entry(rb_node, struct extent_map, rb_node);
319 if (end > em->start && start < extent_map_end(em))
320 goto found;
322 em = NULL;
323 goto out;
325 found:
326 atomic_inc(&em->refs);
327 out:
328 return em;
330 EXPORT_SYMBOL(lookup_extent_mapping);
333 * remove_extent_mapping - removes an extent_map from the extent tree
334 * @tree: extent tree to remove from
335 * @em: extent map beeing removed
337 * Removes @em from @tree. No reference counts are dropped, and no checks
338 * are done to see if the range is in use
340 int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
342 int ret = 0;
344 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
345 assert_spin_locked(&tree->lock);
346 rb_erase(&em->rb_node, &tree->map);
347 em->in_tree = 0;
348 return ret;
350 EXPORT_SYMBOL(remove_extent_mapping);