Skip to contentSkip to conversation

btrfs-progs: enhance the backup root behavior

PATCH In review Qu Wenruo 2 participants 8 messages Sep 7 – Sep 8

Subsystems (2)
lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

cover btrfs-progs: enhance the backup root behavior

The first one is a doc update to co-operate with the recent "rescue=usebackuproot_*" mount option changes.

The second one is a simple unexport found during the development.

The third one is to fix false warnings when --tree-root option is utilized.

The last one introduces new "--backup-slot <n>" option to allow end users to specify which backup slot to use, giving them more control on certain corrupted fses.

Qu Wenruo (4):
btrfs-progs: docs: update the rescue=usebackuproot option
btrfs-progs: unexport btrfs_setup_chunk_tree_and_device_map()
btrfs-progs: check: use correct generation when -r option is provided
btrfs-progs: check: introduce --backup-slot option

 Documentation/btrfs-check.rst      | 12 +++--
 Documentation/ch-mount-options.rst | 16 ++++++
 check/main.c                       | 58 ++++++++++++++++++++--
 kernel-shared/disk-io.c            | 80 +++++++++++++++++++++++-------
 kernel-shared/disk-io.h            | 12 +++--
 5 files changed, 150 insertions(+), 28 deletions(-)

--
2.55.0

author
#1 lore raw

PATCH 1/4 btrfs-progs: docs: update the rescue=usebackuproot option

This is to co-operate with the kernel changes, and allow end users to have more control on exactly which backup slot to use.

---
 Documentation/ch-mount-options.rst | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
Documentation/ch-mount-options.rst +16 -0
index b3997edda7ff..174cb0c87ce7 100644
@@ -343,6 +343,22 @@ rescue
 	  Try to use backup root slots inside super block.
 	  Replaces standalone option *usebackuproot*
 
+	  Before 7.4 kernel, this option will automatically load the newest
+	  slot that can load correct root nodes.
+	  Since 7.4 kernel, this option will always load the second newest
+	  slot.
+
+        * *usebackuproot_0* (since 7.4)
+        * *usebackuproot_1* (since 7.4)
+        * *usebackuproot_2* (since 7.4)
+        * *usebackuproot_3* (since 7.4)
+
+	  Load the exact backup slot 0/1/2/3.
+
+	  For slot 0, it means the newest slot, and should match the current
+	  tree roots recorded in the superblock.
+	  For slot 1/2/3, it means the second/third/fourth(oldest) newest slot.
+
         * *nologreplay* (since 5.9)
 
 	  Do not replay any dirty logs.
Signature · 1 line
2.55.0
author
#2 lore raw

PATCH 2/4 btrfs-progs: unexport btrfs_setup_chunk_tree_and_device_map()

In reply to Qu Wenruo

That function is only utilized during __open_ctree_fd(), no need to export it.

Unexport it and remove the "btrfs_" prefix.

---
 kernel-shared/disk-io.c | 6 +++---
 kernel-shared/disk-io.h | 3 ---
 2 files changed, 3 insertions(+), 6 deletions(-)
kernel-shared/disk-io.c +3 -3
index 385b74bbac3c..2b1f6c7f6cad 100644
@@ -1477,8 +1477,8 @@ int btrfs_scan_fs_devices(int fd, const char *path,
 	return 0;
 }
 
-int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info,
-					  u64 chunk_root_bytenr)
+static int setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info,
+					   u64 chunk_root_bytenr)
 {
 	struct btrfs_super_block *sb = fs_info->super_copy;
 	u64 generation;
@@ -1666,7 +1666,7 @@ static struct btrfs_fs_info *__open_ctree_fd(int fp, struct open_ctree_args *oca
 	if (fcntl(fp, F_GETFL) & O_DIRECT)
 		fs_info->zoned = 1;
 
-	ret = btrfs_setup_chunk_tree_and_device_map(fs_info, oca->chunk_tree_bytenr);
+	ret = setup_chunk_tree_and_device_map(fs_info, oca->chunk_tree_bytenr);
 	if (ret)
 		goto out_chunk;
 
kernel-shared/disk-io.h +0 -3
index 839f6e41a709..b3e85c89eab7 100644
@@ -176,9 +176,6 @@ void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info);
 int btrfs_scan_fs_devices(int fd, const char *path,
 			  struct btrfs_fs_devices **fs_devices, u64 sb_bytenr,
 			  unsigned sbflags, int skip_devices);
-int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info,
-			  u64 chunk_root_bytenr);
-
 struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr,
 			      unsigned flags);
 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
Signature · 1 line
2.55.0
author
#4 lore raw

PATCH 3/4 btrfs-progs: check: use correct generation when -r option is provided

In reply to Qu Wenruo

[MINOR WARNING]
When using "-r|--tree-root <bytenr>" option, even if a completely valid tree root (e.g. from backup root) is provided, btrfs check will still give warning on the transid mismatch:

Opening filesystem to check...
parent transid verify failed on 30605312 wanted 9 found 8 parent transid verify failed on 30605312 wanted 9 found 8 parent transid verify failed on 30605312 wanted 9 found 8 Ignoring transid failure
Checking filesystem on test.img

[CAUSE]
When using "-r|--tree-root <bytenr>" option, btrfs-check is still using the generation/level from the super block, thus it will always cause transid mismatch warning.

On the other hand, "--chunk-root <bytenr>" option will properly read out the tree block and use the tree block's level/generation to avoid such warning.

[ENHANCEMENT]
Follow the handling of "--chunk-root" option, by reading out the tree block and use its generation/level for later read verification.

With this fixed, now btrfs check no longer gives transid mismatch warning:

