Qu Wenruo fe1c6c7acc btrfs: fix wrong block_start calculation for btrfs_drop_extent_map_range()
[BUG]
During my extent_map cleanup/refactor, with extra sanity checks,
extent-map-tests::test_case_7() would not pass the checks.

The problem is, after btrfs_drop_extent_map_range(), the resulted
extent_map has a @block_start way too large.
Meanwhile my btrfs_file_extent_item based members are returning a
correct @disk_bytenr/@offset combination.

The extent map layout looks like this:

     0        16K    32K       48K
     | PINNED |      | Regular |

The regular em at [32K, 48K) also has 32K @block_start.

Then drop range [0, 36K), which should shrink the regular one to be
[36K, 48K).
However the @block_start is incorrect, we expect 32K + 4K, but got 52K.

[CAUSE]
Inside btrfs_drop_extent_map_range() function, if we hit an extent_map
that covers the target range but is still beyond it, we need to split
that extent map into half:

	|<-- drop range -->|
		 |<----- existing extent_map --->|

And if the extent map is not compressed, we need to forward
extent_map::block_start by the difference between the end of drop range
and the extent map start.

However in that particular case, the difference is calculated using
(start + len - em->start).

The problem is @start can be modified if the drop range covers any
pinned extent.

This leads to wrong calculation, and would be caught by my later
extent_map sanity checks, which checks the em::block_start against
btrfs_file_extent_item::disk_bytenr + btrfs_file_extent_item::offset.

This is a regression caused by commit c962098ca4af ("btrfs: fix
incorrect splitting in btrfs_drop_extent_map_range"), which removed the
@len update for pinned extents.

[FIX]
Fix it by avoiding using @start completely, and use @end - em->start
instead, which @end is exclusive bytenr number.

And update the test case to verify the @block_start to prevent such
problem from happening.

Thankfully this is not going to lead to any data corruption, as IO path
does not utilize btrfs_drop_extent_map_range() with @skip_pinned set.

So this fix is only here for the sake of consistency/correctness.

CC: stable@vger.kernel.org # 6.5+
Fixes: c962098ca4af ("btrfs: fix incorrect splitting in btrfs_drop_extent_map_range")
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2024-04-18 18:18:50 +02:00
..
2024-01-11 20:11:35 -08:00
2024-01-11 20:11:35 -08:00
2023-10-30 09:47:13 -10:00
2023-11-07 12:11:26 -08:00
2024-02-25 09:53:13 -08:00
2024-03-01 12:22:30 -08:00
2023-11-07 12:11:26 -08:00
2024-02-25 02:10:32 -05:00
2023-12-29 11:58:34 -08:00
2024-01-10 17:44:36 -08:00
2024-02-07 17:48:15 +00:00
2024-01-11 20:11:35 -08:00
2024-01-10 17:44:36 -08:00
2024-02-12 07:15:45 -08:00
2024-01-10 17:44:36 -08:00
2023-10-30 09:47:13 -10:00
2024-01-06 23:49:50 +01:00
2024-01-11 10:07:29 -08:00
2024-02-16 10:20:35 +09:00
2023-12-21 13:17:54 +01:00
2023-10-30 19:28:19 -10:00
2023-10-30 19:28:19 -10:00
2024-02-09 23:31:16 -05:00
2024-01-24 13:32:29 -08:00
2024-01-11 20:11:35 -08:00
2023-12-12 14:24:14 +01:00
2024-01-11 20:00:22 -08:00
2024-01-11 20:11:35 -08:00
2024-01-19 09:10:23 -08:00
2024-01-11 20:11:35 -08:00
2024-01-19 09:10:23 -08:00
2023-11-25 02:49:43 -05:00
2024-01-08 11:11:51 -08:00
2024-01-10 17:44:36 -08:00
2024-01-08 10:57:34 -08:00
2024-01-17 13:03:37 -08:00