Skip to content

Commit cc53bd2

Browse files
committed
btrfs: add unlikely annotations to branches leading to EIO
The unlikely() annotation is a static prediction hint that compiler may use to reorder code out of hot path. We use it elsewhere (namely tree-checker.c) for error branches that almost never happen, where EIO is one of them. Reviewed-by: Filipe Manana <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 9264d00 commit cc53bd2

19 files changed

+81
-83
lines changed

fs/btrfs/backref.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ static int add_missing_keys(struct btrfs_fs_info *fs_info,
859859
free_pref(ref);
860860
return PTR_ERR(eb);
861861
}
862-
if (!extent_buffer_uptodate(eb)) {
862+
if (unlikely(!extent_buffer_uptodate(eb))) {
863863
free_pref(ref);
864864
free_extent_buffer(eb);
865865
return -EIO;
@@ -1614,7 +1614,7 @@ static int find_parent_nodes(struct btrfs_backref_walk_ctx *ctx,
16141614
ret = PTR_ERR(eb);
16151615
goto out;
16161616
}
1617-
if (!extent_buffer_uptodate(eb)) {
1617+
if (unlikely(!extent_buffer_uptodate(eb))) {
16181618
free_extent_buffer(eb);
16191619
ret = -EIO;
16201620
goto out;

fs/btrfs/bio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,8 +849,8 @@ int btrfs_repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
849849
if (ret < 0)
850850
goto out_counter_dec;
851851

852-
if (!smap.dev->bdev ||
853-
!test_bit(BTRFS_DEV_STATE_WRITEABLE, &smap.dev->dev_state)) {
852+
if (unlikely(!smap.dev->bdev ||
853+
!test_bit(BTRFS_DEV_STATE_WRITEABLE, &smap.dev->dev_state))) {
854854
ret = -EIO;
855855
goto out_counter_dec;
856856
}

fs/btrfs/defrag.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ static struct folio *defrag_prepare_one_folio(struct btrfs_inode *inode, pgoff_t
924924
folio_put(folio);
925925
goto again;
926926
}
927-
if (!folio_test_uptodate(folio)) {
927+
if (unlikely(!folio_test_uptodate(folio))) {
928928
folio_unlock(folio);
929929
folio_put(folio);
930930
return ERR_PTR(-EIO);

fs/btrfs/dev-replace.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,15 @@ int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
177177
* allow 'btrfs dev replace_cancel' if src/tgt device is
178178
* missing
179179
*/
180-
if (!dev_replace->srcdev &&
181-
!btrfs_test_opt(fs_info, DEGRADED)) {
180+
if (unlikely(!dev_replace->srcdev && !btrfs_test_opt(fs_info, DEGRADED))) {
182181
ret = -EIO;
183182
btrfs_warn(fs_info,
184183
"cannot mount because device replace operation is ongoing and");
185184
btrfs_warn(fs_info,
186185
"srcdev (devid %llu) is missing, need to run 'btrfs dev scan'?",
187186
src_devid);
188187
}
189-
if (!dev_replace->tgtdev &&
190-
!btrfs_test_opt(fs_info, DEGRADED)) {
188+
if (unlikely(!dev_replace->tgtdev && !btrfs_test_opt(fs_info, DEGRADED))) {
191189
ret = -EIO;
192190
btrfs_warn(fs_info,
193191
"cannot mount because device replace operation is ongoing and");

fs/btrfs/disk-io.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -370,21 +370,21 @@ int btrfs_validate_extent_buffer(struct extent_buffer *eb,
370370
ASSERT(check);
371371

372372
found_start = btrfs_header_bytenr(eb);
373-
if (found_start != eb->start) {
373+
if (unlikely(found_start != eb->start)) {
374374
btrfs_err_rl(fs_info,
375375
"bad tree block start, mirror %u want %llu have %llu",
376376
eb->read_mirror, eb->start, found_start);
377377
ret = -EIO;
378378
goto out;
379379
}
380-
if (check_tree_block_fsid(eb)) {
380+
if (unlikely(check_tree_block_fsid(eb))) {
381381
btrfs_err_rl(fs_info, "bad fsid on logical %llu mirror %u",
382382
eb->start, eb->read_mirror);
383383
ret = -EIO;
384384
goto out;
385385
}
386386
found_level = btrfs_header_level(eb);
387-
if (found_level >= BTRFS_MAX_LEVEL) {
387+
if (unlikely(found_level >= BTRFS_MAX_LEVEL)) {
388388
btrfs_err(fs_info,
389389
"bad tree block level, mirror %u level %d on logical %llu",
390390
eb->read_mirror, btrfs_header_level(eb), eb->start);
@@ -410,7 +410,7 @@ int btrfs_validate_extent_buffer(struct extent_buffer *eb,
410410
}
411411
}
412412

413-
if (found_level != check->level) {
413+
if (unlikely(found_level != check->level)) {
414414
btrfs_err(fs_info,
415415
"level verify failed on logical %llu mirror %u wanted %u found %u",
416416
eb->start, eb->read_mirror, check->level, found_level);
@@ -1046,7 +1046,7 @@ static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root,
10461046
root->node = NULL;
10471047
goto fail;
10481048
}
1049-
if (!btrfs_buffer_uptodate(root->node, generation, false)) {
1049+
if (unlikely(!btrfs_buffer_uptodate(root->node, generation, false))) {
10501050
ret = -EIO;
10511051
goto fail;
10521052
}
@@ -2058,7 +2058,7 @@ static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
20582058
u64 bytenr = btrfs_super_log_root(disk_super);
20592059
int level = btrfs_super_log_root_level(disk_super);
20602060

2061-
if (fs_devices->rw_devices == 0) {
2061+
if (unlikely(fs_devices->rw_devices == 0)) {
20622062
btrfs_warn(fs_info, "log replay required on RO media");
20632063
return -EIO;
20642064
}
@@ -2079,7 +2079,7 @@ static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
20792079
btrfs_put_root(log_tree_root);
20802080
return ret;
20812081
}
2082-
if (!extent_buffer_uptodate(log_tree_root->node)) {
2082+
if (unlikely(!extent_buffer_uptodate(log_tree_root->node))) {
20832083
btrfs_err(fs_info, "failed to read log tree");
20842084
btrfs_put_root(log_tree_root);
20852085
return -EIO;
@@ -2641,7 +2641,7 @@ static int load_super_root(struct btrfs_root *root, u64 bytenr, u64 gen, int lev
26412641
root->node = NULL;
26422642
return ret;
26432643
}
2644-
if (!extent_buffer_uptodate(root->node)) {
2644+
if (unlikely(!extent_buffer_uptodate(root->node))) {
26452645
free_extent_buffer(root->node);
26462646
root->node = NULL;
26472647
return -EIO;
@@ -3469,7 +3469,7 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
34693469
* below in btrfs_init_dev_replace().
34703470
*/
34713471
btrfs_free_extra_devids(fs_devices);
3472-
if (!fs_devices->latest_dev->bdev) {
3472+
if (unlikely(!fs_devices->latest_dev->bdev)) {
34733473
btrfs_err(fs_info, "failed to read devices");
34743474
ret = -EIO;
34753475
goto fail_tree_roots;
@@ -3963,7 +3963,7 @@ static int barrier_all_devices(struct btrfs_fs_info *info)
39633963
* Checks last_flush_error of disks in order to determine the device
39643964
* state.
39653965
*/
3966-
if (errors_wait && !btrfs_check_rw_degradable(info, NULL))
3966+
if (unlikely(errors_wait && !btrfs_check_rw_degradable(info, NULL)))
39673967
return -EIO;
39683968

39693969
return 0;
@@ -4076,7 +4076,7 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
40764076
if (ret)
40774077
total_errors++;
40784078
}
4079-
if (total_errors > max_errors) {
4079+
if (unlikely(total_errors > max_errors)) {
40804080
btrfs_err(fs_info, "%d errors while writing supers",
40814081
total_errors);
40824082
mutex_unlock(&fs_info->fs_devices->device_list_mutex);
@@ -4101,7 +4101,7 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
41014101
total_errors++;
41024102
}
41034103
mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4104-
if (total_errors > max_errors) {
4104+
if (unlikely(total_errors > max_errors)) {
41054105
btrfs_handle_fs_error(fs_info, -EIO,
41064106
"%d errors while writing supers",
41074107
total_errors);

fs/btrfs/extent-tree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5638,7 +5638,7 @@ static int maybe_drop_reference(struct btrfs_trans_handle *trans, struct btrfs_r
56385638
ref.parent = path->nodes[level]->start;
56395639
} else {
56405640
ASSERT(btrfs_root_id(root) == btrfs_header_owner(path->nodes[level]));
5641-
if (btrfs_root_id(root) != btrfs_header_owner(path->nodes[level])) {
5641+
if (unlikely(btrfs_root_id(root) != btrfs_header_owner(path->nodes[level]))) {
56425642
btrfs_err(root->fs_info, "mismatched block owner");
56435643
return -EIO;
56445644
}
@@ -5774,7 +5774,7 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
57745774

57755775
level--;
57765776
ASSERT(level == btrfs_header_level(next));
5777-
if (level != btrfs_header_level(next)) {
5777+
if (unlikely(level != btrfs_header_level(next))) {
57785778
btrfs_err(root->fs_info, "mismatched level");
57795779
ret = -EIO;
57805780
goto out_unlock;

fs/btrfs/extent_io.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3880,7 +3880,7 @@ int read_extent_buffer_pages(struct extent_buffer *eb, int mirror_num,
38803880
return ret;
38813881

38823882
wait_on_bit_io(&eb->bflags, EXTENT_BUFFER_READING, TASK_UNINTERRUPTIBLE);
3883-
if (!test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
3883+
if (unlikely(!test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)))
38843884
return -EIO;
38853885
return 0;
38863886
}

fs/btrfs/extent_map.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ int btrfs_split_extent_map(struct btrfs_inode *inode, u64 start, u64 len, u64 pr
10571057
btrfs_lock_extent(&inode->io_tree, start, start + len - 1, NULL);
10581058
write_lock(&em_tree->lock);
10591059
em = btrfs_lookup_extent_mapping(em_tree, start, len);
1060-
if (!em) {
1060+
if (unlikely(!em)) {
10611061
ret = -EIO;
10621062
goto out_unlock;
10631063
}

fs/btrfs/file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ static int prepare_uptodate_folio(struct inode *inode, struct folio *folio, u64
815815
if (ret)
816816
return ret;
817817
folio_lock(folio);
818-
if (!folio_test_uptodate(folio)) {
818+
if (unlikely(!folio_test_uptodate(folio))) {
819819
folio_unlock(folio);
820820
return -EIO;
821821
}

fs/btrfs/free-space-tree.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ static int btrfs_search_prev_slot(struct btrfs_trans_handle *trans,
137137
if (ret < 0)
138138
return ret;
139139

140-
if (ret == 0) {
140+
if (unlikely(ret == 0)) {
141141
DEBUG_WARN();
142142
return -EIO;
143143
}
144144

145-
if (p->slots[0] == 0) {
145+
if (unlikely(p->slots[0] == 0)) {
146146
DEBUG_WARN("no previous slot found");
147147
return -EIO;
148148
}
@@ -293,7 +293,7 @@ int btrfs_convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans,
293293
expected_extent_count = btrfs_free_space_extent_count(leaf, info);
294294
btrfs_release_path(path);
295295

296-
if (extent_count != expected_extent_count) {
296+
if (unlikely(extent_count != expected_extent_count)) {
297297
btrfs_err(fs_info,
298298
"incorrect extent count for %llu; counted %u, expected %u",
299299
block_group->start, extent_count,
@@ -465,7 +465,7 @@ int btrfs_convert_free_space_to_extents(struct btrfs_trans_handle *trans,
465465
start_bit = find_next_bit_le(bitmap, nrbits, end_bit);
466466
}
467467

468-
if (extent_count != expected_extent_count) {
468+
if (unlikely(extent_count != expected_extent_count)) {
469469
btrfs_err(fs_info,
470470
"incorrect extent count for %llu; counted %u, expected %u",
471471
block_group->start, extent_count,
@@ -1611,7 +1611,7 @@ static int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl,
16111611
extent_count++;
16121612
}
16131613

1614-
if (extent_count != expected_extent_count) {
1614+
if (unlikely(extent_count != expected_extent_count)) {
16151615
btrfs_err(fs_info,
16161616
"incorrect extent count for %llu; counted %u, expected %u",
16171617
block_group->start, extent_count,
@@ -1672,7 +1672,7 @@ static int load_free_space_extents(struct btrfs_caching_control *caching_ctl,
16721672
extent_count++;
16731673
}
16741674

1675-
if (extent_count != expected_extent_count) {
1675+
if (unlikely(extent_count != expected_extent_count)) {
16761676
btrfs_err(fs_info,
16771677
"incorrect extent count for %llu; counted %u, expected %u",
16781678
block_group->start, extent_count,

0 commit comments

Comments
 (0)