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