From: Gu Jinxiang <gujx@cn.fujitsu.com>
To: <linux-btrfs@vger.kernel.org>
Cc: Qu Wenruo <quwenruo@cn.fujitsu.com>, Su Yue <suy.fnst@cn.fujitsu.com>
Subject: [v6 06/16] btrfs-progs: scrub: Introduce functions to scrub mirror based data blocks
Date: Fri, 5 Jan 2018 19:01:14 +0800 [thread overview]
Message-ID: <1515150084-17231-7-git-send-email-gujx@cn.fujitsu.com> (raw)
In-Reply-To: <1515150084-17231-1-git-send-email-gujx@cn.fujitsu.com>
From: Qu Wenruo <quwenruo@cn.fujitsu.com>
Introduce new function, check/recover_data_mirror(), to check and recover
mirror based data blocks.
Unlike tree block, data blocks must be recovered sector by sector, so we
introduced corrupted_bitmap for check and recover.
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: Su Yue <suy.fnst@cn.fujitsu.com>
Signed-off-by: Gu Jinxiang <gujx@cn.fujitsu.com>
---
scrub.c | 212 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 212 insertions(+)
diff --git a/scrub.c b/scrub.c
index 00786dd3..cee6fe14 100644
--- a/scrub.c
+++ b/scrub.c
@@ -18,6 +18,7 @@
#include "volumes.h"
#include "disk-io.h"
#include "utils.h"
+#include "kernel-lib/bitops.h"
/*
* For parity based profile (RAID56)
@@ -262,3 +263,214 @@ out:
free(buf);
return ret;
}
+
+/*
+ * Check one data mirror given by @start @len and @mirror, or @data
+ * If @data is not given, try to read it from disk.
+ * This function will try to read out all the data then check sum.
+ *
+ * If @data is given, just use the data.
+ * This behavior is useful for RAID5/6 recovery code to verify recovered data.
+ *
+ * If @corrupt_bitmap is given, restore corrupted sector to that bitmap.
+ * This is useful for mirror based profiles to recover its data.
+ *
+ * Return 0 if everything is OK.
+ * Return <0 if something goes wrong, and @scrub_ctx accounting will be updated
+ * if it's a data corruption.
+ */
+static int check_data_mirror(struct btrfs_fs_info *fs_info,
+ struct btrfs_scrub_progress *scrub_ctx,
+ char *data, u64 start, u64 len, int mirror,
+ unsigned long *corrupt_bitmap)
+{
+ u32 sectorsize = fs_info->sectorsize;
+ u32 data_csum;
+ u32 *csums = NULL;
+ char *buf = NULL;
+ int ret = 0;
+ int err = 0;
+ int i;
+ unsigned long *csum_bitmap = NULL;
+
+ if (!data) {
+ buf = malloc(len);
+ if (!buf)
+ return -ENOMEM;
+ ret = read_extent_data_loop(fs_info, scrub_ctx, buf, start,
+ len, mirror);
+ if (ret < 0)
+ goto out;
+ scrub_ctx->data_bytes_scrubbed += len;
+ } else {
+ buf = data;
+ }
+
+ /* Alloc and Check csums */
+ csums = malloc(len / sectorsize * sizeof(data_csum));
+ if (!csums) {
+ ret = -ENOMEM;
+ goto out;
+ }
+ csum_bitmap = malloc(calculate_bitmap_len(len / sectorsize));
+ if (!csum_bitmap) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ if (corrupt_bitmap)
+ memset(corrupt_bitmap, 0,
+ calculate_bitmap_len(len / sectorsize));
+ ret = btrfs_read_data_csums(fs_info, start, len, csums, csum_bitmap);
+ if (ret < 0)
+ goto out;
+
+ for (i = 0; i < len / sectorsize; i++) {
+ if (!test_bit(i, csum_bitmap)) {
+ scrub_ctx->csum_discards++;
+ continue;
+ }
+
+ data_csum = ~(u32)0;
+ data_csum = btrfs_csum_data(buf + i * sectorsize, data_csum,
+ sectorsize);
+ btrfs_csum_final(data_csum, (u8 *)&data_csum);
+
+ if (memcmp(&data_csum, (char *)csums + i * sizeof(data_csum),
+ sizeof(data_csum))) {
+ error("data at bytenr %llu mirror %d csum mismatch, have 0x%08x expect 0x%08x",
+ start + i * sectorsize, mirror, data_csum,
+ *(u32 *)((char *)csums + i * sizeof(data_csum)));
+ err = 1;
+ scrub_ctx->csum_errors++;
+ if (corrupt_bitmap)
+ set_bit(i, corrupt_bitmap);
+ continue;
+ }
+ scrub_ctx->data_bytes_scrubbed += sectorsize;
+ }
+out:
+ if (!data)
+ free(buf);
+ free(csums);
+ free(csum_bitmap);
+
+ if (!ret && err)
+ return -EIO;
+ return ret;
+}
+
+/* Helper to check all mirrors for a good copy */
+static int has_good_mirror(unsigned long *corrupt_bitmaps[], int num_copies,
+ int bit, int *good_mirror)
+{
+ int found_good = 0;
+ int i;
+
+ for (i = 0; i < num_copies; i++) {
+ if (!test_bit(bit, corrupt_bitmaps[i])) {
+ found_good = 1;
+ if (good_mirror)
+ *good_mirror = i + 1;
+ break;
+ }
+ }
+ return found_good;
+}
+
+/*
+ * Helper function to check @corrupt_bitmaps, to verify if it's recoverable
+ * for mirror based data extent.
+ *
+ * Return 1 for recoverable, and 0 for not recoverable
+ */
+static int check_data_mirror_recoverable(struct btrfs_fs_info *fs_info,
+ u64 start, u64 len, u32 sectorsize,
+ unsigned long *corrupt_bitmaps[])
+{
+ int i;
+ int corrupted = 0;
+ int bit;
+ int num_copies = btrfs_num_copies(fs_info, start, len);
+
+ for (i = 0; i < num_copies; i++) {
+ for_each_set_bit(bit, corrupt_bitmaps[i], len / sectorsize) {
+ if (!has_good_mirror(corrupt_bitmaps, num_copies,
+ bit, NULL)) {
+ corrupted = 1;
+ goto out;
+ }
+ }
+ }
+out:
+ return !corrupted;
+}
+
+/*
+ * Try to recover all corrupted sectors specified by @corrupt_bitmaps,
+ * by reading out good sector in other mirror.
+ */
+static int recover_data_mirror(struct btrfs_fs_info *fs_info,
+ struct btrfs_scrub_progress *scrub_ctx,
+ u64 start, u64 len,
+ unsigned long *corrupt_bitmaps[])
+{
+ char *buf;
+ u32 sectorsize = fs_info->sectorsize;
+ int ret = 0;
+ int bit;
+ int i;
+ int bad_mirror;
+ int num_copies;
+
+ /* Don't bother to recover unrecoverable extents */
+ if (!check_data_mirror_recoverable(fs_info, start, len,
+ sectorsize, corrupt_bitmaps))
+ return -EIO;
+
+ buf = malloc(sectorsize);
+ if (!buf)
+ return -ENOMEM;
+
+ num_copies = btrfs_num_copies(fs_info, start, len);
+ for (i = 0; i < num_copies; i++) {
+ for_each_set_bit(bit, corrupt_bitmaps[i], BITS_PER_LONG) {
+ u64 cur = start + bit * sectorsize;
+ int good;
+
+ /* Find good mirror */
+ ret = has_good_mirror(corrupt_bitmaps, num_copies, bit,
+ &good);
+ if (!ret) {
+ error("failed to find good mirror for bytenr %llu",
+ cur);
+ ret = -EIO;
+ goto out;
+ }
+ /* Read out good mirror */
+ ret = read_data_from_disk(fs_info, buf, cur,
+ sectorsize, good);
+ if (ret < 0) {
+ error("failed to read good mirror from bytenr %llu mirror %d",
+ cur, good);
+ goto out;
+ }
+ /* Write back to all other mirrors */
+ for (bad_mirror = 1; bad_mirror <= num_copies;
+ bad_mirror++) {
+ if (bad_mirror == good)
+ continue;
+ ret = write_data_to_disk(fs_info, buf, cur,
+ sectorsize, bad_mirror);
+ if (ret < 0) {
+ error("failed to recover mirror for bytenr %llu mirror %d",
+ cur, bad_mirror);
+ goto out;
+ }
+ }
+ }
+ }
+out:
+ free(buf);
+ return ret;
+}
--
2.14.3
next prev parent reply other threads:[~2018-01-05 11:17 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-01-05 11:01 [v6 00/16] Btrfs-progs offline scrub Gu Jinxiang
2018-01-05 11:01 ` [v6 01/16] btrfs-progs: Introduce new btrfs_map_block function which returns more unified result Gu Jinxiang
2018-01-05 11:01 ` [v6 02/16] btrfs-progs: Allow __btrfs_map_block_v2 to remove unrelated stripes Gu Jinxiang
2018-01-05 11:01 ` [v6 03/16] btrfs-progs: csum: Introduce function to read out data csums Gu Jinxiang
2018-01-05 11:01 ` [v6 04/16] btrfs-progs: scrub: Introduce structures to support offline scrub for RAID56 Gu Jinxiang
2018-01-05 11:01 ` [v6 05/16] btrfs-progs: scrub: Introduce functions to scrub mirror based tree block Gu Jinxiang
2018-01-05 11:01 ` Gu Jinxiang [this message]
2018-01-05 11:01 ` [v6 07/16] btrfs-progs: scrub: Introduce function to scrub one mirror-based extent Gu Jinxiang
2018-01-05 11:01 ` [v6 08/16] btrfs-progs: scrub: Introduce function to scrub one data stripe Gu Jinxiang
2018-01-05 11:01 ` [v6 09/16] btrfs-progs: scrub: Introduce function to verify parities Gu Jinxiang
2018-01-05 11:01 ` [v6 10/16] btrfs-progs: extent-tree: Introduce function to check if there is any extent in given range Gu Jinxiang
2018-01-05 11:01 ` [v6 11/16] btrfs-progs: scrub: Introduce function to recover data parity Gu Jinxiang
2018-01-05 11:01 ` [v6 12/16] btrfs-progs: scrub: Introduce helper to write a full stripe Gu Jinxiang
2018-01-05 11:01 ` [v6 13/16] btrfs-progs: scrub: Introduce a function to scrub one " Gu Jinxiang
2018-01-05 11:01 ` [v6 14/16] btrfs-progs: scrub: Introduce function to check a whole block group Gu Jinxiang
2018-01-05 11:01 ` [v6 15/16] btrfs-progs: scrub: Introduce offline scrub function Gu Jinxiang
2018-01-05 11:01 ` [v6 16/16] btrfs-progs: add test for offline-scrub Gu Jinxiang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1515150084-17231-7-git-send-email-gujx@cn.fujitsu.com \
--to=gujx@cn.fujitsu.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=quwenruo@cn.fujitsu.com \
--cc=suy.fnst@cn.fujitsu.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).