Commit cad35f98 authored by Filipe Manana's avatar Filipe Manana Committed by Yifan Qiao
Browse files

btrfs: reduce nesting for extent processing at btrfs_lookup_extent_info()

mainline inclusion
from mainline-v6.11-rc1
commit 5c83b3beaee06aa88d4015408ac2d8bb35380b06
bugzilla: https://gitee.com/src-openeuler/kernel/issues/IARX0N
CVE: CVE-2024-46751

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5c83b3beaee06aa88d4015408ac2d8bb35380b06



--------------------------------

Instead of using an if-else statement when processing the extent item at
btrfs_lookup_extent_info(), use a single if statement for the error case
since it does a goto at the end and leave the success (expected) case
following the if statement, reducing indentation and making the logic a
bit easier to follow. Also make the if statement's condition as unlikely
since it's not expected to ever happen, as it signals some corruption,
making it clear and hint the compiler to generate more efficient code.

Reviewed-by: default avatarQu Wenruo <wqu@suse.com>
Signed-off-by: default avatarFilipe Manana <fdmanana@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
Conflicts:
	fs/btrfs/extent-tree.c
[Context differences, no owner now.]
Signed-off-by: default avatarYifan Qiao <qiaoyifan4@huawei.com>
parent 1a77e409
Loading
Loading
Loading
Loading
+8 −11
Original line number Diff line number Diff line
@@ -117,10 +117,7 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
	struct btrfs_delayed_ref_head *head;
	struct btrfs_delayed_ref_root *delayed_refs;
	struct btrfs_path *path;
	struct btrfs_extent_item *ei;
	struct extent_buffer *leaf;
	struct btrfs_key key;
	u32 item_size;
	u64 num_refs;
	u64 extent_flags;
	int ret;
@@ -168,14 +165,11 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
	}

	if (ret == 0) {
		leaf = path->nodes[0];
		item_size = btrfs_item_size_nr(leaf, path->slots[0]);
		if (item_size >= sizeof(*ei)) {
			ei = btrfs_item_ptr(leaf, path->slots[0],
					    struct btrfs_extent_item);
			num_refs = btrfs_extent_refs(leaf, ei);
			extent_flags = btrfs_extent_flags(leaf, ei);
		} else {
		struct extent_buffer *leaf = path->nodes[0];
		struct btrfs_extent_item *ei;
		const u32 item_size = btrfs_item_size_nr(leaf, path->slots[0]);

		if (unlikely(item_size < sizeof(*ei))) {
			ret = -EINVAL;
			btrfs_print_v0_err(fs_info);
			if (trans)
@@ -186,6 +180,9 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
			goto out_free;
		}

		ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
		num_refs = btrfs_extent_refs(leaf, ei);
		extent_flags = btrfs_extent_flags(leaf, ei);
		BUG_ON(num_refs == 0);
	} else {
		num_refs = 0;