Skip to contentSkip to conversation

btrfs-progs: Check and repair invalid root item generation

PATCH Superseded Qu Wenruo 3 participants 9 messages Aug 9, 2019 – Aug 12, 2019 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

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.

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                                  |  20 ++++++++++++
 check/mode-common.c                           |  29 ++++++++++++++++++
 check/mode-common.h                           |   1 +
 check/mode-lowmem.c                           |  16 ++++++++++
 check/mode-lowmem.h                           |   1 +
 .../default_case.img                          | Bin 0 -> 3072 bytes
 6 files changed, 67 insertions(+)
 create mode 100644 tests/fsck-tests/041-invalid-root-generation/default_case.img
Signature · 1 line
2.22.0
author
#1 lore raw

PATCH 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/mode-common.c | 29 +++++++++++++++++++++++++++++
 check/mode-common.h |  1 +
 check/mode-lowmem.c | 16 ++++++++++++++++
 check/mode-lowmem.h |  1 +
 4 files changed, 47 insertions(+)
check/mode-common.c +29 -0
index d5f6c8ef97b1..a5b86a0ac32a 100644
@@ -924,3 +924,32 @@ int check_repair_free_space_inode(struct btrfs_fs_info *fs_info,
 	}
 	return ret;
 }
+
+int repair_root_generation(struct btrfs_root *root)
+{
+	struct btrfs_trans_handle *trans;
+	struct btrfs_path path;
+	struct btrfs_key key;
+	int ret;
+
+	trans = btrfs_start_transaction(root, 1);
+	if (IS_ERR(trans))
+		return PTR_ERR(trans);
+
+	key.objectid = 0;
+	key.type = 0;
+	key.offset = 0;
+	btrfs_init_path(&path);
+
+	/* Here we only CoW the tree root to update the geneartion */
+	path.lowest_level = btrfs_header_level(root->node);
+	root->node->flags |= EXTENT_BAD_TRANSID;
+
+	ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
+	if (ret < 0)
+		return ret;
+
+	btrfs_release_path(&path);
+	ret = btrfs_commit_transaction(trans, root);
+	return ret;
+}
check/mode-common.h +1 -0
index 4c169c6e3b29..4a3abeb02c81 100644
@@ -155,4 +155,5 @@ static inline bool is_valid_imode(u32 imode)
 	return true;
 }
 
+int repair_root_generation(struct btrfs_root *root);
 #endif
check/mode-lowmem.c +16 -0
index a2be0e6d7034..bf3b57f5ad2d 100644
@@ -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,21 @@ 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) {
+			ret = repair_root_generation(root);
+			if (!ret) {
+				err &= ~INVALID_GENERATION;
+				printf("Reset generation for root %llu\n",
+					root->root_key.objectid);
+			}
+		}
+	}
 	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
index d2983fd12eb4..0361fb3382b1 100644
@@ -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
#2 lore raw

Re: [PATCH 1/3] btrfs-progs: check/lowmem: Check and repair root generation

Quoted 37 lines · Qu Wenruo

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/mode-common.c | 29 +++++++++++++++++++++++++++++
 check/mode-common.h |  1 +
 check/mode-lowmem.c | 16 ++++++++++++++++
 check/mode-lowmem.h |  1 +
 4 files changed, 47 insertions(+)
check/mode-common.c +10 -0
index d5f6c8ef97b1..a5b86a0ac32a 100644
@@ -924,3 +924,32 @@ int check_repair_free_space_inode(struct btrfs_fs_info *fs_info,
 	}
 	return ret;
 }
