btrfs: warn on invalid slot in tree mod log rewind
authorBoris Burkov <boris@bur.io>
Thu, 1 Jun 2023 18:55:13 +0000 (11:55 -0700)
committerDavid Sterba <dsterba@suse.com>
Mon, 19 Jun 2023 11:59:34 +0000 (13:59 +0200)
commit95c8e349d8e8f190e28854e7ca96de866d2dc5a4
tree819f6dfc57b1b324fb40862c7b586612bbf23710
parent8ab546bb30bdf975e5ca6024ec6c4f7d324cb11a
btrfs: warn on invalid slot in tree mod log rewind

The way that tree mod log tracks the ultimate length of the eb, the
variable 'n', eventually turns up the correct value, but at intermediate
steps during the rewind, n can be inaccurate as a representation of the
end of the eb. For example, it doesn't get updated on move rewinds, and
it does get updated for add/remove in the middle of the eb.

To detect cases with invalid moves, introduce a separate variable called
max_slot which tries to track the maximum valid slot in the rewind eb.
We can then warn if we do a move whose src range goes beyond the max
valid slot.

There is a commented caveat that it is possible to have this value be an
overestimate due to the challenge of properly handling 'add' operations
in the middle of the eb, but in practice it doesn't cause enough of a
problem to throw out the max idea in favor of tracking every valid slot.

CC: stable@vger.kernel.org # 5.15+
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Boris Burkov <boris@bur.io>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/tree-mod-log.c