void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *eb, int follow) { int i; u32 nr; u32 size; struct btrfs_disk_key disk_key; struct btrfs_key key; if (!eb) return; nr = btrfs_header_nritems(eb); if (btrfs_is_leaf(eb)) { btrfs_print_leaf(root, eb); return; } printf("node %llu level %d items %d free %u generation %llu owner %llu\n", (unsigned long long)eb->start, btrfs_header_level(eb), nr, (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr, (unsigned long long)btrfs_header_generation(eb), (unsigned long long)btrfs_header_owner(eb)); print_uuids(eb); fflush(stdout); size = btrfs_level_size(root, btrfs_header_level(eb) - 1); for (i = 0; i < nr; i++) { u64 blocknr = btrfs_node_blockptr(eb, i); btrfs_node_key(eb, &disk_key, i); btrfs_disk_key_to_cpu(&key, &disk_key); printf("\t"); btrfs_print_key(&disk_key); printf(" block %llu (%llu) gen %llu\n", (unsigned long long)blocknr, (unsigned long long)blocknr / size, (unsigned long long)btrfs_node_ptr_generation(eb, i)); fflush(stdout); } if (!follow) return; for (i = 0; i < nr; i++) { struct extent_buffer *next = read_tree_block(root, btrfs_node_blockptr(eb, i), size, btrfs_node_ptr_generation(eb, i)); if (!next) { fprintf(stderr, "failed to read %llu in tree %llu\n", (unsigned long long)btrfs_node_blockptr(eb, i), (unsigned long long)btrfs_header_owner(eb)); continue; } if (btrfs_is_leaf(next) && btrfs_header_level(eb) != 1) BUG(); if (btrfs_header_level(next) != btrfs_header_level(eb) - 1) BUG(); btrfs_print_tree(root, next, 1); free_extent_buffer(next); } }
void btrfs_print_tree(struct btrfs_fs_info *fs_info, struct extent_buffer *c) { int i; u32 nr; struct btrfs_key key; int level; if (!c) return; nr = btrfs_header_nritems(c); level = btrfs_header_level(c); if (level == 0) { btrfs_print_leaf(fs_info, c); return; } btrfs_info(fs_info, "node %llu level %d total ptrs %d free spc %u", btrfs_header_bytenr(c), level, nr, (u32)BTRFS_NODEPTRS_PER_BLOCK(fs_info) - nr); for (i = 0; i < nr; i++) { btrfs_node_key_to_cpu(c, &key, i); pr_info("\tkey %d (%llu %u %llu) block %llu\n", i, key.objectid, key.type, key.offset, btrfs_node_blockptr(c, i)); } for (i = 0; i < nr; i++) { struct extent_buffer *next = read_tree_block(fs_info, btrfs_node_blockptr(c, i), btrfs_node_ptr_generation(c, i)); if (IS_ERR(next)) { continue; } else if (!extent_buffer_uptodate(next)) { free_extent_buffer(next); continue; } if (btrfs_is_leaf(next) && level != 1) BUG(); if (btrfs_header_level(next) != level - 1) BUG(); btrfs_print_tree(fs_info, next); free_extent_buffer(next); } }
void btrfs_print_tree(struct btrfs_root *root, struct btrfs_buffer *t) { unsigned int i; u32 nr; struct btrfs_node *c; if (!t) return; c = &t->node; nr = btrfs_header_nritems(&c->header); if (btrfs_is_leaf(c)) { btrfs_print_leaf(root, (struct btrfs_leaf *)c); return; } printf("node %llu level %d ptrs %d free %u generation %llu owner %llu\n", (u64)t->blocknr, btrfs_header_level(&c->header), nr, (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr, (u64)btrfs_header_generation(&c->header), (u64)btrfs_header_owner(&c->header)); fflush(stdout); for (i = 0; i < nr; i++) { printf("\tkey %d (%llu %x %llu) block %llu\n", i, (u64)c->ptrs[i].key.objectid, c->ptrs[i].key.flags, (u64)c->ptrs[i].key.offset, (u64)btrfs_node_blockptr(c, i)); fflush(stdout); } for (i = 0; i < nr; i++) { struct btrfs_buffer *next_buf = read_tree_block(root, btrfs_node_blockptr(c, i)); struct btrfs_node *next = &next_buf->node; if (btrfs_is_leaf(next) && btrfs_header_level(&c->header) != 1) BUG(); if (btrfs_header_level(&next->header) != btrfs_header_level(&c->header) - 1) BUG(); btrfs_print_tree(root, next_buf); btrfs_block_release(root, next_buf); } }
void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *c) { int i; u32 nr; struct btrfs_key key; int level; if (!c) return; nr = btrfs_header_nritems(c); level = btrfs_header_level(c); if (level == 0) { btrfs_print_leaf(root, c); return; } printk(KERN_INFO "node %llu level %d total ptrs %d free spc %u\n", (unsigned long long)btrfs_header_bytenr(c), level, nr, (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr); for (i = 0; i < nr; i++) { btrfs_node_key_to_cpu(c, &key, i); printk(KERN_INFO "\tkey %d (%llu %u %llu) block %llu\n", i, (unsigned long long)key.objectid, key.type, (unsigned long long)key.offset, (unsigned long long)btrfs_node_blockptr(c, i)); } for (i = 0; i < nr; i++) { struct extent_buffer *next = read_tree_block(root, btrfs_node_blockptr(c, i), btrfs_level_size(root, level - 1), btrfs_node_ptr_generation(c, i)); if (btrfs_is_leaf(next) && level != 1) BUG(); if (btrfs_header_level(next) != level - 1) BUG(); btrfs_print_tree(root, next); free_extent_buffer(next); } }
int main(int ac, char **av) { struct btrfs_key ins; struct btrfs_key last = { (u64)-1, 0, 0}; char *buf; int i; int num; int ret; int run_size = 300000; int max_key = 100000000; int tree_size = 2; struct btrfs_path path; struct btrfs_root *root; struct btrfs_trans_handle *trans; buf = malloc(512); memset(buf, 0, 512); radix_tree_init(); root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES); if (!root) { fprintf(stderr, "Open ctree failed\n"); exit(1); } trans = btrfs_start_transaction(root, 1); srand(55); btrfs_set_key_type(&ins, BTRFS_STRING_ITEM_KEY); for (i = 0; i < run_size; i++) { num = next_key(i, max_key); // num = i; sprintf(buf, "string-%d", num); if (i % 10000 == 0) fprintf(stderr, "insert %d:%d\n", num, i); ins.objectid = num; ins.offset = 0; ret = btrfs_insert_item(trans, root, &ins, buf, 512); if (!ret) tree_size++; if (i == run_size - 5) { btrfs_commit_transaction(trans, root); trans = btrfs_start_transaction(root, 1); } } btrfs_commit_transaction(trans, root); close_ctree(root); exit(1); root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES); if (!root) { fprintf(stderr, "Open ctree failed\n"); exit(1); } printf("starting search\n"); srand(55); for (i = 0; i < run_size; i++) { num = next_key(i, max_key); ins.objectid = num; btrfs_init_path(&path); if (i % 10000 == 0) fprintf(stderr, "search %d:%d\n", num, i); ret = btrfs_search_slot(NULL, root, &ins, &path, 0, 0); if (ret) { btrfs_print_tree(root, root->node, 1); printf("unable to find %d\n", num); exit(1); } btrfs_release_path(&path); } close_ctree(root); root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES); if (!root) { fprintf(stderr, "Open ctree failed\n"); exit(1); } printf("node %p level %d total ptrs %d free spc %lu\n", root->node, btrfs_header_level(root->node), btrfs_header_nritems(root->node), (unsigned long)BTRFS_NODEPTRS_PER_BLOCK(root) - btrfs_header_nritems(root->node)); printf("all searches good, deleting some items\n"); i = 0; srand(55); trans = btrfs_start_transaction(root, 1); for (i = 0 ; i < run_size/4; i++) { num = next_key(i, max_key); ins.objectid = num; btrfs_init_path(&path); ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1); if (!ret) { if (i % 10000 == 0) fprintf(stderr, "del %d:%d\n", num, i); ret = btrfs_del_item(trans, root, &path); if (ret != 0) BUG(); tree_size--; } btrfs_release_path(&path); } btrfs_commit_transaction(trans, root); close_ctree(root); root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES); if (!root) { fprintf(stderr, "Open ctree failed\n"); exit(1); } trans = btrfs_start_transaction(root, 1); srand(128); for (i = 0; i < run_size; i++) { num = next_key(i, max_key); sprintf(buf, "string-%d", num); ins.objectid = num; if (i % 10000 == 0) fprintf(stderr, "insert %d:%d\n", num, i); ret = btrfs_insert_item(trans, root, &ins, buf, 512); if (!ret) tree_size++; } btrfs_commit_transaction(trans, root); close_ctree(root); root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES); if (!root) { fprintf(stderr, "Open ctree failed\n"); exit(1); } srand(128); printf("starting search2\n"); for (i = 0; i < run_size; i++) { num = next_key(i, max_key); ins.objectid = num; btrfs_init_path(&path); if (i % 10000 == 0) fprintf(stderr, "search %d:%d\n", num, i); ret = btrfs_search_slot(NULL, root, &ins, &path, 0, 0); if (ret) { btrfs_print_tree(root, root->node, 1); printf("unable to find %d\n", num); exit(1); } btrfs_release_path(&path); } printf("starting big long delete run\n"); trans = btrfs_start_transaction(root, 1); while(root->node && btrfs_header_nritems(root->node) > 0) { struct extent_buffer *leaf; int slot; ins.objectid = (u64)-1; btrfs_init_path(&path); ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1); if (ret == 0) BUG(); leaf = path.nodes[0]; slot = path.slots[0]; if (slot != btrfs_header_nritems(leaf)) BUG(); while(path.slots[0] > 0) { path.slots[0] -= 1; slot = path.slots[0]; leaf = path.nodes[0]; btrfs_item_key_to_cpu(leaf, &last, slot); if (tree_size % 10000 == 0) printf("big del %d:%d\n", tree_size, i); ret = btrfs_del_item(trans, root, &path); if (ret != 0) { printf("del_item returned %d\n", ret); BUG(); } tree_size--; } btrfs_release_path(&path); } /* printf("previous tree:\n"); btrfs_print_tree(root, root->commit_root); printf("map before commit\n"); btrfs_print_tree(root->extent_root, root->extent_root->node); */ btrfs_commit_transaction(trans, root); printf("tree size is now %d\n", tree_size); printf("root %p commit root %p\n", root->node, root->commit_root); btrfs_print_tree(root, root->node, 1); close_ctree(root); return 0; }
void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *eb, int follow) { u32 i; u32 nr; u32 size; struct btrfs_disk_key disk_key; struct btrfs_key key; struct extent_buffer *next; if (!eb) return; nr = btrfs_header_nritems(eb); if (btrfs_is_leaf(eb)) { btrfs_print_leaf(root, eb); return; } printf("node %llu level %d items %d free %u generation %llu owner %llu\n", (unsigned long long)eb->start, btrfs_header_level(eb), nr, (u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr, (unsigned long long)btrfs_header_generation(eb), (unsigned long long)btrfs_header_owner(eb)); print_uuids(eb); fflush(stdout); size = root->nodesize; for (i = 0; i < nr; i++) { u64 blocknr = btrfs_node_blockptr(eb, i); btrfs_node_key(eb, &disk_key, i); btrfs_disk_key_to_cpu(&key, &disk_key); printf("\t"); btrfs_print_key(&disk_key); printf(" block %llu (%llu) gen %llu\n", (unsigned long long)blocknr, (unsigned long long)blocknr / size, (unsigned long long)btrfs_node_ptr_generation(eb, i)); fflush(stdout); } if (!follow) return; for (i = 0; i < nr; i++) { next = read_tree_block(root, btrfs_node_blockptr(eb, i), size, btrfs_node_ptr_generation(eb, i)); if (!extent_buffer_uptodate(next)) { fprintf(stderr, "failed to read %llu in tree %llu\n", (unsigned long long)btrfs_node_blockptr(eb, i), (unsigned long long)btrfs_header_owner(eb)); continue; } if (btrfs_is_leaf(next) && btrfs_header_level(eb) != 1) { warning( "eb corrupted: item %d eb level %d next level %d, skipping the rest", i, btrfs_header_level(next), btrfs_header_level(eb)); goto out; } if (btrfs_header_level(next) != btrfs_header_level(eb) - 1) { warning( "eb corrupted: item %d eb level %d next level %d, skipping the rest", i, btrfs_header_level(next), btrfs_header_level(eb)); goto out; } btrfs_print_tree(root, next, 1); free_extent_buffer(next); } return; out: free_extent_buffer(next); }