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>
21 #include <linux/rbtree.h>
25 #include "transaction.h"
26 #include "print-tree.h"
29 static int split_node(struct btrfs_trans_handle
*trans
, struct btrfs_root
30 *root
, struct btrfs_path
*path
, int level
);
31 static int split_leaf(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
32 const struct btrfs_key
*ins_key
, struct btrfs_path
*path
,
33 int data_size
, int extend
);
34 static int push_node_left(struct btrfs_trans_handle
*trans
,
35 struct btrfs_fs_info
*fs_info
,
36 struct extent_buffer
*dst
,
37 struct extent_buffer
*src
, int empty
);
38 static int balance_node_right(struct btrfs_trans_handle
*trans
,
39 struct btrfs_fs_info
*fs_info
,
40 struct extent_buffer
*dst_buf
,
41 struct extent_buffer
*src_buf
);
42 static void del_ptr(struct btrfs_root
*root
, struct btrfs_path
*path
,
44 static int tree_mod_log_free_eb(struct btrfs_fs_info
*fs_info
,
45 struct extent_buffer
*eb
);
47 struct btrfs_path
*btrfs_alloc_path(void)
49 return kmem_cache_zalloc(btrfs_path_cachep
, GFP_NOFS
);
53 * set all locked nodes in the path to blocking locks. This should
54 * be done before scheduling
56 noinline
void btrfs_set_path_blocking(struct btrfs_path
*p
)
59 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
60 if (!p
->nodes
[i
] || !p
->locks
[i
])
62 btrfs_set_lock_blocking_rw(p
->nodes
[i
], p
->locks
[i
]);
63 if (p
->locks
[i
] == BTRFS_READ_LOCK
)
64 p
->locks
[i
] = BTRFS_READ_LOCK_BLOCKING
;
65 else if (p
->locks
[i
] == BTRFS_WRITE_LOCK
)
66 p
->locks
[i
] = BTRFS_WRITE_LOCK_BLOCKING
;
71 * reset all the locked nodes in the patch to spinning locks.
73 * held is used to keep lockdep happy, when lockdep is enabled
74 * we set held to a blocking lock before we go around and
75 * retake all the spinlocks in the path. You can safely use NULL
78 noinline
void btrfs_clear_path_blocking(struct btrfs_path
*p
,
79 struct extent_buffer
*held
, int held_rw
)
84 btrfs_set_lock_blocking_rw(held
, held_rw
);
85 if (held_rw
== BTRFS_WRITE_LOCK
)
86 held_rw
= BTRFS_WRITE_LOCK_BLOCKING
;
87 else if (held_rw
== BTRFS_READ_LOCK
)
88 held_rw
= BTRFS_READ_LOCK_BLOCKING
;
90 btrfs_set_path_blocking(p
);
92 for (i
= BTRFS_MAX_LEVEL
- 1; i
>= 0; i
--) {
93 if (p
->nodes
[i
] && p
->locks
[i
]) {
94 btrfs_clear_lock_blocking_rw(p
->nodes
[i
], p
->locks
[i
]);
95 if (p
->locks
[i
] == BTRFS_WRITE_LOCK_BLOCKING
)
96 p
->locks
[i
] = BTRFS_WRITE_LOCK
;
97 else if (p
->locks
[i
] == BTRFS_READ_LOCK_BLOCKING
)
98 p
->locks
[i
] = BTRFS_READ_LOCK
;
103 btrfs_clear_lock_blocking_rw(held
, held_rw
);
106 /* this also releases the path */
107 void btrfs_free_path(struct btrfs_path
*p
)
111 btrfs_release_path(p
);
112 kmem_cache_free(btrfs_path_cachep
, p
);
116 * path release drops references on the extent buffers in the path
117 * and it drops any locks held by this path
119 * It is safe to call this on paths that no locks or extent buffers held.
121 noinline
void btrfs_release_path(struct btrfs_path
*p
)
125 for (i
= 0; i
< BTRFS_MAX_LEVEL
; i
++) {
130 btrfs_tree_unlock_rw(p
->nodes
[i
], p
->locks
[i
]);
133 free_extent_buffer(p
->nodes
[i
]);
139 * safely gets a reference on the root node of a tree. A lock
140 * is not taken, so a concurrent writer may put a different node
141 * at the root of the tree. See btrfs_lock_root_node for the
144 * The extent buffer returned by this has a reference taken, so
145 * it won't disappear. It may stop being the root of the tree
146 * at any time because there are no locks held.
148 struct extent_buffer
*btrfs_root_node(struct btrfs_root
*root
)
150 struct extent_buffer
*eb
;
154 eb
= rcu_dereference(root
->node
);
157 * RCU really hurts here, we could free up the root node because
158 * it was COWed but we may not get the new root node yet so do
159 * the inc_not_zero dance and if it doesn't work then
160 * synchronize_rcu and try again.
162 if (atomic_inc_not_zero(&eb
->refs
)) {
172 /* loop around taking references on and locking the root node of the
173 * tree until you end up with a lock on the root. A locked buffer
174 * is returned, with a reference held.
176 struct extent_buffer
*btrfs_lock_root_node(struct btrfs_root
*root
)
178 struct extent_buffer
*eb
;
181 eb
= btrfs_root_node(root
);
183 if (eb
== root
->node
)
185 btrfs_tree_unlock(eb
);
186 free_extent_buffer(eb
);
191 /* loop around taking references on and locking the root node of the
192 * tree until you end up with a lock on the root. A locked buffer
193 * is returned, with a reference held.
195 static struct extent_buffer
*btrfs_read_lock_root_node(struct btrfs_root
*root
)
197 struct extent_buffer
*eb
;
200 eb
= btrfs_root_node(root
);
201 btrfs_tree_read_lock(eb
);
202 if (eb
== root
->node
)
204 btrfs_tree_read_unlock(eb
);
205 free_extent_buffer(eb
);
210 /* cowonly root (everything not a reference counted cow subvolume), just get
211 * put onto a simple dirty list. transaction.c walks this to make sure they
212 * get properly updated on disk.
214 static void add_root_to_dirty_list(struct btrfs_root
*root
)
216 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
218 if (test_bit(BTRFS_ROOT_DIRTY
, &root
->state
) ||
219 !test_bit(BTRFS_ROOT_TRACK_DIRTY
, &root
->state
))
222 spin_lock(&fs_info
->trans_lock
);
223 if (!test_and_set_bit(BTRFS_ROOT_DIRTY
, &root
->state
)) {
224 /* Want the extent tree to be the last on the list */
225 if (root
->objectid
== BTRFS_EXTENT_TREE_OBJECTID
)
226 list_move_tail(&root
->dirty_list
,
227 &fs_info
->dirty_cowonly_roots
);
229 list_move(&root
->dirty_list
,
230 &fs_info
->dirty_cowonly_roots
);
232 spin_unlock(&fs_info
->trans_lock
);
236 * used by snapshot creation to make a copy of a root for a tree with
237 * a given objectid. The buffer with the new root node is returned in
238 * cow_ret, and this func returns zero on success or a negative error code.
240 int btrfs_copy_root(struct btrfs_trans_handle
*trans
,
241 struct btrfs_root
*root
,
242 struct extent_buffer
*buf
,
243 struct extent_buffer
**cow_ret
, u64 new_root_objectid
)
245 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
246 struct extent_buffer
*cow
;
249 struct btrfs_disk_key disk_key
;
251 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) &&
252 trans
->transid
!= fs_info
->running_transaction
->transid
);
253 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) &&
254 trans
->transid
!= root
->last_trans
);
256 level
= btrfs_header_level(buf
);
258 btrfs_item_key(buf
, &disk_key
, 0);
260 btrfs_node_key(buf
, &disk_key
, 0);
262 cow
= btrfs_alloc_tree_block(trans
, root
, 0, new_root_objectid
,
263 &disk_key
, level
, buf
->start
, 0);
267 copy_extent_buffer_full(cow
, buf
);
268 btrfs_set_header_bytenr(cow
, cow
->start
);
269 btrfs_set_header_generation(cow
, trans
->transid
);
270 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
271 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
272 BTRFS_HEADER_FLAG_RELOC
);
273 if (new_root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
274 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
276 btrfs_set_header_owner(cow
, new_root_objectid
);
278 write_extent_buffer_fsid(cow
, fs_info
->fsid
);
280 WARN_ON(btrfs_header_generation(buf
) > trans
->transid
);
281 if (new_root_objectid
== BTRFS_TREE_RELOC_OBJECTID
)
282 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
284 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
289 btrfs_mark_buffer_dirty(cow
);
298 MOD_LOG_KEY_REMOVE_WHILE_FREEING
,
299 MOD_LOG_KEY_REMOVE_WHILE_MOVING
,
301 MOD_LOG_ROOT_REPLACE
,
304 struct tree_mod_move
{
309 struct tree_mod_root
{
314 struct tree_mod_elem
{
320 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
323 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
326 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
327 struct btrfs_disk_key key
;
330 /* this is used for op == MOD_LOG_MOVE_KEYS */
331 struct tree_mod_move move
;
333 /* this is used for op == MOD_LOG_ROOT_REPLACE */
334 struct tree_mod_root old_root
;
338 * Pull a new tree mod seq number for our operation.
340 static inline u64
btrfs_inc_tree_mod_seq(struct btrfs_fs_info
*fs_info
)
342 return atomic64_inc_return(&fs_info
->tree_mod_seq
);
346 * This adds a new blocker to the tree mod log's blocker list if the @elem
347 * passed does not already have a sequence number set. So when a caller expects
348 * to record tree modifications, it should ensure to set elem->seq to zero
349 * before calling btrfs_get_tree_mod_seq.
350 * Returns a fresh, unused tree log modification sequence number, even if no new
353 u64
btrfs_get_tree_mod_seq(struct btrfs_fs_info
*fs_info
,
354 struct seq_list
*elem
)
356 write_lock(&fs_info
->tree_mod_log_lock
);
358 elem
->seq
= btrfs_inc_tree_mod_seq(fs_info
);
359 list_add_tail(&elem
->list
, &fs_info
->tree_mod_seq_list
);
361 write_unlock(&fs_info
->tree_mod_log_lock
);
366 void btrfs_put_tree_mod_seq(struct btrfs_fs_info
*fs_info
,
367 struct seq_list
*elem
)
369 struct rb_root
*tm_root
;
370 struct rb_node
*node
;
371 struct rb_node
*next
;
372 struct seq_list
*cur_elem
;
373 struct tree_mod_elem
*tm
;
374 u64 min_seq
= (u64
)-1;
375 u64 seq_putting
= elem
->seq
;
380 write_lock(&fs_info
->tree_mod_log_lock
);
381 list_del(&elem
->list
);
384 list_for_each_entry(cur_elem
, &fs_info
->tree_mod_seq_list
, list
) {
385 if (cur_elem
->seq
< min_seq
) {
386 if (seq_putting
> cur_elem
->seq
) {
388 * blocker with lower sequence number exists, we
389 * cannot remove anything from the log
391 write_unlock(&fs_info
->tree_mod_log_lock
);
394 min_seq
= cur_elem
->seq
;
399 * anything that's lower than the lowest existing (read: blocked)
400 * sequence number can be removed from the tree.
402 tm_root
= &fs_info
->tree_mod_log
;
403 for (node
= rb_first(tm_root
); node
; node
= next
) {
404 next
= rb_next(node
);
405 tm
= rb_entry(node
, struct tree_mod_elem
, node
);
406 if (tm
->seq
>= min_seq
)
408 rb_erase(node
, tm_root
);
411 write_unlock(&fs_info
->tree_mod_log_lock
);
415 * key order of the log:
416 * node/leaf start address -> sequence
418 * The 'start address' is the logical address of the *new* root node
419 * for root replace operations, or the logical address of the affected
420 * block for all other operations.
422 * Note: must be called with write lock for fs_info::tree_mod_log_lock.
425 __tree_mod_log_insert(struct btrfs_fs_info
*fs_info
, struct tree_mod_elem
*tm
)
427 struct rb_root
*tm_root
;
428 struct rb_node
**new;
429 struct rb_node
*parent
= NULL
;
430 struct tree_mod_elem
*cur
;
432 tm
->seq
= btrfs_inc_tree_mod_seq(fs_info
);
434 tm_root
= &fs_info
->tree_mod_log
;
435 new = &tm_root
->rb_node
;
437 cur
= rb_entry(*new, struct tree_mod_elem
, node
);
439 if (cur
->logical
< tm
->logical
)
440 new = &((*new)->rb_left
);
441 else if (cur
->logical
> tm
->logical
)
442 new = &((*new)->rb_right
);
443 else if (cur
->seq
< tm
->seq
)
444 new = &((*new)->rb_left
);
445 else if (cur
->seq
> tm
->seq
)
446 new = &((*new)->rb_right
);
451 rb_link_node(&tm
->node
, parent
, new);
452 rb_insert_color(&tm
->node
, tm_root
);
457 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
458 * returns zero with the tree_mod_log_lock acquired. The caller must hold
459 * this until all tree mod log insertions are recorded in the rb tree and then
460 * write unlock fs_info::tree_mod_log_lock.
462 static inline int tree_mod_dont_log(struct btrfs_fs_info
*fs_info
,
463 struct extent_buffer
*eb
) {
465 if (list_empty(&(fs_info
)->tree_mod_seq_list
))
467 if (eb
&& btrfs_header_level(eb
) == 0)
470 write_lock(&fs_info
->tree_mod_log_lock
);
471 if (list_empty(&(fs_info
)->tree_mod_seq_list
)) {
472 write_unlock(&fs_info
->tree_mod_log_lock
);
479 /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
480 static inline int tree_mod_need_log(const struct btrfs_fs_info
*fs_info
,
481 struct extent_buffer
*eb
)
484 if (list_empty(&(fs_info
)->tree_mod_seq_list
))
486 if (eb
&& btrfs_header_level(eb
) == 0)
492 static struct tree_mod_elem
*
493 alloc_tree_mod_elem(struct extent_buffer
*eb
, int slot
,
494 enum mod_log_op op
, gfp_t flags
)
496 struct tree_mod_elem
*tm
;
498 tm
= kzalloc(sizeof(*tm
), flags
);
502 tm
->logical
= eb
->start
;
503 if (op
!= MOD_LOG_KEY_ADD
) {
504 btrfs_node_key(eb
, &tm
->key
, slot
);
505 tm
->blockptr
= btrfs_node_blockptr(eb
, slot
);
509 tm
->generation
= btrfs_node_ptr_generation(eb
, slot
);
510 RB_CLEAR_NODE(&tm
->node
);
516 tree_mod_log_insert_key(struct btrfs_fs_info
*fs_info
,
517 struct extent_buffer
*eb
, int slot
,
518 enum mod_log_op op
, gfp_t flags
)
520 struct tree_mod_elem
*tm
;
523 if (!tree_mod_need_log(fs_info
, eb
))
526 tm
= alloc_tree_mod_elem(eb
, slot
, op
, flags
);
530 if (tree_mod_dont_log(fs_info
, eb
)) {
535 ret
= __tree_mod_log_insert(fs_info
, tm
);
536 write_unlock(&eb
->fs_info
->tree_mod_log_lock
);
544 tree_mod_log_insert_move(struct btrfs_fs_info
*fs_info
,
545 struct extent_buffer
*eb
, int dst_slot
, int src_slot
,
548 struct tree_mod_elem
*tm
= NULL
;
549 struct tree_mod_elem
**tm_list
= NULL
;
554 if (!tree_mod_need_log(fs_info
, eb
))
557 tm_list
= kcalloc(nr_items
, sizeof(struct tree_mod_elem
*), GFP_NOFS
);
561 tm
= kzalloc(sizeof(*tm
), GFP_NOFS
);
567 tm
->logical
= eb
->start
;
569 tm
->move
.dst_slot
= dst_slot
;
570 tm
->move
.nr_items
= nr_items
;
571 tm
->op
= MOD_LOG_MOVE_KEYS
;
573 for (i
= 0; i
+ dst_slot
< src_slot
&& i
< nr_items
; i
++) {
574 tm_list
[i
] = alloc_tree_mod_elem(eb
, i
+ dst_slot
,
575 MOD_LOG_KEY_REMOVE_WHILE_MOVING
, GFP_NOFS
);
582 if (tree_mod_dont_log(fs_info
, eb
))
587 * When we override something during the move, we log these removals.
588 * This can only happen when we move towards the beginning of the
589 * buffer, i.e. dst_slot < src_slot.
591 for (i
= 0; i
+ dst_slot
< src_slot
&& i
< nr_items
; i
++) {
592 ret
= __tree_mod_log_insert(fs_info
, tm_list
[i
]);
597 ret
= __tree_mod_log_insert(fs_info
, tm
);
600 write_unlock(&eb
->fs_info
->tree_mod_log_lock
);
605 for (i
= 0; i
< nr_items
; i
++) {
606 if (tm_list
[i
] && !RB_EMPTY_NODE(&tm_list
[i
]->node
))
607 rb_erase(&tm_list
[i
]->node
, &fs_info
->tree_mod_log
);
611 write_unlock(&eb
->fs_info
->tree_mod_log_lock
);
619 __tree_mod_log_free_eb(struct btrfs_fs_info
*fs_info
,
620 struct tree_mod_elem
**tm_list
,
626 for (i
= nritems
- 1; i
>= 0; i
--) {
627 ret
= __tree_mod_log_insert(fs_info
, tm_list
[i
]);
629 for (j
= nritems
- 1; j
> i
; j
--)
630 rb_erase(&tm_list
[j
]->node
,
631 &fs_info
->tree_mod_log
);
640 tree_mod_log_insert_root(struct btrfs_fs_info
*fs_info
,
641 struct extent_buffer
*old_root
,
642 struct extent_buffer
*new_root
,
645 struct tree_mod_elem
*tm
= NULL
;
646 struct tree_mod_elem
**tm_list
= NULL
;
651 if (!tree_mod_need_log(fs_info
, NULL
))
654 if (log_removal
&& btrfs_header_level(old_root
) > 0) {
655 nritems
= btrfs_header_nritems(old_root
);
656 tm_list
= kcalloc(nritems
, sizeof(struct tree_mod_elem
*),
662 for (i
= 0; i
< nritems
; i
++) {
663 tm_list
[i
] = alloc_tree_mod_elem(old_root
, i
,
664 MOD_LOG_KEY_REMOVE_WHILE_FREEING
, GFP_NOFS
);
672 tm
= kzalloc(sizeof(*tm
), GFP_NOFS
);
678 tm
->logical
= new_root
->start
;
679 tm
->old_root
.logical
= old_root
->start
;
680 tm
->old_root
.level
= btrfs_header_level(old_root
);
681 tm
->generation
= btrfs_header_generation(old_root
);
682 tm
->op
= MOD_LOG_ROOT_REPLACE
;
684 if (tree_mod_dont_log(fs_info
, NULL
))
688 ret
= __tree_mod_log_free_eb(fs_info
, tm_list
, nritems
);
690 ret
= __tree_mod_log_insert(fs_info
, tm
);
692 write_unlock(&fs_info
->tree_mod_log_lock
);
701 for (i
= 0; i
< nritems
; i
++)
710 static struct tree_mod_elem
*
711 __tree_mod_log_search(struct btrfs_fs_info
*fs_info
, u64 start
, u64 min_seq
,
714 struct rb_root
*tm_root
;
715 struct rb_node
*node
;
716 struct tree_mod_elem
*cur
= NULL
;
717 struct tree_mod_elem
*found
= NULL
;
719 read_lock(&fs_info
->tree_mod_log_lock
);
720 tm_root
= &fs_info
->tree_mod_log
;
721 node
= tm_root
->rb_node
;
723 cur
= rb_entry(node
, struct tree_mod_elem
, node
);
724 if (cur
->logical
< start
) {
725 node
= node
->rb_left
;
726 } else if (cur
->logical
> start
) {
727 node
= node
->rb_right
;
728 } else if (cur
->seq
< min_seq
) {
729 node
= node
->rb_left
;
730 } else if (!smallest
) {
731 /* we want the node with the highest seq */
733 BUG_ON(found
->seq
> cur
->seq
);
735 node
= node
->rb_left
;
736 } else if (cur
->seq
> min_seq
) {
737 /* we want the node with the smallest seq */
739 BUG_ON(found
->seq
< cur
->seq
);
741 node
= node
->rb_right
;
747 read_unlock(&fs_info
->tree_mod_log_lock
);
753 * this returns the element from the log with the smallest time sequence
754 * value that's in the log (the oldest log item). any element with a time
755 * sequence lower than min_seq will be ignored.
757 static struct tree_mod_elem
*
758 tree_mod_log_search_oldest(struct btrfs_fs_info
*fs_info
, u64 start
,
761 return __tree_mod_log_search(fs_info
, start
, min_seq
, 1);
765 * this returns the element from the log with the largest time sequence
766 * value that's in the log (the most recent log item). any element with
767 * a time sequence lower than min_seq will be ignored.
769 static struct tree_mod_elem
*
770 tree_mod_log_search(struct btrfs_fs_info
*fs_info
, u64 start
, u64 min_seq
)
772 return __tree_mod_log_search(fs_info
, start
, min_seq
, 0);
776 tree_mod_log_eb_copy(struct btrfs_fs_info
*fs_info
, struct extent_buffer
*dst
,
777 struct extent_buffer
*src
, unsigned long dst_offset
,
778 unsigned long src_offset
, int nr_items
)
781 struct tree_mod_elem
**tm_list
= NULL
;
782 struct tree_mod_elem
**tm_list_add
, **tm_list_rem
;
786 if (!tree_mod_need_log(fs_info
, NULL
))
789 if (btrfs_header_level(dst
) == 0 && btrfs_header_level(src
) == 0)
792 tm_list
= kcalloc(nr_items
* 2, sizeof(struct tree_mod_elem
*),
797 tm_list_add
= tm_list
;
798 tm_list_rem
= tm_list
+ nr_items
;
799 for (i
= 0; i
< nr_items
; i
++) {
800 tm_list_rem
[i
] = alloc_tree_mod_elem(src
, i
+ src_offset
,
801 MOD_LOG_KEY_REMOVE
, GFP_NOFS
);
802 if (!tm_list_rem
[i
]) {
807 tm_list_add
[i
] = alloc_tree_mod_elem(dst
, i
+ dst_offset
,
808 MOD_LOG_KEY_ADD
, GFP_NOFS
);
809 if (!tm_list_add
[i
]) {
815 if (tree_mod_dont_log(fs_info
, NULL
))
819 for (i
= 0; i
< nr_items
; i
++) {
820 ret
= __tree_mod_log_insert(fs_info
, tm_list_rem
[i
]);
823 ret
= __tree_mod_log_insert(fs_info
, tm_list_add
[i
]);
828 write_unlock(&fs_info
->tree_mod_log_lock
);
834 for (i
= 0; i
< nr_items
* 2; i
++) {
835 if (tm_list
[i
] && !RB_EMPTY_NODE(&tm_list
[i
]->node
))
836 rb_erase(&tm_list
[i
]->node
, &fs_info
->tree_mod_log
);
840 write_unlock(&fs_info
->tree_mod_log_lock
);
847 tree_mod_log_eb_move(struct btrfs_fs_info
*fs_info
, struct extent_buffer
*dst
,
848 int dst_offset
, int src_offset
, int nr_items
)
851 ret
= tree_mod_log_insert_move(fs_info
, dst
, dst_offset
, src_offset
,
857 tree_mod_log_set_node_key(struct btrfs_fs_info
*fs_info
,
858 struct extent_buffer
*eb
, int slot
, int atomic
)
862 ret
= tree_mod_log_insert_key(fs_info
, eb
, slot
,
864 atomic
? GFP_ATOMIC
: GFP_NOFS
);
869 tree_mod_log_free_eb(struct btrfs_fs_info
*fs_info
, struct extent_buffer
*eb
)
871 struct tree_mod_elem
**tm_list
= NULL
;
876 if (btrfs_header_level(eb
) == 0)
879 if (!tree_mod_need_log(fs_info
, NULL
))
882 nritems
= btrfs_header_nritems(eb
);
883 tm_list
= kcalloc(nritems
, sizeof(struct tree_mod_elem
*), GFP_NOFS
);
887 for (i
= 0; i
< nritems
; i
++) {
888 tm_list
[i
] = alloc_tree_mod_elem(eb
, i
,
889 MOD_LOG_KEY_REMOVE_WHILE_FREEING
, GFP_NOFS
);
896 if (tree_mod_dont_log(fs_info
, eb
))
899 ret
= __tree_mod_log_free_eb(fs_info
, tm_list
, nritems
);
900 write_unlock(&eb
->fs_info
->tree_mod_log_lock
);
908 for (i
= 0; i
< nritems
; i
++)
916 tree_mod_log_set_root_pointer(struct btrfs_root
*root
,
917 struct extent_buffer
*new_root_node
,
921 ret
= tree_mod_log_insert_root(root
->fs_info
, root
->node
,
922 new_root_node
, log_removal
);
927 * check if the tree block can be shared by multiple trees
929 int btrfs_block_can_be_shared(struct btrfs_root
*root
,
930 struct extent_buffer
*buf
)
933 * Tree blocks not in reference counted trees and tree roots
934 * are never shared. If a block was allocated after the last
935 * snapshot and the block was not allocated by tree relocation,
936 * we know the block is not shared.
938 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) &&
939 buf
!= root
->node
&& buf
!= root
->commit_root
&&
940 (btrfs_header_generation(buf
) <=
941 btrfs_root_last_snapshot(&root
->root_item
) ||
942 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)))
944 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
945 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) &&
946 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
952 static noinline
int update_ref_for_cow(struct btrfs_trans_handle
*trans
,
953 struct btrfs_root
*root
,
954 struct extent_buffer
*buf
,
955 struct extent_buffer
*cow
,
958 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
966 * Backrefs update rules:
968 * Always use full backrefs for extent pointers in tree block
969 * allocated by tree relocation.
971 * If a shared tree block is no longer referenced by its owner
972 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
973 * use full backrefs for extent pointers in tree block.
975 * If a tree block is been relocating
976 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
977 * use full backrefs for extent pointers in tree block.
978 * The reason for this is some operations (such as drop tree)
979 * are only allowed for blocks use full backrefs.
982 if (btrfs_block_can_be_shared(root
, buf
)) {
983 ret
= btrfs_lookup_extent_info(trans
, fs_info
, buf
->start
,
984 btrfs_header_level(buf
), 1,
990 btrfs_handle_fs_error(fs_info
, ret
, NULL
);
995 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
||
996 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
997 flags
= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
1002 owner
= btrfs_header_owner(buf
);
1003 BUG_ON(owner
== BTRFS_TREE_RELOC_OBJECTID
&&
1004 !(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
));
1007 if ((owner
== root
->root_key
.objectid
||
1008 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) &&
1009 !(flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
)) {
1010 ret
= btrfs_inc_ref(trans
, root
, buf
, 1);
1014 if (root
->root_key
.objectid
==
1015 BTRFS_TREE_RELOC_OBJECTID
) {
1016 ret
= btrfs_dec_ref(trans
, root
, buf
, 0);
1019 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
1023 new_flags
|= BTRFS_BLOCK_FLAG_FULL_BACKREF
;
1026 if (root
->root_key
.objectid
==
1027 BTRFS_TREE_RELOC_OBJECTID
)
1028 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
1030 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
1034 if (new_flags
!= 0) {
1035 int level
= btrfs_header_level(buf
);
1037 ret
= btrfs_set_disk_extent_flags(trans
, fs_info
,
1040 new_flags
, level
, 0);
1045 if (flags
& BTRFS_BLOCK_FLAG_FULL_BACKREF
) {
1046 if (root
->root_key
.objectid
==
1047 BTRFS_TREE_RELOC_OBJECTID
)
1048 ret
= btrfs_inc_ref(trans
, root
, cow
, 1);
1050 ret
= btrfs_inc_ref(trans
, root
, cow
, 0);
1053 ret
= btrfs_dec_ref(trans
, root
, buf
, 1);
1057 clean_tree_block(fs_info
, buf
);
1064 * does the dirty work in cow of a single block. The parent block (if
1065 * supplied) is updated to point to the new cow copy. The new buffer is marked
1066 * dirty and returned locked. If you modify the block it needs to be marked
1069 * search_start -- an allocation hint for the new block
1071 * empty_size -- a hint that you plan on doing more cow. This is the size in
1072 * bytes the allocator should try to find free next to the block it returns.
1073 * This is just a hint and may be ignored by the allocator.
1075 static noinline
int __btrfs_cow_block(struct btrfs_trans_handle
*trans
,
1076 struct btrfs_root
*root
,
1077 struct extent_buffer
*buf
,
1078 struct extent_buffer
*parent
, int parent_slot
,
1079 struct extent_buffer
**cow_ret
,
1080 u64 search_start
, u64 empty_size
)
1082 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1083 struct btrfs_disk_key disk_key
;
1084 struct extent_buffer
*cow
;
1087 int unlock_orig
= 0;
1088 u64 parent_start
= 0;
1090 if (*cow_ret
== buf
)
1093 btrfs_assert_tree_locked(buf
);
1095 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) &&
1096 trans
->transid
!= fs_info
->running_transaction
->transid
);
1097 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) &&
1098 trans
->transid
!= root
->last_trans
);
1100 level
= btrfs_header_level(buf
);
1103 btrfs_item_key(buf
, &disk_key
, 0);
1105 btrfs_node_key(buf
, &disk_key
, 0);
1107 if ((root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
) && parent
)
1108 parent_start
= parent
->start
;
1110 cow
= btrfs_alloc_tree_block(trans
, root
, parent_start
,
1111 root
->root_key
.objectid
, &disk_key
, level
,
1112 search_start
, empty_size
);
1114 return PTR_ERR(cow
);
1116 /* cow is set to blocking by btrfs_init_new_buffer */
1118 copy_extent_buffer_full(cow
, buf
);
1119 btrfs_set_header_bytenr(cow
, cow
->start
);
1120 btrfs_set_header_generation(cow
, trans
->transid
);
1121 btrfs_set_header_backref_rev(cow
, BTRFS_MIXED_BACKREF_REV
);
1122 btrfs_clear_header_flag(cow
, BTRFS_HEADER_FLAG_WRITTEN
|
1123 BTRFS_HEADER_FLAG_RELOC
);
1124 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
)
1125 btrfs_set_header_flag(cow
, BTRFS_HEADER_FLAG_RELOC
);
1127 btrfs_set_header_owner(cow
, root
->root_key
.objectid
);
1129 write_extent_buffer_fsid(cow
, fs_info
->fsid
);
1131 ret
= update_ref_for_cow(trans
, root
, buf
, cow
, &last_ref
);
1133 btrfs_tree_unlock(cow
);
1134 free_extent_buffer(cow
);
1135 btrfs_abort_transaction(trans
, ret
);
1139 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
)) {
1140 ret
= btrfs_reloc_cow_block(trans
, root
, buf
, cow
);
1142 btrfs_tree_unlock(cow
);
1143 free_extent_buffer(cow
);
1144 btrfs_abort_transaction(trans
, ret
);
1149 if (buf
== root
->node
) {
1150 WARN_ON(parent
&& parent
!= buf
);
1151 if (root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
||
1152 btrfs_header_backref_rev(buf
) < BTRFS_MIXED_BACKREF_REV
)
1153 parent_start
= buf
->start
;
1155 extent_buffer_get(cow
);
1156 tree_mod_log_set_root_pointer(root
, cow
, 1);
1157 rcu_assign_pointer(root
->node
, cow
);
1159 btrfs_free_tree_block(trans
, root
, buf
, parent_start
,
1161 free_extent_buffer(buf
);
1162 add_root_to_dirty_list(root
);
1164 WARN_ON(trans
->transid
!= btrfs_header_generation(parent
));
1165 tree_mod_log_insert_key(fs_info
, parent
, parent_slot
,
1166 MOD_LOG_KEY_REPLACE
, GFP_NOFS
);
1167 btrfs_set_node_blockptr(parent
, parent_slot
,
1169 btrfs_set_node_ptr_generation(parent
, parent_slot
,
1171 btrfs_mark_buffer_dirty(parent
);
1173 ret
= tree_mod_log_free_eb(fs_info
, buf
);
1175 btrfs_tree_unlock(cow
);
1176 free_extent_buffer(cow
);
1177 btrfs_abort_transaction(trans
, ret
);
1181 btrfs_free_tree_block(trans
, root
, buf
, parent_start
,
1185 btrfs_tree_unlock(buf
);
1186 free_extent_buffer_stale(buf
);
1187 btrfs_mark_buffer_dirty(cow
);
1193 * returns the logical address of the oldest predecessor of the given root.
1194 * entries older than time_seq are ignored.
1196 static struct tree_mod_elem
*
1197 __tree_mod_log_oldest_root(struct btrfs_fs_info
*fs_info
,
1198 struct extent_buffer
*eb_root
, u64 time_seq
)
1200 struct tree_mod_elem
*tm
;
1201 struct tree_mod_elem
*found
= NULL
;
1202 u64 root_logical
= eb_root
->start
;
1209 * the very last operation that's logged for a root is the
1210 * replacement operation (if it is replaced at all). this has
1211 * the logical address of the *new* root, making it the very
1212 * first operation that's logged for this root.
1215 tm
= tree_mod_log_search_oldest(fs_info
, root_logical
,
1220 * if there are no tree operation for the oldest root, we simply
1221 * return it. this should only happen if that (old) root is at
1228 * if there's an operation that's not a root replacement, we
1229 * found the oldest version of our root. normally, we'll find a
1230 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1232 if (tm
->op
!= MOD_LOG_ROOT_REPLACE
)
1236 root_logical
= tm
->old_root
.logical
;
1240 /* if there's no old root to return, return what we found instead */
1248 * tm is a pointer to the first operation to rewind within eb. then, all
1249 * previous operations will be rewound (until we reach something older than
1253 __tree_mod_log_rewind(struct btrfs_fs_info
*fs_info
, struct extent_buffer
*eb
,
1254 u64 time_seq
, struct tree_mod_elem
*first_tm
)
1257 struct rb_node
*next
;
1258 struct tree_mod_elem
*tm
= first_tm
;
1259 unsigned long o_dst
;
1260 unsigned long o_src
;
1261 unsigned long p_size
= sizeof(struct btrfs_key_ptr
);
1263 n
= btrfs_header_nritems(eb
);
1264 read_lock(&fs_info
->tree_mod_log_lock
);
1265 while (tm
&& tm
->seq
>= time_seq
) {
1267 * all the operations are recorded with the operator used for
1268 * the modification. as we're going backwards, we do the
1269 * opposite of each operation here.
1272 case MOD_LOG_KEY_REMOVE_WHILE_FREEING
:
1273 BUG_ON(tm
->slot
< n
);
1275 case MOD_LOG_KEY_REMOVE_WHILE_MOVING
:
1276 case MOD_LOG_KEY_REMOVE
:
1277 btrfs_set_node_key(eb
, &tm
->key
, tm
->slot
);
1278 btrfs_set_node_blockptr(eb
, tm
->slot
, tm
->blockptr
);
1279 btrfs_set_node_ptr_generation(eb
, tm
->slot
,
1283 case MOD_LOG_KEY_REPLACE
:
1284 BUG_ON(tm
->slot
>= n
);
1285 btrfs_set_node_key(eb
, &tm
->key
, tm
->slot
);
1286 btrfs_set_node_blockptr(eb
, tm
->slot
, tm
->blockptr
);
1287 btrfs_set_node_ptr_generation(eb
, tm
->slot
,
1290 case MOD_LOG_KEY_ADD
:
1291 /* if a move operation is needed it's in the log */
1294 case MOD_LOG_MOVE_KEYS
:
1295 o_dst
= btrfs_node_key_ptr_offset(tm
->slot
);
1296 o_src
= btrfs_node_key_ptr_offset(tm
->move
.dst_slot
);
1297 memmove_extent_buffer(eb
, o_dst
, o_src
,
1298 tm
->move
.nr_items
* p_size
);
1300 case MOD_LOG_ROOT_REPLACE
:
1302 * this operation is special. for roots, this must be
1303 * handled explicitly before rewinding.
1304 * for non-roots, this operation may exist if the node
1305 * was a root: root A -> child B; then A gets empty and
1306 * B is promoted to the new root. in the mod log, we'll
1307 * have a root-replace operation for B, a tree block
1308 * that is no root. we simply ignore that operation.
1312 next
= rb_next(&tm
->node
);
1315 tm
= rb_entry(next
, struct tree_mod_elem
, node
);
1316 if (tm
->logical
!= first_tm
->logical
)
1319 read_unlock(&fs_info
->tree_mod_log_lock
);
1320 btrfs_set_header_nritems(eb
, n
);
1324 * Called with eb read locked. If the buffer cannot be rewound, the same buffer
1325 * is returned. If rewind operations happen, a fresh buffer is returned. The
1326 * returned buffer is always read-locked. If the returned buffer is not the
1327 * input buffer, the lock on the input buffer is released and the input buffer
1328 * is freed (its refcount is decremented).
1330 static struct extent_buffer
*
1331 tree_mod_log_rewind(struct btrfs_fs_info
*fs_info
, struct btrfs_path
*path
,
1332 struct extent_buffer
*eb
, u64 time_seq
)
1334 struct extent_buffer
*eb_rewin
;
1335 struct tree_mod_elem
*tm
;
1340 if (btrfs_header_level(eb
) == 0)
1343 tm
= tree_mod_log_search(fs_info
, eb
->start
, time_seq
);
1347 btrfs_set_path_blocking(path
);
1348 btrfs_set_lock_blocking_rw(eb
, BTRFS_READ_LOCK
);
1350 if (tm
->op
== MOD_LOG_KEY_REMOVE_WHILE_FREEING
) {
1351 BUG_ON(tm
->slot
!= 0);
1352 eb_rewin
= alloc_dummy_extent_buffer(fs_info
, eb
->start
);
1354 btrfs_tree_read_unlock_blocking(eb
);
1355 free_extent_buffer(eb
);
1358 btrfs_set_header_bytenr(eb_rewin
, eb
->start
);
1359 btrfs_set_header_backref_rev(eb_rewin
,
1360 btrfs_header_backref_rev(eb
));
1361 btrfs_set_header_owner(eb_rewin
, btrfs_header_owner(eb
));
1362 btrfs_set_header_level(eb_rewin
, btrfs_header_level(eb
));
1364 eb_rewin
= btrfs_clone_extent_buffer(eb
);
1366 btrfs_tree_read_unlock_blocking(eb
);
1367 free_extent_buffer(eb
);
1372 btrfs_clear_path_blocking(path
, NULL
, BTRFS_READ_LOCK
);
1373 btrfs_tree_read_unlock_blocking(eb
);
1374 free_extent_buffer(eb
);
1376 btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb_rewin
),
1377 eb_rewin
, btrfs_header_level(eb_rewin
));
1378 btrfs_tree_read_lock(eb_rewin
);
1379 __tree_mod_log_rewind(fs_info
, eb_rewin
, time_seq
, tm
);
1380 WARN_ON(btrfs_header_nritems(eb_rewin
) >
1381 BTRFS_NODEPTRS_PER_BLOCK(fs_info
));
1387 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1388 * value. If there are no changes, the current root->root_node is returned. If
1389 * anything changed in between, there's a fresh buffer allocated on which the
1390 * rewind operations are done. In any case, the returned buffer is read locked.
1391 * Returns NULL on error (with no locks held).
1393 static inline struct extent_buffer
*
1394 get_old_root(struct btrfs_root
*root
, u64 time_seq
)
1396 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1397 struct tree_mod_elem
*tm
;
1398 struct extent_buffer
*eb
= NULL
;
1399 struct extent_buffer
*eb_root
;
1400 u64 eb_root_owner
= 0;
1401 struct extent_buffer
*old
;
1402 struct tree_mod_root
*old_root
= NULL
;
1403 u64 old_generation
= 0;
1406 eb_root
= btrfs_read_lock_root_node(root
);
1407 tm
= __tree_mod_log_oldest_root(fs_info
, eb_root
, time_seq
);
1411 if (tm
->op
== MOD_LOG_ROOT_REPLACE
) {
1412 old_root
= &tm
->old_root
;
1413 old_generation
= tm
->generation
;
1414 logical
= old_root
->logical
;
1416 logical
= eb_root
->start
;
1419 tm
= tree_mod_log_search(fs_info
, logical
, time_seq
);
1420 if (old_root
&& tm
&& tm
->op
!= MOD_LOG_KEY_REMOVE_WHILE_FREEING
) {
1421 btrfs_tree_read_unlock(eb_root
);
1422 free_extent_buffer(eb_root
);
1423 old
= read_tree_block(fs_info
, logical
, 0);
1424 if (WARN_ON(IS_ERR(old
) || !extent_buffer_uptodate(old
))) {
1426 free_extent_buffer(old
);
1428 "failed to read tree block %llu from get_old_root",
1431 eb
= btrfs_clone_extent_buffer(old
);
1432 free_extent_buffer(old
);
1434 } else if (old_root
) {
1435 eb_root_owner
= btrfs_header_owner(eb_root
);
1436 btrfs_tree_read_unlock(eb_root
);
1437 free_extent_buffer(eb_root
);
1438 eb
= alloc_dummy_extent_buffer(fs_info
, logical
);
1440 btrfs_set_lock_blocking_rw(eb_root
, BTRFS_READ_LOCK
);
1441 eb
= btrfs_clone_extent_buffer(eb_root
);
1442 btrfs_tree_read_unlock_blocking(eb_root
);
1443 free_extent_buffer(eb_root
);
1449 btrfs_set_header_bytenr(eb
, eb
->start
);
1450 btrfs_set_header_backref_rev(eb
, BTRFS_MIXED_BACKREF_REV
);
1451 btrfs_set_header_owner(eb
, eb_root_owner
);
1452 btrfs_set_header_level(eb
, old_root
->level
);
1453 btrfs_set_header_generation(eb
, old_generation
);
1455 btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb
), eb
,
1456 btrfs_header_level(eb
));
1457 btrfs_tree_read_lock(eb
);
1459 __tree_mod_log_rewind(fs_info
, eb
, time_seq
, tm
);
1461 WARN_ON(btrfs_header_level(eb
) != 0);
1462 WARN_ON(btrfs_header_nritems(eb
) > BTRFS_NODEPTRS_PER_BLOCK(fs_info
));
1467 int btrfs_old_root_level(struct btrfs_root
*root
, u64 time_seq
)
1469 struct tree_mod_elem
*tm
;
1471 struct extent_buffer
*eb_root
= btrfs_root_node(root
);
1473 tm
= __tree_mod_log_oldest_root(root
->fs_info
, eb_root
, time_seq
);
1474 if (tm
&& tm
->op
== MOD_LOG_ROOT_REPLACE
) {
1475 level
= tm
->old_root
.level
;
1477 level
= btrfs_header_level(eb_root
);
1479 free_extent_buffer(eb_root
);
1484 static inline int should_cow_block(struct btrfs_trans_handle
*trans
,
1485 struct btrfs_root
*root
,
1486 struct extent_buffer
*buf
)
1488 if (btrfs_is_testing(root
->fs_info
))
1491 /* ensure we can see the force_cow */
1495 * We do not need to cow a block if
1496 * 1) this block is not created or changed in this transaction;
1497 * 2) this block does not belong to TREE_RELOC tree;
1498 * 3) the root is not forced COW.
1500 * What is forced COW:
1501 * when we create snapshot during committing the transaction,
1502 * after we've finished coping src root, we must COW the shared
1503 * block to ensure the metadata consistency.
1505 if (btrfs_header_generation(buf
) == trans
->transid
&&
1506 !btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_WRITTEN
) &&
1507 !(root
->root_key
.objectid
!= BTRFS_TREE_RELOC_OBJECTID
&&
1508 btrfs_header_flag(buf
, BTRFS_HEADER_FLAG_RELOC
)) &&
1509 !test_bit(BTRFS_ROOT_FORCE_COW
, &root
->state
))
1515 * cows a single block, see __btrfs_cow_block for the real work.
1516 * This version of it has extra checks so that a block isn't COWed more than
1517 * once per transaction, as long as it hasn't been written yet
1519 noinline
int btrfs_cow_block(struct btrfs_trans_handle
*trans
,
1520 struct btrfs_root
*root
, struct extent_buffer
*buf
,
1521 struct extent_buffer
*parent
, int parent_slot
,
1522 struct extent_buffer
**cow_ret
)
1524 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1528 if (trans
->transaction
!= fs_info
->running_transaction
)
1529 WARN(1, KERN_CRIT
"trans %llu running %llu\n",
1531 fs_info
->running_transaction
->transid
);
1533 if (trans
->transid
!= fs_info
->generation
)
1534 WARN(1, KERN_CRIT
"trans %llu running %llu\n",
1535 trans
->transid
, fs_info
->generation
);
1537 if (!should_cow_block(trans
, root
, buf
)) {
1538 trans
->dirty
= true;
1543 search_start
= buf
->start
& ~((u64
)SZ_1G
- 1);
1546 btrfs_set_lock_blocking(parent
);
1547 btrfs_set_lock_blocking(buf
);
1549 ret
= __btrfs_cow_block(trans
, root
, buf
, parent
,
1550 parent_slot
, cow_ret
, search_start
, 0);
1552 trace_btrfs_cow_block(root
, buf
, *cow_ret
);
1558 * helper function for defrag to decide if two blocks pointed to by a
1559 * node are actually close by
1561 static int close_blocks(u64 blocknr
, u64 other
, u32 blocksize
)
1563 if (blocknr
< other
&& other
- (blocknr
+ blocksize
) < 32768)
1565 if (blocknr
> other
&& blocknr
- (other
+ blocksize
) < 32768)
1571 * compare two keys in a memcmp fashion
1573 static int comp_keys(const struct btrfs_disk_key
*disk
,
1574 const struct btrfs_key
*k2
)
1576 struct btrfs_key k1
;
1578 btrfs_disk_key_to_cpu(&k1
, disk
);
1580 return btrfs_comp_cpu_keys(&k1
, k2
);
1584 * same as comp_keys only with two btrfs_key's
1586 int btrfs_comp_cpu_keys(const struct btrfs_key
*k1
, const struct btrfs_key
*k2
)
1588 if (k1
->objectid
> k2
->objectid
)
1590 if (k1
->objectid
< k2
->objectid
)
1592 if (k1
->type
> k2
->type
)
1594 if (k1
->type
< k2
->type
)
1596 if (k1
->offset
> k2
->offset
)
1598 if (k1
->offset
< k2
->offset
)
1604 * this is used by the defrag code to go through all the
1605 * leaves pointed to by a node and reallocate them so that
1606 * disk order is close to key order
1608 int btrfs_realloc_node(struct btrfs_trans_handle
*trans
,
1609 struct btrfs_root
*root
, struct extent_buffer
*parent
,
1610 int start_slot
, u64
*last_ret
,
1611 struct btrfs_key
*progress
)
1613 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1614 struct extent_buffer
*cur
;
1617 u64 search_start
= *last_ret
;
1627 int progress_passed
= 0;
1628 struct btrfs_disk_key disk_key
;
1630 parent_level
= btrfs_header_level(parent
);
1632 WARN_ON(trans
->transaction
!= fs_info
->running_transaction
);
1633 WARN_ON(trans
->transid
!= fs_info
->generation
);
1635 parent_nritems
= btrfs_header_nritems(parent
);
1636 blocksize
= fs_info
->nodesize
;
1637 end_slot
= parent_nritems
- 1;
1639 if (parent_nritems
<= 1)
1642 btrfs_set_lock_blocking(parent
);
1644 for (i
= start_slot
; i
<= end_slot
; i
++) {
1647 btrfs_node_key(parent
, &disk_key
, i
);
1648 if (!progress_passed
&& comp_keys(&disk_key
, progress
) < 0)
1651 progress_passed
= 1;
1652 blocknr
= btrfs_node_blockptr(parent
, i
);
1653 gen
= btrfs_node_ptr_generation(parent
, i
);
1654 if (last_block
== 0)
1655 last_block
= blocknr
;
1658 other
= btrfs_node_blockptr(parent
, i
- 1);
1659 close
= close_blocks(blocknr
, other
, blocksize
);
1661 if (!close
&& i
< end_slot
) {
1662 other
= btrfs_node_blockptr(parent
, i
+ 1);
1663 close
= close_blocks(blocknr
, other
, blocksize
);
1666 last_block
= blocknr
;
1670 cur
= find_extent_buffer(fs_info
, blocknr
);
1672 uptodate
= btrfs_buffer_uptodate(cur
, gen
, 0);
1675 if (!cur
|| !uptodate
) {
1677 cur
= read_tree_block(fs_info
, blocknr
, gen
);
1679 return PTR_ERR(cur
);
1680 } else if (!extent_buffer_uptodate(cur
)) {
1681 free_extent_buffer(cur
);
1684 } else if (!uptodate
) {
1685 err
= btrfs_read_buffer(cur
, gen
);
1687 free_extent_buffer(cur
);
1692 if (search_start
== 0)
1693 search_start
= last_block
;
1695 btrfs_tree_lock(cur
);
1696 btrfs_set_lock_blocking(cur
);
1697 err
= __btrfs_cow_block(trans
, root
, cur
, parent
, i
,
1700 (end_slot
- i
) * blocksize
));
1702 btrfs_tree_unlock(cur
);
1703 free_extent_buffer(cur
);
1706 search_start
= cur
->start
;
1707 last_block
= cur
->start
;
1708 *last_ret
= search_start
;
1709 btrfs_tree_unlock(cur
);
1710 free_extent_buffer(cur
);
1716 * search for key in the extent_buffer. The items start at offset p,
1717 * and they are item_size apart. There are 'max' items in p.
1719 * the slot in the array is returned via slot, and it points to
1720 * the place where you would insert key if it is not found in
1723 * slot may point to max if the key is bigger than all of the keys
1725 static noinline
int generic_bin_search(struct extent_buffer
*eb
,
1726 unsigned long p
, int item_size
,
1727 const struct btrfs_key
*key
,
1734 struct btrfs_disk_key
*tmp
= NULL
;
1735 struct btrfs_disk_key unaligned
;
1736 unsigned long offset
;
1738 unsigned long map_start
= 0;
1739 unsigned long map_len
= 0;
1743 btrfs_err(eb
->fs_info
,
1744 "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
1745 __func__
, low
, high
, eb
->start
,
1746 btrfs_header_owner(eb
), btrfs_header_level(eb
));
1750 while (low
< high
) {
1751 mid
= (low
+ high
) / 2;
1752 offset
= p
+ mid
* item_size
;
1754 if (!kaddr
|| offset
< map_start
||
1755 (offset
+ sizeof(struct btrfs_disk_key
)) >
1756 map_start
+ map_len
) {
1758 err
= map_private_extent_buffer(eb
, offset
,
1759 sizeof(struct btrfs_disk_key
),
1760 &kaddr
, &map_start
, &map_len
);
1763 tmp
= (struct btrfs_disk_key
*)(kaddr
+ offset
-
1765 } else if (err
== 1) {
1766 read_extent_buffer(eb
, &unaligned
,
1767 offset
, sizeof(unaligned
));
1774 tmp
= (struct btrfs_disk_key
*)(kaddr
+ offset
-
1777 ret
= comp_keys(tmp
, key
);
1793 * simple bin_search frontend that does the right thing for
1796 static int bin_search(struct extent_buffer
*eb
, const struct btrfs_key
*key
,
1797 int level
, int *slot
)
1800 return generic_bin_search(eb
,
1801 offsetof(struct btrfs_leaf
, items
),
1802 sizeof(struct btrfs_item
),
1803 key
, btrfs_header_nritems(eb
),
1806 return generic_bin_search(eb
,
1807 offsetof(struct btrfs_node
, ptrs
),
1808 sizeof(struct btrfs_key_ptr
),
1809 key
, btrfs_header_nritems(eb
),
1813 int btrfs_bin_search(struct extent_buffer
*eb
, const struct btrfs_key
*key
,
1814 int level
, int *slot
)
1816 return bin_search(eb
, key
, level
, slot
);
1819 static void root_add_used(struct btrfs_root
*root
, u32 size
)
1821 spin_lock(&root
->accounting_lock
);
1822 btrfs_set_root_used(&root
->root_item
,
1823 btrfs_root_used(&root
->root_item
) + size
);
1824 spin_unlock(&root
->accounting_lock
);
1827 static void root_sub_used(struct btrfs_root
*root
, u32 size
)
1829 spin_lock(&root
->accounting_lock
);
1830 btrfs_set_root_used(&root
->root_item
,
1831 btrfs_root_used(&root
->root_item
) - size
);
1832 spin_unlock(&root
->accounting_lock
);
1835 /* given a node and slot number, this reads the blocks it points to. The
1836 * extent buffer is returned with a reference taken (but unlocked).
1838 static noinline
struct extent_buffer
*
1839 read_node_slot(struct btrfs_fs_info
*fs_info
, struct extent_buffer
*parent
,
1842 int level
= btrfs_header_level(parent
);
1843 struct extent_buffer
*eb
;
1845 if (slot
< 0 || slot
>= btrfs_header_nritems(parent
))
1846 return ERR_PTR(-ENOENT
);
1850 eb
= read_tree_block(fs_info
, btrfs_node_blockptr(parent
, slot
),
1851 btrfs_node_ptr_generation(parent
, slot
));
1852 if (!IS_ERR(eb
) && !extent_buffer_uptodate(eb
)) {
1853 free_extent_buffer(eb
);
1861 * node level balancing, used to make sure nodes are in proper order for
1862 * item deletion. We balance from the top down, so we have to make sure
1863 * that a deletion won't leave an node completely empty later on.
1865 static noinline
int balance_level(struct btrfs_trans_handle
*trans
,
1866 struct btrfs_root
*root
,
1867 struct btrfs_path
*path
, int level
)
1869 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1870 struct extent_buffer
*right
= NULL
;
1871 struct extent_buffer
*mid
;
1872 struct extent_buffer
*left
= NULL
;
1873 struct extent_buffer
*parent
= NULL
;
1877 int orig_slot
= path
->slots
[level
];
1883 mid
= path
->nodes
[level
];
1885 WARN_ON(path
->locks
[level
] != BTRFS_WRITE_LOCK
&&
1886 path
->locks
[level
] != BTRFS_WRITE_LOCK_BLOCKING
);
1887 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
1889 orig_ptr
= btrfs_node_blockptr(mid
, orig_slot
);
1891 if (level
< BTRFS_MAX_LEVEL
- 1) {
1892 parent
= path
->nodes
[level
+ 1];
1893 pslot
= path
->slots
[level
+ 1];
1897 * deal with the case where there is only one pointer in the root
1898 * by promoting the node below to a root
1901 struct extent_buffer
*child
;
1903 if (btrfs_header_nritems(mid
) != 1)
1906 /* promote the child to a root */
1907 child
= read_node_slot(fs_info
, mid
, 0);
1908 if (IS_ERR(child
)) {
1909 ret
= PTR_ERR(child
);
1910 btrfs_handle_fs_error(fs_info
, ret
, NULL
);
1914 btrfs_tree_lock(child
);
1915 btrfs_set_lock_blocking(child
);
1916 ret
= btrfs_cow_block(trans
, root
, child
, mid
, 0, &child
);
1918 btrfs_tree_unlock(child
);
1919 free_extent_buffer(child
);
1923 tree_mod_log_set_root_pointer(root
, child
, 1);
1924 rcu_assign_pointer(root
->node
, child
);
1926 add_root_to_dirty_list(root
);
1927 btrfs_tree_unlock(child
);
1929 path
->locks
[level
] = 0;
1930 path
->nodes
[level
] = NULL
;
1931 clean_tree_block(fs_info
, mid
);
1932 btrfs_tree_unlock(mid
);
1933 /* once for the path */
1934 free_extent_buffer(mid
);
1936 root_sub_used(root
, mid
->len
);
1937 btrfs_free_tree_block(trans
, root
, mid
, 0, 1);
1938 /* once for the root ptr */
1939 free_extent_buffer_stale(mid
);
1942 if (btrfs_header_nritems(mid
) >
1943 BTRFS_NODEPTRS_PER_BLOCK(fs_info
) / 4)
1946 left
= read_node_slot(fs_info
, parent
, pslot
- 1);
1951 btrfs_tree_lock(left
);
1952 btrfs_set_lock_blocking(left
);
1953 wret
= btrfs_cow_block(trans
, root
, left
,
1954 parent
, pslot
- 1, &left
);
1961 right
= read_node_slot(fs_info
, parent
, pslot
+ 1);
1966 btrfs_tree_lock(right
);
1967 btrfs_set_lock_blocking(right
);
1968 wret
= btrfs_cow_block(trans
, root
, right
,
1969 parent
, pslot
+ 1, &right
);
1976 /* first, try to make some room in the middle buffer */
1978 orig_slot
+= btrfs_header_nritems(left
);
1979 wret
= push_node_left(trans
, fs_info
, left
, mid
, 1);
1985 * then try to empty the right most buffer into the middle
1988 wret
= push_node_left(trans
, fs_info
, mid
, right
, 1);
1989 if (wret
< 0 && wret
!= -ENOSPC
)
1991 if (btrfs_header_nritems(right
) == 0) {
1992 clean_tree_block(fs_info
, right
);
1993 btrfs_tree_unlock(right
);
1994 del_ptr(root
, path
, level
+ 1, pslot
+ 1);
1995 root_sub_used(root
, right
->len
);
1996 btrfs_free_tree_block(trans
, root
, right
, 0, 1);
1997 free_extent_buffer_stale(right
);
2000 struct btrfs_disk_key right_key
;
2001 btrfs_node_key(right
, &right_key
, 0);
2002 tree_mod_log_set_node_key(fs_info
, parent
,
2004 btrfs_set_node_key(parent
, &right_key
, pslot
+ 1);
2005 btrfs_mark_buffer_dirty(parent
);
2008 if (btrfs_header_nritems(mid
) == 1) {
2010 * we're not allowed to leave a node with one item in the
2011 * tree during a delete. A deletion from lower in the tree
2012 * could try to delete the only pointer in this node.
2013 * So, pull some keys from the left.
2014 * There has to be a left pointer at this point because
2015 * otherwise we would have pulled some pointers from the
2020 btrfs_handle_fs_error(fs_info
, ret
, NULL
);
2023 wret
= balance_node_right(trans
, fs_info
, mid
, left
);
2029 wret
= push_node_left(trans
, fs_info
, left
, mid
, 1);
2035 if (btrfs_header_nritems(mid
) == 0) {
2036 clean_tree_block(fs_info
, mid
);
2037 btrfs_tree_unlock(mid
);
2038 del_ptr(root
, path
, level
+ 1, pslot
);
2039 root_sub_used(root
, mid
->len
);
2040 btrfs_free_tree_block(trans
, root
, mid
, 0, 1);
2041 free_extent_buffer_stale(mid
);
2044 /* update the parent key to reflect our changes */
2045 struct btrfs_disk_key mid_key
;
2046 btrfs_node_key(mid
, &mid_key
, 0);
2047 tree_mod_log_set_node_key(fs_info
, parent
, pslot
, 0);
2048 btrfs_set_node_key(parent
, &mid_key
, pslot
);
2049 btrfs_mark_buffer_dirty(parent
);
2052 /* update the path */
2054 if (btrfs_header_nritems(left
) > orig_slot
) {
2055 extent_buffer_get(left
);
2056 /* left was locked after cow */
2057 path
->nodes
[level
] = left
;
2058 path
->slots
[level
+ 1] -= 1;
2059 path
->slots
[level
] = orig_slot
;
2061 btrfs_tree_unlock(mid
);
2062 free_extent_buffer(mid
);
2065 orig_slot
-= btrfs_header_nritems(left
);
2066 path
->slots
[level
] = orig_slot
;
2069 /* double check we haven't messed things up */
2071 btrfs_node_blockptr(path
->nodes
[level
], path
->slots
[level
]))
2075 btrfs_tree_unlock(right
);
2076 free_extent_buffer(right
);
2079 if (path
->nodes
[level
] != left
)
2080 btrfs_tree_unlock(left
);
2081 free_extent_buffer(left
);
2086 /* Node balancing for insertion. Here we only split or push nodes around
2087 * when they are completely full. This is also done top down, so we
2088 * have to be pessimistic.
2090 static noinline
int push_nodes_for_insert(struct btrfs_trans_handle
*trans
,
2091 struct btrfs_root
*root
,
2092 struct btrfs_path
*path
, int level
)
2094 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2095 struct extent_buffer
*right
= NULL
;
2096 struct extent_buffer
*mid
;
2097 struct extent_buffer
*left
= NULL
;
2098 struct extent_buffer
*parent
= NULL
;
2102 int orig_slot
= path
->slots
[level
];
2107 mid
= path
->nodes
[level
];
2108 WARN_ON(btrfs_header_generation(mid
) != trans
->transid
);
2110 if (level
< BTRFS_MAX_LEVEL
- 1) {
2111 parent
= path
->nodes
[level
+ 1];
2112 pslot
= path
->slots
[level
+ 1];
2118 left
= read_node_slot(fs_info
, parent
, pslot
- 1);
2122 /* first, try to make some room in the middle buffer */
2126 btrfs_tree_lock(left
);
2127 btrfs_set_lock_blocking(left
);
2129 left_nr
= btrfs_header_nritems(left
);
2130 if (left_nr
>= BTRFS_NODEPTRS_PER_BLOCK(fs_info
) - 1) {
2133 ret
= btrfs_cow_block(trans
, root
, left
, parent
,
2138 wret
= push_node_left(trans
, fs_info
,
2145 struct btrfs_disk_key disk_key
;
2146 orig_slot
+= left_nr
;
2147 btrfs_node_key(mid
, &disk_key
, 0);
2148 tree_mod_log_set_node_key(fs_info
, parent
, pslot
, 0);
2149 btrfs_set_node_key(parent
, &disk_key
, pslot
);
2150 btrfs_mark_buffer_dirty(parent
);
2151 if (btrfs_header_nritems(left
) > orig_slot
) {
2152 path
->nodes
[level
] = left
;
2153 path
->slots
[level
+ 1] -= 1;
2154 path
->slots
[level
] = orig_slot
;
2155 btrfs_tree_unlock(mid
);
2156 free_extent_buffer(mid
);
2159 btrfs_header_nritems(left
);
2160 path
->slots
[level
] = orig_slot
;
2161 btrfs_tree_unlock(left
);
2162 free_extent_buffer(left
);
2166 btrfs_tree_unlock(left
);
2167 free_extent_buffer(left
);
2169 right
= read_node_slot(fs_info
, parent
, pslot
+ 1);
2174 * then try to empty the right most buffer into the middle
2179 btrfs_tree_lock(right
);
2180 btrfs_set_lock_blocking(right
);
2182 right_nr
= btrfs_header_nritems(right
);
2183 if (right_nr
>= BTRFS_NODEPTRS_PER_BLOCK(fs_info
) - 1) {
2186 ret
= btrfs_cow_block(trans
, root
, right
,
2192 wret
= balance_node_right(trans
, fs_info
,
2199 struct btrfs_disk_key disk_key
;
2201 btrfs_node_key(right
, &disk_key
, 0);
2202 tree_mod_log_set_node_key(fs_info
, parent
,
2204 btrfs_set_node_key(parent
, &disk_key
, pslot
+ 1);
2205 btrfs_mark_buffer_dirty(parent
);
2207 if (btrfs_header_nritems(mid
) <= orig_slot
) {
2208 path
->nodes
[level
] = right
;
2209 path
->slots
[level
+ 1] += 1;
2210 path
->slots
[level
] = orig_slot
-
2211 btrfs_header_nritems(mid
);
2212 btrfs_tree_unlock(mid
);
2213 free_extent_buffer(mid
);
2215 btrfs_tree_unlock(right
);
2216 free_extent_buffer(right
);
2220 btrfs_tree_unlock(right
);
2221 free_extent_buffer(right
);
2227 * readahead one full node of leaves, finding things that are close
2228 * to the block in 'slot', and triggering ra on them.
2230 static void reada_for_search(struct btrfs_fs_info
*fs_info
,
2231 struct btrfs_path
*path
,
2232 int level
, int slot
, u64 objectid
)
2234 struct extent_buffer
*node
;
2235 struct btrfs_disk_key disk_key
;
2240 struct extent_buffer
*eb
;
2248 if (!path
->nodes
[level
])
2251 node
= path
->nodes
[level
];
2253 search
= btrfs_node_blockptr(node
, slot
);
2254 blocksize
= fs_info
->nodesize
;
2255 eb
= find_extent_buffer(fs_info
, search
);
2257 free_extent_buffer(eb
);
2263 nritems
= btrfs_header_nritems(node
);
2267 if (path
->reada
== READA_BACK
) {
2271 } else if (path
->reada
== READA_FORWARD
) {
2276 if (path
->reada
== READA_BACK
&& objectid
) {
2277 btrfs_node_key(node
, &disk_key
, nr
);
2278 if (btrfs_disk_key_objectid(&disk_key
) != objectid
)
2281 search
= btrfs_node_blockptr(node
, nr
);
2282 if ((search
<= target
&& target
- search
<= 65536) ||
2283 (search
> target
&& search
- target
<= 65536)) {
2284 readahead_tree_block(fs_info
, search
);
2288 if ((nread
> 65536 || nscan
> 32))
2293 static noinline
void reada_for_balance(struct btrfs_fs_info
*fs_info
,
2294 struct btrfs_path
*path
, int level
)
2298 struct extent_buffer
*parent
;
2299 struct extent_buffer
*eb
;
2304 parent
= path
->nodes
[level
+ 1];
2308 nritems
= btrfs_header_nritems(parent
);
2309 slot
= path
->slots
[level
+ 1];
2312 block1
= btrfs_node_blockptr(parent
, slot
- 1);
2313 gen
= btrfs_node_ptr_generation(parent
, slot
- 1);
2314 eb
= find_extent_buffer(fs_info
, block1
);
2316 * if we get -eagain from btrfs_buffer_uptodate, we
2317 * don't want to return eagain here. That will loop
2320 if (eb
&& btrfs_buffer_uptodate(eb
, gen
, 1) != 0)
2322 free_extent_buffer(eb
);
2324 if (slot
+ 1 < nritems
) {
2325 block2
= btrfs_node_blockptr(parent
, slot
+ 1);
2326 gen
= btrfs_node_ptr_generation(parent
, slot
+ 1);
2327 eb
= find_extent_buffer(fs_info
, block2
);
2328 if (eb
&& btrfs_buffer_uptodate(eb
, gen
, 1) != 0)
2330 free_extent_buffer(eb
);
2334 readahead_tree_block(fs_info
, block1
);
2336 readahead_tree_block(fs_info
, block2
);
2341 * when we walk down the tree, it is usually safe to unlock the higher layers
2342 * in the tree. The exceptions are when our path goes through slot 0, because
2343 * operations on the tree might require changing key pointers higher up in the
2346 * callers might also have set path->keep_locks, which tells this code to keep
2347 * the lock if the path points to the last slot in the block. This is part of
2348 * walking through the tree, and selecting the next slot in the higher block.
2350 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2351 * if lowest_unlock is 1, level 0 won't be unlocked
2353 static noinline
void unlock_up(struct btrfs_path
*path
, int level
,
2354 int lowest_unlock
, int min_write_lock_level
,
2355 int *write_lock_level
)
2358 int skip_level
= level
;
2360 struct extent_buffer
*t
;
2362 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
2363 if (!path
->nodes
[i
])
2365 if (!path
->locks
[i
])
2367 if (!no_skips
&& path
->slots
[i
] == 0) {
2371 if (!no_skips
&& path
->keep_locks
) {
2374 nritems
= btrfs_header_nritems(t
);
2375 if (nritems
< 1 || path
->slots
[i
] >= nritems
- 1) {
2380 if (skip_level
< i
&& i
>= lowest_unlock
)
2384 if (i
>= lowest_unlock
&& i
> skip_level
&& path
->locks
[i
]) {
2385 btrfs_tree_unlock_rw(t
, path
->locks
[i
]);
2387 if (write_lock_level
&&
2388 i
> min_write_lock_level
&&
2389 i
<= *write_lock_level
) {
2390 *write_lock_level
= i
- 1;
2397 * This releases any locks held in the path starting at level and
2398 * going all the way up to the root.
2400 * btrfs_search_slot will keep the lock held on higher nodes in a few
2401 * corner cases, such as COW of the block at slot zero in the node. This
2402 * ignores those rules, and it should only be called when there are no
2403 * more updates to be done higher up in the tree.
2405 noinline
void btrfs_unlock_up_safe(struct btrfs_path
*path
, int level
)
2409 if (path
->keep_locks
)
2412 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
2413 if (!path
->nodes
[i
])
2415 if (!path
->locks
[i
])
2417 btrfs_tree_unlock_rw(path
->nodes
[i
], path
->locks
[i
]);
2423 * helper function for btrfs_search_slot. The goal is to find a block
2424 * in cache without setting the path to blocking. If we find the block
2425 * we return zero and the path is unchanged.
2427 * If we can't find the block, we set the path blocking and do some
2428 * reada. -EAGAIN is returned and the search must be repeated.
2431 read_block_for_search(struct btrfs_root
*root
, struct btrfs_path
*p
,
2432 struct extent_buffer
**eb_ret
, int level
, int slot
,
2433 const struct btrfs_key
*key
)
2435 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2438 struct extent_buffer
*b
= *eb_ret
;
2439 struct extent_buffer
*tmp
;
2442 blocknr
= btrfs_node_blockptr(b
, slot
);
2443 gen
= btrfs_node_ptr_generation(b
, slot
);
2445 tmp
= find_extent_buffer(fs_info
, blocknr
);
2447 /* first we do an atomic uptodate check */
2448 if (btrfs_buffer_uptodate(tmp
, gen
, 1) > 0) {
2453 /* the pages were up to date, but we failed
2454 * the generation number check. Do a full
2455 * read for the generation number that is correct.
2456 * We must do this without dropping locks so
2457 * we can trust our generation number
2459 btrfs_set_path_blocking(p
);
2461 /* now we're allowed to do a blocking uptodate check */
2462 ret
= btrfs_read_buffer(tmp
, gen
);
2467 free_extent_buffer(tmp
);
2468 btrfs_release_path(p
);
2473 * reduce lock contention at high levels
2474 * of the btree by dropping locks before
2475 * we read. Don't release the lock on the current
2476 * level because we need to walk this node to figure
2477 * out which blocks to read.
2479 btrfs_unlock_up_safe(p
, level
+ 1);
2480 btrfs_set_path_blocking(p
);
2482 free_extent_buffer(tmp
);
2483 if (p
->reada
!= READA_NONE
)
2484 reada_for_search(fs_info
, p
, level
, slot
, key
->objectid
);
2487 tmp
= read_tree_block(fs_info
, blocknr
, gen
);
2490 * If the read above didn't mark this buffer up to date,
2491 * it will never end up being up to date. Set ret to EIO now
2492 * and give up so that our caller doesn't loop forever
2495 if (!btrfs_buffer_uptodate(tmp
, 0, 0))
2497 free_extent_buffer(tmp
);
2502 btrfs_release_path(p
);
2507 * helper function for btrfs_search_slot. This does all of the checks
2508 * for node-level blocks and does any balancing required based on
2511 * If no extra work was required, zero is returned. If we had to
2512 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2516 setup_nodes_for_search(struct btrfs_trans_handle
*trans
,
2517 struct btrfs_root
*root
, struct btrfs_path
*p
,
2518 struct extent_buffer
*b
, int level
, int ins_len
,
2519 int *write_lock_level
)
2521 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2524 if ((p
->search_for_split
|| ins_len
> 0) && btrfs_header_nritems(b
) >=
2525 BTRFS_NODEPTRS_PER_BLOCK(fs_info
) - 3) {
2528 if (*write_lock_level
< level
+ 1) {
2529 *write_lock_level
= level
+ 1;
2530 btrfs_release_path(p
);
2534 btrfs_set_path_blocking(p
);
2535 reada_for_balance(fs_info
, p
, level
);
2536 sret
= split_node(trans
, root
, p
, level
);
2537 btrfs_clear_path_blocking(p
, NULL
, 0);
2544 b
= p
->nodes
[level
];
2545 } else if (ins_len
< 0 && btrfs_header_nritems(b
) <
2546 BTRFS_NODEPTRS_PER_BLOCK(fs_info
) / 2) {
2549 if (*write_lock_level
< level
+ 1) {
2550 *write_lock_level
= level
+ 1;
2551 btrfs_release_path(p
);
2555 btrfs_set_path_blocking(p
);
2556 reada_for_balance(fs_info
, p
, level
);
2557 sret
= balance_level(trans
, root
, p
, level
);
2558 btrfs_clear_path_blocking(p
, NULL
, 0);
2564 b
= p
->nodes
[level
];
2566 btrfs_release_path(p
);
2569 BUG_ON(btrfs_header_nritems(b
) == 1);
2579 static void key_search_validate(struct extent_buffer
*b
,
2580 const struct btrfs_key
*key
,
2583 #ifdef CONFIG_BTRFS_ASSERT
2584 struct btrfs_disk_key disk_key
;
2586 btrfs_cpu_key_to_disk(&disk_key
, key
);
2589 ASSERT(!memcmp_extent_buffer(b
, &disk_key
,
2590 offsetof(struct btrfs_leaf
, items
[0].key
),
2593 ASSERT(!memcmp_extent_buffer(b
, &disk_key
,
2594 offsetof(struct btrfs_node
, ptrs
[0].key
),
2599 static int key_search(struct extent_buffer
*b
, const struct btrfs_key
*key
,
2600 int level
, int *prev_cmp
, int *slot
)
2602 if (*prev_cmp
!= 0) {
2603 *prev_cmp
= bin_search(b
, key
, level
, slot
);
2607 key_search_validate(b
, key
, level
);
2613 int btrfs_find_item(struct btrfs_root
*fs_root
, struct btrfs_path
*path
,
2614 u64 iobjectid
, u64 ioff
, u8 key_type
,
2615 struct btrfs_key
*found_key
)
2618 struct btrfs_key key
;
2619 struct extent_buffer
*eb
;
2624 key
.type
= key_type
;
2625 key
.objectid
= iobjectid
;
2628 ret
= btrfs_search_slot(NULL
, fs_root
, &key
, path
, 0, 0);
2632 eb
= path
->nodes
[0];
2633 if (ret
&& path
->slots
[0] >= btrfs_header_nritems(eb
)) {
2634 ret
= btrfs_next_leaf(fs_root
, path
);
2637 eb
= path
->nodes
[0];
2640 btrfs_item_key_to_cpu(eb
, found_key
, path
->slots
[0]);
2641 if (found_key
->type
!= key
.type
||
2642 found_key
->objectid
!= key
.objectid
)
2649 * look for key in the tree. path is filled in with nodes along the way
2650 * if key is found, we return zero and you can find the item in the leaf
2651 * level of the path (level 0)
2653 * If the key isn't found, the path points to the slot where it should
2654 * be inserted, and 1 is returned. If there are other errors during the
2655 * search a negative error number is returned.
2657 * if ins_len > 0, nodes and leaves will be split as we walk down the
2658 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2661 int btrfs_search_slot(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
2662 const struct btrfs_key
*key
, struct btrfs_path
*p
,
2663 int ins_len
, int cow
)
2665 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2666 struct extent_buffer
*b
;
2671 int lowest_unlock
= 1;
2673 /* everything at write_lock_level or lower must be write locked */
2674 int write_lock_level
= 0;
2675 u8 lowest_level
= 0;
2676 int min_write_lock_level
;
2679 lowest_level
= p
->lowest_level
;
2680 WARN_ON(lowest_level
&& ins_len
> 0);
2681 WARN_ON(p
->nodes
[0] != NULL
);
2682 BUG_ON(!cow
&& ins_len
);
2687 /* when we are removing items, we might have to go up to level
2688 * two as we update tree pointers Make sure we keep write
2689 * for those levels as well
2691 write_lock_level
= 2;
2692 } else if (ins_len
> 0) {
2694 * for inserting items, make sure we have a write lock on
2695 * level 1 so we can update keys
2697 write_lock_level
= 1;
2701 write_lock_level
= -1;
2703 if (cow
&& (p
->keep_locks
|| p
->lowest_level
))
2704 write_lock_level
= BTRFS_MAX_LEVEL
;
2706 min_write_lock_level
= write_lock_level
;
2711 * we try very hard to do read locks on the root
2713 root_lock
= BTRFS_READ_LOCK
;
2715 if (p
->search_commit_root
) {
2717 * the commit roots are read only
2718 * so we always do read locks
2720 if (p
->need_commit_sem
)
2721 down_read(&fs_info
->commit_root_sem
);
2722 b
= root
->commit_root
;
2723 extent_buffer_get(b
);
2724 level
= btrfs_header_level(b
);
2725 if (p
->need_commit_sem
)
2726 up_read(&fs_info
->commit_root_sem
);
2727 if (!p
->skip_locking
)
2728 btrfs_tree_read_lock(b
);
2730 if (p
->skip_locking
) {
2731 b
= btrfs_root_node(root
);
2732 level
= btrfs_header_level(b
);
2734 /* we don't know the level of the root node
2735 * until we actually have it read locked
2737 b
= btrfs_read_lock_root_node(root
);
2738 level
= btrfs_header_level(b
);
2739 if (level
<= write_lock_level
) {
2740 /* whoops, must trade for write lock */
2741 btrfs_tree_read_unlock(b
);
2742 free_extent_buffer(b
);
2743 b
= btrfs_lock_root_node(root
);
2744 root_lock
= BTRFS_WRITE_LOCK
;
2746 /* the level might have changed, check again */
2747 level
= btrfs_header_level(b
);
2751 p
->nodes
[level
] = b
;
2752 if (!p
->skip_locking
)
2753 p
->locks
[level
] = root_lock
;
2756 level
= btrfs_header_level(b
);
2759 * setup the path here so we can release it under lock
2760 * contention with the cow code
2763 bool last_level
= (level
== (BTRFS_MAX_LEVEL
- 1));
2766 * if we don't really need to cow this block
2767 * then we don't want to set the path blocking,
2768 * so we test it here
2770 if (!should_cow_block(trans
, root
, b
)) {
2771 trans
->dirty
= true;
2776 * must have write locks on this node and the
2779 if (level
> write_lock_level
||
2780 (level
+ 1 > write_lock_level
&&
2781 level
+ 1 < BTRFS_MAX_LEVEL
&&
2782 p
->nodes
[level
+ 1])) {
2783 write_lock_level
= level
+ 1;
2784 btrfs_release_path(p
);
2788 btrfs_set_path_blocking(p
);
2790 err
= btrfs_cow_block(trans
, root
, b
, NULL
, 0,
2793 err
= btrfs_cow_block(trans
, root
, b
,
2794 p
->nodes
[level
+ 1],
2795 p
->slots
[level
+ 1], &b
);
2802 p
->nodes
[level
] = b
;
2803 btrfs_clear_path_blocking(p
, NULL
, 0);
2806 * we have a lock on b and as long as we aren't changing
2807 * the tree, there is no way to for the items in b to change.
2808 * It is safe to drop the lock on our parent before we
2809 * go through the expensive btree search on b.
2811 * If we're inserting or deleting (ins_len != 0), then we might
2812 * be changing slot zero, which may require changing the parent.
2813 * So, we can't drop the lock until after we know which slot
2814 * we're operating on.
2816 if (!ins_len
&& !p
->keep_locks
) {
2819 if (u
< BTRFS_MAX_LEVEL
&& p
->locks
[u
]) {
2820 btrfs_tree_unlock_rw(p
->nodes
[u
], p
->locks
[u
]);
2825 ret
= key_search(b
, key
, level
, &prev_cmp
, &slot
);
2831 if (ret
&& slot
> 0) {
2835 p
->slots
[level
] = slot
;
2836 err
= setup_nodes_for_search(trans
, root
, p
, b
, level
,
2837 ins_len
, &write_lock_level
);
2844 b
= p
->nodes
[level
];
2845 slot
= p
->slots
[level
];
2848 * slot 0 is special, if we change the key
2849 * we have to update the parent pointer
2850 * which means we must have a write lock
2853 if (slot
== 0 && ins_len
&&
2854 write_lock_level
< level
+ 1) {
2855 write_lock_level
= level
+ 1;
2856 btrfs_release_path(p
);
2860 unlock_up(p
, level
, lowest_unlock
,
2861 min_write_lock_level
, &write_lock_level
);
2863 if (level
== lowest_level
) {
2869 err
= read_block_for_search(root
, p
, &b
, level
,
2878 if (!p
->skip_locking
) {
2879 level
= btrfs_header_level(b
);
2880 if (level
<= write_lock_level
) {
2881 err
= btrfs_try_tree_write_lock(b
);
2883 btrfs_set_path_blocking(p
);
2885 btrfs_clear_path_blocking(p
, b
,
2888 p
->locks
[level
] = BTRFS_WRITE_LOCK
;
2890 err
= btrfs_tree_read_lock_atomic(b
);
2892 btrfs_set_path_blocking(p
);
2893 btrfs_tree_read_lock(b
);
2894 btrfs_clear_path_blocking(p
, b
,
2897 p
->locks
[level
] = BTRFS_READ_LOCK
;
2899 p
->nodes
[level
] = b
;
2902 p
->slots
[level
] = slot
;
2904 btrfs_leaf_free_space(fs_info
, b
) < ins_len
) {
2905 if (write_lock_level
< 1) {
2906 write_lock_level
= 1;
2907 btrfs_release_path(p
);
2911 btrfs_set_path_blocking(p
);
2912 err
= split_leaf(trans
, root
, key
,
2913 p
, ins_len
, ret
== 0);
2914 btrfs_clear_path_blocking(p
, NULL
, 0);
2922 if (!p
->search_for_split
)
2923 unlock_up(p
, level
, lowest_unlock
,
2924 min_write_lock_level
, &write_lock_level
);
2931 * we don't really know what they plan on doing with the path
2932 * from here on, so for now just mark it as blocking
2934 if (!p
->leave_spinning
)
2935 btrfs_set_path_blocking(p
);
2936 if (ret
< 0 && !p
->skip_release_on_error
)
2937 btrfs_release_path(p
);
2942 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2943 * current state of the tree together with the operations recorded in the tree
2944 * modification log to search for the key in a previous version of this tree, as
2945 * denoted by the time_seq parameter.
2947 * Naturally, there is no support for insert, delete or cow operations.
2949 * The resulting path and return value will be set up as if we called
2950 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2952 int btrfs_search_old_slot(struct btrfs_root
*root
, const struct btrfs_key
*key
,
2953 struct btrfs_path
*p
, u64 time_seq
)
2955 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2956 struct extent_buffer
*b
;
2961 int lowest_unlock
= 1;
2962 u8 lowest_level
= 0;
2965 lowest_level
= p
->lowest_level
;
2966 WARN_ON(p
->nodes
[0] != NULL
);
2968 if (p
->search_commit_root
) {
2970 return btrfs_search_slot(NULL
, root
, key
, p
, 0, 0);
2974 b
= get_old_root(root
, time_seq
);
2979 level
= btrfs_header_level(b
);
2980 p
->locks
[level
] = BTRFS_READ_LOCK
;
2983 level
= btrfs_header_level(b
);
2984 p
->nodes
[level
] = b
;
2985 btrfs_clear_path_blocking(p
, NULL
, 0);
2988 * we have a lock on b and as long as we aren't changing
2989 * the tree, there is no way to for the items in b to change.
2990 * It is safe to drop the lock on our parent before we
2991 * go through the expensive btree search on b.
2993 btrfs_unlock_up_safe(p
, level
+ 1);
2996 * Since we can unwind ebs we want to do a real search every
3000 ret
= key_search(b
, key
, level
, &prev_cmp
, &slot
);
3004 if (ret
&& slot
> 0) {
3008 p
->slots
[level
] = slot
;
3009 unlock_up(p
, level
, lowest_unlock
, 0, NULL
);
3011 if (level
== lowest_level
) {
3017 err
= read_block_for_search(root
, p
, &b
, level
,
3026 level
= btrfs_header_level(b
);
3027 err
= btrfs_tree_read_lock_atomic(b
);
3029 btrfs_set_path_blocking(p
);
3030 btrfs_tree_read_lock(b
);
3031 btrfs_clear_path_blocking(p
, b
,
3034 b
= tree_mod_log_rewind(fs_info
, p
, b
, time_seq
);
3039 p
->locks
[level
] = BTRFS_READ_LOCK
;
3040 p
->nodes
[level
] = b
;
3042 p
->slots
[level
] = slot
;
3043 unlock_up(p
, level
, lowest_unlock
, 0, NULL
);
3049 if (!p
->leave_spinning
)
3050 btrfs_set_path_blocking(p
);
3052 btrfs_release_path(p
);
3058 * helper to use instead of search slot if no exact match is needed but
3059 * instead the next or previous item should be returned.
3060 * When find_higher is true, the next higher item is returned, the next lower
3062 * When return_any and find_higher are both true, and no higher item is found,
3063 * return the next lower instead.
3064 * When return_any is true and find_higher is false, and no lower item is found,
3065 * return the next higher instead.
3066 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3069 int btrfs_search_slot_for_read(struct btrfs_root
*root
,
3070 const struct btrfs_key
*key
,
3071 struct btrfs_path
*p
, int find_higher
,
3075 struct extent_buffer
*leaf
;
3078 ret
= btrfs_search_slot(NULL
, root
, key
, p
, 0, 0);
3082 * a return value of 1 means the path is at the position where the
3083 * item should be inserted. Normally this is the next bigger item,
3084 * but in case the previous item is the last in a leaf, path points
3085 * to the first free slot in the previous leaf, i.e. at an invalid
3091 if (p
->slots
[0] >= btrfs_header_nritems(leaf
)) {
3092 ret
= btrfs_next_leaf(root
, p
);
3098 * no higher item found, return the next
3103 btrfs_release_path(p
);
3107 if (p
->slots
[0] == 0) {
3108 ret
= btrfs_prev_leaf(root
, p
);
3113 if (p
->slots
[0] == btrfs_header_nritems(leaf
))
3120 * no lower item found, return the next
3125 btrfs_release_path(p
);
3135 * adjust the pointers going up the tree, starting at level
3136 * making sure the right key of each node is points to 'key'.
3137 * This is used after shifting pointers to the left, so it stops
3138 * fixing up pointers when a given leaf/node is not in slot 0 of the
3142 static void fixup_low_keys(struct btrfs_fs_info
*fs_info
,
3143 struct btrfs_path
*path
,
3144 struct btrfs_disk_key
*key
, int level
)
3147 struct extent_buffer
*t
;
3149 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
3150 int tslot
= path
->slots
[i
];
3151 if (!path
->nodes
[i
])
3154 tree_mod_log_set_node_key(fs_info
, t
, tslot
, 1);
3155 btrfs_set_node_key(t
, key
, tslot
);
3156 btrfs_mark_buffer_dirty(path
->nodes
[i
]);
3165 * This function isn't completely safe. It's the caller's responsibility
3166 * that the new key won't break the order
3168 void btrfs_set_item_key_safe(struct btrfs_fs_info
*fs_info
,
3169 struct btrfs_path
*path
,
3170 const struct btrfs_key
*new_key
)
3172 struct btrfs_disk_key disk_key
;
3173 struct extent_buffer
*eb
;
3176 eb
= path
->nodes
[0];
3177 slot
= path
->slots
[0];
3179 btrfs_item_key(eb
, &disk_key
, slot
- 1);
3180 BUG_ON(comp_keys(&disk_key
, new_key
) >= 0);
3182 if (slot
< btrfs_header_nritems(eb
) - 1) {
3183 btrfs_item_key(eb
, &disk_key
, slot
+ 1);
3184 BUG_ON(comp_keys(&disk_key
, new_key
) <= 0);
3187 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
3188 btrfs_set_item_key(eb
, &disk_key
, slot
);
3189 btrfs_mark_buffer_dirty(eb
);
3191 fixup_low_keys(fs_info
, path
, &disk_key
, 1);
3195 * try to push data from one node into the next node left in the
3198 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3199 * error, and > 0 if there was no room in the left hand block.
3201 static int push_node_left(struct btrfs_trans_handle
*trans
,
3202 struct btrfs_fs_info
*fs_info
,
3203 struct extent_buffer
*dst
,
3204 struct extent_buffer
*src
, int empty
)
3211 src_nritems
= btrfs_header_nritems(src
);
3212 dst_nritems
= btrfs_header_nritems(dst
);
3213 push_items
= BTRFS_NODEPTRS_PER_BLOCK(fs_info
) - dst_nritems
;
3214 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
3215 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
3217 if (!empty
&& src_nritems
<= 8)
3220 if (push_items
<= 0)
3224 push_items
= min(src_nritems
, push_items
);
3225 if (push_items
< src_nritems
) {
3226 /* leave at least 8 pointers in the node if
3227 * we aren't going to empty it
3229 if (src_nritems
- push_items
< 8) {
3230 if (push_items
<= 8)
3236 push_items
= min(src_nritems
- 8, push_items
);
3238 ret
= tree_mod_log_eb_copy(fs_info
, dst
, src
, dst_nritems
, 0,
3241 btrfs_abort_transaction(trans
, ret
);
3244 copy_extent_buffer(dst
, src
,
3245 btrfs_node_key_ptr_offset(dst_nritems
),
3246 btrfs_node_key_ptr_offset(0),
3247 push_items
* sizeof(struct btrfs_key_ptr
));
3249 if (push_items
< src_nritems
) {
3251 * don't call tree_mod_log_eb_move here, key removal was already
3252 * fully logged by tree_mod_log_eb_copy above.
3254 memmove_extent_buffer(src
, btrfs_node_key_ptr_offset(0),
3255 btrfs_node_key_ptr_offset(push_items
),
3256 (src_nritems
- push_items
) *
3257 sizeof(struct btrfs_key_ptr
));
3259 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
3260 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
3261 btrfs_mark_buffer_dirty(src
);
3262 btrfs_mark_buffer_dirty(dst
);
3268 * try to push data from one node into the next node right in the
3271 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3272 * error, and > 0 if there was no room in the right hand block.
3274 * this will only push up to 1/2 the contents of the left node over
3276 static int balance_node_right(struct btrfs_trans_handle
*trans
,
3277 struct btrfs_fs_info
*fs_info
,
3278 struct extent_buffer
*dst
,
3279 struct extent_buffer
*src
)
3287 WARN_ON(btrfs_header_generation(src
) != trans
->transid
);
3288 WARN_ON(btrfs_header_generation(dst
) != trans
->transid
);
3290 src_nritems
= btrfs_header_nritems(src
);
3291 dst_nritems
= btrfs_header_nritems(dst
);
3292 push_items
= BTRFS_NODEPTRS_PER_BLOCK(fs_info
) - dst_nritems
;
3293 if (push_items
<= 0)
3296 if (src_nritems
< 4)
3299 max_push
= src_nritems
/ 2 + 1;
3300 /* don't try to empty the node */
3301 if (max_push
>= src_nritems
)
3304 if (max_push
< push_items
)
3305 push_items
= max_push
;
3307 tree_mod_log_eb_move(fs_info
, dst
, push_items
, 0, dst_nritems
);
3308 memmove_extent_buffer(dst
, btrfs_node_key_ptr_offset(push_items
),
3309 btrfs_node_key_ptr_offset(0),
3311 sizeof(struct btrfs_key_ptr
));
3313 ret
= tree_mod_log_eb_copy(fs_info
, dst
, src
, 0,
3314 src_nritems
- push_items
, push_items
);
3316 btrfs_abort_transaction(trans
, ret
);
3319 copy_extent_buffer(dst
, src
,
3320 btrfs_node_key_ptr_offset(0),
3321 btrfs_node_key_ptr_offset(src_nritems
- push_items
),
3322 push_items
* sizeof(struct btrfs_key_ptr
));
3324 btrfs_set_header_nritems(src
, src_nritems
- push_items
);
3325 btrfs_set_header_nritems(dst
, dst_nritems
+ push_items
);
3327 btrfs_mark_buffer_dirty(src
);
3328 btrfs_mark_buffer_dirty(dst
);
3334 * helper function to insert a new root level in the tree.
3335 * A new node is allocated, and a single item is inserted to
3336 * point to the existing root
3338 * returns zero on success or < 0 on failure.
3340 static noinline
int insert_new_root(struct btrfs_trans_handle
*trans
,
3341 struct btrfs_root
*root
,
3342 struct btrfs_path
*path
, int level
)
3344 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3346 struct extent_buffer
*lower
;
3347 struct extent_buffer
*c
;
3348 struct extent_buffer
*old
;
3349 struct btrfs_disk_key lower_key
;
3351 BUG_ON(path
->nodes
[level
]);
3352 BUG_ON(path
->nodes
[level
-1] != root
->node
);
3354 lower
= path
->nodes
[level
-1];
3356 btrfs_item_key(lower
, &lower_key
, 0);
3358 btrfs_node_key(lower
, &lower_key
, 0);
3360 c
= btrfs_alloc_tree_block(trans
, root
, 0, root
->root_key
.objectid
,
3361 &lower_key
, level
, root
->node
->start
, 0);
3365 root_add_used(root
, fs_info
->nodesize
);
3367 memzero_extent_buffer(c
, 0, sizeof(struct btrfs_header
));
3368 btrfs_set_header_nritems(c
, 1);
3369 btrfs_set_header_level(c
, level
);
3370 btrfs_set_header_bytenr(c
, c
->start
);
3371 btrfs_set_header_generation(c
, trans
->transid
);
3372 btrfs_set_header_backref_rev(c
, BTRFS_MIXED_BACKREF_REV
);
3373 btrfs_set_header_owner(c
, root
->root_key
.objectid
);
3375 write_extent_buffer_fsid(c
, fs_info
->fsid
);
3376 write_extent_buffer_chunk_tree_uuid(c
, fs_info
->chunk_tree_uuid
);
3378 btrfs_set_node_key(c
, &lower_key
, 0);
3379 btrfs_set_node_blockptr(c
, 0, lower
->start
);
3380 lower_gen
= btrfs_header_generation(lower
);
3381 WARN_ON(lower_gen
!= trans
->transid
);
3383 btrfs_set_node_ptr_generation(c
, 0, lower_gen
);
3385 btrfs_mark_buffer_dirty(c
);
3388 tree_mod_log_set_root_pointer(root
, c
, 0);
3389 rcu_assign_pointer(root
->node
, c
);
3391 /* the super has an extra ref to root->node */
3392 free_extent_buffer(old
);
3394 add_root_to_dirty_list(root
);
3395 extent_buffer_get(c
);
3396 path
->nodes
[level
] = c
;
3397 path
->locks
[level
] = BTRFS_WRITE_LOCK_BLOCKING
;
3398 path
->slots
[level
] = 0;
3403 * worker function to insert a single pointer in a node.
3404 * the node should have enough room for the pointer already
3406 * slot and level indicate where you want the key to go, and
3407 * blocknr is the block the key points to.
3409 static void insert_ptr(struct btrfs_trans_handle
*trans
,
3410 struct btrfs_fs_info
*fs_info
, struct btrfs_path
*path
,
3411 struct btrfs_disk_key
*key
, u64 bytenr
,
3412 int slot
, int level
)
3414 struct extent_buffer
*lower
;
3418 BUG_ON(!path
->nodes
[level
]);
3419 btrfs_assert_tree_locked(path
->nodes
[level
]);
3420 lower
= path
->nodes
[level
];
3421 nritems
= btrfs_header_nritems(lower
);
3422 BUG_ON(slot
> nritems
);
3423 BUG_ON(nritems
== BTRFS_NODEPTRS_PER_BLOCK(fs_info
));
3424 if (slot
!= nritems
) {
3426 tree_mod_log_eb_move(fs_info
, lower
, slot
+ 1,
3427 slot
, nritems
- slot
);
3428 memmove_extent_buffer(lower
,
3429 btrfs_node_key_ptr_offset(slot
+ 1),
3430 btrfs_node_key_ptr_offset(slot
),
3431 (nritems
- slot
) * sizeof(struct btrfs_key_ptr
));
3434 ret
= tree_mod_log_insert_key(fs_info
, lower
, slot
,
3435 MOD_LOG_KEY_ADD
, GFP_NOFS
);
3438 btrfs_set_node_key(lower
, key
, slot
);
3439 btrfs_set_node_blockptr(lower
, slot
, bytenr
);
3440 WARN_ON(trans
->transid
== 0);
3441 btrfs_set_node_ptr_generation(lower
, slot
, trans
->transid
);
3442 btrfs_set_header_nritems(lower
, nritems
+ 1);
3443 btrfs_mark_buffer_dirty(lower
);
3447 * split the node at the specified level in path in two.
3448 * The path is corrected to point to the appropriate node after the split
3450 * Before splitting this tries to make some room in the node by pushing
3451 * left and right, if either one works, it returns right away.
3453 * returns 0 on success and < 0 on failure
3455 static noinline
int split_node(struct btrfs_trans_handle
*trans
,
3456 struct btrfs_root
*root
,
3457 struct btrfs_path
*path
, int level
)
3459 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3460 struct extent_buffer
*c
;
3461 struct extent_buffer
*split
;
3462 struct btrfs_disk_key disk_key
;
3467 c
= path
->nodes
[level
];
3468 WARN_ON(btrfs_header_generation(c
) != trans
->transid
);
3469 if (c
== root
->node
) {
3471 * trying to split the root, lets make a new one
3473 * tree mod log: We don't log_removal old root in
3474 * insert_new_root, because that root buffer will be kept as a
3475 * normal node. We are going to log removal of half of the
3476 * elements below with tree_mod_log_eb_copy. We're holding a
3477 * tree lock on the buffer, which is why we cannot race with
3478 * other tree_mod_log users.
3480 ret
= insert_new_root(trans
, root
, path
, level
+ 1);
3484 ret
= push_nodes_for_insert(trans
, root
, path
, level
);
3485 c
= path
->nodes
[level
];
3486 if (!ret
&& btrfs_header_nritems(c
) <
3487 BTRFS_NODEPTRS_PER_BLOCK(fs_info
) - 3)
3493 c_nritems
= btrfs_header_nritems(c
);
3494 mid
= (c_nritems
+ 1) / 2;
3495 btrfs_node_key(c
, &disk_key
, mid
);
3497 split
= btrfs_alloc_tree_block(trans
, root
, 0, root
->root_key
.objectid
,
3498 &disk_key
, level
, c
->start
, 0);
3500 return PTR_ERR(split
);
3502 root_add_used(root
, fs_info
->nodesize
);
3504 memzero_extent_buffer(split
, 0, sizeof(struct btrfs_header
));
3505 btrfs_set_header_level(split
, btrfs_header_level(c
));
3506 btrfs_set_header_bytenr(split
, split
->start
);
3507 btrfs_set_header_generation(split
, trans
->transid
);
3508 btrfs_set_header_backref_rev(split
, BTRFS_MIXED_BACKREF_REV
);
3509 btrfs_set_header_owner(split
, root
->root_key
.objectid
);
3510 write_extent_buffer_fsid(split
, fs_info
->fsid
);
3511 write_extent_buffer_chunk_tree_uuid(split
, fs_info
->chunk_tree_uuid
);
3513 ret
= tree_mod_log_eb_copy(fs_info
, split
, c
, 0, mid
, c_nritems
- mid
);
3515 btrfs_abort_transaction(trans
, ret
);
3518 copy_extent_buffer(split
, c
,
3519 btrfs_node_key_ptr_offset(0),
3520 btrfs_node_key_ptr_offset(mid
),
3521 (c_nritems
- mid
) * sizeof(struct btrfs_key_ptr
));
3522 btrfs_set_header_nritems(split
, c_nritems
- mid
);
3523 btrfs_set_header_nritems(c
, mid
);
3526 btrfs_mark_buffer_dirty(c
);
3527 btrfs_mark_buffer_dirty(split
);
3529 insert_ptr(trans
, fs_info
, path
, &disk_key
, split
->start
,
3530 path
->slots
[level
+ 1] + 1, level
+ 1);
3532 if (path
->slots
[level
] >= mid
) {
3533 path
->slots
[level
] -= mid
;
3534 btrfs_tree_unlock(c
);
3535 free_extent_buffer(c
);
3536 path
->nodes
[level
] = split
;
3537 path
->slots
[level
+ 1] += 1;
3539 btrfs_tree_unlock(split
);
3540 free_extent_buffer(split
);
3546 * how many bytes are required to store the items in a leaf. start
3547 * and nr indicate which items in the leaf to check. This totals up the
3548 * space used both by the item structs and the item data
3550 static int leaf_space_used(struct extent_buffer
*l
, int start
, int nr
)
3552 struct btrfs_item
*start_item
;
3553 struct btrfs_item
*end_item
;
3554 struct btrfs_map_token token
;
3556 int nritems
= btrfs_header_nritems(l
);
3557 int end
= min(nritems
, start
+ nr
) - 1;
3561 btrfs_init_map_token(&token
);
3562 start_item
= btrfs_item_nr(start
);
3563 end_item
= btrfs_item_nr(end
);
3564 data_len
= btrfs_token_item_offset(l
, start_item
, &token
) +
3565 btrfs_token_item_size(l
, start_item
, &token
);
3566 data_len
= data_len
- btrfs_token_item_offset(l
, end_item
, &token
);
3567 data_len
+= sizeof(struct btrfs_item
) * nr
;
3568 WARN_ON(data_len
< 0);
3573 * The space between the end of the leaf items and
3574 * the start of the leaf data. IOW, how much room
3575 * the leaf has left for both items and data
3577 noinline
int btrfs_leaf_free_space(struct btrfs_fs_info
*fs_info
,
3578 struct extent_buffer
*leaf
)
3580 int nritems
= btrfs_header_nritems(leaf
);
3583 ret
= BTRFS_LEAF_DATA_SIZE(fs_info
) - leaf_space_used(leaf
, 0, nritems
);
3586 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
3588 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info
),
3589 leaf_space_used(leaf
, 0, nritems
), nritems
);
3595 * min slot controls the lowest index we're willing to push to the
3596 * right. We'll push up to and including min_slot, but no lower
3598 static noinline
int __push_leaf_right(struct btrfs_fs_info
*fs_info
,
3599 struct btrfs_path
*path
,
3600 int data_size
, int empty
,
3601 struct extent_buffer
*right
,
3602 int free_space
, u32 left_nritems
,
3605 struct extent_buffer
*left
= path
->nodes
[0];
3606 struct extent_buffer
*upper
= path
->nodes
[1];
3607 struct btrfs_map_token token
;
3608 struct btrfs_disk_key disk_key
;
3613 struct btrfs_item
*item
;
3619 btrfs_init_map_token(&token
);
3624 nr
= max_t(u32
, 1, min_slot
);
3626 if (path
->slots
[0] >= left_nritems
)
3627 push_space
+= data_size
;
3629 slot
= path
->slots
[1];
3630 i
= left_nritems
- 1;
3632 item
= btrfs_item_nr(i
);
3634 if (!empty
&& push_items
> 0) {
3635 if (path
->slots
[0] > i
)
3637 if (path
->slots
[0] == i
) {
3638 int space
= btrfs_leaf_free_space(fs_info
, left
);
3639 if (space
+ push_space
* 2 > free_space
)
3644 if (path
->slots
[0] == i
)
3645 push_space
+= data_size
;
3647 this_item_size
= btrfs_item_size(left
, item
);
3648 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
3652 push_space
+= this_item_size
+ sizeof(*item
);
3658 if (push_items
== 0)
3661 WARN_ON(!empty
&& push_items
== left_nritems
);
3663 /* push left to right */
3664 right_nritems
= btrfs_header_nritems(right
);
3666 push_space
= btrfs_item_end_nr(left
, left_nritems
- push_items
);
3667 push_space
-= leaf_data_end(fs_info
, left
);
3669 /* make room in the right data area */
3670 data_end
= leaf_data_end(fs_info
, right
);
3671 memmove_extent_buffer(right
,
3672 BTRFS_LEAF_DATA_OFFSET
+ data_end
- push_space
,
3673 BTRFS_LEAF_DATA_OFFSET
+ data_end
,
3674 BTRFS_LEAF_DATA_SIZE(fs_info
) - data_end
);
3676 /* copy from the left data area */
3677 copy_extent_buffer(right
, left
, BTRFS_LEAF_DATA_OFFSET
+
3678 BTRFS_LEAF_DATA_SIZE(fs_info
) - push_space
,
3679 BTRFS_LEAF_DATA_OFFSET
+ leaf_data_end(fs_info
, left
),
3682 memmove_extent_buffer(right
, btrfs_item_nr_offset(push_items
),
3683 btrfs_item_nr_offset(0),
3684 right_nritems
* sizeof(struct btrfs_item
));
3686 /* copy the items from left to right */
3687 copy_extent_buffer(right
, left
, btrfs_item_nr_offset(0),
3688 btrfs_item_nr_offset(left_nritems
- push_items
),
3689 push_items
* sizeof(struct btrfs_item
));
3691 /* update the item pointers */
3692 right_nritems
+= push_items
;
3693 btrfs_set_header_nritems(right
, right_nritems
);
3694 push_space
= BTRFS_LEAF_DATA_SIZE(fs_info
);
3695 for (i
= 0; i
< right_nritems
; i
++) {
3696 item
= btrfs_item_nr(i
);
3697 push_space
-= btrfs_token_item_size(right
, item
, &token
);
3698 btrfs_set_token_item_offset(right
, item
, push_space
, &token
);
3701 left_nritems
-= push_items
;
3702 btrfs_set_header_nritems(left
, left_nritems
);
3705 btrfs_mark_buffer_dirty(left
);
3707 clean_tree_block(fs_info
, left
);
3709 btrfs_mark_buffer_dirty(right
);
3711 btrfs_item_key(right
, &disk_key
, 0);
3712 btrfs_set_node_key(upper
, &disk_key
, slot
+ 1);
3713 btrfs_mark_buffer_dirty(upper
);
3715 /* then fixup the leaf pointer in the path */
3716 if (path
->slots
[0] >= left_nritems
) {
3717 path
->slots
[0] -= left_nritems
;
3718 if (btrfs_header_nritems(path
->nodes
[0]) == 0)
3719 clean_tree_block(fs_info
, path
->nodes
[0]);
3720 btrfs_tree_unlock(path
->nodes
[0]);
3721 free_extent_buffer(path
->nodes
[0]);
3722 path
->nodes
[0] = right
;
3723 path
->slots
[1] += 1;
3725 btrfs_tree_unlock(right
);
3726 free_extent_buffer(right
);
3731 btrfs_tree_unlock(right
);
3732 free_extent_buffer(right
);
3737 * push some data in the path leaf to the right, trying to free up at
3738 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3740 * returns 1 if the push failed because the other node didn't have enough
3741 * room, 0 if everything worked out and < 0 if there were major errors.
3743 * this will push starting from min_slot to the end of the leaf. It won't
3744 * push any slot lower than min_slot
3746 static int push_leaf_right(struct btrfs_trans_handle
*trans
, struct btrfs_root
3747 *root
, struct btrfs_path
*path
,
3748 int min_data_size
, int data_size
,
3749 int empty
, u32 min_slot
)
3751 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3752 struct extent_buffer
*left
= path
->nodes
[0];
3753 struct extent_buffer
*right
;
3754 struct extent_buffer
*upper
;
3760 if (!path
->nodes
[1])
3763 slot
= path
->slots
[1];
3764 upper
= path
->nodes
[1];
3765 if (slot
>= btrfs_header_nritems(upper
) - 1)
3768 btrfs_assert_tree_locked(path
->nodes
[1]);
3770 right
= read_node_slot(fs_info
, upper
, slot
+ 1);
3772 * slot + 1 is not valid or we fail to read the right node,
3773 * no big deal, just return.
3778 btrfs_tree_lock(right
);
3779 btrfs_set_lock_blocking(right
);
3781 free_space
= btrfs_leaf_free_space(fs_info
, right
);
3782 if (free_space
< data_size
)
3785 /* cow and double check */
3786 ret
= btrfs_cow_block(trans
, root
, right
, upper
,
3791 free_space
= btrfs_leaf_free_space(fs_info
, right
);
3792 if (free_space
< data_size
)
3795 left_nritems
= btrfs_header_nritems(left
);
3796 if (left_nritems
== 0)
3799 if (path
->slots
[0] == left_nritems
&& !empty
) {
3800 /* Key greater than all keys in the leaf, right neighbor has
3801 * enough room for it and we're not emptying our leaf to delete
3802 * it, therefore use right neighbor to insert the new item and
3803 * no need to touch/dirty our left leaft. */
3804 btrfs_tree_unlock(left
);
3805 free_extent_buffer(left
);
3806 path
->nodes
[0] = right
;
3812 return __push_leaf_right(fs_info
, path
, min_data_size
, empty
,
3813 right
, free_space
, left_nritems
, min_slot
);
3815 btrfs_tree_unlock(right
);
3816 free_extent_buffer(right
);
3821 * push some data in the path leaf to the left, trying to free up at
3822 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3824 * max_slot can put a limit on how far into the leaf we'll push items. The
3825 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3828 static noinline
int __push_leaf_left(struct btrfs_fs_info
*fs_info
,
3829 struct btrfs_path
*path
, int data_size
,
3830 int empty
, struct extent_buffer
*left
,
3831 int free_space
, u32 right_nritems
,
3834 struct btrfs_disk_key disk_key
;
3835 struct extent_buffer
*right
= path
->nodes
[0];
3839 struct btrfs_item
*item
;
3840 u32 old_left_nritems
;
3844 u32 old_left_item_size
;
3845 struct btrfs_map_token token
;
3847 btrfs_init_map_token(&token
);
3850 nr
= min(right_nritems
, max_slot
);
3852 nr
= min(right_nritems
- 1, max_slot
);
3854 for (i
= 0; i
< nr
; i
++) {
3855 item
= btrfs_item_nr(i
);
3857 if (!empty
&& push_items
> 0) {
3858 if (path
->slots
[0] < i
)
3860 if (path
->slots
[0] == i
) {
3861 int space
= btrfs_leaf_free_space(fs_info
, right
);
3862 if (space
+ push_space
* 2 > free_space
)
3867 if (path
->slots
[0] == i
)
3868 push_space
+= data_size
;
3870 this_item_size
= btrfs_item_size(right
, item
);
3871 if (this_item_size
+ sizeof(*item
) + push_space
> free_space
)
3875 push_space
+= this_item_size
+ sizeof(*item
);
3878 if (push_items
== 0) {
3882 WARN_ON(!empty
&& push_items
== btrfs_header_nritems(right
));
3884 /* push data from right to left */
3885 copy_extent_buffer(left
, right
,
3886 btrfs_item_nr_offset(btrfs_header_nritems(left
)),
3887 btrfs_item_nr_offset(0),
3888 push_items
* sizeof(struct btrfs_item
));
3890 push_space
= BTRFS_LEAF_DATA_SIZE(fs_info
) -
3891 btrfs_item_offset_nr(right
, push_items
- 1);
3893 copy_extent_buffer(left
, right
, BTRFS_LEAF_DATA_OFFSET
+
3894 leaf_data_end(fs_info
, left
) - push_space
,
3895 BTRFS_LEAF_DATA_OFFSET
+
3896 btrfs_item_offset_nr(right
, push_items
- 1),
3898 old_left_nritems
= btrfs_header_nritems(left
);
3899 BUG_ON(old_left_nritems
<= 0);
3901 old_left_item_size
= btrfs_item_offset_nr(left
, old_left_nritems
- 1);
3902 for (i
= old_left_nritems
; i
< old_left_nritems
+ push_items
; i
++) {
3905 item
= btrfs_item_nr(i
);
3907 ioff
= btrfs_token_item_offset(left
, item
, &token
);
3908 btrfs_set_token_item_offset(left
, item
,
3909 ioff
- (BTRFS_LEAF_DATA_SIZE(fs_info
) - old_left_item_size
),
3912 btrfs_set_header_nritems(left
, old_left_nritems
+ push_items
);
3914 /* fixup right node */
3915 if (push_items
> right_nritems
)
3916 WARN(1, KERN_CRIT
"push items %d nr %u\n", push_items
,
3919 if (push_items
< right_nritems
) {
3920 push_space
= btrfs_item_offset_nr(right
, push_items
- 1) -
3921 leaf_data_end(fs_info
, right
);
3922 memmove_extent_buffer(right
, BTRFS_LEAF_DATA_OFFSET
+
3923 BTRFS_LEAF_DATA_SIZE(fs_info
) - push_space
,
3924 BTRFS_LEAF_DATA_OFFSET
+
3925 leaf_data_end(fs_info
, right
), push_space
);
3927 memmove_extent_buffer(right
, btrfs_item_nr_offset(0),
3928 btrfs_item_nr_offset(push_items
),
3929 (btrfs_header_nritems(right
) - push_items
) *
3930 sizeof(struct btrfs_item
));
3932 right_nritems
-= push_items
;
3933 btrfs_set_header_nritems(right
, right_nritems
);
3934 push_space
= BTRFS_LEAF_DATA_SIZE(fs_info
);
3935 for (i
= 0; i
< right_nritems
; i
++) {
3936 item
= btrfs_item_nr(i
);
3938 push_space
= push_space
- btrfs_token_item_size(right
,
3940 btrfs_set_token_item_offset(right
, item
, push_space
, &token
);
3943 btrfs_mark_buffer_dirty(left
);
3945 btrfs_mark_buffer_dirty(right
);
3947 clean_tree_block(fs_info
, right
);
3949 btrfs_item_key(right
, &disk_key
, 0);
3950 fixup_low_keys(fs_info
, path
, &disk_key
, 1);
3952 /* then fixup the leaf pointer in the path */
3953 if (path
->slots
[0] < push_items
) {
3954 path
->slots
[0] += old_left_nritems
;
3955 btrfs_tree_unlock(path
->nodes
[0]);
3956 free_extent_buffer(path
->nodes
[0]);
3957 path
->nodes
[0] = left
;
3958 path
->slots
[1] -= 1;
3960 btrfs_tree_unlock(left
);
3961 free_extent_buffer(left
);
3962 path
->slots
[0] -= push_items
;
3964 BUG_ON(path
->slots
[0] < 0);
3967 btrfs_tree_unlock(left
);
3968 free_extent_buffer(left
);
3973 * push some data in the path leaf to the left, trying to free up at
3974 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3976 * max_slot can put a limit on how far into the leaf we'll push items. The
3977 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3980 static int push_leaf_left(struct btrfs_trans_handle
*trans
, struct btrfs_root
3981 *root
, struct btrfs_path
*path
, int min_data_size
,
3982 int data_size
, int empty
, u32 max_slot
)
3984 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3985 struct extent_buffer
*right
= path
->nodes
[0];
3986 struct extent_buffer
*left
;
3992 slot
= path
->slots
[1];
3995 if (!path
->nodes
[1])
3998 right_nritems
= btrfs_header_nritems(right
);
3999 if (right_nritems
== 0)
4002 btrfs_assert_tree_locked(path
->nodes
[1]);
4004 left
= read_node_slot(fs_info
, path
->nodes
[1], slot
- 1);
4006 * slot - 1 is not valid or we fail to read the left node,
4007 * no big deal, just return.
4012 btrfs_tree_lock(left
);
4013 btrfs_set_lock_blocking(left
);
4015 free_space
= btrfs_leaf_free_space(fs_info
, left
);
4016 if (free_space
< data_size
) {
4021 /* cow and double check */
4022 ret
= btrfs_cow_block(trans
, root
, left
,
4023 path
->nodes
[1], slot
- 1, &left
);
4025 /* we hit -ENOSPC, but it isn't fatal here */
4031 free_space
= btrfs_leaf_free_space(fs_info
, left
);
4032 if (free_space
< data_size
) {
4037 return __push_leaf_left(fs_info
, path
, min_data_size
,
4038 empty
, left
, free_space
, right_nritems
,
4041 btrfs_tree_unlock(left
);
4042 free_extent_buffer(left
);
4047 * split the path's leaf in two, making sure there is at least data_size
4048 * available for the resulting leaf level of the path.
4050 static noinline
void copy_for_split(struct btrfs_trans_handle
*trans
,
4051 struct btrfs_fs_info
*fs_info
,
4052 struct btrfs_path
*path
,
4053 struct extent_buffer
*l
,
4054 struct extent_buffer
*right
,
4055 int slot
, int mid
, int nritems
)
4060 struct btrfs_disk_key disk_key
;
4061 struct btrfs_map_token token
;
4063 btrfs_init_map_token(&token
);
4065 nritems
= nritems
- mid
;
4066 btrfs_set_header_nritems(right
, nritems
);
4067 data_copy_size
= btrfs_item_end_nr(l
, mid
) - leaf_data_end(fs_info
, l
);
4069 copy_extent_buffer(right
, l
, btrfs_item_nr_offset(0),
4070 btrfs_item_nr_offset(mid
),
4071 nritems
* sizeof(struct btrfs_item
));
4073 copy_extent_buffer(right
, l
,
4074 BTRFS_LEAF_DATA_OFFSET
+ BTRFS_LEAF_DATA_SIZE(fs_info
) -
4075 data_copy_size
, BTRFS_LEAF_DATA_OFFSET
+
4076 leaf_data_end(fs_info
, l
), data_copy_size
);
4078 rt_data_off
= BTRFS_LEAF_DATA_SIZE(fs_info
) - btrfs_item_end_nr(l
, mid
);
4080 for (i
= 0; i
< nritems
; i
++) {
4081 struct btrfs_item
*item
= btrfs_item_nr(i
);
4084 ioff
= btrfs_token_item_offset(right
, item
, &token
);
4085 btrfs_set_token_item_offset(right
, item
,
4086 ioff
+ rt_data_off
, &token
);
4089 btrfs_set_header_nritems(l
, mid
);
4090 btrfs_item_key(right
, &disk_key
, 0);
4091 insert_ptr(trans
, fs_info
, path
, &disk_key
, right
->start
,
4092 path
->slots
[1] + 1, 1);
4094 btrfs_mark_buffer_dirty(right
);
4095 btrfs_mark_buffer_dirty(l
);
4096 BUG_ON(path
->slots
[0] != slot
);
4099 btrfs_tree_unlock(path
->nodes
[0]);
4100 free_extent_buffer(path
->nodes
[0]);
4101 path
->nodes
[0] = right
;
4102 path
->slots
[0] -= mid
;
4103 path
->slots
[1] += 1;
4105 btrfs_tree_unlock(right
);
4106 free_extent_buffer(right
);
4109 BUG_ON(path
->slots
[0] < 0);
4113 * double splits happen when we need to insert a big item in the middle
4114 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4115 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4118 * We avoid this by trying to push the items on either side of our target
4119 * into the adjacent leaves. If all goes well we can avoid the double split
4122 static noinline
int push_for_double_split(struct btrfs_trans_handle
*trans
,
4123 struct btrfs_root
*root
,
4124 struct btrfs_path
*path
,
4127 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4132 int space_needed
= data_size
;
4134 slot
= path
->slots
[0];
4135 if (slot
< btrfs_header_nritems(path
->nodes
[0]))
4136 space_needed
-= btrfs_leaf_free_space(fs_info
, path
->nodes
[0]);
4139 * try to push all the items after our slot into the
4142 ret
= push_leaf_right(trans
, root
, path
, 1, space_needed
, 0, slot
);
4149 nritems
= btrfs_header_nritems(path
->nodes
[0]);
4151 * our goal is to get our slot at the start or end of a leaf. If
4152 * we've done so we're done
4154 if (path
->slots
[0] == 0 || path
->slots
[0] == nritems
)
4157 if (btrfs_leaf_free_space(fs_info
, path
->nodes
[0]) >= data_size
)
4160 /* try to push all the items before our slot into the next leaf */
4161 slot
= path
->slots
[0];
4162 space_needed
= data_size
;
4164 space_needed
-= btrfs_leaf_free_space(fs_info
, path
->nodes
[0]);
4165 ret
= push_leaf_left(trans
, root
, path
, 1, space_needed
, 0, slot
);
4178 * split the path's leaf in two, making sure there is at least data_size
4179 * available for the resulting leaf level of the path.
4181 * returns 0 if all went well and < 0 on failure.
4183 static noinline
int split_leaf(struct btrfs_trans_handle
*trans
,
4184 struct btrfs_root
*root
,
4185 const struct btrfs_key
*ins_key
,
4186 struct btrfs_path
*path
, int data_size
,
4189 struct btrfs_disk_key disk_key
;
4190 struct extent_buffer
*l
;
4194 struct extent_buffer
*right
;
4195 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4199 int num_doubles
= 0;
4200 int tried_avoid_double
= 0;
4203 slot
= path
->slots
[0];
4204 if (extend
&& data_size
+ btrfs_item_size_nr(l
, slot
) +
4205 sizeof(struct btrfs_item
) > BTRFS_LEAF_DATA_SIZE(fs_info
))
4208 /* first try to make some room by pushing left and right */
4209 if (data_size
&& path
->nodes
[1]) {
4210 int space_needed
= data_size
;
4212 if (slot
< btrfs_header_nritems(l
))
4213 space_needed
-= btrfs_leaf_free_space(fs_info
, l
);
4215 wret
= push_leaf_right(trans
, root
, path
, space_needed
,
4216 space_needed
, 0, 0);
4220 space_needed
= data_size
;
4222 space_needed
-= btrfs_leaf_free_space(fs_info
,
4224 wret
= push_leaf_left(trans
, root
, path
, space_needed
,
4225 space_needed
, 0, (u32
)-1);
4231 /* did the pushes work? */
4232 if (btrfs_leaf_free_space(fs_info
, l
) >= data_size
)
4236 if (!path
->nodes
[1]) {
4237 ret
= insert_new_root(trans
, root
, path
, 1);
4244 slot
= path
->slots
[0];
4245 nritems
= btrfs_header_nritems(l
);
4246 mid
= (nritems
+ 1) / 2;
4250 leaf_space_used(l
, mid
, nritems
- mid
) + data_size
>
4251 BTRFS_LEAF_DATA_SIZE(fs_info
)) {
4252 if (slot
>= nritems
) {
4256 if (mid
!= nritems
&&
4257 leaf_space_used(l
, mid
, nritems
- mid
) +
4258 data_size
> BTRFS_LEAF_DATA_SIZE(fs_info
)) {
4259 if (data_size
&& !tried_avoid_double
)
4260 goto push_for_double
;
4266 if (leaf_space_used(l
, 0, mid
) + data_size
>
4267 BTRFS_LEAF_DATA_SIZE(fs_info
)) {
4268 if (!extend
&& data_size
&& slot
== 0) {
4270 } else if ((extend
|| !data_size
) && slot
== 0) {
4274 if (mid
!= nritems
&&
4275 leaf_space_used(l
, mid
, nritems
- mid
) +
4276 data_size
> BTRFS_LEAF_DATA_SIZE(fs_info
)) {
4277 if (data_size
&& !tried_avoid_double
)
4278 goto push_for_double
;
4286 btrfs_cpu_key_to_disk(&disk_key
, ins_key
);
4288 btrfs_item_key(l
, &disk_key
, mid
);
4290 right
= btrfs_alloc_tree_block(trans
, root
, 0, root
->root_key
.objectid
,
4291 &disk_key
, 0, l
->start
, 0);
4293 return PTR_ERR(right
);
4295 root_add_used(root
, fs_info
->nodesize
);
4297 memzero_extent_buffer(right
, 0, sizeof(struct btrfs_header
));
4298 btrfs_set_header_bytenr(right
, right
->start
);
4299 btrfs_set_header_generation(right
, trans
->transid
);
4300 btrfs_set_header_backref_rev(right
, BTRFS_MIXED_BACKREF_REV
);
4301 btrfs_set_header_owner(right
, root
->root_key
.objectid
);
4302 btrfs_set_header_level(right
, 0);
4303 write_extent_buffer_fsid(right
, fs_info
->fsid
);
4304 write_extent_buffer_chunk_tree_uuid(right
, fs_info
->chunk_tree_uuid
);
4308 btrfs_set_header_nritems(right
, 0);
4309 insert_ptr(trans
, fs_info
, path
, &disk_key
,
4310 right
->start
, path
->slots
[1] + 1, 1);
4311 btrfs_tree_unlock(path
->nodes
[0]);
4312 free_extent_buffer(path
->nodes
[0]);
4313 path
->nodes
[0] = right
;
4315 path
->slots
[1] += 1;
4317 btrfs_set_header_nritems(right
, 0);
4318 insert_ptr(trans
, fs_info
, path
, &disk_key
,
4319 right
->start
, path
->slots
[1], 1);
4320 btrfs_tree_unlock(path
->nodes
[0]);
4321 free_extent_buffer(path
->nodes
[0]);
4322 path
->nodes
[0] = right
;
4324 if (path
->slots
[1] == 0)
4325 fixup_low_keys(fs_info
, path
, &disk_key
, 1);
4328 * We create a new leaf 'right' for the required ins_len and
4329 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
4330 * the content of ins_len to 'right'.
4335 copy_for_split(trans
, fs_info
, path
, l
, right
, slot
, mid
, nritems
);
4338 BUG_ON(num_doubles
!= 0);
4346 push_for_double_split(trans
, root
, path
, data_size
);
4347 tried_avoid_double
= 1;
4348 if (btrfs_leaf_free_space(fs_info
, path
->nodes
[0]) >= data_size
)
4353 static noinline
int setup_leaf_for_split(struct btrfs_trans_handle
*trans
,
4354 struct btrfs_root
*root
,
4355 struct btrfs_path
*path
, int ins_len
)
4357 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4358 struct btrfs_key key
;
4359 struct extent_buffer
*leaf
;
4360 struct btrfs_file_extent_item
*fi
;
4365 leaf
= path
->nodes
[0];
4366 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
4368 BUG_ON(key
.type
!= BTRFS_EXTENT_DATA_KEY
&&
4369 key
.type
!= BTRFS_EXTENT_CSUM_KEY
);
4371 if (btrfs_leaf_free_space(fs_info
, leaf
) >= ins_len
)
4374 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
4375 if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
4376 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4377 struct btrfs_file_extent_item
);
4378 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
4380 btrfs_release_path(path
);
4382 path
->keep_locks
= 1;
4383 path
->search_for_split
= 1;
4384 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
4385 path
->search_for_split
= 0;
4392 leaf
= path
->nodes
[0];
4393 /* if our item isn't there, return now */
4394 if (item_size
!= btrfs_item_size_nr(leaf
, path
->slots
[0]))
4397 /* the leaf has changed, it now has room. return now */
4398 if (btrfs_leaf_free_space(fs_info
, path
->nodes
[0]) >= ins_len
)
4401 if (key
.type
== BTRFS_EXTENT_DATA_KEY
) {
4402 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4403 struct btrfs_file_extent_item
);
4404 if (extent_len
!= btrfs_file_extent_num_bytes(leaf
, fi
))
4408 btrfs_set_path_blocking(path
);
4409 ret
= split_leaf(trans
, root
, &key
, path
, ins_len
, 1);
4413 path
->keep_locks
= 0;
4414 btrfs_unlock_up_safe(path
, 1);
4417 path
->keep_locks
= 0;
4421 static noinline
int split_item(struct btrfs_fs_info
*fs_info
,
4422 struct btrfs_path
*path
,
4423 const struct btrfs_key
*new_key
,
4424 unsigned long split_offset
)
4426 struct extent_buffer
*leaf
;
4427 struct btrfs_item
*item
;
4428 struct btrfs_item
*new_item
;
4434 struct btrfs_disk_key disk_key
;
4436 leaf
= path
->nodes
[0];
4437 BUG_ON(btrfs_leaf_free_space(fs_info
, leaf
) < sizeof(struct btrfs_item
));
4439 btrfs_set_path_blocking(path
);
4441 item
= btrfs_item_nr(path
->slots
[0]);
4442 orig_offset
= btrfs_item_offset(leaf
, item
);
4443 item_size
= btrfs_item_size(leaf
, item
);
4445 buf
= kmalloc(item_size
, GFP_NOFS
);
4449 read_extent_buffer(leaf
, buf
, btrfs_item_ptr_offset(leaf
,
4450 path
->slots
[0]), item_size
);
4452 slot
= path
->slots
[0] + 1;
4453 nritems
= btrfs_header_nritems(leaf
);
4454 if (slot
!= nritems
) {
4455 /* shift the items */
4456 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ 1),
4457 btrfs_item_nr_offset(slot
),
4458 (nritems
- slot
) * sizeof(struct btrfs_item
));
4461 btrfs_cpu_key_to_disk(&disk_key
, new_key
);
4462 btrfs_set_item_key(leaf
, &disk_key
, slot
);
4464 new_item
= btrfs_item_nr(slot
);
4466 btrfs_set_item_offset(leaf
, new_item
, orig_offset
);
4467 btrfs_set_item_size(leaf
, new_item
, item_size
- split_offset
);
4469 btrfs_set_item_offset(leaf
, item
,
4470 orig_offset
+ item_size
- split_offset
);
4471 btrfs_set_item_size(leaf
, item
, split_offset
);
4473 btrfs_set_header_nritems(leaf
, nritems
+ 1);
4475 /* write the data for the start of the original item */
4476 write_extent_buffer(leaf
, buf
,
4477 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
4480 /* write the data for the new item */
4481 write_extent_buffer(leaf
, buf
+ split_offset
,
4482 btrfs_item_ptr_offset(leaf
, slot
),
4483 item_size
- split_offset
);
4484 btrfs_mark_buffer_dirty(leaf
);
4486 BUG_ON(btrfs_leaf_free_space(fs_info
, leaf
) < 0);
4492 * This function splits a single item into two items,
4493 * giving 'new_key' to the new item and splitting the
4494 * old one at split_offset (from the start of the item).
4496 * The path may be released by this operation. After
4497 * the split, the path is pointing to the old item. The
4498 * new item is going to be in the same node as the old one.
4500 * Note, the item being split must be smaller enough to live alone on
4501 * a tree block with room for one extra struct btrfs_item
4503 * This allows us to split the item in place, keeping a lock on the
4504 * leaf the entire time.
4506 int btrfs_split_item(struct btrfs_trans_handle
*trans
,
4507 struct btrfs_root
*root
,
4508 struct btrfs_path
*path
,
4509 const struct btrfs_key
*new_key
,
4510 unsigned long split_offset
)
4513 ret
= setup_leaf_for_split(trans
, root
, path
,
4514 sizeof(struct btrfs_item
));
4518 ret
= split_item(root
->fs_info
, path
, new_key
, split_offset
);
4523 * This function duplicate a item, giving 'new_key' to the new item.
4524 * It guarantees both items live in the same tree leaf and the new item
4525 * is contiguous with the original item.
4527 * This allows us to split file extent in place, keeping a lock on the
4528 * leaf the entire time.
4530 int btrfs_duplicate_item(struct btrfs_trans_handle
*trans
,
4531 struct btrfs_root
*root
,
4532 struct btrfs_path
*path
,
4533 const struct btrfs_key
*new_key
)
4535 struct extent_buffer
*leaf
;
4539 leaf
= path
->nodes
[0];
4540 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
4541 ret
= setup_leaf_for_split(trans
, root
, path
,
4542 item_size
+ sizeof(struct btrfs_item
));
4547 setup_items_for_insert(root
, path
, new_key
, &item_size
,
4548 item_size
, item_size
+
4549 sizeof(struct btrfs_item
), 1);
4550 leaf
= path
->nodes
[0];
4551 memcpy_extent_buffer(leaf
,
4552 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
4553 btrfs_item_ptr_offset(leaf
, path
->slots
[0] - 1),
4559 * make the item pointed to by the path smaller. new_size indicates
4560 * how small to make it, and from_end tells us if we just chop bytes
4561 * off the end of the item or if we shift the item to chop bytes off
4564 void btrfs_truncate_item(struct btrfs_fs_info
*fs_info
,
4565 struct btrfs_path
*path
, u32 new_size
, int from_end
)
4568 struct extent_buffer
*leaf
;
4569 struct btrfs_item
*item
;
4571 unsigned int data_end
;
4572 unsigned int old_data_start
;
4573 unsigned int old_size
;
4574 unsigned int size_diff
;
4576 struct btrfs_map_token token
;
4578 btrfs_init_map_token(&token
);
4580 leaf
= path
->nodes
[0];
4581 slot
= path
->slots
[0];
4583 old_size
= btrfs_item_size_nr(leaf
, slot
);
4584 if (old_size
== new_size
)
4587 nritems
= btrfs_header_nritems(leaf
);
4588 data_end
= leaf_data_end(fs_info
, leaf
);
4590 old_data_start
= btrfs_item_offset_nr(leaf
, slot
);
4592 size_diff
= old_size
- new_size
;
4595 BUG_ON(slot
>= nritems
);
4598 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4600 /* first correct the data pointers */
4601 for (i
= slot
; i
< nritems
; i
++) {
4603 item
= btrfs_item_nr(i
);
4605 ioff
= btrfs_token_item_offset(leaf
, item
, &token
);
4606 btrfs_set_token_item_offset(leaf
, item
,
4607 ioff
+ size_diff
, &token
);
4610 /* shift the data */
4612 memmove_extent_buffer(leaf
, BTRFS_LEAF_DATA_OFFSET
+
4613 data_end
+ size_diff
, BTRFS_LEAF_DATA_OFFSET
+
4614 data_end
, old_data_start
+ new_size
- data_end
);
4616 struct btrfs_disk_key disk_key
;
4619 btrfs_item_key(leaf
, &disk_key
, slot
);
4621 if (btrfs_disk_key_type(&disk_key
) == BTRFS_EXTENT_DATA_KEY
) {
4623 struct btrfs_file_extent_item
*fi
;
4625 fi
= btrfs_item_ptr(leaf
, slot
,
4626 struct btrfs_file_extent_item
);
4627 fi
= (struct btrfs_file_extent_item
*)(
4628 (unsigned long)fi
- size_diff
);
4630 if (btrfs_file_extent_type(leaf
, fi
) ==
4631 BTRFS_FILE_EXTENT_INLINE
) {
4632 ptr
= btrfs_item_ptr_offset(leaf
, slot
);
4633 memmove_extent_buffer(leaf
, ptr
,
4635 BTRFS_FILE_EXTENT_INLINE_DATA_START
);
4639 memmove_extent_buffer(leaf
, BTRFS_LEAF_DATA_OFFSET
+
4640 data_end
+ size_diff
, BTRFS_LEAF_DATA_OFFSET
+
4641 data_end
, old_data_start
- data_end
);
4643 offset
= btrfs_disk_key_offset(&disk_key
);
4644 btrfs_set_disk_key_offset(&disk_key
, offset
+ size_diff
);
4645 btrfs_set_item_key(leaf
, &disk_key
, slot
);
4647 fixup_low_keys(fs_info
, path
, &disk_key
, 1);
4650 item
= btrfs_item_nr(slot
);
4651 btrfs_set_item_size(leaf
, item
, new_size
);
4652 btrfs_mark_buffer_dirty(leaf
);
4654 if (btrfs_leaf_free_space(fs_info
, leaf
) < 0) {
4655 btrfs_print_leaf(leaf
);
4661 * make the item pointed to by the path bigger, data_size is the added size.
4663 void btrfs_extend_item(struct btrfs_fs_info
*fs_info
, struct btrfs_path
*path
,
4667 struct extent_buffer
*leaf
;
4668 struct btrfs_item
*item
;
4670 unsigned int data_end
;
4671 unsigned int old_data
;
4672 unsigned int old_size
;
4674 struct btrfs_map_token token
;
4676 btrfs_init_map_token(&token
);
4678 leaf
= path
->nodes
[0];
4680 nritems
= btrfs_header_nritems(leaf
);
4681 data_end
= leaf_data_end(fs_info
, leaf
);
4683 if (btrfs_leaf_free_space(fs_info
, leaf
) < data_size
) {
4684 btrfs_print_leaf(leaf
);
4687 slot
= path
->slots
[0];
4688 old_data
= btrfs_item_end_nr(leaf
, slot
);
4691 if (slot
>= nritems
) {
4692 btrfs_print_leaf(leaf
);
4693 btrfs_crit(fs_info
, "slot %d too large, nritems %d",
4699 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4701 /* first correct the data pointers */
4702 for (i
= slot
; i
< nritems
; i
++) {
4704 item
= btrfs_item_nr(i
);
4706 ioff
= btrfs_token_item_offset(leaf
, item
, &token
);
4707 btrfs_set_token_item_offset(leaf
, item
,
4708 ioff
- data_size
, &token
);
4711 /* shift the data */
4712 memmove_extent_buffer(leaf
, BTRFS_LEAF_DATA_OFFSET
+
4713 data_end
- data_size
, BTRFS_LEAF_DATA_OFFSET
+
4714 data_end
, old_data
- data_end
);
4716 data_end
= old_data
;
4717 old_size
= btrfs_item_size_nr(leaf
, slot
);
4718 item
= btrfs_item_nr(slot
);
4719 btrfs_set_item_size(leaf
, item
, old_size
+ data_size
);
4720 btrfs_mark_buffer_dirty(leaf
);
4722 if (btrfs_leaf_free_space(fs_info
, leaf
) < 0) {
4723 btrfs_print_leaf(leaf
);
4729 * this is a helper for btrfs_insert_empty_items, the main goal here is
4730 * to save stack depth by doing the bulk of the work in a function
4731 * that doesn't call btrfs_search_slot
4733 void setup_items_for_insert(struct btrfs_root
*root
, struct btrfs_path
*path
,
4734 const struct btrfs_key
*cpu_key
, u32
*data_size
,
4735 u32 total_data
, u32 total_size
, int nr
)
4737 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4738 struct btrfs_item
*item
;
4741 unsigned int data_end
;
4742 struct btrfs_disk_key disk_key
;
4743 struct extent_buffer
*leaf
;
4745 struct btrfs_map_token token
;
4747 if (path
->slots
[0] == 0) {
4748 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
);
4749 fixup_low_keys(fs_info
, path
, &disk_key
, 1);
4751 btrfs_unlock_up_safe(path
, 1);
4753 btrfs_init_map_token(&token
);
4755 leaf
= path
->nodes
[0];
4756 slot
= path
->slots
[0];
4758 nritems
= btrfs_header_nritems(leaf
);
4759 data_end
= leaf_data_end(fs_info
, leaf
);
4761 if (btrfs_leaf_free_space(fs_info
, leaf
) < total_size
) {
4762 btrfs_print_leaf(leaf
);
4763 btrfs_crit(fs_info
, "not enough freespace need %u have %d",
4764 total_size
, btrfs_leaf_free_space(fs_info
, leaf
));
4768 if (slot
!= nritems
) {
4769 unsigned int old_data
= btrfs_item_end_nr(leaf
, slot
);
4771 if (old_data
< data_end
) {
4772 btrfs_print_leaf(leaf
);
4773 btrfs_crit(fs_info
, "slot %d old_data %d data_end %d",
4774 slot
, old_data
, data_end
);
4778 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4780 /* first correct the data pointers */
4781 for (i
= slot
; i
< nritems
; i
++) {
4784 item
= btrfs_item_nr(i
);
4785 ioff
= btrfs_token_item_offset(leaf
, item
, &token
);
4786 btrfs_set_token_item_offset(leaf
, item
,
4787 ioff
- total_data
, &token
);
4789 /* shift the items */
4790 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
+ nr
),
4791 btrfs_item_nr_offset(slot
),
4792 (nritems
- slot
) * sizeof(struct btrfs_item
));
4794 /* shift the data */
4795 memmove_extent_buffer(leaf
, BTRFS_LEAF_DATA_OFFSET
+
4796 data_end
- total_data
, BTRFS_LEAF_DATA_OFFSET
+
4797 data_end
, old_data
- data_end
);
4798 data_end
= old_data
;
4801 /* setup the item for the new data */
4802 for (i
= 0; i
< nr
; i
++) {
4803 btrfs_cpu_key_to_disk(&disk_key
, cpu_key
+ i
);
4804 btrfs_set_item_key(leaf
, &disk_key
, slot
+ i
);
4805 item
= btrfs_item_nr(slot
+ i
);
4806 btrfs_set_token_item_offset(leaf
, item
,
4807 data_end
- data_size
[i
], &token
);
4808 data_end
-= data_size
[i
];
4809 btrfs_set_token_item_size(leaf
, item
, data_size
[i
], &token
);
4812 btrfs_set_header_nritems(leaf
, nritems
+ nr
);
4813 btrfs_mark_buffer_dirty(leaf
);
4815 if (btrfs_leaf_free_space(fs_info
, leaf
) < 0) {
4816 btrfs_print_leaf(leaf
);
4822 * Given a key and some data, insert items into the tree.
4823 * This does all the path init required, making room in the tree if needed.
4825 int btrfs_insert_empty_items(struct btrfs_trans_handle
*trans
,
4826 struct btrfs_root
*root
,
4827 struct btrfs_path
*path
,
4828 const struct btrfs_key
*cpu_key
, u32
*data_size
,
4837 for (i
= 0; i
< nr
; i
++)
4838 total_data
+= data_size
[i
];
4840 total_size
= total_data
+ (nr
* sizeof(struct btrfs_item
));
4841 ret
= btrfs_search_slot(trans
, root
, cpu_key
, path
, total_size
, 1);
4847 slot
= path
->slots
[0];
4850 setup_items_for_insert(root
, path
, cpu_key
, data_size
,
4851 total_data
, total_size
, nr
);
4856 * Given a key and some data, insert an item into the tree.
4857 * This does all the path init required, making room in the tree if needed.
4859 int btrfs_insert_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
4860 const struct btrfs_key
*cpu_key
, void *data
,
4864 struct btrfs_path
*path
;
4865 struct extent_buffer
*leaf
;
4868 path
= btrfs_alloc_path();
4871 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
4873 leaf
= path
->nodes
[0];
4874 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
4875 write_extent_buffer(leaf
, data
, ptr
, data_size
);
4876 btrfs_mark_buffer_dirty(leaf
);
4878 btrfs_free_path(path
);
4883 * delete the pointer from a given node.
4885 * the tree should have been previously balanced so the deletion does not
4888 static void del_ptr(struct btrfs_root
*root
, struct btrfs_path
*path
,
4889 int level
, int slot
)
4891 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4892 struct extent_buffer
*parent
= path
->nodes
[level
];
4896 nritems
= btrfs_header_nritems(parent
);
4897 if (slot
!= nritems
- 1) {
4899 tree_mod_log_eb_move(fs_info
, parent
, slot
,
4900 slot
+ 1, nritems
- slot
- 1);
4901 memmove_extent_buffer(parent
,
4902 btrfs_node_key_ptr_offset(slot
),
4903 btrfs_node_key_ptr_offset(slot
+ 1),
4904 sizeof(struct btrfs_key_ptr
) *
4905 (nritems
- slot
- 1));
4907 ret
= tree_mod_log_insert_key(fs_info
, parent
, slot
,
4908 MOD_LOG_KEY_REMOVE
, GFP_NOFS
);
4913 btrfs_set_header_nritems(parent
, nritems
);
4914 if (nritems
== 0 && parent
== root
->node
) {
4915 BUG_ON(btrfs_header_level(root
->node
) != 1);
4916 /* just turn the root into a leaf and break */
4917 btrfs_set_header_level(root
->node
, 0);
4918 } else if (slot
== 0) {
4919 struct btrfs_disk_key disk_key
;
4921 btrfs_node_key(parent
, &disk_key
, 0);
4922 fixup_low_keys(fs_info
, path
, &disk_key
, level
+ 1);
4924 btrfs_mark_buffer_dirty(parent
);
4928 * a helper function to delete the leaf pointed to by path->slots[1] and
4931 * This deletes the pointer in path->nodes[1] and frees the leaf
4932 * block extent. zero is returned if it all worked out, < 0 otherwise.
4934 * The path must have already been setup for deleting the leaf, including
4935 * all the proper balancing. path->nodes[1] must be locked.
4937 static noinline
void btrfs_del_leaf(struct btrfs_trans_handle
*trans
,
4938 struct btrfs_root
*root
,
4939 struct btrfs_path
*path
,
4940 struct extent_buffer
*leaf
)
4942 WARN_ON(btrfs_header_generation(leaf
) != trans
->transid
);
4943 del_ptr(root
, path
, 1, path
->slots
[1]);
4946 * btrfs_free_extent is expensive, we want to make sure we
4947 * aren't holding any locks when we call it
4949 btrfs_unlock_up_safe(path
, 0);
4951 root_sub_used(root
, leaf
->len
);
4953 extent_buffer_get(leaf
);
4954 btrfs_free_tree_block(trans
, root
, leaf
, 0, 1);
4955 free_extent_buffer_stale(leaf
);
4958 * delete the item at the leaf level in path. If that empties
4959 * the leaf, remove it from the tree
4961 int btrfs_del_items(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
4962 struct btrfs_path
*path
, int slot
, int nr
)
4964 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4965 struct extent_buffer
*leaf
;
4966 struct btrfs_item
*item
;
4973 struct btrfs_map_token token
;
4975 btrfs_init_map_token(&token
);
4977 leaf
= path
->nodes
[0];
4978 last_off
= btrfs_item_offset_nr(leaf
, slot
+ nr
- 1);
4980 for (i
= 0; i
< nr
; i
++)
4981 dsize
+= btrfs_item_size_nr(leaf
, slot
+ i
);
4983 nritems
= btrfs_header_nritems(leaf
);
4985 if (slot
+ nr
!= nritems
) {
4986 int data_end
= leaf_data_end(fs_info
, leaf
);
4988 memmove_extent_buffer(leaf
, BTRFS_LEAF_DATA_OFFSET
+
4990 BTRFS_LEAF_DATA_OFFSET
+ data_end
,
4991 last_off
- data_end
);
4993 for (i
= slot
+ nr
; i
< nritems
; i
++) {
4996 item
= btrfs_item_nr(i
);
4997 ioff
= btrfs_token_item_offset(leaf
, item
, &token
);
4998 btrfs_set_token_item_offset(leaf
, item
,
4999 ioff
+ dsize
, &token
);
5002 memmove_extent_buffer(leaf
, btrfs_item_nr_offset(slot
),
5003 btrfs_item_nr_offset(slot
+ nr
),
5004 sizeof(struct btrfs_item
) *
5005 (nritems
- slot
- nr
));
5007 btrfs_set_header_nritems(leaf
, nritems
- nr
);
5010 /* delete the leaf if we've emptied it */
5012 if (leaf
== root
->node
) {
5013 btrfs_set_header_level(leaf
, 0);
5015 btrfs_set_path_blocking(path
);
5016 clean_tree_block(fs_info
, leaf
);
5017 btrfs_del_leaf(trans
, root
, path
, leaf
);
5020 int used
= leaf_space_used(leaf
, 0, nritems
);
5022 struct btrfs_disk_key disk_key
;
5024 btrfs_item_key(leaf
, &disk_key
, 0);
5025 fixup_low_keys(fs_info
, path
, &disk_key
, 1);
5028 /* delete the leaf if it is mostly empty */
5029 if (used
< BTRFS_LEAF_DATA_SIZE(fs_info
) / 3) {
5030 /* push_leaf_left fixes the path.
5031 * make sure the path still points to our leaf
5032 * for possible call to del_ptr below
5034 slot
= path
->slots
[1];
5035 extent_buffer_get(leaf
);
5037 btrfs_set_path_blocking(path
);
5038 wret
= push_leaf_left(trans
, root
, path
, 1, 1,
5040 if (wret
< 0 && wret
!= -ENOSPC
)
5043 if (path
->nodes
[0] == leaf
&&
5044 btrfs_header_nritems(leaf
)) {
5045 wret
= push_leaf_right(trans
, root
, path
, 1,
5047 if (wret
< 0 && wret
!= -ENOSPC
)
5051 if (btrfs_header_nritems(leaf
) == 0) {
5052 path
->slots
[1] = slot
;
5053 btrfs_del_leaf(trans
, root
, path
, leaf
);
5054 free_extent_buffer(leaf
);
5057 /* if we're still in the path, make sure
5058 * we're dirty. Otherwise, one of the
5059 * push_leaf functions must have already
5060 * dirtied this buffer
5062 if (path
->nodes
[0] == leaf
)
5063 btrfs_mark_buffer_dirty(leaf
);
5064 free_extent_buffer(leaf
);
5067 btrfs_mark_buffer_dirty(leaf
);
5074 * search the tree again to find a leaf with lesser keys
5075 * returns 0 if it found something or 1 if there are no lesser leaves.
5076 * returns < 0 on io errors.
5078 * This may release the path, and so you may lose any locks held at the
5081 int btrfs_prev_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
5083 struct btrfs_key key
;
5084 struct btrfs_disk_key found_key
;
5087 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, 0);
5089 if (key
.offset
> 0) {
5091 } else if (key
.type
> 0) {
5093 key
.offset
= (u64
)-1;
5094 } else if (key
.objectid
> 0) {
5097 key
.offset
= (u64
)-1;
5102 btrfs_release_path(path
);
5103 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5106 btrfs_item_key(path
->nodes
[0], &found_key
, 0);
5107 ret
= comp_keys(&found_key
, &key
);
5109 * We might have had an item with the previous key in the tree right
5110 * before we released our path. And after we released our path, that
5111 * item might have been pushed to the first slot (0) of the leaf we
5112 * were holding due to a tree balance. Alternatively, an item with the
5113 * previous key can exist as the only element of a leaf (big fat item).
5114 * Therefore account for these 2 cases, so that our callers (like
5115 * btrfs_previous_item) don't miss an existing item with a key matching
5116 * the previous key we computed above.
5124 * A helper function to walk down the tree starting at min_key, and looking
5125 * for nodes or leaves that are have a minimum transaction id.
5126 * This is used by the btree defrag code, and tree logging
5128 * This does not cow, but it does stuff the starting key it finds back
5129 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5130 * key and get a writable path.
5132 * This does lock as it descends, and path->keep_locks should be set
5133 * to 1 by the caller.
5135 * This honors path->lowest_level to prevent descent past a given level
5138 * min_trans indicates the oldest transaction that you are interested
5139 * in walking through. Any nodes or leaves older than min_trans are
5140 * skipped over (without reading them).
5142 * returns zero if something useful was found, < 0 on error and 1 if there
5143 * was nothing in the tree that matched the search criteria.
5145 int btrfs_search_forward(struct btrfs_root
*root
, struct btrfs_key
*min_key
,
5146 struct btrfs_path
*path
,
5149 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5150 struct extent_buffer
*cur
;
5151 struct btrfs_key found_key
;
5157 int keep_locks
= path
->keep_locks
;
5159 path
->keep_locks
= 1;
5161 cur
= btrfs_read_lock_root_node(root
);
5162 level
= btrfs_header_level(cur
);
5163 WARN_ON(path
->nodes
[level
]);
5164 path
->nodes
[level
] = cur
;
5165 path
->locks
[level
] = BTRFS_READ_LOCK
;
5167 if (btrfs_header_generation(cur
) < min_trans
) {
5172 nritems
= btrfs_header_nritems(cur
);
5173 level
= btrfs_header_level(cur
);
5174 sret
= bin_search(cur
, min_key
, level
, &slot
);
5176 /* at the lowest level, we're done, setup the path and exit */
5177 if (level
== path
->lowest_level
) {
5178 if (slot
>= nritems
)
5181 path
->slots
[level
] = slot
;
5182 btrfs_item_key_to_cpu(cur
, &found_key
, slot
);
5185 if (sret
&& slot
> 0)
5188 * check this node pointer against the min_trans parameters.
5189 * If it is too old, old, skip to the next one.
5191 while (slot
< nritems
) {
5194 gen
= btrfs_node_ptr_generation(cur
, slot
);
5195 if (gen
< min_trans
) {
5203 * we didn't find a candidate key in this node, walk forward
5204 * and find another one
5206 if (slot
>= nritems
) {
5207 path
->slots
[level
] = slot
;
5208 btrfs_set_path_blocking(path
);
5209 sret
= btrfs_find_next_key(root
, path
, min_key
, level
,
5212 btrfs_release_path(path
);
5218 /* save our key for returning back */
5219 btrfs_node_key_to_cpu(cur
, &found_key
, slot
);
5220 path
->slots
[level
] = slot
;
5221 if (level
== path
->lowest_level
) {
5225 btrfs_set_path_blocking(path
);
5226 cur
= read_node_slot(fs_info
, cur
, slot
);
5232 btrfs_tree_read_lock(cur
);
5234 path
->locks
[level
- 1] = BTRFS_READ_LOCK
;
5235 path
->nodes
[level
- 1] = cur
;
5236 unlock_up(path
, level
, 1, 0, NULL
);
5237 btrfs_clear_path_blocking(path
, NULL
, 0);
5240 path
->keep_locks
= keep_locks
;
5242 btrfs_unlock_up_safe(path
, path
->lowest_level
+ 1);
5243 btrfs_set_path_blocking(path
);
5244 memcpy(min_key
, &found_key
, sizeof(found_key
));
5249 static int tree_move_down(struct btrfs_fs_info
*fs_info
,
5250 struct btrfs_path
*path
,
5253 struct extent_buffer
*eb
;
5255 BUG_ON(*level
== 0);
5256 eb
= read_node_slot(fs_info
, path
->nodes
[*level
], path
->slots
[*level
]);
5260 path
->nodes
[*level
- 1] = eb
;
5261 path
->slots
[*level
- 1] = 0;
5266 static int tree_move_next_or_upnext(struct btrfs_path
*path
,
5267 int *level
, int root_level
)
5271 nritems
= btrfs_header_nritems(path
->nodes
[*level
]);
5273 path
->slots
[*level
]++;
5275 while (path
->slots
[*level
] >= nritems
) {
5276 if (*level
== root_level
)
5280 path
->slots
[*level
] = 0;
5281 free_extent_buffer(path
->nodes
[*level
]);
5282 path
->nodes
[*level
] = NULL
;
5284 path
->slots
[*level
]++;
5286 nritems
= btrfs_header_nritems(path
->nodes
[*level
]);
5293 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5296 static int tree_advance(struct btrfs_fs_info
*fs_info
,
5297 struct btrfs_path
*path
,
5298 int *level
, int root_level
,
5300 struct btrfs_key
*key
)
5304 if (*level
== 0 || !allow_down
) {
5305 ret
= tree_move_next_or_upnext(path
, level
, root_level
);
5307 ret
= tree_move_down(fs_info
, path
, level
);
5311 btrfs_item_key_to_cpu(path
->nodes
[*level
], key
,
5312 path
->slots
[*level
]);
5314 btrfs_node_key_to_cpu(path
->nodes
[*level
], key
,
5315 path
->slots
[*level
]);
5320 static int tree_compare_item(struct btrfs_path
*left_path
,
5321 struct btrfs_path
*right_path
,
5326 unsigned long off1
, off2
;
5328 len1
= btrfs_item_size_nr(left_path
->nodes
[0], left_path
->slots
[0]);
5329 len2
= btrfs_item_size_nr(right_path
->nodes
[0], right_path
->slots
[0]);
5333 off1
= btrfs_item_ptr_offset(left_path
->nodes
[0], left_path
->slots
[0]);
5334 off2
= btrfs_item_ptr_offset(right_path
->nodes
[0],
5335 right_path
->slots
[0]);
5337 read_extent_buffer(left_path
->nodes
[0], tmp_buf
, off1
, len1
);
5339 cmp
= memcmp_extent_buffer(right_path
->nodes
[0], tmp_buf
, off2
, len1
);
5346 #define ADVANCE_ONLY_NEXT -1
5349 * This function compares two trees and calls the provided callback for
5350 * every changed/new/deleted item it finds.
5351 * If shared tree blocks are encountered, whole subtrees are skipped, making
5352 * the compare pretty fast on snapshotted subvolumes.
5354 * This currently works on commit roots only. As commit roots are read only,
5355 * we don't do any locking. The commit roots are protected with transactions.
5356 * Transactions are ended and rejoined when a commit is tried in between.
5358 * This function checks for modifications done to the trees while comparing.
5359 * If it detects a change, it aborts immediately.
5361 int btrfs_compare_trees(struct btrfs_root
*left_root
,
5362 struct btrfs_root
*right_root
,
5363 btrfs_changed_cb_t changed_cb
, void *ctx
)
5365 struct btrfs_fs_info
*fs_info
= left_root
->fs_info
;
5368 struct btrfs_path
*left_path
= NULL
;
5369 struct btrfs_path
*right_path
= NULL
;
5370 struct btrfs_key left_key
;
5371 struct btrfs_key right_key
;
5372 char *tmp_buf
= NULL
;
5373 int left_root_level
;
5374 int right_root_level
;
5377 int left_end_reached
;
5378 int right_end_reached
;
5386 left_path
= btrfs_alloc_path();
5391 right_path
= btrfs_alloc_path();
5397 tmp_buf
= kvmalloc(fs_info
->nodesize
, GFP_KERNEL
);
5403 left_path
->search_commit_root
= 1;
5404 left_path
->skip_locking
= 1;
5405 right_path
->search_commit_root
= 1;
5406 right_path
->skip_locking
= 1;
5409 * Strategy: Go to the first items of both trees. Then do
5411 * If both trees are at level 0
5412 * Compare keys of current items
5413 * If left < right treat left item as new, advance left tree
5415 * If left > right treat right item as deleted, advance right tree
5417 * If left == right do deep compare of items, treat as changed if
5418 * needed, advance both trees and repeat
5419 * If both trees are at the same level but not at level 0
5420 * Compare keys of current nodes/leafs
5421 * If left < right advance left tree and repeat
5422 * If left > right advance right tree and repeat
5423 * If left == right compare blockptrs of the next nodes/leafs
5424 * If they match advance both trees but stay at the same level
5426 * If they don't match advance both trees while allowing to go
5428 * If tree levels are different
5429 * Advance the tree that needs it and repeat
5431 * Advancing a tree means:
5432 * If we are at level 0, try to go to the next slot. If that's not
5433 * possible, go one level up and repeat. Stop when we found a level
5434 * where we could go to the next slot. We may at this point be on a
5437 * If we are not at level 0 and not on shared tree blocks, go one
5440 * If we are not at level 0 and on shared tree blocks, go one slot to
5441 * the right if possible or go up and right.
5444 down_read(&fs_info
->commit_root_sem
);
5445 left_level
= btrfs_header_level(left_root
->commit_root
);
5446 left_root_level
= left_level
;
5447 left_path
->nodes
[left_level
] =
5448 btrfs_clone_extent_buffer(left_root
->commit_root
);
5449 if (!left_path
->nodes
[left_level
]) {
5450 up_read(&fs_info
->commit_root_sem
);
5454 extent_buffer_get(left_path
->nodes
[left_level
]);
5456 right_level
= btrfs_header_level(right_root
->commit_root
);
5457 right_root_level
= right_level
;
5458 right_path
->nodes
[right_level
] =
5459 btrfs_clone_extent_buffer(right_root
->commit_root
);
5460 if (!right_path
->nodes
[right_level
]) {
5461 up_read(&fs_info
->commit_root_sem
);
5465 extent_buffer_get(right_path
->nodes
[right_level
]);
5466 up_read(&fs_info
->commit_root_sem
);
5468 if (left_level
== 0)
5469 btrfs_item_key_to_cpu(left_path
->nodes
[left_level
],
5470 &left_key
, left_path
->slots
[left_level
]);
5472 btrfs_node_key_to_cpu(left_path
->nodes
[left_level
],
5473 &left_key
, left_path
->slots
[left_level
]);
5474 if (right_level
== 0)
5475 btrfs_item_key_to_cpu(right_path
->nodes
[right_level
],
5476 &right_key
, right_path
->slots
[right_level
]);
5478 btrfs_node_key_to_cpu(right_path
->nodes
[right_level
],
5479 &right_key
, right_path
->slots
[right_level
]);
5481 left_end_reached
= right_end_reached
= 0;
5482 advance_left
= advance_right
= 0;
5486 if (advance_left
&& !left_end_reached
) {
5487 ret
= tree_advance(fs_info
, left_path
, &left_level
,
5489 advance_left
!= ADVANCE_ONLY_NEXT
,
5492 left_end_reached
= ADVANCE
;
5497 if (advance_right
&& !right_end_reached
) {
5498 ret
= tree_advance(fs_info
, right_path
, &right_level
,
5500 advance_right
!= ADVANCE_ONLY_NEXT
,
5503 right_end_reached
= ADVANCE
;
5509 if (left_end_reached
&& right_end_reached
) {
5512 } else if (left_end_reached
) {
5513 if (right_level
== 0) {
5514 ret
= changed_cb(left_root
, right_root
,
5515 left_path
, right_path
,
5517 BTRFS_COMPARE_TREE_DELETED
,
5522 advance_right
= ADVANCE
;
5524 } else if (right_end_reached
) {
5525 if (left_level
== 0) {
5526 ret
= changed_cb(left_root
, right_root
,
5527 left_path
, right_path
,
5529 BTRFS_COMPARE_TREE_NEW
,
5534 advance_left
= ADVANCE
;
5538 if (left_level
== 0 && right_level
== 0) {
5539 cmp
= btrfs_comp_cpu_keys(&left_key
, &right_key
);
5541 ret
= changed_cb(left_root
, right_root
,
5542 left_path
, right_path
,
5544 BTRFS_COMPARE_TREE_NEW
,
5548 advance_left
= ADVANCE
;
5549 } else if (cmp
> 0) {
5550 ret
= changed_cb(left_root
, right_root
,
5551 left_path
, right_path
,
5553 BTRFS_COMPARE_TREE_DELETED
,
5557 advance_right
= ADVANCE
;
5559 enum btrfs_compare_tree_result result
;
5561 WARN_ON(!extent_buffer_uptodate(left_path
->nodes
[0]));
5562 ret
= tree_compare_item(left_path
, right_path
,
5565 result
= BTRFS_COMPARE_TREE_CHANGED
;
5567 result
= BTRFS_COMPARE_TREE_SAME
;
5568 ret
= changed_cb(left_root
, right_root
,
5569 left_path
, right_path
,
5570 &left_key
, result
, ctx
);
5573 advance_left
= ADVANCE
;
5574 advance_right
= ADVANCE
;
5576 } else if (left_level
== right_level
) {
5577 cmp
= btrfs_comp_cpu_keys(&left_key
, &right_key
);
5579 advance_left
= ADVANCE
;
5580 } else if (cmp
> 0) {
5581 advance_right
= ADVANCE
;
5583 left_blockptr
= btrfs_node_blockptr(
5584 left_path
->nodes
[left_level
],
5585 left_path
->slots
[left_level
]);
5586 right_blockptr
= btrfs_node_blockptr(
5587 right_path
->nodes
[right_level
],
5588 right_path
->slots
[right_level
]);
5589 left_gen
= btrfs_node_ptr_generation(
5590 left_path
->nodes
[left_level
],
5591 left_path
->slots
[left_level
]);
5592 right_gen
= btrfs_node_ptr_generation(
5593 right_path
->nodes
[right_level
],
5594 right_path
->slots
[right_level
]);
5595 if (left_blockptr
== right_blockptr
&&
5596 left_gen
== right_gen
) {
5598 * As we're on a shared block, don't
5599 * allow to go deeper.
5601 advance_left
= ADVANCE_ONLY_NEXT
;
5602 advance_right
= ADVANCE_ONLY_NEXT
;
5604 advance_left
= ADVANCE
;
5605 advance_right
= ADVANCE
;
5608 } else if (left_level
< right_level
) {
5609 advance_right
= ADVANCE
;
5611 advance_left
= ADVANCE
;
5616 btrfs_free_path(left_path
);
5617 btrfs_free_path(right_path
);
5623 * this is similar to btrfs_next_leaf, but does not try to preserve
5624 * and fixup the path. It looks for and returns the next key in the
5625 * tree based on the current path and the min_trans parameters.
5627 * 0 is returned if another key is found, < 0 if there are any errors
5628 * and 1 is returned if there are no higher keys in the tree
5630 * path->keep_locks should be set to 1 on the search made before
5631 * calling this function.
5633 int btrfs_find_next_key(struct btrfs_root
*root
, struct btrfs_path
*path
,
5634 struct btrfs_key
*key
, int level
, u64 min_trans
)
5637 struct extent_buffer
*c
;
5639 WARN_ON(!path
->keep_locks
);
5640 while (level
< BTRFS_MAX_LEVEL
) {
5641 if (!path
->nodes
[level
])
5644 slot
= path
->slots
[level
] + 1;
5645 c
= path
->nodes
[level
];
5647 if (slot
>= btrfs_header_nritems(c
)) {
5650 struct btrfs_key cur_key
;
5651 if (level
+ 1 >= BTRFS_MAX_LEVEL
||
5652 !path
->nodes
[level
+ 1])
5655 if (path
->locks
[level
+ 1]) {
5660 slot
= btrfs_header_nritems(c
) - 1;
5662 btrfs_item_key_to_cpu(c
, &cur_key
, slot
);
5664 btrfs_node_key_to_cpu(c
, &cur_key
, slot
);
5666 orig_lowest
= path
->lowest_level
;
5667 btrfs_release_path(path
);
5668 path
->lowest_level
= level
;
5669 ret
= btrfs_search_slot(NULL
, root
, &cur_key
, path
,
5671 path
->lowest_level
= orig_lowest
;
5675 c
= path
->nodes
[level
];
5676 slot
= path
->slots
[level
];
5683 btrfs_item_key_to_cpu(c
, key
, slot
);
5685 u64 gen
= btrfs_node_ptr_generation(c
, slot
);
5687 if (gen
< min_trans
) {
5691 btrfs_node_key_to_cpu(c
, key
, slot
);
5699 * search the tree again to find a leaf with greater keys
5700 * returns 0 if it found something or 1 if there are no greater leaves.
5701 * returns < 0 on io errors.
5703 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
)
5705 return btrfs_next_old_leaf(root
, path
, 0);
5708 int btrfs_next_old_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
,
5713 struct extent_buffer
*c
;
5714 struct extent_buffer
*next
;
5715 struct btrfs_key key
;
5718 int old_spinning
= path
->leave_spinning
;
5719 int next_rw_lock
= 0;
5721 nritems
= btrfs_header_nritems(path
->nodes
[0]);
5725 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, nritems
- 1);
5730 btrfs_release_path(path
);
5732 path
->keep_locks
= 1;
5733 path
->leave_spinning
= 1;
5736 ret
= btrfs_search_old_slot(root
, &key
, path
, time_seq
);
5738 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5739 path
->keep_locks
= 0;
5744 nritems
= btrfs_header_nritems(path
->nodes
[0]);
5746 * by releasing the path above we dropped all our locks. A balance
5747 * could have added more items next to the key that used to be
5748 * at the very end of the block. So, check again here and
5749 * advance the path if there are now more items available.
5751 if (nritems
> 0 && path
->slots
[0] < nritems
- 1) {
5758 * So the above check misses one case:
5759 * - after releasing the path above, someone has removed the item that
5760 * used to be at the very end of the block, and balance between leafs
5761 * gets another one with bigger key.offset to replace it.
5763 * This one should be returned as well, or we can get leaf corruption
5764 * later(esp. in __btrfs_drop_extents()).
5766 * And a bit more explanation about this check,
5767 * with ret > 0, the key isn't found, the path points to the slot
5768 * where it should be inserted, so the path->slots[0] item must be the
5771 if (nritems
> 0 && ret
> 0 && path
->slots
[0] == nritems
- 1) {
5776 while (level
< BTRFS_MAX_LEVEL
) {
5777 if (!path
->nodes
[level
]) {
5782 slot
= path
->slots
[level
] + 1;
5783 c
= path
->nodes
[level
];
5784 if (slot
>= btrfs_header_nritems(c
)) {
5786 if (level
== BTRFS_MAX_LEVEL
) {
5794 btrfs_tree_unlock_rw(next
, next_rw_lock
);
5795 free_extent_buffer(next
);
5799 next_rw_lock
= path
->locks
[level
];
5800 ret
= read_block_for_search(root
, path
, &next
, level
,
5806 btrfs_release_path(path
);
5810 if (!path
->skip_locking
) {
5811 ret
= btrfs_try_tree_read_lock(next
);
5812 if (!ret
&& time_seq
) {
5814 * If we don't get the lock, we may be racing
5815 * with push_leaf_left, holding that lock while
5816 * itself waiting for the leaf we've currently
5817 * locked. To solve this situation, we give up
5818 * on our lock and cycle.
5820 free_extent_buffer(next
);
5821 btrfs_release_path(path
);
5826 btrfs_set_path_blocking(path
);
5827 btrfs_tree_read_lock(next
);
5828 btrfs_clear_path_blocking(path
, next
,
5831 next_rw_lock
= BTRFS_READ_LOCK
;
5835 path
->slots
[level
] = slot
;
5838 c
= path
->nodes
[level
];
5839 if (path
->locks
[level
])
5840 btrfs_tree_unlock_rw(c
, path
->locks
[level
]);
5842 free_extent_buffer(c
);
5843 path
->nodes
[level
] = next
;
5844 path
->slots
[level
] = 0;
5845 if (!path
->skip_locking
)
5846 path
->locks
[level
] = next_rw_lock
;
5850 ret
= read_block_for_search(root
, path
, &next
, level
,
5856 btrfs_release_path(path
);
5860 if (!path
->skip_locking
) {
5861 ret
= btrfs_try_tree_read_lock(next
);
5863 btrfs_set_path_blocking(path
);
5864 btrfs_tree_read_lock(next
);
5865 btrfs_clear_path_blocking(path
, next
,
5868 next_rw_lock
= BTRFS_READ_LOCK
;
5873 unlock_up(path
, 0, 1, 0, NULL
);
5874 path
->leave_spinning
= old_spinning
;
5876 btrfs_set_path_blocking(path
);
5882 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5883 * searching until it gets past min_objectid or finds an item of 'type'
5885 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5887 int btrfs_previous_item(struct btrfs_root
*root
,
5888 struct btrfs_path
*path
, u64 min_objectid
,
5891 struct btrfs_key found_key
;
5892 struct extent_buffer
*leaf
;
5897 if (path
->slots
[0] == 0) {
5898 btrfs_set_path_blocking(path
);
5899 ret
= btrfs_prev_leaf(root
, path
);
5905 leaf
= path
->nodes
[0];
5906 nritems
= btrfs_header_nritems(leaf
);
5909 if (path
->slots
[0] == nritems
)
5912 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5913 if (found_key
.objectid
< min_objectid
)
5915 if (found_key
.type
== type
)
5917 if (found_key
.objectid
== min_objectid
&&
5918 found_key
.type
< type
)
5925 * search in extent tree to find a previous Metadata/Data extent item with
5928 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5930 int btrfs_previous_extent_item(struct btrfs_root
*root
,
5931 struct btrfs_path
*path
, u64 min_objectid
)
5933 struct btrfs_key found_key
;
5934 struct extent_buffer
*leaf
;
5939 if (path
->slots
[0] == 0) {
5940 btrfs_set_path_blocking(path
);
5941 ret
= btrfs_prev_leaf(root
, path
);
5947 leaf
= path
->nodes
[0];
5948 nritems
= btrfs_header_nritems(leaf
);
5951 if (path
->slots
[0] == nritems
)
5954 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5955 if (found_key
.objectid
< min_objectid
)
5957 if (found_key
.type
== BTRFS_EXTENT_ITEM_KEY
||
5958 found_key
.type
== BTRFS_METADATA_ITEM_KEY
)
5960 if (found_key
.objectid
== min_objectid
&&
5961 found_key
.type
< BTRFS_EXTENT_ITEM_KEY
)