Commit e6138876 authored by Josef Bacik's avatar Josef Bacik Committed by Chris Mason

Btrfs: cache extent state when writing out dirty metadata pages

Everytime we write out dirty pages we search for an offset in the tree,
convert the bits in the state, and then when we wait we search for the
offset again and clear the bits.  So for every dirty range in the io tree we
are doing 4 rb searches, which is suboptimal.  With this patch we are only
doing 2 searches for every cycle (modulo weird things happening).  Thanks,
Signed-off-by: default avatarJosef Bacik <jbacik@fusionio.com>
parent ce195332
...@@ -3572,7 +3572,7 @@ static int btrfs_destroy_marked_extents(struct btrfs_root *root, ...@@ -3572,7 +3572,7 @@ static int btrfs_destroy_marked_extents(struct btrfs_root *root,
while (1) { while (1) {
ret = find_first_extent_bit(dirty_pages, start, &start, &end, ret = find_first_extent_bit(dirty_pages, start, &start, &end,
mark); mark, NULL);
if (ret) if (ret)
break; break;
...@@ -3627,7 +3627,7 @@ static int btrfs_destroy_pinned_extent(struct btrfs_root *root, ...@@ -3627,7 +3627,7 @@ static int btrfs_destroy_pinned_extent(struct btrfs_root *root,
again: again:
while (1) { while (1) {
ret = find_first_extent_bit(unpin, 0, &start, &end, ret = find_first_extent_bit(unpin, 0, &start, &end,
EXTENT_DIRTY); EXTENT_DIRTY, NULL);
if (ret) if (ret)
break; break;
......
...@@ -312,7 +312,8 @@ static u64 add_new_free_space(struct btrfs_block_group_cache *block_group, ...@@ -312,7 +312,8 @@ static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
while (start < end) { while (start < end) {
ret = find_first_extent_bit(info->pinned_extents, start, ret = find_first_extent_bit(info->pinned_extents, start,
&extent_start, &extent_end, &extent_start, &extent_end,
EXTENT_DIRTY | EXTENT_UPTODATE); EXTENT_DIRTY | EXTENT_UPTODATE,
NULL);
if (ret) if (ret)
break; break;
...@@ -5045,7 +5046,7 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, ...@@ -5045,7 +5046,7 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
while (1) { while (1) {
ret = find_first_extent_bit(unpin, 0, &start, &end, ret = find_first_extent_bit(unpin, 0, &start, &end,
EXTENT_DIRTY); EXTENT_DIRTY, NULL);
if (ret) if (ret)
break; break;
......
...@@ -937,6 +937,7 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits, ...@@ -937,6 +937,7 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
* @end: the end offset in bytes (inclusive) * @end: the end offset in bytes (inclusive)
* @bits: the bits to set in this range * @bits: the bits to set in this range
* @clear_bits: the bits to clear in this range * @clear_bits: the bits to clear in this range
* @cached_state: state that we're going to cache
* @mask: the allocation mask * @mask: the allocation mask
* *
* This will go through and set bits for the given range. If any states exist * This will go through and set bits for the given range. If any states exist
...@@ -946,7 +947,8 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits, ...@@ -946,7 +947,8 @@ int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
* boundary bits like LOCK. * boundary bits like LOCK.
*/ */
int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
int bits, int clear_bits, gfp_t mask) int bits, int clear_bits,
struct extent_state **cached_state, gfp_t mask)
{ {
struct extent_state *state; struct extent_state *state;
struct extent_state *prealloc = NULL; struct extent_state *prealloc = NULL;
...@@ -963,6 +965,15 @@ int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, ...@@ -963,6 +965,15 @@ int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
} }
spin_lock(&tree->lock); spin_lock(&tree->lock);
if (cached_state && *cached_state) {
state = *cached_state;
if (state->start <= start && state->end > start &&
state->tree) {
node = &state->rb_node;
goto hit_next;
}
}
/* /*
* this search will find all the extents that end after * this search will find all the extents that end after
* our range starts. * our range starts.
...@@ -993,6 +1004,7 @@ int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, ...@@ -993,6 +1004,7 @@ int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
*/ */
if (state->start == start && state->end <= end) { if (state->start == start && state->end <= end) {
set_state_bits(tree, state, &bits); set_state_bits(tree, state, &bits);
cache_state(state, cached_state);
state = clear_state_bit(tree, state, &clear_bits, 0); state = clear_state_bit(tree, state, &clear_bits, 0);
if (last_end == (u64)-1) if (last_end == (u64)-1)
goto out; goto out;
...@@ -1033,6 +1045,7 @@ int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, ...@@ -1033,6 +1045,7 @@ int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
goto out; goto out;
if (state->end <= end) { if (state->end <= end) {
set_state_bits(tree, state, &bits); set_state_bits(tree, state, &bits);
cache_state(state, cached_state);
state = clear_state_bit(tree, state, &clear_bits, 0); state = clear_state_bit(tree, state, &clear_bits, 0);
if (last_end == (u64)-1) if (last_end == (u64)-1)
goto out; goto out;
...@@ -1071,6 +1084,7 @@ int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, ...@@ -1071,6 +1084,7 @@ int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
&bits); &bits);
if (err) if (err)
extent_io_tree_panic(tree, err); extent_io_tree_panic(tree, err);
cache_state(prealloc, cached_state);
prealloc = NULL; prealloc = NULL;
start = this_end + 1; start = this_end + 1;
goto search_again; goto search_again;
...@@ -1093,6 +1107,7 @@ int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, ...@@ -1093,6 +1107,7 @@ int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
extent_io_tree_panic(tree, err); extent_io_tree_panic(tree, err);
set_state_bits(tree, prealloc, &bits); set_state_bits(tree, prealloc, &bits);
cache_state(prealloc, cached_state);
clear_state_bit(tree, prealloc, &clear_bits, 0); clear_state_bit(tree, prealloc, &clear_bits, 0);
prealloc = NULL; prealloc = NULL;
goto out; goto out;
...@@ -1297,18 +1312,42 @@ struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree, ...@@ -1297,18 +1312,42 @@ struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
* If nothing was found, 1 is returned. If found something, return 0. * If nothing was found, 1 is returned. If found something, return 0.
*/ */
int find_first_extent_bit(struct extent_io_tree *tree, u64 start, int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
u64 *start_ret, u64 *end_ret, int bits) u64 *start_ret, u64 *end_ret, int bits,
struct extent_state **cached_state)
{ {
struct extent_state *state; struct extent_state *state;
struct rb_node *n;
int ret = 1; int ret = 1;
spin_lock(&tree->lock); spin_lock(&tree->lock);
if (cached_state && *cached_state) {
state = *cached_state;
if (state->end == start - 1 && state->tree) {
n = rb_next(&state->rb_node);
while (n) {
state = rb_entry(n, struct extent_state,
rb_node);
if (state->state & bits)
goto got_it;
n = rb_next(n);
}
free_extent_state(*cached_state);
*cached_state = NULL;
goto out;
}
free_extent_state(*cached_state);
*cached_state = NULL;
}
state = find_first_extent_bit_state(tree, start, bits); state = find_first_extent_bit_state(tree, start, bits);
got_it:
if (state) { if (state) {
cache_state(state, cached_state);
*start_ret = state->start; *start_ret = state->start;
*end_ret = state->end; *end_ret = state->end;
ret = 0; ret = 0;
} }
out:
spin_unlock(&tree->lock); spin_unlock(&tree->lock);
return ret; return ret;
} }
......
...@@ -233,13 +233,15 @@ int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, ...@@ -233,13 +233,15 @@ int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
gfp_t mask); gfp_t mask);
int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
int bits, int clear_bits, gfp_t mask); int bits, int clear_bits,
struct extent_state **cached_state, gfp_t mask);
int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end, int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
struct extent_state **cached_state, gfp_t mask); struct extent_state **cached_state, gfp_t mask);
int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end, int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
struct extent_state **cached_state, gfp_t mask); struct extent_state **cached_state, gfp_t mask);
int find_first_extent_bit(struct extent_io_tree *tree, u64 start, int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
u64 *start_ret, u64 *end_ret, int bits); u64 *start_ret, u64 *end_ret, int bits,
struct extent_state **cached_state);
struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree, struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
u64 start, int bits); u64 start, int bits);
int extent_invalidatepage(struct extent_io_tree *tree, int extent_invalidatepage(struct extent_io_tree *tree,
......
...@@ -966,7 +966,7 @@ int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode, ...@@ -966,7 +966,7 @@ int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
block_group->key.offset)) { block_group->key.offset)) {
ret = find_first_extent_bit(unpin, start, ret = find_first_extent_bit(unpin, start,
&extent_start, &extent_end, &extent_start, &extent_end,
EXTENT_DIRTY); EXTENT_DIRTY, NULL);
if (ret) { if (ret) {
ret = 0; ret = 0;
break; break;
......
...@@ -3621,7 +3621,7 @@ int find_next_extent(struct btrfs_trans_handle *trans, ...@@ -3621,7 +3621,7 @@ int find_next_extent(struct btrfs_trans_handle *trans,
ret = find_first_extent_bit(&rc->processed_blocks, ret = find_first_extent_bit(&rc->processed_blocks,
key.objectid, &start, &end, key.objectid, &start, &end,
EXTENT_DIRTY); EXTENT_DIRTY, NULL);
if (ret == 0 && start <= key.objectid) { if (ret == 0 && start <= key.objectid) {
btrfs_release_path(path); btrfs_release_path(path);
......
...@@ -687,13 +687,15 @@ int btrfs_write_marked_extents(struct btrfs_root *root, ...@@ -687,13 +687,15 @@ int btrfs_write_marked_extents(struct btrfs_root *root,
int err = 0; int err = 0;
int werr = 0; int werr = 0;
struct address_space *mapping = root->fs_info->btree_inode->i_mapping; struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
struct extent_state *cached_state = NULL;
u64 start = 0; u64 start = 0;
u64 end; u64 end;
while (!find_first_extent_bit(dirty_pages, start, &start, &end, while (!find_first_extent_bit(dirty_pages, start, &start, &end,
mark)) { mark, &cached_state)) {
convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT, mark, convert_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
GFP_NOFS); mark, &cached_state, GFP_NOFS);
cached_state = NULL;
err = filemap_fdatawrite_range(mapping, start, end); err = filemap_fdatawrite_range(mapping, start, end);
if (err) if (err)
werr = err; werr = err;
...@@ -717,12 +719,14 @@ int btrfs_wait_marked_extents(struct btrfs_root *root, ...@@ -717,12 +719,14 @@ int btrfs_wait_marked_extents(struct btrfs_root *root,
int err = 0; int err = 0;
int werr = 0; int werr = 0;
struct address_space *mapping = root->fs_info->btree_inode->i_mapping; struct address_space *mapping = root->fs_info->btree_inode->i_mapping;
struct extent_state *cached_state = NULL;
u64 start = 0; u64 start = 0;
u64 end; u64 end;
while (!find_first_extent_bit(dirty_pages, start, &start, &end, while (!find_first_extent_bit(dirty_pages, start, &start, &end,
EXTENT_NEED_WAIT)) { EXTENT_NEED_WAIT, &cached_state)) {
clear_extent_bits(dirty_pages, start, end, EXTENT_NEED_WAIT, GFP_NOFS); clear_extent_bit(dirty_pages, start, end, EXTENT_NEED_WAIT,
0, 0, &cached_state, GFP_NOFS);
err = filemap_fdatawait_range(mapping, start, end); err = filemap_fdatawait_range(mapping, start, end);
if (err) if (err)
werr = err; werr = err;
......
...@@ -2463,7 +2463,8 @@ static void free_log_tree(struct btrfs_trans_handle *trans, ...@@ -2463,7 +2463,8 @@ static void free_log_tree(struct btrfs_trans_handle *trans,
while (1) { while (1) {
ret = find_first_extent_bit(&log->dirty_log_pages, ret = find_first_extent_bit(&log->dirty_log_pages,
0, &start, &end, EXTENT_DIRTY | EXTENT_NEW); 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW,
NULL);
if (ret) if (ret)
break; break;
......
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