memset(p, 0, sizeof(*p));
}
+int btrfs_cow_block(struct ctree_root *root,
+ struct tree_buffer *buf,
+ struct tree_buffer *parent,
+ int parent_slot,
+ struct tree_buffer **cow_ret)
+{
+ struct tree_buffer *cow;
+
+ if (!list_empty(&buf->dirty)) {
+ *cow_ret = buf;
+ return 0;
+ }
+ cow = alloc_free_block(root);
+ memcpy(&cow->node, &buf->node, sizeof(buf->node));
+ cow->node.header.blocknr = cow->blocknr;
+ *cow_ret = cow;
+ if (buf == root->node) {
+ root->node = cow;
+ cow->count++;
+ tree_block_release(root, buf);
+ } else {
+ parent->node.blockptrs[parent_slot] = cow->blocknr;
+ BUG_ON(list_empty(&parent->dirty));
+ }
+ if (0 && root != root->extent_root && !is_leaf(cow->node.header.flags)) {
+ btrfs_inc_ref(root, cow);
+ }
+ tree_block_release(root, buf);
+ return 0;
+}
+
/*
* The leaf data grows from end-to-front in the node.
* this returns the address of the start of the last item,
/* first, try to make some room in the middle buffer */
if (left_buf) {
+ btrfs_cow_block(root, left_buf, parent_buf,
+ pslot - 1, &left_buf);
left = &left_buf->node;
orig_slot += left->header.nritems;
wret = push_node_left(root, left_buf, mid_buf);
* then try to empty the right most buffer into the middle
*/
if (right_buf) {
+ btrfs_cow_block(root, right_buf, parent_buf,
+ pslot + 1, &right_buf);
right = &right_buf->node;
wret = push_node_left(root, mid_buf, right_buf);
if (wret < 0)
} else {
memcpy(parent->keys + pslot + 1, right->keys,
sizeof(struct key));
- wret = dirty_tree_block(root, parent_buf);
- if (wret)
- ret = wret;
+ BUG_ON(list_empty(&parent_buf->dirty));
}
}
if (mid->header.nritems == 1) {
} else {
/* update the parent key to reflect our changes */
memcpy(parent->keys + pslot, mid->keys, sizeof(struct key));
- wret = dirty_tree_block(root, parent_buf);
- if (wret)
- ret = wret;
+ BUG_ON(list_empty(&parent_buf->dirty));
}
/* update the path */
* possible)
*/
int search_slot(struct ctree_root *root, struct key *key,
- struct ctree_path *p, int ins_len)
+ struct ctree_path *p, int ins_len, int cow)
{
struct tree_buffer *b;
+ struct tree_buffer *cow_buf;
struct node *c;
int slot;
int ret;
b = root->node;
b->count++;
while (b) {
+ level = node_level(b->node.header.flags);
+ if (cow) {
+ int wret;
+ wret = btrfs_cow_block(root, b, p->nodes[level + 1],
+ p->slots[level + 1], &cow_buf);
+ b = cow_buf;
+ }
+ BUG_ON(!cow && ins_len);
c = &b->node;
- level = node_level(c->header.flags);
p->nodes[level] = b;
ret = check_block(p, level);
if (ret)
{
int i;
int ret = 0;
- int wret;
for (i = level; i < MAX_LEVEL; i++) {
struct node *t;
int tslot = path->slots[i];
break;
t = &path->nodes[i]->node;
memcpy(t->keys + tslot, key, sizeof(*key));
- wret = dirty_tree_block(root, path->nodes[i]);
- if (wret)
- ret = wret;
+ BUG_ON(list_empty(&path->nodes[i]->dirty));
if (tslot != 0)
break;
}
int src_nritems;
int dst_nritems;
int ret = 0;
- int wret;
src_nritems = src->header.nritems;
dst_nritems = dst->header.nritems;
src->header.nritems -= push_items;
dst->header.nritems += push_items;
- wret = dirty_tree_block(root, src_buf);
- if (wret < 0)
- ret = wret;
-
- wret = dirty_tree_block(root, dst_buf);
- if (wret < 0)
- ret = wret;
+ BUG_ON(list_empty(&src_buf->dirty));
+ BUG_ON(list_empty(&dst_buf->dirty));
return ret;
}
int src_nritems;
int dst_nritems;
int ret = 0;
- int wret;
src_nritems = src->header.nritems;
dst_nritems = dst->header.nritems;
src->header.nritems -= push_items;
dst->header.nritems += push_items;
- wret = dirty_tree_block(root, src_buf);
- if (wret < 0)
- ret = wret;
-
- wret = dirty_tree_block(root, dst_buf);
- if (wret < 0)
- ret = wret;
+ BUG_ON(list_empty(&src_buf->dirty));
+ BUG_ON(list_empty(&dst_buf->dirty));
return ret;
}
tree_block_release(root, root->node);
root->node = t;
t->count++;
- dirty_tree_block(root, t);
path->nodes[level] = t;
path->slots[level] = 0;
return 0;
lower->header.nritems++;
if (lower->keys[1].objectid == 0)
BUG();
- dirty_tree_block(root, path->nodes[level]);
+ BUG_ON(list_empty(&path->nodes[level]->dirty));
return 0;
}
c->header.nritems = mid;
ret = 0;
- wret = dirty_tree_block(root, t);
- if (wret)
- ret = wret;
- wret = dirty_tree_block(root, split_buffer);
- if (wret)
- ret = wret;
+ BUG_ON(list_empty(&t->dirty));
wret = insert_ptr(root, path, split->keys, split_buffer->blocknr,
path->slots[level + 1] + 1, level + 1);
if (wret)
tree_block_release(root, right_buf);
return 1;
}
+ /* cow and double check */
+ btrfs_cow_block(root, right_buf, upper, slot + 1, &right_buf);
+ right = &right_buf->leaf;
+ free_space = leaf_free_space(right);
+ if (free_space < data_size + sizeof(struct item)) {
+ tree_block_release(root, right_buf);
+ return 1;
+ }
+
for (i = left->header.nritems - 1; i >= 0; i--) {
item = left->items + i;
if (path->slots[0] == i)
}
left->header.nritems -= push_items;
- dirty_tree_block(root, left_buf);
- dirty_tree_block(root, right_buf);
+ BUG_ON(list_empty(&left_buf->dirty));
+ BUG_ON(list_empty(&right_buf->dirty));
memcpy(upper->node.keys + slot + 1,
&right->items[0].key, sizeof(struct key));
- dirty_tree_block(root, upper);
+ BUG_ON(list_empty(&upper->dirty));
+
/* then fixup the leaf pointer in the path */
if (path->slots[0] >= left->header.nritems) {
path->slots[0] -= left->header.nritems;
tree_block_release(root, t);
return 1;
}
+
+ /* cow and double check */
+ btrfs_cow_block(root, t, path->nodes[1], slot - 1, &t);
+ left = &t->leaf;
+ free_space = leaf_free_space(left);
+ if (free_space < data_size + sizeof(struct item)) {
+ tree_block_release(root, t);
+ return 1;
+ }
+
for (i = 0; i < right->header.nritems; i++) {
item = right->items + i;
if (path->slots[0] == i)
push_space = right->items[i].offset;
}
- wret = dirty_tree_block(root, t);
- if (wret)
- ret = wret;
- wret = dirty_tree_block(root, right_buf);
- if (wret)
- ret = wret;
+ BUG_ON(list_empty(&t->dirty));
+ BUG_ON(list_empty(&right_buf->dirty));
wret = fixup_low_keys(root, path, &right->items[0].key, 1);
if (wret)
if (wret < 0)
return wret;
}
+
l_buf = path->nodes[0];
l = &l_buf->leaf;
right_buffer->blocknr, path->slots[1] + 1, 1);
if (wret)
ret = wret;
- wret = dirty_tree_block(root, right_buffer);
- if (wret)
- ret = wret;
- wret = dirty_tree_block(root, l_buf);
- if (wret)
- ret = wret;
-
+ BUG_ON(list_empty(&right_buffer->dirty));
+ BUG_ON(list_empty(&l_buf->dirty));
BUG_ON(path->slots[0] != slot);
if (mid <= slot) {
tree_block_release(root, path->nodes[0]);
void *data, int data_size)
{
int ret = 0;
- int wret;
int slot;
int slot_orig;
struct leaf *leaf;
if (!root->node)
BUG();
init_path(&path);
- ret = search_slot(root, key, &path, data_size);
+ ret = search_slot(root, key, &path, data_size, 1);
if (ret == 0) {
release_path(root, &path);
return -EEXIST;
if (slot == 0)
ret = fixup_low_keys(root, &path, key, 1);
- wret = dirty_tree_block(root, leaf_buf);
- if (wret)
- ret = wret;
-
+ BUG_ON(list_empty(&leaf_buf->dirty));
if (leaf_free_space(leaf) < 0)
BUG();
check_leaf(&path, 0);
if (wret)
ret = wret;
}
- wret = dirty_tree_block(root, parent);
- if (wret)
- ret = wret;
+ BUG_ON(list_empty(&parent->dirty));
return ret;
}
if (leaf->header.nritems == 0) {
if (leaf_buf == root->node) {
leaf->header.flags = node_level(0);
- dirty_tree_block(root, leaf_buf);
+ BUG_ON(list_empty(&leaf_buf->dirty));
} else {
clean_tree_block(root, leaf_buf);
wret = del_ptr(root, path, 1, path->slots[1]);
if (wret)
ret = wret;
}
- wret = dirty_tree_block(root, leaf_buf);
- if (wret)
- ret = wret;
+ BUG_ON(list_empty(&leaf_buf->dirty));
/* delete the leaf if it is mostly empty */
if (used < LEAF_DATA_SIZE / 3) {
return 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
* them from the extent map
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);
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);
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;
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;
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]);
}
}
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;
}
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);
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;
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;
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;
return ret;
}
}
- if (i % 10000 == 0) {
+ if (i && i % 10000 == 0) {
printf("bigfill %d\n", i);
}
if (!keep_running)
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;