Commit 4eb150d6 authored by Qu Wenruo's avatar Qu Wenruo Committed by David Sterba

btrfs: unify the error handling pattern for read_tree_block()

We had an error handling pattern for read_tree_block() like this:

	eb = read_tree_block();
	if (IS_ERR(eb)) {
		/*
		 * Handling error here
		 * Normally ended up with return or goto out.
		 */
	} else if (!extent_buffer_uptodate(eb)) {
		/*
		 * Different error handling here
		 * Normally also ended up with return or goto out;
		 */
	}

This is fine, but if we want to add extra check for each
read_tree_block(), the existing if-else-if is not that expandable and
will take reader some seconds to figure out there is no extra branch.

Here we change it to a more common way, without the extra else:

	eb = read_tree_block();
	if (IS_ERR(eb)) {
		/*
		 * Handling error here
		 */
		return eb or goto out;
	}
	if (!extent_buffer_uptodate(eb)) {
		/*
		 * Different error handling here
		 */
		return eb or goto out;
	}

This also removes some oddball call sites which uses some creative way
to check error.
Signed-off-by: default avatarQu Wenruo <wqu@suse.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 8f8aa4c7
...@@ -789,11 +789,13 @@ static int add_missing_keys(struct btrfs_fs_info *fs_info, ...@@ -789,11 +789,13 @@ static int add_missing_keys(struct btrfs_fs_info *fs_info,
if (IS_ERR(eb)) { if (IS_ERR(eb)) {
free_pref(ref); free_pref(ref);
return PTR_ERR(eb); return PTR_ERR(eb);
} else if (!extent_buffer_uptodate(eb)) { }
if (!extent_buffer_uptodate(eb)) {
free_pref(ref); free_pref(ref);
free_extent_buffer(eb); free_extent_buffer(eb);
return -EIO; return -EIO;
} }
if (lock) if (lock)
btrfs_tree_read_lock(eb); btrfs_tree_read_lock(eb);
if (btrfs_header_level(eb) == 0) if (btrfs_header_level(eb) == 0)
...@@ -1335,7 +1337,8 @@ static int find_parent_nodes(struct btrfs_trans_handle *trans, ...@@ -1335,7 +1337,8 @@ static int find_parent_nodes(struct btrfs_trans_handle *trans,
if (IS_ERR(eb)) { if (IS_ERR(eb)) {
ret = PTR_ERR(eb); ret = PTR_ERR(eb);
goto out; goto out;
} else if (!extent_buffer_uptodate(eb)) { }
if (!extent_buffer_uptodate(eb)) {
free_extent_buffer(eb); free_extent_buffer(eb);
ret = -EIO; ret = -EIO;
goto out; goto out;
......
...@@ -846,9 +846,11 @@ struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent, ...@@ -846,9 +846,11 @@ struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
btrfs_header_owner(parent), btrfs_header_owner(parent),
btrfs_node_ptr_generation(parent, slot), btrfs_node_ptr_generation(parent, slot),
level - 1, &first_key); level - 1, &first_key);
if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) { if (IS_ERR(eb))
return eb;
if (!extent_buffer_uptodate(eb)) {
free_extent_buffer(eb); free_extent_buffer(eb);
eb = ERR_PTR(-EIO); return ERR_PTR(-EIO);
} }
return eb; return eb;
...@@ -1460,19 +1462,19 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p, ...@@ -1460,19 +1462,19 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
ret = -EAGAIN; ret = -EAGAIN;
tmp = read_tree_block(fs_info, blocknr, root->root_key.objectid, tmp = read_tree_block(fs_info, blocknr, root->root_key.objectid,
gen, parent_level - 1, &first_key); gen, parent_level - 1, &first_key);
if (!IS_ERR(tmp)) { if (IS_ERR(tmp)) {
/* btrfs_release_path(p);
* If the read above didn't mark this buffer up to date, return PTR_ERR(tmp);
* it will never end up being up to date. Set ret to EIO now
* and give up so that our caller doesn't loop forever
* on our EAGAINs.
*/
if (!extent_buffer_uptodate(tmp))
ret = -EIO;
free_extent_buffer(tmp);
} else {
ret = PTR_ERR(tmp);
} }
/*
* If the read above didn't mark this buffer up to date,
* it will never end up being up to date. Set ret to EIO now
* and give up so that our caller doesn't loop forever
* on our EAGAINs.
*/
if (!extent_buffer_uptodate(tmp))
ret = -EIO;
free_extent_buffer(tmp);
btrfs_release_path(p); btrfs_release_path(p);
return ret; return ret;
......
...@@ -1543,7 +1543,8 @@ static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root, ...@@ -1543,7 +1543,8 @@ static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root,
ret = PTR_ERR(root->node); ret = PTR_ERR(root->node);
root->node = NULL; root->node = NULL;
goto fail; goto fail;
} else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { }
if (!btrfs_buffer_uptodate(root->node, generation, 0)) {
ret = -EIO; ret = -EIO;
goto fail; goto fail;
} }
...@@ -2537,11 +2538,13 @@ static int btrfs_replay_log(struct btrfs_fs_info *fs_info, ...@@ -2537,11 +2538,13 @@ static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
log_tree_root->node = NULL; log_tree_root->node = NULL;
btrfs_put_root(log_tree_root); btrfs_put_root(log_tree_root);
return ret; return ret;
} else if (!extent_buffer_uptodate(log_tree_root->node)) { }
if (!extent_buffer_uptodate(log_tree_root->node)) {
btrfs_err(fs_info, "failed to read log tree"); btrfs_err(fs_info, "failed to read log tree");
btrfs_put_root(log_tree_root); btrfs_put_root(log_tree_root);
return -EIO; return -EIO;
} }
/* returns with log_tree_root freed on success */ /* returns with log_tree_root freed on success */
ret = btrfs_recover_log_trees(log_tree_root); ret = btrfs_recover_log_trees(log_tree_root);
if (ret) { if (ret) {
...@@ -2983,15 +2986,14 @@ static int load_super_root(struct btrfs_root *root, u64 bytenr, u64 gen, int lev ...@@ -2983,15 +2986,14 @@ static int load_super_root(struct btrfs_root *root, u64 bytenr, u64 gen, int lev
if (IS_ERR(root->node)) { if (IS_ERR(root->node)) {
ret = PTR_ERR(root->node); ret = PTR_ERR(root->node);
root->node = NULL; root->node = NULL;
} else if (!extent_buffer_uptodate(root->node)) { return ret;
}
if (!extent_buffer_uptodate(root->node)) {
free_extent_buffer(root->node); free_extent_buffer(root->node);
root->node = NULL; root->node = NULL;
ret = -EIO; return -EIO;
} }
if (ret)
return ret;
btrfs_set_root_node(&root->root_item, root->node); btrfs_set_root_node(&root->root_item, root->node);
root->commit_root = btrfs_root_node(root); root->commit_root = btrfs_root_node(root);
btrfs_set_root_refs(&root->root_item, 1); btrfs_set_root_refs(&root->root_item, 1);
......
...@@ -392,9 +392,9 @@ void btrfs_print_tree(struct extent_buffer *c, bool follow) ...@@ -392,9 +392,9 @@ void btrfs_print_tree(struct extent_buffer *c, bool follow)
btrfs_header_owner(c), btrfs_header_owner(c),
btrfs_node_ptr_generation(c, i), btrfs_node_ptr_generation(c, i),
level - 1, &first_key); level - 1, &first_key);
if (IS_ERR(next)) { if (IS_ERR(next))
continue; continue;
} else if (!extent_buffer_uptodate(next)) { if (!extent_buffer_uptodate(next)) {
free_extent_buffer(next); free_extent_buffer(next);
continue; continue;
} }
......
...@@ -2599,9 +2599,9 @@ static int get_tree_block_key(struct btrfs_fs_info *fs_info, ...@@ -2599,9 +2599,9 @@ static int get_tree_block_key(struct btrfs_fs_info *fs_info,
eb = read_tree_block(fs_info, block->bytenr, block->owner, eb = read_tree_block(fs_info, block->bytenr, block->owner,
block->key.offset, block->level, NULL); block->key.offset, block->level, NULL);
if (IS_ERR(eb)) { if (IS_ERR(eb))
return PTR_ERR(eb); return PTR_ERR(eb);
} else if (!extent_buffer_uptodate(eb)) { if (!extent_buffer_uptodate(eb)) {
free_extent_buffer(eb); free_extent_buffer(eb);
return -EIO; return -EIO;
} }
......
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