2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
20 #include "transaction.h"
21 #include "print-tree.h"
23 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
24 *root, struct btrfs_path *path, int level);
25 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
26 *root, struct btrfs_key *ins_key,
27 struct btrfs_path *path, int data_size, int extend);
28 static int push_node_left(struct btrfs_trans_handle *trans,
29 struct btrfs_root *root, struct extent_buffer *dst,
30 struct extent_buffer *src);
31 static int balance_node_right(struct btrfs_trans_handle *trans,
32 struct btrfs_root *root,
33 struct extent_buffer *dst_buf,
34 struct extent_buffer *src_buf);
35 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
36 struct btrfs_path *path, int level, int slot);
38 inline void btrfs_init_path(struct btrfs_path *p)
40 memset(p, 0, sizeof(*p));
43 struct btrfs_path *btrfs_alloc_path(void)
45 struct btrfs_path *path;
46 path = kmalloc(sizeof(struct btrfs_path), GFP_NOFS);
48 btrfs_init_path(path);
54 void btrfs_free_path(struct btrfs_path *p)
56 btrfs_release_path(NULL, p);
60 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
63 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
66 free_extent_buffer(p->nodes[i]);
68 memset(p, 0, sizeof(*p));
71 int btrfs_copy_root(struct btrfs_trans_handle *trans,
72 struct btrfs_root *root,
73 struct extent_buffer *buf,
74 struct extent_buffer **cow_ret, u64 new_root_objectid)
76 struct extent_buffer *cow;
80 struct btrfs_key first_key;
81 struct btrfs_root *new_root;
83 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
87 memcpy(new_root, root, sizeof(*new_root));
88 new_root->root_key.objectid = new_root_objectid;
90 WARN_ON(root->ref_cows && trans->transid !=
91 root->fs_info->running_transaction->transid);
92 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
94 level = btrfs_header_level(buf);
95 nritems = btrfs_header_nritems(buf);
98 btrfs_item_key_to_cpu(buf, &first_key, 0);
100 btrfs_node_key_to_cpu(buf, &first_key, 0);
102 first_key.objectid = 0;
104 cow = __btrfs_alloc_free_block(trans, new_root, buf->len,
106 trans->transid, first_key.objectid,
107 level, buf->start, 0);
113 copy_extent_buffer(cow, buf, 0, 0, cow->len);
114 btrfs_set_header_bytenr(cow, cow->start);
115 btrfs_set_header_generation(cow, trans->transid);
116 btrfs_set_header_owner(cow, new_root_objectid);
118 WARN_ON(btrfs_header_generation(buf) > trans->transid);
119 ret = btrfs_inc_ref(trans, new_root, buf);
125 btrfs_mark_buffer_dirty(cow);
130 int __btrfs_cow_block(struct btrfs_trans_handle *trans,
131 struct btrfs_root *root,
132 struct extent_buffer *buf,
133 struct extent_buffer *parent, int parent_slot,
134 struct extent_buffer **cow_ret,
135 u64 search_start, u64 empty_size)
138 struct extent_buffer *cow;
141 int different_trans = 0;
143 struct btrfs_key first_key;
145 if (root->ref_cows) {
146 root_gen = trans->transid;
151 WARN_ON(root->ref_cows && trans->transid !=
152 root->fs_info->running_transaction->transid);
153 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
155 level = btrfs_header_level(buf);
156 nritems = btrfs_header_nritems(buf);
159 btrfs_item_key_to_cpu(buf, &first_key, 0);
161 btrfs_node_key_to_cpu(buf, &first_key, 0);
163 first_key.objectid = 0;
165 cow = __btrfs_alloc_free_block(trans, root, buf->len,
166 root->root_key.objectid,
167 root_gen, first_key.objectid, level,
168 search_start, empty_size);
172 copy_extent_buffer(cow, buf, 0, 0, cow->len);
173 btrfs_set_header_bytenr(cow, cow->start);
174 btrfs_set_header_generation(cow, trans->transid);
175 btrfs_set_header_owner(cow, root->root_key.objectid);
177 WARN_ON(btrfs_header_generation(buf) > trans->transid);
178 if (btrfs_header_generation(buf) != trans->transid) {
180 ret = btrfs_inc_ref(trans, root, buf);
184 clean_tree_block(trans, root, buf);
187 if (buf == root->node) {
188 root_gen = btrfs_header_generation(buf);
190 extent_buffer_get(cow);
191 if (buf != root->commit_root) {
192 btrfs_free_extent(trans, root, buf->start,
193 buf->len, root->root_key.objectid,
196 free_extent_buffer(buf);
198 root_gen = btrfs_header_generation(parent);
199 btrfs_set_node_blockptr(parent, parent_slot,
201 WARN_ON(trans->transid == 0);
202 btrfs_set_node_ptr_generation(parent, parent_slot,
204 btrfs_mark_buffer_dirty(parent);
205 WARN_ON(btrfs_header_generation(parent) != trans->transid);
206 btrfs_free_extent(trans, root, buf->start, buf->len,
207 btrfs_header_owner(parent), root_gen,
210 free_extent_buffer(buf);
211 btrfs_mark_buffer_dirty(cow);
216 int btrfs_cow_block(struct btrfs_trans_handle *trans,
217 struct btrfs_root *root, struct extent_buffer *buf,
218 struct extent_buffer *parent, int parent_slot,
219 struct extent_buffer **cow_ret)
224 if (trans->transaction != root->fs_info->running_transaction) {
225 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
226 root->fs_info->running_transaction->transid);
230 if (trans->transid != root->fs_info->generation) {
231 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
232 root->fs_info->generation);
235 if (btrfs_header_generation(buf) == trans->transid) {
240 search_start = buf->start & ~((u64)BTRFS_BLOCK_GROUP_SIZE - 1);
241 ret = __btrfs_cow_block(trans, root, buf, parent,
242 parent_slot, cow_ret, search_start, 0);
247 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
249 if (blocknr < other && other - (blocknr + blocksize) < 32768)
251 if (blocknr > other && blocknr - (other + blocksize) < 32768)
258 * compare two keys in a memcmp fashion
260 int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
264 btrfs_disk_key_to_cpu(&k1, disk);
266 if (k1.objectid > k2->objectid)
268 if (k1.objectid < k2->objectid)
270 if (k1.type > k2->type)
272 if (k1.type < k2->type)
274 if (k1.offset > k2->offset)
276 if (k1.offset < k2->offset)
283 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
284 struct btrfs_root *root, struct extent_buffer *parent,
285 int start_slot, int cache_only, u64 *last_ret,
286 struct btrfs_key *progress)
288 struct extent_buffer *cur;
289 struct extent_buffer *tmp;
291 u64 search_start = *last_ret;
301 int progress_passed = 0;
302 struct btrfs_disk_key disk_key;
304 parent_level = btrfs_header_level(parent);
305 if (cache_only && parent_level != 1)
308 if (trans->transaction != root->fs_info->running_transaction) {
309 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
310 root->fs_info->running_transaction->transid);
313 if (trans->transid != root->fs_info->generation) {
314 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
315 root->fs_info->generation);
319 parent_nritems = btrfs_header_nritems(parent);
320 blocksize = btrfs_level_size(root, parent_level - 1);
321 end_slot = parent_nritems;
323 if (parent_nritems == 1)
326 for (i = start_slot; i < end_slot; i++) {
329 if (!parent->map_token) {
330 map_extent_buffer(parent,
331 btrfs_node_key_ptr_offset(i),
332 sizeof(struct btrfs_key_ptr),
333 &parent->map_token, &parent->kaddr,
334 &parent->map_start, &parent->map_len,
337 btrfs_node_key(parent, &disk_key, i);
338 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
342 blocknr = btrfs_node_blockptr(parent, i);
344 last_block = blocknr;
347 other = btrfs_node_blockptr(parent, i - 1);
348 close = close_blocks(blocknr, other, blocksize);
350 if (close && i < end_slot - 2) {
351 other = btrfs_node_blockptr(parent, i + 1);
352 close = close_blocks(blocknr, other, blocksize);
355 last_block = blocknr;
358 if (parent->map_token) {
359 unmap_extent_buffer(parent, parent->map_token,
361 parent->map_token = NULL;
364 cur = btrfs_find_tree_block(root, blocknr, blocksize);
366 uptodate = btrfs_buffer_uptodate(cur);
369 if (!cur || !uptodate) {
371 free_extent_buffer(cur);
375 cur = read_tree_block(root, blocknr,
377 } else if (!uptodate) {
378 btrfs_read_buffer(cur);
381 if (search_start == 0)
382 search_start = last_block;
384 err = __btrfs_cow_block(trans, root, cur, parent, i,
387 (end_slot - i) * blocksize));
389 free_extent_buffer(cur);
392 search_start = tmp->start;
393 last_block = tmp->start;
394 *last_ret = search_start;
395 if (parent_level == 1)
396 btrfs_clear_buffer_defrag(tmp);
397 free_extent_buffer(tmp);
399 if (parent->map_token) {
400 unmap_extent_buffer(parent, parent->map_token,
402 parent->map_token = NULL;
409 * The leaf data grows from end-to-front in the node.
410 * this returns the address of the start of the last item,
411 * which is the stop of the leaf data stack
413 static inline unsigned int leaf_data_end(struct btrfs_root *root,
414 struct extent_buffer *leaf)
416 u32 nr = btrfs_header_nritems(leaf);
418 return BTRFS_LEAF_DATA_SIZE(root);
419 return btrfs_item_offset_nr(leaf, nr - 1);
422 static int check_node(struct btrfs_root *root, struct btrfs_path *path,
425 struct extent_buffer *parent = NULL;
426 struct extent_buffer *node = path->nodes[level];
427 struct btrfs_disk_key parent_key;
428 struct btrfs_disk_key node_key;
431 struct btrfs_key cpukey;
432 u32 nritems = btrfs_header_nritems(node);
434 if (path->nodes[level + 1])
435 parent = path->nodes[level + 1];
437 slot = path->slots[level];
438 BUG_ON(nritems == 0);
440 parent_slot = path->slots[level + 1];
441 btrfs_node_key(parent, &parent_key, parent_slot);
442 btrfs_node_key(node, &node_key, 0);
443 BUG_ON(memcmp(&parent_key, &node_key,
444 sizeof(struct btrfs_disk_key)));
445 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
446 btrfs_header_bytenr(node));
448 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
450 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
451 btrfs_node_key(node, &node_key, slot);
452 BUG_ON(btrfs_comp_keys(&node_key, &cpukey) <= 0);
454 if (slot < nritems - 1) {
455 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
456 btrfs_node_key(node, &node_key, slot);
457 BUG_ON(btrfs_comp_keys(&node_key, &cpukey) >= 0);
462 static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
465 struct extent_buffer *leaf = path->nodes[level];
466 struct extent_buffer *parent = NULL;
468 struct btrfs_key cpukey;
469 struct btrfs_disk_key parent_key;
470 struct btrfs_disk_key leaf_key;
471 int slot = path->slots[0];
473 u32 nritems = btrfs_header_nritems(leaf);
475 if (path->nodes[level + 1])
476 parent = path->nodes[level + 1];
482 parent_slot = path->slots[level + 1];
483 btrfs_node_key(parent, &parent_key, parent_slot);
484 btrfs_item_key(leaf, &leaf_key, 0);
486 BUG_ON(memcmp(&parent_key, &leaf_key,
487 sizeof(struct btrfs_disk_key)));
488 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
489 btrfs_header_bytenr(leaf));
492 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
493 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
494 btrfs_item_key(leaf, &leaf_key, i);
495 if (comp_keys(&leaf_key, &cpukey) >= 0) {
496 btrfs_print_leaf(root, leaf);
497 printk("slot %d offset bad key\n", i);
500 if (btrfs_item_offset_nr(leaf, i) !=
501 btrfs_item_end_nr(leaf, i + 1)) {
502 btrfs_print_leaf(root, leaf);
503 printk("slot %d offset bad\n", i);
507 if (btrfs_item_offset_nr(leaf, i) +
508 btrfs_item_size_nr(leaf, i) !=
509 BTRFS_LEAF_DATA_SIZE(root)) {
510 btrfs_print_leaf(root, leaf);
511 printk("slot %d first offset bad\n", i);
517 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
518 btrfs_print_leaf(root, leaf);
519 printk("slot %d bad size \n", nritems - 1);
524 if (slot != 0 && slot < nritems - 1) {
525 btrfs_item_key(leaf, &leaf_key, slot);
526 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
527 if (btrfs_comp_keys(&leaf_key, &cpukey) <= 0) {
528 btrfs_print_leaf(root, leaf);
529 printk("slot %d offset bad key\n", slot);
532 if (btrfs_item_offset_nr(leaf, slot - 1) !=
533 btrfs_item_end_nr(leaf, slot)) {
534 btrfs_print_leaf(root, leaf);
535 printk("slot %d offset bad\n", slot);
539 if (slot < nritems - 1) {
540 btrfs_item_key(leaf, &leaf_key, slot);
541 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
542 BUG_ON(btrfs_comp_keys(&leaf_key, &cpukey) >= 0);
543 if (btrfs_item_offset_nr(leaf, slot) !=
544 btrfs_item_end_nr(leaf, slot + 1)) {
545 btrfs_print_leaf(root, leaf);
546 printk("slot %d offset bad\n", slot);
550 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
551 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
555 static int noinline check_block(struct btrfs_root *root,
556 struct btrfs_path *path, int level)
560 struct extent_buffer *buf = path->nodes[level];
562 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
563 (unsigned long)btrfs_header_fsid(buf),
565 printk("warning bad block %Lu\n", buf->start);
570 return check_leaf(root, path, level);
571 return check_node(root, path, level);
575 * search for key in the extent_buffer. The items start at offset p,
576 * and they are item_size apart. There are 'max' items in p.
578 * the slot in the array is returned via slot, and it points to
579 * the place where you would insert key if it is not found in
582 * slot may point to max if the key is bigger than all of the keys
584 static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
585 int item_size, struct btrfs_key *key,
592 unsigned long offset;
593 struct btrfs_disk_key *tmp;
596 mid = (low + high) / 2;
597 offset = p + mid * item_size;
599 tmp = (struct btrfs_disk_key *)(eb->data + offset);
600 ret = btrfs_comp_keys(tmp, key);
616 * simple bin_search frontend that does the right thing for
619 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
620 int level, int *slot)
623 return generic_bin_search(eb,
624 offsetof(struct btrfs_leaf, items),
625 sizeof(struct btrfs_item),
626 key, btrfs_header_nritems(eb),
629 return generic_bin_search(eb,
630 offsetof(struct btrfs_node, ptrs),
631 sizeof(struct btrfs_key_ptr),
632 key, btrfs_header_nritems(eb),
638 static struct extent_buffer *read_node_slot(struct btrfs_root *root,
639 struct extent_buffer *parent, int slot)
643 if (slot >= btrfs_header_nritems(parent))
645 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
646 btrfs_level_size(root, btrfs_header_level(parent) - 1));
649 static int balance_level(struct btrfs_trans_handle *trans,
650 struct btrfs_root *root,
651 struct btrfs_path *path, int level)
653 struct extent_buffer *right = NULL;
654 struct extent_buffer *mid;
655 struct extent_buffer *left = NULL;
656 struct extent_buffer *parent = NULL;
660 int orig_slot = path->slots[level];
661 int err_on_enospc = 0;
667 mid = path->nodes[level];
668 WARN_ON(btrfs_header_generation(mid) != trans->transid);
670 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
672 if (level < BTRFS_MAX_LEVEL - 1)
673 parent = path->nodes[level + 1];
674 pslot = path->slots[level + 1];
677 * deal with the case where there is only one pointer in the root
678 * by promoting the node below to a root
681 struct extent_buffer *child;
683 if (btrfs_header_nritems(mid) != 1)
686 /* promote the child to a root */
687 child = read_node_slot(root, mid, 0);
690 path->nodes[level] = NULL;
691 clean_tree_block(trans, root, mid);
692 wait_on_tree_block_writeback(root, mid);
693 /* once for the path */
694 free_extent_buffer(mid);
695 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
696 root->root_key.objectid,
697 btrfs_header_generation(mid), 0, 0, 1);
698 /* once for the root ptr */
699 free_extent_buffer(mid);
702 if (btrfs_header_nritems(mid) >
703 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
706 if (btrfs_header_nritems(mid) < 2)
709 left = read_node_slot(root, parent, pslot - 1);
711 wret = btrfs_cow_block(trans, root, left,
712 parent, pslot - 1, &left);
718 right = read_node_slot(root, parent, pslot + 1);
720 wret = btrfs_cow_block(trans, root, right,
721 parent, pslot + 1, &right);
728 /* first, try to make some room in the middle buffer */
730 orig_slot += btrfs_header_nritems(left);
731 wret = push_node_left(trans, root, left, mid);
734 if (btrfs_header_nritems(mid) < 2)
739 * then try to empty the right most buffer into the middle
742 wret = push_node_left(trans, root, mid, right);
743 if (wret < 0 && wret != -ENOSPC)
745 if (btrfs_header_nritems(right) == 0) {
746 u64 bytenr = right->start;
747 u64 generation = btrfs_header_generation(parent);
748 u32 blocksize = right->len;
750 clean_tree_block(trans, root, right);
751 wait_on_tree_block_writeback(root, right);
752 free_extent_buffer(right);
754 wret = del_ptr(trans, root, path, level + 1, pslot +
758 wret = btrfs_free_extent(trans, root, bytenr,
760 btrfs_header_owner(parent),
761 generation, 0, 0, 1);
765 struct btrfs_disk_key right_key;
766 btrfs_node_key(right, &right_key, 0);
767 btrfs_set_node_key(parent, &right_key, pslot + 1);
768 btrfs_mark_buffer_dirty(parent);
771 if (btrfs_header_nritems(mid) == 1) {
773 * we're not allowed to leave a node with one item in the
774 * tree during a delete. A deletion from lower in the tree
775 * could try to delete the only pointer in this node.
776 * So, pull some keys from the left.
777 * There has to be a left pointer at this point because
778 * otherwise we would have pulled some pointers from the
782 wret = balance_node_right(trans, root, mid, left);
789 if (btrfs_header_nritems(mid) == 0) {
790 /* we've managed to empty the middle node, drop it */
791 u64 root_gen = btrfs_header_generation(parent);
792 u64 bytenr = mid->start;
793 u32 blocksize = mid->len;
794 clean_tree_block(trans, root, mid);
795 wait_on_tree_block_writeback(root, mid);
796 free_extent_buffer(mid);
798 wret = del_ptr(trans, root, path, level + 1, pslot);
801 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
802 btrfs_header_owner(parent),
807 /* update the parent key to reflect our changes */
808 struct btrfs_disk_key mid_key;
809 btrfs_node_key(mid, &mid_key, 0);
810 btrfs_set_node_key(parent, &mid_key, pslot);
811 btrfs_mark_buffer_dirty(parent);
814 /* update the path */
816 if (btrfs_header_nritems(left) > orig_slot) {
817 extent_buffer_get(left);
818 path->nodes[level] = left;
819 path->slots[level + 1] -= 1;
820 path->slots[level] = orig_slot;
822 free_extent_buffer(mid);
824 orig_slot -= btrfs_header_nritems(left);
825 path->slots[level] = orig_slot;
828 /* double check we haven't messed things up */
829 check_block(root, path, level);
831 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
835 free_extent_buffer(right);
837 free_extent_buffer(left);
841 /* returns zero if the push worked, non-zero otherwise */
842 static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
843 struct btrfs_root *root,
844 struct btrfs_path *path, int level)
846 struct extent_buffer *right = NULL;
847 struct extent_buffer *mid;
848 struct extent_buffer *left = NULL;
849 struct extent_buffer *parent = NULL;
853 int orig_slot = path->slots[level];
859 mid = path->nodes[level];
860 WARN_ON(btrfs_header_generation(mid) != trans->transid);
861 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
863 if (level < BTRFS_MAX_LEVEL - 1)
864 parent = path->nodes[level + 1];
865 pslot = path->slots[level + 1];
870 left = read_node_slot(root, parent, pslot - 1);
872 /* first, try to make some room in the middle buffer */
875 left_nr = btrfs_header_nritems(left);
876 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
879 ret = btrfs_cow_block(trans, root, left, parent,
884 wret = push_node_left(trans, root,
891 struct btrfs_disk_key disk_key;
892 orig_slot += left_nr;
893 btrfs_node_key(mid, &disk_key, 0);
894 btrfs_set_node_key(parent, &disk_key, pslot);
895 btrfs_mark_buffer_dirty(parent);
896 if (btrfs_header_nritems(left) > orig_slot) {
897 path->nodes[level] = left;
898 path->slots[level + 1] -= 1;
899 path->slots[level] = orig_slot;
900 free_extent_buffer(mid);
903 btrfs_header_nritems(left);
904 path->slots[level] = orig_slot;
905 free_extent_buffer(left);
909 free_extent_buffer(left);
911 right= read_node_slot(root, parent, pslot + 1);
914 * then try to empty the right most buffer into the middle
918 right_nr = btrfs_header_nritems(right);
919 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
922 ret = btrfs_cow_block(trans, root, right,
928 wret = balance_node_right(trans, root,
935 struct btrfs_disk_key disk_key;
937 btrfs_node_key(right, &disk_key, 0);
938 btrfs_set_node_key(parent, &disk_key, pslot + 1);
939 btrfs_mark_buffer_dirty(parent);
941 if (btrfs_header_nritems(mid) <= orig_slot) {
942 path->nodes[level] = right;
943 path->slots[level + 1] += 1;
944 path->slots[level] = orig_slot -
945 btrfs_header_nritems(mid);
946 free_extent_buffer(mid);
948 free_extent_buffer(right);
952 free_extent_buffer(right);
958 * readahead one full node of leaves
960 static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
961 int level, int slot, u64 objectid)
963 struct extent_buffer *node;
964 struct btrfs_disk_key disk_key;
970 int direction = path->reada;
971 struct extent_buffer *eb;
979 if (!path->nodes[level])
982 node = path->nodes[level];
983 search = btrfs_node_blockptr(node, slot);
984 blocksize = btrfs_level_size(root, level - 1);
985 eb = btrfs_find_tree_block(root, search, blocksize);
987 free_extent_buffer(eb);
991 highest_read = search;
992 lowest_read = search;
994 nritems = btrfs_header_nritems(node);
1001 } else if (direction > 0) {
1006 if (path->reada < 0 && objectid) {
1007 btrfs_node_key(node, &disk_key, nr);
1008 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1011 search = btrfs_node_blockptr(node, nr);
1012 if ((search >= lowest_read && search <= highest_read) ||
1013 (search < lowest_read && lowest_read - search <= 32768) ||
1014 (search > highest_read && search - highest_read <= 32768)) {
1015 readahead_tree_block(root, search, blocksize);
1019 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1021 if(nread > (1024 * 1024) || nscan > 128)
1024 if (search < lowest_read)
1025 lowest_read = search;
1026 if (search > highest_read)
1027 highest_read = search;
1032 * look for key in the tree. path is filled in with nodes along the way
1033 * if key is found, we return zero and you can find the item in the leaf
1034 * level of the path (level 0)
1036 * If the key isn't found, the path points to the slot where it should
1037 * be inserted, and 1 is returned. If there are other errors during the
1038 * search a negative error number is returned.
1040 * if ins_len > 0, nodes and leaves will be split as we walk down the
1041 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1044 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1045 *root, struct btrfs_key *key, struct btrfs_path *p, int
1048 struct extent_buffer *b;
1054 int should_reada = p->reada;
1055 u8 lowest_level = 0;
1057 lowest_level = p->lowest_level;
1058 WARN_ON(lowest_level && ins_len);
1059 WARN_ON(p->nodes[0] != NULL);
1061 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1065 extent_buffer_get(b);
1067 level = btrfs_header_level(b);
1070 wret = btrfs_cow_block(trans, root, b,
1071 p->nodes[level + 1],
1072 p->slots[level + 1],
1075 free_extent_buffer(b);
1079 BUG_ON(!cow && ins_len);
1080 if (level != btrfs_header_level(b))
1082 level = btrfs_header_level(b);
1083 p->nodes[level] = b;
1084 ret = check_block(root, p, level);
1087 ret = bin_search(b, key, level, &slot);
1089 if (ret && slot > 0)
1091 p->slots[level] = slot;
1092 if (ins_len > 0 && btrfs_header_nritems(b) >=
1093 BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1094 int sret = split_node(trans, root, p, level);
1098 b = p->nodes[level];
1099 slot = p->slots[level];
1100 } else if (ins_len < 0) {
1101 int sret = balance_level(trans, root, p,
1105 b = p->nodes[level];
1107 btrfs_release_path(NULL, p);
1110 slot = p->slots[level];
1111 BUG_ON(btrfs_header_nritems(b) == 1);
1113 /* this is only true while dropping a snapshot */
1114 if (level == lowest_level)
1116 bytenr = btrfs_node_blockptr(b, slot);
1117 ptr_gen = btrfs_node_ptr_generation(b, slot);
1119 reada_for_search(root, p, level, slot,
1121 b = read_tree_block(root, bytenr,
1122 btrfs_level_size(root, level - 1));
1123 if (ptr_gen != btrfs_header_generation(b)) {
1124 printk("block %llu bad gen wanted %llu "
1126 (unsigned long long)b->start,
1127 (unsigned long long)ptr_gen,
1128 (unsigned long long)btrfs_header_generation(b));
1131 p->slots[level] = slot;
1132 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
1133 sizeof(struct btrfs_item) + ins_len) {
1134 int sret = split_leaf(trans, root, key,
1135 p, ins_len, ret == 0);
1147 * adjust the pointers going up the tree, starting at level
1148 * making sure the right key of each node is points to 'key'.
1149 * This is used after shifting pointers to the left, so it stops
1150 * fixing up pointers when a given leaf/node is not in slot 0 of the
1153 * If this fails to write a tree block, it returns -1, but continues
1154 * fixing up the blocks in ram so the tree is consistent.
1156 static int fixup_low_keys(struct btrfs_trans_handle *trans,
1157 struct btrfs_root *root, struct btrfs_path *path,
1158 struct btrfs_disk_key *key, int level)
1162 struct extent_buffer *t;
1164 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1165 int tslot = path->slots[i];
1166 if (!path->nodes[i])
1169 btrfs_set_node_key(t, key, tslot);
1170 btrfs_mark_buffer_dirty(path->nodes[i]);
1178 * try to push data from one node into the next node left in the
1181 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1182 * error, and > 0 if there was no room in the left hand block.
1184 static int push_node_left(struct btrfs_trans_handle *trans,
1185 struct btrfs_root *root, struct extent_buffer *dst,
1186 struct extent_buffer *src)
1193 src_nritems = btrfs_header_nritems(src);
1194 dst_nritems = btrfs_header_nritems(dst);
1195 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1196 WARN_ON(btrfs_header_generation(src) != trans->transid);
1197 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1199 if (push_items <= 0) {
1203 if (src_nritems < push_items)
1204 push_items = src_nritems;
1206 copy_extent_buffer(dst, src,
1207 btrfs_node_key_ptr_offset(dst_nritems),
1208 btrfs_node_key_ptr_offset(0),
1209 push_items * sizeof(struct btrfs_key_ptr));
1211 if (push_items < src_nritems) {
1212 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1213 btrfs_node_key_ptr_offset(push_items),
1214 (src_nritems - push_items) *
1215 sizeof(struct btrfs_key_ptr));
1217 btrfs_set_header_nritems(src, src_nritems - push_items);
1218 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1219 btrfs_mark_buffer_dirty(src);
1220 btrfs_mark_buffer_dirty(dst);
1225 * try to push data from one node into the next node right in the
1228 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1229 * error, and > 0 if there was no room in the right hand block.
1231 * this will only push up to 1/2 the contents of the left node over
1233 static int balance_node_right(struct btrfs_trans_handle *trans,
1234 struct btrfs_root *root,
1235 struct extent_buffer *dst,
1236 struct extent_buffer *src)
1244 WARN_ON(btrfs_header_generation(src) != trans->transid);
1245 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1247 src_nritems = btrfs_header_nritems(src);
1248 dst_nritems = btrfs_header_nritems(dst);
1249 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1250 if (push_items <= 0)
1253 max_push = src_nritems / 2 + 1;
1254 /* don't try to empty the node */
1255 if (max_push >= src_nritems)
1258 if (max_push < push_items)
1259 push_items = max_push;
1261 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1262 btrfs_node_key_ptr_offset(0),
1264 sizeof(struct btrfs_key_ptr));
1266 copy_extent_buffer(dst, src,
1267 btrfs_node_key_ptr_offset(0),
1268 btrfs_node_key_ptr_offset(src_nritems - push_items),
1269 push_items * sizeof(struct btrfs_key_ptr));
1271 btrfs_set_header_nritems(src, src_nritems - push_items);
1272 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1274 btrfs_mark_buffer_dirty(src);
1275 btrfs_mark_buffer_dirty(dst);
1280 * helper function to insert a new root level in the tree.
1281 * A new node is allocated, and a single item is inserted to
1282 * point to the existing root
1284 * returns zero on success or < 0 on failure.
1286 static int noinline insert_new_root(struct btrfs_trans_handle *trans,
1287 struct btrfs_root *root,
1288 struct btrfs_path *path, int level)
1292 struct extent_buffer *lower;
1293 struct extent_buffer *c;
1294 struct btrfs_disk_key lower_key;
1296 BUG_ON(path->nodes[level]);
1297 BUG_ON(path->nodes[level-1] != root->node);
1300 root_gen = trans->transid;
1304 lower = path->nodes[level-1];
1306 btrfs_item_key(lower, &lower_key, 0);
1308 btrfs_node_key(lower, &lower_key, 0);
1310 c = __btrfs_alloc_free_block(trans, root, root->nodesize,
1311 root->root_key.objectid,
1312 root_gen, lower_key.objectid, level,
1313 root->node->start, 0);
1316 memset_extent_buffer(c, 0, 0, root->nodesize);
1317 btrfs_set_header_nritems(c, 1);
1318 btrfs_set_header_level(c, level);
1319 btrfs_set_header_bytenr(c, c->start);
1320 btrfs_set_header_generation(c, trans->transid);
1321 btrfs_set_header_owner(c, root->root_key.objectid);
1323 write_extent_buffer(c, root->fs_info->fsid,
1324 (unsigned long)btrfs_header_fsid(c),
1326 btrfs_set_node_key(c, &lower_key, 0);
1327 btrfs_set_node_blockptr(c, 0, lower->start);
1328 lower_gen = btrfs_header_generation(lower);
1329 WARN_ON(lower_gen == 0);
1331 btrfs_set_node_ptr_generation(c, 0, lower_gen);
1333 btrfs_mark_buffer_dirty(c);
1335 /* the super has an extra ref to root->node */
1336 free_extent_buffer(root->node);
1338 extent_buffer_get(c);
1339 path->nodes[level] = c;
1340 path->slots[level] = 0;
1342 if (root->ref_cows && lower_gen != trans->transid) {
1343 struct btrfs_path *back_path = btrfs_alloc_path();
1345 ret = btrfs_insert_extent_backref(trans,
1346 root->fs_info->extent_root,
1348 root->root_key.objectid,
1349 trans->transid, 0, 0);
1351 btrfs_free_path(back_path);
1357 * worker function to insert a single pointer in a node.
1358 * the node should have enough room for the pointer already
1360 * slot and level indicate where you want the key to go, and
1361 * blocknr is the block the key points to.
1363 * returns zero on success and < 0 on any error
1365 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1366 *root, struct btrfs_path *path, struct btrfs_disk_key
1367 *key, u64 bytenr, int slot, int level)
1369 struct extent_buffer *lower;
1372 BUG_ON(!path->nodes[level]);
1373 lower = path->nodes[level];
1374 nritems = btrfs_header_nritems(lower);
1377 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
1379 if (slot != nritems) {
1380 memmove_extent_buffer(lower,
1381 btrfs_node_key_ptr_offset(slot + 1),
1382 btrfs_node_key_ptr_offset(slot),
1383 (nritems - slot) * sizeof(struct btrfs_key_ptr));
1385 btrfs_set_node_key(lower, key, slot);
1386 btrfs_set_node_blockptr(lower, slot, bytenr);
1387 WARN_ON(trans->transid == 0);
1388 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
1389 btrfs_set_header_nritems(lower, nritems + 1);
1390 btrfs_mark_buffer_dirty(lower);
1395 * split the node at the specified level in path in two.
1396 * The path is corrected to point to the appropriate node after the split
1398 * Before splitting this tries to make some room in the node by pushing
1399 * left and right, if either one works, it returns right away.
1401 * returns 0 on success and < 0 on failure
1403 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1404 *root, struct btrfs_path *path, int level)
1407 struct extent_buffer *c;
1408 struct extent_buffer *split;
1409 struct btrfs_disk_key disk_key;
1415 c = path->nodes[level];
1416 WARN_ON(btrfs_header_generation(c) != trans->transid);
1417 if (c == root->node) {
1418 /* trying to split the root, lets make a new one */
1419 ret = insert_new_root(trans, root, path, level + 1);
1423 ret = push_nodes_for_insert(trans, root, path, level);
1424 c = path->nodes[level];
1425 if (!ret && btrfs_header_nritems(c) <
1426 BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
1432 c_nritems = btrfs_header_nritems(c);
1434 root_gen = trans->transid;
1438 btrfs_node_key(c, &disk_key, 0);
1439 split = __btrfs_alloc_free_block(trans, root, root->nodesize,
1440 root->root_key.objectid,
1442 btrfs_disk_key_objectid(&disk_key),
1443 level, c->start, 0);
1445 return PTR_ERR(split);
1447 btrfs_set_header_flags(split, btrfs_header_flags(c));
1448 btrfs_set_header_level(split, btrfs_header_level(c));
1449 btrfs_set_header_bytenr(split, split->start);
1450 btrfs_set_header_generation(split, trans->transid);
1451 btrfs_set_header_owner(split, root->root_key.objectid);
1452 write_extent_buffer(split, root->fs_info->fsid,
1453 (unsigned long)btrfs_header_fsid(split),
1456 mid = (c_nritems + 1) / 2;
1458 copy_extent_buffer(split, c,
1459 btrfs_node_key_ptr_offset(0),
1460 btrfs_node_key_ptr_offset(mid),
1461 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1462 btrfs_set_header_nritems(split, c_nritems - mid);
1463 btrfs_set_header_nritems(c, mid);
1466 btrfs_mark_buffer_dirty(c);
1467 btrfs_mark_buffer_dirty(split);
1469 btrfs_node_key(split, &disk_key, 0);
1470 wret = insert_ptr(trans, root, path, &disk_key, split->start,
1471 path->slots[level + 1] + 1,
1476 if (path->slots[level] >= mid) {
1477 path->slots[level] -= mid;
1478 free_extent_buffer(c);
1479 path->nodes[level] = split;
1480 path->slots[level + 1] += 1;
1482 free_extent_buffer(split);
1488 * how many bytes are required to store the items in a leaf. start
1489 * and nr indicate which items in the leaf to check. This totals up the
1490 * space used both by the item structs and the item data
1492 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
1495 int nritems = btrfs_header_nritems(l);
1496 int end = min(nritems, start + nr) - 1;
1500 data_len = btrfs_item_end_nr(l, start);
1501 data_len = data_len - btrfs_item_offset_nr(l, end);
1502 data_len += sizeof(struct btrfs_item) * nr;
1503 WARN_ON(data_len < 0);
1508 * The space between the end of the leaf items and
1509 * the start of the leaf data. IOW, how much room
1510 * the leaf has left for both items and data
1512 int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
1514 int nritems = btrfs_header_nritems(leaf);
1516 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1518 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
1519 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
1520 leaf_space_used(leaf, 0, nritems), nritems);
1526 * push some data in the path leaf to the right, trying to free up at
1527 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1529 * returns 1 if the push failed because the other node didn't have enough
1530 * room, 0 if everything worked out and < 0 if there were major errors.
1532 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1533 *root, struct btrfs_path *path, int data_size,
1536 struct extent_buffer *left = path->nodes[0];
1537 struct extent_buffer *right;
1538 struct extent_buffer *upper;
1539 struct btrfs_disk_key disk_key;
1545 struct btrfs_item *item;
1553 slot = path->slots[1];
1554 if (!path->nodes[1]) {
1557 upper = path->nodes[1];
1558 if (slot >= btrfs_header_nritems(upper) - 1)
1561 right = read_tree_block(root, btrfs_node_blockptr(upper, slot + 1),
1563 free_space = btrfs_leaf_free_space(root, right);
1564 if (free_space < data_size + sizeof(struct btrfs_item)) {
1565 free_extent_buffer(right);
1569 /* cow and double check */
1570 ret = btrfs_cow_block(trans, root, right, upper,
1573 free_extent_buffer(right);
1576 free_space = btrfs_leaf_free_space(root, right);
1577 if (free_space < data_size + sizeof(struct btrfs_item)) {
1578 free_extent_buffer(right);
1582 left_nritems = btrfs_header_nritems(left);
1583 if (left_nritems == 0) {
1584 free_extent_buffer(right);
1593 i = left_nritems - 1;
1595 item = btrfs_item_nr(left, i);
1597 if (path->slots[0] == i)
1598 push_space += data_size + sizeof(*item);
1600 this_item_size = btrfs_item_size(left, item);
1601 if (this_item_size + sizeof(*item) + push_space > free_space)
1604 push_space += this_item_size + sizeof(*item);
1610 if (push_items == 0) {
1611 free_extent_buffer(right);
1615 if (!empty && push_items == left_nritems)
1618 /* push left to right */
1619 right_nritems = btrfs_header_nritems(right);
1621 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
1622 push_space -= leaf_data_end(root, left);
1624 /* make room in the right data area */
1625 data_end = leaf_data_end(root, right);
1626 memmove_extent_buffer(right,
1627 btrfs_leaf_data(right) + data_end - push_space,
1628 btrfs_leaf_data(right) + data_end,
1629 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1631 /* copy from the left data area */
1632 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
1633 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1634 btrfs_leaf_data(left) + leaf_data_end(root, left),
1637 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1638 btrfs_item_nr_offset(0),
1639 right_nritems * sizeof(struct btrfs_item));
1641 /* copy the items from left to right */
1642 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1643 btrfs_item_nr_offset(left_nritems - push_items),
1644 push_items * sizeof(struct btrfs_item));
1646 /* update the item pointers */
1647 right_nritems += push_items;
1648 btrfs_set_header_nritems(right, right_nritems);
1649 push_space = BTRFS_LEAF_DATA_SIZE(root);
1650 for (i = 0; i < right_nritems; i++) {
1651 item = btrfs_item_nr(right, i);
1652 push_space -= btrfs_item_size(right, item);
1653 btrfs_set_item_offset(right, item, push_space);
1656 left_nritems -= push_items;
1657 btrfs_set_header_nritems(left, left_nritems);
1660 btrfs_mark_buffer_dirty(left);
1661 btrfs_mark_buffer_dirty(right);
1663 btrfs_item_key(right, &disk_key, 0);
1664 btrfs_set_node_key(upper, &disk_key, slot + 1);
1665 btrfs_mark_buffer_dirty(upper);
1667 /* then fixup the leaf pointer in the path */
1668 if (path->slots[0] >= left_nritems) {
1669 path->slots[0] -= left_nritems;
1670 free_extent_buffer(path->nodes[0]);
1671 path->nodes[0] = right;
1672 path->slots[1] += 1;
1674 free_extent_buffer(right);
1679 * push some data in the path leaf to the left, trying to free up at
1680 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1682 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1683 *root, struct btrfs_path *path, int data_size,
1686 struct btrfs_disk_key disk_key;
1687 struct extent_buffer *right = path->nodes[0];
1688 struct extent_buffer *left;
1694 struct btrfs_item *item;
1695 u32 old_left_nritems;
1701 u32 old_left_item_size;
1703 slot = path->slots[1];
1706 if (!path->nodes[1])
1709 right_nritems = btrfs_header_nritems(right);
1710 if (right_nritems == 0) {
1714 left = read_tree_block(root, btrfs_node_blockptr(path->nodes[1],
1715 slot - 1), root->leafsize);
1716 free_space = btrfs_leaf_free_space(root, left);
1717 if (free_space < data_size + sizeof(struct btrfs_item)) {
1718 free_extent_buffer(left);
1722 /* cow and double check */
1723 ret = btrfs_cow_block(trans, root, left,
1724 path->nodes[1], slot - 1, &left);
1726 /* we hit -ENOSPC, but it isn't fatal here */
1727 free_extent_buffer(left);
1731 free_space = btrfs_leaf_free_space(root, left);
1732 if (free_space < data_size + sizeof(struct btrfs_item)) {
1733 free_extent_buffer(left);
1740 nr = right_nritems - 1;
1742 for (i = 0; i < nr; i++) {
1743 item = btrfs_item_nr(right, i);
1745 if (path->slots[0] == i)
1746 push_space += data_size + sizeof(*item);
1748 this_item_size = btrfs_item_size(right, item);
1749 if (this_item_size + sizeof(*item) + push_space > free_space)
1753 push_space += this_item_size + sizeof(*item);
1756 if (push_items == 0) {
1757 free_extent_buffer(left);
1760 if (!empty && push_items == btrfs_header_nritems(right))
1763 /* push data from right to left */
1764 copy_extent_buffer(left, right,
1765 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1766 btrfs_item_nr_offset(0),
1767 push_items * sizeof(struct btrfs_item));
1769 push_space = BTRFS_LEAF_DATA_SIZE(root) -
1770 btrfs_item_offset_nr(right, push_items -1);
1772 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
1773 leaf_data_end(root, left) - push_space,
1774 btrfs_leaf_data(right) +
1775 btrfs_item_offset_nr(right, push_items - 1),
1777 old_left_nritems = btrfs_header_nritems(left);
1778 BUG_ON(old_left_nritems < 0);
1780 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
1781 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
1784 item = btrfs_item_nr(left, i);
1785 ioff = btrfs_item_offset(left, item);
1786 btrfs_set_item_offset(left, item,
1787 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
1789 btrfs_set_header_nritems(left, old_left_nritems + push_items);
1791 /* fixup right node */
1792 if (push_items > right_nritems) {
1793 printk("push items %d nr %u\n", push_items, right_nritems);
1797 if (push_items < right_nritems) {
1798 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1799 leaf_data_end(root, right);
1800 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1801 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1802 btrfs_leaf_data(right) +
1803 leaf_data_end(root, right), push_space);
1805 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
1806 btrfs_item_nr_offset(push_items),
1807 (btrfs_header_nritems(right) - push_items) *
1808 sizeof(struct btrfs_item));
1810 right_nritems -= push_items;
1811 btrfs_set_header_nritems(right, right_nritems);
1812 push_space = BTRFS_LEAF_DATA_SIZE(root);
1813 for (i = 0; i < right_nritems; i++) {
1814 item = btrfs_item_nr(right, i);
1815 push_space = push_space - btrfs_item_size(right, item);
1816 btrfs_set_item_offset(right, item, push_space);
1819 btrfs_mark_buffer_dirty(left);
1821 btrfs_mark_buffer_dirty(right);
1823 btrfs_item_key(right, &disk_key, 0);
1824 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
1828 /* then fixup the leaf pointer in the path */
1829 if (path->slots[0] < push_items) {
1830 path->slots[0] += old_left_nritems;
1831 free_extent_buffer(path->nodes[0]);
1832 path->nodes[0] = left;
1833 path->slots[1] -= 1;
1835 free_extent_buffer(left);
1836 path->slots[0] -= push_items;
1838 BUG_ON(path->slots[0] < 0);
1843 * split the path's leaf in two, making sure there is at least data_size
1844 * available for the resulting leaf level of the path.
1846 * returns 0 if all went well and < 0 on failure.
1848 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
1849 *root, struct btrfs_key *ins_key,
1850 struct btrfs_path *path, int data_size, int extend)
1853 struct extent_buffer *l;
1857 struct extent_buffer *right;
1858 int space_needed = data_size + sizeof(struct btrfs_item);
1865 int num_doubles = 0;
1866 struct btrfs_disk_key disk_key;
1869 space_needed = data_size;
1872 root_gen = trans->transid;
1876 /* first try to make some room by pushing left and right */
1877 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
1878 wret = push_leaf_right(trans, root, path, data_size, 0);
1883 wret = push_leaf_left(trans, root, path, data_size, 0);
1889 /* did the pushes work? */
1890 if (btrfs_leaf_free_space(root, l) >= space_needed)
1894 if (!path->nodes[1]) {
1895 ret = insert_new_root(trans, root, path, 1);
1902 slot = path->slots[0];
1903 nritems = btrfs_header_nritems(l);
1904 mid = (nritems + 1)/ 2;
1906 btrfs_item_key(l, &disk_key, 0);
1908 right = __btrfs_alloc_free_block(trans, root, root->leafsize,
1909 root->root_key.objectid,
1910 root_gen, disk_key.objectid, 0,
1913 return PTR_ERR(right);
1915 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
1916 btrfs_set_header_bytenr(right, right->start);
1917 btrfs_set_header_generation(right, trans->transid);
1918 btrfs_set_header_owner(right, root->root_key.objectid);
1919 btrfs_set_header_level(right, 0);
1920 write_extent_buffer(right, root->fs_info->fsid,
1921 (unsigned long)btrfs_header_fsid(right),
1925 leaf_space_used(l, mid, nritems - mid) + space_needed >
1926 BTRFS_LEAF_DATA_SIZE(root)) {
1927 if (slot >= nritems) {
1928 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1929 btrfs_set_header_nritems(right, 0);
1930 wret = insert_ptr(trans, root, path,
1931 &disk_key, right->start,
1932 path->slots[1] + 1, 1);
1935 free_extent_buffer(path->nodes[0]);
1936 path->nodes[0] = right;
1938 path->slots[1] += 1;
1942 if (mid != nritems &&
1943 leaf_space_used(l, mid, nritems - mid) +
1944 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
1949 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1950 BTRFS_LEAF_DATA_SIZE(root)) {
1951 if (!extend && slot == 0) {
1952 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1953 btrfs_set_header_nritems(right, 0);
1954 wret = insert_ptr(trans, root, path,
1960 free_extent_buffer(path->nodes[0]);
1961 path->nodes[0] = right;
1963 if (path->slots[1] == 0) {
1964 wret = fixup_low_keys(trans, root,
1965 path, &disk_key, 1);
1970 } else if (extend && slot == 0) {
1974 if (mid != nritems &&
1975 leaf_space_used(l, mid, nritems - mid) +
1976 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
1982 nritems = nritems - mid;
1983 btrfs_set_header_nritems(right, nritems);
1984 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
1986 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
1987 btrfs_item_nr_offset(mid),
1988 nritems * sizeof(struct btrfs_item));
1990 copy_extent_buffer(right, l,
1991 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1992 data_copy_size, btrfs_leaf_data(l) +
1993 leaf_data_end(root, l), data_copy_size);
1995 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1996 btrfs_item_end_nr(l, mid);
1998 for (i = 0; i < nritems; i++) {
1999 struct btrfs_item *item = btrfs_item_nr(right, i);
2000 u32 ioff = btrfs_item_offset(right, item);
2001 btrfs_set_item_offset(right, item, ioff + rt_data_off);
2004 btrfs_set_header_nritems(l, mid);
2006 btrfs_item_key(right, &disk_key, 0);
2007 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2008 path->slots[1] + 1, 1);
2012 btrfs_mark_buffer_dirty(right);
2013 btrfs_mark_buffer_dirty(l);
2014 BUG_ON(path->slots[0] != slot);
2017 free_extent_buffer(path->nodes[0]);
2018 path->nodes[0] = right;
2019 path->slots[0] -= mid;
2020 path->slots[1] += 1;
2022 free_extent_buffer(right);
2024 BUG_ON(path->slots[0] < 0);
2027 BUG_ON(num_doubles != 0);
2034 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2035 struct btrfs_root *root,
2036 struct btrfs_path *path,
2037 u32 new_size, int from_end)
2042 struct extent_buffer *leaf;
2043 struct btrfs_item *item;
2045 unsigned int data_end;
2046 unsigned int old_data_start;
2047 unsigned int old_size;
2048 unsigned int size_diff;
2051 slot_orig = path->slots[0];
2052 leaf = path->nodes[0];
2053 slot = path->slots[0];
2055 old_size = btrfs_item_size_nr(leaf, slot);
2056 if (old_size == new_size)
2059 nritems = btrfs_header_nritems(leaf);
2060 data_end = leaf_data_end(root, leaf);
2062 old_data_start = btrfs_item_offset_nr(leaf, slot);
2064 size_diff = old_size - new_size;
2067 BUG_ON(slot >= nritems);
2070 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2072 /* first correct the data pointers */
2073 for (i = slot; i < nritems; i++) {
2075 item = btrfs_item_nr(leaf, i);
2076 ioff = btrfs_item_offset(leaf, item);
2077 btrfs_set_item_offset(leaf, item, ioff + size_diff);
2080 /* shift the data */
2082 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2083 data_end + size_diff, btrfs_leaf_data(leaf) +
2084 data_end, old_data_start + new_size - data_end);
2086 struct btrfs_disk_key disk_key;
2089 btrfs_item_key(leaf, &disk_key, slot);
2091 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2093 struct btrfs_file_extent_item *fi;
2095 fi = btrfs_item_ptr(leaf, slot,
2096 struct btrfs_file_extent_item);
2097 fi = (struct btrfs_file_extent_item *)(
2098 (unsigned long)fi - size_diff);
2100 if (btrfs_file_extent_type(leaf, fi) ==
2101 BTRFS_FILE_EXTENT_INLINE) {
2102 ptr = btrfs_item_ptr_offset(leaf, slot);
2103 memmove_extent_buffer(leaf, ptr,
2105 offsetof(struct btrfs_file_extent_item,
2110 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2111 data_end + size_diff, btrfs_leaf_data(leaf) +
2112 data_end, old_data_start - data_end);
2114 offset = btrfs_disk_key_offset(&disk_key);
2115 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2116 btrfs_set_item_key(leaf, &disk_key, slot);
2118 fixup_low_keys(trans, root, path, &disk_key, 1);
2121 item = btrfs_item_nr(leaf, slot);
2122 btrfs_set_item_size(leaf, item, new_size);
2123 btrfs_mark_buffer_dirty(leaf);
2126 if (btrfs_leaf_free_space(root, leaf) < 0) {
2127 btrfs_print_leaf(root, leaf);
2133 int btrfs_extend_item(struct btrfs_trans_handle *trans,
2134 struct btrfs_root *root, struct btrfs_path *path,
2140 struct extent_buffer *leaf;
2141 struct btrfs_item *item;
2143 unsigned int data_end;
2144 unsigned int old_data;
2145 unsigned int old_size;
2148 slot_orig = path->slots[0];
2149 leaf = path->nodes[0];
2151 nritems = btrfs_header_nritems(leaf);
2152 data_end = leaf_data_end(root, leaf);
2154 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2155 btrfs_print_leaf(root, leaf);
2158 slot = path->slots[0];
2159 old_data = btrfs_item_end_nr(leaf, slot);
2162 if (slot >= nritems) {
2163 btrfs_print_leaf(root, leaf);
2164 printk("slot %d too large, nritems %d\n", slot, nritems);
2169 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2171 /* first correct the data pointers */
2172 for (i = slot; i < nritems; i++) {
2174 item = btrfs_item_nr(leaf, i);
2175 ioff = btrfs_item_offset(leaf, item);
2176 btrfs_set_item_offset(leaf, item, ioff - data_size);
2179 /* shift the data */
2180 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2181 data_end - data_size, btrfs_leaf_data(leaf) +
2182 data_end, old_data - data_end);
2184 data_end = old_data;
2185 old_size = btrfs_item_size_nr(leaf, slot);
2186 item = btrfs_item_nr(leaf, slot);
2187 btrfs_set_item_size(leaf, item, old_size + data_size);
2188 btrfs_mark_buffer_dirty(leaf);
2191 if (btrfs_leaf_free_space(root, leaf) < 0) {
2192 btrfs_print_leaf(root, leaf);
2199 * Given a key and some data, insert an item into the tree.
2200 * This does all the path init required, making room in the tree if needed.
2202 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
2203 struct btrfs_root *root,
2204 struct btrfs_path *path,
2205 struct btrfs_key *cpu_key, u32 *data_size,
2208 struct extent_buffer *leaf;
2209 struct btrfs_item *item;
2217 unsigned int data_end;
2218 struct btrfs_disk_key disk_key;
2220 for (i = 0; i < nr; i++) {
2221 total_data += data_size[i];
2224 /* create a root if there isn't one */
2228 total_size = total_data + (nr - 1) * sizeof(struct btrfs_item);
2229 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
2236 slot_orig = path->slots[0];
2237 leaf = path->nodes[0];
2239 nritems = btrfs_header_nritems(leaf);
2240 data_end = leaf_data_end(root, leaf);
2242 if (btrfs_leaf_free_space(root, leaf) <
2243 sizeof(struct btrfs_item) + total_size) {
2244 btrfs_print_leaf(root, leaf);
2245 printk("not enough freespace need %u have %d\n",
2246 total_size, btrfs_leaf_free_space(root, leaf));
2250 slot = path->slots[0];
2253 if (slot != nritems) {
2255 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
2257 if (old_data < data_end) {
2258 btrfs_print_leaf(root, leaf);
2259 printk("slot %d old_data %d data_end %d\n",
2260 slot, old_data, data_end);
2264 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2266 /* first correct the data pointers */
2267 for (i = slot; i < nritems; i++) {
2270 item = btrfs_item_nr(leaf, i);
2271 ioff = btrfs_item_offset(leaf, item);
2272 btrfs_set_item_offset(leaf, item, ioff - total_data);
2275 /* shift the items */
2276 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
2277 btrfs_item_nr_offset(slot),
2278 (nritems - slot) * sizeof(struct btrfs_item));
2280 /* shift the data */
2281 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2282 data_end - total_data, btrfs_leaf_data(leaf) +
2283 data_end, old_data - data_end);
2284 data_end = old_data;
2287 /* setup the item for the new data */
2288 for (i = 0; i < nr; i++) {
2289 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2290 btrfs_set_item_key(leaf, &disk_key, slot + i);
2291 item = btrfs_item_nr(leaf, slot + i);
2292 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2293 data_end -= data_size[i];
2294 btrfs_set_item_size(leaf, item, data_size[i]);
2296 btrfs_set_header_nritems(leaf, nritems + nr);
2297 btrfs_mark_buffer_dirty(leaf);
2301 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
2303 if (btrfs_leaf_free_space(root, leaf) < 0) {
2304 btrfs_print_leaf(root, leaf);
2312 * Given a key and some data, insert an item into the tree.
2313 * This does all the path init required, making room in the tree if needed.
2315 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2316 *root, struct btrfs_key *cpu_key, void *data, u32
2320 struct btrfs_path *path;
2321 struct extent_buffer *leaf;
2324 path = btrfs_alloc_path();
2326 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
2328 leaf = path->nodes[0];
2329 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2330 write_extent_buffer(leaf, data, ptr, data_size);
2331 btrfs_mark_buffer_dirty(leaf);
2333 btrfs_free_path(path);
2338 * delete the pointer from a given node.
2340 * If the delete empties a node, the node is removed from the tree,
2341 * continuing all the way the root if required. The root is converted into
2342 * a leaf if all the nodes are emptied.
2344 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2345 struct btrfs_path *path, int level, int slot)
2347 struct extent_buffer *parent = path->nodes[level];
2352 nritems = btrfs_header_nritems(parent);
2353 if (slot != nritems -1) {
2354 memmove_extent_buffer(parent,
2355 btrfs_node_key_ptr_offset(slot),
2356 btrfs_node_key_ptr_offset(slot + 1),
2357 sizeof(struct btrfs_key_ptr) *
2358 (nritems - slot - 1));
2361 btrfs_set_header_nritems(parent, nritems);
2362 if (nritems == 0 && parent == root->node) {
2363 BUG_ON(btrfs_header_level(root->node) != 1);
2364 /* just turn the root into a leaf and break */
2365 btrfs_set_header_level(root->node, 0);
2366 } else if (slot == 0) {
2367 struct btrfs_disk_key disk_key;
2369 btrfs_node_key(parent, &disk_key, 0);
2370 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
2374 btrfs_mark_buffer_dirty(parent);
2379 * delete the item at the leaf level in path. If that empties
2380 * the leaf, remove it from the tree
2382 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2383 struct btrfs_path *path, int slot, int nr)
2385 struct extent_buffer *leaf;
2386 struct btrfs_item *item;
2394 leaf = path->nodes[0];
2395 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2397 for (i = 0; i < nr; i++)
2398 dsize += btrfs_item_size_nr(leaf, slot + i);
2400 nritems = btrfs_header_nritems(leaf);
2402 if (slot + nr != nritems) {
2404 int data_end = leaf_data_end(root, leaf);
2406 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2408 btrfs_leaf_data(leaf) + data_end,
2409 last_off - data_end);
2411 for (i = slot + nr; i < nritems; i++) {
2414 item = btrfs_item_nr(leaf, i);
2415 ioff = btrfs_item_offset(leaf, item);
2416 btrfs_set_item_offset(leaf, item, ioff + dsize);
2419 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2420 btrfs_item_nr_offset(slot + nr),
2421 sizeof(struct btrfs_item) *
2422 (nritems - slot - nr));
2424 btrfs_set_header_nritems(leaf, nritems - nr);
2427 /* delete the leaf if we've emptied it */
2429 if (leaf == root->node) {
2430 btrfs_set_header_level(leaf, 0);
2432 u64 root_gen = btrfs_header_generation(path->nodes[1]);
2433 clean_tree_block(trans, root, leaf);
2434 wait_on_tree_block_writeback(root, leaf);
2435 wret = del_ptr(trans, root, path, 1, path->slots[1]);
2438 wret = btrfs_free_extent(trans, root,
2439 leaf->start, leaf->len,
2440 btrfs_header_owner(path->nodes[1]),
2446 int used = leaf_space_used(leaf, 0, nritems);
2448 struct btrfs_disk_key disk_key;
2450 btrfs_item_key(leaf, &disk_key, 0);
2451 wret = fixup_low_keys(trans, root, path,
2457 /* delete the leaf if it is mostly empty */
2458 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
2459 /* push_leaf_left fixes the path.
2460 * make sure the path still points to our leaf
2461 * for possible call to del_ptr below
2463 slot = path->slots[1];
2464 extent_buffer_get(leaf);
2466 wret = push_leaf_left(trans, root, path, 1, 1);
2467 if (wret < 0 && wret != -ENOSPC)
2470 if (path->nodes[0] == leaf &&
2471 btrfs_header_nritems(leaf)) {
2472 wret = push_leaf_right(trans, root, path, 1, 1);
2473 if (wret < 0 && wret != -ENOSPC)
2477 if (btrfs_header_nritems(leaf) == 0) {
2479 u64 bytenr = leaf->start;
2480 u32 blocksize = leaf->len;
2482 root_gen = btrfs_header_generation(
2485 clean_tree_block(trans, root, leaf);
2486 wait_on_tree_block_writeback(root, leaf);
2488 wret = del_ptr(trans, root, path, 1, slot);
2492 free_extent_buffer(leaf);
2493 wret = btrfs_free_extent(trans, root, bytenr,
2495 btrfs_header_owner(path->nodes[1]),
2500 btrfs_mark_buffer_dirty(leaf);
2501 free_extent_buffer(leaf);
2504 btrfs_mark_buffer_dirty(leaf);
2511 * walk up the tree as far as required to find the previous leaf.
2512 * returns 0 if it found something or 1 if there are no lesser leaves.
2513 * returns < 0 on io errors.
2515 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2520 struct extent_buffer *c;
2521 struct extent_buffer *next = NULL;
2523 while(level < BTRFS_MAX_LEVEL) {
2524 if (!path->nodes[level])
2527 slot = path->slots[level];
2528 c = path->nodes[level];
2531 if (level == BTRFS_MAX_LEVEL)
2537 bytenr = btrfs_node_blockptr(c, slot);
2539 free_extent_buffer(next);
2541 next = read_tree_block(root, bytenr,
2542 btrfs_level_size(root, level - 1));
2545 path->slots[level] = slot;
2548 c = path->nodes[level];
2549 free_extent_buffer(c);
2550 slot = btrfs_header_nritems(next);
2553 path->nodes[level] = next;
2554 path->slots[level] = slot;
2557 next = read_tree_block(root, btrfs_node_blockptr(next, slot),
2558 btrfs_level_size(root, level - 1));
2564 * walk up the tree as far as required to find the next leaf.
2565 * returns 0 if it found something or 1 if there are no greater leaves.
2566 * returns < 0 on io errors.
2568 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
2573 struct extent_buffer *c;
2574 struct extent_buffer *next = NULL;
2576 while(level < BTRFS_MAX_LEVEL) {
2577 if (!path->nodes[level])
2580 slot = path->slots[level] + 1;
2581 c = path->nodes[level];
2582 if (slot >= btrfs_header_nritems(c)) {
2584 if (level == BTRFS_MAX_LEVEL)
2589 bytenr = btrfs_node_blockptr(c, slot);
2591 free_extent_buffer(next);
2594 reada_for_search(root, path, level, slot, 0);
2596 next = read_tree_block(root, bytenr,
2597 btrfs_level_size(root, level -1));
2600 path->slots[level] = slot;
2603 c = path->nodes[level];
2604 free_extent_buffer(c);
2605 path->nodes[level] = next;
2606 path->slots[level] = 0;
2610 reada_for_search(root, path, level, 0, 0);
2611 next = read_tree_block(root, btrfs_node_blockptr(next, 0),
2612 btrfs_level_size(root, level - 1));