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 void noinline 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 WARN_ON(btrfs_header_generation(buf) > trans->transid);
189 ret = btrfs_inc_ref(trans, new_root, buf, cow, NULL);
195 btrfs_mark_buffer_dirty(cow);
201 * does the dirty work in cow of a single block. The parent block
202 * (if supplied) is updated to point to the new cow copy. The new
203 * buffer is marked dirty and returned locked. If you modify the block
204 * it needs to be marked dirty again.
206 * search_start -- an allocation hint for the new block
208 * empty_size -- a hint that you plan on doing more cow. This is the size in bytes
209 * the allocator should try to find free next to the block it returns. This is
210 * just a hint and may be ignored by the allocator.
212 * prealloc_dest -- if you have already reserved a destination for the cow,
213 * this uses that block instead of allocating a new one. btrfs_alloc_reserved_extent
214 * is used to finish the allocation.
216 int noinline __btrfs_cow_block(struct btrfs_trans_handle *trans,
217 struct btrfs_root *root,
218 struct extent_buffer *buf,
219 struct extent_buffer *parent, int parent_slot,
220 struct extent_buffer **cow_ret,
221 u64 search_start, u64 empty_size,
225 struct extent_buffer *cow;
234 WARN_ON(!btrfs_tree_locked(buf));
237 parent_start = parent->start;
241 WARN_ON(root->ref_cows && trans->transid !=
242 root->fs_info->running_transaction->transid);
243 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
245 level = btrfs_header_level(buf);
246 nritems = btrfs_header_nritems(buf);
249 struct btrfs_key ins;
251 ins.objectid = prealloc_dest;
252 ins.offset = buf->len;
253 ins.type = BTRFS_EXTENT_ITEM_KEY;
255 ret = btrfs_alloc_reserved_extent(trans, root, parent_start,
256 root->root_key.objectid,
257 trans->transid, level, &ins);
259 cow = btrfs_init_new_buffer(trans, root, prealloc_dest,
262 cow = btrfs_alloc_free_block(trans, root, buf->len,
264 root->root_key.objectid,
265 trans->transid, level,
266 search_start, empty_size);
271 copy_extent_buffer(cow, buf, 0, 0, cow->len);
272 btrfs_set_header_bytenr(cow, cow->start);
273 btrfs_set_header_generation(cow, trans->transid);
274 btrfs_set_header_owner(cow, root->root_key.objectid);
275 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
277 WARN_ON(btrfs_header_generation(buf) > trans->transid);
278 if (btrfs_header_generation(buf) != trans->transid) {
280 ret = btrfs_inc_ref(trans, root, buf, cow, &nr_extents);
284 ret = btrfs_cache_ref(trans, root, buf, nr_extents);
286 } else if (btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID) {
288 * There are only two places that can drop reference to
289 * tree blocks owned by living reloc trees, one is here,
290 * the other place is btrfs_merge_path. In both places,
291 * we check reference count while tree block is locked.
292 * Furthermore, if reference count is one, it won't get
293 * increased by someone else.
296 ret = btrfs_lookup_extent_ref(trans, root, buf->start,
300 ret = btrfs_update_ref(trans, root, buf, cow,
302 clean_tree_block(trans, root, buf);
304 ret = btrfs_inc_ref(trans, root, buf, cow, NULL);
308 ret = btrfs_update_ref(trans, root, buf, cow, 0, nritems);
311 clean_tree_block(trans, root, buf);
314 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
315 ret = btrfs_add_reloc_mapping(root, buf->start,
316 buf->len, cow->start);
318 ret = btrfs_reloc_tree_cache_ref(trans, root, cow, buf->start);
322 if (buf == root->node) {
323 WARN_ON(parent && parent != buf);
325 spin_lock(&root->node_lock);
327 extent_buffer_get(cow);
328 spin_unlock(&root->node_lock);
330 if (buf != root->commit_root) {
331 btrfs_free_extent(trans, root, buf->start,
332 buf->len, buf->start,
333 root->root_key.objectid,
334 btrfs_header_generation(buf),
337 free_extent_buffer(buf);
338 add_root_to_dirty_list(root);
340 btrfs_set_node_blockptr(parent, parent_slot,
342 WARN_ON(trans->transid == 0);
343 btrfs_set_node_ptr_generation(parent, parent_slot,
345 btrfs_mark_buffer_dirty(parent);
346 WARN_ON(btrfs_header_generation(parent) != trans->transid);
347 btrfs_free_extent(trans, root, buf->start, buf->len,
348 parent_start, btrfs_header_owner(parent),
349 btrfs_header_generation(parent), level, 1);
352 btrfs_tree_unlock(buf);
353 free_extent_buffer(buf);
354 btrfs_mark_buffer_dirty(cow);
360 * cows a single block, see __btrfs_cow_block for the real work.
361 * This version of it has extra checks so that a block isn't cow'd more than
362 * once per transaction, as long as it hasn't been written yet
364 int noinline btrfs_cow_block(struct btrfs_trans_handle *trans,
365 struct btrfs_root *root, struct extent_buffer *buf,
366 struct extent_buffer *parent, int parent_slot,
367 struct extent_buffer **cow_ret, u64 prealloc_dest)
372 if (trans->transaction != root->fs_info->running_transaction) {
373 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
374 root->fs_info->running_transaction->transid);
377 if (trans->transid != root->fs_info->generation) {
378 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
379 root->fs_info->generation);
383 spin_lock(&root->fs_info->hash_lock);
384 if (btrfs_header_generation(buf) == trans->transid &&
385 btrfs_header_owner(buf) == root->root_key.objectid &&
386 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
388 spin_unlock(&root->fs_info->hash_lock);
389 WARN_ON(prealloc_dest);
392 spin_unlock(&root->fs_info->hash_lock);
393 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
394 ret = __btrfs_cow_block(trans, root, buf, parent,
395 parent_slot, cow_ret, search_start, 0,
401 * helper function for defrag to decide if two blocks pointed to by a
402 * node are actually close by
404 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
406 if (blocknr < other && other - (blocknr + blocksize) < 32768)
408 if (blocknr > other && blocknr - (other + blocksize) < 32768)
414 * compare two keys in a memcmp fashion
416 static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
420 btrfs_disk_key_to_cpu(&k1, disk);
422 if (k1.objectid > k2->objectid)
424 if (k1.objectid < k2->objectid)
426 if (k1.type > k2->type)
428 if (k1.type < k2->type)
430 if (k1.offset > k2->offset)
432 if (k1.offset < k2->offset)
439 * this is used by the defrag code to go through all the
440 * leaves pointed to by a node and reallocate them so that
441 * disk order is close to key order
443 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
444 struct btrfs_root *root, struct extent_buffer *parent,
445 int start_slot, int cache_only, u64 *last_ret,
446 struct btrfs_key *progress)
448 struct extent_buffer *cur;
451 u64 search_start = *last_ret;
461 int progress_passed = 0;
462 struct btrfs_disk_key disk_key;
464 parent_level = btrfs_header_level(parent);
465 if (cache_only && parent_level != 1)
468 if (trans->transaction != root->fs_info->running_transaction) {
469 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
470 root->fs_info->running_transaction->transid);
473 if (trans->transid != root->fs_info->generation) {
474 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
475 root->fs_info->generation);
479 parent_nritems = btrfs_header_nritems(parent);
480 blocksize = btrfs_level_size(root, parent_level - 1);
481 end_slot = parent_nritems;
483 if (parent_nritems == 1)
486 for (i = start_slot; i < end_slot; i++) {
489 if (!parent->map_token) {
490 map_extent_buffer(parent,
491 btrfs_node_key_ptr_offset(i),
492 sizeof(struct btrfs_key_ptr),
493 &parent->map_token, &parent->kaddr,
494 &parent->map_start, &parent->map_len,
497 btrfs_node_key(parent, &disk_key, i);
498 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
502 blocknr = btrfs_node_blockptr(parent, i);
503 gen = btrfs_node_ptr_generation(parent, i);
505 last_block = blocknr;
508 other = btrfs_node_blockptr(parent, i - 1);
509 close = close_blocks(blocknr, other, blocksize);
511 if (!close && i < end_slot - 2) {
512 other = btrfs_node_blockptr(parent, i + 1);
513 close = close_blocks(blocknr, other, blocksize);
516 last_block = blocknr;
519 if (parent->map_token) {
520 unmap_extent_buffer(parent, parent->map_token,
522 parent->map_token = NULL;
525 cur = btrfs_find_tree_block(root, blocknr, blocksize);
527 uptodate = btrfs_buffer_uptodate(cur, gen);
530 if (!cur || !uptodate) {
532 free_extent_buffer(cur);
536 cur = read_tree_block(root, blocknr,
538 } else if (!uptodate) {
539 btrfs_read_buffer(cur, gen);
542 if (search_start == 0)
543 search_start = last_block;
545 btrfs_tree_lock(cur);
546 err = __btrfs_cow_block(trans, root, cur, parent, i,
549 (end_slot - i) * blocksize), 0);
551 btrfs_tree_unlock(cur);
552 free_extent_buffer(cur);
555 search_start = cur->start;
556 last_block = cur->start;
557 *last_ret = search_start;
558 btrfs_tree_unlock(cur);
559 free_extent_buffer(cur);
561 if (parent->map_token) {
562 unmap_extent_buffer(parent, parent->map_token,
564 parent->map_token = NULL;
570 * The leaf data grows from end-to-front in the node.
571 * this returns the address of the start of the last item,
572 * which is the stop of the leaf data stack
574 static inline unsigned int leaf_data_end(struct btrfs_root *root,
575 struct extent_buffer *leaf)
577 u32 nr = btrfs_header_nritems(leaf);
579 return BTRFS_LEAF_DATA_SIZE(root);
580 return btrfs_item_offset_nr(leaf, nr - 1);
584 * extra debugging checks to make sure all the items in a key are
585 * well formed and in the proper order
587 static int check_node(struct btrfs_root *root, struct btrfs_path *path,
590 struct extent_buffer *parent = NULL;
591 struct extent_buffer *node = path->nodes[level];
592 struct btrfs_disk_key parent_key;
593 struct btrfs_disk_key node_key;
596 struct btrfs_key cpukey;
597 u32 nritems = btrfs_header_nritems(node);
599 if (path->nodes[level + 1])
600 parent = path->nodes[level + 1];
602 slot = path->slots[level];
603 BUG_ON(nritems == 0);
605 parent_slot = path->slots[level + 1];
606 btrfs_node_key(parent, &parent_key, parent_slot);
607 btrfs_node_key(node, &node_key, 0);
608 BUG_ON(memcmp(&parent_key, &node_key,
609 sizeof(struct btrfs_disk_key)));
610 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
611 btrfs_header_bytenr(node));
613 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
615 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
616 btrfs_node_key(node, &node_key, slot);
617 BUG_ON(comp_keys(&node_key, &cpukey) <= 0);
619 if (slot < nritems - 1) {
620 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
621 btrfs_node_key(node, &node_key, slot);
622 BUG_ON(comp_keys(&node_key, &cpukey) >= 0);
628 * extra checking to make sure all the items in a leaf are
629 * well formed and in the proper order
631 static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
634 struct extent_buffer *leaf = path->nodes[level];
635 struct extent_buffer *parent = NULL;
637 struct btrfs_key cpukey;
638 struct btrfs_disk_key parent_key;
639 struct btrfs_disk_key leaf_key;
640 int slot = path->slots[0];
642 u32 nritems = btrfs_header_nritems(leaf);
644 if (path->nodes[level + 1])
645 parent = path->nodes[level + 1];
651 parent_slot = path->slots[level + 1];
652 btrfs_node_key(parent, &parent_key, parent_slot);
653 btrfs_item_key(leaf, &leaf_key, 0);
655 BUG_ON(memcmp(&parent_key, &leaf_key,
656 sizeof(struct btrfs_disk_key)));
657 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
658 btrfs_header_bytenr(leaf));
661 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
662 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
663 btrfs_item_key(leaf, &leaf_key, i);
664 if (comp_keys(&leaf_key, &cpukey) >= 0) {
665 btrfs_print_leaf(root, leaf);
666 printk("slot %d offset bad key\n", i);
669 if (btrfs_item_offset_nr(leaf, i) !=
670 btrfs_item_end_nr(leaf, i + 1)) {
671 btrfs_print_leaf(root, leaf);
672 printk("slot %d offset bad\n", i);
676 if (btrfs_item_offset_nr(leaf, i) +
677 btrfs_item_size_nr(leaf, i) !=
678 BTRFS_LEAF_DATA_SIZE(root)) {
679 btrfs_print_leaf(root, leaf);
680 printk("slot %d first offset bad\n", i);
686 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
687 btrfs_print_leaf(root, leaf);
688 printk("slot %d bad size \n", nritems - 1);
693 if (slot != 0 && slot < nritems - 1) {
694 btrfs_item_key(leaf, &leaf_key, slot);
695 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
696 if (comp_keys(&leaf_key, &cpukey) <= 0) {
697 btrfs_print_leaf(root, leaf);
698 printk("slot %d offset bad key\n", slot);
701 if (btrfs_item_offset_nr(leaf, slot - 1) !=
702 btrfs_item_end_nr(leaf, slot)) {
703 btrfs_print_leaf(root, leaf);
704 printk("slot %d offset bad\n", slot);
708 if (slot < nritems - 1) {
709 btrfs_item_key(leaf, &leaf_key, slot);
710 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
711 BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0);
712 if (btrfs_item_offset_nr(leaf, slot) !=
713 btrfs_item_end_nr(leaf, slot + 1)) {
714 btrfs_print_leaf(root, leaf);
715 printk("slot %d offset bad\n", slot);
719 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
720 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
724 static int noinline check_block(struct btrfs_root *root,
725 struct btrfs_path *path, int level)
729 if (btrfs_header_level(path->nodes[level]) != level)
730 printk("warning: bad level %Lu wanted %d found %d\n",
731 path->nodes[level]->start, level,
732 btrfs_header_level(path->nodes[level]));
733 found_start = btrfs_header_bytenr(path->nodes[level]);
734 if (found_start != path->nodes[level]->start) {
735 printk("warning: bad bytentr %Lu found %Lu\n",
736 path->nodes[level]->start, found_start);
739 struct extent_buffer *buf = path->nodes[level];
741 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
742 (unsigned long)btrfs_header_fsid(buf),
744 printk("warning bad block %Lu\n", buf->start);
749 return check_leaf(root, path, level);
750 return check_node(root, path, level);
754 * search for key in the extent_buffer. The items start at offset p,
755 * and they are item_size apart. There are 'max' items in p.
757 * the slot in the array is returned via slot, and it points to
758 * the place where you would insert key if it is not found in
761 * slot may point to max if the key is bigger than all of the keys
763 static noinline int generic_bin_search(struct extent_buffer *eb,
765 int item_size, struct btrfs_key *key,
772 struct btrfs_disk_key *tmp = NULL;
773 struct btrfs_disk_key unaligned;
774 unsigned long offset;
775 char *map_token = NULL;
777 unsigned long map_start = 0;
778 unsigned long map_len = 0;
782 mid = (low + high) / 2;
783 offset = p + mid * item_size;
785 if (!map_token || offset < map_start ||
786 (offset + sizeof(struct btrfs_disk_key)) >
787 map_start + map_len) {
789 unmap_extent_buffer(eb, map_token, KM_USER0);
792 err = map_extent_buffer(eb, offset,
793 sizeof(struct btrfs_disk_key),
795 &map_start, &map_len, KM_USER0);
798 tmp = (struct btrfs_disk_key *)(kaddr + offset -
801 read_extent_buffer(eb, &unaligned,
802 offset, sizeof(unaligned));
807 tmp = (struct btrfs_disk_key *)(kaddr + offset -
810 ret = comp_keys(tmp, key);
819 unmap_extent_buffer(eb, map_token, KM_USER0);
825 unmap_extent_buffer(eb, map_token, KM_USER0);
830 * simple bin_search frontend that does the right thing for
833 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
834 int level, int *slot)
837 return generic_bin_search(eb,
838 offsetof(struct btrfs_leaf, items),
839 sizeof(struct btrfs_item),
840 key, btrfs_header_nritems(eb),
843 return generic_bin_search(eb,
844 offsetof(struct btrfs_node, ptrs),
845 sizeof(struct btrfs_key_ptr),
846 key, btrfs_header_nritems(eb),
852 /* given a node and slot number, this reads the blocks it points to. The
853 * extent buffer is returned with a reference taken (but unlocked).
854 * NULL is returned on error.
856 static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
857 struct extent_buffer *parent, int slot)
859 int level = btrfs_header_level(parent);
862 if (slot >= btrfs_header_nritems(parent))
867 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
868 btrfs_level_size(root, level - 1),
869 btrfs_node_ptr_generation(parent, slot));
873 * node level balancing, used to make sure nodes are in proper order for
874 * item deletion. We balance from the top down, so we have to make sure
875 * that a deletion won't leave an node completely empty later on.
877 static noinline int balance_level(struct btrfs_trans_handle *trans,
878 struct btrfs_root *root,
879 struct btrfs_path *path, int level)
881 struct extent_buffer *right = NULL;
882 struct extent_buffer *mid;
883 struct extent_buffer *left = NULL;
884 struct extent_buffer *parent = NULL;
888 int orig_slot = path->slots[level];
889 int err_on_enospc = 0;
895 mid = path->nodes[level];
896 WARN_ON(!path->locks[level]);
897 WARN_ON(btrfs_header_generation(mid) != trans->transid);
899 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
901 if (level < BTRFS_MAX_LEVEL - 1)
902 parent = path->nodes[level + 1];
903 pslot = path->slots[level + 1];
906 * deal with the case where there is only one pointer in the root
907 * by promoting the node below to a root
910 struct extent_buffer *child;
912 if (btrfs_header_nritems(mid) != 1)
915 /* promote the child to a root */
916 child = read_node_slot(root, mid, 0);
917 btrfs_tree_lock(child);
919 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
922 spin_lock(&root->node_lock);
924 spin_unlock(&root->node_lock);
926 ret = btrfs_update_extent_ref(trans, root, child->start,
927 mid->start, child->start,
928 root->root_key.objectid,
929 trans->transid, level - 1);
932 add_root_to_dirty_list(root);
933 btrfs_tree_unlock(child);
934 path->locks[level] = 0;
935 path->nodes[level] = NULL;
936 clean_tree_block(trans, root, mid);
937 btrfs_tree_unlock(mid);
938 /* once for the path */
939 free_extent_buffer(mid);
940 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
941 mid->start, root->root_key.objectid,
942 btrfs_header_generation(mid),
944 /* once for the root ptr */
945 free_extent_buffer(mid);
948 if (btrfs_header_nritems(mid) >
949 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
952 if (btrfs_header_nritems(mid) < 2)
955 left = read_node_slot(root, parent, pslot - 1);
957 btrfs_tree_lock(left);
958 wret = btrfs_cow_block(trans, root, left,
959 parent, pslot - 1, &left, 0);
965 right = read_node_slot(root, parent, pslot + 1);
967 btrfs_tree_lock(right);
968 wret = btrfs_cow_block(trans, root, right,
969 parent, pslot + 1, &right, 0);
976 /* first, try to make some room in the middle buffer */
978 orig_slot += btrfs_header_nritems(left);
979 wret = push_node_left(trans, root, left, mid, 1);
982 if (btrfs_header_nritems(mid) < 2)
987 * then try to empty the right most buffer into the middle
990 wret = push_node_left(trans, root, mid, right, 1);
991 if (wret < 0 && wret != -ENOSPC)
993 if (btrfs_header_nritems(right) == 0) {
994 u64 bytenr = right->start;
995 u64 generation = btrfs_header_generation(parent);
996 u32 blocksize = right->len;
998 clean_tree_block(trans, root, right);
999 btrfs_tree_unlock(right);
1000 free_extent_buffer(right);
1002 wret = del_ptr(trans, root, path, level + 1, pslot +
1006 wret = btrfs_free_extent(trans, root, bytenr,
1007 blocksize, parent->start,
1008 btrfs_header_owner(parent),
1009 generation, level, 1);
1013 struct btrfs_disk_key right_key;
1014 btrfs_node_key(right, &right_key, 0);
1015 btrfs_set_node_key(parent, &right_key, pslot + 1);
1016 btrfs_mark_buffer_dirty(parent);
1019 if (btrfs_header_nritems(mid) == 1) {
1021 * we're not allowed to leave a node with one item in the
1022 * tree during a delete. A deletion from lower in the tree
1023 * could try to delete the only pointer in this node.
1024 * So, pull some keys from the left.
1025 * There has to be a left pointer at this point because
1026 * otherwise we would have pulled some pointers from the
1030 wret = balance_node_right(trans, root, mid, left);
1036 wret = push_node_left(trans, root, left, mid, 1);
1042 if (btrfs_header_nritems(mid) == 0) {
1043 /* we've managed to empty the middle node, drop it */
1044 u64 root_gen = btrfs_header_generation(parent);
1045 u64 bytenr = mid->start;
1046 u32 blocksize = mid->len;
1048 clean_tree_block(trans, root, mid);
1049 btrfs_tree_unlock(mid);
1050 free_extent_buffer(mid);
1052 wret = del_ptr(trans, root, path, level + 1, pslot);
1055 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
1057 btrfs_header_owner(parent),
1058 root_gen, level, 1);
1062 /* update the parent key to reflect our changes */
1063 struct btrfs_disk_key mid_key;
1064 btrfs_node_key(mid, &mid_key, 0);
1065 btrfs_set_node_key(parent, &mid_key, pslot);
1066 btrfs_mark_buffer_dirty(parent);
1069 /* update the path */
1071 if (btrfs_header_nritems(left) > orig_slot) {
1072 extent_buffer_get(left);
1073 /* left was locked after cow */
1074 path->nodes[level] = left;
1075 path->slots[level + 1] -= 1;
1076 path->slots[level] = orig_slot;
1078 btrfs_tree_unlock(mid);
1079 free_extent_buffer(mid);
1082 orig_slot -= btrfs_header_nritems(left);
1083 path->slots[level] = orig_slot;
1086 /* double check we haven't messed things up */
1087 check_block(root, path, level);
1089 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
1093 btrfs_tree_unlock(right);
1094 free_extent_buffer(right);
1097 if (path->nodes[level] != left)
1098 btrfs_tree_unlock(left);
1099 free_extent_buffer(left);
1104 /* Node balancing for insertion. Here we only split or push nodes around
1105 * when they are completely full. This is also done top down, so we
1106 * have to be pessimistic.
1108 static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
1109 struct btrfs_root *root,
1110 struct btrfs_path *path, int level)
1112 struct extent_buffer *right = NULL;
1113 struct extent_buffer *mid;
1114 struct extent_buffer *left = NULL;
1115 struct extent_buffer *parent = NULL;
1119 int orig_slot = path->slots[level];
1125 mid = path->nodes[level];
1126 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1127 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1129 if (level < BTRFS_MAX_LEVEL - 1)
1130 parent = path->nodes[level + 1];
1131 pslot = path->slots[level + 1];
1136 left = read_node_slot(root, parent, pslot - 1);
1138 /* first, try to make some room in the middle buffer */
1142 btrfs_tree_lock(left);
1143 left_nr = btrfs_header_nritems(left);
1144 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1147 ret = btrfs_cow_block(trans, root, left, parent,
1148 pslot - 1, &left, 0);
1152 wret = push_node_left(trans, root,
1159 struct btrfs_disk_key disk_key;
1160 orig_slot += left_nr;
1161 btrfs_node_key(mid, &disk_key, 0);
1162 btrfs_set_node_key(parent, &disk_key, pslot);
1163 btrfs_mark_buffer_dirty(parent);
1164 if (btrfs_header_nritems(left) > orig_slot) {
1165 path->nodes[level] = left;
1166 path->slots[level + 1] -= 1;
1167 path->slots[level] = orig_slot;
1168 btrfs_tree_unlock(mid);
1169 free_extent_buffer(mid);
1172 btrfs_header_nritems(left);
1173 path->slots[level] = orig_slot;
1174 btrfs_tree_unlock(left);
1175 free_extent_buffer(left);
1179 btrfs_tree_unlock(left);
1180 free_extent_buffer(left);
1182 right = read_node_slot(root, parent, pslot + 1);
1185 * then try to empty the right most buffer into the middle
1189 btrfs_tree_lock(right);
1190 right_nr = btrfs_header_nritems(right);
1191 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1194 ret = btrfs_cow_block(trans, root, right,
1200 wret = balance_node_right(trans, root,
1207 struct btrfs_disk_key disk_key;
1209 btrfs_node_key(right, &disk_key, 0);
1210 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1211 btrfs_mark_buffer_dirty(parent);
1213 if (btrfs_header_nritems(mid) <= orig_slot) {
1214 path->nodes[level] = right;
1215 path->slots[level + 1] += 1;
1216 path->slots[level] = orig_slot -
1217 btrfs_header_nritems(mid);
1218 btrfs_tree_unlock(mid);
1219 free_extent_buffer(mid);
1221 btrfs_tree_unlock(right);
1222 free_extent_buffer(right);
1226 btrfs_tree_unlock(right);
1227 free_extent_buffer(right);
1233 * readahead one full node of leaves, finding things that are close
1234 * to the block in 'slot', and triggering ra on them.
1236 static noinline void reada_for_search(struct btrfs_root *root,
1237 struct btrfs_path *path,
1238 int level, int slot, u64 objectid)
1240 struct extent_buffer *node;
1241 struct btrfs_disk_key disk_key;
1247 int direction = path->reada;
1248 struct extent_buffer *eb;
1256 if (!path->nodes[level])
1259 node = path->nodes[level];
1261 search = btrfs_node_blockptr(node, slot);
1262 blocksize = btrfs_level_size(root, level - 1);
1263 eb = btrfs_find_tree_block(root, search, blocksize);
1265 free_extent_buffer(eb);
1269 highest_read = search;
1270 lowest_read = search;
1272 nritems = btrfs_header_nritems(node);
1275 if (direction < 0) {
1279 } else if (direction > 0) {
1284 if (path->reada < 0 && objectid) {
1285 btrfs_node_key(node, &disk_key, nr);
1286 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1289 search = btrfs_node_blockptr(node, nr);
1290 if ((search >= lowest_read && search <= highest_read) ||
1291 (search < lowest_read && lowest_read - search <= 32768) ||
1292 (search > highest_read && search - highest_read <= 32768)) {
1293 readahead_tree_block(root, search, blocksize,
1294 btrfs_node_ptr_generation(node, nr));
1298 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1300 if(nread > (1024 * 1024) || nscan > 128)
1303 if (search < lowest_read)
1304 lowest_read = search;
1305 if (search > highest_read)
1306 highest_read = search;
1311 * when we walk down the tree, it is usually safe to unlock the higher layers in
1312 * the tree. The exceptions are when our path goes through slot 0, because operations
1313 * on the tree might require changing key pointers higher up in the tree.
1315 * callers might also have set path->keep_locks, which tells this code to
1316 * keep the lock if the path points to the last slot in the block. This is
1317 * part of walking through the tree, and selecting the next slot in the higher
1320 * lowest_unlock sets the lowest level in the tree we're allowed to unlock.
1321 * so if lowest_unlock is 1, level 0 won't be unlocked
1323 static noinline void unlock_up(struct btrfs_path *path, int level,
1327 int skip_level = level;
1329 struct extent_buffer *t;
1331 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1332 if (!path->nodes[i])
1334 if (!path->locks[i])
1336 if (!no_skips && path->slots[i] == 0) {
1340 if (!no_skips && path->keep_locks) {
1343 nritems = btrfs_header_nritems(t);
1344 if (nritems < 1 || path->slots[i] >= nritems - 1) {
1349 if (skip_level < i && i >= lowest_unlock)
1353 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1354 btrfs_tree_unlock(t);
1361 * look for key in the tree. path is filled in with nodes along the way
1362 * if key is found, we return zero and you can find the item in the leaf
1363 * level of the path (level 0)
1365 * If the key isn't found, the path points to the slot where it should
1366 * be inserted, and 1 is returned. If there are other errors during the
1367 * search a negative error number is returned.
1369 * if ins_len > 0, nodes and leaves will be split as we walk down the
1370 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1373 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1374 *root, struct btrfs_key *key, struct btrfs_path *p, int
1377 struct extent_buffer *b;
1378 struct extent_buffer *tmp;
1382 int should_reada = p->reada;
1383 int lowest_unlock = 1;
1385 u8 lowest_level = 0;
1388 struct btrfs_key prealloc_block;
1390 lowest_level = p->lowest_level;
1391 WARN_ON(lowest_level && ins_len > 0);
1392 WARN_ON(p->nodes[0] != NULL);
1393 WARN_ON(cow && root == root->fs_info->extent_root &&
1394 !mutex_is_locked(&root->fs_info->alloc_mutex));
1398 prealloc_block.objectid = 0;
1401 if (p->skip_locking)
1402 b = btrfs_root_node(root);
1404 b = btrfs_lock_root_node(root);
1407 level = btrfs_header_level(b);
1410 * setup the path here so we can release it under lock
1411 * contention with the cow code
1413 p->nodes[level] = b;
1414 if (!p->skip_locking)
1415 p->locks[level] = 1;
1420 /* is a cow on this block not required */
1421 spin_lock(&root->fs_info->hash_lock);
1422 if (btrfs_header_generation(b) == trans->transid &&
1423 btrfs_header_owner(b) == root->root_key.objectid &&
1424 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
1425 spin_unlock(&root->fs_info->hash_lock);
1428 spin_unlock(&root->fs_info->hash_lock);
1430 /* ok, we have to cow, is our old prealloc the right
1433 if (prealloc_block.objectid &&
1434 prealloc_block.offset != b->len) {
1435 btrfs_free_reserved_extent(root,
1436 prealloc_block.objectid,
1437 prealloc_block.offset);
1438 prealloc_block.objectid = 0;
1442 * for higher level blocks, try not to allocate blocks
1443 * with the block and the parent locks held.
1445 if (level > 1 && !prealloc_block.objectid &&
1446 btrfs_path_lock_waiting(p, level)) {
1448 u64 hint = b->start;
1450 btrfs_release_path(root, p);
1451 ret = btrfs_reserve_extent(trans, root,
1454 &prealloc_block, 0);
1459 wret = btrfs_cow_block(trans, root, b,
1460 p->nodes[level + 1],
1461 p->slots[level + 1],
1462 &b, prealloc_block.objectid);
1463 prealloc_block.objectid = 0;
1465 free_extent_buffer(b);
1471 BUG_ON(!cow && ins_len);
1472 if (level != btrfs_header_level(b))
1474 level = btrfs_header_level(b);
1476 p->nodes[level] = b;
1477 if (!p->skip_locking)
1478 p->locks[level] = 1;
1480 ret = check_block(root, p, level);
1486 ret = bin_search(b, key, level, &slot);
1488 if (ret && slot > 0)
1490 p->slots[level] = slot;
1491 if (ins_len > 0 && btrfs_header_nritems(b) >=
1492 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1493 int sret = split_node(trans, root, p, level);
1499 b = p->nodes[level];
1500 slot = p->slots[level];
1501 } else if (ins_len < 0) {
1502 int sret = balance_level(trans, root, p,
1508 b = p->nodes[level];
1510 btrfs_release_path(NULL, p);
1513 slot = p->slots[level];
1514 BUG_ON(btrfs_header_nritems(b) == 1);
1516 unlock_up(p, level, lowest_unlock);
1518 /* this is only true while dropping a snapshot */
1519 if (level == lowest_level) {
1524 blocknr = btrfs_node_blockptr(b, slot);
1525 gen = btrfs_node_ptr_generation(b, slot);
1526 blocksize = btrfs_level_size(root, level - 1);
1528 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1529 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1533 * reduce lock contention at high levels
1534 * of the btree by dropping locks before
1538 btrfs_release_path(NULL, p);
1540 free_extent_buffer(tmp);
1542 reada_for_search(root, p,
1546 tmp = read_tree_block(root, blocknr,
1549 free_extent_buffer(tmp);
1553 free_extent_buffer(tmp);
1555 reada_for_search(root, p,
1558 b = read_node_slot(root, b, slot);
1561 if (!p->skip_locking)
1564 p->slots[level] = slot;
1565 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
1566 sizeof(struct btrfs_item) + ins_len) {
1567 int sret = split_leaf(trans, root, key,
1568 p, ins_len, ret == 0);
1575 unlock_up(p, level, lowest_unlock);
1581 if (prealloc_block.objectid) {
1582 btrfs_free_reserved_extent(root,
1583 prealloc_block.objectid,
1584 prealloc_block.offset);
1590 int btrfs_merge_path(struct btrfs_trans_handle *trans,
1591 struct btrfs_root *root,
1592 struct btrfs_key *node_keys,
1593 u64 *nodes, int lowest_level)
1595 struct extent_buffer *eb;
1596 struct extent_buffer *parent;
1597 struct btrfs_key key;
1606 eb = btrfs_lock_root_node(root);
1607 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0);
1612 level = btrfs_header_level(parent);
1613 if (level == 0 || level <= lowest_level)
1616 ret = bin_search(parent, &node_keys[lowest_level], level,
1618 if (ret && slot > 0)
1621 bytenr = btrfs_node_blockptr(parent, slot);
1622 if (nodes[level - 1] == bytenr)
1625 blocksize = btrfs_level_size(root, level - 1);
1626 generation = btrfs_node_ptr_generation(parent, slot);
1627 btrfs_node_key_to_cpu(eb, &key, slot);
1628 key_match = !memcmp(&key, &node_keys[level - 1], sizeof(key));
1631 * if node keys match and node pointer hasn't been modified
1632 * in the running transaction, we can merge the path. for
1633 * reloc trees, the node pointer check is skipped, this is
1634 * because the reloc trees are fully controlled by the space
1635 * balance code, no one else can modify them.
1637 if (!nodes[level - 1] || !key_match ||
1638 (generation == trans->transid &&
1639 root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)) {
1641 if (level == 1 || level == lowest_level + 1)
1644 eb = read_tree_block(root, bytenr, blocksize,
1646 btrfs_tree_lock(eb);
1648 ret = btrfs_cow_block(trans, root, eb, parent, slot,
1652 btrfs_tree_unlock(parent);
1653 free_extent_buffer(parent);
1658 if (generation == trans->transid) {
1660 BUG_ON(btrfs_header_owner(eb) !=
1661 BTRFS_TREE_RELOC_OBJECTID);
1663 * lock the block to keep __btrfs_cow_block from
1664 * changing the reference count.
1666 eb = read_tree_block(root, bytenr, blocksize,
1668 btrfs_tree_lock(eb);
1670 ret = btrfs_lookup_extent_ref(trans, root, bytenr,
1674 * if replace block whose reference count is one,
1675 * we have to "drop the subtree". so skip it for
1679 btrfs_tree_unlock(eb);
1680 free_extent_buffer(eb);
1685 btrfs_set_node_blockptr(parent, slot, nodes[level - 1]);
1686 btrfs_set_node_ptr_generation(parent, slot, trans->transid);
1687 btrfs_mark_buffer_dirty(parent);
1689 ret = btrfs_inc_extent_ref(trans, root,
1691 blocksize, parent->start,
1692 btrfs_header_owner(parent),
1693 btrfs_header_generation(parent),
1696 ret = btrfs_free_extent(trans, root, bytenr,
1697 blocksize, parent->start,
1698 btrfs_header_owner(parent),
1699 btrfs_header_generation(parent),
1703 if (generation == trans->transid) {
1704 btrfs_tree_unlock(eb);
1705 free_extent_buffer(eb);
1709 btrfs_tree_unlock(parent);
1710 free_extent_buffer(parent);
1715 * adjust the pointers going up the tree, starting at level
1716 * making sure the right key of each node is points to 'key'.
1717 * This is used after shifting pointers to the left, so it stops
1718 * fixing up pointers when a given leaf/node is not in slot 0 of the
1721 * If this fails to write a tree block, it returns -1, but continues
1722 * fixing up the blocks in ram so the tree is consistent.
1724 static int fixup_low_keys(struct btrfs_trans_handle *trans,
1725 struct btrfs_root *root, struct btrfs_path *path,
1726 struct btrfs_disk_key *key, int level)
1730 struct extent_buffer *t;
1732 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1733 int tslot = path->slots[i];
1734 if (!path->nodes[i])
1737 btrfs_set_node_key(t, key, tslot);
1738 btrfs_mark_buffer_dirty(path->nodes[i]);
1748 * This function isn't completely safe. It's the caller's responsibility
1749 * that the new key won't break the order
1751 int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1752 struct btrfs_root *root, struct btrfs_path *path,
1753 struct btrfs_key *new_key)
1755 struct btrfs_disk_key disk_key;
1756 struct extent_buffer *eb;
1759 eb = path->nodes[0];
1760 slot = path->slots[0];
1762 btrfs_item_key(eb, &disk_key, slot - 1);
1763 if (comp_keys(&disk_key, new_key) >= 0)
1766 if (slot < btrfs_header_nritems(eb) - 1) {
1767 btrfs_item_key(eb, &disk_key, slot + 1);
1768 if (comp_keys(&disk_key, new_key) <= 0)
1772 btrfs_cpu_key_to_disk(&disk_key, new_key);
1773 btrfs_set_item_key(eb, &disk_key, slot);
1774 btrfs_mark_buffer_dirty(eb);
1776 fixup_low_keys(trans, root, path, &disk_key, 1);
1781 * try to push data from one node into the next node left in the
1784 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1785 * error, and > 0 if there was no room in the left hand block.
1787 static int push_node_left(struct btrfs_trans_handle *trans,
1788 struct btrfs_root *root, struct extent_buffer *dst,
1789 struct extent_buffer *src, int empty)
1796 src_nritems = btrfs_header_nritems(src);
1797 dst_nritems = btrfs_header_nritems(dst);
1798 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1799 WARN_ON(btrfs_header_generation(src) != trans->transid);
1800 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1802 if (!empty && src_nritems <= 8)
1805 if (push_items <= 0) {
1810 push_items = min(src_nritems, push_items);
1811 if (push_items < src_nritems) {
1812 /* leave at least 8 pointers in the node if
1813 * we aren't going to empty it
1815 if (src_nritems - push_items < 8) {
1816 if (push_items <= 8)
1822 push_items = min(src_nritems - 8, push_items);
1824 copy_extent_buffer(dst, src,
1825 btrfs_node_key_ptr_offset(dst_nritems),
1826 btrfs_node_key_ptr_offset(0),
1827 push_items * sizeof(struct btrfs_key_ptr));
1829 if (push_items < src_nritems) {
1830 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1831 btrfs_node_key_ptr_offset(push_items),
1832 (src_nritems - push_items) *
1833 sizeof(struct btrfs_key_ptr));
1835 btrfs_set_header_nritems(src, src_nritems - push_items);
1836 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1837 btrfs_mark_buffer_dirty(src);
1838 btrfs_mark_buffer_dirty(dst);
1840 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
1847 * try to push data from one node into the next node right in the
1850 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1851 * error, and > 0 if there was no room in the right hand block.
1853 * this will only push up to 1/2 the contents of the left node over
1855 static int balance_node_right(struct btrfs_trans_handle *trans,
1856 struct btrfs_root *root,
1857 struct extent_buffer *dst,
1858 struct extent_buffer *src)
1866 WARN_ON(btrfs_header_generation(src) != trans->transid);
1867 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1869 src_nritems = btrfs_header_nritems(src);
1870 dst_nritems = btrfs_header_nritems(dst);
1871 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1872 if (push_items <= 0) {
1876 if (src_nritems < 4) {
1880 max_push = src_nritems / 2 + 1;
1881 /* don't try to empty the node */
1882 if (max_push >= src_nritems) {
1886 if (max_push < push_items)
1887 push_items = max_push;
1889 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1890 btrfs_node_key_ptr_offset(0),
1892 sizeof(struct btrfs_key_ptr));
1894 copy_extent_buffer(dst, src,
1895 btrfs_node_key_ptr_offset(0),
1896 btrfs_node_key_ptr_offset(src_nritems - push_items),
1897 push_items * sizeof(struct btrfs_key_ptr));
1899 btrfs_set_header_nritems(src, src_nritems - push_items);
1900 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1902 btrfs_mark_buffer_dirty(src);
1903 btrfs_mark_buffer_dirty(dst);
1905 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
1912 * helper function to insert a new root level in the tree.
1913 * A new node is allocated, and a single item is inserted to
1914 * point to the existing root
1916 * returns zero on success or < 0 on failure.
1918 static int noinline insert_new_root(struct btrfs_trans_handle *trans,
1919 struct btrfs_root *root,
1920 struct btrfs_path *path, int level)
1923 struct extent_buffer *lower;
1924 struct extent_buffer *c;
1925 struct extent_buffer *old;
1926 struct btrfs_disk_key lower_key;
1929 BUG_ON(path->nodes[level]);
1930 BUG_ON(path->nodes[level-1] != root->node);
1932 lower = path->nodes[level-1];
1934 btrfs_item_key(lower, &lower_key, 0);
1936 btrfs_node_key(lower, &lower_key, 0);
1938 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
1939 root->root_key.objectid, trans->transid,
1940 level, root->node->start, 0);
1944 memset_extent_buffer(c, 0, 0, root->nodesize);
1945 btrfs_set_header_nritems(c, 1);
1946 btrfs_set_header_level(c, level);
1947 btrfs_set_header_bytenr(c, c->start);
1948 btrfs_set_header_generation(c, trans->transid);
1949 btrfs_set_header_owner(c, root->root_key.objectid);
1951 write_extent_buffer(c, root->fs_info->fsid,
1952 (unsigned long)btrfs_header_fsid(c),
1955 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1956 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1959 btrfs_set_node_key(c, &lower_key, 0);
1960 btrfs_set_node_blockptr(c, 0, lower->start);
1961 lower_gen = btrfs_header_generation(lower);
1962 WARN_ON(lower_gen != trans->transid);
1964 btrfs_set_node_ptr_generation(c, 0, lower_gen);
1966 btrfs_mark_buffer_dirty(c);
1968 spin_lock(&root->node_lock);
1971 spin_unlock(&root->node_lock);
1973 ret = btrfs_update_extent_ref(trans, root, lower->start,
1974 lower->start, c->start,
1975 root->root_key.objectid,
1976 trans->transid, level - 1);
1979 /* the super has an extra ref to root->node */
1980 free_extent_buffer(old);
1982 add_root_to_dirty_list(root);
1983 extent_buffer_get(c);
1984 path->nodes[level] = c;
1985 path->locks[level] = 1;
1986 path->slots[level] = 0;
1991 * worker function to insert a single pointer in a node.
1992 * the node should have enough room for the pointer already
1994 * slot and level indicate where you want the key to go, and
1995 * blocknr is the block the key points to.
1997 * returns zero on success and < 0 on any error
1999 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2000 *root, struct btrfs_path *path, struct btrfs_disk_key
2001 *key, u64 bytenr, int slot, int level)
2003 struct extent_buffer *lower;
2006 BUG_ON(!path->nodes[level]);
2007 lower = path->nodes[level];
2008 nritems = btrfs_header_nritems(lower);
2011 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
2013 if (slot != nritems) {
2014 memmove_extent_buffer(lower,
2015 btrfs_node_key_ptr_offset(slot + 1),
2016 btrfs_node_key_ptr_offset(slot),
2017 (nritems - slot) * sizeof(struct btrfs_key_ptr));
2019 btrfs_set_node_key(lower, key, slot);
2020 btrfs_set_node_blockptr(lower, slot, bytenr);
2021 WARN_ON(trans->transid == 0);
2022 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
2023 btrfs_set_header_nritems(lower, nritems + 1);
2024 btrfs_mark_buffer_dirty(lower);
2029 * split the node at the specified level in path in two.
2030 * The path is corrected to point to the appropriate node after the split
2032 * Before splitting this tries to make some room in the node by pushing
2033 * left and right, if either one works, it returns right away.
2035 * returns 0 on success and < 0 on failure
2037 static noinline int split_node(struct btrfs_trans_handle *trans,
2038 struct btrfs_root *root,
2039 struct btrfs_path *path, int level)
2041 struct extent_buffer *c;
2042 struct extent_buffer *split;
2043 struct btrfs_disk_key disk_key;
2049 c = path->nodes[level];
2050 WARN_ON(btrfs_header_generation(c) != trans->transid);
2051 if (c == root->node) {
2052 /* trying to split the root, lets make a new one */
2053 ret = insert_new_root(trans, root, path, level + 1);
2057 ret = push_nodes_for_insert(trans, root, path, level);
2058 c = path->nodes[level];
2059 if (!ret && btrfs_header_nritems(c) <
2060 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
2066 c_nritems = btrfs_header_nritems(c);
2068 split = btrfs_alloc_free_block(trans, root, root->nodesize,
2069 path->nodes[level + 1]->start,
2070 root->root_key.objectid,
2071 trans->transid, level, c->start, 0);
2073 return PTR_ERR(split);
2075 btrfs_set_header_flags(split, btrfs_header_flags(c));
2076 btrfs_set_header_level(split, btrfs_header_level(c));
2077 btrfs_set_header_bytenr(split, split->start);
2078 btrfs_set_header_generation(split, trans->transid);
2079 btrfs_set_header_owner(split, root->root_key.objectid);
2080 btrfs_set_header_flags(split, 0);
2081 write_extent_buffer(split, root->fs_info->fsid,
2082 (unsigned long)btrfs_header_fsid(split),
2084 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2085 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2088 mid = (c_nritems + 1) / 2;
2090 copy_extent_buffer(split, c,
2091 btrfs_node_key_ptr_offset(0),
2092 btrfs_node_key_ptr_offset(mid),
2093 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2094 btrfs_set_header_nritems(split, c_nritems - mid);
2095 btrfs_set_header_nritems(c, mid);
2098 btrfs_mark_buffer_dirty(c);
2099 btrfs_mark_buffer_dirty(split);
2101 btrfs_node_key(split, &disk_key, 0);
2102 wret = insert_ptr(trans, root, path, &disk_key, split->start,
2103 path->slots[level + 1] + 1,
2108 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
2111 if (path->slots[level] >= mid) {
2112 path->slots[level] -= mid;
2113 btrfs_tree_unlock(c);
2114 free_extent_buffer(c);
2115 path->nodes[level] = split;
2116 path->slots[level + 1] += 1;
2118 btrfs_tree_unlock(split);
2119 free_extent_buffer(split);
2125 * how many bytes are required to store the items in a leaf. start
2126 * and nr indicate which items in the leaf to check. This totals up the
2127 * space used both by the item structs and the item data
2129 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
2132 int nritems = btrfs_header_nritems(l);
2133 int end = min(nritems, start + nr) - 1;
2137 data_len = btrfs_item_end_nr(l, start);
2138 data_len = data_len - btrfs_item_offset_nr(l, end);
2139 data_len += sizeof(struct btrfs_item) * nr;
2140 WARN_ON(data_len < 0);
2145 * The space between the end of the leaf items and
2146 * the start of the leaf data. IOW, how much room
2147 * the leaf has left for both items and data
2149 int noinline btrfs_leaf_free_space(struct btrfs_root *root,
2150 struct extent_buffer *leaf)
2152 int nritems = btrfs_header_nritems(leaf);
2154 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2156 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
2157 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
2158 leaf_space_used(leaf, 0, nritems), nritems);
2164 * push some data in the path leaf to the right, trying to free up at
2165 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2167 * returns 1 if the push failed because the other node didn't have enough
2168 * room, 0 if everything worked out and < 0 if there were major errors.
2170 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
2171 *root, struct btrfs_path *path, int data_size,
2174 struct extent_buffer *left = path->nodes[0];
2175 struct extent_buffer *right;
2176 struct extent_buffer *upper;
2177 struct btrfs_disk_key disk_key;
2183 struct btrfs_item *item;
2191 slot = path->slots[1];
2192 if (!path->nodes[1]) {
2195 upper = path->nodes[1];
2196 if (slot >= btrfs_header_nritems(upper) - 1)
2199 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2201 right = read_node_slot(root, upper, slot + 1);
2202 btrfs_tree_lock(right);
2203 free_space = btrfs_leaf_free_space(root, right);
2204 if (free_space < data_size + sizeof(struct btrfs_item))
2207 /* cow and double check */
2208 ret = btrfs_cow_block(trans, root, right, upper,
2209 slot + 1, &right, 0);
2213 free_space = btrfs_leaf_free_space(root, right);
2214 if (free_space < data_size + sizeof(struct btrfs_item))
2217 left_nritems = btrfs_header_nritems(left);
2218 if (left_nritems == 0)
2226 if (path->slots[0] >= left_nritems)
2227 push_space += data_size + sizeof(*item);
2229 i = left_nritems - 1;
2231 item = btrfs_item_nr(left, i);
2233 if (!empty && push_items > 0) {
2234 if (path->slots[0] > i)
2236 if (path->slots[0] == i) {
2237 int space = btrfs_leaf_free_space(root, left);
2238 if (space + push_space * 2 > free_space)
2243 if (path->slots[0] == i)
2244 push_space += data_size + sizeof(*item);
2246 if (!left->map_token) {
2247 map_extent_buffer(left, (unsigned long)item,
2248 sizeof(struct btrfs_item),
2249 &left->map_token, &left->kaddr,
2250 &left->map_start, &left->map_len,
2254 this_item_size = btrfs_item_size(left, item);
2255 if (this_item_size + sizeof(*item) + push_space > free_space)
2259 push_space += this_item_size + sizeof(*item);
2264 if (left->map_token) {
2265 unmap_extent_buffer(left, left->map_token, KM_USER1);
2266 left->map_token = NULL;
2269 if (push_items == 0)
2272 if (!empty && push_items == left_nritems)
2275 /* push left to right */
2276 right_nritems = btrfs_header_nritems(right);
2278 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
2279 push_space -= leaf_data_end(root, left);
2281 /* make room in the right data area */
2282 data_end = leaf_data_end(root, right);
2283 memmove_extent_buffer(right,
2284 btrfs_leaf_data(right) + data_end - push_space,
2285 btrfs_leaf_data(right) + data_end,
2286 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2288 /* copy from the left data area */
2289 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
2290 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2291 btrfs_leaf_data(left) + leaf_data_end(root, left),
2294 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2295 btrfs_item_nr_offset(0),
2296 right_nritems * sizeof(struct btrfs_item));
2298 /* copy the items from left to right */
2299 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2300 btrfs_item_nr_offset(left_nritems - push_items),
2301 push_items * sizeof(struct btrfs_item));
2303 /* update the item pointers */
2304 right_nritems += push_items;
2305 btrfs_set_header_nritems(right, right_nritems);
2306 push_space = BTRFS_LEAF_DATA_SIZE(root);
2307 for (i = 0; i < right_nritems; i++) {
2308 item = btrfs_item_nr(right, i);
2309 if (!right->map_token) {
2310 map_extent_buffer(right, (unsigned long)item,
2311 sizeof(struct btrfs_item),
2312 &right->map_token, &right->kaddr,
2313 &right->map_start, &right->map_len,
2316 push_space -= btrfs_item_size(right, item);
2317 btrfs_set_item_offset(right, item, push_space);
2320 if (right->map_token) {
2321 unmap_extent_buffer(right, right->map_token, KM_USER1);
2322 right->map_token = NULL;
2324 left_nritems -= push_items;
2325 btrfs_set_header_nritems(left, left_nritems);
2328 btrfs_mark_buffer_dirty(left);
2329 btrfs_mark_buffer_dirty(right);
2331 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
2334 btrfs_item_key(right, &disk_key, 0);
2335 btrfs_set_node_key(upper, &disk_key, slot + 1);
2336 btrfs_mark_buffer_dirty(upper);
2338 /* then fixup the leaf pointer in the path */
2339 if (path->slots[0] >= left_nritems) {
2340 path->slots[0] -= left_nritems;
2341 if (btrfs_header_nritems(path->nodes[0]) == 0)
2342 clean_tree_block(trans, root, path->nodes[0]);
2343 btrfs_tree_unlock(path->nodes[0]);
2344 free_extent_buffer(path->nodes[0]);
2345 path->nodes[0] = right;
2346 path->slots[1] += 1;
2348 btrfs_tree_unlock(right);
2349 free_extent_buffer(right);
2354 btrfs_tree_unlock(right);
2355 free_extent_buffer(right);
2360 * push some data in the path leaf to the left, trying to free up at
2361 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2363 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
2364 *root, struct btrfs_path *path, int data_size,
2367 struct btrfs_disk_key disk_key;
2368 struct extent_buffer *right = path->nodes[0];
2369 struct extent_buffer *left;
2375 struct btrfs_item *item;
2376 u32 old_left_nritems;
2382 u32 old_left_item_size;
2384 slot = path->slots[1];
2387 if (!path->nodes[1])
2390 right_nritems = btrfs_header_nritems(right);
2391 if (right_nritems == 0) {
2395 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
2397 left = read_node_slot(root, path->nodes[1], slot - 1);
2398 btrfs_tree_lock(left);
2399 free_space = btrfs_leaf_free_space(root, left);
2400 if (free_space < data_size + sizeof(struct btrfs_item)) {
2405 /* cow and double check */
2406 ret = btrfs_cow_block(trans, root, left,
2407 path->nodes[1], slot - 1, &left, 0);
2409 /* we hit -ENOSPC, but it isn't fatal here */
2414 free_space = btrfs_leaf_free_space(root, left);
2415 if (free_space < data_size + sizeof(struct btrfs_item)) {
2423 nr = right_nritems - 1;
2425 for (i = 0; i < nr; i++) {
2426 item = btrfs_item_nr(right, i);
2427 if (!right->map_token) {
2428 map_extent_buffer(right, (unsigned long)item,
2429 sizeof(struct btrfs_item),
2430 &right->map_token, &right->kaddr,
2431 &right->map_start, &right->map_len,
2435 if (!empty && push_items > 0) {
2436 if (path->slots[0] < i)
2438 if (path->slots[0] == i) {
2439 int space = btrfs_leaf_free_space(root, right);
2440 if (space + push_space * 2 > free_space)
2445 if (path->slots[0] == i)
2446 push_space += data_size + sizeof(*item);
2448 this_item_size = btrfs_item_size(right, item);
2449 if (this_item_size + sizeof(*item) + push_space > free_space)
2453 push_space += this_item_size + sizeof(*item);
2456 if (right->map_token) {
2457 unmap_extent_buffer(right, right->map_token, KM_USER1);
2458 right->map_token = NULL;
2461 if (push_items == 0) {
2465 if (!empty && push_items == btrfs_header_nritems(right))
2468 /* push data from right to left */
2469 copy_extent_buffer(left, right,
2470 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2471 btrfs_item_nr_offset(0),
2472 push_items * sizeof(struct btrfs_item));
2474 push_space = BTRFS_LEAF_DATA_SIZE(root) -
2475 btrfs_item_offset_nr(right, push_items -1);
2477 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
2478 leaf_data_end(root, left) - push_space,
2479 btrfs_leaf_data(right) +
2480 btrfs_item_offset_nr(right, push_items - 1),
2482 old_left_nritems = btrfs_header_nritems(left);
2483 BUG_ON(old_left_nritems < 0);
2485 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
2486 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
2489 item = btrfs_item_nr(left, i);
2490 if (!left->map_token) {
2491 map_extent_buffer(left, (unsigned long)item,
2492 sizeof(struct btrfs_item),
2493 &left->map_token, &left->kaddr,
2494 &left->map_start, &left->map_len,
2498 ioff = btrfs_item_offset(left, item);
2499 btrfs_set_item_offset(left, item,
2500 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
2502 btrfs_set_header_nritems(left, old_left_nritems + push_items);
2503 if (left->map_token) {
2504 unmap_extent_buffer(left, left->map_token, KM_USER1);
2505 left->map_token = NULL;
2508 /* fixup right node */
2509 if (push_items > right_nritems) {
2510 printk("push items %d nr %u\n", push_items, right_nritems);
2514 if (push_items < right_nritems) {
2515 push_space = btrfs_item_offset_nr(right, push_items - 1) -
2516 leaf_data_end(root, right);
2517 memmove_extent_buffer(right, btrfs_leaf_data(right) +
2518 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2519 btrfs_leaf_data(right) +
2520 leaf_data_end(root, right), push_space);
2522 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
2523 btrfs_item_nr_offset(push_items),
2524 (btrfs_header_nritems(right) - push_items) *
2525 sizeof(struct btrfs_item));
2527 right_nritems -= push_items;
2528 btrfs_set_header_nritems(right, right_nritems);
2529 push_space = BTRFS_LEAF_DATA_SIZE(root);
2530 for (i = 0; i < right_nritems; i++) {
2531 item = btrfs_item_nr(right, i);
2533 if (!right->map_token) {
2534 map_extent_buffer(right, (unsigned long)item,
2535 sizeof(struct btrfs_item),
2536 &right->map_token, &right->kaddr,
2537 &right->map_start, &right->map_len,
2541 push_space = push_space - btrfs_item_size(right, item);
2542 btrfs_set_item_offset(right, item, push_space);
2544 if (right->map_token) {
2545 unmap_extent_buffer(right, right->map_token, KM_USER1);
2546 right->map_token = NULL;
2549 btrfs_mark_buffer_dirty(left);
2551 btrfs_mark_buffer_dirty(right);
2553 ret = btrfs_update_ref(trans, root, right, left,
2554 old_left_nritems, push_items);
2557 btrfs_item_key(right, &disk_key, 0);
2558 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
2562 /* then fixup the leaf pointer in the path */
2563 if (path->slots[0] < push_items) {
2564 path->slots[0] += old_left_nritems;
2565 if (btrfs_header_nritems(path->nodes[0]) == 0)
2566 clean_tree_block(trans, root, path->nodes[0]);
2567 btrfs_tree_unlock(path->nodes[0]);
2568 free_extent_buffer(path->nodes[0]);
2569 path->nodes[0] = left;
2570 path->slots[1] -= 1;
2572 btrfs_tree_unlock(left);
2573 free_extent_buffer(left);
2574 path->slots[0] -= push_items;
2576 BUG_ON(path->slots[0] < 0);
2579 btrfs_tree_unlock(left);
2580 free_extent_buffer(left);
2585 * split the path's leaf in two, making sure there is at least data_size
2586 * available for the resulting leaf level of the path.
2588 * returns 0 if all went well and < 0 on failure.
2590 static noinline int split_leaf(struct btrfs_trans_handle *trans,
2591 struct btrfs_root *root,
2592 struct btrfs_key *ins_key,
2593 struct btrfs_path *path, int data_size,
2596 struct extent_buffer *l;
2600 struct extent_buffer *right;
2601 int space_needed = data_size + sizeof(struct btrfs_item);
2608 int num_doubles = 0;
2609 struct btrfs_disk_key disk_key;
2612 space_needed = data_size;
2614 /* first try to make some room by pushing left and right */
2615 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
2616 wret = push_leaf_right(trans, root, path, data_size, 0);
2621 wret = push_leaf_left(trans, root, path, data_size, 0);
2627 /* did the pushes work? */
2628 if (btrfs_leaf_free_space(root, l) >= space_needed)
2632 if (!path->nodes[1]) {
2633 ret = insert_new_root(trans, root, path, 1);
2640 slot = path->slots[0];
2641 nritems = btrfs_header_nritems(l);
2642 mid = (nritems + 1)/ 2;
2644 right = btrfs_alloc_free_block(trans, root, root->leafsize,
2645 path->nodes[1]->start,
2646 root->root_key.objectid,
2647 trans->transid, 0, l->start, 0);
2648 if (IS_ERR(right)) {
2650 return PTR_ERR(right);
2653 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
2654 btrfs_set_header_bytenr(right, right->start);
2655 btrfs_set_header_generation(right, trans->transid);
2656 btrfs_set_header_owner(right, root->root_key.objectid);
2657 btrfs_set_header_level(right, 0);
2658 write_extent_buffer(right, root->fs_info->fsid,
2659 (unsigned long)btrfs_header_fsid(right),
2662 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
2663 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2667 leaf_space_used(l, mid, nritems - mid) + space_needed >
2668 BTRFS_LEAF_DATA_SIZE(root)) {
2669 if (slot >= nritems) {
2670 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2671 btrfs_set_header_nritems(right, 0);
2672 wret = insert_ptr(trans, root, path,
2673 &disk_key, right->start,
2674 path->slots[1] + 1, 1);
2678 btrfs_tree_unlock(path->nodes[0]);
2679 free_extent_buffer(path->nodes[0]);
2680 path->nodes[0] = right;
2682 path->slots[1] += 1;
2683 btrfs_mark_buffer_dirty(right);
2687 if (mid != nritems &&
2688 leaf_space_used(l, mid, nritems - mid) +
2689 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2694 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2695 BTRFS_LEAF_DATA_SIZE(root)) {
2696 if (!extend && slot == 0) {
2697 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2698 btrfs_set_header_nritems(right, 0);
2699 wret = insert_ptr(trans, root, path,
2705 btrfs_tree_unlock(path->nodes[0]);
2706 free_extent_buffer(path->nodes[0]);
2707 path->nodes[0] = right;
2709 if (path->slots[1] == 0) {
2710 wret = fixup_low_keys(trans, root,
2711 path, &disk_key, 1);
2715 btrfs_mark_buffer_dirty(right);
2717 } else if (extend && slot == 0) {
2721 if (mid != nritems &&
2722 leaf_space_used(l, mid, nritems - mid) +
2723 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2729 nritems = nritems - mid;
2730 btrfs_set_header_nritems(right, nritems);
2731 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2733 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2734 btrfs_item_nr_offset(mid),
2735 nritems * sizeof(struct btrfs_item));
2737 copy_extent_buffer(right, l,
2738 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2739 data_copy_size, btrfs_leaf_data(l) +
2740 leaf_data_end(root, l), data_copy_size);
2742 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2743 btrfs_item_end_nr(l, mid);
2745 for (i = 0; i < nritems; i++) {
2746 struct btrfs_item *item = btrfs_item_nr(right, i);
2749 if (!right->map_token) {
2750 map_extent_buffer(right, (unsigned long)item,
2751 sizeof(struct btrfs_item),
2752 &right->map_token, &right->kaddr,
2753 &right->map_start, &right->map_len,
2757 ioff = btrfs_item_offset(right, item);
2758 btrfs_set_item_offset(right, item, ioff + rt_data_off);
2761 if (right->map_token) {
2762 unmap_extent_buffer(right, right->map_token, KM_USER1);
2763 right->map_token = NULL;
2766 btrfs_set_header_nritems(l, mid);
2768 btrfs_item_key(right, &disk_key, 0);
2769 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2770 path->slots[1] + 1, 1);
2774 btrfs_mark_buffer_dirty(right);
2775 btrfs_mark_buffer_dirty(l);
2776 BUG_ON(path->slots[0] != slot);
2778 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2782 btrfs_tree_unlock(path->nodes[0]);
2783 free_extent_buffer(path->nodes[0]);
2784 path->nodes[0] = right;
2785 path->slots[0] -= mid;
2786 path->slots[1] += 1;
2788 btrfs_tree_unlock(right);
2789 free_extent_buffer(right);
2792 BUG_ON(path->slots[0] < 0);
2795 BUG_ON(num_doubles != 0);
2803 * make the item pointed to by the path smaller. new_size indicates
2804 * how small to make it, and from_end tells us if we just chop bytes
2805 * off the end of the item or if we shift the item to chop bytes off
2808 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2809 struct btrfs_root *root,
2810 struct btrfs_path *path,
2811 u32 new_size, int from_end)
2816 struct extent_buffer *leaf;
2817 struct btrfs_item *item;
2819 unsigned int data_end;
2820 unsigned int old_data_start;
2821 unsigned int old_size;
2822 unsigned int size_diff;
2825 slot_orig = path->slots[0];
2826 leaf = path->nodes[0];
2827 slot = path->slots[0];
2829 old_size = btrfs_item_size_nr(leaf, slot);
2830 if (old_size == new_size)
2833 nritems = btrfs_header_nritems(leaf);
2834 data_end = leaf_data_end(root, leaf);
2836 old_data_start = btrfs_item_offset_nr(leaf, slot);
2838 size_diff = old_size - new_size;
2841 BUG_ON(slot >= nritems);
2844 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2846 /* first correct the data pointers */
2847 for (i = slot; i < nritems; i++) {
2849 item = btrfs_item_nr(leaf, i);
2851 if (!leaf->map_token) {
2852 map_extent_buffer(leaf, (unsigned long)item,
2853 sizeof(struct btrfs_item),
2854 &leaf->map_token, &leaf->kaddr,
2855 &leaf->map_start, &leaf->map_len,
2859 ioff = btrfs_item_offset(leaf, item);
2860 btrfs_set_item_offset(leaf, item, ioff + size_diff);
2863 if (leaf->map_token) {
2864 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2865 leaf->map_token = NULL;
2868 /* shift the data */
2870 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2871 data_end + size_diff, btrfs_leaf_data(leaf) +
2872 data_end, old_data_start + new_size - data_end);
2874 struct btrfs_disk_key disk_key;
2877 btrfs_item_key(leaf, &disk_key, slot);
2879 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2881 struct btrfs_file_extent_item *fi;
2883 fi = btrfs_item_ptr(leaf, slot,
2884 struct btrfs_file_extent_item);
2885 fi = (struct btrfs_file_extent_item *)(
2886 (unsigned long)fi - size_diff);
2888 if (btrfs_file_extent_type(leaf, fi) ==
2889 BTRFS_FILE_EXTENT_INLINE) {
2890 ptr = btrfs_item_ptr_offset(leaf, slot);
2891 memmove_extent_buffer(leaf, ptr,
2893 offsetof(struct btrfs_file_extent_item,
2898 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2899 data_end + size_diff, btrfs_leaf_data(leaf) +
2900 data_end, old_data_start - data_end);
2902 offset = btrfs_disk_key_offset(&disk_key);
2903 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2904 btrfs_set_item_key(leaf, &disk_key, slot);
2906 fixup_low_keys(trans, root, path, &disk_key, 1);
2909 item = btrfs_item_nr(leaf, slot);
2910 btrfs_set_item_size(leaf, item, new_size);
2911 btrfs_mark_buffer_dirty(leaf);
2914 if (btrfs_leaf_free_space(root, leaf) < 0) {
2915 btrfs_print_leaf(root, leaf);
2922 * make the item pointed to by the path bigger, data_size is the new size.
2924 int btrfs_extend_item(struct btrfs_trans_handle *trans,
2925 struct btrfs_root *root, struct btrfs_path *path,
2931 struct extent_buffer *leaf;
2932 struct btrfs_item *item;
2934 unsigned int data_end;
2935 unsigned int old_data;
2936 unsigned int old_size;
2939 slot_orig = path->slots[0];
2940 leaf = path->nodes[0];
2942 nritems = btrfs_header_nritems(leaf);
2943 data_end = leaf_data_end(root, leaf);
2945 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2946 btrfs_print_leaf(root, leaf);
2949 slot = path->slots[0];
2950 old_data = btrfs_item_end_nr(leaf, slot);
2953 if (slot >= nritems) {
2954 btrfs_print_leaf(root, leaf);
2955 printk("slot %d too large, nritems %d\n", slot, nritems);
2960 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2962 /* first correct the data pointers */
2963 for (i = slot; i < nritems; i++) {
2965 item = btrfs_item_nr(leaf, i);
2967 if (!leaf->map_token) {
2968 map_extent_buffer(leaf, (unsigned long)item,
2969 sizeof(struct btrfs_item),
2970 &leaf->map_token, &leaf->kaddr,
2971 &leaf->map_start, &leaf->map_len,
2974 ioff = btrfs_item_offset(leaf, item);
2975 btrfs_set_item_offset(leaf, item, ioff - data_size);
2978 if (leaf->map_token) {
2979 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2980 leaf->map_token = NULL;
2983 /* shift the data */
2984 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2985 data_end - data_size, btrfs_leaf_data(leaf) +
2986 data_end, old_data - data_end);
2988 data_end = old_data;
2989 old_size = btrfs_item_size_nr(leaf, slot);
2990 item = btrfs_item_nr(leaf, slot);
2991 btrfs_set_item_size(leaf, item, old_size + data_size);
2992 btrfs_mark_buffer_dirty(leaf);
2995 if (btrfs_leaf_free_space(root, leaf) < 0) {
2996 btrfs_print_leaf(root, leaf);
3003 * Given a key and some data, insert items into the tree.
3004 * This does all the path init required, making room in the tree if needed.
3006 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3007 struct btrfs_root *root,
3008 struct btrfs_path *path,
3009 struct btrfs_key *cpu_key, u32 *data_size,
3012 struct extent_buffer *leaf;
3013 struct btrfs_item *item;
3021 unsigned int data_end;
3022 struct btrfs_disk_key disk_key;
3024 for (i = 0; i < nr; i++) {
3025 total_data += data_size[i];
3028 total_size = total_data + (nr * sizeof(struct btrfs_item));
3029 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3035 slot_orig = path->slots[0];
3036 leaf = path->nodes[0];
3038 nritems = btrfs_header_nritems(leaf);
3039 data_end = leaf_data_end(root, leaf);
3041 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3042 btrfs_print_leaf(root, leaf);
3043 printk("not enough freespace need %u have %d\n",
3044 total_size, btrfs_leaf_free_space(root, leaf));
3048 slot = path->slots[0];
3051 if (slot != nritems) {
3052 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3054 if (old_data < data_end) {
3055 btrfs_print_leaf(root, leaf);
3056 printk("slot %d old_data %d data_end %d\n",
3057 slot, old_data, data_end);
3061 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3063 /* first correct the data pointers */
3064 WARN_ON(leaf->map_token);
3065 for (i = slot; i < nritems; i++) {
3068 item = btrfs_item_nr(leaf, i);
3069 if (!leaf->map_token) {
3070 map_extent_buffer(leaf, (unsigned long)item,
3071 sizeof(struct btrfs_item),
3072 &leaf->map_token, &leaf->kaddr,
3073 &leaf->map_start, &leaf->map_len,
3077 ioff = btrfs_item_offset(leaf, item);
3078 btrfs_set_item_offset(leaf, item, ioff - total_data);
3080 if (leaf->map_token) {
3081 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3082 leaf->map_token = NULL;
3085 /* shift the items */
3086 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3087 btrfs_item_nr_offset(slot),
3088 (nritems - slot) * sizeof(struct btrfs_item));
3090 /* shift the data */
3091 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3092 data_end - total_data, btrfs_leaf_data(leaf) +
3093 data_end, old_data - data_end);
3094 data_end = old_data;
3097 /* setup the item for the new data */
3098 for (i = 0; i < nr; i++) {
3099 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3100 btrfs_set_item_key(leaf, &disk_key, slot + i);
3101 item = btrfs_item_nr(leaf, slot + i);
3102 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3103 data_end -= data_size[i];
3104 btrfs_set_item_size(leaf, item, data_size[i]);
3106 btrfs_set_header_nritems(leaf, nritems + nr);
3107 btrfs_mark_buffer_dirty(leaf);
3111 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3112 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3115 if (btrfs_leaf_free_space(root, leaf) < 0) {
3116 btrfs_print_leaf(root, leaf);
3124 * Given a key and some data, insert an item into the tree.
3125 * This does all the path init required, making room in the tree if needed.
3127 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3128 *root, struct btrfs_key *cpu_key, void *data, u32
3132 struct btrfs_path *path;
3133 struct extent_buffer *leaf;
3136 path = btrfs_alloc_path();
3138 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
3140 leaf = path->nodes[0];
3141 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3142 write_extent_buffer(leaf, data, ptr, data_size);
3143 btrfs_mark_buffer_dirty(leaf);
3145 btrfs_free_path(path);
3150 * delete the pointer from a given node.
3152 * the tree should have been previously balanced so the deletion does not
3155 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3156 struct btrfs_path *path, int level, int slot)
3158 struct extent_buffer *parent = path->nodes[level];
3163 nritems = btrfs_header_nritems(parent);
3164 if (slot != nritems -1) {
3165 memmove_extent_buffer(parent,
3166 btrfs_node_key_ptr_offset(slot),
3167 btrfs_node_key_ptr_offset(slot + 1),
3168 sizeof(struct btrfs_key_ptr) *
3169 (nritems - slot - 1));
3172 btrfs_set_header_nritems(parent, nritems);
3173 if (nritems == 0 && parent == root->node) {
3174 BUG_ON(btrfs_header_level(root->node) != 1);
3175 /* just turn the root into a leaf and break */
3176 btrfs_set_header_level(root->node, 0);
3177 } else if (slot == 0) {
3178 struct btrfs_disk_key disk_key;
3180 btrfs_node_key(parent, &disk_key, 0);
3181 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
3185 btrfs_mark_buffer_dirty(parent);
3190 * a helper function to delete the leaf pointed to by path->slots[1] and
3191 * path->nodes[1]. bytenr is the node block pointer, but since the callers
3192 * already know it, it is faster to have them pass it down than to
3193 * read it out of the node again.
3195 * This deletes the pointer in path->nodes[1] and frees the leaf
3196 * block extent. zero is returned if it all worked out, < 0 otherwise.
3198 * The path must have already been setup for deleting the leaf, including
3199 * all the proper balancing. path->nodes[1] must be locked.
3201 noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3202 struct btrfs_root *root,
3203 struct btrfs_path *path, u64 bytenr)
3206 u64 root_gen = btrfs_header_generation(path->nodes[1]);
3208 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3212 ret = btrfs_free_extent(trans, root, bytenr,
3213 btrfs_level_size(root, 0),
3214 path->nodes[1]->start,
3215 btrfs_header_owner(path->nodes[1]),
3220 * delete the item at the leaf level in path. If that empties
3221 * the leaf, remove it from the tree
3223 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3224 struct btrfs_path *path, int slot, int nr)
3226 struct extent_buffer *leaf;
3227 struct btrfs_item *item;
3235 leaf = path->nodes[0];
3236 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
3238 for (i = 0; i < nr; i++)
3239 dsize += btrfs_item_size_nr(leaf, slot + i);
3241 nritems = btrfs_header_nritems(leaf);
3243 if (slot + nr != nritems) {
3244 int data_end = leaf_data_end(root, leaf);
3246 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3248 btrfs_leaf_data(leaf) + data_end,
3249 last_off - data_end);
3251 for (i = slot + nr; i < nritems; i++) {
3254 item = btrfs_item_nr(leaf, i);
3255 if (!leaf->map_token) {
3256 map_extent_buffer(leaf, (unsigned long)item,
3257 sizeof(struct btrfs_item),
3258 &leaf->map_token, &leaf->kaddr,
3259 &leaf->map_start, &leaf->map_len,
3262 ioff = btrfs_item_offset(leaf, item);
3263 btrfs_set_item_offset(leaf, item, ioff + dsize);
3266 if (leaf->map_token) {
3267 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3268 leaf->map_token = NULL;
3271 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
3272 btrfs_item_nr_offset(slot + nr),
3273 sizeof(struct btrfs_item) *
3274 (nritems - slot - nr));
3276 btrfs_set_header_nritems(leaf, nritems - nr);
3279 /* delete the leaf if we've emptied it */
3281 if (leaf == root->node) {
3282 btrfs_set_header_level(leaf, 0);
3284 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3288 int used = leaf_space_used(leaf, 0, nritems);
3290 struct btrfs_disk_key disk_key;
3292 btrfs_item_key(leaf, &disk_key, 0);
3293 wret = fixup_low_keys(trans, root, path,
3299 /* delete the leaf if it is mostly empty */
3300 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
3301 /* push_leaf_left fixes the path.
3302 * make sure the path still points to our leaf
3303 * for possible call to del_ptr below
3305 slot = path->slots[1];
3306 extent_buffer_get(leaf);
3308 wret = push_leaf_left(trans, root, path, 1, 1);
3309 if (wret < 0 && wret != -ENOSPC)
3312 if (path->nodes[0] == leaf &&
3313 btrfs_header_nritems(leaf)) {
3314 wret = push_leaf_right(trans, root, path, 1, 1);
3315 if (wret < 0 && wret != -ENOSPC)
3319 if (btrfs_header_nritems(leaf) == 0) {
3320 path->slots[1] = slot;
3321 ret = btrfs_del_leaf(trans, root, path, leaf->start);
3323 free_extent_buffer(leaf);
3325 /* if we're still in the path, make sure
3326 * we're dirty. Otherwise, one of the
3327 * push_leaf functions must have already
3328 * dirtied this buffer
3330 if (path->nodes[0] == leaf)
3331 btrfs_mark_buffer_dirty(leaf);
3332 free_extent_buffer(leaf);
3335 btrfs_mark_buffer_dirty(leaf);
3342 * search the tree again to find a leaf with lesser keys
3343 * returns 0 if it found something or 1 if there are no lesser leaves.
3344 * returns < 0 on io errors.
3346 * This may release the path, and so you may lose any locks held at the
3349 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
3351 struct btrfs_key key;
3352 struct btrfs_disk_key found_key;
3355 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
3359 else if (key.type > 0)
3361 else if (key.objectid > 0)
3366 btrfs_release_path(root, path);
3367 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3370 btrfs_item_key(path->nodes[0], &found_key, 0);
3371 ret = comp_keys(&found_key, &key);
3378 * A helper function to walk down the tree starting at min_key, and looking
3379 * for nodes or leaves that are either in cache or have a minimum
3380 * transaction id. This is used by the btree defrag code, and tree logging
3382 * This does not cow, but it does stuff the starting key it finds back
3383 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3384 * key and get a writable path.
3386 * This does lock as it descends, and path->keep_locks should be set
3387 * to 1 by the caller.
3389 * This honors path->lowest_level to prevent descent past a given level
3392 * min_trans indicates the oldest transaction that you are interested
3393 * in walking through. Any nodes or leaves older than min_trans are
3394 * skipped over (without reading them).
3396 * returns zero if something useful was found, < 0 on error and 1 if there
3397 * was nothing in the tree that matched the search criteria.
3399 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
3400 struct btrfs_key *max_key,
3401 struct btrfs_path *path, int cache_only,
3404 struct extent_buffer *cur;
3405 struct btrfs_key found_key;
3413 cur = btrfs_lock_root_node(root);
3414 level = btrfs_header_level(cur);
3415 WARN_ON(path->nodes[level]);
3416 path->nodes[level] = cur;
3417 path->locks[level] = 1;
3419 if (btrfs_header_generation(cur) < min_trans) {
3424 nritems = btrfs_header_nritems(cur);
3425 level = btrfs_header_level(cur);
3426 sret = bin_search(cur, min_key, level, &slot);
3428 /* at the lowest level, we're done, setup the path and exit */
3429 if (level == path->lowest_level) {
3430 if (slot >= nritems)
3433 path->slots[level] = slot;
3434 btrfs_item_key_to_cpu(cur, &found_key, slot);
3437 if (sret && slot > 0)
3440 * check this node pointer against the cache_only and
3441 * min_trans parameters. If it isn't in cache or is too
3442 * old, skip to the next one.
3444 while(slot < nritems) {
3447 struct extent_buffer *tmp;
3448 struct btrfs_disk_key disk_key;
3450 blockptr = btrfs_node_blockptr(cur, slot);
3451 gen = btrfs_node_ptr_generation(cur, slot);
3452 if (gen < min_trans) {
3460 btrfs_node_key(cur, &disk_key, slot);
3461 if (comp_keys(&disk_key, max_key) >= 0) {
3467 tmp = btrfs_find_tree_block(root, blockptr,
3468 btrfs_level_size(root, level - 1));
3470 if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
3471 free_extent_buffer(tmp);
3475 free_extent_buffer(tmp);
3480 * we didn't find a candidate key in this node, walk forward
3481 * and find another one
3483 if (slot >= nritems) {
3484 path->slots[level] = slot;
3485 sret = btrfs_find_next_key(root, path, min_key, level,
3486 cache_only, min_trans);
3488 btrfs_release_path(root, path);
3494 /* save our key for returning back */
3495 btrfs_node_key_to_cpu(cur, &found_key, slot);
3496 path->slots[level] = slot;
3497 if (level == path->lowest_level) {
3499 unlock_up(path, level, 1);
3502 cur = read_node_slot(root, cur, slot);
3504 btrfs_tree_lock(cur);
3505 path->locks[level - 1] = 1;
3506 path->nodes[level - 1] = cur;
3507 unlock_up(path, level, 1);
3511 memcpy(min_key, &found_key, sizeof(found_key));
3516 * this is similar to btrfs_next_leaf, but does not try to preserve
3517 * and fixup the path. It looks for and returns the next key in the
3518 * tree based on the current path and the cache_only and min_trans
3521 * 0 is returned if another key is found, < 0 if there are any errors
3522 * and 1 is returned if there are no higher keys in the tree
3524 * path->keep_locks should be set to 1 on the search made before
3525 * calling this function.
3527 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
3528 struct btrfs_key *key, int lowest_level,
3529 int cache_only, u64 min_trans)
3531 int level = lowest_level;
3533 struct extent_buffer *c;
3535 while(level < BTRFS_MAX_LEVEL) {
3536 if (!path->nodes[level])
3539 slot = path->slots[level] + 1;
3540 c = path->nodes[level];
3542 if (slot >= btrfs_header_nritems(c)) {
3544 if (level == BTRFS_MAX_LEVEL) {
3550 btrfs_item_key_to_cpu(c, key, slot);
3552 u64 blockptr = btrfs_node_blockptr(c, slot);
3553 u64 gen = btrfs_node_ptr_generation(c, slot);
3556 struct extent_buffer *cur;
3557 cur = btrfs_find_tree_block(root, blockptr,
3558 btrfs_level_size(root, level - 1));
3559 if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
3562 free_extent_buffer(cur);
3565 free_extent_buffer(cur);
3567 if (gen < min_trans) {
3571 btrfs_node_key_to_cpu(c, key, slot);
3579 * search the tree again to find a leaf with greater keys
3580 * returns 0 if it found something or 1 if there are no greater leaves.
3581 * returns < 0 on io errors.
3583 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
3587 struct extent_buffer *c;
3588 struct extent_buffer *next = NULL;
3589 struct btrfs_key key;
3593 nritems = btrfs_header_nritems(path->nodes[0]);
3598 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
3600 btrfs_release_path(root, path);
3601 path->keep_locks = 1;
3602 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3603 path->keep_locks = 0;
3608 nritems = btrfs_header_nritems(path->nodes[0]);
3610 * by releasing the path above we dropped all our locks. A balance
3611 * could have added more items next to the key that used to be
3612 * at the very end of the block. So, check again here and
3613 * advance the path if there are now more items available.
3615 if (nritems > 0 && path->slots[0] < nritems - 1) {
3620 while(level < BTRFS_MAX_LEVEL) {
3621 if (!path->nodes[level])
3624 slot = path->slots[level] + 1;
3625 c = path->nodes[level];
3626 if (slot >= btrfs_header_nritems(c)) {
3628 if (level == BTRFS_MAX_LEVEL) {
3635 btrfs_tree_unlock(next);
3636 free_extent_buffer(next);
3639 if (level == 1 && (path->locks[1] || path->skip_locking) &&
3641 reada_for_search(root, path, level, slot, 0);
3643 next = read_node_slot(root, c, slot);
3644 if (!path->skip_locking) {
3645 WARN_ON(!btrfs_tree_locked(c));
3646 btrfs_tree_lock(next);
3650 path->slots[level] = slot;
3653 c = path->nodes[level];
3654 if (path->locks[level])
3655 btrfs_tree_unlock(c);
3656 free_extent_buffer(c);
3657 path->nodes[level] = next;
3658 path->slots[level] = 0;
3659 if (!path->skip_locking)
3660 path->locks[level] = 1;
3663 if (level == 1 && path->locks[1] && path->reada)
3664 reada_for_search(root, path, level, slot, 0);
3665 next = read_node_slot(root, next, 0);
3666 if (!path->skip_locking) {
3667 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3668 btrfs_tree_lock(next);
3672 unlock_up(path, 0, 1);
3677 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
3678 * searching until it gets past min_objectid or finds an item of 'type'
3680 * returns 0 if something is found, 1 if nothing was found and < 0 on error
3682 int btrfs_previous_item(struct btrfs_root *root,
3683 struct btrfs_path *path, u64 min_objectid,
3686 struct btrfs_key found_key;
3687 struct extent_buffer *leaf;
3692 if (path->slots[0] == 0) {
3693 ret = btrfs_prev_leaf(root, path);
3699 leaf = path->nodes[0];
3700 nritems = btrfs_header_nritems(leaf);
3703 if (path->slots[0] == nritems)
3706 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3707 if (found_key.type == type)
3709 if (found_key.objectid < min_objectid)
3711 if (found_key.objectid == min_objectid &&
3712 found_key.type < type)