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
);
42 struct btrfs_path
*btrfs_alloc_path(void)
44 struct btrfs_path
*path
;
45 path
= kmem_cache_zalloc(btrfs_path_cachep
, GFP_NOFS
);
50 * set all locked nodes in the path to blocking locks. This should
51 * be done before scheduling
53 noinline
void btrfs_set_path_blocking(struct btrfs_path
*p
)
56 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
57 if (p
->nodes
[i
] && p
->locks
[i
])
58 btrfs_set_lock_blocking(p
->nodes
[i
]);
63 * reset all the locked nodes in the patch to spinning locks.
65 * held is used to keep lockdep happy, when lockdep is enabled
66 * we set held to a blocking lock before we go around and
67 * retake all the spinlocks in the path. You can safely use NULL
70 noinline
void btrfs_clear_path_blocking(struct btrfs_path
*p
,
71 struct extent_buffer
*held
)
75 #ifdef CONFIG_DEBUG_LOCK_ALLOC
76 /* lockdep really cares that we take all of these spinlocks
77 * in the right order. If any of the locks in the path are not
78 * currently blocking, it is going to complain. So, make really
79 * really sure by forcing the path to blocking before we clear
83 btrfs_set_lock_blocking(held
);
84 btrfs_set_path_blocking(p
);
87 for (i
= BTRFS_MAX_LEVEL
- 1; i
>= 0; i
--) {
88 if (p
->nodes
[i
] && p
->locks
[i
])
89 btrfs_clear_lock_blocking(p
->nodes
[i
]);
92 #ifdef CONFIG_DEBUG_LOCK_ALLOC
94 btrfs_clear_lock_blocking(held
);
98 /* this also releases the path */
99 void btrfs_free_path(struct btrfs_path
*p
)
103 btrfs_release_path(p
);
104 kmem_cache_free(btrfs_path_cachep
, p
);
108 * path release drops references on the extent buffers in the path
109 * and it drops any locks held by this path
111 * It is safe to call this on paths that no locks or extent buffers held.
113 noinline
void btrfs_release_path(struct btrfs_path
*p
)
117 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
122 btrfs_tree_unlock(p
->nodes
[i
]);
125 free_extent_buffer(p
->nodes
[i
]);
131 * safely gets a reference on the root node of a tree. A lock
132 * is not taken, so a concurrent writer may put a different node
133 * at the root of the tree. See btrfs_lock_root_node for the
136 * The extent buffer returned by this has a reference taken, so
137 * it won't disappear. It may stop being the root of the tree
138 * at any time because there are no locks held.
140 struct extent_buffer
*btrfs_root_node(struct btrfs_root
*root
)
142 struct extent_buffer
*eb
;
145 eb
= rcu_dereference(root
->node
);
146 extent_buffer_get(eb
);
151 /* loop around taking references on and locking the root node of the
152 * tree until you end up with a lock on the root. A locked buffer
153 * is returned, with a reference held.
155 struct extent_buffer
*btrfs_lock_root_node(struct btrfs_root
*root
)
157 struct extent_buffer
*eb
;
160 eb
= btrfs_root_node(root
);
162 if (eb
== root
->node
)
164 btrfs_tree_unlock(eb
);
165 free_extent_buffer(eb
);
170 /* cowonly root (everything not a reference counted cow subvolume), just get
171 * put onto a simple dirty list. transaction.c walks this to make sure they
172 * get properly updated on disk.
174 static void add_root_to_dirty_list(struct btrfs_root
*root
)
176 if (root
->track_dirty
&& list_empty(&root
->dirty_list
)) {
177 list_add(&root
->dirty_list
,
178 &root
->fs_info
->dirty_cowonly_roots
);
183 * used by snapshot creation to make a copy of a root for a tree with
184 * a given objectid. The buffer with the new root node is returned in
185 * cow_ret, and this func returns zero on success or a negative error code.
187 int btrfs_copy_root(struct btrfs_trans_handle
*trans
,
188 struct btrfs_root
*root
,
189 struct extent_buffer
*buf
,
190 struct extent_buffer
**cow_ret
, u64 new_root_objectid
)
192 struct extent_buffer
*cow
;
195 struct btrfs_disk_key disk_key
;
197 WARN_ON(root
->ref_cows
&& trans
->transid
!=
198 root
->fs_info
->running_transaction
->transid
);
199 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
201 level
= btrfs_header_level(buf
);
203 btrfs_item_key(buf
, &disk_key
, 0);
205 btrfs_node_key(buf
, &disk_key
, 0);
207 cow
= btrfs_alloc_free_block(trans
, root
, buf
->len
, 0,
208 new_root_objectid
, &disk_key
, level
,
213 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
214 btrfs_set_header_bytenr(cow
, cow
->start
);
215 btrfs_set_header_generation(cow
, trans
->transid
);
216 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
217 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
218 BTRFS_HEADER_FLAG_RELOC
);
219 if (new_root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
220 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
222 btrfs_set_header_owner(cow
, new_root_objectid
);
224 write_extent_buffer(cow
, root
->fs_info
->fsid
,
225 (unsigned long)btrfs_header_fsid(cow
),
228 WARN_ON(btrfs_header_generation(buf
) > trans
->transid
);
229 if (new_root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
230 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
232 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
237 btrfs_mark_buffer_dirty(cow
);
243 * check if the tree block can be shared by multiple trees
245 int btrfs_block_can_be_shared(struct btrfs_root
*root
,
246 struct extent_buffer
*buf
)
249 * Tree blocks not in refernece counted trees and tree roots
250 * are never shared. If a block was allocated after the last
251 * snapshot and the block was not allocated by tree relocation,
252 * we know the block is not shared.
254 if (root
->ref_cows
&&
255 buf
!= root
->node
&& buf
!= root
->commit_root
&&
256 (btrfs_header_generation(buf
) <=
257 btrfs_root_last_snapshot(&root
->root_item
) ||
258 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)))
260 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
261 if (root
->ref_cows
&&
262 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
268 static noinline
int update_ref_for_cow(struct btrfs_trans_handle
*trans
,
269 struct btrfs_root
*root
,
270 struct extent_buffer
*buf
,
271 struct extent_buffer
*cow
,
281 * Backrefs update rules:
283 * Always use full backrefs for extent pointers in tree block
284 * allocated by tree relocation.
286 * If a shared tree block is no longer referenced by its owner
287 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
288 * use full backrefs for extent pointers in tree block.
290 * If a tree block is been relocating
291 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
292 * use full backrefs for extent pointers in tree block.
293 * The reason for this is some operations (such as drop tree)
294 * are only allowed for blocks use full backrefs.
297 if (btrfs_block_can_be_shared(root
, buf
)) {
298 ret
= btrfs_lookup_extent_info(trans
, root
, buf
->start
,
299 buf
->len
, &refs
, &flags
);
304 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
||
305 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
306 flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
311 owner
= btrfs_header_owner(buf
);
312 BUG_ON(owner
== BTRFS_TREE_RELOC_OBJECTID
&&
313 !(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
));
316 if ((owner
== root
->root_key
.objectid
||
317 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) &&
318 !(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
)) {
319 ret
= btrfs_inc_ref(trans
, root
, buf
, 1);
322 if (root
->root_key
.objectid
==
323 BTRFS_TREE_RELOC_OBJECTID
) {
324 ret
= btrfs_dec_ref(trans
, root
, buf
, 0);
326 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
329 new_flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
332 if (root
->root_key
.objectid
==
333 BTRFS_TREE_RELOC_OBJECTID
)
334 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
336 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
339 if (new_flags
!= 0) {
340 ret
= btrfs_set_disk_extent_flags(trans
, root
,
347 if (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
348 if (root
->root_key
.objectid
==
349 BTRFS_TREE_RELOC_OBJECTID
)
350 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
352 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
354 ret
= btrfs_dec_ref(trans
, root
, buf
, 1);
357 clean_tree_block(trans
, root
, buf
);
364 * does the dirty work in cow of a single block. The parent block (if
365 * supplied) is updated to point to the new cow copy. The new buffer is marked
366 * dirty and returned locked. If you modify the block it needs to be marked
369 * search_start -- an allocation hint for the new block
371 * empty_size -- a hint that you plan on doing more cow. This is the size in
372 * bytes the allocator should try to find free next to the block it returns.
373 * This is just a hint and may be ignored by the allocator.
375 static noinline
int __btrfs_cow_block(struct btrfs_trans_handle
*trans
,
376 struct btrfs_root
*root
,
377 struct extent_buffer
*buf
,
378 struct extent_buffer
*parent
, int parent_slot
,
379 struct extent_buffer
**cow_ret
,
380 u64 search_start
, u64 empty_size
)
382 struct btrfs_disk_key disk_key
;
383 struct extent_buffer
*cow
;
392 btrfs_assert_tree_locked(buf
);
394 WARN_ON(root
->ref_cows
&& trans
->transid
!=
395 root
->fs_info
->running_transaction
->transid
);
396 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
398 level
= btrfs_header_level(buf
);
401 btrfs_item_key(buf
, &disk_key
, 0);
403 btrfs_node_key(buf
, &disk_key
, 0);
405 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
407 parent_start
= parent
->start
;
413 cow
= btrfs_alloc_free_block(trans
, root
, buf
->len
, parent_start
,
414 root
->root_key
.objectid
, &disk_key
,
415 level
, search_start
, empty_size
);
419 /* cow is set to blocking by btrfs_init_new_buffer */
421 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
422 btrfs_set_header_bytenr(cow
, cow
->start
);
423 btrfs_set_header_generation(cow
, trans
->transid
);
424 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
425 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
426 BTRFS_HEADER_FLAG_RELOC
);
427 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
428 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
430 btrfs_set_header_owner(cow
, root
->root_key
.objectid
);
432 write_extent_buffer(cow
, root
->fs_info
->fsid
,
433 (unsigned long)btrfs_header_fsid(cow
),
436 update_ref_for_cow(trans
, root
, buf
, cow
, &last_ref
);
439 btrfs_reloc_cow_block(trans
, root
, buf
, cow
);
441 if (buf
== root
->node
) {
442 WARN_ON(parent
&& parent
!= buf
);
443 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
||
444 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
445 parent_start
= buf
->start
;
449 extent_buffer_get(cow
);
450 rcu_assign_pointer(root
->node
, cow
);
452 btrfs_free_tree_block(trans
, root
, buf
, parent_start
,
454 free_extent_buffer(buf
);
455 add_root_to_dirty_list(root
);
457 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
458 parent_start
= parent
->start
;
462 WARN_ON(trans
->transid
!= btrfs_header_generation(parent
));
463 btrfs_set_node_blockptr(parent
, parent_slot
,
465 btrfs_set_node_ptr_generation(parent
, parent_slot
,
467 btrfs_mark_buffer_dirty(parent
);
468 btrfs_free_tree_block(trans
, root
, buf
, parent_start
,
472 btrfs_tree_unlock(buf
);
473 free_extent_buffer(buf
);
474 btrfs_mark_buffer_dirty(cow
);
479 static inline int should_cow_block(struct btrfs_trans_handle
*trans
,
480 struct btrfs_root
*root
,
481 struct extent_buffer
*buf
)
483 if (btrfs_header_generation(buf
) == trans
->transid
&&
484 !btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
) &&
485 !(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
&&
486 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)))
492 * cows a single block, see __btrfs_cow_block for the real work.
493 * This version of it has extra checks so that a block isn't cow'd more than
494 * once per transaction, as long as it hasn't been written yet
496 noinline
int btrfs_cow_block(struct btrfs_trans_handle
*trans
,
497 struct btrfs_root
*root
, struct extent_buffer
*buf
,
498 struct extent_buffer
*parent
, int parent_slot
,
499 struct extent_buffer
**cow_ret
)
504 if (trans
->transaction
!= root
->fs_info
->running_transaction
) {
505 printk(KERN_CRIT
"trans %llu running %llu\n",
506 (unsigned long long)trans
->transid
,
508 root
->fs_info
->running_transaction
->transid
);
511 if (trans
->transid
!= root
->fs_info
->generation
) {
512 printk(KERN_CRIT
"trans %llu running %llu\n",
513 (unsigned long long)trans
->transid
,
514 (unsigned long long)root
->fs_info
->generation
);
518 if (!should_cow_block(trans
, root
, buf
)) {
523 search_start
= buf
->start
& ~((u64
)(1024 * 1024 * 1024) - 1);
526 btrfs_set_lock_blocking(parent
);
527 btrfs_set_lock_blocking(buf
);
529 ret
= __btrfs_cow_block(trans
, root
, buf
, parent
,
530 parent_slot
, cow_ret
, search_start
, 0);
532 trace_btrfs_cow_block(root
, buf
, *cow_ret
);
538 * helper function for defrag to decide if two blocks pointed to by a
539 * node are actually close by
541 static int close_blocks(u64 blocknr
, u64 other
, u32 blocksize
)
543 if (blocknr
< other
&& other
- (blocknr
+ blocksize
) < 32768)
545 if (blocknr
> other
&& blocknr
- (other
+ blocksize
) < 32768)
551 * compare two keys in a memcmp fashion
553 static int comp_keys(struct btrfs_disk_key
*disk
, struct btrfs_key
*k2
)
557 btrfs_disk_key_to_cpu(&k1
, disk
);
559 return btrfs_comp_cpu_keys(&k1
, k2
);
563 * same as comp_keys only with two btrfs_key's
565 int btrfs_comp_cpu_keys(struct btrfs_key
*k1
, struct btrfs_key
*k2
)
567 if (k1
->objectid
> k2
->objectid
)
569 if (k1
->objectid
< k2
->objectid
)
571 if (k1
->type
> k2
->type
)
573 if (k1
->type
< k2
->type
)
575 if (k1
->offset
> k2
->offset
)
577 if (k1
->offset
< k2
->offset
)
583 * this is used by the defrag code to go through all the
584 * leaves pointed to by a node and reallocate them so that
585 * disk order is close to key order
587 int btrfs_realloc_node(struct btrfs_trans_handle
*trans
,
588 struct btrfs_root
*root
, struct extent_buffer
*parent
,
589 int start_slot
, int cache_only
, u64
*last_ret
,
590 struct btrfs_key
*progress
)
592 struct extent_buffer
*cur
;
595 u64 search_start
= *last_ret
;
605 int progress_passed
= 0;
606 struct btrfs_disk_key disk_key
;
608 parent_level
= btrfs_header_level(parent
);
609 if (cache_only
&& parent_level
!= 1)
612 if (trans
->transaction
!= root
->fs_info
->running_transaction
)
614 if (trans
->transid
!= root
->fs_info
->generation
)
617 parent_nritems
= btrfs_header_nritems(parent
);
618 blocksize
= btrfs_level_size(root
, parent_level
- 1);
619 end_slot
= parent_nritems
;
621 if (parent_nritems
== 1)
624 btrfs_set_lock_blocking(parent
);
626 for (i
= start_slot
; i
< end_slot
; i
++) {
629 if (!parent
->map_token
) {
630 map_extent_buffer(parent
,
631 btrfs_node_key_ptr_offset(i
),
632 sizeof(struct btrfs_key_ptr
),
633 &parent
->map_token
, &parent
->kaddr
,
634 &parent
->map_start
, &parent
->map_len
,
637 btrfs_node_key(parent
, &disk_key
, i
);
638 if (!progress_passed
&& comp_keys(&disk_key
, progress
) < 0)
642 blocknr
= btrfs_node_blockptr(parent
, i
);
643 gen
= btrfs_node_ptr_generation(parent
, i
);
645 last_block
= blocknr
;
648 other
= btrfs_node_blockptr(parent
, i
- 1);
649 close
= close_blocks(blocknr
, other
, blocksize
);
651 if (!close
&& i
< end_slot
- 2) {
652 other
= btrfs_node_blockptr(parent
, i
+ 1);
653 close
= close_blocks(blocknr
, other
, blocksize
);
656 last_block
= blocknr
;
659 if (parent
->map_token
) {
660 unmap_extent_buffer(parent
, parent
->map_token
,
662 parent
->map_token
= NULL
;
665 cur
= btrfs_find_tree_block(root
, blocknr
, blocksize
);
667 uptodate
= btrfs_buffer_uptodate(cur
, gen
);
670 if (!cur
|| !uptodate
) {
672 free_extent_buffer(cur
);
676 cur
= read_tree_block(root
, blocknr
,
680 } else if (!uptodate
) {
681 btrfs_read_buffer(cur
, gen
);
684 if (search_start
== 0)
685 search_start
= last_block
;
687 btrfs_tree_lock(cur
);
688 btrfs_set_lock_blocking(cur
);
689 err
= __btrfs_cow_block(trans
, root
, cur
, parent
, i
,
692 (end_slot
- i
) * blocksize
));
694 btrfs_tree_unlock(cur
);
695 free_extent_buffer(cur
);
698 search_start
= cur
->start
;
699 last_block
= cur
->start
;
700 *last_ret
= search_start
;
701 btrfs_tree_unlock(cur
);
702 free_extent_buffer(cur
);
704 if (parent
->map_token
) {
705 unmap_extent_buffer(parent
, parent
->map_token
,
707 parent
->map_token
= NULL
;
713 * The leaf data grows from end-to-front in the node.
714 * this returns the address of the start of the last item,
715 * which is the stop of the leaf data stack
717 static inline unsigned int leaf_data_end(struct btrfs_root
*root
,
718 struct extent_buffer
*leaf
)
720 u32 nr
= btrfs_header_nritems(leaf
);
722 return BTRFS_LEAF_DATA_SIZE(root
);
723 return btrfs_item_offset_nr(leaf
, nr
- 1);
728 * search for key in the extent_buffer. The items start at offset p,
729 * and they are item_size apart. There are 'max' items in p.
731 * the slot in the array is returned via slot, and it points to
732 * the place where you would insert key if it is not found in
735 * slot may point to max if the key is bigger than all of the keys
737 static noinline
int generic_bin_search(struct extent_buffer
*eb
,
739 int item_size
, struct btrfs_key
*key
,
746 struct btrfs_disk_key
*tmp
= NULL
;
747 struct btrfs_disk_key unaligned
;
748 unsigned long offset
;
749 char *map_token
= NULL
;
751 unsigned long map_start
= 0;
752 unsigned long map_len
= 0;
756 mid
= (low
+ high
) / 2;
757 offset
= p
+ mid
* item_size
;
759 if (!map_token
|| offset
< map_start
||
760 (offset
+ sizeof(struct btrfs_disk_key
)) >
761 map_start
+ map_len
) {
763 unmap_extent_buffer(eb
, map_token
, KM_USER0
);
767 err
= map_private_extent_buffer(eb
, offset
,
768 sizeof(struct btrfs_disk_key
),
770 &map_start
, &map_len
, KM_USER0
);
773 tmp
= (struct btrfs_disk_key
*)(kaddr
+ offset
-
776 read_extent_buffer(eb
, &unaligned
,
777 offset
, sizeof(unaligned
));
782 tmp
= (struct btrfs_disk_key
*)(kaddr
+ offset
-
785 ret
= comp_keys(tmp
, key
);
794 unmap_extent_buffer(eb
, map_token
, KM_USER0
);
800 unmap_extent_buffer(eb
, map_token
, KM_USER0
);
805 * simple bin_search frontend that does the right thing for
808 static int bin_search(struct extent_buffer
*eb
, struct btrfs_key
*key
,
809 int level
, int *slot
)
812 return generic_bin_search(eb
,
813 offsetof(struct btrfs_leaf
, items
),
814 sizeof(struct btrfs_item
),
815 key
, btrfs_header_nritems(eb
),
818 return generic_bin_search(eb
,
819 offsetof(struct btrfs_node
, ptrs
),
820 sizeof(struct btrfs_key_ptr
),
821 key
, btrfs_header_nritems(eb
),
827 int btrfs_bin_search(struct extent_buffer
*eb
, struct btrfs_key
*key
,
828 int level
, int *slot
)
830 return bin_search(eb
, key
, level
, slot
);
833 static void root_add_used(struct btrfs_root
*root
, u32 size
)
835 spin_lock(&root
->accounting_lock
);
836 btrfs_set_root_used(&root
->root_item
,
837 btrfs_root_used(&root
->root_item
) + size
);
838 spin_unlock(&root
->accounting_lock
);
841 static void root_sub_used(struct btrfs_root
*root
, u32 size
)
843 spin_lock(&root
->accounting_lock
);
844 btrfs_set_root_used(&root
->root_item
,
845 btrfs_root_used(&root
->root_item
) - size
);
846 spin_unlock(&root
->accounting_lock
);
849 /* given a node and slot number, this reads the blocks it points to. The
850 * extent buffer is returned with a reference taken (but unlocked).
851 * NULL is returned on error.
853 static noinline
struct extent_buffer
*read_node_slot(struct btrfs_root
*root
,
854 struct extent_buffer
*parent
, int slot
)
856 int level
= btrfs_header_level(parent
);
859 if (slot
>= btrfs_header_nritems(parent
))
864 return read_tree_block(root
, btrfs_node_blockptr(parent
, slot
),
865 btrfs_level_size(root
, level
- 1),
866 btrfs_node_ptr_generation(parent
, slot
));
870 * node level balancing, used to make sure nodes are in proper order for
871 * item deletion. We balance from the top down, so we have to make sure
872 * that a deletion won't leave an node completely empty later on.
874 static noinline
int balance_level(struct btrfs_trans_handle
*trans
,
875 struct btrfs_root
*root
,
876 struct btrfs_path
*path
, int level
)
878 struct extent_buffer
*right
= NULL
;
879 struct extent_buffer
*mid
;
880 struct extent_buffer
*left
= NULL
;
881 struct extent_buffer
*parent
= NULL
;
885 int orig_slot
= path
->slots
[level
];
891 mid
= path
->nodes
[level
];
893 WARN_ON(!path
->locks
[level
]);
894 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
896 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
898 if (level
< BTRFS_MAX_LEVEL
- 1)
899 parent
= path
->nodes
[level
+ 1];
900 pslot
= path
->slots
[level
+ 1];
903 * deal with the case where there is only one pointer in the root
904 * by promoting the node below to a root
907 struct extent_buffer
*child
;
909 if (btrfs_header_nritems(mid
) != 1)
912 /* promote the child to a root */
913 child
= read_node_slot(root
, mid
, 0);
915 btrfs_tree_lock(child
);
916 btrfs_set_lock_blocking(child
);
917 ret
= btrfs_cow_block(trans
, root
, child
, mid
, 0, &child
);
919 btrfs_tree_unlock(child
);
920 free_extent_buffer(child
);
924 rcu_assign_pointer(root
->node
, child
);
926 add_root_to_dirty_list(root
);
927 btrfs_tree_unlock(child
);
929 path
->locks
[level
] = 0;
930 path
->nodes
[level
] = NULL
;
931 clean_tree_block(trans
, root
, mid
);
932 btrfs_tree_unlock(mid
);
933 /* once for the path */
934 free_extent_buffer(mid
);
936 root_sub_used(root
, mid
->len
);
937 btrfs_free_tree_block(trans
, root
, mid
, 0, 1);
938 /* once for the root ptr */
939 free_extent_buffer(mid
);
942 if (btrfs_header_nritems(mid
) >
943 BTRFS_NODEPTRS_PER_BLOCK(root
) / 4)
946 btrfs_header_nritems(mid
);
948 left
= read_node_slot(root
, parent
, pslot
- 1);
950 btrfs_tree_lock(left
);
951 btrfs_set_lock_blocking(left
);
952 wret
= btrfs_cow_block(trans
, root
, left
,
953 parent
, pslot
- 1, &left
);
959 right
= read_node_slot(root
, parent
, pslot
+ 1);
961 btrfs_tree_lock(right
);
962 btrfs_set_lock_blocking(right
);
963 wret
= btrfs_cow_block(trans
, root
, right
,
964 parent
, pslot
+ 1, &right
);
971 /* first, try to make some room in the middle buffer */
973 orig_slot
+= btrfs_header_nritems(left
);
974 wret
= push_node_left(trans
, root
, left
, mid
, 1);
977 btrfs_header_nritems(mid
);
981 * then try to empty the right most buffer into the middle
984 wret
= push_node_left(trans
, root
, mid
, right
, 1);
985 if (wret
< 0 && wret
!= -ENOSPC
)
987 if (btrfs_header_nritems(right
) == 0) {
988 clean_tree_block(trans
, root
, right
);
989 btrfs_tree_unlock(right
);
990 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
+
994 root_sub_used(root
, right
->len
);
995 btrfs_free_tree_block(trans
, root
, right
, 0, 1);
996 free_extent_buffer(right
);
999 struct btrfs_disk_key right_key
;
1000 btrfs_node_key(right
, &right_key
, 0);
1001 btrfs_set_node_key(parent
, &right_key
, pslot
+ 1);
1002 btrfs_mark_buffer_dirty(parent
);
1005 if (btrfs_header_nritems(mid
) == 1) {
1007 * we're not allowed to leave a node with one item in the
1008 * tree during a delete. A deletion from lower in the tree
1009 * could try to delete the only pointer in this node.
1010 * So, pull some keys from the left.
1011 * There has to be a left pointer at this point because
1012 * otherwise we would have pulled some pointers from the
1016 wret
= balance_node_right(trans
, root
, mid
, left
);
1022 wret
= push_node_left(trans
, root
, left
, mid
, 1);
1028 if (btrfs_header_nritems(mid
) == 0) {
1029 clean_tree_block(trans
, root
, mid
);
1030 btrfs_tree_unlock(mid
);
1031 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
);
1034 root_sub_used(root
, mid
->len
);
1035 btrfs_free_tree_block(trans
, root
, mid
, 0, 1);
1036 free_extent_buffer(mid
);
1039 /* update the parent key to reflect our changes */
1040 struct btrfs_disk_key mid_key
;
1041 btrfs_node_key(mid
, &mid_key
, 0);
1042 btrfs_set_node_key(parent
, &mid_key
, pslot
);
1043 btrfs_mark_buffer_dirty(parent
);
1046 /* update the path */
1048 if (btrfs_header_nritems(left
) > orig_slot
) {
1049 extent_buffer_get(left
);
1050 /* left was locked after cow */
1051 path
->nodes
[level
] = left
;
1052 path
->slots
[level
+ 1] -= 1;
1053 path
->slots
[level
] = orig_slot
;
1055 btrfs_tree_unlock(mid
);
1056 free_extent_buffer(mid
);
1059 orig_slot
-= btrfs_header_nritems(left
);
1060 path
->slots
[level
] = orig_slot
;
1063 /* double check we haven't messed things up */
1065 btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]))
1069 btrfs_tree_unlock(right
);
1070 free_extent_buffer(right
);
1073 if (path
->nodes
[level
] != left
)
1074 btrfs_tree_unlock(left
);
1075 free_extent_buffer(left
);
1080 /* Node balancing for insertion. Here we only split or push nodes around
1081 * when they are completely full. This is also done top down, so we
1082 * have to be pessimistic.
1084 static noinline
int push_nodes_for_insert(struct btrfs_trans_handle
*trans
,
1085 struct btrfs_root
*root
,
1086 struct btrfs_path
*path
, int level
)
1088 struct extent_buffer
*right
= NULL
;
1089 struct extent_buffer
*mid
;
1090 struct extent_buffer
*left
= NULL
;
1091 struct extent_buffer
*parent
= NULL
;
1095 int orig_slot
= path
->slots
[level
];
1100 mid
= path
->nodes
[level
];
1101 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
1103 if (level
< BTRFS_MAX_LEVEL
- 1)
1104 parent
= path
->nodes
[level
+ 1];
1105 pslot
= path
->slots
[level
+ 1];
1110 left
= read_node_slot(root
, parent
, pslot
- 1);
1112 /* first, try to make some room in the middle buffer */
1116 btrfs_tree_lock(left
);
1117 btrfs_set_lock_blocking(left
);
1119 left_nr
= btrfs_header_nritems(left
);
1120 if (left_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
1123 ret
= btrfs_cow_block(trans
, root
, left
, parent
,
1128 wret
= push_node_left(trans
, root
,
1135 struct btrfs_disk_key disk_key
;
1136 orig_slot
+= left_nr
;
1137 btrfs_node_key(mid
, &disk_key
, 0);
1138 btrfs_set_node_key(parent
, &disk_key
, pslot
);
1139 btrfs_mark_buffer_dirty(parent
);
1140 if (btrfs_header_nritems(left
) > orig_slot
) {
1141 path
->nodes
[level
] = left
;
1142 path
->slots
[level
+ 1] -= 1;
1143 path
->slots
[level
] = orig_slot
;
1144 btrfs_tree_unlock(mid
);
1145 free_extent_buffer(mid
);
1148 btrfs_header_nritems(left
);
1149 path
->slots
[level
] = orig_slot
;
1150 btrfs_tree_unlock(left
);
1151 free_extent_buffer(left
);
1155 btrfs_tree_unlock(left
);
1156 free_extent_buffer(left
);
1158 right
= read_node_slot(root
, parent
, pslot
+ 1);
1161 * then try to empty the right most buffer into the middle
1166 btrfs_tree_lock(right
);
1167 btrfs_set_lock_blocking(right
);
1169 right_nr
= btrfs_header_nritems(right
);
1170 if (right_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
1173 ret
= btrfs_cow_block(trans
, root
, right
,
1179 wret
= balance_node_right(trans
, root
,
1186 struct btrfs_disk_key disk_key
;
1188 btrfs_node_key(right
, &disk_key
, 0);
1189 btrfs_set_node_key(parent
, &disk_key
, pslot
+ 1);
1190 btrfs_mark_buffer_dirty(parent
);
1192 if (btrfs_header_nritems(mid
) <= orig_slot
) {
1193 path
->nodes
[level
] = right
;
1194 path
->slots
[level
+ 1] += 1;
1195 path
->slots
[level
] = orig_slot
-
1196 btrfs_header_nritems(mid
);
1197 btrfs_tree_unlock(mid
);
1198 free_extent_buffer(mid
);
1200 btrfs_tree_unlock(right
);
1201 free_extent_buffer(right
);
1205 btrfs_tree_unlock(right
);
1206 free_extent_buffer(right
);
1212 * readahead one full node of leaves, finding things that are close
1213 * to the block in 'slot', and triggering ra on them.
1215 static void reada_for_search(struct btrfs_root
*root
,
1216 struct btrfs_path
*path
,
1217 int level
, int slot
, u64 objectid
)
1219 struct extent_buffer
*node
;
1220 struct btrfs_disk_key disk_key
;
1226 int direction
= path
->reada
;
1227 struct extent_buffer
*eb
;
1236 if (!path
->nodes
[level
])
1239 node
= path
->nodes
[level
];
1241 search
= btrfs_node_blockptr(node
, slot
);
1242 blocksize
= btrfs_level_size(root
, level
- 1);
1243 eb
= btrfs_find_tree_block(root
, search
, blocksize
);
1245 free_extent_buffer(eb
);
1251 nritems
= btrfs_header_nritems(node
);
1253 if (node
->map_token
|| path
->skip_locking
)
1257 if (map
&& !node
->map_token
) {
1258 unsigned long offset
= btrfs_node_key_ptr_offset(nr
);
1259 map_private_extent_buffer(node
, offset
,
1260 sizeof(struct btrfs_key_ptr
),
1264 &node
->map_len
, KM_USER1
);
1266 if (direction
< 0) {
1270 } else if (direction
> 0) {
1275 if (path
->reada
< 0 && objectid
) {
1276 btrfs_node_key(node
, &disk_key
, nr
);
1277 if (btrfs_disk_key_objectid(&disk_key
) != objectid
)
1280 search
= btrfs_node_blockptr(node
, nr
);
1281 if ((search
<= target
&& target
- search
<= 65536) ||
1282 (search
> target
&& search
- target
<= 65536)) {
1283 gen
= btrfs_node_ptr_generation(node
, nr
);
1284 if (map
&& node
->map_token
) {
1285 unmap_extent_buffer(node
, node
->map_token
,
1287 node
->map_token
= NULL
;
1289 readahead_tree_block(root
, search
, blocksize
, gen
);
1293 if ((nread
> 65536 || nscan
> 32))
1296 if (map
&& node
->map_token
) {
1297 unmap_extent_buffer(node
, node
->map_token
, KM_USER1
);
1298 node
->map_token
= NULL
;
1303 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1306 static noinline
int reada_for_balance(struct btrfs_root
*root
,
1307 struct btrfs_path
*path
, int level
)
1311 struct extent_buffer
*parent
;
1312 struct extent_buffer
*eb
;
1319 parent
= path
->nodes
[level
+ 1];
1323 nritems
= btrfs_header_nritems(parent
);
1324 slot
= path
->slots
[level
+ 1];
1325 blocksize
= btrfs_level_size(root
, level
);
1328 block1
= btrfs_node_blockptr(parent
, slot
- 1);
1329 gen
= btrfs_node_ptr_generation(parent
, slot
- 1);
1330 eb
= btrfs_find_tree_block(root
, block1
, blocksize
);
1331 if (eb
&& btrfs_buffer_uptodate(eb
, gen
))
1333 free_extent_buffer(eb
);
1335 if (slot
+ 1 < nritems
) {
1336 block2
= btrfs_node_blockptr(parent
, slot
+ 1);
1337 gen
= btrfs_node_ptr_generation(parent
, slot
+ 1);
1338 eb
= btrfs_find_tree_block(root
, block2
, blocksize
);
1339 if (eb
&& btrfs_buffer_uptodate(eb
, gen
))
1341 free_extent_buffer(eb
);
1343 if (block1
|| block2
) {
1346 /* release the whole path */
1347 btrfs_release_path(path
);
1349 /* read the blocks */
1351 readahead_tree_block(root
, block1
, blocksize
, 0);
1353 readahead_tree_block(root
, block2
, blocksize
, 0);
1356 eb
= read_tree_block(root
, block1
, blocksize
, 0);
1357 free_extent_buffer(eb
);
1360 eb
= read_tree_block(root
, block2
, blocksize
, 0);
1361 free_extent_buffer(eb
);
1369 * when we walk down the tree, it is usually safe to unlock the higher layers
1370 * in the tree. The exceptions are when our path goes through slot 0, because
1371 * operations on the tree might require changing key pointers higher up in the
1374 * callers might also have set path->keep_locks, which tells this code to keep
1375 * the lock if the path points to the last slot in the block. This is part of
1376 * walking through the tree, and selecting the next slot in the higher block.
1378 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1379 * if lowest_unlock is 1, level 0 won't be unlocked
1381 static noinline
void unlock_up(struct btrfs_path
*path
, int level
,
1385 int skip_level
= level
;
1387 struct extent_buffer
*t
;
1389 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1390 if (!path
->nodes
[i
])
1392 if (!path
->locks
[i
])
1394 if (!no_skips
&& path
->slots
[i
] == 0) {
1398 if (!no_skips
&& path
->keep_locks
) {
1401 nritems
= btrfs_header_nritems(t
);
1402 if (nritems
< 1 || path
->slots
[i
] >= nritems
- 1) {
1407 if (skip_level
< i
&& i
>= lowest_unlock
)
1411 if (i
>= lowest_unlock
&& i
> skip_level
&& path
->locks
[i
]) {
1412 btrfs_tree_unlock(t
);
1419 * This releases any locks held in the path starting at level and
1420 * going all the way up to the root.
1422 * btrfs_search_slot will keep the lock held on higher nodes in a few
1423 * corner cases, such as COW of the block at slot zero in the node. This
1424 * ignores those rules, and it should only be called when there are no
1425 * more updates to be done higher up in the tree.
1427 noinline
void btrfs_unlock_up_safe(struct btrfs_path
*path
, int level
)
1431 if (path
->keep_locks
)
1434 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1435 if (!path
->nodes
[i
])
1437 if (!path
->locks
[i
])
1439 btrfs_tree_unlock(path
->nodes
[i
]);
1445 * helper function for btrfs_search_slot. The goal is to find a block
1446 * in cache without setting the path to blocking. If we find the block
1447 * we return zero and the path is unchanged.
1449 * If we can't find the block, we set the path blocking and do some
1450 * reada. -EAGAIN is returned and the search must be repeated.
1453 read_block_for_search(struct btrfs_trans_handle
*trans
,
1454 struct btrfs_root
*root
, struct btrfs_path
*p
,
1455 struct extent_buffer
**eb_ret
, int level
, int slot
,
1456 struct btrfs_key
*key
)
1461 struct extent_buffer
*b
= *eb_ret
;
1462 struct extent_buffer
*tmp
;
1465 blocknr
= btrfs_node_blockptr(b
, slot
);
1466 gen
= btrfs_node_ptr_generation(b
, slot
);
1467 blocksize
= btrfs_level_size(root
, level
- 1);
1469 tmp
= btrfs_find_tree_block(root
, blocknr
, blocksize
);
1471 if (btrfs_buffer_uptodate(tmp
, 0)) {
1472 if (btrfs_buffer_uptodate(tmp
, gen
)) {
1474 * we found an up to date block without
1481 /* the pages were up to date, but we failed
1482 * the generation number check. Do a full
1483 * read for the generation number that is correct.
1484 * We must do this without dropping locks so
1485 * we can trust our generation number
1487 free_extent_buffer(tmp
);
1488 tmp
= read_tree_block(root
, blocknr
, blocksize
, gen
);
1489 if (tmp
&& btrfs_buffer_uptodate(tmp
, gen
)) {
1493 free_extent_buffer(tmp
);
1494 btrfs_release_path(p
);
1500 * reduce lock contention at high levels
1501 * of the btree by dropping locks before
1502 * we read. Don't release the lock on the current
1503 * level because we need to walk this node to figure
1504 * out which blocks to read.
1506 btrfs_unlock_up_safe(p
, level
+ 1);
1507 btrfs_set_path_blocking(p
);
1509 free_extent_buffer(tmp
);
1511 reada_for_search(root
, p
, level
, slot
, key
->objectid
);
1513 btrfs_release_path(p
);
1516 tmp
= read_tree_block(root
, blocknr
, blocksize
, 0);
1519 * If the read above didn't mark this buffer up to date,
1520 * it will never end up being up to date. Set ret to EIO now
1521 * and give up so that our caller doesn't loop forever
1524 if (!btrfs_buffer_uptodate(tmp
, 0))
1526 free_extent_buffer(tmp
);
1532 * helper function for btrfs_search_slot. This does all of the checks
1533 * for node-level blocks and does any balancing required based on
1536 * If no extra work was required, zero is returned. If we had to
1537 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1541 setup_nodes_for_search(struct btrfs_trans_handle
*trans
,
1542 struct btrfs_root
*root
, struct btrfs_path
*p
,
1543 struct extent_buffer
*b
, int level
, int ins_len
)
1546 if ((p
->search_for_split
|| ins_len
> 0) && btrfs_header_nritems(b
) >=
1547 BTRFS_NODEPTRS_PER_BLOCK(root
) - 3) {
1550 sret
= reada_for_balance(root
, p
, level
);
1554 btrfs_set_path_blocking(p
);
1555 sret
= split_node(trans
, root
, p
, level
);
1556 btrfs_clear_path_blocking(p
, NULL
);
1563 b
= p
->nodes
[level
];
1564 } else if (ins_len
< 0 && btrfs_header_nritems(b
) <
1565 BTRFS_NODEPTRS_PER_BLOCK(root
) / 2) {
1568 sret
= reada_for_balance(root
, p
, level
);
1572 btrfs_set_path_blocking(p
);
1573 sret
= balance_level(trans
, root
, p
, level
);
1574 btrfs_clear_path_blocking(p
, NULL
);
1580 b
= p
->nodes
[level
];
1582 btrfs_release_path(p
);
1585 BUG_ON(btrfs_header_nritems(b
) == 1);
1596 * look for key in the tree. path is filled in with nodes along the way
1597 * if key is found, we return zero and you can find the item in the leaf
1598 * level of the path (level 0)
1600 * If the key isn't found, the path points to the slot where it should
1601 * be inserted, and 1 is returned. If there are other errors during the
1602 * search a negative error number is returned.
1604 * if ins_len > 0, nodes and leaves will be split as we walk down the
1605 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1608 int btrfs_search_slot(struct btrfs_trans_handle
*trans
, struct btrfs_root
1609 *root
, struct btrfs_key
*key
, struct btrfs_path
*p
, int
1612 struct extent_buffer
*b
;
1617 int lowest_unlock
= 1;
1618 u8 lowest_level
= 0;
1620 lowest_level
= p
->lowest_level
;
1621 WARN_ON(lowest_level
&& ins_len
> 0);
1622 WARN_ON(p
->nodes
[0] != NULL
);
1628 if (p
->search_commit_root
) {
1629 b
= root
->commit_root
;
1630 extent_buffer_get(b
);
1631 if (!p
->skip_locking
)
1634 if (p
->skip_locking
)
1635 b
= btrfs_root_node(root
);
1637 b
= btrfs_lock_root_node(root
);
1641 level
= btrfs_header_level(b
);
1644 * setup the path here so we can release it under lock
1645 * contention with the cow code
1647 p
->nodes
[level
] = b
;
1648 if (!p
->skip_locking
)
1649 p
->locks
[level
] = 1;
1653 * if we don't really need to cow this block
1654 * then we don't want to set the path blocking,
1655 * so we test it here
1657 if (!should_cow_block(trans
, root
, b
))
1660 btrfs_set_path_blocking(p
);
1662 err
= btrfs_cow_block(trans
, root
, b
,
1663 p
->nodes
[level
+ 1],
1664 p
->slots
[level
+ 1], &b
);
1671 BUG_ON(!cow
&& ins_len
);
1673 p
->nodes
[level
] = b
;
1674 if (!p
->skip_locking
)
1675 p
->locks
[level
] = 1;
1677 btrfs_clear_path_blocking(p
, NULL
);
1680 * we have a lock on b and as long as we aren't changing
1681 * the tree, there is no way to for the items in b to change.
1682 * It is safe to drop the lock on our parent before we
1683 * go through the expensive btree search on b.
1685 * If cow is true, then we might be changing slot zero,
1686 * which may require changing the parent. So, we can't
1687 * drop the lock until after we know which slot we're
1691 btrfs_unlock_up_safe(p
, level
+ 1);
1693 ret
= bin_search(b
, key
, level
, &slot
);
1697 if (ret
&& slot
> 0) {
1701 p
->slots
[level
] = slot
;
1702 err
= setup_nodes_for_search(trans
, root
, p
, b
, level
,
1710 b
= p
->nodes
[level
];
1711 slot
= p
->slots
[level
];
1713 unlock_up(p
, level
, lowest_unlock
);
1715 if (level
== lowest_level
) {
1721 err
= read_block_for_search(trans
, root
, p
,
1722 &b
, level
, slot
, key
);
1730 if (!p
->skip_locking
) {
1731 btrfs_clear_path_blocking(p
, NULL
);
1732 err
= btrfs_try_spin_lock(b
);
1735 btrfs_set_path_blocking(p
);
1737 btrfs_clear_path_blocking(p
, b
);
1741 p
->slots
[level
] = slot
;
1743 btrfs_leaf_free_space(root
, b
) < ins_len
) {
1744 btrfs_set_path_blocking(p
);
1745 err
= split_leaf(trans
, root
, key
,
1746 p
, ins_len
, ret
== 0);
1747 btrfs_clear_path_blocking(p
, NULL
);
1755 if (!p
->search_for_split
)
1756 unlock_up(p
, level
, lowest_unlock
);
1763 * we don't really know what they plan on doing with the path
1764 * from here on, so for now just mark it as blocking
1766 if (!p
->leave_spinning
)
1767 btrfs_set_path_blocking(p
);
1769 btrfs_release_path(p
);
1774 * adjust the pointers going up the tree, starting at level
1775 * making sure the right key of each node is points to 'key'.
1776 * This is used after shifting pointers to the left, so it stops
1777 * fixing up pointers when a given leaf/node is not in slot 0 of the
1780 * If this fails to write a tree block, it returns -1, but continues
1781 * fixing up the blocks in ram so the tree is consistent.
1783 static int fixup_low_keys(struct btrfs_trans_handle
*trans
,
1784 struct btrfs_root
*root
, struct btrfs_path
*path
,
1785 struct btrfs_disk_key
*key
, int level
)
1789 struct extent_buffer
*t
;
1791 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1792 int tslot
= path
->slots
[i
];
1793 if (!path
->nodes
[i
])
1796 btrfs_set_node_key(t
, key
, tslot
);
1797 btrfs_mark_buffer_dirty(path
->nodes
[i
]);
1807 * This function isn't completely safe. It's the caller's responsibility
1808 * that the new key won't break the order
1810 int btrfs_set_item_key_safe(struct btrfs_trans_handle
*trans
,
1811 struct btrfs_root
*root
, struct btrfs_path
*path
,
1812 struct btrfs_key
*new_key
)
1814 struct btrfs_disk_key disk_key
;
1815 struct extent_buffer
*eb
;
1818 eb
= path
->nodes
[0];
1819 slot
= path
->slots
[0];
1821 btrfs_item_key(eb
, &disk_key
, slot
- 1);
1822 if (comp_keys(&disk_key
, new_key
) >= 0)
1825 if (slot
< btrfs_header_nritems(eb
) - 1) {
1826 btrfs_item_key(eb
, &disk_key
, slot
+ 1);
1827 if (comp_keys(&disk_key
, new_key
) <= 0)
1831 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
1832 btrfs_set_item_key(eb
, &disk_key
, slot
);
1833 btrfs_mark_buffer_dirty(eb
);
1835 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
1840 * try to push data from one node into the next node left in the
1843 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1844 * error, and > 0 if there was no room in the left hand block.
1846 static int push_node_left(struct btrfs_trans_handle
*trans
,
1847 struct btrfs_root
*root
, struct extent_buffer
*dst
,
1848 struct extent_buffer
*src
, int empty
)
1855 src_nritems
= btrfs_header_nritems(src
);
1856 dst_nritems
= btrfs_header_nritems(dst
);
1857 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1858 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1859 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1861 if (!empty
&& src_nritems
<= 8)
1864 if (push_items
<= 0)
1868 push_items
= min(src_nritems
, push_items
);
1869 if (push_items
< src_nritems
) {
1870 /* leave at least 8 pointers in the node if
1871 * we aren't going to empty it
1873 if (src_nritems
- push_items
< 8) {
1874 if (push_items
<= 8)
1880 push_items
= min(src_nritems
- 8, push_items
);
1882 copy_extent_buffer(dst
, src
,
1883 btrfs_node_key_ptr_offset(dst_nritems
),
1884 btrfs_node_key_ptr_offset(0),
1885 push_items
* sizeof(struct btrfs_key_ptr
));
1887 if (push_items
< src_nritems
) {
1888 memmove_extent_buffer(src
, btrfs_node_key_ptr_offset(0),
1889 btrfs_node_key_ptr_offset(push_items
),
1890 (src_nritems
- push_items
) *
1891 sizeof(struct btrfs_key_ptr
));
1893 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1894 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1895 btrfs_mark_buffer_dirty(src
);
1896 btrfs_mark_buffer_dirty(dst
);
1902 * try to push data from one node into the next node right in the
1905 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1906 * error, and > 0 if there was no room in the right hand block.
1908 * this will only push up to 1/2 the contents of the left node over
1910 static int balance_node_right(struct btrfs_trans_handle
*trans
,
1911 struct btrfs_root
*root
,
1912 struct extent_buffer
*dst
,
1913 struct extent_buffer
*src
)
1921 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1922 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1924 src_nritems
= btrfs_header_nritems(src
);
1925 dst_nritems
= btrfs_header_nritems(dst
);
1926 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1927 if (push_items
<= 0)
1930 if (src_nritems
< 4)
1933 max_push
= src_nritems
/ 2 + 1;
1934 /* don't try to empty the node */
1935 if (max_push
>= src_nritems
)
1938 if (max_push
< push_items
)
1939 push_items
= max_push
;
1941 memmove_extent_buffer(dst
, btrfs_node_key_ptr_offset(push_items
),
1942 btrfs_node_key_ptr_offset(0),
1944 sizeof(struct btrfs_key_ptr
));
1946 copy_extent_buffer(dst
, src
,
1947 btrfs_node_key_ptr_offset(0),
1948 btrfs_node_key_ptr_offset(src_nritems
- push_items
),
1949 push_items
* sizeof(struct btrfs_key_ptr
));
1951 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1952 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1954 btrfs_mark_buffer_dirty(src
);
1955 btrfs_mark_buffer_dirty(dst
);
1961 * helper function to insert a new root level in the tree.
1962 * A new node is allocated, and a single item is inserted to
1963 * point to the existing root
1965 * returns zero on success or < 0 on failure.
1967 static noinline
int insert_new_root(struct btrfs_trans_handle
*trans
,
1968 struct btrfs_root
*root
,
1969 struct btrfs_path
*path
, int level
)
1972 struct extent_buffer
*lower
;
1973 struct extent_buffer
*c
;
1974 struct extent_buffer
*old
;
1975 struct btrfs_disk_key lower_key
;
1977 BUG_ON(path
->nodes
[level
]);
1978 BUG_ON(path
->nodes
[level
-1] != root
->node
);
1980 lower
= path
->nodes
[level
-1];
1982 btrfs_item_key(lower
, &lower_key
, 0);
1984 btrfs_node_key(lower
, &lower_key
, 0);
1986 c
= btrfs_alloc_free_block(trans
, root
, root
->nodesize
, 0,
1987 root
->root_key
.objectid
, &lower_key
,
1988 level
, root
->node
->start
, 0);
1992 root_add_used(root
, root
->nodesize
);
1994 memset_extent_buffer(c
, 0, 0, sizeof(struct btrfs_header
));
1995 btrfs_set_header_nritems(c
, 1);
1996 btrfs_set_header_level(c
, level
);
1997 btrfs_set_header_bytenr(c
, c
->start
);
1998 btrfs_set_header_generation(c
, trans
->transid
);
1999 btrfs_set_header_backref_rev(c
, BTRFS_MIXED_BACKREF_REV
);
2000 btrfs_set_header_owner(c
, root
->root_key
.objectid
);
2002 write_extent_buffer(c
, root
->fs_info
->fsid
,
2003 (unsigned long)btrfs_header_fsid(c
),
2006 write_extent_buffer(c
, root
->fs_info
->chunk_tree_uuid
,
2007 (unsigned long)btrfs_header_chunk_tree_uuid(c
),
2010 btrfs_set_node_key(c
, &lower_key
, 0);
2011 btrfs_set_node_blockptr(c
, 0, lower
->start
);
2012 lower_gen
= btrfs_header_generation(lower
);
2013 WARN_ON(lower_gen
!= trans
->transid
);
2015 btrfs_set_node_ptr_generation(c
, 0, lower_gen
);
2017 btrfs_mark_buffer_dirty(c
);
2020 rcu_assign_pointer(root
->node
, c
);
2022 /* the super has an extra ref to root->node */
2023 free_extent_buffer(old
);
2025 add_root_to_dirty_list(root
);
2026 extent_buffer_get(c
);
2027 path
->nodes
[level
] = c
;
2028 path
->locks
[level
] = 1;
2029 path
->slots
[level
] = 0;
2034 * worker function to insert a single pointer in a node.
2035 * the node should have enough room for the pointer already
2037 * slot and level indicate where you want the key to go, and
2038 * blocknr is the block the key points to.
2040 * returns zero on success and < 0 on any error
2042 static int insert_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
2043 *root
, struct btrfs_path
*path
, struct btrfs_disk_key
2044 *key
, u64 bytenr
, int slot
, int level
)
2046 struct extent_buffer
*lower
;
2049 BUG_ON(!path
->nodes
[level
]);
2050 btrfs_assert_tree_locked(path
->nodes
[level
]);
2051 lower
= path
->nodes
[level
];
2052 nritems
= btrfs_header_nritems(lower
);
2053 BUG_ON(slot
> nritems
);
2054 if (nritems
== BTRFS_NODEPTRS_PER_BLOCK(root
))
2056 if (slot
!= nritems
) {
2057 memmove_extent_buffer(lower
,
2058 btrfs_node_key_ptr_offset(slot
+ 1),
2059 btrfs_node_key_ptr_offset(slot
),
2060 (nritems
- slot
) * sizeof(struct btrfs_key_ptr
));
2062 btrfs_set_node_key(lower
, key
, slot
);
2063 btrfs_set_node_blockptr(lower
, slot
, bytenr
);
2064 WARN_ON(trans
->transid
== 0);
2065 btrfs_set_node_ptr_generation(lower
, slot
, trans
->transid
);
2066 btrfs_set_header_nritems(lower
, nritems
+ 1);
2067 btrfs_mark_buffer_dirty(lower
);
2072 * split the node at the specified level in path in two.
2073 * The path is corrected to point to the appropriate node after the split
2075 * Before splitting this tries to make some room in the node by pushing
2076 * left and right, if either one works, it returns right away.
2078 * returns 0 on success and < 0 on failure
2080 static noinline
int split_node(struct btrfs_trans_handle
*trans
,
2081 struct btrfs_root
*root
,
2082 struct btrfs_path
*path
, int level
)
2084 struct extent_buffer
*c
;
2085 struct extent_buffer
*split
;
2086 struct btrfs_disk_key disk_key
;
2092 c
= path
->nodes
[level
];
2093 WARN_ON(btrfs_header_generation(c
) != trans
->transid
);
2094 if (c
== root
->node
) {
2095 /* trying to split the root, lets make a new one */
2096 ret
= insert_new_root(trans
, root
, path
, level
+ 1);
2100 ret
= push_nodes_for_insert(trans
, root
, path
, level
);
2101 c
= path
->nodes
[level
];
2102 if (!ret
&& btrfs_header_nritems(c
) <
2103 BTRFS_NODEPTRS_PER_BLOCK(root
) - 3)
2109 c_nritems
= btrfs_header_nritems(c
);
2110 mid
= (c_nritems
+ 1) / 2;
2111 btrfs_node_key(c
, &disk_key
, mid
);
2113 split
= btrfs_alloc_free_block(trans
, root
, root
->nodesize
, 0,
2114 root
->root_key
.objectid
,
2115 &disk_key
, level
, c
->start
, 0);
2117 return PTR_ERR(split
);
2119 root_add_used(root
, root
->nodesize
);
2121 memset_extent_buffer(split
, 0, 0, sizeof(struct btrfs_header
));
2122 btrfs_set_header_level(split
, btrfs_header_level(c
));
2123 btrfs_set_header_bytenr(split
, split
->start
);
2124 btrfs_set_header_generation(split
, trans
->transid
);
2125 btrfs_set_header_backref_rev(split
, BTRFS_MIXED_BACKREF_REV
);
2126 btrfs_set_header_owner(split
, root
->root_key
.objectid
);
2127 write_extent_buffer(split
, root
->fs_info
->fsid
,
2128 (unsigned long)btrfs_header_fsid(split
),
2130 write_extent_buffer(split
, root
->fs_info
->chunk_tree_uuid
,
2131 (unsigned long)btrfs_header_chunk_tree_uuid(split
),
2135 copy_extent_buffer(split
, c
,
2136 btrfs_node_key_ptr_offset(0),
2137 btrfs_node_key_ptr_offset(mid
),
2138 (c_nritems
- mid
) * sizeof(struct btrfs_key_ptr
));
2139 btrfs_set_header_nritems(split
, c_nritems
- mid
);
2140 btrfs_set_header_nritems(c
, mid
);
2143 btrfs_mark_buffer_dirty(c
);
2144 btrfs_mark_buffer_dirty(split
);
2146 wret
= insert_ptr(trans
, root
, path
, &disk_key
, split
->start
,
2147 path
->slots
[level
+ 1] + 1,
2152 if (path
->slots
[level
] >= mid
) {
2153 path
->slots
[level
] -= mid
;
2154 btrfs_tree_unlock(c
);
2155 free_extent_buffer(c
);
2156 path
->nodes
[level
] = split
;
2157 path
->slots
[level
+ 1] += 1;
2159 btrfs_tree_unlock(split
);
2160 free_extent_buffer(split
);
2166 * how many bytes are required to store the items in a leaf. start
2167 * and nr indicate which items in the leaf to check. This totals up the
2168 * space used both by the item structs and the item data
2170 static int leaf_space_used(struct extent_buffer
*l
, int start
, int nr
)
2173 int nritems
= btrfs_header_nritems(l
);
2174 int end
= min(nritems
, start
+ nr
) - 1;
2178 data_len
= btrfs_item_end_nr(l
, start
);
2179 data_len
= data_len
- btrfs_item_offset_nr(l
, end
);
2180 data_len
+= sizeof(struct btrfs_item
) * nr
;
2181 WARN_ON(data_len
< 0);
2186 * The space between the end of the leaf items and
2187 * the start of the leaf data. IOW, how much room
2188 * the leaf has left for both items and data
2190 noinline
int btrfs_leaf_free_space(struct btrfs_root
*root
,
2191 struct extent_buffer
*leaf
)
2193 int nritems
= btrfs_header_nritems(leaf
);
2195 ret
= BTRFS_LEAF_DATA_SIZE(root
) - leaf_space_used(leaf
, 0, nritems
);
2197 printk(KERN_CRIT
"leaf free space ret %d, leaf data size %lu, "
2198 "used %d nritems %d\n",
2199 ret
, (unsigned long) BTRFS_LEAF_DATA_SIZE(root
),
2200 leaf_space_used(leaf
, 0, nritems
), nritems
);
2206 * min slot controls the lowest index we're willing to push to the
2207 * right. We'll push up to and including min_slot, but no lower
2209 static noinline
int __push_leaf_right(struct btrfs_trans_handle
*trans
,
2210 struct btrfs_root
*root
,
2211 struct btrfs_path
*path
,
2212 int data_size
, int empty
,
2213 struct extent_buffer
*right
,
2214 int free_space
, u32 left_nritems
,
2217 struct extent_buffer
*left
= path
->nodes
[0];
2218 struct extent_buffer
*upper
= path
->nodes
[1];
2219 struct btrfs_disk_key disk_key
;
2224 struct btrfs_item
*item
;
2233 nr
= max_t(u32
, 1, min_slot
);
2235 if (path
->slots
[0] >= left_nritems
)
2236 push_space
+= data_size
;
2238 slot
= path
->slots
[1];
2239 i
= left_nritems
- 1;
2241 item
= btrfs_item_nr(left
, i
);
2243 if (!empty
&& push_items
> 0) {
2244 if (path
->slots
[0] > i
)
2246 if (path
->slots
[0] == i
) {
2247 int space
= btrfs_leaf_free_space(root
, left
);
2248 if (space
+ push_space
* 2 > free_space
)
2253 if (path
->slots
[0] == i
)
2254 push_space
+= data_size
;
2256 if (!left
->map_token
) {
2257 map_extent_buffer(left
, (unsigned long)item
,
2258 sizeof(struct btrfs_item
),
2259 &left
->map_token
, &left
->kaddr
,
2260 &left
->map_start
, &left
->map_len
,
2264 this_item_size
= btrfs_item_size(left
, item
);
2265 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
2269 push_space
+= this_item_size
+ sizeof(*item
);
2274 if (left
->map_token
) {
2275 unmap_extent_buffer(left
, left
->map_token
, KM_USER1
);
2276 left
->map_token
= NULL
;
2279 if (push_items
== 0)
2282 if (!empty
&& push_items
== left_nritems
)
2285 /* push left to right */
2286 right_nritems
= btrfs_header_nritems(right
);
2288 push_space
= btrfs_item_end_nr(left
, left_nritems
- push_items
);
2289 push_space
-= leaf_data_end(root
, left
);
2291 /* make room in the right data area */
2292 data_end
= leaf_data_end(root
, right
);
2293 memmove_extent_buffer(right
,
2294 btrfs_leaf_data(right
) + data_end
- push_space
,
2295 btrfs_leaf_data(right
) + data_end
,
2296 BTRFS_LEAF_DATA_SIZE(root
) - data_end
);
2298 /* copy from the left data area */
2299 copy_extent_buffer(right
, left
, btrfs_leaf_data(right
) +
2300 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
2301 btrfs_leaf_data(left
) + leaf_data_end(root
, left
),
2304 memmove_extent_buffer(right
, btrfs_item_nr_offset(push_items
),
2305 btrfs_item_nr_offset(0),
2306 right_nritems
* sizeof(struct btrfs_item
));
2308 /* copy the items from left to right */
2309 copy_extent_buffer(right
, left
, btrfs_item_nr_offset(0),
2310 btrfs_item_nr_offset(left_nritems
- push_items
),
2311 push_items
* sizeof(struct btrfs_item
));
2313 /* update the item pointers */
2314 right_nritems
+= push_items
;
2315 btrfs_set_header_nritems(right
, right_nritems
);
2316 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
2317 for (i
= 0; i
< right_nritems
; i
++) {
2318 item
= btrfs_item_nr(right
, i
);
2319 if (!right
->map_token
) {
2320 map_extent_buffer(right
, (unsigned long)item
,
2321 sizeof(struct btrfs_item
),
2322 &right
->map_token
, &right
->kaddr
,
2323 &right
->map_start
, &right
->map_len
,
2326 push_space
-= btrfs_item_size(right
, item
);
2327 btrfs_set_item_offset(right
, item
, push_space
);
2330 if (right
->map_token
) {
2331 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
2332 right
->map_token
= NULL
;
2334 left_nritems
-= push_items
;
2335 btrfs_set_header_nritems(left
, left_nritems
);
2338 btrfs_mark_buffer_dirty(left
);
2340 clean_tree_block(trans
, root
, left
);
2342 btrfs_mark_buffer_dirty(right
);
2344 btrfs_item_key(right
, &disk_key
, 0);
2345 btrfs_set_node_key(upper
, &disk_key
, slot
+ 1);
2346 btrfs_mark_buffer_dirty(upper
);
2348 /* then fixup the leaf pointer in the path */
2349 if (path
->slots
[0] >= left_nritems
) {
2350 path
->slots
[0] -= left_nritems
;
2351 if (btrfs_header_nritems(path
->nodes
[0]) == 0)
2352 clean_tree_block(trans
, root
, path
->nodes
[0]);
2353 btrfs_tree_unlock(path
->nodes
[0]);
2354 free_extent_buffer(path
->nodes
[0]);
2355 path
->nodes
[0] = right
;
2356 path
->slots
[1] += 1;
2358 btrfs_tree_unlock(right
);
2359 free_extent_buffer(right
);
2364 btrfs_tree_unlock(right
);
2365 free_extent_buffer(right
);
2370 * push some data in the path leaf to the right, trying to free up at
2371 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2373 * returns 1 if the push failed because the other node didn't have enough
2374 * room, 0 if everything worked out and < 0 if there were major errors.
2376 * this will push starting from min_slot to the end of the leaf. It won't
2377 * push any slot lower than min_slot
2379 static int push_leaf_right(struct btrfs_trans_handle
*trans
, struct btrfs_root
2380 *root
, struct btrfs_path
*path
,
2381 int min_data_size
, int data_size
,
2382 int empty
, u32 min_slot
)
2384 struct extent_buffer
*left
= path
->nodes
[0];
2385 struct extent_buffer
*right
;
2386 struct extent_buffer
*upper
;
2392 if (!path
->nodes
[1])
2395 slot
= path
->slots
[1];
2396 upper
= path
->nodes
[1];
2397 if (slot
>= btrfs_header_nritems(upper
) - 1)
2400 btrfs_assert_tree_locked(path
->nodes
[1]);
2402 right
= read_node_slot(root
, upper
, slot
+ 1);
2406 btrfs_tree_lock(right
);
2407 btrfs_set_lock_blocking(right
);
2409 free_space
= btrfs_leaf_free_space(root
, right
);
2410 if (free_space
< data_size
)
2413 /* cow and double check */
2414 ret
= btrfs_cow_block(trans
, root
, right
, upper
,
2419 free_space
= btrfs_leaf_free_space(root
, right
);
2420 if (free_space
< data_size
)
2423 left_nritems
= btrfs_header_nritems(left
);
2424 if (left_nritems
== 0)
2427 return __push_leaf_right(trans
, root
, path
, min_data_size
, empty
,
2428 right
, free_space
, left_nritems
, min_slot
);
2430 btrfs_tree_unlock(right
);
2431 free_extent_buffer(right
);
2436 * push some data in the path leaf to the left, trying to free up at
2437 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2439 * max_slot can put a limit on how far into the leaf we'll push items. The
2440 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2443 static noinline
int __push_leaf_left(struct btrfs_trans_handle
*trans
,
2444 struct btrfs_root
*root
,
2445 struct btrfs_path
*path
, int data_size
,
2446 int empty
, struct extent_buffer
*left
,
2447 int free_space
, u32 right_nritems
,
2450 struct btrfs_disk_key disk_key
;
2451 struct extent_buffer
*right
= path
->nodes
[0];
2455 struct btrfs_item
*item
;
2456 u32 old_left_nritems
;
2461 u32 old_left_item_size
;
2464 nr
= min(right_nritems
, max_slot
);
2466 nr
= min(right_nritems
- 1, max_slot
);
2468 for (i
= 0; i
< nr
; i
++) {
2469 item
= btrfs_item_nr(right
, i
);
2470 if (!right
->map_token
) {
2471 map_extent_buffer(right
, (unsigned long)item
,
2472 sizeof(struct btrfs_item
),
2473 &right
->map_token
, &right
->kaddr
,
2474 &right
->map_start
, &right
->map_len
,
2478 if (!empty
&& push_items
> 0) {
2479 if (path
->slots
[0] < i
)
2481 if (path
->slots
[0] == i
) {
2482 int space
= btrfs_leaf_free_space(root
, right
);
2483 if (space
+ push_space
* 2 > free_space
)
2488 if (path
->slots
[0] == i
)
2489 push_space
+= data_size
;
2491 this_item_size
= btrfs_item_size(right
, item
);
2492 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
2496 push_space
+= this_item_size
+ sizeof(*item
);
2499 if (right
->map_token
) {
2500 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
2501 right
->map_token
= NULL
;
2504 if (push_items
== 0) {
2508 if (!empty
&& push_items
== btrfs_header_nritems(right
))
2511 /* push data from right to left */
2512 copy_extent_buffer(left
, right
,
2513 btrfs_item_nr_offset(btrfs_header_nritems(left
)),
2514 btrfs_item_nr_offset(0),
2515 push_items
* sizeof(struct btrfs_item
));
2517 push_space
= BTRFS_LEAF_DATA_SIZE(root
) -
2518 btrfs_item_offset_nr(right
, push_items
- 1);
2520 copy_extent_buffer(left
, right
, btrfs_leaf_data(left
) +
2521 leaf_data_end(root
, left
) - push_space
,
2522 btrfs_leaf_data(right
) +
2523 btrfs_item_offset_nr(right
, push_items
- 1),
2525 old_left_nritems
= btrfs_header_nritems(left
);
2526 BUG_ON(old_left_nritems
<= 0);
2528 old_left_item_size
= btrfs_item_offset_nr(left
, old_left_nritems
- 1);
2529 for (i
= old_left_nritems
; i
< old_left_nritems
+ push_items
; i
++) {
2532 item
= btrfs_item_nr(left
, i
);
2533 if (!left
->map_token
) {
2534 map_extent_buffer(left
, (unsigned long)item
,
2535 sizeof(struct btrfs_item
),
2536 &left
->map_token
, &left
->kaddr
,
2537 &left
->map_start
, &left
->map_len
,
2541 ioff
= btrfs_item_offset(left
, item
);
2542 btrfs_set_item_offset(left
, item
,
2543 ioff
- (BTRFS_LEAF_DATA_SIZE(root
) - old_left_item_size
));
2545 btrfs_set_header_nritems(left
, old_left_nritems
+ push_items
);
2546 if (left
->map_token
) {
2547 unmap_extent_buffer(left
, left
->map_token
, KM_USER1
);
2548 left
->map_token
= NULL
;
2551 /* fixup right node */
2552 if (push_items
> right_nritems
) {
2553 printk(KERN_CRIT
"push items %d nr %u\n", push_items
,
2558 if (push_items
< right_nritems
) {
2559 push_space
= btrfs_item_offset_nr(right
, push_items
- 1) -
2560 leaf_data_end(root
, right
);
2561 memmove_extent_buffer(right
, btrfs_leaf_data(right
) +
2562 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
2563 btrfs_leaf_data(right
) +
2564 leaf_data_end(root
, right
), push_space
);
2566 memmove_extent_buffer(right
, btrfs_item_nr_offset(0),
2567 btrfs_item_nr_offset(push_items
),
2568 (btrfs_header_nritems(right
) - push_items
) *
2569 sizeof(struct btrfs_item
));
2571 right_nritems
-= push_items
;
2572 btrfs_set_header_nritems(right
, right_nritems
);
2573 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
2574 for (i
= 0; i
< right_nritems
; i
++) {
2575 item
= btrfs_item_nr(right
, i
);
2577 if (!right
->map_token
) {
2578 map_extent_buffer(right
, (unsigned long)item
,
2579 sizeof(struct btrfs_item
),
2580 &right
->map_token
, &right
->kaddr
,
2581 &right
->map_start
, &right
->map_len
,
2585 push_space
= push_space
- btrfs_item_size(right
, item
);
2586 btrfs_set_item_offset(right
, item
, push_space
);
2588 if (right
->map_token
) {
2589 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
2590 right
->map_token
= NULL
;
2593 btrfs_mark_buffer_dirty(left
);
2595 btrfs_mark_buffer_dirty(right
);
2597 clean_tree_block(trans
, root
, right
);
2599 btrfs_item_key(right
, &disk_key
, 0);
2600 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
2604 /* then fixup the leaf pointer in the path */
2605 if (path
->slots
[0] < push_items
) {
2606 path
->slots
[0] += old_left_nritems
;
2607 btrfs_tree_unlock(path
->nodes
[0]);
2608 free_extent_buffer(path
->nodes
[0]);
2609 path
->nodes
[0] = left
;
2610 path
->slots
[1] -= 1;
2612 btrfs_tree_unlock(left
);
2613 free_extent_buffer(left
);
2614 path
->slots
[0] -= push_items
;
2616 BUG_ON(path
->slots
[0] < 0);
2619 btrfs_tree_unlock(left
);
2620 free_extent_buffer(left
);
2625 * push some data in the path leaf to the left, trying to free up at
2626 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2628 * max_slot can put a limit on how far into the leaf we'll push items. The
2629 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2632 static int push_leaf_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
2633 *root
, struct btrfs_path
*path
, int min_data_size
,
2634 int data_size
, int empty
, u32 max_slot
)
2636 struct extent_buffer
*right
= path
->nodes
[0];
2637 struct extent_buffer
*left
;
2643 slot
= path
->slots
[1];
2646 if (!path
->nodes
[1])
2649 right_nritems
= btrfs_header_nritems(right
);
2650 if (right_nritems
== 0)
2653 btrfs_assert_tree_locked(path
->nodes
[1]);
2655 left
= read_node_slot(root
, path
->nodes
[1], slot
- 1);
2659 btrfs_tree_lock(left
);
2660 btrfs_set_lock_blocking(left
);
2662 free_space
= btrfs_leaf_free_space(root
, left
);
2663 if (free_space
< data_size
) {
2668 /* cow and double check */
2669 ret
= btrfs_cow_block(trans
, root
, left
,
2670 path
->nodes
[1], slot
- 1, &left
);
2672 /* we hit -ENOSPC, but it isn't fatal here */
2677 free_space
= btrfs_leaf_free_space(root
, left
);
2678 if (free_space
< data_size
) {
2683 return __push_leaf_left(trans
, root
, path
, min_data_size
,
2684 empty
, left
, free_space
, right_nritems
,
2687 btrfs_tree_unlock(left
);
2688 free_extent_buffer(left
);
2693 * split the path's leaf in two, making sure there is at least data_size
2694 * available for the resulting leaf level of the path.
2696 * returns 0 if all went well and < 0 on failure.
2698 static noinline
int copy_for_split(struct btrfs_trans_handle
*trans
,
2699 struct btrfs_root
*root
,
2700 struct btrfs_path
*path
,
2701 struct extent_buffer
*l
,
2702 struct extent_buffer
*right
,
2703 int slot
, int mid
, int nritems
)
2710 struct btrfs_disk_key disk_key
;
2712 nritems
= nritems
- mid
;
2713 btrfs_set_header_nritems(right
, nritems
);
2714 data_copy_size
= btrfs_item_end_nr(l
, mid
) - leaf_data_end(root
, l
);
2716 copy_extent_buffer(right
, l
, btrfs_item_nr_offset(0),
2717 btrfs_item_nr_offset(mid
),
2718 nritems
* sizeof(struct btrfs_item
));
2720 copy_extent_buffer(right
, l
,
2721 btrfs_leaf_data(right
) + BTRFS_LEAF_DATA_SIZE(root
) -
2722 data_copy_size
, btrfs_leaf_data(l
) +
2723 leaf_data_end(root
, l
), data_copy_size
);
2725 rt_data_off
= BTRFS_LEAF_DATA_SIZE(root
) -
2726 btrfs_item_end_nr(l
, mid
);
2728 for (i
= 0; i
< nritems
; i
++) {
2729 struct btrfs_item
*item
= btrfs_item_nr(right
, i
);
2732 if (!right
->map_token
) {
2733 map_extent_buffer(right
, (unsigned long)item
,
2734 sizeof(struct btrfs_item
),
2735 &right
->map_token
, &right
->kaddr
,
2736 &right
->map_start
, &right
->map_len
,
2740 ioff
= btrfs_item_offset(right
, item
);
2741 btrfs_set_item_offset(right
, item
, ioff
+ rt_data_off
);
2744 if (right
->map_token
) {
2745 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
2746 right
->map_token
= NULL
;
2749 btrfs_set_header_nritems(l
, mid
);
2751 btrfs_item_key(right
, &disk_key
, 0);
2752 wret
= insert_ptr(trans
, root
, path
, &disk_key
, right
->start
,
2753 path
->slots
[1] + 1, 1);
2757 btrfs_mark_buffer_dirty(right
);
2758 btrfs_mark_buffer_dirty(l
);
2759 BUG_ON(path
->slots
[0] != slot
);
2762 btrfs_tree_unlock(path
->nodes
[0]);
2763 free_extent_buffer(path
->nodes
[0]);
2764 path
->nodes
[0] = right
;
2765 path
->slots
[0] -= mid
;
2766 path
->slots
[1] += 1;
2768 btrfs_tree_unlock(right
);
2769 free_extent_buffer(right
);
2772 BUG_ON(path
->slots
[0] < 0);
2778 * double splits happen when we need to insert a big item in the middle
2779 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2780 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2783 * We avoid this by trying to push the items on either side of our target
2784 * into the adjacent leaves. If all goes well we can avoid the double split
2787 static noinline
int push_for_double_split(struct btrfs_trans_handle
*trans
,
2788 struct btrfs_root
*root
,
2789 struct btrfs_path
*path
,
2797 slot
= path
->slots
[0];
2800 * try to push all the items after our slot into the
2803 ret
= push_leaf_right(trans
, root
, path
, 1, data_size
, 0, slot
);
2810 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2812 * our goal is to get our slot at the start or end of a leaf. If
2813 * we've done so we're done
2815 if (path
->slots
[0] == 0 || path
->slots
[0] == nritems
)
2818 if (btrfs_leaf_free_space(root
, path
->nodes
[0]) >= data_size
)
2821 /* try to push all the items before our slot into the next leaf */
2822 slot
= path
->slots
[0];
2823 ret
= push_leaf_left(trans
, root
, path
, 1, data_size
, 0, slot
);
2836 * split the path's leaf in two, making sure there is at least data_size
2837 * available for the resulting leaf level of the path.
2839 * returns 0 if all went well and < 0 on failure.
2841 static noinline
int split_leaf(struct btrfs_trans_handle
*trans
,
2842 struct btrfs_root
*root
,
2843 struct btrfs_key
*ins_key
,
2844 struct btrfs_path
*path
, int data_size
,
2847 struct btrfs_disk_key disk_key
;
2848 struct extent_buffer
*l
;
2852 struct extent_buffer
*right
;
2856 int num_doubles
= 0;
2857 int tried_avoid_double
= 0;
2860 slot
= path
->slots
[0];
2861 if (extend
&& data_size
+ btrfs_item_size_nr(l
, slot
) +
2862 sizeof(struct btrfs_item
) > BTRFS_LEAF_DATA_SIZE(root
))
2865 /* first try to make some room by pushing left and right */
2867 wret
= push_leaf_right(trans
, root
, path
, data_size
,
2872 wret
= push_leaf_left(trans
, root
, path
, data_size
,
2873 data_size
, 0, (u32
)-1);
2879 /* did the pushes work? */
2880 if (btrfs_leaf_free_space(root
, l
) >= data_size
)
2884 if (!path
->nodes
[1]) {
2885 ret
= insert_new_root(trans
, root
, path
, 1);
2892 slot
= path
->slots
[0];
2893 nritems
= btrfs_header_nritems(l
);
2894 mid
= (nritems
+ 1) / 2;
2898 leaf_space_used(l
, mid
, nritems
- mid
) + data_size
>
2899 BTRFS_LEAF_DATA_SIZE(root
)) {
2900 if (slot
>= nritems
) {
2904 if (mid
!= nritems
&&
2905 leaf_space_used(l
, mid
, nritems
- mid
) +
2906 data_size
> BTRFS_LEAF_DATA_SIZE(root
)) {
2907 if (data_size
&& !tried_avoid_double
)
2908 goto push_for_double
;
2914 if (leaf_space_used(l
, 0, mid
) + data_size
>
2915 BTRFS_LEAF_DATA_SIZE(root
)) {
2916 if (!extend
&& data_size
&& slot
== 0) {
2918 } else if ((extend
|| !data_size
) && slot
== 0) {
2922 if (mid
!= nritems
&&
2923 leaf_space_used(l
, mid
, nritems
- mid
) +
2924 data_size
> BTRFS_LEAF_DATA_SIZE(root
)) {
2925 if (data_size
&& !tried_avoid_double
)
2926 goto push_for_double
;
2934 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
2936 btrfs_item_key(l
, &disk_key
, mid
);
2938 right
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
, 0,
2939 root
->root_key
.objectid
,
2940 &disk_key
, 0, l
->start
, 0);
2942 return PTR_ERR(right
);
2944 root_add_used(root
, root
->leafsize
);
2946 memset_extent_buffer(right
, 0, 0, sizeof(struct btrfs_header
));
2947 btrfs_set_header_bytenr(right
, right
->start
);
2948 btrfs_set_header_generation(right
, trans
->transid
);
2949 btrfs_set_header_backref_rev(right
, BTRFS_MIXED_BACKREF_REV
);
2950 btrfs_set_header_owner(right
, root
->root_key
.objectid
);
2951 btrfs_set_header_level(right
, 0);
2952 write_extent_buffer(right
, root
->fs_info
->fsid
,
2953 (unsigned long)btrfs_header_fsid(right
),
2956 write_extent_buffer(right
, root
->fs_info
->chunk_tree_uuid
,
2957 (unsigned long)btrfs_header_chunk_tree_uuid(right
),
2962 btrfs_set_header_nritems(right
, 0);
2963 wret
= insert_ptr(trans
, root
, path
,
2964 &disk_key
, right
->start
,
2965 path
->slots
[1] + 1, 1);
2969 btrfs_tree_unlock(path
->nodes
[0]);
2970 free_extent_buffer(path
->nodes
[0]);
2971 path
->nodes
[0] = right
;
2973 path
->slots
[1] += 1;
2975 btrfs_set_header_nritems(right
, 0);
2976 wret
= insert_ptr(trans
, root
, path
,
2982 btrfs_tree_unlock(path
->nodes
[0]);
2983 free_extent_buffer(path
->nodes
[0]);
2984 path
->nodes
[0] = right
;
2986 if (path
->slots
[1] == 0) {
2987 wret
= fixup_low_keys(trans
, root
,
2988 path
, &disk_key
, 1);
2993 btrfs_mark_buffer_dirty(right
);
2997 ret
= copy_for_split(trans
, root
, path
, l
, right
, slot
, mid
, nritems
);
3001 BUG_ON(num_doubles
!= 0);
3009 push_for_double_split(trans
, root
, path
, data_size
);
3010 tried_avoid_double
= 1;
3011 if (btrfs_leaf_free_space(root
, path
->nodes
[0]) >= data_size
)
3016 static noinline
int setup_leaf_for_split(struct btrfs_trans_handle
*trans
,
3017 struct btrfs_root
*root
,
3018 struct btrfs_path
*path
, int ins_len
)
3020 struct btrfs_key key
;
3021 struct extent_buffer
*leaf
;
3022 struct btrfs_file_extent_item
*fi
;
3027 leaf
= path
->nodes
[0];
3028 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3030 BUG_ON(key
.type
!= BTRFS_EXTENT_DATA_KEY
&&
3031 key
.type
!= BTRFS_EXTENT_CSUM_KEY
);
3033 if (btrfs_leaf_free_space(root
, leaf
) >= ins_len
)
3036 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
3037 if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
3038 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3039 struct btrfs_file_extent_item
);
3040 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
3042 btrfs_release_path(path
);
3044 path
->keep_locks
= 1;
3045 path
->search_for_split
= 1;
3046 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
3047 path
->search_for_split
= 0;
3052 leaf
= path
->nodes
[0];
3053 /* if our item isn't there or got smaller, return now */
3054 if (ret
> 0 || item_size
!= btrfs_item_size_nr(leaf
, path
->slots
[0]))
3057 /* the leaf has changed, it now has room. return now */
3058 if (btrfs_leaf_free_space(root
, path
->nodes
[0]) >= ins_len
)
3061 if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
3062 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3063 struct btrfs_file_extent_item
);
3064 if (extent_len
!= btrfs_file_extent_num_bytes(leaf
, fi
))
3068 btrfs_set_path_blocking(path
);
3069 ret
= split_leaf(trans
, root
, &key
, path
, ins_len
, 1);
3073 path
->keep_locks
= 0;
3074 btrfs_unlock_up_safe(path
, 1);
3077 path
->keep_locks
= 0;
3081 static noinline
int split_item(struct btrfs_trans_handle
*trans
,
3082 struct btrfs_root
*root
,
3083 struct btrfs_path
*path
,
3084 struct btrfs_key
*new_key
,
3085 unsigned long split_offset
)
3087 struct extent_buffer
*leaf
;
3088 struct btrfs_item
*item
;
3089 struct btrfs_item
*new_item
;
3095 struct btrfs_disk_key disk_key
;
3097 leaf
= path
->nodes
[0];
3098 BUG_ON(btrfs_leaf_free_space(root
, leaf
) < sizeof(struct btrfs_item
));
3100 btrfs_set_path_blocking(path
);
3102 item
= btrfs_item_nr(leaf
, path
->slots
[0]);
3103 orig_offset
= btrfs_item_offset(leaf
, item
);
3104 item_size
= btrfs_item_size(leaf
, item
);
3106 buf
= kmalloc(item_size
, GFP_NOFS
);
3110 read_extent_buffer(leaf
, buf
, btrfs_item_ptr_offset(leaf
,
3111 path
->slots
[0]), item_size
);
3113 slot
= path
->slots
[0] + 1;
3114 nritems
= btrfs_header_nritems(leaf
);
3115 if (slot
!= nritems
) {
3116 /* shift the items */
3117 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ 1),
3118 btrfs_item_nr_offset(slot
),
3119 (nritems
- slot
) * sizeof(struct btrfs_item
));
3122 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
3123 btrfs_set_item_key(leaf
, &disk_key
, slot
);
3125 new_item
= btrfs_item_nr(leaf
, slot
);
3127 btrfs_set_item_offset(leaf
, new_item
, orig_offset
);
3128 btrfs_set_item_size(leaf
, new_item
, item_size
- split_offset
);
3130 btrfs_set_item_offset(leaf
, item
,
3131 orig_offset
+ item_size
- split_offset
);
3132 btrfs_set_item_size(leaf
, item
, split_offset
);
3134 btrfs_set_header_nritems(leaf
, nritems
+ 1);
3136 /* write the data for the start of the original item */
3137 write_extent_buffer(leaf
, buf
,
3138 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
3141 /* write the data for the new item */
3142 write_extent_buffer(leaf
, buf
+ split_offset
,
3143 btrfs_item_ptr_offset(leaf
, slot
),
3144 item_size
- split_offset
);
3145 btrfs_mark_buffer_dirty(leaf
);
3147 BUG_ON(btrfs_leaf_free_space(root
, leaf
) < 0);
3153 * This function splits a single item into two items,
3154 * giving 'new_key' to the new item and splitting the
3155 * old one at split_offset (from the start of the item).
3157 * The path may be released by this operation. After
3158 * the split, the path is pointing to the old item. The
3159 * new item is going to be in the same node as the old one.
3161 * Note, the item being split must be smaller enough to live alone on
3162 * a tree block with room for one extra struct btrfs_item
3164 * This allows us to split the item in place, keeping a lock on the
3165 * leaf the entire time.
3167 int btrfs_split_item(struct btrfs_trans_handle
*trans
,
3168 struct btrfs_root
*root
,
3169 struct btrfs_path
*path
,
3170 struct btrfs_key
*new_key
,
3171 unsigned long split_offset
)
3174 ret
= setup_leaf_for_split(trans
, root
, path
,
3175 sizeof(struct btrfs_item
));
3179 ret
= split_item(trans
, root
, path
, new_key
, split_offset
);
3184 * This function duplicate a item, giving 'new_key' to the new item.
3185 * It guarantees both items live in the same tree leaf and the new item
3186 * is contiguous with the original item.
3188 * This allows us to split file extent in place, keeping a lock on the
3189 * leaf the entire time.
3191 int btrfs_duplicate_item(struct btrfs_trans_handle
*trans
,
3192 struct btrfs_root
*root
,
3193 struct btrfs_path
*path
,
3194 struct btrfs_key
*new_key
)
3196 struct extent_buffer
*leaf
;
3200 leaf
= path
->nodes
[0];
3201 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
3202 ret
= setup_leaf_for_split(trans
, root
, path
,
3203 item_size
+ sizeof(struct btrfs_item
));
3208 ret
= setup_items_for_insert(trans
, root
, path
, new_key
, &item_size
,
3209 item_size
, item_size
+
3210 sizeof(struct btrfs_item
), 1);
3213 leaf
= path
->nodes
[0];
3214 memcpy_extent_buffer(leaf
,
3215 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
3216 btrfs_item_ptr_offset(leaf
, path
->slots
[0] - 1),
3222 * make the item pointed to by the path smaller. new_size indicates
3223 * how small to make it, and from_end tells us if we just chop bytes
3224 * off the end of the item or if we shift the item to chop bytes off
3227 int btrfs_truncate_item(struct btrfs_trans_handle
*trans
,
3228 struct btrfs_root
*root
,
3229 struct btrfs_path
*path
,
3230 u32 new_size
, int from_end
)
3233 struct extent_buffer
*leaf
;
3234 struct btrfs_item
*item
;
3236 unsigned int data_end
;
3237 unsigned int old_data_start
;
3238 unsigned int old_size
;
3239 unsigned int size_diff
;
3242 leaf
= path
->nodes
[0];
3243 slot
= path
->slots
[0];
3245 old_size
= btrfs_item_size_nr(leaf
, slot
);
3246 if (old_size
== new_size
)
3249 nritems
= btrfs_header_nritems(leaf
);
3250 data_end
= leaf_data_end(root
, leaf
);
3252 old_data_start
= btrfs_item_offset_nr(leaf
, slot
);
3254 size_diff
= old_size
- new_size
;
3257 BUG_ON(slot
>= nritems
);
3260 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3262 /* first correct the data pointers */
3263 for (i
= slot
; i
< nritems
; i
++) {
3265 item
= btrfs_item_nr(leaf
, i
);
3267 if (!leaf
->map_token
) {
3268 map_extent_buffer(leaf
, (unsigned long)item
,
3269 sizeof(struct btrfs_item
),
3270 &leaf
->map_token
, &leaf
->kaddr
,
3271 &leaf
->map_start
, &leaf
->map_len
,
3275 ioff
= btrfs_item_offset(leaf
, item
);
3276 btrfs_set_item_offset(leaf
, item
, ioff
+ size_diff
);
3279 if (leaf
->map_token
) {
3280 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3281 leaf
->map_token
= NULL
;
3284 /* shift the data */
3286 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3287 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
3288 data_end
, old_data_start
+ new_size
- data_end
);
3290 struct btrfs_disk_key disk_key
;
3293 btrfs_item_key(leaf
, &disk_key
, slot
);
3295 if (btrfs_disk_key_type(&disk_key
) == BTRFS_EXTENT_DATA_KEY
) {
3297 struct btrfs_file_extent_item
*fi
;
3299 fi
= btrfs_item_ptr(leaf
, slot
,
3300 struct btrfs_file_extent_item
);
3301 fi
= (struct btrfs_file_extent_item
*)(
3302 (unsigned long)fi
- size_diff
);
3304 if (btrfs_file_extent_type(leaf
, fi
) ==
3305 BTRFS_FILE_EXTENT_INLINE
) {
3306 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
3307 memmove_extent_buffer(leaf
, ptr
,
3309 offsetof(struct btrfs_file_extent_item
,
3314 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3315 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
3316 data_end
, old_data_start
- data_end
);
3318 offset
= btrfs_disk_key_offset(&disk_key
);
3319 btrfs_set_disk_key_offset(&disk_key
, offset
+ size_diff
);
3320 btrfs_set_item_key(leaf
, &disk_key
, slot
);
3322 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
3325 item
= btrfs_item_nr(leaf
, slot
);
3326 btrfs_set_item_size(leaf
, item
, new_size
);
3327 btrfs_mark_buffer_dirty(leaf
);
3329 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3330 btrfs_print_leaf(root
, leaf
);
3337 * make the item pointed to by the path bigger, data_size is the new size.
3339 int btrfs_extend_item(struct btrfs_trans_handle
*trans
,
3340 struct btrfs_root
*root
, struct btrfs_path
*path
,
3344 struct extent_buffer
*leaf
;
3345 struct btrfs_item
*item
;
3347 unsigned int data_end
;
3348 unsigned int old_data
;
3349 unsigned int old_size
;
3352 leaf
= path
->nodes
[0];
3354 nritems
= btrfs_header_nritems(leaf
);
3355 data_end
= leaf_data_end(root
, leaf
);
3357 if (btrfs_leaf_free_space(root
, leaf
) < data_size
) {
3358 btrfs_print_leaf(root
, leaf
);
3361 slot
= path
->slots
[0];
3362 old_data
= btrfs_item_end_nr(leaf
, slot
);
3365 if (slot
>= nritems
) {
3366 btrfs_print_leaf(root
, leaf
);
3367 printk(KERN_CRIT
"slot %d too large, nritems %d\n",
3373 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3375 /* first correct the data pointers */
3376 for (i
= slot
; i
< nritems
; i
++) {
3378 item
= btrfs_item_nr(leaf
, i
);
3380 if (!leaf
->map_token
) {
3381 map_extent_buffer(leaf
, (unsigned long)item
,
3382 sizeof(struct btrfs_item
),
3383 &leaf
->map_token
, &leaf
->kaddr
,
3384 &leaf
->map_start
, &leaf
->map_len
,
3387 ioff
= btrfs_item_offset(leaf
, item
);
3388 btrfs_set_item_offset(leaf
, item
, ioff
- data_size
);
3391 if (leaf
->map_token
) {
3392 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3393 leaf
->map_token
= NULL
;
3396 /* shift the data */
3397 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3398 data_end
- data_size
, btrfs_leaf_data(leaf
) +
3399 data_end
, old_data
- data_end
);
3401 data_end
= old_data
;
3402 old_size
= btrfs_item_size_nr(leaf
, slot
);
3403 item
= btrfs_item_nr(leaf
, slot
);
3404 btrfs_set_item_size(leaf
, item
, old_size
+ data_size
);
3405 btrfs_mark_buffer_dirty(leaf
);
3407 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3408 btrfs_print_leaf(root
, leaf
);
3415 * Given a key and some data, insert items into the tree.
3416 * This does all the path init required, making room in the tree if needed.
3417 * Returns the number of keys that were inserted.
3419 int btrfs_insert_some_items(struct btrfs_trans_handle
*trans
,
3420 struct btrfs_root
*root
,
3421 struct btrfs_path
*path
,
3422 struct btrfs_key
*cpu_key
, u32
*data_size
,
3425 struct extent_buffer
*leaf
;
3426 struct btrfs_item
*item
;
3433 unsigned int data_end
;
3434 struct btrfs_disk_key disk_key
;
3435 struct btrfs_key found_key
;
3437 for (i
= 0; i
< nr
; i
++) {
3438 if (total_size
+ data_size
[i
] + sizeof(struct btrfs_item
) >
3439 BTRFS_LEAF_DATA_SIZE(root
)) {
3443 total_data
+= data_size
[i
];
3444 total_size
+= data_size
[i
] + sizeof(struct btrfs_item
);
3448 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
3454 leaf
= path
->nodes
[0];
3456 nritems
= btrfs_header_nritems(leaf
);
3457 data_end
= leaf_data_end(root
, leaf
);
3459 if (btrfs_leaf_free_space(root
, leaf
) < total_size
) {
3460 for (i
= nr
; i
>= 0; i
--) {
3461 total_data
-= data_size
[i
];
3462 total_size
-= data_size
[i
] + sizeof(struct btrfs_item
);
3463 if (total_size
< btrfs_leaf_free_space(root
, leaf
))
3469 slot
= path
->slots
[0];
3472 if (slot
!= nritems
) {
3473 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
3475 item
= btrfs_item_nr(leaf
, slot
);
3476 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3478 /* figure out how many keys we can insert in here */
3479 total_data
= data_size
[0];
3480 for (i
= 1; i
< nr
; i
++) {
3481 if (btrfs_comp_cpu_keys(&found_key
, cpu_key
+ i
) <= 0)
3483 total_data
+= data_size
[i
];
3487 if (old_data
< data_end
) {
3488 btrfs_print_leaf(root
, leaf
);
3489 printk(KERN_CRIT
"slot %d old_data %d data_end %d\n",
3490 slot
, old_data
, data_end
);
3494 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3496 /* first correct the data pointers */
3497 WARN_ON(leaf
->map_token
);
3498 for (i
= slot
; i
< nritems
; i
++) {
3501 item
= btrfs_item_nr(leaf
, i
);
3502 if (!leaf
->map_token
) {
3503 map_extent_buffer(leaf
, (unsigned long)item
,
3504 sizeof(struct btrfs_item
),
3505 &leaf
->map_token
, &leaf
->kaddr
,
3506 &leaf
->map_start
, &leaf
->map_len
,
3510 ioff
= btrfs_item_offset(leaf
, item
);
3511 btrfs_set_item_offset(leaf
, item
, ioff
- total_data
);
3513 if (leaf
->map_token
) {
3514 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3515 leaf
->map_token
= NULL
;
3518 /* shift the items */
3519 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
3520 btrfs_item_nr_offset(slot
),
3521 (nritems
- slot
) * sizeof(struct btrfs_item
));
3523 /* shift the data */
3524 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3525 data_end
- total_data
, btrfs_leaf_data(leaf
) +
3526 data_end
, old_data
- data_end
);
3527 data_end
= old_data
;
3530 * this sucks but it has to be done, if we are inserting at
3531 * the end of the leaf only insert 1 of the items, since we
3532 * have no way of knowing whats on the next leaf and we'd have
3533 * to drop our current locks to figure it out
3538 /* setup the item for the new data */
3539 for (i
= 0; i
< nr
; i
++) {
3540 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
3541 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
3542 item
= btrfs_item_nr(leaf
, slot
+ i
);
3543 btrfs_set_item_offset(leaf
, item
, data_end
- data_size
[i
]);
3544 data_end
-= data_size
[i
];
3545 btrfs_set_item_size(leaf
, item
, data_size
[i
]);
3547 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
3548 btrfs_mark_buffer_dirty(leaf
);
3552 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
3553 ret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
3556 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3557 btrfs_print_leaf(root
, leaf
);
3567 * this is a helper for btrfs_insert_empty_items, the main goal here is
3568 * to save stack depth by doing the bulk of the work in a function
3569 * that doesn't call btrfs_search_slot
3571 int setup_items_for_insert(struct btrfs_trans_handle
*trans
,
3572 struct btrfs_root
*root
, struct btrfs_path
*path
,
3573 struct btrfs_key
*cpu_key
, u32
*data_size
,
3574 u32 total_data
, u32 total_size
, int nr
)
3576 struct btrfs_item
*item
;
3579 unsigned int data_end
;
3580 struct btrfs_disk_key disk_key
;
3582 struct extent_buffer
*leaf
;
3585 leaf
= path
->nodes
[0];
3586 slot
= path
->slots
[0];
3588 nritems
= btrfs_header_nritems(leaf
);
3589 data_end
= leaf_data_end(root
, leaf
);
3591 if (btrfs_leaf_free_space(root
, leaf
) < total_size
) {
3592 btrfs_print_leaf(root
, leaf
);
3593 printk(KERN_CRIT
"not enough freespace need %u have %d\n",
3594 total_size
, btrfs_leaf_free_space(root
, leaf
));
3598 if (slot
!= nritems
) {
3599 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
3601 if (old_data
< data_end
) {
3602 btrfs_print_leaf(root
, leaf
);
3603 printk(KERN_CRIT
"slot %d old_data %d data_end %d\n",
3604 slot
, old_data
, data_end
);
3608 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3610 /* first correct the data pointers */
3611 WARN_ON(leaf
->map_token
);
3612 for (i
= slot
; i
< nritems
; i
++) {
3615 item
= btrfs_item_nr(leaf
, i
);
3616 if (!leaf
->map_token
) {
3617 map_extent_buffer(leaf
, (unsigned long)item
,
3618 sizeof(struct btrfs_item
),
3619 &leaf
->map_token
, &leaf
->kaddr
,
3620 &leaf
->map_start
, &leaf
->map_len
,
3624 ioff
= btrfs_item_offset(leaf
, item
);
3625 btrfs_set_item_offset(leaf
, item
, ioff
- total_data
);
3627 if (leaf
->map_token
) {
3628 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3629 leaf
->map_token
= NULL
;
3632 /* shift the items */
3633 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
3634 btrfs_item_nr_offset(slot
),
3635 (nritems
- slot
) * sizeof(struct btrfs_item
));
3637 /* shift the data */
3638 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3639 data_end
- total_data
, btrfs_leaf_data(leaf
) +
3640 data_end
, old_data
- data_end
);
3641 data_end
= old_data
;
3644 /* setup the item for the new data */
3645 for (i
= 0; i
< nr
; i
++) {
3646 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
3647 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
3648 item
= btrfs_item_nr(leaf
, slot
+ i
);
3649 btrfs_set_item_offset(leaf
, item
, data_end
- data_size
[i
]);
3650 data_end
-= data_size
[i
];
3651 btrfs_set_item_size(leaf
, item
, data_size
[i
]);
3654 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
3658 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
3659 ret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
3661 btrfs_unlock_up_safe(path
, 1);
3662 btrfs_mark_buffer_dirty(leaf
);
3664 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3665 btrfs_print_leaf(root
, leaf
);
3672 * Given a key and some data, insert items into the tree.
3673 * This does all the path init required, making room in the tree if needed.
3675 int btrfs_insert_empty_items(struct btrfs_trans_handle
*trans
,
3676 struct btrfs_root
*root
,
3677 struct btrfs_path
*path
,
3678 struct btrfs_key
*cpu_key
, u32
*data_size
,
3687 for (i
= 0; i
< nr
; i
++)
3688 total_data
+= data_size
[i
];
3690 total_size
= total_data
+ (nr
* sizeof(struct btrfs_item
));
3691 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
3697 slot
= path
->slots
[0];
3700 ret
= setup_items_for_insert(trans
, root
, path
, cpu_key
, data_size
,
3701 total_data
, total_size
, nr
);
3708 * Given a key and some data, insert an item into the tree.
3709 * This does all the path init required, making room in the tree if needed.
3711 int btrfs_insert_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
3712 *root
, struct btrfs_key
*cpu_key
, void *data
, u32
3716 struct btrfs_path
*path
;
3717 struct extent_buffer
*leaf
;
3720 path
= btrfs_alloc_path();
3723 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
3725 leaf
= path
->nodes
[0];
3726 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
3727 write_extent_buffer(leaf
, data
, ptr
, data_size
);
3728 btrfs_mark_buffer_dirty(leaf
);
3730 btrfs_free_path(path
);
3735 * delete the pointer from a given node.
3737 * the tree should have been previously balanced so the deletion does not
3740 static int del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
3741 struct btrfs_path
*path
, int level
, int slot
)
3743 struct extent_buffer
*parent
= path
->nodes
[level
];
3748 nritems
= btrfs_header_nritems(parent
);
3749 if (slot
!= nritems
- 1) {
3750 memmove_extent_buffer(parent
,
3751 btrfs_node_key_ptr_offset(slot
),
3752 btrfs_node_key_ptr_offset(slot
+ 1),
3753 sizeof(struct btrfs_key_ptr
) *
3754 (nritems
- slot
- 1));
3757 btrfs_set_header_nritems(parent
, nritems
);
3758 if (nritems
== 0 && parent
== root
->node
) {
3759 BUG_ON(btrfs_header_level(root
->node
) != 1);
3760 /* just turn the root into a leaf and break */
3761 btrfs_set_header_level(root
->node
, 0);
3762 } else if (slot
== 0) {
3763 struct btrfs_disk_key disk_key
;
3765 btrfs_node_key(parent
, &disk_key
, 0);
3766 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, level
+ 1);
3770 btrfs_mark_buffer_dirty(parent
);
3775 * a helper function to delete the leaf pointed to by path->slots[1] and
3778 * This deletes the pointer in path->nodes[1] and frees the leaf
3779 * block extent. zero is returned if it all worked out, < 0 otherwise.
3781 * The path must have already been setup for deleting the leaf, including
3782 * all the proper balancing. path->nodes[1] must be locked.
3784 static noinline
int btrfs_del_leaf(struct btrfs_trans_handle
*trans
,
3785 struct btrfs_root
*root
,
3786 struct btrfs_path
*path
,
3787 struct extent_buffer
*leaf
)
3791 WARN_ON(btrfs_header_generation(leaf
) != trans
->transid
);
3792 ret
= del_ptr(trans
, root
, path
, 1, path
->slots
[1]);
3797 * btrfs_free_extent is expensive, we want to make sure we
3798 * aren't holding any locks when we call it
3800 btrfs_unlock_up_safe(path
, 0);
3802 root_sub_used(root
, leaf
->len
);
3804 btrfs_free_tree_block(trans
, root
, leaf
, 0, 1);
3808 * delete the item at the leaf level in path. If that empties
3809 * the leaf, remove it from the tree
3811 int btrfs_del_items(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
3812 struct btrfs_path
*path
, int slot
, int nr
)
3814 struct extent_buffer
*leaf
;
3815 struct btrfs_item
*item
;
3823 leaf
= path
->nodes
[0];
3824 last_off
= btrfs_item_offset_nr(leaf
, slot
+ nr
- 1);
3826 for (i
= 0; i
< nr
; i
++)
3827 dsize
+= btrfs_item_size_nr(leaf
, slot
+ i
);
3829 nritems
= btrfs_header_nritems(leaf
);
3831 if (slot
+ nr
!= nritems
) {
3832 int data_end
= leaf_data_end(root
, leaf
);
3834 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3836 btrfs_leaf_data(leaf
) + data_end
,
3837 last_off
- data_end
);
3839 for (i
= slot
+ nr
; i
< nritems
; i
++) {
3842 item
= btrfs_item_nr(leaf
, i
);
3843 if (!leaf
->map_token
) {
3844 map_extent_buffer(leaf
, (unsigned long)item
,
3845 sizeof(struct btrfs_item
),
3846 &leaf
->map_token
, &leaf
->kaddr
,
3847 &leaf
->map_start
, &leaf
->map_len
,
3850 ioff
= btrfs_item_offset(leaf
, item
);
3851 btrfs_set_item_offset(leaf
, item
, ioff
+ dsize
);
3854 if (leaf
->map_token
) {
3855 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3856 leaf
->map_token
= NULL
;
3859 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
),
3860 btrfs_item_nr_offset(slot
+ nr
),
3861 sizeof(struct btrfs_item
) *
3862 (nritems
- slot
- nr
));
3864 btrfs_set_header_nritems(leaf
, nritems
- nr
);
3867 /* delete the leaf if we've emptied it */
3869 if (leaf
== root
->node
) {
3870 btrfs_set_header_level(leaf
, 0);
3872 btrfs_set_path_blocking(path
);
3873 clean_tree_block(trans
, root
, leaf
);
3874 ret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
3878 int used
= leaf_space_used(leaf
, 0, nritems
);
3880 struct btrfs_disk_key disk_key
;
3882 btrfs_item_key(leaf
, &disk_key
, 0);
3883 wret
= fixup_low_keys(trans
, root
, path
,
3889 /* delete the leaf if it is mostly empty */
3890 if (used
< BTRFS_LEAF_DATA_SIZE(root
) / 3) {
3891 /* push_leaf_left fixes the path.
3892 * make sure the path still points to our leaf
3893 * for possible call to del_ptr below
3895 slot
= path
->slots
[1];
3896 extent_buffer_get(leaf
);
3898 btrfs_set_path_blocking(path
);
3899 wret
= push_leaf_left(trans
, root
, path
, 1, 1,
3901 if (wret
< 0 && wret
!= -ENOSPC
)
3904 if (path
->nodes
[0] == leaf
&&
3905 btrfs_header_nritems(leaf
)) {
3906 wret
= push_leaf_right(trans
, root
, path
, 1,
3908 if (wret
< 0 && wret
!= -ENOSPC
)
3912 if (btrfs_header_nritems(leaf
) == 0) {
3913 path
->slots
[1] = slot
;
3914 ret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
3916 free_extent_buffer(leaf
);
3918 /* if we're still in the path, make sure
3919 * we're dirty. Otherwise, one of the
3920 * push_leaf functions must have already
3921 * dirtied this buffer
3923 if (path
->nodes
[0] == leaf
)
3924 btrfs_mark_buffer_dirty(leaf
);
3925 free_extent_buffer(leaf
);
3928 btrfs_mark_buffer_dirty(leaf
);
3935 * search the tree again to find a leaf with lesser keys
3936 * returns 0 if it found something or 1 if there are no lesser leaves.
3937 * returns < 0 on io errors.
3939 * This may release the path, and so you may lose any locks held at the
3942 int btrfs_prev_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
3944 struct btrfs_key key
;
3945 struct btrfs_disk_key found_key
;
3948 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, 0);
3952 else if (key
.type
> 0)
3954 else if (key
.objectid
> 0)
3959 btrfs_release_path(path
);
3960 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3963 btrfs_item_key(path
->nodes
[0], &found_key
, 0);
3964 ret
= comp_keys(&found_key
, &key
);
3971 * A helper function to walk down the tree starting at min_key, and looking
3972 * for nodes or leaves that are either in cache or have a minimum
3973 * transaction id. This is used by the btree defrag code, and tree logging
3975 * This does not cow, but it does stuff the starting key it finds back
3976 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3977 * key and get a writable path.
3979 * This does lock as it descends, and path->keep_locks should be set
3980 * to 1 by the caller.
3982 * This honors path->lowest_level to prevent descent past a given level
3985 * min_trans indicates the oldest transaction that you are interested
3986 * in walking through. Any nodes or leaves older than min_trans are
3987 * skipped over (without reading them).
3989 * returns zero if something useful was found, < 0 on error and 1 if there
3990 * was nothing in the tree that matched the search criteria.
3992 int btrfs_search_forward(struct btrfs_root
*root
, struct btrfs_key
*min_key
,
3993 struct btrfs_key
*max_key
,
3994 struct btrfs_path
*path
, int cache_only
,
3997 struct extent_buffer
*cur
;
3998 struct btrfs_key found_key
;
4005 WARN_ON(!path
->keep_locks
);
4007 cur
= btrfs_lock_root_node(root
);
4008 level
= btrfs_header_level(cur
);
4009 WARN_ON(path
->nodes
[level
]);
4010 path
->nodes
[level
] = cur
;
4011 path
->locks
[level
] = 1;
4013 if (btrfs_header_generation(cur
) < min_trans
) {
4018 nritems
= btrfs_header_nritems(cur
);
4019 level
= btrfs_header_level(cur
);
4020 sret
= bin_search(cur
, min_key
, level
, &slot
);
4022 /* at the lowest level, we're done, setup the path and exit */
4023 if (level
== path
->lowest_level
) {
4024 if (slot
>= nritems
)
4027 path
->slots
[level
] = slot
;
4028 btrfs_item_key_to_cpu(cur
, &found_key
, slot
);
4031 if (sret
&& slot
> 0)
4034 * check this node pointer against the cache_only and
4035 * min_trans parameters. If it isn't in cache or is too
4036 * old, skip to the next one.
4038 while (slot
< nritems
) {
4041 struct extent_buffer
*tmp
;
4042 struct btrfs_disk_key disk_key
;
4044 blockptr
= btrfs_node_blockptr(cur
, slot
);
4045 gen
= btrfs_node_ptr_generation(cur
, slot
);
4046 if (gen
< min_trans
) {
4054 btrfs_node_key(cur
, &disk_key
, slot
);
4055 if (comp_keys(&disk_key
, max_key
) >= 0) {
4061 tmp
= btrfs_find_tree_block(root
, blockptr
,
4062 btrfs_level_size(root
, level
- 1));
4064 if (tmp
&& btrfs_buffer_uptodate(tmp
, gen
)) {
4065 free_extent_buffer(tmp
);
4069 free_extent_buffer(tmp
);
4074 * we didn't find a candidate key in this node, walk forward
4075 * and find another one
4077 if (slot
>= nritems
) {
4078 path
->slots
[level
] = slot
;
4079 btrfs_set_path_blocking(path
);
4080 sret
= btrfs_find_next_key(root
, path
, min_key
, level
,
4081 cache_only
, min_trans
);
4083 btrfs_release_path(path
);
4089 /* save our key for returning back */
4090 btrfs_node_key_to_cpu(cur
, &found_key
, slot
);
4091 path
->slots
[level
] = slot
;
4092 if (level
== path
->lowest_level
) {
4094 unlock_up(path
, level
, 1);
4097 btrfs_set_path_blocking(path
);
4098 cur
= read_node_slot(root
, cur
, slot
);
4101 btrfs_tree_lock(cur
);
4103 path
->locks
[level
- 1] = 1;
4104 path
->nodes
[level
- 1] = cur
;
4105 unlock_up(path
, level
, 1);
4106 btrfs_clear_path_blocking(path
, NULL
);
4110 memcpy(min_key
, &found_key
, sizeof(found_key
));
4111 btrfs_set_path_blocking(path
);
4116 * this is similar to btrfs_next_leaf, but does not try to preserve
4117 * and fixup the path. It looks for and returns the next key in the
4118 * tree based on the current path and the cache_only and min_trans
4121 * 0 is returned if another key is found, < 0 if there are any errors
4122 * and 1 is returned if there are no higher keys in the tree
4124 * path->keep_locks should be set to 1 on the search made before
4125 * calling this function.
4127 int btrfs_find_next_key(struct btrfs_root
*root
, struct btrfs_path
*path
,
4128 struct btrfs_key
*key
, int level
,
4129 int cache_only
, u64 min_trans
)
4132 struct extent_buffer
*c
;
4134 WARN_ON(!path
->keep_locks
);
4135 while (level
< BTRFS_MAX_LEVEL
) {
4136 if (!path
->nodes
[level
])
4139 slot
= path
->slots
[level
] + 1;
4140 c
= path
->nodes
[level
];
4142 if (slot
>= btrfs_header_nritems(c
)) {
4145 struct btrfs_key cur_key
;
4146 if (level
+ 1 >= BTRFS_MAX_LEVEL
||
4147 !path
->nodes
[level
+ 1])
4150 if (path
->locks
[level
+ 1]) {
4155 slot
= btrfs_header_nritems(c
) - 1;
4157 btrfs_item_key_to_cpu(c
, &cur_key
, slot
);
4159 btrfs_node_key_to_cpu(c
, &cur_key
, slot
);
4161 orig_lowest
= path
->lowest_level
;
4162 btrfs_release_path(path
);
4163 path
->lowest_level
= level
;
4164 ret
= btrfs_search_slot(NULL
, root
, &cur_key
, path
,
4166 path
->lowest_level
= orig_lowest
;
4170 c
= path
->nodes
[level
];
4171 slot
= path
->slots
[level
];
4178 btrfs_item_key_to_cpu(c
, key
, slot
);
4180 u64 blockptr
= btrfs_node_blockptr(c
, slot
);
4181 u64 gen
= btrfs_node_ptr_generation(c
, slot
);
4184 struct extent_buffer
*cur
;
4185 cur
= btrfs_find_tree_block(root
, blockptr
,
4186 btrfs_level_size(root
, level
- 1));
4187 if (!cur
|| !btrfs_buffer_uptodate(cur
, gen
)) {
4190 free_extent_buffer(cur
);
4193 free_extent_buffer(cur
);
4195 if (gen
< min_trans
) {
4199 btrfs_node_key_to_cpu(c
, key
, slot
);
4207 * search the tree again to find a leaf with greater keys
4208 * returns 0 if it found something or 1 if there are no greater leaves.
4209 * returns < 0 on io errors.
4211 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
4215 struct extent_buffer
*c
;
4216 struct extent_buffer
*next
;
4217 struct btrfs_key key
;
4220 int old_spinning
= path
->leave_spinning
;
4221 int force_blocking
= 0;
4223 nritems
= btrfs_header_nritems(path
->nodes
[0]);
4228 * we take the blocks in an order that upsets lockdep. Using
4229 * blocking mode is the only way around it.
4231 #ifdef CONFIG_DEBUG_LOCK_ALLOC
4235 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, nritems
- 1);
4239 btrfs_release_path(path
);
4241 path
->keep_locks
= 1;
4243 if (!force_blocking
)
4244 path
->leave_spinning
= 1;
4246 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4247 path
->keep_locks
= 0;
4252 nritems
= btrfs_header_nritems(path
->nodes
[0]);
4254 * by releasing the path above we dropped all our locks. A balance
4255 * could have added more items next to the key that used to be
4256 * at the very end of the block. So, check again here and
4257 * advance the path if there are now more items available.
4259 if (nritems
> 0 && path
->slots
[0] < nritems
- 1) {
4266 while (level
< BTRFS_MAX_LEVEL
) {
4267 if (!path
->nodes
[level
]) {
4272 slot
= path
->slots
[level
] + 1;
4273 c
= path
->nodes
[level
];
4274 if (slot
>= btrfs_header_nritems(c
)) {
4276 if (level
== BTRFS_MAX_LEVEL
) {
4284 btrfs_tree_unlock(next
);
4285 free_extent_buffer(next
);
4289 ret
= read_block_for_search(NULL
, root
, path
, &next
, level
,
4295 btrfs_release_path(path
);
4299 if (!path
->skip_locking
) {
4300 ret
= btrfs_try_spin_lock(next
);
4302 btrfs_set_path_blocking(path
);
4303 btrfs_tree_lock(next
);
4304 if (!force_blocking
)
4305 btrfs_clear_path_blocking(path
, next
);
4308 btrfs_set_lock_blocking(next
);
4312 path
->slots
[level
] = slot
;
4315 c
= path
->nodes
[level
];
4316 if (path
->locks
[level
])
4317 btrfs_tree_unlock(c
);
4319 free_extent_buffer(c
);
4320 path
->nodes
[level
] = next
;
4321 path
->slots
[level
] = 0;
4322 if (!path
->skip_locking
)
4323 path
->locks
[level
] = 1;
4328 ret
= read_block_for_search(NULL
, root
, path
, &next
, level
,
4334 btrfs_release_path(path
);
4338 if (!path
->skip_locking
) {
4339 btrfs_assert_tree_locked(path
->nodes
[level
]);
4340 ret
= btrfs_try_spin_lock(next
);
4342 btrfs_set_path_blocking(path
);
4343 btrfs_tree_lock(next
);
4344 if (!force_blocking
)
4345 btrfs_clear_path_blocking(path
, next
);
4348 btrfs_set_lock_blocking(next
);
4353 unlock_up(path
, 0, 1);
4354 path
->leave_spinning
= old_spinning
;
4356 btrfs_set_path_blocking(path
);
4362 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4363 * searching until it gets past min_objectid or finds an item of 'type'
4365 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4367 int btrfs_previous_item(struct btrfs_root
*root
,
4368 struct btrfs_path
*path
, u64 min_objectid
,
4371 struct btrfs_key found_key
;
4372 struct extent_buffer
*leaf
;
4377 if (path
->slots
[0] == 0) {
4378 btrfs_set_path_blocking(path
);
4379 ret
= btrfs_prev_leaf(root
, path
);
4385 leaf
= path
->nodes
[0];
4386 nritems
= btrfs_header_nritems(leaf
);
4389 if (path
->slots
[0] == nritems
)
4392 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4393 if (found_key
.objectid
< min_objectid
)
4395 if (found_key
.type
== type
)
4397 if (found_key
.objectid
== min_objectid
&&
4398 found_key
.type
< type
)