From patchwork Wed Feb 4 07:16:52 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 5774791 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 447589F2ED for ; Wed, 4 Feb 2015 07:20:19 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 4891D202AE for ; Wed, 4 Feb 2015 07:20:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 33F0E2024F for ; Wed, 4 Feb 2015 07:20:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753826AbbBDHUM (ORCPT ); Wed, 4 Feb 2015 02:20:12 -0500 Received: from cn.fujitsu.com ([59.151.112.132]:31549 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1753796AbbBDHUJ (ORCPT ); Wed, 4 Feb 2015 02:20:09 -0500 X-IronPort-AV: E=Sophos;i="5.04,848,1406563200"; d="scan'208";a="57098272" Received: from localhost (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 04 Feb 2015 15:15:30 +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 t147IIsG020247 for ; Wed, 4 Feb 2015 15:18:19 +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:06 +0800 From: Qu Wenruo To: Subject: [PATCH 6/7] btrfs-progs: Introduce new function reset_(one_root/roots)_csum() to reset one/all tree's csum in tree root. Date: Wed, 4 Feb 2015 15:16:52 +0800 Message-ID: <1423034213-14018-9-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_one_root_csum() will reset all csum in one root. And reset_roots_csum() will reset all csum of all trees in tree root. which provides the basis for later dangerous options. Signed-off-by: Qu Wenruo --- cmds-check.c | 176 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) diff --git a/cmds-check.c b/cmds-check.c index e4b4f4a..535a518 100644 --- a/cmds-check.c +++ b/cmds-check.c @@ -9193,6 +9193,182 @@ out: return ret; } +static int reset_one_root_csum(struct btrfs_root *root) +{ + struct btrfs_fs_info *fs_info = root->fs_info; + struct btrfs_path *path; + struct btrfs_key key; + struct extent_buffer *node; + u32 sectorsize = root->sectorsize; + int max_level = btrfs_root_level(&root->root_item); + int slot; + int i; + int ret = 0; + + path = btrfs_alloc_path(); + if (!path) + return -ENOMEM; + path->reada = 0; + + /* Iterate all levels except level 0 */ + for (i = 0; i < max_level; i++) { + int cur_level = max_level - i; + + path->lowest_level = cur_level; + key.offset = 0; + key.objectid = 0; + key.type = 0; + + /* + * btrfs_search_slot() can't work with lowest_level and cow, + * so use inslen=0 and cow=0 and later modify will be done + * directly to disk. + */ + ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); + if (ret < 0) + goto out; + + /* Iterate all node slots in this level */ + while (1) { + u64 bytenr; + + node = path->nodes[0]; + slot = path->slots[0]; + bytenr = btrfs_node_blockptr(node, slot); + + if (bytenr != round_down(bytenr, sectorsize)) { + bytenr = round_down(bytenr, sectorsize); + btrfs_set_node_blockptr(node, slot, bytenr); + ret = write_tree_block(NULL, root, node); + if (ret < 0) { + fprintf(stderr, + "Fail to write extent at %llu\n", + bytenr); + goto out; + } + } + ret = reset_tree_block_csum(fs_info, bytenr, + root->nodesize); + if (ret < 0) { + fprintf(stderr, + "Fail to reset csum for tree block at %llu\n", + bytenr); + goto out; + } + + ret = btrfs_next_slot(root, path, cur_level); + /* + * Error should not happen since higher level iteration + * has already reset the csum of this level. + * Either way, goto next level should be OK. + */ + if (ret) + break; + } + btrfs_release_path(path); + } +out: + btrfs_free_path(path); + return ret; +} + +static int reset_roots_csum(struct btrfs_root *tree_root) +{ + struct btrfs_fs_info *fs_info = tree_root->fs_info; + struct btrfs_key key; + struct btrfs_path *path; + struct btrfs_root_item *ri; + struct btrfs_root *root; + struct extent_buffer *node; + u32 nodesize = tree_root->nodesize; + u32 sectorsize = tree_root->sectorsize; + u64 bytenr; + int slot; + int ret = 0; + + path = btrfs_alloc_path(); + if (!path) + return -ENOMEM; + + key.objectid = 0; + key.offset = 0; + key.type = 0; + + /* + * Tree root is OK and we can do cow. But in case extent tree is + * corrupted, we still use the nocow method. + */ + ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0); + if (ret < 0) + goto out; + while (1) { + slot = path->slots[0]; + node = path->nodes[0]; + btrfs_item_key_to_cpu(node, &key, slot); + if (key.type != BTRFS_ROOT_ITEM_KEY) + goto next; + + /* + * skip tree reloc tree, it's not support by + * btrfs_read_fs_root() yet. + */ + if (key.objectid == BTRFS_TREE_RELOC_OBJECTID) + goto next; + + ri = btrfs_item_ptr(node, slot, struct btrfs_root_item); + bytenr = btrfs_disk_root_bytenr(node, ri); + + /* + * Check if the bytenr is aligned. + * If the error bit is only in the low all zero bits, + * no real damage will happen since we will align it. + */ + if (round_down(bytenr, sectorsize) != bytenr) { + bytenr = round_down(bytenr, sectorsize); + btrfs_set_disk_root_bytenr(node, ri, bytenr); + ret = write_tree_block(NULL, root, node); + if (ret < 0) { + fprintf(stderr, + "Fail to write extent at %llu\n", + bytenr); + goto out; + } + } + + ret = reset_tree_block_csum(fs_info, bytenr, nodesize); + if (ret < 0) { + fprintf(stderr, + "Failed to reset root for tree %llu, skip it\n", + key.objectid); + goto next; + } + key.offset = (u64)-1; + root = btrfs_read_fs_root(fs_info, &key); + if (!root || IS_ERR(root) || + !extent_buffer_uptodate(root->node)) { + fprintf(stderr, + "Root of tree %lld is still corrupted, skip it\n", + key.objectid); + goto next; + } + ret = reset_one_root_csum(root); + if (ret < 0) + goto next; + +next: + ret = btrfs_next_item(tree_root, path); + if (ret < 0) + goto out; + if (ret > 0) { + ret = 0; + goto out; + } + } +out: + btrfs_free_path(path); + return ret; +} + const char * const cmd_check_usage[] = { "btrfs check [options] ", "Check an unmounted btrfs filesystem.",