btrfs-progs: Check and repair invalid root item generation
v2 cover btrfs-progs: Check and repair invalid root item generation
Kernel is going to reject invalid root generation.
Consider the existing checks are causing some error reports, we should handle such problem in advance, so that's the patchset is going to do, check and repair such invalid root generation.
Changelog:
v2:
- Use existing recow_extent_buffer() to do the repair
Qu Wenruo (3):
btrfs-progs: check/lowmem: Check and repair root generation
btrfs-progs: check/original: Check and repair root item geneartion
btrfs-progs: fsck-tests: Add test case for invalid root generation
check/main.c | 57 +++++++----------- check/mode-common.c | 36 +++++++++++ check/mode-common.h | 1 + check/mode-lowmem.c | 17 ++++++ check/mode-lowmem.h | 1 + .../default_case.img | Bin 0 -> 3072 bytes 6 files changed, 76 insertions(+), 36 deletions(-) create mode 100644 tests/fsck-tests/041-invalid-root-generation/default_case.img
Signature · 1 line
2.22.0
PATCH v2 1/3 btrfs-progs: check/lowmem: Check and repair root generation
Since kernel is going to reject any root item which is newer than super block generation, we need to provide a way to fix such problem in btrfs-check.
This patch addes the ability to report and repair root generation in lowmem mode.
This is done by cowing the root node, so we will update the root generation along with the root node generation at commit transaction time.
--- check/main.c | 36 ------------------------------------ check/mode-common.c | 36 ++++++++++++++++++++++++++++++++++++ check/mode-common.h | 1 + check/mode-lowmem.c | 17 +++++++++++++++++ check/mode-lowmem.h | 1 + 5 files changed, 55 insertions(+), 36 deletions(-)
check/main.c +0 -36
@@ -9024,42 +9024,6 @@ again:return ret; } -static int recow_extent_buffer (struct btrfs_root *root,struct extent_buffer *eb) -{ -struct btrfs_path path; -struct btrfs_trans_handle *trans; -struct btrfs_key key; -int ret; - -printf ("Recowing metadata block %llu\n" , eb->start); - key.objectid =btrfs_header_owner (eb); - key.type =BTRFS_ROOT_ITEM_KEY ; - key.offset = (u64)-1 ; - - root =btrfs_read_fs_root (root->fs_info, &key); -if (IS_ERR (root)) { -fprintf (stderr,"Couldn't find owner root %llu\n" , - key.objectid); -return PTR_ERR (root); - } - - trans =btrfs_start_transaction (root,1 ); -if (IS_ERR (trans)) -return PTR_ERR (trans); - -btrfs_init_path (&path); - path.lowest_level =btrfs_header_level (eb); -if (path.lowest_level) -btrfs_node_key_to_cpu (eb, &key,0 ); -else -btrfs_item_key_to_cpu (eb, &key,0 ); - - ret =btrfs_search_slot (trans, root, &key, &path,0 ,1 ); -btrfs_commit_transaction (trans, root); -btrfs_release_path (&path); -return ret; -} -static int delete_bad_item (struct btrfs_root *root,struct bad_item *bad) {struct btrfs_path path;
check/mode-common.c +36 -0
@@ -924,3 +924,39 @@ int check_repair_free_space_inode(struct btrfs_fs_info *fs_info, }return ret; } + +int recow_extent_buffer (struct btrfs_root *root,struct extent_buffer *eb) +{ +struct btrfs_path path; +struct btrfs_trans_handle *trans; +struct btrfs_key key; +int ret; + +printf ("Recowing metadata block %llu\n" , eb->start); + key.objectid =btrfs_header_owner (eb); + key.type =BTRFS_ROOT_ITEM_KEY ; + key.offset = (u64)-1 ; + + root =btrfs_read_fs_root (root->fs_info, &key); +if (IS_ERR (root)) { +fprintf (stderr,"Couldn't find owner root %llu\n" , + key.objectid); +return PTR_ERR (root); + } + + trans =btrfs_start_transaction (root,1 ); +if (IS_ERR (trans)) +return PTR_ERR (trans); + +btrfs_init_path (&path); + path.lowest_level =btrfs_header_level (eb); +if (path.lowest_level) +btrfs_node_key_to_cpu (eb, &key,0 ); +else +btrfs_item_key_to_cpu (eb, &key,0 ); + + ret =btrfs_search_slot (trans, root, &key, &path,0 ,1 ); +btrfs_commit_transaction (trans, root); +btrfs_release_path (&path); +return ret; +}
check/mode-common.h +1 -0
@@ -155,4 +155,5 @@ static inline bool is_valid_imode(u32 imode) return true; } +int recow_extent_buffer (struct btrfs_root *root,struct extent_buffer *eb);#endif
check/mode-lowmem.c +17 -0
@@ -4957,6 +4957,7 @@ static int check_btrfs_root(struct btrfs_root *root, int check_all)struct btrfs_path path;struct node_refs nrefs;struct btrfs_root_item *root_item = &root->root_item; +u64 super_generation =btrfs_super_generation (root->fs_info->super_copy);int ret;int level;int err =0 ;
@@ -4978,6 +4979,22 @@ static int check_btrfs_root(struct btrfs_root *root, int check_all) level =btrfs_header_level (root->node);btrfs_init_path (&path); +if (btrfs_root_generation (root_item) > super_generation +1 ) { +error ( +"invalid root generation for root %llu, have %llu expect (0, %llu)" , + root->root_key.objectid,btrfs_root_generation (root_item), + super_generation +1 ); + err |=INVALID_GENERATION ; +if (repair) { + root->node->flags |=EXTENT_BAD_TRANSID ; + ret =recow_extent_buffer (root, root->node); +if (!ret) { +printf ("Reset generation for root %llu\n" , + root->root_key.objectid); + err &= ~INVALID_GENERATION ; + } + } + }if (btrfs_root_refs (root_item) >0 ||btrfs_disk_key_objectid (&root_item->drop_progress) ==0 ) { path.nodes[level] = root->node;
check/mode-lowmem.h +1 -0
@@ -47,6 +47,7 @@#define INODE_FLAGS_ERROR (1<<23)/* Invalid inode flags */ #define DIR_ITEM_HASH_MISMATCH (1<<24)/* Dir item hash mismatch */ #define INODE_MODE_ERROR (1<<25)/* Bad inode mode */ +#define INVALID_GENERATION (1<<26)/* Generation is too new */ /* * Error bit for low memory mode check.
Signature · 1 line
2.22.0
PATCH v2 2/3 btrfs-progs: check/original: Check and repair root item geneartion
In reply to Qu WenruoAdd such ability to original mode to fix root generation mismatch, which can be rejected by kernel.
--- check/main.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
check/main.c +21 -0
@@ -3437,8 +3437,10 @@ static int check_fs_root(struct btrfs_root *root, {int ret =0 ;int err =0 ; +bool generation_err = false;int wret;int level; +u64 super_generation;struct btrfs_path path;struct shared_node root_node;struct root_record *rec;
@@ -3449,6 +3451,23 @@ static int check_fs_root(struct btrfs_root *root,struct unaligned_extent_rec_t *urec;struct unaligned_extent_rec_t *tmp; + super_generation =btrfs_super_generation (root->fs_info->super_copy); +if (btrfs_root_generation (root_item) > super_generation +1 ) { +error ( +"invalid generation for root %llu, have %llu expect (0, %llu]" , + root->root_key.objectid,btrfs_root_generation (root_item), + super_generation +1 ); + generation_err = true; +if (repair) { + root->node->flags |=EXTENT_BAD_TRANSID ; + ret =recow_extent_buffer (root, root->node); +if (!ret) { +printf ("Reset generation for root %llu\n" , + root->root_key.objectid); + generation_err = false; + } + } + }/* * Reuse the corrupt_block cache tree to record corrupted tree block *
@@ -3597,6 +3616,8 @@ skip_walking:free_corrupt_blocks_tree (&corrupt_blocks); root->fs_info->corrupt_blocks =NULL ; +if (!ret && generation_err) + ret =-1 ;return ret; }
Signature · 1 line
2.22.0
PATCH v2 3/3 btrfs-progs: fsck-tests: Add test case for invalid root generation
In reply to Qu WenruoThe image contains a fs tree whose generation is over 100 larger than super block generation.
--- .../041-invalid-root-generation/default_case.img | Bin 0 -> 3072 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/fsck-tests/041-invalid-root-generation/default_case.img
tests/fsck-tests/041-invalid-root-generation/default_case.img new file binary +0 -0
Signature · 1 line
2.22.0