Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include "kerncompat.h" |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 4 | #include "radix-tree.h" |
| 5 | #include "ctree.h" |
| 6 | #include "disk-io.h" |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 7 | #include "print-tree.h" |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 8 | |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 9 | static int split_node(struct ctree_root *root, struct ctree_path *path, |
| 10 | int level); |
| 11 | static int split_leaf(struct ctree_root *root, struct ctree_path *path, |
| 12 | int data_size); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 13 | static int push_node_left(struct ctree_root *root, struct tree_buffer *dst, |
| 14 | struct tree_buffer *src); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 15 | static int balance_node_right(struct ctree_root *root, |
| 16 | struct tree_buffer *dst_buf, |
| 17 | struct tree_buffer *src_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 18 | static int del_ptr(struct ctree_root *root, struct ctree_path *path, int level, |
| 19 | int slot); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 20 | |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 21 | inline void init_path(struct ctree_path *p) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 22 | { |
| 23 | memset(p, 0, sizeof(*p)); |
| 24 | } |
| 25 | |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 26 | void release_path(struct ctree_root *root, struct ctree_path *p) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 27 | { |
| 28 | int i; |
| 29 | for (i = 0; i < MAX_LEVEL; i++) { |
| 30 | if (!p->nodes[i]) |
| 31 | break; |
| 32 | tree_block_release(root, p->nodes[i]); |
| 33 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 34 | memset(p, 0, sizeof(*p)); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 35 | } |
| 36 | |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 37 | int btrfs_cow_block(struct ctree_root *root, |
| 38 | struct tree_buffer *buf, |
| 39 | struct tree_buffer *parent, |
| 40 | int parent_slot, |
| 41 | struct tree_buffer **cow_ret) |
| 42 | { |
| 43 | struct tree_buffer *cow; |
| 44 | |
| 45 | if (!list_empty(&buf->dirty)) { |
| 46 | *cow_ret = buf; |
| 47 | return 0; |
| 48 | } |
| 49 | cow = alloc_free_block(root); |
| 50 | memcpy(&cow->node, &buf->node, sizeof(buf->node)); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 51 | btrfs_set_header_blocknr(&cow->node.header, cow->blocknr); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 52 | *cow_ret = cow; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 53 | btrfs_inc_ref(root, buf); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 54 | if (buf == root->node) { |
| 55 | root->node = cow; |
| 56 | cow->count++; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 57 | if (buf != root->commit_root) |
| 58 | free_extent(root, buf->blocknr, 1); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 59 | tree_block_release(root, buf); |
| 60 | } else { |
| 61 | parent->node.blockptrs[parent_slot] = cow->blocknr; |
| 62 | BUG_ON(list_empty(&parent->dirty)); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 63 | free_extent(root, buf->blocknr, 1); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 64 | } |
| 65 | tree_block_release(root, buf); |
| 66 | return 0; |
| 67 | } |
| 68 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 69 | /* |
| 70 | * The leaf data grows from end-to-front in the node. |
| 71 | * this returns the address of the start of the last item, |
| 72 | * which is the stop of the leaf data stack |
| 73 | */ |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 74 | static inline unsigned int leaf_data_end(struct leaf *leaf) |
| 75 | { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 76 | u32 nr = btrfs_header_nritems(&leaf->header); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 77 | if (nr == 0) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 78 | return sizeof(leaf->data); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 79 | return btrfs_item_offset(leaf->items + nr - 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 80 | } |
| 81 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 82 | /* |
| 83 | * The space between the end of the leaf items and |
| 84 | * the start of the leaf data. IOW, how much room |
| 85 | * the leaf has left for both items and data |
| 86 | */ |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 87 | int leaf_free_space(struct leaf *leaf) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 88 | { |
| 89 | int data_end = leaf_data_end(leaf); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 90 | int nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 91 | char *items_end = (char *)(leaf->items + nritems + 1); |
| 92 | return (char *)(leaf->data + data_end) - (char *)items_end; |
| 93 | } |
| 94 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 95 | /* |
| 96 | * compare two keys in a memcmp fashion |
| 97 | */ |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 98 | int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 99 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 100 | struct btrfs_key k1; |
| 101 | |
| 102 | btrfs_disk_key_to_cpu(&k1, disk); |
| 103 | |
| 104 | if (k1.objectid > k2->objectid) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 105 | return 1; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 106 | if (k1.objectid < k2->objectid) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 107 | return -1; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 108 | if (k1.flags > k2->flags) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 109 | return 1; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 110 | if (k1.flags < k2->flags) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 111 | return -1; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 112 | if (k1.offset > k2->offset) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 113 | return 1; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 114 | if (k1.offset < k2->offset) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 115 | return -1; |
| 116 | return 0; |
| 117 | } |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 118 | |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 119 | int check_node(struct ctree_path *path, int level) |
| 120 | { |
| 121 | int i; |
| 122 | struct node *parent = NULL; |
| 123 | struct node *node = &path->nodes[level]->node; |
| 124 | int parent_slot; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 125 | u32 nritems = btrfs_header_nritems(&node->header); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 126 | |
| 127 | if (path->nodes[level + 1]) |
| 128 | parent = &path->nodes[level + 1]->node; |
| 129 | parent_slot = path->slots[level + 1]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 130 | BUG_ON(nritems == 0); |
| 131 | if (parent) { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 132 | struct btrfs_disk_key *parent_key; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 133 | parent_key = &parent->keys[parent_slot]; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 134 | BUG_ON(memcmp(parent_key, node->keys, |
| 135 | sizeof(struct btrfs_disk_key))); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 136 | BUG_ON(parent->blockptrs[parent_slot] != |
| 137 | btrfs_header_blocknr(&node->header)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 138 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 139 | BUG_ON(nritems > NODEPTRS_PER_BLOCK); |
| 140 | for (i = 0; nritems > 1 && i < nritems - 2; i++) { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 141 | struct btrfs_key cpukey; |
| 142 | btrfs_disk_key_to_cpu(&cpukey, &node->keys[i + 1]); |
| 143 | BUG_ON(comp_keys(&node->keys[i], &cpukey) >= 0); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 144 | } |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | int check_leaf(struct ctree_path *path, int level) |
| 149 | { |
| 150 | int i; |
| 151 | struct leaf *leaf = &path->nodes[level]->leaf; |
| 152 | struct node *parent = NULL; |
| 153 | int parent_slot; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 154 | u32 nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 155 | |
| 156 | if (path->nodes[level + 1]) |
| 157 | parent = &path->nodes[level + 1]->node; |
| 158 | parent_slot = path->slots[level + 1]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 159 | BUG_ON(leaf_free_space(leaf) < 0); |
| 160 | |
| 161 | if (nritems == 0) |
| 162 | return 0; |
| 163 | |
| 164 | if (parent) { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 165 | struct btrfs_disk_key *parent_key; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 166 | parent_key = &parent->keys[parent_slot]; |
| 167 | BUG_ON(memcmp(parent_key, &leaf->items[0].key, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 168 | sizeof(struct btrfs_disk_key))); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 169 | BUG_ON(parent->blockptrs[parent_slot] != |
| 170 | btrfs_header_blocknr(&leaf->header)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 171 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 172 | for (i = 0; nritems > 1 && i < nritems - 2; i++) { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 173 | struct btrfs_key cpukey; |
| 174 | btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 175 | BUG_ON(comp_keys(&leaf->items[i].key, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 176 | &cpukey) >= 0); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 177 | BUG_ON(btrfs_item_offset(leaf->items + i) != |
| 178 | btrfs_item_end(leaf->items + i + 1)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 179 | if (i == 0) { |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 180 | BUG_ON(btrfs_item_offset(leaf->items + i) + |
| 181 | btrfs_item_size(leaf->items + i) != |
| 182 | LEAF_DATA_SIZE); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 183 | } |
| 184 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | int check_block(struct ctree_path *path, int level) |
| 189 | { |
| 190 | if (level == 0) |
| 191 | return check_leaf(path, level); |
| 192 | return check_node(path, level); |
| 193 | } |
| 194 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 195 | /* |
| 196 | * search for key in the array p. items p are item_size apart |
| 197 | * and there are 'max' items in p |
| 198 | * the slot in the array is returned via slot, and it points to |
| 199 | * the place where you would insert key if it is not found in |
| 200 | * the array. |
| 201 | * |
| 202 | * slot may point to max if the key is bigger than all of the keys |
| 203 | */ |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 204 | 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] | 205 | int max, int *slot) |
| 206 | { |
| 207 | int low = 0; |
| 208 | int high = max; |
| 209 | int mid; |
| 210 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 211 | struct btrfs_disk_key *tmp; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 212 | |
| 213 | while(low < high) { |
| 214 | mid = (low + high) / 2; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 215 | tmp = (struct btrfs_disk_key *)(p + mid * item_size); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 216 | ret = comp_keys(tmp, key); |
| 217 | |
| 218 | if (ret < 0) |
| 219 | low = mid + 1; |
| 220 | else if (ret > 0) |
| 221 | high = mid; |
| 222 | else { |
| 223 | *slot = mid; |
| 224 | return 0; |
| 225 | } |
| 226 | } |
| 227 | *slot = low; |
| 228 | return 1; |
| 229 | } |
| 230 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 231 | /* |
| 232 | * simple bin_search frontend that does the right thing for |
| 233 | * leaves vs nodes |
| 234 | */ |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 235 | int bin_search(struct node *c, struct btrfs_key *key, int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 236 | { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 237 | if (btrfs_is_leaf(c)) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 238 | struct leaf *l = (struct leaf *)c; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 239 | return generic_bin_search((void *)l->items, |
| 240 | sizeof(struct btrfs_item), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 241 | key, btrfs_header_nritems(&c->header), |
| 242 | slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 243 | } else { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 244 | return generic_bin_search((void *)c->keys, |
| 245 | sizeof(struct btrfs_disk_key), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 246 | key, btrfs_header_nritems(&c->header), |
| 247 | slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 248 | } |
| 249 | return -1; |
| 250 | } |
| 251 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 252 | struct tree_buffer *read_node_slot(struct ctree_root *root, |
| 253 | struct tree_buffer *parent_buf, |
| 254 | int slot) |
| 255 | { |
| 256 | struct node *node = &parent_buf->node; |
| 257 | if (slot < 0) |
| 258 | return NULL; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 259 | if (slot >= btrfs_header_nritems(&node->header)) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 260 | return NULL; |
| 261 | return read_tree_block(root, node->blockptrs[slot]); |
| 262 | } |
| 263 | |
| 264 | static int balance_level(struct ctree_root *root, struct ctree_path *path, |
| 265 | int level) |
| 266 | { |
| 267 | struct tree_buffer *right_buf; |
| 268 | struct tree_buffer *mid_buf; |
| 269 | struct tree_buffer *left_buf; |
| 270 | struct tree_buffer *parent_buf = NULL; |
| 271 | struct node *right = NULL; |
| 272 | struct node *mid; |
| 273 | struct node *left = NULL; |
| 274 | struct node *parent = NULL; |
| 275 | int ret = 0; |
| 276 | int wret; |
| 277 | int pslot; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 278 | int orig_slot = path->slots[level]; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 279 | u64 orig_ptr; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 280 | |
| 281 | if (level == 0) |
| 282 | return 0; |
| 283 | |
| 284 | mid_buf = path->nodes[level]; |
| 285 | mid = &mid_buf->node; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 286 | orig_ptr = mid->blockptrs[orig_slot]; |
| 287 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 288 | if (level < MAX_LEVEL - 1) |
| 289 | parent_buf = path->nodes[level + 1]; |
| 290 | pslot = path->slots[level + 1]; |
| 291 | |
| 292 | if (!parent_buf) { |
| 293 | struct tree_buffer *child; |
| 294 | u64 blocknr = mid_buf->blocknr; |
| 295 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 296 | if (btrfs_header_nritems(&mid->header) != 1) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 297 | return 0; |
| 298 | |
| 299 | /* promote the child to a root */ |
| 300 | child = read_node_slot(root, mid_buf, 0); |
| 301 | BUG_ON(!child); |
| 302 | root->node = child; |
| 303 | path->nodes[level] = NULL; |
| 304 | /* once for the path */ |
| 305 | tree_block_release(root, mid_buf); |
| 306 | /* once for the root ptr */ |
| 307 | tree_block_release(root, mid_buf); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 308 | clean_tree_block(root, mid_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 309 | return free_extent(root, blocknr, 1); |
| 310 | } |
| 311 | parent = &parent_buf->node; |
| 312 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 313 | if (btrfs_header_nritems(&mid->header) > NODEPTRS_PER_BLOCK / 4) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 314 | return 0; |
| 315 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 316 | left_buf = read_node_slot(root, parent_buf, pslot - 1); |
| 317 | right_buf = read_node_slot(root, parent_buf, pslot + 1); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 318 | |
| 319 | /* first, try to make some room in the middle buffer */ |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 320 | if (left_buf) { |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 321 | btrfs_cow_block(root, left_buf, parent_buf, |
| 322 | pslot - 1, &left_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 323 | left = &left_buf->node; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 324 | orig_slot += btrfs_header_nritems(&left->header); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 325 | wret = push_node_left(root, left_buf, mid_buf); |
| 326 | if (wret < 0) |
| 327 | ret = wret; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 328 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 329 | |
| 330 | /* |
| 331 | * then try to empty the right most buffer into the middle |
| 332 | */ |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 333 | if (right_buf) { |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 334 | btrfs_cow_block(root, right_buf, parent_buf, |
| 335 | pslot + 1, &right_buf); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 336 | right = &right_buf->node; |
| 337 | wret = push_node_left(root, mid_buf, right_buf); |
| 338 | if (wret < 0) |
| 339 | ret = wret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 340 | if (btrfs_header_nritems(&right->header) == 0) { |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 341 | u64 blocknr = right_buf->blocknr; |
| 342 | tree_block_release(root, right_buf); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 343 | clean_tree_block(root, right_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 344 | right_buf = NULL; |
| 345 | right = NULL; |
| 346 | wret = del_ptr(root, path, level + 1, pslot + 1); |
| 347 | if (wret) |
| 348 | ret = wret; |
| 349 | wret = free_extent(root, blocknr, 1); |
| 350 | if (wret) |
| 351 | ret = wret; |
| 352 | } else { |
| 353 | memcpy(parent->keys + pslot + 1, right->keys, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 354 | sizeof(struct btrfs_disk_key)); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 355 | BUG_ON(list_empty(&parent_buf->dirty)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 356 | } |
| 357 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 358 | if (btrfs_header_nritems(&mid->header) == 1) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 359 | /* |
| 360 | * we're not allowed to leave a node with one item in the |
| 361 | * tree during a delete. A deletion from lower in the tree |
| 362 | * could try to delete the only pointer in this node. |
| 363 | * So, pull some keys from the left. |
| 364 | * There has to be a left pointer at this point because |
| 365 | * otherwise we would have pulled some pointers from the |
| 366 | * right |
| 367 | */ |
| 368 | BUG_ON(!left_buf); |
| 369 | wret = balance_node_right(root, mid_buf, left_buf); |
| 370 | if (wret < 0) |
| 371 | ret = wret; |
| 372 | BUG_ON(wret == 1); |
| 373 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 374 | if (btrfs_header_nritems(&mid->header) == 0) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 375 | /* we've managed to empty the middle node, drop it */ |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 376 | u64 blocknr = mid_buf->blocknr; |
| 377 | tree_block_release(root, mid_buf); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 378 | clean_tree_block(root, mid_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 379 | mid_buf = NULL; |
| 380 | mid = NULL; |
| 381 | wret = del_ptr(root, path, level + 1, pslot); |
| 382 | if (wret) |
| 383 | ret = wret; |
| 384 | wret = free_extent(root, blocknr, 1); |
| 385 | if (wret) |
| 386 | ret = wret; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 387 | } else { |
| 388 | /* update the parent key to reflect our changes */ |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 389 | memcpy(parent->keys + pslot, mid->keys, |
| 390 | sizeof(struct btrfs_disk_key)); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 391 | BUG_ON(list_empty(&parent_buf->dirty)); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 392 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 393 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 394 | /* update the path */ |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 395 | if (left_buf) { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 396 | if (btrfs_header_nritems(&left->header) > orig_slot) { |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 397 | left_buf->count++; // released below |
| 398 | path->nodes[level] = left_buf; |
| 399 | path->slots[level + 1] -= 1; |
| 400 | path->slots[level] = orig_slot; |
| 401 | if (mid_buf) |
| 402 | tree_block_release(root, mid_buf); |
| 403 | } else { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 404 | orig_slot -= btrfs_header_nritems(&left->header); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 405 | path->slots[level] = orig_slot; |
| 406 | } |
| 407 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 408 | /* double check we haven't messed things up */ |
| 409 | check_block(path, level); |
| 410 | if (orig_ptr != path->nodes[level]->node.blockptrs[path->slots[level]]) |
| 411 | BUG(); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 412 | |
| 413 | if (right_buf) |
| 414 | tree_block_release(root, right_buf); |
| 415 | if (left_buf) |
| 416 | tree_block_release(root, left_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 417 | return ret; |
| 418 | } |
| 419 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 420 | /* |
| 421 | * look for key in the tree. path is filled in with nodes along the way |
| 422 | * if key is found, we return zero and you can find the item in the leaf |
| 423 | * level of the path (level 0) |
| 424 | * |
| 425 | * 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] | 426 | * be inserted, and 1 is returned. If there are other errors during the |
| 427 | * search a negative error number is returned. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 428 | * |
| 429 | * if ins_len > 0, nodes and leaves will be split as we walk down the |
| 430 | * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if |
| 431 | * possible) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 432 | */ |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 433 | int search_slot(struct ctree_root *root, struct btrfs_key *key, |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 434 | struct ctree_path *p, int ins_len, int cow) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 435 | { |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 436 | struct tree_buffer *b; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 437 | struct tree_buffer *cow_buf; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 438 | struct node *c; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 439 | int slot; |
| 440 | int ret; |
| 441 | int level; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 442 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 443 | again: |
| 444 | b = root->node; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 445 | b->count++; |
| 446 | while (b) { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 447 | level = btrfs_header_level(&b->node.header); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 448 | if (cow) { |
| 449 | int wret; |
| 450 | wret = btrfs_cow_block(root, b, p->nodes[level + 1], |
| 451 | p->slots[level + 1], &cow_buf); |
| 452 | b = cow_buf; |
| 453 | } |
| 454 | BUG_ON(!cow && ins_len); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 455 | c = &b->node; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 456 | p->nodes[level] = b; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 457 | ret = check_block(p, level); |
| 458 | if (ret) |
| 459 | return -1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 460 | ret = bin_search(c, key, &slot); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 461 | if (!btrfs_is_leaf(c)) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 462 | if (ret && slot > 0) |
| 463 | slot -= 1; |
| 464 | p->slots[level] = slot; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 465 | if (ins_len > 0 && btrfs_header_nritems(&c->header) == |
| 466 | NODEPTRS_PER_BLOCK) { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 467 | int sret = split_node(root, p, level); |
| 468 | BUG_ON(sret > 0); |
| 469 | if (sret) |
| 470 | return sret; |
| 471 | b = p->nodes[level]; |
| 472 | c = &b->node; |
| 473 | slot = p->slots[level]; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 474 | } else if (ins_len < 0) { |
| 475 | int sret = balance_level(root, p, level); |
| 476 | if (sret) |
| 477 | return sret; |
| 478 | b = p->nodes[level]; |
| 479 | if (!b) |
| 480 | goto again; |
| 481 | c = &b->node; |
| 482 | slot = p->slots[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 483 | BUG_ON(btrfs_header_nritems(&c->header) == 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 484 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 485 | b = read_tree_block(root, c->blockptrs[slot]); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 486 | } else { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 487 | struct leaf *l = (struct leaf *)c; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 488 | p->slots[level] = slot; |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 489 | if (ins_len > 0 && leaf_free_space(l) < |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 490 | sizeof(struct btrfs_item) + ins_len) { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 491 | int sret = split_leaf(root, p, ins_len); |
| 492 | BUG_ON(sret > 0); |
| 493 | if (sret) |
| 494 | return sret; |
| 495 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 496 | BUG_ON(root->node->count == 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 497 | return ret; |
| 498 | } |
| 499 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 500 | BUG_ON(root->node->count == 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 501 | return 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 502 | } |
| 503 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 504 | /* |
| 505 | * adjust the pointers going up the tree, starting at level |
| 506 | * making sure the right key of each node is points to 'key'. |
| 507 | * This is used after shifting pointers to the left, so it stops |
| 508 | * fixing up pointers when a given leaf/node is not in slot 0 of the |
| 509 | * higher levels |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 510 | * |
| 511 | * If this fails to write a tree block, it returns -1, but continues |
| 512 | * fixing up the blocks in ram so the tree is consistent. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 513 | */ |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 514 | static int fixup_low_keys(struct ctree_root *root, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 515 | struct ctree_path *path, struct btrfs_disk_key *key, |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 516 | int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 517 | { |
| 518 | int i; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 519 | int ret = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 520 | for (i = level; i < MAX_LEVEL; i++) { |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 521 | struct node *t; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 522 | int tslot = path->slots[i]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 523 | if (!path->nodes[i]) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 524 | break; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 525 | t = &path->nodes[i]->node; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 526 | memcpy(t->keys + tslot, key, sizeof(*key)); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 527 | BUG_ON(list_empty(&path->nodes[i]->dirty)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 528 | if (tslot != 0) |
| 529 | break; |
| 530 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 531 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 532 | } |
| 533 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 534 | /* |
| 535 | * 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] | 536 | * tree. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 537 | * |
| 538 | * returns 0 if some ptrs were pushed left, < 0 if there was some horrible |
| 539 | * 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] | 540 | */ |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 541 | static int push_node_left(struct ctree_root *root, struct tree_buffer *dst_buf, |
| 542 | struct tree_buffer *src_buf) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 543 | { |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 544 | struct node *src = &src_buf->node; |
| 545 | struct node *dst = &dst_buf->node; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 546 | int push_items = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 547 | int src_nritems; |
| 548 | int dst_nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 549 | int ret = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 550 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 551 | src_nritems = btrfs_header_nritems(&src->header); |
| 552 | dst_nritems = btrfs_header_nritems(&dst->header); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 553 | push_items = NODEPTRS_PER_BLOCK - dst_nritems; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 554 | if (push_items <= 0) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 555 | return 1; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 556 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 557 | |
| 558 | if (src_nritems < push_items) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 559 | push_items = src_nritems; |
| 560 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 561 | memcpy(dst->keys + dst_nritems, src->keys, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 562 | push_items * sizeof(struct btrfs_disk_key)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 563 | memcpy(dst->blockptrs + dst_nritems, src->blockptrs, |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 564 | push_items * sizeof(u64)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 565 | if (push_items < src_nritems) { |
| 566 | memmove(src->keys, src->keys + push_items, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 567 | (src_nritems - push_items) * |
| 568 | sizeof(struct btrfs_disk_key)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 569 | memmove(src->blockptrs, src->blockptrs + push_items, |
| 570 | (src_nritems - push_items) * sizeof(u64)); |
| 571 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 572 | btrfs_set_header_nritems(&src->header, src_nritems - push_items); |
| 573 | btrfs_set_header_nritems(&dst->header, dst_nritems + push_items); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 574 | BUG_ON(list_empty(&src_buf->dirty)); |
| 575 | BUG_ON(list_empty(&dst_buf->dirty)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 576 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 577 | } |
| 578 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 579 | /* |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 580 | * try to push data from one node into the next node right in the |
| 581 | * tree. |
| 582 | * |
| 583 | * returns 0 if some ptrs were pushed, < 0 if there was some horrible |
| 584 | * error, and > 0 if there was no room in the right hand block. |
| 585 | * |
| 586 | * this will only push up to 1/2 the contents of the left node over |
| 587 | */ |
| 588 | static int balance_node_right(struct ctree_root *root, |
| 589 | struct tree_buffer *dst_buf, |
| 590 | struct tree_buffer *src_buf) |
| 591 | { |
| 592 | struct node *src = &src_buf->node; |
| 593 | struct node *dst = &dst_buf->node; |
| 594 | int push_items = 0; |
| 595 | int max_push; |
| 596 | int src_nritems; |
| 597 | int dst_nritems; |
| 598 | int ret = 0; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 599 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 600 | src_nritems = btrfs_header_nritems(&src->header); |
| 601 | dst_nritems = btrfs_header_nritems(&dst->header); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 602 | push_items = NODEPTRS_PER_BLOCK - dst_nritems; |
| 603 | if (push_items <= 0) { |
| 604 | return 1; |
| 605 | } |
| 606 | |
| 607 | max_push = src_nritems / 2 + 1; |
| 608 | /* don't try to empty the node */ |
| 609 | if (max_push > src_nritems) |
| 610 | return 1; |
| 611 | if (max_push < push_items) |
| 612 | push_items = max_push; |
| 613 | |
| 614 | memmove(dst->keys + push_items, dst->keys, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 615 | dst_nritems * sizeof(struct btrfs_disk_key)); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 616 | memmove(dst->blockptrs + push_items, dst->blockptrs, |
| 617 | dst_nritems * sizeof(u64)); |
| 618 | memcpy(dst->keys, src->keys + src_nritems - push_items, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 619 | push_items * sizeof(struct btrfs_disk_key)); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 620 | memcpy(dst->blockptrs, src->blockptrs + src_nritems - push_items, |
| 621 | push_items * sizeof(u64)); |
| 622 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 623 | btrfs_set_header_nritems(&src->header, src_nritems - push_items); |
| 624 | btrfs_set_header_nritems(&dst->header, dst_nritems + push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 625 | |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 626 | BUG_ON(list_empty(&src_buf->dirty)); |
| 627 | BUG_ON(list_empty(&dst_buf->dirty)); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 628 | return ret; |
| 629 | } |
| 630 | |
| 631 | /* |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 632 | * helper function to insert a new root level in the tree. |
| 633 | * A new node is allocated, and a single item is inserted to |
| 634 | * point to the existing root |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 635 | * |
| 636 | * returns zero on success or < 0 on failure. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 637 | */ |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 638 | static int insert_new_root(struct ctree_root *root, |
| 639 | struct ctree_path *path, int level) |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 640 | { |
| 641 | struct tree_buffer *t; |
| 642 | struct node *lower; |
| 643 | struct node *c; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 644 | struct btrfs_disk_key *lower_key; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 645 | |
| 646 | BUG_ON(path->nodes[level]); |
| 647 | BUG_ON(path->nodes[level-1] != root->node); |
| 648 | |
| 649 | t = alloc_free_block(root); |
| 650 | c = &t->node; |
| 651 | memset(c, 0, sizeof(c)); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 652 | btrfs_set_header_nritems(&c->header, 1); |
| 653 | btrfs_set_header_level(&c->header, level); |
| 654 | btrfs_set_header_blocknr(&c->header, t->blocknr); |
| 655 | btrfs_set_header_parentid(&c->header, |
| 656 | btrfs_header_parentid(&root->node->node.header)); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 657 | lower = &path->nodes[level-1]->node; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 658 | if (btrfs_is_leaf(lower)) |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 659 | lower_key = &((struct leaf *)lower)->items[0].key; |
| 660 | else |
| 661 | lower_key = lower->keys; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 662 | memcpy(c->keys, lower_key, sizeof(struct btrfs_disk_key)); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 663 | c->blockptrs[0] = path->nodes[level-1]->blocknr; |
| 664 | /* the super has an extra ref to root->node */ |
| 665 | tree_block_release(root, root->node); |
| 666 | root->node = t; |
| 667 | t->count++; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 668 | path->nodes[level] = t; |
| 669 | path->slots[level] = 0; |
| 670 | return 0; |
| 671 | } |
| 672 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 673 | /* |
| 674 | * worker function to insert a single pointer in a node. |
| 675 | * the node should have enough room for the pointer already |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 676 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 677 | * slot and level indicate where you want the key to go, and |
| 678 | * blocknr is the block the key points to. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 679 | * |
| 680 | * returns zero on success and < 0 on any error |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 681 | */ |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 682 | static int insert_ptr(struct ctree_root *root, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 683 | struct ctree_path *path, struct btrfs_disk_key *key, |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 684 | u64 blocknr, int slot, int level) |
| 685 | { |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 686 | struct node *lower; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 687 | int nritems; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 688 | |
| 689 | BUG_ON(!path->nodes[level]); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 690 | lower = &path->nodes[level]->node; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 691 | nritems = btrfs_header_nritems(&lower->header); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 692 | if (slot > nritems) |
| 693 | BUG(); |
| 694 | if (nritems == NODEPTRS_PER_BLOCK) |
| 695 | BUG(); |
| 696 | if (slot != nritems) { |
| 697 | memmove(lower->keys + slot + 1, lower->keys + slot, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 698 | (nritems - slot) * sizeof(struct btrfs_disk_key)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 699 | memmove(lower->blockptrs + slot + 1, lower->blockptrs + slot, |
| 700 | (nritems - slot) * sizeof(u64)); |
| 701 | } |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 702 | memcpy(lower->keys + slot, key, sizeof(struct btrfs_disk_key)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 703 | lower->blockptrs[slot] = blocknr; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 704 | btrfs_set_header_nritems(&lower->header, nritems + 1); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 705 | if (lower->keys[1].objectid == 0) |
| 706 | BUG(); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 707 | BUG_ON(list_empty(&path->nodes[level]->dirty)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 708 | return 0; |
| 709 | } |
| 710 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 711 | /* |
| 712 | * split the node at the specified level in path in two. |
| 713 | * The path is corrected to point to the appropriate node after the split |
| 714 | * |
| 715 | * Before splitting this tries to make some room in the node by pushing |
| 716 | * left and right, if either one works, it returns right away. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 717 | * |
| 718 | * returns 0 on success and < 0 on failure |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 719 | */ |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 720 | static int split_node(struct ctree_root *root, struct ctree_path *path, |
| 721 | int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 722 | { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 723 | struct tree_buffer *t; |
| 724 | struct node *c; |
| 725 | struct tree_buffer *split_buffer; |
| 726 | struct node *split; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 727 | int mid; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 728 | int ret; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 729 | int wret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 730 | u32 c_nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 731 | |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 732 | t = path->nodes[level]; |
| 733 | c = &t->node; |
| 734 | if (t == root->node) { |
| 735 | /* trying to split the root, lets make a new one */ |
| 736 | ret = insert_new_root(root, path, level + 1); |
| 737 | if (ret) |
| 738 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 739 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 740 | c_nritems = btrfs_header_nritems(&c->header); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 741 | split_buffer = alloc_free_block(root); |
| 742 | split = &split_buffer->node; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 743 | btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header)); |
| 744 | btrfs_set_header_blocknr(&split->header, split_buffer->blocknr); |
| 745 | btrfs_set_header_parentid(&split->header, |
| 746 | btrfs_header_parentid(&root->node->node.header)); |
| 747 | mid = (c_nritems + 1) / 2; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 748 | memcpy(split->keys, c->keys + mid, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 749 | (c_nritems - mid) * sizeof(struct btrfs_disk_key)); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 750 | memcpy(split->blockptrs, c->blockptrs + mid, |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 751 | (c_nritems - mid) * sizeof(u64)); |
| 752 | btrfs_set_header_nritems(&split->header, c_nritems - mid); |
| 753 | btrfs_set_header_nritems(&c->header, mid); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 754 | ret = 0; |
| 755 | |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 756 | BUG_ON(list_empty(&t->dirty)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 757 | wret = insert_ptr(root, path, split->keys, split_buffer->blocknr, |
| 758 | path->slots[level + 1] + 1, level + 1); |
| 759 | if (wret) |
| 760 | ret = wret; |
| 761 | |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 762 | if (path->slots[level] >= mid) { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 763 | path->slots[level] -= mid; |
| 764 | tree_block_release(root, t); |
| 765 | path->nodes[level] = split_buffer; |
| 766 | path->slots[level + 1] += 1; |
| 767 | } else { |
| 768 | tree_block_release(root, split_buffer); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 769 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 770 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 771 | } |
| 772 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 773 | /* |
| 774 | * how many bytes are required to store the items in a leaf. start |
| 775 | * and nr indicate which items in the leaf to check. This totals up the |
| 776 | * space used both by the item structs and the item data |
| 777 | */ |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 778 | static int leaf_space_used(struct leaf *l, int start, int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 779 | { |
| 780 | int data_len; |
| 781 | int end = start + nr - 1; |
| 782 | |
| 783 | if (!nr) |
| 784 | return 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 785 | data_len = btrfs_item_end(l->items + start); |
| 786 | data_len = data_len - btrfs_item_offset(l->items + end); |
| 787 | data_len += sizeof(struct btrfs_item) * nr; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 788 | return data_len; |
| 789 | } |
| 790 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 791 | /* |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 792 | * push some data in the path leaf to the right, trying to free up at |
| 793 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 794 | * |
| 795 | * returns 1 if the push failed because the other node didn't have enough |
| 796 | * 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] | 797 | */ |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 798 | static int push_leaf_right(struct ctree_root *root, struct ctree_path *path, |
| 799 | int data_size) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 800 | { |
| 801 | struct tree_buffer *left_buf = path->nodes[0]; |
| 802 | struct leaf *left = &left_buf->leaf; |
| 803 | struct leaf *right; |
| 804 | struct tree_buffer *right_buf; |
| 805 | struct tree_buffer *upper; |
| 806 | int slot; |
| 807 | int i; |
| 808 | int free_space; |
| 809 | int push_space = 0; |
| 810 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 811 | struct btrfs_item *item; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 812 | u32 left_nritems; |
| 813 | u32 right_nritems; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 814 | |
| 815 | slot = path->slots[1]; |
| 816 | if (!path->nodes[1]) { |
| 817 | return 1; |
| 818 | } |
| 819 | upper = path->nodes[1]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 820 | if (slot >= btrfs_header_nritems(&upper->node.header) - 1) { |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 821 | return 1; |
| 822 | } |
| 823 | right_buf = read_tree_block(root, upper->node.blockptrs[slot + 1]); |
| 824 | right = &right_buf->leaf; |
| 825 | free_space = leaf_free_space(right); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 826 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 827 | tree_block_release(root, right_buf); |
| 828 | return 1; |
| 829 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 830 | /* cow and double check */ |
| 831 | btrfs_cow_block(root, right_buf, upper, slot + 1, &right_buf); |
| 832 | right = &right_buf->leaf; |
| 833 | free_space = leaf_free_space(right); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 834 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 835 | tree_block_release(root, right_buf); |
| 836 | return 1; |
| 837 | } |
| 838 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 839 | left_nritems = btrfs_header_nritems(&left->header); |
| 840 | for (i = left_nritems - 1; i >= 0; i--) { |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 841 | item = left->items + i; |
| 842 | if (path->slots[0] == i) |
| 843 | push_space += data_size + sizeof(*item); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 844 | if (btrfs_item_size(item) + sizeof(*item) + push_space > |
| 845 | free_space) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 846 | break; |
| 847 | push_items++; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 848 | push_space += btrfs_item_size(item) + sizeof(*item); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 849 | } |
| 850 | if (push_items == 0) { |
| 851 | tree_block_release(root, right_buf); |
| 852 | return 1; |
| 853 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 854 | right_nritems = btrfs_header_nritems(&right->header); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 855 | /* push left to right */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 856 | push_space = btrfs_item_end(left->items + left_nritems - push_items); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 857 | push_space -= leaf_data_end(left); |
| 858 | /* make room in the right data area */ |
| 859 | memmove(right->data + leaf_data_end(right) - push_space, |
| 860 | right->data + leaf_data_end(right), |
| 861 | LEAF_DATA_SIZE - leaf_data_end(right)); |
| 862 | /* copy from the left data area */ |
| 863 | memcpy(right->data + LEAF_DATA_SIZE - push_space, |
| 864 | left->data + leaf_data_end(left), |
| 865 | push_space); |
| 866 | memmove(right->items + push_items, right->items, |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 867 | right_nritems * sizeof(struct btrfs_item)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 868 | /* copy the items from left to right */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 869 | memcpy(right->items, left->items + left_nritems - push_items, |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 870 | push_items * sizeof(struct btrfs_item)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 871 | |
| 872 | /* update the item pointers */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 873 | right_nritems += push_items; |
| 874 | btrfs_set_header_nritems(&right->header, right_nritems); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 875 | push_space = LEAF_DATA_SIZE; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 876 | for (i = 0; i < right_nritems; i++) { |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 877 | btrfs_set_item_offset(right->items + i, push_space - |
| 878 | btrfs_item_size(right->items + i)); |
| 879 | push_space = btrfs_item_offset(right->items + i); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 880 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 881 | left_nritems -= push_items; |
| 882 | btrfs_set_header_nritems(&left->header, left_nritems); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 883 | |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 884 | BUG_ON(list_empty(&left_buf->dirty)); |
| 885 | BUG_ON(list_empty(&right_buf->dirty)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 886 | memcpy(upper->node.keys + slot + 1, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 887 | &right->items[0].key, sizeof(struct btrfs_disk_key)); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 888 | BUG_ON(list_empty(&upper->dirty)); |
| 889 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 890 | /* then fixup the leaf pointer in the path */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 891 | if (path->slots[0] >= left_nritems) { |
| 892 | path->slots[0] -= left_nritems; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 893 | tree_block_release(root, path->nodes[0]); |
| 894 | path->nodes[0] = right_buf; |
| 895 | path->slots[1] += 1; |
| 896 | } else { |
| 897 | tree_block_release(root, right_buf); |
| 898 | } |
| 899 | return 0; |
| 900 | } |
| 901 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 902 | * push some data in the path leaf to the left, trying to free up at |
| 903 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
| 904 | */ |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 905 | static int push_leaf_left(struct ctree_root *root, struct ctree_path *path, |
| 906 | int data_size) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 907 | { |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 908 | struct tree_buffer *right_buf = path->nodes[0]; |
| 909 | struct leaf *right = &right_buf->leaf; |
| 910 | struct tree_buffer *t; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 911 | struct leaf *left; |
| 912 | int slot; |
| 913 | int i; |
| 914 | int free_space; |
| 915 | int push_space = 0; |
| 916 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 917 | struct btrfs_item *item; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 918 | u32 old_left_nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 919 | int ret = 0; |
| 920 | int wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 921 | |
| 922 | slot = path->slots[1]; |
| 923 | if (slot == 0) { |
| 924 | return 1; |
| 925 | } |
| 926 | if (!path->nodes[1]) { |
| 927 | return 1; |
| 928 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 929 | t = read_tree_block(root, path->nodes[1]->node.blockptrs[slot - 1]); |
| 930 | left = &t->leaf; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 931 | free_space = leaf_free_space(left); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 932 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 933 | tree_block_release(root, t); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 934 | return 1; |
| 935 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 936 | |
| 937 | /* cow and double check */ |
| 938 | btrfs_cow_block(root, t, path->nodes[1], slot - 1, &t); |
| 939 | left = &t->leaf; |
| 940 | free_space = leaf_free_space(left); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 941 | if (free_space < data_size + sizeof(struct btrfs_item)) { |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 942 | tree_block_release(root, t); |
| 943 | return 1; |
| 944 | } |
| 945 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 946 | for (i = 0; i < btrfs_header_nritems(&right->header); i++) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 947 | item = right->items + i; |
| 948 | if (path->slots[0] == i) |
| 949 | push_space += data_size + sizeof(*item); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 950 | if (btrfs_item_size(item) + sizeof(*item) + push_space > |
| 951 | free_space) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 952 | break; |
| 953 | push_items++; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 954 | push_space += btrfs_item_size(item) + sizeof(*item); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 955 | } |
| 956 | if (push_items == 0) { |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 957 | tree_block_release(root, t); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 958 | return 1; |
| 959 | } |
| 960 | /* push data from right to left */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 961 | memcpy(left->items + btrfs_header_nritems(&left->header), |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 962 | right->items, push_items * sizeof(struct btrfs_item)); |
| 963 | push_space = LEAF_DATA_SIZE - |
| 964 | btrfs_item_offset(right->items + push_items -1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 965 | memcpy(left->data + leaf_data_end(left) - push_space, |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 966 | right->data + btrfs_item_offset(right->items + push_items - 1), |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 967 | push_space); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 968 | old_left_nritems = btrfs_header_nritems(&left->header); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 969 | BUG_ON(old_left_nritems < 0); |
| 970 | |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 971 | for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { |
| 972 | u16 ioff = btrfs_item_offset(left->items + i); |
| 973 | btrfs_set_item_offset(left->items + i, ioff - (LEAF_DATA_SIZE - |
| 974 | btrfs_item_offset(left->items + |
| 975 | old_left_nritems - 1))); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 976 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 977 | btrfs_set_header_nritems(&left->header, old_left_nritems + push_items); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 978 | |
| 979 | /* fixup right node */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 980 | push_space = btrfs_item_offset(right->items + push_items - 1) - |
| 981 | leaf_data_end(right); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 982 | memmove(right->data + LEAF_DATA_SIZE - push_space, right->data + |
| 983 | leaf_data_end(right), push_space); |
| 984 | memmove(right->items, right->items + push_items, |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 985 | (btrfs_header_nritems(&right->header) - push_items) * |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 986 | sizeof(struct btrfs_item)); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 987 | btrfs_set_header_nritems(&right->header, |
| 988 | btrfs_header_nritems(&right->header) - |
| 989 | push_items); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 990 | push_space = LEAF_DATA_SIZE; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 991 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 992 | for (i = 0; i < btrfs_header_nritems(&right->header); i++) { |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 993 | btrfs_set_item_offset(right->items + i, push_space - |
| 994 | btrfs_item_size(right->items + i)); |
| 995 | push_space = btrfs_item_offset(right->items + i); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 996 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 997 | |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 998 | BUG_ON(list_empty(&t->dirty)); |
| 999 | BUG_ON(list_empty(&right_buf->dirty)); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1000 | |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1001 | wret = fixup_low_keys(root, path, &right->items[0].key, 1); |
| 1002 | if (wret) |
| 1003 | ret = wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1004 | |
| 1005 | /* then fixup the leaf pointer in the path */ |
| 1006 | if (path->slots[0] < push_items) { |
| 1007 | path->slots[0] += old_left_nritems; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1008 | tree_block_release(root, path->nodes[0]); |
| 1009 | path->nodes[0] = t; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1010 | path->slots[1] -= 1; |
| 1011 | } else { |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1012 | tree_block_release(root, t); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1013 | path->slots[0] -= push_items; |
| 1014 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1015 | BUG_ON(path->slots[0] < 0); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1016 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1017 | } |
| 1018 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1019 | /* |
| 1020 | * split the path's leaf in two, making sure there is at least data_size |
| 1021 | * available for the resulting leaf level of the path. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1022 | * |
| 1023 | * returns 0 if all went well and < 0 on failure. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1024 | */ |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1025 | static int split_leaf(struct ctree_root *root, struct ctree_path *path, |
| 1026 | int data_size) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1027 | { |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1028 | struct tree_buffer *l_buf; |
| 1029 | struct leaf *l; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1030 | u32 nritems; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1031 | int mid; |
| 1032 | int slot; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1033 | struct leaf *right; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1034 | struct tree_buffer *right_buffer; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 1035 | int space_needed = data_size + sizeof(struct btrfs_item); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1036 | int data_copy_size; |
| 1037 | int rt_data_off; |
| 1038 | int i; |
| 1039 | int ret; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1040 | int wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1041 | |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1042 | l_buf = path->nodes[0]; |
| 1043 | l = &l_buf->leaf; |
| 1044 | |
| 1045 | /* did the pushes work? */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 1046 | if (leaf_free_space(l) >= sizeof(struct btrfs_item) + data_size) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1047 | return 0; |
| 1048 | |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1049 | if (!path->nodes[1]) { |
| 1050 | ret = insert_new_root(root, path, 1); |
| 1051 | if (ret) |
| 1052 | return ret; |
| 1053 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1054 | slot = path->slots[0]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1055 | nritems = btrfs_header_nritems(&l->header); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1056 | mid = (nritems + 1)/ 2; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1057 | right_buffer = alloc_free_block(root); |
| 1058 | BUG_ON(!right_buffer); |
| 1059 | BUG_ON(mid == nritems); |
| 1060 | right = &right_buffer->leaf; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1061 | memset(right, 0, sizeof(*right)); |
| 1062 | if (mid <= slot) { |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1063 | /* FIXME, just alloc a new leaf here */ |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1064 | if (leaf_space_used(l, mid, nritems - mid) + space_needed > |
| 1065 | LEAF_DATA_SIZE) |
| 1066 | BUG(); |
| 1067 | } else { |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1068 | /* FIXME, just alloc a new leaf here */ |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1069 | if (leaf_space_used(l, 0, mid + 1) + space_needed > |
| 1070 | LEAF_DATA_SIZE) |
| 1071 | BUG(); |
| 1072 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1073 | btrfs_set_header_nritems(&right->header, nritems - mid); |
| 1074 | btrfs_set_header_blocknr(&right->header, right_buffer->blocknr); |
| 1075 | btrfs_set_header_level(&right->header, 0); |
| 1076 | btrfs_set_header_parentid(&right->header, |
| 1077 | btrfs_header_parentid(&root->node->node.header)); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 1078 | data_copy_size = btrfs_item_end(l->items + mid) - leaf_data_end(l); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1079 | memcpy(right->items, l->items + mid, |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 1080 | (nritems - mid) * sizeof(struct btrfs_item)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1081 | memcpy(right->data + LEAF_DATA_SIZE - data_copy_size, |
| 1082 | l->data + leaf_data_end(l), data_copy_size); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 1083 | rt_data_off = LEAF_DATA_SIZE - btrfs_item_end(l->items + mid); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1084 | |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 1085 | for (i = 0; i < btrfs_header_nritems(&right->header); i++) { |
| 1086 | u16 ioff = btrfs_item_offset(right->items + i); |
| 1087 | btrfs_set_item_offset(right->items + i, ioff + rt_data_off); |
| 1088 | } |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1089 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1090 | btrfs_set_header_nritems(&l->header, mid); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1091 | ret = 0; |
| 1092 | wret = insert_ptr(root, path, &right->items[0].key, |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1093 | right_buffer->blocknr, path->slots[1] + 1, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1094 | if (wret) |
| 1095 | ret = wret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1096 | BUG_ON(list_empty(&right_buffer->dirty)); |
| 1097 | BUG_ON(list_empty(&l_buf->dirty)); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1098 | BUG_ON(path->slots[0] != slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1099 | if (mid <= slot) { |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1100 | tree_block_release(root, path->nodes[0]); |
| 1101 | path->nodes[0] = right_buffer; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1102 | path->slots[0] -= mid; |
| 1103 | path->slots[1] += 1; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1104 | } else |
| 1105 | tree_block_release(root, right_buffer); |
| 1106 | BUG_ON(path->slots[0] < 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1107 | return ret; |
| 1108 | } |
| 1109 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1110 | /* |
| 1111 | * Given a key and some data, insert an item into the tree. |
| 1112 | * This does all the path init required, making room in the tree if needed. |
| 1113 | */ |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1114 | int insert_item(struct ctree_root *root, struct btrfs_key *cpu_key, |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1115 | void *data, int data_size) |
| 1116 | { |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1117 | int ret = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1118 | int slot; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1119 | int slot_orig; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1120 | struct leaf *leaf; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1121 | struct tree_buffer *leaf_buf; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1122 | u32 nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1123 | unsigned int data_end; |
| 1124 | struct ctree_path path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1125 | struct btrfs_disk_key disk_key; |
| 1126 | |
| 1127 | btrfs_cpu_key_to_disk(&disk_key, cpu_key); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1128 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1129 | /* create a root if there isn't one */ |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 1130 | if (!root->node) |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 1131 | BUG(); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1132 | init_path(&path); |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1133 | ret = search_slot(root, cpu_key, &path, data_size, 1); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1134 | if (ret == 0) { |
| 1135 | release_path(root, &path); |
Chris Mason | f0930a3 | 2007-03-02 09:47:58 -0500 | [diff] [blame] | 1136 | return -EEXIST; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1137 | } |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 1138 | if (ret < 0) |
| 1139 | goto out; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1140 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1141 | slot_orig = path.slots[0]; |
| 1142 | leaf_buf = path.nodes[0]; |
| 1143 | leaf = &leaf_buf->leaf; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1144 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1145 | nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1146 | data_end = leaf_data_end(leaf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1147 | |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 1148 | if (leaf_free_space(leaf) < sizeof(struct btrfs_item) + data_size) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1149 | BUG(); |
| 1150 | |
| 1151 | slot = path.slots[0]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1152 | BUG_ON(slot < 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1153 | if (slot != nritems) { |
| 1154 | int i; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 1155 | unsigned int old_data = btrfs_item_end(leaf->items + slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1156 | |
| 1157 | /* |
| 1158 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 1159 | */ |
| 1160 | /* first correct the data pointers */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 1161 | for (i = slot; i < nritems; i++) { |
| 1162 | u16 ioff = btrfs_item_offset(leaf->items + i); |
| 1163 | btrfs_set_item_offset(leaf->items + i, |
| 1164 | ioff - data_size); |
| 1165 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1166 | |
| 1167 | /* shift the items */ |
| 1168 | memmove(leaf->items + slot + 1, leaf->items + slot, |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 1169 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1170 | |
| 1171 | /* shift the data */ |
| 1172 | memmove(leaf->data + data_end - data_size, leaf->data + |
| 1173 | data_end, old_data - data_end); |
| 1174 | data_end = old_data; |
| 1175 | } |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1176 | /* copy the new data in */ |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1177 | memcpy(&leaf->items[slot].key, &disk_key, |
| 1178 | sizeof(struct btrfs_disk_key)); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 1179 | btrfs_set_item_offset(leaf->items + slot, data_end - data_size); |
| 1180 | btrfs_set_item_size(leaf->items + slot, data_size); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1181 | memcpy(leaf->data + data_end - data_size, data, data_size); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1182 | btrfs_set_header_nritems(&leaf->header, nritems + 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1183 | |
| 1184 | ret = 0; |
Chris Mason | 8e19f2c | 2007-02-28 09:27:02 -0500 | [diff] [blame] | 1185 | if (slot == 0) |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1186 | ret = fixup_low_keys(root, &path, &disk_key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1187 | |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1188 | BUG_ON(list_empty(&leaf_buf->dirty)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1189 | if (leaf_free_space(leaf) < 0) |
| 1190 | BUG(); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1191 | check_leaf(&path, 0); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 1192 | out: |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1193 | release_path(root, &path); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1194 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1195 | } |
| 1196 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1197 | /* |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 1198 | * delete the pointer from a given node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1199 | * |
| 1200 | * If the delete empties a node, the node is removed from the tree, |
| 1201 | * continuing all the way the root if required. The root is converted into |
| 1202 | * a leaf if all the nodes are emptied. |
| 1203 | */ |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1204 | static int del_ptr(struct ctree_root *root, struct ctree_path *path, int level, |
| 1205 | int slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1206 | { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1207 | struct node *node; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1208 | struct tree_buffer *parent = path->nodes[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1209 | u32 nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1210 | int ret = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1211 | int wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1212 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1213 | node = &parent->node; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1214 | nritems = btrfs_header_nritems(&node->header); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1215 | if (slot != nritems -1) { |
| 1216 | memmove(node->keys + slot, node->keys + slot + 1, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1217 | sizeof(struct btrfs_disk_key) * (nritems - slot - 1)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1218 | memmove(node->blockptrs + slot, |
| 1219 | node->blockptrs + slot + 1, |
| 1220 | sizeof(u64) * (nritems - slot - 1)); |
| 1221 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1222 | nritems--; |
| 1223 | btrfs_set_header_nritems(&node->header, nritems); |
| 1224 | if (nritems == 0 && parent == root->node) { |
| 1225 | BUG_ON(btrfs_header_level(&root->node->node.header) != 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1226 | /* just turn the root into a leaf and break */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1227 | btrfs_set_header_level(&root->node->node.header, 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1228 | } else if (slot == 0) { |
| 1229 | wret = fixup_low_keys(root, path, node->keys, level + 1); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1230 | if (wret) |
| 1231 | ret = wret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1232 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1233 | BUG_ON(list_empty(&parent->dirty)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1234 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1235 | } |
| 1236 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1237 | /* |
| 1238 | * delete the item at the leaf level in path. If that empties |
| 1239 | * the leaf, remove it from the tree |
| 1240 | */ |
Chris Mason | 4920c9a | 2007-01-26 16:38:42 -0500 | [diff] [blame] | 1241 | int del_item(struct ctree_root *root, struct ctree_path *path) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1242 | { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1243 | int slot; |
| 1244 | struct leaf *leaf; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1245 | struct tree_buffer *leaf_buf; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1246 | int doff; |
| 1247 | int dsize; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1248 | int ret = 0; |
| 1249 | int wret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1250 | u32 nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1251 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1252 | leaf_buf = path->nodes[0]; |
| 1253 | leaf = &leaf_buf->leaf; |
Chris Mason | 4920c9a | 2007-01-26 16:38:42 -0500 | [diff] [blame] | 1254 | slot = path->slots[0]; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 1255 | doff = btrfs_item_offset(leaf->items + slot); |
| 1256 | dsize = btrfs_item_size(leaf->items + slot); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1257 | nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1258 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1259 | if (slot != nritems - 1) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1260 | int i; |
| 1261 | int data_end = leaf_data_end(leaf); |
| 1262 | memmove(leaf->data + data_end + dsize, |
| 1263 | leaf->data + data_end, |
| 1264 | doff - data_end); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 1265 | for (i = slot + 1; i < nritems; i++) { |
| 1266 | u16 ioff = btrfs_item_offset(leaf->items + i); |
| 1267 | btrfs_set_item_offset(leaf->items + i, ioff + dsize); |
| 1268 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1269 | memmove(leaf->items + slot, leaf->items + slot + 1, |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame^] | 1270 | sizeof(struct btrfs_item) * |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1271 | (nritems - slot - 1)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1272 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1273 | btrfs_set_header_nritems(&leaf->header, nritems - 1); |
| 1274 | nritems--; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1275 | /* delete the leaf if we've emptied it */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1276 | if (nritems == 0) { |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1277 | if (leaf_buf == root->node) { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1278 | btrfs_set_header_level(&leaf->header, 0); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1279 | BUG_ON(list_empty(&leaf_buf->dirty)); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 1280 | } else { |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 1281 | clean_tree_block(root, leaf_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1282 | wret = del_ptr(root, path, 1, path->slots[1]); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1283 | if (wret) |
| 1284 | ret = wret; |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1285 | wret = free_extent(root, leaf_buf->blocknr, 1); |
| 1286 | if (wret) |
| 1287 | ret = wret; |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 1288 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1289 | } else { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1290 | int used = leaf_space_used(leaf, 0, nritems); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1291 | if (slot == 0) { |
| 1292 | wret = fixup_low_keys(root, path, |
| 1293 | &leaf->items[0].key, 1); |
| 1294 | if (wret) |
| 1295 | ret = wret; |
| 1296 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1297 | BUG_ON(list_empty(&leaf_buf->dirty)); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1298 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1299 | /* delete the leaf if it is mostly empty */ |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 1300 | if (used < LEAF_DATA_SIZE / 3) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1301 | /* push_leaf_left fixes the path. |
| 1302 | * make sure the path still points to our leaf |
| 1303 | * for possible call to del_ptr below |
| 1304 | */ |
Chris Mason | 4920c9a | 2007-01-26 16:38:42 -0500 | [diff] [blame] | 1305 | slot = path->slots[1]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1306 | leaf_buf->count++; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1307 | wret = push_leaf_left(root, path, 1); |
| 1308 | if (wret < 0) |
| 1309 | ret = wret; |
Chris Mason | f0930a3 | 2007-03-02 09:47:58 -0500 | [diff] [blame] | 1310 | if (path->nodes[0] == leaf_buf && |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1311 | btrfs_header_nritems(&leaf->header)) { |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1312 | wret = push_leaf_right(root, path, 1); |
| 1313 | if (wret < 0) |
| 1314 | ret = wret; |
| 1315 | } |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1316 | if (btrfs_header_nritems(&leaf->header) == 0) { |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 1317 | u64 blocknr = leaf_buf->blocknr; |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 1318 | clean_tree_block(root, leaf_buf); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1319 | wret = del_ptr(root, path, 1, slot); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1320 | if (wret) |
| 1321 | ret = wret; |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 1322 | tree_block_release(root, leaf_buf); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1323 | wret = free_extent(root, blocknr, 1); |
| 1324 | if (wret) |
| 1325 | ret = wret; |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 1326 | } else { |
| 1327 | tree_block_release(root, leaf_buf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1328 | } |
| 1329 | } |
| 1330 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1331 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1332 | } |
| 1333 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1334 | /* |
| 1335 | * 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] | 1336 | * returns 0 if it found something or 1 if there are no greater leaves. |
| 1337 | * returns < 0 on io errors. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1338 | */ |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1339 | int next_leaf(struct ctree_root *root, struct ctree_path *path) |
| 1340 | { |
| 1341 | int slot; |
| 1342 | int level = 1; |
| 1343 | u64 blocknr; |
| 1344 | struct tree_buffer *c; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 1345 | struct tree_buffer *next = NULL; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1346 | |
| 1347 | while(level < MAX_LEVEL) { |
| 1348 | if (!path->nodes[level]) |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1349 | return 1; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1350 | slot = path->slots[level] + 1; |
| 1351 | c = path->nodes[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1352 | if (slot >= btrfs_header_nritems(&c->node.header)) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1353 | level++; |
| 1354 | continue; |
| 1355 | } |
| 1356 | blocknr = c->node.blockptrs[slot]; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 1357 | if (next) |
| 1358 | tree_block_release(root, next); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 1359 | next = read_tree_block(root, blocknr); |
| 1360 | break; |
| 1361 | } |
| 1362 | path->slots[level] = slot; |
| 1363 | while(1) { |
| 1364 | level--; |
| 1365 | c = path->nodes[level]; |
| 1366 | tree_block_release(root, c); |
| 1367 | path->nodes[level] = next; |
| 1368 | path->slots[level] = 0; |
| 1369 | if (!level) |
| 1370 | break; |
| 1371 | next = read_tree_block(root, next->node.blockptrs[0]); |
| 1372 | } |
| 1373 | return 0; |
| 1374 | } |
| 1375 | |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1376 | |