From patchwork Fri Oct 28 02:31:47 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 9400893 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 D70D7605EE for ; Fri, 28 Oct 2016 02:32:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C48C62A0E3 for ; Fri, 28 Oct 2016 02:32:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B71382A113; Fri, 28 Oct 2016 02:32:22 +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 569C52A0E3 for ; Fri, 28 Oct 2016 02:32:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1034247AbcJ1CcS (ORCPT ); Thu, 27 Oct 2016 22:32:18 -0400 Received: from cn.fujitsu.com ([222.73.24.84]:48358 "EHLO song.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-OK) by vger.kernel.org with ESMTP id S965899AbcJ1CcR (ORCPT ); Thu, 27 Oct 2016 22:32:17 -0400 X-IronPort-AV: E=Sophos;i="5.20,367,1444665600"; d="scan'208";a="932105" Received: from unknown (HELO cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 28 Oct 2016 10:32:04 +0800 Received: from adam-work.localdomain (unknown [10.167.226.34]) by cn.fujitsu.com (Postfix) with ESMTP id D22A841B4BCA; Fri, 28 Oct 2016 10:32:03 +0800 (CST) From: Qu Wenruo To: linux-btrfs@vger.kernel.org, dsterba@suse.cz Subject: [PATCH 11/19] btrfs-progs: check/scrub: Introduce function to scrub mirror based data blocks Date: Fri, 28 Oct 2016 10:31:47 +0800 Message-Id: <20161028023155.27336-12-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.10.1 In-Reply-To: <20161028023155.27336-1-quwenruo@cn.fujitsu.com> References: <20161028023155.27336-1-quwenruo@cn.fujitsu.com> MIME-Version: 1.0 X-yoursite-MailScanner-ID: D22A841B4BCA.ADA42 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: quwenruo@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 Introduce a new function, scrub_data_mirror(), to check mirror based data blocks. Signed-off-by: Qu Wenruo --- check/scrub.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/check/scrub.c b/check/scrub.c index ef94193..b4bb081 100644 --- a/check/scrub.c +++ b/check/scrub.c @@ -167,3 +167,72 @@ out: return ret; } +static int scrub_data_mirror(struct btrfs_fs_info *fs_info, + struct btrfs_scrub_progress *scrub_ctx, + char *data, u64 start, u64 len, int mirror) +{ + u64 cur = 0; + u32 csum; + u32 sectorsize = fs_info->tree_root->sectorsize; + char *buf = NULL; + int ret = 0; + int err = 0; + + if (!data) { + buf = malloc(len); + if (!buf) + return -ENOMEM; + /* Read out as much data as possible to speed up read */ + while (cur < len) { + u64 read_len = len - cur; + + ret = read_extent_data(fs_info->tree_root, buf + cur, + start + cur, &read_len, mirror); + if (ret < 0) { + error("failed to read out data at logical bytenr %llu mirror %d", + start + cur, mirror); + scrub_ctx->read_errors++; + goto out; + } + scrub_ctx->data_bytes_scrubbed += read_len; + cur += read_len; + } + } else { + buf = data; + } + + /* Check csum per-sectorsize */ + cur = 0; + while (cur < len) { + u32 data_csum = ~(u32)0; + + ret = btrfs_read_one_data_csum(fs_info, start + cur, &csum); + if (ret > 0) { + scrub_ctx->csum_discards++; + ret = 0; + + /* In case only some csum are missing */ + goto next; + } + data_csum = btrfs_csum_data(NULL, buf + cur, data_csum, + sectorsize); + btrfs_csum_final(data_csum, (u8 *)&data_csum); + if (data_csum != csum) { + error("data at bytenr %llu mirror %d csum mismatch, have %u expect %u", + start + cur, mirror, data_csum, csum); + err = 1; + scrub_ctx->csum_errors++; + cur += sectorsize; + continue; + } + scrub_ctx->data_bytes_scrubbed += sectorsize; +next: + cur += sectorsize; + } +out: + if (!data) + free(buf); + if (!ret && err) + return -EIO; + return ret; +}