[RFC,v0.8,11/14] btrfs-progs: check/scrub: Introduce function to recover data parity
diff mbox

Message ID 20161017012743.9692-12-quwenruo@cn.fujitsu.com
State New
Headers show

Commit Message

Qu Wenruo Oct. 17, 2016, 1:27 a.m. UTC
Introduce function, recover_from_parities(), to recover data stripes.

However this function only support RAID5 yet, but should be good enough
for the scrub framework.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
---
 check/scrub.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

Patch
diff mbox

diff --git a/check/scrub.c b/check/scrub.c
index d8182d6..c965328 100644
--- a/check/scrub.c
+++ b/check/scrub.c
@@ -58,6 +58,9 @@  struct scrub_full_stripe {
 	/* Missing stripe index */
 	int missing_stripes[2];
 
+	/* Has already been recovered using parities */
+	unsigned int recovered:1;
+
 	struct scrub_stripe stripes[];
 };
 
@@ -467,3 +470,49 @@  out:
 	free(ptrs);
 	return ret;
 }
+
+static int recovery_from_parities(struct btrfs_fs_info *fs_info,
+				  struct btrfs_scrub_progress *scrub_ctx,
+				  struct scrub_full_stripe *fstripe)
+{
+	void **ptrs;
+	int nr_stripes = fstripe->nr_stripes;
+	int corrupted = -1;
+	int stripe_len = BTRFS_STRIPE_LEN;
+	int i;
+	int ret;
+
+	/* No need to recover */
+	if (!fstripe->err_read_stripes && !fstripe->err_csum_dstripes)
+		return 0;
+
+	/* Already recovered once, no more chance */
+	if (fstripe->recovered)
+		return -EINVAL;
+
+	if (fstripe->bg_type == BTRFS_BLOCK_GROUP_RAID6) {
+		/* Need to recover 2 stripes, not supported yet */
+		error("recover data stripes for RAID6 is not support yet");
+		return -ENOTTY;
+	}
+
+	/* Out of repair */
+	if (fstripe->err_read_stripes + fstripe->err_csum_dstripes > 1)
+		return -EINVAL;
+
+	ptrs = malloc(sizeof(void *) * fstripe->nr_stripes);
+	if (!ptrs)
+		return -ENOMEM;
+
+	/* Construct ptrs */
+	for (i = 0; i < nr_stripes; i++)
+		ptrs[i] = fstripe->stripes[i].data;
+	corrupted = fstripe->missing_stripes[0];
+
+	/* Recover the corrupted data csum */
+	ret = raid5_gen_result(nr_stripes, stripe_len, corrupted, ptrs);
+
+	fstripe->recovered = 1;
+	free(ptrs);
+	return ret;
+}