btrfs-progs: check/original: Avoid infinite loop when failed to repair inode
[btrfs-progs-unstable/devel.git] / btrfs-find-root.c
blobe2d2e70c408c94ce4b4e5f6eaf3e94584b803e2d
1 /*
2 * Copyright (C) 2011 Red Hat. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <sys/stat.h>
24 #include <zlib.h>
25 #include <getopt.h>
27 #include "kerncompat.h"
28 #include "ctree.h"
29 #include "disk-io.h"
30 #include "print-tree.h"
31 #include "transaction.h"
32 #include "list.h"
33 #include "volumes.h"
34 #include "utils.h"
35 #include "crc32c.h"
36 #include "extent-cache.h"
37 #include "find-root.h"
38 #include "help.h"
40 static void find_root_usage(void)
42 fprintf(stderr, "Usage: find-roots [-a] [-o search_objectid] "
43 "[ -g search_generation ] [ -l search_level ] <device>\n");
47 * Get reliable generation and level for given root.
49 * We have two sources of gen/level: superblock and tree root.
50 * superblock include the following level:
51 * Root, chunk, log
52 * and the following generations:
53 * Root, chunk, uuid
54 * Other gen/leven can only be read from its btrfs_tree_root if possible.
56 * Currently we only believe things from superblock.
58 static void get_root_gen_and_level(u64 objectid, struct btrfs_fs_info *fs_info,
59 u64 *ret_gen, u8 *ret_level)
61 struct btrfs_super_block *super = fs_info->super_copy;
62 u64 gen = (u64)-1;
63 u8 level = (u8)-1;
65 switch (objectid) {
66 case BTRFS_ROOT_TREE_OBJECTID:
67 level = btrfs_super_root_level(super);
68 gen = btrfs_super_generation(super);
69 break;
70 case BTRFS_CHUNK_TREE_OBJECTID:
71 level = btrfs_super_chunk_root_level(super);
72 gen = btrfs_super_chunk_root_generation(super);
73 break;
74 case BTRFS_TREE_LOG_OBJECTID:
75 level = btrfs_super_log_root_level(super);
76 gen = btrfs_super_log_root_transid(super);
77 break;
78 case BTRFS_UUID_TREE_OBJECTID:
79 gen = btrfs_super_uuid_tree_generation(super);
80 break;
82 if (gen != (u64)-1) {
83 printf("Superblock thinks the generation is %llu\n", gen);
84 if (ret_gen)
85 *ret_gen = gen;
86 } else {
87 printf("Superblock doesn't contain generation info for root %llu\n",
88 objectid);
90 if (level != (u8)-1) {
91 printf("Superblock thinks the level is %u\n", level);
92 if (ret_level)
93 *ret_level = level;
94 } else {
95 printf("Superblock doesn't contain the level info for root %llu\n",
96 objectid);
100 static void print_one_result(struct cache_extent *tree_block,
101 u8 level, u64 generation,
102 struct btrfs_find_root_filter *filter)
104 int unsure = 0;
106 if (filter->match_gen == (u64)-1 || filter->match_level == (u8)-1)
107 unsure = 1;
108 printf("Well block %llu(gen: %llu level: %u) seems good, ",
109 tree_block->start, generation, level);
110 if (unsure)
111 printf("but we are unsure about the correct generation/level\n");
112 else if (level == filter->match_level &&
113 generation == filter->match_gen)
114 printf("and it matches superblock\n");
115 else
116 printf("but generation/level doesn't match, want gen: %llu level: %u\n",
117 filter->match_gen, filter->match_level);
120 static void print_find_root_result(struct cache_tree *result,
121 struct btrfs_find_root_filter *filter)
123 struct btrfs_find_root_gen_cache *gen_cache;
124 struct cache_extent *cache;
125 struct cache_extent *tree_block;
126 u64 generation = 0;
127 u8 level = 0;
129 for (cache = last_cache_extent(result);
130 cache; cache = prev_cache_extent(cache)) {
131 gen_cache = container_of(cache,
132 struct btrfs_find_root_gen_cache, cache);
133 level = gen_cache->highest_level;
134 generation = cache->start;
135 /* For exact found one, skip it as it's output before */
136 if (level == filter->match_level &&
137 generation == filter->match_gen &&
138 !filter->search_all)
139 continue;
140 for (tree_block = last_cache_extent(&gen_cache->eb_tree);
141 tree_block; tree_block = prev_cache_extent(tree_block))
142 print_one_result(tree_block, level, generation, filter);
146 int main(int argc, char **argv)
148 struct btrfs_fs_info *fs_info;
149 struct btrfs_find_root_filter filter = {0};
150 struct cache_tree result;
151 struct cache_extent *found;
152 int ret;
154 /* Default to search root tree */
155 filter.objectid = BTRFS_ROOT_TREE_OBJECTID;
156 filter.match_gen = (u64)-1;
157 filter.match_level = (u8)-1;
158 while (1) {
159 static const struct option long_options[] = {
160 { "help", no_argument, NULL, GETOPT_VAL_HELP},
161 { NULL, 0, NULL, 0 }
163 int c = getopt_long(argc, argv, "al:o:g:", long_options, NULL);
165 if (c < 0)
166 break;
168 switch (c) {
169 case 'a':
170 filter.search_all = 1;
171 break;
172 case 'o':
173 filter.objectid = arg_strtou64(optarg);
174 break;
175 case 'g':
176 filter.generation = arg_strtou64(optarg);
177 break;
178 case 'l':
179 filter.level = arg_strtou64(optarg);
180 break;
181 case GETOPT_VAL_HELP:
182 default:
183 find_root_usage();
184 exit(c != GETOPT_VAL_HELP);
188 set_argv0(argv);
189 if (check_argc_min(argc - optind, 1)) {
190 find_root_usage();
191 exit(1);
194 fs_info = open_ctree_fs_info(argv[optind], 0, 0, 0,
195 OPEN_CTREE_CHUNK_ROOT_ONLY |
196 OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR);
197 if (!fs_info) {
198 error("open ctree failed");
199 exit(1);
201 cache_tree_init(&result);
203 get_root_gen_and_level(filter.objectid, fs_info,
204 &filter.match_gen, &filter.match_level);
205 ret = btrfs_find_root_search(fs_info, &filter, &result, &found);
206 if (ret < 0) {
207 fprintf(stderr, "Fail to search the tree root: %s\n",
208 strerror(-ret));
209 goto out;
211 if (ret > 0) {
212 printf("Found tree root at %llu gen %llu level %u\n",
213 found->start, filter.match_gen, filter.match_level);
214 ret = 0;
216 print_find_root_result(&result, &filter);
217 out:
218 btrfs_find_root_free(&result);
219 close_ctree_fs_info(fs_info);
220 btrfs_close_all_devices();
221 return ret;