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 static void add_root_to_dirty_list(struct btrfs_root *root)
73 if (root->track_dirty && list_empty(&root->dirty_list)) {
74 list_add(&root->dirty_list,
75 &root->fs_info->dirty_cowonly_roots);
79 int btrfs_copy_root(struct btrfs_trans_handle *trans,
80 struct btrfs_root *root,
81 struct extent_buffer *buf,
82 struct extent_buffer **cow_ret, u64 new_root_objectid)
84 struct extent_buffer *cow;
88 struct btrfs_key first_key;
89 struct btrfs_root *new_root;
91 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
95 memcpy(new_root, root, sizeof(*new_root));
96 new_root->root_key.objectid = new_root_objectid;
98 WARN_ON(root->ref_cows && trans->transid !=
99 root->fs_info->running_transaction->transid);
100 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
102 level = btrfs_header_level(buf);
103 nritems = btrfs_header_nritems(buf);
106 btrfs_item_key_to_cpu(buf, &first_key, 0);
108 btrfs_node_key_to_cpu(buf, &first_key, 0);
110 first_key.objectid = 0;
112 cow = __btrfs_alloc_free_block(trans, new_root, buf->len,
114 trans->transid, first_key.objectid,
115 level, buf->start, 0);
121 copy_extent_buffer(cow, buf, 0, 0, cow->len);
122 btrfs_set_header_bytenr(cow, cow->start);
123 btrfs_set_header_generation(cow, trans->transid);
124 btrfs_set_header_owner(cow, new_root_objectid);
125 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
127 WARN_ON(btrfs_header_generation(buf) > trans->transid);
128 ret = btrfs_inc_ref(trans, new_root, buf);
134 btrfs_mark_buffer_dirty(cow);
139 int __btrfs_cow_block(struct btrfs_trans_handle *trans,
140 struct btrfs_root *root,
141 struct extent_buffer *buf,
142 struct extent_buffer *parent, int parent_slot,
143 struct extent_buffer **cow_ret,
144 u64 search_start, u64 empty_size)
147 struct extent_buffer *cow;
150 int different_trans = 0;
152 struct btrfs_key first_key;
154 if (root->ref_cows) {
155 root_gen = trans->transid;
160 WARN_ON(root->ref_cows && trans->transid !=
161 root->fs_info->running_transaction->transid);
162 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
164 level = btrfs_header_level(buf);
165 nritems = btrfs_header_nritems(buf);
168 btrfs_item_key_to_cpu(buf, &first_key, 0);
170 btrfs_node_key_to_cpu(buf, &first_key, 0);
172 first_key.objectid = 0;
174 cow = __btrfs_alloc_free_block(trans, root, buf->len,
175 root->root_key.objectid,
176 root_gen, first_key.objectid, level,
177 search_start, empty_size);
181 copy_extent_buffer(cow, buf, 0, 0, cow->len);
182 btrfs_set_header_bytenr(cow, cow->start);
183 btrfs_set_header_generation(cow, trans->transid);
184 btrfs_set_header_owner(cow, root->root_key.objectid);
185 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
187 WARN_ON(btrfs_header_generation(buf) > trans->transid);
188 if (btrfs_header_generation(buf) != trans->transid) {
190 ret = btrfs_inc_ref(trans, root, buf);
194 clean_tree_block(trans, root, buf);
197 if (buf == root->node) {
198 root_gen = btrfs_header_generation(buf);
200 extent_buffer_get(cow);
201 if (buf != root->commit_root) {
202 btrfs_free_extent(trans, root, buf->start,
203 buf->len, root->root_key.objectid,
206 free_extent_buffer(buf);
207 add_root_to_dirty_list(root);
209 root_gen = btrfs_header_generation(parent);
210 btrfs_set_node_blockptr(parent, parent_slot,
212 WARN_ON(trans->transid == 0);
213 btrfs_set_node_ptr_generation(parent, parent_slot,
215 btrfs_mark_buffer_dirty(parent);
216 WARN_ON(btrfs_header_generation(parent) != trans->transid);
217 btrfs_free_extent(trans, root, buf->start, buf->len,
218 btrfs_header_owner(parent), root_gen,
221 free_extent_buffer(buf);
222 btrfs_mark_buffer_dirty(cow);
227 int btrfs_cow_block(struct btrfs_trans_handle *trans,
228 struct btrfs_root *root, struct extent_buffer *buf,
229 struct extent_buffer *parent, int parent_slot,
230 struct extent_buffer **cow_ret)
235 if (trans->transaction != root->fs_info->running_transaction) {
236 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
237 root->fs_info->running_transaction->transid);
241 if (trans->transid != root->fs_info->generation) {
242 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
243 root->fs_info->generation);
246 if (btrfs_header_generation(buf) == trans->transid) {
249 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
255 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
256 ret = __btrfs_cow_block(trans, root, buf, parent,
257 parent_slot, cow_ret, search_start, 0);
262 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
264 if (blocknr < other && other - (blocknr + blocksize) < 32768)
266 if (blocknr > other && blocknr - (other + blocksize) < 32768)
273 * compare two keys in a memcmp fashion
275 int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
279 btrfs_disk_key_to_cpu(&k1, disk);
281 if (k1.objectid > k2->objectid)
283 if (k1.objectid < k2->objectid)
285 if (k1.type > k2->type)
287 if (k1.type < k2->type)
289 if (k1.offset > k2->offset)
291 if (k1.offset < k2->offset)
298 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
299 struct btrfs_root *root, struct extent_buffer *parent,
300 int start_slot, int cache_only, u64 *last_ret,
301 struct btrfs_key *progress)
303 struct extent_buffer *cur;
304 struct extent_buffer *tmp;
306 u64 search_start = *last_ret;
316 int progress_passed = 0;
317 struct btrfs_disk_key disk_key;
319 parent_level = btrfs_header_level(parent);
320 if (cache_only && parent_level != 1)
323 if (trans->transaction != root->fs_info->running_transaction) {
324 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
325 root->fs_info->running_transaction->transid);
328 if (trans->transid != root->fs_info->generation) {
329 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
330 root->fs_info->generation);
334 parent_nritems = btrfs_header_nritems(parent);
335 blocksize = btrfs_level_size(root, parent_level - 1);
336 end_slot = parent_nritems;
338 if (parent_nritems == 1)
341 for (i = start_slot; i < end_slot; i++) {
344 if (!parent->map_token) {
345 map_extent_buffer(parent,
346 btrfs_node_key_ptr_offset(i),
347 sizeof(struct btrfs_key_ptr),
348 &parent->map_token, &parent->kaddr,
349 &parent->map_start, &parent->map_len,
352 btrfs_node_key(parent, &disk_key, i);
353 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
357 blocknr = btrfs_node_blockptr(parent, i);
359 last_block = blocknr;
362 other = btrfs_node_blockptr(parent, i - 1);
363 close = close_blocks(blocknr, other, blocksize);
365 if (close && i < end_slot - 2) {
366 other = btrfs_node_blockptr(parent, i + 1);
367 close = close_blocks(blocknr, other, blocksize);
370 last_block = blocknr;
373 if (parent->map_token) {
374 unmap_extent_buffer(parent, parent->map_token,
376 parent->map_token = NULL;
379 cur = btrfs_find_tree_block(root, blocknr, blocksize);
381 uptodate = btrfs_buffer_uptodate(cur);
384 if (!cur || !uptodate) {
386 free_extent_buffer(cur);
390 cur = read_tree_block(root, blocknr,
392 } else if (!uptodate) {
393 btrfs_read_buffer(cur);
396 if (search_start == 0)
397 search_start = last_block;
399 err = __btrfs_cow_block(trans, root, cur, parent, i,
402 (end_slot - i) * blocksize));
404 free_extent_buffer(cur);
407 search_start = tmp->start;
408 last_block = tmp->start;
409 *last_ret = search_start;
410 if (parent_level == 1)
411 btrfs_clear_buffer_defrag(tmp);
412 free_extent_buffer(tmp);
414 if (parent->map_token) {
415 unmap_extent_buffer(parent, parent->map_token,
417 parent->map_token = NULL;
424 * The leaf data grows from end-to-front in the node.
425 * this returns the address of the start of the last item,
426 * which is the stop of the leaf data stack
428 static inline unsigned int leaf_data_end(struct btrfs_root *root,
429 struct extent_buffer *leaf)
431 u32 nr = btrfs_header_nritems(leaf);
433 return BTRFS_LEAF_DATA_SIZE(root);
434 return btrfs_item_offset_nr(leaf, nr - 1);
437 static int check_node(struct btrfs_root *root, struct btrfs_path *path,
440 struct extent_buffer *parent = NULL;
441 struct extent_buffer *node = path->nodes[level];
442 struct btrfs_disk_key parent_key;
443 struct btrfs_disk_key node_key;
446 struct btrfs_key cpukey;
447 u32 nritems = btrfs_header_nritems(node);
449 if (path->nodes[level + 1])
450 parent = path->nodes[level + 1];
452 slot = path->slots[level];
453 BUG_ON(nritems == 0);
455 parent_slot = path->slots[level + 1];
456 btrfs_node_key(parent, &parent_key, parent_slot);
457 btrfs_node_key(node, &node_key, 0);
458 BUG_ON(memcmp(&parent_key, &node_key,
459 sizeof(struct btrfs_disk_key)));
460 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
461 btrfs_header_bytenr(node));
463 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
465 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
466 btrfs_node_key(node, &node_key, slot);
467 BUG_ON(btrfs_comp_keys(&node_key, &cpukey) <= 0);
469 if (slot < nritems - 1) {
470 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
471 btrfs_node_key(node, &node_key, slot);
472 BUG_ON(btrfs_comp_keys(&node_key, &cpukey) >= 0);
477 static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
480 struct extent_buffer *leaf = path->nodes[level];
481 struct extent_buffer *parent = NULL;
483 struct btrfs_key cpukey;
484 struct btrfs_disk_key parent_key;
485 struct btrfs_disk_key leaf_key;
486 int slot = path->slots[0];
488 u32 nritems = btrfs_header_nritems(leaf);
490 if (path->nodes[level + 1])
491 parent = path->nodes[level + 1];
497 parent_slot = path->slots[level + 1];
498 btrfs_node_key(parent, &parent_key, parent_slot);
499 btrfs_item_key(leaf, &leaf_key, 0);
501 BUG_ON(memcmp(&parent_key, &leaf_key,
502 sizeof(struct btrfs_disk_key)));
503 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
504 btrfs_header_bytenr(leaf));
507 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
508 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
509 btrfs_item_key(leaf, &leaf_key, i);
510 if (comp_keys(&leaf_key, &cpukey) >= 0) {
511 btrfs_print_leaf(root, leaf);
512 printk("slot %d offset bad key\n", i);
515 if (btrfs_item_offset_nr(leaf, i) !=
516 btrfs_item_end_nr(leaf, i + 1)) {
517 btrfs_print_leaf(root, leaf);
518 printk("slot %d offset bad\n", i);
522 if (btrfs_item_offset_nr(leaf, i) +
523 btrfs_item_size_nr(leaf, i) !=
524 BTRFS_LEAF_DATA_SIZE(root)) {
525 btrfs_print_leaf(root, leaf);
526 printk("slot %d first offset bad\n", i);
532 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
533 btrfs_print_leaf(root, leaf);
534 printk("slot %d bad size \n", nritems - 1);
539 if (slot != 0 && slot < nritems - 1) {
540 btrfs_item_key(leaf, &leaf_key, slot);
541 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
542 if (btrfs_comp_keys(&leaf_key, &cpukey) <= 0) {
543 btrfs_print_leaf(root, leaf);
544 printk("slot %d offset bad key\n", slot);
547 if (btrfs_item_offset_nr(leaf, slot - 1) !=
548 btrfs_item_end_nr(leaf, slot)) {
549 btrfs_print_leaf(root, leaf);
550 printk("slot %d offset bad\n", slot);
554 if (slot < nritems - 1) {
555 btrfs_item_key(leaf, &leaf_key, slot);
556 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
557 BUG_ON(btrfs_comp_keys(&leaf_key, &cpukey) >= 0);
558 if (btrfs_item_offset_nr(leaf, slot) !=
559 btrfs_item_end_nr(leaf, slot + 1)) {
560 btrfs_print_leaf(root, leaf);
561 printk("slot %d offset bad\n", slot);
565 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
566 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
570 static int noinline check_block(struct btrfs_root *root,
571 struct btrfs_path *path, int level)
575 struct extent_buffer *buf = path->nodes[level];
577 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
578 (unsigned long)btrfs_header_fsid(buf),
580 printk("warning bad block %Lu\n", buf->start);
585 return check_leaf(root, path, level);
586 return check_node(root, path, level);
590 * search for key in the extent_buffer. The items start at offset p,
591 * and they are item_size apart. There are 'max' items in p.
593 * the slot in the array is returned via slot, and it points to
594 * the place where you would insert key if it is not found in
597 * slot may point to max if the key is bigger than all of the keys
599 static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
600 int item_size, struct btrfs_key *key,
607 unsigned long offset;
608 struct btrfs_disk_key *tmp;
611 mid = (low + high) / 2;
612 offset = p + mid * item_size;
614 tmp = (struct btrfs_disk_key *)(eb->data + offset);
615 ret = btrfs_comp_keys(tmp, key);
631 * simple bin_search frontend that does the right thing for
634 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
635 int level, int *slot)
638 return generic_bin_search(eb,
639 offsetof(struct btrfs_leaf, items),
640 sizeof(struct btrfs_item),
641 key, btrfs_header_nritems(eb),
644 return generic_bin_search(eb,
645 offsetof(struct btrfs_node, ptrs),
646 sizeof(struct btrfs_key_ptr),
647 key, btrfs_header_nritems(eb),
653 static struct extent_buffer *read_node_slot(struct btrfs_root *root,
654 struct extent_buffer *parent, int slot)
658 if (slot >= btrfs_header_nritems(parent))
660 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
661 btrfs_level_size(root, btrfs_header_level(parent) - 1));
664 static int balance_level(struct btrfs_trans_handle *trans,
665 struct btrfs_root *root,
666 struct btrfs_path *path, int level)
668 struct extent_buffer *right = NULL;
669 struct extent_buffer *mid;
670 struct extent_buffer *left = NULL;
671 struct extent_buffer *parent = NULL;
675 int orig_slot = path->slots[level];
676 int err_on_enospc = 0;
682 mid = path->nodes[level];
683 WARN_ON(btrfs_header_generation(mid) != trans->transid);
685 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
687 if (level < BTRFS_MAX_LEVEL - 1)
688 parent = path->nodes[level + 1];
689 pslot = path->slots[level + 1];
692 * deal with the case where there is only one pointer in the root
693 * by promoting the node below to a root
696 struct extent_buffer *child;
698 if (btrfs_header_nritems(mid) != 1)
701 /* promote the child to a root */
702 child = read_node_slot(root, mid, 0);
704 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
708 add_root_to_dirty_list(root);
709 path->nodes[level] = NULL;
710 clean_tree_block(trans, root, mid);
711 wait_on_tree_block_writeback(root, mid);
712 /* once for the path */
713 free_extent_buffer(mid);
714 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
715 root->root_key.objectid,
716 btrfs_header_generation(mid), 0, 0, 1);
717 /* once for the root ptr */
718 free_extent_buffer(mid);
721 if (btrfs_header_nritems(mid) >
722 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
725 if (btrfs_header_nritems(mid) < 2)
728 left = read_node_slot(root, parent, pslot - 1);
730 wret = btrfs_cow_block(trans, root, left,
731 parent, pslot - 1, &left);
737 right = read_node_slot(root, parent, pslot + 1);
739 wret = btrfs_cow_block(trans, root, right,
740 parent, pslot + 1, &right);
747 /* first, try to make some room in the middle buffer */
749 orig_slot += btrfs_header_nritems(left);
750 wret = push_node_left(trans, root, left, mid);
753 if (btrfs_header_nritems(mid) < 2)
758 * then try to empty the right most buffer into the middle
761 wret = push_node_left(trans, root, mid, right);
762 if (wret < 0 && wret != -ENOSPC)
764 if (btrfs_header_nritems(right) == 0) {
765 u64 bytenr = right->start;
766 u64 generation = btrfs_header_generation(parent);
767 u32 blocksize = right->len;
769 clean_tree_block(trans, root, right);
770 wait_on_tree_block_writeback(root, right);
771 free_extent_buffer(right);
773 wret = del_ptr(trans, root, path, level + 1, pslot +
777 wret = btrfs_free_extent(trans, root, bytenr,
779 btrfs_header_owner(parent),
780 generation, 0, 0, 1);
784 struct btrfs_disk_key right_key;
785 btrfs_node_key(right, &right_key, 0);
786 btrfs_set_node_key(parent, &right_key, pslot + 1);
787 btrfs_mark_buffer_dirty(parent);
790 if (btrfs_header_nritems(mid) == 1) {
792 * we're not allowed to leave a node with one item in the
793 * tree during a delete. A deletion from lower in the tree
794 * could try to delete the only pointer in this node.
795 * So, pull some keys from the left.
796 * There has to be a left pointer at this point because
797 * otherwise we would have pulled some pointers from the
801 wret = balance_node_right(trans, root, mid, left);
808 if (btrfs_header_nritems(mid) == 0) {
809 /* we've managed to empty the middle node, drop it */
810 u64 root_gen = btrfs_header_generation(parent);
811 u64 bytenr = mid->start;
812 u32 blocksize = mid->len;
813 clean_tree_block(trans, root, mid);
814 wait_on_tree_block_writeback(root, mid);
815 free_extent_buffer(mid);
817 wret = del_ptr(trans, root, path, level + 1, pslot);
820 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
821 btrfs_header_owner(parent),
826 /* update the parent key to reflect our changes */
827 struct btrfs_disk_key mid_key;
828 btrfs_node_key(mid, &mid_key, 0);
829 btrfs_set_node_key(parent, &mid_key, pslot);
830 btrfs_mark_buffer_dirty(parent);
833 /* update the path */
835 if (btrfs_header_nritems(left) > orig_slot) {
836 extent_buffer_get(left);
837 path->nodes[level] = left;
838 path->slots[level + 1] -= 1;
839 path->slots[level] = orig_slot;
841 free_extent_buffer(mid);
843 orig_slot -= btrfs_header_nritems(left);
844 path->slots[level] = orig_slot;
847 /* double check we haven't messed things up */
848 check_block(root, path, level);
850 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
854 free_extent_buffer(right);
856 free_extent_buffer(left);
860 /* returns zero if the push worked, non-zero otherwise */
861 static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
862 struct btrfs_root *root,
863 struct btrfs_path *path, int level)
865 struct extent_buffer *right = NULL;
866 struct extent_buffer *mid;
867 struct extent_buffer *left = NULL;
868 struct extent_buffer *parent = NULL;
872 int orig_slot = path->slots[level];
878 mid = path->nodes[level];
879 WARN_ON(btrfs_header_generation(mid) != trans->transid);
880 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
882 if (level < BTRFS_MAX_LEVEL - 1)
883 parent = path->nodes[level + 1];
884 pslot = path->slots[level + 1];
889 left = read_node_slot(root, parent, pslot - 1);
891 /* first, try to make some room in the middle buffer */
894 left_nr = btrfs_header_nritems(left);
895 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
898 ret = btrfs_cow_block(trans, root, left, parent,
903 wret = push_node_left(trans, root,
910 struct btrfs_disk_key disk_key;
911 orig_slot += left_nr;
912 btrfs_node_key(mid, &disk_key, 0);
913 btrfs_set_node_key(parent, &disk_key, pslot);
914 btrfs_mark_buffer_dirty(parent);
915 if (btrfs_header_nritems(left) > orig_slot) {
916 path->nodes[level] = left;
917 path->slots[level + 1] -= 1;
918 path->slots[level] = orig_slot;
919 free_extent_buffer(mid);
922 btrfs_header_nritems(left);
923 path->slots[level] = orig_slot;
924 free_extent_buffer(left);
928 free_extent_buffer(left);
930 right= read_node_slot(root, parent, pslot + 1);
933 * then try to empty the right most buffer into the middle
937 right_nr = btrfs_header_nritems(right);
938 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
941 ret = btrfs_cow_block(trans, root, right,
947 wret = balance_node_right(trans, root,
954 struct btrfs_disk_key disk_key;
956 btrfs_node_key(right, &disk_key, 0);
957 btrfs_set_node_key(parent, &disk_key, pslot + 1);
958 btrfs_mark_buffer_dirty(parent);
960 if (btrfs_header_nritems(mid) <= orig_slot) {
961 path->nodes[level] = right;
962 path->slots[level + 1] += 1;
963 path->slots[level] = orig_slot -
964 btrfs_header_nritems(mid);
965 free_extent_buffer(mid);
967 free_extent_buffer(right);
971 free_extent_buffer(right);
977 * readahead one full node of leaves
979 static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
980 int level, int slot, u64 objectid)
982 struct extent_buffer *node;
983 struct btrfs_disk_key disk_key;
989 int direction = path->reada;
990 struct extent_buffer *eb;
998 if (!path->nodes[level])
1001 node = path->nodes[level];
1002 search = btrfs_node_blockptr(node, slot);
1003 blocksize = btrfs_level_size(root, level - 1);
1004 eb = btrfs_find_tree_block(root, search, blocksize);
1006 free_extent_buffer(eb);
1010 highest_read = search;
1011 lowest_read = search;
1013 nritems = btrfs_header_nritems(node);
1016 if (direction < 0) {
1020 } else if (direction > 0) {
1025 if (path->reada < 0 && objectid) {
1026 btrfs_node_key(node, &disk_key, nr);
1027 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1030 search = btrfs_node_blockptr(node, nr);
1031 if ((search >= lowest_read && search <= highest_read) ||
1032 (search < lowest_read && lowest_read - search <= 32768) ||
1033 (search > highest_read && search - highest_read <= 32768)) {
1034 readahead_tree_block(root, search, blocksize);
1038 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1040 if(nread > (1024 * 1024) || nscan > 128)
1043 if (search < lowest_read)
1044 lowest_read = search;
1045 if (search > highest_read)
1046 highest_read = search;
1051 * look for key in the tree. path is filled in with nodes along the way
1052 * if key is found, we return zero and you can find the item in the leaf
1053 * level of the path (level 0)
1055 * If the key isn't found, the path points to the slot where it should
1056 * be inserted, and 1 is returned. If there are other errors during the
1057 * search a negative error number is returned.
1059 * if ins_len > 0, nodes and leaves will be split as we walk down the
1060 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1063 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1064 *root, struct btrfs_key *key, struct btrfs_path *p, int
1067 struct extent_buffer *b;
1073 int should_reada = p->reada;
1074 u8 lowest_level = 0;
1076 lowest_level = p->lowest_level;
1077 WARN_ON(lowest_level && ins_len);
1078 WARN_ON(p->nodes[0] != NULL);
1080 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1084 extent_buffer_get(b);
1086 level = btrfs_header_level(b);
1089 wret = btrfs_cow_block(trans, root, b,
1090 p->nodes[level + 1],
1091 p->slots[level + 1],
1094 free_extent_buffer(b);
1098 BUG_ON(!cow && ins_len);
1099 if (level != btrfs_header_level(b))
1101 level = btrfs_header_level(b);
1102 p->nodes[level] = b;
1103 ret = check_block(root, p, level);
1106 ret = bin_search(b, key, level, &slot);
1108 if (ret && slot > 0)
1110 p->slots[level] = slot;
1111 if (ins_len > 0 && btrfs_header_nritems(b) >=
1112 BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1113 int sret = split_node(trans, root, p, level);
1117 b = p->nodes[level];
1118 slot = p->slots[level];
1119 } else if (ins_len < 0) {
1120 int sret = balance_level(trans, root, p,
1124 b = p->nodes[level];
1126 btrfs_release_path(NULL, p);
1129 slot = p->slots[level];
1130 BUG_ON(btrfs_header_nritems(b) == 1);
1132 /* this is only true while dropping a snapshot */
1133 if (level == lowest_level)
1135 bytenr = btrfs_node_blockptr(b, slot);
1136 ptr_gen = btrfs_node_ptr_generation(b, slot);
1138 reada_for_search(root, p, level, slot,
1140 b = read_tree_block(root, bytenr,
1141 btrfs_level_size(root, level - 1));
1142 if (ptr_gen != btrfs_header_generation(b)) {
1143 printk("block %llu bad gen wanted %llu "
1145 (unsigned long long)b->start,
1146 (unsigned long long)ptr_gen,
1147 (unsigned long long)btrfs_header_generation(b));
1150 p->slots[level] = slot;
1151 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
1152 sizeof(struct btrfs_item) + ins_len) {
1153 int sret = split_leaf(trans, root, key,
1154 p, ins_len, ret == 0);
1166 * adjust the pointers going up the tree, starting at level
1167 * making sure the right key of each node is points to 'key'.
1168 * This is used after shifting pointers to the left, so it stops
1169 * fixing up pointers when a given leaf/node is not in slot 0 of the
1172 * If this fails to write a tree block, it returns -1, but continues
1173 * fixing up the blocks in ram so the tree is consistent.
1175 static int fixup_low_keys(struct btrfs_trans_handle *trans,
1176 struct btrfs_root *root, struct btrfs_path *path,
1177 struct btrfs_disk_key *key, int level)
1181 struct extent_buffer *t;
1183 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1184 int tslot = path->slots[i];
1185 if (!path->nodes[i])
1188 btrfs_set_node_key(t, key, tslot);
1189 btrfs_mark_buffer_dirty(path->nodes[i]);
1197 * try to push data from one node into the next node left in the
1200 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1201 * error, and > 0 if there was no room in the left hand block.
1203 static int push_node_left(struct btrfs_trans_handle *trans,
1204 struct btrfs_root *root, struct extent_buffer *dst,
1205 struct extent_buffer *src)
1212 src_nritems = btrfs_header_nritems(src);
1213 dst_nritems = btrfs_header_nritems(dst);
1214 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1215 WARN_ON(btrfs_header_generation(src) != trans->transid);
1216 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1218 if (push_items <= 0) {
1222 if (src_nritems < push_items)
1223 push_items = src_nritems;
1225 copy_extent_buffer(dst, src,
1226 btrfs_node_key_ptr_offset(dst_nritems),
1227 btrfs_node_key_ptr_offset(0),
1228 push_items * sizeof(struct btrfs_key_ptr));
1230 if (push_items < src_nritems) {
1231 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1232 btrfs_node_key_ptr_offset(push_items),
1233 (src_nritems - push_items) *
1234 sizeof(struct btrfs_key_ptr));
1236 btrfs_set_header_nritems(src, src_nritems - push_items);
1237 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1238 btrfs_mark_buffer_dirty(src);
1239 btrfs_mark_buffer_dirty(dst);
1244 * try to push data from one node into the next node right in the
1247 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1248 * error, and > 0 if there was no room in the right hand block.
1250 * this will only push up to 1/2 the contents of the left node over
1252 static int balance_node_right(struct btrfs_trans_handle *trans,
1253 struct btrfs_root *root,
1254 struct extent_buffer *dst,
1255 struct extent_buffer *src)
1263 WARN_ON(btrfs_header_generation(src) != trans->transid);
1264 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1266 src_nritems = btrfs_header_nritems(src);
1267 dst_nritems = btrfs_header_nritems(dst);
1268 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1269 if (push_items <= 0)
1272 max_push = src_nritems / 2 + 1;
1273 /* don't try to empty the node */
1274 if (max_push >= src_nritems)
1277 if (max_push < push_items)
1278 push_items = max_push;
1280 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1281 btrfs_node_key_ptr_offset(0),
1283 sizeof(struct btrfs_key_ptr));
1285 copy_extent_buffer(dst, src,
1286 btrfs_node_key_ptr_offset(0),
1287 btrfs_node_key_ptr_offset(src_nritems - push_items),
1288 push_items * sizeof(struct btrfs_key_ptr));
1290 btrfs_set_header_nritems(src, src_nritems - push_items);
1291 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1293 btrfs_mark_buffer_dirty(src);
1294 btrfs_mark_buffer_dirty(dst);
1299 * helper function to insert a new root level in the tree.
1300 * A new node is allocated, and a single item is inserted to
1301 * point to the existing root
1303 * returns zero on success or < 0 on failure.
1305 static int noinline insert_new_root(struct btrfs_trans_handle *trans,
1306 struct btrfs_root *root,
1307 struct btrfs_path *path, int level)
1311 struct extent_buffer *lower;
1312 struct extent_buffer *c;
1313 struct btrfs_disk_key lower_key;
1315 BUG_ON(path->nodes[level]);
1316 BUG_ON(path->nodes[level-1] != root->node);
1319 root_gen = trans->transid;
1323 lower = path->nodes[level-1];
1325 btrfs_item_key(lower, &lower_key, 0);
1327 btrfs_node_key(lower, &lower_key, 0);
1329 c = __btrfs_alloc_free_block(trans, root, root->nodesize,
1330 root->root_key.objectid,
1331 root_gen, lower_key.objectid, level,
1332 root->node->start, 0);
1335 memset_extent_buffer(c, 0, 0, root->nodesize);
1336 btrfs_set_header_nritems(c, 1);
1337 btrfs_set_header_level(c, level);
1338 btrfs_set_header_bytenr(c, c->start);
1339 btrfs_set_header_generation(c, trans->transid);
1340 btrfs_set_header_owner(c, root->root_key.objectid);
1342 write_extent_buffer(c, root->fs_info->fsid,
1343 (unsigned long)btrfs_header_fsid(c),
1345 btrfs_set_node_key(c, &lower_key, 0);
1346 btrfs_set_node_blockptr(c, 0, lower->start);
1347 lower_gen = btrfs_header_generation(lower);
1348 WARN_ON(lower_gen == 0);
1350 btrfs_set_node_ptr_generation(c, 0, lower_gen);
1352 btrfs_mark_buffer_dirty(c);
1354 /* the super has an extra ref to root->node */
1355 free_extent_buffer(root->node);
1357 add_root_to_dirty_list(root);
1358 extent_buffer_get(c);
1359 path->nodes[level] = c;
1360 path->slots[level] = 0;
1362 if (root->ref_cows && lower_gen != trans->transid) {
1363 struct btrfs_path *back_path = btrfs_alloc_path();
1365 ret = btrfs_insert_extent_backref(trans,
1366 root->fs_info->extent_root,
1368 root->root_key.objectid,
1369 trans->transid, 0, 0);
1371 btrfs_free_path(back_path);
1377 * worker function to insert a single pointer in a node.
1378 * the node should have enough room for the pointer already
1380 * slot and level indicate where you want the key to go, and
1381 * blocknr is the block the key points to.
1383 * returns zero on success and < 0 on any error
1385 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1386 *root, struct btrfs_path *path, struct btrfs_disk_key
1387 *key, u64 bytenr, int slot, int level)
1389 struct extent_buffer *lower;
1392 BUG_ON(!path->nodes[level]);
1393 lower = path->nodes[level];
1394 nritems = btrfs_header_nritems(lower);
1397 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
1399 if (slot != nritems) {
1400 memmove_extent_buffer(lower,
1401 btrfs_node_key_ptr_offset(slot + 1),
1402 btrfs_node_key_ptr_offset(slot),
1403 (nritems - slot) * sizeof(struct btrfs_key_ptr));
1405 btrfs_set_node_key(lower, key, slot);
1406 btrfs_set_node_blockptr(lower, slot, bytenr);
1407 WARN_ON(trans->transid == 0);
1408 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
1409 btrfs_set_header_nritems(lower, nritems + 1);
1410 btrfs_mark_buffer_dirty(lower);
1415 * split the node at the specified level in path in two.
1416 * The path is corrected to point to the appropriate node after the split
1418 * Before splitting this tries to make some room in the node by pushing
1419 * left and right, if either one works, it returns right away.
1421 * returns 0 on success and < 0 on failure
1423 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1424 *root, struct btrfs_path *path, int level)
1427 struct extent_buffer *c;
1428 struct extent_buffer *split;
1429 struct btrfs_disk_key disk_key;
1435 c = path->nodes[level];
1436 WARN_ON(btrfs_header_generation(c) != trans->transid);
1437 if (c == root->node) {
1438 /* trying to split the root, lets make a new one */
1439 ret = insert_new_root(trans, root, path, level + 1);
1443 ret = push_nodes_for_insert(trans, root, path, level);
1444 c = path->nodes[level];
1445 if (!ret && btrfs_header_nritems(c) <
1446 BTRFS_NODEPTRS_PER_BLOCK(root) - 1)
1452 c_nritems = btrfs_header_nritems(c);
1454 root_gen = trans->transid;
1458 btrfs_node_key(c, &disk_key, 0);
1459 split = __btrfs_alloc_free_block(trans, root, root->nodesize,
1460 root->root_key.objectid,
1462 btrfs_disk_key_objectid(&disk_key),
1463 level, c->start, 0);
1465 return PTR_ERR(split);
1467 btrfs_set_header_flags(split, btrfs_header_flags(c));
1468 btrfs_set_header_level(split, btrfs_header_level(c));
1469 btrfs_set_header_bytenr(split, split->start);
1470 btrfs_set_header_generation(split, trans->transid);
1471 btrfs_set_header_owner(split, root->root_key.objectid);
1472 btrfs_set_header_flags(split, 0);
1473 write_extent_buffer(split, root->fs_info->fsid,
1474 (unsigned long)btrfs_header_fsid(split),
1477 mid = (c_nritems + 1) / 2;
1479 copy_extent_buffer(split, c,
1480 btrfs_node_key_ptr_offset(0),
1481 btrfs_node_key_ptr_offset(mid),
1482 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1483 btrfs_set_header_nritems(split, c_nritems - mid);
1484 btrfs_set_header_nritems(c, mid);
1487 btrfs_mark_buffer_dirty(c);
1488 btrfs_mark_buffer_dirty(split);
1490 btrfs_node_key(split, &disk_key, 0);
1491 wret = insert_ptr(trans, root, path, &disk_key, split->start,
1492 path->slots[level + 1] + 1,
1497 if (path->slots[level] >= mid) {
1498 path->slots[level] -= mid;
1499 free_extent_buffer(c);
1500 path->nodes[level] = split;
1501 path->slots[level + 1] += 1;
1503 free_extent_buffer(split);
1509 * how many bytes are required to store the items in a leaf. start
1510 * and nr indicate which items in the leaf to check. This totals up the
1511 * space used both by the item structs and the item data
1513 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
1516 int nritems = btrfs_header_nritems(l);
1517 int end = min(nritems, start + nr) - 1;
1521 data_len = btrfs_item_end_nr(l, start);
1522 data_len = data_len - btrfs_item_offset_nr(l, end);
1523 data_len += sizeof(struct btrfs_item) * nr;
1524 WARN_ON(data_len < 0);
1529 * The space between the end of the leaf items and
1530 * the start of the leaf data. IOW, how much room
1531 * the leaf has left for both items and data
1533 int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
1535 int nritems = btrfs_header_nritems(leaf);
1537 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1539 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
1540 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
1541 leaf_space_used(leaf, 0, nritems), nritems);
1547 * push some data in the path leaf to the right, trying to free up at
1548 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1550 * returns 1 if the push failed because the other node didn't have enough
1551 * room, 0 if everything worked out and < 0 if there were major errors.
1553 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1554 *root, struct btrfs_path *path, int data_size,
1557 struct extent_buffer *left = path->nodes[0];
1558 struct extent_buffer *right;
1559 struct extent_buffer *upper;
1560 struct btrfs_disk_key disk_key;
1566 struct btrfs_item *item;
1574 slot = path->slots[1];
1575 if (!path->nodes[1]) {
1578 upper = path->nodes[1];
1579 if (slot >= btrfs_header_nritems(upper) - 1)
1582 right = read_tree_block(root, btrfs_node_blockptr(upper, slot + 1),
1584 free_space = btrfs_leaf_free_space(root, right);
1585 if (free_space < data_size + sizeof(struct btrfs_item)) {
1586 free_extent_buffer(right);
1590 /* cow and double check */
1591 ret = btrfs_cow_block(trans, root, right, upper,
1594 free_extent_buffer(right);
1597 free_space = btrfs_leaf_free_space(root, right);
1598 if (free_space < data_size + sizeof(struct btrfs_item)) {
1599 free_extent_buffer(right);
1603 left_nritems = btrfs_header_nritems(left);
1604 if (left_nritems == 0) {
1605 free_extent_buffer(right);
1614 i = left_nritems - 1;
1616 item = btrfs_item_nr(left, i);
1618 if (path->slots[0] == i)
1619 push_space += data_size + sizeof(*item);
1621 this_item_size = btrfs_item_size(left, item);
1622 if (this_item_size + sizeof(*item) + push_space > free_space)
1625 push_space += this_item_size + sizeof(*item);
1631 if (push_items == 0) {
1632 free_extent_buffer(right);
1636 if (!empty && push_items == left_nritems)
1639 /* push left to right */
1640 right_nritems = btrfs_header_nritems(right);
1642 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
1643 push_space -= leaf_data_end(root, left);
1645 /* make room in the right data area */
1646 data_end = leaf_data_end(root, right);
1647 memmove_extent_buffer(right,
1648 btrfs_leaf_data(right) + data_end - push_space,
1649 btrfs_leaf_data(right) + data_end,
1650 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1652 /* copy from the left data area */
1653 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
1654 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1655 btrfs_leaf_data(left) + leaf_data_end(root, left),
1658 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1659 btrfs_item_nr_offset(0),
1660 right_nritems * sizeof(struct btrfs_item));
1662 /* copy the items from left to right */
1663 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1664 btrfs_item_nr_offset(left_nritems - push_items),
1665 push_items * sizeof(struct btrfs_item));
1667 /* update the item pointers */
1668 right_nritems += push_items;
1669 btrfs_set_header_nritems(right, right_nritems);
1670 push_space = BTRFS_LEAF_DATA_SIZE(root);
1671 for (i = 0; i < right_nritems; i++) {
1672 item = btrfs_item_nr(right, i);
1673 push_space -= btrfs_item_size(right, item);
1674 btrfs_set_item_offset(right, item, push_space);
1677 left_nritems -= push_items;
1678 btrfs_set_header_nritems(left, left_nritems);
1681 btrfs_mark_buffer_dirty(left);
1682 btrfs_mark_buffer_dirty(right);
1684 btrfs_item_key(right, &disk_key, 0);
1685 btrfs_set_node_key(upper, &disk_key, slot + 1);
1686 btrfs_mark_buffer_dirty(upper);
1688 /* then fixup the leaf pointer in the path */
1689 if (path->slots[0] >= left_nritems) {
1690 path->slots[0] -= left_nritems;
1691 free_extent_buffer(path->nodes[0]);
1692 path->nodes[0] = right;
1693 path->slots[1] += 1;
1695 free_extent_buffer(right);
1700 * push some data in the path leaf to the left, trying to free up at
1701 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1703 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1704 *root, struct btrfs_path *path, int data_size,
1707 struct btrfs_disk_key disk_key;
1708 struct extent_buffer *right = path->nodes[0];
1709 struct extent_buffer *left;
1715 struct btrfs_item *item;
1716 u32 old_left_nritems;
1722 u32 old_left_item_size;
1724 slot = path->slots[1];
1727 if (!path->nodes[1])
1730 right_nritems = btrfs_header_nritems(right);
1731 if (right_nritems == 0) {
1735 left = read_tree_block(root, btrfs_node_blockptr(path->nodes[1],
1736 slot - 1), root->leafsize);
1737 free_space = btrfs_leaf_free_space(root, left);
1738 if (free_space < data_size + sizeof(struct btrfs_item)) {
1739 free_extent_buffer(left);
1743 /* cow and double check */
1744 ret = btrfs_cow_block(trans, root, left,
1745 path->nodes[1], slot - 1, &left);
1747 /* we hit -ENOSPC, but it isn't fatal here */
1748 free_extent_buffer(left);
1752 free_space = btrfs_leaf_free_space(root, left);
1753 if (free_space < data_size + sizeof(struct btrfs_item)) {
1754 free_extent_buffer(left);
1761 nr = right_nritems - 1;
1763 for (i = 0; i < nr; i++) {
1764 item = btrfs_item_nr(right, i);
1766 if (path->slots[0] == i)
1767 push_space += data_size + sizeof(*item);
1769 this_item_size = btrfs_item_size(right, item);
1770 if (this_item_size + sizeof(*item) + push_space > free_space)
1774 push_space += this_item_size + sizeof(*item);
1777 if (push_items == 0) {
1778 free_extent_buffer(left);
1781 if (!empty && push_items == btrfs_header_nritems(right))
1784 /* push data from right to left */
1785 copy_extent_buffer(left, right,
1786 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1787 btrfs_item_nr_offset(0),
1788 push_items * sizeof(struct btrfs_item));
1790 push_space = BTRFS_LEAF_DATA_SIZE(root) -
1791 btrfs_item_offset_nr(right, push_items -1);
1793 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
1794 leaf_data_end(root, left) - push_space,
1795 btrfs_leaf_data(right) +
1796 btrfs_item_offset_nr(right, push_items - 1),
1798 old_left_nritems = btrfs_header_nritems(left);
1799 BUG_ON(old_left_nritems < 0);
1801 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
1802 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
1805 item = btrfs_item_nr(left, i);
1806 ioff = btrfs_item_offset(left, item);
1807 btrfs_set_item_offset(left, item,
1808 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
1810 btrfs_set_header_nritems(left, old_left_nritems + push_items);
1812 /* fixup right node */
1813 if (push_items > right_nritems) {
1814 printk("push items %d nr %u\n", push_items, right_nritems);
1818 if (push_items < right_nritems) {
1819 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1820 leaf_data_end(root, right);
1821 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1822 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1823 btrfs_leaf_data(right) +
1824 leaf_data_end(root, right), push_space);
1826 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
1827 btrfs_item_nr_offset(push_items),
1828 (btrfs_header_nritems(right) - push_items) *
1829 sizeof(struct btrfs_item));
1831 right_nritems -= push_items;
1832 btrfs_set_header_nritems(right, right_nritems);
1833 push_space = BTRFS_LEAF_DATA_SIZE(root);
1834 for (i = 0; i < right_nritems; i++) {
1835 item = btrfs_item_nr(right, i);
1836 push_space = push_space - btrfs_item_size(right, item);
1837 btrfs_set_item_offset(right, item, push_space);
1840 btrfs_mark_buffer_dirty(left);
1842 btrfs_mark_buffer_dirty(right);
1844 btrfs_item_key(right, &disk_key, 0);
1845 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
1849 /* then fixup the leaf pointer in the path */
1850 if (path->slots[0] < push_items) {
1851 path->slots[0] += old_left_nritems;
1852 free_extent_buffer(path->nodes[0]);
1853 path->nodes[0] = left;
1854 path->slots[1] -= 1;
1856 free_extent_buffer(left);
1857 path->slots[0] -= push_items;
1859 BUG_ON(path->slots[0] < 0);
1864 * split the path's leaf in two, making sure there is at least data_size
1865 * available for the resulting leaf level of the path.
1867 * returns 0 if all went well and < 0 on failure.
1869 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
1870 *root, struct btrfs_key *ins_key,
1871 struct btrfs_path *path, int data_size, int extend)
1874 struct extent_buffer *l;
1878 struct extent_buffer *right;
1879 int space_needed = data_size + sizeof(struct btrfs_item);
1886 int num_doubles = 0;
1887 struct btrfs_disk_key disk_key;
1890 space_needed = data_size;
1893 root_gen = trans->transid;
1897 /* first try to make some room by pushing left and right */
1898 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
1899 wret = push_leaf_right(trans, root, path, data_size, 0);
1904 wret = push_leaf_left(trans, root, path, data_size, 0);
1910 /* did the pushes work? */
1911 if (btrfs_leaf_free_space(root, l) >= space_needed)
1915 if (!path->nodes[1]) {
1916 ret = insert_new_root(trans, root, path, 1);
1923 slot = path->slots[0];
1924 nritems = btrfs_header_nritems(l);
1925 mid = (nritems + 1)/ 2;
1927 btrfs_item_key(l, &disk_key, 0);
1929 right = __btrfs_alloc_free_block(trans, root, root->leafsize,
1930 root->root_key.objectid,
1931 root_gen, disk_key.objectid, 0,
1934 return PTR_ERR(right);
1936 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
1937 btrfs_set_header_bytenr(right, right->start);
1938 btrfs_set_header_generation(right, trans->transid);
1939 btrfs_set_header_owner(right, root->root_key.objectid);
1940 btrfs_set_header_level(right, 0);
1941 write_extent_buffer(right, root->fs_info->fsid,
1942 (unsigned long)btrfs_header_fsid(right),
1946 leaf_space_used(l, mid, nritems - mid) + space_needed >
1947 BTRFS_LEAF_DATA_SIZE(root)) {
1948 if (slot >= nritems) {
1949 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1950 btrfs_set_header_nritems(right, 0);
1951 wret = insert_ptr(trans, root, path,
1952 &disk_key, right->start,
1953 path->slots[1] + 1, 1);
1956 free_extent_buffer(path->nodes[0]);
1957 path->nodes[0] = right;
1959 path->slots[1] += 1;
1963 if (mid != nritems &&
1964 leaf_space_used(l, mid, nritems - mid) +
1965 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
1970 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1971 BTRFS_LEAF_DATA_SIZE(root)) {
1972 if (!extend && slot == 0) {
1973 btrfs_cpu_key_to_disk(&disk_key, ins_key);
1974 btrfs_set_header_nritems(right, 0);
1975 wret = insert_ptr(trans, root, path,
1981 free_extent_buffer(path->nodes[0]);
1982 path->nodes[0] = right;
1984 if (path->slots[1] == 0) {
1985 wret = fixup_low_keys(trans, root,
1986 path, &disk_key, 1);
1991 } else if (extend && slot == 0) {
1995 if (mid != nritems &&
1996 leaf_space_used(l, mid, nritems - mid) +
1997 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2003 nritems = nritems - mid;
2004 btrfs_set_header_nritems(right, nritems);
2005 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2007 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2008 btrfs_item_nr_offset(mid),
2009 nritems * sizeof(struct btrfs_item));
2011 copy_extent_buffer(right, l,
2012 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2013 data_copy_size, btrfs_leaf_data(l) +
2014 leaf_data_end(root, l), data_copy_size);
2016 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2017 btrfs_item_end_nr(l, mid);
2019 for (i = 0; i < nritems; i++) {
2020 struct btrfs_item *item = btrfs_item_nr(right, i);
2021 u32 ioff = btrfs_item_offset(right, item);
2022 btrfs_set_item_offset(right, item, ioff + rt_data_off);
2025 btrfs_set_header_nritems(l, mid);
2027 btrfs_item_key(right, &disk_key, 0);
2028 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2029 path->slots[1] + 1, 1);
2033 btrfs_mark_buffer_dirty(right);
2034 btrfs_mark_buffer_dirty(l);
2035 BUG_ON(path->slots[0] != slot);
2038 free_extent_buffer(path->nodes[0]);
2039 path->nodes[0] = right;
2040 path->slots[0] -= mid;
2041 path->slots[1] += 1;
2043 free_extent_buffer(right);
2045 BUG_ON(path->slots[0] < 0);
2048 BUG_ON(num_doubles != 0);
2055 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2056 struct btrfs_root *root,
2057 struct btrfs_path *path,
2058 u32 new_size, int from_end)
2063 struct extent_buffer *leaf;
2064 struct btrfs_item *item;
2066 unsigned int data_end;
2067 unsigned int old_data_start;
2068 unsigned int old_size;
2069 unsigned int size_diff;
2072 slot_orig = path->slots[0];
2073 leaf = path->nodes[0];
2074 slot = path->slots[0];
2076 old_size = btrfs_item_size_nr(leaf, slot);
2077 if (old_size == new_size)
2080 nritems = btrfs_header_nritems(leaf);
2081 data_end = leaf_data_end(root, leaf);
2083 old_data_start = btrfs_item_offset_nr(leaf, slot);
2085 size_diff = old_size - new_size;
2088 BUG_ON(slot >= nritems);
2091 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2093 /* first correct the data pointers */
2094 for (i = slot; i < nritems; i++) {
2096 item = btrfs_item_nr(leaf, i);
2097 ioff = btrfs_item_offset(leaf, item);
2098 btrfs_set_item_offset(leaf, item, ioff + size_diff);
2101 /* shift the data */
2103 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2104 data_end + size_diff, btrfs_leaf_data(leaf) +
2105 data_end, old_data_start + new_size - data_end);
2107 struct btrfs_disk_key disk_key;
2110 btrfs_item_key(leaf, &disk_key, slot);
2112 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2114 struct btrfs_file_extent_item *fi;
2116 fi = btrfs_item_ptr(leaf, slot,
2117 struct btrfs_file_extent_item);
2118 fi = (struct btrfs_file_extent_item *)(
2119 (unsigned long)fi - size_diff);
2121 if (btrfs_file_extent_type(leaf, fi) ==
2122 BTRFS_FILE_EXTENT_INLINE) {
2123 ptr = btrfs_item_ptr_offset(leaf, slot);
2124 memmove_extent_buffer(leaf, ptr,
2126 offsetof(struct btrfs_file_extent_item,
2131 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2132 data_end + size_diff, btrfs_leaf_data(leaf) +
2133 data_end, old_data_start - data_end);
2135 offset = btrfs_disk_key_offset(&disk_key);
2136 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2137 btrfs_set_item_key(leaf, &disk_key, slot);
2139 fixup_low_keys(trans, root, path, &disk_key, 1);
2142 item = btrfs_item_nr(leaf, slot);
2143 btrfs_set_item_size(leaf, item, new_size);
2144 btrfs_mark_buffer_dirty(leaf);
2147 if (btrfs_leaf_free_space(root, leaf) < 0) {
2148 btrfs_print_leaf(root, leaf);
2154 int btrfs_extend_item(struct btrfs_trans_handle *trans,
2155 struct btrfs_root *root, struct btrfs_path *path,
2161 struct extent_buffer *leaf;
2162 struct btrfs_item *item;
2164 unsigned int data_end;
2165 unsigned int old_data;
2166 unsigned int old_size;
2169 slot_orig = path->slots[0];
2170 leaf = path->nodes[0];
2172 nritems = btrfs_header_nritems(leaf);
2173 data_end = leaf_data_end(root, leaf);
2175 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2176 btrfs_print_leaf(root, leaf);
2179 slot = path->slots[0];
2180 old_data = btrfs_item_end_nr(leaf, slot);
2183 if (slot >= nritems) {
2184 btrfs_print_leaf(root, leaf);
2185 printk("slot %d too large, nritems %d\n", slot, nritems);
2190 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2192 /* first correct the data pointers */
2193 for (i = slot; i < nritems; i++) {
2195 item = btrfs_item_nr(leaf, i);
2196 ioff = btrfs_item_offset(leaf, item);
2197 btrfs_set_item_offset(leaf, item, ioff - data_size);
2200 /* shift the data */
2201 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2202 data_end - data_size, btrfs_leaf_data(leaf) +
2203 data_end, old_data - data_end);
2205 data_end = old_data;
2206 old_size = btrfs_item_size_nr(leaf, slot);
2207 item = btrfs_item_nr(leaf, slot);
2208 btrfs_set_item_size(leaf, item, old_size + data_size);
2209 btrfs_mark_buffer_dirty(leaf);
2212 if (btrfs_leaf_free_space(root, leaf) < 0) {
2213 btrfs_print_leaf(root, leaf);
2220 * Given a key and some data, insert an item into the tree.
2221 * This does all the path init required, making room in the tree if needed.
2223 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
2224 struct btrfs_root *root,
2225 struct btrfs_path *path,
2226 struct btrfs_key *cpu_key, u32 *data_size,
2229 struct extent_buffer *leaf;
2230 struct btrfs_item *item;
2238 unsigned int data_end;
2239 struct btrfs_disk_key disk_key;
2241 for (i = 0; i < nr; i++) {
2242 total_data += data_size[i];
2245 /* create a root if there isn't one */
2249 total_size = total_data + (nr - 1) * sizeof(struct btrfs_item);
2250 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
2257 slot_orig = path->slots[0];
2258 leaf = path->nodes[0];
2260 nritems = btrfs_header_nritems(leaf);
2261 data_end = leaf_data_end(root, leaf);
2263 if (btrfs_leaf_free_space(root, leaf) <
2264 sizeof(struct btrfs_item) + total_size) {
2265 btrfs_print_leaf(root, leaf);
2266 printk("not enough freespace need %u have %d\n",
2267 total_size, btrfs_leaf_free_space(root, leaf));
2271 slot = path->slots[0];
2274 if (slot != nritems) {
2276 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
2278 if (old_data < data_end) {
2279 btrfs_print_leaf(root, leaf);
2280 printk("slot %d old_data %d data_end %d\n",
2281 slot, old_data, data_end);
2285 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2287 /* first correct the data pointers */
2288 for (i = slot; i < nritems; i++) {
2291 item = btrfs_item_nr(leaf, i);
2292 ioff = btrfs_item_offset(leaf, item);
2293 btrfs_set_item_offset(leaf, item, ioff - total_data);
2296 /* shift the items */
2297 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
2298 btrfs_item_nr_offset(slot),
2299 (nritems - slot) * sizeof(struct btrfs_item));
2301 /* shift the data */
2302 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2303 data_end - total_data, btrfs_leaf_data(leaf) +
2304 data_end, old_data - data_end);
2305 data_end = old_data;
2308 /* setup the item for the new data */
2309 for (i = 0; i < nr; i++) {
2310 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2311 btrfs_set_item_key(leaf, &disk_key, slot + i);
2312 item = btrfs_item_nr(leaf, slot + i);
2313 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2314 data_end -= data_size[i];
2315 btrfs_set_item_size(leaf, item, data_size[i]);
2317 btrfs_set_header_nritems(leaf, nritems + nr);
2318 btrfs_mark_buffer_dirty(leaf);
2322 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
2323 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
2326 if (btrfs_leaf_free_space(root, leaf) < 0) {
2327 btrfs_print_leaf(root, leaf);
2336 * Given a key and some data, insert an item into the tree.
2337 * This does all the path init required, making room in the tree if needed.
2339 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2340 *root, struct btrfs_key *cpu_key, void *data, u32
2344 struct btrfs_path *path;
2345 struct extent_buffer *leaf;
2348 path = btrfs_alloc_path();
2350 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
2352 leaf = path->nodes[0];
2353 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2354 write_extent_buffer(leaf, data, ptr, data_size);
2355 btrfs_mark_buffer_dirty(leaf);
2357 btrfs_free_path(path);
2362 * delete the pointer from a given node.
2364 * If the delete empties a node, the node is removed from the tree,
2365 * continuing all the way the root if required. The root is converted into
2366 * a leaf if all the nodes are emptied.
2368 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2369 struct btrfs_path *path, int level, int slot)
2371 struct extent_buffer *parent = path->nodes[level];
2376 nritems = btrfs_header_nritems(parent);
2377 if (slot != nritems -1) {
2378 memmove_extent_buffer(parent,
2379 btrfs_node_key_ptr_offset(slot),
2380 btrfs_node_key_ptr_offset(slot + 1),
2381 sizeof(struct btrfs_key_ptr) *
2382 (nritems - slot - 1));
2385 btrfs_set_header_nritems(parent, nritems);
2386 if (nritems == 0 && parent == root->node) {
2387 BUG_ON(btrfs_header_level(root->node) != 1);
2388 /* just turn the root into a leaf and break */
2389 btrfs_set_header_level(root->node, 0);
2390 } else if (slot == 0) {
2391 struct btrfs_disk_key disk_key;
2393 btrfs_node_key(parent, &disk_key, 0);
2394 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
2398 btrfs_mark_buffer_dirty(parent);
2403 * delete the item at the leaf level in path. If that empties
2404 * the leaf, remove it from the tree
2406 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2407 struct btrfs_path *path, int slot, int nr)
2409 struct extent_buffer *leaf;
2410 struct btrfs_item *item;
2418 leaf = path->nodes[0];
2419 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2421 for (i = 0; i < nr; i++)
2422 dsize += btrfs_item_size_nr(leaf, slot + i);
2424 nritems = btrfs_header_nritems(leaf);
2426 if (slot + nr != nritems) {
2428 int data_end = leaf_data_end(root, leaf);
2430 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2432 btrfs_leaf_data(leaf) + data_end,
2433 last_off - data_end);
2435 for (i = slot + nr; i < nritems; i++) {
2438 item = btrfs_item_nr(leaf, i);
2439 ioff = btrfs_item_offset(leaf, item);
2440 btrfs_set_item_offset(leaf, item, ioff + dsize);
2443 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2444 btrfs_item_nr_offset(slot + nr),
2445 sizeof(struct btrfs_item) *
2446 (nritems - slot - nr));
2448 btrfs_set_header_nritems(leaf, nritems - nr);
2451 /* delete the leaf if we've emptied it */
2453 if (leaf == root->node) {
2454 btrfs_set_header_level(leaf, 0);
2456 u64 root_gen = btrfs_header_generation(path->nodes[1]);
2457 clean_tree_block(trans, root, leaf);
2458 wait_on_tree_block_writeback(root, leaf);
2459 wret = del_ptr(trans, root, path, 1, path->slots[1]);
2462 wret = btrfs_free_extent(trans, root,
2463 leaf->start, leaf->len,
2464 btrfs_header_owner(path->nodes[1]),
2470 int used = leaf_space_used(leaf, 0, nritems);
2472 struct btrfs_disk_key disk_key;
2474 btrfs_item_key(leaf, &disk_key, 0);
2475 wret = fixup_low_keys(trans, root, path,
2481 /* delete the leaf if it is mostly empty */
2482 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
2483 /* push_leaf_left fixes the path.
2484 * make sure the path still points to our leaf
2485 * for possible call to del_ptr below
2487 slot = path->slots[1];
2488 extent_buffer_get(leaf);
2490 wret = push_leaf_left(trans, root, path, 1, 1);
2491 if (wret < 0 && wret != -ENOSPC)
2494 if (path->nodes[0] == leaf &&
2495 btrfs_header_nritems(leaf)) {
2496 wret = push_leaf_right(trans, root, path, 1, 1);
2497 if (wret < 0 && wret != -ENOSPC)
2501 if (btrfs_header_nritems(leaf) == 0) {
2503 u64 bytenr = leaf->start;
2504 u32 blocksize = leaf->len;
2506 root_gen = btrfs_header_generation(
2509 clean_tree_block(trans, root, leaf);
2510 wait_on_tree_block_writeback(root, leaf);
2512 wret = del_ptr(trans, root, path, 1, slot);
2516 free_extent_buffer(leaf);
2517 wret = btrfs_free_extent(trans, root, bytenr,
2519 btrfs_header_owner(path->nodes[1]),
2524 btrfs_mark_buffer_dirty(leaf);
2525 free_extent_buffer(leaf);
2528 btrfs_mark_buffer_dirty(leaf);
2535 * walk up the tree as far as required to find the previous leaf.
2536 * returns 0 if it found something or 1 if there are no lesser leaves.
2537 * returns < 0 on io errors.
2539 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2544 struct extent_buffer *c;
2545 struct extent_buffer *next = NULL;
2547 while(level < BTRFS_MAX_LEVEL) {
2548 if (!path->nodes[level])
2551 slot = path->slots[level];
2552 c = path->nodes[level];
2555 if (level == BTRFS_MAX_LEVEL)
2561 bytenr = btrfs_node_blockptr(c, slot);
2563 free_extent_buffer(next);
2565 next = read_tree_block(root, bytenr,
2566 btrfs_level_size(root, level - 1));
2569 path->slots[level] = slot;
2572 c = path->nodes[level];
2573 free_extent_buffer(c);
2574 slot = btrfs_header_nritems(next);
2577 path->nodes[level] = next;
2578 path->slots[level] = slot;
2581 next = read_tree_block(root, btrfs_node_blockptr(next, slot),
2582 btrfs_level_size(root, level - 1));
2588 * walk up the tree as far as required to find the next leaf.
2589 * returns 0 if it found something or 1 if there are no greater leaves.
2590 * returns < 0 on io errors.
2592 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
2597 struct extent_buffer *c;
2598 struct extent_buffer *next = NULL;
2600 while(level < BTRFS_MAX_LEVEL) {
2601 if (!path->nodes[level])
2604 slot = path->slots[level] + 1;
2605 c = path->nodes[level];
2606 if (slot >= btrfs_header_nritems(c)) {
2608 if (level == BTRFS_MAX_LEVEL)
2613 bytenr = btrfs_node_blockptr(c, slot);
2615 free_extent_buffer(next);
2618 reada_for_search(root, path, level, slot, 0);
2620 next = read_tree_block(root, bytenr,
2621 btrfs_level_size(root, level -1));
2624 path->slots[level] = slot;
2627 c = path->nodes[level];
2628 free_extent_buffer(c);
2629 path->nodes[level] = next;
2630 path->slots[level] = 0;
2634 reada_for_search(root, path, level, 0, 0);
2635 next = read_tree_block(root, btrfs_node_blockptr(next, 0),
2636 btrfs_level_size(root, level - 1));
2641 int btrfs_previous_item(struct btrfs_root *root,
2642 struct btrfs_path *path, u64 min_objectid,
2645 struct btrfs_key found_key;
2646 struct extent_buffer *leaf;
2650 if (path->slots[0] == 0) {
2651 ret = btrfs_prev_leaf(root, path);
2657 leaf = path->nodes[0];
2658 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2659 if (found_key.type == type)