All of lore.kernel.org
 help / color / mirror / Atom feed
From: Qu Wenruo <quwenruo@cn.fujitsu.com>
To: <linux-btrfs@vger.kernel.org>
Cc: <dsterba@suse.cz>, <lakshmipathi.g@gmail.com>,
	Su Yue <suy.fnst@cn.fujitsu.com>
Subject: [PATCH v4 11/20] btrfs-progs: scrub: Introduce functions to scrub mirror based data blocks
Date: Thu, 25 May 2017 14:21:56 +0800	[thread overview]
Message-ID: <20170525062205.11660-12-quwenruo@cn.fujitsu.com> (raw)
In-Reply-To: <20170525062205.11660-1-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>
---
 scrub.c | 212 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 212 insertions(+)

diff --git a/scrub.c b/scrub.c
index f5a5c205..e473d168 100644
--- a/scrub.c
+++ b/scrub.c
@@ -25,6 +25,7 @@
 #include "volumes.h"
 #include "disk-io.h"
 #include "utils.h"
+#include "kernel-lib/bitops.h"
 
 /*
  * For parity based profile(RAID56)
@@ -269,3 +270,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->tree_root->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->mapping_tree, 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->tree_root->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->mapping_tree, 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.13.0




  parent reply	other threads:[~2017-05-25  6:22 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-25  6:21 [PATCH v4 00/20] Btrfs-progs offline scrub Qu Wenruo
2017-05-25  6:21 ` [PATCH v4 01/20] btrfs-progs: raid56: Introduce raid56 header for later recovery usage Qu Wenruo
2017-05-31 13:25   ` David Sterba
2017-05-25  6:21 ` [PATCH v4 02/20] btrfs-progs: raid56: Introduce tables for RAID6 recovery Qu Wenruo
2017-05-25  6:21 ` [PATCH v4 03/20] btrfs-progs: raid56: Allow raid6 to recover 2 data stripes Qu Wenruo
2017-05-25  6:21 ` [PATCH v4 04/20] btrfs-progs: raid56: Allow raid6 to recover data and p Qu Wenruo
2017-05-25  6:21 ` [PATCH v4 05/20] btrfs-progs: Introduce wrapper to recover raid56 data Qu Wenruo
2017-05-31 13:52   ` David Sterba
2017-06-01  1:04     ` Qu Wenruo
2017-05-25  6:21 ` [PATCH v4 06/20] btrfs-progs: Introduce new btrfs_map_block function which returns more unified result Qu Wenruo
2017-05-25  6:21 ` [PATCH v4 07/20] btrfs-progs: Allow __btrfs_map_block_v2 to remove unrelated stripes Qu Wenruo
2017-05-25  6:21 ` [PATCH v4 08/20] btrfs-progs: csum: Introduce function to read out data csums Qu Wenruo
2017-05-25  6:21 ` [PATCH v4 09/20] btrfs-progs: scrub: Introduce structures to support offline scrub for RAID56 Qu Wenruo
2017-05-25  6:21 ` [PATCH v4 10/20] btrfs-progs: scrub: Introduce functions to scrub mirror based tree block Qu Wenruo
2017-05-25  6:21 ` Qu Wenruo [this message]
2017-05-25  6:21 ` [PATCH v4 12/20] btrfs-progs: scrub: Introduce function to scrub one mirror-based extent Qu Wenruo
2017-05-25  6:21 ` [PATCH v4 13/20] btrfs-progs: scrub: Introduce function to scrub one data stripe Qu Wenruo
2017-05-25  6:21 ` [PATCH v4 14/20] btrfs-progs: scrub: Introduce function to verify parities Qu Wenruo
2017-05-25  6:22 ` [PATCH v4 15/20] btrfs-progs: extent-tree: Introduce function to check if there is any extent in given range Qu Wenruo
2017-05-25  6:22 ` [PATCH v4 16/20] btrfs-progs: scrub: Introduce function to recover data parity Qu Wenruo
2017-05-25  6:22 ` [PATCH v4 17/20] btrfs-progs: scrub: Introduce helper to write a full stripe Qu Wenruo
2017-05-25  6:22 ` [PATCH v4 18/20] btrfs-progs: scrub: Introduce a function to scrub one " Qu Wenruo
2017-05-25  6:22 ` [PATCH v4 19/20] btrfs-progs: scrub: Introduce function to check a whole block group Qu Wenruo
2017-05-25  6:22 ` [PATCH v4 20/20] btrfs-progs: scrub: Introduce offline scrub function Qu Wenruo
2017-05-26 18:37 ` [PATCH v4 00/20] Btrfs-progs offline scrub Goffredo Baroncelli
2017-05-29  0:21   ` Qu Wenruo
2017-05-29 16:52     ` Goffredo Baroncelli
2017-05-30 18:54 ` David Sterba
2017-05-31 14:07   ` David Sterba
2017-06-18  6:20 ` Lakshmipathi.G

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=20170525062205.11660-12-quwenruo@cn.fujitsu.com \
    --to=quwenruo@cn.fujitsu.com \
    --cc=dsterba@suse.cz \
    --cc=lakshmipathi.g@gmail.com \
    --cc=linux-btrfs@vger.kernel.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.