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