2 #include <linux/slab.h>
3 #include <linux/module.h>
4 #include <linux/spinlock.h>
5 #include <linux/hardirq.h>
7 #include "extent_map.h"
10 static struct kmem_cache *extent_map_cache;
12 int __init extent_map_init(void)
14 extent_map_cache = kmem_cache_create("btrfs_extent_map",
15 sizeof(struct extent_map), 0,
16 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
17 if (!extent_map_cache)
22 void extent_map_exit(void)
25 kmem_cache_destroy(extent_map_cache);
29 * extent_map_tree_init - initialize extent map tree
30 * @tree: tree to initialize
32 * Initialize the extent tree @tree. Should be called for each new inode
33 * or other user of the extent_map interface.
35 void extent_map_tree_init(struct extent_map_tree *tree)
38 INIT_LIST_HEAD(&tree->modified_extents);
39 rwlock_init(&tree->lock);
43 * alloc_extent_map - allocate new extent map structure
45 * Allocate a new extent_map structure. The new structure is
46 * returned with a reference count of one and needs to be
47 * freed using free_extent_map()
49 struct extent_map *alloc_extent_map(void)
51 struct extent_map *em;
52 em = kmem_cache_alloc(extent_map_cache, GFP_NOFS);
57 em->compress_type = BTRFS_COMPRESS_NONE;
59 atomic_set(&em->refs, 1);
60 INIT_LIST_HEAD(&em->list);
65 * free_extent_map - drop reference count of an extent_map
66 * @em: extent map beeing releasead
68 * Drops the reference out on @em by one and free the structure
69 * if the reference count hits zero.
71 void free_extent_map(struct extent_map *em)
75 WARN_ON(atomic_read(&em->refs) == 0);
76 if (atomic_dec_and_test(&em->refs)) {
78 WARN_ON(!list_empty(&em->list));
79 kmem_cache_free(extent_map_cache, em);
83 static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
86 struct rb_node **p = &root->rb_node;
87 struct rb_node *parent = NULL;
88 struct extent_map *entry;
92 entry = rb_entry(parent, struct extent_map, rb_node);
94 WARN_ON(!entry->in_tree);
96 if (offset < entry->start)
98 else if (offset >= extent_map_end(entry))
104 entry = rb_entry(node, struct extent_map, rb_node);
106 rb_link_node(node, parent, p);
107 rb_insert_color(node, root);
112 * search through the tree for an extent_map with a given offset. If
113 * it can't be found, try to find some neighboring extents
115 static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
116 struct rb_node **prev_ret,
117 struct rb_node **next_ret)
119 struct rb_node *n = root->rb_node;
120 struct rb_node *prev = NULL;
121 struct rb_node *orig_prev = NULL;
122 struct extent_map *entry;
123 struct extent_map *prev_entry = NULL;
126 entry = rb_entry(n, struct extent_map, rb_node);
130 WARN_ON(!entry->in_tree);
132 if (offset < entry->start)
134 else if (offset >= extent_map_end(entry))
142 while (prev && offset >= extent_map_end(prev_entry)) {
143 prev = rb_next(prev);
144 prev_entry = rb_entry(prev, struct extent_map, rb_node);
151 prev_entry = rb_entry(prev, struct extent_map, rb_node);
152 while (prev && offset < prev_entry->start) {
153 prev = rb_prev(prev);
154 prev_entry = rb_entry(prev, struct extent_map, rb_node);
161 /* check to see if two extent_map structs are adjacent and safe to merge */
162 static int mergable_maps(struct extent_map *prev, struct extent_map *next)
164 if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
168 * don't merge compressed extents, we need to know their
171 if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
174 if (extent_map_end(prev) == next->start &&
175 prev->flags == next->flags &&
176 prev->bdev == next->bdev &&
177 ((next->block_start == EXTENT_MAP_HOLE &&
178 prev->block_start == EXTENT_MAP_HOLE) ||
179 (next->block_start == EXTENT_MAP_INLINE &&
180 prev->block_start == EXTENT_MAP_INLINE) ||
181 (next->block_start == EXTENT_MAP_DELALLOC &&
182 prev->block_start == EXTENT_MAP_DELALLOC) ||
183 (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
184 next->block_start == extent_map_block_end(prev)))) {
190 static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
192 struct extent_map *merge = NULL;
195 if (em->start != 0) {
196 rb = rb_prev(&em->rb_node);
198 merge = rb_entry(rb, struct extent_map, rb_node);
199 if (rb && mergable_maps(merge, em)) {
200 em->start = merge->start;
201 em->len += merge->len;
202 em->block_len += merge->block_len;
203 em->block_start = merge->block_start;
205 if (merge->generation > em->generation) {
206 em->mod_start = em->start;
207 em->mod_len = em->len;
208 em->generation = merge->generation;
209 list_move(&em->list, &tree->modified_extents);
212 list_del_init(&merge->list);
213 rb_erase(&merge->rb_node, &tree->map);
214 free_extent_map(merge);
218 rb = rb_next(&em->rb_node);
220 merge = rb_entry(rb, struct extent_map, rb_node);
221 if (rb && mergable_maps(em, merge)) {
222 em->len += merge->len;
223 em->block_len += merge->len;
224 rb_erase(&merge->rb_node, &tree->map);
226 if (merge->generation > em->generation) {
227 em->mod_len = em->len;
228 em->generation = merge->generation;
229 list_move(&em->list, &tree->modified_extents);
231 list_del_init(&merge->list);
232 free_extent_map(merge);
237 * unpint_extent_cache - unpin an extent from the cache
238 * @tree: tree to unpin the extent in
239 * @start: logical offset in the file
240 * @len: length of the extent
241 * @gen: generation that this extent has been modified in
242 * @prealloc: if this is set we need to clear the prealloc flag
244 * Called after an extent has been written to disk properly. Set the generation
245 * to the generation that actually added the file item to the inode so we know
246 * we need to sync this extent when we call fsync().
248 int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
252 struct extent_map *em;
253 bool prealloc = false;
255 write_lock(&tree->lock);
256 em = lookup_extent_mapping(tree, start, len);
258 WARN_ON(!em || em->start != start);
263 list_move(&em->list, &tree->modified_extents);
264 em->generation = gen;
265 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
266 em->mod_start = em->start;
267 em->mod_len = em->len;
269 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
271 clear_bit(EXTENT_FLAG_PREALLOC, &em->flags);
274 try_merge_map(tree, em);
277 em->mod_start = em->start;
278 em->mod_len = em->len;
283 write_unlock(&tree->lock);
289 * add_extent_mapping - add new extent map to the extent tree
290 * @tree: tree to insert new map in
293 * Insert @em into @tree or perform a simple forward/backward merge with
294 * existing mappings. The extent_map struct passed in will be inserted
295 * into the tree directly, with an additional reference taken, or a
296 * reference dropped if the merge attempt was successful.
298 int add_extent_mapping(struct extent_map_tree *tree,
299 struct extent_map *em)
303 struct extent_map *exist;
305 exist = lookup_extent_mapping(tree, em->start, em->len);
307 free_extent_map(exist);
311 rb = tree_insert(&tree->map, em->start, &em->rb_node);
316 atomic_inc(&em->refs);
318 em->mod_start = em->start;
319 em->mod_len = em->len;
321 try_merge_map(tree, em);
326 /* simple helper to do math around the end of an extent, handling wrap */
327 static u64 range_end(u64 start, u64 len)
329 if (start + len < start)
334 struct extent_map *__lookup_extent_mapping(struct extent_map_tree *tree,
335 u64 start, u64 len, int strict)
337 struct extent_map *em;
338 struct rb_node *rb_node;
339 struct rb_node *prev = NULL;
340 struct rb_node *next = NULL;
341 u64 end = range_end(start, len);
343 rb_node = __tree_search(&tree->map, start, &prev, &next);
353 em = rb_entry(rb_node, struct extent_map, rb_node);
355 if (strict && !(end > em->start && start < extent_map_end(em)))
358 atomic_inc(&em->refs);
363 * lookup_extent_mapping - lookup extent_map
364 * @tree: tree to lookup in
365 * @start: byte offset to start the search
366 * @len: length of the lookup range
368 * Find and return the first extent_map struct in @tree that intersects the
369 * [start, len] range. There may be additional objects in the tree that
370 * intersect, so check the object returned carefully to make sure that no
371 * additional lookups are needed.
373 struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
376 return __lookup_extent_mapping(tree, start, len, 1);
380 * search_extent_mapping - find a nearby extent map
381 * @tree: tree to lookup in
382 * @start: byte offset to start the search
383 * @len: length of the lookup range
385 * Find and return the first extent_map struct in @tree that intersects the
386 * [start, len] range.
388 * If one can't be found, any nearby extent may be returned
390 struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
393 return __lookup_extent_mapping(tree, start, len, 0);
397 * remove_extent_mapping - removes an extent_map from the extent tree
398 * @tree: extent tree to remove from
399 * @em: extent map beeing removed
401 * Removes @em from @tree. No reference counts are dropped, and no checks
402 * are done to see if the range is in use
404 int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
408 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
409 rb_erase(&em->rb_node, &tree->map);
410 if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
411 list_del_init(&em->list);