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
;
1235 if (!path
->nodes
[level
])
1238 node
= path
->nodes
[level
];
1240 search
= btrfs_node_blockptr(node
, slot
);
1241 blocksize
= btrfs_level_size(root
, level
- 1);
1242 eb
= btrfs_find_tree_block(root
, search
, blocksize
);
1244 free_extent_buffer(eb
);
1250 nritems
= btrfs_header_nritems(node
);
1253 if (!node
->map_token
) {
1254 unsigned long offset
= btrfs_node_key_ptr_offset(nr
);
1255 map_private_extent_buffer(node
, offset
,
1256 sizeof(struct btrfs_key_ptr
),
1260 &node
->map_len
, KM_USER1
);
1262 if (direction
< 0) {
1266 } else if (direction
> 0) {
1271 if (path
->reada
< 0 && objectid
) {
1272 btrfs_node_key(node
, &disk_key
, nr
);
1273 if (btrfs_disk_key_objectid(&disk_key
) != objectid
)
1276 search
= btrfs_node_blockptr(node
, nr
);
1277 if ((search
<= target
&& target
- search
<= 65536) ||
1278 (search
> target
&& search
- target
<= 65536)) {
1279 gen
= btrfs_node_ptr_generation(node
, nr
);
1280 if (node
->map_token
) {
1281 unmap_extent_buffer(node
, node
->map_token
,
1283 node
->map_token
= NULL
;
1285 readahead_tree_block(root
, search
, blocksize
, gen
);
1289 if ((nread
> 65536 || nscan
> 32))
1292 if (node
->map_token
) {
1293 unmap_extent_buffer(node
, node
->map_token
, KM_USER1
);
1294 node
->map_token
= NULL
;
1299 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1302 static noinline
int reada_for_balance(struct btrfs_root
*root
,
1303 struct btrfs_path
*path
, int level
)
1307 struct extent_buffer
*parent
;
1308 struct extent_buffer
*eb
;
1315 parent
= path
->nodes
[level
+ 1];
1319 nritems
= btrfs_header_nritems(parent
);
1320 slot
= path
->slots
[level
+ 1];
1321 blocksize
= btrfs_level_size(root
, level
);
1324 block1
= btrfs_node_blockptr(parent
, slot
- 1);
1325 gen
= btrfs_node_ptr_generation(parent
, slot
- 1);
1326 eb
= btrfs_find_tree_block(root
, block1
, blocksize
);
1327 if (eb
&& btrfs_buffer_uptodate(eb
, gen
))
1329 free_extent_buffer(eb
);
1331 if (slot
+ 1 < nritems
) {
1332 block2
= btrfs_node_blockptr(parent
, slot
+ 1);
1333 gen
= btrfs_node_ptr_generation(parent
, slot
+ 1);
1334 eb
= btrfs_find_tree_block(root
, block2
, blocksize
);
1335 if (eb
&& btrfs_buffer_uptodate(eb
, gen
))
1337 free_extent_buffer(eb
);
1339 if (block1
|| block2
) {
1342 /* release the whole path */
1343 btrfs_release_path(path
);
1345 /* read the blocks */
1347 readahead_tree_block(root
, block1
, blocksize
, 0);
1349 readahead_tree_block(root
, block2
, blocksize
, 0);
1352 eb
= read_tree_block(root
, block1
, blocksize
, 0);
1353 free_extent_buffer(eb
);
1356 eb
= read_tree_block(root
, block2
, blocksize
, 0);
1357 free_extent_buffer(eb
);
1365 * when we walk down the tree, it is usually safe to unlock the higher layers
1366 * in the tree. The exceptions are when our path goes through slot 0, because
1367 * operations on the tree might require changing key pointers higher up in the
1370 * callers might also have set path->keep_locks, which tells this code to keep
1371 * the lock if the path points to the last slot in the block. This is part of
1372 * walking through the tree, and selecting the next slot in the higher block.
1374 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1375 * if lowest_unlock is 1, level 0 won't be unlocked
1377 static noinline
void unlock_up(struct btrfs_path
*path
, int level
,
1381 int skip_level
= level
;
1383 struct extent_buffer
*t
;
1385 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1386 if (!path
->nodes
[i
])
1388 if (!path
->locks
[i
])
1390 if (!no_skips
&& path
->slots
[i
] == 0) {
1394 if (!no_skips
&& path
->keep_locks
) {
1397 nritems
= btrfs_header_nritems(t
);
1398 if (nritems
< 1 || path
->slots
[i
] >= nritems
- 1) {
1403 if (skip_level
< i
&& i
>= lowest_unlock
)
1407 if (i
>= lowest_unlock
&& i
> skip_level
&& path
->locks
[i
]) {
1408 btrfs_tree_unlock(t
);
1415 * This releases any locks held in the path starting at level and
1416 * going all the way up to the root.
1418 * btrfs_search_slot will keep the lock held on higher nodes in a few
1419 * corner cases, such as COW of the block at slot zero in the node. This
1420 * ignores those rules, and it should only be called when there are no
1421 * more updates to be done higher up in the tree.
1423 noinline
void btrfs_unlock_up_safe(struct btrfs_path
*path
, int level
)
1427 if (path
->keep_locks
)
1430 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1431 if (!path
->nodes
[i
])
1433 if (!path
->locks
[i
])
1435 btrfs_tree_unlock(path
->nodes
[i
]);
1441 * helper function for btrfs_search_slot. The goal is to find a block
1442 * in cache without setting the path to blocking. If we find the block
1443 * we return zero and the path is unchanged.
1445 * If we can't find the block, we set the path blocking and do some
1446 * reada. -EAGAIN is returned and the search must be repeated.
1449 read_block_for_search(struct btrfs_trans_handle
*trans
,
1450 struct btrfs_root
*root
, struct btrfs_path
*p
,
1451 struct extent_buffer
**eb_ret
, int level
, int slot
,
1452 struct btrfs_key
*key
)
1457 struct extent_buffer
*b
= *eb_ret
;
1458 struct extent_buffer
*tmp
;
1461 blocknr
= btrfs_node_blockptr(b
, slot
);
1462 gen
= btrfs_node_ptr_generation(b
, slot
);
1463 blocksize
= btrfs_level_size(root
, level
- 1);
1465 tmp
= btrfs_find_tree_block(root
, blocknr
, blocksize
);
1467 if (btrfs_buffer_uptodate(tmp
, 0)) {
1468 if (btrfs_buffer_uptodate(tmp
, gen
)) {
1470 * we found an up to date block without
1477 /* the pages were up to date, but we failed
1478 * the generation number check. Do a full
1479 * read for the generation number that is correct.
1480 * We must do this without dropping locks so
1481 * we can trust our generation number
1483 free_extent_buffer(tmp
);
1484 tmp
= read_tree_block(root
, blocknr
, blocksize
, gen
);
1485 if (tmp
&& btrfs_buffer_uptodate(tmp
, gen
)) {
1489 free_extent_buffer(tmp
);
1490 btrfs_release_path(p
);
1496 * reduce lock contention at high levels
1497 * of the btree by dropping locks before
1498 * we read. Don't release the lock on the current
1499 * level because we need to walk this node to figure
1500 * out which blocks to read.
1502 btrfs_unlock_up_safe(p
, level
+ 1);
1503 btrfs_set_path_blocking(p
);
1505 free_extent_buffer(tmp
);
1507 reada_for_search(root
, p
, level
, slot
, key
->objectid
);
1509 btrfs_release_path(p
);
1512 tmp
= read_tree_block(root
, blocknr
, blocksize
, 0);
1515 * If the read above didn't mark this buffer up to date,
1516 * it will never end up being up to date. Set ret to EIO now
1517 * and give up so that our caller doesn't loop forever
1520 if (!btrfs_buffer_uptodate(tmp
, 0))
1522 free_extent_buffer(tmp
);
1528 * helper function for btrfs_search_slot. This does all of the checks
1529 * for node-level blocks and does any balancing required based on
1532 * If no extra work was required, zero is returned. If we had to
1533 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1537 setup_nodes_for_search(struct btrfs_trans_handle
*trans
,
1538 struct btrfs_root
*root
, struct btrfs_path
*p
,
1539 struct extent_buffer
*b
, int level
, int ins_len
)
1542 if ((p
->search_for_split
|| ins_len
> 0) && btrfs_header_nritems(b
) >=
1543 BTRFS_NODEPTRS_PER_BLOCK(root
) - 3) {
1546 sret
= reada_for_balance(root
, p
, level
);
1550 btrfs_set_path_blocking(p
);
1551 sret
= split_node(trans
, root
, p
, level
);
1552 btrfs_clear_path_blocking(p
, NULL
);
1559 b
= p
->nodes
[level
];
1560 } else if (ins_len
< 0 && btrfs_header_nritems(b
) <
1561 BTRFS_NODEPTRS_PER_BLOCK(root
) / 2) {
1564 sret
= reada_for_balance(root
, p
, level
);
1568 btrfs_set_path_blocking(p
);
1569 sret
= balance_level(trans
, root
, p
, level
);
1570 btrfs_clear_path_blocking(p
, NULL
);
1576 b
= p
->nodes
[level
];
1578 btrfs_release_path(p
);
1581 BUG_ON(btrfs_header_nritems(b
) == 1);
1592 * look for key in the tree. path is filled in with nodes along the way
1593 * if key is found, we return zero and you can find the item in the leaf
1594 * level of the path (level 0)
1596 * If the key isn't found, the path points to the slot where it should
1597 * be inserted, and 1 is returned. If there are other errors during the
1598 * search a negative error number is returned.
1600 * if ins_len > 0, nodes and leaves will be split as we walk down the
1601 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1604 int btrfs_search_slot(struct btrfs_trans_handle
*trans
, struct btrfs_root
1605 *root
, struct btrfs_key
*key
, struct btrfs_path
*p
, int
1608 struct extent_buffer
*b
;
1613 int lowest_unlock
= 1;
1614 u8 lowest_level
= 0;
1616 lowest_level
= p
->lowest_level
;
1617 WARN_ON(lowest_level
&& ins_len
> 0);
1618 WARN_ON(p
->nodes
[0] != NULL
);
1624 if (p
->search_commit_root
) {
1625 b
= root
->commit_root
;
1626 extent_buffer_get(b
);
1627 if (!p
->skip_locking
)
1630 if (p
->skip_locking
)
1631 b
= btrfs_root_node(root
);
1633 b
= btrfs_lock_root_node(root
);
1637 level
= btrfs_header_level(b
);
1640 * setup the path here so we can release it under lock
1641 * contention with the cow code
1643 p
->nodes
[level
] = b
;
1644 if (!p
->skip_locking
)
1645 p
->locks
[level
] = 1;
1649 * if we don't really need to cow this block
1650 * then we don't want to set the path blocking,
1651 * so we test it here
1653 if (!should_cow_block(trans
, root
, b
))
1656 btrfs_set_path_blocking(p
);
1658 err
= btrfs_cow_block(trans
, root
, b
,
1659 p
->nodes
[level
+ 1],
1660 p
->slots
[level
+ 1], &b
);
1667 BUG_ON(!cow
&& ins_len
);
1669 p
->nodes
[level
] = b
;
1670 if (!p
->skip_locking
)
1671 p
->locks
[level
] = 1;
1673 btrfs_clear_path_blocking(p
, NULL
);
1676 * we have a lock on b and as long as we aren't changing
1677 * the tree, there is no way to for the items in b to change.
1678 * It is safe to drop the lock on our parent before we
1679 * go through the expensive btree search on b.
1681 * If cow is true, then we might be changing slot zero,
1682 * which may require changing the parent. So, we can't
1683 * drop the lock until after we know which slot we're
1687 btrfs_unlock_up_safe(p
, level
+ 1);
1689 ret
= bin_search(b
, key
, level
, &slot
);
1693 if (ret
&& slot
> 0) {
1697 p
->slots
[level
] = slot
;
1698 err
= setup_nodes_for_search(trans
, root
, p
, b
, level
,
1706 b
= p
->nodes
[level
];
1707 slot
= p
->slots
[level
];
1709 unlock_up(p
, level
, lowest_unlock
);
1711 if (level
== lowest_level
) {
1717 err
= read_block_for_search(trans
, root
, p
,
1718 &b
, level
, slot
, key
);
1726 if (!p
->skip_locking
) {
1727 btrfs_clear_path_blocking(p
, NULL
);
1728 err
= btrfs_try_spin_lock(b
);
1731 btrfs_set_path_blocking(p
);
1733 btrfs_clear_path_blocking(p
, b
);
1737 p
->slots
[level
] = slot
;
1739 btrfs_leaf_free_space(root
, b
) < ins_len
) {
1740 btrfs_set_path_blocking(p
);
1741 err
= split_leaf(trans
, root
, key
,
1742 p
, ins_len
, ret
== 0);
1743 btrfs_clear_path_blocking(p
, NULL
);
1751 if (!p
->search_for_split
)
1752 unlock_up(p
, level
, lowest_unlock
);
1759 * we don't really know what they plan on doing with the path
1760 * from here on, so for now just mark it as blocking
1762 if (!p
->leave_spinning
)
1763 btrfs_set_path_blocking(p
);
1765 btrfs_release_path(p
);
1770 * adjust the pointers going up the tree, starting at level
1771 * making sure the right key of each node is points to 'key'.
1772 * This is used after shifting pointers to the left, so it stops
1773 * fixing up pointers when a given leaf/node is not in slot 0 of the
1776 * If this fails to write a tree block, it returns -1, but continues
1777 * fixing up the blocks in ram so the tree is consistent.
1779 static int fixup_low_keys(struct btrfs_trans_handle
*trans
,
1780 struct btrfs_root
*root
, struct btrfs_path
*path
,
1781 struct btrfs_disk_key
*key
, int level
)
1785 struct extent_buffer
*t
;
1787 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1788 int tslot
= path
->slots
[i
];
1789 if (!path
->nodes
[i
])
1792 btrfs_set_node_key(t
, key
, tslot
);
1793 btrfs_mark_buffer_dirty(path
->nodes
[i
]);
1803 * This function isn't completely safe. It's the caller's responsibility
1804 * that the new key won't break the order
1806 int btrfs_set_item_key_safe(struct btrfs_trans_handle
*trans
,
1807 struct btrfs_root
*root
, struct btrfs_path
*path
,
1808 struct btrfs_key
*new_key
)
1810 struct btrfs_disk_key disk_key
;
1811 struct extent_buffer
*eb
;
1814 eb
= path
->nodes
[0];
1815 slot
= path
->slots
[0];
1817 btrfs_item_key(eb
, &disk_key
, slot
- 1);
1818 if (comp_keys(&disk_key
, new_key
) >= 0)
1821 if (slot
< btrfs_header_nritems(eb
) - 1) {
1822 btrfs_item_key(eb
, &disk_key
, slot
+ 1);
1823 if (comp_keys(&disk_key
, new_key
) <= 0)
1827 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
1828 btrfs_set_item_key(eb
, &disk_key
, slot
);
1829 btrfs_mark_buffer_dirty(eb
);
1831 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
1836 * try to push data from one node into the next node left in the
1839 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1840 * error, and > 0 if there was no room in the left hand block.
1842 static int push_node_left(struct btrfs_trans_handle
*trans
,
1843 struct btrfs_root
*root
, struct extent_buffer
*dst
,
1844 struct extent_buffer
*src
, int empty
)
1851 src_nritems
= btrfs_header_nritems(src
);
1852 dst_nritems
= btrfs_header_nritems(dst
);
1853 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1854 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1855 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1857 if (!empty
&& src_nritems
<= 8)
1860 if (push_items
<= 0)
1864 push_items
= min(src_nritems
, push_items
);
1865 if (push_items
< src_nritems
) {
1866 /* leave at least 8 pointers in the node if
1867 * we aren't going to empty it
1869 if (src_nritems
- push_items
< 8) {
1870 if (push_items
<= 8)
1876 push_items
= min(src_nritems
- 8, push_items
);
1878 copy_extent_buffer(dst
, src
,
1879 btrfs_node_key_ptr_offset(dst_nritems
),
1880 btrfs_node_key_ptr_offset(0),
1881 push_items
* sizeof(struct btrfs_key_ptr
));
1883 if (push_items
< src_nritems
) {
1884 memmove_extent_buffer(src
, btrfs_node_key_ptr_offset(0),
1885 btrfs_node_key_ptr_offset(push_items
),
1886 (src_nritems
- push_items
) *
1887 sizeof(struct btrfs_key_ptr
));
1889 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1890 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1891 btrfs_mark_buffer_dirty(src
);
1892 btrfs_mark_buffer_dirty(dst
);
1898 * try to push data from one node into the next node right in the
1901 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
1902 * error, and > 0 if there was no room in the right hand block.
1904 * this will only push up to 1/2 the contents of the left node over
1906 static int balance_node_right(struct btrfs_trans_handle
*trans
,
1907 struct btrfs_root
*root
,
1908 struct extent_buffer
*dst
,
1909 struct extent_buffer
*src
)
1917 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1918 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1920 src_nritems
= btrfs_header_nritems(src
);
1921 dst_nritems
= btrfs_header_nritems(dst
);
1922 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1923 if (push_items
<= 0)
1926 if (src_nritems
< 4)
1929 max_push
= src_nritems
/ 2 + 1;
1930 /* don't try to empty the node */
1931 if (max_push
>= src_nritems
)
1934 if (max_push
< push_items
)
1935 push_items
= max_push
;
1937 memmove_extent_buffer(dst
, btrfs_node_key_ptr_offset(push_items
),
1938 btrfs_node_key_ptr_offset(0),
1940 sizeof(struct btrfs_key_ptr
));
1942 copy_extent_buffer(dst
, src
,
1943 btrfs_node_key_ptr_offset(0),
1944 btrfs_node_key_ptr_offset(src_nritems
- push_items
),
1945 push_items
* sizeof(struct btrfs_key_ptr
));
1947 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
1948 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
1950 btrfs_mark_buffer_dirty(src
);
1951 btrfs_mark_buffer_dirty(dst
);
1957 * helper function to insert a new root level in the tree.
1958 * A new node is allocated, and a single item is inserted to
1959 * point to the existing root
1961 * returns zero on success or < 0 on failure.
1963 static noinline
int insert_new_root(struct btrfs_trans_handle
*trans
,
1964 struct btrfs_root
*root
,
1965 struct btrfs_path
*path
, int level
)
1968 struct extent_buffer
*lower
;
1969 struct extent_buffer
*c
;
1970 struct extent_buffer
*old
;
1971 struct btrfs_disk_key lower_key
;
1973 BUG_ON(path
->nodes
[level
]);
1974 BUG_ON(path
->nodes
[level
-1] != root
->node
);
1976 lower
= path
->nodes
[level
-1];
1978 btrfs_item_key(lower
, &lower_key
, 0);
1980 btrfs_node_key(lower
, &lower_key
, 0);
1982 c
= btrfs_alloc_free_block(trans
, root
, root
->nodesize
, 0,
1983 root
->root_key
.objectid
, &lower_key
,
1984 level
, root
->node
->start
, 0);
1988 root_add_used(root
, root
->nodesize
);
1990 memset_extent_buffer(c
, 0, 0, sizeof(struct btrfs_header
));
1991 btrfs_set_header_nritems(c
, 1);
1992 btrfs_set_header_level(c
, level
);
1993 btrfs_set_header_bytenr(c
, c
->start
);
1994 btrfs_set_header_generation(c
, trans
->transid
);
1995 btrfs_set_header_backref_rev(c
, BTRFS_MIXED_BACKREF_REV
);
1996 btrfs_set_header_owner(c
, root
->root_key
.objectid
);
1998 write_extent_buffer(c
, root
->fs_info
->fsid
,
1999 (unsigned long)btrfs_header_fsid(c
),
2002 write_extent_buffer(c
, root
->fs_info
->chunk_tree_uuid
,
2003 (unsigned long)btrfs_header_chunk_tree_uuid(c
),
2006 btrfs_set_node_key(c
, &lower_key
, 0);
2007 btrfs_set_node_blockptr(c
, 0, lower
->start
);
2008 lower_gen
= btrfs_header_generation(lower
);
2009 WARN_ON(lower_gen
!= trans
->transid
);
2011 btrfs_set_node_ptr_generation(c
, 0, lower_gen
);
2013 btrfs_mark_buffer_dirty(c
);
2016 rcu_assign_pointer(root
->node
, c
);
2018 /* the super has an extra ref to root->node */
2019 free_extent_buffer(old
);
2021 add_root_to_dirty_list(root
);
2022 extent_buffer_get(c
);
2023 path
->nodes
[level
] = c
;
2024 path
->locks
[level
] = 1;
2025 path
->slots
[level
] = 0;
2030 * worker function to insert a single pointer in a node.
2031 * the node should have enough room for the pointer already
2033 * slot and level indicate where you want the key to go, and
2034 * blocknr is the block the key points to.
2036 * returns zero on success and < 0 on any error
2038 static int insert_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
2039 *root
, struct btrfs_path
*path
, struct btrfs_disk_key
2040 *key
, u64 bytenr
, int slot
, int level
)
2042 struct extent_buffer
*lower
;
2045 BUG_ON(!path
->nodes
[level
]);
2046 btrfs_assert_tree_locked(path
->nodes
[level
]);
2047 lower
= path
->nodes
[level
];
2048 nritems
= btrfs_header_nritems(lower
);
2049 BUG_ON(slot
> nritems
);
2050 if (nritems
== BTRFS_NODEPTRS_PER_BLOCK(root
))
2052 if (slot
!= nritems
) {
2053 memmove_extent_buffer(lower
,
2054 btrfs_node_key_ptr_offset(slot
+ 1),
2055 btrfs_node_key_ptr_offset(slot
),
2056 (nritems
- slot
) * sizeof(struct btrfs_key_ptr
));
2058 btrfs_set_node_key(lower
, key
, slot
);
2059 btrfs_set_node_blockptr(lower
, slot
, bytenr
);
2060 WARN_ON(trans
->transid
== 0);
2061 btrfs_set_node_ptr_generation(lower
, slot
, trans
->transid
);
2062 btrfs_set_header_nritems(lower
, nritems
+ 1);
2063 btrfs_mark_buffer_dirty(lower
);
2068 * split the node at the specified level in path in two.
2069 * The path is corrected to point to the appropriate node after the split
2071 * Before splitting this tries to make some room in the node by pushing
2072 * left and right, if either one works, it returns right away.
2074 * returns 0 on success and < 0 on failure
2076 static noinline
int split_node(struct btrfs_trans_handle
*trans
,
2077 struct btrfs_root
*root
,
2078 struct btrfs_path
*path
, int level
)
2080 struct extent_buffer
*c
;
2081 struct extent_buffer
*split
;
2082 struct btrfs_disk_key disk_key
;
2088 c
= path
->nodes
[level
];
2089 WARN_ON(btrfs_header_generation(c
) != trans
->transid
);
2090 if (c
== root
->node
) {
2091 /* trying to split the root, lets make a new one */
2092 ret
= insert_new_root(trans
, root
, path
, level
+ 1);
2096 ret
= push_nodes_for_insert(trans
, root
, path
, level
);
2097 c
= path
->nodes
[level
];
2098 if (!ret
&& btrfs_header_nritems(c
) <
2099 BTRFS_NODEPTRS_PER_BLOCK(root
) - 3)
2105 c_nritems
= btrfs_header_nritems(c
);
2106 mid
= (c_nritems
+ 1) / 2;
2107 btrfs_node_key(c
, &disk_key
, mid
);
2109 split
= btrfs_alloc_free_block(trans
, root
, root
->nodesize
, 0,
2110 root
->root_key
.objectid
,
2111 &disk_key
, level
, c
->start
, 0);
2113 return PTR_ERR(split
);
2115 root_add_used(root
, root
->nodesize
);
2117 memset_extent_buffer(split
, 0, 0, sizeof(struct btrfs_header
));
2118 btrfs_set_header_level(split
, btrfs_header_level(c
));
2119 btrfs_set_header_bytenr(split
, split
->start
);
2120 btrfs_set_header_generation(split
, trans
->transid
);
2121 btrfs_set_header_backref_rev(split
, BTRFS_MIXED_BACKREF_REV
);
2122 btrfs_set_header_owner(split
, root
->root_key
.objectid
);
2123 write_extent_buffer(split
, root
->fs_info
->fsid
,
2124 (unsigned long)btrfs_header_fsid(split
),
2126 write_extent_buffer(split
, root
->fs_info
->chunk_tree_uuid
,
2127 (unsigned long)btrfs_header_chunk_tree_uuid(split
),
2131 copy_extent_buffer(split
, c
,
2132 btrfs_node_key_ptr_offset(0),
2133 btrfs_node_key_ptr_offset(mid
),
2134 (c_nritems
- mid
) * sizeof(struct btrfs_key_ptr
));
2135 btrfs_set_header_nritems(split
, c_nritems
- mid
);
2136 btrfs_set_header_nritems(c
, mid
);
2139 btrfs_mark_buffer_dirty(c
);
2140 btrfs_mark_buffer_dirty(split
);
2142 wret
= insert_ptr(trans
, root
, path
, &disk_key
, split
->start
,
2143 path
->slots
[level
+ 1] + 1,
2148 if (path
->slots
[level
] >= mid
) {
2149 path
->slots
[level
] -= mid
;
2150 btrfs_tree_unlock(c
);
2151 free_extent_buffer(c
);
2152 path
->nodes
[level
] = split
;
2153 path
->slots
[level
+ 1] += 1;
2155 btrfs_tree_unlock(split
);
2156 free_extent_buffer(split
);
2162 * how many bytes are required to store the items in a leaf. start
2163 * and nr indicate which items in the leaf to check. This totals up the
2164 * space used both by the item structs and the item data
2166 static int leaf_space_used(struct extent_buffer
*l
, int start
, int nr
)
2169 int nritems
= btrfs_header_nritems(l
);
2170 int end
= min(nritems
, start
+ nr
) - 1;
2174 data_len
= btrfs_item_end_nr(l
, start
);
2175 data_len
= data_len
- btrfs_item_offset_nr(l
, end
);
2176 data_len
+= sizeof(struct btrfs_item
) * nr
;
2177 WARN_ON(data_len
< 0);
2182 * The space between the end of the leaf items and
2183 * the start of the leaf data. IOW, how much room
2184 * the leaf has left for both items and data
2186 noinline
int btrfs_leaf_free_space(struct btrfs_root
*root
,
2187 struct extent_buffer
*leaf
)
2189 int nritems
= btrfs_header_nritems(leaf
);
2191 ret
= BTRFS_LEAF_DATA_SIZE(root
) - leaf_space_used(leaf
, 0, nritems
);
2193 printk(KERN_CRIT
"leaf free space ret %d, leaf data size %lu, "
2194 "used %d nritems %d\n",
2195 ret
, (unsigned long) BTRFS_LEAF_DATA_SIZE(root
),
2196 leaf_space_used(leaf
, 0, nritems
), nritems
);
2202 * min slot controls the lowest index we're willing to push to the
2203 * right. We'll push up to and including min_slot, but no lower
2205 static noinline
int __push_leaf_right(struct btrfs_trans_handle
*trans
,
2206 struct btrfs_root
*root
,
2207 struct btrfs_path
*path
,
2208 int data_size
, int empty
,
2209 struct extent_buffer
*right
,
2210 int free_space
, u32 left_nritems
,
2213 struct extent_buffer
*left
= path
->nodes
[0];
2214 struct extent_buffer
*upper
= path
->nodes
[1];
2215 struct btrfs_disk_key disk_key
;
2220 struct btrfs_item
*item
;
2229 nr
= max_t(u32
, 1, min_slot
);
2231 if (path
->slots
[0] >= left_nritems
)
2232 push_space
+= data_size
;
2234 slot
= path
->slots
[1];
2235 i
= left_nritems
- 1;
2237 item
= btrfs_item_nr(left
, i
);
2239 if (!empty
&& push_items
> 0) {
2240 if (path
->slots
[0] > i
)
2242 if (path
->slots
[0] == i
) {
2243 int space
= btrfs_leaf_free_space(root
, left
);
2244 if (space
+ push_space
* 2 > free_space
)
2249 if (path
->slots
[0] == i
)
2250 push_space
+= data_size
;
2252 if (!left
->map_token
) {
2253 map_extent_buffer(left
, (unsigned long)item
,
2254 sizeof(struct btrfs_item
),
2255 &left
->map_token
, &left
->kaddr
,
2256 &left
->map_start
, &left
->map_len
,
2260 this_item_size
= btrfs_item_size(left
, item
);
2261 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
2265 push_space
+= this_item_size
+ sizeof(*item
);
2270 if (left
->map_token
) {
2271 unmap_extent_buffer(left
, left
->map_token
, KM_USER1
);
2272 left
->map_token
= NULL
;
2275 if (push_items
== 0)
2278 if (!empty
&& push_items
== left_nritems
)
2281 /* push left to right */
2282 right_nritems
= btrfs_header_nritems(right
);
2284 push_space
= btrfs_item_end_nr(left
, left_nritems
- push_items
);
2285 push_space
-= leaf_data_end(root
, left
);
2287 /* make room in the right data area */
2288 data_end
= leaf_data_end(root
, right
);
2289 memmove_extent_buffer(right
,
2290 btrfs_leaf_data(right
) + data_end
- push_space
,
2291 btrfs_leaf_data(right
) + data_end
,
2292 BTRFS_LEAF_DATA_SIZE(root
) - data_end
);
2294 /* copy from the left data area */
2295 copy_extent_buffer(right
, left
, btrfs_leaf_data(right
) +
2296 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
2297 btrfs_leaf_data(left
) + leaf_data_end(root
, left
),
2300 memmove_extent_buffer(right
, btrfs_item_nr_offset(push_items
),
2301 btrfs_item_nr_offset(0),
2302 right_nritems
* sizeof(struct btrfs_item
));
2304 /* copy the items from left to right */
2305 copy_extent_buffer(right
, left
, btrfs_item_nr_offset(0),
2306 btrfs_item_nr_offset(left_nritems
- push_items
),
2307 push_items
* sizeof(struct btrfs_item
));
2309 /* update the item pointers */
2310 right_nritems
+= push_items
;
2311 btrfs_set_header_nritems(right
, right_nritems
);
2312 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
2313 for (i
= 0; i
< right_nritems
; i
++) {
2314 item
= btrfs_item_nr(right
, i
);
2315 if (!right
->map_token
) {
2316 map_extent_buffer(right
, (unsigned long)item
,
2317 sizeof(struct btrfs_item
),
2318 &right
->map_token
, &right
->kaddr
,
2319 &right
->map_start
, &right
->map_len
,
2322 push_space
-= btrfs_item_size(right
, item
);
2323 btrfs_set_item_offset(right
, item
, push_space
);
2326 if (right
->map_token
) {
2327 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
2328 right
->map_token
= NULL
;
2330 left_nritems
-= push_items
;
2331 btrfs_set_header_nritems(left
, left_nritems
);
2334 btrfs_mark_buffer_dirty(left
);
2336 clean_tree_block(trans
, root
, left
);
2338 btrfs_mark_buffer_dirty(right
);
2340 btrfs_item_key(right
, &disk_key
, 0);
2341 btrfs_set_node_key(upper
, &disk_key
, slot
+ 1);
2342 btrfs_mark_buffer_dirty(upper
);
2344 /* then fixup the leaf pointer in the path */
2345 if (path
->slots
[0] >= left_nritems
) {
2346 path
->slots
[0] -= left_nritems
;
2347 if (btrfs_header_nritems(path
->nodes
[0]) == 0)
2348 clean_tree_block(trans
, root
, path
->nodes
[0]);
2349 btrfs_tree_unlock(path
->nodes
[0]);
2350 free_extent_buffer(path
->nodes
[0]);
2351 path
->nodes
[0] = right
;
2352 path
->slots
[1] += 1;
2354 btrfs_tree_unlock(right
);
2355 free_extent_buffer(right
);
2360 btrfs_tree_unlock(right
);
2361 free_extent_buffer(right
);
2366 * push some data in the path leaf to the right, trying to free up at
2367 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2369 * returns 1 if the push failed because the other node didn't have enough
2370 * room, 0 if everything worked out and < 0 if there were major errors.
2372 * this will push starting from min_slot to the end of the leaf. It won't
2373 * push any slot lower than min_slot
2375 static int push_leaf_right(struct btrfs_trans_handle
*trans
, struct btrfs_root
2376 *root
, struct btrfs_path
*path
,
2377 int min_data_size
, int data_size
,
2378 int empty
, u32 min_slot
)
2380 struct extent_buffer
*left
= path
->nodes
[0];
2381 struct extent_buffer
*right
;
2382 struct extent_buffer
*upper
;
2388 if (!path
->nodes
[1])
2391 slot
= path
->slots
[1];
2392 upper
= path
->nodes
[1];
2393 if (slot
>= btrfs_header_nritems(upper
) - 1)
2396 btrfs_assert_tree_locked(path
->nodes
[1]);
2398 right
= read_node_slot(root
, upper
, slot
+ 1);
2402 btrfs_tree_lock(right
);
2403 btrfs_set_lock_blocking(right
);
2405 free_space
= btrfs_leaf_free_space(root
, right
);
2406 if (free_space
< data_size
)
2409 /* cow and double check */
2410 ret
= btrfs_cow_block(trans
, root
, right
, upper
,
2415 free_space
= btrfs_leaf_free_space(root
, right
);
2416 if (free_space
< data_size
)
2419 left_nritems
= btrfs_header_nritems(left
);
2420 if (left_nritems
== 0)
2423 return __push_leaf_right(trans
, root
, path
, min_data_size
, empty
,
2424 right
, free_space
, left_nritems
, min_slot
);
2426 btrfs_tree_unlock(right
);
2427 free_extent_buffer(right
);
2432 * push some data in the path leaf to the left, trying to free up at
2433 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2435 * max_slot can put a limit on how far into the leaf we'll push items. The
2436 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2439 static noinline
int __push_leaf_left(struct btrfs_trans_handle
*trans
,
2440 struct btrfs_root
*root
,
2441 struct btrfs_path
*path
, int data_size
,
2442 int empty
, struct extent_buffer
*left
,
2443 int free_space
, u32 right_nritems
,
2446 struct btrfs_disk_key disk_key
;
2447 struct extent_buffer
*right
= path
->nodes
[0];
2451 struct btrfs_item
*item
;
2452 u32 old_left_nritems
;
2457 u32 old_left_item_size
;
2460 nr
= min(right_nritems
, max_slot
);
2462 nr
= min(right_nritems
- 1, max_slot
);
2464 for (i
= 0; i
< nr
; i
++) {
2465 item
= btrfs_item_nr(right
, i
);
2466 if (!right
->map_token
) {
2467 map_extent_buffer(right
, (unsigned long)item
,
2468 sizeof(struct btrfs_item
),
2469 &right
->map_token
, &right
->kaddr
,
2470 &right
->map_start
, &right
->map_len
,
2474 if (!empty
&& push_items
> 0) {
2475 if (path
->slots
[0] < i
)
2477 if (path
->slots
[0] == i
) {
2478 int space
= btrfs_leaf_free_space(root
, right
);
2479 if (space
+ push_space
* 2 > free_space
)
2484 if (path
->slots
[0] == i
)
2485 push_space
+= data_size
;
2487 this_item_size
= btrfs_item_size(right
, item
);
2488 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
2492 push_space
+= this_item_size
+ sizeof(*item
);
2495 if (right
->map_token
) {
2496 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
2497 right
->map_token
= NULL
;
2500 if (push_items
== 0) {
2504 if (!empty
&& push_items
== btrfs_header_nritems(right
))
2507 /* push data from right to left */
2508 copy_extent_buffer(left
, right
,
2509 btrfs_item_nr_offset(btrfs_header_nritems(left
)),
2510 btrfs_item_nr_offset(0),
2511 push_items
* sizeof(struct btrfs_item
));
2513 push_space
= BTRFS_LEAF_DATA_SIZE(root
) -
2514 btrfs_item_offset_nr(right
, push_items
- 1);
2516 copy_extent_buffer(left
, right
, btrfs_leaf_data(left
) +
2517 leaf_data_end(root
, left
) - push_space
,
2518 btrfs_leaf_data(right
) +
2519 btrfs_item_offset_nr(right
, push_items
- 1),
2521 old_left_nritems
= btrfs_header_nritems(left
);
2522 BUG_ON(old_left_nritems
<= 0);
2524 old_left_item_size
= btrfs_item_offset_nr(left
, old_left_nritems
- 1);
2525 for (i
= old_left_nritems
; i
< old_left_nritems
+ push_items
; i
++) {
2528 item
= btrfs_item_nr(left
, i
);
2529 if (!left
->map_token
) {
2530 map_extent_buffer(left
, (unsigned long)item
,
2531 sizeof(struct btrfs_item
),
2532 &left
->map_token
, &left
->kaddr
,
2533 &left
->map_start
, &left
->map_len
,
2537 ioff
= btrfs_item_offset(left
, item
);
2538 btrfs_set_item_offset(left
, item
,
2539 ioff
- (BTRFS_LEAF_DATA_SIZE(root
) - old_left_item_size
));
2541 btrfs_set_header_nritems(left
, old_left_nritems
+ push_items
);
2542 if (left
->map_token
) {
2543 unmap_extent_buffer(left
, left
->map_token
, KM_USER1
);
2544 left
->map_token
= NULL
;
2547 /* fixup right node */
2548 if (push_items
> right_nritems
) {
2549 printk(KERN_CRIT
"push items %d nr %u\n", push_items
,
2554 if (push_items
< right_nritems
) {
2555 push_space
= btrfs_item_offset_nr(right
, push_items
- 1) -
2556 leaf_data_end(root
, right
);
2557 memmove_extent_buffer(right
, btrfs_leaf_data(right
) +
2558 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
2559 btrfs_leaf_data(right
) +
2560 leaf_data_end(root
, right
), push_space
);
2562 memmove_extent_buffer(right
, btrfs_item_nr_offset(0),
2563 btrfs_item_nr_offset(push_items
),
2564 (btrfs_header_nritems(right
) - push_items
) *
2565 sizeof(struct btrfs_item
));
2567 right_nritems
-= push_items
;
2568 btrfs_set_header_nritems(right
, right_nritems
);
2569 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
2570 for (i
= 0; i
< right_nritems
; i
++) {
2571 item
= btrfs_item_nr(right
, i
);
2573 if (!right
->map_token
) {
2574 map_extent_buffer(right
, (unsigned long)item
,
2575 sizeof(struct btrfs_item
),
2576 &right
->map_token
, &right
->kaddr
,
2577 &right
->map_start
, &right
->map_len
,
2581 push_space
= push_space
- btrfs_item_size(right
, item
);
2582 btrfs_set_item_offset(right
, item
, push_space
);
2584 if (right
->map_token
) {
2585 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
2586 right
->map_token
= NULL
;
2589 btrfs_mark_buffer_dirty(left
);
2591 btrfs_mark_buffer_dirty(right
);
2593 clean_tree_block(trans
, root
, right
);
2595 btrfs_item_key(right
, &disk_key
, 0);
2596 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
2600 /* then fixup the leaf pointer in the path */
2601 if (path
->slots
[0] < push_items
) {
2602 path
->slots
[0] += old_left_nritems
;
2603 btrfs_tree_unlock(path
->nodes
[0]);
2604 free_extent_buffer(path
->nodes
[0]);
2605 path
->nodes
[0] = left
;
2606 path
->slots
[1] -= 1;
2608 btrfs_tree_unlock(left
);
2609 free_extent_buffer(left
);
2610 path
->slots
[0] -= push_items
;
2612 BUG_ON(path
->slots
[0] < 0);
2615 btrfs_tree_unlock(left
);
2616 free_extent_buffer(left
);
2621 * push some data in the path leaf to the left, trying to free up at
2622 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2624 * max_slot can put a limit on how far into the leaf we'll push items. The
2625 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2628 static int push_leaf_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
2629 *root
, struct btrfs_path
*path
, int min_data_size
,
2630 int data_size
, int empty
, u32 max_slot
)
2632 struct extent_buffer
*right
= path
->nodes
[0];
2633 struct extent_buffer
*left
;
2639 slot
= path
->slots
[1];
2642 if (!path
->nodes
[1])
2645 right_nritems
= btrfs_header_nritems(right
);
2646 if (right_nritems
== 0)
2649 btrfs_assert_tree_locked(path
->nodes
[1]);
2651 left
= read_node_slot(root
, path
->nodes
[1], slot
- 1);
2655 btrfs_tree_lock(left
);
2656 btrfs_set_lock_blocking(left
);
2658 free_space
= btrfs_leaf_free_space(root
, left
);
2659 if (free_space
< data_size
) {
2664 /* cow and double check */
2665 ret
= btrfs_cow_block(trans
, root
, left
,
2666 path
->nodes
[1], slot
- 1, &left
);
2668 /* we hit -ENOSPC, but it isn't fatal here */
2673 free_space
= btrfs_leaf_free_space(root
, left
);
2674 if (free_space
< data_size
) {
2679 return __push_leaf_left(trans
, root
, path
, min_data_size
,
2680 empty
, left
, free_space
, right_nritems
,
2683 btrfs_tree_unlock(left
);
2684 free_extent_buffer(left
);
2689 * split the path's leaf in two, making sure there is at least data_size
2690 * available for the resulting leaf level of the path.
2692 * returns 0 if all went well and < 0 on failure.
2694 static noinline
int copy_for_split(struct btrfs_trans_handle
*trans
,
2695 struct btrfs_root
*root
,
2696 struct btrfs_path
*path
,
2697 struct extent_buffer
*l
,
2698 struct extent_buffer
*right
,
2699 int slot
, int mid
, int nritems
)
2706 struct btrfs_disk_key disk_key
;
2708 nritems
= nritems
- mid
;
2709 btrfs_set_header_nritems(right
, nritems
);
2710 data_copy_size
= btrfs_item_end_nr(l
, mid
) - leaf_data_end(root
, l
);
2712 copy_extent_buffer(right
, l
, btrfs_item_nr_offset(0),
2713 btrfs_item_nr_offset(mid
),
2714 nritems
* sizeof(struct btrfs_item
));
2716 copy_extent_buffer(right
, l
,
2717 btrfs_leaf_data(right
) + BTRFS_LEAF_DATA_SIZE(root
) -
2718 data_copy_size
, btrfs_leaf_data(l
) +
2719 leaf_data_end(root
, l
), data_copy_size
);
2721 rt_data_off
= BTRFS_LEAF_DATA_SIZE(root
) -
2722 btrfs_item_end_nr(l
, mid
);
2724 for (i
= 0; i
< nritems
; i
++) {
2725 struct btrfs_item
*item
= btrfs_item_nr(right
, i
);
2728 if (!right
->map_token
) {
2729 map_extent_buffer(right
, (unsigned long)item
,
2730 sizeof(struct btrfs_item
),
2731 &right
->map_token
, &right
->kaddr
,
2732 &right
->map_start
, &right
->map_len
,
2736 ioff
= btrfs_item_offset(right
, item
);
2737 btrfs_set_item_offset(right
, item
, ioff
+ rt_data_off
);
2740 if (right
->map_token
) {
2741 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
2742 right
->map_token
= NULL
;
2745 btrfs_set_header_nritems(l
, mid
);
2747 btrfs_item_key(right
, &disk_key
, 0);
2748 wret
= insert_ptr(trans
, root
, path
, &disk_key
, right
->start
,
2749 path
->slots
[1] + 1, 1);
2753 btrfs_mark_buffer_dirty(right
);
2754 btrfs_mark_buffer_dirty(l
);
2755 BUG_ON(path
->slots
[0] != slot
);
2758 btrfs_tree_unlock(path
->nodes
[0]);
2759 free_extent_buffer(path
->nodes
[0]);
2760 path
->nodes
[0] = right
;
2761 path
->slots
[0] -= mid
;
2762 path
->slots
[1] += 1;
2764 btrfs_tree_unlock(right
);
2765 free_extent_buffer(right
);
2768 BUG_ON(path
->slots
[0] < 0);
2774 * double splits happen when we need to insert a big item in the middle
2775 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2776 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2779 * We avoid this by trying to push the items on either side of our target
2780 * into the adjacent leaves. If all goes well we can avoid the double split
2783 static noinline
int push_for_double_split(struct btrfs_trans_handle
*trans
,
2784 struct btrfs_root
*root
,
2785 struct btrfs_path
*path
,
2793 slot
= path
->slots
[0];
2796 * try to push all the items after our slot into the
2799 ret
= push_leaf_right(trans
, root
, path
, 1, data_size
, 0, slot
);
2806 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2808 * our goal is to get our slot at the start or end of a leaf. If
2809 * we've done so we're done
2811 if (path
->slots
[0] == 0 || path
->slots
[0] == nritems
)
2814 if (btrfs_leaf_free_space(root
, path
->nodes
[0]) >= data_size
)
2817 /* try to push all the items before our slot into the next leaf */
2818 slot
= path
->slots
[0];
2819 ret
= push_leaf_left(trans
, root
, path
, 1, data_size
, 0, slot
);
2832 * split the path's leaf in two, making sure there is at least data_size
2833 * available for the resulting leaf level of the path.
2835 * returns 0 if all went well and < 0 on failure.
2837 static noinline
int split_leaf(struct btrfs_trans_handle
*trans
,
2838 struct btrfs_root
*root
,
2839 struct btrfs_key
*ins_key
,
2840 struct btrfs_path
*path
, int data_size
,
2843 struct btrfs_disk_key disk_key
;
2844 struct extent_buffer
*l
;
2848 struct extent_buffer
*right
;
2852 int num_doubles
= 0;
2853 int tried_avoid_double
= 0;
2856 slot
= path
->slots
[0];
2857 if (extend
&& data_size
+ btrfs_item_size_nr(l
, slot
) +
2858 sizeof(struct btrfs_item
) > BTRFS_LEAF_DATA_SIZE(root
))
2861 /* first try to make some room by pushing left and right */
2863 wret
= push_leaf_right(trans
, root
, path
, data_size
,
2868 wret
= push_leaf_left(trans
, root
, path
, data_size
,
2869 data_size
, 0, (u32
)-1);
2875 /* did the pushes work? */
2876 if (btrfs_leaf_free_space(root
, l
) >= data_size
)
2880 if (!path
->nodes
[1]) {
2881 ret
= insert_new_root(trans
, root
, path
, 1);
2888 slot
= path
->slots
[0];
2889 nritems
= btrfs_header_nritems(l
);
2890 mid
= (nritems
+ 1) / 2;
2894 leaf_space_used(l
, mid
, nritems
- mid
) + data_size
>
2895 BTRFS_LEAF_DATA_SIZE(root
)) {
2896 if (slot
>= nritems
) {
2900 if (mid
!= nritems
&&
2901 leaf_space_used(l
, mid
, nritems
- mid
) +
2902 data_size
> BTRFS_LEAF_DATA_SIZE(root
)) {
2903 if (data_size
&& !tried_avoid_double
)
2904 goto push_for_double
;
2910 if (leaf_space_used(l
, 0, mid
) + data_size
>
2911 BTRFS_LEAF_DATA_SIZE(root
)) {
2912 if (!extend
&& data_size
&& slot
== 0) {
2914 } else if ((extend
|| !data_size
) && slot
== 0) {
2918 if (mid
!= nritems
&&
2919 leaf_space_used(l
, mid
, nritems
- mid
) +
2920 data_size
> BTRFS_LEAF_DATA_SIZE(root
)) {
2921 if (data_size
&& !tried_avoid_double
)
2922 goto push_for_double
;
2930 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
2932 btrfs_item_key(l
, &disk_key
, mid
);
2934 right
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
, 0,
2935 root
->root_key
.objectid
,
2936 &disk_key
, 0, l
->start
, 0);
2938 return PTR_ERR(right
);
2940 root_add_used(root
, root
->leafsize
);
2942 memset_extent_buffer(right
, 0, 0, sizeof(struct btrfs_header
));
2943 btrfs_set_header_bytenr(right
, right
->start
);
2944 btrfs_set_header_generation(right
, trans
->transid
);
2945 btrfs_set_header_backref_rev(right
, BTRFS_MIXED_BACKREF_REV
);
2946 btrfs_set_header_owner(right
, root
->root_key
.objectid
);
2947 btrfs_set_header_level(right
, 0);
2948 write_extent_buffer(right
, root
->fs_info
->fsid
,
2949 (unsigned long)btrfs_header_fsid(right
),
2952 write_extent_buffer(right
, root
->fs_info
->chunk_tree_uuid
,
2953 (unsigned long)btrfs_header_chunk_tree_uuid(right
),
2958 btrfs_set_header_nritems(right
, 0);
2959 wret
= insert_ptr(trans
, root
, path
,
2960 &disk_key
, right
->start
,
2961 path
->slots
[1] + 1, 1);
2965 btrfs_tree_unlock(path
->nodes
[0]);
2966 free_extent_buffer(path
->nodes
[0]);
2967 path
->nodes
[0] = right
;
2969 path
->slots
[1] += 1;
2971 btrfs_set_header_nritems(right
, 0);
2972 wret
= insert_ptr(trans
, root
, path
,
2978 btrfs_tree_unlock(path
->nodes
[0]);
2979 free_extent_buffer(path
->nodes
[0]);
2980 path
->nodes
[0] = right
;
2982 if (path
->slots
[1] == 0) {
2983 wret
= fixup_low_keys(trans
, root
,
2984 path
, &disk_key
, 1);
2989 btrfs_mark_buffer_dirty(right
);
2993 ret
= copy_for_split(trans
, root
, path
, l
, right
, slot
, mid
, nritems
);
2997 BUG_ON(num_doubles
!= 0);
3005 push_for_double_split(trans
, root
, path
, data_size
);
3006 tried_avoid_double
= 1;
3007 if (btrfs_leaf_free_space(root
, path
->nodes
[0]) >= data_size
)
3012 static noinline
int setup_leaf_for_split(struct btrfs_trans_handle
*trans
,
3013 struct btrfs_root
*root
,
3014 struct btrfs_path
*path
, int ins_len
)
3016 struct btrfs_key key
;
3017 struct extent_buffer
*leaf
;
3018 struct btrfs_file_extent_item
*fi
;
3023 leaf
= path
->nodes
[0];
3024 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3026 BUG_ON(key
.type
!= BTRFS_EXTENT_DATA_KEY
&&
3027 key
.type
!= BTRFS_EXTENT_CSUM_KEY
);
3029 if (btrfs_leaf_free_space(root
, leaf
) >= ins_len
)
3032 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
3033 if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
3034 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3035 struct btrfs_file_extent_item
);
3036 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
3038 btrfs_release_path(path
);
3040 path
->keep_locks
= 1;
3041 path
->search_for_split
= 1;
3042 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
3043 path
->search_for_split
= 0;
3048 leaf
= path
->nodes
[0];
3049 /* if our item isn't there or got smaller, return now */
3050 if (ret
> 0 || item_size
!= btrfs_item_size_nr(leaf
, path
->slots
[0]))
3053 /* the leaf has changed, it now has room. return now */
3054 if (btrfs_leaf_free_space(root
, path
->nodes
[0]) >= ins_len
)
3057 if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
3058 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3059 struct btrfs_file_extent_item
);
3060 if (extent_len
!= btrfs_file_extent_num_bytes(leaf
, fi
))
3064 btrfs_set_path_blocking(path
);
3065 ret
= split_leaf(trans
, root
, &key
, path
, ins_len
, 1);
3069 path
->keep_locks
= 0;
3070 btrfs_unlock_up_safe(path
, 1);
3073 path
->keep_locks
= 0;
3077 static noinline
int split_item(struct btrfs_trans_handle
*trans
,
3078 struct btrfs_root
*root
,
3079 struct btrfs_path
*path
,
3080 struct btrfs_key
*new_key
,
3081 unsigned long split_offset
)
3083 struct extent_buffer
*leaf
;
3084 struct btrfs_item
*item
;
3085 struct btrfs_item
*new_item
;
3091 struct btrfs_disk_key disk_key
;
3093 leaf
= path
->nodes
[0];
3094 BUG_ON(btrfs_leaf_free_space(root
, leaf
) < sizeof(struct btrfs_item
));
3096 btrfs_set_path_blocking(path
);
3098 item
= btrfs_item_nr(leaf
, path
->slots
[0]);
3099 orig_offset
= btrfs_item_offset(leaf
, item
);
3100 item_size
= btrfs_item_size(leaf
, item
);
3102 buf
= kmalloc(item_size
, GFP_NOFS
);
3106 read_extent_buffer(leaf
, buf
, btrfs_item_ptr_offset(leaf
,
3107 path
->slots
[0]), item_size
);
3109 slot
= path
->slots
[0] + 1;
3110 nritems
= btrfs_header_nritems(leaf
);
3111 if (slot
!= nritems
) {
3112 /* shift the items */
3113 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ 1),
3114 btrfs_item_nr_offset(slot
),
3115 (nritems
- slot
) * sizeof(struct btrfs_item
));
3118 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
3119 btrfs_set_item_key(leaf
, &disk_key
, slot
);
3121 new_item
= btrfs_item_nr(leaf
, slot
);
3123 btrfs_set_item_offset(leaf
, new_item
, orig_offset
);
3124 btrfs_set_item_size(leaf
, new_item
, item_size
- split_offset
);
3126 btrfs_set_item_offset(leaf
, item
,
3127 orig_offset
+ item_size
- split_offset
);
3128 btrfs_set_item_size(leaf
, item
, split_offset
);
3130 btrfs_set_header_nritems(leaf
, nritems
+ 1);
3132 /* write the data for the start of the original item */
3133 write_extent_buffer(leaf
, buf
,
3134 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
3137 /* write the data for the new item */
3138 write_extent_buffer(leaf
, buf
+ split_offset
,
3139 btrfs_item_ptr_offset(leaf
, slot
),
3140 item_size
- split_offset
);
3141 btrfs_mark_buffer_dirty(leaf
);
3143 BUG_ON(btrfs_leaf_free_space(root
, leaf
) < 0);
3149 * This function splits a single item into two items,
3150 * giving 'new_key' to the new item and splitting the
3151 * old one at split_offset (from the start of the item).
3153 * The path may be released by this operation. After
3154 * the split, the path is pointing to the old item. The
3155 * new item is going to be in the same node as the old one.
3157 * Note, the item being split must be smaller enough to live alone on
3158 * a tree block with room for one extra struct btrfs_item
3160 * This allows us to split the item in place, keeping a lock on the
3161 * leaf the entire time.
3163 int btrfs_split_item(struct btrfs_trans_handle
*trans
,
3164 struct btrfs_root
*root
,
3165 struct btrfs_path
*path
,
3166 struct btrfs_key
*new_key
,
3167 unsigned long split_offset
)
3170 ret
= setup_leaf_for_split(trans
, root
, path
,
3171 sizeof(struct btrfs_item
));
3175 ret
= split_item(trans
, root
, path
, new_key
, split_offset
);
3180 * This function duplicate a item, giving 'new_key' to the new item.
3181 * It guarantees both items live in the same tree leaf and the new item
3182 * is contiguous with the original item.
3184 * This allows us to split file extent in place, keeping a lock on the
3185 * leaf the entire time.
3187 int btrfs_duplicate_item(struct btrfs_trans_handle
*trans
,
3188 struct btrfs_root
*root
,
3189 struct btrfs_path
*path
,
3190 struct btrfs_key
*new_key
)
3192 struct extent_buffer
*leaf
;
3196 leaf
= path
->nodes
[0];
3197 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
3198 ret
= setup_leaf_for_split(trans
, root
, path
,
3199 item_size
+ sizeof(struct btrfs_item
));
3204 ret
= setup_items_for_insert(trans
, root
, path
, new_key
, &item_size
,
3205 item_size
, item_size
+
3206 sizeof(struct btrfs_item
), 1);
3209 leaf
= path
->nodes
[0];
3210 memcpy_extent_buffer(leaf
,
3211 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
3212 btrfs_item_ptr_offset(leaf
, path
->slots
[0] - 1),
3218 * make the item pointed to by the path smaller. new_size indicates
3219 * how small to make it, and from_end tells us if we just chop bytes
3220 * off the end of the item or if we shift the item to chop bytes off
3223 int btrfs_truncate_item(struct btrfs_trans_handle
*trans
,
3224 struct btrfs_root
*root
,
3225 struct btrfs_path
*path
,
3226 u32 new_size
, int from_end
)
3229 struct extent_buffer
*leaf
;
3230 struct btrfs_item
*item
;
3232 unsigned int data_end
;
3233 unsigned int old_data_start
;
3234 unsigned int old_size
;
3235 unsigned int size_diff
;
3238 leaf
= path
->nodes
[0];
3239 slot
= path
->slots
[0];
3241 old_size
= btrfs_item_size_nr(leaf
, slot
);
3242 if (old_size
== new_size
)
3245 nritems
= btrfs_header_nritems(leaf
);
3246 data_end
= leaf_data_end(root
, leaf
);
3248 old_data_start
= btrfs_item_offset_nr(leaf
, slot
);
3250 size_diff
= old_size
- new_size
;
3253 BUG_ON(slot
>= nritems
);
3256 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3258 /* first correct the data pointers */
3259 for (i
= slot
; i
< nritems
; i
++) {
3261 item
= btrfs_item_nr(leaf
, i
);
3263 if (!leaf
->map_token
) {
3264 map_extent_buffer(leaf
, (unsigned long)item
,
3265 sizeof(struct btrfs_item
),
3266 &leaf
->map_token
, &leaf
->kaddr
,
3267 &leaf
->map_start
, &leaf
->map_len
,
3271 ioff
= btrfs_item_offset(leaf
, item
);
3272 btrfs_set_item_offset(leaf
, item
, ioff
+ size_diff
);
3275 if (leaf
->map_token
) {
3276 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3277 leaf
->map_token
= NULL
;
3280 /* shift the data */
3282 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3283 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
3284 data_end
, old_data_start
+ new_size
- data_end
);
3286 struct btrfs_disk_key disk_key
;
3289 btrfs_item_key(leaf
, &disk_key
, slot
);
3291 if (btrfs_disk_key_type(&disk_key
) == BTRFS_EXTENT_DATA_KEY
) {
3293 struct btrfs_file_extent_item
*fi
;
3295 fi
= btrfs_item_ptr(leaf
, slot
,
3296 struct btrfs_file_extent_item
);
3297 fi
= (struct btrfs_file_extent_item
*)(
3298 (unsigned long)fi
- size_diff
);
3300 if (btrfs_file_extent_type(leaf
, fi
) ==
3301 BTRFS_FILE_EXTENT_INLINE
) {
3302 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
3303 memmove_extent_buffer(leaf
, ptr
,
3305 offsetof(struct btrfs_file_extent_item
,
3310 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3311 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
3312 data_end
, old_data_start
- data_end
);
3314 offset
= btrfs_disk_key_offset(&disk_key
);
3315 btrfs_set_disk_key_offset(&disk_key
, offset
+ size_diff
);
3316 btrfs_set_item_key(leaf
, &disk_key
, slot
);
3318 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
3321 item
= btrfs_item_nr(leaf
, slot
);
3322 btrfs_set_item_size(leaf
, item
, new_size
);
3323 btrfs_mark_buffer_dirty(leaf
);
3325 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3326 btrfs_print_leaf(root
, leaf
);
3333 * make the item pointed to by the path bigger, data_size is the new size.
3335 int btrfs_extend_item(struct btrfs_trans_handle
*trans
,
3336 struct btrfs_root
*root
, struct btrfs_path
*path
,
3340 struct extent_buffer
*leaf
;
3341 struct btrfs_item
*item
;
3343 unsigned int data_end
;
3344 unsigned int old_data
;
3345 unsigned int old_size
;
3348 leaf
= path
->nodes
[0];
3350 nritems
= btrfs_header_nritems(leaf
);
3351 data_end
= leaf_data_end(root
, leaf
);
3353 if (btrfs_leaf_free_space(root
, leaf
) < data_size
) {
3354 btrfs_print_leaf(root
, leaf
);
3357 slot
= path
->slots
[0];
3358 old_data
= btrfs_item_end_nr(leaf
, slot
);
3361 if (slot
>= nritems
) {
3362 btrfs_print_leaf(root
, leaf
);
3363 printk(KERN_CRIT
"slot %d too large, nritems %d\n",
3369 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3371 /* first correct the data pointers */
3372 for (i
= slot
; i
< nritems
; i
++) {
3374 item
= btrfs_item_nr(leaf
, i
);
3376 if (!leaf
->map_token
) {
3377 map_extent_buffer(leaf
, (unsigned long)item
,
3378 sizeof(struct btrfs_item
),
3379 &leaf
->map_token
, &leaf
->kaddr
,
3380 &leaf
->map_start
, &leaf
->map_len
,
3383 ioff
= btrfs_item_offset(leaf
, item
);
3384 btrfs_set_item_offset(leaf
, item
, ioff
- data_size
);
3387 if (leaf
->map_token
) {
3388 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3389 leaf
->map_token
= NULL
;
3392 /* shift the data */
3393 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3394 data_end
- data_size
, btrfs_leaf_data(leaf
) +
3395 data_end
, old_data
- data_end
);
3397 data_end
= old_data
;
3398 old_size
= btrfs_item_size_nr(leaf
, slot
);
3399 item
= btrfs_item_nr(leaf
, slot
);
3400 btrfs_set_item_size(leaf
, item
, old_size
+ data_size
);
3401 btrfs_mark_buffer_dirty(leaf
);
3403 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3404 btrfs_print_leaf(root
, leaf
);
3411 * Given a key and some data, insert items into the tree.
3412 * This does all the path init required, making room in the tree if needed.
3413 * Returns the number of keys that were inserted.
3415 int btrfs_insert_some_items(struct btrfs_trans_handle
*trans
,
3416 struct btrfs_root
*root
,
3417 struct btrfs_path
*path
,
3418 struct btrfs_key
*cpu_key
, u32
*data_size
,
3421 struct extent_buffer
*leaf
;
3422 struct btrfs_item
*item
;
3429 unsigned int data_end
;
3430 struct btrfs_disk_key disk_key
;
3431 struct btrfs_key found_key
;
3433 for (i
= 0; i
< nr
; i
++) {
3434 if (total_size
+ data_size
[i
] + sizeof(struct btrfs_item
) >
3435 BTRFS_LEAF_DATA_SIZE(root
)) {
3439 total_data
+= data_size
[i
];
3440 total_size
+= data_size
[i
] + sizeof(struct btrfs_item
);
3444 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
3450 leaf
= path
->nodes
[0];
3452 nritems
= btrfs_header_nritems(leaf
);
3453 data_end
= leaf_data_end(root
, leaf
);
3455 if (btrfs_leaf_free_space(root
, leaf
) < total_size
) {
3456 for (i
= nr
; i
>= 0; i
--) {
3457 total_data
-= data_size
[i
];
3458 total_size
-= data_size
[i
] + sizeof(struct btrfs_item
);
3459 if (total_size
< btrfs_leaf_free_space(root
, leaf
))
3465 slot
= path
->slots
[0];
3468 if (slot
!= nritems
) {
3469 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
3471 item
= btrfs_item_nr(leaf
, slot
);
3472 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3474 /* figure out how many keys we can insert in here */
3475 total_data
= data_size
[0];
3476 for (i
= 1; i
< nr
; i
++) {
3477 if (btrfs_comp_cpu_keys(&found_key
, cpu_key
+ i
) <= 0)
3479 total_data
+= data_size
[i
];
3483 if (old_data
< data_end
) {
3484 btrfs_print_leaf(root
, leaf
);
3485 printk(KERN_CRIT
"slot %d old_data %d data_end %d\n",
3486 slot
, old_data
, data_end
);
3490 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3492 /* first correct the data pointers */
3493 WARN_ON(leaf
->map_token
);
3494 for (i
= slot
; i
< nritems
; i
++) {
3497 item
= btrfs_item_nr(leaf
, i
);
3498 if (!leaf
->map_token
) {
3499 map_extent_buffer(leaf
, (unsigned long)item
,
3500 sizeof(struct btrfs_item
),
3501 &leaf
->map_token
, &leaf
->kaddr
,
3502 &leaf
->map_start
, &leaf
->map_len
,
3506 ioff
= btrfs_item_offset(leaf
, item
);
3507 btrfs_set_item_offset(leaf
, item
, ioff
- total_data
);
3509 if (leaf
->map_token
) {
3510 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3511 leaf
->map_token
= NULL
;
3514 /* shift the items */
3515 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
3516 btrfs_item_nr_offset(slot
),
3517 (nritems
- slot
) * sizeof(struct btrfs_item
));
3519 /* shift the data */
3520 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3521 data_end
- total_data
, btrfs_leaf_data(leaf
) +
3522 data_end
, old_data
- data_end
);
3523 data_end
= old_data
;
3526 * this sucks but it has to be done, if we are inserting at
3527 * the end of the leaf only insert 1 of the items, since we
3528 * have no way of knowing whats on the next leaf and we'd have
3529 * to drop our current locks to figure it out
3534 /* setup the item for the new data */
3535 for (i
= 0; i
< nr
; i
++) {
3536 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
3537 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
3538 item
= btrfs_item_nr(leaf
, slot
+ i
);
3539 btrfs_set_item_offset(leaf
, item
, data_end
- data_size
[i
]);
3540 data_end
-= data_size
[i
];
3541 btrfs_set_item_size(leaf
, item
, data_size
[i
]);
3543 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
3544 btrfs_mark_buffer_dirty(leaf
);
3548 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
3549 ret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
3552 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3553 btrfs_print_leaf(root
, leaf
);
3563 * this is a helper for btrfs_insert_empty_items, the main goal here is
3564 * to save stack depth by doing the bulk of the work in a function
3565 * that doesn't call btrfs_search_slot
3567 int setup_items_for_insert(struct btrfs_trans_handle
*trans
,
3568 struct btrfs_root
*root
, struct btrfs_path
*path
,
3569 struct btrfs_key
*cpu_key
, u32
*data_size
,
3570 u32 total_data
, u32 total_size
, int nr
)
3572 struct btrfs_item
*item
;
3575 unsigned int data_end
;
3576 struct btrfs_disk_key disk_key
;
3578 struct extent_buffer
*leaf
;
3581 leaf
= path
->nodes
[0];
3582 slot
= path
->slots
[0];
3584 nritems
= btrfs_header_nritems(leaf
);
3585 data_end
= leaf_data_end(root
, leaf
);
3587 if (btrfs_leaf_free_space(root
, leaf
) < total_size
) {
3588 btrfs_print_leaf(root
, leaf
);
3589 printk(KERN_CRIT
"not enough freespace need %u have %d\n",
3590 total_size
, btrfs_leaf_free_space(root
, leaf
));
3594 if (slot
!= nritems
) {
3595 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
3597 if (old_data
< data_end
) {
3598 btrfs_print_leaf(root
, leaf
);
3599 printk(KERN_CRIT
"slot %d old_data %d data_end %d\n",
3600 slot
, old_data
, data_end
);
3604 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3606 /* first correct the data pointers */
3607 WARN_ON(leaf
->map_token
);
3608 for (i
= slot
; i
< nritems
; i
++) {
3611 item
= btrfs_item_nr(leaf
, i
);
3612 if (!leaf
->map_token
) {
3613 map_extent_buffer(leaf
, (unsigned long)item
,
3614 sizeof(struct btrfs_item
),
3615 &leaf
->map_token
, &leaf
->kaddr
,
3616 &leaf
->map_start
, &leaf
->map_len
,
3620 ioff
= btrfs_item_offset(leaf
, item
);
3621 btrfs_set_item_offset(leaf
, item
, ioff
- total_data
);
3623 if (leaf
->map_token
) {
3624 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3625 leaf
->map_token
= NULL
;
3628 /* shift the items */
3629 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
3630 btrfs_item_nr_offset(slot
),
3631 (nritems
- slot
) * sizeof(struct btrfs_item
));
3633 /* shift the data */
3634 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3635 data_end
- total_data
, btrfs_leaf_data(leaf
) +
3636 data_end
, old_data
- data_end
);
3637 data_end
= old_data
;
3640 /* setup the item for the new data */
3641 for (i
= 0; i
< nr
; i
++) {
3642 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
3643 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
3644 item
= btrfs_item_nr(leaf
, slot
+ i
);
3645 btrfs_set_item_offset(leaf
, item
, data_end
- data_size
[i
]);
3646 data_end
-= data_size
[i
];
3647 btrfs_set_item_size(leaf
, item
, data_size
[i
]);
3650 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
3654 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
3655 ret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
3657 btrfs_unlock_up_safe(path
, 1);
3658 btrfs_mark_buffer_dirty(leaf
);
3660 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3661 btrfs_print_leaf(root
, leaf
);
3668 * Given a key and some data, insert items into the tree.
3669 * This does all the path init required, making room in the tree if needed.
3671 int btrfs_insert_empty_items(struct btrfs_trans_handle
*trans
,
3672 struct btrfs_root
*root
,
3673 struct btrfs_path
*path
,
3674 struct btrfs_key
*cpu_key
, u32
*data_size
,
3683 for (i
= 0; i
< nr
; i
++)
3684 total_data
+= data_size
[i
];
3686 total_size
= total_data
+ (nr
* sizeof(struct btrfs_item
));
3687 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
3693 slot
= path
->slots
[0];
3696 ret
= setup_items_for_insert(trans
, root
, path
, cpu_key
, data_size
,
3697 total_data
, total_size
, nr
);
3704 * Given a key and some data, insert an item into the tree.
3705 * This does all the path init required, making room in the tree if needed.
3707 int btrfs_insert_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
3708 *root
, struct btrfs_key
*cpu_key
, void *data
, u32
3712 struct btrfs_path
*path
;
3713 struct extent_buffer
*leaf
;
3716 path
= btrfs_alloc_path();
3719 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
3721 leaf
= path
->nodes
[0];
3722 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
3723 write_extent_buffer(leaf
, data
, ptr
, data_size
);
3724 btrfs_mark_buffer_dirty(leaf
);
3726 btrfs_free_path(path
);
3731 * delete the pointer from a given node.
3733 * the tree should have been previously balanced so the deletion does not
3736 static int del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
3737 struct btrfs_path
*path
, int level
, int slot
)
3739 struct extent_buffer
*parent
= path
->nodes
[level
];
3744 nritems
= btrfs_header_nritems(parent
);
3745 if (slot
!= nritems
- 1) {
3746 memmove_extent_buffer(parent
,
3747 btrfs_node_key_ptr_offset(slot
),
3748 btrfs_node_key_ptr_offset(slot
+ 1),
3749 sizeof(struct btrfs_key_ptr
) *
3750 (nritems
- slot
- 1));
3753 btrfs_set_header_nritems(parent
, nritems
);
3754 if (nritems
== 0 && parent
== root
->node
) {
3755 BUG_ON(btrfs_header_level(root
->node
) != 1);
3756 /* just turn the root into a leaf and break */
3757 btrfs_set_header_level(root
->node
, 0);
3758 } else if (slot
== 0) {
3759 struct btrfs_disk_key disk_key
;
3761 btrfs_node_key(parent
, &disk_key
, 0);
3762 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, level
+ 1);
3766 btrfs_mark_buffer_dirty(parent
);
3771 * a helper function to delete the leaf pointed to by path->slots[1] and
3774 * This deletes the pointer in path->nodes[1] and frees the leaf
3775 * block extent. zero is returned if it all worked out, < 0 otherwise.
3777 * The path must have already been setup for deleting the leaf, including
3778 * all the proper balancing. path->nodes[1] must be locked.
3780 static noinline
int btrfs_del_leaf(struct btrfs_trans_handle
*trans
,
3781 struct btrfs_root
*root
,
3782 struct btrfs_path
*path
,
3783 struct extent_buffer
*leaf
)
3787 WARN_ON(btrfs_header_generation(leaf
) != trans
->transid
);
3788 ret
= del_ptr(trans
, root
, path
, 1, path
->slots
[1]);
3793 * btrfs_free_extent is expensive, we want to make sure we
3794 * aren't holding any locks when we call it
3796 btrfs_unlock_up_safe(path
, 0);
3798 root_sub_used(root
, leaf
->len
);
3800 btrfs_free_tree_block(trans
, root
, leaf
, 0, 1);
3804 * delete the item at the leaf level in path. If that empties
3805 * the leaf, remove it from the tree
3807 int btrfs_del_items(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
3808 struct btrfs_path
*path
, int slot
, int nr
)
3810 struct extent_buffer
*leaf
;
3811 struct btrfs_item
*item
;
3819 leaf
= path
->nodes
[0];
3820 last_off
= btrfs_item_offset_nr(leaf
, slot
+ nr
- 1);
3822 for (i
= 0; i
< nr
; i
++)
3823 dsize
+= btrfs_item_size_nr(leaf
, slot
+ i
);
3825 nritems
= btrfs_header_nritems(leaf
);
3827 if (slot
+ nr
!= nritems
) {
3828 int data_end
= leaf_data_end(root
, leaf
);
3830 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3832 btrfs_leaf_data(leaf
) + data_end
,
3833 last_off
- data_end
);
3835 for (i
= slot
+ nr
; i
< nritems
; i
++) {
3838 item
= btrfs_item_nr(leaf
, i
);
3839 if (!leaf
->map_token
) {
3840 map_extent_buffer(leaf
, (unsigned long)item
,
3841 sizeof(struct btrfs_item
),
3842 &leaf
->map_token
, &leaf
->kaddr
,
3843 &leaf
->map_start
, &leaf
->map_len
,
3846 ioff
= btrfs_item_offset(leaf
, item
);
3847 btrfs_set_item_offset(leaf
, item
, ioff
+ dsize
);
3850 if (leaf
->map_token
) {
3851 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3852 leaf
->map_token
= NULL
;
3855 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
),
3856 btrfs_item_nr_offset(slot
+ nr
),
3857 sizeof(struct btrfs_item
) *
3858 (nritems
- slot
- nr
));
3860 btrfs_set_header_nritems(leaf
, nritems
- nr
);
3863 /* delete the leaf if we've emptied it */
3865 if (leaf
== root
->node
) {
3866 btrfs_set_header_level(leaf
, 0);
3868 btrfs_set_path_blocking(path
);
3869 clean_tree_block(trans
, root
, leaf
);
3870 ret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
3874 int used
= leaf_space_used(leaf
, 0, nritems
);
3876 struct btrfs_disk_key disk_key
;
3878 btrfs_item_key(leaf
, &disk_key
, 0);
3879 wret
= fixup_low_keys(trans
, root
, path
,
3885 /* delete the leaf if it is mostly empty */
3886 if (used
< BTRFS_LEAF_DATA_SIZE(root
) / 3) {
3887 /* push_leaf_left fixes the path.
3888 * make sure the path still points to our leaf
3889 * for possible call to del_ptr below
3891 slot
= path
->slots
[1];
3892 extent_buffer_get(leaf
);
3894 btrfs_set_path_blocking(path
);
3895 wret
= push_leaf_left(trans
, root
, path
, 1, 1,
3897 if (wret
< 0 && wret
!= -ENOSPC
)
3900 if (path
->nodes
[0] == leaf
&&
3901 btrfs_header_nritems(leaf
)) {
3902 wret
= push_leaf_right(trans
, root
, path
, 1,
3904 if (wret
< 0 && wret
!= -ENOSPC
)
3908 if (btrfs_header_nritems(leaf
) == 0) {
3909 path
->slots
[1] = slot
;
3910 ret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
3912 free_extent_buffer(leaf
);
3914 /* if we're still in the path, make sure
3915 * we're dirty. Otherwise, one of the
3916 * push_leaf functions must have already
3917 * dirtied this buffer
3919 if (path
->nodes
[0] == leaf
)
3920 btrfs_mark_buffer_dirty(leaf
);
3921 free_extent_buffer(leaf
);
3924 btrfs_mark_buffer_dirty(leaf
);
3931 * search the tree again to find a leaf with lesser keys
3932 * returns 0 if it found something or 1 if there are no lesser leaves.
3933 * returns < 0 on io errors.
3935 * This may release the path, and so you may lose any locks held at the
3938 int btrfs_prev_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
3940 struct btrfs_key key
;
3941 struct btrfs_disk_key found_key
;
3944 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, 0);
3948 else if (key
.type
> 0)
3950 else if (key
.objectid
> 0)
3955 btrfs_release_path(path
);
3956 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3959 btrfs_item_key(path
->nodes
[0], &found_key
, 0);
3960 ret
= comp_keys(&found_key
, &key
);
3967 * A helper function to walk down the tree starting at min_key, and looking
3968 * for nodes or leaves that are either in cache or have a minimum
3969 * transaction id. This is used by the btree defrag code, and tree logging
3971 * This does not cow, but it does stuff the starting key it finds back
3972 * into min_key, so you can call btrfs_search_slot with cow=1 on the
3973 * key and get a writable path.
3975 * This does lock as it descends, and path->keep_locks should be set
3976 * to 1 by the caller.
3978 * This honors path->lowest_level to prevent descent past a given level
3981 * min_trans indicates the oldest transaction that you are interested
3982 * in walking through. Any nodes or leaves older than min_trans are
3983 * skipped over (without reading them).
3985 * returns zero if something useful was found, < 0 on error and 1 if there
3986 * was nothing in the tree that matched the search criteria.
3988 int btrfs_search_forward(struct btrfs_root
*root
, struct btrfs_key
*min_key
,
3989 struct btrfs_key
*max_key
,
3990 struct btrfs_path
*path
, int cache_only
,
3993 struct extent_buffer
*cur
;
3994 struct btrfs_key found_key
;
4001 WARN_ON(!path
->keep_locks
);
4003 cur
= btrfs_lock_root_node(root
);
4004 level
= btrfs_header_level(cur
);
4005 WARN_ON(path
->nodes
[level
]);
4006 path
->nodes
[level
] = cur
;
4007 path
->locks
[level
] = 1;
4009 if (btrfs_header_generation(cur
) < min_trans
) {
4014 nritems
= btrfs_header_nritems(cur
);
4015 level
= btrfs_header_level(cur
);
4016 sret
= bin_search(cur
, min_key
, level
, &slot
);
4018 /* at the lowest level, we're done, setup the path and exit */
4019 if (level
== path
->lowest_level
) {
4020 if (slot
>= nritems
)
4023 path
->slots
[level
] = slot
;
4024 btrfs_item_key_to_cpu(cur
, &found_key
, slot
);
4027 if (sret
&& slot
> 0)
4030 * check this node pointer against the cache_only and
4031 * min_trans parameters. If it isn't in cache or is too
4032 * old, skip to the next one.
4034 while (slot
< nritems
) {
4037 struct extent_buffer
*tmp
;
4038 struct btrfs_disk_key disk_key
;
4040 blockptr
= btrfs_node_blockptr(cur
, slot
);
4041 gen
= btrfs_node_ptr_generation(cur
, slot
);
4042 if (gen
< min_trans
) {
4050 btrfs_node_key(cur
, &disk_key
, slot
);
4051 if (comp_keys(&disk_key
, max_key
) >= 0) {
4057 tmp
= btrfs_find_tree_block(root
, blockptr
,
4058 btrfs_level_size(root
, level
- 1));
4060 if (tmp
&& btrfs_buffer_uptodate(tmp
, gen
)) {
4061 free_extent_buffer(tmp
);
4065 free_extent_buffer(tmp
);
4070 * we didn't find a candidate key in this node, walk forward
4071 * and find another one
4073 if (slot
>= nritems
) {
4074 path
->slots
[level
] = slot
;
4075 btrfs_set_path_blocking(path
);
4076 sret
= btrfs_find_next_key(root
, path
, min_key
, level
,
4077 cache_only
, min_trans
);
4079 btrfs_release_path(path
);
4085 /* save our key for returning back */
4086 btrfs_node_key_to_cpu(cur
, &found_key
, slot
);
4087 path
->slots
[level
] = slot
;
4088 if (level
== path
->lowest_level
) {
4090 unlock_up(path
, level
, 1);
4093 btrfs_set_path_blocking(path
);
4094 cur
= read_node_slot(root
, cur
, slot
);
4097 btrfs_tree_lock(cur
);
4099 path
->locks
[level
- 1] = 1;
4100 path
->nodes
[level
- 1] = cur
;
4101 unlock_up(path
, level
, 1);
4102 btrfs_clear_path_blocking(path
, NULL
);
4106 memcpy(min_key
, &found_key
, sizeof(found_key
));
4107 btrfs_set_path_blocking(path
);
4112 * this is similar to btrfs_next_leaf, but does not try to preserve
4113 * and fixup the path. It looks for and returns the next key in the
4114 * tree based on the current path and the cache_only and min_trans
4117 * 0 is returned if another key is found, < 0 if there are any errors
4118 * and 1 is returned if there are no higher keys in the tree
4120 * path->keep_locks should be set to 1 on the search made before
4121 * calling this function.
4123 int btrfs_find_next_key(struct btrfs_root
*root
, struct btrfs_path
*path
,
4124 struct btrfs_key
*key
, int level
,
4125 int cache_only
, u64 min_trans
)
4128 struct extent_buffer
*c
;
4130 WARN_ON(!path
->keep_locks
);
4131 while (level
< BTRFS_MAX_LEVEL
) {
4132 if (!path
->nodes
[level
])
4135 slot
= path
->slots
[level
] + 1;
4136 c
= path
->nodes
[level
];
4138 if (slot
>= btrfs_header_nritems(c
)) {
4141 struct btrfs_key cur_key
;
4142 if (level
+ 1 >= BTRFS_MAX_LEVEL
||
4143 !path
->nodes
[level
+ 1])
4146 if (path
->locks
[level
+ 1]) {
4151 slot
= btrfs_header_nritems(c
) - 1;
4153 btrfs_item_key_to_cpu(c
, &cur_key
, slot
);
4155 btrfs_node_key_to_cpu(c
, &cur_key
, slot
);
4157 orig_lowest
= path
->lowest_level
;
4158 btrfs_release_path(path
);
4159 path
->lowest_level
= level
;
4160 ret
= btrfs_search_slot(NULL
, root
, &cur_key
, path
,
4162 path
->lowest_level
= orig_lowest
;
4166 c
= path
->nodes
[level
];
4167 slot
= path
->slots
[level
];
4174 btrfs_item_key_to_cpu(c
, key
, slot
);
4176 u64 blockptr
= btrfs_node_blockptr(c
, slot
);
4177 u64 gen
= btrfs_node_ptr_generation(c
, slot
);
4180 struct extent_buffer
*cur
;
4181 cur
= btrfs_find_tree_block(root
, blockptr
,
4182 btrfs_level_size(root
, level
- 1));
4183 if (!cur
|| !btrfs_buffer_uptodate(cur
, gen
)) {
4186 free_extent_buffer(cur
);
4189 free_extent_buffer(cur
);
4191 if (gen
< min_trans
) {
4195 btrfs_node_key_to_cpu(c
, key
, slot
);
4203 * search the tree again to find a leaf with greater keys
4204 * returns 0 if it found something or 1 if there are no greater leaves.
4205 * returns < 0 on io errors.
4207 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
4211 struct extent_buffer
*c
;
4212 struct extent_buffer
*next
;
4213 struct btrfs_key key
;
4216 int old_spinning
= path
->leave_spinning
;
4217 int force_blocking
= 0;
4219 nritems
= btrfs_header_nritems(path
->nodes
[0]);
4224 * we take the blocks in an order that upsets lockdep. Using
4225 * blocking mode is the only way around it.
4227 #ifdef CONFIG_DEBUG_LOCK_ALLOC
4231 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, nritems
- 1);
4235 btrfs_release_path(path
);
4237 path
->keep_locks
= 1;
4239 if (!force_blocking
)
4240 path
->leave_spinning
= 1;
4242 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4243 path
->keep_locks
= 0;
4248 nritems
= btrfs_header_nritems(path
->nodes
[0]);
4250 * by releasing the path above we dropped all our locks. A balance
4251 * could have added more items next to the key that used to be
4252 * at the very end of the block. So, check again here and
4253 * advance the path if there are now more items available.
4255 if (nritems
> 0 && path
->slots
[0] < nritems
- 1) {
4262 while (level
< BTRFS_MAX_LEVEL
) {
4263 if (!path
->nodes
[level
]) {
4268 slot
= path
->slots
[level
] + 1;
4269 c
= path
->nodes
[level
];
4270 if (slot
>= btrfs_header_nritems(c
)) {
4272 if (level
== BTRFS_MAX_LEVEL
) {
4280 btrfs_tree_unlock(next
);
4281 free_extent_buffer(next
);
4285 ret
= read_block_for_search(NULL
, root
, path
, &next
, level
,
4291 btrfs_release_path(path
);
4295 if (!path
->skip_locking
) {
4296 ret
= btrfs_try_spin_lock(next
);
4298 btrfs_set_path_blocking(path
);
4299 btrfs_tree_lock(next
);
4300 if (!force_blocking
)
4301 btrfs_clear_path_blocking(path
, next
);
4304 btrfs_set_lock_blocking(next
);
4308 path
->slots
[level
] = slot
;
4311 c
= path
->nodes
[level
];
4312 if (path
->locks
[level
])
4313 btrfs_tree_unlock(c
);
4315 free_extent_buffer(c
);
4316 path
->nodes
[level
] = next
;
4317 path
->slots
[level
] = 0;
4318 if (!path
->skip_locking
)
4319 path
->locks
[level
] = 1;
4324 ret
= read_block_for_search(NULL
, root
, path
, &next
, level
,
4330 btrfs_release_path(path
);
4334 if (!path
->skip_locking
) {
4335 btrfs_assert_tree_locked(path
->nodes
[level
]);
4336 ret
= btrfs_try_spin_lock(next
);
4338 btrfs_set_path_blocking(path
);
4339 btrfs_tree_lock(next
);
4340 if (!force_blocking
)
4341 btrfs_clear_path_blocking(path
, next
);
4344 btrfs_set_lock_blocking(next
);
4349 unlock_up(path
, 0, 1);
4350 path
->leave_spinning
= old_spinning
;
4352 btrfs_set_path_blocking(path
);
4358 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4359 * searching until it gets past min_objectid or finds an item of 'type'
4361 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4363 int btrfs_previous_item(struct btrfs_root
*root
,
4364 struct btrfs_path
*path
, u64 min_objectid
,
4367 struct btrfs_key found_key
;
4368 struct extent_buffer
*leaf
;
4373 if (path
->slots
[0] == 0) {
4374 btrfs_set_path_blocking(path
);
4375 ret
= btrfs_prev_leaf(root
, path
);
4381 leaf
= path
->nodes
[0];
4382 nritems
= btrfs_header_nritems(leaf
);
4385 if (path
->slots
[0] == nritems
)
4388 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4389 if (found_key
.objectid
< min_objectid
)
4391 if (found_key
.type
== type
)
4393 if (found_key
.objectid
== min_objectid
&&
4394 found_key
.type
< type
)