Skip to contentSkip to conversation

btrfs-progs: handle raid56 properly

PATCH In review Qu Wenruo 2 participants 4 messages Nov 13, 2022 – Nov 14, 2022 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

cover btrfs-progs: handle raid56 properly

There is a bug that "btrfs check" can fail to even open the filesystem.

The root cause is that raid56 read path doesn't even allow any missing device, which is pretty ironic.

This patchset will fix the raid56 read path, and slightly improve the raid56 handling (still not reaching the granularity of kernel yet).

And finally add a test case for it.

Qu Wenruo (2):
btrfs-progs: properly handle degraded raid56 reads
btrfs-progs: fsck-tests: add test case for degraded raid5

 kernel-lib/bitmap.h                         | 45 +++++++++++++++++
 kernel-shared/extent_io.c                   | 54 ++++++++++++---------
 tests/fsck-tests/060-degraded-check/test.sh | 36 ++++++++++++++
 3 files changed, 113 insertions(+), 22 deletions(-)
 create mode 100644 kernel-lib/bitmap.h
 create mode 100755 tests/fsck-tests/060-degraded-check/test.sh
Signature · 1 line
2.38.1
author
#1 lore raw

PATCH 1/2 btrfs-progs: properly handle degraded raid56 reads

[BUG]
For a degraded RAID5, btrfs check will fail to even read the chunk root:

  # mkfs.btrfs -f -m raid5 -d raid5 $dev1 $dev2 $dev3
  # wipefs -fa $dev1
  # btrfs check $dev2
  Opening filesystem to check...
  warning, device 1 is missing
  bad tree block 22036480, bytenr mismatch, want=22036480, have=0
  ERROR: cannot read chunk root
  ERROR: cannot open file system

[CAUSE]
Although read_tree_block() function from btrfs-progs is properly iterating the mirrors (mirror 1 is reading from the disk directly, mirror 2 will be rebuild from parity), the raid56 recovery path is not handling the read error correctly.

The existing code will try to read the full stripe, but any read failure (including missing device) will immedately cause an error:

	for (i = 0; i < num_stripes; i++) {
		ret = btrfs_pread(multi->stripes[i].dev->fd, pointers[i],
				  BTRFS_STRIPE_LEN, multi->stripes[i].physical,
				  fs_info->zoned);
		if (ret < BTRFS_STRIPE_LEN) {
			ret = -EIO;
			goto out;
		}
	}

[FIX]
To make failed_a/failed_b calculation much easier, and properly handle too many missing devices, here this patch will introduce a new bitmap based solution.

The new @failed_stripe_bitmap will represent all the failed stripes.

So the initial read will mark all the missing devices in the @failed_stripe_bitmap, and later operations will all operate on that bitmap.

Only before we call raid56_recov(), we convert the bitmap to the old failed_a/failed_b interface and continue.

Now btrfs check can handle above case properly:

  # btrfs check $dev2
  Opening filesystem to check...
  warning, device 1 is missing
  Checking filesystem on /dev/test/scratch2
  UUID: 8b2e1cb4-f35b-4856-9b11-262d39d8458b
  [1/7] checking root items
  [2/7] checking extents
  [3/7] checking free space tree
  [4/7] checking fs roots
  [5/7] checking only csums items (without verifying data)
  [6/7] checking root refs
  [7/7] checking quota groups skipped (not enabled on this FS)
  found 147456 bytes used, no error found
  total csum bytes: 0
  total tree bytes: 147456
  total fs tree bytes: 32768
  total extent tree bytes: 16384
  btree space waste bytes: 139871
  file data blocks allocated: 0
   referenced 0
---
 kernel-lib/bitmap.h       | 45 ++++++++++++++++++++++++++++++++
 kernel-shared/extent_io.c | 54 +++++++++++++++++++++++----------------
 2 files changed, 77 insertions(+), 22 deletions(-)
 create mode 100644 kernel-lib/bitmap.h
