blob: df7a803005fbf6b4ebdd080cd103667822797768 [file] [log] [blame]
Chris Masond1310b22008-01-24 16:13:08 -05001#include <linux/err.h>
Chris Masond1310b22008-01-24 16:13:08 -05002#include <linux/slab.h>
Chris Masona52d9a82007-08-27 16:49:44 -04003#include <linux/module.h>
4#include <linux/spinlock.h>
Chris Masond1310b22008-01-24 16:13:08 -05005#include <linux/hardirq.h>
Li Zefan261507a02010-12-17 14:21:50 +08006#include "ctree.h"
Chris Masona52d9a82007-08-27 16:49:44 -04007#include "extent_map.h"
8
Chris Mason86479a02007-09-10 19:58:16 -04009
Chris Masona52d9a82007-08-27 16:49:44 -040010static struct kmem_cache *extent_map_cache;
Chris Masonca664622007-11-27 11:16:35 -050011
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050012int __init extent_map_init(void)
Chris Masona52d9a82007-08-27 16:49:44 -040013{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020014 extent_map_cache = kmem_cache_create("extent_map",
15 sizeof(struct extent_map), 0,
16 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050017 if (!extent_map_cache)
18 return -ENOMEM;
Wyatt Banks2f4cbe62007-11-19 10:22:33 -050019 return 0;
Chris Masona52d9a82007-08-27 16:49:44 -040020}
21
Christian Hesse17636e02007-12-11 09:25:06 -050022void extent_map_exit(void)
Chris Masona52d9a82007-08-27 16:49:44 -040023{
Chris Masona52d9a82007-08-27 16:49:44 -040024 if (extent_map_cache)
25 kmem_cache_destroy(extent_map_cache);
Chris Masona52d9a82007-08-27 16:49:44 -040026}
27
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040028/**
29 * extent_map_tree_init - initialize extent map tree
30 * @tree: tree to initialize
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040031 *
32 * Initialize the extent tree @tree. Should be called for each new inode
33 * or other user of the extent_map interface.
34 */
David Sterbaa8067e02011-04-21 00:34:43 +020035void extent_map_tree_init(struct extent_map_tree *tree)
Chris Masona52d9a82007-08-27 16:49:44 -040036{
Eric Paris6bef4d32010-02-23 19:43:04 +000037 tree->map = RB_ROOT;
Chris Mason890871b2009-09-02 16:24:52 -040038 rwlock_init(&tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -040039}
Chris Masona52d9a82007-08-27 16:49:44 -040040
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040041/**
42 * alloc_extent_map - allocate new extent map structure
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040043 *
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()
47 */
David Sterba172ddd62011-04-21 00:48:27 +020048struct extent_map *alloc_extent_map(void)
Chris Masona52d9a82007-08-27 16:49:44 -040049{
50 struct extent_map *em;
David Sterba172ddd62011-04-21 00:48:27 +020051 em = kmem_cache_alloc(extent_map_cache, GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +000052 if (!em)
53 return NULL;
Chris Masona52d9a82007-08-27 16:49:44 -040054 em->in_tree = 0;
Chris Masond1310b22008-01-24 16:13:08 -050055 em->flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +080056 em->compress_type = BTRFS_COMPRESS_NONE;
Chris Masona52d9a82007-08-27 16:49:44 -040057 atomic_set(&em->refs, 1);
58 return em;
59}
Chris Masona52d9a82007-08-27 16:49:44 -040060
Christoph Hellwig9d2423c2008-06-11 21:52:17 -040061/**
62 * free_extent_map - drop reference count of an extent_map
63 * @em: extent map beeing releasead
64 *
65 * Drops the reference out on @em by one and free the structure
66 * if the reference count hits zero.
67 */
Chris Masona52d9a82007-08-27 16:49:44 -040068void free_extent_map(struct extent_map *em)
69{
Chris Mason2bf5a722007-08-30 11:54:02 -040070 if (!em)
71 return;
Chris Masond1310b22008-01-24 16:13:08 -050072 WARN_ON(atomic_read(&em->refs) == 0);
Chris Masona52d9a82007-08-27 16:49:44 -040073 if (atomic_dec_and_test(&em->refs)) {
74 WARN_ON(em->in_tree);
75 kmem_cache_free(extent_map_cache, em);
76 }
77}
Chris Masona52d9a82007-08-27 16:49:44 -040078
Chris Masona52d9a82007-08-27 16:49:44 -040079static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
80 struct rb_node *node)
81{
Chris Masond3977122009-01-05 21:25:51 -050082 struct rb_node **p = &root->rb_node;
83 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -050084 struct extent_map *entry;
Chris Masona52d9a82007-08-27 16:49:44 -040085
Chris Masond3977122009-01-05 21:25:51 -050086 while (*p) {
Chris Masona52d9a82007-08-27 16:49:44 -040087 parent = *p;
Chris Masond1310b22008-01-24 16:13:08 -050088 entry = rb_entry(parent, struct extent_map, rb_node);
89
90 WARN_ON(!entry->in_tree);
Chris Masona52d9a82007-08-27 16:49:44 -040091
92 if (offset < entry->start)
93 p = &(*p)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -050094 else if (offset >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -040095 p = &(*p)->rb_right;
96 else
97 return parent;
98 }
99
Chris Masond1310b22008-01-24 16:13:08 -0500100 entry = rb_entry(node, struct extent_map, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400101 entry->in_tree = 1;
102 rb_link_node(node, parent, p);
103 rb_insert_color(node, root);
104 return NULL;
105}
106
Chris Masond352ac62008-09-29 15:18:18 -0400107/*
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
110 */
Chris Masona52d9a82007-08-27 16:49:44 -0400111static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
Chris Mason5f564062008-01-22 16:47:59 -0500112 struct rb_node **prev_ret,
113 struct rb_node **next_ret)
Chris Masona52d9a82007-08-27 16:49:44 -0400114{
Chris Masond3977122009-01-05 21:25:51 -0500115 struct rb_node *n = root->rb_node;
Chris Masona52d9a82007-08-27 16:49:44 -0400116 struct rb_node *prev = NULL;
Chris Mason5f564062008-01-22 16:47:59 -0500117 struct rb_node *orig_prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500118 struct extent_map *entry;
119 struct extent_map *prev_entry = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400120
Chris Masond3977122009-01-05 21:25:51 -0500121 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500122 entry = rb_entry(n, struct extent_map, rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400123 prev = n;
124 prev_entry = entry;
125
Chris Masond1310b22008-01-24 16:13:08 -0500126 WARN_ON(!entry->in_tree);
127
Chris Masona52d9a82007-08-27 16:49:44 -0400128 if (offset < entry->start)
129 n = n->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500130 else if (offset >= extent_map_end(entry))
Chris Masona52d9a82007-08-27 16:49:44 -0400131 n = n->rb_right;
132 else
133 return n;
134 }
Chris Mason5f564062008-01-22 16:47:59 -0500135
136 if (prev_ret) {
137 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500138 while (prev && offset >= extent_map_end(prev_entry)) {
Chris Mason5f564062008-01-22 16:47:59 -0500139 prev = rb_next(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500140 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500141 }
142 *prev_ret = prev;
143 prev = orig_prev;
Chris Masona52d9a82007-08-27 16:49:44 -0400144 }
Chris Mason5f564062008-01-22 16:47:59 -0500145
146 if (next_ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500147 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500148 while (prev && offset < prev_entry->start) {
Chris Mason5f564062008-01-22 16:47:59 -0500149 prev = rb_prev(prev);
Chris Masond1310b22008-01-24 16:13:08 -0500150 prev_entry = rb_entry(prev, struct extent_map, rb_node);
Chris Mason5f564062008-01-22 16:47:59 -0500151 }
152 *next_ret = prev;
153 }
Chris Masona52d9a82007-08-27 16:49:44 -0400154 return NULL;
155}
156
Chris Masond352ac62008-09-29 15:18:18 -0400157/* check to see if two extent_map structs are adjacent and safe to merge */
Chris Masond1310b22008-01-24 16:13:08 -0500158static int mergable_maps(struct extent_map *prev, struct extent_map *next)
Chris Masona52d9a82007-08-27 16:49:44 -0400159{
Chris Mason7f3c74f2008-07-18 12:01:11 -0400160 if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
161 return 0;
162
Chris Masonc8b97812008-10-29 14:49:59 -0400163 /*
164 * don't merge compressed extents, we need to know their
165 * actual size
166 */
167 if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
168 return 0;
169
Chris Masond1310b22008-01-24 16:13:08 -0500170 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)))) {
181 return 1;
182 }
Chris Masona52d9a82007-08-27 16:49:44 -0400183 return 0;
184}
185
Chris Masona1ed8352009-09-11 12:27:37 -0400186int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len)
187{
188 int ret = 0;
189 struct extent_map *merge = NULL;
190 struct rb_node *rb;
191 struct extent_map *em;
192
193 write_lock(&tree->lock);
194 em = lookup_extent_mapping(tree, start, len);
195
Dan Carpenter4eb39912009-11-10 09:01:43 +0000196 WARN_ON(!em || em->start != start);
Chris Masona1ed8352009-09-11 12:27:37 -0400197
198 if (!em)
199 goto out;
200
201 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
202
203 if (em->start != 0) {
204 rb = rb_prev(&em->rb_node);
205 if (rb)
206 merge = rb_entry(rb, struct extent_map, rb_node);
207 if (rb && mergable_maps(merge, em)) {
208 em->start = merge->start;
209 em->len += merge->len;
210 em->block_len += merge->block_len;
211 em->block_start = merge->block_start;
212 merge->in_tree = 0;
213 rb_erase(&merge->rb_node, &tree->map);
214 free_extent_map(merge);
215 }
216 }
217
218 rb = rb_next(&em->rb_node);
219 if (rb)
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);
225 merge->in_tree = 0;
226 free_extent_map(merge);
227 }
228
229 free_extent_map(em);
230out:
231 write_unlock(&tree->lock);
232 return ret;
233
234}
235
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400236/**
237 * add_extent_mapping - add new extent map to the extent tree
238 * @tree: tree to insert new map in
239 * @em: map to insert
240 *
241 * Insert @em into @tree or perform a simple forward/backward merge with
242 * existing mappings. The extent_map struct passed in will be inserted
243 * into the tree directly, with an additional reference taken, or a
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300244 * reference dropped if the merge attempt was successful.
Chris Masona52d9a82007-08-27 16:49:44 -0400245 */
246int add_extent_mapping(struct extent_map_tree *tree,
247 struct extent_map *em)
248{
249 int ret = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500250 struct extent_map *merge = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400251 struct rb_node *rb;
Chris Mason7c2fe322008-08-20 08:51:50 -0400252 struct extent_map *exist;
Chris Masona52d9a82007-08-27 16:49:44 -0400253
Chris Mason7c2fe322008-08-20 08:51:50 -0400254 exist = lookup_extent_mapping(tree, em->start, em->len);
255 if (exist) {
256 free_extent_map(exist);
257 ret = -EEXIST;
258 goto out;
259 }
Chris Masond1310b22008-01-24 16:13:08 -0500260 rb = tree_insert(&tree->map, em->start, &em->rb_node);
Chris Masona52d9a82007-08-27 16:49:44 -0400261 if (rb) {
Chris Masona52d9a82007-08-27 16:49:44 -0400262 ret = -EEXIST;
263 goto out;
264 }
265 atomic_inc(&em->refs);
266 if (em->start != 0) {
267 rb = rb_prev(&em->rb_node);
268 if (rb)
Chris Masond1310b22008-01-24 16:13:08 -0500269 merge = rb_entry(rb, struct extent_map, rb_node);
270 if (rb && mergable_maps(merge, em)) {
271 em->start = merge->start;
272 em->len += merge->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400273 em->block_len += merge->block_len;
Chris Masond1310b22008-01-24 16:13:08 -0500274 em->block_start = merge->block_start;
275 merge->in_tree = 0;
276 rb_erase(&merge->rb_node, &tree->map);
277 free_extent_map(merge);
Chris Masona52d9a82007-08-27 16:49:44 -0400278 }
279 }
Chris Masond1310b22008-01-24 16:13:08 -0500280 rb = rb_next(&em->rb_node);
281 if (rb)
282 merge = rb_entry(rb, struct extent_map, rb_node);
283 if (rb && mergable_maps(em, merge)) {
284 em->len += merge->len;
Chris Masonc8b97812008-10-29 14:49:59 -0400285 em->block_len += merge->len;
Chris Masond1310b22008-01-24 16:13:08 -0500286 rb_erase(&merge->rb_node, &tree->map);
287 merge->in_tree = 0;
288 free_extent_map(merge);
289 }
Chris Masona52d9a82007-08-27 16:49:44 -0400290out:
Chris Masona52d9a82007-08-27 16:49:44 -0400291 return ret;
292}
Chris Masona52d9a82007-08-27 16:49:44 -0400293
Chris Masond352ac62008-09-29 15:18:18 -0400294/* simple helper to do math around the end of an extent, handling wrap */
Chris Masond1310b22008-01-24 16:13:08 -0500295static u64 range_end(u64 start, u64 len)
296{
297 if (start + len < start)
298 return (u64)-1;
299 return start + len;
300}
301
Li Zefaned64f062011-07-14 03:18:15 +0000302struct extent_map *__lookup_extent_mapping(struct extent_map_tree *tree,
303 u64 start, u64 len, int strict)
304{
305 struct extent_map *em;
306 struct rb_node *rb_node;
307 struct rb_node *prev = NULL;
308 struct rb_node *next = NULL;
309 u64 end = range_end(start, len);
310
311 rb_node = __tree_search(&tree->map, start, &prev, &next);
312 if (!rb_node) {
313 if (prev)
314 rb_node = prev;
315 else if (next)
316 rb_node = next;
317 else
318 return NULL;
319 }
320
321 em = rb_entry(rb_node, struct extent_map, rb_node);
322
323 if (strict && !(end > em->start && start < extent_map_end(em)))
324 return NULL;
325
326 atomic_inc(&em->refs);
327 return em;
328}
329
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400330/**
331 * lookup_extent_mapping - lookup extent_map
332 * @tree: tree to lookup in
333 * @start: byte offset to start the search
334 * @len: length of the lookup range
335 *
336 * Find and return the first extent_map struct in @tree that intersects the
337 * [start, len] range. There may be additional objects in the tree that
338 * intersect, so check the object returned carefully to make sure that no
339 * additional lookups are needed.
Chris Masona52d9a82007-08-27 16:49:44 -0400340 */
341struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500342 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -0400343{
Li Zefaned64f062011-07-14 03:18:15 +0000344 return __lookup_extent_mapping(tree, start, len, 1);
Chris Masona52d9a82007-08-27 16:49:44 -0400345}
Chris Masona52d9a82007-08-27 16:49:44 -0400346
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400347/**
Chris Masonb917b7c2009-09-18 16:07:03 -0400348 * search_extent_mapping - find a nearby extent map
349 * @tree: tree to lookup in
350 * @start: byte offset to start the search
351 * @len: length of the lookup range
352 *
353 * Find and return the first extent_map struct in @tree that intersects the
354 * [start, len] range.
355 *
356 * If one can't be found, any nearby extent may be returned
357 */
358struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
359 u64 start, u64 len)
360{
Li Zefaned64f062011-07-14 03:18:15 +0000361 return __lookup_extent_mapping(tree, start, len, 0);
Chris Masonb917b7c2009-09-18 16:07:03 -0400362}
363
364/**
Christoph Hellwig9d2423c2008-06-11 21:52:17 -0400365 * remove_extent_mapping - removes an extent_map from the extent tree
366 * @tree: extent tree to remove from
367 * @em: extent map beeing removed
368 *
369 * Removes @em from @tree. No reference counts are dropped, and no checks
370 * are done to see if the range is in use
Chris Masona52d9a82007-08-27 16:49:44 -0400371 */
372int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
373{
Chris Masond1310b22008-01-24 16:13:08 -0500374 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400375
Chris Mason7f3c74f2008-07-18 12:01:11 -0400376 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
Chris Masond1310b22008-01-24 16:13:08 -0500377 rb_erase(&em->rb_node, &tree->map);
378 em->in_tree = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400379 return ret;
380}