2 * Copyright (C) 2007,2008 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
22 #include "transaction.h"
23 #include "print-tree.h"
26 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
27 *root, struct btrfs_path *path, int level);
28 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
29 *root, struct btrfs_key *ins_key,
30 struct btrfs_path *path, int data_size, int extend);
31 static int push_node_left(struct btrfs_trans_handle *trans,
32 struct btrfs_root *root, struct extent_buffer *dst,
33 struct extent_buffer *src, int empty);
34 static int balance_node_right(struct btrfs_trans_handle *trans,
35 struct btrfs_root *root,
36 struct extent_buffer *dst_buf,
37 struct extent_buffer *src_buf);
38 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
39 struct btrfs_path *path, int level, int slot);
41 inline void btrfs_init_path(struct btrfs_path *p)
43 memset(p, 0, sizeof(*p));
46 struct btrfs_path *btrfs_alloc_path(void)
48 struct btrfs_path *path;
49 path = kmem_cache_alloc(btrfs_path_cachep, GFP_NOFS);
51 btrfs_init_path(path);
57 /* this also releases the path */
58 void btrfs_free_path(struct btrfs_path *p)
60 btrfs_release_path(NULL, p);
61 kmem_cache_free(btrfs_path_cachep, p);
65 * path release drops references on the extent buffers in the path
66 * and it drops any locks held by this path
68 * It is safe to call this on paths that no locks or extent buffers held.
70 noinline void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
74 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
79 btrfs_tree_unlock(p->nodes[i]);
82 free_extent_buffer(p->nodes[i]);
88 * safely gets a reference on the root node of a tree. A lock
89 * is not taken, so a concurrent writer may put a different node
90 * at the root of the tree. See btrfs_lock_root_node for the
93 * The extent buffer returned by this has a reference taken, so
94 * it won't disappear. It may stop being the root of the tree
95 * at any time because there are no locks held.
97 struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
99 struct extent_buffer *eb;
100 spin_lock(&root->node_lock);
102 extent_buffer_get(eb);
103 spin_unlock(&root->node_lock);
107 /* loop around taking references on and locking the root node of the
108 * tree until you end up with a lock on the root. A locked buffer
109 * is returned, with a reference held.
111 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
113 struct extent_buffer *eb;
116 eb = btrfs_root_node(root);
119 spin_lock(&root->node_lock);
120 if (eb == root->node) {
121 spin_unlock(&root->node_lock);
124 spin_unlock(&root->node_lock);
126 btrfs_tree_unlock(eb);
127 free_extent_buffer(eb);
132 /* cowonly root (everything not a reference counted cow subvolume), just get
133 * put onto a simple dirty list. transaction.c walks this to make sure they
134 * get properly updated on disk.
136 static void add_root_to_dirty_list(struct btrfs_root *root)
138 if (root->track_dirty && list_empty(&root->dirty_list)) {
139 list_add(&root->dirty_list,
140 &root->fs_info->dirty_cowonly_roots);
145 * used by snapshot creation to make a copy of a root for a tree with
146 * a given objectid. The buffer with the new root node is returned in
147 * cow_ret, and this func returns zero on success or a negative error code.
149 int btrfs_copy_root(struct btrfs_trans_handle *trans,
150 struct btrfs_root *root,
151 struct extent_buffer *buf,
152 struct extent_buffer **cow_ret, u64 new_root_objectid)
154 struct extent_buffer *cow;
158 struct btrfs_root *new_root;
160 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
164 memcpy(new_root, root, sizeof(*new_root));
165 new_root->root_key.objectid = new_root_objectid;
167 WARN_ON(root->ref_cows && trans->transid !=
168 root->fs_info->running_transaction->transid);
169 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
171 level = btrfs_header_level(buf);
172 nritems = btrfs_header_nritems(buf);
174 cow = btrfs_alloc_free_block(trans, new_root, buf->len, 0,
175 new_root_objectid, trans->transid,
176 level, buf->start, 0);
182 copy_extent_buffer(cow, buf, 0, 0, cow->len);
183 btrfs_set_header_bytenr(cow, cow->start);
184 btrfs_set_header_generation(cow, trans->transid);
185 btrfs_set_header_owner(cow, new_root_objectid);
186 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
188 write_extent_buffer(cow, root->fs_info->fsid,
189 (unsigned long)btrfs_header_fsid(cow),
192 WARN_ON(btrfs_header_generation(buf) > trans->transid);
193 ret = btrfs_inc_ref(trans, new_root, buf, cow, NULL);
199 btrfs_mark_buffer_dirty(cow);
205 * does the dirty work in cow of a single block. The parent block (if
206 * supplied) is updated to point to the new cow copy. The new buffer is marked
207 * dirty and returned locked. If you modify the block it needs to be marked
210 * search_start -- an allocation hint for the new block
212 * empty_size -- a hint that you plan on doing more cow. This is the size in
213 * bytes the allocator should try to find free next to the block it returns.
214 * This is just a hint and may be ignored by the allocator.
216 * prealloc_dest -- if you have already reserved a destination for the cow,
217 * this uses that block instead of allocating a new one.
218 * btrfs_alloc_reserved_extent is used to finish the allocation.
220 static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
221 struct btrfs_root *root,
222 struct extent_buffer *buf,
223 struct extent_buffer *parent, int parent_slot,
224 struct extent_buffer **cow_ret,
225 u64 search_start, u64 empty_size,
229 struct extent_buffer *cow;
238 WARN_ON(!btrfs_tree_locked(buf));
241 parent_start = parent->start;
245 WARN_ON(root->ref_cows && trans->transid !=
246 root->fs_info->running_transaction->transid);
247 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
249 level = btrfs_header_level(buf);
250 nritems = btrfs_header_nritems(buf);
253 struct btrfs_key ins;
255 ins.objectid = prealloc_dest;
256 ins.offset = buf->len;
257 ins.type = BTRFS_EXTENT_ITEM_KEY;
259 ret = btrfs_alloc_reserved_extent(trans, root, parent_start,
260 root->root_key.objectid,
261 trans->transid, level, &ins);
263 cow = btrfs_init_new_buffer(trans, root, prealloc_dest,
266 cow = btrfs_alloc_free_block(trans, root, buf->len,
268 root->root_key.objectid,
269 trans->transid, level,
270 search_start, empty_size);
275 copy_extent_buffer(cow, buf, 0, 0, cow->len);
276 btrfs_set_header_bytenr(cow, cow->start);
277 btrfs_set_header_generation(cow, trans->transid);
278 btrfs_set_header_owner(cow, root->root_key.objectid);
279 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
281 write_extent_buffer(cow, root->fs_info->fsid,
282 (unsigned long)btrfs_header_fsid(cow),
285 WARN_ON(btrfs_header_generation(buf) > trans->transid);
286 if (btrfs_header_generation(buf) != trans->transid) {
288 ret = btrfs_inc_ref(trans, root, buf, cow, &nr_extents);
292 ret = btrfs_cache_ref(trans, root, buf, nr_extents);
294 } else if (btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID) {
296 * There are only two places that can drop reference to
297 * tree blocks owned by living reloc trees, one is here,
298 * the other place is btrfs_drop_subtree. In both places,
299 * we check reference count while tree block is locked.
300 * Furthermore, if reference count is one, it won't get
301 * increased by someone else.
304 ret = btrfs_lookup_extent_ref(trans, root, buf->start,
308 ret = btrfs_update_ref(trans, root, buf, cow,
310 clean_tree_block(trans, root, buf);
312 ret = btrfs_inc_ref(trans, root, buf, cow, NULL);
316 ret = btrfs_update_ref(trans, root, buf, cow, 0, nritems);
319 clean_tree_block(trans, root, buf);
322 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
323 ret = btrfs_reloc_tree_cache_ref(trans, root, cow, buf->start);
327 if (buf == root->node) {
328 WARN_ON(parent && parent != buf);
330 spin_lock(&root->node_lock);
332 extent_buffer_get(cow);
333 spin_unlock(&root->node_lock);
335 if (buf != root->commit_root) {
336 btrfs_free_extent(trans, root, buf->start,
337 buf->len, buf->start,
338 root->root_key.objectid,
339 btrfs_header_generation(buf),
342 free_extent_buffer(buf);
343 add_root_to_dirty_list(root);
345 btrfs_set_node_blockptr(parent, parent_slot,
347 WARN_ON(trans->transid == 0);
348 btrfs_set_node_ptr_generation(parent, parent_slot,
350 btrfs_mark_buffer_dirty(parent);
351 WARN_ON(btrfs_header_generation(parent) != trans->transid);
352 btrfs_free_extent(trans, root, buf->start, buf->len,
353 parent_start, btrfs_header_owner(parent),
354 btrfs_header_generation(parent), level, 1);
357 btrfs_tree_unlock(buf);
358 free_extent_buffer(buf);
359 btrfs_mark_buffer_dirty(cow);
365 * cows a single block, see __btrfs_cow_block for the real work.
366 * This version of it has extra checks so that a block isn't cow'd more than
367 * once per transaction, as long as it hasn't been written yet
369 noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
370 struct btrfs_root *root, struct extent_buffer *buf,
371 struct extent_buffer *parent, int parent_slot,
372 struct extent_buffer **cow_ret, u64 prealloc_dest)
377 if (trans->transaction != root->fs_info->running_transaction) {
378 printk(KERN_CRIT "trans %llu running %llu\n",
379 (unsigned long long)trans->transid,
381 root->fs_info->running_transaction->transid);
384 if (trans->transid != root->fs_info->generation) {
385 printk(KERN_CRIT "trans %llu running %llu\n",
386 (unsigned long long)trans->transid,
387 (unsigned long long)root->fs_info->generation);
391 if (btrfs_header_generation(buf) == trans->transid &&
392 btrfs_header_owner(buf) == root->root_key.objectid &&
393 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
395 WARN_ON(prealloc_dest);
399 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
400 ret = __btrfs_cow_block(trans, root, buf, parent,
401 parent_slot, cow_ret, search_start, 0,
407 * helper function for defrag to decide if two blocks pointed to by a
408 * node are actually close by
410 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
412 if (blocknr < other && other - (blocknr + blocksize) < 32768)
414 if (blocknr > other && blocknr - (other + blocksize) < 32768)
420 * compare two keys in a memcmp fashion
422 static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
426 btrfs_disk_key_to_cpu(&k1, disk);
428 if (k1.objectid > k2->objectid)
430 if (k1.objectid < k2->objectid)
432 if (k1.type > k2->type)
434 if (k1.type < k2->type)
436 if (k1.offset > k2->offset)
438 if (k1.offset < k2->offset)
444 * same as comp_keys only with two btrfs_key's
446 static int comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
448 if (k1->objectid > k2->objectid)
450 if (k1->objectid < k2->objectid)
452 if (k1->type > k2->type)
454 if (k1->type < k2->type)
456 if (k1->offset > k2->offset)
458 if (k1->offset < k2->offset)
464 * this is used by the defrag code to go through all the
465 * leaves pointed to by a node and reallocate them so that
466 * disk order is close to key order
468 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
469 struct btrfs_root *root, struct extent_buffer *parent,
470 int start_slot, int cache_only, u64 *last_ret,
471 struct btrfs_key *progress)
473 struct extent_buffer *cur;
476 u64 search_start = *last_ret;
486 int progress_passed = 0;
487 struct btrfs_disk_key disk_key;
489 parent_level = btrfs_header_level(parent);
490 if (cache_only && parent_level != 1)
493 if (trans->transaction != root->fs_info->running_transaction)
495 if (trans->transid != root->fs_info->generation)
498 parent_nritems = btrfs_header_nritems(parent);
499 blocksize = btrfs_level_size(root, parent_level - 1);
500 end_slot = parent_nritems;
502 if (parent_nritems == 1)
505 for (i = start_slot; i < end_slot; i++) {
508 if (!parent->map_token) {
509 map_extent_buffer(parent,
510 btrfs_node_key_ptr_offset(i),
511 sizeof(struct btrfs_key_ptr),
512 &parent->map_token, &parent->kaddr,
513 &parent->map_start, &parent->map_len,
516 btrfs_node_key(parent, &disk_key, i);
517 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
521 blocknr = btrfs_node_blockptr(parent, i);
522 gen = btrfs_node_ptr_generation(parent, i);
524 last_block = blocknr;
527 other = btrfs_node_blockptr(parent, i - 1);
528 close = close_blocks(blocknr, other, blocksize);
530 if (!close && i < end_slot - 2) {
531 other = btrfs_node_blockptr(parent, i + 1);
532 close = close_blocks(blocknr, other, blocksize);
535 last_block = blocknr;
538 if (parent->map_token) {
539 unmap_extent_buffer(parent, parent->map_token,
541 parent->map_token = NULL;
544 cur = btrfs_find_tree_block(root, blocknr, blocksize);
546 uptodate = btrfs_buffer_uptodate(cur, gen);
549 if (!cur || !uptodate) {
551 free_extent_buffer(cur);
555 cur = read_tree_block(root, blocknr,
557 } else if (!uptodate) {
558 btrfs_read_buffer(cur, gen);
561 if (search_start == 0)
562 search_start = last_block;
564 btrfs_tree_lock(cur);
565 err = __btrfs_cow_block(trans, root, cur, parent, i,
568 (end_slot - i) * blocksize), 0);
570 btrfs_tree_unlock(cur);
571 free_extent_buffer(cur);
574 search_start = cur->start;
575 last_block = cur->start;
576 *last_ret = search_start;
577 btrfs_tree_unlock(cur);
578 free_extent_buffer(cur);
580 if (parent->map_token) {
581 unmap_extent_buffer(parent, parent->map_token,
583 parent->map_token = NULL;
589 * The leaf data grows from end-to-front in the node.
590 * this returns the address of the start of the last item,
591 * which is the stop of the leaf data stack
593 static inline unsigned int leaf_data_end(struct btrfs_root *root,
594 struct extent_buffer *leaf)
596 u32 nr = btrfs_header_nritems(leaf);
598 return BTRFS_LEAF_DATA_SIZE(root);
599 return btrfs_item_offset_nr(leaf, nr - 1);
603 * extra debugging checks to make sure all the items in a key are
604 * well formed and in the proper order
606 static int check_node(struct btrfs_root *root, struct btrfs_path *path,
609 struct extent_buffer *parent = NULL;
610 struct extent_buffer *node = path->nodes[level];
611 struct btrfs_disk_key parent_key;
612 struct btrfs_disk_key node_key;
615 struct btrfs_key cpukey;
616 u32 nritems = btrfs_header_nritems(node);
618 if (path->nodes[level + 1])
619 parent = path->nodes[level + 1];
621 slot = path->slots[level];
622 BUG_ON(nritems == 0);
624 parent_slot = path->slots[level + 1];
625 btrfs_node_key(parent, &parent_key, parent_slot);
626 btrfs_node_key(node, &node_key, 0);
627 BUG_ON(memcmp(&parent_key, &node_key,
628 sizeof(struct btrfs_disk_key)));
629 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
630 btrfs_header_bytenr(node));
632 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
634 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
635 btrfs_node_key(node, &node_key, slot);
636 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
638 if (slot < nritems - 1) {
639 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
640 btrfs_node_key(node, &node_key, slot);
641 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
647 * extra checking to make sure all the items in a leaf are
648 * well formed and in the proper order
650 static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
653 struct extent_buffer *leaf = path->nodes[level];
654 struct extent_buffer *parent = NULL;
656 struct btrfs_key cpukey;
657 struct btrfs_disk_key parent_key;
658 struct btrfs_disk_key leaf_key;
659 int slot = path->slots[0];
661 u32 nritems = btrfs_header_nritems(leaf);
663 if (path->nodes[level + 1])
664 parent = path->nodes[level + 1];
670 parent_slot = path->slots[level + 1];
671 btrfs_node_key(parent, &parent_key, parent_slot);
672 btrfs_item_key(leaf, &leaf_key, 0);
674 BUG_ON(memcmp(&parent_key, &leaf_key,
675 sizeof(struct btrfs_disk_key)));
676 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
677 btrfs_header_bytenr(leaf));
679 if (slot != 0 && slot < nritems - 1) {
680 btrfs_item_key(leaf, &leaf_key, slot);
681 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
682 if (comp_keys(&leaf_key, &cpukey) <= 0) {
683 btrfs_print_leaf(root, leaf);
684 printk(KERN_CRIT "slot %d offset bad key\n", slot);
687 if (btrfs_item_offset_nr(leaf, slot - 1) !=
688 btrfs_item_end_nr(leaf, slot)) {
689 btrfs_print_leaf(root, leaf);
690 printk(KERN_CRIT "slot %d offset bad\n", slot);
694 if (slot < nritems - 1) {
695 btrfs_item_key(leaf, &leaf_key, slot);
696 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
697 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
698 if (btrfs_item_offset_nr(leaf, slot) !=
699 btrfs_item_end_nr(leaf, slot + 1)) {
700 btrfs_print_leaf(root, leaf);
701 printk(KERN_CRIT "slot %d offset bad\n", slot);
705 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
706 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
710 static noinline int check_block(struct btrfs_root *root,
711 struct btrfs_path *path, int level)
715 return check_leaf(root, path, level);
716 return check_node(root, path, level);
720 * search for key in the extent_buffer. The items start at offset p,
721 * and they are item_size apart. There are 'max' items in p.
723 * the slot in the array is returned via slot, and it points to
724 * the place where you would insert key if it is not found in
727 * slot may point to max if the key is bigger than all of the keys
729 static noinline int generic_bin_search(struct extent_buffer *eb,
731 int item_size, struct btrfs_key *key,
738 struct btrfs_disk_key *tmp = NULL;
739 struct btrfs_disk_key unaligned;
740 unsigned long offset;
741 char *map_token = NULL;
743 unsigned long map_start = 0;
744 unsigned long map_len = 0;
748 mid = (low + high) / 2;
749 offset = p + mid * item_size;
751 if (!map_token || offset < map_start ||
752 (offset + sizeof(struct btrfs_disk_key)) >
753 map_start + map_len) {
755 unmap_extent_buffer(eb, map_token, KM_USER0);
759 err = map_private_extent_buffer(eb, offset,
760 sizeof(struct btrfs_disk_key),
762 &map_start, &map_len, KM_USER0);
765 tmp = (struct btrfs_disk_key *)(kaddr + offset -
768 read_extent_buffer(eb, &unaligned,
769 offset, sizeof(unaligned));
774 tmp = (struct btrfs_disk_key *)(kaddr + offset -
777 ret = comp_keys(tmp, key);
786 unmap_extent_buffer(eb, map_token, KM_USER0);
792 unmap_extent_buffer(eb, map_token, KM_USER0);
797 * simple bin_search frontend that does the right thing for
800 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
801 int level, int *slot)
804 return generic_bin_search(eb,
805 offsetof(struct btrfs_leaf, items),
806 sizeof(struct btrfs_item),
807 key, btrfs_header_nritems(eb),
810 return generic_bin_search(eb,
811 offsetof(struct btrfs_node, ptrs),
812 sizeof(struct btrfs_key_ptr),
813 key, btrfs_header_nritems(eb),
819 /* given a node and slot number, this reads the blocks it points to. The
820 * extent buffer is returned with a reference taken (but unlocked).
821 * NULL is returned on error.
823 static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
824 struct extent_buffer *parent, int slot)
826 int level = btrfs_header_level(parent);
829 if (slot >= btrfs_header_nritems(parent))
834 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
835 btrfs_level_size(root, level - 1),
836 btrfs_node_ptr_generation(parent, slot));
840 * node level balancing, used to make sure nodes are in proper order for
841 * item deletion. We balance from the top down, so we have to make sure
842 * that a deletion won't leave an node completely empty later on.
844 static noinline int balance_level(struct btrfs_trans_handle *trans,
845 struct btrfs_root *root,
846 struct btrfs_path *path, int level)
848 struct extent_buffer *right = NULL;
849 struct extent_buffer *mid;
850 struct extent_buffer *left = NULL;
851 struct extent_buffer *parent = NULL;
855 int orig_slot = path->slots[level];
856 int err_on_enospc = 0;
862 mid = path->nodes[level];
863 WARN_ON(!path->locks[level]);
864 WARN_ON(btrfs_header_generation(mid) != trans->transid);
866 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
868 if (level < BTRFS_MAX_LEVEL - 1)
869 parent = path->nodes[level + 1];
870 pslot = path->slots[level + 1];
873 * deal with the case where there is only one pointer in the root
874 * by promoting the node below to a root
877 struct extent_buffer *child;
879 if (btrfs_header_nritems(mid) != 1)
882 /* promote the child to a root */
883 child = read_node_slot(root, mid, 0);
884 btrfs_tree_lock(child);
886 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
889 spin_lock(&root->node_lock);
891 spin_unlock(&root->node_lock);
893 ret = btrfs_update_extent_ref(trans, root, child->start,
894 mid->start, child->start,
895 root->root_key.objectid,
896 trans->transid, level - 1);
899 add_root_to_dirty_list(root);
900 btrfs_tree_unlock(child);
901 path->locks[level] = 0;
902 path->nodes[level] = NULL;
903 clean_tree_block(trans, root, mid);
904 btrfs_tree_unlock(mid);
905 /* once for the path */
906 free_extent_buffer(mid);
907 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
908 mid->start, root->root_key.objectid,
909 btrfs_header_generation(mid),
911 /* once for the root ptr */
912 free_extent_buffer(mid);
915 if (btrfs_header_nritems(mid) >
916 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
919 if (btrfs_header_nritems(mid) < 2)
922 left = read_node_slot(root, parent, pslot - 1);
924 btrfs_tree_lock(left);
925 wret = btrfs_cow_block(trans, root, left,
926 parent, pslot - 1, &left, 0);
932 right = read_node_slot(root, parent, pslot + 1);
934 btrfs_tree_lock(right);
935 wret = btrfs_cow_block(trans, root, right,
936 parent, pslot + 1, &right, 0);
943 /* first, try to make some room in the middle buffer */
945 orig_slot += btrfs_header_nritems(left);
946 wret = push_node_left(trans, root, left, mid, 1);
949 if (btrfs_header_nritems(mid) < 2)
954 * then try to empty the right most buffer into the middle
957 wret = push_node_left(trans, root, mid, right, 1);
958 if (wret < 0 && wret != -ENOSPC)
960 if (btrfs_header_nritems(right) == 0) {
961 u64 bytenr = right->start;
962 u64 generation = btrfs_header_generation(parent);
963 u32 blocksize = right->len;
965 clean_tree_block(trans, root, right);
966 btrfs_tree_unlock(right);
967 free_extent_buffer(right);
969 wret = del_ptr(trans, root, path, level + 1, pslot +
973 wret = btrfs_free_extent(trans, root, bytenr,
974 blocksize, parent->start,
975 btrfs_header_owner(parent),
976 generation, level, 1);
980 struct btrfs_disk_key right_key;
981 btrfs_node_key(right, &right_key, 0);
982 btrfs_set_node_key(parent, &right_key, pslot + 1);
983 btrfs_mark_buffer_dirty(parent);
986 if (btrfs_header_nritems(mid) == 1) {
988 * we're not allowed to leave a node with one item in the
989 * tree during a delete. A deletion from lower in the tree
990 * could try to delete the only pointer in this node.
991 * So, pull some keys from the left.
992 * There has to be a left pointer at this point because
993 * otherwise we would have pulled some pointers from the
997 wret = balance_node_right(trans, root, mid, left);
1003 wret = push_node_left(trans, root, left, mid, 1);
1009 if (btrfs_header_nritems(mid) == 0) {
1010 /* we've managed to empty the middle node, drop it */
1011 u64 root_gen = btrfs_header_generation(parent);
1012 u64 bytenr = mid->start;
1013 u32 blocksize = mid->len;
1015 clean_tree_block(trans, root, mid);
1016 btrfs_tree_unlock(mid);
1017 free_extent_buffer(mid);
1019 wret = del_ptr(trans, root, path, level + 1, pslot);
1022 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
1024 btrfs_header_owner(parent),
1025 root_gen, level, 1);
1029 /* update the parent key to reflect our changes */
1030 struct btrfs_disk_key mid_key;
1031 btrfs_node_key(mid, &mid_key, 0);
1032 btrfs_set_node_key(parent, &mid_key, pslot);
1033 btrfs_mark_buffer_dirty(parent);
1036 /* update the path */
1038 if (btrfs_header_nritems(left) > orig_slot) {
1039 extent_buffer_get(left);
1040 /* left was locked after cow */
1041 path->nodes[level] = left;
1042 path->slots[level + 1] -= 1;
1043 path->slots[level] = orig_slot;
1045 btrfs_tree_unlock(mid);
1046 free_extent_buffer(mid);
1049 orig_slot -= btrfs_header_nritems(left);
1050 path->slots[level] = orig_slot;
1053 /* double check we haven't messed things up */
1054 check_block(root, path, level);
1056 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
1060 btrfs_tree_unlock(right);
1061 free_extent_buffer(right);
1064 if (path->nodes[level] != left)
1065 btrfs_tree_unlock(left);
1066 free_extent_buffer(left);
1071 /* Node balancing for insertion. Here we only split or push nodes around
1072 * when they are completely full. This is also done top down, so we
1073 * have to be pessimistic.
1075 static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1076 struct btrfs_root *root,
1077 struct btrfs_path *path, int level)
1079 struct extent_buffer *right = NULL;
1080 struct extent_buffer *mid;
1081 struct extent_buffer *left = NULL;
1082 struct extent_buffer *parent = NULL;
1086 int orig_slot = path->slots[level];
1092 mid = path->nodes[level];
1093 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1094 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1096 if (level < BTRFS_MAX_LEVEL - 1)
1097 parent = path->nodes[level + 1];
1098 pslot = path->slots[level + 1];
1103 left = read_node_slot(root, parent, pslot - 1);
1105 /* first, try to make some room in the middle buffer */
1109 btrfs_tree_lock(left);
1110 left_nr = btrfs_header_nritems(left);
1111 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1114 ret = btrfs_cow_block(trans, root, left, parent,
1115 pslot - 1, &left, 0);
1119 wret = push_node_left(trans, root,
1126 struct btrfs_disk_key disk_key;
1127 orig_slot += left_nr;
1128 btrfs_node_key(mid, &disk_key, 0);
1129 btrfs_set_node_key(parent, &disk_key, pslot);
1130 btrfs_mark_buffer_dirty(parent);
1131 if (btrfs_header_nritems(left) > orig_slot) {
1132 path->nodes[level] = left;
1133 path->slots[level + 1] -= 1;
1134 path->slots[level] = orig_slot;
1135 btrfs_tree_unlock(mid);
1136 free_extent_buffer(mid);
1139 btrfs_header_nritems(left);
1140 path->slots[level] = orig_slot;
1141 btrfs_tree_unlock(left);
1142 free_extent_buffer(left);
1146 btrfs_tree_unlock(left);
1147 free_extent_buffer(left);
1149 right = read_node_slot(root, parent, pslot + 1);
1152 * then try to empty the right most buffer into the middle
1156 btrfs_tree_lock(right);
1157 right_nr = btrfs_header_nritems(right);
1158 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1161 ret = btrfs_cow_block(trans, root, right,
1167 wret = balance_node_right(trans, root,
1174 struct btrfs_disk_key disk_key;
1176 btrfs_node_key(right, &disk_key, 0);
1177 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1178 btrfs_mark_buffer_dirty(parent);
1180 if (btrfs_header_nritems(mid) <= orig_slot) {
1181 path->nodes[level] = right;
1182 path->slots[level + 1] += 1;
1183 path->slots[level] = orig_slot -
1184 btrfs_header_nritems(mid);
1185 btrfs_tree_unlock(mid);
1186 free_extent_buffer(mid);
1188 btrfs_tree_unlock(right);
1189 free_extent_buffer(right);
1193 btrfs_tree_unlock(right);
1194 free_extent_buffer(right);
1200 * readahead one full node of leaves, finding things that are close
1201 * to the block in 'slot', and triggering ra on them.
1203 static noinline void reada_for_search(struct btrfs_root *root,
1204 struct btrfs_path *path,
1205 int level, int slot, u64 objectid)
1207 struct extent_buffer *node;
1208 struct btrfs_disk_key disk_key;
1213 int direction = path->reada;
1214 struct extent_buffer *eb;
1222 if (!path->nodes[level])
1225 node = path->nodes[level];
1227 search = btrfs_node_blockptr(node, slot);
1228 blocksize = btrfs_level_size(root, level - 1);
1229 eb = btrfs_find_tree_block(root, search, blocksize);
1231 free_extent_buffer(eb);
1237 nritems = btrfs_header_nritems(node);
1240 if (direction < 0) {
1244 } else if (direction > 0) {
1249 if (path->reada < 0 && objectid) {
1250 btrfs_node_key(node, &disk_key, nr);
1251 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1254 search = btrfs_node_blockptr(node, nr);
1255 if ((search <= target && target - search <= 65536) ||
1256 (search > target && search - target <= 65536)) {
1257 readahead_tree_block(root, search, blocksize,
1258 btrfs_node_ptr_generation(node, nr));
1262 if ((nread > 65536 || nscan > 32))
1268 * when we walk down the tree, it is usually safe to unlock the higher layers
1269 * in the tree. The exceptions are when our path goes through slot 0, because
1270 * operations on the tree might require changing key pointers higher up in the
1273 * callers might also have set path->keep_locks, which tells this code to keep
1274 * the lock if the path points to the last slot in the block. This is part of
1275 * walking through the tree, and selecting the next slot in the higher block.
1277 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1278 * if lowest_unlock is 1, level 0 won't be unlocked
1280 static noinline void unlock_up(struct btrfs_path *path, int level,
1284 int skip_level = level;
1286 struct extent_buffer *t;
1288 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1289 if (!path->nodes[i])
1291 if (!path->locks[i])
1293 if (!no_skips && path->slots[i] == 0) {
1297 if (!no_skips && path->keep_locks) {
1300 nritems = btrfs_header_nritems(t);
1301 if (nritems < 1 || path->slots[i] >= nritems - 1) {
1306 if (skip_level < i && i >= lowest_unlock)
1310 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1311 btrfs_tree_unlock(t);
1318 * look for key in the tree. path is filled in with nodes along the way
1319 * if key is found, we return zero and you can find the item in the leaf
1320 * level of the path (level 0)
1322 * If the key isn't found, the path points to the slot where it should
1323 * be inserted, and 1 is returned. If there are other errors during the
1324 * search a negative error number is returned.
1326 * if ins_len > 0, nodes and leaves will be split as we walk down the
1327 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1330 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1331 *root, struct btrfs_key *key, struct btrfs_path *p, int
1334 struct extent_buffer *b;
1335 struct extent_buffer *tmp;
1339 int should_reada = p->reada;
1340 int lowest_unlock = 1;
1342 u8 lowest_level = 0;
1345 struct btrfs_key prealloc_block;
1347 lowest_level = p->lowest_level;
1348 WARN_ON(lowest_level && ins_len > 0);
1349 WARN_ON(p->nodes[0] != NULL);
1354 prealloc_block.objectid = 0;
1357 if (p->skip_locking)
1358 b = btrfs_root_node(root);
1360 b = btrfs_lock_root_node(root);
1363 level = btrfs_header_level(b);
1366 * setup the path here so we can release it under lock
1367 * contention with the cow code
1369 p->nodes[level] = b;
1370 if (!p->skip_locking)
1371 p->locks[level] = 1;
1376 /* is a cow on this block not required */
1377 if (btrfs_header_generation(b) == trans->transid &&
1378 btrfs_header_owner(b) == root->root_key.objectid &&
1379 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
1383 /* ok, we have to cow, is our old prealloc the right
1386 if (prealloc_block.objectid &&
1387 prealloc_block.offset != b->len) {
1388 btrfs_free_reserved_extent(root,
1389 prealloc_block.objectid,
1390 prealloc_block.offset);
1391 prealloc_block.objectid = 0;
1395 * for higher level blocks, try not to allocate blocks
1396 * with the block and the parent locks held.
1398 if (level > 1 && !prealloc_block.objectid &&
1399 btrfs_path_lock_waiting(p, level)) {
1401 u64 hint = b->start;
1403 btrfs_release_path(root, p);
1404 ret = btrfs_reserve_extent(trans, root,
1407 &prealloc_block, 0);
1412 wret = btrfs_cow_block(trans, root, b,
1413 p->nodes[level + 1],
1414 p->slots[level + 1],
1415 &b, prealloc_block.objectid);
1416 prealloc_block.objectid = 0;
1418 free_extent_buffer(b);
1424 BUG_ON(!cow && ins_len);
1425 if (level != btrfs_header_level(b))
1427 level = btrfs_header_level(b);
1429 p->nodes[level] = b;
1430 if (!p->skip_locking)
1431 p->locks[level] = 1;
1433 ret = check_block(root, p, level);
1439 ret = bin_search(b, key, level, &slot);
1441 if (ret && slot > 0)
1443 p->slots[level] = slot;
1444 if ((p->search_for_split || ins_len > 0) &&
1445 btrfs_header_nritems(b) >=
1446 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1447 int sret = split_node(trans, root, p, level);
1453 b = p->nodes[level];
1454 slot = p->slots[level];
1455 } else if (ins_len < 0) {
1456 int sret = balance_level(trans, root, p,
1462 b = p->nodes[level];
1464 btrfs_release_path(NULL, p);
1467 slot = p->slots[level];
1468 BUG_ON(btrfs_header_nritems(b) == 1);
1470 unlock_up(p, level, lowest_unlock);
1472 /* this is only true while dropping a snapshot */
1473 if (level == lowest_level) {
1478 blocknr = btrfs_node_blockptr(b, slot);
1479 gen = btrfs_node_ptr_generation(b, slot);
1480 blocksize = btrfs_level_size(root, level - 1);
1482 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1483 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1487 * reduce lock contention at high levels
1488 * of the btree by dropping locks before
1492 btrfs_release_path(NULL, p);
1494 free_extent_buffer(tmp);
1496 reada_for_search(root, p,
1500 tmp = read_tree_block(root, blocknr,
1503 free_extent_buffer(tmp);
1507 free_extent_buffer(tmp);
1509 reada_for_search(root, p,
1512 b = read_node_slot(root, b, slot);
1515 if (!p->skip_locking)
1518 p->slots[level] = slot;
1520 btrfs_leaf_free_space(root, b) < ins_len) {
1521 int sret = split_leaf(trans, root, key,
1522 p, ins_len, ret == 0);
1529 if (!p->search_for_split)
1530 unlock_up(p, level, lowest_unlock);
1536 if (prealloc_block.objectid) {
1537 btrfs_free_reserved_extent(root,
1538 prealloc_block.objectid,
1539 prealloc_block.offset);
1545 int btrfs_merge_path(struct btrfs_trans_handle *trans,
1546 struct btrfs_root *root,
1547 struct btrfs_key *node_keys,
1548 u64 *nodes, int lowest_level)
1550 struct extent_buffer *eb;
1551 struct extent_buffer *parent;
1552 struct btrfs_key key;
1561 eb = btrfs_lock_root_node(root);
1562 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0);
1567 level = btrfs_header_level(parent);
1568 if (level == 0 || level <= lowest_level)
1571 ret = bin_search(parent, &node_keys[lowest_level], level,
1573 if (ret && slot > 0)
1576 bytenr = btrfs_node_blockptr(parent, slot);
1577 if (nodes[level - 1] == bytenr)
1580 blocksize = btrfs_level_size(root, level - 1);
1581 generation = btrfs_node_ptr_generation(parent, slot);
1582 btrfs_node_key_to_cpu(eb, &key, slot);
1583 key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key));
1585 if (generation == trans->transid) {
1586 eb = read_tree_block(root, bytenr, blocksize,
1588 btrfs_tree_lock(eb);
1592 * if node keys match and node pointer hasn't been modified
1593 * in the running transaction, we can merge the path. for
1594 * blocks owened by reloc trees, the node pointer check is
1595 * skipped, this is because these blocks are fully controlled
1596 * by the space balance code, no one else can modify them.
1598 if (!nodes[level - 1] || !key_match ||
1599 (generation == trans->transid &&
1600 btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID)) {
1601 if (level == 1 || level == lowest_level + 1) {
1602 if (generation == trans->transid) {
1603 btrfs_tree_unlock(eb);
1604 free_extent_buffer(eb);
1609 if (generation != trans->transid) {
1610 eb = read_tree_block(root, bytenr, blocksize,
1612 btrfs_tree_lock(eb);
1615 ret = btrfs_cow_block(trans, root, eb, parent, slot,
1619 if (root->root_key.objectid ==
1620 BTRFS_TREE_RELOC_OBJECTID) {
1621 if (!nodes[level - 1]) {
1622 nodes[level - 1] = eb->start;
1623 memcpy(&node_keys[level - 1], &key,
1624 sizeof(node_keys[0]));
1630 btrfs_tree_unlock(parent);
1631 free_extent_buffer(parent);
1636 btrfs_set_node_blockptr(parent, slot, nodes[level - 1]);
1637 btrfs_set_node_ptr_generation(parent, slot, trans->transid);
1638 btrfs_mark_buffer_dirty(parent);
1640 ret = btrfs_inc_extent_ref(trans, root,
1642 blocksize, parent->start,
1643 btrfs_header_owner(parent),
1644 btrfs_header_generation(parent),
1649 * If the block was created in the running transaction,
1650 * it's possible this is the last reference to it, so we
1651 * should drop the subtree.
1653 if (generation == trans->transid) {
1654 ret = btrfs_drop_subtree(trans, root, eb, parent);
1656 btrfs_tree_unlock(eb);
1657 free_extent_buffer(eb);
1659 ret = btrfs_free_extent(trans, root, bytenr,
1660 blocksize, parent->start,
1661 btrfs_header_owner(parent),
1662 btrfs_header_generation(parent),
1668 btrfs_tree_unlock(parent);
1669 free_extent_buffer(parent);
1674 * adjust the pointers going up the tree, starting at level
1675 * making sure the right key of each node is points to 'key'.
1676 * This is used after shifting pointers to the left, so it stops
1677 * fixing up pointers when a given leaf/node is not in slot 0 of the
1680 * If this fails to write a tree block, it returns -1, but continues
1681 * fixing up the blocks in ram so the tree is consistent.
1683 static int fixup_low_keys(struct btrfs_trans_handle *trans,
1684 struct btrfs_root *root, struct btrfs_path *path,
1685 struct btrfs_disk_key *key, int level)
1689 struct extent_buffer *t;
1691 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1692 int tslot = path->slots[i];
1693 if (!path->nodes[i])
1696 btrfs_set_node_key(t, key, tslot);
1697 btrfs_mark_buffer_dirty(path->nodes[i]);
1707 * This function isn't completely safe. It's the caller's responsibility
1708 * that the new key won't break the order
1710 int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1711 struct btrfs_root *root, struct btrfs_path *path,
1712 struct btrfs_key *new_key)
1714 struct btrfs_disk_key disk_key;
1715 struct extent_buffer *eb;
1718 eb = path->nodes[0];
1719 slot = path->slots[0];
1721 btrfs_item_key(eb, &disk_key, slot - 1);
1722 if (comp_keys(&disk_key, new_key) >= 0)
1725 if (slot < btrfs_header_nritems(eb) - 1) {
1726 btrfs_item_key(eb, &disk_key, slot + 1);
1727 if (comp_keys(&disk_key, new_key) <= 0)
1731 btrfs_cpu_key_to_disk(&disk_key, new_key);
1732 btrfs_set_item_key(eb, &disk_key, slot);
1733 btrfs_mark_buffer_dirty(eb);
1735 fixup_low_keys(trans, root, path, &disk_key, 1);
1740 * try to push data from one node into the next node left in the
1743 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1744 * error, and > 0 if there was no room in the left hand block.
1746 static int push_node_left(struct btrfs_trans_handle *trans,
1747 struct btrfs_root *root, struct extent_buffer *dst,
1748 struct extent_buffer *src, int empty)
1755 src_nritems = btrfs_header_nritems(src);
1756 dst_nritems = btrfs_header_nritems(dst);
1757 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1758 WARN_ON(btrfs_header_generation(src) != trans->transid);
1759 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1761 if (!empty && src_nritems <= 8)
1764 if (push_items <= 0)
1768 push_items = min(src_nritems, push_items);
1769 if (push_items < src_nritems) {
1770 /* leave at least 8 pointers in the node if
1771 * we aren't going to empty it
1773 if (src_nritems - push_items < 8) {
1774 if (push_items <= 8)
1780 push_items = min(src_nritems - 8, push_items);
1782 copy_extent_buffer(dst, src,
1783 btrfs_node_key_ptr_offset(dst_nritems),
1784 btrfs_node_key_ptr_offset(0),
1785 push_items * sizeof(struct btrfs_key_ptr));
1787 if (push_items < src_nritems) {
1788 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1789 btrfs_node_key_ptr_offset(push_items),
1790 (src_nritems - push_items) *
1791 sizeof(struct btrfs_key_ptr));
1793 btrfs_set_header_nritems(src, src_nritems - push_items);
1794 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1795 btrfs_mark_buffer_dirty(src);
1796 btrfs_mark_buffer_dirty(dst);
1798 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
1805 * try to push data from one node into the next node right in the
1808 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1809 * error, and > 0 if there was no room in the right hand block.
1811 * this will only push up to 1/2 the contents of the left node over
1813 static int balance_node_right(struct btrfs_trans_handle *trans,
1814 struct btrfs_root *root,
1815 struct extent_buffer *dst,
1816 struct extent_buffer *src)
1824 WARN_ON(btrfs_header_generation(src) != trans->transid);
1825 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1827 src_nritems = btrfs_header_nritems(src);
1828 dst_nritems = btrfs_header_nritems(dst);
1829 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1830 if (push_items <= 0)
1833 if (src_nritems < 4)
1836 max_push = src_nritems / 2 + 1;
1837 /* don't try to empty the node */
1838 if (max_push >= src_nritems)
1841 if (max_push < push_items)
1842 push_items = max_push;
1844 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1845 btrfs_node_key_ptr_offset(0),
1847 sizeof(struct btrfs_key_ptr));
1849 copy_extent_buffer(dst, src,
1850 btrfs_node_key_ptr_offset(0),
1851 btrfs_node_key_ptr_offset(src_nritems - push_items),
1852 push_items * sizeof(struct btrfs_key_ptr));
1854 btrfs_set_header_nritems(src, src_nritems - push_items);
1855 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1857 btrfs_mark_buffer_dirty(src);
1858 btrfs_mark_buffer_dirty(dst);
1860 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
1867 * helper function to insert a new root level in the tree.
1868 * A new node is allocated, and a single item is inserted to
1869 * point to the existing root
1871 * returns zero on success or < 0 on failure.
1873 static noinline int insert_new_root(struct btrfs_trans_handle *trans,
1874 struct btrfs_root *root,
1875 struct btrfs_path *path, int level)
1878 struct extent_buffer *lower;
1879 struct extent_buffer *c;
1880 struct extent_buffer *old;
1881 struct btrfs_disk_key lower_key;
1884 BUG_ON(path->nodes[level]);
1885 BUG_ON(path->nodes[level-1] != root->node);
1887 lower = path->nodes[level-1];
1889 btrfs_item_key(lower, &lower_key, 0);
1891 btrfs_node_key(lower, &lower_key, 0);
1893 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
1894 root->root_key.objectid, trans->transid,
1895 level, root->node->start, 0);
1899 memset_extent_buffer(c, 0, 0, root->nodesize);
1900 btrfs_set_header_nritems(c, 1);
1901 btrfs_set_header_level(c, level);
1902 btrfs_set_header_bytenr(c, c->start);
1903 btrfs_set_header_generation(c, trans->transid);
1904 btrfs_set_header_owner(c, root->root_key.objectid);
1906 write_extent_buffer(c, root->fs_info->fsid,
1907 (unsigned long)btrfs_header_fsid(c),
1910 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1911 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1914 btrfs_set_node_key(c, &lower_key, 0);
1915 btrfs_set_node_blockptr(c, 0, lower->start);
1916 lower_gen = btrfs_header_generation(lower);
1917 WARN_ON(lower_gen != trans->transid);
1919 btrfs_set_node_ptr_generation(c, 0, lower_gen);
1921 btrfs_mark_buffer_dirty(c);
1923 spin_lock(&root->node_lock);
1926 spin_unlock(&root->node_lock);
1928 ret = btrfs_update_extent_ref(trans, root, lower->start,
1929 lower->start, c->start,
1930 root->root_key.objectid,
1931 trans->transid, level - 1);
1934 /* the super has an extra ref to root->node */
1935 free_extent_buffer(old);
1937 add_root_to_dirty_list(root);
1938 extent_buffer_get(c);
1939 path->nodes[level] = c;
1940 path->locks[level] = 1;
1941 path->slots[level] = 0;
1946 * worker function to insert a single pointer in a node.
1947 * the node should have enough room for the pointer already
1949 * slot and level indicate where you want the key to go, and
1950 * blocknr is the block the key points to.
1952 * returns zero on success and < 0 on any error
1954 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1955 *root, struct btrfs_path *path, struct btrfs_disk_key
1956 *key, u64 bytenr, int slot, int level)
1958 struct extent_buffer *lower;
1961 BUG_ON(!path->nodes[level]);
1962 lower = path->nodes[level];
1963 nritems = btrfs_header_nritems(lower);
1966 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
1968 if (slot != nritems) {
1969 memmove_extent_buffer(lower,
1970 btrfs_node_key_ptr_offset(slot + 1),
1971 btrfs_node_key_ptr_offset(slot),
1972 (nritems - slot) * sizeof(struct btrfs_key_ptr));
1974 btrfs_set_node_key(lower, key, slot);
1975 btrfs_set_node_blockptr(lower, slot, bytenr);
1976 WARN_ON(trans->transid == 0);
1977 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
1978 btrfs_set_header_nritems(lower, nritems + 1);
1979 btrfs_mark_buffer_dirty(lower);
1984 * split the node at the specified level in path in two.
1985 * The path is corrected to point to the appropriate node after the split
1987 * Before splitting this tries to make some room in the node by pushing
1988 * left and right, if either one works, it returns right away.
1990 * returns 0 on success and < 0 on failure
1992 static noinline int split_node(struct btrfs_trans_handle *trans,
1993 struct btrfs_root *root,
1994 struct btrfs_path *path, int level)
1996 struct extent_buffer *c;
1997 struct extent_buffer *split;
1998 struct btrfs_disk_key disk_key;
2004 c = path->nodes[level];
2005 WARN_ON(btrfs_header_generation(c) != trans->transid);
2006 if (c == root->node) {
2007 /* trying to split the root, lets make a new one */
2008 ret = insert_new_root(trans, root, path, level + 1);
2012 ret = push_nodes_for_insert(trans, root, path, level);
2013 c = path->nodes[level];
2014 if (!ret && btrfs_header_nritems(c) <
2015 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
2021 c_nritems = btrfs_header_nritems(c);
2023 split = btrfs_alloc_free_block(trans, root, root->nodesize,
2024 path->nodes[level + 1]->start,
2025 root->root_key.objectid,
2026 trans->transid, level, c->start, 0);
2028 return PTR_ERR(split);
2030 btrfs_set_header_flags(split, btrfs_header_flags(c));
2031 btrfs_set_header_level(split, btrfs_header_level(c));
2032 btrfs_set_header_bytenr(split, split->start);
2033 btrfs_set_header_generation(split, trans->transid);
2034 btrfs_set_header_owner(split, root->root_key.objectid);
2035 btrfs_set_header_flags(split, 0);
2036 write_extent_buffer(split, root->fs_info->fsid,
2037 (unsigned long)btrfs_header_fsid(split),
2039 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2040 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2043 mid = (c_nritems + 1) / 2;
2045 copy_extent_buffer(split, c,
2046 btrfs_node_key_ptr_offset(0),
2047 btrfs_node_key_ptr_offset(mid),
2048 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2049 btrfs_set_header_nritems(split, c_nritems - mid);
2050 btrfs_set_header_nritems(c, mid);
2053 btrfs_mark_buffer_dirty(c);
2054 btrfs_mark_buffer_dirty(split);
2056 btrfs_node_key(split, &disk_key, 0);
2057 wret = insert_ptr(trans, root, path, &disk_key, split->start,
2058 path->slots[level + 1] + 1,
2063 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
2066 if (path->slots[level] >= mid) {
2067 path->slots[level] -= mid;
2068 btrfs_tree_unlock(c);
2069 free_extent_buffer(c);
2070 path->nodes[level] = split;
2071 path->slots[level + 1] += 1;
2073 btrfs_tree_unlock(split);
2074 free_extent_buffer(split);
2080 * how many bytes are required to store the items in a leaf. start
2081 * and nr indicate which items in the leaf to check. This totals up the
2082 * space used both by the item structs and the item data
2084 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
2087 int nritems = btrfs_header_nritems(l);
2088 int end = min(nritems, start + nr) - 1;
2092 data_len = btrfs_item_end_nr(l, start);
2093 data_len = data_len - btrfs_item_offset_nr(l, end);
2094 data_len += sizeof(struct btrfs_item) * nr;
2095 WARN_ON(data_len < 0);
2100 * The space between the end of the leaf items and
2101 * the start of the leaf data. IOW, how much room
2102 * the leaf has left for both items and data
2104 noinline int btrfs_leaf_free_space(struct btrfs_root *root,
2105 struct extent_buffer *leaf)
2107 int nritems = btrfs_header_nritems(leaf);
2109 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2111 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2112 "used %d nritems %d\n",
2113 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
2114 leaf_space_used(leaf, 0, nritems), nritems);
2120 * push some data in the path leaf to the right, trying to free up at
2121 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2123 * returns 1 if the push failed because the other node didn't have enough
2124 * room, 0 if everything worked out and < 0 if there were major errors.
2126 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
2127 *root, struct btrfs_path *path, int data_size,
2130 struct extent_buffer *left = path->nodes[0];
2131 struct extent_buffer *right;
2132 struct extent_buffer *upper;
2133 struct btrfs_disk_key disk_key;
2139 struct btrfs_item *item;
2147 slot = path->slots[1];
2148 if (!path->nodes[1])
2151 upper = path->nodes[1];
2152 if (slot >= btrfs_header_nritems(upper) - 1)
2155 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2157 right = read_node_slot(root, upper, slot + 1);
2158 btrfs_tree_lock(right);
2159 free_space = btrfs_leaf_free_space(root, right);
2160 if (free_space < data_size)
2163 /* cow and double check */
2164 ret = btrfs_cow_block(trans, root, right, upper,
2165 slot + 1, &right, 0);
2169 free_space = btrfs_leaf_free_space(root, right);
2170 if (free_space < data_size)
2173 left_nritems = btrfs_header_nritems(left);
2174 if (left_nritems == 0)
2182 if (path->slots[0] >= left_nritems)
2183 push_space += data_size;
2185 i = left_nritems - 1;
2187 item = btrfs_item_nr(left, i);
2189 if (!empty && push_items > 0) {
2190 if (path->slots[0] > i)
2192 if (path->slots[0] == i) {
2193 int space = btrfs_leaf_free_space(root, left);
2194 if (space + push_space * 2 > free_space)
2199 if (path->slots[0] == i)
2200 push_space += data_size;
2202 if (!left->map_token) {
2203 map_extent_buffer(left, (unsigned long)item,
2204 sizeof(struct btrfs_item),
2205 &left->map_token, &left->kaddr,
2206 &left->map_start, &left->map_len,
2210 this_item_size = btrfs_item_size(left, item);
2211 if (this_item_size + sizeof(*item) + push_space > free_space)
2215 push_space += this_item_size + sizeof(*item);
2220 if (left->map_token) {
2221 unmap_extent_buffer(left, left->map_token, KM_USER1);
2222 left->map_token = NULL;
2225 if (push_items == 0)
2228 if (!empty && push_items == left_nritems)
2231 /* push left to right */
2232 right_nritems = btrfs_header_nritems(right);
2234 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
2235 push_space -= leaf_data_end(root, left);
2237 /* make room in the right data area */
2238 data_end = leaf_data_end(root, right);
2239 memmove_extent_buffer(right,
2240 btrfs_leaf_data(right) + data_end - push_space,
2241 btrfs_leaf_data(right) + data_end,
2242 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2244 /* copy from the left data area */
2245 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
2246 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2247 btrfs_leaf_data(left) + leaf_data_end(root, left),
2250 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2251 btrfs_item_nr_offset(0),
2252 right_nritems * sizeof(struct btrfs_item));
2254 /* copy the items from left to right */
2255 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2256 btrfs_item_nr_offset(left_nritems - push_items),
2257 push_items * sizeof(struct btrfs_item));
2259 /* update the item pointers */
2260 right_nritems += push_items;
2261 btrfs_set_header_nritems(right, right_nritems);
2262 push_space = BTRFS_LEAF_DATA_SIZE(root);
2263 for (i = 0; i < right_nritems; i++) {
2264 item = btrfs_item_nr(right, i);
2265 if (!right->map_token) {
2266 map_extent_buffer(right, (unsigned long)item,
2267 sizeof(struct btrfs_item),
2268 &right->map_token, &right->kaddr,
2269 &right->map_start, &right->map_len,
2272 push_space -= btrfs_item_size(right, item);
2273 btrfs_set_item_offset(right, item, push_space);
2276 if (right->map_token) {
2277 unmap_extent_buffer(right, right->map_token, KM_USER1);
2278 right->map_token = NULL;
2280 left_nritems -= push_items;
2281 btrfs_set_header_nritems(left, left_nritems);
2284 btrfs_mark_buffer_dirty(left);
2285 btrfs_mark_buffer_dirty(right);
2287 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2290 btrfs_item_key(right, &disk_key, 0);
2291 btrfs_set_node_key(upper, &disk_key, slot + 1);
2292 btrfs_mark_buffer_dirty(upper);
2294 /* then fixup the leaf pointer in the path */
2295 if (path->slots[0] >= left_nritems) {
2296 path->slots[0] -= left_nritems;
2297 if (btrfs_header_nritems(path->nodes[0]) == 0)
2298 clean_tree_block(trans, root, path->nodes[0]);
2299 btrfs_tree_unlock(path->nodes[0]);
2300 free_extent_buffer(path->nodes[0]);
2301 path->nodes[0] = right;
2302 path->slots[1] += 1;
2304 btrfs_tree_unlock(right);
2305 free_extent_buffer(right);
2310 btrfs_tree_unlock(right);
2311 free_extent_buffer(right);
2316 * push some data in the path leaf to the left, trying to free up at
2317 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2319 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
2320 *root, struct btrfs_path *path, int data_size,
2323 struct btrfs_disk_key disk_key;
2324 struct extent_buffer *right = path->nodes[0];
2325 struct extent_buffer *left;
2331 struct btrfs_item *item;
2332 u32 old_left_nritems;
2338 u32 old_left_item_size;
2340 slot = path->slots[1];
2343 if (!path->nodes[1])
2346 right_nritems = btrfs_header_nritems(right);
2347 if (right_nritems == 0)
2350 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2352 left = read_node_slot(root, path->nodes[1], slot - 1);
2353 btrfs_tree_lock(left);
2354 free_space = btrfs_leaf_free_space(root, left);
2355 if (free_space < data_size) {
2360 /* cow and double check */
2361 ret = btrfs_cow_block(trans, root, left,
2362 path->nodes[1], slot - 1, &left, 0);
2364 /* we hit -ENOSPC, but it isn't fatal here */
2369 free_space = btrfs_leaf_free_space(root, left);
2370 if (free_space < data_size) {
2378 nr = right_nritems - 1;
2380 for (i = 0; i < nr; i++) {
2381 item = btrfs_item_nr(right, i);
2382 if (!right->map_token) {
2383 map_extent_buffer(right, (unsigned long)item,
2384 sizeof(struct btrfs_item),
2385 &right->map_token, &right->kaddr,
2386 &right->map_start, &right->map_len,
2390 if (!empty && push_items > 0) {
2391 if (path->slots[0] < i)
2393 if (path->slots[0] == i) {
2394 int space = btrfs_leaf_free_space(root, right);
2395 if (space + push_space * 2 > free_space)
2400 if (path->slots[0] == i)
2401 push_space += data_size;
2403 this_item_size = btrfs_item_size(right, item);
2404 if (this_item_size + sizeof(*item) + push_space > free_space)
2408 push_space += this_item_size + sizeof(*item);
2411 if (right->map_token) {
2412 unmap_extent_buffer(right, right->map_token, KM_USER1);
2413 right->map_token = NULL;
2416 if (push_items == 0) {
2420 if (!empty && push_items == btrfs_header_nritems(right))
2423 /* push data from right to left */
2424 copy_extent_buffer(left, right,
2425 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2426 btrfs_item_nr_offset(0),
2427 push_items * sizeof(struct btrfs_item));
2429 push_space = BTRFS_LEAF_DATA_SIZE(root) -
2430 btrfs_item_offset_nr(right, push_items - 1);
2432 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
2433 leaf_data_end(root, left) - push_space,
2434 btrfs_leaf_data(right) +
2435 btrfs_item_offset_nr(right, push_items - 1),
2437 old_left_nritems = btrfs_header_nritems(left);
2438 BUG_ON(old_left_nritems <= 0);
2440 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
2441 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
2444 item = btrfs_item_nr(left, i);
2445 if (!left->map_token) {
2446 map_extent_buffer(left, (unsigned long)item,
2447 sizeof(struct btrfs_item),
2448 &left->map_token, &left->kaddr,
2449 &left->map_start, &left->map_len,
2453 ioff = btrfs_item_offset(left, item);
2454 btrfs_set_item_offset(left, item,
2455 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
2457 btrfs_set_header_nritems(left, old_left_nritems + push_items);
2458 if (left->map_token) {
2459 unmap_extent_buffer(left, left->map_token, KM_USER1);
2460 left->map_token = NULL;
2463 /* fixup right node */
2464 if (push_items > right_nritems) {
2465 printk(KERN_CRIT "push items %d nr %u\n", push_items,
2470 if (push_items < right_nritems) {
2471 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2472 leaf_data_end(root, right);
2473 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2474 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2475 btrfs_leaf_data(right) +
2476 leaf_data_end(root, right), push_space);
2478 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
2479 btrfs_item_nr_offset(push_items),
2480 (btrfs_header_nritems(right) - push_items) *
2481 sizeof(struct btrfs_item));
2483 right_nritems -= push_items;
2484 btrfs_set_header_nritems(right, right_nritems);
2485 push_space = BTRFS_LEAF_DATA_SIZE(root);
2486 for (i = 0; i < right_nritems; i++) {
2487 item = btrfs_item_nr(right, i);
2489 if (!right->map_token) {
2490 map_extent_buffer(right, (unsigned long)item,
2491 sizeof(struct btrfs_item),
2492 &right->map_token, &right->kaddr,
2493 &right->map_start, &right->map_len,
2497 push_space = push_space - btrfs_item_size(right, item);
2498 btrfs_set_item_offset(right, item, push_space);
2500 if (right->map_token) {
2501 unmap_extent_buffer(right, right->map_token, KM_USER1);
2502 right->map_token = NULL;
2505 btrfs_mark_buffer_dirty(left);
2507 btrfs_mark_buffer_dirty(right);
2509 ret = btrfs_update_ref(trans, root, right, left,
2510 old_left_nritems, push_items);
2513 btrfs_item_key(right, &disk_key, 0);
2514 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
2518 /* then fixup the leaf pointer in the path */
2519 if (path->slots[0] < push_items) {
2520 path->slots[0] += old_left_nritems;
2521 if (btrfs_header_nritems(path->nodes[0]) == 0)
2522 clean_tree_block(trans, root, path->nodes[0]);
2523 btrfs_tree_unlock(path->nodes[0]);
2524 free_extent_buffer(path->nodes[0]);
2525 path->nodes[0] = left;
2526 path->slots[1] -= 1;
2528 btrfs_tree_unlock(left);
2529 free_extent_buffer(left);
2530 path->slots[0] -= push_items;
2532 BUG_ON(path->slots[0] < 0);
2535 btrfs_tree_unlock(left);
2536 free_extent_buffer(left);
2541 * split the path's leaf in two, making sure there is at least data_size
2542 * available for the resulting leaf level of the path.
2544 * returns 0 if all went well and < 0 on failure.
2546 static noinline int split_leaf(struct btrfs_trans_handle *trans,
2547 struct btrfs_root *root,
2548 struct btrfs_key *ins_key,
2549 struct btrfs_path *path, int data_size,
2552 struct extent_buffer *l;
2556 struct extent_buffer *right;
2563 int num_doubles = 0;
2564 struct btrfs_disk_key disk_key;
2566 /* first try to make some room by pushing left and right */
2567 if (data_size && ins_key->type != BTRFS_DIR_ITEM_KEY) {
2568 wret = push_leaf_right(trans, root, path, data_size, 0);
2572 wret = push_leaf_left(trans, root, path, data_size, 0);
2578 /* did the pushes work? */
2579 if (btrfs_leaf_free_space(root, l) >= data_size)
2583 if (!path->nodes[1]) {
2584 ret = insert_new_root(trans, root, path, 1);
2591 slot = path->slots[0];
2592 nritems = btrfs_header_nritems(l);
2593 mid = (nritems + 1) / 2;
2595 right = btrfs_alloc_free_block(trans, root, root->leafsize,
2596 path->nodes[1]->start,
2597 root->root_key.objectid,
2598 trans->transid, 0, l->start, 0);
2599 if (IS_ERR(right)) {
2601 return PTR_ERR(right);
2604 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2605 btrfs_set_header_bytenr(right, right->start);
2606 btrfs_set_header_generation(right, trans->transid);
2607 btrfs_set_header_owner(right, root->root_key.objectid);
2608 btrfs_set_header_level(right, 0);
2609 write_extent_buffer(right, root->fs_info->fsid,
2610 (unsigned long)btrfs_header_fsid(right),
2613 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2614 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2618 leaf_space_used(l, mid, nritems - mid) + data_size >
2619 BTRFS_LEAF_DATA_SIZE(root)) {
2620 if (slot >= nritems) {
2621 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2622 btrfs_set_header_nritems(right, 0);
2623 wret = insert_ptr(trans, root, path,
2624 &disk_key, right->start,
2625 path->slots[1] + 1, 1);
2629 btrfs_tree_unlock(path->nodes[0]);
2630 free_extent_buffer(path->nodes[0]);
2631 path->nodes[0] = right;
2633 path->slots[1] += 1;
2634 btrfs_mark_buffer_dirty(right);
2638 if (mid != nritems &&
2639 leaf_space_used(l, mid, nritems - mid) +
2640 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2645 if (leaf_space_used(l, 0, mid) + data_size >
2646 BTRFS_LEAF_DATA_SIZE(root)) {
2647 if (!extend && data_size && slot == 0) {
2648 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2649 btrfs_set_header_nritems(right, 0);
2650 wret = insert_ptr(trans, root, path,
2656 btrfs_tree_unlock(path->nodes[0]);
2657 free_extent_buffer(path->nodes[0]);
2658 path->nodes[0] = right;
2660 if (path->slots[1] == 0) {
2661 wret = fixup_low_keys(trans, root,
2662 path, &disk_key, 1);
2666 btrfs_mark_buffer_dirty(right);
2668 } else if ((extend || !data_size) && slot == 0) {
2672 if (mid != nritems &&
2673 leaf_space_used(l, mid, nritems - mid) +
2674 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
2680 nritems = nritems - mid;
2681 btrfs_set_header_nritems(right, nritems);
2682 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2684 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2685 btrfs_item_nr_offset(mid),
2686 nritems * sizeof(struct btrfs_item));
2688 copy_extent_buffer(right, l,
2689 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2690 data_copy_size, btrfs_leaf_data(l) +
2691 leaf_data_end(root, l), data_copy_size);
2693 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2694 btrfs_item_end_nr(l, mid);
2696 for (i = 0; i < nritems; i++) {
2697 struct btrfs_item *item = btrfs_item_nr(right, i);
2700 if (!right->map_token) {
2701 map_extent_buffer(right, (unsigned long)item,
2702 sizeof(struct btrfs_item),
2703 &right->map_token, &right->kaddr,
2704 &right->map_start, &right->map_len,
2708 ioff = btrfs_item_offset(right, item);
2709 btrfs_set_item_offset(right, item, ioff + rt_data_off);
2712 if (right->map_token) {
2713 unmap_extent_buffer(right, right->map_token, KM_USER1);
2714 right->map_token = NULL;
2717 btrfs_set_header_nritems(l, mid);
2719 btrfs_item_key(right, &disk_key, 0);
2720 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2721 path->slots[1] + 1, 1);
2725 btrfs_mark_buffer_dirty(right);
2726 btrfs_mark_buffer_dirty(l);
2727 BUG_ON(path->slots[0] != slot);
2729 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2733 btrfs_tree_unlock(path->nodes[0]);
2734 free_extent_buffer(path->nodes[0]);
2735 path->nodes[0] = right;
2736 path->slots[0] -= mid;
2737 path->slots[1] += 1;
2739 btrfs_tree_unlock(right);
2740 free_extent_buffer(right);
2743 BUG_ON(path->slots[0] < 0);
2746 BUG_ON(num_doubles != 0);
2754 * This function splits a single item into two items,
2755 * giving 'new_key' to the new item and splitting the
2756 * old one at split_offset (from the start of the item).
2758 * The path may be released by this operation. After
2759 * the split, the path is pointing to the old item. The
2760 * new item is going to be in the same node as the old one.
2762 * Note, the item being split must be smaller enough to live alone on
2763 * a tree block with room for one extra struct btrfs_item
2765 * This allows us to split the item in place, keeping a lock on the
2766 * leaf the entire time.
2768 int btrfs_split_item(struct btrfs_trans_handle *trans,
2769 struct btrfs_root *root,
2770 struct btrfs_path *path,
2771 struct btrfs_key *new_key,
2772 unsigned long split_offset)
2775 struct extent_buffer *leaf;
2776 struct btrfs_key orig_key;
2777 struct btrfs_item *item;
2778 struct btrfs_item *new_item;
2783 struct btrfs_disk_key disk_key;
2786 leaf = path->nodes[0];
2787 btrfs_item_key_to_cpu(leaf, &orig_key, path->slots[0]);
2788 if (btrfs_leaf_free_space(root, leaf) >= sizeof(struct btrfs_item))
2791 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2792 btrfs_release_path(root, path);
2794 path->search_for_split = 1;
2795 path->keep_locks = 1;
2797 ret = btrfs_search_slot(trans, root, &orig_key, path, 0, 1);
2798 path->search_for_split = 0;
2800 /* if our item isn't there or got smaller, return now */
2801 if (ret != 0 || item_size != btrfs_item_size_nr(path->nodes[0],
2803 path->keep_locks = 0;
2807 ret = split_leaf(trans, root, &orig_key, path,
2808 sizeof(struct btrfs_item), 1);
2809 path->keep_locks = 0;
2812 leaf = path->nodes[0];
2813 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
2816 item = btrfs_item_nr(leaf, path->slots[0]);
2817 orig_offset = btrfs_item_offset(leaf, item);
2818 item_size = btrfs_item_size(leaf, item);
2821 buf = kmalloc(item_size, GFP_NOFS);
2822 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
2823 path->slots[0]), item_size);
2824 slot = path->slots[0] + 1;
2825 leaf = path->nodes[0];
2827 nritems = btrfs_header_nritems(leaf);
2829 if (slot != nritems) {
2830 /* shift the items */
2831 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
2832 btrfs_item_nr_offset(slot),
2833 (nritems - slot) * sizeof(struct btrfs_item));
2837 btrfs_cpu_key_to_disk(&disk_key, new_key);
2838 btrfs_set_item_key(leaf, &disk_key, slot);
2840 new_item = btrfs_item_nr(leaf, slot);
2842 btrfs_set_item_offset(leaf, new_item, orig_offset);
2843 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
2845 btrfs_set_item_offset(leaf, item,
2846 orig_offset + item_size - split_offset);
2847 btrfs_set_item_size(leaf, item, split_offset);
2849 btrfs_set_header_nritems(leaf, nritems + 1);
2851 /* write the data for the start of the original item */
2852 write_extent_buffer(leaf, buf,
2853 btrfs_item_ptr_offset(leaf, path->slots[0]),
2856 /* write the data for the new item */
2857 write_extent_buffer(leaf, buf + split_offset,
2858 btrfs_item_ptr_offset(leaf, slot),
2859 item_size - split_offset);
2860 btrfs_mark_buffer_dirty(leaf);
2863 if (btrfs_leaf_free_space(root, leaf) < 0) {
2864 btrfs_print_leaf(root, leaf);
2872 * make the item pointed to by the path smaller. new_size indicates
2873 * how small to make it, and from_end tells us if we just chop bytes
2874 * off the end of the item or if we shift the item to chop bytes off
2877 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2878 struct btrfs_root *root,
2879 struct btrfs_path *path,
2880 u32 new_size, int from_end)
2885 struct extent_buffer *leaf;
2886 struct btrfs_item *item;
2888 unsigned int data_end;
2889 unsigned int old_data_start;
2890 unsigned int old_size;
2891 unsigned int size_diff;
2894 slot_orig = path->slots[0];
2895 leaf = path->nodes[0];
2896 slot = path->slots[0];
2898 old_size = btrfs_item_size_nr(leaf, slot);
2899 if (old_size == new_size)
2902 nritems = btrfs_header_nritems(leaf);
2903 data_end = leaf_data_end(root, leaf);
2905 old_data_start = btrfs_item_offset_nr(leaf, slot);
2907 size_diff = old_size - new_size;
2910 BUG_ON(slot >= nritems);
2913 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2915 /* first correct the data pointers */
2916 for (i = slot; i < nritems; i++) {
2918 item = btrfs_item_nr(leaf, i);
2920 if (!leaf->map_token) {
2921 map_extent_buffer(leaf, (unsigned long)item,
2922 sizeof(struct btrfs_item),
2923 &leaf->map_token, &leaf->kaddr,
2924 &leaf->map_start, &leaf->map_len,
2928 ioff = btrfs_item_offset(leaf, item);
2929 btrfs_set_item_offset(leaf, item, ioff + size_diff);
2932 if (leaf->map_token) {
2933 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2934 leaf->map_token = NULL;
2937 /* shift the data */
2939 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2940 data_end + size_diff, btrfs_leaf_data(leaf) +
2941 data_end, old_data_start + new_size - data_end);
2943 struct btrfs_disk_key disk_key;
2946 btrfs_item_key(leaf, &disk_key, slot);
2948 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2950 struct btrfs_file_extent_item *fi;
2952 fi = btrfs_item_ptr(leaf, slot,
2953 struct btrfs_file_extent_item);
2954 fi = (struct btrfs_file_extent_item *)(
2955 (unsigned long)fi - size_diff);
2957 if (btrfs_file_extent_type(leaf, fi) ==
2958 BTRFS_FILE_EXTENT_INLINE) {
2959 ptr = btrfs_item_ptr_offset(leaf, slot);
2960 memmove_extent_buffer(leaf, ptr,
2962 offsetof(struct btrfs_file_extent_item,
2967 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2968 data_end + size_diff, btrfs_leaf_data(leaf) +
2969 data_end, old_data_start - data_end);
2971 offset = btrfs_disk_key_offset(&disk_key);
2972 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2973 btrfs_set_item_key(leaf, &disk_key, slot);
2975 fixup_low_keys(trans, root, path, &disk_key, 1);
2978 item = btrfs_item_nr(leaf, slot);
2979 btrfs_set_item_size(leaf, item, new_size);
2980 btrfs_mark_buffer_dirty(leaf);
2983 if (btrfs_leaf_free_space(root, leaf) < 0) {
2984 btrfs_print_leaf(root, leaf);
2991 * make the item pointed to by the path bigger, data_size is the new size.
2993 int btrfs_extend_item(struct btrfs_trans_handle *trans,
2994 struct btrfs_root *root, struct btrfs_path *path,
3000 struct extent_buffer *leaf;
3001 struct btrfs_item *item;
3003 unsigned int data_end;
3004 unsigned int old_data;
3005 unsigned int old_size;
3008 slot_orig = path->slots[0];
3009 leaf = path->nodes[0];
3011 nritems = btrfs_header_nritems(leaf);
3012 data_end = leaf_data_end(root, leaf);
3014 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3015 btrfs_print_leaf(root, leaf);
3018 slot = path->slots[0];
3019 old_data = btrfs_item_end_nr(leaf, slot);
3022 if (slot >= nritems) {
3023 btrfs_print_leaf(root, leaf);
3024 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3030 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3032 /* first correct the data pointers */
3033 for (i = slot; i < nritems; i++) {
3035 item = btrfs_item_nr(leaf, i);
3037 if (!leaf->map_token) {
3038 map_extent_buffer(leaf, (unsigned long)item,
3039 sizeof(struct btrfs_item),
3040 &leaf->map_token, &leaf->kaddr,
3041 &leaf->map_start, &leaf->map_len,
3044 ioff = btrfs_item_offset(leaf, item);
3045 btrfs_set_item_offset(leaf, item, ioff - data_size);
3048 if (leaf->map_token) {
3049 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3050 leaf->map_token = NULL;
3053 /* shift the data */
3054 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3055 data_end - data_size, btrfs_leaf_data(leaf) +
3056 data_end, old_data - data_end);
3058 data_end = old_data;
3059 old_size = btrfs_item_size_nr(leaf, slot);
3060 item = btrfs_item_nr(leaf, slot);
3061 btrfs_set_item_size(leaf, item, old_size + data_size);
3062 btrfs_mark_buffer_dirty(leaf);
3065 if (btrfs_leaf_free_space(root, leaf) < 0) {
3066 btrfs_print_leaf(root, leaf);
3073 * Given a key and some data, insert items into the tree.
3074 * This does all the path init required, making room in the tree if needed.
3075 * Returns the number of keys that were inserted.
3077 int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3078 struct btrfs_root *root,
3079 struct btrfs_path *path,
3080 struct btrfs_key *cpu_key, u32 *data_size,
3083 struct extent_buffer *leaf;
3084 struct btrfs_item *item;
3091 unsigned int data_end;
3092 struct btrfs_disk_key disk_key;
3093 struct btrfs_key found_key;
3095 for (i = 0; i < nr; i++) {
3096 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3097 BTRFS_LEAF_DATA_SIZE(root)) {
3101 total_data += data_size[i];
3102 total_size += data_size[i] + sizeof(struct btrfs_item);
3106 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3112 leaf = path->nodes[0];
3114 nritems = btrfs_header_nritems(leaf);
3115 data_end = leaf_data_end(root, leaf);
3117 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3118 for (i = nr; i >= 0; i--) {
3119 total_data -= data_size[i];
3120 total_size -= data_size[i] + sizeof(struct btrfs_item);
3121 if (total_size < btrfs_leaf_free_space(root, leaf))
3127 slot = path->slots[0];
3130 if (slot != nritems) {
3131 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3133 item = btrfs_item_nr(leaf, slot);
3134 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3136 /* figure out how many keys we can insert in here */
3137 total_data = data_size[0];
3138 for (i = 1; i < nr; i++) {
3139 if (comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3141 total_data += data_size[i];
3145 if (old_data < data_end) {
3146 btrfs_print_leaf(root, leaf);
3147 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3148 slot, old_data, data_end);
3152 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3154 /* first correct the data pointers */
3155 WARN_ON(leaf->map_token);
3156 for (i = slot; i < nritems; i++) {
3159 item = btrfs_item_nr(leaf, i);
3160 if (!leaf->map_token) {
3161 map_extent_buffer(leaf, (unsigned long)item,
3162 sizeof(struct btrfs_item),
3163 &leaf->map_token, &leaf->kaddr,
3164 &leaf->map_start, &leaf->map_len,
3168 ioff = btrfs_item_offset(leaf, item);
3169 btrfs_set_item_offset(leaf, item, ioff - total_data);
3171 if (leaf->map_token) {
3172 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3173 leaf->map_token = NULL;
3176 /* shift the items */
3177 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3178 btrfs_item_nr_offset(slot),
3179 (nritems - slot) * sizeof(struct btrfs_item));
3181 /* shift the data */
3182 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3183 data_end - total_data, btrfs_leaf_data(leaf) +
3184 data_end, old_data - data_end);
3185 data_end = old_data;
3188 * this sucks but it has to be done, if we are inserting at
3189 * the end of the leaf only insert 1 of the items, since we
3190 * have no way of knowing whats on the next leaf and we'd have
3191 * to drop our current locks to figure it out
3196 /* setup the item for the new data */
3197 for (i = 0; i < nr; i++) {
3198 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3199 btrfs_set_item_key(leaf, &disk_key, slot + i);
3200 item = btrfs_item_nr(leaf, slot + i);
3201 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3202 data_end -= data_size[i];
3203 btrfs_set_item_size(leaf, item, data_size[i]);
3205 btrfs_set_header_nritems(leaf, nritems + nr);
3206 btrfs_mark_buffer_dirty(leaf);
3210 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3211 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3214 if (btrfs_leaf_free_space(root, leaf) < 0) {
3215 btrfs_print_leaf(root, leaf);
3225 * Given a key and some data, insert items into the tree.
3226 * This does all the path init required, making room in the tree if needed.
3228 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3229 struct btrfs_root *root,
3230 struct btrfs_path *path,
3231 struct btrfs_key *cpu_key, u32 *data_size,
3234 struct extent_buffer *leaf;
3235 struct btrfs_item *item;
3243 unsigned int data_end;
3244 struct btrfs_disk_key disk_key;
3246 for (i = 0; i < nr; i++)
3247 total_data += data_size[i];
3249 total_size = total_data + (nr * sizeof(struct btrfs_item));
3250 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3256 slot_orig = path->slots[0];
3257 leaf = path->nodes[0];
3259 nritems = btrfs_header_nritems(leaf);
3260 data_end = leaf_data_end(root, leaf);
3262 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3263 btrfs_print_leaf(root, leaf);
3264 printk(KERN_CRIT "not enough freespace need %u have %d\n",
3265 total_size, btrfs_leaf_free_space(root, leaf));
3269 slot = path->slots[0];
3272 if (slot != nritems) {
3273 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3275 if (old_data < data_end) {
3276 btrfs_print_leaf(root, leaf);
3277 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3278 slot, old_data, data_end);
3282 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3284 /* first correct the data pointers */
3285 WARN_ON(leaf->map_token);
3286 for (i = slot; i < nritems; i++) {
3289 item = btrfs_item_nr(leaf, i);
3290 if (!leaf->map_token) {
3291 map_extent_buffer(leaf, (unsigned long)item,
3292 sizeof(struct btrfs_item),
3293 &leaf->map_token, &leaf->kaddr,
3294 &leaf->map_start, &leaf->map_len,
3298 ioff = btrfs_item_offset(leaf, item);
3299 btrfs_set_item_offset(leaf, item, ioff - total_data);
3301 if (leaf->map_token) {
3302 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3303 leaf->map_token = NULL;
3306 /* shift the items */
3307 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3308 btrfs_item_nr_offset(slot),
3309 (nritems - slot) * sizeof(struct btrfs_item));
3311 /* shift the data */
3312 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3313 data_end - total_data, btrfs_leaf_data(leaf) +
3314 data_end, old_data - data_end);
3315 data_end = old_data;
3318 /* setup the item for the new data */
3319 for (i = 0; i < nr; i++) {
3320 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3321 btrfs_set_item_key(leaf, &disk_key, slot + i);
3322 item = btrfs_item_nr(leaf, slot + i);
3323 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3324 data_end -= data_size[i];
3325 btrfs_set_item_size(leaf, item, data_size[i]);
3327 btrfs_set_header_nritems(leaf, nritems + nr);
3328 btrfs_mark_buffer_dirty(leaf);
3332 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3333 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3336 if (btrfs_leaf_free_space(root, leaf) < 0) {
3337 btrfs_print_leaf(root, leaf);
3345 * Given a key and some data, insert an item into the tree.
3346 * This does all the path init required, making room in the tree if needed.
3348 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3349 *root, struct btrfs_key *cpu_key, void *data, u32
3353 struct btrfs_path *path;
3354 struct extent_buffer *leaf;
3357 path = btrfs_alloc_path();
3359 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
3361 leaf = path->nodes[0];
3362 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3363 write_extent_buffer(leaf, data, ptr, data_size);
3364 btrfs_mark_buffer_dirty(leaf);
3366 btrfs_free_path(path);
3371 * delete the pointer from a given node.
3373 * the tree should have been previously balanced so the deletion does not
3376 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3377 struct btrfs_path *path, int level, int slot)
3379 struct extent_buffer *parent = path->nodes[level];
3384 nritems = btrfs_header_nritems(parent);
3385 if (slot != nritems - 1) {
3386 memmove_extent_buffer(parent,
3387 btrfs_node_key_ptr_offset(slot),
3388 btrfs_node_key_ptr_offset(slot + 1),
3389 sizeof(struct btrfs_key_ptr) *
3390 (nritems - slot - 1));
3393 btrfs_set_header_nritems(parent, nritems);
3394 if (nritems == 0 && parent == root->node) {
3395 BUG_ON(btrfs_header_level(root->node) != 1);
3396 /* just turn the root into a leaf and break */
3397 btrfs_set_header_level(root->node, 0);
3398 } else if (slot == 0) {
3399 struct btrfs_disk_key disk_key;
3401 btrfs_node_key(parent, &disk_key, 0);
3402 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
3406 btrfs_mark_buffer_dirty(parent);
3411 * a helper function to delete the leaf pointed to by path->slots[1] and
3412 * path->nodes[1]. bytenr is the node block pointer, but since the callers
3413 * already know it, it is faster to have them pass it down than to
3414 * read it out of the node again.
3416 * This deletes the pointer in path->nodes[1] and frees the leaf
3417 * block extent. zero is returned if it all worked out, < 0 otherwise.
3419 * The path must have already been setup for deleting the leaf, including
3420 * all the proper balancing. path->nodes[1] must be locked.
3422 noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3423 struct btrfs_root *root,
3424 struct btrfs_path *path, u64 bytenr)
3427 u64 root_gen = btrfs_header_generation(path->nodes[1]);
3429 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3433 ret = btrfs_free_extent(trans, root, bytenr,
3434 btrfs_level_size(root, 0),
3435 path->nodes[1]->start,
3436 btrfs_header_owner(path->nodes[1]),
3441 * delete the item at the leaf level in path. If that empties
3442 * the leaf, remove it from the tree
3444 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3445 struct btrfs_path *path, int slot, int nr)
3447 struct extent_buffer *leaf;
3448 struct btrfs_item *item;
3456 leaf = path->nodes[0];
3457 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3459 for (i = 0; i < nr; i++)
3460 dsize += btrfs_item_size_nr(leaf, slot + i);
3462 nritems = btrfs_header_nritems(leaf);
3464 if (slot + nr != nritems) {
3465 int data_end = leaf_data_end(root, leaf);
3467 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3469 btrfs_leaf_data(leaf) + data_end,
3470 last_off - data_end);
3472 for (i = slot + nr; i < nritems; i++) {
3475 item = btrfs_item_nr(leaf, i);
3476 if (!leaf->map_token) {
3477 map_extent_buffer(leaf, (unsigned long)item,
3478 sizeof(struct btrfs_item),
3479 &leaf->map_token, &leaf->kaddr,
3480 &leaf->map_start, &leaf->map_len,
3483 ioff = btrfs_item_offset(leaf, item);
3484 btrfs_set_item_offset(leaf, item, ioff + dsize);
3487 if (leaf->map_token) {
3488 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3489 leaf->map_token = NULL;
3492 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
3493 btrfs_item_nr_offset(slot + nr),
3494 sizeof(struct btrfs_item) *
3495 (nritems - slot - nr));
3497 btrfs_set_header_nritems(leaf, nritems - nr);
3500 /* delete the leaf if we've emptied it */
3502 if (leaf == root->node) {
3503 btrfs_set_header_level(leaf, 0);
3505 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3509 int used = leaf_space_used(leaf, 0, nritems);
3511 struct btrfs_disk_key disk_key;
3513 btrfs_item_key(leaf, &disk_key, 0);
3514 wret = fixup_low_keys(trans, root, path,
3520 /* delete the leaf if it is mostly empty */
3521 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
3522 /* push_leaf_left fixes the path.
3523 * make sure the path still points to our leaf
3524 * for possible call to del_ptr below
3526 slot = path->slots[1];
3527 extent_buffer_get(leaf);
3529 wret = push_leaf_left(trans, root, path, 1, 1);
3530 if (wret < 0 && wret != -ENOSPC)
3533 if (path->nodes[0] == leaf &&
3534 btrfs_header_nritems(leaf)) {
3535 wret = push_leaf_right(trans, root, path, 1, 1);
3536 if (wret < 0 && wret != -ENOSPC)
3540 if (btrfs_header_nritems(leaf) == 0) {
3541 path->slots[1] = slot;
3542 ret = btrfs_del_leaf(trans, root, path,
3545 free_extent_buffer(leaf);
3547 /* if we're still in the path, make sure
3548 * we're dirty. Otherwise, one of the
3549 * push_leaf functions must have already
3550 * dirtied this buffer
3552 if (path->nodes[0] == leaf)
3553 btrfs_mark_buffer_dirty(leaf);
3554 free_extent_buffer(leaf);
3557 btrfs_mark_buffer_dirty(leaf);
3564 * search the tree again to find a leaf with lesser keys
3565 * returns 0 if it found something or 1 if there are no lesser leaves.
3566 * returns < 0 on io errors.
3568 * This may release the path, and so you may lose any locks held at the
3571 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3573 struct btrfs_key key;
3574 struct btrfs_disk_key found_key;
3577 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
3581 else if (key.type > 0)
3583 else if (key.objectid > 0)
3588 btrfs_release_path(root, path);
3589 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3592 btrfs_item_key(path->nodes[0], &found_key, 0);
3593 ret = comp_keys(&found_key, &key);
3600 * A helper function to walk down the tree starting at min_key, and looking
3601 * for nodes or leaves that are either in cache or have a minimum
3602 * transaction id. This is used by the btree defrag code, and tree logging
3604 * This does not cow, but it does stuff the starting key it finds back
3605 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3606 * key and get a writable path.
3608 * This does lock as it descends, and path->keep_locks should be set
3609 * to 1 by the caller.
3611 * This honors path->lowest_level to prevent descent past a given level
3614 * min_trans indicates the oldest transaction that you are interested
3615 * in walking through. Any nodes or leaves older than min_trans are
3616 * skipped over (without reading them).
3618 * returns zero if something useful was found, < 0 on error and 1 if there
3619 * was nothing in the tree that matched the search criteria.
3621 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
3622 struct btrfs_key *max_key,
3623 struct btrfs_path *path, int cache_only,
3626 struct extent_buffer *cur;
3627 struct btrfs_key found_key;
3634 WARN_ON(!path->keep_locks);
3636 cur = btrfs_lock_root_node(root);
3637 level = btrfs_header_level(cur);
3638 WARN_ON(path->nodes[level]);
3639 path->nodes[level] = cur;
3640 path->locks[level] = 1;
3642 if (btrfs_header_generation(cur) < min_trans) {
3647 nritems = btrfs_header_nritems(cur);
3648 level = btrfs_header_level(cur);
3649 sret = bin_search(cur, min_key, level, &slot);
3651 /* at the lowest level, we're done, setup the path and exit */
3652 if (level == path->lowest_level) {
3653 if (slot >= nritems)
3656 path->slots[level] = slot;
3657 btrfs_item_key_to_cpu(cur, &found_key, slot);
3660 if (sret && slot > 0)
3663 * check this node pointer against the cache_only and
3664 * min_trans parameters. If it isn't in cache or is too
3665 * old, skip to the next one.
3667 while (slot < nritems) {
3670 struct extent_buffer *tmp;
3671 struct btrfs_disk_key disk_key;
3673 blockptr = btrfs_node_blockptr(cur, slot);
3674 gen = btrfs_node_ptr_generation(cur, slot);
3675 if (gen < min_trans) {
3683 btrfs_node_key(cur, &disk_key, slot);
3684 if (comp_keys(&disk_key, max_key) >= 0) {
3690 tmp = btrfs_find_tree_block(root, blockptr,
3691 btrfs_level_size(root, level - 1));
3693 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3694 free_extent_buffer(tmp);
3698 free_extent_buffer(tmp);
3703 * we didn't find a candidate key in this node, walk forward
3704 * and find another one
3706 if (slot >= nritems) {
3707 path->slots[level] = slot;
3708 sret = btrfs_find_next_key(root, path, min_key, level,
3709 cache_only, min_trans);
3711 btrfs_release_path(root, path);
3717 /* save our key for returning back */
3718 btrfs_node_key_to_cpu(cur, &found_key, slot);
3719 path->slots[level] = slot;
3720 if (level == path->lowest_level) {
3722 unlock_up(path, level, 1);
3725 cur = read_node_slot(root, cur, slot);
3727 btrfs_tree_lock(cur);
3728 path->locks[level - 1] = 1;
3729 path->nodes[level - 1] = cur;
3730 unlock_up(path, level, 1);
3734 memcpy(min_key, &found_key, sizeof(found_key));
3739 * this is similar to btrfs_next_leaf, but does not try to preserve
3740 * and fixup the path. It looks for and returns the next key in the
3741 * tree based on the current path and the cache_only and min_trans
3744 * 0 is returned if another key is found, < 0 if there are any errors
3745 * and 1 is returned if there are no higher keys in the tree
3747 * path->keep_locks should be set to 1 on the search made before
3748 * calling this function.
3750 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
3751 struct btrfs_key *key, int lowest_level,
3752 int cache_only, u64 min_trans)
3754 int level = lowest_level;
3756 struct extent_buffer *c;
3758 WARN_ON(!path->keep_locks);
3759 while (level < BTRFS_MAX_LEVEL) {
3760 if (!path->nodes[level])
3763 slot = path->slots[level] + 1;
3764 c = path->nodes[level];
3766 if (slot >= btrfs_header_nritems(c)) {
3768 if (level == BTRFS_MAX_LEVEL)
3773 btrfs_item_key_to_cpu(c, key, slot);
3775 u64 blockptr = btrfs_node_blockptr(c, slot);
3776 u64 gen = btrfs_node_ptr_generation(c, slot);
3779 struct extent_buffer *cur;
3780 cur = btrfs_find_tree_block(root, blockptr,
3781 btrfs_level_size(root, level - 1));
3782 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3785 free_extent_buffer(cur);
3788 free_extent_buffer(cur);
3790 if (gen < min_trans) {
3794 btrfs_node_key_to_cpu(c, key, slot);
3802 * search the tree again to find a leaf with greater keys
3803 * returns 0 if it found something or 1 if there are no greater leaves.
3804 * returns < 0 on io errors.
3806 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
3810 struct extent_buffer *c;
3811 struct extent_buffer *next = NULL;
3812 struct btrfs_key key;
3816 nritems = btrfs_header_nritems(path->nodes[0]);
3820 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3822 btrfs_release_path(root, path);
3823 path->keep_locks = 1;
3824 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3825 path->keep_locks = 0;
3830 nritems = btrfs_header_nritems(path->nodes[0]);
3832 * by releasing the path above we dropped all our locks. A balance
3833 * could have added more items next to the key that used to be
3834 * at the very end of the block. So, check again here and
3835 * advance the path if there are now more items available.
3837 if (nritems > 0 && path->slots[0] < nritems - 1) {
3842 while (level < BTRFS_MAX_LEVEL) {
3843 if (!path->nodes[level])
3846 slot = path->slots[level] + 1;
3847 c = path->nodes[level];
3848 if (slot >= btrfs_header_nritems(c)) {
3850 if (level == BTRFS_MAX_LEVEL)
3856 btrfs_tree_unlock(next);
3857 free_extent_buffer(next);
3860 if (level == 1 && (path->locks[1] || path->skip_locking) &&
3862 reada_for_search(root, path, level, slot, 0);
3864 next = read_node_slot(root, c, slot);
3865 if (!path->skip_locking) {
3866 WARN_ON(!btrfs_tree_locked(c));
3867 btrfs_tree_lock(next);
3871 path->slots[level] = slot;
3874 c = path->nodes[level];
3875 if (path->locks[level])
3876 btrfs_tree_unlock(c);
3877 free_extent_buffer(c);
3878 path->nodes[level] = next;
3879 path->slots[level] = 0;
3880 if (!path->skip_locking)
3881 path->locks[level] = 1;
3884 if (level == 1 && path->locks[1] && path->reada)
3885 reada_for_search(root, path, level, slot, 0);
3886 next = read_node_slot(root, next, 0);
3887 if (!path->skip_locking) {
3888 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3889 btrfs_tree_lock(next);
3893 unlock_up(path, 0, 1);
3898 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
3899 * searching until it gets past min_objectid or finds an item of 'type'
3901 * returns 0 if something is found, 1 if nothing was found and < 0 on error
3903 int btrfs_previous_item(struct btrfs_root *root,
3904 struct btrfs_path *path, u64 min_objectid,
3907 struct btrfs_key found_key;
3908 struct extent_buffer *leaf;
3913 if (path->slots[0] == 0) {
3914 ret = btrfs_prev_leaf(root, path);
3920 leaf = path->nodes[0];
3921 nritems = btrfs_header_nritems(leaf);
3924 if (path->slots[0] == nritems)
3927 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3928 if (found_key.type == type)
3930 if (found_key.objectid < min_objectid)
3932 if (found_key.objectid == min_objectid &&
3933 found_key.type < type)