1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007,2008 Oracle. All rights reserved.
6 #include <linux/sched.h>
7 #include <linux/slab.h>
8 #include <linux/rbtree.h>
12 #include "transaction.h"
13 #include "print-tree.h"
18 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
19 *root, struct btrfs_path *path, int level);
20 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
21 const struct btrfs_key *ins_key, struct btrfs_path *path,
22 int data_size, int extend);
23 static int push_node_left(struct btrfs_trans_handle *trans,
24 struct extent_buffer *dst,
25 struct extent_buffer *src, int empty);
26 static int balance_node_right(struct btrfs_trans_handle *trans,
27 struct extent_buffer *dst_buf,
28 struct extent_buffer *src_buf);
29 static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
32 static const struct btrfs_csums {
36 [BTRFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" },
39 int btrfs_super_csum_size(const struct btrfs_super_block *s)
41 u16 t = btrfs_super_csum_type(s);
43 * csum type is validated at mount time
45 return btrfs_csums[t].size;
48 const char *btrfs_super_csum_name(u16 csum_type)
50 /* csum type is validated at mount time */
51 return btrfs_csums[csum_type].name;
54 struct btrfs_path *btrfs_alloc_path(void)
56 return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
59 /* this also releases the path */
60 void btrfs_free_path(struct btrfs_path *p)
64 btrfs_release_path(p);
65 kmem_cache_free(btrfs_path_cachep, p);
69 * path release drops references on the extent buffers in the path
70 * and it drops any locks held by this path
72 * It is safe to call this on paths that no locks or extent buffers held.
74 noinline void btrfs_release_path(struct btrfs_path *p)
78 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
83 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
86 free_extent_buffer(p->nodes[i]);
92 * safely gets a reference on the root node of a tree. A lock
93 * is not taken, so a concurrent writer may put a different node
94 * at the root of the tree. See btrfs_lock_root_node for the
97 * The extent buffer returned by this has a reference taken, so
98 * it won't disappear. It may stop being the root of the tree
99 * at any time because there are no locks held.
101 struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
103 struct extent_buffer *eb;
107 eb = rcu_dereference(root->node);
110 * RCU really hurts here, we could free up the root node because
111 * it was COWed but we may not get the new root node yet so do
112 * the inc_not_zero dance and if it doesn't work then
113 * synchronize_rcu and try again.
115 if (atomic_inc_not_zero(&eb->refs)) {
125 /* loop around taking references on and locking the root node of the
126 * tree until you end up with a lock on the root. A locked buffer
127 * is returned, with a reference held.
129 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
131 struct extent_buffer *eb;
134 eb = btrfs_root_node(root);
136 if (eb == root->node)
138 btrfs_tree_unlock(eb);
139 free_extent_buffer(eb);
144 /* loop around taking references on and locking the root node of the
145 * tree until you end up with a lock on the root. A locked buffer
146 * is returned, with a reference held.
148 struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
150 struct extent_buffer *eb;
153 eb = btrfs_root_node(root);
154 btrfs_tree_read_lock(eb);
155 if (eb == root->node)
157 btrfs_tree_read_unlock(eb);
158 free_extent_buffer(eb);
163 /* cowonly root (everything not a reference counted cow subvolume), just get
164 * put onto a simple dirty list. transaction.c walks this to make sure they
165 * get properly updated on disk.
167 static void add_root_to_dirty_list(struct btrfs_root *root)
169 struct btrfs_fs_info *fs_info = root->fs_info;
171 if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
172 !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
175 spin_lock(&fs_info->trans_lock);
176 if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
177 /* Want the extent tree to be the last on the list */
178 if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID)
179 list_move_tail(&root->dirty_list,
180 &fs_info->dirty_cowonly_roots);
182 list_move(&root->dirty_list,
183 &fs_info->dirty_cowonly_roots);
185 spin_unlock(&fs_info->trans_lock);
189 * used by snapshot creation to make a copy of a root for a tree with
190 * a given objectid. The buffer with the new root node is returned in
191 * cow_ret, and this func returns zero on success or a negative error code.
193 int btrfs_copy_root(struct btrfs_trans_handle *trans,
194 struct btrfs_root *root,
195 struct extent_buffer *buf,
196 struct extent_buffer **cow_ret, u64 new_root_objectid)
198 struct btrfs_fs_info *fs_info = root->fs_info;
199 struct extent_buffer *cow;
202 struct btrfs_disk_key disk_key;
204 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
205 trans->transid != fs_info->running_transaction->transid);
206 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
207 trans->transid != root->last_trans);
209 level = btrfs_header_level(buf);
211 btrfs_item_key(buf, &disk_key, 0);
213 btrfs_node_key(buf, &disk_key, 0);
215 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
216 &disk_key, level, buf->start, 0);
220 copy_extent_buffer_full(cow, buf);
221 btrfs_set_header_bytenr(cow, cow->start);
222 btrfs_set_header_generation(cow, trans->transid);
223 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
224 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
225 BTRFS_HEADER_FLAG_RELOC);
226 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
227 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
229 btrfs_set_header_owner(cow, new_root_objectid);
231 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
233 WARN_ON(btrfs_header_generation(buf) > trans->transid);
234 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
235 ret = btrfs_inc_ref(trans, root, cow, 1);
237 ret = btrfs_inc_ref(trans, root, cow, 0);
242 btrfs_mark_buffer_dirty(cow);
251 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
252 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
254 MOD_LOG_ROOT_REPLACE,
257 struct tree_mod_root {
262 struct tree_mod_elem {
268 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
271 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
274 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
275 struct btrfs_disk_key key;
278 /* this is used for op == MOD_LOG_MOVE_KEYS */
284 /* this is used for op == MOD_LOG_ROOT_REPLACE */
285 struct tree_mod_root old_root;
289 * Pull a new tree mod seq number for our operation.
291 static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
293 return atomic64_inc_return(&fs_info->tree_mod_seq);
297 * This adds a new blocker to the tree mod log's blocker list if the @elem
298 * passed does not already have a sequence number set. So when a caller expects
299 * to record tree modifications, it should ensure to set elem->seq to zero
300 * before calling btrfs_get_tree_mod_seq.
301 * Returns a fresh, unused tree log modification sequence number, even if no new
304 u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
305 struct seq_list *elem)
307 write_lock(&fs_info->tree_mod_log_lock);
308 spin_lock(&fs_info->tree_mod_seq_lock);
310 elem->seq = btrfs_inc_tree_mod_seq(fs_info);
311 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
313 spin_unlock(&fs_info->tree_mod_seq_lock);
314 write_unlock(&fs_info->tree_mod_log_lock);
319 void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
320 struct seq_list *elem)
322 struct rb_root *tm_root;
323 struct rb_node *node;
324 struct rb_node *next;
325 struct seq_list *cur_elem;
326 struct tree_mod_elem *tm;
327 u64 min_seq = (u64)-1;
328 u64 seq_putting = elem->seq;
333 spin_lock(&fs_info->tree_mod_seq_lock);
334 list_del(&elem->list);
337 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
338 if (cur_elem->seq < min_seq) {
339 if (seq_putting > cur_elem->seq) {
341 * blocker with lower sequence number exists, we
342 * cannot remove anything from the log
344 spin_unlock(&fs_info->tree_mod_seq_lock);
347 min_seq = cur_elem->seq;
350 spin_unlock(&fs_info->tree_mod_seq_lock);
353 * anything that's lower than the lowest existing (read: blocked)
354 * sequence number can be removed from the tree.
356 write_lock(&fs_info->tree_mod_log_lock);
357 tm_root = &fs_info->tree_mod_log;
358 for (node = rb_first(tm_root); node; node = next) {
359 next = rb_next(node);
360 tm = rb_entry(node, struct tree_mod_elem, node);
361 if (tm->seq > min_seq)
363 rb_erase(node, tm_root);
366 write_unlock(&fs_info->tree_mod_log_lock);
370 * key order of the log:
371 * node/leaf start address -> sequence
373 * The 'start address' is the logical address of the *new* root node
374 * for root replace operations, or the logical address of the affected
375 * block for all other operations.
378 __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
380 struct rb_root *tm_root;
381 struct rb_node **new;
382 struct rb_node *parent = NULL;
383 struct tree_mod_elem *cur;
385 lockdep_assert_held_write(&fs_info->tree_mod_log_lock);
387 tm->seq = btrfs_inc_tree_mod_seq(fs_info);
389 tm_root = &fs_info->tree_mod_log;
390 new = &tm_root->rb_node;
392 cur = rb_entry(*new, struct tree_mod_elem, node);
394 if (cur->logical < tm->logical)
395 new = &((*new)->rb_left);
396 else if (cur->logical > tm->logical)
397 new = &((*new)->rb_right);
398 else if (cur->seq < tm->seq)
399 new = &((*new)->rb_left);
400 else if (cur->seq > tm->seq)
401 new = &((*new)->rb_right);
406 rb_link_node(&tm->node, parent, new);
407 rb_insert_color(&tm->node, tm_root);
412 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
413 * returns zero with the tree_mod_log_lock acquired. The caller must hold
414 * this until all tree mod log insertions are recorded in the rb tree and then
415 * write unlock fs_info::tree_mod_log_lock.
417 static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
418 struct extent_buffer *eb) {
420 if (list_empty(&(fs_info)->tree_mod_seq_list))
422 if (eb && btrfs_header_level(eb) == 0)
425 write_lock(&fs_info->tree_mod_log_lock);
426 if (list_empty(&(fs_info)->tree_mod_seq_list)) {
427 write_unlock(&fs_info->tree_mod_log_lock);
434 /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
435 static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
436 struct extent_buffer *eb)
439 if (list_empty(&(fs_info)->tree_mod_seq_list))
441 if (eb && btrfs_header_level(eb) == 0)
447 static struct tree_mod_elem *
448 alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
449 enum mod_log_op op, gfp_t flags)
451 struct tree_mod_elem *tm;
453 tm = kzalloc(sizeof(*tm), flags);
457 tm->logical = eb->start;
458 if (op != MOD_LOG_KEY_ADD) {
459 btrfs_node_key(eb, &tm->key, slot);
460 tm->blockptr = btrfs_node_blockptr(eb, slot);
464 tm->generation = btrfs_node_ptr_generation(eb, slot);
465 RB_CLEAR_NODE(&tm->node);
470 static noinline int tree_mod_log_insert_key(struct extent_buffer *eb, int slot,
471 enum mod_log_op op, gfp_t flags)
473 struct tree_mod_elem *tm;
476 if (!tree_mod_need_log(eb->fs_info, eb))
479 tm = alloc_tree_mod_elem(eb, slot, op, flags);
483 if (tree_mod_dont_log(eb->fs_info, eb)) {
488 ret = __tree_mod_log_insert(eb->fs_info, tm);
489 write_unlock(&eb->fs_info->tree_mod_log_lock);
496 static noinline int tree_mod_log_insert_move(struct extent_buffer *eb,
497 int dst_slot, int src_slot, int nr_items)
499 struct tree_mod_elem *tm = NULL;
500 struct tree_mod_elem **tm_list = NULL;
505 if (!tree_mod_need_log(eb->fs_info, eb))
508 tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS);
512 tm = kzalloc(sizeof(*tm), GFP_NOFS);
518 tm->logical = eb->start;
520 tm->move.dst_slot = dst_slot;
521 tm->move.nr_items = nr_items;
522 tm->op = MOD_LOG_MOVE_KEYS;
524 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
525 tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
526 MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
533 if (tree_mod_dont_log(eb->fs_info, eb))
538 * When we override something during the move, we log these removals.
539 * This can only happen when we move towards the beginning of the
540 * buffer, i.e. dst_slot < src_slot.
542 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
543 ret = __tree_mod_log_insert(eb->fs_info, tm_list[i]);
548 ret = __tree_mod_log_insert(eb->fs_info, tm);
551 write_unlock(&eb->fs_info->tree_mod_log_lock);
556 for (i = 0; i < nr_items; i++) {
557 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
558 rb_erase(&tm_list[i]->node, &eb->fs_info->tree_mod_log);
562 write_unlock(&eb->fs_info->tree_mod_log_lock);
570 __tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
571 struct tree_mod_elem **tm_list,
577 for (i = nritems - 1; i >= 0; i--) {
578 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
580 for (j = nritems - 1; j > i; j--)
581 rb_erase(&tm_list[j]->node,
582 &fs_info->tree_mod_log);
590 static noinline int tree_mod_log_insert_root(struct extent_buffer *old_root,
591 struct extent_buffer *new_root, int log_removal)
593 struct btrfs_fs_info *fs_info = old_root->fs_info;
594 struct tree_mod_elem *tm = NULL;
595 struct tree_mod_elem **tm_list = NULL;
600 if (!tree_mod_need_log(fs_info, NULL))
603 if (log_removal && btrfs_header_level(old_root) > 0) {
604 nritems = btrfs_header_nritems(old_root);
605 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
611 for (i = 0; i < nritems; i++) {
612 tm_list[i] = alloc_tree_mod_elem(old_root, i,
613 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
621 tm = kzalloc(sizeof(*tm), GFP_NOFS);
627 tm->logical = new_root->start;
628 tm->old_root.logical = old_root->start;
629 tm->old_root.level = btrfs_header_level(old_root);
630 tm->generation = btrfs_header_generation(old_root);
631 tm->op = MOD_LOG_ROOT_REPLACE;
633 if (tree_mod_dont_log(fs_info, NULL))
637 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
639 ret = __tree_mod_log_insert(fs_info, tm);
641 write_unlock(&fs_info->tree_mod_log_lock);
650 for (i = 0; i < nritems; i++)
659 static struct tree_mod_elem *
660 __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
663 struct rb_root *tm_root;
664 struct rb_node *node;
665 struct tree_mod_elem *cur = NULL;
666 struct tree_mod_elem *found = NULL;
668 read_lock(&fs_info->tree_mod_log_lock);
669 tm_root = &fs_info->tree_mod_log;
670 node = tm_root->rb_node;
672 cur = rb_entry(node, struct tree_mod_elem, node);
673 if (cur->logical < start) {
674 node = node->rb_left;
675 } else if (cur->logical > start) {
676 node = node->rb_right;
677 } else if (cur->seq < min_seq) {
678 node = node->rb_left;
679 } else if (!smallest) {
680 /* we want the node with the highest seq */
682 BUG_ON(found->seq > cur->seq);
684 node = node->rb_left;
685 } else if (cur->seq > min_seq) {
686 /* we want the node with the smallest seq */
688 BUG_ON(found->seq < cur->seq);
690 node = node->rb_right;
696 read_unlock(&fs_info->tree_mod_log_lock);
702 * this returns the element from the log with the smallest time sequence
703 * value that's in the log (the oldest log item). any element with a time
704 * sequence lower than min_seq will be ignored.
706 static struct tree_mod_elem *
707 tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
710 return __tree_mod_log_search(fs_info, start, min_seq, 1);
714 * this returns the element from the log with the largest time sequence
715 * value that's in the log (the most recent log item). any element with
716 * a time sequence lower than min_seq will be ignored.
718 static struct tree_mod_elem *
719 tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
721 return __tree_mod_log_search(fs_info, start, min_seq, 0);
724 static noinline int tree_mod_log_eb_copy(struct extent_buffer *dst,
725 struct extent_buffer *src, unsigned long dst_offset,
726 unsigned long src_offset, int nr_items)
728 struct btrfs_fs_info *fs_info = dst->fs_info;
730 struct tree_mod_elem **tm_list = NULL;
731 struct tree_mod_elem **tm_list_add, **tm_list_rem;
735 if (!tree_mod_need_log(fs_info, NULL))
738 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
741 tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
746 tm_list_add = tm_list;
747 tm_list_rem = tm_list + nr_items;
748 for (i = 0; i < nr_items; i++) {
749 tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
750 MOD_LOG_KEY_REMOVE, GFP_NOFS);
751 if (!tm_list_rem[i]) {
756 tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
757 MOD_LOG_KEY_ADD, GFP_NOFS);
758 if (!tm_list_add[i]) {
764 if (tree_mod_dont_log(fs_info, NULL))
768 for (i = 0; i < nr_items; i++) {
769 ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
772 ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
777 write_unlock(&fs_info->tree_mod_log_lock);
783 for (i = 0; i < nr_items * 2; i++) {
784 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
785 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
789 write_unlock(&fs_info->tree_mod_log_lock);
795 static noinline int tree_mod_log_free_eb(struct extent_buffer *eb)
797 struct tree_mod_elem **tm_list = NULL;
802 if (btrfs_header_level(eb) == 0)
805 if (!tree_mod_need_log(eb->fs_info, NULL))
808 nritems = btrfs_header_nritems(eb);
809 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
813 for (i = 0; i < nritems; i++) {
814 tm_list[i] = alloc_tree_mod_elem(eb, i,
815 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
822 if (tree_mod_dont_log(eb->fs_info, eb))
825 ret = __tree_mod_log_free_eb(eb->fs_info, tm_list, nritems);
826 write_unlock(&eb->fs_info->tree_mod_log_lock);
834 for (i = 0; i < nritems; i++)
842 * check if the tree block can be shared by multiple trees
844 int btrfs_block_can_be_shared(struct btrfs_root *root,
845 struct extent_buffer *buf)
848 * Tree blocks not in reference counted trees and tree roots
849 * are never shared. If a block was allocated after the last
850 * snapshot and the block was not allocated by tree relocation,
851 * we know the block is not shared.
853 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
854 buf != root->node && buf != root->commit_root &&
855 (btrfs_header_generation(buf) <=
856 btrfs_root_last_snapshot(&root->root_item) ||
857 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
863 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
864 struct btrfs_root *root,
865 struct extent_buffer *buf,
866 struct extent_buffer *cow,
869 struct btrfs_fs_info *fs_info = root->fs_info;
877 * Backrefs update rules:
879 * Always use full backrefs for extent pointers in tree block
880 * allocated by tree relocation.
882 * If a shared tree block is no longer referenced by its owner
883 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
884 * use full backrefs for extent pointers in tree block.
886 * If a tree block is been relocating
887 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
888 * use full backrefs for extent pointers in tree block.
889 * The reason for this is some operations (such as drop tree)
890 * are only allowed for blocks use full backrefs.
893 if (btrfs_block_can_be_shared(root, buf)) {
894 ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
895 btrfs_header_level(buf), 1,
901 btrfs_handle_fs_error(fs_info, ret, NULL);
906 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
907 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
908 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
913 owner = btrfs_header_owner(buf);
914 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
915 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
918 if ((owner == root->root_key.objectid ||
919 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
920 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
921 ret = btrfs_inc_ref(trans, root, buf, 1);
925 if (root->root_key.objectid ==
926 BTRFS_TREE_RELOC_OBJECTID) {
927 ret = btrfs_dec_ref(trans, root, buf, 0);
930 ret = btrfs_inc_ref(trans, root, cow, 1);
934 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
937 if (root->root_key.objectid ==
938 BTRFS_TREE_RELOC_OBJECTID)
939 ret = btrfs_inc_ref(trans, root, cow, 1);
941 ret = btrfs_inc_ref(trans, root, cow, 0);
945 if (new_flags != 0) {
946 int level = btrfs_header_level(buf);
948 ret = btrfs_set_disk_extent_flags(trans,
951 new_flags, level, 0);
956 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
957 if (root->root_key.objectid ==
958 BTRFS_TREE_RELOC_OBJECTID)
959 ret = btrfs_inc_ref(trans, root, cow, 1);
961 ret = btrfs_inc_ref(trans, root, cow, 0);
964 ret = btrfs_dec_ref(trans, root, buf, 1);
968 btrfs_clean_tree_block(buf);
974 static struct extent_buffer *alloc_tree_block_no_bg_flush(
975 struct btrfs_trans_handle *trans,
976 struct btrfs_root *root,
978 const struct btrfs_disk_key *disk_key,
983 struct btrfs_fs_info *fs_info = root->fs_info;
984 struct extent_buffer *ret;
987 * If we are COWing a node/leaf from the extent, chunk, device or free
988 * space trees, make sure that we do not finish block group creation of
989 * pending block groups. We do this to avoid a deadlock.
990 * COWing can result in allocation of a new chunk, and flushing pending
991 * block groups (btrfs_create_pending_block_groups()) can be triggered
992 * when finishing allocation of a new chunk. Creation of a pending block
993 * group modifies the extent, chunk, device and free space trees,
994 * therefore we could deadlock with ourselves since we are holding a
995 * lock on an extent buffer that btrfs_create_pending_block_groups() may
997 * For similar reasons, we also need to delay flushing pending block
998 * groups when splitting a leaf or node, from one of those trees, since
999 * we are holding a write lock on it and its parent or when inserting a
1000 * new root node for one of those trees.
1002 if (root == fs_info->extent_root ||
1003 root == fs_info->chunk_root ||
1004 root == fs_info->dev_root ||
1005 root == fs_info->free_space_root)
1006 trans->can_flush_pending_bgs = false;
1008 ret = btrfs_alloc_tree_block(trans, root, parent_start,
1009 root->root_key.objectid, disk_key, level,
1011 trans->can_flush_pending_bgs = true;
1017 * does the dirty work in cow of a single block. The parent block (if
1018 * supplied) is updated to point to the new cow copy. The new buffer is marked
1019 * dirty and returned locked. If you modify the block it needs to be marked
1022 * search_start -- an allocation hint for the new block
1024 * empty_size -- a hint that you plan on doing more cow. This is the size in
1025 * bytes the allocator should try to find free next to the block it returns.
1026 * This is just a hint and may be ignored by the allocator.
1028 static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
1029 struct btrfs_root *root,
1030 struct extent_buffer *buf,
1031 struct extent_buffer *parent, int parent_slot,
1032 struct extent_buffer **cow_ret,
1033 u64 search_start, u64 empty_size)
1035 struct btrfs_fs_info *fs_info = root->fs_info;
1036 struct btrfs_disk_key disk_key;
1037 struct extent_buffer *cow;
1040 int unlock_orig = 0;
1041 u64 parent_start = 0;
1043 if (*cow_ret == buf)
1046 btrfs_assert_tree_locked(buf);
1048 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1049 trans->transid != fs_info->running_transaction->transid);
1050 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1051 trans->transid != root->last_trans);
1053 level = btrfs_header_level(buf);
1056 btrfs_item_key(buf, &disk_key, 0);
1058 btrfs_node_key(buf, &disk_key, 0);
1060 if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
1061 parent_start = parent->start;
1063 cow = alloc_tree_block_no_bg_flush(trans, root, parent_start, &disk_key,
1064 level, search_start, empty_size);
1066 return PTR_ERR(cow);
1068 /* cow is set to blocking by btrfs_init_new_buffer */
1070 copy_extent_buffer_full(cow, buf);
1071 btrfs_set_header_bytenr(cow, cow->start);
1072 btrfs_set_header_generation(cow, trans->transid);
1073 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1074 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1075 BTRFS_HEADER_FLAG_RELOC);
1076 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1077 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1079 btrfs_set_header_owner(cow, root->root_key.objectid);
1081 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
1083 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
1085 btrfs_abort_transaction(trans, ret);
1089 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
1090 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
1092 btrfs_abort_transaction(trans, ret);
1097 if (buf == root->node) {
1098 WARN_ON(parent && parent != buf);
1099 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1100 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1101 parent_start = buf->start;
1103 atomic_inc(&cow->refs);
1104 ret = tree_mod_log_insert_root(root->node, cow, 1);
1106 rcu_assign_pointer(root->node, cow);
1108 btrfs_free_tree_block(trans, root, buf, parent_start,
1110 free_extent_buffer(buf);
1111 add_root_to_dirty_list(root);
1113 WARN_ON(trans->transid != btrfs_header_generation(parent));
1114 tree_mod_log_insert_key(parent, parent_slot,
1115 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1116 btrfs_set_node_blockptr(parent, parent_slot,
1118 btrfs_set_node_ptr_generation(parent, parent_slot,
1120 btrfs_mark_buffer_dirty(parent);
1122 ret = tree_mod_log_free_eb(buf);
1124 btrfs_abort_transaction(trans, ret);
1128 btrfs_free_tree_block(trans, root, buf, parent_start,
1132 btrfs_tree_unlock(buf);
1133 free_extent_buffer_stale(buf);
1134 btrfs_mark_buffer_dirty(cow);
1140 * returns the logical address of the oldest predecessor of the given root.
1141 * entries older than time_seq are ignored.
1143 static struct tree_mod_elem *__tree_mod_log_oldest_root(
1144 struct extent_buffer *eb_root, u64 time_seq)
1146 struct tree_mod_elem *tm;
1147 struct tree_mod_elem *found = NULL;
1148 u64 root_logical = eb_root->start;
1155 * the very last operation that's logged for a root is the
1156 * replacement operation (if it is replaced at all). this has
1157 * the logical address of the *new* root, making it the very
1158 * first operation that's logged for this root.
1161 tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical,
1166 * if there are no tree operation for the oldest root, we simply
1167 * return it. this should only happen if that (old) root is at
1174 * if there's an operation that's not a root replacement, we
1175 * found the oldest version of our root. normally, we'll find a
1176 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1178 if (tm->op != MOD_LOG_ROOT_REPLACE)
1182 root_logical = tm->old_root.logical;
1186 /* if there's no old root to return, return what we found instead */
1194 * tm is a pointer to the first operation to rewind within eb. then, all
1195 * previous operations will be rewound (until we reach something older than
1199 __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1200 u64 time_seq, struct tree_mod_elem *first_tm)
1203 struct rb_node *next;
1204 struct tree_mod_elem *tm = first_tm;
1205 unsigned long o_dst;
1206 unsigned long o_src;
1207 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1209 n = btrfs_header_nritems(eb);
1210 read_lock(&fs_info->tree_mod_log_lock);
1211 while (tm && tm->seq >= time_seq) {
1213 * all the operations are recorded with the operator used for
1214 * the modification. as we're going backwards, we do the
1215 * opposite of each operation here.
1218 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1219 BUG_ON(tm->slot < n);
1221 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
1222 case MOD_LOG_KEY_REMOVE:
1223 btrfs_set_node_key(eb, &tm->key, tm->slot);
1224 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1225 btrfs_set_node_ptr_generation(eb, tm->slot,
1229 case MOD_LOG_KEY_REPLACE:
1230 BUG_ON(tm->slot >= n);
1231 btrfs_set_node_key(eb, &tm->key, tm->slot);
1232 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1233 btrfs_set_node_ptr_generation(eb, tm->slot,
1236 case MOD_LOG_KEY_ADD:
1237 /* if a move operation is needed it's in the log */
1240 case MOD_LOG_MOVE_KEYS:
1241 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1242 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1243 memmove_extent_buffer(eb, o_dst, o_src,
1244 tm->move.nr_items * p_size);
1246 case MOD_LOG_ROOT_REPLACE:
1248 * this operation is special. for roots, this must be
1249 * handled explicitly before rewinding.
1250 * for non-roots, this operation may exist if the node
1251 * was a root: root A -> child B; then A gets empty and
1252 * B is promoted to the new root. in the mod log, we'll
1253 * have a root-replace operation for B, a tree block
1254 * that is no root. we simply ignore that operation.
1258 next = rb_next(&tm->node);
1261 tm = rb_entry(next, struct tree_mod_elem, node);
1262 if (tm->logical != first_tm->logical)
1265 read_unlock(&fs_info->tree_mod_log_lock);
1266 btrfs_set_header_nritems(eb, n);
1270 * Called with eb read locked. If the buffer cannot be rewound, the same buffer
1271 * is returned. If rewind operations happen, a fresh buffer is returned. The
1272 * returned buffer is always read-locked. If the returned buffer is not the
1273 * input buffer, the lock on the input buffer is released and the input buffer
1274 * is freed (its refcount is decremented).
1276 static struct extent_buffer *
1277 tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1278 struct extent_buffer *eb, u64 time_seq)
1280 struct extent_buffer *eb_rewin;
1281 struct tree_mod_elem *tm;
1286 if (btrfs_header_level(eb) == 0)
1289 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1293 btrfs_set_path_blocking(path);
1294 btrfs_set_lock_blocking_read(eb);
1296 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1297 BUG_ON(tm->slot != 0);
1298 eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
1300 btrfs_tree_read_unlock_blocking(eb);
1301 free_extent_buffer(eb);
1304 btrfs_set_header_bytenr(eb_rewin, eb->start);
1305 btrfs_set_header_backref_rev(eb_rewin,
1306 btrfs_header_backref_rev(eb));
1307 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
1308 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
1310 eb_rewin = btrfs_clone_extent_buffer(eb);
1312 btrfs_tree_read_unlock_blocking(eb);
1313 free_extent_buffer(eb);
1318 btrfs_tree_read_unlock_blocking(eb);
1319 free_extent_buffer(eb);
1321 btrfs_tree_read_lock(eb_rewin);
1322 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
1323 WARN_ON(btrfs_header_nritems(eb_rewin) >
1324 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
1330 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1331 * value. If there are no changes, the current root->root_node is returned. If
1332 * anything changed in between, there's a fresh buffer allocated on which the
1333 * rewind operations are done. In any case, the returned buffer is read locked.
1334 * Returns NULL on error (with no locks held).
1336 static inline struct extent_buffer *
1337 get_old_root(struct btrfs_root *root, u64 time_seq)
1339 struct btrfs_fs_info *fs_info = root->fs_info;
1340 struct tree_mod_elem *tm;
1341 struct extent_buffer *eb = NULL;
1342 struct extent_buffer *eb_root;
1343 u64 eb_root_owner = 0;
1344 struct extent_buffer *old;
1345 struct tree_mod_root *old_root = NULL;
1346 u64 old_generation = 0;
1350 eb_root = btrfs_read_lock_root_node(root);
1351 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
1355 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1356 old_root = &tm->old_root;
1357 old_generation = tm->generation;
1358 logical = old_root->logical;
1359 level = old_root->level;
1361 logical = eb_root->start;
1362 level = btrfs_header_level(eb_root);
1365 tm = tree_mod_log_search(fs_info, logical, time_seq);
1366 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1367 btrfs_tree_read_unlock(eb_root);
1368 free_extent_buffer(eb_root);
1369 old = read_tree_block(fs_info, logical, 0, level, NULL);
1370 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
1372 free_extent_buffer(old);
1374 "failed to read tree block %llu from get_old_root",
1377 eb = btrfs_clone_extent_buffer(old);
1378 free_extent_buffer(old);
1380 } else if (old_root) {
1381 eb_root_owner = btrfs_header_owner(eb_root);
1382 btrfs_tree_read_unlock(eb_root);
1383 free_extent_buffer(eb_root);
1384 eb = alloc_dummy_extent_buffer(fs_info, logical);
1386 btrfs_set_lock_blocking_read(eb_root);
1387 eb = btrfs_clone_extent_buffer(eb_root);
1388 btrfs_tree_read_unlock_blocking(eb_root);
1389 free_extent_buffer(eb_root);
1394 btrfs_tree_read_lock(eb);
1396 btrfs_set_header_bytenr(eb, eb->start);
1397 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
1398 btrfs_set_header_owner(eb, eb_root_owner);
1399 btrfs_set_header_level(eb, old_root->level);
1400 btrfs_set_header_generation(eb, old_generation);
1403 __tree_mod_log_rewind(fs_info, eb, time_seq, tm);
1405 WARN_ON(btrfs_header_level(eb) != 0);
1406 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
1411 int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1413 struct tree_mod_elem *tm;
1415 struct extent_buffer *eb_root = btrfs_root_node(root);
1417 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
1418 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1419 level = tm->old_root.level;
1421 level = btrfs_header_level(eb_root);
1423 free_extent_buffer(eb_root);
1428 static inline int should_cow_block(struct btrfs_trans_handle *trans,
1429 struct btrfs_root *root,
1430 struct extent_buffer *buf)
1432 if (btrfs_is_testing(root->fs_info))
1435 /* Ensure we can see the FORCE_COW bit */
1436 smp_mb__before_atomic();
1439 * We do not need to cow a block if
1440 * 1) this block is not created or changed in this transaction;
1441 * 2) this block does not belong to TREE_RELOC tree;
1442 * 3) the root is not forced COW.
1444 * What is forced COW:
1445 * when we create snapshot during committing the transaction,
1446 * after we've finished copying src root, we must COW the shared
1447 * block to ensure the metadata consistency.
1449 if (btrfs_header_generation(buf) == trans->transid &&
1450 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1451 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
1452 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1453 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
1459 * cows a single block, see __btrfs_cow_block for the real work.
1460 * This version of it has extra checks so that a block isn't COWed more than
1461 * once per transaction, as long as it hasn't been written yet
1463 noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
1464 struct btrfs_root *root, struct extent_buffer *buf,
1465 struct extent_buffer *parent, int parent_slot,
1466 struct extent_buffer **cow_ret)
1468 struct btrfs_fs_info *fs_info = root->fs_info;
1472 if (test_bit(BTRFS_ROOT_DELETING, &root->state))
1474 "COW'ing blocks on a fs root that's being dropped");
1476 if (trans->transaction != fs_info->running_transaction)
1477 WARN(1, KERN_CRIT "trans %llu running %llu\n",
1479 fs_info->running_transaction->transid);
1481 if (trans->transid != fs_info->generation)
1482 WARN(1, KERN_CRIT "trans %llu running %llu\n",
1483 trans->transid, fs_info->generation);
1485 if (!should_cow_block(trans, root, buf)) {
1486 trans->dirty = true;
1491 search_start = buf->start & ~((u64)SZ_1G - 1);
1494 btrfs_set_lock_blocking_write(parent);
1495 btrfs_set_lock_blocking_write(buf);
1498 * Before CoWing this block for later modification, check if it's
1499 * the subtree root and do the delayed subtree trace if needed.
1501 * Also We don't care about the error, as it's handled internally.
1503 btrfs_qgroup_trace_subtree_after_cow(trans, root, buf);
1504 ret = __btrfs_cow_block(trans, root, buf, parent,
1505 parent_slot, cow_ret, search_start, 0);
1507 trace_btrfs_cow_block(root, buf, *cow_ret);
1513 * helper function for defrag to decide if two blocks pointed to by a
1514 * node are actually close by
1516 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
1518 if (blocknr < other && other - (blocknr + blocksize) < 32768)
1520 if (blocknr > other && blocknr - (other + blocksize) < 32768)
1526 * compare two keys in a memcmp fashion
1528 static int comp_keys(const struct btrfs_disk_key *disk,
1529 const struct btrfs_key *k2)
1531 struct btrfs_key k1;
1533 btrfs_disk_key_to_cpu(&k1, disk);
1535 return btrfs_comp_cpu_keys(&k1, k2);
1539 * same as comp_keys only with two btrfs_key's
1541 int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
1543 if (k1->objectid > k2->objectid)
1545 if (k1->objectid < k2->objectid)
1547 if (k1->type > k2->type)
1549 if (k1->type < k2->type)
1551 if (k1->offset > k2->offset)
1553 if (k1->offset < k2->offset)
1559 * this is used by the defrag code to go through all the
1560 * leaves pointed to by a node and reallocate them so that
1561 * disk order is close to key order
1563 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
1564 struct btrfs_root *root, struct extent_buffer *parent,
1565 int start_slot, u64 *last_ret,
1566 struct btrfs_key *progress)
1568 struct btrfs_fs_info *fs_info = root->fs_info;
1569 struct extent_buffer *cur;
1572 u64 search_start = *last_ret;
1582 int progress_passed = 0;
1583 struct btrfs_disk_key disk_key;
1585 parent_level = btrfs_header_level(parent);
1587 WARN_ON(trans->transaction != fs_info->running_transaction);
1588 WARN_ON(trans->transid != fs_info->generation);
1590 parent_nritems = btrfs_header_nritems(parent);
1591 blocksize = fs_info->nodesize;
1592 end_slot = parent_nritems - 1;
1594 if (parent_nritems <= 1)
1597 btrfs_set_lock_blocking_write(parent);
1599 for (i = start_slot; i <= end_slot; i++) {
1600 struct btrfs_key first_key;
1603 btrfs_node_key(parent, &disk_key, i);
1604 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1607 progress_passed = 1;
1608 blocknr = btrfs_node_blockptr(parent, i);
1609 gen = btrfs_node_ptr_generation(parent, i);
1610 btrfs_node_key_to_cpu(parent, &first_key, i);
1611 if (last_block == 0)
1612 last_block = blocknr;
1615 other = btrfs_node_blockptr(parent, i - 1);
1616 close = close_blocks(blocknr, other, blocksize);
1618 if (!close && i < end_slot) {
1619 other = btrfs_node_blockptr(parent, i + 1);
1620 close = close_blocks(blocknr, other, blocksize);
1623 last_block = blocknr;
1627 cur = find_extent_buffer(fs_info, blocknr);
1629 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
1632 if (!cur || !uptodate) {
1634 cur = read_tree_block(fs_info, blocknr, gen,
1638 return PTR_ERR(cur);
1639 } else if (!extent_buffer_uptodate(cur)) {
1640 free_extent_buffer(cur);
1643 } else if (!uptodate) {
1644 err = btrfs_read_buffer(cur, gen,
1645 parent_level - 1,&first_key);
1647 free_extent_buffer(cur);
1652 if (search_start == 0)
1653 search_start = last_block;
1655 btrfs_tree_lock(cur);
1656 btrfs_set_lock_blocking_write(cur);
1657 err = __btrfs_cow_block(trans, root, cur, parent, i,
1660 (end_slot - i) * blocksize));
1662 btrfs_tree_unlock(cur);
1663 free_extent_buffer(cur);
1666 search_start = cur->start;
1667 last_block = cur->start;
1668 *last_ret = search_start;
1669 btrfs_tree_unlock(cur);
1670 free_extent_buffer(cur);
1676 * search for key in the extent_buffer. The items start at offset p,
1677 * and they are item_size apart. There are 'max' items in p.
1679 * the slot in the array is returned via slot, and it points to
1680 * the place where you would insert key if it is not found in
1683 * slot may point to max if the key is bigger than all of the keys
1685 static noinline int generic_bin_search(struct extent_buffer *eb,
1686 unsigned long p, int item_size,
1687 const struct btrfs_key *key,
1694 struct btrfs_disk_key *tmp = NULL;
1695 struct btrfs_disk_key unaligned;
1696 unsigned long offset;
1698 unsigned long map_start = 0;
1699 unsigned long map_len = 0;
1703 btrfs_err(eb->fs_info,
1704 "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
1705 __func__, low, high, eb->start,
1706 btrfs_header_owner(eb), btrfs_header_level(eb));
1710 while (low < high) {
1711 mid = (low + high) / 2;
1712 offset = p + mid * item_size;
1714 if (!kaddr || offset < map_start ||
1715 (offset + sizeof(struct btrfs_disk_key)) >
1716 map_start + map_len) {
1718 err = map_private_extent_buffer(eb, offset,
1719 sizeof(struct btrfs_disk_key),
1720 &kaddr, &map_start, &map_len);
1723 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1725 } else if (err == 1) {
1726 read_extent_buffer(eb, &unaligned,
1727 offset, sizeof(unaligned));
1734 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1737 ret = comp_keys(tmp, key);
1753 * simple bin_search frontend that does the right thing for
1756 int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
1757 int level, int *slot)
1760 return generic_bin_search(eb,
1761 offsetof(struct btrfs_leaf, items),
1762 sizeof(struct btrfs_item),
1763 key, btrfs_header_nritems(eb),
1766 return generic_bin_search(eb,
1767 offsetof(struct btrfs_node, ptrs),
1768 sizeof(struct btrfs_key_ptr),
1769 key, btrfs_header_nritems(eb),
1773 static void root_add_used(struct btrfs_root *root, u32 size)
1775 spin_lock(&root->accounting_lock);
1776 btrfs_set_root_used(&root->root_item,
1777 btrfs_root_used(&root->root_item) + size);
1778 spin_unlock(&root->accounting_lock);
1781 static void root_sub_used(struct btrfs_root *root, u32 size)
1783 spin_lock(&root->accounting_lock);
1784 btrfs_set_root_used(&root->root_item,
1785 btrfs_root_used(&root->root_item) - size);
1786 spin_unlock(&root->accounting_lock);
1789 /* given a node and slot number, this reads the blocks it points to. The
1790 * extent buffer is returned with a reference taken (but unlocked).
1792 struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
1795 int level = btrfs_header_level(parent);
1796 struct extent_buffer *eb;
1797 struct btrfs_key first_key;
1799 if (slot < 0 || slot >= btrfs_header_nritems(parent))
1800 return ERR_PTR(-ENOENT);
1804 btrfs_node_key_to_cpu(parent, &first_key, slot);
1805 eb = read_tree_block(parent->fs_info, btrfs_node_blockptr(parent, slot),
1806 btrfs_node_ptr_generation(parent, slot),
1807 level - 1, &first_key);
1808 if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
1809 free_extent_buffer(eb);
1817 * node level balancing, used to make sure nodes are in proper order for
1818 * item deletion. We balance from the top down, so we have to make sure
1819 * that a deletion won't leave an node completely empty later on.
1821 static noinline int balance_level(struct btrfs_trans_handle *trans,
1822 struct btrfs_root *root,
1823 struct btrfs_path *path, int level)
1825 struct btrfs_fs_info *fs_info = root->fs_info;
1826 struct extent_buffer *right = NULL;
1827 struct extent_buffer *mid;
1828 struct extent_buffer *left = NULL;
1829 struct extent_buffer *parent = NULL;
1833 int orig_slot = path->slots[level];
1838 mid = path->nodes[level];
1840 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1841 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
1842 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1844 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1846 if (level < BTRFS_MAX_LEVEL - 1) {
1847 parent = path->nodes[level + 1];
1848 pslot = path->slots[level + 1];
1852 * deal with the case where there is only one pointer in the root
1853 * by promoting the node below to a root
1856 struct extent_buffer *child;
1858 if (btrfs_header_nritems(mid) != 1)
1861 /* promote the child to a root */
1862 child = btrfs_read_node_slot(mid, 0);
1863 if (IS_ERR(child)) {
1864 ret = PTR_ERR(child);
1865 btrfs_handle_fs_error(fs_info, ret, NULL);
1869 btrfs_tree_lock(child);
1870 btrfs_set_lock_blocking_write(child);
1871 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
1873 btrfs_tree_unlock(child);
1874 free_extent_buffer(child);
1878 ret = tree_mod_log_insert_root(root->node, child, 1);
1880 rcu_assign_pointer(root->node, child);
1882 add_root_to_dirty_list(root);
1883 btrfs_tree_unlock(child);
1885 path->locks[level] = 0;
1886 path->nodes[level] = NULL;
1887 btrfs_clean_tree_block(mid);
1888 btrfs_tree_unlock(mid);
1889 /* once for the path */
1890 free_extent_buffer(mid);
1892 root_sub_used(root, mid->len);
1893 btrfs_free_tree_block(trans, root, mid, 0, 1);
1894 /* once for the root ptr */
1895 free_extent_buffer_stale(mid);
1898 if (btrfs_header_nritems(mid) >
1899 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
1902 left = btrfs_read_node_slot(parent, pslot - 1);
1907 btrfs_tree_lock(left);
1908 btrfs_set_lock_blocking_write(left);
1909 wret = btrfs_cow_block(trans, root, left,
1910 parent, pslot - 1, &left);
1917 right = btrfs_read_node_slot(parent, pslot + 1);
1922 btrfs_tree_lock(right);
1923 btrfs_set_lock_blocking_write(right);
1924 wret = btrfs_cow_block(trans, root, right,
1925 parent, pslot + 1, &right);
1932 /* first, try to make some room in the middle buffer */
1934 orig_slot += btrfs_header_nritems(left);
1935 wret = push_node_left(trans, left, mid, 1);
1941 * then try to empty the right most buffer into the middle
1944 wret = push_node_left(trans, mid, right, 1);
1945 if (wret < 0 && wret != -ENOSPC)
1947 if (btrfs_header_nritems(right) == 0) {
1948 btrfs_clean_tree_block(right);
1949 btrfs_tree_unlock(right);
1950 del_ptr(root, path, level + 1, pslot + 1);
1951 root_sub_used(root, right->len);
1952 btrfs_free_tree_block(trans, root, right, 0, 1);
1953 free_extent_buffer_stale(right);
1956 struct btrfs_disk_key right_key;
1957 btrfs_node_key(right, &right_key, 0);
1958 ret = tree_mod_log_insert_key(parent, pslot + 1,
1959 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1961 btrfs_set_node_key(parent, &right_key, pslot + 1);
1962 btrfs_mark_buffer_dirty(parent);
1965 if (btrfs_header_nritems(mid) == 1) {
1967 * we're not allowed to leave a node with one item in the
1968 * tree during a delete. A deletion from lower in the tree
1969 * could try to delete the only pointer in this node.
1970 * So, pull some keys from the left.
1971 * There has to be a left pointer at this point because
1972 * otherwise we would have pulled some pointers from the
1977 btrfs_handle_fs_error(fs_info, ret, NULL);
1980 wret = balance_node_right(trans, mid, left);
1986 wret = push_node_left(trans, left, mid, 1);
1992 if (btrfs_header_nritems(mid) == 0) {
1993 btrfs_clean_tree_block(mid);
1994 btrfs_tree_unlock(mid);
1995 del_ptr(root, path, level + 1, pslot);
1996 root_sub_used(root, mid->len);
1997 btrfs_free_tree_block(trans, root, mid, 0, 1);
1998 free_extent_buffer_stale(mid);
2001 /* update the parent key to reflect our changes */
2002 struct btrfs_disk_key mid_key;
2003 btrfs_node_key(mid, &mid_key, 0);
2004 ret = tree_mod_log_insert_key(parent, pslot,
2005 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2007 btrfs_set_node_key(parent, &mid_key, pslot);
2008 btrfs_mark_buffer_dirty(parent);
2011 /* update the path */
2013 if (btrfs_header_nritems(left) > orig_slot) {
2014 atomic_inc(&left->refs);
2015 /* left was locked after cow */
2016 path->nodes[level] = left;
2017 path->slots[level + 1] -= 1;
2018 path->slots[level] = orig_slot;
2020 btrfs_tree_unlock(mid);
2021 free_extent_buffer(mid);
2024 orig_slot -= btrfs_header_nritems(left);
2025 path->slots[level] = orig_slot;
2028 /* double check we haven't messed things up */
2030 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
2034 btrfs_tree_unlock(right);
2035 free_extent_buffer(right);
2038 if (path->nodes[level] != left)
2039 btrfs_tree_unlock(left);
2040 free_extent_buffer(left);
2045 /* Node balancing for insertion. Here we only split or push nodes around
2046 * when they are completely full. This is also done top down, so we
2047 * have to be pessimistic.
2049 static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
2050 struct btrfs_root *root,
2051 struct btrfs_path *path, int level)
2053 struct btrfs_fs_info *fs_info = root->fs_info;
2054 struct extent_buffer *right = NULL;
2055 struct extent_buffer *mid;
2056 struct extent_buffer *left = NULL;
2057 struct extent_buffer *parent = NULL;
2061 int orig_slot = path->slots[level];
2066 mid = path->nodes[level];
2067 WARN_ON(btrfs_header_generation(mid) != trans->transid);
2069 if (level < BTRFS_MAX_LEVEL - 1) {
2070 parent = path->nodes[level + 1];
2071 pslot = path->slots[level + 1];
2077 left = btrfs_read_node_slot(parent, pslot - 1);
2081 /* first, try to make some room in the middle buffer */
2085 btrfs_tree_lock(left);
2086 btrfs_set_lock_blocking_write(left);
2088 left_nr = btrfs_header_nritems(left);
2089 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
2092 ret = btrfs_cow_block(trans, root, left, parent,
2097 wret = push_node_left(trans, left, mid, 0);
2103 struct btrfs_disk_key disk_key;
2104 orig_slot += left_nr;
2105 btrfs_node_key(mid, &disk_key, 0);
2106 ret = tree_mod_log_insert_key(parent, pslot,
2107 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2109 btrfs_set_node_key(parent, &disk_key, pslot);
2110 btrfs_mark_buffer_dirty(parent);
2111 if (btrfs_header_nritems(left) > orig_slot) {
2112 path->nodes[level] = left;
2113 path->slots[level + 1] -= 1;
2114 path->slots[level] = orig_slot;
2115 btrfs_tree_unlock(mid);
2116 free_extent_buffer(mid);
2119 btrfs_header_nritems(left);
2120 path->slots[level] = orig_slot;
2121 btrfs_tree_unlock(left);
2122 free_extent_buffer(left);
2126 btrfs_tree_unlock(left);
2127 free_extent_buffer(left);
2129 right = btrfs_read_node_slot(parent, pslot + 1);
2134 * then try to empty the right most buffer into the middle
2139 btrfs_tree_lock(right);
2140 btrfs_set_lock_blocking_write(right);
2142 right_nr = btrfs_header_nritems(right);
2143 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
2146 ret = btrfs_cow_block(trans, root, right,
2152 wret = balance_node_right(trans, right, mid);
2158 struct btrfs_disk_key disk_key;
2160 btrfs_node_key(right, &disk_key, 0);
2161 ret = tree_mod_log_insert_key(parent, pslot + 1,
2162 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2164 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2165 btrfs_mark_buffer_dirty(parent);
2167 if (btrfs_header_nritems(mid) <= orig_slot) {
2168 path->nodes[level] = right;
2169 path->slots[level + 1] += 1;
2170 path->slots[level] = orig_slot -
2171 btrfs_header_nritems(mid);
2172 btrfs_tree_unlock(mid);
2173 free_extent_buffer(mid);
2175 btrfs_tree_unlock(right);
2176 free_extent_buffer(right);
2180 btrfs_tree_unlock(right);
2181 free_extent_buffer(right);
2187 * readahead one full node of leaves, finding things that are close
2188 * to the block in 'slot', and triggering ra on them.
2190 static void reada_for_search(struct btrfs_fs_info *fs_info,
2191 struct btrfs_path *path,
2192 int level, int slot, u64 objectid)
2194 struct extent_buffer *node;
2195 struct btrfs_disk_key disk_key;
2200 struct extent_buffer *eb;
2208 if (!path->nodes[level])
2211 node = path->nodes[level];
2213 search = btrfs_node_blockptr(node, slot);
2214 blocksize = fs_info->nodesize;
2215 eb = find_extent_buffer(fs_info, search);
2217 free_extent_buffer(eb);
2223 nritems = btrfs_header_nritems(node);
2227 if (path->reada == READA_BACK) {
2231 } else if (path->reada == READA_FORWARD) {
2236 if (path->reada == READA_BACK && objectid) {
2237 btrfs_node_key(node, &disk_key, nr);
2238 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2241 search = btrfs_node_blockptr(node, nr);
2242 if ((search <= target && target - search <= 65536) ||
2243 (search > target && search - target <= 65536)) {
2244 readahead_tree_block(fs_info, search);
2248 if ((nread > 65536 || nscan > 32))
2253 static noinline void reada_for_balance(struct btrfs_fs_info *fs_info,
2254 struct btrfs_path *path, int level)
2258 struct extent_buffer *parent;
2259 struct extent_buffer *eb;
2264 parent = path->nodes[level + 1];
2268 nritems = btrfs_header_nritems(parent);
2269 slot = path->slots[level + 1];
2272 block1 = btrfs_node_blockptr(parent, slot - 1);
2273 gen = btrfs_node_ptr_generation(parent, slot - 1);
2274 eb = find_extent_buffer(fs_info, block1);
2276 * if we get -eagain from btrfs_buffer_uptodate, we
2277 * don't want to return eagain here. That will loop
2280 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
2282 free_extent_buffer(eb);
2284 if (slot + 1 < nritems) {
2285 block2 = btrfs_node_blockptr(parent, slot + 1);
2286 gen = btrfs_node_ptr_generation(parent, slot + 1);
2287 eb = find_extent_buffer(fs_info, block2);
2288 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
2290 free_extent_buffer(eb);
2294 readahead_tree_block(fs_info, block1);
2296 readahead_tree_block(fs_info, block2);
2301 * when we walk down the tree, it is usually safe to unlock the higher layers
2302 * in the tree. The exceptions are when our path goes through slot 0, because
2303 * operations on the tree might require changing key pointers higher up in the
2306 * callers might also have set path->keep_locks, which tells this code to keep
2307 * the lock if the path points to the last slot in the block. This is part of
2308 * walking through the tree, and selecting the next slot in the higher block.
2310 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2311 * if lowest_unlock is 1, level 0 won't be unlocked
2313 static noinline void unlock_up(struct btrfs_path *path, int level,
2314 int lowest_unlock, int min_write_lock_level,
2315 int *write_lock_level)
2318 int skip_level = level;
2320 struct extent_buffer *t;
2322 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2323 if (!path->nodes[i])
2325 if (!path->locks[i])
2327 if (!no_skips && path->slots[i] == 0) {
2331 if (!no_skips && path->keep_locks) {
2334 nritems = btrfs_header_nritems(t);
2335 if (nritems < 1 || path->slots[i] >= nritems - 1) {
2340 if (skip_level < i && i >= lowest_unlock)
2344 if (i >= lowest_unlock && i > skip_level) {
2345 btrfs_tree_unlock_rw(t, path->locks[i]);
2347 if (write_lock_level &&
2348 i > min_write_lock_level &&
2349 i <= *write_lock_level) {
2350 *write_lock_level = i - 1;
2357 * helper function for btrfs_search_slot. The goal is to find a block
2358 * in cache without setting the path to blocking. If we find the block
2359 * we return zero and the path is unchanged.
2361 * If we can't find the block, we set the path blocking and do some
2362 * reada. -EAGAIN is returned and the search must be repeated.
2365 read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
2366 struct extent_buffer **eb_ret, int level, int slot,
2367 const struct btrfs_key *key)
2369 struct btrfs_fs_info *fs_info = root->fs_info;
2372 struct extent_buffer *b = *eb_ret;
2373 struct extent_buffer *tmp;
2374 struct btrfs_key first_key;
2378 blocknr = btrfs_node_blockptr(b, slot);
2379 gen = btrfs_node_ptr_generation(b, slot);
2380 parent_level = btrfs_header_level(b);
2381 btrfs_node_key_to_cpu(b, &first_key, slot);
2383 tmp = find_extent_buffer(fs_info, blocknr);
2385 /* first we do an atomic uptodate check */
2386 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2388 * Do extra check for first_key, eb can be stale due to
2389 * being cached, read from scrub, or have multiple
2390 * parents (shared tree blocks).
2392 if (btrfs_verify_level_key(tmp,
2393 parent_level - 1, &first_key, gen)) {
2394 free_extent_buffer(tmp);
2401 /* the pages were up to date, but we failed
2402 * the generation number check. Do a full
2403 * read for the generation number that is correct.
2404 * We must do this without dropping locks so
2405 * we can trust our generation number
2407 btrfs_set_path_blocking(p);
2409 /* now we're allowed to do a blocking uptodate check */
2410 ret = btrfs_read_buffer(tmp, gen, parent_level - 1, &first_key);
2415 free_extent_buffer(tmp);
2416 btrfs_release_path(p);
2421 * reduce lock contention at high levels
2422 * of the btree by dropping locks before
2423 * we read. Don't release the lock on the current
2424 * level because we need to walk this node to figure
2425 * out which blocks to read.
2427 btrfs_unlock_up_safe(p, level + 1);
2428 btrfs_set_path_blocking(p);
2430 if (p->reada != READA_NONE)
2431 reada_for_search(fs_info, p, level, slot, key->objectid);
2434 tmp = read_tree_block(fs_info, blocknr, gen, parent_level - 1,
2438 * If the read above didn't mark this buffer up to date,
2439 * it will never end up being up to date. Set ret to EIO now
2440 * and give up so that our caller doesn't loop forever
2443 if (!extent_buffer_uptodate(tmp))
2445 free_extent_buffer(tmp);
2450 btrfs_release_path(p);
2455 * helper function for btrfs_search_slot. This does all of the checks
2456 * for node-level blocks and does any balancing required based on
2459 * If no extra work was required, zero is returned. If we had to
2460 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2464 setup_nodes_for_search(struct btrfs_trans_handle *trans,
2465 struct btrfs_root *root, struct btrfs_path *p,
2466 struct extent_buffer *b, int level, int ins_len,
2467 int *write_lock_level)
2469 struct btrfs_fs_info *fs_info = root->fs_info;
2472 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2473 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
2476 if (*write_lock_level < level + 1) {
2477 *write_lock_level = level + 1;
2478 btrfs_release_path(p);
2482 btrfs_set_path_blocking(p);
2483 reada_for_balance(fs_info, p, level);
2484 sret = split_node(trans, root, p, level);
2491 b = p->nodes[level];
2492 } else if (ins_len < 0 && btrfs_header_nritems(b) <
2493 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
2496 if (*write_lock_level < level + 1) {
2497 *write_lock_level = level + 1;
2498 btrfs_release_path(p);
2502 btrfs_set_path_blocking(p);
2503 reada_for_balance(fs_info, p, level);
2504 sret = balance_level(trans, root, p, level);
2510 b = p->nodes[level];
2512 btrfs_release_path(p);
2515 BUG_ON(btrfs_header_nritems(b) == 1);
2525 static int key_search(struct extent_buffer *b, const struct btrfs_key *key,
2526 int level, int *prev_cmp, int *slot)
2528 if (*prev_cmp != 0) {
2529 *prev_cmp = btrfs_bin_search(b, key, level, slot);
2538 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
2539 u64 iobjectid, u64 ioff, u8 key_type,
2540 struct btrfs_key *found_key)
2543 struct btrfs_key key;
2544 struct extent_buffer *eb;
2549 key.type = key_type;
2550 key.objectid = iobjectid;
2553 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
2557 eb = path->nodes[0];
2558 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2559 ret = btrfs_next_leaf(fs_root, path);
2562 eb = path->nodes[0];
2565 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2566 if (found_key->type != key.type ||
2567 found_key->objectid != key.objectid)
2573 static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,
2574 struct btrfs_path *p,
2575 int write_lock_level)
2577 struct btrfs_fs_info *fs_info = root->fs_info;
2578 struct extent_buffer *b;
2582 /* We try very hard to do read locks on the root */
2583 root_lock = BTRFS_READ_LOCK;
2585 if (p->search_commit_root) {
2587 * The commit roots are read only so we always do read locks,
2588 * and we always must hold the commit_root_sem when doing
2589 * searches on them, the only exception is send where we don't
2590 * want to block transaction commits for a long time, so
2591 * we need to clone the commit root in order to avoid races
2592 * with transaction commits that create a snapshot of one of
2593 * the roots used by a send operation.
2595 if (p->need_commit_sem) {
2596 down_read(&fs_info->commit_root_sem);
2597 b = btrfs_clone_extent_buffer(root->commit_root);
2598 up_read(&fs_info->commit_root_sem);
2600 return ERR_PTR(-ENOMEM);
2603 b = root->commit_root;
2604 atomic_inc(&b->refs);
2606 level = btrfs_header_level(b);
2608 * Ensure that all callers have set skip_locking when
2609 * p->search_commit_root = 1.
2611 ASSERT(p->skip_locking == 1);
2616 if (p->skip_locking) {
2617 b = btrfs_root_node(root);
2618 level = btrfs_header_level(b);
2623 * If the level is set to maximum, we can skip trying to get the read
2626 if (write_lock_level < BTRFS_MAX_LEVEL) {
2628 * We don't know the level of the root node until we actually
2629 * have it read locked
2631 b = btrfs_read_lock_root_node(root);
2632 level = btrfs_header_level(b);
2633 if (level > write_lock_level)
2636 /* Whoops, must trade for write lock */
2637 btrfs_tree_read_unlock(b);
2638 free_extent_buffer(b);
2641 b = btrfs_lock_root_node(root);
2642 root_lock = BTRFS_WRITE_LOCK;
2644 /* The level might have changed, check again */
2645 level = btrfs_header_level(b);
2648 p->nodes[level] = b;
2649 if (!p->skip_locking)
2650 p->locks[level] = root_lock;
2652 * Callers are responsible for dropping b's references.
2659 * btrfs_search_slot - look for a key in a tree and perform necessary
2660 * modifications to preserve tree invariants.
2662 * @trans: Handle of transaction, used when modifying the tree
2663 * @p: Holds all btree nodes along the search path
2664 * @root: The root node of the tree
2665 * @key: The key we are looking for
2666 * @ins_len: Indicates purpose of search, for inserts it is 1, for
2667 * deletions it's -1. 0 for plain searches
2668 * @cow: boolean should CoW operations be performed. Must always be 1
2669 * when modifying the tree.
2671 * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
2672 * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
2674 * If @key is found, 0 is returned and you can find the item in the leaf level
2675 * of the path (level 0)
2677 * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
2678 * points to the slot where it should be inserted
2680 * If an error is encountered while searching the tree a negative error number
2683 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2684 const struct btrfs_key *key, struct btrfs_path *p,
2685 int ins_len, int cow)
2687 struct extent_buffer *b;
2692 int lowest_unlock = 1;
2693 /* everything at write_lock_level or lower must be write locked */
2694 int write_lock_level = 0;
2695 u8 lowest_level = 0;
2696 int min_write_lock_level;
2699 lowest_level = p->lowest_level;
2700 WARN_ON(lowest_level && ins_len > 0);
2701 WARN_ON(p->nodes[0] != NULL);
2702 BUG_ON(!cow && ins_len);
2707 /* when we are removing items, we might have to go up to level
2708 * two as we update tree pointers Make sure we keep write
2709 * for those levels as well
2711 write_lock_level = 2;
2712 } else if (ins_len > 0) {
2714 * for inserting items, make sure we have a write lock on
2715 * level 1 so we can update keys
2717 write_lock_level = 1;
2721 write_lock_level = -1;
2723 if (cow && (p->keep_locks || p->lowest_level))
2724 write_lock_level = BTRFS_MAX_LEVEL;
2726 min_write_lock_level = write_lock_level;
2730 b = btrfs_search_slot_get_root(root, p, write_lock_level);
2739 level = btrfs_header_level(b);
2742 bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
2745 * if we don't really need to cow this block
2746 * then we don't want to set the path blocking,
2747 * so we test it here
2749 if (!should_cow_block(trans, root, b)) {
2750 trans->dirty = true;
2755 * must have write locks on this node and the
2758 if (level > write_lock_level ||
2759 (level + 1 > write_lock_level &&
2760 level + 1 < BTRFS_MAX_LEVEL &&
2761 p->nodes[level + 1])) {
2762 write_lock_level = level + 1;
2763 btrfs_release_path(p);
2767 btrfs_set_path_blocking(p);
2769 err = btrfs_cow_block(trans, root, b, NULL, 0,
2772 err = btrfs_cow_block(trans, root, b,
2773 p->nodes[level + 1],
2774 p->slots[level + 1], &b);
2781 p->nodes[level] = b;
2783 * Leave path with blocking locks to avoid massive
2784 * lock context switch, this is made on purpose.
2788 * we have a lock on b and as long as we aren't changing
2789 * the tree, there is no way to for the items in b to change.
2790 * It is safe to drop the lock on our parent before we
2791 * go through the expensive btree search on b.
2793 * If we're inserting or deleting (ins_len != 0), then we might
2794 * be changing slot zero, which may require changing the parent.
2795 * So, we can't drop the lock until after we know which slot
2796 * we're operating on.
2798 if (!ins_len && !p->keep_locks) {
2801 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2802 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2807 ret = key_search(b, key, level, &prev_cmp, &slot);
2812 p->slots[level] = slot;
2814 btrfs_leaf_free_space(b) < ins_len) {
2815 if (write_lock_level < 1) {
2816 write_lock_level = 1;
2817 btrfs_release_path(p);
2821 btrfs_set_path_blocking(p);
2822 err = split_leaf(trans, root, key,
2823 p, ins_len, ret == 0);
2831 if (!p->search_for_split)
2832 unlock_up(p, level, lowest_unlock,
2833 min_write_lock_level, NULL);
2836 if (ret && slot > 0) {
2840 p->slots[level] = slot;
2841 err = setup_nodes_for_search(trans, root, p, b, level, ins_len,
2849 b = p->nodes[level];
2850 slot = p->slots[level];
2853 * Slot 0 is special, if we change the key we have to update
2854 * the parent pointer which means we must have a write lock on
2857 if (slot == 0 && ins_len && write_lock_level < level + 1) {
2858 write_lock_level = level + 1;
2859 btrfs_release_path(p);
2863 unlock_up(p, level, lowest_unlock, min_write_lock_level,
2866 if (level == lowest_level) {
2872 err = read_block_for_search(root, p, &b, level, slot, key);
2880 if (!p->skip_locking) {
2881 level = btrfs_header_level(b);
2882 if (level <= write_lock_level) {
2883 if (!btrfs_try_tree_write_lock(b)) {
2884 btrfs_set_path_blocking(p);
2887 p->locks[level] = BTRFS_WRITE_LOCK;
2889 if (!btrfs_tree_read_lock_atomic(b)) {
2890 btrfs_set_path_blocking(p);
2891 btrfs_tree_read_lock(b);
2893 p->locks[level] = BTRFS_READ_LOCK;
2895 p->nodes[level] = b;
2901 * we don't really know what they plan on doing with the path
2902 * from here on, so for now just mark it as blocking
2904 if (!p->leave_spinning)
2905 btrfs_set_path_blocking(p);
2906 if (ret < 0 && !p->skip_release_on_error)
2907 btrfs_release_path(p);
2912 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2913 * current state of the tree together with the operations recorded in the tree
2914 * modification log to search for the key in a previous version of this tree, as
2915 * denoted by the time_seq parameter.
2917 * Naturally, there is no support for insert, delete or cow operations.
2919 * The resulting path and return value will be set up as if we called
2920 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2922 int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
2923 struct btrfs_path *p, u64 time_seq)
2925 struct btrfs_fs_info *fs_info = root->fs_info;
2926 struct extent_buffer *b;
2931 int lowest_unlock = 1;
2932 u8 lowest_level = 0;
2935 lowest_level = p->lowest_level;
2936 WARN_ON(p->nodes[0] != NULL);
2938 if (p->search_commit_root) {
2940 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2944 b = get_old_root(root, time_seq);
2949 level = btrfs_header_level(b);
2950 p->locks[level] = BTRFS_READ_LOCK;
2955 level = btrfs_header_level(b);
2956 p->nodes[level] = b;
2959 * we have a lock on b and as long as we aren't changing
2960 * the tree, there is no way to for the items in b to change.
2961 * It is safe to drop the lock on our parent before we
2962 * go through the expensive btree search on b.
2964 btrfs_unlock_up_safe(p, level + 1);
2967 * Since we can unwind ebs we want to do a real search every
2971 ret = key_search(b, key, level, &prev_cmp, &slot);
2976 p->slots[level] = slot;
2977 unlock_up(p, level, lowest_unlock, 0, NULL);
2981 if (ret && slot > 0) {
2985 p->slots[level] = slot;
2986 unlock_up(p, level, lowest_unlock, 0, NULL);
2988 if (level == lowest_level) {
2994 err = read_block_for_search(root, p, &b, level, slot, key);
3002 level = btrfs_header_level(b);
3003 if (!btrfs_tree_read_lock_atomic(b)) {
3004 btrfs_set_path_blocking(p);
3005 btrfs_tree_read_lock(b);
3007 b = tree_mod_log_rewind(fs_info, p, b, time_seq);
3012 p->locks[level] = BTRFS_READ_LOCK;
3013 p->nodes[level] = b;
3017 if (!p->leave_spinning)
3018 btrfs_set_path_blocking(p);
3020 btrfs_release_path(p);
3026 * helper to use instead of search slot if no exact match is needed but
3027 * instead the next or previous item should be returned.
3028 * When find_higher is true, the next higher item is returned, the next lower
3030 * When return_any and find_higher are both true, and no higher item is found,
3031 * return the next lower instead.
3032 * When return_any is true and find_higher is false, and no lower item is found,
3033 * return the next higher instead.
3034 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3037 int btrfs_search_slot_for_read(struct btrfs_root *root,
3038 const struct btrfs_key *key,
3039 struct btrfs_path *p, int find_higher,
3043 struct extent_buffer *leaf;
3046 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3050 * a return value of 1 means the path is at the position where the
3051 * item should be inserted. Normally this is the next bigger item,
3052 * but in case the previous item is the last in a leaf, path points
3053 * to the first free slot in the previous leaf, i.e. at an invalid
3059 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3060 ret = btrfs_next_leaf(root, p);
3066 * no higher item found, return the next
3071 btrfs_release_path(p);
3075 if (p->slots[0] == 0) {
3076 ret = btrfs_prev_leaf(root, p);
3081 if (p->slots[0] == btrfs_header_nritems(leaf))
3088 * no lower item found, return the next
3093 btrfs_release_path(p);
3103 * adjust the pointers going up the tree, starting at level
3104 * making sure the right key of each node is points to 'key'.
3105 * This is used after shifting pointers to the left, so it stops
3106 * fixing up pointers when a given leaf/node is not in slot 0 of the
3110 static void fixup_low_keys(struct btrfs_path *path,
3111 struct btrfs_disk_key *key, int level)
3114 struct extent_buffer *t;
3117 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
3118 int tslot = path->slots[i];
3120 if (!path->nodes[i])
3123 ret = tree_mod_log_insert_key(t, tslot, MOD_LOG_KEY_REPLACE,
3126 btrfs_set_node_key(t, key, tslot);
3127 btrfs_mark_buffer_dirty(path->nodes[i]);
3136 * This function isn't completely safe. It's the caller's responsibility
3137 * that the new key won't break the order
3139 void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
3140 struct btrfs_path *path,
3141 const struct btrfs_key *new_key)
3143 struct btrfs_disk_key disk_key;
3144 struct extent_buffer *eb;
3147 eb = path->nodes[0];
3148 slot = path->slots[0];
3150 btrfs_item_key(eb, &disk_key, slot - 1);
3151 if (unlikely(comp_keys(&disk_key, new_key) >= 0)) {
3153 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
3154 slot, btrfs_disk_key_objectid(&disk_key),
3155 btrfs_disk_key_type(&disk_key),
3156 btrfs_disk_key_offset(&disk_key),
3157 new_key->objectid, new_key->type,
3159 btrfs_print_leaf(eb);
3163 if (slot < btrfs_header_nritems(eb) - 1) {
3164 btrfs_item_key(eb, &disk_key, slot + 1);
3165 if (unlikely(comp_keys(&disk_key, new_key) <= 0)) {
3167 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
3168 slot, btrfs_disk_key_objectid(&disk_key),
3169 btrfs_disk_key_type(&disk_key),
3170 btrfs_disk_key_offset(&disk_key),
3171 new_key->objectid, new_key->type,
3173 btrfs_print_leaf(eb);
3178 btrfs_cpu_key_to_disk(&disk_key, new_key);
3179 btrfs_set_item_key(eb, &disk_key, slot);
3180 btrfs_mark_buffer_dirty(eb);
3182 fixup_low_keys(path, &disk_key, 1);
3186 * try to push data from one node into the next node left in the
3189 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3190 * error, and > 0 if there was no room in the left hand block.
3192 static int push_node_left(struct btrfs_trans_handle *trans,
3193 struct extent_buffer *dst,
3194 struct extent_buffer *src, int empty)
3196 struct btrfs_fs_info *fs_info = trans->fs_info;
3202 src_nritems = btrfs_header_nritems(src);
3203 dst_nritems = btrfs_header_nritems(dst);
3204 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
3205 WARN_ON(btrfs_header_generation(src) != trans->transid);
3206 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3208 if (!empty && src_nritems <= 8)
3211 if (push_items <= 0)
3215 push_items = min(src_nritems, push_items);
3216 if (push_items < src_nritems) {
3217 /* leave at least 8 pointers in the node if
3218 * we aren't going to empty it
3220 if (src_nritems - push_items < 8) {
3221 if (push_items <= 8)
3227 push_items = min(src_nritems - 8, push_items);
3229 ret = tree_mod_log_eb_copy(dst, src, dst_nritems, 0, push_items);
3231 btrfs_abort_transaction(trans, ret);
3234 copy_extent_buffer(dst, src,
3235 btrfs_node_key_ptr_offset(dst_nritems),
3236 btrfs_node_key_ptr_offset(0),
3237 push_items * sizeof(struct btrfs_key_ptr));
3239 if (push_items < src_nritems) {
3241 * Don't call tree_mod_log_insert_move here, key removal was
3242 * already fully logged by tree_mod_log_eb_copy above.
3244 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3245 btrfs_node_key_ptr_offset(push_items),
3246 (src_nritems - push_items) *
3247 sizeof(struct btrfs_key_ptr));
3249 btrfs_set_header_nritems(src, src_nritems - push_items);
3250 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3251 btrfs_mark_buffer_dirty(src);
3252 btrfs_mark_buffer_dirty(dst);
3258 * try to push data from one node into the next node right in the
3261 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3262 * error, and > 0 if there was no room in the right hand block.
3264 * this will only push up to 1/2 the contents of the left node over
3266 static int balance_node_right(struct btrfs_trans_handle *trans,
3267 struct extent_buffer *dst,
3268 struct extent_buffer *src)
3270 struct btrfs_fs_info *fs_info = trans->fs_info;
3277 WARN_ON(btrfs_header_generation(src) != trans->transid);
3278 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3280 src_nritems = btrfs_header_nritems(src);
3281 dst_nritems = btrfs_header_nritems(dst);
3282 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
3283 if (push_items <= 0)
3286 if (src_nritems < 4)
3289 max_push = src_nritems / 2 + 1;
3290 /* don't try to empty the node */
3291 if (max_push >= src_nritems)
3294 if (max_push < push_items)
3295 push_items = max_push;
3297 ret = tree_mod_log_insert_move(dst, push_items, 0, dst_nritems);
3299 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3300 btrfs_node_key_ptr_offset(0),
3302 sizeof(struct btrfs_key_ptr));
3304 ret = tree_mod_log_eb_copy(dst, src, 0, src_nritems - push_items,
3307 btrfs_abort_transaction(trans, ret);
3310 copy_extent_buffer(dst, src,
3311 btrfs_node_key_ptr_offset(0),
3312 btrfs_node_key_ptr_offset(src_nritems - push_items),
3313 push_items * sizeof(struct btrfs_key_ptr));
3315 btrfs_set_header_nritems(src, src_nritems - push_items);
3316 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3318 btrfs_mark_buffer_dirty(src);
3319 btrfs_mark_buffer_dirty(dst);
3325 * helper function to insert a new root level in the tree.
3326 * A new node is allocated, and a single item is inserted to
3327 * point to the existing root
3329 * returns zero on success or < 0 on failure.
3331 static noinline int insert_new_root(struct btrfs_trans_handle *trans,
3332 struct btrfs_root *root,
3333 struct btrfs_path *path, int level)
3335 struct btrfs_fs_info *fs_info = root->fs_info;
3337 struct extent_buffer *lower;
3338 struct extent_buffer *c;
3339 struct extent_buffer *old;
3340 struct btrfs_disk_key lower_key;
3343 BUG_ON(path->nodes[level]);
3344 BUG_ON(path->nodes[level-1] != root->node);
3346 lower = path->nodes[level-1];
3348 btrfs_item_key(lower, &lower_key, 0);
3350 btrfs_node_key(lower, &lower_key, 0);
3352 c = alloc_tree_block_no_bg_flush(trans, root, 0, &lower_key, level,
3353 root->node->start, 0);
3357 root_add_used(root, fs_info->nodesize);
3359 btrfs_set_header_nritems(c, 1);
3360 btrfs_set_node_key(c, &lower_key, 0);
3361 btrfs_set_node_blockptr(c, 0, lower->start);
3362 lower_gen = btrfs_header_generation(lower);
3363 WARN_ON(lower_gen != trans->transid);
3365 btrfs_set_node_ptr_generation(c, 0, lower_gen);
3367 btrfs_mark_buffer_dirty(c);
3370 ret = tree_mod_log_insert_root(root->node, c, 0);
3372 rcu_assign_pointer(root->node, c);
3374 /* the super has an extra ref to root->node */
3375 free_extent_buffer(old);
3377 add_root_to_dirty_list(root);
3378 atomic_inc(&c->refs);
3379 path->nodes[level] = c;
3380 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
3381 path->slots[level] = 0;
3386 * worker function to insert a single pointer in a node.
3387 * the node should have enough room for the pointer already
3389 * slot and level indicate where you want the key to go, and
3390 * blocknr is the block the key points to.
3392 static void insert_ptr(struct btrfs_trans_handle *trans,
3393 struct btrfs_path *path,
3394 struct btrfs_disk_key *key, u64 bytenr,
3395 int slot, int level)
3397 struct extent_buffer *lower;
3401 BUG_ON(!path->nodes[level]);
3402 btrfs_assert_tree_locked(path->nodes[level]);
3403 lower = path->nodes[level];
3404 nritems = btrfs_header_nritems(lower);
3405 BUG_ON(slot > nritems);
3406 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(trans->fs_info));
3407 if (slot != nritems) {
3409 ret = tree_mod_log_insert_move(lower, slot + 1, slot,
3413 memmove_extent_buffer(lower,
3414 btrfs_node_key_ptr_offset(slot + 1),
3415 btrfs_node_key_ptr_offset(slot),
3416 (nritems - slot) * sizeof(struct btrfs_key_ptr));
3419 ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD,
3423 btrfs_set_node_key(lower, key, slot);
3424 btrfs_set_node_blockptr(lower, slot, bytenr);
3425 WARN_ON(trans->transid == 0);
3426 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
3427 btrfs_set_header_nritems(lower, nritems + 1);
3428 btrfs_mark_buffer_dirty(lower);
3432 * split the node at the specified level in path in two.
3433 * The path is corrected to point to the appropriate node after the split
3435 * Before splitting this tries to make some room in the node by pushing
3436 * left and right, if either one works, it returns right away.
3438 * returns 0 on success and < 0 on failure
3440 static noinline int split_node(struct btrfs_trans_handle *trans,
3441 struct btrfs_root *root,
3442 struct btrfs_path *path, int level)
3444 struct btrfs_fs_info *fs_info = root->fs_info;
3445 struct extent_buffer *c;
3446 struct extent_buffer *split;
3447 struct btrfs_disk_key disk_key;
3452 c = path->nodes[level];
3453 WARN_ON(btrfs_header_generation(c) != trans->transid);
3454 if (c == root->node) {
3456 * trying to split the root, lets make a new one
3458 * tree mod log: We don't log_removal old root in
3459 * insert_new_root, because that root buffer will be kept as a
3460 * normal node. We are going to log removal of half of the
3461 * elements below with tree_mod_log_eb_copy. We're holding a
3462 * tree lock on the buffer, which is why we cannot race with
3463 * other tree_mod_log users.
3465 ret = insert_new_root(trans, root, path, level + 1);
3469 ret = push_nodes_for_insert(trans, root, path, level);
3470 c = path->nodes[level];
3471 if (!ret && btrfs_header_nritems(c) <
3472 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
3478 c_nritems = btrfs_header_nritems(c);
3479 mid = (c_nritems + 1) / 2;
3480 btrfs_node_key(c, &disk_key, mid);
3482 split = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, level,
3485 return PTR_ERR(split);
3487 root_add_used(root, fs_info->nodesize);
3488 ASSERT(btrfs_header_level(c) == level);
3490 ret = tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid);
3492 btrfs_abort_transaction(trans, ret);
3495 copy_extent_buffer(split, c,
3496 btrfs_node_key_ptr_offset(0),
3497 btrfs_node_key_ptr_offset(mid),
3498 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3499 btrfs_set_header_nritems(split, c_nritems - mid);
3500 btrfs_set_header_nritems(c, mid);
3503 btrfs_mark_buffer_dirty(c);
3504 btrfs_mark_buffer_dirty(split);
3506 insert_ptr(trans, path, &disk_key, split->start,
3507 path->slots[level + 1] + 1, level + 1);
3509 if (path->slots[level] >= mid) {
3510 path->slots[level] -= mid;
3511 btrfs_tree_unlock(c);
3512 free_extent_buffer(c);
3513 path->nodes[level] = split;
3514 path->slots[level + 1] += 1;
3516 btrfs_tree_unlock(split);
3517 free_extent_buffer(split);
3523 * how many bytes are required to store the items in a leaf. start
3524 * and nr indicate which items in the leaf to check. This totals up the
3525 * space used both by the item structs and the item data
3527 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
3529 struct btrfs_item *start_item;
3530 struct btrfs_item *end_item;
3531 struct btrfs_map_token token;
3533 int nritems = btrfs_header_nritems(l);
3534 int end = min(nritems, start + nr) - 1;
3538 btrfs_init_map_token(&token, l);
3539 start_item = btrfs_item_nr(start);
3540 end_item = btrfs_item_nr(end);
3541 data_len = btrfs_token_item_offset(l, start_item, &token) +
3542 btrfs_token_item_size(l, start_item, &token);
3543 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
3544 data_len += sizeof(struct btrfs_item) * nr;
3545 WARN_ON(data_len < 0);
3550 * The space between the end of the leaf items and
3551 * the start of the leaf data. IOW, how much room
3552 * the leaf has left for both items and data
3554 noinline int btrfs_leaf_free_space(struct extent_buffer *leaf)
3556 struct btrfs_fs_info *fs_info = leaf->fs_info;
3557 int nritems = btrfs_header_nritems(leaf);
3560 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
3563 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
3565 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
3566 leaf_space_used(leaf, 0, nritems), nritems);
3572 * min slot controls the lowest index we're willing to push to the
3573 * right. We'll push up to and including min_slot, but no lower
3575 static noinline int __push_leaf_right(struct btrfs_path *path,
3576 int data_size, int empty,
3577 struct extent_buffer *right,
3578 int free_space, u32 left_nritems,
3581 struct btrfs_fs_info *fs_info = right->fs_info;
3582 struct extent_buffer *left = path->nodes[0];
3583 struct extent_buffer *upper = path->nodes[1];
3584 struct btrfs_map_token token;
3585 struct btrfs_disk_key disk_key;
3590 struct btrfs_item *item;
3599 nr = max_t(u32, 1, min_slot);
3601 if (path->slots[0] >= left_nritems)
3602 push_space += data_size;
3604 slot = path->slots[1];
3605 i = left_nritems - 1;
3607 item = btrfs_item_nr(i);
3609 if (!empty && push_items > 0) {
3610 if (path->slots[0] > i)
3612 if (path->slots[0] == i) {
3613 int space = btrfs_leaf_free_space(left);
3615 if (space + push_space * 2 > free_space)
3620 if (path->slots[0] == i)
3621 push_space += data_size;
3623 this_item_size = btrfs_item_size(left, item);
3624 if (this_item_size + sizeof(*item) + push_space > free_space)
3628 push_space += this_item_size + sizeof(*item);
3634 if (push_items == 0)
3637 WARN_ON(!empty && push_items == left_nritems);
3639 /* push left to right */
3640 right_nritems = btrfs_header_nritems(right);
3642 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
3643 push_space -= leaf_data_end(left);
3645 /* make room in the right data area */
3646 data_end = leaf_data_end(right);
3647 memmove_extent_buffer(right,
3648 BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
3649 BTRFS_LEAF_DATA_OFFSET + data_end,
3650 BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
3652 /* copy from the left data area */
3653 copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
3654 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
3655 BTRFS_LEAF_DATA_OFFSET + leaf_data_end(left),
3658 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3659 btrfs_item_nr_offset(0),
3660 right_nritems * sizeof(struct btrfs_item));
3662 /* copy the items from left to right */
3663 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3664 btrfs_item_nr_offset(left_nritems - push_items),
3665 push_items * sizeof(struct btrfs_item));
3667 /* update the item pointers */
3668 btrfs_init_map_token(&token, right);
3669 right_nritems += push_items;
3670 btrfs_set_header_nritems(right, right_nritems);
3671 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
3672 for (i = 0; i < right_nritems; i++) {
3673 item = btrfs_item_nr(i);
3674 push_space -= btrfs_token_item_size(right, item, &token);
3675 btrfs_set_token_item_offset(right, item, push_space, &token);
3678 left_nritems -= push_items;
3679 btrfs_set_header_nritems(left, left_nritems);
3682 btrfs_mark_buffer_dirty(left);
3684 btrfs_clean_tree_block(left);
3686 btrfs_mark_buffer_dirty(right);
3688 btrfs_item_key(right, &disk_key, 0);
3689 btrfs_set_node_key(upper, &disk_key, slot + 1);
3690 btrfs_mark_buffer_dirty(upper);
3692 /* then fixup the leaf pointer in the path */
3693 if (path->slots[0] >= left_nritems) {
3694 path->slots[0] -= left_nritems;
3695 if (btrfs_header_nritems(path->nodes[0]) == 0)
3696 btrfs_clean_tree_block(path->nodes[0]);
3697 btrfs_tree_unlock(path->nodes[0]);
3698 free_extent_buffer(path->nodes[0]);
3699 path->nodes[0] = right;
3700 path->slots[1] += 1;
3702 btrfs_tree_unlock(right);
3703 free_extent_buffer(right);
3708 btrfs_tree_unlock(right);
3709 free_extent_buffer(right);
3714 * push some data in the path leaf to the right, trying to free up at
3715 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3717 * returns 1 if the push failed because the other node didn't have enough
3718 * room, 0 if everything worked out and < 0 if there were major errors.
3720 * this will push starting from min_slot to the end of the leaf. It won't
3721 * push any slot lower than min_slot
3723 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
3724 *root, struct btrfs_path *path,
3725 int min_data_size, int data_size,
3726 int empty, u32 min_slot)
3728 struct extent_buffer *left = path->nodes[0];
3729 struct extent_buffer *right;
3730 struct extent_buffer *upper;
3736 if (!path->nodes[1])
3739 slot = path->slots[1];
3740 upper = path->nodes[1];
3741 if (slot >= btrfs_header_nritems(upper) - 1)
3744 btrfs_assert_tree_locked(path->nodes[1]);
3746 right = btrfs_read_node_slot(upper, slot + 1);
3748 * slot + 1 is not valid or we fail to read the right node,
3749 * no big deal, just return.
3754 btrfs_tree_lock(right);
3755 btrfs_set_lock_blocking_write(right);
3757 free_space = btrfs_leaf_free_space(right);
3758 if (free_space < data_size)
3761 /* cow and double check */
3762 ret = btrfs_cow_block(trans, root, right, upper,
3767 free_space = btrfs_leaf_free_space(right);
3768 if (free_space < data_size)
3771 left_nritems = btrfs_header_nritems(left);
3772 if (left_nritems == 0)
3775 if (path->slots[0] == left_nritems && !empty) {
3776 /* Key greater than all keys in the leaf, right neighbor has
3777 * enough room for it and we're not emptying our leaf to delete
3778 * it, therefore use right neighbor to insert the new item and
3779 * no need to touch/dirty our left leaf. */
3780 btrfs_tree_unlock(left);
3781 free_extent_buffer(left);
3782 path->nodes[0] = right;
3788 return __push_leaf_right(path, min_data_size, empty,
3789 right, free_space, left_nritems, min_slot);
3791 btrfs_tree_unlock(right);
3792 free_extent_buffer(right);
3797 * push some data in the path leaf to the left, trying to free up at
3798 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3800 * max_slot can put a limit on how far into the leaf we'll push items. The
3801 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3804 static noinline int __push_leaf_left(struct btrfs_path *path, int data_size,
3805 int empty, struct extent_buffer *left,
3806 int free_space, u32 right_nritems,
3809 struct btrfs_fs_info *fs_info = left->fs_info;
3810 struct btrfs_disk_key disk_key;
3811 struct extent_buffer *right = path->nodes[0];
3815 struct btrfs_item *item;
3816 u32 old_left_nritems;
3820 u32 old_left_item_size;
3821 struct btrfs_map_token token;
3824 nr = min(right_nritems, max_slot);
3826 nr = min(right_nritems - 1, max_slot);
3828 for (i = 0; i < nr; i++) {
3829 item = btrfs_item_nr(i);
3831 if (!empty && push_items > 0) {
3832 if (path->slots[0] < i)
3834 if (path->slots[0] == i) {
3835 int space = btrfs_leaf_free_space(right);
3837 if (space + push_space * 2 > free_space)
3842 if (path->slots[0] == i)
3843 push_space += data_size;
3845 this_item_size = btrfs_item_size(right, item);
3846 if (this_item_size + sizeof(*item) + push_space > free_space)
3850 push_space += this_item_size + sizeof(*item);
3853 if (push_items == 0) {
3857 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
3859 /* push data from right to left */
3860 copy_extent_buffer(left, right,
3861 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3862 btrfs_item_nr_offset(0),
3863 push_items * sizeof(struct btrfs_item));
3865 push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
3866 btrfs_item_offset_nr(right, push_items - 1);
3868 copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
3869 leaf_data_end(left) - push_space,
3870 BTRFS_LEAF_DATA_OFFSET +
3871 btrfs_item_offset_nr(right, push_items - 1),
3873 old_left_nritems = btrfs_header_nritems(left);
3874 BUG_ON(old_left_nritems <= 0);
3876 btrfs_init_map_token(&token, left);
3877 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
3878 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
3881 item = btrfs_item_nr(i);
3883 ioff = btrfs_token_item_offset(left, item, &token);
3884 btrfs_set_token_item_offset(left, item,
3885 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size),
3888 btrfs_set_header_nritems(left, old_left_nritems + push_items);
3890 /* fixup right node */
3891 if (push_items > right_nritems)
3892 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
3895 if (push_items < right_nritems) {
3896 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3897 leaf_data_end(right);
3898 memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
3899 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
3900 BTRFS_LEAF_DATA_OFFSET +
3901 leaf_data_end(right), push_space);
3903 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
3904 btrfs_item_nr_offset(push_items),
3905 (btrfs_header_nritems(right) - push_items) *
3906 sizeof(struct btrfs_item));
3909 btrfs_init_map_token(&token, right);
3910 right_nritems -= push_items;
3911 btrfs_set_header_nritems(right, right_nritems);
3912 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
3913 for (i = 0; i < right_nritems; i++) {
3914 item = btrfs_item_nr(i);
3916 push_space = push_space - btrfs_token_item_size(right,
3918 btrfs_set_token_item_offset(right, item, push_space, &token);
3921 btrfs_mark_buffer_dirty(left);
3923 btrfs_mark_buffer_dirty(right);
3925 btrfs_clean_tree_block(right);
3927 btrfs_item_key(right, &disk_key, 0);
3928 fixup_low_keys(path, &disk_key, 1);
3930 /* then fixup the leaf pointer in the path */
3931 if (path->slots[0] < push_items) {
3932 path->slots[0] += old_left_nritems;
3933 btrfs_tree_unlock(path->nodes[0]);
3934 free_extent_buffer(path->nodes[0]);
3935 path->nodes[0] = left;
3936 path->slots[1] -= 1;
3938 btrfs_tree_unlock(left);
3939 free_extent_buffer(left);
3940 path->slots[0] -= push_items;
3942 BUG_ON(path->slots[0] < 0);
3945 btrfs_tree_unlock(left);
3946 free_extent_buffer(left);
3951 * push some data in the path leaf to the left, trying to free up at
3952 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3954 * max_slot can put a limit on how far into the leaf we'll push items. The
3955 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3958 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
3959 *root, struct btrfs_path *path, int min_data_size,
3960 int data_size, int empty, u32 max_slot)
3962 struct extent_buffer *right = path->nodes[0];
3963 struct extent_buffer *left;
3969 slot = path->slots[1];
3972 if (!path->nodes[1])
3975 right_nritems = btrfs_header_nritems(right);
3976 if (right_nritems == 0)
3979 btrfs_assert_tree_locked(path->nodes[1]);
3981 left = btrfs_read_node_slot(path->nodes[1], slot - 1);
3983 * slot - 1 is not valid or we fail to read the left node,
3984 * no big deal, just return.
3989 btrfs_tree_lock(left);
3990 btrfs_set_lock_blocking_write(left);
3992 free_space = btrfs_leaf_free_space(left);
3993 if (free_space < data_size) {
3998 /* cow and double check */
3999 ret = btrfs_cow_block(trans, root, left,
4000 path->nodes[1], slot - 1, &left);
4002 /* we hit -ENOSPC, but it isn't fatal here */
4008 free_space = btrfs_leaf_free_space(left);
4009 if (free_space < data_size) {
4014 return __push_leaf_left(path, min_data_size,
4015 empty, left, free_space, right_nritems,
4018 btrfs_tree_unlock(left);
4019 free_extent_buffer(left);
4024 * split the path's leaf in two, making sure there is at least data_size
4025 * available for the resulting leaf level of the path.
4027 static noinline void copy_for_split(struct btrfs_trans_handle *trans,
4028 struct btrfs_path *path,
4029 struct extent_buffer *l,
4030 struct extent_buffer *right,
4031 int slot, int mid, int nritems)
4033 struct btrfs_fs_info *fs_info = trans->fs_info;
4037 struct btrfs_disk_key disk_key;
4038 struct btrfs_map_token token;
4040 nritems = nritems - mid;
4041 btrfs_set_header_nritems(right, nritems);
4042 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(l);
4044 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4045 btrfs_item_nr_offset(mid),
4046 nritems * sizeof(struct btrfs_item));
4048 copy_extent_buffer(right, l,
4049 BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
4050 data_copy_size, BTRFS_LEAF_DATA_OFFSET +
4051 leaf_data_end(l), data_copy_size);
4053 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
4055 btrfs_init_map_token(&token, right);
4056 for (i = 0; i < nritems; i++) {
4057 struct btrfs_item *item = btrfs_item_nr(i);
4060 ioff = btrfs_token_item_offset(right, item, &token);
4061 btrfs_set_token_item_offset(right, item,
4062 ioff + rt_data_off, &token);
4065 btrfs_set_header_nritems(l, mid);
4066 btrfs_item_key(right, &disk_key, 0);
4067 insert_ptr(trans, path, &disk_key, right->start, path->slots[1] + 1, 1);
4069 btrfs_mark_buffer_dirty(right);
4070 btrfs_mark_buffer_dirty(l);
4071 BUG_ON(path->slots[0] != slot);
4074 btrfs_tree_unlock(path->nodes[0]);
4075 free_extent_buffer(path->nodes[0]);
4076 path->nodes[0] = right;
4077 path->slots[0] -= mid;
4078 path->slots[1] += 1;
4080 btrfs_tree_unlock(right);
4081 free_extent_buffer(right);
4084 BUG_ON(path->slots[0] < 0);
4088 * double splits happen when we need to insert a big item in the middle
4089 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4090 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4093 * We avoid this by trying to push the items on either side of our target
4094 * into the adjacent leaves. If all goes well we can avoid the double split
4097 static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4098 struct btrfs_root *root,
4099 struct btrfs_path *path,
4106 int space_needed = data_size;
4108 slot = path->slots[0];
4109 if (slot < btrfs_header_nritems(path->nodes[0]))
4110 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
4113 * try to push all the items after our slot into the
4116 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
4123 nritems = btrfs_header_nritems(path->nodes[0]);
4125 * our goal is to get our slot at the start or end of a leaf. If
4126 * we've done so we're done
4128 if (path->slots[0] == 0 || path->slots[0] == nritems)
4131 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
4134 /* try to push all the items before our slot into the next leaf */
4135 slot = path->slots[0];
4136 space_needed = data_size;
4138 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
4139 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
4152 * split the path's leaf in two, making sure there is at least data_size
4153 * available for the resulting leaf level of the path.
4155 * returns 0 if all went well and < 0 on failure.
4157 static noinline int split_leaf(struct btrfs_trans_handle *trans,
4158 struct btrfs_root *root,
4159 const struct btrfs_key *ins_key,
4160 struct btrfs_path *path, int data_size,
4163 struct btrfs_disk_key disk_key;
4164 struct extent_buffer *l;
4168 struct extent_buffer *right;
4169 struct btrfs_fs_info *fs_info = root->fs_info;
4173 int num_doubles = 0;
4174 int tried_avoid_double = 0;
4177 slot = path->slots[0];
4178 if (extend && data_size + btrfs_item_size_nr(l, slot) +
4179 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
4182 /* first try to make some room by pushing left and right */
4183 if (data_size && path->nodes[1]) {
4184 int space_needed = data_size;
4186 if (slot < btrfs_header_nritems(l))
4187 space_needed -= btrfs_leaf_free_space(l);
4189 wret = push_leaf_right(trans, root, path, space_needed,
4190 space_needed, 0, 0);
4194 space_needed = data_size;
4196 space_needed -= btrfs_leaf_free_space(l);
4197 wret = push_leaf_left(trans, root, path, space_needed,
4198 space_needed, 0, (u32)-1);
4204 /* did the pushes work? */
4205 if (btrfs_leaf_free_space(l) >= data_size)
4209 if (!path->nodes[1]) {
4210 ret = insert_new_root(trans, root, path, 1);
4217 slot = path->slots[0];
4218 nritems = btrfs_header_nritems(l);
4219 mid = (nritems + 1) / 2;
4223 leaf_space_used(l, mid, nritems - mid) + data_size >
4224 BTRFS_LEAF_DATA_SIZE(fs_info)) {
4225 if (slot >= nritems) {
4229 if (mid != nritems &&
4230 leaf_space_used(l, mid, nritems - mid) +
4231 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
4232 if (data_size && !tried_avoid_double)
4233 goto push_for_double;
4239 if (leaf_space_used(l, 0, mid) + data_size >
4240 BTRFS_LEAF_DATA_SIZE(fs_info)) {
4241 if (!extend && data_size && slot == 0) {
4243 } else if ((extend || !data_size) && slot == 0) {
4247 if (mid != nritems &&
4248 leaf_space_used(l, mid, nritems - mid) +
4249 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
4250 if (data_size && !tried_avoid_double)
4251 goto push_for_double;
4259 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4261 btrfs_item_key(l, &disk_key, mid);
4263 right = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, 0,
4266 return PTR_ERR(right);
4268 root_add_used(root, fs_info->nodesize);
4272 btrfs_set_header_nritems(right, 0);
4273 insert_ptr(trans, path, &disk_key,
4274 right->start, path->slots[1] + 1, 1);
4275 btrfs_tree_unlock(path->nodes[0]);
4276 free_extent_buffer(path->nodes[0]);
4277 path->nodes[0] = right;
4279 path->slots[1] += 1;
4281 btrfs_set_header_nritems(right, 0);
4282 insert_ptr(trans, path, &disk_key,
4283 right->start, path->slots[1], 1);
4284 btrfs_tree_unlock(path->nodes[0]);
4285 free_extent_buffer(path->nodes[0]);
4286 path->nodes[0] = right;
4288 if (path->slots[1] == 0)
4289 fixup_low_keys(path, &disk_key, 1);
4292 * We create a new leaf 'right' for the required ins_len and
4293 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
4294 * the content of ins_len to 'right'.
4299 copy_for_split(trans, path, l, right, slot, mid, nritems);
4302 BUG_ON(num_doubles != 0);
4310 push_for_double_split(trans, root, path, data_size);
4311 tried_avoid_double = 1;
4312 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
4317 static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4318 struct btrfs_root *root,
4319 struct btrfs_path *path, int ins_len)
4321 struct btrfs_key key;
4322 struct extent_buffer *leaf;
4323 struct btrfs_file_extent_item *fi;
4328 leaf = path->nodes[0];
4329 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4331 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4332 key.type != BTRFS_EXTENT_CSUM_KEY);
4334 if (btrfs_leaf_free_space(leaf) >= ins_len)
4337 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4338 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4339 fi = btrfs_item_ptr(leaf, path->slots[0],
4340 struct btrfs_file_extent_item);
4341 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4343 btrfs_release_path(path);
4345 path->keep_locks = 1;
4346 path->search_for_split = 1;
4347 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4348 path->search_for_split = 0;
4355 leaf = path->nodes[0];
4356 /* if our item isn't there, return now */
4357 if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4360 /* the leaf has changed, it now has room. return now */
4361 if (btrfs_leaf_free_space(path->nodes[0]) >= ins_len)
4364 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4365 fi = btrfs_item_ptr(leaf, path->slots[0],
4366 struct btrfs_file_extent_item);
4367 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4371 btrfs_set_path_blocking(path);
4372 ret = split_leaf(trans, root, &key, path, ins_len, 1);
4376 path->keep_locks = 0;
4377 btrfs_unlock_up_safe(path, 1);
4380 path->keep_locks = 0;
4384 static noinline int split_item(struct btrfs_path *path,
4385 const struct btrfs_key *new_key,
4386 unsigned long split_offset)
4388 struct extent_buffer *leaf;
4389 struct btrfs_item *item;
4390 struct btrfs_item *new_item;
4396 struct btrfs_disk_key disk_key;
4398 leaf = path->nodes[0];
4399 BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item));
4401 btrfs_set_path_blocking(path);
4403 item = btrfs_item_nr(path->slots[0]);
4404 orig_offset = btrfs_item_offset(leaf, item);
4405 item_size = btrfs_item_size(leaf, item);
4407 buf = kmalloc(item_size, GFP_NOFS);
4411 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4412 path->slots[0]), item_size);
4414 slot = path->slots[0] + 1;
4415 nritems = btrfs_header_nritems(leaf);
4416 if (slot != nritems) {
4417 /* shift the items */
4418 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
4419 btrfs_item_nr_offset(slot),
4420 (nritems - slot) * sizeof(struct btrfs_item));
4423 btrfs_cpu_key_to_disk(&disk_key, new_key);
4424 btrfs_set_item_key(leaf, &disk_key, slot);
4426 new_item = btrfs_item_nr(slot);
4428 btrfs_set_item_offset(leaf, new_item, orig_offset);
4429 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4431 btrfs_set_item_offset(leaf, item,
4432 orig_offset + item_size - split_offset);
4433 btrfs_set_item_size(leaf, item, split_offset);
4435 btrfs_set_header_nritems(leaf, nritems + 1);
4437 /* write the data for the start of the original item */
4438 write_extent_buffer(leaf, buf,
4439 btrfs_item_ptr_offset(leaf, path->slots[0]),
4442 /* write the data for the new item */
4443 write_extent_buffer(leaf, buf + split_offset,
4444 btrfs_item_ptr_offset(leaf, slot),
4445 item_size - split_offset);
4446 btrfs_mark_buffer_dirty(leaf);
4448 BUG_ON(btrfs_leaf_free_space(leaf) < 0);
4454 * This function splits a single item into two items,
4455 * giving 'new_key' to the new item and splitting the
4456 * old one at split_offset (from the start of the item).
4458 * The path may be released by this operation. After
4459 * the split, the path is pointing to the old item. The
4460 * new item is going to be in the same node as the old one.
4462 * Note, the item being split must be smaller enough to live alone on
4463 * a tree block with room for one extra struct btrfs_item
4465 * This allows us to split the item in place, keeping a lock on the
4466 * leaf the entire time.
4468 int btrfs_split_item(struct btrfs_trans_handle *trans,
4469 struct btrfs_root *root,
4470 struct btrfs_path *path,
4471 const struct btrfs_key *new_key,
4472 unsigned long split_offset)
4475 ret = setup_leaf_for_split(trans, root, path,
4476 sizeof(struct btrfs_item));
4480 ret = split_item(path, new_key, split_offset);
4485 * This function duplicate a item, giving 'new_key' to the new item.
4486 * It guarantees both items live in the same tree leaf and the new item
4487 * is contiguous with the original item.
4489 * This allows us to split file extent in place, keeping a lock on the
4490 * leaf the entire time.
4492 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4493 struct btrfs_root *root,
4494 struct btrfs_path *path,
4495 const struct btrfs_key *new_key)
4497 struct extent_buffer *leaf;
4501 leaf = path->nodes[0];
4502 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4503 ret = setup_leaf_for_split(trans, root, path,
4504 item_size + sizeof(struct btrfs_item));
4509 setup_items_for_insert(root, path, new_key, &item_size,
4510 item_size, item_size +
4511 sizeof(struct btrfs_item), 1);
4512 leaf = path->nodes[0];
4513 memcpy_extent_buffer(leaf,
4514 btrfs_item_ptr_offset(leaf, path->slots[0]),
4515 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4521 * make the item pointed to by the path smaller. new_size indicates
4522 * how small to make it, and from_end tells us if we just chop bytes
4523 * off the end of the item or if we shift the item to chop bytes off
4526 void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end)
4529 struct extent_buffer *leaf;
4530 struct btrfs_item *item;
4532 unsigned int data_end;
4533 unsigned int old_data_start;
4534 unsigned int old_size;
4535 unsigned int size_diff;
4537 struct btrfs_map_token token;
4539 leaf = path->nodes[0];
4540 slot = path->slots[0];
4542 old_size = btrfs_item_size_nr(leaf, slot);
4543 if (old_size == new_size)
4546 nritems = btrfs_header_nritems(leaf);
4547 data_end = leaf_data_end(leaf);
4549 old_data_start = btrfs_item_offset_nr(leaf, slot);
4551 size_diff = old_size - new_size;
4554 BUG_ON(slot >= nritems);
4557 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4559 /* first correct the data pointers */
4560 btrfs_init_map_token(&token, leaf);
4561 for (i = slot; i < nritems; i++) {
4563 item = btrfs_item_nr(i);
4565 ioff = btrfs_token_item_offset(leaf, item, &token);
4566 btrfs_set_token_item_offset(leaf, item,
4567 ioff + size_diff, &token);
4570 /* shift the data */
4572 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4573 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
4574 data_end, old_data_start + new_size - data_end);
4576 struct btrfs_disk_key disk_key;
4579 btrfs_item_key(leaf, &disk_key, slot);
4581 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4583 struct btrfs_file_extent_item *fi;
4585 fi = btrfs_item_ptr(leaf, slot,
4586 struct btrfs_file_extent_item);
4587 fi = (struct btrfs_file_extent_item *)(
4588 (unsigned long)fi - size_diff);
4590 if (btrfs_file_extent_type(leaf, fi) ==
4591 BTRFS_FILE_EXTENT_INLINE) {
4592 ptr = btrfs_item_ptr_offset(leaf, slot);
4593 memmove_extent_buffer(leaf, ptr,
4595 BTRFS_FILE_EXTENT_INLINE_DATA_START);
4599 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4600 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
4601 data_end, old_data_start - data_end);
4603 offset = btrfs_disk_key_offset(&disk_key);
4604 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4605 btrfs_set_item_key(leaf, &disk_key, slot);
4607 fixup_low_keys(path, &disk_key, 1);
4610 item = btrfs_item_nr(slot);
4611 btrfs_set_item_size(leaf, item, new_size);
4612 btrfs_mark_buffer_dirty(leaf);
4614 if (btrfs_leaf_free_space(leaf) < 0) {
4615 btrfs_print_leaf(leaf);
4621 * make the item pointed to by the path bigger, data_size is the added size.
4623 void btrfs_extend_item(struct btrfs_path *path, u32 data_size)
4626 struct extent_buffer *leaf;
4627 struct btrfs_item *item;
4629 unsigned int data_end;
4630 unsigned int old_data;
4631 unsigned int old_size;
4633 struct btrfs_map_token token;
4635 leaf = path->nodes[0];
4637 nritems = btrfs_header_nritems(leaf);
4638 data_end = leaf_data_end(leaf);
4640 if (btrfs_leaf_free_space(leaf) < data_size) {
4641 btrfs_print_leaf(leaf);
4644 slot = path->slots[0];
4645 old_data = btrfs_item_end_nr(leaf, slot);
4648 if (slot >= nritems) {
4649 btrfs_print_leaf(leaf);
4650 btrfs_crit(leaf->fs_info, "slot %d too large, nritems %d",
4656 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4658 /* first correct the data pointers */
4659 btrfs_init_map_token(&token, leaf);
4660 for (i = slot; i < nritems; i++) {
4662 item = btrfs_item_nr(i);
4664 ioff = btrfs_token_item_offset(leaf, item, &token);
4665 btrfs_set_token_item_offset(leaf, item,
4666 ioff - data_size, &token);
4669 /* shift the data */
4670 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4671 data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
4672 data_end, old_data - data_end);
4674 data_end = old_data;
4675 old_size = btrfs_item_size_nr(leaf, slot);
4676 item = btrfs_item_nr(slot);
4677 btrfs_set_item_size(leaf, item, old_size + data_size);
4678 btrfs_mark_buffer_dirty(leaf);
4680 if (btrfs_leaf_free_space(leaf) < 0) {
4681 btrfs_print_leaf(leaf);
4687 * this is a helper for btrfs_insert_empty_items, the main goal here is
4688 * to save stack depth by doing the bulk of the work in a function
4689 * that doesn't call btrfs_search_slot
4691 void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
4692 const struct btrfs_key *cpu_key, u32 *data_size,
4693 u32 total_data, u32 total_size, int nr)
4695 struct btrfs_fs_info *fs_info = root->fs_info;
4696 struct btrfs_item *item;
4699 unsigned int data_end;
4700 struct btrfs_disk_key disk_key;
4701 struct extent_buffer *leaf;
4703 struct btrfs_map_token token;
4705 if (path->slots[0] == 0) {
4706 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
4707 fixup_low_keys(path, &disk_key, 1);
4709 btrfs_unlock_up_safe(path, 1);
4711 leaf = path->nodes[0];
4712 slot = path->slots[0];
4714 nritems = btrfs_header_nritems(leaf);
4715 data_end = leaf_data_end(leaf);
4717 if (btrfs_leaf_free_space(leaf) < total_size) {
4718 btrfs_print_leaf(leaf);
4719 btrfs_crit(fs_info, "not enough freespace need %u have %d",
4720 total_size, btrfs_leaf_free_space(leaf));
4724 btrfs_init_map_token(&token, leaf);
4725 if (slot != nritems) {
4726 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4728 if (old_data < data_end) {
4729 btrfs_print_leaf(leaf);
4730 btrfs_crit(fs_info, "slot %d old_data %d data_end %d",
4731 slot, old_data, data_end);
4735 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4737 /* first correct the data pointers */
4738 for (i = slot; i < nritems; i++) {
4741 item = btrfs_item_nr(i);
4742 ioff = btrfs_token_item_offset(leaf, item, &token);
4743 btrfs_set_token_item_offset(leaf, item,
4744 ioff - total_data, &token);
4746 /* shift the items */
4747 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4748 btrfs_item_nr_offset(slot),
4749 (nritems - slot) * sizeof(struct btrfs_item));
4751 /* shift the data */
4752 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4753 data_end - total_data, BTRFS_LEAF_DATA_OFFSET +
4754 data_end, old_data - data_end);
4755 data_end = old_data;
4758 /* setup the item for the new data */
4759 for (i = 0; i < nr; i++) {
4760 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4761 btrfs_set_item_key(leaf, &disk_key, slot + i);
4762 item = btrfs_item_nr(slot + i);
4763 btrfs_set_token_item_offset(leaf, item,
4764 data_end - data_size[i], &token);
4765 data_end -= data_size[i];
4766 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
4769 btrfs_set_header_nritems(leaf, nritems + nr);
4770 btrfs_mark_buffer_dirty(leaf);
4772 if (btrfs_leaf_free_space(leaf) < 0) {
4773 btrfs_print_leaf(leaf);
4779 * Given a key and some data, insert items into the tree.
4780 * This does all the path init required, making room in the tree if needed.
4782 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4783 struct btrfs_root *root,
4784 struct btrfs_path *path,
4785 const struct btrfs_key *cpu_key, u32 *data_size,
4794 for (i = 0; i < nr; i++)
4795 total_data += data_size[i];
4797 total_size = total_data + (nr * sizeof(struct btrfs_item));
4798 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4804 slot = path->slots[0];
4807 setup_items_for_insert(root, path, cpu_key, data_size,
4808 total_data, total_size, nr);
4813 * Given a key and some data, insert an item into the tree.
4814 * This does all the path init required, making room in the tree if needed.
4816 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4817 const struct btrfs_key *cpu_key, void *data,
4821 struct btrfs_path *path;
4822 struct extent_buffer *leaf;
4825 path = btrfs_alloc_path();
4828 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
4830 leaf = path->nodes[0];
4831 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4832 write_extent_buffer(leaf, data, ptr, data_size);
4833 btrfs_mark_buffer_dirty(leaf);
4835 btrfs_free_path(path);
4840 * delete the pointer from a given node.
4842 * the tree should have been previously balanced so the deletion does not
4845 static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4846 int level, int slot)
4848 struct extent_buffer *parent = path->nodes[level];
4852 nritems = btrfs_header_nritems(parent);
4853 if (slot != nritems - 1) {
4855 ret = tree_mod_log_insert_move(parent, slot, slot + 1,
4856 nritems - slot - 1);
4859 memmove_extent_buffer(parent,
4860 btrfs_node_key_ptr_offset(slot),
4861 btrfs_node_key_ptr_offset(slot + 1),
4862 sizeof(struct btrfs_key_ptr) *
4863 (nritems - slot - 1));
4865 ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE,
4871 btrfs_set_header_nritems(parent, nritems);
4872 if (nritems == 0 && parent == root->node) {
4873 BUG_ON(btrfs_header_level(root->node) != 1);
4874 /* just turn the root into a leaf and break */
4875 btrfs_set_header_level(root->node, 0);
4876 } else if (slot == 0) {
4877 struct btrfs_disk_key disk_key;
4879 btrfs_node_key(parent, &disk_key, 0);
4880 fixup_low_keys(path, &disk_key, level + 1);
4882 btrfs_mark_buffer_dirty(parent);
4886 * a helper function to delete the leaf pointed to by path->slots[1] and
4889 * This deletes the pointer in path->nodes[1] and frees the leaf
4890 * block extent. zero is returned if it all worked out, < 0 otherwise.
4892 * The path must have already been setup for deleting the leaf, including
4893 * all the proper balancing. path->nodes[1] must be locked.
4895 static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4896 struct btrfs_root *root,
4897 struct btrfs_path *path,
4898 struct extent_buffer *leaf)
4900 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
4901 del_ptr(root, path, 1, path->slots[1]);
4904 * btrfs_free_extent is expensive, we want to make sure we
4905 * aren't holding any locks when we call it
4907 btrfs_unlock_up_safe(path, 0);
4909 root_sub_used(root, leaf->len);
4911 atomic_inc(&leaf->refs);
4912 btrfs_free_tree_block(trans, root, leaf, 0, 1);
4913 free_extent_buffer_stale(leaf);
4916 * delete the item at the leaf level in path. If that empties
4917 * the leaf, remove it from the tree
4919 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4920 struct btrfs_path *path, int slot, int nr)
4922 struct btrfs_fs_info *fs_info = root->fs_info;
4923 struct extent_buffer *leaf;
4924 struct btrfs_item *item;
4932 leaf = path->nodes[0];
4933 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4935 for (i = 0; i < nr; i++)
4936 dsize += btrfs_item_size_nr(leaf, slot + i);
4938 nritems = btrfs_header_nritems(leaf);
4940 if (slot + nr != nritems) {
4941 int data_end = leaf_data_end(leaf);
4942 struct btrfs_map_token token;
4944 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4946 BTRFS_LEAF_DATA_OFFSET + data_end,
4947 last_off - data_end);
4949 btrfs_init_map_token(&token, leaf);
4950 for (i = slot + nr; i < nritems; i++) {
4953 item = btrfs_item_nr(i);
4954 ioff = btrfs_token_item_offset(leaf, item, &token);
4955 btrfs_set_token_item_offset(leaf, item,
4956 ioff + dsize, &token);
4959 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
4960 btrfs_item_nr_offset(slot + nr),
4961 sizeof(struct btrfs_item) *
4962 (nritems - slot - nr));
4964 btrfs_set_header_nritems(leaf, nritems - nr);
4967 /* delete the leaf if we've emptied it */
4969 if (leaf == root->node) {
4970 btrfs_set_header_level(leaf, 0);
4972 btrfs_set_path_blocking(path);
4973 btrfs_clean_tree_block(leaf);
4974 btrfs_del_leaf(trans, root, path, leaf);
4977 int used = leaf_space_used(leaf, 0, nritems);
4979 struct btrfs_disk_key disk_key;
4981 btrfs_item_key(leaf, &disk_key, 0);
4982 fixup_low_keys(path, &disk_key, 1);
4985 /* delete the leaf if it is mostly empty */
4986 if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
4987 /* push_leaf_left fixes the path.
4988 * make sure the path still points to our leaf
4989 * for possible call to del_ptr below
4991 slot = path->slots[1];
4992 atomic_inc(&leaf->refs);
4994 btrfs_set_path_blocking(path);
4995 wret = push_leaf_left(trans, root, path, 1, 1,
4997 if (wret < 0 && wret != -ENOSPC)
5000 if (path->nodes[0] == leaf &&
5001 btrfs_header_nritems(leaf)) {
5002 wret = push_leaf_right(trans, root, path, 1,
5004 if (wret < 0 && wret != -ENOSPC)
5008 if (btrfs_header_nritems(leaf) == 0) {
5009 path->slots[1] = slot;
5010 btrfs_del_leaf(trans, root, path, leaf);
5011 free_extent_buffer(leaf);
5014 /* if we're still in the path, make sure
5015 * we're dirty. Otherwise, one of the
5016 * push_leaf functions must have already
5017 * dirtied this buffer
5019 if (path->nodes[0] == leaf)
5020 btrfs_mark_buffer_dirty(leaf);
5021 free_extent_buffer(leaf);
5024 btrfs_mark_buffer_dirty(leaf);
5031 * search the tree again to find a leaf with lesser keys
5032 * returns 0 if it found something or 1 if there are no lesser leaves.
5033 * returns < 0 on io errors.
5035 * This may release the path, and so you may lose any locks held at the
5038 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
5040 struct btrfs_key key;
5041 struct btrfs_disk_key found_key;
5044 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
5046 if (key.offset > 0) {
5048 } else if (key.type > 0) {
5050 key.offset = (u64)-1;
5051 } else if (key.objectid > 0) {
5054 key.offset = (u64)-1;
5059 btrfs_release_path(path);
5060 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5063 btrfs_item_key(path->nodes[0], &found_key, 0);
5064 ret = comp_keys(&found_key, &key);
5066 * We might have had an item with the previous key in the tree right
5067 * before we released our path. And after we released our path, that
5068 * item might have been pushed to the first slot (0) of the leaf we
5069 * were holding due to a tree balance. Alternatively, an item with the
5070 * previous key can exist as the only element of a leaf (big fat item).
5071 * Therefore account for these 2 cases, so that our callers (like
5072 * btrfs_previous_item) don't miss an existing item with a key matching
5073 * the previous key we computed above.
5081 * A helper function to walk down the tree starting at min_key, and looking
5082 * for nodes or leaves that are have a minimum transaction id.
5083 * This is used by the btree defrag code, and tree logging
5085 * This does not cow, but it does stuff the starting key it finds back
5086 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5087 * key and get a writable path.
5089 * This honors path->lowest_level to prevent descent past a given level
5092 * min_trans indicates the oldest transaction that you are interested
5093 * in walking through. Any nodes or leaves older than min_trans are
5094 * skipped over (without reading them).
5096 * returns zero if something useful was found, < 0 on error and 1 if there
5097 * was nothing in the tree that matched the search criteria.
5099 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
5100 struct btrfs_path *path,
5103 struct extent_buffer *cur;
5104 struct btrfs_key found_key;
5110 int keep_locks = path->keep_locks;
5112 path->keep_locks = 1;
5114 cur = btrfs_read_lock_root_node(root);
5115 level = btrfs_header_level(cur);
5116 WARN_ON(path->nodes[level]);
5117 path->nodes[level] = cur;
5118 path->locks[level] = BTRFS_READ_LOCK;
5120 if (btrfs_header_generation(cur) < min_trans) {
5125 nritems = btrfs_header_nritems(cur);
5126 level = btrfs_header_level(cur);
5127 sret = btrfs_bin_search(cur, min_key, level, &slot);
5133 /* at the lowest level, we're done, setup the path and exit */
5134 if (level == path->lowest_level) {
5135 if (slot >= nritems)
5138 path->slots[level] = slot;
5139 btrfs_item_key_to_cpu(cur, &found_key, slot);
5142 if (sret && slot > 0)
5145 * check this node pointer against the min_trans parameters.
5146 * If it is too old, old, skip to the next one.
5148 while (slot < nritems) {
5151 gen = btrfs_node_ptr_generation(cur, slot);
5152 if (gen < min_trans) {
5160 * we didn't find a candidate key in this node, walk forward
5161 * and find another one
5163 if (slot >= nritems) {
5164 path->slots[level] = slot;
5165 btrfs_set_path_blocking(path);
5166 sret = btrfs_find_next_key(root, path, min_key, level,
5169 btrfs_release_path(path);
5175 /* save our key for returning back */
5176 btrfs_node_key_to_cpu(cur, &found_key, slot);
5177 path->slots[level] = slot;
5178 if (level == path->lowest_level) {
5182 btrfs_set_path_blocking(path);
5183 cur = btrfs_read_node_slot(cur, slot);
5189 btrfs_tree_read_lock(cur);
5191 path->locks[level - 1] = BTRFS_READ_LOCK;
5192 path->nodes[level - 1] = cur;
5193 unlock_up(path, level, 1, 0, NULL);
5196 path->keep_locks = keep_locks;
5198 btrfs_unlock_up_safe(path, path->lowest_level + 1);
5199 btrfs_set_path_blocking(path);
5200 memcpy(min_key, &found_key, sizeof(found_key));
5206 * this is similar to btrfs_next_leaf, but does not try to preserve
5207 * and fixup the path. It looks for and returns the next key in the
5208 * tree based on the current path and the min_trans parameters.
5210 * 0 is returned if another key is found, < 0 if there are any errors
5211 * and 1 is returned if there are no higher keys in the tree
5213 * path->keep_locks should be set to 1 on the search made before
5214 * calling this function.
5216 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
5217 struct btrfs_key *key, int level, u64 min_trans)
5220 struct extent_buffer *c;
5222 WARN_ON(!path->keep_locks && !path->skip_locking);
5223 while (level < BTRFS_MAX_LEVEL) {
5224 if (!path->nodes[level])
5227 slot = path->slots[level] + 1;
5228 c = path->nodes[level];
5230 if (slot >= btrfs_header_nritems(c)) {
5233 struct btrfs_key cur_key;
5234 if (level + 1 >= BTRFS_MAX_LEVEL ||
5235 !path->nodes[level + 1])
5238 if (path->locks[level + 1] || path->skip_locking) {
5243 slot = btrfs_header_nritems(c) - 1;
5245 btrfs_item_key_to_cpu(c, &cur_key, slot);
5247 btrfs_node_key_to_cpu(c, &cur_key, slot);
5249 orig_lowest = path->lowest_level;
5250 btrfs_release_path(path);
5251 path->lowest_level = level;
5252 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5254 path->lowest_level = orig_lowest;
5258 c = path->nodes[level];
5259 slot = path->slots[level];
5266 btrfs_item_key_to_cpu(c, key, slot);
5268 u64 gen = btrfs_node_ptr_generation(c, slot);
5270 if (gen < min_trans) {
5274 btrfs_node_key_to_cpu(c, key, slot);
5282 * search the tree again to find a leaf with greater keys
5283 * returns 0 if it found something or 1 if there are no greater leaves.
5284 * returns < 0 on io errors.
5286 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
5288 return btrfs_next_old_leaf(root, path, 0);
5291 int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5296 struct extent_buffer *c;
5297 struct extent_buffer *next;
5298 struct btrfs_key key;
5301 int old_spinning = path->leave_spinning;
5302 int next_rw_lock = 0;
5304 nritems = btrfs_header_nritems(path->nodes[0]);
5308 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5313 btrfs_release_path(path);
5315 path->keep_locks = 1;
5316 path->leave_spinning = 1;
5319 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5321 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5322 path->keep_locks = 0;
5327 nritems = btrfs_header_nritems(path->nodes[0]);
5329 * by releasing the path above we dropped all our locks. A balance
5330 * could have added more items next to the key that used to be
5331 * at the very end of the block. So, check again here and
5332 * advance the path if there are now more items available.
5334 if (nritems > 0 && path->slots[0] < nritems - 1) {
5341 * So the above check misses one case:
5342 * - after releasing the path above, someone has removed the item that
5343 * used to be at the very end of the block, and balance between leafs
5344 * gets another one with bigger key.offset to replace it.
5346 * This one should be returned as well, or we can get leaf corruption
5347 * later(esp. in __btrfs_drop_extents()).
5349 * And a bit more explanation about this check,
5350 * with ret > 0, the key isn't found, the path points to the slot
5351 * where it should be inserted, so the path->slots[0] item must be the
5354 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
5359 while (level < BTRFS_MAX_LEVEL) {
5360 if (!path->nodes[level]) {
5365 slot = path->slots[level] + 1;
5366 c = path->nodes[level];
5367 if (slot >= btrfs_header_nritems(c)) {
5369 if (level == BTRFS_MAX_LEVEL) {
5377 btrfs_tree_unlock_rw(next, next_rw_lock);
5378 free_extent_buffer(next);
5382 next_rw_lock = path->locks[level];
5383 ret = read_block_for_search(root, path, &next, level,
5389 btrfs_release_path(path);
5393 if (!path->skip_locking) {
5394 ret = btrfs_try_tree_read_lock(next);
5395 if (!ret && time_seq) {
5397 * If we don't get the lock, we may be racing
5398 * with push_leaf_left, holding that lock while
5399 * itself waiting for the leaf we've currently
5400 * locked. To solve this situation, we give up
5401 * on our lock and cycle.
5403 free_extent_buffer(next);
5404 btrfs_release_path(path);
5409 btrfs_set_path_blocking(path);
5410 btrfs_tree_read_lock(next);
5412 next_rw_lock = BTRFS_READ_LOCK;
5416 path->slots[level] = slot;
5419 c = path->nodes[level];
5420 if (path->locks[level])
5421 btrfs_tree_unlock_rw(c, path->locks[level]);
5423 free_extent_buffer(c);
5424 path->nodes[level] = next;
5425 path->slots[level] = 0;
5426 if (!path->skip_locking)
5427 path->locks[level] = next_rw_lock;
5431 ret = read_block_for_search(root, path, &next, level,
5437 btrfs_release_path(path);
5441 if (!path->skip_locking) {
5442 ret = btrfs_try_tree_read_lock(next);
5444 btrfs_set_path_blocking(path);
5445 btrfs_tree_read_lock(next);
5447 next_rw_lock = BTRFS_READ_LOCK;
5452 unlock_up(path, 0, 1, 0, NULL);
5453 path->leave_spinning = old_spinning;
5455 btrfs_set_path_blocking(path);
5461 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5462 * searching until it gets past min_objectid or finds an item of 'type'
5464 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5466 int btrfs_previous_item(struct btrfs_root *root,
5467 struct btrfs_path *path, u64 min_objectid,
5470 struct btrfs_key found_key;
5471 struct extent_buffer *leaf;
5476 if (path->slots[0] == 0) {
5477 btrfs_set_path_blocking(path);
5478 ret = btrfs_prev_leaf(root, path);
5484 leaf = path->nodes[0];
5485 nritems = btrfs_header_nritems(leaf);
5488 if (path->slots[0] == nritems)
5491 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5492 if (found_key.objectid < min_objectid)
5494 if (found_key.type == type)
5496 if (found_key.objectid == min_objectid &&
5497 found_key.type < type)
5504 * search in extent tree to find a previous Metadata/Data extent item with
5507 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5509 int btrfs_previous_extent_item(struct btrfs_root *root,
5510 struct btrfs_path *path, u64 min_objectid)
5512 struct btrfs_key found_key;
5513 struct extent_buffer *leaf;
5518 if (path->slots[0] == 0) {
5519 btrfs_set_path_blocking(path);
5520 ret = btrfs_prev_leaf(root, path);
5526 leaf = path->nodes[0];
5527 nritems = btrfs_header_nritems(leaf);
5530 if (path->slots[0] == nritems)
5533 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5534 if (found_key.objectid < min_objectid)
5536 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5537 found_key.type == BTRFS_METADATA_ITEM_KEY)
5539 if (found_key.objectid == min_objectid &&
5540 found_key.type < BTRFS_EXTENT_ITEM_KEY)