btrfs-progs: make check handle invalid bg items
cover btrfs-progs: make check handle invalid bg items
Hello,
While writing code for extent tree v2 I noticed that I was generating a fs with an invalid block group ->used value. However fsck wasn't catching this, because we don't actuall check the used value of the block group items in normal mode. lowmem mode does this properly thankfully, so this only needs to be added to the normal fsck mode.
I've added code to btrfs-corrupt-block to generate the corrupt image I need for the test case. Then of course the actual patch to detect and fix the problem. Thanks,
Josef
Josef Bacik (3):
btrfs-progs: add the ability to corrupt block group items
btrfs-progs: make check detect and fix invalid used for block groups
btrfs-progs: add a test image with a corrupt block group item
btrfs-corrupt-block.c | 108 +++++++++++++++++- check/common.h | 5 + check/main.c | 89 ++++++++++++++- .../default.img.xz | Bin 0 -> 1036 bytes 4 files changed, 197 insertions(+), 5 deletions(-) create mode 100644 tests/fsck-tests/050-invalid-block-group-used/default.img.xz
Signature · 1 line
2.26.3
PATCH 1/3 btrfs-progs: add the ability to corrupt block group items
While doing the extent tree v2 stuff I noticed that fsck doesn't detect an invalid ->used value on the block group item in the normal mode. To build a test case for this I need the ability to corrupt block group items. This allows us to corrupt the various fields of a block group.
--- btrfs-corrupt-block.c | 108 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 1 deletion(-)
btrfs-corrupt-block.c +107 -1
@@ -348,6 +348,24 @@ enum btrfs_key_field { BTRFS_KEY_BAD, }; +enum btrfs_block_group_field { +BTRFS_BLOCK_GROUP_ITEM_USED , +BTRFS_BLOCK_GROUP_ITEM_FLAGS , +BTRFS_BLOCK_GROUP_ITEM_CHUNK_OBJECTID , +BTRFS_BLOCK_GROUP_ITEM_BAD , +}; + +static enum btrfs_block_group_field convert_block_group_field (char *field) +{ +if (!strncmp (field,"used" ,FIELD_BUF_LEN )) +return BTRFS_BLOCK_GROUP_ITEM_USED ; +if (!strncmp (field,"flags" ,FIELD_BUF_LEN )) +return BTRFS_BLOCK_GROUP_ITEM_FLAGS ; +if (!strncmp (field,"chunk_objectid" ,FIELD_BUF_LEN )) +return BTRFS_BLOCK_GROUP_ITEM_CHUNK_OBJECTID ; +return BTRFS_BLOCK_GROUP_ITEM_BAD ; +} +static enum btrfs_inode_field convert_inode_field (char *field) {if (!strncmp (field,"isize" ,FIELD_BUF_LEN ))
@@ -442,6 +460,83 @@ static u8 generate_u8(u8 orig)return ret; } +static int corrupt_block_group (struct btrfs_root *root,u64 bg,char *field) +{ +struct btrfs_trans_handle *trans; +struct btrfs_path *path; +struct btrfs_block_group_item *bgi; +struct btrfs_key key; +enum btrfs_block_group_field corrupt_field; +u64 orig, bogus; +int ret =0 ; + + root = root->fs_info->extent_root; + + corrupt_field =convert_block_group_field (field); +if (corrupt_field ==BTRFS_BLOCK_GROUP_ITEM_BAD ) { +fprintf (stderr,"Invalid field %s\n" , field); +return -EINVAL ; + } + + path =btrfs_alloc_path (); +if (!path) +return -ENOMEM ; + + trans =btrfs_start_transaction (root,1 ); +if (IS_ERR (trans)) { +btrfs_free_path (path); +fprintf (stderr,"Couldn't start transaction %ld\n" , +PTR_ERR (trans)); +return PTR_ERR (trans); + } + + key.objectid = bg; + key.type =BTRFS_BLOCK_GROUP_ITEM_KEY ; + key.offset =0 ; + + ret =btrfs_search_slot (trans, root, &key, path,0 ,1 ); +if (ret <0 ) { +fprintf (stderr,"Error searching for bg %llu %d\n" , bg, ret); + goto out; + } + + ret =0 ; +btrfs_item_key_to_cpu (path->nodes[0 ], &key, path->slots[0 ]); +if (key.type !=BTRFS_BLOCK_GROUP_ITEM_KEY ) { +fprintf (stderr,"Couldn't find the bg %llu\n" , bg); + goto out; + } + + bgi =btrfs_item_ptr (path->nodes[0 ], path->slots[0 ], + struct btrfs_block_group_item); +switch (corrupt_field) { +case BTRFS_BLOCK_GROUP_ITEM_USED : + orig =btrfs_block_group_used (path->nodes[0 ], bgi); + bogus =generate_u64 (orig); +btrfs_set_block_group_used (path->nodes[0 ], bgi, bogus); +break ; +case BTRFS_BLOCK_GROUP_ITEM_CHUNK_OBJECTID : + orig =btrfs_block_group_chunk_objectid (path->nodes[0 ], bgi); + bogus =generate_u64 (orig); +btrfs_set_block_group_chunk_objectid (path->nodes[0 ], bgi, + bogus); +break ; +case BTRFS_BLOCK_GROUP_ITEM_FLAGS : + orig =btrfs_block_group_flags (path->nodes[0 ], bgi); + bogus =generate_u64 (orig); +btrfs_set_block_group_flags (path->nodes[0 ], bgi, bogus); +break ; +default : + ret = -EINVAL ; + goto out; + } +btrfs_mark_buffer_dirty (path->nodes[0 ]); +out: +btrfs_commit_transaction (trans, root); +btrfs_free_path (path); +return ret; +} +static int corrupt_key (struct btrfs_root *root,struct btrfs_key *key,char *field) {
@@ -1150,6 +1245,7 @@ int main(int argc, char **argv)u64 file_extent = (u64)-1 ;u64 root_objectid =0 ;u64 csum_bytenr =0 ; +u64 block_group =0 ;char field[FIELD_BUF_LEN ]; field[0 ] ='\0' ;
@@ -1177,11 +1273,12 @@ int main(int argc, char **argv) { "delete", no_argument, NULL, 'd'}, { "root", no_argument, NULL, 'r'}, { "csum", required_argument, NULL, 'C'}, + { "block-group", required_argument, NULL, 'B'}, { "help", no_argument, NULL, GETOPT_VAL_HELP}, { NULL, 0, NULL, 0 } }; - c =getopt_long (argc, argv,"l:c:b:eEkuUi:f:x:m:K:I:D:d:r:C:" , + c =getopt_long (argc, argv,"l:c:b:eEkuUi:f:x:m:K:I:D:d:r:C:B:" , long_options,NULL );if (c <0 )break ;
@@ -1244,6 +1341,9 @@ int main(int argc, char **argv)case 'C' : csum_bytenr =arg_strtou64 (optarg);break ; +case 'B' : + block_group =arg_strtou64 (optarg); +break ;case GETOPT_VAL_HELP :default :print_usage (c !=GETOPT_VAL_HELP );
@@ -1385,6 +1485,12 @@ int main(int argc, char **argv) ret =corrupt_key (target_root, &key, field); goto out_close; } +if (block_group) { +if (*field ==0 ) +print_usage (1 ); + ret =corrupt_block_group (root, block_group, field); + goto out_close; + }/* * If we made it here and we have extent set then we didn't specify * inode and we're screwed.
Signature · 1 line
2.26.3
Re: [PATCH 1/3] btrfs-progs: add the ability to corrupt block group items
Quoted 132 lines · Josef Bacik
While doing the extent tree v2 stuff I noticed that fsck doesn't detect an invalid ->used value on the block group item in the normal mode. To build a test case for this I need the ability to corrupt block group items. This allows us to corrupt the various fields of a block group.
Signed-off-by Josef Bacik <[email protected]>--- btrfs-corrupt-block.c | 108 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 1 deletion(-)btrfs-corrupt-block.c +96
-0@@ -348,6 +348,24 @@ enum btrfs_key_field { BTRFS_KEY_BAD, }; +enum btrfs_block_group_field { +BTRFS_BLOCK_GROUP_ITEM_USED , +BTRFS_BLOCK_GROUP_ITEM_FLAGS , +BTRFS_BLOCK_GROUP_ITEM_CHUNK_OBJECTID , +BTRFS_BLOCK_GROUP_ITEM_BAD , +}; + +static enum btrfs_block_group_field convert_block_group_field (char *field) +{ +if (!strncmp (field,"used" ,FIELD_BUF_LEN )) +return BTRFS_BLOCK_GROUP_ITEM_USED ; +if (!strncmp (field,"flags" ,FIELD_BUF_LEN )) +return BTRFS_BLOCK_GROUP_ITEM_FLAGS ; +if (!strncmp (field,"chunk_objectid" ,FIELD_BUF_LEN )) +return BTRFS_BLOCK_GROUP_ITEM_CHUNK_OBJECTID ; +return BTRFS_BLOCK_GROUP_ITEM_BAD ; +} +static enum btrfs_inode_field convert_inode_field (char *field) {if (!strncmp (field,"isize" ,FIELD_BUF_LEN ))@@ -442,6 +460,83 @@ static u8 generate_u8(u8 orig)return ret; } +static int corrupt_block_group (struct btrfs_root *root,u64 bg,char *field) +{ +struct btrfs_trans_handle *trans; +struct btrfs_path *path; +struct btrfs_block_group_item *bgi; +struct btrfs_key key; +enum btrfs_block_group_field corrupt_field; +u64 orig, bogus; +int ret =0 ; + + root = root->fs_info->extent_root; + + corrupt_field =convert_block_group_field (field); +if (corrupt_field ==BTRFS_BLOCK_GROUP_ITEM_BAD ) { +fprintf (stderr,"Invalid field %s\n" , field); +return -EINVAL ; + } + + path =btrfs_alloc_path (); +if (!path) +return -ENOMEM ; + + trans =btrfs_start_transaction (root,1 ); +if (IS_ERR (trans)) { +btrfs_free_path (path); +fprintf (stderr,"Couldn't start transaction %ld\n" , +PTR_ERR (trans)); +return PTR_ERR (trans); + } + + key.objectid = bg; + key.type =BTRFS_BLOCK_GROUP_ITEM_KEY ; + key.offset =0 ; + + ret =btrfs_search_slot (trans, root, &key, path,0 ,1 ); +if (ret <0 ) { +fprintf (stderr,"Error searching for bg %llu %d\n" , bg, ret); + goto out; + } + + ret =0 ; +btrfs_item_key_to_cpu (path->nodes[0 ], &key, path->slots[0 ]); +if (key.type !=BTRFS_BLOCK_GROUP_ITEM_KEY ) { +fprintf (stderr,"Couldn't find the bg %llu\n" , bg); + goto out; + } + + bgi =btrfs_item_ptr (path->nodes[0 ], path->slots[0 ], + struct btrfs_block_group_item); +switch (corrupt_field) { +case BTRFS_BLOCK_GROUP_ITEM_USED : + orig =btrfs_block_group_used (path->nodes[0 ], bgi); + bogus =generate_u64 (orig); +btrfs_set_block_group_used (path->nodes[0 ], bgi, bogus); +break ; +case BTRFS_BLOCK_GROUP_ITEM_CHUNK_OBJECTID : + orig =btrfs_block_group_chunk_objectid (path->nodes[0 ], bgi); + bogus =generate_u64 (orig); +btrfs_set_block_group_chunk_objectid (path->nodes[0 ], bgi, + bogus); +break ; +case BTRFS_BLOCK_GROUP_ITEM_FLAGS : + orig =btrfs_block_group_flags (path->nodes[0 ], bgi); + bogus =generate_u64 (orig); +btrfs_set_block_group_flags (path->nodes[0 ], bgi, bogus); +break ; +default : + ret = -EINVAL ; + goto out; + } +btrfs_mark_buffer_dirty (path->nodes[0 ]); +out: +btrfs_commit_transaction (trans, root); +btrfs_free_path (path); +return ret; +} +static int corrupt_key (struct btrfs_root *root,struct btrfs_key *key,char *field) {@@ -1150,6 +1245,7 @@ int main(int argc, char **argv)u64 file_extent = (u64)-1 ;u64 root_objectid =0 ;u64 csum_bytenr =0 ; +u64 block_group =0 ;char field[FIELD_BUF_LEN ]; field[0 ] ='\0' ;
btrfs-corrupt-block.c int main(int argc, char **argv) · line 1276
Quoted 4 lines
btrfs-corrupt-block.c +1
-0@@ -1177,11 +1273,12 @@ int main(int argc, char **argv) { "delete", no_argument, NULL, 'd'}, { "root", no_argument, NULL, 'r'}, { "csum", required_argument, NULL, 'C'}, + { "block-group", required_argument, NULL, 'B'},
The command line interface of corrupt-block is absolute mess because of the incremental additions like so this so at least please don't use single letter options. Updated in devel in a separate patch.
PATCH 2/3 btrfs-progs: make check detect and fix invalid used for block groups
In reply to Josef BacikThe lowmem mode validates the used field of the block group item, but the normal mode does not. Fix this by keeping a running tally of what we think the used value for the block group should be, and then if it mismatches report an error and fix the problem if we have repair set. We have to keep track of pending extents because we process leaves as we see them, so it could be much later in the process that we find the block group item to associate the extents with.
--- check/common.h | 5 +++ check/main.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 90 insertions(+), 4 deletions(-)
check/common.h +5 -0
@@ -37,10 +37,14 @@ struct block_group_record {u64 offset;u64 flags; + +u64 disk_used; +u64 actual_used; };struct block_group_tree {struct cache_tree tree; +struct extent_io_tree pending_extents;struct list_head block_groups; };
@@ -141,6 +145,7 @@ u64 calc_stripe_length(u64 type, u64 length, int num_stripes);static inline void block_group_tree_init (struct block_group_tree *tree) {cache_tree_init (&tree->tree); +extent_io_tree_init (&tree->pending_extents);INIT_LIST_HEAD (&tree->block_groups); }
check/main.c +85 -4
149 lines not on this page · raw message
Signature · 1 line
2.26.3
PATCH 3/3 btrfs-progs: add a test image with a corrupt block group item
In reply to Josef BacikThis image has a broken used field of a block group item to validate fsck does the correct thing.
--- .../050-invalid-block-group-used/default.img.xz | Bin 0 -> 1036 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/fsck-tests/050-invalid-block-group-used/default.img.xz
tests/fsck-tests/050-invalid-block-group-used/default.img.xz new file binary +0 -0
Signature · 1 line
2.26.3
Quoted 3 lines · Josef Bacik
Hello,
While writing code for extent tree v2
Any doc on the extent tree v2?
Maybe it's a good chance for me to prompt my previous skinny block group tree?
As that would greatly reduce mount time, and since it will introduce incompat flags anyway, it may be a good time to pack those two features together?
Quoted 5 lines
I noticed that I was generating a fs with
an invalid block group ->used value. However fsck wasn't catching this, because we don't actuall check the used value of the block group items in normal mode. lowmem mode does this properly thankfully, so this only needs to be added to the normal fsck mode.
Lowmem mode has a lot of hidden extra checks waiting to be found. :)
Quoted 7 lines
I've added code to btrfs-corrupt-block to generate the corrupt image I need for the test case. Then of course the actual patch to detect and fix the problem. Thanks,
Josef
For the whole series:
Thanks,
Qu
Quoted 12 lines
Josef Bacik (3):
btrfs-progs: add the ability to corrupt block group items
btrfs-progs: make check detect and fix invalid used for block groups
btrfs-progs: add a test image with a corrupt block group itembtrfs-corrupt-block.c | 108 +++++++++++++++++- check/common.h | 5 + check/main.c | 89 ++++++++++++++- .../default.img.xz | Bin 0 -> 1036 bytes 4 files changed, 197 insertions(+), 5 deletions(-)create mode 100644 tests/fsck-tests/050-invalid-block-group-used/default.img.xz
Quoted 12 lines · Josef Bacik
Hello,
While writing code for extent tree v2 I noticed that I was generating a fs with an invalid block group ->used value. However fsck wasn't catching this, because we don't actuall check the used value of the block group items in normal mode. lowmem mode does this properly thankfully, so this only needs to be added to the normal fsck mode.
I've added code to btrfs-corrupt-block to generate the corrupt image I need for the test case. Then of course the actual patch to detect and fix the problem. Thanks,
Quoted 6 lines
Josef
Josef Bacik (3):
btrfs-progs: add the ability to corrupt block group items
btrfs-progs: make check detect and fix invalid used for block groups
btrfs-progs: add a test image with a corrupt block group item
Please use prefixes for the "subsystems" that get changed like
btrfs-progs: corrupt-block: add ability to corrupt block group items
btrfs-progs: check: detect and fix invalid used for block groups
btrfs-progs: tests: add image with a corrupt block group item
Series added to devel, thanks.