Commit 02217ed2 authored by Chris Mason's avatar Chris Mason Committed by David Woodhouse

Btrfs: early reference counting

Signed-off-by: default avatarChris Mason <chris.mason@oracle.com>
parent 77ce6846
This diff is collapsed.
...@@ -142,8 +142,9 @@ struct ctree_path { ...@@ -142,8 +142,9 @@ struct ctree_path {
}; };
struct tree_buffer *alloc_free_block(struct ctree_root *root); struct tree_buffer *alloc_free_block(struct ctree_root *root);
int btrfs_inc_ref(struct ctree_root *root, struct tree_buffer *buf);
int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks); int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks);
int search_slot(struct ctree_root *root, struct key *key, struct ctree_path *p, int ins_len); int search_slot(struct ctree_root *root, struct key *key, struct ctree_path *p, int ins_len, int cow);
void release_path(struct ctree_root *root, struct ctree_path *p); void release_path(struct ctree_root *root, struct ctree_path *p);
void init_path(struct ctree_path *p); void init_path(struct ctree_path *p);
int del_item(struct ctree_root *root, struct ctree_path *path); int del_item(struct ctree_root *root, struct ctree_path *path);
......
...@@ -260,6 +260,8 @@ void tree_block_release(struct ctree_root *root, struct tree_buffer *buf) ...@@ -260,6 +260,8 @@ void tree_block_release(struct ctree_root *root, struct tree_buffer *buf)
if (buf->count < 0) if (buf->count < 0)
BUG(); BUG();
if (buf->count == 0) { if (buf->count == 0) {
BUG_ON(!list_empty(&buf->cache));
BUG_ON(!list_empty(&buf->dirty));
if (!radix_tree_lookup(&root->cache_radix, buf->blocknr)) if (!radix_tree_lookup(&root->cache_radix, buf->blocknr))
BUG(); BUG();
radix_tree_delete(&root->cache_radix, buf->blocknr); radix_tree_delete(&root->cache_radix, buf->blocknr);
......
...@@ -15,6 +15,39 @@ ...@@ -15,6 +15,39 @@
*/ */
#define CTREE_EXTENT_PENDING 0 #define CTREE_EXTENT_PENDING 0
static int inc_block_ref(struct ctree_root *root, u64 blocknr)
{
struct ctree_path path;
int ret;
struct key key;
struct leaf *l;
struct extent_item *item;
init_path(&path);
key.objectid = blocknr;
key.flags = 0;
key.offset = 1;
ret = search_slot(root->extent_root, &key, &path, 0, 1);
BUG_ON(ret != 0);
l = &path.nodes[0]->leaf;
item = (struct extent_item *)(l->data +
l->items[path.slots[0]].offset);
item->refs++;
BUG_ON(list_empty(&path.nodes[0]->dirty));
release_path(root->extent_root, &path);
return 0;
}
int btrfs_inc_ref(struct ctree_root *root, struct tree_buffer *buf)
{
u64 blocknr;
int i;
for (i = 0; i < buf->node.header.nritems; i++) {
blocknr = buf->node.blockptrs[i];
inc_block_ref(root, blocknr);
}
return 0;
}
/* /*
* find all the blocks marked as pending in the radix tree and remove * find all the blocks marked as pending in the radix tree and remove
* them from the extent map * them from the extent map
...@@ -39,7 +72,7 @@ static int del_pending_extents(struct ctree_root *extent_root) ...@@ -39,7 +72,7 @@ static int del_pending_extents(struct ctree_root *extent_root)
key.flags = 0; key.flags = 0;
key.offset = 1; key.offset = 1;
init_path(&path); init_path(&path);
ret = search_slot(extent_root, &key, &path, -1); ret = search_slot(extent_root, &key, &path, -1, 1);
if (ret) { if (ret) {
print_tree(extent_root, extent_root->node); print_tree(extent_root, extent_root->node);
printf("unable to find %Lu\n", key.objectid); printf("unable to find %Lu\n", key.objectid);
...@@ -83,7 +116,7 @@ int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks) ...@@ -83,7 +116,7 @@ int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks)
return 0; return 0;
} }
init_path(&path); init_path(&path);
ret = search_slot(extent_root, &key, &path, -1); ret = search_slot(extent_root, &key, &path, -1, 1);
if (ret) { if (ret) {
print_tree(extent_root, extent_root->node); print_tree(extent_root, extent_root->node);
printf("failed to find %Lu\n", key.objectid); printf("failed to find %Lu\n", key.objectid);
...@@ -124,7 +157,7 @@ static int find_free_extent(struct ctree_root *orig_root, u64 num_blocks, ...@@ -124,7 +157,7 @@ static int find_free_extent(struct ctree_root *orig_root, u64 num_blocks,
ins->offset = 0; ins->offset = 0;
ins->flags = 0; ins->flags = 0;
start_found = 0; start_found = 0;
ret = search_slot(root, ins, &path, 0); ret = search_slot(root, ins, &path, 0, 0);
if (ret < 0) if (ret < 0)
goto error; goto error;
...@@ -221,6 +254,8 @@ static int insert_pending_extents(struct ctree_root *extent_root) ...@@ -221,6 +254,8 @@ static int insert_pending_extents(struct ctree_root *extent_root)
ret = insert_item(extent_root, &key, &item, ret = insert_item(extent_root, &key, &item,
sizeof(item)); sizeof(item));
if (ret) { if (ret) {
printf("%Lu already in tree\n", key.objectid);
print_tree(extent_root, extent_root->node);
BUG(); BUG();
// FIXME undo it and return sane // FIXME undo it and return sane
return ret; return ret;
...@@ -228,6 +263,7 @@ static int insert_pending_extents(struct ctree_root *extent_root) ...@@ -228,6 +263,7 @@ static int insert_pending_extents(struct ctree_root *extent_root)
radix_tree_tag_clear(&extent_root->cache_radix, radix_tree_tag_clear(&extent_root->cache_radix,
gang[i]->blocknr, gang[i]->blocknr,
CTREE_EXTENT_PENDING); CTREE_EXTENT_PENDING);
printf("%Lu is not pending\n", gang[i]->blocknr);
tree_block_release(extent_root, gang[i]); tree_block_release(extent_root, gang[i]);
} }
} }
...@@ -266,15 +302,18 @@ int alloc_extent(struct ctree_root *root, u64 num_blocks, u64 search_start, ...@@ -266,15 +302,18 @@ int alloc_extent(struct ctree_root *root, u64 num_blocks, u64 search_start,
if (pending_ret) if (pending_ret)
return pending_ret; return pending_ret;
*buf = find_tree_block(root, ins->objectid); *buf = find_tree_block(root, ins->objectid);
dirty_tree_block(root, *buf);
return 0; return 0;
} }
/* we're allocating an extent for the extent tree, don't recurse */ /* we're allocating an extent for the extent tree, don't recurse */
BUG_ON(ins->offset != 1); BUG_ON(ins->offset != 1);
*buf = find_tree_block(root, ins->objectid); *buf = find_tree_block(root, ins->objectid);
BUG_ON(!*buf); BUG_ON(!*buf);
printf("%Lu is pending\n", ins->objectid);
radix_tree_tag_set(&root->cache_radix, ins->objectid, radix_tree_tag_set(&root->cache_radix, ins->objectid,
CTREE_EXTENT_PENDING); CTREE_EXTENT_PENDING);
(*buf)->count++; (*buf)->count++;
dirty_tree_block(root, *buf);
return 0; return 0;
} }
......
...@@ -19,7 +19,7 @@ int main(int ac, char **av) { ...@@ -19,7 +19,7 @@ int main(int ac, char **av) {
int i; int i;
int num; int num;
int ret; int ret;
int run_size = 100000; int run_size = 1024;
int max_key = 100000000; int max_key = 100000000;
int tree_size = 0; int tree_size = 0;
struct ctree_path path; struct ctree_path path;
...@@ -57,7 +57,7 @@ int main(int ac, char **av) { ...@@ -57,7 +57,7 @@ int main(int ac, char **av) {
init_path(&path); init_path(&path);
if (i % 10000 == 0) if (i % 10000 == 0)
fprintf(stderr, "search %d:%d\n", num, i); fprintf(stderr, "search %d:%d\n", num, i);
ret = search_slot(root, &ins, &path, 0); ret = search_slot(root, &ins, &path, 0, 0);
if (ret) { if (ret) {
print_tree(root, root->node); print_tree(root, root->node);
printf("unable to find %d\n", num); printf("unable to find %d\n", num);
...@@ -79,7 +79,7 @@ int main(int ac, char **av) { ...@@ -79,7 +79,7 @@ int main(int ac, char **av) {
num = next_key(i, max_key); num = next_key(i, max_key);
ins.objectid = num; ins.objectid = num;
init_path(&path); init_path(&path);
ret = search_slot(root, &ins, &path, -1); ret = search_slot(root, &ins, &path, -1, 1);
if (!ret) { if (!ret) {
if (i % 10000 == 0) if (i % 10000 == 0)
fprintf(stderr, "del %d:%d\n", num, i); fprintf(stderr, "del %d:%d\n", num, i);
...@@ -117,7 +117,7 @@ int main(int ac, char **av) { ...@@ -117,7 +117,7 @@ int main(int ac, char **av) {
init_path(&path); init_path(&path);
if (i % 10000 == 0) if (i % 10000 == 0)
fprintf(stderr, "search %d:%d\n", num, i); fprintf(stderr, "search %d:%d\n", num, i);
ret = search_slot(root, &ins, &path, 0); ret = search_slot(root, &ins, &path, 0, 0);
if (ret) { if (ret) {
print_tree(root, root->node); print_tree(root, root->node);
printf("unable to find %d\n", num); printf("unable to find %d\n", num);
...@@ -131,7 +131,7 @@ int main(int ac, char **av) { ...@@ -131,7 +131,7 @@ int main(int ac, char **av) {
int slot; int slot;
ins.objectid = (u64)-1; ins.objectid = (u64)-1;
init_path(&path); init_path(&path);
ret = search_slot(root, &ins, &path, -1); ret = search_slot(root, &ins, &path, -1, 1);
if (ret == 0) if (ret == 0)
BUG(); BUG();
......
...@@ -93,7 +93,7 @@ static int del_one(struct ctree_root *root, struct radix_tree_root *radix) ...@@ -93,7 +93,7 @@ static int del_one(struct ctree_root *root, struct radix_tree_root *radix)
ret = setup_key(radix, &key, 1); ret = setup_key(radix, &key, 1);
if (ret < 0) if (ret < 0)
return 0; return 0;
ret = search_slot(root, &key, &path, -1); ret = search_slot(root, &key, &path, -1, 1);
if (ret) if (ret)
goto error; goto error;
ret = del_item(root, &path); ret = del_item(root, &path);
...@@ -118,7 +118,7 @@ static int lookup_item(struct ctree_root *root, struct radix_tree_root *radix) ...@@ -118,7 +118,7 @@ static int lookup_item(struct ctree_root *root, struct radix_tree_root *radix)
ret = setup_key(radix, &key, 1); ret = setup_key(radix, &key, 1);
if (ret < 0) if (ret < 0)
return 0; return 0;
ret = search_slot(root, &key, &path, 0); ret = search_slot(root, &key, &path, 0, 1);
release_path(root, &path); release_path(root, &path);
if (ret) if (ret)
goto error; goto error;
...@@ -137,7 +137,7 @@ static int lookup_enoent(struct ctree_root *root, struct radix_tree_root *radix) ...@@ -137,7 +137,7 @@ static int lookup_enoent(struct ctree_root *root, struct radix_tree_root *radix)
ret = setup_key(radix, &key, 0); ret = setup_key(radix, &key, 0);
if (ret < 0) if (ret < 0)
return ret; return ret;
ret = search_slot(root, &key, &path, 0); ret = search_slot(root, &key, &path, 0, 0);
release_path(root, &path); release_path(root, &path);
if (ret <= 0) if (ret <= 0)
goto error; goto error;
...@@ -163,7 +163,7 @@ static int empty_tree(struct ctree_root *root, struct radix_tree_root *radix, ...@@ -163,7 +163,7 @@ static int empty_tree(struct ctree_root *root, struct radix_tree_root *radix,
key.objectid = (unsigned long)-1; key.objectid = (unsigned long)-1;
while(nr-- >= 0) { while(nr-- >= 0) {
init_path(&path); init_path(&path);
ret = search_slot(root, &key, &path, -1); ret = search_slot(root, &key, &path, -1, 1);
if (ret < 0) { if (ret < 0) {
release_path(root, &path); release_path(root, &path);
return ret; return ret;
...@@ -216,7 +216,7 @@ static int fill_tree(struct ctree_root *root, struct radix_tree_root *radix, ...@@ -216,7 +216,7 @@ static int fill_tree(struct ctree_root *root, struct radix_tree_root *radix,
return ret; return ret;
} }
} }
if (i % 10000 == 0) { if (i && i % 10000 == 0) {
printf("bigfill %d\n", i); printf("bigfill %d\n", i);
} }
if (!keep_running) if (!keep_running)
...@@ -263,7 +263,7 @@ static int fill_radix(struct ctree_root *root, struct radix_tree_root *radix) ...@@ -263,7 +263,7 @@ static int fill_radix(struct ctree_root *root, struct radix_tree_root *radix)
key.objectid = (unsigned long)-1; key.objectid = (unsigned long)-1;
while(1) { while(1) {
init_path(&path); init_path(&path);
ret = search_slot(root, &key, &path, 0); ret = search_slot(root, &key, &path, 0, 0);
if (ret < 0) { if (ret < 0) {
release_path(root, &path); release_path(root, &path);
return ret; return ret;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment