2 * Copyright (C) 2007,2008 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"
26 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
27 *root, struct btrfs_path *path, int level);
28 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
29 *root, struct btrfs_key *ins_key,
30 struct btrfs_path *path, int data_size, int extend);
31 static int push_node_left(struct btrfs_trans_handle *trans,
32 struct btrfs_root *root, struct extent_buffer *dst,
33 struct extent_buffer *src, int empty);
34 static int balance_node_right(struct btrfs_trans_handle *trans,
35 struct btrfs_root *root,
36 struct extent_buffer *dst_buf,
37 struct extent_buffer *src_buf);
38 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
39 struct btrfs_path *path, int level, int slot);
41 inline void btrfs_init_path(struct btrfs_path *p)
43 memset(p, 0, sizeof(*p));
46 struct btrfs_path *btrfs_alloc_path(void)
48 struct btrfs_path *path;
49 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
51 btrfs_init_path(path);
57 /* this also releases the path */
58 void btrfs_free_path(struct btrfs_path *p)
60 btrfs_release_path(NULL, p);
61 kmem_cache_free(btrfs_path_cachep, p);
65 * path release drops references on the extent buffers in the path
66 * and it drops any locks held by this path
68 * It is safe to call this on paths that no locks or extent buffers held.
70 noinline void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
74 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
79 btrfs_tree_unlock(p->nodes[i]);
82 free_extent_buffer(p->nodes[i]);
88 * safely gets a reference on the root node of a tree. A lock
89 * is not taken, so a concurrent writer may put a different node
90 * at the root of the tree. See btrfs_lock_root_node for the
93 * The extent buffer returned by this has a reference taken, so
94 * it won't disappear. It may stop being the root of the tree
95 * at any time because there are no locks held.
97 struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
99 struct extent_buffer *eb;
100 spin_lock(&root->node_lock);
102 extent_buffer_get(eb);
103 spin_unlock(&root->node_lock);
107 /* loop around taking references on and locking the root node of the
108 * tree until you end up with a lock on the root. A locked buffer
109 * is returned, with a reference held.
111 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
113 struct extent_buffer *eb;
116 eb = btrfs_root_node(root);
119 spin_lock(&root->node_lock);
120 if (eb == root->node) {
121 spin_unlock(&root->node_lock);
124 spin_unlock(&root->node_lock);
126 btrfs_tree_unlock(eb);
127 free_extent_buffer(eb);
132 /* cowonly root (everything not a reference counted cow subvolume), just get
133 * put onto a simple dirty list. transaction.c walks this to make sure they
134 * get properly updated on disk.
136 static void add_root_to_dirty_list(struct btrfs_root *root)
138 if (root->track_dirty && list_empty(&root->dirty_list)) {
139 list_add(&root->dirty_list,
140 &root->fs_info->dirty_cowonly_roots);
145 * used by snapshot creation to make a copy of a root for a tree with
146 * a given objectid. The buffer with the new root node is returned in
147 * cow_ret, and this func returns zero on success or a negative error code.
149 int btrfs_copy_root(struct btrfs_trans_handle *trans,
150 struct btrfs_root *root,
151 struct extent_buffer *buf,
152 struct extent_buffer **cow_ret, u64 new_root_objectid)
154 struct extent_buffer *cow;
158 struct btrfs_root *new_root;
160 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
164 memcpy(new_root, root, sizeof(*new_root));
165 new_root->root_key.objectid = new_root_objectid;
167 WARN_ON(root->ref_cows && trans->transid !=
168 root->fs_info->running_transaction->transid);
169 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
171 level = btrfs_header_level(buf);
172 nritems = btrfs_header_nritems(buf);
174 cow = btrfs_alloc_free_block(trans, new_root, buf->len, 0,
175 new_root_objectid, trans->transid,
176 level, buf->start, 0);
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, new_root_objectid);
186 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
188 write_extent_buffer(cow, root->fs_info->fsid,
189 (unsigned long)btrfs_header_fsid(cow),
192 WARN_ON(btrfs_header_generation(buf) > trans->transid);
193 ret = btrfs_inc_ref(trans, new_root, buf, cow, NULL);
199 btrfs_mark_buffer_dirty(cow);
205 * does the dirty work in cow of a single block. The parent block (if
206 * supplied) is updated to point to the new cow copy. The new buffer is marked
207 * dirty and returned locked. If you modify the block it needs to be marked
210 * search_start -- an allocation hint for the new block
212 * empty_size -- a hint that you plan on doing more cow. This is the size in
213 * bytes the allocator should try to find free next to the block it returns.
214 * This is just a hint and may be ignored by the allocator.
216 * prealloc_dest -- if you have already reserved a destination for the cow,
217 * this uses that block instead of allocating a new one.
218 * btrfs_alloc_reserved_extent is used to finish the allocation.
220 static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
221 struct btrfs_root *root,
222 struct extent_buffer *buf,
223 struct extent_buffer *parent, int parent_slot,
224 struct extent_buffer **cow_ret,
225 u64 search_start, u64 empty_size,
229 struct extent_buffer *cow;
238 WARN_ON(!btrfs_tree_locked(buf));
241 parent_start = parent->start;
245 WARN_ON(root->ref_cows && trans->transid !=
246 root->fs_info->running_transaction->transid);
247 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
249 level = btrfs_header_level(buf);
250 nritems = btrfs_header_nritems(buf);
253 struct btrfs_key ins;
255 ins.objectid = prealloc_dest;
256 ins.offset = buf->len;
257 ins.type = BTRFS_EXTENT_ITEM_KEY;
259 ret = btrfs_alloc_reserved_extent(trans, root, parent_start,
260 root->root_key.objectid,
261 trans->transid, level, &ins);
263 cow = btrfs_init_new_buffer(trans, root, prealloc_dest,
266 cow = btrfs_alloc_free_block(trans, root, buf->len,
268 root->root_key.objectid,
269 trans->transid, level,
270 search_start, empty_size);
275 copy_extent_buffer(cow, buf, 0, 0, cow->len);
276 btrfs_set_header_bytenr(cow, cow->start);
277 btrfs_set_header_generation(cow, trans->transid);
278 btrfs_set_header_owner(cow, root->root_key.objectid);
279 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
281 write_extent_buffer(cow, root->fs_info->fsid,
282 (unsigned long)btrfs_header_fsid(cow),
285 WARN_ON(btrfs_header_generation(buf) > trans->transid);
286 if (btrfs_header_generation(buf) != trans->transid) {
288 ret = btrfs_inc_ref(trans, root, buf, cow, &nr_extents);
292 ret = btrfs_cache_ref(trans, root, buf, nr_extents);
294 } else if (btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID) {
296 * There are only two places that can drop reference to
297 * tree blocks owned by living reloc trees, one is here,
298 * the other place is btrfs_drop_subtree. In both places,
299 * we check reference count while tree block is locked.
300 * Furthermore, if reference count is one, it won't get
301 * increased by someone else.
304 ret = btrfs_lookup_extent_ref(trans, root, buf->start,
308 ret = btrfs_update_ref(trans, root, buf, cow,
310 clean_tree_block(trans, root, buf);
312 ret = btrfs_inc_ref(trans, root, buf, cow, NULL);
316 ret = btrfs_update_ref(trans, root, buf, cow, 0, nritems);
319 clean_tree_block(trans, root, buf);
322 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
323 ret = btrfs_reloc_tree_cache_ref(trans, root, cow, buf->start);
327 if (buf == root->node) {
328 WARN_ON(parent && parent != buf);
330 spin_lock(&root->node_lock);
332 extent_buffer_get(cow);
333 spin_unlock(&root->node_lock);
335 if (buf != root->commit_root) {
336 btrfs_free_extent(trans, root, buf->start,
337 buf->len, buf->start,
338 root->root_key.objectid,
339 btrfs_header_generation(buf),
342 free_extent_buffer(buf);
343 add_root_to_dirty_list(root);
345 btrfs_set_node_blockptr(parent, parent_slot,
347 WARN_ON(trans->transid == 0);
348 btrfs_set_node_ptr_generation(parent, parent_slot,
350 btrfs_mark_buffer_dirty(parent);
351 WARN_ON(btrfs_header_generation(parent) != trans->transid);
352 btrfs_free_extent(trans, root, buf->start, buf->len,
353 parent_start, btrfs_header_owner(parent),
354 btrfs_header_generation(parent), level, 1);
357 btrfs_tree_unlock(buf);
358 free_extent_buffer(buf);
359 btrfs_mark_buffer_dirty(cow);
365 * cows a single block, see __btrfs_cow_block for the real work.
366 * This version of it has extra checks so that a block isn't cow'd more than
367 * once per transaction, as long as it hasn't been written yet
369 noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
370 struct btrfs_root *root, struct extent_buffer *buf,
371 struct extent_buffer *parent, int parent_slot,
372 struct extent_buffer **cow_ret, u64 prealloc_dest)
377 if (trans->transaction != root->fs_info->running_transaction) {
378 printk(KERN_CRIT "trans %llu running %llu\n",
379 (unsigned long long)trans->transid,
381 root->fs_info->running_transaction->transid);
384 if (trans->transid != root->fs_info->generation) {
385 printk(KERN_CRIT "trans %llu running %llu\n",
386 (unsigned long long)trans->transid,
387 (unsigned long long)root->fs_info->generation);
391 spin_lock(&root->fs_info->hash_lock);
392 if (btrfs_header_generation(buf) == trans->transid &&
393 btrfs_header_owner(buf) == root->root_key.objectid &&
394 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
396 spin_unlock(&root->fs_info->hash_lock);
397 WARN_ON(prealloc_dest);
400 spin_unlock(&root->fs_info->hash_lock);
401 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
402 ret = __btrfs_cow_block(trans, root, buf, parent,
403 parent_slot, cow_ret, search_start, 0,
409 * helper function for defrag to decide if two blocks pointed to by a
410 * node are actually close by
412 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
414 if (blocknr < other && other - (blocknr + blocksize) < 32768)
416 if (blocknr > other && blocknr - (other + blocksize) < 32768)
422 * compare two keys in a memcmp fashion
424 static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
428 btrfs_disk_key_to_cpu(&k1, disk);
430 if (k1.objectid > k2->objectid)
432 if (k1.objectid < k2->objectid)
434 if (k1.type > k2->type)
436 if (k1.type < k2->type)
438 if (k1.offset > k2->offset)
440 if (k1.offset < k2->offset)
446 * same as comp_keys only with two btrfs_key's
448 static int comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
450 if (k1->objectid > k2->objectid)
452 if (k1->objectid < k2->objectid)
454 if (k1->type > k2->type)
456 if (k1->type < k2->type)
458 if (k1->offset > k2->offset)
460 if (k1->offset < k2->offset)
466 * this is used by the defrag code to go through all the
467 * leaves pointed to by a node and reallocate them so that
468 * disk order is close to key order
470 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
471 struct btrfs_root *root, struct extent_buffer *parent,
472 int start_slot, int cache_only, u64 *last_ret,
473 struct btrfs_key *progress)
475 struct extent_buffer *cur;
478 u64 search_start = *last_ret;
488 int progress_passed = 0;
489 struct btrfs_disk_key disk_key;
491 parent_level = btrfs_header_level(parent);
492 if (cache_only && parent_level != 1)
495 if (trans->transaction != root->fs_info->running_transaction)
497 if (trans->transid != root->fs_info->generation)
500 parent_nritems = btrfs_header_nritems(parent);
501 blocksize = btrfs_level_size(root, parent_level - 1);
502 end_slot = parent_nritems;
504 if (parent_nritems == 1)
507 for (i = start_slot; i < end_slot; i++) {
510 if (!parent->map_token) {
511 map_extent_buffer(parent,
512 btrfs_node_key_ptr_offset(i),
513 sizeof(struct btrfs_key_ptr),
514 &parent->map_token, &parent->kaddr,
515 &parent->map_start, &parent->map_len,
518 btrfs_node_key(parent, &disk_key, i);
519 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
523 blocknr = btrfs_node_blockptr(parent, i);
524 gen = btrfs_node_ptr_generation(parent, i);
526 last_block = blocknr;
529 other = btrfs_node_blockptr(parent, i - 1);
530 close = close_blocks(blocknr, other, blocksize);
532 if (!close && i < end_slot - 2) {
533 other = btrfs_node_blockptr(parent, i + 1);
534 close = close_blocks(blocknr, other, blocksize);
537 last_block = blocknr;
540 if (parent->map_token) {
541 unmap_extent_buffer(parent, parent->map_token,
543 parent->map_token = NULL;
546 cur = btrfs_find_tree_block(root, blocknr, blocksize);
548 uptodate = btrfs_buffer_uptodate(cur, gen);
551 if (!cur || !uptodate) {
553 free_extent_buffer(cur);
557 cur = read_tree_block(root, blocknr,
559 } else if (!uptodate) {
560 btrfs_read_buffer(cur, gen);
563 if (search_start == 0)
564 search_start = last_block;
566 btrfs_tree_lock(cur);
567 err = __btrfs_cow_block(trans, root, cur, parent, i,
570 (end_slot - i) * blocksize), 0);
572 btrfs_tree_unlock(cur);
573 free_extent_buffer(cur);
576 search_start = cur->start;
577 last_block = cur->start;
578 *last_ret = search_start;
579 btrfs_tree_unlock(cur);
580 free_extent_buffer(cur);
582 if (parent->map_token) {
583 unmap_extent_buffer(parent, parent->map_token,
585 parent->map_token = NULL;
591 * The leaf data grows from end-to-front in the node.
592 * this returns the address of the start of the last item,
593 * which is the stop of the leaf data stack
595 static inline unsigned int leaf_data_end(struct btrfs_root *root,
596 struct extent_buffer *leaf)
598 u32 nr = btrfs_header_nritems(leaf);
600 return BTRFS_LEAF_DATA_SIZE(root);
601 return btrfs_item_offset_nr(leaf, nr - 1);
605 * extra debugging checks to make sure all the items in a key are
606 * well formed and in the proper order
608 static int check_node(struct btrfs_root *root, struct btrfs_path *path,
611 struct extent_buffer *parent = NULL;
612 struct extent_buffer *node = path->nodes[level];
613 struct btrfs_disk_key parent_key;
614 struct btrfs_disk_key node_key;
617 struct btrfs_key cpukey;
618 u32 nritems = btrfs_header_nritems(node);
620 if (path->nodes[level + 1])
621 parent = path->nodes[level + 1];
623 slot = path->slots[level];
624 BUG_ON(nritems == 0);
626 parent_slot = path->slots[level + 1];
627 btrfs_node_key(parent, &parent_key, parent_slot);
628 btrfs_node_key(node, &node_key, 0);
629 BUG_ON(memcmp(&parent_key, &node_key,
630 sizeof(struct btrfs_disk_key)));
631 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
632 btrfs_header_bytenr(node));
634 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
636 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
637 btrfs_node_key(node, &node_key, slot);
638 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
640 if (slot < nritems - 1) {
641 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
642 btrfs_node_key(node, &node_key, slot);
643 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
649 * extra checking to make sure all the items in a leaf are
650 * well formed and in the proper order
652 static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
655 struct extent_buffer *leaf = path->nodes[level];
656 struct extent_buffer *parent = NULL;
658 struct btrfs_key cpukey;
659 struct btrfs_disk_key parent_key;
660 struct btrfs_disk_key leaf_key;
661 int slot = path->slots[0];
663 u32 nritems = btrfs_header_nritems(leaf);
665 if (path->nodes[level + 1])
666 parent = path->nodes[level + 1];
672 parent_slot = path->slots[level + 1];
673 btrfs_node_key(parent, &parent_key, parent_slot);
674 btrfs_item_key(leaf, &leaf_key, 0);
676 BUG_ON(memcmp(&parent_key, &leaf_key,
677 sizeof(struct btrfs_disk_key)));
678 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
679 btrfs_header_bytenr(leaf));
681 if (slot != 0 && slot < nritems - 1) {
682 btrfs_item_key(leaf, &leaf_key, slot);
683 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
684 if (comp_keys(&leaf_key, &cpukey) <= 0) {
685 btrfs_print_leaf(root, leaf);
686 printk(KERN_CRIT "slot %d offset bad key\n", slot);
689 if (btrfs_item_offset_nr(leaf, slot - 1) !=
690 btrfs_item_end_nr(leaf, slot)) {
691 btrfs_print_leaf(root, leaf);
692 printk(KERN_CRIT "slot %d offset bad\n", slot);
696 if (slot < nritems - 1) {
697 btrfs_item_key(leaf, &leaf_key, slot);
698 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
699 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
700 if (btrfs_item_offset_nr(leaf, slot) !=
701 btrfs_item_end_nr(leaf, slot + 1)) {
702 btrfs_print_leaf(root, leaf);
703 printk(KERN_CRIT "slot %d offset bad\n", slot);
707 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
708 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
712 static noinline int check_block(struct btrfs_root *root,
713 struct btrfs_path *path, int level)
717 return check_leaf(root, path, level);
718 return check_node(root, path, level);
722 * search for key in the extent_buffer. The items start at offset p,
723 * and they are item_size apart. There are 'max' items in p.
725 * the slot in the array is returned via slot, and it points to
726 * the place where you would insert key if it is not found in
729 * slot may point to max if the key is bigger than all of the keys
731 static noinline int generic_bin_search(struct extent_buffer *eb,
733 int item_size, struct btrfs_key *key,
740 struct btrfs_disk_key *tmp = NULL;
741 struct btrfs_disk_key unaligned;
742 unsigned long offset;
743 char *map_token = NULL;
745 unsigned long map_start = 0;
746 unsigned long map_len = 0;
750 mid = (low + high) / 2;
751 offset = p + mid * item_size;
753 if (!map_token || offset < map_start ||
754 (offset + sizeof(struct btrfs_disk_key)) >
755 map_start + map_len) {
757 unmap_extent_buffer(eb, map_token, KM_USER0);
761 err = map_private_extent_buffer(eb, offset,
762 sizeof(struct btrfs_disk_key),
764 &map_start, &map_len, KM_USER0);
767 tmp = (struct btrfs_disk_key *)(kaddr + offset -
770 read_extent_buffer(eb, &unaligned,
771 offset, sizeof(unaligned));
776 tmp = (struct btrfs_disk_key *)(kaddr + offset -
779 ret = comp_keys(tmp, key);
788 unmap_extent_buffer(eb, map_token, KM_USER0);
794 unmap_extent_buffer(eb, map_token, KM_USER0);
799 * simple bin_search frontend that does the right thing for
802 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
803 int level, int *slot)
806 return generic_bin_search(eb,
807 offsetof(struct btrfs_leaf, items),
808 sizeof(struct btrfs_item),
809 key, btrfs_header_nritems(eb),
812 return generic_bin_search(eb,
813 offsetof(struct btrfs_node, ptrs),
814 sizeof(struct btrfs_key_ptr),
815 key, btrfs_header_nritems(eb),
821 /* given a node and slot number, this reads the blocks it points to. The
822 * extent buffer is returned with a reference taken (but unlocked).
823 * NULL is returned on error.
825 static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
826 struct extent_buffer *parent, int slot)
828 int level = btrfs_header_level(parent);
831 if (slot >= btrfs_header_nritems(parent))
836 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
837 btrfs_level_size(root, level - 1),
838 btrfs_node_ptr_generation(parent, slot));
842 * node level balancing, used to make sure nodes are in proper order for
843 * item deletion. We balance from the top down, so we have to make sure
844 * that a deletion won't leave an node completely empty later on.
846 static noinline int balance_level(struct btrfs_trans_handle *trans,
847 struct btrfs_root *root,
848 struct btrfs_path *path, int level)
850 struct extent_buffer *right = NULL;
851 struct extent_buffer *mid;
852 struct extent_buffer *left = NULL;
853 struct extent_buffer *parent = NULL;
857 int orig_slot = path->slots[level];
858 int err_on_enospc = 0;
864 mid = path->nodes[level];
865 WARN_ON(!path->locks[level]);
866 WARN_ON(btrfs_header_generation(mid) != trans->transid);
868 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
870 if (level < BTRFS_MAX_LEVEL - 1)
871 parent = path->nodes[level + 1];
872 pslot = path->slots[level + 1];
875 * deal with the case where there is only one pointer in the root
876 * by promoting the node below to a root
879 struct extent_buffer *child;
881 if (btrfs_header_nritems(mid) != 1)
884 /* promote the child to a root */
885 child = read_node_slot(root, mid, 0);
886 btrfs_tree_lock(child);
888 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
891 spin_lock(&root->node_lock);
893 spin_unlock(&root->node_lock);
895 ret = btrfs_update_extent_ref(trans, root, child->start,
896 mid->start, child->start,
897 root->root_key.objectid,
898 trans->transid, level - 1);
901 add_root_to_dirty_list(root);
902 btrfs_tree_unlock(child);
903 path->locks[level] = 0;
904 path->nodes[level] = NULL;
905 clean_tree_block(trans, root, mid);
906 btrfs_tree_unlock(mid);
907 /* once for the path */
908 free_extent_buffer(mid);
909 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
910 mid->start, root->root_key.objectid,
911 btrfs_header_generation(mid),
913 /* once for the root ptr */
914 free_extent_buffer(mid);
917 if (btrfs_header_nritems(mid) >
918 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
921 if (btrfs_header_nritems(mid) < 2)
924 left = read_node_slot(root, parent, pslot - 1);
926 btrfs_tree_lock(left);
927 wret = btrfs_cow_block(trans, root, left,
928 parent, pslot - 1, &left, 0);
934 right = read_node_slot(root, parent, pslot + 1);
936 btrfs_tree_lock(right);
937 wret = btrfs_cow_block(trans, root, right,
938 parent, pslot + 1, &right, 0);
945 /* first, try to make some room in the middle buffer */
947 orig_slot += btrfs_header_nritems(left);
948 wret = push_node_left(trans, root, left, mid, 1);
951 if (btrfs_header_nritems(mid) < 2)
956 * then try to empty the right most buffer into the middle
959 wret = push_node_left(trans, root, mid, right, 1);
960 if (wret < 0 && wret != -ENOSPC)
962 if (btrfs_header_nritems(right) == 0) {
963 u64 bytenr = right->start;
964 u64 generation = btrfs_header_generation(parent);
965 u32 blocksize = right->len;
967 clean_tree_block(trans, root, right);
968 btrfs_tree_unlock(right);
969 free_extent_buffer(right);
971 wret = del_ptr(trans, root, path, level + 1, pslot +
975 wret = btrfs_free_extent(trans, root, bytenr,
976 blocksize, parent->start,
977 btrfs_header_owner(parent),
978 generation, level, 1);
982 struct btrfs_disk_key right_key;
983 btrfs_node_key(right, &right_key, 0);
984 btrfs_set_node_key(parent, &right_key, pslot + 1);
985 btrfs_mark_buffer_dirty(parent);
988 if (btrfs_header_nritems(mid) == 1) {
990 * we're not allowed to leave a node with one item in the
991 * tree during a delete. A deletion from lower in the tree
992 * could try to delete the only pointer in this node.
993 * So, pull some keys from the left.
994 * There has to be a left pointer at this point because
995 * otherwise we would have pulled some pointers from the
999 wret = balance_node_right(trans, root, mid, left);
1005 wret = push_node_left(trans, root, left, mid, 1);
1011 if (btrfs_header_nritems(mid) == 0) {
1012 /* we've managed to empty the middle node, drop it */
1013 u64 root_gen = btrfs_header_generation(parent);
1014 u64 bytenr = mid->start;
1015 u32 blocksize = mid->len;
1017 clean_tree_block(trans, root, mid);
1018 btrfs_tree_unlock(mid);
1019 free_extent_buffer(mid);
1021 wret = del_ptr(trans, root, path, level + 1, pslot);
1024 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
1026 btrfs_header_owner(parent),
1027 root_gen, level, 1);
1031 /* update the parent key to reflect our changes */
1032 struct btrfs_disk_key mid_key;
1033 btrfs_node_key(mid, &mid_key, 0);
1034 btrfs_set_node_key(parent, &mid_key, pslot);
1035 btrfs_mark_buffer_dirty(parent);
1038 /* update the path */
1040 if (btrfs_header_nritems(left) > orig_slot) {
1041 extent_buffer_get(left);
1042 /* left was locked after cow */
1043 path->nodes[level] = left;
1044 path->slots[level + 1] -= 1;
1045 path->slots[level] = orig_slot;
1047 btrfs_tree_unlock(mid);
1048 free_extent_buffer(mid);
1051 orig_slot -= btrfs_header_nritems(left);
1052 path->slots[level] = orig_slot;
1055 /* double check we haven't messed things up */
1056 check_block(root, path, level);
1058 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
1062 btrfs_tree_unlock(right);
1063 free_extent_buffer(right);
1066 if (path->nodes[level] != left)
1067 btrfs_tree_unlock(left);
1068 free_extent_buffer(left);
1073 /* Node balancing for insertion. Here we only split or push nodes around
1074 * when they are completely full. This is also done top down, so we
1075 * have to be pessimistic.
1077 static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1078 struct btrfs_root *root,
1079 struct btrfs_path *path, int level)
1081 struct extent_buffer *right = NULL;
1082 struct extent_buffer *mid;
1083 struct extent_buffer *left = NULL;
1084 struct extent_buffer *parent = NULL;
1088 int orig_slot = path->slots[level];
1094 mid = path->nodes[level];
1095 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1096 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1098 if (level < BTRFS_MAX_LEVEL - 1)
1099 parent = path->nodes[level + 1];
1100 pslot = path->slots[level + 1];
1105 left = read_node_slot(root, parent, pslot - 1);
1107 /* first, try to make some room in the middle buffer */
1111 btrfs_tree_lock(left);
1112 left_nr = btrfs_header_nritems(left);
1113 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1116 ret = btrfs_cow_block(trans, root, left, parent,
1117 pslot - 1, &left, 0);
1121 wret = push_node_left(trans, root,
1128 struct btrfs_disk_key disk_key;
1129 orig_slot += left_nr;
1130 btrfs_node_key(mid, &disk_key, 0);
1131 btrfs_set_node_key(parent, &disk_key, pslot);
1132 btrfs_mark_buffer_dirty(parent);
1133 if (btrfs_header_nritems(left) > orig_slot) {
1134 path->nodes[level] = left;
1135 path->slots[level + 1] -= 1;
1136 path->slots[level] = orig_slot;
1137 btrfs_tree_unlock(mid);
1138 free_extent_buffer(mid);
1141 btrfs_header_nritems(left);
1142 path->slots[level] = orig_slot;
1143 btrfs_tree_unlock(left);
1144 free_extent_buffer(left);
1148 btrfs_tree_unlock(left);
1149 free_extent_buffer(left);
1151 right = read_node_slot(root, parent, pslot + 1);
1154 * then try to empty the right most buffer into the middle
1158 btrfs_tree_lock(right);
1159 right_nr = btrfs_header_nritems(right);
1160 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1163 ret = btrfs_cow_block(trans, root, right,
1169 wret = balance_node_right(trans, root,
1176 struct btrfs_disk_key disk_key;
1178 btrfs_node_key(right, &disk_key, 0);
1179 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1180 btrfs_mark_buffer_dirty(parent);
1182 if (btrfs_header_nritems(mid) <= orig_slot) {
1183 path->nodes[level] = right;
1184 path->slots[level + 1] += 1;
1185 path->slots[level] = orig_slot -
1186 btrfs_header_nritems(mid);
1187 btrfs_tree_unlock(mid);
1188 free_extent_buffer(mid);
1190 btrfs_tree_unlock(right);
1191 free_extent_buffer(right);
1195 btrfs_tree_unlock(right);
1196 free_extent_buffer(right);
1202 * readahead one full node of leaves, finding things that are close
1203 * to the block in 'slot', and triggering ra on them.
1205 static noinline void reada_for_search(struct btrfs_root *root,
1206 struct btrfs_path *path,
1207 int level, int slot, u64 objectid)
1209 struct extent_buffer *node;
1210 struct btrfs_disk_key disk_key;
1215 int direction = path->reada;
1216 struct extent_buffer *eb;
1224 if (!path->nodes[level])
1227 node = path->nodes[level];
1229 search = btrfs_node_blockptr(node, slot);
1230 blocksize = btrfs_level_size(root, level - 1);
1231 eb = btrfs_find_tree_block(root, search, blocksize);
1233 free_extent_buffer(eb);
1239 nritems = btrfs_header_nritems(node);
1242 if (direction < 0) {
1246 } else if (direction > 0) {
1251 if (path->reada < 0 && objectid) {
1252 btrfs_node_key(node, &disk_key, nr);
1253 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1256 search = btrfs_node_blockptr(node, nr);
1257 if ((search <= target && target - search <= 65536) ||
1258 (search > target && search - target <= 65536)) {
1259 readahead_tree_block(root, search, blocksize,
1260 btrfs_node_ptr_generation(node, nr));
1264 if ((nread > 65536 || nscan > 32))
1270 * when we walk down the tree, it is usually safe to unlock the higher layers
1271 * in the tree. The exceptions are when our path goes through slot 0, because
1272 * operations on the tree might require changing key pointers higher up in the
1275 * callers might also have set path->keep_locks, which tells this code to keep
1276 * the lock if the path points to the last slot in the block. This is part of
1277 * walking through the tree, and selecting the next slot in the higher block.
1279 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1280 * if lowest_unlock is 1, level 0 won't be unlocked
1282 static noinline void unlock_up(struct btrfs_path *path, int level,
1286 int skip_level = level;
1288 struct extent_buffer *t;
1290 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1291 if (!path->nodes[i])
1293 if (!path->locks[i])
1295 if (!no_skips && path->slots[i] == 0) {
1299 if (!no_skips && path->keep_locks) {
1302 nritems = btrfs_header_nritems(t);
1303 if (nritems < 1 || path->slots[i] >= nritems - 1) {
1308 if (skip_level < i && i >= lowest_unlock)
1312 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1313 btrfs_tree_unlock(t);
1320 * look for key in the tree. path is filled in with nodes along the way
1321 * if key is found, we return zero and you can find the item in the leaf
1322 * level of the path (level 0)
1324 * If the key isn't found, the path points to the slot where it should
1325 * be inserted, and 1 is returned. If there are other errors during the
1326 * search a negative error number is returned.
1328 * if ins_len > 0, nodes and leaves will be split as we walk down the
1329 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1332 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1333 *root, struct btrfs_key *key, struct btrfs_path *p, int
1336 struct extent_buffer *b;
1337 struct extent_buffer *tmp;
1341 int should_reada = p->reada;
1342 int lowest_unlock = 1;
1344 u8 lowest_level = 0;
1347 struct btrfs_key prealloc_block;
1349 lowest_level = p->lowest_level;
1350 WARN_ON(lowest_level && ins_len > 0);
1351 WARN_ON(p->nodes[0] != NULL);
1356 prealloc_block.objectid = 0;
1359 if (p->skip_locking)
1360 b = btrfs_root_node(root);
1362 b = btrfs_lock_root_node(root);
1365 level = btrfs_header_level(b);
1368 * setup the path here so we can release it under lock
1369 * contention with the cow code
1371 p->nodes[level] = b;
1372 if (!p->skip_locking)
1373 p->locks[level] = 1;
1378 /* is a cow on this block not required */
1379 spin_lock(&root->fs_info->hash_lock);
1380 if (btrfs_header_generation(b) == trans->transid &&
1381 btrfs_header_owner(b) == root->root_key.objectid &&
1382 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
1383 spin_unlock(&root->fs_info->hash_lock);
1386 spin_unlock(&root->fs_info->hash_lock);
1388 /* ok, we have to cow, is our old prealloc the right
1391 if (prealloc_block.objectid &&
1392 prealloc_block.offset != b->len) {
1393 btrfs_free_reserved_extent(root,
1394 prealloc_block.objectid,
1395 prealloc_block.offset);
1396 prealloc_block.objectid = 0;
1400 * for higher level blocks, try not to allocate blocks
1401 * with the block and the parent locks held.
1403 if (level > 1 && !prealloc_block.objectid &&
1404 btrfs_path_lock_waiting(p, level)) {
1406 u64 hint = b->start;
1408 btrfs_release_path(root, p);
1409 ret = btrfs_reserve_extent(trans, root,
1412 &prealloc_block, 0);
1417 wret = btrfs_cow_block(trans, root, b,
1418 p->nodes[level + 1],
1419 p->slots[level + 1],
1420 &b, prealloc_block.objectid);
1421 prealloc_block.objectid = 0;
1423 free_extent_buffer(b);
1429 BUG_ON(!cow && ins_len);
1430 if (level != btrfs_header_level(b))
1432 level = btrfs_header_level(b);
1434 p->nodes[level] = b;
1435 if (!p->skip_locking)
1436 p->locks[level] = 1;
1438 ret = check_block(root, p, level);
1444 ret = bin_search(b, key, level, &slot);
1446 if (ret && slot > 0)
1448 p->slots[level] = slot;
1449 if ((p->search_for_split || ins_len > 0) &&
1450 btrfs_header_nritems(b) >=
1451 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1452 int sret = split_node(trans, root, p, level);
1458 b = p->nodes[level];
1459 slot = p->slots[level];
1460 } else if (ins_len < 0) {
1461 int sret = balance_level(trans, root, p,
1467 b = p->nodes[level];
1469 btrfs_release_path(NULL, p);
1472 slot = p->slots[level];
1473 BUG_ON(btrfs_header_nritems(b) == 1);
1475 unlock_up(p, level, lowest_unlock);
1477 /* this is only true while dropping a snapshot */
1478 if (level == lowest_level) {
1483 blocknr = btrfs_node_blockptr(b, slot);
1484 gen = btrfs_node_ptr_generation(b, slot);
1485 blocksize = btrfs_level_size(root, level - 1);
1487 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1488 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1492 * reduce lock contention at high levels
1493 * of the btree by dropping locks before
1497 btrfs_release_path(NULL, p);
1499 free_extent_buffer(tmp);
1501 reada_for_search(root, p,
1505 tmp = read_tree_block(root, blocknr,
1508 free_extent_buffer(tmp);
1512 free_extent_buffer(tmp);
1514 reada_for_search(root, p,
1517 b = read_node_slot(root, b, slot);
1520 if (!p->skip_locking)
1523 p->slots[level] = slot;
1525 btrfs_leaf_free_space(root, b) < ins_len) {
1526 int sret = split_leaf(trans, root, key,
1527 p, ins_len, ret == 0);
1534 if (!p->search_for_split)
1535 unlock_up(p, level, lowest_unlock);
1541 if (prealloc_block.objectid) {
1542 btrfs_free_reserved_extent(root,
1543 prealloc_block.objectid,
1544 prealloc_block.offset);
1550 int btrfs_merge_path(struct btrfs_trans_handle *trans,
1551 struct btrfs_root *root,
1552 struct btrfs_key *node_keys,
1553 u64 *nodes, int lowest_level)
1555 struct extent_buffer *eb;
1556 struct extent_buffer *parent;
1557 struct btrfs_key key;
1566 eb = btrfs_lock_root_node(root);
1567 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0);
1572 level = btrfs_header_level(parent);
1573 if (level == 0 || level <= lowest_level)
1576 ret = bin_search(parent, &node_keys[lowest_level], level,
1578 if (ret && slot > 0)
1581 bytenr = btrfs_node_blockptr(parent, slot);
1582 if (nodes[level - 1] == bytenr)
1585 blocksize = btrfs_level_size(root, level - 1);
1586 generation = btrfs_node_ptr_generation(parent, slot);
1587 btrfs_node_key_to_cpu(eb, &key, slot);
1588 key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key));
1590 if (generation == trans->transid) {
1591 eb = read_tree_block(root, bytenr, blocksize,
1593 btrfs_tree_lock(eb);
1597 * if node keys match and node pointer hasn't been modified
1598 * in the running transaction, we can merge the path. for
1599 * blocks owened by reloc trees, the node pointer check is
1600 * skipped, this is because these blocks are fully controlled
1601 * by the space balance code, no one else can modify them.
1603 if (!nodes[level - 1] || !key_match ||
1604 (generation == trans->transid &&
1605 btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID)) {
1606 if (level == 1 || level == lowest_level + 1) {
1607 if (generation == trans->transid) {
1608 btrfs_tree_unlock(eb);
1609 free_extent_buffer(eb);
1614 if (generation != trans->transid) {
1615 eb = read_tree_block(root, bytenr, blocksize,
1617 btrfs_tree_lock(eb);
1620 ret = btrfs_cow_block(trans, root, eb, parent, slot,
1624 if (root->root_key.objectid ==
1625 BTRFS_TREE_RELOC_OBJECTID) {
1626 if (!nodes[level - 1]) {
1627 nodes[level - 1] = eb->start;
1628 memcpy(&node_keys[level - 1], &key,
1629 sizeof(node_keys[0]));
1635 btrfs_tree_unlock(parent);
1636 free_extent_buffer(parent);
1641 btrfs_set_node_blockptr(parent, slot, nodes[level - 1]);
1642 btrfs_set_node_ptr_generation(parent, slot, trans->transid);
1643 btrfs_mark_buffer_dirty(parent);
1645 ret = btrfs_inc_extent_ref(trans, root,
1647 blocksize, parent->start,
1648 btrfs_header_owner(parent),
1649 btrfs_header_generation(parent),
1654 * If the block was created in the running transaction,
1655 * it's possible this is the last reference to it, so we
1656 * should drop the subtree.
1658 if (generation == trans->transid) {
1659 ret = btrfs_drop_subtree(trans, root, eb, parent);
1661 btrfs_tree_unlock(eb);
1662 free_extent_buffer(eb);
1664 ret = btrfs_free_extent(trans, root, bytenr,
1665 blocksize, parent->start,
1666 btrfs_header_owner(parent),
1667 btrfs_header_generation(parent),
1673 btrfs_tree_unlock(parent);
1674 free_extent_buffer(parent);
1679 * adjust the pointers going up the tree, starting at level
1680 * making sure the right key of each node is points to 'key'.
1681 * This is used after shifting pointers to the left, so it stops
1682 * fixing up pointers when a given leaf/node is not in slot 0 of the
1685 * If this fails to write a tree block, it returns -1, but continues
1686 * fixing up the blocks in ram so the tree is consistent.
1688 static int fixup_low_keys(struct btrfs_trans_handle *trans,
1689 struct btrfs_root *root, struct btrfs_path *path,
1690 struct btrfs_disk_key *key, int level)
1694 struct extent_buffer *t;
1696 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1697 int tslot = path->slots[i];
1698 if (!path->nodes[i])
1701 btrfs_set_node_key(t, key, tslot);
1702 btrfs_mark_buffer_dirty(path->nodes[i]);
1712 * This function isn't completely safe. It's the caller's responsibility
1713 * that the new key won't break the order
1715 int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1716 struct btrfs_root *root, struct btrfs_path *path,
1717 struct btrfs_key *new_key)
1719 struct btrfs_disk_key disk_key;
1720 struct extent_buffer *eb;
1723 eb = path->nodes[0];
1724 slot = path->slots[0];
1726 btrfs_item_key(eb, &disk_key, slot - 1);
1727 if (comp_keys(&disk_key, new_key) >= 0)
1730 if (slot < btrfs_header_nritems(eb) - 1) {
1731 btrfs_item_key(eb, &disk_key, slot + 1);
1732 if (comp_keys(&disk_key, new_key) <= 0)
1736 btrfs_cpu_key_to_disk(&disk_key, new_key);
1737 btrfs_set_item_key(eb, &disk_key, slot);
1738 btrfs_mark_buffer_dirty(eb);
1740 fixup_low_keys(trans, root, path, &disk_key, 1);
1745 * try to push data from one node into the next node left in the
1748 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1749 * error, and > 0 if there was no room in the left hand block.
1751 static int push_node_left(struct btrfs_trans_handle *trans,
1752 struct btrfs_root *root, struct extent_buffer *dst,
1753 struct extent_buffer *src, int empty)
1760 src_nritems = btrfs_header_nritems(src);
1761 dst_nritems = btrfs_header_nritems(dst);
1762 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1763 WARN_ON(btrfs_header_generation(src) != trans->transid);
1764 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1766 if (!empty && src_nritems <= 8)
1769 if (push_items <= 0)
1773 push_items = min(src_nritems, push_items);
1774 if (push_items < src_nritems) {
1775 /* leave at least 8 pointers in the node if
1776 * we aren't going to empty it
1778 if (src_nritems - push_items < 8) {
1779 if (push_items <= 8)
1785 push_items = min(src_nritems - 8, push_items);
1787 copy_extent_buffer(dst, src,
1788 btrfs_node_key_ptr_offset(dst_nritems),
1789 btrfs_node_key_ptr_offset(0),
1790 push_items * sizeof(struct btrfs_key_ptr));
1792 if (push_items < src_nritems) {
1793 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1794 btrfs_node_key_ptr_offset(push_items),
1795 (src_nritems - push_items) *
1796 sizeof(struct btrfs_key_ptr));
1798 btrfs_set_header_nritems(src, src_nritems - push_items);
1799 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1800 btrfs_mark_buffer_dirty(src);
1801 btrfs_mark_buffer_dirty(dst);
1803 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
1810 * try to push data from one node into the next node right in the
1813 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1814 * error, and > 0 if there was no room in the right hand block.
1816 * this will only push up to 1/2 the contents of the left node over
1818 static int balance_node_right(struct btrfs_trans_handle *trans,
1819 struct btrfs_root *root,
1820 struct extent_buffer *dst,
1821 struct extent_buffer *src)
1829 WARN_ON(btrfs_header_generation(src) != trans->transid);
1830 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1832 src_nritems = btrfs_header_nritems(src);
1833 dst_nritems = btrfs_header_nritems(dst);
1834 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1835 if (push_items <= 0)
1838 if (src_nritems < 4)
1841 max_push = src_nritems / 2 + 1;
1842 /* don't try to empty the node */
1843 if (max_push >= src_nritems)
1846 if (max_push < push_items)
1847 push_items = max_push;
1849 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1850 btrfs_node_key_ptr_offset(0),
1852 sizeof(struct btrfs_key_ptr));
1854 copy_extent_buffer(dst, src,
1855 btrfs_node_key_ptr_offset(0),
1856 btrfs_node_key_ptr_offset(src_nritems - push_items),
1857 push_items * sizeof(struct btrfs_key_ptr));
1859 btrfs_set_header_nritems(src, src_nritems - push_items);
1860 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1862 btrfs_mark_buffer_dirty(src);
1863 btrfs_mark_buffer_dirty(dst);
1865 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
1872 * helper function to insert a new root level in the tree.
1873 * A new node is allocated, and a single item is inserted to
1874 * point to the existing root
1876 * returns zero on success or < 0 on failure.
1878 static noinline int insert_new_root(struct btrfs_trans_handle *trans,
1879 struct btrfs_root *root,
1880 struct btrfs_path *path, int level)
1883 struct extent_buffer *lower;
1884 struct extent_buffer *c;
1885 struct extent_buffer *old;
1886 struct btrfs_disk_key lower_key;
1889 BUG_ON(path->nodes[level]);
1890 BUG_ON(path->nodes[level-1] != root->node);
1892 lower = path->nodes[level-1];
1894 btrfs_item_key(lower, &lower_key, 0);
1896 btrfs_node_key(lower, &lower_key, 0);
1898 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
1899 root->root_key.objectid, trans->transid,
1900 level, root->node->start, 0);
1904 memset_extent_buffer(c, 0, 0, root->nodesize);
1905 btrfs_set_header_nritems(c, 1);
1906 btrfs_set_header_level(c, level);
1907 btrfs_set_header_bytenr(c, c->start);
1908 btrfs_set_header_generation(c, trans->transid);
1909 btrfs_set_header_owner(c, root->root_key.objectid);
1911 write_extent_buffer(c, root->fs_info->fsid,
1912 (unsigned long)btrfs_header_fsid(c),
1915 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1916 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1919 btrfs_set_node_key(c, &lower_key, 0);
1920 btrfs_set_node_blockptr(c, 0, lower->start);
1921 lower_gen = btrfs_header_generation(lower);
1922 WARN_ON(lower_gen != trans->transid);
1924 btrfs_set_node_ptr_generation(c, 0, lower_gen);
1926 btrfs_mark_buffer_dirty(c);
1928 spin_lock(&root->node_lock);
1931 spin_unlock(&root->node_lock);
1933 ret = btrfs_update_extent_ref(trans, root, lower->start,
1934 lower->start, c->start,
1935 root->root_key.objectid,
1936 trans->transid, level - 1);
1939 /* the super has an extra ref to root->node */
1940 free_extent_buffer(old);
1942 add_root_to_dirty_list(root);
1943 extent_buffer_get(c);
1944 path->nodes[level] = c;
1945 path->locks[level] = 1;
1946 path->slots[level] = 0;
1951 * worker function to insert a single pointer in a node.
1952 * the node should have enough room for the pointer already
1954 * slot and level indicate where you want the key to go, and
1955 * blocknr is the block the key points to.
1957 * returns zero on success and < 0 on any error
1959 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1960 *root, struct btrfs_path *path, struct btrfs_disk_key
1961 *key, u64 bytenr, int slot, int level)
1963 struct extent_buffer *lower;
1966 BUG_ON(!path->nodes[level]);
1967 lower = path->nodes[level];
1968 nritems = btrfs_header_nritems(lower);
1971 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
1973 if (slot != nritems) {
1974 memmove_extent_buffer(lower,
1975 btrfs_node_key_ptr_offset(slot + 1),
1976 btrfs_node_key_ptr_offset(slot),
1977 (nritems - slot) * sizeof(struct btrfs_key_ptr));
1979 btrfs_set_node_key(lower, key, slot);
1980 btrfs_set_node_blockptr(lower, slot, bytenr);
1981 WARN_ON(trans->transid == 0);
1982 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
1983 btrfs_set_header_nritems(lower, nritems + 1);
1984 btrfs_mark_buffer_dirty(lower);
1989 * split the node at the specified level in path in two.
1990 * The path is corrected to point to the appropriate node after the split
1992 * Before splitting this tries to make some room in the node by pushing
1993 * left and right, if either one works, it returns right away.
1995 * returns 0 on success and < 0 on failure
1997 static noinline int split_node(struct btrfs_trans_handle *trans,
1998 struct btrfs_root *root,
1999 struct btrfs_path *path, int level)
2001 struct extent_buffer *c;
2002 struct extent_buffer *split;
2003 struct btrfs_disk_key disk_key;
2009 c = path->nodes[level];
2010 WARN_ON(btrfs_header_generation(c) != trans->transid);
2011 if (c == root->node) {
2012 /* trying to split the root, lets make a new one */
2013 ret = insert_new_root(trans, root, path, level + 1);
2017 ret = push_nodes_for_insert(trans, root, path, level);
2018 c = path->nodes[level];
2019 if (!ret && btrfs_header_nritems(c) <
2020 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
2026 c_nritems = btrfs_header_nritems(c);
2028 split = btrfs_alloc_free_block(trans, root, root->nodesize,
2029 path->nodes[level + 1]->start,
2030 root->root_key.objectid,
2031 trans->transid, level, c->start, 0);
2033 return PTR_ERR(split);
2035 btrfs_set_header_flags(split, btrfs_header_flags(c));
2036 btrfs_set_header_level(split, btrfs_header_level(c));
2037 btrfs_set_header_bytenr(split, split->start);
2038 btrfs_set_header_generation(split, trans->transid);
2039 btrfs_set_header_owner(split, root->root_key.objectid);
2040 btrfs_set_header_flags(split, 0);
2041 write_extent_buffer(split, root->fs_info->fsid,
2042 (unsigned long)btrfs_header_fsid(split),
2044 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2045 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2048 mid = (c_nritems + 1) / 2;
2050 copy_extent_buffer(split, c,
2051 btrfs_node_key_ptr_offset(0),
2052 btrfs_node_key_ptr_offset(mid),
2053 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2054 btrfs_set_header_nritems(split, c_nritems - mid);
2055 btrfs_set_header_nritems(c, mid);
2058 btrfs_mark_buffer_dirty(c);
2059 btrfs_mark_buffer_dirty(split);
2061 btrfs_node_key(split, &disk_key, 0);
2062 wret = insert_ptr(trans, root, path, &disk_key, split->start,
2063 path->slots[level + 1] + 1,
2068 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
2071 if (path->slots[level] >= mid) {
2072 path->slots[level] -= mid;
2073 btrfs_tree_unlock(c);
2074 free_extent_buffer(c);
2075 path->nodes[level] = split;
2076 path->slots[level + 1] += 1;
2078 btrfs_tree_unlock(split);
2079 free_extent_buffer(split);
2085 * how many bytes are required to store the items in a leaf. start
2086 * and nr indicate which items in the leaf to check. This totals up the
2087 * space used both by the item structs and the item data
2089 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
2092 int nritems = btrfs_header_nritems(l);
2093 int end = min(nritems, start + nr) - 1;
2097 data_len = btrfs_item_end_nr(l, start);
2098 data_len = data_len - btrfs_item_offset_nr(l, end);
2099 data_len += sizeof(struct btrfs_item) * nr;
2100 WARN_ON(data_len < 0);
2105 * The space between the end of the leaf items and
2106 * the start of the leaf data. IOW, how much room
2107 * the leaf has left for both items and data
2109 noinline int btrfs_leaf_free_space(struct btrfs_root *root,
2110 struct extent_buffer *leaf)
2112 int nritems = btrfs_header_nritems(leaf);
2114 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2116 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2117 "used %d nritems %d\n",
2118 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
2119 leaf_space_used(leaf, 0, nritems), nritems);
2125 * push some data in the path leaf to the right, trying to free up at
2126 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2128 * returns 1 if the push failed because the other node didn't have enough
2129 * room, 0 if everything worked out and < 0 if there were major errors.
2131 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
2132 *root, struct btrfs_path *path, int data_size,
2135 struct extent_buffer *left = path->nodes[0];
2136 struct extent_buffer *right;
2137 struct extent_buffer *upper;
2138 struct btrfs_disk_key disk_key;
2144 struct btrfs_item *item;
2152 slot = path->slots[1];
2153 if (!path->nodes[1])
2156 upper = path->nodes[1];
2157 if (slot >= btrfs_header_nritems(upper) - 1)
2160 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2162 right = read_node_slot(root, upper, slot + 1);
2163 btrfs_tree_lock(right);
2164 free_space = btrfs_leaf_free_space(root, right);
2165 if (free_space < data_size)
2168 /* cow and double check */
2169 ret = btrfs_cow_block(trans, root, right, upper,
2170 slot + 1, &right, 0);
2174 free_space = btrfs_leaf_free_space(root, right);
2175 if (free_space < data_size)
2178 left_nritems = btrfs_header_nritems(left);
2179 if (left_nritems == 0)
2187 if (path->slots[0] >= left_nritems)
2188 push_space += data_size;
2190 i = left_nritems - 1;
2192 item = btrfs_item_nr(left, i);
2194 if (!empty && push_items > 0) {
2195 if (path->slots[0] > i)
2197 if (path->slots[0] == i) {
2198 int space = btrfs_leaf_free_space(root, left);
2199 if (space + push_space * 2 > free_space)
2204 if (path->slots[0] == i)
2205 push_space += data_size;
2207 if (!left->map_token) {
2208 map_extent_buffer(left, (unsigned long)item,
2209 sizeof(struct btrfs_item),
2210 &left->map_token, &left->kaddr,
2211 &left->map_start, &left->map_len,
2215 this_item_size = btrfs_item_size(left, item);
2216 if (this_item_size + sizeof(*item) + push_space > free_space)
2220 push_space += this_item_size + sizeof(*item);
2225 if (left->map_token) {
2226 unmap_extent_buffer(left, left->map_token, KM_USER1);
2227 left->map_token = NULL;
2230 if (push_items == 0)
2233 if (!empty && push_items == left_nritems)
2236 /* push left to right */
2237 right_nritems = btrfs_header_nritems(right);
2239 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
2240 push_space -= leaf_data_end(root, left);
2242 /* make room in the right data area */
2243 data_end = leaf_data_end(root, right);
2244 memmove_extent_buffer(right,
2245 btrfs_leaf_data(right) + data_end - push_space,
2246 btrfs_leaf_data(right) + data_end,
2247 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2249 /* copy from the left data area */
2250 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
2251 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2252 btrfs_leaf_data(left) + leaf_data_end(root, left),
2255 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2256 btrfs_item_nr_offset(0),
2257 right_nritems * sizeof(struct btrfs_item));
2259 /* copy the items from left to right */
2260 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2261 btrfs_item_nr_offset(left_nritems - push_items),
2262 push_items * sizeof(struct btrfs_item));
2264 /* update the item pointers */
2265 right_nritems += push_items;
2266 btrfs_set_header_nritems(right, right_nritems);
2267 push_space = BTRFS_LEAF_DATA_SIZE(root);
2268 for (i = 0; i < right_nritems; i++) {
2269 item = btrfs_item_nr(right, i);
2270 if (!right->map_token) {
2271 map_extent_buffer(right, (unsigned long)item,
2272 sizeof(struct btrfs_item),
2273 &right->map_token, &right->kaddr,
2274 &right->map_start, &right->map_len,
2277 push_space -= btrfs_item_size(right, item);
2278 btrfs_set_item_offset(right, item, push_space);
2281 if (right->map_token) {
2282 unmap_extent_buffer(right, right->map_token, KM_USER1);
2283 right->map_token = NULL;
2285 left_nritems -= push_items;
2286 btrfs_set_header_nritems(left, left_nritems);
2289 btrfs_mark_buffer_dirty(left);
2290 btrfs_mark_buffer_dirty(right);
2292 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2295 btrfs_item_key(right, &disk_key, 0);
2296 btrfs_set_node_key(upper, &disk_key, slot + 1);
2297 btrfs_mark_buffer_dirty(upper);
2299 /* then fixup the leaf pointer in the path */
2300 if (path->slots[0] >= left_nritems) {
2301 path->slots[0] -= left_nritems;
2302 if (btrfs_header_nritems(path->nodes[0]) == 0)
2303 clean_tree_block(trans, root, path->nodes[0]);
2304 btrfs_tree_unlock(path->nodes[0]);
2305 free_extent_buffer(path->nodes[0]);
2306 path->nodes[0] = right;
2307 path->slots[1] += 1;
2309 btrfs_tree_unlock(right);
2310 free_extent_buffer(right);
2315 btrfs_tree_unlock(right);
2316 free_extent_buffer(right);
2321 * push some data in the path leaf to the left, trying to free up at
2322 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2324 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
2325 *root, struct btrfs_path *path, int data_size,
2328 struct btrfs_disk_key disk_key;
2329 struct extent_buffer *right = path->nodes[0];
2330 struct extent_buffer *left;
2336 struct btrfs_item *item;
2337 u32 old_left_nritems;
2343 u32 old_left_item_size;
2345 slot = path->slots[1];
2348 if (!path->nodes[1])
2351 right_nritems = btrfs_header_nritems(right);
2352 if (right_nritems == 0)
2355 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2357 left = read_node_slot(root, path->nodes[1], slot - 1);
2358 btrfs_tree_lock(left);
2359 free_space = btrfs_leaf_free_space(root, left);
2360 if (free_space < data_size) {
2365 /* cow and double check */
2366 ret = btrfs_cow_block(trans, root, left,
2367 path->nodes[1], slot - 1, &left, 0);
2369 /* we hit -ENOSPC, but it isn't fatal here */
2374 free_space = btrfs_leaf_free_space(root, left);
2375 if (free_space < data_size) {
2383 nr = right_nritems - 1;
2385 for (i = 0; i < nr; i++) {
2386 item = btrfs_item_nr(right, i);
2387 if (!right->map_token) {
2388 map_extent_buffer(right, (unsigned long)item,
2389 sizeof(struct btrfs_item),
2390 &right->map_token, &right->kaddr,
2391 &right->map_start, &right->map_len,
2395 if (!empty && push_items > 0) {
2396 if (path->slots[0] < i)
2398 if (path->slots[0] == i) {
2399 int space = btrfs_leaf_free_space(root, right);
2400 if (space + push_space * 2 > free_space)
2405 if (path->slots[0] == i)
2406 push_space += data_size;
2408 this_item_size = btrfs_item_size(right, item);
2409 if (this_item_size + sizeof(*item) + push_space > free_space)
2413 push_space += this_item_size + sizeof(*item);
2416 if (right->map_token) {
2417 unmap_extent_buffer(right, right->map_token, KM_USER1);
2418 right->map_token = NULL;
2421 if (push_items == 0) {
2425 if (!empty && push_items == btrfs_header_nritems(right))
2428 /* push data from right to left */
2429 copy_extent_buffer(left, right,
2430 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2431 btrfs_item_nr_offset(0),
2432 push_items * sizeof(struct btrfs_item));
2434 push_space = BTRFS_LEAF_DATA_SIZE(root) -
2435 btrfs_item_offset_nr(right, push_items - 1);
2437 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
2438 leaf_data_end(root, left) - push_space,
2439 btrfs_leaf_data(right) +
2440 btrfs_item_offset_nr(right, push_items - 1),
2442 old_left_nritems = btrfs_header_nritems(left);
2443 BUG_ON(old_left_nritems <= 0);
2445 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
2446 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
2449 item = btrfs_item_nr(left, i);
2450 if (!left->map_token) {
2451 map_extent_buffer(left, (unsigned long)item,
2452 sizeof(struct btrfs_item),
2453 &left->map_token, &left->kaddr,
2454 &left->map_start, &left->map_len,
2458 ioff = btrfs_item_offset(left, item);
2459 btrfs_set_item_offset(left, item,
2460 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
2462 btrfs_set_header_nritems(left, old_left_nritems + push_items);
2463 if (left->map_token) {
2464 unmap_extent_buffer(left, left->map_token, KM_USER1);
2465 left->map_token = NULL;
2468 /* fixup right node */
2469 if (push_items > right_nritems) {
2470 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2475 if (push_items < right_nritems) {
2476 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2477 leaf_data_end(root, right);
2478 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2479 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2480 btrfs_leaf_data(right) +
2481 leaf_data_end(root, right), push_space);
2483 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
2484 btrfs_item_nr_offset(push_items),
2485 (btrfs_header_nritems(right) - push_items) *
2486 sizeof(struct btrfs_item));
2488 right_nritems -= push_items;
2489 btrfs_set_header_nritems(right, right_nritems);
2490 push_space = BTRFS_LEAF_DATA_SIZE(root);
2491 for (i = 0; i < right_nritems; i++) {
2492 item = btrfs_item_nr(right, i);
2494 if (!right->map_token) {
2495 map_extent_buffer(right, (unsigned long)item,
2496 sizeof(struct btrfs_item),
2497 &right->map_token, &right->kaddr,
2498 &right->map_start, &right->map_len,
2502 push_space = push_space - btrfs_item_size(right, item);
2503 btrfs_set_item_offset(right, item, push_space);
2505 if (right->map_token) {
2506 unmap_extent_buffer(right, right->map_token, KM_USER1);
2507 right->map_token = NULL;
2510 btrfs_mark_buffer_dirty(left);
2512 btrfs_mark_buffer_dirty(right);
2514 ret = btrfs_update_ref(trans, root, right, left,
2515 old_left_nritems, push_items);
2518 btrfs_item_key(right, &disk_key, 0);
2519 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
2523 /* then fixup the leaf pointer in the path */
2524 if (path->slots[0] < push_items) {
2525 path->slots[0] += old_left_nritems;
2526 if (btrfs_header_nritems(path->nodes[0]) == 0)
2527 clean_tree_block(trans, root, path->nodes[0]);
2528 btrfs_tree_unlock(path->nodes[0]);
2529 free_extent_buffer(path->nodes[0]);
2530 path->nodes[0] = left;
2531 path->slots[1] -= 1;
2533 btrfs_tree_unlock(left);
2534 free_extent_buffer(left);
2535 path->slots[0] -= push_items;
2537 BUG_ON(path->slots[0] < 0);
2540 btrfs_tree_unlock(left);
2541 free_extent_buffer(left);
2546 * split the path's leaf in two, making sure there is at least data_size
2547 * available for the resulting leaf level of the path.
2549 * returns 0 if all went well and < 0 on failure.
2551 static noinline int split_leaf(struct btrfs_trans_handle *trans,
2552 struct btrfs_root *root,
2553 struct btrfs_key *ins_key,
2554 struct btrfs_path *path, int data_size,
2557 struct extent_buffer *l;
2561 struct extent_buffer *right;
2568 int num_doubles = 0;
2569 struct btrfs_disk_key disk_key;
2571 /* first try to make some room by pushing left and right */
2572 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
2573 wret = push_leaf_right(trans, root, path, data_size, 0);
2577 wret = push_leaf_left(trans, root, path, data_size, 0);
2583 /* did the pushes work? */
2584 if (btrfs_leaf_free_space(root, l) >= data_size)
2588 if (!path->nodes[1]) {
2589 ret = insert_new_root(trans, root, path, 1);
2596 slot = path->slots[0];
2597 nritems = btrfs_header_nritems(l);
2598 mid = (nritems + 1) / 2;
2600 right = btrfs_alloc_free_block(trans, root, root->leafsize,
2601 path->nodes[1]->start,
2602 root->root_key.objectid,
2603 trans->transid, 0, l->start, 0);
2604 if (IS_ERR(right)) {
2606 return PTR_ERR(right);
2609 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2610 btrfs_set_header_bytenr(right, right->start);
2611 btrfs_set_header_generation(right, trans->transid);
2612 btrfs_set_header_owner(right, root->root_key.objectid);
2613 btrfs_set_header_level(right, 0);
2614 write_extent_buffer(right, root->fs_info->fsid,
2615 (unsigned long)btrfs_header_fsid(right),
2618 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2619 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2623 leaf_space_used(l, mid, nritems - mid) + data_size >
2624 BTRFS_LEAF_DATA_SIZE(root)) {
2625 if (slot >= nritems) {
2626 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2627 btrfs_set_header_nritems(right, 0);
2628 wret = insert_ptr(trans, root, path,
2629 &disk_key, right->start,
2630 path->slots[1] + 1, 1);
2634 btrfs_tree_unlock(path->nodes[0]);
2635 free_extent_buffer(path->nodes[0]);
2636 path->nodes[0] = right;
2638 path->slots[1] += 1;
2639 btrfs_mark_buffer_dirty(right);
2643 if (mid != nritems &&
2644 leaf_space_used(l, mid, nritems - mid) +
2645 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2650 if (leaf_space_used(l, 0, mid) + data_size >
2651 BTRFS_LEAF_DATA_SIZE(root)) {
2652 if (!extend && data_size && slot == 0) {
2653 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2654 btrfs_set_header_nritems(right, 0);
2655 wret = insert_ptr(trans, root, path,
2661 btrfs_tree_unlock(path->nodes[0]);
2662 free_extent_buffer(path->nodes[0]);
2663 path->nodes[0] = right;
2665 if (path->slots[1] == 0) {
2666 wret = fixup_low_keys(trans, root,
2667 path, &disk_key, 1);
2671 btrfs_mark_buffer_dirty(right);
2673 } else if ((extend || !data_size) && slot == 0) {
2677 if (mid != nritems &&
2678 leaf_space_used(l, mid, nritems - mid) +
2679 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2685 nritems = nritems - mid;
2686 btrfs_set_header_nritems(right, nritems);
2687 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2689 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2690 btrfs_item_nr_offset(mid),
2691 nritems * sizeof(struct btrfs_item));
2693 copy_extent_buffer(right, l,
2694 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2695 data_copy_size, btrfs_leaf_data(l) +
2696 leaf_data_end(root, l), data_copy_size);
2698 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2699 btrfs_item_end_nr(l, mid);
2701 for (i = 0; i < nritems; i++) {
2702 struct btrfs_item *item = btrfs_item_nr(right, i);
2705 if (!right->map_token) {
2706 map_extent_buffer(right, (unsigned long)item,
2707 sizeof(struct btrfs_item),
2708 &right->map_token, &right->kaddr,
2709 &right->map_start, &right->map_len,
2713 ioff = btrfs_item_offset(right, item);
2714 btrfs_set_item_offset(right, item, ioff + rt_data_off);
2717 if (right->map_token) {
2718 unmap_extent_buffer(right, right->map_token, KM_USER1);
2719 right->map_token = NULL;
2722 btrfs_set_header_nritems(l, mid);
2724 btrfs_item_key(right, &disk_key, 0);
2725 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2726 path->slots[1] + 1, 1);
2730 btrfs_mark_buffer_dirty(right);
2731 btrfs_mark_buffer_dirty(l);
2732 BUG_ON(path->slots[0] != slot);
2734 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2738 btrfs_tree_unlock(path->nodes[0]);
2739 free_extent_buffer(path->nodes[0]);
2740 path->nodes[0] = right;
2741 path->slots[0] -= mid;
2742 path->slots[1] += 1;
2744 btrfs_tree_unlock(right);
2745 free_extent_buffer(right);
2748 BUG_ON(path->slots[0] < 0);
2751 BUG_ON(num_doubles != 0);
2759 * This function splits a single item into two items,
2760 * giving 'new_key' to the new item and splitting the
2761 * old one at split_offset (from the start of the item).
2763 * The path may be released by this operation. After
2764 * the split, the path is pointing to the old item. The
2765 * new item is going to be in the same node as the old one.
2767 * Note, the item being split must be smaller enough to live alone on
2768 * a tree block with room for one extra struct btrfs_item
2770 * This allows us to split the item in place, keeping a lock on the
2771 * leaf the entire time.
2773 int btrfs_split_item(struct btrfs_trans_handle *trans,
2774 struct btrfs_root *root,
2775 struct btrfs_path *path,
2776 struct btrfs_key *new_key,
2777 unsigned long split_offset)
2780 struct extent_buffer *leaf;
2781 struct btrfs_key orig_key;
2782 struct btrfs_item *item;
2783 struct btrfs_item *new_item;
2788 struct btrfs_disk_key disk_key;
2791 leaf = path->nodes[0];
2792 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
2793 if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item))
2796 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2797 btrfs_release_path(root, path);
2799 path->search_for_split = 1;
2800 path->keep_locks = 1;
2802 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
2803 path->search_for_split = 0;
2805 /* if our item isn't there or got smaller, return now */
2806 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
2808 path->keep_locks = 0;
2812 ret = split_leaf(trans, root, &orig_key, path,
2813 sizeof(struct btrfs_item), 1);
2814 path->keep_locks = 0;
2817 leaf = path->nodes[0];
2818 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
2821 item = btrfs_item_nr(leaf, path->slots[0]);
2822 orig_offset = btrfs_item_offset(leaf, item);
2823 item_size = btrfs_item_size(leaf, item);
2826 buf = kmalloc(item_size, GFP_NOFS);
2827 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
2828 path->slots[0]), item_size);
2829 slot = path->slots[0] + 1;
2830 leaf = path->nodes[0];
2832 nritems = btrfs_header_nritems(leaf);
2834 if (slot != nritems) {
2835 /* shift the items */
2836 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2837 btrfs_item_nr_offset(slot),
2838 (nritems - slot) * sizeof(struct btrfs_item));
2842 btrfs_cpu_key_to_disk(&disk_key, new_key);
2843 btrfs_set_item_key(leaf, &disk_key, slot);
2845 new_item = btrfs_item_nr(leaf, slot);
2847 btrfs_set_item_offset(leaf, new_item, orig_offset);
2848 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
2850 btrfs_set_item_offset(leaf, item,
2851 orig_offset + item_size - split_offset);
2852 btrfs_set_item_size(leaf, item, split_offset);
2854 btrfs_set_header_nritems(leaf, nritems + 1);
2856 /* write the data for the start of the original item */
2857 write_extent_buffer(leaf, buf,
2858 btrfs_item_ptr_offset(leaf, path->slots[0]),
2861 /* write the data for the new item */
2862 write_extent_buffer(leaf, buf + split_offset,
2863 btrfs_item_ptr_offset(leaf, slot),
2864 item_size - split_offset);
2865 btrfs_mark_buffer_dirty(leaf);
2868 if (btrfs_leaf_free_space(root, leaf) < 0) {
2869 btrfs_print_leaf(root, leaf);
2877 * make the item pointed to by the path smaller. new_size indicates
2878 * how small to make it, and from_end tells us if we just chop bytes
2879 * off the end of the item or if we shift the item to chop bytes off
2882 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2883 struct btrfs_root *root,
2884 struct btrfs_path *path,
2885 u32 new_size, int from_end)
2890 struct extent_buffer *leaf;
2891 struct btrfs_item *item;
2893 unsigned int data_end;
2894 unsigned int old_data_start;
2895 unsigned int old_size;
2896 unsigned int size_diff;
2899 slot_orig = path->slots[0];
2900 leaf = path->nodes[0];
2901 slot = path->slots[0];
2903 old_size = btrfs_item_size_nr(leaf, slot);
2904 if (old_size == new_size)
2907 nritems = btrfs_header_nritems(leaf);
2908 data_end = leaf_data_end(root, leaf);
2910 old_data_start = btrfs_item_offset_nr(leaf, slot);
2912 size_diff = old_size - new_size;
2915 BUG_ON(slot >= nritems);
2918 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2920 /* first correct the data pointers */
2921 for (i = slot; i < nritems; i++) {
2923 item = btrfs_item_nr(leaf, i);
2925 if (!leaf->map_token) {
2926 map_extent_buffer(leaf, (unsigned long)item,
2927 sizeof(struct btrfs_item),
2928 &leaf->map_token, &leaf->kaddr,
2929 &leaf->map_start, &leaf->map_len,
2933 ioff = btrfs_item_offset(leaf, item);
2934 btrfs_set_item_offset(leaf, item, ioff + size_diff);
2937 if (leaf->map_token) {
2938 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2939 leaf->map_token = NULL;
2942 /* shift the data */
2944 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2945 data_end + size_diff, btrfs_leaf_data(leaf) +
2946 data_end, old_data_start + new_size - data_end);
2948 struct btrfs_disk_key disk_key;
2951 btrfs_item_key(leaf, &disk_key, slot);
2953 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2955 struct btrfs_file_extent_item *fi;
2957 fi = btrfs_item_ptr(leaf, slot,
2958 struct btrfs_file_extent_item);
2959 fi = (struct btrfs_file_extent_item *)(
2960 (unsigned long)fi - size_diff);
2962 if (btrfs_file_extent_type(leaf, fi) ==
2963 BTRFS_FILE_EXTENT_INLINE) {
2964 ptr = btrfs_item_ptr_offset(leaf, slot);
2965 memmove_extent_buffer(leaf, ptr,
2967 offsetof(struct btrfs_file_extent_item,
2972 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2973 data_end + size_diff, btrfs_leaf_data(leaf) +
2974 data_end, old_data_start - data_end);
2976 offset = btrfs_disk_key_offset(&disk_key);
2977 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2978 btrfs_set_item_key(leaf, &disk_key, slot);
2980 fixup_low_keys(trans, root, path, &disk_key, 1);
2983 item = btrfs_item_nr(leaf, slot);
2984 btrfs_set_item_size(leaf, item, new_size);
2985 btrfs_mark_buffer_dirty(leaf);
2988 if (btrfs_leaf_free_space(root, leaf) < 0) {
2989 btrfs_print_leaf(root, leaf);
2996 * make the item pointed to by the path bigger, data_size is the new size.
2998 int btrfs_extend_item(struct btrfs_trans_handle *trans,
2999 struct btrfs_root *root, struct btrfs_path *path,
3005 struct extent_buffer *leaf;
3006 struct btrfs_item *item;
3008 unsigned int data_end;
3009 unsigned int old_data;
3010 unsigned int old_size;
3013 slot_orig = path->slots[0];
3014 leaf = path->nodes[0];
3016 nritems = btrfs_header_nritems(leaf);
3017 data_end = leaf_data_end(root, leaf);
3019 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3020 btrfs_print_leaf(root, leaf);
3023 slot = path->slots[0];
3024 old_data = btrfs_item_end_nr(leaf, slot);
3027 if (slot >= nritems) {
3028 btrfs_print_leaf(root, leaf);
3029 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3035 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3037 /* first correct the data pointers */
3038 for (i = slot; i < nritems; i++) {
3040 item = btrfs_item_nr(leaf, i);
3042 if (!leaf->map_token) {
3043 map_extent_buffer(leaf, (unsigned long)item,
3044 sizeof(struct btrfs_item),
3045 &leaf->map_token, &leaf->kaddr,
3046 &leaf->map_start, &leaf->map_len,
3049 ioff = btrfs_item_offset(leaf, item);
3050 btrfs_set_item_offset(leaf, item, ioff - data_size);
3053 if (leaf->map_token) {
3054 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3055 leaf->map_token = NULL;
3058 /* shift the data */
3059 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3060 data_end - data_size, btrfs_leaf_data(leaf) +
3061 data_end, old_data - data_end);
3063 data_end = old_data;
3064 old_size = btrfs_item_size_nr(leaf, slot);
3065 item = btrfs_item_nr(leaf, slot);
3066 btrfs_set_item_size(leaf, item, old_size + data_size);
3067 btrfs_mark_buffer_dirty(leaf);
3070 if (btrfs_leaf_free_space(root, leaf) < 0) {
3071 btrfs_print_leaf(root, leaf);
3078 * Given a key and some data, insert items into the tree.
3079 * This does all the path init required, making room in the tree if needed.
3080 * Returns the number of keys that were inserted.
3082 int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3083 struct btrfs_root *root,
3084 struct btrfs_path *path,
3085 struct btrfs_key *cpu_key, u32 *data_size,
3088 struct extent_buffer *leaf;
3089 struct btrfs_item *item;
3096 unsigned int data_end;
3097 struct btrfs_disk_key disk_key;
3098 struct btrfs_key found_key;
3100 for (i = 0; i < nr; i++) {
3101 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3102 BTRFS_LEAF_DATA_SIZE(root)) {
3106 total_data += data_size[i];
3107 total_size += data_size[i] + sizeof(struct btrfs_item);
3111 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3117 leaf = path->nodes[0];
3119 nritems = btrfs_header_nritems(leaf);
3120 data_end = leaf_data_end(root, leaf);
3122 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3123 for (i = nr; i >= 0; i--) {
3124 total_data -= data_size[i];
3125 total_size -= data_size[i] + sizeof(struct btrfs_item);
3126 if (total_size < btrfs_leaf_free_space(root, leaf))
3132 slot = path->slots[0];
3135 if (slot != nritems) {
3136 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3138 item = btrfs_item_nr(leaf, slot);
3139 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3141 /* figure out how many keys we can insert in here */
3142 total_data = data_size[0];
3143 for (i = 1; i < nr; i++) {
3144 if (comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3146 total_data += data_size[i];
3150 if (old_data < data_end) {
3151 btrfs_print_leaf(root, leaf);
3152 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3153 slot, old_data, data_end);
3157 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3159 /* first correct the data pointers */
3160 WARN_ON(leaf->map_token);
3161 for (i = slot; i < nritems; i++) {
3164 item = btrfs_item_nr(leaf, i);
3165 if (!leaf->map_token) {
3166 map_extent_buffer(leaf, (unsigned long)item,
3167 sizeof(struct btrfs_item),
3168 &leaf->map_token, &leaf->kaddr,
3169 &leaf->map_start, &leaf->map_len,
3173 ioff = btrfs_item_offset(leaf, item);
3174 btrfs_set_item_offset(leaf, item, ioff - total_data);
3176 if (leaf->map_token) {
3177 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3178 leaf->map_token = NULL;
3181 /* shift the items */
3182 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3183 btrfs_item_nr_offset(slot),
3184 (nritems - slot) * sizeof(struct btrfs_item));
3186 /* shift the data */
3187 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3188 data_end - total_data, btrfs_leaf_data(leaf) +
3189 data_end, old_data - data_end);
3190 data_end = old_data;
3193 * this sucks but it has to be done, if we are inserting at
3194 * the end of the leaf only insert 1 of the items, since we
3195 * have no way of knowing whats on the next leaf and we'd have
3196 * to drop our current locks to figure it out
3201 /* setup the item for the new data */
3202 for (i = 0; i < nr; i++) {
3203 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3204 btrfs_set_item_key(leaf, &disk_key, slot + i);
3205 item = btrfs_item_nr(leaf, slot + i);
3206 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3207 data_end -= data_size[i];
3208 btrfs_set_item_size(leaf, item, data_size[i]);
3210 btrfs_set_header_nritems(leaf, nritems + nr);
3211 btrfs_mark_buffer_dirty(leaf);
3215 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3216 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3219 if (btrfs_leaf_free_space(root, leaf) < 0) {
3220 btrfs_print_leaf(root, leaf);
3230 * Given a key and some data, insert items into the tree.
3231 * This does all the path init required, making room in the tree if needed.
3233 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3234 struct btrfs_root *root,
3235 struct btrfs_path *path,
3236 struct btrfs_key *cpu_key, u32 *data_size,
3239 struct extent_buffer *leaf;
3240 struct btrfs_item *item;
3248 unsigned int data_end;
3249 struct btrfs_disk_key disk_key;
3251 for (i = 0; i < nr; i++)
3252 total_data += data_size[i];
3254 total_size = total_data + (nr * sizeof(struct btrfs_item));
3255 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3261 slot_orig = path->slots[0];
3262 leaf = path->nodes[0];
3264 nritems = btrfs_header_nritems(leaf);
3265 data_end = leaf_data_end(root, leaf);
3267 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3268 btrfs_print_leaf(root, leaf);
3269 printk(KERN_CRIT "not enough freespace need %u have %d\n",
3270 total_size, btrfs_leaf_free_space(root, leaf));
3274 slot = path->slots[0];
3277 if (slot != nritems) {
3278 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3280 if (old_data < data_end) {
3281 btrfs_print_leaf(root, leaf);
3282 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3283 slot, old_data, data_end);
3287 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3289 /* first correct the data pointers */
3290 WARN_ON(leaf->map_token);
3291 for (i = slot; i < nritems; i++) {
3294 item = btrfs_item_nr(leaf, i);
3295 if (!leaf->map_token) {
3296 map_extent_buffer(leaf, (unsigned long)item,
3297 sizeof(struct btrfs_item),
3298 &leaf->map_token, &leaf->kaddr,
3299 &leaf->map_start, &leaf->map_len,
3303 ioff = btrfs_item_offset(leaf, item);
3304 btrfs_set_item_offset(leaf, item, ioff - total_data);
3306 if (leaf->map_token) {
3307 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3308 leaf->map_token = NULL;
3311 /* shift the items */
3312 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3313 btrfs_item_nr_offset(slot),
3314 (nritems - slot) * sizeof(struct btrfs_item));
3316 /* shift the data */
3317 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3318 data_end - total_data, btrfs_leaf_data(leaf) +
3319 data_end, old_data - data_end);
3320 data_end = old_data;
3323 /* setup the item for the new data */
3324 for (i = 0; i < nr; i++) {
3325 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3326 btrfs_set_item_key(leaf, &disk_key, slot + i);
3327 item = btrfs_item_nr(leaf, slot + i);
3328 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3329 data_end -= data_size[i];
3330 btrfs_set_item_size(leaf, item, data_size[i]);
3332 btrfs_set_header_nritems(leaf, nritems + nr);
3333 btrfs_mark_buffer_dirty(leaf);
3337 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3338 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3341 if (btrfs_leaf_free_space(root, leaf) < 0) {
3342 btrfs_print_leaf(root, leaf);
3350 * Given a key and some data, insert an item into the tree.
3351 * This does all the path init required, making room in the tree if needed.
3353 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3354 *root, struct btrfs_key *cpu_key, void *data, u32
3358 struct btrfs_path *path;
3359 struct extent_buffer *leaf;
3362 path = btrfs_alloc_path();
3364 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
3366 leaf = path->nodes[0];
3367 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3368 write_extent_buffer(leaf, data, ptr, data_size);
3369 btrfs_mark_buffer_dirty(leaf);
3371 btrfs_free_path(path);
3376 * delete the pointer from a given node.
3378 * the tree should have been previously balanced so the deletion does not
3381 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3382 struct btrfs_path *path, int level, int slot)
3384 struct extent_buffer *parent = path->nodes[level];
3389 nritems = btrfs_header_nritems(parent);
3390 if (slot != nritems - 1) {
3391 memmove_extent_buffer(parent,
3392 btrfs_node_key_ptr_offset(slot),
3393 btrfs_node_key_ptr_offset(slot + 1),
3394 sizeof(struct btrfs_key_ptr) *
3395 (nritems - slot - 1));
3398 btrfs_set_header_nritems(parent, nritems);
3399 if (nritems == 0 && parent == root->node) {
3400 BUG_ON(btrfs_header_level(root->node) != 1);
3401 /* just turn the root into a leaf and break */
3402 btrfs_set_header_level(root->node, 0);
3403 } else if (slot == 0) {
3404 struct btrfs_disk_key disk_key;
3406 btrfs_node_key(parent, &disk_key, 0);
3407 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
3411 btrfs_mark_buffer_dirty(parent);
3416 * a helper function to delete the leaf pointed to by path->slots[1] and
3417 * path->nodes[1]. bytenr is the node block pointer, but since the callers
3418 * already know it, it is faster to have them pass it down than to
3419 * read it out of the node again.
3421 * This deletes the pointer in path->nodes[1] and frees the leaf
3422 * block extent. zero is returned if it all worked out, < 0 otherwise.
3424 * The path must have already been setup for deleting the leaf, including
3425 * all the proper balancing. path->nodes[1] must be locked.
3427 noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3428 struct btrfs_root *root,
3429 struct btrfs_path *path, u64 bytenr)
3432 u64 root_gen = btrfs_header_generation(path->nodes[1]);
3434 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3438 ret = btrfs_free_extent(trans, root, bytenr,
3439 btrfs_level_size(root, 0),
3440 path->nodes[1]->start,
3441 btrfs_header_owner(path->nodes[1]),
3446 * delete the item at the leaf level in path. If that empties
3447 * the leaf, remove it from the tree
3449 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3450 struct btrfs_path *path, int slot, int nr)
3452 struct extent_buffer *leaf;
3453 struct btrfs_item *item;
3461 leaf = path->nodes[0];
3462 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3464 for (i = 0; i < nr; i++)
3465 dsize += btrfs_item_size_nr(leaf, slot + i);
3467 nritems = btrfs_header_nritems(leaf);
3469 if (slot + nr != nritems) {
3470 int data_end = leaf_data_end(root, leaf);
3472 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3474 btrfs_leaf_data(leaf) + data_end,
3475 last_off - data_end);
3477 for (i = slot + nr; i < nritems; i++) {
3480 item = btrfs_item_nr(leaf, i);
3481 if (!leaf->map_token) {
3482 map_extent_buffer(leaf, (unsigned long)item,
3483 sizeof(struct btrfs_item),
3484 &leaf->map_token, &leaf->kaddr,
3485 &leaf->map_start, &leaf->map_len,
3488 ioff = btrfs_item_offset(leaf, item);
3489 btrfs_set_item_offset(leaf, item, ioff + dsize);
3492 if (leaf->map_token) {
3493 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3494 leaf->map_token = NULL;
3497 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
3498 btrfs_item_nr_offset(slot + nr),
3499 sizeof(struct btrfs_item) *
3500 (nritems - slot - nr));
3502 btrfs_set_header_nritems(leaf, nritems - nr);
3505 /* delete the leaf if we've emptied it */
3507 if (leaf == root->node) {
3508 btrfs_set_header_level(leaf, 0);
3510 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3514 int used = leaf_space_used(leaf, 0, nritems);
3516 struct btrfs_disk_key disk_key;
3518 btrfs_item_key(leaf, &disk_key, 0);
3519 wret = fixup_low_keys(trans, root, path,
3525 /* delete the leaf if it is mostly empty */
3526 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
3527 /* push_leaf_left fixes the path.
3528 * make sure the path still points to our leaf
3529 * for possible call to del_ptr below
3531 slot = path->slots[1];
3532 extent_buffer_get(leaf);
3534 wret = push_leaf_left(trans, root, path, 1, 1);
3535 if (wret < 0 && wret != -ENOSPC)
3538 if (path->nodes[0] == leaf &&
3539 btrfs_header_nritems(leaf)) {
3540 wret = push_leaf_right(trans, root, path, 1, 1);
3541 if (wret < 0 && wret != -ENOSPC)
3545 if (btrfs_header_nritems(leaf) == 0) {
3546 path->slots[1] = slot;
3547 ret = btrfs_del_leaf(trans, root, path,
3550 free_extent_buffer(leaf);
3552 /* if we're still in the path, make sure
3553 * we're dirty. Otherwise, one of the
3554 * push_leaf functions must have already
3555 * dirtied this buffer
3557 if (path->nodes[0] == leaf)
3558 btrfs_mark_buffer_dirty(leaf);
3559 free_extent_buffer(leaf);
3562 btrfs_mark_buffer_dirty(leaf);
3569 * search the tree again to find a leaf with lesser keys
3570 * returns 0 if it found something or 1 if there are no lesser leaves.
3571 * returns < 0 on io errors.
3573 * This may release the path, and so you may lose any locks held at the
3576 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3578 struct btrfs_key key;
3579 struct btrfs_disk_key found_key;
3582 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
3586 else if (key.type > 0)
3588 else if (key.objectid > 0)
3593 btrfs_release_path(root, path);
3594 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3597 btrfs_item_key(path->nodes[0], &found_key, 0);
3598 ret = comp_keys(&found_key, &key);
3605 * A helper function to walk down the tree starting at min_key, and looking
3606 * for nodes or leaves that are either in cache or have a minimum
3607 * transaction id. This is used by the btree defrag code, and tree logging
3609 * This does not cow, but it does stuff the starting key it finds back
3610 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3611 * key and get a writable path.
3613 * This does lock as it descends, and path->keep_locks should be set
3614 * to 1 by the caller.
3616 * This honors path->lowest_level to prevent descent past a given level
3619 * min_trans indicates the oldest transaction that you are interested
3620 * in walking through. Any nodes or leaves older than min_trans are
3621 * skipped over (without reading them).
3623 * returns zero if something useful was found, < 0 on error and 1 if there
3624 * was nothing in the tree that matched the search criteria.
3626 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
3627 struct btrfs_key *max_key,
3628 struct btrfs_path *path, int cache_only,
3631 struct extent_buffer *cur;
3632 struct btrfs_key found_key;
3639 WARN_ON(!path->keep_locks);
3641 cur = btrfs_lock_root_node(root);
3642 level = btrfs_header_level(cur);
3643 WARN_ON(path->nodes[level]);
3644 path->nodes[level] = cur;
3645 path->locks[level] = 1;
3647 if (btrfs_header_generation(cur) < min_trans) {
3652 nritems = btrfs_header_nritems(cur);
3653 level = btrfs_header_level(cur);
3654 sret = bin_search(cur, min_key, level, &slot);
3656 /* at the lowest level, we're done, setup the path and exit */
3657 if (level == path->lowest_level) {
3658 if (slot >= nritems)
3661 path->slots[level] = slot;
3662 btrfs_item_key_to_cpu(cur, &found_key, slot);
3665 if (sret && slot > 0)
3668 * check this node pointer against the cache_only and
3669 * min_trans parameters. If it isn't in cache or is too
3670 * old, skip to the next one.
3672 while (slot < nritems) {
3675 struct extent_buffer *tmp;
3676 struct btrfs_disk_key disk_key;
3678 blockptr = btrfs_node_blockptr(cur, slot);
3679 gen = btrfs_node_ptr_generation(cur, slot);
3680 if (gen < min_trans) {
3688 btrfs_node_key(cur, &disk_key, slot);
3689 if (comp_keys(&disk_key, max_key) >= 0) {
3695 tmp = btrfs_find_tree_block(root, blockptr,
3696 btrfs_level_size(root, level - 1));
3698 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3699 free_extent_buffer(tmp);
3703 free_extent_buffer(tmp);
3708 * we didn't find a candidate key in this node, walk forward
3709 * and find another one
3711 if (slot >= nritems) {
3712 path->slots[level] = slot;
3713 sret = btrfs_find_next_key(root, path, min_key, level,
3714 cache_only, min_trans);
3716 btrfs_release_path(root, path);
3722 /* save our key for returning back */
3723 btrfs_node_key_to_cpu(cur, &found_key, slot);
3724 path->slots[level] = slot;
3725 if (level == path->lowest_level) {
3727 unlock_up(path, level, 1);
3730 cur = read_node_slot(root, cur, slot);
3732 btrfs_tree_lock(cur);
3733 path->locks[level - 1] = 1;
3734 path->nodes[level - 1] = cur;
3735 unlock_up(path, level, 1);
3739 memcpy(min_key, &found_key, sizeof(found_key));
3744 * this is similar to btrfs_next_leaf, but does not try to preserve
3745 * and fixup the path. It looks for and returns the next key in the
3746 * tree based on the current path and the cache_only and min_trans
3749 * 0 is returned if another key is found, < 0 if there are any errors
3750 * and 1 is returned if there are no higher keys in the tree
3752 * path->keep_locks should be set to 1 on the search made before
3753 * calling this function.
3755 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
3756 struct btrfs_key *key, int lowest_level,
3757 int cache_only, u64 min_trans)
3759 int level = lowest_level;
3761 struct extent_buffer *c;
3763 WARN_ON(!path->keep_locks);
3764 while (level < BTRFS_MAX_LEVEL) {
3765 if (!path->nodes[level])
3768 slot = path->slots[level] + 1;
3769 c = path->nodes[level];
3771 if (slot >= btrfs_header_nritems(c)) {
3773 if (level == BTRFS_MAX_LEVEL)
3778 btrfs_item_key_to_cpu(c, key, slot);
3780 u64 blockptr = btrfs_node_blockptr(c, slot);
3781 u64 gen = btrfs_node_ptr_generation(c, slot);
3784 struct extent_buffer *cur;
3785 cur = btrfs_find_tree_block(root, blockptr,
3786 btrfs_level_size(root, level - 1));
3787 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3790 free_extent_buffer(cur);
3793 free_extent_buffer(cur);
3795 if (gen < min_trans) {
3799 btrfs_node_key_to_cpu(c, key, slot);
3807 * search the tree again to find a leaf with greater keys
3808 * returns 0 if it found something or 1 if there are no greater leaves.
3809 * returns < 0 on io errors.
3811 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
3815 struct extent_buffer *c;
3816 struct extent_buffer *next = NULL;
3817 struct btrfs_key key;
3821 nritems = btrfs_header_nritems(path->nodes[0]);
3825 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3827 btrfs_release_path(root, path);
3828 path->keep_locks = 1;
3829 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3830 path->keep_locks = 0;
3835 nritems = btrfs_header_nritems(path->nodes[0]);
3837 * by releasing the path above we dropped all our locks. A balance
3838 * could have added more items next to the key that used to be
3839 * at the very end of the block. So, check again here and
3840 * advance the path if there are now more items available.
3842 if (nritems > 0 && path->slots[0] < nritems - 1) {
3847 while (level < BTRFS_MAX_LEVEL) {
3848 if (!path->nodes[level])
3851 slot = path->slots[level] + 1;
3852 c = path->nodes[level];
3853 if (slot >= btrfs_header_nritems(c)) {
3855 if (level == BTRFS_MAX_LEVEL)
3861 btrfs_tree_unlock(next);
3862 free_extent_buffer(next);
3865 if (level == 1 && (path->locks[1] || path->skip_locking) &&
3867 reada_for_search(root, path, level, slot, 0);
3869 next = read_node_slot(root, c, slot);
3870 if (!path->skip_locking) {
3871 WARN_ON(!btrfs_tree_locked(c));
3872 btrfs_tree_lock(next);
3876 path->slots[level] = slot;
3879 c = path->nodes[level];
3880 if (path->locks[level])
3881 btrfs_tree_unlock(c);
3882 free_extent_buffer(c);
3883 path->nodes[level] = next;
3884 path->slots[level] = 0;
3885 if (!path->skip_locking)
3886 path->locks[level] = 1;
3889 if (level == 1 && path->locks[1] && path->reada)
3890 reada_for_search(root, path, level, slot, 0);
3891 next = read_node_slot(root, next, 0);
3892 if (!path->skip_locking) {
3893 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3894 btrfs_tree_lock(next);
3898 unlock_up(path, 0, 1);
3903 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
3904 * searching until it gets past min_objectid or finds an item of 'type'
3906 * returns 0 if something is found, 1 if nothing was found and < 0 on error
3908 int btrfs_previous_item(struct btrfs_root *root,
3909 struct btrfs_path *path, u64 min_objectid,
3912 struct btrfs_key found_key;
3913 struct extent_buffer *leaf;
3918 if (path->slots[0] == 0) {
3919 ret = btrfs_prev_leaf(root, path);
3925 leaf = path->nodes[0];
3926 nritems = btrfs_header_nritems(leaf);
3929 if (path->slots[0] == nritems)
3932 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3933 if (found_key.type == type)
3935 if (found_key.objectid < min_objectid)
3937 if (found_key.objectid == min_objectid &&
3938 found_key.type < type)