From patchwork Thu Jul 20 06:56:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gu Jinxiang X-Patchwork-Id: 9853937 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7E44660388 for ; Thu, 20 Jul 2017 06:57:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6E8792876E for ; Thu, 20 Jul 2017 06:57:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6354528775; Thu, 20 Jul 2017 06:57:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E17CA2876E for ; Thu, 20 Jul 2017 06:57:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933966AbdGTG47 (ORCPT ); Thu, 20 Jul 2017 02:56:59 -0400 Received: from cn.fujitsu.com ([59.151.112.132]:24333 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S933516AbdGTG44 (ORCPT ); Thu, 20 Jul 2017 02:56:56 -0400 X-IronPort-AV: E=Sophos;i="5.22,518,1449504000"; d="scan'208";a="21556355" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 20 Jul 2017 14:56:47 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (unknown [10.167.33.83]) by cn.fujitsu.com (Postfix) with ESMTP id 79EBE46B5DFF; Thu, 20 Jul 2017 14:56:45 +0800 (CST) Received: from localhost.localdomain (10.167.226.132) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.319.2; Thu, 20 Jul 2017 14:56:45 +0800 From: Gu Jinxiang To: CC: Qu Wenruo Subject: [PATCH v6 07/15] btrfs-progs: scrub: Introduce function to scrub one mirror-based extent Date: Thu, 20 Jul 2017 14:56:00 +0800 Message-ID: <20170720065608.27563-8-gujx@cn.fujitsu.com> X-Mailer: git-send-email 2.9.4 In-Reply-To: <20170720065608.27563-1-gujx@cn.fujitsu.com> References: <20170720065608.27563-1-gujx@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.132] X-yoursite-MailScanner-ID: 79EBE46B5DFF.AA5F9 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: gujx@cn.fujitsu.com Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Qu Wenruo Introduce a new function, scrub_one_extent(), as a wrapper to check one mirror-based extent. It will accept a btrfs_path parameter @path, which must point to a META/EXTENT_ITEM. And @start, @len, which must be a subset of META/EXTENT_ITEM. Signed-off-by: Qu Wenruo Signed-off-by: Gu Jinxiang --- scrub.c | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 147 insertions(+), 1 deletion(-) diff --git a/scrub.c b/scrub.c index 74bfe01..01e8b6b 100644 --- a/scrub.c +++ b/scrub.c @@ -434,7 +434,7 @@ static int recover_data_mirror(struct btrfs_fs_info *fs_info, 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) { + for_each_set_bit(bit, corrupt_bitmaps[i], len / sectorsize) { u64 cur = start + bit * sectorsize; int good; @@ -474,3 +474,149 @@ out: free(buf); return ret; } + +/* Btrfs only supports up to 2 copies of data, yet */ +#define BTRFS_MAX_COPIES 2 + +/* + * Check all copies of range @start, @len. + * Caller must ensure the range is covered by EXTENT_ITEM/METADATA_ITEM + * specified by leaf of @path. + * And @start, @len must be a subset of the EXTENT_ITEM/METADATA_ITEM. + * + * Return 0 if the range is all OK or recovered or recoverable. + * Return <0 if the range can't be recoverable. + */ +static int scrub_one_extent(struct btrfs_fs_info *fs_info, + struct btrfs_scrub_progress *scrub_ctx, + struct btrfs_path *path, u64 start, u64 len, + int write) +{ + struct btrfs_key key; + struct btrfs_extent_item *ei; + struct extent_buffer *leaf = path->nodes[0]; + u32 sectorsize = fs_info->sectorsize; + unsigned long *corrupt_bitmaps[BTRFS_MAX_COPIES] = { NULL }; + int slot = path->slots[0]; + int num_copies; + int meta_corrupted = 0; + int meta_good_mirror = 0; + int data_bad_mirror = 0; + u64 extent_start; + u64 extent_len; + int metadata = 0; + int i; + int ret = 0; + + btrfs_item_key_to_cpu(leaf, &key, slot); + if (key.type != BTRFS_METADATA_ITEM_KEY && + key.type != BTRFS_EXTENT_ITEM_KEY) + goto invalid_arg; + + extent_start = key.objectid; + if (key.type == BTRFS_METADATA_ITEM_KEY) { + extent_len = fs_info->nodesize; + metadata = 1; + } else { + extent_len = key.offset; + ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); + if (btrfs_extent_flags(leaf, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK) + metadata = 1; + } + if (start >= extent_start + extent_len || + start + len <= extent_start) + goto invalid_arg; + + for (i = 0; i < BTRFS_MAX_COPIES; i++) { + corrupt_bitmaps[i] = malloc( + calculate_bitmap_len(len / sectorsize)); + if (!corrupt_bitmaps[i]) + goto out; + } + num_copies = btrfs_num_copies(fs_info, start, len); + for (i = 1; i <= num_copies; i++) { + if (metadata) { + ret = check_tree_mirror(fs_info, scrub_ctx, + NULL, extent_start, i); + scrub_ctx->tree_extents_scrubbed++; + if (ret < 0) + meta_corrupted++; + else + meta_good_mirror = i; + } else { + ret = check_data_mirror(fs_info, scrub_ctx, NULL, start, + len, i, corrupt_bitmaps[i - 1]); + scrub_ctx->data_extents_scrubbed++; + } + } + + /* Metadata recover and report */ + if (metadata) { + if (!meta_corrupted) { + goto out; + } else if (meta_corrupted && meta_corrupted < num_copies) { + if (write) { + ret = recover_tree_mirror(fs_info, scrub_ctx, + start, meta_good_mirror); + if (ret < 0) { + error("failed to recover tree block at bytenr %llu", + start); + goto out; + } + printf("extent %llu len %llu REPAIRED: has corrupted mirror, repaired\n", + start, len); + goto out; + } + printf("extent %llu len %llu RECOVERABLE: has corrupted mirror, but is recoverable\n", + start, len); + goto out; + } else { + error("extent %llu len %llu CORRUPTED: all mirror(s) corrupted, can't be recovered", + start, len); + ret = -EIO; + goto out; + } + } + /* Data recover and report */ + for (i = 0; i < num_copies; i++) { + if (find_first_bit(corrupt_bitmaps[i], len / sectorsize) >= + len / sectorsize) + continue; + data_bad_mirror = i + 1; + } + /* All data sectors are good */ + if (!data_bad_mirror) { + ret = 0; + goto out; + } + + if (check_data_mirror_recoverable(fs_info, start, len, + sectorsize, corrupt_bitmaps)) { + if (write) { + ret = recover_data_mirror(fs_info, scrub_ctx, start, + len, corrupt_bitmaps); + if (ret < 0) { + error("failed to recover data extent at bytenr %llu len %llu", + start, len); + goto out; + } + printf("extent %llu len %llu REPARIED: has corrupted mirror, repaired\n", + start, len); + goto out; + } + printf("extent %llu len %llu RECOVERABLE: has corrupted mirror, recoverable\n", + start, len); + goto out; + } + error("extent %llu len %llu CORRUPTED, all mirror(s) corrupted, can't be repaired", + start, len); + ret = -EIO; +out: + for (i = 0; i < BTRFS_MAX_COPIES; i++) + kfree(corrupt_bitmaps[i]); + return ret; + +invalid_arg: + error("invalid parameter for %s", __func__); + return -EINVAL; +}