Skip to content

Commit cffc5a2

Browse files
adam900710kakra
authored andcommitted
btrfs: tree-checker: validate dref root and objectid
Not yet upstreamed. [CORRUPTION] There is a bug report that btrfs flips RO due to a corruption in the extent tree, the involved dumps looks like this: item 188 key (402811572224 168 4096) itemoff 14598 itemsize 79 extent refs 3 gen 3678544 flags 1 ref#0: extent data backref root 13835058055282163977 objectid 281473384125923 offset 81432576 count 1 ref#1: shared data backref parent 1947073626112 count 1 ref#2: shared data backref parent 1156030103552 count 1 BTRFS critical (device vdc1: state EA): unable to find ref byte nr 402811572224 parent 0 root 265 owner 28703026 offset 81432576 slot 189 BTRFS error (device vdc1: state EA): failed to run delayed ref for logical 402811572224 num_bytes 4096 type 178 action 2 ref_mod 1: -2 [CAUSE] The corrupted entry is ref#0 of item 188. The root number 13835058055282163977 is beyond the upper limit for root items (the current limit is 1 << 48), and the objectid also looks suspicious. Only the offset and count is correct. [ENHANCEMENT] Although it's still unknown why we have such many bytes corrupted randomly, we can still enhance the tree-checker for data backrefs by: - Validate the root value For now there should only be 3 types of roots can have data backref: * subvolume trees * data reloc trees * root tree Only for v1 space cache - validate the objectid value The objectid should be a valid inode number. Hopefully we can catch such problem in the future with the new checkers. Reported-by: Kai Krakow <hurikhan77@gmail.com> Link: https://lore.kernel.org/linux-btrfs/CAMthOuPjg5RDT-G_LXeBBUUtzt3cq=JywF+D1_h+JYxe=WKp-Q@mail.gmail.com/#t Reviewed-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: Qu Wenruo <wqu@suse.com>
1 parent f8ae3d1 commit cffc5a2

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

fs/btrfs/tree-checker.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,19 @@ static void extent_err(const struct extent_buffer *eb, int slot,
12651265
va_end(args);
12661266
}
12671267

1268+
static bool is_valid_dref_root(u64 rootid)
1269+
{
1270+
/*
1271+
* The following tree root objectids are allowed to have a data backref:
1272+
* - subvolume trees
1273+
* - data reloc tree
1274+
* - tree root
1275+
* For v1 space cache
1276+
*/
1277+
return is_fstree(rootid) || rootid == BTRFS_DATA_RELOC_TREE_OBJECTID ||
1278+
rootid == BTRFS_ROOT_TREE_OBJECTID;
1279+
}
1280+
12681281
static int check_extent_item(struct extent_buffer *leaf,
12691282
struct btrfs_key *key, int slot,
12701283
struct btrfs_key *prev_key)
@@ -1417,6 +1430,8 @@ static int check_extent_item(struct extent_buffer *leaf,
14171430
struct btrfs_extent_data_ref *dref;
14181431
struct btrfs_shared_data_ref *sref;
14191432
u64 seq;
1433+
u64 dref_root;
1434+
u64 dref_objectid;
14201435
u64 dref_offset;
14211436
u64 inline_offset;
14221437
u8 inline_type;
@@ -1460,11 +1475,26 @@ static int check_extent_item(struct extent_buffer *leaf,
14601475
*/
14611476
case BTRFS_EXTENT_DATA_REF_KEY:
14621477
dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1478+
dref_root = btrfs_extent_data_ref_root(leaf, dref);
1479+
dref_objectid = btrfs_extent_data_ref_objectid(leaf, dref);
14631480
dref_offset = btrfs_extent_data_ref_offset(leaf, dref);
14641481
seq = hash_extent_data_ref(
14651482
btrfs_extent_data_ref_root(leaf, dref),
14661483
btrfs_extent_data_ref_objectid(leaf, dref),
14671484
btrfs_extent_data_ref_offset(leaf, dref));
1485+
if (unlikely(!is_valid_dref_root(dref_root))) {
1486+
extent_err(leaf, slot,
1487+
"invalid data ref root value %llu",
1488+
dref_root);
1489+
return -EUCLEAN;
1490+
}
1491+
if (unlikely(dref_objectid < BTRFS_FIRST_FREE_OBJECTID ||
1492+
dref_objectid > BTRFS_LAST_FREE_OBJECTID)) {
1493+
extent_err(leaf, slot,
1494+
"invalid data ref objectid value %llu",
1495+
dref_root);
1496+
return -EUCLEAN;
1497+
}
14681498
if (unlikely(!IS_ALIGNED(dref_offset,
14691499
fs_info->sectorsize))) {
14701500
extent_err(leaf, slot,
@@ -1600,14 +1630,31 @@ static int check_extent_data_ref(struct extent_buffer *leaf,
16001630
return -EUCLEAN;
16011631
}
16021632
for (; ptr < end; ptr += sizeof(*dref)) {
1633+
u64 root;
1634+
u64 objectid;
16031635
u64 offset;
16041636

16051637
/*
16061638
* We cannot check the extent_data_ref hash due to possible
16071639
* overflow from the leaf due to hash collisions.
16081640
*/
16091641
dref = (struct btrfs_extent_data_ref *)ptr;
1642+
root = btrfs_extent_data_ref_root(leaf, dref);
1643+
objectid = btrfs_extent_data_ref_objectid(leaf, dref);
16101644
offset = btrfs_extent_data_ref_offset(leaf, dref);
1645+
if (unlikely(!is_valid_dref_root(root))) {
1646+
extent_err(leaf, slot,
1647+
"invalid extent data backref root value %llu",
1648+
root);
1649+
return -EUCLEAN;
1650+
}
1651+
if (unlikely(objectid < BTRFS_FIRST_FREE_OBJECTID ||
1652+
objectid > BTRFS_LAST_FREE_OBJECTID)) {
1653+
extent_err(leaf, slot,
1654+
"invalid extent data backref objectid value %llu",
1655+
root);
1656+
return -EUCLEAN;
1657+
}
16111658
if (unlikely(!IS_ALIGNED(offset, leaf->fs_info->sectorsize))) {
16121659
extent_err(leaf, slot,
16131660
"invalid extent data backref offset, have %llu expect aligned to %u",

0 commit comments

Comments
 (0)