3 #include "kerncompat.h"
4 #include "radix-tree.h"
7 #include "print-tree.h"
9 static int split_node(struct ctree_root *root, struct ctree_path *path,
11 static int split_leaf(struct ctree_root *root, struct ctree_path *path,
13 static int push_node_left(struct ctree_root *root, struct tree_buffer *dst,
14 struct tree_buffer *src);
15 static int balance_node_right(struct ctree_root *root,
16 struct tree_buffer *dst_buf,
17 struct tree_buffer *src_buf);
18 static int del_ptr(struct ctree_root *root, struct ctree_path *path, int level,
21 inline void init_path(struct ctree_path *p)
23 memset(p, 0, sizeof(*p));
26 void release_path(struct ctree_root *root, struct ctree_path *p)
29 for (i = 0; i < MAX_LEVEL; i++) {
32 tree_block_release(root, p->nodes[i]);
34 memset(p, 0, sizeof(*p));
38 * The leaf data grows from end-to-front in the node.
39 * this returns the address of the start of the last item,
40 * which is the stop of the leaf data stack
42 static inline unsigned int leaf_data_end(struct leaf *leaf)
44 unsigned int nr = leaf->header.nritems;
46 return sizeof(leaf->data);
47 return leaf->items[nr-1].offset;
51 * The space between the end of the leaf items and
52 * the start of the leaf data. IOW, how much room
53 * the leaf has left for both items and data
55 int leaf_free_space(struct leaf *leaf)
57 int data_end = leaf_data_end(leaf);
58 int nritems = leaf->header.nritems;
59 char *items_end = (char *)(leaf->items + nritems + 1);
60 return (char *)(leaf->data + data_end) - (char *)items_end;
64 * compare two keys in a memcmp fashion
66 int comp_keys(struct key *k1, struct key *k2)
68 if (k1->objectid > k2->objectid)
70 if (k1->objectid < k2->objectid)
72 if (k1->flags > k2->flags)
74 if (k1->flags < k2->flags)
76 if (k1->offset > k2->offset)
78 if (k1->offset < k2->offset)
83 int check_node(struct ctree_path *path, int level)
86 struct node *parent = NULL;
87 struct node *node = &path->nodes[level]->node;
90 if (path->nodes[level + 1])
91 parent = &path->nodes[level + 1]->node;
92 parent_slot = path->slots[level + 1];
93 if (parent && node->header.nritems > 0) {
94 struct key *parent_key;
95 parent_key = &parent->keys[parent_slot];
96 BUG_ON(memcmp(parent_key, node->keys, sizeof(struct key)));
97 BUG_ON(parent->blockptrs[parent_slot] != node->header.blocknr);
99 BUG_ON(node->header.nritems > NODEPTRS_PER_BLOCK);
100 for (i = 0; i < node->header.nritems - 2; i++) {
101 BUG_ON(comp_keys(&node->keys[i], &node->keys[i+1]) >= 0);
106 int check_leaf(struct ctree_path *path, int level)
109 struct leaf *leaf = &path->nodes[level]->leaf;
110 struct node *parent = NULL;
113 if (path->nodes[level + 1])
114 parent = &path->nodes[level + 1]->node;
115 parent_slot = path->slots[level + 1];
116 if (parent && leaf->header.nritems > 0) {
117 struct key *parent_key;
118 parent_key = &parent->keys[parent_slot];
119 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
120 sizeof(struct key)));
121 BUG_ON(parent->blockptrs[parent_slot] != leaf->header.blocknr);
123 for (i = 0; i < leaf->header.nritems - 2; i++) {
124 BUG_ON(comp_keys(&leaf->items[i].key,
125 &leaf->items[i+1].key) >= 0);
126 BUG_ON(leaf->items[i].offset != leaf->items[i + 1].offset +
127 leaf->items[i + 1].size);
129 BUG_ON(leaf->items[i].offset + leaf->items[i].size !=
133 BUG_ON(leaf_free_space(leaf) < 0);
137 int check_block(struct ctree_path *path, int level)
140 return check_leaf(path, level);
141 return check_node(path, level);
145 * search for key in the array p. items p are item_size apart
146 * and there are 'max' items in p
147 * the slot in the array is returned via slot, and it points to
148 * the place where you would insert key if it is not found in
151 * slot may point to max if the key is bigger than all of the keys
153 int generic_bin_search(char *p, int item_size, struct key *key,
163 mid = (low + high) / 2;
164 tmp = (struct key *)(p + mid * item_size);
165 ret = comp_keys(tmp, key);
181 * simple bin_search frontend that does the right thing for
184 int bin_search(struct node *c, struct key *key, int *slot)
186 if (is_leaf(c->header.flags)) {
187 struct leaf *l = (struct leaf *)c;
188 return generic_bin_search((void *)l->items, sizeof(struct item),
189 key, c->header.nritems, slot);
191 return generic_bin_search((void *)c->keys, sizeof(struct key),
192 key, c->header.nritems, slot);
197 struct tree_buffer *read_node_slot(struct ctree_root *root,
198 struct tree_buffer *parent_buf,
201 struct node *node = &parent_buf->node;
204 if (slot >= node->header.nritems)
206 return read_tree_block(root, node->blockptrs[slot]);
209 static int balance_level(struct ctree_root *root, struct ctree_path *path,
212 struct tree_buffer *right_buf;
213 struct tree_buffer *mid_buf;
214 struct tree_buffer *left_buf;
215 struct tree_buffer *parent_buf = NULL;
216 struct node *right = NULL;
218 struct node *left = NULL;
219 struct node *parent = NULL;
223 int orig_slot = path->slots[level];
229 mid_buf = path->nodes[level];
230 mid = &mid_buf->node;
231 orig_ptr = mid->blockptrs[orig_slot];
233 if (level < MAX_LEVEL - 1)
234 parent_buf = path->nodes[level + 1];
235 pslot = path->slots[level + 1];
238 struct tree_buffer *child;
239 u64 blocknr = mid_buf->blocknr;
241 if (mid->header.nritems != 1)
244 /* promote the child to a root */
245 child = read_node_slot(root, mid_buf, 0);
248 path->nodes[level] = NULL;
249 /* once for the path */
250 tree_block_release(root, mid_buf);
251 /* once for the root ptr */
252 tree_block_release(root, mid_buf);
253 clean_tree_block(root, mid_buf);
254 return free_extent(root, blocknr, 1);
256 parent = &parent_buf->node;
258 if (mid->header.nritems > NODEPTRS_PER_BLOCK / 4)
261 left_buf = read_node_slot(root, parent_buf, pslot - 1);
262 right_buf = read_node_slot(root, parent_buf, pslot + 1);
264 /* first, try to make some room in the middle buffer */
266 left = &left_buf->node;
267 orig_slot += left->header.nritems;
268 wret = push_node_left(root, left_buf, mid_buf);
274 * then try to empty the right most buffer into the middle
277 right = &right_buf->node;
278 wret = push_node_left(root, mid_buf, right_buf);
281 if (right->header.nritems == 0) {
282 u64 blocknr = right_buf->blocknr;
283 tree_block_release(root, right_buf);
284 clean_tree_block(root, right_buf);
287 wret = del_ptr(root, path, level + 1, pslot + 1);
290 wret = free_extent(root, blocknr, 1);
294 memcpy(parent->keys + pslot + 1, right->keys,
296 wret = dirty_tree_block(root, parent_buf);
301 if (mid->header.nritems == 1) {
303 * we're not allowed to leave a node with one item in the
304 * tree during a delete. A deletion from lower in the tree
305 * could try to delete the only pointer in this node.
306 * So, pull some keys from the left.
307 * There has to be a left pointer at this point because
308 * otherwise we would have pulled some pointers from the
312 wret = balance_node_right(root, mid_buf, left_buf);
317 if (mid->header.nritems == 0) {
318 /* we've managed to empty the middle node, drop it */
319 u64 blocknr = mid_buf->blocknr;
320 tree_block_release(root, mid_buf);
321 clean_tree_block(root, mid_buf);
324 wret = del_ptr(root, path, level + 1, pslot);
327 wret = free_extent(root, blocknr, 1);
331 /* update the parent key to reflect our changes */
332 memcpy(parent->keys + pslot, mid->keys, sizeof(struct key));
333 wret = dirty_tree_block(root, parent_buf);
338 /* update the path */
340 if (left->header.nritems > orig_slot) {
341 left_buf->count++; // released below
342 path->nodes[level] = left_buf;
343 path->slots[level + 1] -= 1;
344 path->slots[level] = orig_slot;
346 tree_block_release(root, mid_buf);
348 orig_slot -= left->header.nritems;
349 path->slots[level] = orig_slot;
352 /* double check we haven't messed things up */
353 check_block(path, level);
354 if (orig_ptr != path->nodes[level]->node.blockptrs[path->slots[level]])
358 tree_block_release(root, right_buf);
360 tree_block_release(root, left_buf);
365 * look for key in the tree. path is filled in with nodes along the way
366 * if key is found, we return zero and you can find the item in the leaf
367 * level of the path (level 0)
369 * If the key isn't found, the path points to the slot where it should
370 * be inserted, and 1 is returned. If there are other errors during the
371 * search a negative error number is returned.
373 * if ins_len > 0, nodes and leaves will be split as we walk down the
374 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
377 int search_slot(struct ctree_root *root, struct key *key,
378 struct ctree_path *p, int ins_len)
380 struct tree_buffer *b;
391 level = node_level(c->header.flags);
393 ret = check_block(p, level);
396 ret = bin_search(c, key, &slot);
397 if (!is_leaf(c->header.flags)) {
400 p->slots[level] = slot;
402 c->header.nritems == NODEPTRS_PER_BLOCK) {
403 int sret = split_node(root, p, level);
409 slot = p->slots[level];
410 } else if (ins_len < 0) {
411 int sret = balance_level(root, p, level);
418 slot = p->slots[level];
419 BUG_ON(c->header.nritems == 1);
421 b = read_tree_block(root, c->blockptrs[slot]);
423 struct leaf *l = (struct leaf *)c;
424 p->slots[level] = slot;
425 if (ins_len > 0 && leaf_free_space(l) <
426 sizeof(struct item) + ins_len) {
427 int sret = split_leaf(root, p, ins_len);
432 BUG_ON(root->node->count == 1);
436 BUG_ON(root->node->count == 1);
441 * adjust the pointers going up the tree, starting at level
442 * making sure the right key of each node is points to 'key'.
443 * This is used after shifting pointers to the left, so it stops
444 * fixing up pointers when a given leaf/node is not in slot 0 of the
447 * If this fails to write a tree block, it returns -1, but continues
448 * fixing up the blocks in ram so the tree is consistent.
450 static int fixup_low_keys(struct ctree_root *root,
451 struct ctree_path *path, struct key *key,
457 for (i = level; i < MAX_LEVEL; i++) {
459 int tslot = path->slots[i];
462 t = &path->nodes[i]->node;
463 memcpy(t->keys + tslot, key, sizeof(*key));
464 wret = dirty_tree_block(root, path->nodes[i]);
474 * try to push data from one node into the next node left in the
477 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
478 * error, and > 0 if there was no room in the left hand block.
480 static int push_node_left(struct ctree_root *root, struct tree_buffer *dst_buf,
481 struct tree_buffer *src_buf)
483 struct node *src = &src_buf->node;
484 struct node *dst = &dst_buf->node;
491 src_nritems = src->header.nritems;
492 dst_nritems = dst->header.nritems;
493 push_items = NODEPTRS_PER_BLOCK - dst_nritems;
494 if (push_items <= 0) {
498 if (src_nritems < push_items)
499 push_items = src_nritems;
501 memcpy(dst->keys + dst_nritems, src->keys,
502 push_items * sizeof(struct key));
503 memcpy(dst->blockptrs + dst_nritems, src->blockptrs,
504 push_items * sizeof(u64));
505 if (push_items < src_nritems) {
506 memmove(src->keys, src->keys + push_items,
507 (src_nritems - push_items) * sizeof(struct key));
508 memmove(src->blockptrs, src->blockptrs + push_items,
509 (src_nritems - push_items) * sizeof(u64));
511 src->header.nritems -= push_items;
512 dst->header.nritems += push_items;
514 wret = dirty_tree_block(root, src_buf);
518 wret = dirty_tree_block(root, dst_buf);
525 * try to push data from one node into the next node right in the
528 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
529 * error, and > 0 if there was no room in the right hand block.
531 * this will only push up to 1/2 the contents of the left node over
533 static int balance_node_right(struct ctree_root *root,
534 struct tree_buffer *dst_buf,
535 struct tree_buffer *src_buf)
537 struct node *src = &src_buf->node;
538 struct node *dst = &dst_buf->node;
546 src_nritems = src->header.nritems;
547 dst_nritems = dst->header.nritems;
548 push_items = NODEPTRS_PER_BLOCK - dst_nritems;
549 if (push_items <= 0) {
553 max_push = src_nritems / 2 + 1;
554 /* don't try to empty the node */
555 if (max_push > src_nritems)
557 if (max_push < push_items)
558 push_items = max_push;
560 memmove(dst->keys + push_items, dst->keys,
561 dst_nritems * sizeof(struct key));
562 memmove(dst->blockptrs + push_items, dst->blockptrs,
563 dst_nritems * sizeof(u64));
564 memcpy(dst->keys, src->keys + src_nritems - push_items,
565 push_items * sizeof(struct key));
566 memcpy(dst->blockptrs, src->blockptrs + src_nritems - push_items,
567 push_items * sizeof(u64));
569 src->header.nritems -= push_items;
570 dst->header.nritems += push_items;
572 wret = dirty_tree_block(root, src_buf);
576 wret = dirty_tree_block(root, dst_buf);
583 * helper function to insert a new root level in the tree.
584 * A new node is allocated, and a single item is inserted to
585 * point to the existing root
587 * returns zero on success or < 0 on failure.
589 static int insert_new_root(struct ctree_root *root,
590 struct ctree_path *path, int level)
592 struct tree_buffer *t;
595 struct key *lower_key;
597 BUG_ON(path->nodes[level]);
598 BUG_ON(path->nodes[level-1] != root->node);
600 t = alloc_free_block(root);
602 memset(c, 0, sizeof(c));
603 c->header.nritems = 1;
604 c->header.flags = node_level(level);
605 c->header.blocknr = t->blocknr;
606 c->header.parentid = root->node->node.header.parentid;
607 lower = &path->nodes[level-1]->node;
608 if (is_leaf(lower->header.flags))
609 lower_key = &((struct leaf *)lower)->items[0].key;
611 lower_key = lower->keys;
612 memcpy(c->keys, lower_key, sizeof(struct key));
613 c->blockptrs[0] = path->nodes[level-1]->blocknr;
614 /* the super has an extra ref to root->node */
615 tree_block_release(root, root->node);
618 dirty_tree_block(root, t);
619 path->nodes[level] = t;
620 path->slots[level] = 0;
625 * worker function to insert a single pointer in a node.
626 * the node should have enough room for the pointer already
628 * slot and level indicate where you want the key to go, and
629 * blocknr is the block the key points to.
631 * returns zero on success and < 0 on any error
633 static int insert_ptr(struct ctree_root *root,
634 struct ctree_path *path, struct key *key,
635 u64 blocknr, int slot, int level)
640 BUG_ON(!path->nodes[level]);
641 lower = &path->nodes[level]->node;
642 nritems = lower->header.nritems;
645 if (nritems == NODEPTRS_PER_BLOCK)
647 if (slot != nritems) {
648 memmove(lower->keys + slot + 1, lower->keys + slot,
649 (nritems - slot) * sizeof(struct key));
650 memmove(lower->blockptrs + slot + 1, lower->blockptrs + slot,
651 (nritems - slot) * sizeof(u64));
653 memcpy(lower->keys + slot, key, sizeof(struct key));
654 lower->blockptrs[slot] = blocknr;
655 lower->header.nritems++;
656 if (lower->keys[1].objectid == 0)
658 dirty_tree_block(root, path->nodes[level]);
663 * split the node at the specified level in path in two.
664 * The path is corrected to point to the appropriate node after the split
666 * Before splitting this tries to make some room in the node by pushing
667 * left and right, if either one works, it returns right away.
669 * returns 0 on success and < 0 on failure
671 static int split_node(struct ctree_root *root, struct ctree_path *path,
674 struct tree_buffer *t;
676 struct tree_buffer *split_buffer;
682 t = path->nodes[level];
684 if (t == root->node) {
685 /* trying to split the root, lets make a new one */
686 ret = insert_new_root(root, path, level + 1);
690 split_buffer = alloc_free_block(root);
691 split = &split_buffer->node;
692 split->header.flags = c->header.flags;
693 split->header.blocknr = split_buffer->blocknr;
694 split->header.parentid = root->node->node.header.parentid;
695 mid = (c->header.nritems + 1) / 2;
696 memcpy(split->keys, c->keys + mid,
697 (c->header.nritems - mid) * sizeof(struct key));
698 memcpy(split->blockptrs, c->blockptrs + mid,
699 (c->header.nritems - mid) * sizeof(u64));
700 split->header.nritems = c->header.nritems - mid;
701 c->header.nritems = mid;
704 wret = dirty_tree_block(root, t);
707 wret = dirty_tree_block(root, split_buffer);
710 wret = insert_ptr(root, path, split->keys, split_buffer->blocknr,
711 path->slots[level + 1] + 1, level + 1);
715 if (path->slots[level] >= mid) {
716 path->slots[level] -= mid;
717 tree_block_release(root, t);
718 path->nodes[level] = split_buffer;
719 path->slots[level + 1] += 1;
721 tree_block_release(root, split_buffer);
727 * how many bytes are required to store the items in a leaf. start
728 * and nr indicate which items in the leaf to check. This totals up the
729 * space used both by the item structs and the item data
731 static int leaf_space_used(struct leaf *l, int start, int nr)
734 int end = start + nr - 1;
738 data_len = l->items[start].offset + l->items[start].size;
739 data_len = data_len - l->items[end].offset;
740 data_len += sizeof(struct item) * nr;
745 * push some data in the path leaf to the right, trying to free up at
746 * least data_size bytes. returns zero if the push worked, nonzero otherwise
748 * returns 1 if the push failed because the other node didn't have enough
749 * room, 0 if everything worked out and < 0 if there were major errors.
751 static int push_leaf_right(struct ctree_root *root, struct ctree_path *path,
754 struct tree_buffer *left_buf = path->nodes[0];
755 struct leaf *left = &left_buf->leaf;
757 struct tree_buffer *right_buf;
758 struct tree_buffer *upper;
766 slot = path->slots[1];
767 if (!path->nodes[1]) {
770 upper = path->nodes[1];
771 if (slot >= upper->node.header.nritems - 1) {
774 right_buf = read_tree_block(root, upper->node.blockptrs[slot + 1]);
775 right = &right_buf->leaf;
776 free_space = leaf_free_space(right);
777 if (free_space < data_size + sizeof(struct item)) {
778 tree_block_release(root, right_buf);
781 for (i = left->header.nritems - 1; i >= 0; i--) {
782 item = left->items + i;
783 if (path->slots[0] == i)
784 push_space += data_size + sizeof(*item);
785 if (item->size + sizeof(*item) + push_space > free_space)
788 push_space += item->size + sizeof(*item);
790 if (push_items == 0) {
791 tree_block_release(root, right_buf);
794 /* push left to right */
795 push_space = left->items[left->header.nritems - push_items].offset +
796 left->items[left->header.nritems - push_items].size;
797 push_space -= leaf_data_end(left);
798 /* make room in the right data area */
799 memmove(right->data + leaf_data_end(right) - push_space,
800 right->data + leaf_data_end(right),
801 LEAF_DATA_SIZE - leaf_data_end(right));
802 /* copy from the left data area */
803 memcpy(right->data + LEAF_DATA_SIZE - push_space,
804 left->data + leaf_data_end(left),
806 memmove(right->items + push_items, right->items,
807 right->header.nritems * sizeof(struct item));
808 /* copy the items from left to right */
809 memcpy(right->items, left->items + left->header.nritems - push_items,
810 push_items * sizeof(struct item));
812 /* update the item pointers */
813 right->header.nritems += push_items;
814 push_space = LEAF_DATA_SIZE;
815 for (i = 0; i < right->header.nritems; i++) {
816 right->items[i].offset = push_space - right->items[i].size;
817 push_space = right->items[i].offset;
819 left->header.nritems -= push_items;
821 dirty_tree_block(root, left_buf);
822 dirty_tree_block(root, right_buf);
823 memcpy(upper->node.keys + slot + 1,
824 &right->items[0].key, sizeof(struct key));
825 dirty_tree_block(root, upper);
826 /* then fixup the leaf pointer in the path */
827 if (path->slots[0] >= left->header.nritems) {
828 path->slots[0] -= left->header.nritems;
829 tree_block_release(root, path->nodes[0]);
830 path->nodes[0] = right_buf;
833 tree_block_release(root, right_buf);
838 * push some data in the path leaf to the left, trying to free up at
839 * least data_size bytes. returns zero if the push worked, nonzero otherwise
841 static int push_leaf_left(struct ctree_root *root, struct ctree_path *path,
844 struct tree_buffer *right_buf = path->nodes[0];
845 struct leaf *right = &right_buf->leaf;
846 struct tree_buffer *t;
854 int old_left_nritems;
858 slot = path->slots[1];
862 if (!path->nodes[1]) {
865 t = read_tree_block(root, path->nodes[1]->node.blockptrs[slot - 1]);
867 free_space = leaf_free_space(left);
868 if (free_space < data_size + sizeof(struct item)) {
869 tree_block_release(root, t);
872 for (i = 0; i < right->header.nritems; i++) {
873 item = right->items + i;
874 if (path->slots[0] == i)
875 push_space += data_size + sizeof(*item);
876 if (item->size + sizeof(*item) + push_space > free_space)
879 push_space += item->size + sizeof(*item);
881 if (push_items == 0) {
882 tree_block_release(root, t);
885 /* push data from right to left */
886 memcpy(left->items + left->header.nritems,
887 right->items, push_items * sizeof(struct item));
888 push_space = LEAF_DATA_SIZE - right->items[push_items -1].offset;
889 memcpy(left->data + leaf_data_end(left) - push_space,
890 right->data + right->items[push_items - 1].offset,
892 old_left_nritems = left->header.nritems;
893 BUG_ON(old_left_nritems < 0);
895 for(i = old_left_nritems; i < old_left_nritems + push_items; i++) {
896 left->items[i].offset -= LEAF_DATA_SIZE -
897 left->items[old_left_nritems -1].offset;
899 left->header.nritems += push_items;
901 /* fixup right node */
902 push_space = right->items[push_items-1].offset - leaf_data_end(right);
903 memmove(right->data + LEAF_DATA_SIZE - push_space, right->data +
904 leaf_data_end(right), push_space);
905 memmove(right->items, right->items + push_items,
906 (right->header.nritems - push_items) * sizeof(struct item));
907 right->header.nritems -= push_items;
908 push_space = LEAF_DATA_SIZE;
910 for (i = 0; i < right->header.nritems; i++) {
911 right->items[i].offset = push_space - right->items[i].size;
912 push_space = right->items[i].offset;
915 wret = dirty_tree_block(root, t);
918 wret = dirty_tree_block(root, right_buf);
922 wret = fixup_low_keys(root, path, &right->items[0].key, 1);
926 /* then fixup the leaf pointer in the path */
927 if (path->slots[0] < push_items) {
928 path->slots[0] += old_left_nritems;
929 tree_block_release(root, path->nodes[0]);
933 tree_block_release(root, t);
934 path->slots[0] -= push_items;
936 BUG_ON(path->slots[0] < 0);
941 * split the path's leaf in two, making sure there is at least data_size
942 * available for the resulting leaf level of the path.
944 * returns 0 if all went well and < 0 on failure.
946 static int split_leaf(struct ctree_root *root, struct ctree_path *path,
949 struct tree_buffer *l_buf;
955 struct tree_buffer *right_buffer;
956 int space_needed = data_size + sizeof(struct item);
963 wret = push_leaf_left(root, path, data_size);
967 wret = push_leaf_right(root, path, data_size);
971 l_buf = path->nodes[0];
974 /* did the pushes work? */
975 if (leaf_free_space(l) >= sizeof(struct item) + data_size)
978 if (!path->nodes[1]) {
979 ret = insert_new_root(root, path, 1);
983 slot = path->slots[0];
984 nritems = l->header.nritems;
985 mid = (nritems + 1)/ 2;
987 right_buffer = alloc_free_block(root);
988 BUG_ON(!right_buffer);
989 BUG_ON(mid == nritems);
990 right = &right_buffer->leaf;
991 memset(right, 0, sizeof(*right));
993 /* FIXME, just alloc a new leaf here */
994 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
998 /* FIXME, just alloc a new leaf here */
999 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1003 right->header.nritems = nritems - mid;
1004 right->header.blocknr = right_buffer->blocknr;
1005 right->header.flags = node_level(0);
1006 right->header.parentid = root->node->node.header.parentid;
1007 data_copy_size = l->items[mid].offset + l->items[mid].size -
1009 memcpy(right->items, l->items + mid,
1010 (nritems - mid) * sizeof(struct item));
1011 memcpy(right->data + LEAF_DATA_SIZE - data_copy_size,
1012 l->data + leaf_data_end(l), data_copy_size);
1013 rt_data_off = LEAF_DATA_SIZE -
1014 (l->items[mid].offset + l->items[mid].size);
1016 for (i = 0; i < right->header.nritems; i++)
1017 right->items[i].offset += rt_data_off;
1019 l->header.nritems = mid;
1021 wret = insert_ptr(root, path, &right->items[0].key,
1022 right_buffer->blocknr, path->slots[1] + 1, 1);
1025 wret = dirty_tree_block(root, right_buffer);
1028 wret = dirty_tree_block(root, l_buf);
1032 BUG_ON(path->slots[0] != slot);
1034 tree_block_release(root, path->nodes[0]);
1035 path->nodes[0] = right_buffer;
1036 path->slots[0] -= mid;
1037 path->slots[1] += 1;
1039 tree_block_release(root, right_buffer);
1040 BUG_ON(path->slots[0] < 0);
1045 * Given a key and some data, insert an item into the tree.
1046 * This does all the path init required, making room in the tree if needed.
1048 int insert_item(struct ctree_root *root, struct key *key,
1049 void *data, int data_size)
1056 struct tree_buffer *leaf_buf;
1057 unsigned int nritems;
1058 unsigned int data_end;
1059 struct ctree_path path;
1061 /* create a root if there isn't one */
1065 ret = search_slot(root, key, &path, data_size);
1067 release_path(root, &path);
1073 slot_orig = path.slots[0];
1074 leaf_buf = path.nodes[0];
1075 leaf = &leaf_buf->leaf;
1077 nritems = leaf->header.nritems;
1078 data_end = leaf_data_end(leaf);
1080 if (leaf_free_space(leaf) < sizeof(struct item) + data_size)
1083 slot = path.slots[0];
1085 if (slot != nritems) {
1087 unsigned int old_data = leaf->items[slot].offset +
1088 leaf->items[slot].size;
1091 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1093 /* first correct the data pointers */
1094 for (i = slot; i < nritems; i++)
1095 leaf->items[i].offset -= data_size;
1097 /* shift the items */
1098 memmove(leaf->items + slot + 1, leaf->items + slot,
1099 (nritems - slot) * sizeof(struct item));
1101 /* shift the data */
1102 memmove(leaf->data + data_end - data_size, leaf->data +
1103 data_end, old_data - data_end);
1104 data_end = old_data;
1106 /* copy the new data in */
1107 memcpy(&leaf->items[slot].key, key, sizeof(struct key));
1108 leaf->items[slot].offset = data_end - data_size;
1109 leaf->items[slot].size = data_size;
1110 memcpy(leaf->data + data_end - data_size, data, data_size);
1111 leaf->header.nritems += 1;
1115 ret = fixup_low_keys(root, &path, key, 1);
1117 wret = dirty_tree_block(root, leaf_buf);
1121 if (leaf_free_space(leaf) < 0)
1123 check_leaf(&path, 0);
1125 release_path(root, &path);
1130 * delete the pointer from a given node.
1132 * If the delete empties a node, the node is removed from the tree,
1133 * continuing all the way the root if required. The root is converted into
1134 * a leaf if all the nodes are emptied.
1136 static int del_ptr(struct ctree_root *root, struct ctree_path *path, int level,
1140 struct tree_buffer *parent = path->nodes[level];
1145 node = &parent->node;
1146 nritems = node->header.nritems;
1148 if (slot != nritems -1) {
1149 memmove(node->keys + slot, node->keys + slot + 1,
1150 sizeof(struct key) * (nritems - slot - 1));
1151 memmove(node->blockptrs + slot,
1152 node->blockptrs + slot + 1,
1153 sizeof(u64) * (nritems - slot - 1));
1155 node->header.nritems--;
1156 if (node->header.nritems == 0 && parent == root->node) {
1157 BUG_ON(node_level(root->node->node.header.flags) != 1);
1158 /* just turn the root into a leaf and break */
1159 root->node->node.header.flags = node_level(0);
1160 } else if (slot == 0) {
1161 wret = fixup_low_keys(root, path, node->keys, level + 1);
1165 wret = dirty_tree_block(root, parent);
1172 * delete the item at the leaf level in path. If that empties
1173 * the leaf, remove it from the tree
1175 int del_item(struct ctree_root *root, struct ctree_path *path)
1179 struct tree_buffer *leaf_buf;
1185 leaf_buf = path->nodes[0];
1186 leaf = &leaf_buf->leaf;
1187 slot = path->slots[0];
1188 doff = leaf->items[slot].offset;
1189 dsize = leaf->items[slot].size;
1191 if (slot != leaf->header.nritems - 1) {
1193 int data_end = leaf_data_end(leaf);
1194 memmove(leaf->data + data_end + dsize,
1195 leaf->data + data_end,
1197 for (i = slot + 1; i < leaf->header.nritems; i++)
1198 leaf->items[i].offset += dsize;
1199 memmove(leaf->items + slot, leaf->items + slot + 1,
1200 sizeof(struct item) *
1201 (leaf->header.nritems - slot - 1));
1203 leaf->header.nritems -= 1;
1204 /* delete the leaf if we've emptied it */
1205 if (leaf->header.nritems == 0) {
1206 if (leaf_buf == root->node) {
1207 leaf->header.flags = node_level(0);
1208 dirty_tree_block(root, leaf_buf);
1210 clean_tree_block(root, leaf_buf);
1211 wret = del_ptr(root, path, 1, path->slots[1]);
1214 wret = free_extent(root, leaf_buf->blocknr, 1);
1219 int used = leaf_space_used(leaf, 0, leaf->header.nritems);
1221 wret = fixup_low_keys(root, path,
1222 &leaf->items[0].key, 1);
1226 wret = dirty_tree_block(root, leaf_buf);
1230 /* delete the leaf if it is mostly empty */
1231 if (used < LEAF_DATA_SIZE / 3) {
1232 /* push_leaf_left fixes the path.
1233 * make sure the path still points to our leaf
1234 * for possible call to del_ptr below
1236 slot = path->slots[1];
1238 wret = push_leaf_left(root, path, 1);
1241 if (path->nodes[0] == leaf_buf &&
1242 leaf->header.nritems) {
1243 wret = push_leaf_right(root, path, 1);
1247 if (leaf->header.nritems == 0) {
1248 u64 blocknr = leaf_buf->blocknr;
1249 clean_tree_block(root, leaf_buf);
1250 wret = del_ptr(root, path, 1, slot);
1253 tree_block_release(root, leaf_buf);
1254 wret = free_extent(root, blocknr, 1);
1258 tree_block_release(root, leaf_buf);
1266 * walk up the tree as far as required to find the next leaf.
1267 * returns 0 if it found something or 1 if there are no greater leaves.
1268 * returns < 0 on io errors.
1270 int next_leaf(struct ctree_root *root, struct ctree_path *path)
1275 struct tree_buffer *c;
1276 struct tree_buffer *next = NULL;
1278 while(level < MAX_LEVEL) {
1279 if (!path->nodes[level])
1281 slot = path->slots[level] + 1;
1282 c = path->nodes[level];
1283 if (slot >= c->node.header.nritems) {
1287 blocknr = c->node.blockptrs[slot];
1289 tree_block_release(root, next);
1290 next = read_tree_block(root, blocknr);
1293 path->slots[level] = slot;
1296 c = path->nodes[level];
1297 tree_block_release(root, c);
1298 path->nodes[level] = next;
1299 path->slots[level] = 0;
1302 next = read_tree_block(root, next->node.blockptrs[0]);