Skip to content

Commit 9264d00

Browse files
committed
btrfs: add unlikely annotations to branches leading to EUCLEAN
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 EUCLEAN (a corruption) is one of them. Reviewed-by: Filipe Manana <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 4ca6f24 commit 9264d00

19 files changed

+110
-110
lines changed

fs/btrfs/backref.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ static int add_inline_refs(struct btrfs_backref_walk_ctx *ctx,
10621062
iref = (struct btrfs_extent_inline_ref *)ptr;
10631063
type = btrfs_get_extent_inline_ref_type(leaf, iref,
10641064
BTRFS_REF_TYPE_ANY);
1065-
if (type == BTRFS_REF_TYPE_INVALID)
1065+
if (unlikely(type == BTRFS_REF_TYPE_INVALID))
10661066
return -EUCLEAN;
10671067

10681068
offset = btrfs_extent_inline_ref_offset(leaf, iref);
@@ -1422,7 +1422,7 @@ static int find_parent_nodes(struct btrfs_backref_walk_ctx *ctx,
14221422
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
14231423
if (ret < 0)
14241424
goto out;
1425-
if (ret == 0) {
1425+
if (unlikely(ret == 0)) {
14261426
/*
14271427
* Key with offset -1 found, there would have to exist an extent
14281428
* item with such offset, but this is out of the valid range.
@@ -1652,7 +1652,7 @@ static int find_parent_nodes(struct btrfs_backref_walk_ctx *ctx,
16521652
* case.
16531653
*/
16541654
ASSERT(eie);
1655-
if (!eie) {
1655+
if (unlikely(!eie)) {
16561656
ret = -EUCLEAN;
16571657
goto out;
16581658
}
@@ -2215,7 +2215,7 @@ int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
22152215
ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
22162216
if (ret < 0)
22172217
return ret;
2218-
if (ret == 0) {
2218+
if (unlikely(ret == 0)) {
22192219
/*
22202220
* Key with offset -1 found, there would have to exist an extent
22212221
* item with such offset, but this is out of the valid range.
@@ -2312,7 +2312,7 @@ static int get_extent_inline_ref(unsigned long *ptr,
23122312
*out_eiref = (struct btrfs_extent_inline_ref *)(*ptr);
23132313
*out_type = btrfs_get_extent_inline_ref_type(eb, *out_eiref,
23142314
BTRFS_REF_TYPE_ANY);
2315-
if (*out_type == BTRFS_REF_TYPE_INVALID)
2315+
if (unlikely(*out_type == BTRFS_REF_TYPE_INVALID))
23162316
return -EUCLEAN;
23172317

23182318
*ptr += btrfs_extent_inline_ref_size(*out_type);
@@ -2868,15 +2868,15 @@ int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr)
28682868
ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
28692869
if (ret < 0)
28702870
return ret;
2871-
if (ret == 0) {
2871+
if (unlikely(ret == 0)) {
28722872
/*
28732873
* Key with offset -1 found, there would have to exist an extent
28742874
* item with such offset, but this is out of the valid range.
28752875
*/
28762876
ret = -EUCLEAN;
28772877
goto release;
28782878
}
2879-
if (path->slots[0] == 0) {
2879+
if (unlikely(path->slots[0] == 0)) {
28802880
DEBUG_WARN();
28812881
ret = -EUCLEAN;
28822882
goto release;
@@ -3457,7 +3457,7 @@ int btrfs_backref_add_tree_node(struct btrfs_trans_handle *trans,
34573457
if (ret < 0)
34583458
goto out;
34593459
/* No extra backref? This means the tree block is corrupted */
3460-
if (ret > 0) {
3460+
if (unlikely(ret > 0)) {
34613461
ret = -EUCLEAN;
34623462
goto out;
34633463
}
@@ -3500,7 +3500,7 @@ int btrfs_backref_add_tree_node(struct btrfs_trans_handle *trans,
35003500
((unsigned long)iter->cur_ptr);
35013501
type = btrfs_get_extent_inline_ref_type(eb, iref,
35023502
BTRFS_REF_TYPE_BLOCK);
3503-
if (type == BTRFS_REF_TYPE_INVALID) {
3503+
if (unlikely(type == BTRFS_REF_TYPE_INVALID)) {
35043504
ret = -EUCLEAN;
35053505
goto out;
35063506
}
@@ -3612,7 +3612,7 @@ int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache,
36123612
}
36133613

36143614
/* Sanity check, we shouldn't have any unchecked nodes */
3615-
if (!upper->checked) {
3615+
if (unlikely(!upper->checked)) {
36163616
DEBUG_WARN("we should not have any unchecked nodes");
36173617
return -EUCLEAN;
36183618
}

fs/btrfs/block-group.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,7 +2071,7 @@ static int read_bg_from_eb(struct btrfs_fs_info *fs_info, const struct btrfs_key
20712071
return -ENOENT;
20722072
}
20732073

2074-
if (map->start != key->objectid || map->chunk_len != key->offset) {
2074+
if (unlikely(map->start != key->objectid || map->chunk_len != key->offset)) {
20752075
btrfs_err(fs_info,
20762076
"block group %llu len %llu mismatch with chunk %llu len %llu",
20772077
key->objectid, key->offset, map->start, map->chunk_len);
@@ -2084,7 +2084,7 @@ static int read_bg_from_eb(struct btrfs_fs_info *fs_info, const struct btrfs_key
20842084
flags = btrfs_stack_block_group_flags(&bg) &
20852085
BTRFS_BLOCK_GROUP_TYPE_MASK;
20862086

2087-
if (flags != (map->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
2087+
if (unlikely(flags != (map->type & BTRFS_BLOCK_GROUP_TYPE_MASK))) {
20882088
btrfs_err(fs_info,
20892089
"block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx",
20902090
key->objectid, key->offset, flags,
@@ -2245,7 +2245,7 @@ static int exclude_super_stripes(struct btrfs_block_group *cache)
22452245
return ret;
22462246

22472247
/* Shouldn't have super stripes in sequential zones */
2248-
if (zoned && nr) {
2248+
if (unlikely(zoned && nr)) {
22492249
kfree(logical);
22502250
btrfs_err(fs_info,
22512251
"zoned: block group %llu must not contain super block",
@@ -2336,17 +2336,17 @@ static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info)
23362336
break;
23372337

23382338
bg = btrfs_lookup_block_group(fs_info, map->start);
2339-
if (!bg) {
2339+
if (unlikely(!bg)) {
23402340
btrfs_err(fs_info,
23412341
"chunk start=%llu len=%llu doesn't have corresponding block group",
23422342
map->start, map->chunk_len);
23432343
ret = -EUCLEAN;
23442344
btrfs_free_chunk_map(map);
23452345
break;
23462346
}
2347-
if (bg->start != map->start || bg->length != map->chunk_len ||
2348-
(bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) !=
2349-
(map->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
2347+
if (unlikely(bg->start != map->start || bg->length != map->chunk_len ||
2348+
(bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) !=
2349+
(map->type & BTRFS_BLOCK_GROUP_TYPE_MASK))) {
23502350
btrfs_err(fs_info,
23512351
"chunk start=%llu len=%llu flags=0x%llx doesn't match block group start=%llu len=%llu flags=0x%llx",
23522352
map->start, map->chunk_len,

fs/btrfs/ctree.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,7 @@ read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
14991499
* being cached, read from scrub, or have multiple
15001500
* parents (shared tree blocks).
15011501
*/
1502-
if (btrfs_verify_level_key(tmp, &check)) {
1502+
if (unlikely(btrfs_verify_level_key(tmp, &check))) {
15031503
ret = -EUCLEAN;
15041504
goto out;
15051505
}
@@ -2731,7 +2731,7 @@ static int push_node_left(struct btrfs_trans_handle *trans,
27312731
push_items = min(src_nritems - 8, push_items);
27322732

27332733
/* dst is the left eb, src is the middle eb */
2734-
if (check_sibling_keys(dst, src)) {
2734+
if (unlikely(check_sibling_keys(dst, src))) {
27352735
ret = -EUCLEAN;
27362736
btrfs_abort_transaction(trans, ret);
27372737
return ret;
@@ -2805,7 +2805,7 @@ static int balance_node_right(struct btrfs_trans_handle *trans,
28052805
push_items = max_push;
28062806

28072807
/* dst is the right eb, src is the middle eb */
2808-
if (check_sibling_keys(src, dst)) {
2808+
if (unlikely(check_sibling_keys(src, dst))) {
28092809
ret = -EUCLEAN;
28102810
btrfs_abort_transaction(trans, ret);
28112811
return ret;
@@ -3287,7 +3287,7 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
32873287
if (left_nritems == 0)
32883288
goto out_unlock;
32893289

3290-
if (check_sibling_keys(left, right)) {
3290+
if (unlikely(check_sibling_keys(left, right))) {
32913291
ret = -EUCLEAN;
32923292
btrfs_abort_transaction(trans, ret);
32933293
btrfs_tree_unlock(right);
@@ -3503,7 +3503,7 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
35033503
goto out;
35043504
}
35053505

3506-
if (check_sibling_keys(left, right)) {
3506+
if (unlikely(check_sibling_keys(left, right))) {
35073507
ret = -EUCLEAN;
35083508
btrfs_abort_transaction(trans, ret);
35093509
goto out;

fs/btrfs/dev-replace.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
9898
* We don't have a replace item or it's corrupted. If there is
9999
* a replace target, fail the mount.
100100
*/
101-
if (btrfs_find_device(fs_info->fs_devices, &args)) {
101+
if (unlikely(btrfs_find_device(fs_info->fs_devices, &args))) {
102102
btrfs_err(fs_info,
103103
"found replace target device without a valid replace item");
104104
return -EUCLEAN;
@@ -158,7 +158,7 @@ int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info)
158158
* We don't have an active replace item but if there is a
159159
* replace target, fail the mount.
160160
*/
161-
if (btrfs_find_device(fs_info->fs_devices, &args)) {
161+
if (unlikely(btrfs_find_device(fs_info->fs_devices, &args))) {
162162
btrfs_err(fs_info,
163163
"replace without active item, run 'device scan --forget' on the target device");
164164
ret = -EUCLEAN;

fs/btrfs/disk-io.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ int btrfs_validate_extent_buffer(struct extent_buffer *eb,
404404
CSUM_FMT_VALUE(csum_size, result),
405405
btrfs_header_level(eb),
406406
ignore_csum ? ", ignored" : "");
407-
if (!ignore_csum) {
407+
if (unlikely(!ignore_csum)) {
408408
ret = -EUCLEAN;
409409
goto out;
410410
}
@@ -1055,10 +1055,10 @@ static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root,
10551055
* For real fs, and not log/reloc trees, root owner must
10561056
* match its root node owner
10571057
*/
1058-
if (!btrfs_is_testing(fs_info) &&
1059-
btrfs_root_id(root) != BTRFS_TREE_LOG_OBJECTID &&
1060-
btrfs_root_id(root) != BTRFS_TREE_RELOC_OBJECTID &&
1061-
btrfs_root_id(root) != btrfs_header_owner(root->node)) {
1058+
if (unlikely(!btrfs_is_testing(fs_info) &&
1059+
btrfs_root_id(root) != BTRFS_TREE_LOG_OBJECTID &&
1060+
btrfs_root_id(root) != BTRFS_TREE_RELOC_OBJECTID &&
1061+
btrfs_root_id(root) != btrfs_header_owner(root->node))) {
10621062
btrfs_crit(fs_info,
10631063
"root=%llu block=%llu, tree root owner mismatch, have %llu expect %llu",
10641064
btrfs_root_id(root), root->node->start,
@@ -2324,7 +2324,7 @@ static int validate_sys_chunk_array(const struct btrfs_fs_info *fs_info,
23242324
const u32 sectorsize = btrfs_super_sectorsize(sb);
23252325
u32 sys_array_size = btrfs_super_sys_array_size(sb);
23262326

2327-
if (sys_array_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
2327+
if (unlikely(sys_array_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)) {
23282328
btrfs_err(fs_info, "system chunk array too big %u > %u",
23292329
sys_array_size, BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
23302330
return -EUCLEAN;
@@ -2342,23 +2342,23 @@ static int validate_sys_chunk_array(const struct btrfs_fs_info *fs_info,
23422342
disk_key = (struct btrfs_disk_key *)(sb->sys_chunk_array + cur);
23432343
len = sizeof(*disk_key);
23442344

2345-
if (cur + len > sys_array_size)
2345+
if (unlikely(cur + len > sys_array_size))
23462346
goto short_read;
23472347
cur += len;
23482348

23492349
btrfs_disk_key_to_cpu(&key, disk_key);
2350-
if (key.type != BTRFS_CHUNK_ITEM_KEY) {
2350+
if (unlikely(key.type != BTRFS_CHUNK_ITEM_KEY)) {
23512351
btrfs_err(fs_info,
23522352
"unexpected item type %u in sys_array at offset %u",
23532353
key.type, cur);
23542354
return -EUCLEAN;
23552355
}
23562356
chunk = (struct btrfs_chunk *)(sb->sys_chunk_array + cur);
23572357
num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2358-
if (cur + btrfs_chunk_item_size(num_stripes) > sys_array_size)
2358+
if (unlikely(cur + btrfs_chunk_item_size(num_stripes) > sys_array_size))
23592359
goto short_read;
23602360
type = btrfs_stack_chunk_type(chunk);
2361-
if (!(type & BTRFS_BLOCK_GROUP_SYSTEM)) {
2361+
if (unlikely(!(type & BTRFS_BLOCK_GROUP_SYSTEM))) {
23622362
btrfs_err(fs_info,
23632363
"invalid chunk type %llu in sys_array at offset %u",
23642364
type, cur);
@@ -2605,13 +2605,13 @@ static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info,
26052605
ret = btrfs_validate_super(fs_info, sb, -1);
26062606
if (ret < 0)
26072607
goto out;
2608-
if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) {
2608+
if (unlikely(!btrfs_supported_super_csum(btrfs_super_csum_type(sb)))) {
26092609
ret = -EUCLEAN;
26102610
btrfs_err(fs_info, "invalid csum type, has %u want %u",
26112611
btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32);
26122612
goto out;
26132613
}
2614-
if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) {
2614+
if (unlikely(btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP)) {
26152615
ret = -EUCLEAN;
26162616
btrfs_err(fs_info,
26172617
"invalid incompat flags, has 0x%llx valid mask 0x%llx",
@@ -4065,7 +4065,7 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
40654065
btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
40664066

40674067
ret = btrfs_validate_write_super(fs_info, sb);
4068-
if (ret < 0) {
4068+
if (unlikely(ret < 0)) {
40694069
mutex_unlock(&fs_info->fs_devices->device_list_mutex);
40704070
btrfs_handle_fs_error(fs_info, -EUCLEAN,
40714071
"unexpected superblock corruption detected");
@@ -4881,7 +4881,7 @@ int btrfs_init_root_free_objectid(struct btrfs_root *root)
48814881
ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
48824882
if (ret < 0)
48834883
return ret;
4884-
if (ret == 0) {
4884+
if (unlikely(ret == 0)) {
48854885
/*
48864886
* Key with offset -1 found, there would have to exist a root
48874887
* with such id, but this is out of valid range.

fs/btrfs/export.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ struct dentry *btrfs_get_parent(struct dentry *child)
174174
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
175175
if (ret < 0)
176176
goto fail;
177-
if (ret == 0) {
177+
if (unlikely(ret == 0)) {
178178
/*
179179
* Key with offset of -1 found, there would have to exist an
180180
* inode with such number or a root with such id.

0 commit comments

Comments
 (0)