2 * Copyright (C) 2007,2008 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
22 #include "transaction.h"
23 #include "print-tree.h"
26 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
27 *root, struct btrfs_path *path, int level);
28 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
29 *root, struct btrfs_key *ins_key,
30 struct btrfs_path *path, int data_size, int extend);
31 static int push_node_left(struct btrfs_trans_handle *trans,
32 struct btrfs_root *root, struct extent_buffer *dst,
33 struct extent_buffer *src, int empty);
34 static int balance_node_right(struct btrfs_trans_handle *trans,
35 struct btrfs_root *root,
36 struct extent_buffer *dst_buf,
37 struct extent_buffer *src_buf);
38 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
39 struct btrfs_path *path, int level, int slot);
41 inline void btrfs_init_path(struct btrfs_path *p)
43 memset(p, 0, sizeof(*p));
46 struct btrfs_path *btrfs_alloc_path(void)
48 struct btrfs_path *path;
49 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
51 btrfs_init_path(path);
58 * set all locked nodes in the path to blocking locks. This should
59 * be done before scheduling
61 noinline void btrfs_set_path_blocking(struct btrfs_path *p)
64 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
65 if (p->nodes[i] && p->locks[i])
66 btrfs_set_lock_blocking(p->nodes[i]);
71 * reset all the locked nodes in the patch to spinning locks.
73 noinline void btrfs_clear_path_blocking(struct btrfs_path *p)
76 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
77 if (p->nodes[i] && p->locks[i])
78 btrfs_clear_lock_blocking(p->nodes[i]);
82 /* this also releases the path */
83 void btrfs_free_path(struct btrfs_path *p)
85 btrfs_release_path(NULL, p);
86 kmem_cache_free(btrfs_path_cachep, p);
90 * path release drops references on the extent buffers in the path
91 * and it drops any locks held by this path
93 * It is safe to call this on paths that no locks or extent buffers held.
95 noinline void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
99 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
104 btrfs_tree_unlock(p->nodes[i]);
107 free_extent_buffer(p->nodes[i]);
113 * safely gets a reference on the root node of a tree. A lock
114 * is not taken, so a concurrent writer may put a different node
115 * at the root of the tree. See btrfs_lock_root_node for the
118 * The extent buffer returned by this has a reference taken, so
119 * it won't disappear. It may stop being the root of the tree
120 * at any time because there are no locks held.
122 struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
124 struct extent_buffer *eb;
125 spin_lock(&root->node_lock);
127 extent_buffer_get(eb);
128 spin_unlock(&root->node_lock);
132 /* loop around taking references on and locking the root node of the
133 * tree until you end up with a lock on the root. A locked buffer
134 * is returned, with a reference held.
136 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
138 struct extent_buffer *eb;
141 eb = btrfs_root_node(root);
144 spin_lock(&root->node_lock);
145 if (eb == root->node) {
146 spin_unlock(&root->node_lock);
149 spin_unlock(&root->node_lock);
151 btrfs_tree_unlock(eb);
152 free_extent_buffer(eb);
157 /* cowonly root (everything not a reference counted cow subvolume), just get
158 * put onto a simple dirty list. transaction.c walks this to make sure they
159 * get properly updated on disk.
161 static void add_root_to_dirty_list(struct btrfs_root *root)
163 if (root->track_dirty && list_empty(&root->dirty_list)) {
164 list_add(&root->dirty_list,
165 &root->fs_info->dirty_cowonly_roots);
170 * used by snapshot creation to make a copy of a root for a tree with
171 * a given objectid. The buffer with the new root node is returned in
172 * cow_ret, and this func returns zero on success or a negative error code.
174 int btrfs_copy_root(struct btrfs_trans_handle *trans,
175 struct btrfs_root *root,
176 struct extent_buffer *buf,
177 struct extent_buffer **cow_ret, u64 new_root_objectid)
179 struct extent_buffer *cow;
183 struct btrfs_root *new_root;
185 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
189 memcpy(new_root, root, sizeof(*new_root));
190 new_root->root_key.objectid = new_root_objectid;
192 WARN_ON(root->ref_cows && trans->transid !=
193 root->fs_info->running_transaction->transid);
194 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
196 level = btrfs_header_level(buf);
197 nritems = btrfs_header_nritems(buf);
199 cow = btrfs_alloc_free_block(trans, new_root, buf->len, 0,
200 new_root_objectid, trans->transid,
201 level, buf->start, 0);
207 copy_extent_buffer(cow, buf, 0, 0, cow->len);
208 btrfs_set_header_bytenr(cow, cow->start);
209 btrfs_set_header_generation(cow, trans->transid);
210 btrfs_set_header_owner(cow, new_root_objectid);
211 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
213 write_extent_buffer(cow, root->fs_info->fsid,
214 (unsigned long)btrfs_header_fsid(cow),
217 WARN_ON(btrfs_header_generation(buf) > trans->transid);
218 ret = btrfs_inc_ref(trans, new_root, buf, cow, NULL);
224 btrfs_mark_buffer_dirty(cow);
230 * does the dirty work in cow of a single block. The parent block (if
231 * supplied) is updated to point to the new cow copy. The new buffer is marked
232 * dirty and returned locked. If you modify the block it needs to be marked
235 * search_start -- an allocation hint for the new block
237 * empty_size -- a hint that you plan on doing more cow. This is the size in
238 * bytes the allocator should try to find free next to the block it returns.
239 * This is just a hint and may be ignored by the allocator.
241 * prealloc_dest -- if you have already reserved a destination for the cow,
242 * this uses that block instead of allocating a new one.
243 * btrfs_alloc_reserved_extent is used to finish the allocation.
245 static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
246 struct btrfs_root *root,
247 struct extent_buffer *buf,
248 struct extent_buffer *parent, int parent_slot,
249 struct extent_buffer **cow_ret,
250 u64 search_start, u64 empty_size,
254 struct extent_buffer *cow;
263 WARN_ON(!btrfs_tree_locked(buf));
266 parent_start = parent->start;
270 WARN_ON(root->ref_cows && trans->transid !=
271 root->fs_info->running_transaction->transid);
272 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
274 level = btrfs_header_level(buf);
275 nritems = btrfs_header_nritems(buf);
278 struct btrfs_key ins;
280 ins.objectid = prealloc_dest;
281 ins.offset = buf->len;
282 ins.type = BTRFS_EXTENT_ITEM_KEY;
284 ret = btrfs_alloc_reserved_extent(trans, root, parent_start,
285 root->root_key.objectid,
286 trans->transid, level, &ins);
288 cow = btrfs_init_new_buffer(trans, root, prealloc_dest,
291 cow = btrfs_alloc_free_block(trans, root, buf->len,
293 root->root_key.objectid,
294 trans->transid, level,
295 search_start, empty_size);
300 /* cow is set to blocking by btrfs_init_new_buffer */
302 copy_extent_buffer(cow, buf, 0, 0, cow->len);
303 btrfs_set_header_bytenr(cow, cow->start);
304 btrfs_set_header_generation(cow, trans->transid);
305 btrfs_set_header_owner(cow, root->root_key.objectid);
306 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
308 write_extent_buffer(cow, root->fs_info->fsid,
309 (unsigned long)btrfs_header_fsid(cow),
312 WARN_ON(btrfs_header_generation(buf) > trans->transid);
313 if (btrfs_header_generation(buf) != trans->transid) {
315 ret = btrfs_inc_ref(trans, root, buf, cow, &nr_extents);
319 ret = btrfs_cache_ref(trans, root, buf, nr_extents);
321 } else if (btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID) {
323 * There are only two places that can drop reference to
324 * tree blocks owned by living reloc trees, one is here,
325 * the other place is btrfs_drop_subtree. In both places,
326 * we check reference count while tree block is locked.
327 * Furthermore, if reference count is one, it won't get
328 * increased by someone else.
331 ret = btrfs_lookup_extent_ref(trans, root, buf->start,
335 ret = btrfs_update_ref(trans, root, buf, cow,
337 clean_tree_block(trans, root, buf);
339 ret = btrfs_inc_ref(trans, root, buf, cow, NULL);
343 ret = btrfs_update_ref(trans, root, buf, cow, 0, nritems);
346 clean_tree_block(trans, root, buf);
349 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
350 ret = btrfs_reloc_tree_cache_ref(trans, root, cow, buf->start);
354 if (buf == root->node) {
355 WARN_ON(parent && parent != buf);
357 spin_lock(&root->node_lock);
359 extent_buffer_get(cow);
360 spin_unlock(&root->node_lock);
362 if (buf != root->commit_root) {
363 btrfs_free_extent(trans, root, buf->start,
364 buf->len, buf->start,
365 root->root_key.objectid,
366 btrfs_header_generation(buf),
369 free_extent_buffer(buf);
370 add_root_to_dirty_list(root);
372 btrfs_set_node_blockptr(parent, parent_slot,
374 WARN_ON(trans->transid == 0);
375 btrfs_set_node_ptr_generation(parent, parent_slot,
377 btrfs_mark_buffer_dirty(parent);
378 WARN_ON(btrfs_header_generation(parent) != trans->transid);
379 btrfs_free_extent(trans, root, buf->start, buf->len,
380 parent_start, btrfs_header_owner(parent),
381 btrfs_header_generation(parent), level, 1);
384 btrfs_tree_unlock(buf);
385 free_extent_buffer(buf);
386 btrfs_mark_buffer_dirty(cow);
392 * cows a single block, see __btrfs_cow_block for the real work.
393 * This version of it has extra checks so that a block isn't cow'd more than
394 * once per transaction, as long as it hasn't been written yet
396 noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
397 struct btrfs_root *root, struct extent_buffer *buf,
398 struct extent_buffer *parent, int parent_slot,
399 struct extent_buffer **cow_ret, u64 prealloc_dest)
404 if (trans->transaction != root->fs_info->running_transaction) {
405 printk(KERN_CRIT "trans %llu running %llu\n",
406 (unsigned long long)trans->transid,
408 root->fs_info->running_transaction->transid);
411 if (trans->transid != root->fs_info->generation) {
412 printk(KERN_CRIT "trans %llu running %llu\n",
413 (unsigned long long)trans->transid,
414 (unsigned long long)root->fs_info->generation);
418 if (btrfs_header_generation(buf) == trans->transid &&
419 btrfs_header_owner(buf) == root->root_key.objectid &&
420 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
422 WARN_ON(prealloc_dest);
426 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
429 btrfs_set_lock_blocking(parent);
430 btrfs_set_lock_blocking(buf);
432 ret = __btrfs_cow_block(trans, root, buf, parent,
433 parent_slot, cow_ret, search_start, 0,
439 * helper function for defrag to decide if two blocks pointed to by a
440 * node are actually close by
442 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
444 if (blocknr < other && other - (blocknr + blocksize) < 32768)
446 if (blocknr > other && blocknr - (other + blocksize) < 32768)
452 * compare two keys in a memcmp fashion
454 static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
458 btrfs_disk_key_to_cpu(&k1, disk);
460 if (k1.objectid > k2->objectid)
462 if (k1.objectid < k2->objectid)
464 if (k1.type > k2->type)
466 if (k1.type < k2->type)
468 if (k1.offset > k2->offset)
470 if (k1.offset < k2->offset)
476 * same as comp_keys only with two btrfs_key's
478 static int comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
480 if (k1->objectid > k2->objectid)
482 if (k1->objectid < k2->objectid)
484 if (k1->type > k2->type)
486 if (k1->type < k2->type)
488 if (k1->offset > k2->offset)
490 if (k1->offset < k2->offset)
496 * this is used by the defrag code to go through all the
497 * leaves pointed to by a node and reallocate them so that
498 * disk order is close to key order
500 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
501 struct btrfs_root *root, struct extent_buffer *parent,
502 int start_slot, int cache_only, u64 *last_ret,
503 struct btrfs_key *progress)
505 struct extent_buffer *cur;
508 u64 search_start = *last_ret;
518 int progress_passed = 0;
519 struct btrfs_disk_key disk_key;
521 parent_level = btrfs_header_level(parent);
522 if (cache_only && parent_level != 1)
525 if (trans->transaction != root->fs_info->running_transaction)
527 if (trans->transid != root->fs_info->generation)
530 parent_nritems = btrfs_header_nritems(parent);
531 blocksize = btrfs_level_size(root, parent_level - 1);
532 end_slot = parent_nritems;
534 if (parent_nritems == 1)
537 btrfs_set_lock_blocking(parent);
539 for (i = start_slot; i < end_slot; i++) {
542 if (!parent->map_token) {
543 map_extent_buffer(parent,
544 btrfs_node_key_ptr_offset(i),
545 sizeof(struct btrfs_key_ptr),
546 &parent->map_token, &parent->kaddr,
547 &parent->map_start, &parent->map_len,
550 btrfs_node_key(parent, &disk_key, i);
551 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
555 blocknr = btrfs_node_blockptr(parent, i);
556 gen = btrfs_node_ptr_generation(parent, i);
558 last_block = blocknr;
561 other = btrfs_node_blockptr(parent, i - 1);
562 close = close_blocks(blocknr, other, blocksize);
564 if (!close && i < end_slot - 2) {
565 other = btrfs_node_blockptr(parent, i + 1);
566 close = close_blocks(blocknr, other, blocksize);
569 last_block = blocknr;
572 if (parent->map_token) {
573 unmap_extent_buffer(parent, parent->map_token,
575 parent->map_token = NULL;
578 cur = btrfs_find_tree_block(root, blocknr, blocksize);
580 uptodate = btrfs_buffer_uptodate(cur, gen);
583 if (!cur || !uptodate) {
585 free_extent_buffer(cur);
589 cur = read_tree_block(root, blocknr,
591 } else if (!uptodate) {
592 btrfs_read_buffer(cur, gen);
595 if (search_start == 0)
596 search_start = last_block;
598 btrfs_tree_lock(cur);
599 btrfs_set_lock_blocking(cur);
600 err = __btrfs_cow_block(trans, root, cur, parent, i,
603 (end_slot - i) * blocksize), 0);
605 btrfs_tree_unlock(cur);
606 free_extent_buffer(cur);
609 search_start = cur->start;
610 last_block = cur->start;
611 *last_ret = search_start;
612 btrfs_tree_unlock(cur);
613 free_extent_buffer(cur);
615 if (parent->map_token) {
616 unmap_extent_buffer(parent, parent->map_token,
618 parent->map_token = NULL;
624 * The leaf data grows from end-to-front in the node.
625 * this returns the address of the start of the last item,
626 * which is the stop of the leaf data stack
628 static inline unsigned int leaf_data_end(struct btrfs_root *root,
629 struct extent_buffer *leaf)
631 u32 nr = btrfs_header_nritems(leaf);
633 return BTRFS_LEAF_DATA_SIZE(root);
634 return btrfs_item_offset_nr(leaf, nr - 1);
638 * extra debugging checks to make sure all the items in a key are
639 * well formed and in the proper order
641 static int check_node(struct btrfs_root *root, struct btrfs_path *path,
644 struct extent_buffer *parent = NULL;
645 struct extent_buffer *node = path->nodes[level];
646 struct btrfs_disk_key parent_key;
647 struct btrfs_disk_key node_key;
650 struct btrfs_key cpukey;
651 u32 nritems = btrfs_header_nritems(node);
653 if (path->nodes[level + 1])
654 parent = path->nodes[level + 1];
656 slot = path->slots[level];
657 BUG_ON(nritems == 0);
659 parent_slot = path->slots[level + 1];
660 btrfs_node_key(parent, &parent_key, parent_slot);
661 btrfs_node_key(node, &node_key, 0);
662 BUG_ON(memcmp(&parent_key, &node_key,
663 sizeof(struct btrfs_disk_key)));
664 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
665 btrfs_header_bytenr(node));
667 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
669 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
670 btrfs_node_key(node, &node_key, slot);
671 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
673 if (slot < nritems - 1) {
674 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
675 btrfs_node_key(node, &node_key, slot);
676 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
682 * extra checking to make sure all the items in a leaf are
683 * well formed and in the proper order
685 static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
688 struct extent_buffer *leaf = path->nodes[level];
689 struct extent_buffer *parent = NULL;
691 struct btrfs_key cpukey;
692 struct btrfs_disk_key parent_key;
693 struct btrfs_disk_key leaf_key;
694 int slot = path->slots[0];
696 u32 nritems = btrfs_header_nritems(leaf);
698 if (path->nodes[level + 1])
699 parent = path->nodes[level + 1];
705 parent_slot = path->slots[level + 1];
706 btrfs_node_key(parent, &parent_key, parent_slot);
707 btrfs_item_key(leaf, &leaf_key, 0);
709 BUG_ON(memcmp(&parent_key, &leaf_key,
710 sizeof(struct btrfs_disk_key)));
711 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
712 btrfs_header_bytenr(leaf));
714 if (slot != 0 && slot < nritems - 1) {
715 btrfs_item_key(leaf, &leaf_key, slot);
716 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
717 if (comp_keys(&leaf_key, &cpukey) <= 0) {
718 btrfs_print_leaf(root, leaf);
719 printk(KERN_CRIT "slot %d offset bad key\n", slot);
722 if (btrfs_item_offset_nr(leaf, slot - 1) !=
723 btrfs_item_end_nr(leaf, slot)) {
724 btrfs_print_leaf(root, leaf);
725 printk(KERN_CRIT "slot %d offset bad\n", slot);
729 if (slot < nritems - 1) {
730 btrfs_item_key(leaf, &leaf_key, slot);
731 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
732 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
733 if (btrfs_item_offset_nr(leaf, slot) !=
734 btrfs_item_end_nr(leaf, slot + 1)) {
735 btrfs_print_leaf(root, leaf);
736 printk(KERN_CRIT "slot %d offset bad\n", slot);
740 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
741 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
745 static noinline int check_block(struct btrfs_root *root,
746 struct btrfs_path *path, int level)
750 return check_leaf(root, path, level);
751 return check_node(root, path, level);
755 * search for key in the extent_buffer. The items start at offset p,
756 * and they are item_size apart. There are 'max' items in p.
758 * the slot in the array is returned via slot, and it points to
759 * the place where you would insert key if it is not found in
762 * slot may point to max if the key is bigger than all of the keys
764 static noinline int generic_bin_search(struct extent_buffer *eb,
766 int item_size, struct btrfs_key *key,
773 struct btrfs_disk_key *tmp = NULL;
774 struct btrfs_disk_key unaligned;
775 unsigned long offset;
776 char *map_token = NULL;
778 unsigned long map_start = 0;
779 unsigned long map_len = 0;
783 mid = (low + high) / 2;
784 offset = p + mid * item_size;
786 if (!map_token || offset < map_start ||
787 (offset + sizeof(struct btrfs_disk_key)) >
788 map_start + map_len) {
790 unmap_extent_buffer(eb, map_token, KM_USER0);
794 err = map_private_extent_buffer(eb, offset,
795 sizeof(struct btrfs_disk_key),
797 &map_start, &map_len, KM_USER0);
800 tmp = (struct btrfs_disk_key *)(kaddr + offset -
803 read_extent_buffer(eb, &unaligned,
804 offset, sizeof(unaligned));
809 tmp = (struct btrfs_disk_key *)(kaddr + offset -
812 ret = comp_keys(tmp, key);
821 unmap_extent_buffer(eb, map_token, KM_USER0);
827 unmap_extent_buffer(eb, map_token, KM_USER0);
832 * simple bin_search frontend that does the right thing for
835 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
836 int level, int *slot)
839 return generic_bin_search(eb,
840 offsetof(struct btrfs_leaf, items),
841 sizeof(struct btrfs_item),
842 key, btrfs_header_nritems(eb),
845 return generic_bin_search(eb,
846 offsetof(struct btrfs_node, ptrs),
847 sizeof(struct btrfs_key_ptr),
848 key, btrfs_header_nritems(eb),
854 /* given a node and slot number, this reads the blocks it points to. The
855 * extent buffer is returned with a reference taken (but unlocked).
856 * NULL is returned on error.
858 static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
859 struct extent_buffer *parent, int slot)
861 int level = btrfs_header_level(parent);
864 if (slot >= btrfs_header_nritems(parent))
869 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
870 btrfs_level_size(root, level - 1),
871 btrfs_node_ptr_generation(parent, slot));
875 * node level balancing, used to make sure nodes are in proper order for
876 * item deletion. We balance from the top down, so we have to make sure
877 * that a deletion won't leave an node completely empty later on.
879 static noinline int balance_level(struct btrfs_trans_handle *trans,
880 struct btrfs_root *root,
881 struct btrfs_path *path, int level)
883 struct extent_buffer *right = NULL;
884 struct extent_buffer *mid;
885 struct extent_buffer *left = NULL;
886 struct extent_buffer *parent = NULL;
890 int orig_slot = path->slots[level];
891 int err_on_enospc = 0;
897 mid = path->nodes[level];
899 WARN_ON(!path->locks[level]);
900 WARN_ON(btrfs_header_generation(mid) != trans->transid);
902 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
904 if (level < BTRFS_MAX_LEVEL - 1)
905 parent = path->nodes[level + 1];
906 pslot = path->slots[level + 1];
909 * deal with the case where there is only one pointer in the root
910 * by promoting the node below to a root
913 struct extent_buffer *child;
915 if (btrfs_header_nritems(mid) != 1)
918 /* promote the child to a root */
919 child = read_node_slot(root, mid, 0);
920 btrfs_tree_lock(child);
921 btrfs_set_lock_blocking(child);
923 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
926 spin_lock(&root->node_lock);
928 spin_unlock(&root->node_lock);
930 ret = btrfs_update_extent_ref(trans, root, child->start,
931 mid->start, child->start,
932 root->root_key.objectid,
933 trans->transid, level - 1);
936 add_root_to_dirty_list(root);
937 btrfs_tree_unlock(child);
939 path->locks[level] = 0;
940 path->nodes[level] = NULL;
941 clean_tree_block(trans, root, mid);
942 btrfs_tree_unlock(mid);
943 /* once for the path */
944 free_extent_buffer(mid);
945 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
946 mid->start, root->root_key.objectid,
947 btrfs_header_generation(mid),
949 /* once for the root ptr */
950 free_extent_buffer(mid);
953 if (btrfs_header_nritems(mid) >
954 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
957 if (btrfs_header_nritems(mid) < 2)
960 left = read_node_slot(root, parent, pslot - 1);
962 btrfs_tree_lock(left);
963 btrfs_set_lock_blocking(left);
964 wret = btrfs_cow_block(trans, root, left,
965 parent, pslot - 1, &left, 0);
971 right = read_node_slot(root, parent, pslot + 1);
973 btrfs_tree_lock(right);
974 btrfs_set_lock_blocking(right);
975 wret = btrfs_cow_block(trans, root, right,
976 parent, pslot + 1, &right, 0);
983 /* first, try to make some room in the middle buffer */
985 orig_slot += btrfs_header_nritems(left);
986 wret = push_node_left(trans, root, left, mid, 1);
989 if (btrfs_header_nritems(mid) < 2)
994 * then try to empty the right most buffer into the middle
997 wret = push_node_left(trans, root, mid, right, 1);
998 if (wret < 0 && wret != -ENOSPC)
1000 if (btrfs_header_nritems(right) == 0) {
1001 u64 bytenr = right->start;
1002 u64 generation = btrfs_header_generation(parent);
1003 u32 blocksize = right->len;
1005 clean_tree_block(trans, root, right);
1006 btrfs_tree_unlock(right);
1007 free_extent_buffer(right);
1009 wret = del_ptr(trans, root, path, level + 1, pslot +
1013 wret = btrfs_free_extent(trans, root, bytenr,
1014 blocksize, parent->start,
1015 btrfs_header_owner(parent),
1016 generation, level, 1);
1020 struct btrfs_disk_key right_key;
1021 btrfs_node_key(right, &right_key, 0);
1022 btrfs_set_node_key(parent, &right_key, pslot + 1);
1023 btrfs_mark_buffer_dirty(parent);
1026 if (btrfs_header_nritems(mid) == 1) {
1028 * we're not allowed to leave a node with one item in the
1029 * tree during a delete. A deletion from lower in the tree
1030 * could try to delete the only pointer in this node.
1031 * So, pull some keys from the left.
1032 * There has to be a left pointer at this point because
1033 * otherwise we would have pulled some pointers from the
1037 wret = balance_node_right(trans, root, mid, left);
1043 wret = push_node_left(trans, root, left, mid, 1);
1049 if (btrfs_header_nritems(mid) == 0) {
1050 /* we've managed to empty the middle node, drop it */
1051 u64 root_gen = btrfs_header_generation(parent);
1052 u64 bytenr = mid->start;
1053 u32 blocksize = mid->len;
1055 clean_tree_block(trans, root, mid);
1056 btrfs_tree_unlock(mid);
1057 free_extent_buffer(mid);
1059 wret = del_ptr(trans, root, path, level + 1, pslot);
1062 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
1064 btrfs_header_owner(parent),
1065 root_gen, level, 1);
1069 /* update the parent key to reflect our changes */
1070 struct btrfs_disk_key mid_key;
1071 btrfs_node_key(mid, &mid_key, 0);
1072 btrfs_set_node_key(parent, &mid_key, pslot);
1073 btrfs_mark_buffer_dirty(parent);
1076 /* update the path */
1078 if (btrfs_header_nritems(left) > orig_slot) {
1079 extent_buffer_get(left);
1080 /* left was locked after cow */
1081 path->nodes[level] = left;
1082 path->slots[level + 1] -= 1;
1083 path->slots[level] = orig_slot;
1085 btrfs_tree_unlock(mid);
1086 free_extent_buffer(mid);
1089 orig_slot -= btrfs_header_nritems(left);
1090 path->slots[level] = orig_slot;
1093 /* double check we haven't messed things up */
1094 check_block(root, path, level);
1096 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
1100 btrfs_tree_unlock(right);
1101 free_extent_buffer(right);
1104 if (path->nodes[level] != left)
1105 btrfs_tree_unlock(left);
1106 free_extent_buffer(left);
1111 /* Node balancing for insertion. Here we only split or push nodes around
1112 * when they are completely full. This is also done top down, so we
1113 * have to be pessimistic.
1115 static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1116 struct btrfs_root *root,
1117 struct btrfs_path *path, int level)
1119 struct extent_buffer *right = NULL;
1120 struct extent_buffer *mid;
1121 struct extent_buffer *left = NULL;
1122 struct extent_buffer *parent = NULL;
1126 int orig_slot = path->slots[level];
1132 mid = path->nodes[level];
1133 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1134 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1136 if (level < BTRFS_MAX_LEVEL - 1)
1137 parent = path->nodes[level + 1];
1138 pslot = path->slots[level + 1];
1143 left = read_node_slot(root, parent, pslot - 1);
1145 /* first, try to make some room in the middle buffer */
1149 btrfs_tree_lock(left);
1150 btrfs_set_lock_blocking(left);
1152 left_nr = btrfs_header_nritems(left);
1153 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1156 ret = btrfs_cow_block(trans, root, left, parent,
1157 pslot - 1, &left, 0);
1161 wret = push_node_left(trans, root,
1168 struct btrfs_disk_key disk_key;
1169 orig_slot += left_nr;
1170 btrfs_node_key(mid, &disk_key, 0);
1171 btrfs_set_node_key(parent, &disk_key, pslot);
1172 btrfs_mark_buffer_dirty(parent);
1173 if (btrfs_header_nritems(left) > orig_slot) {
1174 path->nodes[level] = left;
1175 path->slots[level + 1] -= 1;
1176 path->slots[level] = orig_slot;
1177 btrfs_tree_unlock(mid);
1178 free_extent_buffer(mid);
1181 btrfs_header_nritems(left);
1182 path->slots[level] = orig_slot;
1183 btrfs_tree_unlock(left);
1184 free_extent_buffer(left);
1188 btrfs_tree_unlock(left);
1189 free_extent_buffer(left);
1191 right = read_node_slot(root, parent, pslot + 1);
1194 * then try to empty the right most buffer into the middle
1199 btrfs_tree_lock(right);
1200 btrfs_set_lock_blocking(right);
1202 right_nr = btrfs_header_nritems(right);
1203 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1206 ret = btrfs_cow_block(trans, root, right,
1212 wret = balance_node_right(trans, root,
1219 struct btrfs_disk_key disk_key;
1221 btrfs_node_key(right, &disk_key, 0);
1222 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1223 btrfs_mark_buffer_dirty(parent);
1225 if (btrfs_header_nritems(mid) <= orig_slot) {
1226 path->nodes[level] = right;
1227 path->slots[level + 1] += 1;
1228 path->slots[level] = orig_slot -
1229 btrfs_header_nritems(mid);
1230 btrfs_tree_unlock(mid);
1231 free_extent_buffer(mid);
1233 btrfs_tree_unlock(right);
1234 free_extent_buffer(right);
1238 btrfs_tree_unlock(right);
1239 free_extent_buffer(right);
1245 * readahead one full node of leaves, finding things that are close
1246 * to the block in 'slot', and triggering ra on them.
1248 static noinline void reada_for_search(struct btrfs_root *root,
1249 struct btrfs_path *path,
1250 int level, int slot, u64 objectid)
1252 struct extent_buffer *node;
1253 struct btrfs_disk_key disk_key;
1258 int direction = path->reada;
1259 struct extent_buffer *eb;
1267 if (!path->nodes[level])
1270 node = path->nodes[level];
1272 search = btrfs_node_blockptr(node, slot);
1273 blocksize = btrfs_level_size(root, level - 1);
1274 eb = btrfs_find_tree_block(root, search, blocksize);
1276 free_extent_buffer(eb);
1282 nritems = btrfs_header_nritems(node);
1285 if (direction < 0) {
1289 } else if (direction > 0) {
1294 if (path->reada < 0 && objectid) {
1295 btrfs_node_key(node, &disk_key, nr);
1296 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1299 search = btrfs_node_blockptr(node, nr);
1300 if ((search <= target && target - search <= 65536) ||
1301 (search > target && search - target <= 65536)) {
1302 readahead_tree_block(root, search, blocksize,
1303 btrfs_node_ptr_generation(node, nr));
1307 if ((nread > 65536 || nscan > 32))
1313 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1316 static noinline int reada_for_balance(struct btrfs_root *root,
1317 struct btrfs_path *path, int level)
1321 struct extent_buffer *parent;
1322 struct extent_buffer *eb;
1329 parent = path->nodes[level - 1];
1333 nritems = btrfs_header_nritems(parent);
1334 slot = path->slots[level];
1335 blocksize = btrfs_level_size(root, level);
1338 block1 = btrfs_node_blockptr(parent, slot - 1);
1339 gen = btrfs_node_ptr_generation(parent, slot - 1);
1340 eb = btrfs_find_tree_block(root, block1, blocksize);
1341 if (eb && btrfs_buffer_uptodate(eb, gen))
1343 free_extent_buffer(eb);
1345 if (slot < nritems) {
1346 block2 = btrfs_node_blockptr(parent, slot + 1);
1347 gen = btrfs_node_ptr_generation(parent, slot + 1);
1348 eb = btrfs_find_tree_block(root, block2, blocksize);
1349 if (eb && btrfs_buffer_uptodate(eb, gen))
1351 free_extent_buffer(eb);
1353 if (block1 || block2) {
1355 btrfs_release_path(root, path);
1357 readahead_tree_block(root, block1, blocksize, 0);
1359 readahead_tree_block(root, block2, blocksize, 0);
1362 eb = read_tree_block(root, block1, blocksize, 0);
1363 free_extent_buffer(eb);
1366 eb = read_tree_block(root, block2, blocksize, 0);
1367 free_extent_buffer(eb);
1375 * when we walk down the tree, it is usually safe to unlock the higher layers
1376 * in the tree. The exceptions are when our path goes through slot 0, because
1377 * operations on the tree might require changing key pointers higher up in the
1380 * callers might also have set path->keep_locks, which tells this code to keep
1381 * the lock if the path points to the last slot in the block. This is part of
1382 * walking through the tree, and selecting the next slot in the higher block.
1384 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1385 * if lowest_unlock is 1, level 0 won't be unlocked
1387 static noinline void unlock_up(struct btrfs_path *path, int level,
1391 int skip_level = level;
1393 struct extent_buffer *t;
1395 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1396 if (!path->nodes[i])
1398 if (!path->locks[i])
1400 if (!no_skips && path->slots[i] == 0) {
1404 if (!no_skips && path->keep_locks) {
1407 nritems = btrfs_header_nritems(t);
1408 if (nritems < 1 || path->slots[i] >= nritems - 1) {
1413 if (skip_level < i && i >= lowest_unlock)
1417 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1418 btrfs_tree_unlock(t);
1425 * This releases any locks held in the path starting at level and
1426 * going all the way up to the root.
1428 * btrfs_search_slot will keep the lock held on higher nodes in a few
1429 * corner cases, such as COW of the block at slot zero in the node. This
1430 * ignores those rules, and it should only be called when there are no
1431 * more updates to be done higher up in the tree.
1433 noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1437 if (path->keep_locks || path->lowest_level)
1440 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1441 if (!path->nodes[i])
1443 if (!path->locks[i])
1445 btrfs_tree_unlock(path->nodes[i]);
1451 * look for key in the tree. path is filled in with nodes along the way
1452 * if key is found, we return zero and you can find the item in the leaf
1453 * level of the path (level 0)
1455 * If the key isn't found, the path points to the slot where it should
1456 * be inserted, and 1 is returned. If there are other errors during the
1457 * search a negative error number is returned.
1459 * if ins_len > 0, nodes and leaves will be split as we walk down the
1460 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1463 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1464 *root, struct btrfs_key *key, struct btrfs_path *p, int
1467 struct extent_buffer *b;
1468 struct extent_buffer *tmp;
1472 int should_reada = p->reada;
1473 int lowest_unlock = 1;
1475 u8 lowest_level = 0;
1478 struct btrfs_key prealloc_block;
1480 lowest_level = p->lowest_level;
1481 WARN_ON(lowest_level && ins_len > 0);
1482 WARN_ON(p->nodes[0] != NULL);
1487 prealloc_block.objectid = 0;
1490 if (p->skip_locking)
1491 b = btrfs_root_node(root);
1493 b = btrfs_lock_root_node(root);
1496 level = btrfs_header_level(b);
1499 * setup the path here so we can release it under lock
1500 * contention with the cow code
1502 p->nodes[level] = b;
1503 if (!p->skip_locking)
1504 p->locks[level] = 1;
1509 /* is a cow on this block not required */
1510 if (btrfs_header_generation(b) == trans->transid &&
1511 btrfs_header_owner(b) == root->root_key.objectid &&
1512 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
1516 /* ok, we have to cow, is our old prealloc the right
1519 if (prealloc_block.objectid &&
1520 prealloc_block.offset != b->len) {
1521 btrfs_release_path(root, p);
1522 btrfs_free_reserved_extent(root,
1523 prealloc_block.objectid,
1524 prealloc_block.offset);
1525 prealloc_block.objectid = 0;
1530 * for higher level blocks, try not to allocate blocks
1531 * with the block and the parent locks held.
1533 if (level > 0 && !prealloc_block.objectid) {
1535 u64 hint = b->start;
1537 btrfs_release_path(root, p);
1538 ret = btrfs_reserve_extent(trans, root,
1541 &prealloc_block, 0);
1546 btrfs_set_path_blocking(p);
1548 wret = btrfs_cow_block(trans, root, b,
1549 p->nodes[level + 1],
1550 p->slots[level + 1],
1551 &b, prealloc_block.objectid);
1552 prealloc_block.objectid = 0;
1554 free_extent_buffer(b);
1560 BUG_ON(!cow && ins_len);
1561 if (level != btrfs_header_level(b))
1563 level = btrfs_header_level(b);
1565 p->nodes[level] = b;
1566 if (!p->skip_locking)
1567 p->locks[level] = 1;
1569 btrfs_clear_path_blocking(p);
1572 * we have a lock on b and as long as we aren't changing
1573 * the tree, there is no way to for the items in b to change.
1574 * It is safe to drop the lock on our parent before we
1575 * go through the expensive btree search on b.
1577 * If cow is true, then we might be changing slot zero,
1578 * which may require changing the parent. So, we can't
1579 * drop the lock until after we know which slot we're
1583 btrfs_unlock_up_safe(p, level + 1);
1585 ret = check_block(root, p, level);
1591 ret = bin_search(b, key, level, &slot);
1594 if (ret && slot > 0)
1596 p->slots[level] = slot;
1597 if ((p->search_for_split || ins_len > 0) &&
1598 btrfs_header_nritems(b) >=
1599 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1602 sret = reada_for_balance(root, p, level);
1606 btrfs_set_path_blocking(p);
1607 sret = split_node(trans, root, p, level);
1608 btrfs_clear_path_blocking(p);
1615 b = p->nodes[level];
1616 slot = p->slots[level];
1617 } else if (ins_len < 0 &&
1618 btrfs_header_nritems(b) <
1619 BTRFS_NODEPTRS_PER_BLOCK(root) / 4) {
1622 sret = reada_for_balance(root, p, level);
1626 btrfs_set_path_blocking(p);
1627 sret = balance_level(trans, root, p, level);
1628 btrfs_clear_path_blocking(p);
1634 b = p->nodes[level];
1636 btrfs_release_path(NULL, p);
1639 slot = p->slots[level];
1640 BUG_ON(btrfs_header_nritems(b) == 1);
1642 unlock_up(p, level, lowest_unlock);
1644 /* this is only true while dropping a snapshot */
1645 if (level == lowest_level) {
1650 blocknr = btrfs_node_blockptr(b, slot);
1651 gen = btrfs_node_ptr_generation(b, slot);
1652 blocksize = btrfs_level_size(root, level - 1);
1654 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1655 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1659 * reduce lock contention at high levels
1660 * of the btree by dropping locks before
1664 btrfs_release_path(NULL, p);
1666 free_extent_buffer(tmp);
1668 reada_for_search(root, p,
1672 tmp = read_tree_block(root, blocknr,
1675 free_extent_buffer(tmp);
1678 btrfs_set_path_blocking(p);
1680 free_extent_buffer(tmp);
1682 reada_for_search(root, p,
1685 b = read_node_slot(root, b, slot);
1688 if (!p->skip_locking) {
1691 btrfs_clear_path_blocking(p);
1692 lret = btrfs_try_spin_lock(b);
1695 btrfs_set_path_blocking(p);
1697 btrfs_clear_path_blocking(p);
1701 p->slots[level] = slot;
1703 btrfs_leaf_free_space(root, b) < ins_len) {
1706 btrfs_set_path_blocking(p);
1707 sret = split_leaf(trans, root, key,
1708 p, ins_len, ret == 0);
1709 btrfs_clear_path_blocking(p);
1717 if (!p->search_for_split)
1718 unlock_up(p, level, lowest_unlock);
1725 * we don't really know what they plan on doing with the path
1726 * from here on, so for now just mark it as blocking
1728 btrfs_set_path_blocking(p);
1729 if (prealloc_block.objectid) {
1730 btrfs_free_reserved_extent(root,
1731 prealloc_block.objectid,
1732 prealloc_block.offset);
1737 int btrfs_merge_path(struct btrfs_trans_handle *trans,
1738 struct btrfs_root *root,
1739 struct btrfs_key *node_keys,
1740 u64 *nodes, int lowest_level)
1742 struct extent_buffer *eb;
1743 struct extent_buffer *parent;
1744 struct btrfs_key key;
1753 eb = btrfs_lock_root_node(root);
1754 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0);
1757 btrfs_set_lock_blocking(eb);
1761 level = btrfs_header_level(parent);
1762 if (level == 0 || level <= lowest_level)
1765 ret = bin_search(parent, &node_keys[lowest_level], level,
1767 if (ret && slot > 0)
1770 bytenr = btrfs_node_blockptr(parent, slot);
1771 if (nodes[level - 1] == bytenr)
1774 blocksize = btrfs_level_size(root, level - 1);
1775 generation = btrfs_node_ptr_generation(parent, slot);
1776 btrfs_node_key_to_cpu(eb, &key, slot);
1777 key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key));
1779 if (generation == trans->transid) {
1780 eb = read_tree_block(root, bytenr, blocksize,
1782 btrfs_tree_lock(eb);
1783 btrfs_set_lock_blocking(eb);
1787 * if node keys match and node pointer hasn't been modified
1788 * in the running transaction, we can merge the path. for
1789 * blocks owened by reloc trees, the node pointer check is
1790 * skipped, this is because these blocks are fully controlled
1791 * by the space balance code, no one else can modify them.
1793 if (!nodes[level - 1] || !key_match ||
1794 (generation == trans->transid &&
1795 btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID)) {
1796 if (level == 1 || level == lowest_level + 1) {
1797 if (generation == trans->transid) {
1798 btrfs_tree_unlock(eb);
1799 free_extent_buffer(eb);
1804 if (generation != trans->transid) {
1805 eb = read_tree_block(root, bytenr, blocksize,
1807 btrfs_tree_lock(eb);
1808 btrfs_set_lock_blocking(eb);
1811 ret = btrfs_cow_block(trans, root, eb, parent, slot,
1815 if (root->root_key.objectid ==
1816 BTRFS_TREE_RELOC_OBJECTID) {
1817 if (!nodes[level - 1]) {
1818 nodes[level - 1] = eb->start;
1819 memcpy(&node_keys[level - 1], &key,
1820 sizeof(node_keys[0]));
1826 btrfs_tree_unlock(parent);
1827 free_extent_buffer(parent);
1832 btrfs_set_node_blockptr(parent, slot, nodes[level - 1]);
1833 btrfs_set_node_ptr_generation(parent, slot, trans->transid);
1834 btrfs_mark_buffer_dirty(parent);
1836 ret = btrfs_inc_extent_ref(trans, root,
1838 blocksize, parent->start,
1839 btrfs_header_owner(parent),
1840 btrfs_header_generation(parent),
1845 * If the block was created in the running transaction,
1846 * it's possible this is the last reference to it, so we
1847 * should drop the subtree.
1849 if (generation == trans->transid) {
1850 ret = btrfs_drop_subtree(trans, root, eb, parent);
1852 btrfs_tree_unlock(eb);
1853 free_extent_buffer(eb);
1855 ret = btrfs_free_extent(trans, root, bytenr,
1856 blocksize, parent->start,
1857 btrfs_header_owner(parent),
1858 btrfs_header_generation(parent),
1864 btrfs_tree_unlock(parent);
1865 free_extent_buffer(parent);
1870 * adjust the pointers going up the tree, starting at level
1871 * making sure the right key of each node is points to 'key'.
1872 * This is used after shifting pointers to the left, so it stops
1873 * fixing up pointers when a given leaf/node is not in slot 0 of the
1876 * If this fails to write a tree block, it returns -1, but continues
1877 * fixing up the blocks in ram so the tree is consistent.
1879 static int fixup_low_keys(struct btrfs_trans_handle *trans,
1880 struct btrfs_root *root, struct btrfs_path *path,
1881 struct btrfs_disk_key *key, int level)
1885 struct extent_buffer *t;
1887 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1888 int tslot = path->slots[i];
1889 if (!path->nodes[i])
1892 btrfs_set_node_key(t, key, tslot);
1893 btrfs_mark_buffer_dirty(path->nodes[i]);
1903 * This function isn't completely safe. It's the caller's responsibility
1904 * that the new key won't break the order
1906 int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1907 struct btrfs_root *root, struct btrfs_path *path,
1908 struct btrfs_key *new_key)
1910 struct btrfs_disk_key disk_key;
1911 struct extent_buffer *eb;
1914 eb = path->nodes[0];
1915 slot = path->slots[0];
1917 btrfs_item_key(eb, &disk_key, slot - 1);
1918 if (comp_keys(&disk_key, new_key) >= 0)
1921 if (slot < btrfs_header_nritems(eb) - 1) {
1922 btrfs_item_key(eb, &disk_key, slot + 1);
1923 if (comp_keys(&disk_key, new_key) <= 0)
1927 btrfs_cpu_key_to_disk(&disk_key, new_key);
1928 btrfs_set_item_key(eb, &disk_key, slot);
1929 btrfs_mark_buffer_dirty(eb);
1931 fixup_low_keys(trans, root, path, &disk_key, 1);
1936 * try to push data from one node into the next node left in the
1939 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1940 * error, and > 0 if there was no room in the left hand block.
1942 static int push_node_left(struct btrfs_trans_handle *trans,
1943 struct btrfs_root *root, struct extent_buffer *dst,
1944 struct extent_buffer *src, int empty)
1951 src_nritems = btrfs_header_nritems(src);
1952 dst_nritems = btrfs_header_nritems(dst);
1953 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1954 WARN_ON(btrfs_header_generation(src) != trans->transid);
1955 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1957 if (!empty && src_nritems <= 8)
1960 if (push_items <= 0)
1964 push_items = min(src_nritems, push_items);
1965 if (push_items < src_nritems) {
1966 /* leave at least 8 pointers in the node if
1967 * we aren't going to empty it
1969 if (src_nritems - push_items < 8) {
1970 if (push_items <= 8)
1976 push_items = min(src_nritems - 8, push_items);
1978 copy_extent_buffer(dst, src,
1979 btrfs_node_key_ptr_offset(dst_nritems),
1980 btrfs_node_key_ptr_offset(0),
1981 push_items * sizeof(struct btrfs_key_ptr));
1983 if (push_items < src_nritems) {
1984 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1985 btrfs_node_key_ptr_offset(push_items),
1986 (src_nritems - push_items) *
1987 sizeof(struct btrfs_key_ptr));
1989 btrfs_set_header_nritems(src, src_nritems - push_items);
1990 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1991 btrfs_mark_buffer_dirty(src);
1992 btrfs_mark_buffer_dirty(dst);
1994 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
2001 * try to push data from one node into the next node right in the
2004 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2005 * error, and > 0 if there was no room in the right hand block.
2007 * this will only push up to 1/2 the contents of the left node over
2009 static int balance_node_right(struct btrfs_trans_handle *trans,
2010 struct btrfs_root *root,
2011 struct extent_buffer *dst,
2012 struct extent_buffer *src)
2020 WARN_ON(btrfs_header_generation(src) != trans->transid);
2021 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2023 src_nritems = btrfs_header_nritems(src);
2024 dst_nritems = btrfs_header_nritems(dst);
2025 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
2026 if (push_items <= 0)
2029 if (src_nritems < 4)
2032 max_push = src_nritems / 2 + 1;
2033 /* don't try to empty the node */
2034 if (max_push >= src_nritems)
2037 if (max_push < push_items)
2038 push_items = max_push;
2040 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2041 btrfs_node_key_ptr_offset(0),
2043 sizeof(struct btrfs_key_ptr));
2045 copy_extent_buffer(dst, src,
2046 btrfs_node_key_ptr_offset(0),
2047 btrfs_node_key_ptr_offset(src_nritems - push_items),
2048 push_items * sizeof(struct btrfs_key_ptr));
2050 btrfs_set_header_nritems(src, src_nritems - push_items);
2051 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2053 btrfs_mark_buffer_dirty(src);
2054 btrfs_mark_buffer_dirty(dst);
2056 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
2063 * helper function to insert a new root level in the tree.
2064 * A new node is allocated, and a single item is inserted to
2065 * point to the existing root
2067 * returns zero on success or < 0 on failure.
2069 static noinline int insert_new_root(struct btrfs_trans_handle *trans,
2070 struct btrfs_root *root,
2071 struct btrfs_path *path, int level)
2074 struct extent_buffer *lower;
2075 struct extent_buffer *c;
2076 struct extent_buffer *old;
2077 struct btrfs_disk_key lower_key;
2080 BUG_ON(path->nodes[level]);
2081 BUG_ON(path->nodes[level-1] != root->node);
2083 lower = path->nodes[level-1];
2085 btrfs_item_key(lower, &lower_key, 0);
2087 btrfs_node_key(lower, &lower_key, 0);
2089 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
2090 root->root_key.objectid, trans->transid,
2091 level, root->node->start, 0);
2095 memset_extent_buffer(c, 0, 0, root->nodesize);
2096 btrfs_set_header_nritems(c, 1);
2097 btrfs_set_header_level(c, level);
2098 btrfs_set_header_bytenr(c, c->start);
2099 btrfs_set_header_generation(c, trans->transid);
2100 btrfs_set_header_owner(c, root->root_key.objectid);
2102 write_extent_buffer(c, root->fs_info->fsid,
2103 (unsigned long)btrfs_header_fsid(c),
2106 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2107 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2110 btrfs_set_node_key(c, &lower_key, 0);
2111 btrfs_set_node_blockptr(c, 0, lower->start);
2112 lower_gen = btrfs_header_generation(lower);
2113 WARN_ON(lower_gen != trans->transid);
2115 btrfs_set_node_ptr_generation(c, 0, lower_gen);
2117 btrfs_mark_buffer_dirty(c);
2119 spin_lock(&root->node_lock);
2122 spin_unlock(&root->node_lock);
2124 ret = btrfs_update_extent_ref(trans, root, lower->start,
2125 lower->start, c->start,
2126 root->root_key.objectid,
2127 trans->transid, level - 1);
2130 /* the super has an extra ref to root->node */
2131 free_extent_buffer(old);
2133 add_root_to_dirty_list(root);
2134 extent_buffer_get(c);
2135 path->nodes[level] = c;
2136 path->locks[level] = 1;
2137 path->slots[level] = 0;
2142 * worker function to insert a single pointer in a node.
2143 * the node should have enough room for the pointer already
2145 * slot and level indicate where you want the key to go, and
2146 * blocknr is the block the key points to.
2148 * returns zero on success and < 0 on any error
2150 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2151 *root, struct btrfs_path *path, struct btrfs_disk_key
2152 *key, u64 bytenr, int slot, int level)
2154 struct extent_buffer *lower;
2157 BUG_ON(!path->nodes[level]);
2158 lower = path->nodes[level];
2159 nritems = btrfs_header_nritems(lower);
2162 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
2164 if (slot != nritems) {
2165 memmove_extent_buffer(lower,
2166 btrfs_node_key_ptr_offset(slot + 1),
2167 btrfs_node_key_ptr_offset(slot),
2168 (nritems - slot) * sizeof(struct btrfs_key_ptr));
2170 btrfs_set_node_key(lower, key, slot);
2171 btrfs_set_node_blockptr(lower, slot, bytenr);
2172 WARN_ON(trans->transid == 0);
2173 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
2174 btrfs_set_header_nritems(lower, nritems + 1);
2175 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;
2200 c = path->nodes[level];
2201 WARN_ON(btrfs_header_generation(c) != trans->transid);
2202 if (c == root->node) {
2203 /* trying to split the root, lets make a new one */
2204 ret = insert_new_root(trans, root, path, level + 1);
2208 ret = push_nodes_for_insert(trans, root, path, level);
2209 c = path->nodes[level];
2210 if (!ret && btrfs_header_nritems(c) <
2211 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
2217 c_nritems = btrfs_header_nritems(c);
2219 split = btrfs_alloc_free_block(trans, root, root->nodesize,
2220 path->nodes[level + 1]->start,
2221 root->root_key.objectid,
2222 trans->transid, level, c->start, 0);
2224 return PTR_ERR(split);
2226 btrfs_set_header_flags(split, btrfs_header_flags(c));
2227 btrfs_set_header_level(split, btrfs_header_level(c));
2228 btrfs_set_header_bytenr(split, split->start);
2229 btrfs_set_header_generation(split, trans->transid);
2230 btrfs_set_header_owner(split, root->root_key.objectid);
2231 btrfs_set_header_flags(split, 0);
2232 write_extent_buffer(split, root->fs_info->fsid,
2233 (unsigned long)btrfs_header_fsid(split),
2235 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2236 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2239 mid = (c_nritems + 1) / 2;
2241 copy_extent_buffer(split, c,
2242 btrfs_node_key_ptr_offset(0),
2243 btrfs_node_key_ptr_offset(mid),
2244 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2245 btrfs_set_header_nritems(split, c_nritems - mid);
2246 btrfs_set_header_nritems(c, mid);
2249 btrfs_mark_buffer_dirty(c);
2250 btrfs_mark_buffer_dirty(split);
2252 btrfs_node_key(split, &disk_key, 0);
2253 wret = insert_ptr(trans, root, path, &disk_key, split->start,
2254 path->slots[level + 1] + 1,
2259 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
2262 if (path->slots[level] >= mid) {
2263 path->slots[level] -= mid;
2264 btrfs_tree_unlock(c);
2265 free_extent_buffer(c);
2266 path->nodes[level] = split;
2267 path->slots[level + 1] += 1;
2269 btrfs_tree_unlock(split);
2270 free_extent_buffer(split);
2276 * how many bytes are required to store the items in a leaf. start
2277 * and nr indicate which items in the leaf to check. This totals up the
2278 * space used both by the item structs and the item data
2280 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
2283 int nritems = btrfs_header_nritems(l);
2284 int end = min(nritems, start + nr) - 1;
2288 data_len = btrfs_item_end_nr(l, start);
2289 data_len = data_len - btrfs_item_offset_nr(l, end);
2290 data_len += sizeof(struct btrfs_item) * nr;
2291 WARN_ON(data_len < 0);
2296 * The space between the end of the leaf items and
2297 * the start of the leaf data. IOW, how much room
2298 * the leaf has left for both items and data
2300 noinline int btrfs_leaf_free_space(struct btrfs_root *root,
2301 struct extent_buffer *leaf)
2303 int nritems = btrfs_header_nritems(leaf);
2305 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2307 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2308 "used %d nritems %d\n",
2309 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
2310 leaf_space_used(leaf, 0, nritems), nritems);
2316 * push some data in the path leaf to the right, trying to free up at
2317 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2319 * returns 1 if the push failed because the other node didn't have enough
2320 * room, 0 if everything worked out and < 0 if there were major errors.
2322 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
2323 *root, struct btrfs_path *path, int data_size,
2326 struct extent_buffer *left = path->nodes[0];
2327 struct extent_buffer *right;
2328 struct extent_buffer *upper;
2329 struct btrfs_disk_key disk_key;
2335 struct btrfs_item *item;
2343 slot = path->slots[1];
2344 if (!path->nodes[1])
2347 upper = path->nodes[1];
2348 if (slot >= btrfs_header_nritems(upper) - 1)
2351 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2353 right = read_node_slot(root, upper, slot + 1);
2354 btrfs_tree_lock(right);
2355 btrfs_set_lock_blocking(right);
2357 free_space = btrfs_leaf_free_space(root, right);
2358 if (free_space < data_size)
2361 /* cow and double check */
2362 ret = btrfs_cow_block(trans, root, right, upper,
2363 slot + 1, &right, 0);
2367 free_space = btrfs_leaf_free_space(root, right);
2368 if (free_space < data_size)
2371 left_nritems = btrfs_header_nritems(left);
2372 if (left_nritems == 0)
2380 if (path->slots[0] >= left_nritems)
2381 push_space += data_size;
2383 i = left_nritems - 1;
2385 item = btrfs_item_nr(left, i);
2387 if (!empty && push_items > 0) {
2388 if (path->slots[0] > i)
2390 if (path->slots[0] == i) {
2391 int space = btrfs_leaf_free_space(root, left);
2392 if (space + push_space * 2 > free_space)
2397 if (path->slots[0] == i)
2398 push_space += data_size;
2400 if (!left->map_token) {
2401 map_extent_buffer(left, (unsigned long)item,
2402 sizeof(struct btrfs_item),
2403 &left->map_token, &left->kaddr,
2404 &left->map_start, &left->map_len,
2408 this_item_size = btrfs_item_size(left, item);
2409 if (this_item_size + sizeof(*item) + push_space > free_space)
2413 push_space += this_item_size + sizeof(*item);
2418 if (left->map_token) {
2419 unmap_extent_buffer(left, left->map_token, KM_USER1);
2420 left->map_token = NULL;
2423 if (push_items == 0)
2426 if (!empty && push_items == left_nritems)
2429 /* push left to right */
2430 right_nritems = btrfs_header_nritems(right);
2432 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
2433 push_space -= leaf_data_end(root, left);
2435 /* make room in the right data area */
2436 data_end = leaf_data_end(root, right);
2437 memmove_extent_buffer(right,
2438 btrfs_leaf_data(right) + data_end - push_space,
2439 btrfs_leaf_data(right) + data_end,
2440 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2442 /* copy from the left data area */
2443 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
2444 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2445 btrfs_leaf_data(left) + leaf_data_end(root, left),
2448 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2449 btrfs_item_nr_offset(0),
2450 right_nritems * sizeof(struct btrfs_item));
2452 /* copy the items from left to right */
2453 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2454 btrfs_item_nr_offset(left_nritems - push_items),
2455 push_items * sizeof(struct btrfs_item));
2457 /* update the item pointers */
2458 right_nritems += push_items;
2459 btrfs_set_header_nritems(right, right_nritems);
2460 push_space = BTRFS_LEAF_DATA_SIZE(root);
2461 for (i = 0; i < right_nritems; i++) {
2462 item = btrfs_item_nr(right, i);
2463 if (!right->map_token) {
2464 map_extent_buffer(right, (unsigned long)item,
2465 sizeof(struct btrfs_item),
2466 &right->map_token, &right->kaddr,
2467 &right->map_start, &right->map_len,
2470 push_space -= btrfs_item_size(right, item);
2471 btrfs_set_item_offset(right, item, push_space);
2474 if (right->map_token) {
2475 unmap_extent_buffer(right, right->map_token, KM_USER1);
2476 right->map_token = NULL;
2478 left_nritems -= push_items;
2479 btrfs_set_header_nritems(left, left_nritems);
2482 btrfs_mark_buffer_dirty(left);
2483 btrfs_mark_buffer_dirty(right);
2485 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2488 btrfs_item_key(right, &disk_key, 0);
2489 btrfs_set_node_key(upper, &disk_key, slot + 1);
2490 btrfs_mark_buffer_dirty(upper);
2492 /* then fixup the leaf pointer in the path */
2493 if (path->slots[0] >= left_nritems) {
2494 path->slots[0] -= left_nritems;
2495 if (btrfs_header_nritems(path->nodes[0]) == 0)
2496 clean_tree_block(trans, root, path->nodes[0]);
2497 btrfs_tree_unlock(path->nodes[0]);
2498 free_extent_buffer(path->nodes[0]);
2499 path->nodes[0] = right;
2500 path->slots[1] += 1;
2502 btrfs_tree_unlock(right);
2503 free_extent_buffer(right);
2508 btrfs_tree_unlock(right);
2509 free_extent_buffer(right);
2514 * push some data in the path leaf to the left, trying to free up at
2515 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2517 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
2518 *root, struct btrfs_path *path, int data_size,
2521 struct btrfs_disk_key disk_key;
2522 struct extent_buffer *right = path->nodes[0];
2523 struct extent_buffer *left;
2529 struct btrfs_item *item;
2530 u32 old_left_nritems;
2536 u32 old_left_item_size;
2538 slot = path->slots[1];
2541 if (!path->nodes[1])
2544 right_nritems = btrfs_header_nritems(right);
2545 if (right_nritems == 0)
2548 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2550 left = read_node_slot(root, path->nodes[1], slot - 1);
2551 btrfs_tree_lock(left);
2552 btrfs_set_lock_blocking(left);
2554 free_space = btrfs_leaf_free_space(root, left);
2555 if (free_space < data_size) {
2560 /* cow and double check */
2561 ret = btrfs_cow_block(trans, root, left,
2562 path->nodes[1], slot - 1, &left, 0);
2564 /* we hit -ENOSPC, but it isn't fatal here */
2569 free_space = btrfs_leaf_free_space(root, left);
2570 if (free_space < data_size) {
2578 nr = right_nritems - 1;
2580 for (i = 0; i < nr; i++) {
2581 item = btrfs_item_nr(right, i);
2582 if (!right->map_token) {
2583 map_extent_buffer(right, (unsigned long)item,
2584 sizeof(struct btrfs_item),
2585 &right->map_token, &right->kaddr,
2586 &right->map_start, &right->map_len,
2590 if (!empty && push_items > 0) {
2591 if (path->slots[0] < i)
2593 if (path->slots[0] == i) {
2594 int space = btrfs_leaf_free_space(root, right);
2595 if (space + push_space * 2 > free_space)
2600 if (path->slots[0] == i)
2601 push_space += data_size;
2603 this_item_size = btrfs_item_size(right, item);
2604 if (this_item_size + sizeof(*item) + push_space > free_space)
2608 push_space += this_item_size + sizeof(*item);
2611 if (right->map_token) {
2612 unmap_extent_buffer(right, right->map_token, KM_USER1);
2613 right->map_token = NULL;
2616 if (push_items == 0) {
2620 if (!empty && push_items == btrfs_header_nritems(right))
2623 /* push data from right to left */
2624 copy_extent_buffer(left, right,
2625 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2626 btrfs_item_nr_offset(0),
2627 push_items * sizeof(struct btrfs_item));
2629 push_space = BTRFS_LEAF_DATA_SIZE(root) -
2630 btrfs_item_offset_nr(right, push_items - 1);
2632 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
2633 leaf_data_end(root, left) - push_space,
2634 btrfs_leaf_data(right) +
2635 btrfs_item_offset_nr(right, push_items - 1),
2637 old_left_nritems = btrfs_header_nritems(left);
2638 BUG_ON(old_left_nritems <= 0);
2640 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
2641 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
2644 item = btrfs_item_nr(left, i);
2645 if (!left->map_token) {
2646 map_extent_buffer(left, (unsigned long)item,
2647 sizeof(struct btrfs_item),
2648 &left->map_token, &left->kaddr,
2649 &left->map_start, &left->map_len,
2653 ioff = btrfs_item_offset(left, item);
2654 btrfs_set_item_offset(left, item,
2655 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
2657 btrfs_set_header_nritems(left, old_left_nritems + push_items);
2658 if (left->map_token) {
2659 unmap_extent_buffer(left, left->map_token, KM_USER1);
2660 left->map_token = NULL;
2663 /* fixup right node */
2664 if (push_items > right_nritems) {
2665 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2670 if (push_items < right_nritems) {
2671 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2672 leaf_data_end(root, right);
2673 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2674 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2675 btrfs_leaf_data(right) +
2676 leaf_data_end(root, right), push_space);
2678 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
2679 btrfs_item_nr_offset(push_items),
2680 (btrfs_header_nritems(right) - push_items) *
2681 sizeof(struct btrfs_item));
2683 right_nritems -= push_items;
2684 btrfs_set_header_nritems(right, right_nritems);
2685 push_space = BTRFS_LEAF_DATA_SIZE(root);
2686 for (i = 0; i < right_nritems; i++) {
2687 item = btrfs_item_nr(right, i);
2689 if (!right->map_token) {
2690 map_extent_buffer(right, (unsigned long)item,
2691 sizeof(struct btrfs_item),
2692 &right->map_token, &right->kaddr,
2693 &right->map_start, &right->map_len,
2697 push_space = push_space - btrfs_item_size(right, item);
2698 btrfs_set_item_offset(right, item, push_space);
2700 if (right->map_token) {
2701 unmap_extent_buffer(right, right->map_token, KM_USER1);
2702 right->map_token = NULL;
2705 btrfs_mark_buffer_dirty(left);
2707 btrfs_mark_buffer_dirty(right);
2709 ret = btrfs_update_ref(trans, root, right, left,
2710 old_left_nritems, push_items);
2713 btrfs_item_key(right, &disk_key, 0);
2714 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
2718 /* then fixup the leaf pointer in the path */
2719 if (path->slots[0] < push_items) {
2720 path->slots[0] += old_left_nritems;
2721 if (btrfs_header_nritems(path->nodes[0]) == 0)
2722 clean_tree_block(trans, root, path->nodes[0]);
2723 btrfs_tree_unlock(path->nodes[0]);
2724 free_extent_buffer(path->nodes[0]);
2725 path->nodes[0] = left;
2726 path->slots[1] -= 1;
2728 btrfs_tree_unlock(left);
2729 free_extent_buffer(left);
2730 path->slots[0] -= push_items;
2732 BUG_ON(path->slots[0] < 0);
2735 btrfs_tree_unlock(left);
2736 free_extent_buffer(left);
2741 * split the path's leaf in two, making sure there is at least data_size
2742 * available for the resulting leaf level of the path.
2744 * returns 0 if all went well and < 0 on failure.
2746 static noinline int split_leaf(struct btrfs_trans_handle *trans,
2747 struct btrfs_root *root,
2748 struct btrfs_key *ins_key,
2749 struct btrfs_path *path, int data_size,
2752 struct extent_buffer *l;
2756 struct extent_buffer *right;
2763 int num_doubles = 0;
2764 struct btrfs_disk_key disk_key;
2766 /* first try to make some room by pushing left and right */
2767 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
2768 wret = push_leaf_right(trans, root, path, data_size, 0);
2772 wret = push_leaf_left(trans, root, path, data_size, 0);
2778 /* did the pushes work? */
2779 if (btrfs_leaf_free_space(root, l) >= data_size)
2783 if (!path->nodes[1]) {
2784 ret = insert_new_root(trans, root, path, 1);
2791 slot = path->slots[0];
2792 nritems = btrfs_header_nritems(l);
2793 mid = (nritems + 1) / 2;
2795 right = btrfs_alloc_free_block(trans, root, root->leafsize,
2796 path->nodes[1]->start,
2797 root->root_key.objectid,
2798 trans->transid, 0, l->start, 0);
2799 if (IS_ERR(right)) {
2801 return PTR_ERR(right);
2804 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2805 btrfs_set_header_bytenr(right, right->start);
2806 btrfs_set_header_generation(right, trans->transid);
2807 btrfs_set_header_owner(right, root->root_key.objectid);
2808 btrfs_set_header_level(right, 0);
2809 write_extent_buffer(right, root->fs_info->fsid,
2810 (unsigned long)btrfs_header_fsid(right),
2813 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2814 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2818 leaf_space_used(l, mid, nritems - mid) + data_size >
2819 BTRFS_LEAF_DATA_SIZE(root)) {
2820 if (slot >= nritems) {
2821 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2822 btrfs_set_header_nritems(right, 0);
2823 wret = insert_ptr(trans, root, path,
2824 &disk_key, right->start,
2825 path->slots[1] + 1, 1);
2829 btrfs_tree_unlock(path->nodes[0]);
2830 free_extent_buffer(path->nodes[0]);
2831 path->nodes[0] = right;
2833 path->slots[1] += 1;
2834 btrfs_mark_buffer_dirty(right);
2838 if (mid != nritems &&
2839 leaf_space_used(l, mid, nritems - mid) +
2840 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2845 if (leaf_space_used(l, 0, mid) + data_size >
2846 BTRFS_LEAF_DATA_SIZE(root)) {
2847 if (!extend && data_size && slot == 0) {
2848 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2849 btrfs_set_header_nritems(right, 0);
2850 wret = insert_ptr(trans, root, path,
2856 btrfs_tree_unlock(path->nodes[0]);
2857 free_extent_buffer(path->nodes[0]);
2858 path->nodes[0] = right;
2860 if (path->slots[1] == 0) {
2861 wret = fixup_low_keys(trans, root,
2862 path, &disk_key, 1);
2866 btrfs_mark_buffer_dirty(right);
2868 } else if ((extend || !data_size) && slot == 0) {
2872 if (mid != nritems &&
2873 leaf_space_used(l, mid, nritems - mid) +
2874 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2880 nritems = nritems - mid;
2881 btrfs_set_header_nritems(right, nritems);
2882 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2884 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2885 btrfs_item_nr_offset(mid),
2886 nritems * sizeof(struct btrfs_item));
2888 copy_extent_buffer(right, l,
2889 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2890 data_copy_size, btrfs_leaf_data(l) +
2891 leaf_data_end(root, l), data_copy_size);
2893 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2894 btrfs_item_end_nr(l, mid);
2896 for (i = 0; i < nritems; i++) {
2897 struct btrfs_item *item = btrfs_item_nr(right, i);
2900 if (!right->map_token) {
2901 map_extent_buffer(right, (unsigned long)item,
2902 sizeof(struct btrfs_item),
2903 &right->map_token, &right->kaddr,
2904 &right->map_start, &right->map_len,
2908 ioff = btrfs_item_offset(right, item);
2909 btrfs_set_item_offset(right, item, ioff + rt_data_off);
2912 if (right->map_token) {
2913 unmap_extent_buffer(right, right->map_token, KM_USER1);
2914 right->map_token = NULL;
2917 btrfs_set_header_nritems(l, mid);
2919 btrfs_item_key(right, &disk_key, 0);
2920 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2921 path->slots[1] + 1, 1);
2925 btrfs_mark_buffer_dirty(right);
2926 btrfs_mark_buffer_dirty(l);
2927 BUG_ON(path->slots[0] != slot);
2929 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2933 btrfs_tree_unlock(path->nodes[0]);
2934 free_extent_buffer(path->nodes[0]);
2935 path->nodes[0] = right;
2936 path->slots[0] -= mid;
2937 path->slots[1] += 1;
2939 btrfs_tree_unlock(right);
2940 free_extent_buffer(right);
2943 BUG_ON(path->slots[0] < 0);
2946 BUG_ON(num_doubles != 0);
2954 * This function splits a single item into two items,
2955 * giving 'new_key' to the new item and splitting the
2956 * old one at split_offset (from the start of the item).
2958 * The path may be released by this operation. After
2959 * the split, the path is pointing to the old item. The
2960 * new item is going to be in the same node as the old one.
2962 * Note, the item being split must be smaller enough to live alone on
2963 * a tree block with room for one extra struct btrfs_item
2965 * This allows us to split the item in place, keeping a lock on the
2966 * leaf the entire time.
2968 int btrfs_split_item(struct btrfs_trans_handle *trans,
2969 struct btrfs_root *root,
2970 struct btrfs_path *path,
2971 struct btrfs_key *new_key,
2972 unsigned long split_offset)
2975 struct extent_buffer *leaf;
2976 struct btrfs_key orig_key;
2977 struct btrfs_item *item;
2978 struct btrfs_item *new_item;
2983 struct btrfs_disk_key disk_key;
2986 leaf = path->nodes[0];
2987 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
2988 if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item))
2991 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2992 btrfs_release_path(root, path);
2994 path->search_for_split = 1;
2995 path->keep_locks = 1;
2997 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
2998 path->search_for_split = 0;
3000 /* if our item isn't there or got smaller, return now */
3001 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
3003 path->keep_locks = 0;
3007 ret = split_leaf(trans, root, &orig_key, path,
3008 sizeof(struct btrfs_item), 1);
3009 path->keep_locks = 0;
3013 * make sure any changes to the path from split_leaf leave it
3014 * in a blocking state
3016 btrfs_set_path_blocking(path);
3018 leaf = path->nodes[0];
3019 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3022 item = btrfs_item_nr(leaf, path->slots[0]);
3023 orig_offset = btrfs_item_offset(leaf, item);
3024 item_size = btrfs_item_size(leaf, item);
3027 buf = kmalloc(item_size, GFP_NOFS);
3028 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3029 path->slots[0]), item_size);
3030 slot = path->slots[0] + 1;
3031 leaf = path->nodes[0];
3033 nritems = btrfs_header_nritems(leaf);
3035 if (slot != nritems) {
3036 /* shift the items */
3037 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3038 btrfs_item_nr_offset(slot),
3039 (nritems - slot) * sizeof(struct btrfs_item));
3043 btrfs_cpu_key_to_disk(&disk_key, new_key);
3044 btrfs_set_item_key(leaf, &disk_key, slot);
3046 new_item = btrfs_item_nr(leaf, slot);
3048 btrfs_set_item_offset(leaf, new_item, orig_offset);
3049 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3051 btrfs_set_item_offset(leaf, item,
3052 orig_offset + item_size - split_offset);
3053 btrfs_set_item_size(leaf, item, split_offset);
3055 btrfs_set_header_nritems(leaf, nritems + 1);
3057 /* write the data for the start of the original item */
3058 write_extent_buffer(leaf, buf,
3059 btrfs_item_ptr_offset(leaf, path->slots[0]),
3062 /* write the data for the new item */
3063 write_extent_buffer(leaf, buf + split_offset,
3064 btrfs_item_ptr_offset(leaf, slot),
3065 item_size - split_offset);
3066 btrfs_mark_buffer_dirty(leaf);
3069 if (btrfs_leaf_free_space(root, leaf) < 0) {
3070 btrfs_print_leaf(root, leaf);
3078 * make the item pointed to by the path smaller. new_size indicates
3079 * how small to make it, and from_end tells us if we just chop bytes
3080 * off the end of the item or if we shift the item to chop bytes off
3083 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3084 struct btrfs_root *root,
3085 struct btrfs_path *path,
3086 u32 new_size, int from_end)
3091 struct extent_buffer *leaf;
3092 struct btrfs_item *item;
3094 unsigned int data_end;
3095 unsigned int old_data_start;
3096 unsigned int old_size;
3097 unsigned int size_diff;
3100 slot_orig = path->slots[0];
3101 leaf = path->nodes[0];
3102 slot = path->slots[0];
3104 old_size = btrfs_item_size_nr(leaf, slot);
3105 if (old_size == new_size)
3108 nritems = btrfs_header_nritems(leaf);
3109 data_end = leaf_data_end(root, leaf);
3111 old_data_start = btrfs_item_offset_nr(leaf, slot);
3113 size_diff = old_size - new_size;
3116 BUG_ON(slot >= nritems);
3119 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3121 /* first correct the data pointers */
3122 for (i = slot; i < nritems; i++) {
3124 item = btrfs_item_nr(leaf, i);
3126 if (!leaf->map_token) {
3127 map_extent_buffer(leaf, (unsigned long)item,
3128 sizeof(struct btrfs_item),
3129 &leaf->map_token, &leaf->kaddr,
3130 &leaf->map_start, &leaf->map_len,
3134 ioff = btrfs_item_offset(leaf, item);
3135 btrfs_set_item_offset(leaf, item, ioff + size_diff);
3138 if (leaf->map_token) {
3139 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3140 leaf->map_token = NULL;
3143 /* shift the data */
3145 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3146 data_end + size_diff, btrfs_leaf_data(leaf) +
3147 data_end, old_data_start + new_size - data_end);
3149 struct btrfs_disk_key disk_key;
3152 btrfs_item_key(leaf, &disk_key, slot);
3154 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3156 struct btrfs_file_extent_item *fi;
3158 fi = btrfs_item_ptr(leaf, slot,
3159 struct btrfs_file_extent_item);
3160 fi = (struct btrfs_file_extent_item *)(
3161 (unsigned long)fi - size_diff);
3163 if (btrfs_file_extent_type(leaf, fi) ==
3164 BTRFS_FILE_EXTENT_INLINE) {
3165 ptr = btrfs_item_ptr_offset(leaf, slot);
3166 memmove_extent_buffer(leaf, ptr,
3168 offsetof(struct btrfs_file_extent_item,
3173 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3174 data_end + size_diff, btrfs_leaf_data(leaf) +
3175 data_end, old_data_start - data_end);
3177 offset = btrfs_disk_key_offset(&disk_key);
3178 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3179 btrfs_set_item_key(leaf, &disk_key, slot);
3181 fixup_low_keys(trans, root, path, &disk_key, 1);
3184 item = btrfs_item_nr(leaf, slot);
3185 btrfs_set_item_size(leaf, item, new_size);
3186 btrfs_mark_buffer_dirty(leaf);
3189 if (btrfs_leaf_free_space(root, leaf) < 0) {
3190 btrfs_print_leaf(root, leaf);
3197 * make the item pointed to by the path bigger, data_size is the new size.
3199 int btrfs_extend_item(struct btrfs_trans_handle *trans,
3200 struct btrfs_root *root, struct btrfs_path *path,
3206 struct extent_buffer *leaf;
3207 struct btrfs_item *item;
3209 unsigned int data_end;
3210 unsigned int old_data;
3211 unsigned int old_size;
3214 slot_orig = path->slots[0];
3215 leaf = path->nodes[0];
3217 nritems = btrfs_header_nritems(leaf);
3218 data_end = leaf_data_end(root, leaf);
3220 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3221 btrfs_print_leaf(root, leaf);
3224 slot = path->slots[0];
3225 old_data = btrfs_item_end_nr(leaf, slot);
3228 if (slot >= nritems) {
3229 btrfs_print_leaf(root, leaf);
3230 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3236 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3238 /* first correct the data pointers */
3239 for (i = slot; i < nritems; i++) {
3241 item = btrfs_item_nr(leaf, i);
3243 if (!leaf->map_token) {
3244 map_extent_buffer(leaf, (unsigned long)item,
3245 sizeof(struct btrfs_item),
3246 &leaf->map_token, &leaf->kaddr,
3247 &leaf->map_start, &leaf->map_len,
3250 ioff = btrfs_item_offset(leaf, item);
3251 btrfs_set_item_offset(leaf, item, ioff - data_size);
3254 if (leaf->map_token) {
3255 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3256 leaf->map_token = NULL;
3259 /* shift the data */
3260 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3261 data_end - data_size, btrfs_leaf_data(leaf) +
3262 data_end, old_data - data_end);
3264 data_end = old_data;
3265 old_size = btrfs_item_size_nr(leaf, slot);
3266 item = btrfs_item_nr(leaf, slot);
3267 btrfs_set_item_size(leaf, item, old_size + data_size);
3268 btrfs_mark_buffer_dirty(leaf);
3271 if (btrfs_leaf_free_space(root, leaf) < 0) {
3272 btrfs_print_leaf(root, leaf);
3279 * Given a key and some data, insert items into the tree.
3280 * This does all the path init required, making room in the tree if needed.
3281 * Returns the number of keys that were inserted.
3283 int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3284 struct btrfs_root *root,
3285 struct btrfs_path *path,
3286 struct btrfs_key *cpu_key, u32 *data_size,
3289 struct extent_buffer *leaf;
3290 struct btrfs_item *item;
3297 unsigned int data_end;
3298 struct btrfs_disk_key disk_key;
3299 struct btrfs_key found_key;
3301 for (i = 0; i < nr; i++) {
3302 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3303 BTRFS_LEAF_DATA_SIZE(root)) {
3307 total_data += data_size[i];
3308 total_size += data_size[i] + sizeof(struct btrfs_item);
3312 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3318 leaf = path->nodes[0];
3320 nritems = btrfs_header_nritems(leaf);
3321 data_end = leaf_data_end(root, leaf);
3323 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3324 for (i = nr; i >= 0; i--) {
3325 total_data -= data_size[i];
3326 total_size -= data_size[i] + sizeof(struct btrfs_item);
3327 if (total_size < btrfs_leaf_free_space(root, leaf))
3333 slot = path->slots[0];
3336 if (slot != nritems) {
3337 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3339 item = btrfs_item_nr(leaf, slot);
3340 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3342 /* figure out how many keys we can insert in here */
3343 total_data = data_size[0];
3344 for (i = 1; i < nr; i++) {
3345 if (comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3347 total_data += data_size[i];
3351 if (old_data < data_end) {
3352 btrfs_print_leaf(root, leaf);
3353 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3354 slot, old_data, data_end);
3358 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3360 /* first correct the data pointers */
3361 WARN_ON(leaf->map_token);
3362 for (i = slot; i < nritems; i++) {
3365 item = btrfs_item_nr(leaf, i);
3366 if (!leaf->map_token) {
3367 map_extent_buffer(leaf, (unsigned long)item,
3368 sizeof(struct btrfs_item),
3369 &leaf->map_token, &leaf->kaddr,
3370 &leaf->map_start, &leaf->map_len,
3374 ioff = btrfs_item_offset(leaf, item);
3375 btrfs_set_item_offset(leaf, item, ioff - total_data);
3377 if (leaf->map_token) {
3378 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3379 leaf->map_token = NULL;
3382 /* shift the items */
3383 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3384 btrfs_item_nr_offset(slot),
3385 (nritems - slot) * sizeof(struct btrfs_item));
3387 /* shift the data */
3388 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3389 data_end - total_data, btrfs_leaf_data(leaf) +
3390 data_end, old_data - data_end);
3391 data_end = old_data;
3394 * this sucks but it has to be done, if we are inserting at
3395 * the end of the leaf only insert 1 of the items, since we
3396 * have no way of knowing whats on the next leaf and we'd have
3397 * to drop our current locks to figure it out
3402 /* setup the item for the new data */
3403 for (i = 0; i < nr; i++) {
3404 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3405 btrfs_set_item_key(leaf, &disk_key, slot + i);
3406 item = btrfs_item_nr(leaf, slot + i);
3407 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3408 data_end -= data_size[i];
3409 btrfs_set_item_size(leaf, item, data_size[i]);
3411 btrfs_set_header_nritems(leaf, nritems + nr);
3412 btrfs_mark_buffer_dirty(leaf);
3416 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3417 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3420 if (btrfs_leaf_free_space(root, leaf) < 0) {
3421 btrfs_print_leaf(root, leaf);
3431 * Given a key and some data, insert items into the tree.
3432 * This does all the path init required, making room in the tree if needed.
3434 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3435 struct btrfs_root *root,
3436 struct btrfs_path *path,
3437 struct btrfs_key *cpu_key, u32 *data_size,
3440 struct extent_buffer *leaf;
3441 struct btrfs_item *item;
3449 unsigned int data_end;
3450 struct btrfs_disk_key disk_key;
3452 for (i = 0; i < nr; i++)
3453 total_data += data_size[i];
3455 total_size = total_data + (nr * sizeof(struct btrfs_item));
3456 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3462 slot_orig = path->slots[0];
3463 leaf = path->nodes[0];
3465 nritems = btrfs_header_nritems(leaf);
3466 data_end = leaf_data_end(root, leaf);
3468 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3469 btrfs_print_leaf(root, leaf);
3470 printk(KERN_CRIT "not enough freespace need %u have %d\n",
3471 total_size, btrfs_leaf_free_space(root, leaf));
3475 slot = path->slots[0];
3478 if (slot != nritems) {
3479 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3481 if (old_data < data_end) {
3482 btrfs_print_leaf(root, leaf);
3483 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3484 slot, old_data, data_end);
3488 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3490 /* first correct the data pointers */
3491 WARN_ON(leaf->map_token);
3492 for (i = slot; i < nritems; i++) {
3495 item = btrfs_item_nr(leaf, i);
3496 if (!leaf->map_token) {
3497 map_extent_buffer(leaf, (unsigned long)item,
3498 sizeof(struct btrfs_item),
3499 &leaf->map_token, &leaf->kaddr,
3500 &leaf->map_start, &leaf->map_len,
3504 ioff = btrfs_item_offset(leaf, item);
3505 btrfs_set_item_offset(leaf, item, ioff - total_data);
3507 if (leaf->map_token) {
3508 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3509 leaf->map_token = NULL;
3512 /* shift the items */
3513 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3514 btrfs_item_nr_offset(slot),
3515 (nritems - slot) * sizeof(struct btrfs_item));
3517 /* shift the data */
3518 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3519 data_end - total_data, btrfs_leaf_data(leaf) +
3520 data_end, old_data - data_end);
3521 data_end = old_data;
3524 /* setup the item for the new data */
3525 for (i = 0; i < nr; i++) {
3526 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3527 btrfs_set_item_key(leaf, &disk_key, slot + i);
3528 item = btrfs_item_nr(leaf, slot + i);
3529 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3530 data_end -= data_size[i];
3531 btrfs_set_item_size(leaf, item, data_size[i]);
3533 btrfs_set_header_nritems(leaf, nritems + nr);
3534 btrfs_mark_buffer_dirty(leaf);
3538 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3539 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3542 if (btrfs_leaf_free_space(root, leaf) < 0) {
3543 btrfs_print_leaf(root, leaf);
3547 btrfs_unlock_up_safe(path, 1);
3552 * Given a key and some data, insert an item into the tree.
3553 * This does all the path init required, making room in the tree if needed.
3555 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3556 *root, struct btrfs_key *cpu_key, void *data, u32
3560 struct btrfs_path *path;
3561 struct extent_buffer *leaf;
3564 path = btrfs_alloc_path();
3566 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
3568 leaf = path->nodes[0];
3569 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3570 write_extent_buffer(leaf, data, ptr, data_size);
3571 btrfs_mark_buffer_dirty(leaf);
3573 btrfs_free_path(path);
3578 * delete the pointer from a given node.
3580 * the tree should have been previously balanced so the deletion does not
3583 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3584 struct btrfs_path *path, int level, int slot)
3586 struct extent_buffer *parent = path->nodes[level];
3591 nritems = btrfs_header_nritems(parent);
3592 if (slot != nritems - 1) {
3593 memmove_extent_buffer(parent,
3594 btrfs_node_key_ptr_offset(slot),
3595 btrfs_node_key_ptr_offset(slot + 1),
3596 sizeof(struct btrfs_key_ptr) *
3597 (nritems - slot - 1));
3600 btrfs_set_header_nritems(parent, nritems);
3601 if (nritems == 0 && parent == root->node) {
3602 BUG_ON(btrfs_header_level(root->node) != 1);
3603 /* just turn the root into a leaf and break */
3604 btrfs_set_header_level(root->node, 0);
3605 } else if (slot == 0) {
3606 struct btrfs_disk_key disk_key;
3608 btrfs_node_key(parent, &disk_key, 0);
3609 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
3613 btrfs_mark_buffer_dirty(parent);
3618 * a helper function to delete the leaf pointed to by path->slots[1] and
3619 * path->nodes[1]. bytenr is the node block pointer, but since the callers
3620 * already know it, it is faster to have them pass it down than to
3621 * read it out of the node again.
3623 * This deletes the pointer in path->nodes[1] and frees the leaf
3624 * block extent. zero is returned if it all worked out, < 0 otherwise.
3626 * The path must have already been setup for deleting the leaf, including
3627 * all the proper balancing. path->nodes[1] must be locked.
3629 noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3630 struct btrfs_root *root,
3631 struct btrfs_path *path, u64 bytenr)
3634 u64 root_gen = btrfs_header_generation(path->nodes[1]);
3635 u64 parent_start = path->nodes[1]->start;
3636 u64 parent_owner = btrfs_header_owner(path->nodes[1]);
3638 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3643 * btrfs_free_extent is expensive, we want to make sure we
3644 * aren't holding any locks when we call it
3646 btrfs_unlock_up_safe(path, 0);
3648 ret = btrfs_free_extent(trans, root, bytenr,
3649 btrfs_level_size(root, 0),
3650 parent_start, parent_owner,
3655 * delete the item at the leaf level in path. If that empties
3656 * the leaf, remove it from the tree
3658 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3659 struct btrfs_path *path, int slot, int nr)
3661 struct extent_buffer *leaf;
3662 struct btrfs_item *item;
3670 leaf = path->nodes[0];
3671 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3673 for (i = 0; i < nr; i++)
3674 dsize += btrfs_item_size_nr(leaf, slot + i);
3676 nritems = btrfs_header_nritems(leaf);
3678 if (slot + nr != nritems) {
3679 int data_end = leaf_data_end(root, leaf);
3681 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3683 btrfs_leaf_data(leaf) + data_end,
3684 last_off - data_end);
3686 for (i = slot + nr; i < nritems; i++) {
3689 item = btrfs_item_nr(leaf, i);
3690 if (!leaf->map_token) {
3691 map_extent_buffer(leaf, (unsigned long)item,
3692 sizeof(struct btrfs_item),
3693 &leaf->map_token, &leaf->kaddr,
3694 &leaf->map_start, &leaf->map_len,
3697 ioff = btrfs_item_offset(leaf, item);
3698 btrfs_set_item_offset(leaf, item, ioff + dsize);
3701 if (leaf->map_token) {
3702 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3703 leaf->map_token = NULL;
3706 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
3707 btrfs_item_nr_offset(slot + nr),
3708 sizeof(struct btrfs_item) *
3709 (nritems - slot - nr));
3711 btrfs_set_header_nritems(leaf, nritems - nr);
3714 /* delete the leaf if we've emptied it */
3716 if (leaf == root->node) {
3717 btrfs_set_header_level(leaf, 0);
3719 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3723 int used = leaf_space_used(leaf, 0, nritems);
3725 struct btrfs_disk_key disk_key;
3727 btrfs_item_key(leaf, &disk_key, 0);
3728 wret = fixup_low_keys(trans, root, path,
3734 /* delete the leaf if it is mostly empty */
3735 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
3736 /* push_leaf_left fixes the path.
3737 * make sure the path still points to our leaf
3738 * for possible call to del_ptr below
3740 slot = path->slots[1];
3741 extent_buffer_get(leaf);
3743 wret = push_leaf_left(trans, root, path, 1, 1);
3744 if (wret < 0 && wret != -ENOSPC)
3747 if (path->nodes[0] == leaf &&
3748 btrfs_header_nritems(leaf)) {
3749 wret = push_leaf_right(trans, root, path, 1, 1);
3750 if (wret < 0 && wret != -ENOSPC)
3754 if (btrfs_header_nritems(leaf) == 0) {
3755 path->slots[1] = slot;
3756 ret = btrfs_del_leaf(trans, root, path,
3759 free_extent_buffer(leaf);
3761 /* if we're still in the path, make sure
3762 * we're dirty. Otherwise, one of the
3763 * push_leaf functions must have already
3764 * dirtied this buffer
3766 if (path->nodes[0] == leaf)
3767 btrfs_mark_buffer_dirty(leaf);
3768 free_extent_buffer(leaf);
3771 btrfs_mark_buffer_dirty(leaf);
3778 * search the tree again to find a leaf with lesser keys
3779 * returns 0 if it found something or 1 if there are no lesser leaves.
3780 * returns < 0 on io errors.
3782 * This may release the path, and so you may lose any locks held at the
3785 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3787 struct btrfs_key key;
3788 struct btrfs_disk_key found_key;
3791 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
3795 else if (key.type > 0)
3797 else if (key.objectid > 0)
3802 btrfs_release_path(root, path);
3803 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3806 btrfs_item_key(path->nodes[0], &found_key, 0);
3807 ret = comp_keys(&found_key, &key);
3814 * A helper function to walk down the tree starting at min_key, and looking
3815 * for nodes or leaves that are either in cache or have a minimum
3816 * transaction id. This is used by the btree defrag code, and tree logging
3818 * This does not cow, but it does stuff the starting key it finds back
3819 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3820 * key and get a writable path.
3822 * This does lock as it descends, and path->keep_locks should be set
3823 * to 1 by the caller.
3825 * This honors path->lowest_level to prevent descent past a given level
3828 * min_trans indicates the oldest transaction that you are interested
3829 * in walking through. Any nodes or leaves older than min_trans are
3830 * skipped over (without reading them).
3832 * returns zero if something useful was found, < 0 on error and 1 if there
3833 * was nothing in the tree that matched the search criteria.
3835 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
3836 struct btrfs_key *max_key,
3837 struct btrfs_path *path, int cache_only,
3840 struct extent_buffer *cur;
3841 struct btrfs_key found_key;
3848 WARN_ON(!path->keep_locks);
3850 cur = btrfs_lock_root_node(root);
3851 level = btrfs_header_level(cur);
3852 WARN_ON(path->nodes[level]);
3853 path->nodes[level] = cur;
3854 path->locks[level] = 1;
3856 if (btrfs_header_generation(cur) < min_trans) {
3861 nritems = btrfs_header_nritems(cur);
3862 level = btrfs_header_level(cur);
3863 sret = bin_search(cur, min_key, level, &slot);
3865 /* at the lowest level, we're done, setup the path and exit */
3866 if (level == path->lowest_level) {
3867 if (slot >= nritems)
3870 path->slots[level] = slot;
3871 btrfs_item_key_to_cpu(cur, &found_key, slot);
3874 if (sret && slot > 0)
3877 * check this node pointer against the cache_only and
3878 * min_trans parameters. If it isn't in cache or is too
3879 * old, skip to the next one.
3881 while (slot < nritems) {
3884 struct extent_buffer *tmp;
3885 struct btrfs_disk_key disk_key;
3887 blockptr = btrfs_node_blockptr(cur, slot);
3888 gen = btrfs_node_ptr_generation(cur, slot);
3889 if (gen < min_trans) {
3897 btrfs_node_key(cur, &disk_key, slot);
3898 if (comp_keys(&disk_key, max_key) >= 0) {
3904 tmp = btrfs_find_tree_block(root, blockptr,
3905 btrfs_level_size(root, level - 1));
3907 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3908 free_extent_buffer(tmp);
3912 free_extent_buffer(tmp);
3917 * we didn't find a candidate key in this node, walk forward
3918 * and find another one
3920 if (slot >= nritems) {
3921 path->slots[level] = slot;
3922 btrfs_set_path_blocking(path);
3923 sret = btrfs_find_next_key(root, path, min_key, level,
3924 cache_only, min_trans);
3926 btrfs_release_path(root, path);
3929 btrfs_clear_path_blocking(path);
3933 /* save our key for returning back */
3934 btrfs_node_key_to_cpu(cur, &found_key, slot);
3935 path->slots[level] = slot;
3936 if (level == path->lowest_level) {
3938 unlock_up(path, level, 1);
3941 btrfs_set_path_blocking(path);
3942 cur = read_node_slot(root, cur, slot);
3944 btrfs_tree_lock(cur);
3946 path->locks[level - 1] = 1;
3947 path->nodes[level - 1] = cur;
3948 unlock_up(path, level, 1);
3949 btrfs_clear_path_blocking(path);
3953 memcpy(min_key, &found_key, sizeof(found_key));
3954 btrfs_set_path_blocking(path);
3959 * this is similar to btrfs_next_leaf, but does not try to preserve
3960 * and fixup the path. It looks for and returns the next key in the
3961 * tree based on the current path and the cache_only and min_trans
3964 * 0 is returned if another key is found, < 0 if there are any errors
3965 * and 1 is returned if there are no higher keys in the tree
3967 * path->keep_locks should be set to 1 on the search made before
3968 * calling this function.
3970 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
3971 struct btrfs_key *key, int lowest_level,
3972 int cache_only, u64 min_trans)
3974 int level = lowest_level;
3976 struct extent_buffer *c;
3978 WARN_ON(!path->keep_locks);
3979 while (level < BTRFS_MAX_LEVEL) {
3980 if (!path->nodes[level])
3983 slot = path->slots[level] + 1;
3984 c = path->nodes[level];
3986 if (slot >= btrfs_header_nritems(c)) {
3988 if (level == BTRFS_MAX_LEVEL)
3993 btrfs_item_key_to_cpu(c, key, slot);
3995 u64 blockptr = btrfs_node_blockptr(c, slot);
3996 u64 gen = btrfs_node_ptr_generation(c, slot);
3999 struct extent_buffer *cur;
4000 cur = btrfs_find_tree_block(root, blockptr,
4001 btrfs_level_size(root, level - 1));
4002 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
4005 free_extent_buffer(cur);
4008 free_extent_buffer(cur);
4010 if (gen < min_trans) {
4014 btrfs_node_key_to_cpu(c, key, slot);
4022 * search the tree again to find a leaf with greater keys
4023 * returns 0 if it found something or 1 if there are no greater leaves.
4024 * returns < 0 on io errors.
4026 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
4030 struct extent_buffer *c;
4031 struct extent_buffer *next = NULL;
4032 struct btrfs_key key;
4036 nritems = btrfs_header_nritems(path->nodes[0]);
4040 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4042 btrfs_release_path(root, path);
4043 path->keep_locks = 1;
4044 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4045 path->keep_locks = 0;
4050 btrfs_set_path_blocking(path);
4051 nritems = btrfs_header_nritems(path->nodes[0]);
4053 * by releasing the path above we dropped all our locks. A balance
4054 * could have added more items next to the key that used to be
4055 * at the very end of the block. So, check again here and
4056 * advance the path if there are now more items available.
4058 if (nritems > 0 && path->slots[0] < nritems - 1) {
4063 while (level < BTRFS_MAX_LEVEL) {
4064 if (!path->nodes[level])
4067 slot = path->slots[level] + 1;
4068 c = path->nodes[level];
4069 if (slot >= btrfs_header_nritems(c)) {
4071 if (level == BTRFS_MAX_LEVEL)
4077 btrfs_tree_unlock(next);
4078 free_extent_buffer(next);
4081 /* the path was set to blocking above */
4082 if (level == 1 && (path->locks[1] || path->skip_locking) &&
4084 reada_for_search(root, path, level, slot, 0);
4086 next = read_node_slot(root, c, slot);
4087 if (!path->skip_locking) {
4088 WARN_ON(!btrfs_tree_locked(c));
4089 btrfs_tree_lock(next);
4090 btrfs_set_lock_blocking(next);
4094 path->slots[level] = slot;
4097 c = path->nodes[level];
4098 if (path->locks[level])
4099 btrfs_tree_unlock(c);
4100 free_extent_buffer(c);
4101 path->nodes[level] = next;
4102 path->slots[level] = 0;
4103 if (!path->skip_locking)
4104 path->locks[level] = 1;
4108 btrfs_set_path_blocking(path);
4109 if (level == 1 && path->locks[1] && path->reada)
4110 reada_for_search(root, path, level, slot, 0);
4111 next = read_node_slot(root, next, 0);
4112 if (!path->skip_locking) {
4113 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
4114 btrfs_tree_lock(next);
4115 btrfs_set_lock_blocking(next);
4119 unlock_up(path, 0, 1);
4124 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4125 * searching until it gets past min_objectid or finds an item of 'type'
4127 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4129 int btrfs_previous_item(struct btrfs_root *root,
4130 struct btrfs_path *path, u64 min_objectid,
4133 struct btrfs_key found_key;
4134 struct extent_buffer *leaf;
4139 if (path->slots[0] == 0) {
4140 btrfs_set_path_blocking(path);
4141 ret = btrfs_prev_leaf(root, path);
4147 leaf = path->nodes[0];
4148 nritems = btrfs_header_nritems(leaf);
4151 if (path->slots[0] == nritems)
4154 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4155 if (found_key.type == type)
4157 if (found_key.objectid < min_objectid)
4159 if (found_key.objectid == min_objectid &&
4160 found_key.type < type)