3 #include "kerncompat.h"
4 #include "radix-tree.h"
7 #include "print-tree.h"
9 static int split_node(struct btrfs_root *root, struct btrfs_path *path,
11 static int split_leaf(struct btrfs_root *root, struct btrfs_path *path,
13 static int push_node_left(struct btrfs_root *root, struct btrfs_buffer *dst,
14 struct btrfs_buffer *src);
15 static int balance_node_right(struct btrfs_root *root,
16 struct btrfs_buffer *dst_buf,
17 struct btrfs_buffer *src_buf);
18 static int del_ptr(struct btrfs_root *root, struct btrfs_path *path, int level,
21 inline void btrfs_init_path(struct btrfs_path *p)
23 memset(p, 0, sizeof(*p));
26 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
29 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
32 btrfs_block_release(root, p->nodes[i]);
34 memset(p, 0, sizeof(*p));
37 static int btrfs_cow_block(struct btrfs_root *root,
38 struct btrfs_buffer *buf,
39 struct btrfs_buffer *parent,
41 struct btrfs_buffer **cow_ret)
43 struct btrfs_buffer *cow;
45 if (!list_empty(&buf->dirty)) {
49 cow = btrfs_alloc_free_block(root);
50 memcpy(&cow->node, &buf->node, root->blocksize);
51 btrfs_set_header_blocknr(&cow->node.header, cow->blocknr);
53 btrfs_inc_ref(root, buf);
54 if (buf == root->node) {
57 if (buf != root->commit_root)
58 btrfs_free_extent(root, buf->blocknr, 1);
59 btrfs_block_release(root, buf);
61 btrfs_set_node_blockptr(&parent->node, parent_slot,
63 BUG_ON(list_empty(&parent->dirty));
64 btrfs_free_extent(root, buf->blocknr, 1);
66 btrfs_block_release(root, buf);
71 * The leaf data grows from end-to-front in the node.
72 * this returns the address of the start of the last item,
73 * which is the stop of the leaf data stack
75 static inline unsigned int leaf_data_end(struct btrfs_root *root,
76 struct btrfs_leaf *leaf)
78 u32 nr = btrfs_header_nritems(&leaf->header);
80 return BTRFS_LEAF_DATA_SIZE(root);
81 return btrfs_item_offset(leaf->items + nr - 1);
85 * The space between the end of the leaf items and
86 * the start of the leaf data. IOW, how much room
87 * the leaf has left for both items and data
89 int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
91 int data_end = leaf_data_end(root, leaf);
92 int nritems = btrfs_header_nritems(&leaf->header);
93 char *items_end = (char *)(leaf->items + nritems + 1);
94 return (char *)(btrfs_leaf_data(leaf) + data_end) - (char *)items_end;
98 * compare two keys in a memcmp fashion
100 static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
104 btrfs_disk_key_to_cpu(&k1, disk);
106 if (k1.objectid > k2->objectid)
108 if (k1.objectid < k2->objectid)
110 if (k1.flags > k2->flags)
112 if (k1.flags < k2->flags)
114 if (k1.offset > k2->offset)
116 if (k1.offset < k2->offset)
121 static int check_node(struct btrfs_root *root, struct btrfs_path *path,
125 struct btrfs_node *parent = NULL;
126 struct btrfs_node *node = &path->nodes[level]->node;
128 u32 nritems = btrfs_header_nritems(&node->header);
130 if (path->nodes[level + 1])
131 parent = &path->nodes[level + 1]->node;
132 parent_slot = path->slots[level + 1];
133 BUG_ON(nritems == 0);
135 struct btrfs_disk_key *parent_key;
136 parent_key = &parent->ptrs[parent_slot].key;
137 BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
138 sizeof(struct btrfs_disk_key)));
139 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
140 btrfs_header_blocknr(&node->header));
142 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
143 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
144 struct btrfs_key cpukey;
145 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[i + 1].key);
146 BUG_ON(comp_keys(&node->ptrs[i].key, &cpukey) >= 0);
151 static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
155 struct btrfs_leaf *leaf = &path->nodes[level]->leaf;
156 struct btrfs_node *parent = NULL;
158 u32 nritems = btrfs_header_nritems(&leaf->header);
160 if (path->nodes[level + 1])
161 parent = &path->nodes[level + 1]->node;
162 parent_slot = path->slots[level + 1];
163 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
169 struct btrfs_disk_key *parent_key;
170 parent_key = &parent->ptrs[parent_slot].key;
171 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
172 sizeof(struct btrfs_disk_key)));
173 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
174 btrfs_header_blocknr(&leaf->header));
176 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
177 struct btrfs_key cpukey;
178 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key);
179 BUG_ON(comp_keys(&leaf->items[i].key,
181 BUG_ON(btrfs_item_offset(leaf->items + i) !=
182 btrfs_item_end(leaf->items + i + 1));
184 BUG_ON(btrfs_item_offset(leaf->items + i) +
185 btrfs_item_size(leaf->items + i) !=
186 BTRFS_LEAF_DATA_SIZE(root));
192 static int check_block(struct btrfs_root *root, struct btrfs_path *path,
196 return check_leaf(root, path, level);
197 return check_node(root, path, level);
201 * search for key in the array p. items p are item_size apart
202 * and there are 'max' items in p
203 * the slot in the array is returned via slot, and it points to
204 * the place where you would insert key if it is not found in
207 * slot may point to max if the key is bigger than all of the keys
209 static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
216 struct btrfs_disk_key *tmp;
219 mid = (low + high) / 2;
220 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
221 ret = comp_keys(tmp, key);
237 * simple bin_search frontend that does the right thing for
240 static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
242 if (btrfs_is_leaf(c)) {
243 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
244 return generic_bin_search((void *)l->items,
245 sizeof(struct btrfs_item),
246 key, btrfs_header_nritems(&c->header),
249 return generic_bin_search((void *)c->ptrs,
250 sizeof(struct btrfs_key_ptr),
251 key, btrfs_header_nritems(&c->header),
257 static struct btrfs_buffer *read_node_slot(struct btrfs_root *root,
258 struct btrfs_buffer *parent_buf,
261 struct btrfs_node *node = &parent_buf->node;
264 if (slot >= btrfs_header_nritems(&node->header))
266 return read_tree_block(root, btrfs_node_blockptr(node, slot));
269 static int balance_level(struct btrfs_root *root, struct btrfs_path *path,
272 struct btrfs_buffer *right_buf;
273 struct btrfs_buffer *mid_buf;
274 struct btrfs_buffer *left_buf;
275 struct btrfs_buffer *parent_buf = NULL;
276 struct btrfs_node *right = NULL;
277 struct btrfs_node *mid;
278 struct btrfs_node *left = NULL;
279 struct btrfs_node *parent = NULL;
283 int orig_slot = path->slots[level];
289 mid_buf = path->nodes[level];
290 mid = &mid_buf->node;
291 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
293 if (level < BTRFS_MAX_LEVEL - 1)
294 parent_buf = path->nodes[level + 1];
295 pslot = path->slots[level + 1];
298 struct btrfs_buffer *child;
299 u64 blocknr = mid_buf->blocknr;
301 if (btrfs_header_nritems(&mid->header) != 1)
304 /* promote the child to a root */
305 child = read_node_slot(root, mid_buf, 0);
308 path->nodes[level] = NULL;
309 /* once for the path */
310 btrfs_block_release(root, mid_buf);
311 /* once for the root ptr */
312 btrfs_block_release(root, mid_buf);
313 clean_tree_block(root, mid_buf);
314 return btrfs_free_extent(root, blocknr, 1);
316 parent = &parent_buf->node;
318 if (btrfs_header_nritems(&mid->header) >
319 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
322 left_buf = read_node_slot(root, parent_buf, pslot - 1);
323 right_buf = read_node_slot(root, parent_buf, pslot + 1);
325 /* first, try to make some room in the middle buffer */
327 btrfs_cow_block(root, left_buf, parent_buf,
328 pslot - 1, &left_buf);
329 left = &left_buf->node;
330 orig_slot += btrfs_header_nritems(&left->header);
331 wret = push_node_left(root, left_buf, mid_buf);
337 * then try to empty the right most buffer into the middle
340 btrfs_cow_block(root, right_buf, parent_buf,
341 pslot + 1, &right_buf);
342 right = &right_buf->node;
343 wret = push_node_left(root, mid_buf, right_buf);
346 if (btrfs_header_nritems(&right->header) == 0) {
347 u64 blocknr = right_buf->blocknr;
348 btrfs_block_release(root, right_buf);
349 clean_tree_block(root, right_buf);
352 wret = del_ptr(root, path, level + 1, pslot + 1);
355 wret = btrfs_free_extent(root, blocknr, 1);
359 memcpy(&parent->ptrs[pslot + 1].key,
361 sizeof(struct btrfs_disk_key));
362 BUG_ON(list_empty(&parent_buf->dirty));
365 if (btrfs_header_nritems(&mid->header) == 1) {
367 * we're not allowed to leave a node with one item in the
368 * tree during a delete. A deletion from lower in the tree
369 * could try to delete the only pointer in this node.
370 * So, pull some keys from the left.
371 * There has to be a left pointer at this point because
372 * otherwise we would have pulled some pointers from the
376 wret = balance_node_right(root, mid_buf, left_buf);
381 if (btrfs_header_nritems(&mid->header) == 0) {
382 /* we've managed to empty the middle node, drop it */
383 u64 blocknr = mid_buf->blocknr;
384 btrfs_block_release(root, mid_buf);
385 clean_tree_block(root, mid_buf);
388 wret = del_ptr(root, path, level + 1, pslot);
391 wret = btrfs_free_extent(root, blocknr, 1);
395 /* update the parent key to reflect our changes */
396 memcpy(&parent->ptrs[pslot].key, &mid->ptrs[0].key,
397 sizeof(struct btrfs_disk_key));
398 BUG_ON(list_empty(&parent_buf->dirty));
401 /* update the path */
403 if (btrfs_header_nritems(&left->header) > orig_slot) {
404 left_buf->count++; // released below
405 path->nodes[level] = left_buf;
406 path->slots[level + 1] -= 1;
407 path->slots[level] = orig_slot;
409 btrfs_block_release(root, mid_buf);
411 orig_slot -= btrfs_header_nritems(&left->header);
412 path->slots[level] = orig_slot;
415 /* double check we haven't messed things up */
416 check_block(root, path, level);
417 if (orig_ptr != btrfs_node_blockptr(&path->nodes[level]->node,
422 btrfs_block_release(root, right_buf);
424 btrfs_block_release(root, left_buf);
429 * look for key in the tree. path is filled in with nodes along the way
430 * if key is found, we return zero and you can find the item in the leaf
431 * level of the path (level 0)
433 * If the key isn't found, the path points to the slot where it should
434 * be inserted, and 1 is returned. If there are other errors during the
435 * search a negative error number is returned.
437 * if ins_len > 0, nodes and leaves will be split as we walk down the
438 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
441 int btrfs_search_slot(struct btrfs_root *root, struct btrfs_key *key,
442 struct btrfs_path *p, int ins_len, int cow)
444 struct btrfs_buffer *b;
445 struct btrfs_buffer *cow_buf;
446 struct btrfs_node *c;
455 level = btrfs_header_level(&b->node.header);
458 wret = btrfs_cow_block(root, b, p->nodes[level + 1],
459 p->slots[level + 1], &cow_buf);
462 BUG_ON(!cow && ins_len);
465 ret = check_block(root, p, level);
468 ret = bin_search(c, key, &slot);
469 if (!btrfs_is_leaf(c)) {
472 p->slots[level] = slot;
473 if (ins_len > 0 && btrfs_header_nritems(&c->header) ==
474 BTRFS_NODEPTRS_PER_BLOCK(root)) {
475 int sret = split_node(root, p, level);
481 slot = p->slots[level];
482 } else if (ins_len < 0) {
483 int sret = balance_level(root, p, level);
490 slot = p->slots[level];
491 BUG_ON(btrfs_header_nritems(&c->header) == 1);
493 b = read_tree_block(root, btrfs_node_blockptr(c, slot));
495 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
496 p->slots[level] = slot;
497 if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
498 sizeof(struct btrfs_item) + ins_len) {
499 int sret = split_leaf(root, p, ins_len);
504 BUG_ON(root->node->count == 1);
508 BUG_ON(root->node->count == 1);
513 * adjust the pointers going up the tree, starting at level
514 * making sure the right key of each node is points to 'key'.
515 * This is used after shifting pointers to the left, so it stops
516 * fixing up pointers when a given leaf/node is not in slot 0 of the
519 * If this fails to write a tree block, it returns -1, but continues
520 * fixing up the blocks in ram so the tree is consistent.
522 static int fixup_low_keys(struct btrfs_root *root,
523 struct btrfs_path *path, struct btrfs_disk_key *key,
528 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
529 struct btrfs_node *t;
530 int tslot = path->slots[i];
533 t = &path->nodes[i]->node;
534 memcpy(&t->ptrs[tslot].key, key, sizeof(*key));
535 BUG_ON(list_empty(&path->nodes[i]->dirty));
543 * try to push data from one node into the next node left in the
546 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
547 * error, and > 0 if there was no room in the left hand block.
549 static int push_node_left(struct btrfs_root *root, struct btrfs_buffer *dst_buf,
550 struct btrfs_buffer *src_buf)
552 struct btrfs_node *src = &src_buf->node;
553 struct btrfs_node *dst = &dst_buf->node;
559 src_nritems = btrfs_header_nritems(&src->header);
560 dst_nritems = btrfs_header_nritems(&dst->header);
561 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
562 if (push_items <= 0) {
566 if (src_nritems < push_items)
567 push_items = src_nritems;
569 memcpy(dst->ptrs + dst_nritems, src->ptrs,
570 push_items * sizeof(struct btrfs_key_ptr));
571 if (push_items < src_nritems) {
572 memmove(src->ptrs, src->ptrs + push_items,
573 (src_nritems - push_items) *
574 sizeof(struct btrfs_key_ptr));
576 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
577 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
578 BUG_ON(list_empty(&src_buf->dirty));
579 BUG_ON(list_empty(&dst_buf->dirty));
584 * try to push data from one node into the next node right in the
587 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
588 * error, and > 0 if there was no room in the right hand block.
590 * this will only push up to 1/2 the contents of the left node over
592 static int balance_node_right(struct btrfs_root *root,
593 struct btrfs_buffer *dst_buf,
594 struct btrfs_buffer *src_buf)
596 struct btrfs_node *src = &src_buf->node;
597 struct btrfs_node *dst = &dst_buf->node;
604 src_nritems = btrfs_header_nritems(&src->header);
605 dst_nritems = btrfs_header_nritems(&dst->header);
606 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
607 if (push_items <= 0) {
611 max_push = src_nritems / 2 + 1;
612 /* don't try to empty the node */
613 if (max_push > src_nritems)
615 if (max_push < push_items)
616 push_items = max_push;
618 memmove(dst->ptrs + push_items, dst->ptrs,
619 dst_nritems * sizeof(struct btrfs_key_ptr));
620 memcpy(dst->ptrs, src->ptrs + src_nritems - push_items,
621 push_items * sizeof(struct btrfs_key_ptr));
623 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
624 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
626 BUG_ON(list_empty(&src_buf->dirty));
627 BUG_ON(list_empty(&dst_buf->dirty));
632 * helper function to insert a new root level in the tree.
633 * A new node is allocated, and a single item is inserted to
634 * point to the existing root
636 * returns zero on success or < 0 on failure.
638 static int insert_new_root(struct btrfs_root *root,
639 struct btrfs_path *path, int level)
641 struct btrfs_buffer *t;
642 struct btrfs_node *lower;
643 struct btrfs_node *c;
644 struct btrfs_disk_key *lower_key;
646 BUG_ON(path->nodes[level]);
647 BUG_ON(path->nodes[level-1] != root->node);
649 t = btrfs_alloc_free_block(root);
651 memset(c, 0, root->blocksize);
652 btrfs_set_header_nritems(&c->header, 1);
653 btrfs_set_header_level(&c->header, level);
654 btrfs_set_header_blocknr(&c->header, t->blocknr);
655 btrfs_set_header_parentid(&c->header,
656 btrfs_header_parentid(&root->node->node.header));
657 lower = &path->nodes[level-1]->node;
658 if (btrfs_is_leaf(lower))
659 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
661 lower_key = &lower->ptrs[0].key;
662 memcpy(&c->ptrs[0].key, lower_key, sizeof(struct btrfs_disk_key));
663 btrfs_set_node_blockptr(c, 0, path->nodes[level - 1]->blocknr);
664 /* the super has an extra ref to root->node */
665 btrfs_block_release(root, root->node);
668 path->nodes[level] = t;
669 path->slots[level] = 0;
674 * worker function to insert a single pointer in a node.
675 * the node should have enough room for the pointer already
677 * slot and level indicate where you want the key to go, and
678 * blocknr is the block the key points to.
680 * returns zero on success and < 0 on any error
682 static int insert_ptr(struct btrfs_root *root,
683 struct btrfs_path *path, struct btrfs_disk_key *key,
684 u64 blocknr, int slot, int level)
686 struct btrfs_node *lower;
689 BUG_ON(!path->nodes[level]);
690 lower = &path->nodes[level]->node;
691 nritems = btrfs_header_nritems(&lower->header);
694 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
696 if (slot != nritems) {
697 memmove(lower->ptrs + slot + 1, lower->ptrs + slot,
698 (nritems - slot) * sizeof(struct btrfs_key_ptr));
700 memcpy(&lower->ptrs[slot].key, key, sizeof(struct btrfs_disk_key));
701 btrfs_set_node_blockptr(lower, slot, blocknr);
702 btrfs_set_header_nritems(&lower->header, nritems + 1);
703 BUG_ON(list_empty(&path->nodes[level]->dirty));
708 * split the node at the specified level in path in two.
709 * The path is corrected to point to the appropriate node after the split
711 * Before splitting this tries to make some room in the node by pushing
712 * left and right, if either one works, it returns right away.
714 * returns 0 on success and < 0 on failure
716 static int split_node(struct btrfs_root *root, struct btrfs_path *path,
719 struct btrfs_buffer *t;
720 struct btrfs_node *c;
721 struct btrfs_buffer *split_buffer;
722 struct btrfs_node *split;
728 t = path->nodes[level];
730 if (t == root->node) {
731 /* trying to split the root, lets make a new one */
732 ret = insert_new_root(root, path, level + 1);
736 c_nritems = btrfs_header_nritems(&c->header);
737 split_buffer = btrfs_alloc_free_block(root);
738 split = &split_buffer->node;
739 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
740 btrfs_set_header_blocknr(&split->header, split_buffer->blocknr);
741 btrfs_set_header_parentid(&split->header,
742 btrfs_header_parentid(&root->node->node.header));
743 mid = (c_nritems + 1) / 2;
744 memcpy(split->ptrs, c->ptrs + mid,
745 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
746 btrfs_set_header_nritems(&split->header, c_nritems - mid);
747 btrfs_set_header_nritems(&c->header, mid);
750 BUG_ON(list_empty(&t->dirty));
751 wret = insert_ptr(root, path, &split->ptrs[0].key,
752 split_buffer->blocknr, path->slots[level + 1] + 1,
757 if (path->slots[level] >= mid) {
758 path->slots[level] -= mid;
759 btrfs_block_release(root, t);
760 path->nodes[level] = split_buffer;
761 path->slots[level + 1] += 1;
763 btrfs_block_release(root, split_buffer);
769 * how many bytes are required to store the items in a leaf. start
770 * and nr indicate which items in the leaf to check. This totals up the
771 * space used both by the item structs and the item data
773 static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
776 int end = start + nr - 1;
780 data_len = btrfs_item_end(l->items + start);
781 data_len = data_len - btrfs_item_offset(l->items + end);
782 data_len += sizeof(struct btrfs_item) * nr;
787 * push some data in the path leaf to the right, trying to free up at
788 * least data_size bytes. returns zero if the push worked, nonzero otherwise
790 * returns 1 if the push failed because the other node didn't have enough
791 * room, 0 if everything worked out and < 0 if there were major errors.
793 static int push_leaf_right(struct btrfs_root *root, struct btrfs_path *path,
796 struct btrfs_buffer *left_buf = path->nodes[0];
797 struct btrfs_leaf *left = &left_buf->leaf;
798 struct btrfs_leaf *right;
799 struct btrfs_buffer *right_buf;
800 struct btrfs_buffer *upper;
806 struct btrfs_item *item;
810 slot = path->slots[1];
811 if (!path->nodes[1]) {
814 upper = path->nodes[1];
815 if (slot >= btrfs_header_nritems(&upper->node.header) - 1) {
818 right_buf = read_tree_block(root, btrfs_node_blockptr(&upper->node,
820 right = &right_buf->leaf;
821 free_space = btrfs_leaf_free_space(root, right);
822 if (free_space < data_size + sizeof(struct btrfs_item)) {
823 btrfs_block_release(root, right_buf);
826 /* cow and double check */
827 btrfs_cow_block(root, right_buf, upper, slot + 1, &right_buf);
828 right = &right_buf->leaf;
829 free_space = btrfs_leaf_free_space(root, right);
830 if (free_space < data_size + sizeof(struct btrfs_item)) {
831 btrfs_block_release(root, right_buf);
835 left_nritems = btrfs_header_nritems(&left->header);
836 for (i = left_nritems - 1; i >= 0; i--) {
837 item = left->items + i;
838 if (path->slots[0] == i)
839 push_space += data_size + sizeof(*item);
840 if (btrfs_item_size(item) + sizeof(*item) + push_space >
844 push_space += btrfs_item_size(item) + sizeof(*item);
846 if (push_items == 0) {
847 btrfs_block_release(root, right_buf);
850 right_nritems = btrfs_header_nritems(&right->header);
851 /* push left to right */
852 push_space = btrfs_item_end(left->items + left_nritems - push_items);
853 push_space -= leaf_data_end(root, left);
854 /* make room in the right data area */
855 memmove(btrfs_leaf_data(right) + leaf_data_end(root, right) -
856 push_space, btrfs_leaf_data(right) + leaf_data_end(root, right),
857 BTRFS_LEAF_DATA_SIZE(root) - leaf_data_end(root, right));
858 /* copy from the left data area */
859 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - push_space,
860 btrfs_leaf_data(left) + leaf_data_end(root, left), push_space);
861 memmove(right->items + push_items, right->items,
862 right_nritems * sizeof(struct btrfs_item));
863 /* copy the items from left to right */
864 memcpy(right->items, left->items + left_nritems - push_items,
865 push_items * sizeof(struct btrfs_item));
867 /* update the item pointers */
868 right_nritems += push_items;
869 btrfs_set_header_nritems(&right->header, right_nritems);
870 push_space = BTRFS_LEAF_DATA_SIZE(root);
871 for (i = 0; i < right_nritems; i++) {
872 btrfs_set_item_offset(right->items + i, push_space -
873 btrfs_item_size(right->items + i));
874 push_space = btrfs_item_offset(right->items + i);
876 left_nritems -= push_items;
877 btrfs_set_header_nritems(&left->header, left_nritems);
879 BUG_ON(list_empty(&left_buf->dirty));
880 BUG_ON(list_empty(&right_buf->dirty));
881 memcpy(&upper->node.ptrs[slot + 1].key,
882 &right->items[0].key, sizeof(struct btrfs_disk_key));
883 BUG_ON(list_empty(&upper->dirty));
885 /* then fixup the leaf pointer in the path */
886 if (path->slots[0] >= left_nritems) {
887 path->slots[0] -= left_nritems;
888 btrfs_block_release(root, path->nodes[0]);
889 path->nodes[0] = right_buf;
892 btrfs_block_release(root, right_buf);
897 * push some data in the path leaf to the left, trying to free up at
898 * least data_size bytes. returns zero if the push worked, nonzero otherwise
900 static int push_leaf_left(struct btrfs_root *root, struct btrfs_path *path,
903 struct btrfs_buffer *right_buf = path->nodes[0];
904 struct btrfs_leaf *right = &right_buf->leaf;
905 struct btrfs_buffer *t;
906 struct btrfs_leaf *left;
912 struct btrfs_item *item;
913 u32 old_left_nritems;
917 slot = path->slots[1];
921 if (!path->nodes[1]) {
924 t = read_tree_block(root, btrfs_node_blockptr(&path->nodes[1]->node,
927 free_space = btrfs_leaf_free_space(root, left);
928 if (free_space < data_size + sizeof(struct btrfs_item)) {
929 btrfs_block_release(root, t);
933 /* cow and double check */
934 btrfs_cow_block(root, t, path->nodes[1], slot - 1, &t);
936 free_space = btrfs_leaf_free_space(root, left);
937 if (free_space < data_size + sizeof(struct btrfs_item)) {
938 btrfs_block_release(root, t);
942 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
943 item = right->items + i;
944 if (path->slots[0] == i)
945 push_space += data_size + sizeof(*item);
946 if (btrfs_item_size(item) + sizeof(*item) + push_space >
950 push_space += btrfs_item_size(item) + sizeof(*item);
952 if (push_items == 0) {
953 btrfs_block_release(root, t);
956 /* push data from right to left */
957 memcpy(left->items + btrfs_header_nritems(&left->header),
958 right->items, push_items * sizeof(struct btrfs_item));
959 push_space = BTRFS_LEAF_DATA_SIZE(root) -
960 btrfs_item_offset(right->items + push_items -1);
961 memcpy(btrfs_leaf_data(left) + leaf_data_end(root, left) - push_space,
962 btrfs_leaf_data(right) +
963 btrfs_item_offset(right->items + push_items - 1),
965 old_left_nritems = btrfs_header_nritems(&left->header);
966 BUG_ON(old_left_nritems < 0);
968 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
969 u32 ioff = btrfs_item_offset(left->items + i);
970 btrfs_set_item_offset(left->items + i, ioff -
971 (BTRFS_LEAF_DATA_SIZE(root) -
972 btrfs_item_offset(left->items +
973 old_left_nritems - 1)));
975 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
977 /* fixup right node */
978 push_space = btrfs_item_offset(right->items + push_items - 1) -
979 leaf_data_end(root, right);
980 memmove(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
981 push_space, btrfs_leaf_data(right) +
982 leaf_data_end(root, right), push_space);
983 memmove(right->items, right->items + push_items,
984 (btrfs_header_nritems(&right->header) - push_items) *
985 sizeof(struct btrfs_item));
986 btrfs_set_header_nritems(&right->header,
987 btrfs_header_nritems(&right->header) -
989 push_space = BTRFS_LEAF_DATA_SIZE(root);
991 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
992 btrfs_set_item_offset(right->items + i, push_space -
993 btrfs_item_size(right->items + i));
994 push_space = btrfs_item_offset(right->items + i);
997 BUG_ON(list_empty(&t->dirty));
998 BUG_ON(list_empty(&right_buf->dirty));
1000 wret = fixup_low_keys(root, path, &right->items[0].key, 1);
1004 /* then fixup the leaf pointer in the path */
1005 if (path->slots[0] < push_items) {
1006 path->slots[0] += old_left_nritems;
1007 btrfs_block_release(root, path->nodes[0]);
1009 path->slots[1] -= 1;
1011 btrfs_block_release(root, t);
1012 path->slots[0] -= push_items;
1014 BUG_ON(path->slots[0] < 0);
1019 * split the path's leaf in two, making sure there is at least data_size
1020 * available for the resulting leaf level of the path.
1022 * returns 0 if all went well and < 0 on failure.
1024 static int split_leaf(struct btrfs_root *root, struct btrfs_path *path,
1027 struct btrfs_buffer *l_buf;
1028 struct btrfs_leaf *l;
1032 struct btrfs_leaf *right;
1033 struct btrfs_buffer *right_buffer;
1034 int space_needed = data_size + sizeof(struct btrfs_item);
1041 wret = push_leaf_left(root, path, data_size);
1045 wret = push_leaf_right(root, path, data_size);
1049 l_buf = path->nodes[0];
1052 /* did the pushes work? */
1053 if (btrfs_leaf_free_space(root, l) >=
1054 sizeof(struct btrfs_item) + data_size)
1057 if (!path->nodes[1]) {
1058 ret = insert_new_root(root, path, 1);
1062 slot = path->slots[0];
1063 nritems = btrfs_header_nritems(&l->header);
1064 mid = (nritems + 1)/ 2;
1065 right_buffer = btrfs_alloc_free_block(root);
1066 BUG_ON(!right_buffer);
1067 BUG_ON(mid == nritems);
1068 right = &right_buffer->leaf;
1069 memset(&right->header, 0, sizeof(right->header));
1071 /* FIXME, just alloc a new leaf here */
1072 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
1073 BTRFS_LEAF_DATA_SIZE(root))
1076 /* FIXME, just alloc a new leaf here */
1077 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1078 BTRFS_LEAF_DATA_SIZE(root))
1081 btrfs_set_header_nritems(&right->header, nritems - mid);
1082 btrfs_set_header_blocknr(&right->header, right_buffer->blocknr);
1083 btrfs_set_header_level(&right->header, 0);
1084 btrfs_set_header_parentid(&right->header,
1085 btrfs_header_parentid(&root->node->node.header));
1086 data_copy_size = btrfs_item_end(l->items + mid) -
1087 leaf_data_end(root, l);
1088 memcpy(right->items, l->items + mid,
1089 (nritems - mid) * sizeof(struct btrfs_item));
1090 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1091 data_copy_size, btrfs_leaf_data(l) +
1092 leaf_data_end(root, l), data_copy_size);
1093 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1094 btrfs_item_end(l->items + mid);
1096 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
1097 u32 ioff = btrfs_item_offset(right->items + i);
1098 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1101 btrfs_set_header_nritems(&l->header, mid);
1103 wret = insert_ptr(root, path, &right->items[0].key,
1104 right_buffer->blocknr, path->slots[1] + 1, 1);
1107 BUG_ON(list_empty(&right_buffer->dirty));
1108 BUG_ON(list_empty(&l_buf->dirty));
1109 BUG_ON(path->slots[0] != slot);
1111 btrfs_block_release(root, path->nodes[0]);
1112 path->nodes[0] = right_buffer;
1113 path->slots[0] -= mid;
1114 path->slots[1] += 1;
1116 btrfs_block_release(root, right_buffer);
1117 BUG_ON(path->slots[0] < 0);
1122 * Given a key and some data, insert an item into the tree.
1123 * This does all the path init required, making room in the tree if needed.
1125 int btrfs_insert_empty_item(struct btrfs_root *root, struct btrfs_path *path,
1126 struct btrfs_key *cpu_key, u32 data_size)
1131 struct btrfs_leaf *leaf;
1132 struct btrfs_buffer *leaf_buf;
1134 unsigned int data_end;
1135 struct btrfs_disk_key disk_key;
1137 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
1139 /* create a root if there isn't one */
1142 ret = btrfs_search_slot(root, cpu_key, path, data_size, 1);
1144 btrfs_release_path(root, path);
1150 slot_orig = path->slots[0];
1151 leaf_buf = path->nodes[0];
1152 leaf = &leaf_buf->leaf;
1154 nritems = btrfs_header_nritems(&leaf->header);
1155 data_end = leaf_data_end(root, leaf);
1157 if (btrfs_leaf_free_space(root, leaf) <
1158 sizeof(struct btrfs_item) + data_size)
1161 slot = path->slots[0];
1163 if (slot != nritems) {
1165 unsigned int old_data = btrfs_item_end(leaf->items + slot);
1168 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1170 /* first correct the data pointers */
1171 for (i = slot; i < nritems; i++) {
1172 u32 ioff = btrfs_item_offset(leaf->items + i);
1173 btrfs_set_item_offset(leaf->items + i,
1177 /* shift the items */
1178 memmove(leaf->items + slot + 1, leaf->items + slot,
1179 (nritems - slot) * sizeof(struct btrfs_item));
1181 /* shift the data */
1182 memmove(btrfs_leaf_data(leaf) + data_end - data_size,
1183 btrfs_leaf_data(leaf) +
1184 data_end, old_data - data_end);
1185 data_end = old_data;
1187 /* setup the item for the new data */
1188 memcpy(&leaf->items[slot].key, &disk_key,
1189 sizeof(struct btrfs_disk_key));
1190 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1191 btrfs_set_item_size(leaf->items + slot, data_size);
1192 btrfs_set_header_nritems(&leaf->header, nritems + 1);
1196 ret = fixup_low_keys(root, path, &disk_key, 1);
1198 BUG_ON(list_empty(&leaf_buf->dirty));
1199 if (btrfs_leaf_free_space(root, leaf) < 0)
1201 check_leaf(root, path, 0);
1207 * Given a key and some data, insert an item into the tree.
1208 * This does all the path init required, making room in the tree if needed.
1210 int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *cpu_key,
1211 void *data, u32 data_size)
1214 struct btrfs_path path;
1217 btrfs_init_path(&path);
1218 ret = btrfs_insert_empty_item(root, &path, cpu_key, data_size);
1220 ptr = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0], u8);
1221 memcpy(ptr, data, data_size);
1223 btrfs_release_path(root, &path);
1228 * delete the pointer from a given node.
1230 * If the delete empties a node, the node is removed from the tree,
1231 * continuing all the way the root if required. The root is converted into
1232 * a leaf if all the nodes are emptied.
1234 static int del_ptr(struct btrfs_root *root, struct btrfs_path *path, int level,
1237 struct btrfs_node *node;
1238 struct btrfs_buffer *parent = path->nodes[level];
1243 node = &parent->node;
1244 nritems = btrfs_header_nritems(&node->header);
1245 if (slot != nritems -1) {
1246 memmove(node->ptrs + slot, node->ptrs + slot + 1,
1247 sizeof(struct btrfs_key_ptr) * (nritems - slot - 1));
1250 btrfs_set_header_nritems(&node->header, nritems);
1251 if (nritems == 0 && parent == root->node) {
1252 BUG_ON(btrfs_header_level(&root->node->node.header) != 1);
1253 /* just turn the root into a leaf and break */
1254 btrfs_set_header_level(&root->node->node.header, 0);
1255 } else if (slot == 0) {
1256 wret = fixup_low_keys(root, path, &node->ptrs[0].key,
1261 BUG_ON(list_empty(&parent->dirty));
1266 * delete the item at the leaf level in path. If that empties
1267 * the leaf, remove it from the tree
1269 int btrfs_del_item(struct btrfs_root *root, struct btrfs_path *path)
1272 struct btrfs_leaf *leaf;
1273 struct btrfs_buffer *leaf_buf;
1280 leaf_buf = path->nodes[0];
1281 leaf = &leaf_buf->leaf;
1282 slot = path->slots[0];
1283 doff = btrfs_item_offset(leaf->items + slot);
1284 dsize = btrfs_item_size(leaf->items + slot);
1285 nritems = btrfs_header_nritems(&leaf->header);
1287 if (slot != nritems - 1) {
1289 int data_end = leaf_data_end(root, leaf);
1290 memmove(btrfs_leaf_data(leaf) + data_end + dsize,
1291 btrfs_leaf_data(leaf) + data_end,
1293 for (i = slot + 1; i < nritems; i++) {
1294 u32 ioff = btrfs_item_offset(leaf->items + i);
1295 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
1297 memmove(leaf->items + slot, leaf->items + slot + 1,
1298 sizeof(struct btrfs_item) *
1299 (nritems - slot - 1));
1301 btrfs_set_header_nritems(&leaf->header, nritems - 1);
1303 /* delete the leaf if we've emptied it */
1305 if (leaf_buf == root->node) {
1306 btrfs_set_header_level(&leaf->header, 0);
1307 BUG_ON(list_empty(&leaf_buf->dirty));
1309 clean_tree_block(root, leaf_buf);
1310 wret = del_ptr(root, path, 1, path->slots[1]);
1313 wret = btrfs_free_extent(root, leaf_buf->blocknr, 1);
1318 int used = leaf_space_used(leaf, 0, nritems);
1320 wret = fixup_low_keys(root, path,
1321 &leaf->items[0].key, 1);
1325 BUG_ON(list_empty(&leaf_buf->dirty));
1327 /* delete the leaf if it is mostly empty */
1328 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
1329 /* push_leaf_left fixes the path.
1330 * make sure the path still points to our leaf
1331 * for possible call to del_ptr below
1333 slot = path->slots[1];
1335 wret = push_leaf_left(root, path, 1);
1338 if (path->nodes[0] == leaf_buf &&
1339 btrfs_header_nritems(&leaf->header)) {
1340 wret = push_leaf_right(root, path, 1);
1344 if (btrfs_header_nritems(&leaf->header) == 0) {
1345 u64 blocknr = leaf_buf->blocknr;
1346 clean_tree_block(root, leaf_buf);
1347 wret = del_ptr(root, path, 1, slot);
1350 btrfs_block_release(root, leaf_buf);
1351 wret = btrfs_free_extent(root, blocknr, 1);
1355 btrfs_block_release(root, leaf_buf);
1363 * walk up the tree as far as required to find the next leaf.
1364 * returns 0 if it found something or 1 if there are no greater leaves.
1365 * returns < 0 on io errors.
1367 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
1372 struct btrfs_buffer *c;
1373 struct btrfs_buffer *next = NULL;
1375 while(level < BTRFS_MAX_LEVEL) {
1376 if (!path->nodes[level])
1378 slot = path->slots[level] + 1;
1379 c = path->nodes[level];
1380 if (slot >= btrfs_header_nritems(&c->node.header)) {
1384 blocknr = btrfs_node_blockptr(&c->node, slot);
1386 btrfs_block_release(root, next);
1387 next = read_tree_block(root, blocknr);
1390 path->slots[level] = slot;
1393 c = path->nodes[level];
1394 btrfs_block_release(root, c);
1395 path->nodes[level] = next;
1396 path->slots[level] = 0;
1399 next = read_tree_block(root,
1400 btrfs_node_blockptr(&next->node, 0));