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>
20 #include <linux/slab.h>
23 #include "transaction.h"
24 #include "print-tree.h"
27 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
28 *root
, struct btrfs_path
*path
, int level
);
29 static int split_leaf(struct btrfs_trans_handle
*trans
, struct btrfs_root
30 *root
, struct btrfs_key
*ins_key
,
31 struct btrfs_path
*path
, int data_size
, int extend
);
32 static int push_node_left(struct btrfs_trans_handle
*trans
,
33 struct btrfs_root
*root
, struct extent_buffer
*dst
,
34 struct extent_buffer
*src
, int empty
);
35 static int balance_node_right(struct btrfs_trans_handle
*trans
,
36 struct btrfs_root
*root
,
37 struct extent_buffer
*dst_buf
,
38 struct extent_buffer
*src_buf
);
39 static void del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
40 struct btrfs_path
*path
, int level
, int slot
);
42 struct btrfs_path
*btrfs_alloc_path(void)
44 struct btrfs_path
*path
;
45 path
= kmem_cache_zalloc(btrfs_path_cachep
, GFP_NOFS
);
50 * set all locked nodes in the path to blocking locks. This should
51 * be done before scheduling
53 noinline
void btrfs_set_path_blocking(struct btrfs_path
*p
)
56 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
57 if (!p
->nodes
[i
] || !p
->locks
[i
])
59 btrfs_set_lock_blocking_rw(p
->nodes
[i
], p
->locks
[i
]);
60 if (p
->locks
[i
] == BTRFS_READ_LOCK
)
61 p
->locks
[i
] = BTRFS_READ_LOCK_BLOCKING
;
62 else if (p
->locks
[i
] == BTRFS_WRITE_LOCK
)
63 p
->locks
[i
] = BTRFS_WRITE_LOCK_BLOCKING
;
68 * reset all the locked nodes in the patch to spinning locks.
70 * held is used to keep lockdep happy, when lockdep is enabled
71 * we set held to a blocking lock before we go around and
72 * retake all the spinlocks in the path. You can safely use NULL
75 noinline
void btrfs_clear_path_blocking(struct btrfs_path
*p
,
76 struct extent_buffer
*held
, int held_rw
)
80 #ifdef CONFIG_DEBUG_LOCK_ALLOC
81 /* lockdep really cares that we take all of these spinlocks
82 * in the right order. If any of the locks in the path are not
83 * currently blocking, it is going to complain. So, make really
84 * really sure by forcing the path to blocking before we clear
88 btrfs_set_lock_blocking_rw(held
, held_rw
);
89 if (held_rw
== BTRFS_WRITE_LOCK
)
90 held_rw
= BTRFS_WRITE_LOCK_BLOCKING
;
91 else if (held_rw
== BTRFS_READ_LOCK
)
92 held_rw
= BTRFS_READ_LOCK_BLOCKING
;
94 btrfs_set_path_blocking(p
);
97 for (i
= BTRFS_MAX_LEVEL
- 1; i
>= 0; i
--) {
98 if (p
->nodes
[i
] && p
->locks
[i
]) {
99 btrfs_clear_lock_blocking_rw(p
->nodes
[i
], p
->locks
[i
]);
100 if (p
->locks
[i
] == BTRFS_WRITE_LOCK_BLOCKING
)
101 p
->locks
[i
] = BTRFS_WRITE_LOCK
;
102 else if (p
->locks
[i
] == BTRFS_READ_LOCK_BLOCKING
)
103 p
->locks
[i
] = BTRFS_READ_LOCK
;
107 #ifdef CONFIG_DEBUG_LOCK_ALLOC
109 btrfs_clear_lock_blocking_rw(held
, held_rw
);
113 /* this also releases the path */
114 void btrfs_free_path(struct btrfs_path
*p
)
118 btrfs_release_path(p
);
119 kmem_cache_free(btrfs_path_cachep
, p
);
123 * path release drops references on the extent buffers in the path
124 * and it drops any locks held by this path
126 * It is safe to call this on paths that no locks or extent buffers held.
128 noinline
void btrfs_release_path(struct btrfs_path
*p
)
132 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
137 btrfs_tree_unlock_rw(p
->nodes
[i
], p
->locks
[i
]);
140 free_extent_buffer(p
->nodes
[i
]);
146 * safely gets a reference on the root node of a tree. A lock
147 * is not taken, so a concurrent writer may put a different node
148 * at the root of the tree. See btrfs_lock_root_node for the
151 * The extent buffer returned by this has a reference taken, so
152 * it won't disappear. It may stop being the root of the tree
153 * at any time because there are no locks held.
155 struct extent_buffer
*btrfs_root_node(struct btrfs_root
*root
)
157 struct extent_buffer
*eb
;
161 eb
= rcu_dereference(root
->node
);
164 * RCU really hurts here, we could free up the root node because
165 * it was cow'ed but we may not get the new root node yet so do
166 * the inc_not_zero dance and if it doesn't work then
167 * synchronize_rcu and try again.
169 if (atomic_inc_not_zero(&eb
->refs
)) {
179 /* loop around taking references on and locking the root node of the
180 * tree until you end up with a lock on the root. A locked buffer
181 * is returned, with a reference held.
183 struct extent_buffer
*btrfs_lock_root_node(struct btrfs_root
*root
)
185 struct extent_buffer
*eb
;
188 eb
= btrfs_root_node(root
);
190 if (eb
== root
->node
)
192 btrfs_tree_unlock(eb
);
193 free_extent_buffer(eb
);
198 /* loop around taking references on and locking the root node of the
199 * tree until you end up with a lock on the root. A locked buffer
200 * is returned, with a reference held.
202 struct extent_buffer
*btrfs_read_lock_root_node(struct btrfs_root
*root
)
204 struct extent_buffer
*eb
;
207 eb
= btrfs_root_node(root
);
208 btrfs_tree_read_lock(eb
);
209 if (eb
== root
->node
)
211 btrfs_tree_read_unlock(eb
);
212 free_extent_buffer(eb
);
217 /* cowonly root (everything not a reference counted cow subvolume), just get
218 * put onto a simple dirty list. transaction.c walks this to make sure they
219 * get properly updated on disk.
221 static void add_root_to_dirty_list(struct btrfs_root
*root
)
223 if (root
->track_dirty
&& list_empty(&root
->dirty_list
)) {
224 list_add(&root
->dirty_list
,
225 &root
->fs_info
->dirty_cowonly_roots
);
230 * used by snapshot creation to make a copy of a root for a tree with
231 * a given objectid. The buffer with the new root node is returned in
232 * cow_ret, and this func returns zero on success or a negative error code.
234 int btrfs_copy_root(struct btrfs_trans_handle
*trans
,
235 struct btrfs_root
*root
,
236 struct extent_buffer
*buf
,
237 struct extent_buffer
**cow_ret
, u64 new_root_objectid
)
239 struct extent_buffer
*cow
;
242 struct btrfs_disk_key disk_key
;
244 WARN_ON(root
->ref_cows
&& trans
->transid
!=
245 root
->fs_info
->running_transaction
->transid
);
246 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
248 level
= btrfs_header_level(buf
);
250 btrfs_item_key(buf
, &disk_key
, 0);
252 btrfs_node_key(buf
, &disk_key
, 0);
254 cow
= btrfs_alloc_free_block(trans
, root
, buf
->len
, 0,
255 new_root_objectid
, &disk_key
, level
,
260 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
261 btrfs_set_header_bytenr(cow
, cow
->start
);
262 btrfs_set_header_generation(cow
, trans
->transid
);
263 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
264 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
265 BTRFS_HEADER_FLAG_RELOC
);
266 if (new_root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
267 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
269 btrfs_set_header_owner(cow
, new_root_objectid
);
271 write_extent_buffer(cow
, root
->fs_info
->fsid
,
272 (unsigned long)btrfs_header_fsid(cow
),
275 WARN_ON(btrfs_header_generation(buf
) > trans
->transid
);
276 if (new_root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
277 ret
= btrfs_inc_ref(trans
, root
, cow
, 1, 1);
279 ret
= btrfs_inc_ref(trans
, root
, cow
, 0, 1);
284 btrfs_mark_buffer_dirty(cow
);
290 * check if the tree block can be shared by multiple trees
292 int btrfs_block_can_be_shared(struct btrfs_root
*root
,
293 struct extent_buffer
*buf
)
296 * Tree blocks not in refernece counted trees and tree roots
297 * are never shared. If a block was allocated after the last
298 * snapshot and the block was not allocated by tree relocation,
299 * we know the block is not shared.
301 if (root
->ref_cows
&&
302 buf
!= root
->node
&& buf
!= root
->commit_root
&&
303 (btrfs_header_generation(buf
) <=
304 btrfs_root_last_snapshot(&root
->root_item
) ||
305 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)))
307 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
308 if (root
->ref_cows
&&
309 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
315 static noinline
int update_ref_for_cow(struct btrfs_trans_handle
*trans
,
316 struct btrfs_root
*root
,
317 struct extent_buffer
*buf
,
318 struct extent_buffer
*cow
,
328 * Backrefs update rules:
330 * Always use full backrefs for extent pointers in tree block
331 * allocated by tree relocation.
333 * If a shared tree block is no longer referenced by its owner
334 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
335 * use full backrefs for extent pointers in tree block.
337 * If a tree block is been relocating
338 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
339 * use full backrefs for extent pointers in tree block.
340 * The reason for this is some operations (such as drop tree)
341 * are only allowed for blocks use full backrefs.
344 if (btrfs_block_can_be_shared(root
, buf
)) {
345 ret
= btrfs_lookup_extent_info(trans
, root
, buf
->start
,
346 buf
->len
, &refs
, &flags
);
351 btrfs_std_error(root
->fs_info
, ret
);
356 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
||
357 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
358 flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
363 owner
= btrfs_header_owner(buf
);
364 BUG_ON(owner
== BTRFS_TREE_RELOC_OBJECTID
&&
365 !(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
));
368 if ((owner
== root
->root_key
.objectid
||
369 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) &&
370 !(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
)) {
371 ret
= btrfs_inc_ref(trans
, root
, buf
, 1, 1);
372 BUG_ON(ret
); /* -ENOMEM */
374 if (root
->root_key
.objectid
==
375 BTRFS_TREE_RELOC_OBJECTID
) {
376 ret
= btrfs_dec_ref(trans
, root
, buf
, 0, 1);
377 BUG_ON(ret
); /* -ENOMEM */
378 ret
= btrfs_inc_ref(trans
, root
, cow
, 1, 1);
379 BUG_ON(ret
); /* -ENOMEM */
381 new_flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
384 if (root
->root_key
.objectid
==
385 BTRFS_TREE_RELOC_OBJECTID
)
386 ret
= btrfs_inc_ref(trans
, root
, cow
, 1, 1);
388 ret
= btrfs_inc_ref(trans
, root
, cow
, 0, 1);
389 BUG_ON(ret
); /* -ENOMEM */
391 if (new_flags
!= 0) {
392 ret
= btrfs_set_disk_extent_flags(trans
, root
,
400 if (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
401 if (root
->root_key
.objectid
==
402 BTRFS_TREE_RELOC_OBJECTID
)
403 ret
= btrfs_inc_ref(trans
, root
, cow
, 1, 1);
405 ret
= btrfs_inc_ref(trans
, root
, cow
, 0, 1);
406 BUG_ON(ret
); /* -ENOMEM */
407 ret
= btrfs_dec_ref(trans
, root
, buf
, 1, 1);
408 BUG_ON(ret
); /* -ENOMEM */
410 clean_tree_block(trans
, root
, buf
);
417 * does the dirty work in cow of a single block. The parent block (if
418 * supplied) is updated to point to the new cow copy. The new buffer is marked
419 * dirty and returned locked. If you modify the block it needs to be marked
422 * search_start -- an allocation hint for the new block
424 * empty_size -- a hint that you plan on doing more cow. This is the size in
425 * bytes the allocator should try to find free next to the block it returns.
426 * This is just a hint and may be ignored by the allocator.
428 static noinline
int __btrfs_cow_block(struct btrfs_trans_handle
*trans
,
429 struct btrfs_root
*root
,
430 struct extent_buffer
*buf
,
431 struct extent_buffer
*parent
, int parent_slot
,
432 struct extent_buffer
**cow_ret
,
433 u64 search_start
, u64 empty_size
)
435 struct btrfs_disk_key disk_key
;
436 struct extent_buffer
*cow
;
445 btrfs_assert_tree_locked(buf
);
447 WARN_ON(root
->ref_cows
&& trans
->transid
!=
448 root
->fs_info
->running_transaction
->transid
);
449 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
451 level
= btrfs_header_level(buf
);
454 btrfs_item_key(buf
, &disk_key
, 0);
456 btrfs_node_key(buf
, &disk_key
, 0);
458 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
460 parent_start
= parent
->start
;
466 cow
= btrfs_alloc_free_block(trans
, root
, buf
->len
, parent_start
,
467 root
->root_key
.objectid
, &disk_key
,
468 level
, search_start
, empty_size
, 1);
472 /* cow is set to blocking by btrfs_init_new_buffer */
474 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
475 btrfs_set_header_bytenr(cow
, cow
->start
);
476 btrfs_set_header_generation(cow
, trans
->transid
);
477 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
478 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
479 BTRFS_HEADER_FLAG_RELOC
);
480 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
481 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
483 btrfs_set_header_owner(cow
, root
->root_key
.objectid
);
485 write_extent_buffer(cow
, root
->fs_info
->fsid
,
486 (unsigned long)btrfs_header_fsid(cow
),
489 ret
= update_ref_for_cow(trans
, root
, buf
, cow
, &last_ref
);
491 btrfs_abort_transaction(trans
, root
, ret
);
496 btrfs_reloc_cow_block(trans
, root
, buf
, cow
);
498 if (buf
== root
->node
) {
499 WARN_ON(parent
&& parent
!= buf
);
500 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
||
501 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
502 parent_start
= buf
->start
;
506 extent_buffer_get(cow
);
507 rcu_assign_pointer(root
->node
, cow
);
509 btrfs_free_tree_block(trans
, root
, buf
, parent_start
,
511 free_extent_buffer(buf
);
512 add_root_to_dirty_list(root
);
514 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
515 parent_start
= parent
->start
;
519 WARN_ON(trans
->transid
!= btrfs_header_generation(parent
));
520 btrfs_set_node_blockptr(parent
, parent_slot
,
522 btrfs_set_node_ptr_generation(parent
, parent_slot
,
524 btrfs_mark_buffer_dirty(parent
);
525 btrfs_free_tree_block(trans
, root
, buf
, parent_start
,
529 btrfs_tree_unlock(buf
);
530 free_extent_buffer_stale(buf
);
531 btrfs_mark_buffer_dirty(cow
);
536 static inline int should_cow_block(struct btrfs_trans_handle
*trans
,
537 struct btrfs_root
*root
,
538 struct extent_buffer
*buf
)
540 /* ensure we can see the force_cow */
544 * We do not need to cow a block if
545 * 1) this block is not created or changed in this transaction;
546 * 2) this block does not belong to TREE_RELOC tree;
547 * 3) the root is not forced COW.
549 * What is forced COW:
550 * when we create snapshot during commiting the transaction,
551 * after we've finished coping src root, we must COW the shared
552 * block to ensure the metadata consistency.
554 if (btrfs_header_generation(buf
) == trans
->transid
&&
555 !btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
) &&
556 !(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
&&
557 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)) &&
564 * cows a single block, see __btrfs_cow_block for the real work.
565 * This version of it has extra checks so that a block isn't cow'd more than
566 * once per transaction, as long as it hasn't been written yet
568 noinline
int btrfs_cow_block(struct btrfs_trans_handle
*trans
,
569 struct btrfs_root
*root
, struct extent_buffer
*buf
,
570 struct extent_buffer
*parent
, int parent_slot
,
571 struct extent_buffer
**cow_ret
)
576 if (trans
->transaction
!= root
->fs_info
->running_transaction
) {
577 printk(KERN_CRIT
"trans %llu running %llu\n",
578 (unsigned long long)trans
->transid
,
580 root
->fs_info
->running_transaction
->transid
);
583 if (trans
->transid
!= root
->fs_info
->generation
) {
584 printk(KERN_CRIT
"trans %llu running %llu\n",
585 (unsigned long long)trans
->transid
,
586 (unsigned long long)root
->fs_info
->generation
);
590 if (!should_cow_block(trans
, root
, buf
)) {
595 search_start
= buf
->start
& ~((u64
)(1024 * 1024 * 1024) - 1);
598 btrfs_set_lock_blocking(parent
);
599 btrfs_set_lock_blocking(buf
);
601 ret
= __btrfs_cow_block(trans
, root
, buf
, parent
,
602 parent_slot
, cow_ret
, search_start
, 0);
604 trace_btrfs_cow_block(root
, buf
, *cow_ret
);
610 * helper function for defrag to decide if two blocks pointed to by a
611 * node are actually close by
613 static int close_blocks(u64 blocknr
, u64 other
, u32 blocksize
)
615 if (blocknr
< other
&& other
- (blocknr
+ blocksize
) < 32768)
617 if (blocknr
> other
&& blocknr
- (other
+ blocksize
) < 32768)
623 * compare two keys in a memcmp fashion
625 static int comp_keys(struct btrfs_disk_key
*disk
, struct btrfs_key
*k2
)
629 btrfs_disk_key_to_cpu(&k1
, disk
);
631 return btrfs_comp_cpu_keys(&k1
, k2
);
635 * same as comp_keys only with two btrfs_key's
637 int btrfs_comp_cpu_keys(struct btrfs_key
*k1
, struct btrfs_key
*k2
)
639 if (k1
->objectid
> k2
->objectid
)
641 if (k1
->objectid
< k2
->objectid
)
643 if (k1
->type
> k2
->type
)
645 if (k1
->type
< k2
->type
)
647 if (k1
->offset
> k2
->offset
)
649 if (k1
->offset
< k2
->offset
)
655 * this is used by the defrag code to go through all the
656 * leaves pointed to by a node and reallocate them so that
657 * disk order is close to key order
659 int btrfs_realloc_node(struct btrfs_trans_handle
*trans
,
660 struct btrfs_root
*root
, struct extent_buffer
*parent
,
661 int start_slot
, int cache_only
, u64
*last_ret
,
662 struct btrfs_key
*progress
)
664 struct extent_buffer
*cur
;
667 u64 search_start
= *last_ret
;
677 int progress_passed
= 0;
678 struct btrfs_disk_key disk_key
;
680 parent_level
= btrfs_header_level(parent
);
681 if (cache_only
&& parent_level
!= 1)
684 if (trans
->transaction
!= root
->fs_info
->running_transaction
)
686 if (trans
->transid
!= root
->fs_info
->generation
)
689 parent_nritems
= btrfs_header_nritems(parent
);
690 blocksize
= btrfs_level_size(root
, parent_level
- 1);
691 end_slot
= parent_nritems
;
693 if (parent_nritems
== 1)
696 btrfs_set_lock_blocking(parent
);
698 for (i
= start_slot
; i
< end_slot
; i
++) {
701 btrfs_node_key(parent
, &disk_key
, i
);
702 if (!progress_passed
&& comp_keys(&disk_key
, progress
) < 0)
706 blocknr
= btrfs_node_blockptr(parent
, i
);
707 gen
= btrfs_node_ptr_generation(parent
, i
);
709 last_block
= blocknr
;
712 other
= btrfs_node_blockptr(parent
, i
- 1);
713 close
= close_blocks(blocknr
, other
, blocksize
);
715 if (!close
&& i
< end_slot
- 2) {
716 other
= btrfs_node_blockptr(parent
, i
+ 1);
717 close
= close_blocks(blocknr
, other
, blocksize
);
720 last_block
= blocknr
;
724 cur
= btrfs_find_tree_block(root
, blocknr
, blocksize
);
726 uptodate
= btrfs_buffer_uptodate(cur
, gen
);
729 if (!cur
|| !uptodate
) {
731 free_extent_buffer(cur
);
735 cur
= read_tree_block(root
, blocknr
,
739 } else if (!uptodate
) {
740 btrfs_read_buffer(cur
, gen
);
743 if (search_start
== 0)
744 search_start
= last_block
;
746 btrfs_tree_lock(cur
);
747 btrfs_set_lock_blocking(cur
);
748 err
= __btrfs_cow_block(trans
, root
, cur
, parent
, i
,
751 (end_slot
- i
) * blocksize
));
753 btrfs_tree_unlock(cur
);
754 free_extent_buffer(cur
);
757 search_start
= cur
->start
;
758 last_block
= cur
->start
;
759 *last_ret
= search_start
;
760 btrfs_tree_unlock(cur
);
761 free_extent_buffer(cur
);
767 * The leaf data grows from end-to-front in the node.
768 * this returns the address of the start of the last item,
769 * which is the stop of the leaf data stack
771 static inline unsigned int leaf_data_end(struct btrfs_root
*root
,
772 struct extent_buffer
*leaf
)
774 u32 nr
= btrfs_header_nritems(leaf
);
776 return BTRFS_LEAF_DATA_SIZE(root
);
777 return btrfs_item_offset_nr(leaf
, nr
- 1);
782 * search for key in the extent_buffer. The items start at offset p,
783 * and they are item_size apart. There are 'max' items in p.
785 * the slot in the array is returned via slot, and it points to
786 * the place where you would insert key if it is not found in
789 * slot may point to max if the key is bigger than all of the keys
791 static noinline
int generic_bin_search(struct extent_buffer
*eb
,
793 int item_size
, struct btrfs_key
*key
,
800 struct btrfs_disk_key
*tmp
= NULL
;
801 struct btrfs_disk_key unaligned
;
802 unsigned long offset
;
804 unsigned long map_start
= 0;
805 unsigned long map_len
= 0;
809 mid
= (low
+ high
) / 2;
810 offset
= p
+ mid
* item_size
;
812 if (!kaddr
|| offset
< map_start
||
813 (offset
+ sizeof(struct btrfs_disk_key
)) >
814 map_start
+ map_len
) {
816 err
= map_private_extent_buffer(eb
, offset
,
817 sizeof(struct btrfs_disk_key
),
818 &kaddr
, &map_start
, &map_len
);
821 tmp
= (struct btrfs_disk_key
*)(kaddr
+ offset
-
824 read_extent_buffer(eb
, &unaligned
,
825 offset
, sizeof(unaligned
));
830 tmp
= (struct btrfs_disk_key
*)(kaddr
+ offset
-
833 ret
= comp_keys(tmp
, key
);
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 int btrfs_bin_search(struct extent_buffer
*eb
, struct btrfs_key
*key
,
872 int level
, int *slot
)
874 return bin_search(eb
, key
, level
, slot
);
877 static void root_add_used(struct btrfs_root
*root
, u32 size
)
879 spin_lock(&root
->accounting_lock
);
880 btrfs_set_root_used(&root
->root_item
,
881 btrfs_root_used(&root
->root_item
) + size
);
882 spin_unlock(&root
->accounting_lock
);
885 static void root_sub_used(struct btrfs_root
*root
, u32 size
)
887 spin_lock(&root
->accounting_lock
);
888 btrfs_set_root_used(&root
->root_item
,
889 btrfs_root_used(&root
->root_item
) - size
);
890 spin_unlock(&root
->accounting_lock
);
893 /* given a node and slot number, this reads the blocks it points to. The
894 * extent buffer is returned with a reference taken (but unlocked).
895 * NULL is returned on error.
897 static noinline
struct extent_buffer
*read_node_slot(struct btrfs_root
*root
,
898 struct extent_buffer
*parent
, int slot
)
900 int level
= btrfs_header_level(parent
);
903 if (slot
>= btrfs_header_nritems(parent
))
908 return read_tree_block(root
, btrfs_node_blockptr(parent
, slot
),
909 btrfs_level_size(root
, level
- 1),
910 btrfs_node_ptr_generation(parent
, slot
));
914 * node level balancing, used to make sure nodes are in proper order for
915 * item deletion. We balance from the top down, so we have to make sure
916 * that a deletion won't leave an node completely empty later on.
918 static noinline
int balance_level(struct btrfs_trans_handle
*trans
,
919 struct btrfs_root
*root
,
920 struct btrfs_path
*path
, int level
)
922 struct extent_buffer
*right
= NULL
;
923 struct extent_buffer
*mid
;
924 struct extent_buffer
*left
= NULL
;
925 struct extent_buffer
*parent
= NULL
;
929 int orig_slot
= path
->slots
[level
];
935 mid
= path
->nodes
[level
];
937 WARN_ON(path
->locks
[level
] != BTRFS_WRITE_LOCK
&&
938 path
->locks
[level
] != BTRFS_WRITE_LOCK_BLOCKING
);
939 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
941 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
943 if (level
< BTRFS_MAX_LEVEL
- 1) {
944 parent
= path
->nodes
[level
+ 1];
945 pslot
= path
->slots
[level
+ 1];
949 * deal with the case where there is only one pointer in the root
950 * by promoting the node below to a root
953 struct extent_buffer
*child
;
955 if (btrfs_header_nritems(mid
) != 1)
958 /* promote the child to a root */
959 child
= read_node_slot(root
, mid
, 0);
962 btrfs_std_error(root
->fs_info
, ret
);
966 btrfs_tree_lock(child
);
967 btrfs_set_lock_blocking(child
);
968 ret
= btrfs_cow_block(trans
, root
, child
, mid
, 0, &child
);
970 btrfs_tree_unlock(child
);
971 free_extent_buffer(child
);
975 rcu_assign_pointer(root
->node
, child
);
977 add_root_to_dirty_list(root
);
978 btrfs_tree_unlock(child
);
980 path
->locks
[level
] = 0;
981 path
->nodes
[level
] = NULL
;
982 clean_tree_block(trans
, root
, mid
);
983 btrfs_tree_unlock(mid
);
984 /* once for the path */
985 free_extent_buffer(mid
);
987 root_sub_used(root
, mid
->len
);
988 btrfs_free_tree_block(trans
, root
, mid
, 0, 1, 0);
989 /* once for the root ptr */
990 free_extent_buffer_stale(mid
);
993 if (btrfs_header_nritems(mid
) >
994 BTRFS_NODEPTRS_PER_BLOCK(root
) / 4)
997 btrfs_header_nritems(mid
);
999 left
= read_node_slot(root
, parent
, pslot
- 1);
1001 btrfs_tree_lock(left
);
1002 btrfs_set_lock_blocking(left
);
1003 wret
= btrfs_cow_block(trans
, root
, left
,
1004 parent
, pslot
- 1, &left
);
1010 right
= read_node_slot(root
, parent
, pslot
+ 1);
1012 btrfs_tree_lock(right
);
1013 btrfs_set_lock_blocking(right
);
1014 wret
= btrfs_cow_block(trans
, root
, right
,
1015 parent
, pslot
+ 1, &right
);
1022 /* first, try to make some room in the middle buffer */
1024 orig_slot
+= btrfs_header_nritems(left
);
1025 wret
= push_node_left(trans
, root
, left
, mid
, 1);
1028 btrfs_header_nritems(mid
);
1032 * then try to empty the right most buffer into the middle
1035 wret
= push_node_left(trans
, root
, mid
, right
, 1);
1036 if (wret
< 0 && wret
!= -ENOSPC
)
1038 if (btrfs_header_nritems(right
) == 0) {
1039 clean_tree_block(trans
, root
, right
);
1040 btrfs_tree_unlock(right
);
1041 del_ptr(trans
, root
, path
, level
+ 1, pslot
+ 1);
1042 root_sub_used(root
, right
->len
);
1043 btrfs_free_tree_block(trans
, root
, right
, 0, 1, 0);
1044 free_extent_buffer_stale(right
);
1047 struct btrfs_disk_key right_key
;
1048 btrfs_node_key(right
, &right_key
, 0);
1049 btrfs_set_node_key(parent
, &right_key
, pslot
+ 1);
1050 btrfs_mark_buffer_dirty(parent
);
1053 if (btrfs_header_nritems(mid
) == 1) {
1055 * we're not allowed to leave a node with one item in the
1056 * tree during a delete. A deletion from lower in the tree
1057 * could try to delete the only pointer in this node.
1058 * So, pull some keys from the left.
1059 * There has to be a left pointer at this point because
1060 * otherwise we would have pulled some pointers from the
1065 btrfs_std_error(root
->fs_info
, ret
);
1068 wret
= balance_node_right(trans
, root
, mid
, left
);
1074 wret
= push_node_left(trans
, root
, left
, mid
, 1);
1080 if (btrfs_header_nritems(mid
) == 0) {
1081 clean_tree_block(trans
, root
, mid
);
1082 btrfs_tree_unlock(mid
);
1083 del_ptr(trans
, root
, path
, level
+ 1, pslot
);
1084 root_sub_used(root
, mid
->len
);
1085 btrfs_free_tree_block(trans
, root
, mid
, 0, 1, 0);
1086 free_extent_buffer_stale(mid
);
1089 /* update the parent key to reflect our changes */
1090 struct btrfs_disk_key mid_key
;
1091 btrfs_node_key(mid
, &mid_key
, 0);
1092 btrfs_set_node_key(parent
, &mid_key
, pslot
);
1093 btrfs_mark_buffer_dirty(parent
);
1096 /* update the path */
1098 if (btrfs_header_nritems(left
) > orig_slot
) {
1099 extent_buffer_get(left
);
1100 /* left was locked after cow */
1101 path
->nodes
[level
] = left
;
1102 path
->slots
[level
+ 1] -= 1;
1103 path
->slots
[level
] = orig_slot
;
1105 btrfs_tree_unlock(mid
);
1106 free_extent_buffer(mid
);
1109 orig_slot
-= btrfs_header_nritems(left
);
1110 path
->slots
[level
] = orig_slot
;
1113 /* double check we haven't messed things up */
1115 btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]))
1119 btrfs_tree_unlock(right
);
1120 free_extent_buffer(right
);
1123 if (path
->nodes
[level
] != left
)
1124 btrfs_tree_unlock(left
);
1125 free_extent_buffer(left
);
1130 /* Node balancing for insertion. Here we only split or push nodes around
1131 * when they are completely full. This is also done top down, so we
1132 * have to be pessimistic.
1134 static noinline
int push_nodes_for_insert(struct btrfs_trans_handle
*trans
,
1135 struct btrfs_root
*root
,
1136 struct btrfs_path
*path
, int level
)
1138 struct extent_buffer
*right
= NULL
;
1139 struct extent_buffer
*mid
;
1140 struct extent_buffer
*left
= NULL
;
1141 struct extent_buffer
*parent
= NULL
;
1145 int orig_slot
= path
->slots
[level
];
1150 mid
= path
->nodes
[level
];
1151 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
1153 if (level
< BTRFS_MAX_LEVEL
- 1) {
1154 parent
= path
->nodes
[level
+ 1];
1155 pslot
= path
->slots
[level
+ 1];
1161 left
= read_node_slot(root
, parent
, pslot
- 1);
1163 /* first, try to make some room in the middle buffer */
1167 btrfs_tree_lock(left
);
1168 btrfs_set_lock_blocking(left
);
1170 left_nr
= btrfs_header_nritems(left
);
1171 if (left_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
1174 ret
= btrfs_cow_block(trans
, root
, left
, parent
,
1179 wret
= push_node_left(trans
, root
,
1186 struct btrfs_disk_key disk_key
;
1187 orig_slot
+= left_nr
;
1188 btrfs_node_key(mid
, &disk_key
, 0);
1189 btrfs_set_node_key(parent
, &disk_key
, pslot
);
1190 btrfs_mark_buffer_dirty(parent
);
1191 if (btrfs_header_nritems(left
) > orig_slot
) {
1192 path
->nodes
[level
] = left
;
1193 path
->slots
[level
+ 1] -= 1;
1194 path
->slots
[level
] = orig_slot
;
1195 btrfs_tree_unlock(mid
);
1196 free_extent_buffer(mid
);
1199 btrfs_header_nritems(left
);
1200 path
->slots
[level
] = orig_slot
;
1201 btrfs_tree_unlock(left
);
1202 free_extent_buffer(left
);
1206 btrfs_tree_unlock(left
);
1207 free_extent_buffer(left
);
1209 right
= read_node_slot(root
, parent
, pslot
+ 1);
1212 * then try to empty the right most buffer into the middle
1217 btrfs_tree_lock(right
);
1218 btrfs_set_lock_blocking(right
);
1220 right_nr
= btrfs_header_nritems(right
);
1221 if (right_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
1224 ret
= btrfs_cow_block(trans
, root
, right
,
1230 wret
= balance_node_right(trans
, root
,
1237 struct btrfs_disk_key disk_key
;
1239 btrfs_node_key(right
, &disk_key
, 0);
1240 btrfs_set_node_key(parent
, &disk_key
, pslot
+ 1);
1241 btrfs_mark_buffer_dirty(parent
);
1243 if (btrfs_header_nritems(mid
) <= orig_slot
) {
1244 path
->nodes
[level
] = right
;
1245 path
->slots
[level
+ 1] += 1;
1246 path
->slots
[level
] = orig_slot
-
1247 btrfs_header_nritems(mid
);
1248 btrfs_tree_unlock(mid
);
1249 free_extent_buffer(mid
);
1251 btrfs_tree_unlock(right
);
1252 free_extent_buffer(right
);
1256 btrfs_tree_unlock(right
);
1257 free_extent_buffer(right
);
1263 * readahead one full node of leaves, finding things that are close
1264 * to the block in 'slot', and triggering ra on them.
1266 static void reada_for_search(struct btrfs_root
*root
,
1267 struct btrfs_path
*path
,
1268 int level
, int slot
, u64 objectid
)
1270 struct extent_buffer
*node
;
1271 struct btrfs_disk_key disk_key
;
1277 int direction
= path
->reada
;
1278 struct extent_buffer
*eb
;
1286 if (!path
->nodes
[level
])
1289 node
= path
->nodes
[level
];
1291 search
= btrfs_node_blockptr(node
, slot
);
1292 blocksize
= btrfs_level_size(root
, level
- 1);
1293 eb
= btrfs_find_tree_block(root
, search
, blocksize
);
1295 free_extent_buffer(eb
);
1301 nritems
= btrfs_header_nritems(node
);
1305 if (direction
< 0) {
1309 } else if (direction
> 0) {
1314 if (path
->reada
< 0 && objectid
) {
1315 btrfs_node_key(node
, &disk_key
, nr
);
1316 if (btrfs_disk_key_objectid(&disk_key
) != objectid
)
1319 search
= btrfs_node_blockptr(node
, nr
);
1320 if ((search
<= target
&& target
- search
<= 65536) ||
1321 (search
> target
&& search
- target
<= 65536)) {
1322 gen
= btrfs_node_ptr_generation(node
, nr
);
1323 readahead_tree_block(root
, search
, blocksize
, gen
);
1327 if ((nread
> 65536 || nscan
> 32))
1333 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1336 static noinline
int reada_for_balance(struct btrfs_root
*root
,
1337 struct btrfs_path
*path
, int level
)
1341 struct extent_buffer
*parent
;
1342 struct extent_buffer
*eb
;
1349 parent
= path
->nodes
[level
+ 1];
1353 nritems
= btrfs_header_nritems(parent
);
1354 slot
= path
->slots
[level
+ 1];
1355 blocksize
= btrfs_level_size(root
, level
);
1358 block1
= btrfs_node_blockptr(parent
, slot
- 1);
1359 gen
= btrfs_node_ptr_generation(parent
, slot
- 1);
1360 eb
= btrfs_find_tree_block(root
, block1
, blocksize
);
1361 if (eb
&& btrfs_buffer_uptodate(eb
, gen
))
1363 free_extent_buffer(eb
);
1365 if (slot
+ 1 < nritems
) {
1366 block2
= btrfs_node_blockptr(parent
, slot
+ 1);
1367 gen
= btrfs_node_ptr_generation(parent
, slot
+ 1);
1368 eb
= btrfs_find_tree_block(root
, block2
, blocksize
);
1369 if (eb
&& btrfs_buffer_uptodate(eb
, gen
))
1371 free_extent_buffer(eb
);
1373 if (block1
|| block2
) {
1376 /* release the whole path */
1377 btrfs_release_path(path
);
1379 /* read the blocks */
1381 readahead_tree_block(root
, block1
, blocksize
, 0);
1383 readahead_tree_block(root
, block2
, blocksize
, 0);
1386 eb
= read_tree_block(root
, block1
, blocksize
, 0);
1387 free_extent_buffer(eb
);
1390 eb
= read_tree_block(root
, block2
, blocksize
, 0);
1391 free_extent_buffer(eb
);
1399 * when we walk down the tree, it is usually safe to unlock the higher layers
1400 * in the tree. The exceptions are when our path goes through slot 0, because
1401 * operations on the tree might require changing key pointers higher up in the
1404 * callers might also have set path->keep_locks, which tells this code to keep
1405 * the lock if the path points to the last slot in the block. This is part of
1406 * walking through the tree, and selecting the next slot in the higher block.
1408 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1409 * if lowest_unlock is 1, level 0 won't be unlocked
1411 static noinline
void unlock_up(struct btrfs_path
*path
, int level
,
1412 int lowest_unlock
, int min_write_lock_level
,
1413 int *write_lock_level
)
1416 int skip_level
= level
;
1418 struct extent_buffer
*t
;
1420 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1421 if (!path
->nodes
[i
])
1423 if (!path
->locks
[i
])
1425 if (!no_skips
&& path
->slots
[i
] == 0) {
1429 if (!no_skips
&& path
->keep_locks
) {
1432 nritems
= btrfs_header_nritems(t
);
1433 if (nritems
< 1 || path
->slots
[i
] >= nritems
- 1) {
1438 if (skip_level
< i
&& i
>= lowest_unlock
)
1442 if (i
>= lowest_unlock
&& i
> skip_level
&& path
->locks
[i
]) {
1443 btrfs_tree_unlock_rw(t
, path
->locks
[i
]);
1445 if (write_lock_level
&&
1446 i
> min_write_lock_level
&&
1447 i
<= *write_lock_level
) {
1448 *write_lock_level
= i
- 1;
1455 * This releases any locks held in the path starting at level and
1456 * going all the way up to the root.
1458 * btrfs_search_slot will keep the lock held on higher nodes in a few
1459 * corner cases, such as COW of the block at slot zero in the node. This
1460 * ignores those rules, and it should only be called when there are no
1461 * more updates to be done higher up in the tree.
1463 noinline
void btrfs_unlock_up_safe(struct btrfs_path
*path
, int level
)
1467 if (path
->keep_locks
)
1470 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1471 if (!path
->nodes
[i
])
1473 if (!path
->locks
[i
])
1475 btrfs_tree_unlock_rw(path
->nodes
[i
], path
->locks
[i
]);
1481 * helper function for btrfs_search_slot. The goal is to find a block
1482 * in cache without setting the path to blocking. If we find the block
1483 * we return zero and the path is unchanged.
1485 * If we can't find the block, we set the path blocking and do some
1486 * reada. -EAGAIN is returned and the search must be repeated.
1489 read_block_for_search(struct btrfs_trans_handle
*trans
,
1490 struct btrfs_root
*root
, struct btrfs_path
*p
,
1491 struct extent_buffer
**eb_ret
, int level
, int slot
,
1492 struct btrfs_key
*key
)
1497 struct extent_buffer
*b
= *eb_ret
;
1498 struct extent_buffer
*tmp
;
1501 blocknr
= btrfs_node_blockptr(b
, slot
);
1502 gen
= btrfs_node_ptr_generation(b
, slot
);
1503 blocksize
= btrfs_level_size(root
, level
- 1);
1505 tmp
= btrfs_find_tree_block(root
, blocknr
, blocksize
);
1507 if (btrfs_buffer_uptodate(tmp
, 0)) {
1508 if (btrfs_buffer_uptodate(tmp
, gen
)) {
1510 * we found an up to date block without
1517 /* the pages were up to date, but we failed
1518 * the generation number check. Do a full
1519 * read for the generation number that is correct.
1520 * We must do this without dropping locks so
1521 * we can trust our generation number
1523 free_extent_buffer(tmp
);
1524 btrfs_set_path_blocking(p
);
1526 tmp
= read_tree_block(root
, blocknr
, blocksize
, gen
);
1527 if (tmp
&& btrfs_buffer_uptodate(tmp
, gen
)) {
1531 free_extent_buffer(tmp
);
1532 btrfs_release_path(p
);
1538 * reduce lock contention at high levels
1539 * of the btree by dropping locks before
1540 * we read. Don't release the lock on the current
1541 * level because we need to walk this node to figure
1542 * out which blocks to read.
1544 btrfs_unlock_up_safe(p
, level
+ 1);
1545 btrfs_set_path_blocking(p
);
1547 free_extent_buffer(tmp
);
1549 reada_for_search(root
, p
, level
, slot
, key
->objectid
);
1551 btrfs_release_path(p
);
1554 tmp
= read_tree_block(root
, blocknr
, blocksize
, 0);
1557 * If the read above didn't mark this buffer up to date,
1558 * it will never end up being up to date. Set ret to EIO now
1559 * and give up so that our caller doesn't loop forever
1562 if (!btrfs_buffer_uptodate(tmp
, 0))
1564 free_extent_buffer(tmp
);
1570 * helper function for btrfs_search_slot. This does all of the checks
1571 * for node-level blocks and does any balancing required based on
1574 * If no extra work was required, zero is returned. If we had to
1575 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1579 setup_nodes_for_search(struct btrfs_trans_handle
*trans
,
1580 struct btrfs_root
*root
, struct btrfs_path
*p
,
1581 struct extent_buffer
*b
, int level
, int ins_len
,
1582 int *write_lock_level
)
1585 if ((p
->search_for_split
|| ins_len
> 0) && btrfs_header_nritems(b
) >=
1586 BTRFS_NODEPTRS_PER_BLOCK(root
) - 3) {
1589 if (*write_lock_level
< level
+ 1) {
1590 *write_lock_level
= level
+ 1;
1591 btrfs_release_path(p
);
1595 sret
= reada_for_balance(root
, p
, level
);
1599 btrfs_set_path_blocking(p
);
1600 sret
= split_node(trans
, root
, p
, level
);
1601 btrfs_clear_path_blocking(p
, NULL
, 0);
1608 b
= p
->nodes
[level
];
1609 } else if (ins_len
< 0 && btrfs_header_nritems(b
) <
1610 BTRFS_NODEPTRS_PER_BLOCK(root
) / 2) {
1613 if (*write_lock_level
< level
+ 1) {
1614 *write_lock_level
= level
+ 1;
1615 btrfs_release_path(p
);
1619 sret
= reada_for_balance(root
, p
, level
);
1623 btrfs_set_path_blocking(p
);
1624 sret
= balance_level(trans
, root
, p
, level
);
1625 btrfs_clear_path_blocking(p
, NULL
, 0);
1631 b
= p
->nodes
[level
];
1633 btrfs_release_path(p
);
1636 BUG_ON(btrfs_header_nritems(b
) == 1);
1647 * look for key in the tree. path is filled in with nodes along the way
1648 * if key is found, we return zero and you can find the item in the leaf
1649 * level of the path (level 0)
1651 * If the key isn't found, the path points to the slot where it should
1652 * be inserted, and 1 is returned. If there are other errors during the
1653 * search a negative error number is returned.
1655 * if ins_len > 0, nodes and leaves will be split as we walk down the
1656 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1659 int btrfs_search_slot(struct btrfs_trans_handle
*trans
, struct btrfs_root
1660 *root
, struct btrfs_key
*key
, struct btrfs_path
*p
, int
1663 struct extent_buffer
*b
;
1668 int lowest_unlock
= 1;
1670 /* everything at write_lock_level or lower must be write locked */
1671 int write_lock_level
= 0;
1672 u8 lowest_level
= 0;
1673 int min_write_lock_level
;
1675 lowest_level
= p
->lowest_level
;
1676 WARN_ON(lowest_level
&& ins_len
> 0);
1677 WARN_ON(p
->nodes
[0] != NULL
);
1682 /* when we are removing items, we might have to go up to level
1683 * two as we update tree pointers Make sure we keep write
1684 * for those levels as well
1686 write_lock_level
= 2;
1687 } else if (ins_len
> 0) {
1689 * for inserting items, make sure we have a write lock on
1690 * level 1 so we can update keys
1692 write_lock_level
= 1;
1696 write_lock_level
= -1;
1698 if (cow
&& (p
->keep_locks
|| p
->lowest_level
))
1699 write_lock_level
= BTRFS_MAX_LEVEL
;
1701 min_write_lock_level
= write_lock_level
;
1705 * we try very hard to do read locks on the root
1707 root_lock
= BTRFS_READ_LOCK
;
1709 if (p
->search_commit_root
) {
1711 * the commit roots are read only
1712 * so we always do read locks
1714 b
= root
->commit_root
;
1715 extent_buffer_get(b
);
1716 level
= btrfs_header_level(b
);
1717 if (!p
->skip_locking
)
1718 btrfs_tree_read_lock(b
);
1720 if (p
->skip_locking
) {
1721 b
= btrfs_root_node(root
);
1722 level
= btrfs_header_level(b
);
1724 /* we don't know the level of the root node
1725 * until we actually have it read locked
1727 b
= btrfs_read_lock_root_node(root
);
1728 level
= btrfs_header_level(b
);
1729 if (level
<= write_lock_level
) {
1730 /* whoops, must trade for write lock */
1731 btrfs_tree_read_unlock(b
);
1732 free_extent_buffer(b
);
1733 b
= btrfs_lock_root_node(root
);
1734 root_lock
= BTRFS_WRITE_LOCK
;
1736 /* the level might have changed, check again */
1737 level
= btrfs_header_level(b
);
1741 p
->nodes
[level
] = b
;
1742 if (!p
->skip_locking
)
1743 p
->locks
[level
] = root_lock
;
1746 level
= btrfs_header_level(b
);
1749 * setup the path here so we can release it under lock
1750 * contention with the cow code
1754 * if we don't really need to cow this block
1755 * then we don't want to set the path blocking,
1756 * so we test it here
1758 if (!should_cow_block(trans
, root
, b
))
1761 btrfs_set_path_blocking(p
);
1764 * must have write locks on this node and the
1767 if (level
+ 1 > write_lock_level
) {
1768 write_lock_level
= level
+ 1;
1769 btrfs_release_path(p
);
1773 err
= btrfs_cow_block(trans
, root
, b
,
1774 p
->nodes
[level
+ 1],
1775 p
->slots
[level
+ 1], &b
);
1782 BUG_ON(!cow
&& ins_len
);
1784 p
->nodes
[level
] = b
;
1785 btrfs_clear_path_blocking(p
, NULL
, 0);
1788 * we have a lock on b and as long as we aren't changing
1789 * the tree, there is no way to for the items in b to change.
1790 * It is safe to drop the lock on our parent before we
1791 * go through the expensive btree search on b.
1793 * If cow is true, then we might be changing slot zero,
1794 * which may require changing the parent. So, we can't
1795 * drop the lock until after we know which slot we're
1799 btrfs_unlock_up_safe(p
, level
+ 1);
1801 ret
= bin_search(b
, key
, level
, &slot
);
1805 if (ret
&& slot
> 0) {
1809 p
->slots
[level
] = slot
;
1810 err
= setup_nodes_for_search(trans
, root
, p
, b
, level
,
1811 ins_len
, &write_lock_level
);
1818 b
= p
->nodes
[level
];
1819 slot
= p
->slots
[level
];
1822 * slot 0 is special, if we change the key
1823 * we have to update the parent pointer
1824 * which means we must have a write lock
1827 if (slot
== 0 && cow
&&
1828 write_lock_level
< level
+ 1) {
1829 write_lock_level
= level
+ 1;
1830 btrfs_release_path(p
);
1834 unlock_up(p
, level
, lowest_unlock
,
1835 min_write_lock_level
, &write_lock_level
);
1837 if (level
== lowest_level
) {
1843 err
= read_block_for_search(trans
, root
, p
,
1844 &b
, level
, slot
, key
);
1852 if (!p
->skip_locking
) {
1853 level
= btrfs_header_level(b
);
1854 if (level
<= write_lock_level
) {
1855 err
= btrfs_try_tree_write_lock(b
);
1857 btrfs_set_path_blocking(p
);
1859 btrfs_clear_path_blocking(p
, b
,
1862 p
->locks
[level
] = BTRFS_WRITE_LOCK
;
1864 err
= btrfs_try_tree_read_lock(b
);
1866 btrfs_set_path_blocking(p
);
1867 btrfs_tree_read_lock(b
);
1868 btrfs_clear_path_blocking(p
, b
,
1871 p
->locks
[level
] = BTRFS_READ_LOCK
;
1873 p
->nodes
[level
] = b
;
1876 p
->slots
[level
] = slot
;
1878 btrfs_leaf_free_space(root
, b
) < ins_len
) {
1879 if (write_lock_level
< 1) {
1880 write_lock_level
= 1;
1881 btrfs_release_path(p
);
1885 btrfs_set_path_blocking(p
);
1886 err
= split_leaf(trans
, root
, key
,
1887 p
, ins_len
, ret
== 0);
1888 btrfs_clear_path_blocking(p
, NULL
, 0);
1896 if (!p
->search_for_split
)
1897 unlock_up(p
, level
, lowest_unlock
,
1898 min_write_lock_level
, &write_lock_level
);
1905 * we don't really know what they plan on doing with the path
1906 * from here on, so for now just mark it as blocking
1908 if (!p
->leave_spinning
)
1909 btrfs_set_path_blocking(p
);
1911 btrfs_release_path(p
);
1916 * adjust the pointers going up the tree, starting at level
1917 * making sure the right key of each node is points to 'key'.
1918 * This is used after shifting pointers to the left, so it stops
1919 * fixing up pointers when a given leaf/node is not in slot 0 of the
1923 static void fixup_low_keys(struct btrfs_trans_handle
*trans
,
1924 struct btrfs_root
*root
, struct btrfs_path
*path
,
1925 struct btrfs_disk_key
*key
, int level
)
1928 struct extent_buffer
*t
;
1930 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1931 int tslot
= path
->slots
[i
];
1932 if (!path
->nodes
[i
])
1935 btrfs_set_node_key(t
, key
, tslot
);
1936 btrfs_mark_buffer_dirty(path
->nodes
[i
]);
1945 * This function isn't completely safe. It's the caller's responsibility
1946 * that the new key won't break the order
1948 void btrfs_set_item_key_safe(struct btrfs_trans_handle
*trans
,
1949 struct btrfs_root
*root
, struct btrfs_path
*path
,
1950 struct btrfs_key
*new_key
)
1952 struct btrfs_disk_key disk_key
;
1953 struct extent_buffer
*eb
;
1956 eb
= path
->nodes
[0];
1957 slot
= path
->slots
[0];
1959 btrfs_item_key(eb
, &disk_key
, slot
- 1);
1960 BUG_ON(comp_keys(&disk_key
, new_key
) >= 0);
1962 if (slot
< btrfs_header_nritems(eb
) - 1) {
1963 btrfs_item_key(eb
, &disk_key
, slot
+ 1);
1964 BUG_ON(comp_keys(&disk_key
, new_key
) <= 0);
1967 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
1968 btrfs_set_item_key(eb
, &disk_key
, slot
);
1969 btrfs_mark_buffer_dirty(eb
);
1971 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
1975 * try to push data from one node into the next node left in the
1978 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1979 * error, and > 0 if there was no room in the left hand block.
1981 static int push_node_left(struct btrfs_trans_handle
*trans
,
1982 struct btrfs_root
*root
, struct extent_buffer
*dst
,
1983 struct extent_buffer
*src
, int empty
)
1990 src_nritems
= btrfs_header_nritems(src
);
1991 dst_nritems
= btrfs_header_nritems(dst
);
1992 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1993 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1994 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1996 if (!empty
&& src_nritems
<= 8)
1999 if (push_items
<= 0)
2003 push_items
= min(src_nritems
, push_items
);
2004 if (push_items
< src_nritems
) {
2005 /* leave at least 8 pointers in the node if
2006 * we aren't going to empty it
2008 if (src_nritems
- push_items
< 8) {
2009 if (push_items
<= 8)
2015 push_items
= min(src_nritems
- 8, push_items
);
2017 copy_extent_buffer(dst
, src
,
2018 btrfs_node_key_ptr_offset(dst_nritems
),
2019 btrfs_node_key_ptr_offset(0),
2020 push_items
* sizeof(struct btrfs_key_ptr
));
2022 if (push_items
< src_nritems
) {
2023 memmove_extent_buffer(src
, btrfs_node_key_ptr_offset(0),
2024 btrfs_node_key_ptr_offset(push_items
),
2025 (src_nritems
- push_items
) *
2026 sizeof(struct btrfs_key_ptr
));
2028 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
2029 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
2030 btrfs_mark_buffer_dirty(src
);
2031 btrfs_mark_buffer_dirty(dst
);
2037 * try to push data from one node into the next node right in the
2040 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2041 * error, and > 0 if there was no room in the right hand block.
2043 * this will only push up to 1/2 the contents of the left node over
2045 static int balance_node_right(struct btrfs_trans_handle
*trans
,
2046 struct btrfs_root
*root
,
2047 struct extent_buffer
*dst
,
2048 struct extent_buffer
*src
)
2056 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
2057 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
2059 src_nritems
= btrfs_header_nritems(src
);
2060 dst_nritems
= btrfs_header_nritems(dst
);
2061 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
2062 if (push_items
<= 0)
2065 if (src_nritems
< 4)
2068 max_push
= src_nritems
/ 2 + 1;
2069 /* don't try to empty the node */
2070 if (max_push
>= src_nritems
)
2073 if (max_push
< push_items
)
2074 push_items
= max_push
;
2076 memmove_extent_buffer(dst
, btrfs_node_key_ptr_offset(push_items
),
2077 btrfs_node_key_ptr_offset(0),
2079 sizeof(struct btrfs_key_ptr
));
2081 copy_extent_buffer(dst
, src
,
2082 btrfs_node_key_ptr_offset(0),
2083 btrfs_node_key_ptr_offset(src_nritems
- push_items
),
2084 push_items
* sizeof(struct btrfs_key_ptr
));
2086 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
2087 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
2089 btrfs_mark_buffer_dirty(src
);
2090 btrfs_mark_buffer_dirty(dst
);
2096 * helper function to insert a new root level in the tree.
2097 * A new node is allocated, and a single item is inserted to
2098 * point to the existing root
2100 * returns zero on success or < 0 on failure.
2102 static noinline
int insert_new_root(struct btrfs_trans_handle
*trans
,
2103 struct btrfs_root
*root
,
2104 struct btrfs_path
*path
, int level
)
2107 struct extent_buffer
*lower
;
2108 struct extent_buffer
*c
;
2109 struct extent_buffer
*old
;
2110 struct btrfs_disk_key lower_key
;
2112 BUG_ON(path
->nodes
[level
]);
2113 BUG_ON(path
->nodes
[level
-1] != root
->node
);
2115 lower
= path
->nodes
[level
-1];
2117 btrfs_item_key(lower
, &lower_key
, 0);
2119 btrfs_node_key(lower
, &lower_key
, 0);
2121 c
= btrfs_alloc_free_block(trans
, root
, root
->nodesize
, 0,
2122 root
->root_key
.objectid
, &lower_key
,
2123 level
, root
->node
->start
, 0, 0);
2127 root_add_used(root
, root
->nodesize
);
2129 memset_extent_buffer(c
, 0, 0, sizeof(struct btrfs_header
));
2130 btrfs_set_header_nritems(c
, 1);
2131 btrfs_set_header_level(c
, level
);
2132 btrfs_set_header_bytenr(c
, c
->start
);
2133 btrfs_set_header_generation(c
, trans
->transid
);
2134 btrfs_set_header_backref_rev(c
, BTRFS_MIXED_BACKREF_REV
);
2135 btrfs_set_header_owner(c
, root
->root_key
.objectid
);
2137 write_extent_buffer(c
, root
->fs_info
->fsid
,
2138 (unsigned long)btrfs_header_fsid(c
),
2141 write_extent_buffer(c
, root
->fs_info
->chunk_tree_uuid
,
2142 (unsigned long)btrfs_header_chunk_tree_uuid(c
),
2145 btrfs_set_node_key(c
, &lower_key
, 0);
2146 btrfs_set_node_blockptr(c
, 0, lower
->start
);
2147 lower_gen
= btrfs_header_generation(lower
);
2148 WARN_ON(lower_gen
!= trans
->transid
);
2150 btrfs_set_node_ptr_generation(c
, 0, lower_gen
);
2152 btrfs_mark_buffer_dirty(c
);
2155 rcu_assign_pointer(root
->node
, c
);
2157 /* the super has an extra ref to root->node */
2158 free_extent_buffer(old
);
2160 add_root_to_dirty_list(root
);
2161 extent_buffer_get(c
);
2162 path
->nodes
[level
] = c
;
2163 path
->locks
[level
] = BTRFS_WRITE_LOCK
;
2164 path
->slots
[level
] = 0;
2169 * worker function to insert a single pointer in a node.
2170 * the node should have enough room for the pointer already
2172 * slot and level indicate where you want the key to go, and
2173 * blocknr is the block the key points to.
2175 static void insert_ptr(struct btrfs_trans_handle
*trans
,
2176 struct btrfs_root
*root
, struct btrfs_path
*path
,
2177 struct btrfs_disk_key
*key
, u64 bytenr
,
2178 int slot
, int level
)
2180 struct extent_buffer
*lower
;
2183 BUG_ON(!path
->nodes
[level
]);
2184 btrfs_assert_tree_locked(path
->nodes
[level
]);
2185 lower
= path
->nodes
[level
];
2186 nritems
= btrfs_header_nritems(lower
);
2187 BUG_ON(slot
> nritems
);
2188 BUG_ON(nritems
== BTRFS_NODEPTRS_PER_BLOCK(root
));
2189 if (slot
!= nritems
) {
2190 memmove_extent_buffer(lower
,
2191 btrfs_node_key_ptr_offset(slot
+ 1),
2192 btrfs_node_key_ptr_offset(slot
),
2193 (nritems
- slot
) * sizeof(struct btrfs_key_ptr
));
2195 btrfs_set_node_key(lower
, key
, slot
);
2196 btrfs_set_node_blockptr(lower
, slot
, bytenr
);
2197 WARN_ON(trans
->transid
== 0);
2198 btrfs_set_node_ptr_generation(lower
, slot
, trans
->transid
);
2199 btrfs_set_header_nritems(lower
, nritems
+ 1);
2200 btrfs_mark_buffer_dirty(lower
);
2204 * split the node at the specified level in path in two.
2205 * The path is corrected to point to the appropriate node after the split
2207 * Before splitting this tries to make some room in the node by pushing
2208 * left and right, if either one works, it returns right away.
2210 * returns 0 on success and < 0 on failure
2212 static noinline
int split_node(struct btrfs_trans_handle
*trans
,
2213 struct btrfs_root
*root
,
2214 struct btrfs_path
*path
, int level
)
2216 struct extent_buffer
*c
;
2217 struct extent_buffer
*split
;
2218 struct btrfs_disk_key disk_key
;
2223 c
= path
->nodes
[level
];
2224 WARN_ON(btrfs_header_generation(c
) != trans
->transid
);
2225 if (c
== root
->node
) {
2226 /* trying to split the root, lets make a new one */
2227 ret
= insert_new_root(trans
, root
, path
, level
+ 1);
2231 ret
= push_nodes_for_insert(trans
, root
, path
, level
);
2232 c
= path
->nodes
[level
];
2233 if (!ret
&& btrfs_header_nritems(c
) <
2234 BTRFS_NODEPTRS_PER_BLOCK(root
) - 3)
2240 c_nritems
= btrfs_header_nritems(c
);
2241 mid
= (c_nritems
+ 1) / 2;
2242 btrfs_node_key(c
, &disk_key
, mid
);
2244 split
= btrfs_alloc_free_block(trans
, root
, root
->nodesize
, 0,
2245 root
->root_key
.objectid
,
2246 &disk_key
, level
, c
->start
, 0, 0);
2248 return PTR_ERR(split
);
2250 root_add_used(root
, root
->nodesize
);
2252 memset_extent_buffer(split
, 0, 0, sizeof(struct btrfs_header
));
2253 btrfs_set_header_level(split
, btrfs_header_level(c
));
2254 btrfs_set_header_bytenr(split
, split
->start
);
2255 btrfs_set_header_generation(split
, trans
->transid
);
2256 btrfs_set_header_backref_rev(split
, BTRFS_MIXED_BACKREF_REV
);
2257 btrfs_set_header_owner(split
, root
->root_key
.objectid
);
2258 write_extent_buffer(split
, root
->fs_info
->fsid
,
2259 (unsigned long)btrfs_header_fsid(split
),
2261 write_extent_buffer(split
, root
->fs_info
->chunk_tree_uuid
,
2262 (unsigned long)btrfs_header_chunk_tree_uuid(split
),
2266 copy_extent_buffer(split
, c
,
2267 btrfs_node_key_ptr_offset(0),
2268 btrfs_node_key_ptr_offset(mid
),
2269 (c_nritems
- mid
) * sizeof(struct btrfs_key_ptr
));
2270 btrfs_set_header_nritems(split
, c_nritems
- mid
);
2271 btrfs_set_header_nritems(c
, mid
);
2274 btrfs_mark_buffer_dirty(c
);
2275 btrfs_mark_buffer_dirty(split
);
2277 insert_ptr(trans
, root
, path
, &disk_key
, split
->start
,
2278 path
->slots
[level
+ 1] + 1, level
+ 1);
2280 if (path
->slots
[level
] >= mid
) {
2281 path
->slots
[level
] -= mid
;
2282 btrfs_tree_unlock(c
);
2283 free_extent_buffer(c
);
2284 path
->nodes
[level
] = split
;
2285 path
->slots
[level
+ 1] += 1;
2287 btrfs_tree_unlock(split
);
2288 free_extent_buffer(split
);
2294 * how many bytes are required to store the items in a leaf. start
2295 * and nr indicate which items in the leaf to check. This totals up the
2296 * space used both by the item structs and the item data
2298 static int leaf_space_used(struct extent_buffer
*l
, int start
, int nr
)
2301 int nritems
= btrfs_header_nritems(l
);
2302 int end
= min(nritems
, start
+ nr
) - 1;
2306 data_len
= btrfs_item_end_nr(l
, start
);
2307 data_len
= data_len
- btrfs_item_offset_nr(l
, end
);
2308 data_len
+= sizeof(struct btrfs_item
) * nr
;
2309 WARN_ON(data_len
< 0);
2314 * The space between the end of the leaf items and
2315 * the start of the leaf data. IOW, how much room
2316 * the leaf has left for both items and data
2318 noinline
int btrfs_leaf_free_space(struct btrfs_root
*root
,
2319 struct extent_buffer
*leaf
)
2321 int nritems
= btrfs_header_nritems(leaf
);
2323 ret
= BTRFS_LEAF_DATA_SIZE(root
) - leaf_space_used(leaf
, 0, nritems
);
2325 printk(KERN_CRIT
"leaf free space ret %d, leaf data size %lu, "
2326 "used %d nritems %d\n",
2327 ret
, (unsigned long) BTRFS_LEAF_DATA_SIZE(root
),
2328 leaf_space_used(leaf
, 0, nritems
), nritems
);
2334 * min slot controls the lowest index we're willing to push to the
2335 * right. We'll push up to and including min_slot, but no lower
2337 static noinline
int __push_leaf_right(struct btrfs_trans_handle
*trans
,
2338 struct btrfs_root
*root
,
2339 struct btrfs_path
*path
,
2340 int data_size
, int empty
,
2341 struct extent_buffer
*right
,
2342 int free_space
, u32 left_nritems
,
2345 struct extent_buffer
*left
= path
->nodes
[0];
2346 struct extent_buffer
*upper
= path
->nodes
[1];
2347 struct btrfs_map_token token
;
2348 struct btrfs_disk_key disk_key
;
2353 struct btrfs_item
*item
;
2359 btrfs_init_map_token(&token
);
2364 nr
= max_t(u32
, 1, min_slot
);
2366 if (path
->slots
[0] >= left_nritems
)
2367 push_space
+= data_size
;
2369 slot
= path
->slots
[1];
2370 i
= left_nritems
- 1;
2372 item
= btrfs_item_nr(left
, i
);
2374 if (!empty
&& push_items
> 0) {
2375 if (path
->slots
[0] > i
)
2377 if (path
->slots
[0] == i
) {
2378 int space
= btrfs_leaf_free_space(root
, left
);
2379 if (space
+ push_space
* 2 > free_space
)
2384 if (path
->slots
[0] == i
)
2385 push_space
+= data_size
;
2387 this_item_size
= btrfs_item_size(left
, item
);
2388 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
2392 push_space
+= this_item_size
+ sizeof(*item
);
2398 if (push_items
== 0)
2401 if (!empty
&& push_items
== left_nritems
)
2404 /* push left to right */
2405 right_nritems
= btrfs_header_nritems(right
);
2407 push_space
= btrfs_item_end_nr(left
, left_nritems
- push_items
);
2408 push_space
-= leaf_data_end(root
, left
);
2410 /* make room in the right data area */
2411 data_end
= leaf_data_end(root
, right
);
2412 memmove_extent_buffer(right
,
2413 btrfs_leaf_data(right
) + data_end
- push_space
,
2414 btrfs_leaf_data(right
) + data_end
,
2415 BTRFS_LEAF_DATA_SIZE(root
) - data_end
);
2417 /* copy from the left data area */
2418 copy_extent_buffer(right
, left
, btrfs_leaf_data(right
) +
2419 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
2420 btrfs_leaf_data(left
) + leaf_data_end(root
, left
),
2423 memmove_extent_buffer(right
, btrfs_item_nr_offset(push_items
),
2424 btrfs_item_nr_offset(0),
2425 right_nritems
* sizeof(struct btrfs_item
));
2427 /* copy the items from left to right */
2428 copy_extent_buffer(right
, left
, btrfs_item_nr_offset(0),
2429 btrfs_item_nr_offset(left_nritems
- push_items
),
2430 push_items
* sizeof(struct btrfs_item
));
2432 /* update the item pointers */
2433 right_nritems
+= push_items
;
2434 btrfs_set_header_nritems(right
, right_nritems
);
2435 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
2436 for (i
= 0; i
< right_nritems
; i
++) {
2437 item
= btrfs_item_nr(right
, i
);
2438 push_space
-= btrfs_token_item_size(right
, item
, &token
);
2439 btrfs_set_token_item_offset(right
, item
, push_space
, &token
);
2442 left_nritems
-= push_items
;
2443 btrfs_set_header_nritems(left
, left_nritems
);
2446 btrfs_mark_buffer_dirty(left
);
2448 clean_tree_block(trans
, root
, left
);
2450 btrfs_mark_buffer_dirty(right
);
2452 btrfs_item_key(right
, &disk_key
, 0);
2453 btrfs_set_node_key(upper
, &disk_key
, slot
+ 1);
2454 btrfs_mark_buffer_dirty(upper
);
2456 /* then fixup the leaf pointer in the path */
2457 if (path
->slots
[0] >= left_nritems
) {
2458 path
->slots
[0] -= left_nritems
;
2459 if (btrfs_header_nritems(path
->nodes
[0]) == 0)
2460 clean_tree_block(trans
, root
, path
->nodes
[0]);
2461 btrfs_tree_unlock(path
->nodes
[0]);
2462 free_extent_buffer(path
->nodes
[0]);
2463 path
->nodes
[0] = right
;
2464 path
->slots
[1] += 1;
2466 btrfs_tree_unlock(right
);
2467 free_extent_buffer(right
);
2472 btrfs_tree_unlock(right
);
2473 free_extent_buffer(right
);
2478 * push some data in the path leaf to the right, trying to free up at
2479 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2481 * returns 1 if the push failed because the other node didn't have enough
2482 * room, 0 if everything worked out and < 0 if there were major errors.
2484 * this will push starting from min_slot to the end of the leaf. It won't
2485 * push any slot lower than min_slot
2487 static int push_leaf_right(struct btrfs_trans_handle
*trans
, struct btrfs_root
2488 *root
, struct btrfs_path
*path
,
2489 int min_data_size
, int data_size
,
2490 int empty
, u32 min_slot
)
2492 struct extent_buffer
*left
= path
->nodes
[0];
2493 struct extent_buffer
*right
;
2494 struct extent_buffer
*upper
;
2500 if (!path
->nodes
[1])
2503 slot
= path
->slots
[1];
2504 upper
= path
->nodes
[1];
2505 if (slot
>= btrfs_header_nritems(upper
) - 1)
2508 btrfs_assert_tree_locked(path
->nodes
[1]);
2510 right
= read_node_slot(root
, upper
, slot
+ 1);
2514 btrfs_tree_lock(right
);
2515 btrfs_set_lock_blocking(right
);
2517 free_space
= btrfs_leaf_free_space(root
, right
);
2518 if (free_space
< data_size
)
2521 /* cow and double check */
2522 ret
= btrfs_cow_block(trans
, root
, right
, upper
,
2527 free_space
= btrfs_leaf_free_space(root
, right
);
2528 if (free_space
< data_size
)
2531 left_nritems
= btrfs_header_nritems(left
);
2532 if (left_nritems
== 0)
2535 return __push_leaf_right(trans
, root
, path
, min_data_size
, empty
,
2536 right
, free_space
, left_nritems
, min_slot
);
2538 btrfs_tree_unlock(right
);
2539 free_extent_buffer(right
);
2544 * push some data in the path leaf to the left, trying to free up at
2545 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2547 * max_slot can put a limit on how far into the leaf we'll push items. The
2548 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2551 static noinline
int __push_leaf_left(struct btrfs_trans_handle
*trans
,
2552 struct btrfs_root
*root
,
2553 struct btrfs_path
*path
, int data_size
,
2554 int empty
, struct extent_buffer
*left
,
2555 int free_space
, u32 right_nritems
,
2558 struct btrfs_disk_key disk_key
;
2559 struct extent_buffer
*right
= path
->nodes
[0];
2563 struct btrfs_item
*item
;
2564 u32 old_left_nritems
;
2568 u32 old_left_item_size
;
2569 struct btrfs_map_token token
;
2571 btrfs_init_map_token(&token
);
2574 nr
= min(right_nritems
, max_slot
);
2576 nr
= min(right_nritems
- 1, max_slot
);
2578 for (i
= 0; i
< nr
; i
++) {
2579 item
= btrfs_item_nr(right
, i
);
2581 if (!empty
&& push_items
> 0) {
2582 if (path
->slots
[0] < i
)
2584 if (path
->slots
[0] == i
) {
2585 int space
= btrfs_leaf_free_space(root
, right
);
2586 if (space
+ push_space
* 2 > free_space
)
2591 if (path
->slots
[0] == i
)
2592 push_space
+= data_size
;
2594 this_item_size
= btrfs_item_size(right
, item
);
2595 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
2599 push_space
+= this_item_size
+ sizeof(*item
);
2602 if (push_items
== 0) {
2606 if (!empty
&& push_items
== btrfs_header_nritems(right
))
2609 /* push data from right to left */
2610 copy_extent_buffer(left
, right
,
2611 btrfs_item_nr_offset(btrfs_header_nritems(left
)),
2612 btrfs_item_nr_offset(0),
2613 push_items
* sizeof(struct btrfs_item
));
2615 push_space
= BTRFS_LEAF_DATA_SIZE(root
) -
2616 btrfs_item_offset_nr(right
, push_items
- 1);
2618 copy_extent_buffer(left
, right
, btrfs_leaf_data(left
) +
2619 leaf_data_end(root
, left
) - push_space
,
2620 btrfs_leaf_data(right
) +
2621 btrfs_item_offset_nr(right
, push_items
- 1),
2623 old_left_nritems
= btrfs_header_nritems(left
);
2624 BUG_ON(old_left_nritems
<= 0);
2626 old_left_item_size
= btrfs_item_offset_nr(left
, old_left_nritems
- 1);
2627 for (i
= old_left_nritems
; i
< old_left_nritems
+ push_items
; i
++) {
2630 item
= btrfs_item_nr(left
, i
);
2632 ioff
= btrfs_token_item_offset(left
, item
, &token
);
2633 btrfs_set_token_item_offset(left
, item
,
2634 ioff
- (BTRFS_LEAF_DATA_SIZE(root
) - old_left_item_size
),
2637 btrfs_set_header_nritems(left
, old_left_nritems
+ push_items
);
2639 /* fixup right node */
2640 if (push_items
> right_nritems
) {
2641 printk(KERN_CRIT
"push items %d nr %u\n", push_items
,
2646 if (push_items
< right_nritems
) {
2647 push_space
= btrfs_item_offset_nr(right
, push_items
- 1) -
2648 leaf_data_end(root
, right
);
2649 memmove_extent_buffer(right
, btrfs_leaf_data(right
) +
2650 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
2651 btrfs_leaf_data(right
) +
2652 leaf_data_end(root
, right
), push_space
);
2654 memmove_extent_buffer(right
, btrfs_item_nr_offset(0),
2655 btrfs_item_nr_offset(push_items
),
2656 (btrfs_header_nritems(right
) - push_items
) *
2657 sizeof(struct btrfs_item
));
2659 right_nritems
-= push_items
;
2660 btrfs_set_header_nritems(right
, right_nritems
);
2661 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
2662 for (i
= 0; i
< right_nritems
; i
++) {
2663 item
= btrfs_item_nr(right
, i
);
2665 push_space
= push_space
- btrfs_token_item_size(right
,
2667 btrfs_set_token_item_offset(right
, item
, push_space
, &token
);
2670 btrfs_mark_buffer_dirty(left
);
2672 btrfs_mark_buffer_dirty(right
);
2674 clean_tree_block(trans
, root
, right
);
2676 btrfs_item_key(right
, &disk_key
, 0);
2677 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
2679 /* then fixup the leaf pointer in the path */
2680 if (path
->slots
[0] < push_items
) {
2681 path
->slots
[0] += old_left_nritems
;
2682 btrfs_tree_unlock(path
->nodes
[0]);
2683 free_extent_buffer(path
->nodes
[0]);
2684 path
->nodes
[0] = left
;
2685 path
->slots
[1] -= 1;
2687 btrfs_tree_unlock(left
);
2688 free_extent_buffer(left
);
2689 path
->slots
[0] -= push_items
;
2691 BUG_ON(path
->slots
[0] < 0);
2694 btrfs_tree_unlock(left
);
2695 free_extent_buffer(left
);
2700 * push some data in the path leaf to the left, trying to free up at
2701 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2703 * max_slot can put a limit on how far into the leaf we'll push items. The
2704 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2707 static int push_leaf_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
2708 *root
, struct btrfs_path
*path
, int min_data_size
,
2709 int data_size
, int empty
, u32 max_slot
)
2711 struct extent_buffer
*right
= path
->nodes
[0];
2712 struct extent_buffer
*left
;
2718 slot
= path
->slots
[1];
2721 if (!path
->nodes
[1])
2724 right_nritems
= btrfs_header_nritems(right
);
2725 if (right_nritems
== 0)
2728 btrfs_assert_tree_locked(path
->nodes
[1]);
2730 left
= read_node_slot(root
, path
->nodes
[1], slot
- 1);
2734 btrfs_tree_lock(left
);
2735 btrfs_set_lock_blocking(left
);
2737 free_space
= btrfs_leaf_free_space(root
, left
);
2738 if (free_space
< data_size
) {
2743 /* cow and double check */
2744 ret
= btrfs_cow_block(trans
, root
, left
,
2745 path
->nodes
[1], slot
- 1, &left
);
2747 /* we hit -ENOSPC, but it isn't fatal here */
2753 free_space
= btrfs_leaf_free_space(root
, left
);
2754 if (free_space
< data_size
) {
2759 return __push_leaf_left(trans
, root
, path
, min_data_size
,
2760 empty
, left
, free_space
, right_nritems
,
2763 btrfs_tree_unlock(left
);
2764 free_extent_buffer(left
);
2769 * split the path's leaf in two, making sure there is at least data_size
2770 * available for the resulting leaf level of the path.
2772 static noinline
void copy_for_split(struct btrfs_trans_handle
*trans
,
2773 struct btrfs_root
*root
,
2774 struct btrfs_path
*path
,
2775 struct extent_buffer
*l
,
2776 struct extent_buffer
*right
,
2777 int slot
, int mid
, int nritems
)
2782 struct btrfs_disk_key disk_key
;
2783 struct btrfs_map_token token
;
2785 btrfs_init_map_token(&token
);
2787 nritems
= nritems
- mid
;
2788 btrfs_set_header_nritems(right
, nritems
);
2789 data_copy_size
= btrfs_item_end_nr(l
, mid
) - leaf_data_end(root
, l
);
2791 copy_extent_buffer(right
, l
, btrfs_item_nr_offset(0),
2792 btrfs_item_nr_offset(mid
),
2793 nritems
* sizeof(struct btrfs_item
));
2795 copy_extent_buffer(right
, l
,
2796 btrfs_leaf_data(right
) + BTRFS_LEAF_DATA_SIZE(root
) -
2797 data_copy_size
, btrfs_leaf_data(l
) +
2798 leaf_data_end(root
, l
), data_copy_size
);
2800 rt_data_off
= BTRFS_LEAF_DATA_SIZE(root
) -
2801 btrfs_item_end_nr(l
, mid
);
2803 for (i
= 0; i
< nritems
; i
++) {
2804 struct btrfs_item
*item
= btrfs_item_nr(right
, i
);
2807 ioff
= btrfs_token_item_offset(right
, item
, &token
);
2808 btrfs_set_token_item_offset(right
, item
,
2809 ioff
+ rt_data_off
, &token
);
2812 btrfs_set_header_nritems(l
, mid
);
2813 btrfs_item_key(right
, &disk_key
, 0);
2814 insert_ptr(trans
, root
, path
, &disk_key
, right
->start
,
2815 path
->slots
[1] + 1, 1);
2817 btrfs_mark_buffer_dirty(right
);
2818 btrfs_mark_buffer_dirty(l
);
2819 BUG_ON(path
->slots
[0] != slot
);
2822 btrfs_tree_unlock(path
->nodes
[0]);
2823 free_extent_buffer(path
->nodes
[0]);
2824 path
->nodes
[0] = right
;
2825 path
->slots
[0] -= mid
;
2826 path
->slots
[1] += 1;
2828 btrfs_tree_unlock(right
);
2829 free_extent_buffer(right
);
2832 BUG_ON(path
->slots
[0] < 0);
2836 * double splits happen when we need to insert a big item in the middle
2837 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2838 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2841 * We avoid this by trying to push the items on either side of our target
2842 * into the adjacent leaves. If all goes well we can avoid the double split
2845 static noinline
int push_for_double_split(struct btrfs_trans_handle
*trans
,
2846 struct btrfs_root
*root
,
2847 struct btrfs_path
*path
,
2855 slot
= path
->slots
[0];
2858 * try to push all the items after our slot into the
2861 ret
= push_leaf_right(trans
, root
, path
, 1, data_size
, 0, slot
);
2868 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2870 * our goal is to get our slot at the start or end of a leaf. If
2871 * we've done so we're done
2873 if (path
->slots
[0] == 0 || path
->slots
[0] == nritems
)
2876 if (btrfs_leaf_free_space(root
, path
->nodes
[0]) >= data_size
)
2879 /* try to push all the items before our slot into the next leaf */
2880 slot
= path
->slots
[0];
2881 ret
= push_leaf_left(trans
, root
, path
, 1, data_size
, 0, slot
);
2894 * split the path's leaf in two, making sure there is at least data_size
2895 * available for the resulting leaf level of the path.
2897 * returns 0 if all went well and < 0 on failure.
2899 static noinline
int split_leaf(struct btrfs_trans_handle
*trans
,
2900 struct btrfs_root
*root
,
2901 struct btrfs_key
*ins_key
,
2902 struct btrfs_path
*path
, int data_size
,
2905 struct btrfs_disk_key disk_key
;
2906 struct extent_buffer
*l
;
2910 struct extent_buffer
*right
;
2914 int num_doubles
= 0;
2915 int tried_avoid_double
= 0;
2918 slot
= path
->slots
[0];
2919 if (extend
&& data_size
+ btrfs_item_size_nr(l
, slot
) +
2920 sizeof(struct btrfs_item
) > BTRFS_LEAF_DATA_SIZE(root
))
2923 /* first try to make some room by pushing left and right */
2925 wret
= push_leaf_right(trans
, root
, path
, data_size
,
2930 wret
= push_leaf_left(trans
, root
, path
, data_size
,
2931 data_size
, 0, (u32
)-1);
2937 /* did the pushes work? */
2938 if (btrfs_leaf_free_space(root
, l
) >= data_size
)
2942 if (!path
->nodes
[1]) {
2943 ret
= insert_new_root(trans
, root
, path
, 1);
2950 slot
= path
->slots
[0];
2951 nritems
= btrfs_header_nritems(l
);
2952 mid
= (nritems
+ 1) / 2;
2956 leaf_space_used(l
, mid
, nritems
- mid
) + data_size
>
2957 BTRFS_LEAF_DATA_SIZE(root
)) {
2958 if (slot
>= nritems
) {
2962 if (mid
!= nritems
&&
2963 leaf_space_used(l
, mid
, nritems
- mid
) +
2964 data_size
> BTRFS_LEAF_DATA_SIZE(root
)) {
2965 if (data_size
&& !tried_avoid_double
)
2966 goto push_for_double
;
2972 if (leaf_space_used(l
, 0, mid
) + data_size
>
2973 BTRFS_LEAF_DATA_SIZE(root
)) {
2974 if (!extend
&& data_size
&& slot
== 0) {
2976 } else if ((extend
|| !data_size
) && slot
== 0) {
2980 if (mid
!= nritems
&&
2981 leaf_space_used(l
, mid
, nritems
- mid
) +
2982 data_size
> BTRFS_LEAF_DATA_SIZE(root
)) {
2983 if (data_size
&& !tried_avoid_double
)
2984 goto push_for_double
;
2992 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
2994 btrfs_item_key(l
, &disk_key
, mid
);
2996 right
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
, 0,
2997 root
->root_key
.objectid
,
2998 &disk_key
, 0, l
->start
, 0, 0);
3000 return PTR_ERR(right
);
3002 root_add_used(root
, root
->leafsize
);
3004 memset_extent_buffer(right
, 0, 0, sizeof(struct btrfs_header
));
3005 btrfs_set_header_bytenr(right
, right
->start
);
3006 btrfs_set_header_generation(right
, trans
->transid
);
3007 btrfs_set_header_backref_rev(right
, BTRFS_MIXED_BACKREF_REV
);
3008 btrfs_set_header_owner(right
, root
->root_key
.objectid
);
3009 btrfs_set_header_level(right
, 0);
3010 write_extent_buffer(right
, root
->fs_info
->fsid
,
3011 (unsigned long)btrfs_header_fsid(right
),
3014 write_extent_buffer(right
, root
->fs_info
->chunk_tree_uuid
,
3015 (unsigned long)btrfs_header_chunk_tree_uuid(right
),
3020 btrfs_set_header_nritems(right
, 0);
3021 insert_ptr(trans
, root
, path
, &disk_key
, right
->start
,
3022 path
->slots
[1] + 1, 1);
3023 btrfs_tree_unlock(path
->nodes
[0]);
3024 free_extent_buffer(path
->nodes
[0]);
3025 path
->nodes
[0] = right
;
3027 path
->slots
[1] += 1;
3029 btrfs_set_header_nritems(right
, 0);
3030 insert_ptr(trans
, root
, path
, &disk_key
, right
->start
,
3032 btrfs_tree_unlock(path
->nodes
[0]);
3033 free_extent_buffer(path
->nodes
[0]);
3034 path
->nodes
[0] = right
;
3036 if (path
->slots
[1] == 0)
3037 fixup_low_keys(trans
, root
, path
,
3040 btrfs_mark_buffer_dirty(right
);
3044 copy_for_split(trans
, root
, path
, l
, right
, slot
, mid
, nritems
);
3047 BUG_ON(num_doubles
!= 0);
3055 push_for_double_split(trans
, root
, path
, data_size
);
3056 tried_avoid_double
= 1;
3057 if (btrfs_leaf_free_space(root
, path
->nodes
[0]) >= data_size
)
3062 static noinline
int setup_leaf_for_split(struct btrfs_trans_handle
*trans
,
3063 struct btrfs_root
*root
,
3064 struct btrfs_path
*path
, int ins_len
)
3066 struct btrfs_key key
;
3067 struct extent_buffer
*leaf
;
3068 struct btrfs_file_extent_item
*fi
;
3073 leaf
= path
->nodes
[0];
3074 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3076 BUG_ON(key
.type
!= BTRFS_EXTENT_DATA_KEY
&&
3077 key
.type
!= BTRFS_EXTENT_CSUM_KEY
);
3079 if (btrfs_leaf_free_space(root
, leaf
) >= ins_len
)
3082 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
3083 if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
3084 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3085 struct btrfs_file_extent_item
);
3086 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
3088 btrfs_release_path(path
);
3090 path
->keep_locks
= 1;
3091 path
->search_for_split
= 1;
3092 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
3093 path
->search_for_split
= 0;
3098 leaf
= path
->nodes
[0];
3099 /* if our item isn't there or got smaller, return now */
3100 if (ret
> 0 || item_size
!= btrfs_item_size_nr(leaf
, path
->slots
[0]))
3103 /* the leaf has changed, it now has room. return now */
3104 if (btrfs_leaf_free_space(root
, path
->nodes
[0]) >= ins_len
)
3107 if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
3108 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3109 struct btrfs_file_extent_item
);
3110 if (extent_len
!= btrfs_file_extent_num_bytes(leaf
, fi
))
3114 btrfs_set_path_blocking(path
);
3115 ret
= split_leaf(trans
, root
, &key
, path
, ins_len
, 1);
3119 path
->keep_locks
= 0;
3120 btrfs_unlock_up_safe(path
, 1);
3123 path
->keep_locks
= 0;
3127 static noinline
int split_item(struct btrfs_trans_handle
*trans
,
3128 struct btrfs_root
*root
,
3129 struct btrfs_path
*path
,
3130 struct btrfs_key
*new_key
,
3131 unsigned long split_offset
)
3133 struct extent_buffer
*leaf
;
3134 struct btrfs_item
*item
;
3135 struct btrfs_item
*new_item
;
3141 struct btrfs_disk_key disk_key
;
3143 leaf
= path
->nodes
[0];
3144 BUG_ON(btrfs_leaf_free_space(root
, leaf
) < sizeof(struct btrfs_item
));
3146 btrfs_set_path_blocking(path
);
3148 item
= btrfs_item_nr(leaf
, path
->slots
[0]);
3149 orig_offset
= btrfs_item_offset(leaf
, item
);
3150 item_size
= btrfs_item_size(leaf
, item
);
3152 buf
= kmalloc(item_size
, GFP_NOFS
);
3156 read_extent_buffer(leaf
, buf
, btrfs_item_ptr_offset(leaf
,
3157 path
->slots
[0]), item_size
);
3159 slot
= path
->slots
[0] + 1;
3160 nritems
= btrfs_header_nritems(leaf
);
3161 if (slot
!= nritems
) {
3162 /* shift the items */
3163 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ 1),
3164 btrfs_item_nr_offset(slot
),
3165 (nritems
- slot
) * sizeof(struct btrfs_item
));
3168 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
3169 btrfs_set_item_key(leaf
, &disk_key
, slot
);
3171 new_item
= btrfs_item_nr(leaf
, slot
);
3173 btrfs_set_item_offset(leaf
, new_item
, orig_offset
);
3174 btrfs_set_item_size(leaf
, new_item
, item_size
- split_offset
);
3176 btrfs_set_item_offset(leaf
, item
,
3177 orig_offset
+ item_size
- split_offset
);
3178 btrfs_set_item_size(leaf
, item
, split_offset
);
3180 btrfs_set_header_nritems(leaf
, nritems
+ 1);
3182 /* write the data for the start of the original item */
3183 write_extent_buffer(leaf
, buf
,
3184 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
3187 /* write the data for the new item */
3188 write_extent_buffer(leaf
, buf
+ split_offset
,
3189 btrfs_item_ptr_offset(leaf
, slot
),
3190 item_size
- split_offset
);
3191 btrfs_mark_buffer_dirty(leaf
);
3193 BUG_ON(btrfs_leaf_free_space(root
, leaf
) < 0);
3199 * This function splits a single item into two items,
3200 * giving 'new_key' to the new item and splitting the
3201 * old one at split_offset (from the start of the item).
3203 * The path may be released by this operation. After
3204 * the split, the path is pointing to the old item. The
3205 * new item is going to be in the same node as the old one.
3207 * Note, the item being split must be smaller enough to live alone on
3208 * a tree block with room for one extra struct btrfs_item
3210 * This allows us to split the item in place, keeping a lock on the
3211 * leaf the entire time.
3213 int btrfs_split_item(struct btrfs_trans_handle
*trans
,
3214 struct btrfs_root
*root
,
3215 struct btrfs_path
*path
,
3216 struct btrfs_key
*new_key
,
3217 unsigned long split_offset
)
3220 ret
= setup_leaf_for_split(trans
, root
, path
,
3221 sizeof(struct btrfs_item
));
3225 ret
= split_item(trans
, root
, path
, new_key
, split_offset
);
3230 * This function duplicate a item, giving 'new_key' to the new item.
3231 * It guarantees both items live in the same tree leaf and the new item
3232 * is contiguous with the original item.
3234 * This allows us to split file extent in place, keeping a lock on the
3235 * leaf the entire time.
3237 int btrfs_duplicate_item(struct btrfs_trans_handle
*trans
,
3238 struct btrfs_root
*root
,
3239 struct btrfs_path
*path
,
3240 struct btrfs_key
*new_key
)
3242 struct extent_buffer
*leaf
;
3246 leaf
= path
->nodes
[0];
3247 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
3248 ret
= setup_leaf_for_split(trans
, root
, path
,
3249 item_size
+ sizeof(struct btrfs_item
));
3254 setup_items_for_insert(trans
, root
, path
, new_key
, &item_size
,
3255 item_size
, item_size
+
3256 sizeof(struct btrfs_item
), 1);
3257 leaf
= path
->nodes
[0];
3258 memcpy_extent_buffer(leaf
,
3259 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
3260 btrfs_item_ptr_offset(leaf
, path
->slots
[0] - 1),
3266 * make the item pointed to by the path smaller. new_size indicates
3267 * how small to make it, and from_end tells us if we just chop bytes
3268 * off the end of the item or if we shift the item to chop bytes off
3271 void btrfs_truncate_item(struct btrfs_trans_handle
*trans
,
3272 struct btrfs_root
*root
,
3273 struct btrfs_path
*path
,
3274 u32 new_size
, int from_end
)
3277 struct extent_buffer
*leaf
;
3278 struct btrfs_item
*item
;
3280 unsigned int data_end
;
3281 unsigned int old_data_start
;
3282 unsigned int old_size
;
3283 unsigned int size_diff
;
3285 struct btrfs_map_token token
;
3287 btrfs_init_map_token(&token
);
3289 leaf
= path
->nodes
[0];
3290 slot
= path
->slots
[0];
3292 old_size
= btrfs_item_size_nr(leaf
, slot
);
3293 if (old_size
== new_size
)
3296 nritems
= btrfs_header_nritems(leaf
);
3297 data_end
= leaf_data_end(root
, leaf
);
3299 old_data_start
= btrfs_item_offset_nr(leaf
, slot
);
3301 size_diff
= old_size
- new_size
;
3304 BUG_ON(slot
>= nritems
);
3307 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3309 /* first correct the data pointers */
3310 for (i
= slot
; i
< nritems
; i
++) {
3312 item
= btrfs_item_nr(leaf
, i
);
3314 ioff
= btrfs_token_item_offset(leaf
, item
, &token
);
3315 btrfs_set_token_item_offset(leaf
, item
,
3316 ioff
+ size_diff
, &token
);
3319 /* shift the data */
3321 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3322 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
3323 data_end
, old_data_start
+ new_size
- data_end
);
3325 struct btrfs_disk_key disk_key
;
3328 btrfs_item_key(leaf
, &disk_key
, slot
);
3330 if (btrfs_disk_key_type(&disk_key
) == BTRFS_EXTENT_DATA_KEY
) {
3332 struct btrfs_file_extent_item
*fi
;
3334 fi
= btrfs_item_ptr(leaf
, slot
,
3335 struct btrfs_file_extent_item
);
3336 fi
= (struct btrfs_file_extent_item
*)(
3337 (unsigned long)fi
- size_diff
);
3339 if (btrfs_file_extent_type(leaf
, fi
) ==
3340 BTRFS_FILE_EXTENT_INLINE
) {
3341 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
3342 memmove_extent_buffer(leaf
, ptr
,
3344 offsetof(struct btrfs_file_extent_item
,
3349 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3350 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
3351 data_end
, old_data_start
- data_end
);
3353 offset
= btrfs_disk_key_offset(&disk_key
);
3354 btrfs_set_disk_key_offset(&disk_key
, offset
+ size_diff
);
3355 btrfs_set_item_key(leaf
, &disk_key
, slot
);
3357 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
3360 item
= btrfs_item_nr(leaf
, slot
);
3361 btrfs_set_item_size(leaf
, item
, new_size
);
3362 btrfs_mark_buffer_dirty(leaf
);
3364 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3365 btrfs_print_leaf(root
, leaf
);
3371 * make the item pointed to by the path bigger, data_size is the new size.
3373 void btrfs_extend_item(struct btrfs_trans_handle
*trans
,
3374 struct btrfs_root
*root
, struct btrfs_path
*path
,
3378 struct extent_buffer
*leaf
;
3379 struct btrfs_item
*item
;
3381 unsigned int data_end
;
3382 unsigned int old_data
;
3383 unsigned int old_size
;
3385 struct btrfs_map_token token
;
3387 btrfs_init_map_token(&token
);
3389 leaf
= path
->nodes
[0];
3391 nritems
= btrfs_header_nritems(leaf
);
3392 data_end
= leaf_data_end(root
, leaf
);
3394 if (btrfs_leaf_free_space(root
, leaf
) < data_size
) {
3395 btrfs_print_leaf(root
, leaf
);
3398 slot
= path
->slots
[0];
3399 old_data
= btrfs_item_end_nr(leaf
, slot
);
3402 if (slot
>= nritems
) {
3403 btrfs_print_leaf(root
, leaf
);
3404 printk(KERN_CRIT
"slot %d too large, nritems %d\n",
3410 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3412 /* first correct the data pointers */
3413 for (i
= slot
; i
< nritems
; i
++) {
3415 item
= btrfs_item_nr(leaf
, i
);
3417 ioff
= btrfs_token_item_offset(leaf
, item
, &token
);
3418 btrfs_set_token_item_offset(leaf
, item
,
3419 ioff
- data_size
, &token
);
3422 /* shift the data */
3423 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3424 data_end
- data_size
, btrfs_leaf_data(leaf
) +
3425 data_end
, old_data
- data_end
);
3427 data_end
= old_data
;
3428 old_size
= btrfs_item_size_nr(leaf
, slot
);
3429 item
= btrfs_item_nr(leaf
, slot
);
3430 btrfs_set_item_size(leaf
, item
, old_size
+ data_size
);
3431 btrfs_mark_buffer_dirty(leaf
);
3433 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3434 btrfs_print_leaf(root
, leaf
);
3440 * Given a key and some data, insert items into the tree.
3441 * This does all the path init required, making room in the tree if needed.
3442 * Returns the number of keys that were inserted.
3444 int btrfs_insert_some_items(struct btrfs_trans_handle
*trans
,
3445 struct btrfs_root
*root
,
3446 struct btrfs_path
*path
,
3447 struct btrfs_key
*cpu_key
, u32
*data_size
,
3450 struct extent_buffer
*leaf
;
3451 struct btrfs_item
*item
;
3458 unsigned int data_end
;
3459 struct btrfs_disk_key disk_key
;
3460 struct btrfs_key found_key
;
3461 struct btrfs_map_token token
;
3463 btrfs_init_map_token(&token
);
3465 for (i
= 0; i
< nr
; i
++) {
3466 if (total_size
+ data_size
[i
] + sizeof(struct btrfs_item
) >
3467 BTRFS_LEAF_DATA_SIZE(root
)) {
3471 total_data
+= data_size
[i
];
3472 total_size
+= data_size
[i
] + sizeof(struct btrfs_item
);
3476 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
3482 leaf
= path
->nodes
[0];
3484 nritems
= btrfs_header_nritems(leaf
);
3485 data_end
= leaf_data_end(root
, leaf
);
3487 if (btrfs_leaf_free_space(root
, leaf
) < total_size
) {
3488 for (i
= nr
; i
>= 0; i
--) {
3489 total_data
-= data_size
[i
];
3490 total_size
-= data_size
[i
] + sizeof(struct btrfs_item
);
3491 if (total_size
< btrfs_leaf_free_space(root
, leaf
))
3497 slot
= path
->slots
[0];
3500 if (slot
!= nritems
) {
3501 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
3503 item
= btrfs_item_nr(leaf
, slot
);
3504 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3506 /* figure out how many keys we can insert in here */
3507 total_data
= data_size
[0];
3508 for (i
= 1; i
< nr
; i
++) {
3509 if (btrfs_comp_cpu_keys(&found_key
, cpu_key
+ i
) <= 0)
3511 total_data
+= data_size
[i
];
3515 if (old_data
< data_end
) {
3516 btrfs_print_leaf(root
, leaf
);
3517 printk(KERN_CRIT
"slot %d old_data %d data_end %d\n",
3518 slot
, old_data
, data_end
);
3522 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3524 /* first correct the data pointers */
3525 for (i
= slot
; i
< nritems
; i
++) {
3528 item
= btrfs_item_nr(leaf
, i
);
3529 ioff
= btrfs_token_item_offset(leaf
, item
, &token
);
3530 btrfs_set_token_item_offset(leaf
, item
,
3531 ioff
- total_data
, &token
);
3533 /* shift the items */
3534 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
3535 btrfs_item_nr_offset(slot
),
3536 (nritems
- slot
) * sizeof(struct btrfs_item
));
3538 /* shift the data */
3539 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3540 data_end
- total_data
, btrfs_leaf_data(leaf
) +
3541 data_end
, old_data
- data_end
);
3542 data_end
= old_data
;
3545 * this sucks but it has to be done, if we are inserting at
3546 * the end of the leaf only insert 1 of the items, since we
3547 * have no way of knowing whats on the next leaf and we'd have
3548 * to drop our current locks to figure it out
3553 /* setup the item for the new data */
3554 for (i
= 0; i
< nr
; i
++) {
3555 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
3556 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
3557 item
= btrfs_item_nr(leaf
, slot
+ i
);
3558 btrfs_set_token_item_offset(leaf
, item
,
3559 data_end
- data_size
[i
], &token
);
3560 data_end
-= data_size
[i
];
3561 btrfs_set_token_item_size(leaf
, item
, data_size
[i
], &token
);
3563 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
3564 btrfs_mark_buffer_dirty(leaf
);
3568 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
3569 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
3572 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3573 btrfs_print_leaf(root
, leaf
);
3583 * this is a helper for btrfs_insert_empty_items, the main goal here is
3584 * to save stack depth by doing the bulk of the work in a function
3585 * that doesn't call btrfs_search_slot
3587 void setup_items_for_insert(struct btrfs_trans_handle
*trans
,
3588 struct btrfs_root
*root
, struct btrfs_path
*path
,
3589 struct btrfs_key
*cpu_key
, u32
*data_size
,
3590 u32 total_data
, u32 total_size
, int nr
)
3592 struct btrfs_item
*item
;
3595 unsigned int data_end
;
3596 struct btrfs_disk_key disk_key
;
3597 struct extent_buffer
*leaf
;
3599 struct btrfs_map_token token
;
3601 btrfs_init_map_token(&token
);
3603 leaf
= path
->nodes
[0];
3604 slot
= path
->slots
[0];
3606 nritems
= btrfs_header_nritems(leaf
);
3607 data_end
= leaf_data_end(root
, leaf
);
3609 if (btrfs_leaf_free_space(root
, leaf
) < total_size
) {
3610 btrfs_print_leaf(root
, leaf
);
3611 printk(KERN_CRIT
"not enough freespace need %u have %d\n",
3612 total_size
, btrfs_leaf_free_space(root
, leaf
));
3616 if (slot
!= nritems
) {
3617 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
3619 if (old_data
< data_end
) {
3620 btrfs_print_leaf(root
, leaf
);
3621 printk(KERN_CRIT
"slot %d old_data %d data_end %d\n",
3622 slot
, old_data
, data_end
);
3626 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3628 /* first correct the data pointers */
3629 for (i
= slot
; i
< nritems
; i
++) {
3632 item
= btrfs_item_nr(leaf
, i
);
3633 ioff
= btrfs_token_item_offset(leaf
, item
, &token
);
3634 btrfs_set_token_item_offset(leaf
, item
,
3635 ioff
- total_data
, &token
);
3637 /* shift the items */
3638 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
3639 btrfs_item_nr_offset(slot
),
3640 (nritems
- slot
) * sizeof(struct btrfs_item
));
3642 /* shift the data */
3643 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3644 data_end
- total_data
, btrfs_leaf_data(leaf
) +
3645 data_end
, old_data
- data_end
);
3646 data_end
= old_data
;
3649 /* setup the item for the new data */
3650 for (i
= 0; i
< nr
; i
++) {
3651 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
3652 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
3653 item
= btrfs_item_nr(leaf
, slot
+ i
);
3654 btrfs_set_token_item_offset(leaf
, item
,
3655 data_end
- data_size
[i
], &token
);
3656 data_end
-= data_size
[i
];
3657 btrfs_set_token_item_size(leaf
, item
, data_size
[i
], &token
);
3660 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
3663 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
3664 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
3666 btrfs_unlock_up_safe(path
, 1);
3667 btrfs_mark_buffer_dirty(leaf
);
3669 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3670 btrfs_print_leaf(root
, leaf
);
3676 * Given a key and some data, insert items into the tree.
3677 * This does all the path init required, making room in the tree if needed.
3679 int btrfs_insert_empty_items(struct btrfs_trans_handle
*trans
,
3680 struct btrfs_root
*root
,
3681 struct btrfs_path
*path
,
3682 struct btrfs_key
*cpu_key
, u32
*data_size
,
3691 for (i
= 0; i
< nr
; i
++)
3692 total_data
+= data_size
[i
];
3694 total_size
= total_data
+ (nr
* sizeof(struct btrfs_item
));
3695 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
3701 slot
= path
->slots
[0];
3704 setup_items_for_insert(trans
, root
, path
, cpu_key
, data_size
,
3705 total_data
, total_size
, nr
);
3710 * Given a key and some data, insert an item into the tree.
3711 * This does all the path init required, making room in the tree if needed.
3713 int btrfs_insert_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
3714 *root
, struct btrfs_key
*cpu_key
, void *data
, u32
3718 struct btrfs_path
*path
;
3719 struct extent_buffer
*leaf
;
3722 path
= btrfs_alloc_path();
3725 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
3727 leaf
= path
->nodes
[0];
3728 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
3729 write_extent_buffer(leaf
, data
, ptr
, data_size
);
3730 btrfs_mark_buffer_dirty(leaf
);
3732 btrfs_free_path(path
);
3737 * delete the pointer from a given node.
3739 * the tree should have been previously balanced so the deletion does not
3742 static void del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
3743 struct btrfs_path
*path
, int level
, int slot
)
3745 struct extent_buffer
*parent
= path
->nodes
[level
];
3748 nritems
= btrfs_header_nritems(parent
);
3749 if (slot
!= nritems
- 1) {
3750 memmove_extent_buffer(parent
,
3751 btrfs_node_key_ptr_offset(slot
),
3752 btrfs_node_key_ptr_offset(slot
+ 1),
3753 sizeof(struct btrfs_key_ptr
) *
3754 (nritems
- slot
- 1));
3757 btrfs_set_header_nritems(parent
, nritems
);
3758 if (nritems
== 0 && parent
== root
->node
) {
3759 BUG_ON(btrfs_header_level(root
->node
) != 1);
3760 /* just turn the root into a leaf and break */
3761 btrfs_set_header_level(root
->node
, 0);
3762 } else if (slot
== 0) {
3763 struct btrfs_disk_key disk_key
;
3765 btrfs_node_key(parent
, &disk_key
, 0);
3766 fixup_low_keys(trans
, root
, path
, &disk_key
, level
+ 1);
3768 btrfs_mark_buffer_dirty(parent
);
3772 * a helper function to delete the leaf pointed to by path->slots[1] and
3775 * This deletes the pointer in path->nodes[1] and frees the leaf
3776 * block extent. zero is returned if it all worked out, < 0 otherwise.
3778 * The path must have already been setup for deleting the leaf, including
3779 * all the proper balancing. path->nodes[1] must be locked.
3781 static noinline
void btrfs_del_leaf(struct btrfs_trans_handle
*trans
,
3782 struct btrfs_root
*root
,
3783 struct btrfs_path
*path
,
3784 struct extent_buffer
*leaf
)
3786 WARN_ON(btrfs_header_generation(leaf
) != trans
->transid
);
3787 del_ptr(trans
, root
, path
, 1, path
->slots
[1]);
3790 * btrfs_free_extent is expensive, we want to make sure we
3791 * aren't holding any locks when we call it
3793 btrfs_unlock_up_safe(path
, 0);
3795 root_sub_used(root
, leaf
->len
);
3797 extent_buffer_get(leaf
);
3798 btrfs_free_tree_block(trans
, root
, leaf
, 0, 1, 0);
3799 free_extent_buffer_stale(leaf
);
3802 * delete the item at the leaf level in path. If that empties
3803 * the leaf, remove it from the tree
3805 int btrfs_del_items(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
3806 struct btrfs_path
*path
, int slot
, int nr
)
3808 struct extent_buffer
*leaf
;
3809 struct btrfs_item
*item
;
3816 struct btrfs_map_token token
;
3818 btrfs_init_map_token(&token
);
3820 leaf
= path
->nodes
[0];
3821 last_off
= btrfs_item_offset_nr(leaf
, slot
+ nr
- 1);
3823 for (i
= 0; i
< nr
; i
++)
3824 dsize
+= btrfs_item_size_nr(leaf
, slot
+ i
);
3826 nritems
= btrfs_header_nritems(leaf
);
3828 if (slot
+ nr
!= nritems
) {
3829 int data_end
= leaf_data_end(root
, leaf
);
3831 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3833 btrfs_leaf_data(leaf
) + data_end
,
3834 last_off
- data_end
);
3836 for (i
= slot
+ nr
; i
< nritems
; i
++) {
3839 item
= btrfs_item_nr(leaf
, i
);
3840 ioff
= btrfs_token_item_offset(leaf
, item
, &token
);
3841 btrfs_set_token_item_offset(leaf
, item
,
3842 ioff
+ dsize
, &token
);
3845 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
),
3846 btrfs_item_nr_offset(slot
+ nr
),
3847 sizeof(struct btrfs_item
) *
3848 (nritems
- slot
- nr
));
3850 btrfs_set_header_nritems(leaf
, nritems
- nr
);
3853 /* delete the leaf if we've emptied it */
3855 if (leaf
== root
->node
) {
3856 btrfs_set_header_level(leaf
, 0);
3858 btrfs_set_path_blocking(path
);
3859 clean_tree_block(trans
, root
, leaf
);
3860 btrfs_del_leaf(trans
, root
, path
, leaf
);
3863 int used
= leaf_space_used(leaf
, 0, nritems
);
3865 struct btrfs_disk_key disk_key
;
3867 btrfs_item_key(leaf
, &disk_key
, 0);
3868 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
3871 /* delete the leaf if it is mostly empty */
3872 if (used
< BTRFS_LEAF_DATA_SIZE(root
) / 3) {
3873 /* push_leaf_left fixes the path.
3874 * make sure the path still points to our leaf
3875 * for possible call to del_ptr below
3877 slot
= path
->slots
[1];
3878 extent_buffer_get(leaf
);
3880 btrfs_set_path_blocking(path
);
3881 wret
= push_leaf_left(trans
, root
, path
, 1, 1,
3883 if (wret
< 0 && wret
!= -ENOSPC
)
3886 if (path
->nodes
[0] == leaf
&&
3887 btrfs_header_nritems(leaf
)) {
3888 wret
= push_leaf_right(trans
, root
, path
, 1,
3890 if (wret
< 0 && wret
!= -ENOSPC
)
3894 if (btrfs_header_nritems(leaf
) == 0) {
3895 path
->slots
[1] = slot
;
3896 btrfs_del_leaf(trans
, root
, path
, leaf
);
3897 free_extent_buffer(leaf
);
3900 /* if we're still in the path, make sure
3901 * we're dirty. Otherwise, one of the
3902 * push_leaf functions must have already
3903 * dirtied this buffer
3905 if (path
->nodes
[0] == leaf
)
3906 btrfs_mark_buffer_dirty(leaf
);
3907 free_extent_buffer(leaf
);
3910 btrfs_mark_buffer_dirty(leaf
);
3917 * search the tree again to find a leaf with lesser keys
3918 * returns 0 if it found something or 1 if there are no lesser leaves.
3919 * returns < 0 on io errors.
3921 * This may release the path, and so you may lose any locks held at the
3924 int btrfs_prev_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
3926 struct btrfs_key key
;
3927 struct btrfs_disk_key found_key
;
3930 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, 0);
3934 else if (key
.type
> 0)
3936 else if (key
.objectid
> 0)
3941 btrfs_release_path(path
);
3942 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3945 btrfs_item_key(path
->nodes
[0], &found_key
, 0);
3946 ret
= comp_keys(&found_key
, &key
);
3953 * A helper function to walk down the tree starting at min_key, and looking
3954 * for nodes or leaves that are either in cache or have a minimum
3955 * transaction id. This is used by the btree defrag code, and tree logging
3957 * This does not cow, but it does stuff the starting key it finds back
3958 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3959 * key and get a writable path.
3961 * This does lock as it descends, and path->keep_locks should be set
3962 * to 1 by the caller.
3964 * This honors path->lowest_level to prevent descent past a given level
3967 * min_trans indicates the oldest transaction that you are interested
3968 * in walking through. Any nodes or leaves older than min_trans are
3969 * skipped over (without reading them).
3971 * returns zero if something useful was found, < 0 on error and 1 if there
3972 * was nothing in the tree that matched the search criteria.
3974 int btrfs_search_forward(struct btrfs_root
*root
, struct btrfs_key
*min_key
,
3975 struct btrfs_key
*max_key
,
3976 struct btrfs_path
*path
, int cache_only
,
3979 struct extent_buffer
*cur
;
3980 struct btrfs_key found_key
;
3987 WARN_ON(!path
->keep_locks
);
3989 cur
= btrfs_read_lock_root_node(root
);
3990 level
= btrfs_header_level(cur
);
3991 WARN_ON(path
->nodes
[level
]);
3992 path
->nodes
[level
] = cur
;
3993 path
->locks
[level
] = BTRFS_READ_LOCK
;
3995 if (btrfs_header_generation(cur
) < min_trans
) {
4000 nritems
= btrfs_header_nritems(cur
);
4001 level
= btrfs_header_level(cur
);
4002 sret
= bin_search(cur
, min_key
, level
, &slot
);
4004 /* at the lowest level, we're done, setup the path and exit */
4005 if (level
== path
->lowest_level
) {
4006 if (slot
>= nritems
)
4009 path
->slots
[level
] = slot
;
4010 btrfs_item_key_to_cpu(cur
, &found_key
, slot
);
4013 if (sret
&& slot
> 0)
4016 * check this node pointer against the cache_only and
4017 * min_trans parameters. If it isn't in cache or is too
4018 * old, skip to the next one.
4020 while (slot
< nritems
) {
4023 struct extent_buffer
*tmp
;
4024 struct btrfs_disk_key disk_key
;
4026 blockptr
= btrfs_node_blockptr(cur
, slot
);
4027 gen
= btrfs_node_ptr_generation(cur
, slot
);
4028 if (gen
< min_trans
) {
4036 btrfs_node_key(cur
, &disk_key
, slot
);
4037 if (comp_keys(&disk_key
, max_key
) >= 0) {
4043 tmp
= btrfs_find_tree_block(root
, blockptr
,
4044 btrfs_level_size(root
, level
- 1));
4046 if (tmp
&& btrfs_buffer_uptodate(tmp
, gen
)) {
4047 free_extent_buffer(tmp
);
4051 free_extent_buffer(tmp
);
4056 * we didn't find a candidate key in this node, walk forward
4057 * and find another one
4059 if (slot
>= nritems
) {
4060 path
->slots
[level
] = slot
;
4061 btrfs_set_path_blocking(path
);
4062 sret
= btrfs_find_next_key(root
, path
, min_key
, level
,
4063 cache_only
, min_trans
);
4065 btrfs_release_path(path
);
4071 /* save our key for returning back */
4072 btrfs_node_key_to_cpu(cur
, &found_key
, slot
);
4073 path
->slots
[level
] = slot
;
4074 if (level
== path
->lowest_level
) {
4076 unlock_up(path
, level
, 1, 0, NULL
);
4079 btrfs_set_path_blocking(path
);
4080 cur
= read_node_slot(root
, cur
, slot
);
4081 BUG_ON(!cur
); /* -ENOMEM */
4083 btrfs_tree_read_lock(cur
);
4085 path
->locks
[level
- 1] = BTRFS_READ_LOCK
;
4086 path
->nodes
[level
- 1] = cur
;
4087 unlock_up(path
, level
, 1, 0, NULL
);
4088 btrfs_clear_path_blocking(path
, NULL
, 0);
4092 memcpy(min_key
, &found_key
, sizeof(found_key
));
4093 btrfs_set_path_blocking(path
);
4098 * this is similar to btrfs_next_leaf, but does not try to preserve
4099 * and fixup the path. It looks for and returns the next key in the
4100 * tree based on the current path and the cache_only and min_trans
4103 * 0 is returned if another key is found, < 0 if there are any errors
4104 * and 1 is returned if there are no higher keys in the tree
4106 * path->keep_locks should be set to 1 on the search made before
4107 * calling this function.
4109 int btrfs_find_next_key(struct btrfs_root
*root
, struct btrfs_path
*path
,
4110 struct btrfs_key
*key
, int level
,
4111 int cache_only
, u64 min_trans
)
4114 struct extent_buffer
*c
;
4116 WARN_ON(!path
->keep_locks
);
4117 while (level
< BTRFS_MAX_LEVEL
) {
4118 if (!path
->nodes
[level
])
4121 slot
= path
->slots
[level
] + 1;
4122 c
= path
->nodes
[level
];
4124 if (slot
>= btrfs_header_nritems(c
)) {
4127 struct btrfs_key cur_key
;
4128 if (level
+ 1 >= BTRFS_MAX_LEVEL
||
4129 !path
->nodes
[level
+ 1])
4132 if (path
->locks
[level
+ 1]) {
4137 slot
= btrfs_header_nritems(c
) - 1;
4139 btrfs_item_key_to_cpu(c
, &cur_key
, slot
);
4141 btrfs_node_key_to_cpu(c
, &cur_key
, slot
);
4143 orig_lowest
= path
->lowest_level
;
4144 btrfs_release_path(path
);
4145 path
->lowest_level
= level
;
4146 ret
= btrfs_search_slot(NULL
, root
, &cur_key
, path
,
4148 path
->lowest_level
= orig_lowest
;
4152 c
= path
->nodes
[level
];
4153 slot
= path
->slots
[level
];
4160 btrfs_item_key_to_cpu(c
, key
, slot
);
4162 u64 blockptr
= btrfs_node_blockptr(c
, slot
);
4163 u64 gen
= btrfs_node_ptr_generation(c
, slot
);
4166 struct extent_buffer
*cur
;
4167 cur
= btrfs_find_tree_block(root
, blockptr
,
4168 btrfs_level_size(root
, level
- 1));
4169 if (!cur
|| !btrfs_buffer_uptodate(cur
, gen
)) {
4172 free_extent_buffer(cur
);
4175 free_extent_buffer(cur
);
4177 if (gen
< min_trans
) {
4181 btrfs_node_key_to_cpu(c
, key
, slot
);
4189 * search the tree again to find a leaf with greater keys
4190 * returns 0 if it found something or 1 if there are no greater leaves.
4191 * returns < 0 on io errors.
4193 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
4197 struct extent_buffer
*c
;
4198 struct extent_buffer
*next
;
4199 struct btrfs_key key
;
4202 int old_spinning
= path
->leave_spinning
;
4203 int next_rw_lock
= 0;
4205 nritems
= btrfs_header_nritems(path
->nodes
[0]);
4209 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, nritems
- 1);
4214 btrfs_release_path(path
);
4216 path
->keep_locks
= 1;
4217 path
->leave_spinning
= 1;
4219 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4220 path
->keep_locks
= 0;
4225 nritems
= btrfs_header_nritems(path
->nodes
[0]);
4227 * by releasing the path above we dropped all our locks. A balance
4228 * could have added more items next to the key that used to be
4229 * at the very end of the block. So, check again here and
4230 * advance the path if there are now more items available.
4232 if (nritems
> 0 && path
->slots
[0] < nritems
- 1) {
4239 while (level
< BTRFS_MAX_LEVEL
) {
4240 if (!path
->nodes
[level
]) {
4245 slot
= path
->slots
[level
] + 1;
4246 c
= path
->nodes
[level
];
4247 if (slot
>= btrfs_header_nritems(c
)) {
4249 if (level
== BTRFS_MAX_LEVEL
) {
4257 btrfs_tree_unlock_rw(next
, next_rw_lock
);
4258 free_extent_buffer(next
);
4262 next_rw_lock
= path
->locks
[level
];
4263 ret
= read_block_for_search(NULL
, root
, path
, &next
, level
,
4269 btrfs_release_path(path
);
4273 if (!path
->skip_locking
) {
4274 ret
= btrfs_try_tree_read_lock(next
);
4276 btrfs_set_path_blocking(path
);
4277 btrfs_tree_read_lock(next
);
4278 btrfs_clear_path_blocking(path
, next
,
4281 next_rw_lock
= BTRFS_READ_LOCK
;
4285 path
->slots
[level
] = slot
;
4288 c
= path
->nodes
[level
];
4289 if (path
->locks
[level
])
4290 btrfs_tree_unlock_rw(c
, path
->locks
[level
]);
4292 free_extent_buffer(c
);
4293 path
->nodes
[level
] = next
;
4294 path
->slots
[level
] = 0;
4295 if (!path
->skip_locking
)
4296 path
->locks
[level
] = next_rw_lock
;
4300 ret
= read_block_for_search(NULL
, root
, path
, &next
, level
,
4306 btrfs_release_path(path
);
4310 if (!path
->skip_locking
) {
4311 ret
= btrfs_try_tree_read_lock(next
);
4313 btrfs_set_path_blocking(path
);
4314 btrfs_tree_read_lock(next
);
4315 btrfs_clear_path_blocking(path
, next
,
4318 next_rw_lock
= BTRFS_READ_LOCK
;
4323 unlock_up(path
, 0, 1, 0, NULL
);
4324 path
->leave_spinning
= old_spinning
;
4326 btrfs_set_path_blocking(path
);
4332 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4333 * searching until it gets past min_objectid or finds an item of 'type'
4335 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4337 int btrfs_previous_item(struct btrfs_root
*root
,
4338 struct btrfs_path
*path
, u64 min_objectid
,
4341 struct btrfs_key found_key
;
4342 struct extent_buffer
*leaf
;
4347 if (path
->slots
[0] == 0) {
4348 btrfs_set_path_blocking(path
);
4349 ret
= btrfs_prev_leaf(root
, path
);
4355 leaf
= path
->nodes
[0];
4356 nritems
= btrfs_header_nritems(leaf
);
4359 if (path
->slots
[0] == nritems
)
4362 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4363 if (found_key
.objectid
< min_objectid
)
4365 if (found_key
.type
== type
)
4367 if (found_key
.objectid
== min_objectid
&&
4368 found_key
.type
< type
)