Skip to content

Commit d3ce2d0

Browse files
adam900710gregkh
authored andcommitted
btrfs: tree-checker: fix false alert caused by legacy btrfs root item
commit 1465af1 upstream. Commit 259ee77 ("btrfs: tree-checker: Add ROOT_ITEM check") introduced btrfs root item size check, however btrfs root item has two versions, the legacy one which just ends before generation_v2 member, is smaller than current btrfs root item size. This caused btrfs kernel to reject valid but old tree root leaves. Fix this problem by also allowing legacy root item, since kernel can already handle them pretty well and upgrade to newer root item format when needed. Reported-by: Martin Steigerwald <[email protected]> Fixes: 259ee77 ("btrfs: tree-checker: Add ROOT_ITEM check") CC: [email protected] # 5.4+ Tested-By: Martin Steigerwald <[email protected]> Reviewed-by: Josef Bacik <[email protected]> Signed-off-by: Qu Wenruo <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 4b82b8a commit d3ce2d0

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

fs/btrfs/tree-checker.c

+12-5
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ static int check_root_item(struct extent_buffer *leaf, struct btrfs_key *key,
869869
int slot)
870870
{
871871
struct btrfs_fs_info *fs_info = leaf->fs_info;
872-
struct btrfs_root_item ri;
872+
struct btrfs_root_item ri = { 0 };
873873
const u64 valid_root_flags = BTRFS_ROOT_SUBVOL_RDONLY |
874874
BTRFS_ROOT_SUBVOL_DEAD;
875875

@@ -889,14 +889,21 @@ static int check_root_item(struct extent_buffer *leaf, struct btrfs_key *key,
889889
return -EUCLEAN;
890890
}
891891

892-
if (btrfs_item_size_nr(leaf, slot) != sizeof(ri)) {
892+
if (btrfs_item_size_nr(leaf, slot) != sizeof(ri) &&
893+
btrfs_item_size_nr(leaf, slot) != btrfs_legacy_root_item_size()) {
893894
generic_err(leaf, slot,
894-
"invalid root item size, have %u expect %zu",
895-
btrfs_item_size_nr(leaf, slot), sizeof(ri));
895+
"invalid root item size, have %u expect %zu or %u",
896+
btrfs_item_size_nr(leaf, slot), sizeof(ri),
897+
btrfs_legacy_root_item_size());
896898
}
897899

900+
/*
901+
* For legacy root item, the members starting at generation_v2 will be
902+
* all filled with 0.
903+
* And since we allow geneartion_v2 as 0, it will still pass the check.
904+
*/
898905
read_extent_buffer(leaf, &ri, btrfs_item_ptr_offset(leaf, slot),
899-
sizeof(ri));
906+
btrfs_item_size_nr(leaf, slot));
900907

901908
/* Generation related */
902909
if (btrfs_root_generation(&ri) >

include/uapi/linux/btrfs_tree.h

+14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
#include <linux/btrfs.h>
66
#include <linux/types.h>
7+
#ifdef __KERNEL__
8+
#include <linux/stddef.h>
9+
#else
10+
#include <stddef.h>
11+
#endif
712

813
/*
914
* This header contains the structure definitions and constants used
@@ -650,6 +655,15 @@ struct btrfs_root_item {
650655
__le64 reserved[8]; /* for future */
651656
} __attribute__ ((__packed__));
652657

658+
/*
659+
* Btrfs root item used to be smaller than current size. The old format ends
660+
* at where member generation_v2 is.
661+
*/
662+
static inline __u32 btrfs_legacy_root_item_size(void)
663+
{
664+
return offsetof(struct btrfs_root_item, generation_v2);
665+
}
666+
653667
/*
654668
* this is used for both forward and backward root refs
655669
*/

0 commit comments

Comments
 (0)