Btrfs: check items for correctness as we search
PATCH 1/1 Btrfs: check items for correctness as we search
Currently if we have corrupted items things will blow up in spectacular ways. So as we search check the item that we are pointing at currently to make sure it passes sanity checks before moving on. This will catch corruptions in the base item, the more complex item checking will be up to various consumers of btrfs_search_slot, but for now this gets us good sanity checking. Thanks,
--- fs/btrfs/ctree.c | 88 +++++++++++++++++++++++++++++++++++++++--------------- 1 files changed, 64 insertions(+), 24 deletions(-)
fs/btrfs/ctree.c +64 -24
@@ -732,6 +732,12 @@ static inline unsigned int leaf_data_end(struct btrfs_root *root,return btrfs_item_offset_nr (leaf, nr -1 ); } +#define CORRUPT (reason, eb, root, level, slot) \ + printk(KERN_CRIT "btrfs: corrupt %s, %s: block=%llu, root=%llu, " \ + "level=%d, slot=%d\n", level ? "node" : "leaf", reason, \ + (unsigned long long)btrfs_header_bytenr(eb), \ + (unsigned long long)root->objectid, level, slot) +/* * extra debugging checks to make sure all the items in a key are * well formed and in the proper order
@@ -757,21 +763,42 @@ static int check_node(struct btrfs_root *root, struct btrfs_path *path, parent_slot = path->slots[level +1 ];btrfs_node_key (parent, &parent_key, parent_slot);btrfs_node_key (node, &node_key,0 ); -BUG_ON (memcmp (&parent_key, &node_key, -sizeof (struct btrfs_disk_key ))); -BUG_ON (btrfs_node_blockptr (parent, parent_slot) != -btrfs_header_bytenr (node)); +if (memcmp (&parent_key, &node_key, +sizeof (struct btrfs_disk_key ))) { +CORRUPT ("invalid node key" , node, root, level, slot); +return -EIO ; + } +if (btrfs_node_blockptr (parent, parent_slot) != +btrfs_header_bytenr (node)) { +CORRUPT ("blockptr is not right" , node, root, level, + slot); +return -EIO ; + } } -BUG_ON (nritems >BTRFS_NODEPTRS_PER_BLOCK (root)); + +if (nritems >BTRFS_NODEPTRS_PER_BLOCK (root)) { +CORRUPT ("wrong nritems count" , node, root, level, slot); +return -EIO ; + } +if (slot !=0 ) {btrfs_node_key_to_cpu (node, &cpukey, slot -1 );btrfs_node_key (node, &node_key, slot); -BUG_ON (comp_keys (&node_key, &cpukey) <=0 ); +if (comp_keys (&node_key, &cpukey) <=0 ) { +CORRUPT ("keys in wrong order" , node, root, level, + slot); +return -EIO ; + } } +if (slot < nritems -1 ) {btrfs_node_key_to_cpu (node, &cpukey, slot +1 );btrfs_node_key (node, &node_key, slot); -BUG_ON (comp_keys (&node_key, &cpukey) >=0 ); +if (comp_keys (&node_key, &cpukey) >=0 ) { +CORRUPT ("keys in wrong order" , node, root, level, + slot); +return -EIO ; + } }return 0 ; }
@@ -804,24 +831,31 @@ static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,btrfs_node_key (parent, &parent_key, parent_slot);btrfs_item_key (leaf, &leaf_key,0 ); -BUG_ON (memcmp (&parent_key, &leaf_key, -sizeof (struct btrfs_disk_key ))); -BUG_ON (btrfs_node_blockptr (parent, parent_slot) != -btrfs_header_bytenr (leaf)); +if (memcmp (&parent_key, &leaf_key, +sizeof (struct btrfs_disk_key ))) { +CORRUPT ("bad first key" , leaf, root, level, slot); +return -EIO ; + } + +if (btrfs_node_blockptr (parent, parent_slot) != +btrfs_header_bytenr (leaf)) { +CORRUPT ("bad block ptr" , leaf, root, level, slot); +return -EIO ; + } }if (slot !=0 && slot < nritems -1 ) {btrfs_item_key (leaf, &leaf_key, slot);btrfs_item_key_to_cpu (leaf, &cpukey, slot -1 );if (comp_keys (&leaf_key, &cpukey) <=0 ) {btrfs_print_leaf (root, leaf); -printk (KERN_CRIT "slot %d offset bad key\n" , slot); -BUG_ON (1 ); +CORRUPT ("offset bad key" , leaf, root, level, slot); +return -EIO ; }if (btrfs_item_offset_nr (leaf, slot -1 ) !=btrfs_item_end_nr (leaf, slot)) {btrfs_print_leaf (root, leaf); -printk (KERN_CRIT "slot %d offset bad\n" , slot); -BUG_ON (1 ); +CORRUPT ("slot offset bad" , leaf, root, level, slot); +return -EIO ; } }if (slot < nritems -1 ) {
@@ -831,19 +865,22 @@ static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,if (btrfs_item_offset_nr (leaf, slot) !=btrfs_item_end_nr (leaf, slot +1 )) {btrfs_print_leaf (root, leaf); -printk (KERN_CRIT "slot %d offset bad\n" , slot); -BUG_ON (1 ); +CORRUPT ("slot offset bad" , leaf, root, level, slot); +return -EIO ; } } -BUG_ON (btrfs_item_offset_nr (leaf,0 ) + -btrfs_item_size_nr (leaf,0 ) !=BTRFS_LEAF_DATA_SIZE (root)); +if (btrfs_item_offset_nr (leaf,0 ) +btrfs_item_size_nr (leaf,0 ) != +BTRFS_LEAF_DATA_SIZE (root)) { +CORRUPT ("invalid offset size pair" , leaf, root, level, slot); +return -EIO ; + } +return 0 ; } static noinline int check_block(struct btrfs_root *root,struct btrfs_path *path,int level) { -return 0 ;if (level ==0 )return check_leaf (root, path, level);return check_node (root, path, level);
@@ -1188,7 +1225,6 @@ static noinline int balance_level(struct btrfs_trans_handle *trans, } }/* double check we haven't messed things up */ -check_block (root, path, level);if (orig_ptr !=btrfs_node_blockptr (path->nodes[level], path->slots[level]))BUG ();
@@ -1799,10 +1835,8 @@ cow_done:btrfs_unlock_up_safe (p, level +1 ); ret =check_block (root, p, level); -if (ret) { - ret =-1 ; +if (ret) goto done; - } ret =bin_search (b, key, level, &slot);
@@ -4442,6 +4476,12 @@ again:if (!path->skip_locking) path->locks[level] =1 ; + ret =check_block (root, path, level); +if (ret) { +btrfs_release_path (root, path); + goto done; + } +if (!level)break ;
Signature · 1 line
1.7.2.3
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 8 lines · Andrey Kuzmin
Quoted 5 lines · Josef Bacik
Currently if we have corrupted items things will blow up in spectacular ways.
So as we search check the item that we are pointing at currently to make sure it
Quoted 4 lines
Will these checks run multiple times on the same slot? If yes, why not have an in-memory bit 'checked' and skip checks of already checked items? Or simply check once when loading from disk?
I thought about this but I have some reservations. In order to make sure the block is actually right we'd have to check the entire thing when it's read from disk, which isn't too bad, but what happens when only one item in the block is corrupt? For example if just the xattr item for an inode is corrupt, this method would keep you from reading the inode item in the same block which could be just fine. If this is acceptable for everybody then I don't mind doing it this way, but the current implementation makes sure we only check what we are actually interested in so we can have the best possible chance of limping along if only part of the block is bogus. Thanks,
Josef
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 26 lines · Andrey Kuzmin
Quoted 16 lines · Josef Bacik
Quoted 5 lines · Andrey Kuzmin
Quoted 3 lines · Josef Bacik
Currently if we have corrupted items things will blow up in spectacular ways.
So as we search check the item that we are pointing at currently tomake
Quoted 4 lines
Quoted 2 lines
sure it
Will these checks run multiple times on the same slot? If yes, why not
have
Quoted 3 lines
an in-memory bit 'checked' and skip checks of already checked items? Or simply check once when loading from disk?
Quoted 7 lines
I thought about this but I have some reservations. In order to make sure the
block is actually right we'd have to check the entire thing when it's read from
disk, which isn't too bad, but what happens when only one item in the block is
corrupt?
Quoted 9 lines
For example if just the xattr item for an inode is corrupt, this
Quoted 3 lines
method would keep you from reading the inode item in the same block which could
be just fine.I've lost you here. If you check once on load, you can just reread and that's it.
Hrm I'll be a little clearer. If in one block we have 4 valid items and one bogus one, with my current patch we'd only fail if we tried to use the bogus one, the valid items would work fine.
However if we check on read, we have to verify the _entire_ block, so if 1 item is broken the entire block is thrown out, and if there were valid items in that block we won't be able to read those items either.
It seemed to me a better choice to let the user limp along if at all possible, but I'm not convinced that I'm right, so I could be persuaded to do it on read. I'll see what other people think. Thanks,
Josef