btrfs: add missing unlikely to if branches leading to a DEBUG_WARN()

If statement branches that lead to a DEBUG_WARN() are unexpected to happen
and in most places we surround their expressions with the unlikely tag,
however a few places are missing. Add the unlikely tag to those missing
places to make it explicit to a reader that it's not expected and to hint
the compiler to generate better code.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Filipe Manana
2026-04-23 12:31:46 +01:00
committed by David Sterba
parent 4c45ac3623
commit d54d38fed9
7 changed files with 17 additions and 17 deletions

View File

@@ -4117,7 +4117,7 @@ int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type)
struct btrfs_space_info *space_info;
space_info = btrfs_find_space_info(trans->fs_info, type);
if (!space_info) {
if (unlikely(!space_info)) {
DEBUG_WARN();
return -EINVAL;
}

View File

@@ -4398,7 +4398,7 @@ void __cold close_ctree(struct btrfs_fs_info *fs_info)
ASSERT(list_empty(&fs_info->delayed_iputs));
set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags);
if (btrfs_check_quota_leak(fs_info)) {
if (unlikely(btrfs_check_quota_leak(fs_info))) {
DEBUG_WARN("qgroup reserved space leaked");
btrfs_err(fs_info, "qgroup reserved space leaked");
}

View File

@@ -6655,7 +6655,7 @@ static int btrfs_trim_free_extents_throttle(struct btrfs_device *device,
start = max(start, cur_start);
/* Check if there are any CHUNK_* bits left */
if (start > device->total_bytes) {
if (unlikely(start > device->total_bytes)) {
DEBUG_WARN();
btrfs_warn(fs_info,
"ignoring attempt to trim beyond device size: offset %llu length %llu device %s device size %llu",

View File

@@ -109,7 +109,7 @@ struct btrfs_free_space_info *btrfs_search_free_space_info(
ret = btrfs_search_slot(trans, root, &key, path, 0, cow);
if (ret < 0)
return ERR_PTR(ret);
if (ret != 0) {
if (unlikely(ret != 0)) {
btrfs_warn(fs_info, "missing free space info for %llu",
block_group->start);
DEBUG_WARN();

View File

@@ -755,7 +755,7 @@ static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
{
struct btrfs_fs_info *fs_info = inode->root->fs_info;
if (!btrfs_inode_can_compress(inode)) {
if (unlikely(!btrfs_inode_can_compress(inode))) {
DEBUG_WARN("BTRFS: unexpected compression for ino %llu", btrfs_ino(inode));
return 0;
}

View File

@@ -1858,9 +1858,9 @@ int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
* Thus its reserved space should all be zero, no matter if qgroup
* is consistent or the mode.
*/
if (qgroup->rsv.values[BTRFS_QGROUP_RSV_DATA] ||
qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PREALLOC] ||
qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PERTRANS]) {
if (unlikely(qgroup->rsv.values[BTRFS_QGROUP_RSV_DATA] ||
qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PREALLOC] ||
qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PERTRANS])) {
DEBUG_WARN();
btrfs_warn_rl(fs_info,
"to be deleted qgroup %u/%llu has non-zero numbers, data %llu meta prealloc %llu meta pertrans %llu",
@@ -1879,8 +1879,8 @@ int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
*/
if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_FULL &&
!(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT)) {
if (qgroup->rfer || qgroup->excl ||
qgroup->rfer_cmpr || qgroup->excl_cmpr) {
if (unlikely(qgroup->rfer || qgroup->excl ||
qgroup->rfer_cmpr || qgroup->excl_cmpr)) {
DEBUG_WARN();
qgroup_mark_inconsistent(fs_info,
"to be deleted qgroup %u/%llu has non-zero numbers, rfer %llu rfer_cmpr %llu excl %llu excl_cmpr %llu",
@@ -4822,9 +4822,9 @@ int btrfs_qgroup_add_swapped_blocks(struct btrfs_root *subvol_root,
entry = rb_entry(node, struct btrfs_qgroup_swapped_block, node);
if (entry->subvol_generation != block->subvol_generation ||
entry->reloc_bytenr != block->reloc_bytenr ||
entry->reloc_generation != block->reloc_generation) {
if (unlikely(entry->subvol_generation != block->subvol_generation ||
entry->reloc_bytenr != block->reloc_bytenr ||
entry->reloc_generation != block->reloc_generation)) {
/*
* Duplicated but mismatch entry found. Shouldn't happen.
* Marking qgroup inconsistent should be enough for end

View File

@@ -6081,7 +6081,7 @@ struct btrfs_block_group *btrfs_create_chunk(struct btrfs_trans_handle *trans,
lockdep_assert_held(&info->chunk_mutex);
if (!alloc_profile_is_valid(type, 0)) {
if (unlikely(!alloc_profile_is_valid(type, 0))) {
DEBUG_WARN("invalid alloc profile for type %llu", type);
return ERR_PTR(-EINVAL);
}
@@ -6092,7 +6092,7 @@ struct btrfs_block_group *btrfs_create_chunk(struct btrfs_trans_handle *trans,
return ERR_PTR(-ENOSPC);
}
if (!(type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
if (unlikely(!(type & BTRFS_BLOCK_GROUP_TYPE_MASK))) {
btrfs_err(info, "invalid chunk type 0x%llx requested", type);
DEBUG_WARN();
return ERR_PTR(-EINVAL);
@@ -6262,7 +6262,7 @@ static noinline int init_first_rw_device(struct btrfs_trans_handle *trans)
alloc_profile = btrfs_metadata_alloc_profile(fs_info);
meta_space_info = btrfs_find_space_info(fs_info, alloc_profile);
if (!meta_space_info) {
if (unlikely(!meta_space_info)) {
DEBUG_WARN();
return -EINVAL;
}
@@ -6272,7 +6272,7 @@ static noinline int init_first_rw_device(struct btrfs_trans_handle *trans)
alloc_profile = btrfs_system_alloc_profile(fs_info);
sys_space_info = btrfs_find_space_info(fs_info, alloc_profile);
if (!sys_space_info) {
if (unlikely(!sys_space_info)) {
DEBUG_WARN();
return -EINVAL;
}