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 btrfs_fs_info *fs_info,
25 struct extent_buffer *dst,
26 struct extent_buffer *src, int empty);
27 static int balance_node_right(struct btrfs_trans_handle *trans,
28 struct btrfs_fs_info *fs_info,
29 struct extent_buffer *dst_buf,
30 struct extent_buffer *src_buf);
31 static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
34 struct btrfs_path *btrfs_alloc_path(void)
36 return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
40 * set all locked nodes in the path to blocking locks. This should
41 * be done before scheduling
43 noinline void btrfs_set_path_blocking(struct btrfs_path *p)
46 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
47 if (!p->nodes[i] || !p->locks[i])
49 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
50 if (p->locks[i] == BTRFS_READ_LOCK)
51 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
52 else if (p->locks[i] == BTRFS_WRITE_LOCK)
53 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
57 /* this also releases the path */
58 void btrfs_free_path(struct btrfs_path *p)
62 btrfs_release_path(p);
63 kmem_cache_free(btrfs_path_cachep, p);
67 * path release drops references on the extent buffers in the path
68 * and it drops any locks held by this path
70 * It is safe to call this on paths that no locks or extent buffers held.
72 noinline void btrfs_release_path(struct btrfs_path *p)
76 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
81 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
84 free_extent_buffer(p->nodes[i]);
90 * safely gets a reference on the root node of a tree. A lock
91 * is not taken, so a concurrent writer may put a different node
92 * at the root of the tree. See btrfs_lock_root_node for the
95 * The extent buffer returned by this has a reference taken, so
96 * it won't disappear. It may stop being the root of the tree
97 * at any time because there are no locks held.
99 struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
101 struct extent_buffer *eb;
105 eb = rcu_dereference(root->node);
108 * RCU really hurts here, we could free up the root node because
109 * it was COWed but we may not get the new root node yet so do
110 * the inc_not_zero dance and if it doesn't work then
111 * synchronize_rcu and try again.
113 if (atomic_inc_not_zero(&eb->refs)) {
123 /* loop around taking references on and locking the root node of the
124 * tree until you end up with a lock on the root. A locked buffer
125 * is returned, with a reference held.
127 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
129 struct extent_buffer *eb;
132 eb = btrfs_root_node(root);
134 if (eb == root->node)
136 btrfs_tree_unlock(eb);
137 free_extent_buffer(eb);
142 /* loop around taking references on and locking the root node of the
143 * tree until you end up with a lock on the root. A locked buffer
144 * is returned, with a reference held.
146 struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
148 struct extent_buffer *eb;
151 eb = btrfs_root_node(root);
152 btrfs_tree_read_lock(eb);
153 if (eb == root->node)
155 btrfs_tree_read_unlock(eb);
156 free_extent_buffer(eb);
161 /* cowonly root (everything not a reference counted cow subvolume), just get
162 * put onto a simple dirty list. transaction.c walks this to make sure they
163 * get properly updated on disk.
165 static void add_root_to_dirty_list(struct btrfs_root *root)
167 struct btrfs_fs_info *fs_info = root->fs_info;
169 if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
170 !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
173 spin_lock(&fs_info->trans_lock);
174 if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
175 /* Want the extent tree to be the last on the list */
176 if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID)
177 list_move_tail(&root->dirty_list,
178 &fs_info->dirty_cowonly_roots);
180 list_move(&root->dirty_list,
181 &fs_info->dirty_cowonly_roots);
183 spin_unlock(&fs_info->trans_lock);
187 * used by snapshot creation to make a copy of a root for a tree with
188 * a given objectid. The buffer with the new root node is returned in
189 * cow_ret, and this func returns zero on success or a negative error code.
191 int btrfs_copy_root(struct btrfs_trans_handle *trans,
192 struct btrfs_root *root,
193 struct extent_buffer *buf,
194 struct extent_buffer **cow_ret, u64 new_root_objectid)
196 struct btrfs_fs_info *fs_info = root->fs_info;
197 struct extent_buffer *cow;
200 struct btrfs_disk_key disk_key;
202 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
203 trans->transid != fs_info->running_transaction->transid);
204 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
205 trans->transid != root->last_trans);
207 level = btrfs_header_level(buf);
209 btrfs_item_key(buf, &disk_key, 0);
211 btrfs_node_key(buf, &disk_key, 0);
213 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
214 &disk_key, level, buf->start, 0);
218 copy_extent_buffer_full(cow, buf);
219 btrfs_set_header_bytenr(cow, cow->start);
220 btrfs_set_header_generation(cow, trans->transid);
221 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
222 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
223 BTRFS_HEADER_FLAG_RELOC);
224 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
225 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
227 btrfs_set_header_owner(cow, new_root_objectid);
229 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
231 WARN_ON(btrfs_header_generation(buf) > trans->transid);
232 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
233 ret = btrfs_inc_ref(trans, root, cow, 1);
235 ret = btrfs_inc_ref(trans, root, cow, 0);
240 btrfs_mark_buffer_dirty(cow);
249 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
250 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
252 MOD_LOG_ROOT_REPLACE,
255 struct tree_mod_root {
260 struct tree_mod_elem {
266 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
269 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
272 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
273 struct btrfs_disk_key key;
276 /* this is used for op == MOD_LOG_MOVE_KEYS */
282 /* this is used for op == MOD_LOG_ROOT_REPLACE */
283 struct tree_mod_root old_root;
287 * Pull a new tree mod seq number for our operation.
289 static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
291 return atomic64_inc_return(&fs_info->tree_mod_seq);
295 * This adds a new blocker to the tree mod log's blocker list if the @elem
296 * passed does not already have a sequence number set. So when a caller expects
297 * to record tree modifications, it should ensure to set elem->seq to zero
298 * before calling btrfs_get_tree_mod_seq.
299 * Returns a fresh, unused tree log modification sequence number, even if no new
302 u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
303 struct seq_list *elem)
305 write_lock(&fs_info->tree_mod_log_lock);
306 spin_lock(&fs_info->tree_mod_seq_lock);
308 elem->seq = btrfs_inc_tree_mod_seq(fs_info);
309 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
311 spin_unlock(&fs_info->tree_mod_seq_lock);
312 write_unlock(&fs_info->tree_mod_log_lock);
317 void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
318 struct seq_list *elem)
320 struct rb_root *tm_root;
321 struct rb_node *node;
322 struct rb_node *next;
323 struct seq_list *cur_elem;
324 struct tree_mod_elem *tm;
325 u64 min_seq = (u64)-1;
326 u64 seq_putting = elem->seq;
331 spin_lock(&fs_info->tree_mod_seq_lock);
332 list_del(&elem->list);
335 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
336 if (cur_elem->seq < min_seq) {
337 if (seq_putting > cur_elem->seq) {
339 * blocker with lower sequence number exists, we
340 * cannot remove anything from the log
342 spin_unlock(&fs_info->tree_mod_seq_lock);
345 min_seq = cur_elem->seq;
348 spin_unlock(&fs_info->tree_mod_seq_lock);
351 * anything that's lower than the lowest existing (read: blocked)
352 * sequence number can be removed from the tree.
354 write_lock(&fs_info->tree_mod_log_lock);
355 tm_root = &fs_info->tree_mod_log;
356 for (node = rb_first(tm_root); node; node = next) {
357 next = rb_next(node);
358 tm = rb_entry(node, struct tree_mod_elem, node);
359 if (tm->seq > min_seq)
361 rb_erase(node, tm_root);
364 write_unlock(&fs_info->tree_mod_log_lock);
368 * key order of the log:
369 * node/leaf start address -> sequence
371 * The 'start address' is the logical address of the *new* root node
372 * for root replace operations, or the logical address of the affected
373 * block for all other operations.
375 * Note: must be called with write lock for fs_info::tree_mod_log_lock.
378 __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
380 struct rb_root *tm_root;
381 struct rb_node **new;
382 struct rb_node *parent = NULL;
383 struct tree_mod_elem *cur;
385 tm->seq = btrfs_inc_tree_mod_seq(fs_info);
387 tm_root = &fs_info->tree_mod_log;
388 new = &tm_root->rb_node;
390 cur = rb_entry(*new, struct tree_mod_elem, node);
392 if (cur->logical < tm->logical)
393 new = &((*new)->rb_left);
394 else if (cur->logical > tm->logical)
395 new = &((*new)->rb_right);
396 else if (cur->seq < tm->seq)
397 new = &((*new)->rb_left);
398 else if (cur->seq > tm->seq)
399 new = &((*new)->rb_right);
404 rb_link_node(&tm->node, parent, new);
405 rb_insert_color(&tm->node, tm_root);
410 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
411 * returns zero with the tree_mod_log_lock acquired. The caller must hold
412 * this until all tree mod log insertions are recorded in the rb tree and then
413 * write unlock fs_info::tree_mod_log_lock.
415 static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
416 struct extent_buffer *eb) {
418 if (list_empty(&(fs_info)->tree_mod_seq_list))
420 if (eb && btrfs_header_level(eb) == 0)
423 write_lock(&fs_info->tree_mod_log_lock);
424 if (list_empty(&(fs_info)->tree_mod_seq_list)) {
425 write_unlock(&fs_info->tree_mod_log_lock);
432 /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
433 static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
434 struct extent_buffer *eb)
437 if (list_empty(&(fs_info)->tree_mod_seq_list))
439 if (eb && btrfs_header_level(eb) == 0)
445 static struct tree_mod_elem *
446 alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
447 enum mod_log_op op, gfp_t flags)
449 struct tree_mod_elem *tm;
451 tm = kzalloc(sizeof(*tm), flags);
455 tm->logical = eb->start;
456 if (op != MOD_LOG_KEY_ADD) {
457 btrfs_node_key(eb, &tm->key, slot);
458 tm->blockptr = btrfs_node_blockptr(eb, slot);
462 tm->generation = btrfs_node_ptr_generation(eb, slot);
463 RB_CLEAR_NODE(&tm->node);
468 static noinline int tree_mod_log_insert_key(struct extent_buffer *eb, int slot,
469 enum mod_log_op op, gfp_t flags)
471 struct tree_mod_elem *tm;
474 if (!tree_mod_need_log(eb->fs_info, eb))
477 tm = alloc_tree_mod_elem(eb, slot, op, flags);
481 if (tree_mod_dont_log(eb->fs_info, eb)) {
486 ret = __tree_mod_log_insert(eb->fs_info, tm);
487 write_unlock(&eb->fs_info->tree_mod_log_lock);
494 static noinline int tree_mod_log_insert_move(struct extent_buffer *eb,
495 int dst_slot, int src_slot, int nr_items)
497 struct tree_mod_elem *tm = NULL;
498 struct tree_mod_elem **tm_list = NULL;
503 if (!tree_mod_need_log(eb->fs_info, eb))
506 tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS);
510 tm = kzalloc(sizeof(*tm), GFP_NOFS);
516 tm->logical = eb->start;
518 tm->move.dst_slot = dst_slot;
519 tm->move.nr_items = nr_items;
520 tm->op = MOD_LOG_MOVE_KEYS;
522 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
523 tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
524 MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
531 if (tree_mod_dont_log(eb->fs_info, eb))
536 * When we override something during the move, we log these removals.
537 * This can only happen when we move towards the beginning of the
538 * buffer, i.e. dst_slot < src_slot.
540 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
541 ret = __tree_mod_log_insert(eb->fs_info, tm_list[i]);
546 ret = __tree_mod_log_insert(eb->fs_info, tm);
549 write_unlock(&eb->fs_info->tree_mod_log_lock);
554 for (i = 0; i < nr_items; i++) {
555 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
556 rb_erase(&tm_list[i]->node, &eb->fs_info->tree_mod_log);
560 write_unlock(&eb->fs_info->tree_mod_log_lock);
568 __tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
569 struct tree_mod_elem **tm_list,
575 for (i = nritems - 1; i >= 0; i--) {
576 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
578 for (j = nritems - 1; j > i; j--)
579 rb_erase(&tm_list[j]->node,
580 &fs_info->tree_mod_log);
588 static noinline int tree_mod_log_insert_root(struct extent_buffer *old_root,
589 struct extent_buffer *new_root, int log_removal)
591 struct btrfs_fs_info *fs_info = old_root->fs_info;
592 struct tree_mod_elem *tm = NULL;
593 struct tree_mod_elem **tm_list = NULL;
598 if (!tree_mod_need_log(fs_info, NULL))
601 if (log_removal && btrfs_header_level(old_root) > 0) {
602 nritems = btrfs_header_nritems(old_root);
603 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
609 for (i = 0; i < nritems; i++) {
610 tm_list[i] = alloc_tree_mod_elem(old_root, i,
611 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
619 tm = kzalloc(sizeof(*tm), GFP_NOFS);
625 tm->logical = new_root->start;
626 tm->old_root.logical = old_root->start;
627 tm->old_root.level = btrfs_header_level(old_root);
628 tm->generation = btrfs_header_generation(old_root);
629 tm->op = MOD_LOG_ROOT_REPLACE;
631 if (tree_mod_dont_log(fs_info, NULL))
635 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
637 ret = __tree_mod_log_insert(fs_info, tm);
639 write_unlock(&fs_info->tree_mod_log_lock);
648 for (i = 0; i < nritems; i++)
657 static struct tree_mod_elem *
658 __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
661 struct rb_root *tm_root;
662 struct rb_node *node;
663 struct tree_mod_elem *cur = NULL;
664 struct tree_mod_elem *found = NULL;
666 read_lock(&fs_info->tree_mod_log_lock);
667 tm_root = &fs_info->tree_mod_log;
668 node = tm_root->rb_node;
670 cur = rb_entry(node, struct tree_mod_elem, node);
671 if (cur->logical < start) {
672 node = node->rb_left;
673 } else if (cur->logical > start) {
674 node = node->rb_right;
675 } else if (cur->seq < min_seq) {
676 node = node->rb_left;
677 } else if (!smallest) {
678 /* we want the node with the highest seq */
680 BUG_ON(found->seq > cur->seq);
682 node = node->rb_left;
683 } else if (cur->seq > min_seq) {
684 /* we want the node with the smallest seq */
686 BUG_ON(found->seq < cur->seq);
688 node = node->rb_right;
694 read_unlock(&fs_info->tree_mod_log_lock);
700 * this returns the element from the log with the smallest time sequence
701 * value that's in the log (the oldest log item). any element with a time
702 * sequence lower than min_seq will be ignored.
704 static struct tree_mod_elem *
705 tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
708 return __tree_mod_log_search(fs_info, start, min_seq, 1);
712 * this returns the element from the log with the largest time sequence
713 * value that's in the log (the most recent log item). any element with
714 * a time sequence lower than min_seq will be ignored.
716 static struct tree_mod_elem *
717 tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
719 return __tree_mod_log_search(fs_info, start, min_seq, 0);
723 tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
724 struct extent_buffer *src, unsigned long dst_offset,
725 unsigned long src_offset, int nr_items)
728 struct tree_mod_elem **tm_list = NULL;
729 struct tree_mod_elem **tm_list_add, **tm_list_rem;
733 if (!tree_mod_need_log(fs_info, NULL))
736 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
739 tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
744 tm_list_add = tm_list;
745 tm_list_rem = tm_list + nr_items;
746 for (i = 0; i < nr_items; i++) {
747 tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
748 MOD_LOG_KEY_REMOVE, GFP_NOFS);
749 if (!tm_list_rem[i]) {
754 tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
755 MOD_LOG_KEY_ADD, GFP_NOFS);
756 if (!tm_list_add[i]) {
762 if (tree_mod_dont_log(fs_info, NULL))
766 for (i = 0; i < nr_items; i++) {
767 ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
770 ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
775 write_unlock(&fs_info->tree_mod_log_lock);
781 for (i = 0; i < nr_items * 2; i++) {
782 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
783 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
787 write_unlock(&fs_info->tree_mod_log_lock);
793 static noinline int tree_mod_log_free_eb(struct extent_buffer *eb)
795 struct tree_mod_elem **tm_list = NULL;
800 if (btrfs_header_level(eb) == 0)
803 if (!tree_mod_need_log(eb->fs_info, NULL))
806 nritems = btrfs_header_nritems(eb);
807 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
811 for (i = 0; i < nritems; i++) {
812 tm_list[i] = alloc_tree_mod_elem(eb, i,
813 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
820 if (tree_mod_dont_log(eb->fs_info, eb))
823 ret = __tree_mod_log_free_eb(eb->fs_info, tm_list, nritems);
824 write_unlock(&eb->fs_info->tree_mod_log_lock);
832 for (i = 0; i < nritems; i++)
840 * check if the tree block can be shared by multiple trees
842 int btrfs_block_can_be_shared(struct btrfs_root *root,
843 struct extent_buffer *buf)
846 * Tree blocks not in reference counted trees and tree roots
847 * are never shared. If a block was allocated after the last
848 * snapshot and the block was not allocated by tree relocation,
849 * we know the block is not shared.
851 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
852 buf != root->node && buf != root->commit_root &&
853 (btrfs_header_generation(buf) <=
854 btrfs_root_last_snapshot(&root->root_item) ||
855 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
861 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
862 struct btrfs_root *root,
863 struct extent_buffer *buf,
864 struct extent_buffer *cow,
867 struct btrfs_fs_info *fs_info = root->fs_info;
875 * Backrefs update rules:
877 * Always use full backrefs for extent pointers in tree block
878 * allocated by tree relocation.
880 * If a shared tree block is no longer referenced by its owner
881 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
882 * use full backrefs for extent pointers in tree block.
884 * If a tree block is been relocating
885 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
886 * use full backrefs for extent pointers in tree block.
887 * The reason for this is some operations (such as drop tree)
888 * are only allowed for blocks use full backrefs.
891 if (btrfs_block_can_be_shared(root, buf)) {
892 ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
893 btrfs_header_level(buf), 1,
899 btrfs_handle_fs_error(fs_info, ret, NULL);
904 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
905 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
906 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
911 owner = btrfs_header_owner(buf);
912 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
913 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
916 if ((owner == root->root_key.objectid ||
917 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
918 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
919 ret = btrfs_inc_ref(trans, root, buf, 1);
923 if (root->root_key.objectid ==
924 BTRFS_TREE_RELOC_OBJECTID) {
925 ret = btrfs_dec_ref(trans, root, buf, 0);
928 ret = btrfs_inc_ref(trans, root, cow, 1);
932 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
935 if (root->root_key.objectid ==
936 BTRFS_TREE_RELOC_OBJECTID)
937 ret = btrfs_inc_ref(trans, root, cow, 1);
939 ret = btrfs_inc_ref(trans, root, cow, 0);
943 if (new_flags != 0) {
944 int level = btrfs_header_level(buf);
946 ret = btrfs_set_disk_extent_flags(trans, fs_info,
949 new_flags, level, 0);
954 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
955 if (root->root_key.objectid ==
956 BTRFS_TREE_RELOC_OBJECTID)
957 ret = btrfs_inc_ref(trans, root, cow, 1);
959 ret = btrfs_inc_ref(trans, root, cow, 0);
962 ret = btrfs_dec_ref(trans, root, buf, 1);
966 clean_tree_block(fs_info, buf);
972 static struct extent_buffer *alloc_tree_block_no_bg_flush(
973 struct btrfs_trans_handle *trans,
974 struct btrfs_root *root,
976 const struct btrfs_disk_key *disk_key,
981 struct btrfs_fs_info *fs_info = root->fs_info;
982 struct extent_buffer *ret;
985 * If we are COWing a node/leaf from the extent, chunk, device or free
986 * space trees, make sure that we do not finish block group creation of
987 * pending block groups. We do this to avoid a deadlock.
988 * COWing can result in allocation of a new chunk, and flushing pending
989 * block groups (btrfs_create_pending_block_groups()) can be triggered
990 * when finishing allocation of a new chunk. Creation of a pending block
991 * group modifies the extent, chunk, device and free space trees,
992 * therefore we could deadlock with ourselves since we are holding a
993 * lock on an extent buffer that btrfs_create_pending_block_groups() may
995 * For similar reasons, we also need to delay flushing pending block
996 * groups when splitting a leaf or node, from one of those trees, since
997 * we are holding a write lock on it and its parent or when inserting a
998 * new root node for one of those trees.
1000 if (root == fs_info->extent_root ||
1001 root == fs_info->chunk_root ||
1002 root == fs_info->dev_root ||
1003 root == fs_info->free_space_root)
1004 trans->can_flush_pending_bgs = false;
1006 ret = btrfs_alloc_tree_block(trans, root, parent_start,
1007 root->root_key.objectid, disk_key, level,
1009 trans->can_flush_pending_bgs = true;
1015 * does the dirty work in cow of a single block. The parent block (if
1016 * supplied) is updated to point to the new cow copy. The new buffer is marked
1017 * dirty and returned locked. If you modify the block it needs to be marked
1020 * search_start -- an allocation hint for the new block
1022 * empty_size -- a hint that you plan on doing more cow. This is the size in
1023 * bytes the allocator should try to find free next to the block it returns.
1024 * This is just a hint and may be ignored by the allocator.
1026 static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
1027 struct btrfs_root *root,
1028 struct extent_buffer *buf,
1029 struct extent_buffer *parent, int parent_slot,
1030 struct extent_buffer **cow_ret,
1031 u64 search_start, u64 empty_size)
1033 struct btrfs_fs_info *fs_info = root->fs_info;
1034 struct btrfs_disk_key disk_key;
1035 struct extent_buffer *cow;
1038 int unlock_orig = 0;
1039 u64 parent_start = 0;
1041 if (*cow_ret == buf)
1044 btrfs_assert_tree_locked(buf);
1046 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1047 trans->transid != fs_info->running_transaction->transid);
1048 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1049 trans->transid != root->last_trans);
1051 level = btrfs_header_level(buf);
1054 btrfs_item_key(buf, &disk_key, 0);
1056 btrfs_node_key(buf, &disk_key, 0);
1058 if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
1059 parent_start = parent->start;
1061 cow = alloc_tree_block_no_bg_flush(trans, root, parent_start, &disk_key,
1062 level, search_start, empty_size);
1064 return PTR_ERR(cow);
1066 /* cow is set to blocking by btrfs_init_new_buffer */
1068 copy_extent_buffer_full(cow, buf);
1069 btrfs_set_header_bytenr(cow, cow->start);
1070 btrfs_set_header_generation(cow, trans->transid);
1071 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1072 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1073 BTRFS_HEADER_FLAG_RELOC);
1074 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1075 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1077 btrfs_set_header_owner(cow, root->root_key.objectid);
1079 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
1081 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
1083 btrfs_abort_transaction(trans, ret);
1087 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
1088 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
1090 btrfs_abort_transaction(trans, ret);
1095 if (buf == root->node) {
1096 WARN_ON(parent && parent != buf);
1097 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1098 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1099 parent_start = buf->start;
1101 extent_buffer_get(cow);
1102 ret = tree_mod_log_insert_root(root->node, cow, 1);
1104 rcu_assign_pointer(root->node, cow);
1106 btrfs_free_tree_block(trans, root, buf, parent_start,
1108 free_extent_buffer(buf);
1109 add_root_to_dirty_list(root);
1111 WARN_ON(trans->transid != btrfs_header_generation(parent));
1112 tree_mod_log_insert_key(parent, parent_slot,
1113 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1114 btrfs_set_node_blockptr(parent, parent_slot,
1116 btrfs_set_node_ptr_generation(parent, parent_slot,
1118 btrfs_mark_buffer_dirty(parent);
1120 ret = tree_mod_log_free_eb(buf);
1122 btrfs_abort_transaction(trans, ret);
1126 btrfs_free_tree_block(trans, root, buf, parent_start,
1130 btrfs_tree_unlock(buf);
1131 free_extent_buffer_stale(buf);
1132 btrfs_mark_buffer_dirty(cow);
1138 * returns the logical address of the oldest predecessor of the given root.
1139 * entries older than time_seq are ignored.
1141 static struct tree_mod_elem *__tree_mod_log_oldest_root(
1142 struct extent_buffer *eb_root, u64 time_seq)
1144 struct tree_mod_elem *tm;
1145 struct tree_mod_elem *found = NULL;
1146 u64 root_logical = eb_root->start;
1153 * the very last operation that's logged for a root is the
1154 * replacement operation (if it is replaced at all). this has
1155 * the logical address of the *new* root, making it the very
1156 * first operation that's logged for this root.
1159 tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical,
1164 * if there are no tree operation for the oldest root, we simply
1165 * return it. this should only happen if that (old) root is at
1172 * if there's an operation that's not a root replacement, we
1173 * found the oldest version of our root. normally, we'll find a
1174 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1176 if (tm->op != MOD_LOG_ROOT_REPLACE)
1180 root_logical = tm->old_root.logical;
1184 /* if there's no old root to return, return what we found instead */
1192 * tm is a pointer to the first operation to rewind within eb. then, all
1193 * previous operations will be rewound (until we reach something older than
1197 __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1198 u64 time_seq, struct tree_mod_elem *first_tm)
1201 struct rb_node *next;
1202 struct tree_mod_elem *tm = first_tm;
1203 unsigned long o_dst;
1204 unsigned long o_src;
1205 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1207 n = btrfs_header_nritems(eb);
1208 read_lock(&fs_info->tree_mod_log_lock);
1209 while (tm && tm->seq >= time_seq) {
1211 * all the operations are recorded with the operator used for
1212 * the modification. as we're going backwards, we do the
1213 * opposite of each operation here.
1216 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1217 BUG_ON(tm->slot < n);
1219 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
1220 case MOD_LOG_KEY_REMOVE:
1221 btrfs_set_node_key(eb, &tm->key, tm->slot);
1222 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1223 btrfs_set_node_ptr_generation(eb, tm->slot,
1227 case MOD_LOG_KEY_REPLACE:
1228 BUG_ON(tm->slot >= n);
1229 btrfs_set_node_key(eb, &tm->key, tm->slot);
1230 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1231 btrfs_set_node_ptr_generation(eb, tm->slot,
1234 case MOD_LOG_KEY_ADD:
1235 /* if a move operation is needed it's in the log */
1238 case MOD_LOG_MOVE_KEYS:
1239 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1240 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1241 memmove_extent_buffer(eb, o_dst, o_src,
1242 tm->move.nr_items * p_size);
1244 case MOD_LOG_ROOT_REPLACE:
1246 * this operation is special. for roots, this must be
1247 * handled explicitly before rewinding.
1248 * for non-roots, this operation may exist if the node
1249 * was a root: root A -> child B; then A gets empty and
1250 * B is promoted to the new root. in the mod log, we'll
1251 * have a root-replace operation for B, a tree block
1252 * that is no root. we simply ignore that operation.
1256 next = rb_next(&tm->node);
1259 tm = rb_entry(next, struct tree_mod_elem, node);
1260 if (tm->logical != first_tm->logical)
1263 read_unlock(&fs_info->tree_mod_log_lock);
1264 btrfs_set_header_nritems(eb, n);
1268 * Called with eb read locked. If the buffer cannot be rewound, the same buffer
1269 * is returned. If rewind operations happen, a fresh buffer is returned. The
1270 * returned buffer is always read-locked. If the returned buffer is not the
1271 * input buffer, the lock on the input buffer is released and the input buffer
1272 * is freed (its refcount is decremented).
1274 static struct extent_buffer *
1275 tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1276 struct extent_buffer *eb, u64 time_seq)
1278 struct extent_buffer *eb_rewin;
1279 struct tree_mod_elem *tm;
1284 if (btrfs_header_level(eb) == 0)
1287 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1291 btrfs_set_path_blocking(path);
1292 btrfs_set_lock_blocking_read(eb);
1294 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1295 BUG_ON(tm->slot != 0);
1296 eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
1298 btrfs_tree_read_unlock_blocking(eb);
1299 free_extent_buffer(eb);
1302 btrfs_set_header_bytenr(eb_rewin, eb->start);
1303 btrfs_set_header_backref_rev(eb_rewin,
1304 btrfs_header_backref_rev(eb));
1305 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
1306 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
1308 eb_rewin = btrfs_clone_extent_buffer(eb);
1310 btrfs_tree_read_unlock_blocking(eb);
1311 free_extent_buffer(eb);
1316 btrfs_tree_read_unlock_blocking(eb);
1317 free_extent_buffer(eb);
1319 btrfs_tree_read_lock(eb_rewin);
1320 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
1321 WARN_ON(btrfs_header_nritems(eb_rewin) >
1322 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
1328 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1329 * value. If there are no changes, the current root->root_node is returned. If
1330 * anything changed in between, there's a fresh buffer allocated on which the
1331 * rewind operations are done. In any case, the returned buffer is read locked.
1332 * Returns NULL on error (with no locks held).
1334 static inline struct extent_buffer *
1335 get_old_root(struct btrfs_root *root, u64 time_seq)
1337 struct btrfs_fs_info *fs_info = root->fs_info;
1338 struct tree_mod_elem *tm;
1339 struct extent_buffer *eb = NULL;
1340 struct extent_buffer *eb_root;
1341 struct extent_buffer *old;
1342 struct tree_mod_root *old_root = NULL;
1343 u64 old_generation = 0;
1347 eb_root = btrfs_read_lock_root_node(root);
1348 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
1352 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1353 old_root = &tm->old_root;
1354 old_generation = tm->generation;
1355 logical = old_root->logical;
1356 level = old_root->level;
1358 logical = eb_root->start;
1359 level = btrfs_header_level(eb_root);
1362 tm = tree_mod_log_search(fs_info, logical, time_seq);
1363 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1364 btrfs_tree_read_unlock(eb_root);
1365 free_extent_buffer(eb_root);
1366 old = read_tree_block(fs_info, logical, 0, level, NULL);
1367 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
1369 free_extent_buffer(old);
1371 "failed to read tree block %llu from get_old_root",
1374 eb = btrfs_clone_extent_buffer(old);
1375 free_extent_buffer(old);
1377 } else if (old_root) {
1378 btrfs_tree_read_unlock(eb_root);
1379 free_extent_buffer(eb_root);
1380 eb = alloc_dummy_extent_buffer(fs_info, logical);
1382 btrfs_set_lock_blocking_read(eb_root);
1383 eb = btrfs_clone_extent_buffer(eb_root);
1384 btrfs_tree_read_unlock_blocking(eb_root);
1385 free_extent_buffer(eb_root);
1390 btrfs_tree_read_lock(eb);
1392 btrfs_set_header_bytenr(eb, eb->start);
1393 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
1394 btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
1395 btrfs_set_header_level(eb, old_root->level);
1396 btrfs_set_header_generation(eb, old_generation);
1399 __tree_mod_log_rewind(fs_info, eb, time_seq, tm);
1401 WARN_ON(btrfs_header_level(eb) != 0);
1402 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
1407 int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1409 struct tree_mod_elem *tm;
1411 struct extent_buffer *eb_root = btrfs_root_node(root);
1413 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
1414 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1415 level = tm->old_root.level;
1417 level = btrfs_header_level(eb_root);
1419 free_extent_buffer(eb_root);
1424 static inline int should_cow_block(struct btrfs_trans_handle *trans,
1425 struct btrfs_root *root,
1426 struct extent_buffer *buf)
1428 if (btrfs_is_testing(root->fs_info))
1431 /* Ensure we can see the FORCE_COW bit */
1432 smp_mb__before_atomic();
1435 * We do not need to cow a block if
1436 * 1) this block is not created or changed in this transaction;
1437 * 2) this block does not belong to TREE_RELOC tree;
1438 * 3) the root is not forced COW.
1440 * What is forced COW:
1441 * when we create snapshot during committing the transaction,
1442 * after we've finished copying src root, we must COW the shared
1443 * block to ensure the metadata consistency.
1445 if (btrfs_header_generation(buf) == trans->transid &&
1446 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1447 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
1448 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1449 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
1455 * cows a single block, see __btrfs_cow_block for the real work.
1456 * This version of it has extra checks so that a block isn't COWed more than
1457 * once per transaction, as long as it hasn't been written yet
1459 noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
1460 struct btrfs_root *root, struct extent_buffer *buf,
1461 struct extent_buffer *parent, int parent_slot,
1462 struct extent_buffer **cow_ret)
1464 struct btrfs_fs_info *fs_info = root->fs_info;
1468 if (test_bit(BTRFS_ROOT_DELETING, &root->state))
1470 "COW'ing blocks on a fs root that's being dropped");
1472 if (trans->transaction != fs_info->running_transaction)
1473 WARN(1, KERN_CRIT "trans %llu running %llu\n",
1475 fs_info->running_transaction->transid);
1477 if (trans->transid != fs_info->generation)
1478 WARN(1, KERN_CRIT "trans %llu running %llu\n",
1479 trans->transid, fs_info->generation);
1481 if (!should_cow_block(trans, root, buf)) {
1482 trans->dirty = true;
1487 search_start = buf->start & ~((u64)SZ_1G - 1);
1490 btrfs_set_lock_blocking(parent);
1491 btrfs_set_lock_blocking(buf);
1494 * Before CoWing this block for later modification, check if it's
1495 * the subtree root and do the delayed subtree trace if needed.
1497 * Also We don't care about the error, as it's handled internally.
1499 btrfs_qgroup_trace_subtree_after_cow(trans, root, buf);
1500 ret = __btrfs_cow_block(trans, root, buf, parent,
1501 parent_slot, cow_ret, search_start, 0);
1503 trace_btrfs_cow_block(root, buf, *cow_ret);
1509 * helper function for defrag to decide if two blocks pointed to by a
1510 * node are actually close by
1512 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
1514 if (blocknr < other && other - (blocknr + blocksize) < 32768)
1516 if (blocknr > other && blocknr - (other + blocksize) < 32768)
1522 * compare two keys in a memcmp fashion
1524 static int comp_keys(const struct btrfs_disk_key *disk,
1525 const struct btrfs_key *k2)
1527 struct btrfs_key k1;
1529 btrfs_disk_key_to_cpu(&k1, disk);
1531 return btrfs_comp_cpu_keys(&k1, k2);
1535 * same as comp_keys only with two btrfs_key's
1537 int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
1539 if (k1->objectid > k2->objectid)
1541 if (k1->objectid < k2->objectid)
1543 if (k1->type > k2->type)
1545 if (k1->type < k2->type)
1547 if (k1->offset > k2->offset)
1549 if (k1->offset < k2->offset)
1555 * this is used by the defrag code to go through all the
1556 * leaves pointed to by a node and reallocate them so that
1557 * disk order is close to key order
1559 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
1560 struct btrfs_root *root, struct extent_buffer *parent,
1561 int start_slot, u64 *last_ret,
1562 struct btrfs_key *progress)
1564 struct btrfs_fs_info *fs_info = root->fs_info;
1565 struct extent_buffer *cur;
1568 u64 search_start = *last_ret;
1578 int progress_passed = 0;
1579 struct btrfs_disk_key disk_key;
1581 parent_level = btrfs_header_level(parent);
1583 WARN_ON(trans->transaction != fs_info->running_transaction);
1584 WARN_ON(trans->transid != fs_info->generation);
1586 parent_nritems = btrfs_header_nritems(parent);
1587 blocksize = fs_info->nodesize;
1588 end_slot = parent_nritems - 1;
1590 if (parent_nritems <= 1)
1593 btrfs_set_lock_blocking(parent);
1595 for (i = start_slot; i <= end_slot; i++) {
1596 struct btrfs_key first_key;
1599 btrfs_node_key(parent, &disk_key, i);
1600 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1603 progress_passed = 1;
1604 blocknr = btrfs_node_blockptr(parent, i);
1605 gen = btrfs_node_ptr_generation(parent, i);
1606 btrfs_node_key_to_cpu(parent, &first_key, i);
1607 if (last_block == 0)
1608 last_block = blocknr;
1611 other = btrfs_node_blockptr(parent, i - 1);
1612 close = close_blocks(blocknr, other, blocksize);
1614 if (!close && i < end_slot) {
1615 other = btrfs_node_blockptr(parent, i + 1);
1616 close = close_blocks(blocknr, other, blocksize);
1619 last_block = blocknr;
1623 cur = find_extent_buffer(fs_info, blocknr);
1625 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
1628 if (!cur || !uptodate) {
1630 cur = read_tree_block(fs_info, blocknr, gen,
1634 return PTR_ERR(cur);
1635 } else if (!extent_buffer_uptodate(cur)) {
1636 free_extent_buffer(cur);
1639 } else if (!uptodate) {
1640 err = btrfs_read_buffer(cur, gen,
1641 parent_level - 1,&first_key);
1643 free_extent_buffer(cur);
1648 if (search_start == 0)
1649 search_start = last_block;
1651 btrfs_tree_lock(cur);
1652 btrfs_set_lock_blocking(cur);
1653 err = __btrfs_cow_block(trans, root, cur, parent, i,
1656 (end_slot - i) * blocksize));
1658 btrfs_tree_unlock(cur);
1659 free_extent_buffer(cur);
1662 search_start = cur->start;
1663 last_block = cur->start;
1664 *last_ret = search_start;
1665 btrfs_tree_unlock(cur);
1666 free_extent_buffer(cur);
1672 * search for key in the extent_buffer. The items start at offset p,
1673 * and they are item_size apart. There are 'max' items in p.
1675 * the slot in the array is returned via slot, and it points to
1676 * the place where you would insert key if it is not found in
1679 * slot may point to max if the key is bigger than all of the keys
1681 static noinline int generic_bin_search(struct extent_buffer *eb,
1682 unsigned long p, int item_size,
1683 const struct btrfs_key *key,
1690 struct btrfs_disk_key *tmp = NULL;
1691 struct btrfs_disk_key unaligned;
1692 unsigned long offset;
1694 unsigned long map_start = 0;
1695 unsigned long map_len = 0;
1699 btrfs_err(eb->fs_info,
1700 "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
1701 __func__, low, high, eb->start,
1702 btrfs_header_owner(eb), btrfs_header_level(eb));
1706 while (low < high) {
1707 mid = (low + high) / 2;
1708 offset = p + mid * item_size;
1710 if (!kaddr || offset < map_start ||
1711 (offset + sizeof(struct btrfs_disk_key)) >
1712 map_start + map_len) {
1714 err = map_private_extent_buffer(eb, offset,
1715 sizeof(struct btrfs_disk_key),
1716 &kaddr, &map_start, &map_len);
1719 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1721 } else if (err == 1) {
1722 read_extent_buffer(eb, &unaligned,
1723 offset, sizeof(unaligned));
1730 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1733 ret = comp_keys(tmp, key);
1749 * simple bin_search frontend that does the right thing for
1752 int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
1753 int level, int *slot)
1756 return generic_bin_search(eb,
1757 offsetof(struct btrfs_leaf, items),
1758 sizeof(struct btrfs_item),
1759 key, btrfs_header_nritems(eb),
1762 return generic_bin_search(eb,
1763 offsetof(struct btrfs_node, ptrs),
1764 sizeof(struct btrfs_key_ptr),
1765 key, btrfs_header_nritems(eb),
1769 static void root_add_used(struct btrfs_root *root, u32 size)
1771 spin_lock(&root->accounting_lock);
1772 btrfs_set_root_used(&root->root_item,
1773 btrfs_root_used(&root->root_item) + size);
1774 spin_unlock(&root->accounting_lock);
1777 static void root_sub_used(struct btrfs_root *root, u32 size)
1779 spin_lock(&root->accounting_lock);
1780 btrfs_set_root_used(&root->root_item,
1781 btrfs_root_used(&root->root_item) - size);
1782 spin_unlock(&root->accounting_lock);
1785 /* given a node and slot number, this reads the blocks it points to. The
1786 * extent buffer is returned with a reference taken (but unlocked).
1788 static noinline struct extent_buffer *
1789 read_node_slot(struct btrfs_fs_info *fs_info, struct extent_buffer *parent,
1792 int level = btrfs_header_level(parent);
1793 struct extent_buffer *eb;
1794 struct btrfs_key first_key;
1796 if (slot < 0 || slot >= btrfs_header_nritems(parent))
1797 return ERR_PTR(-ENOENT);
1801 btrfs_node_key_to_cpu(parent, &first_key, slot);
1802 eb = read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
1803 btrfs_node_ptr_generation(parent, slot),
1804 level - 1, &first_key);
1805 if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
1806 free_extent_buffer(eb);
1814 * node level balancing, used to make sure nodes are in proper order for
1815 * item deletion. We balance from the top down, so we have to make sure
1816 * that a deletion won't leave an node completely empty later on.
1818 static noinline int balance_level(struct btrfs_trans_handle *trans,
1819 struct btrfs_root *root,
1820 struct btrfs_path *path, int level)
1822 struct btrfs_fs_info *fs_info = root->fs_info;
1823 struct extent_buffer *right = NULL;
1824 struct extent_buffer *mid;
1825 struct extent_buffer *left = NULL;
1826 struct extent_buffer *parent = NULL;
1830 int orig_slot = path->slots[level];
1835 mid = path->nodes[level];
1837 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1838 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
1839 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1841 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1843 if (level < BTRFS_MAX_LEVEL - 1) {
1844 parent = path->nodes[level + 1];
1845 pslot = path->slots[level + 1];
1849 * deal with the case where there is only one pointer in the root
1850 * by promoting the node below to a root
1853 struct extent_buffer *child;
1855 if (btrfs_header_nritems(mid) != 1)
1858 /* promote the child to a root */
1859 child = read_node_slot(fs_info, mid, 0);
1860 if (IS_ERR(child)) {
1861 ret = PTR_ERR(child);
1862 btrfs_handle_fs_error(fs_info, ret, NULL);
1866 btrfs_tree_lock(child);
1867 btrfs_set_lock_blocking(child);
1868 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
1870 btrfs_tree_unlock(child);
1871 free_extent_buffer(child);
1875 ret = tree_mod_log_insert_root(root->node, child, 1);
1877 rcu_assign_pointer(root->node, child);
1879 add_root_to_dirty_list(root);
1880 btrfs_tree_unlock(child);
1882 path->locks[level] = 0;
1883 path->nodes[level] = NULL;
1884 clean_tree_block(fs_info, mid);
1885 btrfs_tree_unlock(mid);
1886 /* once for the path */
1887 free_extent_buffer(mid);
1889 root_sub_used(root, mid->len);
1890 btrfs_free_tree_block(trans, root, mid, 0, 1);
1891 /* once for the root ptr */
1892 free_extent_buffer_stale(mid);
1895 if (btrfs_header_nritems(mid) >
1896 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
1899 left = read_node_slot(fs_info, parent, pslot - 1);
1904 btrfs_tree_lock(left);
1905 btrfs_set_lock_blocking(left);
1906 wret = btrfs_cow_block(trans, root, left,
1907 parent, pslot - 1, &left);
1914 right = read_node_slot(fs_info, parent, pslot + 1);
1919 btrfs_tree_lock(right);
1920 btrfs_set_lock_blocking(right);
1921 wret = btrfs_cow_block(trans, root, right,
1922 parent, pslot + 1, &right);
1929 /* first, try to make some room in the middle buffer */
1931 orig_slot += btrfs_header_nritems(left);
1932 wret = push_node_left(trans, fs_info, left, mid, 1);
1938 * then try to empty the right most buffer into the middle
1941 wret = push_node_left(trans, fs_info, mid, right, 1);
1942 if (wret < 0 && wret != -ENOSPC)
1944 if (btrfs_header_nritems(right) == 0) {
1945 clean_tree_block(fs_info, right);
1946 btrfs_tree_unlock(right);
1947 del_ptr(root, path, level + 1, pslot + 1);
1948 root_sub_used(root, right->len);
1949 btrfs_free_tree_block(trans, root, right, 0, 1);
1950 free_extent_buffer_stale(right);
1953 struct btrfs_disk_key right_key;
1954 btrfs_node_key(right, &right_key, 0);
1955 ret = tree_mod_log_insert_key(parent, pslot + 1,
1956 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1958 btrfs_set_node_key(parent, &right_key, pslot + 1);
1959 btrfs_mark_buffer_dirty(parent);
1962 if (btrfs_header_nritems(mid) == 1) {
1964 * we're not allowed to leave a node with one item in the
1965 * tree during a delete. A deletion from lower in the tree
1966 * could try to delete the only pointer in this node.
1967 * So, pull some keys from the left.
1968 * There has to be a left pointer at this point because
1969 * otherwise we would have pulled some pointers from the
1974 btrfs_handle_fs_error(fs_info, ret, NULL);
1977 wret = balance_node_right(trans, fs_info, mid, left);
1983 wret = push_node_left(trans, fs_info, left, mid, 1);
1989 if (btrfs_header_nritems(mid) == 0) {
1990 clean_tree_block(fs_info, mid);
1991 btrfs_tree_unlock(mid);
1992 del_ptr(root, path, level + 1, pslot);
1993 root_sub_used(root, mid->len);
1994 btrfs_free_tree_block(trans, root, mid, 0, 1);
1995 free_extent_buffer_stale(mid);
1998 /* update the parent key to reflect our changes */
1999 struct btrfs_disk_key mid_key;
2000 btrfs_node_key(mid, &mid_key, 0);
2001 ret = tree_mod_log_insert_key(parent, pslot,
2002 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2004 btrfs_set_node_key(parent, &mid_key, pslot);
2005 btrfs_mark_buffer_dirty(parent);
2008 /* update the path */
2010 if (btrfs_header_nritems(left) > orig_slot) {
2011 extent_buffer_get(left);
2012 /* left was locked after cow */
2013 path->nodes[level] = left;
2014 path->slots[level + 1] -= 1;
2015 path->slots[level] = orig_slot;
2017 btrfs_tree_unlock(mid);
2018 free_extent_buffer(mid);
2021 orig_slot -= btrfs_header_nritems(left);
2022 path->slots[level] = orig_slot;
2025 /* double check we haven't messed things up */
2027 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
2031 btrfs_tree_unlock(right);
2032 free_extent_buffer(right);
2035 if (path->nodes[level] != left)
2036 btrfs_tree_unlock(left);
2037 free_extent_buffer(left);
2042 /* Node balancing for insertion. Here we only split or push nodes around
2043 * when they are completely full. This is also done top down, so we
2044 * have to be pessimistic.
2046 static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
2047 struct btrfs_root *root,
2048 struct btrfs_path *path, int level)
2050 struct btrfs_fs_info *fs_info = root->fs_info;
2051 struct extent_buffer *right = NULL;
2052 struct extent_buffer *mid;
2053 struct extent_buffer *left = NULL;
2054 struct extent_buffer *parent = NULL;
2058 int orig_slot = path->slots[level];
2063 mid = path->nodes[level];
2064 WARN_ON(btrfs_header_generation(mid) != trans->transid);
2066 if (level < BTRFS_MAX_LEVEL - 1) {
2067 parent = path->nodes[level + 1];
2068 pslot = path->slots[level + 1];
2074 left = read_node_slot(fs_info, parent, pslot - 1);
2078 /* first, try to make some room in the middle buffer */
2082 btrfs_tree_lock(left);
2083 btrfs_set_lock_blocking(left);
2085 left_nr = btrfs_header_nritems(left);
2086 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
2089 ret = btrfs_cow_block(trans, root, left, parent,
2094 wret = push_node_left(trans, fs_info,
2101 struct btrfs_disk_key disk_key;
2102 orig_slot += left_nr;
2103 btrfs_node_key(mid, &disk_key, 0);
2104 ret = tree_mod_log_insert_key(parent, pslot,
2105 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2107 btrfs_set_node_key(parent, &disk_key, pslot);
2108 btrfs_mark_buffer_dirty(parent);
2109 if (btrfs_header_nritems(left) > orig_slot) {
2110 path->nodes[level] = left;
2111 path->slots[level + 1] -= 1;
2112 path->slots[level] = orig_slot;
2113 btrfs_tree_unlock(mid);
2114 free_extent_buffer(mid);
2117 btrfs_header_nritems(left);
2118 path->slots[level] = orig_slot;
2119 btrfs_tree_unlock(left);
2120 free_extent_buffer(left);
2124 btrfs_tree_unlock(left);
2125 free_extent_buffer(left);
2127 right = read_node_slot(fs_info, parent, pslot + 1);
2132 * then try to empty the right most buffer into the middle
2137 btrfs_tree_lock(right);
2138 btrfs_set_lock_blocking(right);
2140 right_nr = btrfs_header_nritems(right);
2141 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
2144 ret = btrfs_cow_block(trans, root, right,
2150 wret = balance_node_right(trans, fs_info,
2157 struct btrfs_disk_key disk_key;
2159 btrfs_node_key(right, &disk_key, 0);
2160 ret = tree_mod_log_insert_key(parent, pslot + 1,
2161 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2163 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2164 btrfs_mark_buffer_dirty(parent);
2166 if (btrfs_header_nritems(mid) <= orig_slot) {
2167 path->nodes[level] = right;
2168 path->slots[level + 1] += 1;
2169 path->slots[level] = orig_slot -
2170 btrfs_header_nritems(mid);
2171 btrfs_tree_unlock(mid);
2172 free_extent_buffer(mid);
2174 btrfs_tree_unlock(right);
2175 free_extent_buffer(right);
2179 btrfs_tree_unlock(right);
2180 free_extent_buffer(right);
2186 * readahead one full node of leaves, finding things that are close
2187 * to the block in 'slot', and triggering ra on them.
2189 static void reada_for_search(struct btrfs_fs_info *fs_info,
2190 struct btrfs_path *path,
2191 int level, int slot, u64 objectid)
2193 struct extent_buffer *node;
2194 struct btrfs_disk_key disk_key;
2199 struct extent_buffer *eb;
2207 if (!path->nodes[level])
2210 node = path->nodes[level];
2212 search = btrfs_node_blockptr(node, slot);
2213 blocksize = fs_info->nodesize;
2214 eb = find_extent_buffer(fs_info, search);
2216 free_extent_buffer(eb);
2222 nritems = btrfs_header_nritems(node);
2226 if (path->reada == READA_BACK) {
2230 } else if (path->reada == READA_FORWARD) {
2235 if (path->reada == READA_BACK && objectid) {
2236 btrfs_node_key(node, &disk_key, nr);
2237 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2240 search = btrfs_node_blockptr(node, nr);
2241 if ((search <= target && target - search <= 65536) ||
2242 (search > target && search - target <= 65536)) {
2243 readahead_tree_block(fs_info, search);
2247 if ((nread > 65536 || nscan > 32))
2252 static noinline void reada_for_balance(struct btrfs_fs_info *fs_info,
2253 struct btrfs_path *path, int level)
2257 struct extent_buffer *parent;
2258 struct extent_buffer *eb;
2263 parent = path->nodes[level + 1];
2267 nritems = btrfs_header_nritems(parent);
2268 slot = path->slots[level + 1];
2271 block1 = btrfs_node_blockptr(parent, slot - 1);
2272 gen = btrfs_node_ptr_generation(parent, slot - 1);
2273 eb = find_extent_buffer(fs_info, block1);
2275 * if we get -eagain from btrfs_buffer_uptodate, we
2276 * don't want to return eagain here. That will loop
2279 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
2281 free_extent_buffer(eb);
2283 if (slot + 1 < nritems) {
2284 block2 = btrfs_node_blockptr(parent, slot + 1);
2285 gen = btrfs_node_ptr_generation(parent, slot + 1);
2286 eb = find_extent_buffer(fs_info, block2);
2287 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
2289 free_extent_buffer(eb);
2293 readahead_tree_block(fs_info, block1);
2295 readahead_tree_block(fs_info, block2);
2300 * when we walk down the tree, it is usually safe to unlock the higher layers
2301 * in the tree. The exceptions are when our path goes through slot 0, because
2302 * operations on the tree might require changing key pointers higher up in the
2305 * callers might also have set path->keep_locks, which tells this code to keep
2306 * the lock if the path points to the last slot in the block. This is part of
2307 * walking through the tree, and selecting the next slot in the higher block.
2309 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2310 * if lowest_unlock is 1, level 0 won't be unlocked
2312 static noinline void unlock_up(struct btrfs_path *path, int level,
2313 int lowest_unlock, int min_write_lock_level,
2314 int *write_lock_level)
2317 int skip_level = level;
2319 struct extent_buffer *t;
2321 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2322 if (!path->nodes[i])
2324 if (!path->locks[i])
2326 if (!no_skips && path->slots[i] == 0) {
2330 if (!no_skips && path->keep_locks) {
2333 nritems = btrfs_header_nritems(t);
2334 if (nritems < 1 || path->slots[i] >= nritems - 1) {
2339 if (skip_level < i && i >= lowest_unlock)
2343 if (i >= lowest_unlock && i > skip_level) {
2344 btrfs_tree_unlock_rw(t, path->locks[i]);
2346 if (write_lock_level &&
2347 i > min_write_lock_level &&
2348 i <= *write_lock_level) {
2349 *write_lock_level = i - 1;
2356 * This releases any locks held in the path starting at level and
2357 * going all the way up to the root.
2359 * btrfs_search_slot will keep the lock held on higher nodes in a few
2360 * corner cases, such as COW of the block at slot zero in the node. This
2361 * ignores those rules, and it should only be called when there are no
2362 * more updates to be done higher up in the tree.
2364 noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2368 if (path->keep_locks)
2371 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2372 if (!path->nodes[i])
2374 if (!path->locks[i])
2376 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
2382 * helper function for btrfs_search_slot. The goal is to find a block
2383 * in cache without setting the path to blocking. If we find the block
2384 * we return zero and the path is unchanged.
2386 * If we can't find the block, we set the path blocking and do some
2387 * reada. -EAGAIN is returned and the search must be repeated.
2390 read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
2391 struct extent_buffer **eb_ret, int level, int slot,
2392 const struct btrfs_key *key)
2394 struct btrfs_fs_info *fs_info = root->fs_info;
2397 struct extent_buffer *b = *eb_ret;
2398 struct extent_buffer *tmp;
2399 struct btrfs_key first_key;
2403 blocknr = btrfs_node_blockptr(b, slot);
2404 gen = btrfs_node_ptr_generation(b, slot);
2405 parent_level = btrfs_header_level(b);
2406 btrfs_node_key_to_cpu(b, &first_key, slot);
2408 tmp = find_extent_buffer(fs_info, blocknr);
2410 /* first we do an atomic uptodate check */
2411 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2416 /* the pages were up to date, but we failed
2417 * the generation number check. Do a full
2418 * read for the generation number that is correct.
2419 * We must do this without dropping locks so
2420 * we can trust our generation number
2422 btrfs_set_path_blocking(p);
2424 /* now we're allowed to do a blocking uptodate check */
2425 ret = btrfs_read_buffer(tmp, gen, parent_level - 1, &first_key);
2430 free_extent_buffer(tmp);
2431 btrfs_release_path(p);
2436 * reduce lock contention at high levels
2437 * of the btree by dropping locks before
2438 * we read. Don't release the lock on the current
2439 * level because we need to walk this node to figure
2440 * out which blocks to read.
2442 btrfs_unlock_up_safe(p, level + 1);
2443 btrfs_set_path_blocking(p);
2445 if (p->reada != READA_NONE)
2446 reada_for_search(fs_info, p, level, slot, key->objectid);
2449 tmp = read_tree_block(fs_info, blocknr, gen, parent_level - 1,
2453 * If the read above didn't mark this buffer up to date,
2454 * it will never end up being up to date. Set ret to EIO now
2455 * and give up so that our caller doesn't loop forever
2458 if (!extent_buffer_uptodate(tmp))
2460 free_extent_buffer(tmp);
2465 btrfs_release_path(p);
2470 * helper function for btrfs_search_slot. This does all of the checks
2471 * for node-level blocks and does any balancing required based on
2474 * If no extra work was required, zero is returned. If we had to
2475 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2479 setup_nodes_for_search(struct btrfs_trans_handle *trans,
2480 struct btrfs_root *root, struct btrfs_path *p,
2481 struct extent_buffer *b, int level, int ins_len,
2482 int *write_lock_level)
2484 struct btrfs_fs_info *fs_info = root->fs_info;
2487 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2488 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
2491 if (*write_lock_level < level + 1) {
2492 *write_lock_level = level + 1;
2493 btrfs_release_path(p);
2497 btrfs_set_path_blocking(p);
2498 reada_for_balance(fs_info, p, level);
2499 sret = split_node(trans, root, p, level);
2506 b = p->nodes[level];
2507 } else if (ins_len < 0 && btrfs_header_nritems(b) <
2508 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
2511 if (*write_lock_level < level + 1) {
2512 *write_lock_level = level + 1;
2513 btrfs_release_path(p);
2517 btrfs_set_path_blocking(p);
2518 reada_for_balance(fs_info, p, level);
2519 sret = balance_level(trans, root, p, level);
2525 b = p->nodes[level];
2527 btrfs_release_path(p);
2530 BUG_ON(btrfs_header_nritems(b) == 1);
2540 static void key_search_validate(struct extent_buffer *b,
2541 const struct btrfs_key *key,
2544 #ifdef CONFIG_BTRFS_ASSERT
2545 struct btrfs_disk_key disk_key;
2547 btrfs_cpu_key_to_disk(&disk_key, key);
2550 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2551 offsetof(struct btrfs_leaf, items[0].key),
2554 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2555 offsetof(struct btrfs_node, ptrs[0].key),
2560 static int key_search(struct extent_buffer *b, const struct btrfs_key *key,
2561 int level, int *prev_cmp, int *slot)
2563 if (*prev_cmp != 0) {
2564 *prev_cmp = btrfs_bin_search(b, key, level, slot);
2568 key_search_validate(b, key, level);
2574 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
2575 u64 iobjectid, u64 ioff, u8 key_type,
2576 struct btrfs_key *found_key)
2579 struct btrfs_key key;
2580 struct extent_buffer *eb;
2585 key.type = key_type;
2586 key.objectid = iobjectid;
2589 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
2593 eb = path->nodes[0];
2594 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2595 ret = btrfs_next_leaf(fs_root, path);
2598 eb = path->nodes[0];
2601 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2602 if (found_key->type != key.type ||
2603 found_key->objectid != key.objectid)
2609 static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,
2610 struct btrfs_path *p,
2611 int write_lock_level)
2613 struct btrfs_fs_info *fs_info = root->fs_info;
2614 struct extent_buffer *b;
2618 /* We try very hard to do read locks on the root */
2619 root_lock = BTRFS_READ_LOCK;
2621 if (p->search_commit_root) {
2623 * The commit roots are read only so we always do read locks,
2624 * and we always must hold the commit_root_sem when doing
2625 * searches on them, the only exception is send where we don't
2626 * want to block transaction commits for a long time, so
2627 * we need to clone the commit root in order to avoid races
2628 * with transaction commits that create a snapshot of one of
2629 * the roots used by a send operation.
2631 if (p->need_commit_sem) {
2632 down_read(&fs_info->commit_root_sem);
2633 b = btrfs_clone_extent_buffer(root->commit_root);
2634 up_read(&fs_info->commit_root_sem);
2636 return ERR_PTR(-ENOMEM);
2639 b = root->commit_root;
2640 extent_buffer_get(b);
2642 level = btrfs_header_level(b);
2644 * Ensure that all callers have set skip_locking when
2645 * p->search_commit_root = 1.
2647 ASSERT(p->skip_locking == 1);
2652 if (p->skip_locking) {
2653 b = btrfs_root_node(root);
2654 level = btrfs_header_level(b);
2659 * If the level is set to maximum, we can skip trying to get the read
2662 if (write_lock_level < BTRFS_MAX_LEVEL) {
2664 * We don't know the level of the root node until we actually
2665 * have it read locked
2667 b = btrfs_read_lock_root_node(root);
2668 level = btrfs_header_level(b);
2669 if (level > write_lock_level)
2672 /* Whoops, must trade for write lock */
2673 btrfs_tree_read_unlock(b);
2674 free_extent_buffer(b);
2677 b = btrfs_lock_root_node(root);
2678 root_lock = BTRFS_WRITE_LOCK;
2680 /* The level might have changed, check again */
2681 level = btrfs_header_level(b);
2684 p->nodes[level] = b;
2685 if (!p->skip_locking)
2686 p->locks[level] = root_lock;
2688 * Callers are responsible for dropping b's references.
2695 * btrfs_search_slot - look for a key in a tree and perform necessary
2696 * modifications to preserve tree invariants.
2698 * @trans: Handle of transaction, used when modifying the tree
2699 * @p: Holds all btree nodes along the search path
2700 * @root: The root node of the tree
2701 * @key: The key we are looking for
2702 * @ins_len: Indicates purpose of search, for inserts it is 1, for
2703 * deletions it's -1. 0 for plain searches
2704 * @cow: boolean should CoW operations be performed. Must always be 1
2705 * when modifying the tree.
2707 * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
2708 * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
2710 * If @key is found, 0 is returned and you can find the item in the leaf level
2711 * of the path (level 0)
2713 * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
2714 * points to the slot where it should be inserted
2716 * If an error is encountered while searching the tree a negative error number
2719 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2720 const struct btrfs_key *key, struct btrfs_path *p,
2721 int ins_len, int cow)
2723 struct btrfs_fs_info *fs_info = root->fs_info;
2724 struct extent_buffer *b;
2729 int lowest_unlock = 1;
2730 /* everything at write_lock_level or lower must be write locked */
2731 int write_lock_level = 0;
2732 u8 lowest_level = 0;
2733 int min_write_lock_level;
2736 lowest_level = p->lowest_level;
2737 WARN_ON(lowest_level && ins_len > 0);
2738 WARN_ON(p->nodes[0] != NULL);
2739 BUG_ON(!cow && ins_len);
2744 /* when we are removing items, we might have to go up to level
2745 * two as we update tree pointers Make sure we keep write
2746 * for those levels as well
2748 write_lock_level = 2;
2749 } else if (ins_len > 0) {
2751 * for inserting items, make sure we have a write lock on
2752 * level 1 so we can update keys
2754 write_lock_level = 1;
2758 write_lock_level = -1;
2760 if (cow && (p->keep_locks || p->lowest_level))
2761 write_lock_level = BTRFS_MAX_LEVEL;
2763 min_write_lock_level = write_lock_level;
2767 b = btrfs_search_slot_get_root(root, p, write_lock_level);
2774 level = btrfs_header_level(b);
2777 * setup the path here so we can release it under lock
2778 * contention with the cow code
2781 bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
2784 * if we don't really need to cow this block
2785 * then we don't want to set the path blocking,
2786 * so we test it here
2788 if (!should_cow_block(trans, root, b)) {
2789 trans->dirty = true;
2794 * must have write locks on this node and the
2797 if (level > write_lock_level ||
2798 (level + 1 > write_lock_level &&
2799 level + 1 < BTRFS_MAX_LEVEL &&
2800 p->nodes[level + 1])) {
2801 write_lock_level = level + 1;
2802 btrfs_release_path(p);
2806 btrfs_set_path_blocking(p);
2808 err = btrfs_cow_block(trans, root, b, NULL, 0,
2811 err = btrfs_cow_block(trans, root, b,
2812 p->nodes[level + 1],
2813 p->slots[level + 1], &b);
2820 p->nodes[level] = b;
2822 * Leave path with blocking locks to avoid massive
2823 * lock context switch, this is made on purpose.
2827 * we have a lock on b and as long as we aren't changing
2828 * the tree, there is no way to for the items in b to change.
2829 * It is safe to drop the lock on our parent before we
2830 * go through the expensive btree search on b.
2832 * If we're inserting or deleting (ins_len != 0), then we might
2833 * be changing slot zero, which may require changing the parent.
2834 * So, we can't drop the lock until after we know which slot
2835 * we're operating on.
2837 if (!ins_len && !p->keep_locks) {
2840 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2841 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2846 ret = key_search(b, key, level, &prev_cmp, &slot);
2852 if (ret && slot > 0) {
2856 p->slots[level] = slot;
2857 err = setup_nodes_for_search(trans, root, p, b, level,
2858 ins_len, &write_lock_level);
2865 b = p->nodes[level];
2866 slot = p->slots[level];
2869 * slot 0 is special, if we change the key
2870 * we have to update the parent pointer
2871 * which means we must have a write lock
2874 if (slot == 0 && ins_len &&
2875 write_lock_level < level + 1) {
2876 write_lock_level = level + 1;
2877 btrfs_release_path(p);
2881 unlock_up(p, level, lowest_unlock,
2882 min_write_lock_level, &write_lock_level);
2884 if (level == lowest_level) {
2890 err = read_block_for_search(root, p, &b, level,
2899 if (!p->skip_locking) {
2900 level = btrfs_header_level(b);
2901 if (level <= write_lock_level) {
2902 err = btrfs_try_tree_write_lock(b);
2904 btrfs_set_path_blocking(p);
2907 p->locks[level] = BTRFS_WRITE_LOCK;
2909 err = btrfs_tree_read_lock_atomic(b);
2911 btrfs_set_path_blocking(p);
2912 btrfs_tree_read_lock(b);
2914 p->locks[level] = BTRFS_READ_LOCK;
2916 p->nodes[level] = b;
2919 p->slots[level] = slot;
2921 btrfs_leaf_free_space(fs_info, b) < ins_len) {
2922 if (write_lock_level < 1) {
2923 write_lock_level = 1;
2924 btrfs_release_path(p);
2928 btrfs_set_path_blocking(p);
2929 err = split_leaf(trans, root, key,
2930 p, ins_len, ret == 0);
2938 if (!p->search_for_split)
2939 unlock_up(p, level, lowest_unlock,
2940 min_write_lock_level, NULL);
2947 * we don't really know what they plan on doing with the path
2948 * from here on, so for now just mark it as blocking
2950 if (!p->leave_spinning)
2951 btrfs_set_path_blocking(p);
2952 if (ret < 0 && !p->skip_release_on_error)
2953 btrfs_release_path(p);
2958 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2959 * current state of the tree together with the operations recorded in the tree
2960 * modification log to search for the key in a previous version of this tree, as
2961 * denoted by the time_seq parameter.
2963 * Naturally, there is no support for insert, delete or cow operations.
2965 * The resulting path and return value will be set up as if we called
2966 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2968 int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
2969 struct btrfs_path *p, u64 time_seq)
2971 struct btrfs_fs_info *fs_info = root->fs_info;
2972 struct extent_buffer *b;
2977 int lowest_unlock = 1;
2978 u8 lowest_level = 0;
2981 lowest_level = p->lowest_level;
2982 WARN_ON(p->nodes[0] != NULL);
2984 if (p->search_commit_root) {
2986 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2990 b = get_old_root(root, time_seq);
2995 level = btrfs_header_level(b);
2996 p->locks[level] = BTRFS_READ_LOCK;
2999 level = btrfs_header_level(b);
3000 p->nodes[level] = b;
3003 * we have a lock on b and as long as we aren't changing
3004 * the tree, there is no way to for the items in b to change.
3005 * It is safe to drop the lock on our parent before we
3006 * go through the expensive btree search on b.
3008 btrfs_unlock_up_safe(p, level + 1);
3011 * Since we can unwind ebs we want to do a real search every
3015 ret = key_search(b, key, level, &prev_cmp, &slot);
3019 if (ret && slot > 0) {
3023 p->slots[level] = slot;
3024 unlock_up(p, level, lowest_unlock, 0, NULL);
3026 if (level == lowest_level) {
3032 err = read_block_for_search(root, p, &b, level,
3041 level = btrfs_header_level(b);
3042 err = btrfs_tree_read_lock_atomic(b);
3044 btrfs_set_path_blocking(p);
3045 btrfs_tree_read_lock(b);
3047 b = tree_mod_log_rewind(fs_info, p, b, time_seq);
3052 p->locks[level] = BTRFS_READ_LOCK;
3053 p->nodes[level] = b;
3055 p->slots[level] = slot;
3056 unlock_up(p, level, lowest_unlock, 0, NULL);
3062 if (!p->leave_spinning)
3063 btrfs_set_path_blocking(p);
3065 btrfs_release_path(p);
3071 * helper to use instead of search slot if no exact match is needed but
3072 * instead the next or previous item should be returned.
3073 * When find_higher is true, the next higher item is returned, the next lower
3075 * When return_any and find_higher are both true, and no higher item is found,
3076 * return the next lower instead.
3077 * When return_any is true and find_higher is false, and no lower item is found,
3078 * return the next higher instead.
3079 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3082 int btrfs_search_slot_for_read(struct btrfs_root *root,
3083 const struct btrfs_key *key,
3084 struct btrfs_path *p, int find_higher,
3088 struct extent_buffer *leaf;
3091 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3095 * a return value of 1 means the path is at the position where the
3096 * item should be inserted. Normally this is the next bigger item,
3097 * but in case the previous item is the last in a leaf, path points
3098 * to the first free slot in the previous leaf, i.e. at an invalid
3104 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3105 ret = btrfs_next_leaf(root, p);
3111 * no higher item found, return the next
3116 btrfs_release_path(p);
3120 if (p->slots[0] == 0) {
3121 ret = btrfs_prev_leaf(root, p);
3126 if (p->slots[0] == btrfs_header_nritems(leaf))
3133 * no lower item found, return the next
3138 btrfs_release_path(p);
3148 * adjust the pointers going up the tree, starting at level
3149 * making sure the right key of each node is points to 'key'.
3150 * This is used after shifting pointers to the left, so it stops
3151 * fixing up pointers when a given leaf/node is not in slot 0 of the
3155 static void fixup_low_keys(struct btrfs_path *path,
3156 struct btrfs_disk_key *key, int level)
3159 struct extent_buffer *t;
3162 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
3163 int tslot = path->slots[i];
3165 if (!path->nodes[i])
3168 ret = tree_mod_log_insert_key(t, tslot, MOD_LOG_KEY_REPLACE,
3171 btrfs_set_node_key(t, key, tslot);
3172 btrfs_mark_buffer_dirty(path->nodes[i]);
3181 * This function isn't completely safe. It's the caller's responsibility
3182 * that the new key won't break the order
3184 void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
3185 struct btrfs_path *path,
3186 const struct btrfs_key *new_key)
3188 struct btrfs_disk_key disk_key;
3189 struct extent_buffer *eb;
3192 eb = path->nodes[0];
3193 slot = path->slots[0];
3195 btrfs_item_key(eb, &disk_key, slot - 1);
3196 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
3198 if (slot < btrfs_header_nritems(eb) - 1) {
3199 btrfs_item_key(eb, &disk_key, slot + 1);
3200 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
3203 btrfs_cpu_key_to_disk(&disk_key, new_key);
3204 btrfs_set_item_key(eb, &disk_key, slot);
3205 btrfs_mark_buffer_dirty(eb);
3207 fixup_low_keys(path, &disk_key, 1);
3211 * try to push data from one node into the next node left in the
3214 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3215 * error, and > 0 if there was no room in the left hand block.
3217 static int push_node_left(struct btrfs_trans_handle *trans,
3218 struct btrfs_fs_info *fs_info,
3219 struct extent_buffer *dst,
3220 struct extent_buffer *src, int empty)
3227 src_nritems = btrfs_header_nritems(src);
3228 dst_nritems = btrfs_header_nritems(dst);
3229 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
3230 WARN_ON(btrfs_header_generation(src) != trans->transid);
3231 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3233 if (!empty && src_nritems <= 8)
3236 if (push_items <= 0)
3240 push_items = min(src_nritems, push_items);
3241 if (push_items < src_nritems) {
3242 /* leave at least 8 pointers in the node if
3243 * we aren't going to empty it
3245 if (src_nritems - push_items < 8) {
3246 if (push_items <= 8)
3252 push_items = min(src_nritems - 8, push_items);
3254 ret = tree_mod_log_eb_copy(fs_info, dst, src, dst_nritems, 0,
3257 btrfs_abort_transaction(trans, ret);
3260 copy_extent_buffer(dst, src,
3261 btrfs_node_key_ptr_offset(dst_nritems),
3262 btrfs_node_key_ptr_offset(0),
3263 push_items * sizeof(struct btrfs_key_ptr));
3265 if (push_items < src_nritems) {
3267 * Don't call tree_mod_log_insert_move here, key removal was
3268 * already fully logged by tree_mod_log_eb_copy above.
3270 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3271 btrfs_node_key_ptr_offset(push_items),
3272 (src_nritems - push_items) *
3273 sizeof(struct btrfs_key_ptr));
3275 btrfs_set_header_nritems(src, src_nritems - push_items);
3276 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3277 btrfs_mark_buffer_dirty(src);
3278 btrfs_mark_buffer_dirty(dst);
3284 * try to push data from one node into the next node right in the
3287 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3288 * error, and > 0 if there was no room in the right hand block.
3290 * this will only push up to 1/2 the contents of the left node over
3292 static int balance_node_right(struct btrfs_trans_handle *trans,
3293 struct btrfs_fs_info *fs_info,
3294 struct extent_buffer *dst,
3295 struct extent_buffer *src)
3303 WARN_ON(btrfs_header_generation(src) != trans->transid);
3304 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3306 src_nritems = btrfs_header_nritems(src);
3307 dst_nritems = btrfs_header_nritems(dst);
3308 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
3309 if (push_items <= 0)
3312 if (src_nritems < 4)
3315 max_push = src_nritems / 2 + 1;
3316 /* don't try to empty the node */
3317 if (max_push >= src_nritems)
3320 if (max_push < push_items)
3321 push_items = max_push;
3323 ret = tree_mod_log_insert_move(dst, push_items, 0, dst_nritems);
3325 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3326 btrfs_node_key_ptr_offset(0),
3328 sizeof(struct btrfs_key_ptr));
3330 ret = tree_mod_log_eb_copy(fs_info, dst, src, 0,
3331 src_nritems - push_items, push_items);
3333 btrfs_abort_transaction(trans, ret);
3336 copy_extent_buffer(dst, src,
3337 btrfs_node_key_ptr_offset(0),
3338 btrfs_node_key_ptr_offset(src_nritems - push_items),
3339 push_items * sizeof(struct btrfs_key_ptr));
3341 btrfs_set_header_nritems(src, src_nritems - push_items);
3342 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3344 btrfs_mark_buffer_dirty(src);
3345 btrfs_mark_buffer_dirty(dst);
3351 * helper function to insert a new root level in the tree.
3352 * A new node is allocated, and a single item is inserted to
3353 * point to the existing root
3355 * returns zero on success or < 0 on failure.
3357 static noinline int insert_new_root(struct btrfs_trans_handle *trans,
3358 struct btrfs_root *root,
3359 struct btrfs_path *path, int level)
3361 struct btrfs_fs_info *fs_info = root->fs_info;
3363 struct extent_buffer *lower;
3364 struct extent_buffer *c;
3365 struct extent_buffer *old;
3366 struct btrfs_disk_key lower_key;
3369 BUG_ON(path->nodes[level]);
3370 BUG_ON(path->nodes[level-1] != root->node);
3372 lower = path->nodes[level-1];
3374 btrfs_item_key(lower, &lower_key, 0);
3376 btrfs_node_key(lower, &lower_key, 0);
3378 c = alloc_tree_block_no_bg_flush(trans, root, 0, &lower_key, level,
3379 root->node->start, 0);
3383 root_add_used(root, fs_info->nodesize);
3385 btrfs_set_header_nritems(c, 1);
3386 btrfs_set_node_key(c, &lower_key, 0);
3387 btrfs_set_node_blockptr(c, 0, lower->start);
3388 lower_gen = btrfs_header_generation(lower);
3389 WARN_ON(lower_gen != trans->transid);
3391 btrfs_set_node_ptr_generation(c, 0, lower_gen);
3393 btrfs_mark_buffer_dirty(c);
3396 ret = tree_mod_log_insert_root(root->node, c, 0);
3398 rcu_assign_pointer(root->node, c);
3400 /* the super has an extra ref to root->node */
3401 free_extent_buffer(old);
3403 add_root_to_dirty_list(root);
3404 extent_buffer_get(c);
3405 path->nodes[level] = c;
3406 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
3407 path->slots[level] = 0;
3412 * worker function to insert a single pointer in a node.
3413 * the node should have enough room for the pointer already
3415 * slot and level indicate where you want the key to go, and
3416 * blocknr is the block the key points to.
3418 static void insert_ptr(struct btrfs_trans_handle *trans,
3419 struct btrfs_fs_info *fs_info, struct btrfs_path *path,
3420 struct btrfs_disk_key *key, u64 bytenr,
3421 int slot, int level)
3423 struct extent_buffer *lower;
3427 BUG_ON(!path->nodes[level]);
3428 btrfs_assert_tree_locked(path->nodes[level]);
3429 lower = path->nodes[level];
3430 nritems = btrfs_header_nritems(lower);
3431 BUG_ON(slot > nritems);
3432 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(fs_info));
3433 if (slot != nritems) {
3435 ret = tree_mod_log_insert_move(lower, slot + 1, slot,
3439 memmove_extent_buffer(lower,
3440 btrfs_node_key_ptr_offset(slot + 1),
3441 btrfs_node_key_ptr_offset(slot),
3442 (nritems - slot) * sizeof(struct btrfs_key_ptr));
3445 ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD,
3449 btrfs_set_node_key(lower, key, slot);
3450 btrfs_set_node_blockptr(lower, slot, bytenr);
3451 WARN_ON(trans->transid == 0);
3452 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
3453 btrfs_set_header_nritems(lower, nritems + 1);
3454 btrfs_mark_buffer_dirty(lower);
3458 * split the node at the specified level in path in two.
3459 * The path is corrected to point to the appropriate node after the split
3461 * Before splitting this tries to make some room in the node by pushing
3462 * left and right, if either one works, it returns right away.
3464 * returns 0 on success and < 0 on failure
3466 static noinline int split_node(struct btrfs_trans_handle *trans,
3467 struct btrfs_root *root,
3468 struct btrfs_path *path, int level)
3470 struct btrfs_fs_info *fs_info = root->fs_info;
3471 struct extent_buffer *c;
3472 struct extent_buffer *split;
3473 struct btrfs_disk_key disk_key;
3478 c = path->nodes[level];
3479 WARN_ON(btrfs_header_generation(c) != trans->transid);
3480 if (c == root->node) {
3482 * trying to split the root, lets make a new one
3484 * tree mod log: We don't log_removal old root in
3485 * insert_new_root, because that root buffer will be kept as a
3486 * normal node. We are going to log removal of half of the
3487 * elements below with tree_mod_log_eb_copy. We're holding a
3488 * tree lock on the buffer, which is why we cannot race with
3489 * other tree_mod_log users.
3491 ret = insert_new_root(trans, root, path, level + 1);
3495 ret = push_nodes_for_insert(trans, root, path, level);
3496 c = path->nodes[level];
3497 if (!ret && btrfs_header_nritems(c) <
3498 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
3504 c_nritems = btrfs_header_nritems(c);
3505 mid = (c_nritems + 1) / 2;
3506 btrfs_node_key(c, &disk_key, mid);
3508 split = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, level,
3511 return PTR_ERR(split);
3513 root_add_used(root, fs_info->nodesize);
3514 ASSERT(btrfs_header_level(c) == level);
3516 ret = tree_mod_log_eb_copy(fs_info, split, c, 0, mid, c_nritems - mid);
3518 btrfs_abort_transaction(trans, ret);
3521 copy_extent_buffer(split, c,
3522 btrfs_node_key_ptr_offset(0),
3523 btrfs_node_key_ptr_offset(mid),
3524 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3525 btrfs_set_header_nritems(split, c_nritems - mid);
3526 btrfs_set_header_nritems(c, mid);
3529 btrfs_mark_buffer_dirty(c);
3530 btrfs_mark_buffer_dirty(split);
3532 insert_ptr(trans, fs_info, path, &disk_key, split->start,
3533 path->slots[level + 1] + 1, level + 1);
3535 if (path->slots[level] >= mid) {
3536 path->slots[level] -= mid;
3537 btrfs_tree_unlock(c);
3538 free_extent_buffer(c);
3539 path->nodes[level] = split;
3540 path->slots[level + 1] += 1;
3542 btrfs_tree_unlock(split);
3543 free_extent_buffer(split);
3549 * how many bytes are required to store the items in a leaf. start
3550 * and nr indicate which items in the leaf to check. This totals up the
3551 * space used both by the item structs and the item data
3553 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
3555 struct btrfs_item *start_item;
3556 struct btrfs_item *end_item;
3557 struct btrfs_map_token token;
3559 int nritems = btrfs_header_nritems(l);
3560 int end = min(nritems, start + nr) - 1;
3564 btrfs_init_map_token(&token);
3565 start_item = btrfs_item_nr(start);
3566 end_item = btrfs_item_nr(end);
3567 data_len = btrfs_token_item_offset(l, start_item, &token) +
3568 btrfs_token_item_size(l, start_item, &token);
3569 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
3570 data_len += sizeof(struct btrfs_item) * nr;
3571 WARN_ON(data_len < 0);
3576 * The space between the end of the leaf items and
3577 * the start of the leaf data. IOW, how much room
3578 * the leaf has left for both items and data
3580 noinline int btrfs_leaf_free_space(struct btrfs_fs_info *fs_info,
3581 struct extent_buffer *leaf)
3583 int nritems = btrfs_header_nritems(leaf);
3586 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
3589 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
3591 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
3592 leaf_space_used(leaf, 0, nritems), nritems);
3598 * min slot controls the lowest index we're willing to push to the
3599 * right. We'll push up to and including min_slot, but no lower
3601 static noinline int __push_leaf_right(struct btrfs_fs_info *fs_info,
3602 struct btrfs_path *path,
3603 int data_size, int empty,
3604 struct extent_buffer *right,
3605 int free_space, u32 left_nritems,
3608 struct extent_buffer *left = path->nodes[0];
3609 struct extent_buffer *upper = path->nodes[1];
3610 struct btrfs_map_token token;
3611 struct btrfs_disk_key disk_key;
3616 struct btrfs_item *item;
3622 btrfs_init_map_token(&token);
3627 nr = max_t(u32, 1, min_slot);
3629 if (path->slots[0] >= left_nritems)
3630 push_space += data_size;
3632 slot = path->slots[1];
3633 i = left_nritems - 1;
3635 item = btrfs_item_nr(i);
3637 if (!empty && push_items > 0) {
3638 if (path->slots[0] > i)
3640 if (path->slots[0] == i) {
3641 int space = btrfs_leaf_free_space(fs_info, left);
3642 if (space + push_space * 2 > free_space)
3647 if (path->slots[0] == i)
3648 push_space += data_size;
3650 this_item_size = btrfs_item_size(left, item);
3651 if (this_item_size + sizeof(*item) + push_space > free_space)
3655 push_space += this_item_size + sizeof(*item);
3661 if (push_items == 0)
3664 WARN_ON(!empty && push_items == left_nritems);
3666 /* push left to right */
3667 right_nritems = btrfs_header_nritems(right);
3669 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
3670 push_space -= leaf_data_end(fs_info, left);
3672 /* make room in the right data area */
3673 data_end = leaf_data_end(fs_info, right);
3674 memmove_extent_buffer(right,
3675 BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
3676 BTRFS_LEAF_DATA_OFFSET + data_end,
3677 BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
3679 /* copy from the left data area */
3680 copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
3681 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
3682 BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, left),
3685 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3686 btrfs_item_nr_offset(0),
3687 right_nritems * sizeof(struct btrfs_item));
3689 /* copy the items from left to right */
3690 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3691 btrfs_item_nr_offset(left_nritems - push_items),
3692 push_items * sizeof(struct btrfs_item));
3694 /* update the item pointers */
3695 right_nritems += push_items;
3696 btrfs_set_header_nritems(right, right_nritems);
3697 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
3698 for (i = 0; i < right_nritems; i++) {
3699 item = btrfs_item_nr(i);
3700 push_space -= btrfs_token_item_size(right, item, &token);
3701 btrfs_set_token_item_offset(right, item, push_space, &token);
3704 left_nritems -= push_items;
3705 btrfs_set_header_nritems(left, left_nritems);
3708 btrfs_mark_buffer_dirty(left);
3710 clean_tree_block(fs_info, left);
3712 btrfs_mark_buffer_dirty(right);
3714 btrfs_item_key(right, &disk_key, 0);
3715 btrfs_set_node_key(upper, &disk_key, slot + 1);
3716 btrfs_mark_buffer_dirty(upper);
3718 /* then fixup the leaf pointer in the path */
3719 if (path->slots[0] >= left_nritems) {
3720 path->slots[0] -= left_nritems;
3721 if (btrfs_header_nritems(path->nodes[0]) == 0)
3722 clean_tree_block(fs_info, path->nodes[0]);
3723 btrfs_tree_unlock(path->nodes[0]);
3724 free_extent_buffer(path->nodes[0]);
3725 path->nodes[0] = right;
3726 path->slots[1] += 1;
3728 btrfs_tree_unlock(right);
3729 free_extent_buffer(right);
3734 btrfs_tree_unlock(right);
3735 free_extent_buffer(right);
3740 * push some data in the path leaf to the right, trying to free up at
3741 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3743 * returns 1 if the push failed because the other node didn't have enough
3744 * room, 0 if everything worked out and < 0 if there were major errors.
3746 * this will push starting from min_slot to the end of the leaf. It won't
3747 * push any slot lower than min_slot
3749 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
3750 *root, struct btrfs_path *path,
3751 int min_data_size, int data_size,
3752 int empty, u32 min_slot)
3754 struct btrfs_fs_info *fs_info = root->fs_info;
3755 struct extent_buffer *left = path->nodes[0];
3756 struct extent_buffer *right;
3757 struct extent_buffer *upper;
3763 if (!path->nodes[1])
3766 slot = path->slots[1];
3767 upper = path->nodes[1];
3768 if (slot >= btrfs_header_nritems(upper) - 1)
3771 btrfs_assert_tree_locked(path->nodes[1]);
3773 right = read_node_slot(fs_info, upper, slot + 1);
3775 * slot + 1 is not valid or we fail to read the right node,
3776 * no big deal, just return.
3781 btrfs_tree_lock(right);
3782 btrfs_set_lock_blocking(right);
3784 free_space = btrfs_leaf_free_space(fs_info, right);
3785 if (free_space < data_size)
3788 /* cow and double check */
3789 ret = btrfs_cow_block(trans, root, right, upper,
3794 free_space = btrfs_leaf_free_space(fs_info, right);
3795 if (free_space < data_size)
3798 left_nritems = btrfs_header_nritems(left);
3799 if (left_nritems == 0)
3802 if (path->slots[0] == left_nritems && !empty) {
3803 /* Key greater than all keys in the leaf, right neighbor has
3804 * enough room for it and we're not emptying our leaf to delete
3805 * it, therefore use right neighbor to insert the new item and
3806 * no need to touch/dirty our left leaf. */
3807 btrfs_tree_unlock(left);
3808 free_extent_buffer(left);
3809 path->nodes[0] = right;
3815 return __push_leaf_right(fs_info, path, min_data_size, empty,
3816 right, free_space, left_nritems, min_slot);
3818 btrfs_tree_unlock(right);
3819 free_extent_buffer(right);
3824 * push some data in the path leaf to the left, trying to free up at
3825 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3827 * max_slot can put a limit on how far into the leaf we'll push items. The
3828 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3831 static noinline int __push_leaf_left(struct btrfs_fs_info *fs_info,
3832 struct btrfs_path *path, int data_size,
3833 int empty, struct extent_buffer *left,
3834 int free_space, u32 right_nritems,
3837 struct btrfs_disk_key disk_key;
3838 struct extent_buffer *right = path->nodes[0];
3842 struct btrfs_item *item;
3843 u32 old_left_nritems;
3847 u32 old_left_item_size;
3848 struct btrfs_map_token token;
3850 btrfs_init_map_token(&token);
3853 nr = min(right_nritems, max_slot);
3855 nr = min(right_nritems - 1, max_slot);
3857 for (i = 0; i < nr; i++) {
3858 item = btrfs_item_nr(i);
3860 if (!empty && push_items > 0) {
3861 if (path->slots[0] < i)
3863 if (path->slots[0] == i) {
3864 int space = btrfs_leaf_free_space(fs_info, right);
3865 if (space + push_space * 2 > free_space)
3870 if (path->slots[0] == i)
3871 push_space += data_size;
3873 this_item_size = btrfs_item_size(right, item);
3874 if (this_item_size + sizeof(*item) + push_space > free_space)
3878 push_space += this_item_size + sizeof(*item);
3881 if (push_items == 0) {
3885 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
3887 /* push data from right to left */
3888 copy_extent_buffer(left, right,
3889 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3890 btrfs_item_nr_offset(0),
3891 push_items * sizeof(struct btrfs_item));
3893 push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
3894 btrfs_item_offset_nr(right, push_items - 1);
3896 copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
3897 leaf_data_end(fs_info, left) - push_space,
3898 BTRFS_LEAF_DATA_OFFSET +
3899 btrfs_item_offset_nr(right, push_items - 1),
3901 old_left_nritems = btrfs_header_nritems(left);
3902 BUG_ON(old_left_nritems <= 0);
3904 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
3905 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
3908 item = btrfs_item_nr(i);
3910 ioff = btrfs_token_item_offset(left, item, &token);
3911 btrfs_set_token_item_offset(left, item,
3912 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size),
3915 btrfs_set_header_nritems(left, old_left_nritems + push_items);
3917 /* fixup right node */
3918 if (push_items > right_nritems)
3919 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
3922 if (push_items < right_nritems) {
3923 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3924 leaf_data_end(fs_info, right);
3925 memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
3926 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
3927 BTRFS_LEAF_DATA_OFFSET +
3928 leaf_data_end(fs_info, right), push_space);
3930 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
3931 btrfs_item_nr_offset(push_items),
3932 (btrfs_header_nritems(right) - push_items) *
3933 sizeof(struct btrfs_item));
3935 right_nritems -= push_items;
3936 btrfs_set_header_nritems(right, right_nritems);
3937 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
3938 for (i = 0; i < right_nritems; i++) {
3939 item = btrfs_item_nr(i);
3941 push_space = push_space - btrfs_token_item_size(right,
3943 btrfs_set_token_item_offset(right, item, push_space, &token);
3946 btrfs_mark_buffer_dirty(left);
3948 btrfs_mark_buffer_dirty(right);
3950 clean_tree_block(fs_info, right);
3952 btrfs_item_key(right, &disk_key, 0);
3953 fixup_low_keys(path, &disk_key, 1);
3955 /* then fixup the leaf pointer in the path */
3956 if (path->slots[0] < push_items) {
3957 path->slots[0] += old_left_nritems;
3958 btrfs_tree_unlock(path->nodes[0]);
3959 free_extent_buffer(path->nodes[0]);
3960 path->nodes[0] = left;
3961 path->slots[1] -= 1;
3963 btrfs_tree_unlock(left);
3964 free_extent_buffer(left);
3965 path->slots[0] -= push_items;
3967 BUG_ON(path->slots[0] < 0);
3970 btrfs_tree_unlock(left);
3971 free_extent_buffer(left);
3976 * push some data in the path leaf to the left, trying to free up at
3977 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3979 * max_slot can put a limit on how far into the leaf we'll push items. The
3980 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3983 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
3984 *root, struct btrfs_path *path, int min_data_size,
3985 int data_size, int empty, u32 max_slot)
3987 struct btrfs_fs_info *fs_info = root->fs_info;
3988 struct extent_buffer *right = path->nodes[0];
3989 struct extent_buffer *left;
3995 slot = path->slots[1];
3998 if (!path->nodes[1])
4001 right_nritems = btrfs_header_nritems(right);
4002 if (right_nritems == 0)
4005 btrfs_assert_tree_locked(path->nodes[1]);
4007 left = read_node_slot(fs_info, path->nodes[1], slot - 1);
4009 * slot - 1 is not valid or we fail to read the left node,
4010 * no big deal, just return.
4015 btrfs_tree_lock(left);
4016 btrfs_set_lock_blocking(left);
4018 free_space = btrfs_leaf_free_space(fs_info, left);
4019 if (free_space < data_size) {
4024 /* cow and double check */
4025 ret = btrfs_cow_block(trans, root, left,
4026 path->nodes[1], slot - 1, &left);
4028 /* we hit -ENOSPC, but it isn't fatal here */
4034 free_space = btrfs_leaf_free_space(fs_info, left);
4035 if (free_space < data_size) {
4040 return __push_leaf_left(fs_info, path, min_data_size,
4041 empty, left, free_space, right_nritems,
4044 btrfs_tree_unlock(left);
4045 free_extent_buffer(left);
4050 * split the path's leaf in two, making sure there is at least data_size
4051 * available for the resulting leaf level of the path.
4053 static noinline void copy_for_split(struct btrfs_trans_handle *trans,
4054 struct btrfs_fs_info *fs_info,
4055 struct btrfs_path *path,
4056 struct extent_buffer *l,
4057 struct extent_buffer *right,
4058 int slot, int mid, int nritems)
4063 struct btrfs_disk_key disk_key;
4064 struct btrfs_map_token token;
4066 btrfs_init_map_token(&token);
4068 nritems = nritems - mid;
4069 btrfs_set_header_nritems(right, nritems);
4070 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(fs_info, l);
4072 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4073 btrfs_item_nr_offset(mid),
4074 nritems * sizeof(struct btrfs_item));
4076 copy_extent_buffer(right, l,
4077 BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
4078 data_copy_size, BTRFS_LEAF_DATA_OFFSET +
4079 leaf_data_end(fs_info, l), data_copy_size);
4081 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
4083 for (i = 0; i < nritems; i++) {
4084 struct btrfs_item *item = btrfs_item_nr(i);
4087 ioff = btrfs_token_item_offset(right, item, &token);
4088 btrfs_set_token_item_offset(right, item,
4089 ioff + rt_data_off, &token);
4092 btrfs_set_header_nritems(l, mid);
4093 btrfs_item_key(right, &disk_key, 0);
4094 insert_ptr(trans, fs_info, path, &disk_key, right->start,
4095 path->slots[1] + 1, 1);
4097 btrfs_mark_buffer_dirty(right);
4098 btrfs_mark_buffer_dirty(l);
4099 BUG_ON(path->slots[0] != slot);
4102 btrfs_tree_unlock(path->nodes[0]);
4103 free_extent_buffer(path->nodes[0]);
4104 path->nodes[0] = right;
4105 path->slots[0] -= mid;
4106 path->slots[1] += 1;
4108 btrfs_tree_unlock(right);
4109 free_extent_buffer(right);
4112 BUG_ON(path->slots[0] < 0);
4116 * double splits happen when we need to insert a big item in the middle
4117 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4118 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4121 * We avoid this by trying to push the items on either side of our target
4122 * into the adjacent leaves. If all goes well we can avoid the double split
4125 static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4126 struct btrfs_root *root,
4127 struct btrfs_path *path,
4130 struct btrfs_fs_info *fs_info = root->fs_info;
4135 int space_needed = data_size;
4137 slot = path->slots[0];
4138 if (slot < btrfs_header_nritems(path->nodes[0]))
4139 space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
4142 * try to push all the items after our slot into the
4145 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
4152 nritems = btrfs_header_nritems(path->nodes[0]);
4154 * our goal is to get our slot at the start or end of a leaf. If
4155 * we've done so we're done
4157 if (path->slots[0] == 0 || path->slots[0] == nritems)
4160 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
4163 /* try to push all the items before our slot into the next leaf */
4164 slot = path->slots[0];
4165 space_needed = data_size;
4167 space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
4168 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
4181 * split the path's leaf in two, making sure there is at least data_size
4182 * available for the resulting leaf level of the path.
4184 * returns 0 if all went well and < 0 on failure.
4186 static noinline int split_leaf(struct btrfs_trans_handle *trans,
4187 struct btrfs_root *root,
4188 const struct btrfs_key *ins_key,
4189 struct btrfs_path *path, int data_size,
4192 struct btrfs_disk_key disk_key;
4193 struct extent_buffer *l;
4197 struct extent_buffer *right;
4198 struct btrfs_fs_info *fs_info = root->fs_info;
4202 int num_doubles = 0;
4203 int tried_avoid_double = 0;
4206 slot = path->slots[0];
4207 if (extend && data_size + btrfs_item_size_nr(l, slot) +
4208 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
4211 /* first try to make some room by pushing left and right */
4212 if (data_size && path->nodes[1]) {
4213 int space_needed = data_size;
4215 if (slot < btrfs_header_nritems(l))
4216 space_needed -= btrfs_leaf_free_space(fs_info, l);
4218 wret = push_leaf_right(trans, root, path, space_needed,
4219 space_needed, 0, 0);
4223 space_needed = data_size;
4225 space_needed -= btrfs_leaf_free_space(fs_info,
4227 wret = push_leaf_left(trans, root, path, space_needed,
4228 space_needed, 0, (u32)-1);
4234 /* did the pushes work? */
4235 if (btrfs_leaf_free_space(fs_info, l) >= data_size)
4239 if (!path->nodes[1]) {
4240 ret = insert_new_root(trans, root, path, 1);
4247 slot = path->slots[0];
4248 nritems = btrfs_header_nritems(l);
4249 mid = (nritems + 1) / 2;
4253 leaf_space_used(l, mid, nritems - mid) + data_size >
4254 BTRFS_LEAF_DATA_SIZE(fs_info)) {
4255 if (slot >= nritems) {
4259 if (mid != nritems &&
4260 leaf_space_used(l, mid, nritems - mid) +
4261 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
4262 if (data_size && !tried_avoid_double)
4263 goto push_for_double;
4269 if (leaf_space_used(l, 0, mid) + data_size >
4270 BTRFS_LEAF_DATA_SIZE(fs_info)) {
4271 if (!extend && data_size && slot == 0) {
4273 } else if ((extend || !data_size) && slot == 0) {
4277 if (mid != nritems &&
4278 leaf_space_used(l, mid, nritems - mid) +
4279 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
4280 if (data_size && !tried_avoid_double)
4281 goto push_for_double;
4289 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4291 btrfs_item_key(l, &disk_key, mid);
4293 right = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, 0,
4296 return PTR_ERR(right);
4298 root_add_used(root, fs_info->nodesize);
4302 btrfs_set_header_nritems(right, 0);
4303 insert_ptr(trans, fs_info, path, &disk_key,
4304 right->start, path->slots[1] + 1, 1);
4305 btrfs_tree_unlock(path->nodes[0]);
4306 free_extent_buffer(path->nodes[0]);
4307 path->nodes[0] = right;
4309 path->slots[1] += 1;
4311 btrfs_set_header_nritems(right, 0);
4312 insert_ptr(trans, fs_info, path, &disk_key,
4313 right->start, path->slots[1], 1);
4314 btrfs_tree_unlock(path->nodes[0]);
4315 free_extent_buffer(path->nodes[0]);
4316 path->nodes[0] = right;
4318 if (path->slots[1] == 0)
4319 fixup_low_keys(path, &disk_key, 1);
4322 * We create a new leaf 'right' for the required ins_len and
4323 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
4324 * the content of ins_len to 'right'.
4329 copy_for_split(trans, fs_info, path, l, right, slot, mid, nritems);
4332 BUG_ON(num_doubles != 0);
4340 push_for_double_split(trans, root, path, data_size);
4341 tried_avoid_double = 1;
4342 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
4347 static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4348 struct btrfs_root *root,
4349 struct btrfs_path *path, int ins_len)
4351 struct btrfs_fs_info *fs_info = root->fs_info;
4352 struct btrfs_key key;
4353 struct extent_buffer *leaf;
4354 struct btrfs_file_extent_item *fi;
4359 leaf = path->nodes[0];
4360 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4362 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4363 key.type != BTRFS_EXTENT_CSUM_KEY);
4365 if (btrfs_leaf_free_space(fs_info, leaf) >= ins_len)
4368 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4369 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4370 fi = btrfs_item_ptr(leaf, path->slots[0],
4371 struct btrfs_file_extent_item);
4372 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4374 btrfs_release_path(path);
4376 path->keep_locks = 1;
4377 path->search_for_split = 1;
4378 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4379 path->search_for_split = 0;
4386 leaf = path->nodes[0];
4387 /* if our item isn't there, return now */
4388 if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4391 /* the leaf has changed, it now has room. return now */
4392 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= ins_len)
4395 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4396 fi = btrfs_item_ptr(leaf, path->slots[0],
4397 struct btrfs_file_extent_item);
4398 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4402 btrfs_set_path_blocking(path);
4403 ret = split_leaf(trans, root, &key, path, ins_len, 1);
4407 path->keep_locks = 0;
4408 btrfs_unlock_up_safe(path, 1);
4411 path->keep_locks = 0;
4415 static noinline int split_item(struct btrfs_fs_info *fs_info,
4416 struct btrfs_path *path,
4417 const struct btrfs_key *new_key,
4418 unsigned long split_offset)
4420 struct extent_buffer *leaf;
4421 struct btrfs_item *item;
4422 struct btrfs_item *new_item;
4428 struct btrfs_disk_key disk_key;
4430 leaf = path->nodes[0];
4431 BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < sizeof(struct btrfs_item));
4433 btrfs_set_path_blocking(path);
4435 item = btrfs_item_nr(path->slots[0]);
4436 orig_offset = btrfs_item_offset(leaf, item);
4437 item_size = btrfs_item_size(leaf, item);
4439 buf = kmalloc(item_size, GFP_NOFS);
4443 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4444 path->slots[0]), item_size);
4446 slot = path->slots[0] + 1;
4447 nritems = btrfs_header_nritems(leaf);
4448 if (slot != nritems) {
4449 /* shift the items */
4450 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
4451 btrfs_item_nr_offset(slot),
4452 (nritems - slot) * sizeof(struct btrfs_item));
4455 btrfs_cpu_key_to_disk(&disk_key, new_key);
4456 btrfs_set_item_key(leaf, &disk_key, slot);
4458 new_item = btrfs_item_nr(slot);
4460 btrfs_set_item_offset(leaf, new_item, orig_offset);
4461 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4463 btrfs_set_item_offset(leaf, item,
4464 orig_offset + item_size - split_offset);
4465 btrfs_set_item_size(leaf, item, split_offset);
4467 btrfs_set_header_nritems(leaf, nritems + 1);
4469 /* write the data for the start of the original item */
4470 write_extent_buffer(leaf, buf,
4471 btrfs_item_ptr_offset(leaf, path->slots[0]),
4474 /* write the data for the new item */
4475 write_extent_buffer(leaf, buf + split_offset,
4476 btrfs_item_ptr_offset(leaf, slot),
4477 item_size - split_offset);
4478 btrfs_mark_buffer_dirty(leaf);
4480 BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < 0);
4486 * This function splits a single item into two items,
4487 * giving 'new_key' to the new item and splitting the
4488 * old one at split_offset (from the start of the item).
4490 * The path may be released by this operation. After
4491 * the split, the path is pointing to the old item. The
4492 * new item is going to be in the same node as the old one.
4494 * Note, the item being split must be smaller enough to live alone on
4495 * a tree block with room for one extra struct btrfs_item
4497 * This allows us to split the item in place, keeping a lock on the
4498 * leaf the entire time.
4500 int btrfs_split_item(struct btrfs_trans_handle *trans,
4501 struct btrfs_root *root,
4502 struct btrfs_path *path,
4503 const struct btrfs_key *new_key,
4504 unsigned long split_offset)
4507 ret = setup_leaf_for_split(trans, root, path,
4508 sizeof(struct btrfs_item));
4512 ret = split_item(root->fs_info, path, new_key, split_offset);
4517 * This function duplicate a item, giving 'new_key' to the new item.
4518 * It guarantees both items live in the same tree leaf and the new item
4519 * is contiguous with the original item.
4521 * This allows us to split file extent in place, keeping a lock on the
4522 * leaf the entire time.
4524 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4525 struct btrfs_root *root,
4526 struct btrfs_path *path,
4527 const struct btrfs_key *new_key)
4529 struct extent_buffer *leaf;
4533 leaf = path->nodes[0];
4534 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4535 ret = setup_leaf_for_split(trans, root, path,
4536 item_size + sizeof(struct btrfs_item));
4541 setup_items_for_insert(root, path, new_key, &item_size,
4542 item_size, item_size +
4543 sizeof(struct btrfs_item), 1);
4544 leaf = path->nodes[0];
4545 memcpy_extent_buffer(leaf,
4546 btrfs_item_ptr_offset(leaf, path->slots[0]),
4547 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4553 * make the item pointed to by the path smaller. new_size indicates
4554 * how small to make it, and from_end tells us if we just chop bytes
4555 * off the end of the item or if we shift the item to chop bytes off
4558 void btrfs_truncate_item(struct btrfs_fs_info *fs_info,
4559 struct btrfs_path *path, u32 new_size, int from_end)
4562 struct extent_buffer *leaf;
4563 struct btrfs_item *item;
4565 unsigned int data_end;
4566 unsigned int old_data_start;
4567 unsigned int old_size;
4568 unsigned int size_diff;
4570 struct btrfs_map_token token;
4572 btrfs_init_map_token(&token);
4574 leaf = path->nodes[0];
4575 slot = path->slots[0];
4577 old_size = btrfs_item_size_nr(leaf, slot);
4578 if (old_size == new_size)
4581 nritems = btrfs_header_nritems(leaf);
4582 data_end = leaf_data_end(fs_info, leaf);
4584 old_data_start = btrfs_item_offset_nr(leaf, slot);
4586 size_diff = old_size - new_size;
4589 BUG_ON(slot >= nritems);
4592 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4594 /* first correct the data pointers */
4595 for (i = slot; i < nritems; i++) {
4597 item = btrfs_item_nr(i);
4599 ioff = btrfs_token_item_offset(leaf, item, &token);
4600 btrfs_set_token_item_offset(leaf, item,
4601 ioff + size_diff, &token);
4604 /* shift the data */
4606 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4607 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
4608 data_end, old_data_start + new_size - data_end);
4610 struct btrfs_disk_key disk_key;
4613 btrfs_item_key(leaf, &disk_key, slot);
4615 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4617 struct btrfs_file_extent_item *fi;
4619 fi = btrfs_item_ptr(leaf, slot,
4620 struct btrfs_file_extent_item);
4621 fi = (struct btrfs_file_extent_item *)(
4622 (unsigned long)fi - size_diff);
4624 if (btrfs_file_extent_type(leaf, fi) ==
4625 BTRFS_FILE_EXTENT_INLINE) {
4626 ptr = btrfs_item_ptr_offset(leaf, slot);
4627 memmove_extent_buffer(leaf, ptr,
4629 BTRFS_FILE_EXTENT_INLINE_DATA_START);
4633 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4634 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
4635 data_end, old_data_start - data_end);
4637 offset = btrfs_disk_key_offset(&disk_key);
4638 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4639 btrfs_set_item_key(leaf, &disk_key, slot);
4641 fixup_low_keys(path, &disk_key, 1);
4644 item = btrfs_item_nr(slot);
4645 btrfs_set_item_size(leaf, item, new_size);
4646 btrfs_mark_buffer_dirty(leaf);
4648 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
4649 btrfs_print_leaf(leaf);
4655 * make the item pointed to by the path bigger, data_size is the added size.
4657 void btrfs_extend_item(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
4661 struct extent_buffer *leaf;
4662 struct btrfs_item *item;
4664 unsigned int data_end;
4665 unsigned int old_data;
4666 unsigned int old_size;
4668 struct btrfs_map_token token;
4670 btrfs_init_map_token(&token);
4672 leaf = path->nodes[0];
4674 nritems = btrfs_header_nritems(leaf);
4675 data_end = leaf_data_end(fs_info, leaf);
4677 if (btrfs_leaf_free_space(fs_info, leaf) < data_size) {
4678 btrfs_print_leaf(leaf);
4681 slot = path->slots[0];
4682 old_data = btrfs_item_end_nr(leaf, slot);
4685 if (slot >= nritems) {
4686 btrfs_print_leaf(leaf);
4687 btrfs_crit(fs_info, "slot %d too large, nritems %d",
4693 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4695 /* first correct the data pointers */
4696 for (i = slot; i < nritems; i++) {
4698 item = btrfs_item_nr(i);
4700 ioff = btrfs_token_item_offset(leaf, item, &token);
4701 btrfs_set_token_item_offset(leaf, item,
4702 ioff - data_size, &token);
4705 /* shift the data */
4706 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4707 data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
4708 data_end, old_data - data_end);
4710 data_end = old_data;
4711 old_size = btrfs_item_size_nr(leaf, slot);
4712 item = btrfs_item_nr(slot);
4713 btrfs_set_item_size(leaf, item, old_size + data_size);
4714 btrfs_mark_buffer_dirty(leaf);
4716 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
4717 btrfs_print_leaf(leaf);
4723 * this is a helper for btrfs_insert_empty_items, the main goal here is
4724 * to save stack depth by doing the bulk of the work in a function
4725 * that doesn't call btrfs_search_slot
4727 void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
4728 const struct btrfs_key *cpu_key, u32 *data_size,
4729 u32 total_data, u32 total_size, int nr)
4731 struct btrfs_fs_info *fs_info = root->fs_info;
4732 struct btrfs_item *item;
4735 unsigned int data_end;
4736 struct btrfs_disk_key disk_key;
4737 struct extent_buffer *leaf;
4739 struct btrfs_map_token token;
4741 if (path->slots[0] == 0) {
4742 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
4743 fixup_low_keys(path, &disk_key, 1);
4745 btrfs_unlock_up_safe(path, 1);
4747 btrfs_init_map_token(&token);
4749 leaf = path->nodes[0];
4750 slot = path->slots[0];
4752 nritems = btrfs_header_nritems(leaf);
4753 data_end = leaf_data_end(fs_info, leaf);
4755 if (btrfs_leaf_free_space(fs_info, leaf) < total_size) {
4756 btrfs_print_leaf(leaf);
4757 btrfs_crit(fs_info, "not enough freespace need %u have %d",
4758 total_size, btrfs_leaf_free_space(fs_info, leaf));
4762 if (slot != nritems) {
4763 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4765 if (old_data < data_end) {
4766 btrfs_print_leaf(leaf);
4767 btrfs_crit(fs_info, "slot %d old_data %d data_end %d",
4768 slot, old_data, data_end);
4772 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4774 /* first correct the data pointers */
4775 for (i = slot; i < nritems; i++) {
4778 item = btrfs_item_nr(i);
4779 ioff = btrfs_token_item_offset(leaf, item, &token);
4780 btrfs_set_token_item_offset(leaf, item,
4781 ioff - total_data, &token);
4783 /* shift the items */
4784 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4785 btrfs_item_nr_offset(slot),
4786 (nritems - slot) * sizeof(struct btrfs_item));
4788 /* shift the data */
4789 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4790 data_end - total_data, BTRFS_LEAF_DATA_OFFSET +
4791 data_end, old_data - data_end);
4792 data_end = old_data;
4795 /* setup the item for the new data */
4796 for (i = 0; i < nr; i++) {
4797 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4798 btrfs_set_item_key(leaf, &disk_key, slot + i);
4799 item = btrfs_item_nr(slot + i);
4800 btrfs_set_token_item_offset(leaf, item,
4801 data_end - data_size[i], &token);
4802 data_end -= data_size[i];
4803 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
4806 btrfs_set_header_nritems(leaf, nritems + nr);
4807 btrfs_mark_buffer_dirty(leaf);
4809 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
4810 btrfs_print_leaf(leaf);
4816 * Given a key and some data, insert items into the tree.
4817 * This does all the path init required, making room in the tree if needed.
4819 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4820 struct btrfs_root *root,
4821 struct btrfs_path *path,
4822 const struct btrfs_key *cpu_key, u32 *data_size,
4831 for (i = 0; i < nr; i++)
4832 total_data += data_size[i];
4834 total_size = total_data + (nr * sizeof(struct btrfs_item));
4835 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4841 slot = path->slots[0];
4844 setup_items_for_insert(root, path, cpu_key, data_size,
4845 total_data, total_size, nr);
4850 * Given a key and some data, insert an item into the tree.
4851 * This does all the path init required, making room in the tree if needed.
4853 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4854 const struct btrfs_key *cpu_key, void *data,
4858 struct btrfs_path *path;
4859 struct extent_buffer *leaf;
4862 path = btrfs_alloc_path();
4865 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
4867 leaf = path->nodes[0];
4868 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4869 write_extent_buffer(leaf, data, ptr, data_size);
4870 btrfs_mark_buffer_dirty(leaf);
4872 btrfs_free_path(path);
4877 * delete the pointer from a given node.
4879 * the tree should have been previously balanced so the deletion does not
4882 static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4883 int level, int slot)
4885 struct extent_buffer *parent = path->nodes[level];
4889 nritems = btrfs_header_nritems(parent);
4890 if (slot != nritems - 1) {
4892 ret = tree_mod_log_insert_move(parent, slot, slot + 1,
4893 nritems - slot - 1);
4896 memmove_extent_buffer(parent,
4897 btrfs_node_key_ptr_offset(slot),
4898 btrfs_node_key_ptr_offset(slot + 1),
4899 sizeof(struct btrfs_key_ptr) *
4900 (nritems - slot - 1));
4902 ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE,
4908 btrfs_set_header_nritems(parent, nritems);
4909 if (nritems == 0 && parent == root->node) {
4910 BUG_ON(btrfs_header_level(root->node) != 1);
4911 /* just turn the root into a leaf and break */
4912 btrfs_set_header_level(root->node, 0);
4913 } else if (slot == 0) {
4914 struct btrfs_disk_key disk_key;
4916 btrfs_node_key(parent, &disk_key, 0);
4917 fixup_low_keys(path, &disk_key, level + 1);
4919 btrfs_mark_buffer_dirty(parent);
4923 * a helper function to delete the leaf pointed to by path->slots[1] and
4926 * This deletes the pointer in path->nodes[1] and frees the leaf
4927 * block extent. zero is returned if it all worked out, < 0 otherwise.
4929 * The path must have already been setup for deleting the leaf, including
4930 * all the proper balancing. path->nodes[1] must be locked.
4932 static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4933 struct btrfs_root *root,
4934 struct btrfs_path *path,
4935 struct extent_buffer *leaf)
4937 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
4938 del_ptr(root, path, 1, path->slots[1]);
4941 * btrfs_free_extent is expensive, we want to make sure we
4942 * aren't holding any locks when we call it
4944 btrfs_unlock_up_safe(path, 0);
4946 root_sub_used(root, leaf->len);
4948 extent_buffer_get(leaf);
4949 btrfs_free_tree_block(trans, root, leaf, 0, 1);
4950 free_extent_buffer_stale(leaf);
4953 * delete the item at the leaf level in path. If that empties
4954 * the leaf, remove it from the tree
4956 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4957 struct btrfs_path *path, int slot, int nr)
4959 struct btrfs_fs_info *fs_info = root->fs_info;
4960 struct extent_buffer *leaf;
4961 struct btrfs_item *item;
4968 struct btrfs_map_token token;
4970 btrfs_init_map_token(&token);
4972 leaf = path->nodes[0];
4973 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4975 for (i = 0; i < nr; i++)
4976 dsize += btrfs_item_size_nr(leaf, slot + i);
4978 nritems = btrfs_header_nritems(leaf);
4980 if (slot + nr != nritems) {
4981 int data_end = leaf_data_end(fs_info, leaf);
4983 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4985 BTRFS_LEAF_DATA_OFFSET + data_end,
4986 last_off - data_end);
4988 for (i = slot + nr; i < nritems; i++) {
4991 item = btrfs_item_nr(i);
4992 ioff = btrfs_token_item_offset(leaf, item, &token);
4993 btrfs_set_token_item_offset(leaf, item,
4994 ioff + dsize, &token);
4997 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
4998 btrfs_item_nr_offset(slot + nr),
4999 sizeof(struct btrfs_item) *
5000 (nritems - slot - nr));
5002 btrfs_set_header_nritems(leaf, nritems - nr);
5005 /* delete the leaf if we've emptied it */
5007 if (leaf == root->node) {
5008 btrfs_set_header_level(leaf, 0);
5010 btrfs_set_path_blocking(path);
5011 clean_tree_block(fs_info, leaf);
5012 btrfs_del_leaf(trans, root, path, leaf);
5015 int used = leaf_space_used(leaf, 0, nritems);
5017 struct btrfs_disk_key disk_key;
5019 btrfs_item_key(leaf, &disk_key, 0);
5020 fixup_low_keys(path, &disk_key, 1);
5023 /* delete the leaf if it is mostly empty */
5024 if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
5025 /* push_leaf_left fixes the path.
5026 * make sure the path still points to our leaf
5027 * for possible call to del_ptr below
5029 slot = path->slots[1];
5030 extent_buffer_get(leaf);
5032 btrfs_set_path_blocking(path);
5033 wret = push_leaf_left(trans, root, path, 1, 1,
5035 if (wret < 0 && wret != -ENOSPC)
5038 if (path->nodes[0] == leaf &&
5039 btrfs_header_nritems(leaf)) {
5040 wret = push_leaf_right(trans, root, path, 1,
5042 if (wret < 0 && wret != -ENOSPC)
5046 if (btrfs_header_nritems(leaf) == 0) {
5047 path->slots[1] = slot;
5048 btrfs_del_leaf(trans, root, path, leaf);
5049 free_extent_buffer(leaf);
5052 /* if we're still in the path, make sure
5053 * we're dirty. Otherwise, one of the
5054 * push_leaf functions must have already
5055 * dirtied this buffer
5057 if (path->nodes[0] == leaf)
5058 btrfs_mark_buffer_dirty(leaf);
5059 free_extent_buffer(leaf);
5062 btrfs_mark_buffer_dirty(leaf);
5069 * search the tree again to find a leaf with lesser keys
5070 * returns 0 if it found something or 1 if there are no lesser leaves.
5071 * returns < 0 on io errors.
5073 * This may release the path, and so you may lose any locks held at the
5076 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
5078 struct btrfs_key key;
5079 struct btrfs_disk_key found_key;
5082 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
5084 if (key.offset > 0) {
5086 } else if (key.type > 0) {
5088 key.offset = (u64)-1;
5089 } else if (key.objectid > 0) {
5092 key.offset = (u64)-1;
5097 btrfs_release_path(path);
5098 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5101 btrfs_item_key(path->nodes[0], &found_key, 0);
5102 ret = comp_keys(&found_key, &key);
5104 * We might have had an item with the previous key in the tree right
5105 * before we released our path. And after we released our path, that
5106 * item might have been pushed to the first slot (0) of the leaf we
5107 * were holding due to a tree balance. Alternatively, an item with the
5108 * previous key can exist as the only element of a leaf (big fat item).
5109 * Therefore account for these 2 cases, so that our callers (like
5110 * btrfs_previous_item) don't miss an existing item with a key matching
5111 * the previous key we computed above.
5119 * A helper function to walk down the tree starting at min_key, and looking
5120 * for nodes or leaves that are have a minimum transaction id.
5121 * This is used by the btree defrag code, and tree logging
5123 * This does not cow, but it does stuff the starting key it finds back
5124 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5125 * key and get a writable path.
5127 * This honors path->lowest_level to prevent descent past a given level
5130 * min_trans indicates the oldest transaction that you are interested
5131 * in walking through. Any nodes or leaves older than min_trans are
5132 * skipped over (without reading them).
5134 * returns zero if something useful was found, < 0 on error and 1 if there
5135 * was nothing in the tree that matched the search criteria.
5137 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
5138 struct btrfs_path *path,
5141 struct btrfs_fs_info *fs_info = root->fs_info;
5142 struct extent_buffer *cur;
5143 struct btrfs_key found_key;
5149 int keep_locks = path->keep_locks;
5151 path->keep_locks = 1;
5153 cur = btrfs_read_lock_root_node(root);
5154 level = btrfs_header_level(cur);
5155 WARN_ON(path->nodes[level]);
5156 path->nodes[level] = cur;
5157 path->locks[level] = BTRFS_READ_LOCK;
5159 if (btrfs_header_generation(cur) < min_trans) {
5164 nritems = btrfs_header_nritems(cur);
5165 level = btrfs_header_level(cur);
5166 sret = btrfs_bin_search(cur, min_key, level, &slot);
5168 /* at the lowest level, we're done, setup the path and exit */
5169 if (level == path->lowest_level) {
5170 if (slot >= nritems)
5173 path->slots[level] = slot;
5174 btrfs_item_key_to_cpu(cur, &found_key, slot);
5177 if (sret && slot > 0)
5180 * check this node pointer against the min_trans parameters.
5181 * If it is too old, old, skip to the next one.
5183 while (slot < nritems) {
5186 gen = btrfs_node_ptr_generation(cur, slot);
5187 if (gen < min_trans) {
5195 * we didn't find a candidate key in this node, walk forward
5196 * and find another one
5198 if (slot >= nritems) {
5199 path->slots[level] = slot;
5200 btrfs_set_path_blocking(path);
5201 sret = btrfs_find_next_key(root, path, min_key, level,
5204 btrfs_release_path(path);
5210 /* save our key for returning back */
5211 btrfs_node_key_to_cpu(cur, &found_key, slot);
5212 path->slots[level] = slot;
5213 if (level == path->lowest_level) {
5217 btrfs_set_path_blocking(path);
5218 cur = read_node_slot(fs_info, cur, slot);
5224 btrfs_tree_read_lock(cur);
5226 path->locks[level - 1] = BTRFS_READ_LOCK;
5227 path->nodes[level - 1] = cur;
5228 unlock_up(path, level, 1, 0, NULL);
5231 path->keep_locks = keep_locks;
5233 btrfs_unlock_up_safe(path, path->lowest_level + 1);
5234 btrfs_set_path_blocking(path);
5235 memcpy(min_key, &found_key, sizeof(found_key));
5240 static int tree_move_down(struct btrfs_fs_info *fs_info,
5241 struct btrfs_path *path,
5244 struct extent_buffer *eb;
5246 BUG_ON(*level == 0);
5247 eb = read_node_slot(fs_info, path->nodes[*level], path->slots[*level]);
5251 path->nodes[*level - 1] = eb;
5252 path->slots[*level - 1] = 0;
5257 static int tree_move_next_or_upnext(struct btrfs_path *path,
5258 int *level, int root_level)
5262 nritems = btrfs_header_nritems(path->nodes[*level]);
5264 path->slots[*level]++;
5266 while (path->slots[*level] >= nritems) {
5267 if (*level == root_level)
5271 path->slots[*level] = 0;
5272 free_extent_buffer(path->nodes[*level]);
5273 path->nodes[*level] = NULL;
5275 path->slots[*level]++;
5277 nritems = btrfs_header_nritems(path->nodes[*level]);
5284 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5287 static int tree_advance(struct btrfs_fs_info *fs_info,
5288 struct btrfs_path *path,
5289 int *level, int root_level,
5291 struct btrfs_key *key)
5295 if (*level == 0 || !allow_down) {
5296 ret = tree_move_next_or_upnext(path, level, root_level);
5298 ret = tree_move_down(fs_info, path, level);
5302 btrfs_item_key_to_cpu(path->nodes[*level], key,
5303 path->slots[*level]);
5305 btrfs_node_key_to_cpu(path->nodes[*level], key,
5306 path->slots[*level]);
5311 static int tree_compare_item(struct btrfs_path *left_path,
5312 struct btrfs_path *right_path,
5317 unsigned long off1, off2;
5319 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5320 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5324 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5325 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5326 right_path->slots[0]);
5328 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5330 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5337 #define ADVANCE_ONLY_NEXT -1
5340 * This function compares two trees and calls the provided callback for
5341 * every changed/new/deleted item it finds.
5342 * If shared tree blocks are encountered, whole subtrees are skipped, making
5343 * the compare pretty fast on snapshotted subvolumes.
5345 * This currently works on commit roots only. As commit roots are read only,
5346 * we don't do any locking. The commit roots are protected with transactions.
5347 * Transactions are ended and rejoined when a commit is tried in between.
5349 * This function checks for modifications done to the trees while comparing.
5350 * If it detects a change, it aborts immediately.
5352 int btrfs_compare_trees(struct btrfs_root *left_root,
5353 struct btrfs_root *right_root,
5354 btrfs_changed_cb_t changed_cb, void *ctx)
5356 struct btrfs_fs_info *fs_info = left_root->fs_info;
5359 struct btrfs_path *left_path = NULL;
5360 struct btrfs_path *right_path = NULL;
5361 struct btrfs_key left_key;
5362 struct btrfs_key right_key;
5363 char *tmp_buf = NULL;
5364 int left_root_level;
5365 int right_root_level;
5368 int left_end_reached;
5369 int right_end_reached;
5377 left_path = btrfs_alloc_path();
5382 right_path = btrfs_alloc_path();
5388 tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
5394 left_path->search_commit_root = 1;
5395 left_path->skip_locking = 1;
5396 right_path->search_commit_root = 1;
5397 right_path->skip_locking = 1;
5400 * Strategy: Go to the first items of both trees. Then do
5402 * If both trees are at level 0
5403 * Compare keys of current items
5404 * If left < right treat left item as new, advance left tree
5406 * If left > right treat right item as deleted, advance right tree
5408 * If left == right do deep compare of items, treat as changed if
5409 * needed, advance both trees and repeat
5410 * If both trees are at the same level but not at level 0
5411 * Compare keys of current nodes/leafs
5412 * If left < right advance left tree and repeat
5413 * If left > right advance right tree and repeat
5414 * If left == right compare blockptrs of the next nodes/leafs
5415 * If they match advance both trees but stay at the same level
5417 * If they don't match advance both trees while allowing to go
5419 * If tree levels are different
5420 * Advance the tree that needs it and repeat
5422 * Advancing a tree means:
5423 * If we are at level 0, try to go to the next slot. If that's not
5424 * possible, go one level up and repeat. Stop when we found a level
5425 * where we could go to the next slot. We may at this point be on a
5428 * If we are not at level 0 and not on shared tree blocks, go one
5431 * If we are not at level 0 and on shared tree blocks, go one slot to
5432 * the right if possible or go up and right.
5435 down_read(&fs_info->commit_root_sem);
5436 left_level = btrfs_header_level(left_root->commit_root);
5437 left_root_level = left_level;
5438 left_path->nodes[left_level] =
5439 btrfs_clone_extent_buffer(left_root->commit_root);
5440 if (!left_path->nodes[left_level]) {
5441 up_read(&fs_info->commit_root_sem);
5446 right_level = btrfs_header_level(right_root->commit_root);
5447 right_root_level = right_level;
5448 right_path->nodes[right_level] =
5449 btrfs_clone_extent_buffer(right_root->commit_root);
5450 if (!right_path->nodes[right_level]) {
5451 up_read(&fs_info->commit_root_sem);
5455 up_read(&fs_info->commit_root_sem);
5457 if (left_level == 0)
5458 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5459 &left_key, left_path->slots[left_level]);
5461 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5462 &left_key, left_path->slots[left_level]);
5463 if (right_level == 0)
5464 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5465 &right_key, right_path->slots[right_level]);
5467 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5468 &right_key, right_path->slots[right_level]);
5470 left_end_reached = right_end_reached = 0;
5471 advance_left = advance_right = 0;
5474 if (advance_left && !left_end_reached) {
5475 ret = tree_advance(fs_info, left_path, &left_level,
5477 advance_left != ADVANCE_ONLY_NEXT,
5480 left_end_reached = ADVANCE;
5485 if (advance_right && !right_end_reached) {
5486 ret = tree_advance(fs_info, right_path, &right_level,
5488 advance_right != ADVANCE_ONLY_NEXT,
5491 right_end_reached = ADVANCE;
5497 if (left_end_reached && right_end_reached) {
5500 } else if (left_end_reached) {
5501 if (right_level == 0) {
5502 ret = changed_cb(left_path, right_path,
5504 BTRFS_COMPARE_TREE_DELETED,
5509 advance_right = ADVANCE;
5511 } else if (right_end_reached) {
5512 if (left_level == 0) {
5513 ret = changed_cb(left_path, right_path,
5515 BTRFS_COMPARE_TREE_NEW,
5520 advance_left = ADVANCE;
5524 if (left_level == 0 && right_level == 0) {
5525 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5527 ret = changed_cb(left_path, right_path,
5529 BTRFS_COMPARE_TREE_NEW,
5533 advance_left = ADVANCE;
5534 } else if (cmp > 0) {
5535 ret = changed_cb(left_path, right_path,
5537 BTRFS_COMPARE_TREE_DELETED,
5541 advance_right = ADVANCE;
5543 enum btrfs_compare_tree_result result;
5545 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
5546 ret = tree_compare_item(left_path, right_path,
5549 result = BTRFS_COMPARE_TREE_CHANGED;
5551 result = BTRFS_COMPARE_TREE_SAME;
5552 ret = changed_cb(left_path, right_path,
5553 &left_key, result, ctx);
5556 advance_left = ADVANCE;
5557 advance_right = ADVANCE;
5559 } else if (left_level == right_level) {
5560 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5562 advance_left = ADVANCE;
5563 } else if (cmp > 0) {
5564 advance_right = ADVANCE;
5566 left_blockptr = btrfs_node_blockptr(
5567 left_path->nodes[left_level],
5568 left_path->slots[left_level]);
5569 right_blockptr = btrfs_node_blockptr(
5570 right_path->nodes[right_level],
5571 right_path->slots[right_level]);
5572 left_gen = btrfs_node_ptr_generation(
5573 left_path->nodes[left_level],
5574 left_path->slots[left_level]);
5575 right_gen = btrfs_node_ptr_generation(
5576 right_path->nodes[right_level],
5577 right_path->slots[right_level]);
5578 if (left_blockptr == right_blockptr &&
5579 left_gen == right_gen) {
5581 * As we're on a shared block, don't
5582 * allow to go deeper.
5584 advance_left = ADVANCE_ONLY_NEXT;
5585 advance_right = ADVANCE_ONLY_NEXT;
5587 advance_left = ADVANCE;
5588 advance_right = ADVANCE;
5591 } else if (left_level < right_level) {
5592 advance_right = ADVANCE;
5594 advance_left = ADVANCE;
5599 btrfs_free_path(left_path);
5600 btrfs_free_path(right_path);
5606 * this is similar to btrfs_next_leaf, but does not try to preserve
5607 * and fixup the path. It looks for and returns the next key in the
5608 * tree based on the current path and the min_trans parameters.
5610 * 0 is returned if another key is found, < 0 if there are any errors
5611 * and 1 is returned if there are no higher keys in the tree
5613 * path->keep_locks should be set to 1 on the search made before
5614 * calling this function.
5616 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
5617 struct btrfs_key *key, int level, u64 min_trans)
5620 struct extent_buffer *c;
5622 WARN_ON(!path->keep_locks);
5623 while (level < BTRFS_MAX_LEVEL) {
5624 if (!path->nodes[level])
5627 slot = path->slots[level] + 1;
5628 c = path->nodes[level];
5630 if (slot >= btrfs_header_nritems(c)) {
5633 struct btrfs_key cur_key;
5634 if (level + 1 >= BTRFS_MAX_LEVEL ||
5635 !path->nodes[level + 1])
5638 if (path->locks[level + 1]) {
5643 slot = btrfs_header_nritems(c) - 1;
5645 btrfs_item_key_to_cpu(c, &cur_key, slot);
5647 btrfs_node_key_to_cpu(c, &cur_key, slot);
5649 orig_lowest = path->lowest_level;
5650 btrfs_release_path(path);
5651 path->lowest_level = level;
5652 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5654 path->lowest_level = orig_lowest;
5658 c = path->nodes[level];
5659 slot = path->slots[level];
5666 btrfs_item_key_to_cpu(c, key, slot);
5668 u64 gen = btrfs_node_ptr_generation(c, slot);
5670 if (gen < min_trans) {
5674 btrfs_node_key_to_cpu(c, key, slot);
5682 * search the tree again to find a leaf with greater keys
5683 * returns 0 if it found something or 1 if there are no greater leaves.
5684 * returns < 0 on io errors.
5686 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
5688 return btrfs_next_old_leaf(root, path, 0);
5691 int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5696 struct extent_buffer *c;
5697 struct extent_buffer *next;
5698 struct btrfs_key key;
5701 int old_spinning = path->leave_spinning;
5702 int next_rw_lock = 0;
5704 nritems = btrfs_header_nritems(path->nodes[0]);
5708 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5713 btrfs_release_path(path);
5715 path->keep_locks = 1;
5716 path->leave_spinning = 1;
5719 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5721 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5722 path->keep_locks = 0;
5727 nritems = btrfs_header_nritems(path->nodes[0]);
5729 * by releasing the path above we dropped all our locks. A balance
5730 * could have added more items next to the key that used to be
5731 * at the very end of the block. So, check again here and
5732 * advance the path if there are now more items available.
5734 if (nritems > 0 && path->slots[0] < nritems - 1) {
5741 * So the above check misses one case:
5742 * - after releasing the path above, someone has removed the item that
5743 * used to be at the very end of the block, and balance between leafs
5744 * gets another one with bigger key.offset to replace it.
5746 * This one should be returned as well, or we can get leaf corruption
5747 * later(esp. in __btrfs_drop_extents()).
5749 * And a bit more explanation about this check,
5750 * with ret > 0, the key isn't found, the path points to the slot
5751 * where it should be inserted, so the path->slots[0] item must be the
5754 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
5759 while (level < BTRFS_MAX_LEVEL) {
5760 if (!path->nodes[level]) {
5765 slot = path->slots[level] + 1;
5766 c = path->nodes[level];
5767 if (slot >= btrfs_header_nritems(c)) {
5769 if (level == BTRFS_MAX_LEVEL) {
5777 btrfs_tree_unlock_rw(next, next_rw_lock);
5778 free_extent_buffer(next);
5782 next_rw_lock = path->locks[level];
5783 ret = read_block_for_search(root, path, &next, level,
5789 btrfs_release_path(path);
5793 if (!path->skip_locking) {
5794 ret = btrfs_try_tree_read_lock(next);
5795 if (!ret && time_seq) {
5797 * If we don't get the lock, we may be racing
5798 * with push_leaf_left, holding that lock while
5799 * itself waiting for the leaf we've currently
5800 * locked. To solve this situation, we give up
5801 * on our lock and cycle.
5803 free_extent_buffer(next);
5804 btrfs_release_path(path);
5809 btrfs_set_path_blocking(path);
5810 btrfs_tree_read_lock(next);
5812 next_rw_lock = BTRFS_READ_LOCK;
5816 path->slots[level] = slot;
5819 c = path->nodes[level];
5820 if (path->locks[level])
5821 btrfs_tree_unlock_rw(c, path->locks[level]);
5823 free_extent_buffer(c);
5824 path->nodes[level] = next;
5825 path->slots[level] = 0;
5826 if (!path->skip_locking)
5827 path->locks[level] = next_rw_lock;
5831 ret = read_block_for_search(root, path, &next, level,
5837 btrfs_release_path(path);
5841 if (!path->skip_locking) {
5842 ret = btrfs_try_tree_read_lock(next);
5844 btrfs_set_path_blocking(path);
5845 btrfs_tree_read_lock(next);
5847 next_rw_lock = BTRFS_READ_LOCK;
5852 unlock_up(path, 0, 1, 0, NULL);
5853 path->leave_spinning = old_spinning;
5855 btrfs_set_path_blocking(path);
5861 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5862 * searching until it gets past min_objectid or finds an item of 'type'
5864 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5866 int btrfs_previous_item(struct btrfs_root *root,
5867 struct btrfs_path *path, u64 min_objectid,
5870 struct btrfs_key found_key;
5871 struct extent_buffer *leaf;
5876 if (path->slots[0] == 0) {
5877 btrfs_set_path_blocking(path);
5878 ret = btrfs_prev_leaf(root, path);
5884 leaf = path->nodes[0];
5885 nritems = btrfs_header_nritems(leaf);
5888 if (path->slots[0] == nritems)
5891 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5892 if (found_key.objectid < min_objectid)
5894 if (found_key.type == type)
5896 if (found_key.objectid == min_objectid &&
5897 found_key.type < type)
5904 * search in extent tree to find a previous Metadata/Data extent item with
5907 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5909 int btrfs_previous_extent_item(struct btrfs_root *root,
5910 struct btrfs_path *path, u64 min_objectid)
5912 struct btrfs_key found_key;
5913 struct extent_buffer *leaf;
5918 if (path->slots[0] == 0) {
5919 btrfs_set_path_blocking(path);
5920 ret = btrfs_prev_leaf(root, path);
5926 leaf = path->nodes[0];
5927 nritems = btrfs_header_nritems(leaf);
5930 if (path->slots[0] == nritems)
5933 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5934 if (found_key.objectid < min_objectid)
5936 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5937 found_key.type == BTRFS_METADATA_ITEM_KEY)
5939 if (found_key.objectid == min_objectid &&
5940 found_key.type < BTRFS_EXTENT_ITEM_KEY)