2 * Copyright (C) 2007,2008 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
20 #include <linux/slab.h>
23 #include "transaction.h"
24 #include "print-tree.h"
27 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
28 *root
, struct btrfs_path
*path
, int level
);
29 static int split_leaf(struct btrfs_trans_handle
*trans
, struct btrfs_root
30 *root
, struct btrfs_key
*ins_key
,
31 struct btrfs_path
*path
, int data_size
, int extend
);
32 static int push_node_left(struct btrfs_trans_handle
*trans
,
33 struct btrfs_root
*root
, struct extent_buffer
*dst
,
34 struct extent_buffer
*src
, int empty
);
35 static int balance_node_right(struct btrfs_trans_handle
*trans
,
36 struct btrfs_root
*root
,
37 struct extent_buffer
*dst_buf
,
38 struct extent_buffer
*src_buf
);
39 static int del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
40 struct btrfs_path
*path
, int level
, int slot
);
41 static int setup_items_for_insert(struct btrfs_trans_handle
*trans
,
42 struct btrfs_root
*root
, struct btrfs_path
*path
,
43 struct btrfs_key
*cpu_key
, u32
*data_size
,
44 u32 total_data
, u32 total_size
, int nr
);
47 struct btrfs_path
*btrfs_alloc_path(void)
49 struct btrfs_path
*path
;
50 path
= kmem_cache_zalloc(btrfs_path_cachep
, GFP_NOFS
);
57 * set all locked nodes in the path to blocking locks. This should
58 * be done before scheduling
60 noinline
void btrfs_set_path_blocking(struct btrfs_path
*p
)
63 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
64 if (p
->nodes
[i
] && p
->locks
[i
])
65 btrfs_set_lock_blocking(p
->nodes
[i
]);
70 * reset all the locked nodes in the patch to spinning locks.
72 * held is used to keep lockdep happy, when lockdep is enabled
73 * we set held to a blocking lock before we go around and
74 * retake all the spinlocks in the path. You can safely use NULL
77 noinline
void btrfs_clear_path_blocking(struct btrfs_path
*p
,
78 struct extent_buffer
*held
)
82 #ifdef CONFIG_DEBUG_LOCK_ALLOC
83 /* lockdep really cares that we take all of these spinlocks
84 * in the right order. If any of the locks in the path are not
85 * currently blocking, it is going to complain. So, make really
86 * really sure by forcing the path to blocking before we clear
90 btrfs_set_lock_blocking(held
);
91 btrfs_set_path_blocking(p
);
94 for (i
= BTRFS_MAX_LEVEL
- 1; i
>= 0; i
--) {
95 if (p
->nodes
[i
] && p
->locks
[i
])
96 btrfs_clear_lock_blocking(p
->nodes
[i
]);
99 #ifdef CONFIG_DEBUG_LOCK_ALLOC
101 btrfs_clear_lock_blocking(held
);
105 /* this also releases the path */
106 void btrfs_free_path(struct btrfs_path
*p
)
108 btrfs_release_path(NULL
, p
);
109 kmem_cache_free(btrfs_path_cachep
, p
);
113 * path release drops references on the extent buffers in the path
114 * and it drops any locks held by this path
116 * It is safe to call this on paths that no locks or extent buffers held.
118 noinline
void btrfs_release_path(struct btrfs_root
*root
, struct btrfs_path
*p
)
122 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
127 btrfs_tree_unlock(p
->nodes
[i
]);
130 free_extent_buffer(p
->nodes
[i
]);
136 * safely gets a reference on the root node of a tree. A lock
137 * is not taken, so a concurrent writer may put a different node
138 * at the root of the tree. See btrfs_lock_root_node for the
141 * The extent buffer returned by this has a reference taken, so
142 * it won't disappear. It may stop being the root of the tree
143 * at any time because there are no locks held.
145 struct extent_buffer
*btrfs_root_node(struct btrfs_root
*root
)
147 struct extent_buffer
*eb
;
148 spin_lock(&root
->node_lock
);
150 extent_buffer_get(eb
);
151 spin_unlock(&root
->node_lock
);
155 /* loop around taking references on and locking the root node of the
156 * tree until you end up with a lock on the root. A locked buffer
157 * is returned, with a reference held.
159 struct extent_buffer
*btrfs_lock_root_node(struct btrfs_root
*root
)
161 struct extent_buffer
*eb
;
164 eb
= btrfs_root_node(root
);
167 spin_lock(&root
->node_lock
);
168 if (eb
== root
->node
) {
169 spin_unlock(&root
->node_lock
);
172 spin_unlock(&root
->node_lock
);
174 btrfs_tree_unlock(eb
);
175 free_extent_buffer(eb
);
180 /* cowonly root (everything not a reference counted cow subvolume), just get
181 * put onto a simple dirty list. transaction.c walks this to make sure they
182 * get properly updated on disk.
184 static void add_root_to_dirty_list(struct btrfs_root
*root
)
186 if (root
->track_dirty
&& list_empty(&root
->dirty_list
)) {
187 list_add(&root
->dirty_list
,
188 &root
->fs_info
->dirty_cowonly_roots
);
193 * used by snapshot creation to make a copy of a root for a tree with
194 * a given objectid. The buffer with the new root node is returned in
195 * cow_ret, and this func returns zero on success or a negative error code.
197 int btrfs_copy_root(struct btrfs_trans_handle
*trans
,
198 struct btrfs_root
*root
,
199 struct extent_buffer
*buf
,
200 struct extent_buffer
**cow_ret
, u64 new_root_objectid
)
202 struct extent_buffer
*cow
;
206 struct btrfs_disk_key disk_key
;
208 WARN_ON(root
->ref_cows
&& trans
->transid
!=
209 root
->fs_info
->running_transaction
->transid
);
210 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
212 level
= btrfs_header_level(buf
);
213 nritems
= btrfs_header_nritems(buf
);
215 btrfs_item_key(buf
, &disk_key
, 0);
217 btrfs_node_key(buf
, &disk_key
, 0);
219 cow
= btrfs_alloc_free_block(trans
, root
, buf
->len
, 0,
220 new_root_objectid
, &disk_key
, level
,
225 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
226 btrfs_set_header_bytenr(cow
, cow
->start
);
227 btrfs_set_header_generation(cow
, trans
->transid
);
228 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
229 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
230 BTRFS_HEADER_FLAG_RELOC
);
231 if (new_root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
232 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
234 btrfs_set_header_owner(cow
, new_root_objectid
);
236 write_extent_buffer(cow
, root
->fs_info
->fsid
,
237 (unsigned long)btrfs_header_fsid(cow
),
240 WARN_ON(btrfs_header_generation(buf
) > trans
->transid
);
241 if (new_root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
242 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
244 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
249 btrfs_mark_buffer_dirty(cow
);
255 * check if the tree block can be shared by multiple trees
257 int btrfs_block_can_be_shared(struct btrfs_root
*root
,
258 struct extent_buffer
*buf
)
261 * Tree blocks not in refernece counted trees and tree roots
262 * are never shared. If a block was allocated after the last
263 * snapshot and the block was not allocated by tree relocation,
264 * we know the block is not shared.
266 if (root
->ref_cows
&&
267 buf
!= root
->node
&& buf
!= root
->commit_root
&&
268 (btrfs_header_generation(buf
) <=
269 btrfs_root_last_snapshot(&root
->root_item
) ||
270 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)))
272 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
273 if (root
->ref_cows
&&
274 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
280 static noinline
int update_ref_for_cow(struct btrfs_trans_handle
*trans
,
281 struct btrfs_root
*root
,
282 struct extent_buffer
*buf
,
283 struct extent_buffer
*cow
,
293 * Backrefs update rules:
295 * Always use full backrefs for extent pointers in tree block
296 * allocated by tree relocation.
298 * If a shared tree block is no longer referenced by its owner
299 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
300 * use full backrefs for extent pointers in tree block.
302 * If a tree block is been relocating
303 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
304 * use full backrefs for extent pointers in tree block.
305 * The reason for this is some operations (such as drop tree)
306 * are only allowed for blocks use full backrefs.
309 if (btrfs_block_can_be_shared(root
, buf
)) {
310 ret
= btrfs_lookup_extent_info(trans
, root
, buf
->start
,
311 buf
->len
, &refs
, &flags
);
316 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
||
317 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
318 flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
323 owner
= btrfs_header_owner(buf
);
324 BUG_ON(owner
== BTRFS_TREE_RELOC_OBJECTID
&&
325 !(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
));
328 if ((owner
== root
->root_key
.objectid
||
329 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) &&
330 !(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
)) {
331 ret
= btrfs_inc_ref(trans
, root
, buf
, 1);
334 if (root
->root_key
.objectid
==
335 BTRFS_TREE_RELOC_OBJECTID
) {
336 ret
= btrfs_dec_ref(trans
, root
, buf
, 0);
338 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
341 new_flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
344 if (root
->root_key
.objectid
==
345 BTRFS_TREE_RELOC_OBJECTID
)
346 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
348 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
351 if (new_flags
!= 0) {
352 ret
= btrfs_set_disk_extent_flags(trans
, root
,
359 if (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
360 if (root
->root_key
.objectid
==
361 BTRFS_TREE_RELOC_OBJECTID
)
362 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
364 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
366 ret
= btrfs_dec_ref(trans
, root
, buf
, 1);
369 clean_tree_block(trans
, root
, buf
);
376 * does the dirty work in cow of a single block. The parent block (if
377 * supplied) is updated to point to the new cow copy. The new buffer is marked
378 * dirty and returned locked. If you modify the block it needs to be marked
381 * search_start -- an allocation hint for the new block
383 * empty_size -- a hint that you plan on doing more cow. This is the size in
384 * bytes the allocator should try to find free next to the block it returns.
385 * This is just a hint and may be ignored by the allocator.
387 static noinline
int __btrfs_cow_block(struct btrfs_trans_handle
*trans
,
388 struct btrfs_root
*root
,
389 struct extent_buffer
*buf
,
390 struct extent_buffer
*parent
, int parent_slot
,
391 struct extent_buffer
**cow_ret
,
392 u64 search_start
, u64 empty_size
)
394 struct btrfs_disk_key disk_key
;
395 struct extent_buffer
*cow
;
404 btrfs_assert_tree_locked(buf
);
406 WARN_ON(root
->ref_cows
&& trans
->transid
!=
407 root
->fs_info
->running_transaction
->transid
);
408 WARN_ON(root
->ref_cows
&& trans
->transid
!= root
->last_trans
);
410 level
= btrfs_header_level(buf
);
413 btrfs_item_key(buf
, &disk_key
, 0);
415 btrfs_node_key(buf
, &disk_key
, 0);
417 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) {
419 parent_start
= parent
->start
;
425 cow
= btrfs_alloc_free_block(trans
, root
, buf
->len
, parent_start
,
426 root
->root_key
.objectid
, &disk_key
,
427 level
, search_start
, empty_size
);
431 /* cow is set to blocking by btrfs_init_new_buffer */
433 copy_extent_buffer(cow
, buf
, 0, 0, cow
->len
);
434 btrfs_set_header_bytenr(cow
, cow
->start
);
435 btrfs_set_header_generation(cow
, trans
->transid
);
436 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
437 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
438 BTRFS_HEADER_FLAG_RELOC
);
439 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
440 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
442 btrfs_set_header_owner(cow
, root
->root_key
.objectid
);
444 write_extent_buffer(cow
, root
->fs_info
->fsid
,
445 (unsigned long)btrfs_header_fsid(cow
),
448 update_ref_for_cow(trans
, root
, buf
, cow
, &last_ref
);
451 btrfs_reloc_cow_block(trans
, root
, buf
, cow
);
453 if (buf
== root
->node
) {
454 WARN_ON(parent
&& parent
!= buf
);
455 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
||
456 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
457 parent_start
= buf
->start
;
461 spin_lock(&root
->node_lock
);
463 extent_buffer_get(cow
);
464 spin_unlock(&root
->node_lock
);
466 btrfs_free_tree_block(trans
, root
, buf
, parent_start
,
468 free_extent_buffer(buf
);
469 add_root_to_dirty_list(root
);
471 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
472 parent_start
= parent
->start
;
476 WARN_ON(trans
->transid
!= btrfs_header_generation(parent
));
477 btrfs_set_node_blockptr(parent
, parent_slot
,
479 btrfs_set_node_ptr_generation(parent
, parent_slot
,
481 btrfs_mark_buffer_dirty(parent
);
482 btrfs_free_tree_block(trans
, root
, buf
, parent_start
,
486 btrfs_tree_unlock(buf
);
487 free_extent_buffer(buf
);
488 btrfs_mark_buffer_dirty(cow
);
493 static inline int should_cow_block(struct btrfs_trans_handle
*trans
,
494 struct btrfs_root
*root
,
495 struct extent_buffer
*buf
)
497 if (btrfs_header_generation(buf
) == trans
->transid
&&
498 !btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
) &&
499 !(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
&&
500 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)))
506 * cows a single block, see __btrfs_cow_block for the real work.
507 * This version of it has extra checks so that a block isn't cow'd more than
508 * once per transaction, as long as it hasn't been written yet
510 noinline
int btrfs_cow_block(struct btrfs_trans_handle
*trans
,
511 struct btrfs_root
*root
, struct extent_buffer
*buf
,
512 struct extent_buffer
*parent
, int parent_slot
,
513 struct extent_buffer
**cow_ret
)
518 if (trans
->transaction
!= root
->fs_info
->running_transaction
) {
519 printk(KERN_CRIT
"trans %llu running %llu\n",
520 (unsigned long long)trans
->transid
,
522 root
->fs_info
->running_transaction
->transid
);
525 if (trans
->transid
!= root
->fs_info
->generation
) {
526 printk(KERN_CRIT
"trans %llu running %llu\n",
527 (unsigned long long)trans
->transid
,
528 (unsigned long long)root
->fs_info
->generation
);
532 if (!should_cow_block(trans
, root
, buf
)) {
537 search_start
= buf
->start
& ~((u64
)(1024 * 1024 * 1024) - 1);
540 btrfs_set_lock_blocking(parent
);
541 btrfs_set_lock_blocking(buf
);
543 ret
= __btrfs_cow_block(trans
, root
, buf
, parent
,
544 parent_slot
, cow_ret
, search_start
, 0);
549 * helper function for defrag to decide if two blocks pointed to by a
550 * node are actually close by
552 static int close_blocks(u64 blocknr
, u64 other
, u32 blocksize
)
554 if (blocknr
< other
&& other
- (blocknr
+ blocksize
) < 32768)
556 if (blocknr
> other
&& blocknr
- (other
+ blocksize
) < 32768)
562 * compare two keys in a memcmp fashion
564 static int comp_keys(struct btrfs_disk_key
*disk
, struct btrfs_key
*k2
)
568 btrfs_disk_key_to_cpu(&k1
, disk
);
570 return btrfs_comp_cpu_keys(&k1
, k2
);
574 * same as comp_keys only with two btrfs_key's
576 int btrfs_comp_cpu_keys(struct btrfs_key
*k1
, struct btrfs_key
*k2
)
578 if (k1
->objectid
> k2
->objectid
)
580 if (k1
->objectid
< k2
->objectid
)
582 if (k1
->type
> k2
->type
)
584 if (k1
->type
< k2
->type
)
586 if (k1
->offset
> k2
->offset
)
588 if (k1
->offset
< k2
->offset
)
594 * this is used by the defrag code to go through all the
595 * leaves pointed to by a node and reallocate them so that
596 * disk order is close to key order
598 int btrfs_realloc_node(struct btrfs_trans_handle
*trans
,
599 struct btrfs_root
*root
, struct extent_buffer
*parent
,
600 int start_slot
, int cache_only
, u64
*last_ret
,
601 struct btrfs_key
*progress
)
603 struct extent_buffer
*cur
;
606 u64 search_start
= *last_ret
;
616 int progress_passed
= 0;
617 struct btrfs_disk_key disk_key
;
619 parent_level
= btrfs_header_level(parent
);
620 if (cache_only
&& parent_level
!= 1)
623 if (trans
->transaction
!= root
->fs_info
->running_transaction
)
625 if (trans
->transid
!= root
->fs_info
->generation
)
628 parent_nritems
= btrfs_header_nritems(parent
);
629 blocksize
= btrfs_level_size(root
, parent_level
- 1);
630 end_slot
= parent_nritems
;
632 if (parent_nritems
== 1)
635 btrfs_set_lock_blocking(parent
);
637 for (i
= start_slot
; i
< end_slot
; i
++) {
640 if (!parent
->map_token
) {
641 map_extent_buffer(parent
,
642 btrfs_node_key_ptr_offset(i
),
643 sizeof(struct btrfs_key_ptr
),
644 &parent
->map_token
, &parent
->kaddr
,
645 &parent
->map_start
, &parent
->map_len
,
648 btrfs_node_key(parent
, &disk_key
, i
);
649 if (!progress_passed
&& comp_keys(&disk_key
, progress
) < 0)
653 blocknr
= btrfs_node_blockptr(parent
, i
);
654 gen
= btrfs_node_ptr_generation(parent
, i
);
656 last_block
= blocknr
;
659 other
= btrfs_node_blockptr(parent
, i
- 1);
660 close
= close_blocks(blocknr
, other
, blocksize
);
662 if (!close
&& i
< end_slot
- 2) {
663 other
= btrfs_node_blockptr(parent
, i
+ 1);
664 close
= close_blocks(blocknr
, other
, blocksize
);
667 last_block
= blocknr
;
670 if (parent
->map_token
) {
671 unmap_extent_buffer(parent
, parent
->map_token
,
673 parent
->map_token
= NULL
;
676 cur
= btrfs_find_tree_block(root
, blocknr
, blocksize
);
678 uptodate
= btrfs_buffer_uptodate(cur
, gen
);
681 if (!cur
|| !uptodate
) {
683 free_extent_buffer(cur
);
687 cur
= read_tree_block(root
, blocknr
,
689 } else if (!uptodate
) {
690 btrfs_read_buffer(cur
, gen
);
693 if (search_start
== 0)
694 search_start
= last_block
;
696 btrfs_tree_lock(cur
);
697 btrfs_set_lock_blocking(cur
);
698 err
= __btrfs_cow_block(trans
, root
, cur
, parent
, i
,
701 (end_slot
- i
) * blocksize
));
703 btrfs_tree_unlock(cur
);
704 free_extent_buffer(cur
);
707 search_start
= cur
->start
;
708 last_block
= cur
->start
;
709 *last_ret
= search_start
;
710 btrfs_tree_unlock(cur
);
711 free_extent_buffer(cur
);
713 if (parent
->map_token
) {
714 unmap_extent_buffer(parent
, parent
->map_token
,
716 parent
->map_token
= NULL
;
722 * The leaf data grows from end-to-front in the node.
723 * this returns the address of the start of the last item,
724 * which is the stop of the leaf data stack
726 static inline unsigned int leaf_data_end(struct btrfs_root
*root
,
727 struct extent_buffer
*leaf
)
729 u32 nr
= btrfs_header_nritems(leaf
);
731 return BTRFS_LEAF_DATA_SIZE(root
);
732 return btrfs_item_offset_nr(leaf
, nr
- 1);
736 * extra debugging checks to make sure all the items in a key are
737 * well formed and in the proper order
739 static int check_node(struct btrfs_root
*root
, struct btrfs_path
*path
,
742 struct extent_buffer
*parent
= NULL
;
743 struct extent_buffer
*node
= path
->nodes
[level
];
744 struct btrfs_disk_key parent_key
;
745 struct btrfs_disk_key node_key
;
748 struct btrfs_key cpukey
;
749 u32 nritems
= btrfs_header_nritems(node
);
751 if (path
->nodes
[level
+ 1])
752 parent
= path
->nodes
[level
+ 1];
754 slot
= path
->slots
[level
];
755 BUG_ON(nritems
== 0);
757 parent_slot
= path
->slots
[level
+ 1];
758 btrfs_node_key(parent
, &parent_key
, parent_slot
);
759 btrfs_node_key(node
, &node_key
, 0);
760 BUG_ON(memcmp(&parent_key
, &node_key
,
761 sizeof(struct btrfs_disk_key
)));
762 BUG_ON(btrfs_node_blockptr(parent
, parent_slot
) !=
763 btrfs_header_bytenr(node
));
765 BUG_ON(nritems
> BTRFS_NODEPTRS_PER_BLOCK(root
));
767 btrfs_node_key_to_cpu(node
, &cpukey
, slot
- 1);
768 btrfs_node_key(node
, &node_key
, slot
);
769 BUG_ON(comp_keys(&node_key
, &cpukey
) <= 0);
771 if (slot
< nritems
- 1) {
772 btrfs_node_key_to_cpu(node
, &cpukey
, slot
+ 1);
773 btrfs_node_key(node
, &node_key
, slot
);
774 BUG_ON(comp_keys(&node_key
, &cpukey
) >= 0);
780 * extra checking to make sure all the items in a leaf are
781 * well formed and in the proper order
783 static int check_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
,
786 struct extent_buffer
*leaf
= path
->nodes
[level
];
787 struct extent_buffer
*parent
= NULL
;
789 struct btrfs_key cpukey
;
790 struct btrfs_disk_key parent_key
;
791 struct btrfs_disk_key leaf_key
;
792 int slot
= path
->slots
[0];
794 u32 nritems
= btrfs_header_nritems(leaf
);
796 if (path
->nodes
[level
+ 1])
797 parent
= path
->nodes
[level
+ 1];
803 parent_slot
= path
->slots
[level
+ 1];
804 btrfs_node_key(parent
, &parent_key
, parent_slot
);
805 btrfs_item_key(leaf
, &leaf_key
, 0);
807 BUG_ON(memcmp(&parent_key
, &leaf_key
,
808 sizeof(struct btrfs_disk_key
)));
809 BUG_ON(btrfs_node_blockptr(parent
, parent_slot
) !=
810 btrfs_header_bytenr(leaf
));
812 if (slot
!= 0 && slot
< nritems
- 1) {
813 btrfs_item_key(leaf
, &leaf_key
, slot
);
814 btrfs_item_key_to_cpu(leaf
, &cpukey
, slot
- 1);
815 if (comp_keys(&leaf_key
, &cpukey
) <= 0) {
816 btrfs_print_leaf(root
, leaf
);
817 printk(KERN_CRIT
"slot %d offset bad key\n", slot
);
820 if (btrfs_item_offset_nr(leaf
, slot
- 1) !=
821 btrfs_item_end_nr(leaf
, slot
)) {
822 btrfs_print_leaf(root
, leaf
);
823 printk(KERN_CRIT
"slot %d offset bad\n", slot
);
827 if (slot
< nritems
- 1) {
828 btrfs_item_key(leaf
, &leaf_key
, slot
);
829 btrfs_item_key_to_cpu(leaf
, &cpukey
, slot
+ 1);
830 BUG_ON(comp_keys(&leaf_key
, &cpukey
) >= 0);
831 if (btrfs_item_offset_nr(leaf
, slot
) !=
832 btrfs_item_end_nr(leaf
, slot
+ 1)) {
833 btrfs_print_leaf(root
, leaf
);
834 printk(KERN_CRIT
"slot %d offset bad\n", slot
);
838 BUG_ON(btrfs_item_offset_nr(leaf
, 0) +
839 btrfs_item_size_nr(leaf
, 0) != BTRFS_LEAF_DATA_SIZE(root
));
843 static noinline
int check_block(struct btrfs_root
*root
,
844 struct btrfs_path
*path
, int level
)
848 return check_leaf(root
, path
, level
);
849 return check_node(root
, path
, level
);
853 * search for key in the extent_buffer. The items start at offset p,
854 * and they are item_size apart. There are 'max' items in p.
856 * the slot in the array is returned via slot, and it points to
857 * the place where you would insert key if it is not found in
860 * slot may point to max if the key is bigger than all of the keys
862 static noinline
int generic_bin_search(struct extent_buffer
*eb
,
864 int item_size
, struct btrfs_key
*key
,
871 struct btrfs_disk_key
*tmp
= NULL
;
872 struct btrfs_disk_key unaligned
;
873 unsigned long offset
;
874 char *map_token
= NULL
;
876 unsigned long map_start
= 0;
877 unsigned long map_len
= 0;
881 mid
= (low
+ high
) / 2;
882 offset
= p
+ mid
* item_size
;
884 if (!map_token
|| offset
< map_start
||
885 (offset
+ sizeof(struct btrfs_disk_key
)) >
886 map_start
+ map_len
) {
888 unmap_extent_buffer(eb
, map_token
, KM_USER0
);
892 err
= map_private_extent_buffer(eb
, offset
,
893 sizeof(struct btrfs_disk_key
),
895 &map_start
, &map_len
, KM_USER0
);
898 tmp
= (struct btrfs_disk_key
*)(kaddr
+ offset
-
901 read_extent_buffer(eb
, &unaligned
,
902 offset
, sizeof(unaligned
));
907 tmp
= (struct btrfs_disk_key
*)(kaddr
+ offset
-
910 ret
= comp_keys(tmp
, key
);
919 unmap_extent_buffer(eb
, map_token
, KM_USER0
);
925 unmap_extent_buffer(eb
, map_token
, KM_USER0
);
930 * simple bin_search frontend that does the right thing for
933 static int bin_search(struct extent_buffer
*eb
, struct btrfs_key
*key
,
934 int level
, int *slot
)
937 return generic_bin_search(eb
,
938 offsetof(struct btrfs_leaf
, items
),
939 sizeof(struct btrfs_item
),
940 key
, btrfs_header_nritems(eb
),
943 return generic_bin_search(eb
,
944 offsetof(struct btrfs_node
, ptrs
),
945 sizeof(struct btrfs_key_ptr
),
946 key
, btrfs_header_nritems(eb
),
952 int btrfs_bin_search(struct extent_buffer
*eb
, struct btrfs_key
*key
,
953 int level
, int *slot
)
955 return bin_search(eb
, key
, level
, slot
);
958 static void root_add_used(struct btrfs_root
*root
, u32 size
)
960 spin_lock(&root
->accounting_lock
);
961 btrfs_set_root_used(&root
->root_item
,
962 btrfs_root_used(&root
->root_item
) + size
);
963 spin_unlock(&root
->accounting_lock
);
966 static void root_sub_used(struct btrfs_root
*root
, u32 size
)
968 spin_lock(&root
->accounting_lock
);
969 btrfs_set_root_used(&root
->root_item
,
970 btrfs_root_used(&root
->root_item
) - size
);
971 spin_unlock(&root
->accounting_lock
);
974 /* given a node and slot number, this reads the blocks it points to. The
975 * extent buffer is returned with a reference taken (but unlocked).
976 * NULL is returned on error.
978 static noinline
struct extent_buffer
*read_node_slot(struct btrfs_root
*root
,
979 struct extent_buffer
*parent
, int slot
)
981 int level
= btrfs_header_level(parent
);
984 if (slot
>= btrfs_header_nritems(parent
))
989 return read_tree_block(root
, btrfs_node_blockptr(parent
, slot
),
990 btrfs_level_size(root
, level
- 1),
991 btrfs_node_ptr_generation(parent
, slot
));
995 * node level balancing, used to make sure nodes are in proper order for
996 * item deletion. We balance from the top down, so we have to make sure
997 * that a deletion won't leave an node completely empty later on.
999 static noinline
int balance_level(struct btrfs_trans_handle
*trans
,
1000 struct btrfs_root
*root
,
1001 struct btrfs_path
*path
, int level
)
1003 struct extent_buffer
*right
= NULL
;
1004 struct extent_buffer
*mid
;
1005 struct extent_buffer
*left
= NULL
;
1006 struct extent_buffer
*parent
= NULL
;
1010 int orig_slot
= path
->slots
[level
];
1011 int err_on_enospc
= 0;
1017 mid
= path
->nodes
[level
];
1019 WARN_ON(!path
->locks
[level
]);
1020 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
1022 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
1024 if (level
< BTRFS_MAX_LEVEL
- 1)
1025 parent
= path
->nodes
[level
+ 1];
1026 pslot
= path
->slots
[level
+ 1];
1029 * deal with the case where there is only one pointer in the root
1030 * by promoting the node below to a root
1033 struct extent_buffer
*child
;
1035 if (btrfs_header_nritems(mid
) != 1)
1038 /* promote the child to a root */
1039 child
= read_node_slot(root
, mid
, 0);
1041 btrfs_tree_lock(child
);
1042 btrfs_set_lock_blocking(child
);
1043 ret
= btrfs_cow_block(trans
, root
, child
, mid
, 0, &child
);
1045 btrfs_tree_unlock(child
);
1046 free_extent_buffer(child
);
1050 spin_lock(&root
->node_lock
);
1052 spin_unlock(&root
->node_lock
);
1054 add_root_to_dirty_list(root
);
1055 btrfs_tree_unlock(child
);
1057 path
->locks
[level
] = 0;
1058 path
->nodes
[level
] = NULL
;
1059 clean_tree_block(trans
, root
, mid
);
1060 btrfs_tree_unlock(mid
);
1061 /* once for the path */
1062 free_extent_buffer(mid
);
1064 root_sub_used(root
, mid
->len
);
1065 btrfs_free_tree_block(trans
, root
, mid
, 0, 1);
1066 /* once for the root ptr */
1067 free_extent_buffer(mid
);
1070 if (btrfs_header_nritems(mid
) >
1071 BTRFS_NODEPTRS_PER_BLOCK(root
) / 4)
1074 if (btrfs_header_nritems(mid
) < 2)
1077 left
= read_node_slot(root
, parent
, pslot
- 1);
1079 btrfs_tree_lock(left
);
1080 btrfs_set_lock_blocking(left
);
1081 wret
= btrfs_cow_block(trans
, root
, left
,
1082 parent
, pslot
- 1, &left
);
1088 right
= read_node_slot(root
, parent
, pslot
+ 1);
1090 btrfs_tree_lock(right
);
1091 btrfs_set_lock_blocking(right
);
1092 wret
= btrfs_cow_block(trans
, root
, right
,
1093 parent
, pslot
+ 1, &right
);
1100 /* first, try to make some room in the middle buffer */
1102 orig_slot
+= btrfs_header_nritems(left
);
1103 wret
= push_node_left(trans
, root
, left
, mid
, 1);
1106 if (btrfs_header_nritems(mid
) < 2)
1111 * then try to empty the right most buffer into the middle
1114 wret
= push_node_left(trans
, root
, mid
, right
, 1);
1115 if (wret
< 0 && wret
!= -ENOSPC
)
1117 if (btrfs_header_nritems(right
) == 0) {
1118 clean_tree_block(trans
, root
, right
);
1119 btrfs_tree_unlock(right
);
1120 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
+
1124 root_sub_used(root
, right
->len
);
1125 btrfs_free_tree_block(trans
, root
, right
, 0, 1);
1126 free_extent_buffer(right
);
1129 struct btrfs_disk_key right_key
;
1130 btrfs_node_key(right
, &right_key
, 0);
1131 btrfs_set_node_key(parent
, &right_key
, pslot
+ 1);
1132 btrfs_mark_buffer_dirty(parent
);
1135 if (btrfs_header_nritems(mid
) == 1) {
1137 * we're not allowed to leave a node with one item in the
1138 * tree during a delete. A deletion from lower in the tree
1139 * could try to delete the only pointer in this node.
1140 * So, pull some keys from the left.
1141 * There has to be a left pointer at this point because
1142 * otherwise we would have pulled some pointers from the
1146 wret
= balance_node_right(trans
, root
, mid
, left
);
1152 wret
= push_node_left(trans
, root
, left
, mid
, 1);
1158 if (btrfs_header_nritems(mid
) == 0) {
1159 clean_tree_block(trans
, root
, mid
);
1160 btrfs_tree_unlock(mid
);
1161 wret
= del_ptr(trans
, root
, path
, level
+ 1, pslot
);
1164 root_sub_used(root
, mid
->len
);
1165 btrfs_free_tree_block(trans
, root
, mid
, 0, 1);
1166 free_extent_buffer(mid
);
1169 /* update the parent key to reflect our changes */
1170 struct btrfs_disk_key mid_key
;
1171 btrfs_node_key(mid
, &mid_key
, 0);
1172 btrfs_set_node_key(parent
, &mid_key
, pslot
);
1173 btrfs_mark_buffer_dirty(parent
);
1176 /* update the path */
1178 if (btrfs_header_nritems(left
) > orig_slot
) {
1179 extent_buffer_get(left
);
1180 /* left was locked after cow */
1181 path
->nodes
[level
] = left
;
1182 path
->slots
[level
+ 1] -= 1;
1183 path
->slots
[level
] = orig_slot
;
1185 btrfs_tree_unlock(mid
);
1186 free_extent_buffer(mid
);
1189 orig_slot
-= btrfs_header_nritems(left
);
1190 path
->slots
[level
] = orig_slot
;
1193 /* double check we haven't messed things up */
1194 check_block(root
, path
, level
);
1196 btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]))
1200 btrfs_tree_unlock(right
);
1201 free_extent_buffer(right
);
1204 if (path
->nodes
[level
] != left
)
1205 btrfs_tree_unlock(left
);
1206 free_extent_buffer(left
);
1211 /* Node balancing for insertion. Here we only split or push nodes around
1212 * when they are completely full. This is also done top down, so we
1213 * have to be pessimistic.
1215 static noinline
int push_nodes_for_insert(struct btrfs_trans_handle
*trans
,
1216 struct btrfs_root
*root
,
1217 struct btrfs_path
*path
, int level
)
1219 struct extent_buffer
*right
= NULL
;
1220 struct extent_buffer
*mid
;
1221 struct extent_buffer
*left
= NULL
;
1222 struct extent_buffer
*parent
= NULL
;
1226 int orig_slot
= path
->slots
[level
];
1232 mid
= path
->nodes
[level
];
1233 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
1234 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
1236 if (level
< BTRFS_MAX_LEVEL
- 1)
1237 parent
= path
->nodes
[level
+ 1];
1238 pslot
= path
->slots
[level
+ 1];
1243 left
= read_node_slot(root
, parent
, pslot
- 1);
1245 /* first, try to make some room in the middle buffer */
1249 btrfs_tree_lock(left
);
1250 btrfs_set_lock_blocking(left
);
1252 left_nr
= btrfs_header_nritems(left
);
1253 if (left_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
1256 ret
= btrfs_cow_block(trans
, root
, left
, parent
,
1261 wret
= push_node_left(trans
, root
,
1268 struct btrfs_disk_key disk_key
;
1269 orig_slot
+= left_nr
;
1270 btrfs_node_key(mid
, &disk_key
, 0);
1271 btrfs_set_node_key(parent
, &disk_key
, pslot
);
1272 btrfs_mark_buffer_dirty(parent
);
1273 if (btrfs_header_nritems(left
) > orig_slot
) {
1274 path
->nodes
[level
] = left
;
1275 path
->slots
[level
+ 1] -= 1;
1276 path
->slots
[level
] = orig_slot
;
1277 btrfs_tree_unlock(mid
);
1278 free_extent_buffer(mid
);
1281 btrfs_header_nritems(left
);
1282 path
->slots
[level
] = orig_slot
;
1283 btrfs_tree_unlock(left
);
1284 free_extent_buffer(left
);
1288 btrfs_tree_unlock(left
);
1289 free_extent_buffer(left
);
1291 right
= read_node_slot(root
, parent
, pslot
+ 1);
1294 * then try to empty the right most buffer into the middle
1299 btrfs_tree_lock(right
);
1300 btrfs_set_lock_blocking(right
);
1302 right_nr
= btrfs_header_nritems(right
);
1303 if (right_nr
>= BTRFS_NODEPTRS_PER_BLOCK(root
) - 1) {
1306 ret
= btrfs_cow_block(trans
, root
, right
,
1312 wret
= balance_node_right(trans
, root
,
1319 struct btrfs_disk_key disk_key
;
1321 btrfs_node_key(right
, &disk_key
, 0);
1322 btrfs_set_node_key(parent
, &disk_key
, pslot
+ 1);
1323 btrfs_mark_buffer_dirty(parent
);
1325 if (btrfs_header_nritems(mid
) <= orig_slot
) {
1326 path
->nodes
[level
] = right
;
1327 path
->slots
[level
+ 1] += 1;
1328 path
->slots
[level
] = orig_slot
-
1329 btrfs_header_nritems(mid
);
1330 btrfs_tree_unlock(mid
);
1331 free_extent_buffer(mid
);
1333 btrfs_tree_unlock(right
);
1334 free_extent_buffer(right
);
1338 btrfs_tree_unlock(right
);
1339 free_extent_buffer(right
);
1345 * readahead one full node of leaves, finding things that are close
1346 * to the block in 'slot', and triggering ra on them.
1348 static void reada_for_search(struct btrfs_root
*root
,
1349 struct btrfs_path
*path
,
1350 int level
, int slot
, u64 objectid
)
1352 struct extent_buffer
*node
;
1353 struct btrfs_disk_key disk_key
;
1358 int direction
= path
->reada
;
1359 struct extent_buffer
*eb
;
1367 if (!path
->nodes
[level
])
1370 node
= path
->nodes
[level
];
1372 search
= btrfs_node_blockptr(node
, slot
);
1373 blocksize
= btrfs_level_size(root
, level
- 1);
1374 eb
= btrfs_find_tree_block(root
, search
, blocksize
);
1376 free_extent_buffer(eb
);
1382 nritems
= btrfs_header_nritems(node
);
1385 if (direction
< 0) {
1389 } else if (direction
> 0) {
1394 if (path
->reada
< 0 && objectid
) {
1395 btrfs_node_key(node
, &disk_key
, nr
);
1396 if (btrfs_disk_key_objectid(&disk_key
) != objectid
)
1399 search
= btrfs_node_blockptr(node
, nr
);
1400 if ((search
<= target
&& target
- search
<= 65536) ||
1401 (search
> target
&& search
- target
<= 65536)) {
1402 readahead_tree_block(root
, search
, blocksize
,
1403 btrfs_node_ptr_generation(node
, nr
));
1407 if ((nread
> 65536 || nscan
> 32))
1413 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1416 static noinline
int reada_for_balance(struct btrfs_root
*root
,
1417 struct btrfs_path
*path
, int level
)
1421 struct extent_buffer
*parent
;
1422 struct extent_buffer
*eb
;
1429 parent
= path
->nodes
[level
+ 1];
1433 nritems
= btrfs_header_nritems(parent
);
1434 slot
= path
->slots
[level
+ 1];
1435 blocksize
= btrfs_level_size(root
, level
);
1438 block1
= btrfs_node_blockptr(parent
, slot
- 1);
1439 gen
= btrfs_node_ptr_generation(parent
, slot
- 1);
1440 eb
= btrfs_find_tree_block(root
, block1
, blocksize
);
1441 if (eb
&& btrfs_buffer_uptodate(eb
, gen
))
1443 free_extent_buffer(eb
);
1445 if (slot
+ 1 < nritems
) {
1446 block2
= btrfs_node_blockptr(parent
, slot
+ 1);
1447 gen
= btrfs_node_ptr_generation(parent
, slot
+ 1);
1448 eb
= btrfs_find_tree_block(root
, block2
, blocksize
);
1449 if (eb
&& btrfs_buffer_uptodate(eb
, gen
))
1451 free_extent_buffer(eb
);
1453 if (block1
|| block2
) {
1456 /* release the whole path */
1457 btrfs_release_path(root
, path
);
1459 /* read the blocks */
1461 readahead_tree_block(root
, block1
, blocksize
, 0);
1463 readahead_tree_block(root
, block2
, blocksize
, 0);
1466 eb
= read_tree_block(root
, block1
, blocksize
, 0);
1467 free_extent_buffer(eb
);
1470 eb
= read_tree_block(root
, block2
, blocksize
, 0);
1471 free_extent_buffer(eb
);
1479 * when we walk down the tree, it is usually safe to unlock the higher layers
1480 * in the tree. The exceptions are when our path goes through slot 0, because
1481 * operations on the tree might require changing key pointers higher up in the
1484 * callers might also have set path->keep_locks, which tells this code to keep
1485 * the lock if the path points to the last slot in the block. This is part of
1486 * walking through the tree, and selecting the next slot in the higher block.
1488 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1489 * if lowest_unlock is 1, level 0 won't be unlocked
1491 static noinline
void unlock_up(struct btrfs_path
*path
, int level
,
1495 int skip_level
= level
;
1497 struct extent_buffer
*t
;
1499 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1500 if (!path
->nodes
[i
])
1502 if (!path
->locks
[i
])
1504 if (!no_skips
&& path
->slots
[i
] == 0) {
1508 if (!no_skips
&& path
->keep_locks
) {
1511 nritems
= btrfs_header_nritems(t
);
1512 if (nritems
< 1 || path
->slots
[i
] >= nritems
- 1) {
1517 if (skip_level
< i
&& i
>= lowest_unlock
)
1521 if (i
>= lowest_unlock
&& i
> skip_level
&& path
->locks
[i
]) {
1522 btrfs_tree_unlock(t
);
1529 * This releases any locks held in the path starting at level and
1530 * going all the way up to the root.
1532 * btrfs_search_slot will keep the lock held on higher nodes in a few
1533 * corner cases, such as COW of the block at slot zero in the node. This
1534 * ignores those rules, and it should only be called when there are no
1535 * more updates to be done higher up in the tree.
1537 noinline
void btrfs_unlock_up_safe(struct btrfs_path
*path
, int level
)
1541 if (path
->keep_locks
)
1544 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1545 if (!path
->nodes
[i
])
1547 if (!path
->locks
[i
])
1549 btrfs_tree_unlock(path
->nodes
[i
]);
1555 * helper function for btrfs_search_slot. The goal is to find a block
1556 * in cache without setting the path to blocking. If we find the block
1557 * we return zero and the path is unchanged.
1559 * If we can't find the block, we set the path blocking and do some
1560 * reada. -EAGAIN is returned and the search must be repeated.
1563 read_block_for_search(struct btrfs_trans_handle
*trans
,
1564 struct btrfs_root
*root
, struct btrfs_path
*p
,
1565 struct extent_buffer
**eb_ret
, int level
, int slot
,
1566 struct btrfs_key
*key
)
1571 struct extent_buffer
*b
= *eb_ret
;
1572 struct extent_buffer
*tmp
;
1575 blocknr
= btrfs_node_blockptr(b
, slot
);
1576 gen
= btrfs_node_ptr_generation(b
, slot
);
1577 blocksize
= btrfs_level_size(root
, level
- 1);
1579 tmp
= btrfs_find_tree_block(root
, blocknr
, blocksize
);
1581 if (btrfs_buffer_uptodate(tmp
, 0)) {
1582 if (btrfs_buffer_uptodate(tmp
, gen
)) {
1584 * we found an up to date block without
1591 /* the pages were up to date, but we failed
1592 * the generation number check. Do a full
1593 * read for the generation number that is correct.
1594 * We must do this without dropping locks so
1595 * we can trust our generation number
1597 free_extent_buffer(tmp
);
1598 tmp
= read_tree_block(root
, blocknr
, blocksize
, gen
);
1599 if (tmp
&& btrfs_buffer_uptodate(tmp
, gen
)) {
1603 free_extent_buffer(tmp
);
1604 btrfs_release_path(NULL
, p
);
1610 * reduce lock contention at high levels
1611 * of the btree by dropping locks before
1612 * we read. Don't release the lock on the current
1613 * level because we need to walk this node to figure
1614 * out which blocks to read.
1616 btrfs_unlock_up_safe(p
, level
+ 1);
1617 btrfs_set_path_blocking(p
);
1619 free_extent_buffer(tmp
);
1621 reada_for_search(root
, p
, level
, slot
, key
->objectid
);
1623 btrfs_release_path(NULL
, p
);
1626 tmp
= read_tree_block(root
, blocknr
, blocksize
, 0);
1629 * If the read above didn't mark this buffer up to date,
1630 * it will never end up being up to date. Set ret to EIO now
1631 * and give up so that our caller doesn't loop forever
1634 if (!btrfs_buffer_uptodate(tmp
, 0))
1636 free_extent_buffer(tmp
);
1642 * helper function for btrfs_search_slot. This does all of the checks
1643 * for node-level blocks and does any balancing required based on
1646 * If no extra work was required, zero is returned. If we had to
1647 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1651 setup_nodes_for_search(struct btrfs_trans_handle
*trans
,
1652 struct btrfs_root
*root
, struct btrfs_path
*p
,
1653 struct extent_buffer
*b
, int level
, int ins_len
)
1656 if ((p
->search_for_split
|| ins_len
> 0) && btrfs_header_nritems(b
) >=
1657 BTRFS_NODEPTRS_PER_BLOCK(root
) - 3) {
1660 sret
= reada_for_balance(root
, p
, level
);
1664 btrfs_set_path_blocking(p
);
1665 sret
= split_node(trans
, root
, p
, level
);
1666 btrfs_clear_path_blocking(p
, NULL
);
1673 b
= p
->nodes
[level
];
1674 } else if (ins_len
< 0 && btrfs_header_nritems(b
) <
1675 BTRFS_NODEPTRS_PER_BLOCK(root
) / 2) {
1678 sret
= reada_for_balance(root
, p
, level
);
1682 btrfs_set_path_blocking(p
);
1683 sret
= balance_level(trans
, root
, p
, level
);
1684 btrfs_clear_path_blocking(p
, NULL
);
1690 b
= p
->nodes
[level
];
1692 btrfs_release_path(NULL
, p
);
1695 BUG_ON(btrfs_header_nritems(b
) == 1);
1706 * look for key in the tree. path is filled in with nodes along the way
1707 * if key is found, we return zero and you can find the item in the leaf
1708 * level of the path (level 0)
1710 * If the key isn't found, the path points to the slot where it should
1711 * be inserted, and 1 is returned. If there are other errors during the
1712 * search a negative error number is returned.
1714 * if ins_len > 0, nodes and leaves will be split as we walk down the
1715 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
1718 int btrfs_search_slot(struct btrfs_trans_handle
*trans
, struct btrfs_root
1719 *root
, struct btrfs_key
*key
, struct btrfs_path
*p
, int
1722 struct extent_buffer
*b
;
1727 int lowest_unlock
= 1;
1728 u8 lowest_level
= 0;
1730 lowest_level
= p
->lowest_level
;
1731 WARN_ON(lowest_level
&& ins_len
> 0);
1732 WARN_ON(p
->nodes
[0] != NULL
);
1738 if (p
->search_commit_root
) {
1739 b
= root
->commit_root
;
1740 extent_buffer_get(b
);
1741 if (!p
->skip_locking
)
1744 if (p
->skip_locking
)
1745 b
= btrfs_root_node(root
);
1747 b
= btrfs_lock_root_node(root
);
1751 level
= btrfs_header_level(b
);
1754 * setup the path here so we can release it under lock
1755 * contention with the cow code
1757 p
->nodes
[level
] = b
;
1758 if (!p
->skip_locking
)
1759 p
->locks
[level
] = 1;
1763 * if we don't really need to cow this block
1764 * then we don't want to set the path blocking,
1765 * so we test it here
1767 if (!should_cow_block(trans
, root
, b
))
1770 btrfs_set_path_blocking(p
);
1772 err
= btrfs_cow_block(trans
, root
, b
,
1773 p
->nodes
[level
+ 1],
1774 p
->slots
[level
+ 1], &b
);
1781 BUG_ON(!cow
&& ins_len
);
1782 if (level
!= btrfs_header_level(b
))
1784 level
= btrfs_header_level(b
);
1786 p
->nodes
[level
] = b
;
1787 if (!p
->skip_locking
)
1788 p
->locks
[level
] = 1;
1790 btrfs_clear_path_blocking(p
, NULL
);
1793 * we have a lock on b and as long as we aren't changing
1794 * the tree, there is no way to for the items in b to change.
1795 * It is safe to drop the lock on our parent before we
1796 * go through the expensive btree search on b.
1798 * If cow is true, then we might be changing slot zero,
1799 * which may require changing the parent. So, we can't
1800 * drop the lock until after we know which slot we're
1804 btrfs_unlock_up_safe(p
, level
+ 1);
1806 ret
= check_block(root
, p
, level
);
1812 ret
= bin_search(b
, key
, level
, &slot
);
1816 if (ret
&& slot
> 0) {
1820 p
->slots
[level
] = slot
;
1821 err
= setup_nodes_for_search(trans
, root
, p
, b
, level
,
1829 b
= p
->nodes
[level
];
1830 slot
= p
->slots
[level
];
1832 unlock_up(p
, level
, lowest_unlock
);
1834 if (level
== lowest_level
) {
1840 err
= read_block_for_search(trans
, root
, p
,
1841 &b
, level
, slot
, key
);
1849 if (!p
->skip_locking
) {
1850 btrfs_clear_path_blocking(p
, NULL
);
1851 err
= btrfs_try_spin_lock(b
);
1854 btrfs_set_path_blocking(p
);
1856 btrfs_clear_path_blocking(p
, b
);
1860 p
->slots
[level
] = slot
;
1862 btrfs_leaf_free_space(root
, b
) < ins_len
) {
1863 btrfs_set_path_blocking(p
);
1864 err
= split_leaf(trans
, root
, key
,
1865 p
, ins_len
, ret
== 0);
1866 btrfs_clear_path_blocking(p
, NULL
);
1874 if (!p
->search_for_split
)
1875 unlock_up(p
, level
, lowest_unlock
);
1882 * we don't really know what they plan on doing with the path
1883 * from here on, so for now just mark it as blocking
1885 if (!p
->leave_spinning
)
1886 btrfs_set_path_blocking(p
);
1888 btrfs_release_path(root
, p
);
1893 * adjust the pointers going up the tree, starting at level
1894 * making sure the right key of each node is points to 'key'.
1895 * This is used after shifting pointers to the left, so it stops
1896 * fixing up pointers when a given leaf/node is not in slot 0 of the
1899 * If this fails to write a tree block, it returns -1, but continues
1900 * fixing up the blocks in ram so the tree is consistent.
1902 static int fixup_low_keys(struct btrfs_trans_handle
*trans
,
1903 struct btrfs_root
*root
, struct btrfs_path
*path
,
1904 struct btrfs_disk_key
*key
, int level
)
1908 struct extent_buffer
*t
;
1910 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
1911 int tslot
= path
->slots
[i
];
1912 if (!path
->nodes
[i
])
1915 btrfs_set_node_key(t
, key
, tslot
);
1916 btrfs_mark_buffer_dirty(path
->nodes
[i
]);
1926 * This function isn't completely safe. It's the caller's responsibility
1927 * that the new key won't break the order
1929 int btrfs_set_item_key_safe(struct btrfs_trans_handle
*trans
,
1930 struct btrfs_root
*root
, struct btrfs_path
*path
,
1931 struct btrfs_key
*new_key
)
1933 struct btrfs_disk_key disk_key
;
1934 struct extent_buffer
*eb
;
1937 eb
= path
->nodes
[0];
1938 slot
= path
->slots
[0];
1940 btrfs_item_key(eb
, &disk_key
, slot
- 1);
1941 if (comp_keys(&disk_key
, new_key
) >= 0)
1944 if (slot
< btrfs_header_nritems(eb
) - 1) {
1945 btrfs_item_key(eb
, &disk_key
, slot
+ 1);
1946 if (comp_keys(&disk_key
, new_key
) <= 0)
1950 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
1951 btrfs_set_item_key(eb
, &disk_key
, slot
);
1952 btrfs_mark_buffer_dirty(eb
);
1954 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
1959 * try to push data from one node into the next node left in the
1962 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1963 * error, and > 0 if there was no room in the left hand block.
1965 static int push_node_left(struct btrfs_trans_handle
*trans
,
1966 struct btrfs_root
*root
, struct extent_buffer
*dst
,
1967 struct extent_buffer
*src
, int empty
)
1974 src_nritems
= btrfs_header_nritems(src
);
1975 dst_nritems
= btrfs_header_nritems(dst
);
1976 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
1977 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
1978 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
1980 if (!empty
&& src_nritems
<= 8)
1983 if (push_items
<= 0)
1987 push_items
= min(src_nritems
, push_items
);
1988 if (push_items
< src_nritems
) {
1989 /* leave at least 8 pointers in the node if
1990 * we aren't going to empty it
1992 if (src_nritems
- push_items
< 8) {
1993 if (push_items
<= 8)
1999 push_items
= min(src_nritems
- 8, push_items
);
2001 copy_extent_buffer(dst
, src
,
2002 btrfs_node_key_ptr_offset(dst_nritems
),
2003 btrfs_node_key_ptr_offset(0),
2004 push_items
* sizeof(struct btrfs_key_ptr
));
2006 if (push_items
< src_nritems
) {
2007 memmove_extent_buffer(src
, btrfs_node_key_ptr_offset(0),
2008 btrfs_node_key_ptr_offset(push_items
),
2009 (src_nritems
- push_items
) *
2010 sizeof(struct btrfs_key_ptr
));
2012 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
2013 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
2014 btrfs_mark_buffer_dirty(src
);
2015 btrfs_mark_buffer_dirty(dst
);
2021 * try to push data from one node into the next node right in the
2024 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2025 * error, and > 0 if there was no room in the right hand block.
2027 * this will only push up to 1/2 the contents of the left node over
2029 static int balance_node_right(struct btrfs_trans_handle
*trans
,
2030 struct btrfs_root
*root
,
2031 struct extent_buffer
*dst
,
2032 struct extent_buffer
*src
)
2040 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
2041 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
2043 src_nritems
= btrfs_header_nritems(src
);
2044 dst_nritems
= btrfs_header_nritems(dst
);
2045 push_items
= BTRFS_NODEPTRS_PER_BLOCK(root
) - dst_nritems
;
2046 if (push_items
<= 0)
2049 if (src_nritems
< 4)
2052 max_push
= src_nritems
/ 2 + 1;
2053 /* don't try to empty the node */
2054 if (max_push
>= src_nritems
)
2057 if (max_push
< push_items
)
2058 push_items
= max_push
;
2060 memmove_extent_buffer(dst
, btrfs_node_key_ptr_offset(push_items
),
2061 btrfs_node_key_ptr_offset(0),
2063 sizeof(struct btrfs_key_ptr
));
2065 copy_extent_buffer(dst
, src
,
2066 btrfs_node_key_ptr_offset(0),
2067 btrfs_node_key_ptr_offset(src_nritems
- push_items
),
2068 push_items
* sizeof(struct btrfs_key_ptr
));
2070 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
2071 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
2073 btrfs_mark_buffer_dirty(src
);
2074 btrfs_mark_buffer_dirty(dst
);
2080 * helper function to insert a new root level in the tree.
2081 * A new node is allocated, and a single item is inserted to
2082 * point to the existing root
2084 * returns zero on success or < 0 on failure.
2086 static noinline
int insert_new_root(struct btrfs_trans_handle
*trans
,
2087 struct btrfs_root
*root
,
2088 struct btrfs_path
*path
, int level
)
2091 struct extent_buffer
*lower
;
2092 struct extent_buffer
*c
;
2093 struct extent_buffer
*old
;
2094 struct btrfs_disk_key lower_key
;
2096 BUG_ON(path
->nodes
[level
]);
2097 BUG_ON(path
->nodes
[level
-1] != root
->node
);
2099 lower
= path
->nodes
[level
-1];
2101 btrfs_item_key(lower
, &lower_key
, 0);
2103 btrfs_node_key(lower
, &lower_key
, 0);
2105 c
= btrfs_alloc_free_block(trans
, root
, root
->nodesize
, 0,
2106 root
->root_key
.objectid
, &lower_key
,
2107 level
, root
->node
->start
, 0);
2111 root_add_used(root
, root
->nodesize
);
2113 memset_extent_buffer(c
, 0, 0, sizeof(struct btrfs_header
));
2114 btrfs_set_header_nritems(c
, 1);
2115 btrfs_set_header_level(c
, level
);
2116 btrfs_set_header_bytenr(c
, c
->start
);
2117 btrfs_set_header_generation(c
, trans
->transid
);
2118 btrfs_set_header_backref_rev(c
, BTRFS_MIXED_BACKREF_REV
);
2119 btrfs_set_header_owner(c
, root
->root_key
.objectid
);
2121 write_extent_buffer(c
, root
->fs_info
->fsid
,
2122 (unsigned long)btrfs_header_fsid(c
),
2125 write_extent_buffer(c
, root
->fs_info
->chunk_tree_uuid
,
2126 (unsigned long)btrfs_header_chunk_tree_uuid(c
),
2129 btrfs_set_node_key(c
, &lower_key
, 0);
2130 btrfs_set_node_blockptr(c
, 0, lower
->start
);
2131 lower_gen
= btrfs_header_generation(lower
);
2132 WARN_ON(lower_gen
!= trans
->transid
);
2134 btrfs_set_node_ptr_generation(c
, 0, lower_gen
);
2136 btrfs_mark_buffer_dirty(c
);
2138 spin_lock(&root
->node_lock
);
2141 spin_unlock(&root
->node_lock
);
2143 /* the super has an extra ref to root->node */
2144 free_extent_buffer(old
);
2146 add_root_to_dirty_list(root
);
2147 extent_buffer_get(c
);
2148 path
->nodes
[level
] = c
;
2149 path
->locks
[level
] = 1;
2150 path
->slots
[level
] = 0;
2155 * worker function to insert a single pointer in a node.
2156 * the node should have enough room for the pointer already
2158 * slot and level indicate where you want the key to go, and
2159 * blocknr is the block the key points to.
2161 * returns zero on success and < 0 on any error
2163 static int insert_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
2164 *root
, struct btrfs_path
*path
, struct btrfs_disk_key
2165 *key
, u64 bytenr
, int slot
, int level
)
2167 struct extent_buffer
*lower
;
2170 BUG_ON(!path
->nodes
[level
]);
2171 btrfs_assert_tree_locked(path
->nodes
[level
]);
2172 lower
= path
->nodes
[level
];
2173 nritems
= btrfs_header_nritems(lower
);
2174 BUG_ON(slot
> nritems
);
2175 if (nritems
== BTRFS_NODEPTRS_PER_BLOCK(root
))
2177 if (slot
!= nritems
) {
2178 memmove_extent_buffer(lower
,
2179 btrfs_node_key_ptr_offset(slot
+ 1),
2180 btrfs_node_key_ptr_offset(slot
),
2181 (nritems
- slot
) * sizeof(struct btrfs_key_ptr
));
2183 btrfs_set_node_key(lower
, key
, slot
);
2184 btrfs_set_node_blockptr(lower
, slot
, bytenr
);
2185 WARN_ON(trans
->transid
== 0);
2186 btrfs_set_node_ptr_generation(lower
, slot
, trans
->transid
);
2187 btrfs_set_header_nritems(lower
, nritems
+ 1);
2188 btrfs_mark_buffer_dirty(lower
);
2193 * split the node at the specified level in path in two.
2194 * The path is corrected to point to the appropriate node after the split
2196 * Before splitting this tries to make some room in the node by pushing
2197 * left and right, if either one works, it returns right away.
2199 * returns 0 on success and < 0 on failure
2201 static noinline
int split_node(struct btrfs_trans_handle
*trans
,
2202 struct btrfs_root
*root
,
2203 struct btrfs_path
*path
, int level
)
2205 struct extent_buffer
*c
;
2206 struct extent_buffer
*split
;
2207 struct btrfs_disk_key disk_key
;
2213 c
= path
->nodes
[level
];
2214 WARN_ON(btrfs_header_generation(c
) != trans
->transid
);
2215 if (c
== root
->node
) {
2216 /* trying to split the root, lets make a new one */
2217 ret
= insert_new_root(trans
, root
, path
, level
+ 1);
2221 ret
= push_nodes_for_insert(trans
, root
, path
, level
);
2222 c
= path
->nodes
[level
];
2223 if (!ret
&& btrfs_header_nritems(c
) <
2224 BTRFS_NODEPTRS_PER_BLOCK(root
) - 3)
2230 c_nritems
= btrfs_header_nritems(c
);
2231 mid
= (c_nritems
+ 1) / 2;
2232 btrfs_node_key(c
, &disk_key
, mid
);
2234 split
= btrfs_alloc_free_block(trans
, root
, root
->nodesize
, 0,
2235 root
->root_key
.objectid
,
2236 &disk_key
, level
, c
->start
, 0);
2238 return PTR_ERR(split
);
2240 root_add_used(root
, root
->nodesize
);
2242 memset_extent_buffer(split
, 0, 0, sizeof(struct btrfs_header
));
2243 btrfs_set_header_level(split
, btrfs_header_level(c
));
2244 btrfs_set_header_bytenr(split
, split
->start
);
2245 btrfs_set_header_generation(split
, trans
->transid
);
2246 btrfs_set_header_backref_rev(split
, BTRFS_MIXED_BACKREF_REV
);
2247 btrfs_set_header_owner(split
, root
->root_key
.objectid
);
2248 write_extent_buffer(split
, root
->fs_info
->fsid
,
2249 (unsigned long)btrfs_header_fsid(split
),
2251 write_extent_buffer(split
, root
->fs_info
->chunk_tree_uuid
,
2252 (unsigned long)btrfs_header_chunk_tree_uuid(split
),
2256 copy_extent_buffer(split
, c
,
2257 btrfs_node_key_ptr_offset(0),
2258 btrfs_node_key_ptr_offset(mid
),
2259 (c_nritems
- mid
) * sizeof(struct btrfs_key_ptr
));
2260 btrfs_set_header_nritems(split
, c_nritems
- mid
);
2261 btrfs_set_header_nritems(c
, mid
);
2264 btrfs_mark_buffer_dirty(c
);
2265 btrfs_mark_buffer_dirty(split
);
2267 wret
= insert_ptr(trans
, root
, path
, &disk_key
, split
->start
,
2268 path
->slots
[level
+ 1] + 1,
2273 if (path
->slots
[level
] >= mid
) {
2274 path
->slots
[level
] -= mid
;
2275 btrfs_tree_unlock(c
);
2276 free_extent_buffer(c
);
2277 path
->nodes
[level
] = split
;
2278 path
->slots
[level
+ 1] += 1;
2280 btrfs_tree_unlock(split
);
2281 free_extent_buffer(split
);
2287 * how many bytes are required to store the items in a leaf. start
2288 * and nr indicate which items in the leaf to check. This totals up the
2289 * space used both by the item structs and the item data
2291 static int leaf_space_used(struct extent_buffer
*l
, int start
, int nr
)
2294 int nritems
= btrfs_header_nritems(l
);
2295 int end
= min(nritems
, start
+ nr
) - 1;
2299 data_len
= btrfs_item_end_nr(l
, start
);
2300 data_len
= data_len
- btrfs_item_offset_nr(l
, end
);
2301 data_len
+= sizeof(struct btrfs_item
) * nr
;
2302 WARN_ON(data_len
< 0);
2307 * The space between the end of the leaf items and
2308 * the start of the leaf data. IOW, how much room
2309 * the leaf has left for both items and data
2311 noinline
int btrfs_leaf_free_space(struct btrfs_root
*root
,
2312 struct extent_buffer
*leaf
)
2314 int nritems
= btrfs_header_nritems(leaf
);
2316 ret
= BTRFS_LEAF_DATA_SIZE(root
) - leaf_space_used(leaf
, 0, nritems
);
2318 printk(KERN_CRIT
"leaf free space ret %d, leaf data size %lu, "
2319 "used %d nritems %d\n",
2320 ret
, (unsigned long) BTRFS_LEAF_DATA_SIZE(root
),
2321 leaf_space_used(leaf
, 0, nritems
), nritems
);
2327 * min slot controls the lowest index we're willing to push to the
2328 * right. We'll push up to and including min_slot, but no lower
2330 static noinline
int __push_leaf_right(struct btrfs_trans_handle
*trans
,
2331 struct btrfs_root
*root
,
2332 struct btrfs_path
*path
,
2333 int data_size
, int empty
,
2334 struct extent_buffer
*right
,
2335 int free_space
, u32 left_nritems
,
2338 struct extent_buffer
*left
= path
->nodes
[0];
2339 struct extent_buffer
*upper
= path
->nodes
[1];
2340 struct btrfs_disk_key disk_key
;
2345 struct btrfs_item
*item
;
2354 nr
= max_t(u32
, 1, min_slot
);
2356 if (path
->slots
[0] >= left_nritems
)
2357 push_space
+= data_size
;
2359 slot
= path
->slots
[1];
2360 i
= left_nritems
- 1;
2362 item
= btrfs_item_nr(left
, i
);
2364 if (!empty
&& push_items
> 0) {
2365 if (path
->slots
[0] > i
)
2367 if (path
->slots
[0] == i
) {
2368 int space
= btrfs_leaf_free_space(root
, left
);
2369 if (space
+ push_space
* 2 > free_space
)
2374 if (path
->slots
[0] == i
)
2375 push_space
+= data_size
;
2377 if (!left
->map_token
) {
2378 map_extent_buffer(left
, (unsigned long)item
,
2379 sizeof(struct btrfs_item
),
2380 &left
->map_token
, &left
->kaddr
,
2381 &left
->map_start
, &left
->map_len
,
2385 this_item_size
= btrfs_item_size(left
, item
);
2386 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
2390 push_space
+= this_item_size
+ sizeof(*item
);
2395 if (left
->map_token
) {
2396 unmap_extent_buffer(left
, left
->map_token
, KM_USER1
);
2397 left
->map_token
= NULL
;
2400 if (push_items
== 0)
2403 if (!empty
&& push_items
== left_nritems
)
2406 /* push left to right */
2407 right_nritems
= btrfs_header_nritems(right
);
2409 push_space
= btrfs_item_end_nr(left
, left_nritems
- push_items
);
2410 push_space
-= leaf_data_end(root
, left
);
2412 /* make room in the right data area */
2413 data_end
= leaf_data_end(root
, right
);
2414 memmove_extent_buffer(right
,
2415 btrfs_leaf_data(right
) + data_end
- push_space
,
2416 btrfs_leaf_data(right
) + data_end
,
2417 BTRFS_LEAF_DATA_SIZE(root
) - data_end
);
2419 /* copy from the left data area */
2420 copy_extent_buffer(right
, left
, btrfs_leaf_data(right
) +
2421 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
2422 btrfs_leaf_data(left
) + leaf_data_end(root
, left
),
2425 memmove_extent_buffer(right
, btrfs_item_nr_offset(push_items
),
2426 btrfs_item_nr_offset(0),
2427 right_nritems
* sizeof(struct btrfs_item
));
2429 /* copy the items from left to right */
2430 copy_extent_buffer(right
, left
, btrfs_item_nr_offset(0),
2431 btrfs_item_nr_offset(left_nritems
- push_items
),
2432 push_items
* sizeof(struct btrfs_item
));
2434 /* update the item pointers */
2435 right_nritems
+= push_items
;
2436 btrfs_set_header_nritems(right
, right_nritems
);
2437 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
2438 for (i
= 0; i
< right_nritems
; i
++) {
2439 item
= btrfs_item_nr(right
, i
);
2440 if (!right
->map_token
) {
2441 map_extent_buffer(right
, (unsigned long)item
,
2442 sizeof(struct btrfs_item
),
2443 &right
->map_token
, &right
->kaddr
,
2444 &right
->map_start
, &right
->map_len
,
2447 push_space
-= btrfs_item_size(right
, item
);
2448 btrfs_set_item_offset(right
, item
, push_space
);
2451 if (right
->map_token
) {
2452 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
2453 right
->map_token
= NULL
;
2455 left_nritems
-= push_items
;
2456 btrfs_set_header_nritems(left
, left_nritems
);
2459 btrfs_mark_buffer_dirty(left
);
2461 clean_tree_block(trans
, root
, left
);
2463 btrfs_mark_buffer_dirty(right
);
2465 btrfs_item_key(right
, &disk_key
, 0);
2466 btrfs_set_node_key(upper
, &disk_key
, slot
+ 1);
2467 btrfs_mark_buffer_dirty(upper
);
2469 /* then fixup the leaf pointer in the path */
2470 if (path
->slots
[0] >= left_nritems
) {
2471 path
->slots
[0] -= left_nritems
;
2472 if (btrfs_header_nritems(path
->nodes
[0]) == 0)
2473 clean_tree_block(trans
, root
, path
->nodes
[0]);
2474 btrfs_tree_unlock(path
->nodes
[0]);
2475 free_extent_buffer(path
->nodes
[0]);
2476 path
->nodes
[0] = right
;
2477 path
->slots
[1] += 1;
2479 btrfs_tree_unlock(right
);
2480 free_extent_buffer(right
);
2485 btrfs_tree_unlock(right
);
2486 free_extent_buffer(right
);
2491 * push some data in the path leaf to the right, trying to free up at
2492 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2494 * returns 1 if the push failed because the other node didn't have enough
2495 * room, 0 if everything worked out and < 0 if there were major errors.
2497 * this will push starting from min_slot to the end of the leaf. It won't
2498 * push any slot lower than min_slot
2500 static int push_leaf_right(struct btrfs_trans_handle
*trans
, struct btrfs_root
2501 *root
, struct btrfs_path
*path
,
2502 int min_data_size
, int data_size
,
2503 int empty
, u32 min_slot
)
2505 struct extent_buffer
*left
= path
->nodes
[0];
2506 struct extent_buffer
*right
;
2507 struct extent_buffer
*upper
;
2513 if (!path
->nodes
[1])
2516 slot
= path
->slots
[1];
2517 upper
= path
->nodes
[1];
2518 if (slot
>= btrfs_header_nritems(upper
) - 1)
2521 btrfs_assert_tree_locked(path
->nodes
[1]);
2523 right
= read_node_slot(root
, upper
, slot
+ 1);
2524 btrfs_tree_lock(right
);
2525 btrfs_set_lock_blocking(right
);
2527 free_space
= btrfs_leaf_free_space(root
, right
);
2528 if (free_space
< data_size
)
2531 /* cow and double check */
2532 ret
= btrfs_cow_block(trans
, root
, right
, upper
,
2537 free_space
= btrfs_leaf_free_space(root
, right
);
2538 if (free_space
< data_size
)
2541 left_nritems
= btrfs_header_nritems(left
);
2542 if (left_nritems
== 0)
2545 return __push_leaf_right(trans
, root
, path
, min_data_size
, empty
,
2546 right
, free_space
, left_nritems
, min_slot
);
2548 btrfs_tree_unlock(right
);
2549 free_extent_buffer(right
);
2554 * push some data in the path leaf to the left, trying to free up at
2555 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2557 * max_slot can put a limit on how far into the leaf we'll push items. The
2558 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2561 static noinline
int __push_leaf_left(struct btrfs_trans_handle
*trans
,
2562 struct btrfs_root
*root
,
2563 struct btrfs_path
*path
, int data_size
,
2564 int empty
, struct extent_buffer
*left
,
2565 int free_space
, u32 right_nritems
,
2568 struct btrfs_disk_key disk_key
;
2569 struct extent_buffer
*right
= path
->nodes
[0];
2574 struct btrfs_item
*item
;
2575 u32 old_left_nritems
;
2580 u32 old_left_item_size
;
2582 slot
= path
->slots
[1];
2585 nr
= min(right_nritems
, max_slot
);
2587 nr
= min(right_nritems
- 1, max_slot
);
2589 for (i
= 0; i
< nr
; i
++) {
2590 item
= btrfs_item_nr(right
, i
);
2591 if (!right
->map_token
) {
2592 map_extent_buffer(right
, (unsigned long)item
,
2593 sizeof(struct btrfs_item
),
2594 &right
->map_token
, &right
->kaddr
,
2595 &right
->map_start
, &right
->map_len
,
2599 if (!empty
&& push_items
> 0) {
2600 if (path
->slots
[0] < i
)
2602 if (path
->slots
[0] == i
) {
2603 int space
= btrfs_leaf_free_space(root
, right
);
2604 if (space
+ push_space
* 2 > free_space
)
2609 if (path
->slots
[0] == i
)
2610 push_space
+= data_size
;
2612 this_item_size
= btrfs_item_size(right
, item
);
2613 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
2617 push_space
+= this_item_size
+ sizeof(*item
);
2620 if (right
->map_token
) {
2621 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
2622 right
->map_token
= NULL
;
2625 if (push_items
== 0) {
2629 if (!empty
&& push_items
== btrfs_header_nritems(right
))
2632 /* push data from right to left */
2633 copy_extent_buffer(left
, right
,
2634 btrfs_item_nr_offset(btrfs_header_nritems(left
)),
2635 btrfs_item_nr_offset(0),
2636 push_items
* sizeof(struct btrfs_item
));
2638 push_space
= BTRFS_LEAF_DATA_SIZE(root
) -
2639 btrfs_item_offset_nr(right
, push_items
- 1);
2641 copy_extent_buffer(left
, right
, btrfs_leaf_data(left
) +
2642 leaf_data_end(root
, left
) - push_space
,
2643 btrfs_leaf_data(right
) +
2644 btrfs_item_offset_nr(right
, push_items
- 1),
2646 old_left_nritems
= btrfs_header_nritems(left
);
2647 BUG_ON(old_left_nritems
<= 0);
2649 old_left_item_size
= btrfs_item_offset_nr(left
, old_left_nritems
- 1);
2650 for (i
= old_left_nritems
; i
< old_left_nritems
+ push_items
; i
++) {
2653 item
= btrfs_item_nr(left
, i
);
2654 if (!left
->map_token
) {
2655 map_extent_buffer(left
, (unsigned long)item
,
2656 sizeof(struct btrfs_item
),
2657 &left
->map_token
, &left
->kaddr
,
2658 &left
->map_start
, &left
->map_len
,
2662 ioff
= btrfs_item_offset(left
, item
);
2663 btrfs_set_item_offset(left
, item
,
2664 ioff
- (BTRFS_LEAF_DATA_SIZE(root
) - old_left_item_size
));
2666 btrfs_set_header_nritems(left
, old_left_nritems
+ push_items
);
2667 if (left
->map_token
) {
2668 unmap_extent_buffer(left
, left
->map_token
, KM_USER1
);
2669 left
->map_token
= NULL
;
2672 /* fixup right node */
2673 if (push_items
> right_nritems
) {
2674 printk(KERN_CRIT
"push items %d nr %u\n", push_items
,
2679 if (push_items
< right_nritems
) {
2680 push_space
= btrfs_item_offset_nr(right
, push_items
- 1) -
2681 leaf_data_end(root
, right
);
2682 memmove_extent_buffer(right
, btrfs_leaf_data(right
) +
2683 BTRFS_LEAF_DATA_SIZE(root
) - push_space
,
2684 btrfs_leaf_data(right
) +
2685 leaf_data_end(root
, right
), push_space
);
2687 memmove_extent_buffer(right
, btrfs_item_nr_offset(0),
2688 btrfs_item_nr_offset(push_items
),
2689 (btrfs_header_nritems(right
) - push_items
) *
2690 sizeof(struct btrfs_item
));
2692 right_nritems
-= push_items
;
2693 btrfs_set_header_nritems(right
, right_nritems
);
2694 push_space
= BTRFS_LEAF_DATA_SIZE(root
);
2695 for (i
= 0; i
< right_nritems
; i
++) {
2696 item
= btrfs_item_nr(right
, i
);
2698 if (!right
->map_token
) {
2699 map_extent_buffer(right
, (unsigned long)item
,
2700 sizeof(struct btrfs_item
),
2701 &right
->map_token
, &right
->kaddr
,
2702 &right
->map_start
, &right
->map_len
,
2706 push_space
= push_space
- btrfs_item_size(right
, item
);
2707 btrfs_set_item_offset(right
, item
, push_space
);
2709 if (right
->map_token
) {
2710 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
2711 right
->map_token
= NULL
;
2714 btrfs_mark_buffer_dirty(left
);
2716 btrfs_mark_buffer_dirty(right
);
2718 clean_tree_block(trans
, root
, right
);
2720 btrfs_item_key(right
, &disk_key
, 0);
2721 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
2725 /* then fixup the leaf pointer in the path */
2726 if (path
->slots
[0] < push_items
) {
2727 path
->slots
[0] += old_left_nritems
;
2728 btrfs_tree_unlock(path
->nodes
[0]);
2729 free_extent_buffer(path
->nodes
[0]);
2730 path
->nodes
[0] = left
;
2731 path
->slots
[1] -= 1;
2733 btrfs_tree_unlock(left
);
2734 free_extent_buffer(left
);
2735 path
->slots
[0] -= push_items
;
2737 BUG_ON(path
->slots
[0] < 0);
2740 btrfs_tree_unlock(left
);
2741 free_extent_buffer(left
);
2746 * push some data in the path leaf to the left, trying to free up at
2747 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2749 * max_slot can put a limit on how far into the leaf we'll push items. The
2750 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
2753 static int push_leaf_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
2754 *root
, struct btrfs_path
*path
, int min_data_size
,
2755 int data_size
, int empty
, u32 max_slot
)
2757 struct extent_buffer
*right
= path
->nodes
[0];
2758 struct extent_buffer
*left
;
2764 slot
= path
->slots
[1];
2767 if (!path
->nodes
[1])
2770 right_nritems
= btrfs_header_nritems(right
);
2771 if (right_nritems
== 0)
2774 btrfs_assert_tree_locked(path
->nodes
[1]);
2776 left
= read_node_slot(root
, path
->nodes
[1], slot
- 1);
2777 btrfs_tree_lock(left
);
2778 btrfs_set_lock_blocking(left
);
2780 free_space
= btrfs_leaf_free_space(root
, left
);
2781 if (free_space
< data_size
) {
2786 /* cow and double check */
2787 ret
= btrfs_cow_block(trans
, root
, left
,
2788 path
->nodes
[1], slot
- 1, &left
);
2790 /* we hit -ENOSPC, but it isn't fatal here */
2795 free_space
= btrfs_leaf_free_space(root
, left
);
2796 if (free_space
< data_size
) {
2801 return __push_leaf_left(trans
, root
, path
, min_data_size
,
2802 empty
, left
, free_space
, right_nritems
,
2805 btrfs_tree_unlock(left
);
2806 free_extent_buffer(left
);
2811 * split the path's leaf in two, making sure there is at least data_size
2812 * available for the resulting leaf level of the path.
2814 * returns 0 if all went well and < 0 on failure.
2816 static noinline
int copy_for_split(struct btrfs_trans_handle
*trans
,
2817 struct btrfs_root
*root
,
2818 struct btrfs_path
*path
,
2819 struct extent_buffer
*l
,
2820 struct extent_buffer
*right
,
2821 int slot
, int mid
, int nritems
)
2828 struct btrfs_disk_key disk_key
;
2830 nritems
= nritems
- mid
;
2831 btrfs_set_header_nritems(right
, nritems
);
2832 data_copy_size
= btrfs_item_end_nr(l
, mid
) - leaf_data_end(root
, l
);
2834 copy_extent_buffer(right
, l
, btrfs_item_nr_offset(0),
2835 btrfs_item_nr_offset(mid
),
2836 nritems
* sizeof(struct btrfs_item
));
2838 copy_extent_buffer(right
, l
,
2839 btrfs_leaf_data(right
) + BTRFS_LEAF_DATA_SIZE(root
) -
2840 data_copy_size
, btrfs_leaf_data(l
) +
2841 leaf_data_end(root
, l
), data_copy_size
);
2843 rt_data_off
= BTRFS_LEAF_DATA_SIZE(root
) -
2844 btrfs_item_end_nr(l
, mid
);
2846 for (i
= 0; i
< nritems
; i
++) {
2847 struct btrfs_item
*item
= btrfs_item_nr(right
, i
);
2850 if (!right
->map_token
) {
2851 map_extent_buffer(right
, (unsigned long)item
,
2852 sizeof(struct btrfs_item
),
2853 &right
->map_token
, &right
->kaddr
,
2854 &right
->map_start
, &right
->map_len
,
2858 ioff
= btrfs_item_offset(right
, item
);
2859 btrfs_set_item_offset(right
, item
, ioff
+ rt_data_off
);
2862 if (right
->map_token
) {
2863 unmap_extent_buffer(right
, right
->map_token
, KM_USER1
);
2864 right
->map_token
= NULL
;
2867 btrfs_set_header_nritems(l
, mid
);
2869 btrfs_item_key(right
, &disk_key
, 0);
2870 wret
= insert_ptr(trans
, root
, path
, &disk_key
, right
->start
,
2871 path
->slots
[1] + 1, 1);
2875 btrfs_mark_buffer_dirty(right
);
2876 btrfs_mark_buffer_dirty(l
);
2877 BUG_ON(path
->slots
[0] != slot
);
2880 btrfs_tree_unlock(path
->nodes
[0]);
2881 free_extent_buffer(path
->nodes
[0]);
2882 path
->nodes
[0] = right
;
2883 path
->slots
[0] -= mid
;
2884 path
->slots
[1] += 1;
2886 btrfs_tree_unlock(right
);
2887 free_extent_buffer(right
);
2890 BUG_ON(path
->slots
[0] < 0);
2896 * double splits happen when we need to insert a big item in the middle
2897 * of a leaf. A double split can leave us with 3 mostly empty leaves:
2898 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
2901 * We avoid this by trying to push the items on either side of our target
2902 * into the adjacent leaves. If all goes well we can avoid the double split
2905 static noinline
int push_for_double_split(struct btrfs_trans_handle
*trans
,
2906 struct btrfs_root
*root
,
2907 struct btrfs_path
*path
,
2915 slot
= path
->slots
[0];
2918 * try to push all the items after our slot into the
2921 ret
= push_leaf_right(trans
, root
, path
, 1, data_size
, 0, slot
);
2928 nritems
= btrfs_header_nritems(path
->nodes
[0]);
2930 * our goal is to get our slot at the start or end of a leaf. If
2931 * we've done so we're done
2933 if (path
->slots
[0] == 0 || path
->slots
[0] == nritems
)
2936 if (btrfs_leaf_free_space(root
, path
->nodes
[0]) >= data_size
)
2939 /* try to push all the items before our slot into the next leaf */
2940 slot
= path
->slots
[0];
2941 ret
= push_leaf_left(trans
, root
, path
, 1, data_size
, 0, slot
);
2954 * split the path's leaf in two, making sure there is at least data_size
2955 * available for the resulting leaf level of the path.
2957 * returns 0 if all went well and < 0 on failure.
2959 static noinline
int split_leaf(struct btrfs_trans_handle
*trans
,
2960 struct btrfs_root
*root
,
2961 struct btrfs_key
*ins_key
,
2962 struct btrfs_path
*path
, int data_size
,
2965 struct btrfs_disk_key disk_key
;
2966 struct extent_buffer
*l
;
2970 struct extent_buffer
*right
;
2974 int num_doubles
= 0;
2975 int tried_avoid_double
= 0;
2978 slot
= path
->slots
[0];
2979 if (extend
&& data_size
+ btrfs_item_size_nr(l
, slot
) +
2980 sizeof(struct btrfs_item
) > BTRFS_LEAF_DATA_SIZE(root
))
2983 /* first try to make some room by pushing left and right */
2985 wret
= push_leaf_right(trans
, root
, path
, data_size
,
2990 wret
= push_leaf_left(trans
, root
, path
, data_size
,
2991 data_size
, 0, (u32
)-1);
2997 /* did the pushes work? */
2998 if (btrfs_leaf_free_space(root
, l
) >= data_size
)
3002 if (!path
->nodes
[1]) {
3003 ret
= insert_new_root(trans
, root
, path
, 1);
3010 slot
= path
->slots
[0];
3011 nritems
= btrfs_header_nritems(l
);
3012 mid
= (nritems
+ 1) / 2;
3016 leaf_space_used(l
, mid
, nritems
- mid
) + data_size
>
3017 BTRFS_LEAF_DATA_SIZE(root
)) {
3018 if (slot
>= nritems
) {
3022 if (mid
!= nritems
&&
3023 leaf_space_used(l
, mid
, nritems
- mid
) +
3024 data_size
> BTRFS_LEAF_DATA_SIZE(root
)) {
3025 if (data_size
&& !tried_avoid_double
)
3026 goto push_for_double
;
3032 if (leaf_space_used(l
, 0, mid
) + data_size
>
3033 BTRFS_LEAF_DATA_SIZE(root
)) {
3034 if (!extend
&& data_size
&& slot
== 0) {
3036 } else if ((extend
|| !data_size
) && slot
== 0) {
3040 if (mid
!= nritems
&&
3041 leaf_space_used(l
, mid
, nritems
- mid
) +
3042 data_size
> BTRFS_LEAF_DATA_SIZE(root
)) {
3043 if (data_size
&& !tried_avoid_double
)
3044 goto push_for_double
;
3052 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
3054 btrfs_item_key(l
, &disk_key
, mid
);
3056 right
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
, 0,
3057 root
->root_key
.objectid
,
3058 &disk_key
, 0, l
->start
, 0);
3060 return PTR_ERR(right
);
3062 root_add_used(root
, root
->leafsize
);
3064 memset_extent_buffer(right
, 0, 0, sizeof(struct btrfs_header
));
3065 btrfs_set_header_bytenr(right
, right
->start
);
3066 btrfs_set_header_generation(right
, trans
->transid
);
3067 btrfs_set_header_backref_rev(right
, BTRFS_MIXED_BACKREF_REV
);
3068 btrfs_set_header_owner(right
, root
->root_key
.objectid
);
3069 btrfs_set_header_level(right
, 0);
3070 write_extent_buffer(right
, root
->fs_info
->fsid
,
3071 (unsigned long)btrfs_header_fsid(right
),
3074 write_extent_buffer(right
, root
->fs_info
->chunk_tree_uuid
,
3075 (unsigned long)btrfs_header_chunk_tree_uuid(right
),
3080 btrfs_set_header_nritems(right
, 0);
3081 wret
= insert_ptr(trans
, root
, path
,
3082 &disk_key
, right
->start
,
3083 path
->slots
[1] + 1, 1);
3087 btrfs_tree_unlock(path
->nodes
[0]);
3088 free_extent_buffer(path
->nodes
[0]);
3089 path
->nodes
[0] = right
;
3091 path
->slots
[1] += 1;
3093 btrfs_set_header_nritems(right
, 0);
3094 wret
= insert_ptr(trans
, root
, path
,
3100 btrfs_tree_unlock(path
->nodes
[0]);
3101 free_extent_buffer(path
->nodes
[0]);
3102 path
->nodes
[0] = right
;
3104 if (path
->slots
[1] == 0) {
3105 wret
= fixup_low_keys(trans
, root
,
3106 path
, &disk_key
, 1);
3111 btrfs_mark_buffer_dirty(right
);
3115 ret
= copy_for_split(trans
, root
, path
, l
, right
, slot
, mid
, nritems
);
3119 BUG_ON(num_doubles
!= 0);
3127 push_for_double_split(trans
, root
, path
, data_size
);
3128 tried_avoid_double
= 1;
3129 if (btrfs_leaf_free_space(root
, path
->nodes
[0]) >= data_size
)
3134 static noinline
int setup_leaf_for_split(struct btrfs_trans_handle
*trans
,
3135 struct btrfs_root
*root
,
3136 struct btrfs_path
*path
, int ins_len
)
3138 struct btrfs_key key
;
3139 struct extent_buffer
*leaf
;
3140 struct btrfs_file_extent_item
*fi
;
3145 leaf
= path
->nodes
[0];
3146 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3148 BUG_ON(key
.type
!= BTRFS_EXTENT_DATA_KEY
&&
3149 key
.type
!= BTRFS_EXTENT_CSUM_KEY
);
3151 if (btrfs_leaf_free_space(root
, leaf
) >= ins_len
)
3154 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
3155 if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
3156 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3157 struct btrfs_file_extent_item
);
3158 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
3160 btrfs_release_path(root
, path
);
3162 path
->keep_locks
= 1;
3163 path
->search_for_split
= 1;
3164 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
3165 path
->search_for_split
= 0;
3170 leaf
= path
->nodes
[0];
3171 /* if our item isn't there or got smaller, return now */
3172 if (ret
> 0 || item_size
!= btrfs_item_size_nr(leaf
, path
->slots
[0]))
3175 /* the leaf has changed, it now has room. return now */
3176 if (btrfs_leaf_free_space(root
, path
->nodes
[0]) >= ins_len
)
3179 if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
3180 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3181 struct btrfs_file_extent_item
);
3182 if (extent_len
!= btrfs_file_extent_num_bytes(leaf
, fi
))
3186 btrfs_set_path_blocking(path
);
3187 ret
= split_leaf(trans
, root
, &key
, path
, ins_len
, 1);
3191 path
->keep_locks
= 0;
3192 btrfs_unlock_up_safe(path
, 1);
3195 path
->keep_locks
= 0;
3199 static noinline
int split_item(struct btrfs_trans_handle
*trans
,
3200 struct btrfs_root
*root
,
3201 struct btrfs_path
*path
,
3202 struct btrfs_key
*new_key
,
3203 unsigned long split_offset
)
3205 struct extent_buffer
*leaf
;
3206 struct btrfs_item
*item
;
3207 struct btrfs_item
*new_item
;
3213 struct btrfs_disk_key disk_key
;
3215 leaf
= path
->nodes
[0];
3216 BUG_ON(btrfs_leaf_free_space(root
, leaf
) < sizeof(struct btrfs_item
));
3218 btrfs_set_path_blocking(path
);
3220 item
= btrfs_item_nr(leaf
, path
->slots
[0]);
3221 orig_offset
= btrfs_item_offset(leaf
, item
);
3222 item_size
= btrfs_item_size(leaf
, item
);
3224 buf
= kmalloc(item_size
, GFP_NOFS
);
3228 read_extent_buffer(leaf
, buf
, btrfs_item_ptr_offset(leaf
,
3229 path
->slots
[0]), item_size
);
3231 slot
= path
->slots
[0] + 1;
3232 nritems
= btrfs_header_nritems(leaf
);
3233 if (slot
!= nritems
) {
3234 /* shift the items */
3235 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ 1),
3236 btrfs_item_nr_offset(slot
),
3237 (nritems
- slot
) * sizeof(struct btrfs_item
));
3240 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
3241 btrfs_set_item_key(leaf
, &disk_key
, slot
);
3243 new_item
= btrfs_item_nr(leaf
, slot
);
3245 btrfs_set_item_offset(leaf
, new_item
, orig_offset
);
3246 btrfs_set_item_size(leaf
, new_item
, item_size
- split_offset
);
3248 btrfs_set_item_offset(leaf
, item
,
3249 orig_offset
+ item_size
- split_offset
);
3250 btrfs_set_item_size(leaf
, item
, split_offset
);
3252 btrfs_set_header_nritems(leaf
, nritems
+ 1);
3254 /* write the data for the start of the original item */
3255 write_extent_buffer(leaf
, buf
,
3256 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
3259 /* write the data for the new item */
3260 write_extent_buffer(leaf
, buf
+ split_offset
,
3261 btrfs_item_ptr_offset(leaf
, slot
),
3262 item_size
- split_offset
);
3263 btrfs_mark_buffer_dirty(leaf
);
3265 BUG_ON(btrfs_leaf_free_space(root
, leaf
) < 0);
3271 * This function splits a single item into two items,
3272 * giving 'new_key' to the new item and splitting the
3273 * old one at split_offset (from the start of the item).
3275 * The path may be released by this operation. After
3276 * the split, the path is pointing to the old item. The
3277 * new item is going to be in the same node as the old one.
3279 * Note, the item being split must be smaller enough to live alone on
3280 * a tree block with room for one extra struct btrfs_item
3282 * This allows us to split the item in place, keeping a lock on the
3283 * leaf the entire time.
3285 int btrfs_split_item(struct btrfs_trans_handle
*trans
,
3286 struct btrfs_root
*root
,
3287 struct btrfs_path
*path
,
3288 struct btrfs_key
*new_key
,
3289 unsigned long split_offset
)
3292 ret
= setup_leaf_for_split(trans
, root
, path
,
3293 sizeof(struct btrfs_item
));
3297 ret
= split_item(trans
, root
, path
, new_key
, split_offset
);
3302 * This function duplicate a item, giving 'new_key' to the new item.
3303 * It guarantees both items live in the same tree leaf and the new item
3304 * is contiguous with the original item.
3306 * This allows us to split file extent in place, keeping a lock on the
3307 * leaf the entire time.
3309 int btrfs_duplicate_item(struct btrfs_trans_handle
*trans
,
3310 struct btrfs_root
*root
,
3311 struct btrfs_path
*path
,
3312 struct btrfs_key
*new_key
)
3314 struct extent_buffer
*leaf
;
3318 leaf
= path
->nodes
[0];
3319 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
3320 ret
= setup_leaf_for_split(trans
, root
, path
,
3321 item_size
+ sizeof(struct btrfs_item
));
3326 ret
= setup_items_for_insert(trans
, root
, path
, new_key
, &item_size
,
3327 item_size
, item_size
+
3328 sizeof(struct btrfs_item
), 1);
3331 leaf
= path
->nodes
[0];
3332 memcpy_extent_buffer(leaf
,
3333 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
3334 btrfs_item_ptr_offset(leaf
, path
->slots
[0] - 1),
3340 * make the item pointed to by the path smaller. new_size indicates
3341 * how small to make it, and from_end tells us if we just chop bytes
3342 * off the end of the item or if we shift the item to chop bytes off
3345 int btrfs_truncate_item(struct btrfs_trans_handle
*trans
,
3346 struct btrfs_root
*root
,
3347 struct btrfs_path
*path
,
3348 u32 new_size
, int from_end
)
3353 struct extent_buffer
*leaf
;
3354 struct btrfs_item
*item
;
3356 unsigned int data_end
;
3357 unsigned int old_data_start
;
3358 unsigned int old_size
;
3359 unsigned int size_diff
;
3362 slot_orig
= path
->slots
[0];
3363 leaf
= path
->nodes
[0];
3364 slot
= path
->slots
[0];
3366 old_size
= btrfs_item_size_nr(leaf
, slot
);
3367 if (old_size
== new_size
)
3370 nritems
= btrfs_header_nritems(leaf
);
3371 data_end
= leaf_data_end(root
, leaf
);
3373 old_data_start
= btrfs_item_offset_nr(leaf
, slot
);
3375 size_diff
= old_size
- new_size
;
3378 BUG_ON(slot
>= nritems
);
3381 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3383 /* first correct the data pointers */
3384 for (i
= slot
; i
< nritems
; i
++) {
3386 item
= btrfs_item_nr(leaf
, i
);
3388 if (!leaf
->map_token
) {
3389 map_extent_buffer(leaf
, (unsigned long)item
,
3390 sizeof(struct btrfs_item
),
3391 &leaf
->map_token
, &leaf
->kaddr
,
3392 &leaf
->map_start
, &leaf
->map_len
,
3396 ioff
= btrfs_item_offset(leaf
, item
);
3397 btrfs_set_item_offset(leaf
, item
, ioff
+ size_diff
);
3400 if (leaf
->map_token
) {
3401 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3402 leaf
->map_token
= NULL
;
3405 /* shift the data */
3407 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3408 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
3409 data_end
, old_data_start
+ new_size
- data_end
);
3411 struct btrfs_disk_key disk_key
;
3414 btrfs_item_key(leaf
, &disk_key
, slot
);
3416 if (btrfs_disk_key_type(&disk_key
) == BTRFS_EXTENT_DATA_KEY
) {
3418 struct btrfs_file_extent_item
*fi
;
3420 fi
= btrfs_item_ptr(leaf
, slot
,
3421 struct btrfs_file_extent_item
);
3422 fi
= (struct btrfs_file_extent_item
*)(
3423 (unsigned long)fi
- size_diff
);
3425 if (btrfs_file_extent_type(leaf
, fi
) ==
3426 BTRFS_FILE_EXTENT_INLINE
) {
3427 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
3428 memmove_extent_buffer(leaf
, ptr
,
3430 offsetof(struct btrfs_file_extent_item
,
3435 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3436 data_end
+ size_diff
, btrfs_leaf_data(leaf
) +
3437 data_end
, old_data_start
- data_end
);
3439 offset
= btrfs_disk_key_offset(&disk_key
);
3440 btrfs_set_disk_key_offset(&disk_key
, offset
+ size_diff
);
3441 btrfs_set_item_key(leaf
, &disk_key
, slot
);
3443 fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
3446 item
= btrfs_item_nr(leaf
, slot
);
3447 btrfs_set_item_size(leaf
, item
, new_size
);
3448 btrfs_mark_buffer_dirty(leaf
);
3451 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3452 btrfs_print_leaf(root
, leaf
);
3459 * make the item pointed to by the path bigger, data_size is the new size.
3461 int btrfs_extend_item(struct btrfs_trans_handle
*trans
,
3462 struct btrfs_root
*root
, struct btrfs_path
*path
,
3468 struct extent_buffer
*leaf
;
3469 struct btrfs_item
*item
;
3471 unsigned int data_end
;
3472 unsigned int old_data
;
3473 unsigned int old_size
;
3476 slot_orig
= path
->slots
[0];
3477 leaf
= path
->nodes
[0];
3479 nritems
= btrfs_header_nritems(leaf
);
3480 data_end
= leaf_data_end(root
, leaf
);
3482 if (btrfs_leaf_free_space(root
, leaf
) < data_size
) {
3483 btrfs_print_leaf(root
, leaf
);
3486 slot
= path
->slots
[0];
3487 old_data
= btrfs_item_end_nr(leaf
, slot
);
3490 if (slot
>= nritems
) {
3491 btrfs_print_leaf(root
, leaf
);
3492 printk(KERN_CRIT
"slot %d too large, nritems %d\n",
3498 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3500 /* first correct the data pointers */
3501 for (i
= slot
; i
< nritems
; i
++) {
3503 item
= btrfs_item_nr(leaf
, i
);
3505 if (!leaf
->map_token
) {
3506 map_extent_buffer(leaf
, (unsigned long)item
,
3507 sizeof(struct btrfs_item
),
3508 &leaf
->map_token
, &leaf
->kaddr
,
3509 &leaf
->map_start
, &leaf
->map_len
,
3512 ioff
= btrfs_item_offset(leaf
, item
);
3513 btrfs_set_item_offset(leaf
, item
, ioff
- data_size
);
3516 if (leaf
->map_token
) {
3517 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3518 leaf
->map_token
= NULL
;
3521 /* shift the data */
3522 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3523 data_end
- data_size
, btrfs_leaf_data(leaf
) +
3524 data_end
, old_data
- data_end
);
3526 data_end
= old_data
;
3527 old_size
= btrfs_item_size_nr(leaf
, slot
);
3528 item
= btrfs_item_nr(leaf
, slot
);
3529 btrfs_set_item_size(leaf
, item
, old_size
+ data_size
);
3530 btrfs_mark_buffer_dirty(leaf
);
3533 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3534 btrfs_print_leaf(root
, leaf
);
3541 * Given a key and some data, insert items into the tree.
3542 * This does all the path init required, making room in the tree if needed.
3543 * Returns the number of keys that were inserted.
3545 int btrfs_insert_some_items(struct btrfs_trans_handle
*trans
,
3546 struct btrfs_root
*root
,
3547 struct btrfs_path
*path
,
3548 struct btrfs_key
*cpu_key
, u32
*data_size
,
3551 struct extent_buffer
*leaf
;
3552 struct btrfs_item
*item
;
3559 unsigned int data_end
;
3560 struct btrfs_disk_key disk_key
;
3561 struct btrfs_key found_key
;
3563 for (i
= 0; i
< nr
; i
++) {
3564 if (total_size
+ data_size
[i
] + sizeof(struct btrfs_item
) >
3565 BTRFS_LEAF_DATA_SIZE(root
)) {
3569 total_data
+= data_size
[i
];
3570 total_size
+= data_size
[i
] + sizeof(struct btrfs_item
);
3574 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
3580 leaf
= path
->nodes
[0];
3582 nritems
= btrfs_header_nritems(leaf
);
3583 data_end
= leaf_data_end(root
, leaf
);
3585 if (btrfs_leaf_free_space(root
, leaf
) < total_size
) {
3586 for (i
= nr
; i
>= 0; i
--) {
3587 total_data
-= data_size
[i
];
3588 total_size
-= data_size
[i
] + sizeof(struct btrfs_item
);
3589 if (total_size
< btrfs_leaf_free_space(root
, leaf
))
3595 slot
= path
->slots
[0];
3598 if (slot
!= nritems
) {
3599 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
3601 item
= btrfs_item_nr(leaf
, slot
);
3602 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3604 /* figure out how many keys we can insert in here */
3605 total_data
= data_size
[0];
3606 for (i
= 1; i
< nr
; i
++) {
3607 if (btrfs_comp_cpu_keys(&found_key
, cpu_key
+ i
) <= 0)
3609 total_data
+= data_size
[i
];
3613 if (old_data
< data_end
) {
3614 btrfs_print_leaf(root
, leaf
);
3615 printk(KERN_CRIT
"slot %d old_data %d data_end %d\n",
3616 slot
, old_data
, data_end
);
3620 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3622 /* first correct the data pointers */
3623 WARN_ON(leaf
->map_token
);
3624 for (i
= slot
; i
< nritems
; i
++) {
3627 item
= btrfs_item_nr(leaf
, i
);
3628 if (!leaf
->map_token
) {
3629 map_extent_buffer(leaf
, (unsigned long)item
,
3630 sizeof(struct btrfs_item
),
3631 &leaf
->map_token
, &leaf
->kaddr
,
3632 &leaf
->map_start
, &leaf
->map_len
,
3636 ioff
= btrfs_item_offset(leaf
, item
);
3637 btrfs_set_item_offset(leaf
, item
, ioff
- total_data
);
3639 if (leaf
->map_token
) {
3640 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3641 leaf
->map_token
= NULL
;
3644 /* shift the items */
3645 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
3646 btrfs_item_nr_offset(slot
),
3647 (nritems
- slot
) * sizeof(struct btrfs_item
));
3649 /* shift the data */
3650 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3651 data_end
- total_data
, btrfs_leaf_data(leaf
) +
3652 data_end
, old_data
- data_end
);
3653 data_end
= old_data
;
3656 * this sucks but it has to be done, if we are inserting at
3657 * the end of the leaf only insert 1 of the items, since we
3658 * have no way of knowing whats on the next leaf and we'd have
3659 * to drop our current locks to figure it out
3664 /* setup the item for the new data */
3665 for (i
= 0; i
< nr
; i
++) {
3666 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
3667 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
3668 item
= btrfs_item_nr(leaf
, slot
+ i
);
3669 btrfs_set_item_offset(leaf
, item
, data_end
- data_size
[i
]);
3670 data_end
-= data_size
[i
];
3671 btrfs_set_item_size(leaf
, item
, data_size
[i
]);
3673 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
3674 btrfs_mark_buffer_dirty(leaf
);
3678 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
3679 ret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
3682 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3683 btrfs_print_leaf(root
, leaf
);
3693 * this is a helper for btrfs_insert_empty_items, the main goal here is
3694 * to save stack depth by doing the bulk of the work in a function
3695 * that doesn't call btrfs_search_slot
3697 static noinline_for_stack
int
3698 setup_items_for_insert(struct btrfs_trans_handle
*trans
,
3699 struct btrfs_root
*root
, struct btrfs_path
*path
,
3700 struct btrfs_key
*cpu_key
, u32
*data_size
,
3701 u32 total_data
, u32 total_size
, int nr
)
3703 struct btrfs_item
*item
;
3706 unsigned int data_end
;
3707 struct btrfs_disk_key disk_key
;
3709 struct extent_buffer
*leaf
;
3712 leaf
= path
->nodes
[0];
3713 slot
= path
->slots
[0];
3715 nritems
= btrfs_header_nritems(leaf
);
3716 data_end
= leaf_data_end(root
, leaf
);
3718 if (btrfs_leaf_free_space(root
, leaf
) < total_size
) {
3719 btrfs_print_leaf(root
, leaf
);
3720 printk(KERN_CRIT
"not enough freespace need %u have %d\n",
3721 total_size
, btrfs_leaf_free_space(root
, leaf
));
3725 if (slot
!= nritems
) {
3726 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
3728 if (old_data
< data_end
) {
3729 btrfs_print_leaf(root
, leaf
);
3730 printk(KERN_CRIT
"slot %d old_data %d data_end %d\n",
3731 slot
, old_data
, data_end
);
3735 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3737 /* first correct the data pointers */
3738 WARN_ON(leaf
->map_token
);
3739 for (i
= slot
; i
< nritems
; i
++) {
3742 item
= btrfs_item_nr(leaf
, i
);
3743 if (!leaf
->map_token
) {
3744 map_extent_buffer(leaf
, (unsigned long)item
,
3745 sizeof(struct btrfs_item
),
3746 &leaf
->map_token
, &leaf
->kaddr
,
3747 &leaf
->map_start
, &leaf
->map_len
,
3751 ioff
= btrfs_item_offset(leaf
, item
);
3752 btrfs_set_item_offset(leaf
, item
, ioff
- total_data
);
3754 if (leaf
->map_token
) {
3755 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3756 leaf
->map_token
= NULL
;
3759 /* shift the items */
3760 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
3761 btrfs_item_nr_offset(slot
),
3762 (nritems
- slot
) * sizeof(struct btrfs_item
));
3764 /* shift the data */
3765 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3766 data_end
- total_data
, btrfs_leaf_data(leaf
) +
3767 data_end
, old_data
- data_end
);
3768 data_end
= old_data
;
3771 /* setup the item for the new data */
3772 for (i
= 0; i
< nr
; i
++) {
3773 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
3774 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
3775 item
= btrfs_item_nr(leaf
, slot
+ i
);
3776 btrfs_set_item_offset(leaf
, item
, data_end
- data_size
[i
]);
3777 data_end
-= data_size
[i
];
3778 btrfs_set_item_size(leaf
, item
, data_size
[i
]);
3781 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
3785 struct btrfs_disk_key disk_key
;
3786 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
3787 ret
= fixup_low_keys(trans
, root
, path
, &disk_key
, 1);
3789 btrfs_unlock_up_safe(path
, 1);
3790 btrfs_mark_buffer_dirty(leaf
);
3792 if (btrfs_leaf_free_space(root
, leaf
) < 0) {
3793 btrfs_print_leaf(root
, leaf
);
3800 * Given a key and some data, insert items into the tree.
3801 * This does all the path init required, making room in the tree if needed.
3803 int btrfs_insert_empty_items(struct btrfs_trans_handle
*trans
,
3804 struct btrfs_root
*root
,
3805 struct btrfs_path
*path
,
3806 struct btrfs_key
*cpu_key
, u32
*data_size
,
3809 struct extent_buffer
*leaf
;
3816 for (i
= 0; i
< nr
; i
++)
3817 total_data
+= data_size
[i
];
3819 total_size
= total_data
+ (nr
* sizeof(struct btrfs_item
));
3820 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
3826 leaf
= path
->nodes
[0];
3827 slot
= path
->slots
[0];
3830 ret
= setup_items_for_insert(trans
, root
, path
, cpu_key
, data_size
,
3831 total_data
, total_size
, nr
);
3838 * Given a key and some data, insert an item into the tree.
3839 * This does all the path init required, making room in the tree if needed.
3841 int btrfs_insert_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
3842 *root
, struct btrfs_key
*cpu_key
, void *data
, u32
3846 struct btrfs_path
*path
;
3847 struct extent_buffer
*leaf
;
3850 path
= btrfs_alloc_path();
3852 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
3854 leaf
= path
->nodes
[0];
3855 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
3856 write_extent_buffer(leaf
, data
, ptr
, data_size
);
3857 btrfs_mark_buffer_dirty(leaf
);
3859 btrfs_free_path(path
);
3864 * delete the pointer from a given node.
3866 * the tree should have been previously balanced so the deletion does not
3869 static int del_ptr(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
3870 struct btrfs_path
*path
, int level
, int slot
)
3872 struct extent_buffer
*parent
= path
->nodes
[level
];
3877 nritems
= btrfs_header_nritems(parent
);
3878 if (slot
!= nritems
- 1) {
3879 memmove_extent_buffer(parent
,
3880 btrfs_node_key_ptr_offset(slot
),
3881 btrfs_node_key_ptr_offset(slot
+ 1),
3882 sizeof(struct btrfs_key_ptr
) *
3883 (nritems
- slot
- 1));
3886 btrfs_set_header_nritems(parent
, nritems
);
3887 if (nritems
== 0 && parent
== root
->node
) {
3888 BUG_ON(btrfs_header_level(root
->node
) != 1);
3889 /* just turn the root into a leaf and break */
3890 btrfs_set_header_level(root
->node
, 0);
3891 } else if (slot
== 0) {
3892 struct btrfs_disk_key disk_key
;
3894 btrfs_node_key(parent
, &disk_key
, 0);
3895 wret
= fixup_low_keys(trans
, root
, path
, &disk_key
, level
+ 1);
3899 btrfs_mark_buffer_dirty(parent
);
3904 * a helper function to delete the leaf pointed to by path->slots[1] and
3907 * This deletes the pointer in path->nodes[1] and frees the leaf
3908 * block extent. zero is returned if it all worked out, < 0 otherwise.
3910 * The path must have already been setup for deleting the leaf, including
3911 * all the proper balancing. path->nodes[1] must be locked.
3913 static noinline
int btrfs_del_leaf(struct btrfs_trans_handle
*trans
,
3914 struct btrfs_root
*root
,
3915 struct btrfs_path
*path
,
3916 struct extent_buffer
*leaf
)
3920 WARN_ON(btrfs_header_generation(leaf
) != trans
->transid
);
3921 ret
= del_ptr(trans
, root
, path
, 1, path
->slots
[1]);
3926 * btrfs_free_extent is expensive, we want to make sure we
3927 * aren't holding any locks when we call it
3929 btrfs_unlock_up_safe(path
, 0);
3931 root_sub_used(root
, leaf
->len
);
3933 btrfs_free_tree_block(trans
, root
, leaf
, 0, 1);
3937 * delete the item at the leaf level in path. If that empties
3938 * the leaf, remove it from the tree
3940 int btrfs_del_items(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
3941 struct btrfs_path
*path
, int slot
, int nr
)
3943 struct extent_buffer
*leaf
;
3944 struct btrfs_item
*item
;
3952 leaf
= path
->nodes
[0];
3953 last_off
= btrfs_item_offset_nr(leaf
, slot
+ nr
- 1);
3955 for (i
= 0; i
< nr
; i
++)
3956 dsize
+= btrfs_item_size_nr(leaf
, slot
+ i
);
3958 nritems
= btrfs_header_nritems(leaf
);
3960 if (slot
+ nr
!= nritems
) {
3961 int data_end
= leaf_data_end(root
, leaf
);
3963 memmove_extent_buffer(leaf
, btrfs_leaf_data(leaf
) +
3965 btrfs_leaf_data(leaf
) + data_end
,
3966 last_off
- data_end
);
3968 for (i
= slot
+ nr
; i
< nritems
; i
++) {
3971 item
= btrfs_item_nr(leaf
, i
);
3972 if (!leaf
->map_token
) {
3973 map_extent_buffer(leaf
, (unsigned long)item
,
3974 sizeof(struct btrfs_item
),
3975 &leaf
->map_token
, &leaf
->kaddr
,
3976 &leaf
->map_start
, &leaf
->map_len
,
3979 ioff
= btrfs_item_offset(leaf
, item
);
3980 btrfs_set_item_offset(leaf
, item
, ioff
+ dsize
);
3983 if (leaf
->map_token
) {
3984 unmap_extent_buffer(leaf
, leaf
->map_token
, KM_USER1
);
3985 leaf
->map_token
= NULL
;
3988 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
),
3989 btrfs_item_nr_offset(slot
+ nr
),
3990 sizeof(struct btrfs_item
) *
3991 (nritems
- slot
- nr
));
3993 btrfs_set_header_nritems(leaf
, nritems
- nr
);
3996 /* delete the leaf if we've emptied it */
3998 if (leaf
== root
->node
) {
3999 btrfs_set_header_level(leaf
, 0);
4001 btrfs_set_path_blocking(path
);
4002 clean_tree_block(trans
, root
, leaf
);
4003 ret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
4007 int used
= leaf_space_used(leaf
, 0, nritems
);
4009 struct btrfs_disk_key disk_key
;
4011 btrfs_item_key(leaf
, &disk_key
, 0);
4012 wret
= fixup_low_keys(trans
, root
, path
,
4018 /* delete the leaf if it is mostly empty */
4019 if (used
< BTRFS_LEAF_DATA_SIZE(root
) / 3) {
4020 /* push_leaf_left fixes the path.
4021 * make sure the path still points to our leaf
4022 * for possible call to del_ptr below
4024 slot
= path
->slots
[1];
4025 extent_buffer_get(leaf
);
4027 btrfs_set_path_blocking(path
);
4028 wret
= push_leaf_left(trans
, root
, path
, 1, 1,
4030 if (wret
< 0 && wret
!= -ENOSPC
)
4033 if (path
->nodes
[0] == leaf
&&
4034 btrfs_header_nritems(leaf
)) {
4035 wret
= push_leaf_right(trans
, root
, path
, 1,
4037 if (wret
< 0 && wret
!= -ENOSPC
)
4041 if (btrfs_header_nritems(leaf
) == 0) {
4042 path
->slots
[1] = slot
;
4043 ret
= btrfs_del_leaf(trans
, root
, path
, leaf
);
4045 free_extent_buffer(leaf
);
4047 /* if we're still in the path, make sure
4048 * we're dirty. Otherwise, one of the
4049 * push_leaf functions must have already
4050 * dirtied this buffer
4052 if (path
->nodes
[0] == leaf
)
4053 btrfs_mark_buffer_dirty(leaf
);
4054 free_extent_buffer(leaf
);
4057 btrfs_mark_buffer_dirty(leaf
);
4064 * search the tree again to find a leaf with lesser keys
4065 * returns 0 if it found something or 1 if there are no lesser leaves.
4066 * returns < 0 on io errors.
4068 * This may release the path, and so you may lose any locks held at the
4071 int btrfs_prev_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
4073 struct btrfs_key key
;
4074 struct btrfs_disk_key found_key
;
4077 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, 0);
4081 else if (key
.type
> 0)
4083 else if (key
.objectid
> 0)
4088 btrfs_release_path(root
, path
);
4089 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4092 btrfs_item_key(path
->nodes
[0], &found_key
, 0);
4093 ret
= comp_keys(&found_key
, &key
);
4100 * A helper function to walk down the tree starting at min_key, and looking
4101 * for nodes or leaves that are either in cache or have a minimum
4102 * transaction id. This is used by the btree defrag code, and tree logging
4104 * This does not cow, but it does stuff the starting key it finds back
4105 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4106 * key and get a writable path.
4108 * This does lock as it descends, and path->keep_locks should be set
4109 * to 1 by the caller.
4111 * This honors path->lowest_level to prevent descent past a given level
4114 * min_trans indicates the oldest transaction that you are interested
4115 * in walking through. Any nodes or leaves older than min_trans are
4116 * skipped over (without reading them).
4118 * returns zero if something useful was found, < 0 on error and 1 if there
4119 * was nothing in the tree that matched the search criteria.
4121 int btrfs_search_forward(struct btrfs_root
*root
, struct btrfs_key
*min_key
,
4122 struct btrfs_key
*max_key
,
4123 struct btrfs_path
*path
, int cache_only
,
4126 struct extent_buffer
*cur
;
4127 struct btrfs_key found_key
;
4134 WARN_ON(!path
->keep_locks
);
4136 cur
= btrfs_lock_root_node(root
);
4137 level
= btrfs_header_level(cur
);
4138 WARN_ON(path
->nodes
[level
]);
4139 path
->nodes
[level
] = cur
;
4140 path
->locks
[level
] = 1;
4142 if (btrfs_header_generation(cur
) < min_trans
) {
4147 nritems
= btrfs_header_nritems(cur
);
4148 level
= btrfs_header_level(cur
);
4149 sret
= bin_search(cur
, min_key
, level
, &slot
);
4151 /* at the lowest level, we're done, setup the path and exit */
4152 if (level
== path
->lowest_level
) {
4153 if (slot
>= nritems
)
4156 path
->slots
[level
] = slot
;
4157 btrfs_item_key_to_cpu(cur
, &found_key
, slot
);
4160 if (sret
&& slot
> 0)
4163 * check this node pointer against the cache_only and
4164 * min_trans parameters. If it isn't in cache or is too
4165 * old, skip to the next one.
4167 while (slot
< nritems
) {
4170 struct extent_buffer
*tmp
;
4171 struct btrfs_disk_key disk_key
;
4173 blockptr
= btrfs_node_blockptr(cur
, slot
);
4174 gen
= btrfs_node_ptr_generation(cur
, slot
);
4175 if (gen
< min_trans
) {
4183 btrfs_node_key(cur
, &disk_key
, slot
);
4184 if (comp_keys(&disk_key
, max_key
) >= 0) {
4190 tmp
= btrfs_find_tree_block(root
, blockptr
,
4191 btrfs_level_size(root
, level
- 1));
4193 if (tmp
&& btrfs_buffer_uptodate(tmp
, gen
)) {
4194 free_extent_buffer(tmp
);
4198 free_extent_buffer(tmp
);
4203 * we didn't find a candidate key in this node, walk forward
4204 * and find another one
4206 if (slot
>= nritems
) {
4207 path
->slots
[level
] = slot
;
4208 btrfs_set_path_blocking(path
);
4209 sret
= btrfs_find_next_key(root
, path
, min_key
, level
,
4210 cache_only
, min_trans
);
4212 btrfs_release_path(root
, path
);
4218 /* save our key for returning back */
4219 btrfs_node_key_to_cpu(cur
, &found_key
, slot
);
4220 path
->slots
[level
] = slot
;
4221 if (level
== path
->lowest_level
) {
4223 unlock_up(path
, level
, 1);
4226 btrfs_set_path_blocking(path
);
4227 cur
= read_node_slot(root
, cur
, slot
);
4229 btrfs_tree_lock(cur
);
4231 path
->locks
[level
- 1] = 1;
4232 path
->nodes
[level
- 1] = cur
;
4233 unlock_up(path
, level
, 1);
4234 btrfs_clear_path_blocking(path
, NULL
);
4238 memcpy(min_key
, &found_key
, sizeof(found_key
));
4239 btrfs_set_path_blocking(path
);
4244 * this is similar to btrfs_next_leaf, but does not try to preserve
4245 * and fixup the path. It looks for and returns the next key in the
4246 * tree based on the current path and the cache_only and min_trans
4249 * 0 is returned if another key is found, < 0 if there are any errors
4250 * and 1 is returned if there are no higher keys in the tree
4252 * path->keep_locks should be set to 1 on the search made before
4253 * calling this function.
4255 int btrfs_find_next_key(struct btrfs_root
*root
, struct btrfs_path
*path
,
4256 struct btrfs_key
*key
, int level
,
4257 int cache_only
, u64 min_trans
)
4260 struct extent_buffer
*c
;
4262 WARN_ON(!path
->keep_locks
);
4263 while (level
< BTRFS_MAX_LEVEL
) {
4264 if (!path
->nodes
[level
])
4267 slot
= path
->slots
[level
] + 1;
4268 c
= path
->nodes
[level
];
4270 if (slot
>= btrfs_header_nritems(c
)) {
4273 struct btrfs_key cur_key
;
4274 if (level
+ 1 >= BTRFS_MAX_LEVEL
||
4275 !path
->nodes
[level
+ 1])
4278 if (path
->locks
[level
+ 1]) {
4283 slot
= btrfs_header_nritems(c
) - 1;
4285 btrfs_item_key_to_cpu(c
, &cur_key
, slot
);
4287 btrfs_node_key_to_cpu(c
, &cur_key
, slot
);
4289 orig_lowest
= path
->lowest_level
;
4290 btrfs_release_path(root
, path
);
4291 path
->lowest_level
= level
;
4292 ret
= btrfs_search_slot(NULL
, root
, &cur_key
, path
,
4294 path
->lowest_level
= orig_lowest
;
4298 c
= path
->nodes
[level
];
4299 slot
= path
->slots
[level
];
4306 btrfs_item_key_to_cpu(c
, key
, slot
);
4308 u64 blockptr
= btrfs_node_blockptr(c
, slot
);
4309 u64 gen
= btrfs_node_ptr_generation(c
, slot
);
4312 struct extent_buffer
*cur
;
4313 cur
= btrfs_find_tree_block(root
, blockptr
,
4314 btrfs_level_size(root
, level
- 1));
4315 if (!cur
|| !btrfs_buffer_uptodate(cur
, gen
)) {
4318 free_extent_buffer(cur
);
4321 free_extent_buffer(cur
);
4323 if (gen
< min_trans
) {
4327 btrfs_node_key_to_cpu(c
, key
, slot
);
4335 * search the tree again to find a leaf with greater keys
4336 * returns 0 if it found something or 1 if there are no greater leaves.
4337 * returns < 0 on io errors.
4339 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
4343 struct extent_buffer
*c
;
4344 struct extent_buffer
*next
;
4345 struct btrfs_key key
;
4348 int old_spinning
= path
->leave_spinning
;
4349 int force_blocking
= 0;
4351 nritems
= btrfs_header_nritems(path
->nodes
[0]);
4356 * we take the blocks in an order that upsets lockdep. Using
4357 * blocking mode is the only way around it.
4359 #ifdef CONFIG_DEBUG_LOCK_ALLOC
4363 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, nritems
- 1);
4367 btrfs_release_path(root
, path
);
4369 path
->keep_locks
= 1;
4371 if (!force_blocking
)
4372 path
->leave_spinning
= 1;
4374 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
4375 path
->keep_locks
= 0;
4380 nritems
= btrfs_header_nritems(path
->nodes
[0]);
4382 * by releasing the path above we dropped all our locks. A balance
4383 * could have added more items next to the key that used to be
4384 * at the very end of the block. So, check again here and
4385 * advance the path if there are now more items available.
4387 if (nritems
> 0 && path
->slots
[0] < nritems
- 1) {
4394 while (level
< BTRFS_MAX_LEVEL
) {
4395 if (!path
->nodes
[level
]) {
4400 slot
= path
->slots
[level
] + 1;
4401 c
= path
->nodes
[level
];
4402 if (slot
>= btrfs_header_nritems(c
)) {
4404 if (level
== BTRFS_MAX_LEVEL
) {
4412 btrfs_tree_unlock(next
);
4413 free_extent_buffer(next
);
4417 ret
= read_block_for_search(NULL
, root
, path
, &next
, level
,
4423 btrfs_release_path(root
, path
);
4427 if (!path
->skip_locking
) {
4428 ret
= btrfs_try_spin_lock(next
);
4430 btrfs_set_path_blocking(path
);
4431 btrfs_tree_lock(next
);
4432 if (!force_blocking
)
4433 btrfs_clear_path_blocking(path
, next
);
4436 btrfs_set_lock_blocking(next
);
4440 path
->slots
[level
] = slot
;
4443 c
= path
->nodes
[level
];
4444 if (path
->locks
[level
])
4445 btrfs_tree_unlock(c
);
4447 free_extent_buffer(c
);
4448 path
->nodes
[level
] = next
;
4449 path
->slots
[level
] = 0;
4450 if (!path
->skip_locking
)
4451 path
->locks
[level
] = 1;
4456 ret
= read_block_for_search(NULL
, root
, path
, &next
, level
,
4462 btrfs_release_path(root
, path
);
4466 if (!path
->skip_locking
) {
4467 btrfs_assert_tree_locked(path
->nodes
[level
]);
4468 ret
= btrfs_try_spin_lock(next
);
4470 btrfs_set_path_blocking(path
);
4471 btrfs_tree_lock(next
);
4472 if (!force_blocking
)
4473 btrfs_clear_path_blocking(path
, next
);
4476 btrfs_set_lock_blocking(next
);
4481 unlock_up(path
, 0, 1);
4482 path
->leave_spinning
= old_spinning
;
4484 btrfs_set_path_blocking(path
);
4490 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4491 * searching until it gets past min_objectid or finds an item of 'type'
4493 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4495 int btrfs_previous_item(struct btrfs_root
*root
,
4496 struct btrfs_path
*path
, u64 min_objectid
,
4499 struct btrfs_key found_key
;
4500 struct extent_buffer
*leaf
;
4505 if (path
->slots
[0] == 0) {
4506 btrfs_set_path_blocking(path
);
4507 ret
= btrfs_prev_leaf(root
, path
);
4513 leaf
= path
->nodes
[0];
4514 nritems
= btrfs_header_nritems(leaf
);
4517 if (path
->slots
[0] == nritems
)
4520 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4521 if (found_key
.objectid
< min_objectid
)
4523 if (found_key
.type
== type
)
4525 if (found_key
.objectid
== min_objectid
&&
4526 found_key
.type
< type
)