Opening filesystem to check...
Checking filesystem on /home/adam/test.img
UUID: 7d1773d5-9f1f-4109-9736-a2c996d1bcae
...

---
 kernel-shared/disk-io.c | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)
kernel-shared/disk-io.c +17 -5
index 2b1f6c7f6cad..195d9e5df688 100644
@@ -1216,15 +1216,27 @@ static int load_important_roots(struct btrfs_fs_info *fs_info,
 	btrfs_setup_root(root, fs_info, BTRFS_BLOCK_GROUP_TREE_OBJECTID);
 
 tree_root:
-	if (backup) {
+	if (root_tree_bytenr) {
+		struct extent_buffer *eb;
+		struct btrfs_tree_parent_check check = { 0 };
+
+		eb = read_tree_block(fs_info, root_tree_bytenr, &check);
+		if (IS_ERR(eb)) {
+			ret = PTR_ERR(eb);
+			errno = -ret;
+			error("unable to read tree block at %llu: %m", root_tree_bytenr);
+			return ret;
+		}
+		bytenr = root_tree_bytenr;
+		level = btrfs_header_level(eb);
+		gen = btrfs_header_generation(eb);
+		free_extent_buffer(eb);
+	} else if (backup) {
 		bytenr = btrfs_backup_tree_root(backup);
 		gen = btrfs_backup_tree_root_gen(backup);
 		level = btrfs_backup_tree_root_level(backup);
 	} else {
-		if (root_tree_bytenr)
-			bytenr = root_tree_bytenr;
-		else
-			bytenr = btrfs_super_root(sb);
+		bytenr = btrfs_super_root(sb);
 		gen = btrfs_super_generation(sb);
 		level = btrfs_super_root_level(sb);
 	}
Signature · 1 line
2.55.0
author
#6 lore raw

PATCH 4/4 btrfs-progs: check: introduce --backup-slot option

In reply to Qu Wenruo

Just like the recent "rescue=usebackuproot_*" mount option, introduce a similar option for btrfs-check, --backup-slot n.

This will allow the end user to select exact which backup slot to use.

This is inspired by a user report that btrfs-sb-mod is required to use a specific backup root to fix a corrupted fs.

Previously it's still possible using btrfs-check's --chunk-root and --tree-root option, but it requires extra dump-super output and manually passing those bytenrs.

Now it will be much easier to just use a single option to do the same work.

---
 Documentation/btrfs-check.rst | 12 ++++++--
 check/main.c                  | 58 +++++++++++++++++++++++++++++++++--
 kernel-shared/disk-io.c       | 58 ++++++++++++++++++++++++++---------
 kernel-shared/disk-io.h       |  9 ++++++
 4 files changed, 117 insertions(+), 20 deletions(-)
Documentation/btrfs-check.rst +9 -3
index 99fea5ae1d5d..b4a891f951dd 100644
@@ -47,10 +47,16 @@ filesystem, similarly the run time. Check the modes that can also affect that.
 SAFE OR ADVISORY OPTIONS
 ------------------------
 
--b|--backup
-        use the first valid set of backup roots stored in the superblock
+--backup-slot <n>
+	use the *n*-th newest set of backup roots stored in the superblock
 
-        This can be combined with *--super* if some of the superblocks are damaged.
+	If *n* is 0, it means the latest backup roots, which should match the superblock.
+	If *n* is 1/2/3, it means the second/third/fourth(oldest) newest backup roots.
+
+	This option conflicts with *-r|--tree-root* and *--chunk-root* options.
+
+-b|--backup
+	this is an alias to *--backup-slot 1* option
 
 --check-data-csum
         verify checksums of data blocks
check/main.c +55 -3

106 lines not on this page · raw message

kernel-shared/disk-io.c +44 -14

97 lines not on this page · raw message

kernel-shared/disk-io.h +9 -0
index b3e85c89eab7..34ef038b678f 100644
@@ -113,7 +113,16 @@ enum btrfs_open_ctree_flags {
 	 * Use the superblock of the latest device for the transaction commit.
 	 */
 	OPEN_CTREE_USE_LATEST_BDEV		= (1U << 18),
+
+	/* To use which backup slot. */
+	OPEN_CTREE_BACKUP_SLOT_0		= (1U << 19),
+	OPEN_CTREE_BACKUP_SLOT_1		= (1U << 20),
+	OPEN_CTREE_BACKUP_SLOT_2		= (1U << 21),
+	OPEN_CTREE_BACKUP_SLOT_3		= (1U << 22),
 };
+#define OPEN_CTREE_BACKUP_SLOT_MASK		\
+	(OPEN_CTREE_BACKUP_SLOT_0 | OPEN_CTREE_BACKUP_SLOT_1 |\
+	 OPEN_CTREE_BACKUP_SLOT_2 | OPEN_CTREE_BACKUP_SLOT_3)
 
 /*
  * Modes of superblock access
Signature · 1 line
2.55.0
#7 lore raw

Re: [PATCH 4/4] btrfs-progs: check: introduce --backup-slot option

On 9/7/26 7:07 AM, Qu Wenruo wrote:

[...]

check/main.c static int cmd_check(const struct cmd_struct *cmd, int argc, char **argv) · line 10727

Quoted 13 lines
  		switch(c) {
  			case 'a': /* ignored */ break;
  			case 'b':
check/main.c +6 -1
-				ctree_flags |= OPEN_CTREE_BACKUP_ROOT;
+				ret = parse_backup_slot("1", &ctree_flags);
+				if (ret < 0) {
+					error("invalid backup slot, must be 0/1/2/3");
+					exit(1);
+				}
+				backup_slot_set = true;
  				break;
  			case 's':
  				num = arg_strtou64(optarg);

The error message doesn't make sense here, the backup slot is hardcoded to "1" above.