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;
160 eb = rcu_dereference(root->node);
161 extent_buffer_get(eb);
166 /* loop around taking references on and locking the root node of the
167 * tree until you end up with a lock on the root. A locked buffer
168 * is returned, with a reference held.
170 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
172 struct extent_buffer *eb;
175 eb = btrfs_root_node(root);
177 if (eb == root->node)
179 btrfs_tree_unlock(eb);
180 free_extent_buffer(eb);
185 /* loop around taking references on and locking the root node of the
186 * tree until you end up with a lock on the root. A locked buffer
187 * is returned, with a reference held.
189 struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
191 struct extent_buffer *eb;
194 eb = btrfs_root_node(root);
195 btrfs_tree_read_lock(eb);
196 if (eb == root->node)
198 btrfs_tree_read_unlock(eb);
199 free_extent_buffer(eb);
204 /* cowonly root (everything not a reference counted cow subvolume), just get
205 * put onto a simple dirty list. transaction.c walks this to make sure they
206 * get properly updated on disk.
208 static void add_root_to_dirty_list(struct btrfs_root *root)
210 if (root->track_dirty && list_empty(&root->dirty_list)) {
211 list_add(&root->dirty_list,
212 &root->fs_info->dirty_cowonly_roots);
217 * used by snapshot creation to make a copy of a root for a tree with
218 * a given objectid. The buffer with the new root node is returned in
219 * cow_ret, and this func returns zero on success or a negative error code.
221 int btrfs_copy_root(struct btrfs_trans_handle *trans,
222 struct btrfs_root *root,
223 struct extent_buffer *buf,
224 struct extent_buffer **cow_ret, u64 new_root_objectid)
226 struct extent_buffer *cow;
229 struct btrfs_disk_key disk_key;
231 WARN_ON(root->ref_cows && trans->transid !=
232 root->fs_info->running_transaction->transid);
233 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
235 level = btrfs_header_level(buf);
237 btrfs_item_key(buf, &disk_key, 0);
239 btrfs_node_key(buf, &disk_key, 0);
241 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
242 new_root_objectid, &disk_key, level,
247 copy_extent_buffer(cow, buf, 0, 0, cow->len);
248 btrfs_set_header_bytenr(cow, cow->start);
249 btrfs_set_header_generation(cow, trans->transid);
250 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
251 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
252 BTRFS_HEADER_FLAG_RELOC);
253 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
254 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
256 btrfs_set_header_owner(cow, new_root_objectid);
258 write_extent_buffer(cow, root->fs_info->fsid,
259 (unsigned long)btrfs_header_fsid(cow),
262 WARN_ON(btrfs_header_generation(buf) > trans->transid);
263 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
264 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
266 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
271 btrfs_mark_buffer_dirty(cow);
277 * check if the tree block can be shared by multiple trees
279 int btrfs_block_can_be_shared(struct btrfs_root *root,
280 struct extent_buffer *buf)
283 * Tree blocks not in refernece counted trees and tree roots
284 * are never shared. If a block was allocated after the last
285 * snapshot and the block was not allocated by tree relocation,
286 * we know the block is not shared.
288 if (root->ref_cows &&
289 buf != root->node && buf != root->commit_root &&
290 (btrfs_header_generation(buf) <=
291 btrfs_root_last_snapshot(&root->root_item) ||
292 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
294 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
295 if (root->ref_cows &&
296 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
302 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
303 struct btrfs_root *root,
304 struct extent_buffer *buf,
305 struct extent_buffer *cow,
315 * Backrefs update rules:
317 * Always use full backrefs for extent pointers in tree block
318 * allocated by tree relocation.
320 * If a shared tree block is no longer referenced by its owner
321 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
322 * use full backrefs for extent pointers in tree block.
324 * If a tree block is been relocating
325 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
326 * use full backrefs for extent pointers in tree block.
327 * The reason for this is some operations (such as drop tree)
328 * are only allowed for blocks use full backrefs.
331 if (btrfs_block_can_be_shared(root, buf)) {
332 ret = btrfs_lookup_extent_info(trans, root, buf->start,
333 buf->len, &refs, &flags);
338 btrfs_std_error(root->fs_info, ret);
343 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
344 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
345 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
350 owner = btrfs_header_owner(buf);
351 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
352 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
355 if ((owner == root->root_key.objectid ||
356 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
357 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
358 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
359 BUG_ON(ret); /* -ENOMEM */
361 if (root->root_key.objectid ==
362 BTRFS_TREE_RELOC_OBJECTID) {
363 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
364 BUG_ON(ret); /* -ENOMEM */
365 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
366 BUG_ON(ret); /* -ENOMEM */
368 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
371 if (root->root_key.objectid ==
372 BTRFS_TREE_RELOC_OBJECTID)
373 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
375 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
376 BUG_ON(ret); /* -ENOMEM */
378 if (new_flags != 0) {
379 ret = btrfs_set_disk_extent_flags(trans, root,
387 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
388 if (root->root_key.objectid ==
389 BTRFS_TREE_RELOC_OBJECTID)
390 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
392 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
393 BUG_ON(ret); /* -ENOMEM */
394 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
395 BUG_ON(ret); /* -ENOMEM */
397 clean_tree_block(trans, root, buf);
404 * does the dirty work in cow of a single block. The parent block (if
405 * supplied) is updated to point to the new cow copy. The new buffer is marked
406 * dirty and returned locked. If you modify the block it needs to be marked
409 * search_start -- an allocation hint for the new block
411 * empty_size -- a hint that you plan on doing more cow. This is the size in
412 * bytes the allocator should try to find free next to the block it returns.
413 * This is just a hint and may be ignored by the allocator.
415 static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
416 struct btrfs_root *root,
417 struct extent_buffer *buf,
418 struct extent_buffer *parent, int parent_slot,
419 struct extent_buffer **cow_ret,
420 u64 search_start, u64 empty_size)
422 struct btrfs_disk_key disk_key;
423 struct extent_buffer *cow;
432 btrfs_assert_tree_locked(buf);
434 WARN_ON(root->ref_cows && trans->transid !=
435 root->fs_info->running_transaction->transid);
436 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
438 level = btrfs_header_level(buf);
441 btrfs_item_key(buf, &disk_key, 0);
443 btrfs_node_key(buf, &disk_key, 0);
445 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
447 parent_start = parent->start;
453 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
454 root->root_key.objectid, &disk_key,
455 level, search_start, empty_size, 1);
459 /* cow is set to blocking by btrfs_init_new_buffer */
461 copy_extent_buffer(cow, buf, 0, 0, cow->len);
462 btrfs_set_header_bytenr(cow, cow->start);
463 btrfs_set_header_generation(cow, trans->transid);
464 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
465 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
466 BTRFS_HEADER_FLAG_RELOC);
467 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
468 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
470 btrfs_set_header_owner(cow, root->root_key.objectid);
472 write_extent_buffer(cow, root->fs_info->fsid,
473 (unsigned long)btrfs_header_fsid(cow),
476 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
478 btrfs_abort_transaction(trans, root, ret);
483 btrfs_reloc_cow_block(trans, root, buf, cow);
485 if (buf == root->node) {
486 WARN_ON(parent && parent != buf);
487 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
488 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
489 parent_start = buf->start;
493 extent_buffer_get(cow);
494 rcu_assign_pointer(root->node, cow);
496 btrfs_free_tree_block(trans, root, buf, parent_start,
498 free_extent_buffer(buf);
499 add_root_to_dirty_list(root);
501 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
502 parent_start = parent->start;
506 WARN_ON(trans->transid != btrfs_header_generation(parent));
507 btrfs_set_node_blockptr(parent, parent_slot,
509 btrfs_set_node_ptr_generation(parent, parent_slot,
511 btrfs_mark_buffer_dirty(parent);
512 btrfs_free_tree_block(trans, root, buf, parent_start,
516 btrfs_tree_unlock(buf);
517 free_extent_buffer(buf);
518 btrfs_mark_buffer_dirty(cow);
523 static inline int should_cow_block(struct btrfs_trans_handle *trans,
524 struct btrfs_root *root,
525 struct extent_buffer *buf)
527 /* ensure we can see the force_cow */
531 * We do not need to cow a block if
532 * 1) this block is not created or changed in this transaction;
533 * 2) this block does not belong to TREE_RELOC tree;
534 * 3) the root is not forced COW.
536 * What is forced COW:
537 * when we create snapshot during commiting the transaction,
538 * after we've finished coping src root, we must COW the shared
539 * block to ensure the metadata consistency.
541 if (btrfs_header_generation(buf) == trans->transid &&
542 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
543 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
544 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
551 * cows a single block, see __btrfs_cow_block for the real work.
552 * This version of it has extra checks so that a block isn't cow'd more than
553 * once per transaction, as long as it hasn't been written yet
555 noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
556 struct btrfs_root *root, struct extent_buffer *buf,
557 struct extent_buffer *parent, int parent_slot,
558 struct extent_buffer **cow_ret)
563 if (trans->transaction != root->fs_info->running_transaction) {
564 printk(KERN_CRIT "trans %llu running %llu\n",
565 (unsigned long long)trans->transid,
567 root->fs_info->running_transaction->transid);
570 if (trans->transid != root->fs_info->generation) {
571 printk(KERN_CRIT "trans %llu running %llu\n",
572 (unsigned long long)trans->transid,
573 (unsigned long long)root->fs_info->generation);
577 if (!should_cow_block(trans, root, buf)) {
582 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
585 btrfs_set_lock_blocking(parent);
586 btrfs_set_lock_blocking(buf);
588 ret = __btrfs_cow_block(trans, root, buf, parent,
589 parent_slot, cow_ret, search_start, 0);
591 trace_btrfs_cow_block(root, buf, *cow_ret);
597 * helper function for defrag to decide if two blocks pointed to by a
598 * node are actually close by
600 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
602 if (blocknr < other && other - (blocknr + blocksize) < 32768)
604 if (blocknr > other && blocknr - (other + blocksize) < 32768)
610 * compare two keys in a memcmp fashion
612 static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
616 btrfs_disk_key_to_cpu(&k1, disk);
618 return btrfs_comp_cpu_keys(&k1, k2);
622 * same as comp_keys only with two btrfs_key's
624 int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
626 if (k1->objectid > k2->objectid)
628 if (k1->objectid < k2->objectid)
630 if (k1->type > k2->type)
632 if (k1->type < k2->type)
634 if (k1->offset > k2->offset)
636 if (k1->offset < k2->offset)
642 * this is used by the defrag code to go through all the
643 * leaves pointed to by a node and reallocate them so that
644 * disk order is close to key order
646 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
647 struct btrfs_root *root, struct extent_buffer *parent,
648 int start_slot, int cache_only, u64 *last_ret,
649 struct btrfs_key *progress)
651 struct extent_buffer *cur;
654 u64 search_start = *last_ret;
664 int progress_passed = 0;
665 struct btrfs_disk_key disk_key;
667 parent_level = btrfs_header_level(parent);
668 if (cache_only && parent_level != 1)
671 if (trans->transaction != root->fs_info->running_transaction)
673 if (trans->transid != root->fs_info->generation)
676 parent_nritems = btrfs_header_nritems(parent);
677 blocksize = btrfs_level_size(root, parent_level - 1);
678 end_slot = parent_nritems;
680 if (parent_nritems == 1)
683 btrfs_set_lock_blocking(parent);
685 for (i = start_slot; i < end_slot; i++) {
688 btrfs_node_key(parent, &disk_key, i);
689 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
693 blocknr = btrfs_node_blockptr(parent, i);
694 gen = btrfs_node_ptr_generation(parent, i);
696 last_block = blocknr;
699 other = btrfs_node_blockptr(parent, i - 1);
700 close = close_blocks(blocknr, other, blocksize);
702 if (!close && i < end_slot - 2) {
703 other = btrfs_node_blockptr(parent, i + 1);
704 close = close_blocks(blocknr, other, blocksize);
707 last_block = blocknr;
711 cur = btrfs_find_tree_block(root, blocknr, blocksize);
713 uptodate = btrfs_buffer_uptodate(cur, gen);
716 if (!cur || !uptodate) {
718 free_extent_buffer(cur);
722 cur = read_tree_block(root, blocknr,
726 } else if (!uptodate) {
727 btrfs_read_buffer(cur, gen);
730 if (search_start == 0)
731 search_start = last_block;
733 btrfs_tree_lock(cur);
734 btrfs_set_lock_blocking(cur);
735 err = __btrfs_cow_block(trans, root, cur, parent, i,
738 (end_slot - i) * blocksize));
740 btrfs_tree_unlock(cur);
741 free_extent_buffer(cur);
744 search_start = cur->start;
745 last_block = cur->start;
746 *last_ret = search_start;
747 btrfs_tree_unlock(cur);
748 free_extent_buffer(cur);
754 * The leaf data grows from end-to-front in the node.
755 * this returns the address of the start of the last item,
756 * which is the stop of the leaf data stack
758 static inline unsigned int leaf_data_end(struct btrfs_root *root,
759 struct extent_buffer *leaf)
761 u32 nr = btrfs_header_nritems(leaf);
763 return BTRFS_LEAF_DATA_SIZE(root);
764 return btrfs_item_offset_nr(leaf, nr - 1);
769 * search for key in the extent_buffer. The items start at offset p,
770 * and they are item_size apart. There are 'max' items in p.
772 * the slot in the array is returned via slot, and it points to
773 * the place where you would insert key if it is not found in
776 * slot may point to max if the key is bigger than all of the keys
778 static noinline int generic_bin_search(struct extent_buffer *eb,
780 int item_size, struct btrfs_key *key,
787 struct btrfs_disk_key *tmp = NULL;
788 struct btrfs_disk_key unaligned;
789 unsigned long offset;
791 unsigned long map_start = 0;
792 unsigned long map_len = 0;
796 mid = (low + high) / 2;
797 offset = p + mid * item_size;
799 if (!kaddr || offset < map_start ||
800 (offset + sizeof(struct btrfs_disk_key)) >
801 map_start + map_len) {
803 err = map_private_extent_buffer(eb, offset,
804 sizeof(struct btrfs_disk_key),
805 &kaddr, &map_start, &map_len);
808 tmp = (struct btrfs_disk_key *)(kaddr + offset -
811 read_extent_buffer(eb, &unaligned,
812 offset, sizeof(unaligned));
817 tmp = (struct btrfs_disk_key *)(kaddr + offset -
820 ret = comp_keys(tmp, key);
836 * simple bin_search frontend that does the right thing for
839 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
840 int level, int *slot)
843 return generic_bin_search(eb,
844 offsetof(struct btrfs_leaf, items),
845 sizeof(struct btrfs_item),
846 key, btrfs_header_nritems(eb),
849 return generic_bin_search(eb,
850 offsetof(struct btrfs_node, ptrs),
851 sizeof(struct btrfs_key_ptr),
852 key, btrfs_header_nritems(eb),
858 int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
859 int level, int *slot)
861 return bin_search(eb, key, level, slot);
864 static void root_add_used(struct btrfs_root *root, u32 size)
866 spin_lock(&root->accounting_lock);
867 btrfs_set_root_used(&root->root_item,
868 btrfs_root_used(&root->root_item) + size);
869 spin_unlock(&root->accounting_lock);
872 static void root_sub_used(struct btrfs_root *root, u32 size)
874 spin_lock(&root->accounting_lock);
875 btrfs_set_root_used(&root->root_item,
876 btrfs_root_used(&root->root_item) - size);
877 spin_unlock(&root->accounting_lock);
880 /* given a node and slot number, this reads the blocks it points to. The
881 * extent buffer is returned with a reference taken (but unlocked).
882 * NULL is returned on error.
884 static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
885 struct extent_buffer *parent, int slot)
887 int level = btrfs_header_level(parent);
890 if (slot >= btrfs_header_nritems(parent))
895 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
896 btrfs_level_size(root, level - 1),
897 btrfs_node_ptr_generation(parent, slot));
901 * node level balancing, used to make sure nodes are in proper order for
902 * item deletion. We balance from the top down, so we have to make sure
903 * that a deletion won't leave an node completely empty later on.
905 static noinline int balance_level(struct btrfs_trans_handle *trans,
906 struct btrfs_root *root,
907 struct btrfs_path *path, int level)
909 struct extent_buffer *right = NULL;
910 struct extent_buffer *mid;
911 struct extent_buffer *left = NULL;
912 struct extent_buffer *parent = NULL;
916 int orig_slot = path->slots[level];
922 mid = path->nodes[level];
924 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
925 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
926 WARN_ON(btrfs_header_generation(mid) != trans->transid);
928 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
930 if (level < BTRFS_MAX_LEVEL - 1) {
931 parent = path->nodes[level + 1];
932 pslot = path->slots[level + 1];
936 * deal with the case where there is only one pointer in the root
937 * by promoting the node below to a root
940 struct extent_buffer *child;
942 if (btrfs_header_nritems(mid) != 1)
945 /* promote the child to a root */
946 child = read_node_slot(root, mid, 0);
949 btrfs_std_error(root->fs_info, ret);
953 btrfs_tree_lock(child);
954 btrfs_set_lock_blocking(child);
955 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
957 btrfs_tree_unlock(child);
958 free_extent_buffer(child);
962 rcu_assign_pointer(root->node, child);
964 add_root_to_dirty_list(root);
965 btrfs_tree_unlock(child);
967 path->locks[level] = 0;
968 path->nodes[level] = NULL;
969 clean_tree_block(trans, root, mid);
970 btrfs_tree_unlock(mid);
971 /* once for the path */
972 free_extent_buffer(mid);
974 root_sub_used(root, mid->len);
975 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
976 /* once for the root ptr */
977 free_extent_buffer(mid);
980 if (btrfs_header_nritems(mid) >
981 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
984 btrfs_header_nritems(mid);
986 left = read_node_slot(root, parent, pslot - 1);
988 btrfs_tree_lock(left);
989 btrfs_set_lock_blocking(left);
990 wret = btrfs_cow_block(trans, root, left,
991 parent, pslot - 1, &left);
997 right = read_node_slot(root, parent, pslot + 1);
999 btrfs_tree_lock(right);
1000 btrfs_set_lock_blocking(right);
1001 wret = btrfs_cow_block(trans, root, right,
1002 parent, pslot + 1, &right);
1009 /* first, try to make some room in the middle buffer */
1011 orig_slot += btrfs_header_nritems(left);
1012 wret = push_node_left(trans, root, left, mid, 1);
1015 btrfs_header_nritems(mid);
1019 * then try to empty the right most buffer into the middle
1022 wret = push_node_left(trans, root, mid, right, 1);
1023 if (wret < 0 && wret != -ENOSPC)
1025 if (btrfs_header_nritems(right) == 0) {
1026 clean_tree_block(trans, root, right);
1027 btrfs_tree_unlock(right);
1028 del_ptr(trans, root, path, level + 1, pslot + 1);
1029 root_sub_used(root, right->len);
1030 btrfs_free_tree_block(trans, root, right, 0, 1, 0);
1031 free_extent_buffer(right);
1034 struct btrfs_disk_key right_key;
1035 btrfs_node_key(right, &right_key, 0);
1036 btrfs_set_node_key(parent, &right_key, pslot + 1);
1037 btrfs_mark_buffer_dirty(parent);
1040 if (btrfs_header_nritems(mid) == 1) {
1042 * we're not allowed to leave a node with one item in the
1043 * tree during a delete. A deletion from lower in the tree
1044 * could try to delete the only pointer in this node.
1045 * So, pull some keys from the left.
1046 * There has to be a left pointer at this point because
1047 * otherwise we would have pulled some pointers from the
1052 btrfs_std_error(root->fs_info, ret);
1055 wret = balance_node_right(trans, root, mid, left);
1061 wret = push_node_left(trans, root, left, mid, 1);
1067 if (btrfs_header_nritems(mid) == 0) {
1068 clean_tree_block(trans, root, mid);
1069 btrfs_tree_unlock(mid);
1070 del_ptr(trans, root, path, level + 1, pslot);
1071 root_sub_used(root, mid->len);
1072 btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
1073 free_extent_buffer(mid);
1076 /* update the parent key to reflect our changes */
1077 struct btrfs_disk_key mid_key;
1078 btrfs_node_key(mid, &mid_key, 0);
1079 btrfs_set_node_key(parent, &mid_key, pslot);
1080 btrfs_mark_buffer_dirty(parent);
1083 /* update the path */
1085 if (btrfs_header_nritems(left) > orig_slot) {
1086 extent_buffer_get(left);
1087 /* left was locked after cow */
1088 path->nodes[level] = left;
1089 path->slots[level + 1] -= 1;
1090 path->slots[level] = orig_slot;
1092 btrfs_tree_unlock(mid);
1093 free_extent_buffer(mid);
1096 orig_slot -= btrfs_header_nritems(left);
1097 path->slots[level] = orig_slot;
1100 /* double check we haven't messed things up */
1102 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
1106 btrfs_tree_unlock(right);
1107 free_extent_buffer(right);
1110 if (path->nodes[level] != left)
1111 btrfs_tree_unlock(left);
1112 free_extent_buffer(left);
1117 /* Node balancing for insertion. Here we only split or push nodes around
1118 * when they are completely full. This is also done top down, so we
1119 * have to be pessimistic.
1121 static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1122 struct btrfs_root *root,
1123 struct btrfs_path *path, int level)
1125 struct extent_buffer *right = NULL;
1126 struct extent_buffer *mid;
1127 struct extent_buffer *left = NULL;
1128 struct extent_buffer *parent = NULL;
1132 int orig_slot = path->slots[level];
1137 mid = path->nodes[level];
1138 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1140 if (level < BTRFS_MAX_LEVEL - 1) {
1141 parent = path->nodes[level + 1];
1142 pslot = path->slots[level + 1];
1148 left = read_node_slot(root, parent, pslot - 1);
1150 /* first, try to make some room in the middle buffer */
1154 btrfs_tree_lock(left);
1155 btrfs_set_lock_blocking(left);
1157 left_nr = btrfs_header_nritems(left);
1158 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1161 ret = btrfs_cow_block(trans, root, left, parent,
1166 wret = push_node_left(trans, root,
1173 struct btrfs_disk_key disk_key;
1174 orig_slot += left_nr;
1175 btrfs_node_key(mid, &disk_key, 0);
1176 btrfs_set_node_key(parent, &disk_key, pslot);
1177 btrfs_mark_buffer_dirty(parent);
1178 if (btrfs_header_nritems(left) > orig_slot) {
1179 path->nodes[level] = left;
1180 path->slots[level + 1] -= 1;
1181 path->slots[level] = orig_slot;
1182 btrfs_tree_unlock(mid);
1183 free_extent_buffer(mid);
1186 btrfs_header_nritems(left);
1187 path->slots[level] = orig_slot;
1188 btrfs_tree_unlock(left);
1189 free_extent_buffer(left);
1193 btrfs_tree_unlock(left);
1194 free_extent_buffer(left);
1196 right = read_node_slot(root, parent, pslot + 1);
1199 * then try to empty the right most buffer into the middle
1204 btrfs_tree_lock(right);
1205 btrfs_set_lock_blocking(right);
1207 right_nr = btrfs_header_nritems(right);
1208 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1211 ret = btrfs_cow_block(trans, root, right,
1217 wret = balance_node_right(trans, root,
1224 struct btrfs_disk_key disk_key;
1226 btrfs_node_key(right, &disk_key, 0);
1227 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1228 btrfs_mark_buffer_dirty(parent);
1230 if (btrfs_header_nritems(mid) <= orig_slot) {
1231 path->nodes[level] = right;
1232 path->slots[level + 1] += 1;
1233 path->slots[level] = orig_slot -
1234 btrfs_header_nritems(mid);
1235 btrfs_tree_unlock(mid);
1236 free_extent_buffer(mid);
1238 btrfs_tree_unlock(right);
1239 free_extent_buffer(right);
1243 btrfs_tree_unlock(right);
1244 free_extent_buffer(right);
1250 * readahead one full node of leaves, finding things that are close
1251 * to the block in 'slot', and triggering ra on them.
1253 static void reada_for_search(struct btrfs_root *root,
1254 struct btrfs_path *path,
1255 int level, int slot, u64 objectid)
1257 struct extent_buffer *node;
1258 struct btrfs_disk_key disk_key;
1264 int direction = path->reada;
1265 struct extent_buffer *eb;
1273 if (!path->nodes[level])
1276 node = path->nodes[level];
1278 search = btrfs_node_blockptr(node, slot);
1279 blocksize = btrfs_level_size(root, level - 1);
1280 eb = btrfs_find_tree_block(root, search, blocksize);
1282 free_extent_buffer(eb);
1288 nritems = btrfs_header_nritems(node);
1292 if (direction < 0) {
1296 } else if (direction > 0) {
1301 if (path->reada < 0 && objectid) {
1302 btrfs_node_key(node, &disk_key, nr);
1303 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1306 search = btrfs_node_blockptr(node, nr);
1307 if ((search <= target && target - search <= 65536) ||
1308 (search > target && search - target <= 65536)) {
1309 gen = btrfs_node_ptr_generation(node, nr);
1310 readahead_tree_block(root, search, blocksize, gen);
1314 if ((nread > 65536 || nscan > 32))
1320 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1323 static noinline int reada_for_balance(struct btrfs_root *root,
1324 struct btrfs_path *path, int level)
1328 struct extent_buffer *parent;
1329 struct extent_buffer *eb;
1336 parent = path->nodes[level + 1];
1340 nritems = btrfs_header_nritems(parent);
1341 slot = path->slots[level + 1];
1342 blocksize = btrfs_level_size(root, level);
1345 block1 = btrfs_node_blockptr(parent, slot - 1);
1346 gen = btrfs_node_ptr_generation(parent, slot - 1);
1347 eb = btrfs_find_tree_block(root, block1, blocksize);
1348 if (eb && btrfs_buffer_uptodate(eb, gen))
1350 free_extent_buffer(eb);
1352 if (slot + 1 < nritems) {
1353 block2 = btrfs_node_blockptr(parent, slot + 1);
1354 gen = btrfs_node_ptr_generation(parent, slot + 1);
1355 eb = btrfs_find_tree_block(root, block2, blocksize);
1356 if (eb && btrfs_buffer_uptodate(eb, gen))
1358 free_extent_buffer(eb);
1360 if (block1 || block2) {
1363 /* release the whole path */
1364 btrfs_release_path(path);
1366 /* read the blocks */
1368 readahead_tree_block(root, block1, blocksize, 0);
1370 readahead_tree_block(root, block2, blocksize, 0);
1373 eb = read_tree_block(root, block1, blocksize, 0);
1374 free_extent_buffer(eb);
1377 eb = read_tree_block(root, block2, blocksize, 0);
1378 free_extent_buffer(eb);
1386 * when we walk down the tree, it is usually safe to unlock the higher layers
1387 * in the tree. The exceptions are when our path goes through slot 0, because
1388 * operations on the tree might require changing key pointers higher up in the
1391 * callers might also have set path->keep_locks, which tells this code to keep
1392 * the lock if the path points to the last slot in the block. This is part of
1393 * walking through the tree, and selecting the next slot in the higher block.
1395 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1396 * if lowest_unlock is 1, level 0 won't be unlocked
1398 static noinline void unlock_up(struct btrfs_path *path, int level,
1402 int skip_level = level;
1404 struct extent_buffer *t;
1406 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1407 if (!path->nodes[i])
1409 if (!path->locks[i])
1411 if (!no_skips && path->slots[i] == 0) {
1415 if (!no_skips && path->keep_locks) {
1418 nritems = btrfs_header_nritems(t);
1419 if (nritems < 1 || path->slots[i] >= nritems - 1) {
1424 if (skip_level < i && i >= lowest_unlock)
1428 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1429 btrfs_tree_unlock_rw(t, path->locks[i]);
1436 * This releases any locks held in the path starting at level and
1437 * going all the way up to the root.
1439 * btrfs_search_slot will keep the lock held on higher nodes in a few
1440 * corner cases, such as COW of the block at slot zero in the node. This
1441 * ignores those rules, and it should only be called when there are no
1442 * more updates to be done higher up in the tree.
1444 noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1448 if (path->keep_locks)
1451 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1452 if (!path->nodes[i])
1454 if (!path->locks[i])
1456 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
1462 * helper function for btrfs_search_slot. The goal is to find a block
1463 * in cache without setting the path to blocking. If we find the block
1464 * we return zero and the path is unchanged.
1466 * If we can't find the block, we set the path blocking and do some
1467 * reada. -EAGAIN is returned and the search must be repeated.
1470 read_block_for_search(struct btrfs_trans_handle *trans,
1471 struct btrfs_root *root, struct btrfs_path *p,
1472 struct extent_buffer **eb_ret, int level, int slot,
1473 struct btrfs_key *key)
1478 struct extent_buffer *b = *eb_ret;
1479 struct extent_buffer *tmp;
1482 blocknr = btrfs_node_blockptr(b, slot);
1483 gen = btrfs_node_ptr_generation(b, slot);
1484 blocksize = btrfs_level_size(root, level - 1);
1486 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1488 if (btrfs_buffer_uptodate(tmp, 0)) {
1489 if (btrfs_buffer_uptodate(tmp, gen)) {
1491 * we found an up to date block without
1498 /* the pages were up to date, but we failed
1499 * the generation number check. Do a full
1500 * read for the generation number that is correct.
1501 * We must do this without dropping locks so
1502 * we can trust our generation number
1504 free_extent_buffer(tmp);
1505 btrfs_set_path_blocking(p);
1507 tmp = read_tree_block(root, blocknr, blocksize, gen);
1508 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1512 free_extent_buffer(tmp);
1513 btrfs_release_path(p);
1519 * reduce lock contention at high levels
1520 * of the btree by dropping locks before
1521 * we read. Don't release the lock on the current
1522 * level because we need to walk this node to figure
1523 * out which blocks to read.
1525 btrfs_unlock_up_safe(p, level + 1);
1526 btrfs_set_path_blocking(p);
1528 free_extent_buffer(tmp);
1530 reada_for_search(root, p, level, slot, key->objectid);
1532 btrfs_release_path(p);
1535 tmp = read_tree_block(root, blocknr, blocksize, 0);
1538 * If the read above didn't mark this buffer up to date,
1539 * it will never end up being up to date. Set ret to EIO now
1540 * and give up so that our caller doesn't loop forever
1543 if (!btrfs_buffer_uptodate(tmp, 0))
1545 free_extent_buffer(tmp);
1551 * helper function for btrfs_search_slot. This does all of the checks
1552 * for node-level blocks and does any balancing required based on
1555 * If no extra work was required, zero is returned. If we had to
1556 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1560 setup_nodes_for_search(struct btrfs_trans_handle *trans,
1561 struct btrfs_root *root, struct btrfs_path *p,
1562 struct extent_buffer *b, int level, int ins_len,
1563 int *write_lock_level)
1566 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1567 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1570 if (*write_lock_level < level + 1) {
1571 *write_lock_level = level + 1;
1572 btrfs_release_path(p);
1576 sret = reada_for_balance(root, p, level);
1580 btrfs_set_path_blocking(p);
1581 sret = split_node(trans, root, p, level);
1582 btrfs_clear_path_blocking(p, NULL, 0);
1589 b = p->nodes[level];
1590 } else if (ins_len < 0 && btrfs_header_nritems(b) <
1591 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
1594 if (*write_lock_level < level + 1) {
1595 *write_lock_level = level + 1;
1596 btrfs_release_path(p);
1600 sret = reada_for_balance(root, p, level);
1604 btrfs_set_path_blocking(p);
1605 sret = balance_level(trans, root, p, level);
1606 btrfs_clear_path_blocking(p, NULL, 0);
1612 b = p->nodes[level];
1614 btrfs_release_path(p);
1617 BUG_ON(btrfs_header_nritems(b) == 1);
1628 * look for key in the tree. path is filled in with nodes along the way
1629 * if key is found, we return zero and you can find the item in the leaf
1630 * level of the path (level 0)
1632 * If the key isn't found, the path points to the slot where it should
1633 * be inserted, and 1 is returned. If there are other errors during the
1634 * search a negative error number is returned.
1636 * if ins_len > 0, nodes and leaves will be split as we walk down the
1637 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1640 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1641 *root, struct btrfs_key *key, struct btrfs_path *p, int
1644 struct extent_buffer *b;
1649 int lowest_unlock = 1;
1651 /* everything at write_lock_level or lower must be write locked */
1652 int write_lock_level = 0;
1653 u8 lowest_level = 0;
1655 lowest_level = p->lowest_level;
1656 WARN_ON(lowest_level && ins_len > 0);
1657 WARN_ON(p->nodes[0] != NULL);
1662 /* when we are removing items, we might have to go up to level
1663 * two as we update tree pointers Make sure we keep write
1664 * for those levels as well
1666 write_lock_level = 2;
1667 } else if (ins_len > 0) {
1669 * for inserting items, make sure we have a write lock on
1670 * level 1 so we can update keys
1672 write_lock_level = 1;
1676 write_lock_level = -1;
1678 if (cow && (p->keep_locks || p->lowest_level))
1679 write_lock_level = BTRFS_MAX_LEVEL;
1683 * we try very hard to do read locks on the root
1685 root_lock = BTRFS_READ_LOCK;
1687 if (p->search_commit_root) {
1689 * the commit roots are read only
1690 * so we always do read locks
1692 b = root->commit_root;
1693 extent_buffer_get(b);
1694 level = btrfs_header_level(b);
1695 if (!p->skip_locking)
1696 btrfs_tree_read_lock(b);
1698 if (p->skip_locking) {
1699 b = btrfs_root_node(root);
1700 level = btrfs_header_level(b);
1702 /* we don't know the level of the root node
1703 * until we actually have it read locked
1705 b = btrfs_read_lock_root_node(root);
1706 level = btrfs_header_level(b);
1707 if (level <= write_lock_level) {
1708 /* whoops, must trade for write lock */
1709 btrfs_tree_read_unlock(b);
1710 free_extent_buffer(b);
1711 b = btrfs_lock_root_node(root);
1712 root_lock = BTRFS_WRITE_LOCK;
1714 /* the level might have changed, check again */
1715 level = btrfs_header_level(b);
1719 p->nodes[level] = b;
1720 if (!p->skip_locking)
1721 p->locks[level] = root_lock;
1724 level = btrfs_header_level(b);
1727 * setup the path here so we can release it under lock
1728 * contention with the cow code
1732 * if we don't really need to cow this block
1733 * then we don't want to set the path blocking,
1734 * so we test it here
1736 if (!should_cow_block(trans, root, b))
1739 btrfs_set_path_blocking(p);
1742 * must have write locks on this node and the
1745 if (level + 1 > write_lock_level) {
1746 write_lock_level = level + 1;
1747 btrfs_release_path(p);
1751 err = btrfs_cow_block(trans, root, b,
1752 p->nodes[level + 1],
1753 p->slots[level + 1], &b);
1760 BUG_ON(!cow && ins_len);
1762 p->nodes[level] = b;
1763 btrfs_clear_path_blocking(p, NULL, 0);
1766 * we have a lock on b and as long as we aren't changing
1767 * the tree, there is no way to for the items in b to change.
1768 * It is safe to drop the lock on our parent before we
1769 * go through the expensive btree search on b.
1771 * If cow is true, then we might be changing slot zero,
1772 * which may require changing the parent. So, we can't
1773 * drop the lock until after we know which slot we're
1777 btrfs_unlock_up_safe(p, level + 1);
1779 ret = bin_search(b, key, level, &slot);
1783 if (ret && slot > 0) {
1787 p->slots[level] = slot;
1788 err = setup_nodes_for_search(trans, root, p, b, level,
1789 ins_len, &write_lock_level);
1796 b = p->nodes[level];
1797 slot = p->slots[level];
1800 * slot 0 is special, if we change the key
1801 * we have to update the parent pointer
1802 * which means we must have a write lock
1805 if (slot == 0 && cow &&
1806 write_lock_level < level + 1) {
1807 write_lock_level = level + 1;
1808 btrfs_release_path(p);
1812 unlock_up(p, level, lowest_unlock);
1814 if (level == lowest_level) {
1820 err = read_block_for_search(trans, root, p,
1821 &b, level, slot, key);
1829 if (!p->skip_locking) {
1830 level = btrfs_header_level(b);
1831 if (level <= write_lock_level) {
1832 err = btrfs_try_tree_write_lock(b);
1834 btrfs_set_path_blocking(p);
1836 btrfs_clear_path_blocking(p, b,
1839 p->locks[level] = BTRFS_WRITE_LOCK;
1841 err = btrfs_try_tree_read_lock(b);
1843 btrfs_set_path_blocking(p);
1844 btrfs_tree_read_lock(b);
1845 btrfs_clear_path_blocking(p, b,
1848 p->locks[level] = BTRFS_READ_LOCK;
1850 p->nodes[level] = b;
1853 p->slots[level] = slot;
1855 btrfs_leaf_free_space(root, b) < ins_len) {
1856 if (write_lock_level < 1) {
1857 write_lock_level = 1;
1858 btrfs_release_path(p);
1862 btrfs_set_path_blocking(p);
1863 err = split_leaf(trans, root, key,
1864 p, ins_len, ret == 0);
1865 btrfs_clear_path_blocking(p, NULL, 0);
1873 if (!p->search_for_split)
1874 unlock_up(p, level, lowest_unlock);
1881 * we don't really know what they plan on doing with the path
1882 * from here on, so for now just mark it as blocking
1884 if (!p->leave_spinning)
1885 btrfs_set_path_blocking(p);
1887 btrfs_release_path(p);
1892 * adjust the pointers going up the tree, starting at level
1893 * making sure the right key of each node is points to 'key'.
1894 * This is used after shifting pointers to the left, so it stops
1895 * fixing up pointers when a given leaf/node is not in slot 0 of the
1899 static void fixup_low_keys(struct btrfs_trans_handle *trans,
1900 struct btrfs_root *root, struct btrfs_path *path,
1901 struct btrfs_disk_key *key, int level)
1904 struct extent_buffer *t;
1906 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1907 int tslot = path->slots[i];
1908 if (!path->nodes[i])
1911 btrfs_set_node_key(t, key, tslot);
1912 btrfs_mark_buffer_dirty(path->nodes[i]);
1921 * This function isn't completely safe. It's the caller's responsibility
1922 * that the new key won't break the order
1924 void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1925 struct btrfs_root *root, struct btrfs_path *path,
1926 struct btrfs_key *new_key)
1928 struct btrfs_disk_key disk_key;
1929 struct extent_buffer *eb;
1932 eb = path->nodes[0];
1933 slot = path->slots[0];
1935 btrfs_item_key(eb, &disk_key, slot - 1);
1936 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
1938 if (slot < btrfs_header_nritems(eb) - 1) {
1939 btrfs_item_key(eb, &disk_key, slot + 1);
1940 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
1943 btrfs_cpu_key_to_disk(&disk_key, new_key);
1944 btrfs_set_item_key(eb, &disk_key, slot);
1945 btrfs_mark_buffer_dirty(eb);
1947 fixup_low_keys(trans, root, path, &disk_key, 1);
1951 * try to push data from one node into the next node left in the
1954 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1955 * error, and > 0 if there was no room in the left hand block.
1957 static int push_node_left(struct btrfs_trans_handle *trans,
1958 struct btrfs_root *root, struct extent_buffer *dst,
1959 struct extent_buffer *src, int empty)
1966 src_nritems = btrfs_header_nritems(src);
1967 dst_nritems = btrfs_header_nritems(dst);
1968 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1969 WARN_ON(btrfs_header_generation(src) != trans->transid);
1970 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1972 if (!empty && src_nritems <= 8)
1975 if (push_items <= 0)
1979 push_items = min(src_nritems, push_items);
1980 if (push_items < src_nritems) {
1981 /* leave at least 8 pointers in the node if
1982 * we aren't going to empty it
1984 if (src_nritems - push_items < 8) {
1985 if (push_items <= 8)
1991 push_items = min(src_nritems - 8, push_items);
1993 copy_extent_buffer(dst, src,
1994 btrfs_node_key_ptr_offset(dst_nritems),
1995 btrfs_node_key_ptr_offset(0),
1996 push_items * sizeof(struct btrfs_key_ptr));
1998 if (push_items < src_nritems) {
1999 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2000 btrfs_node_key_ptr_offset(push_items),
2001 (src_nritems - push_items) *
2002 sizeof(struct btrfs_key_ptr));
2004 btrfs_set_header_nritems(src, src_nritems - push_items);
2005 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2006 btrfs_mark_buffer_dirty(src);
2007 btrfs_mark_buffer_dirty(dst);
2013 * try to push data from one node into the next node right in the
2016 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2017 * error, and > 0 if there was no room in the right hand block.
2019 * this will only push up to 1/2 the contents of the left node over
2021 static int balance_node_right(struct btrfs_trans_handle *trans,
2022 struct btrfs_root *root,
2023 struct extent_buffer *dst,
2024 struct extent_buffer *src)
2032 WARN_ON(btrfs_header_generation(src) != trans->transid);
2033 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2035 src_nritems = btrfs_header_nritems(src);
2036 dst_nritems = btrfs_header_nritems(dst);
2037 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
2038 if (push_items <= 0)
2041 if (src_nritems < 4)
2044 max_push = src_nritems / 2 + 1;
2045 /* don't try to empty the node */
2046 if (max_push >= src_nritems)
2049 if (max_push < push_items)
2050 push_items = max_push;
2052 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2053 btrfs_node_key_ptr_offset(0),
2055 sizeof(struct btrfs_key_ptr));
2057 copy_extent_buffer(dst, src,
2058 btrfs_node_key_ptr_offset(0),
2059 btrfs_node_key_ptr_offset(src_nritems - push_items),
2060 push_items * sizeof(struct btrfs_key_ptr));
2062 btrfs_set_header_nritems(src, src_nritems - push_items);
2063 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2065 btrfs_mark_buffer_dirty(src);
2066 btrfs_mark_buffer_dirty(dst);
2072 * helper function to insert a new root level in the tree.
2073 * A new node is allocated, and a single item is inserted to
2074 * point to the existing root
2076 * returns zero on success or < 0 on failure.
2078 static noinline int insert_new_root(struct btrfs_trans_handle *trans,
2079 struct btrfs_root *root,
2080 struct btrfs_path *path, int level)
2083 struct extent_buffer *lower;
2084 struct extent_buffer *c;
2085 struct extent_buffer *old;
2086 struct btrfs_disk_key lower_key;
2088 BUG_ON(path->nodes[level]);
2089 BUG_ON(path->nodes[level-1] != root->node);
2091 lower = path->nodes[level-1];
2093 btrfs_item_key(lower, &lower_key, 0);
2095 btrfs_node_key(lower, &lower_key, 0);
2097 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
2098 root->root_key.objectid, &lower_key,
2099 level, root->node->start, 0, 0);
2103 root_add_used(root, root->nodesize);
2105 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
2106 btrfs_set_header_nritems(c, 1);
2107 btrfs_set_header_level(c, level);
2108 btrfs_set_header_bytenr(c, c->start);
2109 btrfs_set_header_generation(c, trans->transid);
2110 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
2111 btrfs_set_header_owner(c, root->root_key.objectid);
2113 write_extent_buffer(c, root->fs_info->fsid,
2114 (unsigned long)btrfs_header_fsid(c),
2117 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2118 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2121 btrfs_set_node_key(c, &lower_key, 0);
2122 btrfs_set_node_blockptr(c, 0, lower->start);
2123 lower_gen = btrfs_header_generation(lower);
2124 WARN_ON(lower_gen != trans->transid);
2126 btrfs_set_node_ptr_generation(c, 0, lower_gen);
2128 btrfs_mark_buffer_dirty(c);
2131 rcu_assign_pointer(root->node, c);
2133 /* the super has an extra ref to root->node */
2134 free_extent_buffer(old);
2136 add_root_to_dirty_list(root);
2137 extent_buffer_get(c);
2138 path->nodes[level] = c;
2139 path->locks[level] = BTRFS_WRITE_LOCK;
2140 path->slots[level] = 0;
2145 * worker function to insert a single pointer in a node.
2146 * the node should have enough room for the pointer already
2148 * slot and level indicate where you want the key to go, and
2149 * blocknr is the block the key points to.
2151 static void insert_ptr(struct btrfs_trans_handle *trans,
2152 struct btrfs_root *root, struct btrfs_path *path,
2153 struct btrfs_disk_key *key, u64 bytenr,
2154 int slot, int level)
2156 struct extent_buffer *lower;
2159 BUG_ON(!path->nodes[level]);
2160 btrfs_assert_tree_locked(path->nodes[level]);
2161 lower = path->nodes[level];
2162 nritems = btrfs_header_nritems(lower);
2163 BUG_ON(slot > nritems);
2164 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
2165 if (slot != nritems) {
2166 memmove_extent_buffer(lower,
2167 btrfs_node_key_ptr_offset(slot + 1),
2168 btrfs_node_key_ptr_offset(slot),
2169 (nritems - slot) * sizeof(struct btrfs_key_ptr));
2171 btrfs_set_node_key(lower, key, slot);
2172 btrfs_set_node_blockptr(lower, slot, bytenr);
2173 WARN_ON(trans->transid == 0);
2174 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
2175 btrfs_set_header_nritems(lower, nritems + 1);
2176 btrfs_mark_buffer_dirty(lower);
2180 * split the node at the specified level in path in two.
2181 * The path is corrected to point to the appropriate node after the split
2183 * Before splitting this tries to make some room in the node by pushing
2184 * left and right, if either one works, it returns right away.
2186 * returns 0 on success and < 0 on failure
2188 static noinline int split_node(struct btrfs_trans_handle *trans,
2189 struct btrfs_root *root,
2190 struct btrfs_path *path, int level)
2192 struct extent_buffer *c;
2193 struct extent_buffer *split;
2194 struct btrfs_disk_key disk_key;
2199 c = path->nodes[level];
2200 WARN_ON(btrfs_header_generation(c) != trans->transid);
2201 if (c == root->node) {
2202 /* trying to split the root, lets make a new one */
2203 ret = insert_new_root(trans, root, path, level + 1);
2207 ret = push_nodes_for_insert(trans, root, path, level);
2208 c = path->nodes[level];
2209 if (!ret && btrfs_header_nritems(c) <
2210 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
2216 c_nritems = btrfs_header_nritems(c);
2217 mid = (c_nritems + 1) / 2;
2218 btrfs_node_key(c, &disk_key, mid);
2220 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
2221 root->root_key.objectid,
2222 &disk_key, level, c->start, 0, 0);
2224 return PTR_ERR(split);
2226 root_add_used(root, root->nodesize);
2228 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
2229 btrfs_set_header_level(split, btrfs_header_level(c));
2230 btrfs_set_header_bytenr(split, split->start);
2231 btrfs_set_header_generation(split, trans->transid);
2232 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
2233 btrfs_set_header_owner(split, root->root_key.objectid);
2234 write_extent_buffer(split, root->fs_info->fsid,
2235 (unsigned long)btrfs_header_fsid(split),
2237 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2238 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2242 copy_extent_buffer(split, c,
2243 btrfs_node_key_ptr_offset(0),
2244 btrfs_node_key_ptr_offset(mid),
2245 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2246 btrfs_set_header_nritems(split, c_nritems - mid);
2247 btrfs_set_header_nritems(c, mid);
2250 btrfs_mark_buffer_dirty(c);
2251 btrfs_mark_buffer_dirty(split);
2253 insert_ptr(trans, root, path, &disk_key, split->start,
2254 path->slots[level + 1] + 1, level + 1);
2256 if (path->slots[level] >= mid) {
2257 path->slots[level] -= mid;
2258 btrfs_tree_unlock(c);
2259 free_extent_buffer(c);
2260 path->nodes[level] = split;
2261 path->slots[level + 1] += 1;
2263 btrfs_tree_unlock(split);
2264 free_extent_buffer(split);
2270 * how many bytes are required to store the items in a leaf. start
2271 * and nr indicate which items in the leaf to check. This totals up the
2272 * space used both by the item structs and the item data
2274 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
2277 int nritems = btrfs_header_nritems(l);
2278 int end = min(nritems, start + nr) - 1;
2282 data_len = btrfs_item_end_nr(l, start);
2283 data_len = data_len - btrfs_item_offset_nr(l, end);
2284 data_len += sizeof(struct btrfs_item) * nr;
2285 WARN_ON(data_len < 0);
2290 * The space between the end of the leaf items and
2291 * the start of the leaf data. IOW, how much room
2292 * the leaf has left for both items and data
2294 noinline int btrfs_leaf_free_space(struct btrfs_root *root,
2295 struct extent_buffer *leaf)
2297 int nritems = btrfs_header_nritems(leaf);
2299 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2301 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2302 "used %d nritems %d\n",
2303 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
2304 leaf_space_used(leaf, 0, nritems), nritems);
2310 * min slot controls the lowest index we're willing to push to the
2311 * right. We'll push up to and including min_slot, but no lower
2313 static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2314 struct btrfs_root *root,
2315 struct btrfs_path *path,
2316 int data_size, int empty,
2317 struct extent_buffer *right,
2318 int free_space, u32 left_nritems,
2321 struct extent_buffer *left = path->nodes[0];
2322 struct extent_buffer *upper = path->nodes[1];
2323 struct btrfs_disk_key disk_key;
2328 struct btrfs_item *item;
2337 nr = max_t(u32, 1, min_slot);
2339 if (path->slots[0] >= left_nritems)
2340 push_space += data_size;
2342 slot = path->slots[1];
2343 i = left_nritems - 1;
2345 item = btrfs_item_nr(left, i);
2347 if (!empty && push_items > 0) {
2348 if (path->slots[0] > i)
2350 if (path->slots[0] == i) {
2351 int space = btrfs_leaf_free_space(root, left);
2352 if (space + push_space * 2 > free_space)
2357 if (path->slots[0] == i)
2358 push_space += data_size;
2360 this_item_size = btrfs_item_size(left, item);
2361 if (this_item_size + sizeof(*item) + push_space > free_space)
2365 push_space += this_item_size + sizeof(*item);
2371 if (push_items == 0)
2374 if (!empty && push_items == left_nritems)
2377 /* push left to right */
2378 right_nritems = btrfs_header_nritems(right);
2380 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
2381 push_space -= leaf_data_end(root, left);
2383 /* make room in the right data area */
2384 data_end = leaf_data_end(root, right);
2385 memmove_extent_buffer(right,
2386 btrfs_leaf_data(right) + data_end - push_space,
2387 btrfs_leaf_data(right) + data_end,
2388 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2390 /* copy from the left data area */
2391 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
2392 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2393 btrfs_leaf_data(left) + leaf_data_end(root, left),
2396 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2397 btrfs_item_nr_offset(0),
2398 right_nritems * sizeof(struct btrfs_item));
2400 /* copy the items from left to right */
2401 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2402 btrfs_item_nr_offset(left_nritems - push_items),
2403 push_items * sizeof(struct btrfs_item));
2405 /* update the item pointers */
2406 right_nritems += push_items;
2407 btrfs_set_header_nritems(right, right_nritems);
2408 push_space = BTRFS_LEAF_DATA_SIZE(root);
2409 for (i = 0; i < right_nritems; i++) {
2410 item = btrfs_item_nr(right, i);
2411 push_space -= btrfs_item_size(right, item);
2412 btrfs_set_item_offset(right, item, push_space);
2415 left_nritems -= push_items;
2416 btrfs_set_header_nritems(left, left_nritems);
2419 btrfs_mark_buffer_dirty(left);
2421 clean_tree_block(trans, root, left);
2423 btrfs_mark_buffer_dirty(right);
2425 btrfs_item_key(right, &disk_key, 0);
2426 btrfs_set_node_key(upper, &disk_key, slot + 1);
2427 btrfs_mark_buffer_dirty(upper);
2429 /* then fixup the leaf pointer in the path */
2430 if (path->slots[0] >= left_nritems) {
2431 path->slots[0] -= left_nritems;
2432 if (btrfs_header_nritems(path->nodes[0]) == 0)
2433 clean_tree_block(trans, root, path->nodes[0]);
2434 btrfs_tree_unlock(path->nodes[0]);
2435 free_extent_buffer(path->nodes[0]);
2436 path->nodes[0] = right;
2437 path->slots[1] += 1;
2439 btrfs_tree_unlock(right);
2440 free_extent_buffer(right);
2445 btrfs_tree_unlock(right);
2446 free_extent_buffer(right);
2451 * push some data in the path leaf to the right, trying to free up at
2452 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2454 * returns 1 if the push failed because the other node didn't have enough
2455 * room, 0 if everything worked out and < 0 if there were major errors.
2457 * this will push starting from min_slot to the end of the leaf. It won't
2458 * push any slot lower than min_slot
2460 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
2461 *root, struct btrfs_path *path,
2462 int min_data_size, int data_size,
2463 int empty, u32 min_slot)
2465 struct extent_buffer *left = path->nodes[0];
2466 struct extent_buffer *right;
2467 struct extent_buffer *upper;
2473 if (!path->nodes[1])
2476 slot = path->slots[1];
2477 upper = path->nodes[1];
2478 if (slot >= btrfs_header_nritems(upper) - 1)
2481 btrfs_assert_tree_locked(path->nodes[1]);
2483 right = read_node_slot(root, upper, slot + 1);
2487 btrfs_tree_lock(right);
2488 btrfs_set_lock_blocking(right);
2490 free_space = btrfs_leaf_free_space(root, right);
2491 if (free_space < data_size)
2494 /* cow and double check */
2495 ret = btrfs_cow_block(trans, root, right, upper,
2500 free_space = btrfs_leaf_free_space(root, right);
2501 if (free_space < data_size)
2504 left_nritems = btrfs_header_nritems(left);
2505 if (left_nritems == 0)
2508 return __push_leaf_right(trans, root, path, min_data_size, empty,
2509 right, free_space, left_nritems, min_slot);
2511 btrfs_tree_unlock(right);
2512 free_extent_buffer(right);
2517 * push some data in the path leaf to the left, trying to free up at
2518 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2520 * max_slot can put a limit on how far into the leaf we'll push items. The
2521 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2524 static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2525 struct btrfs_root *root,
2526 struct btrfs_path *path, int data_size,
2527 int empty, struct extent_buffer *left,
2528 int free_space, u32 right_nritems,
2531 struct btrfs_disk_key disk_key;
2532 struct extent_buffer *right = path->nodes[0];
2536 struct btrfs_item *item;
2537 u32 old_left_nritems;
2541 u32 old_left_item_size;
2544 nr = min(right_nritems, max_slot);
2546 nr = min(right_nritems - 1, max_slot);
2548 for (i = 0; i < nr; i++) {
2549 item = btrfs_item_nr(right, i);
2551 if (!empty && push_items > 0) {
2552 if (path->slots[0] < i)
2554 if (path->slots[0] == i) {
2555 int space = btrfs_leaf_free_space(root, right);
2556 if (space + push_space * 2 > free_space)
2561 if (path->slots[0] == i)
2562 push_space += data_size;
2564 this_item_size = btrfs_item_size(right, item);
2565 if (this_item_size + sizeof(*item) + push_space > free_space)
2569 push_space += this_item_size + sizeof(*item);
2572 if (push_items == 0) {
2576 if (!empty && push_items == btrfs_header_nritems(right))
2579 /* push data from right to left */
2580 copy_extent_buffer(left, right,
2581 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2582 btrfs_item_nr_offset(0),
2583 push_items * sizeof(struct btrfs_item));
2585 push_space = BTRFS_LEAF_DATA_SIZE(root) -
2586 btrfs_item_offset_nr(right, push_items - 1);
2588 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
2589 leaf_data_end(root, left) - push_space,
2590 btrfs_leaf_data(right) +
2591 btrfs_item_offset_nr(right, push_items - 1),
2593 old_left_nritems = btrfs_header_nritems(left);
2594 BUG_ON(old_left_nritems <= 0);
2596 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
2597 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
2600 item = btrfs_item_nr(left, i);
2602 ioff = btrfs_item_offset(left, item);
2603 btrfs_set_item_offset(left, item,
2604 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
2606 btrfs_set_header_nritems(left, old_left_nritems + push_items);
2608 /* fixup right node */
2609 if (push_items > right_nritems) {
2610 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2615 if (push_items < right_nritems) {
2616 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2617 leaf_data_end(root, right);
2618 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2619 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2620 btrfs_leaf_data(right) +
2621 leaf_data_end(root, right), push_space);
2623 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
2624 btrfs_item_nr_offset(push_items),
2625 (btrfs_header_nritems(right) - push_items) *
2626 sizeof(struct btrfs_item));
2628 right_nritems -= push_items;
2629 btrfs_set_header_nritems(right, right_nritems);
2630 push_space = BTRFS_LEAF_DATA_SIZE(root);
2631 for (i = 0; i < right_nritems; i++) {
2632 item = btrfs_item_nr(right, i);
2634 push_space = push_space - btrfs_item_size(right, item);
2635 btrfs_set_item_offset(right, item, push_space);
2638 btrfs_mark_buffer_dirty(left);
2640 btrfs_mark_buffer_dirty(right);
2642 clean_tree_block(trans, root, right);
2644 btrfs_item_key(right, &disk_key, 0);
2645 fixup_low_keys(trans, root, path, &disk_key, 1);
2647 /* then fixup the leaf pointer in the path */
2648 if (path->slots[0] < push_items) {
2649 path->slots[0] += old_left_nritems;
2650 btrfs_tree_unlock(path->nodes[0]);
2651 free_extent_buffer(path->nodes[0]);
2652 path->nodes[0] = left;
2653 path->slots[1] -= 1;
2655 btrfs_tree_unlock(left);
2656 free_extent_buffer(left);
2657 path->slots[0] -= push_items;
2659 BUG_ON(path->slots[0] < 0);
2662 btrfs_tree_unlock(left);
2663 free_extent_buffer(left);
2668 * push some data in the path leaf to the left, trying to free up at
2669 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2671 * max_slot can put a limit on how far into the leaf we'll push items. The
2672 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2675 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
2676 *root, struct btrfs_path *path, int min_data_size,
2677 int data_size, int empty, u32 max_slot)
2679 struct extent_buffer *right = path->nodes[0];
2680 struct extent_buffer *left;
2686 slot = path->slots[1];
2689 if (!path->nodes[1])
2692 right_nritems = btrfs_header_nritems(right);
2693 if (right_nritems == 0)
2696 btrfs_assert_tree_locked(path->nodes[1]);
2698 left = read_node_slot(root, path->nodes[1], slot - 1);
2702 btrfs_tree_lock(left);
2703 btrfs_set_lock_blocking(left);
2705 free_space = btrfs_leaf_free_space(root, left);
2706 if (free_space < data_size) {
2711 /* cow and double check */
2712 ret = btrfs_cow_block(trans, root, left,
2713 path->nodes[1], slot - 1, &left);
2715 /* we hit -ENOSPC, but it isn't fatal here */
2721 free_space = btrfs_leaf_free_space(root, left);
2722 if (free_space < data_size) {
2727 return __push_leaf_left(trans, root, path, min_data_size,
2728 empty, left, free_space, right_nritems,
2731 btrfs_tree_unlock(left);
2732 free_extent_buffer(left);
2737 * split the path's leaf in two, making sure there is at least data_size
2738 * available for the resulting leaf level of the path.
2740 static noinline void copy_for_split(struct btrfs_trans_handle *trans,
2741 struct btrfs_root *root,
2742 struct btrfs_path *path,
2743 struct extent_buffer *l,
2744 struct extent_buffer *right,
2745 int slot, int mid, int nritems)
2750 struct btrfs_disk_key disk_key;
2752 nritems = nritems - mid;
2753 btrfs_set_header_nritems(right, nritems);
2754 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2756 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2757 btrfs_item_nr_offset(mid),
2758 nritems * sizeof(struct btrfs_item));
2760 copy_extent_buffer(right, l,
2761 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2762 data_copy_size, btrfs_leaf_data(l) +
2763 leaf_data_end(root, l), data_copy_size);
2765 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2766 btrfs_item_end_nr(l, mid);
2768 for (i = 0; i < nritems; i++) {
2769 struct btrfs_item *item = btrfs_item_nr(right, i);
2772 ioff = btrfs_item_offset(right, item);
2773 btrfs_set_item_offset(right, item, ioff + rt_data_off);
2776 btrfs_set_header_nritems(l, mid);
2777 btrfs_item_key(right, &disk_key, 0);
2778 insert_ptr(trans, root, path, &disk_key, right->start,
2779 path->slots[1] + 1, 1);
2781 btrfs_mark_buffer_dirty(right);
2782 btrfs_mark_buffer_dirty(l);
2783 BUG_ON(path->slots[0] != slot);
2786 btrfs_tree_unlock(path->nodes[0]);
2787 free_extent_buffer(path->nodes[0]);
2788 path->nodes[0] = right;
2789 path->slots[0] -= mid;
2790 path->slots[1] += 1;
2792 btrfs_tree_unlock(right);
2793 free_extent_buffer(right);
2796 BUG_ON(path->slots[0] < 0);
2800 * double splits happen when we need to insert a big item in the middle
2801 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2802 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2805 * We avoid this by trying to push the items on either side of our target
2806 * into the adjacent leaves. If all goes well we can avoid the double split
2809 static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
2810 struct btrfs_root *root,
2811 struct btrfs_path *path,
2819 slot = path->slots[0];
2822 * try to push all the items after our slot into the
2825 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
2832 nritems = btrfs_header_nritems(path->nodes[0]);
2834 * our goal is to get our slot at the start or end of a leaf. If
2835 * we've done so we're done
2837 if (path->slots[0] == 0 || path->slots[0] == nritems)
2840 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
2843 /* try to push all the items before our slot into the next leaf */
2844 slot = path->slots[0];
2845 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
2858 * split the path's leaf in two, making sure there is at least data_size
2859 * available for the resulting leaf level of the path.
2861 * returns 0 if all went well and < 0 on failure.
2863 static noinline int split_leaf(struct btrfs_trans_handle *trans,
2864 struct btrfs_root *root,
2865 struct btrfs_key *ins_key,
2866 struct btrfs_path *path, int data_size,
2869 struct btrfs_disk_key disk_key;
2870 struct extent_buffer *l;
2874 struct extent_buffer *right;
2878 int num_doubles = 0;
2879 int tried_avoid_double = 0;
2882 slot = path->slots[0];
2883 if (extend && data_size + btrfs_item_size_nr(l, slot) +
2884 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2887 /* first try to make some room by pushing left and right */
2889 wret = push_leaf_right(trans, root, path, data_size,
2894 wret = push_leaf_left(trans, root, path, data_size,
2895 data_size, 0, (u32)-1);
2901 /* did the pushes work? */
2902 if (btrfs_leaf_free_space(root, l) >= data_size)
2906 if (!path->nodes[1]) {
2907 ret = insert_new_root(trans, root, path, 1);
2914 slot = path->slots[0];
2915 nritems = btrfs_header_nritems(l);
2916 mid = (nritems + 1) / 2;
2920 leaf_space_used(l, mid, nritems - mid) + data_size >
2921 BTRFS_LEAF_DATA_SIZE(root)) {
2922 if (slot >= nritems) {
2926 if (mid != nritems &&
2927 leaf_space_used(l, mid, nritems - mid) +
2928 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2929 if (data_size && !tried_avoid_double)
2930 goto push_for_double;
2936 if (leaf_space_used(l, 0, mid) + data_size >
2937 BTRFS_LEAF_DATA_SIZE(root)) {
2938 if (!extend && data_size && slot == 0) {
2940 } else if ((extend || !data_size) && slot == 0) {
2944 if (mid != nritems &&
2945 leaf_space_used(l, mid, nritems - mid) +
2946 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2947 if (data_size && !tried_avoid_double)
2948 goto push_for_double;
2956 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2958 btrfs_item_key(l, &disk_key, mid);
2960 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
2961 root->root_key.objectid,
2962 &disk_key, 0, l->start, 0, 0);
2964 return PTR_ERR(right);
2966 root_add_used(root, root->leafsize);
2968 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2969 btrfs_set_header_bytenr(right, right->start);
2970 btrfs_set_header_generation(right, trans->transid);
2971 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
2972 btrfs_set_header_owner(right, root->root_key.objectid);
2973 btrfs_set_header_level(right, 0);
2974 write_extent_buffer(right, root->fs_info->fsid,
2975 (unsigned long)btrfs_header_fsid(right),
2978 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2979 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2984 btrfs_set_header_nritems(right, 0);
2985 insert_ptr(trans, root, path, &disk_key, right->start,
2986 path->slots[1] + 1, 1);
2987 btrfs_tree_unlock(path->nodes[0]);
2988 free_extent_buffer(path->nodes[0]);
2989 path->nodes[0] = right;
2991 path->slots[1] += 1;
2993 btrfs_set_header_nritems(right, 0);
2994 insert_ptr(trans, root, path, &disk_key, right->start,
2996 btrfs_tree_unlock(path->nodes[0]);
2997 free_extent_buffer(path->nodes[0]);
2998 path->nodes[0] = right;
3000 if (path->slots[1] == 0)
3001 fixup_low_keys(trans, root, path,
3004 btrfs_mark_buffer_dirty(right);
3008 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
3011 BUG_ON(num_doubles != 0);
3019 push_for_double_split(trans, root, path, data_size);
3020 tried_avoid_double = 1;
3021 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3026 static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3027 struct btrfs_root *root,
3028 struct btrfs_path *path, int ins_len)
3030 struct btrfs_key key;
3031 struct extent_buffer *leaf;
3032 struct btrfs_file_extent_item *fi;
3037 leaf = path->nodes[0];
3038 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3040 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3041 key.type != BTRFS_EXTENT_CSUM_KEY);
3043 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3046 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3047 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3048 fi = btrfs_item_ptr(leaf, path->slots[0],
3049 struct btrfs_file_extent_item);
3050 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3052 btrfs_release_path(path);
3054 path->keep_locks = 1;
3055 path->search_for_split = 1;
3056 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3057 path->search_for_split = 0;
3062 leaf = path->nodes[0];
3063 /* if our item isn't there or got smaller, return now */
3064 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3067 /* the leaf has changed, it now has room. return now */
3068 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3071 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3072 fi = btrfs_item_ptr(leaf, path->slots[0],
3073 struct btrfs_file_extent_item);
3074 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3078 btrfs_set_path_blocking(path);
3079 ret = split_leaf(trans, root, &key, path, ins_len, 1);
3083 path->keep_locks = 0;
3084 btrfs_unlock_up_safe(path, 1);
3087 path->keep_locks = 0;
3091 static noinline int split_item(struct btrfs_trans_handle *trans,
3092 struct btrfs_root *root,
3093 struct btrfs_path *path,
3094 struct btrfs_key *new_key,
3095 unsigned long split_offset)
3097 struct extent_buffer *leaf;
3098 struct btrfs_item *item;
3099 struct btrfs_item *new_item;
3105 struct btrfs_disk_key disk_key;
3107 leaf = path->nodes[0];
3108 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3110 btrfs_set_path_blocking(path);
3112 item = btrfs_item_nr(leaf, path->slots[0]);
3113 orig_offset = btrfs_item_offset(leaf, item);
3114 item_size = btrfs_item_size(leaf, item);
3116 buf = kmalloc(item_size, GFP_NOFS);
3120 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3121 path->slots[0]), item_size);
3123 slot = path->slots[0] + 1;
3124 nritems = btrfs_header_nritems(leaf);
3125 if (slot != nritems) {
3126 /* shift the items */
3127 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3128 btrfs_item_nr_offset(slot),
3129 (nritems - slot) * sizeof(struct btrfs_item));
3132 btrfs_cpu_key_to_disk(&disk_key, new_key);
3133 btrfs_set_item_key(leaf, &disk_key, slot);
3135 new_item = btrfs_item_nr(leaf, slot);
3137 btrfs_set_item_offset(leaf, new_item, orig_offset);
3138 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3140 btrfs_set_item_offset(leaf, item,
3141 orig_offset + item_size - split_offset);
3142 btrfs_set_item_size(leaf, item, split_offset);
3144 btrfs_set_header_nritems(leaf, nritems + 1);
3146 /* write the data for the start of the original item */
3147 write_extent_buffer(leaf, buf,
3148 btrfs_item_ptr_offset(leaf, path->slots[0]),
3151 /* write the data for the new item */
3152 write_extent_buffer(leaf, buf + split_offset,
3153 btrfs_item_ptr_offset(leaf, slot),
3154 item_size - split_offset);
3155 btrfs_mark_buffer_dirty(leaf);
3157 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
3163 * This function splits a single item into two items,
3164 * giving 'new_key' to the new item and splitting the
3165 * old one at split_offset (from the start of the item).
3167 * The path may be released by this operation. After
3168 * the split, the path is pointing to the old item. The
3169 * new item is going to be in the same node as the old one.
3171 * Note, the item being split must be smaller enough to live alone on
3172 * a tree block with room for one extra struct btrfs_item
3174 * This allows us to split the item in place, keeping a lock on the
3175 * leaf the entire time.
3177 int btrfs_split_item(struct btrfs_trans_handle *trans,
3178 struct btrfs_root *root,
3179 struct btrfs_path *path,
3180 struct btrfs_key *new_key,
3181 unsigned long split_offset)
3184 ret = setup_leaf_for_split(trans, root, path,
3185 sizeof(struct btrfs_item));
3189 ret = split_item(trans, root, path, new_key, split_offset);
3194 * This function duplicate a item, giving 'new_key' to the new item.
3195 * It guarantees both items live in the same tree leaf and the new item
3196 * is contiguous with the original item.
3198 * This allows us to split file extent in place, keeping a lock on the
3199 * leaf the entire time.
3201 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3202 struct btrfs_root *root,
3203 struct btrfs_path *path,
3204 struct btrfs_key *new_key)
3206 struct extent_buffer *leaf;
3210 leaf = path->nodes[0];
3211 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3212 ret = setup_leaf_for_split(trans, root, path,
3213 item_size + sizeof(struct btrfs_item));
3218 setup_items_for_insert(trans, root, path, new_key, &item_size,
3219 item_size, item_size +
3220 sizeof(struct btrfs_item), 1);
3221 leaf = path->nodes[0];
3222 memcpy_extent_buffer(leaf,
3223 btrfs_item_ptr_offset(leaf, path->slots[0]),
3224 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3230 * make the item pointed to by the path smaller. new_size indicates
3231 * how small to make it, and from_end tells us if we just chop bytes
3232 * off the end of the item or if we shift the item to chop bytes off
3235 void btrfs_truncate_item(struct btrfs_trans_handle *trans,
3236 struct btrfs_root *root,
3237 struct btrfs_path *path,
3238 u32 new_size, int from_end)
3241 struct extent_buffer *leaf;
3242 struct btrfs_item *item;
3244 unsigned int data_end;
3245 unsigned int old_data_start;
3246 unsigned int old_size;
3247 unsigned int size_diff;
3250 leaf = path->nodes[0];
3251 slot = path->slots[0];
3253 old_size = btrfs_item_size_nr(leaf, slot);
3254 if (old_size == new_size)
3257 nritems = btrfs_header_nritems(leaf);
3258 data_end = leaf_data_end(root, leaf);
3260 old_data_start = btrfs_item_offset_nr(leaf, slot);
3262 size_diff = old_size - new_size;
3265 BUG_ON(slot >= nritems);
3268 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3270 /* first correct the data pointers */
3271 for (i = slot; i < nritems; i++) {
3273 item = btrfs_item_nr(leaf, i);
3275 ioff = btrfs_item_offset(leaf, item);
3276 btrfs_set_item_offset(leaf, item, ioff + size_diff);
3279 /* shift the data */
3281 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3282 data_end + size_diff, btrfs_leaf_data(leaf) +
3283 data_end, old_data_start + new_size - data_end);
3285 struct btrfs_disk_key disk_key;
3288 btrfs_item_key(leaf, &disk_key, slot);
3290 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3292 struct btrfs_file_extent_item *fi;
3294 fi = btrfs_item_ptr(leaf, slot,
3295 struct btrfs_file_extent_item);
3296 fi = (struct btrfs_file_extent_item *)(
3297 (unsigned long)fi - size_diff);
3299 if (btrfs_file_extent_type(leaf, fi) ==
3300 BTRFS_FILE_EXTENT_INLINE) {
3301 ptr = btrfs_item_ptr_offset(leaf, slot);
3302 memmove_extent_buffer(leaf, ptr,
3304 offsetof(struct btrfs_file_extent_item,
3309 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3310 data_end + size_diff, btrfs_leaf_data(leaf) +
3311 data_end, old_data_start - data_end);
3313 offset = btrfs_disk_key_offset(&disk_key);
3314 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3315 btrfs_set_item_key(leaf, &disk_key, slot);
3317 fixup_low_keys(trans, root, path, &disk_key, 1);
3320 item = btrfs_item_nr(leaf, slot);
3321 btrfs_set_item_size(leaf, item, new_size);
3322 btrfs_mark_buffer_dirty(leaf);
3324 if (btrfs_leaf_free_space(root, leaf) < 0) {
3325 btrfs_print_leaf(root, leaf);
3331 * make the item pointed to by the path bigger, data_size is the new size.
3333 void btrfs_extend_item(struct btrfs_trans_handle *trans,
3334 struct btrfs_root *root, struct btrfs_path *path,
3338 struct extent_buffer *leaf;
3339 struct btrfs_item *item;
3341 unsigned int data_end;
3342 unsigned int old_data;
3343 unsigned int old_size;
3346 leaf = path->nodes[0];
3348 nritems = btrfs_header_nritems(leaf);
3349 data_end = leaf_data_end(root, leaf);
3351 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3352 btrfs_print_leaf(root, leaf);
3355 slot = path->slots[0];
3356 old_data = btrfs_item_end_nr(leaf, slot);
3359 if (slot >= nritems) {
3360 btrfs_print_leaf(root, leaf);
3361 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3367 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3369 /* first correct the data pointers */
3370 for (i = slot; i < nritems; i++) {
3372 item = btrfs_item_nr(leaf, i);
3374 ioff = btrfs_item_offset(leaf, item);
3375 btrfs_set_item_offset(leaf, item, ioff - data_size);
3378 /* shift the data */
3379 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3380 data_end - data_size, btrfs_leaf_data(leaf) +
3381 data_end, old_data - data_end);
3383 data_end = old_data;
3384 old_size = btrfs_item_size_nr(leaf, slot);
3385 item = btrfs_item_nr(leaf, slot);
3386 btrfs_set_item_size(leaf, item, old_size + data_size);
3387 btrfs_mark_buffer_dirty(leaf);
3389 if (btrfs_leaf_free_space(root, leaf) < 0) {
3390 btrfs_print_leaf(root, leaf);
3396 * Given a key and some data, insert items into the tree.
3397 * This does all the path init required, making room in the tree if needed.
3398 * Returns the number of keys that were inserted.
3400 int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3401 struct btrfs_root *root,
3402 struct btrfs_path *path,
3403 struct btrfs_key *cpu_key, u32 *data_size,
3406 struct extent_buffer *leaf;
3407 struct btrfs_item *item;
3414 unsigned int data_end;
3415 struct btrfs_disk_key disk_key;
3416 struct btrfs_key found_key;
3418 for (i = 0; i < nr; i++) {
3419 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3420 BTRFS_LEAF_DATA_SIZE(root)) {
3424 total_data += data_size[i];
3425 total_size += data_size[i] + sizeof(struct btrfs_item);
3429 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3435 leaf = path->nodes[0];
3437 nritems = btrfs_header_nritems(leaf);
3438 data_end = leaf_data_end(root, leaf);
3440 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3441 for (i = nr; i >= 0; i--) {
3442 total_data -= data_size[i];
3443 total_size -= data_size[i] + sizeof(struct btrfs_item);
3444 if (total_size < btrfs_leaf_free_space(root, leaf))
3450 slot = path->slots[0];
3453 if (slot != nritems) {
3454 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3456 item = btrfs_item_nr(leaf, slot);
3457 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3459 /* figure out how many keys we can insert in here */
3460 total_data = data_size[0];
3461 for (i = 1; i < nr; i++) {
3462 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3464 total_data += data_size[i];
3468 if (old_data < data_end) {
3469 btrfs_print_leaf(root, leaf);
3470 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3471 slot, old_data, data_end);
3475 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3477 /* first correct the data pointers */
3478 for (i = slot; i < nritems; i++) {
3481 item = btrfs_item_nr(leaf, i);
3482 ioff = btrfs_item_offset(leaf, item);
3483 btrfs_set_item_offset(leaf, item, ioff - total_data);
3485 /* shift the items */
3486 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3487 btrfs_item_nr_offset(slot),
3488 (nritems - slot) * sizeof(struct btrfs_item));
3490 /* shift the data */
3491 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3492 data_end - total_data, btrfs_leaf_data(leaf) +
3493 data_end, old_data - data_end);
3494 data_end = old_data;
3497 * this sucks but it has to be done, if we are inserting at
3498 * the end of the leaf only insert 1 of the items, since we
3499 * have no way of knowing whats on the next leaf and we'd have
3500 * to drop our current locks to figure it out
3505 /* setup the item for the new data */
3506 for (i = 0; i < nr; i++) {
3507 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3508 btrfs_set_item_key(leaf, &disk_key, slot + i);
3509 item = btrfs_item_nr(leaf, slot + i);
3510 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3511 data_end -= data_size[i];
3512 btrfs_set_item_size(leaf, item, data_size[i]);
3514 btrfs_set_header_nritems(leaf, nritems + nr);
3515 btrfs_mark_buffer_dirty(leaf);
3519 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3520 fixup_low_keys(trans, root, path, &disk_key, 1);
3523 if (btrfs_leaf_free_space(root, leaf) < 0) {
3524 btrfs_print_leaf(root, leaf);
3534 * this is a helper for btrfs_insert_empty_items, the main goal here is
3535 * to save stack depth by doing the bulk of the work in a function
3536 * that doesn't call btrfs_search_slot
3538 void setup_items_for_insert(struct btrfs_trans_handle *trans,
3539 struct btrfs_root *root, struct btrfs_path *path,
3540 struct btrfs_key *cpu_key, u32 *data_size,
3541 u32 total_data, u32 total_size, int nr)
3543 struct btrfs_item *item;
3546 unsigned int data_end;
3547 struct btrfs_disk_key disk_key;
3548 struct extent_buffer *leaf;
3551 leaf = path->nodes[0];
3552 slot = path->slots[0];
3554 nritems = btrfs_header_nritems(leaf);
3555 data_end = leaf_data_end(root, leaf);
3557 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3558 btrfs_print_leaf(root, leaf);
3559 printk(KERN_CRIT "not enough freespace need %u have %d\n",
3560 total_size, btrfs_leaf_free_space(root, leaf));
3564 if (slot != nritems) {
3565 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3567 if (old_data < data_end) {
3568 btrfs_print_leaf(root, leaf);
3569 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3570 slot, old_data, data_end);
3574 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3576 /* first correct the data pointers */
3577 for (i = slot; i < nritems; i++) {
3580 item = btrfs_item_nr(leaf, i);
3581 ioff = btrfs_item_offset(leaf, item);
3582 btrfs_set_item_offset(leaf, item, ioff - total_data);
3584 /* shift the items */
3585 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3586 btrfs_item_nr_offset(slot),
3587 (nritems - slot) * sizeof(struct btrfs_item));
3589 /* shift the data */
3590 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3591 data_end - total_data, btrfs_leaf_data(leaf) +
3592 data_end, old_data - data_end);
3593 data_end = old_data;
3596 /* setup the item for the new data */
3597 for (i = 0; i < nr; i++) {
3598 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3599 btrfs_set_item_key(leaf, &disk_key, slot + i);
3600 item = btrfs_item_nr(leaf, slot + i);
3601 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3602 data_end -= data_size[i];
3603 btrfs_set_item_size(leaf, item, data_size[i]);
3606 btrfs_set_header_nritems(leaf, nritems + nr);
3609 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3610 fixup_low_keys(trans, root, path, &disk_key, 1);
3612 btrfs_unlock_up_safe(path, 1);
3613 btrfs_mark_buffer_dirty(leaf);
3615 if (btrfs_leaf_free_space(root, leaf) < 0) {
3616 btrfs_print_leaf(root, leaf);
3622 * Given a key and some data, insert items into the tree.
3623 * This does all the path init required, making room in the tree if needed.
3625 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3626 struct btrfs_root *root,
3627 struct btrfs_path *path,
3628 struct btrfs_key *cpu_key, u32 *data_size,
3637 for (i = 0; i < nr; i++)
3638 total_data += data_size[i];
3640 total_size = total_data + (nr * sizeof(struct btrfs_item));
3641 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3647 slot = path->slots[0];
3650 setup_items_for_insert(trans, root, path, cpu_key, data_size,
3651 total_data, total_size, nr);
3656 * Given a key and some data, insert an item into the tree.
3657 * This does all the path init required, making room in the tree if needed.
3659 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3660 *root, struct btrfs_key *cpu_key, void *data, u32
3664 struct btrfs_path *path;
3665 struct extent_buffer *leaf;
3668 path = btrfs_alloc_path();
3671 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
3673 leaf = path->nodes[0];
3674 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3675 write_extent_buffer(leaf, data, ptr, data_size);
3676 btrfs_mark_buffer_dirty(leaf);
3678 btrfs_free_path(path);
3683 * delete the pointer from a given node.
3685 * the tree should have been previously balanced so the deletion does not
3688 static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3689 struct btrfs_path *path, int level, int slot)
3691 struct extent_buffer *parent = path->nodes[level];
3694 nritems = btrfs_header_nritems(parent);
3695 if (slot != nritems - 1) {
3696 memmove_extent_buffer(parent,
3697 btrfs_node_key_ptr_offset(slot),
3698 btrfs_node_key_ptr_offset(slot + 1),
3699 sizeof(struct btrfs_key_ptr) *
3700 (nritems - slot - 1));
3703 btrfs_set_header_nritems(parent, nritems);
3704 if (nritems == 0 && parent == root->node) {
3705 BUG_ON(btrfs_header_level(root->node) != 1);
3706 /* just turn the root into a leaf and break */
3707 btrfs_set_header_level(root->node, 0);
3708 } else if (slot == 0) {
3709 struct btrfs_disk_key disk_key;
3711 btrfs_node_key(parent, &disk_key, 0);
3712 fixup_low_keys(trans, root, path, &disk_key, level + 1);
3714 btrfs_mark_buffer_dirty(parent);
3718 * a helper function to delete the leaf pointed to by path->slots[1] and
3721 * This deletes the pointer in path->nodes[1] and frees the leaf
3722 * block extent. zero is returned if it all worked out, < 0 otherwise.
3724 * The path must have already been setup for deleting the leaf, including
3725 * all the proper balancing. path->nodes[1] must be locked.
3727 static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
3728 struct btrfs_root *root,
3729 struct btrfs_path *path,
3730 struct extent_buffer *leaf)
3732 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
3733 del_ptr(trans, root, path, 1, path->slots[1]);
3736 * btrfs_free_extent is expensive, we want to make sure we
3737 * aren't holding any locks when we call it
3739 btrfs_unlock_up_safe(path, 0);
3741 root_sub_used(root, leaf->len);
3743 btrfs_free_tree_block(trans, root, leaf, 0, 1, 0);
3746 * delete the item at the leaf level in path. If that empties
3747 * the leaf, remove it from the tree
3749 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3750 struct btrfs_path *path, int slot, int nr)
3752 struct extent_buffer *leaf;
3753 struct btrfs_item *item;
3761 leaf = path->nodes[0];
3762 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3764 for (i = 0; i < nr; i++)
3765 dsize += btrfs_item_size_nr(leaf, slot + i);
3767 nritems = btrfs_header_nritems(leaf);
3769 if (slot + nr != nritems) {
3770 int data_end = leaf_data_end(root, leaf);
3772 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3774 btrfs_leaf_data(leaf) + data_end,
3775 last_off - data_end);
3777 for (i = slot + nr; i < nritems; i++) {
3780 item = btrfs_item_nr(leaf, i);
3781 ioff = btrfs_item_offset(leaf, item);
3782 btrfs_set_item_offset(leaf, item, ioff + dsize);
3785 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
3786 btrfs_item_nr_offset(slot + nr),
3787 sizeof(struct btrfs_item) *
3788 (nritems - slot - nr));
3790 btrfs_set_header_nritems(leaf, nritems - nr);
3793 /* delete the leaf if we've emptied it */
3795 if (leaf == root->node) {
3796 btrfs_set_header_level(leaf, 0);
3798 btrfs_set_path_blocking(path);
3799 clean_tree_block(trans, root, leaf);
3800 btrfs_del_leaf(trans, root, path, leaf);
3803 int used = leaf_space_used(leaf, 0, nritems);
3805 struct btrfs_disk_key disk_key;
3807 btrfs_item_key(leaf, &disk_key, 0);
3808 fixup_low_keys(trans, root, path, &disk_key, 1);
3811 /* delete the leaf if it is mostly empty */
3812 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
3813 /* push_leaf_left fixes the path.
3814 * make sure the path still points to our leaf
3815 * for possible call to del_ptr below
3817 slot = path->slots[1];
3818 extent_buffer_get(leaf);
3820 btrfs_set_path_blocking(path);
3821 wret = push_leaf_left(trans, root, path, 1, 1,
3823 if (wret < 0 && wret != -ENOSPC)
3826 if (path->nodes[0] == leaf &&
3827 btrfs_header_nritems(leaf)) {
3828 wret = push_leaf_right(trans, root, path, 1,
3830 if (wret < 0 && wret != -ENOSPC)
3834 if (btrfs_header_nritems(leaf) == 0) {
3835 path->slots[1] = slot;
3836 btrfs_del_leaf(trans, root, path, leaf);
3837 free_extent_buffer(leaf);
3840 /* if we're still in the path, make sure
3841 * we're dirty. Otherwise, one of the
3842 * push_leaf functions must have already
3843 * dirtied this buffer
3845 if (path->nodes[0] == leaf)
3846 btrfs_mark_buffer_dirty(leaf);
3847 free_extent_buffer(leaf);
3850 btrfs_mark_buffer_dirty(leaf);
3857 * search the tree again to find a leaf with lesser keys
3858 * returns 0 if it found something or 1 if there are no lesser leaves.
3859 * returns < 0 on io errors.
3861 * This may release the path, and so you may lose any locks held at the
3864 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3866 struct btrfs_key key;
3867 struct btrfs_disk_key found_key;
3870 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
3874 else if (key.type > 0)
3876 else if (key.objectid > 0)
3881 btrfs_release_path(path);
3882 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3885 btrfs_item_key(path->nodes[0], &found_key, 0);
3886 ret = comp_keys(&found_key, &key);
3893 * A helper function to walk down the tree starting at min_key, and looking
3894 * for nodes or leaves that are either in cache or have a minimum
3895 * transaction id. This is used by the btree defrag code, and tree logging
3897 * This does not cow, but it does stuff the starting key it finds back
3898 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3899 * key and get a writable path.
3901 * This does lock as it descends, and path->keep_locks should be set
3902 * to 1 by the caller.
3904 * This honors path->lowest_level to prevent descent past a given level
3907 * min_trans indicates the oldest transaction that you are interested
3908 * in walking through. Any nodes or leaves older than min_trans are
3909 * skipped over (without reading them).
3911 * returns zero if something useful was found, < 0 on error and 1 if there
3912 * was nothing in the tree that matched the search criteria.
3914 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
3915 struct btrfs_key *max_key,
3916 struct btrfs_path *path, int cache_only,
3919 struct extent_buffer *cur;
3920 struct btrfs_key found_key;
3927 WARN_ON(!path->keep_locks);
3929 cur = btrfs_read_lock_root_node(root);
3930 level = btrfs_header_level(cur);
3931 WARN_ON(path->nodes[level]);
3932 path->nodes[level] = cur;
3933 path->locks[level] = BTRFS_READ_LOCK;
3935 if (btrfs_header_generation(cur) < min_trans) {
3940 nritems = btrfs_header_nritems(cur);
3941 level = btrfs_header_level(cur);
3942 sret = bin_search(cur, min_key, level, &slot);
3944 /* at the lowest level, we're done, setup the path and exit */
3945 if (level == path->lowest_level) {
3946 if (slot >= nritems)
3949 path->slots[level] = slot;
3950 btrfs_item_key_to_cpu(cur, &found_key, slot);
3953 if (sret && slot > 0)
3956 * check this node pointer against the cache_only and
3957 * min_trans parameters. If it isn't in cache or is too
3958 * old, skip to the next one.
3960 while (slot < nritems) {
3963 struct extent_buffer *tmp;
3964 struct btrfs_disk_key disk_key;
3966 blockptr = btrfs_node_blockptr(cur, slot);
3967 gen = btrfs_node_ptr_generation(cur, slot);
3968 if (gen < min_trans) {
3976 btrfs_node_key(cur, &disk_key, slot);
3977 if (comp_keys(&disk_key, max_key) >= 0) {
3983 tmp = btrfs_find_tree_block(root, blockptr,
3984 btrfs_level_size(root, level - 1));
3986 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3987 free_extent_buffer(tmp);
3991 free_extent_buffer(tmp);
3996 * we didn't find a candidate key in this node, walk forward
3997 * and find another one
3999 if (slot >= nritems) {
4000 path->slots[level] = slot;
4001 btrfs_set_path_blocking(path);
4002 sret = btrfs_find_next_key(root, path, min_key, level,
4003 cache_only, min_trans);
4005 btrfs_release_path(path);
4011 /* save our key for returning back */
4012 btrfs_node_key_to_cpu(cur, &found_key, slot);
4013 path->slots[level] = slot;
4014 if (level == path->lowest_level) {
4016 unlock_up(path, level, 1);
4019 btrfs_set_path_blocking(path);
4020 cur = read_node_slot(root, cur, slot);
4021 BUG_ON(!cur); /* -ENOMEM */
4023 btrfs_tree_read_lock(cur);
4025 path->locks[level - 1] = BTRFS_READ_LOCK;
4026 path->nodes[level - 1] = cur;
4027 unlock_up(path, level, 1);
4028 btrfs_clear_path_blocking(path, NULL, 0);
4032 memcpy(min_key, &found_key, sizeof(found_key));
4033 btrfs_set_path_blocking(path);
4038 * this is similar to btrfs_next_leaf, but does not try to preserve
4039 * and fixup the path. It looks for and returns the next key in the
4040 * tree based on the current path and the cache_only and min_trans
4043 * 0 is returned if another key is found, < 0 if there are any errors
4044 * and 1 is returned if there are no higher keys in the tree
4046 * path->keep_locks should be set to 1 on the search made before
4047 * calling this function.
4049 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
4050 struct btrfs_key *key, int level,
4051 int cache_only, u64 min_trans)
4054 struct extent_buffer *c;
4056 WARN_ON(!path->keep_locks);
4057 while (level < BTRFS_MAX_LEVEL) {
4058 if (!path->nodes[level])
4061 slot = path->slots[level] + 1;
4062 c = path->nodes[level];
4064 if (slot >= btrfs_header_nritems(c)) {
4067 struct btrfs_key cur_key;
4068 if (level + 1 >= BTRFS_MAX_LEVEL ||
4069 !path->nodes[level + 1])
4072 if (path->locks[level + 1]) {
4077 slot = btrfs_header_nritems(c) - 1;
4079 btrfs_item_key_to_cpu(c, &cur_key, slot);
4081 btrfs_node_key_to_cpu(c, &cur_key, slot);
4083 orig_lowest = path->lowest_level;
4084 btrfs_release_path(path);
4085 path->lowest_level = level;
4086 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4088 path->lowest_level = orig_lowest;
4092 c = path->nodes[level];
4093 slot = path->slots[level];
4100 btrfs_item_key_to_cpu(c, key, slot);
4102 u64 blockptr = btrfs_node_blockptr(c, slot);
4103 u64 gen = btrfs_node_ptr_generation(c, slot);
4106 struct extent_buffer *cur;
4107 cur = btrfs_find_tree_block(root, blockptr,
4108 btrfs_level_size(root, level - 1));
4109 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4112 free_extent_buffer(cur);
4115 free_extent_buffer(cur);
4117 if (gen < min_trans) {
4121 btrfs_node_key_to_cpu(c, key, slot);
4129 * search the tree again to find a leaf with greater keys
4130 * returns 0 if it found something or 1 if there are no greater leaves.
4131 * returns < 0 on io errors.
4133 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
4137 struct extent_buffer *c;
4138 struct extent_buffer *next;
4139 struct btrfs_key key;
4142 int old_spinning = path->leave_spinning;
4143 int next_rw_lock = 0;
4145 nritems = btrfs_header_nritems(path->nodes[0]);
4149 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4154 btrfs_release_path(path);
4156 path->keep_locks = 1;
4157 path->leave_spinning = 1;
4159 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4160 path->keep_locks = 0;
4165 nritems = btrfs_header_nritems(path->nodes[0]);
4167 * by releasing the path above we dropped all our locks. A balance
4168 * could have added more items next to the key that used to be
4169 * at the very end of the block. So, check again here and
4170 * advance the path if there are now more items available.
4172 if (nritems > 0 && path->slots[0] < nritems - 1) {
4179 while (level < BTRFS_MAX_LEVEL) {
4180 if (!path->nodes[level]) {
4185 slot = path->slots[level] + 1;
4186 c = path->nodes[level];
4187 if (slot >= btrfs_header_nritems(c)) {
4189 if (level == BTRFS_MAX_LEVEL) {
4197 btrfs_tree_unlock_rw(next, next_rw_lock);
4198 free_extent_buffer(next);
4202 next_rw_lock = path->locks[level];
4203 ret = read_block_for_search(NULL, root, path, &next, level,
4209 btrfs_release_path(path);
4213 if (!path->skip_locking) {
4214 ret = btrfs_try_tree_read_lock(next);
4216 btrfs_set_path_blocking(path);
4217 btrfs_tree_read_lock(next);
4218 btrfs_clear_path_blocking(path, next,
4221 next_rw_lock = BTRFS_READ_LOCK;
4225 path->slots[level] = slot;
4228 c = path->nodes[level];
4229 if (path->locks[level])
4230 btrfs_tree_unlock_rw(c, path->locks[level]);
4232 free_extent_buffer(c);
4233 path->nodes[level] = next;
4234 path->slots[level] = 0;
4235 if (!path->skip_locking)
4236 path->locks[level] = next_rw_lock;
4240 ret = read_block_for_search(NULL, root, path, &next, level,
4246 btrfs_release_path(path);
4250 if (!path->skip_locking) {
4251 ret = btrfs_try_tree_read_lock(next);
4253 btrfs_set_path_blocking(path);
4254 btrfs_tree_read_lock(next);
4255 btrfs_clear_path_blocking(path, next,
4258 next_rw_lock = BTRFS_READ_LOCK;
4263 unlock_up(path, 0, 1);
4264 path->leave_spinning = old_spinning;
4266 btrfs_set_path_blocking(path);
4272 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4273 * searching until it gets past min_objectid or finds an item of 'type'
4275 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4277 int btrfs_previous_item(struct btrfs_root *root,
4278 struct btrfs_path *path, u64 min_objectid,
4281 struct btrfs_key found_key;
4282 struct extent_buffer *leaf;
4287 if (path->slots[0] == 0) {
4288 btrfs_set_path_blocking(path);
4289 ret = btrfs_prev_leaf(root, path);
4295 leaf = path->nodes[0];
4296 nritems = btrfs_header_nritems(leaf);
4299 if (path->slots[0] == nritems)
4302 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4303 if (found_key.objectid < min_objectid)
4305 if (found_key.type == type)
4307 if (found_key.objectid == min_objectid &&
4308 found_key.type < type)