linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Qu Wenruo <quwenruo@cn.fujitsu.com>
To: <linux-btrfs@vger.kernel.org>
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	[thread overview]
Message-ID: <1421649912-14539-8-git-send-email-quwenruo@cn.fujitsu.com> (raw)
In-Reply-To: <1421649912-14539-1-git-send-email-quwenruo@cn.fujitsu.com>

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 <quwenruo@cn.fujitsu.com>
---
 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;
 }
-- 
2.2.2


  parent reply	other threads:[~2015-01-19  6:47 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-19  6:45 [PATCH v2 00/10] Enhance btrfs-find-root and open_ctree() to provide better chance on damaged btrfs Qu Wenruo
2015-01-19  6:45 ` [PATCH v2 01/10] btrfs-progs: Cleanup, use bitshift instead of immediate number in btrfs_open_ctree_flags Qu Wenruo
2015-01-19  6:45 ` [PATCH v2 02/10] btrfs-progs: Add support to suppress tree block csum error output Qu Wenruo
2015-01-28 18:16   ` David Sterba
2015-01-29  0:58     ` Qu Wenruo
2015-01-19  6:45 ` [PATCH v2 03/10] btrfs-progs: Add new btrfs_open_ctree_flags CHUNK_ONLY Qu Wenruo
2015-01-19  6:45 ` [PATCH v2 04/10] btrfs-progs: Add new find-root.[ch] infrastructure Qu Wenruo
2015-01-19  6:45 ` [PATCH v2 05/10] btrfs-progs: Switch btrfs-find-root to use the new open_ctree flags Qu Wenruo
2015-01-19  6:45 ` [PATCH v2 06/10] btrfs-progs: Add better search generation judgment for btrfs-find-root Qu Wenruo
2015-01-19  6:45 ` Qu Wenruo [this message]
2015-01-19  6:45 ` [PATCH v2 08/10] btrfs-progs: Cleanup unneeded btrfs-find-root codes Qu Wenruo
2015-01-19  6:45 ` [PATCH v2 09/10] btrfs-progs: Add new option for btrfs-find-root to search through all the metadata extents Qu Wenruo
2015-01-19  6:45 ` [PATCH v2 10/10] btrfs-progs: Allow open_ctree use backup tree root or search it automatically if primary tree root is corrupted Qu Wenruo
2015-07-27 15:04   ` David Sterba
2015-07-28  0:34     ` Qu Wenruo
2015-07-28 12:00       ` David Sterba

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1421649912-14539-8-git-send-email-quwenruo@cn.fujitsu.com \
    --to=quwenruo@cn.fujitsu.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).