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