Commit ccaeff92 authored by Josef Bacik's avatar Josef Bacik Committed by David Sterba

btrfs: use next_state instead of rb_next where we can

We can simplify a lot of these functions where we have to cycle through
extent_state's by simply using next_state() instead of rb_next().  In
many spots this allows us to do things like

while (state) {
	/* whatever */
	state = next_state(state);
}

instead of

while (1) {
	state = rb_entry(n, struct extent_state, rb_node);
	n = rb_next(n);
	if (!n)
		break;
}
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 071d19f5
...@@ -748,12 +748,10 @@ void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits) ...@@ -748,12 +748,10 @@ void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits)
* range starts. * range starts.
*/ */
node = tree_search(tree, start); node = tree_search(tree, start);
process_node:
if (!node) if (!node)
break; break;
state = rb_entry(node, struct extent_state, rb_node); state = rb_entry(node, struct extent_state, rb_node);
process_node:
if (state->start > end) if (state->start > end)
goto out; goto out;
...@@ -770,7 +768,7 @@ void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits) ...@@ -770,7 +768,7 @@ void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, u32 bits)
break; break;
if (!cond_resched_lock(&tree->lock)) { if (!cond_resched_lock(&tree->lock)) {
node = rb_next(node); state = next_state(state);
goto process_node; goto process_node;
} }
} }
...@@ -815,15 +813,13 @@ static struct extent_state *find_first_extent_bit_state(struct extent_io_tree *t ...@@ -815,15 +813,13 @@ static struct extent_state *find_first_extent_bit_state(struct extent_io_tree *t
node = tree_search(tree, start); node = tree_search(tree, start);
if (!node) if (!node)
goto out; goto out;
state = rb_entry(node, struct extent_state, rb_node);
while (1) { while (state) {
state = rb_entry(node, struct extent_state, rb_node);
if (state->end >= start && (state->state & bits)) if (state->end >= start && (state->state & bits))
return state; return state;
node = rb_next(node); state = next_state(state);
if (!node)
break;
} }
out: out:
return NULL; return NULL;
...@@ -939,8 +935,8 @@ bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start, ...@@ -939,8 +935,8 @@ bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
goto out; goto out;
} }
while (1) { state = rb_entry(node, struct extent_state, rb_node);
state = rb_entry(node, struct extent_state, rb_node); while (state) {
if (found && (state->start != cur_start || if (found && (state->start != cur_start ||
(state->state & EXTENT_BOUNDARY))) { (state->state & EXTENT_BOUNDARY))) {
goto out; goto out;
...@@ -958,12 +954,10 @@ bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start, ...@@ -958,12 +954,10 @@ bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
found = true; found = true;
*end = state->end; *end = state->end;
cur_start = state->end + 1; cur_start = state->end + 1;
node = rb_next(node);
total_bytes += state->end - state->start + 1; total_bytes += state->end - state->start + 1;
if (total_bytes >= max_bytes) if (total_bytes >= max_bytes)
break; break;
if (!node) state = next_state(state);
break;
} }
out: out:
spin_unlock(&tree->lock); spin_unlock(&tree->lock);
...@@ -1514,18 +1508,15 @@ void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start, ...@@ -1514,18 +1508,15 @@ 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
*/ */
while (1) { state = rb_entry(node, struct extent_state, rb_node);
state = rb_entry(node, struct extent_state, rb_node); while (state) {
if (state->end >= start && !(state->state & bits)) { if (state->end >= start && !(state->state & bits)) {
*end_ret = state->end; *end_ret = state->end;
} else { } else {
*end_ret = state->start - 1; *end_ret = state->start - 1;
break; break;
} }
state = next_state(state);
node = rb_next(node);
if (!node)
break;
} }
out: out:
spin_unlock(&tree->lock); spin_unlock(&tree->lock);
...@@ -1563,8 +1554,8 @@ u64 count_range_bits(struct extent_io_tree *tree, ...@@ -1563,8 +1554,8 @@ u64 count_range_bits(struct extent_io_tree *tree,
if (!node) if (!node)
goto out; goto out;
while (1) { state = rb_entry(node, struct extent_state, rb_node);
state = rb_entry(node, struct extent_state, rb_node); while (state) {
if (state->start > search_end) if (state->start > search_end)
break; break;
if (contig && found && state->start > last + 1) if (contig && found && state->start > last + 1)
...@@ -1582,9 +1573,7 @@ u64 count_range_bits(struct extent_io_tree *tree, ...@@ -1582,9 +1573,7 @@ u64 count_range_bits(struct extent_io_tree *tree,
} else if (contig && found) { } else if (contig && found) {
break; break;
} }
node = rb_next(node); state = next_state(state);
if (!node)
break;
} }
out: out:
spin_unlock(&tree->lock); spin_unlock(&tree->lock);
...@@ -1609,9 +1598,11 @@ int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, ...@@ -1609,9 +1598,11 @@ int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
node = &cached->rb_node; node = &cached->rb_node;
else else
node = tree_search(tree, start); node = tree_search(tree, start);
while (node && start <= end) { if (!node)
state = rb_entry(node, struct extent_state, rb_node); goto out;
state = rb_entry(node, struct extent_state, rb_node);
while (state && start <= end) {
if (filled && state->start > start) { if (filled && state->start > start) {
bitset = 0; bitset = 0;
break; break;
...@@ -1635,13 +1626,13 @@ int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end, ...@@ -1635,13 +1626,13 @@ int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
start = state->end + 1; start = state->end + 1;
if (start > end) if (start > end)
break; break;
node = rb_next(node); state = next_state(state);
if (!node) {
if (filled)
bitset = 0;
break;
}
} }
/* We ran out of states and were still inside of our range. */
if (filled && !state)
bitset = 0;
out:
spin_unlock(&tree->lock); spin_unlock(&tree->lock);
return bitset; return bitset;
} }
......
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