X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;ds=sidebyside;f=btrfs-find-root.c;h=e2d2e70c408c94ce4b4e5f6eaf3e94584b803e2d;hb=992aa558397e519d2251cdc6c4d3a9e019b240a0;hp=b74816454402a516e4751007eaf134a0152a15c1;hpb=e08b2a5845e56fbd9441f7d734ca6b6143f7750d;p=platform%2Fupstream%2Fbtrfs-progs.git diff --git a/btrfs-find-root.c b/btrfs-find-root.c index b748164..e2d2e70 100644 --- a/btrfs-find-root.c +++ b/btrfs-find-root.c @@ -22,6 +22,8 @@ #include #include #include +#include + #include "kerncompat.h" #include "ctree.h" #include "disk-io.h" @@ -33,10 +35,11 @@ #include "crc32c.h" #include "extent-cache.h" #include "find-root.h" +#include "help.h" -static void usage(void) +static void find_root_usage(void) { - fprintf(stderr, "Usage: find-roots [-o search_objectid] " + fprintf(stderr, "Usage: find-roots [-a] [-o search_objectid] " "[ -g search_generation ] [ -l search_level ] \n"); } @@ -67,7 +70,6 @@ static void get_root_gen_and_level(u64 objectid, struct btrfs_fs_info *fs_info, case BTRFS_CHUNK_TREE_OBJECTID: level = btrfs_super_chunk_root_level(super); gen = btrfs_super_chunk_root_generation(super); - printf("Search for chunk root is not supported yet\n"); break; case BTRFS_TREE_LOG_OBJECTID: level = btrfs_super_log_root_level(super); @@ -107,6 +109,9 @@ static void print_one_result(struct cache_extent *tree_block, tree_block->start, generation, level); if (unsure) printf("but we are unsure about the correct generation/level\n"); + else if (level == filter->match_level && + generation == filter->match_gen) + printf("and it matches superblock\n"); else printf("but generation/level doesn't match, want gen: %llu level: %u\n", filter->match_gen, filter->match_level); @@ -127,8 +132,10 @@ static void print_find_root_result(struct cache_tree *result, struct btrfs_find_root_gen_cache, cache); level = gen_cache->highest_level; generation = cache->start; + /* For exact found one, skip it as it's output before */ if (level == filter->match_level && - generation == filter->match_gen) + generation == filter->match_gen && + !filter->search_all) continue; for (tree_block = last_cache_extent(&gen_cache->eb_tree); tree_block; tree_block = prev_cache_extent(tree_block)) @@ -138,51 +145,64 @@ static void print_find_root_result(struct cache_tree *result, int main(int argc, char **argv) { - struct btrfs_root *root; + struct btrfs_fs_info *fs_info; 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': - filter.objectid = arg_strtou64(optarg); - break; - case 'g': - filter.generation = arg_strtou64(optarg); - break; - case 'l': - filter.level = arg_strtou64(optarg); - break; - default: - usage(); - exit(1); + while (1) { + static const struct option long_options[] = { + { "help", no_argument, NULL, GETOPT_VAL_HELP}, + { NULL, 0, NULL, 0 } + }; + int c = getopt_long(argc, argv, "al:o:g:", long_options, NULL); + + if (c < 0) + break; + + switch (c) { + case 'a': + filter.search_all = 1; + break; + case 'o': + filter.objectid = arg_strtou64(optarg); + break; + case 'g': + filter.generation = arg_strtou64(optarg); + break; + case 'l': + filter.level = arg_strtou64(optarg); + break; + case GETOPT_VAL_HELP: + default: + find_root_usage(); + exit(c != GETOPT_VAL_HELP); } } set_argv0(argv); - argc = argc - optind; - if (check_argc_min(argc, 1)) { - usage(); + if (check_argc_min(argc - optind, 1)) { + find_root_usage(); exit(1); } - root = open_ctree(argv[optind], 0, OPEN_CTREE_CHUNK_ROOT_ONLY); - if (!root) { - fprintf(stderr, "Open ctree failed\n"); + fs_info = open_ctree_fs_info(argv[optind], 0, 0, 0, + OPEN_CTREE_CHUNK_ROOT_ONLY | + OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR); + if (!fs_info) { + error("open ctree failed"); exit(1); } cache_tree_init(&result); - get_root_gen_and_level(filter.objectid, root->fs_info, + get_root_gen_and_level(filter.objectid, fs_info, &filter.match_gen, &filter.match_level); - ret = btrfs_find_root_search(root, &filter, &result, &found); + ret = btrfs_find_root_search(fs_info, &filter, &result, &found); if (ret < 0) { fprintf(stderr, "Fail to search the tree root: %s\n", strerror(-ret)); @@ -196,6 +216,7 @@ int main(int argc, char **argv) print_find_root_result(&result, &filter); out: btrfs_find_root_free(&result); - close_ctree(root); + close_ctree_fs_info(fs_info); + btrfs_close_all_devices(); return ret; }