+
+int repair_root_generation(struct btrfs_root *root)
+{
+	struct btrfs_trans_handle *trans;
+	struct btrfs_path path;
+	struct btrfs_key key;
+	int ret;
+
+	trans = btrfs_start_transaction(root, 1);
+	if (IS_ERR(trans))

check/mode-common.c int check_repair_free_space_inode(struct btrfs_fs_info *fs_info, · line 946

Quoted 10 lines
check/mode-common.c +10 -0
+		return PTR_ERR(trans);
+
+	key.objectid = 0;
+	key.type = 0;
+	key.offset = 0;
+	btrfs_init_path(&path);
+
+	/* Here we only CoW the tree root to update the geneartion */
+	path.lowest_level = btrfs_header_level(root->node);
+	root->node->flags |= EXTENT_BAD_TRANSID;

Why do you set EXTENT_BAD_TRANSID? This flag is currently used only in read_tree_block to link blocks which have failed ordinary validation to the recow_ebs list, and this only happens in case we have a single copy for this eb. This list is then processed in the main check logic.

So repair_root_generation could possibly be as simple as just linking root->node to the fs_info->recow_ebs and leave the rest to the existing logic?

Quoted 66 lines
check/mode-common.c +9 -0
+
+	ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
+	if (ret < 0)
+		return ret;
+
+	btrfs_release_path(&path);
+	ret = btrfs_commit_transaction(trans, root);
+	return ret;
+}
check/mode-common.h +1 -0
index 4c169c6e3b29..4a3abeb02c81 100644
@@ -155,4 +155,5 @@ static inline bool is_valid_imode(u32 imode)
 	return true;
 }
 
+int repair_root_generation(struct btrfs_root *root);
 #endif
check/mode-lowmem.c +16 -0
index a2be0e6d7034..bf3b57f5ad2d 100644
@@ -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,21 @@ 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) {
+			ret = repair_root_generation(root);
+			if (!ret) {
+				err &= ~INVALID_GENERATION;
+				printf("Reset generation for root %llu\n",
+					root->root_key.objectid);
+			}
+		}
+	}
 	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
index d2983fd12eb4..0361fb3382b1 100644
@@ -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.
#3 lore raw

Re: [PATCH 1/3] btrfs-progs: check/lowmem: Check and repair root generation

Quoted 47 lines · Nikolay Borisov
Quoted 44 lines · Qu Wenruo

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/mode-common.c | 29 +++++++++++++++++++++++++++++
 check/mode-common.h |  1 +
 check/mode-lowmem.c | 16 ++++++++++++++++
 check/mode-lowmem.h |  1 +
 4 files changed, 47 insertions(+)
check/mode-common.c +17 -0
index d5f6c8ef97b1..a5b86a0ac32a 100644
@@ -924,3 +924,32 @@ int check_repair_free_space_inode(struct btrfs_fs_info *fs_info,
 	}
 	return ret;
 }
+
+int repair_root_generation(struct btrfs_root *root)
+{
+	struct btrfs_trans_handle *trans;
+	struct btrfs_path path;
+	struct btrfs_key key;
+	int ret;
+
+	trans = btrfs_start_transaction(root, 1);
+	if (IS_ERR(trans))
+		return PTR_ERR(trans);
+
+	key.objectid = 0;
+	key.type = 0;
+	key.offset = 0;
+	btrfs_init_path(&path);
+
Quoted 8 lines

check/mode-common.c int check_repair_free_space_inode(struct btrfs_fs_info *fs_info, · line 946

Quoted 3 lines
check/mode-common.c +3 -0
+	/* Here we only CoW the tree root to update the geneartion */
+	path.lowest_level = btrfs_header_level(root->node);
+	root->node->flags |= EXTENT_BAD_TRANSID;

Why do you set EXTENT_BAD_TRANSID? This flag is currently used only in read_tree_block to link blocks which have failed ordinary validation to the recow_ebs list, and this only happens in case we have a single copy for this eb. This list is then processed in the main check logic.

Because we have extra transid check in
btrfs_search_slot()/btrfs_cow_block().

EXTENT_BAD_TRANSID is to suppress such warning.

Quoted 4 lines

So repair_root_generation could possibly be as simple as just linking root->node to the fs_info->recow_ebs and leave the rest to the existing logic?

It doesn't change the root generation.

Quoted 67 lines
Quoted 67 lines
check/mode-common.c +9 -0
+
+	ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
+	if (ret < 0)
+		return ret;
+
+	btrfs_release_path(&path);
+	ret = btrfs_commit_transaction(trans, root);
+	return ret;
+}
check/mode-common.h +1 -0
index 4c169c6e3b29..4a3abeb02c81 100644
@@ -155,4 +155,5 @@ static inline bool is_valid_imode(u32 imode)
 	return true;
 }
 
+int repair_root_generation(struct btrfs_root *root);
 #endif
check/mode-lowmem.c +16 -0
index a2be0e6d7034..bf3b57f5ad2d 100644
@@ -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,21 @@ 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) {
+			ret = repair_root_generation(root);
+			if (!ret) {
+				err &= ~INVALID_GENERATION;
+				printf("Reset generation for root %llu\n",
+					root->root_key.objectid);
+			}
+		}
+	}
 	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
