xref: /openbmc/linux/fs/btrfs/ctree.c (revision 143bede5)
16cbd5570SChris Mason /*
2d352ac68SChris Mason  * Copyright (C) 2007,2008 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
19a6b6e75eSChris Mason #include <linux/sched.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
21eb60ceacSChris Mason #include "ctree.h"
22eb60ceacSChris Mason #include "disk-io.h"
237f5c1516SChris Mason #include "transaction.h"
245f39d397SChris Mason #include "print-tree.h"
25925baeddSChris Mason #include "locking.h"
269a8dd150SChris Mason 
27e089f05cSChris Mason static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
28e089f05cSChris Mason 		      *root, struct btrfs_path *path, int level);
29e089f05cSChris Mason static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
30d4dbff95SChris Mason 		      *root, struct btrfs_key *ins_key,
31cc0c5538SChris Mason 		      struct btrfs_path *path, int data_size, int extend);
325f39d397SChris Mason static int push_node_left(struct btrfs_trans_handle *trans,
335f39d397SChris Mason 			  struct btrfs_root *root, struct extent_buffer *dst,
34971a1f66SChris Mason 			  struct extent_buffer *src, int empty);
355f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans,
365f39d397SChris Mason 			      struct btrfs_root *root,
375f39d397SChris Mason 			      struct extent_buffer *dst_buf,
385f39d397SChris Mason 			      struct extent_buffer *src_buf);
39143bede5SJeff Mahoney static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
40e089f05cSChris Mason 		   struct btrfs_path *path, int level, int slot);
41d97e63b6SChris Mason 
422c90e5d6SChris Mason struct btrfs_path *btrfs_alloc_path(void)
432c90e5d6SChris Mason {
44df24a2b9SChris Mason 	struct btrfs_path *path;
45e00f7308SJeff Mahoney 	path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
46df24a2b9SChris Mason 	return path;
472c90e5d6SChris Mason }
482c90e5d6SChris Mason 
49b4ce94deSChris Mason /*
50b4ce94deSChris Mason  * set all locked nodes in the path to blocking locks.  This should
51b4ce94deSChris Mason  * be done before scheduling
52b4ce94deSChris Mason  */
53b4ce94deSChris Mason noinline void btrfs_set_path_blocking(struct btrfs_path *p)
54b4ce94deSChris Mason {
55b4ce94deSChris Mason 	int i;
56b4ce94deSChris Mason 	for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
57bd681513SChris Mason 		if (!p->nodes[i] || !p->locks[i])
58bd681513SChris Mason 			continue;
59bd681513SChris Mason 		btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
60bd681513SChris Mason 		if (p->locks[i] == BTRFS_READ_LOCK)
61bd681513SChris Mason 			p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
62bd681513SChris Mason 		else if (p->locks[i] == BTRFS_WRITE_LOCK)
63bd681513SChris Mason 			p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
64b4ce94deSChris Mason 	}
65b4ce94deSChris Mason }
66b4ce94deSChris Mason 
67b4ce94deSChris Mason /*
68b4ce94deSChris Mason  * reset all the locked nodes in the patch to spinning locks.
694008c04aSChris Mason  *
704008c04aSChris Mason  * held is used to keep lockdep happy, when lockdep is enabled
714008c04aSChris Mason  * we set held to a blocking lock before we go around and
724008c04aSChris Mason  * retake all the spinlocks in the path.  You can safely use NULL
734008c04aSChris Mason  * for held
74b4ce94deSChris Mason  */
754008c04aSChris Mason noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
76bd681513SChris Mason 					struct extent_buffer *held, int held_rw)
77b4ce94deSChris Mason {
78b4ce94deSChris Mason 	int i;
794008c04aSChris Mason 
804008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC
814008c04aSChris Mason 	/* lockdep really cares that we take all of these spinlocks
824008c04aSChris Mason 	 * in the right order.  If any of the locks in the path are not
834008c04aSChris Mason 	 * currently blocking, it is going to complain.  So, make really
844008c04aSChris Mason 	 * really sure by forcing the path to blocking before we clear
854008c04aSChris Mason 	 * the path blocking.
864008c04aSChris Mason 	 */
87bd681513SChris Mason 	if (held) {
88bd681513SChris Mason 		btrfs_set_lock_blocking_rw(held, held_rw);
89bd681513SChris Mason 		if (held_rw == BTRFS_WRITE_LOCK)
90bd681513SChris Mason 			held_rw = BTRFS_WRITE_LOCK_BLOCKING;
91bd681513SChris Mason 		else if (held_rw == BTRFS_READ_LOCK)
92bd681513SChris Mason 			held_rw = BTRFS_READ_LOCK_BLOCKING;
93bd681513SChris Mason 	}
944008c04aSChris Mason 	btrfs_set_path_blocking(p);
954008c04aSChris Mason #endif
964008c04aSChris Mason 
974008c04aSChris Mason 	for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
98bd681513SChris Mason 		if (p->nodes[i] && p->locks[i]) {
99bd681513SChris Mason 			btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
100bd681513SChris Mason 			if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
101bd681513SChris Mason 				p->locks[i] = BTRFS_WRITE_LOCK;
102bd681513SChris Mason 			else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
103bd681513SChris Mason 				p->locks[i] = BTRFS_READ_LOCK;
104bd681513SChris Mason 		}
105b4ce94deSChris Mason 	}
1064008c04aSChris Mason 
1074008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC
1084008c04aSChris Mason 	if (held)
109bd681513SChris Mason 		btrfs_clear_lock_blocking_rw(held, held_rw);
1104008c04aSChris Mason #endif
111b4ce94deSChris Mason }
112b4ce94deSChris Mason 
113d352ac68SChris Mason /* this also releases the path */
1142c90e5d6SChris Mason void btrfs_free_path(struct btrfs_path *p)
1152c90e5d6SChris Mason {
116ff175d57SJesper Juhl 	if (!p)
117ff175d57SJesper Juhl 		return;
118b3b4aa74SDavid Sterba 	btrfs_release_path(p);
1192c90e5d6SChris Mason 	kmem_cache_free(btrfs_path_cachep, p);
1202c90e5d6SChris Mason }
1212c90e5d6SChris Mason 
122d352ac68SChris Mason /*
123d352ac68SChris Mason  * path release drops references on the extent buffers in the path
124d352ac68SChris Mason  * and it drops any locks held by this path
125d352ac68SChris Mason  *
126d352ac68SChris Mason  * It is safe to call this on paths that no locks or extent buffers held.
127d352ac68SChris Mason  */
128b3b4aa74SDavid Sterba noinline void btrfs_release_path(struct btrfs_path *p)
129eb60ceacSChris Mason {
130eb60ceacSChris Mason 	int i;
131a2135011SChris Mason 
132234b63a0SChris Mason 	for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
1333f157a2fSChris Mason 		p->slots[i] = 0;
134eb60ceacSChris Mason 		if (!p->nodes[i])
135925baeddSChris Mason 			continue;
136925baeddSChris Mason 		if (p->locks[i]) {
137bd681513SChris Mason 			btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
138925baeddSChris Mason 			p->locks[i] = 0;
139925baeddSChris Mason 		}
1405f39d397SChris Mason 		free_extent_buffer(p->nodes[i]);
1413f157a2fSChris Mason 		p->nodes[i] = NULL;
142eb60ceacSChris Mason 	}
143eb60ceacSChris Mason }
144eb60ceacSChris Mason 
145d352ac68SChris Mason /*
146d352ac68SChris Mason  * safely gets a reference on the root node of a tree.  A lock
147d352ac68SChris Mason  * is not taken, so a concurrent writer may put a different node
148d352ac68SChris Mason  * at the root of the tree.  See btrfs_lock_root_node for the
149d352ac68SChris Mason  * looping required.
150d352ac68SChris Mason  *
151d352ac68SChris Mason  * The extent buffer returned by this has a reference taken, so
152d352ac68SChris Mason  * it won't disappear.  It may stop being the root of the tree
153d352ac68SChris Mason  * at any time because there are no locks held.
154d352ac68SChris Mason  */
155925baeddSChris Mason struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
156925baeddSChris Mason {
157925baeddSChris Mason 	struct extent_buffer *eb;
158240f62c8SChris Mason 
159240f62c8SChris Mason 	rcu_read_lock();
160240f62c8SChris Mason 	eb = rcu_dereference(root->node);
161925baeddSChris Mason 	extent_buffer_get(eb);
162240f62c8SChris Mason 	rcu_read_unlock();
163925baeddSChris Mason 	return eb;
164925baeddSChris Mason }
165925baeddSChris Mason 
166d352ac68SChris Mason /* loop around taking references on and locking the root node of the
167d352ac68SChris Mason  * tree until you end up with a lock on the root.  A locked buffer
168d352ac68SChris Mason  * is returned, with a reference held.
169d352ac68SChris Mason  */
170925baeddSChris Mason struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
171925baeddSChris Mason {
172925baeddSChris Mason 	struct extent_buffer *eb;
173925baeddSChris Mason 
174925baeddSChris Mason 	while (1) {
175925baeddSChris Mason 		eb = btrfs_root_node(root);
176925baeddSChris Mason 		btrfs_tree_lock(eb);
177240f62c8SChris Mason 		if (eb == root->node)
178925baeddSChris Mason 			break;
179925baeddSChris Mason 		btrfs_tree_unlock(eb);
180925baeddSChris Mason 		free_extent_buffer(eb);
181925baeddSChris Mason 	}
182925baeddSChris Mason 	return eb;
183925baeddSChris Mason }
184925baeddSChris Mason 
185bd681513SChris Mason /* loop around taking references on and locking the root node of the
186bd681513SChris Mason  * tree until you end up with a lock on the root.  A locked buffer
187bd681513SChris Mason  * is returned, with a reference held.
188bd681513SChris Mason  */
189bd681513SChris Mason struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
190bd681513SChris Mason {
191bd681513SChris Mason 	struct extent_buffer *eb;
192bd681513SChris Mason 
193bd681513SChris Mason 	while (1) {
194bd681513SChris Mason 		eb = btrfs_root_node(root);
195bd681513SChris Mason 		btrfs_tree_read_lock(eb);
196bd681513SChris Mason 		if (eb == root->node)
197bd681513SChris Mason 			break;
198bd681513SChris Mason 		btrfs_tree_read_unlock(eb);
199bd681513SChris Mason 		free_extent_buffer(eb);
200bd681513SChris Mason 	}
201bd681513SChris Mason 	return eb;
202bd681513SChris Mason }
203bd681513SChris Mason 
204d352ac68SChris Mason /* cowonly root (everything not a reference counted cow subvolume), just get
205d352ac68SChris Mason  * put onto a simple dirty list.  transaction.c walks this to make sure they
206d352ac68SChris Mason  * get properly updated on disk.
207d352ac68SChris Mason  */
2080b86a832SChris Mason static void add_root_to_dirty_list(struct btrfs_root *root)
2090b86a832SChris Mason {
2100b86a832SChris Mason 	if (root->track_dirty && list_empty(&root->dirty_list)) {
2110b86a832SChris Mason 		list_add(&root->dirty_list,
2120b86a832SChris Mason 			 &root->fs_info->dirty_cowonly_roots);
2130b86a832SChris Mason 	}
2140b86a832SChris Mason }
2150b86a832SChris Mason 
216d352ac68SChris Mason /*
217d352ac68SChris Mason  * used by snapshot creation to make a copy of a root for a tree with
218d352ac68SChris Mason  * a given objectid.  The buffer with the new root node is returned in
219d352ac68SChris Mason  * cow_ret, and this func returns zero on success or a negative error code.
220d352ac68SChris Mason  */
221be20aa9dSChris Mason int btrfs_copy_root(struct btrfs_trans_handle *trans,
222be20aa9dSChris Mason 		      struct btrfs_root *root,
223be20aa9dSChris Mason 		      struct extent_buffer *buf,
224be20aa9dSChris Mason 		      struct extent_buffer **cow_ret, u64 new_root_objectid)
225be20aa9dSChris Mason {
226be20aa9dSChris Mason 	struct extent_buffer *cow;
227be20aa9dSChris Mason 	int ret = 0;
228be20aa9dSChris Mason 	int level;
2295d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
230be20aa9dSChris Mason 
231be20aa9dSChris Mason 	WARN_ON(root->ref_cows && trans->transid !=
232be20aa9dSChris Mason 		root->fs_info->running_transaction->transid);
233be20aa9dSChris Mason 	WARN_ON(root->ref_cows && trans->transid != root->last_trans);
234be20aa9dSChris Mason 
235be20aa9dSChris Mason 	level = btrfs_header_level(buf);
2365d4f98a2SYan Zheng 	if (level == 0)
2375d4f98a2SYan Zheng 		btrfs_item_key(buf, &disk_key, 0);
2385d4f98a2SYan Zheng 	else
2395d4f98a2SYan Zheng 		btrfs_node_key(buf, &disk_key, 0);
24031840ae1SZheng Yan 
2415d4f98a2SYan Zheng 	cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
2425d4f98a2SYan Zheng 				     new_root_objectid, &disk_key, level,
24366d7e7f0SArne Jansen 				     buf->start, 0, 1);
2445d4f98a2SYan Zheng 	if (IS_ERR(cow))
245be20aa9dSChris Mason 		return PTR_ERR(cow);
246be20aa9dSChris Mason 
247be20aa9dSChris Mason 	copy_extent_buffer(cow, buf, 0, 0, cow->len);
248be20aa9dSChris Mason 	btrfs_set_header_bytenr(cow, cow->start);
249be20aa9dSChris Mason 	btrfs_set_header_generation(cow, trans->transid);
2505d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
2515d4f98a2SYan Zheng 	btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
2525d4f98a2SYan Zheng 				     BTRFS_HEADER_FLAG_RELOC);
2535d4f98a2SYan Zheng 	if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
2545d4f98a2SYan Zheng 		btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
2555d4f98a2SYan Zheng 	else
256be20aa9dSChris Mason 		btrfs_set_header_owner(cow, new_root_objectid);
257be20aa9dSChris Mason 
2582b82032cSYan Zheng 	write_extent_buffer(cow, root->fs_info->fsid,
2592b82032cSYan Zheng 			    (unsigned long)btrfs_header_fsid(cow),
2602b82032cSYan Zheng 			    BTRFS_FSID_SIZE);
2612b82032cSYan Zheng 
262be20aa9dSChris Mason 	WARN_ON(btrfs_header_generation(buf) > trans->transid);
2635d4f98a2SYan Zheng 	if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
26466d7e7f0SArne Jansen 		ret = btrfs_inc_ref(trans, root, cow, 1, 1);
2655d4f98a2SYan Zheng 	else
26666d7e7f0SArne Jansen 		ret = btrfs_inc_ref(trans, root, cow, 0, 1);
2674aec2b52SChris Mason 
268be20aa9dSChris Mason 	if (ret)
269be20aa9dSChris Mason 		return ret;
270be20aa9dSChris Mason 
271be20aa9dSChris Mason 	btrfs_mark_buffer_dirty(cow);
272be20aa9dSChris Mason 	*cow_ret = cow;
273be20aa9dSChris Mason 	return 0;
274be20aa9dSChris Mason }
275be20aa9dSChris Mason 
276d352ac68SChris Mason /*
2775d4f98a2SYan Zheng  * check if the tree block can be shared by multiple trees
2785d4f98a2SYan Zheng  */
2795d4f98a2SYan Zheng int btrfs_block_can_be_shared(struct btrfs_root *root,
2805d4f98a2SYan Zheng 			      struct extent_buffer *buf)
2815d4f98a2SYan Zheng {
2825d4f98a2SYan Zheng 	/*
2835d4f98a2SYan Zheng 	 * Tree blocks not in refernece counted trees and tree roots
2845d4f98a2SYan Zheng 	 * are never shared. If a block was allocated after the last
2855d4f98a2SYan Zheng 	 * snapshot and the block was not allocated by tree relocation,
2865d4f98a2SYan Zheng 	 * we know the block is not shared.
2875d4f98a2SYan Zheng 	 */
2885d4f98a2SYan Zheng 	if (root->ref_cows &&
2895d4f98a2SYan Zheng 	    buf != root->node && buf != root->commit_root &&
2905d4f98a2SYan Zheng 	    (btrfs_header_generation(buf) <=
2915d4f98a2SYan Zheng 	     btrfs_root_last_snapshot(&root->root_item) ||
2925d4f98a2SYan Zheng 	     btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
2935d4f98a2SYan Zheng 		return 1;
2945d4f98a2SYan Zheng #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2955d4f98a2SYan Zheng 	if (root->ref_cows &&
2965d4f98a2SYan Zheng 	    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
2975d4f98a2SYan Zheng 		return 1;
2985d4f98a2SYan Zheng #endif
2995d4f98a2SYan Zheng 	return 0;
3005d4f98a2SYan Zheng }
3015d4f98a2SYan Zheng 
3025d4f98a2SYan Zheng static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
3035d4f98a2SYan Zheng 				       struct btrfs_root *root,
3045d4f98a2SYan Zheng 				       struct extent_buffer *buf,
305f0486c68SYan, Zheng 				       struct extent_buffer *cow,
306f0486c68SYan, Zheng 				       int *last_ref)
3075d4f98a2SYan Zheng {
3085d4f98a2SYan Zheng 	u64 refs;
3095d4f98a2SYan Zheng 	u64 owner;
3105d4f98a2SYan Zheng 	u64 flags;
3115d4f98a2SYan Zheng 	u64 new_flags = 0;
3125d4f98a2SYan Zheng 	int ret;
3135d4f98a2SYan Zheng 
3145d4f98a2SYan Zheng 	/*
3155d4f98a2SYan Zheng 	 * Backrefs update rules:
3165d4f98a2SYan Zheng 	 *
3175d4f98a2SYan Zheng 	 * Always use full backrefs for extent pointers in tree block
3185d4f98a2SYan Zheng 	 * allocated by tree relocation.
3195d4f98a2SYan Zheng 	 *
3205d4f98a2SYan Zheng 	 * If a shared tree block is no longer referenced by its owner
3215d4f98a2SYan Zheng 	 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
3225d4f98a2SYan Zheng 	 * use full backrefs for extent pointers in tree block.
3235d4f98a2SYan Zheng 	 *
3245d4f98a2SYan Zheng 	 * If a tree block is been relocating
3255d4f98a2SYan Zheng 	 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
3265d4f98a2SYan Zheng 	 * use full backrefs for extent pointers in tree block.
3275d4f98a2SYan Zheng 	 * The reason for this is some operations (such as drop tree)
3285d4f98a2SYan Zheng 	 * are only allowed for blocks use full backrefs.
3295d4f98a2SYan Zheng 	 */
3305d4f98a2SYan Zheng 
3315d4f98a2SYan Zheng 	if (btrfs_block_can_be_shared(root, buf)) {
3325d4f98a2SYan Zheng 		ret = btrfs_lookup_extent_info(trans, root, buf->start,
3335d4f98a2SYan Zheng 					       buf->len, &refs, &flags);
3345d4f98a2SYan Zheng 		BUG_ON(ret);
3355d4f98a2SYan Zheng 		BUG_ON(refs == 0);
3365d4f98a2SYan Zheng 	} else {
3375d4f98a2SYan Zheng 		refs = 1;
3385d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
3395d4f98a2SYan Zheng 		    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
3405d4f98a2SYan Zheng 			flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
3415d4f98a2SYan Zheng 		else
3425d4f98a2SYan Zheng 			flags = 0;
3435d4f98a2SYan Zheng 	}
3445d4f98a2SYan Zheng 
3455d4f98a2SYan Zheng 	owner = btrfs_header_owner(buf);
3465d4f98a2SYan Zheng 	BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
3475d4f98a2SYan Zheng 	       !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
3485d4f98a2SYan Zheng 
3495d4f98a2SYan Zheng 	if (refs > 1) {
3505d4f98a2SYan Zheng 		if ((owner == root->root_key.objectid ||
3515d4f98a2SYan Zheng 		     root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
3525d4f98a2SYan Zheng 		    !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
35366d7e7f0SArne Jansen 			ret = btrfs_inc_ref(trans, root, buf, 1, 1);
3545d4f98a2SYan Zheng 			BUG_ON(ret);
3555d4f98a2SYan Zheng 
3565d4f98a2SYan Zheng 			if (root->root_key.objectid ==
3575d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID) {
35866d7e7f0SArne Jansen 				ret = btrfs_dec_ref(trans, root, buf, 0, 1);
3595d4f98a2SYan Zheng 				BUG_ON(ret);
36066d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 1, 1);
3615d4f98a2SYan Zheng 				BUG_ON(ret);
3625d4f98a2SYan Zheng 			}
3635d4f98a2SYan Zheng 			new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
3645d4f98a2SYan Zheng 		} else {
3655d4f98a2SYan Zheng 
3665d4f98a2SYan Zheng 			if (root->root_key.objectid ==
3675d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID)
36866d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 1, 1);
3695d4f98a2SYan Zheng 			else
37066d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 0, 1);
3715d4f98a2SYan Zheng 			BUG_ON(ret);
3725d4f98a2SYan Zheng 		}
3735d4f98a2SYan Zheng 		if (new_flags != 0) {
3745d4f98a2SYan Zheng 			ret = btrfs_set_disk_extent_flags(trans, root,
3755d4f98a2SYan Zheng 							  buf->start,
3765d4f98a2SYan Zheng 							  buf->len,
3775d4f98a2SYan Zheng 							  new_flags, 0);
3785d4f98a2SYan Zheng 			BUG_ON(ret);
3795d4f98a2SYan Zheng 		}
3805d4f98a2SYan Zheng 	} else {
3815d4f98a2SYan Zheng 		if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
3825d4f98a2SYan Zheng 			if (root->root_key.objectid ==
3835d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID)
38466d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 1, 1);
3855d4f98a2SYan Zheng 			else
38666d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 0, 1);
3875d4f98a2SYan Zheng 			BUG_ON(ret);
38866d7e7f0SArne Jansen 			ret = btrfs_dec_ref(trans, root, buf, 1, 1);
3895d4f98a2SYan Zheng 			BUG_ON(ret);
3905d4f98a2SYan Zheng 		}
3915d4f98a2SYan Zheng 		clean_tree_block(trans, root, buf);
392f0486c68SYan, Zheng 		*last_ref = 1;
3935d4f98a2SYan Zheng 	}
3945d4f98a2SYan Zheng 	return 0;
3955d4f98a2SYan Zheng }
3965d4f98a2SYan Zheng 
3975d4f98a2SYan Zheng /*
398d397712bSChris Mason  * does the dirty work in cow of a single block.  The parent block (if
399d397712bSChris Mason  * supplied) is updated to point to the new cow copy.  The new buffer is marked
400d397712bSChris Mason  * dirty and returned locked.  If you modify the block it needs to be marked
401d397712bSChris Mason  * dirty again.
402d352ac68SChris Mason  *
403d352ac68SChris Mason  * search_start -- an allocation hint for the new block
404d352ac68SChris Mason  *
405d397712bSChris Mason  * empty_size -- a hint that you plan on doing more cow.  This is the size in
406d397712bSChris Mason  * bytes the allocator should try to find free next to the block it returns.
407d397712bSChris Mason  * This is just a hint and may be ignored by the allocator.
408d352ac68SChris Mason  */
409d397712bSChris Mason static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
4105f39d397SChris Mason 			     struct btrfs_root *root,
4115f39d397SChris Mason 			     struct extent_buffer *buf,
4125f39d397SChris Mason 			     struct extent_buffer *parent, int parent_slot,
4135f39d397SChris Mason 			     struct extent_buffer **cow_ret,
4149fa8cfe7SChris Mason 			     u64 search_start, u64 empty_size)
4156702ed49SChris Mason {
4165d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
4175f39d397SChris Mason 	struct extent_buffer *cow;
4187bb86316SChris Mason 	int level;
419f0486c68SYan, Zheng 	int last_ref = 0;
420925baeddSChris Mason 	int unlock_orig = 0;
4215d4f98a2SYan Zheng 	u64 parent_start;
4226702ed49SChris Mason 
423925baeddSChris Mason 	if (*cow_ret == buf)
424925baeddSChris Mason 		unlock_orig = 1;
425925baeddSChris Mason 
426b9447ef8SChris Mason 	btrfs_assert_tree_locked(buf);
427925baeddSChris Mason 
4287bb86316SChris Mason 	WARN_ON(root->ref_cows && trans->transid !=
4297bb86316SChris Mason 		root->fs_info->running_transaction->transid);
4306702ed49SChris Mason 	WARN_ON(root->ref_cows && trans->transid != root->last_trans);
4315f39d397SChris Mason 
4327bb86316SChris Mason 	level = btrfs_header_level(buf);
43331840ae1SZheng Yan 
4345d4f98a2SYan Zheng 	if (level == 0)
4355d4f98a2SYan Zheng 		btrfs_item_key(buf, &disk_key, 0);
4365d4f98a2SYan Zheng 	else
4375d4f98a2SYan Zheng 		btrfs_node_key(buf, &disk_key, 0);
4385d4f98a2SYan Zheng 
4395d4f98a2SYan Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
4405d4f98a2SYan Zheng 		if (parent)
4415d4f98a2SYan Zheng 			parent_start = parent->start;
4425d4f98a2SYan Zheng 		else
4435d4f98a2SYan Zheng 			parent_start = 0;
4445d4f98a2SYan Zheng 	} else
4455d4f98a2SYan Zheng 		parent_start = 0;
4465d4f98a2SYan Zheng 
4475d4f98a2SYan Zheng 	cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
4485d4f98a2SYan Zheng 				     root->root_key.objectid, &disk_key,
44966d7e7f0SArne Jansen 				     level, search_start, empty_size, 1);
4506702ed49SChris Mason 	if (IS_ERR(cow))
4516702ed49SChris Mason 		return PTR_ERR(cow);
4526702ed49SChris Mason 
453b4ce94deSChris Mason 	/* cow is set to blocking by btrfs_init_new_buffer */
454b4ce94deSChris Mason 
4555f39d397SChris Mason 	copy_extent_buffer(cow, buf, 0, 0, cow->len);
456db94535dSChris Mason 	btrfs_set_header_bytenr(cow, cow->start);
4575f39d397SChris Mason 	btrfs_set_header_generation(cow, trans->transid);
4585d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
4595d4f98a2SYan Zheng 	btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
4605d4f98a2SYan Zheng 				     BTRFS_HEADER_FLAG_RELOC);
4615d4f98a2SYan Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4625d4f98a2SYan Zheng 		btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
4635d4f98a2SYan Zheng 	else
4645f39d397SChris Mason 		btrfs_set_header_owner(cow, root->root_key.objectid);
4656702ed49SChris Mason 
4662b82032cSYan Zheng 	write_extent_buffer(cow, root->fs_info->fsid,
4672b82032cSYan Zheng 			    (unsigned long)btrfs_header_fsid(cow),
4682b82032cSYan Zheng 			    BTRFS_FSID_SIZE);
4692b82032cSYan Zheng 
470f0486c68SYan, Zheng 	update_ref_for_cow(trans, root, buf, cow, &last_ref);
4711a40e23bSZheng Yan 
4723fd0a558SYan, Zheng 	if (root->ref_cows)
4733fd0a558SYan, Zheng 		btrfs_reloc_cow_block(trans, root, buf, cow);
4743fd0a558SYan, Zheng 
4756702ed49SChris Mason 	if (buf == root->node) {
476925baeddSChris Mason 		WARN_ON(parent && parent != buf);
4775d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
4785d4f98a2SYan Zheng 		    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
4795d4f98a2SYan Zheng 			parent_start = buf->start;
4805d4f98a2SYan Zheng 		else
4815d4f98a2SYan Zheng 			parent_start = 0;
482925baeddSChris Mason 
4835f39d397SChris Mason 		extent_buffer_get(cow);
484240f62c8SChris Mason 		rcu_assign_pointer(root->node, cow);
485925baeddSChris Mason 
486f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, buf, parent_start,
48766d7e7f0SArne Jansen 				      last_ref, 1);
4885f39d397SChris Mason 		free_extent_buffer(buf);
4890b86a832SChris Mason 		add_root_to_dirty_list(root);
4906702ed49SChris Mason 	} else {
4915d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4925d4f98a2SYan Zheng 			parent_start = parent->start;
4935d4f98a2SYan Zheng 		else
4945d4f98a2SYan Zheng 			parent_start = 0;
4955d4f98a2SYan Zheng 
4965d4f98a2SYan Zheng 		WARN_ON(trans->transid != btrfs_header_generation(parent));
4975f39d397SChris Mason 		btrfs_set_node_blockptr(parent, parent_slot,
498db94535dSChris Mason 					cow->start);
49974493f7aSChris Mason 		btrfs_set_node_ptr_generation(parent, parent_slot,
50074493f7aSChris Mason 					      trans->transid);
5016702ed49SChris Mason 		btrfs_mark_buffer_dirty(parent);
502f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, buf, parent_start,
50366d7e7f0SArne Jansen 				      last_ref, 1);
5046702ed49SChris Mason 	}
505925baeddSChris Mason 	if (unlock_orig)
506925baeddSChris Mason 		btrfs_tree_unlock(buf);
5075f39d397SChris Mason 	free_extent_buffer(buf);
5086702ed49SChris Mason 	btrfs_mark_buffer_dirty(cow);
5096702ed49SChris Mason 	*cow_ret = cow;
5106702ed49SChris Mason 	return 0;
5116702ed49SChris Mason }
5126702ed49SChris Mason 
5135d4f98a2SYan Zheng static inline int should_cow_block(struct btrfs_trans_handle *trans,
5145d4f98a2SYan Zheng 				   struct btrfs_root *root,
5155d4f98a2SYan Zheng 				   struct extent_buffer *buf)
5165d4f98a2SYan Zheng {
517f1ebcc74SLiu Bo 	/* ensure we can see the force_cow */
518f1ebcc74SLiu Bo 	smp_rmb();
519f1ebcc74SLiu Bo 
520f1ebcc74SLiu Bo 	/*
521f1ebcc74SLiu Bo 	 * We do not need to cow a block if
522f1ebcc74SLiu Bo 	 * 1) this block is not created or changed in this transaction;
523f1ebcc74SLiu Bo 	 * 2) this block does not belong to TREE_RELOC tree;
524f1ebcc74SLiu Bo 	 * 3) the root is not forced COW.
525f1ebcc74SLiu Bo 	 *
526f1ebcc74SLiu Bo 	 * What is forced COW:
527f1ebcc74SLiu Bo 	 *    when we create snapshot during commiting the transaction,
528f1ebcc74SLiu Bo 	 *    after we've finished coping src root, we must COW the shared
529f1ebcc74SLiu Bo 	 *    block to ensure the metadata consistency.
530f1ebcc74SLiu Bo 	 */
5315d4f98a2SYan Zheng 	if (btrfs_header_generation(buf) == trans->transid &&
5325d4f98a2SYan Zheng 	    !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
5335d4f98a2SYan Zheng 	    !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
534f1ebcc74SLiu Bo 	      btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
535f1ebcc74SLiu Bo 	    !root->force_cow)
5365d4f98a2SYan Zheng 		return 0;
5375d4f98a2SYan Zheng 	return 1;
5385d4f98a2SYan Zheng }
5395d4f98a2SYan Zheng 
540d352ac68SChris Mason /*
541d352ac68SChris Mason  * cows a single block, see __btrfs_cow_block for the real work.
542d352ac68SChris Mason  * This version of it has extra checks so that a block isn't cow'd more than
543d352ac68SChris Mason  * once per transaction, as long as it hasn't been written yet
544d352ac68SChris Mason  */
545d397712bSChris Mason noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
5465f39d397SChris Mason 		    struct btrfs_root *root, struct extent_buffer *buf,
5475f39d397SChris Mason 		    struct extent_buffer *parent, int parent_slot,
5489fa8cfe7SChris Mason 		    struct extent_buffer **cow_ret)
54902217ed2SChris Mason {
5506702ed49SChris Mason 	u64 search_start;
551f510cfecSChris Mason 	int ret;
552dc17ff8fSChris Mason 
553ccd467d6SChris Mason 	if (trans->transaction != root->fs_info->running_transaction) {
554d397712bSChris Mason 		printk(KERN_CRIT "trans %llu running %llu\n",
555d397712bSChris Mason 		       (unsigned long long)trans->transid,
556d397712bSChris Mason 		       (unsigned long long)
557ccd467d6SChris Mason 		       root->fs_info->running_transaction->transid);
558ccd467d6SChris Mason 		WARN_ON(1);
559ccd467d6SChris Mason 	}
560ccd467d6SChris Mason 	if (trans->transid != root->fs_info->generation) {
561d397712bSChris Mason 		printk(KERN_CRIT "trans %llu running %llu\n",
562d397712bSChris Mason 		       (unsigned long long)trans->transid,
563d397712bSChris Mason 		       (unsigned long long)root->fs_info->generation);
564ccd467d6SChris Mason 		WARN_ON(1);
565ccd467d6SChris Mason 	}
566dc17ff8fSChris Mason 
5675d4f98a2SYan Zheng 	if (!should_cow_block(trans, root, buf)) {
56802217ed2SChris Mason 		*cow_ret = buf;
56902217ed2SChris Mason 		return 0;
57002217ed2SChris Mason 	}
571c487685dSChris Mason 
5720b86a832SChris Mason 	search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
573b4ce94deSChris Mason 
574b4ce94deSChris Mason 	if (parent)
575b4ce94deSChris Mason 		btrfs_set_lock_blocking(parent);
576b4ce94deSChris Mason 	btrfs_set_lock_blocking(buf);
577b4ce94deSChris Mason 
578f510cfecSChris Mason 	ret = __btrfs_cow_block(trans, root, buf, parent,
5799fa8cfe7SChris Mason 				 parent_slot, cow_ret, search_start, 0);
5801abe9b8aSliubo 
5811abe9b8aSliubo 	trace_btrfs_cow_block(root, buf, *cow_ret);
5821abe9b8aSliubo 
583f510cfecSChris Mason 	return ret;
5842c90e5d6SChris Mason }
5856702ed49SChris Mason 
586d352ac68SChris Mason /*
587d352ac68SChris Mason  * helper function for defrag to decide if two blocks pointed to by a
588d352ac68SChris Mason  * node are actually close by
589d352ac68SChris Mason  */
5906b80053dSChris Mason static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
5916702ed49SChris Mason {
5926b80053dSChris Mason 	if (blocknr < other && other - (blocknr + blocksize) < 32768)
5936702ed49SChris Mason 		return 1;
5946b80053dSChris Mason 	if (blocknr > other && blocknr - (other + blocksize) < 32768)
5956702ed49SChris Mason 		return 1;
59602217ed2SChris Mason 	return 0;
59702217ed2SChris Mason }
59802217ed2SChris Mason 
599081e9573SChris Mason /*
600081e9573SChris Mason  * compare two keys in a memcmp fashion
601081e9573SChris Mason  */
602081e9573SChris Mason static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
603081e9573SChris Mason {
604081e9573SChris Mason 	struct btrfs_key k1;
605081e9573SChris Mason 
606081e9573SChris Mason 	btrfs_disk_key_to_cpu(&k1, disk);
607081e9573SChris Mason 
60820736abaSDiego Calleja 	return btrfs_comp_cpu_keys(&k1, k2);
609081e9573SChris Mason }
610081e9573SChris Mason 
611f3465ca4SJosef Bacik /*
612f3465ca4SJosef Bacik  * same as comp_keys only with two btrfs_key's
613f3465ca4SJosef Bacik  */
6145d4f98a2SYan Zheng int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
615f3465ca4SJosef Bacik {
616f3465ca4SJosef Bacik 	if (k1->objectid > k2->objectid)
617f3465ca4SJosef Bacik 		return 1;
618f3465ca4SJosef Bacik 	if (k1->objectid < k2->objectid)
619f3465ca4SJosef Bacik 		return -1;
620f3465ca4SJosef Bacik 	if (k1->type > k2->type)
621f3465ca4SJosef Bacik 		return 1;
622f3465ca4SJosef Bacik 	if (k1->type < k2->type)
623f3465ca4SJosef Bacik 		return -1;
624f3465ca4SJosef Bacik 	if (k1->offset > k2->offset)
625f3465ca4SJosef Bacik 		return 1;
626f3465ca4SJosef Bacik 	if (k1->offset < k2->offset)
627f3465ca4SJosef Bacik 		return -1;
628f3465ca4SJosef Bacik 	return 0;
629f3465ca4SJosef Bacik }
630081e9573SChris Mason 
631d352ac68SChris Mason /*
632d352ac68SChris Mason  * this is used by the defrag code to go through all the
633d352ac68SChris Mason  * leaves pointed to by a node and reallocate them so that
634d352ac68SChris Mason  * disk order is close to key order
635d352ac68SChris Mason  */
6366702ed49SChris Mason int btrfs_realloc_node(struct btrfs_trans_handle *trans,
6375f39d397SChris Mason 		       struct btrfs_root *root, struct extent_buffer *parent,
638a6b6e75eSChris Mason 		       int start_slot, int cache_only, u64 *last_ret,
639a6b6e75eSChris Mason 		       struct btrfs_key *progress)
6406702ed49SChris Mason {
6416b80053dSChris Mason 	struct extent_buffer *cur;
6426702ed49SChris Mason 	u64 blocknr;
643ca7a79adSChris Mason 	u64 gen;
644e9d0b13bSChris Mason 	u64 search_start = *last_ret;
645e9d0b13bSChris Mason 	u64 last_block = 0;
6466702ed49SChris Mason 	u64 other;
6476702ed49SChris Mason 	u32 parent_nritems;
6486702ed49SChris Mason 	int end_slot;
6496702ed49SChris Mason 	int i;
6506702ed49SChris Mason 	int err = 0;
651f2183bdeSChris Mason 	int parent_level;
6526b80053dSChris Mason 	int uptodate;
6536b80053dSChris Mason 	u32 blocksize;
654081e9573SChris Mason 	int progress_passed = 0;
655081e9573SChris Mason 	struct btrfs_disk_key disk_key;
6566702ed49SChris Mason 
6575708b959SChris Mason 	parent_level = btrfs_header_level(parent);
6585708b959SChris Mason 	if (cache_only && parent_level != 1)
6595708b959SChris Mason 		return 0;
6605708b959SChris Mason 
661d397712bSChris Mason 	if (trans->transaction != root->fs_info->running_transaction)
6626702ed49SChris Mason 		WARN_ON(1);
663d397712bSChris Mason 	if (trans->transid != root->fs_info->generation)
6646702ed49SChris Mason 		WARN_ON(1);
66586479a04SChris Mason 
6666b80053dSChris Mason 	parent_nritems = btrfs_header_nritems(parent);
6676b80053dSChris Mason 	blocksize = btrfs_level_size(root, parent_level - 1);
6686702ed49SChris Mason 	end_slot = parent_nritems;
6696702ed49SChris Mason 
6706702ed49SChris Mason 	if (parent_nritems == 1)
6716702ed49SChris Mason 		return 0;
6726702ed49SChris Mason 
673b4ce94deSChris Mason 	btrfs_set_lock_blocking(parent);
674b4ce94deSChris Mason 
6756702ed49SChris Mason 	for (i = start_slot; i < end_slot; i++) {
6766702ed49SChris Mason 		int close = 1;
677a6b6e75eSChris Mason 
678081e9573SChris Mason 		btrfs_node_key(parent, &disk_key, i);
679081e9573SChris Mason 		if (!progress_passed && comp_keys(&disk_key, progress) < 0)
680081e9573SChris Mason 			continue;
681081e9573SChris Mason 
682081e9573SChris Mason 		progress_passed = 1;
6836b80053dSChris Mason 		blocknr = btrfs_node_blockptr(parent, i);
684ca7a79adSChris Mason 		gen = btrfs_node_ptr_generation(parent, i);
685e9d0b13bSChris Mason 		if (last_block == 0)
686e9d0b13bSChris Mason 			last_block = blocknr;
6875708b959SChris Mason 
6886702ed49SChris Mason 		if (i > 0) {
6896b80053dSChris Mason 			other = btrfs_node_blockptr(parent, i - 1);
6906b80053dSChris Mason 			close = close_blocks(blocknr, other, blocksize);
6916702ed49SChris Mason 		}
6920ef3e66bSChris Mason 		if (!close && i < end_slot - 2) {
6936b80053dSChris Mason 			other = btrfs_node_blockptr(parent, i + 1);
6946b80053dSChris Mason 			close = close_blocks(blocknr, other, blocksize);
6956702ed49SChris Mason 		}
696e9d0b13bSChris Mason 		if (close) {
697e9d0b13bSChris Mason 			last_block = blocknr;
6986702ed49SChris Mason 			continue;
699e9d0b13bSChris Mason 		}
7006702ed49SChris Mason 
7016b80053dSChris Mason 		cur = btrfs_find_tree_block(root, blocknr, blocksize);
7026b80053dSChris Mason 		if (cur)
7031259ab75SChris Mason 			uptodate = btrfs_buffer_uptodate(cur, gen);
7046b80053dSChris Mason 		else
7056b80053dSChris Mason 			uptodate = 0;
7065708b959SChris Mason 		if (!cur || !uptodate) {
7076702ed49SChris Mason 			if (cache_only) {
7086b80053dSChris Mason 				free_extent_buffer(cur);
7096702ed49SChris Mason 				continue;
7106702ed49SChris Mason 			}
7116b80053dSChris Mason 			if (!cur) {
7126b80053dSChris Mason 				cur = read_tree_block(root, blocknr,
713ca7a79adSChris Mason 							 blocksize, gen);
71497d9a8a4STsutomu Itoh 				if (!cur)
71597d9a8a4STsutomu Itoh 					return -EIO;
7166b80053dSChris Mason 			} else if (!uptodate) {
717ca7a79adSChris Mason 				btrfs_read_buffer(cur, gen);
7186702ed49SChris Mason 			}
719f2183bdeSChris Mason 		}
720e9d0b13bSChris Mason 		if (search_start == 0)
7216b80053dSChris Mason 			search_start = last_block;
722e9d0b13bSChris Mason 
723e7a84565SChris Mason 		btrfs_tree_lock(cur);
724b4ce94deSChris Mason 		btrfs_set_lock_blocking(cur);
7256b80053dSChris Mason 		err = __btrfs_cow_block(trans, root, cur, parent, i,
726e7a84565SChris Mason 					&cur, search_start,
7276b80053dSChris Mason 					min(16 * blocksize,
7289fa8cfe7SChris Mason 					    (end_slot - i) * blocksize));
729252c38f0SYan 		if (err) {
730e7a84565SChris Mason 			btrfs_tree_unlock(cur);
7316b80053dSChris Mason 			free_extent_buffer(cur);
7326702ed49SChris Mason 			break;
733252c38f0SYan 		}
734e7a84565SChris Mason 		search_start = cur->start;
735e7a84565SChris Mason 		last_block = cur->start;
736f2183bdeSChris Mason 		*last_ret = search_start;
737e7a84565SChris Mason 		btrfs_tree_unlock(cur);
738e7a84565SChris Mason 		free_extent_buffer(cur);
7396702ed49SChris Mason 	}
7406702ed49SChris Mason 	return err;
7416702ed49SChris Mason }
7426702ed49SChris Mason 
74374123bd7SChris Mason /*
74474123bd7SChris Mason  * The leaf data grows from end-to-front in the node.
74574123bd7SChris Mason  * this returns the address of the start of the last item,
74674123bd7SChris Mason  * which is the stop of the leaf data stack
74774123bd7SChris Mason  */
748123abc88SChris Mason static inline unsigned int leaf_data_end(struct btrfs_root *root,
7495f39d397SChris Mason 					 struct extent_buffer *leaf)
750be0e5c09SChris Mason {
7515f39d397SChris Mason 	u32 nr = btrfs_header_nritems(leaf);
752be0e5c09SChris Mason 	if (nr == 0)
753123abc88SChris Mason 		return BTRFS_LEAF_DATA_SIZE(root);
7545f39d397SChris Mason 	return btrfs_item_offset_nr(leaf, nr - 1);
755be0e5c09SChris Mason }
756be0e5c09SChris Mason 
757aa5d6bedSChris Mason 
75874123bd7SChris Mason /*
7595f39d397SChris Mason  * search for key in the extent_buffer.  The items start at offset p,
7605f39d397SChris Mason  * and they are item_size apart.  There are 'max' items in p.
7615f39d397SChris Mason  *
76274123bd7SChris Mason  * the slot in the array is returned via slot, and it points to
76374123bd7SChris Mason  * the place where you would insert key if it is not found in
76474123bd7SChris Mason  * the array.
76574123bd7SChris Mason  *
76674123bd7SChris Mason  * slot may point to max if the key is bigger than all of the keys
76774123bd7SChris Mason  */
768e02119d5SChris Mason static noinline int generic_bin_search(struct extent_buffer *eb,
769e02119d5SChris Mason 				       unsigned long p,
7705f39d397SChris Mason 				       int item_size, struct btrfs_key *key,
771be0e5c09SChris Mason 				       int max, int *slot)
772be0e5c09SChris Mason {
773be0e5c09SChris Mason 	int low = 0;
774be0e5c09SChris Mason 	int high = max;
775be0e5c09SChris Mason 	int mid;
776be0e5c09SChris Mason 	int ret;
777479965d6SChris Mason 	struct btrfs_disk_key *tmp = NULL;
7785f39d397SChris Mason 	struct btrfs_disk_key unaligned;
7795f39d397SChris Mason 	unsigned long offset;
7805f39d397SChris Mason 	char *kaddr = NULL;
7815f39d397SChris Mason 	unsigned long map_start = 0;
7825f39d397SChris Mason 	unsigned long map_len = 0;
783479965d6SChris Mason 	int err;
784be0e5c09SChris Mason 
785be0e5c09SChris Mason 	while (low < high) {
786be0e5c09SChris Mason 		mid = (low + high) / 2;
7875f39d397SChris Mason 		offset = p + mid * item_size;
7885f39d397SChris Mason 
789a6591715SChris Mason 		if (!kaddr || offset < map_start ||
7905f39d397SChris Mason 		    (offset + sizeof(struct btrfs_disk_key)) >
7915f39d397SChris Mason 		    map_start + map_len) {
792934d375bSChris Mason 
793934d375bSChris Mason 			err = map_private_extent_buffer(eb, offset,
794479965d6SChris Mason 						sizeof(struct btrfs_disk_key),
795a6591715SChris Mason 						&kaddr, &map_start, &map_len);
7965f39d397SChris Mason 
797479965d6SChris Mason 			if (!err) {
798479965d6SChris Mason 				tmp = (struct btrfs_disk_key *)(kaddr + offset -
799479965d6SChris Mason 							map_start);
800479965d6SChris Mason 			} else {
8015f39d397SChris Mason 				read_extent_buffer(eb, &unaligned,
8025f39d397SChris Mason 						   offset, sizeof(unaligned));
8035f39d397SChris Mason 				tmp = &unaligned;
804479965d6SChris Mason 			}
805479965d6SChris Mason 
8065f39d397SChris Mason 		} else {
8075f39d397SChris Mason 			tmp = (struct btrfs_disk_key *)(kaddr + offset -
8085f39d397SChris Mason 							map_start);
8095f39d397SChris Mason 		}
810be0e5c09SChris Mason 		ret = comp_keys(tmp, key);
811be0e5c09SChris Mason 
812be0e5c09SChris Mason 		if (ret < 0)
813be0e5c09SChris Mason 			low = mid + 1;
814be0e5c09SChris Mason 		else if (ret > 0)
815be0e5c09SChris Mason 			high = mid;
816be0e5c09SChris Mason 		else {
817be0e5c09SChris Mason 			*slot = mid;
818be0e5c09SChris Mason 			return 0;
819be0e5c09SChris Mason 		}
820be0e5c09SChris Mason 	}
821be0e5c09SChris Mason 	*slot = low;
822be0e5c09SChris Mason 	return 1;
823be0e5c09SChris Mason }
824be0e5c09SChris Mason 
82597571fd0SChris Mason /*
82697571fd0SChris Mason  * simple bin_search frontend that does the right thing for
82797571fd0SChris Mason  * leaves vs nodes
82897571fd0SChris Mason  */
8295f39d397SChris Mason static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
8305f39d397SChris Mason 		      int level, int *slot)
831be0e5c09SChris Mason {
8325f39d397SChris Mason 	if (level == 0) {
8335f39d397SChris Mason 		return generic_bin_search(eb,
8345f39d397SChris Mason 					  offsetof(struct btrfs_leaf, items),
8350783fcfcSChris Mason 					  sizeof(struct btrfs_item),
8365f39d397SChris Mason 					  key, btrfs_header_nritems(eb),
8377518a238SChris Mason 					  slot);
838be0e5c09SChris Mason 	} else {
8395f39d397SChris Mason 		return generic_bin_search(eb,
8405f39d397SChris Mason 					  offsetof(struct btrfs_node, ptrs),
841123abc88SChris Mason 					  sizeof(struct btrfs_key_ptr),
8425f39d397SChris Mason 					  key, btrfs_header_nritems(eb),
8437518a238SChris Mason 					  slot);
844be0e5c09SChris Mason 	}
845be0e5c09SChris Mason 	return -1;
846be0e5c09SChris Mason }
847be0e5c09SChris Mason 
8485d4f98a2SYan Zheng int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
8495d4f98a2SYan Zheng 		     int level, int *slot)
8505d4f98a2SYan Zheng {
8515d4f98a2SYan Zheng 	return bin_search(eb, key, level, slot);
8525d4f98a2SYan Zheng }
8535d4f98a2SYan Zheng 
854f0486c68SYan, Zheng static void root_add_used(struct btrfs_root *root, u32 size)
855f0486c68SYan, Zheng {
856f0486c68SYan, Zheng 	spin_lock(&root->accounting_lock);
857f0486c68SYan, Zheng 	btrfs_set_root_used(&root->root_item,
858f0486c68SYan, Zheng 			    btrfs_root_used(&root->root_item) + size);
859f0486c68SYan, Zheng 	spin_unlock(&root->accounting_lock);
860f0486c68SYan, Zheng }
861f0486c68SYan, Zheng 
862f0486c68SYan, Zheng static void root_sub_used(struct btrfs_root *root, u32 size)
863f0486c68SYan, Zheng {
864f0486c68SYan, Zheng 	spin_lock(&root->accounting_lock);
865f0486c68SYan, Zheng 	btrfs_set_root_used(&root->root_item,
866f0486c68SYan, Zheng 			    btrfs_root_used(&root->root_item) - size);
867f0486c68SYan, Zheng 	spin_unlock(&root->accounting_lock);
868f0486c68SYan, Zheng }
869f0486c68SYan, Zheng 
870d352ac68SChris Mason /* given a node and slot number, this reads the blocks it points to.  The
871d352ac68SChris Mason  * extent buffer is returned with a reference taken (but unlocked).
872d352ac68SChris Mason  * NULL is returned on error.
873d352ac68SChris Mason  */
874e02119d5SChris Mason static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
8755f39d397SChris Mason 				   struct extent_buffer *parent, int slot)
876bb803951SChris Mason {
877ca7a79adSChris Mason 	int level = btrfs_header_level(parent);
878bb803951SChris Mason 	if (slot < 0)
879bb803951SChris Mason 		return NULL;
8805f39d397SChris Mason 	if (slot >= btrfs_header_nritems(parent))
881bb803951SChris Mason 		return NULL;
882ca7a79adSChris Mason 
883ca7a79adSChris Mason 	BUG_ON(level == 0);
884ca7a79adSChris Mason 
885db94535dSChris Mason 	return read_tree_block(root, btrfs_node_blockptr(parent, slot),
886ca7a79adSChris Mason 		       btrfs_level_size(root, level - 1),
887ca7a79adSChris Mason 		       btrfs_node_ptr_generation(parent, slot));
888bb803951SChris Mason }
889bb803951SChris Mason 
890d352ac68SChris Mason /*
891d352ac68SChris Mason  * node level balancing, used to make sure nodes are in proper order for
892d352ac68SChris Mason  * item deletion.  We balance from the top down, so we have to make sure
893d352ac68SChris Mason  * that a deletion won't leave an node completely empty later on.
894d352ac68SChris Mason  */
895e02119d5SChris Mason static noinline int balance_level(struct btrfs_trans_handle *trans,
89698ed5174SChris Mason 			 struct btrfs_root *root,
89798ed5174SChris Mason 			 struct btrfs_path *path, int level)
898bb803951SChris Mason {
8995f39d397SChris Mason 	struct extent_buffer *right = NULL;
9005f39d397SChris Mason 	struct extent_buffer *mid;
9015f39d397SChris Mason 	struct extent_buffer *left = NULL;
9025f39d397SChris Mason 	struct extent_buffer *parent = NULL;
903bb803951SChris Mason 	int ret = 0;
904bb803951SChris Mason 	int wret;
905bb803951SChris Mason 	int pslot;
906bb803951SChris Mason 	int orig_slot = path->slots[level];
90779f95c82SChris Mason 	u64 orig_ptr;
908bb803951SChris Mason 
909bb803951SChris Mason 	if (level == 0)
910bb803951SChris Mason 		return 0;
911bb803951SChris Mason 
9125f39d397SChris Mason 	mid = path->nodes[level];
913b4ce94deSChris Mason 
914bd681513SChris Mason 	WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
915bd681513SChris Mason 		path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
9167bb86316SChris Mason 	WARN_ON(btrfs_header_generation(mid) != trans->transid);
9177bb86316SChris Mason 
9181d4f8a0cSChris Mason 	orig_ptr = btrfs_node_blockptr(mid, orig_slot);
91979f95c82SChris Mason 
920a05a9bb1SLi Zefan 	if (level < BTRFS_MAX_LEVEL - 1) {
9215f39d397SChris Mason 		parent = path->nodes[level + 1];
922bb803951SChris Mason 		pslot = path->slots[level + 1];
923a05a9bb1SLi Zefan 	}
924bb803951SChris Mason 
92540689478SChris Mason 	/*
92640689478SChris Mason 	 * deal with the case where there is only one pointer in the root
92740689478SChris Mason 	 * by promoting the node below to a root
92840689478SChris Mason 	 */
9295f39d397SChris Mason 	if (!parent) {
9305f39d397SChris Mason 		struct extent_buffer *child;
931bb803951SChris Mason 
9325f39d397SChris Mason 		if (btrfs_header_nritems(mid) != 1)
933bb803951SChris Mason 			return 0;
934bb803951SChris Mason 
935bb803951SChris Mason 		/* promote the child to a root */
9365f39d397SChris Mason 		child = read_node_slot(root, mid, 0);
9377951f3ceSJeff Mahoney 		BUG_ON(!child);
938925baeddSChris Mason 		btrfs_tree_lock(child);
939b4ce94deSChris Mason 		btrfs_set_lock_blocking(child);
9409fa8cfe7SChris Mason 		ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
941f0486c68SYan, Zheng 		if (ret) {
942f0486c68SYan, Zheng 			btrfs_tree_unlock(child);
943f0486c68SYan, Zheng 			free_extent_buffer(child);
944f0486c68SYan, Zheng 			goto enospc;
945f0486c68SYan, Zheng 		}
9462f375ab9SYan 
947240f62c8SChris Mason 		rcu_assign_pointer(root->node, child);
948925baeddSChris Mason 
9490b86a832SChris Mason 		add_root_to_dirty_list(root);
950925baeddSChris Mason 		btrfs_tree_unlock(child);
951b4ce94deSChris Mason 
952925baeddSChris Mason 		path->locks[level] = 0;
953bb803951SChris Mason 		path->nodes[level] = NULL;
9545f39d397SChris Mason 		clean_tree_block(trans, root, mid);
955925baeddSChris Mason 		btrfs_tree_unlock(mid);
956bb803951SChris Mason 		/* once for the path */
9575f39d397SChris Mason 		free_extent_buffer(mid);
958f0486c68SYan, Zheng 
959f0486c68SYan, Zheng 		root_sub_used(root, mid->len);
96066d7e7f0SArne Jansen 		btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
961bb803951SChris Mason 		/* once for the root ptr */
9625f39d397SChris Mason 		free_extent_buffer(mid);
963f0486c68SYan, Zheng 		return 0;
964bb803951SChris Mason 	}
9655f39d397SChris Mason 	if (btrfs_header_nritems(mid) >
966123abc88SChris Mason 	    BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
967bb803951SChris Mason 		return 0;
968bb803951SChris Mason 
969559af821SAndi Kleen 	btrfs_header_nritems(mid);
97054aa1f4dSChris Mason 
9715f39d397SChris Mason 	left = read_node_slot(root, parent, pslot - 1);
9725f39d397SChris Mason 	if (left) {
973925baeddSChris Mason 		btrfs_tree_lock(left);
974b4ce94deSChris Mason 		btrfs_set_lock_blocking(left);
9755f39d397SChris Mason 		wret = btrfs_cow_block(trans, root, left,
9769fa8cfe7SChris Mason 				       parent, pslot - 1, &left);
97754aa1f4dSChris Mason 		if (wret) {
97854aa1f4dSChris Mason 			ret = wret;
97954aa1f4dSChris Mason 			goto enospc;
98054aa1f4dSChris Mason 		}
9812cc58cf2SChris Mason 	}
9825f39d397SChris Mason 	right = read_node_slot(root, parent, pslot + 1);
9835f39d397SChris Mason 	if (right) {
984925baeddSChris Mason 		btrfs_tree_lock(right);
985b4ce94deSChris Mason 		btrfs_set_lock_blocking(right);
9865f39d397SChris Mason 		wret = btrfs_cow_block(trans, root, right,
9879fa8cfe7SChris Mason 				       parent, pslot + 1, &right);
9882cc58cf2SChris Mason 		if (wret) {
9892cc58cf2SChris Mason 			ret = wret;
9902cc58cf2SChris Mason 			goto enospc;
9912cc58cf2SChris Mason 		}
9922cc58cf2SChris Mason 	}
9932cc58cf2SChris Mason 
9942cc58cf2SChris Mason 	/* first, try to make some room in the middle buffer */
9955f39d397SChris Mason 	if (left) {
9965f39d397SChris Mason 		orig_slot += btrfs_header_nritems(left);
997bce4eae9SChris Mason 		wret = push_node_left(trans, root, left, mid, 1);
99879f95c82SChris Mason 		if (wret < 0)
99979f95c82SChris Mason 			ret = wret;
1000559af821SAndi Kleen 		btrfs_header_nritems(mid);
1001bb803951SChris Mason 	}
100279f95c82SChris Mason 
100379f95c82SChris Mason 	/*
100479f95c82SChris Mason 	 * then try to empty the right most buffer into the middle
100579f95c82SChris Mason 	 */
10065f39d397SChris Mason 	if (right) {
1007971a1f66SChris Mason 		wret = push_node_left(trans, root, mid, right, 1);
100854aa1f4dSChris Mason 		if (wret < 0 && wret != -ENOSPC)
100979f95c82SChris Mason 			ret = wret;
10105f39d397SChris Mason 		if (btrfs_header_nritems(right) == 0) {
10115f39d397SChris Mason 			clean_tree_block(trans, root, right);
1012925baeddSChris Mason 			btrfs_tree_unlock(right);
1013143bede5SJeff Mahoney 			del_ptr(trans, root, path, level + 1, pslot + 1);
1014f0486c68SYan, Zheng 			root_sub_used(root, right->len);
101566d7e7f0SArne Jansen 			btrfs_free_tree_block(trans, root, right, 0, 1, 0);
1016f0486c68SYan, Zheng 			free_extent_buffer(right);
1017f0486c68SYan, Zheng 			right = NULL;
1018bb803951SChris Mason 		} else {
10195f39d397SChris Mason 			struct btrfs_disk_key right_key;
10205f39d397SChris Mason 			btrfs_node_key(right, &right_key, 0);
10215f39d397SChris Mason 			btrfs_set_node_key(parent, &right_key, pslot + 1);
10225f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
1023bb803951SChris Mason 		}
1024bb803951SChris Mason 	}
10255f39d397SChris Mason 	if (btrfs_header_nritems(mid) == 1) {
102679f95c82SChris Mason 		/*
102779f95c82SChris Mason 		 * we're not allowed to leave a node with one item in the
102879f95c82SChris Mason 		 * tree during a delete.  A deletion from lower in the tree
102979f95c82SChris Mason 		 * could try to delete the only pointer in this node.
103079f95c82SChris Mason 		 * So, pull some keys from the left.
103179f95c82SChris Mason 		 * There has to be a left pointer at this point because
103279f95c82SChris Mason 		 * otherwise we would have pulled some pointers from the
103379f95c82SChris Mason 		 * right
103479f95c82SChris Mason 		 */
10355f39d397SChris Mason 		BUG_ON(!left);
10365f39d397SChris Mason 		wret = balance_node_right(trans, root, mid, left);
103754aa1f4dSChris Mason 		if (wret < 0) {
103879f95c82SChris Mason 			ret = wret;
103954aa1f4dSChris Mason 			goto enospc;
104054aa1f4dSChris Mason 		}
1041bce4eae9SChris Mason 		if (wret == 1) {
1042bce4eae9SChris Mason 			wret = push_node_left(trans, root, left, mid, 1);
1043bce4eae9SChris Mason 			if (wret < 0)
1044bce4eae9SChris Mason 				ret = wret;
1045bce4eae9SChris Mason 		}
104679f95c82SChris Mason 		BUG_ON(wret == 1);
104779f95c82SChris Mason 	}
10485f39d397SChris Mason 	if (btrfs_header_nritems(mid) == 0) {
10495f39d397SChris Mason 		clean_tree_block(trans, root, mid);
1050925baeddSChris Mason 		btrfs_tree_unlock(mid);
1051143bede5SJeff Mahoney 		del_ptr(trans, root, path, level + 1, pslot);
1052f0486c68SYan, Zheng 		root_sub_used(root, mid->len);
105366d7e7f0SArne Jansen 		btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
1054f0486c68SYan, Zheng 		free_extent_buffer(mid);
1055f0486c68SYan, Zheng 		mid = NULL;
105679f95c82SChris Mason 	} else {
105779f95c82SChris Mason 		/* update the parent key to reflect our changes */
10585f39d397SChris Mason 		struct btrfs_disk_key mid_key;
10595f39d397SChris Mason 		btrfs_node_key(mid, &mid_key, 0);
10605f39d397SChris Mason 		btrfs_set_node_key(parent, &mid_key, pslot);
10615f39d397SChris Mason 		btrfs_mark_buffer_dirty(parent);
106279f95c82SChris Mason 	}
1063bb803951SChris Mason 
106479f95c82SChris Mason 	/* update the path */
10655f39d397SChris Mason 	if (left) {
10665f39d397SChris Mason 		if (btrfs_header_nritems(left) > orig_slot) {
10675f39d397SChris Mason 			extent_buffer_get(left);
1068925baeddSChris Mason 			/* left was locked after cow */
10695f39d397SChris Mason 			path->nodes[level] = left;
1070bb803951SChris Mason 			path->slots[level + 1] -= 1;
1071bb803951SChris Mason 			path->slots[level] = orig_slot;
1072925baeddSChris Mason 			if (mid) {
1073925baeddSChris Mason 				btrfs_tree_unlock(mid);
10745f39d397SChris Mason 				free_extent_buffer(mid);
1075925baeddSChris Mason 			}
1076bb803951SChris Mason 		} else {
10775f39d397SChris Mason 			orig_slot -= btrfs_header_nritems(left);
1078bb803951SChris Mason 			path->slots[level] = orig_slot;
1079bb803951SChris Mason 		}
1080bb803951SChris Mason 	}
108179f95c82SChris Mason 	/* double check we haven't messed things up */
1082e20d96d6SChris Mason 	if (orig_ptr !=
10835f39d397SChris Mason 	    btrfs_node_blockptr(path->nodes[level], path->slots[level]))
108479f95c82SChris Mason 		BUG();
108554aa1f4dSChris Mason enospc:
1086925baeddSChris Mason 	if (right) {
1087925baeddSChris Mason 		btrfs_tree_unlock(right);
10885f39d397SChris Mason 		free_extent_buffer(right);
1089925baeddSChris Mason 	}
1090925baeddSChris Mason 	if (left) {
1091925baeddSChris Mason 		if (path->nodes[level] != left)
1092925baeddSChris Mason 			btrfs_tree_unlock(left);
10935f39d397SChris Mason 		free_extent_buffer(left);
1094925baeddSChris Mason 	}
1095bb803951SChris Mason 	return ret;
1096bb803951SChris Mason }
1097bb803951SChris Mason 
1098d352ac68SChris Mason /* Node balancing for insertion.  Here we only split or push nodes around
1099d352ac68SChris Mason  * when they are completely full.  This is also done top down, so we
1100d352ac68SChris Mason  * have to be pessimistic.
1101d352ac68SChris Mason  */
1102d397712bSChris Mason static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1103e66f709bSChris Mason 					  struct btrfs_root *root,
1104e66f709bSChris Mason 					  struct btrfs_path *path, int level)
1105e66f709bSChris Mason {
11065f39d397SChris Mason 	struct extent_buffer *right = NULL;
11075f39d397SChris Mason 	struct extent_buffer *mid;
11085f39d397SChris Mason 	struct extent_buffer *left = NULL;
11095f39d397SChris Mason 	struct extent_buffer *parent = NULL;
1110e66f709bSChris Mason 	int ret = 0;
1111e66f709bSChris Mason 	int wret;
1112e66f709bSChris Mason 	int pslot;
1113e66f709bSChris Mason 	int orig_slot = path->slots[level];
1114e66f709bSChris Mason 
1115e66f709bSChris Mason 	if (level == 0)
1116e66f709bSChris Mason 		return 1;
1117e66f709bSChris Mason 
11185f39d397SChris Mason 	mid = path->nodes[level];
11197bb86316SChris Mason 	WARN_ON(btrfs_header_generation(mid) != trans->transid);
1120e66f709bSChris Mason 
1121a05a9bb1SLi Zefan 	if (level < BTRFS_MAX_LEVEL - 1) {
11225f39d397SChris Mason 		parent = path->nodes[level + 1];
1123e66f709bSChris Mason 		pslot = path->slots[level + 1];
1124a05a9bb1SLi Zefan 	}
1125e66f709bSChris Mason 
11265f39d397SChris Mason 	if (!parent)
1127e66f709bSChris Mason 		return 1;
1128e66f709bSChris Mason 
11295f39d397SChris Mason 	left = read_node_slot(root, parent, pslot - 1);
1130e66f709bSChris Mason 
1131e66f709bSChris Mason 	/* first, try to make some room in the middle buffer */
11325f39d397SChris Mason 	if (left) {
1133e66f709bSChris Mason 		u32 left_nr;
1134925baeddSChris Mason 
1135925baeddSChris Mason 		btrfs_tree_lock(left);
1136b4ce94deSChris Mason 		btrfs_set_lock_blocking(left);
1137b4ce94deSChris Mason 
11385f39d397SChris Mason 		left_nr = btrfs_header_nritems(left);
113933ade1f8SChris Mason 		if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
114033ade1f8SChris Mason 			wret = 1;
114133ade1f8SChris Mason 		} else {
11425f39d397SChris Mason 			ret = btrfs_cow_block(trans, root, left, parent,
11439fa8cfe7SChris Mason 					      pslot - 1, &left);
114454aa1f4dSChris Mason 			if (ret)
114554aa1f4dSChris Mason 				wret = 1;
114654aa1f4dSChris Mason 			else {
114754aa1f4dSChris Mason 				wret = push_node_left(trans, root,
1148971a1f66SChris Mason 						      left, mid, 0);
114954aa1f4dSChris Mason 			}
115033ade1f8SChris Mason 		}
1151e66f709bSChris Mason 		if (wret < 0)
1152e66f709bSChris Mason 			ret = wret;
1153e66f709bSChris Mason 		if (wret == 0) {
11545f39d397SChris Mason 			struct btrfs_disk_key disk_key;
1155e66f709bSChris Mason 			orig_slot += left_nr;
11565f39d397SChris Mason 			btrfs_node_key(mid, &disk_key, 0);
11575f39d397SChris Mason 			btrfs_set_node_key(parent, &disk_key, pslot);
11585f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
11595f39d397SChris Mason 			if (btrfs_header_nritems(left) > orig_slot) {
11605f39d397SChris Mason 				path->nodes[level] = left;
1161e66f709bSChris Mason 				path->slots[level + 1] -= 1;
1162e66f709bSChris Mason 				path->slots[level] = orig_slot;
1163925baeddSChris Mason 				btrfs_tree_unlock(mid);
11645f39d397SChris Mason 				free_extent_buffer(mid);
1165e66f709bSChris Mason 			} else {
1166e66f709bSChris Mason 				orig_slot -=
11675f39d397SChris Mason 					btrfs_header_nritems(left);
1168e66f709bSChris Mason 				path->slots[level] = orig_slot;
1169925baeddSChris Mason 				btrfs_tree_unlock(left);
11705f39d397SChris Mason 				free_extent_buffer(left);
1171e66f709bSChris Mason 			}
1172e66f709bSChris Mason 			return 0;
1173e66f709bSChris Mason 		}
1174925baeddSChris Mason 		btrfs_tree_unlock(left);
11755f39d397SChris Mason 		free_extent_buffer(left);
1176e66f709bSChris Mason 	}
11775f39d397SChris Mason 	right = read_node_slot(root, parent, pslot + 1);
1178e66f709bSChris Mason 
1179e66f709bSChris Mason 	/*
1180e66f709bSChris Mason 	 * then try to empty the right most buffer into the middle
1181e66f709bSChris Mason 	 */
11825f39d397SChris Mason 	if (right) {
118333ade1f8SChris Mason 		u32 right_nr;
1184b4ce94deSChris Mason 
1185925baeddSChris Mason 		btrfs_tree_lock(right);
1186b4ce94deSChris Mason 		btrfs_set_lock_blocking(right);
1187b4ce94deSChris Mason 
11885f39d397SChris Mason 		right_nr = btrfs_header_nritems(right);
118933ade1f8SChris Mason 		if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
119033ade1f8SChris Mason 			wret = 1;
119133ade1f8SChris Mason 		} else {
11925f39d397SChris Mason 			ret = btrfs_cow_block(trans, root, right,
11935f39d397SChris Mason 					      parent, pslot + 1,
11949fa8cfe7SChris Mason 					      &right);
119554aa1f4dSChris Mason 			if (ret)
119654aa1f4dSChris Mason 				wret = 1;
119754aa1f4dSChris Mason 			else {
119833ade1f8SChris Mason 				wret = balance_node_right(trans, root,
11995f39d397SChris Mason 							  right, mid);
120033ade1f8SChris Mason 			}
120154aa1f4dSChris Mason 		}
1202e66f709bSChris Mason 		if (wret < 0)
1203e66f709bSChris Mason 			ret = wret;
1204e66f709bSChris Mason 		if (wret == 0) {
12055f39d397SChris Mason 			struct btrfs_disk_key disk_key;
12065f39d397SChris Mason 
12075f39d397SChris Mason 			btrfs_node_key(right, &disk_key, 0);
12085f39d397SChris Mason 			btrfs_set_node_key(parent, &disk_key, pslot + 1);
12095f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
12105f39d397SChris Mason 
12115f39d397SChris Mason 			if (btrfs_header_nritems(mid) <= orig_slot) {
12125f39d397SChris Mason 				path->nodes[level] = right;
1213e66f709bSChris Mason 				path->slots[level + 1] += 1;
1214e66f709bSChris Mason 				path->slots[level] = orig_slot -
12155f39d397SChris Mason 					btrfs_header_nritems(mid);
1216925baeddSChris Mason 				btrfs_tree_unlock(mid);
12175f39d397SChris Mason 				free_extent_buffer(mid);
1218e66f709bSChris Mason 			} else {
1219925baeddSChris Mason 				btrfs_tree_unlock(right);
12205f39d397SChris Mason 				free_extent_buffer(right);
1221e66f709bSChris Mason 			}
1222e66f709bSChris Mason 			return 0;
1223e66f709bSChris Mason 		}
1224925baeddSChris Mason 		btrfs_tree_unlock(right);
12255f39d397SChris Mason 		free_extent_buffer(right);
1226e66f709bSChris Mason 	}
1227e66f709bSChris Mason 	return 1;
1228e66f709bSChris Mason }
1229e66f709bSChris Mason 
123074123bd7SChris Mason /*
1231d352ac68SChris Mason  * readahead one full node of leaves, finding things that are close
1232d352ac68SChris Mason  * to the block in 'slot', and triggering ra on them.
12333c69faecSChris Mason  */
1234c8c42864SChris Mason static void reada_for_search(struct btrfs_root *root,
1235e02119d5SChris Mason 			     struct btrfs_path *path,
123601f46658SChris Mason 			     int level, int slot, u64 objectid)
12373c69faecSChris Mason {
12385f39d397SChris Mason 	struct extent_buffer *node;
123901f46658SChris Mason 	struct btrfs_disk_key disk_key;
12403c69faecSChris Mason 	u32 nritems;
12413c69faecSChris Mason 	u64 search;
1242a7175319SChris Mason 	u64 target;
12436b80053dSChris Mason 	u64 nread = 0;
1244cb25c2eaSJosef Bacik 	u64 gen;
12453c69faecSChris Mason 	int direction = path->reada;
12465f39d397SChris Mason 	struct extent_buffer *eb;
12476b80053dSChris Mason 	u32 nr;
12486b80053dSChris Mason 	u32 blocksize;
12496b80053dSChris Mason 	u32 nscan = 0;
1250db94535dSChris Mason 
1251a6b6e75eSChris Mason 	if (level != 1)
12523c69faecSChris Mason 		return;
12533c69faecSChris Mason 
12546702ed49SChris Mason 	if (!path->nodes[level])
12556702ed49SChris Mason 		return;
12566702ed49SChris Mason 
12575f39d397SChris Mason 	node = path->nodes[level];
1258925baeddSChris Mason 
12593c69faecSChris Mason 	search = btrfs_node_blockptr(node, slot);
12606b80053dSChris Mason 	blocksize = btrfs_level_size(root, level - 1);
12616b80053dSChris Mason 	eb = btrfs_find_tree_block(root, search, blocksize);
12625f39d397SChris Mason 	if (eb) {
12635f39d397SChris Mason 		free_extent_buffer(eb);
12643c69faecSChris Mason 		return;
12653c69faecSChris Mason 	}
12663c69faecSChris Mason 
1267a7175319SChris Mason 	target = search;
12686b80053dSChris Mason 
12695f39d397SChris Mason 	nritems = btrfs_header_nritems(node);
12706b80053dSChris Mason 	nr = slot;
127125b8b936SJosef Bacik 
12723c69faecSChris Mason 	while (1) {
12736b80053dSChris Mason 		if (direction < 0) {
12746b80053dSChris Mason 			if (nr == 0)
12753c69faecSChris Mason 				break;
12766b80053dSChris Mason 			nr--;
12776b80053dSChris Mason 		} else if (direction > 0) {
12786b80053dSChris Mason 			nr++;
12796b80053dSChris Mason 			if (nr >= nritems)
12806b80053dSChris Mason 				break;
12813c69faecSChris Mason 		}
128201f46658SChris Mason 		if (path->reada < 0 && objectid) {
128301f46658SChris Mason 			btrfs_node_key(node, &disk_key, nr);
128401f46658SChris Mason 			if (btrfs_disk_key_objectid(&disk_key) != objectid)
128501f46658SChris Mason 				break;
128601f46658SChris Mason 		}
12876b80053dSChris Mason 		search = btrfs_node_blockptr(node, nr);
1288a7175319SChris Mason 		if ((search <= target && target - search <= 65536) ||
1289a7175319SChris Mason 		    (search > target && search - target <= 65536)) {
1290cb25c2eaSJosef Bacik 			gen = btrfs_node_ptr_generation(node, nr);
1291cb25c2eaSJosef Bacik 			readahead_tree_block(root, search, blocksize, gen);
12926b80053dSChris Mason 			nread += blocksize;
12933c69faecSChris Mason 		}
12946b80053dSChris Mason 		nscan++;
1295a7175319SChris Mason 		if ((nread > 65536 || nscan > 32))
12966b80053dSChris Mason 			break;
12973c69faecSChris Mason 	}
12983c69faecSChris Mason }
1299925baeddSChris Mason 
1300d352ac68SChris Mason /*
1301b4ce94deSChris Mason  * returns -EAGAIN if it had to drop the path, or zero if everything was in
1302b4ce94deSChris Mason  * cache
1303b4ce94deSChris Mason  */
1304b4ce94deSChris Mason static noinline int reada_for_balance(struct btrfs_root *root,
1305b4ce94deSChris Mason 				      struct btrfs_path *path, int level)
1306b4ce94deSChris Mason {
1307b4ce94deSChris Mason 	int slot;
1308b4ce94deSChris Mason 	int nritems;
1309b4ce94deSChris Mason 	struct extent_buffer *parent;
1310b4ce94deSChris Mason 	struct extent_buffer *eb;
1311b4ce94deSChris Mason 	u64 gen;
1312b4ce94deSChris Mason 	u64 block1 = 0;
1313b4ce94deSChris Mason 	u64 block2 = 0;
1314b4ce94deSChris Mason 	int ret = 0;
1315b4ce94deSChris Mason 	int blocksize;
1316b4ce94deSChris Mason 
13178c594ea8SChris Mason 	parent = path->nodes[level + 1];
1318b4ce94deSChris Mason 	if (!parent)
1319b4ce94deSChris Mason 		return 0;
1320b4ce94deSChris Mason 
1321b4ce94deSChris Mason 	nritems = btrfs_header_nritems(parent);
13228c594ea8SChris Mason 	slot = path->slots[level + 1];
1323b4ce94deSChris Mason 	blocksize = btrfs_level_size(root, level);
1324b4ce94deSChris Mason 
1325b4ce94deSChris Mason 	if (slot > 0) {
1326b4ce94deSChris Mason 		block1 = btrfs_node_blockptr(parent, slot - 1);
1327b4ce94deSChris Mason 		gen = btrfs_node_ptr_generation(parent, slot - 1);
1328b4ce94deSChris Mason 		eb = btrfs_find_tree_block(root, block1, blocksize);
1329b4ce94deSChris Mason 		if (eb && btrfs_buffer_uptodate(eb, gen))
1330b4ce94deSChris Mason 			block1 = 0;
1331b4ce94deSChris Mason 		free_extent_buffer(eb);
1332b4ce94deSChris Mason 	}
13338c594ea8SChris Mason 	if (slot + 1 < nritems) {
1334b4ce94deSChris Mason 		block2 = btrfs_node_blockptr(parent, slot + 1);
1335b4ce94deSChris Mason 		gen = btrfs_node_ptr_generation(parent, slot + 1);
1336b4ce94deSChris Mason 		eb = btrfs_find_tree_block(root, block2, blocksize);
1337b4ce94deSChris Mason 		if (eb && btrfs_buffer_uptodate(eb, gen))
1338b4ce94deSChris Mason 			block2 = 0;
1339b4ce94deSChris Mason 		free_extent_buffer(eb);
1340b4ce94deSChris Mason 	}
1341b4ce94deSChris Mason 	if (block1 || block2) {
1342b4ce94deSChris Mason 		ret = -EAGAIN;
13438c594ea8SChris Mason 
13448c594ea8SChris Mason 		/* release the whole path */
1345b3b4aa74SDavid Sterba 		btrfs_release_path(path);
13468c594ea8SChris Mason 
13478c594ea8SChris Mason 		/* read the blocks */
1348b4ce94deSChris Mason 		if (block1)
1349b4ce94deSChris Mason 			readahead_tree_block(root, block1, blocksize, 0);
1350b4ce94deSChris Mason 		if (block2)
1351b4ce94deSChris Mason 			readahead_tree_block(root, block2, blocksize, 0);
1352b4ce94deSChris Mason 
1353b4ce94deSChris Mason 		if (block1) {
1354b4ce94deSChris Mason 			eb = read_tree_block(root, block1, blocksize, 0);
1355b4ce94deSChris Mason 			free_extent_buffer(eb);
1356b4ce94deSChris Mason 		}
13578c594ea8SChris Mason 		if (block2) {
1358b4ce94deSChris Mason 			eb = read_tree_block(root, block2, blocksize, 0);
1359b4ce94deSChris Mason 			free_extent_buffer(eb);
1360b4ce94deSChris Mason 		}
1361b4ce94deSChris Mason 	}
1362b4ce94deSChris Mason 	return ret;
1363b4ce94deSChris Mason }
1364b4ce94deSChris Mason 
1365b4ce94deSChris Mason 
1366b4ce94deSChris Mason /*
1367d397712bSChris Mason  * when we walk down the tree, it is usually safe to unlock the higher layers
1368d397712bSChris Mason  * in the tree.  The exceptions are when our path goes through slot 0, because
1369d397712bSChris Mason  * operations on the tree might require changing key pointers higher up in the
1370d397712bSChris Mason  * tree.
1371d352ac68SChris Mason  *
1372d397712bSChris Mason  * callers might also have set path->keep_locks, which tells this code to keep
1373d397712bSChris Mason  * the lock if the path points to the last slot in the block.  This is part of
1374d397712bSChris Mason  * walking through the tree, and selecting the next slot in the higher block.
1375d352ac68SChris Mason  *
1376d397712bSChris Mason  * lowest_unlock sets the lowest level in the tree we're allowed to unlock.  so
1377d397712bSChris Mason  * if lowest_unlock is 1, level 0 won't be unlocked
1378d352ac68SChris Mason  */
1379e02119d5SChris Mason static noinline void unlock_up(struct btrfs_path *path, int level,
1380e02119d5SChris Mason 			       int lowest_unlock)
1381925baeddSChris Mason {
1382925baeddSChris Mason 	int i;
1383925baeddSChris Mason 	int skip_level = level;
1384051e1b9fSChris Mason 	int no_skips = 0;
1385925baeddSChris Mason 	struct extent_buffer *t;
1386925baeddSChris Mason 
1387925baeddSChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1388925baeddSChris Mason 		if (!path->nodes[i])
1389925baeddSChris Mason 			break;
1390925baeddSChris Mason 		if (!path->locks[i])
1391925baeddSChris Mason 			break;
1392051e1b9fSChris Mason 		if (!no_skips && path->slots[i] == 0) {
1393925baeddSChris Mason 			skip_level = i + 1;
1394925baeddSChris Mason 			continue;
1395925baeddSChris Mason 		}
1396051e1b9fSChris Mason 		if (!no_skips && path->keep_locks) {
1397925baeddSChris Mason 			u32 nritems;
1398925baeddSChris Mason 			t = path->nodes[i];
1399925baeddSChris Mason 			nritems = btrfs_header_nritems(t);
1400051e1b9fSChris Mason 			if (nritems < 1 || path->slots[i] >= nritems - 1) {
1401925baeddSChris Mason 				skip_level = i + 1;
1402925baeddSChris Mason 				continue;
1403925baeddSChris Mason 			}
1404925baeddSChris Mason 		}
1405051e1b9fSChris Mason 		if (skip_level < i && i >= lowest_unlock)
1406051e1b9fSChris Mason 			no_skips = 1;
1407051e1b9fSChris Mason 
1408925baeddSChris Mason 		t = path->nodes[i];
1409925baeddSChris Mason 		if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1410bd681513SChris Mason 			btrfs_tree_unlock_rw(t, path->locks[i]);
1411925baeddSChris Mason 			path->locks[i] = 0;
1412925baeddSChris Mason 		}
1413925baeddSChris Mason 	}
1414925baeddSChris Mason }
1415925baeddSChris Mason 
14163c69faecSChris Mason /*
1417b4ce94deSChris Mason  * This releases any locks held in the path starting at level and
1418b4ce94deSChris Mason  * going all the way up to the root.
1419b4ce94deSChris Mason  *
1420b4ce94deSChris Mason  * btrfs_search_slot will keep the lock held on higher nodes in a few
1421b4ce94deSChris Mason  * corner cases, such as COW of the block at slot zero in the node.  This
1422b4ce94deSChris Mason  * ignores those rules, and it should only be called when there are no
1423b4ce94deSChris Mason  * more updates to be done higher up in the tree.
1424b4ce94deSChris Mason  */
1425b4ce94deSChris Mason noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1426b4ce94deSChris Mason {
1427b4ce94deSChris Mason 	int i;
1428b4ce94deSChris Mason 
14295d4f98a2SYan Zheng 	if (path->keep_locks)
1430b4ce94deSChris Mason 		return;
1431b4ce94deSChris Mason 
1432b4ce94deSChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1433b4ce94deSChris Mason 		if (!path->nodes[i])
143412f4daccSChris Mason 			continue;
1435b4ce94deSChris Mason 		if (!path->locks[i])
143612f4daccSChris Mason 			continue;
1437bd681513SChris Mason 		btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
1438b4ce94deSChris Mason 		path->locks[i] = 0;
1439b4ce94deSChris Mason 	}
1440b4ce94deSChris Mason }
1441b4ce94deSChris Mason 
1442b4ce94deSChris Mason /*
1443c8c42864SChris Mason  * helper function for btrfs_search_slot.  The goal is to find a block
1444c8c42864SChris Mason  * in cache without setting the path to blocking.  If we find the block
1445c8c42864SChris Mason  * we return zero and the path is unchanged.
1446c8c42864SChris Mason  *
1447c8c42864SChris Mason  * If we can't find the block, we set the path blocking and do some
1448c8c42864SChris Mason  * reada.  -EAGAIN is returned and the search must be repeated.
1449c8c42864SChris Mason  */
1450c8c42864SChris Mason static int
1451c8c42864SChris Mason read_block_for_search(struct btrfs_trans_handle *trans,
1452c8c42864SChris Mason 		       struct btrfs_root *root, struct btrfs_path *p,
1453c8c42864SChris Mason 		       struct extent_buffer **eb_ret, int level, int slot,
1454c8c42864SChris Mason 		       struct btrfs_key *key)
1455c8c42864SChris Mason {
1456c8c42864SChris Mason 	u64 blocknr;
1457c8c42864SChris Mason 	u64 gen;
1458c8c42864SChris Mason 	u32 blocksize;
1459c8c42864SChris Mason 	struct extent_buffer *b = *eb_ret;
1460c8c42864SChris Mason 	struct extent_buffer *tmp;
146176a05b35SChris Mason 	int ret;
1462c8c42864SChris Mason 
1463c8c42864SChris Mason 	blocknr = btrfs_node_blockptr(b, slot);
1464c8c42864SChris Mason 	gen = btrfs_node_ptr_generation(b, slot);
1465c8c42864SChris Mason 	blocksize = btrfs_level_size(root, level - 1);
1466c8c42864SChris Mason 
1467c8c42864SChris Mason 	tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1468cb44921aSChris Mason 	if (tmp) {
1469cb44921aSChris Mason 		if (btrfs_buffer_uptodate(tmp, 0)) {
1470cb44921aSChris Mason 			if (btrfs_buffer_uptodate(tmp, gen)) {
147176a05b35SChris Mason 				/*
1472cb44921aSChris Mason 				 * we found an up to date block without
1473cb44921aSChris Mason 				 * sleeping, return
147476a05b35SChris Mason 				 * right away
147576a05b35SChris Mason 				 */
1476c8c42864SChris Mason 				*eb_ret = tmp;
1477c8c42864SChris Mason 				return 0;
1478c8c42864SChris Mason 			}
1479cb44921aSChris Mason 			/* the pages were up to date, but we failed
1480cb44921aSChris Mason 			 * the generation number check.  Do a full
1481cb44921aSChris Mason 			 * read for the generation number that is correct.
1482cb44921aSChris Mason 			 * We must do this without dropping locks so
1483cb44921aSChris Mason 			 * we can trust our generation number
1484cb44921aSChris Mason 			 */
1485cb44921aSChris Mason 			free_extent_buffer(tmp);
1486bd681513SChris Mason 			btrfs_set_path_blocking(p);
1487bd681513SChris Mason 
1488cb44921aSChris Mason 			tmp = read_tree_block(root, blocknr, blocksize, gen);
1489cb44921aSChris Mason 			if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1490cb44921aSChris Mason 				*eb_ret = tmp;
1491cb44921aSChris Mason 				return 0;
1492cb44921aSChris Mason 			}
1493cb44921aSChris Mason 			free_extent_buffer(tmp);
1494b3b4aa74SDavid Sterba 			btrfs_release_path(p);
1495cb44921aSChris Mason 			return -EIO;
1496cb44921aSChris Mason 		}
1497cb44921aSChris Mason 	}
1498c8c42864SChris Mason 
1499c8c42864SChris Mason 	/*
1500c8c42864SChris Mason 	 * reduce lock contention at high levels
1501c8c42864SChris Mason 	 * of the btree by dropping locks before
150276a05b35SChris Mason 	 * we read.  Don't release the lock on the current
150376a05b35SChris Mason 	 * level because we need to walk this node to figure
150476a05b35SChris Mason 	 * out which blocks to read.
1505c8c42864SChris Mason 	 */
15068c594ea8SChris Mason 	btrfs_unlock_up_safe(p, level + 1);
15078c594ea8SChris Mason 	btrfs_set_path_blocking(p);
15088c594ea8SChris Mason 
1509c8c42864SChris Mason 	free_extent_buffer(tmp);
1510c8c42864SChris Mason 	if (p->reada)
1511c8c42864SChris Mason 		reada_for_search(root, p, level, slot, key->objectid);
1512c8c42864SChris Mason 
1513b3b4aa74SDavid Sterba 	btrfs_release_path(p);
151476a05b35SChris Mason 
151576a05b35SChris Mason 	ret = -EAGAIN;
15165bdd3536SYan, Zheng 	tmp = read_tree_block(root, blocknr, blocksize, 0);
151776a05b35SChris Mason 	if (tmp) {
151876a05b35SChris Mason 		/*
151976a05b35SChris Mason 		 * If the read above didn't mark this buffer up to date,
152076a05b35SChris Mason 		 * it will never end up being up to date.  Set ret to EIO now
152176a05b35SChris Mason 		 * and give up so that our caller doesn't loop forever
152276a05b35SChris Mason 		 * on our EAGAINs.
152376a05b35SChris Mason 		 */
152476a05b35SChris Mason 		if (!btrfs_buffer_uptodate(tmp, 0))
152576a05b35SChris Mason 			ret = -EIO;
1526c8c42864SChris Mason 		free_extent_buffer(tmp);
152776a05b35SChris Mason 	}
152876a05b35SChris Mason 	return ret;
1529c8c42864SChris Mason }
1530c8c42864SChris Mason 
1531c8c42864SChris Mason /*
1532c8c42864SChris Mason  * helper function for btrfs_search_slot.  This does all of the checks
1533c8c42864SChris Mason  * for node-level blocks and does any balancing required based on
1534c8c42864SChris Mason  * the ins_len.
1535c8c42864SChris Mason  *
1536c8c42864SChris Mason  * If no extra work was required, zero is returned.  If we had to
1537c8c42864SChris Mason  * drop the path, -EAGAIN is returned and btrfs_search_slot must
1538c8c42864SChris Mason  * start over
1539c8c42864SChris Mason  */
1540c8c42864SChris Mason static int
1541c8c42864SChris Mason setup_nodes_for_search(struct btrfs_trans_handle *trans,
1542c8c42864SChris Mason 		       struct btrfs_root *root, struct btrfs_path *p,
1543bd681513SChris Mason 		       struct extent_buffer *b, int level, int ins_len,
1544bd681513SChris Mason 		       int *write_lock_level)
1545c8c42864SChris Mason {
1546c8c42864SChris Mason 	int ret;
1547c8c42864SChris Mason 	if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1548c8c42864SChris Mason 	    BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1549c8c42864SChris Mason 		int sret;
1550c8c42864SChris Mason 
1551bd681513SChris Mason 		if (*write_lock_level < level + 1) {
1552bd681513SChris Mason 			*write_lock_level = level + 1;
1553bd681513SChris Mason 			btrfs_release_path(p);
1554bd681513SChris Mason 			goto again;
1555bd681513SChris Mason 		}
1556bd681513SChris Mason 
1557c8c42864SChris Mason 		sret = reada_for_balance(root, p, level);
1558c8c42864SChris Mason 		if (sret)
1559c8c42864SChris Mason 			goto again;
1560c8c42864SChris Mason 
1561c8c42864SChris Mason 		btrfs_set_path_blocking(p);
1562c8c42864SChris Mason 		sret = split_node(trans, root, p, level);
1563bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
1564c8c42864SChris Mason 
1565c8c42864SChris Mason 		BUG_ON(sret > 0);
1566c8c42864SChris Mason 		if (sret) {
1567c8c42864SChris Mason 			ret = sret;
1568c8c42864SChris Mason 			goto done;
1569c8c42864SChris Mason 		}
1570c8c42864SChris Mason 		b = p->nodes[level];
1571c8c42864SChris Mason 	} else if (ins_len < 0 && btrfs_header_nritems(b) <
1572cfbb9308SChris Mason 		   BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
1573c8c42864SChris Mason 		int sret;
1574c8c42864SChris Mason 
1575bd681513SChris Mason 		if (*write_lock_level < level + 1) {
1576bd681513SChris Mason 			*write_lock_level = level + 1;
1577bd681513SChris Mason 			btrfs_release_path(p);
1578bd681513SChris Mason 			goto again;
1579bd681513SChris Mason 		}
1580bd681513SChris Mason 
1581c8c42864SChris Mason 		sret = reada_for_balance(root, p, level);
1582c8c42864SChris Mason 		if (sret)
1583c8c42864SChris Mason 			goto again;
1584c8c42864SChris Mason 
1585c8c42864SChris Mason 		btrfs_set_path_blocking(p);
1586c8c42864SChris Mason 		sret = balance_level(trans, root, p, level);
1587bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
1588c8c42864SChris Mason 
1589c8c42864SChris Mason 		if (sret) {
1590c8c42864SChris Mason 			ret = sret;
1591c8c42864SChris Mason 			goto done;
1592c8c42864SChris Mason 		}
1593c8c42864SChris Mason 		b = p->nodes[level];
1594c8c42864SChris Mason 		if (!b) {
1595b3b4aa74SDavid Sterba 			btrfs_release_path(p);
1596c8c42864SChris Mason 			goto again;
1597c8c42864SChris Mason 		}
1598c8c42864SChris Mason 		BUG_ON(btrfs_header_nritems(b) == 1);
1599c8c42864SChris Mason 	}
1600c8c42864SChris Mason 	return 0;
1601c8c42864SChris Mason 
1602c8c42864SChris Mason again:
1603c8c42864SChris Mason 	ret = -EAGAIN;
1604c8c42864SChris Mason done:
1605c8c42864SChris Mason 	return ret;
1606c8c42864SChris Mason }
1607c8c42864SChris Mason 
1608c8c42864SChris Mason /*
160974123bd7SChris Mason  * look for key in the tree.  path is filled in with nodes along the way
161074123bd7SChris Mason  * if key is found, we return zero and you can find the item in the leaf
161174123bd7SChris Mason  * level of the path (level 0)
161274123bd7SChris Mason  *
161374123bd7SChris Mason  * If the key isn't found, the path points to the slot where it should
1614aa5d6bedSChris Mason  * be inserted, and 1 is returned.  If there are other errors during the
1615aa5d6bedSChris Mason  * search a negative error number is returned.
161697571fd0SChris Mason  *
161797571fd0SChris Mason  * if ins_len > 0, nodes and leaves will be split as we walk down the
161897571fd0SChris Mason  * tree.  if ins_len < 0, nodes will be merged as we walk down the tree (if
161997571fd0SChris Mason  * possible)
162074123bd7SChris Mason  */
1621e089f05cSChris Mason int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1622e089f05cSChris Mason 		      *root, struct btrfs_key *key, struct btrfs_path *p, int
1623e089f05cSChris Mason 		      ins_len, int cow)
1624be0e5c09SChris Mason {
16255f39d397SChris Mason 	struct extent_buffer *b;
1626be0e5c09SChris Mason 	int slot;
1627be0e5c09SChris Mason 	int ret;
162833c66f43SYan Zheng 	int err;
1629be0e5c09SChris Mason 	int level;
1630925baeddSChris Mason 	int lowest_unlock = 1;
1631bd681513SChris Mason 	int root_lock;
1632bd681513SChris Mason 	/* everything at write_lock_level or lower must be write locked */
1633bd681513SChris Mason 	int write_lock_level = 0;
16349f3a7427SChris Mason 	u8 lowest_level = 0;
16359f3a7427SChris Mason 
16366702ed49SChris Mason 	lowest_level = p->lowest_level;
1637323ac95bSChris Mason 	WARN_ON(lowest_level && ins_len > 0);
163822b0ebdaSChris Mason 	WARN_ON(p->nodes[0] != NULL);
163925179201SJosef Bacik 
1640bd681513SChris Mason 	if (ins_len < 0) {
1641925baeddSChris Mason 		lowest_unlock = 2;
164265b51a00SChris Mason 
1643bd681513SChris Mason 		/* when we are removing items, we might have to go up to level
1644bd681513SChris Mason 		 * two as we update tree pointers  Make sure we keep write
1645bd681513SChris Mason 		 * for those levels as well
1646bd681513SChris Mason 		 */
1647bd681513SChris Mason 		write_lock_level = 2;
1648bd681513SChris Mason 	} else if (ins_len > 0) {
1649bd681513SChris Mason 		/*
1650bd681513SChris Mason 		 * for inserting items, make sure we have a write lock on
1651bd681513SChris Mason 		 * level 1 so we can update keys
1652bd681513SChris Mason 		 */
1653bd681513SChris Mason 		write_lock_level = 1;
1654bd681513SChris Mason 	}
1655bd681513SChris Mason 
1656bd681513SChris Mason 	if (!cow)
1657bd681513SChris Mason 		write_lock_level = -1;
1658bd681513SChris Mason 
1659bd681513SChris Mason 	if (cow && (p->keep_locks || p->lowest_level))
1660bd681513SChris Mason 		write_lock_level = BTRFS_MAX_LEVEL;
1661bd681513SChris Mason 
1662bb803951SChris Mason again:
1663bd681513SChris Mason 	/*
1664bd681513SChris Mason 	 * we try very hard to do read locks on the root
1665bd681513SChris Mason 	 */
1666bd681513SChris Mason 	root_lock = BTRFS_READ_LOCK;
1667bd681513SChris Mason 	level = 0;
16685d4f98a2SYan Zheng 	if (p->search_commit_root) {
1669bd681513SChris Mason 		/*
1670bd681513SChris Mason 		 * the commit roots are read only
1671bd681513SChris Mason 		 * so we always do read locks
1672bd681513SChris Mason 		 */
16735d4f98a2SYan Zheng 		b = root->commit_root;
16745d4f98a2SYan Zheng 		extent_buffer_get(b);
1675bd681513SChris Mason 		level = btrfs_header_level(b);
16765d4f98a2SYan Zheng 		if (!p->skip_locking)
1677bd681513SChris Mason 			btrfs_tree_read_lock(b);
16785d4f98a2SYan Zheng 	} else {
1679bd681513SChris Mason 		if (p->skip_locking) {
16805cd57b2cSChris Mason 			b = btrfs_root_node(root);
1681bd681513SChris Mason 			level = btrfs_header_level(b);
1682bd681513SChris Mason 		} else {
1683bd681513SChris Mason 			/* we don't know the level of the root node
1684bd681513SChris Mason 			 * until we actually have it read locked
1685bd681513SChris Mason 			 */
1686bd681513SChris Mason 			b = btrfs_read_lock_root_node(root);
1687bd681513SChris Mason 			level = btrfs_header_level(b);
1688bd681513SChris Mason 			if (level <= write_lock_level) {
1689bd681513SChris Mason 				/* whoops, must trade for write lock */
1690bd681513SChris Mason 				btrfs_tree_read_unlock(b);
1691bd681513SChris Mason 				free_extent_buffer(b);
1692925baeddSChris Mason 				b = btrfs_lock_root_node(root);
1693bd681513SChris Mason 				root_lock = BTRFS_WRITE_LOCK;
1694bd681513SChris Mason 
1695bd681513SChris Mason 				/* the level might have changed, check again */
1696bd681513SChris Mason 				level = btrfs_header_level(b);
16975d4f98a2SYan Zheng 			}
1698bd681513SChris Mason 		}
1699bd681513SChris Mason 	}
1700bd681513SChris Mason 	p->nodes[level] = b;
1701bd681513SChris Mason 	if (!p->skip_locking)
1702bd681513SChris Mason 		p->locks[level] = root_lock;
1703925baeddSChris Mason 
1704eb60ceacSChris Mason 	while (b) {
17055f39d397SChris Mason 		level = btrfs_header_level(b);
170665b51a00SChris Mason 
170765b51a00SChris Mason 		/*
170865b51a00SChris Mason 		 * setup the path here so we can release it under lock
170965b51a00SChris Mason 		 * contention with the cow code
171065b51a00SChris Mason 		 */
171102217ed2SChris Mason 		if (cow) {
1712c8c42864SChris Mason 			/*
1713c8c42864SChris Mason 			 * if we don't really need to cow this block
1714c8c42864SChris Mason 			 * then we don't want to set the path blocking,
1715c8c42864SChris Mason 			 * so we test it here
1716c8c42864SChris Mason 			 */
17175d4f98a2SYan Zheng 			if (!should_cow_block(trans, root, b))
171865b51a00SChris Mason 				goto cow_done;
17195d4f98a2SYan Zheng 
1720b4ce94deSChris Mason 			btrfs_set_path_blocking(p);
1721b4ce94deSChris Mason 
1722bd681513SChris Mason 			/*
1723bd681513SChris Mason 			 * must have write locks on this node and the
1724bd681513SChris Mason 			 * parent
1725bd681513SChris Mason 			 */
1726bd681513SChris Mason 			if (level + 1 > write_lock_level) {
1727bd681513SChris Mason 				write_lock_level = level + 1;
1728bd681513SChris Mason 				btrfs_release_path(p);
1729bd681513SChris Mason 				goto again;
1730bd681513SChris Mason 			}
1731bd681513SChris Mason 
173233c66f43SYan Zheng 			err = btrfs_cow_block(trans, root, b,
1733e20d96d6SChris Mason 					      p->nodes[level + 1],
17349fa8cfe7SChris Mason 					      p->slots[level + 1], &b);
173533c66f43SYan Zheng 			if (err) {
173633c66f43SYan Zheng 				ret = err;
173765b51a00SChris Mason 				goto done;
173854aa1f4dSChris Mason 			}
173902217ed2SChris Mason 		}
174065b51a00SChris Mason cow_done:
174102217ed2SChris Mason 		BUG_ON(!cow && ins_len);
174265b51a00SChris Mason 
1743eb60ceacSChris Mason 		p->nodes[level] = b;
1744bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
1745b4ce94deSChris Mason 
1746b4ce94deSChris Mason 		/*
1747b4ce94deSChris Mason 		 * we have a lock on b and as long as we aren't changing
1748b4ce94deSChris Mason 		 * the tree, there is no way to for the items in b to change.
1749b4ce94deSChris Mason 		 * It is safe to drop the lock on our parent before we
1750b4ce94deSChris Mason 		 * go through the expensive btree search on b.
1751b4ce94deSChris Mason 		 *
1752b4ce94deSChris Mason 		 * If cow is true, then we might be changing slot zero,
1753b4ce94deSChris Mason 		 * which may require changing the parent.  So, we can't
1754b4ce94deSChris Mason 		 * drop the lock until after we know which slot we're
1755b4ce94deSChris Mason 		 * operating on.
1756b4ce94deSChris Mason 		 */
1757b4ce94deSChris Mason 		if (!cow)
1758b4ce94deSChris Mason 			btrfs_unlock_up_safe(p, level + 1);
1759b4ce94deSChris Mason 
17605f39d397SChris Mason 		ret = bin_search(b, key, level, &slot);
1761b4ce94deSChris Mason 
17625f39d397SChris Mason 		if (level != 0) {
176333c66f43SYan Zheng 			int dec = 0;
176433c66f43SYan Zheng 			if (ret && slot > 0) {
176533c66f43SYan Zheng 				dec = 1;
1766be0e5c09SChris Mason 				slot -= 1;
176733c66f43SYan Zheng 			}
1768be0e5c09SChris Mason 			p->slots[level] = slot;
176933c66f43SYan Zheng 			err = setup_nodes_for_search(trans, root, p, b, level,
1770bd681513SChris Mason 					     ins_len, &write_lock_level);
177133c66f43SYan Zheng 			if (err == -EAGAIN)
1772b4ce94deSChris Mason 				goto again;
177333c66f43SYan Zheng 			if (err) {
177433c66f43SYan Zheng 				ret = err;
177565b51a00SChris Mason 				goto done;
177633c66f43SYan Zheng 			}
17775c680ed6SChris Mason 			b = p->nodes[level];
17785c680ed6SChris Mason 			slot = p->slots[level];
1779b4ce94deSChris Mason 
1780bd681513SChris Mason 			/*
1781bd681513SChris Mason 			 * slot 0 is special, if we change the key
1782bd681513SChris Mason 			 * we have to update the parent pointer
1783bd681513SChris Mason 			 * which means we must have a write lock
1784bd681513SChris Mason 			 * on the parent
1785bd681513SChris Mason 			 */
1786bd681513SChris Mason 			if (slot == 0 && cow &&
1787bd681513SChris Mason 			    write_lock_level < level + 1) {
1788bd681513SChris Mason 				write_lock_level = level + 1;
1789bd681513SChris Mason 				btrfs_release_path(p);
1790bd681513SChris Mason 				goto again;
1791bd681513SChris Mason 			}
1792bd681513SChris Mason 
1793f9efa9c7SChris Mason 			unlock_up(p, level, lowest_unlock);
1794f9efa9c7SChris Mason 
1795925baeddSChris Mason 			if (level == lowest_level) {
179633c66f43SYan Zheng 				if (dec)
179733c66f43SYan Zheng 					p->slots[level]++;
17985b21f2edSZheng Yan 				goto done;
1799925baeddSChris Mason 			}
1800ca7a79adSChris Mason 
180133c66f43SYan Zheng 			err = read_block_for_search(trans, root, p,
1802c8c42864SChris Mason 						    &b, level, slot, key);
180333c66f43SYan Zheng 			if (err == -EAGAIN)
1804051e1b9fSChris Mason 				goto again;
180533c66f43SYan Zheng 			if (err) {
180633c66f43SYan Zheng 				ret = err;
180776a05b35SChris Mason 				goto done;
180833c66f43SYan Zheng 			}
180976a05b35SChris Mason 
1810b4ce94deSChris Mason 			if (!p->skip_locking) {
1811bd681513SChris Mason 				level = btrfs_header_level(b);
1812bd681513SChris Mason 				if (level <= write_lock_level) {
1813bd681513SChris Mason 					err = btrfs_try_tree_write_lock(b);
181433c66f43SYan Zheng 					if (!err) {
1815b4ce94deSChris Mason 						btrfs_set_path_blocking(p);
1816925baeddSChris Mason 						btrfs_tree_lock(b);
1817bd681513SChris Mason 						btrfs_clear_path_blocking(p, b,
1818bd681513SChris Mason 								  BTRFS_WRITE_LOCK);
1819b4ce94deSChris Mason 					}
1820bd681513SChris Mason 					p->locks[level] = BTRFS_WRITE_LOCK;
1821bd681513SChris Mason 				} else {
1822bd681513SChris Mason 					err = btrfs_try_tree_read_lock(b);
1823bd681513SChris Mason 					if (!err) {
1824bd681513SChris Mason 						btrfs_set_path_blocking(p);
1825bd681513SChris Mason 						btrfs_tree_read_lock(b);
1826bd681513SChris Mason 						btrfs_clear_path_blocking(p, b,
1827bd681513SChris Mason 								  BTRFS_READ_LOCK);
1828bd681513SChris Mason 					}
1829bd681513SChris Mason 					p->locks[level] = BTRFS_READ_LOCK;
1830bd681513SChris Mason 				}
1831bd681513SChris Mason 				p->nodes[level] = b;
1832b4ce94deSChris Mason 			}
1833be0e5c09SChris Mason 		} else {
1834be0e5c09SChris Mason 			p->slots[level] = slot;
183587b29b20SYan Zheng 			if (ins_len > 0 &&
183687b29b20SYan Zheng 			    btrfs_leaf_free_space(root, b) < ins_len) {
1837bd681513SChris Mason 				if (write_lock_level < 1) {
1838bd681513SChris Mason 					write_lock_level = 1;
1839bd681513SChris Mason 					btrfs_release_path(p);
1840bd681513SChris Mason 					goto again;
1841bd681513SChris Mason 				}
1842bd681513SChris Mason 
1843b4ce94deSChris Mason 				btrfs_set_path_blocking(p);
184433c66f43SYan Zheng 				err = split_leaf(trans, root, key,
1845cc0c5538SChris Mason 						 p, ins_len, ret == 0);
1846bd681513SChris Mason 				btrfs_clear_path_blocking(p, NULL, 0);
1847b4ce94deSChris Mason 
184833c66f43SYan Zheng 				BUG_ON(err > 0);
184933c66f43SYan Zheng 				if (err) {
185033c66f43SYan Zheng 					ret = err;
185165b51a00SChris Mason 					goto done;
185265b51a00SChris Mason 				}
18535c680ed6SChris Mason 			}
1854459931ecSChris Mason 			if (!p->search_for_split)
1855925baeddSChris Mason 				unlock_up(p, level, lowest_unlock);
185665b51a00SChris Mason 			goto done;
185765b51a00SChris Mason 		}
185865b51a00SChris Mason 	}
185965b51a00SChris Mason 	ret = 1;
186065b51a00SChris Mason done:
1861b4ce94deSChris Mason 	/*
1862b4ce94deSChris Mason 	 * we don't really know what they plan on doing with the path
1863b4ce94deSChris Mason 	 * from here on, so for now just mark it as blocking
1864b4ce94deSChris Mason 	 */
1865b9473439SChris Mason 	if (!p->leave_spinning)
1866b4ce94deSChris Mason 		btrfs_set_path_blocking(p);
186776a05b35SChris Mason 	if (ret < 0)
1868b3b4aa74SDavid Sterba 		btrfs_release_path(p);
1869be0e5c09SChris Mason 	return ret;
1870be0e5c09SChris Mason }
1871be0e5c09SChris Mason 
187274123bd7SChris Mason /*
187374123bd7SChris Mason  * adjust the pointers going up the tree, starting at level
187474123bd7SChris Mason  * making sure the right key of each node is points to 'key'.
187574123bd7SChris Mason  * This is used after shifting pointers to the left, so it stops
187674123bd7SChris Mason  * fixing up pointers when a given leaf/node is not in slot 0 of the
187774123bd7SChris Mason  * higher levels
1878aa5d6bedSChris Mason  *
187974123bd7SChris Mason  */
1880143bede5SJeff Mahoney static void fixup_low_keys(struct btrfs_trans_handle *trans,
18815f39d397SChris Mason 			   struct btrfs_root *root, struct btrfs_path *path,
18825f39d397SChris Mason 			   struct btrfs_disk_key *key, int level)
1883be0e5c09SChris Mason {
1884be0e5c09SChris Mason 	int i;
18855f39d397SChris Mason 	struct extent_buffer *t;
18865f39d397SChris Mason 
1887234b63a0SChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1888be0e5c09SChris Mason 		int tslot = path->slots[i];
1889eb60ceacSChris Mason 		if (!path->nodes[i])
1890be0e5c09SChris Mason 			break;
18915f39d397SChris Mason 		t = path->nodes[i];
18925f39d397SChris Mason 		btrfs_set_node_key(t, key, tslot);
1893d6025579SChris Mason 		btrfs_mark_buffer_dirty(path->nodes[i]);
1894be0e5c09SChris Mason 		if (tslot != 0)
1895be0e5c09SChris Mason 			break;
1896be0e5c09SChris Mason 	}
1897be0e5c09SChris Mason }
1898be0e5c09SChris Mason 
189974123bd7SChris Mason /*
190031840ae1SZheng Yan  * update item key.
190131840ae1SZheng Yan  *
190231840ae1SZheng Yan  * This function isn't completely safe. It's the caller's responsibility
190331840ae1SZheng Yan  * that the new key won't break the order
190431840ae1SZheng Yan  */
1905143bede5SJeff Mahoney void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
190631840ae1SZheng Yan 			     struct btrfs_root *root, struct btrfs_path *path,
190731840ae1SZheng Yan 			     struct btrfs_key *new_key)
190831840ae1SZheng Yan {
190931840ae1SZheng Yan 	struct btrfs_disk_key disk_key;
191031840ae1SZheng Yan 	struct extent_buffer *eb;
191131840ae1SZheng Yan 	int slot;
191231840ae1SZheng Yan 
191331840ae1SZheng Yan 	eb = path->nodes[0];
191431840ae1SZheng Yan 	slot = path->slots[0];
191531840ae1SZheng Yan 	if (slot > 0) {
191631840ae1SZheng Yan 		btrfs_item_key(eb, &disk_key, slot - 1);
1917143bede5SJeff Mahoney 		BUG_ON(comp_keys(&disk_key, new_key) >= 0);
191831840ae1SZheng Yan 	}
191931840ae1SZheng Yan 	if (slot < btrfs_header_nritems(eb) - 1) {
192031840ae1SZheng Yan 		btrfs_item_key(eb, &disk_key, slot + 1);
1921143bede5SJeff Mahoney 		BUG_ON(comp_keys(&disk_key, new_key) <= 0);
192231840ae1SZheng Yan 	}
192331840ae1SZheng Yan 
192431840ae1SZheng Yan 	btrfs_cpu_key_to_disk(&disk_key, new_key);
192531840ae1SZheng Yan 	btrfs_set_item_key(eb, &disk_key, slot);
192631840ae1SZheng Yan 	btrfs_mark_buffer_dirty(eb);
192731840ae1SZheng Yan 	if (slot == 0)
192831840ae1SZheng Yan 		fixup_low_keys(trans, root, path, &disk_key, 1);
192931840ae1SZheng Yan }
193031840ae1SZheng Yan 
193131840ae1SZheng Yan /*
193274123bd7SChris Mason  * try to push data from one node into the next node left in the
193379f95c82SChris Mason  * tree.
1934aa5d6bedSChris Mason  *
1935aa5d6bedSChris Mason  * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1936aa5d6bedSChris Mason  * error, and > 0 if there was no room in the left hand block.
193774123bd7SChris Mason  */
193898ed5174SChris Mason static int push_node_left(struct btrfs_trans_handle *trans,
193998ed5174SChris Mason 			  struct btrfs_root *root, struct extent_buffer *dst,
1940971a1f66SChris Mason 			  struct extent_buffer *src, int empty)
1941be0e5c09SChris Mason {
1942be0e5c09SChris Mason 	int push_items = 0;
1943bb803951SChris Mason 	int src_nritems;
1944bb803951SChris Mason 	int dst_nritems;
1945aa5d6bedSChris Mason 	int ret = 0;
1946be0e5c09SChris Mason 
19475f39d397SChris Mason 	src_nritems = btrfs_header_nritems(src);
19485f39d397SChris Mason 	dst_nritems = btrfs_header_nritems(dst);
1949123abc88SChris Mason 	push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
19507bb86316SChris Mason 	WARN_ON(btrfs_header_generation(src) != trans->transid);
19517bb86316SChris Mason 	WARN_ON(btrfs_header_generation(dst) != trans->transid);
195254aa1f4dSChris Mason 
1953bce4eae9SChris Mason 	if (!empty && src_nritems <= 8)
1954971a1f66SChris Mason 		return 1;
1955971a1f66SChris Mason 
1956d397712bSChris Mason 	if (push_items <= 0)
1957be0e5c09SChris Mason 		return 1;
1958be0e5c09SChris Mason 
1959bce4eae9SChris Mason 	if (empty) {
1960971a1f66SChris Mason 		push_items = min(src_nritems, push_items);
1961bce4eae9SChris Mason 		if (push_items < src_nritems) {
1962bce4eae9SChris Mason 			/* leave at least 8 pointers in the node if
1963bce4eae9SChris Mason 			 * we aren't going to empty it
1964bce4eae9SChris Mason 			 */
1965bce4eae9SChris Mason 			if (src_nritems - push_items < 8) {
1966bce4eae9SChris Mason 				if (push_items <= 8)
1967bce4eae9SChris Mason 					return 1;
1968bce4eae9SChris Mason 				push_items -= 8;
1969bce4eae9SChris Mason 			}
1970bce4eae9SChris Mason 		}
1971bce4eae9SChris Mason 	} else
1972bce4eae9SChris Mason 		push_items = min(src_nritems - 8, push_items);
197379f95c82SChris Mason 
19745f39d397SChris Mason 	copy_extent_buffer(dst, src,
19755f39d397SChris Mason 			   btrfs_node_key_ptr_offset(dst_nritems),
19765f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
1977123abc88SChris Mason 			   push_items * sizeof(struct btrfs_key_ptr));
19785f39d397SChris Mason 
1979bb803951SChris Mason 	if (push_items < src_nritems) {
19805f39d397SChris Mason 		memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
19815f39d397SChris Mason 				      btrfs_node_key_ptr_offset(push_items),
1982e2fa7227SChris Mason 				      (src_nritems - push_items) *
1983123abc88SChris Mason 				      sizeof(struct btrfs_key_ptr));
1984bb803951SChris Mason 	}
19855f39d397SChris Mason 	btrfs_set_header_nritems(src, src_nritems - push_items);
19865f39d397SChris Mason 	btrfs_set_header_nritems(dst, dst_nritems + push_items);
19875f39d397SChris Mason 	btrfs_mark_buffer_dirty(src);
19885f39d397SChris Mason 	btrfs_mark_buffer_dirty(dst);
198931840ae1SZheng Yan 
1990bb803951SChris Mason 	return ret;
1991be0e5c09SChris Mason }
1992be0e5c09SChris Mason 
199397571fd0SChris Mason /*
199479f95c82SChris Mason  * try to push data from one node into the next node right in the
199579f95c82SChris Mason  * tree.
199679f95c82SChris Mason  *
199779f95c82SChris Mason  * returns 0 if some ptrs were pushed, < 0 if there was some horrible
199879f95c82SChris Mason  * error, and > 0 if there was no room in the right hand block.
199979f95c82SChris Mason  *
200079f95c82SChris Mason  * this will  only push up to 1/2 the contents of the left node over
200179f95c82SChris Mason  */
20025f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans,
20035f39d397SChris Mason 			      struct btrfs_root *root,
20045f39d397SChris Mason 			      struct extent_buffer *dst,
20055f39d397SChris Mason 			      struct extent_buffer *src)
200679f95c82SChris Mason {
200779f95c82SChris Mason 	int push_items = 0;
200879f95c82SChris Mason 	int max_push;
200979f95c82SChris Mason 	int src_nritems;
201079f95c82SChris Mason 	int dst_nritems;
201179f95c82SChris Mason 	int ret = 0;
201279f95c82SChris Mason 
20137bb86316SChris Mason 	WARN_ON(btrfs_header_generation(src) != trans->transid);
20147bb86316SChris Mason 	WARN_ON(btrfs_header_generation(dst) != trans->transid);
20157bb86316SChris Mason 
20165f39d397SChris Mason 	src_nritems = btrfs_header_nritems(src);
20175f39d397SChris Mason 	dst_nritems = btrfs_header_nritems(dst);
2018123abc88SChris Mason 	push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
2019d397712bSChris Mason 	if (push_items <= 0)
202079f95c82SChris Mason 		return 1;
2021bce4eae9SChris Mason 
2022d397712bSChris Mason 	if (src_nritems < 4)
2023bce4eae9SChris Mason 		return 1;
202479f95c82SChris Mason 
202579f95c82SChris Mason 	max_push = src_nritems / 2 + 1;
202679f95c82SChris Mason 	/* don't try to empty the node */
2027d397712bSChris Mason 	if (max_push >= src_nritems)
202879f95c82SChris Mason 		return 1;
2029252c38f0SYan 
203079f95c82SChris Mason 	if (max_push < push_items)
203179f95c82SChris Mason 		push_items = max_push;
203279f95c82SChris Mason 
20335f39d397SChris Mason 	memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
20345f39d397SChris Mason 				      btrfs_node_key_ptr_offset(0),
20355f39d397SChris Mason 				      (dst_nritems) *
20365f39d397SChris Mason 				      sizeof(struct btrfs_key_ptr));
2037d6025579SChris Mason 
20385f39d397SChris Mason 	copy_extent_buffer(dst, src,
20395f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
20405f39d397SChris Mason 			   btrfs_node_key_ptr_offset(src_nritems - push_items),
2041123abc88SChris Mason 			   push_items * sizeof(struct btrfs_key_ptr));
204279f95c82SChris Mason 
20435f39d397SChris Mason 	btrfs_set_header_nritems(src, src_nritems - push_items);
20445f39d397SChris Mason 	btrfs_set_header_nritems(dst, dst_nritems + push_items);
204579f95c82SChris Mason 
20465f39d397SChris Mason 	btrfs_mark_buffer_dirty(src);
20475f39d397SChris Mason 	btrfs_mark_buffer_dirty(dst);
204831840ae1SZheng Yan 
204979f95c82SChris Mason 	return ret;
205079f95c82SChris Mason }
205179f95c82SChris Mason 
205279f95c82SChris Mason /*
205397571fd0SChris Mason  * helper function to insert a new root level in the tree.
205497571fd0SChris Mason  * A new node is allocated, and a single item is inserted to
205597571fd0SChris Mason  * point to the existing root
2056aa5d6bedSChris Mason  *
2057aa5d6bedSChris Mason  * returns zero on success or < 0 on failure.
205897571fd0SChris Mason  */
2059d397712bSChris Mason static noinline int insert_new_root(struct btrfs_trans_handle *trans,
20605f39d397SChris Mason 			   struct btrfs_root *root,
20615f39d397SChris Mason 			   struct btrfs_path *path, int level)
206274123bd7SChris Mason {
20637bb86316SChris Mason 	u64 lower_gen;
20645f39d397SChris Mason 	struct extent_buffer *lower;
20655f39d397SChris Mason 	struct extent_buffer *c;
2066925baeddSChris Mason 	struct extent_buffer *old;
20675f39d397SChris Mason 	struct btrfs_disk_key lower_key;
20685c680ed6SChris Mason 
20695c680ed6SChris Mason 	BUG_ON(path->nodes[level]);
20705c680ed6SChris Mason 	BUG_ON(path->nodes[level-1] != root->node);
20715c680ed6SChris Mason 
20727bb86316SChris Mason 	lower = path->nodes[level-1];
20737bb86316SChris Mason 	if (level == 1)
20747bb86316SChris Mason 		btrfs_item_key(lower, &lower_key, 0);
20757bb86316SChris Mason 	else
20767bb86316SChris Mason 		btrfs_node_key(lower, &lower_key, 0);
20777bb86316SChris Mason 
207831840ae1SZheng Yan 	c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
20795d4f98a2SYan Zheng 				   root->root_key.objectid, &lower_key,
208066d7e7f0SArne Jansen 				   level, root->node->start, 0, 0);
20815f39d397SChris Mason 	if (IS_ERR(c))
20825f39d397SChris Mason 		return PTR_ERR(c);
2083925baeddSChris Mason 
2084f0486c68SYan, Zheng 	root_add_used(root, root->nodesize);
2085f0486c68SYan, Zheng 
20865d4f98a2SYan Zheng 	memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
20875f39d397SChris Mason 	btrfs_set_header_nritems(c, 1);
20885f39d397SChris Mason 	btrfs_set_header_level(c, level);
2089db94535dSChris Mason 	btrfs_set_header_bytenr(c, c->start);
20905f39d397SChris Mason 	btrfs_set_header_generation(c, trans->transid);
20915d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
20925f39d397SChris Mason 	btrfs_set_header_owner(c, root->root_key.objectid);
2093d5719762SChris Mason 
20945f39d397SChris Mason 	write_extent_buffer(c, root->fs_info->fsid,
20955f39d397SChris Mason 			    (unsigned long)btrfs_header_fsid(c),
20965f39d397SChris Mason 			    BTRFS_FSID_SIZE);
2097e17cade2SChris Mason 
2098e17cade2SChris Mason 	write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2099e17cade2SChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(c),
2100e17cade2SChris Mason 			    BTRFS_UUID_SIZE);
2101e17cade2SChris Mason 
21025f39d397SChris Mason 	btrfs_set_node_key(c, &lower_key, 0);
2103db94535dSChris Mason 	btrfs_set_node_blockptr(c, 0, lower->start);
21047bb86316SChris Mason 	lower_gen = btrfs_header_generation(lower);
210531840ae1SZheng Yan 	WARN_ON(lower_gen != trans->transid);
21067bb86316SChris Mason 
21077bb86316SChris Mason 	btrfs_set_node_ptr_generation(c, 0, lower_gen);
21085f39d397SChris Mason 
21095f39d397SChris Mason 	btrfs_mark_buffer_dirty(c);
2110d5719762SChris Mason 
2111925baeddSChris Mason 	old = root->node;
2112240f62c8SChris Mason 	rcu_assign_pointer(root->node, c);
2113925baeddSChris Mason 
2114925baeddSChris Mason 	/* the super has an extra ref to root->node */
2115925baeddSChris Mason 	free_extent_buffer(old);
2116925baeddSChris Mason 
21170b86a832SChris Mason 	add_root_to_dirty_list(root);
21185f39d397SChris Mason 	extent_buffer_get(c);
21195f39d397SChris Mason 	path->nodes[level] = c;
2120bd681513SChris Mason 	path->locks[level] = BTRFS_WRITE_LOCK;
212174123bd7SChris Mason 	path->slots[level] = 0;
212274123bd7SChris Mason 	return 0;
212374123bd7SChris Mason }
21245c680ed6SChris Mason 
21255c680ed6SChris Mason /*
21265c680ed6SChris Mason  * worker function to insert a single pointer in a node.
21275c680ed6SChris Mason  * the node should have enough room for the pointer already
212897571fd0SChris Mason  *
21295c680ed6SChris Mason  * slot and level indicate where you want the key to go, and
21305c680ed6SChris Mason  * blocknr is the block the key points to.
21315c680ed6SChris Mason  */
2132143bede5SJeff Mahoney static void insert_ptr(struct btrfs_trans_handle *trans,
2133143bede5SJeff Mahoney 		       struct btrfs_root *root, struct btrfs_path *path,
2134143bede5SJeff Mahoney 		       struct btrfs_disk_key *key, u64 bytenr,
2135143bede5SJeff Mahoney 		       int slot, int level)
21365c680ed6SChris Mason {
21375f39d397SChris Mason 	struct extent_buffer *lower;
21385c680ed6SChris Mason 	int nritems;
21395c680ed6SChris Mason 
21405c680ed6SChris Mason 	BUG_ON(!path->nodes[level]);
2141f0486c68SYan, Zheng 	btrfs_assert_tree_locked(path->nodes[level]);
21425f39d397SChris Mason 	lower = path->nodes[level];
21435f39d397SChris Mason 	nritems = btrfs_header_nritems(lower);
2144c293498bSStoyan Gaydarov 	BUG_ON(slot > nritems);
2145143bede5SJeff Mahoney 	BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
214674123bd7SChris Mason 	if (slot != nritems) {
21475f39d397SChris Mason 		memmove_extent_buffer(lower,
21485f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot + 1),
21495f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot),
2150123abc88SChris Mason 			      (nritems - slot) * sizeof(struct btrfs_key_ptr));
215174123bd7SChris Mason 	}
21525f39d397SChris Mason 	btrfs_set_node_key(lower, key, slot);
2153db94535dSChris Mason 	btrfs_set_node_blockptr(lower, slot, bytenr);
215474493f7aSChris Mason 	WARN_ON(trans->transid == 0);
215574493f7aSChris Mason 	btrfs_set_node_ptr_generation(lower, slot, trans->transid);
21565f39d397SChris Mason 	btrfs_set_header_nritems(lower, nritems + 1);
21575f39d397SChris Mason 	btrfs_mark_buffer_dirty(lower);
215874123bd7SChris Mason }
215974123bd7SChris Mason 
216097571fd0SChris Mason /*
216197571fd0SChris Mason  * split the node at the specified level in path in two.
216297571fd0SChris Mason  * The path is corrected to point to the appropriate node after the split
216397571fd0SChris Mason  *
216497571fd0SChris Mason  * Before splitting this tries to make some room in the node by pushing
216597571fd0SChris Mason  * left and right, if either one works, it returns right away.
2166aa5d6bedSChris Mason  *
2167aa5d6bedSChris Mason  * returns 0 on success and < 0 on failure
216897571fd0SChris Mason  */
2169e02119d5SChris Mason static noinline int split_node(struct btrfs_trans_handle *trans,
2170e02119d5SChris Mason 			       struct btrfs_root *root,
2171e02119d5SChris Mason 			       struct btrfs_path *path, int level)
2172be0e5c09SChris Mason {
21735f39d397SChris Mason 	struct extent_buffer *c;
21745f39d397SChris Mason 	struct extent_buffer *split;
21755f39d397SChris Mason 	struct btrfs_disk_key disk_key;
2176be0e5c09SChris Mason 	int mid;
21775c680ed6SChris Mason 	int ret;
21787518a238SChris Mason 	u32 c_nritems;
2179be0e5c09SChris Mason 
21805f39d397SChris Mason 	c = path->nodes[level];
21817bb86316SChris Mason 	WARN_ON(btrfs_header_generation(c) != trans->transid);
21825f39d397SChris Mason 	if (c == root->node) {
21835c680ed6SChris Mason 		/* trying to split the root, lets make a new one */
2184e089f05cSChris Mason 		ret = insert_new_root(trans, root, path, level + 1);
21855c680ed6SChris Mason 		if (ret)
21865c680ed6SChris Mason 			return ret;
2187b3612421SChris Mason 	} else {
2188e66f709bSChris Mason 		ret = push_nodes_for_insert(trans, root, path, level);
21895f39d397SChris Mason 		c = path->nodes[level];
21905f39d397SChris Mason 		if (!ret && btrfs_header_nritems(c) <
2191c448acf0SChris Mason 		    BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
2192e66f709bSChris Mason 			return 0;
219354aa1f4dSChris Mason 		if (ret < 0)
219454aa1f4dSChris Mason 			return ret;
21955c680ed6SChris Mason 	}
2196e66f709bSChris Mason 
21975f39d397SChris Mason 	c_nritems = btrfs_header_nritems(c);
21985d4f98a2SYan Zheng 	mid = (c_nritems + 1) / 2;
21995d4f98a2SYan Zheng 	btrfs_node_key(c, &disk_key, mid);
22007bb86316SChris Mason 
22015d4f98a2SYan Zheng 	split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
22027bb86316SChris Mason 					root->root_key.objectid,
220366d7e7f0SArne Jansen 					&disk_key, level, c->start, 0, 0);
22045f39d397SChris Mason 	if (IS_ERR(split))
22055f39d397SChris Mason 		return PTR_ERR(split);
220654aa1f4dSChris Mason 
2207f0486c68SYan, Zheng 	root_add_used(root, root->nodesize);
2208f0486c68SYan, Zheng 
22095d4f98a2SYan Zheng 	memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
22105f39d397SChris Mason 	btrfs_set_header_level(split, btrfs_header_level(c));
2211db94535dSChris Mason 	btrfs_set_header_bytenr(split, split->start);
22125f39d397SChris Mason 	btrfs_set_header_generation(split, trans->transid);
22135d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
22145f39d397SChris Mason 	btrfs_set_header_owner(split, root->root_key.objectid);
22155f39d397SChris Mason 	write_extent_buffer(split, root->fs_info->fsid,
22165f39d397SChris Mason 			    (unsigned long)btrfs_header_fsid(split),
22175f39d397SChris Mason 			    BTRFS_FSID_SIZE);
2218e17cade2SChris Mason 	write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2219e17cade2SChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(split),
2220e17cade2SChris Mason 			    BTRFS_UUID_SIZE);
22215f39d397SChris Mason 
22225f39d397SChris Mason 
22235f39d397SChris Mason 	copy_extent_buffer(split, c,
22245f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
22255f39d397SChris Mason 			   btrfs_node_key_ptr_offset(mid),
2226123abc88SChris Mason 			   (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
22275f39d397SChris Mason 	btrfs_set_header_nritems(split, c_nritems - mid);
22285f39d397SChris Mason 	btrfs_set_header_nritems(c, mid);
2229aa5d6bedSChris Mason 	ret = 0;
2230aa5d6bedSChris Mason 
22315f39d397SChris Mason 	btrfs_mark_buffer_dirty(c);
22325f39d397SChris Mason 	btrfs_mark_buffer_dirty(split);
22335f39d397SChris Mason 
2234143bede5SJeff Mahoney 	insert_ptr(trans, root, path, &disk_key, split->start,
2235143bede5SJeff Mahoney 		   path->slots[level + 1] + 1, level + 1);
2236aa5d6bedSChris Mason 
22375de08d7dSChris Mason 	if (path->slots[level] >= mid) {
22385c680ed6SChris Mason 		path->slots[level] -= mid;
2239925baeddSChris Mason 		btrfs_tree_unlock(c);
22405f39d397SChris Mason 		free_extent_buffer(c);
22415f39d397SChris Mason 		path->nodes[level] = split;
22425c680ed6SChris Mason 		path->slots[level + 1] += 1;
2243eb60ceacSChris Mason 	} else {
2244925baeddSChris Mason 		btrfs_tree_unlock(split);
22455f39d397SChris Mason 		free_extent_buffer(split);
2246be0e5c09SChris Mason 	}
2247aa5d6bedSChris Mason 	return ret;
2248be0e5c09SChris Mason }
2249be0e5c09SChris Mason 
225074123bd7SChris Mason /*
225174123bd7SChris Mason  * how many bytes are required to store the items in a leaf.  start
225274123bd7SChris Mason  * and nr indicate which items in the leaf to check.  This totals up the
225374123bd7SChris Mason  * space used both by the item structs and the item data
225474123bd7SChris Mason  */
22555f39d397SChris Mason static int leaf_space_used(struct extent_buffer *l, int start, int nr)
2256be0e5c09SChris Mason {
2257be0e5c09SChris Mason 	int data_len;
22585f39d397SChris Mason 	int nritems = btrfs_header_nritems(l);
2259d4dbff95SChris Mason 	int end = min(nritems, start + nr) - 1;
2260be0e5c09SChris Mason 
2261be0e5c09SChris Mason 	if (!nr)
2262be0e5c09SChris Mason 		return 0;
22635f39d397SChris Mason 	data_len = btrfs_item_end_nr(l, start);
22645f39d397SChris Mason 	data_len = data_len - btrfs_item_offset_nr(l, end);
22650783fcfcSChris Mason 	data_len += sizeof(struct btrfs_item) * nr;
2266d4dbff95SChris Mason 	WARN_ON(data_len < 0);
2267be0e5c09SChris Mason 	return data_len;
2268be0e5c09SChris Mason }
2269be0e5c09SChris Mason 
227074123bd7SChris Mason /*
2271d4dbff95SChris Mason  * The space between the end of the leaf items and
2272d4dbff95SChris Mason  * the start of the leaf data.  IOW, how much room
2273d4dbff95SChris Mason  * the leaf has left for both items and data
2274d4dbff95SChris Mason  */
2275d397712bSChris Mason noinline int btrfs_leaf_free_space(struct btrfs_root *root,
2276e02119d5SChris Mason 				   struct extent_buffer *leaf)
2277d4dbff95SChris Mason {
22785f39d397SChris Mason 	int nritems = btrfs_header_nritems(leaf);
22795f39d397SChris Mason 	int ret;
22805f39d397SChris Mason 	ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
22815f39d397SChris Mason 	if (ret < 0) {
2282d397712bSChris Mason 		printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2283d397712bSChris Mason 		       "used %d nritems %d\n",
2284ae2f5411SJens Axboe 		       ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
22855f39d397SChris Mason 		       leaf_space_used(leaf, 0, nritems), nritems);
22865f39d397SChris Mason 	}
22875f39d397SChris Mason 	return ret;
2288d4dbff95SChris Mason }
2289d4dbff95SChris Mason 
229099d8f83cSChris Mason /*
229199d8f83cSChris Mason  * min slot controls the lowest index we're willing to push to the
229299d8f83cSChris Mason  * right.  We'll push up to and including min_slot, but no lower
229399d8f83cSChris Mason  */
229444871b1bSChris Mason static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
229544871b1bSChris Mason 				      struct btrfs_root *root,
229644871b1bSChris Mason 				      struct btrfs_path *path,
229744871b1bSChris Mason 				      int data_size, int empty,
229844871b1bSChris Mason 				      struct extent_buffer *right,
229999d8f83cSChris Mason 				      int free_space, u32 left_nritems,
230099d8f83cSChris Mason 				      u32 min_slot)
230100ec4c51SChris Mason {
23025f39d397SChris Mason 	struct extent_buffer *left = path->nodes[0];
230344871b1bSChris Mason 	struct extent_buffer *upper = path->nodes[1];
23045f39d397SChris Mason 	struct btrfs_disk_key disk_key;
230500ec4c51SChris Mason 	int slot;
230634a38218SChris Mason 	u32 i;
230700ec4c51SChris Mason 	int push_space = 0;
230800ec4c51SChris Mason 	int push_items = 0;
23090783fcfcSChris Mason 	struct btrfs_item *item;
231034a38218SChris Mason 	u32 nr;
23117518a238SChris Mason 	u32 right_nritems;
23125f39d397SChris Mason 	u32 data_end;
2313db94535dSChris Mason 	u32 this_item_size;
231400ec4c51SChris Mason 
231534a38218SChris Mason 	if (empty)
231634a38218SChris Mason 		nr = 0;
231734a38218SChris Mason 	else
231899d8f83cSChris Mason 		nr = max_t(u32, 1, min_slot);
231934a38218SChris Mason 
232031840ae1SZheng Yan 	if (path->slots[0] >= left_nritems)
232187b29b20SYan Zheng 		push_space += data_size;
232231840ae1SZheng Yan 
232344871b1bSChris Mason 	slot = path->slots[1];
232434a38218SChris Mason 	i = left_nritems - 1;
232534a38218SChris Mason 	while (i >= nr) {
23265f39d397SChris Mason 		item = btrfs_item_nr(left, i);
2327db94535dSChris Mason 
232831840ae1SZheng Yan 		if (!empty && push_items > 0) {
232931840ae1SZheng Yan 			if (path->slots[0] > i)
233031840ae1SZheng Yan 				break;
233131840ae1SZheng Yan 			if (path->slots[0] == i) {
233231840ae1SZheng Yan 				int space = btrfs_leaf_free_space(root, left);
233331840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
233431840ae1SZheng Yan 					break;
233531840ae1SZheng Yan 			}
233631840ae1SZheng Yan 		}
233731840ae1SZheng Yan 
233800ec4c51SChris Mason 		if (path->slots[0] == i)
233987b29b20SYan Zheng 			push_space += data_size;
2340db94535dSChris Mason 
2341db94535dSChris Mason 		this_item_size = btrfs_item_size(left, item);
2342db94535dSChris Mason 		if (this_item_size + sizeof(*item) + push_space > free_space)
234300ec4c51SChris Mason 			break;
234431840ae1SZheng Yan 
234500ec4c51SChris Mason 		push_items++;
2346db94535dSChris Mason 		push_space += this_item_size + sizeof(*item);
234734a38218SChris Mason 		if (i == 0)
234834a38218SChris Mason 			break;
234934a38218SChris Mason 		i--;
2350db94535dSChris Mason 	}
23515f39d397SChris Mason 
2352925baeddSChris Mason 	if (push_items == 0)
2353925baeddSChris Mason 		goto out_unlock;
23545f39d397SChris Mason 
235534a38218SChris Mason 	if (!empty && push_items == left_nritems)
2356a429e513SChris Mason 		WARN_ON(1);
23575f39d397SChris Mason 
235800ec4c51SChris Mason 	/* push left to right */
23595f39d397SChris Mason 	right_nritems = btrfs_header_nritems(right);
236034a38218SChris Mason 
23615f39d397SChris Mason 	push_space = btrfs_item_end_nr(left, left_nritems - push_items);
2362123abc88SChris Mason 	push_space -= leaf_data_end(root, left);
23635f39d397SChris Mason 
236400ec4c51SChris Mason 	/* make room in the right data area */
23655f39d397SChris Mason 	data_end = leaf_data_end(root, right);
23665f39d397SChris Mason 	memmove_extent_buffer(right,
23675f39d397SChris Mason 			      btrfs_leaf_data(right) + data_end - push_space,
23685f39d397SChris Mason 			      btrfs_leaf_data(right) + data_end,
23695f39d397SChris Mason 			      BTRFS_LEAF_DATA_SIZE(root) - data_end);
23705f39d397SChris Mason 
237100ec4c51SChris Mason 	/* copy from the left data area */
23725f39d397SChris Mason 	copy_extent_buffer(right, left, btrfs_leaf_data(right) +
2373d6025579SChris Mason 		     BTRFS_LEAF_DATA_SIZE(root) - push_space,
2374d6025579SChris Mason 		     btrfs_leaf_data(left) + leaf_data_end(root, left),
2375d6025579SChris Mason 		     push_space);
23765f39d397SChris Mason 
23775f39d397SChris Mason 	memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
23785f39d397SChris Mason 			      btrfs_item_nr_offset(0),
23790783fcfcSChris Mason 			      right_nritems * sizeof(struct btrfs_item));
23805f39d397SChris Mason 
238100ec4c51SChris Mason 	/* copy the items from left to right */
23825f39d397SChris Mason 	copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
23835f39d397SChris Mason 		   btrfs_item_nr_offset(left_nritems - push_items),
23840783fcfcSChris Mason 		   push_items * sizeof(struct btrfs_item));
238500ec4c51SChris Mason 
238600ec4c51SChris Mason 	/* update the item pointers */
23877518a238SChris Mason 	right_nritems += push_items;
23885f39d397SChris Mason 	btrfs_set_header_nritems(right, right_nritems);
2389123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root);
23907518a238SChris Mason 	for (i = 0; i < right_nritems; i++) {
23915f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2392db94535dSChris Mason 		push_space -= btrfs_item_size(right, item);
2393db94535dSChris Mason 		btrfs_set_item_offset(right, item, push_space);
2394db94535dSChris Mason 	}
2395db94535dSChris Mason 
23967518a238SChris Mason 	left_nritems -= push_items;
23975f39d397SChris Mason 	btrfs_set_header_nritems(left, left_nritems);
239800ec4c51SChris Mason 
239934a38218SChris Mason 	if (left_nritems)
24005f39d397SChris Mason 		btrfs_mark_buffer_dirty(left);
2401f0486c68SYan, Zheng 	else
2402f0486c68SYan, Zheng 		clean_tree_block(trans, root, left);
2403f0486c68SYan, Zheng 
24045f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
2405a429e513SChris Mason 
24065f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
24075f39d397SChris Mason 	btrfs_set_node_key(upper, &disk_key, slot + 1);
2408d6025579SChris Mason 	btrfs_mark_buffer_dirty(upper);
240902217ed2SChris Mason 
241000ec4c51SChris Mason 	/* then fixup the leaf pointer in the path */
24117518a238SChris Mason 	if (path->slots[0] >= left_nritems) {
24127518a238SChris Mason 		path->slots[0] -= left_nritems;
2413925baeddSChris Mason 		if (btrfs_header_nritems(path->nodes[0]) == 0)
2414925baeddSChris Mason 			clean_tree_block(trans, root, path->nodes[0]);
2415925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
24165f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
24175f39d397SChris Mason 		path->nodes[0] = right;
241800ec4c51SChris Mason 		path->slots[1] += 1;
241900ec4c51SChris Mason 	} else {
2420925baeddSChris Mason 		btrfs_tree_unlock(right);
24215f39d397SChris Mason 		free_extent_buffer(right);
242200ec4c51SChris Mason 	}
242300ec4c51SChris Mason 	return 0;
2424925baeddSChris Mason 
2425925baeddSChris Mason out_unlock:
2426925baeddSChris Mason 	btrfs_tree_unlock(right);
2427925baeddSChris Mason 	free_extent_buffer(right);
2428925baeddSChris Mason 	return 1;
242900ec4c51SChris Mason }
2430925baeddSChris Mason 
243100ec4c51SChris Mason /*
243244871b1bSChris Mason  * push some data in the path leaf to the right, trying to free up at
243374123bd7SChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
243444871b1bSChris Mason  *
243544871b1bSChris Mason  * returns 1 if the push failed because the other node didn't have enough
243644871b1bSChris Mason  * room, 0 if everything worked out and < 0 if there were major errors.
243799d8f83cSChris Mason  *
243899d8f83cSChris Mason  * this will push starting from min_slot to the end of the leaf.  It won't
243999d8f83cSChris Mason  * push any slot lower than min_slot
244074123bd7SChris Mason  */
244144871b1bSChris Mason static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
244299d8f83cSChris Mason 			   *root, struct btrfs_path *path,
244399d8f83cSChris Mason 			   int min_data_size, int data_size,
244499d8f83cSChris Mason 			   int empty, u32 min_slot)
2445be0e5c09SChris Mason {
244644871b1bSChris Mason 	struct extent_buffer *left = path->nodes[0];
244744871b1bSChris Mason 	struct extent_buffer *right;
244844871b1bSChris Mason 	struct extent_buffer *upper;
244944871b1bSChris Mason 	int slot;
245044871b1bSChris Mason 	int free_space;
245144871b1bSChris Mason 	u32 left_nritems;
245244871b1bSChris Mason 	int ret;
245344871b1bSChris Mason 
245444871b1bSChris Mason 	if (!path->nodes[1])
245544871b1bSChris Mason 		return 1;
245644871b1bSChris Mason 
245744871b1bSChris Mason 	slot = path->slots[1];
245844871b1bSChris Mason 	upper = path->nodes[1];
245944871b1bSChris Mason 	if (slot >= btrfs_header_nritems(upper) - 1)
246044871b1bSChris Mason 		return 1;
246144871b1bSChris Mason 
246244871b1bSChris Mason 	btrfs_assert_tree_locked(path->nodes[1]);
246344871b1bSChris Mason 
246444871b1bSChris Mason 	right = read_node_slot(root, upper, slot + 1);
246591ca338dSTsutomu Itoh 	if (right == NULL)
246691ca338dSTsutomu Itoh 		return 1;
246791ca338dSTsutomu Itoh 
246844871b1bSChris Mason 	btrfs_tree_lock(right);
246944871b1bSChris Mason 	btrfs_set_lock_blocking(right);
247044871b1bSChris Mason 
247144871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, right);
247244871b1bSChris Mason 	if (free_space < data_size)
247344871b1bSChris Mason 		goto out_unlock;
247444871b1bSChris Mason 
247544871b1bSChris Mason 	/* cow and double check */
247644871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, right, upper,
247744871b1bSChris Mason 			      slot + 1, &right);
247844871b1bSChris Mason 	if (ret)
247944871b1bSChris Mason 		goto out_unlock;
248044871b1bSChris Mason 
248144871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, right);
248244871b1bSChris Mason 	if (free_space < data_size)
248344871b1bSChris Mason 		goto out_unlock;
248444871b1bSChris Mason 
248544871b1bSChris Mason 	left_nritems = btrfs_header_nritems(left);
248644871b1bSChris Mason 	if (left_nritems == 0)
248744871b1bSChris Mason 		goto out_unlock;
248844871b1bSChris Mason 
248999d8f83cSChris Mason 	return __push_leaf_right(trans, root, path, min_data_size, empty,
249099d8f83cSChris Mason 				right, free_space, left_nritems, min_slot);
249144871b1bSChris Mason out_unlock:
249244871b1bSChris Mason 	btrfs_tree_unlock(right);
249344871b1bSChris Mason 	free_extent_buffer(right);
249444871b1bSChris Mason 	return 1;
249544871b1bSChris Mason }
249644871b1bSChris Mason 
249744871b1bSChris Mason /*
249844871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
249944871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
250099d8f83cSChris Mason  *
250199d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
250299d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us do all the
250399d8f83cSChris Mason  * items
250444871b1bSChris Mason  */
250544871b1bSChris Mason static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
250644871b1bSChris Mason 				     struct btrfs_root *root,
250744871b1bSChris Mason 				     struct btrfs_path *path, int data_size,
250844871b1bSChris Mason 				     int empty, struct extent_buffer *left,
250999d8f83cSChris Mason 				     int free_space, u32 right_nritems,
251099d8f83cSChris Mason 				     u32 max_slot)
251144871b1bSChris Mason {
25125f39d397SChris Mason 	struct btrfs_disk_key disk_key;
25135f39d397SChris Mason 	struct extent_buffer *right = path->nodes[0];
2514be0e5c09SChris Mason 	int i;
2515be0e5c09SChris Mason 	int push_space = 0;
2516be0e5c09SChris Mason 	int push_items = 0;
25170783fcfcSChris Mason 	struct btrfs_item *item;
25187518a238SChris Mason 	u32 old_left_nritems;
251934a38218SChris Mason 	u32 nr;
2520aa5d6bedSChris Mason 	int ret = 0;
2521db94535dSChris Mason 	u32 this_item_size;
2522db94535dSChris Mason 	u32 old_left_item_size;
2523be0e5c09SChris Mason 
252434a38218SChris Mason 	if (empty)
252599d8f83cSChris Mason 		nr = min(right_nritems, max_slot);
252634a38218SChris Mason 	else
252799d8f83cSChris Mason 		nr = min(right_nritems - 1, max_slot);
252834a38218SChris Mason 
252934a38218SChris Mason 	for (i = 0; i < nr; i++) {
25305f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2531db94535dSChris Mason 
253231840ae1SZheng Yan 		if (!empty && push_items > 0) {
253331840ae1SZheng Yan 			if (path->slots[0] < i)
253431840ae1SZheng Yan 				break;
253531840ae1SZheng Yan 			if (path->slots[0] == i) {
253631840ae1SZheng Yan 				int space = btrfs_leaf_free_space(root, right);
253731840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
253831840ae1SZheng Yan 					break;
253931840ae1SZheng Yan 			}
254031840ae1SZheng Yan 		}
254131840ae1SZheng Yan 
2542be0e5c09SChris Mason 		if (path->slots[0] == i)
254387b29b20SYan Zheng 			push_space += data_size;
2544db94535dSChris Mason 
2545db94535dSChris Mason 		this_item_size = btrfs_item_size(right, item);
2546db94535dSChris Mason 		if (this_item_size + sizeof(*item) + push_space > free_space)
2547be0e5c09SChris Mason 			break;
2548db94535dSChris Mason 
2549be0e5c09SChris Mason 		push_items++;
2550db94535dSChris Mason 		push_space += this_item_size + sizeof(*item);
2551be0e5c09SChris Mason 	}
2552db94535dSChris Mason 
2553be0e5c09SChris Mason 	if (push_items == 0) {
2554925baeddSChris Mason 		ret = 1;
2555925baeddSChris Mason 		goto out;
2556be0e5c09SChris Mason 	}
255734a38218SChris Mason 	if (!empty && push_items == btrfs_header_nritems(right))
2558a429e513SChris Mason 		WARN_ON(1);
25595f39d397SChris Mason 
2560be0e5c09SChris Mason 	/* push data from right to left */
25615f39d397SChris Mason 	copy_extent_buffer(left, right,
25625f39d397SChris Mason 			   btrfs_item_nr_offset(btrfs_header_nritems(left)),
25635f39d397SChris Mason 			   btrfs_item_nr_offset(0),
25645f39d397SChris Mason 			   push_items * sizeof(struct btrfs_item));
25655f39d397SChris Mason 
2566123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root) -
25675f39d397SChris Mason 		     btrfs_item_offset_nr(right, push_items - 1);
25685f39d397SChris Mason 
25695f39d397SChris Mason 	copy_extent_buffer(left, right, btrfs_leaf_data(left) +
2570d6025579SChris Mason 		     leaf_data_end(root, left) - push_space,
2571123abc88SChris Mason 		     btrfs_leaf_data(right) +
25725f39d397SChris Mason 		     btrfs_item_offset_nr(right, push_items - 1),
2573be0e5c09SChris Mason 		     push_space);
25745f39d397SChris Mason 	old_left_nritems = btrfs_header_nritems(left);
257587b29b20SYan Zheng 	BUG_ON(old_left_nritems <= 0);
2576eb60ceacSChris Mason 
2577db94535dSChris Mason 	old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
2578be0e5c09SChris Mason 	for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
25795f39d397SChris Mason 		u32 ioff;
2580db94535dSChris Mason 
25815f39d397SChris Mason 		item = btrfs_item_nr(left, i);
2582db94535dSChris Mason 
25835f39d397SChris Mason 		ioff = btrfs_item_offset(left, item);
25845f39d397SChris Mason 		btrfs_set_item_offset(left, item,
2585db94535dSChris Mason 		      ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
2586be0e5c09SChris Mason 	}
25875f39d397SChris Mason 	btrfs_set_header_nritems(left, old_left_nritems + push_items);
2588be0e5c09SChris Mason 
2589be0e5c09SChris Mason 	/* fixup right node */
259034a38218SChris Mason 	if (push_items > right_nritems) {
2591d397712bSChris Mason 		printk(KERN_CRIT "push items %d nr %u\n", push_items,
2592d397712bSChris Mason 		       right_nritems);
259334a38218SChris Mason 		WARN_ON(1);
259434a38218SChris Mason 	}
259534a38218SChris Mason 
259634a38218SChris Mason 	if (push_items < right_nritems) {
25975f39d397SChris Mason 		push_space = btrfs_item_offset_nr(right, push_items - 1) -
2598123abc88SChris Mason 						  leaf_data_end(root, right);
25995f39d397SChris Mason 		memmove_extent_buffer(right, btrfs_leaf_data(right) +
2600d6025579SChris Mason 				      BTRFS_LEAF_DATA_SIZE(root) - push_space,
2601d6025579SChris Mason 				      btrfs_leaf_data(right) +
2602123abc88SChris Mason 				      leaf_data_end(root, right), push_space);
26035f39d397SChris Mason 
26045f39d397SChris Mason 		memmove_extent_buffer(right, btrfs_item_nr_offset(0),
26055f39d397SChris Mason 			      btrfs_item_nr_offset(push_items),
26065f39d397SChris Mason 			     (btrfs_header_nritems(right) - push_items) *
26070783fcfcSChris Mason 			     sizeof(struct btrfs_item));
260834a38218SChris Mason 	}
2609eef1c494SYan 	right_nritems -= push_items;
2610eef1c494SYan 	btrfs_set_header_nritems(right, right_nritems);
2611123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root);
26125f39d397SChris Mason 	for (i = 0; i < right_nritems; i++) {
26135f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2614db94535dSChris Mason 
2615db94535dSChris Mason 		push_space = push_space - btrfs_item_size(right, item);
2616db94535dSChris Mason 		btrfs_set_item_offset(right, item, push_space);
2617db94535dSChris Mason 	}
2618eb60ceacSChris Mason 
26195f39d397SChris Mason 	btrfs_mark_buffer_dirty(left);
262034a38218SChris Mason 	if (right_nritems)
26215f39d397SChris Mason 		btrfs_mark_buffer_dirty(right);
2622f0486c68SYan, Zheng 	else
2623f0486c68SYan, Zheng 		clean_tree_block(trans, root, right);
2624098f59c2SChris Mason 
26255f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
2626143bede5SJeff Mahoney 	fixup_low_keys(trans, root, path, &disk_key, 1);
2627be0e5c09SChris Mason 
2628be0e5c09SChris Mason 	/* then fixup the leaf pointer in the path */
2629be0e5c09SChris Mason 	if (path->slots[0] < push_items) {
2630be0e5c09SChris Mason 		path->slots[0] += old_left_nritems;
2631925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
26325f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
26335f39d397SChris Mason 		path->nodes[0] = left;
2634be0e5c09SChris Mason 		path->slots[1] -= 1;
2635be0e5c09SChris Mason 	} else {
2636925baeddSChris Mason 		btrfs_tree_unlock(left);
26375f39d397SChris Mason 		free_extent_buffer(left);
2638be0e5c09SChris Mason 		path->slots[0] -= push_items;
2639be0e5c09SChris Mason 	}
2640eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
2641aa5d6bedSChris Mason 	return ret;
2642925baeddSChris Mason out:
2643925baeddSChris Mason 	btrfs_tree_unlock(left);
2644925baeddSChris Mason 	free_extent_buffer(left);
2645925baeddSChris Mason 	return ret;
2646be0e5c09SChris Mason }
2647be0e5c09SChris Mason 
264874123bd7SChris Mason /*
264944871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
265044871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
265199d8f83cSChris Mason  *
265299d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
265399d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us push all the
265499d8f83cSChris Mason  * items
265544871b1bSChris Mason  */
265644871b1bSChris Mason static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
265799d8f83cSChris Mason 			  *root, struct btrfs_path *path, int min_data_size,
265899d8f83cSChris Mason 			  int data_size, int empty, u32 max_slot)
265944871b1bSChris Mason {
266044871b1bSChris Mason 	struct extent_buffer *right = path->nodes[0];
266144871b1bSChris Mason 	struct extent_buffer *left;
266244871b1bSChris Mason 	int slot;
266344871b1bSChris Mason 	int free_space;
266444871b1bSChris Mason 	u32 right_nritems;
266544871b1bSChris Mason 	int ret = 0;
266644871b1bSChris Mason 
266744871b1bSChris Mason 	slot = path->slots[1];
266844871b1bSChris Mason 	if (slot == 0)
266944871b1bSChris Mason 		return 1;
267044871b1bSChris Mason 	if (!path->nodes[1])
267144871b1bSChris Mason 		return 1;
267244871b1bSChris Mason 
267344871b1bSChris Mason 	right_nritems = btrfs_header_nritems(right);
267444871b1bSChris Mason 	if (right_nritems == 0)
267544871b1bSChris Mason 		return 1;
267644871b1bSChris Mason 
267744871b1bSChris Mason 	btrfs_assert_tree_locked(path->nodes[1]);
267844871b1bSChris Mason 
267944871b1bSChris Mason 	left = read_node_slot(root, path->nodes[1], slot - 1);
268091ca338dSTsutomu Itoh 	if (left == NULL)
268191ca338dSTsutomu Itoh 		return 1;
268291ca338dSTsutomu Itoh 
268344871b1bSChris Mason 	btrfs_tree_lock(left);
268444871b1bSChris Mason 	btrfs_set_lock_blocking(left);
268544871b1bSChris Mason 
268644871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, left);
268744871b1bSChris Mason 	if (free_space < data_size) {
268844871b1bSChris Mason 		ret = 1;
268944871b1bSChris Mason 		goto out;
269044871b1bSChris Mason 	}
269144871b1bSChris Mason 
269244871b1bSChris Mason 	/* cow and double check */
269344871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, left,
269444871b1bSChris Mason 			      path->nodes[1], slot - 1, &left);
269544871b1bSChris Mason 	if (ret) {
269644871b1bSChris Mason 		/* we hit -ENOSPC, but it isn't fatal here */
269744871b1bSChris Mason 		ret = 1;
269844871b1bSChris Mason 		goto out;
269944871b1bSChris Mason 	}
270044871b1bSChris Mason 
270144871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, left);
270244871b1bSChris Mason 	if (free_space < data_size) {
270344871b1bSChris Mason 		ret = 1;
270444871b1bSChris Mason 		goto out;
270544871b1bSChris Mason 	}
270644871b1bSChris Mason 
270799d8f83cSChris Mason 	return __push_leaf_left(trans, root, path, min_data_size,
270899d8f83cSChris Mason 			       empty, left, free_space, right_nritems,
270999d8f83cSChris Mason 			       max_slot);
271044871b1bSChris Mason out:
271144871b1bSChris Mason 	btrfs_tree_unlock(left);
271244871b1bSChris Mason 	free_extent_buffer(left);
271344871b1bSChris Mason 	return ret;
271444871b1bSChris Mason }
271544871b1bSChris Mason 
271644871b1bSChris Mason /*
271774123bd7SChris Mason  * split the path's leaf in two, making sure there is at least data_size
271874123bd7SChris Mason  * available for the resulting leaf level of the path.
271974123bd7SChris Mason  */
2720143bede5SJeff Mahoney static noinline void copy_for_split(struct btrfs_trans_handle *trans,
2721e02119d5SChris Mason 				    struct btrfs_root *root,
272244871b1bSChris Mason 				    struct btrfs_path *path,
272344871b1bSChris Mason 				    struct extent_buffer *l,
272444871b1bSChris Mason 				    struct extent_buffer *right,
272544871b1bSChris Mason 				    int slot, int mid, int nritems)
2726be0e5c09SChris Mason {
2727be0e5c09SChris Mason 	int data_copy_size;
2728be0e5c09SChris Mason 	int rt_data_off;
2729be0e5c09SChris Mason 	int i;
2730d4dbff95SChris Mason 	struct btrfs_disk_key disk_key;
2731be0e5c09SChris Mason 
27325f39d397SChris Mason 	nritems = nritems - mid;
27335f39d397SChris Mason 	btrfs_set_header_nritems(right, nritems);
27345f39d397SChris Mason 	data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
27355f39d397SChris Mason 
27365f39d397SChris Mason 	copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
27375f39d397SChris Mason 			   btrfs_item_nr_offset(mid),
27385f39d397SChris Mason 			   nritems * sizeof(struct btrfs_item));
27395f39d397SChris Mason 
27405f39d397SChris Mason 	copy_extent_buffer(right, l,
2741d6025579SChris Mason 		     btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2742123abc88SChris Mason 		     data_copy_size, btrfs_leaf_data(l) +
2743123abc88SChris Mason 		     leaf_data_end(root, l), data_copy_size);
274474123bd7SChris Mason 
27455f39d397SChris Mason 	rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
27465f39d397SChris Mason 		      btrfs_item_end_nr(l, mid);
27475f39d397SChris Mason 
27485f39d397SChris Mason 	for (i = 0; i < nritems; i++) {
27495f39d397SChris Mason 		struct btrfs_item *item = btrfs_item_nr(right, i);
2750db94535dSChris Mason 		u32 ioff;
2751db94535dSChris Mason 
2752db94535dSChris Mason 		ioff = btrfs_item_offset(right, item);
27535f39d397SChris Mason 		btrfs_set_item_offset(right, item, ioff + rt_data_off);
27540783fcfcSChris Mason 	}
275574123bd7SChris Mason 
27565f39d397SChris Mason 	btrfs_set_header_nritems(l, mid);
27575f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
2758143bede5SJeff Mahoney 	insert_ptr(trans, root, path, &disk_key, right->start,
2759db94535dSChris Mason 		   path->slots[1] + 1, 1);
27605f39d397SChris Mason 
27615f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
27625f39d397SChris Mason 	btrfs_mark_buffer_dirty(l);
2763eb60ceacSChris Mason 	BUG_ON(path->slots[0] != slot);
27645f39d397SChris Mason 
2765be0e5c09SChris Mason 	if (mid <= slot) {
2766925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
27675f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
27685f39d397SChris Mason 		path->nodes[0] = right;
2769be0e5c09SChris Mason 		path->slots[0] -= mid;
2770be0e5c09SChris Mason 		path->slots[1] += 1;
2771925baeddSChris Mason 	} else {
2772925baeddSChris Mason 		btrfs_tree_unlock(right);
27735f39d397SChris Mason 		free_extent_buffer(right);
2774925baeddSChris Mason 	}
27755f39d397SChris Mason 
2776eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
277744871b1bSChris Mason }
277844871b1bSChris Mason 
277944871b1bSChris Mason /*
278099d8f83cSChris Mason  * double splits happen when we need to insert a big item in the middle
278199d8f83cSChris Mason  * of a leaf.  A double split can leave us with 3 mostly empty leaves:
278299d8f83cSChris Mason  * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
278399d8f83cSChris Mason  *          A                 B                 C
278499d8f83cSChris Mason  *
278599d8f83cSChris Mason  * We avoid this by trying to push the items on either side of our target
278699d8f83cSChris Mason  * into the adjacent leaves.  If all goes well we can avoid the double split
278799d8f83cSChris Mason  * completely.
278899d8f83cSChris Mason  */
278999d8f83cSChris Mason static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
279099d8f83cSChris Mason 					  struct btrfs_root *root,
279199d8f83cSChris Mason 					  struct btrfs_path *path,
279299d8f83cSChris Mason 					  int data_size)
279399d8f83cSChris Mason {
279499d8f83cSChris Mason 	int ret;
279599d8f83cSChris Mason 	int progress = 0;
279699d8f83cSChris Mason 	int slot;
279799d8f83cSChris Mason 	u32 nritems;
279899d8f83cSChris Mason 
279999d8f83cSChris Mason 	slot = path->slots[0];
280099d8f83cSChris Mason 
280199d8f83cSChris Mason 	/*
280299d8f83cSChris Mason 	 * try to push all the items after our slot into the
280399d8f83cSChris Mason 	 * right leaf
280499d8f83cSChris Mason 	 */
280599d8f83cSChris Mason 	ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
280699d8f83cSChris Mason 	if (ret < 0)
280799d8f83cSChris Mason 		return ret;
280899d8f83cSChris Mason 
280999d8f83cSChris Mason 	if (ret == 0)
281099d8f83cSChris Mason 		progress++;
281199d8f83cSChris Mason 
281299d8f83cSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
281399d8f83cSChris Mason 	/*
281499d8f83cSChris Mason 	 * our goal is to get our slot at the start or end of a leaf.  If
281599d8f83cSChris Mason 	 * we've done so we're done
281699d8f83cSChris Mason 	 */
281799d8f83cSChris Mason 	if (path->slots[0] == 0 || path->slots[0] == nritems)
281899d8f83cSChris Mason 		return 0;
281999d8f83cSChris Mason 
282099d8f83cSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
282199d8f83cSChris Mason 		return 0;
282299d8f83cSChris Mason 
282399d8f83cSChris Mason 	/* try to push all the items before our slot into the next leaf */
282499d8f83cSChris Mason 	slot = path->slots[0];
282599d8f83cSChris Mason 	ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
282699d8f83cSChris Mason 	if (ret < 0)
282799d8f83cSChris Mason 		return ret;
282899d8f83cSChris Mason 
282999d8f83cSChris Mason 	if (ret == 0)
283099d8f83cSChris Mason 		progress++;
283199d8f83cSChris Mason 
283299d8f83cSChris Mason 	if (progress)
283399d8f83cSChris Mason 		return 0;
283499d8f83cSChris Mason 	return 1;
283599d8f83cSChris Mason }
283699d8f83cSChris Mason 
283799d8f83cSChris Mason /*
283844871b1bSChris Mason  * split the path's leaf in two, making sure there is at least data_size
283944871b1bSChris Mason  * available for the resulting leaf level of the path.
284044871b1bSChris Mason  *
284144871b1bSChris Mason  * returns 0 if all went well and < 0 on failure.
284244871b1bSChris Mason  */
284344871b1bSChris Mason static noinline int split_leaf(struct btrfs_trans_handle *trans,
284444871b1bSChris Mason 			       struct btrfs_root *root,
284544871b1bSChris Mason 			       struct btrfs_key *ins_key,
284644871b1bSChris Mason 			       struct btrfs_path *path, int data_size,
284744871b1bSChris Mason 			       int extend)
284844871b1bSChris Mason {
28495d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
285044871b1bSChris Mason 	struct extent_buffer *l;
285144871b1bSChris Mason 	u32 nritems;
285244871b1bSChris Mason 	int mid;
285344871b1bSChris Mason 	int slot;
285444871b1bSChris Mason 	struct extent_buffer *right;
285544871b1bSChris Mason 	int ret = 0;
285644871b1bSChris Mason 	int wret;
28575d4f98a2SYan Zheng 	int split;
285844871b1bSChris Mason 	int num_doubles = 0;
285999d8f83cSChris Mason 	int tried_avoid_double = 0;
286044871b1bSChris Mason 
2861a5719521SYan, Zheng 	l = path->nodes[0];
2862a5719521SYan, Zheng 	slot = path->slots[0];
2863a5719521SYan, Zheng 	if (extend && data_size + btrfs_item_size_nr(l, slot) +
2864a5719521SYan, Zheng 	    sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2865a5719521SYan, Zheng 		return -EOVERFLOW;
2866a5719521SYan, Zheng 
286744871b1bSChris Mason 	/* first try to make some room by pushing left and right */
286899d8f83cSChris Mason 	if (data_size) {
286999d8f83cSChris Mason 		wret = push_leaf_right(trans, root, path, data_size,
287099d8f83cSChris Mason 				       data_size, 0, 0);
287144871b1bSChris Mason 		if (wret < 0)
287244871b1bSChris Mason 			return wret;
287344871b1bSChris Mason 		if (wret) {
287499d8f83cSChris Mason 			wret = push_leaf_left(trans, root, path, data_size,
287599d8f83cSChris Mason 					      data_size, 0, (u32)-1);
287644871b1bSChris Mason 			if (wret < 0)
287744871b1bSChris Mason 				return wret;
287844871b1bSChris Mason 		}
287944871b1bSChris Mason 		l = path->nodes[0];
288044871b1bSChris Mason 
288144871b1bSChris Mason 		/* did the pushes work? */
288244871b1bSChris Mason 		if (btrfs_leaf_free_space(root, l) >= data_size)
288344871b1bSChris Mason 			return 0;
288444871b1bSChris Mason 	}
288544871b1bSChris Mason 
288644871b1bSChris Mason 	if (!path->nodes[1]) {
288744871b1bSChris Mason 		ret = insert_new_root(trans, root, path, 1);
288844871b1bSChris Mason 		if (ret)
288944871b1bSChris Mason 			return ret;
289044871b1bSChris Mason 	}
289144871b1bSChris Mason again:
28925d4f98a2SYan Zheng 	split = 1;
289344871b1bSChris Mason 	l = path->nodes[0];
289444871b1bSChris Mason 	slot = path->slots[0];
289544871b1bSChris Mason 	nritems = btrfs_header_nritems(l);
289644871b1bSChris Mason 	mid = (nritems + 1) / 2;
289744871b1bSChris Mason 
28985d4f98a2SYan Zheng 	if (mid <= slot) {
28995d4f98a2SYan Zheng 		if (nritems == 1 ||
29005d4f98a2SYan Zheng 		    leaf_space_used(l, mid, nritems - mid) + data_size >
29015d4f98a2SYan Zheng 			BTRFS_LEAF_DATA_SIZE(root)) {
29025d4f98a2SYan Zheng 			if (slot >= nritems) {
29035d4f98a2SYan Zheng 				split = 0;
29045d4f98a2SYan Zheng 			} else {
29055d4f98a2SYan Zheng 				mid = slot;
29065d4f98a2SYan Zheng 				if (mid != nritems &&
29075d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
29085d4f98a2SYan Zheng 				    data_size > BTRFS_LEAF_DATA_SIZE(root)) {
290999d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
291099d8f83cSChris Mason 						goto push_for_double;
29115d4f98a2SYan Zheng 					split = 2;
29125d4f98a2SYan Zheng 				}
29135d4f98a2SYan Zheng 			}
29145d4f98a2SYan Zheng 		}
29155d4f98a2SYan Zheng 	} else {
29165d4f98a2SYan Zheng 		if (leaf_space_used(l, 0, mid) + data_size >
29175d4f98a2SYan Zheng 			BTRFS_LEAF_DATA_SIZE(root)) {
29185d4f98a2SYan Zheng 			if (!extend && data_size && slot == 0) {
29195d4f98a2SYan Zheng 				split = 0;
29205d4f98a2SYan Zheng 			} else if ((extend || !data_size) && slot == 0) {
29215d4f98a2SYan Zheng 				mid = 1;
29225d4f98a2SYan Zheng 			} else {
29235d4f98a2SYan Zheng 				mid = slot;
29245d4f98a2SYan Zheng 				if (mid != nritems &&
29255d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
29265d4f98a2SYan Zheng 				    data_size > BTRFS_LEAF_DATA_SIZE(root)) {
292799d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
292899d8f83cSChris Mason 						goto push_for_double;
29295d4f98a2SYan Zheng 					split = 2 ;
29305d4f98a2SYan Zheng 				}
29315d4f98a2SYan Zheng 			}
29325d4f98a2SYan Zheng 		}
29335d4f98a2SYan Zheng 	}
29345d4f98a2SYan Zheng 
29355d4f98a2SYan Zheng 	if (split == 0)
29365d4f98a2SYan Zheng 		btrfs_cpu_key_to_disk(&disk_key, ins_key);
29375d4f98a2SYan Zheng 	else
29385d4f98a2SYan Zheng 		btrfs_item_key(l, &disk_key, mid);
29395d4f98a2SYan Zheng 
29405d4f98a2SYan Zheng 	right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
294144871b1bSChris Mason 					root->root_key.objectid,
294266d7e7f0SArne Jansen 					&disk_key, 0, l->start, 0, 0);
2943f0486c68SYan, Zheng 	if (IS_ERR(right))
294444871b1bSChris Mason 		return PTR_ERR(right);
2945f0486c68SYan, Zheng 
2946f0486c68SYan, Zheng 	root_add_used(root, root->leafsize);
294744871b1bSChris Mason 
294844871b1bSChris Mason 	memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
294944871b1bSChris Mason 	btrfs_set_header_bytenr(right, right->start);
295044871b1bSChris Mason 	btrfs_set_header_generation(right, trans->transid);
29515d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
295244871b1bSChris Mason 	btrfs_set_header_owner(right, root->root_key.objectid);
295344871b1bSChris Mason 	btrfs_set_header_level(right, 0);
295444871b1bSChris Mason 	write_extent_buffer(right, root->fs_info->fsid,
295544871b1bSChris Mason 			    (unsigned long)btrfs_header_fsid(right),
295644871b1bSChris Mason 			    BTRFS_FSID_SIZE);
295744871b1bSChris Mason 
295844871b1bSChris Mason 	write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
295944871b1bSChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(right),
296044871b1bSChris Mason 			    BTRFS_UUID_SIZE);
296144871b1bSChris Mason 
29625d4f98a2SYan Zheng 	if (split == 0) {
296344871b1bSChris Mason 		if (mid <= slot) {
296444871b1bSChris Mason 			btrfs_set_header_nritems(right, 0);
2965143bede5SJeff Mahoney 			insert_ptr(trans, root, path, &disk_key, right->start,
296644871b1bSChris Mason 				   path->slots[1] + 1, 1);
296744871b1bSChris Mason 			btrfs_tree_unlock(path->nodes[0]);
296844871b1bSChris Mason 			free_extent_buffer(path->nodes[0]);
296944871b1bSChris Mason 			path->nodes[0] = right;
297044871b1bSChris Mason 			path->slots[0] = 0;
297144871b1bSChris Mason 			path->slots[1] += 1;
297244871b1bSChris Mason 		} else {
297344871b1bSChris Mason 			btrfs_set_header_nritems(right, 0);
2974143bede5SJeff Mahoney 			insert_ptr(trans, root, path, &disk_key, right->start,
297544871b1bSChris Mason 					  path->slots[1], 1);
297644871b1bSChris Mason 			btrfs_tree_unlock(path->nodes[0]);
297744871b1bSChris Mason 			free_extent_buffer(path->nodes[0]);
297844871b1bSChris Mason 			path->nodes[0] = right;
297944871b1bSChris Mason 			path->slots[0] = 0;
2980143bede5SJeff Mahoney 			if (path->slots[1] == 0)
2981143bede5SJeff Mahoney 				fixup_low_keys(trans, root, path,
2982143bede5SJeff Mahoney 					       &disk_key, 1);
29835d4f98a2SYan Zheng 		}
298444871b1bSChris Mason 		btrfs_mark_buffer_dirty(right);
298544871b1bSChris Mason 		return ret;
298644871b1bSChris Mason 	}
298744871b1bSChris Mason 
2988143bede5SJeff Mahoney 	copy_for_split(trans, root, path, l, right, slot, mid, nritems);
298944871b1bSChris Mason 
29905d4f98a2SYan Zheng 	if (split == 2) {
2991cc0c5538SChris Mason 		BUG_ON(num_doubles != 0);
2992cc0c5538SChris Mason 		num_doubles++;
2993cc0c5538SChris Mason 		goto again;
29943326d1b0SChris Mason 	}
299544871b1bSChris Mason 
2996143bede5SJeff Mahoney 	return 0;
299799d8f83cSChris Mason 
299899d8f83cSChris Mason push_for_double:
299999d8f83cSChris Mason 	push_for_double_split(trans, root, path, data_size);
300099d8f83cSChris Mason 	tried_avoid_double = 1;
300199d8f83cSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
300299d8f83cSChris Mason 		return 0;
300399d8f83cSChris Mason 	goto again;
3004be0e5c09SChris Mason }
3005be0e5c09SChris Mason 
3006ad48fd75SYan, Zheng static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3007ad48fd75SYan, Zheng 					 struct btrfs_root *root,
3008ad48fd75SYan, Zheng 					 struct btrfs_path *path, int ins_len)
3009ad48fd75SYan, Zheng {
3010ad48fd75SYan, Zheng 	struct btrfs_key key;
3011ad48fd75SYan, Zheng 	struct extent_buffer *leaf;
3012ad48fd75SYan, Zheng 	struct btrfs_file_extent_item *fi;
3013ad48fd75SYan, Zheng 	u64 extent_len = 0;
3014ad48fd75SYan, Zheng 	u32 item_size;
3015ad48fd75SYan, Zheng 	int ret;
3016ad48fd75SYan, Zheng 
3017ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3018ad48fd75SYan, Zheng 	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3019ad48fd75SYan, Zheng 
3020ad48fd75SYan, Zheng 	BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3021ad48fd75SYan, Zheng 	       key.type != BTRFS_EXTENT_CSUM_KEY);
3022ad48fd75SYan, Zheng 
3023ad48fd75SYan, Zheng 	if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3024ad48fd75SYan, Zheng 		return 0;
3025ad48fd75SYan, Zheng 
3026ad48fd75SYan, Zheng 	item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3027ad48fd75SYan, Zheng 	if (key.type == BTRFS_EXTENT_DATA_KEY) {
3028ad48fd75SYan, Zheng 		fi = btrfs_item_ptr(leaf, path->slots[0],
3029ad48fd75SYan, Zheng 				    struct btrfs_file_extent_item);
3030ad48fd75SYan, Zheng 		extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3031ad48fd75SYan, Zheng 	}
3032b3b4aa74SDavid Sterba 	btrfs_release_path(path);
3033ad48fd75SYan, Zheng 
3034ad48fd75SYan, Zheng 	path->keep_locks = 1;
3035ad48fd75SYan, Zheng 	path->search_for_split = 1;
3036ad48fd75SYan, Zheng 	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3037ad48fd75SYan, Zheng 	path->search_for_split = 0;
3038ad48fd75SYan, Zheng 	if (ret < 0)
3039ad48fd75SYan, Zheng 		goto err;
3040ad48fd75SYan, Zheng 
3041ad48fd75SYan, Zheng 	ret = -EAGAIN;
3042ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3043ad48fd75SYan, Zheng 	/* if our item isn't there or got smaller, return now */
3044ad48fd75SYan, Zheng 	if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3045ad48fd75SYan, Zheng 		goto err;
3046ad48fd75SYan, Zheng 
3047109f6aefSChris Mason 	/* the leaf has  changed, it now has room.  return now */
3048109f6aefSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3049109f6aefSChris Mason 		goto err;
3050109f6aefSChris Mason 
3051ad48fd75SYan, Zheng 	if (key.type == BTRFS_EXTENT_DATA_KEY) {
3052ad48fd75SYan, Zheng 		fi = btrfs_item_ptr(leaf, path->slots[0],
3053ad48fd75SYan, Zheng 				    struct btrfs_file_extent_item);
3054ad48fd75SYan, Zheng 		if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3055ad48fd75SYan, Zheng 			goto err;
3056ad48fd75SYan, Zheng 	}
3057ad48fd75SYan, Zheng 
3058ad48fd75SYan, Zheng 	btrfs_set_path_blocking(path);
3059ad48fd75SYan, Zheng 	ret = split_leaf(trans, root, &key, path, ins_len, 1);
3060f0486c68SYan, Zheng 	if (ret)
3061f0486c68SYan, Zheng 		goto err;
3062ad48fd75SYan, Zheng 
3063ad48fd75SYan, Zheng 	path->keep_locks = 0;
3064ad48fd75SYan, Zheng 	btrfs_unlock_up_safe(path, 1);
3065ad48fd75SYan, Zheng 	return 0;
3066ad48fd75SYan, Zheng err:
3067ad48fd75SYan, Zheng 	path->keep_locks = 0;
3068ad48fd75SYan, Zheng 	return ret;
3069ad48fd75SYan, Zheng }
3070ad48fd75SYan, Zheng 
3071ad48fd75SYan, Zheng static noinline int split_item(struct btrfs_trans_handle *trans,
3072459931ecSChris Mason 			       struct btrfs_root *root,
3073459931ecSChris Mason 			       struct btrfs_path *path,
3074459931ecSChris Mason 			       struct btrfs_key *new_key,
3075459931ecSChris Mason 			       unsigned long split_offset)
3076459931ecSChris Mason {
3077459931ecSChris Mason 	struct extent_buffer *leaf;
3078459931ecSChris Mason 	struct btrfs_item *item;
3079459931ecSChris Mason 	struct btrfs_item *new_item;
3080459931ecSChris Mason 	int slot;
3081ad48fd75SYan, Zheng 	char *buf;
3082459931ecSChris Mason 	u32 nritems;
3083ad48fd75SYan, Zheng 	u32 item_size;
3084459931ecSChris Mason 	u32 orig_offset;
3085459931ecSChris Mason 	struct btrfs_disk_key disk_key;
3086459931ecSChris Mason 
3087459931ecSChris Mason 	leaf = path->nodes[0];
3088b9473439SChris Mason 	BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3089b9473439SChris Mason 
3090b4ce94deSChris Mason 	btrfs_set_path_blocking(path);
3091b4ce94deSChris Mason 
3092459931ecSChris Mason 	item = btrfs_item_nr(leaf, path->slots[0]);
3093459931ecSChris Mason 	orig_offset = btrfs_item_offset(leaf, item);
3094459931ecSChris Mason 	item_size = btrfs_item_size(leaf, item);
3095459931ecSChris Mason 
3096459931ecSChris Mason 	buf = kmalloc(item_size, GFP_NOFS);
3097ad48fd75SYan, Zheng 	if (!buf)
3098ad48fd75SYan, Zheng 		return -ENOMEM;
3099ad48fd75SYan, Zheng 
3100459931ecSChris Mason 	read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3101459931ecSChris Mason 			    path->slots[0]), item_size);
3102ad48fd75SYan, Zheng 
3103459931ecSChris Mason 	slot = path->slots[0] + 1;
3104459931ecSChris Mason 	nritems = btrfs_header_nritems(leaf);
3105459931ecSChris Mason 	if (slot != nritems) {
3106459931ecSChris Mason 		/* shift the items */
3107459931ecSChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3108459931ecSChris Mason 				btrfs_item_nr_offset(slot),
3109459931ecSChris Mason 				(nritems - slot) * sizeof(struct btrfs_item));
3110459931ecSChris Mason 	}
3111459931ecSChris Mason 
3112459931ecSChris Mason 	btrfs_cpu_key_to_disk(&disk_key, new_key);
3113459931ecSChris Mason 	btrfs_set_item_key(leaf, &disk_key, slot);
3114459931ecSChris Mason 
3115459931ecSChris Mason 	new_item = btrfs_item_nr(leaf, slot);
3116459931ecSChris Mason 
3117459931ecSChris Mason 	btrfs_set_item_offset(leaf, new_item, orig_offset);
3118459931ecSChris Mason 	btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3119459931ecSChris Mason 
3120459931ecSChris Mason 	btrfs_set_item_offset(leaf, item,
3121459931ecSChris Mason 			      orig_offset + item_size - split_offset);
3122459931ecSChris Mason 	btrfs_set_item_size(leaf, item, split_offset);
3123459931ecSChris Mason 
3124459931ecSChris Mason 	btrfs_set_header_nritems(leaf, nritems + 1);
3125459931ecSChris Mason 
3126459931ecSChris Mason 	/* write the data for the start of the original item */
3127459931ecSChris Mason 	write_extent_buffer(leaf, buf,
3128459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, path->slots[0]),
3129459931ecSChris Mason 			    split_offset);
3130459931ecSChris Mason 
3131459931ecSChris Mason 	/* write the data for the new item */
3132459931ecSChris Mason 	write_extent_buffer(leaf, buf + split_offset,
3133459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, slot),
3134459931ecSChris Mason 			    item_size - split_offset);
3135459931ecSChris Mason 	btrfs_mark_buffer_dirty(leaf);
3136459931ecSChris Mason 
3137ad48fd75SYan, Zheng 	BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
3138459931ecSChris Mason 	kfree(buf);
3139ad48fd75SYan, Zheng 	return 0;
3140ad48fd75SYan, Zheng }
3141ad48fd75SYan, Zheng 
3142ad48fd75SYan, Zheng /*
3143ad48fd75SYan, Zheng  * This function splits a single item into two items,
3144ad48fd75SYan, Zheng  * giving 'new_key' to the new item and splitting the
3145ad48fd75SYan, Zheng  * old one at split_offset (from the start of the item).
3146ad48fd75SYan, Zheng  *
3147ad48fd75SYan, Zheng  * The path may be released by this operation.  After
3148ad48fd75SYan, Zheng  * the split, the path is pointing to the old item.  The
3149ad48fd75SYan, Zheng  * new item is going to be in the same node as the old one.
3150ad48fd75SYan, Zheng  *
3151ad48fd75SYan, Zheng  * Note, the item being split must be smaller enough to live alone on
3152ad48fd75SYan, Zheng  * a tree block with room for one extra struct btrfs_item
3153ad48fd75SYan, Zheng  *
3154ad48fd75SYan, Zheng  * This allows us to split the item in place, keeping a lock on the
3155ad48fd75SYan, Zheng  * leaf the entire time.
3156ad48fd75SYan, Zheng  */
3157ad48fd75SYan, Zheng int btrfs_split_item(struct btrfs_trans_handle *trans,
3158ad48fd75SYan, Zheng 		     struct btrfs_root *root,
3159ad48fd75SYan, Zheng 		     struct btrfs_path *path,
3160ad48fd75SYan, Zheng 		     struct btrfs_key *new_key,
3161ad48fd75SYan, Zheng 		     unsigned long split_offset)
3162ad48fd75SYan, Zheng {
3163ad48fd75SYan, Zheng 	int ret;
3164ad48fd75SYan, Zheng 	ret = setup_leaf_for_split(trans, root, path,
3165ad48fd75SYan, Zheng 				   sizeof(struct btrfs_item));
3166ad48fd75SYan, Zheng 	if (ret)
3167459931ecSChris Mason 		return ret;
3168ad48fd75SYan, Zheng 
3169ad48fd75SYan, Zheng 	ret = split_item(trans, root, path, new_key, split_offset);
3170ad48fd75SYan, Zheng 	return ret;
3171ad48fd75SYan, Zheng }
3172ad48fd75SYan, Zheng 
3173ad48fd75SYan, Zheng /*
3174ad48fd75SYan, Zheng  * This function duplicate a item, giving 'new_key' to the new item.
3175ad48fd75SYan, Zheng  * It guarantees both items live in the same tree leaf and the new item
3176ad48fd75SYan, Zheng  * is contiguous with the original item.
3177ad48fd75SYan, Zheng  *
3178ad48fd75SYan, Zheng  * This allows us to split file extent in place, keeping a lock on the
3179ad48fd75SYan, Zheng  * leaf the entire time.
3180ad48fd75SYan, Zheng  */
3181ad48fd75SYan, Zheng int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3182ad48fd75SYan, Zheng 			 struct btrfs_root *root,
3183ad48fd75SYan, Zheng 			 struct btrfs_path *path,
3184ad48fd75SYan, Zheng 			 struct btrfs_key *new_key)
3185ad48fd75SYan, Zheng {
3186ad48fd75SYan, Zheng 	struct extent_buffer *leaf;
3187ad48fd75SYan, Zheng 	int ret;
3188ad48fd75SYan, Zheng 	u32 item_size;
3189ad48fd75SYan, Zheng 
3190ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3191ad48fd75SYan, Zheng 	item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3192ad48fd75SYan, Zheng 	ret = setup_leaf_for_split(trans, root, path,
3193ad48fd75SYan, Zheng 				   item_size + sizeof(struct btrfs_item));
3194ad48fd75SYan, Zheng 	if (ret)
3195ad48fd75SYan, Zheng 		return ret;
3196ad48fd75SYan, Zheng 
3197ad48fd75SYan, Zheng 	path->slots[0]++;
3198143bede5SJeff Mahoney 	setup_items_for_insert(trans, root, path, new_key, &item_size,
3199ad48fd75SYan, Zheng 			       item_size, item_size +
3200ad48fd75SYan, Zheng 			       sizeof(struct btrfs_item), 1);
3201ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3202ad48fd75SYan, Zheng 	memcpy_extent_buffer(leaf,
3203ad48fd75SYan, Zheng 			     btrfs_item_ptr_offset(leaf, path->slots[0]),
3204ad48fd75SYan, Zheng 			     btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3205ad48fd75SYan, Zheng 			     item_size);
3206ad48fd75SYan, Zheng 	return 0;
3207459931ecSChris Mason }
3208459931ecSChris Mason 
3209459931ecSChris Mason /*
3210d352ac68SChris Mason  * make the item pointed to by the path smaller.  new_size indicates
3211d352ac68SChris Mason  * how small to make it, and from_end tells us if we just chop bytes
3212d352ac68SChris Mason  * off the end of the item or if we shift the item to chop bytes off
3213d352ac68SChris Mason  * the front.
3214d352ac68SChris Mason  */
3215143bede5SJeff Mahoney void btrfs_truncate_item(struct btrfs_trans_handle *trans,
3216b18c6685SChris Mason 			 struct btrfs_root *root,
3217b18c6685SChris Mason 			 struct btrfs_path *path,
3218179e29e4SChris Mason 			 u32 new_size, int from_end)
3219b18c6685SChris Mason {
3220b18c6685SChris Mason 	int slot;
32215f39d397SChris Mason 	struct extent_buffer *leaf;
32225f39d397SChris Mason 	struct btrfs_item *item;
3223b18c6685SChris Mason 	u32 nritems;
3224b18c6685SChris Mason 	unsigned int data_end;
3225b18c6685SChris Mason 	unsigned int old_data_start;
3226b18c6685SChris Mason 	unsigned int old_size;
3227b18c6685SChris Mason 	unsigned int size_diff;
3228b18c6685SChris Mason 	int i;
3229b18c6685SChris Mason 
32305f39d397SChris Mason 	leaf = path->nodes[0];
3231179e29e4SChris Mason 	slot = path->slots[0];
3232179e29e4SChris Mason 
3233179e29e4SChris Mason 	old_size = btrfs_item_size_nr(leaf, slot);
3234179e29e4SChris Mason 	if (old_size == new_size)
3235143bede5SJeff Mahoney 		return;
3236b18c6685SChris Mason 
32375f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3238b18c6685SChris Mason 	data_end = leaf_data_end(root, leaf);
3239b18c6685SChris Mason 
32405f39d397SChris Mason 	old_data_start = btrfs_item_offset_nr(leaf, slot);
3241179e29e4SChris Mason 
3242b18c6685SChris Mason 	size_diff = old_size - new_size;
3243b18c6685SChris Mason 
3244b18c6685SChris Mason 	BUG_ON(slot < 0);
3245b18c6685SChris Mason 	BUG_ON(slot >= nritems);
3246b18c6685SChris Mason 
3247b18c6685SChris Mason 	/*
3248b18c6685SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3249b18c6685SChris Mason 	 */
3250b18c6685SChris Mason 	/* first correct the data pointers */
3251b18c6685SChris Mason 	for (i = slot; i < nritems; i++) {
32525f39d397SChris Mason 		u32 ioff;
32535f39d397SChris Mason 		item = btrfs_item_nr(leaf, i);
3254db94535dSChris Mason 
32555f39d397SChris Mason 		ioff = btrfs_item_offset(leaf, item);
32565f39d397SChris Mason 		btrfs_set_item_offset(leaf, item, ioff + size_diff);
3257b18c6685SChris Mason 	}
3258db94535dSChris Mason 
3259b18c6685SChris Mason 	/* shift the data */
3260179e29e4SChris Mason 	if (from_end) {
32615f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3262b18c6685SChris Mason 			      data_end + size_diff, btrfs_leaf_data(leaf) +
3263b18c6685SChris Mason 			      data_end, old_data_start + new_size - data_end);
3264179e29e4SChris Mason 	} else {
3265179e29e4SChris Mason 		struct btrfs_disk_key disk_key;
3266179e29e4SChris Mason 		u64 offset;
3267179e29e4SChris Mason 
3268179e29e4SChris Mason 		btrfs_item_key(leaf, &disk_key, slot);
3269179e29e4SChris Mason 
3270179e29e4SChris Mason 		if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3271179e29e4SChris Mason 			unsigned long ptr;
3272179e29e4SChris Mason 			struct btrfs_file_extent_item *fi;
3273179e29e4SChris Mason 
3274179e29e4SChris Mason 			fi = btrfs_item_ptr(leaf, slot,
3275179e29e4SChris Mason 					    struct btrfs_file_extent_item);
3276179e29e4SChris Mason 			fi = (struct btrfs_file_extent_item *)(
3277179e29e4SChris Mason 			     (unsigned long)fi - size_diff);
3278179e29e4SChris Mason 
3279179e29e4SChris Mason 			if (btrfs_file_extent_type(leaf, fi) ==
3280179e29e4SChris Mason 			    BTRFS_FILE_EXTENT_INLINE) {
3281179e29e4SChris Mason 				ptr = btrfs_item_ptr_offset(leaf, slot);
3282179e29e4SChris Mason 				memmove_extent_buffer(leaf, ptr,
3283179e29e4SChris Mason 				      (unsigned long)fi,
3284179e29e4SChris Mason 				      offsetof(struct btrfs_file_extent_item,
3285179e29e4SChris Mason 						 disk_bytenr));
3286179e29e4SChris Mason 			}
3287179e29e4SChris Mason 		}
3288179e29e4SChris Mason 
3289179e29e4SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3290179e29e4SChris Mason 			      data_end + size_diff, btrfs_leaf_data(leaf) +
3291179e29e4SChris Mason 			      data_end, old_data_start - data_end);
3292179e29e4SChris Mason 
3293179e29e4SChris Mason 		offset = btrfs_disk_key_offset(&disk_key);
3294179e29e4SChris Mason 		btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3295179e29e4SChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot);
3296179e29e4SChris Mason 		if (slot == 0)
3297179e29e4SChris Mason 			fixup_low_keys(trans, root, path, &disk_key, 1);
3298179e29e4SChris Mason 	}
32995f39d397SChris Mason 
33005f39d397SChris Mason 	item = btrfs_item_nr(leaf, slot);
33015f39d397SChris Mason 	btrfs_set_item_size(leaf, item, new_size);
33025f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
3303b18c6685SChris Mason 
33045f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
33055f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
3306b18c6685SChris Mason 		BUG();
33075f39d397SChris Mason 	}
3308b18c6685SChris Mason }
3309b18c6685SChris Mason 
3310d352ac68SChris Mason /*
3311d352ac68SChris Mason  * make the item pointed to by the path bigger, data_size is the new size.
3312d352ac68SChris Mason  */
3313143bede5SJeff Mahoney void btrfs_extend_item(struct btrfs_trans_handle *trans,
33145f39d397SChris Mason 		       struct btrfs_root *root, struct btrfs_path *path,
33155f39d397SChris Mason 		       u32 data_size)
33166567e837SChris Mason {
33176567e837SChris Mason 	int slot;
33185f39d397SChris Mason 	struct extent_buffer *leaf;
33195f39d397SChris Mason 	struct btrfs_item *item;
33206567e837SChris Mason 	u32 nritems;
33216567e837SChris Mason 	unsigned int data_end;
33226567e837SChris Mason 	unsigned int old_data;
33236567e837SChris Mason 	unsigned int old_size;
33246567e837SChris Mason 	int i;
33256567e837SChris Mason 
33265f39d397SChris Mason 	leaf = path->nodes[0];
33276567e837SChris Mason 
33285f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
33296567e837SChris Mason 	data_end = leaf_data_end(root, leaf);
33306567e837SChris Mason 
33315f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < data_size) {
33325f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
33336567e837SChris Mason 		BUG();
33345f39d397SChris Mason 	}
33356567e837SChris Mason 	slot = path->slots[0];
33365f39d397SChris Mason 	old_data = btrfs_item_end_nr(leaf, slot);
33376567e837SChris Mason 
33386567e837SChris Mason 	BUG_ON(slot < 0);
33393326d1b0SChris Mason 	if (slot >= nritems) {
33403326d1b0SChris Mason 		btrfs_print_leaf(root, leaf);
3341d397712bSChris Mason 		printk(KERN_CRIT "slot %d too large, nritems %d\n",
3342d397712bSChris Mason 		       slot, nritems);
33433326d1b0SChris Mason 		BUG_ON(1);
33443326d1b0SChris Mason 	}
33456567e837SChris Mason 
33466567e837SChris Mason 	/*
33476567e837SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
33486567e837SChris Mason 	 */
33496567e837SChris Mason 	/* first correct the data pointers */
33506567e837SChris Mason 	for (i = slot; i < nritems; i++) {
33515f39d397SChris Mason 		u32 ioff;
33525f39d397SChris Mason 		item = btrfs_item_nr(leaf, i);
3353db94535dSChris Mason 
33545f39d397SChris Mason 		ioff = btrfs_item_offset(leaf, item);
33555f39d397SChris Mason 		btrfs_set_item_offset(leaf, item, ioff - data_size);
33566567e837SChris Mason 	}
33575f39d397SChris Mason 
33586567e837SChris Mason 	/* shift the data */
33595f39d397SChris Mason 	memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
33606567e837SChris Mason 		      data_end - data_size, btrfs_leaf_data(leaf) +
33616567e837SChris Mason 		      data_end, old_data - data_end);
33625f39d397SChris Mason 
33636567e837SChris Mason 	data_end = old_data;
33645f39d397SChris Mason 	old_size = btrfs_item_size_nr(leaf, slot);
33655f39d397SChris Mason 	item = btrfs_item_nr(leaf, slot);
33665f39d397SChris Mason 	btrfs_set_item_size(leaf, item, old_size + data_size);
33675f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
33686567e837SChris Mason 
33695f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
33705f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
33716567e837SChris Mason 		BUG();
33725f39d397SChris Mason 	}
33736567e837SChris Mason }
33746567e837SChris Mason 
337574123bd7SChris Mason /*
3376d352ac68SChris Mason  * Given a key and some data, insert items into the tree.
337774123bd7SChris Mason  * This does all the path init required, making room in the tree if needed.
3378f3465ca4SJosef Bacik  * Returns the number of keys that were inserted.
3379f3465ca4SJosef Bacik  */
3380f3465ca4SJosef Bacik int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3381f3465ca4SJosef Bacik 			    struct btrfs_root *root,
3382f3465ca4SJosef Bacik 			    struct btrfs_path *path,
3383f3465ca4SJosef Bacik 			    struct btrfs_key *cpu_key, u32 *data_size,
3384f3465ca4SJosef Bacik 			    int nr)
3385f3465ca4SJosef Bacik {
3386f3465ca4SJosef Bacik 	struct extent_buffer *leaf;
3387f3465ca4SJosef Bacik 	struct btrfs_item *item;
3388f3465ca4SJosef Bacik 	int ret = 0;
3389f3465ca4SJosef Bacik 	int slot;
3390f3465ca4SJosef Bacik 	int i;
3391f3465ca4SJosef Bacik 	u32 nritems;
3392f3465ca4SJosef Bacik 	u32 total_data = 0;
3393f3465ca4SJosef Bacik 	u32 total_size = 0;
3394f3465ca4SJosef Bacik 	unsigned int data_end;
3395f3465ca4SJosef Bacik 	struct btrfs_disk_key disk_key;
3396f3465ca4SJosef Bacik 	struct btrfs_key found_key;
3397f3465ca4SJosef Bacik 
339887b29b20SYan Zheng 	for (i = 0; i < nr; i++) {
339987b29b20SYan Zheng 		if (total_size + data_size[i] + sizeof(struct btrfs_item) >
340087b29b20SYan Zheng 		    BTRFS_LEAF_DATA_SIZE(root)) {
340187b29b20SYan Zheng 			break;
340287b29b20SYan Zheng 			nr = i;
340387b29b20SYan Zheng 		}
3404f3465ca4SJosef Bacik 		total_data += data_size[i];
340587b29b20SYan Zheng 		total_size += data_size[i] + sizeof(struct btrfs_item);
340687b29b20SYan Zheng 	}
340787b29b20SYan Zheng 	BUG_ON(nr == 0);
3408f3465ca4SJosef Bacik 
3409f3465ca4SJosef Bacik 	ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3410f3465ca4SJosef Bacik 	if (ret == 0)
3411f3465ca4SJosef Bacik 		return -EEXIST;
3412f3465ca4SJosef Bacik 	if (ret < 0)
3413f3465ca4SJosef Bacik 		goto out;
3414f3465ca4SJosef Bacik 
3415f3465ca4SJosef Bacik 	leaf = path->nodes[0];
3416f3465ca4SJosef Bacik 
3417f3465ca4SJosef Bacik 	nritems = btrfs_header_nritems(leaf);
3418f3465ca4SJosef Bacik 	data_end = leaf_data_end(root, leaf);
3419f3465ca4SJosef Bacik 
3420f3465ca4SJosef Bacik 	if (btrfs_leaf_free_space(root, leaf) < total_size) {
3421f3465ca4SJosef Bacik 		for (i = nr; i >= 0; i--) {
3422f3465ca4SJosef Bacik 			total_data -= data_size[i];
3423f3465ca4SJosef Bacik 			total_size -= data_size[i] + sizeof(struct btrfs_item);
3424f3465ca4SJosef Bacik 			if (total_size < btrfs_leaf_free_space(root, leaf))
3425f3465ca4SJosef Bacik 				break;
3426f3465ca4SJosef Bacik 		}
3427f3465ca4SJosef Bacik 		nr = i;
3428f3465ca4SJosef Bacik 	}
3429f3465ca4SJosef Bacik 
3430f3465ca4SJosef Bacik 	slot = path->slots[0];
3431f3465ca4SJosef Bacik 	BUG_ON(slot < 0);
3432f3465ca4SJosef Bacik 
3433f3465ca4SJosef Bacik 	if (slot != nritems) {
3434f3465ca4SJosef Bacik 		unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3435f3465ca4SJosef Bacik 
3436f3465ca4SJosef Bacik 		item = btrfs_item_nr(leaf, slot);
3437f3465ca4SJosef Bacik 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
3438f3465ca4SJosef Bacik 
3439f3465ca4SJosef Bacik 		/* figure out how many keys we can insert in here */
3440f3465ca4SJosef Bacik 		total_data = data_size[0];
3441f3465ca4SJosef Bacik 		for (i = 1; i < nr; i++) {
34425d4f98a2SYan Zheng 			if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3443f3465ca4SJosef Bacik 				break;
3444f3465ca4SJosef Bacik 			total_data += data_size[i];
3445f3465ca4SJosef Bacik 		}
3446f3465ca4SJosef Bacik 		nr = i;
3447f3465ca4SJosef Bacik 
3448f3465ca4SJosef Bacik 		if (old_data < data_end) {
3449f3465ca4SJosef Bacik 			btrfs_print_leaf(root, leaf);
3450d397712bSChris Mason 			printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3451f3465ca4SJosef Bacik 			       slot, old_data, data_end);
3452f3465ca4SJosef Bacik 			BUG_ON(1);
3453f3465ca4SJosef Bacik 		}
3454f3465ca4SJosef Bacik 		/*
3455f3465ca4SJosef Bacik 		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3456f3465ca4SJosef Bacik 		 */
3457f3465ca4SJosef Bacik 		/* first correct the data pointers */
3458f3465ca4SJosef Bacik 		for (i = slot; i < nritems; i++) {
3459f3465ca4SJosef Bacik 			u32 ioff;
3460f3465ca4SJosef Bacik 
3461f3465ca4SJosef Bacik 			item = btrfs_item_nr(leaf, i);
3462f3465ca4SJosef Bacik 			ioff = btrfs_item_offset(leaf, item);
3463f3465ca4SJosef Bacik 			btrfs_set_item_offset(leaf, item, ioff - total_data);
3464f3465ca4SJosef Bacik 		}
3465f3465ca4SJosef Bacik 		/* shift the items */
3466f3465ca4SJosef Bacik 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3467f3465ca4SJosef Bacik 			      btrfs_item_nr_offset(slot),
3468f3465ca4SJosef Bacik 			      (nritems - slot) * sizeof(struct btrfs_item));
3469f3465ca4SJosef Bacik 
3470f3465ca4SJosef Bacik 		/* shift the data */
3471f3465ca4SJosef Bacik 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3472f3465ca4SJosef Bacik 			      data_end - total_data, btrfs_leaf_data(leaf) +
3473f3465ca4SJosef Bacik 			      data_end, old_data - data_end);
3474f3465ca4SJosef Bacik 		data_end = old_data;
3475f3465ca4SJosef Bacik 	} else {
3476f3465ca4SJosef Bacik 		/*
3477f3465ca4SJosef Bacik 		 * this sucks but it has to be done, if we are inserting at
3478f3465ca4SJosef Bacik 		 * the end of the leaf only insert 1 of the items, since we
3479f3465ca4SJosef Bacik 		 * have no way of knowing whats on the next leaf and we'd have
3480f3465ca4SJosef Bacik 		 * to drop our current locks to figure it out
3481f3465ca4SJosef Bacik 		 */
3482f3465ca4SJosef Bacik 		nr = 1;
3483f3465ca4SJosef Bacik 	}
3484f3465ca4SJosef Bacik 
3485f3465ca4SJosef Bacik 	/* setup the item for the new data */
3486f3465ca4SJosef Bacik 	for (i = 0; i < nr; i++) {
3487f3465ca4SJosef Bacik 		btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3488f3465ca4SJosef Bacik 		btrfs_set_item_key(leaf, &disk_key, slot + i);
3489f3465ca4SJosef Bacik 		item = btrfs_item_nr(leaf, slot + i);
3490f3465ca4SJosef Bacik 		btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3491f3465ca4SJosef Bacik 		data_end -= data_size[i];
3492f3465ca4SJosef Bacik 		btrfs_set_item_size(leaf, item, data_size[i]);
3493f3465ca4SJosef Bacik 	}
3494f3465ca4SJosef Bacik 	btrfs_set_header_nritems(leaf, nritems + nr);
3495f3465ca4SJosef Bacik 	btrfs_mark_buffer_dirty(leaf);
3496f3465ca4SJosef Bacik 
3497f3465ca4SJosef Bacik 	ret = 0;
3498f3465ca4SJosef Bacik 	if (slot == 0) {
3499f3465ca4SJosef Bacik 		btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3500143bede5SJeff Mahoney 		fixup_low_keys(trans, root, path, &disk_key, 1);
3501f3465ca4SJosef Bacik 	}
3502f3465ca4SJosef Bacik 
3503f3465ca4SJosef Bacik 	if (btrfs_leaf_free_space(root, leaf) < 0) {
3504f3465ca4SJosef Bacik 		btrfs_print_leaf(root, leaf);
3505f3465ca4SJosef Bacik 		BUG();
3506f3465ca4SJosef Bacik 	}
3507f3465ca4SJosef Bacik out:
3508f3465ca4SJosef Bacik 	if (!ret)
3509f3465ca4SJosef Bacik 		ret = nr;
3510f3465ca4SJosef Bacik 	return ret;
3511f3465ca4SJosef Bacik }
3512f3465ca4SJosef Bacik 
3513f3465ca4SJosef Bacik /*
351444871b1bSChris Mason  * this is a helper for btrfs_insert_empty_items, the main goal here is
351544871b1bSChris Mason  * to save stack depth by doing the bulk of the work in a function
351644871b1bSChris Mason  * that doesn't call btrfs_search_slot
351774123bd7SChris Mason  */
3518143bede5SJeff Mahoney void setup_items_for_insert(struct btrfs_trans_handle *trans,
351944871b1bSChris Mason 			    struct btrfs_root *root, struct btrfs_path *path,
35209c58309dSChris Mason 			    struct btrfs_key *cpu_key, u32 *data_size,
352144871b1bSChris Mason 			    u32 total_data, u32 total_size, int nr)
3522be0e5c09SChris Mason {
35235f39d397SChris Mason 	struct btrfs_item *item;
35249c58309dSChris Mason 	int i;
35257518a238SChris Mason 	u32 nritems;
3526be0e5c09SChris Mason 	unsigned int data_end;
3527e2fa7227SChris Mason 	struct btrfs_disk_key disk_key;
352844871b1bSChris Mason 	struct extent_buffer *leaf;
352944871b1bSChris Mason 	int slot;
3530e2fa7227SChris Mason 
35315f39d397SChris Mason 	leaf = path->nodes[0];
353244871b1bSChris Mason 	slot = path->slots[0];
353374123bd7SChris Mason 
35345f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3535123abc88SChris Mason 	data_end = leaf_data_end(root, leaf);
3536eb60ceacSChris Mason 
3537f25956ccSChris Mason 	if (btrfs_leaf_free_space(root, leaf) < total_size) {
35383326d1b0SChris Mason 		btrfs_print_leaf(root, leaf);
3539d397712bSChris Mason 		printk(KERN_CRIT "not enough freespace need %u have %d\n",
35409c58309dSChris Mason 		       total_size, btrfs_leaf_free_space(root, leaf));
3541be0e5c09SChris Mason 		BUG();
3542d4dbff95SChris Mason 	}
35435f39d397SChris Mason 
3544be0e5c09SChris Mason 	if (slot != nritems) {
35455f39d397SChris Mason 		unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3546be0e5c09SChris Mason 
35475f39d397SChris Mason 		if (old_data < data_end) {
35485f39d397SChris Mason 			btrfs_print_leaf(root, leaf);
3549d397712bSChris Mason 			printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
35505f39d397SChris Mason 			       slot, old_data, data_end);
35515f39d397SChris Mason 			BUG_ON(1);
35525f39d397SChris Mason 		}
3553be0e5c09SChris Mason 		/*
3554be0e5c09SChris Mason 		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3555be0e5c09SChris Mason 		 */
3556be0e5c09SChris Mason 		/* first correct the data pointers */
35570783fcfcSChris Mason 		for (i = slot; i < nritems; i++) {
35585f39d397SChris Mason 			u32 ioff;
3559db94535dSChris Mason 
35605f39d397SChris Mason 			item = btrfs_item_nr(leaf, i);
35615f39d397SChris Mason 			ioff = btrfs_item_offset(leaf, item);
35629c58309dSChris Mason 			btrfs_set_item_offset(leaf, item, ioff - total_data);
35630783fcfcSChris Mason 		}
3564be0e5c09SChris Mason 		/* shift the items */
35659c58309dSChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
35665f39d397SChris Mason 			      btrfs_item_nr_offset(slot),
35670783fcfcSChris Mason 			      (nritems - slot) * sizeof(struct btrfs_item));
3568be0e5c09SChris Mason 
3569be0e5c09SChris Mason 		/* shift the data */
35705f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
35719c58309dSChris Mason 			      data_end - total_data, btrfs_leaf_data(leaf) +
3572be0e5c09SChris Mason 			      data_end, old_data - data_end);
3573be0e5c09SChris Mason 		data_end = old_data;
3574be0e5c09SChris Mason 	}
35755f39d397SChris Mason 
357662e2749eSChris Mason 	/* setup the item for the new data */
35779c58309dSChris Mason 	for (i = 0; i < nr; i++) {
35789c58309dSChris Mason 		btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
35799c58309dSChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot + i);
35809c58309dSChris Mason 		item = btrfs_item_nr(leaf, slot + i);
35819c58309dSChris Mason 		btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
35829c58309dSChris Mason 		data_end -= data_size[i];
35839c58309dSChris Mason 		btrfs_set_item_size(leaf, item, data_size[i]);
35849c58309dSChris Mason 	}
358544871b1bSChris Mason 
35869c58309dSChris Mason 	btrfs_set_header_nritems(leaf, nritems + nr);
3587aa5d6bedSChris Mason 
35885a01a2e3SChris Mason 	if (slot == 0) {
35895a01a2e3SChris Mason 		btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3590143bede5SJeff Mahoney 		fixup_low_keys(trans, root, path, &disk_key, 1);
35915a01a2e3SChris Mason 	}
3592b9473439SChris Mason 	btrfs_unlock_up_safe(path, 1);
3593b9473439SChris Mason 	btrfs_mark_buffer_dirty(leaf);
3594aa5d6bedSChris Mason 
35955f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
35965f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
3597be0e5c09SChris Mason 		BUG();
35985f39d397SChris Mason 	}
359944871b1bSChris Mason }
360044871b1bSChris Mason 
360144871b1bSChris Mason /*
360244871b1bSChris Mason  * Given a key and some data, insert items into the tree.
360344871b1bSChris Mason  * This does all the path init required, making room in the tree if needed.
360444871b1bSChris Mason  */
360544871b1bSChris Mason int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
360644871b1bSChris Mason 			    struct btrfs_root *root,
360744871b1bSChris Mason 			    struct btrfs_path *path,
360844871b1bSChris Mason 			    struct btrfs_key *cpu_key, u32 *data_size,
360944871b1bSChris Mason 			    int nr)
361044871b1bSChris Mason {
361144871b1bSChris Mason 	int ret = 0;
361244871b1bSChris Mason 	int slot;
361344871b1bSChris Mason 	int i;
361444871b1bSChris Mason 	u32 total_size = 0;
361544871b1bSChris Mason 	u32 total_data = 0;
361644871b1bSChris Mason 
361744871b1bSChris Mason 	for (i = 0; i < nr; i++)
361844871b1bSChris Mason 		total_data += data_size[i];
361944871b1bSChris Mason 
362044871b1bSChris Mason 	total_size = total_data + (nr * sizeof(struct btrfs_item));
362144871b1bSChris Mason 	ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
362244871b1bSChris Mason 	if (ret == 0)
362344871b1bSChris Mason 		return -EEXIST;
362444871b1bSChris Mason 	if (ret < 0)
3625143bede5SJeff Mahoney 		return ret;
362644871b1bSChris Mason 
362744871b1bSChris Mason 	slot = path->slots[0];
362844871b1bSChris Mason 	BUG_ON(slot < 0);
362944871b1bSChris Mason 
3630143bede5SJeff Mahoney 	setup_items_for_insert(trans, root, path, cpu_key, data_size,
363144871b1bSChris Mason 			       total_data, total_size, nr);
3632143bede5SJeff Mahoney 	return 0;
363362e2749eSChris Mason }
363462e2749eSChris Mason 
363562e2749eSChris Mason /*
363662e2749eSChris Mason  * Given a key and some data, insert an item into the tree.
363762e2749eSChris Mason  * This does all the path init required, making room in the tree if needed.
363862e2749eSChris Mason  */
3639e089f05cSChris Mason int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3640e089f05cSChris Mason 		      *root, struct btrfs_key *cpu_key, void *data, u32
3641e089f05cSChris Mason 		      data_size)
364262e2749eSChris Mason {
364362e2749eSChris Mason 	int ret = 0;
36442c90e5d6SChris Mason 	struct btrfs_path *path;
36455f39d397SChris Mason 	struct extent_buffer *leaf;
36465f39d397SChris Mason 	unsigned long ptr;
364762e2749eSChris Mason 
36482c90e5d6SChris Mason 	path = btrfs_alloc_path();
3649db5b493aSTsutomu Itoh 	if (!path)
3650db5b493aSTsutomu Itoh 		return -ENOMEM;
36512c90e5d6SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
365262e2749eSChris Mason 	if (!ret) {
36535f39d397SChris Mason 		leaf = path->nodes[0];
36545f39d397SChris Mason 		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
36555f39d397SChris Mason 		write_extent_buffer(leaf, data, ptr, data_size);
36565f39d397SChris Mason 		btrfs_mark_buffer_dirty(leaf);
365762e2749eSChris Mason 	}
36582c90e5d6SChris Mason 	btrfs_free_path(path);
3659aa5d6bedSChris Mason 	return ret;
3660be0e5c09SChris Mason }
3661be0e5c09SChris Mason 
366274123bd7SChris Mason /*
36635de08d7dSChris Mason  * delete the pointer from a given node.
366474123bd7SChris Mason  *
3665d352ac68SChris Mason  * the tree should have been previously balanced so the deletion does not
3666d352ac68SChris Mason  * empty a node.
366774123bd7SChris Mason  */
3668143bede5SJeff Mahoney static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3669e089f05cSChris Mason 		    struct btrfs_path *path, int level, int slot)
3670be0e5c09SChris Mason {
36715f39d397SChris Mason 	struct extent_buffer *parent = path->nodes[level];
36727518a238SChris Mason 	u32 nritems;
3673be0e5c09SChris Mason 
36745f39d397SChris Mason 	nritems = btrfs_header_nritems(parent);
3675be0e5c09SChris Mason 	if (slot != nritems - 1) {
36765f39d397SChris Mason 		memmove_extent_buffer(parent,
36775f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot),
36785f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot + 1),
3679d6025579SChris Mason 			      sizeof(struct btrfs_key_ptr) *
3680d6025579SChris Mason 			      (nritems - slot - 1));
3681be0e5c09SChris Mason 	}
36827518a238SChris Mason 	nritems--;
36835f39d397SChris Mason 	btrfs_set_header_nritems(parent, nritems);
36847518a238SChris Mason 	if (nritems == 0 && parent == root->node) {
36855f39d397SChris Mason 		BUG_ON(btrfs_header_level(root->node) != 1);
3686eb60ceacSChris Mason 		/* just turn the root into a leaf and break */
36875f39d397SChris Mason 		btrfs_set_header_level(root->node, 0);
3688bb803951SChris Mason 	} else if (slot == 0) {
36895f39d397SChris Mason 		struct btrfs_disk_key disk_key;
36905f39d397SChris Mason 
36915f39d397SChris Mason 		btrfs_node_key(parent, &disk_key, 0);
3692143bede5SJeff Mahoney 		fixup_low_keys(trans, root, path, &disk_key, level + 1);
3693be0e5c09SChris Mason 	}
3694d6025579SChris Mason 	btrfs_mark_buffer_dirty(parent);
3695be0e5c09SChris Mason }
3696be0e5c09SChris Mason 
369774123bd7SChris Mason /*
3698323ac95bSChris Mason  * a helper function to delete the leaf pointed to by path->slots[1] and
36995d4f98a2SYan Zheng  * path->nodes[1].
3700323ac95bSChris Mason  *
3701323ac95bSChris Mason  * This deletes the pointer in path->nodes[1] and frees the leaf
3702323ac95bSChris Mason  * block extent.  zero is returned if it all worked out, < 0 otherwise.
3703323ac95bSChris Mason  *
3704323ac95bSChris Mason  * The path must have already been setup for deleting the leaf, including
3705323ac95bSChris Mason  * all the proper balancing.  path->nodes[1] must be locked.
3706323ac95bSChris Mason  */
3707143bede5SJeff Mahoney static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
3708323ac95bSChris Mason 				    struct btrfs_root *root,
37095d4f98a2SYan Zheng 				    struct btrfs_path *path,
37105d4f98a2SYan Zheng 				    struct extent_buffer *leaf)
3711323ac95bSChris Mason {
37125d4f98a2SYan Zheng 	WARN_ON(btrfs_header_generation(leaf) != trans->transid);
3713143bede5SJeff Mahoney 	del_ptr(trans, root, path, 1, path->slots[1]);
3714323ac95bSChris Mason 
37154d081c41SChris Mason 	/*
37164d081c41SChris Mason 	 * btrfs_free_extent is expensive, we want to make sure we
37174d081c41SChris Mason 	 * aren't holding any locks when we call it
37184d081c41SChris Mason 	 */
37194d081c41SChris Mason 	btrfs_unlock_up_safe(path, 0);
37204d081c41SChris Mason 
3721f0486c68SYan, Zheng 	root_sub_used(root, leaf->len);
3722f0486c68SYan, Zheng 
372366d7e7f0SArne Jansen 	btrfs_free_tree_block(trans, root, leaf, 0, 1, 0);
3724323ac95bSChris Mason }
3725323ac95bSChris Mason /*
372674123bd7SChris Mason  * delete the item at the leaf level in path.  If that empties
372774123bd7SChris Mason  * the leaf, remove it from the tree
372874123bd7SChris Mason  */
372985e21bacSChris Mason int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
373085e21bacSChris Mason 		    struct btrfs_path *path, int slot, int nr)
3731be0e5c09SChris Mason {
37325f39d397SChris Mason 	struct extent_buffer *leaf;
37335f39d397SChris Mason 	struct btrfs_item *item;
373485e21bacSChris Mason 	int last_off;
373585e21bacSChris Mason 	int dsize = 0;
3736aa5d6bedSChris Mason 	int ret = 0;
3737aa5d6bedSChris Mason 	int wret;
373885e21bacSChris Mason 	int i;
37397518a238SChris Mason 	u32 nritems;
3740be0e5c09SChris Mason 
37415f39d397SChris Mason 	leaf = path->nodes[0];
374285e21bacSChris Mason 	last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
374385e21bacSChris Mason 
374485e21bacSChris Mason 	for (i = 0; i < nr; i++)
374585e21bacSChris Mason 		dsize += btrfs_item_size_nr(leaf, slot + i);
374685e21bacSChris Mason 
37475f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3748be0e5c09SChris Mason 
374985e21bacSChris Mason 	if (slot + nr != nritems) {
3750123abc88SChris Mason 		int data_end = leaf_data_end(root, leaf);
37515f39d397SChris Mason 
37525f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3753d6025579SChris Mason 			      data_end + dsize,
3754123abc88SChris Mason 			      btrfs_leaf_data(leaf) + data_end,
375585e21bacSChris Mason 			      last_off - data_end);
37565f39d397SChris Mason 
375785e21bacSChris Mason 		for (i = slot + nr; i < nritems; i++) {
37585f39d397SChris Mason 			u32 ioff;
3759db94535dSChris Mason 
37605f39d397SChris Mason 			item = btrfs_item_nr(leaf, i);
37615f39d397SChris Mason 			ioff = btrfs_item_offset(leaf, item);
37625f39d397SChris Mason 			btrfs_set_item_offset(leaf, item, ioff + dsize);
37630783fcfcSChris Mason 		}
3764db94535dSChris Mason 
37655f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
376685e21bacSChris Mason 			      btrfs_item_nr_offset(slot + nr),
37670783fcfcSChris Mason 			      sizeof(struct btrfs_item) *
376885e21bacSChris Mason 			      (nritems - slot - nr));
3769be0e5c09SChris Mason 	}
377085e21bacSChris Mason 	btrfs_set_header_nritems(leaf, nritems - nr);
377185e21bacSChris Mason 	nritems -= nr;
37725f39d397SChris Mason 
377374123bd7SChris Mason 	/* delete the leaf if we've emptied it */
37747518a238SChris Mason 	if (nritems == 0) {
37755f39d397SChris Mason 		if (leaf == root->node) {
37765f39d397SChris Mason 			btrfs_set_header_level(leaf, 0);
37779a8dd150SChris Mason 		} else {
3778f0486c68SYan, Zheng 			btrfs_set_path_blocking(path);
3779f0486c68SYan, Zheng 			clean_tree_block(trans, root, leaf);
3780143bede5SJeff Mahoney 			btrfs_del_leaf(trans, root, path, leaf);
37819a8dd150SChris Mason 		}
3782be0e5c09SChris Mason 	} else {
37837518a238SChris Mason 		int used = leaf_space_used(leaf, 0, nritems);
3784aa5d6bedSChris Mason 		if (slot == 0) {
37855f39d397SChris Mason 			struct btrfs_disk_key disk_key;
37865f39d397SChris Mason 
37875f39d397SChris Mason 			btrfs_item_key(leaf, &disk_key, 0);
3788143bede5SJeff Mahoney 			fixup_low_keys(trans, root, path, &disk_key, 1);
3789aa5d6bedSChris Mason 		}
3790aa5d6bedSChris Mason 
379174123bd7SChris Mason 		/* delete the leaf if it is mostly empty */
3792d717aa1dSYan Zheng 		if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
3793be0e5c09SChris Mason 			/* push_leaf_left fixes the path.
3794be0e5c09SChris Mason 			 * make sure the path still points to our leaf
3795be0e5c09SChris Mason 			 * for possible call to del_ptr below
3796be0e5c09SChris Mason 			 */
37974920c9acSChris Mason 			slot = path->slots[1];
37985f39d397SChris Mason 			extent_buffer_get(leaf);
37995f39d397SChris Mason 
3800b9473439SChris Mason 			btrfs_set_path_blocking(path);
380199d8f83cSChris Mason 			wret = push_leaf_left(trans, root, path, 1, 1,
380299d8f83cSChris Mason 					      1, (u32)-1);
380354aa1f4dSChris Mason 			if (wret < 0 && wret != -ENOSPC)
3804aa5d6bedSChris Mason 				ret = wret;
38055f39d397SChris Mason 
38065f39d397SChris Mason 			if (path->nodes[0] == leaf &&
38075f39d397SChris Mason 			    btrfs_header_nritems(leaf)) {
380899d8f83cSChris Mason 				wret = push_leaf_right(trans, root, path, 1,
380999d8f83cSChris Mason 						       1, 1, 0);
381054aa1f4dSChris Mason 				if (wret < 0 && wret != -ENOSPC)
3811aa5d6bedSChris Mason 					ret = wret;
3812aa5d6bedSChris Mason 			}
38135f39d397SChris Mason 
38145f39d397SChris Mason 			if (btrfs_header_nritems(leaf) == 0) {
3815323ac95bSChris Mason 				path->slots[1] = slot;
3816143bede5SJeff Mahoney 				btrfs_del_leaf(trans, root, path, leaf);
38175f39d397SChris Mason 				free_extent_buffer(leaf);
3818143bede5SJeff Mahoney 				ret = 0;
38195de08d7dSChris Mason 			} else {
3820925baeddSChris Mason 				/* if we're still in the path, make sure
3821925baeddSChris Mason 				 * we're dirty.  Otherwise, one of the
3822925baeddSChris Mason 				 * push_leaf functions must have already
3823925baeddSChris Mason 				 * dirtied this buffer
3824925baeddSChris Mason 				 */
3825925baeddSChris Mason 				if (path->nodes[0] == leaf)
38265f39d397SChris Mason 					btrfs_mark_buffer_dirty(leaf);
38275f39d397SChris Mason 				free_extent_buffer(leaf);
3828be0e5c09SChris Mason 			}
3829d5719762SChris Mason 		} else {
38305f39d397SChris Mason 			btrfs_mark_buffer_dirty(leaf);
3831be0e5c09SChris Mason 		}
38329a8dd150SChris Mason 	}
3833aa5d6bedSChris Mason 	return ret;
38349a8dd150SChris Mason }
38359a8dd150SChris Mason 
383697571fd0SChris Mason /*
3837925baeddSChris Mason  * search the tree again to find a leaf with lesser keys
38387bb86316SChris Mason  * returns 0 if it found something or 1 if there are no lesser leaves.
38397bb86316SChris Mason  * returns < 0 on io errors.
3840d352ac68SChris Mason  *
3841d352ac68SChris Mason  * This may release the path, and so you may lose any locks held at the
3842d352ac68SChris Mason  * time you call it.
38437bb86316SChris Mason  */
38447bb86316SChris Mason int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
38457bb86316SChris Mason {
3846925baeddSChris Mason 	struct btrfs_key key;
3847925baeddSChris Mason 	struct btrfs_disk_key found_key;
3848925baeddSChris Mason 	int ret;
38497bb86316SChris Mason 
3850925baeddSChris Mason 	btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
3851925baeddSChris Mason 
3852925baeddSChris Mason 	if (key.offset > 0)
3853925baeddSChris Mason 		key.offset--;
3854925baeddSChris Mason 	else if (key.type > 0)
3855925baeddSChris Mason 		key.type--;
3856925baeddSChris Mason 	else if (key.objectid > 0)
3857925baeddSChris Mason 		key.objectid--;
3858925baeddSChris Mason 	else
38597bb86316SChris Mason 		return 1;
38607bb86316SChris Mason 
3861b3b4aa74SDavid Sterba 	btrfs_release_path(path);
3862925baeddSChris Mason 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3863925baeddSChris Mason 	if (ret < 0)
3864925baeddSChris Mason 		return ret;
3865925baeddSChris Mason 	btrfs_item_key(path->nodes[0], &found_key, 0);
3866925baeddSChris Mason 	ret = comp_keys(&found_key, &key);
3867925baeddSChris Mason 	if (ret < 0)
38687bb86316SChris Mason 		return 0;
3869925baeddSChris Mason 	return 1;
38707bb86316SChris Mason }
38717bb86316SChris Mason 
38723f157a2fSChris Mason /*
38733f157a2fSChris Mason  * A helper function to walk down the tree starting at min_key, and looking
38743f157a2fSChris Mason  * for nodes or leaves that are either in cache or have a minimum
3875d352ac68SChris Mason  * transaction id.  This is used by the btree defrag code, and tree logging
38763f157a2fSChris Mason  *
38773f157a2fSChris Mason  * This does not cow, but it does stuff the starting key it finds back
38783f157a2fSChris Mason  * into min_key, so you can call btrfs_search_slot with cow=1 on the
38793f157a2fSChris Mason  * key and get a writable path.
38803f157a2fSChris Mason  *
38813f157a2fSChris Mason  * This does lock as it descends, and path->keep_locks should be set
38823f157a2fSChris Mason  * to 1 by the caller.
38833f157a2fSChris Mason  *
38843f157a2fSChris Mason  * This honors path->lowest_level to prevent descent past a given level
38853f157a2fSChris Mason  * of the tree.
38863f157a2fSChris Mason  *
3887d352ac68SChris Mason  * min_trans indicates the oldest transaction that you are interested
3888d352ac68SChris Mason  * in walking through.  Any nodes or leaves older than min_trans are
3889d352ac68SChris Mason  * skipped over (without reading them).
3890d352ac68SChris Mason  *
38913f157a2fSChris Mason  * returns zero if something useful was found, < 0 on error and 1 if there
38923f157a2fSChris Mason  * was nothing in the tree that matched the search criteria.
38933f157a2fSChris Mason  */
38943f157a2fSChris Mason int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
3895e02119d5SChris Mason 			 struct btrfs_key *max_key,
38963f157a2fSChris Mason 			 struct btrfs_path *path, int cache_only,
38973f157a2fSChris Mason 			 u64 min_trans)
38983f157a2fSChris Mason {
38993f157a2fSChris Mason 	struct extent_buffer *cur;
39003f157a2fSChris Mason 	struct btrfs_key found_key;
39013f157a2fSChris Mason 	int slot;
39029652480bSYan 	int sret;
39033f157a2fSChris Mason 	u32 nritems;
39043f157a2fSChris Mason 	int level;
39053f157a2fSChris Mason 	int ret = 1;
39063f157a2fSChris Mason 
3907934d375bSChris Mason 	WARN_ON(!path->keep_locks);
39083f157a2fSChris Mason again:
3909bd681513SChris Mason 	cur = btrfs_read_lock_root_node(root);
39103f157a2fSChris Mason 	level = btrfs_header_level(cur);
3911e02119d5SChris Mason 	WARN_ON(path->nodes[level]);
39123f157a2fSChris Mason 	path->nodes[level] = cur;
3913bd681513SChris Mason 	path->locks[level] = BTRFS_READ_LOCK;
39143f157a2fSChris Mason 
39153f157a2fSChris Mason 	if (btrfs_header_generation(cur) < min_trans) {
39163f157a2fSChris Mason 		ret = 1;
39173f157a2fSChris Mason 		goto out;
39183f157a2fSChris Mason 	}
39193f157a2fSChris Mason 	while (1) {
39203f157a2fSChris Mason 		nritems = btrfs_header_nritems(cur);
39213f157a2fSChris Mason 		level = btrfs_header_level(cur);
39229652480bSYan 		sret = bin_search(cur, min_key, level, &slot);
39233f157a2fSChris Mason 
3924323ac95bSChris Mason 		/* at the lowest level, we're done, setup the path and exit */
3925323ac95bSChris Mason 		if (level == path->lowest_level) {
3926e02119d5SChris Mason 			if (slot >= nritems)
3927e02119d5SChris Mason 				goto find_next_key;
39283f157a2fSChris Mason 			ret = 0;
39293f157a2fSChris Mason 			path->slots[level] = slot;
39303f157a2fSChris Mason 			btrfs_item_key_to_cpu(cur, &found_key, slot);
39313f157a2fSChris Mason 			goto out;
39323f157a2fSChris Mason 		}
39339652480bSYan 		if (sret && slot > 0)
39349652480bSYan 			slot--;
39353f157a2fSChris Mason 		/*
39363f157a2fSChris Mason 		 * check this node pointer against the cache_only and
39373f157a2fSChris Mason 		 * min_trans parameters.  If it isn't in cache or is too
39383f157a2fSChris Mason 		 * old, skip to the next one.
39393f157a2fSChris Mason 		 */
39403f157a2fSChris Mason 		while (slot < nritems) {
39413f157a2fSChris Mason 			u64 blockptr;
39423f157a2fSChris Mason 			u64 gen;
39433f157a2fSChris Mason 			struct extent_buffer *tmp;
3944e02119d5SChris Mason 			struct btrfs_disk_key disk_key;
3945e02119d5SChris Mason 
39463f157a2fSChris Mason 			blockptr = btrfs_node_blockptr(cur, slot);
39473f157a2fSChris Mason 			gen = btrfs_node_ptr_generation(cur, slot);
39483f157a2fSChris Mason 			if (gen < min_trans) {
39493f157a2fSChris Mason 				slot++;
39503f157a2fSChris Mason 				continue;
39513f157a2fSChris Mason 			}
39523f157a2fSChris Mason 			if (!cache_only)
39533f157a2fSChris Mason 				break;
39543f157a2fSChris Mason 
3955e02119d5SChris Mason 			if (max_key) {
3956e02119d5SChris Mason 				btrfs_node_key(cur, &disk_key, slot);
3957e02119d5SChris Mason 				if (comp_keys(&disk_key, max_key) >= 0) {
3958e02119d5SChris Mason 					ret = 1;
3959e02119d5SChris Mason 					goto out;
3960e02119d5SChris Mason 				}
3961e02119d5SChris Mason 			}
3962e02119d5SChris Mason 
39633f157a2fSChris Mason 			tmp = btrfs_find_tree_block(root, blockptr,
39643f157a2fSChris Mason 					    btrfs_level_size(root, level - 1));
39653f157a2fSChris Mason 
39663f157a2fSChris Mason 			if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
39673f157a2fSChris Mason 				free_extent_buffer(tmp);
39683f157a2fSChris Mason 				break;
39693f157a2fSChris Mason 			}
39703f157a2fSChris Mason 			if (tmp)
39713f157a2fSChris Mason 				free_extent_buffer(tmp);
39723f157a2fSChris Mason 			slot++;
39733f157a2fSChris Mason 		}
3974e02119d5SChris Mason find_next_key:
39753f157a2fSChris Mason 		/*
39763f157a2fSChris Mason 		 * we didn't find a candidate key in this node, walk forward
39773f157a2fSChris Mason 		 * and find another one
39783f157a2fSChris Mason 		 */
39793f157a2fSChris Mason 		if (slot >= nritems) {
3980e02119d5SChris Mason 			path->slots[level] = slot;
3981b4ce94deSChris Mason 			btrfs_set_path_blocking(path);
3982e02119d5SChris Mason 			sret = btrfs_find_next_key(root, path, min_key, level,
39833f157a2fSChris Mason 						  cache_only, min_trans);
3984e02119d5SChris Mason 			if (sret == 0) {
3985b3b4aa74SDavid Sterba 				btrfs_release_path(path);
39863f157a2fSChris Mason 				goto again;
39873f157a2fSChris Mason 			} else {
39883f157a2fSChris Mason 				goto out;
39893f157a2fSChris Mason 			}
39903f157a2fSChris Mason 		}
39913f157a2fSChris Mason 		/* save our key for returning back */
39923f157a2fSChris Mason 		btrfs_node_key_to_cpu(cur, &found_key, slot);
39933f157a2fSChris Mason 		path->slots[level] = slot;
39943f157a2fSChris Mason 		if (level == path->lowest_level) {
39953f157a2fSChris Mason 			ret = 0;
39963f157a2fSChris Mason 			unlock_up(path, level, 1);
39973f157a2fSChris Mason 			goto out;
39983f157a2fSChris Mason 		}
3999b4ce94deSChris Mason 		btrfs_set_path_blocking(path);
40003f157a2fSChris Mason 		cur = read_node_slot(root, cur, slot);
400197d9a8a4STsutomu Itoh 		BUG_ON(!cur);
40023f157a2fSChris Mason 
4003bd681513SChris Mason 		btrfs_tree_read_lock(cur);
4004b4ce94deSChris Mason 
4005bd681513SChris Mason 		path->locks[level - 1] = BTRFS_READ_LOCK;
40063f157a2fSChris Mason 		path->nodes[level - 1] = cur;
40073f157a2fSChris Mason 		unlock_up(path, level, 1);
4008bd681513SChris Mason 		btrfs_clear_path_blocking(path, NULL, 0);
40093f157a2fSChris Mason 	}
40103f157a2fSChris Mason out:
40113f157a2fSChris Mason 	if (ret == 0)
40123f157a2fSChris Mason 		memcpy(min_key, &found_key, sizeof(found_key));
4013b4ce94deSChris Mason 	btrfs_set_path_blocking(path);
40143f157a2fSChris Mason 	return ret;
40153f157a2fSChris Mason }
40163f157a2fSChris Mason 
40173f157a2fSChris Mason /*
40183f157a2fSChris Mason  * this is similar to btrfs_next_leaf, but does not try to preserve
40193f157a2fSChris Mason  * and fixup the path.  It looks for and returns the next key in the
40203f157a2fSChris Mason  * tree based on the current path and the cache_only and min_trans
40213f157a2fSChris Mason  * parameters.
40223f157a2fSChris Mason  *
40233f157a2fSChris Mason  * 0 is returned if another key is found, < 0 if there are any errors
40243f157a2fSChris Mason  * and 1 is returned if there are no higher keys in the tree
40253f157a2fSChris Mason  *
40263f157a2fSChris Mason  * path->keep_locks should be set to 1 on the search made before
40273f157a2fSChris Mason  * calling this function.
40283f157a2fSChris Mason  */
4029e7a84565SChris Mason int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
403033c66f43SYan Zheng 			struct btrfs_key *key, int level,
40313f157a2fSChris Mason 			int cache_only, u64 min_trans)
4032e7a84565SChris Mason {
4033e7a84565SChris Mason 	int slot;
4034e7a84565SChris Mason 	struct extent_buffer *c;
4035e7a84565SChris Mason 
4036934d375bSChris Mason 	WARN_ON(!path->keep_locks);
4037e7a84565SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
4038e7a84565SChris Mason 		if (!path->nodes[level])
4039e7a84565SChris Mason 			return 1;
4040e7a84565SChris Mason 
4041e7a84565SChris Mason 		slot = path->slots[level] + 1;
4042e7a84565SChris Mason 		c = path->nodes[level];
40433f157a2fSChris Mason next:
4044e7a84565SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
404533c66f43SYan Zheng 			int ret;
404633c66f43SYan Zheng 			int orig_lowest;
404733c66f43SYan Zheng 			struct btrfs_key cur_key;
404833c66f43SYan Zheng 			if (level + 1 >= BTRFS_MAX_LEVEL ||
404933c66f43SYan Zheng 			    !path->nodes[level + 1])
4050e7a84565SChris Mason 				return 1;
405133c66f43SYan Zheng 
405233c66f43SYan Zheng 			if (path->locks[level + 1]) {
405333c66f43SYan Zheng 				level++;
4054e7a84565SChris Mason 				continue;
4055e7a84565SChris Mason 			}
405633c66f43SYan Zheng 
405733c66f43SYan Zheng 			slot = btrfs_header_nritems(c) - 1;
405833c66f43SYan Zheng 			if (level == 0)
405933c66f43SYan Zheng 				btrfs_item_key_to_cpu(c, &cur_key, slot);
406033c66f43SYan Zheng 			else
406133c66f43SYan Zheng 				btrfs_node_key_to_cpu(c, &cur_key, slot);
406233c66f43SYan Zheng 
406333c66f43SYan Zheng 			orig_lowest = path->lowest_level;
4064b3b4aa74SDavid Sterba 			btrfs_release_path(path);
406533c66f43SYan Zheng 			path->lowest_level = level;
406633c66f43SYan Zheng 			ret = btrfs_search_slot(NULL, root, &cur_key, path,
406733c66f43SYan Zheng 						0, 0);
406833c66f43SYan Zheng 			path->lowest_level = orig_lowest;
406933c66f43SYan Zheng 			if (ret < 0)
407033c66f43SYan Zheng 				return ret;
407133c66f43SYan Zheng 
407233c66f43SYan Zheng 			c = path->nodes[level];
407333c66f43SYan Zheng 			slot = path->slots[level];
407433c66f43SYan Zheng 			if (ret == 0)
407533c66f43SYan Zheng 				slot++;
407633c66f43SYan Zheng 			goto next;
407733c66f43SYan Zheng 		}
407833c66f43SYan Zheng 
4079e7a84565SChris Mason 		if (level == 0)
4080e7a84565SChris Mason 			btrfs_item_key_to_cpu(c, key, slot);
40813f157a2fSChris Mason 		else {
40823f157a2fSChris Mason 			u64 blockptr = btrfs_node_blockptr(c, slot);
40833f157a2fSChris Mason 			u64 gen = btrfs_node_ptr_generation(c, slot);
40843f157a2fSChris Mason 
40853f157a2fSChris Mason 			if (cache_only) {
40863f157a2fSChris Mason 				struct extent_buffer *cur;
40873f157a2fSChris Mason 				cur = btrfs_find_tree_block(root, blockptr,
40883f157a2fSChris Mason 					    btrfs_level_size(root, level - 1));
40893f157a2fSChris Mason 				if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
40903f157a2fSChris Mason 					slot++;
40913f157a2fSChris Mason 					if (cur)
40923f157a2fSChris Mason 						free_extent_buffer(cur);
40933f157a2fSChris Mason 					goto next;
40943f157a2fSChris Mason 				}
40953f157a2fSChris Mason 				free_extent_buffer(cur);
40963f157a2fSChris Mason 			}
40973f157a2fSChris Mason 			if (gen < min_trans) {
40983f157a2fSChris Mason 				slot++;
40993f157a2fSChris Mason 				goto next;
41003f157a2fSChris Mason 			}
4101e7a84565SChris Mason 			btrfs_node_key_to_cpu(c, key, slot);
41023f157a2fSChris Mason 		}
4103e7a84565SChris Mason 		return 0;
4104e7a84565SChris Mason 	}
4105e7a84565SChris Mason 	return 1;
4106e7a84565SChris Mason }
4107e7a84565SChris Mason 
41087bb86316SChris Mason /*
4109925baeddSChris Mason  * search the tree again to find a leaf with greater keys
41100f70abe2SChris Mason  * returns 0 if it found something or 1 if there are no greater leaves.
41110f70abe2SChris Mason  * returns < 0 on io errors.
411297571fd0SChris Mason  */
4113234b63a0SChris Mason int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
4114d97e63b6SChris Mason {
4115d97e63b6SChris Mason 	int slot;
41168e73f275SChris Mason 	int level;
41175f39d397SChris Mason 	struct extent_buffer *c;
41188e73f275SChris Mason 	struct extent_buffer *next;
4119925baeddSChris Mason 	struct btrfs_key key;
4120925baeddSChris Mason 	u32 nritems;
4121925baeddSChris Mason 	int ret;
41228e73f275SChris Mason 	int old_spinning = path->leave_spinning;
4123bd681513SChris Mason 	int next_rw_lock = 0;
4124925baeddSChris Mason 
4125925baeddSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
4126d397712bSChris Mason 	if (nritems == 0)
4127925baeddSChris Mason 		return 1;
4128925baeddSChris Mason 
41298e73f275SChris Mason 	btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
41308e73f275SChris Mason again:
41318e73f275SChris Mason 	level = 1;
41328e73f275SChris Mason 	next = NULL;
4133bd681513SChris Mason 	next_rw_lock = 0;
4134b3b4aa74SDavid Sterba 	btrfs_release_path(path);
41358e73f275SChris Mason 
4136a2135011SChris Mason 	path->keep_locks = 1;
41378e73f275SChris Mason 	path->leave_spinning = 1;
41388e73f275SChris Mason 
4139925baeddSChris Mason 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4140925baeddSChris Mason 	path->keep_locks = 0;
4141925baeddSChris Mason 
4142925baeddSChris Mason 	if (ret < 0)
4143925baeddSChris Mason 		return ret;
4144925baeddSChris Mason 
4145a2135011SChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
4146168fd7d2SChris Mason 	/*
4147168fd7d2SChris Mason 	 * by releasing the path above we dropped all our locks.  A balance
4148168fd7d2SChris Mason 	 * could have added more items next to the key that used to be
4149168fd7d2SChris Mason 	 * at the very end of the block.  So, check again here and
4150168fd7d2SChris Mason 	 * advance the path if there are now more items available.
4151168fd7d2SChris Mason 	 */
4152a2135011SChris Mason 	if (nritems > 0 && path->slots[0] < nritems - 1) {
4153e457afecSYan Zheng 		if (ret == 0)
4154168fd7d2SChris Mason 			path->slots[0]++;
41558e73f275SChris Mason 		ret = 0;
4156925baeddSChris Mason 		goto done;
4157925baeddSChris Mason 	}
4158d97e63b6SChris Mason 
4159234b63a0SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
41608e73f275SChris Mason 		if (!path->nodes[level]) {
41618e73f275SChris Mason 			ret = 1;
41628e73f275SChris Mason 			goto done;
41638e73f275SChris Mason 		}
41645f39d397SChris Mason 
4165d97e63b6SChris Mason 		slot = path->slots[level] + 1;
4166d97e63b6SChris Mason 		c = path->nodes[level];
41675f39d397SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
4168d97e63b6SChris Mason 			level++;
41698e73f275SChris Mason 			if (level == BTRFS_MAX_LEVEL) {
41708e73f275SChris Mason 				ret = 1;
41718e73f275SChris Mason 				goto done;
41728e73f275SChris Mason 			}
4173d97e63b6SChris Mason 			continue;
4174d97e63b6SChris Mason 		}
41755f39d397SChris Mason 
4176925baeddSChris Mason 		if (next) {
4177bd681513SChris Mason 			btrfs_tree_unlock_rw(next, next_rw_lock);
41785f39d397SChris Mason 			free_extent_buffer(next);
4179925baeddSChris Mason 		}
41805f39d397SChris Mason 
41818e73f275SChris Mason 		next = c;
4182bd681513SChris Mason 		next_rw_lock = path->locks[level];
41838e73f275SChris Mason 		ret = read_block_for_search(NULL, root, path, &next, level,
41848e73f275SChris Mason 					    slot, &key);
41858e73f275SChris Mason 		if (ret == -EAGAIN)
41868e73f275SChris Mason 			goto again;
41875f39d397SChris Mason 
418876a05b35SChris Mason 		if (ret < 0) {
4189b3b4aa74SDavid Sterba 			btrfs_release_path(path);
419076a05b35SChris Mason 			goto done;
419176a05b35SChris Mason 		}
419276a05b35SChris Mason 
41935cd57b2cSChris Mason 		if (!path->skip_locking) {
4194bd681513SChris Mason 			ret = btrfs_try_tree_read_lock(next);
41958e73f275SChris Mason 			if (!ret) {
41968e73f275SChris Mason 				btrfs_set_path_blocking(path);
4197bd681513SChris Mason 				btrfs_tree_read_lock(next);
4198bd681513SChris Mason 				btrfs_clear_path_blocking(path, next,
4199bd681513SChris Mason 							  BTRFS_READ_LOCK);
42008e73f275SChris Mason 			}
4201bd681513SChris Mason 			next_rw_lock = BTRFS_READ_LOCK;
4202bd681513SChris Mason 		}
4203d97e63b6SChris Mason 		break;
4204d97e63b6SChris Mason 	}
4205d97e63b6SChris Mason 	path->slots[level] = slot;
4206d97e63b6SChris Mason 	while (1) {
4207d97e63b6SChris Mason 		level--;
4208d97e63b6SChris Mason 		c = path->nodes[level];
4209925baeddSChris Mason 		if (path->locks[level])
4210bd681513SChris Mason 			btrfs_tree_unlock_rw(c, path->locks[level]);
42118e73f275SChris Mason 
42125f39d397SChris Mason 		free_extent_buffer(c);
4213d97e63b6SChris Mason 		path->nodes[level] = next;
4214d97e63b6SChris Mason 		path->slots[level] = 0;
4215a74a4b97SChris Mason 		if (!path->skip_locking)
4216bd681513SChris Mason 			path->locks[level] = next_rw_lock;
4217d97e63b6SChris Mason 		if (!level)
4218d97e63b6SChris Mason 			break;
4219b4ce94deSChris Mason 
42208e73f275SChris Mason 		ret = read_block_for_search(NULL, root, path, &next, level,
42218e73f275SChris Mason 					    0, &key);
42228e73f275SChris Mason 		if (ret == -EAGAIN)
42238e73f275SChris Mason 			goto again;
42248e73f275SChris Mason 
422576a05b35SChris Mason 		if (ret < 0) {
4226b3b4aa74SDavid Sterba 			btrfs_release_path(path);
422776a05b35SChris Mason 			goto done;
422876a05b35SChris Mason 		}
422976a05b35SChris Mason 
42305cd57b2cSChris Mason 		if (!path->skip_locking) {
4231bd681513SChris Mason 			ret = btrfs_try_tree_read_lock(next);
42328e73f275SChris Mason 			if (!ret) {
42338e73f275SChris Mason 				btrfs_set_path_blocking(path);
4234bd681513SChris Mason 				btrfs_tree_read_lock(next);
4235bd681513SChris Mason 				btrfs_clear_path_blocking(path, next,
4236bd681513SChris Mason 							  BTRFS_READ_LOCK);
42378e73f275SChris Mason 			}
4238bd681513SChris Mason 			next_rw_lock = BTRFS_READ_LOCK;
4239bd681513SChris Mason 		}
4240d97e63b6SChris Mason 	}
42418e73f275SChris Mason 	ret = 0;
4242925baeddSChris Mason done:
4243925baeddSChris Mason 	unlock_up(path, 0, 1);
42448e73f275SChris Mason 	path->leave_spinning = old_spinning;
42458e73f275SChris Mason 	if (!old_spinning)
42468e73f275SChris Mason 		btrfs_set_path_blocking(path);
42478e73f275SChris Mason 
42488e73f275SChris Mason 	return ret;
4249d97e63b6SChris Mason }
42500b86a832SChris Mason 
42513f157a2fSChris Mason /*
42523f157a2fSChris Mason  * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
42533f157a2fSChris Mason  * searching until it gets past min_objectid or finds an item of 'type'
42543f157a2fSChris Mason  *
42553f157a2fSChris Mason  * returns 0 if something is found, 1 if nothing was found and < 0 on error
42563f157a2fSChris Mason  */
42570b86a832SChris Mason int btrfs_previous_item(struct btrfs_root *root,
42580b86a832SChris Mason 			struct btrfs_path *path, u64 min_objectid,
42590b86a832SChris Mason 			int type)
42600b86a832SChris Mason {
42610b86a832SChris Mason 	struct btrfs_key found_key;
42620b86a832SChris Mason 	struct extent_buffer *leaf;
4263e02119d5SChris Mason 	u32 nritems;
42640b86a832SChris Mason 	int ret;
42650b86a832SChris Mason 
42660b86a832SChris Mason 	while (1) {
42670b86a832SChris Mason 		if (path->slots[0] == 0) {
4268b4ce94deSChris Mason 			btrfs_set_path_blocking(path);
42690b86a832SChris Mason 			ret = btrfs_prev_leaf(root, path);
42700b86a832SChris Mason 			if (ret != 0)
42710b86a832SChris Mason 				return ret;
42720b86a832SChris Mason 		} else {
42730b86a832SChris Mason 			path->slots[0]--;
42740b86a832SChris Mason 		}
42750b86a832SChris Mason 		leaf = path->nodes[0];
4276e02119d5SChris Mason 		nritems = btrfs_header_nritems(leaf);
4277e02119d5SChris Mason 		if (nritems == 0)
4278e02119d5SChris Mason 			return 1;
4279e02119d5SChris Mason 		if (path->slots[0] == nritems)
4280e02119d5SChris Mason 			path->slots[0]--;
4281e02119d5SChris Mason 
42820b86a832SChris Mason 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4283e02119d5SChris Mason 		if (found_key.objectid < min_objectid)
4284e02119d5SChris Mason 			break;
42850a4eefbbSYan Zheng 		if (found_key.type == type)
42860a4eefbbSYan Zheng 			return 0;
4287e02119d5SChris Mason 		if (found_key.objectid == min_objectid &&
4288e02119d5SChris Mason 		    found_key.type < type)
4289e02119d5SChris Mason 			break;
42900b86a832SChris Mason 	}
42910b86a832SChris Mason 	return 1;
42920b86a832SChris Mason }
4293