kernel-lib/bitmap.h new file +45 -0
new file mode 100644
index 000000000000..dbd046db9d94
@@ -0,0 +1,45 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * A user-space bitmap wrapper to provide a subset of kernel bitmap operations.
+ *
+ * Most functions are not a direct copy of the kernel version, but should be
+ * good enough for single thread usage.
+ */
+
+#ifndef _BTRFS_PROGS_LINUX_BITMAP_H_
+#define _BTRFS_PROGS_LINUX_BITMAP_H_
+
+#include <stdlib.h>
+#include "kerncompat.h"
+#include "kernel-lib/bitops.h"
+
+static inline unsigned long *bitmap_zalloc(unsigned int nbits)
+{
+	return calloc(BITS_TO_LONGS(nbits), BITS_PER_LONG);
+}
+
+static inline void bitmap_free(unsigned long *bitmap)
+{
+	free(bitmap);
+}
+
+#define BITMAP_LAST_WORK_MASK(nbits) (~0ULL >> (-(nbits) & (BITS_PER_LONG - 1)))
+static inline unsigned int bitmap_weight(const unsigned long *bitmap,
+					 unsigned int nbits)
+{
+	int ret = 0;
+	int i;
+
+	/* Handle the aligned part first.*/
+	for (i = 0; i < nbits / BITS_PER_LONG; i++)
+		ret += hweight_long(bitmap[i]);
+
+	/* The remaining unaligned part. */
+	if (nbits % BITS_PER_LONG)
+		ret += bitmap[i] & BITMAP_LAST_WORD_MASK(nbits);
+
+	return ret;
+}
+
+#endif
kernel-shared/extent_io.c +32 -22

98 lines not on this page · raw message

Signature · 1 line
2.38.1
author
#2 lore raw

PATCH 2/2 btrfs-progs: fsck-tests: add test case for degraded raid5

In reply to Qu Wenruo

The new test case will make sure btrfs check is fine checking a degraded raid5 filesystem.

---
 tests/fsck-tests/060-degraded-check/test.sh | 36 +++++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100755 tests/fsck-tests/060-degraded-check/test.sh
tests/fsck-tests/060-degraded-check/test.sh new file +36 -0
new file mode 100755
index 000000000000..6a1f50c816da
@@ -0,0 +1,36 @@
+#!/bin/bash
+#
+# Make sure "btrfs check" can handle degraded raid5.
+#
+
+source "$TEST_TOP/common"
+
+check_prereq btrfs
+check_prereq mkfs.btrfs
+check_global_prereq losetup
+check_global_prereq wipefs
+
+setup_loopdevs 3
+prepare_loopdevs
+dev1=${loopdevs[1]}
+dev2=${loopdevs[2]}
+dev3=${loopdevs[3]}
+
+setup_root_helper
+
+# Run 1: victim is dev1
+run_check $SUDO_HELPER "$TOP/mkfs.btrfs" -f -m raid5 -d raid5 "${loopdevs[@]}"
+run_check $SUDO_HELPER wipefs -fa $dev1
+run_check $SUDO_HELPER "$TOP/btrfs" check $dev2
+
+# Run 2: victim is dev2
+run_check $SUDO_HELPER "$TOP/mkfs.btrfs" -f -m raid5 -d raid5 "${loopdevs[@]}"
+run_check $SUDO_HELPER wipefs -fa $dev2
+run_check $SUDO_HELPER "$TOP/btrfs" check $dev3
+
+# Run 3: victim is dev3
+run_check $SUDO_HELPER "$TOP/mkfs.btrfs" -f -m raid5 -d raid5 "${loopdevs[@]}"
+run_check $SUDO_HELPER wipefs -fa $dev3
+run_check $SUDO_HELPER "$TOP/btrfs" check $dev1
+
+cleanup_loopdevs
Signature · 1 line
2.38.1
#3 lore raw
In reply to Qu Wenruo
Quoted 5 lines · Qu Wenruo

There is a bug that "btrfs check" can fail to even open the filesystem.

The root cause is that raid56 read path doesn't even allow any missing device, which is pretty ironic.

Quoted 8 lines

This patchset will fix the raid56 read path, and slightly improve the raid56 handling (still not reaching the granularity of kernel yet).

And finally add a test case for it.

Qu Wenruo (2):
btrfs-progs: properly handle degraded raid56 reads
btrfs-progs: fsck-tests: add test case for degraded raid5

Added to devel, thanks.