Btrfs: change around extent-tree prealloc
[deliverable/linux.git] / fs / btrfs / ctree.c
index 5c160c23677318d8b57ab616effc2f61db0261b5..b4783bf8bf4fc2729c1cedaacd272917fcb88885 100644 (file)
@@ -68,6 +68,7 @@ static int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
        memcpy(cow_node, btrfs_buffer_node(buf), root->blocksize);
        btrfs_set_header_blocknr(&cow_node->header, bh_blocknr(cow));
        btrfs_set_header_generation(&cow_node->header, trans->transid);
+       btrfs_set_header_owner(&cow_node->header, root->root_key.objectid);
        btrfs_inc_ref(trans, root, buf);
        if (buf == root->node) {
                root->node = cow;
@@ -448,6 +449,124 @@ static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
        return ret;
 }
 
+/* returns zero if the push worked, non-zero otherwise */
+static int push_nodes_for_insert(struct btrfs_trans_handle *trans,
+                               struct btrfs_root *root,
+                               struct btrfs_path *path, int level)
+{
+       struct buffer_head *right_buf;
+       struct buffer_head *mid_buf;
+       struct buffer_head *left_buf;
+       struct buffer_head *parent_buf = NULL;
+       struct btrfs_node *right = NULL;
+       struct btrfs_node *mid;
+       struct btrfs_node *left = NULL;
+       struct btrfs_node *parent = NULL;
+       int ret = 0;
+       int wret;
+       int pslot;
+       int orig_slot = path->slots[level];
+       u64 orig_ptr;
+
+       if (level == 0)
+               return 1;
+
+       mid_buf = path->nodes[level];
+       mid = btrfs_buffer_node(mid_buf);
+       orig_ptr = btrfs_node_blockptr(mid, orig_slot);
+
+       if (level < BTRFS_MAX_LEVEL - 1)
+               parent_buf = path->nodes[level + 1];
+       pslot = path->slots[level + 1];
+
+       if (!parent_buf)
+               return 1;
+       parent = btrfs_buffer_node(parent_buf);
+
+       left_buf = read_node_slot(root, parent_buf, pslot - 1);
+
+       /* first, try to make some room in the middle buffer */
+       if (left_buf) {
+               u32 left_nr;
+               left = btrfs_buffer_node(left_buf);
+               left_nr = btrfs_header_nritems(&left->header);
+               if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
+                       wret = 1;
+               } else {
+                       btrfs_cow_block(trans, root, left_buf, parent_buf,
+                                       pslot - 1, &left_buf);
+                       left = btrfs_buffer_node(left_buf);
+                       wret = push_node_left(trans, root, left_buf, mid_buf);
+               }
+               if (wret < 0)
+                       ret = wret;
+               if (wret == 0) {
+                       orig_slot += left_nr;
+                       btrfs_memcpy(root, parent,
+                                    &parent->ptrs[pslot].key,
+                                    &mid->ptrs[0].key,
+                                    sizeof(struct btrfs_disk_key));
+                       btrfs_mark_buffer_dirty(parent_buf);
+                       if (btrfs_header_nritems(&left->header) > orig_slot) {
+                               path->nodes[level] = left_buf;
+                               path->slots[level + 1] -= 1;
+                               path->slots[level] = orig_slot;
+                               btrfs_block_release(root, mid_buf);
+                       } else {
+                               orig_slot -=
+                                       btrfs_header_nritems(&left->header);
+                               path->slots[level] = orig_slot;
+                               btrfs_block_release(root, left_buf);
+                       }
+                       check_node(root, path, level);
+                       return 0;
+               }
+               btrfs_block_release(root, left_buf);
+       }
+       right_buf = read_node_slot(root, parent_buf, pslot + 1);
+
+       /*
+        * then try to empty the right most buffer into the middle
+        */
+       if (right_buf) {
+               u32 right_nr;
+               right = btrfs_buffer_node(right_buf);
+               right_nr = btrfs_header_nritems(&right->header);
+               if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
+                       wret = 1;
+               } else {
+                       btrfs_cow_block(trans, root, right_buf,
+                                       parent_buf, pslot + 1, &right_buf);
+                       right = btrfs_buffer_node(right_buf);
+                       wret = balance_node_right(trans, root,
+                                                 right_buf, mid_buf);
+               }
+               if (wret < 0)
+                       ret = wret;
+               if (wret == 0) {
+                       btrfs_memcpy(root, parent,
+                                    &parent->ptrs[pslot + 1].key,
+                                    &right->ptrs[0].key,
+                                    sizeof(struct btrfs_disk_key));
+                       btrfs_mark_buffer_dirty(parent_buf);
+                       if (btrfs_header_nritems(&mid->header) <= orig_slot) {
+                               path->nodes[level] = right_buf;
+                               path->slots[level + 1] += 1;
+                               path->slots[level] = orig_slot -
+                                       btrfs_header_nritems(&mid->header);
+                               btrfs_block_release(root, mid_buf);
+                       } else {
+                               btrfs_block_release(root, right_buf);
+                       }
+                       check_node(root, path, level);
+                       return 0;
+               }
+               btrfs_block_release(root, right_buf);
+       }
+       check_node(root, path, level);
+       return 1;
+}
+
 /*
  * look for key in the tree.  path is filled in with nodes along the way
  * if key is found, we return zero and you can find the item in the leaf
@@ -688,6 +807,7 @@ static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root
        btrfs_set_header_level(&c->header, level);
        btrfs_set_header_blocknr(&c->header, bh_blocknr(t));
        btrfs_set_header_generation(&c->header, trans->transid);
+       btrfs_set_header_owner(&c->header, root->root_key.objectid);
        lower = btrfs_buffer_node(path->nodes[level-1]);
        memcpy(c->header.fsid, root->fs_info->disk_super->fsid,
               sizeof(c->header.fsid));
@@ -774,7 +894,16 @@ static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
                ret = insert_new_root(trans, root, path, level + 1);
                if (ret)
                        return ret;
+       } else {
+               ret = push_nodes_for_insert(trans, root, path, level);
+               t = path->nodes[level];
+               c = btrfs_buffer_node(t);
+               if (!ret &&
+                   btrfs_header_nritems(&c->header) <
+                   BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
+                       return 0;
        }
+
        c_nritems = btrfs_header_nritems(&c->header);
        split_buffer = btrfs_alloc_free_block(trans, root);
        split = btrfs_buffer_node(split_buffer);
@@ -782,6 +911,7 @@ static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
        btrfs_set_header_level(&split->header, btrfs_header_level(&c->header));
        btrfs_set_header_blocknr(&split->header, bh_blocknr(split_buffer));
        btrfs_set_header_generation(&split->header, trans->transid);
+       btrfs_set_header_owner(&split->header, root->root_key.objectid);
        memcpy(split->header.fsid, root->fs_info->disk_super->fsid,
               sizeof(split->header.fsid));
        mid = (c_nritems + 1) / 2;
@@ -893,7 +1023,11 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
        }
 
        left_nritems = btrfs_header_nritems(&left->header);
-       for (i = left_nritems - 1; i >= 0; i--) {
+       if (left_nritems == 0) {
+               btrfs_block_release(root, right_buf);
+               return 1;
+       }
+       for (i = left_nritems - 1; i >= 1; i--) {
                item = left->items + i;
                if (path->slots[0] == i)
                        push_space += data_size + sizeof(*item);
@@ -907,6 +1041,8 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
                btrfs_block_release(root, right_buf);
                return 1;
        }
+       if (push_items == left_nritems)
+               WARN_ON(1);
        right_nritems = btrfs_header_nritems(&right->header);
        /* push left to right */
        push_space = btrfs_item_end(left->items + left_nritems - push_items);
