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 int del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
40 struct btrfs_path
*path
, int level
, int slot
);
41 static int setup_items_for_insert(struct btrfs_trans_handle
*trans
,
42 struct btrfs_root
*root
, struct btrfs_path
*path
,
43 struct btrfs_key
*cpu_key
, u32
*data_size
,
44 u32 total_data
, u32 total_size
, int nr
);
47 struct btrfs_path
*btrfs_alloc_path(void)
49 struct btrfs_path
*path
;
50 path
= kmem_cache_zalloc(btrfs_path_cachep
, GFP_NOFS
);
57 * set all locked nodes in the path to blocking locks. This should
58 * be done before scheduling
60 noinline
void btrfs_set_path_blocking(struct btrfs_path
*p
)
63 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
64 if (p
->nodes
[i
] && p
->locks
[i
])
65 btrfs_set_lock_blocking(p
->nodes
[i
]);
70 * reset all the locked nodes in the patch to spinning locks.
72 * held is used to keep lockdep happy, when lockdep is enabled
73 * we set held to a blocking lock before we go around and
74 * retake all the spinlocks in the path. You can safely use NULL
77 noinline
void btrfs_clear_path_blocking(struct btrfs_path
*p
,
78 struct extent_buffer
*held
)
82 #ifdef CONFIG_DEBUG_LOCK_ALLOC
83 /* lockdep really cares that we take all of these spinlocks
84 * in the right order. If any of the locks in the path are not
85 * currently blocking, it is going to complain. So, make really
86 * really sure by forcing the path to blocking before we clear
90 btrfs_set_lock_blocking(held
);
91 btrfs_set_path_blocking(p
);
94 for (i
= BTRFS_MAX_LEVEL
- 1; i
>= 0; i
--) {
95 if (p
->nodes
[i
] && p
->locks
[i
])
96 btrfs_clear_lock_blocking(p
->nodes
[i
]);
99 #ifdef CONFIG_DEBUG_LOCK_ALLOC
101 btrfs_clear_lock_blocking(held
);
105 /* this also releases the path */
106 void btrfs_free_path(struct btrfs_path
*p
)
110 btrfs_release_path(NULL
, p
);
111 kmem_cache_free(btrfs_path_cachep
, p
);
115 * path release drops references on the extent buffers in the path
116 * and it drops any locks held by this path
118 * It is safe to call this on paths that no locks or extent buffers held.
120 noinline
void btrfs_release_path(struct btrfs_root
*root
, struct btrfs_path
*p
)
124 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
129 btrfs_tree_unlock(p
->nodes
[i
]);
132 free_extent_buffer(p
->nodes
[i
]);
138 * safely gets a reference on the root node of a tree. A lock
139 * is not taken, so a concurrent writer may put a different node
140 * at the root of the tree. See btrfs_lock_root_node for the
143 * The extent buffer returned by this has a reference taken, so
144 * it won't disappear. It may stop being the root of the tree
145 * at any time because there are no locks held.
147 struct extent_buffer
*btrfs_root_node(struct btrfs_root
*root
)
149 struct extent_buffer
*eb
;
152 eb
= rcu_dereference(root
->node
);
153 extent_buffer_get(eb
);
158 /* loop around taking references on and locking the root node of the
159 * tree until you end up with a lock on the root. A locked buffer
160 * is returned, with a reference held.
162 struct extent_buffer
*btrfs_lock_root_node(struct btrfs_root
*root
)
164 struct extent_buffer
*eb
;
167 eb
= btrfs_root_node(root
);
169 if (eb
== root
->node
)
171 btrfs_tree_unlock(eb
);
172 free_extent_buffer(eb
);
177 /* cowonly root (everything not a reference counted cow subvolume), just get
178 * put onto a simple dirty list. transaction.c walks this to make sure they
179 * get properly updated on disk.
181 static void add_root_to_dirty_list(struct btrfs_root
*root
)
183 if (root
->track_dirty
&& list_empty(&root
->dirty_list
)) {
184 list_add(&root
->dirty_list
,
185 &root
->fs_info
->dirty_cowonly_roots
);
190 * used by snapshot creation to make a copy of a root for a tree with
191 * a given objectid. The buffer with the new root node is returned in
192 * cow_ret, and this func returns zero on success or a negative error code.
194 int btrfs_copy_root(struct btrfs_trans_handle
*trans
,
195 struct btrfs_root
*root
,
196 struct extent_buffer
*buf
,
197 struct extent_buffer
**cow_ret
, u64 new_root_objectid
)
199 struct extent_buffer
*cow
;
202 struct btrfs_disk_key disk_key
;
204 WARN_ON(root
->ref_cows
&& trans
->transid
!=
205 root
->fs_info
->running_transaction
->transid
);
206 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
208 level
= btrfs_header_level(buf
);
210 btrfs_item_key(buf
, &disk_key
, 0);
212 btrfs_node_key(buf
, &disk_key
, 0);
214 cow
= btrfs_alloc_free_block(trans
, root
, buf
->len
, 0,
215 new_root_objectid
, &disk_key
, level
,
220 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
221 btrfs_set_header_bytenr(cow
, cow
->start
);
222 btrfs_set_header_generation(cow
, trans
->transid
);
223 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
224 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
225 BTRFS_HEADER_FLAG_RELOC
);
226 if (new_root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
227 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
229 btrfs_set_header_owner(cow
, new_root_objectid
);
231 write_extent_buffer(cow
, root
->fs_info
->fsid
,
232 (unsigned long)btrfs_header_fsid(cow
),
235 WARN_ON(btrfs_header_generation(buf
) > trans
->transid
);
236 if (new_root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
237 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
239 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
244 btrfs_mark_buffer_dirty(cow
);
250 * check if the tree block can be shared by multiple trees
252 int btrfs_block_can_be_shared(struct btrfs_root
*root
,
253 struct extent_buffer
*buf
)
256 * Tree blocks not in refernece counted trees and tree roots
257 * are never shared. If a block was allocated after the last
258 * snapshot and the block was not allocated by tree relocation,
259 * we know the block is not shared.
261 if (root
->ref_cows
&&
262 buf
!= root
->node
&& buf
!= root
->commit_root
&&
263 (btrfs_header_generation(buf
) <=
264 btrfs_root_last_snapshot(&root
->root_item
) ||
265 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)))
267 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
268 if (root
->ref_cows
&&
269 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
275 static noinline
int update_ref_for_cow(struct btrfs_trans_handle
*trans
,
276 struct btrfs_root
*root
,
277 struct extent_buffer
*buf
,
278 struct extent_buffer
*cow
,
288 * Backrefs update rules:
290 * Always use full backrefs for extent pointers in tree block
291 * allocated by tree relocation.
293 * If a shared tree block is no longer referenced by its owner
294 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
295 * use full backrefs for extent pointers in tree block.
297 * If a tree block is been relocating
298 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
299 * use full backrefs for extent pointers in tree block.
300 * The reason for this is some operations (such as drop tree)
301 * are only allowed for blocks use full backrefs.
304 if (btrfs_block_can_be_shared(root
, buf
)) {
305 ret
= btrfs_lookup_extent_info(trans
, root
, buf
->start
,
306 buf
->len
, &refs
, &flags
);
311 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
||
312 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
313 flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
318 owner
= btrfs_header_owner(buf
);
319 BUG_ON(owner
== BTRFS_TREE_RELOC_OBJECTID
&&
320 !(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
));
323 if ((owner
== root
->root_key
.objectid
||
324 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) &&
325 !(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
)) {
326 ret
= btrfs_inc_ref(trans
, root
, buf
, 1);
329 if (root
->root_key
.objectid
==
330 BTRFS_TREE_RELOC_OBJECTID
) {
331 ret
= btrfs_dec_ref(trans
, root
, buf
, 0);
333 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
336 new_flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
339 if (root
->root_key
.objectid
==
340 BTRFS_TREE_RELOC_OBJECTID
)
341 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
343 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
346 if (new_flags
!= 0) {
347 ret
= btrfs_set_disk_extent_flags(trans
, root
,
354 if (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
355 if (root
->root_key
.objectid
==
356 BTRFS_TREE_RELOC_OBJECTID
)
357 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
359 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
361 ret
= btrfs_dec_ref(trans
, root
, buf
, 1);
364 clean_tree_block(trans
, root
, buf
);
371 * does the dirty work in cow of a single block. The parent block (if
372 * supplied) is updated to point to the new cow copy. The new buffer is marked
373 * dirty and returned locked. If you modify the block it needs to be marked
376 * search_start -- an allocation hint for the new block
378 * empty_size -- a hint that you plan on doing more cow. This is the size in
379 * bytes the allocator should try to find free next to the block it returns.
380 * This is just a hint and may be ignored by the allocator.
382 static noinline
int __btrfs_cow_block(struct btrfs_trans_handle
*trans
,
383 struct btrfs_root
*root
,
384 struct extent_buffer
*buf
,
385 struct extent_buffer
*parent
, int parent_slot
,
386 struct extent_buffer
**cow_ret
,
387 u64 search_start
, u64 empty_size
)
389 struct btrfs_disk_key disk_key
;
390 struct extent_buffer
*cow
;
399 btrfs_assert_tree_locked(buf
);
401 WARN_ON(root
->ref_cows
&& trans
->transid
!=
402 root
->fs_info
->running_transaction
->transid
);
403 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
405 level
= btrfs_header_level(buf
);
408 btrfs_item_key(buf
, &disk_key
, 0);
410 btrfs_node_key(buf
, &disk_key
, 0);
412 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
414 parent_start
= parent
->start
;
420 cow
= btrfs_alloc_free_block(trans
, root
, buf
->len
, parent_start
,
421 root
->root_key
.objectid
, &disk_key
,
422 level
, search_start
, empty_size
);
426 /* cow is set to blocking by btrfs_init_new_buffer */
428 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
429 btrfs_set_header_bytenr(cow
, cow
->start
);
430 btrfs_set_header_generation(cow
, trans
->transid
);
431 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
432 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
433 BTRFS_HEADER_FLAG_RELOC
);
434 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
435 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
437 btrfs_set_header_owner(cow
, root
->root_key
.objectid
);
439 write_extent_buffer(cow
, root
->fs_info
->fsid
,
440 (unsigned long)btrfs_header_fsid(cow
),
443 update_ref_for_cow(trans
, root
, buf
, cow
, &last_ref
);
446 btrfs_reloc_cow_block(trans
, root
, buf
, cow
);
448 if (buf
== root
->node
) {
449 WARN_ON(parent
&& parent
!= buf
);
450 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
||
451 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
452 parent_start
= buf
->start
;
456 extent_buffer_get(cow
);
457 rcu_assign_pointer(root
->node
, cow
);
459 btrfs_free_tree_block(trans
, root
, buf
, parent_start
,
461 free_extent_buffer(buf
);
462 add_root_to_dirty_list(root
);
464 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
465 parent_start
= parent
->start
;
469 WARN_ON(trans
->transid
!= btrfs_header_generation(parent
));
470 btrfs_set_node_blockptr(parent
, parent_slot
,
472 btrfs_set_node_ptr_generation(parent
, parent_slot
,
474 btrfs_mark_buffer_dirty(parent
);
475 btrfs_free_tree_block(trans
, root
, buf
, parent_start
,
479 btrfs_tree_unlock(buf
);
480 free_extent_buffer(buf
);
481 btrfs_mark_buffer_dirty(cow
);
486 static inline int should_cow_block(struct btrfs_trans_handle
*trans
,
487 struct btrfs_root
*root
,
488 struct extent_buffer
*buf
)
490 if (btrfs_header_generation(buf
) == trans
->transid
&&
491 !btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
) &&
492 !(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
&&
493 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)))
499 * cows a single block, see __btrfs_cow_block for the real work.
500 * This version of it has extra checks so that a block isn't cow'd more than
501 * once per transaction, as long as it hasn't been written yet
503 noinline
int btrfs_cow_block(struct btrfs_trans_handle
*trans
,
504 struct btrfs_root
*root
, struct extent_buffer
*buf
,
505 struct extent_buffer
*parent
, int parent_slot
,
506 struct extent_buffer
**cow_ret
)
511 if (trans
->transaction
!= root
->fs_info
->running_transaction
) {
512 printk(KERN_CRIT
"trans %llu running %llu\n",
513 (unsigned long long)trans
->transid
,
515 root
->fs_info
->running_transaction
->transid
);
518 if (trans
->transid
!= root
->fs_info
->generation
) {
519 printk(KERN_CRIT
"trans %llu running %llu\n",
520 (unsigned long long)trans
->transid
,
521 (unsigned long long)root
->fs_info
->generation
);
525 if (!should_cow_block(trans
, root
, buf
)) {
530 search_start
= buf
->start
& ~((u64
)(1024 * 1024 * 1024) - 1);
533 btrfs_set_lock_blocking(parent
);
534 btrfs_set_lock_blocking(buf
);
536 ret
= __btrfs_cow_block(trans
, root
, buf
, parent
,
537 parent_slot
, cow_ret
, search_start
, 0);
539 trace_btrfs_cow_block(root
, buf
, *cow_ret
);
545 * helper function for defrag to decide if two blocks pointed to by a
546 * node are actually close by
548 static int close_blocks(u64 blocknr
, u64 other
, u32 blocksize
)
550 if (blocknr
< other
&& other
- (blocknr
+ blocksize
) < 32768)
552 if (blocknr
> other
&& blocknr
- (other
+ blocksize
) < 32768)
558 * compare two keys in a memcmp fashion
560 static int comp_keys(struct btrfs_disk_key
*disk
, struct btrfs_key
*k2
)
564 btrfs_disk_key_to_cpu(&k1
, disk
);
566 return btrfs_comp_cpu_keys(&k1
, k2
);
570 * same as comp_keys only with two btrfs_key's
572 int btrfs_comp_cpu_keys(struct btrfs_key
*k1
, struct btrfs_key
*k2
)
574 if (k1
->objectid
> k2
->objectid
)
576 if (k1
->objectid
< k2
->objectid
)
578 if (k1
->type
> k2
->type
)
580 if (k1
->type
< k2
->type
)
582 if (k1
->offset
> k2
->offset
)
584 if (k1
->offset
< k2
->offset
)
590 * this is used by the defrag code to go through all the
591 * leaves pointed to by a node and reallocate them so that
592 * disk order is close to key order
594 int btrfs_realloc_node(struct btrfs_trans_handle
*trans
,
595 struct btrfs_root
*root
, struct extent_buffer
*parent
,
596 int start_slot
, int cache_only
, u64
*last_ret
,
597 struct btrfs_key
*progress
)
599 struct extent_buffer
*cur
;
602 u64 search_start
= *last_ret
;
612 int progress_passed
= 0;
613 struct btrfs_disk_key disk_key
;
615 parent_level
= btrfs_header_level(parent
);
616 if (cache_only
&& parent_level
!= 1)
619 if (trans
->transaction
!= root
->fs_info
->running_transaction
)
621 if (trans
->transid
!= root
->fs_info
->generation
)
624 parent_nritems
= btrfs_header_nritems(parent
);
625 blocksize
= btrfs_level_size(root
, parent_level
- 1);
626 end_slot
= parent_nritems
;
628 if (parent_nritems
== 1)
631 btrfs_set_lock_blocking(parent
);
633 for (i
= start_slot
; i
< end_slot
; i
++) {
636 if (!parent
->map_token
) {
637 map_extent_buffer(parent
,
638 btrfs_node_key_ptr_offset(i
),
639 sizeof(struct btrfs_key_ptr
),
640 &parent
->map_token
, &parent
->kaddr
,
641 &parent
->map_start
, &parent
->map_len
,
644 btrfs_node_key(parent
, &disk_key
, i
);
645 if (!progress_passed
&& comp_keys(&disk_key
, progress
) < 0)
649 blocknr
= btrfs_node_blockptr(parent
, i
);
650 gen
= btrfs_node_ptr_generation(parent
, i
);
652 last_block
= blocknr
;
655 other
= btrfs_node_blockptr(parent
, i
- 1);
656 close
= close_blocks(blocknr
, other
, blocksize
);
658 if (!close
&& i
< end_slot
- 2) {
659 other
= btrfs_node_blockptr(parent
, i
+ 1);
660 close
= close_blocks(blocknr
, other
, blocksize
);
663 last_block
= blocknr
;
666 if (parent
->map_token
) {
667 unmap_extent_buffer(parent
, parent
->map_token
,
669 parent
->map_token
= NULL
;
672 cur
= btrfs_find_tree_block(root
, blocknr
, blocksize
);
674 uptodate
= btrfs_buffer_uptodate(cur
, gen
);
677 if (!cur
|| !uptodate
) {
679 free_extent_buffer(cur
);
683 cur
= read_tree_block(root
, blocknr
,
687 } else if (!uptodate
) {
688 btrfs_read_buffer(cur
, gen
);
691 if (search_start
== 0)
692 search_start
= last_block
;
694 btrfs_tree_lock(cur
);
695 btrfs_set_lock_blocking(cur
);
696 err
= __btrfs_cow_block(trans
, root
, cur
, parent
, i
,
699 (end_slot
- i
) * blocksize
));
701 btrfs_tree_unlock(cur
);
702 free_extent_buffer(cur
);
705 search_start
= cur
->start
;
706 last_block
= cur
->start
;
707 *last_ret
= search_start
;
708 btrfs_tree_unlock(cur
);
709 free_extent_buffer(cur
);
711 if (parent
->map_token
) {
712 unmap_extent_buffer(parent
, parent
->map_token
,
714 parent
->map_token
= NULL
;
720 * The leaf data grows from end-to-front in the node.
721 * this returns the address of the start of the last item,
722 * which is the stop of the leaf data stack
724 static inline unsigned int leaf_data_end(struct btrfs_root
*root
,
725 struct extent_buffer
*leaf
)
727 u32 nr
= btrfs_header_nritems(leaf
);
729 return BTRFS_LEAF_DATA_SIZE(root
);
730 return btrfs_item_offset_nr(leaf
, nr
- 1);
735 * search for key in the extent_buffer. The items start at offset p,
736 * and they are item_size apart. There are 'max' items in p.
738 * the slot in the array is returned via slot, and it points to
739 * the place where you would insert key if it is not found in
742 * slot may point to max if the key is bigger than all of the keys
744 static noinline
int generic_bin_search(struct extent_buffer
*eb
,
746 int item_size
, struct btrfs_key
*key
,
753 struct btrfs_disk_key
*tmp
= NULL
;
754 struct btrfs_disk_key unaligned
;
755 unsigned long offset
;
756 char *map_token
= NULL
;
758 unsigned long map_start
= 0;
759 unsigned long map_len
= 0;
763 mid
= (low
+ high
) / 2;
764 offset
= p
+ mid
* item_size
;
766 if (!map_token
|| offset
< map_start
||
767 (offset
+ sizeof(struct btrfs_disk_key
)) >
768 map_start
+ map_len
) {
770 unmap_extent_buffer(eb
, map_token
, KM_USER0
);
774 err
= map_private_extent_buffer(eb
, offset
,
775 sizeof(struct btrfs_disk_key
),
777 &map_start
, &map_len
, KM_USER0
);
780 tmp
= (struct btrfs_disk_key
*)(kaddr
+ offset
-
783 read_extent_buffer(eb
, &unaligned
,
784 offset
, sizeof(unaligned
));
789 tmp
= (struct btrfs_disk_key
*)(kaddr
+ offset
-
792 ret
= comp_keys(tmp
, key
);
801 unmap_extent_buffer(eb
, map_token
, KM_USER0
);
807 unmap_extent_buffer(eb
, map_token
, KM_USER0
);
812 * simple bin_search frontend that does the right thing for
815 static int bin_search(struct extent_buffer
*eb
, struct btrfs_key
*key
,
816 int level
, int *slot
)
819 return generic_bin_search(eb
,
820 offsetof(struct btrfs_leaf
, items
),
821 sizeof(struct btrfs_item
),
822 key
, btrfs_header_nritems(eb
),
825 return generic_bin_search(eb
,
826 offsetof(struct btrfs_node
, ptrs
),
827 sizeof(struct btrfs_key_ptr
),
828 key
, btrfs_header_nritems(eb
),
834 int btrfs_bin_search(struct extent_buffer
*eb
, struct btrfs_key
*key
,
835 int level
, int *slot
)
837 return bin_search(eb
, key
, level
, slot
);
840 static void root_add_used(struct btrfs_root
*root
, u32 size
)
842 spin_lock(&root
->accounting_lock
);
843 btrfs_set_root_used(&root
->root_item
,
844 btrfs_root_used(&root
->root_item
) + size
);
845 spin_unlock(&root
->accounting_lock
);
848 static void root_sub_used(struct btrfs_root
*root
, u32 size
)
850 spin_lock(&root
->accounting_lock
);
851 btrfs_set_root_used(&root
->root_item
,
852 btrfs_root_used(&root
->root_item
) - size
);
853 spin_unlock(&root
->accounting_lock
);
856 /* given a node and slot number, this reads the blocks it points to. The
857 * extent buffer is returned with a reference taken (but unlocked).
858 * NULL is returned on error.
860 static noinline
struct extent_buffer
*read_node_slot(struct btrfs_root
*root
,
861 struct extent_buffer
*parent
, int slot
)
863 int level
= btrfs_header_level(parent
);
866 if (slot
>= btrfs_header_nritems(parent
))
871 return read_tree_block(root
, btrfs_node_blockptr(parent
, slot
),
872 btrfs_level_size(root
, level
- 1),
873 btrfs_node_ptr_generation(parent
, slot
));
877 * node level balancing, used to make sure nodes are in proper order for
878 * item deletion. We balance from the top down, so we have to make sure
879 * that a deletion won't leave an node completely empty later on.
881 static noinline
int balance_level(struct btrfs_trans_handle
*trans
,
882 struct btrfs_root
*root
,
883 struct btrfs_path
*path
, int level
)
885 struct extent_buffer
*right
= NULL
;
886 struct extent_buffer
*mid
;
887 struct extent_buffer
*left
= NULL
;
888 struct extent_buffer
*parent
= NULL
;
892 int orig_slot
= path
->slots
[level
];
898 mid
= path
->nodes
[level
];
900 WARN_ON(!path
->locks
[level
]);
901 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
903 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
905 if (level
< BTRFS_MAX_LEVEL
- 1)
906 parent
= path
->nodes
[level
+ 1];
907 pslot
= path
->slots
[level
+ 1];
910 * deal with the case where there is only one pointer in the root
911 * by promoting the node below to a root
914 struct extent_buffer
*child
;
916 if (btrfs_header_nritems(mid
) != 1)
919 /* promote the child to a root */
920 child
= read_node_slot(root
, mid
, 0);
922 btrfs_tree_lock(child
);
923 btrfs_set_lock_blocking(child
);
924 ret
= btrfs_cow_block(trans
, root
, child
, mid
, 0, &child
);
926 btrfs_tree_unlock(child
);
927 free_extent_buffer(child
);
931 rcu_assign_pointer(root
->node
, child
);
933 add_root_to_dirty_list(root
);
934 btrfs_tree_unlock(child
);
936 path
->locks
[level
] = 0;
937 path
->nodes
[level
] = NULL
;
938 clean_tree_block(trans
, root
, mid
);
939 btrfs_tree_unlock(mid
);
940 /* once for the path */
941 free_extent_buffer(mid
);
943 root_sub_used(root
, mid
->len
);
944 btrfs_free_tree_block(trans
, root
, mid
, 0, 1);
945 /* once for the root ptr */
946 free_extent_buffer(mid
);
949 if (btrfs_header_nritems(mid
) >
950 BTRFS_NODEPTRS_PER_BLOCK(root
) / 4)
953 btrfs_header_nritems(mid
);
955 left
= read_node_slot(root
, parent
, pslot
- 1);
957 btrfs_tree_lock(left
);
958 btrfs_set_lock_blocking(left
);
959 wret
= btrfs_cow_block(trans
, root
, left
,
960 parent
, pslot
- 1, &left
);
966 right
= read_node_slot(root
, parent
, pslot
+ 1);
968 btrfs_tree_lock(right
);
969 btrfs_set_lock_blocking(right
);
970 wret
= btrfs_cow_block(trans
, root
, right
,
971 parent
, pslot
+ 1, &right
);
978 /* first, try to make some room in the middle buffer */
980 orig_slot
+= btrfs_header_nritems(left
);
981 wret
= push_node_left(trans
, root
, left
, mid
, 1);
984 btrfs_header_nritems(mid
);
988 * then try to empty the right most buffer into the middle
991 wret
= push_node_left(trans
, root
, mid
, right
, 1);
992 if (wret
< 0 && wret
!= -ENOSPC
)
994 if (btrfs_header_nritems(right
) == 0) {
995 clean_tree_block(trans
, root
, right
);
996 btrfs_tree_unlock(right
);
997 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
+
1001 root_sub_used(root
, right
->len
);
1002 btrfs_free_tree_block(trans
, root
, right
, 0, 1);
1003 free_extent_buffer(right
);
1006 struct btrfs_disk_key right_key
;
1007 btrfs_node_key(right
, &right_key
, 0);
1008 btrfs_set_node_key(parent
, &right_key
, pslot
+ 1);
1009 btrfs_mark_buffer_dirty(parent
);
1012 if (btrfs_header_nritems(mid
) == 1) {
1014 * we're not allowed to leave a node with one item in the
1015 * tree during a delete. A deletion from lower in the tree
1016 * could try to delete the only pointer in this node.
1017 * So, pull some keys from the left.
1018 * There has to be a left pointer at this point because
1019 * otherwise we would have pulled some pointers from the
1023 wret
= balance_node_right(trans
, root
, mid
, left
);
1029 wret
= push_node_left(trans
, root
, left
, mid
, 1);
1035 if (btrfs_header_nritems(mid
) == 0) {
1036 clean_tree_block(trans
, root
, mid
);
1037 btrfs_tree_unlock(mid
);
1038 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
);
1041 root_sub_used(root
, mid
->len
);
1042 btrfs_free_tree_block(trans
, root
, mid
, 0, 1);
1043 free_extent_buffer(mid
);
1046 /* update the parent key to reflect our changes */
1047 struct btrfs_disk_key mid_key
;
1048 btrfs_node_key(mid
, &mid_key
, 0);
1049 btrfs_set_node_key(parent
, &mid_key
, pslot
);
1050 btrfs_mark_buffer_dirty(parent
);
1053 /* update the path */
1055 if (btrfs_header_nritems(left
) > orig_slot
) {
1056 extent_buffer_get(left
);
1057 /* left was locked after cow */
1058 path
->nodes
[level
] = left
;
1059 path
->slots
[level
+ 1] -= 1;
1060 path
->slots
[level
] = orig_slot
;
1062 btrfs_tree_unlock(mid
);
1063 free_extent_buffer(mid
);
1066 orig_slot
-= btrfs_header_nritems(left
);
1067 path
->slots
[level
] = orig_slot
;
1070 /* double check we haven't messed things up */
1072 btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]))
1076 btrfs_tree_unlock(right
);
1077 free_extent_buffer(right
);
1080 if (path
->nodes
[level
] != left
)
1081 btrfs_tree_unlock(left
);
1082 free_extent_buffer(left
);
1087 /* Node balancing for insertion. Here we only split or push nodes around
1088 * when they are completely full. This is also done top down, so we
1089 * have to be pessimistic.
1091 static noinline
int push_nodes_for_insert(struct btrfs_trans_handle
*trans
,
1092 struct btrfs_root
*root
,
1093 struct btrfs_path
*path
, int level
)
1095 struct extent_buffer
*right
= NULL
;
1096 struct extent_buffer
*mid
;
1097 struct extent_buffer
*left
= NULL
;
1098 struct extent_buffer
*parent
= NULL
;
1102 int orig_slot
= path
->slots
[level
];
1107 mid
= path
->nodes
[level
];
1108 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
1110 if (level
< BTRFS_MAX_LEVEL
- 1)
1111 parent
= path
->nodes
[level
+ 1];
1112 pslot
= path
->slots
[level
+ 1];
1117 left
= read_node_slot(root
, parent
, pslot
- 1);
1119 /* first, try to make some room in the middle buffer */
1123 btrfs_tree_lock(left
);
1124 btrfs_set_lock_blocking(left
);
1126 left_nr
= btrfs_header_nritems(left
);
1127 if (left_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
1130 ret
= btrfs_cow_block(trans
, root
, left
, parent
,
1135 wret
= push_node_left(trans
, root
,
1142 struct btrfs_disk_key disk_key
;
1143 orig_slot
+= left_nr
;
1144 btrfs_node_key(mid
, &disk_key
, 0);
1145 btrfs_set_node_key(parent
, &disk_key
, pslot
);
1146 btrfs_mark_buffer_dirty(parent
);
1147 if (btrfs_header_nritems(left
) > orig_slot
) {
1148 path
->nodes
[level
] = left
;
1149 path
->slots
[level
+ 1] -= 1;
1150 path
->slots
[level
] = orig_slot
;
1151 btrfs_tree_unlock(mid
);
1152 free_extent_buffer(mid
);
1155 btrfs_header_nritems(left
);
1156 path
->slots
[level
] = orig_slot
;
1157 btrfs_tree_unlock(left
);
1158 free_extent_buffer(left
);
1162 btrfs_tree_unlock(left
);
1163 free_extent_buffer(left
);
1165 right
= read_node_slot(root
, parent
, pslot
+ 1);
1168 * then try to empty the right most buffer into the middle
1173 btrfs_tree_lock(right
);
1174 btrfs_set_lock_blocking(right
);
1176 right_nr
= btrfs_header_nritems(right
);
1177 if (right_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
1180 ret
= btrfs_cow_block(trans
, root
, right
,
1186 wret
= balance_node_right(trans
, root
,
1193 struct btrfs_disk_key disk_key
;
1195 btrfs_node_key(right
, &disk_key
, 0);
1196 btrfs_set_node_key(parent
, &disk_key
, pslot
+ 1);
1197 btrfs_mark_buffer_dirty(parent
);
1199 if (btrfs_header_nritems(mid
) <= orig_slot
) {
1200 path
->nodes
[level
] = right
;
1201 path
->slots
[level
+ 1] += 1;
1202 path
->slots
[level
] = orig_slot
-
1203 btrfs_header_nritems(mid
);
1204 btrfs_tree_unlock(mid
);
1205 free_extent_buffer(mid
);
1207 btrfs_tree_unlock(right
);
1208 free_extent_buffer(right
);
1212 btrfs_tree_unlock(right
);
1213 free_extent_buffer(right
);
1219 * readahead one full node of leaves, finding things that are close
1220 * to the block in 'slot', and triggering ra on them.
1222 static void reada_for_search(struct btrfs_root
*root
,
1223 struct btrfs_path
*path
,
1224 int level
, int slot
, u64 objectid
)
1226 struct extent_buffer
*node
;
1227 struct btrfs_disk_key disk_key
;
1232 int direction
= path
->reada
;
1233 struct extent_buffer
*eb
;
1241 if (!path
->nodes
[level
])
1244 node
= path
->nodes
[level
];
1246 search
= btrfs_node_blockptr(node
, slot
);
1247 blocksize
= btrfs_level_size(root
, level
- 1);
1248 eb
= btrfs_find_tree_block(root
, search
, blocksize
);
1250 free_extent_buffer(eb
);
1256 nritems
= btrfs_header_nritems(node
);
1259 if (direction
< 0) {
1263 } else if (direction
> 0) {
1268 if (path
->reada
< 0 && objectid
) {
1269 btrfs_node_key(node
, &disk_key
, nr
);
1270 if (btrfs_disk_key_objectid(&disk_key
) != objectid
)
1273 search
= btrfs_node_blockptr(node
, nr
);
1274 if ((search
<= target
&& target
- search
<= 65536) ||
1275 (search
> target
&& search
- target
<= 65536)) {
1276 readahead_tree_block(root
, search
, blocksize
,
1277 btrfs_node_ptr_generation(node
, nr
));
1281 if ((nread
> 65536 || nscan
> 32))
1287 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1290 static noinline
int reada_for_balance(struct btrfs_root
*root
,
1291 struct btrfs_path
*path
, int level
)
1295 struct extent_buffer
*parent
;
1296 struct extent_buffer
*eb
;
1303 parent
= path
->nodes
[level
+ 1];
1307 nritems
= btrfs_header_nritems(parent
);
1308 slot
= path
->slots
[level
+ 1];
1309 blocksize
= btrfs_level_size(root
, level
);
1312 block1
= btrfs_node_blockptr(parent
, slot
- 1);
1313 gen
= btrfs_node_ptr_generation(parent
, slot
- 1);
1314 eb
= btrfs_find_tree_block(root
, block1
, blocksize
);
1315 if (eb
&& btrfs_buffer_uptodate(eb
, gen
))
1317 free_extent_buffer(eb
);
1319 if (slot
+ 1 < nritems
) {
1320 block2
= btrfs_node_blockptr(parent
, slot
+ 1);
1321 gen
= btrfs_node_ptr_generation(parent
, slot
+ 1);
1322 eb
= btrfs_find_tree_block(root
, block2
, blocksize
);
1323 if (eb
&& btrfs_buffer_uptodate(eb
, gen
))
1325 free_extent_buffer(eb
);
1327 if (block1
|| block2
) {
1330 /* release the whole path */
1331 btrfs_release_path(root
, path
);
1333 /* read the blocks */
1335 readahead_tree_block(root
, block1
, blocksize
, 0);
1337 readahead_tree_block(root
, block2
, blocksize
, 0);
1340 eb
= read_tree_block(root
, block1
, blocksize
, 0);
1341 free_extent_buffer(eb
);
1344 eb
= read_tree_block(root
, block2
, blocksize
, 0);
1345 free_extent_buffer(eb
);
1353 * when we walk down the tree, it is usually safe to unlock the higher layers
1354 * in the tree. The exceptions are when our path goes through slot 0, because
1355 * operations on the tree might require changing key pointers higher up in the
1358 * callers might also have set path->keep_locks, which tells this code to keep
1359 * the lock if the path points to the last slot in the block. This is part of
1360 * walking through the tree, and selecting the next slot in the higher block.
1362 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1363 * if lowest_unlock is 1, level 0 won't be unlocked
1365 static noinline
void unlock_up(struct btrfs_path
*path
, int level
,
1369 int skip_level
= level
;
1371 struct extent_buffer
*t
;
1373 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1374 if (!path
->nodes
[i
])
1376 if (!path
->locks
[i
])
1378 if (!no_skips
&& path
->slots
[i
] == 0) {
1382 if (!no_skips
&& path
->keep_locks
) {
1385 nritems
= btrfs_header_nritems(t
);
1386 if (nritems
< 1 || path
->slots
[i
] >= nritems
- 1) {
1391 if (skip_level
< i
&& i
>= lowest_unlock
)
1395 if (i
>= lowest_unlock
&& i
> skip_level
&& path
->locks
[i
]) {
1396 btrfs_tree_unlock(t
);
1403 * This releases any locks held in the path starting at level and
1404 * going all the way up to the root.
1406 * btrfs_search_slot will keep the lock held on higher nodes in a few
1407 * corner cases, such as COW of the block at slot zero in the node. This
1408 * ignores those rules, and it should only be called when there are no
1409 * more updates to be done higher up in the tree.
1411 noinline
void btrfs_unlock_up_safe(struct btrfs_path
*path
, int level
)
1415 if (path
->keep_locks
)
1418 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1419 if (!path
->nodes
[i
])
1421 if (!path
->locks
[i
])
1423 btrfs_tree_unlock(path
->nodes
[i
]);
1429 * helper function for btrfs_search_slot. The goal is to find a block
1430 * in cache without setting the path to blocking. If we find the block
1431 * we return zero and the path is unchanged.
1433 * If we can't find the block, we set the path blocking and do some
1434 * reada. -EAGAIN is returned and the search must be repeated.
1437 read_block_for_search(struct btrfs_trans_handle
*trans
,
1438 struct btrfs_root
*root
, struct btrfs_path
*p
,
1439 struct extent_buffer
**eb_ret
, int level
, int slot
,
1440 struct btrfs_key
*key
)
1445 struct extent_buffer
*b
= *eb_ret
;
1446 struct extent_buffer
*tmp
;
1449 blocknr
= btrfs_node_blockptr(b
, slot
);
1450 gen
= btrfs_node_ptr_generation(b
, slot
);
1451 blocksize
= btrfs_level_size(root
, level
- 1);
1453 tmp
= btrfs_find_tree_block(root
, blocknr
, blocksize
);
1455 if (btrfs_buffer_uptodate(tmp
, 0)) {
1456 if (btrfs_buffer_uptodate(tmp
, gen
)) {
1458 * we found an up to date block without
1465 /* the pages were up to date, but we failed
1466 * the generation number check. Do a full
1467 * read for the generation number that is correct.
1468 * We must do this without dropping locks so
1469 * we can trust our generation number
1471 free_extent_buffer(tmp
);
1472 tmp
= read_tree_block(root
, blocknr
, blocksize
, gen
);
1473 if (tmp
&& btrfs_buffer_uptodate(tmp
, gen
)) {
1477 free_extent_buffer(tmp
);
1478 btrfs_release_path(NULL
, p
);
1484 * reduce lock contention at high levels
1485 * of the btree by dropping locks before
1486 * we read. Don't release the lock on the current
1487 * level because we need to walk this node to figure
1488 * out which blocks to read.
1490 btrfs_unlock_up_safe(p
, level
+ 1);
1491 btrfs_set_path_blocking(p
);
1493 free_extent_buffer(tmp
);
1495 reada_for_search(root
, p
, level
, slot
, key
->objectid
);
1497 btrfs_release_path(NULL
, p
);
1500 tmp
= read_tree_block(root
, blocknr
, blocksize
, 0);
1503 * If the read above didn't mark this buffer up to date,
1504 * it will never end up being up to date. Set ret to EIO now
1505 * and give up so that our caller doesn't loop forever
1508 if (!btrfs_buffer_uptodate(tmp
, 0))
1510 free_extent_buffer(tmp
);
1516 * helper function for btrfs_search_slot. This does all of the checks
1517 * for node-level blocks and does any balancing required based on
1520 * If no extra work was required, zero is returned. If we had to
1521 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1525 setup_nodes_for_search(struct btrfs_trans_handle
*trans
,
1526 struct btrfs_root
*root
, struct btrfs_path
*p
,
1527 struct extent_buffer
*b
, int level
, int ins_len
)
1530 if ((p
->search_for_split
|| ins_len
> 0) && btrfs_header_nritems(b
) >=
1531 BTRFS_NODEPTRS_PER_BLOCK(root
) - 3) {
1534 sret
= reada_for_balance(root
, p
, level
);
1538 btrfs_set_path_blocking(p
);
1539 sret
= split_node(trans
, root
, p
, level
);
1540 btrfs_clear_path_blocking(p
, NULL
);
1547 b
= p
->nodes
[level
];
1548 } else if (ins_len
< 0 && btrfs_header_nritems(b
) <
1549 BTRFS_NODEPTRS_PER_BLOCK(root
) / 2) {
1552 sret
= reada_for_balance(root
, p
, level
);
1556 btrfs_set_path_blocking(p
);
1557 sret
= balance_level(trans
, root
, p
, level
);
1558 btrfs_clear_path_blocking(p
, NULL
);
1564 b
= p
->nodes
[level
];
1566 btrfs_release_path(NULL
, p
);
1569 BUG_ON(btrfs_header_nritems(b
) == 1);
1580 * look for key in the tree. path is filled in with nodes along the way
1581 * if key is found, we return zero and you can find the item in the leaf
1582 * level of the path (level 0)
1584 * If the key isn't found, the path points to the slot where it should
1585 * be inserted, and 1 is returned. If there are other errors during the
1586 * search a negative error number is returned.
1588 * if ins_len > 0, nodes and leaves will be split as we walk down the
1589 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1592 int btrfs_search_slot(struct btrfs_trans_handle
*trans
, struct btrfs_root
1593 *root
, struct btrfs_key
*key
, struct btrfs_path
*p
, int
1596 struct extent_buffer
*b
;
1601 int lowest_unlock
= 1;
1602 u8 lowest_level
= 0;
1604 lowest_level
= p
->lowest_level
;
1605 WARN_ON(lowest_level
&& ins_len
> 0);
1606 WARN_ON(p
->nodes
[0] != NULL
);
1612 if (p
->search_commit_root
) {
1613 b
= root
->commit_root
;
1614 extent_buffer_get(b
);
1615 if (!p
->skip_locking
)
1618 if (p
->skip_locking
)
1619 b
= btrfs_root_node(root
);
1621 b
= btrfs_lock_root_node(root
);
1625 level
= btrfs_header_level(b
);
1628 * setup the path here so we can release it under lock
1629 * contention with the cow code
1631 p
->nodes
[level
] = b
;
1632 if (!p
->skip_locking
)
1633 p
->locks
[level
] = 1;
1637 * if we don't really need to cow this block
1638 * then we don't want to set the path blocking,
1639 * so we test it here
1641 if (!should_cow_block(trans
, root
, b
))
1644 btrfs_set_path_blocking(p
);
1646 err
= btrfs_cow_block(trans
, root
, b
,
1647 p
->nodes
[level
+ 1],
1648 p
->slots
[level
+ 1], &b
);
1655 BUG_ON(!cow
&& ins_len
);
1656 if (level
!= btrfs_header_level(b
))
1658 level
= btrfs_header_level(b
);
1660 p
->nodes
[level
] = b
;
1661 if (!p
->skip_locking
)
1662 p
->locks
[level
] = 1;
1664 btrfs_clear_path_blocking(p
, NULL
);
1667 * we have a lock on b and as long as we aren't changing
1668 * the tree, there is no way to for the items in b to change.
1669 * It is safe to drop the lock on our parent before we
1670 * go through the expensive btree search on b.
1672 * If cow is true, then we might be changing slot zero,
1673 * which may require changing the parent. So, we can't
1674 * drop the lock until after we know which slot we're
1678 btrfs_unlock_up_safe(p
, level
+ 1);
1680 ret
= bin_search(b
, key
, level
, &slot
);
1684 if (ret
&& slot
> 0) {
1688 p
->slots
[level
] = slot
;
1689 err
= setup_nodes_for_search(trans
, root
, p
, b
, level
,
1697 b
= p
->nodes
[level
];
1698 slot
= p
->slots
[level
];
1700 unlock_up(p
, level
, lowest_unlock
);
1702 if (level
== lowest_level
) {
1708 err
= read_block_for_search(trans
, root
, p
,
1709 &b
, level
, slot
, key
);
1717 if (!p
->skip_locking
) {
1718 btrfs_clear_path_blocking(p
, NULL
);
1719 err
= btrfs_try_spin_lock(b
);
1722 btrfs_set_path_blocking(p
);
1724 btrfs_clear_path_blocking(p
, b
);
1728 p
->slots
[level
] = slot
;
1730 btrfs_leaf_free_space(root
, b
) < ins_len
) {
1731 btrfs_set_path_blocking(p
);
1732 err
= split_leaf(trans
, root
, key
,
1733 p
, ins_len
, ret
== 0);
1734 btrfs_clear_path_blocking(p
, NULL
);
1742 if (!p
->search_for_split
)
1743 unlock_up(p
, level
, lowest_unlock
);
1750 * we don't really know what they plan on doing with the path
1751 * from here on, so for now just mark it as blocking
1753 if (!p
->leave_spinning
)
1754 btrfs_set_path_blocking(p
);
1756 btrfs_release_path(root
, p
);
1761 * adjust the pointers going up the tree, starting at level
1762 * making sure the right key of each node is points to 'key'.
1763 * This is used after shifting pointers to the left, so it stops
1764 * fixing up pointers when a given leaf/node is not in slot 0 of the
1767 * If this fails to write a tree block, it returns -1, but continues
1768 * fixing up the blocks in ram so the tree is consistent.
1770 static int fixup_low_keys(struct btrfs_trans_handle
*trans
,
1771 struct btrfs_root
*root
, struct btrfs_path
*path
,
1772 struct btrfs_disk_key
*key
, int level
)
1776 struct extent_buffer
*t
;
1778 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1779 int tslot
= path
->slots
[i
];
1780 if (!path
->nodes
[i
])
1783 btrfs_set_node_key(t
, key
, tslot
);
1784 btrfs_mark_buffer_dirty(path
->nodes
[i
]);
1794 * This function isn't completely safe. It's the caller's responsibility
1795 * that the new key won't break the order
1797 int btrfs_set_item_key_safe(struct btrfs_trans_handle
*trans
,
1798 struct btrfs_root
*root
, struct btrfs_path
*path
,
1799 struct btrfs_key
*new_key
)
1801 struct btrfs_disk_key disk_key
;
1802 struct extent_buffer
*eb
;
1805 eb
= path
->nodes
[0];
1806 slot
= path
->slots
[0];
1808 btrfs_item_key(eb
, &disk_key
, slot
- 1);
1809 if (comp_keys(&disk_key
, new_key
) >= 0)
1812 if (slot
< btrfs_header_nritems(eb
) - 1) {
1813 btrfs_item_key(eb
, &disk_key
, slot
+ 1);
1814 if (comp_keys(&disk_key
, new_key
) <= 0)
1818 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
1819 btrfs_set_item_key(eb
, &disk_key
, slot
);
1820 btrfs_mark_buffer_dirty(eb
);
1822 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
1827 * try to push data from one node into the next node left in the
1830 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1831 * error, and > 0 if there was no room in the left hand block.
1833 static int push_node_left(struct btrfs_trans_handle
*trans
,
1834 struct btrfs_root
*root
, struct extent_buffer
*dst
,
1835 struct extent_buffer
*src
, int empty
)
1842 src_nritems
= btrfs_header_nritems(src
);
1843 dst_nritems
= btrfs_header_nritems(dst
);
1844 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1845 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1846 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1848 if (!empty
&& src_nritems
<= 8)
1851 if (push_items
<= 0)
1855 push_items
= min(src_nritems
, push_items
);
1856 if (push_items
< src_nritems
) {
1857 /* leave at least 8 pointers in the node if
1858 * we aren't going to empty it
1860 if (src_nritems
- push_items
< 8) {
1861 if (push_items
<= 8)
1867 push_items
= min(src_nritems
- 8, push_items
);
1869 copy_extent_buffer(dst
, src
,
1870 btrfs_node_key_ptr_offset(dst_nritems
),
1871 btrfs_node_key_ptr_offset(0),
1872 push_items
* sizeof(struct btrfs_key_ptr
));
1874 if (push_items
< src_nritems
) {
1875 memmove_extent_buffer(src
, btrfs_node_key_ptr_offset(0),
1876 btrfs_node_key_ptr_offset(push_items
),
1877 (src_nritems
- push_items
) *
1878 sizeof(struct btrfs_key_ptr
));
1880 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1881 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1882 btrfs_mark_buffer_dirty(src
);
1883 btrfs_mark_buffer_dirty(dst
);
1889 * try to push data from one node into the next node right in the
1892 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1893 * error, and > 0 if there was no room in the right hand block.
1895 * this will only push up to 1/2 the contents of the left node over
1897 static int balance_node_right(struct btrfs_trans_handle
*trans
,
1898 struct btrfs_root
*root
,
1899 struct extent_buffer
*dst
,
1900 struct extent_buffer
*src
)
1908 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1909 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1911 src_nritems
= btrfs_header_nritems(src
);
1912 dst_nritems
= btrfs_header_nritems(dst
);
1913 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1914 if (push_items
<= 0)
1917 if (src_nritems
< 4)
1920 max_push
= src_nritems
/ 2 + 1;
1921 /* don't try to empty the node */
1922 if (max_push
>= src_nritems
)
1925 if (max_push
< push_items
)
1926 push_items
= max_push
;
1928 memmove_extent_buffer(dst
, btrfs_node_key_ptr_offset(push_items
),
1929 btrfs_node_key_ptr_offset(0),
1931 sizeof(struct btrfs_key_ptr
));
1933 copy_extent_buffer(dst
, src
,
1934 btrfs_node_key_ptr_offset(0),
1935 btrfs_node_key_ptr_offset(src_nritems
- push_items
),
1936 push_items
* sizeof(struct btrfs_key_ptr
));
1938 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1939 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1941 btrfs_mark_buffer_dirty(src
);
1942 btrfs_mark_buffer_dirty(dst
);
1948 * helper function to insert a new root level in the tree.
1949 * A new node is allocated, and a single item is inserted to
1950 * point to the existing root
1952 * returns zero on success or < 0 on failure.
1954 static noinline
int insert_new_root(struct btrfs_trans_handle
*trans
,
1955 struct btrfs_root
*root
,
1956 struct btrfs_path
*path
, int level
)
1959 struct extent_buffer
*lower
;
1960 struct extent_buffer
*c
;
1961 struct extent_buffer
*old
;
1962 struct btrfs_disk_key lower_key
;
1964 BUG_ON(path
->nodes
[level
]);
1965 BUG_ON(path
->nodes
[level
-1] != root
->node
);
1967 lower
= path
->nodes
[level
-1];
1969 btrfs_item_key(lower
, &lower_key
, 0);
1971 btrfs_node_key(lower
, &lower_key
, 0);
1973 c
= btrfs_alloc_free_block(trans
, root
, root
->nodesize
, 0,
1974 root
->root_key
.objectid
, &lower_key
,
1975 level
, root
->node
->start
, 0);
1979 root_add_used(root
, root
->nodesize
);
1981 memset_extent_buffer(c
, 0, 0, sizeof(struct btrfs_header
));
1982 btrfs_set_header_nritems(c
, 1);
1983 btrfs_set_header_level(c
, level
);
1984 btrfs_set_header_bytenr(c
, c
->start
);
1985 btrfs_set_header_generation(c
, trans
->transid
);
1986 btrfs_set_header_backref_rev(c
, BTRFS_MIXED_BACKREF_REV
);
1987 btrfs_set_header_owner(c
, root
->root_key
.objectid
);
1989 write_extent_buffer(c
, root
->fs_info
->fsid
,
1990 (unsigned long)btrfs_header_fsid(c
),
1993 write_extent_buffer(c
, root
->fs_info
->chunk_tree_uuid
,
1994 (unsigned long)btrfs_header_chunk_tree_uuid(c
),
1997 btrfs_set_node_key(c
, &lower_key
, 0);
1998 btrfs_set_node_blockptr(c
, 0, lower
->start
);
1999 lower_gen
= btrfs_header_generation(lower
);
2000 WARN_ON(lower_gen
!= trans
->transid
);
2002 btrfs_set_node_ptr_generation(c
, 0, lower_gen
);
2004 btrfs_mark_buffer_dirty(c
);
2007 rcu_assign_pointer(root
->node
, c
);
2009 /* the super has an extra ref to root->node */
2010 free_extent_buffer(old
);
2012 add_root_to_dirty_list(root
);
2013 extent_buffer_get(c
);
2014 path
->nodes
[level
] = c
;
2015 path
->locks
[level
] = 1;
2016 path
->slots
[level
] = 0;
2021 * worker function to insert a single pointer in a node.
2022 * the node should have enough room for the pointer already
2024 * slot and level indicate where you want the key to go, and
2025 * blocknr is the block the key points to.
2027 * returns zero on success and < 0 on any error
2029 static int insert_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
2030 *root
, struct btrfs_path
*path
, struct btrfs_disk_key
2031 *key
, u64 bytenr
, int slot
, int level
)
2033 struct extent_buffer
*lower
;
2036 BUG_ON(!path
->nodes
[level
]);
2037 btrfs_assert_tree_locked(path
->nodes
[level
]);
2038 lower
= path
->nodes
[level
];
2039 nritems
= btrfs_header_nritems(lower
);
2040 BUG_ON(slot
> nritems
);
2041 if (nritems
== BTRFS_NODEPTRS_PER_BLOCK(root
))
2043 if (slot
!= nritems
) {
2044 memmove_extent_buffer(lower
,
2045 btrfs_node_key_ptr_offset(slot
+ 1),
2046 btrfs_node_key_ptr_offset(slot
),
2047 (nritems
- slot
) * sizeof(struct btrfs_key_ptr
));
2049 btrfs_set_node_key(lower
, key
, slot
);
2050 btrfs_set_node_blockptr(lower
, slot
, bytenr
);
2051 WARN_ON(trans
->transid
== 0);
2052 btrfs_set_node_ptr_generation(lower
, slot
, trans
->transid
);
2053 btrfs_set_header_nritems(lower
, nritems
+ 1);
2054 btrfs_mark_buffer_dirty(lower
);
2059 * split the node at the specified level in path in two.
2060 * The path is corrected to point to the appropriate node after the split
2062 * Before splitting this tries to make some room in the node by pushing
2063 * left and right, if either one works, it returns right away.
2065 * returns 0 on success and < 0 on failure
2067 static noinline
int split_node(struct btrfs_trans_handle
*trans
,
2068 struct btrfs_root
*root
,
2069 struct btrfs_path
*path
, int level
)
2071 struct extent_buffer
*c
;
2072 struct extent_buffer
*split
;
2073 struct btrfs_disk_key disk_key
;
2079 c
= path
->nodes
[level
];
2080 WARN_ON(btrfs_header_generation(c
) != trans
->transid
);
2081 if (c
== root
->node
) {
2082 /* trying to split the root, lets make a new one */
2083 ret
= insert_new_root(trans
, root
, path
, level
+ 1);
2087 ret
= push_nodes_for_insert(trans
, root
, path
, level
);
2088 c
= path
->nodes
[level
];
2089 if (!ret
&& btrfs_header_nritems(c
) <
2090 BTRFS_NODEPTRS_PER_BLOCK(root
) - 3)
2096 c_nritems
= btrfs_header_nritems(c
);
2097 mid
= (c_nritems
+ 1) / 2;
2098 btrfs_node_key(c
, &disk_key
, mid
);
2100 split
= btrfs_alloc_free_block(trans
, root
, root
->nodesize
, 0,
2101 root
->root_key
.objectid
,
2102 &disk_key
, level
, c
->start
, 0);
2104 return PTR_ERR(split
);
2106 root_add_used(root
, root
->nodesize
);
2108 memset_extent_buffer(split
, 0, 0, sizeof(struct btrfs_header
));
2109 btrfs_set_header_level(split
, btrfs_header_level(c
));
2110 btrfs_set_header_bytenr(split
, split
->start
);
2111 btrfs_set_header_generation(split
, trans
->transid
);
2112 btrfs_set_header_backref_rev(split
, BTRFS_MIXED_BACKREF_REV
);
2113 btrfs_set_header_owner(split
, root
->root_key
.objectid
);
2114 write_extent_buffer(split
, root
->fs_info
->fsid
,
2115 (unsigned long)btrfs_header_fsid(split
),
2117 write_extent_buffer(split
, root
->fs_info
->chunk_tree_uuid
,
2118 (unsigned long)btrfs_header_chunk_tree_uuid(split
),
2122 copy_extent_buffer(split
, c
,
2123 btrfs_node_key_ptr_offset(0),
2124 btrfs_node_key_ptr_offset(mid
),
2125 (c_nritems
- mid
) * sizeof(struct btrfs_key_ptr
));
2126 btrfs_set_header_nritems(split
, c_nritems
- mid
);
2127 btrfs_set_header_nritems(c
, mid
);
2130 btrfs_mark_buffer_dirty(c
);
2131 btrfs_mark_buffer_dirty(split
);
2133 wret
= insert_ptr(trans
, root
, path
, &disk_key
, split
->start
,
2134 path
->slots
[level
+ 1] + 1,
2139 if (path
->slots
[level
] >= mid
) {
2140 path
->slots
[level
] -= mid
;
2141 btrfs_tree_unlock(c
);
2142 free_extent_buffer(c
);
2143 path
->nodes
[level
] = split
;
2144 path
->slots
[level
+ 1] += 1;
2146 btrfs_tree_unlock(split
);
2147 free_extent_buffer(split
);
2153 * how many bytes are required to store the items in a leaf. start
2154 * and nr indicate which items in the leaf to check. This totals up the
2155 * space used both by the item structs and the item data
2157 static int leaf_space_used(struct extent_buffer
*l
, int start
, int nr
)
2160 int nritems
= btrfs_header_nritems(l
);
2161 int end
= min(nritems
, start
+ nr
) - 1;
2165 data_len
= btrfs_item_end_nr(l
, start
);
2166 data_len
= data_len
- btrfs_item_offset_nr(l
, end
);
2167 data_len
+= sizeof(struct btrfs_item
) * nr
;
2168 WARN_ON(data_len
< 0);
2173 * The space between the end of the leaf items and
2174 * the start of the leaf data. IOW, how much room
2175 * the leaf has left for both items and data
2177 noinline
int btrfs_leaf_free_space(struct btrfs_root
*root
,
2178 struct extent_buffer
*leaf
)
2180 int nritems
= btrfs_header_nritems(leaf
);
2182 ret
= BTRFS_LEAF_DATA_SIZE(root
) - leaf_space_used(leaf
, 0, nritems
);
2184 printk(KERN_CRIT
"leaf free space ret %d, leaf data size %lu, "
2185 "used %d nritems %d\n",
2186 ret
, (unsigned long) BTRFS_LEAF_DATA_SIZE(root
),
2187 leaf_space_used(leaf
, 0, nritems
), nritems
);
2193 * min slot controls the lowest index we're willing to push to the
2194 * right. We'll push up to and including min_slot, but no lower
2196 static noinline
int __push_leaf_right(struct btrfs_trans_handle
*trans
,
2197 struct btrfs_root
*root
,
2198 struct btrfs_path
*path
,
2199 int data_size
, int empty
,
2200 struct extent_buffer
*right
,
2201 int free_space
, u32 left_nritems
,
2204 struct extent_buffer
*left
= path
->nodes
[0];
2205 struct extent_buffer
*upper
= path
->nodes
[1];
2206 struct btrfs_disk_key disk_key
;
2211 struct btrfs_item
*item
;
2220 nr
= max_t(u32
, 1, min_slot
);
2222 if (path
->slots
[0] >= left_nritems
)
2223 push_space
+= data_size
;
2225 slot
= path
->slots
[1];
2226 i
= left_nritems
- 1;
2228 item
= btrfs_item_nr(left
, i
);
2230 if (!empty
&& push_items
> 0) {
2231 if (path
->slots
[0] > i
)
2233 if (path
->slots
[0] == i
) {
2234 int space
= btrfs_leaf_free_space(root
, left
);
2235 if (space
+ push_space
* 2 > free_space
)
2240 if (path
->slots
[0] == i
)
2241 push_space
+= data_size
;
2243 if (!left
->map_token
) {
2244 map_extent_buffer(left
, (unsigned long)item
,
2245 sizeof(struct btrfs_item
),
2246 &left
->map_token
, &left
->kaddr
,
2247 &left
->map_start
, &left
->map_len
,
2251 this_item_size
= btrfs_item_size(left
, item
);
2252 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
2256 push_space
+= this_item_size
+ sizeof(*item
);
2261 if (left
->map_token
) {
2262 unmap_extent_buffer(left
, left
->map_token
, KM_USER1
);
2263 left
->map_token
= NULL
;
2266 if (push_items
== 0)
2269 if (!empty
&& push_items
== left_nritems
)
2272 /* push left to right */
2273 right_nritems
= btrfs_header_nritems(right
);
2275 push_space
= btrfs_item_end_nr(left
, left_nritems
- push_items
);
2276 push_space
-= leaf_data_end(root
, left
);
2278 /* make room in the right data area */
2279 data_end
= leaf_data_end(root
, right
);
2280 memmove_extent_buffer(right
,
2281 btrfs_leaf_data(right
) + data_end
- push_space
,
2282 btrfs_leaf_data(right
) + data_end
,
2283 BTRFS_LEAF_DATA_SIZE(root
) - data_end
);
2285 /* copy from the left data area */
2286 copy_extent_buffer(right
, left
, btrfs_leaf_data(right
) +
2287 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
2288 btrfs_leaf_data(left
) + leaf_data_end(root
, left
),
2291 memmove_extent_buffer(right
, btrfs_item_nr_offset(push_items
),
2292 btrfs_item_nr_offset(0),
2293 right_nritems
* sizeof(struct btrfs_item
));
2295 /* copy the items from left to right */
2296 copy_extent_buffer(right
, left
, btrfs_item_nr_offset(0),
2297 btrfs_item_nr_offset(left_nritems
- push_items
),
2298 push_items
* sizeof(struct btrfs_item
));
2300 /* update the item pointers */
2301 right_nritems
+= push_items
;
2302 btrfs_set_header_nritems(right
, right_nritems
);
2303 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
2304 for (i
= 0; i
< right_nritems
; i
++) {
2305 item
= btrfs_item_nr(right
, i
);
2306 if (!right
->map_token
) {
2307 map_extent_buffer(right
, (unsigned long)item
,
2308 sizeof(struct btrfs_item
),
2309 &right
->map_token
, &right
->kaddr
,
2310 &right
->map_start
, &right
->map_len
,
2313 push_space
-= btrfs_item_size(right
, item
);
2314 btrfs_set_item_offset(right
, item
, push_space
);
2317 if (right
->map_token
) {
2318 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
2319 right
->map_token
= NULL
;
2321 left_nritems
-= push_items
;
2322 btrfs_set_header_nritems(left
, left_nritems
);
2325 btrfs_mark_buffer_dirty(left
);
2327 clean_tree_block(trans
, root
, left
);
2329 btrfs_mark_buffer_dirty(right
);
2331 btrfs_item_key(right
, &disk_key
, 0);
2332 btrfs_set_node_key(upper
, &disk_key
, slot
+ 1);
2333 btrfs_mark_buffer_dirty(upper
);
2335 /* then fixup the leaf pointer in the path */
2336 if (path
->slots
[0] >= left_nritems
) {
2337 path
->slots
[0] -= left_nritems
;
2338 if (btrfs_header_nritems(path
->nodes
[0]) == 0)
2339 clean_tree_block(trans
, root
, path
->nodes
[0]);
2340 btrfs_tree_unlock(path
->nodes
[0]);
2341 free_extent_buffer(path
->nodes
[0]);
2342 path
->nodes
[0] = right
;
2343 path
->slots
[1] += 1;
2345 btrfs_tree_unlock(right
);
2346 free_extent_buffer(right
);
2351 btrfs_tree_unlock(right
);
2352 free_extent_buffer(right
);
2357 * push some data in the path leaf to the right, trying to free up at
2358 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2360 * returns 1 if the push failed because the other node didn't have enough
2361 * room, 0 if everything worked out and < 0 if there were major errors.
2363 * this will push starting from min_slot to the end of the leaf. It won't
2364 * push any slot lower than min_slot
2366 static int push_leaf_right(struct btrfs_trans_handle
*trans
, struct btrfs_root
2367 *root
, struct btrfs_path
*path
,
2368 int min_data_size
, int data_size
,
2369 int empty
, u32 min_slot
)
2371 struct extent_buffer
*left
= path
->nodes
[0];
2372 struct extent_buffer
*right
;
2373 struct extent_buffer
*upper
;
2379 if (!path
->nodes
[1])
2382 slot
= path
->slots
[1];
2383 upper
= path
->nodes
[1];
2384 if (slot
>= btrfs_header_nritems(upper
) - 1)
2387 btrfs_assert_tree_locked(path
->nodes
[1]);
2389 right
= read_node_slot(root
, upper
, slot
+ 1);
2393 btrfs_tree_lock(right
);
2394 btrfs_set_lock_blocking(right
);
2396 free_space
= btrfs_leaf_free_space(root
, right
);
2397 if (free_space
< data_size
)
2400 /* cow and double check */
2401 ret
= btrfs_cow_block(trans
, root
, right
, upper
,
2406 free_space
= btrfs_leaf_free_space(root
, right
);
2407 if (free_space
< data_size
)
2410 left_nritems
= btrfs_header_nritems(left
);
2411 if (left_nritems
== 0)
2414 return __push_leaf_right(trans
, root
, path
, min_data_size
, empty
,
2415 right
, free_space
, left_nritems
, min_slot
);
2417 btrfs_tree_unlock(right
);
2418 free_extent_buffer(right
);
2423 * push some data in the path leaf to the left, trying to free up at
2424 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2426 * max_slot can put a limit on how far into the leaf we'll push items. The
2427 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2430 static noinline
int __push_leaf_left(struct btrfs_trans_handle
*trans
,
2431 struct btrfs_root
*root
,
2432 struct btrfs_path
*path
, int data_size
,
2433 int empty
, struct extent_buffer
*left
,
2434 int free_space
, u32 right_nritems
,
2437 struct btrfs_disk_key disk_key
;
2438 struct extent_buffer
*right
= path
->nodes
[0];
2442 struct btrfs_item
*item
;
2443 u32 old_left_nritems
;
2448 u32 old_left_item_size
;
2451 nr
= min(right_nritems
, max_slot
);
2453 nr
= min(right_nritems
- 1, max_slot
);
2455 for (i
= 0; i
< nr
; i
++) {
2456 item
= btrfs_item_nr(right
, i
);
2457 if (!right
->map_token
) {
2458 map_extent_buffer(right
, (unsigned long)item
,
2459 sizeof(struct btrfs_item
),
2460 &right
->map_token
, &right
->kaddr
,
2461 &right
->map_start
, &right
->map_len
,
2465 if (!empty
&& push_items
> 0) {
2466 if (path
->slots
[0] < i
)
2468 if (path
->slots
[0] == i
) {
2469 int space
= btrfs_leaf_free_space(root
, right
);
2470 if (space
+ push_space
* 2 > free_space
)
2475 if (path
->slots
[0] == i
)
2476 push_space
+= data_size
;
2478 this_item_size
= btrfs_item_size(right
, item
);
2479 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
2483 push_space
+= this_item_size
+ sizeof(*item
);
2486 if (right
->map_token
) {
2487 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
2488 right
->map_token
= NULL
;
2491 if (push_items
== 0) {
2495 if (!empty
&& push_items
== btrfs_header_nritems(right
))
2498 /* push data from right to left */
2499 copy_extent_buffer(left
, right
,
2500 btrfs_item_nr_offset(btrfs_header_nritems(left
)),
2501 btrfs_item_nr_offset(0),
2502 push_items
* sizeof(struct btrfs_item
));
2504 push_space
= BTRFS_LEAF_DATA_SIZE(root
) -
2505 btrfs_item_offset_nr(right
, push_items
- 1);
2507 copy_extent_buffer(left
, right
, btrfs_leaf_data(left
) +
2508 leaf_data_end(root
, left
) - push_space
,
2509 btrfs_leaf_data(right
) +
2510 btrfs_item_offset_nr(right
, push_items
- 1),
2512 old_left_nritems
= btrfs_header_nritems(left
);
2513 BUG_ON(old_left_nritems
<= 0);
2515 old_left_item_size
= btrfs_item_offset_nr(left
, old_left_nritems
- 1);
2516 for (i
= old_left_nritems
; i
< old_left_nritems
+ push_items
; i
++) {
2519 item
= btrfs_item_nr(left
, i
);
2520 if (!left
->map_token
) {
2521 map_extent_buffer(left
, (unsigned long)item
,
2522 sizeof(struct btrfs_item
),
2523 &left
->map_token
, &left
->kaddr
,
2524 &left
->map_start
, &left
->map_len
,
2528 ioff
= btrfs_item_offset(left
, item
);
2529 btrfs_set_item_offset(left
, item
,
2530 ioff
- (BTRFS_LEAF_DATA_SIZE(root
) - old_left_item_size
));
2532 btrfs_set_header_nritems(left
, old_left_nritems
+ push_items
);
2533 if (left
->map_token
) {
2534 unmap_extent_buffer(left
, left
->map_token
, KM_USER1
);
2535 left
->map_token
= NULL
;
2538 /* fixup right node */
2539 if (push_items
> right_nritems
) {
2540 printk(KERN_CRIT
"push items %d nr %u\n", push_items
,
2545 if (push_items
< right_nritems
) {
2546 push_space
= btrfs_item_offset_nr(right
, push_items
- 1) -
2547 leaf_data_end(root
, right
);
2548 memmove_extent_buffer(right
, btrfs_leaf_data(right
) +
2549 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
2550 btrfs_leaf_data(right
) +
2551 leaf_data_end(root
, right
), push_space
);
2553 memmove_extent_buffer(right
, btrfs_item_nr_offset(0),
2554 btrfs_item_nr_offset(push_items
),
2555 (btrfs_header_nritems(right
) - push_items
) *
2556 sizeof(struct btrfs_item
));
2558 right_nritems
-= push_items
;
2559 btrfs_set_header_nritems(right
, right_nritems
);
2560 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
2561 for (i
= 0; i
< right_nritems
; i
++) {
2562 item
= btrfs_item_nr(right
, i
);
2564 if (!right
->map_token
) {
2565 map_extent_buffer(right
, (unsigned long)item
,
2566 sizeof(struct btrfs_item
),
2567 &right
->map_token
, &right
->kaddr
,
2568 &right
->map_start
, &right
->map_len
,
2572 push_space
= push_space
- btrfs_item_size(right
, item
);
2573 btrfs_set_item_offset(right
, item
, push_space
);
2575 if (right
->map_token
) {
2576 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
2577 right
->map_token
= NULL
;
2580 btrfs_mark_buffer_dirty(left
);
2582 btrfs_mark_buffer_dirty(right
);
2584 clean_tree_block(trans
, root
, right
);
2586 btrfs_item_key(right
, &disk_key
, 0);
2587 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
2591 /* then fixup the leaf pointer in the path */
2592 if (path
->slots
[0] < push_items
) {
2593 path
->slots
[0] += old_left_nritems
;
2594 btrfs_tree_unlock(path
->nodes
[0]);
2595 free_extent_buffer(path
->nodes
[0]);
2596 path
->nodes
[0] = left
;
2597 path
->slots
[1] -= 1;
2599 btrfs_tree_unlock(left
);
2600 free_extent_buffer(left
);
2601 path
->slots
[0] -= push_items
;
2603 BUG_ON(path
->slots
[0] < 0);
2606 btrfs_tree_unlock(left
);
2607 free_extent_buffer(left
);
2612 * push some data in the path leaf to the left, trying to free up at
2613 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2615 * max_slot can put a limit on how far into the leaf we'll push items. The
2616 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2619 static int push_leaf_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
2620 *root
, struct btrfs_path
*path
, int min_data_size
,
2621 int data_size
, int empty
, u32 max_slot
)
2623 struct extent_buffer
*right
= path
->nodes
[0];
2624 struct extent_buffer
*left
;
2630 slot
= path
->slots
[1];
2633 if (!path
->nodes
[1])
2636 right_nritems
= btrfs_header_nritems(right
);
2637 if (right_nritems
== 0)
2640 btrfs_assert_tree_locked(path
->nodes
[1]);
2642 left
= read_node_slot(root
, path
->nodes
[1], slot
- 1);
2646 btrfs_tree_lock(left
);
2647 btrfs_set_lock_blocking(left
);
2649 free_space
= btrfs_leaf_free_space(root
, left
);
2650 if (free_space
< data_size
) {
2655 /* cow and double check */
2656 ret
= btrfs_cow_block(trans
, root
, left
,
2657 path
->nodes
[1], slot
- 1, &left
);
2659 /* we hit -ENOSPC, but it isn't fatal here */
2664 free_space
= btrfs_leaf_free_space(root
, left
);
2665 if (free_space
< data_size
) {
2670 return __push_leaf_left(trans
, root
, path
, min_data_size
,
2671 empty
, left
, free_space
, right_nritems
,
2674 btrfs_tree_unlock(left
);
2675 free_extent_buffer(left
);
2680 * split the path's leaf in two, making sure there is at least data_size
2681 * available for the resulting leaf level of the path.
2683 * returns 0 if all went well and < 0 on failure.
2685 static noinline
int copy_for_split(struct btrfs_trans_handle
*trans
,
2686 struct btrfs_root
*root
,
2687 struct btrfs_path
*path
,
2688 struct extent_buffer
*l
,
2689 struct extent_buffer
*right
,
2690 int slot
, int mid
, int nritems
)
2697 struct btrfs_disk_key disk_key
;
2699 nritems
= nritems
- mid
;
2700 btrfs_set_header_nritems(right
, nritems
);
2701 data_copy_size
= btrfs_item_end_nr(l
, mid
) - leaf_data_end(root
, l
);
2703 copy_extent_buffer(right
, l
, btrfs_item_nr_offset(0),
2704 btrfs_item_nr_offset(mid
),
2705 nritems
* sizeof(struct btrfs_item
));
2707 copy_extent_buffer(right
, l
,
2708 btrfs_leaf_data(right
) + BTRFS_LEAF_DATA_SIZE(root
) -
2709 data_copy_size
, btrfs_leaf_data(l
) +
2710 leaf_data_end(root
, l
), data_copy_size
);
2712 rt_data_off
= BTRFS_LEAF_DATA_SIZE(root
) -
2713 btrfs_item_end_nr(l
, mid
);
2715 for (i
= 0; i
< nritems
; i
++) {
2716 struct btrfs_item
*item
= btrfs_item_nr(right
, i
);
2719 if (!right
->map_token
) {
2720 map_extent_buffer(right
, (unsigned long)item
,
2721 sizeof(struct btrfs_item
),
2722 &right
->map_token
, &right
->kaddr
,
2723 &right
->map_start
, &right
->map_len
,
2727 ioff
= btrfs_item_offset(right
, item
);
2728 btrfs_set_item_offset(right
, item
, ioff
+ rt_data_off
);
2731 if (right
->map_token
) {
2732 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
2733 right
->map_token
= NULL
;
2736 btrfs_set_header_nritems(l
, mid
);
2738 btrfs_item_key(right
, &disk_key
, 0);
2739 wret
= insert_ptr(trans
, root
, path
, &disk_key
, right
->start
,
2740 path
->slots
[1] + 1, 1);
2744 btrfs_mark_buffer_dirty(right
);
2745 btrfs_mark_buffer_dirty(l
);
2746 BUG_ON(path
->slots
[0] != slot
);
2749 btrfs_tree_unlock(path
->nodes
[0]);
2750 free_extent_buffer(path
->nodes
[0]);
2751 path
->nodes
[0] = right
;
2752 path
->slots
[0] -= mid
;
2753 path
->slots
[1] += 1;
2755 btrfs_tree_unlock(right
);
2756 free_extent_buffer(right
);
2759 BUG_ON(path
->slots
[0] < 0);
2765 * double splits happen when we need to insert a big item in the middle
2766 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2767 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2770 * We avoid this by trying to push the items on either side of our target
2771 * into the adjacent leaves. If all goes well we can avoid the double split
2774 static noinline
int push_for_double_split(struct btrfs_trans_handle
*trans
,
2775 struct btrfs_root
*root
,
2776 struct btrfs_path
*path
,
2784 slot
= path
->slots
[0];
2787 * try to push all the items after our slot into the
2790 ret
= push_leaf_right(trans
, root
, path
, 1, data_size
, 0, slot
);
2797 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2799 * our goal is to get our slot at the start or end of a leaf. If
2800 * we've done so we're done
2802 if (path
->slots
[0] == 0 || path
->slots
[0] == nritems
)
2805 if (btrfs_leaf_free_space(root
, path
->nodes
[0]) >= data_size
)
2808 /* try to push all the items before our slot into the next leaf */
2809 slot
= path
->slots
[0];
2810 ret
= push_leaf_left(trans
, root
, path
, 1, data_size
, 0, slot
);
2823 * split the path's leaf in two, making sure there is at least data_size
2824 * available for the resulting leaf level of the path.
2826 * returns 0 if all went well and < 0 on failure.
2828 static noinline
int split_leaf(struct btrfs_trans_handle
*trans
,
2829 struct btrfs_root
*root
,
2830 struct btrfs_key
*ins_key
,
2831 struct btrfs_path
*path
, int data_size
,
2834 struct btrfs_disk_key disk_key
;
2835 struct extent_buffer
*l
;
2839 struct extent_buffer
*right
;
2843 int num_doubles
= 0;
2844 int tried_avoid_double
= 0;
2847 slot
= path
->slots
[0];
2848 if (extend
&& data_size
+ btrfs_item_size_nr(l
, slot
) +
2849 sizeof(struct btrfs_item
) > BTRFS_LEAF_DATA_SIZE(root
))
2852 /* first try to make some room by pushing left and right */
2854 wret
= push_leaf_right(trans
, root
, path
, data_size
,
2859 wret
= push_leaf_left(trans
, root
, path
, data_size
,
2860 data_size
, 0, (u32
)-1);
2866 /* did the pushes work? */
2867 if (btrfs_leaf_free_space(root
, l
) >= data_size
)
2871 if (!path
->nodes
[1]) {
2872 ret
= insert_new_root(trans
, root
, path
, 1);
2879 slot
= path
->slots
[0];
2880 nritems
= btrfs_header_nritems(l
);
2881 mid
= (nritems
+ 1) / 2;
2885 leaf_space_used(l
, mid
, nritems
- mid
) + data_size
>
2886 BTRFS_LEAF_DATA_SIZE(root
)) {
2887 if (slot
>= nritems
) {
2891 if (mid
!= nritems
&&
2892 leaf_space_used(l
, mid
, nritems
- mid
) +
2893 data_size
> BTRFS_LEAF_DATA_SIZE(root
)) {
2894 if (data_size
&& !tried_avoid_double
)
2895 goto push_for_double
;
2901 if (leaf_space_used(l
, 0, mid
) + data_size
>
2902 BTRFS_LEAF_DATA_SIZE(root
)) {
2903 if (!extend
&& data_size
&& slot
== 0) {
2905 } else if ((extend
|| !data_size
) && slot
== 0) {
2909 if (mid
!= nritems
&&
2910 leaf_space_used(l
, mid
, nritems
- mid
) +
2911 data_size
> BTRFS_LEAF_DATA_SIZE(root
)) {
2912 if (data_size
&& !tried_avoid_double
)
2913 goto push_for_double
;
2921 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
2923 btrfs_item_key(l
, &disk_key
, mid
);
2925 right
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
, 0,
2926 root
->root_key
.objectid
,
2927 &disk_key
, 0, l
->start
, 0);
2929 return PTR_ERR(right
);
2931 root_add_used(root
, root
->leafsize
);
2933 memset_extent_buffer(right
, 0, 0, sizeof(struct btrfs_header
));
2934 btrfs_set_header_bytenr(right
, right
->start
);
2935 btrfs_set_header_generation(right
, trans
->transid
);
2936 btrfs_set_header_backref_rev(right
, BTRFS_MIXED_BACKREF_REV
);
2937 btrfs_set_header_owner(right
, root
->root_key
.objectid
);
2938 btrfs_set_header_level(right
, 0);
2939 write_extent_buffer(right
, root
->fs_info
->fsid
,
2940 (unsigned long)btrfs_header_fsid(right
),
2943 write_extent_buffer(right
, root
->fs_info
->chunk_tree_uuid
,
2944 (unsigned long)btrfs_header_chunk_tree_uuid(right
),
2949 btrfs_set_header_nritems(right
, 0);
2950 wret
= insert_ptr(trans
, root
, path
,
2951 &disk_key
, right
->start
,
2952 path
->slots
[1] + 1, 1);
2956 btrfs_tree_unlock(path
->nodes
[0]);
2957 free_extent_buffer(path
->nodes
[0]);
2958 path
->nodes
[0] = right
;
2960 path
->slots
[1] += 1;
2962 btrfs_set_header_nritems(right
, 0);
2963 wret
= insert_ptr(trans
, root
, path
,
2969 btrfs_tree_unlock(path
->nodes
[0]);
2970 free_extent_buffer(path
->nodes
[0]);
2971 path
->nodes
[0] = right
;
2973 if (path
->slots
[1] == 0) {
2974 wret
= fixup_low_keys(trans
, root
,
2975 path
, &disk_key
, 1);
2980 btrfs_mark_buffer_dirty(right
);
2984 ret
= copy_for_split(trans
, root
, path
, l
, right
, slot
, mid
, nritems
);
2988 BUG_ON(num_doubles
!= 0);
2996 push_for_double_split(trans
, root
, path
, data_size
);
2997 tried_avoid_double
= 1;
2998 if (btrfs_leaf_free_space(root
, path
->nodes
[0]) >= data_size
)
3003 static noinline
int setup_leaf_for_split(struct btrfs_trans_handle
*trans
,
3004 struct btrfs_root
*root
,
3005 struct btrfs_path
*path
, int ins_len
)
3007 struct btrfs_key key
;
3008 struct extent_buffer
*leaf
;
3009 struct btrfs_file_extent_item
*fi
;
3014 leaf
= path
->nodes
[0];
3015 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3017 BUG_ON(key
.type
!= BTRFS_EXTENT_DATA_KEY
&&
3018 key
.type
!= BTRFS_EXTENT_CSUM_KEY
);
3020 if (btrfs_leaf_free_space(root
, leaf
) >= ins_len
)
3023 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
3024 if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
3025 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3026 struct btrfs_file_extent_item
);
3027 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
3029 btrfs_release_path(root
, path
);
3031 path
->keep_locks
= 1;
3032 path
->search_for_split
= 1;
3033 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
3034 path
->search_for_split
= 0;
3039 leaf
= path
->nodes
[0];
3040 /* if our item isn't there or got smaller, return now */
3041 if (ret
> 0 || item_size
!= btrfs_item_size_nr(leaf
, path
->slots
[0]))
3044 /* the leaf has changed, it now has room. return now */
3045 if (btrfs_leaf_free_space(root
, path
->nodes
[0]) >= ins_len
)
3048 if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
3049 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3050 struct btrfs_file_extent_item
);
3051 if (extent_len
!= btrfs_file_extent_num_bytes(leaf
, fi
))
3055 btrfs_set_path_blocking(path
);
3056 ret
= split_leaf(trans
, root
, &key
, path
, ins_len
, 1);
3060 path
->keep_locks
= 0;
3061 btrfs_unlock_up_safe(path
, 1);
3064 path
->keep_locks
= 0;
3068 static noinline
int split_item(struct btrfs_trans_handle
*trans
,
3069 struct btrfs_root
*root
,
3070 struct btrfs_path
*path
,
3071 struct btrfs_key
*new_key
,
3072 unsigned long split_offset
)
3074 struct extent_buffer
*leaf
;
3075 struct btrfs_item
*item
;
3076 struct btrfs_item
*new_item
;
3082 struct btrfs_disk_key disk_key
;
3084 leaf
= path
->nodes
[0];
3085 BUG_ON(btrfs_leaf_free_space(root
, leaf
) < sizeof(struct btrfs_item
));
3087 btrfs_set_path_blocking(path
);
3089 item
= btrfs_item_nr(leaf
, path
->slots
[0]);
3090 orig_offset
= btrfs_item_offset(leaf
, item
);
3091 item_size
= btrfs_item_size(leaf
, item
);
3093 buf
= kmalloc(item_size
, GFP_NOFS
);
3097 read_extent_buffer(leaf
, buf
, btrfs_item_ptr_offset(leaf
,
3098 path
->slots
[0]), item_size
);
3100 slot
= path
->slots
[0] + 1;
3101 nritems
= btrfs_header_nritems(leaf
);
3102 if (slot
!= nritems
) {
3103 /* shift the items */
3104 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ 1),
3105 btrfs_item_nr_offset(slot
),
3106 (nritems
- slot
) * sizeof(struct btrfs_item
));
3109 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
3110 btrfs_set_item_key(leaf
, &disk_key
, slot
);
3112 new_item
= btrfs_item_nr(leaf
, slot
);
3114 btrfs_set_item_offset(leaf
, new_item
, orig_offset
);
3115 btrfs_set_item_size(leaf
, new_item
, item_size
- split_offset
);
3117 btrfs_set_item_offset(leaf
, item
,
3118 orig_offset
+ item_size
- split_offset
);
3119 btrfs_set_item_size(leaf
, item
, split_offset
);
3121 btrfs_set_header_nritems(leaf
, nritems
+ 1);
3123 /* write the data for the start of the original item */
3124 write_extent_buffer(leaf
, buf
,
3125 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
3128 /* write the data for the new item */
3129 write_extent_buffer(leaf
, buf
+ split_offset
,
3130 btrfs_item_ptr_offset(leaf
, slot
),
3131 item_size
- split_offset
);
3132 btrfs_mark_buffer_dirty(leaf
);
3134 BUG_ON(btrfs_leaf_free_space(root
, leaf
) < 0);
3140 * This function splits a single item into two items,
3141 * giving 'new_key' to the new item and splitting the
3142 * old one at split_offset (from the start of the item).
3144 * The path may be released by this operation. After
3145 * the split, the path is pointing to the old item. The
3146 * new item is going to be in the same node as the old one.
3148 * Note, the item being split must be smaller enough to live alone on
3149 * a tree block with room for one extra struct btrfs_item
3151 * This allows us to split the item in place, keeping a lock on the
3152 * leaf the entire time.
3154 int btrfs_split_item(struct btrfs_trans_handle
*trans
,
3155 struct btrfs_root
*root
,
3156 struct btrfs_path
*path
,
3157 struct btrfs_key
*new_key
,
3158 unsigned long split_offset
)
3161 ret
= setup_leaf_for_split(trans
, root
, path
,
3162 sizeof(struct btrfs_item
));
3166 ret
= split_item(trans
, root
, path
, new_key
, split_offset
);
3171 * This function duplicate a item, giving 'new_key' to the new item.
3172 * It guarantees both items live in the same tree leaf and the new item
3173 * is contiguous with the original item.
3175 * This allows us to split file extent in place, keeping a lock on the
3176 * leaf the entire time.
3178 int btrfs_duplicate_item(struct btrfs_trans_handle
*trans
,
3179 struct btrfs_root
*root
,
3180 struct btrfs_path
*path
,
3181 struct btrfs_key
*new_key
)
3183 struct extent_buffer
*leaf
;
3187 leaf
= path
->nodes
[0];
3188 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
3189 ret
= setup_leaf_for_split(trans
, root
, path
,
3190 item_size
+ sizeof(struct btrfs_item
));
3195 ret
= setup_items_for_insert(trans
, root
, path
, new_key
, &item_size
,
3196 item_size
, item_size
+
3197 sizeof(struct btrfs_item
), 1);
3200 leaf
= path
->nodes
[0];
3201 memcpy_extent_buffer(leaf
,
3202 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
3203 btrfs_item_ptr_offset(leaf
, path
->slots
[0] - 1),
3209 * make the item pointed to by the path smaller. new_size indicates
3210 * how small to make it, and from_end tells us if we just chop bytes
3211 * off the end of the item or if we shift the item to chop bytes off
3214 int btrfs_truncate_item(struct btrfs_trans_handle
*trans
,
3215 struct btrfs_root
*root
,
3216 struct btrfs_path
*path
,
3217 u32 new_size
, int from_end
)
3221 struct extent_buffer
*leaf
;
3222 struct btrfs_item
*item
;
3224 unsigned int data_end
;
3225 unsigned int old_data_start
;
3226 unsigned int old_size
;
3227 unsigned int size_diff
;
3230 leaf
= path
->nodes
[0];
3231 slot
= path
->slots
[0];
3233 old_size
= btrfs_item_size_nr(leaf
, slot
);
3234 if (old_size
== new_size
)
3237 nritems
= btrfs_header_nritems(leaf
);
3238 data_end
= leaf_data_end(root
, leaf
);
3240 old_data_start
= btrfs_item_offset_nr(leaf
, slot
);
3242 size_diff
= old_size
- new_size
;
3245 BUG_ON(slot
>= nritems
);
3248 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3250 /* first correct the data pointers */
3251 for (i
= slot
; i
< nritems
; i
++) {
3253 item
= btrfs_item_nr(leaf
, i
);
3255 if (!leaf
->map_token
) {
3256 map_extent_buffer(leaf
, (unsigned long)item
,
3257 sizeof(struct btrfs_item
),
3258 &leaf
->map_token
, &leaf
->kaddr
,
3259 &leaf
->map_start
, &leaf
->map_len
,
3263 ioff
= btrfs_item_offset(leaf
, item
);
3264 btrfs_set_item_offset(leaf
, item
, ioff
+ size_diff
);
3267 if (leaf
->map_token
) {
3268 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3269 leaf
->map_token
= NULL
;
3272 /* shift the data */
3274 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3275 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
3276 data_end
, old_data_start
+ new_size
- data_end
);
3278 struct btrfs_disk_key disk_key
;
3281 btrfs_item_key(leaf
, &disk_key
, slot
);
3283 if (btrfs_disk_key_type(&disk_key
) == BTRFS_EXTENT_DATA_KEY
) {
3285 struct btrfs_file_extent_item
*fi
;
3287 fi
= btrfs_item_ptr(leaf
, slot
,
3288 struct btrfs_file_extent_item
);
3289 fi
= (struct btrfs_file_extent_item
*)(
3290 (unsigned long)fi
- size_diff
);
3292 if (btrfs_file_extent_type(leaf
, fi
) ==
3293 BTRFS_FILE_EXTENT_INLINE
) {
3294 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
3295 memmove_extent_buffer(leaf
, ptr
,
3297 offsetof(struct btrfs_file_extent_item
,
3302 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3303 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
3304 data_end
, old_data_start
- data_end
);
3306 offset
= btrfs_disk_key_offset(&disk_key
);
3307 btrfs_set_disk_key_offset(&disk_key
, offset
+ size_diff
);
3308 btrfs_set_item_key(leaf
, &disk_key
, slot
);
3310 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
3313 item
= btrfs_item_nr(leaf
, slot
);
3314 btrfs_set_item_size(leaf
, item
, new_size
);
3315 btrfs_mark_buffer_dirty(leaf
);
3318 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3319 btrfs_print_leaf(root
, leaf
);
3326 * make the item pointed to by the path bigger, data_size is the new size.
3328 int btrfs_extend_item(struct btrfs_trans_handle
*trans
,
3329 struct btrfs_root
*root
, struct btrfs_path
*path
,
3334 struct extent_buffer
*leaf
;
3335 struct btrfs_item
*item
;
3337 unsigned int data_end
;
3338 unsigned int old_data
;
3339 unsigned int old_size
;
3342 leaf
= path
->nodes
[0];
3344 nritems
= btrfs_header_nritems(leaf
);
3345 data_end
= leaf_data_end(root
, leaf
);
3347 if (btrfs_leaf_free_space(root
, leaf
) < data_size
) {
3348 btrfs_print_leaf(root
, leaf
);
3351 slot
= path
->slots
[0];
3352 old_data
= btrfs_item_end_nr(leaf
, slot
);
3355 if (slot
>= nritems
) {
3356 btrfs_print_leaf(root
, leaf
);
3357 printk(KERN_CRIT
"slot %d too large, nritems %d\n",
3363 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3365 /* first correct the data pointers */
3366 for (i
= slot
; i
< nritems
; i
++) {
3368 item
= btrfs_item_nr(leaf
, i
);
3370 if (!leaf
->map_token
) {
3371 map_extent_buffer(leaf
, (unsigned long)item
,
3372 sizeof(struct btrfs_item
),
3373 &leaf
->map_token
, &leaf
->kaddr
,
3374 &leaf
->map_start
, &leaf
->map_len
,
3377 ioff
= btrfs_item_offset(leaf
, item
);
3378 btrfs_set_item_offset(leaf
, item
, ioff
- data_size
);
3381 if (leaf
->map_token
) {
3382 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3383 leaf
->map_token
= NULL
;
3386 /* shift the data */
3387 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3388 data_end
- data_size
, btrfs_leaf_data(leaf
) +
3389 data_end
, old_data
- data_end
);
3391 data_end
= old_data
;
3392 old_size
= btrfs_item_size_nr(leaf
, slot
);
3393 item
= btrfs_item_nr(leaf
, slot
);
3394 btrfs_set_item_size(leaf
, item
, old_size
+ data_size
);
3395 btrfs_mark_buffer_dirty(leaf
);
3398 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3399 btrfs_print_leaf(root
, leaf
);
3406 * Given a key and some data, insert items into the tree.
3407 * This does all the path init required, making room in the tree if needed.
3408 * Returns the number of keys that were inserted.
3410 int btrfs_insert_some_items(struct btrfs_trans_handle
*trans
,
3411 struct btrfs_root
*root
,
3412 struct btrfs_path
*path
,
3413 struct btrfs_key
*cpu_key
, u32
*data_size
,
3416 struct extent_buffer
*leaf
;
3417 struct btrfs_item
*item
;
3424 unsigned int data_end
;
3425 struct btrfs_disk_key disk_key
;
3426 struct btrfs_key found_key
;
3428 for (i
= 0; i
< nr
; i
++) {
3429 if (total_size
+ data_size
[i
] + sizeof(struct btrfs_item
) >
3430 BTRFS_LEAF_DATA_SIZE(root
)) {
3434 total_data
+= data_size
[i
];
3435 total_size
+= data_size
[i
] + sizeof(struct btrfs_item
);
3439 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
3445 leaf
= path
->nodes
[0];
3447 nritems
= btrfs_header_nritems(leaf
);
3448 data_end
= leaf_data_end(root
, leaf
);
3450 if (btrfs_leaf_free_space(root
, leaf
) < total_size
) {
3451 for (i
= nr
; i
>= 0; i
--) {
3452 total_data
-= data_size
[i
];
3453 total_size
-= data_size
[i
] + sizeof(struct btrfs_item
);
3454 if (total_size
< btrfs_leaf_free_space(root
, leaf
))
3460 slot
= path
->slots
[0];
3463 if (slot
!= nritems
) {
3464 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
3466 item
= btrfs_item_nr(leaf
, slot
);
3467 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3469 /* figure out how many keys we can insert in here */
3470 total_data
= data_size
[0];
3471 for (i
= 1; i
< nr
; i
++) {
3472 if (btrfs_comp_cpu_keys(&found_key
, cpu_key
+ i
) <= 0)
3474 total_data
+= data_size
[i
];
3478 if (old_data
< data_end
) {
3479 btrfs_print_leaf(root
, leaf
);
3480 printk(KERN_CRIT
"slot %d old_data %d data_end %d\n",
3481 slot
, old_data
, data_end
);
3485 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3487 /* first correct the data pointers */
3488 WARN_ON(leaf
->map_token
);
3489 for (i
= slot
; i
< nritems
; i
++) {
3492 item
= btrfs_item_nr(leaf
, i
);
3493 if (!leaf
->map_token
) {
3494 map_extent_buffer(leaf
, (unsigned long)item
,
3495 sizeof(struct btrfs_item
),
3496 &leaf
->map_token
, &leaf
->kaddr
,
3497 &leaf
->map_start
, &leaf
->map_len
,
3501 ioff
= btrfs_item_offset(leaf
, item
);
3502 btrfs_set_item_offset(leaf
, item
, ioff
- total_data
);
3504 if (leaf
->map_token
) {
3505 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3506 leaf
->map_token
= NULL
;
3509 /* shift the items */
3510 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
3511 btrfs_item_nr_offset(slot
),
3512 (nritems
- slot
) * sizeof(struct btrfs_item
));
3514 /* shift the data */
3515 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3516 data_end
- total_data
, btrfs_leaf_data(leaf
) +
3517 data_end
, old_data
- data_end
);
3518 data_end
= old_data
;
3521 * this sucks but it has to be done, if we are inserting at
3522 * the end of the leaf only insert 1 of the items, since we
3523 * have no way of knowing whats on the next leaf and we'd have
3524 * to drop our current locks to figure it out
3529 /* setup the item for the new data */
3530 for (i
= 0; i
< nr
; i
++) {
3531 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
3532 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
3533 item
= btrfs_item_nr(leaf
, slot
+ i
);
3534 btrfs_set_item_offset(leaf
, item
, data_end
- data_size
[i
]);
3535 data_end
-= data_size
[i
];
3536 btrfs_set_item_size(leaf
, item
, data_size
[i
]);
3538 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
3539 btrfs_mark_buffer_dirty(leaf
);
3543 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
3544 ret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
3547 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3548 btrfs_print_leaf(root
, leaf
);
3558 * this is a helper for btrfs_insert_empty_items, the main goal here is
3559 * to save stack depth by doing the bulk of the work in a function
3560 * that doesn't call btrfs_search_slot
3562 static noinline_for_stack
int
3563 setup_items_for_insert(struct btrfs_trans_handle
*trans
,
3564 struct btrfs_root
*root
, struct btrfs_path
*path
,
3565 struct btrfs_key
*cpu_key
, u32
*data_size
,
3566 u32 total_data
, u32 total_size
, int nr
)
3568 struct btrfs_item
*item
;
3571 unsigned int data_end
;
3572 struct btrfs_disk_key disk_key
;
3574 struct extent_buffer
*leaf
;
3577 leaf
= path
->nodes
[0];
3578 slot
= path
->slots
[0];
3580 nritems
= btrfs_header_nritems(leaf
);
3581 data_end
= leaf_data_end(root
, leaf
);
3583 if (btrfs_leaf_free_space(root
, leaf
) < total_size
) {
3584 btrfs_print_leaf(root
, leaf
);
3585 printk(KERN_CRIT
"not enough freespace need %u have %d\n",
3586 total_size
, btrfs_leaf_free_space(root
, leaf
));
3590 if (slot
!= nritems
) {
3591 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
3593 if (old_data
< data_end
) {
3594 btrfs_print_leaf(root
, leaf
);
3595 printk(KERN_CRIT
"slot %d old_data %d data_end %d\n",
3596 slot
, old_data
, data_end
);
3600 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3602 /* first correct the data pointers */
3603 WARN_ON(leaf
->map_token
);
3604 for (i
= slot
; i
< nritems
; i
++) {
3607 item
= btrfs_item_nr(leaf
, i
);
3608 if (!leaf
->map_token
) {
3609 map_extent_buffer(leaf
, (unsigned long)item
,
3610 sizeof(struct btrfs_item
),
3611 &leaf
->map_token
, &leaf
->kaddr
,
3612 &leaf
->map_start
, &leaf
->map_len
,
3616 ioff
= btrfs_item_offset(leaf
, item
);
3617 btrfs_set_item_offset(leaf
, item
, ioff
- total_data
);
3619 if (leaf
->map_token
) {
3620 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3621 leaf
->map_token
= NULL
;
3624 /* shift the items */
3625 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
3626 btrfs_item_nr_offset(slot
),
3627 (nritems
- slot
) * sizeof(struct btrfs_item
));
3629 /* shift the data */
3630 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3631 data_end
- total_data
, btrfs_leaf_data(leaf
) +
3632 data_end
, old_data
- data_end
);
3633 data_end
= old_data
;
3636 /* setup the item for the new data */
3637 for (i
= 0; i
< nr
; i
++) {
3638 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
3639 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
3640 item
= btrfs_item_nr(leaf
, slot
+ i
);
3641 btrfs_set_item_offset(leaf
, item
, data_end
- data_size
[i
]);
3642 data_end
-= data_size
[i
];
3643 btrfs_set_item_size(leaf
, item
, data_size
[i
]);
3646 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
3650 struct btrfs_disk_key disk_key
;
3651 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
3652 ret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
3654 btrfs_unlock_up_safe(path
, 1);
3655 btrfs_mark_buffer_dirty(leaf
);
3657 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3658 btrfs_print_leaf(root
, leaf
);
3665 * Given a key and some data, insert items into the tree.
3666 * This does all the path init required, making room in the tree if needed.
3668 int btrfs_insert_empty_items(struct btrfs_trans_handle
*trans
,
3669 struct btrfs_root
*root
,
3670 struct btrfs_path
*path
,
3671 struct btrfs_key
*cpu_key
, u32
*data_size
,
3680 for (i
= 0; i
< nr
; i
++)
3681 total_data
+= data_size
[i
];
3683 total_size
= total_data
+ (nr
* sizeof(struct btrfs_item
));
3684 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
3690 slot
= path
->slots
[0];
3693 ret
= setup_items_for_insert(trans
, root
, path
, cpu_key
, data_size
,
3694 total_data
, total_size
, nr
);
3701 * Given a key and some data, insert an item into the tree.
3702 * This does all the path init required, making room in the tree if needed.
3704 int btrfs_insert_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
3705 *root
, struct btrfs_key
*cpu_key
, void *data
, u32
3709 struct btrfs_path
*path
;
3710 struct extent_buffer
*leaf
;
3713 path
= btrfs_alloc_path();
3716 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
3718 leaf
= path
->nodes
[0];
3719 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
3720 write_extent_buffer(leaf
, data
, ptr
, data_size
);
3721 btrfs_mark_buffer_dirty(leaf
);
3723 btrfs_free_path(path
);
3728 * delete the pointer from a given node.
3730 * the tree should have been previously balanced so the deletion does not
3733 static int del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
3734 struct btrfs_path
*path
, int level
, int slot
)
3736 struct extent_buffer
*parent
= path
->nodes
[level
];
3741 nritems
= btrfs_header_nritems(parent
);
3742 if (slot
!= nritems
- 1) {
3743 memmove_extent_buffer(parent
,
3744 btrfs_node_key_ptr_offset(slot
),
3745 btrfs_node_key_ptr_offset(slot
+ 1),
3746 sizeof(struct btrfs_key_ptr
) *
3747 (nritems
- slot
- 1));
3750 btrfs_set_header_nritems(parent
, nritems
);
3751 if (nritems
== 0 && parent
== root
->node
) {
3752 BUG_ON(btrfs_header_level(root
->node
) != 1);
3753 /* just turn the root into a leaf and break */
3754 btrfs_set_header_level(root
->node
, 0);
3755 } else if (slot
== 0) {
3756 struct btrfs_disk_key disk_key
;
3758 btrfs_node_key(parent
, &disk_key
, 0);
3759 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, level
+ 1);
3763 btrfs_mark_buffer_dirty(parent
);
3768 * a helper function to delete the leaf pointed to by path->slots[1] and
3771 * This deletes the pointer in path->nodes[1] and frees the leaf
3772 * block extent. zero is returned if it all worked out, < 0 otherwise.
3774 * The path must have already been setup for deleting the leaf, including
3775 * all the proper balancing. path->nodes[1] must be locked.
3777 static noinline
int btrfs_del_leaf(struct btrfs_trans_handle
*trans
,
3778 struct btrfs_root
*root
,
3779 struct btrfs_path
*path
,
3780 struct extent_buffer
*leaf
)
3784 WARN_ON(btrfs_header_generation(leaf
) != trans
->transid
);
3785 ret
= 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 btrfs_free_tree_block(trans
, root
, leaf
, 0, 1);
3801 * delete the item at the leaf level in path. If that empties
3802 * the leaf, remove it from the tree
3804 int btrfs_del_items(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
3805 struct btrfs_path
*path
, int slot
, int nr
)
3807 struct extent_buffer
*leaf
;
3808 struct btrfs_item
*item
;
3816 leaf
= path
->nodes
[0];
3817 last_off
= btrfs_item_offset_nr(leaf
, slot
+ nr
- 1);
3819 for (i
= 0; i
< nr
; i
++)
3820 dsize
+= btrfs_item_size_nr(leaf
, slot
+ i
);
3822 nritems
= btrfs_header_nritems(leaf
);
3824 if (slot
+ nr
!= nritems
) {
3825 int data_end
= leaf_data_end(root
, leaf
);
3827 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3829 btrfs_leaf_data(leaf
) + data_end
,
3830 last_off
- data_end
);
3832 for (i
= slot
+ nr
; i
< nritems
; i
++) {
3835 item
= btrfs_item_nr(leaf
, i
);
3836 if (!leaf
->map_token
) {
3837 map_extent_buffer(leaf
, (unsigned long)item
,
3838 sizeof(struct btrfs_item
),
3839 &leaf
->map_token
, &leaf
->kaddr
,
3840 &leaf
->map_start
, &leaf
->map_len
,
3843 ioff
= btrfs_item_offset(leaf
, item
);
3844 btrfs_set_item_offset(leaf
, item
, ioff
+ dsize
);
3847 if (leaf
->map_token
) {
3848 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3849 leaf
->map_token
= NULL
;
3852 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
),
3853 btrfs_item_nr_offset(slot
+ nr
),
3854 sizeof(struct btrfs_item
) *
3855 (nritems
- slot
- nr
));
3857 btrfs_set_header_nritems(leaf
, nritems
- nr
);
3860 /* delete the leaf if we've emptied it */
3862 if (leaf
== root
->node
) {
3863 btrfs_set_header_level(leaf
, 0);
3865 btrfs_set_path_blocking(path
);
3866 clean_tree_block(trans
, root
, leaf
);
3867 ret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
3871 int used
= leaf_space_used(leaf
, 0, nritems
);
3873 struct btrfs_disk_key disk_key
;
3875 btrfs_item_key(leaf
, &disk_key
, 0);
3876 wret
= fixup_low_keys(trans
, root
, path
,
3882 /* delete the leaf if it is mostly empty */
3883 if (used
< BTRFS_LEAF_DATA_SIZE(root
) / 3) {
3884 /* push_leaf_left fixes the path.
3885 * make sure the path still points to our leaf
3886 * for possible call to del_ptr below
3888 slot
= path
->slots
[1];
3889 extent_buffer_get(leaf
);
3891 btrfs_set_path_blocking(path
);
3892 wret
= push_leaf_left(trans
, root
, path
, 1, 1,
3894 if (wret
< 0 && wret
!= -ENOSPC
)
3897 if (path
->nodes
[0] == leaf
&&
3898 btrfs_header_nritems(leaf
)) {
3899 wret
= push_leaf_right(trans
, root
, path
, 1,
3901 if (wret
< 0 && wret
!= -ENOSPC
)
3905 if (btrfs_header_nritems(leaf
) == 0) {
3906 path
->slots
[1] = slot
;
3907 ret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
3909 free_extent_buffer(leaf
);
3911 /* if we're still in the path, make sure
3912 * we're dirty. Otherwise, one of the
3913 * push_leaf functions must have already
3914 * dirtied this buffer
3916 if (path
->nodes
[0] == leaf
)
3917 btrfs_mark_buffer_dirty(leaf
);
3918 free_extent_buffer(leaf
);
3921 btrfs_mark_buffer_dirty(leaf
);
3928 * search the tree again to find a leaf with lesser keys
3929 * returns 0 if it found something or 1 if there are no lesser leaves.
3930 * returns < 0 on io errors.
3932 * This may release the path, and so you may lose any locks held at the
3935 int btrfs_prev_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
3937 struct btrfs_key key
;
3938 struct btrfs_disk_key found_key
;
3941 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, 0);
3945 else if (key
.type
> 0)
3947 else if (key
.objectid
> 0)
3952 btrfs_release_path(root
, path
);
3953 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3956 btrfs_item_key(path
->nodes
[0], &found_key
, 0);
3957 ret
= comp_keys(&found_key
, &key
);
3964 * A helper function to walk down the tree starting at min_key, and looking
3965 * for nodes or leaves that are either in cache or have a minimum
3966 * transaction id. This is used by the btree defrag code, and tree logging
3968 * This does not cow, but it does stuff the starting key it finds back
3969 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3970 * key and get a writable path.
3972 * This does lock as it descends, and path->keep_locks should be set
3973 * to 1 by the caller.
3975 * This honors path->lowest_level to prevent descent past a given level
3978 * min_trans indicates the oldest transaction that you are interested
3979 * in walking through. Any nodes or leaves older than min_trans are
3980 * skipped over (without reading them).
3982 * returns zero if something useful was found, < 0 on error and 1 if there
3983 * was nothing in the tree that matched the search criteria.
3985 int btrfs_search_forward(struct btrfs_root
*root
, struct btrfs_key
*min_key
,
3986 struct btrfs_key
*max_key
,
3987 struct btrfs_path
*path
, int cache_only
,
3990 struct extent_buffer
*cur
;
3991 struct btrfs_key found_key
;
3998 WARN_ON(!path
->keep_locks
);
4000 cur
= btrfs_lock_root_node(root
);
4001 level
= btrfs_header_level(cur
);
4002 WARN_ON(path
->nodes
[level
]);
4003 path
->nodes
[level
] = cur
;
4004 path
->locks
[level
] = 1;
4006 if (btrfs_header_generation(cur
) < min_trans
) {
4011 nritems
= btrfs_header_nritems(cur
);
4012 level
= btrfs_header_level(cur
);
4013 sret
= bin_search(cur
, min_key
, level
, &slot
);
4015 /* at the lowest level, we're done, setup the path and exit */
4016 if (level
== path
->lowest_level
) {
4017 if (slot
>= nritems
)
4020 path
->slots
[level
] = slot
;
4021 btrfs_item_key_to_cpu(cur
, &found_key
, slot
);
4024 if (sret
&& slot
> 0)
4027 * check this node pointer against the cache_only and
4028 * min_trans parameters. If it isn't in cache or is too
4029 * old, skip to the next one.
4031 while (slot
< nritems
) {
4034 struct extent_buffer
*tmp
;
4035 struct btrfs_disk_key disk_key
;
4037 blockptr
= btrfs_node_blockptr(cur
, slot
);
4038 gen
= btrfs_node_ptr_generation(cur
, slot
);
4039 if (gen
< min_trans
) {
4047 btrfs_node_key(cur
, &disk_key
, slot
);
4048 if (comp_keys(&disk_key
, max_key
) >= 0) {
4054 tmp
= btrfs_find_tree_block(root
, blockptr
,
4055 btrfs_level_size(root
, level
- 1));
4057 if (tmp
&& btrfs_buffer_uptodate(tmp
, gen
)) {
4058 free_extent_buffer(tmp
);
4062 free_extent_buffer(tmp
);
4067 * we didn't find a candidate key in this node, walk forward
4068 * and find another one
4070 if (slot
>= nritems
) {
4071 path
->slots
[level
] = slot
;
4072 btrfs_set_path_blocking(path
);
4073 sret
= btrfs_find_next_key(root
, path
, min_key
, level
,
4074 cache_only
, min_trans
);
4076 btrfs_release_path(root
, path
);
4082 /* save our key for returning back */
4083 btrfs_node_key_to_cpu(cur
, &found_key
, slot
);
4084 path
->slots
[level
] = slot
;
4085 if (level
== path
->lowest_level
) {
4087 unlock_up(path
, level
, 1);
4090 btrfs_set_path_blocking(path
);
4091 cur
= read_node_slot(root
, cur
, slot
);
4094 btrfs_tree_lock(cur
);
4096 path
->locks
[level
- 1] = 1;
4097 path
->nodes
[level
- 1] = cur
;
4098 unlock_up(path
, level
, 1);
4099 btrfs_clear_path_blocking(path
, NULL
);
4103 memcpy(min_key
, &found_key
, sizeof(found_key
));
4104 btrfs_set_path_blocking(path
);
4109 * this is similar to btrfs_next_leaf, but does not try to preserve
4110 * and fixup the path. It looks for and returns the next key in the
4111 * tree based on the current path and the cache_only and min_trans
4114 * 0 is returned if another key is found, < 0 if there are any errors
4115 * and 1 is returned if there are no higher keys in the tree
4117 * path->keep_locks should be set to 1 on the search made before
4118 * calling this function.
4120 int btrfs_find_next_key(struct btrfs_root
*root
, struct btrfs_path
*path
,
4121 struct btrfs_key
*key
, int level
,
4122 int cache_only
, u64 min_trans
)
4125 struct extent_buffer
*c
;
4127 WARN_ON(!path
->keep_locks
);
4128 while (level
< BTRFS_MAX_LEVEL
) {
4129 if (!path
->nodes
[level
])
4132 slot
= path
->slots
[level
] + 1;
4133 c
= path
->nodes
[level
];
4135 if (slot
>= btrfs_header_nritems(c
)) {
4138 struct btrfs_key cur_key
;
4139 if (level
+ 1 >= BTRFS_MAX_LEVEL
||
4140 !path
->nodes
[level
+ 1])
4143 if (path
->locks
[level
+ 1]) {
4148 slot
= btrfs_header_nritems(c
) - 1;
4150 btrfs_item_key_to_cpu(c
, &cur_key
, slot
);
4152 btrfs_node_key_to_cpu(c
, &cur_key
, slot
);
4154 orig_lowest
= path
->lowest_level
;
4155 btrfs_release_path(root
, path
);
4156 path
->lowest_level
= level
;
4157 ret
= btrfs_search_slot(NULL
, root
, &cur_key
, path
,
4159 path
->lowest_level
= orig_lowest
;
4163 c
= path
->nodes
[level
];
4164 slot
= path
->slots
[level
];
4171 btrfs_item_key_to_cpu(c
, key
, slot
);
4173 u64 blockptr
= btrfs_node_blockptr(c
, slot
);
4174 u64 gen
= btrfs_node_ptr_generation(c
, slot
);
4177 struct extent_buffer
*cur
;
4178 cur
= btrfs_find_tree_block(root
, blockptr
,
4179 btrfs_level_size(root
, level
- 1));
4180 if (!cur
|| !btrfs_buffer_uptodate(cur
, gen
)) {
4183 free_extent_buffer(cur
);
4186 free_extent_buffer(cur
);
4188 if (gen
< min_trans
) {
4192 btrfs_node_key_to_cpu(c
, key
, slot
);
4200 * search the tree again to find a leaf with greater keys
4201 * returns 0 if it found something or 1 if there are no greater leaves.
4202 * returns < 0 on io errors.
4204 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
4208 struct extent_buffer
*c
;
4209 struct extent_buffer
*next
;
4210 struct btrfs_key key
;
4213 int old_spinning
= path
->leave_spinning
;
4214 int force_blocking
= 0;
4216 nritems
= btrfs_header_nritems(path
->nodes
[0]);
4221 * we take the blocks in an order that upsets lockdep. Using
4222 * blocking mode is the only way around it.
4224 #ifdef CONFIG_DEBUG_LOCK_ALLOC
4228 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, nritems
- 1);
4232 btrfs_release_path(root
, path
);
4234 path
->keep_locks
= 1;
4236 if (!force_blocking
)
4237 path
->leave_spinning
= 1;
4239 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4240 path
->keep_locks
= 0;
4245 nritems
= btrfs_header_nritems(path
->nodes
[0]);
4247 * by releasing the path above we dropped all our locks. A balance
4248 * could have added more items next to the key that used to be
4249 * at the very end of the block. So, check again here and
4250 * advance the path if there are now more items available.
4252 if (nritems
> 0 && path
->slots
[0] < nritems
- 1) {
4259 while (level
< BTRFS_MAX_LEVEL
) {
4260 if (!path
->nodes
[level
]) {
4265 slot
= path
->slots
[level
] + 1;
4266 c
= path
->nodes
[level
];
4267 if (slot
>= btrfs_header_nritems(c
)) {
4269 if (level
== BTRFS_MAX_LEVEL
) {
4277 btrfs_tree_unlock(next
);
4278 free_extent_buffer(next
);
4282 ret
= read_block_for_search(NULL
, root
, path
, &next
, level
,
4288 btrfs_release_path(root
, path
);
4292 if (!path
->skip_locking
) {
4293 ret
= btrfs_try_spin_lock(next
);
4295 btrfs_set_path_blocking(path
);
4296 btrfs_tree_lock(next
);
4297 if (!force_blocking
)
4298 btrfs_clear_path_blocking(path
, next
);
4301 btrfs_set_lock_blocking(next
);
4305 path
->slots
[level
] = slot
;
4308 c
= path
->nodes
[level
];
4309 if (path
->locks
[level
])
4310 btrfs_tree_unlock(c
);
4312 free_extent_buffer(c
);
4313 path
->nodes
[level
] = next
;
4314 path
->slots
[level
] = 0;
4315 if (!path
->skip_locking
)
4316 path
->locks
[level
] = 1;
4321 ret
= read_block_for_search(NULL
, root
, path
, &next
, level
,
4327 btrfs_release_path(root
, path
);
4331 if (!path
->skip_locking
) {
4332 btrfs_assert_tree_locked(path
->nodes
[level
]);
4333 ret
= btrfs_try_spin_lock(next
);
4335 btrfs_set_path_blocking(path
);
4336 btrfs_tree_lock(next
);
4337 if (!force_blocking
)
4338 btrfs_clear_path_blocking(path
, next
);
4341 btrfs_set_lock_blocking(next
);
4346 unlock_up(path
, 0, 1);
4347 path
->leave_spinning
= old_spinning
;
4349 btrfs_set_path_blocking(path
);
4355 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4356 * searching until it gets past min_objectid or finds an item of 'type'
4358 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4360 int btrfs_previous_item(struct btrfs_root
*root
,
4361 struct btrfs_path
*path
, u64 min_objectid
,
4364 struct btrfs_key found_key
;
4365 struct extent_buffer
*leaf
;
4370 if (path
->slots
[0] == 0) {
4371 btrfs_set_path_blocking(path
);
4372 ret
= btrfs_prev_leaf(root
, path
);
4378 leaf
= path
->nodes
[0];
4379 nritems
= btrfs_header_nritems(leaf
);
4382 if (path
->slots
[0] == nritems
)
4385 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4386 if (found_key
.objectid
< min_objectid
)
4388 if (found_key
.type
== type
)
4390 if (found_key
.objectid
== min_objectid
&&
4391 found_key
.type
< type
)