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
*,
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,
22 if (!extent_map_cache
)
27 void extent_map_exit(void)
30 kmem_cache_destroy(extent_map_cache
);
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
);
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
))
64 atomic_set(&em
->refs
, 1);
67 EXPORT_SYMBOL(alloc_extent_map
);
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
)
80 WARN_ON(atomic_read(&em
->refs
) == 0);
81 if (atomic_dec_and_test(&em
->refs
)) {
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
,
91 struct rb_node
**p
= &root
->rb_node
;
92 struct rb_node
*parent
= NULL
;
93 struct extent_map
*entry
;
97 entry
= rb_entry(parent
, struct extent_map
, rb_node
);
99 WARN_ON(!entry
->in_tree
);
101 if (offset
< entry
->start
)
103 else if (offset
>= extent_map_end(entry
))
109 entry
= rb_entry(node
, struct extent_map
, rb_node
);
111 rb_link_node(node
, parent
, p
);
112 rb_insert_color(node
, root
);
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
;
131 entry
= rb_entry(n
, struct extent_map
, rb_node
);
135 WARN_ON(!entry
->in_tree
);
137 if (offset
< entry
->start
)
139 else if (offset
>= extent_map_end(entry
))
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
);
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
);
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
;
174 ret
= __tree_search(root
, offset
, &prev
, NULL
);
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
))
187 * don't merge compressed extents, we need to know their
190 if (test_bit(EXTENT_FLAG_COMPRESSED
, &prev
->flags
))
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
)))) {
210 * add_extent_mapping - add new extent map to the extent tree
211 * @tree: tree to insert new map in
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
)
223 struct extent_map
*merge
= NULL
;
225 struct extent_map
*exist
;
227 exist
= lookup_extent_mapping(tree
, em
->start
, em
->len
);
229 free_extent_map(exist
);
233 assert_spin_locked(&tree
->lock
);
234 rb
= tree_insert(&tree
->map
, em
->start
, &em
->rb_node
);
237 free_extent_map(merge
);
240 atomic_inc(&em
->refs
);
241 if (em
->start
!= 0) {
242 rb
= rb_prev(&em
->rb_node
);
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
;
251 rb_erase(&merge
->rb_node
, &tree
->map
);
252 free_extent_map(merge
);
255 rb
= rb_next(&em
->rb_node
);
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
);
263 free_extent_map(merge
);
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
)
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
,
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
))
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
))
314 if (IS_ERR(rb_node
)) {
315 em
= ERR_PTR(PTR_ERR(rb_node
));
318 em
= rb_entry(rb_node
, struct extent_map
, rb_node
);
319 if (end
> em
->start
&& start
< extent_map_end(em
))
326 atomic_inc(&em
->refs
);
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
)
344 WARN_ON(test_bit(EXTENT_FLAG_PINNED
, &em
->flags
));
345 assert_spin_locked(&tree
->lock
);
346 rb_erase(&em
->rb_node
, &tree
->map
);
350 EXPORT_SYMBOL(remove_extent_mapping
);