From patchwork Mon Jan 19 06:45:09 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 5654961 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 79A53C058D for ; Mon, 19 Jan 2015 06:52:32 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 8A589202A1 for ; Mon, 19 Jan 2015 06:52:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9D33B2028D for ; Mon, 19 Jan 2015 06:52:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751663AbbASGsk (ORCPT ); Mon, 19 Jan 2015 01:48:40 -0500 Received: from cn.fujitsu.com ([59.151.112.132]:60017 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1751430AbbASGrX (ORCPT ); Mon, 19 Jan 2015 01:47:23 -0500 X-IronPort-AV: E=Sophos;i="5.04,848,1406563200"; d="scan'208";a="56253742" Received: from unknown (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 19 Jan 2015 14:43:54 +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 t0J6kjbF022581 for ; Mon, 19 Jan 2015 14:46:45 +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; Mon, 19 Jan 2015 14:47:25 +0800 From: Qu Wenruo To: Subject: [PATCH v2 07/10] btrfs-progs: Swith btrfs-find-root to use the find-root infrastructure. Date: Mon, 19 Jan 2015 14:45:09 +0800 Message-ID: <1421649912-14539-8-git-send-email-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.2.2 In-Reply-To: <1421649912-14539-1-git-send-email-quwenruo@cn.fujitsu.com> References: <1421649912-14539-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 Since the new find-root infrastructure is here with better root judgement with less codes, just switch to it. To switch to the new infrastructure, new print function is added and output format is slighted changed. Signed-off-by: Qu Wenruo --- btrfs-find-root.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 71 insertions(+), 10 deletions(-) diff --git a/btrfs-find-root.c b/btrfs-find-root.c index 45c48c2..c451c7b 100644 --- a/btrfs-find-root.c +++ b/btrfs-find-root.c @@ -34,6 +34,8 @@ #include "volumes.h" #include "utils.h" #include "crc32c.h" +#include "extent-cache.h" +#include "find-root.h" static u16 csum_size = 0; static u64 search_objectid = BTRFS_ROOT_TREE_OBJECTID; @@ -333,22 +335,70 @@ static void get_root_gen_and_level(u64 objectid, struct btrfs_fs_info *fs_info, } } +static void print_one_result(struct cache_extent *tree_block, + u8 level, u64 generation, + struct btrfs_find_root_filter *filter) +{ + int unsure = 0; + + if (filter->match_gen == (u64)-1 || filter->match_level == (u8)-1) + unsure = 1; + printf("Well block %llu(gen: %llu level: %u) seems good, ", + tree_block->start, generation, level); + if (unsure) + printf("but we are unsure about the correct generation/level\n"); + else + printf("but generation/level doesn't match, want gen: %llu level: %u\n", + filter->match_gen, filter->match_level); +} + +static void print_find_root_result(struct cache_tree *result, + struct btrfs_find_root_filter *filter) +{ + struct btrfs_find_root_gen_cache *gen_cache; + struct cache_extent *cache; + struct cache_extent *tree_block; + u64 generation = 0; + u8 level = 0; + + for (cache = last_cache_extent(result); + cache; cache = prev_cache_extent(cache)) { + gen_cache = container_of(cache, + struct btrfs_find_root_gen_cache, cache); + level = gen_cache->highest_level; + generation = cache->start; + if (level == filter->match_level && + generation == filter->match_gen) + continue; + for (tree_block = last_cache_extent(&gen_cache->eb_tree); + tree_block; tree_block = prev_cache_extent(tree_block)) + print_one_result(tree_block, level, generation, filter); + } +} + int main(int argc, char **argv) { struct btrfs_root *root; + struct btrfs_find_root_filter filter = {0}; + struct cache_tree result; + struct cache_extent *found; int opt; int ret; + /* Default to search root tree */ + filter.objectid = BTRFS_ROOT_TREE_OBJECTID; + filter.match_gen = (u64)-1; + filter.match_level = (u8)-1; while ((opt = getopt(argc, argv, "l:o:g:")) != -1) { switch(opt) { case 'o': - search_objectid = arg_strtou64(optarg); + filter.objectid = arg_strtou64(optarg); break; case 'g': - search_generation = arg_strtou64(optarg); + filter.generation = arg_strtou64(optarg); break; case 'l': - search_level = arg_strtou64(optarg); + filter.level = arg_strtou64(optarg); break; default: usage(); @@ -369,13 +419,24 @@ int main(int argc, char **argv) fprintf(stderr, "Open ctree failed\n"); exit(1); } - - if (search_generation == 0) - get_root_gen_and_level(search_objectid, root->fs_info, - &search_generation, NULL); - - csum_size = btrfs_super_csum_size(root->fs_info->super_copy); - ret = find_root(root); + cache_tree_init(&result); + + get_root_gen_and_level(filter.objectid, root->fs_info, + &filter.match_gen, &filter.match_level); + ret = btrfs_find_root_search(root, &filter, &result, &found); + if (ret < 0) { + fprintf(stderr, "Fail to search the tree root: %s\n", + strerror(-ret)); + goto out; + } + if (ret > 0) { + printf("Found tree root at %llu gen %llu level %u\n", + found->start, filter.match_gen, filter.match_level); + ret = 0; + } + print_find_root_result(&result, &filter); +out: + btrfs_find_root_free(&result); close_ctree(root); return ret; }