index d2983fd12eb4..0361fb3382b1 100644
@@ -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.
#4 lore raw

Re: [PATCH 1/3] btrfs-progs: check/lowmem: Check and repair root generation

Quoted 59 lines · Qu Wenruo
Quoted 47 lines · Nikolay Borisov
Quoted 44 lines · Qu Wenruo

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/mode-common.c | 29 +++++++++++++++++++++++++++++
 check/mode-common.h |  1 +
 check/mode-lowmem.c | 16 ++++++++++++++++
 check/mode-lowmem.h |  1 +
 4 files changed, 47 insertions(+)
check/mode-common.c +17 -0
index d5f6c8ef97b1..a5b86a0ac32a 100644
@@ -924,3 +924,32 @@ int check_repair_free_space_inode(struct btrfs_fs_info *fs_info,
 	}
 	return ret;
 }
+
+int repair_root_generation(struct btrfs_root *root)
+{
+	struct btrfs_trans_handle *trans;
+	struct btrfs_path path;
+	struct btrfs_key key;
+	int ret;
+
+	trans = btrfs_start_transaction(root, 1);
+	if (IS_ERR(trans))
+		return PTR_ERR(trans);
+
+	key.objectid = 0;
+	key.type = 0;
+	key.offset = 0;
+	btrfs_init_path(&path);
+
Quoted 8 lines

check/mode-common.c int check_repair_free_space_inode(struct btrfs_fs_info *fs_info, · line 946

Quoted 3 lines
check/mode-common.c +3 -0
+	/* Here we only CoW the tree root to update the geneartion */
+	path.lowest_level = btrfs_header_level(root->node);
+	root->node->flags |= EXTENT_BAD_TRANSID;

Why do you set EXTENT_BAD_TRANSID? This flag is currently used only in read_tree_block to link blocks which have failed ordinary validation to the recow_ebs list, and this only happens in case we have a single copy for this eb. This list is then processed in the main check logic.

Quoted 4 lines

Because we have extra transid check in
btrfs_search_slot()/btrfs_cow_block().

EXTENT_BAD_TRANSID is to suppress such warning.

nod

Quoted 7 lines
Quoted 5 lines

So repair_root_generation could possibly be as simple as just linking root->node to the fs_info->recow_ebs and leave the rest to the existing logic?

It doesn't change the root generation.

recow_extent_buffer seems to be doing exactly the same thing repair_root_generation does - findw the root item and commits the transaction. What am I missing?

Quoted 69 lines
Quoted 68 lines
Quoted 68 lines
check/mode-common.c +9 -0
+
+	ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
+	if (ret < 0)
+		return ret;
+
+	btrfs_release_path(&path);
+	ret = btrfs_commit_transaction(trans, root);
+	return ret;
+}
check/mode-common.h +1 -0
index 4c169c6e3b29..4a3abeb02c81 100644
@@ -155,4 +155,5 @@ static inline bool is_valid_imode(u32 imode)
 	return true;
 }
 
+int repair_root_generation(struct btrfs_root *root);
 #endif
check/mode-lowmem.c +16 -0
index a2be0e6d7034..bf3b57f5ad2d 100644
@@ -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,21 @@ 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) {
+			ret = repair_root_generation(root);
+			if (!ret) {
+				err &= ~INVALID_GENERATION;
+				printf("Reset generation for root %llu\n",
+					root->root_key.objectid);
+			}
+		}
+	}
 	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
index d2983fd12eb4..0361fb3382b1 100644
@@ -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.
#5 lore raw

Re: [PATCH 1/3] btrfs-progs: check/lowmem: Check and repair root generation

[...]

Quoted 14 lines
Quoted 5 lines

Because we have extra transid check in
btrfs_search_slot()/btrfs_cow_block().

EXTENT_BAD_TRANSID is to suppress such warning.

nod

Quoted 6 lines
Quoted 5 lines

So repair_root_generation could possibly be as simple as just linking root->node to the fs_info->recow_ebs and leave the rest to the existing logic?

Quoted 5 lines
Quoted 1 line

It doesn't change the root generation.

recow_extent_buffer seems to be doing exactly the same thing repair_root_generation does - findw the root item and commits the transaction. What am I missing?

The way it gets the root is not correct.

