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 struct btrfs_path
*btrfs_alloc_path(void)
43 struct btrfs_path
*path
;
44 path
= kmem_cache_zalloc(btrfs_path_cachep
, GFP_NOFS
);
51 * set all locked nodes in the path to blocking locks. This should
52 * be done before scheduling
54 noinline
void btrfs_set_path_blocking(struct btrfs_path
*p
)
57 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
58 if (p
->nodes
[i
] && p
->locks
[i
])
59 btrfs_set_lock_blocking(p
->nodes
[i
]);
64 * reset all the locked nodes in the patch to spinning locks.
66 * held is used to keep lockdep happy, when lockdep is enabled
67 * we set held to a blocking lock before we go around and
68 * retake all the spinlocks in the path. You can safely use NULL
71 noinline
void btrfs_clear_path_blocking(struct btrfs_path
*p
,
72 struct extent_buffer
*held
)
76 #ifdef CONFIG_DEBUG_LOCK_ALLOC
77 /* lockdep really cares that we take all of these spinlocks
78 * in the right order. If any of the locks in the path are not
79 * currently blocking, it is going to complain. So, make really
80 * really sure by forcing the path to blocking before we clear
84 btrfs_set_lock_blocking(held
);
85 btrfs_set_path_blocking(p
);
88 for (i
= BTRFS_MAX_LEVEL
- 1; i
>= 0; i
--) {
89 if (p
->nodes
[i
] && p
->locks
[i
])
90 btrfs_clear_lock_blocking(p
->nodes
[i
]);
93 #ifdef CONFIG_DEBUG_LOCK_ALLOC
95 btrfs_clear_lock_blocking(held
);
99 /* this also releases the path */
100 void btrfs_free_path(struct btrfs_path
*p
)
102 btrfs_release_path(NULL
, p
);
103 kmem_cache_free(btrfs_path_cachep
, p
);
107 * path release drops references on the extent buffers in the path
108 * and it drops any locks held by this path
110 * It is safe to call this on paths that no locks or extent buffers held.
112 noinline
void btrfs_release_path(struct btrfs_root
*root
, struct btrfs_path
*p
)
116 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
121 btrfs_tree_unlock(p
->nodes
[i
]);
124 free_extent_buffer(p
->nodes
[i
]);
130 * safely gets a reference on the root node of a tree. A lock
131 * is not taken, so a concurrent writer may put a different node
132 * at the root of the tree. See btrfs_lock_root_node for the
135 * The extent buffer returned by this has a reference taken, so
136 * it won't disappear. It may stop being the root of the tree
137 * at any time because there are no locks held.
139 struct extent_buffer
*btrfs_root_node(struct btrfs_root
*root
)
141 struct extent_buffer
*eb
;
142 spin_lock(&root
->node_lock
);
144 extent_buffer_get(eb
);
145 spin_unlock(&root
->node_lock
);
149 /* loop around taking references on and locking the root node of the
150 * tree until you end up with a lock on the root. A locked buffer
151 * is returned, with a reference held.
153 struct extent_buffer
*btrfs_lock_root_node(struct btrfs_root
*root
)
155 struct extent_buffer
*eb
;
158 eb
= btrfs_root_node(root
);
161 spin_lock(&root
->node_lock
);
162 if (eb
== root
->node
) {
163 spin_unlock(&root
->node_lock
);
166 spin_unlock(&root
->node_lock
);
168 btrfs_tree_unlock(eb
);
169 free_extent_buffer(eb
);
174 /* cowonly root (everything not a reference counted cow subvolume), just get
175 * put onto a simple dirty list. transaction.c walks this to make sure they
176 * get properly updated on disk.
178 static void add_root_to_dirty_list(struct btrfs_root
*root
)
180 if (root
->track_dirty
&& list_empty(&root
->dirty_list
)) {
181 list_add(&root
->dirty_list
,
182 &root
->fs_info
->dirty_cowonly_roots
);
187 * used by snapshot creation to make a copy of a root for a tree with
188 * a given objectid. The buffer with the new root node is returned in
189 * cow_ret, and this func returns zero on success or a negative error code.
191 int btrfs_copy_root(struct btrfs_trans_handle
*trans
,
192 struct btrfs_root
*root
,
193 struct extent_buffer
*buf
,
194 struct extent_buffer
**cow_ret
, u64 new_root_objectid
)
196 struct extent_buffer
*cow
;
200 struct btrfs_root
*new_root
;
202 new_root
= kmalloc(sizeof(*new_root
), GFP_NOFS
);
206 memcpy(new_root
, root
, sizeof(*new_root
));
207 new_root
->root_key
.objectid
= new_root_objectid
;
209 WARN_ON(root
->ref_cows
&& trans
->transid
!=
210 root
->fs_info
->running_transaction
->transid
);
211 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
213 level
= btrfs_header_level(buf
);
214 nritems
= btrfs_header_nritems(buf
);
216 cow
= btrfs_alloc_free_block(trans
, new_root
, buf
->len
, 0,
217 new_root_objectid
, trans
->transid
,
218 level
, buf
->start
, 0);
224 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
225 btrfs_set_header_bytenr(cow
, cow
->start
);
226 btrfs_set_header_generation(cow
, trans
->transid
);
227 btrfs_set_header_owner(cow
, new_root_objectid
);
228 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
);
230 write_extent_buffer(cow
, root
->fs_info
->fsid
,
231 (unsigned long)btrfs_header_fsid(cow
),
234 WARN_ON(btrfs_header_generation(buf
) > trans
->transid
);
235 ret
= btrfs_inc_ref(trans
, new_root
, buf
, cow
, NULL
);
241 btrfs_mark_buffer_dirty(cow
);
247 * does the dirty work in cow of a single block. The parent block (if
248 * supplied) is updated to point to the new cow copy. The new buffer is marked
249 * dirty and returned locked. If you modify the block it needs to be marked
252 * search_start -- an allocation hint for the new block
254 * empty_size -- a hint that you plan on doing more cow. This is the size in
255 * bytes the allocator should try to find free next to the block it returns.
256 * This is just a hint and may be ignored by the allocator.
258 * prealloc_dest -- if you have already reserved a destination for the cow,
259 * this uses that block instead of allocating a new one.
260 * btrfs_alloc_reserved_extent is used to finish the allocation.
262 static noinline
int __btrfs_cow_block(struct btrfs_trans_handle
*trans
,
263 struct btrfs_root
*root
,
264 struct extent_buffer
*buf
,
265 struct extent_buffer
*parent
, int parent_slot
,
266 struct extent_buffer
**cow_ret
,
267 u64 search_start
, u64 empty_size
,
271 struct extent_buffer
*cow
;
280 btrfs_assert_tree_locked(buf
);
283 parent_start
= parent
->start
;
287 WARN_ON(root
->ref_cows
&& trans
->transid
!=
288 root
->fs_info
->running_transaction
->transid
);
289 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
291 level
= btrfs_header_level(buf
);
292 nritems
= btrfs_header_nritems(buf
);
295 struct btrfs_key ins
;
297 ins
.objectid
= prealloc_dest
;
298 ins
.offset
= buf
->len
;
299 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
301 ret
= btrfs_alloc_reserved_extent(trans
, root
, parent_start
,
302 root
->root_key
.objectid
,
303 trans
->transid
, level
, &ins
);
305 cow
= btrfs_init_new_buffer(trans
, root
, prealloc_dest
,
308 cow
= btrfs_alloc_free_block(trans
, root
, buf
->len
,
310 root
->root_key
.objectid
,
311 trans
->transid
, level
,
312 search_start
, empty_size
);
317 /* cow is set to blocking by btrfs_init_new_buffer */
319 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
320 btrfs_set_header_bytenr(cow
, cow
->start
);
321 btrfs_set_header_generation(cow
, trans
->transid
);
322 btrfs_set_header_owner(cow
, root
->root_key
.objectid
);
323 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
);
325 write_extent_buffer(cow
, root
->fs_info
->fsid
,
326 (unsigned long)btrfs_header_fsid(cow
),
329 WARN_ON(btrfs_header_generation(buf
) > trans
->transid
);
330 if (btrfs_header_generation(buf
) != trans
->transid
) {
332 ret
= btrfs_inc_ref(trans
, root
, buf
, cow
, &nr_extents
);
336 ret
= btrfs_cache_ref(trans
, root
, buf
, nr_extents
);
338 } else if (btrfs_header_owner(buf
) == BTRFS_TREE_RELOC_OBJECTID
) {
340 * There are only two places that can drop reference to
341 * tree blocks owned by living reloc trees, one is here,
342 * the other place is btrfs_drop_subtree. In both places,
343 * we check reference count while tree block is locked.
344 * Furthermore, if reference count is one, it won't get
345 * increased by someone else.
348 ret
= btrfs_lookup_extent_ref(trans
, root
, buf
->start
,
352 ret
= btrfs_update_ref(trans
, root
, buf
, cow
,
354 clean_tree_block(trans
, root
, buf
);
356 ret
= btrfs_inc_ref(trans
, root
, buf
, cow
, NULL
);
360 ret
= btrfs_update_ref(trans
, root
, buf
, cow
, 0, nritems
);
363 clean_tree_block(trans
, root
, buf
);
366 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
367 ret
= btrfs_reloc_tree_cache_ref(trans
, root
, cow
, buf
->start
);
371 if (buf
== root
->node
) {
372 WARN_ON(parent
&& parent
!= buf
);
374 spin_lock(&root
->node_lock
);
376 extent_buffer_get(cow
);
377 spin_unlock(&root
->node_lock
);
379 if (buf
!= root
->commit_root
) {
380 btrfs_free_extent(trans
, root
, buf
->start
,
381 buf
->len
, buf
->start
,
382 root
->root_key
.objectid
,
383 btrfs_header_generation(buf
),
386 free_extent_buffer(buf
);
387 add_root_to_dirty_list(root
);
389 btrfs_set_node_blockptr(parent
, parent_slot
,
391 WARN_ON(trans
->transid
== 0);
392 btrfs_set_node_ptr_generation(parent
, parent_slot
,
394 btrfs_mark_buffer_dirty(parent
);
395 WARN_ON(btrfs_header_generation(parent
) != trans
->transid
);
396 btrfs_free_extent(trans
, root
, buf
->start
, buf
->len
,
397 parent_start
, btrfs_header_owner(parent
),
398 btrfs_header_generation(parent
), level
, 1);
401 btrfs_tree_unlock(buf
);
402 free_extent_buffer(buf
);
403 btrfs_mark_buffer_dirty(cow
);
409 * cows a single block, see __btrfs_cow_block for the real work.
410 * This version of it has extra checks so that a block isn't cow'd more than
411 * once per transaction, as long as it hasn't been written yet
413 noinline
int btrfs_cow_block(struct btrfs_trans_handle
*trans
,
414 struct btrfs_root
*root
, struct extent_buffer
*buf
,
415 struct extent_buffer
*parent
, int parent_slot
,
416 struct extent_buffer
**cow_ret
, u64 prealloc_dest
)
421 if (trans
->transaction
!= root
->fs_info
->running_transaction
) {
422 printk(KERN_CRIT
"trans %llu running %llu\n",
423 (unsigned long long)trans
->transid
,
425 root
->fs_info
->running_transaction
->transid
);
428 if (trans
->transid
!= root
->fs_info
->generation
) {
429 printk(KERN_CRIT
"trans %llu running %llu\n",
430 (unsigned long long)trans
->transid
,
431 (unsigned long long)root
->fs_info
->generation
);
435 if (btrfs_header_generation(buf
) == trans
->transid
&&
436 btrfs_header_owner(buf
) == root
->root_key
.objectid
&&
437 !btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
)) {
439 WARN_ON(prealloc_dest
);
443 search_start
= buf
->start
& ~((u64
)(1024 * 1024 * 1024) - 1);
446 btrfs_set_lock_blocking(parent
);
447 btrfs_set_lock_blocking(buf
);
449 ret
= __btrfs_cow_block(trans
, root
, buf
, parent
,
450 parent_slot
, cow_ret
, search_start
, 0,
456 * helper function for defrag to decide if two blocks pointed to by a
457 * node are actually close by
459 static int close_blocks(u64 blocknr
, u64 other
, u32 blocksize
)
461 if (blocknr
< other
&& other
- (blocknr
+ blocksize
) < 32768)
463 if (blocknr
> other
&& blocknr
- (other
+ blocksize
) < 32768)
469 * compare two keys in a memcmp fashion
471 static int comp_keys(struct btrfs_disk_key
*disk
, struct btrfs_key
*k2
)
475 btrfs_disk_key_to_cpu(&k1
, disk
);
477 if (k1
.objectid
> k2
->objectid
)
479 if (k1
.objectid
< k2
->objectid
)
481 if (k1
.type
> k2
->type
)
483 if (k1
.type
< k2
->type
)
485 if (k1
.offset
> k2
->offset
)
487 if (k1
.offset
< k2
->offset
)
493 * same as comp_keys only with two btrfs_key's
495 static int comp_cpu_keys(struct btrfs_key
*k1
, struct btrfs_key
*k2
)
497 if (k1
->objectid
> k2
->objectid
)
499 if (k1
->objectid
< k2
->objectid
)
501 if (k1
->type
> k2
->type
)
503 if (k1
->type
< k2
->type
)
505 if (k1
->offset
> k2
->offset
)
507 if (k1
->offset
< k2
->offset
)
513 * this is used by the defrag code to go through all the
514 * leaves pointed to by a node and reallocate them so that
515 * disk order is close to key order
517 int btrfs_realloc_node(struct btrfs_trans_handle
*trans
,
518 struct btrfs_root
*root
, struct extent_buffer
*parent
,
519 int start_slot
, int cache_only
, u64
*last_ret
,
520 struct btrfs_key
*progress
)
522 struct extent_buffer
*cur
;
525 u64 search_start
= *last_ret
;
535 int progress_passed
= 0;
536 struct btrfs_disk_key disk_key
;
538 parent_level
= btrfs_header_level(parent
);
539 if (cache_only
&& parent_level
!= 1)
542 if (trans
->transaction
!= root
->fs_info
->running_transaction
)
544 if (trans
->transid
!= root
->fs_info
->generation
)
547 parent_nritems
= btrfs_header_nritems(parent
);
548 blocksize
= btrfs_level_size(root
, parent_level
- 1);
549 end_slot
= parent_nritems
;
551 if (parent_nritems
== 1)
554 btrfs_set_lock_blocking(parent
);
556 for (i
= start_slot
; i
< end_slot
; i
++) {
559 if (!parent
->map_token
) {
560 map_extent_buffer(parent
,
561 btrfs_node_key_ptr_offset(i
),
562 sizeof(struct btrfs_key_ptr
),
563 &parent
->map_token
, &parent
->kaddr
,
564 &parent
->map_start
, &parent
->map_len
,
567 btrfs_node_key(parent
, &disk_key
, i
);
568 if (!progress_passed
&& comp_keys(&disk_key
, progress
) < 0)
572 blocknr
= btrfs_node_blockptr(parent
, i
);
573 gen
= btrfs_node_ptr_generation(parent
, i
);
575 last_block
= blocknr
;
578 other
= btrfs_node_blockptr(parent
, i
- 1);
579 close
= close_blocks(blocknr
, other
, blocksize
);
581 if (!close
&& i
< end_slot
- 2) {
582 other
= btrfs_node_blockptr(parent
, i
+ 1);
583 close
= close_blocks(blocknr
, other
, blocksize
);
586 last_block
= blocknr
;
589 if (parent
->map_token
) {
590 unmap_extent_buffer(parent
, parent
->map_token
,
592 parent
->map_token
= NULL
;
595 cur
= btrfs_find_tree_block(root
, blocknr
, blocksize
);
597 uptodate
= btrfs_buffer_uptodate(cur
, gen
);
600 if (!cur
|| !uptodate
) {
602 free_extent_buffer(cur
);
606 cur
= read_tree_block(root
, blocknr
,
608 } else if (!uptodate
) {
609 btrfs_read_buffer(cur
, gen
);
612 if (search_start
== 0)
613 search_start
= last_block
;
615 btrfs_tree_lock(cur
);
616 btrfs_set_lock_blocking(cur
);
617 err
= __btrfs_cow_block(trans
, root
, cur
, parent
, i
,
620 (end_slot
- i
) * blocksize
), 0);
622 btrfs_tree_unlock(cur
);
623 free_extent_buffer(cur
);
626 search_start
= cur
->start
;
627 last_block
= cur
->start
;
628 *last_ret
= search_start
;
629 btrfs_tree_unlock(cur
);
630 free_extent_buffer(cur
);
632 if (parent
->map_token
) {
633 unmap_extent_buffer(parent
, parent
->map_token
,
635 parent
->map_token
= NULL
;
641 * The leaf data grows from end-to-front in the node.
642 * this returns the address of the start of the last item,
643 * which is the stop of the leaf data stack
645 static inline unsigned int leaf_data_end(struct btrfs_root
*root
,
646 struct extent_buffer
*leaf
)
648 u32 nr
= btrfs_header_nritems(leaf
);
650 return BTRFS_LEAF_DATA_SIZE(root
);
651 return btrfs_item_offset_nr(leaf
, nr
- 1);
655 * extra debugging checks to make sure all the items in a key are
656 * well formed and in the proper order
658 static int check_node(struct btrfs_root
*root
, struct btrfs_path
*path
,
661 struct extent_buffer
*parent
= NULL
;
662 struct extent_buffer
*node
= path
->nodes
[level
];
663 struct btrfs_disk_key parent_key
;
664 struct btrfs_disk_key node_key
;
667 struct btrfs_key cpukey
;
668 u32 nritems
= btrfs_header_nritems(node
);
670 if (path
->nodes
[level
+ 1])
671 parent
= path
->nodes
[level
+ 1];
673 slot
= path
->slots
[level
];
674 BUG_ON(nritems
== 0);
676 parent_slot
= path
->slots
[level
+ 1];
677 btrfs_node_key(parent
, &parent_key
, parent_slot
);
678 btrfs_node_key(node
, &node_key
, 0);
679 BUG_ON(memcmp(&parent_key
, &node_key
,
680 sizeof(struct btrfs_disk_key
)));
681 BUG_ON(btrfs_node_blockptr(parent
, parent_slot
) !=
682 btrfs_header_bytenr(node
));
684 BUG_ON(nritems
> BTRFS_NODEPTRS_PER_BLOCK(root
));
686 btrfs_node_key_to_cpu(node
, &cpukey
, slot
- 1);
687 btrfs_node_key(node
, &node_key
, slot
);
688 BUG_ON(comp_keys(&node_key
, &cpukey
) <= 0);
690 if (slot
< nritems
- 1) {
691 btrfs_node_key_to_cpu(node
, &cpukey
, slot
+ 1);
692 btrfs_node_key(node
, &node_key
, slot
);
693 BUG_ON(comp_keys(&node_key
, &cpukey
) >= 0);
699 * extra checking to make sure all the items in a leaf are
700 * well formed and in the proper order
702 static int check_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
,
705 struct extent_buffer
*leaf
= path
->nodes
[level
];
706 struct extent_buffer
*parent
= NULL
;
708 struct btrfs_key cpukey
;
709 struct btrfs_disk_key parent_key
;
710 struct btrfs_disk_key leaf_key
;
711 int slot
= path
->slots
[0];
713 u32 nritems
= btrfs_header_nritems(leaf
);
715 if (path
->nodes
[level
+ 1])
716 parent
= path
->nodes
[level
+ 1];
722 parent_slot
= path
->slots
[level
+ 1];
723 btrfs_node_key(parent
, &parent_key
, parent_slot
);
724 btrfs_item_key(leaf
, &leaf_key
, 0);
726 BUG_ON(memcmp(&parent_key
, &leaf_key
,
727 sizeof(struct btrfs_disk_key
)));
728 BUG_ON(btrfs_node_blockptr(parent
, parent_slot
) !=
729 btrfs_header_bytenr(leaf
));
731 if (slot
!= 0 && slot
< nritems
- 1) {
732 btrfs_item_key(leaf
, &leaf_key
, slot
);
733 btrfs_item_key_to_cpu(leaf
, &cpukey
, slot
- 1);
734 if (comp_keys(&leaf_key
, &cpukey
) <= 0) {
735 btrfs_print_leaf(root
, leaf
);
736 printk(KERN_CRIT
"slot %d offset bad key\n", slot
);
739 if (btrfs_item_offset_nr(leaf
, slot
- 1) !=
740 btrfs_item_end_nr(leaf
, slot
)) {
741 btrfs_print_leaf(root
, leaf
);
742 printk(KERN_CRIT
"slot %d offset bad\n", slot
);
746 if (slot
< nritems
- 1) {
747 btrfs_item_key(leaf
, &leaf_key
, slot
);
748 btrfs_item_key_to_cpu(leaf
, &cpukey
, slot
+ 1);
749 BUG_ON(comp_keys(&leaf_key
, &cpukey
) >= 0);
750 if (btrfs_item_offset_nr(leaf
, slot
) !=
751 btrfs_item_end_nr(leaf
, slot
+ 1)) {
752 btrfs_print_leaf(root
, leaf
);
753 printk(KERN_CRIT
"slot %d offset bad\n", slot
);
757 BUG_ON(btrfs_item_offset_nr(leaf
, 0) +
758 btrfs_item_size_nr(leaf
, 0) != BTRFS_LEAF_DATA_SIZE(root
));
762 static noinline
int check_block(struct btrfs_root
*root
,
763 struct btrfs_path
*path
, int level
)
767 return check_leaf(root
, path
, level
);
768 return check_node(root
, path
, level
);
772 * search for key in the extent_buffer. The items start at offset p,
773 * and they are item_size apart. There are 'max' items in p.
775 * the slot in the array is returned via slot, and it points to
776 * the place where you would insert key if it is not found in
779 * slot may point to max if the key is bigger than all of the keys
781 static noinline
int generic_bin_search(struct extent_buffer
*eb
,
783 int item_size
, struct btrfs_key
*key
,
790 struct btrfs_disk_key
*tmp
= NULL
;
791 struct btrfs_disk_key unaligned
;
792 unsigned long offset
;
793 char *map_token
= NULL
;
795 unsigned long map_start
= 0;
796 unsigned long map_len
= 0;
800 mid
= (low
+ high
) / 2;
801 offset
= p
+ mid
* item_size
;
803 if (!map_token
|| offset
< map_start
||
804 (offset
+ sizeof(struct btrfs_disk_key
)) >
805 map_start
+ map_len
) {
807 unmap_extent_buffer(eb
, map_token
, KM_USER0
);
811 err
= map_private_extent_buffer(eb
, offset
,
812 sizeof(struct btrfs_disk_key
),
814 &map_start
, &map_len
, KM_USER0
);
817 tmp
= (struct btrfs_disk_key
*)(kaddr
+ offset
-
820 read_extent_buffer(eb
, &unaligned
,
821 offset
, sizeof(unaligned
));
826 tmp
= (struct btrfs_disk_key
*)(kaddr
+ offset
-
829 ret
= comp_keys(tmp
, key
);
838 unmap_extent_buffer(eb
, map_token
, KM_USER0
);
844 unmap_extent_buffer(eb
, map_token
, KM_USER0
);
849 * simple bin_search frontend that does the right thing for
852 static int bin_search(struct extent_buffer
*eb
, struct btrfs_key
*key
,
853 int level
, int *slot
)
856 return generic_bin_search(eb
,
857 offsetof(struct btrfs_leaf
, items
),
858 sizeof(struct btrfs_item
),
859 key
, btrfs_header_nritems(eb
),
862 return generic_bin_search(eb
,
863 offsetof(struct btrfs_node
, ptrs
),
864 sizeof(struct btrfs_key_ptr
),
865 key
, btrfs_header_nritems(eb
),
871 /* given a node and slot number, this reads the blocks it points to. The
872 * extent buffer is returned with a reference taken (but unlocked).
873 * NULL is returned on error.
875 static noinline
struct extent_buffer
*read_node_slot(struct btrfs_root
*root
,
876 struct extent_buffer
*parent
, int slot
)
878 int level
= btrfs_header_level(parent
);
881 if (slot
>= btrfs_header_nritems(parent
))
886 return read_tree_block(root
, btrfs_node_blockptr(parent
, slot
),
887 btrfs_level_size(root
, level
- 1),
888 btrfs_node_ptr_generation(parent
, slot
));
892 * node level balancing, used to make sure nodes are in proper order for
893 * item deletion. We balance from the top down, so we have to make sure
894 * that a deletion won't leave an node completely empty later on.
896 static noinline
int balance_level(struct btrfs_trans_handle
*trans
,
897 struct btrfs_root
*root
,
898 struct btrfs_path
*path
, int level
)
900 struct extent_buffer
*right
= NULL
;
901 struct extent_buffer
*mid
;
902 struct extent_buffer
*left
= NULL
;
903 struct extent_buffer
*parent
= NULL
;
907 int orig_slot
= path
->slots
[level
];
908 int err_on_enospc
= 0;
914 mid
= path
->nodes
[level
];
916 WARN_ON(!path
->locks
[level
]);
917 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
919 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
921 if (level
< BTRFS_MAX_LEVEL
- 1)
922 parent
= path
->nodes
[level
+ 1];
923 pslot
= path
->slots
[level
+ 1];
926 * deal with the case where there is only one pointer in the root
927 * by promoting the node below to a root
930 struct extent_buffer
*child
;
932 if (btrfs_header_nritems(mid
) != 1)
935 /* promote the child to a root */
936 child
= read_node_slot(root
, mid
, 0);
938 btrfs_tree_lock(child
);
939 btrfs_set_lock_blocking(child
);
940 ret
= btrfs_cow_block(trans
, root
, child
, mid
, 0, &child
, 0);
943 spin_lock(&root
->node_lock
);
945 spin_unlock(&root
->node_lock
);
947 ret
= btrfs_update_extent_ref(trans
, root
, child
->start
,
948 mid
->start
, child
->start
,
949 root
->root_key
.objectid
,
950 trans
->transid
, level
- 1);
953 add_root_to_dirty_list(root
);
954 btrfs_tree_unlock(child
);
956 path
->locks
[level
] = 0;
957 path
->nodes
[level
] = NULL
;
958 clean_tree_block(trans
, root
, mid
);
959 btrfs_tree_unlock(mid
);
960 /* once for the path */
961 free_extent_buffer(mid
);
962 ret
= btrfs_free_extent(trans
, root
, mid
->start
, mid
->len
,
963 mid
->start
, root
->root_key
.objectid
,
964 btrfs_header_generation(mid
),
966 /* once for the root ptr */
967 free_extent_buffer(mid
);
970 if (btrfs_header_nritems(mid
) >
971 BTRFS_NODEPTRS_PER_BLOCK(root
) / 4)
974 if (btrfs_header_nritems(mid
) < 2)
977 left
= read_node_slot(root
, parent
, pslot
- 1);
979 btrfs_tree_lock(left
);
980 btrfs_set_lock_blocking(left
);
981 wret
= btrfs_cow_block(trans
, root
, left
,
982 parent
, pslot
- 1, &left
, 0);
988 right
= read_node_slot(root
, parent
, pslot
+ 1);
990 btrfs_tree_lock(right
);
991 btrfs_set_lock_blocking(right
);
992 wret
= btrfs_cow_block(trans
, root
, right
,
993 parent
, pslot
+ 1, &right
, 0);
1000 /* first, try to make some room in the middle buffer */
1002 orig_slot
+= btrfs_header_nritems(left
);
1003 wret
= push_node_left(trans
, root
, left
, mid
, 1);
1006 if (btrfs_header_nritems(mid
) < 2)
1011 * then try to empty the right most buffer into the middle
1014 wret
= push_node_left(trans
, root
, mid
, right
, 1);
1015 if (wret
< 0 && wret
!= -ENOSPC
)
1017 if (btrfs_header_nritems(right
) == 0) {
1018 u64 bytenr
= right
->start
;
1019 u64 generation
= btrfs_header_generation(parent
);
1020 u32 blocksize
= right
->len
;
1022 clean_tree_block(trans
, root
, right
);
1023 btrfs_tree_unlock(right
);
1024 free_extent_buffer(right
);
1026 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
+
1030 wret
= btrfs_free_extent(trans
, root
, bytenr
,
1031 blocksize
, parent
->start
,
1032 btrfs_header_owner(parent
),
1033 generation
, level
, 1);
1037 struct btrfs_disk_key right_key
;
1038 btrfs_node_key(right
, &right_key
, 0);
1039 btrfs_set_node_key(parent
, &right_key
, pslot
+ 1);
1040 btrfs_mark_buffer_dirty(parent
);
1043 if (btrfs_header_nritems(mid
) == 1) {
1045 * we're not allowed to leave a node with one item in the
1046 * tree during a delete. A deletion from lower in the tree
1047 * could try to delete the only pointer in this node.
1048 * So, pull some keys from the left.
1049 * There has to be a left pointer at this point because
1050 * otherwise we would have pulled some pointers from the
1054 wret
= balance_node_right(trans
, root
, mid
, left
);
1060 wret
= push_node_left(trans
, root
, left
, mid
, 1);
1066 if (btrfs_header_nritems(mid
) == 0) {
1067 /* we've managed to empty the middle node, drop it */
1068 u64 root_gen
= btrfs_header_generation(parent
);
1069 u64 bytenr
= mid
->start
;
1070 u32 blocksize
= mid
->len
;
1072 clean_tree_block(trans
, root
, mid
);
1073 btrfs_tree_unlock(mid
);
1074 free_extent_buffer(mid
);
1076 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
);
1079 wret
= btrfs_free_extent(trans
, root
, bytenr
, blocksize
,
1081 btrfs_header_owner(parent
),
1082 root_gen
, level
, 1);
1086 /* update the parent key to reflect our changes */
1087 struct btrfs_disk_key mid_key
;
1088 btrfs_node_key(mid
, &mid_key
, 0);
1089 btrfs_set_node_key(parent
, &mid_key
, pslot
);
1090 btrfs_mark_buffer_dirty(parent
);
1093 /* update the path */
1095 if (btrfs_header_nritems(left
) > orig_slot
) {
1096 extent_buffer_get(left
);
1097 /* left was locked after cow */
1098 path
->nodes
[level
] = left
;
1099 path
->slots
[level
+ 1] -= 1;
1100 path
->slots
[level
] = orig_slot
;
1102 btrfs_tree_unlock(mid
);
1103 free_extent_buffer(mid
);
1106 orig_slot
-= btrfs_header_nritems(left
);
1107 path
->slots
[level
] = orig_slot
;
1110 /* double check we haven't messed things up */
1111 check_block(root
, path
, level
);
1113 btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]))
1117 btrfs_tree_unlock(right
);
1118 free_extent_buffer(right
);
1121 if (path
->nodes
[level
] != left
)
1122 btrfs_tree_unlock(left
);
1123 free_extent_buffer(left
);
1128 /* Node balancing for insertion. Here we only split or push nodes around
1129 * when they are completely full. This is also done top down, so we
1130 * have to be pessimistic.
1132 static noinline
int push_nodes_for_insert(struct btrfs_trans_handle
*trans
,
1133 struct btrfs_root
*root
,
1134 struct btrfs_path
*path
, int level
)
1136 struct extent_buffer
*right
= NULL
;
1137 struct extent_buffer
*mid
;
1138 struct extent_buffer
*left
= NULL
;
1139 struct extent_buffer
*parent
= NULL
;
1143 int orig_slot
= path
->slots
[level
];
1149 mid
= path
->nodes
[level
];
1150 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
1151 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
1153 if (level
< BTRFS_MAX_LEVEL
- 1)
1154 parent
= path
->nodes
[level
+ 1];
1155 pslot
= path
->slots
[level
+ 1];
1160 left
= read_node_slot(root
, parent
, pslot
- 1);
1162 /* first, try to make some room in the middle buffer */
1166 btrfs_tree_lock(left
);
1167 btrfs_set_lock_blocking(left
);
1169 left_nr
= btrfs_header_nritems(left
);
1170 if (left_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
1173 ret
= btrfs_cow_block(trans
, root
, left
, parent
,
1174 pslot
- 1, &left
, 0);
1178 wret
= push_node_left(trans
, root
,
1185 struct btrfs_disk_key disk_key
;
1186 orig_slot
+= left_nr
;
1187 btrfs_node_key(mid
, &disk_key
, 0);
1188 btrfs_set_node_key(parent
, &disk_key
, pslot
);
1189 btrfs_mark_buffer_dirty(parent
);
1190 if (btrfs_header_nritems(left
) > orig_slot
) {
1191 path
->nodes
[level
] = left
;
1192 path
->slots
[level
+ 1] -= 1;
1193 path
->slots
[level
] = orig_slot
;
1194 btrfs_tree_unlock(mid
);
1195 free_extent_buffer(mid
);
1198 btrfs_header_nritems(left
);
1199 path
->slots
[level
] = orig_slot
;
1200 btrfs_tree_unlock(left
);
1201 free_extent_buffer(left
);
1205 btrfs_tree_unlock(left
);
1206 free_extent_buffer(left
);
1208 right
= read_node_slot(root
, parent
, pslot
+ 1);
1211 * then try to empty the right most buffer into the middle
1216 btrfs_tree_lock(right
);
1217 btrfs_set_lock_blocking(right
);
1219 right_nr
= btrfs_header_nritems(right
);
1220 if (right_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
1223 ret
= btrfs_cow_block(trans
, root
, right
,
1229 wret
= balance_node_right(trans
, root
,
1236 struct btrfs_disk_key disk_key
;
1238 btrfs_node_key(right
, &disk_key
, 0);
1239 btrfs_set_node_key(parent
, &disk_key
, pslot
+ 1);
1240 btrfs_mark_buffer_dirty(parent
);
1242 if (btrfs_header_nritems(mid
) <= orig_slot
) {
1243 path
->nodes
[level
] = right
;
1244 path
->slots
[level
+ 1] += 1;
1245 path
->slots
[level
] = orig_slot
-
1246 btrfs_header_nritems(mid
);
1247 btrfs_tree_unlock(mid
);
1248 free_extent_buffer(mid
);
1250 btrfs_tree_unlock(right
);
1251 free_extent_buffer(right
);
1255 btrfs_tree_unlock(right
);
1256 free_extent_buffer(right
);
1262 * readahead one full node of leaves, finding things that are close
1263 * to the block in 'slot', and triggering ra on them.
1265 static noinline
void reada_for_search(struct btrfs_root
*root
,
1266 struct btrfs_path
*path
,
1267 int level
, int slot
, u64 objectid
)
1269 struct extent_buffer
*node
;
1270 struct btrfs_disk_key disk_key
;
1275 int direction
= path
->reada
;
1276 struct extent_buffer
*eb
;
1284 if (!path
->nodes
[level
])
1287 node
= path
->nodes
[level
];
1289 search
= btrfs_node_blockptr(node
, slot
);
1290 blocksize
= btrfs_level_size(root
, level
- 1);
1291 eb
= btrfs_find_tree_block(root
, search
, blocksize
);
1293 free_extent_buffer(eb
);
1299 nritems
= btrfs_header_nritems(node
);
1302 if (direction
< 0) {
1306 } else if (direction
> 0) {
1311 if (path
->reada
< 0 && objectid
) {
1312 btrfs_node_key(node
, &disk_key
, nr
);
1313 if (btrfs_disk_key_objectid(&disk_key
) != objectid
)
1316 search
= btrfs_node_blockptr(node
, nr
);
1317 if ((search
<= target
&& target
- search
<= 65536) ||
1318 (search
> target
&& search
- target
<= 65536)) {
1319 readahead_tree_block(root
, search
, blocksize
,
1320 btrfs_node_ptr_generation(node
, nr
));
1324 if ((nread
> 65536 || nscan
> 32))
1330 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1333 static noinline
int reada_for_balance(struct btrfs_root
*root
,
1334 struct btrfs_path
*path
, int level
)
1338 struct extent_buffer
*parent
;
1339 struct extent_buffer
*eb
;
1346 parent
= path
->nodes
[level
- 1];
1350 nritems
= btrfs_header_nritems(parent
);
1351 slot
= path
->slots
[level
];
1352 blocksize
= btrfs_level_size(root
, level
);
1355 block1
= btrfs_node_blockptr(parent
, slot
- 1);
1356 gen
= btrfs_node_ptr_generation(parent
, slot
- 1);
1357 eb
= btrfs_find_tree_block(root
, block1
, blocksize
);
1358 if (eb
&& btrfs_buffer_uptodate(eb
, gen
))
1360 free_extent_buffer(eb
);
1362 if (slot
< nritems
) {
1363 block2
= btrfs_node_blockptr(parent
, slot
+ 1);
1364 gen
= btrfs_node_ptr_generation(parent
, slot
+ 1);
1365 eb
= btrfs_find_tree_block(root
, block2
, blocksize
);
1366 if (eb
&& btrfs_buffer_uptodate(eb
, gen
))
1368 free_extent_buffer(eb
);
1370 if (block1
|| block2
) {
1372 btrfs_release_path(root
, path
);
1374 readahead_tree_block(root
, block1
, blocksize
, 0);
1376 readahead_tree_block(root
, block2
, blocksize
, 0);
1379 eb
= read_tree_block(root
, block1
, blocksize
, 0);
1380 free_extent_buffer(eb
);
1383 eb
= read_tree_block(root
, block2
, blocksize
, 0);
1384 free_extent_buffer(eb
);
1392 * when we walk down the tree, it is usually safe to unlock the higher layers
1393 * in the tree. The exceptions are when our path goes through slot 0, because
1394 * operations on the tree might require changing key pointers higher up in the
1397 * callers might also have set path->keep_locks, which tells this code to keep
1398 * the lock if the path points to the last slot in the block. This is part of
1399 * walking through the tree, and selecting the next slot in the higher block.
1401 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1402 * if lowest_unlock is 1, level 0 won't be unlocked
1404 static noinline
void unlock_up(struct btrfs_path
*path
, int level
,
1408 int skip_level
= level
;
1410 struct extent_buffer
*t
;
1412 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1413 if (!path
->nodes
[i
])
1415 if (!path
->locks
[i
])
1417 if (!no_skips
&& path
->slots
[i
] == 0) {
1421 if (!no_skips
&& path
->keep_locks
) {
1424 nritems
= btrfs_header_nritems(t
);
1425 if (nritems
< 1 || path
->slots
[i
] >= nritems
- 1) {
1430 if (skip_level
< i
&& i
>= lowest_unlock
)
1434 if (i
>= lowest_unlock
&& i
> skip_level
&& path
->locks
[i
]) {
1435 btrfs_tree_unlock(t
);
1442 * This releases any locks held in the path starting at level and
1443 * going all the way up to the root.
1445 * btrfs_search_slot will keep the lock held on higher nodes in a few
1446 * corner cases, such as COW of the block at slot zero in the node. This
1447 * ignores those rules, and it should only be called when there are no
1448 * more updates to be done higher up in the tree.
1450 noinline
void btrfs_unlock_up_safe(struct btrfs_path
*path
, int level
)
1454 if (path
->keep_locks
|| path
->lowest_level
)
1457 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1458 if (!path
->nodes
[i
])
1460 if (!path
->locks
[i
])
1462 btrfs_tree_unlock(path
->nodes
[i
]);
1468 * look for key in the tree. path is filled in with nodes along the way
1469 * if key is found, we return zero and you can find the item in the leaf
1470 * level of the path (level 0)
1472 * If the key isn't found, the path points to the slot where it should
1473 * be inserted, and 1 is returned. If there are other errors during the
1474 * search a negative error number is returned.
1476 * if ins_len > 0, nodes and leaves will be split as we walk down the
1477 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1480 int btrfs_search_slot(struct btrfs_trans_handle
*trans
, struct btrfs_root
1481 *root
, struct btrfs_key
*key
, struct btrfs_path
*p
, int
1484 struct extent_buffer
*b
;
1485 struct extent_buffer
*tmp
;
1489 int should_reada
= p
->reada
;
1490 int lowest_unlock
= 1;
1492 u8 lowest_level
= 0;
1495 struct btrfs_key prealloc_block
;
1497 lowest_level
= p
->lowest_level
;
1498 WARN_ON(lowest_level
&& ins_len
> 0);
1499 WARN_ON(p
->nodes
[0] != NULL
);
1504 prealloc_block
.objectid
= 0;
1507 if (p
->skip_locking
)
1508 b
= btrfs_root_node(root
);
1510 b
= btrfs_lock_root_node(root
);
1513 level
= btrfs_header_level(b
);
1516 * setup the path here so we can release it under lock
1517 * contention with the cow code
1519 p
->nodes
[level
] = b
;
1520 if (!p
->skip_locking
)
1521 p
->locks
[level
] = 1;
1526 /* is a cow on this block not required */
1527 if (btrfs_header_generation(b
) == trans
->transid
&&
1528 btrfs_header_owner(b
) == root
->root_key
.objectid
&&
1529 !btrfs_header_flag(b
, BTRFS_HEADER_FLAG_WRITTEN
)) {
1533 /* ok, we have to cow, is our old prealloc the right
1536 if (prealloc_block
.objectid
&&
1537 prealloc_block
.offset
!= b
->len
) {
1538 btrfs_release_path(root
, p
);
1539 btrfs_free_reserved_extent(root
,
1540 prealloc_block
.objectid
,
1541 prealloc_block
.offset
);
1542 prealloc_block
.objectid
= 0;
1547 * for higher level blocks, try not to allocate blocks
1548 * with the block and the parent locks held.
1550 if (level
> 0 && !prealloc_block
.objectid
) {
1552 u64 hint
= b
->start
;
1554 btrfs_release_path(root
, p
);
1555 ret
= btrfs_reserve_extent(trans
, root
,
1558 &prealloc_block
, 0);
1563 btrfs_set_path_blocking(p
);
1565 wret
= btrfs_cow_block(trans
, root
, b
,
1566 p
->nodes
[level
+ 1],
1567 p
->slots
[level
+ 1],
1568 &b
, prealloc_block
.objectid
);
1569 prealloc_block
.objectid
= 0;
1571 free_extent_buffer(b
);
1577 BUG_ON(!cow
&& ins_len
);
1578 if (level
!= btrfs_header_level(b
))
1580 level
= btrfs_header_level(b
);
1582 p
->nodes
[level
] = b
;
1583 if (!p
->skip_locking
)
1584 p
->locks
[level
] = 1;
1586 btrfs_clear_path_blocking(p
, NULL
);
1589 * we have a lock on b and as long as we aren't changing
1590 * the tree, there is no way to for the items in b to change.
1591 * It is safe to drop the lock on our parent before we
1592 * go through the expensive btree search on b.
1594 * If cow is true, then we might be changing slot zero,
1595 * which may require changing the parent. So, we can't
1596 * drop the lock until after we know which slot we're
1600 btrfs_unlock_up_safe(p
, level
+ 1);
1602 ret
= check_block(root
, p
, level
);
1608 ret
= bin_search(b
, key
, level
, &slot
);
1611 if (ret
&& slot
> 0)
1613 p
->slots
[level
] = slot
;
1614 if ((p
->search_for_split
|| ins_len
> 0) &&
1615 btrfs_header_nritems(b
) >=
1616 BTRFS_NODEPTRS_PER_BLOCK(root
) - 3) {
1619 sret
= reada_for_balance(root
, p
, level
);
1623 btrfs_set_path_blocking(p
);
1624 sret
= split_node(trans
, root
, p
, level
);
1625 btrfs_clear_path_blocking(p
, NULL
);
1632 b
= p
->nodes
[level
];
1633 slot
= p
->slots
[level
];
1634 } else if (ins_len
< 0 &&
1635 btrfs_header_nritems(b
) <
1636 BTRFS_NODEPTRS_PER_BLOCK(root
) / 4) {
1639 sret
= reada_for_balance(root
, p
, level
);
1643 btrfs_set_path_blocking(p
);
1644 sret
= balance_level(trans
, root
, p
, level
);
1645 btrfs_clear_path_blocking(p
, NULL
);
1651 b
= p
->nodes
[level
];
1653 btrfs_release_path(NULL
, p
);
1656 slot
= p
->slots
[level
];
1657 BUG_ON(btrfs_header_nritems(b
) == 1);
1659 unlock_up(p
, level
, lowest_unlock
);
1661 /* this is only true while dropping a snapshot */
1662 if (level
== lowest_level
) {
1667 blocknr
= btrfs_node_blockptr(b
, slot
);
1668 gen
= btrfs_node_ptr_generation(b
, slot
);
1669 blocksize
= btrfs_level_size(root
, level
- 1);
1671 tmp
= btrfs_find_tree_block(root
, blocknr
, blocksize
);
1672 if (tmp
&& btrfs_buffer_uptodate(tmp
, gen
)) {
1676 * reduce lock contention at high levels
1677 * of the btree by dropping locks before
1681 btrfs_release_path(NULL
, p
);
1683 free_extent_buffer(tmp
);
1685 reada_for_search(root
, p
,
1689 tmp
= read_tree_block(root
, blocknr
,
1692 free_extent_buffer(tmp
);
1695 btrfs_set_path_blocking(p
);
1697 free_extent_buffer(tmp
);
1699 reada_for_search(root
, p
,
1702 b
= read_node_slot(root
, b
, slot
);
1705 if (!p
->skip_locking
) {
1708 btrfs_clear_path_blocking(p
, NULL
);
1709 lret
= btrfs_try_spin_lock(b
);
1712 btrfs_set_path_blocking(p
);
1714 btrfs_clear_path_blocking(p
, b
);
1718 p
->slots
[level
] = slot
;
1720 btrfs_leaf_free_space(root
, b
) < ins_len
) {
1723 btrfs_set_path_blocking(p
);
1724 sret
= split_leaf(trans
, root
, key
,
1725 p
, ins_len
, ret
== 0);
1726 btrfs_clear_path_blocking(p
, NULL
);
1734 if (!p
->search_for_split
)
1735 unlock_up(p
, level
, lowest_unlock
);
1742 * we don't really know what they plan on doing with the path
1743 * from here on, so for now just mark it as blocking
1745 btrfs_set_path_blocking(p
);
1746 if (prealloc_block
.objectid
) {
1747 btrfs_free_reserved_extent(root
,
1748 prealloc_block
.objectid
,
1749 prealloc_block
.offset
);
1754 int btrfs_merge_path(struct btrfs_trans_handle
*trans
,
1755 struct btrfs_root
*root
,
1756 struct btrfs_key
*node_keys
,
1757 u64
*nodes
, int lowest_level
)
1759 struct extent_buffer
*eb
;
1760 struct extent_buffer
*parent
;
1761 struct btrfs_key key
;
1770 eb
= btrfs_lock_root_node(root
);
1771 ret
= btrfs_cow_block(trans
, root
, eb
, NULL
, 0, &eb
, 0);
1774 btrfs_set_lock_blocking(eb
);
1778 level
= btrfs_header_level(parent
);
1779 if (level
== 0 || level
<= lowest_level
)
1782 ret
= bin_search(parent
, &node_keys
[lowest_level
], level
,
1784 if (ret
&& slot
> 0)
1787 bytenr
= btrfs_node_blockptr(parent
, slot
);
1788 if (nodes
[level
- 1] == bytenr
)
1791 blocksize
= btrfs_level_size(root
, level
- 1);
1792 generation
= btrfs_node_ptr_generation(parent
, slot
);
1793 btrfs_node_key_to_cpu(eb
, &key
, slot
);
1794 key_match
= !memcmp(&key
, &node_keys
[level
- 1], sizeof(key
));
1796 if (generation
== trans
->transid
) {
1797 eb
= read_tree_block(root
, bytenr
, blocksize
,
1799 btrfs_tree_lock(eb
);
1800 btrfs_set_lock_blocking(eb
);
1804 * if node keys match and node pointer hasn't been modified
1805 * in the running transaction, we can merge the path. for
1806 * blocks owened by reloc trees, the node pointer check is
1807 * skipped, this is because these blocks are fully controlled
1808 * by the space balance code, no one else can modify them.
1810 if (!nodes
[level
- 1] || !key_match
||
1811 (generation
== trans
->transid
&&
1812 btrfs_header_owner(eb
) != BTRFS_TREE_RELOC_OBJECTID
)) {
1813 if (level
== 1 || level
== lowest_level
+ 1) {
1814 if (generation
== trans
->transid
) {
1815 btrfs_tree_unlock(eb
);
1816 free_extent_buffer(eb
);
1821 if (generation
!= trans
->transid
) {
1822 eb
= read_tree_block(root
, bytenr
, blocksize
,
1824 btrfs_tree_lock(eb
);
1825 btrfs_set_lock_blocking(eb
);
1828 ret
= btrfs_cow_block(trans
, root
, eb
, parent
, slot
,
1832 if (root
->root_key
.objectid
==
1833 BTRFS_TREE_RELOC_OBJECTID
) {
1834 if (!nodes
[level
- 1]) {
1835 nodes
[level
- 1] = eb
->start
;
1836 memcpy(&node_keys
[level
- 1], &key
,
1837 sizeof(node_keys
[0]));
1843 btrfs_tree_unlock(parent
);
1844 free_extent_buffer(parent
);
1849 btrfs_set_node_blockptr(parent
, slot
, nodes
[level
- 1]);
1850 btrfs_set_node_ptr_generation(parent
, slot
, trans
->transid
);
1851 btrfs_mark_buffer_dirty(parent
);
1853 ret
= btrfs_inc_extent_ref(trans
, root
,
1855 blocksize
, parent
->start
,
1856 btrfs_header_owner(parent
),
1857 btrfs_header_generation(parent
),
1862 * If the block was created in the running transaction,
1863 * it's possible this is the last reference to it, so we
1864 * should drop the subtree.
1866 if (generation
== trans
->transid
) {
1867 ret
= btrfs_drop_subtree(trans
, root
, eb
, parent
);
1869 btrfs_tree_unlock(eb
);
1870 free_extent_buffer(eb
);
1872 ret
= btrfs_free_extent(trans
, root
, bytenr
,
1873 blocksize
, parent
->start
,
1874 btrfs_header_owner(parent
),
1875 btrfs_header_generation(parent
),
1881 btrfs_tree_unlock(parent
);
1882 free_extent_buffer(parent
);
1887 * adjust the pointers going up the tree, starting at level
1888 * making sure the right key of each node is points to 'key'.
1889 * This is used after shifting pointers to the left, so it stops
1890 * fixing up pointers when a given leaf/node is not in slot 0 of the
1893 * If this fails to write a tree block, it returns -1, but continues
1894 * fixing up the blocks in ram so the tree is consistent.
1896 static int fixup_low_keys(struct btrfs_trans_handle
*trans
,
1897 struct btrfs_root
*root
, struct btrfs_path
*path
,
1898 struct btrfs_disk_key
*key
, int level
)
1902 struct extent_buffer
*t
;
1904 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1905 int tslot
= path
->slots
[i
];
1906 if (!path
->nodes
[i
])
1909 btrfs_set_node_key(t
, key
, tslot
);
1910 btrfs_mark_buffer_dirty(path
->nodes
[i
]);
1920 * This function isn't completely safe. It's the caller's responsibility
1921 * that the new key won't break the order
1923 int btrfs_set_item_key_safe(struct btrfs_trans_handle
*trans
,
1924 struct btrfs_root
*root
, struct btrfs_path
*path
,
1925 struct btrfs_key
*new_key
)
1927 struct btrfs_disk_key disk_key
;
1928 struct extent_buffer
*eb
;
1931 eb
= path
->nodes
[0];
1932 slot
= path
->slots
[0];
1934 btrfs_item_key(eb
, &disk_key
, slot
- 1);
1935 if (comp_keys(&disk_key
, new_key
) >= 0)
1938 if (slot
< btrfs_header_nritems(eb
) - 1) {
1939 btrfs_item_key(eb
, &disk_key
, slot
+ 1);
1940 if (comp_keys(&disk_key
, new_key
) <= 0)
1944 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
1945 btrfs_set_item_key(eb
, &disk_key
, slot
);
1946 btrfs_mark_buffer_dirty(eb
);
1948 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
1953 * try to push data from one node into the next node left in the
1956 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1957 * error, and > 0 if there was no room in the left hand block.
1959 static int push_node_left(struct btrfs_trans_handle
*trans
,
1960 struct btrfs_root
*root
, struct extent_buffer
*dst
,
1961 struct extent_buffer
*src
, int empty
)
1968 src_nritems
= btrfs_header_nritems(src
);
1969 dst_nritems
= btrfs_header_nritems(dst
);
1970 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1971 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1972 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1974 if (!empty
&& src_nritems
<= 8)
1977 if (push_items
<= 0)
1981 push_items
= min(src_nritems
, push_items
);
1982 if (push_items
< src_nritems
) {
1983 /* leave at least 8 pointers in the node if
1984 * we aren't going to empty it
1986 if (src_nritems
- push_items
< 8) {
1987 if (push_items
<= 8)
1993 push_items
= min(src_nritems
- 8, push_items
);
1995 copy_extent_buffer(dst
, src
,
1996 btrfs_node_key_ptr_offset(dst_nritems
),
1997 btrfs_node_key_ptr_offset(0),
1998 push_items
* sizeof(struct btrfs_key_ptr
));
2000 if (push_items
< src_nritems
) {
2001 memmove_extent_buffer(src
, btrfs_node_key_ptr_offset(0),
2002 btrfs_node_key_ptr_offset(push_items
),
2003 (src_nritems
- push_items
) *
2004 sizeof(struct btrfs_key_ptr
));
2006 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
2007 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
2008 btrfs_mark_buffer_dirty(src
);
2009 btrfs_mark_buffer_dirty(dst
);
2011 ret
= btrfs_update_ref(trans
, root
, src
, dst
, dst_nritems
, push_items
);
2018 * try to push data from one node into the next node right in the
2021 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2022 * error, and > 0 if there was no room in the right hand block.
2024 * this will only push up to 1/2 the contents of the left node over
2026 static int balance_node_right(struct btrfs_trans_handle
*trans
,
2027 struct btrfs_root
*root
,
2028 struct extent_buffer
*dst
,
2029 struct extent_buffer
*src
)
2037 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
2038 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
2040 src_nritems
= btrfs_header_nritems(src
);
2041 dst_nritems
= btrfs_header_nritems(dst
);
2042 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
2043 if (push_items
<= 0)
2046 if (src_nritems
< 4)
2049 max_push
= src_nritems
/ 2 + 1;
2050 /* don't try to empty the node */
2051 if (max_push
>= src_nritems
)
2054 if (max_push
< push_items
)
2055 push_items
= max_push
;
2057 memmove_extent_buffer(dst
, btrfs_node_key_ptr_offset(push_items
),
2058 btrfs_node_key_ptr_offset(0),
2060 sizeof(struct btrfs_key_ptr
));
2062 copy_extent_buffer(dst
, src
,
2063 btrfs_node_key_ptr_offset(0),
2064 btrfs_node_key_ptr_offset(src_nritems
- push_items
),
2065 push_items
* sizeof(struct btrfs_key_ptr
));
2067 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
2068 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
2070 btrfs_mark_buffer_dirty(src
);
2071 btrfs_mark_buffer_dirty(dst
);
2073 ret
= btrfs_update_ref(trans
, root
, src
, dst
, 0, push_items
);
2080 * helper function to insert a new root level in the tree.
2081 * A new node is allocated, and a single item is inserted to
2082 * point to the existing root
2084 * returns zero on success or < 0 on failure.
2086 static noinline
int insert_new_root(struct btrfs_trans_handle
*trans
,
2087 struct btrfs_root
*root
,
2088 struct btrfs_path
*path
, int level
)
2091 struct extent_buffer
*lower
;
2092 struct extent_buffer
*c
;
2093 struct extent_buffer
*old
;
2094 struct btrfs_disk_key lower_key
;
2097 BUG_ON(path
->nodes
[level
]);
2098 BUG_ON(path
->nodes
[level
-1] != root
->node
);
2100 lower
= path
->nodes
[level
-1];
2102 btrfs_item_key(lower
, &lower_key
, 0);
2104 btrfs_node_key(lower
, &lower_key
, 0);
2106 c
= btrfs_alloc_free_block(trans
, root
, root
->nodesize
, 0,
2107 root
->root_key
.objectid
, trans
->transid
,
2108 level
, root
->node
->start
, 0);
2112 memset_extent_buffer(c
, 0, 0, root
->nodesize
);
2113 btrfs_set_header_nritems(c
, 1);
2114 btrfs_set_header_level(c
, level
);
2115 btrfs_set_header_bytenr(c
, c
->start
);
2116 btrfs_set_header_generation(c
, trans
->transid
);
2117 btrfs_set_header_owner(c
, root
->root_key
.objectid
);
2119 write_extent_buffer(c
, root
->fs_info
->fsid
,
2120 (unsigned long)btrfs_header_fsid(c
),
2123 write_extent_buffer(c
, root
->fs_info
->chunk_tree_uuid
,
2124 (unsigned long)btrfs_header_chunk_tree_uuid(c
),
2127 btrfs_set_node_key(c
, &lower_key
, 0);
2128 btrfs_set_node_blockptr(c
, 0, lower
->start
);
2129 lower_gen
= btrfs_header_generation(lower
);
2130 WARN_ON(lower_gen
!= trans
->transid
);
2132 btrfs_set_node_ptr_generation(c
, 0, lower_gen
);
2134 btrfs_mark_buffer_dirty(c
);
2136 spin_lock(&root
->node_lock
);
2139 spin_unlock(&root
->node_lock
);
2141 ret
= btrfs_update_extent_ref(trans
, root
, lower
->start
,
2142 lower
->start
, c
->start
,
2143 root
->root_key
.objectid
,
2144 trans
->transid
, level
- 1);
2147 /* the super has an extra ref to root->node */
2148 free_extent_buffer(old
);
2150 add_root_to_dirty_list(root
);
2151 extent_buffer_get(c
);
2152 path
->nodes
[level
] = c
;
2153 path
->locks
[level
] = 1;
2154 path
->slots
[level
] = 0;
2159 * worker function to insert a single pointer in a node.
2160 * the node should have enough room for the pointer already
2162 * slot and level indicate where you want the key to go, and
2163 * blocknr is the block the key points to.
2165 * returns zero on success and < 0 on any error
2167 static int insert_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
2168 *root
, struct btrfs_path
*path
, struct btrfs_disk_key
2169 *key
, u64 bytenr
, int slot
, int level
)
2171 struct extent_buffer
*lower
;
2174 BUG_ON(!path
->nodes
[level
]);
2175 lower
= path
->nodes
[level
];
2176 nritems
= btrfs_header_nritems(lower
);
2179 if (nritems
== BTRFS_NODEPTRS_PER_BLOCK(root
))
2181 if (slot
!= nritems
) {
2182 memmove_extent_buffer(lower
,
2183 btrfs_node_key_ptr_offset(slot
+ 1),
2184 btrfs_node_key_ptr_offset(slot
),
2185 (nritems
- slot
) * sizeof(struct btrfs_key_ptr
));
2187 btrfs_set_node_key(lower
, key
, slot
);
2188 btrfs_set_node_blockptr(lower
, slot
, bytenr
);
2189 WARN_ON(trans
->transid
== 0);
2190 btrfs_set_node_ptr_generation(lower
, slot
, trans
->transid
);
2191 btrfs_set_header_nritems(lower
, nritems
+ 1);
2192 btrfs_mark_buffer_dirty(lower
);
2197 * split the node at the specified level in path in two.
2198 * The path is corrected to point to the appropriate node after the split
2200 * Before splitting this tries to make some room in the node by pushing
2201 * left and right, if either one works, it returns right away.
2203 * returns 0 on success and < 0 on failure
2205 static noinline
int split_node(struct btrfs_trans_handle
*trans
,
2206 struct btrfs_root
*root
,
2207 struct btrfs_path
*path
, int level
)
2209 struct extent_buffer
*c
;
2210 struct extent_buffer
*split
;
2211 struct btrfs_disk_key disk_key
;
2217 c
= path
->nodes
[level
];
2218 WARN_ON(btrfs_header_generation(c
) != trans
->transid
);
2219 if (c
== root
->node
) {
2220 /* trying to split the root, lets make a new one */
2221 ret
= insert_new_root(trans
, root
, path
, level
+ 1);
2225 ret
= push_nodes_for_insert(trans
, root
, path
, level
);
2226 c
= path
->nodes
[level
];
2227 if (!ret
&& btrfs_header_nritems(c
) <
2228 BTRFS_NODEPTRS_PER_BLOCK(root
) - 3)
2234 c_nritems
= btrfs_header_nritems(c
);
2236 split
= btrfs_alloc_free_block(trans
, root
, root
->nodesize
,
2237 path
->nodes
[level
+ 1]->start
,
2238 root
->root_key
.objectid
,
2239 trans
->transid
, level
, c
->start
, 0);
2241 return PTR_ERR(split
);
2243 btrfs_set_header_flags(split
, btrfs_header_flags(c
));
2244 btrfs_set_header_level(split
, btrfs_header_level(c
));
2245 btrfs_set_header_bytenr(split
, split
->start
);
2246 btrfs_set_header_generation(split
, trans
->transid
);
2247 btrfs_set_header_owner(split
, root
->root_key
.objectid
);
2248 btrfs_set_header_flags(split
, 0);
2249 write_extent_buffer(split
, root
->fs_info
->fsid
,
2250 (unsigned long)btrfs_header_fsid(split
),
2252 write_extent_buffer(split
, root
->fs_info
->chunk_tree_uuid
,
2253 (unsigned long)btrfs_header_chunk_tree_uuid(split
),
2256 mid
= (c_nritems
+ 1) / 2;
2258 copy_extent_buffer(split
, c
,
2259 btrfs_node_key_ptr_offset(0),
2260 btrfs_node_key_ptr_offset(mid
),
2261 (c_nritems
- mid
) * sizeof(struct btrfs_key_ptr
));
2262 btrfs_set_header_nritems(split
, c_nritems
- mid
);
2263 btrfs_set_header_nritems(c
, mid
);
2266 btrfs_mark_buffer_dirty(c
);
2267 btrfs_mark_buffer_dirty(split
);
2269 btrfs_node_key(split
, &disk_key
, 0);
2270 wret
= insert_ptr(trans
, root
, path
, &disk_key
, split
->start
,
2271 path
->slots
[level
+ 1] + 1,
2276 ret
= btrfs_update_ref(trans
, root
, c
, split
, 0, c_nritems
- mid
);
2279 if (path
->slots
[level
] >= mid
) {
2280 path
->slots
[level
] -= mid
;
2281 btrfs_tree_unlock(c
);
2282 free_extent_buffer(c
);
2283 path
->nodes
[level
] = split
;
2284 path
->slots
[level
+ 1] += 1;
2286 btrfs_tree_unlock(split
);
2287 free_extent_buffer(split
);
2293 * how many bytes are required to store the items in a leaf. start
2294 * and nr indicate which items in the leaf to check. This totals up the
2295 * space used both by the item structs and the item data
2297 static int leaf_space_used(struct extent_buffer
*l
, int start
, int nr
)
2300 int nritems
= btrfs_header_nritems(l
);
2301 int end
= min(nritems
, start
+ nr
) - 1;
2305 data_len
= btrfs_item_end_nr(l
, start
);
2306 data_len
= data_len
- btrfs_item_offset_nr(l
, end
);
2307 data_len
+= sizeof(struct btrfs_item
) * nr
;
2308 WARN_ON(data_len
< 0);
2313 * The space between the end of the leaf items and
2314 * the start of the leaf data. IOW, how much room
2315 * the leaf has left for both items and data
2317 noinline
int btrfs_leaf_free_space(struct btrfs_root
*root
,
2318 struct extent_buffer
*leaf
)
2320 int nritems
= btrfs_header_nritems(leaf
);
2322 ret
= BTRFS_LEAF_DATA_SIZE(root
) - leaf_space_used(leaf
, 0, nritems
);
2324 printk(KERN_CRIT
"leaf free space ret %d, leaf data size %lu, "
2325 "used %d nritems %d\n",
2326 ret
, (unsigned long) BTRFS_LEAF_DATA_SIZE(root
),
2327 leaf_space_used(leaf
, 0, nritems
), nritems
);
2333 * push some data in the path leaf to the right, trying to free up at
2334 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2336 * returns 1 if the push failed because the other node didn't have enough
2337 * room, 0 if everything worked out and < 0 if there were major errors.
2339 static int push_leaf_right(struct btrfs_trans_handle
*trans
, struct btrfs_root
2340 *root
, struct btrfs_path
*path
, int data_size
,
2343 struct extent_buffer
*left
= path
->nodes
[0];
2344 struct extent_buffer
*right
;
2345 struct extent_buffer
*upper
;
2346 struct btrfs_disk_key disk_key
;
2352 struct btrfs_item
*item
;
2360 slot
= path
->slots
[1];
2361 if (!path
->nodes
[1])
2364 upper
= path
->nodes
[1];
2365 if (slot
>= btrfs_header_nritems(upper
) - 1)
2368 btrfs_assert_tree_locked(path
->nodes
[1]);
2370 right
= read_node_slot(root
, upper
, slot
+ 1);
2371 btrfs_tree_lock(right
);
2372 btrfs_set_lock_blocking(right
);
2374 free_space
= btrfs_leaf_free_space(root
, right
);
2375 if (free_space
< data_size
)
2378 /* cow and double check */
2379 ret
= btrfs_cow_block(trans
, root
, right
, upper
,
2380 slot
+ 1, &right
, 0);
2384 free_space
= btrfs_leaf_free_space(root
, right
);
2385 if (free_space
< data_size
)
2388 left_nritems
= btrfs_header_nritems(left
);
2389 if (left_nritems
== 0)
2397 if (path
->slots
[0] >= left_nritems
)
2398 push_space
+= data_size
;
2400 i
= left_nritems
- 1;
2402 item
= btrfs_item_nr(left
, i
);
2404 if (!empty
&& push_items
> 0) {
2405 if (path
->slots
[0] > i
)
2407 if (path
->slots
[0] == i
) {
2408 int space
= btrfs_leaf_free_space(root
, left
);
2409 if (space
+ push_space
* 2 > free_space
)
2414 if (path
->slots
[0] == i
)
2415 push_space
+= data_size
;
2417 if (!left
->map_token
) {
2418 map_extent_buffer(left
, (unsigned long)item
,
2419 sizeof(struct btrfs_item
),
2420 &left
->map_token
, &left
->kaddr
,
2421 &left
->map_start
, &left
->map_len
,
2425 this_item_size
= btrfs_item_size(left
, item
);
2426 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
2430 push_space
+= this_item_size
+ sizeof(*item
);
2435 if (left
->map_token
) {
2436 unmap_extent_buffer(left
, left
->map_token
, KM_USER1
);
2437 left
->map_token
= NULL
;
2440 if (push_items
== 0)
2443 if (!empty
&& push_items
== left_nritems
)
2446 /* push left to right */
2447 right_nritems
= btrfs_header_nritems(right
);
2449 push_space
= btrfs_item_end_nr(left
, left_nritems
- push_items
);
2450 push_space
-= leaf_data_end(root
, left
);
2452 /* make room in the right data area */
2453 data_end
= leaf_data_end(root
, right
);
2454 memmove_extent_buffer(right
,
2455 btrfs_leaf_data(right
) + data_end
- push_space
,
2456 btrfs_leaf_data(right
) + data_end
,
2457 BTRFS_LEAF_DATA_SIZE(root
) - data_end
);
2459 /* copy from the left data area */
2460 copy_extent_buffer(right
, left
, btrfs_leaf_data(right
) +
2461 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
2462 btrfs_leaf_data(left
) + leaf_data_end(root
, left
),
2465 memmove_extent_buffer(right
, btrfs_item_nr_offset(push_items
),
2466 btrfs_item_nr_offset(0),
2467 right_nritems
* sizeof(struct btrfs_item
));
2469 /* copy the items from left to right */
2470 copy_extent_buffer(right
, left
, btrfs_item_nr_offset(0),
2471 btrfs_item_nr_offset(left_nritems
- push_items
),
2472 push_items
* sizeof(struct btrfs_item
));
2474 /* update the item pointers */
2475 right_nritems
+= push_items
;
2476 btrfs_set_header_nritems(right
, right_nritems
);
2477 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
2478 for (i
= 0; i
< right_nritems
; i
++) {
2479 item
= btrfs_item_nr(right
, i
);
2480 if (!right
->map_token
) {
2481 map_extent_buffer(right
, (unsigned long)item
,
2482 sizeof(struct btrfs_item
),
2483 &right
->map_token
, &right
->kaddr
,
2484 &right
->map_start
, &right
->map_len
,
2487 push_space
-= btrfs_item_size(right
, item
);
2488 btrfs_set_item_offset(right
, item
, push_space
);
2491 if (right
->map_token
) {
2492 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
2493 right
->map_token
= NULL
;
2495 left_nritems
-= push_items
;
2496 btrfs_set_header_nritems(left
, left_nritems
);
2499 btrfs_mark_buffer_dirty(left
);
2500 btrfs_mark_buffer_dirty(right
);
2502 ret
= btrfs_update_ref(trans
, root
, left
, right
, 0, push_items
);
2505 btrfs_item_key(right
, &disk_key
, 0);
2506 btrfs_set_node_key(upper
, &disk_key
, slot
+ 1);
2507 btrfs_mark_buffer_dirty(upper
);
2509 /* then fixup the leaf pointer in the path */
2510 if (path
->slots
[0] >= left_nritems
) {
2511 path
->slots
[0] -= left_nritems
;
2512 if (btrfs_header_nritems(path
->nodes
[0]) == 0)
2513 clean_tree_block(trans
, root
, path
->nodes
[0]);
2514 btrfs_tree_unlock(path
->nodes
[0]);
2515 free_extent_buffer(path
->nodes
[0]);
2516 path
->nodes
[0] = right
;
2517 path
->slots
[1] += 1;
2519 btrfs_tree_unlock(right
);
2520 free_extent_buffer(right
);
2525 btrfs_tree_unlock(right
);
2526 free_extent_buffer(right
);
2531 * push some data in the path leaf to the left, trying to free up at
2532 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2534 static int push_leaf_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
2535 *root
, struct btrfs_path
*path
, int data_size
,
2538 struct btrfs_disk_key disk_key
;
2539 struct extent_buffer
*right
= path
->nodes
[0];
2540 struct extent_buffer
*left
;
2546 struct btrfs_item
*item
;
2547 u32 old_left_nritems
;
2553 u32 old_left_item_size
;
2555 slot
= path
->slots
[1];
2558 if (!path
->nodes
[1])
2561 right_nritems
= btrfs_header_nritems(right
);
2562 if (right_nritems
== 0)
2565 btrfs_assert_tree_locked(path
->nodes
[1]);
2567 left
= read_node_slot(root
, path
->nodes
[1], slot
- 1);
2568 btrfs_tree_lock(left
);
2569 btrfs_set_lock_blocking(left
);
2571 free_space
= btrfs_leaf_free_space(root
, left
);
2572 if (free_space
< data_size
) {
2577 /* cow and double check */
2578 ret
= btrfs_cow_block(trans
, root
, left
,
2579 path
->nodes
[1], slot
- 1, &left
, 0);
2581 /* we hit -ENOSPC, but it isn't fatal here */
2586 free_space
= btrfs_leaf_free_space(root
, left
);
2587 if (free_space
< data_size
) {
2595 nr
= right_nritems
- 1;
2597 for (i
= 0; i
< nr
; i
++) {
2598 item
= btrfs_item_nr(right
, i
);
2599 if (!right
->map_token
) {
2600 map_extent_buffer(right
, (unsigned long)item
,
2601 sizeof(struct btrfs_item
),
2602 &right
->map_token
, &right
->kaddr
,
2603 &right
->map_start
, &right
->map_len
,
2607 if (!empty
&& push_items
> 0) {
2608 if (path
->slots
[0] < i
)
2610 if (path
->slots
[0] == i
) {
2611 int space
= btrfs_leaf_free_space(root
, right
);
2612 if (space
+ push_space
* 2 > free_space
)
2617 if (path
->slots
[0] == i
)
2618 push_space
+= data_size
;
2620 this_item_size
= btrfs_item_size(right
, item
);
2621 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
2625 push_space
+= this_item_size
+ sizeof(*item
);
2628 if (right
->map_token
) {
2629 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
2630 right
->map_token
= NULL
;
2633 if (push_items
== 0) {
2637 if (!empty
&& push_items
== btrfs_header_nritems(right
))
2640 /* push data from right to left */
2641 copy_extent_buffer(left
, right
,
2642 btrfs_item_nr_offset(btrfs_header_nritems(left
)),
2643 btrfs_item_nr_offset(0),
2644 push_items
* sizeof(struct btrfs_item
));
2646 push_space
= BTRFS_LEAF_DATA_SIZE(root
) -
2647 btrfs_item_offset_nr(right
, push_items
- 1);
2649 copy_extent_buffer(left
, right
, btrfs_leaf_data(left
) +
2650 leaf_data_end(root
, left
) - push_space
,
2651 btrfs_leaf_data(right
) +
2652 btrfs_item_offset_nr(right
, push_items
- 1),
2654 old_left_nritems
= btrfs_header_nritems(left
);
2655 BUG_ON(old_left_nritems
<= 0);
2657 old_left_item_size
= btrfs_item_offset_nr(left
, old_left_nritems
- 1);
2658 for (i
= old_left_nritems
; i
< old_left_nritems
+ push_items
; i
++) {
2661 item
= btrfs_item_nr(left
, i
);
2662 if (!left
->map_token
) {
2663 map_extent_buffer(left
, (unsigned long)item
,
2664 sizeof(struct btrfs_item
),
2665 &left
->map_token
, &left
->kaddr
,
2666 &left
->map_start
, &left
->map_len
,
2670 ioff
= btrfs_item_offset(left
, item
);
2671 btrfs_set_item_offset(left
, item
,
2672 ioff
- (BTRFS_LEAF_DATA_SIZE(root
) - old_left_item_size
));
2674 btrfs_set_header_nritems(left
, old_left_nritems
+ push_items
);
2675 if (left
->map_token
) {
2676 unmap_extent_buffer(left
, left
->map_token
, KM_USER1
);
2677 left
->map_token
= NULL
;
2680 /* fixup right node */
2681 if (push_items
> right_nritems
) {
2682 printk(KERN_CRIT
"push items %d nr %u\n", push_items
,
2687 if (push_items
< right_nritems
) {
2688 push_space
= btrfs_item_offset_nr(right
, push_items
- 1) -
2689 leaf_data_end(root
, right
);
2690 memmove_extent_buffer(right
, btrfs_leaf_data(right
) +
2691 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
2692 btrfs_leaf_data(right
) +
2693 leaf_data_end(root
, right
), push_space
);
2695 memmove_extent_buffer(right
, btrfs_item_nr_offset(0),
2696 btrfs_item_nr_offset(push_items
),
2697 (btrfs_header_nritems(right
) - push_items
) *
2698 sizeof(struct btrfs_item
));
2700 right_nritems
-= push_items
;
2701 btrfs_set_header_nritems(right
, right_nritems
);
2702 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
2703 for (i
= 0; i
< right_nritems
; i
++) {
2704 item
= btrfs_item_nr(right
, i
);
2706 if (!right
->map_token
) {
2707 map_extent_buffer(right
, (unsigned long)item
,
2708 sizeof(struct btrfs_item
),
2709 &right
->map_token
, &right
->kaddr
,
2710 &right
->map_start
, &right
->map_len
,
2714 push_space
= push_space
- btrfs_item_size(right
, item
);
2715 btrfs_set_item_offset(right
, item
, push_space
);
2717 if (right
->map_token
) {
2718 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
2719 right
->map_token
= NULL
;
2722 btrfs_mark_buffer_dirty(left
);
2724 btrfs_mark_buffer_dirty(right
);
2726 ret
= btrfs_update_ref(trans
, root
, right
, left
,
2727 old_left_nritems
, push_items
);
2730 btrfs_item_key(right
, &disk_key
, 0);
2731 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
2735 /* then fixup the leaf pointer in the path */
2736 if (path
->slots
[0] < push_items
) {
2737 path
->slots
[0] += old_left_nritems
;
2738 if (btrfs_header_nritems(path
->nodes
[0]) == 0)
2739 clean_tree_block(trans
, root
, path
->nodes
[0]);
2740 btrfs_tree_unlock(path
->nodes
[0]);
2741 free_extent_buffer(path
->nodes
[0]);
2742 path
->nodes
[0] = left
;
2743 path
->slots
[1] -= 1;
2745 btrfs_tree_unlock(left
);
2746 free_extent_buffer(left
);
2747 path
->slots
[0] -= push_items
;
2749 BUG_ON(path
->slots
[0] < 0);
2752 btrfs_tree_unlock(left
);
2753 free_extent_buffer(left
);
2758 * split the path's leaf in two, making sure there is at least data_size
2759 * available for the resulting leaf level of the path.
2761 * returns 0 if all went well and < 0 on failure.
2763 static noinline
int split_leaf(struct btrfs_trans_handle
*trans
,
2764 struct btrfs_root
*root
,
2765 struct btrfs_key
*ins_key
,
2766 struct btrfs_path
*path
, int data_size
,
2769 struct extent_buffer
*l
;
2773 struct extent_buffer
*right
;
2780 int num_doubles
= 0;
2781 struct btrfs_disk_key disk_key
;
2783 /* first try to make some room by pushing left and right */
2784 if (data_size
&& ins_key
->type
!= BTRFS_DIR_ITEM_KEY
) {
2785 wret
= push_leaf_right(trans
, root
, path
, data_size
, 0);
2789 wret
= push_leaf_left(trans
, root
, path
, data_size
, 0);
2795 /* did the pushes work? */
2796 if (btrfs_leaf_free_space(root
, l
) >= data_size
)
2800 if (!path
->nodes
[1]) {
2801 ret
= insert_new_root(trans
, root
, path
, 1);
2808 slot
= path
->slots
[0];
2809 nritems
= btrfs_header_nritems(l
);
2810 mid
= (nritems
+ 1) / 2;
2812 right
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
2813 path
->nodes
[1]->start
,
2814 root
->root_key
.objectid
,
2815 trans
->transid
, 0, l
->start
, 0);
2816 if (IS_ERR(right
)) {
2818 return PTR_ERR(right
);
2821 memset_extent_buffer(right
, 0, 0, sizeof(struct btrfs_header
));
2822 btrfs_set_header_bytenr(right
, right
->start
);
2823 btrfs_set_header_generation(right
, trans
->transid
);
2824 btrfs_set_header_owner(right
, root
->root_key
.objectid
);
2825 btrfs_set_header_level(right
, 0);
2826 write_extent_buffer(right
, root
->fs_info
->fsid
,
2827 (unsigned long)btrfs_header_fsid(right
),
2830 write_extent_buffer(right
, root
->fs_info
->chunk_tree_uuid
,
2831 (unsigned long)btrfs_header_chunk_tree_uuid(right
),
2835 leaf_space_used(l
, mid
, nritems
- mid
) + data_size
>
2836 BTRFS_LEAF_DATA_SIZE(root
)) {
2837 if (slot
>= nritems
) {
2838 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
2839 btrfs_set_header_nritems(right
, 0);
2840 wret
= insert_ptr(trans
, root
, path
,
2841 &disk_key
, right
->start
,
2842 path
->slots
[1] + 1, 1);
2846 btrfs_tree_unlock(path
->nodes
[0]);
2847 free_extent_buffer(path
->nodes
[0]);
2848 path
->nodes
[0] = right
;
2850 path
->slots
[1] += 1;
2851 btrfs_mark_buffer_dirty(right
);
2855 if (mid
!= nritems
&&
2856 leaf_space_used(l
, mid
, nritems
- mid
) +
2857 data_size
> BTRFS_LEAF_DATA_SIZE(root
)) {
2862 if (leaf_space_used(l
, 0, mid
) + data_size
>
2863 BTRFS_LEAF_DATA_SIZE(root
)) {
2864 if (!extend
&& data_size
&& slot
== 0) {
2865 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
2866 btrfs_set_header_nritems(right
, 0);
2867 wret
= insert_ptr(trans
, root
, path
,
2873 btrfs_tree_unlock(path
->nodes
[0]);
2874 free_extent_buffer(path
->nodes
[0]);
2875 path
->nodes
[0] = right
;
2877 if (path
->slots
[1] == 0) {
2878 wret
= fixup_low_keys(trans
, root
,
2879 path
, &disk_key
, 1);
2883 btrfs_mark_buffer_dirty(right
);
2885 } else if ((extend
|| !data_size
) && slot
== 0) {
2889 if (mid
!= nritems
&&
2890 leaf_space_used(l
, mid
, nritems
- mid
) +
2891 data_size
> BTRFS_LEAF_DATA_SIZE(root
)) {
2897 nritems
= nritems
- mid
;
2898 btrfs_set_header_nritems(right
, nritems
);
2899 data_copy_size
= btrfs_item_end_nr(l
, mid
) - leaf_data_end(root
, l
);
2901 copy_extent_buffer(right
, l
, btrfs_item_nr_offset(0),
2902 btrfs_item_nr_offset(mid
),
2903 nritems
* sizeof(struct btrfs_item
));
2905 copy_extent_buffer(right
, l
,
2906 btrfs_leaf_data(right
) + BTRFS_LEAF_DATA_SIZE(root
) -
2907 data_copy_size
, btrfs_leaf_data(l
) +
2908 leaf_data_end(root
, l
), data_copy_size
);
2910 rt_data_off
= BTRFS_LEAF_DATA_SIZE(root
) -
2911 btrfs_item_end_nr(l
, mid
);
2913 for (i
= 0; i
< nritems
; i
++) {
2914 struct btrfs_item
*item
= btrfs_item_nr(right
, i
);
2917 if (!right
->map_token
) {
2918 map_extent_buffer(right
, (unsigned long)item
,
2919 sizeof(struct btrfs_item
),
2920 &right
->map_token
, &right
->kaddr
,
2921 &right
->map_start
, &right
->map_len
,
2925 ioff
= btrfs_item_offset(right
, item
);
2926 btrfs_set_item_offset(right
, item
, ioff
+ rt_data_off
);
2929 if (right
->map_token
) {
2930 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
2931 right
->map_token
= NULL
;
2934 btrfs_set_header_nritems(l
, mid
);
2936 btrfs_item_key(right
, &disk_key
, 0);
2937 wret
= insert_ptr(trans
, root
, path
, &disk_key
, right
->start
,
2938 path
->slots
[1] + 1, 1);
2942 btrfs_mark_buffer_dirty(right
);
2943 btrfs_mark_buffer_dirty(l
);
2944 BUG_ON(path
->slots
[0] != slot
);
2946 ret
= btrfs_update_ref(trans
, root
, l
, right
, 0, nritems
);
2950 btrfs_tree_unlock(path
->nodes
[0]);
2951 free_extent_buffer(path
->nodes
[0]);
2952 path
->nodes
[0] = right
;
2953 path
->slots
[0] -= mid
;
2954 path
->slots
[1] += 1;
2956 btrfs_tree_unlock(right
);
2957 free_extent_buffer(right
);
2960 BUG_ON(path
->slots
[0] < 0);
2963 BUG_ON(num_doubles
!= 0);
2971 * This function splits a single item into two items,
2972 * giving 'new_key' to the new item and splitting the
2973 * old one at split_offset (from the start of the item).
2975 * The path may be released by this operation. After
2976 * the split, the path is pointing to the old item. The
2977 * new item is going to be in the same node as the old one.
2979 * Note, the item being split must be smaller enough to live alone on
2980 * a tree block with room for one extra struct btrfs_item
2982 * This allows us to split the item in place, keeping a lock on the
2983 * leaf the entire time.
2985 int btrfs_split_item(struct btrfs_trans_handle
*trans
,
2986 struct btrfs_root
*root
,
2987 struct btrfs_path
*path
,
2988 struct btrfs_key
*new_key
,
2989 unsigned long split_offset
)
2992 struct extent_buffer
*leaf
;
2993 struct btrfs_key orig_key
;
2994 struct btrfs_item
*item
;
2995 struct btrfs_item
*new_item
;
3000 struct btrfs_disk_key disk_key
;
3003 leaf
= path
->nodes
[0];
3004 btrfs_item_key_to_cpu(leaf
, &orig_key
, path
->slots
[0]);
3005 if (btrfs_leaf_free_space(root
, leaf
) >= sizeof(struct btrfs_item
))
3008 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
3009 btrfs_release_path(root
, path
);
3011 path
->search_for_split
= 1;
3012 path
->keep_locks
= 1;
3014 ret
= btrfs_search_slot(trans
, root
, &orig_key
, path
, 0, 1);
3015 path
->search_for_split
= 0;
3017 /* if our item isn't there or got smaller, return now */
3018 if (ret
!= 0 || item_size
!= btrfs_item_size_nr(path
->nodes
[0],
3020 path
->keep_locks
= 0;
3024 ret
= split_leaf(trans
, root
, &orig_key
, path
,
3025 sizeof(struct btrfs_item
), 1);
3026 path
->keep_locks
= 0;
3030 * make sure any changes to the path from split_leaf leave it
3031 * in a blocking state
3033 btrfs_set_path_blocking(path
);
3035 leaf
= path
->nodes
[0];
3036 BUG_ON(btrfs_leaf_free_space(root
, leaf
) < sizeof(struct btrfs_item
));
3039 item
= btrfs_item_nr(leaf
, path
->slots
[0]);
3040 orig_offset
= btrfs_item_offset(leaf
, item
);
3041 item_size
= btrfs_item_size(leaf
, item
);
3044 buf
= kmalloc(item_size
, GFP_NOFS
);
3045 read_extent_buffer(leaf
, buf
, btrfs_item_ptr_offset(leaf
,
3046 path
->slots
[0]), item_size
);
3047 slot
= path
->slots
[0] + 1;
3048 leaf
= path
->nodes
[0];
3050 nritems
= btrfs_header_nritems(leaf
);
3052 if (slot
!= nritems
) {
3053 /* shift the items */
3054 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ 1),
3055 btrfs_item_nr_offset(slot
),
3056 (nritems
- slot
) * sizeof(struct btrfs_item
));
3060 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
3061 btrfs_set_item_key(leaf
, &disk_key
, slot
);
3063 new_item
= btrfs_item_nr(leaf
, slot
);
3065 btrfs_set_item_offset(leaf
, new_item
, orig_offset
);
3066 btrfs_set_item_size(leaf
, new_item
, item_size
- split_offset
);
3068 btrfs_set_item_offset(leaf
, item
,
3069 orig_offset
+ item_size
- split_offset
);
3070 btrfs_set_item_size(leaf
, item
, split_offset
);
3072 btrfs_set_header_nritems(leaf
, nritems
+ 1);
3074 /* write the data for the start of the original item */
3075 write_extent_buffer(leaf
, buf
,
3076 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
3079 /* write the data for the new item */
3080 write_extent_buffer(leaf
, buf
+ split_offset
,
3081 btrfs_item_ptr_offset(leaf
, slot
),
3082 item_size
- split_offset
);
3083 btrfs_mark_buffer_dirty(leaf
);
3086 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3087 btrfs_print_leaf(root
, leaf
);
3095 * make the item pointed to by the path smaller. new_size indicates
3096 * how small to make it, and from_end tells us if we just chop bytes
3097 * off the end of the item or if we shift the item to chop bytes off
3100 int btrfs_truncate_item(struct btrfs_trans_handle
*trans
,
3101 struct btrfs_root
*root
,
3102 struct btrfs_path
*path
,
3103 u32 new_size
, int from_end
)
3108 struct extent_buffer
*leaf
;
3109 struct btrfs_item
*item
;
3111 unsigned int data_end
;
3112 unsigned int old_data_start
;
3113 unsigned int old_size
;
3114 unsigned int size_diff
;
3117 slot_orig
= path
->slots
[0];
3118 leaf
= path
->nodes
[0];
3119 slot
= path
->slots
[0];
3121 old_size
= btrfs_item_size_nr(leaf
, slot
);
3122 if (old_size
== new_size
)
3125 nritems
= btrfs_header_nritems(leaf
);
3126 data_end
= leaf_data_end(root
, leaf
);
3128 old_data_start
= btrfs_item_offset_nr(leaf
, slot
);
3130 size_diff
= old_size
- new_size
;
3133 BUG_ON(slot
>= nritems
);
3136 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3138 /* first correct the data pointers */
3139 for (i
= slot
; i
< nritems
; i
++) {
3141 item
= btrfs_item_nr(leaf
, i
);
3143 if (!leaf
->map_token
) {
3144 map_extent_buffer(leaf
, (unsigned long)item
,
3145 sizeof(struct btrfs_item
),
3146 &leaf
->map_token
, &leaf
->kaddr
,
3147 &leaf
->map_start
, &leaf
->map_len
,
3151 ioff
= btrfs_item_offset(leaf
, item
);
3152 btrfs_set_item_offset(leaf
, item
, ioff
+ size_diff
);
3155 if (leaf
->map_token
) {
3156 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3157 leaf
->map_token
= NULL
;
3160 /* shift the data */
3162 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3163 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
3164 data_end
, old_data_start
+ new_size
- data_end
);
3166 struct btrfs_disk_key disk_key
;
3169 btrfs_item_key(leaf
, &disk_key
, slot
);
3171 if (btrfs_disk_key_type(&disk_key
) == BTRFS_EXTENT_DATA_KEY
) {
3173 struct btrfs_file_extent_item
*fi
;
3175 fi
= btrfs_item_ptr(leaf
, slot
,
3176 struct btrfs_file_extent_item
);
3177 fi
= (struct btrfs_file_extent_item
*)(
3178 (unsigned long)fi
- size_diff
);
3180 if (btrfs_file_extent_type(leaf
, fi
) ==
3181 BTRFS_FILE_EXTENT_INLINE
) {
3182 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
3183 memmove_extent_buffer(leaf
, ptr
,
3185 offsetof(struct btrfs_file_extent_item
,
3190 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3191 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
3192 data_end
, old_data_start
- data_end
);
3194 offset
= btrfs_disk_key_offset(&disk_key
);
3195 btrfs_set_disk_key_offset(&disk_key
, offset
+ size_diff
);
3196 btrfs_set_item_key(leaf
, &disk_key
, slot
);
3198 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
3201 item
= btrfs_item_nr(leaf
, slot
);
3202 btrfs_set_item_size(leaf
, item
, new_size
);
3203 btrfs_mark_buffer_dirty(leaf
);
3206 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3207 btrfs_print_leaf(root
, leaf
);
3214 * make the item pointed to by the path bigger, data_size is the new size.
3216 int btrfs_extend_item(struct btrfs_trans_handle
*trans
,
3217 struct btrfs_root
*root
, struct btrfs_path
*path
,
3223 struct extent_buffer
*leaf
;
3224 struct btrfs_item
*item
;
3226 unsigned int data_end
;
3227 unsigned int old_data
;
3228 unsigned int old_size
;
3231 slot_orig
= path
->slots
[0];
3232 leaf
= path
->nodes
[0];
3234 nritems
= btrfs_header_nritems(leaf
);
3235 data_end
= leaf_data_end(root
, leaf
);
3237 if (btrfs_leaf_free_space(root
, leaf
) < data_size
) {
3238 btrfs_print_leaf(root
, leaf
);
3241 slot
= path
->slots
[0];
3242 old_data
= btrfs_item_end_nr(leaf
, slot
);
3245 if (slot
>= nritems
) {
3246 btrfs_print_leaf(root
, leaf
);
3247 printk(KERN_CRIT
"slot %d too large, nritems %d\n",
3253 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3255 /* first correct the data pointers */
3256 for (i
= slot
; i
< nritems
; i
++) {
3258 item
= btrfs_item_nr(leaf
, i
);
3260 if (!leaf
->map_token
) {
3261 map_extent_buffer(leaf
, (unsigned long)item
,
3262 sizeof(struct btrfs_item
),
3263 &leaf
->map_token
, &leaf
->kaddr
,
3264 &leaf
->map_start
, &leaf
->map_len
,
3267 ioff
= btrfs_item_offset(leaf
, item
);
3268 btrfs_set_item_offset(leaf
, item
, ioff
- data_size
);
3271 if (leaf
->map_token
) {
3272 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3273 leaf
->map_token
= NULL
;
3276 /* shift the data */
3277 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3278 data_end
- data_size
, btrfs_leaf_data(leaf
) +
3279 data_end
, old_data
- data_end
);
3281 data_end
= old_data
;
3282 old_size
= btrfs_item_size_nr(leaf
, slot
);
3283 item
= btrfs_item_nr(leaf
, slot
);
3284 btrfs_set_item_size(leaf
, item
, old_size
+ data_size
);
3285 btrfs_mark_buffer_dirty(leaf
);
3288 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3289 btrfs_print_leaf(root
, leaf
);
3296 * Given a key and some data, insert items into the tree.
3297 * This does all the path init required, making room in the tree if needed.
3298 * Returns the number of keys that were inserted.
3300 int btrfs_insert_some_items(struct btrfs_trans_handle
*trans
,
3301 struct btrfs_root
*root
,
3302 struct btrfs_path
*path
,
3303 struct btrfs_key
*cpu_key
, u32
*data_size
,
3306 struct extent_buffer
*leaf
;
3307 struct btrfs_item
*item
;
3314 unsigned int data_end
;
3315 struct btrfs_disk_key disk_key
;
3316 struct btrfs_key found_key
;
3318 for (i
= 0; i
< nr
; i
++) {
3319 if (total_size
+ data_size
[i
] + sizeof(struct btrfs_item
) >
3320 BTRFS_LEAF_DATA_SIZE(root
)) {
3324 total_data
+= data_size
[i
];
3325 total_size
+= data_size
[i
] + sizeof(struct btrfs_item
);
3329 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
3335 leaf
= path
->nodes
[0];
3337 nritems
= btrfs_header_nritems(leaf
);
3338 data_end
= leaf_data_end(root
, leaf
);
3340 if (btrfs_leaf_free_space(root
, leaf
) < total_size
) {
3341 for (i
= nr
; i
>= 0; i
--) {
3342 total_data
-= data_size
[i
];
3343 total_size
-= data_size
[i
] + sizeof(struct btrfs_item
);
3344 if (total_size
< btrfs_leaf_free_space(root
, leaf
))
3350 slot
= path
->slots
[0];
3353 if (slot
!= nritems
) {
3354 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
3356 item
= btrfs_item_nr(leaf
, slot
);
3357 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3359 /* figure out how many keys we can insert in here */
3360 total_data
= data_size
[0];
3361 for (i
= 1; i
< nr
; i
++) {
3362 if (comp_cpu_keys(&found_key
, cpu_key
+ i
) <= 0)
3364 total_data
+= data_size
[i
];
3368 if (old_data
< data_end
) {
3369 btrfs_print_leaf(root
, leaf
);
3370 printk(KERN_CRIT
"slot %d old_data %d data_end %d\n",
3371 slot
, old_data
, data_end
);
3375 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3377 /* first correct the data pointers */
3378 WARN_ON(leaf
->map_token
);
3379 for (i
= slot
; i
< nritems
; i
++) {
3382 item
= btrfs_item_nr(leaf
, i
);
3383 if (!leaf
->map_token
) {
3384 map_extent_buffer(leaf
, (unsigned long)item
,
3385 sizeof(struct btrfs_item
),
3386 &leaf
->map_token
, &leaf
->kaddr
,
3387 &leaf
->map_start
, &leaf
->map_len
,
3391 ioff
= btrfs_item_offset(leaf
, item
);
3392 btrfs_set_item_offset(leaf
, item
, ioff
- total_data
);
3394 if (leaf
->map_token
) {
3395 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3396 leaf
->map_token
= NULL
;
3399 /* shift the items */
3400 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
3401 btrfs_item_nr_offset(slot
),
3402 (nritems
- slot
) * sizeof(struct btrfs_item
));
3404 /* shift the data */
3405 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3406 data_end
- total_data
, btrfs_leaf_data(leaf
) +
3407 data_end
, old_data
- data_end
);
3408 data_end
= old_data
;
3411 * this sucks but it has to be done, if we are inserting at
3412 * the end of the leaf only insert 1 of the items, since we
3413 * have no way of knowing whats on the next leaf and we'd have
3414 * to drop our current locks to figure it out
3419 /* setup the item for the new data */
3420 for (i
= 0; i
< nr
; i
++) {
3421 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
3422 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
3423 item
= btrfs_item_nr(leaf
, slot
+ i
);
3424 btrfs_set_item_offset(leaf
, item
, data_end
- data_size
[i
]);
3425 data_end
-= data_size
[i
];
3426 btrfs_set_item_size(leaf
, item
, data_size
[i
]);
3428 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
3429 btrfs_mark_buffer_dirty(leaf
);
3433 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
3434 ret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
3437 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3438 btrfs_print_leaf(root
, leaf
);
3448 * Given a key and some data, insert items into the tree.
3449 * This does all the path init required, making room in the tree if needed.
3451 int btrfs_insert_empty_items(struct btrfs_trans_handle
*trans
,
3452 struct btrfs_root
*root
,
3453 struct btrfs_path
*path
,
3454 struct btrfs_key
*cpu_key
, u32
*data_size
,
3457 struct extent_buffer
*leaf
;
3458 struct btrfs_item
*item
;
3466 unsigned int data_end
;
3467 struct btrfs_disk_key disk_key
;
3469 for (i
= 0; i
< nr
; i
++)
3470 total_data
+= data_size
[i
];
3472 total_size
= total_data
+ (nr
* sizeof(struct btrfs_item
));
3473 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
3479 slot_orig
= path
->slots
[0];
3480 leaf
= path
->nodes
[0];
3482 nritems
= btrfs_header_nritems(leaf
);
3483 data_end
= leaf_data_end(root
, leaf
);
3485 if (btrfs_leaf_free_space(root
, leaf
) < total_size
) {
3486 btrfs_print_leaf(root
, leaf
);
3487 printk(KERN_CRIT
"not enough freespace need %u have %d\n",
3488 total_size
, btrfs_leaf_free_space(root
, leaf
));
3492 slot
= path
->slots
[0];
3495 if (slot
!= nritems
) {
3496 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
3498 if (old_data
< data_end
) {
3499 btrfs_print_leaf(root
, leaf
);
3500 printk(KERN_CRIT
"slot %d old_data %d data_end %d\n",
3501 slot
, old_data
, data_end
);
3505 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3507 /* first correct the data pointers */
3508 WARN_ON(leaf
->map_token
);
3509 for (i
= slot
; i
< nritems
; i
++) {
3512 item
= btrfs_item_nr(leaf
, i
);
3513 if (!leaf
->map_token
) {
3514 map_extent_buffer(leaf
, (unsigned long)item
,
3515 sizeof(struct btrfs_item
),
3516 &leaf
->map_token
, &leaf
->kaddr
,
3517 &leaf
->map_start
, &leaf
->map_len
,
3521 ioff
= btrfs_item_offset(leaf
, item
);
3522 btrfs_set_item_offset(leaf
, item
, ioff
- total_data
);
3524 if (leaf
->map_token
) {
3525 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3526 leaf
->map_token
= NULL
;
3529 /* shift the items */
3530 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
3531 btrfs_item_nr_offset(slot
),
3532 (nritems
- slot
) * sizeof(struct btrfs_item
));
3534 /* shift the data */
3535 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3536 data_end
- total_data
, btrfs_leaf_data(leaf
) +
3537 data_end
, old_data
- data_end
);
3538 data_end
= old_data
;
3541 /* setup the item for the new data */
3542 for (i
= 0; i
< nr
; i
++) {
3543 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
3544 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
3545 item
= btrfs_item_nr(leaf
, slot
+ i
);
3546 btrfs_set_item_offset(leaf
, item
, data_end
- data_size
[i
]);
3547 data_end
-= data_size
[i
];
3548 btrfs_set_item_size(leaf
, item
, data_size
[i
]);
3550 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
3551 btrfs_mark_buffer_dirty(leaf
);
3555 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
3556 ret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
3559 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3560 btrfs_print_leaf(root
, leaf
);
3564 btrfs_unlock_up_safe(path
, 1);
3569 * Given a key and some data, insert an item into the tree.
3570 * This does all the path init required, making room in the tree if needed.
3572 int btrfs_insert_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
3573 *root
, struct btrfs_key
*cpu_key
, void *data
, u32
3577 struct btrfs_path
*path
;
3578 struct extent_buffer
*leaf
;
3581 path
= btrfs_alloc_path();
3583 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
3585 leaf
= path
->nodes
[0];
3586 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
3587 write_extent_buffer(leaf
, data
, ptr
, data_size
);
3588 btrfs_mark_buffer_dirty(leaf
);
3590 btrfs_free_path(path
);
3595 * delete the pointer from a given node.
3597 * the tree should have been previously balanced so the deletion does not
3600 static int del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
3601 struct btrfs_path
*path
, int level
, int slot
)
3603 struct extent_buffer
*parent
= path
->nodes
[level
];
3608 nritems
= btrfs_header_nritems(parent
);
3609 if (slot
!= nritems
- 1) {
3610 memmove_extent_buffer(parent
,
3611 btrfs_node_key_ptr_offset(slot
),
3612 btrfs_node_key_ptr_offset(slot
+ 1),
3613 sizeof(struct btrfs_key_ptr
) *
3614 (nritems
- slot
- 1));
3617 btrfs_set_header_nritems(parent
, nritems
);
3618 if (nritems
== 0 && parent
== root
->node
) {
3619 BUG_ON(btrfs_header_level(root
->node
) != 1);
3620 /* just turn the root into a leaf and break */
3621 btrfs_set_header_level(root
->node
, 0);
3622 } else if (slot
== 0) {
3623 struct btrfs_disk_key disk_key
;
3625 btrfs_node_key(parent
, &disk_key
, 0);
3626 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, level
+ 1);
3630 btrfs_mark_buffer_dirty(parent
);
3635 * a helper function to delete the leaf pointed to by path->slots[1] and
3636 * path->nodes[1]. bytenr is the node block pointer, but since the callers
3637 * already know it, it is faster to have them pass it down than to
3638 * read it out of the node again.
3640 * This deletes the pointer in path->nodes[1] and frees the leaf
3641 * block extent. zero is returned if it all worked out, < 0 otherwise.
3643 * The path must have already been setup for deleting the leaf, including
3644 * all the proper balancing. path->nodes[1] must be locked.
3646 noinline
int btrfs_del_leaf(struct btrfs_trans_handle
*trans
,
3647 struct btrfs_root
*root
,
3648 struct btrfs_path
*path
, u64 bytenr
)
3651 u64 root_gen
= btrfs_header_generation(path
->nodes
[1]);
3652 u64 parent_start
= path
->nodes
[1]->start
;
3653 u64 parent_owner
= btrfs_header_owner(path
->nodes
[1]);
3655 ret
= del_ptr(trans
, root
, path
, 1, path
->slots
[1]);
3660 * btrfs_free_extent is expensive, we want to make sure we
3661 * aren't holding any locks when we call it
3663 btrfs_unlock_up_safe(path
, 0);
3665 ret
= btrfs_free_extent(trans
, root
, bytenr
,
3666 btrfs_level_size(root
, 0),
3667 parent_start
, parent_owner
,
3672 * delete the item at the leaf level in path. If that empties
3673 * the leaf, remove it from the tree
3675 int btrfs_del_items(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
3676 struct btrfs_path
*path
, int slot
, int nr
)
3678 struct extent_buffer
*leaf
;
3679 struct btrfs_item
*item
;
3687 leaf
= path
->nodes
[0];
3688 last_off
= btrfs_item_offset_nr(leaf
, slot
+ nr
- 1);
3690 for (i
= 0; i
< nr
; i
++)
3691 dsize
+= btrfs_item_size_nr(leaf
, slot
+ i
);
3693 nritems
= btrfs_header_nritems(leaf
);
3695 if (slot
+ nr
!= nritems
) {
3696 int data_end
= leaf_data_end(root
, leaf
);
3698 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3700 btrfs_leaf_data(leaf
) + data_end
,
3701 last_off
- data_end
);
3703 for (i
= slot
+ nr
; i
< nritems
; i
++) {
3706 item
= btrfs_item_nr(leaf
, i
);
3707 if (!leaf
->map_token
) {
3708 map_extent_buffer(leaf
, (unsigned long)item
,
3709 sizeof(struct btrfs_item
),
3710 &leaf
->map_token
, &leaf
->kaddr
,
3711 &leaf
->map_start
, &leaf
->map_len
,
3714 ioff
= btrfs_item_offset(leaf
, item
);
3715 btrfs_set_item_offset(leaf
, item
, ioff
+ dsize
);
3718 if (leaf
->map_token
) {
3719 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3720 leaf
->map_token
= NULL
;
3723 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
),
3724 btrfs_item_nr_offset(slot
+ nr
),
3725 sizeof(struct btrfs_item
) *
3726 (nritems
- slot
- nr
));
3728 btrfs_set_header_nritems(leaf
, nritems
- nr
);
3731 /* delete the leaf if we've emptied it */
3733 if (leaf
== root
->node
) {
3734 btrfs_set_header_level(leaf
, 0);
3736 ret
= btrfs_del_leaf(trans
, root
, path
, leaf
->start
);
3740 int used
= leaf_space_used(leaf
, 0, nritems
);
3742 struct btrfs_disk_key disk_key
;
3744 btrfs_item_key(leaf
, &disk_key
, 0);
3745 wret
= fixup_low_keys(trans
, root
, path
,
3751 /* delete the leaf if it is mostly empty */
3752 if (used
< BTRFS_LEAF_DATA_SIZE(root
) / 4) {
3753 /* push_leaf_left fixes the path.
3754 * make sure the path still points to our leaf
3755 * for possible call to del_ptr below
3757 slot
= path
->slots
[1];
3758 extent_buffer_get(leaf
);
3760 wret
= push_leaf_left(trans
, root
, path
, 1, 1);
3761 if (wret
< 0 && wret
!= -ENOSPC
)
3764 if (path
->nodes
[0] == leaf
&&
3765 btrfs_header_nritems(leaf
)) {
3766 wret
= push_leaf_right(trans
, root
, path
, 1, 1);
3767 if (wret
< 0 && wret
!= -ENOSPC
)
3771 if (btrfs_header_nritems(leaf
) == 0) {
3772 path
->slots
[1] = slot
;
3773 ret
= btrfs_del_leaf(trans
, root
, path
,
3776 free_extent_buffer(leaf
);
3778 /* if we're still in the path, make sure
3779 * we're dirty. Otherwise, one of the
3780 * push_leaf functions must have already
3781 * dirtied this buffer
3783 if (path
->nodes
[0] == leaf
)
3784 btrfs_mark_buffer_dirty(leaf
);
3785 free_extent_buffer(leaf
);
3788 btrfs_mark_buffer_dirty(leaf
);
3795 * search the tree again to find a leaf with lesser keys
3796 * returns 0 if it found something or 1 if there are no lesser leaves.
3797 * returns < 0 on io errors.
3799 * This may release the path, and so you may lose any locks held at the
3802 int btrfs_prev_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
3804 struct btrfs_key key
;
3805 struct btrfs_disk_key found_key
;
3808 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, 0);
3812 else if (key
.type
> 0)
3814 else if (key
.objectid
> 0)
3819 btrfs_release_path(root
, path
);
3820 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3823 btrfs_item_key(path
->nodes
[0], &found_key
, 0);
3824 ret
= comp_keys(&found_key
, &key
);
3831 * A helper function to walk down the tree starting at min_key, and looking
3832 * for nodes or leaves that are either in cache or have a minimum
3833 * transaction id. This is used by the btree defrag code, and tree logging
3835 * This does not cow, but it does stuff the starting key it finds back
3836 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3837 * key and get a writable path.
3839 * This does lock as it descends, and path->keep_locks should be set
3840 * to 1 by the caller.
3842 * This honors path->lowest_level to prevent descent past a given level
3845 * min_trans indicates the oldest transaction that you are interested
3846 * in walking through. Any nodes or leaves older than min_trans are
3847 * skipped over (without reading them).
3849 * returns zero if something useful was found, < 0 on error and 1 if there
3850 * was nothing in the tree that matched the search criteria.
3852 int btrfs_search_forward(struct btrfs_root
*root
, struct btrfs_key
*min_key
,
3853 struct btrfs_key
*max_key
,
3854 struct btrfs_path
*path
, int cache_only
,
3857 struct extent_buffer
*cur
;
3858 struct btrfs_key found_key
;
3865 WARN_ON(!path
->keep_locks
);
3867 cur
= btrfs_lock_root_node(root
);
3868 level
= btrfs_header_level(cur
);
3869 WARN_ON(path
->nodes
[level
]);
3870 path
->nodes
[level
] = cur
;
3871 path
->locks
[level
] = 1;
3873 if (btrfs_header_generation(cur
) < min_trans
) {
3878 nritems
= btrfs_header_nritems(cur
);
3879 level
= btrfs_header_level(cur
);
3880 sret
= bin_search(cur
, min_key
, level
, &slot
);
3882 /* at the lowest level, we're done, setup the path and exit */
3883 if (level
== path
->lowest_level
) {
3884 if (slot
>= nritems
)
3887 path
->slots
[level
] = slot
;
3888 btrfs_item_key_to_cpu(cur
, &found_key
, slot
);
3891 if (sret
&& slot
> 0)
3894 * check this node pointer against the cache_only and
3895 * min_trans parameters. If it isn't in cache or is too
3896 * old, skip to the next one.
3898 while (slot
< nritems
) {
3901 struct extent_buffer
*tmp
;
3902 struct btrfs_disk_key disk_key
;
3904 blockptr
= btrfs_node_blockptr(cur
, slot
);
3905 gen
= btrfs_node_ptr_generation(cur
, slot
);
3906 if (gen
< min_trans
) {
3914 btrfs_node_key(cur
, &disk_key
, slot
);
3915 if (comp_keys(&disk_key
, max_key
) >= 0) {
3921 tmp
= btrfs_find_tree_block(root
, blockptr
,
3922 btrfs_level_size(root
, level
- 1));
3924 if (tmp
&& btrfs_buffer_uptodate(tmp
, gen
)) {
3925 free_extent_buffer(tmp
);
3929 free_extent_buffer(tmp
);
3934 * we didn't find a candidate key in this node, walk forward
3935 * and find another one
3937 if (slot
>= nritems
) {
3938 path
->slots
[level
] = slot
;
3939 btrfs_set_path_blocking(path
);
3940 sret
= btrfs_find_next_key(root
, path
, min_key
, level
,
3941 cache_only
, min_trans
);
3943 btrfs_release_path(root
, path
);
3949 /* save our key for returning back */
3950 btrfs_node_key_to_cpu(cur
, &found_key
, slot
);
3951 path
->slots
[level
] = slot
;
3952 if (level
== path
->lowest_level
) {
3954 unlock_up(path
, level
, 1);
3957 btrfs_set_path_blocking(path
);
3958 cur
= read_node_slot(root
, cur
, slot
);
3960 btrfs_tree_lock(cur
);
3962 path
->locks
[level
- 1] = 1;
3963 path
->nodes
[level
- 1] = cur
;
3964 unlock_up(path
, level
, 1);
3965 btrfs_clear_path_blocking(path
, NULL
);
3969 memcpy(min_key
, &found_key
, sizeof(found_key
));
3970 btrfs_set_path_blocking(path
);
3975 * this is similar to btrfs_next_leaf, but does not try to preserve
3976 * and fixup the path. It looks for and returns the next key in the
3977 * tree based on the current path and the cache_only and min_trans
3980 * 0 is returned if another key is found, < 0 if there are any errors
3981 * and 1 is returned if there are no higher keys in the tree
3983 * path->keep_locks should be set to 1 on the search made before
3984 * calling this function.
3986 int btrfs_find_next_key(struct btrfs_root
*root
, struct btrfs_path
*path
,
3987 struct btrfs_key
*key
, int lowest_level
,
3988 int cache_only
, u64 min_trans
)
3990 int level
= lowest_level
;
3992 struct extent_buffer
*c
;
3994 WARN_ON(!path
->keep_locks
);
3995 while (level
< BTRFS_MAX_LEVEL
) {
3996 if (!path
->nodes
[level
])
3999 slot
= path
->slots
[level
] + 1;
4000 c
= path
->nodes
[level
];
4002 if (slot
>= btrfs_header_nritems(c
)) {
4004 if (level
== BTRFS_MAX_LEVEL
)
4009 btrfs_item_key_to_cpu(c
, key
, slot
);
4011 u64 blockptr
= btrfs_node_blockptr(c
, slot
);
4012 u64 gen
= btrfs_node_ptr_generation(c
, slot
);
4015 struct extent_buffer
*cur
;
4016 cur
= btrfs_find_tree_block(root
, blockptr
,
4017 btrfs_level_size(root
, level
- 1));
4018 if (!cur
|| !btrfs_buffer_uptodate(cur
, gen
)) {
4021 free_extent_buffer(cur
);
4024 free_extent_buffer(cur
);
4026 if (gen
< min_trans
) {
4030 btrfs_node_key_to_cpu(c
, key
, slot
);
4038 * search the tree again to find a leaf with greater keys
4039 * returns 0 if it found something or 1 if there are no greater leaves.
4040 * returns < 0 on io errors.
4042 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
4046 struct extent_buffer
*c
;
4047 struct extent_buffer
*next
= NULL
;
4048 struct btrfs_key key
;
4052 nritems
= btrfs_header_nritems(path
->nodes
[0]);
4056 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, nritems
- 1);
4058 btrfs_release_path(root
, path
);
4059 path
->keep_locks
= 1;
4060 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4061 path
->keep_locks
= 0;
4066 btrfs_set_path_blocking(path
);
4067 nritems
= btrfs_header_nritems(path
->nodes
[0]);
4069 * by releasing the path above we dropped all our locks. A balance
4070 * could have added more items next to the key that used to be
4071 * at the very end of the block. So, check again here and
4072 * advance the path if there are now more items available.
4074 if (nritems
> 0 && path
->slots
[0] < nritems
- 1) {
4079 while (level
< BTRFS_MAX_LEVEL
) {
4080 if (!path
->nodes
[level
])
4083 slot
= path
->slots
[level
] + 1;
4084 c
= path
->nodes
[level
];
4085 if (slot
>= btrfs_header_nritems(c
)) {
4087 if (level
== BTRFS_MAX_LEVEL
)
4093 btrfs_tree_unlock(next
);
4094 free_extent_buffer(next
);
4097 /* the path was set to blocking above */
4098 if (level
== 1 && (path
->locks
[1] || path
->skip_locking
) &&
4100 reada_for_search(root
, path
, level
, slot
, 0);
4102 next
= read_node_slot(root
, c
, slot
);
4103 if (!path
->skip_locking
) {
4104 btrfs_assert_tree_locked(c
);
4105 btrfs_tree_lock(next
);
4106 btrfs_set_lock_blocking(next
);
4110 path
->slots
[level
] = slot
;
4113 c
= path
->nodes
[level
];
4114 if (path
->locks
[level
])
4115 btrfs_tree_unlock(c
);
4116 free_extent_buffer(c
);
4117 path
->nodes
[level
] = next
;
4118 path
->slots
[level
] = 0;
4119 if (!path
->skip_locking
)
4120 path
->locks
[level
] = 1;
4124 btrfs_set_path_blocking(path
);
4125 if (level
== 1 && path
->locks
[1] && path
->reada
)
4126 reada_for_search(root
, path
, level
, slot
, 0);
4127 next
= read_node_slot(root
, next
, 0);
4128 if (!path
->skip_locking
) {
4129 btrfs_assert_tree_locked(path
->nodes
[level
]);
4130 btrfs_tree_lock(next
);
4131 btrfs_set_lock_blocking(next
);
4135 unlock_up(path
, 0, 1);
4140 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4141 * searching until it gets past min_objectid or finds an item of 'type'
4143 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4145 int btrfs_previous_item(struct btrfs_root
*root
,
4146 struct btrfs_path
*path
, u64 min_objectid
,
4149 struct btrfs_key found_key
;
4150 struct extent_buffer
*leaf
;
4155 if (path
->slots
[0] == 0) {
4156 btrfs_set_path_blocking(path
);
4157 ret
= btrfs_prev_leaf(root
, path
);
4163 leaf
= path
->nodes
[0];
4164 nritems
= btrfs_header_nritems(leaf
);
4167 if (path
->slots
[0] == nritems
)
4170 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4171 if (found_key
.type
== type
)
4173 if (found_key
.objectid
< min_objectid
)
4175 if (found_key
.objectid
== min_objectid
&&
4176 found_key
.type
< type
)