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 void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
40 struct btrfs_path *path, int level, int slot);
42 struct btrfs_path *btrfs_alloc_path(void)
44 struct btrfs_path *path;
45 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
50 * set all locked nodes in the path to blocking locks. This should
51 * be done before scheduling
53 noinline void btrfs_set_path_blocking(struct btrfs_path *p)
56 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
57 if (!p->nodes[i] || !p->locks[i])
59 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
60 if (p->locks[i] == BTRFS_READ_LOCK)
61 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
62 else if (p->locks[i] == BTRFS_WRITE_LOCK)
63 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
68 * reset all the locked nodes in the patch to spinning locks.
70 * held is used to keep lockdep happy, when lockdep is enabled
71 * we set held to a blocking lock before we go around and
72 * retake all the spinlocks in the path. You can safely use NULL
75 noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
76 struct extent_buffer *held, int held_rw)
80 #ifdef CONFIG_DEBUG_LOCK_ALLOC
81 /* lockdep really cares that we take all of these spinlocks
82 * in the right order. If any of the locks in the path are not
83 * currently blocking, it is going to complain. So, make really
84 * really sure by forcing the path to blocking before we clear
88 btrfs_set_lock_blocking_rw(held, held_rw);
89 if (held_rw == BTRFS_WRITE_LOCK)
90 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
91 else if (held_rw == BTRFS_READ_LOCK)
92 held_rw = BTRFS_READ_LOCK_BLOCKING;
94 btrfs_set_path_blocking(p);
97 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
98 if (p->nodes[i] && p->locks[i]) {
99 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
100 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
101 p->locks[i] = BTRFS_WRITE_LOCK;
102 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
103 p->locks[i] = BTRFS_READ_LOCK;
107 #ifdef CONFIG_DEBUG_LOCK_ALLOC
109 btrfs_clear_lock_blocking_rw(held, held_rw);
113 /* this also releases the path */
114 void btrfs_free_path(struct btrfs_path *p)
118 btrfs_release_path(p);
119 kmem_cache_free(btrfs_path_cachep, p);
123 * path release drops references on the extent buffers in the path
124 * and it drops any locks held by this path
126 * It is safe to call this on paths that no locks or extent buffers held.
128 noinline void btrfs_release_path(struct btrfs_path *p)
132 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
137 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
140 free_extent_buffer(p->nodes[i]);
146 * safely gets a reference on the root node of a tree. A lock
147 * is not taken, so a concurrent writer may put a different node
148 * at the root of the tree. See btrfs_lock_root_node for the
151 * The extent buffer returned by this has a reference taken, so
152 * it won't disappear. It may stop being the root of the tree
153 * at any time because there are no locks held.
155 struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
157 struct extent_buffer *eb;
161 eb = rcu_dereference(root->node);
164 * RCU really hurts here, we could free up the root node because
165 * it was cow'ed but we may not get the new root node yet so do
166 * the inc_not_zero dance and if it doesn't work then
167 * synchronize_rcu and try again.
169 if (atomic_inc_not_zero(&eb->refs)) {
179 /* loop around taking references on and locking the root node of the
180 * tree until you end up with a lock on the root. A locked buffer
181 * is returned, with a reference held.
183 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
185 struct extent_buffer *eb;
188 eb = btrfs_root_node(root);
190 if (eb == root->node)
192 btrfs_tree_unlock(eb);
193 free_extent_buffer(eb);
198 /* loop around taking references on and locking the root node of the
199 * tree until you end up with a lock on the root. A locked buffer
200 * is returned, with a reference held.
202 struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
204 struct extent_buffer *eb;
207 eb = btrfs_root_node(root);
208 btrfs_tree_read_lock(eb);
209 if (eb == root->node)
211 btrfs_tree_read_unlock(eb);
212 free_extent_buffer(eb);
217 /* cowonly root (everything not a reference counted cow subvolume), just get
218 * put onto a simple dirty list. transaction.c walks this to make sure they
219 * get properly updated on disk.
221 static void add_root_to_dirty_list(struct btrfs_root *root)
223 spin_lock(&root->fs_info->trans_lock);
224 if (root->track_dirty && list_empty(&root->dirty_list)) {
225 list_add(&root->dirty_list,
226 &root->fs_info->dirty_cowonly_roots);
228 spin_unlock(&root->fs_info->trans_lock);
232 * used by snapshot creation to make a copy of a root for a tree with
233 * a given objectid. The buffer with the new root node is returned in
234 * cow_ret, and this func returns zero on success or a negative error code.
236 int btrfs_copy_root(struct btrfs_trans_handle *trans,
237 struct btrfs_root *root,
238 struct extent_buffer *buf,
239 struct extent_buffer **cow_ret, u64 new_root_objectid)
241 struct extent_buffer *cow;
244 struct btrfs_disk_key disk_key;
246 WARN_ON(root->ref_cows && trans->transid !=
247 root->fs_info->running_transaction->transid);
248 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
250 level = btrfs_header_level(buf);
252 btrfs_item_key(buf, &disk_key, 0);
254 btrfs_node_key(buf, &disk_key, 0);
256 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
257 new_root_objectid, &disk_key, level,
262 copy_extent_buffer(cow, buf, 0, 0, cow->len);
263 btrfs_set_header_bytenr(cow, cow->start);
264 btrfs_set_header_generation(cow, trans->transid);
265 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
266 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
267 BTRFS_HEADER_FLAG_RELOC);
268 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
269 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
271 btrfs_set_header_owner(cow, new_root_objectid);
273 write_extent_buffer(cow, root->fs_info->fsid,
274 (unsigned long)btrfs_header_fsid(cow),
277 WARN_ON(btrfs_header_generation(buf) > trans->transid);
278 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
279 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
281 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
286 btrfs_mark_buffer_dirty(cow);
292 * check if the tree block can be shared by multiple trees
294 int btrfs_block_can_be_shared(struct btrfs_root *root,
295 struct extent_buffer *buf)
298 * Tree blocks not in refernece counted trees and tree roots
299 * are never shared. If a block was allocated after the last
300 * snapshot and the block was not allocated by tree relocation,
301 * we know the block is not shared.
303 if (root->ref_cows &&
304 buf != root->node && buf != root->commit_root &&
305 (btrfs_header_generation(buf) <=
306 btrfs_root_last_snapshot(&root->root_item) ||
307 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
309 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
310 if (root->ref_cows &&
311 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
317 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
318 struct btrfs_root *root,
319 struct extent_buffer *buf,
320 struct extent_buffer *cow,
330 * Backrefs update rules:
332 * Always use full backrefs for extent pointers in tree block
333 * allocated by tree relocation.
335 * If a shared tree block is no longer referenced by its owner
336 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
337 * use full backrefs for extent pointers in tree block.
339 * If a tree block is been relocating
340 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
341 * use full backrefs for extent pointers in tree block.
342 * The reason for this is some operations (such as drop tree)
343 * are only allowed for blocks use full backrefs.
346 if (btrfs_block_can_be_shared(root, buf)) {
347 ret = btrfs_lookup_extent_info(trans, root, buf->start,
348 buf->len, &refs, &flags);
353 btrfs_std_error(root->fs_info, ret);
358 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
359 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
360 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
365 owner = btrfs_header_owner(buf);
366 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
367 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
370 if ((owner == root->root_key.objectid ||
371 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
372 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
373 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
374 BUG_ON(ret); /* -ENOMEM */
376 if (root->root_key.objectid ==
377 BTRFS_TREE_RELOC_OBJECTID) {
378 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
379 BUG_ON(ret); /* -ENOMEM */
380 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
381 BUG_ON(ret); /* -ENOMEM */
383 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
386 if (root->root_key.objectid ==
387 BTRFS_TREE_RELOC_OBJECTID)
388 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
390 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
391 BUG_ON(ret); /* -ENOMEM */
393 if (new_flags != 0) {
394 ret = btrfs_set_disk_extent_flags(trans, root,
402 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
403 if (root->root_key.objectid ==
404 BTRFS_TREE_RELOC_OBJECTID)
405 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
407 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
408 BUG_ON(ret); /* -ENOMEM */
409 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
410 BUG_ON(ret); /* -ENOMEM */
412 clean_tree_block(trans, root, buf);
419 * does the dirty work in cow of a single block. The parent block (if
420 * supplied) is updated to point to the new cow copy. The new buffer is marked
421 * dirty and returned locked. If you modify the block it needs to be marked
424 * search_start -- an allocation hint for the new block
426 * empty_size -- a hint that you plan on doing more cow. This is the size in
427 * bytes the allocator should try to find free next to the block it returns.
428 * This is just a hint and may be ignored by the allocator.
430 static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
431 struct btrfs_root *root,
432 struct extent_buffer *buf,
433 struct extent_buffer *parent, int parent_slot,
434 struct extent_buffer **cow_ret,
435 u64 search_start, u64 empty_size)
437 struct btrfs_disk_key disk_key;
438 struct extent_buffer *cow;
447 btrfs_assert_tree_locked(buf);
449 WARN_ON(root->ref_cows && trans->transid !=
450 root->fs_info->running_transaction->transid);
451 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
453 level = btrfs_header_level(buf);
456 btrfs_item_key(buf, &disk_key, 0);
458 btrfs_node_key(buf, &disk_key, 0);
460 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
462 parent_start = parent->start;
468 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
469 root->root_key.objectid, &disk_key,
470 level, search_start, empty_size, 1);
474 /* cow is set to blocking by btrfs_init_new_buffer */
476 copy_extent_buffer(cow, buf, 0, 0, cow->len);
477 btrfs_set_header_bytenr(cow, cow->start);
478 btrfs_set_header_generation(cow, trans->transid);
479 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
480 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
481 BTRFS_HEADER_FLAG_RELOC);
482 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
483 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
485 btrfs_set_header_owner(cow, root->root_key.objectid);
487 write_extent_buffer(cow, root->fs_info->fsid,
488 (unsigned long)btrfs_header_fsid(cow),
491 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
493 btrfs_abort_transaction(trans, root, ret);
498 btrfs_reloc_cow_block(trans, root, buf, cow);
500 if (buf == root->node) {
501 WARN_ON(parent && parent != buf);
502 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
503 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
504 parent_start = buf->start;
508 extent_buffer_get(cow);
509 rcu_assign_pointer(root->node, cow);
511 btrfs_free_tree_block(trans, root, buf, parent_start,
513 free_extent_buffer(buf);
514 add_root_to_dirty_list(root);
516 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
517 parent_start = parent->start;
521 WARN_ON(trans->transid != btrfs_header_generation(parent));
522 btrfs_set_node_blockptr(parent, parent_slot,
524 btrfs_set_node_ptr_generation(parent, parent_slot,
526 btrfs_mark_buffer_dirty(parent);
527 btrfs_free_tree_block(trans, root, buf, parent_start,
531 btrfs_tree_unlock(buf);
532 free_extent_buffer_stale(buf);
533 btrfs_mark_buffer_dirty(cow);
538 static inline int should_cow_block(struct btrfs_trans_handle *trans,
539 struct btrfs_root *root,
540 struct extent_buffer *buf)
542 /* ensure we can see the force_cow */
546 * We do not need to cow a block if
547 * 1) this block is not created or changed in this transaction;
548 * 2) this block does not belong to TREE_RELOC tree;
549 * 3) the root is not forced COW.
551 * What is forced COW:
552 * when we create snapshot during commiting the transaction,
553 * after we've finished coping src root, we must COW the shared
554 * block to ensure the metadata consistency.
556 if (btrfs_header_generation(buf) == trans->transid &&
557 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
558 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
559 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
566 * cows a single block, see __btrfs_cow_block for the real work.
567 * This version of it has extra checks so that a block isn't cow'd more than
568 * once per transaction, as long as it hasn't been written yet
570 noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
571 struct btrfs_root *root, struct extent_buffer *buf,
572 struct extent_buffer *parent, int parent_slot,
573 struct extent_buffer **cow_ret)
578 if (trans->transaction != root->fs_info->running_transaction) {
579 printk(KERN_CRIT "trans %llu running %llu\n",
580 (unsigned long long)trans->transid,
582 root->fs_info->running_transaction->transid);
585 if (trans->transid != root->fs_info->generation) {
586 printk(KERN_CRIT "trans %llu running %llu\n",
587 (unsigned long long)trans->transid,
588 (unsigned long long)root->fs_info->generation);
592 if (!should_cow_block(trans, root, buf)) {
597 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
600 btrfs_set_lock_blocking(parent);
601 btrfs_set_lock_blocking(buf);
603 ret = __btrfs_cow_block(trans, root, buf, parent,
604 parent_slot, cow_ret, search_start, 0);
606 trace_btrfs_cow_block(root, buf, *cow_ret);
612 * helper function for defrag to decide if two blocks pointed to by a
613 * node are actually close by
615 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
617 if (blocknr < other && other - (blocknr + blocksize) < 32768)
619 if (blocknr > other && blocknr - (other + blocksize) < 32768)
625 * compare two keys in a memcmp fashion
627 static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
631 btrfs_disk_key_to_cpu(&k1, disk);
633 return btrfs_comp_cpu_keys(&k1, k2);
637 * same as comp_keys only with two btrfs_key's
639 int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
641 if (k1->objectid > k2->objectid)
643 if (k1->objectid < k2->objectid)
645 if (k1->type > k2->type)
647 if (k1->type < k2->type)
649 if (k1->offset > k2->offset)
651 if (k1->offset < k2->offset)
657 * this is used by the defrag code to go through all the
658 * leaves pointed to by a node and reallocate them so that
659 * disk order is close to key order
661 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
662 struct btrfs_root *root, struct extent_buffer *parent,
663 int start_slot, int cache_only, u64 *last_ret,
664 struct btrfs_key *progress)
666 struct extent_buffer *cur;
669 u64 search_start = *last_ret;
679 int progress_passed = 0;
680 struct btrfs_disk_key disk_key;
682 parent_level = btrfs_header_level(parent);
683 if (cache_only && parent_level != 1)
686 if (trans->transaction != root->fs_info->running_transaction)
688 if (trans->transid != root->fs_info->generation)
691 parent_nritems = btrfs_header_nritems(parent);
692 blocksize = btrfs_level_size(root, parent_level - 1);
693 end_slot = parent_nritems;
695 if (parent_nritems == 1)
698 btrfs_set_lock_blocking(parent);
700 for (i = start_slot; i < end_slot; i++) {
703 btrfs_node_key(parent, &disk_key, i);
704 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
708 blocknr = btrfs_node_blockptr(parent, i);
709 gen = btrfs_node_ptr_generation(parent, i);
711 last_block = blocknr;
714 other = btrfs_node_blockptr(parent, i - 1);
715 close = close_blocks(blocknr, other, blocksize);
717 if (!close && i < end_slot - 2) {
718 other = btrfs_node_blockptr(parent, i + 1);
719 close = close_blocks(blocknr, other, blocksize);
722 last_block = blocknr;
726 cur = btrfs_find_tree_block(root, blocknr, blocksize);
728 uptodate = btrfs_buffer_uptodate(cur, gen);
731 if (!cur || !uptodate) {
733 free_extent_buffer(cur);
737 cur = read_tree_block(root, blocknr,
741 } else if (!uptodate) {
742 btrfs_read_buffer(cur, gen);
745 if (search_start == 0)
746 search_start = last_block;
748 btrfs_tree_lock(cur);
749 btrfs_set_lock_blocking(cur);
750 err = __btrfs_cow_block(trans, root, cur, parent, i,
753 (end_slot - i) * blocksize));
755 btrfs_tree_unlock(cur);
756 free_extent_buffer(cur);
759 search_start = cur->start;
760 last_block = cur->start;
761 *last_ret = search_start;
762 btrfs_tree_unlock(cur);
763 free_extent_buffer(cur);
769 * The leaf data grows from end-to-front in the node.
770 * this returns the address of the start of the last item,
771 * which is the stop of the leaf data stack
773 static inline unsigned int leaf_data_end(struct btrfs_root *root,
774 struct extent_buffer *leaf)
776 u32 nr = btrfs_header_nritems(leaf);
778 return BTRFS_LEAF_DATA_SIZE(root);
779 return btrfs_item_offset_nr(leaf, nr - 1);
784 * search for key in the extent_buffer. The items start at offset p,
785 * and they are item_size apart. There are 'max' items in p.
787 * the slot in the array is returned via slot, and it points to
788 * the place where you would insert key if it is not found in
791 * slot may point to max if the key is bigger than all of the keys
793 static noinline int generic_bin_search(struct extent_buffer *eb,
795 int item_size, struct btrfs_key *key,
802 struct btrfs_disk_key *tmp = NULL;
803 struct btrfs_disk_key unaligned;
804 unsigned long offset;
806 unsigned long map_start = 0;
807 unsigned long map_len = 0;
811 mid = (low + high) / 2;
812 offset = p + mid * item_size;
814 if (!kaddr || offset < map_start ||
815 (offset + sizeof(struct btrfs_disk_key)) >
816 map_start + map_len) {
818 err = map_private_extent_buffer(eb, offset,
819 sizeof(struct btrfs_disk_key),
820 &kaddr, &map_start, &map_len);
823 tmp = (struct btrfs_disk_key *)(kaddr + offset -
826 read_extent_buffer(eb, &unaligned,
827 offset, sizeof(unaligned));
832 tmp = (struct btrfs_disk_key *)(kaddr + offset -
835 ret = comp_keys(tmp, key);
851 * simple bin_search frontend that does the right thing for
854 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
855 int level, int *slot)
858 return generic_bin_search(eb,
859 offsetof(struct btrfs_leaf, items),
860 sizeof(struct btrfs_item),
861 key, btrfs_header_nritems(eb),
864 return generic_bin_search(eb,
865 offsetof(struct btrfs_node, ptrs),
866 sizeof(struct btrfs_key_ptr),
867 key, btrfs_header_nritems(eb),
873 int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
874 int level, int *slot)
876 return bin_search(eb, key, level, slot);
879 static void root_add_used(struct btrfs_root *root, u32 size)
881 spin_lock(&root->accounting_lock);
882 btrfs_set_root_used(&root->root_item,
883 btrfs_root_used(&root->root_item) + size);
884 spin_unlock(&root->accounting_lock);
887 static void root_sub_used(struct btrfs_root *root, u32 size)
889 spin_lock(&root->accounting_lock);
890 btrfs_set_root_used(&root->root_item,
891 btrfs_root_used(&root->root_item) - size);
892 spin_unlock(&root->accounting_lock);
895 /* given a node and slot number, this reads the blocks it points to. The
896 * extent buffer is returned with a reference taken (but unlocked).
897 * NULL is returned on error.
899 static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
900 struct extent_buffer *parent, int slot)
902 int level = btrfs_header_level(parent);
905 if (slot >= btrfs_header_nritems(parent))
910 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
911 btrfs_level_size(root, level - 1),
912 btrfs_node_ptr_generation(parent, slot));
916 * node level balancing, used to make sure nodes are in proper order for
917 * item deletion. We balance from the top down, so we have to make sure
918 * that a deletion won't leave an node completely empty later on.
920 static noinline int balance_level(struct btrfs_trans_handle *trans,
921 struct btrfs_root *root,
922 struct btrfs_path *path, int level)
924 struct extent_buffer *right = NULL;
925 struct extent_buffer *mid;
926 struct extent_buffer *left = NULL;
927 struct extent_buffer *parent = NULL;
931 int orig_slot = path->slots[level];
937 mid = path->nodes[level];
939 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
940 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
941 WARN_ON(btrfs_header_generation(mid) != trans->transid);
943 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
945 if (level < BTRFS_MAX_LEVEL - 1) {
946 parent = path->nodes[level + 1];
947 pslot = path->slots[level + 1];
951 * deal with the case where there is only one pointer in the root
952 * by promoting the node below to a root
955 struct extent_buffer *child;
957 if (btrfs_header_nritems(mid) != 1)
960 /* promote the child to a root */
961 child = read_node_slot(root, mid, 0);
964 btrfs_std_error(root->fs_info, ret);
968 btrfs_tree_lock(child);
969 btrfs_set_lock_blocking(child);
970 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
972 btrfs_tree_unlock(child);
973 free_extent_buffer(child);
977 rcu_assign_pointer(root->node, child);
979 add_root_to_dirty_list(root);
980 btrfs_tree_unlock(child);
982 path->locks[level] = 0;
983 path->nodes[level] = NULL;
984 clean_tree_block(trans, root, mid);
985 btrfs_tree_unlock(mid);
986 /* once for the path */
987 free_extent_buffer(mid);
989 root_sub_used(root, mid->len);
990 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
991 /* once for the root ptr */
992 free_extent_buffer_stale(mid);
995 if (btrfs_header_nritems(mid) >
996 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
999 btrfs_header_nritems(mid);
1001 left = read_node_slot(root, parent, pslot - 1);
1003 btrfs_tree_lock(left);
1004 btrfs_set_lock_blocking(left);
1005 wret = btrfs_cow_block(trans, root, left,
1006 parent, pslot - 1, &left);
1012 right = read_node_slot(root, parent, pslot + 1);
1014 btrfs_tree_lock(right);
1015 btrfs_set_lock_blocking(right);
1016 wret = btrfs_cow_block(trans, root, right,
1017 parent, pslot + 1, &right);
1024 /* first, try to make some room in the middle buffer */
1026 orig_slot += btrfs_header_nritems(left);
1027 wret = push_node_left(trans, root, left, mid, 1);
1030 btrfs_header_nritems(mid);
1034 * then try to empty the right most buffer into the middle
1037 wret = push_node_left(trans, root, mid, right, 1);
1038 if (wret < 0 && wret != -ENOSPC)
1040 if (btrfs_header_nritems(right) == 0) {
1041 clean_tree_block(trans, root, right);
1042 btrfs_tree_unlock(right);
1043 del_ptr(trans, root, path, level + 1, pslot + 1);
1044 root_sub_used(root, right->len);
1045 btrfs_free_tree_block(trans, root, right, 0, 1, 0);
1046 free_extent_buffer_stale(right);
1049 struct btrfs_disk_key right_key;
1050 btrfs_node_key(right, &right_key, 0);
1051 btrfs_set_node_key(parent, &right_key, pslot + 1);
1052 btrfs_mark_buffer_dirty(parent);
1055 if (btrfs_header_nritems(mid) == 1) {
1057 * we're not allowed to leave a node with one item in the
1058 * tree during a delete. A deletion from lower in the tree
1059 * could try to delete the only pointer in this node.
1060 * So, pull some keys from the left.
1061 * There has to be a left pointer at this point because
1062 * otherwise we would have pulled some pointers from the
1067 btrfs_std_error(root->fs_info, ret);
1070 wret = balance_node_right(trans, root, mid, left);
1076 wret = push_node_left(trans, root, left, mid, 1);
1082 if (btrfs_header_nritems(mid) == 0) {
1083 clean_tree_block(trans, root, mid);
1084 btrfs_tree_unlock(mid);
1085 del_ptr(trans, root, path, level + 1, pslot);
1086 root_sub_used(root, mid->len);
1087 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
1088 free_extent_buffer_stale(mid);
1091 /* update the parent key to reflect our changes */
1092 struct btrfs_disk_key mid_key;
1093 btrfs_node_key(mid, &mid_key, 0);
1094 btrfs_set_node_key(parent, &mid_key, pslot);
1095 btrfs_mark_buffer_dirty(parent);
1098 /* update the path */
1100 if (btrfs_header_nritems(left) > orig_slot) {
1101 extent_buffer_get(left);
1102 /* left was locked after cow */
1103 path->nodes[level] = left;
1104 path->slots[level + 1] -= 1;
1105 path->slots[level] = orig_slot;
1107 btrfs_tree_unlock(mid);
1108 free_extent_buffer(mid);
1111 orig_slot -= btrfs_header_nritems(left);
1112 path->slots[level] = orig_slot;
1115 /* double check we haven't messed things up */
1117 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
1121 btrfs_tree_unlock(right);
1122 free_extent_buffer(right);
1125 if (path->nodes[level] != left)
1126 btrfs_tree_unlock(left);
1127 free_extent_buffer(left);
1132 /* Node balancing for insertion. Here we only split or push nodes around
1133 * when they are completely full. This is also done top down, so we
1134 * have to be pessimistic.
1136 static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1137 struct btrfs_root *root,
1138 struct btrfs_path *path, int level)
1140 struct extent_buffer *right = NULL;
1141 struct extent_buffer *mid;
1142 struct extent_buffer *left = NULL;
1143 struct extent_buffer *parent = NULL;
1147 int orig_slot = path->slots[level];
1152 mid = path->nodes[level];
1153 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1155 if (level < BTRFS_MAX_LEVEL - 1) {
1156 parent = path->nodes[level + 1];
1157 pslot = path->slots[level + 1];
1163 left = read_node_slot(root, parent, pslot - 1);
1165 /* first, try to make some room in the middle buffer */
1169 btrfs_tree_lock(left);
1170 btrfs_set_lock_blocking(left);
1172 left_nr = btrfs_header_nritems(left);
1173 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1176 ret = btrfs_cow_block(trans, root, left, parent,
1181 wret = push_node_left(trans, root,
1188 struct btrfs_disk_key disk_key;
1189 orig_slot += left_nr;
1190 btrfs_node_key(mid, &disk_key, 0);
1191 btrfs_set_node_key(parent, &disk_key, pslot);
1192 btrfs_mark_buffer_dirty(parent);
1193 if (btrfs_header_nritems(left) > orig_slot) {
1194 path->nodes[level] = left;
1195 path->slots[level + 1] -= 1;
1196 path->slots[level] = orig_slot;
1197 btrfs_tree_unlock(mid);
1198 free_extent_buffer(mid);
1201 btrfs_header_nritems(left);
1202 path->slots[level] = orig_slot;
1203 btrfs_tree_unlock(left);
1204 free_extent_buffer(left);
1208 btrfs_tree_unlock(left);
1209 free_extent_buffer(left);
1211 right = read_node_slot(root, parent, pslot + 1);
1214 * then try to empty the right most buffer into the middle
1219 btrfs_tree_lock(right);
1220 btrfs_set_lock_blocking(right);
1222 right_nr = btrfs_header_nritems(right);
1223 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1226 ret = btrfs_cow_block(trans, root, right,
1232 wret = balance_node_right(trans, root,
1239 struct btrfs_disk_key disk_key;
1241 btrfs_node_key(right, &disk_key, 0);
1242 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1243 btrfs_mark_buffer_dirty(parent);
1245 if (btrfs_header_nritems(mid) <= orig_slot) {
1246 path->nodes[level] = right;
1247 path->slots[level + 1] += 1;
1248 path->slots[level] = orig_slot -
1249 btrfs_header_nritems(mid);
1250 btrfs_tree_unlock(mid);
1251 free_extent_buffer(mid);
1253 btrfs_tree_unlock(right);
1254 free_extent_buffer(right);
1258 btrfs_tree_unlock(right);
1259 free_extent_buffer(right);
1265 * readahead one full node of leaves, finding things that are close
1266 * to the block in 'slot', and triggering ra on them.
1268 static void reada_for_search(struct btrfs_root *root,
1269 struct btrfs_path *path,
1270 int level, int slot, u64 objectid)
1272 struct extent_buffer *node;
1273 struct btrfs_disk_key disk_key;
1279 int direction = path->reada;
1280 struct extent_buffer *eb;
1288 if (!path->nodes[level])
1291 node = path->nodes[level];
1293 search = btrfs_node_blockptr(node, slot);
1294 blocksize = btrfs_level_size(root, level - 1);
1295 eb = btrfs_find_tree_block(root, search, blocksize);
1297 free_extent_buffer(eb);
1303 nritems = btrfs_header_nritems(node);
1307 if (direction < 0) {
1311 } else if (direction > 0) {
1316 if (path->reada < 0 && objectid) {
1317 btrfs_node_key(node, &disk_key, nr);
1318 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1321 search = btrfs_node_blockptr(node, nr);
1322 if ((search <= target && target - search <= 65536) ||
1323 (search > target && search - target <= 65536)) {
1324 gen = btrfs_node_ptr_generation(node, nr);
1325 readahead_tree_block(root, search, blocksize, gen);
1329 if ((nread > 65536 || nscan > 32))
1335 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1338 static noinline int reada_for_balance(struct btrfs_root *root,
1339 struct btrfs_path *path, int level)
1343 struct extent_buffer *parent;
1344 struct extent_buffer *eb;
1351 parent = path->nodes[level + 1];
1355 nritems = btrfs_header_nritems(parent);
1356 slot = path->slots[level + 1];
1357 blocksize = btrfs_level_size(root, level);
1360 block1 = btrfs_node_blockptr(parent, slot - 1);
1361 gen = btrfs_node_ptr_generation(parent, slot - 1);
1362 eb = btrfs_find_tree_block(root, block1, blocksize);
1363 if (eb && btrfs_buffer_uptodate(eb, gen))
1365 free_extent_buffer(eb);
1367 if (slot + 1 < nritems) {
1368 block2 = btrfs_node_blockptr(parent, slot + 1);
1369 gen = btrfs_node_ptr_generation(parent, slot + 1);
1370 eb = btrfs_find_tree_block(root, block2, blocksize);
1371 if (eb && btrfs_buffer_uptodate(eb, gen))
1373 free_extent_buffer(eb);
1375 if (block1 || block2) {
1378 /* release the whole path */
1379 btrfs_release_path(path);
1381 /* read the blocks */
1383 readahead_tree_block(root, block1, blocksize, 0);
1385 readahead_tree_block(root, block2, blocksize, 0);
1388 eb = read_tree_block(root, block1, blocksize, 0);
1389 free_extent_buffer(eb);
1392 eb = read_tree_block(root, block2, blocksize, 0);
1393 free_extent_buffer(eb);
1401 * when we walk down the tree, it is usually safe to unlock the higher layers
1402 * in the tree. The exceptions are when our path goes through slot 0, because
1403 * operations on the tree might require changing key pointers higher up in the
1406 * callers might also have set path->keep_locks, which tells this code to keep
1407 * the lock if the path points to the last slot in the block. This is part of
1408 * walking through the tree, and selecting the next slot in the higher block.
1410 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1411 * if lowest_unlock is 1, level 0 won't be unlocked
1413 static noinline void unlock_up(struct btrfs_path *path, int level,
1414 int lowest_unlock, int min_write_lock_level,
1415 int *write_lock_level)
1418 int skip_level = level;
1420 struct extent_buffer *t;
1422 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1423 if (!path->nodes[i])
1425 if (!path->locks[i])
1427 if (!no_skips && path->slots[i] == 0) {
1431 if (!no_skips && path->keep_locks) {
1434 nritems = btrfs_header_nritems(t);
1435 if (nritems < 1 || path->slots[i] >= nritems - 1) {
1440 if (skip_level < i && i >= lowest_unlock)
1444 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1445 btrfs_tree_unlock_rw(t, path->locks[i]);
1447 if (write_lock_level &&
1448 i > min_write_lock_level &&
1449 i <= *write_lock_level) {
1450 *write_lock_level = i - 1;
1457 * This releases any locks held in the path starting at level and
1458 * going all the way up to the root.
1460 * btrfs_search_slot will keep the lock held on higher nodes in a few
1461 * corner cases, such as COW of the block at slot zero in the node. This
1462 * ignores those rules, and it should only be called when there are no
1463 * more updates to be done higher up in the tree.
1465 noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1469 if (path->keep_locks)
1472 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1473 if (!path->nodes[i])
1475 if (!path->locks[i])
1477 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
1483 * helper function for btrfs_search_slot. The goal is to find a block
1484 * in cache without setting the path to blocking. If we find the block
1485 * we return zero and the path is unchanged.
1487 * If we can't find the block, we set the path blocking and do some
1488 * reada. -EAGAIN is returned and the search must be repeated.
1491 read_block_for_search(struct btrfs_trans_handle *trans,
1492 struct btrfs_root *root, struct btrfs_path *p,
1493 struct extent_buffer **eb_ret, int level, int slot,
1494 struct btrfs_key *key)
1499 struct extent_buffer *b = *eb_ret;
1500 struct extent_buffer *tmp;
1503 blocknr = btrfs_node_blockptr(b, slot);
1504 gen = btrfs_node_ptr_generation(b, slot);
1505 blocksize = btrfs_level_size(root, level - 1);
1507 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1509 if (btrfs_buffer_uptodate(tmp, 0)) {
1510 if (btrfs_buffer_uptodate(tmp, gen)) {
1512 * we found an up to date block without
1519 /* the pages were up to date, but we failed
1520 * the generation number check. Do a full
1521 * read for the generation number that is correct.
1522 * We must do this without dropping locks so
1523 * we can trust our generation number
1525 free_extent_buffer(tmp);
1526 btrfs_set_path_blocking(p);
1528 tmp = read_tree_block(root, blocknr, blocksize, gen);
1529 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1533 free_extent_buffer(tmp);
1534 btrfs_release_path(p);
1540 * reduce lock contention at high levels
1541 * of the btree by dropping locks before
1542 * we read. Don't release the lock on the current
1543 * level because we need to walk this node to figure
1544 * out which blocks to read.
1546 btrfs_unlock_up_safe(p, level + 1);
1547 btrfs_set_path_blocking(p);
1549 free_extent_buffer(tmp);
1551 reada_for_search(root, p, level, slot, key->objectid);
1553 btrfs_release_path(p);
1556 tmp = read_tree_block(root, blocknr, blocksize, 0);
1559 * If the read above didn't mark this buffer up to date,
1560 * it will never end up being up to date. Set ret to EIO now
1561 * and give up so that our caller doesn't loop forever
1564 if (!btrfs_buffer_uptodate(tmp, 0))
1566 free_extent_buffer(tmp);
1572 * helper function for btrfs_search_slot. This does all of the checks
1573 * for node-level blocks and does any balancing required based on
1576 * If no extra work was required, zero is returned. If we had to
1577 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1581 setup_nodes_for_search(struct btrfs_trans_handle *trans,
1582 struct btrfs_root *root, struct btrfs_path *p,
1583 struct extent_buffer *b, int level, int ins_len,
1584 int *write_lock_level)
1587 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1588 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1591 if (*write_lock_level < level + 1) {
1592 *write_lock_level = level + 1;
1593 btrfs_release_path(p);
1597 sret = reada_for_balance(root, p, level);
1601 btrfs_set_path_blocking(p);
1602 sret = split_node(trans, root, p, level);
1603 btrfs_clear_path_blocking(p, NULL, 0);
1610 b = p->nodes[level];
1611 } else if (ins_len < 0 && btrfs_header_nritems(b) <
1612 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
1615 if (*write_lock_level < level + 1) {
1616 *write_lock_level = level + 1;
1617 btrfs_release_path(p);
1621 sret = reada_for_balance(root, p, level);
1625 btrfs_set_path_blocking(p);
1626 sret = balance_level(trans, root, p, level);
1627 btrfs_clear_path_blocking(p, NULL, 0);
1633 b = p->nodes[level];
1635 btrfs_release_path(p);
1638 BUG_ON(btrfs_header_nritems(b) == 1);
1649 * look for key in the tree. path is filled in with nodes along the way
1650 * if key is found, we return zero and you can find the item in the leaf
1651 * level of the path (level 0)
1653 * If the key isn't found, the path points to the slot where it should
1654 * be inserted, and 1 is returned. If there are other errors during the
1655 * search a negative error number is returned.
1657 * if ins_len > 0, nodes and leaves will be split as we walk down the
1658 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1661 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1662 *root, struct btrfs_key *key, struct btrfs_path *p, int
1665 struct extent_buffer *b;
1670 int lowest_unlock = 1;
1672 /* everything at write_lock_level or lower must be write locked */
1673 int write_lock_level = 0;
1674 u8 lowest_level = 0;
1675 int min_write_lock_level;
1677 lowest_level = p->lowest_level;
1678 WARN_ON(lowest_level && ins_len > 0);
1679 WARN_ON(p->nodes[0] != NULL);
1684 /* when we are removing items, we might have to go up to level
1685 * two as we update tree pointers Make sure we keep write
1686 * for those levels as well
1688 write_lock_level = 2;
1689 } else if (ins_len > 0) {
1691 * for inserting items, make sure we have a write lock on
1692 * level 1 so we can update keys
1694 write_lock_level = 1;
1698 write_lock_level = -1;
1700 if (cow && (p->keep_locks || p->lowest_level))
1701 write_lock_level = BTRFS_MAX_LEVEL;
1703 min_write_lock_level = write_lock_level;
1707 * we try very hard to do read locks on the root
1709 root_lock = BTRFS_READ_LOCK;
1711 if (p->search_commit_root) {
1713 * the commit roots are read only
1714 * so we always do read locks
1716 b = root->commit_root;
1717 extent_buffer_get(b);
1718 level = btrfs_header_level(b);
1719 if (!p->skip_locking)
1720 btrfs_tree_read_lock(b);
1722 if (p->skip_locking) {
1723 b = btrfs_root_node(root);
1724 level = btrfs_header_level(b);
1726 /* we don't know the level of the root node
1727 * until we actually have it read locked
1729 b = btrfs_read_lock_root_node(root);
1730 level = btrfs_header_level(b);
1731 if (level <= write_lock_level) {
1732 /* whoops, must trade for write lock */
1733 btrfs_tree_read_unlock(b);
1734 free_extent_buffer(b);
1735 b = btrfs_lock_root_node(root);
1736 root_lock = BTRFS_WRITE_LOCK;
1738 /* the level might have changed, check again */
1739 level = btrfs_header_level(b);
1743 p->nodes[level] = b;
1744 if (!p->skip_locking)
1745 p->locks[level] = root_lock;
1748 level = btrfs_header_level(b);
1751 * setup the path here so we can release it under lock
1752 * contention with the cow code
1756 * if we don't really need to cow this block
1757 * then we don't want to set the path blocking,
1758 * so we test it here
1760 if (!should_cow_block(trans, root, b))
1763 btrfs_set_path_blocking(p);
1766 * must have write locks on this node and the
1769 if (level + 1 > write_lock_level) {
1770 write_lock_level = level + 1;
1771 btrfs_release_path(p);
1775 err = btrfs_cow_block(trans, root, b,
1776 p->nodes[level + 1],
1777 p->slots[level + 1], &b);
1784 BUG_ON(!cow && ins_len);
1786 p->nodes[level] = b;
1787 btrfs_clear_path_blocking(p, NULL, 0);
1790 * we have a lock on b and as long as we aren't changing
1791 * the tree, there is no way to for the items in b to change.
1792 * It is safe to drop the lock on our parent before we
1793 * go through the expensive btree search on b.
1795 * If cow is true, then we might be changing slot zero,
1796 * which may require changing the parent. So, we can't
1797 * drop the lock until after we know which slot we're
1801 btrfs_unlock_up_safe(p, level + 1);
1803 ret = bin_search(b, key, level, &slot);
1807 if (ret && slot > 0) {
1811 p->slots[level] = slot;
1812 err = setup_nodes_for_search(trans, root, p, b, level,
1813 ins_len, &write_lock_level);
1820 b = p->nodes[level];
1821 slot = p->slots[level];
1824 * slot 0 is special, if we change the key
1825 * we have to update the parent pointer
1826 * which means we must have a write lock
1829 if (slot == 0 && cow &&
1830 write_lock_level < level + 1) {
1831 write_lock_level = level + 1;
1832 btrfs_release_path(p);
1836 unlock_up(p, level, lowest_unlock,
1837 min_write_lock_level, &write_lock_level);
1839 if (level == lowest_level) {
1845 err = read_block_for_search(trans, root, p,
1846 &b, level, slot, key);
1854 if (!p->skip_locking) {
1855 level = btrfs_header_level(b);
1856 if (level <= write_lock_level) {
1857 err = btrfs_try_tree_write_lock(b);
1859 btrfs_set_path_blocking(p);
1861 btrfs_clear_path_blocking(p, b,
1864 p->locks[level] = BTRFS_WRITE_LOCK;
1866 err = btrfs_try_tree_read_lock(b);
1868 btrfs_set_path_blocking(p);
1869 btrfs_tree_read_lock(b);
1870 btrfs_clear_path_blocking(p, b,
1873 p->locks[level] = BTRFS_READ_LOCK;
1875 p->nodes[level] = b;
1878 p->slots[level] = slot;
1880 btrfs_leaf_free_space(root, b) < ins_len) {
1881 if (write_lock_level < 1) {
1882 write_lock_level = 1;
1883 btrfs_release_path(p);
1887 btrfs_set_path_blocking(p);
1888 err = split_leaf(trans, root, key,
1889 p, ins_len, ret == 0);
1890 btrfs_clear_path_blocking(p, NULL, 0);
1898 if (!p->search_for_split)
1899 unlock_up(p, level, lowest_unlock,
1900 min_write_lock_level, &write_lock_level);
1907 * we don't really know what they plan on doing with the path
1908 * from here on, so for now just mark it as blocking
1910 if (!p->leave_spinning)
1911 btrfs_set_path_blocking(p);
1913 btrfs_release_path(p);
1918 * adjust the pointers going up the tree, starting at level
1919 * making sure the right key of each node is points to 'key'.
1920 * This is used after shifting pointers to the left, so it stops
1921 * fixing up pointers when a given leaf/node is not in slot 0 of the
1925 static void fixup_low_keys(struct btrfs_trans_handle *trans,
1926 struct btrfs_root *root, struct btrfs_path *path,
1927 struct btrfs_disk_key *key, int level)
1930 struct extent_buffer *t;
1932 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1933 int tslot = path->slots[i];
1934 if (!path->nodes[i])
1937 btrfs_set_node_key(t, key, tslot);
1938 btrfs_mark_buffer_dirty(path->nodes[i]);
1947 * This function isn't completely safe. It's the caller's responsibility
1948 * that the new key won't break the order
1950 void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1951 struct btrfs_root *root, struct btrfs_path *path,
1952 struct btrfs_key *new_key)
1954 struct btrfs_disk_key disk_key;
1955 struct extent_buffer *eb;
1958 eb = path->nodes[0];
1959 slot = path->slots[0];
1961 btrfs_item_key(eb, &disk_key, slot - 1);
1962 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
1964 if (slot < btrfs_header_nritems(eb) - 1) {
1965 btrfs_item_key(eb, &disk_key, slot + 1);
1966 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
1969 btrfs_cpu_key_to_disk(&disk_key, new_key);
1970 btrfs_set_item_key(eb, &disk_key, slot);
1971 btrfs_mark_buffer_dirty(eb);
1973 fixup_low_keys(trans, root, path, &disk_key, 1);
1977 * try to push data from one node into the next node left in the
1980 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1981 * error, and > 0 if there was no room in the left hand block.
1983 static int push_node_left(struct btrfs_trans_handle *trans,
1984 struct btrfs_root *root, struct extent_buffer *dst,
1985 struct extent_buffer *src, int empty)
1992 src_nritems = btrfs_header_nritems(src);
1993 dst_nritems = btrfs_header_nritems(dst);
1994 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1995 WARN_ON(btrfs_header_generation(src) != trans->transid);
1996 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1998 if (!empty && src_nritems <= 8)
2001 if (push_items <= 0)
2005 push_items = min(src_nritems, push_items);
2006 if (push_items < src_nritems) {
2007 /* leave at least 8 pointers in the node if
2008 * we aren't going to empty it
2010 if (src_nritems - push_items < 8) {
2011 if (push_items <= 8)
2017 push_items = min(src_nritems - 8, push_items);
2019 copy_extent_buffer(dst, src,
2020 btrfs_node_key_ptr_offset(dst_nritems),
2021 btrfs_node_key_ptr_offset(0),
2022 push_items * sizeof(struct btrfs_key_ptr));
2024 if (push_items < src_nritems) {
2025 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2026 btrfs_node_key_ptr_offset(push_items),
2027 (src_nritems - push_items) *
2028 sizeof(struct btrfs_key_ptr));
2030 btrfs_set_header_nritems(src, src_nritems - push_items);
2031 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2032 btrfs_mark_buffer_dirty(src);
2033 btrfs_mark_buffer_dirty(dst);
2039 * try to push data from one node into the next node right in the
2042 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2043 * error, and > 0 if there was no room in the right hand block.
2045 * this will only push up to 1/2 the contents of the left node over
2047 static int balance_node_right(struct btrfs_trans_handle *trans,
2048 struct btrfs_root *root,
2049 struct extent_buffer *dst,
2050 struct extent_buffer *src)
2058 WARN_ON(btrfs_header_generation(src) != trans->transid);
2059 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2061 src_nritems = btrfs_header_nritems(src);
2062 dst_nritems = btrfs_header_nritems(dst);
2063 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
2064 if (push_items <= 0)
2067 if (src_nritems < 4)
2070 max_push = src_nritems / 2 + 1;
2071 /* don't try to empty the node */
2072 if (max_push >= src_nritems)
2075 if (max_push < push_items)
2076 push_items = max_push;
2078 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2079 btrfs_node_key_ptr_offset(0),
2081 sizeof(struct btrfs_key_ptr));
2083 copy_extent_buffer(dst, src,
2084 btrfs_node_key_ptr_offset(0),
2085 btrfs_node_key_ptr_offset(src_nritems - push_items),
2086 push_items * sizeof(struct btrfs_key_ptr));
2088 btrfs_set_header_nritems(src, src_nritems - push_items);
2089 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2091 btrfs_mark_buffer_dirty(src);
2092 btrfs_mark_buffer_dirty(dst);
2098 * helper function to insert a new root level in the tree.
2099 * A new node is allocated, and a single item is inserted to
2100 * point to the existing root
2102 * returns zero on success or < 0 on failure.
2104 static noinline int insert_new_root(struct btrfs_trans_handle *trans,
2105 struct btrfs_root *root,
2106 struct btrfs_path *path, int level)
2109 struct extent_buffer *lower;
2110 struct extent_buffer *c;
2111 struct extent_buffer *old;
2112 struct btrfs_disk_key lower_key;
2114 BUG_ON(path->nodes[level]);
2115 BUG_ON(path->nodes[level-1] != root->node);
2117 lower = path->nodes[level-1];
2119 btrfs_item_key(lower, &lower_key, 0);
2121 btrfs_node_key(lower, &lower_key, 0);
2123 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
2124 root->root_key.objectid, &lower_key,
2125 level, root->node->start, 0, 0);
2129 root_add_used(root, root->nodesize);
2131 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
2132 btrfs_set_header_nritems(c, 1);
2133 btrfs_set_header_level(c, level);
2134 btrfs_set_header_bytenr(c, c->start);
2135 btrfs_set_header_generation(c, trans->transid);
2136 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
2137 btrfs_set_header_owner(c, root->root_key.objectid);
2139 write_extent_buffer(c, root->fs_info->fsid,
2140 (unsigned long)btrfs_header_fsid(c),
2143 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2144 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2147 btrfs_set_node_key(c, &lower_key, 0);
2148 btrfs_set_node_blockptr(c, 0, lower->start);
2149 lower_gen = btrfs_header_generation(lower);
2150 WARN_ON(lower_gen != trans->transid);
2152 btrfs_set_node_ptr_generation(c, 0, lower_gen);
2154 btrfs_mark_buffer_dirty(c);
2157 rcu_assign_pointer(root->node, c);
2159 /* the super has an extra ref to root->node */
2160 free_extent_buffer(old);
2162 add_root_to_dirty_list(root);
2163 extent_buffer_get(c);
2164 path->nodes[level] = c;
2165 path->locks[level] = BTRFS_WRITE_LOCK;
2166 path->slots[level] = 0;
2171 * worker function to insert a single pointer in a node.
2172 * the node should have enough room for the pointer already
2174 * slot and level indicate where you want the key to go, and
2175 * blocknr is the block the key points to.
2177 static void insert_ptr(struct btrfs_trans_handle *trans,
2178 struct btrfs_root *root, struct btrfs_path *path,
2179 struct btrfs_disk_key *key, u64 bytenr,
2180 int slot, int level)
2182 struct extent_buffer *lower;
2185 BUG_ON(!path->nodes[level]);
2186 btrfs_assert_tree_locked(path->nodes[level]);
2187 lower = path->nodes[level];
2188 nritems = btrfs_header_nritems(lower);
2189 BUG_ON(slot > nritems);
2190 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
2191 if (slot != nritems) {
2192 memmove_extent_buffer(lower,
2193 btrfs_node_key_ptr_offset(slot + 1),
2194 btrfs_node_key_ptr_offset(slot),
2195 (nritems - slot) * sizeof(struct btrfs_key_ptr));
2197 btrfs_set_node_key(lower, key, slot);
2198 btrfs_set_node_blockptr(lower, slot, bytenr);
2199 WARN_ON(trans->transid == 0);
2200 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
2201 btrfs_set_header_nritems(lower, nritems + 1);
2202 btrfs_mark_buffer_dirty(lower);
2206 * split the node at the specified level in path in two.
2207 * The path is corrected to point to the appropriate node after the split
2209 * Before splitting this tries to make some room in the node by pushing
2210 * left and right, if either one works, it returns right away.
2212 * returns 0 on success and < 0 on failure
2214 static noinline int split_node(struct btrfs_trans_handle *trans,
2215 struct btrfs_root *root,
2216 struct btrfs_path *path, int level)
2218 struct extent_buffer *c;
2219 struct extent_buffer *split;
2220 struct btrfs_disk_key disk_key;
2225 c = path->nodes[level];
2226 WARN_ON(btrfs_header_generation(c) != trans->transid);
2227 if (c == root->node) {
2228 /* trying to split the root, lets make a new one */
2229 ret = insert_new_root(trans, root, path, level + 1);
2233 ret = push_nodes_for_insert(trans, root, path, level);
2234 c = path->nodes[level];
2235 if (!ret && btrfs_header_nritems(c) <
2236 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
2242 c_nritems = btrfs_header_nritems(c);
2243 mid = (c_nritems + 1) / 2;
2244 btrfs_node_key(c, &disk_key, mid);
2246 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
2247 root->root_key.objectid,
2248 &disk_key, level, c->start, 0, 0);
2250 return PTR_ERR(split);
2252 root_add_used(root, root->nodesize);
2254 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
2255 btrfs_set_header_level(split, btrfs_header_level(c));
2256 btrfs_set_header_bytenr(split, split->start);
2257 btrfs_set_header_generation(split, trans->transid);
2258 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
2259 btrfs_set_header_owner(split, root->root_key.objectid);
2260 write_extent_buffer(split, root->fs_info->fsid,
2261 (unsigned long)btrfs_header_fsid(split),
2263 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2264 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2268 copy_extent_buffer(split, c,
2269 btrfs_node_key_ptr_offset(0),
2270 btrfs_node_key_ptr_offset(mid),
2271 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2272 btrfs_set_header_nritems(split, c_nritems - mid);
2273 btrfs_set_header_nritems(c, mid);
2276 btrfs_mark_buffer_dirty(c);
2277 btrfs_mark_buffer_dirty(split);
2279 insert_ptr(trans, root, path, &disk_key, split->start,
2280 path->slots[level + 1] + 1, level + 1);
2282 if (path->slots[level] >= mid) {
2283 path->slots[level] -= mid;
2284 btrfs_tree_unlock(c);
2285 free_extent_buffer(c);
2286 path->nodes[level] = split;
2287 path->slots[level + 1] += 1;
2289 btrfs_tree_unlock(split);
2290 free_extent_buffer(split);
2296 * how many bytes are required to store the items in a leaf. start
2297 * and nr indicate which items in the leaf to check. This totals up the
2298 * space used both by the item structs and the item data
2300 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
2303 int nritems = btrfs_header_nritems(l);
2304 int end = min(nritems, start + nr) - 1;
2308 data_len = btrfs_item_end_nr(l, start);
2309 data_len = data_len - btrfs_item_offset_nr(l, end);
2310 data_len += sizeof(struct btrfs_item) * nr;
2311 WARN_ON(data_len < 0);
2316 * The space between the end of the leaf items and
2317 * the start of the leaf data. IOW, how much room
2318 * the leaf has left for both items and data
2320 noinline int btrfs_leaf_free_space(struct btrfs_root *root,
2321 struct extent_buffer *leaf)
2323 int nritems = btrfs_header_nritems(leaf);
2325 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2327 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2328 "used %d nritems %d\n",
2329 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
2330 leaf_space_used(leaf, 0, nritems), nritems);
2336 * min slot controls the lowest index we're willing to push to the
2337 * right. We'll push up to and including min_slot, but no lower
2339 static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2340 struct btrfs_root *root,
2341 struct btrfs_path *path,
2342 int data_size, int empty,
2343 struct extent_buffer *right,
2344 int free_space, u32 left_nritems,
2347 struct extent_buffer *left = path->nodes[0];
2348 struct extent_buffer *upper = path->nodes[1];
2349 struct btrfs_map_token token;
2350 struct btrfs_disk_key disk_key;
2355 struct btrfs_item *item;
2361 btrfs_init_map_token(&token);
2366 nr = max_t(u32, 1, min_slot);
2368 if (path->slots[0] >= left_nritems)
2369 push_space += data_size;
2371 slot = path->slots[1];
2372 i = left_nritems - 1;
2374 item = btrfs_item_nr(left, i);
2376 if (!empty && push_items > 0) {
2377 if (path->slots[0] > i)
2379 if (path->slots[0] == i) {
2380 int space = btrfs_leaf_free_space(root, left);
2381 if (space + push_space * 2 > free_space)
2386 if (path->slots[0] == i)
2387 push_space += data_size;
2389 this_item_size = btrfs_item_size(left, item);
2390 if (this_item_size + sizeof(*item) + push_space > free_space)
2394 push_space += this_item_size + sizeof(*item);
2400 if (push_items == 0)
2403 if (!empty && push_items == left_nritems)
2406 /* push left to right */
2407 right_nritems = btrfs_header_nritems(right);
2409 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
2410 push_space -= leaf_data_end(root, left);
2412 /* make room in the right data area */
2413 data_end = leaf_data_end(root, right);
2414 memmove_extent_buffer(right,
2415 btrfs_leaf_data(right) + data_end - push_space,
2416 btrfs_leaf_data(right) + data_end,
2417 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2419 /* copy from the left data area */
2420 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
2421 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2422 btrfs_leaf_data(left) + leaf_data_end(root, left),
2425 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2426 btrfs_item_nr_offset(0),
2427 right_nritems * sizeof(struct btrfs_item));
2429 /* copy the items from left to right */
2430 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2431 btrfs_item_nr_offset(left_nritems - push_items),
2432 push_items * sizeof(struct btrfs_item));
2434 /* update the item pointers */
2435 right_nritems += push_items;
2436 btrfs_set_header_nritems(right, right_nritems);
2437 push_space = BTRFS_LEAF_DATA_SIZE(root);
2438 for (i = 0; i < right_nritems; i++) {
2439 item = btrfs_item_nr(right, i);
2440 push_space -= btrfs_token_item_size(right, item, &token);
2441 btrfs_set_token_item_offset(right, item, push_space, &token);
2444 left_nritems -= push_items;
2445 btrfs_set_header_nritems(left, left_nritems);
2448 btrfs_mark_buffer_dirty(left);
2450 clean_tree_block(trans, root, left);
2452 btrfs_mark_buffer_dirty(right);
2454 btrfs_item_key(right, &disk_key, 0);
2455 btrfs_set_node_key(upper, &disk_key, slot + 1);
2456 btrfs_mark_buffer_dirty(upper);
2458 /* then fixup the leaf pointer in the path */
2459 if (path->slots[0] >= left_nritems) {
2460 path->slots[0] -= left_nritems;
2461 if (btrfs_header_nritems(path->nodes[0]) == 0)
2462 clean_tree_block(trans, root, path->nodes[0]);
2463 btrfs_tree_unlock(path->nodes[0]);
2464 free_extent_buffer(path->nodes[0]);
2465 path->nodes[0] = right;
2466 path->slots[1] += 1;
2468 btrfs_tree_unlock(right);
2469 free_extent_buffer(right);
2474 btrfs_tree_unlock(right);
2475 free_extent_buffer(right);
2480 * push some data in the path leaf to the right, trying to free up at
2481 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2483 * returns 1 if the push failed because the other node didn't have enough
2484 * room, 0 if everything worked out and < 0 if there were major errors.
2486 * this will push starting from min_slot to the end of the leaf. It won't
2487 * push any slot lower than min_slot
2489 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
2490 *root, struct btrfs_path *path,
2491 int min_data_size, int data_size,
2492 int empty, u32 min_slot)
2494 struct extent_buffer *left = path->nodes[0];
2495 struct extent_buffer *right;
2496 struct extent_buffer *upper;
2502 if (!path->nodes[1])
2505 slot = path->slots[1];
2506 upper = path->nodes[1];
2507 if (slot >= btrfs_header_nritems(upper) - 1)
2510 btrfs_assert_tree_locked(path->nodes[1]);
2512 right = read_node_slot(root, upper, slot + 1);
2516 btrfs_tree_lock(right);
2517 btrfs_set_lock_blocking(right);
2519 free_space = btrfs_leaf_free_space(root, right);
2520 if (free_space < data_size)
2523 /* cow and double check */
2524 ret = btrfs_cow_block(trans, root, right, upper,
2529 free_space = btrfs_leaf_free_space(root, right);
2530 if (free_space < data_size)
2533 left_nritems = btrfs_header_nritems(left);
2534 if (left_nritems == 0)
2537 return __push_leaf_right(trans, root, path, min_data_size, empty,
2538 right, free_space, left_nritems, min_slot);
2540 btrfs_tree_unlock(right);
2541 free_extent_buffer(right);
2546 * push some data in the path leaf to the left, trying to free up at
2547 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2549 * max_slot can put a limit on how far into the leaf we'll push items. The
2550 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2553 static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2554 struct btrfs_root *root,
2555 struct btrfs_path *path, int data_size,
2556 int empty, struct extent_buffer *left,
2557 int free_space, u32 right_nritems,
2560 struct btrfs_disk_key disk_key;
2561 struct extent_buffer *right = path->nodes[0];
2565 struct btrfs_item *item;
2566 u32 old_left_nritems;
2570 u32 old_left_item_size;
2571 struct btrfs_map_token token;
2573 btrfs_init_map_token(&token);
2576 nr = min(right_nritems, max_slot);
2578 nr = min(right_nritems - 1, max_slot);
2580 for (i = 0; i < nr; i++) {
2581 item = btrfs_item_nr(right, i);
2583 if (!empty && push_items > 0) {
2584 if (path->slots[0] < i)
2586 if (path->slots[0] == i) {
2587 int space = btrfs_leaf_free_space(root, right);
2588 if (space + push_space * 2 > free_space)
2593 if (path->slots[0] == i)
2594 push_space += data_size;
2596 this_item_size = btrfs_item_size(right, item);
2597 if (this_item_size + sizeof(*item) + push_space > free_space)
2601 push_space += this_item_size + sizeof(*item);
2604 if (push_items == 0) {
2608 if (!empty && push_items == btrfs_header_nritems(right))
2611 /* push data from right to left */
2612 copy_extent_buffer(left, right,
2613 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2614 btrfs_item_nr_offset(0),
2615 push_items * sizeof(struct btrfs_item));
2617 push_space = BTRFS_LEAF_DATA_SIZE(root) -
2618 btrfs_item_offset_nr(right, push_items - 1);
2620 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
2621 leaf_data_end(root, left) - push_space,
2622 btrfs_leaf_data(right) +
2623 btrfs_item_offset_nr(right, push_items - 1),
2625 old_left_nritems = btrfs_header_nritems(left);
2626 BUG_ON(old_left_nritems <= 0);
2628 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
2629 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
2632 item = btrfs_item_nr(left, i);
2634 ioff = btrfs_token_item_offset(left, item, &token);
2635 btrfs_set_token_item_offset(left, item,
2636 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
2639 btrfs_set_header_nritems(left, old_left_nritems + push_items);
2641 /* fixup right node */
2642 if (push_items > right_nritems) {
2643 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2648 if (push_items < right_nritems) {
2649 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2650 leaf_data_end(root, right);
2651 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2652 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2653 btrfs_leaf_data(right) +
2654 leaf_data_end(root, right), push_space);
2656 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
2657 btrfs_item_nr_offset(push_items),
2658 (btrfs_header_nritems(right) - push_items) *
2659 sizeof(struct btrfs_item));
2661 right_nritems -= push_items;
2662 btrfs_set_header_nritems(right, right_nritems);
2663 push_space = BTRFS_LEAF_DATA_SIZE(root);
2664 for (i = 0; i < right_nritems; i++) {
2665 item = btrfs_item_nr(right, i);
2667 push_space = push_space - btrfs_token_item_size(right,
2669 btrfs_set_token_item_offset(right, item, push_space, &token);
2672 btrfs_mark_buffer_dirty(left);
2674 btrfs_mark_buffer_dirty(right);
2676 clean_tree_block(trans, root, right);
2678 btrfs_item_key(right, &disk_key, 0);
2679 fixup_low_keys(trans, root, path, &disk_key, 1);
2681 /* then fixup the leaf pointer in the path */
2682 if (path->slots[0] < push_items) {
2683 path->slots[0] += old_left_nritems;
2684 btrfs_tree_unlock(path->nodes[0]);
2685 free_extent_buffer(path->nodes[0]);
2686 path->nodes[0] = left;
2687 path->slots[1] -= 1;
2689 btrfs_tree_unlock(left);
2690 free_extent_buffer(left);
2691 path->slots[0] -= push_items;
2693 BUG_ON(path->slots[0] < 0);
2696 btrfs_tree_unlock(left);
2697 free_extent_buffer(left);
2702 * push some data in the path leaf to the left, trying to free up at
2703 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2705 * max_slot can put a limit on how far into the leaf we'll push items. The
2706 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2709 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
2710 *root, struct btrfs_path *path, int min_data_size,
2711 int data_size, int empty, u32 max_slot)
2713 struct extent_buffer *right = path->nodes[0];
2714 struct extent_buffer *left;
2720 slot = path->slots[1];
2723 if (!path->nodes[1])
2726 right_nritems = btrfs_header_nritems(right);
2727 if (right_nritems == 0)
2730 btrfs_assert_tree_locked(path->nodes[1]);
2732 left = read_node_slot(root, path->nodes[1], slot - 1);
2736 btrfs_tree_lock(left);
2737 btrfs_set_lock_blocking(left);
2739 free_space = btrfs_leaf_free_space(root, left);
2740 if (free_space < data_size) {
2745 /* cow and double check */
2746 ret = btrfs_cow_block(trans, root, left,
2747 path->nodes[1], slot - 1, &left);
2749 /* we hit -ENOSPC, but it isn't fatal here */
2755 free_space = btrfs_leaf_free_space(root, left);
2756 if (free_space < data_size) {
2761 return __push_leaf_left(trans, root, path, min_data_size,
2762 empty, left, free_space, right_nritems,
2765 btrfs_tree_unlock(left);
2766 free_extent_buffer(left);
2771 * split the path's leaf in two, making sure there is at least data_size
2772 * available for the resulting leaf level of the path.
2774 static noinline void copy_for_split(struct btrfs_trans_handle *trans,
2775 struct btrfs_root *root,
2776 struct btrfs_path *path,
2777 struct extent_buffer *l,
2778 struct extent_buffer *right,
2779 int slot, int mid, int nritems)
2784 struct btrfs_disk_key disk_key;
2785 struct btrfs_map_token token;
2787 btrfs_init_map_token(&token);
2789 nritems = nritems - mid;
2790 btrfs_set_header_nritems(right, nritems);
2791 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2793 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2794 btrfs_item_nr_offset(mid),
2795 nritems * sizeof(struct btrfs_item));
2797 copy_extent_buffer(right, l,
2798 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2799 data_copy_size, btrfs_leaf_data(l) +
2800 leaf_data_end(root, l), data_copy_size);
2802 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2803 btrfs_item_end_nr(l, mid);
2805 for (i = 0; i < nritems; i++) {
2806 struct btrfs_item *item = btrfs_item_nr(right, i);
2809 ioff = btrfs_token_item_offset(right, item, &token);
2810 btrfs_set_token_item_offset(right, item,
2811 ioff + rt_data_off, &token);
2814 btrfs_set_header_nritems(l, mid);
2815 btrfs_item_key(right, &disk_key, 0);
2816 insert_ptr(trans, root, path, &disk_key, right->start,
2817 path->slots[1] + 1, 1);
2819 btrfs_mark_buffer_dirty(right);
2820 btrfs_mark_buffer_dirty(l);
2821 BUG_ON(path->slots[0] != slot);
2824 btrfs_tree_unlock(path->nodes[0]);
2825 free_extent_buffer(path->nodes[0]);
2826 path->nodes[0] = right;
2827 path->slots[0] -= mid;
2828 path->slots[1] += 1;
2830 btrfs_tree_unlock(right);
2831 free_extent_buffer(right);
2834 BUG_ON(path->slots[0] < 0);
2838 * double splits happen when we need to insert a big item in the middle
2839 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2840 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2843 * We avoid this by trying to push the items on either side of our target
2844 * into the adjacent leaves. If all goes well we can avoid the double split
2847 static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2848 struct btrfs_root *root,
2849 struct btrfs_path *path,
2857 slot = path->slots[0];
2860 * try to push all the items after our slot into the
2863 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2870 nritems = btrfs_header_nritems(path->nodes[0]);
2872 * our goal is to get our slot at the start or end of a leaf. If
2873 * we've done so we're done
2875 if (path->slots[0] == 0 || path->slots[0] == nritems)
2878 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2881 /* try to push all the items before our slot into the next leaf */
2882 slot = path->slots[0];
2883 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2896 * split the path's leaf in two, making sure there is at least data_size
2897 * available for the resulting leaf level of the path.
2899 * returns 0 if all went well and < 0 on failure.
2901 static noinline int split_leaf(struct btrfs_trans_handle *trans,
2902 struct btrfs_root *root,
2903 struct btrfs_key *ins_key,
2904 struct btrfs_path *path, int data_size,
2907 struct btrfs_disk_key disk_key;
2908 struct extent_buffer *l;
2912 struct extent_buffer *right;
2916 int num_doubles = 0;
2917 int tried_avoid_double = 0;
2920 slot = path->slots[0];
2921 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2922 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2925 /* first try to make some room by pushing left and right */
2927 wret = push_leaf_right(trans, root, path, data_size,
2932 wret = push_leaf_left(trans, root, path, data_size,
2933 data_size, 0, (u32)-1);
2939 /* did the pushes work? */
2940 if (btrfs_leaf_free_space(root, l) >= data_size)
2944 if (!path->nodes[1]) {
2945 ret = insert_new_root(trans, root, path, 1);
2952 slot = path->slots[0];
2953 nritems = btrfs_header_nritems(l);
2954 mid = (nritems + 1) / 2;
2958 leaf_space_used(l, mid, nritems - mid) + data_size >
2959 BTRFS_LEAF_DATA_SIZE(root)) {
2960 if (slot >= nritems) {
2964 if (mid != nritems &&
2965 leaf_space_used(l, mid, nritems - mid) +
2966 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2967 if (data_size && !tried_avoid_double)
2968 goto push_for_double;
2974 if (leaf_space_used(l, 0, mid) + data_size >
2975 BTRFS_LEAF_DATA_SIZE(root)) {
2976 if (!extend && data_size && slot == 0) {
2978 } else if ((extend || !data_size) && slot == 0) {
2982 if (mid != nritems &&
2983 leaf_space_used(l, mid, nritems - mid) +
2984 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2985 if (data_size && !tried_avoid_double)
2986 goto push_for_double;
2994 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2996 btrfs_item_key(l, &disk_key, mid);
2998 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
2999 root->root_key.objectid,
3000 &disk_key, 0, l->start, 0, 0);
3002 return PTR_ERR(right);
3004 root_add_used(root, root->leafsize);
3006 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3007 btrfs_set_header_bytenr(right, right->start);
3008 btrfs_set_header_generation(right, trans->transid);
3009 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
3010 btrfs_set_header_owner(right, root->root_key.objectid);
3011 btrfs_set_header_level(right, 0);
3012 write_extent_buffer(right, root->fs_info->fsid,
3013 (unsigned long)btrfs_header_fsid(right),
3016 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3017 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3022 btrfs_set_header_nritems(right, 0);
3023 insert_ptr(trans, root, path, &disk_key, right->start,
3024 path->slots[1] + 1, 1);
3025 btrfs_tree_unlock(path->nodes[0]);
3026 free_extent_buffer(path->nodes[0]);
3027 path->nodes[0] = right;
3029 path->slots[1] += 1;
3031 btrfs_set_header_nritems(right, 0);
3032 insert_ptr(trans, root, path, &disk_key, right->start,
3034 btrfs_tree_unlock(path->nodes[0]);
3035 free_extent_buffer(path->nodes[0]);
3036 path->nodes[0] = right;
3038 if (path->slots[1] == 0)
3039 fixup_low_keys(trans, root, path,
3042 btrfs_mark_buffer_dirty(right);
3046 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
3049 BUG_ON(num_doubles != 0);
3057 push_for_double_split(trans, root, path, data_size);
3058 tried_avoid_double = 1;
3059 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3064 static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3065 struct btrfs_root *root,
3066 struct btrfs_path *path, int ins_len)
3068 struct btrfs_key key;
3069 struct extent_buffer *leaf;
3070 struct btrfs_file_extent_item *fi;
3075 leaf = path->nodes[0];
3076 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3078 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3079 key.type != BTRFS_EXTENT_CSUM_KEY);
3081 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3084 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3085 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3086 fi = btrfs_item_ptr(leaf, path->slots[0],
3087 struct btrfs_file_extent_item);
3088 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3090 btrfs_release_path(path);
3092 path->keep_locks = 1;
3093 path->search_for_split = 1;
3094 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3095 path->search_for_split = 0;
3100 leaf = path->nodes[0];
3101 /* if our item isn't there or got smaller, return now */
3102 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3105 /* the leaf has changed, it now has room. return now */
3106 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3109 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3110 fi = btrfs_item_ptr(leaf, path->slots[0],
3111 struct btrfs_file_extent_item);
3112 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3116 btrfs_set_path_blocking(path);
3117 ret = split_leaf(trans, root, &key, path, ins_len, 1);
3121 path->keep_locks = 0;
3122 btrfs_unlock_up_safe(path, 1);
3125 path->keep_locks = 0;
3129 static noinline int split_item(struct btrfs_trans_handle *trans,
3130 struct btrfs_root *root,
3131 struct btrfs_path *path,
3132 struct btrfs_key *new_key,
3133 unsigned long split_offset)
3135 struct extent_buffer *leaf;
3136 struct btrfs_item *item;
3137 struct btrfs_item *new_item;
3143 struct btrfs_disk_key disk_key;
3145 leaf = path->nodes[0];
3146 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3148 btrfs_set_path_blocking(path);
3150 item = btrfs_item_nr(leaf, path->slots[0]);
3151 orig_offset = btrfs_item_offset(leaf, item);
3152 item_size = btrfs_item_size(leaf, item);
3154 buf = kmalloc(item_size, GFP_NOFS);
3158 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3159 path->slots[0]), item_size);
3161 slot = path->slots[0] + 1;
3162 nritems = btrfs_header_nritems(leaf);
3163 if (slot != nritems) {
3164 /* shift the items */
3165 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3166 btrfs_item_nr_offset(slot),
3167 (nritems - slot) * sizeof(struct btrfs_item));
3170 btrfs_cpu_key_to_disk(&disk_key, new_key);
3171 btrfs_set_item_key(leaf, &disk_key, slot);
3173 new_item = btrfs_item_nr(leaf, slot);
3175 btrfs_set_item_offset(leaf, new_item, orig_offset);
3176 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3178 btrfs_set_item_offset(leaf, item,
3179 orig_offset + item_size - split_offset);
3180 btrfs_set_item_size(leaf, item, split_offset);
3182 btrfs_set_header_nritems(leaf, nritems + 1);
3184 /* write the data for the start of the original item */
3185 write_extent_buffer(leaf, buf,
3186 btrfs_item_ptr_offset(leaf, path->slots[0]),
3189 /* write the data for the new item */
3190 write_extent_buffer(leaf, buf + split_offset,
3191 btrfs_item_ptr_offset(leaf, slot),
3192 item_size - split_offset);
3193 btrfs_mark_buffer_dirty(leaf);
3195 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
3201 * This function splits a single item into two items,
3202 * giving 'new_key' to the new item and splitting the
3203 * old one at split_offset (from the start of the item).
3205 * The path may be released by this operation. After
3206 * the split, the path is pointing to the old item. The
3207 * new item is going to be in the same node as the old one.
3209 * Note, the item being split must be smaller enough to live alone on
3210 * a tree block with room for one extra struct btrfs_item
3212 * This allows us to split the item in place, keeping a lock on the
3213 * leaf the entire time.
3215 int btrfs_split_item(struct btrfs_trans_handle *trans,
3216 struct btrfs_root *root,
3217 struct btrfs_path *path,
3218 struct btrfs_key *new_key,
3219 unsigned long split_offset)
3222 ret = setup_leaf_for_split(trans, root, path,
3223 sizeof(struct btrfs_item));
3227 ret = split_item(trans, root, path, new_key, split_offset);
3232 * This function duplicate a item, giving 'new_key' to the new item.
3233 * It guarantees both items live in the same tree leaf and the new item
3234 * is contiguous with the original item.
3236 * This allows us to split file extent in place, keeping a lock on the
3237 * leaf the entire time.
3239 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3240 struct btrfs_root *root,
3241 struct btrfs_path *path,
3242 struct btrfs_key *new_key)
3244 struct extent_buffer *leaf;
3248 leaf = path->nodes[0];
3249 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3250 ret = setup_leaf_for_split(trans, root, path,
3251 item_size + sizeof(struct btrfs_item));
3256 setup_items_for_insert(trans, root, path, new_key, &item_size,
3257 item_size, item_size +
3258 sizeof(struct btrfs_item), 1);
3259 leaf = path->nodes[0];
3260 memcpy_extent_buffer(leaf,
3261 btrfs_item_ptr_offset(leaf, path->slots[0]),
3262 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3268 * make the item pointed to by the path smaller. new_size indicates
3269 * how small to make it, and from_end tells us if we just chop bytes
3270 * off the end of the item or if we shift the item to chop bytes off
3273 void btrfs_truncate_item(struct btrfs_trans_handle *trans,
3274 struct btrfs_root *root,
3275 struct btrfs_path *path,
3276 u32 new_size, int from_end)
3279 struct extent_buffer *leaf;
3280 struct btrfs_item *item;
3282 unsigned int data_end;
3283 unsigned int old_data_start;
3284 unsigned int old_size;
3285 unsigned int size_diff;
3287 struct btrfs_map_token token;
3289 btrfs_init_map_token(&token);
3291 leaf = path->nodes[0];
3292 slot = path->slots[0];
3294 old_size = btrfs_item_size_nr(leaf, slot);
3295 if (old_size == new_size)
3298 nritems = btrfs_header_nritems(leaf);
3299 data_end = leaf_data_end(root, leaf);
3301 old_data_start = btrfs_item_offset_nr(leaf, slot);
3303 size_diff = old_size - new_size;
3306 BUG_ON(slot >= nritems);
3309 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3311 /* first correct the data pointers */
3312 for (i = slot; i < nritems; i++) {
3314 item = btrfs_item_nr(leaf, i);
3316 ioff = btrfs_token_item_offset(leaf, item, &token);
3317 btrfs_set_token_item_offset(leaf, item,
3318 ioff + size_diff, &token);
3321 /* shift the data */
3323 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3324 data_end + size_diff, btrfs_leaf_data(leaf) +
3325 data_end, old_data_start + new_size - data_end);
3327 struct btrfs_disk_key disk_key;
3330 btrfs_item_key(leaf, &disk_key, slot);
3332 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3334 struct btrfs_file_extent_item *fi;
3336 fi = btrfs_item_ptr(leaf, slot,
3337 struct btrfs_file_extent_item);
3338 fi = (struct btrfs_file_extent_item *)(
3339 (unsigned long)fi - size_diff);
3341 if (btrfs_file_extent_type(leaf, fi) ==
3342 BTRFS_FILE_EXTENT_INLINE) {
3343 ptr = btrfs_item_ptr_offset(leaf, slot);
3344 memmove_extent_buffer(leaf, ptr,
3346 offsetof(struct btrfs_file_extent_item,
3351 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3352 data_end + size_diff, btrfs_leaf_data(leaf) +
3353 data_end, old_data_start - data_end);
3355 offset = btrfs_disk_key_offset(&disk_key);
3356 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3357 btrfs_set_item_key(leaf, &disk_key, slot);
3359 fixup_low_keys(trans, root, path, &disk_key, 1);
3362 item = btrfs_item_nr(leaf, slot);
3363 btrfs_set_item_size(leaf, item, new_size);
3364 btrfs_mark_buffer_dirty(leaf);
3366 if (btrfs_leaf_free_space(root, leaf) < 0) {
3367 btrfs_print_leaf(root, leaf);
3373 * make the item pointed to by the path bigger, data_size is the new size.
3375 void btrfs_extend_item(struct btrfs_trans_handle *trans,
3376 struct btrfs_root *root, struct btrfs_path *path,
3380 struct extent_buffer *leaf;
3381 struct btrfs_item *item;
3383 unsigned int data_end;
3384 unsigned int old_data;
3385 unsigned int old_size;
3387 struct btrfs_map_token token;
3389 btrfs_init_map_token(&token);
3391 leaf = path->nodes[0];
3393 nritems = btrfs_header_nritems(leaf);
3394 data_end = leaf_data_end(root, leaf);
3396 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3397 btrfs_print_leaf(root, leaf);
3400 slot = path->slots[0];
3401 old_data = btrfs_item_end_nr(leaf, slot);
3404 if (slot >= nritems) {
3405 btrfs_print_leaf(root, leaf);
3406 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3412 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3414 /* first correct the data pointers */
3415 for (i = slot; i < nritems; i++) {
3417 item = btrfs_item_nr(leaf, i);
3419 ioff = btrfs_token_item_offset(leaf, item, &token);
3420 btrfs_set_token_item_offset(leaf, item,
3421 ioff - data_size, &token);
3424 /* shift the data */
3425 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3426 data_end - data_size, btrfs_leaf_data(leaf) +
3427 data_end, old_data - data_end);
3429 data_end = old_data;
3430 old_size = btrfs_item_size_nr(leaf, slot);
3431 item = btrfs_item_nr(leaf, slot);
3432 btrfs_set_item_size(leaf, item, old_size + data_size);
3433 btrfs_mark_buffer_dirty(leaf);
3435 if (btrfs_leaf_free_space(root, leaf) < 0) {
3436 btrfs_print_leaf(root, leaf);
3442 * Given a key and some data, insert items into the tree.
3443 * This does all the path init required, making room in the tree if needed.
3444 * Returns the number of keys that were inserted.
3446 int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3447 struct btrfs_root *root,
3448 struct btrfs_path *path,
3449 struct btrfs_key *cpu_key, u32 *data_size,
3452 struct extent_buffer *leaf;
3453 struct btrfs_item *item;
3460 unsigned int data_end;
3461 struct btrfs_disk_key disk_key;
3462 struct btrfs_key found_key;
3463 struct btrfs_map_token token;
3465 btrfs_init_map_token(&token);
3467 for (i = 0; i < nr; i++) {
3468 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3469 BTRFS_LEAF_DATA_SIZE(root)) {
3473 total_data += data_size[i];
3474 total_size += data_size[i] + sizeof(struct btrfs_item);
3478 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3484 leaf = path->nodes[0];
3486 nritems = btrfs_header_nritems(leaf);
3487 data_end = leaf_data_end(root, leaf);
3489 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3490 for (i = nr; i >= 0; i--) {
3491 total_data -= data_size[i];
3492 total_size -= data_size[i] + sizeof(struct btrfs_item);
3493 if (total_size < btrfs_leaf_free_space(root, leaf))
3499 slot = path->slots[0];
3502 if (slot != nritems) {
3503 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3505 item = btrfs_item_nr(leaf, slot);
3506 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3508 /* figure out how many keys we can insert in here */
3509 total_data = data_size[0];
3510 for (i = 1; i < nr; i++) {
3511 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3513 total_data += data_size[i];
3517 if (old_data < data_end) {
3518 btrfs_print_leaf(root, leaf);
3519 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3520 slot, old_data, data_end);
3524 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3526 /* first correct the data pointers */
3527 for (i = slot; i < nritems; i++) {
3530 item = btrfs_item_nr(leaf, i);
3531 ioff = btrfs_token_item_offset(leaf, item, &token);
3532 btrfs_set_token_item_offset(leaf, item,
3533 ioff - total_data, &token);
3535 /* shift the items */
3536 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3537 btrfs_item_nr_offset(slot),
3538 (nritems - slot) * sizeof(struct btrfs_item));
3540 /* shift the data */
3541 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3542 data_end - total_data, btrfs_leaf_data(leaf) +
3543 data_end, old_data - data_end);
3544 data_end = old_data;
3547 * this sucks but it has to be done, if we are inserting at
3548 * the end of the leaf only insert 1 of the items, since we
3549 * have no way of knowing whats on the next leaf and we'd have
3550 * to drop our current locks to figure it out
3555 /* setup the item for the new data */
3556 for (i = 0; i < nr; i++) {
3557 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3558 btrfs_set_item_key(leaf, &disk_key, slot + i);
3559 item = btrfs_item_nr(leaf, slot + i);
3560 btrfs_set_token_item_offset(leaf, item,
3561 data_end - data_size[i], &token);
3562 data_end -= data_size[i];
3563 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
3565 btrfs_set_header_nritems(leaf, nritems + nr);
3566 btrfs_mark_buffer_dirty(leaf);
3570 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3571 fixup_low_keys(trans, root, path, &disk_key, 1);
3574 if (btrfs_leaf_free_space(root, leaf) < 0) {
3575 btrfs_print_leaf(root, leaf);
3585 * this is a helper for btrfs_insert_empty_items, the main goal here is
3586 * to save stack depth by doing the bulk of the work in a function
3587 * that doesn't call btrfs_search_slot
3589 void setup_items_for_insert(struct btrfs_trans_handle *trans,
3590 struct btrfs_root *root, struct btrfs_path *path,
3591 struct btrfs_key *cpu_key, u32 *data_size,
3592 u32 total_data, u32 total_size, int nr)
3594 struct btrfs_item *item;
3597 unsigned int data_end;
3598 struct btrfs_disk_key disk_key;
3599 struct extent_buffer *leaf;
3601 struct btrfs_map_token token;
3603 btrfs_init_map_token(&token);
3605 leaf = path->nodes[0];
3606 slot = path->slots[0];
3608 nritems = btrfs_header_nritems(leaf);
3609 data_end = leaf_data_end(root, leaf);
3611 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3612 btrfs_print_leaf(root, leaf);
3613 printk(KERN_CRIT "not enough freespace need %u have %d\n",
3614 total_size, btrfs_leaf_free_space(root, leaf));
3618 if (slot != nritems) {
3619 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3621 if (old_data < data_end) {
3622 btrfs_print_leaf(root, leaf);
3623 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3624 slot, old_data, data_end);
3628 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3630 /* first correct the data pointers */
3631 for (i = slot; i < nritems; i++) {
3634 item = btrfs_item_nr(leaf, i);
3635 ioff = btrfs_token_item_offset(leaf, item, &token);
3636 btrfs_set_token_item_offset(leaf, item,
3637 ioff - total_data, &token);
3639 /* shift the items */
3640 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3641 btrfs_item_nr_offset(slot),
3642 (nritems - slot) * sizeof(struct btrfs_item));
3644 /* shift the data */
3645 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3646 data_end - total_data, btrfs_leaf_data(leaf) +
3647 data_end, old_data - data_end);
3648 data_end = old_data;
3651 /* setup the item for the new data */
3652 for (i = 0; i < nr; i++) {
3653 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3654 btrfs_set_item_key(leaf, &disk_key, slot + i);
3655 item = btrfs_item_nr(leaf, slot + i);
3656 btrfs_set_token_item_offset(leaf, item,
3657 data_end - data_size[i], &token);
3658 data_end -= data_size[i];
3659 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
3662 btrfs_set_header_nritems(leaf, nritems + nr);
3665 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3666 fixup_low_keys(trans, root, path, &disk_key, 1);
3668 btrfs_unlock_up_safe(path, 1);
3669 btrfs_mark_buffer_dirty(leaf);
3671 if (btrfs_leaf_free_space(root, leaf) < 0) {
3672 btrfs_print_leaf(root, leaf);
3678 * Given a key and some data, insert items into the tree.
3679 * This does all the path init required, making room in the tree if needed.
3681 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3682 struct btrfs_root *root,
3683 struct btrfs_path *path,
3684 struct btrfs_key *cpu_key, u32 *data_size,
3693 for (i = 0; i < nr; i++)
3694 total_data += data_size[i];
3696 total_size = total_data + (nr * sizeof(struct btrfs_item));
3697 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3703 slot = path->slots[0];
3706 setup_items_for_insert(trans, root, path, cpu_key, data_size,
3707 total_data, total_size, nr);
3712 * Given a key and some data, insert an item into the tree.
3713 * This does all the path init required, making room in the tree if needed.
3715 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3716 *root, struct btrfs_key *cpu_key, void *data, u32
3720 struct btrfs_path *path;
3721 struct extent_buffer *leaf;
3724 path = btrfs_alloc_path();
3727 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
3729 leaf = path->nodes[0];
3730 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3731 write_extent_buffer(leaf, data, ptr, data_size);
3732 btrfs_mark_buffer_dirty(leaf);
3734 btrfs_free_path(path);
3739 * delete the pointer from a given node.
3741 * the tree should have been previously balanced so the deletion does not
3744 static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3745 struct btrfs_path *path, int level, int slot)
3747 struct extent_buffer *parent = path->nodes[level];
3750 nritems = btrfs_header_nritems(parent);
3751 if (slot != nritems - 1) {
3752 memmove_extent_buffer(parent,
3753 btrfs_node_key_ptr_offset(slot),
3754 btrfs_node_key_ptr_offset(slot + 1),
3755 sizeof(struct btrfs_key_ptr) *
3756 (nritems - slot - 1));
3759 btrfs_set_header_nritems(parent, nritems);
3760 if (nritems == 0 && parent == root->node) {
3761 BUG_ON(btrfs_header_level(root->node) != 1);
3762 /* just turn the root into a leaf and break */
3763 btrfs_set_header_level(root->node, 0);
3764 } else if (slot == 0) {
3765 struct btrfs_disk_key disk_key;
3767 btrfs_node_key(parent, &disk_key, 0);
3768 fixup_low_keys(trans, root, path, &disk_key, level + 1);
3770 btrfs_mark_buffer_dirty(parent);
3774 * a helper function to delete the leaf pointed to by path->slots[1] and
3777 * This deletes the pointer in path->nodes[1] and frees the leaf
3778 * block extent. zero is returned if it all worked out, < 0 otherwise.
3780 * The path must have already been setup for deleting the leaf, including
3781 * all the proper balancing. path->nodes[1] must be locked.
3783 static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
3784 struct btrfs_root *root,
3785 struct btrfs_path *path,
3786 struct extent_buffer *leaf)
3788 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
3789 del_ptr(trans, root, path, 1, path->slots[1]);
3792 * btrfs_free_extent is expensive, we want to make sure we
3793 * aren't holding any locks when we call it
3795 btrfs_unlock_up_safe(path, 0);
3797 root_sub_used(root, leaf->len);
3799 extent_buffer_get(leaf);
3800 btrfs_free_tree_block(trans, root, leaf, 0, 1, 0);
3801 free_extent_buffer_stale(leaf);
3804 * delete the item at the leaf level in path. If that empties
3805 * the leaf, remove it from the tree
3807 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3808 struct btrfs_path *path, int slot, int nr)
3810 struct extent_buffer *leaf;
3811 struct btrfs_item *item;
3818 struct btrfs_map_token token;
3820 btrfs_init_map_token(&token);
3822 leaf = path->nodes[0];
3823 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3825 for (i = 0; i < nr; i++)
3826 dsize += btrfs_item_size_nr(leaf, slot + i);
3828 nritems = btrfs_header_nritems(leaf);
3830 if (slot + nr != nritems) {
3831 int data_end = leaf_data_end(root, leaf);
3833 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3835 btrfs_leaf_data(leaf) + data_end,
3836 last_off - data_end);
3838 for (i = slot + nr; i < nritems; i++) {
3841 item = btrfs_item_nr(leaf, i);
3842 ioff = btrfs_token_item_offset(leaf, item, &token);
3843 btrfs_set_token_item_offset(leaf, item,
3844 ioff + dsize, &token);
3847 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
3848 btrfs_item_nr_offset(slot + nr),
3849 sizeof(struct btrfs_item) *
3850 (nritems - slot - nr));
3852 btrfs_set_header_nritems(leaf, nritems - nr);
3855 /* delete the leaf if we've emptied it */
3857 if (leaf == root->node) {
3858 btrfs_set_header_level(leaf, 0);
3860 btrfs_set_path_blocking(path);
3861 clean_tree_block(trans, root, leaf);
3862 btrfs_del_leaf(trans, root, path, leaf);
3865 int used = leaf_space_used(leaf, 0, nritems);
3867 struct btrfs_disk_key disk_key;
3869 btrfs_item_key(leaf, &disk_key, 0);
3870 fixup_low_keys(trans, root, path, &disk_key, 1);
3873 /* delete the leaf if it is mostly empty */
3874 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
3875 /* push_leaf_left fixes the path.
3876 * make sure the path still points to our leaf
3877 * for possible call to del_ptr below
3879 slot = path->slots[1];
3880 extent_buffer_get(leaf);
3882 btrfs_set_path_blocking(path);
3883 wret = push_leaf_left(trans, root, path, 1, 1,
3885 if (wret < 0 && wret != -ENOSPC)
3888 if (path->nodes[0] == leaf &&
3889 btrfs_header_nritems(leaf)) {
3890 wret = push_leaf_right(trans, root, path, 1,
3892 if (wret < 0 && wret != -ENOSPC)
3896 if (btrfs_header_nritems(leaf) == 0) {
3897 path->slots[1] = slot;
3898 btrfs_del_leaf(trans, root, path, leaf);
3899 free_extent_buffer(leaf);
3902 /* if we're still in the path, make sure
3903 * we're dirty. Otherwise, one of the
3904 * push_leaf functions must have already
3905 * dirtied this buffer
3907 if (path->nodes[0] == leaf)
3908 btrfs_mark_buffer_dirty(leaf);
3909 free_extent_buffer(leaf);
3912 btrfs_mark_buffer_dirty(leaf);
3919 * search the tree again to find a leaf with lesser keys
3920 * returns 0 if it found something or 1 if there are no lesser leaves.
3921 * returns < 0 on io errors.
3923 * This may release the path, and so you may lose any locks held at the
3926 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3928 struct btrfs_key key;
3929 struct btrfs_disk_key found_key;
3932 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
3936 else if (key.type > 0)
3938 else if (key.objectid > 0)
3943 btrfs_release_path(path);
3944 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3947 btrfs_item_key(path->nodes[0], &found_key, 0);
3948 ret = comp_keys(&found_key, &key);
3955 * A helper function to walk down the tree starting at min_key, and looking
3956 * for nodes or leaves that are either in cache or have a minimum
3957 * transaction id. This is used by the btree defrag code, and tree logging
3959 * This does not cow, but it does stuff the starting key it finds back
3960 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3961 * key and get a writable path.
3963 * This does lock as it descends, and path->keep_locks should be set
3964 * to 1 by the caller.
3966 * This honors path->lowest_level to prevent descent past a given level
3969 * min_trans indicates the oldest transaction that you are interested
3970 * in walking through. Any nodes or leaves older than min_trans are
3971 * skipped over (without reading them).
3973 * returns zero if something useful was found, < 0 on error and 1 if there
3974 * was nothing in the tree that matched the search criteria.
3976 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
3977 struct btrfs_key *max_key,
3978 struct btrfs_path *path, int cache_only,
3981 struct extent_buffer *cur;
3982 struct btrfs_key found_key;
3989 WARN_ON(!path->keep_locks);
3991 cur = btrfs_read_lock_root_node(root);
3992 level = btrfs_header_level(cur);
3993 WARN_ON(path->nodes[level]);
3994 path->nodes[level] = cur;
3995 path->locks[level] = BTRFS_READ_LOCK;
3997 if (btrfs_header_generation(cur) < min_trans) {
4002 nritems = btrfs_header_nritems(cur);
4003 level = btrfs_header_level(cur);
4004 sret = bin_search(cur, min_key, level, &slot);
4006 /* at the lowest level, we're done, setup the path and exit */
4007 if (level == path->lowest_level) {
4008 if (slot >= nritems)
4011 path->slots[level] = slot;
4012 btrfs_item_key_to_cpu(cur, &found_key, slot);
4015 if (sret && slot > 0)
4018 * check this node pointer against the cache_only and
4019 * min_trans parameters. If it isn't in cache or is too
4020 * old, skip to the next one.
4022 while (slot < nritems) {
4025 struct extent_buffer *tmp;
4026 struct btrfs_disk_key disk_key;
4028 blockptr = btrfs_node_blockptr(cur, slot);
4029 gen = btrfs_node_ptr_generation(cur, slot);
4030 if (gen < min_trans) {
4038 btrfs_node_key(cur, &disk_key, slot);
4039 if (comp_keys(&disk_key, max_key) >= 0) {
4045 tmp = btrfs_find_tree_block(root, blockptr,
4046 btrfs_level_size(root, level - 1));
4048 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
4049 free_extent_buffer(tmp);
4053 free_extent_buffer(tmp);
4058 * we didn't find a candidate key in this node, walk forward
4059 * and find another one
4061 if (slot >= nritems) {
4062 path->slots[level] = slot;
4063 btrfs_set_path_blocking(path);
4064 sret = btrfs_find_next_key(root, path, min_key, level,
4065 cache_only, min_trans);
4067 btrfs_release_path(path);
4073 /* save our key for returning back */
4074 btrfs_node_key_to_cpu(cur, &found_key, slot);
4075 path->slots[level] = slot;
4076 if (level == path->lowest_level) {
4078 unlock_up(path, level, 1, 0, NULL);
4081 btrfs_set_path_blocking(path);
4082 cur = read_node_slot(root, cur, slot);
4083 BUG_ON(!cur); /* -ENOMEM */
4085 btrfs_tree_read_lock(cur);
4087 path->locks[level - 1] = BTRFS_READ_LOCK;
4088 path->nodes[level - 1] = cur;
4089 unlock_up(path, level, 1, 0, NULL);
4090 btrfs_clear_path_blocking(path, NULL, 0);
4094 memcpy(min_key, &found_key, sizeof(found_key));
4095 btrfs_set_path_blocking(path);
4100 * this is similar to btrfs_next_leaf, but does not try to preserve
4101 * and fixup the path. It looks for and returns the next key in the
4102 * tree based on the current path and the cache_only and min_trans
4105 * 0 is returned if another key is found, < 0 if there are any errors
4106 * and 1 is returned if there are no higher keys in the tree
4108 * path->keep_locks should be set to 1 on the search made before
4109 * calling this function.
4111 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
4112 struct btrfs_key *key, int level,
4113 int cache_only, u64 min_trans)
4116 struct extent_buffer *c;
4118 WARN_ON(!path->keep_locks);
4119 while (level < BTRFS_MAX_LEVEL) {
4120 if (!path->nodes[level])
4123 slot = path->slots[level] + 1;
4124 c = path->nodes[level];
4126 if (slot >= btrfs_header_nritems(c)) {
4129 struct btrfs_key cur_key;
4130 if (level + 1 >= BTRFS_MAX_LEVEL ||
4131 !path->nodes[level + 1])
4134 if (path->locks[level + 1]) {
4139 slot = btrfs_header_nritems(c) - 1;
4141 btrfs_item_key_to_cpu(c, &cur_key, slot);
4143 btrfs_node_key_to_cpu(c, &cur_key, slot);
4145 orig_lowest = path->lowest_level;
4146 btrfs_release_path(path);
4147 path->lowest_level = level;
4148 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4150 path->lowest_level = orig_lowest;
4154 c = path->nodes[level];
4155 slot = path->slots[level];
4162 btrfs_item_key_to_cpu(c, key, slot);
4164 u64 blockptr = btrfs_node_blockptr(c, slot);
4165 u64 gen = btrfs_node_ptr_generation(c, slot);
4168 struct extent_buffer *cur;
4169 cur = btrfs_find_tree_block(root, blockptr,
4170 btrfs_level_size(root, level - 1));
4171 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4174 free_extent_buffer(cur);
4177 free_extent_buffer(cur);
4179 if (gen < min_trans) {
4183 btrfs_node_key_to_cpu(c, key, slot);
4191 * search the tree again to find a leaf with greater keys
4192 * returns 0 if it found something or 1 if there are no greater leaves.
4193 * returns < 0 on io errors.
4195 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
4199 struct extent_buffer *c;
4200 struct extent_buffer *next;
4201 struct btrfs_key key;
4204 int old_spinning = path->leave_spinning;
4205 int next_rw_lock = 0;
4207 nritems = btrfs_header_nritems(path->nodes[0]);
4211 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4216 btrfs_release_path(path);
4218 path->keep_locks = 1;
4219 path->leave_spinning = 1;
4221 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4222 path->keep_locks = 0;
4227 nritems = btrfs_header_nritems(path->nodes[0]);
4229 * by releasing the path above we dropped all our locks. A balance
4230 * could have added more items next to the key that used to be
4231 * at the very end of the block. So, check again here and
4232 * advance the path if there are now more items available.
4234 if (nritems > 0 && path->slots[0] < nritems - 1) {
4241 while (level < BTRFS_MAX_LEVEL) {
4242 if (!path->nodes[level]) {
4247 slot = path->slots[level] + 1;
4248 c = path->nodes[level];
4249 if (slot >= btrfs_header_nritems(c)) {
4251 if (level == BTRFS_MAX_LEVEL) {
4259 btrfs_tree_unlock_rw(next, next_rw_lock);
4260 free_extent_buffer(next);
4264 next_rw_lock = path->locks[level];
4265 ret = read_block_for_search(NULL, root, path, &next, level,
4271 btrfs_release_path(path);
4275 if (!path->skip_locking) {
4276 ret = btrfs_try_tree_read_lock(next);
4278 btrfs_set_path_blocking(path);
4279 btrfs_tree_read_lock(next);
4280 btrfs_clear_path_blocking(path, next,
4283 next_rw_lock = BTRFS_READ_LOCK;
4287 path->slots[level] = slot;
4290 c = path->nodes[level];
4291 if (path->locks[level])
4292 btrfs_tree_unlock_rw(c, path->locks[level]);
4294 free_extent_buffer(c);
4295 path->nodes[level] = next;
4296 path->slots[level] = 0;
4297 if (!path->skip_locking)
4298 path->locks[level] = next_rw_lock;
4302 ret = read_block_for_search(NULL, root, path, &next, level,
4308 btrfs_release_path(path);
4312 if (!path->skip_locking) {
4313 ret = btrfs_try_tree_read_lock(next);
4315 btrfs_set_path_blocking(path);
4316 btrfs_tree_read_lock(next);
4317 btrfs_clear_path_blocking(path, next,
4320 next_rw_lock = BTRFS_READ_LOCK;
4325 unlock_up(path, 0, 1, 0, NULL);
4326 path->leave_spinning = old_spinning;
4328 btrfs_set_path_blocking(path);
4334 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4335 * searching until it gets past min_objectid or finds an item of 'type'
4337 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4339 int btrfs_previous_item(struct btrfs_root *root,
4340 struct btrfs_path *path, u64 min_objectid,
4343 struct btrfs_key found_key;
4344 struct extent_buffer *leaf;
4349 if (path->slots[0] == 0) {
4350 btrfs_set_path_blocking(path);
4351 ret = btrfs_prev_leaf(root, path);
4357 leaf = path->nodes[0];
4358 nritems = btrfs_header_nritems(leaf);
4361 if (path->slots[0] == nritems)
4364 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4365 if (found_key.objectid < min_objectid)
4367 if (found_key.type == type)
4369 if (found_key.objectid == min_objectid &&
4370 found_key.type < type)