3 #include "kerncompat.h"
4 #include "radix-tree.h"
7 #include "print-tree.h"
9 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
10 *root, struct btrfs_path *path, int level);
11 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
12 *root, struct btrfs_path *path, int data_size);
13 static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
14 *root, struct btrfs_buffer *dst, struct btrfs_buffer
16 static int balance_node_right(struct btrfs_trans_handle *trans, struct
17 btrfs_root *root, struct btrfs_buffer *dst_buf,
18 struct btrfs_buffer *src_buf);
19 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
20 struct btrfs_path *path, int level, int slot);
22 inline void btrfs_init_path(struct btrfs_path *p)
24 memset(p, 0, sizeof(*p));
27 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
30 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
33 btrfs_block_release(root, p->nodes[i]);
35 memset(p, 0, sizeof(*p));
38 static int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
39 *root, struct btrfs_buffer *buf, struct btrfs_buffer
40 *parent, int parent_slot, struct btrfs_buffer
43 struct btrfs_buffer *cow;
45 if (!list_empty(&buf->dirty)) {
49 cow = btrfs_alloc_free_block(trans, root);
50 memcpy(&cow->node, &buf->node, root->blocksize);
51 btrfs_set_header_blocknr(&cow->node.header, cow->blocknr);
53 btrfs_inc_ref(trans, root, buf);
54 if (buf == root->node) {
57 if (buf != root->commit_root)
58 btrfs_free_extent(trans, root, buf->blocknr, 1, 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(trans, root, buf->blocknr, 1, 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_trans_handle *trans, struct btrfs_root
270 *root, struct btrfs_path *path, int level)
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 * deal with the case where there is only one pointer in the root
299 * by promoting the node below to a root
302 struct btrfs_buffer *child;
303 u64 blocknr = mid_buf->blocknr;
305 if (btrfs_header_nritems(&mid->header) != 1)
308 /* promote the child to a root */
309 child = read_node_slot(root, mid_buf, 0);
312 path->nodes[level] = NULL;
313 /* once for the path */
314 btrfs_block_release(root, mid_buf);
315 /* once for the root ptr */
316 btrfs_block_release(root, mid_buf);
317 clean_tree_block(trans, root, mid_buf);
318 return btrfs_free_extent(trans, root, blocknr, 1, 1);
320 parent = &parent_buf->node;
322 if (btrfs_header_nritems(&mid->header) >
323 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
326 left_buf = read_node_slot(root, parent_buf, pslot - 1);
327 right_buf = read_node_slot(root, parent_buf, pslot + 1);
329 /* first, try to make some room in the middle buffer */
331 btrfs_cow_block(trans, root, left_buf, parent_buf, pslot - 1,
333 left = &left_buf->node;
334 orig_slot += btrfs_header_nritems(&left->header);
335 wret = push_node_left(trans, root, left_buf, mid_buf);
341 * then try to empty the right most buffer into the middle
344 btrfs_cow_block(trans, root, right_buf, parent_buf, pslot + 1,
346 right = &right_buf->node;
347 wret = push_node_left(trans, root, mid_buf, right_buf);
350 if (btrfs_header_nritems(&right->header) == 0) {
351 u64 blocknr = right_buf->blocknr;
352 btrfs_block_release(root, right_buf);
353 clean_tree_block(trans, root, right_buf);
356 wret = del_ptr(trans, root, path, level + 1, pslot +
360 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
364 memcpy(&parent->ptrs[pslot + 1].key,
366 sizeof(struct btrfs_disk_key));
367 BUG_ON(list_empty(&parent_buf->dirty));
370 if (btrfs_header_nritems(&mid->header) == 1) {
372 * we're not allowed to leave a node with one item in the
373 * tree during a delete. A deletion from lower in the tree
374 * could try to delete the only pointer in this node.
375 * So, pull some keys from the left.
376 * There has to be a left pointer at this point because
377 * otherwise we would have pulled some pointers from the
381 wret = balance_node_right(trans, root, mid_buf, left_buf);
386 if (btrfs_header_nritems(&mid->header) == 0) {
387 /* we've managed to empty the middle node, drop it */
388 u64 blocknr = mid_buf->blocknr;
389 btrfs_block_release(root, mid_buf);
390 clean_tree_block(trans, root, mid_buf);
393 wret = del_ptr(trans, root, path, level + 1, pslot);
396 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
400 /* update the parent key to reflect our changes */
401 memcpy(&parent->ptrs[pslot].key, &mid->ptrs[0].key,
402 sizeof(struct btrfs_disk_key));
403 BUG_ON(list_empty(&parent_buf->dirty));
406 /* update the path */
408 if (btrfs_header_nritems(&left->header) > orig_slot) {
409 left_buf->count++; // released below
410 path->nodes[level] = left_buf;
411 path->slots[level + 1] -= 1;
412 path->slots[level] = orig_slot;
414 btrfs_block_release(root, mid_buf);
416 orig_slot -= btrfs_header_nritems(&left->header);
417 path->slots[level] = orig_slot;
420 /* double check we haven't messed things up */
421 check_block(root, path, level);
422 if (orig_ptr != btrfs_node_blockptr(&path->nodes[level]->node,
427 btrfs_block_release(root, right_buf);
429 btrfs_block_release(root, left_buf);
434 * look for key in the tree. path is filled in with nodes along the way
435 * if key is found, we return zero and you can find the item in the leaf
436 * level of the path (level 0)
438 * If the key isn't found, the path points to the slot where it should
439 * be inserted, and 1 is returned. If there are other errors during the
440 * search a negative error number is returned.
442 * if ins_len > 0, nodes and leaves will be split as we walk down the
443 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
446 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
447 *root, struct btrfs_key *key, struct btrfs_path *p, int
450 struct btrfs_buffer *b;
451 struct btrfs_buffer *cow_buf;
452 struct btrfs_node *c;
461 level = btrfs_header_level(&b->node.header);
464 wret = btrfs_cow_block(trans, root, b, p->nodes[level +
465 1], p->slots[level + 1],
469 BUG_ON(!cow && ins_len);
472 ret = check_block(root, p, level);
475 ret = bin_search(c, key, &slot);
476 if (!btrfs_is_leaf(c)) {
479 p->slots[level] = slot;
480 if (ins_len > 0 && btrfs_header_nritems(&c->header) ==
481 BTRFS_NODEPTRS_PER_BLOCK(root)) {
482 int sret = split_node(trans, root, p, level);
488 slot = p->slots[level];
489 } else if (ins_len < 0) {
490 int sret = balance_level(trans, root, p,
498 slot = p->slots[level];
499 BUG_ON(btrfs_header_nritems(&c->header) == 1);
501 b = read_tree_block(root, btrfs_node_blockptr(c, slot));
503 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
504 p->slots[level] = slot;
505 if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
506 sizeof(struct btrfs_item) + ins_len) {
507 int sret = split_leaf(trans, root, p, ins_len);
512 BUG_ON(root->node->count == 1);
516 BUG_ON(root->node->count == 1);
521 * adjust the pointers going up the tree, starting at level
522 * making sure the right key of each node is points to 'key'.
523 * This is used after shifting pointers to the left, so it stops
524 * fixing up pointers when a given leaf/node is not in slot 0 of the
527 * If this fails to write a tree block, it returns -1, but continues
528 * fixing up the blocks in ram so the tree is consistent.
530 static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root
531 *root, struct btrfs_path *path, struct btrfs_disk_key
536 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
537 struct btrfs_node *t;
538 int tslot = path->slots[i];
541 t = &path->nodes[i]->node;
542 memcpy(&t->ptrs[tslot].key, key, sizeof(*key));
543 BUG_ON(list_empty(&path->nodes[i]->dirty));
551 * try to push data from one node into the next node left in the
554 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
555 * error, and > 0 if there was no room in the left hand block.
557 static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
558 *root, struct btrfs_buffer *dst_buf, struct
559 btrfs_buffer *src_buf)
561 struct btrfs_node *src = &src_buf->node;
562 struct btrfs_node *dst = &dst_buf->node;
568 src_nritems = btrfs_header_nritems(&src->header);
569 dst_nritems = btrfs_header_nritems(&dst->header);
570 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
571 if (push_items <= 0) {
575 if (src_nritems < push_items)
576 push_items = src_nritems;
578 memcpy(dst->ptrs + dst_nritems, src->ptrs,
579 push_items * sizeof(struct btrfs_key_ptr));
580 if (push_items < src_nritems) {
581 memmove(src->ptrs, src->ptrs + push_items,
582 (src_nritems - push_items) *
583 sizeof(struct btrfs_key_ptr));
585 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
586 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
587 BUG_ON(list_empty(&src_buf->dirty));
588 BUG_ON(list_empty(&dst_buf->dirty));
593 * try to push data from one node into the next node right in the
596 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
597 * error, and > 0 if there was no room in the right hand block.
599 * this will only push up to 1/2 the contents of the left node over
601 static int balance_node_right(struct btrfs_trans_handle *trans, struct
602 btrfs_root *root, struct btrfs_buffer *dst_buf,
603 struct btrfs_buffer *src_buf)
605 struct btrfs_node *src = &src_buf->node;
606 struct btrfs_node *dst = &dst_buf->node;
613 src_nritems = btrfs_header_nritems(&src->header);
614 dst_nritems = btrfs_header_nritems(&dst->header);
615 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
616 if (push_items <= 0) {
620 max_push = src_nritems / 2 + 1;
621 /* don't try to empty the node */
622 if (max_push > src_nritems)
624 if (max_push < push_items)
625 push_items = max_push;
627 memmove(dst->ptrs + push_items, dst->ptrs,
628 dst_nritems * sizeof(struct btrfs_key_ptr));
629 memcpy(dst->ptrs, src->ptrs + src_nritems - push_items,
630 push_items * sizeof(struct btrfs_key_ptr));
632 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
633 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
635 BUG_ON(list_empty(&src_buf->dirty));
636 BUG_ON(list_empty(&dst_buf->dirty));
641 * helper function to insert a new root level in the tree.
642 * A new node is allocated, and a single item is inserted to
643 * point to the existing root
645 * returns zero on success or < 0 on failure.
647 static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root
648 *root, struct btrfs_path *path, int level)
650 struct btrfs_buffer *t;
651 struct btrfs_node *lower;
652 struct btrfs_node *c;
653 struct btrfs_disk_key *lower_key;
655 BUG_ON(path->nodes[level]);
656 BUG_ON(path->nodes[level-1] != root->node);
658 t = btrfs_alloc_free_block(trans, root);
660 memset(c, 0, root->blocksize);
661 btrfs_set_header_nritems(&c->header, 1);
662 btrfs_set_header_level(&c->header, level);
663 btrfs_set_header_blocknr(&c->header, t->blocknr);
664 lower = &path->nodes[level-1]->node;
665 if (btrfs_is_leaf(lower))
666 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
668 lower_key = &lower->ptrs[0].key;
669 memcpy(&c->ptrs[0].key, lower_key, sizeof(struct btrfs_disk_key));
670 btrfs_set_node_blockptr(c, 0, path->nodes[level - 1]->blocknr);
671 /* the super has an extra ref to root->node */
672 btrfs_block_release(root, root->node);
675 path->nodes[level] = t;
676 path->slots[level] = 0;
681 * worker function to insert a single pointer in a node.
682 * the node should have enough room for the pointer already
684 * slot and level indicate where you want the key to go, and
685 * blocknr is the block the key points to.
687 * returns zero on success and < 0 on any error
689 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
690 *root, struct btrfs_path *path, struct btrfs_disk_key
691 *key, u64 blocknr, int slot, int level)
693 struct btrfs_node *lower;
696 BUG_ON(!path->nodes[level]);
697 lower = &path->nodes[level]->node;
698 nritems = btrfs_header_nritems(&lower->header);
701 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
703 if (slot != nritems) {
704 memmove(lower->ptrs + slot + 1, lower->ptrs + slot,
705 (nritems - slot) * sizeof(struct btrfs_key_ptr));
707 memcpy(&lower->ptrs[slot].key, key, sizeof(struct btrfs_disk_key));
708 btrfs_set_node_blockptr(lower, slot, blocknr);
709 btrfs_set_header_nritems(&lower->header, nritems + 1);
710 BUG_ON(list_empty(&path->nodes[level]->dirty));
715 * split the node at the specified level in path in two.
716 * The path is corrected to point to the appropriate node after the split
718 * Before splitting this tries to make some room in the node by pushing
719 * left and right, if either one works, it returns right away.
721 * returns 0 on success and < 0 on failure
723 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
724 *root, struct btrfs_path *path, int level)
726 struct btrfs_buffer *t;
727 struct btrfs_node *c;
728 struct btrfs_buffer *split_buffer;
729 struct btrfs_node *split;
735 t = path->nodes[level];
737 if (t == root->node) {
738 /* trying to split the root, lets make a new one */
739 ret = insert_new_root(trans, root, path, level + 1);
743 c_nritems = btrfs_header_nritems(&c->header);
744 split_buffer = btrfs_alloc_free_block(trans, root);
745 split = &split_buffer->node;
746 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
747 btrfs_set_header_level(&split->header, btrfs_header_level(&c->header));
748 btrfs_set_header_blocknr(&split->header, split_buffer->blocknr);
749 mid = (c_nritems + 1) / 2;
750 memcpy(split->ptrs, c->ptrs + mid,
751 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
752 btrfs_set_header_nritems(&split->header, c_nritems - mid);
753 btrfs_set_header_nritems(&c->header, mid);
756 BUG_ON(list_empty(&t->dirty));
757 wret = insert_ptr(trans, root, path, &split->ptrs[0].key,
758 split_buffer->blocknr, path->slots[level + 1] + 1,
763 if (path->slots[level] >= mid) {
764 path->slots[level] -= mid;
765 btrfs_block_release(root, t);
766 path->nodes[level] = split_buffer;
767 path->slots[level + 1] += 1;
769 btrfs_block_release(root, split_buffer);
775 * how many bytes are required to store the items in a leaf. start
776 * and nr indicate which items in the leaf to check. This totals up the
777 * space used both by the item structs and the item data
779 static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
782 int end = start + nr - 1;
786 data_len = btrfs_item_end(l->items + start);
787 data_len = data_len - btrfs_item_offset(l->items + end);
788 data_len += sizeof(struct btrfs_item) * nr;
793 * push some data in the path leaf to the right, trying to free up at
794 * least data_size bytes. returns zero if the push worked, nonzero otherwise
796 * returns 1 if the push failed because the other node didn't have enough
797 * room, 0 if everything worked out and < 0 if there were major errors.
799 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
800 *root, struct btrfs_path *path, int data_size)
802 struct btrfs_buffer *left_buf = path->nodes[0];
803 struct btrfs_leaf *left = &left_buf->leaf;
804 struct btrfs_leaf *right;
805 struct btrfs_buffer *right_buf;
806 struct btrfs_buffer *upper;
812 struct btrfs_item *item;
816 slot = path->slots[1];
817 if (!path->nodes[1]) {
820 upper = path->nodes[1];
821 if (slot >= btrfs_header_nritems(&upper->node.header) - 1) {
824 right_buf = read_tree_block(root, btrfs_node_blockptr(&upper->node,
826 right = &right_buf->leaf;
827 free_space = btrfs_leaf_free_space(root, right);
828 if (free_space < data_size + sizeof(struct btrfs_item)) {
829 btrfs_block_release(root, right_buf);
832 /* cow and double check */
833 btrfs_cow_block(trans, root, right_buf, upper, slot + 1, &right_buf);
834 right = &right_buf->leaf;
835 free_space = btrfs_leaf_free_space(root, right);
836 if (free_space < data_size + sizeof(struct btrfs_item)) {
837 btrfs_block_release(root, right_buf);
841 left_nritems = btrfs_header_nritems(&left->header);
842 for (i = left_nritems - 1; i >= 0; i--) {
843 item = left->items + i;
844 if (path->slots[0] == i)
845 push_space += data_size + sizeof(*item);
846 if (btrfs_item_size(item) + sizeof(*item) + push_space >
850 push_space += btrfs_item_size(item) + sizeof(*item);
852 if (push_items == 0) {
853 btrfs_block_release(root, right_buf);
856 right_nritems = btrfs_header_nritems(&right->header);
857 /* push left to right */
858 push_space = btrfs_item_end(left->items + left_nritems - push_items);
859 push_space -= leaf_data_end(root, left);
860 /* make room in the right data area */
861 memmove(btrfs_leaf_data(right) + leaf_data_end(root, right) -
862 push_space, btrfs_leaf_data(right) + leaf_data_end(root, right),
863 BTRFS_LEAF_DATA_SIZE(root) - leaf_data_end(root, right));
864 /* copy from the left data area */
865 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - push_space,
866 btrfs_leaf_data(left) + leaf_data_end(root, left), push_space);
867 memmove(right->items + push_items, right->items,
868 right_nritems * sizeof(struct btrfs_item));
869 /* copy the items from left to right */
870 memcpy(right->items, left->items + left_nritems - push_items,
871 push_items * sizeof(struct btrfs_item));
873 /* update the item pointers */
874 right_nritems += push_items;
875 btrfs_set_header_nritems(&right->header, right_nritems);
876 push_space = BTRFS_LEAF_DATA_SIZE(root);
877 for (i = 0; i < right_nritems; i++) {
878 btrfs_set_item_offset(right->items + i, push_space -
879 btrfs_item_size(right->items + i));
880 push_space = btrfs_item_offset(right->items + i);
882 left_nritems -= push_items;
883 btrfs_set_header_nritems(&left->header, left_nritems);
885 BUG_ON(list_empty(&left_buf->dirty));
886 BUG_ON(list_empty(&right_buf->dirty));
887 memcpy(&upper->node.ptrs[slot + 1].key,
888 &right->items[0].key, sizeof(struct btrfs_disk_key));
889 BUG_ON(list_empty(&upper->dirty));
891 /* then fixup the leaf pointer in the path */
892 if (path->slots[0] >= left_nritems) {
893 path->slots[0] -= left_nritems;
894 btrfs_block_release(root, path->nodes[0]);
895 path->nodes[0] = right_buf;
898 btrfs_block_release(root, right_buf);
903 * push some data in the path leaf to the left, trying to free up at
904 * least data_size bytes. returns zero if the push worked, nonzero otherwise
906 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
907 *root, struct btrfs_path *path, int data_size)
909 struct btrfs_buffer *right_buf = path->nodes[0];
910 struct btrfs_leaf *right = &right_buf->leaf;
911 struct btrfs_buffer *t;
912 struct btrfs_leaf *left;
918 struct btrfs_item *item;
919 u32 old_left_nritems;
923 slot = path->slots[1];
927 if (!path->nodes[1]) {
930 t = read_tree_block(root, btrfs_node_blockptr(&path->nodes[1]->node,
933 free_space = btrfs_leaf_free_space(root, left);
934 if (free_space < data_size + sizeof(struct btrfs_item)) {
935 btrfs_block_release(root, t);
939 /* cow and double check */
940 btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t);
942 free_space = btrfs_leaf_free_space(root, left);
943 if (free_space < data_size + sizeof(struct btrfs_item)) {
944 btrfs_block_release(root, t);
948 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
949 item = right->items + i;
950 if (path->slots[0] == i)
951 push_space += data_size + sizeof(*item);
952 if (btrfs_item_size(item) + sizeof(*item) + push_space >
956 push_space += btrfs_item_size(item) + sizeof(*item);
958 if (push_items == 0) {
959 btrfs_block_release(root, t);
962 /* push data from right to left */
963 memcpy(left->items + btrfs_header_nritems(&left->header),
964 right->items, push_items * sizeof(struct btrfs_item));
965 push_space = BTRFS_LEAF_DATA_SIZE(root) -
966 btrfs_item_offset(right->items + push_items -1);
967 memcpy(btrfs_leaf_data(left) + leaf_data_end(root, left) - push_space,
968 btrfs_leaf_data(right) +
969 btrfs_item_offset(right->items + push_items - 1),
971 old_left_nritems = btrfs_header_nritems(&left->header);
972 BUG_ON(old_left_nritems < 0);
974 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
975 u32 ioff = btrfs_item_offset(left->items + i);
976 btrfs_set_item_offset(left->items + i, ioff -
977 (BTRFS_LEAF_DATA_SIZE(root) -
978 btrfs_item_offset(left->items +
979 old_left_nritems - 1)));
981 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
983 /* fixup right node */
984 push_space = btrfs_item_offset(right->items + push_items - 1) -
985 leaf_data_end(root, right);
986 memmove(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
987 push_space, btrfs_leaf_data(right) +
988 leaf_data_end(root, right), push_space);
989 memmove(right->items, right->items + push_items,
990 (btrfs_header_nritems(&right->header) - push_items) *
991 sizeof(struct btrfs_item));
992 btrfs_set_header_nritems(&right->header,
993 btrfs_header_nritems(&right->header) -
995 push_space = BTRFS_LEAF_DATA_SIZE(root);
997 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
998 btrfs_set_item_offset(right->items + i, push_space -
999 btrfs_item_size(right->items + i));
1000 push_space = btrfs_item_offset(right->items + i);
1003 BUG_ON(list_empty(&t->dirty));
1004 BUG_ON(list_empty(&right_buf->dirty));
1006 wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1);
1010 /* then fixup the leaf pointer in the path */
1011 if (path->slots[0] < push_items) {
1012 path->slots[0] += old_left_nritems;
1013 btrfs_block_release(root, path->nodes[0]);
1015 path->slots[1] -= 1;
1017 btrfs_block_release(root, t);
1018 path->slots[0] -= push_items;
1020 BUG_ON(path->slots[0] < 0);
1025 * split the path's leaf in two, making sure there is at least data_size
1026 * available for the resulting leaf level of the path.
1028 * returns 0 if all went well and < 0 on failure.
1030 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
1031 *root, struct btrfs_path *path, int data_size)
1033 struct btrfs_buffer *l_buf;
1034 struct btrfs_leaf *l;
1038 struct btrfs_leaf *right;
1039 struct btrfs_buffer *right_buffer;
1040 int space_needed = data_size + sizeof(struct btrfs_item);
1047 /* first try to make some room by pushing left and right */
1048 wret = push_leaf_left(trans, root, path, data_size);
1052 wret = push_leaf_right(trans, root, path, data_size);
1056 l_buf = path->nodes[0];
1059 /* did the pushes work? */
1060 if (btrfs_leaf_free_space(root, l) >=
1061 sizeof(struct btrfs_item) + data_size)
1064 if (!path->nodes[1]) {
1065 ret = insert_new_root(trans, root, path, 1);
1069 slot = path->slots[0];
1070 nritems = btrfs_header_nritems(&l->header);
1071 mid = (nritems + 1)/ 2;
1072 right_buffer = btrfs_alloc_free_block(trans, root);
1073 BUG_ON(!right_buffer);
1074 BUG_ON(mid == nritems);
1075 right = &right_buffer->leaf;
1076 memset(&right->header, 0, sizeof(right->header));
1078 /* FIXME, just alloc a new leaf here */
1079 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
1080 BTRFS_LEAF_DATA_SIZE(root))
1083 /* FIXME, just alloc a new leaf here */
1084 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1085 BTRFS_LEAF_DATA_SIZE(root))
1088 btrfs_set_header_nritems(&right->header, nritems - mid);
1089 btrfs_set_header_blocknr(&right->header, right_buffer->blocknr);
1090 btrfs_set_header_level(&right->header, 0);
1091 data_copy_size = btrfs_item_end(l->items + mid) -
1092 leaf_data_end(root, l);
1093 memcpy(right->items, l->items + mid,
1094 (nritems - mid) * sizeof(struct btrfs_item));
1095 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1096 data_copy_size, btrfs_leaf_data(l) +
1097 leaf_data_end(root, l), data_copy_size);
1098 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1099 btrfs_item_end(l->items + mid);
1101 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
1102 u32 ioff = btrfs_item_offset(right->items + i);
1103 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1106 btrfs_set_header_nritems(&l->header, mid);
1108 wret = insert_ptr(trans, root, path, &right->items[0].key,
1109 right_buffer->blocknr, path->slots[1] + 1, 1);
1112 BUG_ON(list_empty(&right_buffer->dirty));
1113 BUG_ON(list_empty(&l_buf->dirty));
1114 BUG_ON(path->slots[0] != slot);
1116 btrfs_block_release(root, path->nodes[0]);
1117 path->nodes[0] = right_buffer;
1118 path->slots[0] -= mid;
1119 path->slots[1] += 1;
1121 btrfs_block_release(root, right_buffer);
1122 BUG_ON(path->slots[0] < 0);
1127 * Given a key and some data, insert an item into the tree.
1128 * This does all the path init required, making room in the tree if needed.
1130 int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1131 *root, struct btrfs_path *path, struct btrfs_key
1132 *cpu_key, u32 data_size)
1137 struct btrfs_leaf *leaf;
1138 struct btrfs_buffer *leaf_buf;
1140 unsigned int data_end;
1141 struct btrfs_disk_key disk_key;
1143 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
1145 /* create a root if there isn't one */
1148 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
1155 slot_orig = path->slots[0];
1156 leaf_buf = path->nodes[0];
1157 leaf = &leaf_buf->leaf;
1159 nritems = btrfs_header_nritems(&leaf->header);
1160 data_end = leaf_data_end(root, leaf);
1162 if (btrfs_leaf_free_space(root, leaf) <
1163 sizeof(struct btrfs_item) + data_size)
1166 slot = path->slots[0];
1168 if (slot != nritems) {
1170 unsigned int old_data = btrfs_item_end(leaf->items + slot);
1173 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1175 /* first correct the data pointers */
1176 for (i = slot; i < nritems; i++) {
1177 u32 ioff = btrfs_item_offset(leaf->items + i);
1178 btrfs_set_item_offset(leaf->items + i,
1182 /* shift the items */
1183 memmove(leaf->items + slot + 1, leaf->items + slot,
1184 (nritems - slot) * sizeof(struct btrfs_item));
1186 /* shift the data */
1187 memmove(btrfs_leaf_data(leaf) + data_end - data_size,
1188 btrfs_leaf_data(leaf) +
1189 data_end, old_data - data_end);
1190 data_end = old_data;
1192 /* setup the item for the new data */
1193 memcpy(&leaf->items[slot].key, &disk_key,
1194 sizeof(struct btrfs_disk_key));
1195 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1196 btrfs_set_item_size(leaf->items + slot, data_size);
1197 btrfs_set_header_nritems(&leaf->header, nritems + 1);
1201 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
1203 BUG_ON(list_empty(&leaf_buf->dirty));
1204 if (btrfs_leaf_free_space(root, leaf) < 0)
1206 check_leaf(root, path, 0);
1212 * Given a key and some data, insert an item into the tree.
1213 * This does all the path init required, making room in the tree if needed.
1215 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1216 *root, struct btrfs_key *cpu_key, void *data, u32
1220 struct btrfs_path path;
1223 btrfs_init_path(&path);
1224 ret = btrfs_insert_empty_item(trans, root, &path, cpu_key, data_size);
1226 ptr = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0], u8);
1227 memcpy(ptr, data, data_size);
1229 btrfs_release_path(root, &path);
1234 * delete the pointer from a given node.
1236 * If the delete empties a node, the node is removed from the tree,
1237 * continuing all the way the root if required. The root is converted into
1238 * a leaf if all the nodes are emptied.
1240 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1241 struct btrfs_path *path, int level, int slot)
1243 struct btrfs_node *node;
1244 struct btrfs_buffer *parent = path->nodes[level];
1249 node = &parent->node;
1250 nritems = btrfs_header_nritems(&node->header);
1251 if (slot != nritems -1) {
1252 memmove(node->ptrs + slot, node->ptrs + slot + 1,
1253 sizeof(struct btrfs_key_ptr) * (nritems - slot - 1));
1256 btrfs_set_header_nritems(&node->header, nritems);
1257 if (nritems == 0 && parent == root->node) {
1258 BUG_ON(btrfs_header_level(&root->node->node.header) != 1);
1259 /* just turn the root into a leaf and break */
1260 btrfs_set_header_level(&root->node->node.header, 0);
1261 } else if (slot == 0) {
1262 wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key,
1267 BUG_ON(list_empty(&parent->dirty));
1272 * delete the item at the leaf level in path. If that empties
1273 * the leaf, remove it from the tree
1275 int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1276 struct btrfs_path *path)
1279 struct btrfs_leaf *leaf;
1280 struct btrfs_buffer *leaf_buf;
1287 leaf_buf = path->nodes[0];
1288 leaf = &leaf_buf->leaf;
1289 slot = path->slots[0];
1290 doff = btrfs_item_offset(leaf->items + slot);
1291 dsize = btrfs_item_size(leaf->items + slot);
1292 nritems = btrfs_header_nritems(&leaf->header);
1294 if (slot != nritems - 1) {
1296 int data_end = leaf_data_end(root, leaf);
1297 memmove(btrfs_leaf_data(leaf) + data_end + dsize,
1298 btrfs_leaf_data(leaf) + data_end,
1300 for (i = slot + 1; i < nritems; i++) {
1301 u32 ioff = btrfs_item_offset(leaf->items + i);
1302 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
1304 memmove(leaf->items + slot, leaf->items + slot + 1,
1305 sizeof(struct btrfs_item) *
1306 (nritems - slot - 1));
1308 btrfs_set_header_nritems(&leaf->header, nritems - 1);
1310 /* delete the leaf if we've emptied it */
1312 if (leaf_buf == root->node) {
1313 btrfs_set_header_level(&leaf->header, 0);
1314 BUG_ON(list_empty(&leaf_buf->dirty));
1316 clean_tree_block(trans, root, leaf_buf);
1317 wret = del_ptr(trans, root, path, 1, path->slots[1]);
1320 wret = btrfs_free_extent(trans, root,
1321 leaf_buf->blocknr, 1, 1);
1326 int used = leaf_space_used(leaf, 0, nritems);
1328 wret = fixup_low_keys(trans, root, path,
1329 &leaf->items[0].key, 1);
1333 BUG_ON(list_empty(&leaf_buf->dirty));
1335 /* delete the leaf if it is mostly empty */
1336 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
1337 /* push_leaf_left fixes the path.
1338 * make sure the path still points to our leaf
1339 * for possible call to del_ptr below
1341 slot = path->slots[1];
1343 wret = push_leaf_left(trans, root, path, 1);
1346 if (path->nodes[0] == leaf_buf &&
1347 btrfs_header_nritems(&leaf->header)) {
1348 wret = push_leaf_right(trans, root, path, 1);
1352 if (btrfs_header_nritems(&leaf->header) == 0) {
1353 u64 blocknr = leaf_buf->blocknr;
1354 clean_tree_block(trans, root, leaf_buf);
1355 wret = del_ptr(trans, root, path, 1, slot);
1358 btrfs_block_release(root, leaf_buf);
1359 wret = btrfs_free_extent(trans, root, blocknr,
1364 btrfs_block_release(root, leaf_buf);
1371 int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
1372 *root, struct btrfs_path *path, u32 data_size)
1377 struct btrfs_leaf *leaf;
1378 struct btrfs_buffer *leaf_buf;
1380 unsigned int data_end;
1381 unsigned int old_data;
1382 unsigned int old_size;
1385 slot_orig = path->slots[0];
1386 leaf_buf = path->nodes[0];
1387 leaf = &leaf_buf->leaf;
1389 nritems = btrfs_header_nritems(&leaf->header);
1390 data_end = leaf_data_end(root, leaf);
1392 if (btrfs_leaf_free_space(root, leaf) < data_size)
1394 slot = path->slots[0];
1395 old_data = btrfs_item_end(leaf->items + slot);
1398 BUG_ON(slot >= nritems);
1401 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1403 /* first correct the data pointers */
1404 for (i = slot; i < nritems; i++) {
1405 u32 ioff = btrfs_item_offset(leaf->items + i);
1406 btrfs_set_item_offset(leaf->items + i,
1409 /* shift the data */
1410 memmove(btrfs_leaf_data(leaf) + data_end - data_size,
1411 btrfs_leaf_data(leaf) + data_end, old_data - data_end);
1412 data_end = old_data;
1413 old_size = btrfs_item_size(leaf->items + slot);
1414 btrfs_set_item_size(leaf->items + slot, old_size + data_size);
1417 if (btrfs_leaf_free_space(root, leaf) < 0)
1419 check_leaf(root, path, 0);
1424 * walk up the tree as far as required to find the next leaf.
1425 * returns 0 if it found something or 1 if there are no greater leaves.
1426 * returns < 0 on io errors.
1428 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
1433 struct btrfs_buffer *c;
1434 struct btrfs_buffer *next = NULL;
1436 while(level < BTRFS_MAX_LEVEL) {
1437 if (!path->nodes[level])
1439 slot = path->slots[level] + 1;
1440 c = path->nodes[level];
1441 if (slot >= btrfs_header_nritems(&c->node.header)) {
1445 blocknr = btrfs_node_blockptr(&c->node, slot);
1447 btrfs_block_release(root, next);
1448 next = read_tree_block(root, blocknr);
1451 path->slots[level] = slot;
1454 c = path->nodes[level];
1455 btrfs_block_release(root, c);
1456 path->nodes[level] = next;
1457 path->slots[level] = 0;
1460 next = read_tree_block(root,
1461 btrfs_node_blockptr(&next->node, 0));