1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007,2008 Oracle. All rights reserved.
6 #include <linux/sched.h>
7 #include <linux/slab.h>
8 #include <linux/rbtree.h>
12 #include "transaction.h"
13 #include "print-tree.h"
18 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
19 *root, struct btrfs_path *path, int level);
20 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
21 const struct btrfs_key *ins_key, struct btrfs_path *path,
22 int data_size, int extend);
23 static int push_node_left(struct btrfs_trans_handle *trans,
24 struct extent_buffer *dst,
25 struct extent_buffer *src, int empty);
26 static int balance_node_right(struct btrfs_trans_handle *trans,
27 struct extent_buffer *dst_buf,
28 struct extent_buffer *src_buf);
29 static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
32 static const struct btrfs_csums {
36 [BTRFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" },
39 int btrfs_super_csum_size(const struct btrfs_super_block *s)
41 u16 t = btrfs_super_csum_type(s);
43 * csum type is validated at mount time
45 return btrfs_csums[t].size;
48 const char *btrfs_super_csum_name(u16 csum_type)
50 /* csum type is validated at mount time */
51 return btrfs_csums[csum_type].name;
54 struct btrfs_path *btrfs_alloc_path(void)
56 return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
60 * set all locked nodes in the path to blocking locks. This should
61 * be done before scheduling
63 noinline void btrfs_set_path_blocking(struct btrfs_path *p)
66 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
67 if (!p->nodes[i] || !p->locks[i])
70 * If we currently have a spinning reader or writer lock this
71 * will bump the count of blocking holders and drop the
74 if (p->locks[i] == BTRFS_READ_LOCK) {
75 btrfs_set_lock_blocking_read(p->nodes[i]);
76 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
77 } else if (p->locks[i] == BTRFS_WRITE_LOCK) {
78 btrfs_set_lock_blocking_write(p->nodes[i]);
79 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
84 /* this also releases the path */
85 void btrfs_free_path(struct btrfs_path *p)
89 btrfs_release_path(p);
90 kmem_cache_free(btrfs_path_cachep, p);
94 * path release drops references on the extent buffers in the path
95 * and it drops any locks held by this path
97 * It is safe to call this on paths that no locks or extent buffers held.
99 noinline void btrfs_release_path(struct btrfs_path *p)
103 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
108 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
111 free_extent_buffer(p->nodes[i]);
117 * safely gets a reference on the root node of a tree. A lock
118 * is not taken, so a concurrent writer may put a different node
119 * at the root of the tree. See btrfs_lock_root_node for the
122 * The extent buffer returned by this has a reference taken, so
123 * it won't disappear. It may stop being the root of the tree
124 * at any time because there are no locks held.
126 struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
128 struct extent_buffer *eb;
132 eb = rcu_dereference(root->node);
135 * RCU really hurts here, we could free up the root node because
136 * it was COWed but we may not get the new root node yet so do
137 * the inc_not_zero dance and if it doesn't work then
138 * synchronize_rcu and try again.
140 if (atomic_inc_not_zero(&eb->refs)) {
150 /* loop around taking references on and locking the root node of the
151 * tree until you end up with a lock on the root. A locked buffer
152 * is returned, with a reference held.
154 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
156 struct extent_buffer *eb;
159 eb = btrfs_root_node(root);
161 if (eb == root->node)
163 btrfs_tree_unlock(eb);
164 free_extent_buffer(eb);
169 /* loop around taking references on and locking the root node of the
170 * tree until you end up with a lock on the root. A locked buffer
171 * is returned, with a reference held.
173 struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
175 struct extent_buffer *eb;
178 eb = btrfs_root_node(root);
179 btrfs_tree_read_lock(eb);
180 if (eb == root->node)
182 btrfs_tree_read_unlock(eb);
183 free_extent_buffer(eb);
188 /* cowonly root (everything not a reference counted cow subvolume), just get
189 * put onto a simple dirty list. transaction.c walks this to make sure they
190 * get properly updated on disk.
192 static void add_root_to_dirty_list(struct btrfs_root *root)
194 struct btrfs_fs_info *fs_info = root->fs_info;
196 if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
197 !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
200 spin_lock(&fs_info->trans_lock);
201 if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
202 /* Want the extent tree to be the last on the list */
203 if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID)
204 list_move_tail(&root->dirty_list,
205 &fs_info->dirty_cowonly_roots);
207 list_move(&root->dirty_list,
208 &fs_info->dirty_cowonly_roots);
210 spin_unlock(&fs_info->trans_lock);
214 * used by snapshot creation to make a copy of a root for a tree with
215 * a given objectid. The buffer with the new root node is returned in
216 * cow_ret, and this func returns zero on success or a negative error code.
218 int btrfs_copy_root(struct btrfs_trans_handle *trans,
219 struct btrfs_root *root,
220 struct extent_buffer *buf,
221 struct extent_buffer **cow_ret, u64 new_root_objectid)
223 struct btrfs_fs_info *fs_info = root->fs_info;
224 struct extent_buffer *cow;
227 struct btrfs_disk_key disk_key;
229 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
230 trans->transid != fs_info->running_transaction->transid);
231 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
232 trans->transid != root->last_trans);
234 level = btrfs_header_level(buf);
236 btrfs_item_key(buf, &disk_key, 0);
238 btrfs_node_key(buf, &disk_key, 0);
240 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
241 &disk_key, level, buf->start, 0);
245 copy_extent_buffer_full(cow, buf);
246 btrfs_set_header_bytenr(cow, cow->start);
247 btrfs_set_header_generation(cow, trans->transid);
248 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
249 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
250 BTRFS_HEADER_FLAG_RELOC);
251 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
252 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
254 btrfs_set_header_owner(cow, new_root_objectid);
256 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
258 WARN_ON(btrfs_header_generation(buf) > trans->transid);
259 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
260 ret = btrfs_inc_ref(trans, root, cow, 1);
262 ret = btrfs_inc_ref(trans, root, cow, 0);
264 btrfs_tree_unlock(cow);
265 free_extent_buffer(cow);
266 btrfs_abort_transaction(trans, ret);
270 btrfs_mark_buffer_dirty(cow);
279 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
280 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
282 MOD_LOG_ROOT_REPLACE,
285 struct tree_mod_root {
290 struct tree_mod_elem {
296 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
299 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
302 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
303 struct btrfs_disk_key key;
306 /* this is used for op == MOD_LOG_MOVE_KEYS */
312 /* this is used for op == MOD_LOG_ROOT_REPLACE */
313 struct tree_mod_root old_root;
317 * Pull a new tree mod seq number for our operation.
319 static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
321 return atomic64_inc_return(&fs_info->tree_mod_seq);
325 * This adds a new blocker to the tree mod log's blocker list if the @elem
326 * passed does not already have a sequence number set. So when a caller expects
327 * to record tree modifications, it should ensure to set elem->seq to zero
328 * before calling btrfs_get_tree_mod_seq.
329 * Returns a fresh, unused tree log modification sequence number, even if no new
332 u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
333 struct seq_list *elem)
335 write_lock(&fs_info->tree_mod_log_lock);
337 elem->seq = btrfs_inc_tree_mod_seq(fs_info);
338 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
340 write_unlock(&fs_info->tree_mod_log_lock);
345 void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
346 struct seq_list *elem)
348 struct rb_root *tm_root;
349 struct rb_node *node;
350 struct rb_node *next;
351 struct seq_list *cur_elem;
352 struct tree_mod_elem *tm;
353 u64 min_seq = (u64)-1;
354 u64 seq_putting = elem->seq;
359 write_lock(&fs_info->tree_mod_log_lock);
360 list_del(&elem->list);
363 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
364 if (cur_elem->seq < min_seq) {
365 if (seq_putting > cur_elem->seq) {
367 * blocker with lower sequence number exists, we
368 * cannot remove anything from the log
370 write_unlock(&fs_info->tree_mod_log_lock);
373 min_seq = cur_elem->seq;
378 * anything that's lower than the lowest existing (read: blocked)
379 * sequence number can be removed from the tree.
381 tm_root = &fs_info->tree_mod_log;
382 for (node = rb_first(tm_root); node; node = next) {
383 next = rb_next(node);
384 tm = rb_entry(node, struct tree_mod_elem, node);
385 if (tm->seq >= min_seq)
387 rb_erase(node, tm_root);
390 write_unlock(&fs_info->tree_mod_log_lock);
394 * key order of the log:
395 * node/leaf start address -> sequence
397 * The 'start address' is the logical address of the *new* root node
398 * for root replace operations, or the logical address of the affected
399 * block for all other operations.
402 __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
404 struct rb_root *tm_root;
405 struct rb_node **new;
406 struct rb_node *parent = NULL;
407 struct tree_mod_elem *cur;
409 lockdep_assert_held_write(&fs_info->tree_mod_log_lock);
411 tm->seq = btrfs_inc_tree_mod_seq(fs_info);
413 tm_root = &fs_info->tree_mod_log;
414 new = &tm_root->rb_node;
416 cur = rb_entry(*new, struct tree_mod_elem, node);
418 if (cur->logical < tm->logical)
419 new = &((*new)->rb_left);
420 else if (cur->logical > tm->logical)
421 new = &((*new)->rb_right);
422 else if (cur->seq < tm->seq)
423 new = &((*new)->rb_left);
424 else if (cur->seq > tm->seq)
425 new = &((*new)->rb_right);
430 rb_link_node(&tm->node, parent, new);
431 rb_insert_color(&tm->node, tm_root);
436 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
437 * returns zero with the tree_mod_log_lock acquired. The caller must hold
438 * this until all tree mod log insertions are recorded in the rb tree and then
439 * write unlock fs_info::tree_mod_log_lock.
441 static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
442 struct extent_buffer *eb) {
444 if (list_empty(&(fs_info)->tree_mod_seq_list))
446 if (eb && btrfs_header_level(eb) == 0)
449 write_lock(&fs_info->tree_mod_log_lock);
450 if (list_empty(&(fs_info)->tree_mod_seq_list)) {
451 write_unlock(&fs_info->tree_mod_log_lock);
458 /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
459 static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
460 struct extent_buffer *eb)
463 if (list_empty(&(fs_info)->tree_mod_seq_list))
465 if (eb && btrfs_header_level(eb) == 0)
471 static struct tree_mod_elem *
472 alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
473 enum mod_log_op op, gfp_t flags)
475 struct tree_mod_elem *tm;
477 tm = kzalloc(sizeof(*tm), flags);
481 tm->logical = eb->start;
482 if (op != MOD_LOG_KEY_ADD) {
483 btrfs_node_key(eb, &tm->key, slot);
484 tm->blockptr = btrfs_node_blockptr(eb, slot);
488 tm->generation = btrfs_node_ptr_generation(eb, slot);
489 RB_CLEAR_NODE(&tm->node);
494 static noinline int tree_mod_log_insert_key(struct extent_buffer *eb, int slot,
495 enum mod_log_op op, gfp_t flags)
497 struct tree_mod_elem *tm;
500 if (!tree_mod_need_log(eb->fs_info, eb))
503 tm = alloc_tree_mod_elem(eb, slot, op, flags);
507 if (tree_mod_dont_log(eb->fs_info, eb)) {
512 ret = __tree_mod_log_insert(eb->fs_info, tm);
513 write_unlock(&eb->fs_info->tree_mod_log_lock);
520 static noinline int tree_mod_log_insert_move(struct extent_buffer *eb,
521 int dst_slot, int src_slot, int nr_items)
523 struct tree_mod_elem *tm = NULL;
524 struct tree_mod_elem **tm_list = NULL;
529 if (!tree_mod_need_log(eb->fs_info, eb))
532 tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS);
536 tm = kzalloc(sizeof(*tm), GFP_NOFS);
542 tm->logical = eb->start;
544 tm->move.dst_slot = dst_slot;
545 tm->move.nr_items = nr_items;
546 tm->op = MOD_LOG_MOVE_KEYS;
548 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
549 tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
550 MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
557 if (tree_mod_dont_log(eb->fs_info, eb))
562 * When we override something during the move, we log these removals.
563 * This can only happen when we move towards the beginning of the
564 * buffer, i.e. dst_slot < src_slot.
566 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
567 ret = __tree_mod_log_insert(eb->fs_info, tm_list[i]);
572 ret = __tree_mod_log_insert(eb->fs_info, tm);
575 write_unlock(&eb->fs_info->tree_mod_log_lock);
580 for (i = 0; i < nr_items; i++) {
581 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
582 rb_erase(&tm_list[i]->node, &eb->fs_info->tree_mod_log);
586 write_unlock(&eb->fs_info->tree_mod_log_lock);
594 __tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
595 struct tree_mod_elem **tm_list,
601 for (i = nritems - 1; i >= 0; i--) {
602 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
604 for (j = nritems - 1; j > i; j--)
605 rb_erase(&tm_list[j]->node,
606 &fs_info->tree_mod_log);
614 static noinline int tree_mod_log_insert_root(struct extent_buffer *old_root,
615 struct extent_buffer *new_root, int log_removal)
617 struct btrfs_fs_info *fs_info = old_root->fs_info;
618 struct tree_mod_elem *tm = NULL;
619 struct tree_mod_elem **tm_list = NULL;
624 if (!tree_mod_need_log(fs_info, NULL))
627 if (log_removal && btrfs_header_level(old_root) > 0) {
628 nritems = btrfs_header_nritems(old_root);
629 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
635 for (i = 0; i < nritems; i++) {
636 tm_list[i] = alloc_tree_mod_elem(old_root, i,
637 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
645 tm = kzalloc(sizeof(*tm), GFP_NOFS);
651 tm->logical = new_root->start;
652 tm->old_root.logical = old_root->start;
653 tm->old_root.level = btrfs_header_level(old_root);
654 tm->generation = btrfs_header_generation(old_root);
655 tm->op = MOD_LOG_ROOT_REPLACE;
657 if (tree_mod_dont_log(fs_info, NULL))
661 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
663 ret = __tree_mod_log_insert(fs_info, tm);
665 write_unlock(&fs_info->tree_mod_log_lock);
674 for (i = 0; i < nritems; i++)
683 static struct tree_mod_elem *
684 __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
687 struct rb_root *tm_root;
688 struct rb_node *node;
689 struct tree_mod_elem *cur = NULL;
690 struct tree_mod_elem *found = NULL;
692 read_lock(&fs_info->tree_mod_log_lock);
693 tm_root = &fs_info->tree_mod_log;
694 node = tm_root->rb_node;
696 cur = rb_entry(node, struct tree_mod_elem, node);
697 if (cur->logical < start) {
698 node = node->rb_left;
699 } else if (cur->logical > start) {
700 node = node->rb_right;
701 } else if (cur->seq < min_seq) {
702 node = node->rb_left;
703 } else if (!smallest) {
704 /* we want the node with the highest seq */
706 BUG_ON(found->seq > cur->seq);
708 node = node->rb_left;
709 } else if (cur->seq > min_seq) {
710 /* we want the node with the smallest seq */
712 BUG_ON(found->seq < cur->seq);
714 node = node->rb_right;
720 read_unlock(&fs_info->tree_mod_log_lock);
726 * this returns the element from the log with the smallest time sequence
727 * value that's in the log (the oldest log item). any element with a time
728 * sequence lower than min_seq will be ignored.
730 static struct tree_mod_elem *
731 tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
734 return __tree_mod_log_search(fs_info, start, min_seq, 1);
738 * this returns the element from the log with the largest time sequence
739 * value that's in the log (the most recent log item). any element with
740 * a time sequence lower than min_seq will be ignored.
742 static struct tree_mod_elem *
743 tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
745 return __tree_mod_log_search(fs_info, start, min_seq, 0);
748 static noinline int tree_mod_log_eb_copy(struct extent_buffer *dst,
749 struct extent_buffer *src, unsigned long dst_offset,
750 unsigned long src_offset, int nr_items)
752 struct btrfs_fs_info *fs_info = dst->fs_info;
754 struct tree_mod_elem **tm_list = NULL;
755 struct tree_mod_elem **tm_list_add, **tm_list_rem;
759 if (!tree_mod_need_log(fs_info, NULL))
762 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
765 tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
770 tm_list_add = tm_list;
771 tm_list_rem = tm_list + nr_items;
772 for (i = 0; i < nr_items; i++) {
773 tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
774 MOD_LOG_KEY_REMOVE, GFP_NOFS);
775 if (!tm_list_rem[i]) {
780 tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
781 MOD_LOG_KEY_ADD, GFP_NOFS);
782 if (!tm_list_add[i]) {
788 if (tree_mod_dont_log(fs_info, NULL))
792 for (i = 0; i < nr_items; i++) {
793 ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
796 ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
801 write_unlock(&fs_info->tree_mod_log_lock);
807 for (i = 0; i < nr_items * 2; i++) {
808 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
809 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
813 write_unlock(&fs_info->tree_mod_log_lock);
819 static noinline int tree_mod_log_free_eb(struct extent_buffer *eb)
821 struct tree_mod_elem **tm_list = NULL;
826 if (btrfs_header_level(eb) == 0)
829 if (!tree_mod_need_log(eb->fs_info, NULL))
832 nritems = btrfs_header_nritems(eb);
833 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
837 for (i = 0; i < nritems; i++) {
838 tm_list[i] = alloc_tree_mod_elem(eb, i,
839 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
846 if (tree_mod_dont_log(eb->fs_info, eb))
849 ret = __tree_mod_log_free_eb(eb->fs_info, tm_list, nritems);
850 write_unlock(&eb->fs_info->tree_mod_log_lock);
858 for (i = 0; i < nritems; i++)
866 * check if the tree block can be shared by multiple trees
868 int btrfs_block_can_be_shared(struct btrfs_root *root,
869 struct extent_buffer *buf)
872 * Tree blocks not in reference counted trees and tree roots
873 * are never shared. If a block was allocated after the last
874 * snapshot and the block was not allocated by tree relocation,
875 * we know the block is not shared.
877 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
878 buf != root->node && buf != root->commit_root &&
879 (btrfs_header_generation(buf) <=
880 btrfs_root_last_snapshot(&root->root_item) ||
881 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
887 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
888 struct btrfs_root *root,
889 struct extent_buffer *buf,
890 struct extent_buffer *cow,
893 struct btrfs_fs_info *fs_info = root->fs_info;
901 * Backrefs update rules:
903 * Always use full backrefs for extent pointers in tree block
904 * allocated by tree relocation.
906 * If a shared tree block is no longer referenced by its owner
907 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
908 * use full backrefs for extent pointers in tree block.
910 * If a tree block is been relocating
911 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
912 * use full backrefs for extent pointers in tree block.
913 * The reason for this is some operations (such as drop tree)
914 * are only allowed for blocks use full backrefs.
917 if (btrfs_block_can_be_shared(root, buf)) {
918 ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
919 btrfs_header_level(buf), 1,
925 btrfs_handle_fs_error(fs_info, ret, NULL);
930 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
931 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
932 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
937 owner = btrfs_header_owner(buf);
938 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
939 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
942 if ((owner == root->root_key.objectid ||
943 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
944 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
945 ret = btrfs_inc_ref(trans, root, buf, 1);
949 if (root->root_key.objectid ==
950 BTRFS_TREE_RELOC_OBJECTID) {
951 ret = btrfs_dec_ref(trans, root, buf, 0);
954 ret = btrfs_inc_ref(trans, root, cow, 1);
958 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
961 if (root->root_key.objectid ==
962 BTRFS_TREE_RELOC_OBJECTID)
963 ret = btrfs_inc_ref(trans, root, cow, 1);
965 ret = btrfs_inc_ref(trans, root, cow, 0);
969 if (new_flags != 0) {
970 int level = btrfs_header_level(buf);
972 ret = btrfs_set_disk_extent_flags(trans,
975 new_flags, level, 0);
980 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
981 if (root->root_key.objectid ==
982 BTRFS_TREE_RELOC_OBJECTID)
983 ret = btrfs_inc_ref(trans, root, cow, 1);
985 ret = btrfs_inc_ref(trans, root, cow, 0);
988 ret = btrfs_dec_ref(trans, root, buf, 1);
992 btrfs_clean_tree_block(buf);
998 static struct extent_buffer *alloc_tree_block_no_bg_flush(
999 struct btrfs_trans_handle *trans,
1000 struct btrfs_root *root,
1002 const struct btrfs_disk_key *disk_key,
1007 struct btrfs_fs_info *fs_info = root->fs_info;
1008 struct extent_buffer *ret;
1011 * If we are COWing a node/leaf from the extent, chunk, device or free
1012 * space trees, make sure that we do not finish block group creation of
1013 * pending block groups. We do this to avoid a deadlock.
1014 * COWing can result in allocation of a new chunk, and flushing pending
1015 * block groups (btrfs_create_pending_block_groups()) can be triggered
1016 * when finishing allocation of a new chunk. Creation of a pending block
1017 * group modifies the extent, chunk, device and free space trees,
1018 * therefore we could deadlock with ourselves since we are holding a
1019 * lock on an extent buffer that btrfs_create_pending_block_groups() may
1021 * For similar reasons, we also need to delay flushing pending block
1022 * groups when splitting a leaf or node, from one of those trees, since
1023 * we are holding a write lock on it and its parent or when inserting a
1024 * new root node for one of those trees.
1026 if (root == fs_info->extent_root ||
1027 root == fs_info->chunk_root ||
1028 root == fs_info->dev_root ||
1029 root == fs_info->free_space_root)
1030 trans->can_flush_pending_bgs = false;
1032 ret = btrfs_alloc_tree_block(trans, root, parent_start,
1033 root->root_key.objectid, disk_key, level,
1035 trans->can_flush_pending_bgs = true;
1041 * does the dirty work in cow of a single block. The parent block (if
1042 * supplied) is updated to point to the new cow copy. The new buffer is marked
1043 * dirty and returned locked. If you modify the block it needs to be marked
1046 * search_start -- an allocation hint for the new block
1048 * empty_size -- a hint that you plan on doing more cow. This is the size in
1049 * bytes the allocator should try to find free next to the block it returns.
1050 * This is just a hint and may be ignored by the allocator.
1052 static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
1053 struct btrfs_root *root,
1054 struct extent_buffer *buf,
1055 struct extent_buffer *parent, int parent_slot,
1056 struct extent_buffer **cow_ret,
1057 u64 search_start, u64 empty_size)
1059 struct btrfs_fs_info *fs_info = root->fs_info;
1060 struct btrfs_disk_key disk_key;
1061 struct extent_buffer *cow;
1064 int unlock_orig = 0;
1065 u64 parent_start = 0;
1067 if (*cow_ret == buf)
1070 btrfs_assert_tree_locked(buf);
1072 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1073 trans->transid != fs_info->running_transaction->transid);
1074 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1075 trans->transid != root->last_trans);
1077 level = btrfs_header_level(buf);
1080 btrfs_item_key(buf, &disk_key, 0);
1082 btrfs_node_key(buf, &disk_key, 0);
1084 if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
1085 parent_start = parent->start;
1087 cow = alloc_tree_block_no_bg_flush(trans, root, parent_start, &disk_key,
1088 level, search_start, empty_size);
1090 return PTR_ERR(cow);
1092 /* cow is set to blocking by btrfs_init_new_buffer */
1094 copy_extent_buffer_full(cow, buf);
1095 btrfs_set_header_bytenr(cow, cow->start);
1096 btrfs_set_header_generation(cow, trans->transid);
1097 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1098 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1099 BTRFS_HEADER_FLAG_RELOC);
1100 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1101 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1103 btrfs_set_header_owner(cow, root->root_key.objectid);
1105 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
1107 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
1109 btrfs_tree_unlock(cow);
1110 free_extent_buffer(cow);
1111 btrfs_abort_transaction(trans, ret);
1115 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
1116 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
1118 btrfs_tree_unlock(cow);
1119 free_extent_buffer(cow);
1120 btrfs_abort_transaction(trans, ret);
1125 if (buf == root->node) {
1126 WARN_ON(parent && parent != buf);
1127 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1128 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1129 parent_start = buf->start;
1131 extent_buffer_get(cow);
1132 ret = tree_mod_log_insert_root(root->node, cow, 1);
1134 rcu_assign_pointer(root->node, cow);
1136 btrfs_free_tree_block(trans, root, buf, parent_start,
1138 free_extent_buffer(buf);
1139 add_root_to_dirty_list(root);
1141 WARN_ON(trans->transid != btrfs_header_generation(parent));
1142 tree_mod_log_insert_key(parent, parent_slot,
1143 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1144 btrfs_set_node_blockptr(parent, parent_slot,
1146 btrfs_set_node_ptr_generation(parent, parent_slot,
1148 btrfs_mark_buffer_dirty(parent);
1150 ret = tree_mod_log_free_eb(buf);
1152 btrfs_tree_unlock(cow);
1153 free_extent_buffer(cow);
1154 btrfs_abort_transaction(trans, ret);
1158 btrfs_free_tree_block(trans, root, buf, parent_start,
1162 btrfs_tree_unlock(buf);
1163 free_extent_buffer_stale(buf);
1164 btrfs_mark_buffer_dirty(cow);
1170 * returns the logical address of the oldest predecessor of the given root.
1171 * entries older than time_seq are ignored.
1173 static struct tree_mod_elem *__tree_mod_log_oldest_root(
1174 struct extent_buffer *eb_root, u64 time_seq)
1176 struct tree_mod_elem *tm;
1177 struct tree_mod_elem *found = NULL;
1178 u64 root_logical = eb_root->start;
1185 * the very last operation that's logged for a root is the
1186 * replacement operation (if it is replaced at all). this has
1187 * the logical address of the *new* root, making it the very
1188 * first operation that's logged for this root.
1191 tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical,
1196 * if there are no tree operation for the oldest root, we simply
1197 * return it. this should only happen if that (old) root is at
1204 * if there's an operation that's not a root replacement, we
1205 * found the oldest version of our root. normally, we'll find a
1206 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1208 if (tm->op != MOD_LOG_ROOT_REPLACE)
1212 root_logical = tm->old_root.logical;
1216 /* if there's no old root to return, return what we found instead */
1224 * tm is a pointer to the first operation to rewind within eb. then, all
1225 * previous operations will be rewound (until we reach something older than
1229 __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1230 u64 time_seq, struct tree_mod_elem *first_tm)
1233 struct rb_node *next;
1234 struct tree_mod_elem *tm = first_tm;
1235 unsigned long o_dst;
1236 unsigned long o_src;
1237 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1239 n = btrfs_header_nritems(eb);
1240 read_lock(&fs_info->tree_mod_log_lock);
1241 while (tm && tm->seq >= time_seq) {
1243 * all the operations are recorded with the operator used for
1244 * the modification. as we're going backwards, we do the
1245 * opposite of each operation here.
1248 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1249 BUG_ON(tm->slot < n);
1251 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
1252 case MOD_LOG_KEY_REMOVE:
1253 btrfs_set_node_key(eb, &tm->key, tm->slot);
1254 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1255 btrfs_set_node_ptr_generation(eb, tm->slot,
1259 case MOD_LOG_KEY_REPLACE:
1260 BUG_ON(tm->slot >= n);
1261 btrfs_set_node_key(eb, &tm->key, tm->slot);
1262 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1263 btrfs_set_node_ptr_generation(eb, tm->slot,
1266 case MOD_LOG_KEY_ADD:
1267 /* if a move operation is needed it's in the log */
1270 case MOD_LOG_MOVE_KEYS:
1271 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1272 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1273 memmove_extent_buffer(eb, o_dst, o_src,
1274 tm->move.nr_items * p_size);
1276 case MOD_LOG_ROOT_REPLACE:
1278 * this operation is special. for roots, this must be
1279 * handled explicitly before rewinding.
1280 * for non-roots, this operation may exist if the node
1281 * was a root: root A -> child B; then A gets empty and
1282 * B is promoted to the new root. in the mod log, we'll
1283 * have a root-replace operation for B, a tree block
1284 * that is no root. we simply ignore that operation.
1288 next = rb_next(&tm->node);
1291 tm = rb_entry(next, struct tree_mod_elem, node);
1292 if (tm->logical != first_tm->logical)
1295 read_unlock(&fs_info->tree_mod_log_lock);
1296 btrfs_set_header_nritems(eb, n);
1300 * Called with eb read locked. If the buffer cannot be rewound, the same buffer
1301 * is returned. If rewind operations happen, a fresh buffer is returned. The
1302 * returned buffer is always read-locked. If the returned buffer is not the
1303 * input buffer, the lock on the input buffer is released and the input buffer
1304 * is freed (its refcount is decremented).
1306 static struct extent_buffer *
1307 tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1308 struct extent_buffer *eb, u64 time_seq)
1310 struct extent_buffer *eb_rewin;
1311 struct tree_mod_elem *tm;
1316 if (btrfs_header_level(eb) == 0)
1319 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1323 btrfs_set_path_blocking(path);
1324 btrfs_set_lock_blocking_read(eb);
1326 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1327 BUG_ON(tm->slot != 0);
1328 eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
1330 btrfs_tree_read_unlock_blocking(eb);
1331 free_extent_buffer(eb);
1334 btrfs_set_header_bytenr(eb_rewin, eb->start);
1335 btrfs_set_header_backref_rev(eb_rewin,
1336 btrfs_header_backref_rev(eb));
1337 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
1338 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
1340 eb_rewin = btrfs_clone_extent_buffer(eb);
1342 btrfs_tree_read_unlock_blocking(eb);
1343 free_extent_buffer(eb);
1348 btrfs_tree_read_unlock_blocking(eb);
1349 free_extent_buffer(eb);
1351 btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb_rewin),
1352 eb_rewin, btrfs_header_level(eb_rewin));
1353 btrfs_tree_read_lock(eb_rewin);
1354 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
1355 WARN_ON(btrfs_header_nritems(eb_rewin) >
1356 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
1362 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1363 * value. If there are no changes, the current root->root_node is returned. If
1364 * anything changed in between, there's a fresh buffer allocated on which the
1365 * rewind operations are done. In any case, the returned buffer is read locked.
1366 * Returns NULL on error (with no locks held).
1368 static inline struct extent_buffer *
1369 get_old_root(struct btrfs_root *root, u64 time_seq)
1371 struct btrfs_fs_info *fs_info = root->fs_info;
1372 struct tree_mod_elem *tm;
1373 struct extent_buffer *eb = NULL;
1374 struct extent_buffer *eb_root;
1375 u64 eb_root_owner = 0;
1376 struct extent_buffer *old;
1377 struct tree_mod_root *old_root = NULL;
1378 u64 old_generation = 0;
1382 eb_root = btrfs_read_lock_root_node(root);
1383 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
1387 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1388 old_root = &tm->old_root;
1389 old_generation = tm->generation;
1390 logical = old_root->logical;
1391 level = old_root->level;
1393 logical = eb_root->start;
1394 level = btrfs_header_level(eb_root);
1397 tm = tree_mod_log_search(fs_info, logical, time_seq);
1398 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1399 btrfs_tree_read_unlock(eb_root);
1400 free_extent_buffer(eb_root);
1401 old = read_tree_block(fs_info, logical, 0, level, NULL);
1402 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
1404 free_extent_buffer(old);
1406 "failed to read tree block %llu from get_old_root",
1409 struct tree_mod_elem *tm2;
1411 btrfs_tree_read_lock(old);
1412 eb = btrfs_clone_extent_buffer(old);
1414 * After the lookup for the most recent tree mod operation
1415 * above and before we locked and cloned the extent buffer
1416 * 'old', a new tree mod log operation may have been added.
1417 * So lookup for a more recent one to make sure the number
1418 * of mod log operations we replay is consistent with the
1419 * number of items we have in the cloned extent buffer,
1420 * otherwise we can hit a BUG_ON when rewinding the extent
1423 tm2 = tree_mod_log_search(fs_info, logical, time_seq);
1424 btrfs_tree_read_unlock(old);
1425 free_extent_buffer(old);
1427 ASSERT(tm2 == tm || tm2->seq > tm->seq);
1428 if (!tm2 || tm2->seq < tm->seq) {
1429 free_extent_buffer(eb);
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_read(eb_root);
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(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 bit */
1492 smp_mb__before_atomic();
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 copying 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 (test_bit(BTRFS_ROOT_DELETING, &root->state))
1530 "COW'ing blocks on a fs root that's being dropped");
1532 if (trans->transaction != fs_info->running_transaction)
1533 WARN(1, KERN_CRIT "trans %llu running %llu\n",
1535 fs_info->running_transaction->transid);
1537 if (trans->transid != fs_info->generation)
1538 WARN(1, KERN_CRIT "trans %llu running %llu\n",
1539 trans->transid, fs_info->generation);
1541 if (!should_cow_block(trans, root, buf)) {
1542 trans->dirty = true;
1547 search_start = buf->start & ~((u64)SZ_1G - 1);
1550 btrfs_set_lock_blocking_write(parent);
1551 btrfs_set_lock_blocking_write(buf);
1554 * Before CoWing this block for later modification, check if it's
1555 * the subtree root and do the delayed subtree trace if needed.
1557 * Also We don't care about the error, as it's handled internally.
1559 btrfs_qgroup_trace_subtree_after_cow(trans, root, buf);
1560 ret = __btrfs_cow_block(trans, root, buf, parent,
1561 parent_slot, cow_ret, search_start, 0);
1563 trace_btrfs_cow_block(root, buf, *cow_ret);
1569 * helper function for defrag to decide if two blocks pointed to by a
1570 * node are actually close by
1572 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
1574 if (blocknr < other && other - (blocknr + blocksize) < 32768)
1576 if (blocknr > other && blocknr - (other + blocksize) < 32768)
1582 * compare two keys in a memcmp fashion
1584 static int comp_keys(const struct btrfs_disk_key *disk,
1585 const struct btrfs_key *k2)
1587 struct btrfs_key k1;
1589 btrfs_disk_key_to_cpu(&k1, disk);
1591 return btrfs_comp_cpu_keys(&k1, k2);
1595 * same as comp_keys only with two btrfs_key's
1597 int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
1599 if (k1->objectid > k2->objectid)
1601 if (k1->objectid < k2->objectid)
1603 if (k1->type > k2->type)
1605 if (k1->type < k2->type)
1607 if (k1->offset > k2->offset)
1609 if (k1->offset < k2->offset)
1615 * this is used by the defrag code to go through all the
1616 * leaves pointed to by a node and reallocate them so that
1617 * disk order is close to key order
1619 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
1620 struct btrfs_root *root, struct extent_buffer *parent,
1621 int start_slot, u64 *last_ret,
1622 struct btrfs_key *progress)
1624 struct btrfs_fs_info *fs_info = root->fs_info;
1625 struct extent_buffer *cur;
1628 u64 search_start = *last_ret;
1638 int progress_passed = 0;
1639 struct btrfs_disk_key disk_key;
1641 parent_level = btrfs_header_level(parent);
1643 WARN_ON(trans->transaction != fs_info->running_transaction);
1644 WARN_ON(trans->transid != fs_info->generation);
1646 parent_nritems = btrfs_header_nritems(parent);
1647 blocksize = fs_info->nodesize;
1648 end_slot = parent_nritems - 1;
1650 if (parent_nritems <= 1)
1653 btrfs_set_lock_blocking_write(parent);
1655 for (i = start_slot; i <= end_slot; i++) {
1656 struct btrfs_key first_key;
1659 btrfs_node_key(parent, &disk_key, i);
1660 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1663 progress_passed = 1;
1664 blocknr = btrfs_node_blockptr(parent, i);
1665 gen = btrfs_node_ptr_generation(parent, i);
1666 btrfs_node_key_to_cpu(parent, &first_key, i);
1667 if (last_block == 0)
1668 last_block = blocknr;
1671 other = btrfs_node_blockptr(parent, i - 1);
1672 close = close_blocks(blocknr, other, blocksize);
1674 if (!close && i < end_slot) {
1675 other = btrfs_node_blockptr(parent, i + 1);
1676 close = close_blocks(blocknr, other, blocksize);
1679 last_block = blocknr;
1683 cur = find_extent_buffer(fs_info, blocknr);
1685 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
1688 if (!cur || !uptodate) {
1690 cur = read_tree_block(fs_info, blocknr, gen,
1694 return PTR_ERR(cur);
1695 } else if (!extent_buffer_uptodate(cur)) {
1696 free_extent_buffer(cur);
1699 } else if (!uptodate) {
1700 err = btrfs_read_buffer(cur, gen,
1701 parent_level - 1,&first_key);
1703 free_extent_buffer(cur);
1708 if (search_start == 0)
1709 search_start = last_block;
1711 btrfs_tree_lock(cur);
1712 btrfs_set_lock_blocking_write(cur);
1713 err = __btrfs_cow_block(trans, root, cur, parent, i,
1716 (end_slot - i) * blocksize));
1718 btrfs_tree_unlock(cur);
1719 free_extent_buffer(cur);
1722 search_start = cur->start;
1723 last_block = cur->start;
1724 *last_ret = search_start;
1725 btrfs_tree_unlock(cur);
1726 free_extent_buffer(cur);
1732 * search for key in the extent_buffer. The items start at offset p,
1733 * and they are item_size apart. There are 'max' items in p.
1735 * the slot in the array is returned via slot, and it points to
1736 * the place where you would insert key if it is not found in
1739 * slot may point to max if the key is bigger than all of the keys
1741 static noinline int generic_bin_search(struct extent_buffer *eb,
1742 unsigned long p, int item_size,
1743 const struct btrfs_key *key,
1750 struct btrfs_disk_key *tmp = NULL;
1751 struct btrfs_disk_key unaligned;
1752 unsigned long offset;
1754 unsigned long map_start = 0;
1755 unsigned long map_len = 0;
1759 btrfs_err(eb->fs_info,
1760 "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
1761 __func__, low, high, eb->start,
1762 btrfs_header_owner(eb), btrfs_header_level(eb));
1766 while (low < high) {
1767 mid = (low + high) / 2;
1768 offset = p + mid * item_size;
1770 if (!kaddr || offset < map_start ||
1771 (offset + sizeof(struct btrfs_disk_key)) >
1772 map_start + map_len) {
1774 err = map_private_extent_buffer(eb, offset,
1775 sizeof(struct btrfs_disk_key),
1776 &kaddr, &map_start, &map_len);
1779 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1781 } else if (err == 1) {
1782 read_extent_buffer(eb, &unaligned,
1783 offset, sizeof(unaligned));
1790 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1793 ret = comp_keys(tmp, key);
1809 * simple bin_search frontend that does the right thing for
1812 int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
1813 int level, int *slot)
1816 return generic_bin_search(eb,
1817 offsetof(struct btrfs_leaf, items),
1818 sizeof(struct btrfs_item),
1819 key, btrfs_header_nritems(eb),
1822 return generic_bin_search(eb,
1823 offsetof(struct btrfs_node, ptrs),
1824 sizeof(struct btrfs_key_ptr),
1825 key, btrfs_header_nritems(eb),
1829 static void root_add_used(struct btrfs_root *root, u32 size)
1831 spin_lock(&root->accounting_lock);
1832 btrfs_set_root_used(&root->root_item,
1833 btrfs_root_used(&root->root_item) + size);
1834 spin_unlock(&root->accounting_lock);
1837 static void root_sub_used(struct btrfs_root *root, u32 size)
1839 spin_lock(&root->accounting_lock);
1840 btrfs_set_root_used(&root->root_item,
1841 btrfs_root_used(&root->root_item) - size);
1842 spin_unlock(&root->accounting_lock);
1845 /* given a node and slot number, this reads the blocks it points to. The
1846 * extent buffer is returned with a reference taken (but unlocked).
1848 struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
1851 int level = btrfs_header_level(parent);
1852 struct extent_buffer *eb;
1853 struct btrfs_key first_key;
1855 if (slot < 0 || slot >= btrfs_header_nritems(parent))
1856 return ERR_PTR(-ENOENT);
1860 btrfs_node_key_to_cpu(parent, &first_key, slot);
1861 eb = read_tree_block(parent->fs_info, btrfs_node_blockptr(parent, slot),
1862 btrfs_node_ptr_generation(parent, slot),
1863 level - 1, &first_key);
1864 if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
1865 free_extent_buffer(eb);
1873 * node level balancing, used to make sure nodes are in proper order for
1874 * item deletion. We balance from the top down, so we have to make sure
1875 * that a deletion won't leave an node completely empty later on.
1877 static noinline int balance_level(struct btrfs_trans_handle *trans,
1878 struct btrfs_root *root,
1879 struct btrfs_path *path, int level)
1881 struct btrfs_fs_info *fs_info = root->fs_info;
1882 struct extent_buffer *right = NULL;
1883 struct extent_buffer *mid;
1884 struct extent_buffer *left = NULL;
1885 struct extent_buffer *parent = NULL;
1889 int orig_slot = path->slots[level];
1894 mid = path->nodes[level];
1896 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1897 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
1898 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1900 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1902 if (level < BTRFS_MAX_LEVEL - 1) {
1903 parent = path->nodes[level + 1];
1904 pslot = path->slots[level + 1];
1908 * deal with the case where there is only one pointer in the root
1909 * by promoting the node below to a root
1912 struct extent_buffer *child;
1914 if (btrfs_header_nritems(mid) != 1)
1917 /* promote the child to a root */
1918 child = btrfs_read_node_slot(mid, 0);
1919 if (IS_ERR(child)) {
1920 ret = PTR_ERR(child);
1921 btrfs_handle_fs_error(fs_info, ret, NULL);
1925 btrfs_tree_lock(child);
1926 btrfs_set_lock_blocking_write(child);
1927 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
1929 btrfs_tree_unlock(child);
1930 free_extent_buffer(child);
1934 ret = tree_mod_log_insert_root(root->node, child, 1);
1936 rcu_assign_pointer(root->node, child);
1938 add_root_to_dirty_list(root);
1939 btrfs_tree_unlock(child);
1941 path->locks[level] = 0;
1942 path->nodes[level] = NULL;
1943 btrfs_clean_tree_block(mid);
1944 btrfs_tree_unlock(mid);
1945 /* once for the path */
1946 free_extent_buffer(mid);
1948 root_sub_used(root, mid->len);
1949 btrfs_free_tree_block(trans, root, mid, 0, 1);
1950 /* once for the root ptr */
1951 free_extent_buffer_stale(mid);
1954 if (btrfs_header_nritems(mid) >
1955 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
1958 left = btrfs_read_node_slot(parent, pslot - 1);
1963 btrfs_tree_lock(left);
1964 btrfs_set_lock_blocking_write(left);
1965 wret = btrfs_cow_block(trans, root, left,
1966 parent, pslot - 1, &left);
1973 right = btrfs_read_node_slot(parent, pslot + 1);
1978 btrfs_tree_lock(right);
1979 btrfs_set_lock_blocking_write(right);
1980 wret = btrfs_cow_block(trans, root, right,
1981 parent, pslot + 1, &right);
1988 /* first, try to make some room in the middle buffer */
1990 orig_slot += btrfs_header_nritems(left);
1991 wret = push_node_left(trans, left, mid, 1);
1997 * then try to empty the right most buffer into the middle
2000 wret = push_node_left(trans, mid, right, 1);
2001 if (wret < 0 && wret != -ENOSPC)
2003 if (btrfs_header_nritems(right) == 0) {
2004 btrfs_clean_tree_block(right);
2005 btrfs_tree_unlock(right);
2006 del_ptr(root, path, level + 1, pslot + 1);
2007 root_sub_used(root, right->len);
2008 btrfs_free_tree_block(trans, root, right, 0, 1);
2009 free_extent_buffer_stale(right);
2012 struct btrfs_disk_key right_key;
2013 btrfs_node_key(right, &right_key, 0);
2014 ret = tree_mod_log_insert_key(parent, pslot + 1,
2015 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2017 btrfs_set_node_key(parent, &right_key, pslot + 1);
2018 btrfs_mark_buffer_dirty(parent);
2021 if (btrfs_header_nritems(mid) == 1) {
2023 * we're not allowed to leave a node with one item in the
2024 * tree during a delete. A deletion from lower in the tree
2025 * could try to delete the only pointer in this node.
2026 * So, pull some keys from the left.
2027 * There has to be a left pointer at this point because
2028 * otherwise we would have pulled some pointers from the
2033 btrfs_handle_fs_error(fs_info, ret, NULL);
2036 wret = balance_node_right(trans, mid, left);
2042 wret = push_node_left(trans, left, mid, 1);
2048 if (btrfs_header_nritems(mid) == 0) {
2049 btrfs_clean_tree_block(mid);
2050 btrfs_tree_unlock(mid);
2051 del_ptr(root, path, level + 1, pslot);
2052 root_sub_used(root, mid->len);
2053 btrfs_free_tree_block(trans, root, mid, 0, 1);
2054 free_extent_buffer_stale(mid);
2057 /* update the parent key to reflect our changes */
2058 struct btrfs_disk_key mid_key;
2059 btrfs_node_key(mid, &mid_key, 0);
2060 ret = tree_mod_log_insert_key(parent, pslot,
2061 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2063 btrfs_set_node_key(parent, &mid_key, pslot);
2064 btrfs_mark_buffer_dirty(parent);
2067 /* update the path */
2069 if (btrfs_header_nritems(left) > orig_slot) {
2070 extent_buffer_get(left);
2071 /* left was locked after cow */
2072 path->nodes[level] = left;
2073 path->slots[level + 1] -= 1;
2074 path->slots[level] = orig_slot;
2076 btrfs_tree_unlock(mid);
2077 free_extent_buffer(mid);
2080 orig_slot -= btrfs_header_nritems(left);
2081 path->slots[level] = orig_slot;
2084 /* double check we haven't messed things up */
2086 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
2090 btrfs_tree_unlock(right);
2091 free_extent_buffer(right);
2094 if (path->nodes[level] != left)
2095 btrfs_tree_unlock(left);
2096 free_extent_buffer(left);
2101 /* Node balancing for insertion. Here we only split or push nodes around
2102 * when they are completely full. This is also done top down, so we
2103 * have to be pessimistic.
2105 static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
2106 struct btrfs_root *root,
2107 struct btrfs_path *path, int level)
2109 struct btrfs_fs_info *fs_info = root->fs_info;
2110 struct extent_buffer *right = NULL;
2111 struct extent_buffer *mid;
2112 struct extent_buffer *left = NULL;
2113 struct extent_buffer *parent = NULL;
2117 int orig_slot = path->slots[level];
2122 mid = path->nodes[level];
2123 WARN_ON(btrfs_header_generation(mid) != trans->transid);
2125 if (level < BTRFS_MAX_LEVEL - 1) {
2126 parent = path->nodes[level + 1];
2127 pslot = path->slots[level + 1];
2133 left = btrfs_read_node_slot(parent, pslot - 1);
2137 /* first, try to make some room in the middle buffer */
2141 btrfs_tree_lock(left);
2142 btrfs_set_lock_blocking_write(left);
2144 left_nr = btrfs_header_nritems(left);
2145 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
2148 ret = btrfs_cow_block(trans, root, left, parent,
2153 wret = push_node_left(trans, left, mid, 0);
2159 struct btrfs_disk_key disk_key;
2160 orig_slot += left_nr;
2161 btrfs_node_key(mid, &disk_key, 0);
2162 ret = tree_mod_log_insert_key(parent, pslot,
2163 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2165 btrfs_set_node_key(parent, &disk_key, pslot);
2166 btrfs_mark_buffer_dirty(parent);
2167 if (btrfs_header_nritems(left) > orig_slot) {
2168 path->nodes[level] = left;
2169 path->slots[level + 1] -= 1;
2170 path->slots[level] = orig_slot;
2171 btrfs_tree_unlock(mid);
2172 free_extent_buffer(mid);
2175 btrfs_header_nritems(left);
2176 path->slots[level] = orig_slot;
2177 btrfs_tree_unlock(left);
2178 free_extent_buffer(left);
2182 btrfs_tree_unlock(left);
2183 free_extent_buffer(left);
2185 right = btrfs_read_node_slot(parent, pslot + 1);
2190 * then try to empty the right most buffer into the middle
2195 btrfs_tree_lock(right);
2196 btrfs_set_lock_blocking_write(right);
2198 right_nr = btrfs_header_nritems(right);
2199 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
2202 ret = btrfs_cow_block(trans, root, right,
2208 wret = balance_node_right(trans, right, mid);
2214 struct btrfs_disk_key disk_key;
2216 btrfs_node_key(right, &disk_key, 0);
2217 ret = tree_mod_log_insert_key(parent, pslot + 1,
2218 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2220 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2221 btrfs_mark_buffer_dirty(parent);
2223 if (btrfs_header_nritems(mid) <= orig_slot) {
2224 path->nodes[level] = right;
2225 path->slots[level + 1] += 1;
2226 path->slots[level] = orig_slot -
2227 btrfs_header_nritems(mid);
2228 btrfs_tree_unlock(mid);
2229 free_extent_buffer(mid);
2231 btrfs_tree_unlock(right);
2232 free_extent_buffer(right);
2236 btrfs_tree_unlock(right);
2237 free_extent_buffer(right);
2243 * readahead one full node of leaves, finding things that are close
2244 * to the block in 'slot', and triggering ra on them.
2246 static void reada_for_search(struct btrfs_fs_info *fs_info,
2247 struct btrfs_path *path,
2248 int level, int slot, u64 objectid)
2250 struct extent_buffer *node;
2251 struct btrfs_disk_key disk_key;
2256 struct extent_buffer *eb;
2264 if (!path->nodes[level])
2267 node = path->nodes[level];
2269 search = btrfs_node_blockptr(node, slot);
2270 blocksize = fs_info->nodesize;
2271 eb = find_extent_buffer(fs_info, search);
2273 free_extent_buffer(eb);
2279 nritems = btrfs_header_nritems(node);
2283 if (path->reada == READA_BACK) {
2287 } else if (path->reada == READA_FORWARD) {
2292 if (path->reada == READA_BACK && objectid) {
2293 btrfs_node_key(node, &disk_key, nr);
2294 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2297 search = btrfs_node_blockptr(node, nr);
2298 if ((search <= target && target - search <= 65536) ||
2299 (search > target && search - target <= 65536)) {
2300 readahead_tree_block(fs_info, search);
2304 if ((nread > 65536 || nscan > 32))
2309 static noinline void reada_for_balance(struct btrfs_fs_info *fs_info,
2310 struct btrfs_path *path, int level)
2314 struct extent_buffer *parent;
2315 struct extent_buffer *eb;
2320 parent = path->nodes[level + 1];
2324 nritems = btrfs_header_nritems(parent);
2325 slot = path->slots[level + 1];
2328 block1 = btrfs_node_blockptr(parent, slot - 1);
2329 gen = btrfs_node_ptr_generation(parent, slot - 1);
2330 eb = find_extent_buffer(fs_info, block1);
2332 * if we get -eagain from btrfs_buffer_uptodate, we
2333 * don't want to return eagain here. That will loop
2336 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
2338 free_extent_buffer(eb);
2340 if (slot + 1 < nritems) {
2341 block2 = btrfs_node_blockptr(parent, slot + 1);
2342 gen = btrfs_node_ptr_generation(parent, slot + 1);
2343 eb = find_extent_buffer(fs_info, block2);
2344 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
2346 free_extent_buffer(eb);
2350 readahead_tree_block(fs_info, block1);
2352 readahead_tree_block(fs_info, block2);
2357 * when we walk down the tree, it is usually safe to unlock the higher layers
2358 * in the tree. The exceptions are when our path goes through slot 0, because
2359 * operations on the tree might require changing key pointers higher up in the
2362 * callers might also have set path->keep_locks, which tells this code to keep
2363 * the lock if the path points to the last slot in the block. This is part of
2364 * walking through the tree, and selecting the next slot in the higher block.
2366 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2367 * if lowest_unlock is 1, level 0 won't be unlocked
2369 static noinline void unlock_up(struct btrfs_path *path, int level,
2370 int lowest_unlock, int min_write_lock_level,
2371 int *write_lock_level)
2374 int skip_level = level;
2376 struct extent_buffer *t;
2378 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2379 if (!path->nodes[i])
2381 if (!path->locks[i])
2383 if (!no_skips && path->slots[i] == 0) {
2387 if (!no_skips && path->keep_locks) {
2390 nritems = btrfs_header_nritems(t);
2391 if (nritems < 1 || path->slots[i] >= nritems - 1) {
2396 if (skip_level < i && i >= lowest_unlock)
2400 if (i >= lowest_unlock && i > skip_level) {
2401 btrfs_tree_unlock_rw(t, path->locks[i]);
2403 if (write_lock_level &&
2404 i > min_write_lock_level &&
2405 i <= *write_lock_level) {
2406 *write_lock_level = i - 1;
2413 * This releases any locks held in the path starting at level and
2414 * going all the way up to the root.
2416 * btrfs_search_slot will keep the lock held on higher nodes in a few
2417 * corner cases, such as COW of the block at slot zero in the node. This
2418 * ignores those rules, and it should only be called when there are no
2419 * more updates to be done higher up in the tree.
2421 noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2425 if (path->keep_locks)
2428 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2429 if (!path->nodes[i])
2431 if (!path->locks[i])
2433 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
2439 * helper function for btrfs_search_slot. The goal is to find a block
2440 * in cache without setting the path to blocking. If we find the block
2441 * we return zero and the path is unchanged.
2443 * If we can't find the block, we set the path blocking and do some
2444 * reada. -EAGAIN is returned and the search must be repeated.
2447 read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
2448 struct extent_buffer **eb_ret, int level, int slot,
2449 const struct btrfs_key *key)
2451 struct btrfs_fs_info *fs_info = root->fs_info;
2454 struct extent_buffer *b = *eb_ret;
2455 struct extent_buffer *tmp;
2456 struct btrfs_key first_key;
2460 blocknr = btrfs_node_blockptr(b, slot);
2461 gen = btrfs_node_ptr_generation(b, slot);
2462 parent_level = btrfs_header_level(b);
2463 btrfs_node_key_to_cpu(b, &first_key, slot);
2465 tmp = find_extent_buffer(fs_info, blocknr);
2467 /* first we do an atomic uptodate check */
2468 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2470 * Do extra check for first_key, eb can be stale due to
2471 * being cached, read from scrub, or have multiple
2472 * parents (shared tree blocks).
2474 if (btrfs_verify_level_key(tmp,
2475 parent_level - 1, &first_key, gen)) {
2476 free_extent_buffer(tmp);
2483 /* the pages were up to date, but we failed
2484 * the generation number check. Do a full
2485 * read for the generation number that is correct.
2486 * We must do this without dropping locks so
2487 * we can trust our generation number
2489 btrfs_set_path_blocking(p);
2491 /* now we're allowed to do a blocking uptodate check */
2492 ret = btrfs_read_buffer(tmp, gen, parent_level - 1, &first_key);
2497 free_extent_buffer(tmp);
2498 btrfs_release_path(p);
2503 * reduce lock contention at high levels
2504 * of the btree by dropping locks before
2505 * we read. Don't release the lock on the current
2506 * level because we need to walk this node to figure
2507 * out which blocks to read.
2509 btrfs_unlock_up_safe(p, level + 1);
2510 btrfs_set_path_blocking(p);
2512 if (p->reada != READA_NONE)
2513 reada_for_search(fs_info, p, level, slot, key->objectid);
2516 tmp = read_tree_block(fs_info, blocknr, gen, parent_level - 1,
2520 * If the read above didn't mark this buffer up to date,
2521 * it will never end up being up to date. Set ret to EIO now
2522 * and give up so that our caller doesn't loop forever
2525 if (!extent_buffer_uptodate(tmp))
2527 free_extent_buffer(tmp);
2532 btrfs_release_path(p);
2537 * helper function for btrfs_search_slot. This does all of the checks
2538 * for node-level blocks and does any balancing required based on
2541 * If no extra work was required, zero is returned. If we had to
2542 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2546 setup_nodes_for_search(struct btrfs_trans_handle *trans,
2547 struct btrfs_root *root, struct btrfs_path *p,
2548 struct extent_buffer *b, int level, int ins_len,
2549 int *write_lock_level)
2551 struct btrfs_fs_info *fs_info = root->fs_info;
2554 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2555 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
2558 if (*write_lock_level < level + 1) {
2559 *write_lock_level = level + 1;
2560 btrfs_release_path(p);
2564 btrfs_set_path_blocking(p);
2565 reada_for_balance(fs_info, p, level);
2566 sret = split_node(trans, root, p, level);
2573 b = p->nodes[level];
2574 } else if (ins_len < 0 && btrfs_header_nritems(b) <
2575 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
2578 if (*write_lock_level < level + 1) {
2579 *write_lock_level = level + 1;
2580 btrfs_release_path(p);
2584 btrfs_set_path_blocking(p);
2585 reada_for_balance(fs_info, p, level);
2586 sret = balance_level(trans, root, p, level);
2592 b = p->nodes[level];
2594 btrfs_release_path(p);
2597 BUG_ON(btrfs_header_nritems(b) == 1);
2607 static int key_search(struct extent_buffer *b, const struct btrfs_key *key,
2608 int level, int *prev_cmp, int *slot)
2610 if (*prev_cmp != 0) {
2611 *prev_cmp = btrfs_bin_search(b, key, level, slot);
2620 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
2621 u64 iobjectid, u64 ioff, u8 key_type,
2622 struct btrfs_key *found_key)
2625 struct btrfs_key key;
2626 struct extent_buffer *eb;
2631 key.type = key_type;
2632 key.objectid = iobjectid;
2635 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
2639 eb = path->nodes[0];
2640 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2641 ret = btrfs_next_leaf(fs_root, path);
2644 eb = path->nodes[0];
2647 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2648 if (found_key->type != key.type ||
2649 found_key->objectid != key.objectid)
2655 static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,
2656 struct btrfs_path *p,
2657 int write_lock_level)
2659 struct btrfs_fs_info *fs_info = root->fs_info;
2660 struct extent_buffer *b;
2664 if (p->search_commit_root) {
2666 * The commit roots are read only so we always do read locks,
2667 * and we always must hold the commit_root_sem when doing
2668 * searches on them, the only exception is send where we don't
2669 * want to block transaction commits for a long time, so
2670 * we need to clone the commit root in order to avoid races
2671 * with transaction commits that create a snapshot of one of
2672 * the roots used by a send operation.
2674 if (p->need_commit_sem) {
2675 down_read(&fs_info->commit_root_sem);
2676 b = btrfs_clone_extent_buffer(root->commit_root);
2677 up_read(&fs_info->commit_root_sem);
2679 return ERR_PTR(-ENOMEM);
2682 b = root->commit_root;
2683 extent_buffer_get(b);
2685 level = btrfs_header_level(b);
2687 * Ensure that all callers have set skip_locking when
2688 * p->search_commit_root = 1.
2690 ASSERT(p->skip_locking == 1);
2695 if (p->skip_locking) {
2696 b = btrfs_root_node(root);
2697 level = btrfs_header_level(b);
2701 /* We try very hard to do read locks on the root */
2702 root_lock = BTRFS_READ_LOCK;
2705 * If the level is set to maximum, we can skip trying to get the read
2708 if (write_lock_level < BTRFS_MAX_LEVEL) {
2710 * We don't know the level of the root node until we actually
2711 * have it read locked
2713 b = btrfs_read_lock_root_node(root);
2714 level = btrfs_header_level(b);
2715 if (level > write_lock_level)
2718 /* Whoops, must trade for write lock */
2719 btrfs_tree_read_unlock(b);
2720 free_extent_buffer(b);
2723 b = btrfs_lock_root_node(root);
2724 root_lock = BTRFS_WRITE_LOCK;
2726 /* The level might have changed, check again */
2727 level = btrfs_header_level(b);
2731 * The root may have failed to write out at some point, and thus is no
2732 * longer valid, return an error in this case.
2734 if (!extent_buffer_uptodate(b)) {
2736 btrfs_tree_unlock_rw(b, root_lock);
2737 free_extent_buffer(b);
2738 return ERR_PTR(-EIO);
2741 p->nodes[level] = b;
2742 if (!p->skip_locking)
2743 p->locks[level] = root_lock;
2745 * Callers are responsible for dropping b's references.
2752 * btrfs_search_slot - look for a key in a tree and perform necessary
2753 * modifications to preserve tree invariants.
2755 * @trans: Handle of transaction, used when modifying the tree
2756 * @p: Holds all btree nodes along the search path
2757 * @root: The root node of the tree
2758 * @key: The key we are looking for
2759 * @ins_len: Indicates purpose of search, for inserts it is 1, for
2760 * deletions it's -1. 0 for plain searches
2761 * @cow: boolean should CoW operations be performed. Must always be 1
2762 * when modifying the tree.
2764 * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
2765 * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
2767 * If @key is found, 0 is returned and you can find the item in the leaf level
2768 * of the path (level 0)
2770 * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
2771 * points to the slot where it should be inserted
2773 * If an error is encountered while searching the tree a negative error number
2776 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2777 const struct btrfs_key *key, struct btrfs_path *p,
2778 int ins_len, int cow)
2780 struct extent_buffer *b;
2785 int lowest_unlock = 1;
2786 /* everything at write_lock_level or lower must be write locked */
2787 int write_lock_level = 0;
2788 u8 lowest_level = 0;
2789 int min_write_lock_level;
2792 lowest_level = p->lowest_level;
2793 WARN_ON(lowest_level && ins_len > 0);
2794 WARN_ON(p->nodes[0] != NULL);
2795 BUG_ON(!cow && ins_len);
2800 /* when we are removing items, we might have to go up to level
2801 * two as we update tree pointers Make sure we keep write
2802 * for those levels as well
2804 write_lock_level = 2;
2805 } else if (ins_len > 0) {
2807 * for inserting items, make sure we have a write lock on
2808 * level 1 so we can update keys
2810 write_lock_level = 1;
2814 write_lock_level = -1;
2816 if (cow && (p->keep_locks || p->lowest_level))
2817 write_lock_level = BTRFS_MAX_LEVEL;
2819 min_write_lock_level = write_lock_level;
2823 b = btrfs_search_slot_get_root(root, p, write_lock_level);
2830 level = btrfs_header_level(b);
2833 * setup the path here so we can release it under lock
2834 * contention with the cow code
2837 bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
2840 * if we don't really need to cow this block
2841 * then we don't want to set the path blocking,
2842 * so we test it here
2844 if (!should_cow_block(trans, root, b)) {
2845 trans->dirty = true;
2850 * must have write locks on this node and the
2853 if (level > write_lock_level ||
2854 (level + 1 > write_lock_level &&
2855 level + 1 < BTRFS_MAX_LEVEL &&
2856 p->nodes[level + 1])) {
2857 write_lock_level = level + 1;
2858 btrfs_release_path(p);
2862 btrfs_set_path_blocking(p);
2864 err = btrfs_cow_block(trans, root, b, NULL, 0,
2867 err = btrfs_cow_block(trans, root, b,
2868 p->nodes[level + 1],
2869 p->slots[level + 1], &b);
2876 p->nodes[level] = b;
2878 * Leave path with blocking locks to avoid massive
2879 * lock context switch, this is made on purpose.
2883 * we have a lock on b and as long as we aren't changing
2884 * the tree, there is no way to for the items in b to change.
2885 * It is safe to drop the lock on our parent before we
2886 * go through the expensive btree search on b.
2888 * If we're inserting or deleting (ins_len != 0), then we might
2889 * be changing slot zero, which may require changing the parent.
2890 * So, we can't drop the lock until after we know which slot
2891 * we're operating on.
2893 if (!ins_len && !p->keep_locks) {
2896 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2897 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2902 ret = key_search(b, key, level, &prev_cmp, &slot);
2908 if (ret && slot > 0) {
2912 p->slots[level] = slot;
2913 err = setup_nodes_for_search(trans, root, p, b, level,
2914 ins_len, &write_lock_level);
2921 b = p->nodes[level];
2922 slot = p->slots[level];
2925 * slot 0 is special, if we change the key
2926 * we have to update the parent pointer
2927 * which means we must have a write lock
2930 if (slot == 0 && ins_len &&
2931 write_lock_level < level + 1) {
2932 write_lock_level = level + 1;
2933 btrfs_release_path(p);
2937 unlock_up(p, level, lowest_unlock,
2938 min_write_lock_level, &write_lock_level);
2940 if (level == lowest_level) {
2946 err = read_block_for_search(root, p, &b, level,
2955 if (!p->skip_locking) {
2956 level = btrfs_header_level(b);
2957 if (level <= write_lock_level) {
2958 if (!btrfs_try_tree_write_lock(b)) {
2959 btrfs_set_path_blocking(p);
2962 p->locks[level] = BTRFS_WRITE_LOCK;
2964 if (!btrfs_tree_read_lock_atomic(b)) {
2965 btrfs_set_path_blocking(p);
2966 btrfs_tree_read_lock(b);
2968 p->locks[level] = BTRFS_READ_LOCK;
2970 p->nodes[level] = b;
2973 p->slots[level] = slot;
2975 btrfs_leaf_free_space(b) < ins_len) {
2976 if (write_lock_level < 1) {
2977 write_lock_level = 1;
2978 btrfs_release_path(p);
2982 btrfs_set_path_blocking(p);
2983 err = split_leaf(trans, root, key,
2984 p, ins_len, ret == 0);
2992 if (!p->search_for_split)
2993 unlock_up(p, level, lowest_unlock,
2994 min_write_lock_level, NULL);
3001 * we don't really know what they plan on doing with the path
3002 * from here on, so for now just mark it as blocking
3004 if (!p->leave_spinning)
3005 btrfs_set_path_blocking(p);
3006 if (ret < 0 && !p->skip_release_on_error)
3007 btrfs_release_path(p);
3012 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
3013 * current state of the tree together with the operations recorded in the tree
3014 * modification log to search for the key in a previous version of this tree, as
3015 * denoted by the time_seq parameter.
3017 * Naturally, there is no support for insert, delete or cow operations.
3019 * The resulting path and return value will be set up as if we called
3020 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
3022 int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
3023 struct btrfs_path *p, u64 time_seq)
3025 struct btrfs_fs_info *fs_info = root->fs_info;
3026 struct extent_buffer *b;
3031 int lowest_unlock = 1;
3032 u8 lowest_level = 0;
3035 lowest_level = p->lowest_level;
3036 WARN_ON(p->nodes[0] != NULL);
3038 if (p->search_commit_root) {
3040 return btrfs_search_slot(NULL, root, key, p, 0, 0);
3044 b = get_old_root(root, time_seq);
3049 level = btrfs_header_level(b);
3050 p->locks[level] = BTRFS_READ_LOCK;
3053 level = btrfs_header_level(b);
3054 p->nodes[level] = b;
3057 * we have a lock on b and as long as we aren't changing
3058 * the tree, there is no way to for the items in b to change.
3059 * It is safe to drop the lock on our parent before we
3060 * go through the expensive btree search on b.
3062 btrfs_unlock_up_safe(p, level + 1);
3065 * Since we can unwind ebs we want to do a real search every
3069 ret = key_search(b, key, level, &prev_cmp, &slot);
3075 if (ret && slot > 0) {
3079 p->slots[level] = slot;
3080 unlock_up(p, level, lowest_unlock, 0, NULL);
3082 if (level == lowest_level) {
3088 err = read_block_for_search(root, p, &b, level,
3097 level = btrfs_header_level(b);
3098 if (!btrfs_tree_read_lock_atomic(b)) {
3099 btrfs_set_path_blocking(p);
3100 btrfs_tree_read_lock(b);
3102 b = tree_mod_log_rewind(fs_info, p, b, time_seq);
3107 p->locks[level] = BTRFS_READ_LOCK;
3108 p->nodes[level] = b;
3110 p->slots[level] = slot;
3111 unlock_up(p, level, lowest_unlock, 0, NULL);
3117 if (!p->leave_spinning)
3118 btrfs_set_path_blocking(p);
3120 btrfs_release_path(p);
3126 * helper to use instead of search slot if no exact match is needed but
3127 * instead the next or previous item should be returned.
3128 * When find_higher is true, the next higher item is returned, the next lower
3130 * When return_any and find_higher are both true, and no higher item is found,
3131 * return the next lower instead.
3132 * When return_any is true and find_higher is false, and no lower item is found,
3133 * return the next higher instead.
3134 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3137 int btrfs_search_slot_for_read(struct btrfs_root *root,
3138 const struct btrfs_key *key,
3139 struct btrfs_path *p, int find_higher,
3143 struct extent_buffer *leaf;
3146 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3150 * a return value of 1 means the path is at the position where the
3151 * item should be inserted. Normally this is the next bigger item,
3152 * but in case the previous item is the last in a leaf, path points
3153 * to the first free slot in the previous leaf, i.e. at an invalid
3159 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3160 ret = btrfs_next_leaf(root, p);
3166 * no higher item found, return the next
3171 btrfs_release_path(p);
3175 if (p->slots[0] == 0) {
3176 ret = btrfs_prev_leaf(root, p);
3181 if (p->slots[0] == btrfs_header_nritems(leaf))
3188 * no lower item found, return the next
3193 btrfs_release_path(p);
3203 * adjust the pointers going up the tree, starting at level
3204 * making sure the right key of each node is points to 'key'.
3205 * This is used after shifting pointers to the left, so it stops
3206 * fixing up pointers when a given leaf/node is not in slot 0 of the
3210 static void fixup_low_keys(struct btrfs_path *path,
3211 struct btrfs_disk_key *key, int level)
3214 struct extent_buffer *t;
3217 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
3218 int tslot = path->slots[i];
3220 if (!path->nodes[i])
3223 ret = tree_mod_log_insert_key(t, tslot, MOD_LOG_KEY_REPLACE,
3226 btrfs_set_node_key(t, key, tslot);
3227 btrfs_mark_buffer_dirty(path->nodes[i]);
3236 * This function isn't completely safe. It's the caller's responsibility
3237 * that the new key won't break the order
3239 void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
3240 struct btrfs_path *path,
3241 const struct btrfs_key *new_key)
3243 struct btrfs_disk_key disk_key;
3244 struct extent_buffer *eb;
3247 eb = path->nodes[0];
3248 slot = path->slots[0];
3250 btrfs_item_key(eb, &disk_key, slot - 1);
3251 if (unlikely(comp_keys(&disk_key, new_key) >= 0)) {
3253 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
3254 slot, btrfs_disk_key_objectid(&disk_key),
3255 btrfs_disk_key_type(&disk_key),
3256 btrfs_disk_key_offset(&disk_key),
3257 new_key->objectid, new_key->type,
3259 btrfs_print_leaf(eb);
3263 if (slot < btrfs_header_nritems(eb) - 1) {
3264 btrfs_item_key(eb, &disk_key, slot + 1);
3265 if (unlikely(comp_keys(&disk_key, new_key) <= 0)) {
3267 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
3268 slot, btrfs_disk_key_objectid(&disk_key),
3269 btrfs_disk_key_type(&disk_key),
3270 btrfs_disk_key_offset(&disk_key),
3271 new_key->objectid, new_key->type,
3273 btrfs_print_leaf(eb);
3278 btrfs_cpu_key_to_disk(&disk_key, new_key);
3279 btrfs_set_item_key(eb, &disk_key, slot);
3280 btrfs_mark_buffer_dirty(eb);
3282 fixup_low_keys(path, &disk_key, 1);
3286 * try to push data from one node into the next node left in the
3289 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3290 * error, and > 0 if there was no room in the left hand block.
3292 static int push_node_left(struct btrfs_trans_handle *trans,
3293 struct extent_buffer *dst,
3294 struct extent_buffer *src, int empty)
3296 struct btrfs_fs_info *fs_info = trans->fs_info;
3302 src_nritems = btrfs_header_nritems(src);
3303 dst_nritems = btrfs_header_nritems(dst);
3304 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
3305 WARN_ON(btrfs_header_generation(src) != trans->transid);
3306 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3308 if (!empty && src_nritems <= 8)
3311 if (push_items <= 0)
3315 push_items = min(src_nritems, push_items);
3316 if (push_items < src_nritems) {
3317 /* leave at least 8 pointers in the node if
3318 * we aren't going to empty it
3320 if (src_nritems - push_items < 8) {
3321 if (push_items <= 8)
3327 push_items = min(src_nritems - 8, push_items);
3329 ret = tree_mod_log_eb_copy(dst, src, dst_nritems, 0, push_items);
3331 btrfs_abort_transaction(trans, ret);
3334 copy_extent_buffer(dst, src,
3335 btrfs_node_key_ptr_offset(dst_nritems),
3336 btrfs_node_key_ptr_offset(0),
3337 push_items * sizeof(struct btrfs_key_ptr));
3339 if (push_items < src_nritems) {
3341 * Don't call tree_mod_log_insert_move here, key removal was
3342 * already fully logged by tree_mod_log_eb_copy above.
3344 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3345 btrfs_node_key_ptr_offset(push_items),
3346 (src_nritems - push_items) *
3347 sizeof(struct btrfs_key_ptr));
3349 btrfs_set_header_nritems(src, src_nritems - push_items);
3350 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3351 btrfs_mark_buffer_dirty(src);
3352 btrfs_mark_buffer_dirty(dst);
3358 * try to push data from one node into the next node right in the
3361 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3362 * error, and > 0 if there was no room in the right hand block.
3364 * this will only push up to 1/2 the contents of the left node over
3366 static int balance_node_right(struct btrfs_trans_handle *trans,
3367 struct extent_buffer *dst,
3368 struct extent_buffer *src)
3370 struct btrfs_fs_info *fs_info = trans->fs_info;
3377 WARN_ON(btrfs_header_generation(src) != trans->transid);
3378 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3380 src_nritems = btrfs_header_nritems(src);
3381 dst_nritems = btrfs_header_nritems(dst);
3382 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
3383 if (push_items <= 0)
3386 if (src_nritems < 4)
3389 max_push = src_nritems / 2 + 1;
3390 /* don't try to empty the node */
3391 if (max_push >= src_nritems)
3394 if (max_push < push_items)
3395 push_items = max_push;
3397 ret = tree_mod_log_insert_move(dst, push_items, 0, dst_nritems);
3399 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3400 btrfs_node_key_ptr_offset(0),
3402 sizeof(struct btrfs_key_ptr));
3404 ret = tree_mod_log_eb_copy(dst, src, 0, src_nritems - push_items,
3407 btrfs_abort_transaction(trans, ret);
3410 copy_extent_buffer(dst, src,
3411 btrfs_node_key_ptr_offset(0),
3412 btrfs_node_key_ptr_offset(src_nritems - push_items),
3413 push_items * sizeof(struct btrfs_key_ptr));
3415 btrfs_set_header_nritems(src, src_nritems - push_items);
3416 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3418 btrfs_mark_buffer_dirty(src);
3419 btrfs_mark_buffer_dirty(dst);
3425 * helper function to insert a new root level in the tree.
3426 * A new node is allocated, and a single item is inserted to
3427 * point to the existing root
3429 * returns zero on success or < 0 on failure.
3431 static noinline int insert_new_root(struct btrfs_trans_handle *trans,
3432 struct btrfs_root *root,
3433 struct btrfs_path *path, int level)
3435 struct btrfs_fs_info *fs_info = root->fs_info;
3437 struct extent_buffer *lower;
3438 struct extent_buffer *c;
3439 struct extent_buffer *old;
3440 struct btrfs_disk_key lower_key;
3443 BUG_ON(path->nodes[level]);
3444 BUG_ON(path->nodes[level-1] != root->node);
3446 lower = path->nodes[level-1];
3448 btrfs_item_key(lower, &lower_key, 0);
3450 btrfs_node_key(lower, &lower_key, 0);
3452 c = alloc_tree_block_no_bg_flush(trans, root, 0, &lower_key, level,
3453 root->node->start, 0);
3457 root_add_used(root, fs_info->nodesize);
3459 btrfs_set_header_nritems(c, 1);
3460 btrfs_set_node_key(c, &lower_key, 0);
3461 btrfs_set_node_blockptr(c, 0, lower->start);
3462 lower_gen = btrfs_header_generation(lower);
3463 WARN_ON(lower_gen != trans->transid);
3465 btrfs_set_node_ptr_generation(c, 0, lower_gen);
3467 btrfs_mark_buffer_dirty(c);
3470 ret = tree_mod_log_insert_root(root->node, c, 0);
3472 rcu_assign_pointer(root->node, c);
3474 /* the super has an extra ref to root->node */
3475 free_extent_buffer(old);
3477 add_root_to_dirty_list(root);
3478 extent_buffer_get(c);
3479 path->nodes[level] = c;
3480 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
3481 path->slots[level] = 0;
3486 * worker function to insert a single pointer in a node.
3487 * the node should have enough room for the pointer already
3489 * slot and level indicate where you want the key to go, and
3490 * blocknr is the block the key points to.
3492 static void insert_ptr(struct btrfs_trans_handle *trans,
3493 struct btrfs_path *path,
3494 struct btrfs_disk_key *key, u64 bytenr,
3495 int slot, int level)
3497 struct extent_buffer *lower;
3501 BUG_ON(!path->nodes[level]);
3502 btrfs_assert_tree_locked(path->nodes[level]);
3503 lower = path->nodes[level];
3504 nritems = btrfs_header_nritems(lower);
3505 BUG_ON(slot > nritems);
3506 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(trans->fs_info));
3507 if (slot != nritems) {
3509 ret = tree_mod_log_insert_move(lower, slot + 1, slot,
3513 memmove_extent_buffer(lower,
3514 btrfs_node_key_ptr_offset(slot + 1),
3515 btrfs_node_key_ptr_offset(slot),
3516 (nritems - slot) * sizeof(struct btrfs_key_ptr));
3519 ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD,
3523 btrfs_set_node_key(lower, key, slot);
3524 btrfs_set_node_blockptr(lower, slot, bytenr);
3525 WARN_ON(trans->transid == 0);
3526 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
3527 btrfs_set_header_nritems(lower, nritems + 1);
3528 btrfs_mark_buffer_dirty(lower);
3532 * split the node at the specified level in path in two.
3533 * The path is corrected to point to the appropriate node after the split
3535 * Before splitting this tries to make some room in the node by pushing
3536 * left and right, if either one works, it returns right away.
3538 * returns 0 on success and < 0 on failure
3540 static noinline int split_node(struct btrfs_trans_handle *trans,
3541 struct btrfs_root *root,
3542 struct btrfs_path *path, int level)
3544 struct btrfs_fs_info *fs_info = root->fs_info;
3545 struct extent_buffer *c;
3546 struct extent_buffer *split;
3547 struct btrfs_disk_key disk_key;
3552 c = path->nodes[level];
3553 WARN_ON(btrfs_header_generation(c) != trans->transid);
3554 if (c == root->node) {
3556 * trying to split the root, lets make a new one
3558 * tree mod log: We don't log_removal old root in
3559 * insert_new_root, because that root buffer will be kept as a
3560 * normal node. We are going to log removal of half of the
3561 * elements below with tree_mod_log_eb_copy. We're holding a
3562 * tree lock on the buffer, which is why we cannot race with
3563 * other tree_mod_log users.
3565 ret = insert_new_root(trans, root, path, level + 1);
3569 ret = push_nodes_for_insert(trans, root, path, level);
3570 c = path->nodes[level];
3571 if (!ret && btrfs_header_nritems(c) <
3572 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
3578 c_nritems = btrfs_header_nritems(c);
3579 mid = (c_nritems + 1) / 2;
3580 btrfs_node_key(c, &disk_key, mid);
3582 split = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, level,
3585 return PTR_ERR(split);
3587 root_add_used(root, fs_info->nodesize);
3588 ASSERT(btrfs_header_level(c) == level);
3590 ret = tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid);
3592 btrfs_tree_unlock(split);
3593 free_extent_buffer(split);
3594 btrfs_abort_transaction(trans, ret);
3597 copy_extent_buffer(split, c,
3598 btrfs_node_key_ptr_offset(0),
3599 btrfs_node_key_ptr_offset(mid),
3600 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3601 btrfs_set_header_nritems(split, c_nritems - mid);
3602 btrfs_set_header_nritems(c, mid);
3605 btrfs_mark_buffer_dirty(c);
3606 btrfs_mark_buffer_dirty(split);
3608 insert_ptr(trans, path, &disk_key, split->start,
3609 path->slots[level + 1] + 1, level + 1);
3611 if (path->slots[level] >= mid) {
3612 path->slots[level] -= mid;
3613 btrfs_tree_unlock(c);
3614 free_extent_buffer(c);
3615 path->nodes[level] = split;
3616 path->slots[level + 1] += 1;
3618 btrfs_tree_unlock(split);
3619 free_extent_buffer(split);
3625 * how many bytes are required to store the items in a leaf. start
3626 * and nr indicate which items in the leaf to check. This totals up the
3627 * space used both by the item structs and the item data
3629 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
3631 struct btrfs_item *start_item;
3632 struct btrfs_item *end_item;
3633 struct btrfs_map_token token;
3635 int nritems = btrfs_header_nritems(l);
3636 int end = min(nritems, start + nr) - 1;
3640 btrfs_init_map_token(&token, l);
3641 start_item = btrfs_item_nr(start);
3642 end_item = btrfs_item_nr(end);
3643 data_len = btrfs_token_item_offset(l, start_item, &token) +
3644 btrfs_token_item_size(l, start_item, &token);
3645 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
3646 data_len += sizeof(struct btrfs_item) * nr;
3647 WARN_ON(data_len < 0);
3652 * The space between the end of the leaf items and
3653 * the start of the leaf data. IOW, how much room
3654 * the leaf has left for both items and data
3656 noinline int btrfs_leaf_free_space(struct extent_buffer *leaf)
3658 struct btrfs_fs_info *fs_info = leaf->fs_info;
3659 int nritems = btrfs_header_nritems(leaf);
3662 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
3665 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
3667 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
3668 leaf_space_used(leaf, 0, nritems), nritems);
3674 * min slot controls the lowest index we're willing to push to the
3675 * right. We'll push up to and including min_slot, but no lower
3677 static noinline int __push_leaf_right(struct btrfs_path *path,
3678 int data_size, int empty,
3679 struct extent_buffer *right,
3680 int free_space, u32 left_nritems,
3683 struct btrfs_fs_info *fs_info = right->fs_info;
3684 struct extent_buffer *left = path->nodes[0];
3685 struct extent_buffer *upper = path->nodes[1];
3686 struct btrfs_map_token token;
3687 struct btrfs_disk_key disk_key;
3692 struct btrfs_item *item;
3701 nr = max_t(u32, 1, min_slot);
3703 if (path->slots[0] >= left_nritems)
3704 push_space += data_size;
3706 slot = path->slots[1];
3707 i = left_nritems - 1;
3709 item = btrfs_item_nr(i);
3711 if (!empty && push_items > 0) {
3712 if (path->slots[0] > i)
3714 if (path->slots[0] == i) {
3715 int space = btrfs_leaf_free_space(left);
3717 if (space + push_space * 2 > free_space)
3722 if (path->slots[0] == i)
3723 push_space += data_size;
3725 this_item_size = btrfs_item_size(left, item);
3726 if (this_item_size + sizeof(*item) + push_space > free_space)
3730 push_space += this_item_size + sizeof(*item);
3736 if (push_items == 0)
3739 WARN_ON(!empty && push_items == left_nritems);
3741 /* push left to right */
3742 right_nritems = btrfs_header_nritems(right);
3744 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
3745 push_space -= leaf_data_end(left);
3747 /* make room in the right data area */
3748 data_end = leaf_data_end(right);
3749 memmove_extent_buffer(right,
3750 BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
3751 BTRFS_LEAF_DATA_OFFSET + data_end,
3752 BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
3754 /* copy from the left data area */
3755 copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
3756 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
3757 BTRFS_LEAF_DATA_OFFSET + leaf_data_end(left),
3760 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3761 btrfs_item_nr_offset(0),
3762 right_nritems * sizeof(struct btrfs_item));
3764 /* copy the items from left to right */
3765 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3766 btrfs_item_nr_offset(left_nritems - push_items),
3767 push_items * sizeof(struct btrfs_item));
3769 /* update the item pointers */
3770 btrfs_init_map_token(&token, right);
3771 right_nritems += push_items;
3772 btrfs_set_header_nritems(right, right_nritems);
3773 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
3774 for (i = 0; i < right_nritems; i++) {
3775 item = btrfs_item_nr(i);
3776 push_space -= btrfs_token_item_size(right, item, &token);
3777 btrfs_set_token_item_offset(right, item, push_space, &token);
3780 left_nritems -= push_items;
3781 btrfs_set_header_nritems(left, left_nritems);
3784 btrfs_mark_buffer_dirty(left);
3786 btrfs_clean_tree_block(left);
3788 btrfs_mark_buffer_dirty(right);
3790 btrfs_item_key(right, &disk_key, 0);
3791 btrfs_set_node_key(upper, &disk_key, slot + 1);
3792 btrfs_mark_buffer_dirty(upper);
3794 /* then fixup the leaf pointer in the path */
3795 if (path->slots[0] >= left_nritems) {
3796 path->slots[0] -= left_nritems;
3797 if (btrfs_header_nritems(path->nodes[0]) == 0)
3798 btrfs_clean_tree_block(path->nodes[0]);
3799 btrfs_tree_unlock(path->nodes[0]);
3800 free_extent_buffer(path->nodes[0]);
3801 path->nodes[0] = right;
3802 path->slots[1] += 1;
3804 btrfs_tree_unlock(right);
3805 free_extent_buffer(right);
3810 btrfs_tree_unlock(right);
3811 free_extent_buffer(right);
3816 * push some data in the path leaf to the right, trying to free up at
3817 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3819 * returns 1 if the push failed because the other node didn't have enough
3820 * room, 0 if everything worked out and < 0 if there were major errors.
3822 * this will push starting from min_slot to the end of the leaf. It won't
3823 * push any slot lower than min_slot
3825 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
3826 *root, struct btrfs_path *path,
3827 int min_data_size, int data_size,
3828 int empty, u32 min_slot)
3830 struct extent_buffer *left = path->nodes[0];
3831 struct extent_buffer *right;
3832 struct extent_buffer *upper;
3838 if (!path->nodes[1])
3841 slot = path->slots[1];
3842 upper = path->nodes[1];
3843 if (slot >= btrfs_header_nritems(upper) - 1)
3846 btrfs_assert_tree_locked(path->nodes[1]);
3848 right = btrfs_read_node_slot(upper, slot + 1);
3850 * slot + 1 is not valid or we fail to read the right node,
3851 * no big deal, just return.
3856 btrfs_tree_lock(right);
3857 btrfs_set_lock_blocking_write(right);
3859 free_space = btrfs_leaf_free_space(right);
3860 if (free_space < data_size)
3863 /* cow and double check */
3864 ret = btrfs_cow_block(trans, root, right, upper,
3869 free_space = btrfs_leaf_free_space(right);
3870 if (free_space < data_size)
3873 left_nritems = btrfs_header_nritems(left);
3874 if (left_nritems == 0)
3877 if (path->slots[0] == left_nritems && !empty) {
3878 /* Key greater than all keys in the leaf, right neighbor has
3879 * enough room for it and we're not emptying our leaf to delete
3880 * it, therefore use right neighbor to insert the new item and
3881 * no need to touch/dirty our left leaf. */
3882 btrfs_tree_unlock(left);
3883 free_extent_buffer(left);
3884 path->nodes[0] = right;
3890 return __push_leaf_right(path, min_data_size, empty,
3891 right, free_space, left_nritems, min_slot);
3893 btrfs_tree_unlock(right);
3894 free_extent_buffer(right);
3899 * push some data in the path leaf to the left, trying to free up at
3900 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3902 * max_slot can put a limit on how far into the leaf we'll push items. The
3903 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3906 static noinline int __push_leaf_left(struct btrfs_path *path, int data_size,
3907 int empty, struct extent_buffer *left,
3908 int free_space, u32 right_nritems,
3911 struct btrfs_fs_info *fs_info = left->fs_info;
3912 struct btrfs_disk_key disk_key;
3913 struct extent_buffer *right = path->nodes[0];
3917 struct btrfs_item *item;
3918 u32 old_left_nritems;
3922 u32 old_left_item_size;
3923 struct btrfs_map_token token;
3926 nr = min(right_nritems, max_slot);
3928 nr = min(right_nritems - 1, max_slot);
3930 for (i = 0; i < nr; i++) {
3931 item = btrfs_item_nr(i);
3933 if (!empty && push_items > 0) {
3934 if (path->slots[0] < i)
3936 if (path->slots[0] == i) {
3937 int space = btrfs_leaf_free_space(right);
3939 if (space + push_space * 2 > free_space)
3944 if (path->slots[0] == i)
3945 push_space += data_size;
3947 this_item_size = btrfs_item_size(right, item);
3948 if (this_item_size + sizeof(*item) + push_space > free_space)
3952 push_space += this_item_size + sizeof(*item);
3955 if (push_items == 0) {
3959 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
3961 /* push data from right to left */
3962 copy_extent_buffer(left, right,
3963 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3964 btrfs_item_nr_offset(0),
3965 push_items * sizeof(struct btrfs_item));
3967 push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
3968 btrfs_item_offset_nr(right, push_items - 1);
3970 copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
3971 leaf_data_end(left) - push_space,
3972 BTRFS_LEAF_DATA_OFFSET +
3973 btrfs_item_offset_nr(right, push_items - 1),
3975 old_left_nritems = btrfs_header_nritems(left);
3976 BUG_ON(old_left_nritems <= 0);
3978 btrfs_init_map_token(&token, left);
3979 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
3980 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
3983 item = btrfs_item_nr(i);
3985 ioff = btrfs_token_item_offset(left, item, &token);
3986 btrfs_set_token_item_offset(left, item,
3987 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size),
3990 btrfs_set_header_nritems(left, old_left_nritems + push_items);
3992 /* fixup right node */
3993 if (push_items > right_nritems)
3994 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
3997 if (push_items < right_nritems) {
3998 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3999 leaf_data_end(right);
4000 memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
4001 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
4002 BTRFS_LEAF_DATA_OFFSET +
4003 leaf_data_end(right), push_space);
4005 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
4006 btrfs_item_nr_offset(push_items),
4007 (btrfs_header_nritems(right) - push_items) *
4008 sizeof(struct btrfs_item));
4011 btrfs_init_map_token(&token, right);
4012 right_nritems -= push_items;
4013 btrfs_set_header_nritems(right, right_nritems);
4014 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
4015 for (i = 0; i < right_nritems; i++) {
4016 item = btrfs_item_nr(i);
4018 push_space = push_space - btrfs_token_item_size(right,
4020 btrfs_set_token_item_offset(right, item, push_space, &token);
4023 btrfs_mark_buffer_dirty(left);
4025 btrfs_mark_buffer_dirty(right);
4027 btrfs_clean_tree_block(right);
4029 btrfs_item_key(right, &disk_key, 0);
4030 fixup_low_keys(path, &disk_key, 1);
4032 /* then fixup the leaf pointer in the path */
4033 if (path->slots[0] < push_items) {
4034 path->slots[0] += old_left_nritems;
4035 btrfs_tree_unlock(path->nodes[0]);
4036 free_extent_buffer(path->nodes[0]);
4037 path->nodes[0] = left;
4038 path->slots[1] -= 1;
4040 btrfs_tree_unlock(left);
4041 free_extent_buffer(left);
4042 path->slots[0] -= push_items;
4044 BUG_ON(path->slots[0] < 0);
4047 btrfs_tree_unlock(left);
4048 free_extent_buffer(left);
4053 * push some data in the path leaf to the left, trying to free up at
4054 * least data_size bytes. returns zero if the push worked, nonzero otherwise
4056 * max_slot can put a limit on how far into the leaf we'll push items. The
4057 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
4060 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
4061 *root, struct btrfs_path *path, int min_data_size,
4062 int data_size, int empty, u32 max_slot)
4064 struct extent_buffer *right = path->nodes[0];
4065 struct extent_buffer *left;
4071 slot = path->slots[1];
4074 if (!path->nodes[1])
4077 right_nritems = btrfs_header_nritems(right);
4078 if (right_nritems == 0)
4081 btrfs_assert_tree_locked(path->nodes[1]);
4083 left = btrfs_read_node_slot(path->nodes[1], slot - 1);
4085 * slot - 1 is not valid or we fail to read the left node,
4086 * no big deal, just return.
4091 btrfs_tree_lock(left);
4092 btrfs_set_lock_blocking_write(left);
4094 free_space = btrfs_leaf_free_space(left);
4095 if (free_space < data_size) {
4100 /* cow and double check */
4101 ret = btrfs_cow_block(trans, root, left,
4102 path->nodes[1], slot - 1, &left);
4104 /* we hit -ENOSPC, but it isn't fatal here */
4110 free_space = btrfs_leaf_free_space(left);
4111 if (free_space < data_size) {
4116 return __push_leaf_left(path, min_data_size,
4117 empty, left, free_space, right_nritems,
4120 btrfs_tree_unlock(left);
4121 free_extent_buffer(left);
4126 * split the path's leaf in two, making sure there is at least data_size
4127 * available for the resulting leaf level of the path.
4129 static noinline void copy_for_split(struct btrfs_trans_handle *trans,
4130 struct btrfs_path *path,
4131 struct extent_buffer *l,
4132 struct extent_buffer *right,
4133 int slot, int mid, int nritems)
4135 struct btrfs_fs_info *fs_info = trans->fs_info;
4139 struct btrfs_disk_key disk_key;
4140 struct btrfs_map_token token;
4142 nritems = nritems - mid;
4143 btrfs_set_header_nritems(right, nritems);
4144 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(l);
4146 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4147 btrfs_item_nr_offset(mid),
4148 nritems * sizeof(struct btrfs_item));
4150 copy_extent_buffer(right, l,
4151 BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
4152 data_copy_size, BTRFS_LEAF_DATA_OFFSET +
4153 leaf_data_end(l), data_copy_size);
4155 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
4157 btrfs_init_map_token(&token, right);
4158 for (i = 0; i < nritems; i++) {
4159 struct btrfs_item *item = btrfs_item_nr(i);
4162 ioff = btrfs_token_item_offset(right, item, &token);
4163 btrfs_set_token_item_offset(right, item,
4164 ioff + rt_data_off, &token);
4167 btrfs_set_header_nritems(l, mid);
4168 btrfs_item_key(right, &disk_key, 0);
4169 insert_ptr(trans, path, &disk_key, right->start, path->slots[1] + 1, 1);
4171 btrfs_mark_buffer_dirty(right);
4172 btrfs_mark_buffer_dirty(l);
4173 BUG_ON(path->slots[0] != slot);
4176 btrfs_tree_unlock(path->nodes[0]);
4177 free_extent_buffer(path->nodes[0]);
4178 path->nodes[0] = right;
4179 path->slots[0] -= mid;
4180 path->slots[1] += 1;
4182 btrfs_tree_unlock(right);
4183 free_extent_buffer(right);
4186 BUG_ON(path->slots[0] < 0);
4190 * double splits happen when we need to insert a big item in the middle
4191 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4192 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4195 * We avoid this by trying to push the items on either side of our target
4196 * into the adjacent leaves. If all goes well we can avoid the double split
4199 static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4200 struct btrfs_root *root,
4201 struct btrfs_path *path,
4208 int space_needed = data_size;
4210 slot = path->slots[0];
4211 if (slot < btrfs_header_nritems(path->nodes[0]))
4212 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
4215 * try to push all the items after our slot into the
4218 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
4225 nritems = btrfs_header_nritems(path->nodes[0]);
4227 * our goal is to get our slot at the start or end of a leaf. If
4228 * we've done so we're done
4230 if (path->slots[0] == 0 || path->slots[0] == nritems)
4233 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
4236 /* try to push all the items before our slot into the next leaf */
4237 slot = path->slots[0];
4238 space_needed = data_size;
4240 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
4241 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
4254 * split the path's leaf in two, making sure there is at least data_size
4255 * available for the resulting leaf level of the path.
4257 * returns 0 if all went well and < 0 on failure.
4259 static noinline int split_leaf(struct btrfs_trans_handle *trans,
4260 struct btrfs_root *root,
4261 const struct btrfs_key *ins_key,
4262 struct btrfs_path *path, int data_size,
4265 struct btrfs_disk_key disk_key;
4266 struct extent_buffer *l;
4270 struct extent_buffer *right;
4271 struct btrfs_fs_info *fs_info = root->fs_info;
4275 int num_doubles = 0;
4276 int tried_avoid_double = 0;
4279 slot = path->slots[0];
4280 if (extend && data_size + btrfs_item_size_nr(l, slot) +
4281 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
4284 /* first try to make some room by pushing left and right */
4285 if (data_size && path->nodes[1]) {
4286 int space_needed = data_size;
4288 if (slot < btrfs_header_nritems(l))
4289 space_needed -= btrfs_leaf_free_space(l);
4291 wret = push_leaf_right(trans, root, path, space_needed,
4292 space_needed, 0, 0);
4296 space_needed = data_size;
4298 space_needed -= btrfs_leaf_free_space(l);
4299 wret = push_leaf_left(trans, root, path, space_needed,
4300 space_needed, 0, (u32)-1);
4306 /* did the pushes work? */
4307 if (btrfs_leaf_free_space(l) >= data_size)
4311 if (!path->nodes[1]) {
4312 ret = insert_new_root(trans, root, path, 1);
4319 slot = path->slots[0];
4320 nritems = btrfs_header_nritems(l);
4321 mid = (nritems + 1) / 2;
4325 leaf_space_used(l, mid, nritems - mid) + data_size >
4326 BTRFS_LEAF_DATA_SIZE(fs_info)) {
4327 if (slot >= nritems) {
4331 if (mid != nritems &&
4332 leaf_space_used(l, mid, nritems - mid) +
4333 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
4334 if (data_size && !tried_avoid_double)
4335 goto push_for_double;
4341 if (leaf_space_used(l, 0, mid) + data_size >
4342 BTRFS_LEAF_DATA_SIZE(fs_info)) {
4343 if (!extend && data_size && slot == 0) {
4345 } else if ((extend || !data_size) && slot == 0) {
4349 if (mid != nritems &&
4350 leaf_space_used(l, mid, nritems - mid) +
4351 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
4352 if (data_size && !tried_avoid_double)
4353 goto push_for_double;
4361 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4363 btrfs_item_key(l, &disk_key, mid);
4365 right = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, 0,
4368 return PTR_ERR(right);
4370 root_add_used(root, fs_info->nodesize);
4374 btrfs_set_header_nritems(right, 0);
4375 insert_ptr(trans, path, &disk_key,
4376 right->start, path->slots[1] + 1, 1);
4377 btrfs_tree_unlock(path->nodes[0]);
4378 free_extent_buffer(path->nodes[0]);
4379 path->nodes[0] = right;
4381 path->slots[1] += 1;
4383 btrfs_set_header_nritems(right, 0);
4384 insert_ptr(trans, path, &disk_key,
4385 right->start, path->slots[1], 1);
4386 btrfs_tree_unlock(path->nodes[0]);
4387 free_extent_buffer(path->nodes[0]);
4388 path->nodes[0] = right;
4390 if (path->slots[1] == 0)
4391 fixup_low_keys(path, &disk_key, 1);
4394 * We create a new leaf 'right' for the required ins_len and
4395 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
4396 * the content of ins_len to 'right'.
4401 copy_for_split(trans, path, l, right, slot, mid, nritems);
4404 BUG_ON(num_doubles != 0);
4412 push_for_double_split(trans, root, path, data_size);
4413 tried_avoid_double = 1;
4414 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
4419 static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4420 struct btrfs_root *root,
4421 struct btrfs_path *path, int ins_len)
4423 struct btrfs_key key;
4424 struct extent_buffer *leaf;
4425 struct btrfs_file_extent_item *fi;
4430 leaf = path->nodes[0];
4431 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4433 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4434 key.type != BTRFS_EXTENT_CSUM_KEY);
4436 if (btrfs_leaf_free_space(leaf) >= ins_len)
4439 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4440 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4441 fi = btrfs_item_ptr(leaf, path->slots[0],
4442 struct btrfs_file_extent_item);
4443 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4445 btrfs_release_path(path);
4447 path->keep_locks = 1;
4448 path->search_for_split = 1;
4449 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4450 path->search_for_split = 0;
4457 leaf = path->nodes[0];
4458 /* if our item isn't there, return now */
4459 if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4462 /* the leaf has changed, it now has room. return now */
4463 if (btrfs_leaf_free_space(path->nodes[0]) >= ins_len)
4466 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4467 fi = btrfs_item_ptr(leaf, path->slots[0],
4468 struct btrfs_file_extent_item);
4469 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4473 btrfs_set_path_blocking(path);
4474 ret = split_leaf(trans, root, &key, path, ins_len, 1);
4478 path->keep_locks = 0;
4479 btrfs_unlock_up_safe(path, 1);
4482 path->keep_locks = 0;
4486 static noinline int split_item(struct btrfs_path *path,
4487 const struct btrfs_key *new_key,
4488 unsigned long split_offset)
4490 struct extent_buffer *leaf;
4491 struct btrfs_item *item;
4492 struct btrfs_item *new_item;
4498 struct btrfs_disk_key disk_key;
4500 leaf = path->nodes[0];
4501 BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item));
4503 btrfs_set_path_blocking(path);
4505 item = btrfs_item_nr(path->slots[0]);
4506 orig_offset = btrfs_item_offset(leaf, item);
4507 item_size = btrfs_item_size(leaf, item);
4509 buf = kmalloc(item_size, GFP_NOFS);
4513 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4514 path->slots[0]), item_size);
4516 slot = path->slots[0] + 1;
4517 nritems = btrfs_header_nritems(leaf);
4518 if (slot != nritems) {
4519 /* shift the items */
4520 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
4521 btrfs_item_nr_offset(slot),
4522 (nritems - slot) * sizeof(struct btrfs_item));
4525 btrfs_cpu_key_to_disk(&disk_key, new_key);
4526 btrfs_set_item_key(leaf, &disk_key, slot);
4528 new_item = btrfs_item_nr(slot);
4530 btrfs_set_item_offset(leaf, new_item, orig_offset);
4531 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4533 btrfs_set_item_offset(leaf, item,
4534 orig_offset + item_size - split_offset);
4535 btrfs_set_item_size(leaf, item, split_offset);
4537 btrfs_set_header_nritems(leaf, nritems + 1);
4539 /* write the data for the start of the original item */
4540 write_extent_buffer(leaf, buf,
4541 btrfs_item_ptr_offset(leaf, path->slots[0]),
4544 /* write the data for the new item */
4545 write_extent_buffer(leaf, buf + split_offset,
4546 btrfs_item_ptr_offset(leaf, slot),
4547 item_size - split_offset);
4548 btrfs_mark_buffer_dirty(leaf);
4550 BUG_ON(btrfs_leaf_free_space(leaf) < 0);
4556 * This function splits a single item into two items,
4557 * giving 'new_key' to the new item and splitting the
4558 * old one at split_offset (from the start of the item).
4560 * The path may be released by this operation. After
4561 * the split, the path is pointing to the old item. The
4562 * new item is going to be in the same node as the old one.
4564 * Note, the item being split must be smaller enough to live alone on
4565 * a tree block with room for one extra struct btrfs_item
4567 * This allows us to split the item in place, keeping a lock on the
4568 * leaf the entire time.
4570 int btrfs_split_item(struct btrfs_trans_handle *trans,
4571 struct btrfs_root *root,
4572 struct btrfs_path *path,
4573 const struct btrfs_key *new_key,
4574 unsigned long split_offset)
4577 ret = setup_leaf_for_split(trans, root, path,
4578 sizeof(struct btrfs_item));
4582 ret = split_item(path, new_key, split_offset);
4587 * This function duplicate a item, giving 'new_key' to the new item.
4588 * It guarantees both items live in the same tree leaf and the new item
4589 * is contiguous with the original item.
4591 * This allows us to split file extent in place, keeping a lock on the
4592 * leaf the entire time.
4594 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4595 struct btrfs_root *root,
4596 struct btrfs_path *path,
4597 const struct btrfs_key *new_key)
4599 struct extent_buffer *leaf;
4603 leaf = path->nodes[0];
4604 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4605 ret = setup_leaf_for_split(trans, root, path,
4606 item_size + sizeof(struct btrfs_item));
4611 setup_items_for_insert(root, path, new_key, &item_size,
4612 item_size, item_size +
4613 sizeof(struct btrfs_item), 1);
4614 leaf = path->nodes[0];
4615 memcpy_extent_buffer(leaf,
4616 btrfs_item_ptr_offset(leaf, path->slots[0]),
4617 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4623 * make the item pointed to by the path smaller. new_size indicates
4624 * how small to make it, and from_end tells us if we just chop bytes
4625 * off the end of the item or if we shift the item to chop bytes off
4628 void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end)
4631 struct extent_buffer *leaf;
4632 struct btrfs_item *item;
4634 unsigned int data_end;
4635 unsigned int old_data_start;
4636 unsigned int old_size;
4637 unsigned int size_diff;
4639 struct btrfs_map_token token;
4641 leaf = path->nodes[0];
4642 slot = path->slots[0];
4644 old_size = btrfs_item_size_nr(leaf, slot);
4645 if (old_size == new_size)
4648 nritems = btrfs_header_nritems(leaf);
4649 data_end = leaf_data_end(leaf);
4651 old_data_start = btrfs_item_offset_nr(leaf, slot);
4653 size_diff = old_size - new_size;
4656 BUG_ON(slot >= nritems);
4659 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4661 /* first correct the data pointers */
4662 btrfs_init_map_token(&token, leaf);
4663 for (i = slot; i < nritems; i++) {
4665 item = btrfs_item_nr(i);
4667 ioff = btrfs_token_item_offset(leaf, item, &token);
4668 btrfs_set_token_item_offset(leaf, item,
4669 ioff + size_diff, &token);
4672 /* shift the data */
4674 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4675 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
4676 data_end, old_data_start + new_size - data_end);
4678 struct btrfs_disk_key disk_key;
4681 btrfs_item_key(leaf, &disk_key, slot);
4683 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4685 struct btrfs_file_extent_item *fi;
4687 fi = btrfs_item_ptr(leaf, slot,
4688 struct btrfs_file_extent_item);
4689 fi = (struct btrfs_file_extent_item *)(
4690 (unsigned long)fi - size_diff);
4692 if (btrfs_file_extent_type(leaf, fi) ==
4693 BTRFS_FILE_EXTENT_INLINE) {
4694 ptr = btrfs_item_ptr_offset(leaf, slot);
4695 memmove_extent_buffer(leaf, ptr,
4697 BTRFS_FILE_EXTENT_INLINE_DATA_START);
4701 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4702 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
4703 data_end, old_data_start - data_end);
4705 offset = btrfs_disk_key_offset(&disk_key);
4706 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4707 btrfs_set_item_key(leaf, &disk_key, slot);
4709 fixup_low_keys(path, &disk_key, 1);
4712 item = btrfs_item_nr(slot);
4713 btrfs_set_item_size(leaf, item, new_size);
4714 btrfs_mark_buffer_dirty(leaf);
4716 if (btrfs_leaf_free_space(leaf) < 0) {
4717 btrfs_print_leaf(leaf);
4723 * make the item pointed to by the path bigger, data_size is the added size.
4725 void btrfs_extend_item(struct btrfs_path *path, u32 data_size)
4728 struct extent_buffer *leaf;
4729 struct btrfs_item *item;
4731 unsigned int data_end;
4732 unsigned int old_data;
4733 unsigned int old_size;
4735 struct btrfs_map_token token;
4737 leaf = path->nodes[0];
4739 nritems = btrfs_header_nritems(leaf);
4740 data_end = leaf_data_end(leaf);
4742 if (btrfs_leaf_free_space(leaf) < data_size) {
4743 btrfs_print_leaf(leaf);
4746 slot = path->slots[0];
4747 old_data = btrfs_item_end_nr(leaf, slot);
4750 if (slot >= nritems) {
4751 btrfs_print_leaf(leaf);
4752 btrfs_crit(leaf->fs_info, "slot %d too large, nritems %d",
4758 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4760 /* first correct the data pointers */
4761 btrfs_init_map_token(&token, leaf);
4762 for (i = slot; i < nritems; i++) {
4764 item = btrfs_item_nr(i);
4766 ioff = btrfs_token_item_offset(leaf, item, &token);
4767 btrfs_set_token_item_offset(leaf, item,
4768 ioff - data_size, &token);
4771 /* shift the data */
4772 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4773 data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
4774 data_end, old_data - data_end);
4776 data_end = old_data;
4777 old_size = btrfs_item_size_nr(leaf, slot);
4778 item = btrfs_item_nr(slot);
4779 btrfs_set_item_size(leaf, item, old_size + data_size);
4780 btrfs_mark_buffer_dirty(leaf);
4782 if (btrfs_leaf_free_space(leaf) < 0) {
4783 btrfs_print_leaf(leaf);
4789 * this is a helper for btrfs_insert_empty_items, the main goal here is
4790 * to save stack depth by doing the bulk of the work in a function
4791 * that doesn't call btrfs_search_slot
4793 void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
4794 const struct btrfs_key *cpu_key, u32 *data_size,
4795 u32 total_data, u32 total_size, int nr)
4797 struct btrfs_fs_info *fs_info = root->fs_info;
4798 struct btrfs_item *item;
4801 unsigned int data_end;
4802 struct btrfs_disk_key disk_key;
4803 struct extent_buffer *leaf;
4805 struct btrfs_map_token token;
4807 if (path->slots[0] == 0) {
4808 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
4809 fixup_low_keys(path, &disk_key, 1);
4811 btrfs_unlock_up_safe(path, 1);
4813 leaf = path->nodes[0];
4814 slot = path->slots[0];
4816 nritems = btrfs_header_nritems(leaf);
4817 data_end = leaf_data_end(leaf);
4819 if (btrfs_leaf_free_space(leaf) < total_size) {
4820 btrfs_print_leaf(leaf);
4821 btrfs_crit(fs_info, "not enough freespace need %u have %d",
4822 total_size, btrfs_leaf_free_space(leaf));
4826 btrfs_init_map_token(&token, leaf);
4827 if (slot != nritems) {
4828 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4830 if (old_data < data_end) {
4831 btrfs_print_leaf(leaf);
4832 btrfs_crit(fs_info, "slot %d old_data %d data_end %d",
4833 slot, old_data, data_end);
4837 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4839 /* first correct the data pointers */
4840 for (i = slot; i < nritems; i++) {
4843 item = btrfs_item_nr(i);
4844 ioff = btrfs_token_item_offset(leaf, item, &token);
4845 btrfs_set_token_item_offset(leaf, item,
4846 ioff - total_data, &token);
4848 /* shift the items */
4849 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4850 btrfs_item_nr_offset(slot),
4851 (nritems - slot) * sizeof(struct btrfs_item));
4853 /* shift the data */
4854 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4855 data_end - total_data, BTRFS_LEAF_DATA_OFFSET +
4856 data_end, old_data - data_end);
4857 data_end = old_data;
4860 /* setup the item for the new data */
4861 for (i = 0; i < nr; i++) {
4862 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4863 btrfs_set_item_key(leaf, &disk_key, slot + i);
4864 item = btrfs_item_nr(slot + i);
4865 btrfs_set_token_item_offset(leaf, item,
4866 data_end - data_size[i], &token);
4867 data_end -= data_size[i];
4868 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
4871 btrfs_set_header_nritems(leaf, nritems + nr);
4872 btrfs_mark_buffer_dirty(leaf);
4874 if (btrfs_leaf_free_space(leaf) < 0) {
4875 btrfs_print_leaf(leaf);
4881 * Given a key and some data, insert items into the tree.
4882 * This does all the path init required, making room in the tree if needed.
4884 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4885 struct btrfs_root *root,
4886 struct btrfs_path *path,
4887 const struct btrfs_key *cpu_key, u32 *data_size,
4896 for (i = 0; i < nr; i++)
4897 total_data += data_size[i];
4899 total_size = total_data + (nr * sizeof(struct btrfs_item));
4900 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4906 slot = path->slots[0];
4909 setup_items_for_insert(root, path, cpu_key, data_size,
4910 total_data, total_size, nr);
4915 * Given a key and some data, insert an item into the tree.
4916 * This does all the path init required, making room in the tree if needed.
4918 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4919 const struct btrfs_key *cpu_key, void *data,
4923 struct btrfs_path *path;
4924 struct extent_buffer *leaf;
4927 path = btrfs_alloc_path();
4930 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
4932 leaf = path->nodes[0];
4933 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4934 write_extent_buffer(leaf, data, ptr, data_size);
4935 btrfs_mark_buffer_dirty(leaf);
4937 btrfs_free_path(path);
4942 * delete the pointer from a given node.
4944 * the tree should have been previously balanced so the deletion does not
4947 static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4948 int level, int slot)
4950 struct extent_buffer *parent = path->nodes[level];
4954 nritems = btrfs_header_nritems(parent);
4955 if (slot != nritems - 1) {
4957 ret = tree_mod_log_insert_move(parent, slot, slot + 1,
4958 nritems - slot - 1);
4961 memmove_extent_buffer(parent,
4962 btrfs_node_key_ptr_offset(slot),
4963 btrfs_node_key_ptr_offset(slot + 1),
4964 sizeof(struct btrfs_key_ptr) *
4965 (nritems - slot - 1));
4967 ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE,
4973 btrfs_set_header_nritems(parent, nritems);
4974 if (nritems == 0 && parent == root->node) {
4975 BUG_ON(btrfs_header_level(root->node) != 1);
4976 /* just turn the root into a leaf and break */
4977 btrfs_set_header_level(root->node, 0);
4978 } else if (slot == 0) {
4979 struct btrfs_disk_key disk_key;
4981 btrfs_node_key(parent, &disk_key, 0);
4982 fixup_low_keys(path, &disk_key, level + 1);
4984 btrfs_mark_buffer_dirty(parent);
4988 * a helper function to delete the leaf pointed to by path->slots[1] and
4991 * This deletes the pointer in path->nodes[1] and frees the leaf
4992 * block extent. zero is returned if it all worked out, < 0 otherwise.
4994 * The path must have already been setup for deleting the leaf, including
4995 * all the proper balancing. path->nodes[1] must be locked.
4997 static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4998 struct btrfs_root *root,
4999 struct btrfs_path *path,
5000 struct extent_buffer *leaf)
5002 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
5003 del_ptr(root, path, 1, path->slots[1]);
5006 * btrfs_free_extent is expensive, we want to make sure we
5007 * aren't holding any locks when we call it
5009 btrfs_unlock_up_safe(path, 0);
5011 root_sub_used(root, leaf->len);
5013 extent_buffer_get(leaf);
5014 btrfs_free_tree_block(trans, root, leaf, 0, 1);
5015 free_extent_buffer_stale(leaf);
5018 * delete the item at the leaf level in path. If that empties
5019 * the leaf, remove it from the tree
5021 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5022 struct btrfs_path *path, int slot, int nr)
5024 struct btrfs_fs_info *fs_info = root->fs_info;
5025 struct extent_buffer *leaf;
5026 struct btrfs_item *item;
5034 leaf = path->nodes[0];
5035 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
5037 for (i = 0; i < nr; i++)
5038 dsize += btrfs_item_size_nr(leaf, slot + i);
5040 nritems = btrfs_header_nritems(leaf);
5042 if (slot + nr != nritems) {
5043 int data_end = leaf_data_end(leaf);
5044 struct btrfs_map_token token;
5046 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
5048 BTRFS_LEAF_DATA_OFFSET + data_end,
5049 last_off - data_end);
5051 btrfs_init_map_token(&token, leaf);
5052 for (i = slot + nr; i < nritems; i++) {
5055 item = btrfs_item_nr(i);
5056 ioff = btrfs_token_item_offset(leaf, item, &token);
5057 btrfs_set_token_item_offset(leaf, item,
5058 ioff + dsize, &token);
5061 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
5062 btrfs_item_nr_offset(slot + nr),
5063 sizeof(struct btrfs_item) *
5064 (nritems - slot - nr));
5066 btrfs_set_header_nritems(leaf, nritems - nr);
5069 /* delete the leaf if we've emptied it */
5071 if (leaf == root->node) {
5072 btrfs_set_header_level(leaf, 0);
5074 btrfs_set_path_blocking(path);
5075 btrfs_clean_tree_block(leaf);
5076 btrfs_del_leaf(trans, root, path, leaf);
5079 int used = leaf_space_used(leaf, 0, nritems);
5081 struct btrfs_disk_key disk_key;
5083 btrfs_item_key(leaf, &disk_key, 0);
5084 fixup_low_keys(path, &disk_key, 1);
5087 /* delete the leaf if it is mostly empty */
5088 if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
5089 /* push_leaf_left fixes the path.
5090 * make sure the path still points to our leaf
5091 * for possible call to del_ptr below
5093 slot = path->slots[1];
5094 extent_buffer_get(leaf);
5096 btrfs_set_path_blocking(path);
5097 wret = push_leaf_left(trans, root, path, 1, 1,
5099 if (wret < 0 && wret != -ENOSPC)
5102 if (path->nodes[0] == leaf &&
5103 btrfs_header_nritems(leaf)) {
5104 wret = push_leaf_right(trans, root, path, 1,
5106 if (wret < 0 && wret != -ENOSPC)
5110 if (btrfs_header_nritems(leaf) == 0) {
5111 path->slots[1] = slot;
5112 btrfs_del_leaf(trans, root, path, leaf);
5113 free_extent_buffer(leaf);
5116 /* if we're still in the path, make sure
5117 * we're dirty. Otherwise, one of the
5118 * push_leaf functions must have already
5119 * dirtied this buffer
5121 if (path->nodes[0] == leaf)
5122 btrfs_mark_buffer_dirty(leaf);
5123 free_extent_buffer(leaf);
5126 btrfs_mark_buffer_dirty(leaf);
5133 * search the tree again to find a leaf with lesser keys
5134 * returns 0 if it found something or 1 if there are no lesser leaves.
5135 * returns < 0 on io errors.
5137 * This may release the path, and so you may lose any locks held at the
5140 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
5142 struct btrfs_key key;
5143 struct btrfs_key orig_key;
5144 struct btrfs_disk_key found_key;
5147 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
5150 if (key.offset > 0) {
5152 } else if (key.type > 0) {
5154 key.offset = (u64)-1;
5155 } else if (key.objectid > 0) {
5158 key.offset = (u64)-1;
5163 btrfs_release_path(path);
5164 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5169 * Previous key not found. Even if we were at slot 0 of the leaf we had
5170 * before releasing the path and calling btrfs_search_slot(), we now may
5171 * be in a slot pointing to the same original key - this can happen if
5172 * after we released the path, one of more items were moved from a
5173 * sibling leaf into the front of the leaf we had due to an insertion
5174 * (see push_leaf_right()).
5175 * If we hit this case and our slot is > 0 and just decrement the slot
5176 * so that the caller does not process the same key again, which may or
5177 * may not break the caller, depending on its logic.
5179 if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
5180 btrfs_item_key(path->nodes[0], &found_key, path->slots[0]);
5181 ret = comp_keys(&found_key, &orig_key);
5183 if (path->slots[0] > 0) {
5188 * At slot 0, same key as before, it means orig_key is
5189 * the lowest, leftmost, key in the tree. We're done.
5195 btrfs_item_key(path->nodes[0], &found_key, 0);
5196 ret = comp_keys(&found_key, &key);
5198 * We might have had an item with the previous key in the tree right
5199 * before we released our path. And after we released our path, that
5200 * item might have been pushed to the first slot (0) of the leaf we
5201 * were holding due to a tree balance. Alternatively, an item with the
5202 * previous key can exist as the only element of a leaf (big fat item).
5203 * Therefore account for these 2 cases, so that our callers (like
5204 * btrfs_previous_item) don't miss an existing item with a key matching
5205 * the previous key we computed above.
5213 * A helper function to walk down the tree starting at min_key, and looking
5214 * for nodes or leaves that are have a minimum transaction id.
5215 * This is used by the btree defrag code, and tree logging
5217 * This does not cow, but it does stuff the starting key it finds back
5218 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5219 * key and get a writable path.
5221 * This honors path->lowest_level to prevent descent past a given level
5224 * min_trans indicates the oldest transaction that you are interested
5225 * in walking through. Any nodes or leaves older than min_trans are
5226 * skipped over (without reading them).
5228 * returns zero if something useful was found, < 0 on error and 1 if there
5229 * was nothing in the tree that matched the search criteria.
5231 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
5232 struct btrfs_path *path,
5235 struct extent_buffer *cur;
5236 struct btrfs_key found_key;
5242 int keep_locks = path->keep_locks;
5244 path->keep_locks = 1;
5246 cur = btrfs_read_lock_root_node(root);
5247 level = btrfs_header_level(cur);
5248 WARN_ON(path->nodes[level]);
5249 path->nodes[level] = cur;
5250 path->locks[level] = BTRFS_READ_LOCK;
5252 if (btrfs_header_generation(cur) < min_trans) {
5257 nritems = btrfs_header_nritems(cur);
5258 level = btrfs_header_level(cur);
5259 sret = btrfs_bin_search(cur, min_key, level, &slot);
5265 /* at the lowest level, we're done, setup the path and exit */
5266 if (level == path->lowest_level) {
5267 if (slot >= nritems)
5270 path->slots[level] = slot;
5271 btrfs_item_key_to_cpu(cur, &found_key, slot);
5274 if (sret && slot > 0)
5277 * check this node pointer against the min_trans parameters.
5278 * If it is too old, skip to the next one.
5280 while (slot < nritems) {
5283 gen = btrfs_node_ptr_generation(cur, slot);
5284 if (gen < min_trans) {
5292 * we didn't find a candidate key in this node, walk forward
5293 * and find another one
5295 if (slot >= nritems) {
5296 path->slots[level] = slot;
5297 btrfs_set_path_blocking(path);
5298 sret = btrfs_find_next_key(root, path, min_key, level,
5301 btrfs_release_path(path);
5307 /* save our key for returning back */
5308 btrfs_node_key_to_cpu(cur, &found_key, slot);
5309 path->slots[level] = slot;
5310 if (level == path->lowest_level) {
5314 btrfs_set_path_blocking(path);
5315 cur = btrfs_read_node_slot(cur, slot);
5321 btrfs_tree_read_lock(cur);
5323 path->locks[level - 1] = BTRFS_READ_LOCK;
5324 path->nodes[level - 1] = cur;
5325 unlock_up(path, level, 1, 0, NULL);
5328 path->keep_locks = keep_locks;
5330 btrfs_unlock_up_safe(path, path->lowest_level + 1);
5331 btrfs_set_path_blocking(path);
5332 memcpy(min_key, &found_key, sizeof(found_key));
5338 * this is similar to btrfs_next_leaf, but does not try to preserve
5339 * and fixup the path. It looks for and returns the next key in the
5340 * tree based on the current path and the min_trans parameters.
5342 * 0 is returned if another key is found, < 0 if there are any errors
5343 * and 1 is returned if there are no higher keys in the tree
5345 * path->keep_locks should be set to 1 on the search made before
5346 * calling this function.
5348 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
5349 struct btrfs_key *key, int level, u64 min_trans)
5352 struct extent_buffer *c;
5354 WARN_ON(!path->keep_locks && !path->skip_locking);
5355 while (level < BTRFS_MAX_LEVEL) {
5356 if (!path->nodes[level])
5359 slot = path->slots[level] + 1;
5360 c = path->nodes[level];
5362 if (slot >= btrfs_header_nritems(c)) {
5365 struct btrfs_key cur_key;
5366 if (level + 1 >= BTRFS_MAX_LEVEL ||
5367 !path->nodes[level + 1])
5370 if (path->locks[level + 1] || path->skip_locking) {
5375 slot = btrfs_header_nritems(c) - 1;
5377 btrfs_item_key_to_cpu(c, &cur_key, slot);
5379 btrfs_node_key_to_cpu(c, &cur_key, slot);
5381 orig_lowest = path->lowest_level;
5382 btrfs_release_path(path);
5383 path->lowest_level = level;
5384 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5386 path->lowest_level = orig_lowest;
5390 c = path->nodes[level];
5391 slot = path->slots[level];
5398 btrfs_item_key_to_cpu(c, key, slot);
5400 u64 gen = btrfs_node_ptr_generation(c, slot);
5402 if (gen < min_trans) {
5406 btrfs_node_key_to_cpu(c, key, slot);
5414 * search the tree again to find a leaf with greater keys
5415 * returns 0 if it found something or 1 if there are no greater leaves.
5416 * returns < 0 on io errors.
5418 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
5420 return btrfs_next_old_leaf(root, path, 0);
5423 int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5428 struct extent_buffer *c;
5429 struct extent_buffer *next;
5430 struct btrfs_key key;
5433 int old_spinning = path->leave_spinning;
5434 int next_rw_lock = 0;
5436 nritems = btrfs_header_nritems(path->nodes[0]);
5440 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5445 btrfs_release_path(path);
5447 path->keep_locks = 1;
5448 path->leave_spinning = 1;
5451 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5453 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5454 path->keep_locks = 0;
5459 nritems = btrfs_header_nritems(path->nodes[0]);
5461 * by releasing the path above we dropped all our locks. A balance
5462 * could have added more items next to the key that used to be
5463 * at the very end of the block. So, check again here and
5464 * advance the path if there are now more items available.
5466 if (nritems > 0 && path->slots[0] < nritems - 1) {
5473 * So the above check misses one case:
5474 * - after releasing the path above, someone has removed the item that
5475 * used to be at the very end of the block, and balance between leafs
5476 * gets another one with bigger key.offset to replace it.
5478 * This one should be returned as well, or we can get leaf corruption
5479 * later(esp. in __btrfs_drop_extents()).
5481 * And a bit more explanation about this check,
5482 * with ret > 0, the key isn't found, the path points to the slot
5483 * where it should be inserted, so the path->slots[0] item must be the
5486 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
5491 while (level < BTRFS_MAX_LEVEL) {
5492 if (!path->nodes[level]) {
5497 slot = path->slots[level] + 1;
5498 c = path->nodes[level];
5499 if (slot >= btrfs_header_nritems(c)) {
5501 if (level == BTRFS_MAX_LEVEL) {
5509 btrfs_tree_unlock_rw(next, next_rw_lock);
5510 free_extent_buffer(next);
5514 next_rw_lock = path->locks[level];
5515 ret = read_block_for_search(root, path, &next, level,
5521 btrfs_release_path(path);
5525 if (!path->skip_locking) {
5526 ret = btrfs_try_tree_read_lock(next);
5527 if (!ret && time_seq) {
5529 * If we don't get the lock, we may be racing
5530 * with push_leaf_left, holding that lock while
5531 * itself waiting for the leaf we've currently
5532 * locked. To solve this situation, we give up
5533 * on our lock and cycle.
5535 free_extent_buffer(next);
5536 btrfs_release_path(path);
5541 btrfs_set_path_blocking(path);
5542 btrfs_tree_read_lock(next);
5544 next_rw_lock = BTRFS_READ_LOCK;
5548 path->slots[level] = slot;
5551 c = path->nodes[level];
5552 if (path->locks[level])
5553 btrfs_tree_unlock_rw(c, path->locks[level]);
5555 free_extent_buffer(c);
5556 path->nodes[level] = next;
5557 path->slots[level] = 0;
5558 if (!path->skip_locking)
5559 path->locks[level] = next_rw_lock;
5563 ret = read_block_for_search(root, path, &next, level,
5569 btrfs_release_path(path);
5573 if (!path->skip_locking) {
5574 ret = btrfs_try_tree_read_lock(next);
5576 btrfs_set_path_blocking(path);
5577 btrfs_tree_read_lock(next);
5579 next_rw_lock = BTRFS_READ_LOCK;
5584 unlock_up(path, 0, 1, 0, NULL);
5585 path->leave_spinning = old_spinning;
5587 btrfs_set_path_blocking(path);
5593 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5594 * searching until it gets past min_objectid or finds an item of 'type'
5596 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5598 int btrfs_previous_item(struct btrfs_root *root,
5599 struct btrfs_path *path, u64 min_objectid,
5602 struct btrfs_key found_key;
5603 struct extent_buffer *leaf;
5608 if (path->slots[0] == 0) {
5609 btrfs_set_path_blocking(path);
5610 ret = btrfs_prev_leaf(root, path);
5616 leaf = path->nodes[0];
5617 nritems = btrfs_header_nritems(leaf);
5620 if (path->slots[0] == nritems)
5623 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5624 if (found_key.objectid < min_objectid)
5626 if (found_key.type == type)
5628 if (found_key.objectid == min_objectid &&
5629 found_key.type < type)
5636 * search in extent tree to find a previous Metadata/Data extent item with
5639 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5641 int btrfs_previous_extent_item(struct btrfs_root *root,
5642 struct btrfs_path *path, u64 min_objectid)
5644 struct btrfs_key found_key;
5645 struct extent_buffer *leaf;
5650 if (path->slots[0] == 0) {
5651 btrfs_set_path_blocking(path);
5652 ret = btrfs_prev_leaf(root, path);
5658 leaf = path->nodes[0];
5659 nritems = btrfs_header_nritems(leaf);
5662 if (path->slots[0] == nritems)
5665 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5666 if (found_key.objectid < min_objectid)
5668 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5669 found_key.type == BTRFS_METADATA_ITEM_KEY)
5671 if (found_key.objectid == min_objectid &&
5672 found_key.type < BTRFS_EXTENT_ITEM_KEY)