1 #include <linux/module.h>
4 #include "transaction.h"
6 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
7 *root, struct btrfs_path *path, int level);
8 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
9 *root, struct btrfs_key *ins_key,
10 struct btrfs_path *path, int data_size);
11 static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
12 *root, struct buffer_head *dst, struct buffer_head
14 static int balance_node_right(struct btrfs_trans_handle *trans, struct
15 btrfs_root *root, struct buffer_head *dst_buf,
16 struct buffer_head *src_buf);
17 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
18 struct btrfs_path *path, int level, int slot);
20 inline void btrfs_init_path(struct btrfs_path *p)
22 memset(p, 0, sizeof(*p));
25 struct btrfs_path *btrfs_alloc_path(void)
27 struct btrfs_path *path;
28 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
30 btrfs_init_path(path);
34 void btrfs_free_path(struct btrfs_path *p)
36 btrfs_release_path(NULL, p);
37 kmem_cache_free(btrfs_path_cachep, p);
40 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
43 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
46 btrfs_block_release(root, p->nodes[i]);
48 memset(p, 0, sizeof(*p));
51 static int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
52 *root, struct buffer_head *buf, struct buffer_head
53 *parent, int parent_slot, struct buffer_head
56 struct buffer_head *cow;
57 struct btrfs_node *cow_node;
59 if (btrfs_header_generation(btrfs_buffer_header(buf)) ==
64 cow = btrfs_alloc_free_block(trans, root);
65 cow_node = btrfs_buffer_node(cow);
66 if (buf->b_size != root->blocksize || cow->b_size != root->blocksize)
68 memcpy(cow_node, btrfs_buffer_node(buf), root->blocksize);
69 btrfs_set_header_blocknr(&cow_node->header, cow->b_blocknr);
70 btrfs_set_header_generation(&cow_node->header, trans->transid);
71 btrfs_inc_ref(trans, root, buf);
72 if (buf == root->node) {
75 if (buf != root->commit_root) {
76 btrfs_free_extent(trans, root, buf->b_blocknr, 1, 1);
78 btrfs_block_release(root, buf);
80 btrfs_set_node_blockptr(btrfs_buffer_node(parent), parent_slot,
82 btrfs_mark_buffer_dirty(parent);
83 btrfs_free_extent(trans, root, buf->b_blocknr, 1, 1);
85 btrfs_block_release(root, buf);
86 mark_buffer_dirty(cow);
92 * The leaf data grows from end-to-front in the node.
93 * this returns the address of the start of the last item,
94 * which is the stop of the leaf data stack
96 static inline unsigned int leaf_data_end(struct btrfs_root *root,
97 struct btrfs_leaf *leaf)
99 u32 nr = btrfs_header_nritems(&leaf->header);
101 return BTRFS_LEAF_DATA_SIZE(root);
102 return btrfs_item_offset(leaf->items + nr - 1);
106 * compare two keys in a memcmp fashion
108 static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
112 btrfs_disk_key_to_cpu(&k1, disk);
114 if (k1.objectid > k2->objectid)
116 if (k1.objectid < k2->objectid)
118 if (k1.offset > k2->offset)
120 if (k1.offset < k2->offset)
122 if (k1.flags > k2->flags)
124 if (k1.flags < k2->flags)
129 static int check_node(struct btrfs_root *root, struct btrfs_path *path,
133 struct btrfs_node *parent = NULL;
134 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
136 u32 nritems = btrfs_header_nritems(&node->header);
138 if (path->nodes[level + 1])
139 parent = btrfs_buffer_node(path->nodes[level + 1]);
140 parent_slot = path->slots[level + 1];
141 BUG_ON(nritems == 0);
143 struct btrfs_disk_key *parent_key;
144 parent_key = &parent->ptrs[parent_slot].key;
145 BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
146 sizeof(struct btrfs_disk_key)));
147 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
148 btrfs_header_blocknr(&node->header));
150 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
151 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
152 struct btrfs_key cpukey;
153 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[i + 1].key);
154 BUG_ON(comp_keys(&node->ptrs[i].key, &cpukey) >= 0);
159 static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
163 struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[level]);
164 struct btrfs_node *parent = NULL;
166 u32 nritems = btrfs_header_nritems(&leaf->header);
168 if (path->nodes[level + 1])
169 parent = btrfs_buffer_node(path->nodes[level + 1]);
170 parent_slot = path->slots[level + 1];
171 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
177 struct btrfs_disk_key *parent_key;
178 parent_key = &parent->ptrs[parent_slot].key;
179 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
180 sizeof(struct btrfs_disk_key)));
181 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
182 btrfs_header_blocknr(&leaf->header));
184 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
185 struct btrfs_key cpukey;
186 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key);
187 BUG_ON(comp_keys(&leaf->items[i].key,
189 BUG_ON(btrfs_item_offset(leaf->items + i) !=
190 btrfs_item_end(leaf->items + i + 1));
192 BUG_ON(btrfs_item_offset(leaf->items + i) +
193 btrfs_item_size(leaf->items + i) !=
194 BTRFS_LEAF_DATA_SIZE(root));
200 static int check_block(struct btrfs_root *root, struct btrfs_path *path,
203 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
204 if (memcmp(node->header.fsid, root->fs_info->disk_super->fsid,
205 sizeof(node->header.fsid)))
208 return check_leaf(root, path, level);
209 return check_node(root, path, level);
213 * search for key in the array p. items p are item_size apart
214 * and there are 'max' items in p
215 * the slot in the array is returned via slot, and it points to
216 * the place where you would insert key if it is not found in
219 * slot may point to max if the key is bigger than all of the keys
221 static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
228 struct btrfs_disk_key *tmp;
231 mid = (low + high) / 2;
232 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
233 ret = comp_keys(tmp, key);
249 * simple bin_search frontend that does the right thing for
252 static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
254 if (btrfs_is_leaf(c)) {
255 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
256 return generic_bin_search((void *)l->items,
257 sizeof(struct btrfs_item),
258 key, btrfs_header_nritems(&c->header),
261 return generic_bin_search((void *)c->ptrs,
262 sizeof(struct btrfs_key_ptr),
263 key, btrfs_header_nritems(&c->header),
269 static struct buffer_head *read_node_slot(struct btrfs_root *root,
270 struct buffer_head *parent_buf,
273 struct btrfs_node *node = btrfs_buffer_node(parent_buf);
276 if (slot >= btrfs_header_nritems(&node->header))
278 return read_tree_block(root, btrfs_node_blockptr(node, slot));
281 static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
282 *root, struct btrfs_path *path, int level)
284 struct buffer_head *right_buf;
285 struct buffer_head *mid_buf;
286 struct buffer_head *left_buf;
287 struct buffer_head *parent_buf = NULL;
288 struct btrfs_node *right = NULL;
289 struct btrfs_node *mid;
290 struct btrfs_node *left = NULL;
291 struct btrfs_node *parent = NULL;
295 int orig_slot = path->slots[level];
301 mid_buf = path->nodes[level];
302 mid = btrfs_buffer_node(mid_buf);
303 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
305 if (level < BTRFS_MAX_LEVEL - 1)
306 parent_buf = path->nodes[level + 1];
307 pslot = path->slots[level + 1];
310 * deal with the case where there is only one pointer in the root
311 * by promoting the node below to a root
314 struct buffer_head *child;
315 u64 blocknr = mid_buf->b_blocknr;
317 if (btrfs_header_nritems(&mid->header) != 1)
320 /* promote the child to a root */
321 child = read_node_slot(root, mid_buf, 0);
324 path->nodes[level] = NULL;
325 clean_tree_block(trans, root, mid_buf);
326 wait_on_buffer(mid_buf);
327 /* once for the path */
328 btrfs_block_release(root, mid_buf);
329 /* once for the root ptr */
330 btrfs_block_release(root, mid_buf);
331 return btrfs_free_extent(trans, root, blocknr, 1, 1);
333 parent = btrfs_buffer_node(parent_buf);
335 if (btrfs_header_nritems(&mid->header) >
336 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
339 left_buf = read_node_slot(root, parent_buf, pslot - 1);
340 right_buf = read_node_slot(root, parent_buf, pslot + 1);
342 /* first, try to make some room in the middle buffer */
344 btrfs_cow_block(trans, root, left_buf, parent_buf, pslot - 1,
346 left = btrfs_buffer_node(left_buf);
347 orig_slot += btrfs_header_nritems(&left->header);
348 wret = push_node_left(trans, root, left_buf, mid_buf);
354 * then try to empty the right most buffer into the middle
357 btrfs_cow_block(trans, root, right_buf, parent_buf, pslot + 1,
359 right = btrfs_buffer_node(right_buf);
360 wret = push_node_left(trans, root, mid_buf, right_buf);
363 if (btrfs_header_nritems(&right->header) == 0) {
364 u64 blocknr = right_buf->b_blocknr;
365 clean_tree_block(trans, root, right_buf);
366 wait_on_buffer(right_buf);
367 btrfs_block_release(root, right_buf);
370 wret = del_ptr(trans, root, path, level + 1, pslot +
374 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
378 btrfs_memcpy(root, parent,
379 &parent->ptrs[pslot + 1].key,
381 sizeof(struct btrfs_disk_key));
382 btrfs_mark_buffer_dirty(parent_buf);
385 if (btrfs_header_nritems(&mid->header) == 1) {
387 * we're not allowed to leave a node with one item in the
388 * tree during a delete. A deletion from lower in the tree
389 * could try to delete the only pointer in this node.
390 * So, pull some keys from the left.
391 * There has to be a left pointer at this point because
392 * otherwise we would have pulled some pointers from the
396 wret = balance_node_right(trans, root, mid_buf, left_buf);
401 if (btrfs_header_nritems(&mid->header) == 0) {
402 /* we've managed to empty the middle node, drop it */
403 u64 blocknr = mid_buf->b_blocknr;
404 clean_tree_block(trans, root, mid_buf);
405 wait_on_buffer(mid_buf);
406 btrfs_block_release(root, mid_buf);
409 wret = del_ptr(trans, root, path, level + 1, pslot);
412 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
416 /* update the parent key to reflect our changes */
417 btrfs_memcpy(root, parent,
418 &parent->ptrs[pslot].key, &mid->ptrs[0].key,
419 sizeof(struct btrfs_disk_key));
420 btrfs_mark_buffer_dirty(parent_buf);
423 /* update the path */
425 if (btrfs_header_nritems(&left->header) > orig_slot) {
427 path->nodes[level] = left_buf;
428 path->slots[level + 1] -= 1;
429 path->slots[level] = orig_slot;
431 btrfs_block_release(root, mid_buf);
433 orig_slot -= btrfs_header_nritems(&left->header);
434 path->slots[level] = orig_slot;
437 /* double check we haven't messed things up */
438 check_block(root, path, level);
440 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[level]),
445 btrfs_block_release(root, right_buf);
447 btrfs_block_release(root, left_buf);
452 * look for key in the tree. path is filled in with nodes along the way
453 * if key is found, we return zero and you can find the item in the leaf
454 * level of the path (level 0)
456 * If the key isn't found, the path points to the slot where it should
457 * be inserted, and 1 is returned. If there are other errors during the
458 * search a negative error number is returned.
460 * if ins_len > 0, nodes and leaves will be split as we walk down the
461 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
464 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
465 *root, struct btrfs_key *key, struct btrfs_path *p, int
468 struct buffer_head *b;
469 struct buffer_head *cow_buf;
470 struct btrfs_node *c;
475 WARN_ON(p->nodes[0] != NULL);
476 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
481 c = btrfs_buffer_node(b);
482 level = btrfs_header_level(&c->header);
485 wret = btrfs_cow_block(trans, root, b,
490 c = btrfs_buffer_node(b);
492 BUG_ON(!cow && ins_len);
493 if (level != btrfs_header_level(&c->header))
495 level = btrfs_header_level(&c->header);
497 ret = check_block(root, p, level);
500 ret = bin_search(c, key, &slot);
501 if (!btrfs_is_leaf(c)) {
504 p->slots[level] = slot;
505 if (ins_len > 0 && btrfs_header_nritems(&c->header) >=
506 BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
507 int sret = split_node(trans, root, p, level);
512 c = btrfs_buffer_node(b);
513 slot = p->slots[level];
514 } else if (ins_len < 0) {
515 int sret = balance_level(trans, root, p,
522 c = btrfs_buffer_node(b);
523 slot = p->slots[level];
524 BUG_ON(btrfs_header_nritems(&c->header) == 1);
526 b = read_tree_block(root, btrfs_node_blockptr(c, slot));
528 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
529 p->slots[level] = slot;
530 if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
531 sizeof(struct btrfs_item) + ins_len) {
532 int sret = split_leaf(trans, root, key,
545 * adjust the pointers going up the tree, starting at level
546 * making sure the right key of each node is points to 'key'.
547 * This is used after shifting pointers to the left, so it stops
548 * fixing up pointers when a given leaf/node is not in slot 0 of the
551 * If this fails to write a tree block, it returns -1, but continues
552 * fixing up the blocks in ram so the tree is consistent.
554 static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root
555 *root, struct btrfs_path *path, struct btrfs_disk_key
560 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
561 struct btrfs_node *t;
562 int tslot = path->slots[i];
565 t = btrfs_buffer_node(path->nodes[i]);
566 btrfs_memcpy(root, t, &t->ptrs[tslot].key, key, sizeof(*key));
567 btrfs_mark_buffer_dirty(path->nodes[i]);
575 * try to push data from one node into the next node left in the
578 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
579 * error, and > 0 if there was no room in the left hand block.
581 static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
582 *root, struct buffer_head *dst_buf, struct
583 buffer_head *src_buf)
585 struct btrfs_node *src = btrfs_buffer_node(src_buf);
586 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
592 src_nritems = btrfs_header_nritems(&src->header);
593 dst_nritems = btrfs_header_nritems(&dst->header);
594 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
595 if (push_items <= 0) {
599 if (src_nritems < push_items)
600 push_items = src_nritems;
602 btrfs_memcpy(root, dst, dst->ptrs + dst_nritems, src->ptrs,
603 push_items * sizeof(struct btrfs_key_ptr));
604 if (push_items < src_nritems) {
605 btrfs_memmove(root, src, src->ptrs, src->ptrs + push_items,
606 (src_nritems - push_items) *
607 sizeof(struct btrfs_key_ptr));
609 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
610 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
611 btrfs_mark_buffer_dirty(src_buf);
612 btrfs_mark_buffer_dirty(dst_buf);
617 * try to push data from one node into the next node right in the
620 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
621 * error, and > 0 if there was no room in the right hand block.
623 * this will only push up to 1/2 the contents of the left node over
625 static int balance_node_right(struct btrfs_trans_handle *trans, struct
626 btrfs_root *root, struct buffer_head *dst_buf,
627 struct buffer_head *src_buf)
629 struct btrfs_node *src = btrfs_buffer_node(src_buf);
630 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
637 src_nritems = btrfs_header_nritems(&src->header);
638 dst_nritems = btrfs_header_nritems(&dst->header);
639 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
640 if (push_items <= 0) {
644 max_push = src_nritems / 2 + 1;
645 /* don't try to empty the node */
646 if (max_push > src_nritems)
648 if (max_push < push_items)
649 push_items = max_push;
651 btrfs_memmove(root, dst, dst->ptrs + push_items, dst->ptrs,
652 dst_nritems * sizeof(struct btrfs_key_ptr));
654 btrfs_memcpy(root, dst, dst->ptrs,
655 src->ptrs + src_nritems - push_items,
656 push_items * sizeof(struct btrfs_key_ptr));
658 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
659 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
661 btrfs_mark_buffer_dirty(src_buf);
662 btrfs_mark_buffer_dirty(dst_buf);
667 * helper function to insert a new root level in the tree.
668 * A new node is allocated, and a single item is inserted to
669 * point to the existing root
671 * returns zero on success or < 0 on failure.
673 static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root
674 *root, struct btrfs_path *path, int level)
676 struct buffer_head *t;
677 struct btrfs_node *lower;
678 struct btrfs_node *c;
679 struct btrfs_disk_key *lower_key;
681 BUG_ON(path->nodes[level]);
682 BUG_ON(path->nodes[level-1] != root->node);
684 t = btrfs_alloc_free_block(trans, root);
685 c = btrfs_buffer_node(t);
686 memset(c, 0, root->blocksize);
687 btrfs_set_header_nritems(&c->header, 1);
688 btrfs_set_header_level(&c->header, level);
689 btrfs_set_header_blocknr(&c->header, t->b_blocknr);
690 btrfs_set_header_generation(&c->header, trans->transid);
691 btrfs_set_header_parentid(&c->header,
692 btrfs_header_parentid(btrfs_buffer_header(root->node)));
693 lower = btrfs_buffer_node(path->nodes[level-1]);
694 memcpy(c->header.fsid, root->fs_info->disk_super->fsid,
695 sizeof(c->header.fsid));
696 if (btrfs_is_leaf(lower))
697 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
699 lower_key = &lower->ptrs[0].key;
700 btrfs_memcpy(root, c, &c->ptrs[0].key, lower_key,
701 sizeof(struct btrfs_disk_key));
702 btrfs_set_node_blockptr(c, 0, path->nodes[level - 1]->b_blocknr);
704 btrfs_mark_buffer_dirty(t);
706 /* the super has an extra ref to root->node */
707 btrfs_block_release(root, root->node);
710 path->nodes[level] = t;
711 path->slots[level] = 0;
716 * worker function to insert a single pointer in a node.
717 * the node should have enough room for the pointer already
719 * slot and level indicate where you want the key to go, and
720 * blocknr is the block the key points to.
722 * returns zero on success and < 0 on any error
724 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
725 *root, struct btrfs_path *path, struct btrfs_disk_key
726 *key, u64 blocknr, int slot, int level)
728 struct btrfs_node *lower;
731 BUG_ON(!path->nodes[level]);
732 lower = btrfs_buffer_node(path->nodes[level]);
733 nritems = btrfs_header_nritems(&lower->header);
736 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
738 if (slot != nritems) {
739 btrfs_memmove(root, lower, lower->ptrs + slot + 1,
741 (nritems - slot) * sizeof(struct btrfs_key_ptr));
743 btrfs_memcpy(root, lower, &lower->ptrs[slot].key,
744 key, sizeof(struct btrfs_disk_key));
745 btrfs_set_node_blockptr(lower, slot, blocknr);
746 btrfs_set_header_nritems(&lower->header, nritems + 1);
747 btrfs_mark_buffer_dirty(path->nodes[level]);
752 * split the node at the specified level in path in two.
753 * The path is corrected to point to the appropriate node after the split
755 * Before splitting this tries to make some room in the node by pushing
756 * left and right, if either one works, it returns right away.
758 * returns 0 on success and < 0 on failure
760 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
761 *root, struct btrfs_path *path, int level)
763 struct buffer_head *t;
764 struct btrfs_node *c;
765 struct buffer_head *split_buffer;
766 struct btrfs_node *split;
772 t = path->nodes[level];
773 c = btrfs_buffer_node(t);
774 if (t == root->node) {
775 /* trying to split the root, lets make a new one */
776 ret = insert_new_root(trans, root, path, level + 1);
780 c_nritems = btrfs_header_nritems(&c->header);
781 split_buffer = btrfs_alloc_free_block(trans, root);
782 split = btrfs_buffer_node(split_buffer);
783 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
784 btrfs_set_header_level(&split->header, btrfs_header_level(&c->header));
785 btrfs_set_header_blocknr(&split->header, split_buffer->b_blocknr);
786 btrfs_set_header_generation(&split->header, trans->transid);
787 btrfs_set_header_parentid(&split->header,
788 btrfs_header_parentid(btrfs_buffer_header(root->node)));
789 memcpy(split->header.fsid, root->fs_info->disk_super->fsid,
790 sizeof(split->header.fsid));
791 mid = (c_nritems + 1) / 2;
792 btrfs_memcpy(root, split, split->ptrs, c->ptrs + mid,
793 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
794 btrfs_set_header_nritems(&split->header, c_nritems - mid);
795 btrfs_set_header_nritems(&c->header, mid);
798 btrfs_mark_buffer_dirty(t);
799 btrfs_mark_buffer_dirty(split_buffer);
800 wret = insert_ptr(trans, root, path, &split->ptrs[0].key,
801 split_buffer->b_blocknr, path->slots[level + 1] + 1,
806 if (path->slots[level] >= mid) {
807 path->slots[level] -= mid;
808 btrfs_block_release(root, t);
809 path->nodes[level] = split_buffer;
810 path->slots[level + 1] += 1;
812 btrfs_block_release(root, split_buffer);
818 * how many bytes are required to store the items in a leaf. start
819 * and nr indicate which items in the leaf to check. This totals up the
820 * space used both by the item structs and the item data
822 static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
825 int nritems = btrfs_header_nritems(&l->header);
826 int end = min(nritems, start + nr) - 1;
830 data_len = btrfs_item_end(l->items + start);
831 data_len = data_len - btrfs_item_offset(l->items + end);
832 data_len += sizeof(struct btrfs_item) * nr;
833 WARN_ON(data_len < 0);
838 * The space between the end of the leaf items and
839 * the start of the leaf data. IOW, how much room
840 * the leaf has left for both items and data
842 int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
844 int nritems = btrfs_header_nritems(&leaf->header);
845 return BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
849 * push some data in the path leaf to the right, trying to free up at
850 * least data_size bytes. returns zero if the push worked, nonzero otherwise
852 * returns 1 if the push failed because the other node didn't have enough
853 * room, 0 if everything worked out and < 0 if there were major errors.
855 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
856 *root, struct btrfs_path *path, int data_size)
858 struct buffer_head *left_buf = path->nodes[0];
859 struct btrfs_leaf *left = btrfs_buffer_leaf(left_buf);
860 struct btrfs_leaf *right;
861 struct buffer_head *right_buf;
862 struct buffer_head *upper;
863 struct btrfs_node *upper_node;
869 struct btrfs_item *item;
873 slot = path->slots[1];
874 if (!path->nodes[1]) {
877 upper = path->nodes[1];
878 upper_node = btrfs_buffer_node(upper);
879 if (slot >= btrfs_header_nritems(&upper_node->header) - 1) {
882 right_buf = read_tree_block(root,
883 btrfs_node_blockptr(btrfs_buffer_node(upper), slot + 1));
884 right = btrfs_buffer_leaf(right_buf);
885 free_space = btrfs_leaf_free_space(root, right);
886 if (free_space < data_size + sizeof(struct btrfs_item)) {
887 btrfs_block_release(root, right_buf);
890 /* cow and double check */
891 btrfs_cow_block(trans, root, right_buf, upper, slot + 1, &right_buf);
892 right = btrfs_buffer_leaf(right_buf);
893 free_space = btrfs_leaf_free_space(root, right);
894 if (free_space < data_size + sizeof(struct btrfs_item)) {
895 btrfs_block_release(root, right_buf);
899 left_nritems = btrfs_header_nritems(&left->header);
900 for (i = left_nritems - 1; i >= 0; i--) {
901 item = left->items + i;
902 if (path->slots[0] == i)
903 push_space += data_size + sizeof(*item);
904 if (btrfs_item_size(item) + sizeof(*item) + push_space >
908 push_space += btrfs_item_size(item) + sizeof(*item);
910 if (push_items == 0) {
911 btrfs_block_release(root, right_buf);
914 right_nritems = btrfs_header_nritems(&right->header);
915 /* push left to right */
916 push_space = btrfs_item_end(left->items + left_nritems - push_items);
917 push_space -= leaf_data_end(root, left);
918 /* make room in the right data area */
919 btrfs_memmove(root, right, btrfs_leaf_data(right) +
920 leaf_data_end(root, right) - push_space,
921 btrfs_leaf_data(right) +
922 leaf_data_end(root, right), BTRFS_LEAF_DATA_SIZE(root) -
923 leaf_data_end(root, right));
924 /* copy from the left data area */
925 btrfs_memcpy(root, right, btrfs_leaf_data(right) +
926 BTRFS_LEAF_DATA_SIZE(root) - push_space,
927 btrfs_leaf_data(left) + leaf_data_end(root, left),
929 btrfs_memmove(root, right, right->items + push_items, right->items,
930 right_nritems * sizeof(struct btrfs_item));
931 /* copy the items from left to right */
932 btrfs_memcpy(root, right, right->items, left->items +
933 left_nritems - push_items,
934 push_items * sizeof(struct btrfs_item));
936 /* update the item pointers */
937 right_nritems += push_items;
938 btrfs_set_header_nritems(&right->header, right_nritems);
939 push_space = BTRFS_LEAF_DATA_SIZE(root);
940 for (i = 0; i < right_nritems; i++) {
941 btrfs_set_item_offset(right->items + i, push_space -
942 btrfs_item_size(right->items + i));
943 push_space = btrfs_item_offset(right->items + i);
945 left_nritems -= push_items;
946 btrfs_set_header_nritems(&left->header, left_nritems);
948 btrfs_mark_buffer_dirty(left_buf);
949 btrfs_mark_buffer_dirty(right_buf);
950 btrfs_memcpy(root, upper_node, &upper_node->ptrs[slot + 1].key,
951 &right->items[0].key, sizeof(struct btrfs_disk_key));
952 btrfs_mark_buffer_dirty(upper);
954 /* then fixup the leaf pointer in the path */
955 if (path->slots[0] >= left_nritems) {
956 path->slots[0] -= left_nritems;
957 btrfs_block_release(root, path->nodes[0]);
958 path->nodes[0] = right_buf;
961 btrfs_block_release(root, right_buf);
966 * push some data in the path leaf to the left, trying to free up at
967 * least data_size bytes. returns zero if the push worked, nonzero otherwise
969 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
970 *root, struct btrfs_path *path, int data_size)
972 struct buffer_head *right_buf = path->nodes[0];
973 struct btrfs_leaf *right = btrfs_buffer_leaf(right_buf);
974 struct buffer_head *t;
975 struct btrfs_leaf *left;
981 struct btrfs_item *item;
982 u32 old_left_nritems;
986 slot = path->slots[1];
990 if (!path->nodes[1]) {
993 t = read_tree_block(root,
994 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[1]), slot - 1));
995 left = btrfs_buffer_leaf(t);
996 free_space = btrfs_leaf_free_space(root, left);
997 if (free_space < data_size + sizeof(struct btrfs_item)) {
998 btrfs_block_release(root, t);
1002 /* cow and double check */
1003 btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t);
1004 left = btrfs_buffer_leaf(t);
1005 free_space = btrfs_leaf_free_space(root, left);
1006 if (free_space < data_size + sizeof(struct btrfs_item)) {
1007 btrfs_block_release(root, t);
1011 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
1012 item = right->items + i;
1013 if (path->slots[0] == i)
1014 push_space += data_size + sizeof(*item);
1015 if (btrfs_item_size(item) + sizeof(*item) + push_space >
1019 push_space += btrfs_item_size(item) + sizeof(*item);
1021 if (push_items == 0) {
1022 btrfs_block_release(root, t);
1025 /* push data from right to left */
1026 btrfs_memcpy(root, left, left->items +
1027 btrfs_header_nritems(&left->header),
1028 right->items, push_items * sizeof(struct btrfs_item));
1029 push_space = BTRFS_LEAF_DATA_SIZE(root) -
1030 btrfs_item_offset(right->items + push_items -1);
1031 btrfs_memcpy(root, left, btrfs_leaf_data(left) +
1032 leaf_data_end(root, left) - push_space,
1033 btrfs_leaf_data(right) +
1034 btrfs_item_offset(right->items + push_items - 1),
1036 old_left_nritems = btrfs_header_nritems(&left->header);
1037 BUG_ON(old_left_nritems < 0);
1039 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
1040 u32 ioff = btrfs_item_offset(left->items + i);
1041 btrfs_set_item_offset(left->items + i, ioff -
1042 (BTRFS_LEAF_DATA_SIZE(root) -
1043 btrfs_item_offset(left->items +
1044 old_left_nritems - 1)));
1046 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
1048 /* fixup right node */
1049 push_space = btrfs_item_offset(right->items + push_items - 1) -
1050 leaf_data_end(root, right);
1051 btrfs_memmove(root, right, btrfs_leaf_data(right) +
1052 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1053 btrfs_leaf_data(right) +
1054 leaf_data_end(root, right), push_space);
1055 btrfs_memmove(root, right, right->items, right->items + push_items,
1056 (btrfs_header_nritems(&right->header) - push_items) *
1057 sizeof(struct btrfs_item));
1058 btrfs_set_header_nritems(&right->header,
1059 btrfs_header_nritems(&right->header) -
1061 push_space = BTRFS_LEAF_DATA_SIZE(root);
1063 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
1064 btrfs_set_item_offset(right->items + i, push_space -
1065 btrfs_item_size(right->items + i));
1066 push_space = btrfs_item_offset(right->items + i);
1069 btrfs_mark_buffer_dirty(t);
1070 btrfs_mark_buffer_dirty(right_buf);
1072 wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1);
1076 /* then fixup the leaf pointer in the path */
1077 if (path->slots[0] < push_items) {
1078 path->slots[0] += old_left_nritems;
1079 btrfs_block_release(root, path->nodes[0]);
1081 path->slots[1] -= 1;
1083 btrfs_block_release(root, t);
1084 path->slots[0] -= push_items;
1086 BUG_ON(path->slots[0] < 0);
1091 * split the path's leaf in two, making sure there is at least data_size
1092 * available for the resulting leaf level of the path.
1094 * returns 0 if all went well and < 0 on failure.
1096 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
1097 *root, struct btrfs_key *ins_key,
1098 struct btrfs_path *path, int data_size)
1100 struct buffer_head *l_buf;
1101 struct btrfs_leaf *l;
1105 struct btrfs_leaf *right;
1106 struct buffer_head *right_buffer;
1107 int space_needed = data_size + sizeof(struct btrfs_item);
1113 int double_split = 0;
1114 struct btrfs_disk_key disk_key;
1116 /* first try to make some room by pushing left and right */
1117 wret = push_leaf_left(trans, root, path, data_size);
1121 wret = push_leaf_right(trans, root, path, data_size);
1125 l_buf = path->nodes[0];
1126 l = btrfs_buffer_leaf(l_buf);
1128 /* did the pushes work? */
1129 if (btrfs_leaf_free_space(root, l) >=
1130 sizeof(struct btrfs_item) + data_size)
1133 if (!path->nodes[1]) {
1134 ret = insert_new_root(trans, root, path, 1);
1138 slot = path->slots[0];
1139 nritems = btrfs_header_nritems(&l->header);
1140 mid = (nritems + 1)/ 2;
1141 right_buffer = btrfs_alloc_free_block(trans, root);
1142 BUG_ON(!right_buffer);
1143 right = btrfs_buffer_leaf(right_buffer);
1144 memset(&right->header, 0, sizeof(right->header));
1145 btrfs_set_header_blocknr(&right->header, right_buffer->b_blocknr);
1146 btrfs_set_header_generation(&right->header, trans->transid);
1147 btrfs_set_header_level(&right->header, 0);
1148 btrfs_set_header_parentid(&right->header,
1149 btrfs_header_parentid(btrfs_buffer_header(root->node)));
1150 memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
1151 sizeof(right->header.fsid));
1154 leaf_space_used(l, mid, nritems - mid) + space_needed >
1155 BTRFS_LEAF_DATA_SIZE(root)) {
1156 if (slot >= nritems) {
1157 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1158 btrfs_set_header_nritems(&right->header, 0);
1159 wret = insert_ptr(trans, root, path,
1161 right_buffer->b_blocknr,
1162 path->slots[1] + 1, 1);
1165 btrfs_block_release(root, path->nodes[0]);
1166 path->nodes[0] = right_buffer;
1168 path->slots[1] += 1;
1175 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1176 BTRFS_LEAF_DATA_SIZE(root)) {
1178 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1179 btrfs_set_header_nritems(&right->header, 0);
1180 wret = insert_ptr(trans, root, path,
1182 right_buffer->b_blocknr,
1183 path->slots[1] - 1, 1);
1186 btrfs_block_release(root, path->nodes[0]);
1187 path->nodes[0] = right_buffer;
1189 path->slots[1] -= 1;
1196 btrfs_set_header_nritems(&right->header, nritems - mid);
1197 data_copy_size = btrfs_item_end(l->items + mid) -
1198 leaf_data_end(root, l);
1199 btrfs_memcpy(root, right, right->items, l->items + mid,
1200 (nritems - mid) * sizeof(struct btrfs_item));
1201 btrfs_memcpy(root, right,
1202 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1203 data_copy_size, btrfs_leaf_data(l) +
1204 leaf_data_end(root, l), data_copy_size);
1205 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1206 btrfs_item_end(l->items + mid);
1208 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
1209 u32 ioff = btrfs_item_offset(right->items + i);
1210 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1213 btrfs_set_header_nritems(&l->header, mid);
1215 wret = insert_ptr(trans, root, path, &right->items[0].key,
1216 right_buffer->b_blocknr, path->slots[1] + 1, 1);
1219 btrfs_mark_buffer_dirty(right_buffer);
1220 btrfs_mark_buffer_dirty(l_buf);
1221 BUG_ON(path->slots[0] != slot);
1223 btrfs_block_release(root, path->nodes[0]);
1224 path->nodes[0] = right_buffer;
1225 path->slots[0] -= mid;
1226 path->slots[1] += 1;
1228 btrfs_block_release(root, right_buffer);
1229 BUG_ON(path->slots[0] < 0);
1233 right_buffer = btrfs_alloc_free_block(trans, root);
1234 BUG_ON(!right_buffer);
1235 right = btrfs_buffer_leaf(right_buffer);
1236 memset(&right->header, 0, sizeof(right->header));
1237 btrfs_set_header_blocknr(&right->header, right_buffer->b_blocknr);
1238 btrfs_set_header_generation(&right->header, trans->transid);
1239 btrfs_set_header_level(&right->header, 0);
1240 btrfs_set_header_parentid(&right->header,
1241 btrfs_header_parentid(btrfs_buffer_header(root->node)));
1242 memcpy(right->header.fsid, root->fs_info->disk_super->fsid,
1243 sizeof(right->header.fsid));
1244 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1245 btrfs_set_header_nritems(&right->header, 0);
1246 wret = insert_ptr(trans, root, path,
1248 right_buffer->b_blocknr,
1252 btrfs_block_release(root, path->nodes[0]);
1253 path->nodes[0] = right_buffer;
1255 check_node(root, path, 1);
1256 check_leaf(root, path, 0);
1261 * Given a key and some data, insert an item into the tree.
1262 * This does all the path init required, making room in the tree if needed.
1264 int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1265 *root, struct btrfs_path *path, struct btrfs_key
1266 *cpu_key, u32 data_size)
1271 struct btrfs_leaf *leaf;
1272 struct buffer_head *leaf_buf;
1274 unsigned int data_end;
1275 struct btrfs_disk_key disk_key;
1277 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
1279 /* create a root if there isn't one */
1282 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
1289 slot_orig = path->slots[0];
1290 leaf_buf = path->nodes[0];
1291 leaf = btrfs_buffer_leaf(leaf_buf);
1293 nritems = btrfs_header_nritems(&leaf->header);
1294 data_end = leaf_data_end(root, leaf);
1296 if (btrfs_leaf_free_space(root, leaf) <
1297 sizeof(struct btrfs_item) + data_size) {
1300 slot = path->slots[0];
1302 if (slot != nritems) {
1304 unsigned int old_data = btrfs_item_end(leaf->items + slot);
1307 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1309 /* first correct the data pointers */
1310 for (i = slot; i < nritems; i++) {
1311 u32 ioff = btrfs_item_offset(leaf->items + i);
1312 btrfs_set_item_offset(leaf->items + i,
1316 /* shift the items */
1317 btrfs_memmove(root, leaf, leaf->items + slot + 1,
1319 (nritems - slot) * sizeof(struct btrfs_item));
1321 /* shift the data */
1322 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1323 data_end - data_size, btrfs_leaf_data(leaf) +
1324 data_end, old_data - data_end);
1325 data_end = old_data;
1327 /* setup the item for the new data */
1328 btrfs_memcpy(root, leaf, &leaf->items[slot].key, &disk_key,
1329 sizeof(struct btrfs_disk_key));
1330 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1331 btrfs_set_item_size(leaf->items + slot, data_size);
1332 btrfs_set_header_nritems(&leaf->header, nritems + 1);
1333 btrfs_mark_buffer_dirty(leaf_buf);
1337 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
1339 if (btrfs_leaf_free_space(root, leaf) < 0)
1341 check_leaf(root, path, 0);
1347 * Given a key and some data, insert an item into the tree.
1348 * This does all the path init required, making room in the tree if needed.
1350 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1351 *root, struct btrfs_key *cpu_key, void *data, u32
1355 struct btrfs_path *path;
1358 path = btrfs_alloc_path();
1360 btrfs_init_path(path);
1361 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
1363 ptr = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1364 path->slots[0], u8);
1365 btrfs_memcpy(root, path->nodes[0]->b_data,
1366 ptr, data, data_size);
1367 btrfs_mark_buffer_dirty(path->nodes[0]);
1369 btrfs_release_path(root, path);
1370 btrfs_free_path(path);
1375 * delete the pointer from a given node.
1377 * If the delete empties a node, the node is removed from the tree,
1378 * continuing all the way the root if required. The root is converted into
1379 * a leaf if all the nodes are emptied.
1381 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1382 struct btrfs_path *path, int level, int slot)
1384 struct btrfs_node *node;
1385 struct buffer_head *parent = path->nodes[level];
1390 node = btrfs_buffer_node(parent);
1391 nritems = btrfs_header_nritems(&node->header);
1392 if (slot != nritems -1) {
1393 btrfs_memmove(root, node, node->ptrs + slot,
1394 node->ptrs + slot + 1,
1395 sizeof(struct btrfs_key_ptr) *
1396 (nritems - slot - 1));
1399 btrfs_set_header_nritems(&node->header, nritems);
1400 if (nritems == 0 && parent == root->node) {
1401 struct btrfs_header *header = btrfs_buffer_header(root->node);
1402 BUG_ON(btrfs_header_level(header) != 1);
1403 /* just turn the root into a leaf and break */
1404 btrfs_set_header_level(header, 0);
1405 } else if (slot == 0) {
1406 wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key,
1411 btrfs_mark_buffer_dirty(parent);
1416 * delete the item at the leaf level in path. If that empties
1417 * the leaf, remove it from the tree
1419 int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1420 struct btrfs_path *path)
1423 struct btrfs_leaf *leaf;
1424 struct buffer_head *leaf_buf;
1431 leaf_buf = path->nodes[0];
1432 leaf = btrfs_buffer_leaf(leaf_buf);
1433 slot = path->slots[0];
1434 doff = btrfs_item_offset(leaf->items + slot);
1435 dsize = btrfs_item_size(leaf->items + slot);
1436 nritems = btrfs_header_nritems(&leaf->header);
1438 if (slot != nritems - 1) {
1440 int data_end = leaf_data_end(root, leaf);
1441 btrfs_memmove(root, leaf, btrfs_leaf_data(leaf) +
1443 btrfs_leaf_data(leaf) + data_end,
1445 for (i = slot + 1; i < nritems; i++) {
1446 u32 ioff = btrfs_item_offset(leaf->items + i);
1447 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
1449 btrfs_memmove(root, leaf, leaf->items + slot,
1450 leaf->items + slot + 1,
1451 sizeof(struct btrfs_item) *
1452 (nritems - slot - 1));
1454 btrfs_set_header_nritems(&leaf->header, nritems - 1);
1456 /* delete the leaf if we've emptied it */
1458 if (leaf_buf == root->node) {
1459 btrfs_set_header_level(&leaf->header, 0);
1461 clean_tree_block(trans, root, leaf_buf);
1462 wait_on_buffer(leaf_buf);
1463 wret = del_ptr(trans, root, path, 1, path->slots[1]);
1466 wret = btrfs_free_extent(trans, root,
1467 leaf_buf->b_blocknr, 1, 1);
1472 int used = leaf_space_used(leaf, 0, nritems);
1474 wret = fixup_low_keys(trans, root, path,
1475 &leaf->items[0].key, 1);
1480 /* delete the leaf if it is mostly empty */
1481 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
1482 /* push_leaf_left fixes the path.
1483 * make sure the path still points to our leaf
1484 * for possible call to del_ptr below
1486 slot = path->slots[1];
1488 wret = push_leaf_left(trans, root, path, 1);
1491 if (path->nodes[0] == leaf_buf &&
1492 btrfs_header_nritems(&leaf->header)) {
1493 wret = push_leaf_right(trans, root, path, 1);
1497 if (btrfs_header_nritems(&leaf->header) == 0) {
1498 u64 blocknr = leaf_buf->b_blocknr;
1499 clean_tree_block(trans, root, leaf_buf);
1500 wait_on_buffer(leaf_buf);
1501 wret = del_ptr(trans, root, path, 1, slot);
1504 btrfs_block_release(root, leaf_buf);
1505 wret = btrfs_free_extent(trans, root, blocknr,
1510 btrfs_mark_buffer_dirty(leaf_buf);
1511 btrfs_block_release(root, leaf_buf);
1514 btrfs_mark_buffer_dirty(leaf_buf);
1521 * walk up the tree as far as required to find the next leaf.
1522 * returns 0 if it found something or 1 if there are no greater leaves.
1523 * returns < 0 on io errors.
1525 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
1530 struct buffer_head *c;
1531 struct btrfs_node *c_node;
1532 struct buffer_head *next = NULL;
1534 while(level < BTRFS_MAX_LEVEL) {
1535 if (!path->nodes[level])
1537 slot = path->slots[level] + 1;
1538 c = path->nodes[level];
1539 c_node = btrfs_buffer_node(c);
1540 if (slot >= btrfs_header_nritems(&c_node->header)) {
1544 blocknr = btrfs_node_blockptr(c_node, slot);
1546 btrfs_block_release(root, next);
1547 next = read_tree_block(root, blocknr);
1550 path->slots[level] = slot;
1553 c = path->nodes[level];
1554 btrfs_block_release(root, c);
1555 path->nodes[level] = next;
1556 path->slots[level] = 0;
1559 next = read_tree_block(root,
1560 btrfs_node_blockptr(btrfs_buffer_node(next), 0));