Commit 43b068ca authored by Josef Bacik's avatar Josef Bacik Committed by David Sterba

btrfs: make tree_search_prev_next return extent_state's

Instead of doing the rb_entry again once we return from this function,
simply return the actual states themselves, and then clean up the only
user of this helper to handle states instead of nodes.
Signed-off-by: default avatarJosef Bacik <josef@toxicpanda.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent e349fd3b
...@@ -204,6 +204,16 @@ static inline struct extent_state *next_state(struct extent_state *state) ...@@ -204,6 +204,16 @@ static inline struct extent_state *next_state(struct extent_state *state)
return NULL; return NULL;
} }
static inline struct extent_state *prev_state(struct extent_state *state)
{
struct rb_node *next = rb_prev(&state->rb_node);
if (next)
return rb_entry(next, struct extent_state, rb_node);
else
return NULL;
}
/* /*
* Search @tree for an entry that contains @offset. Such entry would have * Search @tree for an entry that contains @offset. Such entry would have
* entry->start <= offset && entry->end >= offset. * entry->start <= offset && entry->end >= offset.
...@@ -267,46 +277,39 @@ static inline struct extent_state *tree_search_for_insert(struct extent_io_tree ...@@ -267,46 +277,39 @@ static inline struct extent_state *tree_search_for_insert(struct extent_io_tree
* such entry exists, then return NULL and fill @prev_ret and @next_ret. * such entry exists, then return NULL and fill @prev_ret and @next_ret.
* Otherwise return the found entry and other pointers are left untouched. * Otherwise return the found entry and other pointers are left untouched.
*/ */
static inline struct rb_node *tree_search_prev_next(struct extent_io_tree *tree, static struct extent_state *tree_search_prev_next(struct extent_io_tree *tree,
u64 offset, u64 offset,
struct rb_node **prev_ret, struct extent_state **prev_ret,
struct rb_node **next_ret) struct extent_state **next_ret)
{ {
struct rb_root *root = &tree->state; struct rb_root *root = &tree->state;
struct rb_node **node = &root->rb_node; struct rb_node **node = &root->rb_node;
struct rb_node *prev = NULL; struct extent_state *orig_prev;
struct rb_node *orig_prev = NULL; struct extent_state *entry = NULL;
struct extent_state *entry;
ASSERT(prev_ret); ASSERT(prev_ret);
ASSERT(next_ret); ASSERT(next_ret);
while (*node) { while (*node) {
prev = *node; entry = rb_entry(*node, struct extent_state, rb_node);
entry = rb_entry(prev, struct extent_state, rb_node);
if (offset < entry->start) if (offset < entry->start)
node = &(*node)->rb_left; node = &(*node)->rb_left;
else if (offset > entry->end) else if (offset > entry->end)
node = &(*node)->rb_right; node = &(*node)->rb_right;
else else
return *node; return entry;
} }
orig_prev = prev; orig_prev = entry;
while (prev && offset > entry->end) { while (entry && offset > entry->end)
prev = rb_next(prev); entry = next_state(entry);
entry = rb_entry(prev, struct extent_state, rb_node); *next_ret = entry;
} entry = orig_prev;
*next_ret = prev;
prev = orig_prev;
entry = rb_entry(prev, struct extent_state, rb_node); while (entry && offset < entry->start)
while (prev && offset < entry->start) { entry = prev_state(entry);
prev = rb_prev(prev); *prev_ret = entry;
entry = rb_entry(prev, struct extent_state, rb_node);
}
*prev_ret = prev;
return NULL; return NULL;
} }
...@@ -1404,14 +1407,14 @@ void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start, ...@@ -1404,14 +1407,14 @@ void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
u64 *start_ret, u64 *end_ret, u32 bits) u64 *start_ret, u64 *end_ret, u32 bits)
{ {
struct extent_state *state; struct extent_state *state;
struct rb_node *node, *prev = NULL, *next; struct extent_state *prev = NULL, *next;
spin_lock(&tree->lock); spin_lock(&tree->lock);
/* Find first extent with bits cleared */ /* Find first extent with bits cleared */
while (1) { while (1) {
node = tree_search_prev_next(tree, start, &prev, &next); state = tree_search_prev_next(tree, start, &prev, &next);
if (!node && !next && !prev) { if (!state && !next && !prev) {
/* /*
* Tree is completely empty, send full range and let * Tree is completely empty, send full range and let
* caller deal with it * caller deal with it
...@@ -1419,24 +1422,22 @@ void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start, ...@@ -1419,24 +1422,22 @@ void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
*start_ret = 0; *start_ret = 0;
*end_ret = -1; *end_ret = -1;
goto out; goto out;
} else if (!node && !next) { } else if (!state && !next) {
/* /*
* We are past the last allocated chunk, set start at * We are past the last allocated chunk, set start at
* the end of the last extent. * the end of the last extent.
*/ */
state = rb_entry(prev, struct extent_state, rb_node); *start_ret = prev->end + 1;
*start_ret = state->end + 1;
*end_ret = -1; *end_ret = -1;
goto out; goto out;
} else if (!node) { } else if (!state) {
node = next; state = next;
} }
/* /*
* At this point 'node' either contains 'start' or start is * At this point 'state' either contains 'start' or start is
* before 'node' * before 'state'
*/ */
state = rb_entry(node, struct extent_state, rb_node);
if (in_range(start, state->start, state->end - state->start + 1)) { if (in_range(start, state->start, state->end - state->start + 1)) {
if (state->state & bits) { if (state->state & bits) {
/* /*
...@@ -1470,13 +1471,10 @@ void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start, ...@@ -1470,13 +1471,10 @@ void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
* 0 | * 0 |
* start * start
*/ */
if (prev) { if (prev)
state = rb_entry(prev, struct extent_state, *start_ret = prev->end + 1;
rb_node); else
*start_ret = state->end + 1;
} else {
*start_ret = 0; *start_ret = 0;
}
break; break;
} }
} }
...@@ -1485,7 +1483,6 @@ void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start, ...@@ -1485,7 +1483,6 @@ void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
* Find the longest stretch from start until an entry which has the * Find the longest stretch from start until an entry which has the
* bits set * bits set
*/ */
state = rb_entry(node, struct extent_state, rb_node);
while (state) { while (state) {
if (state->end >= start && !(state->state & bits)) { if (state->end >= start && !(state->state & bits)) {
*end_ret = state->end; *end_ret = state->end;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment