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