Re: [PATCH] btrfs: fix incorrect return value after crossing leaf boundary in lookup_extent_data_ref

Filipe Manana <[email protected]>
Newsgroups org.kernel.vger.fstests,org.kernel.vger.linux-btrfs
Message-ID <CAL3q7H71wE3L14DHYUZ+2cAK-GSd3aU5iY4qypXAdfNNyf9_WA@mail.gmail.com>
On Wed, Mar 25, 2026 at 10:28 AM robbieko <[email protected]> wrote:
>
> After commit 1618aa3c2e01 ("btrfs: simplify return variables in
> lookup_extent_data_ref()"), the err and ret variables were merged into
> a single ret variable. However, when btrfs_next_leaf() returns 0
> (success), ret is overwritten from -ENOENT to 0. If the first key in
> the next leaf does not match (different objectid or type), the function
> returns 0 instead of -ENOENT, making the caller believe the lookup
> succeeded when it did not. This can lead to operations on the wrong
> extent tree item, potentially causing extent tree corruption.
>
> Fix this by returning -ENOENT directly when the key does not match,
> instead of relying on the ret variable.
>
> Fixes: 1618aa3c2e01 ("btrfs: simplify return variables in lookup_extent_data_ref()")
> Signed-off-by: robbieko <[email protected]>

Reviewed-by: Filipe Manana <[email protected]>

Looks good, thanks.

> ---
>  fs/btrfs/extent-tree.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
> index 85ee5c79759d..098e64106d02 100644
> --- a/fs/btrfs/extent-tree.c
> +++ b/fs/btrfs/extent-tree.c
> @@ -495,7 +495,7 @@ static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
>                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
>                 if (key.objectid != bytenr ||
>                     key.type != BTRFS_EXTENT_DATA_REF_KEY)
> -                       return ret;
> +                       return -ENOENT;
>
>                 ref = btrfs_item_ptr(leaf, path->slots[0],
>                                      struct btrfs_extent_data_ref);
> --
> 2.43.0
>
>
> Disclaimer: The contents of this e-mail message and any attachments are confidential and are intended solely for addressee. The information may also be legally privileged. This transmission is sent in trust, for the sole purpose of delivery to the intended recipient. If you have received this transmission in error, any use, reproduction or dissemination of this transmission is strictly prohibited. If you are not the intended recipient, please immediately notify the sender by reply e-mail or phone and delete this message and its attachments, if any.
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.