btrfs: use sector numbers as keys for the dirty extents xarray
authorFilipe Manana <fdmanana@suse.com>
Tue, 24 Sep 2024 14:58:31 +0000 (15:58 +0100)
committerDavid Sterba <dsterba@suse.com>
Fri, 11 Oct 2024 16:33:35 +0000 (18:33 +0200)
commit97420be7bda57030110c7032ad190d320f2e0157
tree82cee71ce13b7e21ecc428a939f8ecea10e6a475
parente761be2a0744086fc4793a4870d4b5746b7fe8cd
btrfs: use sector numbers as keys for the dirty extents xarray

We are using the logical address ("bytenr") of an extent as the key for
qgroup records in the dirty extents xarray. This is a problem because the
xarrays use "unsigned long" for keys/indices, meaning that on a 32 bits
platform any extent starting at or beyond 4G is truncated, which is a too
low limitation as virtually everyone is using storage with more than 4G of
space. This means a "bytenr" of 4G gets truncated to 0, and so does 8G and
16G for example, resulting in incorrect qgroup accounting.

Fix this by using sector numbers as keys instead, that is, using keys that
match the logical address right shifted by fs_info->sectorsize_bits, which
is what we do for the fs_info->buffer_radix that tracks extent buffers
(radix trees also use an "unsigned long" type for keys). This also makes
the index space more dense which helps optimize the xarray (as mentioned
at Documentation/core-api/xarray.rst).

Fixes: 3cce39a8ca4e ("btrfs: qgroup: use xarray to track dirty extents in transaction")
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/delayed-ref.c
fs/btrfs/delayed-ref.h
fs/btrfs/qgroup.c