* [PATCH] btrfs: fix incorrect return value after crossing leaf boundary in lookup_extent_data_ref
@ 2026-03-25 10:18 robbieko
2026-03-25 18:22 ` Filipe Manana
0 siblings, 1 reply; 2+ messages in thread
From: robbieko @ 2026-03-25 10:18 UTC (permalink / raw)
To: fstests; +Cc: linux-btrfs, robbieko
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 <robbieko@synology.com>
---
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.
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] btrfs: fix incorrect return value after crossing leaf boundary in lookup_extent_data_ref
2026-03-25 10:18 [PATCH] btrfs: fix incorrect return value after crossing leaf boundary in lookup_extent_data_ref robbieko
@ 2026-03-25 18:22 ` Filipe Manana
0 siblings, 0 replies; 2+ messages in thread
From: Filipe Manana @ 2026-03-25 18:22 UTC (permalink / raw)
To: robbieko; +Cc: fstests, linux-btrfs
On Wed, Mar 25, 2026 at 10:28 AM robbieko <robbieko@synology.com> 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 <robbieko@synology.com>
Reviewed-by: Filipe Manana <fdmanana@suse.com>
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.
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-03-25 18:23 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-25 10:18 [PATCH] btrfs: fix incorrect return value after crossing leaf boundary in lookup_extent_data_ref robbieko
2026-03-25 18:22 ` Filipe Manana
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox