Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1 | #include <linux/module.h> |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2 | #include "ctree.h" |
| 3 | #include "disk-io.h" |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 4 | #include "transaction.h" |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 5 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 6 | static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root |
| 7 | *root, struct btrfs_path *path, int level); |
| 8 | static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 9 | *root, struct btrfs_key *ins_key, |
| 10 | struct btrfs_path *path, int data_size); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 11 | static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 12 | *root, struct buffer_head *dst, struct buffer_head |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 13 | *src); |
| 14 | static int balance_node_right(struct btrfs_trans_handle *trans, struct |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 15 | btrfs_root *root, struct buffer_head *dst_buf, |
| 16 | struct buffer_head *src_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 17 | static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 18 | struct btrfs_path *path, int level, int slot); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 19 | |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 20 | inline void btrfs_init_path(struct btrfs_path *p) |
| 21 | { |
| 22 | memset(p, 0, sizeof(*p)); |
| 23 | } |
| 24 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 25 | struct btrfs_path *btrfs_alloc_path(void) |
| 26 | { |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 27 | struct btrfs_path *path; |
| 28 | path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS); |
| 29 | if (path) |
| 30 | btrfs_init_path(path); |
| 31 | return path; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void btrfs_free_path(struct btrfs_path *p) |
| 35 | { |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 36 | btrfs_release_path(NULL, p); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 37 | kmem_cache_free(btrfs_path_cachep, p); |
| 38 | } |
| 39 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 40 | void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 41 | { |
| 42 | int i; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 43 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 44 | if (!p->nodes[i]) |
| 45 | break; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 46 | btrfs_block_release(root, p->nodes[i]); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 47 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 48 | memset(p, 0, sizeof(*p)); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 49 | } |
| 50 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 51 | static int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 52 | *root, struct buffer_head *buf, struct buffer_head |
| 53 | *parent, int parent_slot, struct buffer_head |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 54 | **cow_ret) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 55 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 56 | struct buffer_head *cow; |
| 57 | struct btrfs_node *cow_node; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 58 | |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 59 | if (btrfs_header_generation(btrfs_buffer_header(buf)) == |
| 60 | trans->transid) { |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 61 | *cow_ret = buf; |
| 62 | return 0; |
| 63 | } |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 64 | cow = btrfs_alloc_free_block(trans, root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 65 | cow_node = btrfs_buffer_node(cow); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 66 | if (buf->b_size != root->blocksize || cow->b_size != root->blocksize) |
| 67 | WARN_ON(1); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 68 | memcpy(cow_node, btrfs_buffer_node(buf), root->blocksize); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 69 | btrfs_set_header_blocknr(&cow_node->header, bh_blocknr(cow)); |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 70 | btrfs_set_header_generation(&cow_node->header, trans->transid); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 71 | btrfs_inc_ref(trans, root, buf); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 72 | if (buf == root->node) { |
| 73 | root->node = cow; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 74 | get_bh(cow); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 75 | if (buf != root->commit_root) { |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 76 | btrfs_free_extent(trans, root, bh_blocknr(buf), 1, 1); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 77 | } |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 78 | btrfs_block_release(root, buf); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 79 | } else { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 80 | btrfs_set_node_blockptr(btrfs_buffer_node(parent), parent_slot, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 81 | bh_blocknr(cow)); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 82 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 83 | btrfs_free_extent(trans, root, bh_blocknr(buf), 1, 1); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 84 | } |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 85 | btrfs_block_release(root, buf); |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 86 | mark_buffer_dirty(cow); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 87 | *cow_ret = cow; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 91 | /* |
| 92 | * The leaf data grows from end-to-front in the node. |
| 93 | * this returns the address of the start of the last item, |
| 94 | * which is the stop of the leaf data stack |
| 95 | */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 96 | static inline unsigned int leaf_data_end(struct btrfs_root *root, |
| 97 | struct btrfs_leaf *leaf) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 98 | { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 99 | u32 nr = btrfs_header_nritems(&leaf->header); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 100 | if (nr == 0) |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 101 | return BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 102 | return btrfs_item_offset(leaf->items + nr - 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 103 | } |
| 104 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 105 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 106 | * compare two keys in a memcmp fashion |
| 107 | */ |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 108 | static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 109 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 110 | struct btrfs_key k1; |
| 111 | |
| 112 | btrfs_disk_key_to_cpu(&k1, disk); |
| 113 | |
| 114 | if (k1.objectid > k2->objectid) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 115 | return 1; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 116 | if (k1.objectid < k2->objectid) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 117 | return -1; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 118 | if (k1.flags > k2->flags) |
| 119 | return 1; |
| 120 | if (k1.flags < k2->flags) |
| 121 | return -1; |
Chris Mason | 70b2bef | 2007-04-17 15:39:32 -0400 | [diff] [blame] | 122 | if (k1.offset > k2->offset) |
| 123 | return 1; |
| 124 | if (k1.offset < k2->offset) |
| 125 | return -1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 126 | return 0; |
| 127 | } |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 128 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 129 | static int check_node(struct btrfs_root *root, struct btrfs_path *path, |
| 130 | int level) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 131 | { |
| 132 | int i; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 133 | struct btrfs_node *parent = NULL; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 134 | struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 135 | int parent_slot; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 136 | u32 nritems = btrfs_header_nritems(&node->header); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 137 | |
| 138 | if (path->nodes[level + 1]) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 139 | parent = btrfs_buffer_node(path->nodes[level + 1]); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 140 | parent_slot = path->slots[level + 1]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 141 | BUG_ON(nritems == 0); |
| 142 | if (parent) { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 143 | struct btrfs_disk_key *parent_key; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 144 | parent_key = &parent->ptrs[parent_slot].key; |
| 145 | BUG_ON(memcmp(parent_key, &node->ptrs[0].key, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 146 | sizeof(struct btrfs_disk_key))); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 147 | BUG_ON(btrfs_node_blockptr(parent, parent_slot) != |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 148 | btrfs_header_blocknr(&node->header)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 149 | } |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 150 | BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root)); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 151 | for (i = 0; nritems > 1 && i < nritems - 2; i++) { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 152 | struct btrfs_key cpukey; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 153 | btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[i + 1].key); |
| 154 | BUG_ON(comp_keys(&node->ptrs[i].key, &cpukey) >= 0); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 155 | } |
| 156 | return 0; |
| 157 | } |
| 158 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 159 | static int check_leaf(struct btrfs_root *root, struct btrfs_path *path, |
| 160 | int level) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 161 | { |
| 162 | int i; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 163 | struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[level]); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 164 | struct btrfs_node *parent = NULL; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 165 | int parent_slot; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 166 | u32 nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 167 | |
| 168 | if (path->nodes[level + 1]) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 169 | parent = btrfs_buffer_node(path->nodes[level + 1]); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 170 | parent_slot = path->slots[level + 1]; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 171 | BUG_ON(btrfs_leaf_free_space(root, leaf) < 0); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 172 | |
| 173 | if (nritems == 0) |
| 174 | return 0; |
| 175 | |
| 176 | if (parent) { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 177 | struct btrfs_disk_key *parent_key; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 178 | parent_key = &parent->ptrs[parent_slot].key; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 179 | BUG_ON(memcmp(parent_key, &leaf->items[0].key, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 180 | sizeof(struct btrfs_disk_key))); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 181 | BUG_ON(btrfs_node_blockptr(parent, parent_slot) != |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 182 | btrfs_header_blocknr(&leaf->header)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 183 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 184 | for (i = 0; nritems > 1 && i < nritems - 2; i++) { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 185 | struct btrfs_key cpukey; |
| 186 | btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 187 | BUG_ON(comp_keys(&leaf->items[i].key, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 188 | &cpukey) >= 0); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 189 | BUG_ON(btrfs_item_offset(leaf->items + i) != |
| 190 | btrfs_item_end(leaf->items + i + 1)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 191 | if (i == 0) { |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 192 | BUG_ON(btrfs_item_offset(leaf->items + i) + |
| 193 | btrfs_item_size(leaf->items + i) != |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 194 | BTRFS_LEAF_DATA_SIZE(root)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 195 | } |
| 196 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 197 | return 0; |
| 198 | } |
| 199 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 200 | static int check_block(struct btrfs_root *root, struct btrfs_path *path, |
| 201 | int level) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 202 | { |
Chris Mason | 3eb0314 | 2007-04-05 14:28:50 -0400 | [diff] [blame] | 203 | struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]); |
| 204 | if (memcmp(node->header.fsid, root->fs_info->disk_super->fsid, |
| 205 | sizeof(node->header.fsid))) |
| 206 | BUG(); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 207 | if (level == 0) |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 208 | return check_leaf(root, path, level); |
| 209 | return check_node(root, path, level); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 210 | } |
| 211 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 212 | /* |
| 213 | * search for key in the array p. items p are item_size apart |
| 214 | * and there are 'max' items in p |
| 215 | * the slot in the array is returned via slot, and it points to |
| 216 | * the place where you would insert key if it is not found in |
| 217 | * the array. |
| 218 | * |
| 219 | * slot may point to max if the key is bigger than all of the keys |
| 220 | */ |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 221 | static int generic_bin_search(char *p, int item_size, struct btrfs_key *key, |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 222 | int max, int *slot) |
| 223 | { |
| 224 | int low = 0; |
| 225 | int high = max; |
| 226 | int mid; |
| 227 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 228 | struct btrfs_disk_key *tmp; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 229 | |
| 230 | while(low < high) { |
| 231 | mid = (low + high) / 2; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 232 | tmp = (struct btrfs_disk_key *)(p + mid * item_size); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 233 | ret = comp_keys(tmp, key); |
| 234 | |
| 235 | if (ret < 0) |
| 236 | low = mid + 1; |
| 237 | else if (ret > 0) |
| 238 | high = mid; |
| 239 | else { |
| 240 | *slot = mid; |
| 241 | return 0; |
| 242 | } |
| 243 | } |
| 244 | *slot = low; |
| 245 | return 1; |
| 246 | } |
| 247 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 248 | /* |
| 249 | * simple bin_search frontend that does the right thing for |
| 250 | * leaves vs nodes |
| 251 | */ |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 252 | static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 253 | { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 254 | if (btrfs_is_leaf(c)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 255 | struct btrfs_leaf *l = (struct btrfs_leaf *)c; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 256 | return generic_bin_search((void *)l->items, |
| 257 | sizeof(struct btrfs_item), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 258 | key, btrfs_header_nritems(&c->header), |
| 259 | slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 260 | } else { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 261 | return generic_bin_search((void *)c->ptrs, |
| 262 | sizeof(struct btrfs_key_ptr), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 263 | key, btrfs_header_nritems(&c->header), |
| 264 | slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 265 | } |
| 266 | return -1; |
| 267 | } |
| 268 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 269 | static struct buffer_head *read_node_slot(struct btrfs_root *root, |
| 270 | struct buffer_head *parent_buf, |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 271 | int slot) |
| 272 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 273 | struct btrfs_node *node = btrfs_buffer_node(parent_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 274 | if (slot < 0) |
| 275 | return NULL; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 276 | if (slot >= btrfs_header_nritems(&node->header)) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 277 | return NULL; |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 278 | return read_tree_block(root, btrfs_node_blockptr(node, slot)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 279 | } |
| 280 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 281 | static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root |
| 282 | *root, struct btrfs_path *path, int level) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 283 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 284 | struct buffer_head *right_buf; |
| 285 | struct buffer_head *mid_buf; |
| 286 | struct buffer_head *left_buf; |
| 287 | struct buffer_head *parent_buf = NULL; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 288 | struct btrfs_node *right = NULL; |
| 289 | struct btrfs_node *mid; |
| 290 | struct btrfs_node *left = NULL; |
| 291 | struct btrfs_node *parent = NULL; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 292 | int ret = 0; |
| 293 | int wret; |
| 294 | int pslot; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 295 | int orig_slot = path->slots[level]; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 296 | u64 orig_ptr; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 297 | |
| 298 | if (level == 0) |
| 299 | return 0; |
| 300 | |
| 301 | mid_buf = path->nodes[level]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 302 | mid = btrfs_buffer_node(mid_buf); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 303 | orig_ptr = btrfs_node_blockptr(mid, orig_slot); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 304 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 305 | if (level < BTRFS_MAX_LEVEL - 1) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 306 | parent_buf = path->nodes[level + 1]; |
| 307 | pslot = path->slots[level + 1]; |
| 308 | |
Chris Mason | 4068947 | 2007-03-17 14:29:23 -0400 | [diff] [blame] | 309 | /* |
| 310 | * deal with the case where there is only one pointer in the root |
| 311 | * by promoting the node below to a root |
| 312 | */ |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 313 | if (!parent_buf) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 314 | struct buffer_head *child; |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 315 | u64 blocknr = bh_blocknr(mid_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 316 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 317 | if (btrfs_header_nritems(&mid->header) != 1) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 318 | return 0; |
| 319 | |
| 320 | /* promote the child to a root */ |
| 321 | child = read_node_slot(root, mid_buf, 0); |
| 322 | BUG_ON(!child); |
| 323 | root->node = child; |
| 324 | path->nodes[level] = NULL; |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 325 | clean_tree_block(trans, root, mid_buf); |
| 326 | wait_on_buffer(mid_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 327 | /* once for the path */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 328 | btrfs_block_release(root, mid_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 329 | /* once for the root ptr */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 330 | btrfs_block_release(root, mid_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 331 | return btrfs_free_extent(trans, root, blocknr, 1, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 332 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 333 | parent = btrfs_buffer_node(parent_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 334 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 335 | if (btrfs_header_nritems(&mid->header) > |
| 336 | BTRFS_NODEPTRS_PER_BLOCK(root) / 4) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 337 | return 0; |
| 338 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 339 | left_buf = read_node_slot(root, parent_buf, pslot - 1); |
| 340 | right_buf = read_node_slot(root, parent_buf, pslot + 1); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 341 | |
| 342 | /* first, try to make some room in the middle buffer */ |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 343 | if (left_buf) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 344 | btrfs_cow_block(trans, root, left_buf, parent_buf, pslot - 1, |
| 345 | &left_buf); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 346 | left = btrfs_buffer_node(left_buf); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 347 | orig_slot += btrfs_header_nritems(&left->header); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 348 | wret = push_node_left(trans, root, left_buf, mid_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 349 | if (wret < 0) |
| 350 | ret = wret; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 351 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 352 | |
| 353 | /* |
| 354 | * then try to empty the right most buffer into the middle |
| 355 | */ |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 356 | if (right_buf) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 357 | btrfs_cow_block(trans, root, right_buf, parent_buf, pslot + 1, |
| 358 | &right_buf); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 359 | right = btrfs_buffer_node(right_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 360 | wret = push_node_left(trans, root, mid_buf, right_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 361 | if (wret < 0) |
| 362 | ret = wret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 363 | if (btrfs_header_nritems(&right->header) == 0) { |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 364 | u64 blocknr = bh_blocknr(right_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 365 | clean_tree_block(trans, root, right_buf); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 366 | wait_on_buffer(right_buf); |
| 367 | btrfs_block_release(root, right_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 368 | right_buf = NULL; |
| 369 | right = NULL; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 370 | wret = del_ptr(trans, root, path, level + 1, pslot + |
| 371 | 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 372 | if (wret) |
| 373 | ret = wret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 374 | wret = btrfs_free_extent(trans, root, blocknr, 1, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 375 | if (wret) |
| 376 | ret = wret; |
| 377 | } else { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 378 | btrfs_memcpy(root, parent, |
| 379 | &parent->ptrs[pslot + 1].key, |
| 380 | &right->ptrs[0].key, |
| 381 | sizeof(struct btrfs_disk_key)); |
| 382 | btrfs_mark_buffer_dirty(parent_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 383 | } |
| 384 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 385 | if (btrfs_header_nritems(&mid->header) == 1) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 386 | /* |
| 387 | * we're not allowed to leave a node with one item in the |
| 388 | * tree during a delete. A deletion from lower in the tree |
| 389 | * could try to delete the only pointer in this node. |
| 390 | * So, pull some keys from the left. |
| 391 | * There has to be a left pointer at this point because |
| 392 | * otherwise we would have pulled some pointers from the |
| 393 | * right |
| 394 | */ |
| 395 | BUG_ON(!left_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 396 | wret = balance_node_right(trans, root, mid_buf, left_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 397 | if (wret < 0) |
| 398 | ret = wret; |
| 399 | BUG_ON(wret == 1); |
| 400 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 401 | if (btrfs_header_nritems(&mid->header) == 0) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 402 | /* we've managed to empty the middle node, drop it */ |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 403 | u64 blocknr = bh_blocknr(mid_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 404 | clean_tree_block(trans, root, mid_buf); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 405 | wait_on_buffer(mid_buf); |
| 406 | btrfs_block_release(root, mid_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 407 | mid_buf = NULL; |
| 408 | mid = NULL; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 409 | wret = del_ptr(trans, root, path, level + 1, pslot); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 410 | if (wret) |
| 411 | ret = wret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 412 | wret = btrfs_free_extent(trans, root, blocknr, 1, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 413 | if (wret) |
| 414 | ret = wret; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 415 | } else { |
| 416 | /* update the parent key to reflect our changes */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 417 | btrfs_memcpy(root, parent, |
| 418 | &parent->ptrs[pslot].key, &mid->ptrs[0].key, |
| 419 | sizeof(struct btrfs_disk_key)); |
| 420 | btrfs_mark_buffer_dirty(parent_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 421 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 422 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 423 | /* update the path */ |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 424 | if (left_buf) { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 425 | if (btrfs_header_nritems(&left->header) > orig_slot) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 426 | get_bh(left_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 427 | path->nodes[level] = left_buf; |
| 428 | path->slots[level + 1] -= 1; |
| 429 | path->slots[level] = orig_slot; |
| 430 | if (mid_buf) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 431 | btrfs_block_release(root, mid_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 432 | } else { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 433 | orig_slot -= btrfs_header_nritems(&left->header); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 434 | path->slots[level] = orig_slot; |
| 435 | } |
| 436 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 437 | /* double check we haven't messed things up */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 438 | check_block(root, path, level); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 439 | if (orig_ptr != |
| 440 | btrfs_node_blockptr(btrfs_buffer_node(path->nodes[level]), |
| 441 | path->slots[level])) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 442 | BUG(); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 443 | |
| 444 | if (right_buf) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 445 | btrfs_block_release(root, right_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 446 | if (left_buf) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 447 | btrfs_block_release(root, left_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 448 | return ret; |
| 449 | } |
| 450 | |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 451 | /* returns zero if the push worked, non-zero otherwise */ |
| 452 | static int push_nodes_for_insert(struct btrfs_trans_handle *trans, |
| 453 | struct btrfs_root *root, |
| 454 | struct btrfs_path *path, int level) |
| 455 | { |
| 456 | struct buffer_head *right_buf; |
| 457 | struct buffer_head *mid_buf; |
| 458 | struct buffer_head *left_buf; |
| 459 | struct buffer_head *parent_buf = NULL; |
| 460 | struct btrfs_node *right = NULL; |
| 461 | struct btrfs_node *mid; |
| 462 | struct btrfs_node *left = NULL; |
| 463 | struct btrfs_node *parent = NULL; |
| 464 | int ret = 0; |
| 465 | int wret; |
| 466 | int pslot; |
| 467 | int orig_slot = path->slots[level]; |
| 468 | u64 orig_ptr; |
| 469 | |
| 470 | if (level == 0) |
| 471 | return 1; |
| 472 | |
| 473 | mid_buf = path->nodes[level]; |
| 474 | mid = btrfs_buffer_node(mid_buf); |
| 475 | orig_ptr = btrfs_node_blockptr(mid, orig_slot); |
| 476 | |
| 477 | if (level < BTRFS_MAX_LEVEL - 1) |
| 478 | parent_buf = path->nodes[level + 1]; |
| 479 | pslot = path->slots[level + 1]; |
| 480 | |
| 481 | if (!parent_buf) |
| 482 | return 1; |
| 483 | parent = btrfs_buffer_node(parent_buf); |
| 484 | |
| 485 | left_buf = read_node_slot(root, parent_buf, pslot - 1); |
| 486 | |
| 487 | /* first, try to make some room in the middle buffer */ |
| 488 | if (left_buf) { |
| 489 | u32 left_nr; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 490 | left = btrfs_buffer_node(left_buf); |
| 491 | left_nr = btrfs_header_nritems(&left->header); |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame^] | 492 | if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { |
| 493 | wret = 1; |
| 494 | } else { |
| 495 | btrfs_cow_block(trans, root, left_buf, parent_buf, |
| 496 | pslot - 1, &left_buf); |
| 497 | left = btrfs_buffer_node(left_buf); |
| 498 | wret = push_node_left(trans, root, left_buf, mid_buf); |
| 499 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 500 | if (wret < 0) |
| 501 | ret = wret; |
| 502 | if (wret == 0) { |
| 503 | orig_slot += left_nr; |
| 504 | btrfs_memcpy(root, parent, |
| 505 | &parent->ptrs[pslot].key, |
| 506 | &mid->ptrs[0].key, |
| 507 | sizeof(struct btrfs_disk_key)); |
| 508 | btrfs_mark_buffer_dirty(parent_buf); |
| 509 | if (btrfs_header_nritems(&left->header) > orig_slot) { |
| 510 | path->nodes[level] = left_buf; |
| 511 | path->slots[level + 1] -= 1; |
| 512 | path->slots[level] = orig_slot; |
| 513 | btrfs_block_release(root, mid_buf); |
| 514 | } else { |
| 515 | orig_slot -= |
| 516 | btrfs_header_nritems(&left->header); |
| 517 | path->slots[level] = orig_slot; |
| 518 | btrfs_block_release(root, left_buf); |
| 519 | } |
| 520 | check_node(root, path, level); |
| 521 | return 0; |
| 522 | } |
| 523 | btrfs_block_release(root, left_buf); |
| 524 | } |
| 525 | right_buf = read_node_slot(root, parent_buf, pslot + 1); |
| 526 | |
| 527 | /* |
| 528 | * then try to empty the right most buffer into the middle |
| 529 | */ |
| 530 | if (right_buf) { |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame^] | 531 | u32 right_nr; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 532 | right = btrfs_buffer_node(right_buf); |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame^] | 533 | right_nr = btrfs_header_nritems(&right->header); |
| 534 | if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { |
| 535 | wret = 1; |
| 536 | } else { |
| 537 | btrfs_cow_block(trans, root, right_buf, |
| 538 | parent_buf, pslot + 1, &right_buf); |
| 539 | right = btrfs_buffer_node(right_buf); |
| 540 | wret = balance_node_right(trans, root, |
| 541 | right_buf, mid_buf); |
| 542 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 543 | if (wret < 0) |
| 544 | ret = wret; |
| 545 | if (wret == 0) { |
| 546 | btrfs_memcpy(root, parent, |
| 547 | &parent->ptrs[pslot + 1].key, |
| 548 | &right->ptrs[0].key, |
| 549 | sizeof(struct btrfs_disk_key)); |
| 550 | btrfs_mark_buffer_dirty(parent_buf); |
| 551 | if (btrfs_header_nritems(&mid->header) <= orig_slot) { |
| 552 | path->nodes[level] = right_buf; |
| 553 | path->slots[level + 1] += 1; |
| 554 | path->slots[level] = orig_slot - |
| 555 | btrfs_header_nritems(&mid->header); |
| 556 | btrfs_block_release(root, mid_buf); |
| 557 | } else { |
| 558 | btrfs_block_release(root, right_buf); |
| 559 | } |
| 560 | check_node(root, path, level); |
| 561 | return 0; |
| 562 | } |
| 563 | btrfs_block_release(root, right_buf); |
| 564 | } |
| 565 | check_node(root, path, level); |
| 566 | return 1; |
| 567 | } |
| 568 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 569 | /* |
| 570 | * look for key in the tree. path is filled in with nodes along the way |
| 571 | * if key is found, we return zero and you can find the item in the leaf |
| 572 | * level of the path (level 0) |
| 573 | * |
| 574 | * If the key isn't found, the path points to the slot where it should |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 575 | * be inserted, and 1 is returned. If there are other errors during the |
| 576 | * search a negative error number is returned. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 577 | * |
| 578 | * if ins_len > 0, nodes and leaves will be split as we walk down the |
| 579 | * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if |
| 580 | * possible) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 581 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 582 | int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root |
| 583 | *root, struct btrfs_key *key, struct btrfs_path *p, int |
| 584 | ins_len, int cow) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 585 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 586 | struct buffer_head *b; |
| 587 | struct buffer_head *cow_buf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 588 | struct btrfs_node *c; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 589 | int slot; |
| 590 | int ret; |
| 591 | int level; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 592 | |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 593 | WARN_ON(p->nodes[0] != NULL); |
| 594 | WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 595 | again: |
| 596 | b = root->node; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 597 | get_bh(b); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 598 | while (b) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 599 | c = btrfs_buffer_node(b); |
| 600 | level = btrfs_header_level(&c->header); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 601 | if (cow) { |
| 602 | int wret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 603 | wret = btrfs_cow_block(trans, root, b, |
| 604 | p->nodes[level + 1], |
| 605 | p->slots[level + 1], |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 606 | &cow_buf); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 607 | b = cow_buf; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 608 | c = btrfs_buffer_node(b); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 609 | } |
| 610 | BUG_ON(!cow && ins_len); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 611 | if (level != btrfs_header_level(&c->header)) |
| 612 | WARN_ON(1); |
| 613 | level = btrfs_header_level(&c->header); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 614 | p->nodes[level] = b; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 615 | ret = check_block(root, p, level); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 616 | if (ret) |
| 617 | return -1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 618 | ret = bin_search(c, key, &slot); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 619 | if (!btrfs_is_leaf(c)) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 620 | if (ret && slot > 0) |
| 621 | slot -= 1; |
| 622 | p->slots[level] = slot; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 623 | if (ins_len > 0 && btrfs_header_nritems(&c->header) >= |
| 624 | BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 625 | int sret = split_node(trans, root, p, level); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 626 | BUG_ON(sret > 0); |
| 627 | if (sret) |
| 628 | return sret; |
| 629 | b = p->nodes[level]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 630 | c = btrfs_buffer_node(b); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 631 | slot = p->slots[level]; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 632 | } else if (ins_len < 0) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 633 | int sret = balance_level(trans, root, p, |
| 634 | level); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 635 | if (sret) |
| 636 | return sret; |
| 637 | b = p->nodes[level]; |
| 638 | if (!b) |
| 639 | goto again; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 640 | c = btrfs_buffer_node(b); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 641 | slot = p->slots[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 642 | BUG_ON(btrfs_header_nritems(&c->header) == 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 643 | } |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 644 | b = read_tree_block(root, btrfs_node_blockptr(c, slot)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 645 | } else { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 646 | struct btrfs_leaf *l = (struct btrfs_leaf *)c; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 647 | p->slots[level] = slot; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 648 | if (ins_len > 0 && btrfs_leaf_free_space(root, l) < |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 649 | sizeof(struct btrfs_item) + ins_len) { |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 650 | int sret = split_leaf(trans, root, key, |
| 651 | p, ins_len); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 652 | BUG_ON(sret > 0); |
| 653 | if (sret) |
| 654 | return sret; |
| 655 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 656 | return ret; |
| 657 | } |
| 658 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 659 | return 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 660 | } |
| 661 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 662 | /* |
| 663 | * adjust the pointers going up the tree, starting at level |
| 664 | * making sure the right key of each node is points to 'key'. |
| 665 | * This is used after shifting pointers to the left, so it stops |
| 666 | * fixing up pointers when a given leaf/node is not in slot 0 of the |
| 667 | * higher levels |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 668 | * |
| 669 | * If this fails to write a tree block, it returns -1, but continues |
| 670 | * fixing up the blocks in ram so the tree is consistent. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 671 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 672 | static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root |
| 673 | *root, struct btrfs_path *path, struct btrfs_disk_key |
| 674 | *key, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 675 | { |
| 676 | int i; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 677 | int ret = 0; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 678 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
| 679 | struct btrfs_node *t; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 680 | int tslot = path->slots[i]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 681 | if (!path->nodes[i]) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 682 | break; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 683 | t = btrfs_buffer_node(path->nodes[i]); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 684 | btrfs_memcpy(root, t, &t->ptrs[tslot].key, key, sizeof(*key)); |
| 685 | btrfs_mark_buffer_dirty(path->nodes[i]); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 686 | if (tslot != 0) |
| 687 | break; |
| 688 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 689 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 690 | } |
| 691 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 692 | /* |
| 693 | * try to push data from one node into the next node left in the |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 694 | * tree. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 695 | * |
| 696 | * returns 0 if some ptrs were pushed left, < 0 if there was some horrible |
| 697 | * error, and > 0 if there was no room in the left hand block. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 698 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 699 | static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 700 | *root, struct buffer_head *dst_buf, struct |
| 701 | buffer_head *src_buf) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 702 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 703 | struct btrfs_node *src = btrfs_buffer_node(src_buf); |
| 704 | struct btrfs_node *dst = btrfs_buffer_node(dst_buf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 705 | int push_items = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 706 | int src_nritems; |
| 707 | int dst_nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 708 | int ret = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 709 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 710 | src_nritems = btrfs_header_nritems(&src->header); |
| 711 | dst_nritems = btrfs_header_nritems(&dst->header); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 712 | push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 713 | if (push_items <= 0) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 714 | return 1; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 715 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 716 | |
| 717 | if (src_nritems < push_items) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 718 | push_items = src_nritems; |
| 719 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 720 | btrfs_memcpy(root, dst, dst->ptrs + dst_nritems, src->ptrs, |
| 721 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 722 | if (push_items < src_nritems) { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 723 | btrfs_memmove(root, src, src->ptrs, src->ptrs + push_items, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 724 | (src_nritems - push_items) * |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 725 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 726 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 727 | btrfs_set_header_nritems(&src->header, src_nritems - push_items); |
| 728 | btrfs_set_header_nritems(&dst->header, dst_nritems + push_items); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 729 | btrfs_mark_buffer_dirty(src_buf); |
| 730 | btrfs_mark_buffer_dirty(dst_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 731 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 732 | } |
| 733 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 734 | /* |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 735 | * try to push data from one node into the next node right in the |
| 736 | * tree. |
| 737 | * |
| 738 | * returns 0 if some ptrs were pushed, < 0 if there was some horrible |
| 739 | * error, and > 0 if there was no room in the right hand block. |
| 740 | * |
| 741 | * this will only push up to 1/2 the contents of the left node over |
| 742 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 743 | static int balance_node_right(struct btrfs_trans_handle *trans, struct |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 744 | btrfs_root *root, struct buffer_head *dst_buf, |
| 745 | struct buffer_head *src_buf) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 746 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 747 | struct btrfs_node *src = btrfs_buffer_node(src_buf); |
| 748 | struct btrfs_node *dst = btrfs_buffer_node(dst_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 749 | int push_items = 0; |
| 750 | int max_push; |
| 751 | int src_nritems; |
| 752 | int dst_nritems; |
| 753 | int ret = 0; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 754 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 755 | src_nritems = btrfs_header_nritems(&src->header); |
| 756 | dst_nritems = btrfs_header_nritems(&dst->header); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 757 | push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 758 | if (push_items <= 0) { |
| 759 | return 1; |
| 760 | } |
| 761 | |
| 762 | max_push = src_nritems / 2 + 1; |
| 763 | /* don't try to empty the node */ |
| 764 | if (max_push > src_nritems) |
| 765 | return 1; |
| 766 | if (max_push < push_items) |
| 767 | push_items = max_push; |
| 768 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 769 | btrfs_memmove(root, dst, dst->ptrs + push_items, dst->ptrs, |
| 770 | dst_nritems * sizeof(struct btrfs_key_ptr)); |
| 771 | |
| 772 | btrfs_memcpy(root, dst, dst->ptrs, |
| 773 | src->ptrs + src_nritems - push_items, |
| 774 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 775 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 776 | btrfs_set_header_nritems(&src->header, src_nritems - push_items); |
| 777 | btrfs_set_header_nritems(&dst->header, dst_nritems + push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 778 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 779 | btrfs_mark_buffer_dirty(src_buf); |
| 780 | btrfs_mark_buffer_dirty(dst_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 781 | return ret; |
| 782 | } |
| 783 | |
| 784 | /* |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 785 | * helper function to insert a new root level in the tree. |
| 786 | * A new node is allocated, and a single item is inserted to |
| 787 | * point to the existing root |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 788 | * |
| 789 | * returns zero on success or < 0 on failure. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 790 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 791 | static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root |
| 792 | *root, struct btrfs_path *path, int level) |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 793 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 794 | struct buffer_head *t; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 795 | struct btrfs_node *lower; |
| 796 | struct btrfs_node *c; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 797 | struct btrfs_disk_key *lower_key; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 798 | |
| 799 | BUG_ON(path->nodes[level]); |
| 800 | BUG_ON(path->nodes[level-1] != root->node); |
| 801 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 802 | t = btrfs_alloc_free_block(trans, root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 803 | c = btrfs_buffer_node(t); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 804 | memset(c, 0, root->blocksize); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 805 | btrfs_set_header_nritems(&c->header, 1); |
| 806 | btrfs_set_header_level(&c->header, level); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 807 | btrfs_set_header_blocknr(&c->header, bh_blocknr(t)); |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 808 | btrfs_set_header_generation(&c->header, trans->transid); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 809 | lower = btrfs_buffer_node(path->nodes[level-1]); |
Chris Mason | 3eb0314 | 2007-04-05 14:28:50 -0400 | [diff] [blame] | 810 | memcpy(c->header.fsid, root->fs_info->disk_super->fsid, |
| 811 | sizeof(c->header.fsid)); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 812 | if (btrfs_is_leaf(lower)) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 813 | lower_key = &((struct btrfs_leaf *)lower)->items[0].key; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 814 | else |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 815 | lower_key = &lower->ptrs[0].key; |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 816 | btrfs_memcpy(root, c, &c->ptrs[0].key, lower_key, |
| 817 | sizeof(struct btrfs_disk_key)); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 818 | btrfs_set_node_blockptr(c, 0, bh_blocknr(path->nodes[level - 1])); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 819 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 820 | btrfs_mark_buffer_dirty(t); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 821 | |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 822 | /* the super has an extra ref to root->node */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 823 | btrfs_block_release(root, root->node); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 824 | root->node = t; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 825 | get_bh(t); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 826 | path->nodes[level] = t; |
| 827 | path->slots[level] = 0; |
| 828 | return 0; |
| 829 | } |
| 830 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 831 | /* |
| 832 | * worker function to insert a single pointer in a node. |
| 833 | * the node should have enough room for the pointer already |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 834 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 835 | * slot and level indicate where you want the key to go, and |
| 836 | * blocknr is the block the key points to. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 837 | * |
| 838 | * returns zero on success and < 0 on any error |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 839 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 840 | static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root |
| 841 | *root, struct btrfs_path *path, struct btrfs_disk_key |
| 842 | *key, u64 blocknr, int slot, int level) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 843 | { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 844 | struct btrfs_node *lower; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 845 | int nritems; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 846 | |
| 847 | BUG_ON(!path->nodes[level]); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 848 | lower = btrfs_buffer_node(path->nodes[level]); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 849 | nritems = btrfs_header_nritems(&lower->header); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 850 | if (slot > nritems) |
| 851 | BUG(); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 852 | if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root)) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 853 | BUG(); |
| 854 | if (slot != nritems) { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 855 | btrfs_memmove(root, lower, lower->ptrs + slot + 1, |
| 856 | lower->ptrs + slot, |
| 857 | (nritems - slot) * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 858 | } |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 859 | btrfs_memcpy(root, lower, &lower->ptrs[slot].key, |
| 860 | key, sizeof(struct btrfs_disk_key)); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 861 | btrfs_set_node_blockptr(lower, slot, blocknr); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 862 | btrfs_set_header_nritems(&lower->header, nritems + 1); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 863 | btrfs_mark_buffer_dirty(path->nodes[level]); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 864 | return 0; |
| 865 | } |
| 866 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 867 | /* |
| 868 | * split the node at the specified level in path in two. |
| 869 | * The path is corrected to point to the appropriate node after the split |
| 870 | * |
| 871 | * Before splitting this tries to make some room in the node by pushing |
| 872 | * left and right, if either one works, it returns right away. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 873 | * |
| 874 | * returns 0 on success and < 0 on failure |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 875 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 876 | static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root |
| 877 | *root, struct btrfs_path *path, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 878 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 879 | struct buffer_head *t; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 880 | struct btrfs_node *c; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 881 | struct buffer_head *split_buffer; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 882 | struct btrfs_node *split; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 883 | int mid; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 884 | int ret; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 885 | int wret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 886 | u32 c_nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 887 | |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 888 | t = path->nodes[level]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 889 | c = btrfs_buffer_node(t); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 890 | if (t == root->node) { |
| 891 | /* trying to split the root, lets make a new one */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 892 | ret = insert_new_root(trans, root, path, level + 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 893 | if (ret) |
| 894 | return ret; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 895 | } else { |
| 896 | ret = push_nodes_for_insert(trans, root, path, level); |
| 897 | t = path->nodes[level]; |
| 898 | c = btrfs_buffer_node(t); |
| 899 | if (!ret && |
| 900 | btrfs_header_nritems(&c->header) < |
| 901 | BTRFS_NODEPTRS_PER_BLOCK(root) - 1) |
| 902 | return 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 903 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 904 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 905 | c_nritems = btrfs_header_nritems(&c->header); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 906 | split_buffer = btrfs_alloc_free_block(trans, root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 907 | split = btrfs_buffer_node(split_buffer); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 908 | btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header)); |
Chris Mason | 9a6f11e | 2007-03-27 09:06:38 -0400 | [diff] [blame] | 909 | btrfs_set_header_level(&split->header, btrfs_header_level(&c->header)); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 910 | btrfs_set_header_blocknr(&split->header, bh_blocknr(split_buffer)); |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 911 | btrfs_set_header_generation(&split->header, trans->transid); |
Chris Mason | 3eb0314 | 2007-04-05 14:28:50 -0400 | [diff] [blame] | 912 | memcpy(split->header.fsid, root->fs_info->disk_super->fsid, |
| 913 | sizeof(split->header.fsid)); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 914 | mid = (c_nritems + 1) / 2; |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 915 | btrfs_memcpy(root, split, split->ptrs, c->ptrs + mid, |
| 916 | (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 917 | btrfs_set_header_nritems(&split->header, c_nritems - mid); |
| 918 | btrfs_set_header_nritems(&c->header, mid); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 919 | ret = 0; |
| 920 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 921 | btrfs_mark_buffer_dirty(t); |
| 922 | btrfs_mark_buffer_dirty(split_buffer); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 923 | wret = insert_ptr(trans, root, path, &split->ptrs[0].key, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 924 | bh_blocknr(split_buffer), path->slots[level + 1] + 1, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 925 | level + 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 926 | if (wret) |
| 927 | ret = wret; |
| 928 | |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 929 | if (path->slots[level] >= mid) { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 930 | path->slots[level] -= mid; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 931 | btrfs_block_release(root, t); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 932 | path->nodes[level] = split_buffer; |
| 933 | path->slots[level + 1] += 1; |
| 934 | } else { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 935 | btrfs_block_release(root, split_buffer); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 936 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 937 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 938 | } |
| 939 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 940 | /* |
| 941 | * how many bytes are required to store the items in a leaf. start |
| 942 | * and nr indicate which items in the leaf to check. This totals up the |
| 943 | * space used both by the item structs and the item data |
| 944 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 945 | static int leaf_space_used(struct btrfs_leaf *l, int start, int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 946 | { |
| 947 | int data_len; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 948 | int nritems = btrfs_header_nritems(&l->header); |
| 949 | int end = min(nritems, start + nr) - 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 950 | |
| 951 | if (!nr) |
| 952 | return 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 953 | data_len = btrfs_item_end(l->items + start); |
| 954 | data_len = data_len - btrfs_item_offset(l->items + end); |
| 955 | data_len += sizeof(struct btrfs_item) * nr; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 956 | WARN_ON(data_len < 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 957 | return data_len; |
| 958 | } |
| 959 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 960 | /* |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 961 | * The space between the end of the leaf items and |
| 962 | * the start of the leaf data. IOW, how much room |
| 963 | * the leaf has left for both items and data |
| 964 | */ |
| 965 | int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf) |
| 966 | { |
| 967 | int nritems = btrfs_header_nritems(&leaf->header); |
| 968 | return BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems); |
| 969 | } |
| 970 | |
| 971 | /* |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 972 | * push some data in the path leaf to the right, trying to free up at |
| 973 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 974 | * |
| 975 | * returns 1 if the push failed because the other node didn't have enough |
| 976 | * room, 0 if everything worked out and < 0 if there were major errors. |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 977 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 978 | static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root |
| 979 | *root, struct btrfs_path *path, int data_size) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 980 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 981 | struct buffer_head *left_buf = path->nodes[0]; |
| 982 | struct btrfs_leaf *left = btrfs_buffer_leaf(left_buf); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 983 | struct btrfs_leaf *right; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 984 | struct buffer_head *right_buf; |
| 985 | struct buffer_head *upper; |
| 986 | struct btrfs_node *upper_node; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 987 | int slot; |
| 988 | int i; |
| 989 | int free_space; |
| 990 | int push_space = 0; |
| 991 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 992 | struct btrfs_item *item; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 993 | u32 left_nritems; |
| 994 | u32 right_nritems; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 995 | |
| 996 | slot = path->slots[1]; |
| 997 | if (!path->nodes[1]) { |
| 998 | return 1; |
| 999 | } |
| 1000 | upper = path->nodes[1]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1001 | upper_node = btrfs_buffer_node(upper); |
| 1002 | if (slot >= btrfs_header_nritems(&upper_node->header) - 1) { |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1003 | return 1; |
| 1004 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1005 | right_buf = read_tree_block(root, |
| 1006 | btrfs_node_blockptr(btrfs_buffer_node(upper), slot + 1)); |
| 1007 | right = btrfs_buffer_leaf(right_buf); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1008 | free_space = btrfs_leaf_free_space(root, right); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1009 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1010 | btrfs_block_release(root, right_buf); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1011 | return 1; |
| 1012 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1013 | /* cow and double check */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1014 | btrfs_cow_block(trans, root, right_buf, upper, slot + 1, &right_buf); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1015 | right = btrfs_buffer_leaf(right_buf); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1016 | free_space = btrfs_leaf_free_space(root, right); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1017 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1018 | btrfs_block_release(root, right_buf); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1019 | return 1; |
| 1020 | } |
| 1021 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1022 | left_nritems = btrfs_header_nritems(&left->header); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1023 | if (left_nritems == 0) { |
| 1024 | btrfs_block_release(root, right_buf); |
| 1025 | return 1; |
| 1026 | } |
| 1027 | for (i = left_nritems - 1; i >= 1; i--) { |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1028 | item = left->items + i; |
| 1029 | if (path->slots[0] == i) |
| 1030 | push_space += data_size + sizeof(*item); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1031 | if (btrfs_item_size(item) + sizeof(*item) + push_space > |
| 1032 | free_space) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1033 | break; |
| 1034 | push_items++; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1035 | push_space += btrfs_item_size(item) + sizeof(*item); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1036 | } |
| 1037 | if (push_items == 0) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1038 | btrfs_block_release(root, right_buf); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1039 | return 1; |
| 1040 | } |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1041 | if (push_items == left_nritems) |
| 1042 | WARN_ON(1); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1043 | right_nritems = btrfs_header_nritems(&right->header); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1044 | /* push left to right */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1045 | push_space = btrfs_item_end(left->items + left_nritems - push_items); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1046 | push_space -= leaf_data_end(root, left); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1047 | /* make room in the right data area */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1048 | btrfs_memmove(root, right, btrfs_leaf_data(right) + |
| 1049 | leaf_data_end(root, right) - push_space, |
| 1050 | btrfs_leaf_data(right) + |
| 1051 | leaf_data_end(root, right), BTRFS_LEAF_DATA_SIZE(root) - |
| 1052 | leaf_data_end(root, right)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1053 | /* copy from the left data area */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1054 | btrfs_memcpy(root, right, btrfs_leaf_data(right) + |
| 1055 | BTRFS_LEAF_DATA_SIZE(root) - push_space, |
| 1056 | btrfs_leaf_data(left) + leaf_data_end(root, left), |
| 1057 | push_space); |
| 1058 | btrfs_memmove(root, right, right->items + push_items, right->items, |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1059 | right_nritems * sizeof(struct btrfs_item)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1060 | /* copy the items from left to right */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1061 | btrfs_memcpy(root, right, right->items, left->items + |
| 1062 | left_nritems - push_items, |
| 1063 | push_items * sizeof(struct btrfs_item)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1064 | |
| 1065 | /* update the item pointers */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1066 | right_nritems += push_items; |
| 1067 | btrfs_set_header_nritems(&right->header, right_nritems); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1068 | push_space = BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1069 | for (i = 0; i < right_nritems; i++) { |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1070 | btrfs_set_item_offset(right->items + i, push_space - |
| 1071 | btrfs_item_size(right->items + i)); |
| 1072 | push_space = btrfs_item_offset(right->items + i); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1073 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1074 | left_nritems -= push_items; |
| 1075 | btrfs_set_header_nritems(&left->header, left_nritems); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1076 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1077 | btrfs_mark_buffer_dirty(left_buf); |
| 1078 | btrfs_mark_buffer_dirty(right_buf); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1079 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1080 | btrfs_memcpy(root, upper_node, &upper_node->ptrs[slot + 1].key, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1081 | &right->items[0].key, sizeof(struct btrfs_disk_key)); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1082 | btrfs_mark_buffer_dirty(upper); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1083 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1084 | /* then fixup the leaf pointer in the path */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1085 | if (path->slots[0] >= left_nritems) { |
| 1086 | path->slots[0] -= left_nritems; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1087 | btrfs_block_release(root, path->nodes[0]); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1088 | path->nodes[0] = right_buf; |
| 1089 | path->slots[1] += 1; |
| 1090 | } else { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1091 | btrfs_block_release(root, right_buf); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 1092 | } |
| 1093 | return 0; |
| 1094 | } |
| 1095 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1096 | * push some data in the path leaf to the left, trying to free up at |
| 1097 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
| 1098 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1099 | static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1100 | *root, struct btrfs_path *path, int data_size) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1101 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1102 | struct buffer_head *right_buf = path->nodes[0]; |
| 1103 | struct btrfs_leaf *right = btrfs_buffer_leaf(right_buf); |
| 1104 | struct buffer_head *t; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1105 | struct btrfs_leaf *left; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1106 | int slot; |
| 1107 | int i; |
| 1108 | int free_space; |
| 1109 | int push_space = 0; |
| 1110 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1111 | struct btrfs_item *item; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1112 | u32 old_left_nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1113 | int ret = 0; |
| 1114 | int wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1115 | |
| 1116 | slot = path->slots[1]; |
| 1117 | if (slot == 0) { |
| 1118 | return 1; |
| 1119 | } |
| 1120 | if (!path->nodes[1]) { |
| 1121 | return 1; |
| 1122 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1123 | t = read_tree_block(root, |
| 1124 | btrfs_node_blockptr(btrfs_buffer_node(path->nodes[1]), slot - 1)); |
| 1125 | left = btrfs_buffer_leaf(t); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1126 | free_space = btrfs_leaf_free_space(root, left); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1127 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1128 | btrfs_block_release(root, t); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1129 | return 1; |
| 1130 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1131 | |
| 1132 | /* cow and double check */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1133 | btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1134 | left = btrfs_buffer_leaf(t); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1135 | free_space = btrfs_leaf_free_space(root, left); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1136 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1137 | btrfs_block_release(root, t); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1138 | return 1; |
| 1139 | } |
| 1140 | |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1141 | if (btrfs_header_nritems(&right->header) == 0) { |
| 1142 | btrfs_block_release(root, t); |
| 1143 | return 1; |
| 1144 | } |
| 1145 | |
| 1146 | for (i = 0; i < btrfs_header_nritems(&right->header) - 1; i++) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1147 | item = right->items + i; |
| 1148 | if (path->slots[0] == i) |
| 1149 | push_space += data_size + sizeof(*item); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1150 | if (btrfs_item_size(item) + sizeof(*item) + push_space > |
| 1151 | free_space) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1152 | break; |
| 1153 | push_items++; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1154 | push_space += btrfs_item_size(item) + sizeof(*item); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1155 | } |
| 1156 | if (push_items == 0) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1157 | btrfs_block_release(root, t); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1158 | return 1; |
| 1159 | } |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1160 | if (push_items == btrfs_header_nritems(&right->header)) |
| 1161 | WARN_ON(1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1162 | /* push data from right to left */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1163 | btrfs_memcpy(root, left, left->items + |
| 1164 | btrfs_header_nritems(&left->header), |
| 1165 | right->items, push_items * sizeof(struct btrfs_item)); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1166 | push_space = BTRFS_LEAF_DATA_SIZE(root) - |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1167 | btrfs_item_offset(right->items + push_items -1); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1168 | btrfs_memcpy(root, left, btrfs_leaf_data(left) + |
| 1169 | leaf_data_end(root, left) - push_space, |
| 1170 | btrfs_leaf_data(right) + |
| 1171 | btrfs_item_offset(right->items + push_items - 1), |
| 1172 | push_space); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1173 | old_left_nritems = btrfs_header_nritems(&left->header); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1174 | BUG_ON(old_left_nritems < 0); |
| 1175 | |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1176 | for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1177 | u32 ioff = btrfs_item_offset(left->items + i); |
| 1178 | btrfs_set_item_offset(left->items + i, ioff - |
| 1179 | (BTRFS_LEAF_DATA_SIZE(root) - |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1180 | btrfs_item_offset(left->items + |
| 1181 | old_left_nritems - 1))); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1182 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1183 | btrfs_set_header_nritems(&left->header, old_left_nritems + push_items); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1184 | |
| 1185 | /* fixup right node */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1186 | push_space = btrfs_item_offset(right->items + push_items - 1) - |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1187 | leaf_data_end(root, right); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1188 | btrfs_memmove(root, right, btrfs_leaf_data(right) + |
| 1189 | BTRFS_LEAF_DATA_SIZE(root) - push_space, |
| 1190 | btrfs_leaf_data(right) + |
| 1191 | leaf_data_end(root, right), push_space); |
| 1192 | btrfs_memmove(root, right, right->items, right->items + push_items, |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1193 | (btrfs_header_nritems(&right->header) - push_items) * |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1194 | sizeof(struct btrfs_item)); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1195 | btrfs_set_header_nritems(&right->header, |
| 1196 | btrfs_header_nritems(&right->header) - |
| 1197 | push_items); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1198 | push_space = BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1199 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1200 | for (i = 0; i < btrfs_header_nritems(&right->header); i++) { |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1201 | btrfs_set_item_offset(right->items + i, push_space - |
| 1202 | btrfs_item_size(right->items + i)); |
| 1203 | push_space = btrfs_item_offset(right->items + i); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1204 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1205 | |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1206 | btrfs_mark_buffer_dirty(t); |
| 1207 | btrfs_mark_buffer_dirty(right_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1208 | wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1209 | if (wret) |
| 1210 | ret = wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1211 | |
| 1212 | /* then fixup the leaf pointer in the path */ |
| 1213 | if (path->slots[0] < push_items) { |
| 1214 | path->slots[0] += old_left_nritems; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1215 | btrfs_block_release(root, path->nodes[0]); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1216 | path->nodes[0] = t; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1217 | path->slots[1] -= 1; |
| 1218 | } else { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1219 | btrfs_block_release(root, t); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1220 | path->slots[0] -= push_items; |
| 1221 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1222 | BUG_ON(path->slots[0] < 0); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1223 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1224 | } |
| 1225 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1226 | /* |
| 1227 | * split the path's leaf in two, making sure there is at least data_size |
| 1228 | * available for the resulting leaf level of the path. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1229 | * |
| 1230 | * returns 0 if all went well and < 0 on failure. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1231 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1232 | static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1233 | *root, struct btrfs_key *ins_key, |
| 1234 | struct btrfs_path *path, int data_size) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1235 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1236 | struct buffer_head *l_buf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1237 | struct btrfs_leaf *l; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1238 | u32 nritems; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1239 | int mid; |
| 1240 | int slot; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1241 | struct btrfs_leaf *right; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1242 | struct buffer_head *right_buffer; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1243 | int space_needed = data_size + sizeof(struct btrfs_item); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1244 | int data_copy_size; |
| 1245 | int rt_data_off; |
| 1246 | int i; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1247 | int ret = 0; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1248 | int wret; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1249 | int double_split = 0; |
| 1250 | struct btrfs_disk_key disk_key; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1251 | |
Chris Mason | 4068947 | 2007-03-17 14:29:23 -0400 | [diff] [blame] | 1252 | /* first try to make some room by pushing left and right */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1253 | wret = push_leaf_left(trans, root, path, data_size); |
Chris Mason | eaee50e | 2007-03-13 11:17:52 -0400 | [diff] [blame] | 1254 | if (wret < 0) |
| 1255 | return wret; |
| 1256 | if (wret) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1257 | wret = push_leaf_right(trans, root, path, data_size); |
Chris Mason | eaee50e | 2007-03-13 11:17:52 -0400 | [diff] [blame] | 1258 | if (wret < 0) |
| 1259 | return wret; |
| 1260 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1261 | l_buf = path->nodes[0]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1262 | l = btrfs_buffer_leaf(l_buf); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1263 | |
| 1264 | /* did the pushes work? */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1265 | if (btrfs_leaf_free_space(root, l) >= |
| 1266 | sizeof(struct btrfs_item) + data_size) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1267 | return 0; |
| 1268 | |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1269 | if (!path->nodes[1]) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1270 | ret = insert_new_root(trans, root, path, 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1271 | if (ret) |
| 1272 | return ret; |
| 1273 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1274 | slot = path->slots[0]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1275 | nritems = btrfs_header_nritems(&l->header); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1276 | mid = (nritems + 1)/ 2; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1277 | right_buffer = btrfs_alloc_free_block(trans, root); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1278 | BUG_ON(!right_buffer); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1279 | right = btrfs_buffer_leaf(right_buffer); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1280 | memset(&right->header, 0, sizeof(right->header)); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1281 | btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer)); |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 1282 | btrfs_set_header_generation(&right->header, trans->transid); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1283 | btrfs_set_header_level(&right->header, 0); |
Chris Mason | 3eb0314 | 2007-04-05 14:28:50 -0400 | [diff] [blame] | 1284 | memcpy(right->header.fsid, root->fs_info->disk_super->fsid, |
| 1285 | sizeof(right->header.fsid)); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1286 | if (mid <= slot) { |
| 1287 | if (nritems == 1 || |
| 1288 | leaf_space_used(l, mid, nritems - mid) + space_needed > |
| 1289 | BTRFS_LEAF_DATA_SIZE(root)) { |
| 1290 | if (slot >= nritems) { |
| 1291 | btrfs_cpu_key_to_disk(&disk_key, ins_key); |
| 1292 | btrfs_set_header_nritems(&right->header, 0); |
| 1293 | wret = insert_ptr(trans, root, path, |
| 1294 | &disk_key, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1295 | bh_blocknr(right_buffer), |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1296 | path->slots[1] + 1, 1); |
| 1297 | if (wret) |
| 1298 | ret = wret; |
| 1299 | btrfs_block_release(root, path->nodes[0]); |
| 1300 | path->nodes[0] = right_buffer; |
| 1301 | path->slots[0] = 0; |
| 1302 | path->slots[1] += 1; |
| 1303 | return ret; |
| 1304 | } |
| 1305 | mid = slot; |
| 1306 | double_split = 1; |
| 1307 | } |
| 1308 | } else { |
| 1309 | if (leaf_space_used(l, 0, mid + 1) + space_needed > |
| 1310 | BTRFS_LEAF_DATA_SIZE(root)) { |
| 1311 | if (slot == 0) { |
| 1312 | btrfs_cpu_key_to_disk(&disk_key, ins_key); |
| 1313 | btrfs_set_header_nritems(&right->header, 0); |
| 1314 | wret = insert_ptr(trans, root, path, |
| 1315 | &disk_key, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1316 | bh_blocknr(right_buffer), |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1317 | path->slots[1] - 1, 1); |
| 1318 | if (wret) |
| 1319 | ret = wret; |
| 1320 | btrfs_block_release(root, path->nodes[0]); |
| 1321 | path->nodes[0] = right_buffer; |
| 1322 | path->slots[0] = 0; |
| 1323 | path->slots[1] -= 1; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1324 | if (path->slots[1] == 0) { |
| 1325 | wret = fixup_low_keys(trans, root, |
| 1326 | path, &disk_key, 1); |
| 1327 | if (wret) |
| 1328 | ret = wret; |
| 1329 | } |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1330 | return ret; |
| 1331 | } |
| 1332 | mid = slot; |
| 1333 | double_split = 1; |
| 1334 | } |
| 1335 | } |
| 1336 | btrfs_set_header_nritems(&right->header, nritems - mid); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1337 | data_copy_size = btrfs_item_end(l->items + mid) - |
| 1338 | leaf_data_end(root, l); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1339 | btrfs_memcpy(root, right, right->items, l->items + mid, |
| 1340 | (nritems - mid) * sizeof(struct btrfs_item)); |
| 1341 | btrfs_memcpy(root, right, |
| 1342 | btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - |
| 1343 | data_copy_size, btrfs_leaf_data(l) + |
| 1344 | leaf_data_end(root, l), data_copy_size); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1345 | rt_data_off = BTRFS_LEAF_DATA_SIZE(root) - |
| 1346 | btrfs_item_end(l->items + mid); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1347 | |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1348 | for (i = 0; i < btrfs_header_nritems(&right->header); i++) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1349 | u32 ioff = btrfs_item_offset(right->items + i); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1350 | btrfs_set_item_offset(right->items + i, ioff + rt_data_off); |
| 1351 | } |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1352 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1353 | btrfs_set_header_nritems(&l->header, mid); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1354 | ret = 0; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1355 | wret = insert_ptr(trans, root, path, &right->items[0].key, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1356 | bh_blocknr(right_buffer), path->slots[1] + 1, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1357 | if (wret) |
| 1358 | ret = wret; |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1359 | btrfs_mark_buffer_dirty(right_buffer); |
| 1360 | btrfs_mark_buffer_dirty(l_buf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1361 | BUG_ON(path->slots[0] != slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1362 | if (mid <= slot) { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1363 | btrfs_block_release(root, path->nodes[0]); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1364 | path->nodes[0] = right_buffer; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1365 | path->slots[0] -= mid; |
| 1366 | path->slots[1] += 1; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1367 | } else |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1368 | btrfs_block_release(root, right_buffer); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1369 | BUG_ON(path->slots[0] < 0); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1370 | |
| 1371 | if (!double_split) |
| 1372 | return ret; |
| 1373 | right_buffer = btrfs_alloc_free_block(trans, root); |
| 1374 | BUG_ON(!right_buffer); |
| 1375 | right = btrfs_buffer_leaf(right_buffer); |
| 1376 | memset(&right->header, 0, sizeof(right->header)); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1377 | btrfs_set_header_blocknr(&right->header, bh_blocknr(right_buffer)); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1378 | btrfs_set_header_generation(&right->header, trans->transid); |
| 1379 | btrfs_set_header_level(&right->header, 0); |
Chris Mason | 3eb0314 | 2007-04-05 14:28:50 -0400 | [diff] [blame] | 1380 | memcpy(right->header.fsid, root->fs_info->disk_super->fsid, |
| 1381 | sizeof(right->header.fsid)); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1382 | btrfs_cpu_key_to_disk(&disk_key, ins_key); |
| 1383 | btrfs_set_header_nritems(&right->header, 0); |
| 1384 | wret = insert_ptr(trans, root, path, |
| 1385 | &disk_key, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1386 | bh_blocknr(right_buffer), |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1387 | path->slots[1], 1); |
| 1388 | if (wret) |
| 1389 | ret = wret; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1390 | if (path->slots[1] == 0) { |
| 1391 | wret = fixup_low_keys(trans, root, path, &disk_key, 1); |
| 1392 | if (wret) |
| 1393 | ret = wret; |
| 1394 | } |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1395 | btrfs_block_release(root, path->nodes[0]); |
| 1396 | path->nodes[0] = right_buffer; |
| 1397 | path->slots[0] = 0; |
| 1398 | check_node(root, path, 1); |
| 1399 | check_leaf(root, path, 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1400 | return ret; |
| 1401 | } |
| 1402 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1403 | int btrfs_truncate_item(struct btrfs_trans_handle *trans, |
| 1404 | struct btrfs_root *root, |
| 1405 | struct btrfs_path *path, |
| 1406 | u32 new_size) |
| 1407 | { |
| 1408 | int ret = 0; |
| 1409 | int slot; |
| 1410 | int slot_orig; |
| 1411 | struct btrfs_leaf *leaf; |
| 1412 | struct buffer_head *leaf_buf; |
| 1413 | u32 nritems; |
| 1414 | unsigned int data_end; |
| 1415 | unsigned int old_data_start; |
| 1416 | unsigned int old_size; |
| 1417 | unsigned int size_diff; |
| 1418 | int i; |
| 1419 | |
| 1420 | slot_orig = path->slots[0]; |
| 1421 | leaf_buf = path->nodes[0]; |
| 1422 | leaf = btrfs_buffer_leaf(leaf_buf); |
| 1423 | |
| 1424 | nritems = btrfs_header_nritems(&leaf->header); |
| 1425 | data_end = leaf_data_end(root, leaf); |
| 1426 | |
| 1427 | slot = path->slots[0]; |
| 1428 | old_data_start = btrfs_item_offset(leaf->items + slot); |
| 1429 | old_size = btrfs_item_size(leaf->items + slot); |
| 1430 | BUG_ON(old_size <= new_size); |
| 1431 | size_diff = old_size - new_size; |
| 1432 | |
| 1433 | BUG_ON(slot < 0); |
| 1434 | BUG_ON(slot >= nritems); |
| 1435 | |
| 1436 | /* |
| 1437 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 1438 | */ |
| 1439 | /* first correct the data pointers */ |
| 1440 | for (i = slot; i < nritems; i++) { |
| 1441 | u32 ioff = btrfs_item_offset(leaf->items + i); |
| 1442 | btrfs_set_item_offset(leaf->items + i, |
| 1443 | ioff + size_diff); |
| 1444 | } |
| 1445 | /* shift the data */ |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1446 | btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) + |
| 1447 | data_end + size_diff, btrfs_leaf_data(leaf) + |
| 1448 | data_end, old_data_start + new_size - data_end); |
| 1449 | btrfs_set_item_size(leaf->items + slot, new_size); |
| 1450 | btrfs_mark_buffer_dirty(leaf_buf); |
| 1451 | |
| 1452 | ret = 0; |
| 1453 | if (btrfs_leaf_free_space(root, leaf) < 0) |
| 1454 | BUG(); |
| 1455 | check_leaf(root, path, 0); |
| 1456 | return ret; |
| 1457 | } |
| 1458 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1459 | int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1460 | *root, struct btrfs_path *path, u32 data_size) |
| 1461 | { |
| 1462 | int ret = 0; |
| 1463 | int slot; |
| 1464 | int slot_orig; |
| 1465 | struct btrfs_leaf *leaf; |
| 1466 | struct buffer_head *leaf_buf; |
| 1467 | u32 nritems; |
| 1468 | unsigned int data_end; |
| 1469 | unsigned int old_data; |
| 1470 | unsigned int old_size; |
| 1471 | int i; |
| 1472 | |
| 1473 | slot_orig = path->slots[0]; |
| 1474 | leaf_buf = path->nodes[0]; |
| 1475 | leaf = btrfs_buffer_leaf(leaf_buf); |
| 1476 | |
| 1477 | nritems = btrfs_header_nritems(&leaf->header); |
| 1478 | data_end = leaf_data_end(root, leaf); |
| 1479 | |
| 1480 | if (btrfs_leaf_free_space(root, leaf) < data_size) |
| 1481 | BUG(); |
| 1482 | slot = path->slots[0]; |
| 1483 | old_data = btrfs_item_end(leaf->items + slot); |
| 1484 | |
| 1485 | BUG_ON(slot < 0); |
| 1486 | BUG_ON(slot >= nritems); |
| 1487 | |
| 1488 | /* |
| 1489 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 1490 | */ |
| 1491 | /* first correct the data pointers */ |
| 1492 | for (i = slot; i < nritems; i++) { |
| 1493 | u32 ioff = btrfs_item_offset(leaf->items + i); |
| 1494 | btrfs_set_item_offset(leaf->items + i, |
| 1495 | ioff - data_size); |
| 1496 | } |
| 1497 | /* shift the data */ |
| 1498 | btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) + |
| 1499 | data_end - data_size, btrfs_leaf_data(leaf) + |
| 1500 | data_end, old_data - data_end); |
| 1501 | data_end = old_data; |
| 1502 | old_size = btrfs_item_size(leaf->items + slot); |
| 1503 | btrfs_set_item_size(leaf->items + slot, old_size + data_size); |
| 1504 | btrfs_mark_buffer_dirty(leaf_buf); |
| 1505 | |
| 1506 | ret = 0; |
| 1507 | if (btrfs_leaf_free_space(root, leaf) < 0) |
| 1508 | BUG(); |
| 1509 | check_leaf(root, path, 0); |
| 1510 | return ret; |
| 1511 | } |
| 1512 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1513 | /* |
| 1514 | * Given a key and some data, insert an item into the tree. |
| 1515 | * This does all the path init required, making room in the tree if needed. |
| 1516 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1517 | int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1518 | *root, struct btrfs_path *path, struct btrfs_key |
| 1519 | *cpu_key, u32 data_size) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1520 | { |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1521 | int ret = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1522 | int slot; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1523 | int slot_orig; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1524 | struct btrfs_leaf *leaf; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1525 | struct buffer_head *leaf_buf; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1526 | u32 nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1527 | unsigned int data_end; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1528 | struct btrfs_disk_key disk_key; |
| 1529 | |
| 1530 | btrfs_cpu_key_to_disk(&disk_key, cpu_key); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1531 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1532 | /* create a root if there isn't one */ |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1533 | if (!root->node) |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 1534 | BUG(); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1535 | ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1536 | if (ret == 0) { |
Chris Mason | f0930a3 | 2007-03-02 09:47:58 -0500 | [diff] [blame] | 1537 | return -EEXIST; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1538 | } |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 1539 | if (ret < 0) |
| 1540 | goto out; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1541 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1542 | slot_orig = path->slots[0]; |
| 1543 | leaf_buf = path->nodes[0]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1544 | leaf = btrfs_buffer_leaf(leaf_buf); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1545 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1546 | nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1547 | data_end = leaf_data_end(root, leaf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1548 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1549 | if (btrfs_leaf_free_space(root, leaf) < |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1550 | sizeof(struct btrfs_item) + data_size) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1551 | BUG(); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1552 | } |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1553 | slot = path->slots[0]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1554 | BUG_ON(slot < 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1555 | if (slot != nritems) { |
| 1556 | int i; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1557 | unsigned int old_data = btrfs_item_end(leaf->items + slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1558 | |
| 1559 | /* |
| 1560 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 1561 | */ |
| 1562 | /* first correct the data pointers */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1563 | for (i = slot; i < nritems; i++) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1564 | u32 ioff = btrfs_item_offset(leaf->items + i); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1565 | btrfs_set_item_offset(leaf->items + i, |
| 1566 | ioff - data_size); |
| 1567 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1568 | |
| 1569 | /* shift the items */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1570 | btrfs_memmove(root, leaf, leaf->items + slot + 1, |
| 1571 | leaf->items + slot, |
| 1572 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1573 | |
| 1574 | /* shift the data */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1575 | btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) + |
| 1576 | data_end - data_size, btrfs_leaf_data(leaf) + |
| 1577 | data_end, old_data - data_end); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1578 | data_end = old_data; |
| 1579 | } |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1580 | /* setup the item for the new data */ |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1581 | btrfs_memcpy(root, leaf, &leaf->items[slot].key, &disk_key, |
| 1582 | sizeof(struct btrfs_disk_key)); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1583 | btrfs_set_item_offset(leaf->items + slot, data_end - data_size); |
| 1584 | btrfs_set_item_size(leaf->items + slot, data_size); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1585 | btrfs_set_header_nritems(&leaf->header, nritems + 1); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1586 | btrfs_mark_buffer_dirty(leaf_buf); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1587 | |
| 1588 | ret = 0; |
Chris Mason | 8e19f2c | 2007-02-28 09:27:02 -0500 | [diff] [blame] | 1589 | if (slot == 0) |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1590 | ret = fixup_low_keys(trans, root, path, &disk_key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1591 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1592 | if (btrfs_leaf_free_space(root, leaf) < 0) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1593 | BUG(); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1594 | check_leaf(root, path, 0); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 1595 | out: |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1596 | return ret; |
| 1597 | } |
| 1598 | |
| 1599 | /* |
| 1600 | * Given a key and some data, insert an item into the tree. |
| 1601 | * This does all the path init required, making room in the tree if needed. |
| 1602 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1603 | int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1604 | *root, struct btrfs_key *cpu_key, void *data, u32 |
| 1605 | data_size) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1606 | { |
| 1607 | int ret = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1608 | struct btrfs_path *path; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1609 | u8 *ptr; |
| 1610 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1611 | path = btrfs_alloc_path(); |
| 1612 | BUG_ON(!path); |
| 1613 | btrfs_init_path(path); |
| 1614 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1615 | if (!ret) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1616 | ptr = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 1617 | path->slots[0], u8); |
| 1618 | btrfs_memcpy(root, path->nodes[0]->b_data, |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1619 | ptr, data, data_size); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1620 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1621 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1622 | btrfs_release_path(root, path); |
| 1623 | btrfs_free_path(path); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1624 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1625 | } |
| 1626 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1627 | /* |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 1628 | * delete the pointer from a given node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1629 | * |
| 1630 | * If the delete empties a node, the node is removed from the tree, |
| 1631 | * continuing all the way the root if required. The root is converted into |
| 1632 | * a leaf if all the nodes are emptied. |
| 1633 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1634 | static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 1635 | struct btrfs_path *path, int level, int slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1636 | { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1637 | struct btrfs_node *node; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1638 | struct buffer_head *parent = path->nodes[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1639 | u32 nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1640 | int ret = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1641 | int wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1642 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1643 | node = btrfs_buffer_node(parent); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1644 | nritems = btrfs_header_nritems(&node->header); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1645 | if (slot != nritems -1) { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1646 | btrfs_memmove(root, node, node->ptrs + slot, |
| 1647 | node->ptrs + slot + 1, |
| 1648 | sizeof(struct btrfs_key_ptr) * |
| 1649 | (nritems - slot - 1)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1650 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1651 | nritems--; |
| 1652 | btrfs_set_header_nritems(&node->header, nritems); |
| 1653 | if (nritems == 0 && parent == root->node) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1654 | struct btrfs_header *header = btrfs_buffer_header(root->node); |
| 1655 | BUG_ON(btrfs_header_level(header) != 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1656 | /* just turn the root into a leaf and break */ |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1657 | btrfs_set_header_level(header, 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1658 | } else if (slot == 0) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1659 | wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1660 | level + 1); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1661 | if (wret) |
| 1662 | ret = wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1663 | } |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1664 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1665 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1666 | } |
| 1667 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1668 | /* |
| 1669 | * delete the item at the leaf level in path. If that empties |
| 1670 | * the leaf, remove it from the tree |
| 1671 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1672 | int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 1673 | struct btrfs_path *path) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1674 | { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1675 | int slot; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1676 | struct btrfs_leaf *leaf; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1677 | struct buffer_head *leaf_buf; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1678 | int doff; |
| 1679 | int dsize; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1680 | int ret = 0; |
| 1681 | int wret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1682 | u32 nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1683 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1684 | leaf_buf = path->nodes[0]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1685 | leaf = btrfs_buffer_leaf(leaf_buf); |
Chris Mason | 4920c9a | 2007-01-26 16:38:42 -0500 | [diff] [blame] | 1686 | slot = path->slots[0]; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1687 | doff = btrfs_item_offset(leaf->items + slot); |
| 1688 | dsize = btrfs_item_size(leaf->items + slot); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1689 | nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1690 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1691 | if (slot != nritems - 1) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1692 | int i; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1693 | int data_end = leaf_data_end(root, leaf); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1694 | btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) + |
| 1695 | data_end + dsize, |
| 1696 | btrfs_leaf_data(leaf) + data_end, |
| 1697 | doff - data_end); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1698 | for (i = slot + 1; i < nritems; i++) { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1699 | u32 ioff = btrfs_item_offset(leaf->items + i); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1700 | btrfs_set_item_offset(leaf->items + i, ioff + dsize); |
| 1701 | } |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1702 | btrfs_memmove(root, leaf, leaf->items + slot, |
| 1703 | leaf->items + slot + 1, |
| 1704 | sizeof(struct btrfs_item) * |
| 1705 | (nritems - slot - 1)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1706 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1707 | btrfs_set_header_nritems(&leaf->header, nritems - 1); |
| 1708 | nritems--; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1709 | /* delete the leaf if we've emptied it */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1710 | if (nritems == 0) { |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1711 | if (leaf_buf == root->node) { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1712 | btrfs_set_header_level(&leaf->header, 0); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 1713 | } else { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1714 | clean_tree_block(trans, root, leaf_buf); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1715 | wait_on_buffer(leaf_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1716 | wret = del_ptr(trans, root, path, 1, path->slots[1]); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1717 | if (wret) |
| 1718 | ret = wret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1719 | wret = btrfs_free_extent(trans, root, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1720 | bh_blocknr(leaf_buf), 1, 1); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1721 | if (wret) |
| 1722 | ret = wret; |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 1723 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1724 | } else { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1725 | int used = leaf_space_used(leaf, 0, nritems); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1726 | if (slot == 0) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1727 | wret = fixup_low_keys(trans, root, path, |
| 1728 | &leaf->items[0].key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1729 | if (wret) |
| 1730 | ret = wret; |
| 1731 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1732 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1733 | /* delete the leaf if it is mostly empty */ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1734 | if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1735 | /* push_leaf_left fixes the path. |
| 1736 | * make sure the path still points to our leaf |
| 1737 | * for possible call to del_ptr below |
| 1738 | */ |
Chris Mason | 4920c9a | 2007-01-26 16:38:42 -0500 | [diff] [blame] | 1739 | slot = path->slots[1]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1740 | get_bh(leaf_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1741 | wret = push_leaf_left(trans, root, path, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1742 | if (wret < 0) |
| 1743 | ret = wret; |
Chris Mason | f0930a3 | 2007-03-02 09:47:58 -0500 | [diff] [blame] | 1744 | if (path->nodes[0] == leaf_buf && |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1745 | btrfs_header_nritems(&leaf->header)) { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1746 | wret = push_leaf_right(trans, root, path, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1747 | if (wret < 0) |
| 1748 | ret = wret; |
| 1749 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1750 | if (btrfs_header_nritems(&leaf->header) == 0) { |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1751 | u64 blocknr = bh_blocknr(leaf_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1752 | clean_tree_block(trans, root, leaf_buf); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1753 | wait_on_buffer(leaf_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1754 | wret = del_ptr(trans, root, path, 1, slot); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1755 | if (wret) |
| 1756 | ret = wret; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1757 | btrfs_block_release(root, leaf_buf); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1758 | wret = btrfs_free_extent(trans, root, blocknr, |
| 1759 | 1, 1); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1760 | if (wret) |
| 1761 | ret = wret; |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 1762 | } else { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1763 | btrfs_mark_buffer_dirty(leaf_buf); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1764 | btrfs_block_release(root, leaf_buf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1765 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 1766 | } else { |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 1767 | btrfs_mark_buffer_dirty(leaf_buf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1768 | } |
| 1769 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1770 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1771 | } |
| 1772 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1773 | /* |
| 1774 | * walk up the tree as far as required to find the next leaf. |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1775 | * returns 0 if it found something or 1 if there are no greater leaves. |
| 1776 | * returns < 0 on io errors. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1777 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1778 | int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1779 | { |
| 1780 | int slot; |
| 1781 | int level = 1; |
| 1782 | u64 blocknr; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1783 | struct buffer_head *c; |
| 1784 | struct btrfs_node *c_node; |
| 1785 | struct buffer_head *next = NULL; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1786 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1787 | while(level < BTRFS_MAX_LEVEL) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1788 | if (!path->nodes[level]) |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1789 | return 1; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1790 | slot = path->slots[level] + 1; |
| 1791 | c = path->nodes[level]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1792 | c_node = btrfs_buffer_node(c); |
| 1793 | if (slot >= btrfs_header_nritems(&c_node->header)) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1794 | level++; |
| 1795 | continue; |
| 1796 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1797 | blocknr = btrfs_node_blockptr(c_node, slot); |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 1798 | if (next) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1799 | btrfs_block_release(root, next); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1800 | next = read_tree_block(root, blocknr); |
| 1801 | break; |
| 1802 | } |
| 1803 | path->slots[level] = slot; |
| 1804 | while(1) { |
| 1805 | level--; |
| 1806 | c = path->nodes[level]; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1807 | btrfs_block_release(root, c); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1808 | path->nodes[level] = next; |
| 1809 | path->slots[level] = 0; |
| 1810 | if (!level) |
| 1811 | break; |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 1812 | next = read_tree_block(root, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1813 | btrfs_node_blockptr(btrfs_buffer_node(next), 0)); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1814 | } |
| 1815 | return 0; |
| 1816 | } |