3 #include "kerncompat.h"
4 #include "radix-tree.h"
11 #define CTREE_EXTENT_PENDING 0
13 int split_node(struct ctree_root *root, struct ctree_path *path, int level);
14 int split_leaf(struct ctree_root *root, struct ctree_path *path, int data_size);
15 struct tree_buffer *alloc_free_block(struct ctree_root *root);
16 int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks);
18 static inline void init_path(struct ctree_path *p)
20 memset(p, 0, sizeof(*p));
23 static void release_path(struct ctree_root *root, struct ctree_path *p)
26 for (i = 0; i < MAX_LEVEL; i++) {
29 tree_block_release(root, p->nodes[i]);
34 * The leaf data grows from end-to-front in the node.
35 * this returns the address of the start of the last item,
36 * which is the stop of the leaf data stack
38 static inline unsigned int leaf_data_end(struct leaf *leaf)
40 unsigned int nr = leaf->header.nritems;
42 return sizeof(leaf->data);
43 return leaf->items[nr-1].offset;
47 * The space between the end of the leaf items and
48 * the start of the leaf data. IOW, how much room
49 * the leaf has left for both items and data
51 static inline int leaf_free_space(struct leaf *leaf)
53 int data_end = leaf_data_end(leaf);
54 int nritems = leaf->header.nritems;
55 char *items_end = (char *)(leaf->items + nritems + 1);
56 return (char *)(leaf->data + data_end) - (char *)items_end;
60 * compare two keys in a memcmp fashion
62 int comp_keys(struct key *k1, struct key *k2)
64 if (k1->objectid > k2->objectid)
66 if (k1->objectid < k2->objectid)
68 if (k1->flags > k2->flags)
70 if (k1->flags < k2->flags)
72 if (k1->offset > k2->offset)
74 if (k1->offset < k2->offset)
80 * search for key in the array p. items p are item_size apart
81 * and there are 'max' items in p
82 * the slot in the array is returned via slot, and it points to
83 * the place where you would insert key if it is not found in
86 * slot may point to max if the key is bigger than all of the keys
88 int generic_bin_search(char *p, int item_size, struct key *key,
98 mid = (low + high) / 2;
99 tmp = (struct key *)(p + mid * item_size);
100 ret = comp_keys(tmp, key);
115 int bin_search(struct node *c, struct key *key, int *slot)
117 if (is_leaf(c->header.flags)) {
118 struct leaf *l = (struct leaf *)c;
119 return generic_bin_search((void *)l->items, sizeof(struct item),
120 key, c->header.nritems, slot);
122 return generic_bin_search((void *)c->keys, sizeof(struct key),
123 key, c->header.nritems, slot);
129 * look for key in the tree. path is filled in with nodes along the way
130 * if key is found, we return zero and you can find the item in the leaf
131 * level of the path (level 0)
133 * If the key isn't found, the path points to the slot where it should
136 int search_slot(struct ctree_root *root, struct key *key, struct ctree_path *p, int ins_len)
138 struct tree_buffer *b = root->node;
147 level = node_level(c->header.flags);
149 ret = bin_search(c, key, &slot);
150 if (!is_leaf(c->header.flags)) {
153 p->slots[level] = slot;
154 if (ins_len && c->header.nritems == NODEPTRS_PER_BLOCK) {
155 int sret = split_node(root, p, level);
161 slot = p->slots[level];
163 b = read_tree_block(root, c->blockptrs[slot]);
166 struct leaf *l = (struct leaf *)c;
167 p->slots[level] = slot;
168 if (ins_len && leaf_free_space(l) < sizeof(struct item) + ins_len) {
169 int sret = split_leaf(root, p, ins_len);
181 * adjust the pointers going up the tree, starting at level
182 * making sure the right key of each node is points to 'key'.
183 * This is used after shifting pointers to the left, so it stops
184 * fixing up pointers when a given leaf/node is not in slot 0 of the
187 static void fixup_low_keys(struct ctree_root *root,
188 struct ctree_path *path, struct key *key,
192 for (i = level; i < MAX_LEVEL; i++) {
194 int tslot = path->slots[i];
197 t = &path->nodes[i]->node;
198 memcpy(t->keys + tslot, key, sizeof(*key));
199 write_tree_block(root, path->nodes[i]);
206 * try to push data from one node into the next node left in the
207 * tree. The src node is found at specified level in the path.
208 * If some bytes were pushed, return 0, otherwise return 1.
210 * Lower nodes/leaves in the path are not touched, higher nodes may
211 * be modified to reflect the push.
213 * The path is altered to reflect the push.
215 int push_node_left(struct ctree_root *root, struct ctree_path *path, int level)
223 struct tree_buffer *t;
224 struct tree_buffer *right_buf;
226 if (level == MAX_LEVEL - 1 || path->nodes[level + 1] == 0)
228 slot = path->slots[level + 1];
232 t = read_tree_block(root,
233 path->nodes[level + 1]->node.blockptrs[slot - 1]);
235 right_buf = path->nodes[level];
236 right = &right_buf->node;
237 left_nritems = left->header.nritems;
238 right_nritems = right->header.nritems;
239 push_items = NODEPTRS_PER_BLOCK - (left_nritems + 1);
240 if (push_items <= 0) {
241 tree_block_release(root, t);
245 if (right_nritems < push_items)
246 push_items = right_nritems;
247 memcpy(left->keys + left_nritems, right->keys,
248 push_items * sizeof(struct key));
249 memcpy(left->blockptrs + left_nritems, right->blockptrs,
250 push_items * sizeof(u64));
251 memmove(right->keys, right->keys + push_items,
252 (right_nritems - push_items) * sizeof(struct key));
253 memmove(right->blockptrs, right->blockptrs + push_items,
254 (right_nritems - push_items) * sizeof(u64));
255 right->header.nritems -= push_items;
256 left->header.nritems += push_items;
258 /* adjust the pointers going up the tree */
259 fixup_low_keys(root, path, right->keys, level + 1);
261 write_tree_block(root, t);
262 write_tree_block(root, right_buf);
264 /* then fixup the leaf pointer in the path */
265 if (path->slots[level] < push_items) {
266 path->slots[level] += left_nritems;
267 tree_block_release(root, path->nodes[level]);
268 path->nodes[level] = t;
269 path->slots[level + 1] -= 1;
271 path->slots[level] -= push_items;
272 tree_block_release(root, t);
278 * try to push data from one node into the next node right in the
279 * tree. The src node is found at specified level in the path.
280 * If some bytes were pushed, return 0, otherwise return 1.
282 * Lower nodes/leaves in the path are not touched, higher nodes may
283 * be modified to reflect the push.
285 * The path is altered to reflect the push.
287 int push_node_right(struct ctree_root *root, struct ctree_path *path, int level)
290 struct tree_buffer *t;
291 struct tree_buffer *src_buffer;
298 /* can't push from the root */
299 if (level == MAX_LEVEL - 1 || path->nodes[level + 1] == 0)
302 /* only try to push inside the node higher up */
303 slot = path->slots[level + 1];
304 if (slot == NODEPTRS_PER_BLOCK - 1)
307 if (slot >= path->nodes[level + 1]->node.header.nritems -1)
310 t = read_tree_block(root,
311 path->nodes[level + 1]->node.blockptrs[slot + 1]);
313 src_buffer = path->nodes[level];
314 src = &src_buffer->node;
315 dst_nritems = dst->header.nritems;
316 src_nritems = src->header.nritems;
317 push_items = NODEPTRS_PER_BLOCK - (dst_nritems + 1);
318 if (push_items <= 0) {
319 tree_block_release(root, t);
323 if (src_nritems < push_items)
324 push_items = src_nritems;
325 memmove(dst->keys + push_items, dst->keys,
326 dst_nritems * sizeof(struct key));
327 memcpy(dst->keys, src->keys + src_nritems - push_items,
328 push_items * sizeof(struct key));
330 memmove(dst->blockptrs + push_items, dst->blockptrs,
331 dst_nritems * sizeof(u64));
332 memcpy(dst->blockptrs, src->blockptrs + src_nritems - push_items,
333 push_items * sizeof(u64));
335 src->header.nritems -= push_items;
336 dst->header.nritems += push_items;
338 /* adjust the pointers going up the tree */
339 memcpy(path->nodes[level + 1]->node.keys + path->slots[level + 1] + 1,
340 dst->keys, sizeof(struct key));
342 write_tree_block(root, path->nodes[level + 1]);
343 write_tree_block(root, t);
344 write_tree_block(root, src_buffer);
346 /* then fixup the pointers in the path */
347 if (path->slots[level] >= src->header.nritems) {
348 path->slots[level] -= src->header.nritems;
349 tree_block_release(root, path->nodes[level]);
350 path->nodes[level] = t;
351 path->slots[level + 1] += 1;
353 tree_block_release(root, t);
358 static int insert_new_root(struct ctree_root *root, struct ctree_path *path, int level)
360 struct tree_buffer *t;
363 struct key *lower_key;
365 BUG_ON(path->nodes[level]);
366 BUG_ON(path->nodes[level-1] != root->node);
368 t = alloc_free_block(root);
370 memset(c, 0, sizeof(c));
371 c->header.nritems = 1;
372 c->header.flags = node_level(level);
373 c->header.blocknr = t->blocknr;
374 c->header.parentid = root->node->node.header.parentid;
375 lower = &path->nodes[level-1]->node;
376 if (is_leaf(lower->header.flags))
377 lower_key = &((struct leaf *)lower)->items[0].key;
379 lower_key = lower->keys;
380 memcpy(c->keys, lower_key, sizeof(struct key));
381 c->blockptrs[0] = path->nodes[level-1]->blocknr;
382 /* the super has an extra ref to root->node */
383 tree_block_release(root, root->node);
386 write_tree_block(root, t);
387 path->nodes[level] = t;
388 path->slots[level] = 0;
393 * worker function to insert a single pointer in a node.
394 * the node should have enough room for the pointer already
395 * slot and level indicate where you want the key to go, and
396 * blocknr is the block the key points to.
398 int insert_ptr(struct ctree_root *root,
399 struct ctree_path *path, struct key *key,
400 u64 blocknr, int slot, int level)
405 BUG_ON(!path->nodes[level]);
406 lower = &path->nodes[level]->node;
407 nritems = lower->header.nritems;
410 if (nritems == NODEPTRS_PER_BLOCK)
412 if (slot != nritems) {
413 memmove(lower->keys + slot + 1, lower->keys + slot,
414 (nritems - slot) * sizeof(struct key));
415 memmove(lower->blockptrs + slot + 1, lower->blockptrs + slot,
416 (nritems - slot) * sizeof(u64));
418 memcpy(lower->keys + slot, key, sizeof(struct key));
419 lower->blockptrs[slot] = blocknr;
420 lower->header.nritems++;
421 if (lower->keys[1].objectid == 0)
423 write_tree_block(root, path->nodes[level]);
427 int split_node(struct ctree_root *root, struct ctree_path *path, int level)
429 struct tree_buffer *t;
431 struct tree_buffer *split_buffer;
436 ret = push_node_left(root, path, level);
439 ret = push_node_right(root, path, level);
442 t = path->nodes[level];
444 if (t == root->node) {
445 /* trying to split the root, lets make a new one */
446 ret = insert_new_root(root, path, level + 1);
450 split_buffer = alloc_free_block(root);
451 split = &split_buffer->node;
452 split->header.flags = c->header.flags;
453 split->header.blocknr = split_buffer->blocknr;
454 split->header.parentid = root->node->node.header.parentid;
455 mid = (c->header.nritems + 1) / 2;
456 memcpy(split->keys, c->keys + mid,
457 (c->header.nritems - mid) * sizeof(struct key));
458 memcpy(split->blockptrs, c->blockptrs + mid,
459 (c->header.nritems - mid) * sizeof(u64));
460 split->header.nritems = c->header.nritems - mid;
461 c->header.nritems = mid;
462 write_tree_block(root, t);
463 write_tree_block(root, split_buffer);
464 insert_ptr(root, path, split->keys, split_buffer->blocknr,
465 path->slots[level + 1] + 1, level + 1);
466 if (path->slots[level] > mid) {
467 path->slots[level] -= mid;
468 tree_block_release(root, t);
469 path->nodes[level] = split_buffer;
470 path->slots[level + 1] += 1;
472 tree_block_release(root, split_buffer);
478 * how many bytes are required to store the items in a leaf. start
479 * and nr indicate which items in the leaf to check. This totals up the
480 * space used both by the item structs and the item data
482 int leaf_space_used(struct leaf *l, int start, int nr)
485 int end = start + nr - 1;
489 data_len = l->items[start].offset + l->items[start].size;
490 data_len = data_len - l->items[end].offset;
491 data_len += sizeof(struct item) * nr;
496 * push some data in the path leaf to the left, trying to free up at
497 * least data_size bytes. returns zero if the push worked, nonzero otherwise
499 int push_leaf_left(struct ctree_root *root, struct ctree_path *path,
502 struct tree_buffer *right_buf = path->nodes[0];
503 struct leaf *right = &right_buf->leaf;
504 struct tree_buffer *t;
512 int old_left_nritems;
514 slot = path->slots[1];
518 if (!path->nodes[1]) {
521 t = read_tree_block(root, path->nodes[1]->node.blockptrs[slot - 1]);
523 free_space = leaf_free_space(left);
524 if (free_space < data_size + sizeof(struct item)) {
525 tree_block_release(root, t);
528 for (i = 0; i < right->header.nritems; i++) {
529 item = right->items + i;
530 if (path->slots[0] == i)
531 push_space += data_size + sizeof(*item);
532 if (item->size + sizeof(*item) + push_space > free_space)
535 push_space += item->size + sizeof(*item);
537 if (push_items == 0) {
538 tree_block_release(root, t);
541 /* push data from right to left */
542 memcpy(left->items + left->header.nritems,
543 right->items, push_items * sizeof(struct item));
544 push_space = LEAF_DATA_SIZE - right->items[push_items -1].offset;
545 memcpy(left->data + leaf_data_end(left) - push_space,
546 right->data + right->items[push_items - 1].offset,
548 old_left_nritems = left->header.nritems;
549 BUG_ON(old_left_nritems < 0);
551 for(i = old_left_nritems; i < old_left_nritems + push_items; i++) {
552 left->items[i].offset -= LEAF_DATA_SIZE -
553 left->items[old_left_nritems -1].offset;
555 left->header.nritems += push_items;
557 /* fixup right node */
558 push_space = right->items[push_items-1].offset - leaf_data_end(right);
559 memmove(right->data + LEAF_DATA_SIZE - push_space, right->data +
560 leaf_data_end(right), push_space);
561 memmove(right->items, right->items + push_items,
562 (right->header.nritems - push_items) * sizeof(struct item));
563 right->header.nritems -= push_items;
564 push_space = LEAF_DATA_SIZE;
566 for (i = 0; i < right->header.nritems; i++) {
567 right->items[i].offset = push_space - right->items[i].size;
568 push_space = right->items[i].offset;
571 write_tree_block(root, t);
572 write_tree_block(root, right_buf);
574 fixup_low_keys(root, path, &right->items[0].key, 1);
576 /* then fixup the leaf pointer in the path */
577 if (path->slots[0] < push_items) {
578 path->slots[0] += old_left_nritems;
579 tree_block_release(root, path->nodes[0]);
583 tree_block_release(root, t);
584 path->slots[0] -= push_items;
586 BUG_ON(path->slots[0] < 0);
591 * split the path's leaf in two, making sure there is at least data_size
592 * available for the resulting leaf level of the path.
594 int split_leaf(struct ctree_root *root, struct ctree_path *path, int data_size)
596 struct tree_buffer *l_buf = path->nodes[0];
597 struct leaf *l = &l_buf->leaf;
602 struct tree_buffer *right_buffer;
603 int space_needed = data_size + sizeof(struct item);
609 if (push_leaf_left(root, path, data_size) == 0) {
610 l_buf = path->nodes[0];
612 if (leaf_free_space(l) >= sizeof(struct item) + data_size)
615 if (!path->nodes[1]) {
616 ret = insert_new_root(root, path, 1);
620 slot = path->slots[0];
621 nritems = l->header.nritems;
622 mid = (nritems + 1)/ 2;
624 right_buffer = alloc_free_block(root);
625 BUG_ON(!right_buffer);
626 BUG_ON(mid == nritems);
627 right = &right_buffer->leaf;
628 memset(right, 0, sizeof(*right));
630 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
634 if (leaf_space_used(l, 0, mid + 1) + space_needed >
638 right->header.nritems = nritems - mid;
639 right->header.blocknr = right_buffer->blocknr;
640 right->header.flags = node_level(0);
641 right->header.parentid = root->node->node.header.parentid;
642 data_copy_size = l->items[mid].offset + l->items[mid].size -
644 memcpy(right->items, l->items + mid,
645 (nritems - mid) * sizeof(struct item));
646 memcpy(right->data + LEAF_DATA_SIZE - data_copy_size,
647 l->data + leaf_data_end(l), data_copy_size);
648 rt_data_off = LEAF_DATA_SIZE -
649 (l->items[mid].offset + l->items[mid].size);
651 for (i = 0; i < right->header.nritems; i++)
652 right->items[i].offset += rt_data_off;
654 l->header.nritems = mid;
655 ret = insert_ptr(root, path, &right->items[0].key,
656 right_buffer->blocknr, path->slots[1] + 1, 1);
657 write_tree_block(root, right_buffer);
658 write_tree_block(root, l_buf);
660 BUG_ON(path->slots[0] != slot);
662 tree_block_release(root, path->nodes[0]);
663 path->nodes[0] = right_buffer;
664 path->slots[0] -= mid;
667 tree_block_release(root, right_buffer);
668 BUG_ON(path->slots[0] < 0);
673 * Given a key and some data, insert an item into the tree.
674 * This does all the path init required, making room in the tree if needed.
676 int insert_item(struct ctree_root *root, struct key *key,
677 void *data, int data_size)
683 struct tree_buffer *leaf_buf;
684 unsigned int nritems;
685 unsigned int data_end;
686 struct ctree_path path;
688 /* create a root if there isn't one */
692 ret = search_slot(root, key, &path, data_size);
694 release_path(root, &path);
698 slot_orig = path.slots[0];
699 leaf_buf = path.nodes[0];
700 leaf = &leaf_buf->leaf;
702 nritems = leaf->header.nritems;
703 data_end = leaf_data_end(leaf);
705 if (leaf_free_space(leaf) < sizeof(struct item) + data_size)
708 slot = path.slots[0];
711 fixup_low_keys(root, &path, key, 1);
712 if (slot != nritems) {
714 unsigned int old_data = leaf->items[slot].offset +
715 leaf->items[slot].size;
718 * item0..itemN ... dataN.offset..dataN.size .. data0.size
720 /* first correct the data pointers */
721 for (i = slot; i < nritems; i++)
722 leaf->items[i].offset -= data_size;
724 /* shift the items */
725 memmove(leaf->items + slot + 1, leaf->items + slot,
726 (nritems - slot) * sizeof(struct item));
729 memmove(leaf->data + data_end - data_size, leaf->data +
730 data_end, old_data - data_end);
733 /* copy the new data in */
734 memcpy(&leaf->items[slot].key, key, sizeof(struct key));
735 leaf->items[slot].offset = data_end - data_size;
736 leaf->items[slot].size = data_size;
737 memcpy(leaf->data + data_end - data_size, data, data_size);
738 leaf->header.nritems += 1;
739 write_tree_block(root, leaf_buf);
740 if (leaf_free_space(leaf) < 0)
742 release_path(root, &path);
747 * delete the pointer from a given level in the path. The path is not
748 * fixed up, so after calling this it is not valid at that level.
750 * If the delete empties a node, the node is removed from the tree,
751 * continuing all the way the root if required. The root is converted into
752 * a leaf if all the nodes are emptied.
754 int del_ptr(struct ctree_root *root, struct ctree_path *path, int level)
757 struct tree_buffer *t;
763 t = path->nodes[level];
767 slot = path->slots[level];
768 nritems = node->header.nritems;
770 if (slot != nritems -1) {
771 memmove(node->keys + slot, node->keys + slot + 1,
772 sizeof(struct key) * (nritems - slot - 1));
773 memmove(node->blockptrs + slot,
774 node->blockptrs + slot + 1,
775 sizeof(u64) * (nritems - slot - 1));
777 node->header.nritems--;
778 write_tree_block(root, t);
779 blocknr = t->blocknr;
780 if (node->header.nritems != 0) {
783 fixup_low_keys(root, path, node->keys,
785 tslot = path->slots[level+1];
787 push_node_left(root, path, level);
788 if (node->header.nritems) {
789 push_node_right(root, path, level);
791 if (node->header.nritems) {
792 tree_block_release(root, t);
795 tree_block_release(root, t);
796 path->slots[level+1] = tslot;
798 if (t == root->node) {
799 /* just turn the root into a leaf and break */
800 root->node->node.header.flags = node_level(0);
801 write_tree_block(root, t);
805 free_extent(root, blocknr, 1);
806 if (!path->nodes[level])
813 * delete the item at the leaf level in path. If that empties
814 * the leaf, remove it from the tree
816 int del_item(struct ctree_root *root, struct ctree_path *path)
820 struct tree_buffer *leaf_buf;
824 leaf_buf = path->nodes[0];
825 leaf = &leaf_buf->leaf;
826 slot = path->slots[0];
827 doff = leaf->items[slot].offset;
828 dsize = leaf->items[slot].size;
830 if (slot != leaf->header.nritems - 1) {
832 int data_end = leaf_data_end(leaf);
833 memmove(leaf->data + data_end + dsize,
834 leaf->data + data_end,
836 for (i = slot + 1; i < leaf->header.nritems; i++)
837 leaf->items[i].offset += dsize;
838 memmove(leaf->items + slot, leaf->items + slot + 1,
839 sizeof(struct item) *
840 (leaf->header.nritems - slot - 1));
842 leaf->header.nritems -= 1;
843 /* delete the leaf if we've emptied it */
844 if (leaf->header.nritems == 0) {
845 if (leaf_buf == root->node) {
846 leaf->header.flags = node_level(0);
847 write_tree_block(root, leaf_buf);
849 del_ptr(root, path, 1);
850 free_extent(root, leaf_buf->blocknr, 1);
854 fixup_low_keys(root, path, &leaf->items[0].key, 1);
855 write_tree_block(root, leaf_buf);
856 /* delete the leaf if it is mostly empty */
857 if (leaf_space_used(leaf, 0, leaf->header.nritems) <
858 LEAF_DATA_SIZE / 4) {
859 /* push_leaf_left fixes the path.
860 * make sure the path still points to our leaf
861 * for possible call to del_ptr below
863 slot = path->slots[1];
865 push_leaf_left(root, path, 1);
866 if (leaf->header.nritems == 0) {
867 path->slots[1] = slot;
868 del_ptr(root, path, 1);
870 tree_block_release(root, leaf_buf);
876 static int del_pending_extents(struct ctree_root *extent_root)
880 struct tree_buffer *gang[4];
882 struct ctree_path path;
885 ret = radix_tree_gang_lookup_tag(&extent_root->cache_radix,
886 (void **)gang, 0, ARRAY_SIZE(gang),
887 CTREE_EXTENT_PENDING);
890 for (i = 0; i < ret; i++) {
891 key.objectid = gang[i]->blocknr;
895 ret = search_slot(extent_root, &key, &path, 0);
898 // FIXME undo it and return sane
901 ret = del_item(extent_root, &path);
906 release_path(extent_root, &path);
907 radix_tree_tag_clear(&extent_root->cache_radix, gang[i]->blocknr,
908 CTREE_EXTENT_PENDING);
909 tree_block_release(extent_root, gang[i]);
915 int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks)
917 struct ctree_path path;
919 struct ctree_root *extent_root = root->extent_root;
920 struct tree_buffer *t;
924 key.objectid = blocknr;
926 key.offset = num_blocks;
927 if (root == extent_root) {
928 t = read_tree_block(root, key.objectid);
929 radix_tree_tag_set(&root->cache_radix, key.objectid, CTREE_EXTENT_PENDING);
933 ret = search_slot(extent_root, &key, &path, 0);
936 ret = del_item(extent_root, &path);
937 release_path(extent_root, &path);
938 pending_ret = del_pending_extents(root->extent_root);
939 return ret ? ret : pending_ret;
942 int next_leaf(struct ctree_root *root, struct ctree_path *path)
947 struct tree_buffer *c;
948 struct tree_buffer *next = NULL;
950 while(level < MAX_LEVEL) {
951 if (!path->nodes[level])
953 slot = path->slots[level] + 1;
954 c = path->nodes[level];
955 if (slot >= c->node.header.nritems) {
959 blocknr = c->node.blockptrs[slot];
961 tree_block_release(root, next);
962 next = read_tree_block(root, blocknr);
965 path->slots[level] = slot;
968 c = path->nodes[level];
969 tree_block_release(root, c);
970 path->nodes[level] = next;
971 path->slots[level] = 0;
974 next = read_tree_block(root, next->node.blockptrs[0]);
979 int find_free_extent(struct ctree_root *orig_root, u64 num_blocks, u64 search_start,
980 u64 search_end, struct key *ins)
982 struct ctree_path path;
990 struct ctree_root * root = orig_root->extent_root;
993 ins->objectid = search_start;
996 ret = search_slot(root, ins, &path, 0);
998 l = &path.nodes[0]->leaf;
999 slot = path.slots[0];
1001 // FIXME allocate root
1003 if (slot >= l->header.nritems) {
1004 ret = next_leaf(root, &path);
1008 ins->objectid = search_start;
1009 ins->offset = num_blocks;
1010 hole_size = search_end - search_start;
1014 ins->objectid = last_block;
1015 ins->offset = num_blocks;
1016 hole_size = search_end - last_block;
1019 key = &l->items[slot].key;
1021 hole_size = key->objectid - last_block;
1022 if (hole_size > num_blocks) {
1023 ins->objectid = last_block;
1024 ins->offset = num_blocks;
1029 last_block = key->objectid + key->offset;
1035 if (orig_root->extent_root == orig_root) {
1036 BUG_ON(num_blocks != 1);
1037 if ((root->current_insert.objectid <= ins->objectid &&
1038 root->current_insert.objectid + root->current_insert.offset >
1040 (root->current_insert.objectid > ins->objectid &&
1041 root->current_insert.objectid <= ins->objectid + ins->offset) ||
1042 radix_tree_tag_get(&root->cache_radix, ins->objectid,
1043 CTREE_EXTENT_PENDING)) {
1044 last_block = ins->objectid + 1;
1045 search_start = last_block;
1049 release_path(root, &path);
1050 if (ins->offset != 1)
1055 static int insert_pending_extents(struct ctree_root *extent_root)
1059 struct extent_item item;
1060 struct tree_buffer *gang[4];
1065 item.owner = extent_root->node->node.header.parentid;
1067 ret = radix_tree_gang_lookup_tag(&extent_root->cache_radix,
1068 (void **)gang, 0, ARRAY_SIZE(gang),
1069 CTREE_EXTENT_PENDING);
1072 for (i = 0; i < ret; i++) {
1073 key.objectid = gang[i]->blocknr;
1076 ret = insert_item(extent_root, &key, &item, sizeof(item));
1079 // FIXME undo it and return sane
1082 radix_tree_tag_clear(&extent_root->cache_radix, gang[i]->blocknr,
1083 CTREE_EXTENT_PENDING);
1084 tree_block_release(extent_root, gang[i]);
1090 int alloc_extent(struct ctree_root *root, u64 num_blocks, u64 search_start,
1091 u64 search_end, u64 owner, struct key *ins, struct tree_buffer **buf)
1095 struct extent_item extent_item;
1097 extent_item.refs = 1;
1098 extent_item.owner = owner;
1100 ret = find_free_extent(root, num_blocks, search_start, search_end, ins);
1104 if (root != root->extent_root) {
1105 memcpy(&root->extent_root->current_insert, ins, sizeof(*ins));
1106 ret = insert_item(root->extent_root, ins, &extent_item, sizeof(extent_item));
1107 memset(&root->extent_root->current_insert, 0, sizeof(struct key));
1108 pending_ret = insert_pending_extents(root->extent_root);
1113 *buf = find_tree_block(root, ins->objectid);
1116 /* we're allocating an extent for the extent tree, don't recurse */
1117 BUG_ON(ins->offset != 1);
1118 *buf = find_tree_block(root, ins->objectid);
1120 radix_tree_tag_set(&root->cache_radix, ins->objectid, CTREE_EXTENT_PENDING);
1126 struct tree_buffer *alloc_free_block(struct ctree_root *root)
1130 struct tree_buffer *buf = NULL;
1132 ret = alloc_extent(root, 1, 0, (unsigned long)-1, root->node->node.header.parentid,
1139 if (root != root->extent_root)
1140 BUG_ON(radix_tree_tag_get(&root->extent_root->cache_radix, buf->blocknr,
1141 CTREE_EXTENT_PENDING));
1145 void print_leaf(struct leaf *l)
1148 int nr = l->header.nritems;
1150 struct extent_item *ei;
1151 printf("leaf %lu total ptrs %d free space %d\n", l->header.blocknr, nr,
1152 leaf_free_space(l));
1154 for (i = 0 ; i < nr ; i++) {
1155 item = l->items + i;
1156 printf("\titem %d key (%lu %u %lu) itemoff %d itemsize %d\n",
1158 item->key.objectid, item->key.flags, item->key.offset,
1159 item->offset, item->size);
1161 printf("\t\titem data %.*s\n", item->size, l->data+item->offset);
1162 ei = (struct extent_item *)(l->data + item->offset);
1163 printf("\t\textent data %u %lu\n", ei->refs, ei->owner);
1167 void print_tree(struct ctree_root *root, struct tree_buffer *t)
1176 nr = c->header.nritems;
1177 if (c->header.blocknr != t->blocknr)
1179 if (is_leaf(c->header.flags)) {
1180 print_leaf((struct leaf *)c);
1183 printf("node %lu level %d total ptrs %d free spc %lu\n", t->blocknr,
1184 node_level(c->header.flags), c->header.nritems,
1185 NODEPTRS_PER_BLOCK - c->header.nritems);
1187 for (i = 0; i < nr; i++) {
1188 printf("\tkey %d (%lu %u %lu) block %lu\n",
1190 c->keys[i].objectid, c->keys[i].flags, c->keys[i].offset,
1194 for (i = 0; i < nr; i++) {
1195 struct tree_buffer *next_buf = read_tree_block(root,
1197 struct node *next = &next_buf->node;
1198 if (is_leaf(next->header.flags) &&
1199 node_level(c->header.flags) != 1)
1201 if (node_level(next->header.flags) !=
1202 node_level(c->header.flags) - 1)
1204 print_tree(root, next_buf);
1205 tree_block_release(root, next_buf);
1210 /* for testing only */
1211 int next_key(int i, int max_key) {
1212 // return rand() % max_key;
1217 struct ctree_root *root;
1219 struct key last = { (u64)-1, 0, 0};
1224 int run_size = 10000;
1225 int max_key = 100000000;
1227 struct ctree_path path;
1228 struct ctree_super_block super;
1233 root = open_ctree("dbfile", &super);
1234 printf("root tree\n");
1235 print_tree(root, root->node);
1236 printf("map tree\n");
1237 print_tree(root->extent_root, root->extent_root->node);
1241 for (i = 0; i < run_size; i++) {
1243 num = next_key(i, max_key);
1245 sprintf(buf, "string-%d", num);
1246 // printf("insert %d\n", num);
1250 ret = insert_item(root, &ins, buf, strlen(buf));
1254 write_ctree_super(root, &super);
1257 root = open_ctree("dbfile", &super);
1258 printf("starting search\n");
1260 for (i = 0; i < run_size; i++) {
1261 num = next_key(i, max_key);
1264 ret = search_slot(root, &ins, &path, 0);
1266 print_tree(root, root->node);
1267 printf("unable to find %d\n", num);
1270 release_path(root, &path);
1272 write_ctree_super(root, &super);
1274 root = open_ctree("dbfile", &super);
1275 printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
1276 node_level(root->node->node.header.flags),
1277 root->node->node.header.nritems,
1278 NODEPTRS_PER_BLOCK - root->node->node.header.nritems);
1279 printf("all searches good, deleting some items\n");
1282 for (i = 0 ; i < run_size/4; i++) {
1283 num = next_key(i, max_key);
1286 ret = search_slot(root, &ins, &path, 0);
1289 ret = del_item(root, &path);
1292 release_path(root, &path);
1296 for (i = 0; i < run_size; i++) {
1298 num = next_key(i, max_key);
1299 sprintf(buf, "string-%d", num);
1301 ret = insert_item(root, &ins, buf, strlen(buf));
1310 ret = search_slot(root, &ugh, &path, 0);
1312 print_tree(root, root->node);
1313 printf("unable to find 5 %d\n", num);
1316 release_path(root, &path);
1320 write_ctree_super(root, &super);
1322 root = open_ctree("dbfile", &super);
1324 printf("starting search2\n");
1325 for (i = 0; i < run_size; i++) {
1326 num = next_key(i, max_key);
1329 ret = search_slot(root, &ins, &path, 0);
1331 print_tree(root, root->node);
1332 printf("unable to find %d\n", num);
1335 release_path(root, &path);
1337 printf("starting big long delete run\n");
1338 while(root->node && root->node->node.header.nritems > 0) {
1341 ins.objectid = (u64)-1;
1343 ret = search_slot(root, &ins, &path, 0);
1347 leaf = &path.nodes[0]->leaf;
1348 slot = path.slots[0];
1349 if (slot != leaf->header.nritems)
1351 while(path.slots[0] > 0) {
1353 slot = path.slots[0];
1354 leaf = &path.nodes[0]->leaf;
1356 if (comp_keys(&last, &leaf->items[slot].key) <= 0)
1358 memcpy(&last, &leaf->items[slot].key, sizeof(last));
1359 ret = del_item(root, &path);
1361 printf("del_item returned %d\n", ret);
1366 release_path(root, &path);
1368 write_ctree_super(root, &super);
1370 printf("tree size is now %d\n", tree_size);
1371 printf("map tree\n");
1372 print_tree(root->extent_root, root->extent_root->node);