Using header owner is not good enough for locating the tree owning the tree blocks.

Cases like log trees and shared tree blocks (especially for cases like original tree got deleted) are the main corner cases where the old behavior can't handle.

Thanks,
Qu

Quoted 70 lines
Quoted 70 lines
Quoted 69 lines
Quoted 69 lines
check/mode-common.c +9 -0
+
+	ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
+	if (ret < 0)
+		return ret;
+
+	btrfs_release_path(&path);
+	ret = btrfs_commit_transaction(trans, root);
+	return ret;
+}
check/mode-common.h +1 -0
index 4c169c6e3b29..4a3abeb02c81 100644
@@ -155,4 +155,5 @@ static inline bool is_valid_imode(u32 imode)
 	return true;
 }
 
+int repair_root_generation(struct btrfs_root *root);
 #endif
check/mode-lowmem.c +16 -0
index a2be0e6d7034..bf3b57f5ad2d 100644
@@ -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,21 @@ 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) {
+			ret = repair_root_generation(root);
+			if (!ret) {
+				err &= ~INVALID_GENERATION;
+				printf("Reset generation for root %llu\n",
+					root->root_key.objectid);
+			}
+		}
+	}
 	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
index d2983fd12eb4..0361fb3382b1 100644
@@ -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.
#6 lore raw

Re: [PATCH 1/3] btrfs-progs: check/lowmem: Check and repair root generation

Quoted 22 lines · Qu Wenruo

[...]

Quoted 14 lines
Quoted 5 lines

Because we have extra transid check in
btrfs_search_slot()/btrfs_cow_block().

EXTENT_BAD_TRANSID is to suppress such warning.

nod

Quoted 6 lines
Quoted 5 lines

So repair_root_generation could possibly be as simple as just linking root->node to the fs_info->recow_ebs and leave the rest to the existing logic?

Quoted 5 lines
Quoted 1 line

It doesn't change the root generation.

recow_extent_buffer seems to be doing exactly the same thing repair_root_generation does - findw the root item and commits the transaction. What am I missing?

Quoted 8 lines

The way it gets the root is not correct.

Using header owner is not good enough for locating the tree owning the tree blocks.

Cases like log trees and shared tree blocks (especially for cases like original tree got deleted) are the main corner cases where the old behavior can't handle.

My bad, in fact log tree won't exist in repair mode, and since it's the tree root node, it won't be shared with other trees, so the existing facility is completely fine with the use case.

Thanks for pointing this out!
Qu

Quoted 74 lines

Thanks,
Qu

Quoted 70 lines
Quoted 70 lines
Quoted 69 lines
Quoted 69 lines
check/mode-common.c +9 -0
+
+	ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
+	if (ret < 0)
+		return ret;
+
+	btrfs_release_path(&path);
+	ret = btrfs_commit_transaction(trans, root);
+	return ret;
+}
check/mode-common.h +1 -0
index 4c169c6e3b29..4a3abeb02c81 100644
@@ -155,4 +155,5 @@ static inline bool is_valid_imode(u32 imode)
 	return true;
 }
 
+int repair_root_generation(struct btrfs_root *root);
 #endif
check/mode-lowmem.c +16 -0
index a2be0e6d7034..bf3b57f5ad2d 100644
@@ -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,21 @@ 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) {
+			ret = repair_root_generation(root);
+			if (!ret) {
+				err &= ~INVALID_GENERATION;
+				printf("Reset generation for root %llu\n",
+					root->root_key.objectid);
+			}
+		}
+	}
 	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
index d2983fd12eb4..0361fb3382b1 100644
@@ -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.
author
#7 lore raw

PATCH 2/3 btrfs-progs: check/original: Check and repair root item geneartion

In reply to Qu Wenruo

Add such ability to original mode to fix root generation mismatch, which can be rejected by kernel.

---
 check/main.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
check/main.c +20 -0
index 0cc6fdba0289..1b519a67f746 100644
@@ -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,22 @@ 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) {
+			ret = repair_root_generation(root);
+			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 +3615,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
author
#8 lore raw

PATCH 3/3 btrfs-progs: fsck-tests: Add test case for invalid root generation

In reply to Qu Wenruo

The 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
new file mode 100644
index 0000000000000000000000000000000000000000..4a2c8d88a1a885fb9bcae56adc98dcbe75681441
GIT binary patch
Signature · 1 line
2.22.0