Skip to content

Commit 4eb150d

Browse files
adam900710kdave
authored andcommitted
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: Qu Wenruo <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 8f8aa4c commit 4eb150d

File tree

5 files changed

+34
-27
lines changed

5 files changed

+34
-27
lines changed

fs/btrfs/backref.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,11 +789,13 @@ static int add_missing_keys(struct btrfs_fs_info *fs_info,
789789
if (IS_ERR(eb)) {
790790
free_pref(ref);
791791
return PTR_ERR(eb);
792-
} else if (!extent_buffer_uptodate(eb)) {
792+
}
793+
if (!extent_buffer_uptodate(eb)) {
793794
free_pref(ref);
794795
free_extent_buffer(eb);
795796
return -EIO;
796797
}
798+
797799
if (lock)
798800
btrfs_tree_read_lock(eb);
799801
if (btrfs_header_level(eb) == 0)
@@ -1335,7 +1337,8 @@ static int find_parent_nodes(struct btrfs_trans_handle *trans,
13351337
if (IS_ERR(eb)) {
13361338
ret = PTR_ERR(eb);
13371339
goto out;
1338-
} else if (!extent_buffer_uptodate(eb)) {
1340+
}
1341+
if (!extent_buffer_uptodate(eb)) {
13391342
free_extent_buffer(eb);
13401343
ret = -EIO;
13411344
goto out;

fs/btrfs/ctree.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -846,9 +846,11 @@ struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
846846
btrfs_header_owner(parent),
847847
btrfs_node_ptr_generation(parent, slot),
848848
level - 1, &first_key);
849-
if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
849+
if (IS_ERR(eb))
850+
return eb;
851+
if (!extent_buffer_uptodate(eb)) {
850852
free_extent_buffer(eb);
851-
eb = ERR_PTR(-EIO);
853+
return ERR_PTR(-EIO);
852854
}
853855

854856
return eb;
@@ -1460,19 +1462,19 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
14601462
ret = -EAGAIN;
14611463
tmp = read_tree_block(fs_info, blocknr, root->root_key.objectid,
14621464
gen, parent_level - 1, &first_key);
1463-
if (!IS_ERR(tmp)) {
1464-
/*
1465-
* If the read above didn't mark this buffer up to date,
1466-
* it will never end up being up to date. Set ret to EIO now
1467-
* and give up so that our caller doesn't loop forever
1468-
* on our EAGAINs.
1469-
*/
1470-
if (!extent_buffer_uptodate(tmp))
1471-
ret = -EIO;
1472-
free_extent_buffer(tmp);
1473-
} else {
1474-
ret = PTR_ERR(tmp);
1465+
if (IS_ERR(tmp)) {
1466+
btrfs_release_path(p);
1467+
return PTR_ERR(tmp);
14751468
}
1469+
/*
1470+
* If the read above didn't mark this buffer up to date,
1471+
* it will never end up being up to date. Set ret to EIO now
1472+
* and give up so that our caller doesn't loop forever
1473+
* on our EAGAINs.
1474+
*/
1475+
if (!extent_buffer_uptodate(tmp))
1476+
ret = -EIO;
1477+
free_extent_buffer(tmp);
14761478

14771479
btrfs_release_path(p);
14781480
return ret;

fs/btrfs/disk-io.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,7 +1543,8 @@ static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root,
15431543
ret = PTR_ERR(root->node);
15441544
root->node = NULL;
15451545
goto fail;
1546-
} else if (!btrfs_buffer_uptodate(root->node, generation, 0)) {
1546+
}
1547+
if (!btrfs_buffer_uptodate(root->node, generation, 0)) {
15471548
ret = -EIO;
15481549
goto fail;
15491550
}
@@ -2537,11 +2538,13 @@ static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
25372538
log_tree_root->node = NULL;
25382539
btrfs_put_root(log_tree_root);
25392540
return ret;
2540-
} else if (!extent_buffer_uptodate(log_tree_root->node)) {
2541+
}
2542+
if (!extent_buffer_uptodate(log_tree_root->node)) {
25412543
btrfs_err(fs_info, "failed to read log tree");
25422544
btrfs_put_root(log_tree_root);
25432545
return -EIO;
25442546
}
2547+
25452548
/* returns with log_tree_root freed on success */
25462549
ret = btrfs_recover_log_trees(log_tree_root);
25472550
if (ret) {
@@ -2983,15 +2986,14 @@ static int load_super_root(struct btrfs_root *root, u64 bytenr, u64 gen, int lev
29832986
if (IS_ERR(root->node)) {
29842987
ret = PTR_ERR(root->node);
29852988
root->node = NULL;
2986-
} else if (!extent_buffer_uptodate(root->node)) {
2989+
return ret;
2990+
}
2991+
if (!extent_buffer_uptodate(root->node)) {
29872992
free_extent_buffer(root->node);
29882993
root->node = NULL;
2989-
ret = -EIO;
2994+
return -EIO;
29902995
}
29912996

2992-
if (ret)
2993-
return ret;
2994-
29952997
btrfs_set_root_node(&root->root_item, root->node);
29962998
root->commit_root = btrfs_root_node(root);
29972999
btrfs_set_root_refs(&root->root_item, 1);

fs/btrfs/print-tree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,9 @@ void btrfs_print_tree(struct extent_buffer *c, bool follow)
392392
btrfs_header_owner(c),
393393
btrfs_node_ptr_generation(c, i),
394394
level - 1, &first_key);
395-
if (IS_ERR(next)) {
395+
if (IS_ERR(next))
396396
continue;
397-
} else if (!extent_buffer_uptodate(next)) {
397+
if (!extent_buffer_uptodate(next)) {
398398
free_extent_buffer(next);
399399
continue;
400400
}

fs/btrfs/relocation.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2599,9 +2599,9 @@ static int get_tree_block_key(struct btrfs_fs_info *fs_info,
25992599

26002600
eb = read_tree_block(fs_info, block->bytenr, block->owner,
26012601
block->key.offset, block->level, NULL);
2602-
if (IS_ERR(eb)) {
2602+
if (IS_ERR(eb))
26032603
return PTR_ERR(eb);
2604-
} else if (!extent_buffer_uptodate(eb)) {
2604+
if (!extent_buffer_uptodate(eb)) {
26052605
free_extent_buffer(eb);
26062606
return -EIO;
26072607
}

0 commit comments

Comments
 (0)