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>
20 #include <linux/slab.h>
23 #include "transaction.h"
24 #include "print-tree.h"
27 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
28 *root, struct btrfs_path *path, int level);
29 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
30 *root, struct btrfs_key *ins_key,
31 struct btrfs_path *path, int data_size, int extend);
32 static int push_node_left(struct btrfs_trans_handle *trans,
33 struct btrfs_root *root, struct extent_buffer *dst,
34 struct extent_buffer *src, int empty);
35 static int balance_node_right(struct btrfs_trans_handle *trans,
36 struct btrfs_root *root,
37 struct extent_buffer *dst_buf,
38 struct extent_buffer *src_buf);
39 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
40 struct btrfs_path *path, int level, int slot);
41 static int setup_items_for_insert(struct btrfs_trans_handle *trans,
42 struct btrfs_root *root, struct btrfs_path *path,
43 struct btrfs_key *cpu_key, u32 *data_size,
44 u32 total_data, u32 total_size, int nr);
47 struct btrfs_path *btrfs_alloc_path(void)
49 struct btrfs_path *path;
50 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
57 * set all locked nodes in the path to blocking locks. This should
58 * be done before scheduling
60 noinline void btrfs_set_path_blocking(struct btrfs_path *p)
63 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
64 if (p->nodes[i] && p->locks[i])
65 btrfs_set_lock_blocking(p->nodes[i]);
70 * reset all the locked nodes in the patch to spinning locks.
72 * held is used to keep lockdep happy, when lockdep is enabled
73 * we set held to a blocking lock before we go around and
74 * retake all the spinlocks in the path. You can safely use NULL
77 noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
78 struct extent_buffer *held)
82 #ifdef CONFIG_DEBUG_LOCK_ALLOC
83 /* lockdep really cares that we take all of these spinlocks
84 * in the right order. If any of the locks in the path are not
85 * currently blocking, it is going to complain. So, make really
86 * really sure by forcing the path to blocking before we clear
90 btrfs_set_lock_blocking(held);
91 btrfs_set_path_blocking(p);
94 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
95 if (p->nodes[i] && p->locks[i])
96 btrfs_clear_lock_blocking(p->nodes[i]);
99 #ifdef CONFIG_DEBUG_LOCK_ALLOC
101 btrfs_clear_lock_blocking(held);
105 /* this also releases the path */
106 void btrfs_free_path(struct btrfs_path *p)
110 btrfs_release_path(NULL, p);
111 kmem_cache_free(btrfs_path_cachep, p);
115 * path release drops references on the extent buffers in the path
116 * and it drops any locks held by this path
118 * It is safe to call this on paths that no locks or extent buffers held.
120 noinline void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
124 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
129 btrfs_tree_unlock(p->nodes[i]);
132 free_extent_buffer(p->nodes[i]);
138 * safely gets a reference on the root node of a tree. A lock
139 * is not taken, so a concurrent writer may put a different node
140 * at the root of the tree. See btrfs_lock_root_node for the
143 * The extent buffer returned by this has a reference taken, so
144 * it won't disappear. It may stop being the root of the tree
145 * at any time because there are no locks held.
147 struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
149 struct extent_buffer *eb;
152 eb = rcu_dereference(root->node);
153 extent_buffer_get(eb);
158 /* loop around taking references on and locking the root node of the
159 * tree until you end up with a lock on the root. A locked buffer
160 * is returned, with a reference held.
162 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
164 struct extent_buffer *eb;
167 eb = btrfs_root_node(root);
169 if (eb == root->node)
171 btrfs_tree_unlock(eb);
172 free_extent_buffer(eb);
177 /* cowonly root (everything not a reference counted cow subvolume), just get
178 * put onto a simple dirty list. transaction.c walks this to make sure they
179 * get properly updated on disk.
181 static void add_root_to_dirty_list(struct btrfs_root *root)
183 if (root->track_dirty && list_empty(&root->dirty_list)) {
184 list_add(&root->dirty_list,
185 &root->fs_info->dirty_cowonly_roots);
190 * used by snapshot creation to make a copy of a root for a tree with
191 * a given objectid. The buffer with the new root node is returned in
192 * cow_ret, and this func returns zero on success or a negative error code.
194 int btrfs_copy_root(struct btrfs_trans_handle *trans,
195 struct btrfs_root *root,
196 struct extent_buffer *buf,
197 struct extent_buffer **cow_ret, u64 new_root_objectid)
199 struct extent_buffer *cow;
202 struct btrfs_disk_key disk_key;
204 WARN_ON(root->ref_cows && trans->transid !=
205 root->fs_info->running_transaction->transid);
206 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
208 level = btrfs_header_level(buf);
210 btrfs_item_key(buf, &disk_key, 0);
212 btrfs_node_key(buf, &disk_key, 0);
214 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
215 new_root_objectid, &disk_key, level,
220 copy_extent_buffer(cow, buf, 0, 0, cow->len);
221 btrfs_set_header_bytenr(cow, cow->start);
222 btrfs_set_header_generation(cow, trans->transid);
223 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
224 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
225 BTRFS_HEADER_FLAG_RELOC);
226 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
227 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
229 btrfs_set_header_owner(cow, new_root_objectid);
231 write_extent_buffer(cow, root->fs_info->fsid,
232 (unsigned long)btrfs_header_fsid(cow),
235 WARN_ON(btrfs_header_generation(buf) > trans->transid);
236 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
237 ret = btrfs_inc_ref(trans, root, cow, 1);
239 ret = btrfs_inc_ref(trans, root, cow, 0);
244 btrfs_mark_buffer_dirty(cow);
250 * check if the tree block can be shared by multiple trees
252 int btrfs_block_can_be_shared(struct btrfs_root *root,
253 struct extent_buffer *buf)
256 * Tree blocks not in refernece counted trees and tree roots
257 * are never shared. If a block was allocated after the last
258 * snapshot and the block was not allocated by tree relocation,
259 * we know the block is not shared.
261 if (root->ref_cows &&
262 buf != root->node && buf != root->commit_root &&
263 (btrfs_header_generation(buf) <=
264 btrfs_root_last_snapshot(&root->root_item) ||
265 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
267 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
268 if (root->ref_cows &&
269 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
275 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
276 struct btrfs_root *root,
277 struct extent_buffer *buf,
278 struct extent_buffer *cow,
288 * Backrefs update rules:
290 * Always use full backrefs for extent pointers in tree block
291 * allocated by tree relocation.
293 * If a shared tree block is no longer referenced by its owner
294 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
295 * use full backrefs for extent pointers in tree block.
297 * If a tree block is been relocating
298 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
299 * use full backrefs for extent pointers in tree block.
300 * The reason for this is some operations (such as drop tree)
301 * are only allowed for blocks use full backrefs.
304 if (btrfs_block_can_be_shared(root, buf)) {
305 ret = btrfs_lookup_extent_info(trans, root, buf->start,
306 buf->len, &refs, &flags);
311 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
312 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
313 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
318 owner = btrfs_header_owner(buf);
319 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
320 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
323 if ((owner == root->root_key.objectid ||
324 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
325 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
326 ret = btrfs_inc_ref(trans, root, buf, 1);
329 if (root->root_key.objectid ==
330 BTRFS_TREE_RELOC_OBJECTID) {
331 ret = btrfs_dec_ref(trans, root, buf, 0);
333 ret = btrfs_inc_ref(trans, root, cow, 1);
336 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
339 if (root->root_key.objectid ==
340 BTRFS_TREE_RELOC_OBJECTID)
341 ret = btrfs_inc_ref(trans, root, cow, 1);
343 ret = btrfs_inc_ref(trans, root, cow, 0);
346 if (new_flags != 0) {
347 ret = btrfs_set_disk_extent_flags(trans, root,
354 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
355 if (root->root_key.objectid ==
356 BTRFS_TREE_RELOC_OBJECTID)
357 ret = btrfs_inc_ref(trans, root, cow, 1);
359 ret = btrfs_inc_ref(trans, root, cow, 0);
361 ret = btrfs_dec_ref(trans, root, buf, 1);
364 clean_tree_block(trans, root, buf);
371 * does the dirty work in cow of a single block. The parent block (if
372 * supplied) is updated to point to the new cow copy. The new buffer is marked
373 * dirty and returned locked. If you modify the block it needs to be marked
376 * search_start -- an allocation hint for the new block
378 * empty_size -- a hint that you plan on doing more cow. This is the size in
379 * bytes the allocator should try to find free next to the block it returns.
380 * This is just a hint and may be ignored by the allocator.
382 static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
383 struct btrfs_root *root,
384 struct extent_buffer *buf,
385 struct extent_buffer *parent, int parent_slot,
386 struct extent_buffer **cow_ret,
387 u64 search_start, u64 empty_size)
389 struct btrfs_disk_key disk_key;
390 struct extent_buffer *cow;
399 btrfs_assert_tree_locked(buf);
401 WARN_ON(root->ref_cows && trans->transid !=
402 root->fs_info->running_transaction->transid);
403 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
405 level = btrfs_header_level(buf);
408 btrfs_item_key(buf, &disk_key, 0);
410 btrfs_node_key(buf, &disk_key, 0);
412 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
414 parent_start = parent->start;
420 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
421 root->root_key.objectid, &disk_key,
422 level, search_start, empty_size);
426 /* cow is set to blocking by btrfs_init_new_buffer */
428 copy_extent_buffer(cow, buf, 0, 0, cow->len);
429 btrfs_set_header_bytenr(cow, cow->start);
430 btrfs_set_header_generation(cow, trans->transid);
431 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
432 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
433 BTRFS_HEADER_FLAG_RELOC);
434 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
435 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
437 btrfs_set_header_owner(cow, root->root_key.objectid);
439 write_extent_buffer(cow, root->fs_info->fsid,
440 (unsigned long)btrfs_header_fsid(cow),
443 update_ref_for_cow(trans, root, buf, cow, &last_ref);
446 btrfs_reloc_cow_block(trans, root, buf, cow);
448 if (buf == root->node) {
449 WARN_ON(parent && parent != buf);
450 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
451 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
452 parent_start = buf->start;
456 extent_buffer_get(cow);
457 rcu_assign_pointer(root->node, cow);
459 btrfs_free_tree_block(trans, root, buf, parent_start,
461 free_extent_buffer(buf);
462 add_root_to_dirty_list(root);
464 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
465 parent_start = parent->start;
469 WARN_ON(trans->transid != btrfs_header_generation(parent));
470 btrfs_set_node_blockptr(parent, parent_slot,
472 btrfs_set_node_ptr_generation(parent, parent_slot,
474 btrfs_mark_buffer_dirty(parent);
475 btrfs_free_tree_block(trans, root, buf, parent_start,
479 btrfs_tree_unlock(buf);
480 free_extent_buffer(buf);
481 btrfs_mark_buffer_dirty(cow);
486 static inline int should_cow_block(struct btrfs_trans_handle *trans,
487 struct btrfs_root *root,
488 struct extent_buffer *buf)
490 if (btrfs_header_generation(buf) == trans->transid &&
491 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
492 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
493 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
499 * cows a single block, see __btrfs_cow_block for the real work.
500 * This version of it has extra checks so that a block isn't cow'd more than
501 * once per transaction, as long as it hasn't been written yet
503 noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
504 struct btrfs_root *root, struct extent_buffer *buf,
505 struct extent_buffer *parent, int parent_slot,
506 struct extent_buffer **cow_ret)
511 if (trans->transaction != root->fs_info->running_transaction) {
512 printk(KERN_CRIT "trans %llu running %llu\n",
513 (unsigned long long)trans->transid,
515 root->fs_info->running_transaction->transid);
518 if (trans->transid != root->fs_info->generation) {
519 printk(KERN_CRIT "trans %llu running %llu\n",
520 (unsigned long long)trans->transid,
521 (unsigned long long)root->fs_info->generation);
525 if (!should_cow_block(trans, root, buf)) {
530 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
533 btrfs_set_lock_blocking(parent);
534 btrfs_set_lock_blocking(buf);
536 ret = __btrfs_cow_block(trans, root, buf, parent,
537 parent_slot, cow_ret, search_start, 0);
539 trace_btrfs_cow_block(root, buf, *cow_ret);
545 * helper function for defrag to decide if two blocks pointed to by a
546 * node are actually close by
548 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
550 if (blocknr < other && other - (blocknr + blocksize) < 32768)
552 if (blocknr > other && blocknr - (other + blocksize) < 32768)
558 * compare two keys in a memcmp fashion
560 static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
564 btrfs_disk_key_to_cpu(&k1, disk);
566 return btrfs_comp_cpu_keys(&k1, k2);
570 * same as comp_keys only with two btrfs_key's
572 int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
574 if (k1->objectid > k2->objectid)
576 if (k1->objectid < k2->objectid)
578 if (k1->type > k2->type)
580 if (k1->type < k2->type)
582 if (k1->offset > k2->offset)
584 if (k1->offset < k2->offset)
590 * this is used by the defrag code to go through all the
591 * leaves pointed to by a node and reallocate them so that
592 * disk order is close to key order
594 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
595 struct btrfs_root *root, struct extent_buffer *parent,
596 int start_slot, int cache_only, u64 *last_ret,
597 struct btrfs_key *progress)
599 struct extent_buffer *cur;
602 u64 search_start = *last_ret;
612 int progress_passed = 0;
613 struct btrfs_disk_key disk_key;
615 parent_level = btrfs_header_level(parent);
616 if (cache_only && parent_level != 1)
619 if (trans->transaction != root->fs_info->running_transaction)
621 if (trans->transid != root->fs_info->generation)
624 parent_nritems = btrfs_header_nritems(parent);
625 blocksize = btrfs_level_size(root, parent_level - 1);
626 end_slot = parent_nritems;
628 if (parent_nritems == 1)
631 btrfs_set_lock_blocking(parent);
633 for (i = start_slot; i < end_slot; i++) {
636 if (!parent->map_token) {
637 map_extent_buffer(parent,
638 btrfs_node_key_ptr_offset(i),
639 sizeof(struct btrfs_key_ptr),
640 &parent->map_token, &parent->kaddr,
641 &parent->map_start, &parent->map_len,
644 btrfs_node_key(parent, &disk_key, i);
645 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
649 blocknr = btrfs_node_blockptr(parent, i);
650 gen = btrfs_node_ptr_generation(parent, i);
652 last_block = blocknr;
655 other = btrfs_node_blockptr(parent, i - 1);
656 close = close_blocks(blocknr, other, blocksize);
658 if (!close && i < end_slot - 2) {
659 other = btrfs_node_blockptr(parent, i + 1);
660 close = close_blocks(blocknr, other, blocksize);
663 last_block = blocknr;
666 if (parent->map_token) {
667 unmap_extent_buffer(parent, parent->map_token,
669 parent->map_token = NULL;
672 cur = btrfs_find_tree_block(root, blocknr, blocksize);
674 uptodate = btrfs_buffer_uptodate(cur, gen);
677 if (!cur || !uptodate) {
679 free_extent_buffer(cur);
683 cur = read_tree_block(root, blocknr,
685 } else if (!uptodate) {
686 btrfs_read_buffer(cur, gen);
689 if (search_start == 0)
690 search_start = last_block;
692 btrfs_tree_lock(cur);
693 btrfs_set_lock_blocking(cur);
694 err = __btrfs_cow_block(trans, root, cur, parent, i,
697 (end_slot - i) * blocksize));
699 btrfs_tree_unlock(cur);
700 free_extent_buffer(cur);
703 search_start = cur->start;
704 last_block = cur->start;
705 *last_ret = search_start;
706 btrfs_tree_unlock(cur);
707 free_extent_buffer(cur);
709 if (parent->map_token) {
710 unmap_extent_buffer(parent, parent->map_token,
712 parent->map_token = NULL;
718 * The leaf data grows from end-to-front in the node.
719 * this returns the address of the start of the last item,
720 * which is the stop of the leaf data stack
722 static inline unsigned int leaf_data_end(struct btrfs_root *root,
723 struct extent_buffer *leaf)
725 u32 nr = btrfs_header_nritems(leaf);
727 return BTRFS_LEAF_DATA_SIZE(root);
728 return btrfs_item_offset_nr(leaf, nr - 1);
733 * search for key in the extent_buffer. The items start at offset p,
734 * and they are item_size apart. There are 'max' items in p.
736 * the slot in the array is returned via slot, and it points to
737 * the place where you would insert key if it is not found in
740 * slot may point to max if the key is bigger than all of the keys
742 static noinline int generic_bin_search(struct extent_buffer *eb,
744 int item_size, struct btrfs_key *key,
751 struct btrfs_disk_key *tmp = NULL;
752 struct btrfs_disk_key unaligned;
753 unsigned long offset;
754 char *map_token = NULL;
756 unsigned long map_start = 0;
757 unsigned long map_len = 0;
761 mid = (low + high) / 2;
762 offset = p + mid * item_size;
764 if (!map_token || offset < map_start ||
765 (offset + sizeof(struct btrfs_disk_key)) >
766 map_start + map_len) {
768 unmap_extent_buffer(eb, map_token, KM_USER0);
772 err = map_private_extent_buffer(eb, offset,
773 sizeof(struct btrfs_disk_key),
775 &map_start, &map_len, KM_USER0);
778 tmp = (struct btrfs_disk_key *)(kaddr + offset -
781 read_extent_buffer(eb, &unaligned,
782 offset, sizeof(unaligned));
787 tmp = (struct btrfs_disk_key *)(kaddr + offset -
790 ret = comp_keys(tmp, key);
799 unmap_extent_buffer(eb, map_token, KM_USER0);
805 unmap_extent_buffer(eb, map_token, KM_USER0);
810 * simple bin_search frontend that does the right thing for
813 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
814 int level, int *slot)
817 return generic_bin_search(eb,
818 offsetof(struct btrfs_leaf, items),
819 sizeof(struct btrfs_item),
820 key, btrfs_header_nritems(eb),
823 return generic_bin_search(eb,
824 offsetof(struct btrfs_node, ptrs),
825 sizeof(struct btrfs_key_ptr),
826 key, btrfs_header_nritems(eb),
832 int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
833 int level, int *slot)
835 return bin_search(eb, key, level, slot);
838 static void root_add_used(struct btrfs_root *root, u32 size)
840 spin_lock(&root->accounting_lock);
841 btrfs_set_root_used(&root->root_item,
842 btrfs_root_used(&root->root_item) + size);
843 spin_unlock(&root->accounting_lock);
846 static void root_sub_used(struct btrfs_root *root, u32 size)
848 spin_lock(&root->accounting_lock);
849 btrfs_set_root_used(&root->root_item,
850 btrfs_root_used(&root->root_item) - size);
851 spin_unlock(&root->accounting_lock);
854 /* given a node and slot number, this reads the blocks it points to. The
855 * extent buffer is returned with a reference taken (but unlocked).
856 * NULL is returned on error.
858 static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
859 struct extent_buffer *parent, int slot)
861 int level = btrfs_header_level(parent);
864 if (slot >= btrfs_header_nritems(parent))
869 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
870 btrfs_level_size(root, level - 1),
871 btrfs_node_ptr_generation(parent, slot));
875 * node level balancing, used to make sure nodes are in proper order for
876 * item deletion. We balance from the top down, so we have to make sure
877 * that a deletion won't leave an node completely empty later on.
879 static noinline int balance_level(struct btrfs_trans_handle *trans,
880 struct btrfs_root *root,
881 struct btrfs_path *path, int level)
883 struct extent_buffer *right = NULL;
884 struct extent_buffer *mid;
885 struct extent_buffer *left = NULL;
886 struct extent_buffer *parent = NULL;
890 int orig_slot = path->slots[level];
896 mid = path->nodes[level];
898 WARN_ON(!path->locks[level]);
899 WARN_ON(btrfs_header_generation(mid) != trans->transid);
901 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
903 if (level < BTRFS_MAX_LEVEL - 1)
904 parent = path->nodes[level + 1];
905 pslot = path->slots[level + 1];
908 * deal with the case where there is only one pointer in the root
909 * by promoting the node below to a root
912 struct extent_buffer *child;
914 if (btrfs_header_nritems(mid) != 1)
917 /* promote the child to a root */
918 child = read_node_slot(root, mid, 0);
920 btrfs_tree_lock(child);
921 btrfs_set_lock_blocking(child);
922 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
924 btrfs_tree_unlock(child);
925 free_extent_buffer(child);
929 rcu_assign_pointer(root->node, child);
931 add_root_to_dirty_list(root);
932 btrfs_tree_unlock(child);
934 path->locks[level] = 0;
935 path->nodes[level] = NULL;
936 clean_tree_block(trans, root, mid);
937 btrfs_tree_unlock(mid);
938 /* once for the path */
939 free_extent_buffer(mid);
941 root_sub_used(root, mid->len);
942 btrfs_free_tree_block(trans, root, mid, 0, 1);
943 /* once for the root ptr */
944 free_extent_buffer(mid);
947 if (btrfs_header_nritems(mid) >
948 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
951 btrfs_header_nritems(mid);
953 left = read_node_slot(root, parent, pslot - 1);
955 btrfs_tree_lock(left);
956 btrfs_set_lock_blocking(left);
957 wret = btrfs_cow_block(trans, root, left,
958 parent, pslot - 1, &left);
964 right = read_node_slot(root, parent, pslot + 1);
966 btrfs_tree_lock(right);
967 btrfs_set_lock_blocking(right);
968 wret = btrfs_cow_block(trans, root, right,
969 parent, pslot + 1, &right);
976 /* first, try to make some room in the middle buffer */
978 orig_slot += btrfs_header_nritems(left);
979 wret = push_node_left(trans, root, left, mid, 1);
982 btrfs_header_nritems(mid);
986 * then try to empty the right most buffer into the middle
989 wret = push_node_left(trans, root, mid, right, 1);
990 if (wret < 0 && wret != -ENOSPC)
992 if (btrfs_header_nritems(right) == 0) {
993 clean_tree_block(trans, root, right);
994 btrfs_tree_unlock(right);
995 wret = del_ptr(trans, root, path, level + 1, pslot +
999 root_sub_used(root, right->len);
1000 btrfs_free_tree_block(trans, root, right, 0, 1);
1001 free_extent_buffer(right);
1004 struct btrfs_disk_key right_key;
1005 btrfs_node_key(right, &right_key, 0);
1006 btrfs_set_node_key(parent, &right_key, pslot + 1);
1007 btrfs_mark_buffer_dirty(parent);
1010 if (btrfs_header_nritems(mid) == 1) {
1012 * we're not allowed to leave a node with one item in the
1013 * tree during a delete. A deletion from lower in the tree
1014 * could try to delete the only pointer in this node.
1015 * So, pull some keys from the left.
1016 * There has to be a left pointer at this point because
1017 * otherwise we would have pulled some pointers from the
1021 wret = balance_node_right(trans, root, mid, left);
1027 wret = push_node_left(trans, root, left, mid, 1);
1033 if (btrfs_header_nritems(mid) == 0) {
1034 clean_tree_block(trans, root, mid);
1035 btrfs_tree_unlock(mid);
1036 wret = del_ptr(trans, root, path, level + 1, pslot);
1039 root_sub_used(root, mid->len);
1040 btrfs_free_tree_block(trans, root, mid, 0, 1);
1041 free_extent_buffer(mid);
1044 /* update the parent key to reflect our changes */
1045 struct btrfs_disk_key mid_key;
1046 btrfs_node_key(mid, &mid_key, 0);
1047 btrfs_set_node_key(parent, &mid_key, pslot);
1048 btrfs_mark_buffer_dirty(parent);
1051 /* update the path */
1053 if (btrfs_header_nritems(left) > orig_slot) {
1054 extent_buffer_get(left);
1055 /* left was locked after cow */
1056 path->nodes[level] = left;
1057 path->slots[level + 1] -= 1;
1058 path->slots[level] = orig_slot;
1060 btrfs_tree_unlock(mid);
1061 free_extent_buffer(mid);
1064 orig_slot -= btrfs_header_nritems(left);
1065 path->slots[level] = orig_slot;
1068 /* double check we haven't messed things up */
1070 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
1074 btrfs_tree_unlock(right);
1075 free_extent_buffer(right);
1078 if (path->nodes[level] != left)
1079 btrfs_tree_unlock(left);
1080 free_extent_buffer(left);
1085 /* Node balancing for insertion. Here we only split or push nodes around
1086 * when they are completely full. This is also done top down, so we
1087 * have to be pessimistic.
1089 static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1090 struct btrfs_root *root,
1091 struct btrfs_path *path, int level)
1093 struct extent_buffer *right = NULL;
1094 struct extent_buffer *mid;
1095 struct extent_buffer *left = NULL;
1096 struct extent_buffer *parent = NULL;
1100 int orig_slot = path->slots[level];
1105 mid = path->nodes[level];
1106 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1108 if (level < BTRFS_MAX_LEVEL - 1)
1109 parent = path->nodes[level + 1];
1110 pslot = path->slots[level + 1];
1115 left = read_node_slot(root, parent, pslot - 1);
1117 /* first, try to make some room in the middle buffer */
1121 btrfs_tree_lock(left);
1122 btrfs_set_lock_blocking(left);
1124 left_nr = btrfs_header_nritems(left);
1125 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1128 ret = btrfs_cow_block(trans, root, left, parent,
1133 wret = push_node_left(trans, root,
1140 struct btrfs_disk_key disk_key;
1141 orig_slot += left_nr;
1142 btrfs_node_key(mid, &disk_key, 0);
1143 btrfs_set_node_key(parent, &disk_key, pslot);
1144 btrfs_mark_buffer_dirty(parent);
1145 if (btrfs_header_nritems(left) > orig_slot) {
1146 path->nodes[level] = left;
1147 path->slots[level + 1] -= 1;
1148 path->slots[level] = orig_slot;
1149 btrfs_tree_unlock(mid);
1150 free_extent_buffer(mid);
1153 btrfs_header_nritems(left);
1154 path->slots[level] = orig_slot;
1155 btrfs_tree_unlock(left);
1156 free_extent_buffer(left);
1160 btrfs_tree_unlock(left);
1161 free_extent_buffer(left);
1163 right = read_node_slot(root, parent, pslot + 1);
1166 * then try to empty the right most buffer into the middle
1171 btrfs_tree_lock(right);
1172 btrfs_set_lock_blocking(right);
1174 right_nr = btrfs_header_nritems(right);
1175 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1178 ret = btrfs_cow_block(trans, root, right,
1184 wret = balance_node_right(trans, root,
1191 struct btrfs_disk_key disk_key;
1193 btrfs_node_key(right, &disk_key, 0);
1194 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1195 btrfs_mark_buffer_dirty(parent);
1197 if (btrfs_header_nritems(mid) <= orig_slot) {
1198 path->nodes[level] = right;
1199 path->slots[level + 1] += 1;
1200 path->slots[level] = orig_slot -
1201 btrfs_header_nritems(mid);
1202 btrfs_tree_unlock(mid);
1203 free_extent_buffer(mid);
1205 btrfs_tree_unlock(right);
1206 free_extent_buffer(right);
1210 btrfs_tree_unlock(right);
1211 free_extent_buffer(right);
1217 * readahead one full node of leaves, finding things that are close
1218 * to the block in 'slot', and triggering ra on them.
1220 static void reada_for_search(struct btrfs_root *root,
1221 struct btrfs_path *path,
1222 int level, int slot, u64 objectid)
1224 struct extent_buffer *node;
1225 struct btrfs_disk_key disk_key;
1230 int direction = path->reada;
1231 struct extent_buffer *eb;
1239 if (!path->nodes[level])
1242 node = path->nodes[level];
1244 search = btrfs_node_blockptr(node, slot);
1245 blocksize = btrfs_level_size(root, level - 1);
1246 eb = btrfs_find_tree_block(root, search, blocksize);
1248 free_extent_buffer(eb);
1254 nritems = btrfs_header_nritems(node);
1257 if (direction < 0) {
1261 } else if (direction > 0) {
1266 if (path->reada < 0 && objectid) {
1267 btrfs_node_key(node, &disk_key, nr);
1268 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1271 search = btrfs_node_blockptr(node, nr);
1272 if ((search <= target && target - search <= 65536) ||
1273 (search > target && search - target <= 65536)) {
1274 readahead_tree_block(root, search, blocksize,
1275 btrfs_node_ptr_generation(node, nr));
1279 if ((nread > 65536 || nscan > 32))
1285 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1288 static noinline int reada_for_balance(struct btrfs_root *root,
1289 struct btrfs_path *path, int level)
1293 struct extent_buffer *parent;
1294 struct extent_buffer *eb;
1301 parent = path->nodes[level + 1];
1305 nritems = btrfs_header_nritems(parent);
1306 slot = path->slots[level + 1];
1307 blocksize = btrfs_level_size(root, level);
1310 block1 = btrfs_node_blockptr(parent, slot - 1);
1311 gen = btrfs_node_ptr_generation(parent, slot - 1);
1312 eb = btrfs_find_tree_block(root, block1, blocksize);
1313 if (eb && btrfs_buffer_uptodate(eb, gen))
1315 free_extent_buffer(eb);
1317 if (slot + 1 < nritems) {
1318 block2 = btrfs_node_blockptr(parent, slot + 1);
1319 gen = btrfs_node_ptr_generation(parent, slot + 1);
1320 eb = btrfs_find_tree_block(root, block2, blocksize);
1321 if (eb && btrfs_buffer_uptodate(eb, gen))
1323 free_extent_buffer(eb);
1325 if (block1 || block2) {
1328 /* release the whole path */
1329 btrfs_release_path(root, path);
1331 /* read the blocks */
1333 readahead_tree_block(root, block1, blocksize, 0);
1335 readahead_tree_block(root, block2, blocksize, 0);
1338 eb = read_tree_block(root, block1, blocksize, 0);
1339 free_extent_buffer(eb);
1342 eb = read_tree_block(root, block2, blocksize, 0);
1343 free_extent_buffer(eb);
1351 * when we walk down the tree, it is usually safe to unlock the higher layers
1352 * in the tree. The exceptions are when our path goes through slot 0, because
1353 * operations on the tree might require changing key pointers higher up in the
1356 * callers might also have set path->keep_locks, which tells this code to keep
1357 * the lock if the path points to the last slot in the block. This is part of
1358 * walking through the tree, and selecting the next slot in the higher block.
1360 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1361 * if lowest_unlock is 1, level 0 won't be unlocked
1363 static noinline void unlock_up(struct btrfs_path *path, int level,
1367 int skip_level = level;
1369 struct extent_buffer *t;
1371 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1372 if (!path->nodes[i])
1374 if (!path->locks[i])
1376 if (!no_skips && path->slots[i] == 0) {
1380 if (!no_skips && path->keep_locks) {
1383 nritems = btrfs_header_nritems(t);
1384 if (nritems < 1 || path->slots[i] >= nritems - 1) {
1389 if (skip_level < i && i >= lowest_unlock)
1393 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1394 btrfs_tree_unlock(t);
1401 * This releases any locks held in the path starting at level and
1402 * going all the way up to the root.
1404 * btrfs_search_slot will keep the lock held on higher nodes in a few
1405 * corner cases, such as COW of the block at slot zero in the node. This
1406 * ignores those rules, and it should only be called when there are no
1407 * more updates to be done higher up in the tree.
1409 noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1413 if (path->keep_locks)
1416 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1417 if (!path->nodes[i])
1419 if (!path->locks[i])
1421 btrfs_tree_unlock(path->nodes[i]);
1427 * helper function for btrfs_search_slot. The goal is to find a block
1428 * in cache without setting the path to blocking. If we find the block
1429 * we return zero and the path is unchanged.
1431 * If we can't find the block, we set the path blocking and do some
1432 * reada. -EAGAIN is returned and the search must be repeated.
1435 read_block_for_search(struct btrfs_trans_handle *trans,
1436 struct btrfs_root *root, struct btrfs_path *p,
1437 struct extent_buffer **eb_ret, int level, int slot,
1438 struct btrfs_key *key)
1443 struct extent_buffer *b = *eb_ret;
1444 struct extent_buffer *tmp;
1447 blocknr = btrfs_node_blockptr(b, slot);
1448 gen = btrfs_node_ptr_generation(b, slot);
1449 blocksize = btrfs_level_size(root, level - 1);
1451 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1453 if (btrfs_buffer_uptodate(tmp, 0)) {
1454 if (btrfs_buffer_uptodate(tmp, gen)) {
1456 * we found an up to date block without
1463 /* the pages were up to date, but we failed
1464 * the generation number check. Do a full
1465 * read for the generation number that is correct.
1466 * We must do this without dropping locks so
1467 * we can trust our generation number
1469 free_extent_buffer(tmp);
1470 tmp = read_tree_block(root, blocknr, blocksize, gen);
1471 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1475 free_extent_buffer(tmp);
1476 btrfs_release_path(NULL, p);
1482 * reduce lock contention at high levels
1483 * of the btree by dropping locks before
1484 * we read. Don't release the lock on the current
1485 * level because we need to walk this node to figure
1486 * out which blocks to read.
1488 btrfs_unlock_up_safe(p, level + 1);
1489 btrfs_set_path_blocking(p);
1491 free_extent_buffer(tmp);
1493 reada_for_search(root, p, level, slot, key->objectid);
1495 btrfs_release_path(NULL, p);
1498 tmp = read_tree_block(root, blocknr, blocksize, 0);
1501 * If the read above didn't mark this buffer up to date,
1502 * it will never end up being up to date. Set ret to EIO now
1503 * and give up so that our caller doesn't loop forever
1506 if (!btrfs_buffer_uptodate(tmp, 0))
1508 free_extent_buffer(tmp);
1514 * helper function for btrfs_search_slot. This does all of the checks
1515 * for node-level blocks and does any balancing required based on
1518 * If no extra work was required, zero is returned. If we had to
1519 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1523 setup_nodes_for_search(struct btrfs_trans_handle *trans,
1524 struct btrfs_root *root, struct btrfs_path *p,
1525 struct extent_buffer *b, int level, int ins_len)
1528 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1529 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1532 sret = reada_for_balance(root, p, level);
1536 btrfs_set_path_blocking(p);
1537 sret = split_node(trans, root, p, level);
1538 btrfs_clear_path_blocking(p, NULL);
1545 b = p->nodes[level];
1546 } else if (ins_len < 0 && btrfs_header_nritems(b) <
1547 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
1550 sret = reada_for_balance(root, p, level);
1554 btrfs_set_path_blocking(p);
1555 sret = balance_level(trans, root, p, level);
1556 btrfs_clear_path_blocking(p, NULL);
1562 b = p->nodes[level];
1564 btrfs_release_path(NULL, p);
1567 BUG_ON(btrfs_header_nritems(b) == 1);
1578 * look for key in the tree. path is filled in with nodes along the way
1579 * if key is found, we return zero and you can find the item in the leaf
1580 * level of the path (level 0)
1582 * If the key isn't found, the path points to the slot where it should
1583 * be inserted, and 1 is returned. If there are other errors during the
1584 * search a negative error number is returned.
1586 * if ins_len > 0, nodes and leaves will be split as we walk down the
1587 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1590 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1591 *root, struct btrfs_key *key, struct btrfs_path *p, int
1594 struct extent_buffer *b;
1599 int lowest_unlock = 1;
1600 u8 lowest_level = 0;
1602 lowest_level = p->lowest_level;
1603 WARN_ON(lowest_level && ins_len > 0);
1604 WARN_ON(p->nodes[0] != NULL);
1610 if (p->search_commit_root) {
1611 b = root->commit_root;
1612 extent_buffer_get(b);
1613 if (!p->skip_locking)
1616 if (p->skip_locking)
1617 b = btrfs_root_node(root);
1619 b = btrfs_lock_root_node(root);
1623 level = btrfs_header_level(b);
1626 * setup the path here so we can release it under lock
1627 * contention with the cow code
1629 p->nodes[level] = b;
1630 if (!p->skip_locking)
1631 p->locks[level] = 1;
1635 * if we don't really need to cow this block
1636 * then we don't want to set the path blocking,
1637 * so we test it here
1639 if (!should_cow_block(trans, root, b))
1642 btrfs_set_path_blocking(p);
1644 err = btrfs_cow_block(trans, root, b,
1645 p->nodes[level + 1],
1646 p->slots[level + 1], &b);
1653 BUG_ON(!cow && ins_len);
1654 if (level != btrfs_header_level(b))
1656 level = btrfs_header_level(b);
1658 p->nodes[level] = b;
1659 if (!p->skip_locking)
1660 p->locks[level] = 1;
1662 btrfs_clear_path_blocking(p, NULL);
1665 * we have a lock on b and as long as we aren't changing
1666 * the tree, there is no way to for the items in b to change.
1667 * It is safe to drop the lock on our parent before we
1668 * go through the expensive btree search on b.
1670 * If cow is true, then we might be changing slot zero,
1671 * which may require changing the parent. So, we can't
1672 * drop the lock until after we know which slot we're
1676 btrfs_unlock_up_safe(p, level + 1);
1678 ret = bin_search(b, key, level, &slot);
1682 if (ret && slot > 0) {
1686 p->slots[level] = slot;
1687 err = setup_nodes_for_search(trans, root, p, b, level,
1695 b = p->nodes[level];
1696 slot = p->slots[level];
1698 unlock_up(p, level, lowest_unlock);
1700 if (level == lowest_level) {
1706 err = read_block_for_search(trans, root, p,
1707 &b, level, slot, key);
1715 if (!p->skip_locking) {
1716 btrfs_clear_path_blocking(p, NULL);
1717 err = btrfs_try_spin_lock(b);
1720 btrfs_set_path_blocking(p);
1722 btrfs_clear_path_blocking(p, b);
1726 p->slots[level] = slot;
1728 btrfs_leaf_free_space(root, b) < ins_len) {
1729 btrfs_set_path_blocking(p);
1730 err = split_leaf(trans, root, key,
1731 p, ins_len, ret == 0);
1732 btrfs_clear_path_blocking(p, NULL);
1740 if (!p->search_for_split)
1741 unlock_up(p, level, lowest_unlock);
1748 * we don't really know what they plan on doing with the path
1749 * from here on, so for now just mark it as blocking
1751 if (!p->leave_spinning)
1752 btrfs_set_path_blocking(p);
1754 btrfs_release_path(root, p);
1759 * adjust the pointers going up the tree, starting at level
1760 * making sure the right key of each node is points to 'key'.
1761 * This is used after shifting pointers to the left, so it stops
1762 * fixing up pointers when a given leaf/node is not in slot 0 of the
1765 * If this fails to write a tree block, it returns -1, but continues
1766 * fixing up the blocks in ram so the tree is consistent.
1768 static int fixup_low_keys(struct btrfs_trans_handle *trans,
1769 struct btrfs_root *root, struct btrfs_path *path,
1770 struct btrfs_disk_key *key, int level)
1774 struct extent_buffer *t;
1776 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1777 int tslot = path->slots[i];
1778 if (!path->nodes[i])
1781 btrfs_set_node_key(t, key, tslot);
1782 btrfs_mark_buffer_dirty(path->nodes[i]);
1792 * This function isn't completely safe. It's the caller's responsibility
1793 * that the new key won't break the order
1795 int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1796 struct btrfs_root *root, struct btrfs_path *path,
1797 struct btrfs_key *new_key)
1799 struct btrfs_disk_key disk_key;
1800 struct extent_buffer *eb;
1803 eb = path->nodes[0];
1804 slot = path->slots[0];
1806 btrfs_item_key(eb, &disk_key, slot - 1);
1807 if (comp_keys(&disk_key, new_key) >= 0)
1810 if (slot < btrfs_header_nritems(eb) - 1) {
1811 btrfs_item_key(eb, &disk_key, slot + 1);
1812 if (comp_keys(&disk_key, new_key) <= 0)
1816 btrfs_cpu_key_to_disk(&disk_key, new_key);
1817 btrfs_set_item_key(eb, &disk_key, slot);
1818 btrfs_mark_buffer_dirty(eb);
1820 fixup_low_keys(trans, root, path, &disk_key, 1);
1825 * try to push data from one node into the next node left in the
1828 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1829 * error, and > 0 if there was no room in the left hand block.
1831 static int push_node_left(struct btrfs_trans_handle *trans,
1832 struct btrfs_root *root, struct extent_buffer *dst,
1833 struct extent_buffer *src, int empty)
1840 src_nritems = btrfs_header_nritems(src);
1841 dst_nritems = btrfs_header_nritems(dst);
1842 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1843 WARN_ON(btrfs_header_generation(src) != trans->transid);
1844 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1846 if (!empty && src_nritems <= 8)
1849 if (push_items <= 0)
1853 push_items = min(src_nritems, push_items);
1854 if (push_items < src_nritems) {
1855 /* leave at least 8 pointers in the node if
1856 * we aren't going to empty it
1858 if (src_nritems - push_items < 8) {
1859 if (push_items <= 8)
1865 push_items = min(src_nritems - 8, push_items);
1867 copy_extent_buffer(dst, src,
1868 btrfs_node_key_ptr_offset(dst_nritems),
1869 btrfs_node_key_ptr_offset(0),
1870 push_items * sizeof(struct btrfs_key_ptr));
1872 if (push_items < src_nritems) {
1873 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1874 btrfs_node_key_ptr_offset(push_items),
1875 (src_nritems - push_items) *
1876 sizeof(struct btrfs_key_ptr));
1878 btrfs_set_header_nritems(src, src_nritems - push_items);
1879 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1880 btrfs_mark_buffer_dirty(src);
1881 btrfs_mark_buffer_dirty(dst);
1887 * try to push data from one node into the next node right in the
1890 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1891 * error, and > 0 if there was no room in the right hand block.
1893 * this will only push up to 1/2 the contents of the left node over
1895 static int balance_node_right(struct btrfs_trans_handle *trans,
1896 struct btrfs_root *root,
1897 struct extent_buffer *dst,
1898 struct extent_buffer *src)
1906 WARN_ON(btrfs_header_generation(src) != trans->transid);
1907 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1909 src_nritems = btrfs_header_nritems(src);
1910 dst_nritems = btrfs_header_nritems(dst);
1911 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1912 if (push_items <= 0)
1915 if (src_nritems < 4)
1918 max_push = src_nritems / 2 + 1;
1919 /* don't try to empty the node */
1920 if (max_push >= src_nritems)
1923 if (max_push < push_items)
1924 push_items = max_push;
1926 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1927 btrfs_node_key_ptr_offset(0),
1929 sizeof(struct btrfs_key_ptr));
1931 copy_extent_buffer(dst, src,
1932 btrfs_node_key_ptr_offset(0),
1933 btrfs_node_key_ptr_offset(src_nritems - push_items),
1934 push_items * sizeof(struct btrfs_key_ptr));
1936 btrfs_set_header_nritems(src, src_nritems - push_items);
1937 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1939 btrfs_mark_buffer_dirty(src);
1940 btrfs_mark_buffer_dirty(dst);
1946 * helper function to insert a new root level in the tree.
1947 * A new node is allocated, and a single item is inserted to
1948 * point to the existing root
1950 * returns zero on success or < 0 on failure.
1952 static noinline int insert_new_root(struct btrfs_trans_handle *trans,
1953 struct btrfs_root *root,
1954 struct btrfs_path *path, int level)
1957 struct extent_buffer *lower;
1958 struct extent_buffer *c;
1959 struct extent_buffer *old;
1960 struct btrfs_disk_key lower_key;
1962 BUG_ON(path->nodes[level]);
1963 BUG_ON(path->nodes[level-1] != root->node);
1965 lower = path->nodes[level-1];
1967 btrfs_item_key(lower, &lower_key, 0);
1969 btrfs_node_key(lower, &lower_key, 0);
1971 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
1972 root->root_key.objectid, &lower_key,
1973 level, root->node->start, 0);
1977 root_add_used(root, root->nodesize);
1979 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
1980 btrfs_set_header_nritems(c, 1);
1981 btrfs_set_header_level(c, level);
1982 btrfs_set_header_bytenr(c, c->start);
1983 btrfs_set_header_generation(c, trans->transid);
1984 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
1985 btrfs_set_header_owner(c, root->root_key.objectid);
1987 write_extent_buffer(c, root->fs_info->fsid,
1988 (unsigned long)btrfs_header_fsid(c),
1991 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1992 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1995 btrfs_set_node_key(c, &lower_key, 0);
1996 btrfs_set_node_blockptr(c, 0, lower->start);
1997 lower_gen = btrfs_header_generation(lower);
1998 WARN_ON(lower_gen != trans->transid);
2000 btrfs_set_node_ptr_generation(c, 0, lower_gen);
2002 btrfs_mark_buffer_dirty(c);
2005 rcu_assign_pointer(root->node, c);
2007 /* the super has an extra ref to root->node */
2008 free_extent_buffer(old);
2010 add_root_to_dirty_list(root);
2011 extent_buffer_get(c);
2012 path->nodes[level] = c;
2013 path->locks[level] = 1;
2014 path->slots[level] = 0;
2019 * worker function to insert a single pointer in a node.
2020 * the node should have enough room for the pointer already
2022 * slot and level indicate where you want the key to go, and
2023 * blocknr is the block the key points to.
2025 * returns zero on success and < 0 on any error
2027 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2028 *root, struct btrfs_path *path, struct btrfs_disk_key
2029 *key, u64 bytenr, int slot, int level)
2031 struct extent_buffer *lower;
2034 BUG_ON(!path->nodes[level]);
2035 btrfs_assert_tree_locked(path->nodes[level]);
2036 lower = path->nodes[level];
2037 nritems = btrfs_header_nritems(lower);
2038 BUG_ON(slot > nritems);
2039 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
2041 if (slot != nritems) {
2042 memmove_extent_buffer(lower,
2043 btrfs_node_key_ptr_offset(slot + 1),
2044 btrfs_node_key_ptr_offset(slot),
2045 (nritems - slot) * sizeof(struct btrfs_key_ptr));
2047 btrfs_set_node_key(lower, key, slot);
2048 btrfs_set_node_blockptr(lower, slot, bytenr);
2049 WARN_ON(trans->transid == 0);
2050 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
2051 btrfs_set_header_nritems(lower, nritems + 1);
2052 btrfs_mark_buffer_dirty(lower);
2057 * split the node at the specified level in path in two.
2058 * The path is corrected to point to the appropriate node after the split
2060 * Before splitting this tries to make some room in the node by pushing
2061 * left and right, if either one works, it returns right away.
2063 * returns 0 on success and < 0 on failure
2065 static noinline int split_node(struct btrfs_trans_handle *trans,
2066 struct btrfs_root *root,
2067 struct btrfs_path *path, int level)
2069 struct extent_buffer *c;
2070 struct extent_buffer *split;
2071 struct btrfs_disk_key disk_key;
2077 c = path->nodes[level];
2078 WARN_ON(btrfs_header_generation(c) != trans->transid);
2079 if (c == root->node) {
2080 /* trying to split the root, lets make a new one */
2081 ret = insert_new_root(trans, root, path, level + 1);
2085 ret = push_nodes_for_insert(trans, root, path, level);
2086 c = path->nodes[level];
2087 if (!ret && btrfs_header_nritems(c) <
2088 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
2094 c_nritems = btrfs_header_nritems(c);
2095 mid = (c_nritems + 1) / 2;
2096 btrfs_node_key(c, &disk_key, mid);
2098 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
2099 root->root_key.objectid,
2100 &disk_key, level, c->start, 0);
2102 return PTR_ERR(split);
2104 root_add_used(root, root->nodesize);
2106 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
2107 btrfs_set_header_level(split, btrfs_header_level(c));
2108 btrfs_set_header_bytenr(split, split->start);
2109 btrfs_set_header_generation(split, trans->transid);
2110 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
2111 btrfs_set_header_owner(split, root->root_key.objectid);
2112 write_extent_buffer(split, root->fs_info->fsid,
2113 (unsigned long)btrfs_header_fsid(split),
2115 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2116 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2120 copy_extent_buffer(split, c,
2121 btrfs_node_key_ptr_offset(0),
2122 btrfs_node_key_ptr_offset(mid),
2123 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2124 btrfs_set_header_nritems(split, c_nritems - mid);
2125 btrfs_set_header_nritems(c, mid);
2128 btrfs_mark_buffer_dirty(c);
2129 btrfs_mark_buffer_dirty(split);
2131 wret = insert_ptr(trans, root, path, &disk_key, split->start,
2132 path->slots[level + 1] + 1,
2137 if (path->slots[level] >= mid) {
2138 path->slots[level] -= mid;
2139 btrfs_tree_unlock(c);
2140 free_extent_buffer(c);
2141 path->nodes[level] = split;
2142 path->slots[level + 1] += 1;
2144 btrfs_tree_unlock(split);
2145 free_extent_buffer(split);
2151 * how many bytes are required to store the items in a leaf. start
2152 * and nr indicate which items in the leaf to check. This totals up the
2153 * space used both by the item structs and the item data
2155 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
2158 int nritems = btrfs_header_nritems(l);
2159 int end = min(nritems, start + nr) - 1;
2163 data_len = btrfs_item_end_nr(l, start);
2164 data_len = data_len - btrfs_item_offset_nr(l, end);
2165 data_len += sizeof(struct btrfs_item) * nr;
2166 WARN_ON(data_len < 0);
2171 * The space between the end of the leaf items and
2172 * the start of the leaf data. IOW, how much room
2173 * the leaf has left for both items and data
2175 noinline int btrfs_leaf_free_space(struct btrfs_root *root,
2176 struct extent_buffer *leaf)
2178 int nritems = btrfs_header_nritems(leaf);
2180 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2182 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2183 "used %d nritems %d\n",
2184 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
2185 leaf_space_used(leaf, 0, nritems), nritems);
2191 * min slot controls the lowest index we're willing to push to the
2192 * right. We'll push up to and including min_slot, but no lower
2194 static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2195 struct btrfs_root *root,
2196 struct btrfs_path *path,
2197 int data_size, int empty,
2198 struct extent_buffer *right,
2199 int free_space, u32 left_nritems,
2202 struct extent_buffer *left = path->nodes[0];
2203 struct extent_buffer *upper = path->nodes[1];
2204 struct btrfs_disk_key disk_key;
2209 struct btrfs_item *item;
2218 nr = max_t(u32, 1, min_slot);
2220 if (path->slots[0] >= left_nritems)
2221 push_space += data_size;
2223 slot = path->slots[1];
2224 i = left_nritems - 1;
2226 item = btrfs_item_nr(left, i);
2228 if (!empty && push_items > 0) {
2229 if (path->slots[0] > i)
2231 if (path->slots[0] == i) {
2232 int space = btrfs_leaf_free_space(root, left);
2233 if (space + push_space * 2 > free_space)
2238 if (path->slots[0] == i)
2239 push_space += data_size;
2241 if (!left->map_token) {
2242 map_extent_buffer(left, (unsigned long)item,
2243 sizeof(struct btrfs_item),
2244 &left->map_token, &left->kaddr,
2245 &left->map_start, &left->map_len,
2249 this_item_size = btrfs_item_size(left, item);
2250 if (this_item_size + sizeof(*item) + push_space > free_space)
2254 push_space += this_item_size + sizeof(*item);
2259 if (left->map_token) {
2260 unmap_extent_buffer(left, left->map_token, KM_USER1);
2261 left->map_token = NULL;
2264 if (push_items == 0)
2267 if (!empty && push_items == left_nritems)
2270 /* push left to right */
2271 right_nritems = btrfs_header_nritems(right);
2273 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
2274 push_space -= leaf_data_end(root, left);
2276 /* make room in the right data area */
2277 data_end = leaf_data_end(root, right);
2278 memmove_extent_buffer(right,
2279 btrfs_leaf_data(right) + data_end - push_space,
2280 btrfs_leaf_data(right) + data_end,
2281 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2283 /* copy from the left data area */
2284 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
2285 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2286 btrfs_leaf_data(left) + leaf_data_end(root, left),
2289 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2290 btrfs_item_nr_offset(0),
2291 right_nritems * sizeof(struct btrfs_item));
2293 /* copy the items from left to right */
2294 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2295 btrfs_item_nr_offset(left_nritems - push_items),
2296 push_items * sizeof(struct btrfs_item));
2298 /* update the item pointers */
2299 right_nritems += push_items;
2300 btrfs_set_header_nritems(right, right_nritems);
2301 push_space = BTRFS_LEAF_DATA_SIZE(root);
2302 for (i = 0; i < right_nritems; i++) {
2303 item = btrfs_item_nr(right, i);
2304 if (!right->map_token) {
2305 map_extent_buffer(right, (unsigned long)item,
2306 sizeof(struct btrfs_item),
2307 &right->map_token, &right->kaddr,
2308 &right->map_start, &right->map_len,
2311 push_space -= btrfs_item_size(right, item);
2312 btrfs_set_item_offset(right, item, push_space);
2315 if (right->map_token) {
2316 unmap_extent_buffer(right, right->map_token, KM_USER1);
2317 right->map_token = NULL;
2319 left_nritems -= push_items;
2320 btrfs_set_header_nritems(left, left_nritems);
2323 btrfs_mark_buffer_dirty(left);
2325 clean_tree_block(trans, root, left);
2327 btrfs_mark_buffer_dirty(right);
2329 btrfs_item_key(right, &disk_key, 0);
2330 btrfs_set_node_key(upper, &disk_key, slot + 1);
2331 btrfs_mark_buffer_dirty(upper);
2333 /* then fixup the leaf pointer in the path */
2334 if (path->slots[0] >= left_nritems) {
2335 path->slots[0] -= left_nritems;
2336 if (btrfs_header_nritems(path->nodes[0]) == 0)
2337 clean_tree_block(trans, root, path->nodes[0]);
2338 btrfs_tree_unlock(path->nodes[0]);
2339 free_extent_buffer(path->nodes[0]);
2340 path->nodes[0] = right;
2341 path->slots[1] += 1;
2343 btrfs_tree_unlock(right);
2344 free_extent_buffer(right);
2349 btrfs_tree_unlock(right);
2350 free_extent_buffer(right);
2355 * push some data in the path leaf to the right, trying to free up at
2356 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2358 * returns 1 if the push failed because the other node didn't have enough
2359 * room, 0 if everything worked out and < 0 if there were major errors.
2361 * this will push starting from min_slot to the end of the leaf. It won't
2362 * push any slot lower than min_slot
2364 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
2365 *root, struct btrfs_path *path,
2366 int min_data_size, int data_size,
2367 int empty, u32 min_slot)
2369 struct extent_buffer *left = path->nodes[0];
2370 struct extent_buffer *right;
2371 struct extent_buffer *upper;
2377 if (!path->nodes[1])
2380 slot = path->slots[1];
2381 upper = path->nodes[1];
2382 if (slot >= btrfs_header_nritems(upper) - 1)
2385 btrfs_assert_tree_locked(path->nodes[1]);
2387 right = read_node_slot(root, upper, slot + 1);
2391 btrfs_tree_lock(right);
2392 btrfs_set_lock_blocking(right);
2394 free_space = btrfs_leaf_free_space(root, right);
2395 if (free_space < data_size)
2398 /* cow and double check */
2399 ret = btrfs_cow_block(trans, root, right, upper,
2404 free_space = btrfs_leaf_free_space(root, right);
2405 if (free_space < data_size)
2408 left_nritems = btrfs_header_nritems(left);
2409 if (left_nritems == 0)
2412 return __push_leaf_right(trans, root, path, min_data_size, empty,
2413 right, free_space, left_nritems, min_slot);
2415 btrfs_tree_unlock(right);
2416 free_extent_buffer(right);
2421 * push some data in the path leaf to the left, trying to free up at
2422 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2424 * max_slot can put a limit on how far into the leaf we'll push items. The
2425 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2428 static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2429 struct btrfs_root *root,
2430 struct btrfs_path *path, int data_size,
2431 int empty, struct extent_buffer *left,
2432 int free_space, u32 right_nritems,
2435 struct btrfs_disk_key disk_key;
2436 struct extent_buffer *right = path->nodes[0];
2440 struct btrfs_item *item;
2441 u32 old_left_nritems;
2446 u32 old_left_item_size;
2449 nr = min(right_nritems, max_slot);
2451 nr = min(right_nritems - 1, max_slot);
2453 for (i = 0; i < nr; i++) {
2454 item = btrfs_item_nr(right, i);
2455 if (!right->map_token) {
2456 map_extent_buffer(right, (unsigned long)item,
2457 sizeof(struct btrfs_item),
2458 &right->map_token, &right->kaddr,
2459 &right->map_start, &right->map_len,
2463 if (!empty && push_items > 0) {
2464 if (path->slots[0] < i)
2466 if (path->slots[0] == i) {
2467 int space = btrfs_leaf_free_space(root, right);
2468 if (space + push_space * 2 > free_space)
2473 if (path->slots[0] == i)
2474 push_space += data_size;
2476 this_item_size = btrfs_item_size(right, item);
2477 if (this_item_size + sizeof(*item) + push_space > free_space)
2481 push_space += this_item_size + sizeof(*item);
2484 if (right->map_token) {
2485 unmap_extent_buffer(right, right->map_token, KM_USER1);
2486 right->map_token = NULL;
2489 if (push_items == 0) {
2493 if (!empty && push_items == btrfs_header_nritems(right))
2496 /* push data from right to left */
2497 copy_extent_buffer(left, right,
2498 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2499 btrfs_item_nr_offset(0),
2500 push_items * sizeof(struct btrfs_item));
2502 push_space = BTRFS_LEAF_DATA_SIZE(root) -
2503 btrfs_item_offset_nr(right, push_items - 1);
2505 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
2506 leaf_data_end(root, left) - push_space,
2507 btrfs_leaf_data(right) +
2508 btrfs_item_offset_nr(right, push_items - 1),
2510 old_left_nritems = btrfs_header_nritems(left);
2511 BUG_ON(old_left_nritems <= 0);
2513 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
2514 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
2517 item = btrfs_item_nr(left, i);
2518 if (!left->map_token) {
2519 map_extent_buffer(left, (unsigned long)item,
2520 sizeof(struct btrfs_item),
2521 &left->map_token, &left->kaddr,
2522 &left->map_start, &left->map_len,
2526 ioff = btrfs_item_offset(left, item);
2527 btrfs_set_item_offset(left, item,
2528 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
2530 btrfs_set_header_nritems(left, old_left_nritems + push_items);
2531 if (left->map_token) {
2532 unmap_extent_buffer(left, left->map_token, KM_USER1);
2533 left->map_token = NULL;
2536 /* fixup right node */
2537 if (push_items > right_nritems) {
2538 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2543 if (push_items < right_nritems) {
2544 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2545 leaf_data_end(root, right);
2546 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2547 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2548 btrfs_leaf_data(right) +
2549 leaf_data_end(root, right), push_space);
2551 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
2552 btrfs_item_nr_offset(push_items),
2553 (btrfs_header_nritems(right) - push_items) *
2554 sizeof(struct btrfs_item));
2556 right_nritems -= push_items;
2557 btrfs_set_header_nritems(right, right_nritems);
2558 push_space = BTRFS_LEAF_DATA_SIZE(root);
2559 for (i = 0; i < right_nritems; i++) {
2560 item = btrfs_item_nr(right, i);
2562 if (!right->map_token) {
2563 map_extent_buffer(right, (unsigned long)item,
2564 sizeof(struct btrfs_item),
2565 &right->map_token, &right->kaddr,
2566 &right->map_start, &right->map_len,
2570 push_space = push_space - btrfs_item_size(right, item);
2571 btrfs_set_item_offset(right, item, push_space);
2573 if (right->map_token) {
2574 unmap_extent_buffer(right, right->map_token, KM_USER1);
2575 right->map_token = NULL;
2578 btrfs_mark_buffer_dirty(left);
2580 btrfs_mark_buffer_dirty(right);
2582 clean_tree_block(trans, root, right);
2584 btrfs_item_key(right, &disk_key, 0);
2585 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
2589 /* then fixup the leaf pointer in the path */
2590 if (path->slots[0] < push_items) {
2591 path->slots[0] += old_left_nritems;
2592 btrfs_tree_unlock(path->nodes[0]);
2593 free_extent_buffer(path->nodes[0]);
2594 path->nodes[0] = left;
2595 path->slots[1] -= 1;
2597 btrfs_tree_unlock(left);
2598 free_extent_buffer(left);
2599 path->slots[0] -= push_items;
2601 BUG_ON(path->slots[0] < 0);
2604 btrfs_tree_unlock(left);
2605 free_extent_buffer(left);
2610 * push some data in the path leaf to the left, trying to free up at
2611 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2613 * max_slot can put a limit on how far into the leaf we'll push items. The
2614 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2617 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
2618 *root, struct btrfs_path *path, int min_data_size,
2619 int data_size, int empty, u32 max_slot)
2621 struct extent_buffer *right = path->nodes[0];
2622 struct extent_buffer *left;
2628 slot = path->slots[1];
2631 if (!path->nodes[1])
2634 right_nritems = btrfs_header_nritems(right);
2635 if (right_nritems == 0)
2638 btrfs_assert_tree_locked(path->nodes[1]);
2640 left = read_node_slot(root, path->nodes[1], slot - 1);
2644 btrfs_tree_lock(left);
2645 btrfs_set_lock_blocking(left);
2647 free_space = btrfs_leaf_free_space(root, left);
2648 if (free_space < data_size) {
2653 /* cow and double check */
2654 ret = btrfs_cow_block(trans, root, left,
2655 path->nodes[1], slot - 1, &left);
2657 /* we hit -ENOSPC, but it isn't fatal here */
2662 free_space = btrfs_leaf_free_space(root, left);
2663 if (free_space < data_size) {
2668 return __push_leaf_left(trans, root, path, min_data_size,
2669 empty, left, free_space, right_nritems,
2672 btrfs_tree_unlock(left);
2673 free_extent_buffer(left);
2678 * split the path's leaf in two, making sure there is at least data_size
2679 * available for the resulting leaf level of the path.
2681 * returns 0 if all went well and < 0 on failure.
2683 static noinline int copy_for_split(struct btrfs_trans_handle *trans,
2684 struct btrfs_root *root,
2685 struct btrfs_path *path,
2686 struct extent_buffer *l,
2687 struct extent_buffer *right,
2688 int slot, int mid, int nritems)
2695 struct btrfs_disk_key disk_key;
2697 nritems = nritems - mid;
2698 btrfs_set_header_nritems(right, nritems);
2699 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2701 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2702 btrfs_item_nr_offset(mid),
2703 nritems * sizeof(struct btrfs_item));
2705 copy_extent_buffer(right, l,
2706 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2707 data_copy_size, btrfs_leaf_data(l) +
2708 leaf_data_end(root, l), data_copy_size);
2710 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2711 btrfs_item_end_nr(l, mid);
2713 for (i = 0; i < nritems; i++) {
2714 struct btrfs_item *item = btrfs_item_nr(right, i);
2717 if (!right->map_token) {
2718 map_extent_buffer(right, (unsigned long)item,
2719 sizeof(struct btrfs_item),
2720 &right->map_token, &right->kaddr,
2721 &right->map_start, &right->map_len,
2725 ioff = btrfs_item_offset(right, item);
2726 btrfs_set_item_offset(right, item, ioff + rt_data_off);
2729 if (right->map_token) {
2730 unmap_extent_buffer(right, right->map_token, KM_USER1);
2731 right->map_token = NULL;
2734 btrfs_set_header_nritems(l, mid);
2736 btrfs_item_key(right, &disk_key, 0);
2737 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2738 path->slots[1] + 1, 1);
2742 btrfs_mark_buffer_dirty(right);
2743 btrfs_mark_buffer_dirty(l);
2744 BUG_ON(path->slots[0] != slot);
2747 btrfs_tree_unlock(path->nodes[0]);
2748 free_extent_buffer(path->nodes[0]);
2749 path->nodes[0] = right;
2750 path->slots[0] -= mid;
2751 path->slots[1] += 1;
2753 btrfs_tree_unlock(right);
2754 free_extent_buffer(right);
2757 BUG_ON(path->slots[0] < 0);
2763 * double splits happen when we need to insert a big item in the middle
2764 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2765 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2768 * We avoid this by trying to push the items on either side of our target
2769 * into the adjacent leaves. If all goes well we can avoid the double split
2772 static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2773 struct btrfs_root *root,
2774 struct btrfs_path *path,
2782 slot = path->slots[0];
2785 * try to push all the items after our slot into the
2788 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2795 nritems = btrfs_header_nritems(path->nodes[0]);
2797 * our goal is to get our slot at the start or end of a leaf. If
2798 * we've done so we're done
2800 if (path->slots[0] == 0 || path->slots[0] == nritems)
2803 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2806 /* try to push all the items before our slot into the next leaf */
2807 slot = path->slots[0];
2808 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2821 * split the path's leaf in two, making sure there is at least data_size
2822 * available for the resulting leaf level of the path.
2824 * returns 0 if all went well and < 0 on failure.
2826 static noinline int split_leaf(struct btrfs_trans_handle *trans,
2827 struct btrfs_root *root,
2828 struct btrfs_key *ins_key,
2829 struct btrfs_path *path, int data_size,
2832 struct btrfs_disk_key disk_key;
2833 struct extent_buffer *l;
2837 struct extent_buffer *right;
2841 int num_doubles = 0;
2842 int tried_avoid_double = 0;
2845 slot = path->slots[0];
2846 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2847 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2850 /* first try to make some room by pushing left and right */
2852 wret = push_leaf_right(trans, root, path, data_size,
2857 wret = push_leaf_left(trans, root, path, data_size,
2858 data_size, 0, (u32)-1);
2864 /* did the pushes work? */
2865 if (btrfs_leaf_free_space(root, l) >= data_size)
2869 if (!path->nodes[1]) {
2870 ret = insert_new_root(trans, root, path, 1);
2877 slot = path->slots[0];
2878 nritems = btrfs_header_nritems(l);
2879 mid = (nritems + 1) / 2;
2883 leaf_space_used(l, mid, nritems - mid) + data_size >
2884 BTRFS_LEAF_DATA_SIZE(root)) {
2885 if (slot >= nritems) {
2889 if (mid != nritems &&
2890 leaf_space_used(l, mid, nritems - mid) +
2891 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2892 if (data_size && !tried_avoid_double)
2893 goto push_for_double;
2899 if (leaf_space_used(l, 0, mid) + data_size >
2900 BTRFS_LEAF_DATA_SIZE(root)) {
2901 if (!extend && data_size && slot == 0) {
2903 } else if ((extend || !data_size) && slot == 0) {
2907 if (mid != nritems &&
2908 leaf_space_used(l, mid, nritems - mid) +
2909 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2910 if (data_size && !tried_avoid_double)
2911 goto push_for_double;
2919 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2921 btrfs_item_key(l, &disk_key, mid);
2923 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
2924 root->root_key.objectid,
2925 &disk_key, 0, l->start, 0);
2927 return PTR_ERR(right);
2929 root_add_used(root, root->leafsize);
2931 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2932 btrfs_set_header_bytenr(right, right->start);
2933 btrfs_set_header_generation(right, trans->transid);
2934 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
2935 btrfs_set_header_owner(right, root->root_key.objectid);
2936 btrfs_set_header_level(right, 0);
2937 write_extent_buffer(right, root->fs_info->fsid,
2938 (unsigned long)btrfs_header_fsid(right),
2941 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2942 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2947 btrfs_set_header_nritems(right, 0);
2948 wret = insert_ptr(trans, root, path,
2949 &disk_key, right->start,
2950 path->slots[1] + 1, 1);
2954 btrfs_tree_unlock(path->nodes[0]);
2955 free_extent_buffer(path->nodes[0]);
2956 path->nodes[0] = right;
2958 path->slots[1] += 1;
2960 btrfs_set_header_nritems(right, 0);
2961 wret = insert_ptr(trans, root, path,
2967 btrfs_tree_unlock(path->nodes[0]);
2968 free_extent_buffer(path->nodes[0]);
2969 path->nodes[0] = right;
2971 if (path->slots[1] == 0) {
2972 wret = fixup_low_keys(trans, root,
2973 path, &disk_key, 1);
2978 btrfs_mark_buffer_dirty(right);
2982 ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
2986 BUG_ON(num_doubles != 0);
2994 push_for_double_split(trans, root, path, data_size);
2995 tried_avoid_double = 1;
2996 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3001 static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3002 struct btrfs_root *root,
3003 struct btrfs_path *path, int ins_len)
3005 struct btrfs_key key;
3006 struct extent_buffer *leaf;
3007 struct btrfs_file_extent_item *fi;
3012 leaf = path->nodes[0];
3013 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3015 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3016 key.type != BTRFS_EXTENT_CSUM_KEY);
3018 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3021 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3022 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3023 fi = btrfs_item_ptr(leaf, path->slots[0],
3024 struct btrfs_file_extent_item);
3025 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3027 btrfs_release_path(root, path);
3029 path->keep_locks = 1;
3030 path->search_for_split = 1;
3031 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3032 path->search_for_split = 0;
3037 leaf = path->nodes[0];
3038 /* if our item isn't there or got smaller, return now */
3039 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3042 /* the leaf has changed, it now has room. return now */
3043 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3046 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3047 fi = btrfs_item_ptr(leaf, path->slots[0],
3048 struct btrfs_file_extent_item);
3049 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3053 btrfs_set_path_blocking(path);
3054 ret = split_leaf(trans, root, &key, path, ins_len, 1);
3058 path->keep_locks = 0;
3059 btrfs_unlock_up_safe(path, 1);
3062 path->keep_locks = 0;
3066 static noinline int split_item(struct btrfs_trans_handle *trans,
3067 struct btrfs_root *root,
3068 struct btrfs_path *path,
3069 struct btrfs_key *new_key,
3070 unsigned long split_offset)
3072 struct extent_buffer *leaf;
3073 struct btrfs_item *item;
3074 struct btrfs_item *new_item;
3080 struct btrfs_disk_key disk_key;
3082 leaf = path->nodes[0];
3083 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3085 btrfs_set_path_blocking(path);
3087 item = btrfs_item_nr(leaf, path->slots[0]);
3088 orig_offset = btrfs_item_offset(leaf, item);
3089 item_size = btrfs_item_size(leaf, item);
3091 buf = kmalloc(item_size, GFP_NOFS);
3095 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3096 path->slots[0]), item_size);
3098 slot = path->slots[0] + 1;
3099 nritems = btrfs_header_nritems(leaf);
3100 if (slot != nritems) {
3101 /* shift the items */
3102 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3103 btrfs_item_nr_offset(slot),
3104 (nritems - slot) * sizeof(struct btrfs_item));
3107 btrfs_cpu_key_to_disk(&disk_key, new_key);
3108 btrfs_set_item_key(leaf, &disk_key, slot);
3110 new_item = btrfs_item_nr(leaf, slot);
3112 btrfs_set_item_offset(leaf, new_item, orig_offset);
3113 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3115 btrfs_set_item_offset(leaf, item,
3116 orig_offset + item_size - split_offset);
3117 btrfs_set_item_size(leaf, item, split_offset);
3119 btrfs_set_header_nritems(leaf, nritems + 1);
3121 /* write the data for the start of the original item */
3122 write_extent_buffer(leaf, buf,
3123 btrfs_item_ptr_offset(leaf, path->slots[0]),
3126 /* write the data for the new item */
3127 write_extent_buffer(leaf, buf + split_offset,
3128 btrfs_item_ptr_offset(leaf, slot),
3129 item_size - split_offset);
3130 btrfs_mark_buffer_dirty(leaf);
3132 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
3138 * This function splits a single item into two items,
3139 * giving 'new_key' to the new item and splitting the
3140 * old one at split_offset (from the start of the item).
3142 * The path may be released by this operation. After
3143 * the split, the path is pointing to the old item. The
3144 * new item is going to be in the same node as the old one.
3146 * Note, the item being split must be smaller enough to live alone on
3147 * a tree block with room for one extra struct btrfs_item
3149 * This allows us to split the item in place, keeping a lock on the
3150 * leaf the entire time.
3152 int btrfs_split_item(struct btrfs_trans_handle *trans,
3153 struct btrfs_root *root,
3154 struct btrfs_path *path,
3155 struct btrfs_key *new_key,
3156 unsigned long split_offset)
3159 ret = setup_leaf_for_split(trans, root, path,
3160 sizeof(struct btrfs_item));
3164 ret = split_item(trans, root, path, new_key, split_offset);
3169 * This function duplicate a item, giving 'new_key' to the new item.
3170 * It guarantees both items live in the same tree leaf and the new item
3171 * is contiguous with the original item.
3173 * This allows us to split file extent in place, keeping a lock on the
3174 * leaf the entire time.
3176 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3177 struct btrfs_root *root,
3178 struct btrfs_path *path,
3179 struct btrfs_key *new_key)
3181 struct extent_buffer *leaf;
3185 leaf = path->nodes[0];
3186 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3187 ret = setup_leaf_for_split(trans, root, path,
3188 item_size + sizeof(struct btrfs_item));
3193 ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3194 item_size, item_size +
3195 sizeof(struct btrfs_item), 1);
3198 leaf = path->nodes[0];
3199 memcpy_extent_buffer(leaf,
3200 btrfs_item_ptr_offset(leaf, path->slots[0]),
3201 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3207 * make the item pointed to by the path smaller. new_size indicates
3208 * how small to make it, and from_end tells us if we just chop bytes
3209 * off the end of the item or if we shift the item to chop bytes off
3212 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3213 struct btrfs_root *root,
3214 struct btrfs_path *path,
3215 u32 new_size, int from_end)
3219 struct extent_buffer *leaf;
3220 struct btrfs_item *item;
3222 unsigned int data_end;
3223 unsigned int old_data_start;
3224 unsigned int old_size;
3225 unsigned int size_diff;
3228 leaf = path->nodes[0];
3229 slot = path->slots[0];
3231 old_size = btrfs_item_size_nr(leaf, slot);
3232 if (old_size == new_size)
3235 nritems = btrfs_header_nritems(leaf);
3236 data_end = leaf_data_end(root, leaf);
3238 old_data_start = btrfs_item_offset_nr(leaf, slot);
3240 size_diff = old_size - new_size;
3243 BUG_ON(slot >= nritems);
3246 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3248 /* first correct the data pointers */
3249 for (i = slot; i < nritems; i++) {
3251 item = btrfs_item_nr(leaf, i);
3253 if (!leaf->map_token) {
3254 map_extent_buffer(leaf, (unsigned long)item,
3255 sizeof(struct btrfs_item),
3256 &leaf->map_token, &leaf->kaddr,
3257 &leaf->map_start, &leaf->map_len,
3261 ioff = btrfs_item_offset(leaf, item);
3262 btrfs_set_item_offset(leaf, item, ioff + size_diff);
3265 if (leaf->map_token) {
3266 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3267 leaf->map_token = NULL;
3270 /* shift the data */
3272 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3273 data_end + size_diff, btrfs_leaf_data(leaf) +
3274 data_end, old_data_start + new_size - data_end);
3276 struct btrfs_disk_key disk_key;
3279 btrfs_item_key(leaf, &disk_key, slot);
3281 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3283 struct btrfs_file_extent_item *fi;
3285 fi = btrfs_item_ptr(leaf, slot,
3286 struct btrfs_file_extent_item);
3287 fi = (struct btrfs_file_extent_item *)(
3288 (unsigned long)fi - size_diff);
3290 if (btrfs_file_extent_type(leaf, fi) ==
3291 BTRFS_FILE_EXTENT_INLINE) {
3292 ptr = btrfs_item_ptr_offset(leaf, slot);
3293 memmove_extent_buffer(leaf, ptr,
3295 offsetof(struct btrfs_file_extent_item,
3300 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3301 data_end + size_diff, btrfs_leaf_data(leaf) +
3302 data_end, old_data_start - data_end);
3304 offset = btrfs_disk_key_offset(&disk_key);
3305 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3306 btrfs_set_item_key(leaf, &disk_key, slot);
3308 fixup_low_keys(trans, root, path, &disk_key, 1);
3311 item = btrfs_item_nr(leaf, slot);
3312 btrfs_set_item_size(leaf, item, new_size);
3313 btrfs_mark_buffer_dirty(leaf);
3316 if (btrfs_leaf_free_space(root, leaf) < 0) {
3317 btrfs_print_leaf(root, leaf);
3324 * make the item pointed to by the path bigger, data_size is the new size.
3326 int btrfs_extend_item(struct btrfs_trans_handle *trans,
3327 struct btrfs_root *root, struct btrfs_path *path,
3332 struct extent_buffer *leaf;
3333 struct btrfs_item *item;
3335 unsigned int data_end;
3336 unsigned int old_data;
3337 unsigned int old_size;
3340 leaf = path->nodes[0];
3342 nritems = btrfs_header_nritems(leaf);
3343 data_end = leaf_data_end(root, leaf);
3345 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3346 btrfs_print_leaf(root, leaf);
3349 slot = path->slots[0];
3350 old_data = btrfs_item_end_nr(leaf, slot);
3353 if (slot >= nritems) {
3354 btrfs_print_leaf(root, leaf);
3355 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3361 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3363 /* first correct the data pointers */
3364 for (i = slot; i < nritems; i++) {
3366 item = btrfs_item_nr(leaf, i);
3368 if (!leaf->map_token) {
3369 map_extent_buffer(leaf, (unsigned long)item,
3370 sizeof(struct btrfs_item),
3371 &leaf->map_token, &leaf->kaddr,
3372 &leaf->map_start, &leaf->map_len,
3375 ioff = btrfs_item_offset(leaf, item);
3376 btrfs_set_item_offset(leaf, item, ioff - data_size);
3379 if (leaf->map_token) {
3380 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3381 leaf->map_token = NULL;
3384 /* shift the data */
3385 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3386 data_end - data_size, btrfs_leaf_data(leaf) +
3387 data_end, old_data - data_end);
3389 data_end = old_data;
3390 old_size = btrfs_item_size_nr(leaf, slot);
3391 item = btrfs_item_nr(leaf, slot);
3392 btrfs_set_item_size(leaf, item, old_size + data_size);
3393 btrfs_mark_buffer_dirty(leaf);
3396 if (btrfs_leaf_free_space(root, leaf) < 0) {
3397 btrfs_print_leaf(root, leaf);
3404 * Given a key and some data, insert items into the tree.
3405 * This does all the path init required, making room in the tree if needed.
3406 * Returns the number of keys that were inserted.
3408 int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3409 struct btrfs_root *root,
3410 struct btrfs_path *path,
3411 struct btrfs_key *cpu_key, u32 *data_size,
3414 struct extent_buffer *leaf;
3415 struct btrfs_item *item;
3422 unsigned int data_end;
3423 struct btrfs_disk_key disk_key;
3424 struct btrfs_key found_key;
3426 for (i = 0; i < nr; i++) {
3427 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3428 BTRFS_LEAF_DATA_SIZE(root)) {
3432 total_data += data_size[i];
3433 total_size += data_size[i] + sizeof(struct btrfs_item);
3437 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3443 leaf = path->nodes[0];
3445 nritems = btrfs_header_nritems(leaf);
3446 data_end = leaf_data_end(root, leaf);
3448 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3449 for (i = nr; i >= 0; i--) {
3450 total_data -= data_size[i];
3451 total_size -= data_size[i] + sizeof(struct btrfs_item);
3452 if (total_size < btrfs_leaf_free_space(root, leaf))
3458 slot = path->slots[0];
3461 if (slot != nritems) {
3462 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3464 item = btrfs_item_nr(leaf, slot);
3465 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3467 /* figure out how many keys we can insert in here */
3468 total_data = data_size[0];
3469 for (i = 1; i < nr; i++) {
3470 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3472 total_data += data_size[i];
3476 if (old_data < data_end) {
3477 btrfs_print_leaf(root, leaf);
3478 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3479 slot, old_data, data_end);
3483 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3485 /* first correct the data pointers */
3486 WARN_ON(leaf->map_token);
3487 for (i = slot; i < nritems; i++) {
3490 item = btrfs_item_nr(leaf, i);
3491 if (!leaf->map_token) {
3492 map_extent_buffer(leaf, (unsigned long)item,
3493 sizeof(struct btrfs_item),
3494 &leaf->map_token, &leaf->kaddr,
3495 &leaf->map_start, &leaf->map_len,
3499 ioff = btrfs_item_offset(leaf, item);
3500 btrfs_set_item_offset(leaf, item, ioff - total_data);
3502 if (leaf->map_token) {
3503 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3504 leaf->map_token = NULL;
3507 /* shift the items */
3508 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3509 btrfs_item_nr_offset(slot),
3510 (nritems - slot) * sizeof(struct btrfs_item));
3512 /* shift the data */
3513 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3514 data_end - total_data, btrfs_leaf_data(leaf) +
3515 data_end, old_data - data_end);
3516 data_end = old_data;
3519 * this sucks but it has to be done, if we are inserting at
3520 * the end of the leaf only insert 1 of the items, since we
3521 * have no way of knowing whats on the next leaf and we'd have
3522 * to drop our current locks to figure it out
3527 /* setup the item for the new data */
3528 for (i = 0; i < nr; i++) {
3529 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3530 btrfs_set_item_key(leaf, &disk_key, slot + i);
3531 item = btrfs_item_nr(leaf, slot + i);
3532 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3533 data_end -= data_size[i];
3534 btrfs_set_item_size(leaf, item, data_size[i]);
3536 btrfs_set_header_nritems(leaf, nritems + nr);
3537 btrfs_mark_buffer_dirty(leaf);
3541 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3542 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3545 if (btrfs_leaf_free_space(root, leaf) < 0) {
3546 btrfs_print_leaf(root, leaf);
3556 * this is a helper for btrfs_insert_empty_items, the main goal here is
3557 * to save stack depth by doing the bulk of the work in a function
3558 * that doesn't call btrfs_search_slot
3560 static noinline_for_stack int
3561 setup_items_for_insert(struct btrfs_trans_handle *trans,
3562 struct btrfs_root *root, struct btrfs_path *path,
3563 struct btrfs_key *cpu_key, u32 *data_size,
3564 u32 total_data, u32 total_size, int nr)
3566 struct btrfs_item *item;
3569 unsigned int data_end;
3570 struct btrfs_disk_key disk_key;
3572 struct extent_buffer *leaf;
3575 leaf = path->nodes[0];
3576 slot = path->slots[0];
3578 nritems = btrfs_header_nritems(leaf);
3579 data_end = leaf_data_end(root, leaf);
3581 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3582 btrfs_print_leaf(root, leaf);
3583 printk(KERN_CRIT "not enough freespace need %u have %d\n",
3584 total_size, btrfs_leaf_free_space(root, leaf));
3588 if (slot != nritems) {
3589 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3591 if (old_data < data_end) {
3592 btrfs_print_leaf(root, leaf);
3593 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3594 slot, old_data, data_end);
3598 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3600 /* first correct the data pointers */
3601 WARN_ON(leaf->map_token);
3602 for (i = slot; i < nritems; i++) {
3605 item = btrfs_item_nr(leaf, i);
3606 if (!leaf->map_token) {
3607 map_extent_buffer(leaf, (unsigned long)item,
3608 sizeof(struct btrfs_item),
3609 &leaf->map_token, &leaf->kaddr,
3610 &leaf->map_start, &leaf->map_len,
3614 ioff = btrfs_item_offset(leaf, item);
3615 btrfs_set_item_offset(leaf, item, ioff - total_data);
3617 if (leaf->map_token) {
3618 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3619 leaf->map_token = NULL;
3622 /* shift the items */
3623 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3624 btrfs_item_nr_offset(slot),
3625 (nritems - slot) * sizeof(struct btrfs_item));
3627 /* shift the data */
3628 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3629 data_end - total_data, btrfs_leaf_data(leaf) +
3630 data_end, old_data - data_end);
3631 data_end = old_data;
3634 /* setup the item for the new data */
3635 for (i = 0; i < nr; i++) {
3636 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3637 btrfs_set_item_key(leaf, &disk_key, slot + i);
3638 item = btrfs_item_nr(leaf, slot + i);
3639 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3640 data_end -= data_size[i];
3641 btrfs_set_item_size(leaf, item, data_size[i]);
3644 btrfs_set_header_nritems(leaf, nritems + nr);
3648 struct btrfs_disk_key disk_key;
3649 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3650 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3652 btrfs_unlock_up_safe(path, 1);
3653 btrfs_mark_buffer_dirty(leaf);
3655 if (btrfs_leaf_free_space(root, leaf) < 0) {
3656 btrfs_print_leaf(root, leaf);
3663 * Given a key and some data, insert items into the tree.
3664 * This does all the path init required, making room in the tree if needed.
3666 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3667 struct btrfs_root *root,
3668 struct btrfs_path *path,
3669 struct btrfs_key *cpu_key, u32 *data_size,
3678 for (i = 0; i < nr; i++)
3679 total_data += data_size[i];
3681 total_size = total_data + (nr * sizeof(struct btrfs_item));
3682 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3688 slot = path->slots[0];
3691 ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
3692 total_data, total_size, nr);
3699 * Given a key and some data, insert an item into the tree.
3700 * This does all the path init required, making room in the tree if needed.
3702 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3703 *root, struct btrfs_key *cpu_key, void *data, u32
3707 struct btrfs_path *path;
3708 struct extent_buffer *leaf;
3711 path = btrfs_alloc_path();
3713 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
3715 leaf = path->nodes[0];
3716 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3717 write_extent_buffer(leaf, data, ptr, data_size);
3718 btrfs_mark_buffer_dirty(leaf);
3720 btrfs_free_path(path);
3725 * delete the pointer from a given node.
3727 * the tree should have been previously balanced so the deletion does not
3730 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3731 struct btrfs_path *path, int level, int slot)
3733 struct extent_buffer *parent = path->nodes[level];
3738 nritems = btrfs_header_nritems(parent);
3739 if (slot != nritems - 1) {
3740 memmove_extent_buffer(parent,
3741 btrfs_node_key_ptr_offset(slot),
3742 btrfs_node_key_ptr_offset(slot + 1),
3743 sizeof(struct btrfs_key_ptr) *
3744 (nritems - slot - 1));
3747 btrfs_set_header_nritems(parent, nritems);
3748 if (nritems == 0 && parent == root->node) {
3749 BUG_ON(btrfs_header_level(root->node) != 1);
3750 /* just turn the root into a leaf and break */
3751 btrfs_set_header_level(root->node, 0);
3752 } else if (slot == 0) {
3753 struct btrfs_disk_key disk_key;
3755 btrfs_node_key(parent, &disk_key, 0);
3756 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
3760 btrfs_mark_buffer_dirty(parent);
3765 * a helper function to delete the leaf pointed to by path->slots[1] and
3768 * This deletes the pointer in path->nodes[1] and frees the leaf
3769 * block extent. zero is returned if it all worked out, < 0 otherwise.
3771 * The path must have already been setup for deleting the leaf, including
3772 * all the proper balancing. path->nodes[1] must be locked.
3774 static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3775 struct btrfs_root *root,
3776 struct btrfs_path *path,
3777 struct extent_buffer *leaf)
3781 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
3782 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3787 * btrfs_free_extent is expensive, we want to make sure we
3788 * aren't holding any locks when we call it
3790 btrfs_unlock_up_safe(path, 0);
3792 root_sub_used(root, leaf->len);
3794 btrfs_free_tree_block(trans, root, leaf, 0, 1);
3798 * delete the item at the leaf level in path. If that empties
3799 * the leaf, remove it from the tree
3801 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3802 struct btrfs_path *path, int slot, int nr)
3804 struct extent_buffer *leaf;
3805 struct btrfs_item *item;
3813 leaf = path->nodes[0];
3814 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3816 for (i = 0; i < nr; i++)
3817 dsize += btrfs_item_size_nr(leaf, slot + i);
3819 nritems = btrfs_header_nritems(leaf);
3821 if (slot + nr != nritems) {
3822 int data_end = leaf_data_end(root, leaf);
3824 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3826 btrfs_leaf_data(leaf) + data_end,
3827 last_off - data_end);
3829 for (i = slot + nr; i < nritems; i++) {
3832 item = btrfs_item_nr(leaf, i);
3833 if (!leaf->map_token) {
3834 map_extent_buffer(leaf, (unsigned long)item,
3835 sizeof(struct btrfs_item),
3836 &leaf->map_token, &leaf->kaddr,
3837 &leaf->map_start, &leaf->map_len,
3840 ioff = btrfs_item_offset(leaf, item);
3841 btrfs_set_item_offset(leaf, item, ioff + dsize);
3844 if (leaf->map_token) {
3845 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3846 leaf->map_token = NULL;
3849 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
3850 btrfs_item_nr_offset(slot + nr),
3851 sizeof(struct btrfs_item) *
3852 (nritems - slot - nr));
3854 btrfs_set_header_nritems(leaf, nritems - nr);
3857 /* delete the leaf if we've emptied it */
3859 if (leaf == root->node) {
3860 btrfs_set_header_level(leaf, 0);
3862 btrfs_set_path_blocking(path);
3863 clean_tree_block(trans, root, leaf);
3864 ret = btrfs_del_leaf(trans, root, path, leaf);
3868 int used = leaf_space_used(leaf, 0, nritems);
3870 struct btrfs_disk_key disk_key;
3872 btrfs_item_key(leaf, &disk_key, 0);
3873 wret = fixup_low_keys(trans, root, path,
3879 /* delete the leaf if it is mostly empty */
3880 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
3881 /* push_leaf_left fixes the path.
3882 * make sure the path still points to our leaf
3883 * for possible call to del_ptr below
3885 slot = path->slots[1];
3886 extent_buffer_get(leaf);
3888 btrfs_set_path_blocking(path);
3889 wret = push_leaf_left(trans, root, path, 1, 1,
3891 if (wret < 0 && wret != -ENOSPC)
3894 if (path->nodes[0] == leaf &&
3895 btrfs_header_nritems(leaf)) {
3896 wret = push_leaf_right(trans, root, path, 1,
3898 if (wret < 0 && wret != -ENOSPC)
3902 if (btrfs_header_nritems(leaf) == 0) {
3903 path->slots[1] = slot;
3904 ret = btrfs_del_leaf(trans, root, path, leaf);
3906 free_extent_buffer(leaf);
3908 /* if we're still in the path, make sure
3909 * we're dirty. Otherwise, one of the
3910 * push_leaf functions must have already
3911 * dirtied this buffer
3913 if (path->nodes[0] == leaf)
3914 btrfs_mark_buffer_dirty(leaf);
3915 free_extent_buffer(leaf);
3918 btrfs_mark_buffer_dirty(leaf);
3925 * search the tree again to find a leaf with lesser keys
3926 * returns 0 if it found something or 1 if there are no lesser leaves.
3927 * returns < 0 on io errors.
3929 * This may release the path, and so you may lose any locks held at the
3932 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3934 struct btrfs_key key;
3935 struct btrfs_disk_key found_key;
3938 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
3942 else if (key.type > 0)
3944 else if (key.objectid > 0)
3949 btrfs_release_path(root, path);
3950 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3953 btrfs_item_key(path->nodes[0], &found_key, 0);
3954 ret = comp_keys(&found_key, &key);
3961 * A helper function to walk down the tree starting at min_key, and looking
3962 * for nodes or leaves that are either in cache or have a minimum
3963 * transaction id. This is used by the btree defrag code, and tree logging
3965 * This does not cow, but it does stuff the starting key it finds back
3966 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3967 * key and get a writable path.
3969 * This does lock as it descends, and path->keep_locks should be set
3970 * to 1 by the caller.
3972 * This honors path->lowest_level to prevent descent past a given level
3975 * min_trans indicates the oldest transaction that you are interested
3976 * in walking through. Any nodes or leaves older than min_trans are
3977 * skipped over (without reading them).
3979 * returns zero if something useful was found, < 0 on error and 1 if there
3980 * was nothing in the tree that matched the search criteria.
3982 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
3983 struct btrfs_key *max_key,
3984 struct btrfs_path *path, int cache_only,
3987 struct extent_buffer *cur;
3988 struct btrfs_key found_key;
3995 WARN_ON(!path->keep_locks);
3997 cur = btrfs_lock_root_node(root);
3998 level = btrfs_header_level(cur);
3999 WARN_ON(path->nodes[level]);
4000 path->nodes[level] = cur;
4001 path->locks[level] = 1;
4003 if (btrfs_header_generation(cur) < min_trans) {
4008 nritems = btrfs_header_nritems(cur);
4009 level = btrfs_header_level(cur);
4010 sret = bin_search(cur, min_key, level, &slot);
4012 /* at the lowest level, we're done, setup the path and exit */
4013 if (level == path->lowest_level) {
4014 if (slot >= nritems)
4017 path->slots[level] = slot;
4018 btrfs_item_key_to_cpu(cur, &found_key, slot);
4021 if (sret && slot > 0)
4024 * check this node pointer against the cache_only and
4025 * min_trans parameters. If it isn't in cache or is too
4026 * old, skip to the next one.
4028 while (slot < nritems) {
4031 struct extent_buffer *tmp;
4032 struct btrfs_disk_key disk_key;
4034 blockptr = btrfs_node_blockptr(cur, slot);
4035 gen = btrfs_node_ptr_generation(cur, slot);
4036 if (gen < min_trans) {
4044 btrfs_node_key(cur, &disk_key, slot);
4045 if (comp_keys(&disk_key, max_key) >= 0) {
4051 tmp = btrfs_find_tree_block(root, blockptr,
4052 btrfs_level_size(root, level - 1));
4054 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
4055 free_extent_buffer(tmp);
4059 free_extent_buffer(tmp);
4064 * we didn't find a candidate key in this node, walk forward
4065 * and find another one
4067 if (slot >= nritems) {
4068 path->slots[level] = slot;
4069 btrfs_set_path_blocking(path);
4070 sret = btrfs_find_next_key(root, path, min_key, level,
4071 cache_only, min_trans);
4073 btrfs_release_path(root, path);
4079 /* save our key for returning back */
4080 btrfs_node_key_to_cpu(cur, &found_key, slot);
4081 path->slots[level] = slot;
4082 if (level == path->lowest_level) {
4084 unlock_up(path, level, 1);
4087 btrfs_set_path_blocking(path);
4088 cur = read_node_slot(root, cur, slot);
4090 btrfs_tree_lock(cur);
4092 path->locks[level - 1] = 1;
4093 path->nodes[level - 1] = cur;
4094 unlock_up(path, level, 1);
4095 btrfs_clear_path_blocking(path, NULL);
4099 memcpy(min_key, &found_key, sizeof(found_key));
4100 btrfs_set_path_blocking(path);
4105 * this is similar to btrfs_next_leaf, but does not try to preserve
4106 * and fixup the path. It looks for and returns the next key in the
4107 * tree based on the current path and the cache_only and min_trans
4110 * 0 is returned if another key is found, < 0 if there are any errors
4111 * and 1 is returned if there are no higher keys in the tree
4113 * path->keep_locks should be set to 1 on the search made before
4114 * calling this function.
4116 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
4117 struct btrfs_key *key, int level,
4118 int cache_only, u64 min_trans)
4121 struct extent_buffer *c;
4123 WARN_ON(!path->keep_locks);
4124 while (level < BTRFS_MAX_LEVEL) {
4125 if (!path->nodes[level])
4128 slot = path->slots[level] + 1;
4129 c = path->nodes[level];
4131 if (slot >= btrfs_header_nritems(c)) {
4134 struct btrfs_key cur_key;
4135 if (level + 1 >= BTRFS_MAX_LEVEL ||
4136 !path->nodes[level + 1])
4139 if (path->locks[level + 1]) {
4144 slot = btrfs_header_nritems(c) - 1;
4146 btrfs_item_key_to_cpu(c, &cur_key, slot);
4148 btrfs_node_key_to_cpu(c, &cur_key, slot);
4150 orig_lowest = path->lowest_level;
4151 btrfs_release_path(root, path);
4152 path->lowest_level = level;
4153 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4155 path->lowest_level = orig_lowest;
4159 c = path->nodes[level];
4160 slot = path->slots[level];
4167 btrfs_item_key_to_cpu(c, key, slot);
4169 u64 blockptr = btrfs_node_blockptr(c, slot);
4170 u64 gen = btrfs_node_ptr_generation(c, slot);
4173 struct extent_buffer *cur;
4174 cur = btrfs_find_tree_block(root, blockptr,
4175 btrfs_level_size(root, level - 1));
4176 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4179 free_extent_buffer(cur);
4182 free_extent_buffer(cur);
4184 if (gen < min_trans) {
4188 btrfs_node_key_to_cpu(c, key, slot);
4196 * search the tree again to find a leaf with greater keys
4197 * returns 0 if it found something or 1 if there are no greater leaves.
4198 * returns < 0 on io errors.
4200 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
4204 struct extent_buffer *c;
4205 struct extent_buffer *next;
4206 struct btrfs_key key;
4209 int old_spinning = path->leave_spinning;
4210 int force_blocking = 0;
4212 nritems = btrfs_header_nritems(path->nodes[0]);
4217 * we take the blocks in an order that upsets lockdep. Using
4218 * blocking mode is the only way around it.
4220 #ifdef CONFIG_DEBUG_LOCK_ALLOC
4224 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4228 btrfs_release_path(root, path);
4230 path->keep_locks = 1;
4232 if (!force_blocking)
4233 path->leave_spinning = 1;
4235 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4236 path->keep_locks = 0;
4241 nritems = btrfs_header_nritems(path->nodes[0]);
4243 * by releasing the path above we dropped all our locks. A balance
4244 * could have added more items next to the key that used to be
4245 * at the very end of the block. So, check again here and
4246 * advance the path if there are now more items available.
4248 if (nritems > 0 && path->slots[0] < nritems - 1) {
4255 while (level < BTRFS_MAX_LEVEL) {
4256 if (!path->nodes[level]) {
4261 slot = path->slots[level] + 1;
4262 c = path->nodes[level];
4263 if (slot >= btrfs_header_nritems(c)) {
4265 if (level == BTRFS_MAX_LEVEL) {
4273 btrfs_tree_unlock(next);
4274 free_extent_buffer(next);
4278 ret = read_block_for_search(NULL, root, path, &next, level,
4284 btrfs_release_path(root, path);
4288 if (!path->skip_locking) {
4289 ret = btrfs_try_spin_lock(next);
4291 btrfs_set_path_blocking(path);
4292 btrfs_tree_lock(next);
4293 if (!force_blocking)
4294 btrfs_clear_path_blocking(path, next);
4297 btrfs_set_lock_blocking(next);
4301 path->slots[level] = slot;
4304 c = path->nodes[level];
4305 if (path->locks[level])
4306 btrfs_tree_unlock(c);
4308 free_extent_buffer(c);
4309 path->nodes[level] = next;
4310 path->slots[level] = 0;
4311 if (!path->skip_locking)
4312 path->locks[level] = 1;
4317 ret = read_block_for_search(NULL, root, path, &next, level,
4323 btrfs_release_path(root, path);
4327 if (!path->skip_locking) {
4328 btrfs_assert_tree_locked(path->nodes[level]);
4329 ret = btrfs_try_spin_lock(next);
4331 btrfs_set_path_blocking(path);
4332 btrfs_tree_lock(next);
4333 if (!force_blocking)
4334 btrfs_clear_path_blocking(path, next);
4337 btrfs_set_lock_blocking(next);
4342 unlock_up(path, 0, 1);
4343 path->leave_spinning = old_spinning;
4345 btrfs_set_path_blocking(path);
4351 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4352 * searching until it gets past min_objectid or finds an item of 'type'
4354 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4356 int btrfs_previous_item(struct btrfs_root *root,
4357 struct btrfs_path *path, u64 min_objectid,
4360 struct btrfs_key found_key;
4361 struct extent_buffer *leaf;
4366 if (path->slots[0] == 0) {
4367 btrfs_set_path_blocking(path);
4368 ret = btrfs_prev_leaf(root, path);
4374 leaf = path->nodes[0];
4375 nritems = btrfs_header_nritems(leaf);
4378 if (path->slots[0] == nritems)
4381 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4382 if (found_key.objectid < min_objectid)
4384 if (found_key.type == type)
4386 if (found_key.objectid == min_objectid &&
4387 found_key.type < type)