@@ -943,6 +1079,7 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
 
        btrfs_mark_buffer_dirty(left_buf);
        btrfs_mark_buffer_dirty(right_buf);
+
        btrfs_memcpy(root, upper_node, &upper_node->ptrs[slot + 1].key,
                &right->items[0].key, sizeof(struct btrfs_disk_key));
        btrfs_mark_buffer_dirty(upper);
@@ -1004,7 +1141,12 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
                return 1;
        }
 
-       for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
+       if (btrfs_header_nritems(&right->header) == 0) {
+               btrfs_block_release(root, t);
+               return 1;
+       }
+
+       for (i = 0; i < btrfs_header_nritems(&right->header) - 1; i++) {
                item = right->items + i;
                if (path->slots[0] == i)
                        push_space += data_size + sizeof(*item);
@@ -1018,6 +1160,8 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
                btrfs_block_release(root, t);
                return 1;
        }
+       if (push_items == btrfs_header_nritems(&right->header))
+               WARN_ON(1);
        /* push data from right to left */
        btrfs_memcpy(root, left, left->items +
                     btrfs_header_nritems(&left->header),
@@ -1064,7 +1208,6 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
 
        btrfs_mark_buffer_dirty(t);
        btrfs_mark_buffer_dirty(right_buf);
-
        wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1);
        if (wret)
                ret = wret;
@@ -1140,6 +1283,7 @@ static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
        memset(&right->header, 0, sizeof(right->header));
        btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer));
        btrfs_set_header_generation(&right->header, trans->transid);
+       btrfs_set_header_owner(&right->header, root->root_key.objectid);
        btrfs_set_header_level(&right->header, 0);
        memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
               sizeof(right->header.fsid));
@@ -1181,6 +1325,12 @@ static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
                                path->nodes[0] = right_buffer;
                                path->slots[0] = 0;
                                path->slots[1] -= 1;
+                               if (path->slots[1] == 0) {
+                                       wret = fixup_low_keys(trans, root,
+                                                  path, &disk_key, 1);
+                                       if (wret)
+                                               ret = wret;
+                               }
                                return ret;
                        }
                        mid = slot;
@@ -1230,6 +1380,7 @@ static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
        memset(&right->header, 0, sizeof(right->header));
        btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer));
        btrfs_set_header_generation(&right->header, trans->transid);
+       btrfs_set_header_owner(&right->header, root->root_key.objectid);
        btrfs_set_header_level(&right->header, 0);
        memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
               sizeof(right->header.fsid));
@@ -1241,6 +1392,11 @@ static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
                          path->slots[1], 1);
        if (wret)
                ret = wret;
+       if (path->slots[1] == 0) {
+               wret = fixup_low_keys(trans, root, path, &disk_key, 1);
+               if (wret)
+                       ret = wret;
+       }
        btrfs_block_release(root, path->nodes[0]);
        path->nodes[0] = right_buffer;
        path->slots[0] = 0;
This page took 0.041028 seconds and 5 git commands to generate.