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.
19 #include <linux/sched.h>
22 #include "transaction.h"
23 #include "print-tree.h"
25 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
26 *root, struct btrfs_path *path, int level);
27 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
28 *root, struct btrfs_key *ins_key,
29 struct btrfs_path *path, int data_size, int extend);
30 static int push_node_left(struct btrfs_trans_handle *trans,
31 struct btrfs_root *root, struct extent_buffer *dst,
32 struct extent_buffer *src, int empty);
33 static int balance_node_right(struct btrfs_trans_handle *trans,
34 struct btrfs_root *root,
35 struct extent_buffer *dst_buf,
36 struct extent_buffer *src_buf);
37 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
38 struct btrfs_path *path, int level, int slot);
40 inline void btrfs_init_path(struct btrfs_path *p)
42 memset(p, 0, sizeof(*p));
45 struct btrfs_path *btrfs_alloc_path(void)
47 struct btrfs_path *path;
48 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
50 btrfs_init_path(path);
56 void btrfs_free_path(struct btrfs_path *p)
58 btrfs_release_path(NULL, p);
59 kmem_cache_free(btrfs_path_cachep, p);
62 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
65 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
68 free_extent_buffer(p->nodes[i]);
70 memset(p, 0, sizeof(*p));
73 static void add_root_to_dirty_list(struct btrfs_root *root)
75 if (root->track_dirty && list_empty(&root->dirty_list)) {
76 list_add(&root->dirty_list,
77 &root->fs_info->dirty_cowonly_roots);
81 int btrfs_copy_root(struct btrfs_trans_handle *trans,
82 struct btrfs_root *root,
83 struct extent_buffer *buf,
84 struct extent_buffer **cow_ret, u64 new_root_objectid)
86 struct extent_buffer *cow;
90 struct btrfs_key first_key;
91 struct btrfs_root *new_root;
93 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
97 memcpy(new_root, root, sizeof(*new_root));
98 new_root->root_key.objectid = new_root_objectid;
100 WARN_ON(root->ref_cows && trans->transid !=
101 root->fs_info->running_transaction->transid);
102 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
104 level = btrfs_header_level(buf);
105 nritems = btrfs_header_nritems(buf);
108 btrfs_item_key_to_cpu(buf, &first_key, 0);
110 btrfs_node_key_to_cpu(buf, &first_key, 0);
112 first_key.objectid = 0;
114 cow = __btrfs_alloc_free_block(trans, new_root, buf->len,
116 trans->transid, first_key.objectid,
117 level, buf->start, 0);
123 copy_extent_buffer(cow, buf, 0, 0, cow->len);
124 btrfs_set_header_bytenr(cow, cow->start);
125 btrfs_set_header_generation(cow, trans->transid);
126 btrfs_set_header_owner(cow, new_root_objectid);
127 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
129 WARN_ON(btrfs_header_generation(buf) > trans->transid);
130 ret = btrfs_inc_ref(trans, new_root, buf);
136 btrfs_mark_buffer_dirty(cow);
141 int __btrfs_cow_block(struct btrfs_trans_handle *trans,
142 struct btrfs_root *root,
143 struct extent_buffer *buf,
144 struct extent_buffer *parent, int parent_slot,
145 struct extent_buffer **cow_ret,
146 u64 search_start, u64 empty_size)
149 struct extent_buffer *cow;
152 int different_trans = 0;
154 struct btrfs_key first_key;
156 if (root->ref_cows) {
157 root_gen = trans->transid;
161 WARN_ON(root->ref_cows && trans->transid !=
162 root->fs_info->running_transaction->transid);
163 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
165 level = btrfs_header_level(buf);
166 nritems = btrfs_header_nritems(buf);
169 btrfs_item_key_to_cpu(buf, &first_key, 0);
171 btrfs_node_key_to_cpu(buf, &first_key, 0);
173 first_key.objectid = 0;
175 cow = __btrfs_alloc_free_block(trans, root, buf->len,
176 root->root_key.objectid,
177 root_gen, first_key.objectid, level,
178 search_start, empty_size);
182 copy_extent_buffer(cow, buf, 0, 0, cow->len);
183 btrfs_set_header_bytenr(cow, cow->start);
184 btrfs_set_header_generation(cow, trans->transid);
185 btrfs_set_header_owner(cow, root->root_key.objectid);
186 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
188 WARN_ON(btrfs_header_generation(buf) > trans->transid);
189 if (btrfs_header_generation(buf) != trans->transid) {
191 ret = btrfs_inc_ref(trans, root, buf);
195 clean_tree_block(trans, root, buf);
198 if (buf == root->node) {
199 root_gen = btrfs_header_generation(buf);
201 extent_buffer_get(cow);
202 if (buf != root->commit_root) {
203 btrfs_free_extent(trans, root, buf->start,
204 buf->len, root->root_key.objectid,
207 free_extent_buffer(buf);
208 add_root_to_dirty_list(root);
210 root_gen = btrfs_header_generation(parent);
211 btrfs_set_node_blockptr(parent, parent_slot,
213 WARN_ON(trans->transid == 0);
214 btrfs_set_node_ptr_generation(parent, parent_slot,
216 btrfs_mark_buffer_dirty(parent);
217 WARN_ON(btrfs_header_generation(parent) != trans->transid);
218 btrfs_free_extent(trans, root, buf->start, buf->len,
219 btrfs_header_owner(parent), root_gen,
222 free_extent_buffer(buf);
223 btrfs_mark_buffer_dirty(cow);
228 int btrfs_cow_block(struct btrfs_trans_handle *trans,
229 struct btrfs_root *root, struct extent_buffer *buf,
230 struct extent_buffer *parent, int parent_slot,
231 struct extent_buffer **cow_ret)
237 if (trans->transaction != root->fs_info->running_transaction) {
238 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
239 root->fs_info->running_transaction->transid);
242 if (trans->transid != root->fs_info->generation) {
243 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
244 root->fs_info->generation);
248 header_trans = btrfs_header_generation(buf);
249 spin_lock(&root->fs_info->hash_lock);
250 if (header_trans == trans->transid &&
251 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
253 spin_unlock(&root->fs_info->hash_lock);
256 spin_unlock(&root->fs_info->hash_lock);
257 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
258 ret = __btrfs_cow_block(trans, root, buf, parent,
259 parent_slot, cow_ret, search_start, 0);
263 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
265 if (blocknr < other && other - (blocknr + blocksize) < 32768)
267 if (blocknr > other && blocknr - (other + blocksize) < 32768)
273 * compare two keys in a memcmp fashion
275 static int 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)
297 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
298 struct btrfs_root *root, struct extent_buffer *parent,
299 int start_slot, int cache_only, u64 *last_ret,
300 struct btrfs_key *progress)
302 struct extent_buffer *cur;
303 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);
358 gen = btrfs_node_ptr_generation(parent, i);
360 last_block = blocknr;
363 other = btrfs_node_blockptr(parent, i - 1);
364 close = close_blocks(blocknr, other, blocksize);
366 if (close && i < end_slot - 2) {
367 other = btrfs_node_blockptr(parent, i + 1);
368 close = close_blocks(blocknr, other, blocksize);
371 last_block = blocknr;
374 if (parent->map_token) {
375 unmap_extent_buffer(parent, parent->map_token,
377 parent->map_token = NULL;
380 cur = btrfs_find_tree_block(root, blocknr, blocksize);
382 uptodate = btrfs_buffer_uptodate(cur, gen);
385 if (!cur || !uptodate) {
387 free_extent_buffer(cur);
391 cur = read_tree_block(root, blocknr,
393 } else if (!uptodate) {
394 btrfs_read_buffer(cur, gen);
397 if (search_start == 0)
398 search_start = last_block;
400 err = __btrfs_cow_block(trans, root, cur, parent, i,
403 (end_slot - i) * blocksize));
405 free_extent_buffer(cur);
408 search_start = tmp->start;
409 last_block = tmp->start;
410 *last_ret = search_start;
411 if (parent_level == 1)
412 btrfs_clear_buffer_defrag(tmp);
413 free_extent_buffer(tmp);
415 if (parent->map_token) {
416 unmap_extent_buffer(parent, parent->map_token,
418 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(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(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 (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(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 if (btrfs_header_level(path->nodes[level]) != level)
576 printk("warning: bad level %Lu wanted %d found %d\n",
577 path->nodes[level]->start, level,
578 btrfs_header_level(path->nodes[level]));
579 found_start = btrfs_header_bytenr(path->nodes[level]);
580 if (found_start != path->nodes[level]->start) {
581 printk("warning: bad bytentr %Lu found %Lu\n",
582 path->nodes[level]->start, found_start);
585 struct extent_buffer *buf = path->nodes[level];
587 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
588 (unsigned long)btrfs_header_fsid(buf),
590 printk("warning bad block %Lu\n", buf->start);
595 return check_leaf(root, path, level);
596 return check_node(root, path, level);
600 * search for key in the extent_buffer. The items start at offset p,
601 * and they are item_size apart. There are 'max' items in p.
603 * the slot in the array is returned via slot, and it points to
604 * the place where you would insert key if it is not found in
607 * slot may point to max if the key is bigger than all of the keys
609 static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
610 int item_size, struct btrfs_key *key,
617 struct btrfs_disk_key *tmp = NULL;
618 struct btrfs_disk_key unaligned;
619 unsigned long offset;
620 char *map_token = NULL;
622 unsigned long map_start = 0;
623 unsigned long map_len = 0;
627 mid = (low + high) / 2;
628 offset = p + mid * item_size;
630 if (!map_token || offset < map_start ||
631 (offset + sizeof(struct btrfs_disk_key)) >
632 map_start + map_len) {
634 unmap_extent_buffer(eb, map_token, KM_USER0);
637 err = map_extent_buffer(eb, offset,
638 sizeof(struct btrfs_disk_key),
640 &map_start, &map_len, KM_USER0);
643 tmp = (struct btrfs_disk_key *)(kaddr + offset -
646 read_extent_buffer(eb, &unaligned,
647 offset, sizeof(unaligned));
652 tmp = (struct btrfs_disk_key *)(kaddr + offset -
655 ret = comp_keys(tmp, key);
664 unmap_extent_buffer(eb, map_token, KM_USER0);
670 unmap_extent_buffer(eb, map_token, KM_USER0);
675 * simple bin_search frontend that does the right thing for
678 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
679 int level, int *slot)
682 return generic_bin_search(eb,
683 offsetof(struct btrfs_leaf, items),
684 sizeof(struct btrfs_item),
685 key, btrfs_header_nritems(eb),
688 return generic_bin_search(eb,
689 offsetof(struct btrfs_node, ptrs),
690 sizeof(struct btrfs_key_ptr),
691 key, btrfs_header_nritems(eb),
697 static struct extent_buffer *read_node_slot(struct btrfs_root *root,
698 struct extent_buffer *parent, int slot)
700 int level = btrfs_header_level(parent);
703 if (slot >= btrfs_header_nritems(parent))
708 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
709 btrfs_level_size(root, level - 1),
710 btrfs_node_ptr_generation(parent, slot));
713 static int balance_level(struct btrfs_trans_handle *trans,
714 struct btrfs_root *root,
715 struct btrfs_path *path, int level)
717 struct extent_buffer *right = NULL;
718 struct extent_buffer *mid;
719 struct extent_buffer *left = NULL;
720 struct extent_buffer *parent = NULL;
724 int orig_slot = path->slots[level];
725 int err_on_enospc = 0;
731 mid = path->nodes[level];
732 WARN_ON(btrfs_header_generation(mid) != trans->transid);
734 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
736 if (level < BTRFS_MAX_LEVEL - 1)
737 parent = path->nodes[level + 1];
738 pslot = path->slots[level + 1];
741 * deal with the case where there is only one pointer in the root
742 * by promoting the node below to a root
745 struct extent_buffer *child;
747 if (btrfs_header_nritems(mid) != 1)
750 /* promote the child to a root */
751 child = read_node_slot(root, mid, 0);
753 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
757 add_root_to_dirty_list(root);
758 path->nodes[level] = NULL;
759 clean_tree_block(trans, root, mid);
760 /* once for the path */
761 free_extent_buffer(mid);
762 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
763 root->root_key.objectid,
764 btrfs_header_generation(mid), 0, 0, 1);
765 /* once for the root ptr */
766 free_extent_buffer(mid);
769 if (btrfs_header_nritems(mid) >
770 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
773 if (btrfs_header_nritems(mid) < 2)
776 left = read_node_slot(root, parent, pslot - 1);
778 wret = btrfs_cow_block(trans, root, left,
779 parent, pslot - 1, &left);
785 right = read_node_slot(root, parent, pslot + 1);
787 wret = btrfs_cow_block(trans, root, right,
788 parent, pslot + 1, &right);
795 /* first, try to make some room in the middle buffer */
797 orig_slot += btrfs_header_nritems(left);
798 wret = push_node_left(trans, root, left, mid, 1);
801 if (btrfs_header_nritems(mid) < 2)
806 * then try to empty the right most buffer into the middle
809 wret = push_node_left(trans, root, mid, right, 1);
810 if (wret < 0 && wret != -ENOSPC)
812 if (btrfs_header_nritems(right) == 0) {
813 u64 bytenr = right->start;
814 u64 generation = btrfs_header_generation(parent);
815 u32 blocksize = right->len;
817 clean_tree_block(trans, root, right);
818 free_extent_buffer(right);
820 wret = del_ptr(trans, root, path, level + 1, pslot +
824 wret = btrfs_free_extent(trans, root, bytenr,
826 btrfs_header_owner(parent),
827 generation, 0, 0, 1);
831 struct btrfs_disk_key right_key;
832 btrfs_node_key(right, &right_key, 0);
833 btrfs_set_node_key(parent, &right_key, pslot + 1);
834 btrfs_mark_buffer_dirty(parent);
837 if (btrfs_header_nritems(mid) == 1) {
839 * we're not allowed to leave a node with one item in the
840 * tree during a delete. A deletion from lower in the tree
841 * could try to delete the only pointer in this node.
842 * So, pull some keys from the left.
843 * There has to be a left pointer at this point because
844 * otherwise we would have pulled some pointers from the
848 wret = balance_node_right(trans, root, mid, left);
854 wret = push_node_left(trans, root, left, mid, 1);
860 if (btrfs_header_nritems(mid) == 0) {
861 /* we've managed to empty the middle node, drop it */
862 u64 root_gen = btrfs_header_generation(parent);
863 u64 bytenr = mid->start;
864 u32 blocksize = mid->len;
865 clean_tree_block(trans, root, mid);
866 free_extent_buffer(mid);
868 wret = del_ptr(trans, root, path, level + 1, pslot);
871 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
872 btrfs_header_owner(parent),
877 /* update the parent key to reflect our changes */
878 struct btrfs_disk_key mid_key;
879 btrfs_node_key(mid, &mid_key, 0);
880 btrfs_set_node_key(parent, &mid_key, pslot);
881 btrfs_mark_buffer_dirty(parent);
884 /* update the path */
886 if (btrfs_header_nritems(left) > orig_slot) {
887 extent_buffer_get(left);
888 path->nodes[level] = left;
889 path->slots[level + 1] -= 1;
890 path->slots[level] = orig_slot;
892 free_extent_buffer(mid);
894 orig_slot -= btrfs_header_nritems(left);
895 path->slots[level] = orig_slot;
898 /* double check we haven't messed things up */
899 check_block(root, path, level);
901 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
905 free_extent_buffer(right);
907 free_extent_buffer(left);
911 /* returns zero if the push worked, non-zero otherwise */
912 static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
913 struct btrfs_root *root,
914 struct btrfs_path *path, int level)
916 struct extent_buffer *right = NULL;
917 struct extent_buffer *mid;
918 struct extent_buffer *left = NULL;
919 struct extent_buffer *parent = NULL;
923 int orig_slot = path->slots[level];
929 mid = path->nodes[level];
930 WARN_ON(btrfs_header_generation(mid) != trans->transid);
931 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
933 if (level < BTRFS_MAX_LEVEL - 1)
934 parent = path->nodes[level + 1];
935 pslot = path->slots[level + 1];
940 left = read_node_slot(root, parent, pslot - 1);
942 /* first, try to make some room in the middle buffer */
945 left_nr = btrfs_header_nritems(left);
946 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
949 ret = btrfs_cow_block(trans, root, left, parent,
954 wret = push_node_left(trans, root,
961 struct btrfs_disk_key disk_key;
962 orig_slot += left_nr;
963 btrfs_node_key(mid, &disk_key, 0);
964 btrfs_set_node_key(parent, &disk_key, pslot);
965 btrfs_mark_buffer_dirty(parent);
966 if (btrfs_header_nritems(left) > orig_slot) {
967 path->nodes[level] = left;
968 path->slots[level + 1] -= 1;
969 path->slots[level] = orig_slot;
970 free_extent_buffer(mid);
973 btrfs_header_nritems(left);
974 path->slots[level] = orig_slot;
975 free_extent_buffer(left);
979 free_extent_buffer(left);
981 right= read_node_slot(root, parent, pslot + 1);
984 * then try to empty the right most buffer into the middle
988 right_nr = btrfs_header_nritems(right);
989 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
992 ret = btrfs_cow_block(trans, root, right,
998 wret = balance_node_right(trans, root,
1005 struct btrfs_disk_key disk_key;
1007 btrfs_node_key(right, &disk_key, 0);
1008 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1009 btrfs_mark_buffer_dirty(parent);
1011 if (btrfs_header_nritems(mid) <= orig_slot) {
1012 path->nodes[level] = right;
1013 path->slots[level + 1] += 1;
1014 path->slots[level] = orig_slot -
1015 btrfs_header_nritems(mid);
1016 free_extent_buffer(mid);
1018 free_extent_buffer(right);
1022 free_extent_buffer(right);
1028 * readahead one full node of leaves
1030 static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
1031 int level, int slot, u64 objectid)
1033 struct extent_buffer *node;
1034 struct btrfs_disk_key disk_key;
1040 int direction = path->reada;
1041 struct extent_buffer *eb;
1049 if (!path->nodes[level])
1052 node = path->nodes[level];
1053 search = btrfs_node_blockptr(node, slot);
1054 blocksize = btrfs_level_size(root, level - 1);
1055 eb = btrfs_find_tree_block(root, search, blocksize);
1057 free_extent_buffer(eb);
1061 highest_read = search;
1062 lowest_read = search;
1064 nritems = btrfs_header_nritems(node);
1067 if (direction < 0) {
1071 } else if (direction > 0) {
1076 if (path->reada < 0 && objectid) {
1077 btrfs_node_key(node, &disk_key, nr);
1078 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1081 search = btrfs_node_blockptr(node, nr);
1082 if ((search >= lowest_read && search <= highest_read) ||
1083 (search < lowest_read && lowest_read - search <= 32768) ||
1084 (search > highest_read && search - highest_read <= 32768)) {
1085 readahead_tree_block(root, search, blocksize,
1086 btrfs_node_ptr_generation(node, nr));
1090 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1092 if(nread > (1024 * 1024) || nscan > 128)
1095 if (search < lowest_read)
1096 lowest_read = search;
1097 if (search > highest_read)
1098 highest_read = search;
1102 * look for key in the tree. path is filled in with nodes along the way
1103 * if key is found, we return zero and you can find the item in the leaf
1104 * level of the path (level 0)
1106 * If the key isn't found, the path points to the slot where it should
1107 * be inserted, and 1 is returned. If there are other errors during the
1108 * search a negative error number is returned.
1110 * if ins_len > 0, nodes and leaves will be split as we walk down the
1111 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1114 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1115 *root, struct btrfs_key *key, struct btrfs_path *p, int
1118 struct extent_buffer *b;
1122 int should_reada = p->reada;
1123 u8 lowest_level = 0;
1125 lowest_level = p->lowest_level;
1126 WARN_ON(lowest_level && ins_len);
1127 WARN_ON(p->nodes[0] != NULL);
1128 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1131 extent_buffer_get(b);
1133 level = btrfs_header_level(b);
1136 wret = btrfs_cow_block(trans, root, b,
1137 p->nodes[level + 1],
1138 p->slots[level + 1],
1141 free_extent_buffer(b);
1145 BUG_ON(!cow && ins_len);
1146 if (level != btrfs_header_level(b))
1148 level = btrfs_header_level(b);
1149 p->nodes[level] = b;
1150 ret = check_block(root, p, level);
1153 ret = bin_search(b, key, level, &slot);
1155 if (ret && slot > 0)
1157 p->slots[level] = slot;
1158 if (ins_len > 0 && btrfs_header_nritems(b) >=
1159 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1160 int sret = split_node(trans, root, p, level);
1164 b = p->nodes[level];
1165 slot = p->slots[level];
1166 } else if (ins_len < 0) {
1167 int sret = balance_level(trans, root, p,
1171 b = p->nodes[level];
1173 btrfs_release_path(NULL, p);
1176 slot = p->slots[level];
1177 BUG_ON(btrfs_header_nritems(b) == 1);
1179 /* this is only true while dropping a snapshot */
1180 if (level == lowest_level)
1184 reada_for_search(root, p, level, slot,
1187 b = read_node_slot(root, b, slot);
1189 p->slots[level] = slot;
1190 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
1191 sizeof(struct btrfs_item) + ins_len) {
1192 int sret = split_leaf(trans, root, key,
1193 p, ins_len, ret == 0);
1205 * adjust the pointers going up the tree, starting at level
1206 * making sure the right key of each node is points to 'key'.
1207 * This is used after shifting pointers to the left, so it stops
1208 * fixing up pointers when a given leaf/node is not in slot 0 of the
1211 * If this fails to write a tree block, it returns -1, but continues
1212 * fixing up the blocks in ram so the tree is consistent.
1214 static int fixup_low_keys(struct btrfs_trans_handle *trans,
1215 struct btrfs_root *root, struct btrfs_path *path,
1216 struct btrfs_disk_key *key, int level)
1220 struct extent_buffer *t;
1222 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1223 int tslot = path->slots[i];
1224 if (!path->nodes[i])
1227 btrfs_set_node_key(t, key, tslot);
1228 btrfs_mark_buffer_dirty(path->nodes[i]);
1236 * try to push data from one node into the next node left in the
1239 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1240 * error, and > 0 if there was no room in the left hand block.
1242 static int push_node_left(struct btrfs_trans_handle *trans,
1243 struct btrfs_root *root, struct extent_buffer *dst,
1244 struct extent_buffer *src, int empty)
1251 src_nritems = btrfs_header_nritems(src);
1252 dst_nritems = btrfs_header_nritems(dst);
1253 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1254 WARN_ON(btrfs_header_generation(src) != trans->transid);
1255 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1257 if (!empty && src_nritems <= 8)
1260 if (push_items <= 0) {
1265 push_items = min(src_nritems, push_items);
1266 if (push_items < src_nritems) {
1267 /* leave at least 8 pointers in the node if
1268 * we aren't going to empty it
1270 if (src_nritems - push_items < 8) {
1271 if (push_items <= 8)
1277 push_items = min(src_nritems - 8, push_items);
1279 copy_extent_buffer(dst, src,
1280 btrfs_node_key_ptr_offset(dst_nritems),
1281 btrfs_node_key_ptr_offset(0),
1282 push_items * sizeof(struct btrfs_key_ptr));
1284 if (push_items < src_nritems) {
1285 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1286 btrfs_node_key_ptr_offset(push_items),
1287 (src_nritems - push_items) *
1288 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);
1292 btrfs_mark_buffer_dirty(src);
1293 btrfs_mark_buffer_dirty(dst);
1298 * try to push data from one node into the next node right in the
1301 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1302 * error, and > 0 if there was no room in the right hand block.
1304 * this will only push up to 1/2 the contents of the left node over
1306 static int balance_node_right(struct btrfs_trans_handle *trans,
1307 struct btrfs_root *root,
1308 struct extent_buffer *dst,
1309 struct extent_buffer *src)
1317 WARN_ON(btrfs_header_generation(src) != trans->transid);
1318 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1320 src_nritems = btrfs_header_nritems(src);
1321 dst_nritems = btrfs_header_nritems(dst);
1322 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1323 if (push_items <= 0) {
1327 if (src_nritems < 4) {
1331 max_push = src_nritems / 2 + 1;
1332 /* don't try to empty the node */
1333 if (max_push >= src_nritems) {
1337 if (max_push < push_items)
1338 push_items = max_push;
1340 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1341 btrfs_node_key_ptr_offset(0),
1343 sizeof(struct btrfs_key_ptr));
1345 copy_extent_buffer(dst, src,
1346 btrfs_node_key_ptr_offset(0),
1347 btrfs_node_key_ptr_offset(src_nritems - push_items),
1348 push_items * sizeof(struct btrfs_key_ptr));
1350 btrfs_set_header_nritems(src, src_nritems - push_items);
1351 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1353 btrfs_mark_buffer_dirty(src);
1354 btrfs_mark_buffer_dirty(dst);
1359 * helper function to insert a new root level in the tree.
1360 * A new node is allocated, and a single item is inserted to
1361 * point to the existing root
1363 * returns zero on success or < 0 on failure.
1365 static int noinline insert_new_root(struct btrfs_trans_handle *trans,
1366 struct btrfs_root *root,
1367 struct btrfs_path *path, int level)
1371 struct extent_buffer *lower;
1372 struct extent_buffer *c;
1373 struct btrfs_disk_key lower_key;
1375 BUG_ON(path->nodes[level]);
1376 BUG_ON(path->nodes[level-1] != root->node);
1379 root_gen = trans->transid;
1383 lower = path->nodes[level-1];
1385 btrfs_item_key(lower, &lower_key, 0);
1387 btrfs_node_key(lower, &lower_key, 0);
1389 c = __btrfs_alloc_free_block(trans, root, root->nodesize,
1390 root->root_key.objectid,
1391 root_gen, lower_key.objectid, level,
1392 root->node->start, 0);
1395 memset_extent_buffer(c, 0, 0, root->nodesize);
1396 btrfs_set_header_nritems(c, 1);
1397 btrfs_set_header_level(c, level);
1398 btrfs_set_header_bytenr(c, c->start);
1399 btrfs_set_header_generation(c, trans->transid);
1400 btrfs_set_header_owner(c, root->root_key.objectid);
1402 write_extent_buffer(c, root->fs_info->fsid,
1403 (unsigned long)btrfs_header_fsid(c),
1406 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1407 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1410 btrfs_set_node_key(c, &lower_key, 0);
1411 btrfs_set_node_blockptr(c, 0, lower->start);
1412 lower_gen = btrfs_header_generation(lower);
1413 WARN_ON(lower_gen == 0);
1415 btrfs_set_node_ptr_generation(c, 0, lower_gen);
1417 btrfs_mark_buffer_dirty(c);
1419 /* the super has an extra ref to root->node */
1420 free_extent_buffer(root->node);
1422 add_root_to_dirty_list(root);
1423 extent_buffer_get(c);
1424 path->nodes[level] = c;
1425 path->slots[level] = 0;
1427 if (root->ref_cows && lower_gen != trans->transid) {
1428 struct btrfs_path *back_path = btrfs_alloc_path();
1430 ret = btrfs_insert_extent_backref(trans,
1431 root->fs_info->extent_root,
1433 root->root_key.objectid,
1434 trans->transid, 0, 0);
1436 btrfs_free_path(back_path);
1442 * worker function to insert a single pointer in a node.
1443 * the node should have enough room for the pointer already
1445 * slot and level indicate where you want the key to go, and
1446 * blocknr is the block the key points to.
1448 * returns zero on success and < 0 on any error
1450 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1451 *root, struct btrfs_path *path, struct btrfs_disk_key
1452 *key, u64 bytenr, int slot, int level)
1454 struct extent_buffer *lower;
1457 BUG_ON(!path->nodes[level]);
1458 lower = path->nodes[level];
1459 nritems = btrfs_header_nritems(lower);
1462 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
1464 if (slot != nritems) {
1465 memmove_extent_buffer(lower,
1466 btrfs_node_key_ptr_offset(slot + 1),
1467 btrfs_node_key_ptr_offset(slot),
1468 (nritems - slot) * sizeof(struct btrfs_key_ptr));
1470 btrfs_set_node_key(lower, key, slot);
1471 btrfs_set_node_blockptr(lower, slot, bytenr);
1472 WARN_ON(trans->transid == 0);
1473 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
1474 btrfs_set_header_nritems(lower, nritems + 1);
1475 btrfs_mark_buffer_dirty(lower);
1480 * split the node at the specified level in path in two.
1481 * The path is corrected to point to the appropriate node after the split
1483 * Before splitting this tries to make some room in the node by pushing
1484 * left and right, if either one works, it returns right away.
1486 * returns 0 on success and < 0 on failure
1488 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1489 *root, struct btrfs_path *path, int level)
1492 struct extent_buffer *c;
1493 struct extent_buffer *split;
1494 struct btrfs_disk_key disk_key;
1500 c = path->nodes[level];
1501 WARN_ON(btrfs_header_generation(c) != trans->transid);
1502 if (c == root->node) {
1503 /* trying to split the root, lets make a new one */
1504 ret = insert_new_root(trans, root, path, level + 1);
1508 ret = push_nodes_for_insert(trans, root, path, level);
1509 c = path->nodes[level];
1510 if (!ret && btrfs_header_nritems(c) <
1511 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
1517 c_nritems = btrfs_header_nritems(c);
1519 root_gen = trans->transid;
1523 btrfs_node_key(c, &disk_key, 0);
1524 split = __btrfs_alloc_free_block(trans, root, root->nodesize,
1525 root->root_key.objectid,
1527 btrfs_disk_key_objectid(&disk_key),
1528 level, c->start, 0);
1530 return PTR_ERR(split);
1532 btrfs_set_header_flags(split, btrfs_header_flags(c));
1533 btrfs_set_header_level(split, btrfs_header_level(c));
1534 btrfs_set_header_bytenr(split, split->start);
1535 btrfs_set_header_generation(split, trans->transid);
1536 btrfs_set_header_owner(split, root->root_key.objectid);
1537 btrfs_set_header_flags(split, 0);
1538 write_extent_buffer(split, root->fs_info->fsid,
1539 (unsigned long)btrfs_header_fsid(split),
1541 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1542 (unsigned long)btrfs_header_chunk_tree_uuid(split),
1545 mid = (c_nritems + 1) / 2;
1547 copy_extent_buffer(split, c,
1548 btrfs_node_key_ptr_offset(0),
1549 btrfs_node_key_ptr_offset(mid),
1550 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1551 btrfs_set_header_nritems(split, c_nritems - mid);
1552 btrfs_set_header_nritems(c, mid);
1555 btrfs_mark_buffer_dirty(c);
1556 btrfs_mark_buffer_dirty(split);
1558 btrfs_node_key(split, &disk_key, 0);
1559 wret = insert_ptr(trans, root, path, &disk_key, split->start,
1560 path->slots[level + 1] + 1,
1565 if (path->slots[level] >= mid) {
1566 path->slots[level] -= mid;
1567 free_extent_buffer(c);
1568 path->nodes[level] = split;
1569 path->slots[level + 1] += 1;
1571 free_extent_buffer(split);
1577 * how many bytes are required to store the items in a leaf. start
1578 * and nr indicate which items in the leaf to check. This totals up the
1579 * space used both by the item structs and the item data
1581 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
1584 int nritems = btrfs_header_nritems(l);
1585 int end = min(nritems, start + nr) - 1;
1589 data_len = btrfs_item_end_nr(l, start);
1590 data_len = data_len - btrfs_item_offset_nr(l, end);
1591 data_len += sizeof(struct btrfs_item) * nr;
1592 WARN_ON(data_len < 0);
1597 * The space between the end of the leaf items and
1598 * the start of the leaf data. IOW, how much room
1599 * the leaf has left for both items and data
1601 int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
1603 int nritems = btrfs_header_nritems(leaf);
1605 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1607 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
1608 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
1609 leaf_space_used(leaf, 0, nritems), nritems);
1615 * push some data in the path leaf to the right, trying to free up at
1616 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1618 * returns 1 if the push failed because the other node didn't have enough
1619 * room, 0 if everything worked out and < 0 if there were major errors.
1621 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1622 *root, struct btrfs_path *path, int data_size,
1625 struct extent_buffer *left = path->nodes[0];
1626 struct extent_buffer *right;
1627 struct extent_buffer *upper;
1628 struct btrfs_disk_key disk_key;
1634 struct btrfs_item *item;
1642 slot = path->slots[1];
1643 if (!path->nodes[1]) {
1646 upper = path->nodes[1];
1647 if (slot >= btrfs_header_nritems(upper) - 1)
1650 right = read_node_slot(root, upper, slot + 1);
1651 free_space = btrfs_leaf_free_space(root, right);
1652 if (free_space < data_size + sizeof(struct btrfs_item)) {
1653 free_extent_buffer(right);
1657 /* cow and double check */
1658 ret = btrfs_cow_block(trans, root, right, upper,
1661 free_extent_buffer(right);
1664 free_space = btrfs_leaf_free_space(root, right);
1665 if (free_space < data_size + sizeof(struct btrfs_item)) {
1666 free_extent_buffer(right);
1670 left_nritems = btrfs_header_nritems(left);
1671 if (left_nritems == 0) {
1672 free_extent_buffer(right);
1681 i = left_nritems - 1;
1683 item = btrfs_item_nr(left, i);
1685 if (path->slots[0] == i)
1686 push_space += data_size + sizeof(*item);
1688 if (!left->map_token) {
1689 map_extent_buffer(left, (unsigned long)item,
1690 sizeof(struct btrfs_item),
1691 &left->map_token, &left->kaddr,
1692 &left->map_start, &left->map_len,
1696 this_item_size = btrfs_item_size(left, item);
1697 if (this_item_size + sizeof(*item) + push_space > free_space)
1700 push_space += this_item_size + sizeof(*item);
1705 if (left->map_token) {
1706 unmap_extent_buffer(left, left->map_token, KM_USER1);
1707 left->map_token = NULL;
1710 if (push_items == 0) {
1711 free_extent_buffer(right);
1715 if (!empty && push_items == left_nritems)
1718 /* push left to right */
1719 right_nritems = btrfs_header_nritems(right);
1721 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
1722 push_space -= leaf_data_end(root, left);
1724 /* make room in the right data area */
1725 data_end = leaf_data_end(root, right);
1726 memmove_extent_buffer(right,
1727 btrfs_leaf_data(right) + data_end - push_space,
1728 btrfs_leaf_data(right) + data_end,
1729 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1731 /* copy from the left data area */
1732 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
1733 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1734 btrfs_leaf_data(left) + leaf_data_end(root, left),
1737 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1738 btrfs_item_nr_offset(0),
1739 right_nritems * sizeof(struct btrfs_item));
1741 /* copy the items from left to right */
1742 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1743 btrfs_item_nr_offset(left_nritems - push_items),
1744 push_items * sizeof(struct btrfs_item));
1746 /* update the item pointers */
1747 right_nritems += push_items;
1748 btrfs_set_header_nritems(right, right_nritems);
1749 push_space = BTRFS_LEAF_DATA_SIZE(root);
1750 for (i = 0; i < right_nritems; i++) {
1751 item = btrfs_item_nr(right, i);
1752 if (!right->map_token) {
1753 map_extent_buffer(right, (unsigned long)item,
1754 sizeof(struct btrfs_item),
1755 &right->map_token, &right->kaddr,
1756 &right->map_start, &right->map_len,
1759 push_space -= btrfs_item_size(right, item);
1760 btrfs_set_item_offset(right, item, push_space);
1763 if (right->map_token) {
1764 unmap_extent_buffer(right, right->map_token, KM_USER1);
1765 right->map_token = NULL;
1767 left_nritems -= push_items;
1768 btrfs_set_header_nritems(left, left_nritems);
1771 btrfs_mark_buffer_dirty(left);
1772 btrfs_mark_buffer_dirty(right);
1774 btrfs_item_key(right, &disk_key, 0);
1775 btrfs_set_node_key(upper, &disk_key, slot + 1);
1776 btrfs_mark_buffer_dirty(upper);
1778 /* then fixup the leaf pointer in the path */
1779 if (path->slots[0] >= left_nritems) {
1780 path->slots[0] -= left_nritems;
1781 free_extent_buffer(path->nodes[0]);
1782 path->nodes[0] = right;
1783 path->slots[1] += 1;
1785 free_extent_buffer(right);
1790 * push some data in the path leaf to the left, trying to free up at
1791 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1793 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1794 *root, struct btrfs_path *path, int data_size,
1797 struct btrfs_disk_key disk_key;
1798 struct extent_buffer *right = path->nodes[0];
1799 struct extent_buffer *left;
1805 struct btrfs_item *item;
1806 u32 old_left_nritems;
1812 u32 old_left_item_size;
1814 slot = path->slots[1];
1817 if (!path->nodes[1])
1820 right_nritems = btrfs_header_nritems(right);
1821 if (right_nritems == 0) {
1825 left = read_node_slot(root, path->nodes[1], slot - 1);
1826 free_space = btrfs_leaf_free_space(root, left);
1827 if (free_space < data_size + sizeof(struct btrfs_item)) {
1828 free_extent_buffer(left);
1832 /* cow and double check */
1833 ret = btrfs_cow_block(trans, root, left,
1834 path->nodes[1], slot - 1, &left);
1836 /* we hit -ENOSPC, but it isn't fatal here */
1837 free_extent_buffer(left);
1841 free_space = btrfs_leaf_free_space(root, left);
1842 if (free_space < data_size + sizeof(struct btrfs_item)) {
1843 free_extent_buffer(left);
1850 nr = right_nritems - 1;
1852 for (i = 0; i < nr; i++) {
1853 item = btrfs_item_nr(right, i);
1854 if (!right->map_token) {
1855 map_extent_buffer(right, (unsigned long)item,
1856 sizeof(struct btrfs_item),
1857 &right->map_token, &right->kaddr,
1858 &right->map_start, &right->map_len,
1862 if (path->slots[0] == i)
1863 push_space += data_size + sizeof(*item);
1865 this_item_size = btrfs_item_size(right, item);
1866 if (this_item_size + sizeof(*item) + push_space > free_space)
1870 push_space += this_item_size + sizeof(*item);
1873 if (right->map_token) {
1874 unmap_extent_buffer(right, right->map_token, KM_USER1);
1875 right->map_token = NULL;
1878 if (push_items == 0) {
1879 free_extent_buffer(left);
1882 if (!empty && push_items == btrfs_header_nritems(right))
1885 /* push data from right to left */
1886 copy_extent_buffer(left, right,
1887 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1888 btrfs_item_nr_offset(0),
1889 push_items * sizeof(struct btrfs_item));
1891 push_space = BTRFS_LEAF_DATA_SIZE(root) -
1892 btrfs_item_offset_nr(right, push_items -1);
1894 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
1895 leaf_data_end(root, left) - push_space,
1896 btrfs_leaf_data(right) +
1897 btrfs_item_offset_nr(right, push_items - 1),
1899 old_left_nritems = btrfs_header_nritems(left);
1900 BUG_ON(old_left_nritems < 0);
1902 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
1903 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
1906 item = btrfs_item_nr(left, i);
1907 if (!left->map_token) {
1908 map_extent_buffer(left, (unsigned long)item,
1909 sizeof(struct btrfs_item),
1910 &left->map_token, &left->kaddr,
1911 &left->map_start, &left->map_len,
1915 ioff = btrfs_item_offset(left, item);
1916 btrfs_set_item_offset(left, item,
1917 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
1919 btrfs_set_header_nritems(left, old_left_nritems + push_items);
1920 if (left->map_token) {
1921 unmap_extent_buffer(left, left->map_token, KM_USER1);
1922 left->map_token = NULL;
1925 /* fixup right node */
1926 if (push_items > right_nritems) {
1927 printk("push items %d nr %u\n", push_items, right_nritems);
1931 if (push_items < right_nritems) {
1932 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1933 leaf_data_end(root, right);
1934 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1935 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1936 btrfs_leaf_data(right) +
1937 leaf_data_end(root, right), push_space);
1939 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
1940 btrfs_item_nr_offset(push_items),
1941 (btrfs_header_nritems(right) - push_items) *
1942 sizeof(struct btrfs_item));
1944 right_nritems -= push_items;
1945 btrfs_set_header_nritems(right, right_nritems);
1946 push_space = BTRFS_LEAF_DATA_SIZE(root);
1947 for (i = 0; i < right_nritems; i++) {
1948 item = btrfs_item_nr(right, i);
1950 if (!right->map_token) {
1951 map_extent_buffer(right, (unsigned long)item,
1952 sizeof(struct btrfs_item),
1953 &right->map_token, &right->kaddr,
1954 &right->map_start, &right->map_len,
1958 push_space = push_space - btrfs_item_size(right, item);
1959 btrfs_set_item_offset(right, item, push_space);
1961 if (right->map_token) {
1962 unmap_extent_buffer(right, right->map_token, KM_USER1);
1963 right->map_token = NULL;
1966 btrfs_mark_buffer_dirty(left);
1968 btrfs_mark_buffer_dirty(right);
1970 btrfs_item_key(right, &disk_key, 0);
1971 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
1975 /* then fixup the leaf pointer in the path */
1976 if (path->slots[0] < push_items) {
1977 path->slots[0] += old_left_nritems;
1978 free_extent_buffer(path->nodes[0]);
1979 path->nodes[0] = left;
1980 path->slots[1] -= 1;
1982 free_extent_buffer(left);
1983 path->slots[0] -= push_items;
1985 BUG_ON(path->slots[0] < 0);
1990 * split the path's leaf in two, making sure there is at least data_size
1991 * available for the resulting leaf level of the path.
1993 * returns 0 if all went well and < 0 on failure.
1995 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
1996 *root, struct btrfs_key *ins_key,
1997 struct btrfs_path *path, int data_size, int extend)
2000 struct extent_buffer *l;
2004 struct extent_buffer *right;
2005 int space_needed = data_size + sizeof(struct btrfs_item);
2012 int num_doubles = 0;
2013 struct btrfs_disk_key disk_key;
2016 space_needed = data_size;
2019 root_gen = trans->transid;
2023 /* first try to make some room by pushing left and right */
2024 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
2025 wret = push_leaf_right(trans, root, path, data_size, 0);
2030 wret = push_leaf_left(trans, root, path, data_size, 0);
2036 /* did the pushes work? */
2037 if (btrfs_leaf_free_space(root, l) >= space_needed)
2041 if (!path->nodes[1]) {
2042 ret = insert_new_root(trans, root, path, 1);
2049 slot = path->slots[0];
2050 nritems = btrfs_header_nritems(l);
2051 mid = (nritems + 1)/ 2;
2053 btrfs_item_key(l, &disk_key, 0);
2055 right = __btrfs_alloc_free_block(trans, root, root->leafsize,
2056 root->root_key.objectid,
2057 root_gen, disk_key.objectid, 0,
2059 if (IS_ERR(right)) {
2061 return PTR_ERR(right);
2064 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2065 btrfs_set_header_bytenr(right, right->start);
2066 btrfs_set_header_generation(right, trans->transid);
2067 btrfs_set_header_owner(right, root->root_key.objectid);
2068 btrfs_set_header_level(right, 0);
2069 write_extent_buffer(right, root->fs_info->fsid,
2070 (unsigned long)btrfs_header_fsid(right),
2073 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2074 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2078 leaf_space_used(l, mid, nritems - mid) + space_needed >
2079 BTRFS_LEAF_DATA_SIZE(root)) {
2080 if (slot >= nritems) {
2081 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2082 btrfs_set_header_nritems(right, 0);
2083 wret = insert_ptr(trans, root, path,
2084 &disk_key, right->start,
2085 path->slots[1] + 1, 1);
2088 free_extent_buffer(path->nodes[0]);
2089 path->nodes[0] = right;
2091 path->slots[1] += 1;
2092 btrfs_mark_buffer_dirty(right);
2096 if (mid != nritems &&
2097 leaf_space_used(l, mid, nritems - mid) +
2098 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2103 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2104 BTRFS_LEAF_DATA_SIZE(root)) {
2105 if (!extend && slot == 0) {
2106 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2107 btrfs_set_header_nritems(right, 0);
2108 wret = insert_ptr(trans, root, path,
2114 free_extent_buffer(path->nodes[0]);
2115 path->nodes[0] = right;
2117 if (path->slots[1] == 0) {
2118 wret = fixup_low_keys(trans, root,
2119 path, &disk_key, 1);
2123 btrfs_mark_buffer_dirty(right);
2125 } else if (extend && slot == 0) {
2129 if (mid != nritems &&
2130 leaf_space_used(l, mid, nritems - mid) +
2131 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2137 nritems = nritems - mid;
2138 btrfs_set_header_nritems(right, nritems);
2139 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2141 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2142 btrfs_item_nr_offset(mid),
2143 nritems * sizeof(struct btrfs_item));
2145 copy_extent_buffer(right, l,
2146 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2147 data_copy_size, btrfs_leaf_data(l) +
2148 leaf_data_end(root, l), data_copy_size);
2150 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2151 btrfs_item_end_nr(l, mid);
2153 for (i = 0; i < nritems; i++) {
2154 struct btrfs_item *item = btrfs_item_nr(right, i);
2157 if (!right->map_token) {
2158 map_extent_buffer(right, (unsigned long)item,
2159 sizeof(struct btrfs_item),
2160 &right->map_token, &right->kaddr,
2161 &right->map_start, &right->map_len,
2165 ioff = btrfs_item_offset(right, item);
2166 btrfs_set_item_offset(right, item, ioff + rt_data_off);
2169 if (right->map_token) {
2170 unmap_extent_buffer(right, right->map_token, KM_USER1);
2171 right->map_token = NULL;
2174 btrfs_set_header_nritems(l, mid);
2176 btrfs_item_key(right, &disk_key, 0);
2177 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2178 path->slots[1] + 1, 1);
2182 btrfs_mark_buffer_dirty(right);
2183 btrfs_mark_buffer_dirty(l);
2184 BUG_ON(path->slots[0] != slot);
2187 free_extent_buffer(path->nodes[0]);
2188 path->nodes[0] = right;
2189 path->slots[0] -= mid;
2190 path->slots[1] += 1;
2192 free_extent_buffer(right);
2194 BUG_ON(path->slots[0] < 0);
2197 BUG_ON(num_doubles != 0);
2204 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2205 struct btrfs_root *root,
2206 struct btrfs_path *path,
2207 u32 new_size, int from_end)
2212 struct extent_buffer *leaf;
2213 struct btrfs_item *item;
2215 unsigned int data_end;
2216 unsigned int old_data_start;
2217 unsigned int old_size;
2218 unsigned int size_diff;
2221 slot_orig = path->slots[0];
2222 leaf = path->nodes[0];
2223 slot = path->slots[0];
2225 old_size = btrfs_item_size_nr(leaf, slot);
2226 if (old_size == new_size)
2229 nritems = btrfs_header_nritems(leaf);
2230 data_end = leaf_data_end(root, leaf);
2232 old_data_start = btrfs_item_offset_nr(leaf, slot);
2234 size_diff = old_size - new_size;
2237 BUG_ON(slot >= nritems);
2240 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2242 /* first correct the data pointers */
2243 for (i = slot; i < nritems; i++) {
2245 item = btrfs_item_nr(leaf, i);
2247 if (!leaf->map_token) {
2248 map_extent_buffer(leaf, (unsigned long)item,
2249 sizeof(struct btrfs_item),
2250 &leaf->map_token, &leaf->kaddr,
2251 &leaf->map_start, &leaf->map_len,
2255 ioff = btrfs_item_offset(leaf, item);
2256 btrfs_set_item_offset(leaf, item, ioff + size_diff);
2259 if (leaf->map_token) {
2260 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2261 leaf->map_token = NULL;
2264 /* shift the data */
2266 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2267 data_end + size_diff, btrfs_leaf_data(leaf) +
2268 data_end, old_data_start + new_size - data_end);
2270 struct btrfs_disk_key disk_key;
2273 btrfs_item_key(leaf, &disk_key, slot);
2275 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2277 struct btrfs_file_extent_item *fi;
2279 fi = btrfs_item_ptr(leaf, slot,
2280 struct btrfs_file_extent_item);
2281 fi = (struct btrfs_file_extent_item *)(
2282 (unsigned long)fi - size_diff);
2284 if (btrfs_file_extent_type(leaf, fi) ==
2285 BTRFS_FILE_EXTENT_INLINE) {
2286 ptr = btrfs_item_ptr_offset(leaf, slot);
2287 memmove_extent_buffer(leaf, ptr,
2289 offsetof(struct btrfs_file_extent_item,
2294 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2295 data_end + size_diff, btrfs_leaf_data(leaf) +
2296 data_end, old_data_start - data_end);
2298 offset = btrfs_disk_key_offset(&disk_key);
2299 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2300 btrfs_set_item_key(leaf, &disk_key, slot);
2302 fixup_low_keys(trans, root, path, &disk_key, 1);
2305 item = btrfs_item_nr(leaf, slot);
2306 btrfs_set_item_size(leaf, item, new_size);
2307 btrfs_mark_buffer_dirty(leaf);
2310 if (btrfs_leaf_free_space(root, leaf) < 0) {
2311 btrfs_print_leaf(root, leaf);
2317 int btrfs_extend_item(struct btrfs_trans_handle *trans,
2318 struct btrfs_root *root, struct btrfs_path *path,
2324 struct extent_buffer *leaf;
2325 struct btrfs_item *item;
2327 unsigned int data_end;
2328 unsigned int old_data;
2329 unsigned int old_size;
2332 slot_orig = path->slots[0];
2333 leaf = path->nodes[0];
2335 nritems = btrfs_header_nritems(leaf);
2336 data_end = leaf_data_end(root, leaf);
2338 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2339 btrfs_print_leaf(root, leaf);
2342 slot = path->slots[0];
2343 old_data = btrfs_item_end_nr(leaf, slot);
2346 if (slot >= nritems) {
2347 btrfs_print_leaf(root, leaf);
2348 printk("slot %d too large, nritems %d\n", slot, nritems);
2353 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2355 /* first correct the data pointers */
2356 for (i = slot; i < nritems; i++) {
2358 item = btrfs_item_nr(leaf, i);
2360 if (!leaf->map_token) {
2361 map_extent_buffer(leaf, (unsigned long)item,
2362 sizeof(struct btrfs_item),
2363 &leaf->map_token, &leaf->kaddr,
2364 &leaf->map_start, &leaf->map_len,
2367 ioff = btrfs_item_offset(leaf, item);
2368 btrfs_set_item_offset(leaf, item, ioff - data_size);
2371 if (leaf->map_token) {
2372 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2373 leaf->map_token = NULL;
2376 /* shift the data */
2377 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2378 data_end - data_size, btrfs_leaf_data(leaf) +
2379 data_end, old_data - data_end);
2381 data_end = old_data;
2382 old_size = btrfs_item_size_nr(leaf, slot);
2383 item = btrfs_item_nr(leaf, slot);
2384 btrfs_set_item_size(leaf, item, old_size + data_size);
2385 btrfs_mark_buffer_dirty(leaf);
2388 if (btrfs_leaf_free_space(root, leaf) < 0) {
2389 btrfs_print_leaf(root, leaf);
2396 * Given a key and some data, insert an item into the tree.
2397 * This does all the path init required, making room in the tree if needed.
2399 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
2400 struct btrfs_root *root,
2401 struct btrfs_path *path,
2402 struct btrfs_key *cpu_key, u32 *data_size,
2405 struct extent_buffer *leaf;
2406 struct btrfs_item *item;
2414 unsigned int data_end;
2415 struct btrfs_disk_key disk_key;
2417 for (i = 0; i < nr; i++) {
2418 total_data += data_size[i];
2421 /* create a root if there isn't one */
2425 total_size = total_data + (nr - 1) * sizeof(struct btrfs_item);
2426 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
2433 slot_orig = path->slots[0];
2434 leaf = path->nodes[0];
2436 nritems = btrfs_header_nritems(leaf);
2437 data_end = leaf_data_end(root, leaf);
2439 if (btrfs_leaf_free_space(root, leaf) <
2440 sizeof(struct btrfs_item) + total_size) {
2441 btrfs_print_leaf(root, leaf);
2442 printk("not enough freespace need %u have %d\n",
2443 total_size, btrfs_leaf_free_space(root, leaf));
2447 slot = path->slots[0];
2450 if (slot != nritems) {
2452 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
2454 if (old_data < data_end) {
2455 btrfs_print_leaf(root, leaf);
2456 printk("slot %d old_data %d data_end %d\n",
2457 slot, old_data, data_end);
2461 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2463 /* first correct the data pointers */
2464 WARN_ON(leaf->map_token);
2465 for (i = slot; i < nritems; i++) {
2468 item = btrfs_item_nr(leaf, i);
2469 if (!leaf->map_token) {
2470 map_extent_buffer(leaf, (unsigned long)item,
2471 sizeof(struct btrfs_item),
2472 &leaf->map_token, &leaf->kaddr,
2473 &leaf->map_start, &leaf->map_len,
2477 ioff = btrfs_item_offset(leaf, item);
2478 btrfs_set_item_offset(leaf, item, ioff - total_data);
2480 if (leaf->map_token) {
2481 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2482 leaf->map_token = NULL;
2485 /* shift the items */
2486 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
2487 btrfs_item_nr_offset(slot),
2488 (nritems - slot) * sizeof(struct btrfs_item));
2490 /* shift the data */
2491 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2492 data_end - total_data, btrfs_leaf_data(leaf) +
2493 data_end, old_data - data_end);
2494 data_end = old_data;
2497 /* setup the item for the new data */
2498 for (i = 0; i < nr; i++) {
2499 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2500 btrfs_set_item_key(leaf, &disk_key, slot + i);
2501 item = btrfs_item_nr(leaf, slot + i);
2502 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2503 data_end -= data_size[i];
2504 btrfs_set_item_size(leaf, item, data_size[i]);
2506 btrfs_set_header_nritems(leaf, nritems + nr);
2507 btrfs_mark_buffer_dirty(leaf);
2511 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
2512 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
2515 if (btrfs_leaf_free_space(root, leaf) < 0) {
2516 btrfs_print_leaf(root, leaf);
2525 * Given a key and some data, insert an item into the tree.
2526 * This does all the path init required, making room in the tree if needed.
2528 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2529 *root, struct btrfs_key *cpu_key, void *data, u32
2533 struct btrfs_path *path;
2534 struct extent_buffer *leaf;
2537 path = btrfs_alloc_path();
2539 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
2541 leaf = path->nodes[0];
2542 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2543 write_extent_buffer(leaf, data, ptr, data_size);
2544 btrfs_mark_buffer_dirty(leaf);
2546 btrfs_free_path(path);
2551 * delete the pointer from a given node.
2553 * If the delete empties a node, the node is removed from the tree,
2554 * continuing all the way the root if required. The root is converted into
2555 * a leaf if all the nodes are emptied.
2557 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2558 struct btrfs_path *path, int level, int slot)
2560 struct extent_buffer *parent = path->nodes[level];
2565 nritems = btrfs_header_nritems(parent);
2566 if (slot != nritems -1) {
2567 memmove_extent_buffer(parent,
2568 btrfs_node_key_ptr_offset(slot),
2569 btrfs_node_key_ptr_offset(slot + 1),
2570 sizeof(struct btrfs_key_ptr) *
2571 (nritems - slot - 1));
2574 btrfs_set_header_nritems(parent, nritems);
2575 if (nritems == 0 && parent == root->node) {
2576 BUG_ON(btrfs_header_level(root->node) != 1);
2577 /* just turn the root into a leaf and break */
2578 btrfs_set_header_level(root->node, 0);
2579 } else if (slot == 0) {
2580 struct btrfs_disk_key disk_key;
2582 btrfs_node_key(parent, &disk_key, 0);
2583 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
2587 btrfs_mark_buffer_dirty(parent);
2592 * delete the item at the leaf level in path. If that empties
2593 * the leaf, remove it from the tree
2595 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2596 struct btrfs_path *path, int slot, int nr)
2598 struct extent_buffer *leaf;
2599 struct btrfs_item *item;
2607 leaf = path->nodes[0];
2608 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2610 for (i = 0; i < nr; i++)
2611 dsize += btrfs_item_size_nr(leaf, slot + i);
2613 nritems = btrfs_header_nritems(leaf);
2615 if (slot + nr != nritems) {
2617 int data_end = leaf_data_end(root, leaf);
2619 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2621 btrfs_leaf_data(leaf) + data_end,
2622 last_off - data_end);
2624 for (i = slot + nr; i < nritems; i++) {
2627 item = btrfs_item_nr(leaf, i);
2628 if (!leaf->map_token) {
2629 map_extent_buffer(leaf, (unsigned long)item,
2630 sizeof(struct btrfs_item),
2631 &leaf->map_token, &leaf->kaddr,
2632 &leaf->map_start, &leaf->map_len,
2635 ioff = btrfs_item_offset(leaf, item);
2636 btrfs_set_item_offset(leaf, item, ioff + dsize);
2639 if (leaf->map_token) {
2640 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2641 leaf->map_token = NULL;
2644 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2645 btrfs_item_nr_offset(slot + nr),
2646 sizeof(struct btrfs_item) *
2647 (nritems - slot - nr));
2649 btrfs_set_header_nritems(leaf, nritems - nr);
2652 /* delete the leaf if we've emptied it */
2654 if (leaf == root->node) {
2655 btrfs_set_header_level(leaf, 0);
2657 u64 root_gen = btrfs_header_generation(path->nodes[1]);
2658 clean_tree_block(trans, root, leaf);
2659 wret = del_ptr(trans, root, path, 1, path->slots[1]);
2662 wret = btrfs_free_extent(trans, root,
2663 leaf->start, leaf->len,
2664 btrfs_header_owner(path->nodes[1]),
2670 int used = leaf_space_used(leaf, 0, nritems);
2672 struct btrfs_disk_key disk_key;
2674 btrfs_item_key(leaf, &disk_key, 0);
2675 wret = fixup_low_keys(trans, root, path,
2681 /* delete the leaf if it is mostly empty */
2682 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
2683 /* push_leaf_left fixes the path.
2684 * make sure the path still points to our leaf
2685 * for possible call to del_ptr below
2687 slot = path->slots[1];
2688 extent_buffer_get(leaf);
2690 wret = push_leaf_left(trans, root, path, 1, 1);
2691 if (wret < 0 && wret != -ENOSPC)
2694 if (path->nodes[0] == leaf &&
2695 btrfs_header_nritems(leaf)) {
2696 wret = push_leaf_right(trans, root, path, 1, 1);
2697 if (wret < 0 && wret != -ENOSPC)
2701 if (btrfs_header_nritems(leaf) == 0) {
2703 u64 bytenr = leaf->start;
2704 u32 blocksize = leaf->len;
2706 root_gen = btrfs_header_generation(
2709 clean_tree_block(trans, root, leaf);
2711 wret = del_ptr(trans, root, path, 1, slot);
2715 free_extent_buffer(leaf);
2716 wret = btrfs_free_extent(trans, root, bytenr,
2718 btrfs_header_owner(path->nodes[1]),
2723 btrfs_mark_buffer_dirty(leaf);
2724 free_extent_buffer(leaf);
2727 btrfs_mark_buffer_dirty(leaf);
2734 * walk up the tree as far as required to find the previous leaf.
2735 * returns 0 if it found something or 1 if there are no lesser leaves.
2736 * returns < 0 on io errors.
2738 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2742 struct extent_buffer *c;
2743 struct extent_buffer *next = NULL;
2745 while(level < BTRFS_MAX_LEVEL) {
2746 if (!path->nodes[level])
2749 slot = path->slots[level];
2750 c = path->nodes[level];
2753 if (level == BTRFS_MAX_LEVEL)
2760 free_extent_buffer(next);
2762 next = read_node_slot(root, c, slot);
2765 path->slots[level] = slot;
2768 c = path->nodes[level];
2769 free_extent_buffer(c);
2770 slot = btrfs_header_nritems(next);
2773 path->nodes[level] = next;
2774 path->slots[level] = slot;
2777 next = read_node_slot(root, next, slot);
2783 * walk up the tree as far as required to find the next leaf.
2784 * returns 0 if it found something or 1 if there are no greater leaves.
2785 * returns < 0 on io errors.
2787 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
2791 struct extent_buffer *c;
2792 struct extent_buffer *next = NULL;
2794 while(level < BTRFS_MAX_LEVEL) {
2795 if (!path->nodes[level])
2798 slot = path->slots[level] + 1;
2799 c = path->nodes[level];
2800 if (slot >= btrfs_header_nritems(c)) {
2802 if (level == BTRFS_MAX_LEVEL)
2808 free_extent_buffer(next);
2811 reada_for_search(root, path, level, slot, 0);
2813 next = read_node_slot(root, c, slot);
2816 path->slots[level] = slot;
2819 c = path->nodes[level];
2820 free_extent_buffer(c);
2821 path->nodes[level] = next;
2822 path->slots[level] = 0;
2826 reada_for_search(root, path, level, 0, 0);
2827 next = read_node_slot(root, next, 0);
2832 int btrfs_previous_item(struct btrfs_root *root,
2833 struct btrfs_path *path, u64 min_objectid,
2836 struct btrfs_key found_key;
2837 struct extent_buffer *leaf;
2841 if (path->slots[0] == 0) {
2842 ret = btrfs_prev_leaf(root, path);
2848 leaf = path->nodes[0];
2849 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2850 if (found_key.type == type)