diff mbox series

[U-Boot] fs: btrfs: Fix wrong comparison in logical to physical mapping

Message ID 20180704171045.9814-1-marek.behun@nic.cz
State Superseded
Delegated to: Tom Rini
Headers show
Series [U-Boot] fs: btrfs: Fix wrong comparison in logical to physical mapping | expand

Commit Message

Marek Behún July 4, 2018, 5:10 p.m. UTC
The comparison
  logical > item->logical + item->length
in btrfs_map_logical_to_physical is wrong and should be instead
  logical >= item->logical + item->length
For example, if
  item->logical = 4096
  item->length = 4096
and we are looking for logical = 8192, it is not part of item (item is
[4096, 8191]). But the comparison is false and we think we have found
the correct item, although we should be searing in the right subtree.

This fixes some bugs I encountered.

Signed-off-by: Marek Behun <marek.behun@nic.cz>
---
 fs/btrfs/chunk-map.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Joakim Tjernlund July 4, 2018, 5:48 p.m. UTC | #1
On Wed, 2018-07-04 at 19:10 +0200, Marek Behún wrote:
> CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.
> 
> 
> The comparison
>   logical > item->logical + item->length
> in btrfs_map_logical_to_physical is wrong and should be instead
>   logical >= item->logical + item->length
> For example, if
>   item->logical = 4096
>   item->length = 4096
> and we are looking for logical = 8192, it is not part of item (item is
> [4096, 8191]). But the comparison is false and we think we have found
> the correct item, although we should be searing in the right subtree.
> 
> This fixes some bugs I encountered.
> 
> Signed-off-by: Marek Behun <marek.behun@nic.cz>
> ---
>  fs/btrfs/chunk-map.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fs/btrfs/chunk-map.c b/fs/btrfs/chunk-map.c
> index beb6a4bb92..31619f6241 100644
> --- a/fs/btrfs/chunk-map.c
> +++ b/fs/btrfs/chunk-map.c
> @@ -78,7 +78,7 @@ u64 btrfs_map_logical_to_physical(u64 logical)
> 
>                 if (item->logical > logical)
>                         node = node->rb_left;
> -               else if (logical > item->logical + item->length)
> +               else if (logical > item->logical + item->length - 1)
            maybe          logical >= item->logical + item->length   ?

>                         node = node->rb_right;
Marek Behún July 4, 2018, 6:20 p.m. UTC | #2
On Wed, 4 Jul 2018 17:48:30 +0000
Joakim Tjernlund <Joakim.Tjernlund@infinera.com> wrote:

>             maybe          logical >= item->logical + item->length   ?

You're right, i did it correctly in the commit message but not in the
code :)
diff mbox series

Patch

diff --git a/fs/btrfs/chunk-map.c b/fs/btrfs/chunk-map.c
index beb6a4bb92..31619f6241 100644
--- a/fs/btrfs/chunk-map.c
+++ b/fs/btrfs/chunk-map.c
@@ -78,7 +78,7 @@  u64 btrfs_map_logical_to_physical(u64 logical)
 
 		if (item->logical > logical)
 			node = node->rb_left;
-		else if (logical > item->logical + item->length)
+		else if (logical > item->logical + item->length - 1)
 			node = node->rb_right;
 		else
 			return item->physical + logical - item->logical;