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"
17 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
18 *root, struct btrfs_path *path, int level);
19 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
20 const struct btrfs_key *ins_key, struct btrfs_path *path,
21 int data_size, int extend);
22 static int push_node_left(struct btrfs_trans_handle *trans,
23 struct btrfs_fs_info *fs_info,
24 struct extent_buffer *dst,
25 struct extent_buffer *src, int empty);
26 static int balance_node_right(struct btrfs_trans_handle *trans,
27 struct btrfs_fs_info *fs_info,
28 struct extent_buffer *dst_buf,
29 struct extent_buffer *src_buf);
30 static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
33 struct btrfs_path *btrfs_alloc_path(void)
35 return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
39 * set all locked nodes in the path to blocking locks. This should
40 * be done before scheduling
42 noinline void btrfs_set_path_blocking(struct btrfs_path *p)
45 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
46 if (!p->nodes[i] || !p->locks[i])
48 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
49 if (p->locks[i] == BTRFS_READ_LOCK)
50 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
51 else if (p->locks[i] == BTRFS_WRITE_LOCK)
52 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
56 /* this also releases the path */
57 void btrfs_free_path(struct btrfs_path *p)
61 btrfs_release_path(p);
62 kmem_cache_free(btrfs_path_cachep, p);
66 * path release drops references on the extent buffers in the path
67 * and it drops any locks held by this path
69 * It is safe to call this on paths that no locks or extent buffers held.
71 noinline void btrfs_release_path(struct btrfs_path *p)
75 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
80 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
83 free_extent_buffer(p->nodes[i]);
89 * safely gets a reference on the root node of a tree. A lock
90 * is not taken, so a concurrent writer may put a different node
91 * at the root of the tree. See btrfs_lock_root_node for the
94 * The extent buffer returned by this has a reference taken, so
95 * it won't disappear. It may stop being the root of the tree
96 * at any time because there are no locks held.
98 struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
100 struct extent_buffer *eb;
104 eb = rcu_dereference(root->node);
107 * RCU really hurts here, we could free up the root node because
108 * it was COWed but we may not get the new root node yet so do
109 * the inc_not_zero dance and if it doesn't work then
110 * synchronize_rcu and try again.
112 if (atomic_inc_not_zero(&eb->refs)) {
122 /* loop around taking references on and locking the root node of the
123 * tree until you end up with a lock on the root. A locked buffer
124 * is returned, with a reference held.
126 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
128 struct extent_buffer *eb;
131 eb = btrfs_root_node(root);
133 if (eb == root->node)
135 btrfs_tree_unlock(eb);
136 free_extent_buffer(eb);
141 /* loop around taking references on and locking the root node of the
142 * tree until you end up with a lock on the root. A locked buffer
143 * is returned, with a reference held.
145 struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
147 struct extent_buffer *eb;
150 eb = btrfs_root_node(root);
151 btrfs_tree_read_lock(eb);
152 if (eb == root->node)
154 btrfs_tree_read_unlock(eb);
155 free_extent_buffer(eb);
160 /* cowonly root (everything not a reference counted cow subvolume), just get
161 * put onto a simple dirty list. transaction.c walks this to make sure they
162 * get properly updated on disk.
164 static void add_root_to_dirty_list(struct btrfs_root *root)
166 struct btrfs_fs_info *fs_info = root->fs_info;
168 if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
169 !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
172 spin_lock(&fs_info->trans_lock);
173 if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
174 /* Want the extent tree to be the last on the list */
175 if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID)
176 list_move_tail(&root->dirty_list,
177 &fs_info->dirty_cowonly_roots);
179 list_move(&root->dirty_list,
180 &fs_info->dirty_cowonly_roots);
182 spin_unlock(&fs_info->trans_lock);
186 * used by snapshot creation to make a copy of a root for a tree with
187 * a given objectid. The buffer with the new root node is returned in
188 * cow_ret, and this func returns zero on success or a negative error code.
190 int btrfs_copy_root(struct btrfs_trans_handle *trans,
191 struct btrfs_root *root,
192 struct extent_buffer *buf,
193 struct extent_buffer **cow_ret, u64 new_root_objectid)
195 struct btrfs_fs_info *fs_info = root->fs_info;
196 struct extent_buffer *cow;
199 struct btrfs_disk_key disk_key;
201 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
202 trans->transid != fs_info->running_transaction->transid);
203 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
204 trans->transid != root->last_trans);
206 level = btrfs_header_level(buf);
208 btrfs_item_key(buf, &disk_key, 0);
210 btrfs_node_key(buf, &disk_key, 0);
212 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
213 &disk_key, level, buf->start, 0);
217 copy_extent_buffer_full(cow, buf);
218 btrfs_set_header_bytenr(cow, cow->start);
219 btrfs_set_header_generation(cow, trans->transid);
220 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
221 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
222 BTRFS_HEADER_FLAG_RELOC);
223 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
224 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
226 btrfs_set_header_owner(cow, new_root_objectid);
228 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
230 WARN_ON(btrfs_header_generation(buf) > trans->transid);
231 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
232 ret = btrfs_inc_ref(trans, root, cow, 1);
234 ret = btrfs_inc_ref(trans, root, cow, 0);
239 btrfs_mark_buffer_dirty(cow);
248 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
249 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
251 MOD_LOG_ROOT_REPLACE,
254 struct tree_mod_root {
259 struct tree_mod_elem {
265 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
268 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
271 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
272 struct btrfs_disk_key key;
275 /* this is used for op == MOD_LOG_MOVE_KEYS */
281 /* this is used for op == MOD_LOG_ROOT_REPLACE */
282 struct tree_mod_root old_root;
286 * Pull a new tree mod seq number for our operation.
288 static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
290 return atomic64_inc_return(&fs_info->tree_mod_seq);
294 * This adds a new blocker to the tree mod log's blocker list if the @elem
295 * passed does not already have a sequence number set. So when a caller expects
296 * to record tree modifications, it should ensure to set elem->seq to zero
297 * before calling btrfs_get_tree_mod_seq.
298 * Returns a fresh, unused tree log modification sequence number, even if no new
301 u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
302 struct seq_list *elem)
304 write_lock(&fs_info->tree_mod_log_lock);
305 spin_lock(&fs_info->tree_mod_seq_lock);
307 elem->seq = btrfs_inc_tree_mod_seq(fs_info);
308 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
310 spin_unlock(&fs_info->tree_mod_seq_lock);
311 write_unlock(&fs_info->tree_mod_log_lock);
316 void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
317 struct seq_list *elem)
319 struct rb_root *tm_root;
320 struct rb_node *node;
321 struct rb_node *next;
322 struct seq_list *cur_elem;
323 struct tree_mod_elem *tm;
324 u64 min_seq = (u64)-1;
325 u64 seq_putting = elem->seq;
330 spin_lock(&fs_info->tree_mod_seq_lock);
331 list_del(&elem->list);
334 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
335 if (cur_elem->seq < min_seq) {
336 if (seq_putting > cur_elem->seq) {
338 * blocker with lower sequence number exists, we
339 * cannot remove anything from the log
341 spin_unlock(&fs_info->tree_mod_seq_lock);
344 min_seq = cur_elem->seq;
347 spin_unlock(&fs_info->tree_mod_seq_lock);
350 * anything that's lower than the lowest existing (read: blocked)
351 * sequence number can be removed from the tree.
353 write_lock(&fs_info->tree_mod_log_lock);
354 tm_root = &fs_info->tree_mod_log;
355 for (node = rb_first(tm_root); node; node = next) {
356 next = rb_next(node);
357 tm = rb_entry(node, struct tree_mod_elem, node);
358 if (tm->seq > min_seq)
360 rb_erase(node, tm_root);
363 write_unlock(&fs_info->tree_mod_log_lock);
367 * key order of the log:
368 * node/leaf start address -> sequence
370 * The 'start address' is the logical address of the *new* root node
371 * for root replace operations, or the logical address of the affected
372 * block for all other operations.
374 * Note: must be called with write lock for fs_info::tree_mod_log_lock.
377 __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
379 struct rb_root *tm_root;
380 struct rb_node **new;
381 struct rb_node *parent = NULL;
382 struct tree_mod_elem *cur;
384 tm->seq = btrfs_inc_tree_mod_seq(fs_info);
386 tm_root = &fs_info->tree_mod_log;
387 new = &tm_root->rb_node;
389 cur = rb_entry(*new, struct tree_mod_elem, node);
391 if (cur->logical < tm->logical)
392 new = &((*new)->rb_left);
393 else if (cur->logical > tm->logical)
394 new = &((*new)->rb_right);
395 else if (cur->seq < tm->seq)
396 new = &((*new)->rb_left);
397 else if (cur->seq > tm->seq)
398 new = &((*new)->rb_right);
403 rb_link_node(&tm->node, parent, new);
404 rb_insert_color(&tm->node, tm_root);
409 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
410 * returns zero with the tree_mod_log_lock acquired. The caller must hold
411 * this until all tree mod log insertions are recorded in the rb tree and then
412 * write unlock fs_info::tree_mod_log_lock.
414 static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
415 struct extent_buffer *eb) {
417 if (list_empty(&(fs_info)->tree_mod_seq_list))
419 if (eb && btrfs_header_level(eb) == 0)
422 write_lock(&fs_info->tree_mod_log_lock);
423 if (list_empty(&(fs_info)->tree_mod_seq_list)) {
424 write_unlock(&fs_info->tree_mod_log_lock);
431 /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
432 static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
433 struct extent_buffer *eb)
436 if (list_empty(&(fs_info)->tree_mod_seq_list))
438 if (eb && btrfs_header_level(eb) == 0)
444 static struct tree_mod_elem *
445 alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
446 enum mod_log_op op, gfp_t flags)
448 struct tree_mod_elem *tm;
450 tm = kzalloc(sizeof(*tm), flags);
454 tm->logical = eb->start;
455 if (op != MOD_LOG_KEY_ADD) {
456 btrfs_node_key(eb, &tm->key, slot);
457 tm->blockptr = btrfs_node_blockptr(eb, slot);
461 tm->generation = btrfs_node_ptr_generation(eb, slot);
462 RB_CLEAR_NODE(&tm->node);
467 static noinline int tree_mod_log_insert_key(struct extent_buffer *eb, int slot,
468 enum mod_log_op op, gfp_t flags)
470 struct tree_mod_elem *tm;
473 if (!tree_mod_need_log(eb->fs_info, eb))
476 tm = alloc_tree_mod_elem(eb, slot, op, flags);
480 if (tree_mod_dont_log(eb->fs_info, eb)) {
485 ret = __tree_mod_log_insert(eb->fs_info, tm);
486 write_unlock(&eb->fs_info->tree_mod_log_lock);
493 static noinline int tree_mod_log_insert_move(struct extent_buffer *eb,
494 int dst_slot, int src_slot, int nr_items)
496 struct tree_mod_elem *tm = NULL;
497 struct tree_mod_elem **tm_list = NULL;
502 if (!tree_mod_need_log(eb->fs_info, eb))
505 tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS);
509 tm = kzalloc(sizeof(*tm), GFP_NOFS);
515 tm->logical = eb->start;
517 tm->move.dst_slot = dst_slot;
518 tm->move.nr_items = nr_items;
519 tm->op = MOD_LOG_MOVE_KEYS;
521 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
522 tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
523 MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
530 if (tree_mod_dont_log(eb->fs_info, eb))
535 * When we override something during the move, we log these removals.
536 * This can only happen when we move towards the beginning of the
537 * buffer, i.e. dst_slot < src_slot.
539 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
540 ret = __tree_mod_log_insert(eb->fs_info, tm_list[i]);
545 ret = __tree_mod_log_insert(eb->fs_info, tm);
548 write_unlock(&eb->fs_info->tree_mod_log_lock);
553 for (i = 0; i < nr_items; i++) {
554 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
555 rb_erase(&tm_list[i]->node, &eb->fs_info->tree_mod_log);
559 write_unlock(&eb->fs_info->tree_mod_log_lock);
567 __tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
568 struct tree_mod_elem **tm_list,
574 for (i = nritems - 1; i >= 0; i--) {
575 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
577 for (j = nritems - 1; j > i; j--)
578 rb_erase(&tm_list[j]->node,
579 &fs_info->tree_mod_log);
587 static noinline int tree_mod_log_insert_root(struct extent_buffer *old_root,
588 struct extent_buffer *new_root, int log_removal)
590 struct btrfs_fs_info *fs_info = old_root->fs_info;
591 struct tree_mod_elem *tm = NULL;
592 struct tree_mod_elem **tm_list = NULL;
597 if (!tree_mod_need_log(fs_info, NULL))
600 if (log_removal && btrfs_header_level(old_root) > 0) {
601 nritems = btrfs_header_nritems(old_root);
602 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
608 for (i = 0; i < nritems; i++) {
609 tm_list[i] = alloc_tree_mod_elem(old_root, i,
610 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
618 tm = kzalloc(sizeof(*tm), GFP_NOFS);
624 tm->logical = new_root->start;
625 tm->old_root.logical = old_root->start;
626 tm->old_root.level = btrfs_header_level(old_root);
627 tm->generation = btrfs_header_generation(old_root);
628 tm->op = MOD_LOG_ROOT_REPLACE;
630 if (tree_mod_dont_log(fs_info, NULL))
634 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
636 ret = __tree_mod_log_insert(fs_info, tm);
638 write_unlock(&fs_info->tree_mod_log_lock);
647 for (i = 0; i < nritems; i++)
656 static struct tree_mod_elem *
657 __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
660 struct rb_root *tm_root;
661 struct rb_node *node;
662 struct tree_mod_elem *cur = NULL;
663 struct tree_mod_elem *found = NULL;
665 read_lock(&fs_info->tree_mod_log_lock);
666 tm_root = &fs_info->tree_mod_log;
667 node = tm_root->rb_node;
669 cur = rb_entry(node, struct tree_mod_elem, node);
670 if (cur->logical < start) {
671 node = node->rb_left;
672 } else if (cur->logical > start) {
673 node = node->rb_right;
674 } else if (cur->seq < min_seq) {
675 node = node->rb_left;
676 } else if (!smallest) {
677 /* we want the node with the highest seq */
679 BUG_ON(found->seq > cur->seq);
681 node = node->rb_left;
682 } else if (cur->seq > min_seq) {
683 /* we want the node with the smallest seq */
685 BUG_ON(found->seq < cur->seq);
687 node = node->rb_right;
693 read_unlock(&fs_info->tree_mod_log_lock);
699 * this returns the element from the log with the smallest time sequence
700 * value that's in the log (the oldest log item). any element with a time
701 * sequence lower than min_seq will be ignored.
703 static struct tree_mod_elem *
704 tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
707 return __tree_mod_log_search(fs_info, start, min_seq, 1);
711 * this returns the element from the log with the largest time sequence
712 * value that's in the log (the most recent log item). any element with
713 * a time sequence lower than min_seq will be ignored.
715 static struct tree_mod_elem *
716 tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
718 return __tree_mod_log_search(fs_info, start, min_seq, 0);
722 tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
723 struct extent_buffer *src, unsigned long dst_offset,
724 unsigned long src_offset, int nr_items)
727 struct tree_mod_elem **tm_list = NULL;
728 struct tree_mod_elem **tm_list_add, **tm_list_rem;
732 if (!tree_mod_need_log(fs_info, NULL))
735 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
738 tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
743 tm_list_add = tm_list;
744 tm_list_rem = tm_list + nr_items;
745 for (i = 0; i < nr_items; i++) {
746 tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
747 MOD_LOG_KEY_REMOVE, GFP_NOFS);
748 if (!tm_list_rem[i]) {
753 tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
754 MOD_LOG_KEY_ADD, GFP_NOFS);
755 if (!tm_list_add[i]) {
761 if (tree_mod_dont_log(fs_info, NULL))
765 for (i = 0; i < nr_items; i++) {
766 ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
769 ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
774 write_unlock(&fs_info->tree_mod_log_lock);
780 for (i = 0; i < nr_items * 2; i++) {
781 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
782 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
786 write_unlock(&fs_info->tree_mod_log_lock);
792 static noinline int tree_mod_log_free_eb(struct extent_buffer *eb)
794 struct tree_mod_elem **tm_list = NULL;
799 if (btrfs_header_level(eb) == 0)
802 if (!tree_mod_need_log(eb->fs_info, NULL))
805 nritems = btrfs_header_nritems(eb);
806 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
810 for (i = 0; i < nritems; i++) {
811 tm_list[i] = alloc_tree_mod_elem(eb, i,
812 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
819 if (tree_mod_dont_log(eb->fs_info, eb))
822 ret = __tree_mod_log_free_eb(eb->fs_info, tm_list, nritems);
823 write_unlock(&eb->fs_info->tree_mod_log_lock);
831 for (i = 0; i < nritems; i++)
839 * check if the tree block can be shared by multiple trees
841 int btrfs_block_can_be_shared(struct btrfs_root *root,
842 struct extent_buffer *buf)
845 * Tree blocks not in reference counted trees and tree roots
846 * are never shared. If a block was allocated after the last
847 * snapshot and the block was not allocated by tree relocation,
848 * we know the block is not shared.
850 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
851 buf != root->node && buf != root->commit_root &&
852 (btrfs_header_generation(buf) <=
853 btrfs_root_last_snapshot(&root->root_item) ||
854 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
860 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
861 struct btrfs_root *root,
862 struct extent_buffer *buf,
863 struct extent_buffer *cow,
866 struct btrfs_fs_info *fs_info = root->fs_info;
874 * Backrefs update rules:
876 * Always use full backrefs for extent pointers in tree block
877 * allocated by tree relocation.
879 * If a shared tree block is no longer referenced by its owner
880 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
881 * use full backrefs for extent pointers in tree block.
883 * If a tree block is been relocating
884 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
885 * use full backrefs for extent pointers in tree block.
886 * The reason for this is some operations (such as drop tree)
887 * are only allowed for blocks use full backrefs.
890 if (btrfs_block_can_be_shared(root, buf)) {
891 ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
892 btrfs_header_level(buf), 1,
898 btrfs_handle_fs_error(fs_info, ret, NULL);
903 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
904 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
905 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
910 owner = btrfs_header_owner(buf);
911 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
912 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
915 if ((owner == root->root_key.objectid ||
916 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
917 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
918 ret = btrfs_inc_ref(trans, root, buf, 1);
922 if (root->root_key.objectid ==
923 BTRFS_TREE_RELOC_OBJECTID) {
924 ret = btrfs_dec_ref(trans, root, buf, 0);
927 ret = btrfs_inc_ref(trans, root, cow, 1);
931 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
934 if (root->root_key.objectid ==
935 BTRFS_TREE_RELOC_OBJECTID)
936 ret = btrfs_inc_ref(trans, root, cow, 1);
938 ret = btrfs_inc_ref(trans, root, cow, 0);
942 if (new_flags != 0) {
943 int level = btrfs_header_level(buf);
945 ret = btrfs_set_disk_extent_flags(trans, fs_info,
948 new_flags, level, 0);
953 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
954 if (root->root_key.objectid ==
955 BTRFS_TREE_RELOC_OBJECTID)
956 ret = btrfs_inc_ref(trans, root, cow, 1);
958 ret = btrfs_inc_ref(trans, root, cow, 0);
961 ret = btrfs_dec_ref(trans, root, buf, 1);
965 clean_tree_block(fs_info, buf);
971 static struct extent_buffer *alloc_tree_block_no_bg_flush(
972 struct btrfs_trans_handle *trans,
973 struct btrfs_root *root,
975 const struct btrfs_disk_key *disk_key,
980 struct btrfs_fs_info *fs_info = root->fs_info;
981 struct extent_buffer *ret;
984 * If we are COWing a node/leaf from the extent, chunk, device or free
985 * space trees, make sure that we do not finish block group creation of
986 * pending block groups. We do this to avoid a deadlock.
987 * COWing can result in allocation of a new chunk, and flushing pending
988 * block groups (btrfs_create_pending_block_groups()) can be triggered
989 * when finishing allocation of a new chunk. Creation of a pending block
990 * group modifies the extent, chunk, device and free space trees,
991 * therefore we could deadlock with ourselves since we are holding a
992 * lock on an extent buffer that btrfs_create_pending_block_groups() may
994 * For similar reasons, we also need to delay flushing pending block
995 * groups when splitting a leaf or node, from one of those trees, since
996 * we are holding a write lock on it and its parent or when inserting a
997 * new root node for one of those trees.
999 if (root == fs_info->extent_root ||
1000 root == fs_info->chunk_root ||
1001 root == fs_info->dev_root ||
1002 root == fs_info->free_space_root)
1003 trans->can_flush_pending_bgs = false;
1005 ret = btrfs_alloc_tree_block(trans, root, parent_start,
1006 root->root_key.objectid, disk_key, level,
1008 trans->can_flush_pending_bgs = true;
1014 * does the dirty work in cow of a single block. The parent block (if
1015 * supplied) is updated to point to the new cow copy. The new buffer is marked
1016 * dirty and returned locked. If you modify the block it needs to be marked
1019 * search_start -- an allocation hint for the new block
1021 * empty_size -- a hint that you plan on doing more cow. This is the size in
1022 * bytes the allocator should try to find free next to the block it returns.
1023 * This is just a hint and may be ignored by the allocator.
1025 static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
1026 struct btrfs_root *root,
1027 struct extent_buffer *buf,
1028 struct extent_buffer *parent, int parent_slot,
1029 struct extent_buffer **cow_ret,
1030 u64 search_start, u64 empty_size)
1032 struct btrfs_fs_info *fs_info = root->fs_info;
1033 struct btrfs_disk_key disk_key;
1034 struct extent_buffer *cow;
1037 int unlock_orig = 0;
1038 u64 parent_start = 0;
1040 if (*cow_ret == buf)
1043 btrfs_assert_tree_locked(buf);
1045 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1046 trans->transid != fs_info->running_transaction->transid);
1047 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1048 trans->transid != root->last_trans);
1050 level = btrfs_header_level(buf);
1053 btrfs_item_key(buf, &disk_key, 0);
1055 btrfs_node_key(buf, &disk_key, 0);
1057 if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
1058 parent_start = parent->start;
1060 cow = alloc_tree_block_no_bg_flush(trans, root, parent_start, &disk_key,
1061 level, search_start, empty_size);
1063 return PTR_ERR(cow);
1065 /* cow is set to blocking by btrfs_init_new_buffer */
1067 copy_extent_buffer_full(cow, buf);
1068 btrfs_set_header_bytenr(cow, cow->start);
1069 btrfs_set_header_generation(cow, trans->transid);
1070 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1071 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1072 BTRFS_HEADER_FLAG_RELOC);
1073 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1074 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1076 btrfs_set_header_owner(cow, root->root_key.objectid);
1078 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
1080 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
1082 btrfs_abort_transaction(trans, ret);
1086 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
1087 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
1089 btrfs_abort_transaction(trans, ret);
1094 if (buf == root->node) {
1095 WARN_ON(parent && parent != buf);
1096 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1097 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1098 parent_start = buf->start;
1100 extent_buffer_get(cow);
1101 ret = tree_mod_log_insert_root(root->node, cow, 1);
1103 rcu_assign_pointer(root->node, cow);
1105 btrfs_free_tree_block(trans, root, buf, parent_start,
1107 free_extent_buffer(buf);
1108 add_root_to_dirty_list(root);
1110 WARN_ON(trans->transid != btrfs_header_generation(parent));
1111 tree_mod_log_insert_key(parent, parent_slot,
1112 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1113 btrfs_set_node_blockptr(parent, parent_slot,
1115 btrfs_set_node_ptr_generation(parent, parent_slot,
1117 btrfs_mark_buffer_dirty(parent);
1119 ret = tree_mod_log_free_eb(buf);
1121 btrfs_abort_transaction(trans, ret);
1125 btrfs_free_tree_block(trans, root, buf, parent_start,
1129 btrfs_tree_unlock(buf);
1130 free_extent_buffer_stale(buf);
1131 btrfs_mark_buffer_dirty(cow);
1137 * returns the logical address of the oldest predecessor of the given root.
1138 * entries older than time_seq are ignored.
1140 static struct tree_mod_elem *__tree_mod_log_oldest_root(
1141 struct extent_buffer *eb_root, u64 time_seq)
1143 struct tree_mod_elem *tm;
1144 struct tree_mod_elem *found = NULL;
1145 u64 root_logical = eb_root->start;
1152 * the very last operation that's logged for a root is the
1153 * replacement operation (if it is replaced at all). this has
1154 * the logical address of the *new* root, making it the very
1155 * first operation that's logged for this root.
1158 tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical,
1163 * if there are no tree operation for the oldest root, we simply
1164 * return it. this should only happen if that (old) root is at
1171 * if there's an operation that's not a root replacement, we
1172 * found the oldest version of our root. normally, we'll find a
1173 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1175 if (tm->op != MOD_LOG_ROOT_REPLACE)
1179 root_logical = tm->old_root.logical;
1183 /* if there's no old root to return, return what we found instead */
1191 * tm is a pointer to the first operation to rewind within eb. then, all
1192 * previous operations will be rewound (until we reach something older than
1196 __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1197 u64 time_seq, struct tree_mod_elem *first_tm)
1200 struct rb_node *next;
1201 struct tree_mod_elem *tm = first_tm;
1202 unsigned long o_dst;
1203 unsigned long o_src;
1204 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1206 n = btrfs_header_nritems(eb);
1207 read_lock(&fs_info->tree_mod_log_lock);
1208 while (tm && tm->seq >= time_seq) {
1210 * all the operations are recorded with the operator used for
1211 * the modification. as we're going backwards, we do the
1212 * opposite of each operation here.
1215 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1216 BUG_ON(tm->slot < n);
1218 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
1219 case MOD_LOG_KEY_REMOVE:
1220 btrfs_set_node_key(eb, &tm->key, tm->slot);
1221 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1222 btrfs_set_node_ptr_generation(eb, tm->slot,
1226 case MOD_LOG_KEY_REPLACE:
1227 BUG_ON(tm->slot >= n);
1228 btrfs_set_node_key(eb, &tm->key, tm->slot);
1229 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1230 btrfs_set_node_ptr_generation(eb, tm->slot,
1233 case MOD_LOG_KEY_ADD:
1234 /* if a move operation is needed it's in the log */
1237 case MOD_LOG_MOVE_KEYS:
1238 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1239 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1240 memmove_extent_buffer(eb, o_dst, o_src,
1241 tm->move.nr_items * p_size);
1243 case MOD_LOG_ROOT_REPLACE:
1245 * this operation is special. for roots, this must be
1246 * handled explicitly before rewinding.
1247 * for non-roots, this operation may exist if the node
1248 * was a root: root A -> child B; then A gets empty and
1249 * B is promoted to the new root. in the mod log, we'll
1250 * have a root-replace operation for B, a tree block
1251 * that is no root. we simply ignore that operation.
1255 next = rb_next(&tm->node);
1258 tm = rb_entry(next, struct tree_mod_elem, node);
1259 if (tm->logical != first_tm->logical)
1262 read_unlock(&fs_info->tree_mod_log_lock);
1263 btrfs_set_header_nritems(eb, n);
1267 * Called with eb read locked. If the buffer cannot be rewound, the same buffer
1268 * is returned. If rewind operations happen, a fresh buffer is returned. The
1269 * returned buffer is always read-locked. If the returned buffer is not the
1270 * input buffer, the lock on the input buffer is released and the input buffer
1271 * is freed (its refcount is decremented).
1273 static struct extent_buffer *
1274 tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1275 struct extent_buffer *eb, u64 time_seq)
1277 struct extent_buffer *eb_rewin;
1278 struct tree_mod_elem *tm;
1283 if (btrfs_header_level(eb) == 0)
1286 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1290 btrfs_set_path_blocking(path);
1291 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1293 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1294 BUG_ON(tm->slot != 0);
1295 eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
1297 btrfs_tree_read_unlock_blocking(eb);
1298 free_extent_buffer(eb);
1301 btrfs_set_header_bytenr(eb_rewin, eb->start);
1302 btrfs_set_header_backref_rev(eb_rewin,
1303 btrfs_header_backref_rev(eb));
1304 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
1305 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
1307 eb_rewin = btrfs_clone_extent_buffer(eb);
1309 btrfs_tree_read_unlock_blocking(eb);
1310 free_extent_buffer(eb);
1315 btrfs_tree_read_unlock_blocking(eb);
1316 free_extent_buffer(eb);
1318 btrfs_tree_read_lock(eb_rewin);
1319 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
1320 WARN_ON(btrfs_header_nritems(eb_rewin) >
1321 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
1327 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1328 * value. If there are no changes, the current root->root_node is returned. If
1329 * anything changed in between, there's a fresh buffer allocated on which the
1330 * rewind operations are done. In any case, the returned buffer is read locked.
1331 * Returns NULL on error (with no locks held).
1333 static inline struct extent_buffer *
1334 get_old_root(struct btrfs_root *root, u64 time_seq)
1336 struct btrfs_fs_info *fs_info = root->fs_info;
1337 struct tree_mod_elem *tm;
1338 struct extent_buffer *eb = NULL;
1339 struct extent_buffer *eb_root;
1340 struct extent_buffer *old;
1341 struct tree_mod_root *old_root = NULL;
1342 u64 old_generation = 0;
1346 eb_root = btrfs_read_lock_root_node(root);
1347 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
1351 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1352 old_root = &tm->old_root;
1353 old_generation = tm->generation;
1354 logical = old_root->logical;
1355 level = old_root->level;
1357 logical = eb_root->start;
1358 level = btrfs_header_level(eb_root);
1361 tm = tree_mod_log_search(fs_info, logical, time_seq);
1362 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1363 btrfs_tree_read_unlock(eb_root);
1364 free_extent_buffer(eb_root);
1365 old = read_tree_block(fs_info, logical, 0, level, NULL);
1366 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
1368 free_extent_buffer(old);
1370 "failed to read tree block %llu from get_old_root",
1373 eb = btrfs_clone_extent_buffer(old);
1374 free_extent_buffer(old);
1376 } else if (old_root) {
1377 btrfs_tree_read_unlock(eb_root);
1378 free_extent_buffer(eb_root);
1379 eb = alloc_dummy_extent_buffer(fs_info, logical);
1381 btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
1382 eb = btrfs_clone_extent_buffer(eb_root);
1383 btrfs_tree_read_unlock_blocking(eb_root);
1384 free_extent_buffer(eb_root);
1389 btrfs_tree_read_lock(eb);
1391 btrfs_set_header_bytenr(eb, eb->start);
1392 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
1393 btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
1394 btrfs_set_header_level(eb, old_root->level);
1395 btrfs_set_header_generation(eb, old_generation);
1398 __tree_mod_log_rewind(fs_info, eb, time_seq, tm);
1400 WARN_ON(btrfs_header_level(eb) != 0);
1401 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
1406 int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1408 struct tree_mod_elem *tm;
1410 struct extent_buffer *eb_root = btrfs_root_node(root);
1412 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
1413 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1414 level = tm->old_root.level;
1416 level = btrfs_header_level(eb_root);
1418 free_extent_buffer(eb_root);
1423 static inline int should_cow_block(struct btrfs_trans_handle *trans,
1424 struct btrfs_root *root,
1425 struct extent_buffer *buf)
1427 if (btrfs_is_testing(root->fs_info))
1430 /* Ensure we can see the FORCE_COW bit */
1431 smp_mb__before_atomic();
1434 * We do not need to cow a block if
1435 * 1) this block is not created or changed in this transaction;
1436 * 2) this block does not belong to TREE_RELOC tree;
1437 * 3) the root is not forced COW.
1439 * What is forced COW:
1440 * when we create snapshot during committing the transaction,
1441 * after we've finished copying src root, we must COW the shared
1442 * block to ensure the metadata consistency.
1444 if (btrfs_header_generation(buf) == trans->transid &&
1445 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1446 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
1447 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1448 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
1454 * cows a single block, see __btrfs_cow_block for the real work.
1455 * This version of it has extra checks so that a block isn't COWed more than
1456 * once per transaction, as long as it hasn't been written yet
1458 noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
1459 struct btrfs_root *root, struct extent_buffer *buf,
1460 struct extent_buffer *parent, int parent_slot,
1461 struct extent_buffer **cow_ret)
1463 struct btrfs_fs_info *fs_info = root->fs_info;
1467 if (test_bit(BTRFS_ROOT_DELETING, &root->state))
1469 "COW'ing blocks on a fs root that's being dropped");
1471 if (trans->transaction != fs_info->running_transaction)
1472 WARN(1, KERN_CRIT "trans %llu running %llu\n",
1474 fs_info->running_transaction->transid);
1476 if (trans->transid != fs_info->generation)
1477 WARN(1, KERN_CRIT "trans %llu running %llu\n",
1478 trans->transid, fs_info->generation);
1480 if (!should_cow_block(trans, root, buf)) {
1481 trans->dirty = true;
1486 search_start = buf->start & ~((u64)SZ_1G - 1);
1489 btrfs_set_lock_blocking(parent);
1490 btrfs_set_lock_blocking(buf);
1492 ret = __btrfs_cow_block(trans, root, buf, parent,
1493 parent_slot, cow_ret, search_start, 0);
1495 trace_btrfs_cow_block(root, buf, *cow_ret);
1501 * helper function for defrag to decide if two blocks pointed to by a
1502 * node are actually close by
1504 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
1506 if (blocknr < other && other - (blocknr + blocksize) < 32768)
1508 if (blocknr > other && blocknr - (other + blocksize) < 32768)
1514 * compare two keys in a memcmp fashion
1516 static int comp_keys(const struct btrfs_disk_key *disk,
1517 const struct btrfs_key *k2)
1519 struct btrfs_key k1;
1521 btrfs_disk_key_to_cpu(&k1, disk);
1523 return btrfs_comp_cpu_keys(&k1, k2);
1527 * same as comp_keys only with two btrfs_key's
1529 int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
1531 if (k1->objectid > k2->objectid)
1533 if (k1->objectid < k2->objectid)
1535 if (k1->type > k2->type)
1537 if (k1->type < k2->type)
1539 if (k1->offset > k2->offset)
1541 if (k1->offset < k2->offset)
1547 * this is used by the defrag code to go through all the
1548 * leaves pointed to by a node and reallocate them so that
1549 * disk order is close to key order
1551 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
1552 struct btrfs_root *root, struct extent_buffer *parent,
1553 int start_slot, u64 *last_ret,
1554 struct btrfs_key *progress)
1556 struct btrfs_fs_info *fs_info = root->fs_info;
1557 struct extent_buffer *cur;
1560 u64 search_start = *last_ret;
1570 int progress_passed = 0;
1571 struct btrfs_disk_key disk_key;
1573 parent_level = btrfs_header_level(parent);
1575 WARN_ON(trans->transaction != fs_info->running_transaction);
1576 WARN_ON(trans->transid != fs_info->generation);
1578 parent_nritems = btrfs_header_nritems(parent);
1579 blocksize = fs_info->nodesize;
1580 end_slot = parent_nritems - 1;
1582 if (parent_nritems <= 1)
1585 btrfs_set_lock_blocking(parent);
1587 for (i = start_slot; i <= end_slot; i++) {
1588 struct btrfs_key first_key;
1591 btrfs_node_key(parent, &disk_key, i);
1592 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1595 progress_passed = 1;
1596 blocknr = btrfs_node_blockptr(parent, i);
1597 gen = btrfs_node_ptr_generation(parent, i);
1598 btrfs_node_key_to_cpu(parent, &first_key, i);
1599 if (last_block == 0)
1600 last_block = blocknr;
1603 other = btrfs_node_blockptr(parent, i - 1);
1604 close = close_blocks(blocknr, other, blocksize);
1606 if (!close && i < end_slot) {
1607 other = btrfs_node_blockptr(parent, i + 1);
1608 close = close_blocks(blocknr, other, blocksize);
1611 last_block = blocknr;
1615 cur = find_extent_buffer(fs_info, blocknr);
1617 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
1620 if (!cur || !uptodate) {
1622 cur = read_tree_block(fs_info, blocknr, gen,
1626 return PTR_ERR(cur);
1627 } else if (!extent_buffer_uptodate(cur)) {
1628 free_extent_buffer(cur);
1631 } else if (!uptodate) {
1632 err = btrfs_read_buffer(cur, gen,
1633 parent_level - 1,&first_key);
1635 free_extent_buffer(cur);
1640 if (search_start == 0)
1641 search_start = last_block;
1643 btrfs_tree_lock(cur);
1644 btrfs_set_lock_blocking(cur);
1645 err = __btrfs_cow_block(trans, root, cur, parent, i,
1648 (end_slot - i) * blocksize));
1650 btrfs_tree_unlock(cur);
1651 free_extent_buffer(cur);
1654 search_start = cur->start;
1655 last_block = cur->start;
1656 *last_ret = search_start;
1657 btrfs_tree_unlock(cur);
1658 free_extent_buffer(cur);
1664 * search for key in the extent_buffer. The items start at offset p,
1665 * and they are item_size apart. There are 'max' items in p.
1667 * the slot in the array is returned via slot, and it points to
1668 * the place where you would insert key if it is not found in
1671 * slot may point to max if the key is bigger than all of the keys
1673 static noinline int generic_bin_search(struct extent_buffer *eb,
1674 unsigned long p, int item_size,
1675 const struct btrfs_key *key,
1682 struct btrfs_disk_key *tmp = NULL;
1683 struct btrfs_disk_key unaligned;
1684 unsigned long offset;
1686 unsigned long map_start = 0;
1687 unsigned long map_len = 0;
1691 btrfs_err(eb->fs_info,
1692 "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
1693 __func__, low, high, eb->start,
1694 btrfs_header_owner(eb), btrfs_header_level(eb));
1698 while (low < high) {
1699 mid = (low + high) / 2;
1700 offset = p + mid * item_size;
1702 if (!kaddr || offset < map_start ||
1703 (offset + sizeof(struct btrfs_disk_key)) >
1704 map_start + map_len) {
1706 err = map_private_extent_buffer(eb, offset,
1707 sizeof(struct btrfs_disk_key),
1708 &kaddr, &map_start, &map_len);
1711 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1713 } else if (err == 1) {
1714 read_extent_buffer(eb, &unaligned,
1715 offset, sizeof(unaligned));
1722 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1725 ret = comp_keys(tmp, key);
1741 * simple bin_search frontend that does the right thing for
1744 int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
1745 int level, int *slot)
1748 return generic_bin_search(eb,
1749 offsetof(struct btrfs_leaf, items),
1750 sizeof(struct btrfs_item),
1751 key, btrfs_header_nritems(eb),
1754 return generic_bin_search(eb,
1755 offsetof(struct btrfs_node, ptrs),
1756 sizeof(struct btrfs_key_ptr),
1757 key, btrfs_header_nritems(eb),
1761 static void root_add_used(struct btrfs_root *root, u32 size)
1763 spin_lock(&root->accounting_lock);
1764 btrfs_set_root_used(&root->root_item,
1765 btrfs_root_used(&root->root_item) + size);
1766 spin_unlock(&root->accounting_lock);
1769 static void root_sub_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 /* given a node and slot number, this reads the blocks it points to. The
1778 * extent buffer is returned with a reference taken (but unlocked).
1780 static noinline struct extent_buffer *
1781 read_node_slot(struct btrfs_fs_info *fs_info, struct extent_buffer *parent,
1784 int level = btrfs_header_level(parent);
1785 struct extent_buffer *eb;
1786 struct btrfs_key first_key;
1788 if (slot < 0 || slot >= btrfs_header_nritems(parent))
1789 return ERR_PTR(-ENOENT);
1793 btrfs_node_key_to_cpu(parent, &first_key, slot);
1794 eb = read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
1795 btrfs_node_ptr_generation(parent, slot),
1796 level - 1, &first_key);
1797 if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
1798 free_extent_buffer(eb);
1806 * node level balancing, used to make sure nodes are in proper order for
1807 * item deletion. We balance from the top down, so we have to make sure
1808 * that a deletion won't leave an node completely empty later on.
1810 static noinline int balance_level(struct btrfs_trans_handle *trans,
1811 struct btrfs_root *root,
1812 struct btrfs_path *path, int level)
1814 struct btrfs_fs_info *fs_info = root->fs_info;
1815 struct extent_buffer *right = NULL;
1816 struct extent_buffer *mid;
1817 struct extent_buffer *left = NULL;
1818 struct extent_buffer *parent = NULL;
1822 int orig_slot = path->slots[level];
1827 mid = path->nodes[level];
1829 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1830 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
1831 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1833 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1835 if (level < BTRFS_MAX_LEVEL - 1) {
1836 parent = path->nodes[level + 1];
1837 pslot = path->slots[level + 1];
1841 * deal with the case where there is only one pointer in the root
1842 * by promoting the node below to a root
1845 struct extent_buffer *child;
1847 if (btrfs_header_nritems(mid) != 1)
1850 /* promote the child to a root */
1851 child = read_node_slot(fs_info, mid, 0);
1852 if (IS_ERR(child)) {
1853 ret = PTR_ERR(child);
1854 btrfs_handle_fs_error(fs_info, ret, NULL);
1858 btrfs_tree_lock(child);
1859 btrfs_set_lock_blocking(child);
1860 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
1862 btrfs_tree_unlock(child);
1863 free_extent_buffer(child);
1867 ret = tree_mod_log_insert_root(root->node, child, 1);
1869 rcu_assign_pointer(root->node, child);
1871 add_root_to_dirty_list(root);
1872 btrfs_tree_unlock(child);
1874 path->locks[level] = 0;
1875 path->nodes[level] = NULL;
1876 clean_tree_block(fs_info, mid);
1877 btrfs_tree_unlock(mid);
1878 /* once for the path */
1879 free_extent_buffer(mid);
1881 root_sub_used(root, mid->len);
1882 btrfs_free_tree_block(trans, root, mid, 0, 1);
1883 /* once for the root ptr */
1884 free_extent_buffer_stale(mid);
1887 if (btrfs_header_nritems(mid) >
1888 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
1891 left = read_node_slot(fs_info, parent, pslot - 1);
1896 btrfs_tree_lock(left);
1897 btrfs_set_lock_blocking(left);
1898 wret = btrfs_cow_block(trans, root, left,
1899 parent, pslot - 1, &left);
1906 right = read_node_slot(fs_info, parent, pslot + 1);
1911 btrfs_tree_lock(right);
1912 btrfs_set_lock_blocking(right);
1913 wret = btrfs_cow_block(trans, root, right,
1914 parent, pslot + 1, &right);
1921 /* first, try to make some room in the middle buffer */
1923 orig_slot += btrfs_header_nritems(left);
1924 wret = push_node_left(trans, fs_info, left, mid, 1);
1930 * then try to empty the right most buffer into the middle
1933 wret = push_node_left(trans, fs_info, mid, right, 1);
1934 if (wret < 0 && wret != -ENOSPC)
1936 if (btrfs_header_nritems(right) == 0) {
1937 clean_tree_block(fs_info, right);
1938 btrfs_tree_unlock(right);
1939 del_ptr(root, path, level + 1, pslot + 1);
1940 root_sub_used(root, right->len);
1941 btrfs_free_tree_block(trans, root, right, 0, 1);
1942 free_extent_buffer_stale(right);
1945 struct btrfs_disk_key right_key;
1946 btrfs_node_key(right, &right_key, 0);
1947 ret = tree_mod_log_insert_key(parent, pslot + 1,
1948 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1950 btrfs_set_node_key(parent, &right_key, pslot + 1);
1951 btrfs_mark_buffer_dirty(parent);
1954 if (btrfs_header_nritems(mid) == 1) {
1956 * we're not allowed to leave a node with one item in the
1957 * tree during a delete. A deletion from lower in the tree
1958 * could try to delete the only pointer in this node.
1959 * So, pull some keys from the left.
1960 * There has to be a left pointer at this point because
1961 * otherwise we would have pulled some pointers from the
1966 btrfs_handle_fs_error(fs_info, ret, NULL);
1969 wret = balance_node_right(trans, fs_info, mid, left);
1975 wret = push_node_left(trans, fs_info, left, mid, 1);
1981 if (btrfs_header_nritems(mid) == 0) {
1982 clean_tree_block(fs_info, mid);
1983 btrfs_tree_unlock(mid);
1984 del_ptr(root, path, level + 1, pslot);
1985 root_sub_used(root, mid->len);
1986 btrfs_free_tree_block(trans, root, mid, 0, 1);
1987 free_extent_buffer_stale(mid);
1990 /* update the parent key to reflect our changes */
1991 struct btrfs_disk_key mid_key;
1992 btrfs_node_key(mid, &mid_key, 0);
1993 ret = tree_mod_log_insert_key(parent, pslot,
1994 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1996 btrfs_set_node_key(parent, &mid_key, pslot);
1997 btrfs_mark_buffer_dirty(parent);
2000 /* update the path */
2002 if (btrfs_header_nritems(left) > orig_slot) {
2003 extent_buffer_get(left);
2004 /* left was locked after cow */
2005 path->nodes[level] = left;
2006 path->slots[level + 1] -= 1;
2007 path->slots[level] = orig_slot;
2009 btrfs_tree_unlock(mid);
2010 free_extent_buffer(mid);
2013 orig_slot -= btrfs_header_nritems(left);
2014 path->slots[level] = orig_slot;
2017 /* double check we haven't messed things up */
2019 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
2023 btrfs_tree_unlock(right);
2024 free_extent_buffer(right);
2027 if (path->nodes[level] != left)
2028 btrfs_tree_unlock(left);
2029 free_extent_buffer(left);
2034 /* Node balancing for insertion. Here we only split or push nodes around
2035 * when they are completely full. This is also done top down, so we
2036 * have to be pessimistic.
2038 static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
2039 struct btrfs_root *root,
2040 struct btrfs_path *path, int level)
2042 struct btrfs_fs_info *fs_info = root->fs_info;
2043 struct extent_buffer *right = NULL;
2044 struct extent_buffer *mid;
2045 struct extent_buffer *left = NULL;
2046 struct extent_buffer *parent = NULL;
2050 int orig_slot = path->slots[level];
2055 mid = path->nodes[level];
2056 WARN_ON(btrfs_header_generation(mid) != trans->transid);
2058 if (level < BTRFS_MAX_LEVEL - 1) {
2059 parent = path->nodes[level + 1];
2060 pslot = path->slots[level + 1];
2066 left = read_node_slot(fs_info, parent, pslot - 1);
2070 /* first, try to make some room in the middle buffer */
2074 btrfs_tree_lock(left);
2075 btrfs_set_lock_blocking(left);
2077 left_nr = btrfs_header_nritems(left);
2078 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
2081 ret = btrfs_cow_block(trans, root, left, parent,
2086 wret = push_node_left(trans, fs_info,
2093 struct btrfs_disk_key disk_key;
2094 orig_slot += left_nr;
2095 btrfs_node_key(mid, &disk_key, 0);
2096 ret = tree_mod_log_insert_key(parent, pslot,
2097 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2099 btrfs_set_node_key(parent, &disk_key, pslot);
2100 btrfs_mark_buffer_dirty(parent);
2101 if (btrfs_header_nritems(left) > orig_slot) {
2102 path->nodes[level] = left;
2103 path->slots[level + 1] -= 1;
2104 path->slots[level] = orig_slot;
2105 btrfs_tree_unlock(mid);
2106 free_extent_buffer(mid);
2109 btrfs_header_nritems(left);
2110 path->slots[level] = orig_slot;
2111 btrfs_tree_unlock(left);
2112 free_extent_buffer(left);
2116 btrfs_tree_unlock(left);
2117 free_extent_buffer(left);
2119 right = read_node_slot(fs_info, parent, pslot + 1);
2124 * then try to empty the right most buffer into the middle
2129 btrfs_tree_lock(right);
2130 btrfs_set_lock_blocking(right);
2132 right_nr = btrfs_header_nritems(right);
2133 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
2136 ret = btrfs_cow_block(trans, root, right,
2142 wret = balance_node_right(trans, fs_info,
2149 struct btrfs_disk_key disk_key;
2151 btrfs_node_key(right, &disk_key, 0);
2152 ret = tree_mod_log_insert_key(parent, pslot + 1,
2153 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2155 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2156 btrfs_mark_buffer_dirty(parent);
2158 if (btrfs_header_nritems(mid) <= orig_slot) {
2159 path->nodes[level] = right;
2160 path->slots[level + 1] += 1;
2161 path->slots[level] = orig_slot -
2162 btrfs_header_nritems(mid);
2163 btrfs_tree_unlock(mid);
2164 free_extent_buffer(mid);
2166 btrfs_tree_unlock(right);
2167 free_extent_buffer(right);
2171 btrfs_tree_unlock(right);
2172 free_extent_buffer(right);
2178 * readahead one full node of leaves, finding things that are close
2179 * to the block in 'slot', and triggering ra on them.
2181 static void reada_for_search(struct btrfs_fs_info *fs_info,
2182 struct btrfs_path *path,
2183 int level, int slot, u64 objectid)
2185 struct extent_buffer *node;
2186 struct btrfs_disk_key disk_key;
2191 struct extent_buffer *eb;
2199 if (!path->nodes[level])
2202 node = path->nodes[level];
2204 search = btrfs_node_blockptr(node, slot);
2205 blocksize = fs_info->nodesize;
2206 eb = find_extent_buffer(fs_info, search);
2208 free_extent_buffer(eb);
2214 nritems = btrfs_header_nritems(node);
2218 if (path->reada == READA_BACK) {
2222 } else if (path->reada == READA_FORWARD) {
2227 if (path->reada == READA_BACK && objectid) {
2228 btrfs_node_key(node, &disk_key, nr);
2229 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2232 search = btrfs_node_blockptr(node, nr);
2233 if ((search <= target && target - search <= 65536) ||
2234 (search > target && search - target <= 65536)) {
2235 readahead_tree_block(fs_info, search);
2239 if ((nread > 65536 || nscan > 32))
2244 static noinline void reada_for_balance(struct btrfs_fs_info *fs_info,
2245 struct btrfs_path *path, int level)
2249 struct extent_buffer *parent;
2250 struct extent_buffer *eb;
2255 parent = path->nodes[level + 1];
2259 nritems = btrfs_header_nritems(parent);
2260 slot = path->slots[level + 1];
2263 block1 = btrfs_node_blockptr(parent, slot - 1);
2264 gen = btrfs_node_ptr_generation(parent, slot - 1);
2265 eb = find_extent_buffer(fs_info, block1);
2267 * if we get -eagain from btrfs_buffer_uptodate, we
2268 * don't want to return eagain here. That will loop
2271 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
2273 free_extent_buffer(eb);
2275 if (slot + 1 < nritems) {
2276 block2 = btrfs_node_blockptr(parent, slot + 1);
2277 gen = btrfs_node_ptr_generation(parent, slot + 1);
2278 eb = find_extent_buffer(fs_info, block2);
2279 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
2281 free_extent_buffer(eb);
2285 readahead_tree_block(fs_info, block1);
2287 readahead_tree_block(fs_info, block2);
2292 * when we walk down the tree, it is usually safe to unlock the higher layers
2293 * in the tree. The exceptions are when our path goes through slot 0, because
2294 * operations on the tree might require changing key pointers higher up in the
2297 * callers might also have set path->keep_locks, which tells this code to keep
2298 * the lock if the path points to the last slot in the block. This is part of
2299 * walking through the tree, and selecting the next slot in the higher block.
2301 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2302 * if lowest_unlock is 1, level 0 won't be unlocked
2304 static noinline void unlock_up(struct btrfs_path *path, int level,
2305 int lowest_unlock, int min_write_lock_level,
2306 int *write_lock_level)
2309 int skip_level = level;
2311 struct extent_buffer *t;
2313 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2314 if (!path->nodes[i])
2316 if (!path->locks[i])
2318 if (!no_skips && path->slots[i] == 0) {
2322 if (!no_skips && path->keep_locks) {
2325 nritems = btrfs_header_nritems(t);
2326 if (nritems < 1 || path->slots[i] >= nritems - 1) {
2331 if (skip_level < i && i >= lowest_unlock)
2335 if (i >= lowest_unlock && i > skip_level) {
2336 btrfs_tree_unlock_rw(t, path->locks[i]);
2338 if (write_lock_level &&
2339 i > min_write_lock_level &&
2340 i <= *write_lock_level) {
2341 *write_lock_level = i - 1;
2348 * This releases any locks held in the path starting at level and
2349 * going all the way up to the root.
2351 * btrfs_search_slot will keep the lock held on higher nodes in a few
2352 * corner cases, such as COW of the block at slot zero in the node. This
2353 * ignores those rules, and it should only be called when there are no
2354 * more updates to be done higher up in the tree.
2356 noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2360 if (path->keep_locks)
2363 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2364 if (!path->nodes[i])
2366 if (!path->locks[i])
2368 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
2374 * helper function for btrfs_search_slot. The goal is to find a block
2375 * in cache without setting the path to blocking. If we find the block
2376 * we return zero and the path is unchanged.
2378 * If we can't find the block, we set the path blocking and do some
2379 * reada. -EAGAIN is returned and the search must be repeated.
2382 read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
2383 struct extent_buffer **eb_ret, int level, int slot,
2384 const struct btrfs_key *key)
2386 struct btrfs_fs_info *fs_info = root->fs_info;
2389 struct extent_buffer *b = *eb_ret;
2390 struct extent_buffer *tmp;
2391 struct btrfs_key first_key;
2395 blocknr = btrfs_node_blockptr(b, slot);
2396 gen = btrfs_node_ptr_generation(b, slot);
2397 parent_level = btrfs_header_level(b);
2398 btrfs_node_key_to_cpu(b, &first_key, slot);
2400 tmp = find_extent_buffer(fs_info, blocknr);
2402 /* first we do an atomic uptodate check */
2403 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2408 /* the pages were up to date, but we failed
2409 * the generation number check. Do a full
2410 * read for the generation number that is correct.
2411 * We must do this without dropping locks so
2412 * we can trust our generation number
2414 btrfs_set_path_blocking(p);
2416 /* now we're allowed to do a blocking uptodate check */
2417 ret = btrfs_read_buffer(tmp, gen, parent_level - 1, &first_key);
2422 free_extent_buffer(tmp);
2423 btrfs_release_path(p);
2428 * reduce lock contention at high levels
2429 * of the btree by dropping locks before
2430 * we read. Don't release the lock on the current
2431 * level because we need to walk this node to figure
2432 * out which blocks to read.
2434 btrfs_unlock_up_safe(p, level + 1);
2435 btrfs_set_path_blocking(p);
2437 if (p->reada != READA_NONE)
2438 reada_for_search(fs_info, p, level, slot, key->objectid);
2441 tmp = read_tree_block(fs_info, blocknr, gen, parent_level - 1,
2445 * If the read above didn't mark this buffer up to date,
2446 * it will never end up being up to date. Set ret to EIO now
2447 * and give up so that our caller doesn't loop forever
2450 if (!extent_buffer_uptodate(tmp))
2452 free_extent_buffer(tmp);
2457 btrfs_release_path(p);
2462 * helper function for btrfs_search_slot. This does all of the checks
2463 * for node-level blocks and does any balancing required based on
2466 * If no extra work was required, zero is returned. If we had to
2467 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2471 setup_nodes_for_search(struct btrfs_trans_handle *trans,
2472 struct btrfs_root *root, struct btrfs_path *p,
2473 struct extent_buffer *b, int level, int ins_len,
2474 int *write_lock_level)
2476 struct btrfs_fs_info *fs_info = root->fs_info;
2479 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2480 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
2483 if (*write_lock_level < level + 1) {
2484 *write_lock_level = level + 1;
2485 btrfs_release_path(p);
2489 btrfs_set_path_blocking(p);
2490 reada_for_balance(fs_info, p, level);
2491 sret = split_node(trans, root, p, level);
2498 b = p->nodes[level];
2499 } else if (ins_len < 0 && btrfs_header_nritems(b) <
2500 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
2503 if (*write_lock_level < level + 1) {
2504 *write_lock_level = level + 1;
2505 btrfs_release_path(p);
2509 btrfs_set_path_blocking(p);
2510 reada_for_balance(fs_info, p, level);
2511 sret = balance_level(trans, root, p, level);
2517 b = p->nodes[level];
2519 btrfs_release_path(p);
2522 BUG_ON(btrfs_header_nritems(b) == 1);
2532 static void key_search_validate(struct extent_buffer *b,
2533 const struct btrfs_key *key,
2536 #ifdef CONFIG_BTRFS_ASSERT
2537 struct btrfs_disk_key disk_key;
2539 btrfs_cpu_key_to_disk(&disk_key, key);
2542 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2543 offsetof(struct btrfs_leaf, items[0].key),
2546 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2547 offsetof(struct btrfs_node, ptrs[0].key),
2552 static int key_search(struct extent_buffer *b, const struct btrfs_key *key,
2553 int level, int *prev_cmp, int *slot)
2555 if (*prev_cmp != 0) {
2556 *prev_cmp = btrfs_bin_search(b, key, level, slot);
2560 key_search_validate(b, key, level);
2566 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
2567 u64 iobjectid, u64 ioff, u8 key_type,
2568 struct btrfs_key *found_key)
2571 struct btrfs_key key;
2572 struct extent_buffer *eb;
2577 key.type = key_type;
2578 key.objectid = iobjectid;
2581 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
2585 eb = path->nodes[0];
2586 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2587 ret = btrfs_next_leaf(fs_root, path);
2590 eb = path->nodes[0];
2593 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2594 if (found_key->type != key.type ||
2595 found_key->objectid != key.objectid)
2601 static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,
2602 struct btrfs_path *p,
2603 int write_lock_level)
2605 struct btrfs_fs_info *fs_info = root->fs_info;
2606 struct extent_buffer *b;
2610 /* We try very hard to do read locks on the root */
2611 root_lock = BTRFS_READ_LOCK;
2613 if (p->search_commit_root) {
2615 * The commit roots are read only so we always do read locks,
2616 * and we always must hold the commit_root_sem when doing
2617 * searches on them, the only exception is send where we don't
2618 * want to block transaction commits for a long time, so
2619 * we need to clone the commit root in order to avoid races
2620 * with transaction commits that create a snapshot of one of
2621 * the roots used by a send operation.
2623 if (p->need_commit_sem) {
2624 down_read(&fs_info->commit_root_sem);
2625 b = btrfs_clone_extent_buffer(root->commit_root);
2626 up_read(&fs_info->commit_root_sem);
2628 return ERR_PTR(-ENOMEM);
2631 b = root->commit_root;
2632 extent_buffer_get(b);
2634 level = btrfs_header_level(b);
2636 * Ensure that all callers have set skip_locking when
2637 * p->search_commit_root = 1.
2639 ASSERT(p->skip_locking == 1);
2644 if (p->skip_locking) {
2645 b = btrfs_root_node(root);
2646 level = btrfs_header_level(b);
2651 * If the level is set to maximum, we can skip trying to get the read
2654 if (write_lock_level < BTRFS_MAX_LEVEL) {
2656 * We don't know the level of the root node until we actually
2657 * have it read locked
2659 b = btrfs_read_lock_root_node(root);
2660 level = btrfs_header_level(b);
2661 if (level > write_lock_level)
2664 /* Whoops, must trade for write lock */
2665 btrfs_tree_read_unlock(b);
2666 free_extent_buffer(b);
2669 b = btrfs_lock_root_node(root);
2670 root_lock = BTRFS_WRITE_LOCK;
2672 /* The level might have changed, check again */
2673 level = btrfs_header_level(b);
2676 p->nodes[level] = b;
2677 if (!p->skip_locking)
2678 p->locks[level] = root_lock;
2680 * Callers are responsible for dropping b's references.
2687 * btrfs_search_slot - look for a key in a tree and perform necessary
2688 * modifications to preserve tree invariants.
2690 * @trans: Handle of transaction, used when modifying the tree
2691 * @p: Holds all btree nodes along the search path
2692 * @root: The root node of the tree
2693 * @key: The key we are looking for
2694 * @ins_len: Indicates purpose of search, for inserts it is 1, for
2695 * deletions it's -1. 0 for plain searches
2696 * @cow: boolean should CoW operations be performed. Must always be 1
2697 * when modifying the tree.
2699 * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
2700 * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
2702 * If @key is found, 0 is returned and you can find the item in the leaf level
2703 * of the path (level 0)
2705 * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
2706 * points to the slot where it should be inserted
2708 * If an error is encountered while searching the tree a negative error number
2711 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2712 const struct btrfs_key *key, struct btrfs_path *p,
2713 int ins_len, int cow)
2715 struct btrfs_fs_info *fs_info = root->fs_info;
2716 struct extent_buffer *b;
2721 int lowest_unlock = 1;
2722 /* everything at write_lock_level or lower must be write locked */
2723 int write_lock_level = 0;
2724 u8 lowest_level = 0;
2725 int min_write_lock_level;
2728 lowest_level = p->lowest_level;
2729 WARN_ON(lowest_level && ins_len > 0);
2730 WARN_ON(p->nodes[0] != NULL);
2731 BUG_ON(!cow && ins_len);
2736 /* when we are removing items, we might have to go up to level
2737 * two as we update tree pointers Make sure we keep write
2738 * for those levels as well
2740 write_lock_level = 2;
2741 } else if (ins_len > 0) {
2743 * for inserting items, make sure we have a write lock on
2744 * level 1 so we can update keys
2746 write_lock_level = 1;
2750 write_lock_level = -1;
2752 if (cow && (p->keep_locks || p->lowest_level))
2753 write_lock_level = BTRFS_MAX_LEVEL;
2755 min_write_lock_level = write_lock_level;
2759 b = btrfs_search_slot_get_root(root, p, write_lock_level);
2766 level = btrfs_header_level(b);
2769 * setup the path here so we can release it under lock
2770 * contention with the cow code
2773 bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
2776 * if we don't really need to cow this block
2777 * then we don't want to set the path blocking,
2778 * so we test it here
2780 if (!should_cow_block(trans, root, b)) {
2781 trans->dirty = true;
2786 * must have write locks on this node and the
2789 if (level > write_lock_level ||
2790 (level + 1 > write_lock_level &&
2791 level + 1 < BTRFS_MAX_LEVEL &&
2792 p->nodes[level + 1])) {
2793 write_lock_level = level + 1;
2794 btrfs_release_path(p);
2798 btrfs_set_path_blocking(p);
2800 err = btrfs_cow_block(trans, root, b, NULL, 0,
2803 err = btrfs_cow_block(trans, root, b,
2804 p->nodes[level + 1],
2805 p->slots[level + 1], &b);
2812 p->nodes[level] = b;
2814 * Leave path with blocking locks to avoid massive
2815 * lock context switch, this is made on purpose.
2819 * we have a lock on b and as long as we aren't changing
2820 * the tree, there is no way to for the items in b to change.
2821 * It is safe to drop the lock on our parent before we
2822 * go through the expensive btree search on b.
2824 * If we're inserting or deleting (ins_len != 0), then we might
2825 * be changing slot zero, which may require changing the parent.
2826 * So, we can't drop the lock until after we know which slot
2827 * we're operating on.
2829 if (!ins_len && !p->keep_locks) {
2832 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2833 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2838 ret = key_search(b, key, level, &prev_cmp, &slot);
2844 if (ret && slot > 0) {
2848 p->slots[level] = slot;
2849 err = setup_nodes_for_search(trans, root, p, b, level,
2850 ins_len, &write_lock_level);
2857 b = p->nodes[level];
2858 slot = p->slots[level];
2861 * slot 0 is special, if we change the key
2862 * we have to update the parent pointer
2863 * which means we must have a write lock
2866 if (slot == 0 && ins_len &&
2867 write_lock_level < level + 1) {
2868 write_lock_level = level + 1;
2869 btrfs_release_path(p);
2873 unlock_up(p, level, lowest_unlock,
2874 min_write_lock_level, &write_lock_level);
2876 if (level == lowest_level) {
2882 err = read_block_for_search(root, p, &b, level,
2891 if (!p->skip_locking) {
2892 level = btrfs_header_level(b);
2893 if (level <= write_lock_level) {
2894 err = btrfs_try_tree_write_lock(b);
2896 btrfs_set_path_blocking(p);
2899 p->locks[level] = BTRFS_WRITE_LOCK;
2901 err = btrfs_tree_read_lock_atomic(b);
2903 btrfs_set_path_blocking(p);
2904 btrfs_tree_read_lock(b);
2906 p->locks[level] = BTRFS_READ_LOCK;
2908 p->nodes[level] = b;
2911 p->slots[level] = slot;
2913 btrfs_leaf_free_space(fs_info, b) < ins_len) {
2914 if (write_lock_level < 1) {
2915 write_lock_level = 1;
2916 btrfs_release_path(p);
2920 btrfs_set_path_blocking(p);
2921 err = split_leaf(trans, root, key,
2922 p, ins_len, ret == 0);
2930 if (!p->search_for_split)
2931 unlock_up(p, level, lowest_unlock,
2932 min_write_lock_level, NULL);
2939 * we don't really know what they plan on doing with the path
2940 * from here on, so for now just mark it as blocking
2942 if (!p->leave_spinning)
2943 btrfs_set_path_blocking(p);
2944 if (ret < 0 && !p->skip_release_on_error)
2945 btrfs_release_path(p);
2950 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2951 * current state of the tree together with the operations recorded in the tree
2952 * modification log to search for the key in a previous version of this tree, as
2953 * denoted by the time_seq parameter.
2955 * Naturally, there is no support for insert, delete or cow operations.
2957 * The resulting path and return value will be set up as if we called
2958 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2960 int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
2961 struct btrfs_path *p, u64 time_seq)
2963 struct btrfs_fs_info *fs_info = root->fs_info;
2964 struct extent_buffer *b;
2969 int lowest_unlock = 1;
2970 u8 lowest_level = 0;
2973 lowest_level = p->lowest_level;
2974 WARN_ON(p->nodes[0] != NULL);
2976 if (p->search_commit_root) {
2978 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2982 b = get_old_root(root, time_seq);
2987 level = btrfs_header_level(b);
2988 p->locks[level] = BTRFS_READ_LOCK;
2991 level = btrfs_header_level(b);
2992 p->nodes[level] = b;
2995 * we have a lock on b and as long as we aren't changing
2996 * the tree, there is no way to for the items in b to change.
2997 * It is safe to drop the lock on our parent before we
2998 * go through the expensive btree search on b.
3000 btrfs_unlock_up_safe(p, level + 1);
3003 * Since we can unwind ebs we want to do a real search every
3007 ret = key_search(b, key, level, &prev_cmp, &slot);
3011 if (ret && slot > 0) {
3015 p->slots[level] = slot;
3016 unlock_up(p, level, lowest_unlock, 0, NULL);
3018 if (level == lowest_level) {
3024 err = read_block_for_search(root, p, &b, level,
3033 level = btrfs_header_level(b);
3034 err = btrfs_tree_read_lock_atomic(b);
3036 btrfs_set_path_blocking(p);
3037 btrfs_tree_read_lock(b);
3039 b = tree_mod_log_rewind(fs_info, p, b, time_seq);
3044 p->locks[level] = BTRFS_READ_LOCK;
3045 p->nodes[level] = b;
3047 p->slots[level] = slot;
3048 unlock_up(p, level, lowest_unlock, 0, NULL);
3054 if (!p->leave_spinning)
3055 btrfs_set_path_blocking(p);
3057 btrfs_release_path(p);
3063 * helper to use instead of search slot if no exact match is needed but
3064 * instead the next or previous item should be returned.
3065 * When find_higher is true, the next higher item is returned, the next lower
3067 * When return_any and find_higher are both true, and no higher item is found,
3068 * return the next lower instead.
3069 * When return_any is true and find_higher is false, and no lower item is found,
3070 * return the next higher instead.
3071 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3074 int btrfs_search_slot_for_read(struct btrfs_root *root,
3075 const struct btrfs_key *key,
3076 struct btrfs_path *p, int find_higher,
3080 struct extent_buffer *leaf;
3083 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3087 * a return value of 1 means the path is at the position where the
3088 * item should be inserted. Normally this is the next bigger item,
3089 * but in case the previous item is the last in a leaf, path points
3090 * to the first free slot in the previous leaf, i.e. at an invalid
3096 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3097 ret = btrfs_next_leaf(root, p);
3103 * no higher item found, return the next
3108 btrfs_release_path(p);
3112 if (p->slots[0] == 0) {
3113 ret = btrfs_prev_leaf(root, p);
3118 if (p->slots[0] == btrfs_header_nritems(leaf))
3125 * no lower item found, return the next
3130 btrfs_release_path(p);
3140 * adjust the pointers going up the tree, starting at level
3141 * making sure the right key of each node is points to 'key'.
3142 * This is used after shifting pointers to the left, so it stops
3143 * fixing up pointers when a given leaf/node is not in slot 0 of the
3147 static void fixup_low_keys(struct btrfs_path *path,
3148 struct btrfs_disk_key *key, int level)
3151 struct extent_buffer *t;
3154 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
3155 int tslot = path->slots[i];
3157 if (!path->nodes[i])
3160 ret = tree_mod_log_insert_key(t, tslot, MOD_LOG_KEY_REPLACE,
3163 btrfs_set_node_key(t, key, tslot);
3164 btrfs_mark_buffer_dirty(path->nodes[i]);
3173 * This function isn't completely safe. It's the caller's responsibility
3174 * that the new key won't break the order
3176 void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
3177 struct btrfs_path *path,
3178 const struct btrfs_key *new_key)
3180 struct btrfs_disk_key disk_key;
3181 struct extent_buffer *eb;
3184 eb = path->nodes[0];
3185 slot = path->slots[0];
3187 btrfs_item_key(eb, &disk_key, slot - 1);
3188 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
3190 if (slot < btrfs_header_nritems(eb) - 1) {
3191 btrfs_item_key(eb, &disk_key, slot + 1);
3192 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
3195 btrfs_cpu_key_to_disk(&disk_key, new_key);
3196 btrfs_set_item_key(eb, &disk_key, slot);
3197 btrfs_mark_buffer_dirty(eb);
3199 fixup_low_keys(path, &disk_key, 1);
3203 * try to push data from one node into the next node left in the
3206 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3207 * error, and > 0 if there was no room in the left hand block.
3209 static int push_node_left(struct btrfs_trans_handle *trans,
3210 struct btrfs_fs_info *fs_info,
3211 struct extent_buffer *dst,
3212 struct extent_buffer *src, int empty)
3219 src_nritems = btrfs_header_nritems(src);
3220 dst_nritems = btrfs_header_nritems(dst);
3221 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
3222 WARN_ON(btrfs_header_generation(src) != trans->transid);
3223 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3225 if (!empty && src_nritems <= 8)
3228 if (push_items <= 0)
3232 push_items = min(src_nritems, push_items);
3233 if (push_items < src_nritems) {
3234 /* leave at least 8 pointers in the node if
3235 * we aren't going to empty it
3237 if (src_nritems - push_items < 8) {
3238 if (push_items <= 8)
3244 push_items = min(src_nritems - 8, push_items);
3246 ret = tree_mod_log_eb_copy(fs_info, dst, src, dst_nritems, 0,
3249 btrfs_abort_transaction(trans, ret);
3252 copy_extent_buffer(dst, src,
3253 btrfs_node_key_ptr_offset(dst_nritems),
3254 btrfs_node_key_ptr_offset(0),
3255 push_items * sizeof(struct btrfs_key_ptr));
3257 if (push_items < src_nritems) {
3259 * Don't call tree_mod_log_insert_move here, key removal was
3260 * already fully logged by tree_mod_log_eb_copy above.
3262 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3263 btrfs_node_key_ptr_offset(push_items),
3264 (src_nritems - push_items) *
3265 sizeof(struct btrfs_key_ptr));
3267 btrfs_set_header_nritems(src, src_nritems - push_items);
3268 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3269 btrfs_mark_buffer_dirty(src);
3270 btrfs_mark_buffer_dirty(dst);
3276 * try to push data from one node into the next node right in the
3279 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3280 * error, and > 0 if there was no room in the right hand block.
3282 * this will only push up to 1/2 the contents of the left node over
3284 static int balance_node_right(struct btrfs_trans_handle *trans,
3285 struct btrfs_fs_info *fs_info,
3286 struct extent_buffer *dst,
3287 struct extent_buffer *src)
3295 WARN_ON(btrfs_header_generation(src) != trans->transid);
3296 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3298 src_nritems = btrfs_header_nritems(src);
3299 dst_nritems = btrfs_header_nritems(dst);
3300 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
3301 if (push_items <= 0)
3304 if (src_nritems < 4)
3307 max_push = src_nritems / 2 + 1;
3308 /* don't try to empty the node */
3309 if (max_push >= src_nritems)
3312 if (max_push < push_items)
3313 push_items = max_push;
3315 ret = tree_mod_log_insert_move(dst, push_items, 0, dst_nritems);
3317 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3318 btrfs_node_key_ptr_offset(0),
3320 sizeof(struct btrfs_key_ptr));
3322 ret = tree_mod_log_eb_copy(fs_info, dst, src, 0,
3323 src_nritems - push_items, push_items);
3325 btrfs_abort_transaction(trans, ret);
3328 copy_extent_buffer(dst, src,
3329 btrfs_node_key_ptr_offset(0),
3330 btrfs_node_key_ptr_offset(src_nritems - push_items),
3331 push_items * sizeof(struct btrfs_key_ptr));
3333 btrfs_set_header_nritems(src, src_nritems - push_items);
3334 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3336 btrfs_mark_buffer_dirty(src);
3337 btrfs_mark_buffer_dirty(dst);
3343 * helper function to insert a new root level in the tree.
3344 * A new node is allocated, and a single item is inserted to
3345 * point to the existing root
3347 * returns zero on success or < 0 on failure.
3349 static noinline int insert_new_root(struct btrfs_trans_handle *trans,
3350 struct btrfs_root *root,
3351 struct btrfs_path *path, int level)
3353 struct btrfs_fs_info *fs_info = root->fs_info;
3355 struct extent_buffer *lower;
3356 struct extent_buffer *c;
3357 struct extent_buffer *old;
3358 struct btrfs_disk_key lower_key;
3361 BUG_ON(path->nodes[level]);
3362 BUG_ON(path->nodes[level-1] != root->node);
3364 lower = path->nodes[level-1];
3366 btrfs_item_key(lower, &lower_key, 0);
3368 btrfs_node_key(lower, &lower_key, 0);
3370 c = alloc_tree_block_no_bg_flush(trans, root, 0, &lower_key, level,
3371 root->node->start, 0);
3375 root_add_used(root, fs_info->nodesize);
3377 btrfs_set_header_nritems(c, 1);
3378 btrfs_set_node_key(c, &lower_key, 0);
3379 btrfs_set_node_blockptr(c, 0, lower->start);
3380 lower_gen = btrfs_header_generation(lower);
3381 WARN_ON(lower_gen != trans->transid);
3383 btrfs_set_node_ptr_generation(c, 0, lower_gen);
3385 btrfs_mark_buffer_dirty(c);
3388 ret = tree_mod_log_insert_root(root->node, c, 0);
3390 rcu_assign_pointer(root->node, c);
3392 /* the super has an extra ref to root->node */
3393 free_extent_buffer(old);
3395 add_root_to_dirty_list(root);
3396 extent_buffer_get(c);
3397 path->nodes[level] = c;
3398 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
3399 path->slots[level] = 0;
3404 * worker function to insert a single pointer in a node.
3405 * the node should have enough room for the pointer already
3407 * slot and level indicate where you want the key to go, and
3408 * blocknr is the block the key points to.
3410 static void insert_ptr(struct btrfs_trans_handle *trans,
3411 struct btrfs_fs_info *fs_info, struct btrfs_path *path,
3412 struct btrfs_disk_key *key, u64 bytenr,
3413 int slot, int level)
3415 struct extent_buffer *lower;
3419 BUG_ON(!path->nodes[level]);
3420 btrfs_assert_tree_locked(path->nodes[level]);
3421 lower = path->nodes[level];
3422 nritems = btrfs_header_nritems(lower);
3423 BUG_ON(slot > nritems);
3424 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(fs_info));
3425 if (slot != nritems) {
3427 ret = tree_mod_log_insert_move(lower, slot + 1, slot,
3431 memmove_extent_buffer(lower,
3432 btrfs_node_key_ptr_offset(slot + 1),
3433 btrfs_node_key_ptr_offset(slot),
3434 (nritems - slot) * sizeof(struct btrfs_key_ptr));
3437 ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD,
3441 btrfs_set_node_key(lower, key, slot);
3442 btrfs_set_node_blockptr(lower, slot, bytenr);
3443 WARN_ON(trans->transid == 0);
3444 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
3445 btrfs_set_header_nritems(lower, nritems + 1);
3446 btrfs_mark_buffer_dirty(lower);
3450 * split the node at the specified level in path in two.
3451 * The path is corrected to point to the appropriate node after the split
3453 * Before splitting this tries to make some room in the node by pushing
3454 * left and right, if either one works, it returns right away.
3456 * returns 0 on success and < 0 on failure
3458 static noinline int split_node(struct btrfs_trans_handle *trans,
3459 struct btrfs_root *root,
3460 struct btrfs_path *path, int level)
3462 struct btrfs_fs_info *fs_info = root->fs_info;
3463 struct extent_buffer *c;
3464 struct extent_buffer *split;
3465 struct btrfs_disk_key disk_key;
3470 c = path->nodes[level];
3471 WARN_ON(btrfs_header_generation(c) != trans->transid);
3472 if (c == root->node) {
3474 * trying to split the root, lets make a new one
3476 * tree mod log: We don't log_removal old root in
3477 * insert_new_root, because that root buffer will be kept as a
3478 * normal node. We are going to log removal of half of the
3479 * elements below with tree_mod_log_eb_copy. We're holding a
3480 * tree lock on the buffer, which is why we cannot race with
3481 * other tree_mod_log users.
3483 ret = insert_new_root(trans, root, path, level + 1);
3487 ret = push_nodes_for_insert(trans, root, path, level);
3488 c = path->nodes[level];
3489 if (!ret && btrfs_header_nritems(c) <
3490 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
3496 c_nritems = btrfs_header_nritems(c);
3497 mid = (c_nritems + 1) / 2;
3498 btrfs_node_key(c, &disk_key, mid);
3500 split = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, level,
3503 return PTR_ERR(split);
3505 root_add_used(root, fs_info->nodesize);
3506 ASSERT(btrfs_header_level(c) == level);
3508 ret = tree_mod_log_eb_copy(fs_info, split, c, 0, mid, c_nritems - mid);
3510 btrfs_abort_transaction(trans, ret);
3513 copy_extent_buffer(split, c,
3514 btrfs_node_key_ptr_offset(0),
3515 btrfs_node_key_ptr_offset(mid),
3516 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3517 btrfs_set_header_nritems(split, c_nritems - mid);
3518 btrfs_set_header_nritems(c, mid);
3521 btrfs_mark_buffer_dirty(c);
3522 btrfs_mark_buffer_dirty(split);
3524 insert_ptr(trans, fs_info, path, &disk_key, split->start,
3525 path->slots[level + 1] + 1, level + 1);
3527 if (path->slots[level] >= mid) {
3528 path->slots[level] -= mid;
3529 btrfs_tree_unlock(c);
3530 free_extent_buffer(c);
3531 path->nodes[level] = split;
3532 path->slots[level + 1] += 1;
3534 btrfs_tree_unlock(split);
3535 free_extent_buffer(split);
3541 * how many bytes are required to store the items in a leaf. start
3542 * and nr indicate which items in the leaf to check. This totals up the
3543 * space used both by the item structs and the item data
3545 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
3547 struct btrfs_item *start_item;
3548 struct btrfs_item *end_item;
3549 struct btrfs_map_token token;
3551 int nritems = btrfs_header_nritems(l);
3552 int end = min(nritems, start + nr) - 1;
3556 btrfs_init_map_token(&token);
3557 start_item = btrfs_item_nr(start);
3558 end_item = btrfs_item_nr(end);
3559 data_len = btrfs_token_item_offset(l, start_item, &token) +
3560 btrfs_token_item_size(l, start_item, &token);
3561 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
3562 data_len += sizeof(struct btrfs_item) * nr;
3563 WARN_ON(data_len < 0);
3568 * The space between the end of the leaf items and
3569 * the start of the leaf data. IOW, how much room
3570 * the leaf has left for both items and data
3572 noinline int btrfs_leaf_free_space(struct btrfs_fs_info *fs_info,
3573 struct extent_buffer *leaf)
3575 int nritems = btrfs_header_nritems(leaf);
3578 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
3581 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
3583 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
3584 leaf_space_used(leaf, 0, nritems), nritems);
3590 * min slot controls the lowest index we're willing to push to the
3591 * right. We'll push up to and including min_slot, but no lower
3593 static noinline int __push_leaf_right(struct btrfs_fs_info *fs_info,
3594 struct btrfs_path *path,
3595 int data_size, int empty,
3596 struct extent_buffer *right,
3597 int free_space, u32 left_nritems,
3600 struct extent_buffer *left = path->nodes[0];
3601 struct extent_buffer *upper = path->nodes[1];
3602 struct btrfs_map_token token;
3603 struct btrfs_disk_key disk_key;
3608 struct btrfs_item *item;
3614 btrfs_init_map_token(&token);
3619 nr = max_t(u32, 1, min_slot);
3621 if (path->slots[0] >= left_nritems)
3622 push_space += data_size;
3624 slot = path->slots[1];
3625 i = left_nritems - 1;
3627 item = btrfs_item_nr(i);
3629 if (!empty && push_items > 0) {
3630 if (path->slots[0] > i)
3632 if (path->slots[0] == i) {
3633 int space = btrfs_leaf_free_space(fs_info, left);
3634 if (space + push_space * 2 > free_space)
3639 if (path->slots[0] == i)
3640 push_space += data_size;
3642 this_item_size = btrfs_item_size(left, item);
3643 if (this_item_size + sizeof(*item) + push_space > free_space)
3647 push_space += this_item_size + sizeof(*item);
3653 if (push_items == 0)
3656 WARN_ON(!empty && push_items == left_nritems);
3658 /* push left to right */
3659 right_nritems = btrfs_header_nritems(right);
3661 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
3662 push_space -= leaf_data_end(fs_info, left);
3664 /* make room in the right data area */
3665 data_end = leaf_data_end(fs_info, right);
3666 memmove_extent_buffer(right,
3667 BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
3668 BTRFS_LEAF_DATA_OFFSET + data_end,
3669 BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
3671 /* copy from the left data area */
3672 copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
3673 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
3674 BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, left),
3677 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3678 btrfs_item_nr_offset(0),
3679 right_nritems * sizeof(struct btrfs_item));
3681 /* copy the items from left to right */
3682 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3683 btrfs_item_nr_offset(left_nritems - push_items),
3684 push_items * sizeof(struct btrfs_item));
3686 /* update the item pointers */
3687 right_nritems += push_items;
3688 btrfs_set_header_nritems(right, right_nritems);
3689 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
3690 for (i = 0; i < right_nritems; i++) {
3691 item = btrfs_item_nr(i);
3692 push_space -= btrfs_token_item_size(right, item, &token);
3693 btrfs_set_token_item_offset(right, item, push_space, &token);
3696 left_nritems -= push_items;
3697 btrfs_set_header_nritems(left, left_nritems);
3700 btrfs_mark_buffer_dirty(left);
3702 clean_tree_block(fs_info, left);
3704 btrfs_mark_buffer_dirty(right);
3706 btrfs_item_key(right, &disk_key, 0);
3707 btrfs_set_node_key(upper, &disk_key, slot + 1);
3708 btrfs_mark_buffer_dirty(upper);
3710 /* then fixup the leaf pointer in the path */
3711 if (path->slots[0] >= left_nritems) {
3712 path->slots[0] -= left_nritems;
3713 if (btrfs_header_nritems(path->nodes[0]) == 0)
3714 clean_tree_block(fs_info, path->nodes[0]);
3715 btrfs_tree_unlock(path->nodes[0]);
3716 free_extent_buffer(path->nodes[0]);
3717 path->nodes[0] = right;
3718 path->slots[1] += 1;
3720 btrfs_tree_unlock(right);
3721 free_extent_buffer(right);
3726 btrfs_tree_unlock(right);
3727 free_extent_buffer(right);
3732 * push some data in the path leaf to the right, trying to free up at
3733 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3735 * returns 1 if the push failed because the other node didn't have enough
3736 * room, 0 if everything worked out and < 0 if there were major errors.
3738 * this will push starting from min_slot to the end of the leaf. It won't
3739 * push any slot lower than min_slot
3741 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
3742 *root, struct btrfs_path *path,
3743 int min_data_size, int data_size,
3744 int empty, u32 min_slot)
3746 struct btrfs_fs_info *fs_info = root->fs_info;
3747 struct extent_buffer *left = path->nodes[0];
3748 struct extent_buffer *right;
3749 struct extent_buffer *upper;
3755 if (!path->nodes[1])
3758 slot = path->slots[1];
3759 upper = path->nodes[1];
3760 if (slot >= btrfs_header_nritems(upper) - 1)
3763 btrfs_assert_tree_locked(path->nodes[1]);
3765 right = read_node_slot(fs_info, upper, slot + 1);
3767 * slot + 1 is not valid or we fail to read the right node,
3768 * no big deal, just return.
3773 btrfs_tree_lock(right);
3774 btrfs_set_lock_blocking(right);
3776 free_space = btrfs_leaf_free_space(fs_info, right);
3777 if (free_space < data_size)
3780 /* cow and double check */
3781 ret = btrfs_cow_block(trans, root, right, upper,
3786 free_space = btrfs_leaf_free_space(fs_info, right);
3787 if (free_space < data_size)
3790 left_nritems = btrfs_header_nritems(left);
3791 if (left_nritems == 0)
3794 if (path->slots[0] == left_nritems && !empty) {
3795 /* Key greater than all keys in the leaf, right neighbor has
3796 * enough room for it and we're not emptying our leaf to delete
3797 * it, therefore use right neighbor to insert the new item and
3798 * no need to touch/dirty our left leaf. */
3799 btrfs_tree_unlock(left);
3800 free_extent_buffer(left);
3801 path->nodes[0] = right;
3807 return __push_leaf_right(fs_info, path, min_data_size, empty,
3808 right, free_space, left_nritems, min_slot);
3810 btrfs_tree_unlock(right);
3811 free_extent_buffer(right);
3816 * push some data in the path leaf to the left, trying to free up at
3817 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3819 * max_slot can put a limit on how far into the leaf we'll push items. The
3820 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3823 static noinline int __push_leaf_left(struct btrfs_fs_info *fs_info,
3824 struct btrfs_path *path, int data_size,
3825 int empty, struct extent_buffer *left,
3826 int free_space, u32 right_nritems,
3829 struct btrfs_disk_key disk_key;
3830 struct extent_buffer *right = path->nodes[0];
3834 struct btrfs_item *item;
3835 u32 old_left_nritems;
3839 u32 old_left_item_size;
3840 struct btrfs_map_token token;
3842 btrfs_init_map_token(&token);
3845 nr = min(right_nritems, max_slot);
3847 nr = min(right_nritems - 1, max_slot);
3849 for (i = 0; i < nr; i++) {
3850 item = btrfs_item_nr(i);
3852 if (!empty && push_items > 0) {
3853 if (path->slots[0] < i)
3855 if (path->slots[0] == i) {
3856 int space = btrfs_leaf_free_space(fs_info, right);
3857 if (space + push_space * 2 > free_space)
3862 if (path->slots[0] == i)
3863 push_space += data_size;
3865 this_item_size = btrfs_item_size(right, item);
3866 if (this_item_size + sizeof(*item) + push_space > free_space)
3870 push_space += this_item_size + sizeof(*item);
3873 if (push_items == 0) {
3877 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
3879 /* push data from right to left */
3880 copy_extent_buffer(left, right,
3881 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3882 btrfs_item_nr_offset(0),
3883 push_items * sizeof(struct btrfs_item));
3885 push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
3886 btrfs_item_offset_nr(right, push_items - 1);
3888 copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
3889 leaf_data_end(fs_info, left) - push_space,
3890 BTRFS_LEAF_DATA_OFFSET +
3891 btrfs_item_offset_nr(right, push_items - 1),
3893 old_left_nritems = btrfs_header_nritems(left);
3894 BUG_ON(old_left_nritems <= 0);
3896 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
3897 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
3900 item = btrfs_item_nr(i);
3902 ioff = btrfs_token_item_offset(left, item, &token);
3903 btrfs_set_token_item_offset(left, item,
3904 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size),
3907 btrfs_set_header_nritems(left, old_left_nritems + push_items);
3909 /* fixup right node */
3910 if (push_items > right_nritems)
3911 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
3914 if (push_items < right_nritems) {
3915 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3916 leaf_data_end(fs_info, right);
3917 memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
3918 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
3919 BTRFS_LEAF_DATA_OFFSET +
3920 leaf_data_end(fs_info, right), push_space);
3922 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
3923 btrfs_item_nr_offset(push_items),
3924 (btrfs_header_nritems(right) - push_items) *
3925 sizeof(struct btrfs_item));
3927 right_nritems -= push_items;
3928 btrfs_set_header_nritems(right, right_nritems);
3929 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
3930 for (i = 0; i < right_nritems; i++) {
3931 item = btrfs_item_nr(i);
3933 push_space = push_space - btrfs_token_item_size(right,
3935 btrfs_set_token_item_offset(right, item, push_space, &token);
3938 btrfs_mark_buffer_dirty(left);
3940 btrfs_mark_buffer_dirty(right);
3942 clean_tree_block(fs_info, right);
3944 btrfs_item_key(right, &disk_key, 0);
3945 fixup_low_keys(path, &disk_key, 1);
3947 /* then fixup the leaf pointer in the path */
3948 if (path->slots[0] < push_items) {
3949 path->slots[0] += old_left_nritems;
3950 btrfs_tree_unlock(path->nodes[0]);
3951 free_extent_buffer(path->nodes[0]);
3952 path->nodes[0] = left;
3953 path->slots[1] -= 1;
3955 btrfs_tree_unlock(left);
3956 free_extent_buffer(left);
3957 path->slots[0] -= push_items;
3959 BUG_ON(path->slots[0] < 0);
3962 btrfs_tree_unlock(left);
3963 free_extent_buffer(left);
3968 * push some data in the path leaf to the left, trying to free up at
3969 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3971 * max_slot can put a limit on how far into the leaf we'll push items. The
3972 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3975 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
3976 *root, struct btrfs_path *path, int min_data_size,
3977 int data_size, int empty, u32 max_slot)
3979 struct btrfs_fs_info *fs_info = root->fs_info;
3980 struct extent_buffer *right = path->nodes[0];
3981 struct extent_buffer *left;
3987 slot = path->slots[1];
3990 if (!path->nodes[1])
3993 right_nritems = btrfs_header_nritems(right);
3994 if (right_nritems == 0)
3997 btrfs_assert_tree_locked(path->nodes[1]);
3999 left = read_node_slot(fs_info, path->nodes[1], slot - 1);
4001 * slot - 1 is not valid or we fail to read the left node,
4002 * no big deal, just return.
4007 btrfs_tree_lock(left);
4008 btrfs_set_lock_blocking(left);
4010 free_space = btrfs_leaf_free_space(fs_info, left);
4011 if (free_space < data_size) {
4016 /* cow and double check */
4017 ret = btrfs_cow_block(trans, root, left,
4018 path->nodes[1], slot - 1, &left);
4020 /* we hit -ENOSPC, but it isn't fatal here */
4026 free_space = btrfs_leaf_free_space(fs_info, left);
4027 if (free_space < data_size) {
4032 return __push_leaf_left(fs_info, path, min_data_size,
4033 empty, left, free_space, right_nritems,
4036 btrfs_tree_unlock(left);
4037 free_extent_buffer(left);
4042 * split the path's leaf in two, making sure there is at least data_size
4043 * available for the resulting leaf level of the path.
4045 static noinline void copy_for_split(struct btrfs_trans_handle *trans,
4046 struct btrfs_fs_info *fs_info,
4047 struct btrfs_path *path,
4048 struct extent_buffer *l,
4049 struct extent_buffer *right,
4050 int slot, int mid, int nritems)
4055 struct btrfs_disk_key disk_key;
4056 struct btrfs_map_token token;
4058 btrfs_init_map_token(&token);
4060 nritems = nritems - mid;
4061 btrfs_set_header_nritems(right, nritems);
4062 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(fs_info, l);
4064 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4065 btrfs_item_nr_offset(mid),
4066 nritems * sizeof(struct btrfs_item));
4068 copy_extent_buffer(right, l,
4069 BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
4070 data_copy_size, BTRFS_LEAF_DATA_OFFSET +
4071 leaf_data_end(fs_info, l), data_copy_size);
4073 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
4075 for (i = 0; i < nritems; i++) {
4076 struct btrfs_item *item = btrfs_item_nr(i);
4079 ioff = btrfs_token_item_offset(right, item, &token);
4080 btrfs_set_token_item_offset(right, item,
4081 ioff + rt_data_off, &token);
4084 btrfs_set_header_nritems(l, mid);
4085 btrfs_item_key(right, &disk_key, 0);
4086 insert_ptr(trans, fs_info, path, &disk_key, right->start,
4087 path->slots[1] + 1, 1);
4089 btrfs_mark_buffer_dirty(right);
4090 btrfs_mark_buffer_dirty(l);
4091 BUG_ON(path->slots[0] != slot);
4094 btrfs_tree_unlock(path->nodes[0]);
4095 free_extent_buffer(path->nodes[0]);
4096 path->nodes[0] = right;
4097 path->slots[0] -= mid;
4098 path->slots[1] += 1;
4100 btrfs_tree_unlock(right);
4101 free_extent_buffer(right);
4104 BUG_ON(path->slots[0] < 0);
4108 * double splits happen when we need to insert a big item in the middle
4109 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4110 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4113 * We avoid this by trying to push the items on either side of our target
4114 * into the adjacent leaves. If all goes well we can avoid the double split
4117 static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4118 struct btrfs_root *root,
4119 struct btrfs_path *path,
4122 struct btrfs_fs_info *fs_info = root->fs_info;
4127 int space_needed = data_size;
4129 slot = path->slots[0];
4130 if (slot < btrfs_header_nritems(path->nodes[0]))
4131 space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
4134 * try to push all the items after our slot into the
4137 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
4144 nritems = btrfs_header_nritems(path->nodes[0]);
4146 * our goal is to get our slot at the start or end of a leaf. If
4147 * we've done so we're done
4149 if (path->slots[0] == 0 || path->slots[0] == nritems)
4152 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
4155 /* try to push all the items before our slot into the next leaf */
4156 slot = path->slots[0];
4157 space_needed = data_size;
4159 space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
4160 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
4173 * split the path's leaf in two, making sure there is at least data_size
4174 * available for the resulting leaf level of the path.
4176 * returns 0 if all went well and < 0 on failure.
4178 static noinline int split_leaf(struct btrfs_trans_handle *trans,
4179 struct btrfs_root *root,
4180 const struct btrfs_key *ins_key,
4181 struct btrfs_path *path, int data_size,
4184 struct btrfs_disk_key disk_key;
4185 struct extent_buffer *l;
4189 struct extent_buffer *right;
4190 struct btrfs_fs_info *fs_info = root->fs_info;
4194 int num_doubles = 0;
4195 int tried_avoid_double = 0;
4198 slot = path->slots[0];
4199 if (extend && data_size + btrfs_item_size_nr(l, slot) +
4200 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
4203 /* first try to make some room by pushing left and right */
4204 if (data_size && path->nodes[1]) {
4205 int space_needed = data_size;
4207 if (slot < btrfs_header_nritems(l))
4208 space_needed -= btrfs_leaf_free_space(fs_info, l);
4210 wret = push_leaf_right(trans, root, path, space_needed,
4211 space_needed, 0, 0);
4215 space_needed = data_size;
4217 space_needed -= btrfs_leaf_free_space(fs_info,
4219 wret = push_leaf_left(trans, root, path, space_needed,
4220 space_needed, 0, (u32)-1);
4226 /* did the pushes work? */
4227 if (btrfs_leaf_free_space(fs_info, l) >= data_size)
4231 if (!path->nodes[1]) {
4232 ret = insert_new_root(trans, root, path, 1);
4239 slot = path->slots[0];
4240 nritems = btrfs_header_nritems(l);
4241 mid = (nritems + 1) / 2;
4245 leaf_space_used(l, mid, nritems - mid) + data_size >
4246 BTRFS_LEAF_DATA_SIZE(fs_info)) {
4247 if (slot >= nritems) {
4251 if (mid != nritems &&
4252 leaf_space_used(l, mid, nritems - mid) +
4253 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
4254 if (data_size && !tried_avoid_double)
4255 goto push_for_double;
4261 if (leaf_space_used(l, 0, mid) + data_size >
4262 BTRFS_LEAF_DATA_SIZE(fs_info)) {
4263 if (!extend && data_size && slot == 0) {
4265 } else if ((extend || !data_size) && slot == 0) {
4269 if (mid != nritems &&
4270 leaf_space_used(l, mid, nritems - mid) +
4271 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
4272 if (data_size && !tried_avoid_double)
4273 goto push_for_double;
4281 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4283 btrfs_item_key(l, &disk_key, mid);
4285 right = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, 0,
4288 return PTR_ERR(right);
4290 root_add_used(root, fs_info->nodesize);
4294 btrfs_set_header_nritems(right, 0);
4295 insert_ptr(trans, fs_info, path, &disk_key,
4296 right->start, path->slots[1] + 1, 1);
4297 btrfs_tree_unlock(path->nodes[0]);
4298 free_extent_buffer(path->nodes[0]);
4299 path->nodes[0] = right;
4301 path->slots[1] += 1;
4303 btrfs_set_header_nritems(right, 0);
4304 insert_ptr(trans, fs_info, path, &disk_key,
4305 right->start, path->slots[1], 1);
4306 btrfs_tree_unlock(path->nodes[0]);
4307 free_extent_buffer(path->nodes[0]);
4308 path->nodes[0] = right;
4310 if (path->slots[1] == 0)
4311 fixup_low_keys(path, &disk_key, 1);
4314 * We create a new leaf 'right' for the required ins_len and
4315 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
4316 * the content of ins_len to 'right'.
4321 copy_for_split(trans, fs_info, path, l, right, slot, mid, nritems);
4324 BUG_ON(num_doubles != 0);
4332 push_for_double_split(trans, root, path, data_size);
4333 tried_avoid_double = 1;
4334 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
4339 static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4340 struct btrfs_root *root,
4341 struct btrfs_path *path, int ins_len)
4343 struct btrfs_fs_info *fs_info = root->fs_info;
4344 struct btrfs_key key;
4345 struct extent_buffer *leaf;
4346 struct btrfs_file_extent_item *fi;
4351 leaf = path->nodes[0];
4352 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4354 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4355 key.type != BTRFS_EXTENT_CSUM_KEY);
4357 if (btrfs_leaf_free_space(fs_info, leaf) >= ins_len)
4360 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4361 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4362 fi = btrfs_item_ptr(leaf, path->slots[0],
4363 struct btrfs_file_extent_item);
4364 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4366 btrfs_release_path(path);
4368 path->keep_locks = 1;
4369 path->search_for_split = 1;
4370 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4371 path->search_for_split = 0;
4378 leaf = path->nodes[0];
4379 /* if our item isn't there, return now */
4380 if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4383 /* the leaf has changed, it now has room. return now */
4384 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= ins_len)
4387 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4388 fi = btrfs_item_ptr(leaf, path->slots[0],
4389 struct btrfs_file_extent_item);
4390 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4394 btrfs_set_path_blocking(path);
4395 ret = split_leaf(trans, root, &key, path, ins_len, 1);
4399 path->keep_locks = 0;
4400 btrfs_unlock_up_safe(path, 1);
4403 path->keep_locks = 0;
4407 static noinline int split_item(struct btrfs_fs_info *fs_info,
4408 struct btrfs_path *path,
4409 const struct btrfs_key *new_key,
4410 unsigned long split_offset)
4412 struct extent_buffer *leaf;
4413 struct btrfs_item *item;
4414 struct btrfs_item *new_item;
4420 struct btrfs_disk_key disk_key;
4422 leaf = path->nodes[0];
4423 BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < sizeof(struct btrfs_item));
4425 btrfs_set_path_blocking(path);
4427 item = btrfs_item_nr(path->slots[0]);
4428 orig_offset = btrfs_item_offset(leaf, item);
4429 item_size = btrfs_item_size(leaf, item);
4431 buf = kmalloc(item_size, GFP_NOFS);
4435 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4436 path->slots[0]), item_size);
4438 slot = path->slots[0] + 1;
4439 nritems = btrfs_header_nritems(leaf);
4440 if (slot != nritems) {
4441 /* shift the items */
4442 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
4443 btrfs_item_nr_offset(slot),
4444 (nritems - slot) * sizeof(struct btrfs_item));
4447 btrfs_cpu_key_to_disk(&disk_key, new_key);
4448 btrfs_set_item_key(leaf, &disk_key, slot);
4450 new_item = btrfs_item_nr(slot);
4452 btrfs_set_item_offset(leaf, new_item, orig_offset);
4453 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4455 btrfs_set_item_offset(leaf, item,
4456 orig_offset + item_size - split_offset);
4457 btrfs_set_item_size(leaf, item, split_offset);
4459 btrfs_set_header_nritems(leaf, nritems + 1);
4461 /* write the data for the start of the original item */
4462 write_extent_buffer(leaf, buf,
4463 btrfs_item_ptr_offset(leaf, path->slots[0]),
4466 /* write the data for the new item */
4467 write_extent_buffer(leaf, buf + split_offset,
4468 btrfs_item_ptr_offset(leaf, slot),
4469 item_size - split_offset);
4470 btrfs_mark_buffer_dirty(leaf);
4472 BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < 0);
4478 * This function splits a single item into two items,
4479 * giving 'new_key' to the new item and splitting the
4480 * old one at split_offset (from the start of the item).
4482 * The path may be released by this operation. After
4483 * the split, the path is pointing to the old item. The
4484 * new item is going to be in the same node as the old one.
4486 * Note, the item being split must be smaller enough to live alone on
4487 * a tree block with room for one extra struct btrfs_item
4489 * This allows us to split the item in place, keeping a lock on the
4490 * leaf the entire time.
4492 int btrfs_split_item(struct btrfs_trans_handle *trans,
4493 struct btrfs_root *root,
4494 struct btrfs_path *path,
4495 const struct btrfs_key *new_key,
4496 unsigned long split_offset)
4499 ret = setup_leaf_for_split(trans, root, path,
4500 sizeof(struct btrfs_item));
4504 ret = split_item(root->fs_info, path, new_key, split_offset);
4509 * This function duplicate a item, giving 'new_key' to the new item.
4510 * It guarantees both items live in the same tree leaf and the new item
4511 * is contiguous with the original item.
4513 * This allows us to split file extent in place, keeping a lock on the
4514 * leaf the entire time.
4516 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4517 struct btrfs_root *root,
4518 struct btrfs_path *path,
4519 const struct btrfs_key *new_key)
4521 struct extent_buffer *leaf;
4525 leaf = path->nodes[0];
4526 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4527 ret = setup_leaf_for_split(trans, root, path,
4528 item_size + sizeof(struct btrfs_item));
4533 setup_items_for_insert(root, path, new_key, &item_size,
4534 item_size, item_size +
4535 sizeof(struct btrfs_item), 1);
4536 leaf = path->nodes[0];
4537 memcpy_extent_buffer(leaf,
4538 btrfs_item_ptr_offset(leaf, path->slots[0]),
4539 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4545 * make the item pointed to by the path smaller. new_size indicates
4546 * how small to make it, and from_end tells us if we just chop bytes
4547 * off the end of the item or if we shift the item to chop bytes off
4550 void btrfs_truncate_item(struct btrfs_fs_info *fs_info,
4551 struct btrfs_path *path, u32 new_size, int from_end)
4554 struct extent_buffer *leaf;
4555 struct btrfs_item *item;
4557 unsigned int data_end;
4558 unsigned int old_data_start;
4559 unsigned int old_size;
4560 unsigned int size_diff;
4562 struct btrfs_map_token token;
4564 btrfs_init_map_token(&token);
4566 leaf = path->nodes[0];
4567 slot = path->slots[0];
4569 old_size = btrfs_item_size_nr(leaf, slot);
4570 if (old_size == new_size)
4573 nritems = btrfs_header_nritems(leaf);
4574 data_end = leaf_data_end(fs_info, leaf);
4576 old_data_start = btrfs_item_offset_nr(leaf, slot);
4578 size_diff = old_size - new_size;
4581 BUG_ON(slot >= nritems);
4584 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4586 /* first correct the data pointers */
4587 for (i = slot; i < nritems; i++) {
4589 item = btrfs_item_nr(i);
4591 ioff = btrfs_token_item_offset(leaf, item, &token);
4592 btrfs_set_token_item_offset(leaf, item,
4593 ioff + size_diff, &token);
4596 /* shift the data */
4598 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4599 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
4600 data_end, old_data_start + new_size - data_end);
4602 struct btrfs_disk_key disk_key;
4605 btrfs_item_key(leaf, &disk_key, slot);
4607 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4609 struct btrfs_file_extent_item *fi;
4611 fi = btrfs_item_ptr(leaf, slot,
4612 struct btrfs_file_extent_item);
4613 fi = (struct btrfs_file_extent_item *)(
4614 (unsigned long)fi - size_diff);
4616 if (btrfs_file_extent_type(leaf, fi) ==
4617 BTRFS_FILE_EXTENT_INLINE) {
4618 ptr = btrfs_item_ptr_offset(leaf, slot);
4619 memmove_extent_buffer(leaf, ptr,
4621 BTRFS_FILE_EXTENT_INLINE_DATA_START);
4625 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4626 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
4627 data_end, old_data_start - data_end);
4629 offset = btrfs_disk_key_offset(&disk_key);
4630 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4631 btrfs_set_item_key(leaf, &disk_key, slot);
4633 fixup_low_keys(path, &disk_key, 1);
4636 item = btrfs_item_nr(slot);
4637 btrfs_set_item_size(leaf, item, new_size);
4638 btrfs_mark_buffer_dirty(leaf);
4640 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
4641 btrfs_print_leaf(leaf);
4647 * make the item pointed to by the path bigger, data_size is the added size.
4649 void btrfs_extend_item(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
4653 struct extent_buffer *leaf;
4654 struct btrfs_item *item;
4656 unsigned int data_end;
4657 unsigned int old_data;
4658 unsigned int old_size;
4660 struct btrfs_map_token token;
4662 btrfs_init_map_token(&token);
4664 leaf = path->nodes[0];
4666 nritems = btrfs_header_nritems(leaf);
4667 data_end = leaf_data_end(fs_info, leaf);
4669 if (btrfs_leaf_free_space(fs_info, leaf) < data_size) {
4670 btrfs_print_leaf(leaf);
4673 slot = path->slots[0];
4674 old_data = btrfs_item_end_nr(leaf, slot);
4677 if (slot >= nritems) {
4678 btrfs_print_leaf(leaf);
4679 btrfs_crit(fs_info, "slot %d too large, nritems %d",
4685 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4687 /* first correct the data pointers */
4688 for (i = slot; i < nritems; i++) {
4690 item = btrfs_item_nr(i);
4692 ioff = btrfs_token_item_offset(leaf, item, &token);
4693 btrfs_set_token_item_offset(leaf, item,
4694 ioff - data_size, &token);
4697 /* shift the data */
4698 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4699 data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
4700 data_end, old_data - data_end);
4702 data_end = old_data;
4703 old_size = btrfs_item_size_nr(leaf, slot);
4704 item = btrfs_item_nr(slot);
4705 btrfs_set_item_size(leaf, item, old_size + data_size);
4706 btrfs_mark_buffer_dirty(leaf);
4708 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
4709 btrfs_print_leaf(leaf);
4715 * this is a helper for btrfs_insert_empty_items, the main goal here is
4716 * to save stack depth by doing the bulk of the work in a function
4717 * that doesn't call btrfs_search_slot
4719 void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
4720 const struct btrfs_key *cpu_key, u32 *data_size,
4721 u32 total_data, u32 total_size, int nr)
4723 struct btrfs_fs_info *fs_info = root->fs_info;
4724 struct btrfs_item *item;
4727 unsigned int data_end;
4728 struct btrfs_disk_key disk_key;
4729 struct extent_buffer *leaf;
4731 struct btrfs_map_token token;
4733 if (path->slots[0] == 0) {
4734 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
4735 fixup_low_keys(path, &disk_key, 1);
4737 btrfs_unlock_up_safe(path, 1);
4739 btrfs_init_map_token(&token);
4741 leaf = path->nodes[0];
4742 slot = path->slots[0];
4744 nritems = btrfs_header_nritems(leaf);
4745 data_end = leaf_data_end(fs_info, leaf);
4747 if (btrfs_leaf_free_space(fs_info, leaf) < total_size) {
4748 btrfs_print_leaf(leaf);
4749 btrfs_crit(fs_info, "not enough freespace need %u have %d",
4750 total_size, btrfs_leaf_free_space(fs_info, leaf));
4754 if (slot != nritems) {
4755 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4757 if (old_data < data_end) {
4758 btrfs_print_leaf(leaf);
4759 btrfs_crit(fs_info, "slot %d old_data %d data_end %d",
4760 slot, old_data, data_end);
4764 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4766 /* first correct the data pointers */
4767 for (i = slot; i < nritems; i++) {
4770 item = btrfs_item_nr(i);
4771 ioff = btrfs_token_item_offset(leaf, item, &token);
4772 btrfs_set_token_item_offset(leaf, item,
4773 ioff - total_data, &token);
4775 /* shift the items */
4776 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4777 btrfs_item_nr_offset(slot),
4778 (nritems - slot) * sizeof(struct btrfs_item));
4780 /* shift the data */
4781 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4782 data_end - total_data, BTRFS_LEAF_DATA_OFFSET +
4783 data_end, old_data - data_end);
4784 data_end = old_data;
4787 /* setup the item for the new data */
4788 for (i = 0; i < nr; i++) {
4789 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4790 btrfs_set_item_key(leaf, &disk_key, slot + i);
4791 item = btrfs_item_nr(slot + i);
4792 btrfs_set_token_item_offset(leaf, item,
4793 data_end - data_size[i], &token);
4794 data_end -= data_size[i];
4795 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
4798 btrfs_set_header_nritems(leaf, nritems + nr);
4799 btrfs_mark_buffer_dirty(leaf);
4801 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
4802 btrfs_print_leaf(leaf);
4808 * Given a key and some data, insert items into the tree.
4809 * This does all the path init required, making room in the tree if needed.
4811 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4812 struct btrfs_root *root,
4813 struct btrfs_path *path,
4814 const struct btrfs_key *cpu_key, u32 *data_size,
4823 for (i = 0; i < nr; i++)
4824 total_data += data_size[i];
4826 total_size = total_data + (nr * sizeof(struct btrfs_item));
4827 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4833 slot = path->slots[0];
4836 setup_items_for_insert(root, path, cpu_key, data_size,
4837 total_data, total_size, nr);
4842 * Given a key and some data, insert an item into the tree.
4843 * This does all the path init required, making room in the tree if needed.
4845 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4846 const struct btrfs_key *cpu_key, void *data,
4850 struct btrfs_path *path;
4851 struct extent_buffer *leaf;
4854 path = btrfs_alloc_path();
4857 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
4859 leaf = path->nodes[0];
4860 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4861 write_extent_buffer(leaf, data, ptr, data_size);
4862 btrfs_mark_buffer_dirty(leaf);
4864 btrfs_free_path(path);
4869 * delete the pointer from a given node.
4871 * the tree should have been previously balanced so the deletion does not
4874 static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4875 int level, int slot)
4877 struct extent_buffer *parent = path->nodes[level];
4881 nritems = btrfs_header_nritems(parent);
4882 if (slot != nritems - 1) {
4884 ret = tree_mod_log_insert_move(parent, slot, slot + 1,
4885 nritems - slot - 1);
4888 memmove_extent_buffer(parent,
4889 btrfs_node_key_ptr_offset(slot),
4890 btrfs_node_key_ptr_offset(slot + 1),
4891 sizeof(struct btrfs_key_ptr) *
4892 (nritems - slot - 1));
4894 ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE,
4900 btrfs_set_header_nritems(parent, nritems);
4901 if (nritems == 0 && parent == root->node) {
4902 BUG_ON(btrfs_header_level(root->node) != 1);
4903 /* just turn the root into a leaf and break */
4904 btrfs_set_header_level(root->node, 0);
4905 } else if (slot == 0) {
4906 struct btrfs_disk_key disk_key;
4908 btrfs_node_key(parent, &disk_key, 0);
4909 fixup_low_keys(path, &disk_key, level + 1);
4911 btrfs_mark_buffer_dirty(parent);
4915 * a helper function to delete the leaf pointed to by path->slots[1] and
4918 * This deletes the pointer in path->nodes[1] and frees the leaf
4919 * block extent. zero is returned if it all worked out, < 0 otherwise.
4921 * The path must have already been setup for deleting the leaf, including
4922 * all the proper balancing. path->nodes[1] must be locked.
4924 static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4925 struct btrfs_root *root,
4926 struct btrfs_path *path,
4927 struct extent_buffer *leaf)
4929 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
4930 del_ptr(root, path, 1, path->slots[1]);
4933 * btrfs_free_extent is expensive, we want to make sure we
4934 * aren't holding any locks when we call it
4936 btrfs_unlock_up_safe(path, 0);
4938 root_sub_used(root, leaf->len);
4940 extent_buffer_get(leaf);
4941 btrfs_free_tree_block(trans, root, leaf, 0, 1);
4942 free_extent_buffer_stale(leaf);
4945 * delete the item at the leaf level in path. If that empties
4946 * the leaf, remove it from the tree
4948 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4949 struct btrfs_path *path, int slot, int nr)
4951 struct btrfs_fs_info *fs_info = root->fs_info;
4952 struct extent_buffer *leaf;
4953 struct btrfs_item *item;
4960 struct btrfs_map_token token;
4962 btrfs_init_map_token(&token);
4964 leaf = path->nodes[0];
4965 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4967 for (i = 0; i < nr; i++)
4968 dsize += btrfs_item_size_nr(leaf, slot + i);
4970 nritems = btrfs_header_nritems(leaf);
4972 if (slot + nr != nritems) {
4973 int data_end = leaf_data_end(fs_info, leaf);
4975 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4977 BTRFS_LEAF_DATA_OFFSET + data_end,
4978 last_off - data_end);
4980 for (i = slot + nr; i < nritems; i++) {
4983 item = btrfs_item_nr(i);
4984 ioff = btrfs_token_item_offset(leaf, item, &token);
4985 btrfs_set_token_item_offset(leaf, item,
4986 ioff + dsize, &token);
4989 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
4990 btrfs_item_nr_offset(slot + nr),
4991 sizeof(struct btrfs_item) *
4992 (nritems - slot - nr));
4994 btrfs_set_header_nritems(leaf, nritems - nr);
4997 /* delete the leaf if we've emptied it */
4999 if (leaf == root->node) {
5000 btrfs_set_header_level(leaf, 0);
5002 btrfs_set_path_blocking(path);
5003 clean_tree_block(fs_info, leaf);
5004 btrfs_del_leaf(trans, root, path, leaf);
5007 int used = leaf_space_used(leaf, 0, nritems);
5009 struct btrfs_disk_key disk_key;
5011 btrfs_item_key(leaf, &disk_key, 0);
5012 fixup_low_keys(path, &disk_key, 1);
5015 /* delete the leaf if it is mostly empty */
5016 if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
5017 /* push_leaf_left fixes the path.
5018 * make sure the path still points to our leaf
5019 * for possible call to del_ptr below
5021 slot = path->slots[1];
5022 extent_buffer_get(leaf);
5024 btrfs_set_path_blocking(path);
5025 wret = push_leaf_left(trans, root, path, 1, 1,
5027 if (wret < 0 && wret != -ENOSPC)
5030 if (path->nodes[0] == leaf &&
5031 btrfs_header_nritems(leaf)) {
5032 wret = push_leaf_right(trans, root, path, 1,
5034 if (wret < 0 && wret != -ENOSPC)
5038 if (btrfs_header_nritems(leaf) == 0) {
5039 path->slots[1] = slot;
5040 btrfs_del_leaf(trans, root, path, leaf);
5041 free_extent_buffer(leaf);
5044 /* if we're still in the path, make sure
5045 * we're dirty. Otherwise, one of the
5046 * push_leaf functions must have already
5047 * dirtied this buffer
5049 if (path->nodes[0] == leaf)
5050 btrfs_mark_buffer_dirty(leaf);
5051 free_extent_buffer(leaf);
5054 btrfs_mark_buffer_dirty(leaf);
5061 * search the tree again to find a leaf with lesser keys
5062 * returns 0 if it found something or 1 if there are no lesser leaves.
5063 * returns < 0 on io errors.
5065 * This may release the path, and so you may lose any locks held at the
5068 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
5070 struct btrfs_key key;
5071 struct btrfs_disk_key found_key;
5074 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
5076 if (key.offset > 0) {
5078 } else if (key.type > 0) {
5080 key.offset = (u64)-1;
5081 } else if (key.objectid > 0) {
5084 key.offset = (u64)-1;
5089 btrfs_release_path(path);
5090 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5093 btrfs_item_key(path->nodes[0], &found_key, 0);
5094 ret = comp_keys(&found_key, &key);
5096 * We might have had an item with the previous key in the tree right
5097 * before we released our path. And after we released our path, that
5098 * item might have been pushed to the first slot (0) of the leaf we
5099 * were holding due to a tree balance. Alternatively, an item with the
5100 * previous key can exist as the only element of a leaf (big fat item).
5101 * Therefore account for these 2 cases, so that our callers (like
5102 * btrfs_previous_item) don't miss an existing item with a key matching
5103 * the previous key we computed above.
5111 * A helper function to walk down the tree starting at min_key, and looking
5112 * for nodes or leaves that are have a minimum transaction id.
5113 * This is used by the btree defrag code, and tree logging
5115 * This does not cow, but it does stuff the starting key it finds back
5116 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5117 * key and get a writable path.
5119 * This honors path->lowest_level to prevent descent past a given level
5122 * min_trans indicates the oldest transaction that you are interested
5123 * in walking through. Any nodes or leaves older than min_trans are
5124 * skipped over (without reading them).
5126 * returns zero if something useful was found, < 0 on error and 1 if there
5127 * was nothing in the tree that matched the search criteria.
5129 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
5130 struct btrfs_path *path,
5133 struct btrfs_fs_info *fs_info = root->fs_info;
5134 struct extent_buffer *cur;
5135 struct btrfs_key found_key;
5141 int keep_locks = path->keep_locks;
5143 path->keep_locks = 1;
5145 cur = btrfs_read_lock_root_node(root);
5146 level = btrfs_header_level(cur);
5147 WARN_ON(path->nodes[level]);
5148 path->nodes[level] = cur;
5149 path->locks[level] = BTRFS_READ_LOCK;
5151 if (btrfs_header_generation(cur) < min_trans) {
5156 nritems = btrfs_header_nritems(cur);
5157 level = btrfs_header_level(cur);
5158 sret = btrfs_bin_search(cur, min_key, level, &slot);
5160 /* at the lowest level, we're done, setup the path and exit */
5161 if (level == path->lowest_level) {
5162 if (slot >= nritems)
5165 path->slots[level] = slot;
5166 btrfs_item_key_to_cpu(cur, &found_key, slot);
5169 if (sret && slot > 0)
5172 * check this node pointer against the min_trans parameters.
5173 * If it is too old, old, skip to the next one.
5175 while (slot < nritems) {
5178 gen = btrfs_node_ptr_generation(cur, slot);
5179 if (gen < min_trans) {
5187 * we didn't find a candidate key in this node, walk forward
5188 * and find another one
5190 if (slot >= nritems) {
5191 path->slots[level] = slot;
5192 btrfs_set_path_blocking(path);
5193 sret = btrfs_find_next_key(root, path, min_key, level,
5196 btrfs_release_path(path);
5202 /* save our key for returning back */
5203 btrfs_node_key_to_cpu(cur, &found_key, slot);
5204 path->slots[level] = slot;
5205 if (level == path->lowest_level) {
5209 btrfs_set_path_blocking(path);
5210 cur = read_node_slot(fs_info, cur, slot);
5216 btrfs_tree_read_lock(cur);
5218 path->locks[level - 1] = BTRFS_READ_LOCK;
5219 path->nodes[level - 1] = cur;
5220 unlock_up(path, level, 1, 0, NULL);
5223 path->keep_locks = keep_locks;
5225 btrfs_unlock_up_safe(path, path->lowest_level + 1);
5226 btrfs_set_path_blocking(path);
5227 memcpy(min_key, &found_key, sizeof(found_key));
5232 static int tree_move_down(struct btrfs_fs_info *fs_info,
5233 struct btrfs_path *path,
5236 struct extent_buffer *eb;
5238 BUG_ON(*level == 0);
5239 eb = read_node_slot(fs_info, path->nodes[*level], path->slots[*level]);
5243 path->nodes[*level - 1] = eb;
5244 path->slots[*level - 1] = 0;
5249 static int tree_move_next_or_upnext(struct btrfs_path *path,
5250 int *level, int root_level)
5254 nritems = btrfs_header_nritems(path->nodes[*level]);
5256 path->slots[*level]++;
5258 while (path->slots[*level] >= nritems) {
5259 if (*level == root_level)
5263 path->slots[*level] = 0;
5264 free_extent_buffer(path->nodes[*level]);
5265 path->nodes[*level] = NULL;
5267 path->slots[*level]++;
5269 nritems = btrfs_header_nritems(path->nodes[*level]);
5276 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5279 static int tree_advance(struct btrfs_fs_info *fs_info,
5280 struct btrfs_path *path,
5281 int *level, int root_level,
5283 struct btrfs_key *key)
5287 if (*level == 0 || !allow_down) {
5288 ret = tree_move_next_or_upnext(path, level, root_level);
5290 ret = tree_move_down(fs_info, path, level);
5294 btrfs_item_key_to_cpu(path->nodes[*level], key,
5295 path->slots[*level]);
5297 btrfs_node_key_to_cpu(path->nodes[*level], key,
5298 path->slots[*level]);
5303 static int tree_compare_item(struct btrfs_path *left_path,
5304 struct btrfs_path *right_path,
5309 unsigned long off1, off2;
5311 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5312 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5316 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5317 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5318 right_path->slots[0]);
5320 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5322 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5329 #define ADVANCE_ONLY_NEXT -1
5332 * This function compares two trees and calls the provided callback for
5333 * every changed/new/deleted item it finds.
5334 * If shared tree blocks are encountered, whole subtrees are skipped, making
5335 * the compare pretty fast on snapshotted subvolumes.
5337 * This currently works on commit roots only. As commit roots are read only,
5338 * we don't do any locking. The commit roots are protected with transactions.
5339 * Transactions are ended and rejoined when a commit is tried in between.
5341 * This function checks for modifications done to the trees while comparing.
5342 * If it detects a change, it aborts immediately.
5344 int btrfs_compare_trees(struct btrfs_root *left_root,
5345 struct btrfs_root *right_root,
5346 btrfs_changed_cb_t changed_cb, void *ctx)
5348 struct btrfs_fs_info *fs_info = left_root->fs_info;
5351 struct btrfs_path *left_path = NULL;
5352 struct btrfs_path *right_path = NULL;
5353 struct btrfs_key left_key;
5354 struct btrfs_key right_key;
5355 char *tmp_buf = NULL;
5356 int left_root_level;
5357 int right_root_level;
5360 int left_end_reached;
5361 int right_end_reached;
5369 left_path = btrfs_alloc_path();
5374 right_path = btrfs_alloc_path();
5380 tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
5386 left_path->search_commit_root = 1;
5387 left_path->skip_locking = 1;
5388 right_path->search_commit_root = 1;
5389 right_path->skip_locking = 1;
5392 * Strategy: Go to the first items of both trees. Then do
5394 * If both trees are at level 0
5395 * Compare keys of current items
5396 * If left < right treat left item as new, advance left tree
5398 * If left > right treat right item as deleted, advance right tree
5400 * If left == right do deep compare of items, treat as changed if
5401 * needed, advance both trees and repeat
5402 * If both trees are at the same level but not at level 0
5403 * Compare keys of current nodes/leafs
5404 * If left < right advance left tree and repeat
5405 * If left > right advance right tree and repeat
5406 * If left == right compare blockptrs of the next nodes/leafs
5407 * If they match advance both trees but stay at the same level
5409 * If they don't match advance both trees while allowing to go
5411 * If tree levels are different
5412 * Advance the tree that needs it and repeat
5414 * Advancing a tree means:
5415 * If we are at level 0, try to go to the next slot. If that's not
5416 * possible, go one level up and repeat. Stop when we found a level
5417 * where we could go to the next slot. We may at this point be on a
5420 * If we are not at level 0 and not on shared tree blocks, go one
5423 * If we are not at level 0 and on shared tree blocks, go one slot to
5424 * the right if possible or go up and right.
5427 down_read(&fs_info->commit_root_sem);
5428 left_level = btrfs_header_level(left_root->commit_root);
5429 left_root_level = left_level;
5430 left_path->nodes[left_level] =
5431 btrfs_clone_extent_buffer(left_root->commit_root);
5432 if (!left_path->nodes[left_level]) {
5433 up_read(&fs_info->commit_root_sem);
5438 right_level = btrfs_header_level(right_root->commit_root);
5439 right_root_level = right_level;
5440 right_path->nodes[right_level] =
5441 btrfs_clone_extent_buffer(right_root->commit_root);
5442 if (!right_path->nodes[right_level]) {
5443 up_read(&fs_info->commit_root_sem);
5447 up_read(&fs_info->commit_root_sem);
5449 if (left_level == 0)
5450 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5451 &left_key, left_path->slots[left_level]);
5453 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5454 &left_key, left_path->slots[left_level]);
5455 if (right_level == 0)
5456 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5457 &right_key, right_path->slots[right_level]);
5459 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5460 &right_key, right_path->slots[right_level]);
5462 left_end_reached = right_end_reached = 0;
5463 advance_left = advance_right = 0;
5466 if (advance_left && !left_end_reached) {
5467 ret = tree_advance(fs_info, left_path, &left_level,
5469 advance_left != ADVANCE_ONLY_NEXT,
5472 left_end_reached = ADVANCE;
5477 if (advance_right && !right_end_reached) {
5478 ret = tree_advance(fs_info, right_path, &right_level,
5480 advance_right != ADVANCE_ONLY_NEXT,
5483 right_end_reached = ADVANCE;
5489 if (left_end_reached && right_end_reached) {
5492 } else if (left_end_reached) {
5493 if (right_level == 0) {
5494 ret = changed_cb(left_path, right_path,
5496 BTRFS_COMPARE_TREE_DELETED,
5501 advance_right = ADVANCE;
5503 } else if (right_end_reached) {
5504 if (left_level == 0) {
5505 ret = changed_cb(left_path, right_path,
5507 BTRFS_COMPARE_TREE_NEW,
5512 advance_left = ADVANCE;
5516 if (left_level == 0 && right_level == 0) {
5517 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5519 ret = changed_cb(left_path, right_path,
5521 BTRFS_COMPARE_TREE_NEW,
5525 advance_left = ADVANCE;
5526 } else if (cmp > 0) {
5527 ret = changed_cb(left_path, right_path,
5529 BTRFS_COMPARE_TREE_DELETED,
5533 advance_right = ADVANCE;
5535 enum btrfs_compare_tree_result result;
5537 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
5538 ret = tree_compare_item(left_path, right_path,
5541 result = BTRFS_COMPARE_TREE_CHANGED;
5543 result = BTRFS_COMPARE_TREE_SAME;
5544 ret = changed_cb(left_path, right_path,
5545 &left_key, result, ctx);
5548 advance_left = ADVANCE;
5549 advance_right = ADVANCE;
5551 } else if (left_level == right_level) {
5552 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5554 advance_left = ADVANCE;
5555 } else if (cmp > 0) {
5556 advance_right = ADVANCE;
5558 left_blockptr = btrfs_node_blockptr(
5559 left_path->nodes[left_level],
5560 left_path->slots[left_level]);
5561 right_blockptr = btrfs_node_blockptr(
5562 right_path->nodes[right_level],
5563 right_path->slots[right_level]);
5564 left_gen = btrfs_node_ptr_generation(
5565 left_path->nodes[left_level],
5566 left_path->slots[left_level]);
5567 right_gen = btrfs_node_ptr_generation(
5568 right_path->nodes[right_level],
5569 right_path->slots[right_level]);
5570 if (left_blockptr == right_blockptr &&
5571 left_gen == right_gen) {
5573 * As we're on a shared block, don't
5574 * allow to go deeper.
5576 advance_left = ADVANCE_ONLY_NEXT;
5577 advance_right = ADVANCE_ONLY_NEXT;
5579 advance_left = ADVANCE;
5580 advance_right = ADVANCE;
5583 } else if (left_level < right_level) {
5584 advance_right = ADVANCE;
5586 advance_left = ADVANCE;
5591 btrfs_free_path(left_path);
5592 btrfs_free_path(right_path);
5598 * this is similar to btrfs_next_leaf, but does not try to preserve
5599 * and fixup the path. It looks for and returns the next key in the
5600 * tree based on the current path and the min_trans parameters.
5602 * 0 is returned if another key is found, < 0 if there are any errors
5603 * and 1 is returned if there are no higher keys in the tree
5605 * path->keep_locks should be set to 1 on the search made before
5606 * calling this function.
5608 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
5609 struct btrfs_key *key, int level, u64 min_trans)
5612 struct extent_buffer *c;
5614 WARN_ON(!path->keep_locks);
5615 while (level < BTRFS_MAX_LEVEL) {
5616 if (!path->nodes[level])
5619 slot = path->slots[level] + 1;
5620 c = path->nodes[level];
5622 if (slot >= btrfs_header_nritems(c)) {
5625 struct btrfs_key cur_key;
5626 if (level + 1 >= BTRFS_MAX_LEVEL ||
5627 !path->nodes[level + 1])
5630 if (path->locks[level + 1]) {
5635 slot = btrfs_header_nritems(c) - 1;
5637 btrfs_item_key_to_cpu(c, &cur_key, slot);
5639 btrfs_node_key_to_cpu(c, &cur_key, slot);
5641 orig_lowest = path->lowest_level;
5642 btrfs_release_path(path);
5643 path->lowest_level = level;
5644 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5646 path->lowest_level = orig_lowest;
5650 c = path->nodes[level];
5651 slot = path->slots[level];
5658 btrfs_item_key_to_cpu(c, key, slot);
5660 u64 gen = btrfs_node_ptr_generation(c, slot);
5662 if (gen < min_trans) {
5666 btrfs_node_key_to_cpu(c, key, slot);
5674 * search the tree again to find a leaf with greater keys
5675 * returns 0 if it found something or 1 if there are no greater leaves.
5676 * returns < 0 on io errors.
5678 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
5680 return btrfs_next_old_leaf(root, path, 0);
5683 int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5688 struct extent_buffer *c;
5689 struct extent_buffer *next;
5690 struct btrfs_key key;
5693 int old_spinning = path->leave_spinning;
5694 int next_rw_lock = 0;
5696 nritems = btrfs_header_nritems(path->nodes[0]);
5700 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5705 btrfs_release_path(path);
5707 path->keep_locks = 1;
5708 path->leave_spinning = 1;
5711 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5713 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5714 path->keep_locks = 0;
5719 nritems = btrfs_header_nritems(path->nodes[0]);
5721 * by releasing the path above we dropped all our locks. A balance
5722 * could have added more items next to the key that used to be
5723 * at the very end of the block. So, check again here and
5724 * advance the path if there are now more items available.
5726 if (nritems > 0 && path->slots[0] < nritems - 1) {
5733 * So the above check misses one case:
5734 * - after releasing the path above, someone has removed the item that
5735 * used to be at the very end of the block, and balance between leafs
5736 * gets another one with bigger key.offset to replace it.
5738 * This one should be returned as well, or we can get leaf corruption
5739 * later(esp. in __btrfs_drop_extents()).
5741 * And a bit more explanation about this check,
5742 * with ret > 0, the key isn't found, the path points to the slot
5743 * where it should be inserted, so the path->slots[0] item must be the
5746 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
5751 while (level < BTRFS_MAX_LEVEL) {
5752 if (!path->nodes[level]) {
5757 slot = path->slots[level] + 1;
5758 c = path->nodes[level];
5759 if (slot >= btrfs_header_nritems(c)) {
5761 if (level == BTRFS_MAX_LEVEL) {
5769 btrfs_tree_unlock_rw(next, next_rw_lock);
5770 free_extent_buffer(next);
5774 next_rw_lock = path->locks[level];
5775 ret = read_block_for_search(root, path, &next, level,
5781 btrfs_release_path(path);
5785 if (!path->skip_locking) {
5786 ret = btrfs_try_tree_read_lock(next);
5787 if (!ret && time_seq) {
5789 * If we don't get the lock, we may be racing
5790 * with push_leaf_left, holding that lock while
5791 * itself waiting for the leaf we've currently
5792 * locked. To solve this situation, we give up
5793 * on our lock and cycle.
5795 free_extent_buffer(next);
5796 btrfs_release_path(path);
5801 btrfs_set_path_blocking(path);
5802 btrfs_tree_read_lock(next);
5804 next_rw_lock = BTRFS_READ_LOCK;
5808 path->slots[level] = slot;
5811 c = path->nodes[level];
5812 if (path->locks[level])
5813 btrfs_tree_unlock_rw(c, path->locks[level]);
5815 free_extent_buffer(c);
5816 path->nodes[level] = next;
5817 path->slots[level] = 0;
5818 if (!path->skip_locking)
5819 path->locks[level] = next_rw_lock;
5823 ret = read_block_for_search(root, path, &next, level,
5829 btrfs_release_path(path);
5833 if (!path->skip_locking) {
5834 ret = btrfs_try_tree_read_lock(next);
5836 btrfs_set_path_blocking(path);
5837 btrfs_tree_read_lock(next);
5839 next_rw_lock = BTRFS_READ_LOCK;
5844 unlock_up(path, 0, 1, 0, NULL);
5845 path->leave_spinning = old_spinning;
5847 btrfs_set_path_blocking(path);
5853 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5854 * searching until it gets past min_objectid or finds an item of 'type'
5856 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5858 int btrfs_previous_item(struct btrfs_root *root,
5859 struct btrfs_path *path, u64 min_objectid,
5862 struct btrfs_key found_key;
5863 struct extent_buffer *leaf;
5868 if (path->slots[0] == 0) {
5869 btrfs_set_path_blocking(path);
5870 ret = btrfs_prev_leaf(root, path);
5876 leaf = path->nodes[0];
5877 nritems = btrfs_header_nritems(leaf);
5880 if (path->slots[0] == nritems)
5883 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5884 if (found_key.objectid < min_objectid)
5886 if (found_key.type == type)
5888 if (found_key.objectid == min_objectid &&
5889 found_key.type < type)
5896 * search in extent tree to find a previous Metadata/Data extent item with
5899 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5901 int btrfs_previous_extent_item(struct btrfs_root *root,
5902 struct btrfs_path *path, u64 min_objectid)
5904 struct btrfs_key found_key;
5905 struct extent_buffer *leaf;
5910 if (path->slots[0] == 0) {
5911 btrfs_set_path_blocking(path);
5912 ret = btrfs_prev_leaf(root, path);
5918 leaf = path->nodes[0];
5919 nritems = btrfs_header_nritems(leaf);
5922 if (path->slots[0] == nritems)
5925 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5926 if (found_key.objectid < min_objectid)
5928 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5929 found_key.type == BTRFS_METADATA_ITEM_KEY)
5931 if (found_key.objectid == min_objectid &&
5932 found_key.type < BTRFS_EXTENT_ITEM_KEY)