1 #include <linux/module.h>
5 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
6 *root, struct btrfs_path *path, int level);
7 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
8 *root, struct btrfs_path *path, int data_size);
9 static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
10 *root, struct btrfs_buffer *dst, struct btrfs_buffer
12 static int balance_node_right(struct btrfs_trans_handle *trans, struct
13 btrfs_root *root, struct btrfs_buffer *dst_buf,
14 struct btrfs_buffer *src_buf);
15 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
16 struct btrfs_path *path, int level, int slot);
18 inline void btrfs_init_path(struct btrfs_path *p)
20 memset(p, 0, sizeof(*p));
23 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
26 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
29 btrfs_block_release(root, p->nodes[i]);
31 memset(p, 0, sizeof(*p));
34 static int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
35 *root, struct btrfs_buffer *buf, struct btrfs_buffer
36 *parent, int parent_slot, struct btrfs_buffer
39 struct btrfs_buffer *cow;
41 if (!list_empty(&buf->dirty)) {
45 cow = btrfs_alloc_free_block(trans, root);
46 memcpy(&cow->node, &buf->node, root->blocksize);
47 btrfs_set_header_blocknr(&cow->node.header, cow->blocknr);
49 btrfs_inc_ref(trans, root, buf);
50 if (buf == root->node) {
53 if (buf != root->commit_root)
54 btrfs_free_extent(trans, root, buf->blocknr, 1, 1);
55 btrfs_block_release(root, buf);
57 btrfs_set_node_blockptr(&parent->node, parent_slot,
59 BUG_ON(list_empty(&parent->dirty));
60 btrfs_free_extent(trans, root, buf->blocknr, 1, 1);
62 btrfs_block_release(root, buf);
67 * The leaf data grows from end-to-front in the node.
68 * this returns the address of the start of the last item,
69 * which is the stop of the leaf data stack
71 static inline unsigned int leaf_data_end(struct btrfs_root *root,
72 struct btrfs_leaf *leaf)
74 u32 nr = btrfs_header_nritems(&leaf->header);
76 return BTRFS_LEAF_DATA_SIZE(root);
77 return btrfs_item_offset(leaf->items + nr - 1);
81 * The space between the end of the leaf items and
82 * the start of the leaf data. IOW, how much room
83 * the leaf has left for both items and data
85 int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
87 int data_end = leaf_data_end(root, leaf);
88 int nritems = btrfs_header_nritems(&leaf->header);
89 char *items_end = (char *)(leaf->items + nritems + 1);
90 return (char *)(btrfs_leaf_data(leaf) + data_end) - (char *)items_end;
94 * compare two keys in a memcmp fashion
96 static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
100 btrfs_disk_key_to_cpu(&k1, disk);
102 if (k1.objectid > k2->objectid)
104 if (k1.objectid < k2->objectid)
106 if (k1.flags > k2->flags)
108 if (k1.flags < k2->flags)
110 if (k1.offset > k2->offset)
112 if (k1.offset < k2->offset)
117 static int check_node(struct btrfs_root *root, struct btrfs_path *path,
121 struct btrfs_node *parent = NULL;
122 struct btrfs_node *node = &path->nodes[level]->node;
124 u32 nritems = btrfs_header_nritems(&node->header);
126 if (path->nodes[level + 1])
127 parent = &path->nodes[level + 1]->node;
128 parent_slot = path->slots[level + 1];
129 BUG_ON(nritems == 0);
131 struct btrfs_disk_key *parent_key;
132 parent_key = &parent->ptrs[parent_slot].key;
133 BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
134 sizeof(struct btrfs_disk_key)));
135 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
136 btrfs_header_blocknr(&node->header));
138 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
139 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
140 struct btrfs_key cpukey;
141 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[i + 1].key);
142 BUG_ON(comp_keys(&node->ptrs[i].key, &cpukey) >= 0);
147 static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
151 struct btrfs_leaf *leaf = &path->nodes[level]->leaf;
152 struct btrfs_node *parent = NULL;
154 u32 nritems = btrfs_header_nritems(&leaf->header);
156 if (path->nodes[level + 1])
157 parent = &path->nodes[level + 1]->node;
158 parent_slot = path->slots[level + 1];
159 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
165 struct btrfs_disk_key *parent_key;
166 parent_key = &parent->ptrs[parent_slot].key;
167 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
168 sizeof(struct btrfs_disk_key)));
169 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
170 btrfs_header_blocknr(&leaf->header));
172 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
173 struct btrfs_key cpukey;
174 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key);
175 BUG_ON(comp_keys(&leaf->items[i].key,
177 BUG_ON(btrfs_item_offset(leaf->items + i) !=
178 btrfs_item_end(leaf->items + i + 1));
180 BUG_ON(btrfs_item_offset(leaf->items + i) +
181 btrfs_item_size(leaf->items + i) !=
182 BTRFS_LEAF_DATA_SIZE(root));
188 static int check_block(struct btrfs_root *root, struct btrfs_path *path,
192 return check_leaf(root, path, level);
193 return check_node(root, path, level);
197 * search for key in the array p. items p are item_size apart
198 * and there are 'max' items in p
199 * the slot in the array is returned via slot, and it points to
200 * the place where you would insert key if it is not found in
203 * slot may point to max if the key is bigger than all of the keys
205 static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
212 struct btrfs_disk_key *tmp;
215 mid = (low + high) / 2;
216 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
217 ret = comp_keys(tmp, key);
233 * simple bin_search frontend that does the right thing for
236 static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
238 if (btrfs_is_leaf(c)) {
239 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
240 return generic_bin_search((void *)l->items,
241 sizeof(struct btrfs_item),
242 key, btrfs_header_nritems(&c->header),
245 return generic_bin_search((void *)c->ptrs,
246 sizeof(struct btrfs_key_ptr),
247 key, btrfs_header_nritems(&c->header),
253 static struct btrfs_buffer *read_node_slot(struct btrfs_root *root,
254 struct btrfs_buffer *parent_buf,
257 struct btrfs_node *node = &parent_buf->node;
260 if (slot >= btrfs_header_nritems(&node->header))
262 return read_tree_block(root, btrfs_node_blockptr(node, slot));
265 static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
266 *root, struct btrfs_path *path, int level)
268 struct btrfs_buffer *right_buf;
269 struct btrfs_buffer *mid_buf;
270 struct btrfs_buffer *left_buf;
271 struct btrfs_buffer *parent_buf = NULL;
272 struct btrfs_node *right = NULL;
273 struct btrfs_node *mid;
274 struct btrfs_node *left = NULL;
275 struct btrfs_node *parent = NULL;
279 int orig_slot = path->slots[level];
285 mid_buf = path->nodes[level];
286 mid = &mid_buf->node;
287 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
289 if (level < BTRFS_MAX_LEVEL - 1)
290 parent_buf = path->nodes[level + 1];
291 pslot = path->slots[level + 1];
294 * deal with the case where there is only one pointer in the root
295 * by promoting the node below to a root
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(trans, root, mid_buf);
314 return btrfs_free_extent(trans, root, blocknr, 1, 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(trans, root, left_buf, parent_buf, pslot - 1,
329 left = &left_buf->node;
330 orig_slot += btrfs_header_nritems(&left->header);
331 wret = push_node_left(trans, root, left_buf, mid_buf);
337 * then try to empty the right most buffer into the middle
340 btrfs_cow_block(trans, root, right_buf, parent_buf, pslot + 1,
342 right = &right_buf->node;
343 wret = push_node_left(trans, 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(trans, root, right_buf);
352 wret = del_ptr(trans, root, path, level + 1, pslot +
356 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
360 memcpy(&parent->ptrs[pslot + 1].key,
362 sizeof(struct btrfs_disk_key));
363 BUG_ON(list_empty(&parent_buf->dirty));
366 if (btrfs_header_nritems(&mid->header) == 1) {
368 * we're not allowed to leave a node with one item in the
369 * tree during a delete. A deletion from lower in the tree
370 * could try to delete the only pointer in this node.
371 * So, pull some keys from the left.
372 * There has to be a left pointer at this point because
373 * otherwise we would have pulled some pointers from the
377 wret = balance_node_right(trans, root, mid_buf, left_buf);
382 if (btrfs_header_nritems(&mid->header) == 0) {
383 /* we've managed to empty the middle node, drop it */
384 u64 blocknr = mid_buf->blocknr;
385 btrfs_block_release(root, mid_buf);
386 clean_tree_block(trans, root, mid_buf);
389 wret = del_ptr(trans, root, path, level + 1, pslot);
392 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
396 /* update the parent key to reflect our changes */
397 memcpy(&parent->ptrs[pslot].key, &mid->ptrs[0].key,
398 sizeof(struct btrfs_disk_key));
399 BUG_ON(list_empty(&parent_buf->dirty));
402 /* update the path */
404 if (btrfs_header_nritems(&left->header) > orig_slot) {
405 left_buf->count++; // released below
406 path->nodes[level] = left_buf;
407 path->slots[level + 1] -= 1;
408 path->slots[level] = orig_slot;
410 btrfs_block_release(root, mid_buf);
412 orig_slot -= btrfs_header_nritems(&left->header);
413 path->slots[level] = orig_slot;
416 /* double check we haven't messed things up */
417 check_block(root, path, level);
418 if (orig_ptr != btrfs_node_blockptr(&path->nodes[level]->node,
423 btrfs_block_release(root, right_buf);
425 btrfs_block_release(root, left_buf);
430 * look for key in the tree. path is filled in with nodes along the way
431 * if key is found, we return zero and you can find the item in the leaf
432 * level of the path (level 0)
434 * If the key isn't found, the path points to the slot where it should
435 * be inserted, and 1 is returned. If there are other errors during the
436 * search a negative error number is returned.
438 * if ins_len > 0, nodes and leaves will be split as we walk down the
439 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
442 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
443 *root, struct btrfs_key *key, struct btrfs_path *p, int
446 struct btrfs_buffer *b;
447 struct btrfs_buffer *cow_buf;
448 struct btrfs_node *c;
457 level = btrfs_header_level(&b->node.header);
460 wret = btrfs_cow_block(trans, root, b, p->nodes[level +
461 1], p->slots[level + 1],
465 BUG_ON(!cow && ins_len);
468 ret = check_block(root, p, level);
471 ret = bin_search(c, key, &slot);
472 if (!btrfs_is_leaf(c)) {
475 p->slots[level] = slot;
476 if (ins_len > 0 && btrfs_header_nritems(&c->header) ==
477 BTRFS_NODEPTRS_PER_BLOCK(root)) {
478 int sret = split_node(trans, root, p, level);
484 slot = p->slots[level];
485 } else if (ins_len < 0) {
486 int sret = balance_level(trans, root, p,
494 slot = p->slots[level];
495 BUG_ON(btrfs_header_nritems(&c->header) == 1);
497 b = read_tree_block(root, btrfs_node_blockptr(c, slot));
499 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
500 p->slots[level] = slot;
501 if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
502 sizeof(struct btrfs_item) + ins_len) {
503 int sret = split_leaf(trans, root, p, ins_len);
508 BUG_ON(root->node->count == 1);
512 BUG_ON(root->node->count == 1);
517 * adjust the pointers going up the tree, starting at level
518 * making sure the right key of each node is points to 'key'.
519 * This is used after shifting pointers to the left, so it stops
520 * fixing up pointers when a given leaf/node is not in slot 0 of the
523 * If this fails to write a tree block, it returns -1, but continues
524 * fixing up the blocks in ram so the tree is consistent.
526 static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root
527 *root, struct btrfs_path *path, struct btrfs_disk_key
532 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
533 struct btrfs_node *t;
534 int tslot = path->slots[i];
537 t = &path->nodes[i]->node;
538 memcpy(&t->ptrs[tslot].key, key, sizeof(*key));
539 BUG_ON(list_empty(&path->nodes[i]->dirty));
547 * try to push data from one node into the next node left in the
550 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
551 * error, and > 0 if there was no room in the left hand block.
553 static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
554 *root, struct btrfs_buffer *dst_buf, struct
555 btrfs_buffer *src_buf)
557 struct btrfs_node *src = &src_buf->node;
558 struct btrfs_node *dst = &dst_buf->node;
564 src_nritems = btrfs_header_nritems(&src->header);
565 dst_nritems = btrfs_header_nritems(&dst->header);
566 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
567 if (push_items <= 0) {
571 if (src_nritems < push_items)
572 push_items = src_nritems;
574 memcpy(dst->ptrs + dst_nritems, src->ptrs,
575 push_items * sizeof(struct btrfs_key_ptr));
576 if (push_items < src_nritems) {
577 memmove(src->ptrs, src->ptrs + push_items,
578 (src_nritems - push_items) *
579 sizeof(struct btrfs_key_ptr));
581 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
582 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
583 BUG_ON(list_empty(&src_buf->dirty));
584 BUG_ON(list_empty(&dst_buf->dirty));
589 * try to push data from one node into the next node right in the
592 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
593 * error, and > 0 if there was no room in the right hand block.
595 * this will only push up to 1/2 the contents of the left node over
597 static int balance_node_right(struct btrfs_trans_handle *trans, struct
598 btrfs_root *root, struct btrfs_buffer *dst_buf,
599 struct btrfs_buffer *src_buf)
601 struct btrfs_node *src = &src_buf->node;
602 struct btrfs_node *dst = &dst_buf->node;
609 src_nritems = btrfs_header_nritems(&src->header);
610 dst_nritems = btrfs_header_nritems(&dst->header);
611 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
612 if (push_items <= 0) {
616 max_push = src_nritems / 2 + 1;
617 /* don't try to empty the node */
618 if (max_push > src_nritems)
620 if (max_push < push_items)
621 push_items = max_push;
623 memmove(dst->ptrs + push_items, dst->ptrs,
624 dst_nritems * sizeof(struct btrfs_key_ptr));
625 memcpy(dst->ptrs, src->ptrs + src_nritems - push_items,
626 push_items * sizeof(struct btrfs_key_ptr));
628 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
629 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
631 BUG_ON(list_empty(&src_buf->dirty));
632 BUG_ON(list_empty(&dst_buf->dirty));
637 * helper function to insert a new root level in the tree.
638 * A new node is allocated, and a single item is inserted to
639 * point to the existing root
641 * returns zero on success or < 0 on failure.
643 static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root
644 *root, struct btrfs_path *path, int level)
646 struct btrfs_buffer *t;
647 struct btrfs_node *lower;
648 struct btrfs_node *c;
649 struct btrfs_disk_key *lower_key;
651 BUG_ON(path->nodes[level]);
652 BUG_ON(path->nodes[level-1] != root->node);
654 t = btrfs_alloc_free_block(trans, root);
656 memset(c, 0, root->blocksize);
657 btrfs_set_header_nritems(&c->header, 1);
658 btrfs_set_header_level(&c->header, level);
659 btrfs_set_header_blocknr(&c->header, t->blocknr);
660 btrfs_set_header_parentid(&c->header,
661 btrfs_header_parentid(&root->node->node.header));
662 lower = &path->nodes[level-1]->node;
663 if (btrfs_is_leaf(lower))
664 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
666 lower_key = &lower->ptrs[0].key;
667 memcpy(&c->ptrs[0].key, lower_key, sizeof(struct btrfs_disk_key));
668 btrfs_set_node_blockptr(c, 0, path->nodes[level - 1]->blocknr);
669 /* the super has an extra ref to root->node */
670 btrfs_block_release(root, root->node);
673 path->nodes[level] = t;
674 path->slots[level] = 0;
679 * worker function to insert a single pointer in a node.
680 * the node should have enough room for the pointer already
682 * slot and level indicate where you want the key to go, and
683 * blocknr is the block the key points to.
685 * returns zero on success and < 0 on any error
687 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
688 *root, struct btrfs_path *path, struct btrfs_disk_key
689 *key, u64 blocknr, int slot, int level)
691 struct btrfs_node *lower;
694 BUG_ON(!path->nodes[level]);
695 lower = &path->nodes[level]->node;
696 nritems = btrfs_header_nritems(&lower->header);
699 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
701 if (slot != nritems) {
702 memmove(lower->ptrs + slot + 1, lower->ptrs + slot,
703 (nritems - slot) * sizeof(struct btrfs_key_ptr));
705 memcpy(&lower->ptrs[slot].key, key, sizeof(struct btrfs_disk_key));
706 btrfs_set_node_blockptr(lower, slot, blocknr);
707 btrfs_set_header_nritems(&lower->header, nritems + 1);
708 BUG_ON(list_empty(&path->nodes[level]->dirty));
713 * split the node at the specified level in path in two.
714 * The path is corrected to point to the appropriate node after the split
716 * Before splitting this tries to make some room in the node by pushing
717 * left and right, if either one works, it returns right away.
719 * returns 0 on success and < 0 on failure
721 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
722 *root, struct btrfs_path *path, int level)
724 struct btrfs_buffer *t;
725 struct btrfs_node *c;
726 struct btrfs_buffer *split_buffer;
727 struct btrfs_node *split;
733 t = path->nodes[level];
735 if (t == root->node) {
736 /* trying to split the root, lets make a new one */
737 ret = insert_new_root(trans, root, path, level + 1);
741 c_nritems = btrfs_header_nritems(&c->header);
742 split_buffer = btrfs_alloc_free_block(trans, root);
743 split = &split_buffer->node;
744 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
745 btrfs_set_header_blocknr(&split->header, split_buffer->blocknr);
746 btrfs_set_header_parentid(&split->header,
747 btrfs_header_parentid(&root->node->node.header));
748 mid = (c_nritems + 1) / 2;
749 memcpy(split->ptrs, c->ptrs + mid,
750 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
751 btrfs_set_header_nritems(&split->header, c_nritems - mid);
752 btrfs_set_header_nritems(&c->header, mid);
755 BUG_ON(list_empty(&t->dirty));
756 wret = insert_ptr(trans, root, path, &split->ptrs[0].key,
757 split_buffer->blocknr, path->slots[level + 1] + 1,
762 if (path->slots[level] >= mid) {
763 path->slots[level] -= mid;
764 btrfs_block_release(root, t);
765 path->nodes[level] = split_buffer;
766 path->slots[level + 1] += 1;
768 btrfs_block_release(root, split_buffer);
774 * how many bytes are required to store the items in a leaf. start
775 * and nr indicate which items in the leaf to check. This totals up the
776 * space used both by the item structs and the item data
778 static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
781 int end = start + nr - 1;
785 data_len = btrfs_item_end(l->items + start);
786 data_len = data_len - btrfs_item_offset(l->items + end);
787 data_len += sizeof(struct btrfs_item) * nr;
792 * push some data in the path leaf to the right, trying to free up at
793 * least data_size bytes. returns zero if the push worked, nonzero otherwise
795 * returns 1 if the push failed because the other node didn't have enough
796 * room, 0 if everything worked out and < 0 if there were major errors.
798 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
799 *root, struct btrfs_path *path, int data_size)
801 struct btrfs_buffer *left_buf = path->nodes[0];
802 struct btrfs_leaf *left = &left_buf->leaf;
803 struct btrfs_leaf *right;
804 struct btrfs_buffer *right_buf;
805 struct btrfs_buffer *upper;
811 struct btrfs_item *item;
815 slot = path->slots[1];
816 if (!path->nodes[1]) {
819 upper = path->nodes[1];
820 if (slot >= btrfs_header_nritems(&upper->node.header) - 1) {
823 right_buf = read_tree_block(root, btrfs_node_blockptr(&upper->node,
825 right = &right_buf->leaf;
826 free_space = btrfs_leaf_free_space(root, right);
827 if (free_space < data_size + sizeof(struct btrfs_item)) {
828 btrfs_block_release(root, right_buf);
831 /* cow and double check */
832 btrfs_cow_block(trans, root, right_buf, upper, slot + 1, &right_buf);
833 right = &right_buf->leaf;
834 free_space = btrfs_leaf_free_space(root, right);
835 if (free_space < data_size + sizeof(struct btrfs_item)) {
836 btrfs_block_release(root, right_buf);
840 left_nritems = btrfs_header_nritems(&left->header);
841 for (i = left_nritems - 1; i >= 0; i--) {
842 item = left->items + i;
843 if (path->slots[0] == i)
844 push_space += data_size + sizeof(*item);
845 if (btrfs_item_size(item) + sizeof(*item) + push_space >
849 push_space += btrfs_item_size(item) + sizeof(*item);
851 if (push_items == 0) {
852 btrfs_block_release(root, right_buf);
855 right_nritems = btrfs_header_nritems(&right->header);
856 /* push left to right */
857 push_space = btrfs_item_end(left->items + left_nritems - push_items);
858 push_space -= leaf_data_end(root, left);
859 /* make room in the right data area */
860 memmove(btrfs_leaf_data(right) + leaf_data_end(root, right) -
861 push_space, btrfs_leaf_data(right) + leaf_data_end(root, right),
862 BTRFS_LEAF_DATA_SIZE(root) - leaf_data_end(root, right));
863 /* copy from the left data area */
864 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - push_space,
865 btrfs_leaf_data(left) + leaf_data_end(root, left), push_space);
866 memmove(right->items + push_items, right->items,
867 right_nritems * sizeof(struct btrfs_item));
868 /* copy the items from left to right */
869 memcpy(right->items, left->items + left_nritems - push_items,
870 push_items * sizeof(struct btrfs_item));
872 /* update the item pointers */
873 right_nritems += push_items;
874 btrfs_set_header_nritems(&right->header, right_nritems);
875 push_space = BTRFS_LEAF_DATA_SIZE(root);
876 for (i = 0; i < right_nritems; i++) {
877 btrfs_set_item_offset(right->items + i, push_space -
878 btrfs_item_size(right->items + i));
879 push_space = btrfs_item_offset(right->items + i);
881 left_nritems -= push_items;
882 btrfs_set_header_nritems(&left->header, left_nritems);
884 BUG_ON(list_empty(&left_buf->dirty));
885 BUG_ON(list_empty(&right_buf->dirty));
886 memcpy(&upper->node.ptrs[slot + 1].key,
887 &right->items[0].key, sizeof(struct btrfs_disk_key));
888 BUG_ON(list_empty(&upper->dirty));
890 /* then fixup the leaf pointer in the path */
891 if (path->slots[0] >= left_nritems) {
892 path->slots[0] -= left_nritems;
893 btrfs_block_release(root, path->nodes[0]);
894 path->nodes[0] = right_buf;
897 btrfs_block_release(root, right_buf);
902 * push some data in the path leaf to the left, trying to free up at
903 * least data_size bytes. returns zero if the push worked, nonzero otherwise
905 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
906 *root, struct btrfs_path *path, int data_size)
908 struct btrfs_buffer *right_buf = path->nodes[0];
909 struct btrfs_leaf *right = &right_buf->leaf;
910 struct btrfs_buffer *t;
911 struct btrfs_leaf *left;
917 struct btrfs_item *item;
918 u32 old_left_nritems;
922 slot = path->slots[1];
926 if (!path->nodes[1]) {
929 t = read_tree_block(root, btrfs_node_blockptr(&path->nodes[1]->node,
932 free_space = btrfs_leaf_free_space(root, left);
933 if (free_space < data_size + sizeof(struct btrfs_item)) {
934 btrfs_block_release(root, t);
938 /* cow and double check */
939 btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t);
941 free_space = btrfs_leaf_free_space(root, left);
942 if (free_space < data_size + sizeof(struct btrfs_item)) {
943 btrfs_block_release(root, t);
947 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
948 item = right->items + i;
949 if (path->slots[0] == i)
950 push_space += data_size + sizeof(*item);
951 if (btrfs_item_size(item) + sizeof(*item) + push_space >
955 push_space += btrfs_item_size(item) + sizeof(*item);
957 if (push_items == 0) {
958 btrfs_block_release(root, t);
961 /* push data from right to left */
962 memcpy(left->items + btrfs_header_nritems(&left->header),
963 right->items, push_items * sizeof(struct btrfs_item));
964 push_space = BTRFS_LEAF_DATA_SIZE(root) -
965 btrfs_item_offset(right->items + push_items -1);
966 memcpy(btrfs_leaf_data(left) + leaf_data_end(root, left) - push_space,
967 btrfs_leaf_data(right) +
968 btrfs_item_offset(right->items + push_items - 1),
970 old_left_nritems = btrfs_header_nritems(&left->header);
971 BUG_ON(old_left_nritems < 0);
973 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
974 u32 ioff = btrfs_item_offset(left->items + i);
975 btrfs_set_item_offset(left->items + i, ioff -
976 (BTRFS_LEAF_DATA_SIZE(root) -
977 btrfs_item_offset(left->items +
978 old_left_nritems - 1)));
980 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
982 /* fixup right node */
983 push_space = btrfs_item_offset(right->items + push_items - 1) -
984 leaf_data_end(root, right);
985 memmove(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
986 push_space, btrfs_leaf_data(right) +
987 leaf_data_end(root, right), push_space);
988 memmove(right->items, right->items + push_items,
989 (btrfs_header_nritems(&right->header) - push_items) *
990 sizeof(struct btrfs_item));
991 btrfs_set_header_nritems(&right->header,
992 btrfs_header_nritems(&right->header) -
994 push_space = BTRFS_LEAF_DATA_SIZE(root);
996 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
997 btrfs_set_item_offset(right->items + i, push_space -
998 btrfs_item_size(right->items + i));
999 push_space = btrfs_item_offset(right->items + i);
1002 BUG_ON(list_empty(&t->dirty));
1003 BUG_ON(list_empty(&right_buf->dirty));
1005 wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1);
1009 /* then fixup the leaf pointer in the path */
1010 if (path->slots[0] < push_items) {
1011 path->slots[0] += old_left_nritems;
1012 btrfs_block_release(root, path->nodes[0]);
1014 path->slots[1] -= 1;
1016 btrfs_block_release(root, t);
1017 path->slots[0] -= push_items;
1019 BUG_ON(path->slots[0] < 0);
1024 * split the path's leaf in two, making sure there is at least data_size
1025 * available for the resulting leaf level of the path.
1027 * returns 0 if all went well and < 0 on failure.
1029 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
1030 *root, struct btrfs_path *path, int data_size)
1032 struct btrfs_buffer *l_buf;
1033 struct btrfs_leaf *l;
1037 struct btrfs_leaf *right;
1038 struct btrfs_buffer *right_buffer;
1039 int space_needed = data_size + sizeof(struct btrfs_item);
1046 /* first try to make some room by pushing left and right */
1047 wret = push_leaf_left(trans, root, path, data_size);
1051 wret = push_leaf_right(trans, root, path, data_size);
1055 l_buf = path->nodes[0];
1058 /* did the pushes work? */
1059 if (btrfs_leaf_free_space(root, l) >=
1060 sizeof(struct btrfs_item) + data_size)
1063 if (!path->nodes[1]) {
1064 ret = insert_new_root(trans, root, path, 1);
1068 slot = path->slots[0];
1069 nritems = btrfs_header_nritems(&l->header);
1070 mid = (nritems + 1)/ 2;
1071 right_buffer = btrfs_alloc_free_block(trans, root);
1072 BUG_ON(!right_buffer);
1073 BUG_ON(mid == nritems);
1074 right = &right_buffer->leaf;
1075 memset(&right->header, 0, sizeof(right->header));
1077 /* FIXME, just alloc a new leaf here */
1078 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
1079 BTRFS_LEAF_DATA_SIZE(root))
1082 /* FIXME, just alloc a new leaf here */
1083 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1084 BTRFS_LEAF_DATA_SIZE(root))
1087 btrfs_set_header_nritems(&right->header, nritems - mid);
1088 btrfs_set_header_blocknr(&right->header, right_buffer->blocknr);
1089 btrfs_set_header_level(&right->header, 0);
1090 btrfs_set_header_parentid(&right->header,
1091 btrfs_header_parentid(&root->node->node.header));
1092 data_copy_size = btrfs_item_end(l->items + mid) -
1093 leaf_data_end(root, l);
1094 memcpy(right->items, l->items + mid,
1095 (nritems - mid) * sizeof(struct btrfs_item));
1096 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1097 data_copy_size, btrfs_leaf_data(l) +
1098 leaf_data_end(root, l), data_copy_size);
1099 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1100 btrfs_item_end(l->items + mid);
1102 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
1103 u32 ioff = btrfs_item_offset(right->items + i);
1104 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1107 btrfs_set_header_nritems(&l->header, mid);
1109 wret = insert_ptr(trans, root, path, &right->items[0].key,
1110 right_buffer->blocknr, path->slots[1] + 1, 1);
1113 BUG_ON(list_empty(&right_buffer->dirty));
1114 BUG_ON(list_empty(&l_buf->dirty));
1115 BUG_ON(path->slots[0] != slot);
1117 btrfs_block_release(root, path->nodes[0]);
1118 path->nodes[0] = right_buffer;
1119 path->slots[0] -= mid;
1120 path->slots[1] += 1;
1122 btrfs_block_release(root, right_buffer);
1123 BUG_ON(path->slots[0] < 0);
1128 * Given a key and some data, insert an item into the tree.
1129 * This does all the path init required, making room in the tree if needed.
1131 int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1132 *root, struct btrfs_path *path, struct btrfs_key
1133 *cpu_key, u32 data_size)
1138 struct btrfs_leaf *leaf;
1139 struct btrfs_buffer *leaf_buf;
1141 unsigned int data_end;
1142 struct btrfs_disk_key disk_key;
1144 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
1146 /* create a root if there isn't one */
1149 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
1151 btrfs_release_path(root, path);
1157 slot_orig = path->slots[0];
1158 leaf_buf = path->nodes[0];
1159 leaf = &leaf_buf->leaf;
1161 nritems = btrfs_header_nritems(&leaf->header);
1162 data_end = leaf_data_end(root, leaf);
1164 if (btrfs_leaf_free_space(root, leaf) <
1165 sizeof(struct btrfs_item) + data_size)
1168 slot = path->slots[0];
1170 if (slot != nritems) {
1172 unsigned int old_data = btrfs_item_end(leaf->items + slot);
1175 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1177 /* first correct the data pointers */
1178 for (i = slot; i < nritems; i++) {
1179 u32 ioff = btrfs_item_offset(leaf->items + i);
1180 btrfs_set_item_offset(leaf->items + i,
1184 /* shift the items */
1185 memmove(leaf->items + slot + 1, leaf->items + slot,
1186 (nritems - slot) * sizeof(struct btrfs_item));
1188 /* shift the data */
1189 memmove(btrfs_leaf_data(leaf) + data_end - data_size,
1190 btrfs_leaf_data(leaf) +
1191 data_end, old_data - data_end);
1192 data_end = old_data;
1194 /* setup the item for the new data */
1195 memcpy(&leaf->items[slot].key, &disk_key,
1196 sizeof(struct btrfs_disk_key));
1197 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1198 btrfs_set_item_size(leaf->items + slot, data_size);
1199 btrfs_set_header_nritems(&leaf->header, nritems + 1);
1203 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
1205 BUG_ON(list_empty(&leaf_buf->dirty));
1206 if (btrfs_leaf_free_space(root, leaf) < 0)
1208 check_leaf(root, path, 0);
1214 * Given a key and some data, insert an item into the tree.
1215 * This does all the path init required, making room in the tree if needed.
1217 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1218 *root, struct btrfs_key *cpu_key, void *data, u32
1222 struct btrfs_path path;
1225 btrfs_init_path(&path);
1226 ret = btrfs_insert_empty_item(trans, root, &path, cpu_key, data_size);
1228 ptr = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0], u8);
1229 memcpy(ptr, data, data_size);
1231 btrfs_release_path(root, &path);
1236 * delete the pointer from a given node.
1238 * If the delete empties a node, the node is removed from the tree,
1239 * continuing all the way the root if required. The root is converted into
1240 * a leaf if all the nodes are emptied.
1242 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1243 struct btrfs_path *path, int level, int slot)
1245 struct btrfs_node *node;
1246 struct btrfs_buffer *parent = path->nodes[level];
1251 node = &parent->node;
1252 nritems = btrfs_header_nritems(&node->header);
1253 if (slot != nritems -1) {
1254 memmove(node->ptrs + slot, node->ptrs + slot + 1,
1255 sizeof(struct btrfs_key_ptr) * (nritems - slot - 1));
1258 btrfs_set_header_nritems(&node->header, nritems);
1259 if (nritems == 0 && parent == root->node) {
1260 BUG_ON(btrfs_header_level(&root->node->node.header) != 1);
1261 /* just turn the root into a leaf and break */
1262 btrfs_set_header_level(&root->node->node.header, 0);
1263 } else if (slot == 0) {
1264 wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key,
1269 BUG_ON(list_empty(&parent->dirty));
1274 * delete the item at the leaf level in path. If that empties
1275 * the leaf, remove it from the tree
1277 int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1278 struct btrfs_path *path)
1281 struct btrfs_leaf *leaf;
1282 struct btrfs_buffer *leaf_buf;
1289 leaf_buf = path->nodes[0];
1290 leaf = &leaf_buf->leaf;
1291 slot = path->slots[0];
1292 doff = btrfs_item_offset(leaf->items + slot);
1293 dsize = btrfs_item_size(leaf->items + slot);
1294 nritems = btrfs_header_nritems(&leaf->header);
1296 if (slot != nritems - 1) {
1298 int data_end = leaf_data_end(root, leaf);
1299 memmove(btrfs_leaf_data(leaf) + data_end + dsize,
1300 btrfs_leaf_data(leaf) + data_end,
1302 for (i = slot + 1; i < nritems; i++) {
1303 u32 ioff = btrfs_item_offset(leaf->items + i);
1304 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
1306 memmove(leaf->items + slot, leaf->items + slot + 1,
1307 sizeof(struct btrfs_item) *
1308 (nritems - slot - 1));
1310 btrfs_set_header_nritems(&leaf->header, nritems - 1);
1312 /* delete the leaf if we've emptied it */
1314 if (leaf_buf == root->node) {
1315 btrfs_set_header_level(&leaf->header, 0);
1316 BUG_ON(list_empty(&leaf_buf->dirty));
1318 clean_tree_block(trans, root, leaf_buf);
1319 wret = del_ptr(trans, root, path, 1, path->slots[1]);
1322 wret = btrfs_free_extent(trans, root,
1323 leaf_buf->blocknr, 1, 1);
1328 int used = leaf_space_used(leaf, 0, nritems);
1330 wret = fixup_low_keys(trans, root, path,
1331 &leaf->items[0].key, 1);
1335 BUG_ON(list_empty(&leaf_buf->dirty));
1337 /* delete the leaf if it is mostly empty */
1338 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
1339 /* push_leaf_left fixes the path.
1340 * make sure the path still points to our leaf
1341 * for possible call to del_ptr below
1343 slot = path->slots[1];
1345 wret = push_leaf_left(trans, root, path, 1);
1348 if (path->nodes[0] == leaf_buf &&
1349 btrfs_header_nritems(&leaf->header)) {
1350 wret = push_leaf_right(trans, root, path, 1);
1354 if (btrfs_header_nritems(&leaf->header) == 0) {
1355 u64 blocknr = leaf_buf->blocknr;
1356 clean_tree_block(trans, root, leaf_buf);
1357 wret = del_ptr(trans, root, path, 1, slot);
1360 btrfs_block_release(root, leaf_buf);
1361 wret = btrfs_free_extent(trans, root, blocknr,
1366 btrfs_block_release(root, leaf_buf);
1374 * walk up the tree as far as required to find the next leaf.
1375 * returns 0 if it found something or 1 if there are no greater leaves.
1376 * returns < 0 on io errors.
1378 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
1383 struct btrfs_buffer *c;
1384 struct btrfs_buffer *next = NULL;
1386 while(level < BTRFS_MAX_LEVEL) {
1387 if (!path->nodes[level])
1389 slot = path->slots[level] + 1;
1390 c = path->nodes[level];
1391 if (slot >= btrfs_header_nritems(&c->node.header)) {
1395 blocknr = btrfs_node_blockptr(&c->node, slot);
1397 btrfs_block_release(root, next);
1398 next = read_tree_block(root, blocknr);
1401 path->slots[level] = slot;
1404 c = path->nodes[level];
1405 btrfs_block_release(root, c);
1406 path->nodes[level] = next;
1407 path->slots[level] = 0;
1410 next = read_tree_block(root,
1411 btrfs_node_blockptr(&next->node, 0));