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("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 rwlock_init(&tree
->lock
);
42 * alloc_extent_map - allocate new extent map structure
44 * Allocate a new extent_map structure. The new structure is
45 * returned with a reference count of one and needs to be
46 * freed using free_extent_map()
48 struct extent_map
*alloc_extent_map(void)
50 struct extent_map
*em
;
51 em
= kmem_cache_alloc(extent_map_cache
, GFP_NOFS
);
56 em
->compress_type
= BTRFS_COMPRESS_NONE
;
57 atomic_set(&em
->refs
, 1);
62 * free_extent_map - drop reference count of an extent_map
63 * @em: extent map beeing releasead
65 * Drops the reference out on @em by one and free the structure
66 * if the reference count hits zero.
68 void free_extent_map(struct extent_map
*em
)
72 WARN_ON(atomic_read(&em
->refs
) == 0);
73 if (atomic_dec_and_test(&em
->refs
)) {
75 kmem_cache_free(extent_map_cache
, em
);
79 static struct rb_node
*tree_insert(struct rb_root
*root
, u64 offset
,
82 struct rb_node
**p
= &root
->rb_node
;
83 struct rb_node
*parent
= NULL
;
84 struct extent_map
*entry
;
88 entry
= rb_entry(parent
, struct extent_map
, rb_node
);
90 WARN_ON(!entry
->in_tree
);
92 if (offset
< entry
->start
)
94 else if (offset
>= extent_map_end(entry
))
100 entry
= rb_entry(node
, struct extent_map
, rb_node
);
102 rb_link_node(node
, parent
, p
);
103 rb_insert_color(node
, root
);
108 * search through the tree for an extent_map with a given offset. If
109 * it can't be found, try to find some neighboring extents
111 static struct rb_node
*__tree_search(struct rb_root
*root
, u64 offset
,
112 struct rb_node
**prev_ret
,
113 struct rb_node
**next_ret
)
115 struct rb_node
*n
= root
->rb_node
;
116 struct rb_node
*prev
= NULL
;
117 struct rb_node
*orig_prev
= NULL
;
118 struct extent_map
*entry
;
119 struct extent_map
*prev_entry
= NULL
;
122 entry
= rb_entry(n
, struct extent_map
, rb_node
);
126 WARN_ON(!entry
->in_tree
);
128 if (offset
< entry
->start
)
130 else if (offset
>= extent_map_end(entry
))
138 while (prev
&& offset
>= extent_map_end(prev_entry
)) {
139 prev
= rb_next(prev
);
140 prev_entry
= rb_entry(prev
, struct extent_map
, rb_node
);
147 prev_entry
= rb_entry(prev
, struct extent_map
, rb_node
);
148 while (prev
&& offset
< prev_entry
->start
) {
149 prev
= rb_prev(prev
);
150 prev_entry
= rb_entry(prev
, struct extent_map
, rb_node
);
157 /* check to see if two extent_map structs are adjacent and safe to merge */
158 static int mergable_maps(struct extent_map
*prev
, struct extent_map
*next
)
160 if (test_bit(EXTENT_FLAG_PINNED
, &prev
->flags
))
164 * don't merge compressed extents, we need to know their
167 if (test_bit(EXTENT_FLAG_COMPRESSED
, &prev
->flags
))
170 if (extent_map_end(prev
) == next
->start
&&
171 prev
->flags
== next
->flags
&&
172 prev
->bdev
== next
->bdev
&&
173 ((next
->block_start
== EXTENT_MAP_HOLE
&&
174 prev
->block_start
== EXTENT_MAP_HOLE
) ||
175 (next
->block_start
== EXTENT_MAP_INLINE
&&
176 prev
->block_start
== EXTENT_MAP_INLINE
) ||
177 (next
->block_start
== EXTENT_MAP_DELALLOC
&&
178 prev
->block_start
== EXTENT_MAP_DELALLOC
) ||
179 (next
->block_start
< EXTENT_MAP_LAST_BYTE
- 1 &&
180 next
->block_start
== extent_map_block_end(prev
)))) {
186 static void try_merge_map(struct extent_map_tree
*tree
, struct extent_map
*em
)
188 struct extent_map
*merge
= NULL
;
191 if (em
->start
!= 0) {
192 rb
= rb_prev(&em
->rb_node
);
194 merge
= rb_entry(rb
, struct extent_map
, rb_node
);
195 if (rb
&& mergable_maps(merge
, em
)) {
196 em
->start
= merge
->start
;
197 em
->len
+= merge
->len
;
198 em
->block_len
+= merge
->block_len
;
199 em
->block_start
= merge
->block_start
;
201 rb_erase(&merge
->rb_node
, &tree
->map
);
202 free_extent_map(merge
);
206 rb
= rb_next(&em
->rb_node
);
208 merge
= rb_entry(rb
, struct extent_map
, rb_node
);
209 if (rb
&& mergable_maps(em
, merge
)) {
210 em
->len
+= merge
->len
;
211 em
->block_len
+= merge
->len
;
212 rb_erase(&merge
->rb_node
, &tree
->map
);
214 free_extent_map(merge
);
218 int unpin_extent_cache(struct extent_map_tree
*tree
, u64 start
, u64 len
)
221 struct extent_map
*em
;
223 write_lock(&tree
->lock
);
224 em
= lookup_extent_mapping(tree
, start
, len
);
226 WARN_ON(!em
|| em
->start
!= start
);
231 clear_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
233 try_merge_map(tree
, em
);
237 write_unlock(&tree
->lock
);
243 * add_extent_mapping - add new extent map to the extent tree
244 * @tree: tree to insert new map in
247 * Insert @em into @tree or perform a simple forward/backward merge with
248 * existing mappings. The extent_map struct passed in will be inserted
249 * into the tree directly, with an additional reference taken, or a
250 * reference dropped if the merge attempt was successful.
252 int add_extent_mapping(struct extent_map_tree
*tree
,
253 struct extent_map
*em
)
257 struct extent_map
*exist
;
259 exist
= lookup_extent_mapping(tree
, em
->start
, em
->len
);
261 free_extent_map(exist
);
265 rb
= tree_insert(&tree
->map
, em
->start
, &em
->rb_node
);
270 atomic_inc(&em
->refs
);
272 try_merge_map(tree
, em
);
277 /* simple helper to do math around the end of an extent, handling wrap */
278 static u64
range_end(u64 start
, u64 len
)
280 if (start
+ len
< start
)
285 struct extent_map
*__lookup_extent_mapping(struct extent_map_tree
*tree
,
286 u64 start
, u64 len
, int strict
)
288 struct extent_map
*em
;
289 struct rb_node
*rb_node
;
290 struct rb_node
*prev
= NULL
;
291 struct rb_node
*next
= NULL
;
292 u64 end
= range_end(start
, len
);
294 rb_node
= __tree_search(&tree
->map
, start
, &prev
, &next
);
304 em
= rb_entry(rb_node
, struct extent_map
, rb_node
);
306 if (strict
&& !(end
> em
->start
&& start
< extent_map_end(em
)))
309 atomic_inc(&em
->refs
);
314 * lookup_extent_mapping - lookup extent_map
315 * @tree: tree to lookup in
316 * @start: byte offset to start the search
317 * @len: length of the lookup range
319 * Find and return the first extent_map struct in @tree that intersects the
320 * [start, len] range. There may be additional objects in the tree that
321 * intersect, so check the object returned carefully to make sure that no
322 * additional lookups are needed.
324 struct extent_map
*lookup_extent_mapping(struct extent_map_tree
*tree
,
327 return __lookup_extent_mapping(tree
, start
, len
, 1);
331 * search_extent_mapping - find a nearby extent map
332 * @tree: tree to lookup in
333 * @start: byte offset to start the search
334 * @len: length of the lookup range
336 * Find and return the first extent_map struct in @tree that intersects the
337 * [start, len] range.
339 * If one can't be found, any nearby extent may be returned
341 struct extent_map
*search_extent_mapping(struct extent_map_tree
*tree
,
344 return __lookup_extent_mapping(tree
, start
, len
, 0);
348 * remove_extent_mapping - removes an extent_map from the extent tree
349 * @tree: extent tree to remove from
350 * @em: extent map beeing removed
352 * Removes @em from @tree. No reference counts are dropped, and no checks
353 * are done to see if the range is in use
355 int remove_extent_mapping(struct extent_map_tree
*tree
, struct extent_map
*em
)
359 WARN_ON(test_bit(EXTENT_FLAG_PINNED
, &em
->flags
));
360 rb_erase(&em
->rb_node
, &tree
->map
);