btrfs: tree-checker: add type and sequence check for inline backrefs
authorQu Wenruo <wqu@suse.com>
Tue, 24 Oct 2023 02:11:11 +0000 (12:41 +1030)
committerDavid Sterba <dsterba@suse.com>
Wed, 15 Nov 2023 16:08:09 +0000 (17:08 +0100)
commit1645c283a87c61f84b2bffd81f50724df959b11a
tree35964d23776479cb7bf80c7164d26773ee7c363a
parentd3933152442b7f94419e9ea71835d71b620baf0e
btrfs: tree-checker: add type and sequence check for inline backrefs

[BUG]
There is a bug report that ntfs2btrfs had a bug that it can lead to
transaction abort and the filesystem flips to read-only.

[CAUSE]
For inline backref items, kernel has a strict requirement for their
ordered, they must follow the following rules:

- All btrfs_extent_inline_ref::type should be in an ascending order

- Within the same type, the items should follow a descending order by
  their sequence number

  For EXTENT_DATA_REF type, the sequence number is result from
  hash_extent_data_ref().
  For other types, their sequence numbers are
  btrfs_extent_inline_ref::offset.

Thus if there is any code not following above rules, the resulted
inline backrefs can prevent the kernel to locate the needed inline
backref and lead to transaction abort.

[FIX]
Ntrfs2btrfs has already fixed the problem, and btrfs-progs has added the
ability to detect such problems.

For kernel, let's be more noisy and be more specific about the order, so
that the next time kernel hits such problem we would reject it in the
first place, without leading to transaction abort.

Link: https://github.com/kdave/btrfs-progs/pull/622
Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/tree-checker.c