xref: /openbmc/linux/fs/btrfs/ctree.c (revision cfed81a0)
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 
1593083ee2eSJosef Bacik 	while (1) {
160240f62c8SChris Mason 		rcu_read_lock();
161240f62c8SChris Mason 		eb = rcu_dereference(root->node);
1623083ee2eSJosef Bacik 
1633083ee2eSJosef Bacik 		/*
1643083ee2eSJosef Bacik 		 * RCU really hurts here, we could free up the root node because
1653083ee2eSJosef Bacik 		 * it was cow'ed but we may not get the new root node yet so do
1663083ee2eSJosef Bacik 		 * the inc_not_zero dance and if it doesn't work then
1673083ee2eSJosef Bacik 		 * synchronize_rcu and try again.
1683083ee2eSJosef Bacik 		 */
1693083ee2eSJosef Bacik 		if (atomic_inc_not_zero(&eb->refs)) {
170240f62c8SChris Mason 			rcu_read_unlock();
1713083ee2eSJosef Bacik 			break;
1723083ee2eSJosef Bacik 		}
1733083ee2eSJosef Bacik 		rcu_read_unlock();
1743083ee2eSJosef Bacik 		synchronize_rcu();
1753083ee2eSJosef 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);
5203083ee2eSJosef 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 */
9753083ee2eSJosef 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);
10323083ee2eSJosef 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);
10723083ee2eSJosef 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];
2336cfed81a0SChris Mason 	struct btrfs_map_token token;
23375f39d397SChris Mason 	struct btrfs_disk_key disk_key;
233800ec4c51SChris Mason 	int slot;
233934a38218SChris Mason 	u32 i;
234000ec4c51SChris Mason 	int push_space = 0;
234100ec4c51SChris Mason 	int push_items = 0;
23420783fcfcSChris Mason 	struct btrfs_item *item;
234334a38218SChris Mason 	u32 nr;
23447518a238SChris Mason 	u32 right_nritems;
23455f39d397SChris Mason 	u32 data_end;
2346db94535dSChris Mason 	u32 this_item_size;
234700ec4c51SChris Mason 
2348cfed81a0SChris Mason 	btrfs_init_map_token(&token);
2349cfed81a0SChris Mason 
235034a38218SChris Mason 	if (empty)
235134a38218SChris Mason 		nr = 0;
235234a38218SChris Mason 	else
235399d8f83cSChris Mason 		nr = max_t(u32, 1, min_slot);
235434a38218SChris Mason 
235531840ae1SZheng Yan 	if (path->slots[0] >= left_nritems)
235687b29b20SYan Zheng 		push_space += data_size;
235731840ae1SZheng Yan 
235844871b1bSChris Mason 	slot = path->slots[1];
235934a38218SChris Mason 	i = left_nritems - 1;
236034a38218SChris Mason 	while (i >= nr) {
23615f39d397SChris Mason 		item = btrfs_item_nr(left, i);
2362db94535dSChris Mason 
236331840ae1SZheng Yan 		if (!empty && push_items > 0) {
236431840ae1SZheng Yan 			if (path->slots[0] > i)
236531840ae1SZheng Yan 				break;
236631840ae1SZheng Yan 			if (path->slots[0] == i) {
236731840ae1SZheng Yan 				int space = btrfs_leaf_free_space(root, left);
236831840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
236931840ae1SZheng Yan 					break;
237031840ae1SZheng Yan 			}
237131840ae1SZheng Yan 		}
237231840ae1SZheng Yan 
237300ec4c51SChris Mason 		if (path->slots[0] == i)
237487b29b20SYan Zheng 			push_space += data_size;
2375db94535dSChris Mason 
2376db94535dSChris Mason 		this_item_size = btrfs_item_size(left, item);
2377db94535dSChris Mason 		if (this_item_size + sizeof(*item) + push_space > free_space)
237800ec4c51SChris Mason 			break;
237931840ae1SZheng Yan 
238000ec4c51SChris Mason 		push_items++;
2381db94535dSChris Mason 		push_space += this_item_size + sizeof(*item);
238234a38218SChris Mason 		if (i == 0)
238334a38218SChris Mason 			break;
238434a38218SChris Mason 		i--;
2385db94535dSChris Mason 	}
23865f39d397SChris Mason 
2387925baeddSChris Mason 	if (push_items == 0)
2388925baeddSChris Mason 		goto out_unlock;
23895f39d397SChris Mason 
239034a38218SChris Mason 	if (!empty && push_items == left_nritems)
2391a429e513SChris Mason 		WARN_ON(1);
23925f39d397SChris Mason 
239300ec4c51SChris Mason 	/* push left to right */
23945f39d397SChris Mason 	right_nritems = btrfs_header_nritems(right);
239534a38218SChris Mason 
23965f39d397SChris Mason 	push_space = btrfs_item_end_nr(left, left_nritems - push_items);
2397123abc88SChris Mason 	push_space -= leaf_data_end(root, left);
23985f39d397SChris Mason 
239900ec4c51SChris Mason 	/* make room in the right data area */
24005f39d397SChris Mason 	data_end = leaf_data_end(root, right);
24015f39d397SChris Mason 	memmove_extent_buffer(right,
24025f39d397SChris Mason 			      btrfs_leaf_data(right) + data_end - push_space,
24035f39d397SChris Mason 			      btrfs_leaf_data(right) + data_end,
24045f39d397SChris Mason 			      BTRFS_LEAF_DATA_SIZE(root) - data_end);
24055f39d397SChris Mason 
240600ec4c51SChris Mason 	/* copy from the left data area */
24075f39d397SChris Mason 	copy_extent_buffer(right, left, btrfs_leaf_data(right) +
2408d6025579SChris Mason 		     BTRFS_LEAF_DATA_SIZE(root) - push_space,
2409d6025579SChris Mason 		     btrfs_leaf_data(left) + leaf_data_end(root, left),
2410d6025579SChris Mason 		     push_space);
24115f39d397SChris Mason 
24125f39d397SChris Mason 	memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
24135f39d397SChris Mason 			      btrfs_item_nr_offset(0),
24140783fcfcSChris Mason 			      right_nritems * sizeof(struct btrfs_item));
24155f39d397SChris Mason 
241600ec4c51SChris Mason 	/* copy the items from left to right */
24175f39d397SChris Mason 	copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
24185f39d397SChris Mason 		   btrfs_item_nr_offset(left_nritems - push_items),
24190783fcfcSChris Mason 		   push_items * sizeof(struct btrfs_item));
242000ec4c51SChris Mason 
242100ec4c51SChris Mason 	/* update the item pointers */
24227518a238SChris Mason 	right_nritems += push_items;
24235f39d397SChris Mason 	btrfs_set_header_nritems(right, right_nritems);
2424123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root);
24257518a238SChris Mason 	for (i = 0; i < right_nritems; i++) {
24265f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2427cfed81a0SChris Mason 		push_space -= btrfs_token_item_size(right, item, &token);
2428cfed81a0SChris Mason 		btrfs_set_token_item_offset(right, item, push_space, &token);
2429db94535dSChris Mason 	}
2430db94535dSChris Mason 
24317518a238SChris Mason 	left_nritems -= push_items;
24325f39d397SChris Mason 	btrfs_set_header_nritems(left, left_nritems);
243300ec4c51SChris Mason 
243434a38218SChris Mason 	if (left_nritems)
24355f39d397SChris Mason 		btrfs_mark_buffer_dirty(left);
2436f0486c68SYan, Zheng 	else
2437f0486c68SYan, Zheng 		clean_tree_block(trans, root, left);
2438f0486c68SYan, Zheng 
24395f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
2440a429e513SChris Mason 
24415f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
24425f39d397SChris Mason 	btrfs_set_node_key(upper, &disk_key, slot + 1);
2443d6025579SChris Mason 	btrfs_mark_buffer_dirty(upper);
244402217ed2SChris Mason 
244500ec4c51SChris Mason 	/* then fixup the leaf pointer in the path */
24467518a238SChris Mason 	if (path->slots[0] >= left_nritems) {
24477518a238SChris Mason 		path->slots[0] -= left_nritems;
2448925baeddSChris Mason 		if (btrfs_header_nritems(path->nodes[0]) == 0)
2449925baeddSChris Mason 			clean_tree_block(trans, root, path->nodes[0]);
2450925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
24515f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
24525f39d397SChris Mason 		path->nodes[0] = right;
245300ec4c51SChris Mason 		path->slots[1] += 1;
245400ec4c51SChris Mason 	} else {
2455925baeddSChris Mason 		btrfs_tree_unlock(right);
24565f39d397SChris Mason 		free_extent_buffer(right);
245700ec4c51SChris Mason 	}
245800ec4c51SChris Mason 	return 0;
2459925baeddSChris Mason 
2460925baeddSChris Mason out_unlock:
2461925baeddSChris Mason 	btrfs_tree_unlock(right);
2462925baeddSChris Mason 	free_extent_buffer(right);
2463925baeddSChris Mason 	return 1;
246400ec4c51SChris Mason }
2465925baeddSChris Mason 
246600ec4c51SChris Mason /*
246744871b1bSChris Mason  * push some data in the path leaf to the right, trying to free up at
246874123bd7SChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
246944871b1bSChris Mason  *
247044871b1bSChris Mason  * returns 1 if the push failed because the other node didn't have enough
247144871b1bSChris Mason  * room, 0 if everything worked out and < 0 if there were major errors.
247299d8f83cSChris Mason  *
247399d8f83cSChris Mason  * this will push starting from min_slot to the end of the leaf.  It won't
247499d8f83cSChris Mason  * push any slot lower than min_slot
247574123bd7SChris Mason  */
247644871b1bSChris Mason static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
247799d8f83cSChris Mason 			   *root, struct btrfs_path *path,
247899d8f83cSChris Mason 			   int min_data_size, int data_size,
247999d8f83cSChris Mason 			   int empty, u32 min_slot)
2480be0e5c09SChris Mason {
248144871b1bSChris Mason 	struct extent_buffer *left = path->nodes[0];
248244871b1bSChris Mason 	struct extent_buffer *right;
248344871b1bSChris Mason 	struct extent_buffer *upper;
248444871b1bSChris Mason 	int slot;
248544871b1bSChris Mason 	int free_space;
248644871b1bSChris Mason 	u32 left_nritems;
248744871b1bSChris Mason 	int ret;
248844871b1bSChris Mason 
248944871b1bSChris Mason 	if (!path->nodes[1])
249044871b1bSChris Mason 		return 1;
249144871b1bSChris Mason 
249244871b1bSChris Mason 	slot = path->slots[1];
249344871b1bSChris Mason 	upper = path->nodes[1];
249444871b1bSChris Mason 	if (slot >= btrfs_header_nritems(upper) - 1)
249544871b1bSChris Mason 		return 1;
249644871b1bSChris Mason 
249744871b1bSChris Mason 	btrfs_assert_tree_locked(path->nodes[1]);
249844871b1bSChris Mason 
249944871b1bSChris Mason 	right = read_node_slot(root, upper, slot + 1);
250091ca338dSTsutomu Itoh 	if (right == NULL)
250191ca338dSTsutomu Itoh 		return 1;
250291ca338dSTsutomu Itoh 
250344871b1bSChris Mason 	btrfs_tree_lock(right);
250444871b1bSChris Mason 	btrfs_set_lock_blocking(right);
250544871b1bSChris Mason 
250644871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, right);
250744871b1bSChris Mason 	if (free_space < data_size)
250844871b1bSChris Mason 		goto out_unlock;
250944871b1bSChris Mason 
251044871b1bSChris Mason 	/* cow and double check */
251144871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, right, upper,
251244871b1bSChris Mason 			      slot + 1, &right);
251344871b1bSChris Mason 	if (ret)
251444871b1bSChris Mason 		goto out_unlock;
251544871b1bSChris Mason 
251644871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, right);
251744871b1bSChris Mason 	if (free_space < data_size)
251844871b1bSChris Mason 		goto out_unlock;
251944871b1bSChris Mason 
252044871b1bSChris Mason 	left_nritems = btrfs_header_nritems(left);
252144871b1bSChris Mason 	if (left_nritems == 0)
252244871b1bSChris Mason 		goto out_unlock;
252344871b1bSChris Mason 
252499d8f83cSChris Mason 	return __push_leaf_right(trans, root, path, min_data_size, empty,
252599d8f83cSChris Mason 				right, free_space, left_nritems, min_slot);
252644871b1bSChris Mason out_unlock:
252744871b1bSChris Mason 	btrfs_tree_unlock(right);
252844871b1bSChris Mason 	free_extent_buffer(right);
252944871b1bSChris Mason 	return 1;
253044871b1bSChris Mason }
253144871b1bSChris Mason 
253244871b1bSChris Mason /*
253344871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
253444871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
253599d8f83cSChris Mason  *
253699d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
253799d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us do all the
253899d8f83cSChris Mason  * items
253944871b1bSChris Mason  */
254044871b1bSChris Mason static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
254144871b1bSChris Mason 				     struct btrfs_root *root,
254244871b1bSChris Mason 				     struct btrfs_path *path, int data_size,
254344871b1bSChris Mason 				     int empty, struct extent_buffer *left,
254499d8f83cSChris Mason 				     int free_space, u32 right_nritems,
254599d8f83cSChris Mason 				     u32 max_slot)
254644871b1bSChris Mason {
25475f39d397SChris Mason 	struct btrfs_disk_key disk_key;
25485f39d397SChris Mason 	struct extent_buffer *right = path->nodes[0];
2549be0e5c09SChris Mason 	int i;
2550be0e5c09SChris Mason 	int push_space = 0;
2551be0e5c09SChris Mason 	int push_items = 0;
25520783fcfcSChris Mason 	struct btrfs_item *item;
25537518a238SChris Mason 	u32 old_left_nritems;
255434a38218SChris Mason 	u32 nr;
2555aa5d6bedSChris Mason 	int ret = 0;
2556aa5d6bedSChris Mason 	int wret;
2557db94535dSChris Mason 	u32 this_item_size;
2558db94535dSChris Mason 	u32 old_left_item_size;
2559cfed81a0SChris Mason 	struct btrfs_map_token token;
2560cfed81a0SChris Mason 
2561cfed81a0SChris Mason 	btrfs_init_map_token(&token);
2562be0e5c09SChris Mason 
256334a38218SChris Mason 	if (empty)
256499d8f83cSChris Mason 		nr = min(right_nritems, max_slot);
256534a38218SChris Mason 	else
256699d8f83cSChris Mason 		nr = min(right_nritems - 1, max_slot);
256734a38218SChris Mason 
256834a38218SChris Mason 	for (i = 0; i < nr; i++) {
25695f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2570db94535dSChris Mason 
257131840ae1SZheng Yan 		if (!empty && push_items > 0) {
257231840ae1SZheng Yan 			if (path->slots[0] < i)
257331840ae1SZheng Yan 				break;
257431840ae1SZheng Yan 			if (path->slots[0] == i) {
257531840ae1SZheng Yan 				int space = btrfs_leaf_free_space(root, right);
257631840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
257731840ae1SZheng Yan 					break;
257831840ae1SZheng Yan 			}
257931840ae1SZheng Yan 		}
258031840ae1SZheng Yan 
2581be0e5c09SChris Mason 		if (path->slots[0] == i)
258287b29b20SYan Zheng 			push_space += data_size;
2583db94535dSChris Mason 
2584db94535dSChris Mason 		this_item_size = btrfs_item_size(right, item);
2585db94535dSChris Mason 		if (this_item_size + sizeof(*item) + push_space > free_space)
2586be0e5c09SChris Mason 			break;
2587db94535dSChris Mason 
2588be0e5c09SChris Mason 		push_items++;
2589db94535dSChris Mason 		push_space += this_item_size + sizeof(*item);
2590be0e5c09SChris Mason 	}
2591db94535dSChris Mason 
2592be0e5c09SChris Mason 	if (push_items == 0) {
2593925baeddSChris Mason 		ret = 1;
2594925baeddSChris Mason 		goto out;
2595be0e5c09SChris Mason 	}
259634a38218SChris Mason 	if (!empty && push_items == btrfs_header_nritems(right))
2597a429e513SChris Mason 		WARN_ON(1);
25985f39d397SChris Mason 
2599be0e5c09SChris Mason 	/* push data from right to left */
26005f39d397SChris Mason 	copy_extent_buffer(left, right,
26015f39d397SChris Mason 			   btrfs_item_nr_offset(btrfs_header_nritems(left)),
26025f39d397SChris Mason 			   btrfs_item_nr_offset(0),
26035f39d397SChris Mason 			   push_items * sizeof(struct btrfs_item));
26045f39d397SChris Mason 
2605123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root) -
26065f39d397SChris Mason 		     btrfs_item_offset_nr(right, push_items - 1);
26075f39d397SChris Mason 
26085f39d397SChris Mason 	copy_extent_buffer(left, right, btrfs_leaf_data(left) +
2609d6025579SChris Mason 		     leaf_data_end(root, left) - push_space,
2610123abc88SChris Mason 		     btrfs_leaf_data(right) +
26115f39d397SChris Mason 		     btrfs_item_offset_nr(right, push_items - 1),
2612be0e5c09SChris Mason 		     push_space);
26135f39d397SChris Mason 	old_left_nritems = btrfs_header_nritems(left);
261487b29b20SYan Zheng 	BUG_ON(old_left_nritems <= 0);
2615eb60ceacSChris Mason 
2616db94535dSChris Mason 	old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
2617be0e5c09SChris Mason 	for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
26185f39d397SChris Mason 		u32 ioff;
2619db94535dSChris Mason 
26205f39d397SChris Mason 		item = btrfs_item_nr(left, i);
2621db94535dSChris Mason 
2622cfed81a0SChris Mason 		ioff = btrfs_token_item_offset(left, item, &token);
2623cfed81a0SChris Mason 		btrfs_set_token_item_offset(left, item,
2624cfed81a0SChris Mason 		      ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
2625cfed81a0SChris Mason 		      &token);
2626be0e5c09SChris Mason 	}
26275f39d397SChris Mason 	btrfs_set_header_nritems(left, old_left_nritems + push_items);
2628be0e5c09SChris Mason 
2629be0e5c09SChris Mason 	/* fixup right node */
263034a38218SChris Mason 	if (push_items > right_nritems) {
2631d397712bSChris Mason 		printk(KERN_CRIT "push items %d nr %u\n", push_items,
2632d397712bSChris Mason 		       right_nritems);
263334a38218SChris Mason 		WARN_ON(1);
263434a38218SChris Mason 	}
263534a38218SChris Mason 
263634a38218SChris Mason 	if (push_items < right_nritems) {
26375f39d397SChris Mason 		push_space = btrfs_item_offset_nr(right, push_items - 1) -
2638123abc88SChris Mason 						  leaf_data_end(root, right);
26395f39d397SChris Mason 		memmove_extent_buffer(right, btrfs_leaf_data(right) +
2640d6025579SChris Mason 				      BTRFS_LEAF_DATA_SIZE(root) - push_space,
2641d6025579SChris Mason 				      btrfs_leaf_data(right) +
2642123abc88SChris Mason 				      leaf_data_end(root, right), push_space);
26435f39d397SChris Mason 
26445f39d397SChris Mason 		memmove_extent_buffer(right, btrfs_item_nr_offset(0),
26455f39d397SChris Mason 			      btrfs_item_nr_offset(push_items),
26465f39d397SChris Mason 			     (btrfs_header_nritems(right) - push_items) *
26470783fcfcSChris Mason 			     sizeof(struct btrfs_item));
264834a38218SChris Mason 	}
2649eef1c494SYan 	right_nritems -= push_items;
2650eef1c494SYan 	btrfs_set_header_nritems(right, right_nritems);
2651123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root);
26525f39d397SChris Mason 	for (i = 0; i < right_nritems; i++) {
26535f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2654db94535dSChris Mason 
2655cfed81a0SChris Mason 		push_space = push_space - btrfs_token_item_size(right,
2656cfed81a0SChris Mason 								item, &token);
2657cfed81a0SChris Mason 		btrfs_set_token_item_offset(right, item, push_space, &token);
2658db94535dSChris Mason 	}
2659eb60ceacSChris Mason 
26605f39d397SChris Mason 	btrfs_mark_buffer_dirty(left);
266134a38218SChris Mason 	if (right_nritems)
26625f39d397SChris Mason 		btrfs_mark_buffer_dirty(right);
2663f0486c68SYan, Zheng 	else
2664f0486c68SYan, Zheng 		clean_tree_block(trans, root, right);
2665098f59c2SChris Mason 
26665f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
26675f39d397SChris Mason 	wret = fixup_low_keys(trans, root, path, &disk_key, 1);
2668aa5d6bedSChris Mason 	if (wret)
2669aa5d6bedSChris Mason 		ret = wret;
2670be0e5c09SChris Mason 
2671be0e5c09SChris Mason 	/* then fixup the leaf pointer in the path */
2672be0e5c09SChris Mason 	if (path->slots[0] < push_items) {
2673be0e5c09SChris Mason 		path->slots[0] += old_left_nritems;
2674925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
26755f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
26765f39d397SChris Mason 		path->nodes[0] = left;
2677be0e5c09SChris Mason 		path->slots[1] -= 1;
2678be0e5c09SChris Mason 	} else {
2679925baeddSChris Mason 		btrfs_tree_unlock(left);
26805f39d397SChris Mason 		free_extent_buffer(left);
2681be0e5c09SChris Mason 		path->slots[0] -= push_items;
2682be0e5c09SChris Mason 	}
2683eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
2684aa5d6bedSChris Mason 	return ret;
2685925baeddSChris Mason out:
2686925baeddSChris Mason 	btrfs_tree_unlock(left);
2687925baeddSChris Mason 	free_extent_buffer(left);
2688925baeddSChris Mason 	return ret;
2689be0e5c09SChris Mason }
2690be0e5c09SChris Mason 
269174123bd7SChris Mason /*
269244871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
269344871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
269499d8f83cSChris Mason  *
269599d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
269699d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us push all the
269799d8f83cSChris Mason  * items
269844871b1bSChris Mason  */
269944871b1bSChris Mason static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
270099d8f83cSChris Mason 			  *root, struct btrfs_path *path, int min_data_size,
270199d8f83cSChris Mason 			  int data_size, int empty, u32 max_slot)
270244871b1bSChris Mason {
270344871b1bSChris Mason 	struct extent_buffer *right = path->nodes[0];
270444871b1bSChris Mason 	struct extent_buffer *left;
270544871b1bSChris Mason 	int slot;
270644871b1bSChris Mason 	int free_space;
270744871b1bSChris Mason 	u32 right_nritems;
270844871b1bSChris Mason 	int ret = 0;
270944871b1bSChris Mason 
271044871b1bSChris Mason 	slot = path->slots[1];
271144871b1bSChris Mason 	if (slot == 0)
271244871b1bSChris Mason 		return 1;
271344871b1bSChris Mason 	if (!path->nodes[1])
271444871b1bSChris Mason 		return 1;
271544871b1bSChris Mason 
271644871b1bSChris Mason 	right_nritems = btrfs_header_nritems(right);
271744871b1bSChris Mason 	if (right_nritems == 0)
271844871b1bSChris Mason 		return 1;
271944871b1bSChris Mason 
272044871b1bSChris Mason 	btrfs_assert_tree_locked(path->nodes[1]);
272144871b1bSChris Mason 
272244871b1bSChris Mason 	left = read_node_slot(root, path->nodes[1], slot - 1);
272391ca338dSTsutomu Itoh 	if (left == NULL)
272491ca338dSTsutomu Itoh 		return 1;
272591ca338dSTsutomu Itoh 
272644871b1bSChris Mason 	btrfs_tree_lock(left);
272744871b1bSChris Mason 	btrfs_set_lock_blocking(left);
272844871b1bSChris Mason 
272944871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, left);
273044871b1bSChris Mason 	if (free_space < data_size) {
273144871b1bSChris Mason 		ret = 1;
273244871b1bSChris Mason 		goto out;
273344871b1bSChris Mason 	}
273444871b1bSChris Mason 
273544871b1bSChris Mason 	/* cow and double check */
273644871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, left,
273744871b1bSChris Mason 			      path->nodes[1], slot - 1, &left);
273844871b1bSChris Mason 	if (ret) {
273944871b1bSChris Mason 		/* we hit -ENOSPC, but it isn't fatal here */
274044871b1bSChris Mason 		ret = 1;
274144871b1bSChris Mason 		goto out;
274244871b1bSChris Mason 	}
274344871b1bSChris Mason 
274444871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, left);
274544871b1bSChris Mason 	if (free_space < data_size) {
274644871b1bSChris Mason 		ret = 1;
274744871b1bSChris Mason 		goto out;
274844871b1bSChris Mason 	}
274944871b1bSChris Mason 
275099d8f83cSChris Mason 	return __push_leaf_left(trans, root, path, min_data_size,
275199d8f83cSChris Mason 			       empty, left, free_space, right_nritems,
275299d8f83cSChris Mason 			       max_slot);
275344871b1bSChris Mason out:
275444871b1bSChris Mason 	btrfs_tree_unlock(left);
275544871b1bSChris Mason 	free_extent_buffer(left);
275644871b1bSChris Mason 	return ret;
275744871b1bSChris Mason }
275844871b1bSChris Mason 
275944871b1bSChris Mason /*
276074123bd7SChris Mason  * split the path's leaf in two, making sure there is at least data_size
276174123bd7SChris Mason  * available for the resulting leaf level of the path.
2762aa5d6bedSChris Mason  *
2763aa5d6bedSChris Mason  * returns 0 if all went well and < 0 on failure.
276474123bd7SChris Mason  */
276544871b1bSChris Mason static noinline int copy_for_split(struct btrfs_trans_handle *trans,
2766e02119d5SChris Mason 			       struct btrfs_root *root,
276744871b1bSChris Mason 			       struct btrfs_path *path,
276844871b1bSChris Mason 			       struct extent_buffer *l,
276944871b1bSChris Mason 			       struct extent_buffer *right,
277044871b1bSChris Mason 			       int slot, int mid, int nritems)
2771be0e5c09SChris Mason {
2772be0e5c09SChris Mason 	int data_copy_size;
2773be0e5c09SChris Mason 	int rt_data_off;
2774be0e5c09SChris Mason 	int i;
2775d4dbff95SChris Mason 	int ret = 0;
2776aa5d6bedSChris Mason 	int wret;
2777d4dbff95SChris Mason 	struct btrfs_disk_key disk_key;
2778cfed81a0SChris Mason 	struct btrfs_map_token token;
2779cfed81a0SChris Mason 
2780cfed81a0SChris Mason 	btrfs_init_map_token(&token);
2781be0e5c09SChris Mason 
27825f39d397SChris Mason 	nritems = nritems - mid;
27835f39d397SChris Mason 	btrfs_set_header_nritems(right, nritems);
27845f39d397SChris Mason 	data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
27855f39d397SChris Mason 
27865f39d397SChris Mason 	copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
27875f39d397SChris Mason 			   btrfs_item_nr_offset(mid),
27885f39d397SChris Mason 			   nritems * sizeof(struct btrfs_item));
27895f39d397SChris Mason 
27905f39d397SChris Mason 	copy_extent_buffer(right, l,
2791d6025579SChris Mason 		     btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2792123abc88SChris Mason 		     data_copy_size, btrfs_leaf_data(l) +
2793123abc88SChris Mason 		     leaf_data_end(root, l), data_copy_size);
279474123bd7SChris Mason 
27955f39d397SChris Mason 	rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
27965f39d397SChris Mason 		      btrfs_item_end_nr(l, mid);
27975f39d397SChris Mason 
27985f39d397SChris Mason 	for (i = 0; i < nritems; i++) {
27995f39d397SChris Mason 		struct btrfs_item *item = btrfs_item_nr(right, i);
2800db94535dSChris Mason 		u32 ioff;
2801db94535dSChris Mason 
2802cfed81a0SChris Mason 		ioff = btrfs_token_item_offset(right, item, &token);
2803cfed81a0SChris Mason 		btrfs_set_token_item_offset(right, item,
2804cfed81a0SChris Mason 					    ioff + rt_data_off, &token);
28050783fcfcSChris Mason 	}
280674123bd7SChris Mason 
28075f39d397SChris Mason 	btrfs_set_header_nritems(l, mid);
2808aa5d6bedSChris Mason 	ret = 0;
28095f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
2810db94535dSChris Mason 	wret = insert_ptr(trans, root, path, &disk_key, right->start,
2811db94535dSChris Mason 			  path->slots[1] + 1, 1);
2812aa5d6bedSChris Mason 	if (wret)
2813aa5d6bedSChris Mason 		ret = wret;
28145f39d397SChris Mason 
28155f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
28165f39d397SChris Mason 	btrfs_mark_buffer_dirty(l);
2817eb60ceacSChris Mason 	BUG_ON(path->slots[0] != slot);
28185f39d397SChris Mason 
2819be0e5c09SChris Mason 	if (mid <= slot) {
2820925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
28215f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
28225f39d397SChris Mason 		path->nodes[0] = right;
2823be0e5c09SChris Mason 		path->slots[0] -= mid;
2824be0e5c09SChris Mason 		path->slots[1] += 1;
2825925baeddSChris Mason 	} else {
2826925baeddSChris Mason 		btrfs_tree_unlock(right);
28275f39d397SChris Mason 		free_extent_buffer(right);
2828925baeddSChris Mason 	}
28295f39d397SChris Mason 
2830eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
2831d4dbff95SChris Mason 
283244871b1bSChris Mason 	return ret;
283344871b1bSChris Mason }
283444871b1bSChris Mason 
283544871b1bSChris Mason /*
283699d8f83cSChris Mason  * double splits happen when we need to insert a big item in the middle
283799d8f83cSChris Mason  * of a leaf.  A double split can leave us with 3 mostly empty leaves:
283899d8f83cSChris Mason  * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
283999d8f83cSChris Mason  *          A                 B                 C
284099d8f83cSChris Mason  *
284199d8f83cSChris Mason  * We avoid this by trying to push the items on either side of our target
284299d8f83cSChris Mason  * into the adjacent leaves.  If all goes well we can avoid the double split
284399d8f83cSChris Mason  * completely.
284499d8f83cSChris Mason  */
284599d8f83cSChris Mason static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
284699d8f83cSChris Mason 					  struct btrfs_root *root,
284799d8f83cSChris Mason 					  struct btrfs_path *path,
284899d8f83cSChris Mason 					  int data_size)
284999d8f83cSChris Mason {
285099d8f83cSChris Mason 	int ret;
285199d8f83cSChris Mason 	int progress = 0;
285299d8f83cSChris Mason 	int slot;
285399d8f83cSChris Mason 	u32 nritems;
285499d8f83cSChris Mason 
285599d8f83cSChris Mason 	slot = path->slots[0];
285699d8f83cSChris Mason 
285799d8f83cSChris Mason 	/*
285899d8f83cSChris Mason 	 * try to push all the items after our slot into the
285999d8f83cSChris Mason 	 * right leaf
286099d8f83cSChris Mason 	 */
286199d8f83cSChris Mason 	ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
286299d8f83cSChris Mason 	if (ret < 0)
286399d8f83cSChris Mason 		return ret;
286499d8f83cSChris Mason 
286599d8f83cSChris Mason 	if (ret == 0)
286699d8f83cSChris Mason 		progress++;
286799d8f83cSChris Mason 
286899d8f83cSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
286999d8f83cSChris Mason 	/*
287099d8f83cSChris Mason 	 * our goal is to get our slot at the start or end of a leaf.  If
287199d8f83cSChris Mason 	 * we've done so we're done
287299d8f83cSChris Mason 	 */
287399d8f83cSChris Mason 	if (path->slots[0] == 0 || path->slots[0] == nritems)
287499d8f83cSChris Mason 		return 0;
287599d8f83cSChris Mason 
287699d8f83cSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
287799d8f83cSChris Mason 		return 0;
287899d8f83cSChris Mason 
287999d8f83cSChris Mason 	/* try to push all the items before our slot into the next leaf */
288099d8f83cSChris Mason 	slot = path->slots[0];
288199d8f83cSChris Mason 	ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
288299d8f83cSChris Mason 	if (ret < 0)
288399d8f83cSChris Mason 		return ret;
288499d8f83cSChris Mason 
288599d8f83cSChris Mason 	if (ret == 0)
288699d8f83cSChris Mason 		progress++;
288799d8f83cSChris Mason 
288899d8f83cSChris Mason 	if (progress)
288999d8f83cSChris Mason 		return 0;
289099d8f83cSChris Mason 	return 1;
289199d8f83cSChris Mason }
289299d8f83cSChris Mason 
289399d8f83cSChris Mason /*
289444871b1bSChris Mason  * split the path's leaf in two, making sure there is at least data_size
289544871b1bSChris Mason  * available for the resulting leaf level of the path.
289644871b1bSChris Mason  *
289744871b1bSChris Mason  * returns 0 if all went well and < 0 on failure.
289844871b1bSChris Mason  */
289944871b1bSChris Mason static noinline int split_leaf(struct btrfs_trans_handle *trans,
290044871b1bSChris Mason 			       struct btrfs_root *root,
290144871b1bSChris Mason 			       struct btrfs_key *ins_key,
290244871b1bSChris Mason 			       struct btrfs_path *path, int data_size,
290344871b1bSChris Mason 			       int extend)
290444871b1bSChris Mason {
29055d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
290644871b1bSChris Mason 	struct extent_buffer *l;
290744871b1bSChris Mason 	u32 nritems;
290844871b1bSChris Mason 	int mid;
290944871b1bSChris Mason 	int slot;
291044871b1bSChris Mason 	struct extent_buffer *right;
291144871b1bSChris Mason 	int ret = 0;
291244871b1bSChris Mason 	int wret;
29135d4f98a2SYan Zheng 	int split;
291444871b1bSChris Mason 	int num_doubles = 0;
291599d8f83cSChris Mason 	int tried_avoid_double = 0;
291644871b1bSChris Mason 
2917a5719521SYan, Zheng 	l = path->nodes[0];
2918a5719521SYan, Zheng 	slot = path->slots[0];
2919a5719521SYan, Zheng 	if (extend && data_size + btrfs_item_size_nr(l, slot) +
2920a5719521SYan, Zheng 	    sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2921a5719521SYan, Zheng 		return -EOVERFLOW;
2922a5719521SYan, Zheng 
292344871b1bSChris Mason 	/* first try to make some room by pushing left and right */
292499d8f83cSChris Mason 	if (data_size) {
292599d8f83cSChris Mason 		wret = push_leaf_right(trans, root, path, data_size,
292699d8f83cSChris Mason 				       data_size, 0, 0);
292744871b1bSChris Mason 		if (wret < 0)
292844871b1bSChris Mason 			return wret;
292944871b1bSChris Mason 		if (wret) {
293099d8f83cSChris Mason 			wret = push_leaf_left(trans, root, path, data_size,
293199d8f83cSChris Mason 					      data_size, 0, (u32)-1);
293244871b1bSChris Mason 			if (wret < 0)
293344871b1bSChris Mason 				return wret;
293444871b1bSChris Mason 		}
293544871b1bSChris Mason 		l = path->nodes[0];
293644871b1bSChris Mason 
293744871b1bSChris Mason 		/* did the pushes work? */
293844871b1bSChris Mason 		if (btrfs_leaf_free_space(root, l) >= data_size)
293944871b1bSChris Mason 			return 0;
294044871b1bSChris Mason 	}
294144871b1bSChris Mason 
294244871b1bSChris Mason 	if (!path->nodes[1]) {
294344871b1bSChris Mason 		ret = insert_new_root(trans, root, path, 1);
294444871b1bSChris Mason 		if (ret)
294544871b1bSChris Mason 			return ret;
294644871b1bSChris Mason 	}
294744871b1bSChris Mason again:
29485d4f98a2SYan Zheng 	split = 1;
294944871b1bSChris Mason 	l = path->nodes[0];
295044871b1bSChris Mason 	slot = path->slots[0];
295144871b1bSChris Mason 	nritems = btrfs_header_nritems(l);
295244871b1bSChris Mason 	mid = (nritems + 1) / 2;
295344871b1bSChris Mason 
29545d4f98a2SYan Zheng 	if (mid <= slot) {
29555d4f98a2SYan Zheng 		if (nritems == 1 ||
29565d4f98a2SYan Zheng 		    leaf_space_used(l, mid, nritems - mid) + data_size >
29575d4f98a2SYan Zheng 			BTRFS_LEAF_DATA_SIZE(root)) {
29585d4f98a2SYan Zheng 			if (slot >= nritems) {
29595d4f98a2SYan Zheng 				split = 0;
29605d4f98a2SYan Zheng 			} else {
29615d4f98a2SYan Zheng 				mid = slot;
29625d4f98a2SYan Zheng 				if (mid != nritems &&
29635d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
29645d4f98a2SYan Zheng 				    data_size > BTRFS_LEAF_DATA_SIZE(root)) {
296599d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
296699d8f83cSChris Mason 						goto push_for_double;
29675d4f98a2SYan Zheng 					split = 2;
29685d4f98a2SYan Zheng 				}
29695d4f98a2SYan Zheng 			}
29705d4f98a2SYan Zheng 		}
29715d4f98a2SYan Zheng 	} else {
29725d4f98a2SYan Zheng 		if (leaf_space_used(l, 0, mid) + data_size >
29735d4f98a2SYan Zheng 			BTRFS_LEAF_DATA_SIZE(root)) {
29745d4f98a2SYan Zheng 			if (!extend && data_size && slot == 0) {
29755d4f98a2SYan Zheng 				split = 0;
29765d4f98a2SYan Zheng 			} else if ((extend || !data_size) && slot == 0) {
29775d4f98a2SYan Zheng 				mid = 1;
29785d4f98a2SYan Zheng 			} else {
29795d4f98a2SYan Zheng 				mid = slot;
29805d4f98a2SYan Zheng 				if (mid != nritems &&
29815d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
29825d4f98a2SYan Zheng 				    data_size > BTRFS_LEAF_DATA_SIZE(root)) {
298399d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
298499d8f83cSChris Mason 						goto push_for_double;
29855d4f98a2SYan Zheng 					split = 2 ;
29865d4f98a2SYan Zheng 				}
29875d4f98a2SYan Zheng 			}
29885d4f98a2SYan Zheng 		}
29895d4f98a2SYan Zheng 	}
29905d4f98a2SYan Zheng 
29915d4f98a2SYan Zheng 	if (split == 0)
29925d4f98a2SYan Zheng 		btrfs_cpu_key_to_disk(&disk_key, ins_key);
29935d4f98a2SYan Zheng 	else
29945d4f98a2SYan Zheng 		btrfs_item_key(l, &disk_key, mid);
29955d4f98a2SYan Zheng 
29965d4f98a2SYan Zheng 	right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
299744871b1bSChris Mason 					root->root_key.objectid,
299866d7e7f0SArne Jansen 					&disk_key, 0, l->start, 0, 0);
2999f0486c68SYan, Zheng 	if (IS_ERR(right))
300044871b1bSChris Mason 		return PTR_ERR(right);
3001f0486c68SYan, Zheng 
3002f0486c68SYan, Zheng 	root_add_used(root, root->leafsize);
300344871b1bSChris Mason 
300444871b1bSChris Mason 	memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
300544871b1bSChris Mason 	btrfs_set_header_bytenr(right, right->start);
300644871b1bSChris Mason 	btrfs_set_header_generation(right, trans->transid);
30075d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
300844871b1bSChris Mason 	btrfs_set_header_owner(right, root->root_key.objectid);
300944871b1bSChris Mason 	btrfs_set_header_level(right, 0);
301044871b1bSChris Mason 	write_extent_buffer(right, root->fs_info->fsid,
301144871b1bSChris Mason 			    (unsigned long)btrfs_header_fsid(right),
301244871b1bSChris Mason 			    BTRFS_FSID_SIZE);
301344871b1bSChris Mason 
301444871b1bSChris Mason 	write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
301544871b1bSChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(right),
301644871b1bSChris Mason 			    BTRFS_UUID_SIZE);
301744871b1bSChris Mason 
30185d4f98a2SYan Zheng 	if (split == 0) {
301944871b1bSChris Mason 		if (mid <= slot) {
302044871b1bSChris Mason 			btrfs_set_header_nritems(right, 0);
302144871b1bSChris Mason 			wret = insert_ptr(trans, root, path,
302244871b1bSChris Mason 					  &disk_key, right->start,
302344871b1bSChris Mason 					  path->slots[1] + 1, 1);
302444871b1bSChris Mason 			if (wret)
302544871b1bSChris Mason 				ret = wret;
302644871b1bSChris Mason 
302744871b1bSChris Mason 			btrfs_tree_unlock(path->nodes[0]);
302844871b1bSChris Mason 			free_extent_buffer(path->nodes[0]);
302944871b1bSChris Mason 			path->nodes[0] = right;
303044871b1bSChris Mason 			path->slots[0] = 0;
303144871b1bSChris Mason 			path->slots[1] += 1;
303244871b1bSChris Mason 		} else {
303344871b1bSChris Mason 			btrfs_set_header_nritems(right, 0);
303444871b1bSChris Mason 			wret = insert_ptr(trans, root, path,
303544871b1bSChris Mason 					  &disk_key,
303644871b1bSChris Mason 					  right->start,
303744871b1bSChris Mason 					  path->slots[1], 1);
303844871b1bSChris Mason 			if (wret)
303944871b1bSChris Mason 				ret = wret;
304044871b1bSChris Mason 			btrfs_tree_unlock(path->nodes[0]);
304144871b1bSChris Mason 			free_extent_buffer(path->nodes[0]);
304244871b1bSChris Mason 			path->nodes[0] = right;
304344871b1bSChris Mason 			path->slots[0] = 0;
304444871b1bSChris Mason 			if (path->slots[1] == 0) {
304544871b1bSChris Mason 				wret = fixup_low_keys(trans, root,
304644871b1bSChris Mason 						path, &disk_key, 1);
304744871b1bSChris Mason 				if (wret)
304844871b1bSChris Mason 					ret = wret;
304944871b1bSChris Mason 			}
30505d4f98a2SYan Zheng 		}
305144871b1bSChris Mason 		btrfs_mark_buffer_dirty(right);
305244871b1bSChris Mason 		return ret;
305344871b1bSChris Mason 	}
305444871b1bSChris Mason 
305544871b1bSChris Mason 	ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
305644871b1bSChris Mason 	BUG_ON(ret);
305744871b1bSChris Mason 
30585d4f98a2SYan Zheng 	if (split == 2) {
3059cc0c5538SChris Mason 		BUG_ON(num_doubles != 0);
3060cc0c5538SChris Mason 		num_doubles++;
3061cc0c5538SChris Mason 		goto again;
30623326d1b0SChris Mason 	}
306344871b1bSChris Mason 
3064be0e5c09SChris Mason 	return ret;
306599d8f83cSChris Mason 
306699d8f83cSChris Mason push_for_double:
306799d8f83cSChris Mason 	push_for_double_split(trans, root, path, data_size);
306899d8f83cSChris Mason 	tried_avoid_double = 1;
306999d8f83cSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
307099d8f83cSChris Mason 		return 0;
307199d8f83cSChris Mason 	goto again;
3072be0e5c09SChris Mason }
3073be0e5c09SChris Mason 
3074ad48fd75SYan, Zheng static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3075ad48fd75SYan, Zheng 					 struct btrfs_root *root,
3076ad48fd75SYan, Zheng 					 struct btrfs_path *path, int ins_len)
3077ad48fd75SYan, Zheng {
3078ad48fd75SYan, Zheng 	struct btrfs_key key;
3079ad48fd75SYan, Zheng 	struct extent_buffer *leaf;
3080ad48fd75SYan, Zheng 	struct btrfs_file_extent_item *fi;
3081ad48fd75SYan, Zheng 	u64 extent_len = 0;
3082ad48fd75SYan, Zheng 	u32 item_size;
3083ad48fd75SYan, Zheng 	int ret;
3084ad48fd75SYan, Zheng 
3085ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3086ad48fd75SYan, Zheng 	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3087ad48fd75SYan, Zheng 
3088ad48fd75SYan, Zheng 	BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3089ad48fd75SYan, Zheng 	       key.type != BTRFS_EXTENT_CSUM_KEY);
3090ad48fd75SYan, Zheng 
3091ad48fd75SYan, Zheng 	if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3092ad48fd75SYan, Zheng 		return 0;
3093ad48fd75SYan, Zheng 
3094ad48fd75SYan, Zheng 	item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3095ad48fd75SYan, Zheng 	if (key.type == BTRFS_EXTENT_DATA_KEY) {
3096ad48fd75SYan, Zheng 		fi = btrfs_item_ptr(leaf, path->slots[0],
3097ad48fd75SYan, Zheng 				    struct btrfs_file_extent_item);
3098ad48fd75SYan, Zheng 		extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3099ad48fd75SYan, Zheng 	}
3100b3b4aa74SDavid Sterba 	btrfs_release_path(path);
3101ad48fd75SYan, Zheng 
3102ad48fd75SYan, Zheng 	path->keep_locks = 1;
3103ad48fd75SYan, Zheng 	path->search_for_split = 1;
3104ad48fd75SYan, Zheng 	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3105ad48fd75SYan, Zheng 	path->search_for_split = 0;
3106ad48fd75SYan, Zheng 	if (ret < 0)
3107ad48fd75SYan, Zheng 		goto err;
3108ad48fd75SYan, Zheng 
3109ad48fd75SYan, Zheng 	ret = -EAGAIN;
3110ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3111ad48fd75SYan, Zheng 	/* if our item isn't there or got smaller, return now */
3112ad48fd75SYan, Zheng 	if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3113ad48fd75SYan, Zheng 		goto err;
3114ad48fd75SYan, Zheng 
3115109f6aefSChris Mason 	/* the leaf has  changed, it now has room.  return now */
3116109f6aefSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3117109f6aefSChris Mason 		goto err;
3118109f6aefSChris Mason 
3119ad48fd75SYan, Zheng 	if (key.type == BTRFS_EXTENT_DATA_KEY) {
3120ad48fd75SYan, Zheng 		fi = btrfs_item_ptr(leaf, path->slots[0],
3121ad48fd75SYan, Zheng 				    struct btrfs_file_extent_item);
3122ad48fd75SYan, Zheng 		if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3123ad48fd75SYan, Zheng 			goto err;
3124ad48fd75SYan, Zheng 	}
3125ad48fd75SYan, Zheng 
3126ad48fd75SYan, Zheng 	btrfs_set_path_blocking(path);
3127ad48fd75SYan, Zheng 	ret = split_leaf(trans, root, &key, path, ins_len, 1);
3128f0486c68SYan, Zheng 	if (ret)
3129f0486c68SYan, Zheng 		goto err;
3130ad48fd75SYan, Zheng 
3131ad48fd75SYan, Zheng 	path->keep_locks = 0;
3132ad48fd75SYan, Zheng 	btrfs_unlock_up_safe(path, 1);
3133ad48fd75SYan, Zheng 	return 0;
3134ad48fd75SYan, Zheng err:
3135ad48fd75SYan, Zheng 	path->keep_locks = 0;
3136ad48fd75SYan, Zheng 	return ret;
3137ad48fd75SYan, Zheng }
3138ad48fd75SYan, Zheng 
3139ad48fd75SYan, Zheng static noinline int split_item(struct btrfs_trans_handle *trans,
3140459931ecSChris Mason 			       struct btrfs_root *root,
3141459931ecSChris Mason 			       struct btrfs_path *path,
3142459931ecSChris Mason 			       struct btrfs_key *new_key,
3143459931ecSChris Mason 			       unsigned long split_offset)
3144459931ecSChris Mason {
3145459931ecSChris Mason 	struct extent_buffer *leaf;
3146459931ecSChris Mason 	struct btrfs_item *item;
3147459931ecSChris Mason 	struct btrfs_item *new_item;
3148459931ecSChris Mason 	int slot;
3149ad48fd75SYan, Zheng 	char *buf;
3150459931ecSChris Mason 	u32 nritems;
3151ad48fd75SYan, Zheng 	u32 item_size;
3152459931ecSChris Mason 	u32 orig_offset;
3153459931ecSChris Mason 	struct btrfs_disk_key disk_key;
3154459931ecSChris Mason 
3155459931ecSChris Mason 	leaf = path->nodes[0];
3156b9473439SChris Mason 	BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3157b9473439SChris Mason 
3158b4ce94deSChris Mason 	btrfs_set_path_blocking(path);
3159b4ce94deSChris Mason 
3160459931ecSChris Mason 	item = btrfs_item_nr(leaf, path->slots[0]);
3161459931ecSChris Mason 	orig_offset = btrfs_item_offset(leaf, item);
3162459931ecSChris Mason 	item_size = btrfs_item_size(leaf, item);
3163459931ecSChris Mason 
3164459931ecSChris Mason 	buf = kmalloc(item_size, GFP_NOFS);
3165ad48fd75SYan, Zheng 	if (!buf)
3166ad48fd75SYan, Zheng 		return -ENOMEM;
3167ad48fd75SYan, Zheng 
3168459931ecSChris Mason 	read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3169459931ecSChris Mason 			    path->slots[0]), item_size);
3170ad48fd75SYan, Zheng 
3171459931ecSChris Mason 	slot = path->slots[0] + 1;
3172459931ecSChris Mason 	nritems = btrfs_header_nritems(leaf);
3173459931ecSChris Mason 	if (slot != nritems) {
3174459931ecSChris Mason 		/* shift the items */
3175459931ecSChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3176459931ecSChris Mason 				btrfs_item_nr_offset(slot),
3177459931ecSChris Mason 				(nritems - slot) * sizeof(struct btrfs_item));
3178459931ecSChris Mason 	}
3179459931ecSChris Mason 
3180459931ecSChris Mason 	btrfs_cpu_key_to_disk(&disk_key, new_key);
3181459931ecSChris Mason 	btrfs_set_item_key(leaf, &disk_key, slot);
3182459931ecSChris Mason 
3183459931ecSChris Mason 	new_item = btrfs_item_nr(leaf, slot);
3184459931ecSChris Mason 
3185459931ecSChris Mason 	btrfs_set_item_offset(leaf, new_item, orig_offset);
3186459931ecSChris Mason 	btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3187459931ecSChris Mason 
3188459931ecSChris Mason 	btrfs_set_item_offset(leaf, item,
3189459931ecSChris Mason 			      orig_offset + item_size - split_offset);
3190459931ecSChris Mason 	btrfs_set_item_size(leaf, item, split_offset);
3191459931ecSChris Mason 
3192459931ecSChris Mason 	btrfs_set_header_nritems(leaf, nritems + 1);
3193459931ecSChris Mason 
3194459931ecSChris Mason 	/* write the data for the start of the original item */
3195459931ecSChris Mason 	write_extent_buffer(leaf, buf,
3196459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, path->slots[0]),
3197459931ecSChris Mason 			    split_offset);
3198459931ecSChris Mason 
3199459931ecSChris Mason 	/* write the data for the new item */
3200459931ecSChris Mason 	write_extent_buffer(leaf, buf + split_offset,
3201459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, slot),
3202459931ecSChris Mason 			    item_size - split_offset);
3203459931ecSChris Mason 	btrfs_mark_buffer_dirty(leaf);
3204459931ecSChris Mason 
3205ad48fd75SYan, Zheng 	BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
3206459931ecSChris Mason 	kfree(buf);
3207ad48fd75SYan, Zheng 	return 0;
3208ad48fd75SYan, Zheng }
3209ad48fd75SYan, Zheng 
3210ad48fd75SYan, Zheng /*
3211ad48fd75SYan, Zheng  * This function splits a single item into two items,
3212ad48fd75SYan, Zheng  * giving 'new_key' to the new item and splitting the
3213ad48fd75SYan, Zheng  * old one at split_offset (from the start of the item).
3214ad48fd75SYan, Zheng  *
3215ad48fd75SYan, Zheng  * The path may be released by this operation.  After
3216ad48fd75SYan, Zheng  * the split, the path is pointing to the old item.  The
3217ad48fd75SYan, Zheng  * new item is going to be in the same node as the old one.
3218ad48fd75SYan, Zheng  *
3219ad48fd75SYan, Zheng  * Note, the item being split must be smaller enough to live alone on
3220ad48fd75SYan, Zheng  * a tree block with room for one extra struct btrfs_item
3221ad48fd75SYan, Zheng  *
3222ad48fd75SYan, Zheng  * This allows us to split the item in place, keeping a lock on the
3223ad48fd75SYan, Zheng  * leaf the entire time.
3224ad48fd75SYan, Zheng  */
3225ad48fd75SYan, Zheng int btrfs_split_item(struct btrfs_trans_handle *trans,
3226ad48fd75SYan, Zheng 		     struct btrfs_root *root,
3227ad48fd75SYan, Zheng 		     struct btrfs_path *path,
3228ad48fd75SYan, Zheng 		     struct btrfs_key *new_key,
3229ad48fd75SYan, Zheng 		     unsigned long split_offset)
3230ad48fd75SYan, Zheng {
3231ad48fd75SYan, Zheng 	int ret;
3232ad48fd75SYan, Zheng 	ret = setup_leaf_for_split(trans, root, path,
3233ad48fd75SYan, Zheng 				   sizeof(struct btrfs_item));
3234ad48fd75SYan, Zheng 	if (ret)
3235459931ecSChris Mason 		return ret;
3236ad48fd75SYan, Zheng 
3237ad48fd75SYan, Zheng 	ret = split_item(trans, root, path, new_key, split_offset);
3238ad48fd75SYan, Zheng 	return ret;
3239ad48fd75SYan, Zheng }
3240ad48fd75SYan, Zheng 
3241ad48fd75SYan, Zheng /*
3242ad48fd75SYan, Zheng  * This function duplicate a item, giving 'new_key' to the new item.
3243ad48fd75SYan, Zheng  * It guarantees both items live in the same tree leaf and the new item
3244ad48fd75SYan, Zheng  * is contiguous with the original item.
3245ad48fd75SYan, Zheng  *
3246ad48fd75SYan, Zheng  * This allows us to split file extent in place, keeping a lock on the
3247ad48fd75SYan, Zheng  * leaf the entire time.
3248ad48fd75SYan, Zheng  */
3249ad48fd75SYan, Zheng int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3250ad48fd75SYan, Zheng 			 struct btrfs_root *root,
3251ad48fd75SYan, Zheng 			 struct btrfs_path *path,
3252ad48fd75SYan, Zheng 			 struct btrfs_key *new_key)
3253ad48fd75SYan, Zheng {
3254ad48fd75SYan, Zheng 	struct extent_buffer *leaf;
3255ad48fd75SYan, Zheng 	int ret;
3256ad48fd75SYan, Zheng 	u32 item_size;
3257ad48fd75SYan, Zheng 
3258ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3259ad48fd75SYan, Zheng 	item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3260ad48fd75SYan, Zheng 	ret = setup_leaf_for_split(trans, root, path,
3261ad48fd75SYan, Zheng 				   item_size + sizeof(struct btrfs_item));
3262ad48fd75SYan, Zheng 	if (ret)
3263ad48fd75SYan, Zheng 		return ret;
3264ad48fd75SYan, Zheng 
3265ad48fd75SYan, Zheng 	path->slots[0]++;
3266ad48fd75SYan, Zheng 	ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3267ad48fd75SYan, Zheng 				     item_size, item_size +
3268ad48fd75SYan, Zheng 				     sizeof(struct btrfs_item), 1);
3269ad48fd75SYan, Zheng 	BUG_ON(ret);
3270ad48fd75SYan, Zheng 
3271ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3272ad48fd75SYan, Zheng 	memcpy_extent_buffer(leaf,
3273ad48fd75SYan, Zheng 			     btrfs_item_ptr_offset(leaf, path->slots[0]),
3274ad48fd75SYan, Zheng 			     btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3275ad48fd75SYan, Zheng 			     item_size);
3276ad48fd75SYan, Zheng 	return 0;
3277459931ecSChris Mason }
3278459931ecSChris Mason 
3279459931ecSChris Mason /*
3280d352ac68SChris Mason  * make the item pointed to by the path smaller.  new_size indicates
3281d352ac68SChris Mason  * how small to make it, and from_end tells us if we just chop bytes
3282d352ac68SChris Mason  * off the end of the item or if we shift the item to chop bytes off
3283d352ac68SChris Mason  * the front.
3284d352ac68SChris Mason  */
3285b18c6685SChris Mason int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3286b18c6685SChris Mason 			struct btrfs_root *root,
3287b18c6685SChris Mason 			struct btrfs_path *path,
3288179e29e4SChris Mason 			u32 new_size, int from_end)
3289b18c6685SChris Mason {
3290b18c6685SChris Mason 	int slot;
32915f39d397SChris Mason 	struct extent_buffer *leaf;
32925f39d397SChris Mason 	struct btrfs_item *item;
3293b18c6685SChris Mason 	u32 nritems;
3294b18c6685SChris Mason 	unsigned int data_end;
3295b18c6685SChris Mason 	unsigned int old_data_start;
3296b18c6685SChris Mason 	unsigned int old_size;
3297b18c6685SChris Mason 	unsigned int size_diff;
3298b18c6685SChris Mason 	int i;
3299cfed81a0SChris Mason 	struct btrfs_map_token token;
3300cfed81a0SChris Mason 
3301cfed81a0SChris Mason 	btrfs_init_map_token(&token);
3302b18c6685SChris Mason 
33035f39d397SChris Mason 	leaf = path->nodes[0];
3304179e29e4SChris Mason 	slot = path->slots[0];
3305179e29e4SChris Mason 
3306179e29e4SChris Mason 	old_size = btrfs_item_size_nr(leaf, slot);
3307179e29e4SChris Mason 	if (old_size == new_size)
3308179e29e4SChris Mason 		return 0;
3309b18c6685SChris Mason 
33105f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3311b18c6685SChris Mason 	data_end = leaf_data_end(root, leaf);
3312b18c6685SChris Mason 
33135f39d397SChris Mason 	old_data_start = btrfs_item_offset_nr(leaf, slot);
3314179e29e4SChris Mason 
3315b18c6685SChris Mason 	size_diff = old_size - new_size;
3316b18c6685SChris Mason 
3317b18c6685SChris Mason 	BUG_ON(slot < 0);
3318b18c6685SChris Mason 	BUG_ON(slot >= nritems);
3319b18c6685SChris Mason 
3320b18c6685SChris Mason 	/*
3321b18c6685SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3322b18c6685SChris Mason 	 */
3323b18c6685SChris Mason 	/* first correct the data pointers */
3324b18c6685SChris Mason 	for (i = slot; i < nritems; i++) {
33255f39d397SChris Mason 		u32 ioff;
33265f39d397SChris Mason 		item = btrfs_item_nr(leaf, i);
3327db94535dSChris Mason 
3328cfed81a0SChris Mason 		ioff = btrfs_token_item_offset(leaf, item, &token);
3329cfed81a0SChris Mason 		btrfs_set_token_item_offset(leaf, item,
3330cfed81a0SChris Mason 					    ioff + size_diff, &token);
3331b18c6685SChris Mason 	}
3332db94535dSChris Mason 
3333b18c6685SChris Mason 	/* shift the data */
3334179e29e4SChris Mason 	if (from_end) {
33355f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3336b18c6685SChris Mason 			      data_end + size_diff, btrfs_leaf_data(leaf) +
3337b18c6685SChris Mason 			      data_end, old_data_start + new_size - data_end);
3338179e29e4SChris Mason 	} else {
3339179e29e4SChris Mason 		struct btrfs_disk_key disk_key;
3340179e29e4SChris Mason 		u64 offset;
3341179e29e4SChris Mason 
3342179e29e4SChris Mason 		btrfs_item_key(leaf, &disk_key, slot);
3343179e29e4SChris Mason 
3344179e29e4SChris Mason 		if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3345179e29e4SChris Mason 			unsigned long ptr;
3346179e29e4SChris Mason 			struct btrfs_file_extent_item *fi;
3347179e29e4SChris Mason 
3348179e29e4SChris Mason 			fi = btrfs_item_ptr(leaf, slot,
3349179e29e4SChris Mason 					    struct btrfs_file_extent_item);
3350179e29e4SChris Mason 			fi = (struct btrfs_file_extent_item *)(
3351179e29e4SChris Mason 			     (unsigned long)fi - size_diff);
3352179e29e4SChris Mason 
3353179e29e4SChris Mason 			if (btrfs_file_extent_type(leaf, fi) ==
3354179e29e4SChris Mason 			    BTRFS_FILE_EXTENT_INLINE) {
3355179e29e4SChris Mason 				ptr = btrfs_item_ptr_offset(leaf, slot);
3356179e29e4SChris Mason 				memmove_extent_buffer(leaf, ptr,
3357179e29e4SChris Mason 				      (unsigned long)fi,
3358179e29e4SChris Mason 				      offsetof(struct btrfs_file_extent_item,
3359179e29e4SChris Mason 						 disk_bytenr));
3360179e29e4SChris Mason 			}
3361179e29e4SChris Mason 		}
3362179e29e4SChris Mason 
3363179e29e4SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3364179e29e4SChris Mason 			      data_end + size_diff, btrfs_leaf_data(leaf) +
3365179e29e4SChris Mason 			      data_end, old_data_start - data_end);
3366179e29e4SChris Mason 
3367179e29e4SChris Mason 		offset = btrfs_disk_key_offset(&disk_key);
3368179e29e4SChris Mason 		btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3369179e29e4SChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot);
3370179e29e4SChris Mason 		if (slot == 0)
3371179e29e4SChris Mason 			fixup_low_keys(trans, root, path, &disk_key, 1);
3372179e29e4SChris Mason 	}
33735f39d397SChris Mason 
33745f39d397SChris Mason 	item = btrfs_item_nr(leaf, slot);
33755f39d397SChris Mason 	btrfs_set_item_size(leaf, item, new_size);
33765f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
3377b18c6685SChris Mason 
33785f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
33795f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
3380b18c6685SChris Mason 		BUG();
33815f39d397SChris Mason 	}
33821cd30799STsutomu Itoh 	return 0;
3383b18c6685SChris Mason }
3384b18c6685SChris Mason 
3385d352ac68SChris Mason /*
3386d352ac68SChris Mason  * make the item pointed to by the path bigger, data_size is the new size.
3387d352ac68SChris Mason  */
33885f39d397SChris Mason int btrfs_extend_item(struct btrfs_trans_handle *trans,
33895f39d397SChris Mason 		      struct btrfs_root *root, struct btrfs_path *path,
33905f39d397SChris Mason 		      u32 data_size)
33916567e837SChris Mason {
33926567e837SChris Mason 	int slot;
33935f39d397SChris Mason 	struct extent_buffer *leaf;
33945f39d397SChris Mason 	struct btrfs_item *item;
33956567e837SChris Mason 	u32 nritems;
33966567e837SChris Mason 	unsigned int data_end;
33976567e837SChris Mason 	unsigned int old_data;
33986567e837SChris Mason 	unsigned int old_size;
33996567e837SChris Mason 	int i;
3400cfed81a0SChris Mason 	struct btrfs_map_token token;
3401cfed81a0SChris Mason 
3402cfed81a0SChris Mason 	btrfs_init_map_token(&token);
34036567e837SChris Mason 
34045f39d397SChris Mason 	leaf = path->nodes[0];
34056567e837SChris Mason 
34065f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
34076567e837SChris Mason 	data_end = leaf_data_end(root, leaf);
34086567e837SChris Mason 
34095f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < data_size) {
34105f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
34116567e837SChris Mason 		BUG();
34125f39d397SChris Mason 	}
34136567e837SChris Mason 	slot = path->slots[0];
34145f39d397SChris Mason 	old_data = btrfs_item_end_nr(leaf, slot);
34156567e837SChris Mason 
34166567e837SChris Mason 	BUG_ON(slot < 0);
34173326d1b0SChris Mason 	if (slot >= nritems) {
34183326d1b0SChris Mason 		btrfs_print_leaf(root, leaf);
3419d397712bSChris Mason 		printk(KERN_CRIT "slot %d too large, nritems %d\n",
3420d397712bSChris Mason 		       slot, nritems);
34213326d1b0SChris Mason 		BUG_ON(1);
34223326d1b0SChris Mason 	}
34236567e837SChris Mason 
34246567e837SChris Mason 	/*
34256567e837SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
34266567e837SChris Mason 	 */
34276567e837SChris Mason 	/* first correct the data pointers */
34286567e837SChris Mason 	for (i = slot; i < nritems; i++) {
34295f39d397SChris Mason 		u32 ioff;
34305f39d397SChris Mason 		item = btrfs_item_nr(leaf, i);
3431db94535dSChris Mason 
3432cfed81a0SChris Mason 		ioff = btrfs_token_item_offset(leaf, item, &token);
3433cfed81a0SChris Mason 		btrfs_set_token_item_offset(leaf, item,
3434cfed81a0SChris Mason 					    ioff - data_size, &token);
34356567e837SChris Mason 	}
34365f39d397SChris Mason 
34376567e837SChris Mason 	/* shift the data */
34385f39d397SChris Mason 	memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
34396567e837SChris Mason 		      data_end - data_size, btrfs_leaf_data(leaf) +
34406567e837SChris Mason 		      data_end, old_data - data_end);
34415f39d397SChris Mason 
34426567e837SChris Mason 	data_end = old_data;
34435f39d397SChris Mason 	old_size = btrfs_item_size_nr(leaf, slot);
34445f39d397SChris Mason 	item = btrfs_item_nr(leaf, slot);
34455f39d397SChris Mason 	btrfs_set_item_size(leaf, item, old_size + data_size);
34465f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
34476567e837SChris Mason 
34485f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
34495f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
34506567e837SChris Mason 		BUG();
34515f39d397SChris Mason 	}
34521cd30799STsutomu Itoh 	return 0;
34536567e837SChris Mason }
34546567e837SChris Mason 
345574123bd7SChris Mason /*
3456d352ac68SChris Mason  * Given a key and some data, insert items into the tree.
345774123bd7SChris Mason  * This does all the path init required, making room in the tree if needed.
3458f3465ca4SJosef Bacik  * Returns the number of keys that were inserted.
3459f3465ca4SJosef Bacik  */
3460f3465ca4SJosef Bacik int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3461f3465ca4SJosef Bacik 			    struct btrfs_root *root,
3462f3465ca4SJosef Bacik 			    struct btrfs_path *path,
3463f3465ca4SJosef Bacik 			    struct btrfs_key *cpu_key, u32 *data_size,
3464f3465ca4SJosef Bacik 			    int nr)
3465f3465ca4SJosef Bacik {
3466f3465ca4SJosef Bacik 	struct extent_buffer *leaf;
3467f3465ca4SJosef Bacik 	struct btrfs_item *item;
3468f3465ca4SJosef Bacik 	int ret = 0;
3469f3465ca4SJosef Bacik 	int slot;
3470f3465ca4SJosef Bacik 	int i;
3471f3465ca4SJosef Bacik 	u32 nritems;
3472f3465ca4SJosef Bacik 	u32 total_data = 0;
3473f3465ca4SJosef Bacik 	u32 total_size = 0;
3474f3465ca4SJosef Bacik 	unsigned int data_end;
3475f3465ca4SJosef Bacik 	struct btrfs_disk_key disk_key;
3476f3465ca4SJosef Bacik 	struct btrfs_key found_key;
3477cfed81a0SChris Mason 	struct btrfs_map_token token;
3478cfed81a0SChris Mason 
3479cfed81a0SChris Mason 	btrfs_init_map_token(&token);
3480f3465ca4SJosef Bacik 
348187b29b20SYan Zheng 	for (i = 0; i < nr; i++) {
348287b29b20SYan Zheng 		if (total_size + data_size[i] + sizeof(struct btrfs_item) >
348387b29b20SYan Zheng 		    BTRFS_LEAF_DATA_SIZE(root)) {
348487b29b20SYan Zheng 			break;
348587b29b20SYan Zheng 			nr = i;
348687b29b20SYan Zheng 		}
3487f3465ca4SJosef Bacik 		total_data += data_size[i];
348887b29b20SYan Zheng 		total_size += data_size[i] + sizeof(struct btrfs_item);
348987b29b20SYan Zheng 	}
349087b29b20SYan Zheng 	BUG_ON(nr == 0);
3491f3465ca4SJosef Bacik 
3492f3465ca4SJosef Bacik 	ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3493f3465ca4SJosef Bacik 	if (ret == 0)
3494f3465ca4SJosef Bacik 		return -EEXIST;
3495f3465ca4SJosef Bacik 	if (ret < 0)
3496f3465ca4SJosef Bacik 		goto out;
3497f3465ca4SJosef Bacik 
3498f3465ca4SJosef Bacik 	leaf = path->nodes[0];
3499f3465ca4SJosef Bacik 
3500f3465ca4SJosef Bacik 	nritems = btrfs_header_nritems(leaf);
3501f3465ca4SJosef Bacik 	data_end = leaf_data_end(root, leaf);
3502f3465ca4SJosef Bacik 
3503f3465ca4SJosef Bacik 	if (btrfs_leaf_free_space(root, leaf) < total_size) {
3504f3465ca4SJosef Bacik 		for (i = nr; i >= 0; i--) {
3505f3465ca4SJosef Bacik 			total_data -= data_size[i];
3506f3465ca4SJosef Bacik 			total_size -= data_size[i] + sizeof(struct btrfs_item);
3507f3465ca4SJosef Bacik 			if (total_size < btrfs_leaf_free_space(root, leaf))
3508f3465ca4SJosef Bacik 				break;
3509f3465ca4SJosef Bacik 		}
3510f3465ca4SJosef Bacik 		nr = i;
3511f3465ca4SJosef Bacik 	}
3512f3465ca4SJosef Bacik 
3513f3465ca4SJosef Bacik 	slot = path->slots[0];
3514f3465ca4SJosef Bacik 	BUG_ON(slot < 0);
3515f3465ca4SJosef Bacik 
3516f3465ca4SJosef Bacik 	if (slot != nritems) {
3517f3465ca4SJosef Bacik 		unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3518f3465ca4SJosef Bacik 
3519f3465ca4SJosef Bacik 		item = btrfs_item_nr(leaf, slot);
3520f3465ca4SJosef Bacik 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
3521f3465ca4SJosef Bacik 
3522f3465ca4SJosef Bacik 		/* figure out how many keys we can insert in here */
3523f3465ca4SJosef Bacik 		total_data = data_size[0];
3524f3465ca4SJosef Bacik 		for (i = 1; i < nr; i++) {
35255d4f98a2SYan Zheng 			if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3526f3465ca4SJosef Bacik 				break;
3527f3465ca4SJosef Bacik 			total_data += data_size[i];
3528f3465ca4SJosef Bacik 		}
3529f3465ca4SJosef Bacik 		nr = i;
3530f3465ca4SJosef Bacik 
3531f3465ca4SJosef Bacik 		if (old_data < data_end) {
3532f3465ca4SJosef Bacik 			btrfs_print_leaf(root, leaf);
3533d397712bSChris Mason 			printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3534f3465ca4SJosef Bacik 			       slot, old_data, data_end);
3535f3465ca4SJosef Bacik 			BUG_ON(1);
3536f3465ca4SJosef Bacik 		}
3537f3465ca4SJosef Bacik 		/*
3538f3465ca4SJosef Bacik 		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3539f3465ca4SJosef Bacik 		 */
3540f3465ca4SJosef Bacik 		/* first correct the data pointers */
3541f3465ca4SJosef Bacik 		for (i = slot; i < nritems; i++) {
3542f3465ca4SJosef Bacik 			u32 ioff;
3543f3465ca4SJosef Bacik 
3544f3465ca4SJosef Bacik 			item = btrfs_item_nr(leaf, i);
3545cfed81a0SChris Mason 			ioff = btrfs_token_item_offset(leaf, item, &token);
3546cfed81a0SChris Mason 			btrfs_set_token_item_offset(leaf, item,
3547cfed81a0SChris Mason 						    ioff - total_data, &token);
3548f3465ca4SJosef Bacik 		}
3549f3465ca4SJosef Bacik 		/* shift the items */
3550f3465ca4SJosef Bacik 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3551f3465ca4SJosef Bacik 			      btrfs_item_nr_offset(slot),
3552f3465ca4SJosef Bacik 			      (nritems - slot) * sizeof(struct btrfs_item));
3553f3465ca4SJosef Bacik 
3554f3465ca4SJosef Bacik 		/* shift the data */
3555f3465ca4SJosef Bacik 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3556f3465ca4SJosef Bacik 			      data_end - total_data, btrfs_leaf_data(leaf) +
3557f3465ca4SJosef Bacik 			      data_end, old_data - data_end);
3558f3465ca4SJosef Bacik 		data_end = old_data;
3559f3465ca4SJosef Bacik 	} else {
3560f3465ca4SJosef Bacik 		/*
3561f3465ca4SJosef Bacik 		 * this sucks but it has to be done, if we are inserting at
3562f3465ca4SJosef Bacik 		 * the end of the leaf only insert 1 of the items, since we
3563f3465ca4SJosef Bacik 		 * have no way of knowing whats on the next leaf and we'd have
3564f3465ca4SJosef Bacik 		 * to drop our current locks to figure it out
3565f3465ca4SJosef Bacik 		 */
3566f3465ca4SJosef Bacik 		nr = 1;
3567f3465ca4SJosef Bacik 	}
3568f3465ca4SJosef Bacik 
3569f3465ca4SJosef Bacik 	/* setup the item for the new data */
3570f3465ca4SJosef Bacik 	for (i = 0; i < nr; i++) {
3571f3465ca4SJosef Bacik 		btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3572f3465ca4SJosef Bacik 		btrfs_set_item_key(leaf, &disk_key, slot + i);
3573f3465ca4SJosef Bacik 		item = btrfs_item_nr(leaf, slot + i);
3574cfed81a0SChris Mason 		btrfs_set_token_item_offset(leaf, item,
3575cfed81a0SChris Mason 					    data_end - data_size[i], &token);
3576f3465ca4SJosef Bacik 		data_end -= data_size[i];
3577cfed81a0SChris Mason 		btrfs_set_token_item_size(leaf, item, data_size[i], &token);
3578f3465ca4SJosef Bacik 	}
3579f3465ca4SJosef Bacik 	btrfs_set_header_nritems(leaf, nritems + nr);
3580f3465ca4SJosef Bacik 	btrfs_mark_buffer_dirty(leaf);
3581f3465ca4SJosef Bacik 
3582f3465ca4SJosef Bacik 	ret = 0;
3583f3465ca4SJosef Bacik 	if (slot == 0) {
3584f3465ca4SJosef Bacik 		btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3585f3465ca4SJosef Bacik 		ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3586f3465ca4SJosef Bacik 	}
3587f3465ca4SJosef Bacik 
3588f3465ca4SJosef Bacik 	if (btrfs_leaf_free_space(root, leaf) < 0) {
3589f3465ca4SJosef Bacik 		btrfs_print_leaf(root, leaf);
3590f3465ca4SJosef Bacik 		BUG();
3591f3465ca4SJosef Bacik 	}
3592f3465ca4SJosef Bacik out:
3593f3465ca4SJosef Bacik 	if (!ret)
3594f3465ca4SJosef Bacik 		ret = nr;
3595f3465ca4SJosef Bacik 	return ret;
3596f3465ca4SJosef Bacik }
3597f3465ca4SJosef Bacik 
3598f3465ca4SJosef Bacik /*
359944871b1bSChris Mason  * this is a helper for btrfs_insert_empty_items, the main goal here is
360044871b1bSChris Mason  * to save stack depth by doing the bulk of the work in a function
360144871b1bSChris Mason  * that doesn't call btrfs_search_slot
360274123bd7SChris Mason  */
360316cdcec7SMiao Xie int setup_items_for_insert(struct btrfs_trans_handle *trans,
360444871b1bSChris Mason 			   struct btrfs_root *root, struct btrfs_path *path,
36059c58309dSChris Mason 			   struct btrfs_key *cpu_key, u32 *data_size,
360644871b1bSChris Mason 			   u32 total_data, u32 total_size, int nr)
3607be0e5c09SChris Mason {
36085f39d397SChris Mason 	struct btrfs_item *item;
36099c58309dSChris Mason 	int i;
36107518a238SChris Mason 	u32 nritems;
3611be0e5c09SChris Mason 	unsigned int data_end;
3612e2fa7227SChris Mason 	struct btrfs_disk_key disk_key;
361344871b1bSChris Mason 	int ret;
361444871b1bSChris Mason 	struct extent_buffer *leaf;
361544871b1bSChris Mason 	int slot;
3616cfed81a0SChris Mason 	struct btrfs_map_token token;
3617cfed81a0SChris Mason 
3618cfed81a0SChris Mason 	btrfs_init_map_token(&token);
3619e2fa7227SChris Mason 
36205f39d397SChris Mason 	leaf = path->nodes[0];
362144871b1bSChris Mason 	slot = path->slots[0];
362274123bd7SChris Mason 
36235f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3624123abc88SChris Mason 	data_end = leaf_data_end(root, leaf);
3625eb60ceacSChris Mason 
3626f25956ccSChris Mason 	if (btrfs_leaf_free_space(root, leaf) < total_size) {
36273326d1b0SChris Mason 		btrfs_print_leaf(root, leaf);
3628d397712bSChris Mason 		printk(KERN_CRIT "not enough freespace need %u have %d\n",
36299c58309dSChris Mason 		       total_size, btrfs_leaf_free_space(root, leaf));
3630be0e5c09SChris Mason 		BUG();
3631d4dbff95SChris Mason 	}
36325f39d397SChris Mason 
3633be0e5c09SChris Mason 	if (slot != nritems) {
36345f39d397SChris Mason 		unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3635be0e5c09SChris Mason 
36365f39d397SChris Mason 		if (old_data < data_end) {
36375f39d397SChris Mason 			btrfs_print_leaf(root, leaf);
3638d397712bSChris Mason 			printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
36395f39d397SChris Mason 			       slot, old_data, data_end);
36405f39d397SChris Mason 			BUG_ON(1);
36415f39d397SChris Mason 		}
3642be0e5c09SChris Mason 		/*
3643be0e5c09SChris Mason 		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3644be0e5c09SChris Mason 		 */
3645be0e5c09SChris Mason 		/* first correct the data pointers */
36460783fcfcSChris Mason 		for (i = slot; i < nritems; i++) {
36475f39d397SChris Mason 			u32 ioff;
3648db94535dSChris Mason 
36495f39d397SChris Mason 			item = btrfs_item_nr(leaf, i);
3650cfed81a0SChris Mason 			ioff = btrfs_token_item_offset(leaf, item, &token);
3651cfed81a0SChris Mason 			btrfs_set_token_item_offset(leaf, item,
3652cfed81a0SChris Mason 						    ioff - total_data, &token);
36530783fcfcSChris Mason 		}
3654be0e5c09SChris Mason 		/* shift the items */
36559c58309dSChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
36565f39d397SChris Mason 			      btrfs_item_nr_offset(slot),
36570783fcfcSChris Mason 			      (nritems - slot) * sizeof(struct btrfs_item));
3658be0e5c09SChris Mason 
3659be0e5c09SChris Mason 		/* shift the data */
36605f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
36619c58309dSChris Mason 			      data_end - total_data, btrfs_leaf_data(leaf) +
3662be0e5c09SChris Mason 			      data_end, old_data - data_end);
3663be0e5c09SChris Mason 		data_end = old_data;
3664be0e5c09SChris Mason 	}
36655f39d397SChris Mason 
366662e2749eSChris Mason 	/* setup the item for the new data */
36679c58309dSChris Mason 	for (i = 0; i < nr; i++) {
36689c58309dSChris Mason 		btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
36699c58309dSChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot + i);
36709c58309dSChris Mason 		item = btrfs_item_nr(leaf, slot + i);
3671cfed81a0SChris Mason 		btrfs_set_token_item_offset(leaf, item,
3672cfed81a0SChris Mason 					    data_end - data_size[i], &token);
36739c58309dSChris Mason 		data_end -= data_size[i];
3674cfed81a0SChris Mason 		btrfs_set_token_item_size(leaf, item, data_size[i], &token);
36759c58309dSChris Mason 	}
367644871b1bSChris Mason 
36779c58309dSChris Mason 	btrfs_set_header_nritems(leaf, nritems + nr);
3678aa5d6bedSChris Mason 
3679aa5d6bedSChris Mason 	ret = 0;
36805a01a2e3SChris Mason 	if (slot == 0) {
36815a01a2e3SChris Mason 		btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3682e089f05cSChris Mason 		ret = fixup_low_keys(trans, root, path, &disk_key, 1);
36835a01a2e3SChris Mason 	}
3684b9473439SChris Mason 	btrfs_unlock_up_safe(path, 1);
3685b9473439SChris Mason 	btrfs_mark_buffer_dirty(leaf);
3686aa5d6bedSChris Mason 
36875f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
36885f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
3689be0e5c09SChris Mason 		BUG();
36905f39d397SChris Mason 	}
369144871b1bSChris Mason 	return ret;
369244871b1bSChris Mason }
369344871b1bSChris Mason 
369444871b1bSChris Mason /*
369544871b1bSChris Mason  * Given a key and some data, insert items into the tree.
369644871b1bSChris Mason  * This does all the path init required, making room in the tree if needed.
369744871b1bSChris Mason  */
369844871b1bSChris Mason int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
369944871b1bSChris Mason 			    struct btrfs_root *root,
370044871b1bSChris Mason 			    struct btrfs_path *path,
370144871b1bSChris Mason 			    struct btrfs_key *cpu_key, u32 *data_size,
370244871b1bSChris Mason 			    int nr)
370344871b1bSChris Mason {
370444871b1bSChris Mason 	int ret = 0;
370544871b1bSChris Mason 	int slot;
370644871b1bSChris Mason 	int i;
370744871b1bSChris Mason 	u32 total_size = 0;
370844871b1bSChris Mason 	u32 total_data = 0;
370944871b1bSChris Mason 
371044871b1bSChris Mason 	for (i = 0; i < nr; i++)
371144871b1bSChris Mason 		total_data += data_size[i];
371244871b1bSChris Mason 
371344871b1bSChris Mason 	total_size = total_data + (nr * sizeof(struct btrfs_item));
371444871b1bSChris Mason 	ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
371544871b1bSChris Mason 	if (ret == 0)
371644871b1bSChris Mason 		return -EEXIST;
371744871b1bSChris Mason 	if (ret < 0)
371844871b1bSChris Mason 		goto out;
371944871b1bSChris Mason 
372044871b1bSChris Mason 	slot = path->slots[0];
372144871b1bSChris Mason 	BUG_ON(slot < 0);
372244871b1bSChris Mason 
372344871b1bSChris Mason 	ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
372444871b1bSChris Mason 			       total_data, total_size, nr);
372544871b1bSChris Mason 
3726ed2ff2cbSChris Mason out:
372762e2749eSChris Mason 	return ret;
372862e2749eSChris Mason }
372962e2749eSChris Mason 
373062e2749eSChris Mason /*
373162e2749eSChris Mason  * Given a key and some data, insert an item into the tree.
373262e2749eSChris Mason  * This does all the path init required, making room in the tree if needed.
373362e2749eSChris Mason  */
3734e089f05cSChris Mason int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3735e089f05cSChris Mason 		      *root, struct btrfs_key *cpu_key, void *data, u32
3736e089f05cSChris Mason 		      data_size)
373762e2749eSChris Mason {
373862e2749eSChris Mason 	int ret = 0;
37392c90e5d6SChris Mason 	struct btrfs_path *path;
37405f39d397SChris Mason 	struct extent_buffer *leaf;
37415f39d397SChris Mason 	unsigned long ptr;
374262e2749eSChris Mason 
37432c90e5d6SChris Mason 	path = btrfs_alloc_path();
3744db5b493aSTsutomu Itoh 	if (!path)
3745db5b493aSTsutomu Itoh 		return -ENOMEM;
37462c90e5d6SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
374762e2749eSChris Mason 	if (!ret) {
37485f39d397SChris Mason 		leaf = path->nodes[0];
37495f39d397SChris Mason 		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
37505f39d397SChris Mason 		write_extent_buffer(leaf, data, ptr, data_size);
37515f39d397SChris Mason 		btrfs_mark_buffer_dirty(leaf);
375262e2749eSChris Mason 	}
37532c90e5d6SChris Mason 	btrfs_free_path(path);
3754aa5d6bedSChris Mason 	return ret;
3755be0e5c09SChris Mason }
3756be0e5c09SChris Mason 
375774123bd7SChris Mason /*
37585de08d7dSChris Mason  * delete the pointer from a given node.
375974123bd7SChris Mason  *
3760d352ac68SChris Mason  * the tree should have been previously balanced so the deletion does not
3761d352ac68SChris Mason  * empty a node.
376274123bd7SChris Mason  */
3763e089f05cSChris Mason static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3764e089f05cSChris Mason 		   struct btrfs_path *path, int level, int slot)
3765be0e5c09SChris Mason {
37665f39d397SChris Mason 	struct extent_buffer *parent = path->nodes[level];
37677518a238SChris Mason 	u32 nritems;
3768aa5d6bedSChris Mason 	int ret = 0;
3769bb803951SChris Mason 	int wret;
3770be0e5c09SChris Mason 
37715f39d397SChris Mason 	nritems = btrfs_header_nritems(parent);
3772be0e5c09SChris Mason 	if (slot != nritems - 1) {
37735f39d397SChris Mason 		memmove_extent_buffer(parent,
37745f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot),
37755f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot + 1),
3776d6025579SChris Mason 			      sizeof(struct btrfs_key_ptr) *
3777d6025579SChris Mason 			      (nritems - slot - 1));
3778be0e5c09SChris Mason 	}
37797518a238SChris Mason 	nritems--;
37805f39d397SChris Mason 	btrfs_set_header_nritems(parent, nritems);
37817518a238SChris Mason 	if (nritems == 0 && parent == root->node) {
37825f39d397SChris Mason 		BUG_ON(btrfs_header_level(root->node) != 1);
3783eb60ceacSChris Mason 		/* just turn the root into a leaf and break */
37845f39d397SChris Mason 		btrfs_set_header_level(root->node, 0);
3785bb803951SChris Mason 	} else if (slot == 0) {
37865f39d397SChris Mason 		struct btrfs_disk_key disk_key;
37875f39d397SChris Mason 
37885f39d397SChris Mason 		btrfs_node_key(parent, &disk_key, 0);
37895f39d397SChris Mason 		wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
37900f70abe2SChris Mason 		if (wret)
37910f70abe2SChris Mason 			ret = wret;
3792be0e5c09SChris Mason 	}
3793d6025579SChris Mason 	btrfs_mark_buffer_dirty(parent);
3794aa5d6bedSChris Mason 	return ret;
3795be0e5c09SChris Mason }
3796be0e5c09SChris Mason 
379774123bd7SChris Mason /*
3798323ac95bSChris Mason  * a helper function to delete the leaf pointed to by path->slots[1] and
37995d4f98a2SYan Zheng  * path->nodes[1].
3800323ac95bSChris Mason  *
3801323ac95bSChris Mason  * This deletes the pointer in path->nodes[1] and frees the leaf
3802323ac95bSChris Mason  * block extent.  zero is returned if it all worked out, < 0 otherwise.
3803323ac95bSChris Mason  *
3804323ac95bSChris Mason  * The path must have already been setup for deleting the leaf, including
3805323ac95bSChris Mason  * all the proper balancing.  path->nodes[1] must be locked.
3806323ac95bSChris Mason  */
38075d4f98a2SYan Zheng static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3808323ac95bSChris Mason 				   struct btrfs_root *root,
38095d4f98a2SYan Zheng 				   struct btrfs_path *path,
38105d4f98a2SYan Zheng 				   struct extent_buffer *leaf)
3811323ac95bSChris Mason {
3812323ac95bSChris Mason 	int ret;
3813323ac95bSChris Mason 
38145d4f98a2SYan Zheng 	WARN_ON(btrfs_header_generation(leaf) != trans->transid);
3815323ac95bSChris Mason 	ret = del_ptr(trans, root, path, 1, path->slots[1]);
3816323ac95bSChris Mason 	if (ret)
3817323ac95bSChris Mason 		return ret;
3818323ac95bSChris Mason 
38194d081c41SChris Mason 	/*
38204d081c41SChris Mason 	 * btrfs_free_extent is expensive, we want to make sure we
38214d081c41SChris Mason 	 * aren't holding any locks when we call it
38224d081c41SChris Mason 	 */
38234d081c41SChris Mason 	btrfs_unlock_up_safe(path, 0);
38244d081c41SChris Mason 
3825f0486c68SYan, Zheng 	root_sub_used(root, leaf->len);
3826f0486c68SYan, Zheng 
38273083ee2eSJosef Bacik 	extent_buffer_get(leaf);
382866d7e7f0SArne Jansen 	btrfs_free_tree_block(trans, root, leaf, 0, 1, 0);
38293083ee2eSJosef Bacik 	free_extent_buffer_stale(leaf);
3830f0486c68SYan, Zheng 	return 0;
3831323ac95bSChris Mason }
3832323ac95bSChris Mason /*
383374123bd7SChris Mason  * delete the item at the leaf level in path.  If that empties
383474123bd7SChris Mason  * the leaf, remove it from the tree
383574123bd7SChris Mason  */
383685e21bacSChris Mason int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
383785e21bacSChris Mason 		    struct btrfs_path *path, int slot, int nr)
3838be0e5c09SChris Mason {
38395f39d397SChris Mason 	struct extent_buffer *leaf;
38405f39d397SChris Mason 	struct btrfs_item *item;
384185e21bacSChris Mason 	int last_off;
384285e21bacSChris Mason 	int dsize = 0;
3843aa5d6bedSChris Mason 	int ret = 0;
3844aa5d6bedSChris Mason 	int wret;
384585e21bacSChris Mason 	int i;
38467518a238SChris Mason 	u32 nritems;
3847cfed81a0SChris Mason 	struct btrfs_map_token token;
3848cfed81a0SChris Mason 
3849cfed81a0SChris Mason 	btrfs_init_map_token(&token);
3850be0e5c09SChris Mason 
38515f39d397SChris Mason 	leaf = path->nodes[0];
385285e21bacSChris Mason 	last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
385385e21bacSChris Mason 
385485e21bacSChris Mason 	for (i = 0; i < nr; i++)
385585e21bacSChris Mason 		dsize += btrfs_item_size_nr(leaf, slot + i);
385685e21bacSChris Mason 
38575f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3858be0e5c09SChris Mason 
385985e21bacSChris Mason 	if (slot + nr != nritems) {
3860123abc88SChris Mason 		int data_end = leaf_data_end(root, leaf);
38615f39d397SChris Mason 
38625f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3863d6025579SChris Mason 			      data_end + dsize,
3864123abc88SChris Mason 			      btrfs_leaf_data(leaf) + data_end,
386585e21bacSChris Mason 			      last_off - data_end);
38665f39d397SChris Mason 
386785e21bacSChris Mason 		for (i = slot + nr; i < nritems; i++) {
38685f39d397SChris Mason 			u32 ioff;
3869db94535dSChris Mason 
38705f39d397SChris Mason 			item = btrfs_item_nr(leaf, i);
3871cfed81a0SChris Mason 			ioff = btrfs_token_item_offset(leaf, item, &token);
3872cfed81a0SChris Mason 			btrfs_set_token_item_offset(leaf, item,
3873cfed81a0SChris Mason 						    ioff + dsize, &token);
38740783fcfcSChris Mason 		}
3875db94535dSChris Mason 
38765f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
387785e21bacSChris Mason 			      btrfs_item_nr_offset(slot + nr),
38780783fcfcSChris Mason 			      sizeof(struct btrfs_item) *
387985e21bacSChris Mason 			      (nritems - slot - nr));
3880be0e5c09SChris Mason 	}
388185e21bacSChris Mason 	btrfs_set_header_nritems(leaf, nritems - nr);
388285e21bacSChris Mason 	nritems -= nr;
38835f39d397SChris Mason 
388474123bd7SChris Mason 	/* delete the leaf if we've emptied it */
38857518a238SChris Mason 	if (nritems == 0) {
38865f39d397SChris Mason 		if (leaf == root->node) {
38875f39d397SChris Mason 			btrfs_set_header_level(leaf, 0);
38889a8dd150SChris Mason 		} else {
3889f0486c68SYan, Zheng 			btrfs_set_path_blocking(path);
3890f0486c68SYan, Zheng 			clean_tree_block(trans, root, leaf);
38915d4f98a2SYan Zheng 			ret = btrfs_del_leaf(trans, root, path, leaf);
3892323ac95bSChris Mason 			BUG_ON(ret);
38939a8dd150SChris Mason 		}
3894be0e5c09SChris Mason 	} else {
38957518a238SChris Mason 		int used = leaf_space_used(leaf, 0, nritems);
3896aa5d6bedSChris Mason 		if (slot == 0) {
38975f39d397SChris Mason 			struct btrfs_disk_key disk_key;
38985f39d397SChris Mason 
38995f39d397SChris Mason 			btrfs_item_key(leaf, &disk_key, 0);
3900e089f05cSChris Mason 			wret = fixup_low_keys(trans, root, path,
39015f39d397SChris Mason 					      &disk_key, 1);
3902aa5d6bedSChris Mason 			if (wret)
3903aa5d6bedSChris Mason 				ret = wret;
3904aa5d6bedSChris Mason 		}
3905aa5d6bedSChris Mason 
390674123bd7SChris Mason 		/* delete the leaf if it is mostly empty */
3907d717aa1dSYan Zheng 		if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
3908be0e5c09SChris Mason 			/* push_leaf_left fixes the path.
3909be0e5c09SChris Mason 			 * make sure the path still points to our leaf
3910be0e5c09SChris Mason 			 * for possible call to del_ptr below
3911be0e5c09SChris Mason 			 */
39124920c9acSChris Mason 			slot = path->slots[1];
39135f39d397SChris Mason 			extent_buffer_get(leaf);
39145f39d397SChris Mason 
3915b9473439SChris Mason 			btrfs_set_path_blocking(path);
391699d8f83cSChris Mason 			wret = push_leaf_left(trans, root, path, 1, 1,
391799d8f83cSChris Mason 					      1, (u32)-1);
391854aa1f4dSChris Mason 			if (wret < 0 && wret != -ENOSPC)
3919aa5d6bedSChris Mason 				ret = wret;
39205f39d397SChris Mason 
39215f39d397SChris Mason 			if (path->nodes[0] == leaf &&
39225f39d397SChris Mason 			    btrfs_header_nritems(leaf)) {
392399d8f83cSChris Mason 				wret = push_leaf_right(trans, root, path, 1,
392499d8f83cSChris Mason 						       1, 1, 0);
392554aa1f4dSChris Mason 				if (wret < 0 && wret != -ENOSPC)
3926aa5d6bedSChris Mason 					ret = wret;
3927aa5d6bedSChris Mason 			}
39285f39d397SChris Mason 
39295f39d397SChris Mason 			if (btrfs_header_nritems(leaf) == 0) {
3930323ac95bSChris Mason 				path->slots[1] = slot;
39315d4f98a2SYan Zheng 				ret = btrfs_del_leaf(trans, root, path, leaf);
3932323ac95bSChris Mason 				BUG_ON(ret);
39335f39d397SChris Mason 				free_extent_buffer(leaf);
39345de08d7dSChris Mason 			} else {
3935925baeddSChris Mason 				/* if we're still in the path, make sure
3936925baeddSChris Mason 				 * we're dirty.  Otherwise, one of the
3937925baeddSChris Mason 				 * push_leaf functions must have already
3938925baeddSChris Mason 				 * dirtied this buffer
3939925baeddSChris Mason 				 */
3940925baeddSChris Mason 				if (path->nodes[0] == leaf)
39415f39d397SChris Mason 					btrfs_mark_buffer_dirty(leaf);
39425f39d397SChris Mason 				free_extent_buffer(leaf);
3943be0e5c09SChris Mason 			}
3944d5719762SChris Mason 		} else {
39455f39d397SChris Mason 			btrfs_mark_buffer_dirty(leaf);
3946be0e5c09SChris Mason 		}
39479a8dd150SChris Mason 	}
3948aa5d6bedSChris Mason 	return ret;
39499a8dd150SChris Mason }
39509a8dd150SChris Mason 
395197571fd0SChris Mason /*
3952925baeddSChris Mason  * search the tree again to find a leaf with lesser keys
39537bb86316SChris Mason  * returns 0 if it found something or 1 if there are no lesser leaves.
39547bb86316SChris Mason  * returns < 0 on io errors.
3955d352ac68SChris Mason  *
3956d352ac68SChris Mason  * This may release the path, and so you may lose any locks held at the
3957d352ac68SChris Mason  * time you call it.
39587bb86316SChris Mason  */
39597bb86316SChris Mason int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
39607bb86316SChris Mason {
3961925baeddSChris Mason 	struct btrfs_key key;
3962925baeddSChris Mason 	struct btrfs_disk_key found_key;
3963925baeddSChris Mason 	int ret;
39647bb86316SChris Mason 
3965925baeddSChris Mason 	btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
3966925baeddSChris Mason 
3967925baeddSChris Mason 	if (key.offset > 0)
3968925baeddSChris Mason 		key.offset--;
3969925baeddSChris Mason 	else if (key.type > 0)
3970925baeddSChris Mason 		key.type--;
3971925baeddSChris Mason 	else if (key.objectid > 0)
3972925baeddSChris Mason 		key.objectid--;
3973925baeddSChris Mason 	else
39747bb86316SChris Mason 		return 1;
39757bb86316SChris Mason 
3976b3b4aa74SDavid Sterba 	btrfs_release_path(path);
3977925baeddSChris Mason 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3978925baeddSChris Mason 	if (ret < 0)
3979925baeddSChris Mason 		return ret;
3980925baeddSChris Mason 	btrfs_item_key(path->nodes[0], &found_key, 0);
3981925baeddSChris Mason 	ret = comp_keys(&found_key, &key);
3982925baeddSChris Mason 	if (ret < 0)
39837bb86316SChris Mason 		return 0;
3984925baeddSChris Mason 	return 1;
39857bb86316SChris Mason }
39867bb86316SChris Mason 
39873f157a2fSChris Mason /*
39883f157a2fSChris Mason  * A helper function to walk down the tree starting at min_key, and looking
39893f157a2fSChris Mason  * for nodes or leaves that are either in cache or have a minimum
3990d352ac68SChris Mason  * transaction id.  This is used by the btree defrag code, and tree logging
39913f157a2fSChris Mason  *
39923f157a2fSChris Mason  * This does not cow, but it does stuff the starting key it finds back
39933f157a2fSChris Mason  * into min_key, so you can call btrfs_search_slot with cow=1 on the
39943f157a2fSChris Mason  * key and get a writable path.
39953f157a2fSChris Mason  *
39963f157a2fSChris Mason  * This does lock as it descends, and path->keep_locks should be set
39973f157a2fSChris Mason  * to 1 by the caller.
39983f157a2fSChris Mason  *
39993f157a2fSChris Mason  * This honors path->lowest_level to prevent descent past a given level
40003f157a2fSChris Mason  * of the tree.
40013f157a2fSChris Mason  *
4002d352ac68SChris Mason  * min_trans indicates the oldest transaction that you are interested
4003d352ac68SChris Mason  * in walking through.  Any nodes or leaves older than min_trans are
4004d352ac68SChris Mason  * skipped over (without reading them).
4005d352ac68SChris Mason  *
40063f157a2fSChris Mason  * returns zero if something useful was found, < 0 on error and 1 if there
40073f157a2fSChris Mason  * was nothing in the tree that matched the search criteria.
40083f157a2fSChris Mason  */
40093f157a2fSChris Mason int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
4010e02119d5SChris Mason 			 struct btrfs_key *max_key,
40113f157a2fSChris Mason 			 struct btrfs_path *path, int cache_only,
40123f157a2fSChris Mason 			 u64 min_trans)
40133f157a2fSChris Mason {
40143f157a2fSChris Mason 	struct extent_buffer *cur;
40153f157a2fSChris Mason 	struct btrfs_key found_key;
40163f157a2fSChris Mason 	int slot;
40179652480bSYan 	int sret;
40183f157a2fSChris Mason 	u32 nritems;
40193f157a2fSChris Mason 	int level;
40203f157a2fSChris Mason 	int ret = 1;
40213f157a2fSChris Mason 
4022934d375bSChris Mason 	WARN_ON(!path->keep_locks);
40233f157a2fSChris Mason again:
4024bd681513SChris Mason 	cur = btrfs_read_lock_root_node(root);
40253f157a2fSChris Mason 	level = btrfs_header_level(cur);
4026e02119d5SChris Mason 	WARN_ON(path->nodes[level]);
40273f157a2fSChris Mason 	path->nodes[level] = cur;
4028bd681513SChris Mason 	path->locks[level] = BTRFS_READ_LOCK;
40293f157a2fSChris Mason 
40303f157a2fSChris Mason 	if (btrfs_header_generation(cur) < min_trans) {
40313f157a2fSChris Mason 		ret = 1;
40323f157a2fSChris Mason 		goto out;
40333f157a2fSChris Mason 	}
40343f157a2fSChris Mason 	while (1) {
40353f157a2fSChris Mason 		nritems = btrfs_header_nritems(cur);
40363f157a2fSChris Mason 		level = btrfs_header_level(cur);
40379652480bSYan 		sret = bin_search(cur, min_key, level, &slot);
40383f157a2fSChris Mason 
4039323ac95bSChris Mason 		/* at the lowest level, we're done, setup the path and exit */
4040323ac95bSChris Mason 		if (level == path->lowest_level) {
4041e02119d5SChris Mason 			if (slot >= nritems)
4042e02119d5SChris Mason 				goto find_next_key;
40433f157a2fSChris Mason 			ret = 0;
40443f157a2fSChris Mason 			path->slots[level] = slot;
40453f157a2fSChris Mason 			btrfs_item_key_to_cpu(cur, &found_key, slot);
40463f157a2fSChris Mason 			goto out;
40473f157a2fSChris Mason 		}
40489652480bSYan 		if (sret && slot > 0)
40499652480bSYan 			slot--;
40503f157a2fSChris Mason 		/*
40513f157a2fSChris Mason 		 * check this node pointer against the cache_only and
40523f157a2fSChris Mason 		 * min_trans parameters.  If it isn't in cache or is too
40533f157a2fSChris Mason 		 * old, skip to the next one.
40543f157a2fSChris Mason 		 */
40553f157a2fSChris Mason 		while (slot < nritems) {
40563f157a2fSChris Mason 			u64 blockptr;
40573f157a2fSChris Mason 			u64 gen;
40583f157a2fSChris Mason 			struct extent_buffer *tmp;
4059e02119d5SChris Mason 			struct btrfs_disk_key disk_key;
4060e02119d5SChris Mason 
40613f157a2fSChris Mason 			blockptr = btrfs_node_blockptr(cur, slot);
40623f157a2fSChris Mason 			gen = btrfs_node_ptr_generation(cur, slot);
40633f157a2fSChris Mason 			if (gen < min_trans) {
40643f157a2fSChris Mason 				slot++;
40653f157a2fSChris Mason 				continue;
40663f157a2fSChris Mason 			}
40673f157a2fSChris Mason 			if (!cache_only)
40683f157a2fSChris Mason 				break;
40693f157a2fSChris Mason 
4070e02119d5SChris Mason 			if (max_key) {
4071e02119d5SChris Mason 				btrfs_node_key(cur, &disk_key, slot);
4072e02119d5SChris Mason 				if (comp_keys(&disk_key, max_key) >= 0) {
4073e02119d5SChris Mason 					ret = 1;
4074e02119d5SChris Mason 					goto out;
4075e02119d5SChris Mason 				}
4076e02119d5SChris Mason 			}
4077e02119d5SChris Mason 
40783f157a2fSChris Mason 			tmp = btrfs_find_tree_block(root, blockptr,
40793f157a2fSChris Mason 					    btrfs_level_size(root, level - 1));
40803f157a2fSChris Mason 
40813f157a2fSChris Mason 			if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
40823f157a2fSChris Mason 				free_extent_buffer(tmp);
40833f157a2fSChris Mason 				break;
40843f157a2fSChris Mason 			}
40853f157a2fSChris Mason 			if (tmp)
40863f157a2fSChris Mason 				free_extent_buffer(tmp);
40873f157a2fSChris Mason 			slot++;
40883f157a2fSChris Mason 		}
4089e02119d5SChris Mason find_next_key:
40903f157a2fSChris Mason 		/*
40913f157a2fSChris Mason 		 * we didn't find a candidate key in this node, walk forward
40923f157a2fSChris Mason 		 * and find another one
40933f157a2fSChris Mason 		 */
40943f157a2fSChris Mason 		if (slot >= nritems) {
4095e02119d5SChris Mason 			path->slots[level] = slot;
4096b4ce94deSChris Mason 			btrfs_set_path_blocking(path);
4097e02119d5SChris Mason 			sret = btrfs_find_next_key(root, path, min_key, level,
40983f157a2fSChris Mason 						  cache_only, min_trans);
4099e02119d5SChris Mason 			if (sret == 0) {
4100b3b4aa74SDavid Sterba 				btrfs_release_path(path);
41013f157a2fSChris Mason 				goto again;
41023f157a2fSChris Mason 			} else {
41033f157a2fSChris Mason 				goto out;
41043f157a2fSChris Mason 			}
41053f157a2fSChris Mason 		}
41063f157a2fSChris Mason 		/* save our key for returning back */
41073f157a2fSChris Mason 		btrfs_node_key_to_cpu(cur, &found_key, slot);
41083f157a2fSChris Mason 		path->slots[level] = slot;
41093f157a2fSChris Mason 		if (level == path->lowest_level) {
41103f157a2fSChris Mason 			ret = 0;
41113f157a2fSChris Mason 			unlock_up(path, level, 1);
41123f157a2fSChris Mason 			goto out;
41133f157a2fSChris Mason 		}
4114b4ce94deSChris Mason 		btrfs_set_path_blocking(path);
41153f157a2fSChris Mason 		cur = read_node_slot(root, cur, slot);
411697d9a8a4STsutomu Itoh 		BUG_ON(!cur);
41173f157a2fSChris Mason 
4118bd681513SChris Mason 		btrfs_tree_read_lock(cur);
4119b4ce94deSChris Mason 
4120bd681513SChris Mason 		path->locks[level - 1] = BTRFS_READ_LOCK;
41213f157a2fSChris Mason 		path->nodes[level - 1] = cur;
41223f157a2fSChris Mason 		unlock_up(path, level, 1);
4123bd681513SChris Mason 		btrfs_clear_path_blocking(path, NULL, 0);
41243f157a2fSChris Mason 	}
41253f157a2fSChris Mason out:
41263f157a2fSChris Mason 	if (ret == 0)
41273f157a2fSChris Mason 		memcpy(min_key, &found_key, sizeof(found_key));
4128b4ce94deSChris Mason 	btrfs_set_path_blocking(path);
41293f157a2fSChris Mason 	return ret;
41303f157a2fSChris Mason }
41313f157a2fSChris Mason 
41323f157a2fSChris Mason /*
41333f157a2fSChris Mason  * this is similar to btrfs_next_leaf, but does not try to preserve
41343f157a2fSChris Mason  * and fixup the path.  It looks for and returns the next key in the
41353f157a2fSChris Mason  * tree based on the current path and the cache_only and min_trans
41363f157a2fSChris Mason  * parameters.
41373f157a2fSChris Mason  *
41383f157a2fSChris Mason  * 0 is returned if another key is found, < 0 if there are any errors
41393f157a2fSChris Mason  * and 1 is returned if there are no higher keys in the tree
41403f157a2fSChris Mason  *
41413f157a2fSChris Mason  * path->keep_locks should be set to 1 on the search made before
41423f157a2fSChris Mason  * calling this function.
41433f157a2fSChris Mason  */
4144e7a84565SChris Mason int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
414533c66f43SYan Zheng 			struct btrfs_key *key, int level,
41463f157a2fSChris Mason 			int cache_only, u64 min_trans)
4147e7a84565SChris Mason {
4148e7a84565SChris Mason 	int slot;
4149e7a84565SChris Mason 	struct extent_buffer *c;
4150e7a84565SChris Mason 
4151934d375bSChris Mason 	WARN_ON(!path->keep_locks);
4152e7a84565SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
4153e7a84565SChris Mason 		if (!path->nodes[level])
4154e7a84565SChris Mason 			return 1;
4155e7a84565SChris Mason 
4156e7a84565SChris Mason 		slot = path->slots[level] + 1;
4157e7a84565SChris Mason 		c = path->nodes[level];
41583f157a2fSChris Mason next:
4159e7a84565SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
416033c66f43SYan Zheng 			int ret;
416133c66f43SYan Zheng 			int orig_lowest;
416233c66f43SYan Zheng 			struct btrfs_key cur_key;
416333c66f43SYan Zheng 			if (level + 1 >= BTRFS_MAX_LEVEL ||
416433c66f43SYan Zheng 			    !path->nodes[level + 1])
4165e7a84565SChris Mason 				return 1;
416633c66f43SYan Zheng 
416733c66f43SYan Zheng 			if (path->locks[level + 1]) {
416833c66f43SYan Zheng 				level++;
4169e7a84565SChris Mason 				continue;
4170e7a84565SChris Mason 			}
417133c66f43SYan Zheng 
417233c66f43SYan Zheng 			slot = btrfs_header_nritems(c) - 1;
417333c66f43SYan Zheng 			if (level == 0)
417433c66f43SYan Zheng 				btrfs_item_key_to_cpu(c, &cur_key, slot);
417533c66f43SYan Zheng 			else
417633c66f43SYan Zheng 				btrfs_node_key_to_cpu(c, &cur_key, slot);
417733c66f43SYan Zheng 
417833c66f43SYan Zheng 			orig_lowest = path->lowest_level;
4179b3b4aa74SDavid Sterba 			btrfs_release_path(path);
418033c66f43SYan Zheng 			path->lowest_level = level;
418133c66f43SYan Zheng 			ret = btrfs_search_slot(NULL, root, &cur_key, path,
418233c66f43SYan Zheng 						0, 0);
418333c66f43SYan Zheng 			path->lowest_level = orig_lowest;
418433c66f43SYan Zheng 			if (ret < 0)
418533c66f43SYan Zheng 				return ret;
418633c66f43SYan Zheng 
418733c66f43SYan Zheng 			c = path->nodes[level];
418833c66f43SYan Zheng 			slot = path->slots[level];
418933c66f43SYan Zheng 			if (ret == 0)
419033c66f43SYan Zheng 				slot++;
419133c66f43SYan Zheng 			goto next;
419233c66f43SYan Zheng 		}
419333c66f43SYan Zheng 
4194e7a84565SChris Mason 		if (level == 0)
4195e7a84565SChris Mason 			btrfs_item_key_to_cpu(c, key, slot);
41963f157a2fSChris Mason 		else {
41973f157a2fSChris Mason 			u64 blockptr = btrfs_node_blockptr(c, slot);
41983f157a2fSChris Mason 			u64 gen = btrfs_node_ptr_generation(c, slot);
41993f157a2fSChris Mason 
42003f157a2fSChris Mason 			if (cache_only) {
42013f157a2fSChris Mason 				struct extent_buffer *cur;
42023f157a2fSChris Mason 				cur = btrfs_find_tree_block(root, blockptr,
42033f157a2fSChris Mason 					    btrfs_level_size(root, level - 1));
42043f157a2fSChris Mason 				if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
42053f157a2fSChris Mason 					slot++;
42063f157a2fSChris Mason 					if (cur)
42073f157a2fSChris Mason 						free_extent_buffer(cur);
42083f157a2fSChris Mason 					goto next;
42093f157a2fSChris Mason 				}
42103f157a2fSChris Mason 				free_extent_buffer(cur);
42113f157a2fSChris Mason 			}
42123f157a2fSChris Mason 			if (gen < min_trans) {
42133f157a2fSChris Mason 				slot++;
42143f157a2fSChris Mason 				goto next;
42153f157a2fSChris Mason 			}
4216e7a84565SChris Mason 			btrfs_node_key_to_cpu(c, key, slot);
42173f157a2fSChris Mason 		}
4218e7a84565SChris Mason 		return 0;
4219e7a84565SChris Mason 	}
4220e7a84565SChris Mason 	return 1;
4221e7a84565SChris Mason }
4222e7a84565SChris Mason 
42237bb86316SChris Mason /*
4224925baeddSChris Mason  * search the tree again to find a leaf with greater keys
42250f70abe2SChris Mason  * returns 0 if it found something or 1 if there are no greater leaves.
42260f70abe2SChris Mason  * returns < 0 on io errors.
422797571fd0SChris Mason  */
4228234b63a0SChris Mason int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
4229d97e63b6SChris Mason {
4230d97e63b6SChris Mason 	int slot;
42318e73f275SChris Mason 	int level;
42325f39d397SChris Mason 	struct extent_buffer *c;
42338e73f275SChris Mason 	struct extent_buffer *next;
4234925baeddSChris Mason 	struct btrfs_key key;
4235925baeddSChris Mason 	u32 nritems;
4236925baeddSChris Mason 	int ret;
42378e73f275SChris Mason 	int old_spinning = path->leave_spinning;
4238bd681513SChris Mason 	int next_rw_lock = 0;
4239925baeddSChris Mason 
4240925baeddSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
4241d397712bSChris Mason 	if (nritems == 0)
4242925baeddSChris Mason 		return 1;
4243925baeddSChris Mason 
42448e73f275SChris Mason 	btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
42458e73f275SChris Mason again:
42468e73f275SChris Mason 	level = 1;
42478e73f275SChris Mason 	next = NULL;
4248bd681513SChris Mason 	next_rw_lock = 0;
4249b3b4aa74SDavid Sterba 	btrfs_release_path(path);
42508e73f275SChris Mason 
4251a2135011SChris Mason 	path->keep_locks = 1;
42528e73f275SChris Mason 	path->leave_spinning = 1;
42538e73f275SChris Mason 
4254925baeddSChris Mason 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4255925baeddSChris Mason 	path->keep_locks = 0;
4256925baeddSChris Mason 
4257925baeddSChris Mason 	if (ret < 0)
4258925baeddSChris Mason 		return ret;
4259925baeddSChris Mason 
4260a2135011SChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
4261168fd7d2SChris Mason 	/*
4262168fd7d2SChris Mason 	 * by releasing the path above we dropped all our locks.  A balance
4263168fd7d2SChris Mason 	 * could have added more items next to the key that used to be
4264168fd7d2SChris Mason 	 * at the very end of the block.  So, check again here and
4265168fd7d2SChris Mason 	 * advance the path if there are now more items available.
4266168fd7d2SChris Mason 	 */
4267a2135011SChris Mason 	if (nritems > 0 && path->slots[0] < nritems - 1) {
4268e457afecSYan Zheng 		if (ret == 0)
4269168fd7d2SChris Mason 			path->slots[0]++;
42708e73f275SChris Mason 		ret = 0;
4271925baeddSChris Mason 		goto done;
4272925baeddSChris Mason 	}
4273d97e63b6SChris Mason 
4274234b63a0SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
42758e73f275SChris Mason 		if (!path->nodes[level]) {
42768e73f275SChris Mason 			ret = 1;
42778e73f275SChris Mason 			goto done;
42788e73f275SChris Mason 		}
42795f39d397SChris Mason 
4280d97e63b6SChris Mason 		slot = path->slots[level] + 1;
4281d97e63b6SChris Mason 		c = path->nodes[level];
42825f39d397SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
4283d97e63b6SChris Mason 			level++;
42848e73f275SChris Mason 			if (level == BTRFS_MAX_LEVEL) {
42858e73f275SChris Mason 				ret = 1;
42868e73f275SChris Mason 				goto done;
42878e73f275SChris Mason 			}
4288d97e63b6SChris Mason 			continue;
4289d97e63b6SChris Mason 		}
42905f39d397SChris Mason 
4291925baeddSChris Mason 		if (next) {
4292bd681513SChris Mason 			btrfs_tree_unlock_rw(next, next_rw_lock);
42935f39d397SChris Mason 			free_extent_buffer(next);
4294925baeddSChris Mason 		}
42955f39d397SChris Mason 
42968e73f275SChris Mason 		next = c;
4297bd681513SChris Mason 		next_rw_lock = path->locks[level];
42988e73f275SChris Mason 		ret = read_block_for_search(NULL, root, path, &next, level,
42998e73f275SChris Mason 					    slot, &key);
43008e73f275SChris Mason 		if (ret == -EAGAIN)
43018e73f275SChris Mason 			goto again;
43025f39d397SChris Mason 
430376a05b35SChris Mason 		if (ret < 0) {
4304b3b4aa74SDavid Sterba 			btrfs_release_path(path);
430576a05b35SChris Mason 			goto done;
430676a05b35SChris Mason 		}
430776a05b35SChris Mason 
43085cd57b2cSChris Mason 		if (!path->skip_locking) {
4309bd681513SChris Mason 			ret = btrfs_try_tree_read_lock(next);
43108e73f275SChris Mason 			if (!ret) {
43118e73f275SChris Mason 				btrfs_set_path_blocking(path);
4312bd681513SChris Mason 				btrfs_tree_read_lock(next);
4313bd681513SChris Mason 				btrfs_clear_path_blocking(path, next,
4314bd681513SChris Mason 							  BTRFS_READ_LOCK);
43158e73f275SChris Mason 			}
4316bd681513SChris Mason 			next_rw_lock = BTRFS_READ_LOCK;
4317bd681513SChris Mason 		}
4318d97e63b6SChris Mason 		break;
4319d97e63b6SChris Mason 	}
4320d97e63b6SChris Mason 	path->slots[level] = slot;
4321d97e63b6SChris Mason 	while (1) {
4322d97e63b6SChris Mason 		level--;
4323d97e63b6SChris Mason 		c = path->nodes[level];
4324925baeddSChris Mason 		if (path->locks[level])
4325bd681513SChris Mason 			btrfs_tree_unlock_rw(c, path->locks[level]);
43268e73f275SChris Mason 
43275f39d397SChris Mason 		free_extent_buffer(c);
4328d97e63b6SChris Mason 		path->nodes[level] = next;
4329d97e63b6SChris Mason 		path->slots[level] = 0;
4330a74a4b97SChris Mason 		if (!path->skip_locking)
4331bd681513SChris Mason 			path->locks[level] = next_rw_lock;
4332d97e63b6SChris Mason 		if (!level)
4333d97e63b6SChris Mason 			break;
4334b4ce94deSChris Mason 
43358e73f275SChris Mason 		ret = read_block_for_search(NULL, root, path, &next, level,
43368e73f275SChris Mason 					    0, &key);
43378e73f275SChris Mason 		if (ret == -EAGAIN)
43388e73f275SChris Mason 			goto again;
43398e73f275SChris Mason 
434076a05b35SChris Mason 		if (ret < 0) {
4341b3b4aa74SDavid Sterba 			btrfs_release_path(path);
434276a05b35SChris Mason 			goto done;
434376a05b35SChris Mason 		}
434476a05b35SChris Mason 
43455cd57b2cSChris Mason 		if (!path->skip_locking) {
4346bd681513SChris Mason 			ret = btrfs_try_tree_read_lock(next);
43478e73f275SChris Mason 			if (!ret) {
43488e73f275SChris Mason 				btrfs_set_path_blocking(path);
4349bd681513SChris Mason 				btrfs_tree_read_lock(next);
4350bd681513SChris Mason 				btrfs_clear_path_blocking(path, next,
4351bd681513SChris Mason 							  BTRFS_READ_LOCK);
43528e73f275SChris Mason 			}
4353bd681513SChris Mason 			next_rw_lock = BTRFS_READ_LOCK;
4354bd681513SChris Mason 		}
4355d97e63b6SChris Mason 	}
43568e73f275SChris Mason 	ret = 0;
4357925baeddSChris Mason done:
4358925baeddSChris Mason 	unlock_up(path, 0, 1);
43598e73f275SChris Mason 	path->leave_spinning = old_spinning;
43608e73f275SChris Mason 	if (!old_spinning)
43618e73f275SChris Mason 		btrfs_set_path_blocking(path);
43628e73f275SChris Mason 
43638e73f275SChris Mason 	return ret;
4364d97e63b6SChris Mason }
43650b86a832SChris Mason 
43663f157a2fSChris Mason /*
43673f157a2fSChris Mason  * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
43683f157a2fSChris Mason  * searching until it gets past min_objectid or finds an item of 'type'
43693f157a2fSChris Mason  *
43703f157a2fSChris Mason  * returns 0 if something is found, 1 if nothing was found and < 0 on error
43713f157a2fSChris Mason  */
43720b86a832SChris Mason int btrfs_previous_item(struct btrfs_root *root,
43730b86a832SChris Mason 			struct btrfs_path *path, u64 min_objectid,
43740b86a832SChris Mason 			int type)
43750b86a832SChris Mason {
43760b86a832SChris Mason 	struct btrfs_key found_key;
43770b86a832SChris Mason 	struct extent_buffer *leaf;
4378e02119d5SChris Mason 	u32 nritems;
43790b86a832SChris Mason 	int ret;
43800b86a832SChris Mason 
43810b86a832SChris Mason 	while (1) {
43820b86a832SChris Mason 		if (path->slots[0] == 0) {
4383b4ce94deSChris Mason 			btrfs_set_path_blocking(path);
43840b86a832SChris Mason 			ret = btrfs_prev_leaf(root, path);
43850b86a832SChris Mason 			if (ret != 0)
43860b86a832SChris Mason 				return ret;
43870b86a832SChris Mason 		} else {
43880b86a832SChris Mason 			path->slots[0]--;
43890b86a832SChris Mason 		}
43900b86a832SChris Mason 		leaf = path->nodes[0];
4391e02119d5SChris Mason 		nritems = btrfs_header_nritems(leaf);
4392e02119d5SChris Mason 		if (nritems == 0)
4393e02119d5SChris Mason 			return 1;
4394e02119d5SChris Mason 		if (path->slots[0] == nritems)
4395e02119d5SChris Mason 			path->slots[0]--;
4396e02119d5SChris Mason 
43970b86a832SChris Mason 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4398e02119d5SChris Mason 		if (found_key.objectid < min_objectid)
4399e02119d5SChris Mason 			break;
44000a4eefbbSYan Zheng 		if (found_key.type == type)
44010a4eefbbSYan Zheng 			return 0;
4402e02119d5SChris Mason 		if (found_key.objectid == min_objectid &&
4403e02119d5SChris Mason 		    found_key.type < type)
4404e02119d5SChris Mason 			break;
44050b86a832SChris Mason 	}
44060b86a832SChris Mason 	return 1;
44070b86a832SChris Mason }
4408