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 {
};
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 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 init_path(struct ctree_path *p);
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)
if (buf->count < 0)
BUG();
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))
BUG();
radix_tree_delete(&root->cache_radix, buf->blocknr);
......
......@@ -15,6 +15,39 @@
*/
#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
* them from the extent map
......@@ -39,7 +72,7 @@ static int del_pending_extents(struct ctree_root *extent_root)
key.flags = 0;
key.offset = 1;
init_path(&path);
ret = search_slot(extent_root, &key, &path, -1);
ret = search_slot(extent_root, &key, &path, -1, 1);
if (ret) {
print_tree(extent_root, extent_root->node);
printf("unable to find %Lu\n", key.objectid);
......@@ -83,7 +116,7 @@ int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks)
return 0;
}
init_path(&path);
ret = search_slot(extent_root, &key, &path, -1);
ret = search_slot(extent_root, &key, &path, -1, 1);
if (ret) {
print_tree(extent_root, extent_root->node);
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,
ins->offset = 0;
ins->flags = 0;
start_found = 0;
ret = search_slot(root, ins, &path, 0);
ret = search_slot(root, ins, &path, 0, 0);
if (ret < 0)
goto error;
......@@ -221,6 +254,8 @@ static int insert_pending_extents(struct ctree_root *extent_root)
ret = insert_item(extent_root, &key, &item,
sizeof(item));
if (ret) {
printf("%Lu already in tree\n", key.objectid);
print_tree(extent_root, extent_root->node);
BUG();
// FIXME undo it and return sane
return ret;
......@@ -228,6 +263,7 @@ static int insert_pending_extents(struct ctree_root *extent_root)
radix_tree_tag_clear(&extent_root->cache_radix,
gang[i]->blocknr,
CTREE_EXTENT_PENDING);
printf("%Lu is not pending\n", gang[i]->blocknr);
tree_block_release(extent_root, gang[i]);
}
}
......@@ -266,15 +302,18 @@ int alloc_extent(struct ctree_root *root, u64 num_blocks, u64 search_start,
if (pending_ret)
return pending_ret;
*buf = find_tree_block(root, ins->objectid);
dirty_tree_block(root, *buf);
return 0;
}
/* we're allocating an extent for the extent tree, don't recurse */
BUG_ON(ins->offset != 1);
*buf = find_tree_block(root, ins->objectid);
BUG_ON(!*buf);
printf("%Lu is pending\n", ins->objectid);
radix_tree_tag_set(&root->cache_radix, ins->objectid,
CTREE_EXTENT_PENDING);
(*buf)->count++;
dirty_tree_block(root, *buf);
return 0;
}
......
......@@ -19,7 +19,7 @@ int main(int ac, char **av) {
int i;
int num;
int ret;
int run_size = 100000;
int run_size = 1024;
int max_key = 100000000;
int tree_size = 0;
struct ctree_path path;
......@@ -57,7 +57,7 @@ int main(int ac, char **av) {
init_path(&path);
if (i % 10000 == 0)
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) {
print_tree(root, root->node);
printf("unable to find %d\n", num);
......@@ -79,7 +79,7 @@ int main(int ac, char **av) {
num = next_key(i, max_key);
ins.objectid = num;
init_path(&path);
ret = search_slot(root, &ins, &path, -1);
ret = search_slot(root, &ins, &path, -1, 1);
if (!ret) {
if (i % 10000 == 0)
fprintf(stderr, "del %d:%d\n", num, i);
......@@ -117,7 +117,7 @@ int main(int ac, char **av) {
init_path(&path);
if (i % 10000 == 0)
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) {
print_tree(root, root->node);
printf("unable to find %d\n", num);
......@@ -131,7 +131,7 @@ int main(int ac, char **av) {
int slot;
ins.objectid = (u64)-1;
init_path(&path);
ret = search_slot(root, &ins, &path, -1);
ret = search_slot(root, &ins, &path, -1, 1);
if (ret == 0)
BUG();
......
......@@ -93,7 +93,7 @@ static int del_one(struct ctree_root *root, struct radix_tree_root *radix)
ret = setup_key(radix, &key, 1);
if (ret < 0)
return 0;
ret = search_slot(root, &key, &path, -1);
ret = search_slot(root, &key, &path, -1, 1);
if (ret)
goto error;
ret = del_item(root, &path);
......@@ -118,7 +118,7 @@ static int lookup_item(struct ctree_root *root, struct radix_tree_root *radix)
ret = setup_key(radix, &key, 1);
if (ret < 0)
return 0;
ret = search_slot(root, &key, &path, 0);
ret = search_slot(root, &key, &path, 0, 1);
release_path(root, &path);
if (ret)
goto error;
......@@ -137,7 +137,7 @@ static int lookup_enoent(struct ctree_root *root, struct radix_tree_root *radix)
ret = setup_key(radix, &key, 0);
if (ret < 0)
return ret;
ret = search_slot(root, &key, &path, 0);
ret = search_slot(root, &key, &path, 0, 0);
release_path(root, &path);
if (ret <= 0)
goto error;
......@@ -163,7 +163,7 @@ static int empty_tree(struct ctree_root *root, struct radix_tree_root *radix,
key.objectid = (unsigned long)-1;
while(nr-- >= 0) {
init_path(&path);
ret = search_slot(root, &key, &path, -1);
ret = search_slot(root, &key, &path, -1, 1);
if (ret < 0) {
release_path(root, &path);
return ret;
......@@ -216,7 +216,7 @@ static int fill_tree(struct ctree_root *root, struct radix_tree_root *radix,
return ret;
}
}
if (i % 10000 == 0) {
if (i && i % 10000 == 0) {
printf("bigfill %d\n", i);
}
if (!keep_running)
......@@ -263,7 +263,7 @@ static int fill_radix(struct ctree_root *root, struct radix_tree_root *radix)
key.objectid = (unsigned long)-1;
while(1) {
init_path(&path);
ret = search_slot(root, &key, &path, 0);
ret = search_slot(root, &key, &path, 0, 0);
if (ret < 0) {
release_path(root, &path);
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