2 * Copyright (C) 2007 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.
20 #include "transaction.h"
21 #include "print-tree.h"
23 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
24 *root, struct btrfs_path *path, int level);
25 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
26 *root, struct btrfs_key *ins_key,
27 struct btrfs_path *path, int data_size, int extend);
28 static int push_node_left(struct btrfs_trans_handle *trans,
29 struct btrfs_root *root, struct extent_buffer *dst,
30 struct extent_buffer *src, int empty);
31 static int balance_node_right(struct btrfs_trans_handle *trans,
32 struct btrfs_root *root,
33 struct extent_buffer *dst_buf,
34 struct extent_buffer *src_buf);
35 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
36 struct btrfs_path *path, int level, int slot);
38 inline void btrfs_init_path(struct btrfs_path *p)
40 memset(p, 0, sizeof(*p));
43 struct btrfs_path *btrfs_alloc_path(void)
45 struct btrfs_path *path;
46 path = kmalloc(sizeof(struct btrfs_path), GFP_NOFS);
48 btrfs_init_path(path);
54 void btrfs_free_path(struct btrfs_path *p)
56 btrfs_release_path(NULL, p);
60 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
63 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
66 free_extent_buffer(p->nodes[i]);
68 memset(p, 0, sizeof(*p));
71 static void add_root_to_dirty_list(struct btrfs_root *root)
73 if (root->track_dirty && list_empty(&root->dirty_list)) {
74 list_add(&root->dirty_list,
75 &root->fs_info->dirty_cowonly_roots);
79 int btrfs_copy_root(struct btrfs_trans_handle *trans,
80 struct btrfs_root *root,
81 struct extent_buffer *buf,
82 struct extent_buffer **cow_ret, u64 new_root_objectid)
84 struct extent_buffer *cow;
87 struct btrfs_root *new_root;
89 new_root = kmalloc(sizeof(*new_root), GFP_NOFS);
93 memcpy(new_root, root, sizeof(*new_root));
94 new_root->root_key.objectid = new_root_objectid;
96 WARN_ON(root->ref_cows && trans->transid !=
97 root->fs_info->running_transaction->transid);
98 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
100 level = btrfs_header_level(buf);
101 cow = btrfs_alloc_free_block(trans, new_root, buf->len, 0,
102 new_root_objectid, trans->transid,
103 level, buf->start, 0);
109 copy_extent_buffer(cow, buf, 0, 0, cow->len);
110 btrfs_set_header_bytenr(cow, cow->start);
111 btrfs_set_header_generation(cow, trans->transid);
112 btrfs_set_header_owner(cow, new_root_objectid);
113 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
115 WARN_ON(btrfs_header_generation(buf) > trans->transid);
116 ret = btrfs_inc_ref(trans, new_root, buf, cow, NULL);
122 btrfs_mark_buffer_dirty(cow);
127 int __btrfs_cow_block(struct btrfs_trans_handle *trans,
128 struct btrfs_root *root,
129 struct extent_buffer *buf,
130 struct extent_buffer *parent, int parent_slot,
131 struct extent_buffer **cow_ret,
132 u64 search_start, u64 empty_size)
135 struct extent_buffer *cow;
138 int different_trans = 0;
141 WARN_ON(root->ref_cows && trans->transid !=
142 root->fs_info->running_transaction->transid);
143 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
146 parent_start = parent->start;
150 level = btrfs_header_level(buf);
151 nritems = btrfs_header_nritems(buf);
152 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
153 root->root_key.objectid, trans->transid,
154 level, search_start, empty_size);
158 copy_extent_buffer(cow, buf, 0, 0, cow->len);
159 btrfs_set_header_bytenr(cow, cow->start);
160 btrfs_set_header_generation(cow, trans->transid);
161 btrfs_set_header_owner(cow, root->root_key.objectid);
162 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN);
164 WARN_ON(btrfs_header_generation(buf) > trans->transid);
165 if (btrfs_header_generation(buf) != trans->transid) {
167 ret = btrfs_inc_ref(trans, root, buf, cow, NULL);
171 ret = btrfs_update_ref(trans, root, buf, cow, 0, nritems);
174 clean_tree_block(trans, root, buf);
177 if (buf == root->node) {
179 extent_buffer_get(cow);
180 if (buf != root->commit_root) {
181 btrfs_free_extent(trans, root, buf->start,
182 buf->len, buf->start,
183 root->root_key.objectid,
184 btrfs_header_generation(buf),
187 free_extent_buffer(buf);
188 add_root_to_dirty_list(root);
190 btrfs_set_node_blockptr(parent, parent_slot,
192 WARN_ON(trans->transid == 0);
193 btrfs_set_node_ptr_generation(parent, parent_slot,
195 btrfs_mark_buffer_dirty(parent);
196 WARN_ON(btrfs_header_generation(parent) != trans->transid);
197 btrfs_free_extent(trans, root, buf->start, buf->len,
198 parent_start, btrfs_header_owner(parent),
199 btrfs_header_generation(parent), 0, 0, 1);
201 free_extent_buffer(buf);
202 btrfs_mark_buffer_dirty(cow);
207 int btrfs_cow_block(struct btrfs_trans_handle *trans,
208 struct btrfs_root *root, struct extent_buffer *buf,
209 struct extent_buffer *parent, int parent_slot,
210 struct extent_buffer **cow_ret)
215 if (trans->transaction != root->fs_info->running_transaction) {
216 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
217 root->fs_info->running_transaction->transid);
221 if (trans->transid != root->fs_info->generation) {
222 printk(KERN_CRIT "trans %llu running %llu\n",
223 (unsigned long long)trans->transid,
224 (unsigned long long)root->fs_info->generation);
227 if (btrfs_header_generation(buf) == trans->transid &&
228 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
233 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
234 ret = __btrfs_cow_block(trans, root, buf, parent,
235 parent_slot, cow_ret, search_start, 0);
240 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
242 if (blocknr < other && other - (blocknr + blocksize) < 32768)
244 if (blocknr > other && blocknr - (other + blocksize) < 32768)
251 * compare two keys in a memcmp fashion
253 int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
257 btrfs_disk_key_to_cpu(&k1, disk);
259 if (k1.objectid > k2->objectid)
261 if (k1.objectid < k2->objectid)
263 if (k1.type > k2->type)
265 if (k1.type < k2->type)
267 if (k1.offset > k2->offset)
269 if (k1.offset < k2->offset)
276 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
277 struct btrfs_root *root, struct extent_buffer *parent,
278 int start_slot, int cache_only, u64 *last_ret,
279 struct btrfs_key *progress)
281 struct extent_buffer *cur;
282 struct extent_buffer *tmp;
285 u64 search_start = *last_ret;
295 int progress_passed = 0;
296 struct btrfs_disk_key disk_key;
298 parent_level = btrfs_header_level(parent);
299 if (cache_only && parent_level != 1)
302 if (trans->transaction != root->fs_info->running_transaction) {
303 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
304 root->fs_info->running_transaction->transid);
307 if (trans->transid != root->fs_info->generation) {
308 printk(KERN_CRIT "trans %Lu running %Lu\n", trans->transid,
309 root->fs_info->generation);
313 parent_nritems = btrfs_header_nritems(parent);
314 blocksize = btrfs_level_size(root, parent_level - 1);
315 end_slot = parent_nritems;
317 if (parent_nritems == 1)
320 for (i = start_slot; i < end_slot; i++) {
323 if (!parent->map_token) {
324 map_extent_buffer(parent,
325 btrfs_node_key_ptr_offset(i),
326 sizeof(struct btrfs_key_ptr),
327 &parent->map_token, &parent->kaddr,
328 &parent->map_start, &parent->map_len,
331 btrfs_node_key(parent, &disk_key, i);
332 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
336 blocknr = btrfs_node_blockptr(parent, i);
337 gen = btrfs_node_ptr_generation(parent, i);
339 last_block = blocknr;
342 other = btrfs_node_blockptr(parent, i - 1);
343 close = close_blocks(blocknr, other, blocksize);
345 if (close && i < end_slot - 2) {
346 other = btrfs_node_blockptr(parent, i + 1);
347 close = close_blocks(blocknr, other, blocksize);
350 last_block = blocknr;
353 if (parent->map_token) {
354 unmap_extent_buffer(parent, parent->map_token,
356 parent->map_token = NULL;
359 cur = btrfs_find_tree_block(root, blocknr, blocksize);
361 uptodate = btrfs_buffer_uptodate(cur, gen);
364 if (!cur || !uptodate) {
366 free_extent_buffer(cur);
370 cur = read_tree_block(root, blocknr,
372 } else if (!uptodate) {
373 btrfs_read_buffer(cur, gen);
376 if (search_start == 0)
377 search_start = last_block;
379 err = __btrfs_cow_block(trans, root, cur, parent, i,
382 (end_slot - i) * blocksize));
384 free_extent_buffer(cur);
387 search_start = tmp->start;
388 last_block = tmp->start;
389 *last_ret = search_start;
390 if (parent_level == 1)
391 btrfs_clear_buffer_defrag(tmp);
392 free_extent_buffer(tmp);
394 if (parent->map_token) {
395 unmap_extent_buffer(parent, parent->map_token,
397 parent->map_token = NULL;
404 * The leaf data grows from end-to-front in the node.
405 * this returns the address of the start of the last item,
406 * which is the stop of the leaf data stack
408 static inline unsigned int leaf_data_end(struct btrfs_root *root,
409 struct extent_buffer *leaf)
411 u32 nr = btrfs_header_nritems(leaf);
413 return BTRFS_LEAF_DATA_SIZE(root);
414 return btrfs_item_offset_nr(leaf, nr - 1);
417 static int check_node(struct btrfs_root *root, struct btrfs_path *path,
420 struct extent_buffer *parent = NULL;
421 struct extent_buffer *node = path->nodes[level];
422 struct btrfs_disk_key parent_key;
423 struct btrfs_disk_key node_key;
426 struct btrfs_key cpukey;
427 u32 nritems = btrfs_header_nritems(node);
429 if (path->nodes[level + 1])
430 parent = path->nodes[level + 1];
432 slot = path->slots[level];
433 BUG_ON(nritems == 0);
435 parent_slot = path->slots[level + 1];
436 btrfs_node_key(parent, &parent_key, parent_slot);
437 btrfs_node_key(node, &node_key, 0);
438 BUG_ON(memcmp(&parent_key, &node_key,
439 sizeof(struct btrfs_disk_key)));
440 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
441 btrfs_header_bytenr(node));
443 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
445 btrfs_node_key_to_cpu(node, &cpukey, slot - 1);
446 btrfs_node_key(node, &node_key, slot);
447 BUG_ON(btrfs_comp_keys(&node_key, &cpukey) <= 0);
449 if (slot < nritems - 1) {
450 btrfs_node_key_to_cpu(node, &cpukey, slot + 1);
451 btrfs_node_key(node, &node_key, slot);
452 BUG_ON(btrfs_comp_keys(&node_key, &cpukey) >= 0);
457 static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
460 struct extent_buffer *leaf = path->nodes[level];
461 struct extent_buffer *parent = NULL;
463 struct btrfs_key cpukey;
464 struct btrfs_disk_key parent_key;
465 struct btrfs_disk_key leaf_key;
466 int slot = path->slots[0];
468 u32 nritems = btrfs_header_nritems(leaf);
470 if (path->nodes[level + 1])
471 parent = path->nodes[level + 1];
477 parent_slot = path->slots[level + 1];
478 btrfs_node_key(parent, &parent_key, parent_slot);
479 btrfs_item_key(leaf, &leaf_key, 0);
481 BUG_ON(memcmp(&parent_key, &leaf_key,
482 sizeof(struct btrfs_disk_key)));
483 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
484 btrfs_header_bytenr(leaf));
487 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
488 btrfs_item_key_to_cpu(leaf, &cpukey, i + 1);
489 btrfs_item_key(leaf, &leaf_key, i);
490 if (comp_keys(&leaf_key, &cpukey) >= 0) {
491 btrfs_print_leaf(root, leaf);
492 printk("slot %d offset bad key\n", i);
495 if (btrfs_item_offset_nr(leaf, i) !=
496 btrfs_item_end_nr(leaf, i + 1)) {
497 btrfs_print_leaf(root, leaf);
498 printk("slot %d offset bad\n", i);
502 if (btrfs_item_offset_nr(leaf, i) +
503 btrfs_item_size_nr(leaf, i) !=
504 BTRFS_LEAF_DATA_SIZE(root)) {
505 btrfs_print_leaf(root, leaf);
506 printk("slot %d first offset bad\n", i);
512 if (btrfs_item_size_nr(leaf, nritems - 1) > 4096) {
513 btrfs_print_leaf(root, leaf);
514 printk("slot %d bad size \n", nritems - 1);
519 if (slot != 0 && slot < nritems - 1) {
520 btrfs_item_key(leaf, &leaf_key, slot);
521 btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1);
522 if (btrfs_comp_keys(&leaf_key, &cpukey) <= 0) {
523 btrfs_print_leaf(root, leaf);
524 printk("slot %d offset bad key\n", slot);
527 if (btrfs_item_offset_nr(leaf, slot - 1) !=
528 btrfs_item_end_nr(leaf, slot)) {
529 btrfs_print_leaf(root, leaf);
530 printk("slot %d offset bad\n", slot);
534 if (slot < nritems - 1) {
535 btrfs_item_key(leaf, &leaf_key, slot);
536 btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1);
537 BUG_ON(btrfs_comp_keys(&leaf_key, &cpukey) >= 0);
538 if (btrfs_item_offset_nr(leaf, slot) !=
539 btrfs_item_end_nr(leaf, slot + 1)) {
540 btrfs_print_leaf(root, leaf);
541 printk("slot %d offset bad\n", slot);
545 BUG_ON(btrfs_item_offset_nr(leaf, 0) +
546 btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root));
550 static int noinline check_block(struct btrfs_root *root,
551 struct btrfs_path *path, int level)
555 struct extent_buffer *buf = path->nodes[level];
557 if (memcmp_extent_buffer(buf, root->fs_info->fsid,
558 (unsigned long)btrfs_header_fsid(buf),
560 printk("warning bad block %Lu\n", buf->start);
565 return check_leaf(root, path, level);
566 return check_node(root, path, level);
570 * search for key in the extent_buffer. The items start at offset p,
571 * and they are item_size apart. There are 'max' items in p.
573 * the slot in the array is returned via slot, and it points to
574 * the place where you would insert key if it is not found in
577 * slot may point to max if the key is bigger than all of the keys
579 static int generic_bin_search(struct extent_buffer *eb, unsigned long p,
580 int item_size, struct btrfs_key *key,
587 unsigned long offset;
588 struct btrfs_disk_key *tmp;
591 mid = (low + high) / 2;
592 offset = p + mid * item_size;
594 tmp = (struct btrfs_disk_key *)(eb->data + offset);
595 ret = btrfs_comp_keys(tmp, key);
611 * simple bin_search frontend that does the right thing for
614 static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
615 int level, int *slot)
618 return generic_bin_search(eb,
619 offsetof(struct btrfs_leaf, items),
620 sizeof(struct btrfs_item),
621 key, btrfs_header_nritems(eb),
624 return generic_bin_search(eb,
625 offsetof(struct btrfs_node, ptrs),
626 sizeof(struct btrfs_key_ptr),
627 key, btrfs_header_nritems(eb),
633 static struct extent_buffer *read_node_slot(struct btrfs_root *root,
634 struct extent_buffer *parent, int slot)
636 int level = btrfs_header_level(parent);
639 if (slot >= btrfs_header_nritems(parent))
644 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
645 btrfs_level_size(root, level - 1),
646 btrfs_node_ptr_generation(parent, slot));
649 static int balance_level(struct btrfs_trans_handle *trans,
650 struct btrfs_root *root,
651 struct btrfs_path *path, int level)
653 struct extent_buffer *right = NULL;
654 struct extent_buffer *mid;
655 struct extent_buffer *left = NULL;
656 struct extent_buffer *parent = NULL;
660 int orig_slot = path->slots[level];
661 int err_on_enospc = 0;
667 mid = path->nodes[level];
668 WARN_ON(btrfs_header_generation(mid) != trans->transid);
670 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
672 if (level < BTRFS_MAX_LEVEL - 1)
673 parent = path->nodes[level + 1];
674 pslot = path->slots[level + 1];
677 * deal with the case where there is only one pointer in the root
678 * by promoting the node below to a root
681 struct extent_buffer *child;
683 if (btrfs_header_nritems(mid) != 1)
686 /* promote the child to a root */
687 child = read_node_slot(root, mid, 0);
689 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
694 ret = btrfs_update_extent_ref(trans, root, child->start,
695 mid->start, child->start,
696 root->root_key.objectid,
701 add_root_to_dirty_list(root);
702 path->nodes[level] = NULL;
703 clean_tree_block(trans, root, mid);
704 wait_on_tree_block_writeback(root, mid);
705 /* once for the path */
706 free_extent_buffer(mid);
707 ret = btrfs_free_extent(trans, root, mid->start, mid->len,
708 mid->start, root->root_key.objectid,
709 btrfs_header_generation(mid), 0, 0, 1);
710 /* once for the root ptr */
711 free_extent_buffer(mid);
714 if (btrfs_header_nritems(mid) >
715 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
718 if (btrfs_header_nritems(mid) < 2)
721 left = read_node_slot(root, parent, pslot - 1);
723 wret = btrfs_cow_block(trans, root, left,
724 parent, pslot - 1, &left);
730 right = read_node_slot(root, parent, pslot + 1);
732 wret = btrfs_cow_block(trans, root, right,
733 parent, pslot + 1, &right);
740 /* first, try to make some room in the middle buffer */
742 orig_slot += btrfs_header_nritems(left);
743 wret = push_node_left(trans, root, left, mid, 1);
746 if (btrfs_header_nritems(mid) < 2)
751 * then try to empty the right most buffer into the middle
754 wret = push_node_left(trans, root, mid, right, 1);
755 if (wret < 0 && wret != -ENOSPC)
757 if (btrfs_header_nritems(right) == 0) {
758 u64 bytenr = right->start;
759 u64 generation = btrfs_header_generation(parent);
760 u32 blocksize = right->len;
762 clean_tree_block(trans, root, right);
763 wait_on_tree_block_writeback(root, right);
764 free_extent_buffer(right);
766 wret = del_ptr(trans, root, path, level + 1, pslot +
770 wret = btrfs_free_extent(trans, root, bytenr,
771 blocksize, parent->start,
772 btrfs_header_owner(parent),
773 generation, 0, 0, 1);
777 struct btrfs_disk_key right_key;
778 btrfs_node_key(right, &right_key, 0);
779 btrfs_set_node_key(parent, &right_key, pslot + 1);
780 btrfs_mark_buffer_dirty(parent);
783 if (btrfs_header_nritems(mid) == 1) {
785 * we're not allowed to leave a node with one item in the
786 * tree during a delete. A deletion from lower in the tree
787 * could try to delete the only pointer in this node.
788 * So, pull some keys from the left.
789 * There has to be a left pointer at this point because
790 * otherwise we would have pulled some pointers from the
794 wret = balance_node_right(trans, root, mid, left);
800 wret = push_node_left(trans, root, left, mid, 1);
806 if (btrfs_header_nritems(mid) == 0) {
807 /* we've managed to empty the middle node, drop it */
808 u64 root_gen = btrfs_header_generation(parent);
809 u64 bytenr = mid->start;
810 u32 blocksize = mid->len;
811 clean_tree_block(trans, root, mid);
812 wait_on_tree_block_writeback(root, mid);
813 free_extent_buffer(mid);
815 wret = del_ptr(trans, root, path, level + 1, pslot);
818 wret = btrfs_free_extent(trans, root, bytenr, blocksize,
820 btrfs_header_owner(parent),
825 /* update the parent key to reflect our changes */
826 struct btrfs_disk_key mid_key;
827 btrfs_node_key(mid, &mid_key, 0);
828 btrfs_set_node_key(parent, &mid_key, pslot);
829 btrfs_mark_buffer_dirty(parent);
832 /* update the path */
834 if (btrfs_header_nritems(left) > orig_slot) {
835 extent_buffer_get(left);
836 path->nodes[level] = left;
837 path->slots[level + 1] -= 1;
838 path->slots[level] = orig_slot;
840 free_extent_buffer(mid);
842 orig_slot -= btrfs_header_nritems(left);
843 path->slots[level] = orig_slot;
846 /* double check we haven't messed things up */
847 check_block(root, path, level);
849 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
853 free_extent_buffer(right);
855 free_extent_buffer(left);
859 /* returns zero if the push worked, non-zero otherwise */
860 static int noinline push_nodes_for_insert(struct btrfs_trans_handle *trans,
861 struct btrfs_root *root,
862 struct btrfs_path *path, int level)
864 struct extent_buffer *right = NULL;
865 struct extent_buffer *mid;
866 struct extent_buffer *left = NULL;
867 struct extent_buffer *parent = NULL;
871 int orig_slot = path->slots[level];
877 mid = path->nodes[level];
878 WARN_ON(btrfs_header_generation(mid) != trans->transid);
879 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
881 if (level < BTRFS_MAX_LEVEL - 1)
882 parent = path->nodes[level + 1];
883 pslot = path->slots[level + 1];
888 left = read_node_slot(root, parent, pslot - 1);
890 /* first, try to make some room in the middle buffer */
893 left_nr = btrfs_header_nritems(left);
894 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
897 ret = btrfs_cow_block(trans, root, left, parent,
902 wret = push_node_left(trans, root,
909 struct btrfs_disk_key disk_key;
910 orig_slot += left_nr;
911 btrfs_node_key(mid, &disk_key, 0);
912 btrfs_set_node_key(parent, &disk_key, pslot);
913 btrfs_mark_buffer_dirty(parent);
914 if (btrfs_header_nritems(left) > orig_slot) {
915 path->nodes[level] = left;
916 path->slots[level + 1] -= 1;
917 path->slots[level] = orig_slot;
918 free_extent_buffer(mid);
921 btrfs_header_nritems(left);
922 path->slots[level] = orig_slot;
923 free_extent_buffer(left);
927 free_extent_buffer(left);
929 right= read_node_slot(root, parent, pslot + 1);
932 * then try to empty the right most buffer into the middle
936 right_nr = btrfs_header_nritems(right);
937 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
940 ret = btrfs_cow_block(trans, root, right,
946 wret = balance_node_right(trans, root,
953 struct btrfs_disk_key disk_key;
955 btrfs_node_key(right, &disk_key, 0);
956 btrfs_set_node_key(parent, &disk_key, pslot + 1);
957 btrfs_mark_buffer_dirty(parent);
959 if (btrfs_header_nritems(mid) <= orig_slot) {
960 path->nodes[level] = right;
961 path->slots[level + 1] += 1;
962 path->slots[level] = orig_slot -
963 btrfs_header_nritems(mid);
964 free_extent_buffer(mid);
966 free_extent_buffer(right);
970 free_extent_buffer(right);
976 * readahead one full node of leaves
978 static void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
979 int level, int slot, u64 objectid)
981 struct extent_buffer *node;
982 struct btrfs_disk_key disk_key;
988 int direction = path->reada;
989 struct extent_buffer *eb;
997 if (!path->nodes[level])
1000 node = path->nodes[level];
1001 search = btrfs_node_blockptr(node, slot);
1002 blocksize = btrfs_level_size(root, level - 1);
1003 eb = btrfs_find_tree_block(root, search, blocksize);
1005 free_extent_buffer(eb);
1009 highest_read = search;
1010 lowest_read = search;
1012 nritems = btrfs_header_nritems(node);
1015 if (direction < 0) {
1019 } else if (direction > 0) {
1024 if (path->reada < 0 && objectid) {
1025 btrfs_node_key(node, &disk_key, nr);
1026 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1029 search = btrfs_node_blockptr(node, nr);
1030 if ((search >= lowest_read && search <= highest_read) ||
1031 (search < lowest_read && lowest_read - search <= 32768) ||
1032 (search > highest_read && search - highest_read <= 32768)) {
1033 readahead_tree_block(root, search, blocksize,
1034 btrfs_node_ptr_generation(node, nr));
1038 if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
1040 if(nread > (1024 * 1024) || nscan > 128)
1043 if (search < lowest_read)
1044 lowest_read = search;
1045 if (search > highest_read)
1046 highest_read = search;
1051 * look for key in the tree. path is filled in with nodes along the way
1052 * if key is found, we return zero and you can find the item in the leaf
1053 * level of the path (level 0)
1055 * If the key isn't found, the path points to the slot where it should
1056 * be inserted, and 1 is returned. If there are other errors during the
1057 * search a negative error number is returned.
1059 * if ins_len > 0, nodes and leaves will be split as we walk down the
1060 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1063 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1064 *root, struct btrfs_key *key, struct btrfs_path *p, int
1067 struct extent_buffer *b;
1071 int should_reada = p->reada;
1072 u8 lowest_level = 0;
1074 lowest_level = p->lowest_level;
1075 WARN_ON(lowest_level && ins_len);
1076 WARN_ON(p->nodes[0] != NULL);
1078 WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1082 extent_buffer_get(b);
1084 level = btrfs_header_level(b);
1087 wret = btrfs_cow_block(trans, root, b,
1088 p->nodes[level + 1],
1089 p->slots[level + 1],
1092 free_extent_buffer(b);
1096 BUG_ON(!cow && ins_len);
1097 if (level != btrfs_header_level(b))
1099 level = btrfs_header_level(b);
1100 p->nodes[level] = b;
1101 ret = check_block(root, p, level);
1104 ret = bin_search(b, key, level, &slot);
1106 if (ret && slot > 0)
1108 p->slots[level] = slot;
1109 if (ins_len > 0 && btrfs_header_nritems(b) >=
1110 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1111 int sret = split_node(trans, root, p, level);
1115 b = p->nodes[level];
1116 slot = p->slots[level];
1117 } else if (ins_len < 0) {
1118 int sret = balance_level(trans, root, p,
1122 b = p->nodes[level];
1124 btrfs_release_path(NULL, p);
1127 slot = p->slots[level];
1128 BUG_ON(btrfs_header_nritems(b) == 1);
1130 /* this is only true while dropping a snapshot */
1131 if (level == lowest_level)
1135 reada_for_search(root, p, level, slot,
1138 b = read_node_slot(root, b, slot);
1140 p->slots[level] = slot;
1141 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
1142 sizeof(struct btrfs_item) + ins_len) {
1143 int sret = split_leaf(trans, root, key,
1144 p, ins_len, ret == 0);
1156 * adjust the pointers going up the tree, starting at level
1157 * making sure the right key of each node is points to 'key'.
1158 * This is used after shifting pointers to the left, so it stops
1159 * fixing up pointers when a given leaf/node is not in slot 0 of the
1162 * If this fails to write a tree block, it returns -1, but continues
1163 * fixing up the blocks in ram so the tree is consistent.
1165 static int fixup_low_keys(struct btrfs_trans_handle *trans,
1166 struct btrfs_root *root, struct btrfs_path *path,
1167 struct btrfs_disk_key *key, int level)
1171 struct extent_buffer *t;
1173 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1174 int tslot = path->slots[i];
1175 if (!path->nodes[i])
1178 btrfs_set_node_key(t, key, tslot);
1179 btrfs_mark_buffer_dirty(path->nodes[i]);
1189 * This function isn't completely safe. It's the caller's responsibility
1190 * that the new key won't break the order
1192 int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1193 struct btrfs_root *root, struct btrfs_path *path,
1194 struct btrfs_key *new_key)
1196 struct btrfs_disk_key disk_key;
1197 struct extent_buffer *eb;
1200 eb = path->nodes[0];
1201 slot = path->slots[0];
1203 btrfs_item_key(eb, &disk_key, slot - 1);
1204 if (btrfs_comp_keys(&disk_key, new_key) >= 0)
1207 if (slot < btrfs_header_nritems(eb) - 1) {
1208 btrfs_item_key(eb, &disk_key, slot + 1);
1209 if (btrfs_comp_keys(&disk_key, new_key) <= 0)
1213 btrfs_cpu_key_to_disk(&disk_key, new_key);
1214 btrfs_set_item_key(eb, &disk_key, slot);
1215 btrfs_mark_buffer_dirty(eb);
1217 fixup_low_keys(trans, root, path, &disk_key, 1);
1222 * try to push data from one node into the next node left in the
1225 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1226 * error, and > 0 if there was no room in the left hand block.
1228 static int push_node_left(struct btrfs_trans_handle *trans,
1229 struct btrfs_root *root, struct extent_buffer *dst,
1230 struct extent_buffer *src, int empty)
1237 src_nritems = btrfs_header_nritems(src);
1238 dst_nritems = btrfs_header_nritems(dst);
1239 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1240 WARN_ON(btrfs_header_generation(src) != trans->transid);
1241 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1243 if (!empty && src_nritems <= 8)
1246 if (push_items <= 0) {
1251 push_items = min(src_nritems, push_items);
1252 if (push_items < src_nritems) {
1253 /* leave at least 8 pointers in the node if
1254 * we aren't going to empty it
1256 if (src_nritems - push_items < 8) {
1257 if (push_items <= 8)
1263 push_items = min(src_nritems - 8, push_items);
1265 copy_extent_buffer(dst, src,
1266 btrfs_node_key_ptr_offset(dst_nritems),
1267 btrfs_node_key_ptr_offset(0),
1268 push_items * sizeof(struct btrfs_key_ptr));
1270 if (push_items < src_nritems) {
1271 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
1272 btrfs_node_key_ptr_offset(push_items),
1273 (src_nritems - push_items) *
1274 sizeof(struct btrfs_key_ptr));
1276 btrfs_set_header_nritems(src, src_nritems - push_items);
1277 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1278 btrfs_mark_buffer_dirty(src);
1279 btrfs_mark_buffer_dirty(dst);
1281 ret = btrfs_update_ref(trans, root, src, dst, dst_nritems, push_items);
1287 * try to push data from one node into the next node right in the
1290 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1291 * error, and > 0 if there was no room in the right hand block.
1293 * this will only push up to 1/2 the contents of the left node over
1295 static int balance_node_right(struct btrfs_trans_handle *trans,
1296 struct btrfs_root *root,
1297 struct extent_buffer *dst,
1298 struct extent_buffer *src)
1306 WARN_ON(btrfs_header_generation(src) != trans->transid);
1307 WARN_ON(btrfs_header_generation(dst) != trans->transid);
1309 src_nritems = btrfs_header_nritems(src);
1310 dst_nritems = btrfs_header_nritems(dst);
1311 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1312 if (push_items <= 0) {
1316 if (src_nritems < 4) {
1320 max_push = src_nritems / 2 + 1;
1321 /* don't try to empty the node */
1322 if (max_push >= src_nritems) {
1326 if (max_push < push_items)
1327 push_items = max_push;
1329 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
1330 btrfs_node_key_ptr_offset(0),
1332 sizeof(struct btrfs_key_ptr));
1334 copy_extent_buffer(dst, src,
1335 btrfs_node_key_ptr_offset(0),
1336 btrfs_node_key_ptr_offset(src_nritems - push_items),
1337 push_items * sizeof(struct btrfs_key_ptr));
1339 btrfs_set_header_nritems(src, src_nritems - push_items);
1340 btrfs_set_header_nritems(dst, dst_nritems + push_items);
1342 btrfs_mark_buffer_dirty(src);
1343 btrfs_mark_buffer_dirty(dst);
1345 ret = btrfs_update_ref(trans, root, src, dst, 0, push_items);
1351 * helper function to insert a new root level in the tree.
1352 * A new node is allocated, and a single item is inserted to
1353 * point to the existing root
1355 * returns zero on success or < 0 on failure.
1357 static int noinline insert_new_root(struct btrfs_trans_handle *trans,
1358 struct btrfs_root *root,
1359 struct btrfs_path *path, int level)
1362 struct extent_buffer *lower;
1363 struct extent_buffer *c;
1364 struct extent_buffer *old;
1365 struct btrfs_disk_key lower_key;
1368 BUG_ON(path->nodes[level]);
1369 BUG_ON(path->nodes[level-1] != root->node);
1371 lower = path->nodes[level-1];
1373 btrfs_item_key(lower, &lower_key, 0);
1375 btrfs_node_key(lower, &lower_key, 0);
1377 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
1378 root->root_key.objectid,
1379 trans->transid, level,
1380 root->node->start, 0);
1384 memset_extent_buffer(c, 0, 0, root->nodesize);
1385 btrfs_set_header_nritems(c, 1);
1386 btrfs_set_header_level(c, level);
1387 btrfs_set_header_bytenr(c, c->start);
1388 btrfs_set_header_generation(c, trans->transid);
1389 btrfs_set_header_owner(c, root->root_key.objectid);
1391 write_extent_buffer(c, root->fs_info->fsid,
1392 (unsigned long)btrfs_header_fsid(c),
1395 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
1396 (unsigned long)btrfs_header_chunk_tree_uuid(c),
1399 btrfs_set_node_key(c, &lower_key, 0);
1400 btrfs_set_node_blockptr(c, 0, lower->start);
1401 lower_gen = btrfs_header_generation(lower);
1402 WARN_ON(lower_gen != trans->transid);
1404 btrfs_set_node_ptr_generation(c, 0, lower_gen);
1406 btrfs_mark_buffer_dirty(c);
1411 ret = btrfs_update_extent_ref(trans, root, lower->start,
1412 lower->start, c->start,
1413 root->root_key.objectid,
1414 trans->transid, level - 1, 0);
1417 /* the super has an extra ref to root->node */
1418 free_extent_buffer(old);
1420 add_root_to_dirty_list(root);
1421 extent_buffer_get(c);
1422 path->nodes[level] = c;
1423 path->slots[level] = 0;
1428 * worker function to insert a single pointer in a node.
1429 * the node should have enough room for the pointer already
1431 * slot and level indicate where you want the key to go, and
1432 * blocknr is the block the key points to.
1434 * returns zero on success and < 0 on any error
1436 static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
1437 *root, struct btrfs_path *path, struct btrfs_disk_key
1438 *key, u64 bytenr, int slot, int level)
1440 struct extent_buffer *lower;
1443 BUG_ON(!path->nodes[level]);
1444 lower = path->nodes[level];
1445 nritems = btrfs_header_nritems(lower);
1448 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
1450 if (slot != nritems) {
1451 memmove_extent_buffer(lower,
1452 btrfs_node_key_ptr_offset(slot + 1),
1453 btrfs_node_key_ptr_offset(slot),
1454 (nritems - slot) * sizeof(struct btrfs_key_ptr));
1456 btrfs_set_node_key(lower, key, slot);
1457 btrfs_set_node_blockptr(lower, slot, bytenr);
1458 WARN_ON(trans->transid == 0);
1459 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
1460 btrfs_set_header_nritems(lower, nritems + 1);
1461 btrfs_mark_buffer_dirty(lower);
1466 * split the node at the specified level in path in two.
1467 * The path is corrected to point to the appropriate node after the split
1469 * Before splitting this tries to make some room in the node by pushing
1470 * left and right, if either one works, it returns right away.
1472 * returns 0 on success and < 0 on failure
1474 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
1475 *root, struct btrfs_path *path, int level)
1477 struct extent_buffer *c;
1478 struct extent_buffer *split;
1479 struct btrfs_disk_key disk_key;
1485 c = path->nodes[level];
1486 WARN_ON(btrfs_header_generation(c) != trans->transid);
1487 if (c == root->node) {
1488 /* trying to split the root, lets make a new one */
1489 ret = insert_new_root(trans, root, path, level + 1);
1493 ret = push_nodes_for_insert(trans, root, path, level);
1494 c = path->nodes[level];
1495 if (!ret && btrfs_header_nritems(c) <
1496 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
1502 c_nritems = btrfs_header_nritems(c);
1504 btrfs_node_key(c, &disk_key, 0);
1505 split = btrfs_alloc_free_block(trans, root, root->nodesize,
1506 path->nodes[level + 1]->start,
1507 root->root_key.objectid,
1508 trans->transid, level, c->start, 0);
1510 return PTR_ERR(split);
1512 btrfs_set_header_flags(split, btrfs_header_flags(c));
1513 btrfs_set_header_level(split, btrfs_header_level(c));
1514 btrfs_set_header_bytenr(split, split->start);
1515 btrfs_set_header_generation(split, trans->transid);
1516 btrfs_set_header_owner(split, root->root_key.objectid);
1517 btrfs_set_header_flags(split, 0);
1518 write_extent_buffer(split, root->fs_info->fsid,
1519 (unsigned long)btrfs_header_fsid(split),
1521 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
1522 (unsigned long)btrfs_header_chunk_tree_uuid(split),
1525 mid = (c_nritems + 1) / 2;
1527 copy_extent_buffer(split, c,
1528 btrfs_node_key_ptr_offset(0),
1529 btrfs_node_key_ptr_offset(mid),
1530 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
1531 btrfs_set_header_nritems(split, c_nritems - mid);
1532 btrfs_set_header_nritems(c, mid);
1535 btrfs_mark_buffer_dirty(c);
1536 btrfs_mark_buffer_dirty(split);
1538 btrfs_node_key(split, &disk_key, 0);
1539 wret = insert_ptr(trans, root, path, &disk_key, split->start,
1540 path->slots[level + 1] + 1,
1545 ret = btrfs_update_ref(trans, root, c, split, 0, c_nritems - mid);
1548 if (path->slots[level] >= mid) {
1549 path->slots[level] -= mid;
1550 free_extent_buffer(c);
1551 path->nodes[level] = split;
1552 path->slots[level + 1] += 1;
1554 free_extent_buffer(split);
1560 * how many bytes are required to store the items in a leaf. start
1561 * and nr indicate which items in the leaf to check. This totals up the
1562 * space used both by the item structs and the item data
1564 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
1567 int nritems = btrfs_header_nritems(l);
1568 int end = min(nritems, start + nr) - 1;
1572 data_len = btrfs_item_end_nr(l, start);
1573 data_len = data_len - btrfs_item_offset_nr(l, end);
1574 data_len += sizeof(struct btrfs_item) * nr;
1575 WARN_ON(data_len < 0);
1580 * The space between the end of the leaf items and
1581 * the start of the leaf data. IOW, how much room
1582 * the leaf has left for both items and data
1584 int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf)
1586 int nritems = btrfs_header_nritems(leaf);
1588 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
1590 printk("leaf free space ret %d, leaf data size %lu, used %d nritems %d\n",
1591 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
1592 leaf_space_used(leaf, 0, nritems), nritems);
1598 * push some data in the path leaf to the right, trying to free up at
1599 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1601 * returns 1 if the push failed because the other node didn't have enough
1602 * room, 0 if everything worked out and < 0 if there were major errors.
1604 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1605 *root, struct btrfs_path *path, int data_size,
1608 struct extent_buffer *left = path->nodes[0];
1609 struct extent_buffer *right;
1610 struct extent_buffer *upper;
1611 struct btrfs_disk_key disk_key;
1617 struct btrfs_item *item;
1625 slot = path->slots[1];
1626 if (!path->nodes[1]) {
1629 upper = path->nodes[1];
1630 if (slot >= btrfs_header_nritems(upper) - 1)
1633 right = read_node_slot(root, upper, slot + 1);
1634 free_space = btrfs_leaf_free_space(root, right);
1635 if (free_space < data_size + sizeof(struct btrfs_item)) {
1636 free_extent_buffer(right);
1640 /* cow and double check */
1641 ret = btrfs_cow_block(trans, root, right, upper,
1644 free_extent_buffer(right);
1647 free_space = btrfs_leaf_free_space(root, right);
1648 if (free_space < data_size + sizeof(struct btrfs_item)) {
1649 free_extent_buffer(right);
1653 left_nritems = btrfs_header_nritems(left);
1654 if (left_nritems == 0) {
1655 free_extent_buffer(right);
1664 i = left_nritems - 1;
1666 item = btrfs_item_nr(left, i);
1668 if (path->slots[0] == i)
1669 push_space += data_size + sizeof(*item);
1671 this_item_size = btrfs_item_size(left, item);
1672 if (this_item_size + sizeof(*item) + push_space > free_space)
1675 push_space += this_item_size + sizeof(*item);
1681 if (push_items == 0) {
1682 free_extent_buffer(right);
1686 if (!empty && push_items == left_nritems)
1689 /* push left to right */
1690 right_nritems = btrfs_header_nritems(right);
1692 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
1693 push_space -= leaf_data_end(root, left);
1695 /* make room in the right data area */
1696 data_end = leaf_data_end(root, right);
1697 memmove_extent_buffer(right,
1698 btrfs_leaf_data(right) + data_end - push_space,
1699 btrfs_leaf_data(right) + data_end,
1700 BTRFS_LEAF_DATA_SIZE(root) - data_end);
1702 /* copy from the left data area */
1703 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
1704 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1705 btrfs_leaf_data(left) + leaf_data_end(root, left),
1708 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
1709 btrfs_item_nr_offset(0),
1710 right_nritems * sizeof(struct btrfs_item));
1712 /* copy the items from left to right */
1713 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
1714 btrfs_item_nr_offset(left_nritems - push_items),
1715 push_items * sizeof(struct btrfs_item));
1717 /* update the item pointers */
1718 right_nritems += push_items;
1719 btrfs_set_header_nritems(right, right_nritems);
1720 push_space = BTRFS_LEAF_DATA_SIZE(root);
1721 for (i = 0; i < right_nritems; i++) {
1722 item = btrfs_item_nr(right, i);
1723 push_space -= btrfs_item_size(right, item);
1724 btrfs_set_item_offset(right, item, push_space);
1727 left_nritems -= push_items;
1728 btrfs_set_header_nritems(left, left_nritems);
1731 btrfs_mark_buffer_dirty(left);
1732 btrfs_mark_buffer_dirty(right);
1734 btrfs_item_key(right, &disk_key, 0);
1735 btrfs_set_node_key(upper, &disk_key, slot + 1);
1736 btrfs_mark_buffer_dirty(upper);
1738 ret = btrfs_update_ref(trans, root, left, right, 0, push_items);
1741 /* then fixup the leaf pointer in the path */
1742 if (path->slots[0] >= left_nritems) {
1743 path->slots[0] -= left_nritems;
1744 free_extent_buffer(path->nodes[0]);
1745 path->nodes[0] = right;
1746 path->slots[1] += 1;
1748 free_extent_buffer(right);
1753 * push some data in the path leaf to the left, trying to free up at
1754 * least data_size bytes. returns zero if the push worked, nonzero otherwise
1756 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1757 *root, struct btrfs_path *path, int data_size,
1760 struct btrfs_disk_key disk_key;
1761 struct extent_buffer *right = path->nodes[0];
1762 struct extent_buffer *left;
1768 struct btrfs_item *item;
1769 u32 old_left_nritems;
1775 u32 old_left_item_size;
1777 slot = path->slots[1];
1780 if (!path->nodes[1])
1783 right_nritems = btrfs_header_nritems(right);
1784 if (right_nritems == 0) {
1788 left = read_node_slot(root, path->nodes[1], slot - 1);
1789 free_space = btrfs_leaf_free_space(root, left);
1790 if (free_space < data_size + sizeof(struct btrfs_item)) {
1791 free_extent_buffer(left);
1795 /* cow and double check */
1796 ret = btrfs_cow_block(trans, root, left,
1797 path->nodes[1], slot - 1, &left);
1799 /* we hit -ENOSPC, but it isn't fatal here */
1800 free_extent_buffer(left);
1804 free_space = btrfs_leaf_free_space(root, left);
1805 if (free_space < data_size + sizeof(struct btrfs_item)) {
1806 free_extent_buffer(left);
1813 nr = right_nritems - 1;
1815 for (i = 0; i < nr; i++) {
1816 item = btrfs_item_nr(right, i);
1818 if (path->slots[0] == i)
1819 push_space += data_size + sizeof(*item);
1821 this_item_size = btrfs_item_size(right, item);
1822 if (this_item_size + sizeof(*item) + push_space > free_space)
1826 push_space += this_item_size + sizeof(*item);
1829 if (push_items == 0) {
1830 free_extent_buffer(left);
1833 if (!empty && push_items == btrfs_header_nritems(right))
1836 /* push data from right to left */
1837 copy_extent_buffer(left, right,
1838 btrfs_item_nr_offset(btrfs_header_nritems(left)),
1839 btrfs_item_nr_offset(0),
1840 push_items * sizeof(struct btrfs_item));
1842 push_space = BTRFS_LEAF_DATA_SIZE(root) -
1843 btrfs_item_offset_nr(right, push_items -1);
1845 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
1846 leaf_data_end(root, left) - push_space,
1847 btrfs_leaf_data(right) +
1848 btrfs_item_offset_nr(right, push_items - 1),
1850 old_left_nritems = btrfs_header_nritems(left);
1851 BUG_ON(old_left_nritems < 0);
1853 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
1854 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
1857 item = btrfs_item_nr(left, i);
1858 ioff = btrfs_item_offset(left, item);
1859 btrfs_set_item_offset(left, item,
1860 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
1862 btrfs_set_header_nritems(left, old_left_nritems + push_items);
1864 /* fixup right node */
1865 if (push_items > right_nritems) {
1866 printk("push items %d nr %u\n", push_items, right_nritems);
1870 if (push_items < right_nritems) {
1871 push_space = btrfs_item_offset_nr(right, push_items - 1) -
1872 leaf_data_end(root, right);
1873 memmove_extent_buffer(right, btrfs_leaf_data(right) +
1874 BTRFS_LEAF_DATA_SIZE(root) - push_space,
1875 btrfs_leaf_data(right) +
1876 leaf_data_end(root, right), push_space);
1878 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
1879 btrfs_item_nr_offset(push_items),
1880 (btrfs_header_nritems(right) - push_items) *
1881 sizeof(struct btrfs_item));
1883 right_nritems -= push_items;
1884 btrfs_set_header_nritems(right, right_nritems);
1885 push_space = BTRFS_LEAF_DATA_SIZE(root);
1886 for (i = 0; i < right_nritems; i++) {
1887 item = btrfs_item_nr(right, i);
1888 push_space = push_space - btrfs_item_size(right, item);
1889 btrfs_set_item_offset(right, item, push_space);
1892 btrfs_mark_buffer_dirty(left);
1894 btrfs_mark_buffer_dirty(right);
1896 btrfs_item_key(right, &disk_key, 0);
1897 wret = fixup_low_keys(trans, root, path, &disk_key, 1);
1901 ret = btrfs_update_ref(trans, root, right, left,
1902 old_left_nritems, push_items);
1905 /* then fixup the leaf pointer in the path */
1906 if (path->slots[0] < push_items) {
1907 path->slots[0] += old_left_nritems;
1908 free_extent_buffer(path->nodes[0]);
1909 path->nodes[0] = left;
1910 path->slots[1] -= 1;
1912 free_extent_buffer(left);
1913 path->slots[0] -= push_items;
1915 BUG_ON(path->slots[0] < 0);
1920 * split the path's leaf in two, making sure there is at least data_size
1921 * available for the resulting leaf level of the path.
1923 * returns 0 if all went well and < 0 on failure.
1925 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
1926 *root, struct btrfs_key *ins_key,
1927 struct btrfs_path *path, int data_size, int extend)
1929 struct extent_buffer *l;
1933 struct extent_buffer *right;
1934 int space_needed = data_size + sizeof(struct btrfs_item);
1941 int num_doubles = 0;
1942 struct btrfs_disk_key disk_key;
1945 space_needed = data_size;
1947 /* first try to make some room by pushing left and right */
1948 if (ins_key->type != BTRFS_DIR_ITEM_KEY) {
1949 wret = push_leaf_right(trans, root, path, data_size, 0);
1954 wret = push_leaf_left(trans, root, path, data_size, 0);
1960 /* did the pushes work? */
1961 if (btrfs_leaf_free_space(root, l) >= space_needed)
1965 if (!path->nodes[1]) {
1966 ret = insert_new_root(trans, root, path, 1);
1973 slot = path->slots[0];
1974 nritems = btrfs_header_nritems(l);
1975 mid = (nritems + 1)/ 2;
1977 right = btrfs_alloc_free_block(trans, root, root->leafsize,
1978 path->nodes[1]->start,
1979 root->root_key.objectid,
1980 trans->transid, 0, l->start, 0);
1981 if (IS_ERR(right)) {
1983 return PTR_ERR(right);
1986 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
1987 btrfs_set_header_bytenr(right, right->start);
1988 btrfs_set_header_generation(right, trans->transid);
1989 btrfs_set_header_owner(right, root->root_key.objectid);
1990 btrfs_set_header_level(right, 0);
1991 write_extent_buffer(right, root->fs_info->fsid,
1992 (unsigned long)btrfs_header_fsid(right),
1995 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
1996 (unsigned long)btrfs_header_chunk_tree_uuid(right),
2000 leaf_space_used(l, mid, nritems - mid) + space_needed >
2001 BTRFS_LEAF_DATA_SIZE(root)) {
2002 if (slot >= nritems) {
2003 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2004 btrfs_set_header_nritems(right, 0);
2005 wret = insert_ptr(trans, root, path,
2006 &disk_key, right->start,
2007 path->slots[1] + 1, 1);
2010 free_extent_buffer(path->nodes[0]);
2011 path->nodes[0] = right;
2013 path->slots[1] += 1;
2017 if (mid != nritems &&
2018 leaf_space_used(l, mid, nritems - mid) +
2019 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2024 if (leaf_space_used(l, 0, mid + 1) + space_needed >
2025 BTRFS_LEAF_DATA_SIZE(root)) {
2026 if (!extend && slot == 0) {
2027 btrfs_cpu_key_to_disk(&disk_key, ins_key);
2028 btrfs_set_header_nritems(right, 0);
2029 wret = insert_ptr(trans, root, path,
2035 free_extent_buffer(path->nodes[0]);
2036 path->nodes[0] = right;
2038 if (path->slots[1] == 0) {
2039 wret = fixup_low_keys(trans, root,
2040 path, &disk_key, 1);
2045 } else if (extend && slot == 0) {
2049 if (mid != nritems &&
2050 leaf_space_used(l, mid, nritems - mid) +
2051 space_needed > BTRFS_LEAF_DATA_SIZE(root)) {
2057 nritems = nritems - mid;
2058 btrfs_set_header_nritems(right, nritems);
2059 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
2061 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
2062 btrfs_item_nr_offset(mid),
2063 nritems * sizeof(struct btrfs_item));
2065 copy_extent_buffer(right, l,
2066 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2067 data_copy_size, btrfs_leaf_data(l) +
2068 leaf_data_end(root, l), data_copy_size);
2070 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
2071 btrfs_item_end_nr(l, mid);
2073 for (i = 0; i < nritems; i++) {
2074 struct btrfs_item *item = btrfs_item_nr(right, i);
2075 u32 ioff = btrfs_item_offset(right, item);
2076 btrfs_set_item_offset(right, item, ioff + rt_data_off);
2079 btrfs_set_header_nritems(l, mid);
2081 btrfs_item_key(right, &disk_key, 0);
2082 wret = insert_ptr(trans, root, path, &disk_key, right->start,
2083 path->slots[1] + 1, 1);
2087 btrfs_mark_buffer_dirty(right);
2088 btrfs_mark_buffer_dirty(l);
2089 BUG_ON(path->slots[0] != slot);
2091 ret = btrfs_update_ref(trans, root, l, right, 0, nritems);
2095 free_extent_buffer(path->nodes[0]);
2096 path->nodes[0] = right;
2097 path->slots[0] -= mid;
2098 path->slots[1] += 1;
2100 free_extent_buffer(right);
2102 BUG_ON(path->slots[0] < 0);
2105 BUG_ON(num_doubles != 0);
2112 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2113 struct btrfs_root *root,
2114 struct btrfs_path *path,
2115 u32 new_size, int from_end)
2120 struct extent_buffer *leaf;
2121 struct btrfs_item *item;
2123 unsigned int data_end;
2124 unsigned int old_data_start;
2125 unsigned int old_size;
2126 unsigned int size_diff;
2129 slot_orig = path->slots[0];
2130 leaf = path->nodes[0];
2131 slot = path->slots[0];
2133 old_size = btrfs_item_size_nr(leaf, slot);
2134 if (old_size == new_size)
2137 nritems = btrfs_header_nritems(leaf);
2138 data_end = leaf_data_end(root, leaf);
2140 old_data_start = btrfs_item_offset_nr(leaf, slot);
2142 size_diff = old_size - new_size;
2145 BUG_ON(slot >= nritems);
2148 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2150 /* first correct the data pointers */
2151 for (i = slot; i < nritems; i++) {
2153 item = btrfs_item_nr(leaf, i);
2154 ioff = btrfs_item_offset(leaf, item);
2155 btrfs_set_item_offset(leaf, item, ioff + size_diff);
2158 /* shift the data */
2160 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2161 data_end + size_diff, btrfs_leaf_data(leaf) +
2162 data_end, old_data_start + new_size - data_end);
2164 struct btrfs_disk_key disk_key;
2167 btrfs_item_key(leaf, &disk_key, slot);
2169 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
2171 struct btrfs_file_extent_item *fi;
2173 fi = btrfs_item_ptr(leaf, slot,
2174 struct btrfs_file_extent_item);
2175 fi = (struct btrfs_file_extent_item *)(
2176 (unsigned long)fi - size_diff);
2178 if (btrfs_file_extent_type(leaf, fi) ==
2179 BTRFS_FILE_EXTENT_INLINE) {
2180 ptr = btrfs_item_ptr_offset(leaf, slot);
2181 memmove_extent_buffer(leaf, ptr,
2183 offsetof(struct btrfs_file_extent_item,
2188 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2189 data_end + size_diff, btrfs_leaf_data(leaf) +
2190 data_end, old_data_start - data_end);
2192 offset = btrfs_disk_key_offset(&disk_key);
2193 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
2194 btrfs_set_item_key(leaf, &disk_key, slot);
2196 fixup_low_keys(trans, root, path, &disk_key, 1);
2199 item = btrfs_item_nr(leaf, slot);
2200 btrfs_set_item_size(leaf, item, new_size);
2201 btrfs_mark_buffer_dirty(leaf);
2204 if (btrfs_leaf_free_space(root, leaf) < 0) {
2205 btrfs_print_leaf(root, leaf);
2211 int btrfs_extend_item(struct btrfs_trans_handle *trans,
2212 struct btrfs_root *root, struct btrfs_path *path,
2218 struct extent_buffer *leaf;
2219 struct btrfs_item *item;
2221 unsigned int data_end;
2222 unsigned int old_data;
2223 unsigned int old_size;
2226 slot_orig = path->slots[0];
2227 leaf = path->nodes[0];
2229 nritems = btrfs_header_nritems(leaf);
2230 data_end = leaf_data_end(root, leaf);
2232 if (btrfs_leaf_free_space(root, leaf) < data_size) {
2233 btrfs_print_leaf(root, leaf);
2236 slot = path->slots[0];
2237 old_data = btrfs_item_end_nr(leaf, slot);
2240 if (slot >= nritems) {
2241 btrfs_print_leaf(root, leaf);
2242 printk("slot %d too large, nritems %d\n", slot, nritems);
2247 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2249 /* first correct the data pointers */
2250 for (i = slot; i < nritems; i++) {
2252 item = btrfs_item_nr(leaf, i);
2253 ioff = btrfs_item_offset(leaf, item);
2254 btrfs_set_item_offset(leaf, item, ioff - data_size);
2257 /* shift the data */
2258 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2259 data_end - data_size, btrfs_leaf_data(leaf) +
2260 data_end, old_data - data_end);
2262 data_end = old_data;
2263 old_size = btrfs_item_size_nr(leaf, slot);
2264 item = btrfs_item_nr(leaf, slot);
2265 btrfs_set_item_size(leaf, item, old_size + data_size);
2266 btrfs_mark_buffer_dirty(leaf);
2269 if (btrfs_leaf_free_space(root, leaf) < 0) {
2270 btrfs_print_leaf(root, leaf);
2277 * Given a key and some data, insert an item into the tree.
2278 * This does all the path init required, making room in the tree if needed.
2280 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
2281 struct btrfs_root *root,
2282 struct btrfs_path *path,
2283 struct btrfs_key *cpu_key, u32 *data_size,
2286 struct extent_buffer *leaf;
2287 struct btrfs_item *item;
2295 unsigned int data_end;
2296 struct btrfs_disk_key disk_key;
2298 for (i = 0; i < nr; i++) {
2299 total_data += data_size[i];
2302 /* create a root if there isn't one */
2306 total_size = total_data + (nr - 1) * sizeof(struct btrfs_item);
2307 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
2314 slot_orig = path->slots[0];
2315 leaf = path->nodes[0];
2317 nritems = btrfs_header_nritems(leaf);
2318 data_end = leaf_data_end(root, leaf);
2320 if (btrfs_leaf_free_space(root, leaf) <
2321 sizeof(struct btrfs_item) + total_size) {
2322 btrfs_print_leaf(root, leaf);
2323 printk("not enough freespace need %u have %d\n",
2324 total_size, btrfs_leaf_free_space(root, leaf));
2328 slot = path->slots[0];
2331 if (slot != nritems) {
2333 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
2335 if (old_data < data_end) {
2336 btrfs_print_leaf(root, leaf);
2337 printk("slot %d old_data %d data_end %d\n",
2338 slot, old_data, data_end);
2342 * item0..itemN ... dataN.offset..dataN.size .. data0.size
2344 /* first correct the data pointers */
2345 for (i = slot; i < nritems; i++) {
2348 item = btrfs_item_nr(leaf, i);
2349 ioff = btrfs_item_offset(leaf, item);
2350 btrfs_set_item_offset(leaf, item, ioff - total_data);
2353 /* shift the items */
2354 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
2355 btrfs_item_nr_offset(slot),
2356 (nritems - slot) * sizeof(struct btrfs_item));
2358 /* shift the data */
2359 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2360 data_end - total_data, btrfs_leaf_data(leaf) +
2361 data_end, old_data - data_end);
2362 data_end = old_data;
2365 /* setup the item for the new data */
2366 for (i = 0; i < nr; i++) {
2367 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
2368 btrfs_set_item_key(leaf, &disk_key, slot + i);
2369 item = btrfs_item_nr(leaf, slot + i);
2370 btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
2371 data_end -= data_size[i];
2372 btrfs_set_item_size(leaf, item, data_size[i]);
2374 btrfs_set_header_nritems(leaf, nritems + nr);
2375 btrfs_mark_buffer_dirty(leaf);
2379 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
2380 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
2383 if (btrfs_leaf_free_space(root, leaf) < 0) {
2384 btrfs_print_leaf(root, leaf);
2393 * Given a key and some data, insert an item into the tree.
2394 * This does all the path init required, making room in the tree if needed.
2396 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2397 *root, struct btrfs_key *cpu_key, void *data, u32
2401 struct btrfs_path *path;
2402 struct extent_buffer *leaf;
2405 path = btrfs_alloc_path();
2407 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
2409 leaf = path->nodes[0];
2410 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
2411 write_extent_buffer(leaf, data, ptr, data_size);
2412 btrfs_mark_buffer_dirty(leaf);
2414 btrfs_free_path(path);
2419 * delete the pointer from a given node.
2421 * If the delete empties a node, the node is removed from the tree,
2422 * continuing all the way the root if required. The root is converted into
2423 * a leaf if all the nodes are emptied.
2425 static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2426 struct btrfs_path *path, int level, int slot)
2428 struct extent_buffer *parent = path->nodes[level];
2433 nritems = btrfs_header_nritems(parent);
2434 if (slot != nritems -1) {
2435 memmove_extent_buffer(parent,
2436 btrfs_node_key_ptr_offset(slot),
2437 btrfs_node_key_ptr_offset(slot + 1),
2438 sizeof(struct btrfs_key_ptr) *
2439 (nritems - slot - 1));
2442 btrfs_set_header_nritems(parent, nritems);
2443 if (nritems == 0 && parent == root->node) {
2444 BUG_ON(btrfs_header_level(root->node) != 1);
2445 /* just turn the root into a leaf and break */
2446 btrfs_set_header_level(root->node, 0);
2447 } else if (slot == 0) {
2448 struct btrfs_disk_key disk_key;
2450 btrfs_node_key(parent, &disk_key, 0);
2451 wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
2455 btrfs_mark_buffer_dirty(parent);
2460 * delete the item at the leaf level in path. If that empties
2461 * the leaf, remove it from the tree
2463 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2464 struct btrfs_path *path, int slot, int nr)
2466 struct extent_buffer *leaf;
2467 struct btrfs_item *item;
2475 leaf = path->nodes[0];
2476 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
2478 for (i = 0; i < nr; i++)
2479 dsize += btrfs_item_size_nr(leaf, slot + i);
2481 nritems = btrfs_header_nritems(leaf);
2483 if (slot + nr != nritems) {
2485 int data_end = leaf_data_end(root, leaf);
2487 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
2489 btrfs_leaf_data(leaf) + data_end,
2490 last_off - data_end);
2492 for (i = slot + nr; i < nritems; i++) {
2495 item = btrfs_item_nr(leaf, i);
2496 ioff = btrfs_item_offset(leaf, item);
2497 btrfs_set_item_offset(leaf, item, ioff + dsize);
2500 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
2501 btrfs_item_nr_offset(slot + nr),
2502 sizeof(struct btrfs_item) *
2503 (nritems - slot - nr));
2505 btrfs_set_header_nritems(leaf, nritems - nr);
2508 /* delete the leaf if we've emptied it */
2510 if (leaf == root->node) {
2511 btrfs_set_header_level(leaf, 0);
2513 u64 root_gen = btrfs_header_generation(path->nodes[1]);
2514 clean_tree_block(trans, root, leaf);
2515 wait_on_tree_block_writeback(root, leaf);
2516 wret = del_ptr(trans, root, path, 1, path->slots[1]);
2519 wret = btrfs_free_extent(trans, root,
2520 leaf->start, leaf->len,
2521 path->nodes[1]->start,
2522 btrfs_header_owner(path->nodes[1]),
2528 int used = leaf_space_used(leaf, 0, nritems);
2530 struct btrfs_disk_key disk_key;
2532 btrfs_item_key(leaf, &disk_key, 0);
2533 wret = fixup_low_keys(trans, root, path,
2539 /* delete the leaf if it is mostly empty */
2540 if (used < BTRFS_LEAF_DATA_SIZE(root) / 4) {
2541 /* push_leaf_left fixes the path.
2542 * make sure the path still points to our leaf
2543 * for possible call to del_ptr below
2545 slot = path->slots[1];
2546 extent_buffer_get(leaf);
2548 wret = push_leaf_left(trans, root, path, 1, 1);
2549 if (wret < 0 && wret != -ENOSPC)
2552 if (path->nodes[0] == leaf &&
2553 btrfs_header_nritems(leaf)) {
2554 wret = push_leaf_right(trans, root, path, 1, 1);
2555 if (wret < 0 && wret != -ENOSPC)
2559 if (btrfs_header_nritems(leaf) == 0) {
2561 u64 bytenr = leaf->start;
2562 u32 blocksize = leaf->len;
2564 root_gen = btrfs_header_generation(
2567 clean_tree_block(trans, root, leaf);
2568 wait_on_tree_block_writeback(root, leaf);
2570 wret = del_ptr(trans, root, path, 1, slot);
2574 free_extent_buffer(leaf);
2575 wret = btrfs_free_extent(trans, root, bytenr,
2576 blocksize, path->nodes[1]->start,
2577 btrfs_header_owner(path->nodes[1]),
2582 btrfs_mark_buffer_dirty(leaf);
2583 free_extent_buffer(leaf);
2586 btrfs_mark_buffer_dirty(leaf);
2593 * walk up the tree as far as required to find the previous leaf.
2594 * returns 0 if it found something or 1 if there are no lesser leaves.
2595 * returns < 0 on io errors.
2597 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2601 struct extent_buffer *c;
2602 struct extent_buffer *next = NULL;
2604 while(level < BTRFS_MAX_LEVEL) {
2605 if (!path->nodes[level])
2608 slot = path->slots[level];
2609 c = path->nodes[level];
2612 if (level == BTRFS_MAX_LEVEL)
2619 free_extent_buffer(next);
2621 next = read_node_slot(root, c, slot);
2624 path->slots[level] = slot;
2627 c = path->nodes[level];
2628 free_extent_buffer(c);
2629 slot = btrfs_header_nritems(next);
2632 path->nodes[level] = next;
2633 path->slots[level] = slot;
2636 next = read_node_slot(root, next, slot);
2642 * walk up the tree as far as required to find the next leaf.
2643 * returns 0 if it found something or 1 if there are no greater leaves.
2644 * returns < 0 on io errors.
2646 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
2650 struct extent_buffer *c;
2651 struct extent_buffer *next = NULL;
2653 while(level < BTRFS_MAX_LEVEL) {
2654 if (!path->nodes[level])
2657 slot = path->slots[level] + 1;
2658 c = path->nodes[level];
2659 if (slot >= btrfs_header_nritems(c)) {
2661 if (level == BTRFS_MAX_LEVEL)
2667 free_extent_buffer(next);
2670 reada_for_search(root, path, level, slot, 0);
2672 next = read_node_slot(root, c, slot);
2675 path->slots[level] = slot;
2678 c = path->nodes[level];
2679 free_extent_buffer(c);
2680 path->nodes[level] = next;
2681 path->slots[level] = 0;
2685 reada_for_search(root, path, level, 0, 0);
2686 next = read_node_slot(root, next, 0);
2691 int btrfs_previous_item(struct btrfs_root *root,
2692 struct btrfs_path *path, u64 min_objectid,
2695 struct btrfs_key found_key;
2696 struct extent_buffer *leaf;
2700 if (path->slots[0] == 0) {
2701 ret = btrfs_prev_leaf(root, path);
2707 leaf = path->nodes[0];
2708 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2709 if (found_key.type == type)