From patchwork Wed Feb 4 07:16:49 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 5774781 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 181929F399 for ; Wed, 4 Feb 2015 07:20:01 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 401032024F for ; Wed, 4 Feb 2015 07:20:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4E3C8202E5 for ; Wed, 4 Feb 2015 07:19:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753402AbbBDHTz (ORCPT ); Wed, 4 Feb 2015 02:19:55 -0500 Received: from cn.fujitsu.com ([59.151.112.132]:57071 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1753197AbbBDHTw (ORCPT ); Wed, 4 Feb 2015 02:19:52 -0500 X-IronPort-AV: E=Sophos;i="5.04,848,1406563200"; d="scan'208";a="57098269" Received: from localhost (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 04 Feb 2015 15:15:28 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (localhost.localdomain [127.0.0.1]) by edo.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id t147IGHR020233 for ; Wed, 4 Feb 2015 15:18:16 +0800 Received: from localhost.localdomain (10.167.226.33) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.181.6; Wed, 4 Feb 2015 15:19:04 +0800 From: Qu Wenruo To: Subject: [PATCH 4/5] btrfs-progs: Introduce new function reset_tree_block_csum() for later tree block csum reset. Date: Wed, 4 Feb 2015 15:16:49 +0800 Message-ID: <1423034213-14018-6-git-send-email-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.2.2 In-Reply-To: <1423034213-14018-1-git-send-email-quwenruo@cn.fujitsu.com> References: <1423034213-14018-1-git-send-email-quwenruo@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.33] Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP New function reset_tree_block_csum() will do the black magic to reset csum for a tree block in-place. This provides the basis to the whole tree csum resetting function. Signed-off-by: Qu Wenruo --- cmds-check.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/cmds-check.c b/cmds-check.c index 5817ecf..e4b4f4a 100644 --- a/cmds-check.c +++ b/cmds-check.c @@ -9137,6 +9137,62 @@ static u16 report_root_corrupted(struct btrfs_fs_info *fs_info, return ret; } +/* + * Black magics to reset the csum of a tree block. + * The evil part is to modify block without transaction/cow. + * + * Return 0 if the csum is OK or reset the csum + * Return <0 if error happened + */ +static int reset_tree_block_csum(struct btrfs_fs_info *fs_info, + u64 bytenr, u32 len) +{ + /* + * read_tree_block just use root as ladder to reach fs_info, + * so use chunk_root since it must be OK. + */ + struct btrfs_root *root = fs_info->chunk_root; + struct extent_buffer *eb; + char *buf = NULL; + u32 crc; + int ret = 0; + + eb = read_tree_block(root, bytenr, len, 0); + /* No need to do anything since its csum is OK */ + if (extent_buffer_uptodate(eb)) + goto out; + + buf = malloc(len); + if (!buf) { + ret = -ENOMEM; + goto out; + } + ret = read_data_from_disk(fs_info, buf, bytenr, len, 0); + if (ret < 0) + goto out; + crc = ~(u32)0; + crc = btrfs_csum_data(NULL, buf + BTRFS_CSUM_SIZE, crc, + len - BTRFS_CSUM_SIZE); + btrfs_csum_final(crc, buf); + ret = write_data_to_disk(fs_info, buf, bytenr, len, 0); + if (ret < 0) + goto out; + + /* Make sure now we can read the tree block */ + eb = read_tree_block(root, bytenr, len, 0); + if (!extent_buffer_uptodate(eb)) { + if (IS_ERR(eb)) + ret = PTR_ERR(eb); + else + ret = -EINVAL; + goto out; + } +out: + free(buf); + free_extent_buffer(eb); + return ret; +} + const char * const cmd_check_usage[] = { "btrfs check [options] ", "Check an unmounted btrfs filesystem.",