xref: /openbmc/linux/fs/btrfs/ctree.c (revision 3083ee2e18b701122a3b841db83448543a87a583)
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);
39e089f05cSChris Mason static int 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 
159*3083ee2eSJosef Bacik 	while (1) {
160240f62c8SChris Mason 		rcu_read_lock();
161240f62c8SChris Mason 		eb = rcu_dereference(root->node);
162*3083ee2eSJosef Bacik 
163*3083ee2eSJosef Bacik 		/*
164*3083ee2eSJosef Bacik 		 * RCU really hurts here, we could free up the root node because
165*3083ee2eSJosef Bacik 		 * it was cow'ed but we may not get the new root node yet so do
166*3083ee2eSJosef Bacik 		 * the inc_not_zero dance and if it doesn't work then
167*3083ee2eSJosef Bacik 		 * synchronize_rcu and try again.
168*3083ee2eSJosef Bacik 		 */
169*3083ee2eSJosef Bacik 		if (atomic_inc_not_zero(&eb->refs)) {
170240f62c8SChris Mason 			rcu_read_unlock();
171*3083ee2eSJosef Bacik 			break;
172*3083ee2eSJosef Bacik 		}
173*3083ee2eSJosef Bacik 		rcu_read_unlock();
174*3083ee2eSJosef Bacik 		synchronize_rcu();
175*3083ee2eSJosef Bacik 	}
176925baeddSChris Mason 	return eb;
177925baeddSChris Mason }
178925baeddSChris Mason 
179d352ac68SChris Mason /* loop around taking references on and locking the root node of the
180d352ac68SChris Mason  * tree until you end up with a lock on the root.  A locked buffer
181d352ac68SChris Mason  * is returned, with a reference held.
182d352ac68SChris Mason  */
183925baeddSChris Mason struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
184925baeddSChris Mason {
185925baeddSChris Mason 	struct extent_buffer *eb;
186925baeddSChris Mason 
187925baeddSChris Mason 	while (1) {
188925baeddSChris Mason 		eb = btrfs_root_node(root);
189925baeddSChris Mason 		btrfs_tree_lock(eb);
190240f62c8SChris Mason 		if (eb == root->node)
191925baeddSChris Mason 			break;
192925baeddSChris Mason 		btrfs_tree_unlock(eb);
193925baeddSChris Mason 		free_extent_buffer(eb);
194925baeddSChris Mason 	}
195925baeddSChris Mason 	return eb;
196925baeddSChris Mason }
197925baeddSChris Mason 
198bd681513SChris Mason /* loop around taking references on and locking the root node of the
199bd681513SChris Mason  * tree until you end up with a lock on the root.  A locked buffer
200bd681513SChris Mason  * is returned, with a reference held.
201bd681513SChris Mason  */
202bd681513SChris Mason struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
203bd681513SChris Mason {
204bd681513SChris Mason 	struct extent_buffer *eb;
205bd681513SChris Mason 
206bd681513SChris Mason 	while (1) {
207bd681513SChris Mason 		eb = btrfs_root_node(root);
208bd681513SChris Mason 		btrfs_tree_read_lock(eb);
209bd681513SChris Mason 		if (eb == root->node)
210bd681513SChris Mason 			break;
211bd681513SChris Mason 		btrfs_tree_read_unlock(eb);
212bd681513SChris Mason 		free_extent_buffer(eb);
213bd681513SChris Mason 	}
214bd681513SChris Mason 	return eb;
215bd681513SChris Mason }
216bd681513SChris Mason 
217d352ac68SChris Mason /* cowonly root (everything not a reference counted cow subvolume), just get
218d352ac68SChris Mason  * put onto a simple dirty list.  transaction.c walks this to make sure they
219d352ac68SChris Mason  * get properly updated on disk.
220d352ac68SChris Mason  */
2210b86a832SChris Mason static void add_root_to_dirty_list(struct btrfs_root *root)
2220b86a832SChris Mason {
2230b86a832SChris Mason 	if (root->track_dirty && list_empty(&root->dirty_list)) {
2240b86a832SChris Mason 		list_add(&root->dirty_list,
2250b86a832SChris Mason 			 &root->fs_info->dirty_cowonly_roots);
2260b86a832SChris Mason 	}
2270b86a832SChris Mason }
2280b86a832SChris Mason 
229d352ac68SChris Mason /*
230d352ac68SChris Mason  * used by snapshot creation to make a copy of a root for a tree with
231d352ac68SChris Mason  * a given objectid.  The buffer with the new root node is returned in
232d352ac68SChris Mason  * cow_ret, and this func returns zero on success or a negative error code.
233d352ac68SChris Mason  */
234be20aa9dSChris Mason int btrfs_copy_root(struct btrfs_trans_handle *trans,
235be20aa9dSChris Mason 		      struct btrfs_root *root,
236be20aa9dSChris Mason 		      struct extent_buffer *buf,
237be20aa9dSChris Mason 		      struct extent_buffer **cow_ret, u64 new_root_objectid)
238be20aa9dSChris Mason {
239be20aa9dSChris Mason 	struct extent_buffer *cow;
240be20aa9dSChris Mason 	int ret = 0;
241be20aa9dSChris Mason 	int level;
2425d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
243be20aa9dSChris Mason 
244be20aa9dSChris Mason 	WARN_ON(root->ref_cows && trans->transid !=
245be20aa9dSChris Mason 		root->fs_info->running_transaction->transid);
246be20aa9dSChris Mason 	WARN_ON(root->ref_cows && trans->transid != root->last_trans);
247be20aa9dSChris Mason 
248be20aa9dSChris Mason 	level = btrfs_header_level(buf);
2495d4f98a2SYan Zheng 	if (level == 0)
2505d4f98a2SYan Zheng 		btrfs_item_key(buf, &disk_key, 0);
2515d4f98a2SYan Zheng 	else
2525d4f98a2SYan Zheng 		btrfs_node_key(buf, &disk_key, 0);
25331840ae1SZheng Yan 
2545d4f98a2SYan Zheng 	cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
2555d4f98a2SYan Zheng 				     new_root_objectid, &disk_key, level,
25666d7e7f0SArne Jansen 				     buf->start, 0, 1);
2575d4f98a2SYan Zheng 	if (IS_ERR(cow))
258be20aa9dSChris Mason 		return PTR_ERR(cow);
259be20aa9dSChris Mason 
260be20aa9dSChris Mason 	copy_extent_buffer(cow, buf, 0, 0, cow->len);
261be20aa9dSChris Mason 	btrfs_set_header_bytenr(cow, cow->start);
262be20aa9dSChris Mason 	btrfs_set_header_generation(cow, trans->transid);
2635d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
2645d4f98a2SYan Zheng 	btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
2655d4f98a2SYan Zheng 				     BTRFS_HEADER_FLAG_RELOC);
2665d4f98a2SYan Zheng 	if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
2675d4f98a2SYan Zheng 		btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
2685d4f98a2SYan Zheng 	else
269be20aa9dSChris Mason 		btrfs_set_header_owner(cow, new_root_objectid);
270be20aa9dSChris Mason 
2712b82032cSYan Zheng 	write_extent_buffer(cow, root->fs_info->fsid,
2722b82032cSYan Zheng 			    (unsigned long)btrfs_header_fsid(cow),
2732b82032cSYan Zheng 			    BTRFS_FSID_SIZE);
2742b82032cSYan Zheng 
275be20aa9dSChris Mason 	WARN_ON(btrfs_header_generation(buf) > trans->transid);
2765d4f98a2SYan Zheng 	if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
27766d7e7f0SArne Jansen 		ret = btrfs_inc_ref(trans, root, cow, 1, 1);
2785d4f98a2SYan Zheng 	else
27966d7e7f0SArne Jansen 		ret = btrfs_inc_ref(trans, root, cow, 0, 1);
2804aec2b52SChris Mason 
281be20aa9dSChris Mason 	if (ret)
282be20aa9dSChris Mason 		return ret;
283be20aa9dSChris Mason 
284be20aa9dSChris Mason 	btrfs_mark_buffer_dirty(cow);
285be20aa9dSChris Mason 	*cow_ret = cow;
286be20aa9dSChris Mason 	return 0;
287be20aa9dSChris Mason }
288be20aa9dSChris Mason 
289d352ac68SChris Mason /*
2905d4f98a2SYan Zheng  * check if the tree block can be shared by multiple trees
2915d4f98a2SYan Zheng  */
2925d4f98a2SYan Zheng int btrfs_block_can_be_shared(struct btrfs_root *root,
2935d4f98a2SYan Zheng 			      struct extent_buffer *buf)
2945d4f98a2SYan Zheng {
2955d4f98a2SYan Zheng 	/*
2965d4f98a2SYan Zheng 	 * Tree blocks not in refernece counted trees and tree roots
2975d4f98a2SYan Zheng 	 * are never shared. If a block was allocated after the last
2985d4f98a2SYan Zheng 	 * snapshot and the block was not allocated by tree relocation,
2995d4f98a2SYan Zheng 	 * we know the block is not shared.
3005d4f98a2SYan Zheng 	 */
3015d4f98a2SYan Zheng 	if (root->ref_cows &&
3025d4f98a2SYan Zheng 	    buf != root->node && buf != root->commit_root &&
3035d4f98a2SYan Zheng 	    (btrfs_header_generation(buf) <=
3045d4f98a2SYan Zheng 	     btrfs_root_last_snapshot(&root->root_item) ||
3055d4f98a2SYan Zheng 	     btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
3065d4f98a2SYan Zheng 		return 1;
3075d4f98a2SYan Zheng #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3085d4f98a2SYan Zheng 	if (root->ref_cows &&
3095d4f98a2SYan Zheng 	    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
3105d4f98a2SYan Zheng 		return 1;
3115d4f98a2SYan Zheng #endif
3125d4f98a2SYan Zheng 	return 0;
3135d4f98a2SYan Zheng }
3145d4f98a2SYan Zheng 
3155d4f98a2SYan Zheng static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
3165d4f98a2SYan Zheng 				       struct btrfs_root *root,
3175d4f98a2SYan Zheng 				       struct extent_buffer *buf,
318f0486c68SYan, Zheng 				       struct extent_buffer *cow,
319f0486c68SYan, Zheng 				       int *last_ref)
3205d4f98a2SYan Zheng {
3215d4f98a2SYan Zheng 	u64 refs;
3225d4f98a2SYan Zheng 	u64 owner;
3235d4f98a2SYan Zheng 	u64 flags;
3245d4f98a2SYan Zheng 	u64 new_flags = 0;
3255d4f98a2SYan Zheng 	int ret;
3265d4f98a2SYan Zheng 
3275d4f98a2SYan Zheng 	/*
3285d4f98a2SYan Zheng 	 * Backrefs update rules:
3295d4f98a2SYan Zheng 	 *
3305d4f98a2SYan Zheng 	 * Always use full backrefs for extent pointers in tree block
3315d4f98a2SYan Zheng 	 * allocated by tree relocation.
3325d4f98a2SYan Zheng 	 *
3335d4f98a2SYan Zheng 	 * If a shared tree block is no longer referenced by its owner
3345d4f98a2SYan Zheng 	 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
3355d4f98a2SYan Zheng 	 * use full backrefs for extent pointers in tree block.
3365d4f98a2SYan Zheng 	 *
3375d4f98a2SYan Zheng 	 * If a tree block is been relocating
3385d4f98a2SYan Zheng 	 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
3395d4f98a2SYan Zheng 	 * use full backrefs for extent pointers in tree block.
3405d4f98a2SYan Zheng 	 * The reason for this is some operations (such as drop tree)
3415d4f98a2SYan Zheng 	 * are only allowed for blocks use full backrefs.
3425d4f98a2SYan Zheng 	 */
3435d4f98a2SYan Zheng 
3445d4f98a2SYan Zheng 	if (btrfs_block_can_be_shared(root, buf)) {
3455d4f98a2SYan Zheng 		ret = btrfs_lookup_extent_info(trans, root, buf->start,
3465d4f98a2SYan Zheng 					       buf->len, &refs, &flags);
3475d4f98a2SYan Zheng 		BUG_ON(ret);
3485d4f98a2SYan Zheng 		BUG_ON(refs == 0);
3495d4f98a2SYan Zheng 	} else {
3505d4f98a2SYan Zheng 		refs = 1;
3515d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
3525d4f98a2SYan Zheng 		    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
3535d4f98a2SYan Zheng 			flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
3545d4f98a2SYan Zheng 		else
3555d4f98a2SYan Zheng 			flags = 0;
3565d4f98a2SYan Zheng 	}
3575d4f98a2SYan Zheng 
3585d4f98a2SYan Zheng 	owner = btrfs_header_owner(buf);
3595d4f98a2SYan Zheng 	BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
3605d4f98a2SYan Zheng 	       !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
3615d4f98a2SYan Zheng 
3625d4f98a2SYan Zheng 	if (refs > 1) {
3635d4f98a2SYan Zheng 		if ((owner == root->root_key.objectid ||
3645d4f98a2SYan Zheng 		     root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
3655d4f98a2SYan Zheng 		    !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
36666d7e7f0SArne Jansen 			ret = btrfs_inc_ref(trans, root, buf, 1, 1);
3675d4f98a2SYan Zheng 			BUG_ON(ret);
3685d4f98a2SYan Zheng 
3695d4f98a2SYan Zheng 			if (root->root_key.objectid ==
3705d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID) {
37166d7e7f0SArne Jansen 				ret = btrfs_dec_ref(trans, root, buf, 0, 1);
3725d4f98a2SYan Zheng 				BUG_ON(ret);
37366d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 1, 1);
3745d4f98a2SYan Zheng 				BUG_ON(ret);
3755d4f98a2SYan Zheng 			}
3765d4f98a2SYan Zheng 			new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
3775d4f98a2SYan Zheng 		} else {
3785d4f98a2SYan Zheng 
3795d4f98a2SYan Zheng 			if (root->root_key.objectid ==
3805d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID)
38166d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 1, 1);
3825d4f98a2SYan Zheng 			else
38366d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 0, 1);
3845d4f98a2SYan Zheng 			BUG_ON(ret);
3855d4f98a2SYan Zheng 		}
3865d4f98a2SYan Zheng 		if (new_flags != 0) {
3875d4f98a2SYan Zheng 			ret = btrfs_set_disk_extent_flags(trans, root,
3885d4f98a2SYan Zheng 							  buf->start,
3895d4f98a2SYan Zheng 							  buf->len,
3905d4f98a2SYan Zheng 							  new_flags, 0);
3915d4f98a2SYan Zheng 			BUG_ON(ret);
3925d4f98a2SYan Zheng 		}
3935d4f98a2SYan Zheng 	} else {
3945d4f98a2SYan Zheng 		if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
3955d4f98a2SYan Zheng 			if (root->root_key.objectid ==
3965d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID)
39766d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 1, 1);
3985d4f98a2SYan Zheng 			else
39966d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 0, 1);
4005d4f98a2SYan Zheng 			BUG_ON(ret);
40166d7e7f0SArne Jansen 			ret = btrfs_dec_ref(trans, root, buf, 1, 1);
4025d4f98a2SYan Zheng 			BUG_ON(ret);
4035d4f98a2SYan Zheng 		}
4045d4f98a2SYan Zheng 		clean_tree_block(trans, root, buf);
405f0486c68SYan, Zheng 		*last_ref = 1;
4065d4f98a2SYan Zheng 	}
4075d4f98a2SYan Zheng 	return 0;
4085d4f98a2SYan Zheng }
4095d4f98a2SYan Zheng 
4105d4f98a2SYan Zheng /*
411d397712bSChris Mason  * does the dirty work in cow of a single block.  The parent block (if
412d397712bSChris Mason  * supplied) is updated to point to the new cow copy.  The new buffer is marked
413d397712bSChris Mason  * dirty and returned locked.  If you modify the block it needs to be marked
414d397712bSChris Mason  * dirty again.
415d352ac68SChris Mason  *
416d352ac68SChris Mason  * search_start -- an allocation hint for the new block
417d352ac68SChris Mason  *
418d397712bSChris Mason  * empty_size -- a hint that you plan on doing more cow.  This is the size in
419d397712bSChris Mason  * bytes the allocator should try to find free next to the block it returns.
420d397712bSChris Mason  * This is just a hint and may be ignored by the allocator.
421d352ac68SChris Mason  */
422d397712bSChris Mason static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
4235f39d397SChris Mason 			     struct btrfs_root *root,
4245f39d397SChris Mason 			     struct extent_buffer *buf,
4255f39d397SChris Mason 			     struct extent_buffer *parent, int parent_slot,
4265f39d397SChris Mason 			     struct extent_buffer **cow_ret,
4279fa8cfe7SChris Mason 			     u64 search_start, u64 empty_size)
4286702ed49SChris Mason {
4295d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
4305f39d397SChris Mason 	struct extent_buffer *cow;
4317bb86316SChris Mason 	int level;
432f0486c68SYan, Zheng 	int last_ref = 0;
433925baeddSChris Mason 	int unlock_orig = 0;
4345d4f98a2SYan Zheng 	u64 parent_start;
4356702ed49SChris Mason 
436925baeddSChris Mason 	if (*cow_ret == buf)
437925baeddSChris Mason 		unlock_orig = 1;
438925baeddSChris Mason 
439b9447ef8SChris Mason 	btrfs_assert_tree_locked(buf);
440925baeddSChris Mason 
4417bb86316SChris Mason 	WARN_ON(root->ref_cows && trans->transid !=
4427bb86316SChris Mason 		root->fs_info->running_transaction->transid);
4436702ed49SChris Mason 	WARN_ON(root->ref_cows && trans->transid != root->last_trans);
4445f39d397SChris Mason 
4457bb86316SChris Mason 	level = btrfs_header_level(buf);
44631840ae1SZheng Yan 
4475d4f98a2SYan Zheng 	if (level == 0)
4485d4f98a2SYan Zheng 		btrfs_item_key(buf, &disk_key, 0);
4495d4f98a2SYan Zheng 	else
4505d4f98a2SYan Zheng 		btrfs_node_key(buf, &disk_key, 0);
4515d4f98a2SYan Zheng 
4525d4f98a2SYan Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
4535d4f98a2SYan Zheng 		if (parent)
4545d4f98a2SYan Zheng 			parent_start = parent->start;
4555d4f98a2SYan Zheng 		else
4565d4f98a2SYan Zheng 			parent_start = 0;
4575d4f98a2SYan Zheng 	} else
4585d4f98a2SYan Zheng 		parent_start = 0;
4595d4f98a2SYan Zheng 
4605d4f98a2SYan Zheng 	cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
4615d4f98a2SYan Zheng 				     root->root_key.objectid, &disk_key,
46266d7e7f0SArne Jansen 				     level, search_start, empty_size, 1);
4636702ed49SChris Mason 	if (IS_ERR(cow))
4646702ed49SChris Mason 		return PTR_ERR(cow);
4656702ed49SChris Mason 
466b4ce94deSChris Mason 	/* cow is set to blocking by btrfs_init_new_buffer */
467b4ce94deSChris Mason 
4685f39d397SChris Mason 	copy_extent_buffer(cow, buf, 0, 0, cow->len);
469db94535dSChris Mason 	btrfs_set_header_bytenr(cow, cow->start);
4705f39d397SChris Mason 	btrfs_set_header_generation(cow, trans->transid);
4715d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
4725d4f98a2SYan Zheng 	btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
4735d4f98a2SYan Zheng 				     BTRFS_HEADER_FLAG_RELOC);
4745d4f98a2SYan Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4755d4f98a2SYan Zheng 		btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
4765d4f98a2SYan Zheng 	else
4775f39d397SChris Mason 		btrfs_set_header_owner(cow, root->root_key.objectid);
4786702ed49SChris Mason 
4792b82032cSYan Zheng 	write_extent_buffer(cow, root->fs_info->fsid,
4802b82032cSYan Zheng 			    (unsigned long)btrfs_header_fsid(cow),
4812b82032cSYan Zheng 			    BTRFS_FSID_SIZE);
4822b82032cSYan Zheng 
483f0486c68SYan, Zheng 	update_ref_for_cow(trans, root, buf, cow, &last_ref);
4841a40e23bSZheng Yan 
4853fd0a558SYan, Zheng 	if (root->ref_cows)
4863fd0a558SYan, Zheng 		btrfs_reloc_cow_block(trans, root, buf, cow);
4873fd0a558SYan, Zheng 
4886702ed49SChris Mason 	if (buf == root->node) {
489925baeddSChris Mason 		WARN_ON(parent && parent != buf);
4905d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
4915d4f98a2SYan Zheng 		    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
4925d4f98a2SYan Zheng 			parent_start = buf->start;
4935d4f98a2SYan Zheng 		else
4945d4f98a2SYan Zheng 			parent_start = 0;
495925baeddSChris Mason 
4965f39d397SChris Mason 		extent_buffer_get(cow);
497240f62c8SChris Mason 		rcu_assign_pointer(root->node, cow);
498925baeddSChris Mason 
499f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, buf, parent_start,
50066d7e7f0SArne Jansen 				      last_ref, 1);
5015f39d397SChris Mason 		free_extent_buffer(buf);
5020b86a832SChris Mason 		add_root_to_dirty_list(root);
5036702ed49SChris Mason 	} else {
5045d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
5055d4f98a2SYan Zheng 			parent_start = parent->start;
5065d4f98a2SYan Zheng 		else
5075d4f98a2SYan Zheng 			parent_start = 0;
5085d4f98a2SYan Zheng 
5095d4f98a2SYan Zheng 		WARN_ON(trans->transid != btrfs_header_generation(parent));
5105f39d397SChris Mason 		btrfs_set_node_blockptr(parent, parent_slot,
511db94535dSChris Mason 					cow->start);
51274493f7aSChris Mason 		btrfs_set_node_ptr_generation(parent, parent_slot,
51374493f7aSChris Mason 					      trans->transid);
5146702ed49SChris Mason 		btrfs_mark_buffer_dirty(parent);
515f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, buf, parent_start,
51666d7e7f0SArne Jansen 				      last_ref, 1);
5176702ed49SChris Mason 	}
518925baeddSChris Mason 	if (unlock_orig)
519925baeddSChris Mason 		btrfs_tree_unlock(buf);
520*3083ee2eSJosef Bacik 	free_extent_buffer_stale(buf);
5216702ed49SChris Mason 	btrfs_mark_buffer_dirty(cow);
5226702ed49SChris Mason 	*cow_ret = cow;
5236702ed49SChris Mason 	return 0;
5246702ed49SChris Mason }
5256702ed49SChris Mason 
5265d4f98a2SYan Zheng static inline int should_cow_block(struct btrfs_trans_handle *trans,
5275d4f98a2SYan Zheng 				   struct btrfs_root *root,
5285d4f98a2SYan Zheng 				   struct extent_buffer *buf)
5295d4f98a2SYan Zheng {
530f1ebcc74SLiu Bo 	/* ensure we can see the force_cow */
531f1ebcc74SLiu Bo 	smp_rmb();
532f1ebcc74SLiu Bo 
533f1ebcc74SLiu Bo 	/*
534f1ebcc74SLiu Bo 	 * We do not need to cow a block if
535f1ebcc74SLiu Bo 	 * 1) this block is not created or changed in this transaction;
536f1ebcc74SLiu Bo 	 * 2) this block does not belong to TREE_RELOC tree;
537f1ebcc74SLiu Bo 	 * 3) the root is not forced COW.
538f1ebcc74SLiu Bo 	 *
539f1ebcc74SLiu Bo 	 * What is forced COW:
540f1ebcc74SLiu Bo 	 *    when we create snapshot during commiting the transaction,
541f1ebcc74SLiu Bo 	 *    after we've finished coping src root, we must COW the shared
542f1ebcc74SLiu Bo 	 *    block to ensure the metadata consistency.
543f1ebcc74SLiu Bo 	 */
5445d4f98a2SYan Zheng 	if (btrfs_header_generation(buf) == trans->transid &&
5455d4f98a2SYan Zheng 	    !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
5465d4f98a2SYan Zheng 	    !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
547f1ebcc74SLiu Bo 	      btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
548f1ebcc74SLiu Bo 	    !root->force_cow)
5495d4f98a2SYan Zheng 		return 0;
5505d4f98a2SYan Zheng 	return 1;
5515d4f98a2SYan Zheng }
5525d4f98a2SYan Zheng 
553d352ac68SChris Mason /*
554d352ac68SChris Mason  * cows a single block, see __btrfs_cow_block for the real work.
555d352ac68SChris Mason  * This version of it has extra checks so that a block isn't cow'd more than
556d352ac68SChris Mason  * once per transaction, as long as it hasn't been written yet
557d352ac68SChris Mason  */
558d397712bSChris Mason noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
5595f39d397SChris Mason 		    struct btrfs_root *root, struct extent_buffer *buf,
5605f39d397SChris Mason 		    struct extent_buffer *parent, int parent_slot,
5619fa8cfe7SChris Mason 		    struct extent_buffer **cow_ret)
56202217ed2SChris Mason {
5636702ed49SChris Mason 	u64 search_start;
564f510cfecSChris Mason 	int ret;
565dc17ff8fSChris Mason 
566ccd467d6SChris Mason 	if (trans->transaction != root->fs_info->running_transaction) {
567d397712bSChris Mason 		printk(KERN_CRIT "trans %llu running %llu\n",
568d397712bSChris Mason 		       (unsigned long long)trans->transid,
569d397712bSChris Mason 		       (unsigned long long)
570ccd467d6SChris Mason 		       root->fs_info->running_transaction->transid);
571ccd467d6SChris Mason 		WARN_ON(1);
572ccd467d6SChris Mason 	}
573ccd467d6SChris Mason 	if (trans->transid != root->fs_info->generation) {
574d397712bSChris Mason 		printk(KERN_CRIT "trans %llu running %llu\n",
575d397712bSChris Mason 		       (unsigned long long)trans->transid,
576d397712bSChris Mason 		       (unsigned long long)root->fs_info->generation);
577ccd467d6SChris Mason 		WARN_ON(1);
578ccd467d6SChris Mason 	}
579dc17ff8fSChris Mason 
5805d4f98a2SYan Zheng 	if (!should_cow_block(trans, root, buf)) {
58102217ed2SChris Mason 		*cow_ret = buf;
58202217ed2SChris Mason 		return 0;
58302217ed2SChris Mason 	}
584c487685dSChris Mason 
5850b86a832SChris Mason 	search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
586b4ce94deSChris Mason 
587b4ce94deSChris Mason 	if (parent)
588b4ce94deSChris Mason 		btrfs_set_lock_blocking(parent);
589b4ce94deSChris Mason 	btrfs_set_lock_blocking(buf);
590b4ce94deSChris Mason 
591f510cfecSChris Mason 	ret = __btrfs_cow_block(trans, root, buf, parent,
5929fa8cfe7SChris Mason 				 parent_slot, cow_ret, search_start, 0);
5931abe9b8aSliubo 
5941abe9b8aSliubo 	trace_btrfs_cow_block(root, buf, *cow_ret);
5951abe9b8aSliubo 
596f510cfecSChris Mason 	return ret;
5972c90e5d6SChris Mason }
5986702ed49SChris Mason 
599d352ac68SChris Mason /*
600d352ac68SChris Mason  * helper function for defrag to decide if two blocks pointed to by a
601d352ac68SChris Mason  * node are actually close by
602d352ac68SChris Mason  */
6036b80053dSChris Mason static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
6046702ed49SChris Mason {
6056b80053dSChris Mason 	if (blocknr < other && other - (blocknr + blocksize) < 32768)
6066702ed49SChris Mason 		return 1;
6076b80053dSChris Mason 	if (blocknr > other && blocknr - (other + blocksize) < 32768)
6086702ed49SChris Mason 		return 1;
60902217ed2SChris Mason 	return 0;
61002217ed2SChris Mason }
61102217ed2SChris Mason 
612081e9573SChris Mason /*
613081e9573SChris Mason  * compare two keys in a memcmp fashion
614081e9573SChris Mason  */
615081e9573SChris Mason static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
616081e9573SChris Mason {
617081e9573SChris Mason 	struct btrfs_key k1;
618081e9573SChris Mason 
619081e9573SChris Mason 	btrfs_disk_key_to_cpu(&k1, disk);
620081e9573SChris Mason 
62120736abaSDiego Calleja 	return btrfs_comp_cpu_keys(&k1, k2);
622081e9573SChris Mason }
623081e9573SChris Mason 
624f3465ca4SJosef Bacik /*
625f3465ca4SJosef Bacik  * same as comp_keys only with two btrfs_key's
626f3465ca4SJosef Bacik  */
6275d4f98a2SYan Zheng int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
628f3465ca4SJosef Bacik {
629f3465ca4SJosef Bacik 	if (k1->objectid > k2->objectid)
630f3465ca4SJosef Bacik 		return 1;
631f3465ca4SJosef Bacik 	if (k1->objectid < k2->objectid)
632f3465ca4SJosef Bacik 		return -1;
633f3465ca4SJosef Bacik 	if (k1->type > k2->type)
634f3465ca4SJosef Bacik 		return 1;
635f3465ca4SJosef Bacik 	if (k1->type < k2->type)
636f3465ca4SJosef Bacik 		return -1;
637f3465ca4SJosef Bacik 	if (k1->offset > k2->offset)
638f3465ca4SJosef Bacik 		return 1;
639f3465ca4SJosef Bacik 	if (k1->offset < k2->offset)
640f3465ca4SJosef Bacik 		return -1;
641f3465ca4SJosef Bacik 	return 0;
642f3465ca4SJosef Bacik }
643081e9573SChris Mason 
644d352ac68SChris Mason /*
645d352ac68SChris Mason  * this is used by the defrag code to go through all the
646d352ac68SChris Mason  * leaves pointed to by a node and reallocate them so that
647d352ac68SChris Mason  * disk order is close to key order
648d352ac68SChris Mason  */
6496702ed49SChris Mason int btrfs_realloc_node(struct btrfs_trans_handle *trans,
6505f39d397SChris Mason 		       struct btrfs_root *root, struct extent_buffer *parent,
651a6b6e75eSChris Mason 		       int start_slot, int cache_only, u64 *last_ret,
652a6b6e75eSChris Mason 		       struct btrfs_key *progress)
6536702ed49SChris Mason {
6546b80053dSChris Mason 	struct extent_buffer *cur;
6556702ed49SChris Mason 	u64 blocknr;
656ca7a79adSChris Mason 	u64 gen;
657e9d0b13bSChris Mason 	u64 search_start = *last_ret;
658e9d0b13bSChris Mason 	u64 last_block = 0;
6596702ed49SChris Mason 	u64 other;
6606702ed49SChris Mason 	u32 parent_nritems;
6616702ed49SChris Mason 	int end_slot;
6626702ed49SChris Mason 	int i;
6636702ed49SChris Mason 	int err = 0;
664f2183bdeSChris Mason 	int parent_level;
6656b80053dSChris Mason 	int uptodate;
6666b80053dSChris Mason 	u32 blocksize;
667081e9573SChris Mason 	int progress_passed = 0;
668081e9573SChris Mason 	struct btrfs_disk_key disk_key;
6696702ed49SChris Mason 
6705708b959SChris Mason 	parent_level = btrfs_header_level(parent);
6715708b959SChris Mason 	if (cache_only && parent_level != 1)
6725708b959SChris Mason 		return 0;
6735708b959SChris Mason 
674d397712bSChris Mason 	if (trans->transaction != root->fs_info->running_transaction)
6756702ed49SChris Mason 		WARN_ON(1);
676d397712bSChris Mason 	if (trans->transid != root->fs_info->generation)
6776702ed49SChris Mason 		WARN_ON(1);
67886479a04SChris Mason 
6796b80053dSChris Mason 	parent_nritems = btrfs_header_nritems(parent);
6806b80053dSChris Mason 	blocksize = btrfs_level_size(root, parent_level - 1);
6816702ed49SChris Mason 	end_slot = parent_nritems;
6826702ed49SChris Mason 
6836702ed49SChris Mason 	if (parent_nritems == 1)
6846702ed49SChris Mason 		return 0;
6856702ed49SChris Mason 
686b4ce94deSChris Mason 	btrfs_set_lock_blocking(parent);
687b4ce94deSChris Mason 
6886702ed49SChris Mason 	for (i = start_slot; i < end_slot; i++) {
6896702ed49SChris Mason 		int close = 1;
690a6b6e75eSChris Mason 
691081e9573SChris Mason 		btrfs_node_key(parent, &disk_key, i);
692081e9573SChris Mason 		if (!progress_passed && comp_keys(&disk_key, progress) < 0)
693081e9573SChris Mason 			continue;
694081e9573SChris Mason 
695081e9573SChris Mason 		progress_passed = 1;
6966b80053dSChris Mason 		blocknr = btrfs_node_blockptr(parent, i);
697ca7a79adSChris Mason 		gen = btrfs_node_ptr_generation(parent, i);
698e9d0b13bSChris Mason 		if (last_block == 0)
699e9d0b13bSChris Mason 			last_block = blocknr;
7005708b959SChris Mason 
7016702ed49SChris Mason 		if (i > 0) {
7026b80053dSChris Mason 			other = btrfs_node_blockptr(parent, i - 1);
7036b80053dSChris Mason 			close = close_blocks(blocknr, other, blocksize);
7046702ed49SChris Mason 		}
7050ef3e66bSChris Mason 		if (!close && i < end_slot - 2) {
7066b80053dSChris Mason 			other = btrfs_node_blockptr(parent, i + 1);
7076b80053dSChris Mason 			close = close_blocks(blocknr, other, blocksize);
7086702ed49SChris Mason 		}
709e9d0b13bSChris Mason 		if (close) {
710e9d0b13bSChris Mason 			last_block = blocknr;
7116702ed49SChris Mason 			continue;
712e9d0b13bSChris Mason 		}
7136702ed49SChris Mason 
7146b80053dSChris Mason 		cur = btrfs_find_tree_block(root, blocknr, blocksize);
7156b80053dSChris Mason 		if (cur)
7161259ab75SChris Mason 			uptodate = btrfs_buffer_uptodate(cur, gen);
7176b80053dSChris Mason 		else
7186b80053dSChris Mason 			uptodate = 0;
7195708b959SChris Mason 		if (!cur || !uptodate) {
7206702ed49SChris Mason 			if (cache_only) {
7216b80053dSChris Mason 				free_extent_buffer(cur);
7226702ed49SChris Mason 				continue;
7236702ed49SChris Mason 			}
7246b80053dSChris Mason 			if (!cur) {
7256b80053dSChris Mason 				cur = read_tree_block(root, blocknr,
726ca7a79adSChris Mason 							 blocksize, gen);
72797d9a8a4STsutomu Itoh 				if (!cur)
72897d9a8a4STsutomu Itoh 					return -EIO;
7296b80053dSChris Mason 			} else if (!uptodate) {
730ca7a79adSChris Mason 				btrfs_read_buffer(cur, gen);
7316702ed49SChris Mason 			}
732f2183bdeSChris Mason 		}
733e9d0b13bSChris Mason 		if (search_start == 0)
7346b80053dSChris Mason 			search_start = last_block;
735e9d0b13bSChris Mason 
736e7a84565SChris Mason 		btrfs_tree_lock(cur);
737b4ce94deSChris Mason 		btrfs_set_lock_blocking(cur);
7386b80053dSChris Mason 		err = __btrfs_cow_block(trans, root, cur, parent, i,
739e7a84565SChris Mason 					&cur, search_start,
7406b80053dSChris Mason 					min(16 * blocksize,
7419fa8cfe7SChris Mason 					    (end_slot - i) * blocksize));
742252c38f0SYan 		if (err) {
743e7a84565SChris Mason 			btrfs_tree_unlock(cur);
7446b80053dSChris Mason 			free_extent_buffer(cur);
7456702ed49SChris Mason 			break;
746252c38f0SYan 		}
747e7a84565SChris Mason 		search_start = cur->start;
748e7a84565SChris Mason 		last_block = cur->start;
749f2183bdeSChris Mason 		*last_ret = search_start;
750e7a84565SChris Mason 		btrfs_tree_unlock(cur);
751e7a84565SChris Mason 		free_extent_buffer(cur);
7526702ed49SChris Mason 	}
7536702ed49SChris Mason 	return err;
7546702ed49SChris Mason }
7556702ed49SChris Mason 
75674123bd7SChris Mason /*
75774123bd7SChris Mason  * The leaf data grows from end-to-front in the node.
75874123bd7SChris Mason  * this returns the address of the start of the last item,
75974123bd7SChris Mason  * which is the stop of the leaf data stack
76074123bd7SChris Mason  */
761123abc88SChris Mason static inline unsigned int leaf_data_end(struct btrfs_root *root,
7625f39d397SChris Mason 					 struct extent_buffer *leaf)
763be0e5c09SChris Mason {
7645f39d397SChris Mason 	u32 nr = btrfs_header_nritems(leaf);
765be0e5c09SChris Mason 	if (nr == 0)
766123abc88SChris Mason 		return BTRFS_LEAF_DATA_SIZE(root);
7675f39d397SChris Mason 	return btrfs_item_offset_nr(leaf, nr - 1);
768be0e5c09SChris Mason }
769be0e5c09SChris Mason 
770aa5d6bedSChris Mason 
77174123bd7SChris Mason /*
7725f39d397SChris Mason  * search for key in the extent_buffer.  The items start at offset p,
7735f39d397SChris Mason  * and they are item_size apart.  There are 'max' items in p.
7745f39d397SChris Mason  *
77574123bd7SChris Mason  * the slot in the array is returned via slot, and it points to
77674123bd7SChris Mason  * the place where you would insert key if it is not found in
77774123bd7SChris Mason  * the array.
77874123bd7SChris Mason  *
77974123bd7SChris Mason  * slot may point to max if the key is bigger than all of the keys
78074123bd7SChris Mason  */
781e02119d5SChris Mason static noinline int generic_bin_search(struct extent_buffer *eb,
782e02119d5SChris Mason 				       unsigned long p,
7835f39d397SChris Mason 				       int item_size, struct btrfs_key *key,
784be0e5c09SChris Mason 				       int max, int *slot)
785be0e5c09SChris Mason {
786be0e5c09SChris Mason 	int low = 0;
787be0e5c09SChris Mason 	int high = max;
788be0e5c09SChris Mason 	int mid;
789be0e5c09SChris Mason 	int ret;
790479965d6SChris Mason 	struct btrfs_disk_key *tmp = NULL;
7915f39d397SChris Mason 	struct btrfs_disk_key unaligned;
7925f39d397SChris Mason 	unsigned long offset;
7935f39d397SChris Mason 	char *kaddr = NULL;
7945f39d397SChris Mason 	unsigned long map_start = 0;
7955f39d397SChris Mason 	unsigned long map_len = 0;
796479965d6SChris Mason 	int err;
797be0e5c09SChris Mason 
798be0e5c09SChris Mason 	while (low < high) {
799be0e5c09SChris Mason 		mid = (low + high) / 2;
8005f39d397SChris Mason 		offset = p + mid * item_size;
8015f39d397SChris Mason 
802a6591715SChris Mason 		if (!kaddr || offset < map_start ||
8035f39d397SChris Mason 		    (offset + sizeof(struct btrfs_disk_key)) >
8045f39d397SChris Mason 		    map_start + map_len) {
805934d375bSChris Mason 
806934d375bSChris Mason 			err = map_private_extent_buffer(eb, offset,
807479965d6SChris Mason 						sizeof(struct btrfs_disk_key),
808a6591715SChris Mason 						&kaddr, &map_start, &map_len);
8095f39d397SChris Mason 
810479965d6SChris Mason 			if (!err) {
811479965d6SChris Mason 				tmp = (struct btrfs_disk_key *)(kaddr + offset -
812479965d6SChris Mason 							map_start);
813479965d6SChris Mason 			} else {
8145f39d397SChris Mason 				read_extent_buffer(eb, &unaligned,
8155f39d397SChris Mason 						   offset, sizeof(unaligned));
8165f39d397SChris Mason 				tmp = &unaligned;
817479965d6SChris Mason 			}
818479965d6SChris Mason 
8195f39d397SChris Mason 		} else {
8205f39d397SChris Mason 			tmp = (struct btrfs_disk_key *)(kaddr + offset -
8215f39d397SChris Mason 							map_start);
8225f39d397SChris Mason 		}
823be0e5c09SChris Mason 		ret = comp_keys(tmp, key);
824be0e5c09SChris Mason 
825be0e5c09SChris Mason 		if (ret < 0)
826be0e5c09SChris Mason 			low = mid + 1;
827be0e5c09SChris Mason 		else if (ret > 0)
828be0e5c09SChris Mason 			high = mid;
829be0e5c09SChris Mason 		else {
830be0e5c09SChris Mason 			*slot = mid;
831be0e5c09SChris Mason 			return 0;
832be0e5c09SChris Mason 		}
833be0e5c09SChris Mason 	}
834be0e5c09SChris Mason 	*slot = low;
835be0e5c09SChris Mason 	return 1;
836be0e5c09SChris Mason }
837be0e5c09SChris Mason 
83897571fd0SChris Mason /*
83997571fd0SChris Mason  * simple bin_search frontend that does the right thing for
84097571fd0SChris Mason  * leaves vs nodes
84197571fd0SChris Mason  */
8425f39d397SChris Mason static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
8435f39d397SChris Mason 		      int level, int *slot)
844be0e5c09SChris Mason {
8455f39d397SChris Mason 	if (level == 0) {
8465f39d397SChris Mason 		return generic_bin_search(eb,
8475f39d397SChris Mason 					  offsetof(struct btrfs_leaf, items),
8480783fcfcSChris Mason 					  sizeof(struct btrfs_item),
8495f39d397SChris Mason 					  key, btrfs_header_nritems(eb),
8507518a238SChris Mason 					  slot);
851be0e5c09SChris Mason 	} else {
8525f39d397SChris Mason 		return generic_bin_search(eb,
8535f39d397SChris Mason 					  offsetof(struct btrfs_node, ptrs),
854123abc88SChris Mason 					  sizeof(struct btrfs_key_ptr),
8555f39d397SChris Mason 					  key, btrfs_header_nritems(eb),
8567518a238SChris Mason 					  slot);
857be0e5c09SChris Mason 	}
858be0e5c09SChris Mason 	return -1;
859be0e5c09SChris Mason }
860be0e5c09SChris Mason 
8615d4f98a2SYan Zheng int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
8625d4f98a2SYan Zheng 		     int level, int *slot)
8635d4f98a2SYan Zheng {
8645d4f98a2SYan Zheng 	return bin_search(eb, key, level, slot);
8655d4f98a2SYan Zheng }
8665d4f98a2SYan Zheng 
867f0486c68SYan, Zheng static void root_add_used(struct btrfs_root *root, u32 size)
868f0486c68SYan, Zheng {
869f0486c68SYan, Zheng 	spin_lock(&root->accounting_lock);
870f0486c68SYan, Zheng 	btrfs_set_root_used(&root->root_item,
871f0486c68SYan, Zheng 			    btrfs_root_used(&root->root_item) + size);
872f0486c68SYan, Zheng 	spin_unlock(&root->accounting_lock);
873f0486c68SYan, Zheng }
874f0486c68SYan, Zheng 
875f0486c68SYan, Zheng static void root_sub_used(struct btrfs_root *root, u32 size)
876f0486c68SYan, Zheng {
877f0486c68SYan, Zheng 	spin_lock(&root->accounting_lock);
878f0486c68SYan, Zheng 	btrfs_set_root_used(&root->root_item,
879f0486c68SYan, Zheng 			    btrfs_root_used(&root->root_item) - size);
880f0486c68SYan, Zheng 	spin_unlock(&root->accounting_lock);
881f0486c68SYan, Zheng }
882f0486c68SYan, Zheng 
883d352ac68SChris Mason /* given a node and slot number, this reads the blocks it points to.  The
884d352ac68SChris Mason  * extent buffer is returned with a reference taken (but unlocked).
885d352ac68SChris Mason  * NULL is returned on error.
886d352ac68SChris Mason  */
887e02119d5SChris Mason static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
8885f39d397SChris Mason 				   struct extent_buffer *parent, int slot)
889bb803951SChris Mason {
890ca7a79adSChris Mason 	int level = btrfs_header_level(parent);
891bb803951SChris Mason 	if (slot < 0)
892bb803951SChris Mason 		return NULL;
8935f39d397SChris Mason 	if (slot >= btrfs_header_nritems(parent))
894bb803951SChris Mason 		return NULL;
895ca7a79adSChris Mason 
896ca7a79adSChris Mason 	BUG_ON(level == 0);
897ca7a79adSChris Mason 
898db94535dSChris Mason 	return read_tree_block(root, btrfs_node_blockptr(parent, slot),
899ca7a79adSChris Mason 		       btrfs_level_size(root, level - 1),
900ca7a79adSChris Mason 		       btrfs_node_ptr_generation(parent, slot));
901bb803951SChris Mason }
902bb803951SChris Mason 
903d352ac68SChris Mason /*
904d352ac68SChris Mason  * node level balancing, used to make sure nodes are in proper order for
905d352ac68SChris Mason  * item deletion.  We balance from the top down, so we have to make sure
906d352ac68SChris Mason  * that a deletion won't leave an node completely empty later on.
907d352ac68SChris Mason  */
908e02119d5SChris Mason static noinline int balance_level(struct btrfs_trans_handle *trans,
90998ed5174SChris Mason 			 struct btrfs_root *root,
91098ed5174SChris Mason 			 struct btrfs_path *path, int level)
911bb803951SChris Mason {
9125f39d397SChris Mason 	struct extent_buffer *right = NULL;
9135f39d397SChris Mason 	struct extent_buffer *mid;
9145f39d397SChris Mason 	struct extent_buffer *left = NULL;
9155f39d397SChris Mason 	struct extent_buffer *parent = NULL;
916bb803951SChris Mason 	int ret = 0;
917bb803951SChris Mason 	int wret;
918bb803951SChris Mason 	int pslot;
919bb803951SChris Mason 	int orig_slot = path->slots[level];
92079f95c82SChris Mason 	u64 orig_ptr;
921bb803951SChris Mason 
922bb803951SChris Mason 	if (level == 0)
923bb803951SChris Mason 		return 0;
924bb803951SChris Mason 
9255f39d397SChris Mason 	mid = path->nodes[level];
926b4ce94deSChris Mason 
927bd681513SChris Mason 	WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
928bd681513SChris Mason 		path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
9297bb86316SChris Mason 	WARN_ON(btrfs_header_generation(mid) != trans->transid);
9307bb86316SChris Mason 
9311d4f8a0cSChris Mason 	orig_ptr = btrfs_node_blockptr(mid, orig_slot);
93279f95c82SChris Mason 
933a05a9bb1SLi Zefan 	if (level < BTRFS_MAX_LEVEL - 1) {
9345f39d397SChris Mason 		parent = path->nodes[level + 1];
935bb803951SChris Mason 		pslot = path->slots[level + 1];
936a05a9bb1SLi Zefan 	}
937bb803951SChris Mason 
93840689478SChris Mason 	/*
93940689478SChris Mason 	 * deal with the case where there is only one pointer in the root
94040689478SChris Mason 	 * by promoting the node below to a root
94140689478SChris Mason 	 */
9425f39d397SChris Mason 	if (!parent) {
9435f39d397SChris Mason 		struct extent_buffer *child;
944bb803951SChris Mason 
9455f39d397SChris Mason 		if (btrfs_header_nritems(mid) != 1)
946bb803951SChris Mason 			return 0;
947bb803951SChris Mason 
948bb803951SChris Mason 		/* promote the child to a root */
9495f39d397SChris Mason 		child = read_node_slot(root, mid, 0);
9507951f3ceSJeff Mahoney 		BUG_ON(!child);
951925baeddSChris Mason 		btrfs_tree_lock(child);
952b4ce94deSChris Mason 		btrfs_set_lock_blocking(child);
9539fa8cfe7SChris Mason 		ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
954f0486c68SYan, Zheng 		if (ret) {
955f0486c68SYan, Zheng 			btrfs_tree_unlock(child);
956f0486c68SYan, Zheng 			free_extent_buffer(child);
957f0486c68SYan, Zheng 			goto enospc;
958f0486c68SYan, Zheng 		}
9592f375ab9SYan 
960240f62c8SChris Mason 		rcu_assign_pointer(root->node, child);
961925baeddSChris Mason 
9620b86a832SChris Mason 		add_root_to_dirty_list(root);
963925baeddSChris Mason 		btrfs_tree_unlock(child);
964b4ce94deSChris Mason 
965925baeddSChris Mason 		path->locks[level] = 0;
966bb803951SChris Mason 		path->nodes[level] = NULL;
9675f39d397SChris Mason 		clean_tree_block(trans, root, mid);
968925baeddSChris Mason 		btrfs_tree_unlock(mid);
969bb803951SChris Mason 		/* once for the path */
9705f39d397SChris Mason 		free_extent_buffer(mid);
971f0486c68SYan, Zheng 
972f0486c68SYan, Zheng 		root_sub_used(root, mid->len);
97366d7e7f0SArne Jansen 		btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
974bb803951SChris Mason 		/* once for the root ptr */
975*3083ee2eSJosef Bacik 		free_extent_buffer_stale(mid);
976f0486c68SYan, Zheng 		return 0;
977bb803951SChris Mason 	}
9785f39d397SChris Mason 	if (btrfs_header_nritems(mid) >
979123abc88SChris Mason 	    BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
980bb803951SChris Mason 		return 0;
981bb803951SChris Mason 
982559af821SAndi Kleen 	btrfs_header_nritems(mid);
98354aa1f4dSChris Mason 
9845f39d397SChris Mason 	left = read_node_slot(root, parent, pslot - 1);
9855f39d397SChris Mason 	if (left) {
986925baeddSChris Mason 		btrfs_tree_lock(left);
987b4ce94deSChris Mason 		btrfs_set_lock_blocking(left);
9885f39d397SChris Mason 		wret = btrfs_cow_block(trans, root, left,
9899fa8cfe7SChris Mason 				       parent, pslot - 1, &left);
99054aa1f4dSChris Mason 		if (wret) {
99154aa1f4dSChris Mason 			ret = wret;
99254aa1f4dSChris Mason 			goto enospc;
99354aa1f4dSChris Mason 		}
9942cc58cf2SChris Mason 	}
9955f39d397SChris Mason 	right = read_node_slot(root, parent, pslot + 1);
9965f39d397SChris Mason 	if (right) {
997925baeddSChris Mason 		btrfs_tree_lock(right);
998b4ce94deSChris Mason 		btrfs_set_lock_blocking(right);
9995f39d397SChris Mason 		wret = btrfs_cow_block(trans, root, right,
10009fa8cfe7SChris Mason 				       parent, pslot + 1, &right);
10012cc58cf2SChris Mason 		if (wret) {
10022cc58cf2SChris Mason 			ret = wret;
10032cc58cf2SChris Mason 			goto enospc;
10042cc58cf2SChris Mason 		}
10052cc58cf2SChris Mason 	}
10062cc58cf2SChris Mason 
10072cc58cf2SChris Mason 	/* first, try to make some room in the middle buffer */
10085f39d397SChris Mason 	if (left) {
10095f39d397SChris Mason 		orig_slot += btrfs_header_nritems(left);
1010bce4eae9SChris Mason 		wret = push_node_left(trans, root, left, mid, 1);
101179f95c82SChris Mason 		if (wret < 0)
101279f95c82SChris Mason 			ret = wret;
1013559af821SAndi Kleen 		btrfs_header_nritems(mid);
1014bb803951SChris Mason 	}
101579f95c82SChris Mason 
101679f95c82SChris Mason 	/*
101779f95c82SChris Mason 	 * then try to empty the right most buffer into the middle
101879f95c82SChris Mason 	 */
10195f39d397SChris Mason 	if (right) {
1020971a1f66SChris Mason 		wret = push_node_left(trans, root, mid, right, 1);
102154aa1f4dSChris Mason 		if (wret < 0 && wret != -ENOSPC)
102279f95c82SChris Mason 			ret = wret;
10235f39d397SChris Mason 		if (btrfs_header_nritems(right) == 0) {
10245f39d397SChris Mason 			clean_tree_block(trans, root, right);
1025925baeddSChris Mason 			btrfs_tree_unlock(right);
1026e089f05cSChris Mason 			wret = del_ptr(trans, root, path, level + 1, pslot +
1027e089f05cSChris Mason 				       1);
1028bb803951SChris Mason 			if (wret)
1029bb803951SChris Mason 				ret = wret;
1030f0486c68SYan, Zheng 			root_sub_used(root, right->len);
103166d7e7f0SArne Jansen 			btrfs_free_tree_block(trans, root, right, 0, 1, 0);
1032*3083ee2eSJosef Bacik 			free_extent_buffer_stale(right);
1033f0486c68SYan, Zheng 			right = NULL;
1034bb803951SChris Mason 		} else {
10355f39d397SChris Mason 			struct btrfs_disk_key right_key;
10365f39d397SChris Mason 			btrfs_node_key(right, &right_key, 0);
10375f39d397SChris Mason 			btrfs_set_node_key(parent, &right_key, pslot + 1);
10385f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
1039bb803951SChris Mason 		}
1040bb803951SChris Mason 	}
10415f39d397SChris Mason 	if (btrfs_header_nritems(mid) == 1) {
104279f95c82SChris Mason 		/*
104379f95c82SChris Mason 		 * we're not allowed to leave a node with one item in the
104479f95c82SChris Mason 		 * tree during a delete.  A deletion from lower in the tree
104579f95c82SChris Mason 		 * could try to delete the only pointer in this node.
104679f95c82SChris Mason 		 * So, pull some keys from the left.
104779f95c82SChris Mason 		 * There has to be a left pointer at this point because
104879f95c82SChris Mason 		 * otherwise we would have pulled some pointers from the
104979f95c82SChris Mason 		 * right
105079f95c82SChris Mason 		 */
10515f39d397SChris Mason 		BUG_ON(!left);
10525f39d397SChris Mason 		wret = balance_node_right(trans, root, mid, left);
105354aa1f4dSChris Mason 		if (wret < 0) {
105479f95c82SChris Mason 			ret = wret;
105554aa1f4dSChris Mason 			goto enospc;
105654aa1f4dSChris Mason 		}
1057bce4eae9SChris Mason 		if (wret == 1) {
1058bce4eae9SChris Mason 			wret = push_node_left(trans, root, left, mid, 1);
1059bce4eae9SChris Mason 			if (wret < 0)
1060bce4eae9SChris Mason 				ret = wret;
1061bce4eae9SChris Mason 		}
106279f95c82SChris Mason 		BUG_ON(wret == 1);
106379f95c82SChris Mason 	}
10645f39d397SChris Mason 	if (btrfs_header_nritems(mid) == 0) {
10655f39d397SChris Mason 		clean_tree_block(trans, root, mid);
1066925baeddSChris Mason 		btrfs_tree_unlock(mid);
1067e089f05cSChris Mason 		wret = del_ptr(trans, root, path, level + 1, pslot);
1068bb803951SChris Mason 		if (wret)
1069bb803951SChris Mason 			ret = wret;
1070f0486c68SYan, Zheng 		root_sub_used(root, mid->len);
107166d7e7f0SArne Jansen 		btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
1072*3083ee2eSJosef Bacik 		free_extent_buffer_stale(mid);
1073f0486c68SYan, Zheng 		mid = NULL;
107479f95c82SChris Mason 	} else {
107579f95c82SChris Mason 		/* update the parent key to reflect our changes */
10765f39d397SChris Mason 		struct btrfs_disk_key mid_key;
10775f39d397SChris Mason 		btrfs_node_key(mid, &mid_key, 0);
10785f39d397SChris Mason 		btrfs_set_node_key(parent, &mid_key, pslot);
10795f39d397SChris Mason 		btrfs_mark_buffer_dirty(parent);
108079f95c82SChris Mason 	}
1081bb803951SChris Mason 
108279f95c82SChris Mason 	/* update the path */
10835f39d397SChris Mason 	if (left) {
10845f39d397SChris Mason 		if (btrfs_header_nritems(left) > orig_slot) {
10855f39d397SChris Mason 			extent_buffer_get(left);
1086925baeddSChris Mason 			/* left was locked after cow */
10875f39d397SChris Mason 			path->nodes[level] = left;
1088bb803951SChris Mason 			path->slots[level + 1] -= 1;
1089bb803951SChris Mason 			path->slots[level] = orig_slot;
1090925baeddSChris Mason 			if (mid) {
1091925baeddSChris Mason 				btrfs_tree_unlock(mid);
10925f39d397SChris Mason 				free_extent_buffer(mid);
1093925baeddSChris Mason 			}
1094bb803951SChris Mason 		} else {
10955f39d397SChris Mason 			orig_slot -= btrfs_header_nritems(left);
1096bb803951SChris Mason 			path->slots[level] = orig_slot;
1097bb803951SChris Mason 		}
1098bb803951SChris Mason 	}
109979f95c82SChris Mason 	/* double check we haven't messed things up */
1100e20d96d6SChris Mason 	if (orig_ptr !=
11015f39d397SChris Mason 	    btrfs_node_blockptr(path->nodes[level], path->slots[level]))
110279f95c82SChris Mason 		BUG();
110354aa1f4dSChris Mason enospc:
1104925baeddSChris Mason 	if (right) {
1105925baeddSChris Mason 		btrfs_tree_unlock(right);
11065f39d397SChris Mason 		free_extent_buffer(right);
1107925baeddSChris Mason 	}
1108925baeddSChris Mason 	if (left) {
1109925baeddSChris Mason 		if (path->nodes[level] != left)
1110925baeddSChris Mason 			btrfs_tree_unlock(left);
11115f39d397SChris Mason 		free_extent_buffer(left);
1112925baeddSChris Mason 	}
1113bb803951SChris Mason 	return ret;
1114bb803951SChris Mason }
1115bb803951SChris Mason 
1116d352ac68SChris Mason /* Node balancing for insertion.  Here we only split or push nodes around
1117d352ac68SChris Mason  * when they are completely full.  This is also done top down, so we
1118d352ac68SChris Mason  * have to be pessimistic.
1119d352ac68SChris Mason  */
1120d397712bSChris Mason static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1121e66f709bSChris Mason 					  struct btrfs_root *root,
1122e66f709bSChris Mason 					  struct btrfs_path *path, int level)
1123e66f709bSChris Mason {
11245f39d397SChris Mason 	struct extent_buffer *right = NULL;
11255f39d397SChris Mason 	struct extent_buffer *mid;
11265f39d397SChris Mason 	struct extent_buffer *left = NULL;
11275f39d397SChris Mason 	struct extent_buffer *parent = NULL;
1128e66f709bSChris Mason 	int ret = 0;
1129e66f709bSChris Mason 	int wret;
1130e66f709bSChris Mason 	int pslot;
1131e66f709bSChris Mason 	int orig_slot = path->slots[level];
1132e66f709bSChris Mason 
1133e66f709bSChris Mason 	if (level == 0)
1134e66f709bSChris Mason 		return 1;
1135e66f709bSChris Mason 
11365f39d397SChris Mason 	mid = path->nodes[level];
11377bb86316SChris Mason 	WARN_ON(btrfs_header_generation(mid) != trans->transid);
1138e66f709bSChris Mason 
1139a05a9bb1SLi Zefan 	if (level < BTRFS_MAX_LEVEL - 1) {
11405f39d397SChris Mason 		parent = path->nodes[level + 1];
1141e66f709bSChris Mason 		pslot = path->slots[level + 1];
1142a05a9bb1SLi Zefan 	}
1143e66f709bSChris Mason 
11445f39d397SChris Mason 	if (!parent)
1145e66f709bSChris Mason 		return 1;
1146e66f709bSChris Mason 
11475f39d397SChris Mason 	left = read_node_slot(root, parent, pslot - 1);
1148e66f709bSChris Mason 
1149e66f709bSChris Mason 	/* first, try to make some room in the middle buffer */
11505f39d397SChris Mason 	if (left) {
1151e66f709bSChris Mason 		u32 left_nr;
1152925baeddSChris Mason 
1153925baeddSChris Mason 		btrfs_tree_lock(left);
1154b4ce94deSChris Mason 		btrfs_set_lock_blocking(left);
1155b4ce94deSChris Mason 
11565f39d397SChris Mason 		left_nr = btrfs_header_nritems(left);
115733ade1f8SChris Mason 		if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
115833ade1f8SChris Mason 			wret = 1;
115933ade1f8SChris Mason 		} else {
11605f39d397SChris Mason 			ret = btrfs_cow_block(trans, root, left, parent,
11619fa8cfe7SChris Mason 					      pslot - 1, &left);
116254aa1f4dSChris Mason 			if (ret)
116354aa1f4dSChris Mason 				wret = 1;
116454aa1f4dSChris Mason 			else {
116554aa1f4dSChris Mason 				wret = push_node_left(trans, root,
1166971a1f66SChris Mason 						      left, mid, 0);
116754aa1f4dSChris Mason 			}
116833ade1f8SChris Mason 		}
1169e66f709bSChris Mason 		if (wret < 0)
1170e66f709bSChris Mason 			ret = wret;
1171e66f709bSChris Mason 		if (wret == 0) {
11725f39d397SChris Mason 			struct btrfs_disk_key disk_key;
1173e66f709bSChris Mason 			orig_slot += left_nr;
11745f39d397SChris Mason 			btrfs_node_key(mid, &disk_key, 0);
11755f39d397SChris Mason 			btrfs_set_node_key(parent, &disk_key, pslot);
11765f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
11775f39d397SChris Mason 			if (btrfs_header_nritems(left) > orig_slot) {
11785f39d397SChris Mason 				path->nodes[level] = left;
1179e66f709bSChris Mason 				path->slots[level + 1] -= 1;
1180e66f709bSChris Mason 				path->slots[level] = orig_slot;
1181925baeddSChris Mason 				btrfs_tree_unlock(mid);
11825f39d397SChris Mason 				free_extent_buffer(mid);
1183e66f709bSChris Mason 			} else {
1184e66f709bSChris Mason 				orig_slot -=
11855f39d397SChris Mason 					btrfs_header_nritems(left);
1186e66f709bSChris Mason 				path->slots[level] = orig_slot;
1187925baeddSChris Mason 				btrfs_tree_unlock(left);
11885f39d397SChris Mason 				free_extent_buffer(left);
1189e66f709bSChris Mason 			}
1190e66f709bSChris Mason 			return 0;
1191e66f709bSChris Mason 		}
1192925baeddSChris Mason 		btrfs_tree_unlock(left);
11935f39d397SChris Mason 		free_extent_buffer(left);
1194e66f709bSChris Mason 	}
11955f39d397SChris Mason 	right = read_node_slot(root, parent, pslot + 1);
1196e66f709bSChris Mason 
1197e66f709bSChris Mason 	/*
1198e66f709bSChris Mason 	 * then try to empty the right most buffer into the middle
1199e66f709bSChris Mason 	 */
12005f39d397SChris Mason 	if (right) {
120133ade1f8SChris Mason 		u32 right_nr;
1202b4ce94deSChris Mason 
1203925baeddSChris Mason 		btrfs_tree_lock(right);
1204b4ce94deSChris Mason 		btrfs_set_lock_blocking(right);
1205b4ce94deSChris Mason 
12065f39d397SChris Mason 		right_nr = btrfs_header_nritems(right);
120733ade1f8SChris Mason 		if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
120833ade1f8SChris Mason 			wret = 1;
120933ade1f8SChris Mason 		} else {
12105f39d397SChris Mason 			ret = btrfs_cow_block(trans, root, right,
12115f39d397SChris Mason 					      parent, pslot + 1,
12129fa8cfe7SChris Mason 					      &right);
121354aa1f4dSChris Mason 			if (ret)
121454aa1f4dSChris Mason 				wret = 1;
121554aa1f4dSChris Mason 			else {
121633ade1f8SChris Mason 				wret = balance_node_right(trans, root,
12175f39d397SChris Mason 							  right, mid);
121833ade1f8SChris Mason 			}
121954aa1f4dSChris Mason 		}
1220e66f709bSChris Mason 		if (wret < 0)
1221e66f709bSChris Mason 			ret = wret;
1222e66f709bSChris Mason 		if (wret == 0) {
12235f39d397SChris Mason 			struct btrfs_disk_key disk_key;
12245f39d397SChris Mason 
12255f39d397SChris Mason 			btrfs_node_key(right, &disk_key, 0);
12265f39d397SChris Mason 			btrfs_set_node_key(parent, &disk_key, pslot + 1);
12275f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
12285f39d397SChris Mason 
12295f39d397SChris Mason 			if (btrfs_header_nritems(mid) <= orig_slot) {
12305f39d397SChris Mason 				path->nodes[level] = right;
1231e66f709bSChris Mason 				path->slots[level + 1] += 1;
1232e66f709bSChris Mason 				path->slots[level] = orig_slot -
12335f39d397SChris Mason 					btrfs_header_nritems(mid);
1234925baeddSChris Mason 				btrfs_tree_unlock(mid);
12355f39d397SChris Mason 				free_extent_buffer(mid);
1236e66f709bSChris Mason 			} else {
1237925baeddSChris Mason 				btrfs_tree_unlock(right);
12385f39d397SChris Mason 				free_extent_buffer(right);
1239e66f709bSChris Mason 			}
1240e66f709bSChris Mason 			return 0;
1241e66f709bSChris Mason 		}
1242925baeddSChris Mason 		btrfs_tree_unlock(right);
12435f39d397SChris Mason 		free_extent_buffer(right);
1244e66f709bSChris Mason 	}
1245e66f709bSChris Mason 	return 1;
1246e66f709bSChris Mason }
1247e66f709bSChris Mason 
124874123bd7SChris Mason /*
1249d352ac68SChris Mason  * readahead one full node of leaves, finding things that are close
1250d352ac68SChris Mason  * to the block in 'slot', and triggering ra on them.
12513c69faecSChris Mason  */
1252c8c42864SChris Mason static void reada_for_search(struct btrfs_root *root,
1253e02119d5SChris Mason 			     struct btrfs_path *path,
125401f46658SChris Mason 			     int level, int slot, u64 objectid)
12553c69faecSChris Mason {
12565f39d397SChris Mason 	struct extent_buffer *node;
125701f46658SChris Mason 	struct btrfs_disk_key disk_key;
12583c69faecSChris Mason 	u32 nritems;
12593c69faecSChris Mason 	u64 search;
1260a7175319SChris Mason 	u64 target;
12616b80053dSChris Mason 	u64 nread = 0;
1262cb25c2eaSJosef Bacik 	u64 gen;
12633c69faecSChris Mason 	int direction = path->reada;
12645f39d397SChris Mason 	struct extent_buffer *eb;
12656b80053dSChris Mason 	u32 nr;
12666b80053dSChris Mason 	u32 blocksize;
12676b80053dSChris Mason 	u32 nscan = 0;
1268db94535dSChris Mason 
1269a6b6e75eSChris Mason 	if (level != 1)
12703c69faecSChris Mason 		return;
12713c69faecSChris Mason 
12726702ed49SChris Mason 	if (!path->nodes[level])
12736702ed49SChris Mason 		return;
12746702ed49SChris Mason 
12755f39d397SChris Mason 	node = path->nodes[level];
1276925baeddSChris Mason 
12773c69faecSChris Mason 	search = btrfs_node_blockptr(node, slot);
12786b80053dSChris Mason 	blocksize = btrfs_level_size(root, level - 1);
12796b80053dSChris Mason 	eb = btrfs_find_tree_block(root, search, blocksize);
12805f39d397SChris Mason 	if (eb) {
12815f39d397SChris Mason 		free_extent_buffer(eb);
12823c69faecSChris Mason 		return;
12833c69faecSChris Mason 	}
12843c69faecSChris Mason 
1285a7175319SChris Mason 	target = search;
12866b80053dSChris Mason 
12875f39d397SChris Mason 	nritems = btrfs_header_nritems(node);
12886b80053dSChris Mason 	nr = slot;
128925b8b936SJosef Bacik 
12903c69faecSChris Mason 	while (1) {
12916b80053dSChris Mason 		if (direction < 0) {
12926b80053dSChris Mason 			if (nr == 0)
12933c69faecSChris Mason 				break;
12946b80053dSChris Mason 			nr--;
12956b80053dSChris Mason 		} else if (direction > 0) {
12966b80053dSChris Mason 			nr++;
12976b80053dSChris Mason 			if (nr >= nritems)
12986b80053dSChris Mason 				break;
12993c69faecSChris Mason 		}
130001f46658SChris Mason 		if (path->reada < 0 && objectid) {
130101f46658SChris Mason 			btrfs_node_key(node, &disk_key, nr);
130201f46658SChris Mason 			if (btrfs_disk_key_objectid(&disk_key) != objectid)
130301f46658SChris Mason 				break;
130401f46658SChris Mason 		}
13056b80053dSChris Mason 		search = btrfs_node_blockptr(node, nr);
1306a7175319SChris Mason 		if ((search <= target && target - search <= 65536) ||
1307a7175319SChris Mason 		    (search > target && search - target <= 65536)) {
1308cb25c2eaSJosef Bacik 			gen = btrfs_node_ptr_generation(node, nr);
1309cb25c2eaSJosef Bacik 			readahead_tree_block(root, search, blocksize, gen);
13106b80053dSChris Mason 			nread += blocksize;
13113c69faecSChris Mason 		}
13126b80053dSChris Mason 		nscan++;
1313a7175319SChris Mason 		if ((nread > 65536 || nscan > 32))
13146b80053dSChris Mason 			break;
13153c69faecSChris Mason 	}
13163c69faecSChris Mason }
1317925baeddSChris Mason 
1318d352ac68SChris Mason /*
1319b4ce94deSChris Mason  * returns -EAGAIN if it had to drop the path, or zero if everything was in
1320b4ce94deSChris Mason  * cache
1321b4ce94deSChris Mason  */
1322b4ce94deSChris Mason static noinline int reada_for_balance(struct btrfs_root *root,
1323b4ce94deSChris Mason 				      struct btrfs_path *path, int level)
1324b4ce94deSChris Mason {
1325b4ce94deSChris Mason 	int slot;
1326b4ce94deSChris Mason 	int nritems;
1327b4ce94deSChris Mason 	struct extent_buffer *parent;
1328b4ce94deSChris Mason 	struct extent_buffer *eb;
1329b4ce94deSChris Mason 	u64 gen;
1330b4ce94deSChris Mason 	u64 block1 = 0;
1331b4ce94deSChris Mason 	u64 block2 = 0;
1332b4ce94deSChris Mason 	int ret = 0;
1333b4ce94deSChris Mason 	int blocksize;
1334b4ce94deSChris Mason 
13358c594ea8SChris Mason 	parent = path->nodes[level + 1];
1336b4ce94deSChris Mason 	if (!parent)
1337b4ce94deSChris Mason 		return 0;
1338b4ce94deSChris Mason 
1339b4ce94deSChris Mason 	nritems = btrfs_header_nritems(parent);
13408c594ea8SChris Mason 	slot = path->slots[level + 1];
1341b4ce94deSChris Mason 	blocksize = btrfs_level_size(root, level);
1342b4ce94deSChris Mason 
1343b4ce94deSChris Mason 	if (slot > 0) {
1344b4ce94deSChris Mason 		block1 = btrfs_node_blockptr(parent, slot - 1);
1345b4ce94deSChris Mason 		gen = btrfs_node_ptr_generation(parent, slot - 1);
1346b4ce94deSChris Mason 		eb = btrfs_find_tree_block(root, block1, blocksize);
1347b4ce94deSChris Mason 		if (eb && btrfs_buffer_uptodate(eb, gen))
1348b4ce94deSChris Mason 			block1 = 0;
1349b4ce94deSChris Mason 		free_extent_buffer(eb);
1350b4ce94deSChris Mason 	}
13518c594ea8SChris Mason 	if (slot + 1 < nritems) {
1352b4ce94deSChris Mason 		block2 = btrfs_node_blockptr(parent, slot + 1);
1353b4ce94deSChris Mason 		gen = btrfs_node_ptr_generation(parent, slot + 1);
1354b4ce94deSChris Mason 		eb = btrfs_find_tree_block(root, block2, blocksize);
1355b4ce94deSChris Mason 		if (eb && btrfs_buffer_uptodate(eb, gen))
1356b4ce94deSChris Mason 			block2 = 0;
1357b4ce94deSChris Mason 		free_extent_buffer(eb);
1358b4ce94deSChris Mason 	}
1359b4ce94deSChris Mason 	if (block1 || block2) {
1360b4ce94deSChris Mason 		ret = -EAGAIN;
13618c594ea8SChris Mason 
13628c594ea8SChris Mason 		/* release the whole path */
1363b3b4aa74SDavid Sterba 		btrfs_release_path(path);
13648c594ea8SChris Mason 
13658c594ea8SChris Mason 		/* read the blocks */
1366b4ce94deSChris Mason 		if (block1)
1367b4ce94deSChris Mason 			readahead_tree_block(root, block1, blocksize, 0);
1368b4ce94deSChris Mason 		if (block2)
1369b4ce94deSChris Mason 			readahead_tree_block(root, block2, blocksize, 0);
1370b4ce94deSChris Mason 
1371b4ce94deSChris Mason 		if (block1) {
1372b4ce94deSChris Mason 			eb = read_tree_block(root, block1, blocksize, 0);
1373b4ce94deSChris Mason 			free_extent_buffer(eb);
1374b4ce94deSChris Mason 		}
13758c594ea8SChris Mason 		if (block2) {
1376b4ce94deSChris Mason 			eb = read_tree_block(root, block2, blocksize, 0);
1377b4ce94deSChris Mason 			free_extent_buffer(eb);
1378b4ce94deSChris Mason 		}
1379b4ce94deSChris Mason 	}
1380b4ce94deSChris Mason 	return ret;
1381b4ce94deSChris Mason }
1382b4ce94deSChris Mason 
1383b4ce94deSChris Mason 
1384b4ce94deSChris Mason /*
1385d397712bSChris Mason  * when we walk down the tree, it is usually safe to unlock the higher layers
1386d397712bSChris Mason  * in the tree.  The exceptions are when our path goes through slot 0, because
1387d397712bSChris Mason  * operations on the tree might require changing key pointers higher up in the
1388d397712bSChris Mason  * tree.
1389d352ac68SChris Mason  *
1390d397712bSChris Mason  * callers might also have set path->keep_locks, which tells this code to keep
1391d397712bSChris Mason  * the lock if the path points to the last slot in the block.  This is part of
1392d397712bSChris Mason  * walking through the tree, and selecting the next slot in the higher block.
1393d352ac68SChris Mason  *
1394d397712bSChris Mason  * lowest_unlock sets the lowest level in the tree we're allowed to unlock.  so
1395d397712bSChris Mason  * if lowest_unlock is 1, level 0 won't be unlocked
1396d352ac68SChris Mason  */
1397e02119d5SChris Mason static noinline void unlock_up(struct btrfs_path *path, int level,
1398e02119d5SChris Mason 			       int lowest_unlock)
1399925baeddSChris Mason {
1400925baeddSChris Mason 	int i;
1401925baeddSChris Mason 	int skip_level = level;
1402051e1b9fSChris Mason 	int no_skips = 0;
1403925baeddSChris Mason 	struct extent_buffer *t;
1404925baeddSChris Mason 
1405925baeddSChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1406925baeddSChris Mason 		if (!path->nodes[i])
1407925baeddSChris Mason 			break;
1408925baeddSChris Mason 		if (!path->locks[i])
1409925baeddSChris Mason 			break;
1410051e1b9fSChris Mason 		if (!no_skips && path->slots[i] == 0) {
1411925baeddSChris Mason 			skip_level = i + 1;
1412925baeddSChris Mason 			continue;
1413925baeddSChris Mason 		}
1414051e1b9fSChris Mason 		if (!no_skips && path->keep_locks) {
1415925baeddSChris Mason 			u32 nritems;
1416925baeddSChris Mason 			t = path->nodes[i];
1417925baeddSChris Mason 			nritems = btrfs_header_nritems(t);
1418051e1b9fSChris Mason 			if (nritems < 1 || path->slots[i] >= nritems - 1) {
1419925baeddSChris Mason 				skip_level = i + 1;
1420925baeddSChris Mason 				continue;
1421925baeddSChris Mason 			}
1422925baeddSChris Mason 		}
1423051e1b9fSChris Mason 		if (skip_level < i && i >= lowest_unlock)
1424051e1b9fSChris Mason 			no_skips = 1;
1425051e1b9fSChris Mason 
1426925baeddSChris Mason 		t = path->nodes[i];
1427925baeddSChris Mason 		if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1428bd681513SChris Mason 			btrfs_tree_unlock_rw(t, path->locks[i]);
1429925baeddSChris Mason 			path->locks[i] = 0;
1430925baeddSChris Mason 		}
1431925baeddSChris Mason 	}
1432925baeddSChris Mason }
1433925baeddSChris Mason 
14343c69faecSChris Mason /*
1435b4ce94deSChris Mason  * This releases any locks held in the path starting at level and
1436b4ce94deSChris Mason  * going all the way up to the root.
1437b4ce94deSChris Mason  *
1438b4ce94deSChris Mason  * btrfs_search_slot will keep the lock held on higher nodes in a few
1439b4ce94deSChris Mason  * corner cases, such as COW of the block at slot zero in the node.  This
1440b4ce94deSChris Mason  * ignores those rules, and it should only be called when there are no
1441b4ce94deSChris Mason  * more updates to be done higher up in the tree.
1442b4ce94deSChris Mason  */
1443b4ce94deSChris Mason noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1444b4ce94deSChris Mason {
1445b4ce94deSChris Mason 	int i;
1446b4ce94deSChris Mason 
14475d4f98a2SYan Zheng 	if (path->keep_locks)
1448b4ce94deSChris Mason 		return;
1449b4ce94deSChris Mason 
1450b4ce94deSChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1451b4ce94deSChris Mason 		if (!path->nodes[i])
145212f4daccSChris Mason 			continue;
1453b4ce94deSChris Mason 		if (!path->locks[i])
145412f4daccSChris Mason 			continue;
1455bd681513SChris Mason 		btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
1456b4ce94deSChris Mason 		path->locks[i] = 0;
1457b4ce94deSChris Mason 	}
1458b4ce94deSChris Mason }
1459b4ce94deSChris Mason 
1460b4ce94deSChris Mason /*
1461c8c42864SChris Mason  * helper function for btrfs_search_slot.  The goal is to find a block
1462c8c42864SChris Mason  * in cache without setting the path to blocking.  If we find the block
1463c8c42864SChris Mason  * we return zero and the path is unchanged.
1464c8c42864SChris Mason  *
1465c8c42864SChris Mason  * If we can't find the block, we set the path blocking and do some
1466c8c42864SChris Mason  * reada.  -EAGAIN is returned and the search must be repeated.
1467c8c42864SChris Mason  */
1468c8c42864SChris Mason static int
1469c8c42864SChris Mason read_block_for_search(struct btrfs_trans_handle *trans,
1470c8c42864SChris Mason 		       struct btrfs_root *root, struct btrfs_path *p,
1471c8c42864SChris Mason 		       struct extent_buffer **eb_ret, int level, int slot,
1472c8c42864SChris Mason 		       struct btrfs_key *key)
1473c8c42864SChris Mason {
1474c8c42864SChris Mason 	u64 blocknr;
1475c8c42864SChris Mason 	u64 gen;
1476c8c42864SChris Mason 	u32 blocksize;
1477c8c42864SChris Mason 	struct extent_buffer *b = *eb_ret;
1478c8c42864SChris Mason 	struct extent_buffer *tmp;
147976a05b35SChris Mason 	int ret;
1480c8c42864SChris Mason 
1481c8c42864SChris Mason 	blocknr = btrfs_node_blockptr(b, slot);
1482c8c42864SChris Mason 	gen = btrfs_node_ptr_generation(b, slot);
1483c8c42864SChris Mason 	blocksize = btrfs_level_size(root, level - 1);
1484c8c42864SChris Mason 
1485c8c42864SChris Mason 	tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1486cb44921aSChris Mason 	if (tmp) {
1487cb44921aSChris Mason 		if (btrfs_buffer_uptodate(tmp, 0)) {
1488cb44921aSChris Mason 			if (btrfs_buffer_uptodate(tmp, gen)) {
148976a05b35SChris Mason 				/*
1490cb44921aSChris Mason 				 * we found an up to date block without
1491cb44921aSChris Mason 				 * sleeping, return
149276a05b35SChris Mason 				 * right away
149376a05b35SChris Mason 				 */
1494c8c42864SChris Mason 				*eb_ret = tmp;
1495c8c42864SChris Mason 				return 0;
1496c8c42864SChris Mason 			}
1497cb44921aSChris Mason 			/* the pages were up to date, but we failed
1498cb44921aSChris Mason 			 * the generation number check.  Do a full
1499cb44921aSChris Mason 			 * read for the generation number that is correct.
1500cb44921aSChris Mason 			 * We must do this without dropping locks so
1501cb44921aSChris Mason 			 * we can trust our generation number
1502cb44921aSChris Mason 			 */
1503cb44921aSChris Mason 			free_extent_buffer(tmp);
1504bd681513SChris Mason 			btrfs_set_path_blocking(p);
1505bd681513SChris Mason 
1506cb44921aSChris Mason 			tmp = read_tree_block(root, blocknr, blocksize, gen);
1507cb44921aSChris Mason 			if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1508cb44921aSChris Mason 				*eb_ret = tmp;
1509cb44921aSChris Mason 				return 0;
1510cb44921aSChris Mason 			}
1511cb44921aSChris Mason 			free_extent_buffer(tmp);
1512b3b4aa74SDavid Sterba 			btrfs_release_path(p);
1513cb44921aSChris Mason 			return -EIO;
1514cb44921aSChris Mason 		}
1515cb44921aSChris Mason 	}
1516c8c42864SChris Mason 
1517c8c42864SChris Mason 	/*
1518c8c42864SChris Mason 	 * reduce lock contention at high levels
1519c8c42864SChris Mason 	 * of the btree by dropping locks before
152076a05b35SChris Mason 	 * we read.  Don't release the lock on the current
152176a05b35SChris Mason 	 * level because we need to walk this node to figure
152276a05b35SChris Mason 	 * out which blocks to read.
1523c8c42864SChris Mason 	 */
15248c594ea8SChris Mason 	btrfs_unlock_up_safe(p, level + 1);
15258c594ea8SChris Mason 	btrfs_set_path_blocking(p);
15268c594ea8SChris Mason 
1527c8c42864SChris Mason 	free_extent_buffer(tmp);
1528c8c42864SChris Mason 	if (p->reada)
1529c8c42864SChris Mason 		reada_for_search(root, p, level, slot, key->objectid);
1530c8c42864SChris Mason 
1531b3b4aa74SDavid Sterba 	btrfs_release_path(p);
153276a05b35SChris Mason 
153376a05b35SChris Mason 	ret = -EAGAIN;
15345bdd3536SYan, Zheng 	tmp = read_tree_block(root, blocknr, blocksize, 0);
153576a05b35SChris Mason 	if (tmp) {
153676a05b35SChris Mason 		/*
153776a05b35SChris Mason 		 * If the read above didn't mark this buffer up to date,
153876a05b35SChris Mason 		 * it will never end up being up to date.  Set ret to EIO now
153976a05b35SChris Mason 		 * and give up so that our caller doesn't loop forever
154076a05b35SChris Mason 		 * on our EAGAINs.
154176a05b35SChris Mason 		 */
154276a05b35SChris Mason 		if (!btrfs_buffer_uptodate(tmp, 0))
154376a05b35SChris Mason 			ret = -EIO;
1544c8c42864SChris Mason 		free_extent_buffer(tmp);
154576a05b35SChris Mason 	}
154676a05b35SChris Mason 	return ret;
1547c8c42864SChris Mason }
1548c8c42864SChris Mason 
1549c8c42864SChris Mason /*
1550c8c42864SChris Mason  * helper function for btrfs_search_slot.  This does all of the checks
1551c8c42864SChris Mason  * for node-level blocks and does any balancing required based on
1552c8c42864SChris Mason  * the ins_len.
1553c8c42864SChris Mason  *
1554c8c42864SChris Mason  * If no extra work was required, zero is returned.  If we had to
1555c8c42864SChris Mason  * drop the path, -EAGAIN is returned and btrfs_search_slot must
1556c8c42864SChris Mason  * start over
1557c8c42864SChris Mason  */
1558c8c42864SChris Mason static int
1559c8c42864SChris Mason setup_nodes_for_search(struct btrfs_trans_handle *trans,
1560c8c42864SChris Mason 		       struct btrfs_root *root, struct btrfs_path *p,
1561bd681513SChris Mason 		       struct extent_buffer *b, int level, int ins_len,
1562bd681513SChris Mason 		       int *write_lock_level)
1563c8c42864SChris Mason {
1564c8c42864SChris Mason 	int ret;
1565c8c42864SChris Mason 	if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1566c8c42864SChris Mason 	    BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1567c8c42864SChris Mason 		int sret;
1568c8c42864SChris Mason 
1569bd681513SChris Mason 		if (*write_lock_level < level + 1) {
1570bd681513SChris Mason 			*write_lock_level = level + 1;
1571bd681513SChris Mason 			btrfs_release_path(p);
1572bd681513SChris Mason 			goto again;
1573bd681513SChris Mason 		}
1574bd681513SChris Mason 
1575c8c42864SChris Mason 		sret = reada_for_balance(root, p, level);
1576c8c42864SChris Mason 		if (sret)
1577c8c42864SChris Mason 			goto again;
1578c8c42864SChris Mason 
1579c8c42864SChris Mason 		btrfs_set_path_blocking(p);
1580c8c42864SChris Mason 		sret = split_node(trans, root, p, level);
1581bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
1582c8c42864SChris Mason 
1583c8c42864SChris Mason 		BUG_ON(sret > 0);
1584c8c42864SChris Mason 		if (sret) {
1585c8c42864SChris Mason 			ret = sret;
1586c8c42864SChris Mason 			goto done;
1587c8c42864SChris Mason 		}
1588c8c42864SChris Mason 		b = p->nodes[level];
1589c8c42864SChris Mason 	} else if (ins_len < 0 && btrfs_header_nritems(b) <
1590cfbb9308SChris Mason 		   BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
1591c8c42864SChris Mason 		int sret;
1592c8c42864SChris Mason 
1593bd681513SChris Mason 		if (*write_lock_level < level + 1) {
1594bd681513SChris Mason 			*write_lock_level = level + 1;
1595bd681513SChris Mason 			btrfs_release_path(p);
1596bd681513SChris Mason 			goto again;
1597bd681513SChris Mason 		}
1598bd681513SChris Mason 
1599c8c42864SChris Mason 		sret = reada_for_balance(root, p, level);
1600c8c42864SChris Mason 		if (sret)
1601c8c42864SChris Mason 			goto again;
1602c8c42864SChris Mason 
1603c8c42864SChris Mason 		btrfs_set_path_blocking(p);
1604c8c42864SChris Mason 		sret = balance_level(trans, root, p, level);
1605bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
1606c8c42864SChris Mason 
1607c8c42864SChris Mason 		if (sret) {
1608c8c42864SChris Mason 			ret = sret;
1609c8c42864SChris Mason 			goto done;
1610c8c42864SChris Mason 		}
1611c8c42864SChris Mason 		b = p->nodes[level];
1612c8c42864SChris Mason 		if (!b) {
1613b3b4aa74SDavid Sterba 			btrfs_release_path(p);
1614c8c42864SChris Mason 			goto again;
1615c8c42864SChris Mason 		}
1616c8c42864SChris Mason 		BUG_ON(btrfs_header_nritems(b) == 1);
1617c8c42864SChris Mason 	}
1618c8c42864SChris Mason 	return 0;
1619c8c42864SChris Mason 
1620c8c42864SChris Mason again:
1621c8c42864SChris Mason 	ret = -EAGAIN;
1622c8c42864SChris Mason done:
1623c8c42864SChris Mason 	return ret;
1624c8c42864SChris Mason }
1625c8c42864SChris Mason 
1626c8c42864SChris Mason /*
162774123bd7SChris Mason  * look for key in the tree.  path is filled in with nodes along the way
162874123bd7SChris Mason  * if key is found, we return zero and you can find the item in the leaf
162974123bd7SChris Mason  * level of the path (level 0)
163074123bd7SChris Mason  *
163174123bd7SChris Mason  * If the key isn't found, the path points to the slot where it should
1632aa5d6bedSChris Mason  * be inserted, and 1 is returned.  If there are other errors during the
1633aa5d6bedSChris Mason  * search a negative error number is returned.
163497571fd0SChris Mason  *
163597571fd0SChris Mason  * if ins_len > 0, nodes and leaves will be split as we walk down the
163697571fd0SChris Mason  * tree.  if ins_len < 0, nodes will be merged as we walk down the tree (if
163797571fd0SChris Mason  * possible)
163874123bd7SChris Mason  */
1639e089f05cSChris Mason int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1640e089f05cSChris Mason 		      *root, struct btrfs_key *key, struct btrfs_path *p, int
1641e089f05cSChris Mason 		      ins_len, int cow)
1642be0e5c09SChris Mason {
16435f39d397SChris Mason 	struct extent_buffer *b;
1644be0e5c09SChris Mason 	int slot;
1645be0e5c09SChris Mason 	int ret;
164633c66f43SYan Zheng 	int err;
1647be0e5c09SChris Mason 	int level;
1648925baeddSChris Mason 	int lowest_unlock = 1;
1649bd681513SChris Mason 	int root_lock;
1650bd681513SChris Mason 	/* everything at write_lock_level or lower must be write locked */
1651bd681513SChris Mason 	int write_lock_level = 0;
16529f3a7427SChris Mason 	u8 lowest_level = 0;
16539f3a7427SChris Mason 
16546702ed49SChris Mason 	lowest_level = p->lowest_level;
1655323ac95bSChris Mason 	WARN_ON(lowest_level && ins_len > 0);
165622b0ebdaSChris Mason 	WARN_ON(p->nodes[0] != NULL);
165725179201SJosef Bacik 
1658bd681513SChris Mason 	if (ins_len < 0) {
1659925baeddSChris Mason 		lowest_unlock = 2;
166065b51a00SChris Mason 
1661bd681513SChris Mason 		/* when we are removing items, we might have to go up to level
1662bd681513SChris Mason 		 * two as we update tree pointers  Make sure we keep write
1663bd681513SChris Mason 		 * for those levels as well
1664bd681513SChris Mason 		 */
1665bd681513SChris Mason 		write_lock_level = 2;
1666bd681513SChris Mason 	} else if (ins_len > 0) {
1667bd681513SChris Mason 		/*
1668bd681513SChris Mason 		 * for inserting items, make sure we have a write lock on
1669bd681513SChris Mason 		 * level 1 so we can update keys
1670bd681513SChris Mason 		 */
1671bd681513SChris Mason 		write_lock_level = 1;
1672bd681513SChris Mason 	}
1673bd681513SChris Mason 
1674bd681513SChris Mason 	if (!cow)
1675bd681513SChris Mason 		write_lock_level = -1;
1676bd681513SChris Mason 
1677bd681513SChris Mason 	if (cow && (p->keep_locks || p->lowest_level))
1678bd681513SChris Mason 		write_lock_level = BTRFS_MAX_LEVEL;
1679bd681513SChris Mason 
1680bb803951SChris Mason again:
1681bd681513SChris Mason 	/*
1682bd681513SChris Mason 	 * we try very hard to do read locks on the root
1683bd681513SChris Mason 	 */
1684bd681513SChris Mason 	root_lock = BTRFS_READ_LOCK;
1685bd681513SChris Mason 	level = 0;
16865d4f98a2SYan Zheng 	if (p->search_commit_root) {
1687bd681513SChris Mason 		/*
1688bd681513SChris Mason 		 * the commit roots are read only
1689bd681513SChris Mason 		 * so we always do read locks
1690bd681513SChris Mason 		 */
16915d4f98a2SYan Zheng 		b = root->commit_root;
16925d4f98a2SYan Zheng 		extent_buffer_get(b);
1693bd681513SChris Mason 		level = btrfs_header_level(b);
16945d4f98a2SYan Zheng 		if (!p->skip_locking)
1695bd681513SChris Mason 			btrfs_tree_read_lock(b);
16965d4f98a2SYan Zheng 	} else {
1697bd681513SChris Mason 		if (p->skip_locking) {
16985cd57b2cSChris Mason 			b = btrfs_root_node(root);
1699bd681513SChris Mason 			level = btrfs_header_level(b);
1700bd681513SChris Mason 		} else {
1701bd681513SChris Mason 			/* we don't know the level of the root node
1702bd681513SChris Mason 			 * until we actually have it read locked
1703bd681513SChris Mason 			 */
1704bd681513SChris Mason 			b = btrfs_read_lock_root_node(root);
1705bd681513SChris Mason 			level = btrfs_header_level(b);
1706bd681513SChris Mason 			if (level <= write_lock_level) {
1707bd681513SChris Mason 				/* whoops, must trade for write lock */
1708bd681513SChris Mason 				btrfs_tree_read_unlock(b);
1709bd681513SChris Mason 				free_extent_buffer(b);
1710925baeddSChris Mason 				b = btrfs_lock_root_node(root);
1711bd681513SChris Mason 				root_lock = BTRFS_WRITE_LOCK;
1712bd681513SChris Mason 
1713bd681513SChris Mason 				/* the level might have changed, check again */
1714bd681513SChris Mason 				level = btrfs_header_level(b);
17155d4f98a2SYan Zheng 			}
1716bd681513SChris Mason 		}
1717bd681513SChris Mason 	}
1718bd681513SChris Mason 	p->nodes[level] = b;
1719bd681513SChris Mason 	if (!p->skip_locking)
1720bd681513SChris Mason 		p->locks[level] = root_lock;
1721925baeddSChris Mason 
1722eb60ceacSChris Mason 	while (b) {
17235f39d397SChris Mason 		level = btrfs_header_level(b);
172465b51a00SChris Mason 
172565b51a00SChris Mason 		/*
172665b51a00SChris Mason 		 * setup the path here so we can release it under lock
172765b51a00SChris Mason 		 * contention with the cow code
172865b51a00SChris Mason 		 */
172902217ed2SChris Mason 		if (cow) {
1730c8c42864SChris Mason 			/*
1731c8c42864SChris Mason 			 * if we don't really need to cow this block
1732c8c42864SChris Mason 			 * then we don't want to set the path blocking,
1733c8c42864SChris Mason 			 * so we test it here
1734c8c42864SChris Mason 			 */
17355d4f98a2SYan Zheng 			if (!should_cow_block(trans, root, b))
173665b51a00SChris Mason 				goto cow_done;
17375d4f98a2SYan Zheng 
1738b4ce94deSChris Mason 			btrfs_set_path_blocking(p);
1739b4ce94deSChris Mason 
1740bd681513SChris Mason 			/*
1741bd681513SChris Mason 			 * must have write locks on this node and the
1742bd681513SChris Mason 			 * parent
1743bd681513SChris Mason 			 */
1744bd681513SChris Mason 			if (level + 1 > write_lock_level) {
1745bd681513SChris Mason 				write_lock_level = level + 1;
1746bd681513SChris Mason 				btrfs_release_path(p);
1747bd681513SChris Mason 				goto again;
1748bd681513SChris Mason 			}
1749bd681513SChris Mason 
175033c66f43SYan Zheng 			err = btrfs_cow_block(trans, root, b,
1751e20d96d6SChris Mason 					      p->nodes[level + 1],
17529fa8cfe7SChris Mason 					      p->slots[level + 1], &b);
175333c66f43SYan Zheng 			if (err) {
175433c66f43SYan Zheng 				ret = err;
175565b51a00SChris Mason 				goto done;
175654aa1f4dSChris Mason 			}
175702217ed2SChris Mason 		}
175865b51a00SChris Mason cow_done:
175902217ed2SChris Mason 		BUG_ON(!cow && ins_len);
176065b51a00SChris Mason 
1761eb60ceacSChris Mason 		p->nodes[level] = b;
1762bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
1763b4ce94deSChris Mason 
1764b4ce94deSChris Mason 		/*
1765b4ce94deSChris Mason 		 * we have a lock on b and as long as we aren't changing
1766b4ce94deSChris Mason 		 * the tree, there is no way to for the items in b to change.
1767b4ce94deSChris Mason 		 * It is safe to drop the lock on our parent before we
1768b4ce94deSChris Mason 		 * go through the expensive btree search on b.
1769b4ce94deSChris Mason 		 *
1770b4ce94deSChris Mason 		 * If cow is true, then we might be changing slot zero,
1771b4ce94deSChris Mason 		 * which may require changing the parent.  So, we can't
1772b4ce94deSChris Mason 		 * drop the lock until after we know which slot we're
1773b4ce94deSChris Mason 		 * operating on.
1774b4ce94deSChris Mason 		 */
1775b4ce94deSChris Mason 		if (!cow)
1776b4ce94deSChris Mason 			btrfs_unlock_up_safe(p, level + 1);
1777b4ce94deSChris Mason 
17785f39d397SChris Mason 		ret = bin_search(b, key, level, &slot);
1779b4ce94deSChris Mason 
17805f39d397SChris Mason 		if (level != 0) {
178133c66f43SYan Zheng 			int dec = 0;
178233c66f43SYan Zheng 			if (ret && slot > 0) {
178333c66f43SYan Zheng 				dec = 1;
1784be0e5c09SChris Mason 				slot -= 1;
178533c66f43SYan Zheng 			}
1786be0e5c09SChris Mason 			p->slots[level] = slot;
178733c66f43SYan Zheng 			err = setup_nodes_for_search(trans, root, p, b, level,
1788bd681513SChris Mason 					     ins_len, &write_lock_level);
178933c66f43SYan Zheng 			if (err == -EAGAIN)
1790b4ce94deSChris Mason 				goto again;
179133c66f43SYan Zheng 			if (err) {
179233c66f43SYan Zheng 				ret = err;
179365b51a00SChris Mason 				goto done;
179433c66f43SYan Zheng 			}
17955c680ed6SChris Mason 			b = p->nodes[level];
17965c680ed6SChris Mason 			slot = p->slots[level];
1797b4ce94deSChris Mason 
1798bd681513SChris Mason 			/*
1799bd681513SChris Mason 			 * slot 0 is special, if we change the key
1800bd681513SChris Mason 			 * we have to update the parent pointer
1801bd681513SChris Mason 			 * which means we must have a write lock
1802bd681513SChris Mason 			 * on the parent
1803bd681513SChris Mason 			 */
1804bd681513SChris Mason 			if (slot == 0 && cow &&
1805bd681513SChris Mason 			    write_lock_level < level + 1) {
1806bd681513SChris Mason 				write_lock_level = level + 1;
1807bd681513SChris Mason 				btrfs_release_path(p);
1808bd681513SChris Mason 				goto again;
1809bd681513SChris Mason 			}
1810bd681513SChris Mason 
1811f9efa9c7SChris Mason 			unlock_up(p, level, lowest_unlock);
1812f9efa9c7SChris Mason 
1813925baeddSChris Mason 			if (level == lowest_level) {
181433c66f43SYan Zheng 				if (dec)
181533c66f43SYan Zheng 					p->slots[level]++;
18165b21f2edSZheng Yan 				goto done;
1817925baeddSChris Mason 			}
1818ca7a79adSChris Mason 
181933c66f43SYan Zheng 			err = read_block_for_search(trans, root, p,
1820c8c42864SChris Mason 						    &b, level, slot, key);
182133c66f43SYan Zheng 			if (err == -EAGAIN)
1822051e1b9fSChris Mason 				goto again;
182333c66f43SYan Zheng 			if (err) {
182433c66f43SYan Zheng 				ret = err;
182576a05b35SChris Mason 				goto done;
182633c66f43SYan Zheng 			}
182776a05b35SChris Mason 
1828b4ce94deSChris Mason 			if (!p->skip_locking) {
1829bd681513SChris Mason 				level = btrfs_header_level(b);
1830bd681513SChris Mason 				if (level <= write_lock_level) {
1831bd681513SChris Mason 					err = btrfs_try_tree_write_lock(b);
183233c66f43SYan Zheng 					if (!err) {
1833b4ce94deSChris Mason 						btrfs_set_path_blocking(p);
1834925baeddSChris Mason 						btrfs_tree_lock(b);
1835bd681513SChris Mason 						btrfs_clear_path_blocking(p, b,
1836bd681513SChris Mason 								  BTRFS_WRITE_LOCK);
1837b4ce94deSChris Mason 					}
1838bd681513SChris Mason 					p->locks[level] = BTRFS_WRITE_LOCK;
1839bd681513SChris Mason 				} else {
1840bd681513SChris Mason 					err = btrfs_try_tree_read_lock(b);
1841bd681513SChris Mason 					if (!err) {
1842bd681513SChris Mason 						btrfs_set_path_blocking(p);
1843bd681513SChris Mason 						btrfs_tree_read_lock(b);
1844bd681513SChris Mason 						btrfs_clear_path_blocking(p, b,
1845bd681513SChris Mason 								  BTRFS_READ_LOCK);
1846bd681513SChris Mason 					}
1847bd681513SChris Mason 					p->locks[level] = BTRFS_READ_LOCK;
1848bd681513SChris Mason 				}
1849bd681513SChris Mason 				p->nodes[level] = b;
1850b4ce94deSChris Mason 			}
1851be0e5c09SChris Mason 		} else {
1852be0e5c09SChris Mason 			p->slots[level] = slot;
185387b29b20SYan Zheng 			if (ins_len > 0 &&
185487b29b20SYan Zheng 			    btrfs_leaf_free_space(root, b) < ins_len) {
1855bd681513SChris Mason 				if (write_lock_level < 1) {
1856bd681513SChris Mason 					write_lock_level = 1;
1857bd681513SChris Mason 					btrfs_release_path(p);
1858bd681513SChris Mason 					goto again;
1859bd681513SChris Mason 				}
1860bd681513SChris Mason 
1861b4ce94deSChris Mason 				btrfs_set_path_blocking(p);
186233c66f43SYan Zheng 				err = split_leaf(trans, root, key,
1863cc0c5538SChris Mason 						 p, ins_len, ret == 0);
1864bd681513SChris Mason 				btrfs_clear_path_blocking(p, NULL, 0);
1865b4ce94deSChris Mason 
186633c66f43SYan Zheng 				BUG_ON(err > 0);
186733c66f43SYan Zheng 				if (err) {
186833c66f43SYan Zheng 					ret = err;
186965b51a00SChris Mason 					goto done;
187065b51a00SChris Mason 				}
18715c680ed6SChris Mason 			}
1872459931ecSChris Mason 			if (!p->search_for_split)
1873925baeddSChris Mason 				unlock_up(p, level, lowest_unlock);
187465b51a00SChris Mason 			goto done;
187565b51a00SChris Mason 		}
187665b51a00SChris Mason 	}
187765b51a00SChris Mason 	ret = 1;
187865b51a00SChris Mason done:
1879b4ce94deSChris Mason 	/*
1880b4ce94deSChris Mason 	 * we don't really know what they plan on doing with the path
1881b4ce94deSChris Mason 	 * from here on, so for now just mark it as blocking
1882b4ce94deSChris Mason 	 */
1883b9473439SChris Mason 	if (!p->leave_spinning)
1884b4ce94deSChris Mason 		btrfs_set_path_blocking(p);
188576a05b35SChris Mason 	if (ret < 0)
1886b3b4aa74SDavid Sterba 		btrfs_release_path(p);
1887be0e5c09SChris Mason 	return ret;
1888be0e5c09SChris Mason }
1889be0e5c09SChris Mason 
189074123bd7SChris Mason /*
189174123bd7SChris Mason  * adjust the pointers going up the tree, starting at level
189274123bd7SChris Mason  * making sure the right key of each node is points to 'key'.
189374123bd7SChris Mason  * This is used after shifting pointers to the left, so it stops
189474123bd7SChris Mason  * fixing up pointers when a given leaf/node is not in slot 0 of the
189574123bd7SChris Mason  * higher levels
1896aa5d6bedSChris Mason  *
1897aa5d6bedSChris Mason  * If this fails to write a tree block, it returns -1, but continues
1898aa5d6bedSChris Mason  * fixing up the blocks in ram so the tree is consistent.
189974123bd7SChris Mason  */
19005f39d397SChris Mason static int fixup_low_keys(struct btrfs_trans_handle *trans,
19015f39d397SChris Mason 			  struct btrfs_root *root, struct btrfs_path *path,
19025f39d397SChris Mason 			  struct btrfs_disk_key *key, int level)
1903be0e5c09SChris Mason {
1904be0e5c09SChris Mason 	int i;
1905aa5d6bedSChris Mason 	int ret = 0;
19065f39d397SChris Mason 	struct extent_buffer *t;
19075f39d397SChris Mason 
1908234b63a0SChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1909be0e5c09SChris Mason 		int tslot = path->slots[i];
1910eb60ceacSChris Mason 		if (!path->nodes[i])
1911be0e5c09SChris Mason 			break;
19125f39d397SChris Mason 		t = path->nodes[i];
19135f39d397SChris Mason 		btrfs_set_node_key(t, key, tslot);
1914d6025579SChris Mason 		btrfs_mark_buffer_dirty(path->nodes[i]);
1915be0e5c09SChris Mason 		if (tslot != 0)
1916be0e5c09SChris Mason 			break;
1917be0e5c09SChris Mason 	}
1918aa5d6bedSChris Mason 	return ret;
1919be0e5c09SChris Mason }
1920be0e5c09SChris Mason 
192174123bd7SChris Mason /*
192231840ae1SZheng Yan  * update item key.
192331840ae1SZheng Yan  *
192431840ae1SZheng Yan  * This function isn't completely safe. It's the caller's responsibility
192531840ae1SZheng Yan  * that the new key won't break the order
192631840ae1SZheng Yan  */
192731840ae1SZheng Yan int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
192831840ae1SZheng Yan 			    struct btrfs_root *root, struct btrfs_path *path,
192931840ae1SZheng Yan 			    struct btrfs_key *new_key)
193031840ae1SZheng Yan {
193131840ae1SZheng Yan 	struct btrfs_disk_key disk_key;
193231840ae1SZheng Yan 	struct extent_buffer *eb;
193331840ae1SZheng Yan 	int slot;
193431840ae1SZheng Yan 
193531840ae1SZheng Yan 	eb = path->nodes[0];
193631840ae1SZheng Yan 	slot = path->slots[0];
193731840ae1SZheng Yan 	if (slot > 0) {
193831840ae1SZheng Yan 		btrfs_item_key(eb, &disk_key, slot - 1);
193931840ae1SZheng Yan 		if (comp_keys(&disk_key, new_key) >= 0)
194031840ae1SZheng Yan 			return -1;
194131840ae1SZheng Yan 	}
194231840ae1SZheng Yan 	if (slot < btrfs_header_nritems(eb) - 1) {
194331840ae1SZheng Yan 		btrfs_item_key(eb, &disk_key, slot + 1);
194431840ae1SZheng Yan 		if (comp_keys(&disk_key, new_key) <= 0)
194531840ae1SZheng Yan 			return -1;
194631840ae1SZheng Yan 	}
194731840ae1SZheng Yan 
194831840ae1SZheng Yan 	btrfs_cpu_key_to_disk(&disk_key, new_key);
194931840ae1SZheng Yan 	btrfs_set_item_key(eb, &disk_key, slot);
195031840ae1SZheng Yan 	btrfs_mark_buffer_dirty(eb);
195131840ae1SZheng Yan 	if (slot == 0)
195231840ae1SZheng Yan 		fixup_low_keys(trans, root, path, &disk_key, 1);
195331840ae1SZheng Yan 	return 0;
195431840ae1SZheng Yan }
195531840ae1SZheng Yan 
195631840ae1SZheng Yan /*
195774123bd7SChris Mason  * try to push data from one node into the next node left in the
195879f95c82SChris Mason  * tree.
1959aa5d6bedSChris Mason  *
1960aa5d6bedSChris Mason  * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1961aa5d6bedSChris Mason  * error, and > 0 if there was no room in the left hand block.
196274123bd7SChris Mason  */
196398ed5174SChris Mason static int push_node_left(struct btrfs_trans_handle *trans,
196498ed5174SChris Mason 			  struct btrfs_root *root, struct extent_buffer *dst,
1965971a1f66SChris Mason 			  struct extent_buffer *src, int empty)
1966be0e5c09SChris Mason {
1967be0e5c09SChris Mason 	int push_items = 0;
1968bb803951SChris Mason 	int src_nritems;
1969bb803951SChris Mason 	int dst_nritems;
1970aa5d6bedSChris Mason 	int ret = 0;
1971be0e5c09SChris Mason 
19725f39d397SChris Mason 	src_nritems = btrfs_header_nritems(src);
19735f39d397SChris Mason 	dst_nritems = btrfs_header_nritems(dst);
1974123abc88SChris Mason 	push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
19757bb86316SChris Mason 	WARN_ON(btrfs_header_generation(src) != trans->transid);
19767bb86316SChris Mason 	WARN_ON(btrfs_header_generation(dst) != trans->transid);
197754aa1f4dSChris Mason 
1978bce4eae9SChris Mason 	if (!empty && src_nritems <= 8)
1979971a1f66SChris Mason 		return 1;
1980971a1f66SChris Mason 
1981d397712bSChris Mason 	if (push_items <= 0)
1982be0e5c09SChris Mason 		return 1;
1983be0e5c09SChris Mason 
1984bce4eae9SChris Mason 	if (empty) {
1985971a1f66SChris Mason 		push_items = min(src_nritems, push_items);
1986bce4eae9SChris Mason 		if (push_items < src_nritems) {
1987bce4eae9SChris Mason 			/* leave at least 8 pointers in the node if
1988bce4eae9SChris Mason 			 * we aren't going to empty it
1989bce4eae9SChris Mason 			 */
1990bce4eae9SChris Mason 			if (src_nritems - push_items < 8) {
1991bce4eae9SChris Mason 				if (push_items <= 8)
1992bce4eae9SChris Mason 					return 1;
1993bce4eae9SChris Mason 				push_items -= 8;
1994bce4eae9SChris Mason 			}
1995bce4eae9SChris Mason 		}
1996bce4eae9SChris Mason 	} else
1997bce4eae9SChris Mason 		push_items = min(src_nritems - 8, push_items);
199879f95c82SChris Mason 
19995f39d397SChris Mason 	copy_extent_buffer(dst, src,
20005f39d397SChris Mason 			   btrfs_node_key_ptr_offset(dst_nritems),
20015f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
2002123abc88SChris Mason 			   push_items * sizeof(struct btrfs_key_ptr));
20035f39d397SChris Mason 
2004bb803951SChris Mason 	if (push_items < src_nritems) {
20055f39d397SChris Mason 		memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
20065f39d397SChris Mason 				      btrfs_node_key_ptr_offset(push_items),
2007e2fa7227SChris Mason 				      (src_nritems - push_items) *
2008123abc88SChris Mason 				      sizeof(struct btrfs_key_ptr));
2009bb803951SChris Mason 	}
20105f39d397SChris Mason 	btrfs_set_header_nritems(src, src_nritems - push_items);
20115f39d397SChris Mason 	btrfs_set_header_nritems(dst, dst_nritems + push_items);
20125f39d397SChris Mason 	btrfs_mark_buffer_dirty(src);
20135f39d397SChris Mason 	btrfs_mark_buffer_dirty(dst);
201431840ae1SZheng Yan 
2015bb803951SChris Mason 	return ret;
2016be0e5c09SChris Mason }
2017be0e5c09SChris Mason 
201897571fd0SChris Mason /*
201979f95c82SChris Mason  * try to push data from one node into the next node right in the
202079f95c82SChris Mason  * tree.
202179f95c82SChris Mason  *
202279f95c82SChris Mason  * returns 0 if some ptrs were pushed, < 0 if there was some horrible
202379f95c82SChris Mason  * error, and > 0 if there was no room in the right hand block.
202479f95c82SChris Mason  *
202579f95c82SChris Mason  * this will  only push up to 1/2 the contents of the left node over
202679f95c82SChris Mason  */
20275f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans,
20285f39d397SChris Mason 			      struct btrfs_root *root,
20295f39d397SChris Mason 			      struct extent_buffer *dst,
20305f39d397SChris Mason 			      struct extent_buffer *src)
203179f95c82SChris Mason {
203279f95c82SChris Mason 	int push_items = 0;
203379f95c82SChris Mason 	int max_push;
203479f95c82SChris Mason 	int src_nritems;
203579f95c82SChris Mason 	int dst_nritems;
203679f95c82SChris Mason 	int ret = 0;
203779f95c82SChris Mason 
20387bb86316SChris Mason 	WARN_ON(btrfs_header_generation(src) != trans->transid);
20397bb86316SChris Mason 	WARN_ON(btrfs_header_generation(dst) != trans->transid);
20407bb86316SChris Mason 
20415f39d397SChris Mason 	src_nritems = btrfs_header_nritems(src);
20425f39d397SChris Mason 	dst_nritems = btrfs_header_nritems(dst);
2043123abc88SChris Mason 	push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
2044d397712bSChris Mason 	if (push_items <= 0)
204579f95c82SChris Mason 		return 1;
2046bce4eae9SChris Mason 
2047d397712bSChris Mason 	if (src_nritems < 4)
2048bce4eae9SChris Mason 		return 1;
204979f95c82SChris Mason 
205079f95c82SChris Mason 	max_push = src_nritems / 2 + 1;
205179f95c82SChris Mason 	/* don't try to empty the node */
2052d397712bSChris Mason 	if (max_push >= src_nritems)
205379f95c82SChris Mason 		return 1;
2054252c38f0SYan 
205579f95c82SChris Mason 	if (max_push < push_items)
205679f95c82SChris Mason 		push_items = max_push;
205779f95c82SChris Mason 
20585f39d397SChris Mason 	memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
20595f39d397SChris Mason 				      btrfs_node_key_ptr_offset(0),
20605f39d397SChris Mason 				      (dst_nritems) *
20615f39d397SChris Mason 				      sizeof(struct btrfs_key_ptr));
2062d6025579SChris Mason 
20635f39d397SChris Mason 	copy_extent_buffer(dst, src,
20645f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
20655f39d397SChris Mason 			   btrfs_node_key_ptr_offset(src_nritems - push_items),
2066123abc88SChris Mason 			   push_items * sizeof(struct btrfs_key_ptr));
206779f95c82SChris Mason 
20685f39d397SChris Mason 	btrfs_set_header_nritems(src, src_nritems - push_items);
20695f39d397SChris Mason 	btrfs_set_header_nritems(dst, dst_nritems + push_items);
207079f95c82SChris Mason 
20715f39d397SChris Mason 	btrfs_mark_buffer_dirty(src);
20725f39d397SChris Mason 	btrfs_mark_buffer_dirty(dst);
207331840ae1SZheng Yan 
207479f95c82SChris Mason 	return ret;
207579f95c82SChris Mason }
207679f95c82SChris Mason 
207779f95c82SChris Mason /*
207897571fd0SChris Mason  * helper function to insert a new root level in the tree.
207997571fd0SChris Mason  * A new node is allocated, and a single item is inserted to
208097571fd0SChris Mason  * point to the existing root
2081aa5d6bedSChris Mason  *
2082aa5d6bedSChris Mason  * returns zero on success or < 0 on failure.
208397571fd0SChris Mason  */
2084d397712bSChris Mason static noinline int insert_new_root(struct btrfs_trans_handle *trans,
20855f39d397SChris Mason 			   struct btrfs_root *root,
20865f39d397SChris Mason 			   struct btrfs_path *path, int level)
208774123bd7SChris Mason {
20887bb86316SChris Mason 	u64 lower_gen;
20895f39d397SChris Mason 	struct extent_buffer *lower;
20905f39d397SChris Mason 	struct extent_buffer *c;
2091925baeddSChris Mason 	struct extent_buffer *old;
20925f39d397SChris Mason 	struct btrfs_disk_key lower_key;
20935c680ed6SChris Mason 
20945c680ed6SChris Mason 	BUG_ON(path->nodes[level]);
20955c680ed6SChris Mason 	BUG_ON(path->nodes[level-1] != root->node);
20965c680ed6SChris Mason 
20977bb86316SChris Mason 	lower = path->nodes[level-1];
20987bb86316SChris Mason 	if (level == 1)
20997bb86316SChris Mason 		btrfs_item_key(lower, &lower_key, 0);
21007bb86316SChris Mason 	else
21017bb86316SChris Mason 		btrfs_node_key(lower, &lower_key, 0);
21027bb86316SChris Mason 
210331840ae1SZheng Yan 	c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
21045d4f98a2SYan Zheng 				   root->root_key.objectid, &lower_key,
210566d7e7f0SArne Jansen 				   level, root->node->start, 0, 0);
21065f39d397SChris Mason 	if (IS_ERR(c))
21075f39d397SChris Mason 		return PTR_ERR(c);
2108925baeddSChris Mason 
2109f0486c68SYan, Zheng 	root_add_used(root, root->nodesize);
2110f0486c68SYan, Zheng 
21115d4f98a2SYan Zheng 	memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
21125f39d397SChris Mason 	btrfs_set_header_nritems(c, 1);
21135f39d397SChris Mason 	btrfs_set_header_level(c, level);
2114db94535dSChris Mason 	btrfs_set_header_bytenr(c, c->start);
21155f39d397SChris Mason 	btrfs_set_header_generation(c, trans->transid);
21165d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
21175f39d397SChris Mason 	btrfs_set_header_owner(c, root->root_key.objectid);
2118d5719762SChris Mason 
21195f39d397SChris Mason 	write_extent_buffer(c, root->fs_info->fsid,
21205f39d397SChris Mason 			    (unsigned long)btrfs_header_fsid(c),
21215f39d397SChris Mason 			    BTRFS_FSID_SIZE);
2122e17cade2SChris Mason 
2123e17cade2SChris Mason 	write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2124e17cade2SChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(c),
2125e17cade2SChris Mason 			    BTRFS_UUID_SIZE);
2126e17cade2SChris Mason 
21275f39d397SChris Mason 	btrfs_set_node_key(c, &lower_key, 0);
2128db94535dSChris Mason 	btrfs_set_node_blockptr(c, 0, lower->start);
21297bb86316SChris Mason 	lower_gen = btrfs_header_generation(lower);
213031840ae1SZheng Yan 	WARN_ON(lower_gen != trans->transid);
21317bb86316SChris Mason 
21327bb86316SChris Mason 	btrfs_set_node_ptr_generation(c, 0, lower_gen);
21335f39d397SChris Mason 
21345f39d397SChris Mason 	btrfs_mark_buffer_dirty(c);
2135d5719762SChris Mason 
2136925baeddSChris Mason 	old = root->node;
2137240f62c8SChris Mason 	rcu_assign_pointer(root->node, c);
2138925baeddSChris Mason 
2139925baeddSChris Mason 	/* the super has an extra ref to root->node */
2140925baeddSChris Mason 	free_extent_buffer(old);
2141925baeddSChris Mason 
21420b86a832SChris Mason 	add_root_to_dirty_list(root);
21435f39d397SChris Mason 	extent_buffer_get(c);
21445f39d397SChris Mason 	path->nodes[level] = c;
2145bd681513SChris Mason 	path->locks[level] = BTRFS_WRITE_LOCK;
214674123bd7SChris Mason 	path->slots[level] = 0;
214774123bd7SChris Mason 	return 0;
214874123bd7SChris Mason }
21495c680ed6SChris Mason 
21505c680ed6SChris Mason /*
21515c680ed6SChris Mason  * worker function to insert a single pointer in a node.
21525c680ed6SChris Mason  * the node should have enough room for the pointer already
215397571fd0SChris Mason  *
21545c680ed6SChris Mason  * slot and level indicate where you want the key to go, and
21555c680ed6SChris Mason  * blocknr is the block the key points to.
2156aa5d6bedSChris Mason  *
2157aa5d6bedSChris Mason  * returns zero on success and < 0 on any error
21585c680ed6SChris Mason  */
2159e089f05cSChris Mason static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2160e089f05cSChris Mason 		      *root, struct btrfs_path *path, struct btrfs_disk_key
2161db94535dSChris Mason 		      *key, u64 bytenr, int slot, int level)
21625c680ed6SChris Mason {
21635f39d397SChris Mason 	struct extent_buffer *lower;
21645c680ed6SChris Mason 	int nritems;
21655c680ed6SChris Mason 
21665c680ed6SChris Mason 	BUG_ON(!path->nodes[level]);
2167f0486c68SYan, Zheng 	btrfs_assert_tree_locked(path->nodes[level]);
21685f39d397SChris Mason 	lower = path->nodes[level];
21695f39d397SChris Mason 	nritems = btrfs_header_nritems(lower);
2170c293498bSStoyan Gaydarov 	BUG_ON(slot > nritems);
2171123abc88SChris Mason 	if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
217274123bd7SChris Mason 		BUG();
217374123bd7SChris Mason 	if (slot != nritems) {
21745f39d397SChris Mason 		memmove_extent_buffer(lower,
21755f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot + 1),
21765f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot),
2177123abc88SChris Mason 			      (nritems - slot) * sizeof(struct btrfs_key_ptr));
217874123bd7SChris Mason 	}
21795f39d397SChris Mason 	btrfs_set_node_key(lower, key, slot);
2180db94535dSChris Mason 	btrfs_set_node_blockptr(lower, slot, bytenr);
218174493f7aSChris Mason 	WARN_ON(trans->transid == 0);
218274493f7aSChris Mason 	btrfs_set_node_ptr_generation(lower, slot, trans->transid);
21835f39d397SChris Mason 	btrfs_set_header_nritems(lower, nritems + 1);
21845f39d397SChris Mason 	btrfs_mark_buffer_dirty(lower);
218574123bd7SChris Mason 	return 0;
218674123bd7SChris Mason }
218774123bd7SChris Mason 
218897571fd0SChris Mason /*
218997571fd0SChris Mason  * split the node at the specified level in path in two.
219097571fd0SChris Mason  * The path is corrected to point to the appropriate node after the split
219197571fd0SChris Mason  *
219297571fd0SChris Mason  * Before splitting this tries to make some room in the node by pushing
219397571fd0SChris Mason  * left and right, if either one works, it returns right away.
2194aa5d6bedSChris Mason  *
2195aa5d6bedSChris Mason  * returns 0 on success and < 0 on failure
219697571fd0SChris Mason  */
2197e02119d5SChris Mason static noinline int split_node(struct btrfs_trans_handle *trans,
2198e02119d5SChris Mason 			       struct btrfs_root *root,
2199e02119d5SChris Mason 			       struct btrfs_path *path, int level)
2200be0e5c09SChris Mason {
22015f39d397SChris Mason 	struct extent_buffer *c;
22025f39d397SChris Mason 	struct extent_buffer *split;
22035f39d397SChris Mason 	struct btrfs_disk_key disk_key;
2204be0e5c09SChris Mason 	int mid;
22055c680ed6SChris Mason 	int ret;
2206aa5d6bedSChris Mason 	int wret;
22077518a238SChris Mason 	u32 c_nritems;
2208be0e5c09SChris Mason 
22095f39d397SChris Mason 	c = path->nodes[level];
22107bb86316SChris Mason 	WARN_ON(btrfs_header_generation(c) != trans->transid);
22115f39d397SChris Mason 	if (c == root->node) {
22125c680ed6SChris Mason 		/* trying to split the root, lets make a new one */
2213e089f05cSChris Mason 		ret = insert_new_root(trans, root, path, level + 1);
22145c680ed6SChris Mason 		if (ret)
22155c680ed6SChris Mason 			return ret;
2216b3612421SChris Mason 	} else {
2217e66f709bSChris Mason 		ret = push_nodes_for_insert(trans, root, path, level);
22185f39d397SChris Mason 		c = path->nodes[level];
22195f39d397SChris Mason 		if (!ret && btrfs_header_nritems(c) <
2220c448acf0SChris Mason 		    BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
2221e66f709bSChris Mason 			return 0;
222254aa1f4dSChris Mason 		if (ret < 0)
222354aa1f4dSChris Mason 			return ret;
22245c680ed6SChris Mason 	}
2225e66f709bSChris Mason 
22265f39d397SChris Mason 	c_nritems = btrfs_header_nritems(c);
22275d4f98a2SYan Zheng 	mid = (c_nritems + 1) / 2;
22285d4f98a2SYan Zheng 	btrfs_node_key(c, &disk_key, mid);
22297bb86316SChris Mason 
22305d4f98a2SYan Zheng 	split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
22317bb86316SChris Mason 					root->root_key.objectid,
223266d7e7f0SArne Jansen 					&disk_key, level, c->start, 0, 0);
22335f39d397SChris Mason 	if (IS_ERR(split))
22345f39d397SChris Mason 		return PTR_ERR(split);
223554aa1f4dSChris Mason 
2236f0486c68SYan, Zheng 	root_add_used(root, root->nodesize);
2237f0486c68SYan, Zheng 
22385d4f98a2SYan Zheng 	memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
22395f39d397SChris Mason 	btrfs_set_header_level(split, btrfs_header_level(c));
2240db94535dSChris Mason 	btrfs_set_header_bytenr(split, split->start);
22415f39d397SChris Mason 	btrfs_set_header_generation(split, trans->transid);
22425d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
22435f39d397SChris Mason 	btrfs_set_header_owner(split, root->root_key.objectid);
22445f39d397SChris Mason 	write_extent_buffer(split, root->fs_info->fsid,
22455f39d397SChris Mason 			    (unsigned long)btrfs_header_fsid(split),
22465f39d397SChris Mason 			    BTRFS_FSID_SIZE);
2247e17cade2SChris Mason 	write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2248e17cade2SChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(split),
2249e17cade2SChris Mason 			    BTRFS_UUID_SIZE);
22505f39d397SChris Mason 
22515f39d397SChris Mason 
22525f39d397SChris Mason 	copy_extent_buffer(split, c,
22535f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
22545f39d397SChris Mason 			   btrfs_node_key_ptr_offset(mid),
2255123abc88SChris Mason 			   (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
22565f39d397SChris Mason 	btrfs_set_header_nritems(split, c_nritems - mid);
22575f39d397SChris Mason 	btrfs_set_header_nritems(c, mid);
2258aa5d6bedSChris Mason 	ret = 0;
2259aa5d6bedSChris Mason 
22605f39d397SChris Mason 	btrfs_mark_buffer_dirty(c);
22615f39d397SChris Mason 	btrfs_mark_buffer_dirty(split);
22625f39d397SChris Mason 
2263db94535dSChris Mason 	wret = insert_ptr(trans, root, path, &disk_key, split->start,
22645f39d397SChris Mason 			  path->slots[level + 1] + 1,
2265123abc88SChris Mason 			  level + 1);
2266aa5d6bedSChris Mason 	if (wret)
2267aa5d6bedSChris Mason 		ret = wret;
2268aa5d6bedSChris Mason 
22695de08d7dSChris Mason 	if (path->slots[level] >= mid) {
22705c680ed6SChris Mason 		path->slots[level] -= mid;
2271925baeddSChris Mason 		btrfs_tree_unlock(c);
22725f39d397SChris Mason 		free_extent_buffer(c);
22735f39d397SChris Mason 		path->nodes[level] = split;
22745c680ed6SChris Mason 		path->slots[level + 1] += 1;
2275eb60ceacSChris Mason 	} else {
2276925baeddSChris Mason 		btrfs_tree_unlock(split);
22775f39d397SChris Mason 		free_extent_buffer(split);
2278be0e5c09SChris Mason 	}
2279aa5d6bedSChris Mason 	return ret;
2280be0e5c09SChris Mason }
2281be0e5c09SChris Mason 
228274123bd7SChris Mason /*
228374123bd7SChris Mason  * how many bytes are required to store the items in a leaf.  start
228474123bd7SChris Mason  * and nr indicate which items in the leaf to check.  This totals up the
228574123bd7SChris Mason  * space used both by the item structs and the item data
228674123bd7SChris Mason  */
22875f39d397SChris Mason static int leaf_space_used(struct extent_buffer *l, int start, int nr)
2288be0e5c09SChris Mason {
2289be0e5c09SChris Mason 	int data_len;
22905f39d397SChris Mason 	int nritems = btrfs_header_nritems(l);
2291d4dbff95SChris Mason 	int end = min(nritems, start + nr) - 1;
2292be0e5c09SChris Mason 
2293be0e5c09SChris Mason 	if (!nr)
2294be0e5c09SChris Mason 		return 0;
22955f39d397SChris Mason 	data_len = btrfs_item_end_nr(l, start);
22965f39d397SChris Mason 	data_len = data_len - btrfs_item_offset_nr(l, end);
22970783fcfcSChris Mason 	data_len += sizeof(struct btrfs_item) * nr;
2298d4dbff95SChris Mason 	WARN_ON(data_len < 0);
2299be0e5c09SChris Mason 	return data_len;
2300be0e5c09SChris Mason }
2301be0e5c09SChris Mason 
230274123bd7SChris Mason /*
2303d4dbff95SChris Mason  * The space between the end of the leaf items and
2304d4dbff95SChris Mason  * the start of the leaf data.  IOW, how much room
2305d4dbff95SChris Mason  * the leaf has left for both items and data
2306d4dbff95SChris Mason  */
2307d397712bSChris Mason noinline int btrfs_leaf_free_space(struct btrfs_root *root,
2308e02119d5SChris Mason 				   struct extent_buffer *leaf)
2309d4dbff95SChris Mason {
23105f39d397SChris Mason 	int nritems = btrfs_header_nritems(leaf);
23115f39d397SChris Mason 	int ret;
23125f39d397SChris Mason 	ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
23135f39d397SChris Mason 	if (ret < 0) {
2314d397712bSChris Mason 		printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2315d397712bSChris Mason 		       "used %d nritems %d\n",
2316ae2f5411SJens Axboe 		       ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
23175f39d397SChris Mason 		       leaf_space_used(leaf, 0, nritems), nritems);
23185f39d397SChris Mason 	}
23195f39d397SChris Mason 	return ret;
2320d4dbff95SChris Mason }
2321d4dbff95SChris Mason 
232299d8f83cSChris Mason /*
232399d8f83cSChris Mason  * min slot controls the lowest index we're willing to push to the
232499d8f83cSChris Mason  * right.  We'll push up to and including min_slot, but no lower
232599d8f83cSChris Mason  */
232644871b1bSChris Mason static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
232744871b1bSChris Mason 				      struct btrfs_root *root,
232844871b1bSChris Mason 				      struct btrfs_path *path,
232944871b1bSChris Mason 				      int data_size, int empty,
233044871b1bSChris Mason 				      struct extent_buffer *right,
233199d8f83cSChris Mason 				      int free_space, u32 left_nritems,
233299d8f83cSChris Mason 				      u32 min_slot)
233300ec4c51SChris Mason {
23345f39d397SChris Mason 	struct extent_buffer *left = path->nodes[0];
233544871b1bSChris Mason 	struct extent_buffer *upper = path->nodes[1];
23365f39d397SChris Mason 	struct btrfs_disk_key disk_key;
233700ec4c51SChris Mason 	int slot;
233834a38218SChris Mason 	u32 i;
233900ec4c51SChris Mason 	int push_space = 0;
234000ec4c51SChris Mason 	int push_items = 0;
23410783fcfcSChris Mason 	struct btrfs_item *item;
234234a38218SChris Mason 	u32 nr;
23437518a238SChris Mason 	u32 right_nritems;
23445f39d397SChris Mason 	u32 data_end;
2345db94535dSChris Mason 	u32 this_item_size;
234600ec4c51SChris Mason 
234734a38218SChris Mason 	if (empty)
234834a38218SChris Mason 		nr = 0;
234934a38218SChris Mason 	else
235099d8f83cSChris Mason 		nr = max_t(u32, 1, min_slot);
235134a38218SChris Mason 
235231840ae1SZheng Yan 	if (path->slots[0] >= left_nritems)
235387b29b20SYan Zheng 		push_space += data_size;
235431840ae1SZheng Yan 
235544871b1bSChris Mason 	slot = path->slots[1];
235634a38218SChris Mason 	i = left_nritems - 1;
235734a38218SChris Mason 	while (i >= nr) {
23585f39d397SChris Mason 		item = btrfs_item_nr(left, i);
2359db94535dSChris Mason 
236031840ae1SZheng Yan 		if (!empty && push_items > 0) {
236131840ae1SZheng Yan 			if (path->slots[0] > i)
236231840ae1SZheng Yan 				break;
236331840ae1SZheng Yan 			if (path->slots[0] == i) {
236431840ae1SZheng Yan 				int space = btrfs_leaf_free_space(root, left);
236531840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
236631840ae1SZheng Yan 					break;
236731840ae1SZheng Yan 			}
236831840ae1SZheng Yan 		}
236931840ae1SZheng Yan 
237000ec4c51SChris Mason 		if (path->slots[0] == i)
237187b29b20SYan Zheng 			push_space += data_size;
2372db94535dSChris Mason 
2373db94535dSChris Mason 		this_item_size = btrfs_item_size(left, item);
2374db94535dSChris Mason 		if (this_item_size + sizeof(*item) + push_space > free_space)
237500ec4c51SChris Mason 			break;
237631840ae1SZheng Yan 
237700ec4c51SChris Mason 		push_items++;
2378db94535dSChris Mason 		push_space += this_item_size + sizeof(*item);
237934a38218SChris Mason 		if (i == 0)
238034a38218SChris Mason 			break;
238134a38218SChris Mason 		i--;
2382db94535dSChris Mason 	}
23835f39d397SChris Mason 
2384925baeddSChris Mason 	if (push_items == 0)
2385925baeddSChris Mason 		goto out_unlock;
23865f39d397SChris Mason 
238734a38218SChris Mason 	if (!empty && push_items == left_nritems)
2388a429e513SChris Mason 		WARN_ON(1);
23895f39d397SChris Mason 
239000ec4c51SChris Mason 	/* push left to right */
23915f39d397SChris Mason 	right_nritems = btrfs_header_nritems(right);
239234a38218SChris Mason 
23935f39d397SChris Mason 	push_space = btrfs_item_end_nr(left, left_nritems - push_items);
2394123abc88SChris Mason 	push_space -= leaf_data_end(root, left);
23955f39d397SChris Mason 
239600ec4c51SChris Mason 	/* make room in the right data area */
23975f39d397SChris Mason 	data_end = leaf_data_end(root, right);
23985f39d397SChris Mason 	memmove_extent_buffer(right,
23995f39d397SChris Mason 			      btrfs_leaf_data(right) + data_end - push_space,
24005f39d397SChris Mason 			      btrfs_leaf_data(right) + data_end,
24015f39d397SChris Mason 			      BTRFS_LEAF_DATA_SIZE(root) - data_end);
24025f39d397SChris Mason 
240300ec4c51SChris Mason 	/* copy from the left data area */
24045f39d397SChris Mason 	copy_extent_buffer(right, left, btrfs_leaf_data(right) +
2405d6025579SChris Mason 		     BTRFS_LEAF_DATA_SIZE(root) - push_space,
2406d6025579SChris Mason 		     btrfs_leaf_data(left) + leaf_data_end(root, left),
2407d6025579SChris Mason 		     push_space);
24085f39d397SChris Mason 
24095f39d397SChris Mason 	memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
24105f39d397SChris Mason 			      btrfs_item_nr_offset(0),
24110783fcfcSChris Mason 			      right_nritems * sizeof(struct btrfs_item));
24125f39d397SChris Mason 
241300ec4c51SChris Mason 	/* copy the items from left to right */
24145f39d397SChris Mason 	copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
24155f39d397SChris Mason 		   btrfs_item_nr_offset(left_nritems - push_items),
24160783fcfcSChris Mason 		   push_items * sizeof(struct btrfs_item));
241700ec4c51SChris Mason 
241800ec4c51SChris Mason 	/* update the item pointers */
24197518a238SChris Mason 	right_nritems += push_items;
24205f39d397SChris Mason 	btrfs_set_header_nritems(right, right_nritems);
2421123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root);
24227518a238SChris Mason 	for (i = 0; i < right_nritems; i++) {
24235f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2424db94535dSChris Mason 		push_space -= btrfs_item_size(right, item);
2425db94535dSChris Mason 		btrfs_set_item_offset(right, item, push_space);
2426db94535dSChris Mason 	}
2427db94535dSChris Mason 
24287518a238SChris Mason 	left_nritems -= push_items;
24295f39d397SChris Mason 	btrfs_set_header_nritems(left, left_nritems);
243000ec4c51SChris Mason 
243134a38218SChris Mason 	if (left_nritems)
24325f39d397SChris Mason 		btrfs_mark_buffer_dirty(left);
2433f0486c68SYan, Zheng 	else
2434f0486c68SYan, Zheng 		clean_tree_block(trans, root, left);
2435f0486c68SYan, Zheng 
24365f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
2437a429e513SChris Mason 
24385f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
24395f39d397SChris Mason 	btrfs_set_node_key(upper, &disk_key, slot + 1);
2440d6025579SChris Mason 	btrfs_mark_buffer_dirty(upper);
244102217ed2SChris Mason 
244200ec4c51SChris Mason 	/* then fixup the leaf pointer in the path */
24437518a238SChris Mason 	if (path->slots[0] >= left_nritems) {
24447518a238SChris Mason 		path->slots[0] -= left_nritems;
2445925baeddSChris Mason 		if (btrfs_header_nritems(path->nodes[0]) == 0)
2446925baeddSChris Mason 			clean_tree_block(trans, root, path->nodes[0]);
2447925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
24485f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
24495f39d397SChris Mason 		path->nodes[0] = right;
245000ec4c51SChris Mason 		path->slots[1] += 1;
245100ec4c51SChris Mason 	} else {
2452925baeddSChris Mason 		btrfs_tree_unlock(right);
24535f39d397SChris Mason 		free_extent_buffer(right);
245400ec4c51SChris Mason 	}
245500ec4c51SChris Mason 	return 0;
2456925baeddSChris Mason 
2457925baeddSChris Mason out_unlock:
2458925baeddSChris Mason 	btrfs_tree_unlock(right);
2459925baeddSChris Mason 	free_extent_buffer(right);
2460925baeddSChris Mason 	return 1;
246100ec4c51SChris Mason }
2462925baeddSChris Mason 
246300ec4c51SChris Mason /*
246444871b1bSChris Mason  * push some data in the path leaf to the right, trying to free up at
246574123bd7SChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
246644871b1bSChris Mason  *
246744871b1bSChris Mason  * returns 1 if the push failed because the other node didn't have enough
246844871b1bSChris Mason  * room, 0 if everything worked out and < 0 if there were major errors.
246999d8f83cSChris Mason  *
247099d8f83cSChris Mason  * this will push starting from min_slot to the end of the leaf.  It won't
247199d8f83cSChris Mason  * push any slot lower than min_slot
247274123bd7SChris Mason  */
247344871b1bSChris Mason static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
247499d8f83cSChris Mason 			   *root, struct btrfs_path *path,
247599d8f83cSChris Mason 			   int min_data_size, int data_size,
247699d8f83cSChris Mason 			   int empty, u32 min_slot)
2477be0e5c09SChris Mason {
247844871b1bSChris Mason 	struct extent_buffer *left = path->nodes[0];
247944871b1bSChris Mason 	struct extent_buffer *right;
248044871b1bSChris Mason 	struct extent_buffer *upper;
248144871b1bSChris Mason 	int slot;
248244871b1bSChris Mason 	int free_space;
248344871b1bSChris Mason 	u32 left_nritems;
248444871b1bSChris Mason 	int ret;
248544871b1bSChris Mason 
248644871b1bSChris Mason 	if (!path->nodes[1])
248744871b1bSChris Mason 		return 1;
248844871b1bSChris Mason 
248944871b1bSChris Mason 	slot = path->slots[1];
249044871b1bSChris Mason 	upper = path->nodes[1];
249144871b1bSChris Mason 	if (slot >= btrfs_header_nritems(upper) - 1)
249244871b1bSChris Mason 		return 1;
249344871b1bSChris Mason 
249444871b1bSChris Mason 	btrfs_assert_tree_locked(path->nodes[1]);
249544871b1bSChris Mason 
249644871b1bSChris Mason 	right = read_node_slot(root, upper, slot + 1);
249791ca338dSTsutomu Itoh 	if (right == NULL)
249891ca338dSTsutomu Itoh 		return 1;
249991ca338dSTsutomu Itoh 
250044871b1bSChris Mason 	btrfs_tree_lock(right);
250144871b1bSChris Mason 	btrfs_set_lock_blocking(right);
250244871b1bSChris Mason 
250344871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, right);
250444871b1bSChris Mason 	if (free_space < data_size)
250544871b1bSChris Mason 		goto out_unlock;
250644871b1bSChris Mason 
250744871b1bSChris Mason 	/* cow and double check */
250844871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, right, upper,
250944871b1bSChris Mason 			      slot + 1, &right);
251044871b1bSChris Mason 	if (ret)
251144871b1bSChris Mason 		goto out_unlock;
251244871b1bSChris Mason 
251344871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, right);
251444871b1bSChris Mason 	if (free_space < data_size)
251544871b1bSChris Mason 		goto out_unlock;
251644871b1bSChris Mason 
251744871b1bSChris Mason 	left_nritems = btrfs_header_nritems(left);
251844871b1bSChris Mason 	if (left_nritems == 0)
251944871b1bSChris Mason 		goto out_unlock;
252044871b1bSChris Mason 
252199d8f83cSChris Mason 	return __push_leaf_right(trans, root, path, min_data_size, empty,
252299d8f83cSChris Mason 				right, free_space, left_nritems, min_slot);
252344871b1bSChris Mason out_unlock:
252444871b1bSChris Mason 	btrfs_tree_unlock(right);
252544871b1bSChris Mason 	free_extent_buffer(right);
252644871b1bSChris Mason 	return 1;
252744871b1bSChris Mason }
252844871b1bSChris Mason 
252944871b1bSChris Mason /*
253044871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
253144871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
253299d8f83cSChris Mason  *
253399d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
253499d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us do all the
253599d8f83cSChris Mason  * items
253644871b1bSChris Mason  */
253744871b1bSChris Mason static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
253844871b1bSChris Mason 				     struct btrfs_root *root,
253944871b1bSChris Mason 				     struct btrfs_path *path, int data_size,
254044871b1bSChris Mason 				     int empty, struct extent_buffer *left,
254199d8f83cSChris Mason 				     int free_space, u32 right_nritems,
254299d8f83cSChris Mason 				     u32 max_slot)
254344871b1bSChris Mason {
25445f39d397SChris Mason 	struct btrfs_disk_key disk_key;
25455f39d397SChris Mason 	struct extent_buffer *right = path->nodes[0];
2546be0e5c09SChris Mason 	int i;
2547be0e5c09SChris Mason 	int push_space = 0;
2548be0e5c09SChris Mason 	int push_items = 0;
25490783fcfcSChris Mason 	struct btrfs_item *item;
25507518a238SChris Mason 	u32 old_left_nritems;
255134a38218SChris Mason 	u32 nr;
2552aa5d6bedSChris Mason 	int ret = 0;
2553aa5d6bedSChris Mason 	int wret;
2554db94535dSChris Mason 	u32 this_item_size;
2555db94535dSChris Mason 	u32 old_left_item_size;
2556be0e5c09SChris Mason 
255734a38218SChris Mason 	if (empty)
255899d8f83cSChris Mason 		nr = min(right_nritems, max_slot);
255934a38218SChris Mason 	else
256099d8f83cSChris Mason 		nr = min(right_nritems - 1, max_slot);
256134a38218SChris Mason 
256234a38218SChris Mason 	for (i = 0; i < nr; i++) {
25635f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2564db94535dSChris Mason 
256531840ae1SZheng Yan 		if (!empty && push_items > 0) {
256631840ae1SZheng Yan 			if (path->slots[0] < i)
256731840ae1SZheng Yan 				break;
256831840ae1SZheng Yan 			if (path->slots[0] == i) {
256931840ae1SZheng Yan 				int space = btrfs_leaf_free_space(root, right);
257031840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
257131840ae1SZheng Yan 					break;
257231840ae1SZheng Yan 			}
257331840ae1SZheng Yan 		}
257431840ae1SZheng Yan 
2575be0e5c09SChris Mason 		if (path->slots[0] == i)
257687b29b20SYan Zheng 			push_space += data_size;
2577db94535dSChris Mason 
2578db94535dSChris Mason 		this_item_size = btrfs_item_size(right, item);
2579db94535dSChris Mason 		if (this_item_size + sizeof(*item) + push_space > free_space)
2580be0e5c09SChris Mason 			break;
2581db94535dSChris Mason 
2582be0e5c09SChris Mason 		push_items++;
2583db94535dSChris Mason 		push_space += this_item_size + sizeof(*item);
2584be0e5c09SChris Mason 	}
2585db94535dSChris Mason 
2586be0e5c09SChris Mason 	if (push_items == 0) {
2587925baeddSChris Mason 		ret = 1;
2588925baeddSChris Mason 		goto out;
2589be0e5c09SChris Mason 	}
259034a38218SChris Mason 	if (!empty && push_items == btrfs_header_nritems(right))
2591a429e513SChris Mason 		WARN_ON(1);
25925f39d397SChris Mason 
2593be0e5c09SChris Mason 	/* push data from right to left */
25945f39d397SChris Mason 	copy_extent_buffer(left, right,
25955f39d397SChris Mason 			   btrfs_item_nr_offset(btrfs_header_nritems(left)),
25965f39d397SChris Mason 			   btrfs_item_nr_offset(0),
25975f39d397SChris Mason 			   push_items * sizeof(struct btrfs_item));
25985f39d397SChris Mason 
2599123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root) -
26005f39d397SChris Mason 		     btrfs_item_offset_nr(right, push_items - 1);
26015f39d397SChris Mason 
26025f39d397SChris Mason 	copy_extent_buffer(left, right, btrfs_leaf_data(left) +
2603d6025579SChris Mason 		     leaf_data_end(root, left) - push_space,
2604123abc88SChris Mason 		     btrfs_leaf_data(right) +
26055f39d397SChris Mason 		     btrfs_item_offset_nr(right, push_items - 1),
2606be0e5c09SChris Mason 		     push_space);
26075f39d397SChris Mason 	old_left_nritems = btrfs_header_nritems(left);
260887b29b20SYan Zheng 	BUG_ON(old_left_nritems <= 0);
2609eb60ceacSChris Mason 
2610db94535dSChris Mason 	old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
2611be0e5c09SChris Mason 	for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
26125f39d397SChris Mason 		u32 ioff;
2613db94535dSChris Mason 
26145f39d397SChris Mason 		item = btrfs_item_nr(left, i);
2615db94535dSChris Mason 
26165f39d397SChris Mason 		ioff = btrfs_item_offset(left, item);
26175f39d397SChris Mason 		btrfs_set_item_offset(left, item,
2618db94535dSChris Mason 		      ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
2619be0e5c09SChris Mason 	}
26205f39d397SChris Mason 	btrfs_set_header_nritems(left, old_left_nritems + push_items);
2621be0e5c09SChris Mason 
2622be0e5c09SChris Mason 	/* fixup right node */
262334a38218SChris Mason 	if (push_items > right_nritems) {
2624d397712bSChris Mason 		printk(KERN_CRIT "push items %d nr %u\n", push_items,
2625d397712bSChris Mason 		       right_nritems);
262634a38218SChris Mason 		WARN_ON(1);
262734a38218SChris Mason 	}
262834a38218SChris Mason 
262934a38218SChris Mason 	if (push_items < right_nritems) {
26305f39d397SChris Mason 		push_space = btrfs_item_offset_nr(right, push_items - 1) -
2631123abc88SChris Mason 						  leaf_data_end(root, right);
26325f39d397SChris Mason 		memmove_extent_buffer(right, btrfs_leaf_data(right) +
2633d6025579SChris Mason 				      BTRFS_LEAF_DATA_SIZE(root) - push_space,
2634d6025579SChris Mason 				      btrfs_leaf_data(right) +
2635123abc88SChris Mason 				      leaf_data_end(root, right), push_space);
26365f39d397SChris Mason 
26375f39d397SChris Mason 		memmove_extent_buffer(right, btrfs_item_nr_offset(0),
26385f39d397SChris Mason 			      btrfs_item_nr_offset(push_items),
26395f39d397SChris Mason 			     (btrfs_header_nritems(right) - push_items) *
26400783fcfcSChris Mason 			     sizeof(struct btrfs_item));
264134a38218SChris Mason 	}
2642eef1c494SYan 	right_nritems -= push_items;
2643eef1c494SYan 	btrfs_set_header_nritems(right, right_nritems);
2644123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root);
26455f39d397SChris Mason 	for (i = 0; i < right_nritems; i++) {
26465f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2647db94535dSChris Mason 
2648db94535dSChris Mason 		push_space = push_space - btrfs_item_size(right, item);
2649db94535dSChris Mason 		btrfs_set_item_offset(right, item, push_space);
2650db94535dSChris Mason 	}
2651eb60ceacSChris Mason 
26525f39d397SChris Mason 	btrfs_mark_buffer_dirty(left);
265334a38218SChris Mason 	if (right_nritems)
26545f39d397SChris Mason 		btrfs_mark_buffer_dirty(right);
2655f0486c68SYan, Zheng 	else
2656f0486c68SYan, Zheng 		clean_tree_block(trans, root, right);
2657098f59c2SChris Mason 
26585f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
26595f39d397SChris Mason 	wret = fixup_low_keys(trans, root, path, &disk_key, 1);
2660aa5d6bedSChris Mason 	if (wret)
2661aa5d6bedSChris Mason 		ret = wret;
2662be0e5c09SChris Mason 
2663be0e5c09SChris Mason 	/* then fixup the leaf pointer in the path */
2664be0e5c09SChris Mason 	if (path->slots[0] < push_items) {
2665be0e5c09SChris Mason 		path->slots[0] += old_left_nritems;
2666925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
26675f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
26685f39d397SChris Mason 		path->nodes[0] = left;
2669be0e5c09SChris Mason 		path->slots[1] -= 1;
2670be0e5c09SChris Mason 	} else {
2671925baeddSChris Mason 		btrfs_tree_unlock(left);
26725f39d397SChris Mason 		free_extent_buffer(left);
2673be0e5c09SChris Mason 		path->slots[0] -= push_items;
2674be0e5c09SChris Mason 	}
2675eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
2676aa5d6bedSChris Mason 	return ret;
2677925baeddSChris Mason out:
2678925baeddSChris Mason 	btrfs_tree_unlock(left);
2679925baeddSChris Mason 	free_extent_buffer(left);
2680925baeddSChris Mason 	return ret;
2681be0e5c09SChris Mason }
2682be0e5c09SChris Mason 
268374123bd7SChris Mason /*
268444871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
268544871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
268699d8f83cSChris Mason  *
268799d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
268899d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us push all the
268999d8f83cSChris Mason  * items
269044871b1bSChris Mason  */
269144871b1bSChris Mason static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
269299d8f83cSChris Mason 			  *root, struct btrfs_path *path, int min_data_size,
269399d8f83cSChris Mason 			  int data_size, int empty, u32 max_slot)
269444871b1bSChris Mason {
269544871b1bSChris Mason 	struct extent_buffer *right = path->nodes[0];
269644871b1bSChris Mason 	struct extent_buffer *left;
269744871b1bSChris Mason 	int slot;
269844871b1bSChris Mason 	int free_space;
269944871b1bSChris Mason 	u32 right_nritems;
270044871b1bSChris Mason 	int ret = 0;
270144871b1bSChris Mason 
270244871b1bSChris Mason 	slot = path->slots[1];
270344871b1bSChris Mason 	if (slot == 0)
270444871b1bSChris Mason 		return 1;
270544871b1bSChris Mason 	if (!path->nodes[1])
270644871b1bSChris Mason 		return 1;
270744871b1bSChris Mason 
270844871b1bSChris Mason 	right_nritems = btrfs_header_nritems(right);
270944871b1bSChris Mason 	if (right_nritems == 0)
271044871b1bSChris Mason 		return 1;
271144871b1bSChris Mason 
271244871b1bSChris Mason 	btrfs_assert_tree_locked(path->nodes[1]);
271344871b1bSChris Mason 
271444871b1bSChris Mason 	left = read_node_slot(root, path->nodes[1], slot - 1);
271591ca338dSTsutomu Itoh 	if (left == NULL)
271691ca338dSTsutomu Itoh 		return 1;
271791ca338dSTsutomu Itoh 
271844871b1bSChris Mason 	btrfs_tree_lock(left);
271944871b1bSChris Mason 	btrfs_set_lock_blocking(left);
272044871b1bSChris Mason 
272144871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, left);
272244871b1bSChris Mason 	if (free_space < data_size) {
272344871b1bSChris Mason 		ret = 1;
272444871b1bSChris Mason 		goto out;
272544871b1bSChris Mason 	}
272644871b1bSChris Mason 
272744871b1bSChris Mason 	/* cow and double check */
272844871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, left,
272944871b1bSChris Mason 			      path->nodes[1], slot - 1, &left);
273044871b1bSChris Mason 	if (ret) {
273144871b1bSChris Mason 		/* we hit -ENOSPC, but it isn't fatal here */
273244871b1bSChris Mason 		ret = 1;
273344871b1bSChris Mason 		goto out;
273444871b1bSChris Mason 	}
273544871b1bSChris Mason 
273644871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, left);
273744871b1bSChris Mason 	if (free_space < data_size) {
273844871b1bSChris Mason 		ret = 1;
273944871b1bSChris Mason 		goto out;
274044871b1bSChris Mason 	}
274144871b1bSChris Mason 
274299d8f83cSChris Mason 	return __push_leaf_left(trans, root, path, min_data_size,
274399d8f83cSChris Mason 			       empty, left, free_space, right_nritems,
274499d8f83cSChris Mason 			       max_slot);
274544871b1bSChris Mason out:
274644871b1bSChris Mason 	btrfs_tree_unlock(left);
274744871b1bSChris Mason 	free_extent_buffer(left);
274844871b1bSChris Mason 	return ret;
274944871b1bSChris Mason }
275044871b1bSChris Mason 
275144871b1bSChris Mason /*
275274123bd7SChris Mason  * split the path's leaf in two, making sure there is at least data_size
275374123bd7SChris Mason  * available for the resulting leaf level of the path.
2754aa5d6bedSChris Mason  *
2755aa5d6bedSChris Mason  * returns 0 if all went well and < 0 on failure.
275674123bd7SChris Mason  */
275744871b1bSChris Mason static noinline int copy_for_split(struct btrfs_trans_handle *trans,
2758e02119d5SChris Mason 			       struct btrfs_root *root,
275944871b1bSChris Mason 			       struct btrfs_path *path,
276044871b1bSChris Mason 			       struct extent_buffer *l,
276144871b1bSChris Mason 			       struct extent_buffer *right,
276244871b1bSChris Mason 			       int slot, int mid, int nritems)
2763be0e5c09SChris Mason {
2764be0e5c09SChris Mason 	int data_copy_size;
2765be0e5c09SChris Mason 	int rt_data_off;
2766be0e5c09SChris Mason 	int i;
2767d4dbff95SChris Mason 	int ret = 0;
2768aa5d6bedSChris Mason 	int wret;
2769d4dbff95SChris Mason 	struct btrfs_disk_key disk_key;
2770be0e5c09SChris Mason 
27715f39d397SChris Mason 	nritems = nritems - mid;
27725f39d397SChris Mason 	btrfs_set_header_nritems(right, nritems);
27735f39d397SChris Mason 	data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
27745f39d397SChris Mason 
27755f39d397SChris Mason 	copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
27765f39d397SChris Mason 			   btrfs_item_nr_offset(mid),
27775f39d397SChris Mason 			   nritems * sizeof(struct btrfs_item));
27785f39d397SChris Mason 
27795f39d397SChris Mason 	copy_extent_buffer(right, l,
2780d6025579SChris Mason 		     btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2781123abc88SChris Mason 		     data_copy_size, btrfs_leaf_data(l) +
2782123abc88SChris Mason 		     leaf_data_end(root, l), data_copy_size);
278374123bd7SChris Mason 
27845f39d397SChris Mason 	rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
27855f39d397SChris Mason 		      btrfs_item_end_nr(l, mid);
27865f39d397SChris Mason 
27875f39d397SChris Mason 	for (i = 0; i < nritems; i++) {
27885f39d397SChris Mason 		struct btrfs_item *item = btrfs_item_nr(right, i);
2789db94535dSChris Mason 		u32 ioff;
2790db94535dSChris Mason 
2791db94535dSChris Mason 		ioff = btrfs_item_offset(right, item);
27925f39d397SChris Mason 		btrfs_set_item_offset(right, item, ioff + rt_data_off);
27930783fcfcSChris Mason 	}
279474123bd7SChris Mason 
27955f39d397SChris Mason 	btrfs_set_header_nritems(l, mid);
2796aa5d6bedSChris Mason 	ret = 0;
27975f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
2798db94535dSChris Mason 	wret = insert_ptr(trans, root, path, &disk_key, right->start,
2799db94535dSChris Mason 			  path->slots[1] + 1, 1);
2800aa5d6bedSChris Mason 	if (wret)
2801aa5d6bedSChris Mason 		ret = wret;
28025f39d397SChris Mason 
28035f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
28045f39d397SChris Mason 	btrfs_mark_buffer_dirty(l);
2805eb60ceacSChris Mason 	BUG_ON(path->slots[0] != slot);
28065f39d397SChris Mason 
2807be0e5c09SChris Mason 	if (mid <= slot) {
2808925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
28095f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
28105f39d397SChris Mason 		path->nodes[0] = right;
2811be0e5c09SChris Mason 		path->slots[0] -= mid;
2812be0e5c09SChris Mason 		path->slots[1] += 1;
2813925baeddSChris Mason 	} else {
2814925baeddSChris Mason 		btrfs_tree_unlock(right);
28155f39d397SChris Mason 		free_extent_buffer(right);
2816925baeddSChris Mason 	}
28175f39d397SChris Mason 
2818eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
2819d4dbff95SChris Mason 
282044871b1bSChris Mason 	return ret;
282144871b1bSChris Mason }
282244871b1bSChris Mason 
282344871b1bSChris Mason /*
282499d8f83cSChris Mason  * double splits happen when we need to insert a big item in the middle
282599d8f83cSChris Mason  * of a leaf.  A double split can leave us with 3 mostly empty leaves:
282699d8f83cSChris Mason  * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
282799d8f83cSChris Mason  *          A                 B                 C
282899d8f83cSChris Mason  *
282999d8f83cSChris Mason  * We avoid this by trying to push the items on either side of our target
283099d8f83cSChris Mason  * into the adjacent leaves.  If all goes well we can avoid the double split
283199d8f83cSChris Mason  * completely.
283299d8f83cSChris Mason  */
283399d8f83cSChris Mason static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
283499d8f83cSChris Mason 					  struct btrfs_root *root,
283599d8f83cSChris Mason 					  struct btrfs_path *path,
283699d8f83cSChris Mason 					  int data_size)
283799d8f83cSChris Mason {
283899d8f83cSChris Mason 	int ret;
283999d8f83cSChris Mason 	int progress = 0;
284099d8f83cSChris Mason 	int slot;
284199d8f83cSChris Mason 	u32 nritems;
284299d8f83cSChris Mason 
284399d8f83cSChris Mason 	slot = path->slots[0];
284499d8f83cSChris Mason 
284599d8f83cSChris Mason 	/*
284699d8f83cSChris Mason 	 * try to push all the items after our slot into the
284799d8f83cSChris Mason 	 * right leaf
284899d8f83cSChris Mason 	 */
284999d8f83cSChris Mason 	ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
285099d8f83cSChris Mason 	if (ret < 0)
285199d8f83cSChris Mason 		return ret;
285299d8f83cSChris Mason 
285399d8f83cSChris Mason 	if (ret == 0)
285499d8f83cSChris Mason 		progress++;
285599d8f83cSChris Mason 
285699d8f83cSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
285799d8f83cSChris Mason 	/*
285899d8f83cSChris Mason 	 * our goal is to get our slot at the start or end of a leaf.  If
285999d8f83cSChris Mason 	 * we've done so we're done
286099d8f83cSChris Mason 	 */
286199d8f83cSChris Mason 	if (path->slots[0] == 0 || path->slots[0] == nritems)
286299d8f83cSChris Mason 		return 0;
286399d8f83cSChris Mason 
286499d8f83cSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
286599d8f83cSChris Mason 		return 0;
286699d8f83cSChris Mason 
286799d8f83cSChris Mason 	/* try to push all the items before our slot into the next leaf */
286899d8f83cSChris Mason 	slot = path->slots[0];
286999d8f83cSChris Mason 	ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
287099d8f83cSChris Mason 	if (ret < 0)
287199d8f83cSChris Mason 		return ret;
287299d8f83cSChris Mason 
287399d8f83cSChris Mason 	if (ret == 0)
287499d8f83cSChris Mason 		progress++;
287599d8f83cSChris Mason 
287699d8f83cSChris Mason 	if (progress)
287799d8f83cSChris Mason 		return 0;
287899d8f83cSChris Mason 	return 1;
287999d8f83cSChris Mason }
288099d8f83cSChris Mason 
288199d8f83cSChris Mason /*
288244871b1bSChris Mason  * split the path's leaf in two, making sure there is at least data_size
288344871b1bSChris Mason  * available for the resulting leaf level of the path.
288444871b1bSChris Mason  *
288544871b1bSChris Mason  * returns 0 if all went well and < 0 on failure.
288644871b1bSChris Mason  */
288744871b1bSChris Mason static noinline int split_leaf(struct btrfs_trans_handle *trans,
288844871b1bSChris Mason 			       struct btrfs_root *root,
288944871b1bSChris Mason 			       struct btrfs_key *ins_key,
289044871b1bSChris Mason 			       struct btrfs_path *path, int data_size,
289144871b1bSChris Mason 			       int extend)
289244871b1bSChris Mason {
28935d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
289444871b1bSChris Mason 	struct extent_buffer *l;
289544871b1bSChris Mason 	u32 nritems;
289644871b1bSChris Mason 	int mid;
289744871b1bSChris Mason 	int slot;
289844871b1bSChris Mason 	struct extent_buffer *right;
289944871b1bSChris Mason 	int ret = 0;
290044871b1bSChris Mason 	int wret;
29015d4f98a2SYan Zheng 	int split;
290244871b1bSChris Mason 	int num_doubles = 0;
290399d8f83cSChris Mason 	int tried_avoid_double = 0;
290444871b1bSChris Mason 
2905a5719521SYan, Zheng 	l = path->nodes[0];
2906a5719521SYan, Zheng 	slot = path->slots[0];
2907a5719521SYan, Zheng 	if (extend && data_size + btrfs_item_size_nr(l, slot) +
2908a5719521SYan, Zheng 	    sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2909a5719521SYan, Zheng 		return -EOVERFLOW;
2910a5719521SYan, Zheng 
291144871b1bSChris Mason 	/* first try to make some room by pushing left and right */
291299d8f83cSChris Mason 	if (data_size) {
291399d8f83cSChris Mason 		wret = push_leaf_right(trans, root, path, data_size,
291499d8f83cSChris Mason 				       data_size, 0, 0);
291544871b1bSChris Mason 		if (wret < 0)
291644871b1bSChris Mason 			return wret;
291744871b1bSChris Mason 		if (wret) {
291899d8f83cSChris Mason 			wret = push_leaf_left(trans, root, path, data_size,
291999d8f83cSChris Mason 					      data_size, 0, (u32)-1);
292044871b1bSChris Mason 			if (wret < 0)
292144871b1bSChris Mason 				return wret;
292244871b1bSChris Mason 		}
292344871b1bSChris Mason 		l = path->nodes[0];
292444871b1bSChris Mason 
292544871b1bSChris Mason 		/* did the pushes work? */
292644871b1bSChris Mason 		if (btrfs_leaf_free_space(root, l) >= data_size)
292744871b1bSChris Mason 			return 0;
292844871b1bSChris Mason 	}
292944871b1bSChris Mason 
293044871b1bSChris Mason 	if (!path->nodes[1]) {
293144871b1bSChris Mason 		ret = insert_new_root(trans, root, path, 1);
293244871b1bSChris Mason 		if (ret)
293344871b1bSChris Mason 			return ret;
293444871b1bSChris Mason 	}
293544871b1bSChris Mason again:
29365d4f98a2SYan Zheng 	split = 1;
293744871b1bSChris Mason 	l = path->nodes[0];
293844871b1bSChris Mason 	slot = path->slots[0];
293944871b1bSChris Mason 	nritems = btrfs_header_nritems(l);
294044871b1bSChris Mason 	mid = (nritems + 1) / 2;
294144871b1bSChris Mason 
29425d4f98a2SYan Zheng 	if (mid <= slot) {
29435d4f98a2SYan Zheng 		if (nritems == 1 ||
29445d4f98a2SYan Zheng 		    leaf_space_used(l, mid, nritems - mid) + data_size >
29455d4f98a2SYan Zheng 			BTRFS_LEAF_DATA_SIZE(root)) {
29465d4f98a2SYan Zheng 			if (slot >= nritems) {
29475d4f98a2SYan Zheng 				split = 0;
29485d4f98a2SYan Zheng 			} else {
29495d4f98a2SYan Zheng 				mid = slot;
29505d4f98a2SYan Zheng 				if (mid != nritems &&
29515d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
29525d4f98a2SYan Zheng 				    data_size > BTRFS_LEAF_DATA_SIZE(root)) {
295399d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
295499d8f83cSChris Mason 						goto push_for_double;
29555d4f98a2SYan Zheng 					split = 2;
29565d4f98a2SYan Zheng 				}
29575d4f98a2SYan Zheng 			}
29585d4f98a2SYan Zheng 		}
29595d4f98a2SYan Zheng 	} else {
29605d4f98a2SYan Zheng 		if (leaf_space_used(l, 0, mid) + data_size >
29615d4f98a2SYan Zheng 			BTRFS_LEAF_DATA_SIZE(root)) {
29625d4f98a2SYan Zheng 			if (!extend && data_size && slot == 0) {
29635d4f98a2SYan Zheng 				split = 0;
29645d4f98a2SYan Zheng 			} else if ((extend || !data_size) && slot == 0) {
29655d4f98a2SYan Zheng 				mid = 1;
29665d4f98a2SYan Zheng 			} else {
29675d4f98a2SYan Zheng 				mid = slot;
29685d4f98a2SYan Zheng 				if (mid != nritems &&
29695d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
29705d4f98a2SYan Zheng 				    data_size > BTRFS_LEAF_DATA_SIZE(root)) {
297199d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
297299d8f83cSChris Mason 						goto push_for_double;
29735d4f98a2SYan Zheng 					split = 2 ;
29745d4f98a2SYan Zheng 				}
29755d4f98a2SYan Zheng 			}
29765d4f98a2SYan Zheng 		}
29775d4f98a2SYan Zheng 	}
29785d4f98a2SYan Zheng 
29795d4f98a2SYan Zheng 	if (split == 0)
29805d4f98a2SYan Zheng 		btrfs_cpu_key_to_disk(&disk_key, ins_key);
29815d4f98a2SYan Zheng 	else
29825d4f98a2SYan Zheng 		btrfs_item_key(l, &disk_key, mid);
29835d4f98a2SYan Zheng 
29845d4f98a2SYan Zheng 	right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
298544871b1bSChris Mason 					root->root_key.objectid,
298666d7e7f0SArne Jansen 					&disk_key, 0, l->start, 0, 0);
2987f0486c68SYan, Zheng 	if (IS_ERR(right))
298844871b1bSChris Mason 		return PTR_ERR(right);
2989f0486c68SYan, Zheng 
2990f0486c68SYan, Zheng 	root_add_used(root, root->leafsize);
299144871b1bSChris Mason 
299244871b1bSChris Mason 	memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
299344871b1bSChris Mason 	btrfs_set_header_bytenr(right, right->start);
299444871b1bSChris Mason 	btrfs_set_header_generation(right, trans->transid);
29955d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
299644871b1bSChris Mason 	btrfs_set_header_owner(right, root->root_key.objectid);
299744871b1bSChris Mason 	btrfs_set_header_level(right, 0);
299844871b1bSChris Mason 	write_extent_buffer(right, root->fs_info->fsid,
299944871b1bSChris Mason 			    (unsigned long)btrfs_header_fsid(right),
300044871b1bSChris Mason 			    BTRFS_FSID_SIZE);
300144871b1bSChris Mason 
300244871b1bSChris Mason 	write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
300344871b1bSChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(right),
300444871b1bSChris Mason 			    BTRFS_UUID_SIZE);
300544871b1bSChris Mason 
30065d4f98a2SYan Zheng 	if (split == 0) {
300744871b1bSChris Mason 		if (mid <= slot) {
300844871b1bSChris Mason 			btrfs_set_header_nritems(right, 0);
300944871b1bSChris Mason 			wret = insert_ptr(trans, root, path,
301044871b1bSChris Mason 					  &disk_key, right->start,
301144871b1bSChris Mason 					  path->slots[1] + 1, 1);
301244871b1bSChris Mason 			if (wret)
301344871b1bSChris Mason 				ret = wret;
301444871b1bSChris Mason 
301544871b1bSChris Mason 			btrfs_tree_unlock(path->nodes[0]);
301644871b1bSChris Mason 			free_extent_buffer(path->nodes[0]);
301744871b1bSChris Mason 			path->nodes[0] = right;
301844871b1bSChris Mason 			path->slots[0] = 0;
301944871b1bSChris Mason 			path->slots[1] += 1;
302044871b1bSChris Mason 		} else {
302144871b1bSChris Mason 			btrfs_set_header_nritems(right, 0);
302244871b1bSChris Mason 			wret = insert_ptr(trans, root, path,
302344871b1bSChris Mason 					  &disk_key,
302444871b1bSChris Mason 					  right->start,
302544871b1bSChris Mason 					  path->slots[1], 1);
302644871b1bSChris Mason 			if (wret)
302744871b1bSChris Mason 				ret = wret;
302844871b1bSChris Mason 			btrfs_tree_unlock(path->nodes[0]);
302944871b1bSChris Mason 			free_extent_buffer(path->nodes[0]);
303044871b1bSChris Mason 			path->nodes[0] = right;
303144871b1bSChris Mason 			path->slots[0] = 0;
303244871b1bSChris Mason 			if (path->slots[1] == 0) {
303344871b1bSChris Mason 				wret = fixup_low_keys(trans, root,
303444871b1bSChris Mason 						path, &disk_key, 1);
303544871b1bSChris Mason 				if (wret)
303644871b1bSChris Mason 					ret = wret;
303744871b1bSChris Mason 			}
30385d4f98a2SYan Zheng 		}
303944871b1bSChris Mason 		btrfs_mark_buffer_dirty(right);
304044871b1bSChris Mason 		return ret;
304144871b1bSChris Mason 	}
304244871b1bSChris Mason 
304344871b1bSChris Mason 	ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
304444871b1bSChris Mason 	BUG_ON(ret);
304544871b1bSChris Mason 
30465d4f98a2SYan Zheng 	if (split == 2) {
3047cc0c5538SChris Mason 		BUG_ON(num_doubles != 0);
3048cc0c5538SChris Mason 		num_doubles++;
3049cc0c5538SChris Mason 		goto again;
30503326d1b0SChris Mason 	}
305144871b1bSChris Mason 
3052be0e5c09SChris Mason 	return ret;
305399d8f83cSChris Mason 
305499d8f83cSChris Mason push_for_double:
305599d8f83cSChris Mason 	push_for_double_split(trans, root, path, data_size);
305699d8f83cSChris Mason 	tried_avoid_double = 1;
305799d8f83cSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
305899d8f83cSChris Mason 		return 0;
305999d8f83cSChris Mason 	goto again;
3060be0e5c09SChris Mason }
3061be0e5c09SChris Mason 
3062ad48fd75SYan, Zheng static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3063ad48fd75SYan, Zheng 					 struct btrfs_root *root,
3064ad48fd75SYan, Zheng 					 struct btrfs_path *path, int ins_len)
3065ad48fd75SYan, Zheng {
3066ad48fd75SYan, Zheng 	struct btrfs_key key;
3067ad48fd75SYan, Zheng 	struct extent_buffer *leaf;
3068ad48fd75SYan, Zheng 	struct btrfs_file_extent_item *fi;
3069ad48fd75SYan, Zheng 	u64 extent_len = 0;
3070ad48fd75SYan, Zheng 	u32 item_size;
3071ad48fd75SYan, Zheng 	int ret;
3072ad48fd75SYan, Zheng 
3073ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3074ad48fd75SYan, Zheng 	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3075ad48fd75SYan, Zheng 
3076ad48fd75SYan, Zheng 	BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3077ad48fd75SYan, Zheng 	       key.type != BTRFS_EXTENT_CSUM_KEY);
3078ad48fd75SYan, Zheng 
3079ad48fd75SYan, Zheng 	if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3080ad48fd75SYan, Zheng 		return 0;
3081ad48fd75SYan, Zheng 
3082ad48fd75SYan, Zheng 	item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3083ad48fd75SYan, Zheng 	if (key.type == BTRFS_EXTENT_DATA_KEY) {
3084ad48fd75SYan, Zheng 		fi = btrfs_item_ptr(leaf, path->slots[0],
3085ad48fd75SYan, Zheng 				    struct btrfs_file_extent_item);
3086ad48fd75SYan, Zheng 		extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3087ad48fd75SYan, Zheng 	}
3088b3b4aa74SDavid Sterba 	btrfs_release_path(path);
3089ad48fd75SYan, Zheng 
3090ad48fd75SYan, Zheng 	path->keep_locks = 1;
3091ad48fd75SYan, Zheng 	path->search_for_split = 1;
3092ad48fd75SYan, Zheng 	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3093ad48fd75SYan, Zheng 	path->search_for_split = 0;
3094ad48fd75SYan, Zheng 	if (ret < 0)
3095ad48fd75SYan, Zheng 		goto err;
3096ad48fd75SYan, Zheng 
3097ad48fd75SYan, Zheng 	ret = -EAGAIN;
3098ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3099ad48fd75SYan, Zheng 	/* if our item isn't there or got smaller, return now */
3100ad48fd75SYan, Zheng 	if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3101ad48fd75SYan, Zheng 		goto err;
3102ad48fd75SYan, Zheng 
3103109f6aefSChris Mason 	/* the leaf has  changed, it now has room.  return now */
3104109f6aefSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3105109f6aefSChris Mason 		goto err;
3106109f6aefSChris Mason 
3107ad48fd75SYan, Zheng 	if (key.type == BTRFS_EXTENT_DATA_KEY) {
3108ad48fd75SYan, Zheng 		fi = btrfs_item_ptr(leaf, path->slots[0],
3109ad48fd75SYan, Zheng 				    struct btrfs_file_extent_item);
3110ad48fd75SYan, Zheng 		if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3111ad48fd75SYan, Zheng 			goto err;
3112ad48fd75SYan, Zheng 	}
3113ad48fd75SYan, Zheng 
3114ad48fd75SYan, Zheng 	btrfs_set_path_blocking(path);
3115ad48fd75SYan, Zheng 	ret = split_leaf(trans, root, &key, path, ins_len, 1);
3116f0486c68SYan, Zheng 	if (ret)
3117f0486c68SYan, Zheng 		goto err;
3118ad48fd75SYan, Zheng 
3119ad48fd75SYan, Zheng 	path->keep_locks = 0;
3120ad48fd75SYan, Zheng 	btrfs_unlock_up_safe(path, 1);
3121ad48fd75SYan, Zheng 	return 0;
3122ad48fd75SYan, Zheng err:
3123ad48fd75SYan, Zheng 	path->keep_locks = 0;
3124ad48fd75SYan, Zheng 	return ret;
3125ad48fd75SYan, Zheng }
3126ad48fd75SYan, Zheng 
3127ad48fd75SYan, Zheng static noinline int split_item(struct btrfs_trans_handle *trans,
3128459931ecSChris Mason 			       struct btrfs_root *root,
3129459931ecSChris Mason 			       struct btrfs_path *path,
3130459931ecSChris Mason 			       struct btrfs_key *new_key,
3131459931ecSChris Mason 			       unsigned long split_offset)
3132459931ecSChris Mason {
3133459931ecSChris Mason 	struct extent_buffer *leaf;
3134459931ecSChris Mason 	struct btrfs_item *item;
3135459931ecSChris Mason 	struct btrfs_item *new_item;
3136459931ecSChris Mason 	int slot;
3137ad48fd75SYan, Zheng 	char *buf;
3138459931ecSChris Mason 	u32 nritems;
3139ad48fd75SYan, Zheng 	u32 item_size;
3140459931ecSChris Mason 	u32 orig_offset;
3141459931ecSChris Mason 	struct btrfs_disk_key disk_key;
3142459931ecSChris Mason 
3143459931ecSChris Mason 	leaf = path->nodes[0];
3144b9473439SChris Mason 	BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3145b9473439SChris Mason 
3146b4ce94deSChris Mason 	btrfs_set_path_blocking(path);
3147b4ce94deSChris Mason 
3148459931ecSChris Mason 	item = btrfs_item_nr(leaf, path->slots[0]);
3149459931ecSChris Mason 	orig_offset = btrfs_item_offset(leaf, item);
3150459931ecSChris Mason 	item_size = btrfs_item_size(leaf, item);
3151459931ecSChris Mason 
3152459931ecSChris Mason 	buf = kmalloc(item_size, GFP_NOFS);
3153ad48fd75SYan, Zheng 	if (!buf)
3154ad48fd75SYan, Zheng 		return -ENOMEM;
3155ad48fd75SYan, Zheng 
3156459931ecSChris Mason 	read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3157459931ecSChris Mason 			    path->slots[0]), item_size);
3158ad48fd75SYan, Zheng 
3159459931ecSChris Mason 	slot = path->slots[0] + 1;
3160459931ecSChris Mason 	nritems = btrfs_header_nritems(leaf);
3161459931ecSChris Mason 	if (slot != nritems) {
3162459931ecSChris Mason 		/* shift the items */
3163459931ecSChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3164459931ecSChris Mason 				btrfs_item_nr_offset(slot),
3165459931ecSChris Mason 				(nritems - slot) * sizeof(struct btrfs_item));
3166459931ecSChris Mason 	}
3167459931ecSChris Mason 
3168459931ecSChris Mason 	btrfs_cpu_key_to_disk(&disk_key, new_key);
3169459931ecSChris Mason 	btrfs_set_item_key(leaf, &disk_key, slot);
3170459931ecSChris Mason 
3171459931ecSChris Mason 	new_item = btrfs_item_nr(leaf, slot);
3172459931ecSChris Mason 
3173459931ecSChris Mason 	btrfs_set_item_offset(leaf, new_item, orig_offset);
3174459931ecSChris Mason 	btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3175459931ecSChris Mason 
3176459931ecSChris Mason 	btrfs_set_item_offset(leaf, item,
3177459931ecSChris Mason 			      orig_offset + item_size - split_offset);
3178459931ecSChris Mason 	btrfs_set_item_size(leaf, item, split_offset);
3179459931ecSChris Mason 
3180459931ecSChris Mason 	btrfs_set_header_nritems(leaf, nritems + 1);
3181459931ecSChris Mason 
3182459931ecSChris Mason 	/* write the data for the start of the original item */
3183459931ecSChris Mason 	write_extent_buffer(leaf, buf,
3184459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, path->slots[0]),
3185459931ecSChris Mason 			    split_offset);
3186459931ecSChris Mason 
3187459931ecSChris Mason 	/* write the data for the new item */
3188459931ecSChris Mason 	write_extent_buffer(leaf, buf + split_offset,
3189459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, slot),
3190459931ecSChris Mason 			    item_size - split_offset);
3191459931ecSChris Mason 	btrfs_mark_buffer_dirty(leaf);
3192459931ecSChris Mason 
3193ad48fd75SYan, Zheng 	BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
3194459931ecSChris Mason 	kfree(buf);
3195ad48fd75SYan, Zheng 	return 0;
3196ad48fd75SYan, Zheng }
3197ad48fd75SYan, Zheng 
3198ad48fd75SYan, Zheng /*
3199ad48fd75SYan, Zheng  * This function splits a single item into two items,
3200ad48fd75SYan, Zheng  * giving 'new_key' to the new item and splitting the
3201ad48fd75SYan, Zheng  * old one at split_offset (from the start of the item).
3202ad48fd75SYan, Zheng  *
3203ad48fd75SYan, Zheng  * The path may be released by this operation.  After
3204ad48fd75SYan, Zheng  * the split, the path is pointing to the old item.  The
3205ad48fd75SYan, Zheng  * new item is going to be in the same node as the old one.
3206ad48fd75SYan, Zheng  *
3207ad48fd75SYan, Zheng  * Note, the item being split must be smaller enough to live alone on
3208ad48fd75SYan, Zheng  * a tree block with room for one extra struct btrfs_item
3209ad48fd75SYan, Zheng  *
3210ad48fd75SYan, Zheng  * This allows us to split the item in place, keeping a lock on the
3211ad48fd75SYan, Zheng  * leaf the entire time.
3212ad48fd75SYan, Zheng  */
3213ad48fd75SYan, Zheng int btrfs_split_item(struct btrfs_trans_handle *trans,
3214ad48fd75SYan, Zheng 		     struct btrfs_root *root,
3215ad48fd75SYan, Zheng 		     struct btrfs_path *path,
3216ad48fd75SYan, Zheng 		     struct btrfs_key *new_key,
3217ad48fd75SYan, Zheng 		     unsigned long split_offset)
3218ad48fd75SYan, Zheng {
3219ad48fd75SYan, Zheng 	int ret;
3220ad48fd75SYan, Zheng 	ret = setup_leaf_for_split(trans, root, path,
3221ad48fd75SYan, Zheng 				   sizeof(struct btrfs_item));
3222ad48fd75SYan, Zheng 	if (ret)
3223459931ecSChris Mason 		return ret;
3224ad48fd75SYan, Zheng 
3225ad48fd75SYan, Zheng 	ret = split_item(trans, root, path, new_key, split_offset);
3226ad48fd75SYan, Zheng 	return ret;
3227ad48fd75SYan, Zheng }
3228ad48fd75SYan, Zheng 
3229ad48fd75SYan, Zheng /*
3230ad48fd75SYan, Zheng  * This function duplicate a item, giving 'new_key' to the new item.
3231ad48fd75SYan, Zheng  * It guarantees both items live in the same tree leaf and the new item
3232ad48fd75SYan, Zheng  * is contiguous with the original item.
3233ad48fd75SYan, Zheng  *
3234ad48fd75SYan, Zheng  * This allows us to split file extent in place, keeping a lock on the
3235ad48fd75SYan, Zheng  * leaf the entire time.
3236ad48fd75SYan, Zheng  */
3237ad48fd75SYan, Zheng int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3238ad48fd75SYan, Zheng 			 struct btrfs_root *root,
3239ad48fd75SYan, Zheng 			 struct btrfs_path *path,
3240ad48fd75SYan, Zheng 			 struct btrfs_key *new_key)
3241ad48fd75SYan, Zheng {
3242ad48fd75SYan, Zheng 	struct extent_buffer *leaf;
3243ad48fd75SYan, Zheng 	int ret;
3244ad48fd75SYan, Zheng 	u32 item_size;
3245ad48fd75SYan, Zheng 
3246ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3247ad48fd75SYan, Zheng 	item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3248ad48fd75SYan, Zheng 	ret = setup_leaf_for_split(trans, root, path,
3249ad48fd75SYan, Zheng 				   item_size + sizeof(struct btrfs_item));
3250ad48fd75SYan, Zheng 	if (ret)
3251ad48fd75SYan, Zheng 		return ret;
3252ad48fd75SYan, Zheng 
3253ad48fd75SYan, Zheng 	path->slots[0]++;
3254ad48fd75SYan, Zheng 	ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3255ad48fd75SYan, Zheng 				     item_size, item_size +
3256ad48fd75SYan, Zheng 				     sizeof(struct btrfs_item), 1);
3257ad48fd75SYan, Zheng 	BUG_ON(ret);
3258ad48fd75SYan, Zheng 
3259ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3260ad48fd75SYan, Zheng 	memcpy_extent_buffer(leaf,
3261ad48fd75SYan, Zheng 			     btrfs_item_ptr_offset(leaf, path->slots[0]),
3262ad48fd75SYan, Zheng 			     btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3263ad48fd75SYan, Zheng 			     item_size);
3264ad48fd75SYan, Zheng 	return 0;
3265459931ecSChris Mason }
3266459931ecSChris Mason 
3267459931ecSChris Mason /*
3268d352ac68SChris Mason  * make the item pointed to by the path smaller.  new_size indicates
3269d352ac68SChris Mason  * how small to make it, and from_end tells us if we just chop bytes
3270d352ac68SChris Mason  * off the end of the item or if we shift the item to chop bytes off
3271d352ac68SChris Mason  * the front.
3272d352ac68SChris Mason  */
3273b18c6685SChris Mason int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3274b18c6685SChris Mason 			struct btrfs_root *root,
3275b18c6685SChris Mason 			struct btrfs_path *path,
3276179e29e4SChris Mason 			u32 new_size, int from_end)
3277b18c6685SChris Mason {
3278b18c6685SChris Mason 	int slot;
32795f39d397SChris Mason 	struct extent_buffer *leaf;
32805f39d397SChris Mason 	struct btrfs_item *item;
3281b18c6685SChris Mason 	u32 nritems;
3282b18c6685SChris Mason 	unsigned int data_end;
3283b18c6685SChris Mason 	unsigned int old_data_start;
3284b18c6685SChris Mason 	unsigned int old_size;
3285b18c6685SChris Mason 	unsigned int size_diff;
3286b18c6685SChris Mason 	int i;
3287b18c6685SChris Mason 
32885f39d397SChris Mason 	leaf = path->nodes[0];
3289179e29e4SChris Mason 	slot = path->slots[0];
3290179e29e4SChris Mason 
3291179e29e4SChris Mason 	old_size = btrfs_item_size_nr(leaf, slot);
3292179e29e4SChris Mason 	if (old_size == new_size)
3293179e29e4SChris Mason 		return 0;
3294b18c6685SChris Mason 
32955f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3296b18c6685SChris Mason 	data_end = leaf_data_end(root, leaf);
3297b18c6685SChris Mason 
32985f39d397SChris Mason 	old_data_start = btrfs_item_offset_nr(leaf, slot);
3299179e29e4SChris Mason 
3300b18c6685SChris Mason 	size_diff = old_size - new_size;
3301b18c6685SChris Mason 
3302b18c6685SChris Mason 	BUG_ON(slot < 0);
3303b18c6685SChris Mason 	BUG_ON(slot >= nritems);
3304b18c6685SChris Mason 
3305b18c6685SChris Mason 	/*
3306b18c6685SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3307b18c6685SChris Mason 	 */
3308b18c6685SChris Mason 	/* first correct the data pointers */
3309b18c6685SChris Mason 	for (i = slot; i < nritems; i++) {
33105f39d397SChris Mason 		u32 ioff;
33115f39d397SChris Mason 		item = btrfs_item_nr(leaf, i);
3312db94535dSChris Mason 
33135f39d397SChris Mason 		ioff = btrfs_item_offset(leaf, item);
33145f39d397SChris Mason 		btrfs_set_item_offset(leaf, item, ioff + size_diff);
3315b18c6685SChris Mason 	}
3316db94535dSChris Mason 
3317b18c6685SChris Mason 	/* shift the data */
3318179e29e4SChris Mason 	if (from_end) {
33195f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3320b18c6685SChris Mason 			      data_end + size_diff, btrfs_leaf_data(leaf) +
3321b18c6685SChris Mason 			      data_end, old_data_start + new_size - data_end);
3322179e29e4SChris Mason 	} else {
3323179e29e4SChris Mason 		struct btrfs_disk_key disk_key;
3324179e29e4SChris Mason 		u64 offset;
3325179e29e4SChris Mason 
3326179e29e4SChris Mason 		btrfs_item_key(leaf, &disk_key, slot);
3327179e29e4SChris Mason 
3328179e29e4SChris Mason 		if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3329179e29e4SChris Mason 			unsigned long ptr;
3330179e29e4SChris Mason 			struct btrfs_file_extent_item *fi;
3331179e29e4SChris Mason 
3332179e29e4SChris Mason 			fi = btrfs_item_ptr(leaf, slot,
3333179e29e4SChris Mason 					    struct btrfs_file_extent_item);
3334179e29e4SChris Mason 			fi = (struct btrfs_file_extent_item *)(
3335179e29e4SChris Mason 			     (unsigned long)fi - size_diff);
3336179e29e4SChris Mason 
3337179e29e4SChris Mason 			if (btrfs_file_extent_type(leaf, fi) ==
3338179e29e4SChris Mason 			    BTRFS_FILE_EXTENT_INLINE) {
3339179e29e4SChris Mason 				ptr = btrfs_item_ptr_offset(leaf, slot);
3340179e29e4SChris Mason 				memmove_extent_buffer(leaf, ptr,
3341179e29e4SChris Mason 				      (unsigned long)fi,
3342179e29e4SChris Mason 				      offsetof(struct btrfs_file_extent_item,
3343179e29e4SChris Mason 						 disk_bytenr));
3344179e29e4SChris Mason 			}
3345179e29e4SChris Mason 		}
3346179e29e4SChris Mason 
3347179e29e4SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3348179e29e4SChris Mason 			      data_end + size_diff, btrfs_leaf_data(leaf) +
3349179e29e4SChris Mason 			      data_end, old_data_start - data_end);
3350179e29e4SChris Mason 
3351179e29e4SChris Mason 		offset = btrfs_disk_key_offset(&disk_key);
3352179e29e4SChris Mason 		btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3353179e29e4SChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot);
3354179e29e4SChris Mason 		if (slot == 0)
3355179e29e4SChris Mason 			fixup_low_keys(trans, root, path, &disk_key, 1);
3356179e29e4SChris Mason 	}
33575f39d397SChris Mason 
33585f39d397SChris Mason 	item = btrfs_item_nr(leaf, slot);
33595f39d397SChris Mason 	btrfs_set_item_size(leaf, item, new_size);
33605f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
3361b18c6685SChris Mason 
33625f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
33635f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
3364b18c6685SChris Mason 		BUG();
33655f39d397SChris Mason 	}
33661cd30799STsutomu Itoh 	return 0;
3367b18c6685SChris Mason }
3368b18c6685SChris Mason 
3369d352ac68SChris Mason /*
3370d352ac68SChris Mason  * make the item pointed to by the path bigger, data_size is the new size.
3371d352ac68SChris Mason  */
33725f39d397SChris Mason int btrfs_extend_item(struct btrfs_trans_handle *trans,
33735f39d397SChris Mason 		      struct btrfs_root *root, struct btrfs_path *path,
33745f39d397SChris Mason 		      u32 data_size)
33756567e837SChris Mason {
33766567e837SChris Mason 	int slot;
33775f39d397SChris Mason 	struct extent_buffer *leaf;
33785f39d397SChris Mason 	struct btrfs_item *item;
33796567e837SChris Mason 	u32 nritems;
33806567e837SChris Mason 	unsigned int data_end;
33816567e837SChris Mason 	unsigned int old_data;
33826567e837SChris Mason 	unsigned int old_size;
33836567e837SChris Mason 	int i;
33846567e837SChris Mason 
33855f39d397SChris Mason 	leaf = path->nodes[0];
33866567e837SChris Mason 
33875f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
33886567e837SChris Mason 	data_end = leaf_data_end(root, leaf);
33896567e837SChris Mason 
33905f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < data_size) {
33915f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
33926567e837SChris Mason 		BUG();
33935f39d397SChris Mason 	}
33946567e837SChris Mason 	slot = path->slots[0];
33955f39d397SChris Mason 	old_data = btrfs_item_end_nr(leaf, slot);
33966567e837SChris Mason 
33976567e837SChris Mason 	BUG_ON(slot < 0);
33983326d1b0SChris Mason 	if (slot >= nritems) {
33993326d1b0SChris Mason 		btrfs_print_leaf(root, leaf);
3400d397712bSChris Mason 		printk(KERN_CRIT "slot %d too large, nritems %d\n",
3401d397712bSChris Mason 		       slot, nritems);
34023326d1b0SChris Mason 		BUG_ON(1);
34033326d1b0SChris Mason 	}
34046567e837SChris Mason 
34056567e837SChris Mason 	/*
34066567e837SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
34076567e837SChris Mason 	 */
34086567e837SChris Mason 	/* first correct the data pointers */
34096567e837SChris Mason 	for (i = slot; i < nritems; i++) {
34105f39d397SChris Mason 		u32 ioff;
34115f39d397SChris Mason 		item = btrfs_item_nr(leaf, i);
3412db94535dSChris Mason 
34135f39d397SChris Mason 		ioff = btrfs_item_offset(leaf, item);
34145f39d397SChris Mason 		btrfs_set_item_offset(leaf, item, ioff - data_size);
34156567e837SChris Mason 	}
34165f39d397SChris Mason 
34176567e837SChris Mason 	/* shift the data */
34185f39d397SChris Mason 	memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
34196567e837SChris Mason 		      data_end - data_size, btrfs_leaf_data(leaf) +
34206567e837SChris Mason 		      data_end, old_data - data_end);
34215f39d397SChris Mason 
34226567e837SChris Mason 	data_end = old_data;
34235f39d397SChris Mason 	old_size = btrfs_item_size_nr(leaf, slot);
34245f39d397SChris Mason 	item = btrfs_item_nr(leaf, slot);
34255f39d397SChris Mason 	btrfs_set_item_size(leaf, item, old_size + data_size);
34265f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
34276567e837SChris Mason 
34285f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
34295f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
34306567e837SChris Mason 		BUG();
34315f39d397SChris Mason 	}
34321cd30799STsutomu Itoh 	return 0;
34336567e837SChris Mason }
34346567e837SChris Mason 
343574123bd7SChris Mason /*
3436d352ac68SChris Mason  * Given a key and some data, insert items into the tree.
343774123bd7SChris Mason  * This does all the path init required, making room in the tree if needed.
3438f3465ca4SJosef Bacik  * Returns the number of keys that were inserted.
3439f3465ca4SJosef Bacik  */
3440f3465ca4SJosef Bacik int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3441f3465ca4SJosef Bacik 			    struct btrfs_root *root,
3442f3465ca4SJosef Bacik 			    struct btrfs_path *path,
3443f3465ca4SJosef Bacik 			    struct btrfs_key *cpu_key, u32 *data_size,
3444f3465ca4SJosef Bacik 			    int nr)
3445f3465ca4SJosef Bacik {
3446f3465ca4SJosef Bacik 	struct extent_buffer *leaf;
3447f3465ca4SJosef Bacik 	struct btrfs_item *item;
3448f3465ca4SJosef Bacik 	int ret = 0;
3449f3465ca4SJosef Bacik 	int slot;
3450f3465ca4SJosef Bacik 	int i;
3451f3465ca4SJosef Bacik 	u32 nritems;
3452f3465ca4SJosef Bacik 	u32 total_data = 0;
3453f3465ca4SJosef Bacik 	u32 total_size = 0;
3454f3465ca4SJosef Bacik 	unsigned int data_end;
3455f3465ca4SJosef Bacik 	struct btrfs_disk_key disk_key;
3456f3465ca4SJosef Bacik 	struct btrfs_key found_key;
3457f3465ca4SJosef Bacik 
345887b29b20SYan Zheng 	for (i = 0; i < nr; i++) {
345987b29b20SYan Zheng 		if (total_size + data_size[i] + sizeof(struct btrfs_item) >
346087b29b20SYan Zheng 		    BTRFS_LEAF_DATA_SIZE(root)) {
346187b29b20SYan Zheng 			break;
346287b29b20SYan Zheng 			nr = i;
346387b29b20SYan Zheng 		}
3464f3465ca4SJosef Bacik 		total_data += data_size[i];
346587b29b20SYan Zheng 		total_size += data_size[i] + sizeof(struct btrfs_item);
346687b29b20SYan Zheng 	}
346787b29b20SYan Zheng 	BUG_ON(nr == 0);
3468f3465ca4SJosef Bacik 
3469f3465ca4SJosef Bacik 	ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3470f3465ca4SJosef Bacik 	if (ret == 0)
3471f3465ca4SJosef Bacik 		return -EEXIST;
3472f3465ca4SJosef Bacik 	if (ret < 0)
3473f3465ca4SJosef Bacik 		goto out;
3474f3465ca4SJosef Bacik 
3475f3465ca4SJosef Bacik 	leaf = path->nodes[0];
3476f3465ca4SJosef Bacik 
3477f3465ca4SJosef Bacik 	nritems = btrfs_header_nritems(leaf);
3478f3465ca4SJosef Bacik 	data_end = leaf_data_end(root, leaf);
3479f3465ca4SJosef Bacik 
3480f3465ca4SJosef Bacik 	if (btrfs_leaf_free_space(root, leaf) < total_size) {
3481f3465ca4SJosef Bacik 		for (i = nr; i >= 0; i--) {
3482f3465ca4SJosef Bacik 			total_data -= data_size[i];
3483f3465ca4SJosef Bacik 			total_size -= data_size[i] + sizeof(struct btrfs_item);
3484f3465ca4SJosef Bacik 			if (total_size < btrfs_leaf_free_space(root, leaf))
3485f3465ca4SJosef Bacik 				break;
3486f3465ca4SJosef Bacik 		}
3487f3465ca4SJosef Bacik 		nr = i;
3488f3465ca4SJosef Bacik 	}
3489f3465ca4SJosef Bacik 
3490f3465ca4SJosef Bacik 	slot = path->slots[0];
3491f3465ca4SJosef Bacik 	BUG_ON(slot < 0);
3492f3465ca4SJosef Bacik 
3493f3465ca4SJosef Bacik 	if (slot != nritems) {
3494f3465ca4SJosef Bacik 		unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3495f3465ca4SJosef Bacik 
3496f3465ca4SJosef Bacik 		item = btrfs_item_nr(leaf, slot);
3497f3465ca4SJosef Bacik 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
3498f3465ca4SJosef Bacik 
3499f3465ca4SJosef Bacik 		/* figure out how many keys we can insert in here */
3500f3465ca4SJosef Bacik 		total_data = data_size[0];
3501f3465ca4SJosef Bacik 		for (i = 1; i < nr; i++) {
35025d4f98a2SYan Zheng 			if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3503f3465ca4SJosef Bacik 				break;
3504f3465ca4SJosef Bacik 			total_data += data_size[i];
3505f3465ca4SJosef Bacik 		}
3506f3465ca4SJosef Bacik 		nr = i;
3507f3465ca4SJosef Bacik 
3508f3465ca4SJosef Bacik 		if (old_data < data_end) {
3509f3465ca4SJosef Bacik 			btrfs_print_leaf(root, leaf);
3510d397712bSChris Mason 			printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3511f3465ca4SJosef Bacik 			       slot, old_data, data_end);
3512f3465ca4SJosef Bacik 			BUG_ON(1);
3513f3465ca4SJosef Bacik 		}
3514f3465ca4SJosef Bacik 		/*
3515f3465ca4SJosef Bacik 		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3516f3465ca4SJosef Bacik 		 */
3517f3465ca4SJosef Bacik 		/* first correct the data pointers */
3518f3465ca4SJosef Bacik 		for (i = slot; i < nritems; i++) {
3519f3465ca4SJosef Bacik 			u32 ioff;
3520f3465ca4SJosef Bacik 
3521f3465ca4SJosef Bacik 			item = btrfs_item_nr(leaf, i);
3522f3465ca4SJosef Bacik 			ioff = btrfs_item_offset(leaf, item);
3523f3465ca4SJosef Bacik 			btrfs_set_item_offset(leaf, item, ioff - total_data);
3524f3465ca4SJosef Bacik 		}
3525f3465ca4SJosef Bacik 		/* shift the items */
3526f3465ca4SJosef Bacik 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3527f3465ca4SJosef Bacik 			      btrfs_item_nr_offset(slot),
3528f3465ca4SJosef Bacik 			      (nritems - slot) * sizeof(struct btrfs_item));
3529f3465ca4SJosef Bacik 
3530f3465ca4SJosef Bacik 		/* shift the data */
3531f3465ca4SJosef Bacik 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3532f3465ca4SJosef Bacik 			      data_end - total_data, btrfs_leaf_data(leaf) +
3533f3465ca4SJosef Bacik 			      data_end, old_data - data_end);
3534f3465ca4SJosef Bacik 		data_end = old_data;
3535f3465ca4SJosef Bacik 	} else {
3536f3465ca4SJosef Bacik 		/*
3537f3465ca4SJosef Bacik 		 * this sucks but it has to be done, if we are inserting at
3538f3465ca4SJosef Bacik 		 * the end of the leaf only insert 1 of the items, since we
3539f3465ca4SJosef Bacik 		 * have no way of knowing whats on the next leaf and we'd have
3540f3465ca4SJosef Bacik 		 * to drop our current locks to figure it out
3541f3465ca4SJosef Bacik 		 */
3542f3465ca4SJosef Bacik 		nr = 1;
3543f3465ca4SJosef Bacik 	}
3544f3465ca4SJosef Bacik 
3545f3465ca4SJosef Bacik 	/* setup the item for the new data */
3546f3465ca4SJosef Bacik 	for (i = 0; i < nr; i++) {
3547f3465ca4SJosef Bacik 		btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3548f3465ca4SJosef Bacik 		btrfs_set_item_key(leaf, &disk_key, slot + i);
3549f3465ca4SJosef Bacik 		item = btrfs_item_nr(leaf, slot + i);
3550f3465ca4SJosef Bacik 		btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3551f3465ca4SJosef Bacik 		data_end -= data_size[i];
3552f3465ca4SJosef Bacik 		btrfs_set_item_size(leaf, item, data_size[i]);
3553f3465ca4SJosef Bacik 	}
3554f3465ca4SJosef Bacik 	btrfs_set_header_nritems(leaf, nritems + nr);
3555f3465ca4SJosef Bacik 	btrfs_mark_buffer_dirty(leaf);
3556f3465ca4SJosef Bacik 
3557f3465ca4SJosef Bacik 	ret = 0;
3558f3465ca4SJosef Bacik 	if (slot == 0) {
3559f3465ca4SJosef Bacik 		btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3560f3465ca4SJosef Bacik 		ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3561f3465ca4SJosef Bacik 	}
3562f3465ca4SJosef Bacik 
3563f3465ca4SJosef Bacik 	if (btrfs_leaf_free_space(root, leaf) < 0) {
3564f3465ca4SJosef Bacik 		btrfs_print_leaf(root, leaf);
3565f3465ca4SJosef Bacik 		BUG();
3566f3465ca4SJosef Bacik 	}
3567f3465ca4SJosef Bacik out:
3568f3465ca4SJosef Bacik 	if (!ret)
3569f3465ca4SJosef Bacik 		ret = nr;
3570f3465ca4SJosef Bacik 	return ret;
3571f3465ca4SJosef Bacik }
3572f3465ca4SJosef Bacik 
3573f3465ca4SJosef Bacik /*
357444871b1bSChris Mason  * this is a helper for btrfs_insert_empty_items, the main goal here is
357544871b1bSChris Mason  * to save stack depth by doing the bulk of the work in a function
357644871b1bSChris Mason  * that doesn't call btrfs_search_slot
357774123bd7SChris Mason  */
357816cdcec7SMiao Xie int setup_items_for_insert(struct btrfs_trans_handle *trans,
357944871b1bSChris Mason 			   struct btrfs_root *root, struct btrfs_path *path,
35809c58309dSChris Mason 			   struct btrfs_key *cpu_key, u32 *data_size,
358144871b1bSChris Mason 			   u32 total_data, u32 total_size, int nr)
3582be0e5c09SChris Mason {
35835f39d397SChris Mason 	struct btrfs_item *item;
35849c58309dSChris Mason 	int i;
35857518a238SChris Mason 	u32 nritems;
3586be0e5c09SChris Mason 	unsigned int data_end;
3587e2fa7227SChris Mason 	struct btrfs_disk_key disk_key;
358844871b1bSChris Mason 	int ret;
358944871b1bSChris Mason 	struct extent_buffer *leaf;
359044871b1bSChris Mason 	int slot;
3591e2fa7227SChris Mason 
35925f39d397SChris Mason 	leaf = path->nodes[0];
359344871b1bSChris Mason 	slot = path->slots[0];
359474123bd7SChris Mason 
35955f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3596123abc88SChris Mason 	data_end = leaf_data_end(root, leaf);
3597eb60ceacSChris Mason 
3598f25956ccSChris Mason 	if (btrfs_leaf_free_space(root, leaf) < total_size) {
35993326d1b0SChris Mason 		btrfs_print_leaf(root, leaf);
3600d397712bSChris Mason 		printk(KERN_CRIT "not enough freespace need %u have %d\n",
36019c58309dSChris Mason 		       total_size, btrfs_leaf_free_space(root, leaf));
3602be0e5c09SChris Mason 		BUG();
3603d4dbff95SChris Mason 	}
36045f39d397SChris Mason 
3605be0e5c09SChris Mason 	if (slot != nritems) {
36065f39d397SChris Mason 		unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3607be0e5c09SChris Mason 
36085f39d397SChris Mason 		if (old_data < data_end) {
36095f39d397SChris Mason 			btrfs_print_leaf(root, leaf);
3610d397712bSChris Mason 			printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
36115f39d397SChris Mason 			       slot, old_data, data_end);
36125f39d397SChris Mason 			BUG_ON(1);
36135f39d397SChris Mason 		}
3614be0e5c09SChris Mason 		/*
3615be0e5c09SChris Mason 		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3616be0e5c09SChris Mason 		 */
3617be0e5c09SChris Mason 		/* first correct the data pointers */
36180783fcfcSChris Mason 		for (i = slot; i < nritems; i++) {
36195f39d397SChris Mason 			u32 ioff;
3620db94535dSChris Mason 
36215f39d397SChris Mason 			item = btrfs_item_nr(leaf, i);
36225f39d397SChris Mason 			ioff = btrfs_item_offset(leaf, item);
36239c58309dSChris Mason 			btrfs_set_item_offset(leaf, item, ioff - total_data);
36240783fcfcSChris Mason 		}
3625be0e5c09SChris Mason 		/* shift the items */
36269c58309dSChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
36275f39d397SChris Mason 			      btrfs_item_nr_offset(slot),
36280783fcfcSChris Mason 			      (nritems - slot) * sizeof(struct btrfs_item));
3629be0e5c09SChris Mason 
3630be0e5c09SChris Mason 		/* shift the data */
36315f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
36329c58309dSChris Mason 			      data_end - total_data, btrfs_leaf_data(leaf) +
3633be0e5c09SChris Mason 			      data_end, old_data - data_end);
3634be0e5c09SChris Mason 		data_end = old_data;
3635be0e5c09SChris Mason 	}
36365f39d397SChris Mason 
363762e2749eSChris Mason 	/* setup the item for the new data */
36389c58309dSChris Mason 	for (i = 0; i < nr; i++) {
36399c58309dSChris Mason 		btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
36409c58309dSChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot + i);
36419c58309dSChris Mason 		item = btrfs_item_nr(leaf, slot + i);
36429c58309dSChris Mason 		btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
36439c58309dSChris Mason 		data_end -= data_size[i];
36449c58309dSChris Mason 		btrfs_set_item_size(leaf, item, data_size[i]);
36459c58309dSChris Mason 	}
364644871b1bSChris Mason 
36479c58309dSChris Mason 	btrfs_set_header_nritems(leaf, nritems + nr);
3648aa5d6bedSChris Mason 
3649aa5d6bedSChris Mason 	ret = 0;
36505a01a2e3SChris Mason 	if (slot == 0) {
36515a01a2e3SChris Mason 		btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3652e089f05cSChris Mason 		ret = fixup_low_keys(trans, root, path, &disk_key, 1);
36535a01a2e3SChris Mason 	}
3654b9473439SChris Mason 	btrfs_unlock_up_safe(path, 1);
3655b9473439SChris Mason 	btrfs_mark_buffer_dirty(leaf);
3656aa5d6bedSChris Mason 
36575f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
36585f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
3659be0e5c09SChris Mason 		BUG();
36605f39d397SChris Mason 	}
366144871b1bSChris Mason 	return ret;
366244871b1bSChris Mason }
366344871b1bSChris Mason 
366444871b1bSChris Mason /*
366544871b1bSChris Mason  * Given a key and some data, insert items into the tree.
366644871b1bSChris Mason  * This does all the path init required, making room in the tree if needed.
366744871b1bSChris Mason  */
366844871b1bSChris Mason int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
366944871b1bSChris Mason 			    struct btrfs_root *root,
367044871b1bSChris Mason 			    struct btrfs_path *path,
367144871b1bSChris Mason 			    struct btrfs_key *cpu_key, u32 *data_size,
367244871b1bSChris Mason 			    int nr)
367344871b1bSChris Mason {
367444871b1bSChris Mason 	int ret = 0;
367544871b1bSChris Mason 	int slot;
367644871b1bSChris Mason 	int i;
367744871b1bSChris Mason 	u32 total_size = 0;
367844871b1bSChris Mason 	u32 total_data = 0;
367944871b1bSChris Mason 
368044871b1bSChris Mason 	for (i = 0; i < nr; i++)
368144871b1bSChris Mason 		total_data += data_size[i];
368244871b1bSChris Mason 
368344871b1bSChris Mason 	total_size = total_data + (nr * sizeof(struct btrfs_item));
368444871b1bSChris Mason 	ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
368544871b1bSChris Mason 	if (ret == 0)
368644871b1bSChris Mason 		return -EEXIST;
368744871b1bSChris Mason 	if (ret < 0)
368844871b1bSChris Mason 		goto out;
368944871b1bSChris Mason 
369044871b1bSChris Mason 	slot = path->slots[0];
369144871b1bSChris Mason 	BUG_ON(slot < 0);
369244871b1bSChris Mason 
369344871b1bSChris Mason 	ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
369444871b1bSChris Mason 			       total_data, total_size, nr);
369544871b1bSChris Mason 
3696ed2ff2cbSChris Mason out:
369762e2749eSChris Mason 	return ret;
369862e2749eSChris Mason }
369962e2749eSChris Mason 
370062e2749eSChris Mason /*
370162e2749eSChris Mason  * Given a key and some data, insert an item into the tree.
370262e2749eSChris Mason  * This does all the path init required, making room in the tree if needed.
370362e2749eSChris Mason  */
3704e089f05cSChris Mason int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3705e089f05cSChris Mason 		      *root, struct btrfs_key *cpu_key, void *data, u32
3706e089f05cSChris Mason 		      data_size)
370762e2749eSChris Mason {
370862e2749eSChris Mason 	int ret = 0;
37092c90e5d6SChris Mason 	struct btrfs_path *path;
37105f39d397SChris Mason 	struct extent_buffer *leaf;
37115f39d397SChris Mason 	unsigned long ptr;
371262e2749eSChris Mason 
37132c90e5d6SChris Mason 	path = btrfs_alloc_path();
3714db5b493aSTsutomu Itoh 	if (!path)
3715db5b493aSTsutomu Itoh 		return -ENOMEM;
37162c90e5d6SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
371762e2749eSChris Mason 	if (!ret) {
37185f39d397SChris Mason 		leaf = path->nodes[0];
37195f39d397SChris Mason 		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
37205f39d397SChris Mason 		write_extent_buffer(leaf, data, ptr, data_size);
37215f39d397SChris Mason 		btrfs_mark_buffer_dirty(leaf);
372262e2749eSChris Mason 	}
37232c90e5d6SChris Mason 	btrfs_free_path(path);
3724aa5d6bedSChris Mason 	return ret;
3725be0e5c09SChris Mason }
3726be0e5c09SChris Mason 
372774123bd7SChris Mason /*
37285de08d7dSChris Mason  * delete the pointer from a given node.
372974123bd7SChris Mason  *
3730d352ac68SChris Mason  * the tree should have been previously balanced so the deletion does not
3731d352ac68SChris Mason  * empty a node.
373274123bd7SChris Mason  */
3733e089f05cSChris Mason static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3734e089f05cSChris Mason 		   struct btrfs_path *path, int level, int slot)
3735be0e5c09SChris Mason {
37365f39d397SChris Mason 	struct extent_buffer *parent = path->nodes[level];
37377518a238SChris Mason 	u32 nritems;
3738aa5d6bedSChris Mason 	int ret = 0;
3739bb803951SChris Mason 	int wret;
3740be0e5c09SChris Mason 
37415f39d397SChris Mason 	nritems = btrfs_header_nritems(parent);
3742be0e5c09SChris Mason 	if (slot != nritems - 1) {
37435f39d397SChris Mason 		memmove_extent_buffer(parent,
37445f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot),
37455f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot + 1),
3746d6025579SChris Mason 			      sizeof(struct btrfs_key_ptr) *
3747d6025579SChris Mason 			      (nritems - slot - 1));
3748be0e5c09SChris Mason 	}
37497518a238SChris Mason 	nritems--;
37505f39d397SChris Mason 	btrfs_set_header_nritems(parent, nritems);
37517518a238SChris Mason 	if (nritems == 0 && parent == root->node) {
37525f39d397SChris Mason 		BUG_ON(btrfs_header_level(root->node) != 1);
3753eb60ceacSChris Mason 		/* just turn the root into a leaf and break */
37545f39d397SChris Mason 		btrfs_set_header_level(root->node, 0);
3755bb803951SChris Mason 	} else if (slot == 0) {
37565f39d397SChris Mason 		struct btrfs_disk_key disk_key;
37575f39d397SChris Mason 
37585f39d397SChris Mason 		btrfs_node_key(parent, &disk_key, 0);
37595f39d397SChris Mason 		wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
37600f70abe2SChris Mason 		if (wret)
37610f70abe2SChris Mason 			ret = wret;
3762be0e5c09SChris Mason 	}
3763d6025579SChris Mason 	btrfs_mark_buffer_dirty(parent);
3764aa5d6bedSChris Mason 	return ret;
3765be0e5c09SChris Mason }
3766be0e5c09SChris Mason 
376774123bd7SChris Mason /*
3768323ac95bSChris Mason  * a helper function to delete the leaf pointed to by path->slots[1] and
37695d4f98a2SYan Zheng  * path->nodes[1].
3770323ac95bSChris Mason  *
3771323ac95bSChris Mason  * This deletes the pointer in path->nodes[1] and frees the leaf
3772323ac95bSChris Mason  * block extent.  zero is returned if it all worked out, < 0 otherwise.
3773323ac95bSChris Mason  *
3774323ac95bSChris Mason  * The path must have already been setup for deleting the leaf, including
3775323ac95bSChris Mason  * all the proper balancing.  path->nodes[1] must be locked.
3776323ac95bSChris Mason  */
37775d4f98a2SYan Zheng static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3778323ac95bSChris Mason 				   struct btrfs_root *root,
37795d4f98a2SYan Zheng 				   struct btrfs_path *path,
37805d4f98a2SYan Zheng 				   struct extent_buffer *leaf)
3781323ac95bSChris Mason {
3782323ac95bSChris Mason 	int ret;
3783323ac95bSChris Mason 
37845d4f98a2SYan Zheng 	WARN_ON(btrfs_header_generation(leaf) != trans->transid);
3785323ac95bSChris Mason 	ret = del_ptr(trans, root, path, 1, path->slots[1]);
3786323ac95bSChris Mason 	if (ret)
3787323ac95bSChris Mason 		return ret;
3788323ac95bSChris Mason 
37894d081c41SChris Mason 	/*
37904d081c41SChris Mason 	 * btrfs_free_extent is expensive, we want to make sure we
37914d081c41SChris Mason 	 * aren't holding any locks when we call it
37924d081c41SChris Mason 	 */
37934d081c41SChris Mason 	btrfs_unlock_up_safe(path, 0);
37944d081c41SChris Mason 
3795f0486c68SYan, Zheng 	root_sub_used(root, leaf->len);
3796f0486c68SYan, Zheng 
3797*3083ee2eSJosef Bacik 	extent_buffer_get(leaf);
379866d7e7f0SArne Jansen 	btrfs_free_tree_block(trans, root, leaf, 0, 1, 0);
3799*3083ee2eSJosef Bacik 	free_extent_buffer_stale(leaf);
3800f0486c68SYan, Zheng 	return 0;
3801323ac95bSChris Mason }
3802323ac95bSChris Mason /*
380374123bd7SChris Mason  * delete the item at the leaf level in path.  If that empties
380474123bd7SChris Mason  * the leaf, remove it from the tree
380574123bd7SChris Mason  */
380685e21bacSChris Mason int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
380785e21bacSChris Mason 		    struct btrfs_path *path, int slot, int nr)
3808be0e5c09SChris Mason {
38095f39d397SChris Mason 	struct extent_buffer *leaf;
38105f39d397SChris Mason 	struct btrfs_item *item;
381185e21bacSChris Mason 	int last_off;
381285e21bacSChris Mason 	int dsize = 0;
3813aa5d6bedSChris Mason 	int ret = 0;
3814aa5d6bedSChris Mason 	int wret;
381585e21bacSChris Mason 	int i;
38167518a238SChris Mason 	u32 nritems;
3817be0e5c09SChris Mason 
38185f39d397SChris Mason 	leaf = path->nodes[0];
381985e21bacSChris Mason 	last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
382085e21bacSChris Mason 
382185e21bacSChris Mason 	for (i = 0; i < nr; i++)
382285e21bacSChris Mason 		dsize += btrfs_item_size_nr(leaf, slot + i);
382385e21bacSChris Mason 
38245f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3825be0e5c09SChris Mason 
382685e21bacSChris Mason 	if (slot + nr != nritems) {
3827123abc88SChris Mason 		int data_end = leaf_data_end(root, leaf);
38285f39d397SChris Mason 
38295f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3830d6025579SChris Mason 			      data_end + dsize,
3831123abc88SChris Mason 			      btrfs_leaf_data(leaf) + data_end,
383285e21bacSChris Mason 			      last_off - data_end);
38335f39d397SChris Mason 
383485e21bacSChris Mason 		for (i = slot + nr; i < nritems; i++) {
38355f39d397SChris Mason 			u32 ioff;
3836db94535dSChris Mason 
38375f39d397SChris Mason 			item = btrfs_item_nr(leaf, i);
38385f39d397SChris Mason 			ioff = btrfs_item_offset(leaf, item);
38395f39d397SChris Mason 			btrfs_set_item_offset(leaf, item, ioff + dsize);
38400783fcfcSChris Mason 		}
3841db94535dSChris Mason 
38425f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
384385e21bacSChris Mason 			      btrfs_item_nr_offset(slot + nr),
38440783fcfcSChris Mason 			      sizeof(struct btrfs_item) *
384585e21bacSChris Mason 			      (nritems - slot - nr));
3846be0e5c09SChris Mason 	}
384785e21bacSChris Mason 	btrfs_set_header_nritems(leaf, nritems - nr);
384885e21bacSChris Mason 	nritems -= nr;
38495f39d397SChris Mason 
385074123bd7SChris Mason 	/* delete the leaf if we've emptied it */
38517518a238SChris Mason 	if (nritems == 0) {
38525f39d397SChris Mason 		if (leaf == root->node) {
38535f39d397SChris Mason 			btrfs_set_header_level(leaf, 0);
38549a8dd150SChris Mason 		} else {
3855f0486c68SYan, Zheng 			btrfs_set_path_blocking(path);
3856f0486c68SYan, Zheng 			clean_tree_block(trans, root, leaf);
38575d4f98a2SYan Zheng 			ret = btrfs_del_leaf(trans, root, path, leaf);
3858323ac95bSChris Mason 			BUG_ON(ret);
38599a8dd150SChris Mason 		}
3860be0e5c09SChris Mason 	} else {
38617518a238SChris Mason 		int used = leaf_space_used(leaf, 0, nritems);
3862aa5d6bedSChris Mason 		if (slot == 0) {
38635f39d397SChris Mason 			struct btrfs_disk_key disk_key;
38645f39d397SChris Mason 
38655f39d397SChris Mason 			btrfs_item_key(leaf, &disk_key, 0);
3866e089f05cSChris Mason 			wret = fixup_low_keys(trans, root, path,
38675f39d397SChris Mason 					      &disk_key, 1);
3868aa5d6bedSChris Mason 			if (wret)
3869aa5d6bedSChris Mason 				ret = wret;
3870aa5d6bedSChris Mason 		}
3871aa5d6bedSChris Mason 
387274123bd7SChris Mason 		/* delete the leaf if it is mostly empty */
3873d717aa1dSYan Zheng 		if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
3874be0e5c09SChris Mason 			/* push_leaf_left fixes the path.
3875be0e5c09SChris Mason 			 * make sure the path still points to our leaf
3876be0e5c09SChris Mason 			 * for possible call to del_ptr below
3877be0e5c09SChris Mason 			 */
38784920c9acSChris Mason 			slot = path->slots[1];
38795f39d397SChris Mason 			extent_buffer_get(leaf);
38805f39d397SChris Mason 
3881b9473439SChris Mason 			btrfs_set_path_blocking(path);
388299d8f83cSChris Mason 			wret = push_leaf_left(trans, root, path, 1, 1,
388399d8f83cSChris Mason 					      1, (u32)-1);
388454aa1f4dSChris Mason 			if (wret < 0 && wret != -ENOSPC)
3885aa5d6bedSChris Mason 				ret = wret;
38865f39d397SChris Mason 
38875f39d397SChris Mason 			if (path->nodes[0] == leaf &&
38885f39d397SChris Mason 			    btrfs_header_nritems(leaf)) {
388999d8f83cSChris Mason 				wret = push_leaf_right(trans, root, path, 1,
389099d8f83cSChris Mason 						       1, 1, 0);
389154aa1f4dSChris Mason 				if (wret < 0 && wret != -ENOSPC)
3892aa5d6bedSChris Mason 					ret = wret;
3893aa5d6bedSChris Mason 			}
38945f39d397SChris Mason 
38955f39d397SChris Mason 			if (btrfs_header_nritems(leaf) == 0) {
3896323ac95bSChris Mason 				path->slots[1] = slot;
38975d4f98a2SYan Zheng 				ret = btrfs_del_leaf(trans, root, path, leaf);
3898323ac95bSChris Mason 				BUG_ON(ret);
38995f39d397SChris Mason 				free_extent_buffer(leaf);
39005de08d7dSChris Mason 			} else {
3901925baeddSChris Mason 				/* if we're still in the path, make sure
3902925baeddSChris Mason 				 * we're dirty.  Otherwise, one of the
3903925baeddSChris Mason 				 * push_leaf functions must have already
3904925baeddSChris Mason 				 * dirtied this buffer
3905925baeddSChris Mason 				 */
3906925baeddSChris Mason 				if (path->nodes[0] == leaf)
39075f39d397SChris Mason 					btrfs_mark_buffer_dirty(leaf);
39085f39d397SChris Mason 				free_extent_buffer(leaf);
3909be0e5c09SChris Mason 			}
3910d5719762SChris Mason 		} else {
39115f39d397SChris Mason 			btrfs_mark_buffer_dirty(leaf);
3912be0e5c09SChris Mason 		}
39139a8dd150SChris Mason 	}
3914aa5d6bedSChris Mason 	return ret;
39159a8dd150SChris Mason }
39169a8dd150SChris Mason 
391797571fd0SChris Mason /*
3918925baeddSChris Mason  * search the tree again to find a leaf with lesser keys
39197bb86316SChris Mason  * returns 0 if it found something or 1 if there are no lesser leaves.
39207bb86316SChris Mason  * returns < 0 on io errors.
3921d352ac68SChris Mason  *
3922d352ac68SChris Mason  * This may release the path, and so you may lose any locks held at the
3923d352ac68SChris Mason  * time you call it.
39247bb86316SChris Mason  */
39257bb86316SChris Mason int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
39267bb86316SChris Mason {
3927925baeddSChris Mason 	struct btrfs_key key;
3928925baeddSChris Mason 	struct btrfs_disk_key found_key;
3929925baeddSChris Mason 	int ret;
39307bb86316SChris Mason 
3931925baeddSChris Mason 	btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
3932925baeddSChris Mason 
3933925baeddSChris Mason 	if (key.offset > 0)
3934925baeddSChris Mason 		key.offset--;
3935925baeddSChris Mason 	else if (key.type > 0)
3936925baeddSChris Mason 		key.type--;
3937925baeddSChris Mason 	else if (key.objectid > 0)
3938925baeddSChris Mason 		key.objectid--;
3939925baeddSChris Mason 	else
39407bb86316SChris Mason 		return 1;
39417bb86316SChris Mason 
3942b3b4aa74SDavid Sterba 	btrfs_release_path(path);
3943925baeddSChris Mason 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3944925baeddSChris Mason 	if (ret < 0)
3945925baeddSChris Mason 		return ret;
3946925baeddSChris Mason 	btrfs_item_key(path->nodes[0], &found_key, 0);
3947925baeddSChris Mason 	ret = comp_keys(&found_key, &key);
3948925baeddSChris Mason 	if (ret < 0)
39497bb86316SChris Mason 		return 0;
3950925baeddSChris Mason 	return 1;
39517bb86316SChris Mason }
39527bb86316SChris Mason 
39533f157a2fSChris Mason /*
39543f157a2fSChris Mason  * A helper function to walk down the tree starting at min_key, and looking
39553f157a2fSChris Mason  * for nodes or leaves that are either in cache or have a minimum
3956d352ac68SChris Mason  * transaction id.  This is used by the btree defrag code, and tree logging
39573f157a2fSChris Mason  *
39583f157a2fSChris Mason  * This does not cow, but it does stuff the starting key it finds back
39593f157a2fSChris Mason  * into min_key, so you can call btrfs_search_slot with cow=1 on the
39603f157a2fSChris Mason  * key and get a writable path.
39613f157a2fSChris Mason  *
39623f157a2fSChris Mason  * This does lock as it descends, and path->keep_locks should be set
39633f157a2fSChris Mason  * to 1 by the caller.
39643f157a2fSChris Mason  *
39653f157a2fSChris Mason  * This honors path->lowest_level to prevent descent past a given level
39663f157a2fSChris Mason  * of the tree.
39673f157a2fSChris Mason  *
3968d352ac68SChris Mason  * min_trans indicates the oldest transaction that you are interested
3969d352ac68SChris Mason  * in walking through.  Any nodes or leaves older than min_trans are
3970d352ac68SChris Mason  * skipped over (without reading them).
3971d352ac68SChris Mason  *
39723f157a2fSChris Mason  * returns zero if something useful was found, < 0 on error and 1 if there
39733f157a2fSChris Mason  * was nothing in the tree that matched the search criteria.
39743f157a2fSChris Mason  */
39753f157a2fSChris Mason int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
3976e02119d5SChris Mason 			 struct btrfs_key *max_key,
39773f157a2fSChris Mason 			 struct btrfs_path *path, int cache_only,
39783f157a2fSChris Mason 			 u64 min_trans)
39793f157a2fSChris Mason {
39803f157a2fSChris Mason 	struct extent_buffer *cur;
39813f157a2fSChris Mason 	struct btrfs_key found_key;
39823f157a2fSChris Mason 	int slot;
39839652480bSYan 	int sret;
39843f157a2fSChris Mason 	u32 nritems;
39853f157a2fSChris Mason 	int level;
39863f157a2fSChris Mason 	int ret = 1;
39873f157a2fSChris Mason 
3988934d375bSChris Mason 	WARN_ON(!path->keep_locks);
39893f157a2fSChris Mason again:
3990bd681513SChris Mason 	cur = btrfs_read_lock_root_node(root);
39913f157a2fSChris Mason 	level = btrfs_header_level(cur);
3992e02119d5SChris Mason 	WARN_ON(path->nodes[level]);
39933f157a2fSChris Mason 	path->nodes[level] = cur;
3994bd681513SChris Mason 	path->locks[level] = BTRFS_READ_LOCK;
39953f157a2fSChris Mason 
39963f157a2fSChris Mason 	if (btrfs_header_generation(cur) < min_trans) {
39973f157a2fSChris Mason 		ret = 1;
39983f157a2fSChris Mason 		goto out;
39993f157a2fSChris Mason 	}
40003f157a2fSChris Mason 	while (1) {
40013f157a2fSChris Mason 		nritems = btrfs_header_nritems(cur);
40023f157a2fSChris Mason 		level = btrfs_header_level(cur);
40039652480bSYan 		sret = bin_search(cur, min_key, level, &slot);
40043f157a2fSChris Mason 
4005323ac95bSChris Mason 		/* at the lowest level, we're done, setup the path and exit */
4006323ac95bSChris Mason 		if (level == path->lowest_level) {
4007e02119d5SChris Mason 			if (slot >= nritems)
4008e02119d5SChris Mason 				goto find_next_key;
40093f157a2fSChris Mason 			ret = 0;
40103f157a2fSChris Mason 			path->slots[level] = slot;
40113f157a2fSChris Mason 			btrfs_item_key_to_cpu(cur, &found_key, slot);
40123f157a2fSChris Mason 			goto out;
40133f157a2fSChris Mason 		}
40149652480bSYan 		if (sret && slot > 0)
40159652480bSYan 			slot--;
40163f157a2fSChris Mason 		/*
40173f157a2fSChris Mason 		 * check this node pointer against the cache_only and
40183f157a2fSChris Mason 		 * min_trans parameters.  If it isn't in cache or is too
40193f157a2fSChris Mason 		 * old, skip to the next one.
40203f157a2fSChris Mason 		 */
40213f157a2fSChris Mason 		while (slot < nritems) {
40223f157a2fSChris Mason 			u64 blockptr;
40233f157a2fSChris Mason 			u64 gen;
40243f157a2fSChris Mason 			struct extent_buffer *tmp;
4025e02119d5SChris Mason 			struct btrfs_disk_key disk_key;
4026e02119d5SChris Mason 
40273f157a2fSChris Mason 			blockptr = btrfs_node_blockptr(cur, slot);
40283f157a2fSChris Mason 			gen = btrfs_node_ptr_generation(cur, slot);
40293f157a2fSChris Mason 			if (gen < min_trans) {
40303f157a2fSChris Mason 				slot++;
40313f157a2fSChris Mason 				continue;
40323f157a2fSChris Mason 			}
40333f157a2fSChris Mason 			if (!cache_only)
40343f157a2fSChris Mason 				break;
40353f157a2fSChris Mason 
4036e02119d5SChris Mason 			if (max_key) {
4037e02119d5SChris Mason 				btrfs_node_key(cur, &disk_key, slot);
4038e02119d5SChris Mason 				if (comp_keys(&disk_key, max_key) >= 0) {
4039e02119d5SChris Mason 					ret = 1;
4040e02119d5SChris Mason 					goto out;
4041e02119d5SChris Mason 				}
4042e02119d5SChris Mason 			}
4043e02119d5SChris Mason 
40443f157a2fSChris Mason 			tmp = btrfs_find_tree_block(root, blockptr,
40453f157a2fSChris Mason 					    btrfs_level_size(root, level - 1));
40463f157a2fSChris Mason 
40473f157a2fSChris Mason 			if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
40483f157a2fSChris Mason 				free_extent_buffer(tmp);
40493f157a2fSChris Mason 				break;
40503f157a2fSChris Mason 			}
40513f157a2fSChris Mason 			if (tmp)
40523f157a2fSChris Mason 				free_extent_buffer(tmp);
40533f157a2fSChris Mason 			slot++;
40543f157a2fSChris Mason 		}
4055e02119d5SChris Mason find_next_key:
40563f157a2fSChris Mason 		/*
40573f157a2fSChris Mason 		 * we didn't find a candidate key in this node, walk forward
40583f157a2fSChris Mason 		 * and find another one
40593f157a2fSChris Mason 		 */
40603f157a2fSChris Mason 		if (slot >= nritems) {
4061e02119d5SChris Mason 			path->slots[level] = slot;
4062b4ce94deSChris Mason 			btrfs_set_path_blocking(path);
4063e02119d5SChris Mason 			sret = btrfs_find_next_key(root, path, min_key, level,
40643f157a2fSChris Mason 						  cache_only, min_trans);
4065e02119d5SChris Mason 			if (sret == 0) {
4066b3b4aa74SDavid Sterba 				btrfs_release_path(path);
40673f157a2fSChris Mason 				goto again;
40683f157a2fSChris Mason 			} else {
40693f157a2fSChris Mason 				goto out;
40703f157a2fSChris Mason 			}
40713f157a2fSChris Mason 		}
40723f157a2fSChris Mason 		/* save our key for returning back */
40733f157a2fSChris Mason 		btrfs_node_key_to_cpu(cur, &found_key, slot);
40743f157a2fSChris Mason 		path->slots[level] = slot;
40753f157a2fSChris Mason 		if (level == path->lowest_level) {
40763f157a2fSChris Mason 			ret = 0;
40773f157a2fSChris Mason 			unlock_up(path, level, 1);
40783f157a2fSChris Mason 			goto out;
40793f157a2fSChris Mason 		}
4080b4ce94deSChris Mason 		btrfs_set_path_blocking(path);
40813f157a2fSChris Mason 		cur = read_node_slot(root, cur, slot);
408297d9a8a4STsutomu Itoh 		BUG_ON(!cur);
40833f157a2fSChris Mason 
4084bd681513SChris Mason 		btrfs_tree_read_lock(cur);
4085b4ce94deSChris Mason 
4086bd681513SChris Mason 		path->locks[level - 1] = BTRFS_READ_LOCK;
40873f157a2fSChris Mason 		path->nodes[level - 1] = cur;
40883f157a2fSChris Mason 		unlock_up(path, level, 1);
4089bd681513SChris Mason 		btrfs_clear_path_blocking(path, NULL, 0);
40903f157a2fSChris Mason 	}
40913f157a2fSChris Mason out:
40923f157a2fSChris Mason 	if (ret == 0)
40933f157a2fSChris Mason 		memcpy(min_key, &found_key, sizeof(found_key));
4094b4ce94deSChris Mason 	btrfs_set_path_blocking(path);
40953f157a2fSChris Mason 	return ret;
40963f157a2fSChris Mason }
40973f157a2fSChris Mason 
40983f157a2fSChris Mason /*
40993f157a2fSChris Mason  * this is similar to btrfs_next_leaf, but does not try to preserve
41003f157a2fSChris Mason  * and fixup the path.  It looks for and returns the next key in the
41013f157a2fSChris Mason  * tree based on the current path and the cache_only and min_trans
41023f157a2fSChris Mason  * parameters.
41033f157a2fSChris Mason  *
41043f157a2fSChris Mason  * 0 is returned if another key is found, < 0 if there are any errors
41053f157a2fSChris Mason  * and 1 is returned if there are no higher keys in the tree
41063f157a2fSChris Mason  *
41073f157a2fSChris Mason  * path->keep_locks should be set to 1 on the search made before
41083f157a2fSChris Mason  * calling this function.
41093f157a2fSChris Mason  */
4110e7a84565SChris Mason int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
411133c66f43SYan Zheng 			struct btrfs_key *key, int level,
41123f157a2fSChris Mason 			int cache_only, u64 min_trans)
4113e7a84565SChris Mason {
4114e7a84565SChris Mason 	int slot;
4115e7a84565SChris Mason 	struct extent_buffer *c;
4116e7a84565SChris Mason 
4117934d375bSChris Mason 	WARN_ON(!path->keep_locks);
4118e7a84565SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
4119e7a84565SChris Mason 		if (!path->nodes[level])
4120e7a84565SChris Mason 			return 1;
4121e7a84565SChris Mason 
4122e7a84565SChris Mason 		slot = path->slots[level] + 1;
4123e7a84565SChris Mason 		c = path->nodes[level];
41243f157a2fSChris Mason next:
4125e7a84565SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
412633c66f43SYan Zheng 			int ret;
412733c66f43SYan Zheng 			int orig_lowest;
412833c66f43SYan Zheng 			struct btrfs_key cur_key;
412933c66f43SYan Zheng 			if (level + 1 >= BTRFS_MAX_LEVEL ||
413033c66f43SYan Zheng 			    !path->nodes[level + 1])
4131e7a84565SChris Mason 				return 1;
413233c66f43SYan Zheng 
413333c66f43SYan Zheng 			if (path->locks[level + 1]) {
413433c66f43SYan Zheng 				level++;
4135e7a84565SChris Mason 				continue;
4136e7a84565SChris Mason 			}
413733c66f43SYan Zheng 
413833c66f43SYan Zheng 			slot = btrfs_header_nritems(c) - 1;
413933c66f43SYan Zheng 			if (level == 0)
414033c66f43SYan Zheng 				btrfs_item_key_to_cpu(c, &cur_key, slot);
414133c66f43SYan Zheng 			else
414233c66f43SYan Zheng 				btrfs_node_key_to_cpu(c, &cur_key, slot);
414333c66f43SYan Zheng 
414433c66f43SYan Zheng 			orig_lowest = path->lowest_level;
4145b3b4aa74SDavid Sterba 			btrfs_release_path(path);
414633c66f43SYan Zheng 			path->lowest_level = level;
414733c66f43SYan Zheng 			ret = btrfs_search_slot(NULL, root, &cur_key, path,
414833c66f43SYan Zheng 						0, 0);
414933c66f43SYan Zheng 			path->lowest_level = orig_lowest;
415033c66f43SYan Zheng 			if (ret < 0)
415133c66f43SYan Zheng 				return ret;
415233c66f43SYan Zheng 
415333c66f43SYan Zheng 			c = path->nodes[level];
415433c66f43SYan Zheng 			slot = path->slots[level];
415533c66f43SYan Zheng 			if (ret == 0)
415633c66f43SYan Zheng 				slot++;
415733c66f43SYan Zheng 			goto next;
415833c66f43SYan Zheng 		}
415933c66f43SYan Zheng 
4160e7a84565SChris Mason 		if (level == 0)
4161e7a84565SChris Mason 			btrfs_item_key_to_cpu(c, key, slot);
41623f157a2fSChris Mason 		else {
41633f157a2fSChris Mason 			u64 blockptr = btrfs_node_blockptr(c, slot);
41643f157a2fSChris Mason 			u64 gen = btrfs_node_ptr_generation(c, slot);
41653f157a2fSChris Mason 
41663f157a2fSChris Mason 			if (cache_only) {
41673f157a2fSChris Mason 				struct extent_buffer *cur;
41683f157a2fSChris Mason 				cur = btrfs_find_tree_block(root, blockptr,
41693f157a2fSChris Mason 					    btrfs_level_size(root, level - 1));
41703f157a2fSChris Mason 				if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
41713f157a2fSChris Mason 					slot++;
41723f157a2fSChris Mason 					if (cur)
41733f157a2fSChris Mason 						free_extent_buffer(cur);
41743f157a2fSChris Mason 					goto next;
41753f157a2fSChris Mason 				}
41763f157a2fSChris Mason 				free_extent_buffer(cur);
41773f157a2fSChris Mason 			}
41783f157a2fSChris Mason 			if (gen < min_trans) {
41793f157a2fSChris Mason 				slot++;
41803f157a2fSChris Mason 				goto next;
41813f157a2fSChris Mason 			}
4182e7a84565SChris Mason 			btrfs_node_key_to_cpu(c, key, slot);
41833f157a2fSChris Mason 		}
4184e7a84565SChris Mason 		return 0;
4185e7a84565SChris Mason 	}
4186e7a84565SChris Mason 	return 1;
4187e7a84565SChris Mason }
4188e7a84565SChris Mason 
41897bb86316SChris Mason /*
4190925baeddSChris Mason  * search the tree again to find a leaf with greater keys
41910f70abe2SChris Mason  * returns 0 if it found something or 1 if there are no greater leaves.
41920f70abe2SChris Mason  * returns < 0 on io errors.
419397571fd0SChris Mason  */
4194234b63a0SChris Mason int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
4195d97e63b6SChris Mason {
4196d97e63b6SChris Mason 	int slot;
41978e73f275SChris Mason 	int level;
41985f39d397SChris Mason 	struct extent_buffer *c;
41998e73f275SChris Mason 	struct extent_buffer *next;
4200925baeddSChris Mason 	struct btrfs_key key;
4201925baeddSChris Mason 	u32 nritems;
4202925baeddSChris Mason 	int ret;
42038e73f275SChris Mason 	int old_spinning = path->leave_spinning;
4204bd681513SChris Mason 	int next_rw_lock = 0;
4205925baeddSChris Mason 
4206925baeddSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
4207d397712bSChris Mason 	if (nritems == 0)
4208925baeddSChris Mason 		return 1;
4209925baeddSChris Mason 
42108e73f275SChris Mason 	btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
42118e73f275SChris Mason again:
42128e73f275SChris Mason 	level = 1;
42138e73f275SChris Mason 	next = NULL;
4214bd681513SChris Mason 	next_rw_lock = 0;
4215b3b4aa74SDavid Sterba 	btrfs_release_path(path);
42168e73f275SChris Mason 
4217a2135011SChris Mason 	path->keep_locks = 1;
42188e73f275SChris Mason 	path->leave_spinning = 1;
42198e73f275SChris Mason 
4220925baeddSChris Mason 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4221925baeddSChris Mason 	path->keep_locks = 0;
4222925baeddSChris Mason 
4223925baeddSChris Mason 	if (ret < 0)
4224925baeddSChris Mason 		return ret;
4225925baeddSChris Mason 
4226a2135011SChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
4227168fd7d2SChris Mason 	/*
4228168fd7d2SChris Mason 	 * by releasing the path above we dropped all our locks.  A balance
4229168fd7d2SChris Mason 	 * could have added more items next to the key that used to be
4230168fd7d2SChris Mason 	 * at the very end of the block.  So, check again here and
4231168fd7d2SChris Mason 	 * advance the path if there are now more items available.
4232168fd7d2SChris Mason 	 */
4233a2135011SChris Mason 	if (nritems > 0 && path->slots[0] < nritems - 1) {
4234e457afecSYan Zheng 		if (ret == 0)
4235168fd7d2SChris Mason 			path->slots[0]++;
42368e73f275SChris Mason 		ret = 0;
4237925baeddSChris Mason 		goto done;
4238925baeddSChris Mason 	}
4239d97e63b6SChris Mason 
4240234b63a0SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
42418e73f275SChris Mason 		if (!path->nodes[level]) {
42428e73f275SChris Mason 			ret = 1;
42438e73f275SChris Mason 			goto done;
42448e73f275SChris Mason 		}
42455f39d397SChris Mason 
4246d97e63b6SChris Mason 		slot = path->slots[level] + 1;
4247d97e63b6SChris Mason 		c = path->nodes[level];
42485f39d397SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
4249d97e63b6SChris Mason 			level++;
42508e73f275SChris Mason 			if (level == BTRFS_MAX_LEVEL) {
42518e73f275SChris Mason 				ret = 1;
42528e73f275SChris Mason 				goto done;
42538e73f275SChris Mason 			}
4254d97e63b6SChris Mason 			continue;
4255d97e63b6SChris Mason 		}
42565f39d397SChris Mason 
4257925baeddSChris Mason 		if (next) {
4258bd681513SChris Mason 			btrfs_tree_unlock_rw(next, next_rw_lock);
42595f39d397SChris Mason 			free_extent_buffer(next);
4260925baeddSChris Mason 		}
42615f39d397SChris Mason 
42628e73f275SChris Mason 		next = c;
4263bd681513SChris Mason 		next_rw_lock = path->locks[level];
42648e73f275SChris Mason 		ret = read_block_for_search(NULL, root, path, &next, level,
42658e73f275SChris Mason 					    slot, &key);
42668e73f275SChris Mason 		if (ret == -EAGAIN)
42678e73f275SChris Mason 			goto again;
42685f39d397SChris Mason 
426976a05b35SChris Mason 		if (ret < 0) {
4270b3b4aa74SDavid Sterba 			btrfs_release_path(path);
427176a05b35SChris Mason 			goto done;
427276a05b35SChris Mason 		}
427376a05b35SChris Mason 
42745cd57b2cSChris Mason 		if (!path->skip_locking) {
4275bd681513SChris Mason 			ret = btrfs_try_tree_read_lock(next);
42768e73f275SChris Mason 			if (!ret) {
42778e73f275SChris Mason 				btrfs_set_path_blocking(path);
4278bd681513SChris Mason 				btrfs_tree_read_lock(next);
4279bd681513SChris Mason 				btrfs_clear_path_blocking(path, next,
4280bd681513SChris Mason 							  BTRFS_READ_LOCK);
42818e73f275SChris Mason 			}
4282bd681513SChris Mason 			next_rw_lock = BTRFS_READ_LOCK;
4283bd681513SChris Mason 		}
4284d97e63b6SChris Mason 		break;
4285d97e63b6SChris Mason 	}
4286d97e63b6SChris Mason 	path->slots[level] = slot;
4287d97e63b6SChris Mason 	while (1) {
4288d97e63b6SChris Mason 		level--;
4289d97e63b6SChris Mason 		c = path->nodes[level];
4290925baeddSChris Mason 		if (path->locks[level])
4291bd681513SChris Mason 			btrfs_tree_unlock_rw(c, path->locks[level]);
42928e73f275SChris Mason 
42935f39d397SChris Mason 		free_extent_buffer(c);
4294d97e63b6SChris Mason 		path->nodes[level] = next;
4295d97e63b6SChris Mason 		path->slots[level] = 0;
4296a74a4b97SChris Mason 		if (!path->skip_locking)
4297bd681513SChris Mason 			path->locks[level] = next_rw_lock;
4298d97e63b6SChris Mason 		if (!level)
4299d97e63b6SChris Mason 			break;
4300b4ce94deSChris Mason 
43018e73f275SChris Mason 		ret = read_block_for_search(NULL, root, path, &next, level,
43028e73f275SChris Mason 					    0, &key);
43038e73f275SChris Mason 		if (ret == -EAGAIN)
43048e73f275SChris Mason 			goto again;
43058e73f275SChris Mason 
430676a05b35SChris Mason 		if (ret < 0) {
4307b3b4aa74SDavid Sterba 			btrfs_release_path(path);
430876a05b35SChris Mason 			goto done;
430976a05b35SChris Mason 		}
431076a05b35SChris Mason 
43115cd57b2cSChris Mason 		if (!path->skip_locking) {
4312bd681513SChris Mason 			ret = btrfs_try_tree_read_lock(next);
43138e73f275SChris Mason 			if (!ret) {
43148e73f275SChris Mason 				btrfs_set_path_blocking(path);
4315bd681513SChris Mason 				btrfs_tree_read_lock(next);
4316bd681513SChris Mason 				btrfs_clear_path_blocking(path, next,
4317bd681513SChris Mason 							  BTRFS_READ_LOCK);
43188e73f275SChris Mason 			}
4319bd681513SChris Mason 			next_rw_lock = BTRFS_READ_LOCK;
4320bd681513SChris Mason 		}
4321d97e63b6SChris Mason 	}
43228e73f275SChris Mason 	ret = 0;
4323925baeddSChris Mason done:
4324925baeddSChris Mason 	unlock_up(path, 0, 1);
43258e73f275SChris Mason 	path->leave_spinning = old_spinning;
43268e73f275SChris Mason 	if (!old_spinning)
43278e73f275SChris Mason 		btrfs_set_path_blocking(path);
43288e73f275SChris Mason 
43298e73f275SChris Mason 	return ret;
4330d97e63b6SChris Mason }
43310b86a832SChris Mason 
43323f157a2fSChris Mason /*
43333f157a2fSChris Mason  * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
43343f157a2fSChris Mason  * searching until it gets past min_objectid or finds an item of 'type'
43353f157a2fSChris Mason  *
43363f157a2fSChris Mason  * returns 0 if something is found, 1 if nothing was found and < 0 on error
43373f157a2fSChris Mason  */
43380b86a832SChris Mason int btrfs_previous_item(struct btrfs_root *root,
43390b86a832SChris Mason 			struct btrfs_path *path, u64 min_objectid,
43400b86a832SChris Mason 			int type)
43410b86a832SChris Mason {
43420b86a832SChris Mason 	struct btrfs_key found_key;
43430b86a832SChris Mason 	struct extent_buffer *leaf;
4344e02119d5SChris Mason 	u32 nritems;
43450b86a832SChris Mason 	int ret;
43460b86a832SChris Mason 
43470b86a832SChris Mason 	while (1) {
43480b86a832SChris Mason 		if (path->slots[0] == 0) {
4349b4ce94deSChris Mason 			btrfs_set_path_blocking(path);
43500b86a832SChris Mason 			ret = btrfs_prev_leaf(root, path);
43510b86a832SChris Mason 			if (ret != 0)
43520b86a832SChris Mason 				return ret;
43530b86a832SChris Mason 		} else {
43540b86a832SChris Mason 			path->slots[0]--;
43550b86a832SChris Mason 		}
43560b86a832SChris Mason 		leaf = path->nodes[0];
4357e02119d5SChris Mason 		nritems = btrfs_header_nritems(leaf);
4358e02119d5SChris Mason 		if (nritems == 0)
4359e02119d5SChris Mason 			return 1;
4360e02119d5SChris Mason 		if (path->slots[0] == nritems)
4361e02119d5SChris Mason 			path->slots[0]--;
4362e02119d5SChris Mason 
43630b86a832SChris Mason 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4364e02119d5SChris Mason 		if (found_key.objectid < min_objectid)
4365e02119d5SChris Mason 			break;
43660a4eefbbSYan Zheng 		if (found_key.type == type)
43670a4eefbbSYan Zheng 			return 0;
4368e02119d5SChris Mason 		if (found_key.objectid == min_objectid &&
4369e02119d5SChris Mason 		    found_key.type < type)
4370e02119d5SChris Mason 			break;
43710b86a832SChris Mason 	}
43720b86a832SChris Mason 	return 1;
43730b86a832SChris Mason }
4374