X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=find-root.c;h=7e4c51f670aeb89485ea653701f8d3b7726e555e;hb=634e73e9d3464e7a333eda10ef3e5652d8d32293;hp=20ff97211c613a3ecab2bbb0c09802fd9b2fcacc;hpb=82ac34581e8fae6ef3f84758361c75f5b30c576d;p=platform%2Fupstream%2Fbtrfs-progs.git diff --git a/find-root.c b/find-root.c index 20ff972..7e4c51f 100644 --- a/find-root.c +++ b/find-root.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011 Red Hat. All rights reserved. + * Copyright (C) 2015 Fujitsu. All rights reserved. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public @@ -16,418 +16,134 @@ * Boston, MA 021110-1307, USA. */ -#define _XOPEN_SOURCE 500 -#define _GNU_SOURCE 1 +#include "kerncompat.h" + #include #include -#include -#include -#include -#include -#include "kerncompat.h" #include "ctree.h" -#include "disk-io.h" -#include "print-tree.h" -#include "transaction.h" -#include "list.h" -#include "version.h" -#include "volumes.h" #include "utils.h" -#include "crc32c.h" - -static u16 csum_size = 0; -static u64 search_objectid = BTRFS_ROOT_TREE_OBJECTID; - -static void usage() -{ - fprintf(stderr, "Usage: find-roots [-o search_objectid] \n"); -} - -int csum_block(void *buf, u32 len) +#include "find-root.h" +#include "volumes.h" +#include "disk-io.h" +#include "extent-cache.h" + +/* Return value is the same as btrfs_find_root_search(). */ +static int add_eb_to_result(struct extent_buffer *eb, + struct cache_tree *result, + u32 nodesize, + struct btrfs_find_root_filter *filter, + struct cache_extent **match) { - char *result; - u32 crc = ~(u32)0; + u64 generation = btrfs_header_generation(eb); + u64 level = btrfs_header_level(eb); + u64 owner = btrfs_header_owner(eb); + u64 start = eb->start; + struct cache_extent *cache; + struct btrfs_find_root_gen_cache *gen_cache = NULL; int ret = 0; - result = malloc(csum_size * sizeof(char)); - if (!result) { - fprintf(stderr, "No memory\n"); - return 1; - } - - len -= BTRFS_CSUM_SIZE; - crc = crc32c(crc, buf + BTRFS_CSUM_SIZE, len); - btrfs_csum_final(crc, result); + if (owner != filter->objectid || level < filter->level || + generation < filter->generation) + return ret; - if (memcmp(buf, result, csum_size)) + /* + * Get the generation cache or create one + * + * NOTE: search_cache_extent() may return cache that doesn't cover + * the range. So we need an extra check to make sure it's the right one. + */ + cache = search_cache_extent(result, generation); + if (!cache || cache->start != generation) { + gen_cache = malloc(sizeof(*gen_cache)); + BUG_ON(!gen_cache); + cache = &gen_cache->cache; + cache->start = generation; + cache->size = 1; + cache->objectid = 0; + gen_cache->highest_level = 0; + cache_tree_init(&gen_cache->eb_tree); + + ret = insert_cache_extent(result, cache); + if (ret < 0) + return ret; + } + gen_cache = container_of(cache, struct btrfs_find_root_gen_cache, + cache); + + /* Higher level, clean tree and insert the new one */ + if (level > gen_cache->highest_level) { + free_extent_cache_tree(&gen_cache->eb_tree); + gen_cache->highest_level = level; + /* Fall into the insert routine */ + } + + /* Same level, insert it into the eb_tree */ + if (level == gen_cache->highest_level) { + ret = add_cache_extent(&gen_cache->eb_tree, + start, nodesize); + if (ret < 0 && ret != -EEXIST) + return ret; + ret = 0; + } + if (generation == filter->match_gen && + level == filter->match_level && + !filter->search_all) { ret = 1; - free(result); - return ret; -} - -static int close_all_devices(struct btrfs_fs_info *fs_info) -{ - struct list_head *list; - struct list_head *next; - struct btrfs_device *device; - - return 0; - - list = &fs_info->fs_devices->devices; - list_for_each(next, list) { - device = list_entry(next, struct btrfs_device, dev_list); - close(device->fd); - } - return 0; -} - -static struct btrfs_root *open_ctree_broken(int fd, const char *device) -{ - u32 sectorsize; - u32 nodesize; - u32 leafsize; - u32 blocksize; - u32 stripesize; - u64 generation; - struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root)); - struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root)); - struct btrfs_root *chunk_root = malloc(sizeof(struct btrfs_root)); - struct btrfs_root *dev_root = malloc(sizeof(struct btrfs_root)); - struct btrfs_root *csum_root = malloc(sizeof(struct btrfs_root)); - struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info)); - int ret; - struct btrfs_super_block *disk_super; - struct btrfs_fs_devices *fs_devices = NULL; - u64 total_devs; - u64 features; - - ret = btrfs_scan_one_device(fd, device, &fs_devices, - &total_devs, BTRFS_SUPER_INFO_OFFSET); - - if (ret) { - fprintf(stderr, "No valid Btrfs found on %s\n", device); - goto out; + if (match) + *match = search_cache_extent(&gen_cache->eb_tree, + start); } - - if (total_devs != 1) { - ret = btrfs_scan_for_fsid(fs_devices, total_devs, 1); - if (ret) - goto out; - } - - memset(fs_info, 0, sizeof(*fs_info)); - fs_info->tree_root = tree_root; - fs_info->extent_root = extent_root; - fs_info->chunk_root = chunk_root; - fs_info->dev_root = dev_root; - fs_info->csum_root = csum_root; - - fs_info->readonly = 1; - - extent_io_tree_init(&fs_info->extent_cache); - extent_io_tree_init(&fs_info->free_space_cache); - extent_io_tree_init(&fs_info->block_group_cache); - extent_io_tree_init(&fs_info->pinned_extents); - extent_io_tree_init(&fs_info->pending_del); - extent_io_tree_init(&fs_info->extent_ins); - cache_tree_init(&fs_info->fs_root_cache); - - cache_tree_init(&fs_info->mapping_tree.cache_tree); - - mutex_init(&fs_info->fs_mutex); - fs_info->fs_devices = fs_devices; - INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); - INIT_LIST_HEAD(&fs_info->space_info); - - __setup_root(4096, 4096, 4096, 4096, tree_root, - fs_info, BTRFS_ROOT_TREE_OBJECTID); - - ret = btrfs_open_devices(fs_devices, O_RDONLY); - if (ret) - goto out_cleanup; - - fs_info->super_bytenr = BTRFS_SUPER_INFO_OFFSET; - disk_super = &fs_info->super_copy; - ret = btrfs_read_dev_super(fs_devices->latest_bdev, - disk_super, BTRFS_SUPER_INFO_OFFSET); - if (ret) { - printk("No valid btrfs found\n"); - goto out_devices; - } - - memcpy(fs_info->fsid, &disk_super->fsid, BTRFS_FSID_SIZE); - - - features = btrfs_super_incompat_flags(disk_super) & - ~BTRFS_FEATURE_INCOMPAT_SUPP; - if (features) { - printk("couldn't open because of unsupported " - "option features (%Lx).\n", features); - goto out_devices; - } - - features = btrfs_super_incompat_flags(disk_super); - if (!(features & BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF)) { - features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; - btrfs_set_super_incompat_flags(disk_super, features); - } - - nodesize = btrfs_super_nodesize(disk_super); - leafsize = btrfs_super_leafsize(disk_super); - sectorsize = btrfs_super_sectorsize(disk_super); - stripesize = btrfs_super_stripesize(disk_super); - tree_root->nodesize = nodesize; - tree_root->leafsize = leafsize; - tree_root->sectorsize = sectorsize; - tree_root->stripesize = stripesize; - - ret = btrfs_read_sys_array(tree_root); - if (ret) - goto out_devices; - blocksize = btrfs_level_size(tree_root, - btrfs_super_chunk_root_level(disk_super)); - generation = btrfs_super_chunk_root_generation(disk_super); - - __setup_root(nodesize, leafsize, sectorsize, stripesize, - chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); - - chunk_root->node = read_tree_block(chunk_root, - btrfs_super_chunk_root(disk_super), - blocksize, generation); - if (!chunk_root->node) { - printk("Couldn't read chunk root\n"); - goto out_devices; - } - - read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid, - (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root->node), - BTRFS_UUID_SIZE); - - if (!(btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)) { - ret = btrfs_read_chunk_tree(chunk_root); - if (ret) - goto out_chunk; - } - - return fs_info->chunk_root; -out_chunk: - free_extent_buffer(fs_info->chunk_root->node); -out_devices: - close_all_devices(fs_info); -out_cleanup: - extent_io_tree_cleanup(&fs_info->extent_cache); - extent_io_tree_cleanup(&fs_info->free_space_cache); - extent_io_tree_cleanup(&fs_info->block_group_cache); - extent_io_tree_cleanup(&fs_info->pinned_extents); - extent_io_tree_cleanup(&fs_info->pending_del); - extent_io_tree_cleanup(&fs_info->extent_ins); -out: - free(tree_root); - free(extent_root); - free(chunk_root); - free(dev_root); - free(csum_root); - free(fs_info); - return NULL; -} - -static int search_iobuf(struct btrfs_root *root, void *iobuf, - size_t iobuf_size, off_t offset) -{ - u64 gen = btrfs_super_generation(&root->fs_info->super_copy); - u64 objectid = search_objectid; - u32 size = btrfs_super_nodesize(&root->fs_info->super_copy); - u8 level = root->fs_info->super_copy.root_level; - size_t block_off = 0; - - while (block_off < iobuf_size) { - void *block = iobuf + block_off; - struct btrfs_header *header = block; - u64 h_byte, h_level, h_gen, h_owner; - -// printf("searching %Lu\n", offset + block_off); - h_byte = le64_to_cpu(header->bytenr); - h_owner = le64_to_cpu(header->owner); - h_level = header->level; - h_gen = le64_to_cpu(header->generation); - - if (h_owner != objectid) - goto next; - if (h_byte != (offset + block_off)) - goto next; - if (h_level != level) - goto next; - if (csum_block(block, size)) { - fprintf(stderr, "Well block %Lu seems good, " - "but the csum doesn't match\n", - h_byte); - goto next; - } - if (h_gen != gen) { - fprintf(stderr, "Well block %Lu seems great, " - "but generation doesn't match, " - "have=%Lu, want=%Lu\n", h_byte, h_gen, - gen); - goto next; - } - printf("Found tree root at %Lu\n", h_byte); - return 0; -next: - block_off += size; - } - - return 1; -} - -static int read_physical(struct btrfs_root *root, int fd, u64 offset, - u64 bytenr, u64 len) -{ - char *iobuf = malloc(len); - ssize_t done; - size_t total_read = 0; - int ret = 1; - - if (!iobuf) { - fprintf(stderr, "No memory\n"); - return -1; - } - - while (total_read < len) { - done = pread64(fd, iobuf + total_read, len - total_read, - bytenr + total_read); - if (done < 0) { - fprintf(stderr, "Failed to read: %s\n", - strerror(errno)); - ret = -1; - goto out; - } - total_read += done; - } - - ret = search_iobuf(root, iobuf, total_read, offset); -out: - free(iobuf); return ret; } -static int find_root(struct btrfs_root *root) +/* + * Return 0 if iterating all the metadata extents. + * Return 1 if found root with given gen/level and set *match to it. + * Return <0 if error happens + */ +int btrfs_find_root_search(struct btrfs_fs_info *fs_info, + struct btrfs_find_root_filter *filter, + struct cache_tree *result, + struct cache_extent **match) { - struct btrfs_multi_bio *multi = NULL; - struct btrfs_device *device; - u64 metadata_offset = 0, metadata_size = 0; - off_t offset = 0; - off_t bytenr; - int fd; - int err; - int ret = 1; - - printf("Super think's the tree root is at %Lu, chunk root %Lu\n", - btrfs_super_root(&root->fs_info->super_copy), - btrfs_super_chunk_root(&root->fs_info->super_copy)); - - err = btrfs_next_metadata(&root->fs_info->mapping_tree, - &metadata_offset, &metadata_size); - if (err) - return ret; + struct extent_buffer *eb; + u64 chunk_offset = 0; + u64 chunk_size = 0; + u64 offset = 0; + u32 nodesize = btrfs_super_nodesize(fs_info->super_copy); + int suppress_errors = 0; + int ret = 0; - offset = metadata_offset; + suppress_errors = fs_info->suppress_check_block_errors; + fs_info->suppress_check_block_errors = 1; while (1) { - u64 map_length = 4096; - u64 type; - - if (offset > - btrfs_super_total_bytes(&root->fs_info->super_copy)) { - printf("Went past the fs size, exiting"); + if (filter->objectid != BTRFS_CHUNK_TREE_OBJECTID) + ret = btrfs_next_bg_metadata(fs_info, + &chunk_offset, + &chunk_size); + else + ret = btrfs_next_bg_system(fs_info, + &chunk_offset, + &chunk_size); + if (ret) { + if (ret == -ENOENT) + ret = 0; break; } - if (offset >= (metadata_offset + metadata_size)) { - err = btrfs_next_metadata(&root->fs_info->mapping_tree, - &metadata_offset, - &metadata_size); - if (err) { - printf("No more metdata to scan, exiting\n"); - break; - } - offset = metadata_offset; - } - err = __btrfs_map_block(&root->fs_info->mapping_tree, READ, - offset, &map_length, &type, - &multi, 0, NULL); - if (err) { - offset += map_length; - continue; - } - - if (!(type & BTRFS_BLOCK_GROUP_METADATA)) { - offset += map_length; - kfree(multi); - continue; - } - - device = multi->stripes[0].dev; - fd = device->fd; - bytenr = multi->stripes[0].physical; - kfree(multi); - - err = read_physical(root, fd, offset, bytenr, map_length); - if (!err) { - ret = 0; - break; - } else if (err < 0) { - ret = err; - break; - } - offset += map_length; - } - return ret; -} - -int main(int argc, char **argv) -{ - struct btrfs_root *root; - int dev_fd; - int opt; - int ret; - - while ((opt = getopt(argc, argv, "o:")) != -1) { - switch(opt) { - case 'o': - errno = 0; - search_objectid = (u64)strtoll(optarg, NULL, - 10); - if (errno) { - fprintf(stderr, "Error parsing " - "objectid\n"); - exit(1); - } - break; - default: - usage(); - exit(1); + for (offset = chunk_offset; + offset < chunk_offset + chunk_size; + offset += nodesize) { + eb = read_tree_block(fs_info, offset, 0); + if (!eb || IS_ERR(eb)) + continue; + ret = add_eb_to_result(eb, result, nodesize, filter, + match); + free_extent_buffer(eb); + if (ret) + goto out; } } - - if (optind >= argc) { - usage(); - exit(1); - } - - dev_fd = open(argv[optind], O_RDONLY); - if (dev_fd < 0) { - fprintf(stderr, "Failed to open device %s\n", argv[optind]); - exit(1); - } - - root = open_ctree_broken(dev_fd, argv[optind]); - close(dev_fd); - - if (!root) { - fprintf(stderr, "Open ctree failed\n"); - exit(1); - } - - csum_size = btrfs_super_csum_size(&root->fs_info->super_copy); - ret = find_root(root); - close_ctree(root); +out: + fs_info->suppress_check_block_errors = suppress_errors; return ret; }