xref: /openbmc/linux/fs/btrfs/ctree.c (revision 79787eaa)
16cbd5570SChris Mason /*
2d352ac68SChris Mason  * Copyright (C) 2007,2008 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
19a6b6e75eSChris Mason #include <linux/sched.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
21eb60ceacSChris Mason #include "ctree.h"
22eb60ceacSChris Mason #include "disk-io.h"
237f5c1516SChris Mason #include "transaction.h"
245f39d397SChris Mason #include "print-tree.h"
25925baeddSChris Mason #include "locking.h"
269a8dd150SChris Mason 
27e089f05cSChris Mason static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
28e089f05cSChris Mason 		      *root, struct btrfs_path *path, int level);
29e089f05cSChris Mason static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
30d4dbff95SChris Mason 		      *root, struct btrfs_key *ins_key,
31cc0c5538SChris Mason 		      struct btrfs_path *path, int data_size, int extend);
325f39d397SChris Mason static int push_node_left(struct btrfs_trans_handle *trans,
335f39d397SChris Mason 			  struct btrfs_root *root, struct extent_buffer *dst,
34971a1f66SChris Mason 			  struct extent_buffer *src, int empty);
355f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans,
365f39d397SChris Mason 			      struct btrfs_root *root,
375f39d397SChris Mason 			      struct extent_buffer *dst_buf,
385f39d397SChris Mason 			      struct extent_buffer *src_buf);
39143bede5SJeff Mahoney static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
40e089f05cSChris Mason 		   struct btrfs_path *path, int level, int slot);
41d97e63b6SChris Mason 
422c90e5d6SChris Mason struct btrfs_path *btrfs_alloc_path(void)
432c90e5d6SChris Mason {
44df24a2b9SChris Mason 	struct btrfs_path *path;
45e00f7308SJeff Mahoney 	path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
46df24a2b9SChris Mason 	return path;
472c90e5d6SChris Mason }
482c90e5d6SChris Mason 
49b4ce94deSChris Mason /*
50b4ce94deSChris Mason  * set all locked nodes in the path to blocking locks.  This should
51b4ce94deSChris Mason  * be done before scheduling
52b4ce94deSChris Mason  */
53b4ce94deSChris Mason noinline void btrfs_set_path_blocking(struct btrfs_path *p)
54b4ce94deSChris Mason {
55b4ce94deSChris Mason 	int i;
56b4ce94deSChris Mason 	for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
57bd681513SChris Mason 		if (!p->nodes[i] || !p->locks[i])
58bd681513SChris Mason 			continue;
59bd681513SChris Mason 		btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
60bd681513SChris Mason 		if (p->locks[i] == BTRFS_READ_LOCK)
61bd681513SChris Mason 			p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
62bd681513SChris Mason 		else if (p->locks[i] == BTRFS_WRITE_LOCK)
63bd681513SChris Mason 			p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
64b4ce94deSChris Mason 	}
65b4ce94deSChris Mason }
66b4ce94deSChris Mason 
67b4ce94deSChris Mason /*
68b4ce94deSChris Mason  * reset all the locked nodes in the patch to spinning locks.
694008c04aSChris Mason  *
704008c04aSChris Mason  * held is used to keep lockdep happy, when lockdep is enabled
714008c04aSChris Mason  * we set held to a blocking lock before we go around and
724008c04aSChris Mason  * retake all the spinlocks in the path.  You can safely use NULL
734008c04aSChris Mason  * for held
74b4ce94deSChris Mason  */
754008c04aSChris Mason noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
76bd681513SChris Mason 					struct extent_buffer *held, int held_rw)
77b4ce94deSChris Mason {
78b4ce94deSChris Mason 	int i;
794008c04aSChris Mason 
804008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC
814008c04aSChris Mason 	/* lockdep really cares that we take all of these spinlocks
824008c04aSChris Mason 	 * in the right order.  If any of the locks in the path are not
834008c04aSChris Mason 	 * currently blocking, it is going to complain.  So, make really
844008c04aSChris Mason 	 * really sure by forcing the path to blocking before we clear
854008c04aSChris Mason 	 * the path blocking.
864008c04aSChris Mason 	 */
87bd681513SChris Mason 	if (held) {
88bd681513SChris Mason 		btrfs_set_lock_blocking_rw(held, held_rw);
89bd681513SChris Mason 		if (held_rw == BTRFS_WRITE_LOCK)
90bd681513SChris Mason 			held_rw = BTRFS_WRITE_LOCK_BLOCKING;
91bd681513SChris Mason 		else if (held_rw == BTRFS_READ_LOCK)
92bd681513SChris Mason 			held_rw = BTRFS_READ_LOCK_BLOCKING;
93bd681513SChris Mason 	}
944008c04aSChris Mason 	btrfs_set_path_blocking(p);
954008c04aSChris Mason #endif
964008c04aSChris Mason 
974008c04aSChris Mason 	for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
98bd681513SChris Mason 		if (p->nodes[i] && p->locks[i]) {
99bd681513SChris Mason 			btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
100bd681513SChris Mason 			if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
101bd681513SChris Mason 				p->locks[i] = BTRFS_WRITE_LOCK;
102bd681513SChris Mason 			else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
103bd681513SChris Mason 				p->locks[i] = BTRFS_READ_LOCK;
104bd681513SChris Mason 		}
105b4ce94deSChris Mason 	}
1064008c04aSChris Mason 
1074008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC
1084008c04aSChris Mason 	if (held)
109bd681513SChris Mason 		btrfs_clear_lock_blocking_rw(held, held_rw);
1104008c04aSChris Mason #endif
111b4ce94deSChris Mason }
112b4ce94deSChris Mason 
113d352ac68SChris Mason /* this also releases the path */
1142c90e5d6SChris Mason void btrfs_free_path(struct btrfs_path *p)
1152c90e5d6SChris Mason {
116ff175d57SJesper Juhl 	if (!p)
117ff175d57SJesper Juhl 		return;
118b3b4aa74SDavid Sterba 	btrfs_release_path(p);
1192c90e5d6SChris Mason 	kmem_cache_free(btrfs_path_cachep, p);
1202c90e5d6SChris Mason }
1212c90e5d6SChris Mason 
122d352ac68SChris Mason /*
123d352ac68SChris Mason  * path release drops references on the extent buffers in the path
124d352ac68SChris Mason  * and it drops any locks held by this path
125d352ac68SChris Mason  *
126d352ac68SChris Mason  * It is safe to call this on paths that no locks or extent buffers held.
127d352ac68SChris Mason  */
128b3b4aa74SDavid Sterba noinline void btrfs_release_path(struct btrfs_path *p)
129eb60ceacSChris Mason {
130eb60ceacSChris Mason 	int i;
131a2135011SChris Mason 
132234b63a0SChris Mason 	for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
1333f157a2fSChris Mason 		p->slots[i] = 0;
134eb60ceacSChris Mason 		if (!p->nodes[i])
135925baeddSChris Mason 			continue;
136925baeddSChris Mason 		if (p->locks[i]) {
137bd681513SChris Mason 			btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
138925baeddSChris Mason 			p->locks[i] = 0;
139925baeddSChris Mason 		}
1405f39d397SChris Mason 		free_extent_buffer(p->nodes[i]);
1413f157a2fSChris Mason 		p->nodes[i] = NULL;
142eb60ceacSChris Mason 	}
143eb60ceacSChris Mason }
144eb60ceacSChris Mason 
145d352ac68SChris Mason /*
146d352ac68SChris Mason  * safely gets a reference on the root node of a tree.  A lock
147d352ac68SChris Mason  * is not taken, so a concurrent writer may put a different node
148d352ac68SChris Mason  * at the root of the tree.  See btrfs_lock_root_node for the
149d352ac68SChris Mason  * looping required.
150d352ac68SChris Mason  *
151d352ac68SChris Mason  * The extent buffer returned by this has a reference taken, so
152d352ac68SChris Mason  * it won't disappear.  It may stop being the root of the tree
153d352ac68SChris Mason  * at any time because there are no locks held.
154d352ac68SChris Mason  */
155925baeddSChris Mason struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
156925baeddSChris Mason {
157925baeddSChris Mason 	struct extent_buffer *eb;
158240f62c8SChris Mason 
159240f62c8SChris Mason 	rcu_read_lock();
160240f62c8SChris Mason 	eb = rcu_dereference(root->node);
161925baeddSChris Mason 	extent_buffer_get(eb);
162240f62c8SChris Mason 	rcu_read_unlock();
163925baeddSChris Mason 	return eb;
164925baeddSChris Mason }
165925baeddSChris Mason 
166d352ac68SChris Mason /* loop around taking references on and locking the root node of the
167d352ac68SChris Mason  * tree until you end up with a lock on the root.  A locked buffer
168d352ac68SChris Mason  * is returned, with a reference held.
169d352ac68SChris Mason  */
170925baeddSChris Mason struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
171925baeddSChris Mason {
172925baeddSChris Mason 	struct extent_buffer *eb;
173925baeddSChris Mason 
174925baeddSChris Mason 	while (1) {
175925baeddSChris Mason 		eb = btrfs_root_node(root);
176925baeddSChris Mason 		btrfs_tree_lock(eb);
177240f62c8SChris Mason 		if (eb == root->node)
178925baeddSChris Mason 			break;
179925baeddSChris Mason 		btrfs_tree_unlock(eb);
180925baeddSChris Mason 		free_extent_buffer(eb);
181925baeddSChris Mason 	}
182925baeddSChris Mason 	return eb;
183925baeddSChris Mason }
184925baeddSChris Mason 
185bd681513SChris Mason /* loop around taking references on and locking the root node of the
186bd681513SChris Mason  * tree until you end up with a lock on the root.  A locked buffer
187bd681513SChris Mason  * is returned, with a reference held.
188bd681513SChris Mason  */
189bd681513SChris Mason struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
190bd681513SChris Mason {
191bd681513SChris Mason 	struct extent_buffer *eb;
192bd681513SChris Mason 
193bd681513SChris Mason 	while (1) {
194bd681513SChris Mason 		eb = btrfs_root_node(root);
195bd681513SChris Mason 		btrfs_tree_read_lock(eb);
196bd681513SChris Mason 		if (eb == root->node)
197bd681513SChris Mason 			break;
198bd681513SChris Mason 		btrfs_tree_read_unlock(eb);
199bd681513SChris Mason 		free_extent_buffer(eb);
200bd681513SChris Mason 	}
201bd681513SChris Mason 	return eb;
202bd681513SChris Mason }
203bd681513SChris Mason 
204d352ac68SChris Mason /* cowonly root (everything not a reference counted cow subvolume), just get
205d352ac68SChris Mason  * put onto a simple dirty list.  transaction.c walks this to make sure they
206d352ac68SChris Mason  * get properly updated on disk.
207d352ac68SChris Mason  */
2080b86a832SChris Mason static void add_root_to_dirty_list(struct btrfs_root *root)
2090b86a832SChris Mason {
2100b86a832SChris Mason 	if (root->track_dirty && list_empty(&root->dirty_list)) {
2110b86a832SChris Mason 		list_add(&root->dirty_list,
2120b86a832SChris Mason 			 &root->fs_info->dirty_cowonly_roots);
2130b86a832SChris Mason 	}
2140b86a832SChris Mason }
2150b86a832SChris Mason 
216d352ac68SChris Mason /*
217d352ac68SChris Mason  * used by snapshot creation to make a copy of a root for a tree with
218d352ac68SChris Mason  * a given objectid.  The buffer with the new root node is returned in
219d352ac68SChris Mason  * cow_ret, and this func returns zero on success or a negative error code.
220d352ac68SChris Mason  */
221be20aa9dSChris Mason int btrfs_copy_root(struct btrfs_trans_handle *trans,
222be20aa9dSChris Mason 		      struct btrfs_root *root,
223be20aa9dSChris Mason 		      struct extent_buffer *buf,
224be20aa9dSChris Mason 		      struct extent_buffer **cow_ret, u64 new_root_objectid)
225be20aa9dSChris Mason {
226be20aa9dSChris Mason 	struct extent_buffer *cow;
227be20aa9dSChris Mason 	int ret = 0;
228be20aa9dSChris Mason 	int level;
2295d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
230be20aa9dSChris Mason 
231be20aa9dSChris Mason 	WARN_ON(root->ref_cows && trans->transid !=
232be20aa9dSChris Mason 		root->fs_info->running_transaction->transid);
233be20aa9dSChris Mason 	WARN_ON(root->ref_cows && trans->transid != root->last_trans);
234be20aa9dSChris Mason 
235be20aa9dSChris Mason 	level = btrfs_header_level(buf);
2365d4f98a2SYan Zheng 	if (level == 0)
2375d4f98a2SYan Zheng 		btrfs_item_key(buf, &disk_key, 0);
2385d4f98a2SYan Zheng 	else
2395d4f98a2SYan Zheng 		btrfs_node_key(buf, &disk_key, 0);
24031840ae1SZheng Yan 
2415d4f98a2SYan Zheng 	cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
2425d4f98a2SYan Zheng 				     new_root_objectid, &disk_key, level,
24366d7e7f0SArne Jansen 				     buf->start, 0, 1);
2445d4f98a2SYan Zheng 	if (IS_ERR(cow))
245be20aa9dSChris Mason 		return PTR_ERR(cow);
246be20aa9dSChris Mason 
247be20aa9dSChris Mason 	copy_extent_buffer(cow, buf, 0, 0, cow->len);
248be20aa9dSChris Mason 	btrfs_set_header_bytenr(cow, cow->start);
249be20aa9dSChris Mason 	btrfs_set_header_generation(cow, trans->transid);
2505d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
2515d4f98a2SYan Zheng 	btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
2525d4f98a2SYan Zheng 				     BTRFS_HEADER_FLAG_RELOC);
2535d4f98a2SYan Zheng 	if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
2545d4f98a2SYan Zheng 		btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
2555d4f98a2SYan Zheng 	else
256be20aa9dSChris Mason 		btrfs_set_header_owner(cow, new_root_objectid);
257be20aa9dSChris Mason 
2582b82032cSYan Zheng 	write_extent_buffer(cow, root->fs_info->fsid,
2592b82032cSYan Zheng 			    (unsigned long)btrfs_header_fsid(cow),
2602b82032cSYan Zheng 			    BTRFS_FSID_SIZE);
2612b82032cSYan Zheng 
262be20aa9dSChris Mason 	WARN_ON(btrfs_header_generation(buf) > trans->transid);
2635d4f98a2SYan Zheng 	if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
26466d7e7f0SArne Jansen 		ret = btrfs_inc_ref(trans, root, cow, 1, 1);
2655d4f98a2SYan Zheng 	else
26666d7e7f0SArne Jansen 		ret = btrfs_inc_ref(trans, root, cow, 0, 1);
2674aec2b52SChris Mason 
268be20aa9dSChris Mason 	if (ret)
269be20aa9dSChris Mason 		return ret;
270be20aa9dSChris Mason 
271be20aa9dSChris Mason 	btrfs_mark_buffer_dirty(cow);
272be20aa9dSChris Mason 	*cow_ret = cow;
273be20aa9dSChris Mason 	return 0;
274be20aa9dSChris Mason }
275be20aa9dSChris Mason 
276d352ac68SChris Mason /*
2775d4f98a2SYan Zheng  * check if the tree block can be shared by multiple trees
2785d4f98a2SYan Zheng  */
2795d4f98a2SYan Zheng int btrfs_block_can_be_shared(struct btrfs_root *root,
2805d4f98a2SYan Zheng 			      struct extent_buffer *buf)
2815d4f98a2SYan Zheng {
2825d4f98a2SYan Zheng 	/*
2835d4f98a2SYan Zheng 	 * Tree blocks not in refernece counted trees and tree roots
2845d4f98a2SYan Zheng 	 * are never shared. If a block was allocated after the last
2855d4f98a2SYan Zheng 	 * snapshot and the block was not allocated by tree relocation,
2865d4f98a2SYan Zheng 	 * we know the block is not shared.
2875d4f98a2SYan Zheng 	 */
2885d4f98a2SYan Zheng 	if (root->ref_cows &&
2895d4f98a2SYan Zheng 	    buf != root->node && buf != root->commit_root &&
2905d4f98a2SYan Zheng 	    (btrfs_header_generation(buf) <=
2915d4f98a2SYan Zheng 	     btrfs_root_last_snapshot(&root->root_item) ||
2925d4f98a2SYan Zheng 	     btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
2935d4f98a2SYan Zheng 		return 1;
2945d4f98a2SYan Zheng #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2955d4f98a2SYan Zheng 	if (root->ref_cows &&
2965d4f98a2SYan Zheng 	    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
2975d4f98a2SYan Zheng 		return 1;
2985d4f98a2SYan Zheng #endif
2995d4f98a2SYan Zheng 	return 0;
3005d4f98a2SYan Zheng }
3015d4f98a2SYan Zheng 
3025d4f98a2SYan Zheng static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
3035d4f98a2SYan Zheng 				       struct btrfs_root *root,
3045d4f98a2SYan Zheng 				       struct extent_buffer *buf,
305f0486c68SYan, Zheng 				       struct extent_buffer *cow,
306f0486c68SYan, Zheng 				       int *last_ref)
3075d4f98a2SYan Zheng {
3085d4f98a2SYan Zheng 	u64 refs;
3095d4f98a2SYan Zheng 	u64 owner;
3105d4f98a2SYan Zheng 	u64 flags;
3115d4f98a2SYan Zheng 	u64 new_flags = 0;
3125d4f98a2SYan Zheng 	int ret;
3135d4f98a2SYan Zheng 
3145d4f98a2SYan Zheng 	/*
3155d4f98a2SYan Zheng 	 * Backrefs update rules:
3165d4f98a2SYan Zheng 	 *
3175d4f98a2SYan Zheng 	 * Always use full backrefs for extent pointers in tree block
3185d4f98a2SYan Zheng 	 * allocated by tree relocation.
3195d4f98a2SYan Zheng 	 *
3205d4f98a2SYan Zheng 	 * If a shared tree block is no longer referenced by its owner
3215d4f98a2SYan Zheng 	 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
3225d4f98a2SYan Zheng 	 * use full backrefs for extent pointers in tree block.
3235d4f98a2SYan Zheng 	 *
3245d4f98a2SYan Zheng 	 * If a tree block is been relocating
3255d4f98a2SYan Zheng 	 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
3265d4f98a2SYan Zheng 	 * use full backrefs for extent pointers in tree block.
3275d4f98a2SYan Zheng 	 * The reason for this is some operations (such as drop tree)
3285d4f98a2SYan Zheng 	 * are only allowed for blocks use full backrefs.
3295d4f98a2SYan Zheng 	 */
3305d4f98a2SYan Zheng 
3315d4f98a2SYan Zheng 	if (btrfs_block_can_be_shared(root, buf)) {
3325d4f98a2SYan Zheng 		ret = btrfs_lookup_extent_info(trans, root, buf->start,
3335d4f98a2SYan Zheng 					       buf->len, &refs, &flags);
334be1a5564SMark Fasheh 		if (ret)
335be1a5564SMark Fasheh 			return ret;
336e5df9573SMark Fasheh 		if (refs == 0) {
337e5df9573SMark Fasheh 			ret = -EROFS;
338e5df9573SMark Fasheh 			btrfs_std_error(root->fs_info, ret);
339e5df9573SMark Fasheh 			return ret;
340e5df9573SMark Fasheh 		}
3415d4f98a2SYan Zheng 	} else {
3425d4f98a2SYan Zheng 		refs = 1;
3435d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
3445d4f98a2SYan Zheng 		    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
3455d4f98a2SYan Zheng 			flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
3465d4f98a2SYan Zheng 		else
3475d4f98a2SYan Zheng 			flags = 0;
3485d4f98a2SYan Zheng 	}
3495d4f98a2SYan Zheng 
3505d4f98a2SYan Zheng 	owner = btrfs_header_owner(buf);
3515d4f98a2SYan Zheng 	BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
3525d4f98a2SYan Zheng 	       !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
3535d4f98a2SYan Zheng 
3545d4f98a2SYan Zheng 	if (refs > 1) {
3555d4f98a2SYan Zheng 		if ((owner == root->root_key.objectid ||
3565d4f98a2SYan Zheng 		     root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
3575d4f98a2SYan Zheng 		    !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
35866d7e7f0SArne Jansen 			ret = btrfs_inc_ref(trans, root, buf, 1, 1);
35979787eaaSJeff Mahoney 			BUG_ON(ret); /* -ENOMEM */
3605d4f98a2SYan Zheng 
3615d4f98a2SYan Zheng 			if (root->root_key.objectid ==
3625d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID) {
36366d7e7f0SArne Jansen 				ret = btrfs_dec_ref(trans, root, buf, 0, 1);
36479787eaaSJeff Mahoney 				BUG_ON(ret); /* -ENOMEM */
36566d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 1, 1);
36679787eaaSJeff Mahoney 				BUG_ON(ret); /* -ENOMEM */
3675d4f98a2SYan Zheng 			}
3685d4f98a2SYan Zheng 			new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
3695d4f98a2SYan Zheng 		} else {
3705d4f98a2SYan Zheng 
3715d4f98a2SYan Zheng 			if (root->root_key.objectid ==
3725d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID)
37366d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 1, 1);
3745d4f98a2SYan Zheng 			else
37566d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 0, 1);
37679787eaaSJeff Mahoney 			BUG_ON(ret); /* -ENOMEM */
3775d4f98a2SYan Zheng 		}
3785d4f98a2SYan Zheng 		if (new_flags != 0) {
3795d4f98a2SYan Zheng 			ret = btrfs_set_disk_extent_flags(trans, root,
3805d4f98a2SYan Zheng 							  buf->start,
3815d4f98a2SYan Zheng 							  buf->len,
3825d4f98a2SYan Zheng 							  new_flags, 0);
383be1a5564SMark Fasheh 			if (ret)
384be1a5564SMark Fasheh 				return ret;
3855d4f98a2SYan Zheng 		}
3865d4f98a2SYan Zheng 	} else {
3875d4f98a2SYan Zheng 		if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
3885d4f98a2SYan Zheng 			if (root->root_key.objectid ==
3895d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID)
39066d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 1, 1);
3915d4f98a2SYan Zheng 			else
39266d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 0, 1);
39379787eaaSJeff Mahoney 			BUG_ON(ret); /* -ENOMEM */
39466d7e7f0SArne Jansen 			ret = btrfs_dec_ref(trans, root, buf, 1, 1);
39579787eaaSJeff Mahoney 			BUG_ON(ret); /* -ENOMEM */
3965d4f98a2SYan Zheng 		}
3975d4f98a2SYan Zheng 		clean_tree_block(trans, root, buf);
398f0486c68SYan, Zheng 		*last_ref = 1;
3995d4f98a2SYan Zheng 	}
4005d4f98a2SYan Zheng 	return 0;
4015d4f98a2SYan Zheng }
4025d4f98a2SYan Zheng 
4035d4f98a2SYan Zheng /*
404d397712bSChris Mason  * does the dirty work in cow of a single block.  The parent block (if
405d397712bSChris Mason  * supplied) is updated to point to the new cow copy.  The new buffer is marked
406d397712bSChris Mason  * dirty and returned locked.  If you modify the block it needs to be marked
407d397712bSChris Mason  * dirty again.
408d352ac68SChris Mason  *
409d352ac68SChris Mason  * search_start -- an allocation hint for the new block
410d352ac68SChris Mason  *
411d397712bSChris Mason  * empty_size -- a hint that you plan on doing more cow.  This is the size in
412d397712bSChris Mason  * bytes the allocator should try to find free next to the block it returns.
413d397712bSChris Mason  * This is just a hint and may be ignored by the allocator.
414d352ac68SChris Mason  */
415d397712bSChris Mason static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
4165f39d397SChris Mason 			     struct btrfs_root *root,
4175f39d397SChris Mason 			     struct extent_buffer *buf,
4185f39d397SChris Mason 			     struct extent_buffer *parent, int parent_slot,
4195f39d397SChris Mason 			     struct extent_buffer **cow_ret,
4209fa8cfe7SChris Mason 			     u64 search_start, u64 empty_size)
4216702ed49SChris Mason {
4225d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
4235f39d397SChris Mason 	struct extent_buffer *cow;
424be1a5564SMark Fasheh 	int level, ret;
425f0486c68SYan, Zheng 	int last_ref = 0;
426925baeddSChris Mason 	int unlock_orig = 0;
4275d4f98a2SYan Zheng 	u64 parent_start;
4286702ed49SChris Mason 
429925baeddSChris Mason 	if (*cow_ret == buf)
430925baeddSChris Mason 		unlock_orig = 1;
431925baeddSChris Mason 
432b9447ef8SChris Mason 	btrfs_assert_tree_locked(buf);
433925baeddSChris Mason 
4347bb86316SChris Mason 	WARN_ON(root->ref_cows && trans->transid !=
4357bb86316SChris Mason 		root->fs_info->running_transaction->transid);
4366702ed49SChris Mason 	WARN_ON(root->ref_cows && trans->transid != root->last_trans);
4375f39d397SChris Mason 
4387bb86316SChris Mason 	level = btrfs_header_level(buf);
43931840ae1SZheng Yan 
4405d4f98a2SYan Zheng 	if (level == 0)
4415d4f98a2SYan Zheng 		btrfs_item_key(buf, &disk_key, 0);
4425d4f98a2SYan Zheng 	else
4435d4f98a2SYan Zheng 		btrfs_node_key(buf, &disk_key, 0);
4445d4f98a2SYan Zheng 
4455d4f98a2SYan Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
4465d4f98a2SYan Zheng 		if (parent)
4475d4f98a2SYan Zheng 			parent_start = parent->start;
4485d4f98a2SYan Zheng 		else
4495d4f98a2SYan Zheng 			parent_start = 0;
4505d4f98a2SYan Zheng 	} else
4515d4f98a2SYan Zheng 		parent_start = 0;
4525d4f98a2SYan Zheng 
4535d4f98a2SYan Zheng 	cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
4545d4f98a2SYan Zheng 				     root->root_key.objectid, &disk_key,
45566d7e7f0SArne Jansen 				     level, search_start, empty_size, 1);
4566702ed49SChris Mason 	if (IS_ERR(cow))
4576702ed49SChris Mason 		return PTR_ERR(cow);
4586702ed49SChris Mason 
459b4ce94deSChris Mason 	/* cow is set to blocking by btrfs_init_new_buffer */
460b4ce94deSChris Mason 
4615f39d397SChris Mason 	copy_extent_buffer(cow, buf, 0, 0, cow->len);
462db94535dSChris Mason 	btrfs_set_header_bytenr(cow, cow->start);
4635f39d397SChris Mason 	btrfs_set_header_generation(cow, trans->transid);
4645d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
4655d4f98a2SYan Zheng 	btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
4665d4f98a2SYan Zheng 				     BTRFS_HEADER_FLAG_RELOC);
4675d4f98a2SYan Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4685d4f98a2SYan Zheng 		btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
4695d4f98a2SYan Zheng 	else
4705f39d397SChris Mason 		btrfs_set_header_owner(cow, root->root_key.objectid);
4716702ed49SChris Mason 
4722b82032cSYan Zheng 	write_extent_buffer(cow, root->fs_info->fsid,
4732b82032cSYan Zheng 			    (unsigned long)btrfs_header_fsid(cow),
4742b82032cSYan Zheng 			    BTRFS_FSID_SIZE);
4752b82032cSYan Zheng 
476be1a5564SMark Fasheh 	ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
477b68dc2a9SMark Fasheh 	if (ret) {
47879787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
479b68dc2a9SMark Fasheh 		return ret;
480b68dc2a9SMark Fasheh 	}
4811a40e23bSZheng Yan 
4823fd0a558SYan, Zheng 	if (root->ref_cows)
4833fd0a558SYan, Zheng 		btrfs_reloc_cow_block(trans, root, buf, cow);
4843fd0a558SYan, Zheng 
4856702ed49SChris Mason 	if (buf == root->node) {
486925baeddSChris Mason 		WARN_ON(parent && parent != buf);
4875d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
4885d4f98a2SYan Zheng 		    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
4895d4f98a2SYan Zheng 			parent_start = buf->start;
4905d4f98a2SYan Zheng 		else
4915d4f98a2SYan Zheng 			parent_start = 0;
492925baeddSChris Mason 
4935f39d397SChris Mason 		extent_buffer_get(cow);
494240f62c8SChris Mason 		rcu_assign_pointer(root->node, cow);
495925baeddSChris Mason 
496f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, buf, parent_start,
49766d7e7f0SArne Jansen 				      last_ref, 1);
4985f39d397SChris Mason 		free_extent_buffer(buf);
4990b86a832SChris Mason 		add_root_to_dirty_list(root);
5006702ed49SChris Mason 	} else {
5015d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
5025d4f98a2SYan Zheng 			parent_start = parent->start;
5035d4f98a2SYan Zheng 		else
5045d4f98a2SYan Zheng 			parent_start = 0;
5055d4f98a2SYan Zheng 
5065d4f98a2SYan Zheng 		WARN_ON(trans->transid != btrfs_header_generation(parent));
5075f39d397SChris Mason 		btrfs_set_node_blockptr(parent, parent_slot,
508db94535dSChris Mason 					cow->start);
50974493f7aSChris Mason 		btrfs_set_node_ptr_generation(parent, parent_slot,
51074493f7aSChris Mason 					      trans->transid);
5116702ed49SChris Mason 		btrfs_mark_buffer_dirty(parent);
512f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, buf, parent_start,
51366d7e7f0SArne Jansen 				      last_ref, 1);
5146702ed49SChris Mason 	}
515925baeddSChris Mason 	if (unlock_orig)
516925baeddSChris Mason 		btrfs_tree_unlock(buf);
5175f39d397SChris Mason 	free_extent_buffer(buf);
5186702ed49SChris Mason 	btrfs_mark_buffer_dirty(cow);
5196702ed49SChris Mason 	*cow_ret = cow;
5206702ed49SChris Mason 	return 0;
5216702ed49SChris Mason }
5226702ed49SChris Mason 
5235d4f98a2SYan Zheng static inline int should_cow_block(struct btrfs_trans_handle *trans,
5245d4f98a2SYan Zheng 				   struct btrfs_root *root,
5255d4f98a2SYan Zheng 				   struct extent_buffer *buf)
5265d4f98a2SYan Zheng {
527f1ebcc74SLiu Bo 	/* ensure we can see the force_cow */
528f1ebcc74SLiu Bo 	smp_rmb();
529f1ebcc74SLiu Bo 
530f1ebcc74SLiu Bo 	/*
531f1ebcc74SLiu Bo 	 * We do not need to cow a block if
532f1ebcc74SLiu Bo 	 * 1) this block is not created or changed in this transaction;
533f1ebcc74SLiu Bo 	 * 2) this block does not belong to TREE_RELOC tree;
534f1ebcc74SLiu Bo 	 * 3) the root is not forced COW.
535f1ebcc74SLiu Bo 	 *
536f1ebcc74SLiu Bo 	 * What is forced COW:
537f1ebcc74SLiu Bo 	 *    when we create snapshot during commiting the transaction,
538f1ebcc74SLiu Bo 	 *    after we've finished coping src root, we must COW the shared
539f1ebcc74SLiu Bo 	 *    block to ensure the metadata consistency.
540f1ebcc74SLiu Bo 	 */
5415d4f98a2SYan Zheng 	if (btrfs_header_generation(buf) == trans->transid &&
5425d4f98a2SYan Zheng 	    !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
5435d4f98a2SYan Zheng 	    !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
544f1ebcc74SLiu Bo 	      btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
545f1ebcc74SLiu Bo 	    !root->force_cow)
5465d4f98a2SYan Zheng 		return 0;
5475d4f98a2SYan Zheng 	return 1;
5485d4f98a2SYan Zheng }
5495d4f98a2SYan Zheng 
550d352ac68SChris Mason /*
551d352ac68SChris Mason  * cows a single block, see __btrfs_cow_block for the real work.
552d352ac68SChris Mason  * This version of it has extra checks so that a block isn't cow'd more than
553d352ac68SChris Mason  * once per transaction, as long as it hasn't been written yet
554d352ac68SChris Mason  */
555d397712bSChris Mason noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
5565f39d397SChris Mason 		    struct btrfs_root *root, struct extent_buffer *buf,
5575f39d397SChris Mason 		    struct extent_buffer *parent, int parent_slot,
5589fa8cfe7SChris Mason 		    struct extent_buffer **cow_ret)
55902217ed2SChris Mason {
5606702ed49SChris Mason 	u64 search_start;
561f510cfecSChris Mason 	int ret;
562dc17ff8fSChris Mason 
563ccd467d6SChris Mason 	if (trans->transaction != root->fs_info->running_transaction) {
564d397712bSChris Mason 		printk(KERN_CRIT "trans %llu running %llu\n",
565d397712bSChris Mason 		       (unsigned long long)trans->transid,
566d397712bSChris Mason 		       (unsigned long long)
567ccd467d6SChris Mason 		       root->fs_info->running_transaction->transid);
568ccd467d6SChris Mason 		WARN_ON(1);
569ccd467d6SChris Mason 	}
570ccd467d6SChris Mason 	if (trans->transid != root->fs_info->generation) {
571d397712bSChris Mason 		printk(KERN_CRIT "trans %llu running %llu\n",
572d397712bSChris Mason 		       (unsigned long long)trans->transid,
573d397712bSChris Mason 		       (unsigned long long)root->fs_info->generation);
574ccd467d6SChris Mason 		WARN_ON(1);
575ccd467d6SChris Mason 	}
576dc17ff8fSChris Mason 
5775d4f98a2SYan Zheng 	if (!should_cow_block(trans, root, buf)) {
57802217ed2SChris Mason 		*cow_ret = buf;
57902217ed2SChris Mason 		return 0;
58002217ed2SChris Mason 	}
581c487685dSChris Mason 
5820b86a832SChris Mason 	search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
583b4ce94deSChris Mason 
584b4ce94deSChris Mason 	if (parent)
585b4ce94deSChris Mason 		btrfs_set_lock_blocking(parent);
586b4ce94deSChris Mason 	btrfs_set_lock_blocking(buf);
587b4ce94deSChris Mason 
588f510cfecSChris Mason 	ret = __btrfs_cow_block(trans, root, buf, parent,
5899fa8cfe7SChris Mason 				 parent_slot, cow_ret, search_start, 0);
5901abe9b8aSliubo 
5911abe9b8aSliubo 	trace_btrfs_cow_block(root, buf, *cow_ret);
5921abe9b8aSliubo 
593f510cfecSChris Mason 	return ret;
5942c90e5d6SChris Mason }
5956702ed49SChris Mason 
596d352ac68SChris Mason /*
597d352ac68SChris Mason  * helper function for defrag to decide if two blocks pointed to by a
598d352ac68SChris Mason  * node are actually close by
599d352ac68SChris Mason  */
6006b80053dSChris Mason static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
6016702ed49SChris Mason {
6026b80053dSChris Mason 	if (blocknr < other && other - (blocknr + blocksize) < 32768)
6036702ed49SChris Mason 		return 1;
6046b80053dSChris Mason 	if (blocknr > other && blocknr - (other + blocksize) < 32768)
6056702ed49SChris Mason 		return 1;
60602217ed2SChris Mason 	return 0;
60702217ed2SChris Mason }
60802217ed2SChris Mason 
609081e9573SChris Mason /*
610081e9573SChris Mason  * compare two keys in a memcmp fashion
611081e9573SChris Mason  */
612081e9573SChris Mason static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
613081e9573SChris Mason {
614081e9573SChris Mason 	struct btrfs_key k1;
615081e9573SChris Mason 
616081e9573SChris Mason 	btrfs_disk_key_to_cpu(&k1, disk);
617081e9573SChris Mason 
61820736abaSDiego Calleja 	return btrfs_comp_cpu_keys(&k1, k2);
619081e9573SChris Mason }
620081e9573SChris Mason 
621f3465ca4SJosef Bacik /*
622f3465ca4SJosef Bacik  * same as comp_keys only with two btrfs_key's
623f3465ca4SJosef Bacik  */
6245d4f98a2SYan Zheng int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
625f3465ca4SJosef Bacik {
626f3465ca4SJosef Bacik 	if (k1->objectid > k2->objectid)
627f3465ca4SJosef Bacik 		return 1;
628f3465ca4SJosef Bacik 	if (k1->objectid < k2->objectid)
629f3465ca4SJosef Bacik 		return -1;
630f3465ca4SJosef Bacik 	if (k1->type > k2->type)
631f3465ca4SJosef Bacik 		return 1;
632f3465ca4SJosef Bacik 	if (k1->type < k2->type)
633f3465ca4SJosef Bacik 		return -1;
634f3465ca4SJosef Bacik 	if (k1->offset > k2->offset)
635f3465ca4SJosef Bacik 		return 1;
636f3465ca4SJosef Bacik 	if (k1->offset < k2->offset)
637f3465ca4SJosef Bacik 		return -1;
638f3465ca4SJosef Bacik 	return 0;
639f3465ca4SJosef Bacik }
640081e9573SChris Mason 
641d352ac68SChris Mason /*
642d352ac68SChris Mason  * this is used by the defrag code to go through all the
643d352ac68SChris Mason  * leaves pointed to by a node and reallocate them so that
644d352ac68SChris Mason  * disk order is close to key order
645d352ac68SChris Mason  */
6466702ed49SChris Mason int btrfs_realloc_node(struct btrfs_trans_handle *trans,
6475f39d397SChris Mason 		       struct btrfs_root *root, struct extent_buffer *parent,
648a6b6e75eSChris Mason 		       int start_slot, int cache_only, u64 *last_ret,
649a6b6e75eSChris Mason 		       struct btrfs_key *progress)
6506702ed49SChris Mason {
6516b80053dSChris Mason 	struct extent_buffer *cur;
6526702ed49SChris Mason 	u64 blocknr;
653ca7a79adSChris Mason 	u64 gen;
654e9d0b13bSChris Mason 	u64 search_start = *last_ret;
655e9d0b13bSChris Mason 	u64 last_block = 0;
6566702ed49SChris Mason 	u64 other;
6576702ed49SChris Mason 	u32 parent_nritems;
6586702ed49SChris Mason 	int end_slot;
6596702ed49SChris Mason 	int i;
6606702ed49SChris Mason 	int err = 0;
661f2183bdeSChris Mason 	int parent_level;
6626b80053dSChris Mason 	int uptodate;
6636b80053dSChris Mason 	u32 blocksize;
664081e9573SChris Mason 	int progress_passed = 0;
665081e9573SChris Mason 	struct btrfs_disk_key disk_key;
6666702ed49SChris Mason 
6675708b959SChris Mason 	parent_level = btrfs_header_level(parent);
6685708b959SChris Mason 	if (cache_only && parent_level != 1)
6695708b959SChris Mason 		return 0;
6705708b959SChris Mason 
671d397712bSChris Mason 	if (trans->transaction != root->fs_info->running_transaction)
6726702ed49SChris Mason 		WARN_ON(1);
673d397712bSChris Mason 	if (trans->transid != root->fs_info->generation)
6746702ed49SChris Mason 		WARN_ON(1);
67586479a04SChris Mason 
6766b80053dSChris Mason 	parent_nritems = btrfs_header_nritems(parent);
6776b80053dSChris Mason 	blocksize = btrfs_level_size(root, parent_level - 1);
6786702ed49SChris Mason 	end_slot = parent_nritems;
6796702ed49SChris Mason 
6806702ed49SChris Mason 	if (parent_nritems == 1)
6816702ed49SChris Mason 		return 0;
6826702ed49SChris Mason 
683b4ce94deSChris Mason 	btrfs_set_lock_blocking(parent);
684b4ce94deSChris Mason 
6856702ed49SChris Mason 	for (i = start_slot; i < end_slot; i++) {
6866702ed49SChris Mason 		int close = 1;
687a6b6e75eSChris Mason 
688081e9573SChris Mason 		btrfs_node_key(parent, &disk_key, i);
689081e9573SChris Mason 		if (!progress_passed && comp_keys(&disk_key, progress) < 0)
690081e9573SChris Mason 			continue;
691081e9573SChris Mason 
692081e9573SChris Mason 		progress_passed = 1;
6936b80053dSChris Mason 		blocknr = btrfs_node_blockptr(parent, i);
694ca7a79adSChris Mason 		gen = btrfs_node_ptr_generation(parent, i);
695e9d0b13bSChris Mason 		if (last_block == 0)
696e9d0b13bSChris Mason 			last_block = blocknr;
6975708b959SChris Mason 
6986702ed49SChris Mason 		if (i > 0) {
6996b80053dSChris Mason 			other = btrfs_node_blockptr(parent, i - 1);
7006b80053dSChris Mason 			close = close_blocks(blocknr, other, blocksize);
7016702ed49SChris Mason 		}
7020ef3e66bSChris Mason 		if (!close && i < end_slot - 2) {
7036b80053dSChris Mason 			other = btrfs_node_blockptr(parent, i + 1);
7046b80053dSChris Mason 			close = close_blocks(blocknr, other, blocksize);
7056702ed49SChris Mason 		}
706e9d0b13bSChris Mason 		if (close) {
707e9d0b13bSChris Mason 			last_block = blocknr;
7086702ed49SChris Mason 			continue;
709e9d0b13bSChris Mason 		}
7106702ed49SChris Mason 
7116b80053dSChris Mason 		cur = btrfs_find_tree_block(root, blocknr, blocksize);
7126b80053dSChris Mason 		if (cur)
7131259ab75SChris Mason 			uptodate = btrfs_buffer_uptodate(cur, gen);
7146b80053dSChris Mason 		else
7156b80053dSChris Mason 			uptodate = 0;
7165708b959SChris Mason 		if (!cur || !uptodate) {
7176702ed49SChris Mason 			if (cache_only) {
7186b80053dSChris Mason 				free_extent_buffer(cur);
7196702ed49SChris Mason 				continue;
7206702ed49SChris Mason 			}
7216b80053dSChris Mason 			if (!cur) {
7226b80053dSChris Mason 				cur = read_tree_block(root, blocknr,
723ca7a79adSChris Mason 							 blocksize, gen);
72497d9a8a4STsutomu Itoh 				if (!cur)
72597d9a8a4STsutomu Itoh 					return -EIO;
7266b80053dSChris Mason 			} else if (!uptodate) {
727ca7a79adSChris Mason 				btrfs_read_buffer(cur, gen);
7286702ed49SChris Mason 			}
729f2183bdeSChris Mason 		}
730e9d0b13bSChris Mason 		if (search_start == 0)
7316b80053dSChris Mason 			search_start = last_block;
732e9d0b13bSChris Mason 
733e7a84565SChris Mason 		btrfs_tree_lock(cur);
734b4ce94deSChris Mason 		btrfs_set_lock_blocking(cur);
7356b80053dSChris Mason 		err = __btrfs_cow_block(trans, root, cur, parent, i,
736e7a84565SChris Mason 					&cur, search_start,
7376b80053dSChris Mason 					min(16 * blocksize,
7389fa8cfe7SChris Mason 					    (end_slot - i) * blocksize));
739252c38f0SYan 		if (err) {
740e7a84565SChris Mason 			btrfs_tree_unlock(cur);
7416b80053dSChris Mason 			free_extent_buffer(cur);
7426702ed49SChris Mason 			break;
743252c38f0SYan 		}
744e7a84565SChris Mason 		search_start = cur->start;
745e7a84565SChris Mason 		last_block = cur->start;
746f2183bdeSChris Mason 		*last_ret = search_start;
747e7a84565SChris Mason 		btrfs_tree_unlock(cur);
748e7a84565SChris Mason 		free_extent_buffer(cur);
7496702ed49SChris Mason 	}
7506702ed49SChris Mason 	return err;
7516702ed49SChris Mason }
7526702ed49SChris Mason 
75374123bd7SChris Mason /*
75474123bd7SChris Mason  * The leaf data grows from end-to-front in the node.
75574123bd7SChris Mason  * this returns the address of the start of the last item,
75674123bd7SChris Mason  * which is the stop of the leaf data stack
75774123bd7SChris Mason  */
758123abc88SChris Mason static inline unsigned int leaf_data_end(struct btrfs_root *root,
7595f39d397SChris Mason 					 struct extent_buffer *leaf)
760be0e5c09SChris Mason {
7615f39d397SChris Mason 	u32 nr = btrfs_header_nritems(leaf);
762be0e5c09SChris Mason 	if (nr == 0)
763123abc88SChris Mason 		return BTRFS_LEAF_DATA_SIZE(root);
7645f39d397SChris Mason 	return btrfs_item_offset_nr(leaf, nr - 1);
765be0e5c09SChris Mason }
766be0e5c09SChris Mason 
767aa5d6bedSChris Mason 
76874123bd7SChris Mason /*
7695f39d397SChris Mason  * search for key in the extent_buffer.  The items start at offset p,
7705f39d397SChris Mason  * and they are item_size apart.  There are 'max' items in p.
7715f39d397SChris Mason  *
77274123bd7SChris Mason  * the slot in the array is returned via slot, and it points to
77374123bd7SChris Mason  * the place where you would insert key if it is not found in
77474123bd7SChris Mason  * the array.
77574123bd7SChris Mason  *
77674123bd7SChris Mason  * slot may point to max if the key is bigger than all of the keys
77774123bd7SChris Mason  */
778e02119d5SChris Mason static noinline int generic_bin_search(struct extent_buffer *eb,
779e02119d5SChris Mason 				       unsigned long p,
7805f39d397SChris Mason 				       int item_size, struct btrfs_key *key,
781be0e5c09SChris Mason 				       int max, int *slot)
782be0e5c09SChris Mason {
783be0e5c09SChris Mason 	int low = 0;
784be0e5c09SChris Mason 	int high = max;
785be0e5c09SChris Mason 	int mid;
786be0e5c09SChris Mason 	int ret;
787479965d6SChris Mason 	struct btrfs_disk_key *tmp = NULL;
7885f39d397SChris Mason 	struct btrfs_disk_key unaligned;
7895f39d397SChris Mason 	unsigned long offset;
7905f39d397SChris Mason 	char *kaddr = NULL;
7915f39d397SChris Mason 	unsigned long map_start = 0;
7925f39d397SChris Mason 	unsigned long map_len = 0;
793479965d6SChris Mason 	int err;
794be0e5c09SChris Mason 
795be0e5c09SChris Mason 	while (low < high) {
796be0e5c09SChris Mason 		mid = (low + high) / 2;
7975f39d397SChris Mason 		offset = p + mid * item_size;
7985f39d397SChris Mason 
799a6591715SChris Mason 		if (!kaddr || offset < map_start ||
8005f39d397SChris Mason 		    (offset + sizeof(struct btrfs_disk_key)) >
8015f39d397SChris Mason 		    map_start + map_len) {
802934d375bSChris Mason 
803934d375bSChris Mason 			err = map_private_extent_buffer(eb, offset,
804479965d6SChris Mason 						sizeof(struct btrfs_disk_key),
805a6591715SChris Mason 						&kaddr, &map_start, &map_len);
8065f39d397SChris Mason 
807479965d6SChris Mason 			if (!err) {
808479965d6SChris Mason 				tmp = (struct btrfs_disk_key *)(kaddr + offset -
809479965d6SChris Mason 							map_start);
810479965d6SChris Mason 			} else {
8115f39d397SChris Mason 				read_extent_buffer(eb, &unaligned,
8125f39d397SChris Mason 						   offset, sizeof(unaligned));
8135f39d397SChris Mason 				tmp = &unaligned;
814479965d6SChris Mason 			}
815479965d6SChris Mason 
8165f39d397SChris Mason 		} else {
8175f39d397SChris Mason 			tmp = (struct btrfs_disk_key *)(kaddr + offset -
8185f39d397SChris Mason 							map_start);
8195f39d397SChris Mason 		}
820be0e5c09SChris Mason 		ret = comp_keys(tmp, key);
821be0e5c09SChris Mason 
822be0e5c09SChris Mason 		if (ret < 0)
823be0e5c09SChris Mason 			low = mid + 1;
824be0e5c09SChris Mason 		else if (ret > 0)
825be0e5c09SChris Mason 			high = mid;
826be0e5c09SChris Mason 		else {
827be0e5c09SChris Mason 			*slot = mid;
828be0e5c09SChris Mason 			return 0;
829be0e5c09SChris Mason 		}
830be0e5c09SChris Mason 	}
831be0e5c09SChris Mason 	*slot = low;
832be0e5c09SChris Mason 	return 1;
833be0e5c09SChris Mason }
834be0e5c09SChris Mason 
83597571fd0SChris Mason /*
83697571fd0SChris Mason  * simple bin_search frontend that does the right thing for
83797571fd0SChris Mason  * leaves vs nodes
83897571fd0SChris Mason  */
8395f39d397SChris Mason static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
8405f39d397SChris Mason 		      int level, int *slot)
841be0e5c09SChris Mason {
8425f39d397SChris Mason 	if (level == 0) {
8435f39d397SChris Mason 		return generic_bin_search(eb,
8445f39d397SChris Mason 					  offsetof(struct btrfs_leaf, items),
8450783fcfcSChris Mason 					  sizeof(struct btrfs_item),
8465f39d397SChris Mason 					  key, btrfs_header_nritems(eb),
8477518a238SChris Mason 					  slot);
848be0e5c09SChris Mason 	} else {
8495f39d397SChris Mason 		return generic_bin_search(eb,
8505f39d397SChris Mason 					  offsetof(struct btrfs_node, ptrs),
851123abc88SChris Mason 					  sizeof(struct btrfs_key_ptr),
8525f39d397SChris Mason 					  key, btrfs_header_nritems(eb),
8537518a238SChris Mason 					  slot);
854be0e5c09SChris Mason 	}
855be0e5c09SChris Mason 	return -1;
856be0e5c09SChris Mason }
857be0e5c09SChris Mason 
8585d4f98a2SYan Zheng int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
8595d4f98a2SYan Zheng 		     int level, int *slot)
8605d4f98a2SYan Zheng {
8615d4f98a2SYan Zheng 	return bin_search(eb, key, level, slot);
8625d4f98a2SYan Zheng }
8635d4f98a2SYan Zheng 
864f0486c68SYan, Zheng static void root_add_used(struct btrfs_root *root, u32 size)
865f0486c68SYan, Zheng {
866f0486c68SYan, Zheng 	spin_lock(&root->accounting_lock);
867f0486c68SYan, Zheng 	btrfs_set_root_used(&root->root_item,
868f0486c68SYan, Zheng 			    btrfs_root_used(&root->root_item) + size);
869f0486c68SYan, Zheng 	spin_unlock(&root->accounting_lock);
870f0486c68SYan, Zheng }
871f0486c68SYan, Zheng 
872f0486c68SYan, Zheng static void root_sub_used(struct btrfs_root *root, u32 size)
873f0486c68SYan, Zheng {
874f0486c68SYan, Zheng 	spin_lock(&root->accounting_lock);
875f0486c68SYan, Zheng 	btrfs_set_root_used(&root->root_item,
876f0486c68SYan, Zheng 			    btrfs_root_used(&root->root_item) - size);
877f0486c68SYan, Zheng 	spin_unlock(&root->accounting_lock);
878f0486c68SYan, Zheng }
879f0486c68SYan, Zheng 
880d352ac68SChris Mason /* given a node and slot number, this reads the blocks it points to.  The
881d352ac68SChris Mason  * extent buffer is returned with a reference taken (but unlocked).
882d352ac68SChris Mason  * NULL is returned on error.
883d352ac68SChris Mason  */
884e02119d5SChris Mason static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
8855f39d397SChris Mason 				   struct extent_buffer *parent, int slot)
886bb803951SChris Mason {
887ca7a79adSChris Mason 	int level = btrfs_header_level(parent);
888bb803951SChris Mason 	if (slot < 0)
889bb803951SChris Mason 		return NULL;
8905f39d397SChris Mason 	if (slot >= btrfs_header_nritems(parent))
891bb803951SChris Mason 		return NULL;
892ca7a79adSChris Mason 
893ca7a79adSChris Mason 	BUG_ON(level == 0);
894ca7a79adSChris Mason 
895db94535dSChris Mason 	return read_tree_block(root, btrfs_node_blockptr(parent, slot),
896ca7a79adSChris Mason 		       btrfs_level_size(root, level - 1),
897ca7a79adSChris Mason 		       btrfs_node_ptr_generation(parent, slot));
898bb803951SChris Mason }
899bb803951SChris Mason 
900d352ac68SChris Mason /*
901d352ac68SChris Mason  * node level balancing, used to make sure nodes are in proper order for
902d352ac68SChris Mason  * item deletion.  We balance from the top down, so we have to make sure
903d352ac68SChris Mason  * that a deletion won't leave an node completely empty later on.
904d352ac68SChris Mason  */
905e02119d5SChris Mason static noinline int balance_level(struct btrfs_trans_handle *trans,
90698ed5174SChris Mason 			 struct btrfs_root *root,
90798ed5174SChris Mason 			 struct btrfs_path *path, int level)
908bb803951SChris Mason {
9095f39d397SChris Mason 	struct extent_buffer *right = NULL;
9105f39d397SChris Mason 	struct extent_buffer *mid;
9115f39d397SChris Mason 	struct extent_buffer *left = NULL;
9125f39d397SChris Mason 	struct extent_buffer *parent = NULL;
913bb803951SChris Mason 	int ret = 0;
914bb803951SChris Mason 	int wret;
915bb803951SChris Mason 	int pslot;
916bb803951SChris Mason 	int orig_slot = path->slots[level];
91779f95c82SChris Mason 	u64 orig_ptr;
918bb803951SChris Mason 
919bb803951SChris Mason 	if (level == 0)
920bb803951SChris Mason 		return 0;
921bb803951SChris Mason 
9225f39d397SChris Mason 	mid = path->nodes[level];
923b4ce94deSChris Mason 
924bd681513SChris Mason 	WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
925bd681513SChris Mason 		path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
9267bb86316SChris Mason 	WARN_ON(btrfs_header_generation(mid) != trans->transid);
9277bb86316SChris Mason 
9281d4f8a0cSChris Mason 	orig_ptr = btrfs_node_blockptr(mid, orig_slot);
92979f95c82SChris Mason 
930a05a9bb1SLi Zefan 	if (level < BTRFS_MAX_LEVEL - 1) {
9315f39d397SChris Mason 		parent = path->nodes[level + 1];
932bb803951SChris Mason 		pslot = path->slots[level + 1];
933a05a9bb1SLi Zefan 	}
934bb803951SChris Mason 
93540689478SChris Mason 	/*
93640689478SChris Mason 	 * deal with the case where there is only one pointer in the root
93740689478SChris Mason 	 * by promoting the node below to a root
93840689478SChris Mason 	 */
9395f39d397SChris Mason 	if (!parent) {
9405f39d397SChris Mason 		struct extent_buffer *child;
941bb803951SChris Mason 
9425f39d397SChris Mason 		if (btrfs_header_nritems(mid) != 1)
943bb803951SChris Mason 			return 0;
944bb803951SChris Mason 
945bb803951SChris Mason 		/* promote the child to a root */
9465f39d397SChris Mason 		child = read_node_slot(root, mid, 0);
947305a26afSMark Fasheh 		if (!child) {
948305a26afSMark Fasheh 			ret = -EROFS;
949305a26afSMark Fasheh 			btrfs_std_error(root->fs_info, ret);
950305a26afSMark Fasheh 			goto enospc;
951305a26afSMark Fasheh 		}
952305a26afSMark Fasheh 
953925baeddSChris Mason 		btrfs_tree_lock(child);
954b4ce94deSChris Mason 		btrfs_set_lock_blocking(child);
9559fa8cfe7SChris Mason 		ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
956f0486c68SYan, Zheng 		if (ret) {
957f0486c68SYan, Zheng 			btrfs_tree_unlock(child);
958f0486c68SYan, Zheng 			free_extent_buffer(child);
959f0486c68SYan, Zheng 			goto enospc;
960f0486c68SYan, Zheng 		}
9612f375ab9SYan 
962240f62c8SChris Mason 		rcu_assign_pointer(root->node, child);
963925baeddSChris Mason 
9640b86a832SChris Mason 		add_root_to_dirty_list(root);
965925baeddSChris Mason 		btrfs_tree_unlock(child);
966b4ce94deSChris Mason 
967925baeddSChris Mason 		path->locks[level] = 0;
968bb803951SChris Mason 		path->nodes[level] = NULL;
9695f39d397SChris Mason 		clean_tree_block(trans, root, mid);
970925baeddSChris Mason 		btrfs_tree_unlock(mid);
971bb803951SChris Mason 		/* once for the path */
9725f39d397SChris Mason 		free_extent_buffer(mid);
973f0486c68SYan, Zheng 
974f0486c68SYan, Zheng 		root_sub_used(root, mid->len);
97566d7e7f0SArne Jansen 		btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
976bb803951SChris Mason 		/* once for the root ptr */
9775f39d397SChris Mason 		free_extent_buffer(mid);
978f0486c68SYan, Zheng 		return 0;
979bb803951SChris Mason 	}
9805f39d397SChris Mason 	if (btrfs_header_nritems(mid) >
981123abc88SChris Mason 	    BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
982bb803951SChris Mason 		return 0;
983bb803951SChris Mason 
984559af821SAndi Kleen 	btrfs_header_nritems(mid);
98554aa1f4dSChris Mason 
9865f39d397SChris Mason 	left = read_node_slot(root, parent, pslot - 1);
9875f39d397SChris Mason 	if (left) {
988925baeddSChris Mason 		btrfs_tree_lock(left);
989b4ce94deSChris Mason 		btrfs_set_lock_blocking(left);
9905f39d397SChris Mason 		wret = btrfs_cow_block(trans, root, left,
9919fa8cfe7SChris Mason 				       parent, pslot - 1, &left);
99254aa1f4dSChris Mason 		if (wret) {
99354aa1f4dSChris Mason 			ret = wret;
99454aa1f4dSChris Mason 			goto enospc;
99554aa1f4dSChris Mason 		}
9962cc58cf2SChris Mason 	}
9975f39d397SChris Mason 	right = read_node_slot(root, parent, pslot + 1);
9985f39d397SChris Mason 	if (right) {
999925baeddSChris Mason 		btrfs_tree_lock(right);
1000b4ce94deSChris Mason 		btrfs_set_lock_blocking(right);
10015f39d397SChris Mason 		wret = btrfs_cow_block(trans, root, right,
10029fa8cfe7SChris Mason 				       parent, pslot + 1, &right);
10032cc58cf2SChris Mason 		if (wret) {
10042cc58cf2SChris Mason 			ret = wret;
10052cc58cf2SChris Mason 			goto enospc;
10062cc58cf2SChris Mason 		}
10072cc58cf2SChris Mason 	}
10082cc58cf2SChris Mason 
10092cc58cf2SChris Mason 	/* first, try to make some room in the middle buffer */
10105f39d397SChris Mason 	if (left) {
10115f39d397SChris Mason 		orig_slot += btrfs_header_nritems(left);
1012bce4eae9SChris Mason 		wret = push_node_left(trans, root, left, mid, 1);
101379f95c82SChris Mason 		if (wret < 0)
101479f95c82SChris Mason 			ret = wret;
1015559af821SAndi Kleen 		btrfs_header_nritems(mid);
1016bb803951SChris Mason 	}
101779f95c82SChris Mason 
101879f95c82SChris Mason 	/*
101979f95c82SChris Mason 	 * then try to empty the right most buffer into the middle
102079f95c82SChris Mason 	 */
10215f39d397SChris Mason 	if (right) {
1022971a1f66SChris Mason 		wret = push_node_left(trans, root, mid, right, 1);
102354aa1f4dSChris Mason 		if (wret < 0 && wret != -ENOSPC)
102479f95c82SChris Mason 			ret = wret;
10255f39d397SChris Mason 		if (btrfs_header_nritems(right) == 0) {
10265f39d397SChris Mason 			clean_tree_block(trans, root, right);
1027925baeddSChris Mason 			btrfs_tree_unlock(right);
1028143bede5SJeff Mahoney 			del_ptr(trans, root, path, level + 1, pslot + 1);
1029f0486c68SYan, Zheng 			root_sub_used(root, right->len);
103066d7e7f0SArne Jansen 			btrfs_free_tree_block(trans, root, right, 0, 1, 0);
1031f0486c68SYan, Zheng 			free_extent_buffer(right);
1032f0486c68SYan, Zheng 			right = NULL;
1033bb803951SChris Mason 		} else {
10345f39d397SChris Mason 			struct btrfs_disk_key right_key;
10355f39d397SChris Mason 			btrfs_node_key(right, &right_key, 0);
10365f39d397SChris Mason 			btrfs_set_node_key(parent, &right_key, pslot + 1);
10375f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
1038bb803951SChris Mason 		}
1039bb803951SChris Mason 	}
10405f39d397SChris Mason 	if (btrfs_header_nritems(mid) == 1) {
104179f95c82SChris Mason 		/*
104279f95c82SChris Mason 		 * we're not allowed to leave a node with one item in the
104379f95c82SChris Mason 		 * tree during a delete.  A deletion from lower in the tree
104479f95c82SChris Mason 		 * could try to delete the only pointer in this node.
104579f95c82SChris Mason 		 * So, pull some keys from the left.
104679f95c82SChris Mason 		 * There has to be a left pointer at this point because
104779f95c82SChris Mason 		 * otherwise we would have pulled some pointers from the
104879f95c82SChris Mason 		 * right
104979f95c82SChris Mason 		 */
1050305a26afSMark Fasheh 		if (!left) {
1051305a26afSMark Fasheh 			ret = -EROFS;
1052305a26afSMark Fasheh 			btrfs_std_error(root->fs_info, ret);
1053305a26afSMark Fasheh 			goto enospc;
1054305a26afSMark Fasheh 		}
10555f39d397SChris Mason 		wret = balance_node_right(trans, root, mid, left);
105654aa1f4dSChris Mason 		if (wret < 0) {
105779f95c82SChris Mason 			ret = wret;
105854aa1f4dSChris Mason 			goto enospc;
105954aa1f4dSChris Mason 		}
1060bce4eae9SChris Mason 		if (wret == 1) {
1061bce4eae9SChris Mason 			wret = push_node_left(trans, root, left, mid, 1);
1062bce4eae9SChris Mason 			if (wret < 0)
1063bce4eae9SChris Mason 				ret = wret;
1064bce4eae9SChris Mason 		}
106579f95c82SChris Mason 		BUG_ON(wret == 1);
106679f95c82SChris Mason 	}
10675f39d397SChris Mason 	if (btrfs_header_nritems(mid) == 0) {
10685f39d397SChris Mason 		clean_tree_block(trans, root, mid);
1069925baeddSChris Mason 		btrfs_tree_unlock(mid);
1070143bede5SJeff Mahoney 		del_ptr(trans, root, path, level + 1, pslot);
1071f0486c68SYan, Zheng 		root_sub_used(root, mid->len);
107266d7e7f0SArne Jansen 		btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
1073f0486c68SYan, Zheng 		free_extent_buffer(mid);
1074f0486c68SYan, Zheng 		mid = NULL;
107579f95c82SChris Mason 	} else {
107679f95c82SChris Mason 		/* update the parent key to reflect our changes */
10775f39d397SChris Mason 		struct btrfs_disk_key mid_key;
10785f39d397SChris Mason 		btrfs_node_key(mid, &mid_key, 0);
10795f39d397SChris Mason 		btrfs_set_node_key(parent, &mid_key, pslot);
10805f39d397SChris Mason 		btrfs_mark_buffer_dirty(parent);
108179f95c82SChris Mason 	}
1082bb803951SChris Mason 
108379f95c82SChris Mason 	/* update the path */
10845f39d397SChris Mason 	if (left) {
10855f39d397SChris Mason 		if (btrfs_header_nritems(left) > orig_slot) {
10865f39d397SChris Mason 			extent_buffer_get(left);
1087925baeddSChris Mason 			/* left was locked after cow */
10885f39d397SChris Mason 			path->nodes[level] = left;
1089bb803951SChris Mason 			path->slots[level + 1] -= 1;
1090bb803951SChris Mason 			path->slots[level] = orig_slot;
1091925baeddSChris Mason 			if (mid) {
1092925baeddSChris Mason 				btrfs_tree_unlock(mid);
10935f39d397SChris Mason 				free_extent_buffer(mid);
1094925baeddSChris Mason 			}
1095bb803951SChris Mason 		} else {
10965f39d397SChris Mason 			orig_slot -= btrfs_header_nritems(left);
1097bb803951SChris Mason 			path->slots[level] = orig_slot;
1098bb803951SChris Mason 		}
1099bb803951SChris Mason 	}
110079f95c82SChris Mason 	/* double check we haven't messed things up */
1101e20d96d6SChris Mason 	if (orig_ptr !=
11025f39d397SChris Mason 	    btrfs_node_blockptr(path->nodes[level], path->slots[level]))
110379f95c82SChris Mason 		BUG();
110454aa1f4dSChris Mason enospc:
1105925baeddSChris Mason 	if (right) {
1106925baeddSChris Mason 		btrfs_tree_unlock(right);
11075f39d397SChris Mason 		free_extent_buffer(right);
1108925baeddSChris Mason 	}
1109925baeddSChris Mason 	if (left) {
1110925baeddSChris Mason 		if (path->nodes[level] != left)
1111925baeddSChris Mason 			btrfs_tree_unlock(left);
11125f39d397SChris Mason 		free_extent_buffer(left);
1113925baeddSChris Mason 	}
1114bb803951SChris Mason 	return ret;
1115bb803951SChris Mason }
1116bb803951SChris Mason 
1117d352ac68SChris Mason /* Node balancing for insertion.  Here we only split or push nodes around
1118d352ac68SChris Mason  * when they are completely full.  This is also done top down, so we
1119d352ac68SChris Mason  * have to be pessimistic.
1120d352ac68SChris Mason  */
1121d397712bSChris Mason static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1122e66f709bSChris Mason 					  struct btrfs_root *root,
1123e66f709bSChris Mason 					  struct btrfs_path *path, int level)
1124e66f709bSChris Mason {
11255f39d397SChris Mason 	struct extent_buffer *right = NULL;
11265f39d397SChris Mason 	struct extent_buffer *mid;
11275f39d397SChris Mason 	struct extent_buffer *left = NULL;
11285f39d397SChris Mason 	struct extent_buffer *parent = NULL;
1129e66f709bSChris Mason 	int ret = 0;
1130e66f709bSChris Mason 	int wret;
1131e66f709bSChris Mason 	int pslot;
1132e66f709bSChris Mason 	int orig_slot = path->slots[level];
1133e66f709bSChris Mason 
1134e66f709bSChris Mason 	if (level == 0)
1135e66f709bSChris Mason 		return 1;
1136e66f709bSChris Mason 
11375f39d397SChris Mason 	mid = path->nodes[level];
11387bb86316SChris Mason 	WARN_ON(btrfs_header_generation(mid) != trans->transid);
1139e66f709bSChris Mason 
1140a05a9bb1SLi Zefan 	if (level < BTRFS_MAX_LEVEL - 1) {
11415f39d397SChris Mason 		parent = path->nodes[level + 1];
1142e66f709bSChris Mason 		pslot = path->slots[level + 1];
1143a05a9bb1SLi Zefan 	}
1144e66f709bSChris Mason 
11455f39d397SChris Mason 	if (!parent)
1146e66f709bSChris Mason 		return 1;
1147e66f709bSChris Mason 
11485f39d397SChris Mason 	left = read_node_slot(root, parent, pslot - 1);
1149e66f709bSChris Mason 
1150e66f709bSChris Mason 	/* first, try to make some room in the middle buffer */
11515f39d397SChris Mason 	if (left) {
1152e66f709bSChris Mason 		u32 left_nr;
1153925baeddSChris Mason 
1154925baeddSChris Mason 		btrfs_tree_lock(left);
1155b4ce94deSChris Mason 		btrfs_set_lock_blocking(left);
1156b4ce94deSChris Mason 
11575f39d397SChris Mason 		left_nr = btrfs_header_nritems(left);
115833ade1f8SChris Mason 		if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
115933ade1f8SChris Mason 			wret = 1;
116033ade1f8SChris Mason 		} else {
11615f39d397SChris Mason 			ret = btrfs_cow_block(trans, root, left, parent,
11629fa8cfe7SChris Mason 					      pslot - 1, &left);
116354aa1f4dSChris Mason 			if (ret)
116454aa1f4dSChris Mason 				wret = 1;
116554aa1f4dSChris Mason 			else {
116654aa1f4dSChris Mason 				wret = push_node_left(trans, root,
1167971a1f66SChris Mason 						      left, mid, 0);
116854aa1f4dSChris Mason 			}
116933ade1f8SChris Mason 		}
1170e66f709bSChris Mason 		if (wret < 0)
1171e66f709bSChris Mason 			ret = wret;
1172e66f709bSChris Mason 		if (wret == 0) {
11735f39d397SChris Mason 			struct btrfs_disk_key disk_key;
1174e66f709bSChris Mason 			orig_slot += left_nr;
11755f39d397SChris Mason 			btrfs_node_key(mid, &disk_key, 0);
11765f39d397SChris Mason 			btrfs_set_node_key(parent, &disk_key, pslot);
11775f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
11785f39d397SChris Mason 			if (btrfs_header_nritems(left) > orig_slot) {
11795f39d397SChris Mason 				path->nodes[level] = left;
1180e66f709bSChris Mason 				path->slots[level + 1] -= 1;
1181e66f709bSChris Mason 				path->slots[level] = orig_slot;
1182925baeddSChris Mason 				btrfs_tree_unlock(mid);
11835f39d397SChris Mason 				free_extent_buffer(mid);
1184e66f709bSChris Mason 			} else {
1185e66f709bSChris Mason 				orig_slot -=
11865f39d397SChris Mason 					btrfs_header_nritems(left);
1187e66f709bSChris Mason 				path->slots[level] = orig_slot;
1188925baeddSChris Mason 				btrfs_tree_unlock(left);
11895f39d397SChris Mason 				free_extent_buffer(left);
1190e66f709bSChris Mason 			}
1191e66f709bSChris Mason 			return 0;
1192e66f709bSChris Mason 		}
1193925baeddSChris Mason 		btrfs_tree_unlock(left);
11945f39d397SChris Mason 		free_extent_buffer(left);
1195e66f709bSChris Mason 	}
11965f39d397SChris Mason 	right = read_node_slot(root, parent, pslot + 1);
1197e66f709bSChris Mason 
1198e66f709bSChris Mason 	/*
1199e66f709bSChris Mason 	 * then try to empty the right most buffer into the middle
1200e66f709bSChris Mason 	 */
12015f39d397SChris Mason 	if (right) {
120233ade1f8SChris Mason 		u32 right_nr;
1203b4ce94deSChris Mason 
1204925baeddSChris Mason 		btrfs_tree_lock(right);
1205b4ce94deSChris Mason 		btrfs_set_lock_blocking(right);
1206b4ce94deSChris Mason 
12075f39d397SChris Mason 		right_nr = btrfs_header_nritems(right);
120833ade1f8SChris Mason 		if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
120933ade1f8SChris Mason 			wret = 1;
121033ade1f8SChris Mason 		} else {
12115f39d397SChris Mason 			ret = btrfs_cow_block(trans, root, right,
12125f39d397SChris Mason 					      parent, pslot + 1,
12139fa8cfe7SChris Mason 					      &right);
121454aa1f4dSChris Mason 			if (ret)
121554aa1f4dSChris Mason 				wret = 1;
121654aa1f4dSChris Mason 			else {
121733ade1f8SChris Mason 				wret = balance_node_right(trans, root,
12185f39d397SChris Mason 							  right, mid);
121933ade1f8SChris Mason 			}
122054aa1f4dSChris Mason 		}
1221e66f709bSChris Mason 		if (wret < 0)
1222e66f709bSChris Mason 			ret = wret;
1223e66f709bSChris Mason 		if (wret == 0) {
12245f39d397SChris Mason 			struct btrfs_disk_key disk_key;
12255f39d397SChris Mason 
12265f39d397SChris Mason 			btrfs_node_key(right, &disk_key, 0);
12275f39d397SChris Mason 			btrfs_set_node_key(parent, &disk_key, pslot + 1);
12285f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
12295f39d397SChris Mason 
12305f39d397SChris Mason 			if (btrfs_header_nritems(mid) <= orig_slot) {
12315f39d397SChris Mason 				path->nodes[level] = right;
1232e66f709bSChris Mason 				path->slots[level + 1] += 1;
1233e66f709bSChris Mason 				path->slots[level] = orig_slot -
12345f39d397SChris Mason 					btrfs_header_nritems(mid);
1235925baeddSChris Mason 				btrfs_tree_unlock(mid);
12365f39d397SChris Mason 				free_extent_buffer(mid);
1237e66f709bSChris Mason 			} else {
1238925baeddSChris Mason 				btrfs_tree_unlock(right);
12395f39d397SChris Mason 				free_extent_buffer(right);
1240e66f709bSChris Mason 			}
1241e66f709bSChris Mason 			return 0;
1242e66f709bSChris Mason 		}
1243925baeddSChris Mason 		btrfs_tree_unlock(right);
12445f39d397SChris Mason 		free_extent_buffer(right);
1245e66f709bSChris Mason 	}
1246e66f709bSChris Mason 	return 1;
1247e66f709bSChris Mason }
1248e66f709bSChris Mason 
124974123bd7SChris Mason /*
1250d352ac68SChris Mason  * readahead one full node of leaves, finding things that are close
1251d352ac68SChris Mason  * to the block in 'slot', and triggering ra on them.
12523c69faecSChris Mason  */
1253c8c42864SChris Mason static void reada_for_search(struct btrfs_root *root,
1254e02119d5SChris Mason 			     struct btrfs_path *path,
125501f46658SChris Mason 			     int level, int slot, u64 objectid)
12563c69faecSChris Mason {
12575f39d397SChris Mason 	struct extent_buffer *node;
125801f46658SChris Mason 	struct btrfs_disk_key disk_key;
12593c69faecSChris Mason 	u32 nritems;
12603c69faecSChris Mason 	u64 search;
1261a7175319SChris Mason 	u64 target;
12626b80053dSChris Mason 	u64 nread = 0;
1263cb25c2eaSJosef Bacik 	u64 gen;
12643c69faecSChris Mason 	int direction = path->reada;
12655f39d397SChris Mason 	struct extent_buffer *eb;
12666b80053dSChris Mason 	u32 nr;
12676b80053dSChris Mason 	u32 blocksize;
12686b80053dSChris Mason 	u32 nscan = 0;
1269db94535dSChris Mason 
1270a6b6e75eSChris Mason 	if (level != 1)
12713c69faecSChris Mason 		return;
12723c69faecSChris Mason 
12736702ed49SChris Mason 	if (!path->nodes[level])
12746702ed49SChris Mason 		return;
12756702ed49SChris Mason 
12765f39d397SChris Mason 	node = path->nodes[level];
1277925baeddSChris Mason 
12783c69faecSChris Mason 	search = btrfs_node_blockptr(node, slot);
12796b80053dSChris Mason 	blocksize = btrfs_level_size(root, level - 1);
12806b80053dSChris Mason 	eb = btrfs_find_tree_block(root, search, blocksize);
12815f39d397SChris Mason 	if (eb) {
12825f39d397SChris Mason 		free_extent_buffer(eb);
12833c69faecSChris Mason 		return;
12843c69faecSChris Mason 	}
12853c69faecSChris Mason 
1286a7175319SChris Mason 	target = search;
12876b80053dSChris Mason 
12885f39d397SChris Mason 	nritems = btrfs_header_nritems(node);
12896b80053dSChris Mason 	nr = slot;
129025b8b936SJosef Bacik 
12913c69faecSChris Mason 	while (1) {
12926b80053dSChris Mason 		if (direction < 0) {
12936b80053dSChris Mason 			if (nr == 0)
12943c69faecSChris Mason 				break;
12956b80053dSChris Mason 			nr--;
12966b80053dSChris Mason 		} else if (direction > 0) {
12976b80053dSChris Mason 			nr++;
12986b80053dSChris Mason 			if (nr >= nritems)
12996b80053dSChris Mason 				break;
13003c69faecSChris Mason 		}
130101f46658SChris Mason 		if (path->reada < 0 && objectid) {
130201f46658SChris Mason 			btrfs_node_key(node, &disk_key, nr);
130301f46658SChris Mason 			if (btrfs_disk_key_objectid(&disk_key) != objectid)
130401f46658SChris Mason 				break;
130501f46658SChris Mason 		}
13066b80053dSChris Mason 		search = btrfs_node_blockptr(node, nr);
1307a7175319SChris Mason 		if ((search <= target && target - search <= 65536) ||
1308a7175319SChris Mason 		    (search > target && search - target <= 65536)) {
1309cb25c2eaSJosef Bacik 			gen = btrfs_node_ptr_generation(node, nr);
1310cb25c2eaSJosef Bacik 			readahead_tree_block(root, search, blocksize, gen);
13116b80053dSChris Mason 			nread += blocksize;
13123c69faecSChris Mason 		}
13136b80053dSChris Mason 		nscan++;
1314a7175319SChris Mason 		if ((nread > 65536 || nscan > 32))
13156b80053dSChris Mason 			break;
13163c69faecSChris Mason 	}
13173c69faecSChris Mason }
1318925baeddSChris Mason 
1319d352ac68SChris Mason /*
1320b4ce94deSChris Mason  * returns -EAGAIN if it had to drop the path, or zero if everything was in
1321b4ce94deSChris Mason  * cache
1322b4ce94deSChris Mason  */
1323b4ce94deSChris Mason static noinline int reada_for_balance(struct btrfs_root *root,
1324b4ce94deSChris Mason 				      struct btrfs_path *path, int level)
1325b4ce94deSChris Mason {
1326b4ce94deSChris Mason 	int slot;
1327b4ce94deSChris Mason 	int nritems;
1328b4ce94deSChris Mason 	struct extent_buffer *parent;
1329b4ce94deSChris Mason 	struct extent_buffer *eb;
1330b4ce94deSChris Mason 	u64 gen;
1331b4ce94deSChris Mason 	u64 block1 = 0;
1332b4ce94deSChris Mason 	u64 block2 = 0;
1333b4ce94deSChris Mason 	int ret = 0;
1334b4ce94deSChris Mason 	int blocksize;
1335b4ce94deSChris Mason 
13368c594ea8SChris Mason 	parent = path->nodes[level + 1];
1337b4ce94deSChris Mason 	if (!parent)
1338b4ce94deSChris Mason 		return 0;
1339b4ce94deSChris Mason 
1340b4ce94deSChris Mason 	nritems = btrfs_header_nritems(parent);
13418c594ea8SChris Mason 	slot = path->slots[level + 1];
1342b4ce94deSChris Mason 	blocksize = btrfs_level_size(root, level);
1343b4ce94deSChris Mason 
1344b4ce94deSChris Mason 	if (slot > 0) {
1345b4ce94deSChris Mason 		block1 = btrfs_node_blockptr(parent, slot - 1);
1346b4ce94deSChris Mason 		gen = btrfs_node_ptr_generation(parent, slot - 1);
1347b4ce94deSChris Mason 		eb = btrfs_find_tree_block(root, block1, blocksize);
1348b4ce94deSChris Mason 		if (eb && btrfs_buffer_uptodate(eb, gen))
1349b4ce94deSChris Mason 			block1 = 0;
1350b4ce94deSChris Mason 		free_extent_buffer(eb);
1351b4ce94deSChris Mason 	}
13528c594ea8SChris Mason 	if (slot + 1 < nritems) {
1353b4ce94deSChris Mason 		block2 = btrfs_node_blockptr(parent, slot + 1);
1354b4ce94deSChris Mason 		gen = btrfs_node_ptr_generation(parent, slot + 1);
1355b4ce94deSChris Mason 		eb = btrfs_find_tree_block(root, block2, blocksize);
1356b4ce94deSChris Mason 		if (eb && btrfs_buffer_uptodate(eb, gen))
1357b4ce94deSChris Mason 			block2 = 0;
1358b4ce94deSChris Mason 		free_extent_buffer(eb);
1359b4ce94deSChris Mason 	}
1360b4ce94deSChris Mason 	if (block1 || block2) {
1361b4ce94deSChris Mason 		ret = -EAGAIN;
13628c594ea8SChris Mason 
13638c594ea8SChris Mason 		/* release the whole path */
1364b3b4aa74SDavid Sterba 		btrfs_release_path(path);
13658c594ea8SChris Mason 
13668c594ea8SChris Mason 		/* read the blocks */
1367b4ce94deSChris Mason 		if (block1)
1368b4ce94deSChris Mason 			readahead_tree_block(root, block1, blocksize, 0);
1369b4ce94deSChris Mason 		if (block2)
1370b4ce94deSChris Mason 			readahead_tree_block(root, block2, blocksize, 0);
1371b4ce94deSChris Mason 
1372b4ce94deSChris Mason 		if (block1) {
1373b4ce94deSChris Mason 			eb = read_tree_block(root, block1, blocksize, 0);
1374b4ce94deSChris Mason 			free_extent_buffer(eb);
1375b4ce94deSChris Mason 		}
13768c594ea8SChris Mason 		if (block2) {
1377b4ce94deSChris Mason 			eb = read_tree_block(root, block2, blocksize, 0);
1378b4ce94deSChris Mason 			free_extent_buffer(eb);
1379b4ce94deSChris Mason 		}
1380b4ce94deSChris Mason 	}
1381b4ce94deSChris Mason 	return ret;
1382b4ce94deSChris Mason }
1383b4ce94deSChris Mason 
1384b4ce94deSChris Mason 
1385b4ce94deSChris Mason /*
1386d397712bSChris Mason  * when we walk down the tree, it is usually safe to unlock the higher layers
1387d397712bSChris Mason  * in the tree.  The exceptions are when our path goes through slot 0, because
1388d397712bSChris Mason  * operations on the tree might require changing key pointers higher up in the
1389d397712bSChris Mason  * tree.
1390d352ac68SChris Mason  *
1391d397712bSChris Mason  * callers might also have set path->keep_locks, which tells this code to keep
1392d397712bSChris Mason  * the lock if the path points to the last slot in the block.  This is part of
1393d397712bSChris Mason  * walking through the tree, and selecting the next slot in the higher block.
1394d352ac68SChris Mason  *
1395d397712bSChris Mason  * lowest_unlock sets the lowest level in the tree we're allowed to unlock.  so
1396d397712bSChris Mason  * if lowest_unlock is 1, level 0 won't be unlocked
1397d352ac68SChris Mason  */
1398e02119d5SChris Mason static noinline void unlock_up(struct btrfs_path *path, int level,
1399e02119d5SChris Mason 			       int lowest_unlock)
1400925baeddSChris Mason {
1401925baeddSChris Mason 	int i;
1402925baeddSChris Mason 	int skip_level = level;
1403051e1b9fSChris Mason 	int no_skips = 0;
1404925baeddSChris Mason 	struct extent_buffer *t;
1405925baeddSChris Mason 
1406925baeddSChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1407925baeddSChris Mason 		if (!path->nodes[i])
1408925baeddSChris Mason 			break;
1409925baeddSChris Mason 		if (!path->locks[i])
1410925baeddSChris Mason 			break;
1411051e1b9fSChris Mason 		if (!no_skips && path->slots[i] == 0) {
1412925baeddSChris Mason 			skip_level = i + 1;
1413925baeddSChris Mason 			continue;
1414925baeddSChris Mason 		}
1415051e1b9fSChris Mason 		if (!no_skips && path->keep_locks) {
1416925baeddSChris Mason 			u32 nritems;
1417925baeddSChris Mason 			t = path->nodes[i];
1418925baeddSChris Mason 			nritems = btrfs_header_nritems(t);
1419051e1b9fSChris Mason 			if (nritems < 1 || path->slots[i] >= nritems - 1) {
1420925baeddSChris Mason 				skip_level = i + 1;
1421925baeddSChris Mason 				continue;
1422925baeddSChris Mason 			}
1423925baeddSChris Mason 		}
1424051e1b9fSChris Mason 		if (skip_level < i && i >= lowest_unlock)
1425051e1b9fSChris Mason 			no_skips = 1;
1426051e1b9fSChris Mason 
1427925baeddSChris Mason 		t = path->nodes[i];
1428925baeddSChris Mason 		if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1429bd681513SChris Mason 			btrfs_tree_unlock_rw(t, path->locks[i]);
1430925baeddSChris Mason 			path->locks[i] = 0;
1431925baeddSChris Mason 		}
1432925baeddSChris Mason 	}
1433925baeddSChris Mason }
1434925baeddSChris Mason 
14353c69faecSChris Mason /*
1436b4ce94deSChris Mason  * This releases any locks held in the path starting at level and
1437b4ce94deSChris Mason  * going all the way up to the root.
1438b4ce94deSChris Mason  *
1439b4ce94deSChris Mason  * btrfs_search_slot will keep the lock held on higher nodes in a few
1440b4ce94deSChris Mason  * corner cases, such as COW of the block at slot zero in the node.  This
1441b4ce94deSChris Mason  * ignores those rules, and it should only be called when there are no
1442b4ce94deSChris Mason  * more updates to be done higher up in the tree.
1443b4ce94deSChris Mason  */
1444b4ce94deSChris Mason noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1445b4ce94deSChris Mason {
1446b4ce94deSChris Mason 	int i;
1447b4ce94deSChris Mason 
14485d4f98a2SYan Zheng 	if (path->keep_locks)
1449b4ce94deSChris Mason 		return;
1450b4ce94deSChris Mason 
1451b4ce94deSChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1452b4ce94deSChris Mason 		if (!path->nodes[i])
145312f4daccSChris Mason 			continue;
1454b4ce94deSChris Mason 		if (!path->locks[i])
145512f4daccSChris Mason 			continue;
1456bd681513SChris Mason 		btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
1457b4ce94deSChris Mason 		path->locks[i] = 0;
1458b4ce94deSChris Mason 	}
1459b4ce94deSChris Mason }
1460b4ce94deSChris Mason 
1461b4ce94deSChris Mason /*
1462c8c42864SChris Mason  * helper function for btrfs_search_slot.  The goal is to find a block
1463c8c42864SChris Mason  * in cache without setting the path to blocking.  If we find the block
1464c8c42864SChris Mason  * we return zero and the path is unchanged.
1465c8c42864SChris Mason  *
1466c8c42864SChris Mason  * If we can't find the block, we set the path blocking and do some
1467c8c42864SChris Mason  * reada.  -EAGAIN is returned and the search must be repeated.
1468c8c42864SChris Mason  */
1469c8c42864SChris Mason static int
1470c8c42864SChris Mason read_block_for_search(struct btrfs_trans_handle *trans,
1471c8c42864SChris Mason 		       struct btrfs_root *root, struct btrfs_path *p,
1472c8c42864SChris Mason 		       struct extent_buffer **eb_ret, int level, int slot,
1473c8c42864SChris Mason 		       struct btrfs_key *key)
1474c8c42864SChris Mason {
1475c8c42864SChris Mason 	u64 blocknr;
1476c8c42864SChris Mason 	u64 gen;
1477c8c42864SChris Mason 	u32 blocksize;
1478c8c42864SChris Mason 	struct extent_buffer *b = *eb_ret;
1479c8c42864SChris Mason 	struct extent_buffer *tmp;
148076a05b35SChris Mason 	int ret;
1481c8c42864SChris Mason 
1482c8c42864SChris Mason 	blocknr = btrfs_node_blockptr(b, slot);
1483c8c42864SChris Mason 	gen = btrfs_node_ptr_generation(b, slot);
1484c8c42864SChris Mason 	blocksize = btrfs_level_size(root, level - 1);
1485c8c42864SChris Mason 
1486c8c42864SChris Mason 	tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1487cb44921aSChris Mason 	if (tmp) {
1488cb44921aSChris Mason 		if (btrfs_buffer_uptodate(tmp, 0)) {
1489cb44921aSChris Mason 			if (btrfs_buffer_uptodate(tmp, gen)) {
149076a05b35SChris Mason 				/*
1491cb44921aSChris Mason 				 * we found an up to date block without
1492cb44921aSChris Mason 				 * sleeping, return
149376a05b35SChris Mason 				 * right away
149476a05b35SChris Mason 				 */
1495c8c42864SChris Mason 				*eb_ret = tmp;
1496c8c42864SChris Mason 				return 0;
1497c8c42864SChris Mason 			}
1498cb44921aSChris Mason 			/* the pages were up to date, but we failed
1499cb44921aSChris Mason 			 * the generation number check.  Do a full
1500cb44921aSChris Mason 			 * read for the generation number that is correct.
1501cb44921aSChris Mason 			 * We must do this without dropping locks so
1502cb44921aSChris Mason 			 * we can trust our generation number
1503cb44921aSChris Mason 			 */
1504cb44921aSChris Mason 			free_extent_buffer(tmp);
1505bd681513SChris Mason 			btrfs_set_path_blocking(p);
1506bd681513SChris Mason 
1507cb44921aSChris Mason 			tmp = read_tree_block(root, blocknr, blocksize, gen);
1508cb44921aSChris Mason 			if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1509cb44921aSChris Mason 				*eb_ret = tmp;
1510cb44921aSChris Mason 				return 0;
1511cb44921aSChris Mason 			}
1512cb44921aSChris Mason 			free_extent_buffer(tmp);
1513b3b4aa74SDavid Sterba 			btrfs_release_path(p);
1514cb44921aSChris Mason 			return -EIO;
1515cb44921aSChris Mason 		}
1516cb44921aSChris Mason 	}
1517c8c42864SChris Mason 
1518c8c42864SChris Mason 	/*
1519c8c42864SChris Mason 	 * reduce lock contention at high levels
1520c8c42864SChris Mason 	 * of the btree by dropping locks before
152176a05b35SChris Mason 	 * we read.  Don't release the lock on the current
152276a05b35SChris Mason 	 * level because we need to walk this node to figure
152376a05b35SChris Mason 	 * out which blocks to read.
1524c8c42864SChris Mason 	 */
15258c594ea8SChris Mason 	btrfs_unlock_up_safe(p, level + 1);
15268c594ea8SChris Mason 	btrfs_set_path_blocking(p);
15278c594ea8SChris Mason 
1528c8c42864SChris Mason 	free_extent_buffer(tmp);
1529c8c42864SChris Mason 	if (p->reada)
1530c8c42864SChris Mason 		reada_for_search(root, p, level, slot, key->objectid);
1531c8c42864SChris Mason 
1532b3b4aa74SDavid Sterba 	btrfs_release_path(p);
153376a05b35SChris Mason 
153476a05b35SChris Mason 	ret = -EAGAIN;
15355bdd3536SYan, Zheng 	tmp = read_tree_block(root, blocknr, blocksize, 0);
153676a05b35SChris Mason 	if (tmp) {
153776a05b35SChris Mason 		/*
153876a05b35SChris Mason 		 * If the read above didn't mark this buffer up to date,
153976a05b35SChris Mason 		 * it will never end up being up to date.  Set ret to EIO now
154076a05b35SChris Mason 		 * and give up so that our caller doesn't loop forever
154176a05b35SChris Mason 		 * on our EAGAINs.
154276a05b35SChris Mason 		 */
154376a05b35SChris Mason 		if (!btrfs_buffer_uptodate(tmp, 0))
154476a05b35SChris Mason 			ret = -EIO;
1545c8c42864SChris Mason 		free_extent_buffer(tmp);
154676a05b35SChris Mason 	}
154776a05b35SChris Mason 	return ret;
1548c8c42864SChris Mason }
1549c8c42864SChris Mason 
1550c8c42864SChris Mason /*
1551c8c42864SChris Mason  * helper function for btrfs_search_slot.  This does all of the checks
1552c8c42864SChris Mason  * for node-level blocks and does any balancing required based on
1553c8c42864SChris Mason  * the ins_len.
1554c8c42864SChris Mason  *
1555c8c42864SChris Mason  * If no extra work was required, zero is returned.  If we had to
1556c8c42864SChris Mason  * drop the path, -EAGAIN is returned and btrfs_search_slot must
1557c8c42864SChris Mason  * start over
1558c8c42864SChris Mason  */
1559c8c42864SChris Mason static int
1560c8c42864SChris Mason setup_nodes_for_search(struct btrfs_trans_handle *trans,
1561c8c42864SChris Mason 		       struct btrfs_root *root, struct btrfs_path *p,
1562bd681513SChris Mason 		       struct extent_buffer *b, int level, int ins_len,
1563bd681513SChris Mason 		       int *write_lock_level)
1564c8c42864SChris Mason {
1565c8c42864SChris Mason 	int ret;
1566c8c42864SChris Mason 	if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1567c8c42864SChris Mason 	    BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1568c8c42864SChris Mason 		int sret;
1569c8c42864SChris Mason 
1570bd681513SChris Mason 		if (*write_lock_level < level + 1) {
1571bd681513SChris Mason 			*write_lock_level = level + 1;
1572bd681513SChris Mason 			btrfs_release_path(p);
1573bd681513SChris Mason 			goto again;
1574bd681513SChris Mason 		}
1575bd681513SChris Mason 
1576c8c42864SChris Mason 		sret = reada_for_balance(root, p, level);
1577c8c42864SChris Mason 		if (sret)
1578c8c42864SChris Mason 			goto again;
1579c8c42864SChris Mason 
1580c8c42864SChris Mason 		btrfs_set_path_blocking(p);
1581c8c42864SChris Mason 		sret = split_node(trans, root, p, level);
1582bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
1583c8c42864SChris Mason 
1584c8c42864SChris Mason 		BUG_ON(sret > 0);
1585c8c42864SChris Mason 		if (sret) {
1586c8c42864SChris Mason 			ret = sret;
1587c8c42864SChris Mason 			goto done;
1588c8c42864SChris Mason 		}
1589c8c42864SChris Mason 		b = p->nodes[level];
1590c8c42864SChris Mason 	} else if (ins_len < 0 && btrfs_header_nritems(b) <
1591cfbb9308SChris Mason 		   BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
1592c8c42864SChris Mason 		int sret;
1593c8c42864SChris Mason 
1594bd681513SChris Mason 		if (*write_lock_level < level + 1) {
1595bd681513SChris Mason 			*write_lock_level = level + 1;
1596bd681513SChris Mason 			btrfs_release_path(p);
1597bd681513SChris Mason 			goto again;
1598bd681513SChris Mason 		}
1599bd681513SChris Mason 
1600c8c42864SChris Mason 		sret = reada_for_balance(root, p, level);
1601c8c42864SChris Mason 		if (sret)
1602c8c42864SChris Mason 			goto again;
1603c8c42864SChris Mason 
1604c8c42864SChris Mason 		btrfs_set_path_blocking(p);
1605c8c42864SChris Mason 		sret = balance_level(trans, root, p, level);
1606bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
1607c8c42864SChris Mason 
1608c8c42864SChris Mason 		if (sret) {
1609c8c42864SChris Mason 			ret = sret;
1610c8c42864SChris Mason 			goto done;
1611c8c42864SChris Mason 		}
1612c8c42864SChris Mason 		b = p->nodes[level];
1613c8c42864SChris Mason 		if (!b) {
1614b3b4aa74SDavid Sterba 			btrfs_release_path(p);
1615c8c42864SChris Mason 			goto again;
1616c8c42864SChris Mason 		}
1617c8c42864SChris Mason 		BUG_ON(btrfs_header_nritems(b) == 1);
1618c8c42864SChris Mason 	}
1619c8c42864SChris Mason 	return 0;
1620c8c42864SChris Mason 
1621c8c42864SChris Mason again:
1622c8c42864SChris Mason 	ret = -EAGAIN;
1623c8c42864SChris Mason done:
1624c8c42864SChris Mason 	return ret;
1625c8c42864SChris Mason }
1626c8c42864SChris Mason 
1627c8c42864SChris Mason /*
162874123bd7SChris Mason  * look for key in the tree.  path is filled in with nodes along the way
162974123bd7SChris Mason  * if key is found, we return zero and you can find the item in the leaf
163074123bd7SChris Mason  * level of the path (level 0)
163174123bd7SChris Mason  *
163274123bd7SChris Mason  * If the key isn't found, the path points to the slot where it should
1633aa5d6bedSChris Mason  * be inserted, and 1 is returned.  If there are other errors during the
1634aa5d6bedSChris Mason  * search a negative error number is returned.
163597571fd0SChris Mason  *
163697571fd0SChris Mason  * if ins_len > 0, nodes and leaves will be split as we walk down the
163797571fd0SChris Mason  * tree.  if ins_len < 0, nodes will be merged as we walk down the tree (if
163897571fd0SChris Mason  * possible)
163974123bd7SChris Mason  */
1640e089f05cSChris Mason int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1641e089f05cSChris Mason 		      *root, struct btrfs_key *key, struct btrfs_path *p, int
1642e089f05cSChris Mason 		      ins_len, int cow)
1643be0e5c09SChris Mason {
16445f39d397SChris Mason 	struct extent_buffer *b;
1645be0e5c09SChris Mason 	int slot;
1646be0e5c09SChris Mason 	int ret;
164733c66f43SYan Zheng 	int err;
1648be0e5c09SChris Mason 	int level;
1649925baeddSChris Mason 	int lowest_unlock = 1;
1650bd681513SChris Mason 	int root_lock;
1651bd681513SChris Mason 	/* everything at write_lock_level or lower must be write locked */
1652bd681513SChris Mason 	int write_lock_level = 0;
16539f3a7427SChris Mason 	u8 lowest_level = 0;
16549f3a7427SChris Mason 
16556702ed49SChris Mason 	lowest_level = p->lowest_level;
1656323ac95bSChris Mason 	WARN_ON(lowest_level && ins_len > 0);
165722b0ebdaSChris Mason 	WARN_ON(p->nodes[0] != NULL);
165825179201SJosef Bacik 
1659bd681513SChris Mason 	if (ins_len < 0) {
1660925baeddSChris Mason 		lowest_unlock = 2;
166165b51a00SChris Mason 
1662bd681513SChris Mason 		/* when we are removing items, we might have to go up to level
1663bd681513SChris Mason 		 * two as we update tree pointers  Make sure we keep write
1664bd681513SChris Mason 		 * for those levels as well
1665bd681513SChris Mason 		 */
1666bd681513SChris Mason 		write_lock_level = 2;
1667bd681513SChris Mason 	} else if (ins_len > 0) {
1668bd681513SChris Mason 		/*
1669bd681513SChris Mason 		 * for inserting items, make sure we have a write lock on
1670bd681513SChris Mason 		 * level 1 so we can update keys
1671bd681513SChris Mason 		 */
1672bd681513SChris Mason 		write_lock_level = 1;
1673bd681513SChris Mason 	}
1674bd681513SChris Mason 
1675bd681513SChris Mason 	if (!cow)
1676bd681513SChris Mason 		write_lock_level = -1;
1677bd681513SChris Mason 
1678bd681513SChris Mason 	if (cow && (p->keep_locks || p->lowest_level))
1679bd681513SChris Mason 		write_lock_level = BTRFS_MAX_LEVEL;
1680bd681513SChris Mason 
1681bb803951SChris Mason again:
1682bd681513SChris Mason 	/*
1683bd681513SChris Mason 	 * we try very hard to do read locks on the root
1684bd681513SChris Mason 	 */
1685bd681513SChris Mason 	root_lock = BTRFS_READ_LOCK;
1686bd681513SChris Mason 	level = 0;
16875d4f98a2SYan Zheng 	if (p->search_commit_root) {
1688bd681513SChris Mason 		/*
1689bd681513SChris Mason 		 * the commit roots are read only
1690bd681513SChris Mason 		 * so we always do read locks
1691bd681513SChris Mason 		 */
16925d4f98a2SYan Zheng 		b = root->commit_root;
16935d4f98a2SYan Zheng 		extent_buffer_get(b);
1694bd681513SChris Mason 		level = btrfs_header_level(b);
16955d4f98a2SYan Zheng 		if (!p->skip_locking)
1696bd681513SChris Mason 			btrfs_tree_read_lock(b);
16975d4f98a2SYan Zheng 	} else {
1698bd681513SChris Mason 		if (p->skip_locking) {
16995cd57b2cSChris Mason 			b = btrfs_root_node(root);
1700bd681513SChris Mason 			level = btrfs_header_level(b);
1701bd681513SChris Mason 		} else {
1702bd681513SChris Mason 			/* we don't know the level of the root node
1703bd681513SChris Mason 			 * until we actually have it read locked
1704bd681513SChris Mason 			 */
1705bd681513SChris Mason 			b = btrfs_read_lock_root_node(root);
1706bd681513SChris Mason 			level = btrfs_header_level(b);
1707bd681513SChris Mason 			if (level <= write_lock_level) {
1708bd681513SChris Mason 				/* whoops, must trade for write lock */
1709bd681513SChris Mason 				btrfs_tree_read_unlock(b);
1710bd681513SChris Mason 				free_extent_buffer(b);
1711925baeddSChris Mason 				b = btrfs_lock_root_node(root);
1712bd681513SChris Mason 				root_lock = BTRFS_WRITE_LOCK;
1713bd681513SChris Mason 
1714bd681513SChris Mason 				/* the level might have changed, check again */
1715bd681513SChris Mason 				level = btrfs_header_level(b);
17165d4f98a2SYan Zheng 			}
1717bd681513SChris Mason 		}
1718bd681513SChris Mason 	}
1719bd681513SChris Mason 	p->nodes[level] = b;
1720bd681513SChris Mason 	if (!p->skip_locking)
1721bd681513SChris Mason 		p->locks[level] = root_lock;
1722925baeddSChris Mason 
1723eb60ceacSChris Mason 	while (b) {
17245f39d397SChris Mason 		level = btrfs_header_level(b);
172565b51a00SChris Mason 
172665b51a00SChris Mason 		/*
172765b51a00SChris Mason 		 * setup the path here so we can release it under lock
172865b51a00SChris Mason 		 * contention with the cow code
172965b51a00SChris Mason 		 */
173002217ed2SChris Mason 		if (cow) {
1731c8c42864SChris Mason 			/*
1732c8c42864SChris Mason 			 * if we don't really need to cow this block
1733c8c42864SChris Mason 			 * then we don't want to set the path blocking,
1734c8c42864SChris Mason 			 * so we test it here
1735c8c42864SChris Mason 			 */
17365d4f98a2SYan Zheng 			if (!should_cow_block(trans, root, b))
173765b51a00SChris Mason 				goto cow_done;
17385d4f98a2SYan Zheng 
1739b4ce94deSChris Mason 			btrfs_set_path_blocking(p);
1740b4ce94deSChris Mason 
1741bd681513SChris Mason 			/*
1742bd681513SChris Mason 			 * must have write locks on this node and the
1743bd681513SChris Mason 			 * parent
1744bd681513SChris Mason 			 */
1745bd681513SChris Mason 			if (level + 1 > write_lock_level) {
1746bd681513SChris Mason 				write_lock_level = level + 1;
1747bd681513SChris Mason 				btrfs_release_path(p);
1748bd681513SChris Mason 				goto again;
1749bd681513SChris Mason 			}
1750bd681513SChris Mason 
175133c66f43SYan Zheng 			err = btrfs_cow_block(trans, root, b,
1752e20d96d6SChris Mason 					      p->nodes[level + 1],
17539fa8cfe7SChris Mason 					      p->slots[level + 1], &b);
175433c66f43SYan Zheng 			if (err) {
175533c66f43SYan Zheng 				ret = err;
175665b51a00SChris Mason 				goto done;
175754aa1f4dSChris Mason 			}
175802217ed2SChris Mason 		}
175965b51a00SChris Mason cow_done:
176002217ed2SChris Mason 		BUG_ON(!cow && ins_len);
176165b51a00SChris Mason 
1762eb60ceacSChris Mason 		p->nodes[level] = b;
1763bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
1764b4ce94deSChris Mason 
1765b4ce94deSChris Mason 		/*
1766b4ce94deSChris Mason 		 * we have a lock on b and as long as we aren't changing
1767b4ce94deSChris Mason 		 * the tree, there is no way to for the items in b to change.
1768b4ce94deSChris Mason 		 * It is safe to drop the lock on our parent before we
1769b4ce94deSChris Mason 		 * go through the expensive btree search on b.
1770b4ce94deSChris Mason 		 *
1771b4ce94deSChris Mason 		 * If cow is true, then we might be changing slot zero,
1772b4ce94deSChris Mason 		 * which may require changing the parent.  So, we can't
1773b4ce94deSChris Mason 		 * drop the lock until after we know which slot we're
1774b4ce94deSChris Mason 		 * operating on.
1775b4ce94deSChris Mason 		 */
1776b4ce94deSChris Mason 		if (!cow)
1777b4ce94deSChris Mason 			btrfs_unlock_up_safe(p, level + 1);
1778b4ce94deSChris Mason 
17795f39d397SChris Mason 		ret = bin_search(b, key, level, &slot);
1780b4ce94deSChris Mason 
17815f39d397SChris Mason 		if (level != 0) {
178233c66f43SYan Zheng 			int dec = 0;
178333c66f43SYan Zheng 			if (ret && slot > 0) {
178433c66f43SYan Zheng 				dec = 1;
1785be0e5c09SChris Mason 				slot -= 1;
178633c66f43SYan Zheng 			}
1787be0e5c09SChris Mason 			p->slots[level] = slot;
178833c66f43SYan Zheng 			err = setup_nodes_for_search(trans, root, p, b, level,
1789bd681513SChris Mason 					     ins_len, &write_lock_level);
179033c66f43SYan Zheng 			if (err == -EAGAIN)
1791b4ce94deSChris Mason 				goto again;
179233c66f43SYan Zheng 			if (err) {
179333c66f43SYan Zheng 				ret = err;
179465b51a00SChris Mason 				goto done;
179533c66f43SYan Zheng 			}
17965c680ed6SChris Mason 			b = p->nodes[level];
17975c680ed6SChris Mason 			slot = p->slots[level];
1798b4ce94deSChris Mason 
1799bd681513SChris Mason 			/*
1800bd681513SChris Mason 			 * slot 0 is special, if we change the key
1801bd681513SChris Mason 			 * we have to update the parent pointer
1802bd681513SChris Mason 			 * which means we must have a write lock
1803bd681513SChris Mason 			 * on the parent
1804bd681513SChris Mason 			 */
1805bd681513SChris Mason 			if (slot == 0 && cow &&
1806bd681513SChris Mason 			    write_lock_level < level + 1) {
1807bd681513SChris Mason 				write_lock_level = level + 1;
1808bd681513SChris Mason 				btrfs_release_path(p);
1809bd681513SChris Mason 				goto again;
1810bd681513SChris Mason 			}
1811bd681513SChris Mason 
1812f9efa9c7SChris Mason 			unlock_up(p, level, lowest_unlock);
1813f9efa9c7SChris Mason 
1814925baeddSChris Mason 			if (level == lowest_level) {
181533c66f43SYan Zheng 				if (dec)
181633c66f43SYan Zheng 					p->slots[level]++;
18175b21f2edSZheng Yan 				goto done;
1818925baeddSChris Mason 			}
1819ca7a79adSChris Mason 
182033c66f43SYan Zheng 			err = read_block_for_search(trans, root, p,
1821c8c42864SChris Mason 						    &b, level, slot, key);
182233c66f43SYan Zheng 			if (err == -EAGAIN)
1823051e1b9fSChris Mason 				goto again;
182433c66f43SYan Zheng 			if (err) {
182533c66f43SYan Zheng 				ret = err;
182676a05b35SChris Mason 				goto done;
182733c66f43SYan Zheng 			}
182876a05b35SChris Mason 
1829b4ce94deSChris Mason 			if (!p->skip_locking) {
1830bd681513SChris Mason 				level = btrfs_header_level(b);
1831bd681513SChris Mason 				if (level <= write_lock_level) {
1832bd681513SChris Mason 					err = btrfs_try_tree_write_lock(b);
183333c66f43SYan Zheng 					if (!err) {
1834b4ce94deSChris Mason 						btrfs_set_path_blocking(p);
1835925baeddSChris Mason 						btrfs_tree_lock(b);
1836bd681513SChris Mason 						btrfs_clear_path_blocking(p, b,
1837bd681513SChris Mason 								  BTRFS_WRITE_LOCK);
1838b4ce94deSChris Mason 					}
1839bd681513SChris Mason 					p->locks[level] = BTRFS_WRITE_LOCK;
1840bd681513SChris Mason 				} else {
1841bd681513SChris Mason 					err = btrfs_try_tree_read_lock(b);
1842bd681513SChris Mason 					if (!err) {
1843bd681513SChris Mason 						btrfs_set_path_blocking(p);
1844bd681513SChris Mason 						btrfs_tree_read_lock(b);
1845bd681513SChris Mason 						btrfs_clear_path_blocking(p, b,
1846bd681513SChris Mason 								  BTRFS_READ_LOCK);
1847bd681513SChris Mason 					}
1848bd681513SChris Mason 					p->locks[level] = BTRFS_READ_LOCK;
1849bd681513SChris Mason 				}
1850bd681513SChris Mason 				p->nodes[level] = b;
1851b4ce94deSChris Mason 			}
1852be0e5c09SChris Mason 		} else {
1853be0e5c09SChris Mason 			p->slots[level] = slot;
185487b29b20SYan Zheng 			if (ins_len > 0 &&
185587b29b20SYan Zheng 			    btrfs_leaf_free_space(root, b) < ins_len) {
1856bd681513SChris Mason 				if (write_lock_level < 1) {
1857bd681513SChris Mason 					write_lock_level = 1;
1858bd681513SChris Mason 					btrfs_release_path(p);
1859bd681513SChris Mason 					goto again;
1860bd681513SChris Mason 				}
1861bd681513SChris Mason 
1862b4ce94deSChris Mason 				btrfs_set_path_blocking(p);
186333c66f43SYan Zheng 				err = split_leaf(trans, root, key,
1864cc0c5538SChris Mason 						 p, ins_len, ret == 0);
1865bd681513SChris Mason 				btrfs_clear_path_blocking(p, NULL, 0);
1866b4ce94deSChris Mason 
186733c66f43SYan Zheng 				BUG_ON(err > 0);
186833c66f43SYan Zheng 				if (err) {
186933c66f43SYan Zheng 					ret = err;
187065b51a00SChris Mason 					goto done;
187165b51a00SChris Mason 				}
18725c680ed6SChris Mason 			}
1873459931ecSChris Mason 			if (!p->search_for_split)
1874925baeddSChris Mason 				unlock_up(p, level, lowest_unlock);
187565b51a00SChris Mason 			goto done;
187665b51a00SChris Mason 		}
187765b51a00SChris Mason 	}
187865b51a00SChris Mason 	ret = 1;
187965b51a00SChris Mason done:
1880b4ce94deSChris Mason 	/*
1881b4ce94deSChris Mason 	 * we don't really know what they plan on doing with the path
1882b4ce94deSChris Mason 	 * from here on, so for now just mark it as blocking
1883b4ce94deSChris Mason 	 */
1884b9473439SChris Mason 	if (!p->leave_spinning)
1885b4ce94deSChris Mason 		btrfs_set_path_blocking(p);
188676a05b35SChris Mason 	if (ret < 0)
1887b3b4aa74SDavid Sterba 		btrfs_release_path(p);
1888be0e5c09SChris Mason 	return ret;
1889be0e5c09SChris Mason }
1890be0e5c09SChris Mason 
189174123bd7SChris Mason /*
189274123bd7SChris Mason  * adjust the pointers going up the tree, starting at level
189374123bd7SChris Mason  * making sure the right key of each node is points to 'key'.
189474123bd7SChris Mason  * This is used after shifting pointers to the left, so it stops
189574123bd7SChris Mason  * fixing up pointers when a given leaf/node is not in slot 0 of the
189674123bd7SChris Mason  * higher levels
1897aa5d6bedSChris Mason  *
189874123bd7SChris Mason  */
1899143bede5SJeff Mahoney static void fixup_low_keys(struct btrfs_trans_handle *trans,
19005f39d397SChris Mason 			   struct btrfs_root *root, struct btrfs_path *path,
19015f39d397SChris Mason 			   struct btrfs_disk_key *key, int level)
1902be0e5c09SChris Mason {
1903be0e5c09SChris Mason 	int i;
19045f39d397SChris Mason 	struct extent_buffer *t;
19055f39d397SChris Mason 
1906234b63a0SChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1907be0e5c09SChris Mason 		int tslot = path->slots[i];
1908eb60ceacSChris Mason 		if (!path->nodes[i])
1909be0e5c09SChris Mason 			break;
19105f39d397SChris Mason 		t = path->nodes[i];
19115f39d397SChris Mason 		btrfs_set_node_key(t, key, tslot);
1912d6025579SChris Mason 		btrfs_mark_buffer_dirty(path->nodes[i]);
1913be0e5c09SChris Mason 		if (tslot != 0)
1914be0e5c09SChris Mason 			break;
1915be0e5c09SChris Mason 	}
1916be0e5c09SChris Mason }
1917be0e5c09SChris Mason 
191874123bd7SChris Mason /*
191931840ae1SZheng Yan  * update item key.
192031840ae1SZheng Yan  *
192131840ae1SZheng Yan  * This function isn't completely safe. It's the caller's responsibility
192231840ae1SZheng Yan  * that the new key won't break the order
192331840ae1SZheng Yan  */
1924143bede5SJeff Mahoney void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
192531840ae1SZheng Yan 			     struct btrfs_root *root, struct btrfs_path *path,
192631840ae1SZheng Yan 			     struct btrfs_key *new_key)
192731840ae1SZheng Yan {
192831840ae1SZheng Yan 	struct btrfs_disk_key disk_key;
192931840ae1SZheng Yan 	struct extent_buffer *eb;
193031840ae1SZheng Yan 	int slot;
193131840ae1SZheng Yan 
193231840ae1SZheng Yan 	eb = path->nodes[0];
193331840ae1SZheng Yan 	slot = path->slots[0];
193431840ae1SZheng Yan 	if (slot > 0) {
193531840ae1SZheng Yan 		btrfs_item_key(eb, &disk_key, slot - 1);
1936143bede5SJeff Mahoney 		BUG_ON(comp_keys(&disk_key, new_key) >= 0);
193731840ae1SZheng Yan 	}
193831840ae1SZheng Yan 	if (slot < btrfs_header_nritems(eb) - 1) {
193931840ae1SZheng Yan 		btrfs_item_key(eb, &disk_key, slot + 1);
1940143bede5SJeff Mahoney 		BUG_ON(comp_keys(&disk_key, new_key) <= 0);
194131840ae1SZheng Yan 	}
194231840ae1SZheng Yan 
194331840ae1SZheng Yan 	btrfs_cpu_key_to_disk(&disk_key, new_key);
194431840ae1SZheng Yan 	btrfs_set_item_key(eb, &disk_key, slot);
194531840ae1SZheng Yan 	btrfs_mark_buffer_dirty(eb);
194631840ae1SZheng Yan 	if (slot == 0)
194731840ae1SZheng Yan 		fixup_low_keys(trans, root, path, &disk_key, 1);
194831840ae1SZheng Yan }
194931840ae1SZheng Yan 
195031840ae1SZheng Yan /*
195174123bd7SChris Mason  * try to push data from one node into the next node left in the
195279f95c82SChris Mason  * tree.
1953aa5d6bedSChris Mason  *
1954aa5d6bedSChris Mason  * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1955aa5d6bedSChris Mason  * error, and > 0 if there was no room in the left hand block.
195674123bd7SChris Mason  */
195798ed5174SChris Mason static int push_node_left(struct btrfs_trans_handle *trans,
195898ed5174SChris Mason 			  struct btrfs_root *root, struct extent_buffer *dst,
1959971a1f66SChris Mason 			  struct extent_buffer *src, int empty)
1960be0e5c09SChris Mason {
1961be0e5c09SChris Mason 	int push_items = 0;
1962bb803951SChris Mason 	int src_nritems;
1963bb803951SChris Mason 	int dst_nritems;
1964aa5d6bedSChris Mason 	int ret = 0;
1965be0e5c09SChris Mason 
19665f39d397SChris Mason 	src_nritems = btrfs_header_nritems(src);
19675f39d397SChris Mason 	dst_nritems = btrfs_header_nritems(dst);
1968123abc88SChris Mason 	push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
19697bb86316SChris Mason 	WARN_ON(btrfs_header_generation(src) != trans->transid);
19707bb86316SChris Mason 	WARN_ON(btrfs_header_generation(dst) != trans->transid);
197154aa1f4dSChris Mason 
1972bce4eae9SChris Mason 	if (!empty && src_nritems <= 8)
1973971a1f66SChris Mason 		return 1;
1974971a1f66SChris Mason 
1975d397712bSChris Mason 	if (push_items <= 0)
1976be0e5c09SChris Mason 		return 1;
1977be0e5c09SChris Mason 
1978bce4eae9SChris Mason 	if (empty) {
1979971a1f66SChris Mason 		push_items = min(src_nritems, push_items);
1980bce4eae9SChris Mason 		if (push_items < src_nritems) {
1981bce4eae9SChris Mason 			/* leave at least 8 pointers in the node if
1982bce4eae9SChris Mason 			 * we aren't going to empty it
1983bce4eae9SChris Mason 			 */
1984bce4eae9SChris Mason 			if (src_nritems - push_items < 8) {
1985bce4eae9SChris Mason 				if (push_items <= 8)
1986bce4eae9SChris Mason 					return 1;
1987bce4eae9SChris Mason 				push_items -= 8;
1988bce4eae9SChris Mason 			}
1989bce4eae9SChris Mason 		}
1990bce4eae9SChris Mason 	} else
1991bce4eae9SChris Mason 		push_items = min(src_nritems - 8, push_items);
199279f95c82SChris Mason 
19935f39d397SChris Mason 	copy_extent_buffer(dst, src,
19945f39d397SChris Mason 			   btrfs_node_key_ptr_offset(dst_nritems),
19955f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
1996123abc88SChris Mason 			   push_items * sizeof(struct btrfs_key_ptr));
19975f39d397SChris Mason 
1998bb803951SChris Mason 	if (push_items < src_nritems) {
19995f39d397SChris Mason 		memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
20005f39d397SChris Mason 				      btrfs_node_key_ptr_offset(push_items),
2001e2fa7227SChris Mason 				      (src_nritems - push_items) *
2002123abc88SChris Mason 				      sizeof(struct btrfs_key_ptr));
2003bb803951SChris Mason 	}
20045f39d397SChris Mason 	btrfs_set_header_nritems(src, src_nritems - push_items);
20055f39d397SChris Mason 	btrfs_set_header_nritems(dst, dst_nritems + push_items);
20065f39d397SChris Mason 	btrfs_mark_buffer_dirty(src);
20075f39d397SChris Mason 	btrfs_mark_buffer_dirty(dst);
200831840ae1SZheng Yan 
2009bb803951SChris Mason 	return ret;
2010be0e5c09SChris Mason }
2011be0e5c09SChris Mason 
201297571fd0SChris Mason /*
201379f95c82SChris Mason  * try to push data from one node into the next node right in the
201479f95c82SChris Mason  * tree.
201579f95c82SChris Mason  *
201679f95c82SChris Mason  * returns 0 if some ptrs were pushed, < 0 if there was some horrible
201779f95c82SChris Mason  * error, and > 0 if there was no room in the right hand block.
201879f95c82SChris Mason  *
201979f95c82SChris Mason  * this will  only push up to 1/2 the contents of the left node over
202079f95c82SChris Mason  */
20215f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans,
20225f39d397SChris Mason 			      struct btrfs_root *root,
20235f39d397SChris Mason 			      struct extent_buffer *dst,
20245f39d397SChris Mason 			      struct extent_buffer *src)
202579f95c82SChris Mason {
202679f95c82SChris Mason 	int push_items = 0;
202779f95c82SChris Mason 	int max_push;
202879f95c82SChris Mason 	int src_nritems;
202979f95c82SChris Mason 	int dst_nritems;
203079f95c82SChris Mason 	int ret = 0;
203179f95c82SChris Mason 
20327bb86316SChris Mason 	WARN_ON(btrfs_header_generation(src) != trans->transid);
20337bb86316SChris Mason 	WARN_ON(btrfs_header_generation(dst) != trans->transid);
20347bb86316SChris Mason 
20355f39d397SChris Mason 	src_nritems = btrfs_header_nritems(src);
20365f39d397SChris Mason 	dst_nritems = btrfs_header_nritems(dst);
2037123abc88SChris Mason 	push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
2038d397712bSChris Mason 	if (push_items <= 0)
203979f95c82SChris Mason 		return 1;
2040bce4eae9SChris Mason 
2041d397712bSChris Mason 	if (src_nritems < 4)
2042bce4eae9SChris Mason 		return 1;
204379f95c82SChris Mason 
204479f95c82SChris Mason 	max_push = src_nritems / 2 + 1;
204579f95c82SChris Mason 	/* don't try to empty the node */
2046d397712bSChris Mason 	if (max_push >= src_nritems)
204779f95c82SChris Mason 		return 1;
2048252c38f0SYan 
204979f95c82SChris Mason 	if (max_push < push_items)
205079f95c82SChris Mason 		push_items = max_push;
205179f95c82SChris Mason 
20525f39d397SChris Mason 	memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
20535f39d397SChris Mason 				      btrfs_node_key_ptr_offset(0),
20545f39d397SChris Mason 				      (dst_nritems) *
20555f39d397SChris Mason 				      sizeof(struct btrfs_key_ptr));
2056d6025579SChris Mason 
20575f39d397SChris Mason 	copy_extent_buffer(dst, src,
20585f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
20595f39d397SChris Mason 			   btrfs_node_key_ptr_offset(src_nritems - push_items),
2060123abc88SChris Mason 			   push_items * sizeof(struct btrfs_key_ptr));
206179f95c82SChris Mason 
20625f39d397SChris Mason 	btrfs_set_header_nritems(src, src_nritems - push_items);
20635f39d397SChris Mason 	btrfs_set_header_nritems(dst, dst_nritems + push_items);
206479f95c82SChris Mason 
20655f39d397SChris Mason 	btrfs_mark_buffer_dirty(src);
20665f39d397SChris Mason 	btrfs_mark_buffer_dirty(dst);
206731840ae1SZheng Yan 
206879f95c82SChris Mason 	return ret;
206979f95c82SChris Mason }
207079f95c82SChris Mason 
207179f95c82SChris Mason /*
207297571fd0SChris Mason  * helper function to insert a new root level in the tree.
207397571fd0SChris Mason  * A new node is allocated, and a single item is inserted to
207497571fd0SChris Mason  * point to the existing root
2075aa5d6bedSChris Mason  *
2076aa5d6bedSChris Mason  * returns zero on success or < 0 on failure.
207797571fd0SChris Mason  */
2078d397712bSChris Mason static noinline int insert_new_root(struct btrfs_trans_handle *trans,
20795f39d397SChris Mason 			   struct btrfs_root *root,
20805f39d397SChris Mason 			   struct btrfs_path *path, int level)
208174123bd7SChris Mason {
20827bb86316SChris Mason 	u64 lower_gen;
20835f39d397SChris Mason 	struct extent_buffer *lower;
20845f39d397SChris Mason 	struct extent_buffer *c;
2085925baeddSChris Mason 	struct extent_buffer *old;
20865f39d397SChris Mason 	struct btrfs_disk_key lower_key;
20875c680ed6SChris Mason 
20885c680ed6SChris Mason 	BUG_ON(path->nodes[level]);
20895c680ed6SChris Mason 	BUG_ON(path->nodes[level-1] != root->node);
20905c680ed6SChris Mason 
20917bb86316SChris Mason 	lower = path->nodes[level-1];
20927bb86316SChris Mason 	if (level == 1)
20937bb86316SChris Mason 		btrfs_item_key(lower, &lower_key, 0);
20947bb86316SChris Mason 	else
20957bb86316SChris Mason 		btrfs_node_key(lower, &lower_key, 0);
20967bb86316SChris Mason 
209731840ae1SZheng Yan 	c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
20985d4f98a2SYan Zheng 				   root->root_key.objectid, &lower_key,
209966d7e7f0SArne Jansen 				   level, root->node->start, 0, 0);
21005f39d397SChris Mason 	if (IS_ERR(c))
21015f39d397SChris Mason 		return PTR_ERR(c);
2102925baeddSChris Mason 
2103f0486c68SYan, Zheng 	root_add_used(root, root->nodesize);
2104f0486c68SYan, Zheng 
21055d4f98a2SYan Zheng 	memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
21065f39d397SChris Mason 	btrfs_set_header_nritems(c, 1);
21075f39d397SChris Mason 	btrfs_set_header_level(c, level);
2108db94535dSChris Mason 	btrfs_set_header_bytenr(c, c->start);
21095f39d397SChris Mason 	btrfs_set_header_generation(c, trans->transid);
21105d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
21115f39d397SChris Mason 	btrfs_set_header_owner(c, root->root_key.objectid);
2112d5719762SChris Mason 
21135f39d397SChris Mason 	write_extent_buffer(c, root->fs_info->fsid,
21145f39d397SChris Mason 			    (unsigned long)btrfs_header_fsid(c),
21155f39d397SChris Mason 			    BTRFS_FSID_SIZE);
2116e17cade2SChris Mason 
2117e17cade2SChris Mason 	write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2118e17cade2SChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(c),
2119e17cade2SChris Mason 			    BTRFS_UUID_SIZE);
2120e17cade2SChris Mason 
21215f39d397SChris Mason 	btrfs_set_node_key(c, &lower_key, 0);
2122db94535dSChris Mason 	btrfs_set_node_blockptr(c, 0, lower->start);
21237bb86316SChris Mason 	lower_gen = btrfs_header_generation(lower);
212431840ae1SZheng Yan 	WARN_ON(lower_gen != trans->transid);
21257bb86316SChris Mason 
21267bb86316SChris Mason 	btrfs_set_node_ptr_generation(c, 0, lower_gen);
21275f39d397SChris Mason 
21285f39d397SChris Mason 	btrfs_mark_buffer_dirty(c);
2129d5719762SChris Mason 
2130925baeddSChris Mason 	old = root->node;
2131240f62c8SChris Mason 	rcu_assign_pointer(root->node, c);
2132925baeddSChris Mason 
2133925baeddSChris Mason 	/* the super has an extra ref to root->node */
2134925baeddSChris Mason 	free_extent_buffer(old);
2135925baeddSChris Mason 
21360b86a832SChris Mason 	add_root_to_dirty_list(root);
21375f39d397SChris Mason 	extent_buffer_get(c);
21385f39d397SChris Mason 	path->nodes[level] = c;
2139bd681513SChris Mason 	path->locks[level] = BTRFS_WRITE_LOCK;
214074123bd7SChris Mason 	path->slots[level] = 0;
214174123bd7SChris Mason 	return 0;
214274123bd7SChris Mason }
21435c680ed6SChris Mason 
21445c680ed6SChris Mason /*
21455c680ed6SChris Mason  * worker function to insert a single pointer in a node.
21465c680ed6SChris Mason  * the node should have enough room for the pointer already
214797571fd0SChris Mason  *
21485c680ed6SChris Mason  * slot and level indicate where you want the key to go, and
21495c680ed6SChris Mason  * blocknr is the block the key points to.
21505c680ed6SChris Mason  */
2151143bede5SJeff Mahoney static void insert_ptr(struct btrfs_trans_handle *trans,
2152143bede5SJeff Mahoney 		       struct btrfs_root *root, struct btrfs_path *path,
2153143bede5SJeff Mahoney 		       struct btrfs_disk_key *key, u64 bytenr,
2154143bede5SJeff Mahoney 		       int slot, int level)
21555c680ed6SChris Mason {
21565f39d397SChris Mason 	struct extent_buffer *lower;
21575c680ed6SChris Mason 	int nritems;
21585c680ed6SChris Mason 
21595c680ed6SChris Mason 	BUG_ON(!path->nodes[level]);
2160f0486c68SYan, Zheng 	btrfs_assert_tree_locked(path->nodes[level]);
21615f39d397SChris Mason 	lower = path->nodes[level];
21625f39d397SChris Mason 	nritems = btrfs_header_nritems(lower);
2163c293498bSStoyan Gaydarov 	BUG_ON(slot > nritems);
2164143bede5SJeff Mahoney 	BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
216574123bd7SChris Mason 	if (slot != nritems) {
21665f39d397SChris Mason 		memmove_extent_buffer(lower,
21675f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot + 1),
21685f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot),
2169123abc88SChris Mason 			      (nritems - slot) * sizeof(struct btrfs_key_ptr));
217074123bd7SChris Mason 	}
21715f39d397SChris Mason 	btrfs_set_node_key(lower, key, slot);
2172db94535dSChris Mason 	btrfs_set_node_blockptr(lower, slot, bytenr);
217374493f7aSChris Mason 	WARN_ON(trans->transid == 0);
217474493f7aSChris Mason 	btrfs_set_node_ptr_generation(lower, slot, trans->transid);
21755f39d397SChris Mason 	btrfs_set_header_nritems(lower, nritems + 1);
21765f39d397SChris Mason 	btrfs_mark_buffer_dirty(lower);
217774123bd7SChris Mason }
217874123bd7SChris Mason 
217997571fd0SChris Mason /*
218097571fd0SChris Mason  * split the node at the specified level in path in two.
218197571fd0SChris Mason  * The path is corrected to point to the appropriate node after the split
218297571fd0SChris Mason  *
218397571fd0SChris Mason  * Before splitting this tries to make some room in the node by pushing
218497571fd0SChris Mason  * left and right, if either one works, it returns right away.
2185aa5d6bedSChris Mason  *
2186aa5d6bedSChris Mason  * returns 0 on success and < 0 on failure
218797571fd0SChris Mason  */
2188e02119d5SChris Mason static noinline int split_node(struct btrfs_trans_handle *trans,
2189e02119d5SChris Mason 			       struct btrfs_root *root,
2190e02119d5SChris Mason 			       struct btrfs_path *path, int level)
2191be0e5c09SChris Mason {
21925f39d397SChris Mason 	struct extent_buffer *c;
21935f39d397SChris Mason 	struct extent_buffer *split;
21945f39d397SChris Mason 	struct btrfs_disk_key disk_key;
2195be0e5c09SChris Mason 	int mid;
21965c680ed6SChris Mason 	int ret;
21977518a238SChris Mason 	u32 c_nritems;
2198be0e5c09SChris Mason 
21995f39d397SChris Mason 	c = path->nodes[level];
22007bb86316SChris Mason 	WARN_ON(btrfs_header_generation(c) != trans->transid);
22015f39d397SChris Mason 	if (c == root->node) {
22025c680ed6SChris Mason 		/* trying to split the root, lets make a new one */
2203e089f05cSChris Mason 		ret = insert_new_root(trans, root, path, level + 1);
22045c680ed6SChris Mason 		if (ret)
22055c680ed6SChris Mason 			return ret;
2206b3612421SChris Mason 	} else {
2207e66f709bSChris Mason 		ret = push_nodes_for_insert(trans, root, path, level);
22085f39d397SChris Mason 		c = path->nodes[level];
22095f39d397SChris Mason 		if (!ret && btrfs_header_nritems(c) <
2210c448acf0SChris Mason 		    BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
2211e66f709bSChris Mason 			return 0;
221254aa1f4dSChris Mason 		if (ret < 0)
221354aa1f4dSChris Mason 			return ret;
22145c680ed6SChris Mason 	}
2215e66f709bSChris Mason 
22165f39d397SChris Mason 	c_nritems = btrfs_header_nritems(c);
22175d4f98a2SYan Zheng 	mid = (c_nritems + 1) / 2;
22185d4f98a2SYan Zheng 	btrfs_node_key(c, &disk_key, mid);
22197bb86316SChris Mason 
22205d4f98a2SYan Zheng 	split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
22217bb86316SChris Mason 					root->root_key.objectid,
222266d7e7f0SArne Jansen 					&disk_key, level, c->start, 0, 0);
22235f39d397SChris Mason 	if (IS_ERR(split))
22245f39d397SChris Mason 		return PTR_ERR(split);
222554aa1f4dSChris Mason 
2226f0486c68SYan, Zheng 	root_add_used(root, root->nodesize);
2227f0486c68SYan, Zheng 
22285d4f98a2SYan Zheng 	memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
22295f39d397SChris Mason 	btrfs_set_header_level(split, btrfs_header_level(c));
2230db94535dSChris Mason 	btrfs_set_header_bytenr(split, split->start);
22315f39d397SChris Mason 	btrfs_set_header_generation(split, trans->transid);
22325d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
22335f39d397SChris Mason 	btrfs_set_header_owner(split, root->root_key.objectid);
22345f39d397SChris Mason 	write_extent_buffer(split, root->fs_info->fsid,
22355f39d397SChris Mason 			    (unsigned long)btrfs_header_fsid(split),
22365f39d397SChris Mason 			    BTRFS_FSID_SIZE);
2237e17cade2SChris Mason 	write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2238e17cade2SChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(split),
2239e17cade2SChris Mason 			    BTRFS_UUID_SIZE);
22405f39d397SChris Mason 
22415f39d397SChris Mason 
22425f39d397SChris Mason 	copy_extent_buffer(split, c,
22435f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
22445f39d397SChris Mason 			   btrfs_node_key_ptr_offset(mid),
2245123abc88SChris Mason 			   (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
22465f39d397SChris Mason 	btrfs_set_header_nritems(split, c_nritems - mid);
22475f39d397SChris Mason 	btrfs_set_header_nritems(c, mid);
2248aa5d6bedSChris Mason 	ret = 0;
2249aa5d6bedSChris Mason 
22505f39d397SChris Mason 	btrfs_mark_buffer_dirty(c);
22515f39d397SChris Mason 	btrfs_mark_buffer_dirty(split);
22525f39d397SChris Mason 
2253143bede5SJeff Mahoney 	insert_ptr(trans, root, path, &disk_key, split->start,
2254143bede5SJeff Mahoney 		   path->slots[level + 1] + 1, level + 1);
2255aa5d6bedSChris Mason 
22565de08d7dSChris Mason 	if (path->slots[level] >= mid) {
22575c680ed6SChris Mason 		path->slots[level] -= mid;
2258925baeddSChris Mason 		btrfs_tree_unlock(c);
22595f39d397SChris Mason 		free_extent_buffer(c);
22605f39d397SChris Mason 		path->nodes[level] = split;
22615c680ed6SChris Mason 		path->slots[level + 1] += 1;
2262eb60ceacSChris Mason 	} else {
2263925baeddSChris Mason 		btrfs_tree_unlock(split);
22645f39d397SChris Mason 		free_extent_buffer(split);
2265be0e5c09SChris Mason 	}
2266aa5d6bedSChris Mason 	return ret;
2267be0e5c09SChris Mason }
2268be0e5c09SChris Mason 
226974123bd7SChris Mason /*
227074123bd7SChris Mason  * how many bytes are required to store the items in a leaf.  start
227174123bd7SChris Mason  * and nr indicate which items in the leaf to check.  This totals up the
227274123bd7SChris Mason  * space used both by the item structs and the item data
227374123bd7SChris Mason  */
22745f39d397SChris Mason static int leaf_space_used(struct extent_buffer *l, int start, int nr)
2275be0e5c09SChris Mason {
2276be0e5c09SChris Mason 	int data_len;
22775f39d397SChris Mason 	int nritems = btrfs_header_nritems(l);
2278d4dbff95SChris Mason 	int end = min(nritems, start + nr) - 1;
2279be0e5c09SChris Mason 
2280be0e5c09SChris Mason 	if (!nr)
2281be0e5c09SChris Mason 		return 0;
22825f39d397SChris Mason 	data_len = btrfs_item_end_nr(l, start);
22835f39d397SChris Mason 	data_len = data_len - btrfs_item_offset_nr(l, end);
22840783fcfcSChris Mason 	data_len += sizeof(struct btrfs_item) * nr;
2285d4dbff95SChris Mason 	WARN_ON(data_len < 0);
2286be0e5c09SChris Mason 	return data_len;
2287be0e5c09SChris Mason }
2288be0e5c09SChris Mason 
228974123bd7SChris Mason /*
2290d4dbff95SChris Mason  * The space between the end of the leaf items and
2291d4dbff95SChris Mason  * the start of the leaf data.  IOW, how much room
2292d4dbff95SChris Mason  * the leaf has left for both items and data
2293d4dbff95SChris Mason  */
2294d397712bSChris Mason noinline int btrfs_leaf_free_space(struct btrfs_root *root,
2295e02119d5SChris Mason 				   struct extent_buffer *leaf)
2296d4dbff95SChris Mason {
22975f39d397SChris Mason 	int nritems = btrfs_header_nritems(leaf);
22985f39d397SChris Mason 	int ret;
22995f39d397SChris Mason 	ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
23005f39d397SChris Mason 	if (ret < 0) {
2301d397712bSChris Mason 		printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2302d397712bSChris Mason 		       "used %d nritems %d\n",
2303ae2f5411SJens Axboe 		       ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
23045f39d397SChris Mason 		       leaf_space_used(leaf, 0, nritems), nritems);
23055f39d397SChris Mason 	}
23065f39d397SChris Mason 	return ret;
2307d4dbff95SChris Mason }
2308d4dbff95SChris Mason 
230999d8f83cSChris Mason /*
231099d8f83cSChris Mason  * min slot controls the lowest index we're willing to push to the
231199d8f83cSChris Mason  * right.  We'll push up to and including min_slot, but no lower
231299d8f83cSChris Mason  */
231344871b1bSChris Mason static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
231444871b1bSChris Mason 				      struct btrfs_root *root,
231544871b1bSChris Mason 				      struct btrfs_path *path,
231644871b1bSChris Mason 				      int data_size, int empty,
231744871b1bSChris Mason 				      struct extent_buffer *right,
231899d8f83cSChris Mason 				      int free_space, u32 left_nritems,
231999d8f83cSChris Mason 				      u32 min_slot)
232000ec4c51SChris Mason {
23215f39d397SChris Mason 	struct extent_buffer *left = path->nodes[0];
232244871b1bSChris Mason 	struct extent_buffer *upper = path->nodes[1];
23235f39d397SChris Mason 	struct btrfs_disk_key disk_key;
232400ec4c51SChris Mason 	int slot;
232534a38218SChris Mason 	u32 i;
232600ec4c51SChris Mason 	int push_space = 0;
232700ec4c51SChris Mason 	int push_items = 0;
23280783fcfcSChris Mason 	struct btrfs_item *item;
232934a38218SChris Mason 	u32 nr;
23307518a238SChris Mason 	u32 right_nritems;
23315f39d397SChris Mason 	u32 data_end;
2332db94535dSChris Mason 	u32 this_item_size;
233300ec4c51SChris Mason 
233434a38218SChris Mason 	if (empty)
233534a38218SChris Mason 		nr = 0;
233634a38218SChris Mason 	else
233799d8f83cSChris Mason 		nr = max_t(u32, 1, min_slot);
233834a38218SChris Mason 
233931840ae1SZheng Yan 	if (path->slots[0] >= left_nritems)
234087b29b20SYan Zheng 		push_space += data_size;
234131840ae1SZheng Yan 
234244871b1bSChris Mason 	slot = path->slots[1];
234334a38218SChris Mason 	i = left_nritems - 1;
234434a38218SChris Mason 	while (i >= nr) {
23455f39d397SChris Mason 		item = btrfs_item_nr(left, i);
2346db94535dSChris Mason 
234731840ae1SZheng Yan 		if (!empty && push_items > 0) {
234831840ae1SZheng Yan 			if (path->slots[0] > i)
234931840ae1SZheng Yan 				break;
235031840ae1SZheng Yan 			if (path->slots[0] == i) {
235131840ae1SZheng Yan 				int space = btrfs_leaf_free_space(root, left);
235231840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
235331840ae1SZheng Yan 					break;
235431840ae1SZheng Yan 			}
235531840ae1SZheng Yan 		}
235631840ae1SZheng Yan 
235700ec4c51SChris Mason 		if (path->slots[0] == i)
235887b29b20SYan Zheng 			push_space += data_size;
2359db94535dSChris Mason 
2360db94535dSChris Mason 		this_item_size = btrfs_item_size(left, item);
2361db94535dSChris Mason 		if (this_item_size + sizeof(*item) + push_space > free_space)
236200ec4c51SChris Mason 			break;
236331840ae1SZheng Yan 
236400ec4c51SChris Mason 		push_items++;
2365db94535dSChris Mason 		push_space += this_item_size + sizeof(*item);
236634a38218SChris Mason 		if (i == 0)
236734a38218SChris Mason 			break;
236834a38218SChris Mason 		i--;
2369db94535dSChris Mason 	}
23705f39d397SChris Mason 
2371925baeddSChris Mason 	if (push_items == 0)
2372925baeddSChris Mason 		goto out_unlock;
23735f39d397SChris Mason 
237434a38218SChris Mason 	if (!empty && push_items == left_nritems)
2375a429e513SChris Mason 		WARN_ON(1);
23765f39d397SChris Mason 
237700ec4c51SChris Mason 	/* push left to right */
23785f39d397SChris Mason 	right_nritems = btrfs_header_nritems(right);
237934a38218SChris Mason 
23805f39d397SChris Mason 	push_space = btrfs_item_end_nr(left, left_nritems - push_items);
2381123abc88SChris Mason 	push_space -= leaf_data_end(root, left);
23825f39d397SChris Mason 
238300ec4c51SChris Mason 	/* make room in the right data area */
23845f39d397SChris Mason 	data_end = leaf_data_end(root, right);
23855f39d397SChris Mason 	memmove_extent_buffer(right,
23865f39d397SChris Mason 			      btrfs_leaf_data(right) + data_end - push_space,
23875f39d397SChris Mason 			      btrfs_leaf_data(right) + data_end,
23885f39d397SChris Mason 			      BTRFS_LEAF_DATA_SIZE(root) - data_end);
23895f39d397SChris Mason 
239000ec4c51SChris Mason 	/* copy from the left data area */
23915f39d397SChris Mason 	copy_extent_buffer(right, left, btrfs_leaf_data(right) +
2392d6025579SChris Mason 		     BTRFS_LEAF_DATA_SIZE(root) - push_space,
2393d6025579SChris Mason 		     btrfs_leaf_data(left) + leaf_data_end(root, left),
2394d6025579SChris Mason 		     push_space);
23955f39d397SChris Mason 
23965f39d397SChris Mason 	memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
23975f39d397SChris Mason 			      btrfs_item_nr_offset(0),
23980783fcfcSChris Mason 			      right_nritems * sizeof(struct btrfs_item));
23995f39d397SChris Mason 
240000ec4c51SChris Mason 	/* copy the items from left to right */
24015f39d397SChris Mason 	copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
24025f39d397SChris Mason 		   btrfs_item_nr_offset(left_nritems - push_items),
24030783fcfcSChris Mason 		   push_items * sizeof(struct btrfs_item));
240400ec4c51SChris Mason 
240500ec4c51SChris Mason 	/* update the item pointers */
24067518a238SChris Mason 	right_nritems += push_items;
24075f39d397SChris Mason 	btrfs_set_header_nritems(right, right_nritems);
2408123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root);
24097518a238SChris Mason 	for (i = 0; i < right_nritems; i++) {
24105f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2411db94535dSChris Mason 		push_space -= btrfs_item_size(right, item);
2412db94535dSChris Mason 		btrfs_set_item_offset(right, item, push_space);
2413db94535dSChris Mason 	}
2414db94535dSChris Mason 
24157518a238SChris Mason 	left_nritems -= push_items;
24165f39d397SChris Mason 	btrfs_set_header_nritems(left, left_nritems);
241700ec4c51SChris Mason 
241834a38218SChris Mason 	if (left_nritems)
24195f39d397SChris Mason 		btrfs_mark_buffer_dirty(left);
2420f0486c68SYan, Zheng 	else
2421f0486c68SYan, Zheng 		clean_tree_block(trans, root, left);
2422f0486c68SYan, Zheng 
24235f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
2424a429e513SChris Mason 
24255f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
24265f39d397SChris Mason 	btrfs_set_node_key(upper, &disk_key, slot + 1);
2427d6025579SChris Mason 	btrfs_mark_buffer_dirty(upper);
242802217ed2SChris Mason 
242900ec4c51SChris Mason 	/* then fixup the leaf pointer in the path */
24307518a238SChris Mason 	if (path->slots[0] >= left_nritems) {
24317518a238SChris Mason 		path->slots[0] -= left_nritems;
2432925baeddSChris Mason 		if (btrfs_header_nritems(path->nodes[0]) == 0)
2433925baeddSChris Mason 			clean_tree_block(trans, root, path->nodes[0]);
2434925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
24355f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
24365f39d397SChris Mason 		path->nodes[0] = right;
243700ec4c51SChris Mason 		path->slots[1] += 1;
243800ec4c51SChris Mason 	} else {
2439925baeddSChris Mason 		btrfs_tree_unlock(right);
24405f39d397SChris Mason 		free_extent_buffer(right);
244100ec4c51SChris Mason 	}
244200ec4c51SChris Mason 	return 0;
2443925baeddSChris Mason 
2444925baeddSChris Mason out_unlock:
2445925baeddSChris Mason 	btrfs_tree_unlock(right);
2446925baeddSChris Mason 	free_extent_buffer(right);
2447925baeddSChris Mason 	return 1;
244800ec4c51SChris Mason }
2449925baeddSChris Mason 
245000ec4c51SChris Mason /*
245144871b1bSChris Mason  * push some data in the path leaf to the right, trying to free up at
245274123bd7SChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
245344871b1bSChris Mason  *
245444871b1bSChris Mason  * returns 1 if the push failed because the other node didn't have enough
245544871b1bSChris Mason  * room, 0 if everything worked out and < 0 if there were major errors.
245699d8f83cSChris Mason  *
245799d8f83cSChris Mason  * this will push starting from min_slot to the end of the leaf.  It won't
245899d8f83cSChris Mason  * push any slot lower than min_slot
245974123bd7SChris Mason  */
246044871b1bSChris Mason static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
246199d8f83cSChris Mason 			   *root, struct btrfs_path *path,
246299d8f83cSChris Mason 			   int min_data_size, int data_size,
246399d8f83cSChris Mason 			   int empty, u32 min_slot)
2464be0e5c09SChris Mason {
246544871b1bSChris Mason 	struct extent_buffer *left = path->nodes[0];
246644871b1bSChris Mason 	struct extent_buffer *right;
246744871b1bSChris Mason 	struct extent_buffer *upper;
246844871b1bSChris Mason 	int slot;
246944871b1bSChris Mason 	int free_space;
247044871b1bSChris Mason 	u32 left_nritems;
247144871b1bSChris Mason 	int ret;
247244871b1bSChris Mason 
247344871b1bSChris Mason 	if (!path->nodes[1])
247444871b1bSChris Mason 		return 1;
247544871b1bSChris Mason 
247644871b1bSChris Mason 	slot = path->slots[1];
247744871b1bSChris Mason 	upper = path->nodes[1];
247844871b1bSChris Mason 	if (slot >= btrfs_header_nritems(upper) - 1)
247944871b1bSChris Mason 		return 1;
248044871b1bSChris Mason 
248144871b1bSChris Mason 	btrfs_assert_tree_locked(path->nodes[1]);
248244871b1bSChris Mason 
248344871b1bSChris Mason 	right = read_node_slot(root, upper, slot + 1);
248491ca338dSTsutomu Itoh 	if (right == NULL)
248591ca338dSTsutomu Itoh 		return 1;
248691ca338dSTsutomu Itoh 
248744871b1bSChris Mason 	btrfs_tree_lock(right);
248844871b1bSChris Mason 	btrfs_set_lock_blocking(right);
248944871b1bSChris Mason 
249044871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, right);
249144871b1bSChris Mason 	if (free_space < data_size)
249244871b1bSChris Mason 		goto out_unlock;
249344871b1bSChris Mason 
249444871b1bSChris Mason 	/* cow and double check */
249544871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, right, upper,
249644871b1bSChris Mason 			      slot + 1, &right);
249744871b1bSChris Mason 	if (ret)
249844871b1bSChris Mason 		goto out_unlock;
249944871b1bSChris Mason 
250044871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, right);
250144871b1bSChris Mason 	if (free_space < data_size)
250244871b1bSChris Mason 		goto out_unlock;
250344871b1bSChris Mason 
250444871b1bSChris Mason 	left_nritems = btrfs_header_nritems(left);
250544871b1bSChris Mason 	if (left_nritems == 0)
250644871b1bSChris Mason 		goto out_unlock;
250744871b1bSChris Mason 
250899d8f83cSChris Mason 	return __push_leaf_right(trans, root, path, min_data_size, empty,
250999d8f83cSChris Mason 				right, free_space, left_nritems, min_slot);
251044871b1bSChris Mason out_unlock:
251144871b1bSChris Mason 	btrfs_tree_unlock(right);
251244871b1bSChris Mason 	free_extent_buffer(right);
251344871b1bSChris Mason 	return 1;
251444871b1bSChris Mason }
251544871b1bSChris Mason 
251644871b1bSChris Mason /*
251744871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
251844871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
251999d8f83cSChris Mason  *
252099d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
252199d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us do all the
252299d8f83cSChris Mason  * items
252344871b1bSChris Mason  */
252444871b1bSChris Mason static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
252544871b1bSChris Mason 				     struct btrfs_root *root,
252644871b1bSChris Mason 				     struct btrfs_path *path, int data_size,
252744871b1bSChris Mason 				     int empty, struct extent_buffer *left,
252899d8f83cSChris Mason 				     int free_space, u32 right_nritems,
252999d8f83cSChris Mason 				     u32 max_slot)
253044871b1bSChris Mason {
25315f39d397SChris Mason 	struct btrfs_disk_key disk_key;
25325f39d397SChris Mason 	struct extent_buffer *right = path->nodes[0];
2533be0e5c09SChris Mason 	int i;
2534be0e5c09SChris Mason 	int push_space = 0;
2535be0e5c09SChris Mason 	int push_items = 0;
25360783fcfcSChris Mason 	struct btrfs_item *item;
25377518a238SChris Mason 	u32 old_left_nritems;
253834a38218SChris Mason 	u32 nr;
2539aa5d6bedSChris Mason 	int ret = 0;
2540db94535dSChris Mason 	u32 this_item_size;
2541db94535dSChris Mason 	u32 old_left_item_size;
2542be0e5c09SChris Mason 
254334a38218SChris Mason 	if (empty)
254499d8f83cSChris Mason 		nr = min(right_nritems, max_slot);
254534a38218SChris Mason 	else
254699d8f83cSChris Mason 		nr = min(right_nritems - 1, max_slot);
254734a38218SChris Mason 
254834a38218SChris Mason 	for (i = 0; i < nr; i++) {
25495f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2550db94535dSChris Mason 
255131840ae1SZheng Yan 		if (!empty && push_items > 0) {
255231840ae1SZheng Yan 			if (path->slots[0] < i)
255331840ae1SZheng Yan 				break;
255431840ae1SZheng Yan 			if (path->slots[0] == i) {
255531840ae1SZheng Yan 				int space = btrfs_leaf_free_space(root, right);
255631840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
255731840ae1SZheng Yan 					break;
255831840ae1SZheng Yan 			}
255931840ae1SZheng Yan 		}
256031840ae1SZheng Yan 
2561be0e5c09SChris Mason 		if (path->slots[0] == i)
256287b29b20SYan Zheng 			push_space += data_size;
2563db94535dSChris Mason 
2564db94535dSChris Mason 		this_item_size = btrfs_item_size(right, item);
2565db94535dSChris Mason 		if (this_item_size + sizeof(*item) + push_space > free_space)
2566be0e5c09SChris Mason 			break;
2567db94535dSChris Mason 
2568be0e5c09SChris Mason 		push_items++;
2569db94535dSChris Mason 		push_space += this_item_size + sizeof(*item);
2570be0e5c09SChris Mason 	}
2571db94535dSChris Mason 
2572be0e5c09SChris Mason 	if (push_items == 0) {
2573925baeddSChris Mason 		ret = 1;
2574925baeddSChris Mason 		goto out;
2575be0e5c09SChris Mason 	}
257634a38218SChris Mason 	if (!empty && push_items == btrfs_header_nritems(right))
2577a429e513SChris Mason 		WARN_ON(1);
25785f39d397SChris Mason 
2579be0e5c09SChris Mason 	/* push data from right to left */
25805f39d397SChris Mason 	copy_extent_buffer(left, right,
25815f39d397SChris Mason 			   btrfs_item_nr_offset(btrfs_header_nritems(left)),
25825f39d397SChris Mason 			   btrfs_item_nr_offset(0),
25835f39d397SChris Mason 			   push_items * sizeof(struct btrfs_item));
25845f39d397SChris Mason 
2585123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root) -
25865f39d397SChris Mason 		     btrfs_item_offset_nr(right, push_items - 1);
25875f39d397SChris Mason 
25885f39d397SChris Mason 	copy_extent_buffer(left, right, btrfs_leaf_data(left) +
2589d6025579SChris Mason 		     leaf_data_end(root, left) - push_space,
2590123abc88SChris Mason 		     btrfs_leaf_data(right) +
25915f39d397SChris Mason 		     btrfs_item_offset_nr(right, push_items - 1),
2592be0e5c09SChris Mason 		     push_space);
25935f39d397SChris Mason 	old_left_nritems = btrfs_header_nritems(left);
259487b29b20SYan Zheng 	BUG_ON(old_left_nritems <= 0);
2595eb60ceacSChris Mason 
2596db94535dSChris Mason 	old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
2597be0e5c09SChris Mason 	for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
25985f39d397SChris Mason 		u32 ioff;
2599db94535dSChris Mason 
26005f39d397SChris Mason 		item = btrfs_item_nr(left, i);
2601db94535dSChris Mason 
26025f39d397SChris Mason 		ioff = btrfs_item_offset(left, item);
26035f39d397SChris Mason 		btrfs_set_item_offset(left, item,
2604db94535dSChris Mason 		      ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
2605be0e5c09SChris Mason 	}
26065f39d397SChris Mason 	btrfs_set_header_nritems(left, old_left_nritems + push_items);
2607be0e5c09SChris Mason 
2608be0e5c09SChris Mason 	/* fixup right node */
260934a38218SChris Mason 	if (push_items > right_nritems) {
2610d397712bSChris Mason 		printk(KERN_CRIT "push items %d nr %u\n", push_items,
2611d397712bSChris Mason 		       right_nritems);
261234a38218SChris Mason 		WARN_ON(1);
261334a38218SChris Mason 	}
261434a38218SChris Mason 
261534a38218SChris Mason 	if (push_items < right_nritems) {
26165f39d397SChris Mason 		push_space = btrfs_item_offset_nr(right, push_items - 1) -
2617123abc88SChris Mason 						  leaf_data_end(root, right);
26185f39d397SChris Mason 		memmove_extent_buffer(right, btrfs_leaf_data(right) +
2619d6025579SChris Mason 				      BTRFS_LEAF_DATA_SIZE(root) - push_space,
2620d6025579SChris Mason 				      btrfs_leaf_data(right) +
2621123abc88SChris Mason 				      leaf_data_end(root, right), push_space);
26225f39d397SChris Mason 
26235f39d397SChris Mason 		memmove_extent_buffer(right, btrfs_item_nr_offset(0),
26245f39d397SChris Mason 			      btrfs_item_nr_offset(push_items),
26255f39d397SChris Mason 			     (btrfs_header_nritems(right) - push_items) *
26260783fcfcSChris Mason 			     sizeof(struct btrfs_item));
262734a38218SChris Mason 	}
2628eef1c494SYan 	right_nritems -= push_items;
2629eef1c494SYan 	btrfs_set_header_nritems(right, right_nritems);
2630123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root);
26315f39d397SChris Mason 	for (i = 0; i < right_nritems; i++) {
26325f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2633db94535dSChris Mason 
2634db94535dSChris Mason 		push_space = push_space - btrfs_item_size(right, item);
2635db94535dSChris Mason 		btrfs_set_item_offset(right, item, push_space);
2636db94535dSChris Mason 	}
2637eb60ceacSChris Mason 
26385f39d397SChris Mason 	btrfs_mark_buffer_dirty(left);
263934a38218SChris Mason 	if (right_nritems)
26405f39d397SChris Mason 		btrfs_mark_buffer_dirty(right);
2641f0486c68SYan, Zheng 	else
2642f0486c68SYan, Zheng 		clean_tree_block(trans, root, right);
2643098f59c2SChris Mason 
26445f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
2645143bede5SJeff Mahoney 	fixup_low_keys(trans, root, path, &disk_key, 1);
2646be0e5c09SChris Mason 
2647be0e5c09SChris Mason 	/* then fixup the leaf pointer in the path */
2648be0e5c09SChris Mason 	if (path->slots[0] < push_items) {
2649be0e5c09SChris Mason 		path->slots[0] += old_left_nritems;
2650925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
26515f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
26525f39d397SChris Mason 		path->nodes[0] = left;
2653be0e5c09SChris Mason 		path->slots[1] -= 1;
2654be0e5c09SChris Mason 	} else {
2655925baeddSChris Mason 		btrfs_tree_unlock(left);
26565f39d397SChris Mason 		free_extent_buffer(left);
2657be0e5c09SChris Mason 		path->slots[0] -= push_items;
2658be0e5c09SChris Mason 	}
2659eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
2660aa5d6bedSChris Mason 	return ret;
2661925baeddSChris Mason out:
2662925baeddSChris Mason 	btrfs_tree_unlock(left);
2663925baeddSChris Mason 	free_extent_buffer(left);
2664925baeddSChris Mason 	return ret;
2665be0e5c09SChris Mason }
2666be0e5c09SChris Mason 
266774123bd7SChris Mason /*
266844871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
266944871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
267099d8f83cSChris Mason  *
267199d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
267299d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us push all the
267399d8f83cSChris Mason  * items
267444871b1bSChris Mason  */
267544871b1bSChris Mason static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
267699d8f83cSChris Mason 			  *root, struct btrfs_path *path, int min_data_size,
267799d8f83cSChris Mason 			  int data_size, int empty, u32 max_slot)
267844871b1bSChris Mason {
267944871b1bSChris Mason 	struct extent_buffer *right = path->nodes[0];
268044871b1bSChris Mason 	struct extent_buffer *left;
268144871b1bSChris Mason 	int slot;
268244871b1bSChris Mason 	int free_space;
268344871b1bSChris Mason 	u32 right_nritems;
268444871b1bSChris Mason 	int ret = 0;
268544871b1bSChris Mason 
268644871b1bSChris Mason 	slot = path->slots[1];
268744871b1bSChris Mason 	if (slot == 0)
268844871b1bSChris Mason 		return 1;
268944871b1bSChris Mason 	if (!path->nodes[1])
269044871b1bSChris Mason 		return 1;
269144871b1bSChris Mason 
269244871b1bSChris Mason 	right_nritems = btrfs_header_nritems(right);
269344871b1bSChris Mason 	if (right_nritems == 0)
269444871b1bSChris Mason 		return 1;
269544871b1bSChris Mason 
269644871b1bSChris Mason 	btrfs_assert_tree_locked(path->nodes[1]);
269744871b1bSChris Mason 
269844871b1bSChris Mason 	left = read_node_slot(root, path->nodes[1], slot - 1);
269991ca338dSTsutomu Itoh 	if (left == NULL)
270091ca338dSTsutomu Itoh 		return 1;
270191ca338dSTsutomu Itoh 
270244871b1bSChris Mason 	btrfs_tree_lock(left);
270344871b1bSChris Mason 	btrfs_set_lock_blocking(left);
270444871b1bSChris Mason 
270544871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, left);
270644871b1bSChris Mason 	if (free_space < data_size) {
270744871b1bSChris Mason 		ret = 1;
270844871b1bSChris Mason 		goto out;
270944871b1bSChris Mason 	}
271044871b1bSChris Mason 
271144871b1bSChris Mason 	/* cow and double check */
271244871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, left,
271344871b1bSChris Mason 			      path->nodes[1], slot - 1, &left);
271444871b1bSChris Mason 	if (ret) {
271544871b1bSChris Mason 		/* we hit -ENOSPC, but it isn't fatal here */
271679787eaaSJeff Mahoney 		if (ret == -ENOSPC)
271744871b1bSChris Mason 			ret = 1;
271844871b1bSChris Mason 		goto out;
271944871b1bSChris Mason 	}
272044871b1bSChris Mason 
272144871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, left);
272244871b1bSChris Mason 	if (free_space < data_size) {
272344871b1bSChris Mason 		ret = 1;
272444871b1bSChris Mason 		goto out;
272544871b1bSChris Mason 	}
272644871b1bSChris Mason 
272799d8f83cSChris Mason 	return __push_leaf_left(trans, root, path, min_data_size,
272899d8f83cSChris Mason 			       empty, left, free_space, right_nritems,
272999d8f83cSChris Mason 			       max_slot);
273044871b1bSChris Mason out:
273144871b1bSChris Mason 	btrfs_tree_unlock(left);
273244871b1bSChris Mason 	free_extent_buffer(left);
273344871b1bSChris Mason 	return ret;
273444871b1bSChris Mason }
273544871b1bSChris Mason 
273644871b1bSChris Mason /*
273774123bd7SChris Mason  * split the path's leaf in two, making sure there is at least data_size
273874123bd7SChris Mason  * available for the resulting leaf level of the path.
273974123bd7SChris Mason  */
2740143bede5SJeff Mahoney static noinline void copy_for_split(struct btrfs_trans_handle *trans,
2741e02119d5SChris Mason 				    struct btrfs_root *root,
274244871b1bSChris Mason 				    struct btrfs_path *path,
274344871b1bSChris Mason 				    struct extent_buffer *l,
274444871b1bSChris Mason 				    struct extent_buffer *right,
274544871b1bSChris Mason 				    int slot, int mid, int nritems)
2746be0e5c09SChris Mason {
2747be0e5c09SChris Mason 	int data_copy_size;
2748be0e5c09SChris Mason 	int rt_data_off;
2749be0e5c09SChris Mason 	int i;
2750d4dbff95SChris Mason 	struct btrfs_disk_key disk_key;
2751be0e5c09SChris Mason 
27525f39d397SChris Mason 	nritems = nritems - mid;
27535f39d397SChris Mason 	btrfs_set_header_nritems(right, nritems);
27545f39d397SChris Mason 	data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
27555f39d397SChris Mason 
27565f39d397SChris Mason 	copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
27575f39d397SChris Mason 			   btrfs_item_nr_offset(mid),
27585f39d397SChris Mason 			   nritems * sizeof(struct btrfs_item));
27595f39d397SChris Mason 
27605f39d397SChris Mason 	copy_extent_buffer(right, l,
2761d6025579SChris Mason 		     btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2762123abc88SChris Mason 		     data_copy_size, btrfs_leaf_data(l) +
2763123abc88SChris Mason 		     leaf_data_end(root, l), data_copy_size);
276474123bd7SChris Mason 
27655f39d397SChris Mason 	rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
27665f39d397SChris Mason 		      btrfs_item_end_nr(l, mid);
27675f39d397SChris Mason 
27685f39d397SChris Mason 	for (i = 0; i < nritems; i++) {
27695f39d397SChris Mason 		struct btrfs_item *item = btrfs_item_nr(right, i);
2770db94535dSChris Mason 		u32 ioff;
2771db94535dSChris Mason 
2772db94535dSChris Mason 		ioff = btrfs_item_offset(right, item);
27735f39d397SChris Mason 		btrfs_set_item_offset(right, item, ioff + rt_data_off);
27740783fcfcSChris Mason 	}
277574123bd7SChris Mason 
27765f39d397SChris Mason 	btrfs_set_header_nritems(l, mid);
27775f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
2778143bede5SJeff Mahoney 	insert_ptr(trans, root, path, &disk_key, right->start,
2779db94535dSChris Mason 		   path->slots[1] + 1, 1);
27805f39d397SChris Mason 
27815f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
27825f39d397SChris Mason 	btrfs_mark_buffer_dirty(l);
2783eb60ceacSChris Mason 	BUG_ON(path->slots[0] != slot);
27845f39d397SChris Mason 
2785be0e5c09SChris Mason 	if (mid <= slot) {
2786925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
27875f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
27885f39d397SChris Mason 		path->nodes[0] = right;
2789be0e5c09SChris Mason 		path->slots[0] -= mid;
2790be0e5c09SChris Mason 		path->slots[1] += 1;
2791925baeddSChris Mason 	} else {
2792925baeddSChris Mason 		btrfs_tree_unlock(right);
27935f39d397SChris Mason 		free_extent_buffer(right);
2794925baeddSChris Mason 	}
27955f39d397SChris Mason 
2796eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
279744871b1bSChris Mason }
279844871b1bSChris Mason 
279944871b1bSChris Mason /*
280099d8f83cSChris Mason  * double splits happen when we need to insert a big item in the middle
280199d8f83cSChris Mason  * of a leaf.  A double split can leave us with 3 mostly empty leaves:
280299d8f83cSChris Mason  * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
280399d8f83cSChris Mason  *          A                 B                 C
280499d8f83cSChris Mason  *
280599d8f83cSChris Mason  * We avoid this by trying to push the items on either side of our target
280699d8f83cSChris Mason  * into the adjacent leaves.  If all goes well we can avoid the double split
280799d8f83cSChris Mason  * completely.
280899d8f83cSChris Mason  */
280999d8f83cSChris Mason static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
281099d8f83cSChris Mason 					  struct btrfs_root *root,
281199d8f83cSChris Mason 					  struct btrfs_path *path,
281299d8f83cSChris Mason 					  int data_size)
281399d8f83cSChris Mason {
281499d8f83cSChris Mason 	int ret;
281599d8f83cSChris Mason 	int progress = 0;
281699d8f83cSChris Mason 	int slot;
281799d8f83cSChris Mason 	u32 nritems;
281899d8f83cSChris Mason 
281999d8f83cSChris Mason 	slot = path->slots[0];
282099d8f83cSChris Mason 
282199d8f83cSChris Mason 	/*
282299d8f83cSChris Mason 	 * try to push all the items after our slot into the
282399d8f83cSChris Mason 	 * right leaf
282499d8f83cSChris Mason 	 */
282599d8f83cSChris Mason 	ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
282699d8f83cSChris Mason 	if (ret < 0)
282799d8f83cSChris Mason 		return ret;
282899d8f83cSChris Mason 
282999d8f83cSChris Mason 	if (ret == 0)
283099d8f83cSChris Mason 		progress++;
283199d8f83cSChris Mason 
283299d8f83cSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
283399d8f83cSChris Mason 	/*
283499d8f83cSChris Mason 	 * our goal is to get our slot at the start or end of a leaf.  If
283599d8f83cSChris Mason 	 * we've done so we're done
283699d8f83cSChris Mason 	 */
283799d8f83cSChris Mason 	if (path->slots[0] == 0 || path->slots[0] == nritems)
283899d8f83cSChris Mason 		return 0;
283999d8f83cSChris Mason 
284099d8f83cSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
284199d8f83cSChris Mason 		return 0;
284299d8f83cSChris Mason 
284399d8f83cSChris Mason 	/* try to push all the items before our slot into the next leaf */
284499d8f83cSChris Mason 	slot = path->slots[0];
284599d8f83cSChris Mason 	ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
284699d8f83cSChris Mason 	if (ret < 0)
284799d8f83cSChris Mason 		return ret;
284899d8f83cSChris Mason 
284999d8f83cSChris Mason 	if (ret == 0)
285099d8f83cSChris Mason 		progress++;
285199d8f83cSChris Mason 
285299d8f83cSChris Mason 	if (progress)
285399d8f83cSChris Mason 		return 0;
285499d8f83cSChris Mason 	return 1;
285599d8f83cSChris Mason }
285699d8f83cSChris Mason 
285799d8f83cSChris Mason /*
285844871b1bSChris Mason  * split the path's leaf in two, making sure there is at least data_size
285944871b1bSChris Mason  * available for the resulting leaf level of the path.
286044871b1bSChris Mason  *
286144871b1bSChris Mason  * returns 0 if all went well and < 0 on failure.
286244871b1bSChris Mason  */
286344871b1bSChris Mason static noinline int split_leaf(struct btrfs_trans_handle *trans,
286444871b1bSChris Mason 			       struct btrfs_root *root,
286544871b1bSChris Mason 			       struct btrfs_key *ins_key,
286644871b1bSChris Mason 			       struct btrfs_path *path, int data_size,
286744871b1bSChris Mason 			       int extend)
286844871b1bSChris Mason {
28695d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
287044871b1bSChris Mason 	struct extent_buffer *l;
287144871b1bSChris Mason 	u32 nritems;
287244871b1bSChris Mason 	int mid;
287344871b1bSChris Mason 	int slot;
287444871b1bSChris Mason 	struct extent_buffer *right;
287544871b1bSChris Mason 	int ret = 0;
287644871b1bSChris Mason 	int wret;
28775d4f98a2SYan Zheng 	int split;
287844871b1bSChris Mason 	int num_doubles = 0;
287999d8f83cSChris Mason 	int tried_avoid_double = 0;
288044871b1bSChris Mason 
2881a5719521SYan, Zheng 	l = path->nodes[0];
2882a5719521SYan, Zheng 	slot = path->slots[0];
2883a5719521SYan, Zheng 	if (extend && data_size + btrfs_item_size_nr(l, slot) +
2884a5719521SYan, Zheng 	    sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2885a5719521SYan, Zheng 		return -EOVERFLOW;
2886a5719521SYan, Zheng 
288744871b1bSChris Mason 	/* first try to make some room by pushing left and right */
288899d8f83cSChris Mason 	if (data_size) {
288999d8f83cSChris Mason 		wret = push_leaf_right(trans, root, path, data_size,
289099d8f83cSChris Mason 				       data_size, 0, 0);
289144871b1bSChris Mason 		if (wret < 0)
289244871b1bSChris Mason 			return wret;
289344871b1bSChris Mason 		if (wret) {
289499d8f83cSChris Mason 			wret = push_leaf_left(trans, root, path, data_size,
289599d8f83cSChris Mason 					      data_size, 0, (u32)-1);
289644871b1bSChris Mason 			if (wret < 0)
289744871b1bSChris Mason 				return wret;
289844871b1bSChris Mason 		}
289944871b1bSChris Mason 		l = path->nodes[0];
290044871b1bSChris Mason 
290144871b1bSChris Mason 		/* did the pushes work? */
290244871b1bSChris Mason 		if (btrfs_leaf_free_space(root, l) >= data_size)
290344871b1bSChris Mason 			return 0;
290444871b1bSChris Mason 	}
290544871b1bSChris Mason 
290644871b1bSChris Mason 	if (!path->nodes[1]) {
290744871b1bSChris Mason 		ret = insert_new_root(trans, root, path, 1);
290844871b1bSChris Mason 		if (ret)
290944871b1bSChris Mason 			return ret;
291044871b1bSChris Mason 	}
291144871b1bSChris Mason again:
29125d4f98a2SYan Zheng 	split = 1;
291344871b1bSChris Mason 	l = path->nodes[0];
291444871b1bSChris Mason 	slot = path->slots[0];
291544871b1bSChris Mason 	nritems = btrfs_header_nritems(l);
291644871b1bSChris Mason 	mid = (nritems + 1) / 2;
291744871b1bSChris Mason 
29185d4f98a2SYan Zheng 	if (mid <= slot) {
29195d4f98a2SYan Zheng 		if (nritems == 1 ||
29205d4f98a2SYan Zheng 		    leaf_space_used(l, mid, nritems - mid) + data_size >
29215d4f98a2SYan Zheng 			BTRFS_LEAF_DATA_SIZE(root)) {
29225d4f98a2SYan Zheng 			if (slot >= nritems) {
29235d4f98a2SYan Zheng 				split = 0;
29245d4f98a2SYan Zheng 			} else {
29255d4f98a2SYan Zheng 				mid = slot;
29265d4f98a2SYan Zheng 				if (mid != nritems &&
29275d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
29285d4f98a2SYan Zheng 				    data_size > BTRFS_LEAF_DATA_SIZE(root)) {
292999d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
293099d8f83cSChris Mason 						goto push_for_double;
29315d4f98a2SYan Zheng 					split = 2;
29325d4f98a2SYan Zheng 				}
29335d4f98a2SYan Zheng 			}
29345d4f98a2SYan Zheng 		}
29355d4f98a2SYan Zheng 	} else {
29365d4f98a2SYan Zheng 		if (leaf_space_used(l, 0, mid) + data_size >
29375d4f98a2SYan Zheng 			BTRFS_LEAF_DATA_SIZE(root)) {
29385d4f98a2SYan Zheng 			if (!extend && data_size && slot == 0) {
29395d4f98a2SYan Zheng 				split = 0;
29405d4f98a2SYan Zheng 			} else if ((extend || !data_size) && slot == 0) {
29415d4f98a2SYan Zheng 				mid = 1;
29425d4f98a2SYan Zheng 			} else {
29435d4f98a2SYan Zheng 				mid = slot;
29445d4f98a2SYan Zheng 				if (mid != nritems &&
29455d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
29465d4f98a2SYan Zheng 				    data_size > BTRFS_LEAF_DATA_SIZE(root)) {
294799d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
294899d8f83cSChris Mason 						goto push_for_double;
29495d4f98a2SYan Zheng 					split = 2 ;
29505d4f98a2SYan Zheng 				}
29515d4f98a2SYan Zheng 			}
29525d4f98a2SYan Zheng 		}
29535d4f98a2SYan Zheng 	}
29545d4f98a2SYan Zheng 
29555d4f98a2SYan Zheng 	if (split == 0)
29565d4f98a2SYan Zheng 		btrfs_cpu_key_to_disk(&disk_key, ins_key);
29575d4f98a2SYan Zheng 	else
29585d4f98a2SYan Zheng 		btrfs_item_key(l, &disk_key, mid);
29595d4f98a2SYan Zheng 
29605d4f98a2SYan Zheng 	right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
296144871b1bSChris Mason 					root->root_key.objectid,
296266d7e7f0SArne Jansen 					&disk_key, 0, l->start, 0, 0);
2963f0486c68SYan, Zheng 	if (IS_ERR(right))
296444871b1bSChris Mason 		return PTR_ERR(right);
2965f0486c68SYan, Zheng 
2966f0486c68SYan, Zheng 	root_add_used(root, root->leafsize);
296744871b1bSChris Mason 
296844871b1bSChris Mason 	memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
296944871b1bSChris Mason 	btrfs_set_header_bytenr(right, right->start);
297044871b1bSChris Mason 	btrfs_set_header_generation(right, trans->transid);
29715d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
297244871b1bSChris Mason 	btrfs_set_header_owner(right, root->root_key.objectid);
297344871b1bSChris Mason 	btrfs_set_header_level(right, 0);
297444871b1bSChris Mason 	write_extent_buffer(right, root->fs_info->fsid,
297544871b1bSChris Mason 			    (unsigned long)btrfs_header_fsid(right),
297644871b1bSChris Mason 			    BTRFS_FSID_SIZE);
297744871b1bSChris Mason 
297844871b1bSChris Mason 	write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
297944871b1bSChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(right),
298044871b1bSChris Mason 			    BTRFS_UUID_SIZE);
298144871b1bSChris Mason 
29825d4f98a2SYan Zheng 	if (split == 0) {
298344871b1bSChris Mason 		if (mid <= slot) {
298444871b1bSChris Mason 			btrfs_set_header_nritems(right, 0);
2985143bede5SJeff Mahoney 			insert_ptr(trans, root, path, &disk_key, right->start,
298644871b1bSChris Mason 				   path->slots[1] + 1, 1);
298744871b1bSChris Mason 			btrfs_tree_unlock(path->nodes[0]);
298844871b1bSChris Mason 			free_extent_buffer(path->nodes[0]);
298944871b1bSChris Mason 			path->nodes[0] = right;
299044871b1bSChris Mason 			path->slots[0] = 0;
299144871b1bSChris Mason 			path->slots[1] += 1;
299244871b1bSChris Mason 		} else {
299344871b1bSChris Mason 			btrfs_set_header_nritems(right, 0);
2994143bede5SJeff Mahoney 			insert_ptr(trans, root, path, &disk_key, right->start,
299544871b1bSChris Mason 					  path->slots[1], 1);
299644871b1bSChris Mason 			btrfs_tree_unlock(path->nodes[0]);
299744871b1bSChris Mason 			free_extent_buffer(path->nodes[0]);
299844871b1bSChris Mason 			path->nodes[0] = right;
299944871b1bSChris Mason 			path->slots[0] = 0;
3000143bede5SJeff Mahoney 			if (path->slots[1] == 0)
3001143bede5SJeff Mahoney 				fixup_low_keys(trans, root, path,
3002143bede5SJeff Mahoney 					       &disk_key, 1);
30035d4f98a2SYan Zheng 		}
300444871b1bSChris Mason 		btrfs_mark_buffer_dirty(right);
300544871b1bSChris Mason 		return ret;
300644871b1bSChris Mason 	}
300744871b1bSChris Mason 
3008143bede5SJeff Mahoney 	copy_for_split(trans, root, path, l, right, slot, mid, nritems);
300944871b1bSChris Mason 
30105d4f98a2SYan Zheng 	if (split == 2) {
3011cc0c5538SChris Mason 		BUG_ON(num_doubles != 0);
3012cc0c5538SChris Mason 		num_doubles++;
3013cc0c5538SChris Mason 		goto again;
30143326d1b0SChris Mason 	}
301544871b1bSChris Mason 
3016143bede5SJeff Mahoney 	return 0;
301799d8f83cSChris Mason 
301899d8f83cSChris Mason push_for_double:
301999d8f83cSChris Mason 	push_for_double_split(trans, root, path, data_size);
302099d8f83cSChris Mason 	tried_avoid_double = 1;
302199d8f83cSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
302299d8f83cSChris Mason 		return 0;
302399d8f83cSChris Mason 	goto again;
3024be0e5c09SChris Mason }
3025be0e5c09SChris Mason 
3026ad48fd75SYan, Zheng static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3027ad48fd75SYan, Zheng 					 struct btrfs_root *root,
3028ad48fd75SYan, Zheng 					 struct btrfs_path *path, int ins_len)
3029ad48fd75SYan, Zheng {
3030ad48fd75SYan, Zheng 	struct btrfs_key key;
3031ad48fd75SYan, Zheng 	struct extent_buffer *leaf;
3032ad48fd75SYan, Zheng 	struct btrfs_file_extent_item *fi;
3033ad48fd75SYan, Zheng 	u64 extent_len = 0;
3034ad48fd75SYan, Zheng 	u32 item_size;
3035ad48fd75SYan, Zheng 	int ret;
3036ad48fd75SYan, Zheng 
3037ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3038ad48fd75SYan, Zheng 	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3039ad48fd75SYan, Zheng 
3040ad48fd75SYan, Zheng 	BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3041ad48fd75SYan, Zheng 	       key.type != BTRFS_EXTENT_CSUM_KEY);
3042ad48fd75SYan, Zheng 
3043ad48fd75SYan, Zheng 	if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3044ad48fd75SYan, Zheng 		return 0;
3045ad48fd75SYan, Zheng 
3046ad48fd75SYan, Zheng 	item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3047ad48fd75SYan, Zheng 	if (key.type == BTRFS_EXTENT_DATA_KEY) {
3048ad48fd75SYan, Zheng 		fi = btrfs_item_ptr(leaf, path->slots[0],
3049ad48fd75SYan, Zheng 				    struct btrfs_file_extent_item);
3050ad48fd75SYan, Zheng 		extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3051ad48fd75SYan, Zheng 	}
3052b3b4aa74SDavid Sterba 	btrfs_release_path(path);
3053ad48fd75SYan, Zheng 
3054ad48fd75SYan, Zheng 	path->keep_locks = 1;
3055ad48fd75SYan, Zheng 	path->search_for_split = 1;
3056ad48fd75SYan, Zheng 	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3057ad48fd75SYan, Zheng 	path->search_for_split = 0;
3058ad48fd75SYan, Zheng 	if (ret < 0)
3059ad48fd75SYan, Zheng 		goto err;
3060ad48fd75SYan, Zheng 
3061ad48fd75SYan, Zheng 	ret = -EAGAIN;
3062ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3063ad48fd75SYan, Zheng 	/* if our item isn't there or got smaller, return now */
3064ad48fd75SYan, Zheng 	if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3065ad48fd75SYan, Zheng 		goto err;
3066ad48fd75SYan, Zheng 
3067109f6aefSChris Mason 	/* the leaf has  changed, it now has room.  return now */
3068109f6aefSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3069109f6aefSChris Mason 		goto err;
3070109f6aefSChris Mason 
3071ad48fd75SYan, Zheng 	if (key.type == BTRFS_EXTENT_DATA_KEY) {
3072ad48fd75SYan, Zheng 		fi = btrfs_item_ptr(leaf, path->slots[0],
3073ad48fd75SYan, Zheng 				    struct btrfs_file_extent_item);
3074ad48fd75SYan, Zheng 		if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3075ad48fd75SYan, Zheng 			goto err;
3076ad48fd75SYan, Zheng 	}
3077ad48fd75SYan, Zheng 
3078ad48fd75SYan, Zheng 	btrfs_set_path_blocking(path);
3079ad48fd75SYan, Zheng 	ret = split_leaf(trans, root, &key, path, ins_len, 1);
3080f0486c68SYan, Zheng 	if (ret)
3081f0486c68SYan, Zheng 		goto err;
3082ad48fd75SYan, Zheng 
3083ad48fd75SYan, Zheng 	path->keep_locks = 0;
3084ad48fd75SYan, Zheng 	btrfs_unlock_up_safe(path, 1);
3085ad48fd75SYan, Zheng 	return 0;
3086ad48fd75SYan, Zheng err:
3087ad48fd75SYan, Zheng 	path->keep_locks = 0;
3088ad48fd75SYan, Zheng 	return ret;
3089ad48fd75SYan, Zheng }
3090ad48fd75SYan, Zheng 
3091ad48fd75SYan, Zheng static noinline int split_item(struct btrfs_trans_handle *trans,
3092459931ecSChris Mason 			       struct btrfs_root *root,
3093459931ecSChris Mason 			       struct btrfs_path *path,
3094459931ecSChris Mason 			       struct btrfs_key *new_key,
3095459931ecSChris Mason 			       unsigned long split_offset)
3096459931ecSChris Mason {
3097459931ecSChris Mason 	struct extent_buffer *leaf;
3098459931ecSChris Mason 	struct btrfs_item *item;
3099459931ecSChris Mason 	struct btrfs_item *new_item;
3100459931ecSChris Mason 	int slot;
3101ad48fd75SYan, Zheng 	char *buf;
3102459931ecSChris Mason 	u32 nritems;
3103ad48fd75SYan, Zheng 	u32 item_size;
3104459931ecSChris Mason 	u32 orig_offset;
3105459931ecSChris Mason 	struct btrfs_disk_key disk_key;
3106459931ecSChris Mason 
3107459931ecSChris Mason 	leaf = path->nodes[0];
3108b9473439SChris Mason 	BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3109b9473439SChris Mason 
3110b4ce94deSChris Mason 	btrfs_set_path_blocking(path);
3111b4ce94deSChris Mason 
3112459931ecSChris Mason 	item = btrfs_item_nr(leaf, path->slots[0]);
3113459931ecSChris Mason 	orig_offset = btrfs_item_offset(leaf, item);
3114459931ecSChris Mason 	item_size = btrfs_item_size(leaf, item);
3115459931ecSChris Mason 
3116459931ecSChris Mason 	buf = kmalloc(item_size, GFP_NOFS);
3117ad48fd75SYan, Zheng 	if (!buf)
3118ad48fd75SYan, Zheng 		return -ENOMEM;
3119ad48fd75SYan, Zheng 
3120459931ecSChris Mason 	read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3121459931ecSChris Mason 			    path->slots[0]), item_size);
3122ad48fd75SYan, Zheng 
3123459931ecSChris Mason 	slot = path->slots[0] + 1;
3124459931ecSChris Mason 	nritems = btrfs_header_nritems(leaf);
3125459931ecSChris Mason 	if (slot != nritems) {
3126459931ecSChris Mason 		/* shift the items */
3127459931ecSChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3128459931ecSChris Mason 				btrfs_item_nr_offset(slot),
3129459931ecSChris Mason 				(nritems - slot) * sizeof(struct btrfs_item));
3130459931ecSChris Mason 	}
3131459931ecSChris Mason 
3132459931ecSChris Mason 	btrfs_cpu_key_to_disk(&disk_key, new_key);
3133459931ecSChris Mason 	btrfs_set_item_key(leaf, &disk_key, slot);
3134459931ecSChris Mason 
3135459931ecSChris Mason 	new_item = btrfs_item_nr(leaf, slot);
3136459931ecSChris Mason 
3137459931ecSChris Mason 	btrfs_set_item_offset(leaf, new_item, orig_offset);
3138459931ecSChris Mason 	btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3139459931ecSChris Mason 
3140459931ecSChris Mason 	btrfs_set_item_offset(leaf, item,
3141459931ecSChris Mason 			      orig_offset + item_size - split_offset);
3142459931ecSChris Mason 	btrfs_set_item_size(leaf, item, split_offset);
3143459931ecSChris Mason 
3144459931ecSChris Mason 	btrfs_set_header_nritems(leaf, nritems + 1);
3145459931ecSChris Mason 
3146459931ecSChris Mason 	/* write the data for the start of the original item */
3147459931ecSChris Mason 	write_extent_buffer(leaf, buf,
3148459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, path->slots[0]),
3149459931ecSChris Mason 			    split_offset);
3150459931ecSChris Mason 
3151459931ecSChris Mason 	/* write the data for the new item */
3152459931ecSChris Mason 	write_extent_buffer(leaf, buf + split_offset,
3153459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, slot),
3154459931ecSChris Mason 			    item_size - split_offset);
3155459931ecSChris Mason 	btrfs_mark_buffer_dirty(leaf);
3156459931ecSChris Mason 
3157ad48fd75SYan, Zheng 	BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
3158459931ecSChris Mason 	kfree(buf);
3159ad48fd75SYan, Zheng 	return 0;
3160ad48fd75SYan, Zheng }
3161ad48fd75SYan, Zheng 
3162ad48fd75SYan, Zheng /*
3163ad48fd75SYan, Zheng  * This function splits a single item into two items,
3164ad48fd75SYan, Zheng  * giving 'new_key' to the new item and splitting the
3165ad48fd75SYan, Zheng  * old one at split_offset (from the start of the item).
3166ad48fd75SYan, Zheng  *
3167ad48fd75SYan, Zheng  * The path may be released by this operation.  After
3168ad48fd75SYan, Zheng  * the split, the path is pointing to the old item.  The
3169ad48fd75SYan, Zheng  * new item is going to be in the same node as the old one.
3170ad48fd75SYan, Zheng  *
3171ad48fd75SYan, Zheng  * Note, the item being split must be smaller enough to live alone on
3172ad48fd75SYan, Zheng  * a tree block with room for one extra struct btrfs_item
3173ad48fd75SYan, Zheng  *
3174ad48fd75SYan, Zheng  * This allows us to split the item in place, keeping a lock on the
3175ad48fd75SYan, Zheng  * leaf the entire time.
3176ad48fd75SYan, Zheng  */
3177ad48fd75SYan, Zheng int btrfs_split_item(struct btrfs_trans_handle *trans,
3178ad48fd75SYan, Zheng 		     struct btrfs_root *root,
3179ad48fd75SYan, Zheng 		     struct btrfs_path *path,
3180ad48fd75SYan, Zheng 		     struct btrfs_key *new_key,
3181ad48fd75SYan, Zheng 		     unsigned long split_offset)
3182ad48fd75SYan, Zheng {
3183ad48fd75SYan, Zheng 	int ret;
3184ad48fd75SYan, Zheng 	ret = setup_leaf_for_split(trans, root, path,
3185ad48fd75SYan, Zheng 				   sizeof(struct btrfs_item));
3186ad48fd75SYan, Zheng 	if (ret)
3187459931ecSChris Mason 		return ret;
3188ad48fd75SYan, Zheng 
3189ad48fd75SYan, Zheng 	ret = split_item(trans, root, path, new_key, split_offset);
3190ad48fd75SYan, Zheng 	return ret;
3191ad48fd75SYan, Zheng }
3192ad48fd75SYan, Zheng 
3193ad48fd75SYan, Zheng /*
3194ad48fd75SYan, Zheng  * This function duplicate a item, giving 'new_key' to the new item.
3195ad48fd75SYan, Zheng  * It guarantees both items live in the same tree leaf and the new item
3196ad48fd75SYan, Zheng  * is contiguous with the original item.
3197ad48fd75SYan, Zheng  *
3198ad48fd75SYan, Zheng  * This allows us to split file extent in place, keeping a lock on the
3199ad48fd75SYan, Zheng  * leaf the entire time.
3200ad48fd75SYan, Zheng  */
3201ad48fd75SYan, Zheng int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3202ad48fd75SYan, Zheng 			 struct btrfs_root *root,
3203ad48fd75SYan, Zheng 			 struct btrfs_path *path,
3204ad48fd75SYan, Zheng 			 struct btrfs_key *new_key)
3205ad48fd75SYan, Zheng {
3206ad48fd75SYan, Zheng 	struct extent_buffer *leaf;
3207ad48fd75SYan, Zheng 	int ret;
3208ad48fd75SYan, Zheng 	u32 item_size;
3209ad48fd75SYan, Zheng 
3210ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3211ad48fd75SYan, Zheng 	item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3212ad48fd75SYan, Zheng 	ret = setup_leaf_for_split(trans, root, path,
3213ad48fd75SYan, Zheng 				   item_size + sizeof(struct btrfs_item));
3214ad48fd75SYan, Zheng 	if (ret)
3215ad48fd75SYan, Zheng 		return ret;
3216ad48fd75SYan, Zheng 
3217ad48fd75SYan, Zheng 	path->slots[0]++;
3218143bede5SJeff Mahoney 	setup_items_for_insert(trans, root, path, new_key, &item_size,
3219ad48fd75SYan, Zheng 			       item_size, item_size +
3220ad48fd75SYan, Zheng 			       sizeof(struct btrfs_item), 1);
3221ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3222ad48fd75SYan, Zheng 	memcpy_extent_buffer(leaf,
3223ad48fd75SYan, Zheng 			     btrfs_item_ptr_offset(leaf, path->slots[0]),
3224ad48fd75SYan, Zheng 			     btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3225ad48fd75SYan, Zheng 			     item_size);
3226ad48fd75SYan, Zheng 	return 0;
3227459931ecSChris Mason }
3228459931ecSChris Mason 
3229459931ecSChris Mason /*
3230d352ac68SChris Mason  * make the item pointed to by the path smaller.  new_size indicates
3231d352ac68SChris Mason  * how small to make it, and from_end tells us if we just chop bytes
3232d352ac68SChris Mason  * off the end of the item or if we shift the item to chop bytes off
3233d352ac68SChris Mason  * the front.
3234d352ac68SChris Mason  */
3235143bede5SJeff Mahoney void btrfs_truncate_item(struct btrfs_trans_handle *trans,
3236b18c6685SChris Mason 			 struct btrfs_root *root,
3237b18c6685SChris Mason 			 struct btrfs_path *path,
3238179e29e4SChris Mason 			 u32 new_size, int from_end)
3239b18c6685SChris Mason {
3240b18c6685SChris Mason 	int slot;
32415f39d397SChris Mason 	struct extent_buffer *leaf;
32425f39d397SChris Mason 	struct btrfs_item *item;
3243b18c6685SChris Mason 	u32 nritems;
3244b18c6685SChris Mason 	unsigned int data_end;
3245b18c6685SChris Mason 	unsigned int old_data_start;
3246b18c6685SChris Mason 	unsigned int old_size;
3247b18c6685SChris Mason 	unsigned int size_diff;
3248b18c6685SChris Mason 	int i;
3249b18c6685SChris Mason 
32505f39d397SChris Mason 	leaf = path->nodes[0];
3251179e29e4SChris Mason 	slot = path->slots[0];
3252179e29e4SChris Mason 
3253179e29e4SChris Mason 	old_size = btrfs_item_size_nr(leaf, slot);
3254179e29e4SChris Mason 	if (old_size == new_size)
3255143bede5SJeff Mahoney 		return;
3256b18c6685SChris Mason 
32575f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3258b18c6685SChris Mason 	data_end = leaf_data_end(root, leaf);
3259b18c6685SChris Mason 
32605f39d397SChris Mason 	old_data_start = btrfs_item_offset_nr(leaf, slot);
3261179e29e4SChris Mason 
3262b18c6685SChris Mason 	size_diff = old_size - new_size;
3263b18c6685SChris Mason 
3264b18c6685SChris Mason 	BUG_ON(slot < 0);
3265b18c6685SChris Mason 	BUG_ON(slot >= nritems);
3266b18c6685SChris Mason 
3267b18c6685SChris Mason 	/*
3268b18c6685SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3269b18c6685SChris Mason 	 */
3270b18c6685SChris Mason 	/* first correct the data pointers */
3271b18c6685SChris Mason 	for (i = slot; i < nritems; i++) {
32725f39d397SChris Mason 		u32 ioff;
32735f39d397SChris Mason 		item = btrfs_item_nr(leaf, i);
3274db94535dSChris Mason 
32755f39d397SChris Mason 		ioff = btrfs_item_offset(leaf, item);
32765f39d397SChris Mason 		btrfs_set_item_offset(leaf, item, ioff + size_diff);
3277b18c6685SChris Mason 	}
3278db94535dSChris Mason 
3279b18c6685SChris Mason 	/* shift the data */
3280179e29e4SChris Mason 	if (from_end) {
32815f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3282b18c6685SChris Mason 			      data_end + size_diff, btrfs_leaf_data(leaf) +
3283b18c6685SChris Mason 			      data_end, old_data_start + new_size - data_end);
3284179e29e4SChris Mason 	} else {
3285179e29e4SChris Mason 		struct btrfs_disk_key disk_key;
3286179e29e4SChris Mason 		u64 offset;
3287179e29e4SChris Mason 
3288179e29e4SChris Mason 		btrfs_item_key(leaf, &disk_key, slot);
3289179e29e4SChris Mason 
3290179e29e4SChris Mason 		if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3291179e29e4SChris Mason 			unsigned long ptr;
3292179e29e4SChris Mason 			struct btrfs_file_extent_item *fi;
3293179e29e4SChris Mason 
3294179e29e4SChris Mason 			fi = btrfs_item_ptr(leaf, slot,
3295179e29e4SChris Mason 					    struct btrfs_file_extent_item);
3296179e29e4SChris Mason 			fi = (struct btrfs_file_extent_item *)(
3297179e29e4SChris Mason 			     (unsigned long)fi - size_diff);
3298179e29e4SChris Mason 
3299179e29e4SChris Mason 			if (btrfs_file_extent_type(leaf, fi) ==
3300179e29e4SChris Mason 			    BTRFS_FILE_EXTENT_INLINE) {
3301179e29e4SChris Mason 				ptr = btrfs_item_ptr_offset(leaf, slot);
3302179e29e4SChris Mason 				memmove_extent_buffer(leaf, ptr,
3303179e29e4SChris Mason 				      (unsigned long)fi,
3304179e29e4SChris Mason 				      offsetof(struct btrfs_file_extent_item,
3305179e29e4SChris Mason 						 disk_bytenr));
3306179e29e4SChris Mason 			}
3307179e29e4SChris Mason 		}
3308179e29e4SChris Mason 
3309179e29e4SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3310179e29e4SChris Mason 			      data_end + size_diff, btrfs_leaf_data(leaf) +
3311179e29e4SChris Mason 			      data_end, old_data_start - data_end);
3312179e29e4SChris Mason 
3313179e29e4SChris Mason 		offset = btrfs_disk_key_offset(&disk_key);
3314179e29e4SChris Mason 		btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3315179e29e4SChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot);
3316179e29e4SChris Mason 		if (slot == 0)
3317179e29e4SChris Mason 			fixup_low_keys(trans, root, path, &disk_key, 1);
3318179e29e4SChris Mason 	}
33195f39d397SChris Mason 
33205f39d397SChris Mason 	item = btrfs_item_nr(leaf, slot);
33215f39d397SChris Mason 	btrfs_set_item_size(leaf, item, new_size);
33225f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
3323b18c6685SChris Mason 
33245f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
33255f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
3326b18c6685SChris Mason 		BUG();
33275f39d397SChris Mason 	}
3328b18c6685SChris Mason }
3329b18c6685SChris Mason 
3330d352ac68SChris Mason /*
3331d352ac68SChris Mason  * make the item pointed to by the path bigger, data_size is the new size.
3332d352ac68SChris Mason  */
3333143bede5SJeff Mahoney void btrfs_extend_item(struct btrfs_trans_handle *trans,
33345f39d397SChris Mason 		       struct btrfs_root *root, struct btrfs_path *path,
33355f39d397SChris Mason 		       u32 data_size)
33366567e837SChris Mason {
33376567e837SChris Mason 	int slot;
33385f39d397SChris Mason 	struct extent_buffer *leaf;
33395f39d397SChris Mason 	struct btrfs_item *item;
33406567e837SChris Mason 	u32 nritems;
33416567e837SChris Mason 	unsigned int data_end;
33426567e837SChris Mason 	unsigned int old_data;
33436567e837SChris Mason 	unsigned int old_size;
33446567e837SChris Mason 	int i;
33456567e837SChris Mason 
33465f39d397SChris Mason 	leaf = path->nodes[0];
33476567e837SChris Mason 
33485f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
33496567e837SChris Mason 	data_end = leaf_data_end(root, leaf);
33506567e837SChris Mason 
33515f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < data_size) {
33525f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
33536567e837SChris Mason 		BUG();
33545f39d397SChris Mason 	}
33556567e837SChris Mason 	slot = path->slots[0];
33565f39d397SChris Mason 	old_data = btrfs_item_end_nr(leaf, slot);
33576567e837SChris Mason 
33586567e837SChris Mason 	BUG_ON(slot < 0);
33593326d1b0SChris Mason 	if (slot >= nritems) {
33603326d1b0SChris Mason 		btrfs_print_leaf(root, leaf);
3361d397712bSChris Mason 		printk(KERN_CRIT "slot %d too large, nritems %d\n",
3362d397712bSChris Mason 		       slot, nritems);
33633326d1b0SChris Mason 		BUG_ON(1);
33643326d1b0SChris Mason 	}
33656567e837SChris Mason 
33666567e837SChris Mason 	/*
33676567e837SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
33686567e837SChris Mason 	 */
33696567e837SChris Mason 	/* first correct the data pointers */
33706567e837SChris Mason 	for (i = slot; i < nritems; i++) {
33715f39d397SChris Mason 		u32 ioff;
33725f39d397SChris Mason 		item = btrfs_item_nr(leaf, i);
3373db94535dSChris Mason 
33745f39d397SChris Mason 		ioff = btrfs_item_offset(leaf, item);
33755f39d397SChris Mason 		btrfs_set_item_offset(leaf, item, ioff - data_size);
33766567e837SChris Mason 	}
33775f39d397SChris Mason 
33786567e837SChris Mason 	/* shift the data */
33795f39d397SChris Mason 	memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
33806567e837SChris Mason 		      data_end - data_size, btrfs_leaf_data(leaf) +
33816567e837SChris Mason 		      data_end, old_data - data_end);
33825f39d397SChris Mason 
33836567e837SChris Mason 	data_end = old_data;
33845f39d397SChris Mason 	old_size = btrfs_item_size_nr(leaf, slot);
33855f39d397SChris Mason 	item = btrfs_item_nr(leaf, slot);
33865f39d397SChris Mason 	btrfs_set_item_size(leaf, item, old_size + data_size);
33875f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
33886567e837SChris Mason 
33895f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
33905f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
33916567e837SChris Mason 		BUG();
33925f39d397SChris Mason 	}
33936567e837SChris Mason }
33946567e837SChris Mason 
339574123bd7SChris Mason /*
3396d352ac68SChris Mason  * Given a key and some data, insert items into the tree.
339774123bd7SChris Mason  * This does all the path init required, making room in the tree if needed.
3398f3465ca4SJosef Bacik  * Returns the number of keys that were inserted.
3399f3465ca4SJosef Bacik  */
3400f3465ca4SJosef Bacik int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3401f3465ca4SJosef Bacik 			    struct btrfs_root *root,
3402f3465ca4SJosef Bacik 			    struct btrfs_path *path,
3403f3465ca4SJosef Bacik 			    struct btrfs_key *cpu_key, u32 *data_size,
3404f3465ca4SJosef Bacik 			    int nr)
3405f3465ca4SJosef Bacik {
3406f3465ca4SJosef Bacik 	struct extent_buffer *leaf;
3407f3465ca4SJosef Bacik 	struct btrfs_item *item;
3408f3465ca4SJosef Bacik 	int ret = 0;
3409f3465ca4SJosef Bacik 	int slot;
3410f3465ca4SJosef Bacik 	int i;
3411f3465ca4SJosef Bacik 	u32 nritems;
3412f3465ca4SJosef Bacik 	u32 total_data = 0;
3413f3465ca4SJosef Bacik 	u32 total_size = 0;
3414f3465ca4SJosef Bacik 	unsigned int data_end;
3415f3465ca4SJosef Bacik 	struct btrfs_disk_key disk_key;
3416f3465ca4SJosef Bacik 	struct btrfs_key found_key;
3417f3465ca4SJosef Bacik 
341887b29b20SYan Zheng 	for (i = 0; i < nr; i++) {
341987b29b20SYan Zheng 		if (total_size + data_size[i] + sizeof(struct btrfs_item) >
342087b29b20SYan Zheng 		    BTRFS_LEAF_DATA_SIZE(root)) {
342187b29b20SYan Zheng 			break;
342287b29b20SYan Zheng 			nr = i;
342387b29b20SYan Zheng 		}
3424f3465ca4SJosef Bacik 		total_data += data_size[i];
342587b29b20SYan Zheng 		total_size += data_size[i] + sizeof(struct btrfs_item);
342687b29b20SYan Zheng 	}
342787b29b20SYan Zheng 	BUG_ON(nr == 0);
3428f3465ca4SJosef Bacik 
3429f3465ca4SJosef Bacik 	ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3430f3465ca4SJosef Bacik 	if (ret == 0)
3431f3465ca4SJosef Bacik 		return -EEXIST;
3432f3465ca4SJosef Bacik 	if (ret < 0)
3433f3465ca4SJosef Bacik 		goto out;
3434f3465ca4SJosef Bacik 
3435f3465ca4SJosef Bacik 	leaf = path->nodes[0];
3436f3465ca4SJosef Bacik 
3437f3465ca4SJosef Bacik 	nritems = btrfs_header_nritems(leaf);
3438f3465ca4SJosef Bacik 	data_end = leaf_data_end(root, leaf);
3439f3465ca4SJosef Bacik 
3440f3465ca4SJosef Bacik 	if (btrfs_leaf_free_space(root, leaf) < total_size) {
3441f3465ca4SJosef Bacik 		for (i = nr; i >= 0; i--) {
3442f3465ca4SJosef Bacik 			total_data -= data_size[i];
3443f3465ca4SJosef Bacik 			total_size -= data_size[i] + sizeof(struct btrfs_item);
3444f3465ca4SJosef Bacik 			if (total_size < btrfs_leaf_free_space(root, leaf))
3445f3465ca4SJosef Bacik 				break;
3446f3465ca4SJosef Bacik 		}
3447f3465ca4SJosef Bacik 		nr = i;
3448f3465ca4SJosef Bacik 	}
3449f3465ca4SJosef Bacik 
3450f3465ca4SJosef Bacik 	slot = path->slots[0];
3451f3465ca4SJosef Bacik 	BUG_ON(slot < 0);
3452f3465ca4SJosef Bacik 
3453f3465ca4SJosef Bacik 	if (slot != nritems) {
3454f3465ca4SJosef Bacik 		unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3455f3465ca4SJosef Bacik 
3456f3465ca4SJosef Bacik 		item = btrfs_item_nr(leaf, slot);
3457f3465ca4SJosef Bacik 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
3458f3465ca4SJosef Bacik 
3459f3465ca4SJosef Bacik 		/* figure out how many keys we can insert in here */
3460f3465ca4SJosef Bacik 		total_data = data_size[0];
3461f3465ca4SJosef Bacik 		for (i = 1; i < nr; i++) {
34625d4f98a2SYan Zheng 			if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3463f3465ca4SJosef Bacik 				break;
3464f3465ca4SJosef Bacik 			total_data += data_size[i];
3465f3465ca4SJosef Bacik 		}
3466f3465ca4SJosef Bacik 		nr = i;
3467f3465ca4SJosef Bacik 
3468f3465ca4SJosef Bacik 		if (old_data < data_end) {
3469f3465ca4SJosef Bacik 			btrfs_print_leaf(root, leaf);
3470d397712bSChris Mason 			printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3471f3465ca4SJosef Bacik 			       slot, old_data, data_end);
3472f3465ca4SJosef Bacik 			BUG_ON(1);
3473f3465ca4SJosef Bacik 		}
3474f3465ca4SJosef Bacik 		/*
3475f3465ca4SJosef Bacik 		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3476f3465ca4SJosef Bacik 		 */
3477f3465ca4SJosef Bacik 		/* first correct the data pointers */
3478f3465ca4SJosef Bacik 		for (i = slot; i < nritems; i++) {
3479f3465ca4SJosef Bacik 			u32 ioff;
3480f3465ca4SJosef Bacik 
3481f3465ca4SJosef Bacik 			item = btrfs_item_nr(leaf, i);
3482f3465ca4SJosef Bacik 			ioff = btrfs_item_offset(leaf, item);
3483f3465ca4SJosef Bacik 			btrfs_set_item_offset(leaf, item, ioff - total_data);
3484f3465ca4SJosef Bacik 		}
3485f3465ca4SJosef Bacik 		/* shift the items */
3486f3465ca4SJosef Bacik 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3487f3465ca4SJosef Bacik 			      btrfs_item_nr_offset(slot),
3488f3465ca4SJosef Bacik 			      (nritems - slot) * sizeof(struct btrfs_item));
3489f3465ca4SJosef Bacik 
3490f3465ca4SJosef Bacik 		/* shift the data */
3491f3465ca4SJosef Bacik 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3492f3465ca4SJosef Bacik 			      data_end - total_data, btrfs_leaf_data(leaf) +
3493f3465ca4SJosef Bacik 			      data_end, old_data - data_end);
3494f3465ca4SJosef Bacik 		data_end = old_data;
3495f3465ca4SJosef Bacik 	} else {
3496f3465ca4SJosef Bacik 		/*
3497f3465ca4SJosef Bacik 		 * this sucks but it has to be done, if we are inserting at
3498f3465ca4SJosef Bacik 		 * the end of the leaf only insert 1 of the items, since we
3499f3465ca4SJosef Bacik 		 * have no way of knowing whats on the next leaf and we'd have
3500f3465ca4SJosef Bacik 		 * to drop our current locks to figure it out
3501f3465ca4SJosef Bacik 		 */
3502f3465ca4SJosef Bacik 		nr = 1;
3503f3465ca4SJosef Bacik 	}
3504f3465ca4SJosef Bacik 
3505f3465ca4SJosef Bacik 	/* setup the item for the new data */
3506f3465ca4SJosef Bacik 	for (i = 0; i < nr; i++) {
3507f3465ca4SJosef Bacik 		btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3508f3465ca4SJosef Bacik 		btrfs_set_item_key(leaf, &disk_key, slot + i);
3509f3465ca4SJosef Bacik 		item = btrfs_item_nr(leaf, slot + i);
3510f3465ca4SJosef Bacik 		btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3511f3465ca4SJosef Bacik 		data_end -= data_size[i];
3512f3465ca4SJosef Bacik 		btrfs_set_item_size(leaf, item, data_size[i]);
3513f3465ca4SJosef Bacik 	}
3514f3465ca4SJosef Bacik 	btrfs_set_header_nritems(leaf, nritems + nr);
3515f3465ca4SJosef Bacik 	btrfs_mark_buffer_dirty(leaf);
3516f3465ca4SJosef Bacik 
3517f3465ca4SJosef Bacik 	ret = 0;
3518f3465ca4SJosef Bacik 	if (slot == 0) {
3519f3465ca4SJosef Bacik 		btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3520143bede5SJeff Mahoney 		fixup_low_keys(trans, root, path, &disk_key, 1);
3521f3465ca4SJosef Bacik 	}
3522f3465ca4SJosef Bacik 
3523f3465ca4SJosef Bacik 	if (btrfs_leaf_free_space(root, leaf) < 0) {
3524f3465ca4SJosef Bacik 		btrfs_print_leaf(root, leaf);
3525f3465ca4SJosef Bacik 		BUG();
3526f3465ca4SJosef Bacik 	}
3527f3465ca4SJosef Bacik out:
3528f3465ca4SJosef Bacik 	if (!ret)
3529f3465ca4SJosef Bacik 		ret = nr;
3530f3465ca4SJosef Bacik 	return ret;
3531f3465ca4SJosef Bacik }
3532f3465ca4SJosef Bacik 
3533f3465ca4SJosef Bacik /*
353444871b1bSChris Mason  * this is a helper for btrfs_insert_empty_items, the main goal here is
353544871b1bSChris Mason  * to save stack depth by doing the bulk of the work in a function
353644871b1bSChris Mason  * that doesn't call btrfs_search_slot
353774123bd7SChris Mason  */
3538143bede5SJeff Mahoney void setup_items_for_insert(struct btrfs_trans_handle *trans,
353944871b1bSChris Mason 			    struct btrfs_root *root, struct btrfs_path *path,
35409c58309dSChris Mason 			    struct btrfs_key *cpu_key, u32 *data_size,
354144871b1bSChris Mason 			    u32 total_data, u32 total_size, int nr)
3542be0e5c09SChris Mason {
35435f39d397SChris Mason 	struct btrfs_item *item;
35449c58309dSChris Mason 	int i;
35457518a238SChris Mason 	u32 nritems;
3546be0e5c09SChris Mason 	unsigned int data_end;
3547e2fa7227SChris Mason 	struct btrfs_disk_key disk_key;
354844871b1bSChris Mason 	struct extent_buffer *leaf;
354944871b1bSChris Mason 	int slot;
3550e2fa7227SChris Mason 
35515f39d397SChris Mason 	leaf = path->nodes[0];
355244871b1bSChris Mason 	slot = path->slots[0];
355374123bd7SChris Mason 
35545f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3555123abc88SChris Mason 	data_end = leaf_data_end(root, leaf);
3556eb60ceacSChris Mason 
3557f25956ccSChris Mason 	if (btrfs_leaf_free_space(root, leaf) < total_size) {
35583326d1b0SChris Mason 		btrfs_print_leaf(root, leaf);
3559d397712bSChris Mason 		printk(KERN_CRIT "not enough freespace need %u have %d\n",
35609c58309dSChris Mason 		       total_size, btrfs_leaf_free_space(root, leaf));
3561be0e5c09SChris Mason 		BUG();
3562d4dbff95SChris Mason 	}
35635f39d397SChris Mason 
3564be0e5c09SChris Mason 	if (slot != nritems) {
35655f39d397SChris Mason 		unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3566be0e5c09SChris Mason 
35675f39d397SChris Mason 		if (old_data < data_end) {
35685f39d397SChris Mason 			btrfs_print_leaf(root, leaf);
3569d397712bSChris Mason 			printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
35705f39d397SChris Mason 			       slot, old_data, data_end);
35715f39d397SChris Mason 			BUG_ON(1);
35725f39d397SChris Mason 		}
3573be0e5c09SChris Mason 		/*
3574be0e5c09SChris Mason 		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3575be0e5c09SChris Mason 		 */
3576be0e5c09SChris Mason 		/* first correct the data pointers */
35770783fcfcSChris Mason 		for (i = slot; i < nritems; i++) {
35785f39d397SChris Mason 			u32 ioff;
3579db94535dSChris Mason 
35805f39d397SChris Mason 			item = btrfs_item_nr(leaf, i);
35815f39d397SChris Mason 			ioff = btrfs_item_offset(leaf, item);
35829c58309dSChris Mason 			btrfs_set_item_offset(leaf, item, ioff - total_data);
35830783fcfcSChris Mason 		}
3584be0e5c09SChris Mason 		/* shift the items */
35859c58309dSChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
35865f39d397SChris Mason 			      btrfs_item_nr_offset(slot),
35870783fcfcSChris Mason 			      (nritems - slot) * sizeof(struct btrfs_item));
3588be0e5c09SChris Mason 
3589be0e5c09SChris Mason 		/* shift the data */
35905f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
35919c58309dSChris Mason 			      data_end - total_data, btrfs_leaf_data(leaf) +
3592be0e5c09SChris Mason 			      data_end, old_data - data_end);
3593be0e5c09SChris Mason 		data_end = old_data;
3594be0e5c09SChris Mason 	}
35955f39d397SChris Mason 
359662e2749eSChris Mason 	/* setup the item for the new data */
35979c58309dSChris Mason 	for (i = 0; i < nr; i++) {
35989c58309dSChris Mason 		btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
35999c58309dSChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot + i);
36009c58309dSChris Mason 		item = btrfs_item_nr(leaf, slot + i);
36019c58309dSChris Mason 		btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
36029c58309dSChris Mason 		data_end -= data_size[i];
36039c58309dSChris Mason 		btrfs_set_item_size(leaf, item, data_size[i]);
36049c58309dSChris Mason 	}
360544871b1bSChris Mason 
36069c58309dSChris Mason 	btrfs_set_header_nritems(leaf, nritems + nr);
3607aa5d6bedSChris Mason 
36085a01a2e3SChris Mason 	if (slot == 0) {
36095a01a2e3SChris Mason 		btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3610143bede5SJeff Mahoney 		fixup_low_keys(trans, root, path, &disk_key, 1);
36115a01a2e3SChris Mason 	}
3612b9473439SChris Mason 	btrfs_unlock_up_safe(path, 1);
3613b9473439SChris Mason 	btrfs_mark_buffer_dirty(leaf);
3614aa5d6bedSChris Mason 
36155f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
36165f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
3617be0e5c09SChris Mason 		BUG();
36185f39d397SChris Mason 	}
361944871b1bSChris Mason }
362044871b1bSChris Mason 
362144871b1bSChris Mason /*
362244871b1bSChris Mason  * Given a key and some data, insert items into the tree.
362344871b1bSChris Mason  * This does all the path init required, making room in the tree if needed.
362444871b1bSChris Mason  */
362544871b1bSChris Mason int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
362644871b1bSChris Mason 			    struct btrfs_root *root,
362744871b1bSChris Mason 			    struct btrfs_path *path,
362844871b1bSChris Mason 			    struct btrfs_key *cpu_key, u32 *data_size,
362944871b1bSChris Mason 			    int nr)
363044871b1bSChris Mason {
363144871b1bSChris Mason 	int ret = 0;
363244871b1bSChris Mason 	int slot;
363344871b1bSChris Mason 	int i;
363444871b1bSChris Mason 	u32 total_size = 0;
363544871b1bSChris Mason 	u32 total_data = 0;
363644871b1bSChris Mason 
363744871b1bSChris Mason 	for (i = 0; i < nr; i++)
363844871b1bSChris Mason 		total_data += data_size[i];
363944871b1bSChris Mason 
364044871b1bSChris Mason 	total_size = total_data + (nr * sizeof(struct btrfs_item));
364144871b1bSChris Mason 	ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
364244871b1bSChris Mason 	if (ret == 0)
364344871b1bSChris Mason 		return -EEXIST;
364444871b1bSChris Mason 	if (ret < 0)
3645143bede5SJeff Mahoney 		return ret;
364644871b1bSChris Mason 
364744871b1bSChris Mason 	slot = path->slots[0];
364844871b1bSChris Mason 	BUG_ON(slot < 0);
364944871b1bSChris Mason 
3650143bede5SJeff Mahoney 	setup_items_for_insert(trans, root, path, cpu_key, data_size,
365144871b1bSChris Mason 			       total_data, total_size, nr);
3652143bede5SJeff Mahoney 	return 0;
365362e2749eSChris Mason }
365462e2749eSChris Mason 
365562e2749eSChris Mason /*
365662e2749eSChris Mason  * Given a key and some data, insert an item into the tree.
365762e2749eSChris Mason  * This does all the path init required, making room in the tree if needed.
365862e2749eSChris Mason  */
3659e089f05cSChris Mason int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3660e089f05cSChris Mason 		      *root, struct btrfs_key *cpu_key, void *data, u32
3661e089f05cSChris Mason 		      data_size)
366262e2749eSChris Mason {
366362e2749eSChris Mason 	int ret = 0;
36642c90e5d6SChris Mason 	struct btrfs_path *path;
36655f39d397SChris Mason 	struct extent_buffer *leaf;
36665f39d397SChris Mason 	unsigned long ptr;
366762e2749eSChris Mason 
36682c90e5d6SChris Mason 	path = btrfs_alloc_path();
3669db5b493aSTsutomu Itoh 	if (!path)
3670db5b493aSTsutomu Itoh 		return -ENOMEM;
36712c90e5d6SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
367262e2749eSChris Mason 	if (!ret) {
36735f39d397SChris Mason 		leaf = path->nodes[0];
36745f39d397SChris Mason 		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
36755f39d397SChris Mason 		write_extent_buffer(leaf, data, ptr, data_size);
36765f39d397SChris Mason 		btrfs_mark_buffer_dirty(leaf);
367762e2749eSChris Mason 	}
36782c90e5d6SChris Mason 	btrfs_free_path(path);
3679aa5d6bedSChris Mason 	return ret;
3680be0e5c09SChris Mason }
3681be0e5c09SChris Mason 
368274123bd7SChris Mason /*
36835de08d7dSChris Mason  * delete the pointer from a given node.
368474123bd7SChris Mason  *
3685d352ac68SChris Mason  * the tree should have been previously balanced so the deletion does not
3686d352ac68SChris Mason  * empty a node.
368774123bd7SChris Mason  */
3688143bede5SJeff Mahoney static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3689e089f05cSChris Mason 		    struct btrfs_path *path, int level, int slot)
3690be0e5c09SChris Mason {
36915f39d397SChris Mason 	struct extent_buffer *parent = path->nodes[level];
36927518a238SChris Mason 	u32 nritems;
3693be0e5c09SChris Mason 
36945f39d397SChris Mason 	nritems = btrfs_header_nritems(parent);
3695be0e5c09SChris Mason 	if (slot != nritems - 1) {
36965f39d397SChris Mason 		memmove_extent_buffer(parent,
36975f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot),
36985f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot + 1),
3699d6025579SChris Mason 			      sizeof(struct btrfs_key_ptr) *
3700d6025579SChris Mason 			      (nritems - slot - 1));
3701be0e5c09SChris Mason 	}
37027518a238SChris Mason 	nritems--;
37035f39d397SChris Mason 	btrfs_set_header_nritems(parent, nritems);
37047518a238SChris Mason 	if (nritems == 0 && parent == root->node) {
37055f39d397SChris Mason 		BUG_ON(btrfs_header_level(root->node) != 1);
3706eb60ceacSChris Mason 		/* just turn the root into a leaf and break */
37075f39d397SChris Mason 		btrfs_set_header_level(root->node, 0);
3708bb803951SChris Mason 	} else if (slot == 0) {
37095f39d397SChris Mason 		struct btrfs_disk_key disk_key;
37105f39d397SChris Mason 
37115f39d397SChris Mason 		btrfs_node_key(parent, &disk_key, 0);
3712143bede5SJeff Mahoney 		fixup_low_keys(trans, root, path, &disk_key, level + 1);
3713be0e5c09SChris Mason 	}
3714d6025579SChris Mason 	btrfs_mark_buffer_dirty(parent);
3715be0e5c09SChris Mason }
3716be0e5c09SChris Mason 
371774123bd7SChris Mason /*
3718323ac95bSChris Mason  * a helper function to delete the leaf pointed to by path->slots[1] and
37195d4f98a2SYan Zheng  * path->nodes[1].
3720323ac95bSChris Mason  *
3721323ac95bSChris Mason  * This deletes the pointer in path->nodes[1] and frees the leaf
3722323ac95bSChris Mason  * block extent.  zero is returned if it all worked out, < 0 otherwise.
3723323ac95bSChris Mason  *
3724323ac95bSChris Mason  * The path must have already been setup for deleting the leaf, including
3725323ac95bSChris Mason  * all the proper balancing.  path->nodes[1] must be locked.
3726323ac95bSChris Mason  */
3727143bede5SJeff Mahoney static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
3728323ac95bSChris Mason 				    struct btrfs_root *root,
37295d4f98a2SYan Zheng 				    struct btrfs_path *path,
37305d4f98a2SYan Zheng 				    struct extent_buffer *leaf)
3731323ac95bSChris Mason {
37325d4f98a2SYan Zheng 	WARN_ON(btrfs_header_generation(leaf) != trans->transid);
3733143bede5SJeff Mahoney 	del_ptr(trans, root, path, 1, path->slots[1]);
3734323ac95bSChris Mason 
37354d081c41SChris Mason 	/*
37364d081c41SChris Mason 	 * btrfs_free_extent is expensive, we want to make sure we
37374d081c41SChris Mason 	 * aren't holding any locks when we call it
37384d081c41SChris Mason 	 */
37394d081c41SChris Mason 	btrfs_unlock_up_safe(path, 0);
37404d081c41SChris Mason 
3741f0486c68SYan, Zheng 	root_sub_used(root, leaf->len);
3742f0486c68SYan, Zheng 
374366d7e7f0SArne Jansen 	btrfs_free_tree_block(trans, root, leaf, 0, 1, 0);
3744323ac95bSChris Mason }
3745323ac95bSChris Mason /*
374674123bd7SChris Mason  * delete the item at the leaf level in path.  If that empties
374774123bd7SChris Mason  * the leaf, remove it from the tree
374874123bd7SChris Mason  */
374985e21bacSChris Mason int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
375085e21bacSChris Mason 		    struct btrfs_path *path, int slot, int nr)
3751be0e5c09SChris Mason {
37525f39d397SChris Mason 	struct extent_buffer *leaf;
37535f39d397SChris Mason 	struct btrfs_item *item;
375485e21bacSChris Mason 	int last_off;
375585e21bacSChris Mason 	int dsize = 0;
3756aa5d6bedSChris Mason 	int ret = 0;
3757aa5d6bedSChris Mason 	int wret;
375885e21bacSChris Mason 	int i;
37597518a238SChris Mason 	u32 nritems;
3760be0e5c09SChris Mason 
37615f39d397SChris Mason 	leaf = path->nodes[0];
376285e21bacSChris Mason 	last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
376385e21bacSChris Mason 
376485e21bacSChris Mason 	for (i = 0; i < nr; i++)
376585e21bacSChris Mason 		dsize += btrfs_item_size_nr(leaf, slot + i);
376685e21bacSChris Mason 
37675f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3768be0e5c09SChris Mason 
376985e21bacSChris Mason 	if (slot + nr != nritems) {
3770123abc88SChris Mason 		int data_end = leaf_data_end(root, leaf);
37715f39d397SChris Mason 
37725f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3773d6025579SChris Mason 			      data_end + dsize,
3774123abc88SChris Mason 			      btrfs_leaf_data(leaf) + data_end,
377585e21bacSChris Mason 			      last_off - data_end);
37765f39d397SChris Mason 
377785e21bacSChris Mason 		for (i = slot + nr; i < nritems; i++) {
37785f39d397SChris Mason 			u32 ioff;
3779db94535dSChris Mason 
37805f39d397SChris Mason 			item = btrfs_item_nr(leaf, i);
37815f39d397SChris Mason 			ioff = btrfs_item_offset(leaf, item);
37825f39d397SChris Mason 			btrfs_set_item_offset(leaf, item, ioff + dsize);
37830783fcfcSChris Mason 		}
3784db94535dSChris Mason 
37855f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
378685e21bacSChris Mason 			      btrfs_item_nr_offset(slot + nr),
37870783fcfcSChris Mason 			      sizeof(struct btrfs_item) *
378885e21bacSChris Mason 			      (nritems - slot - nr));
3789be0e5c09SChris Mason 	}
379085e21bacSChris Mason 	btrfs_set_header_nritems(leaf, nritems - nr);
379185e21bacSChris Mason 	nritems -= nr;
37925f39d397SChris Mason 
379374123bd7SChris Mason 	/* delete the leaf if we've emptied it */
37947518a238SChris Mason 	if (nritems == 0) {
37955f39d397SChris Mason 		if (leaf == root->node) {
37965f39d397SChris Mason 			btrfs_set_header_level(leaf, 0);
37979a8dd150SChris Mason 		} else {
3798f0486c68SYan, Zheng 			btrfs_set_path_blocking(path);
3799f0486c68SYan, Zheng 			clean_tree_block(trans, root, leaf);
3800143bede5SJeff Mahoney 			btrfs_del_leaf(trans, root, path, leaf);
38019a8dd150SChris Mason 		}
3802be0e5c09SChris Mason 	} else {
38037518a238SChris Mason 		int used = leaf_space_used(leaf, 0, nritems);
3804aa5d6bedSChris Mason 		if (slot == 0) {
38055f39d397SChris Mason 			struct btrfs_disk_key disk_key;
38065f39d397SChris Mason 
38075f39d397SChris Mason 			btrfs_item_key(leaf, &disk_key, 0);
3808143bede5SJeff Mahoney 			fixup_low_keys(trans, root, path, &disk_key, 1);
3809aa5d6bedSChris Mason 		}
3810aa5d6bedSChris Mason 
381174123bd7SChris Mason 		/* delete the leaf if it is mostly empty */
3812d717aa1dSYan Zheng 		if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
3813be0e5c09SChris Mason 			/* push_leaf_left fixes the path.
3814be0e5c09SChris Mason 			 * make sure the path still points to our leaf
3815be0e5c09SChris Mason 			 * for possible call to del_ptr below
3816be0e5c09SChris Mason 			 */
38174920c9acSChris Mason 			slot = path->slots[1];
38185f39d397SChris Mason 			extent_buffer_get(leaf);
38195f39d397SChris Mason 
3820b9473439SChris Mason 			btrfs_set_path_blocking(path);
382199d8f83cSChris Mason 			wret = push_leaf_left(trans, root, path, 1, 1,
382299d8f83cSChris Mason 					      1, (u32)-1);
382354aa1f4dSChris Mason 			if (wret < 0 && wret != -ENOSPC)
3824aa5d6bedSChris Mason 				ret = wret;
38255f39d397SChris Mason 
38265f39d397SChris Mason 			if (path->nodes[0] == leaf &&
38275f39d397SChris Mason 			    btrfs_header_nritems(leaf)) {
382899d8f83cSChris Mason 				wret = push_leaf_right(trans, root, path, 1,
382999d8f83cSChris Mason 						       1, 1, 0);
383054aa1f4dSChris Mason 				if (wret < 0 && wret != -ENOSPC)
3831aa5d6bedSChris Mason 					ret = wret;
3832aa5d6bedSChris Mason 			}
38335f39d397SChris Mason 
38345f39d397SChris Mason 			if (btrfs_header_nritems(leaf) == 0) {
3835323ac95bSChris Mason 				path->slots[1] = slot;
3836143bede5SJeff Mahoney 				btrfs_del_leaf(trans, root, path, leaf);
38375f39d397SChris Mason 				free_extent_buffer(leaf);
3838143bede5SJeff Mahoney 				ret = 0;
38395de08d7dSChris Mason 			} else {
3840925baeddSChris Mason 				/* if we're still in the path, make sure
3841925baeddSChris Mason 				 * we're dirty.  Otherwise, one of the
3842925baeddSChris Mason 				 * push_leaf functions must have already
3843925baeddSChris Mason 				 * dirtied this buffer
3844925baeddSChris Mason 				 */
3845925baeddSChris Mason 				if (path->nodes[0] == leaf)
38465f39d397SChris Mason 					btrfs_mark_buffer_dirty(leaf);
38475f39d397SChris Mason 				free_extent_buffer(leaf);
3848be0e5c09SChris Mason 			}
3849d5719762SChris Mason 		} else {
38505f39d397SChris Mason 			btrfs_mark_buffer_dirty(leaf);
3851be0e5c09SChris Mason 		}
38529a8dd150SChris Mason 	}
3853aa5d6bedSChris Mason 	return ret;
38549a8dd150SChris Mason }
38559a8dd150SChris Mason 
385697571fd0SChris Mason /*
3857925baeddSChris Mason  * search the tree again to find a leaf with lesser keys
38587bb86316SChris Mason  * returns 0 if it found something or 1 if there are no lesser leaves.
38597bb86316SChris Mason  * returns < 0 on io errors.
3860d352ac68SChris Mason  *
3861d352ac68SChris Mason  * This may release the path, and so you may lose any locks held at the
3862d352ac68SChris Mason  * time you call it.
38637bb86316SChris Mason  */
38647bb86316SChris Mason int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
38657bb86316SChris Mason {
3866925baeddSChris Mason 	struct btrfs_key key;
3867925baeddSChris Mason 	struct btrfs_disk_key found_key;
3868925baeddSChris Mason 	int ret;
38697bb86316SChris Mason 
3870925baeddSChris Mason 	btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
3871925baeddSChris Mason 
3872925baeddSChris Mason 	if (key.offset > 0)
3873925baeddSChris Mason 		key.offset--;
3874925baeddSChris Mason 	else if (key.type > 0)
3875925baeddSChris Mason 		key.type--;
3876925baeddSChris Mason 	else if (key.objectid > 0)
3877925baeddSChris Mason 		key.objectid--;
3878925baeddSChris Mason 	else
38797bb86316SChris Mason 		return 1;
38807bb86316SChris Mason 
3881b3b4aa74SDavid Sterba 	btrfs_release_path(path);
3882925baeddSChris Mason 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3883925baeddSChris Mason 	if (ret < 0)
3884925baeddSChris Mason 		return ret;
3885925baeddSChris Mason 	btrfs_item_key(path->nodes[0], &found_key, 0);
3886925baeddSChris Mason 	ret = comp_keys(&found_key, &key);
3887925baeddSChris Mason 	if (ret < 0)
38887bb86316SChris Mason 		return 0;
3889925baeddSChris Mason 	return 1;
38907bb86316SChris Mason }
38917bb86316SChris Mason 
38923f157a2fSChris Mason /*
38933f157a2fSChris Mason  * A helper function to walk down the tree starting at min_key, and looking
38943f157a2fSChris Mason  * for nodes or leaves that are either in cache or have a minimum
3895d352ac68SChris Mason  * transaction id.  This is used by the btree defrag code, and tree logging
38963f157a2fSChris Mason  *
38973f157a2fSChris Mason  * This does not cow, but it does stuff the starting key it finds back
38983f157a2fSChris Mason  * into min_key, so you can call btrfs_search_slot with cow=1 on the
38993f157a2fSChris Mason  * key and get a writable path.
39003f157a2fSChris Mason  *
39013f157a2fSChris Mason  * This does lock as it descends, and path->keep_locks should be set
39023f157a2fSChris Mason  * to 1 by the caller.
39033f157a2fSChris Mason  *
39043f157a2fSChris Mason  * This honors path->lowest_level to prevent descent past a given level
39053f157a2fSChris Mason  * of the tree.
39063f157a2fSChris Mason  *
3907d352ac68SChris Mason  * min_trans indicates the oldest transaction that you are interested
3908d352ac68SChris Mason  * in walking through.  Any nodes or leaves older than min_trans are
3909d352ac68SChris Mason  * skipped over (without reading them).
3910d352ac68SChris Mason  *
39113f157a2fSChris Mason  * returns zero if something useful was found, < 0 on error and 1 if there
39123f157a2fSChris Mason  * was nothing in the tree that matched the search criteria.
39133f157a2fSChris Mason  */
39143f157a2fSChris Mason int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
3915e02119d5SChris Mason 			 struct btrfs_key *max_key,
39163f157a2fSChris Mason 			 struct btrfs_path *path, int cache_only,
39173f157a2fSChris Mason 			 u64 min_trans)
39183f157a2fSChris Mason {
39193f157a2fSChris Mason 	struct extent_buffer *cur;
39203f157a2fSChris Mason 	struct btrfs_key found_key;
39213f157a2fSChris Mason 	int slot;
39229652480bSYan 	int sret;
39233f157a2fSChris Mason 	u32 nritems;
39243f157a2fSChris Mason 	int level;
39253f157a2fSChris Mason 	int ret = 1;
39263f157a2fSChris Mason 
3927934d375bSChris Mason 	WARN_ON(!path->keep_locks);
39283f157a2fSChris Mason again:
3929bd681513SChris Mason 	cur = btrfs_read_lock_root_node(root);
39303f157a2fSChris Mason 	level = btrfs_header_level(cur);
3931e02119d5SChris Mason 	WARN_ON(path->nodes[level]);
39323f157a2fSChris Mason 	path->nodes[level] = cur;
3933bd681513SChris Mason 	path->locks[level] = BTRFS_READ_LOCK;
39343f157a2fSChris Mason 
39353f157a2fSChris Mason 	if (btrfs_header_generation(cur) < min_trans) {
39363f157a2fSChris Mason 		ret = 1;
39373f157a2fSChris Mason 		goto out;
39383f157a2fSChris Mason 	}
39393f157a2fSChris Mason 	while (1) {
39403f157a2fSChris Mason 		nritems = btrfs_header_nritems(cur);
39413f157a2fSChris Mason 		level = btrfs_header_level(cur);
39429652480bSYan 		sret = bin_search(cur, min_key, level, &slot);
39433f157a2fSChris Mason 
3944323ac95bSChris Mason 		/* at the lowest level, we're done, setup the path and exit */
3945323ac95bSChris Mason 		if (level == path->lowest_level) {
3946e02119d5SChris Mason 			if (slot >= nritems)
3947e02119d5SChris Mason 				goto find_next_key;
39483f157a2fSChris Mason 			ret = 0;
39493f157a2fSChris Mason 			path->slots[level] = slot;
39503f157a2fSChris Mason 			btrfs_item_key_to_cpu(cur, &found_key, slot);
39513f157a2fSChris Mason 			goto out;
39523f157a2fSChris Mason 		}
39539652480bSYan 		if (sret && slot > 0)
39549652480bSYan 			slot--;
39553f157a2fSChris Mason 		/*
39563f157a2fSChris Mason 		 * check this node pointer against the cache_only and
39573f157a2fSChris Mason 		 * min_trans parameters.  If it isn't in cache or is too
39583f157a2fSChris Mason 		 * old, skip to the next one.
39593f157a2fSChris Mason 		 */
39603f157a2fSChris Mason 		while (slot < nritems) {
39613f157a2fSChris Mason 			u64 blockptr;
39623f157a2fSChris Mason 			u64 gen;
39633f157a2fSChris Mason 			struct extent_buffer *tmp;
3964e02119d5SChris Mason 			struct btrfs_disk_key disk_key;
3965e02119d5SChris Mason 
39663f157a2fSChris Mason 			blockptr = btrfs_node_blockptr(cur, slot);
39673f157a2fSChris Mason 			gen = btrfs_node_ptr_generation(cur, slot);
39683f157a2fSChris Mason 			if (gen < min_trans) {
39693f157a2fSChris Mason 				slot++;
39703f157a2fSChris Mason 				continue;
39713f157a2fSChris Mason 			}
39723f157a2fSChris Mason 			if (!cache_only)
39733f157a2fSChris Mason 				break;
39743f157a2fSChris Mason 
3975e02119d5SChris Mason 			if (max_key) {
3976e02119d5SChris Mason 				btrfs_node_key(cur, &disk_key, slot);
3977e02119d5SChris Mason 				if (comp_keys(&disk_key, max_key) >= 0) {
3978e02119d5SChris Mason 					ret = 1;
3979e02119d5SChris Mason 					goto out;
3980e02119d5SChris Mason 				}
3981e02119d5SChris Mason 			}
3982e02119d5SChris Mason 
39833f157a2fSChris Mason 			tmp = btrfs_find_tree_block(root, blockptr,
39843f157a2fSChris Mason 					    btrfs_level_size(root, level - 1));
39853f157a2fSChris Mason 
39863f157a2fSChris Mason 			if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
39873f157a2fSChris Mason 				free_extent_buffer(tmp);
39883f157a2fSChris Mason 				break;
39893f157a2fSChris Mason 			}
39903f157a2fSChris Mason 			if (tmp)
39913f157a2fSChris Mason 				free_extent_buffer(tmp);
39923f157a2fSChris Mason 			slot++;
39933f157a2fSChris Mason 		}
3994e02119d5SChris Mason find_next_key:
39953f157a2fSChris Mason 		/*
39963f157a2fSChris Mason 		 * we didn't find a candidate key in this node, walk forward
39973f157a2fSChris Mason 		 * and find another one
39983f157a2fSChris Mason 		 */
39993f157a2fSChris Mason 		if (slot >= nritems) {
4000e02119d5SChris Mason 			path->slots[level] = slot;
4001b4ce94deSChris Mason 			btrfs_set_path_blocking(path);
4002e02119d5SChris Mason 			sret = btrfs_find_next_key(root, path, min_key, level,
40033f157a2fSChris Mason 						  cache_only, min_trans);
4004e02119d5SChris Mason 			if (sret == 0) {
4005b3b4aa74SDavid Sterba 				btrfs_release_path(path);
40063f157a2fSChris Mason 				goto again;
40073f157a2fSChris Mason 			} else {
40083f157a2fSChris Mason 				goto out;
40093f157a2fSChris Mason 			}
40103f157a2fSChris Mason 		}
40113f157a2fSChris Mason 		/* save our key for returning back */
40123f157a2fSChris Mason 		btrfs_node_key_to_cpu(cur, &found_key, slot);
40133f157a2fSChris Mason 		path->slots[level] = slot;
40143f157a2fSChris Mason 		if (level == path->lowest_level) {
40153f157a2fSChris Mason 			ret = 0;
40163f157a2fSChris Mason 			unlock_up(path, level, 1);
40173f157a2fSChris Mason 			goto out;
40183f157a2fSChris Mason 		}
4019b4ce94deSChris Mason 		btrfs_set_path_blocking(path);
40203f157a2fSChris Mason 		cur = read_node_slot(root, cur, slot);
402179787eaaSJeff Mahoney 		BUG_ON(!cur); /* -ENOMEM */
40223f157a2fSChris Mason 
4023bd681513SChris Mason 		btrfs_tree_read_lock(cur);
4024b4ce94deSChris Mason 
4025bd681513SChris Mason 		path->locks[level - 1] = BTRFS_READ_LOCK;
40263f157a2fSChris Mason 		path->nodes[level - 1] = cur;
40273f157a2fSChris Mason 		unlock_up(path, level, 1);
4028bd681513SChris Mason 		btrfs_clear_path_blocking(path, NULL, 0);
40293f157a2fSChris Mason 	}
40303f157a2fSChris Mason out:
40313f157a2fSChris Mason 	if (ret == 0)
40323f157a2fSChris Mason 		memcpy(min_key, &found_key, sizeof(found_key));
4033b4ce94deSChris Mason 	btrfs_set_path_blocking(path);
40343f157a2fSChris Mason 	return ret;
40353f157a2fSChris Mason }
40363f157a2fSChris Mason 
40373f157a2fSChris Mason /*
40383f157a2fSChris Mason  * this is similar to btrfs_next_leaf, but does not try to preserve
40393f157a2fSChris Mason  * and fixup the path.  It looks for and returns the next key in the
40403f157a2fSChris Mason  * tree based on the current path and the cache_only and min_trans
40413f157a2fSChris Mason  * parameters.
40423f157a2fSChris Mason  *
40433f157a2fSChris Mason  * 0 is returned if another key is found, < 0 if there are any errors
40443f157a2fSChris Mason  * and 1 is returned if there are no higher keys in the tree
40453f157a2fSChris Mason  *
40463f157a2fSChris Mason  * path->keep_locks should be set to 1 on the search made before
40473f157a2fSChris Mason  * calling this function.
40483f157a2fSChris Mason  */
4049e7a84565SChris Mason int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
405033c66f43SYan Zheng 			struct btrfs_key *key, int level,
40513f157a2fSChris Mason 			int cache_only, u64 min_trans)
4052e7a84565SChris Mason {
4053e7a84565SChris Mason 	int slot;
4054e7a84565SChris Mason 	struct extent_buffer *c;
4055e7a84565SChris Mason 
4056934d375bSChris Mason 	WARN_ON(!path->keep_locks);
4057e7a84565SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
4058e7a84565SChris Mason 		if (!path->nodes[level])
4059e7a84565SChris Mason 			return 1;
4060e7a84565SChris Mason 
4061e7a84565SChris Mason 		slot = path->slots[level] + 1;
4062e7a84565SChris Mason 		c = path->nodes[level];
40633f157a2fSChris Mason next:
4064e7a84565SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
406533c66f43SYan Zheng 			int ret;
406633c66f43SYan Zheng 			int orig_lowest;
406733c66f43SYan Zheng 			struct btrfs_key cur_key;
406833c66f43SYan Zheng 			if (level + 1 >= BTRFS_MAX_LEVEL ||
406933c66f43SYan Zheng 			    !path->nodes[level + 1])
4070e7a84565SChris Mason 				return 1;
407133c66f43SYan Zheng 
407233c66f43SYan Zheng 			if (path->locks[level + 1]) {
407333c66f43SYan Zheng 				level++;
4074e7a84565SChris Mason 				continue;
4075e7a84565SChris Mason 			}
407633c66f43SYan Zheng 
407733c66f43SYan Zheng 			slot = btrfs_header_nritems(c) - 1;
407833c66f43SYan Zheng 			if (level == 0)
407933c66f43SYan Zheng 				btrfs_item_key_to_cpu(c, &cur_key, slot);
408033c66f43SYan Zheng 			else
408133c66f43SYan Zheng 				btrfs_node_key_to_cpu(c, &cur_key, slot);
408233c66f43SYan Zheng 
408333c66f43SYan Zheng 			orig_lowest = path->lowest_level;
4084b3b4aa74SDavid Sterba 			btrfs_release_path(path);
408533c66f43SYan Zheng 			path->lowest_level = level;
408633c66f43SYan Zheng 			ret = btrfs_search_slot(NULL, root, &cur_key, path,
408733c66f43SYan Zheng 						0, 0);
408833c66f43SYan Zheng 			path->lowest_level = orig_lowest;
408933c66f43SYan Zheng 			if (ret < 0)
409033c66f43SYan Zheng 				return ret;
409133c66f43SYan Zheng 
409233c66f43SYan Zheng 			c = path->nodes[level];
409333c66f43SYan Zheng 			slot = path->slots[level];
409433c66f43SYan Zheng 			if (ret == 0)
409533c66f43SYan Zheng 				slot++;
409633c66f43SYan Zheng 			goto next;
409733c66f43SYan Zheng 		}
409833c66f43SYan Zheng 
4099e7a84565SChris Mason 		if (level == 0)
4100e7a84565SChris Mason 			btrfs_item_key_to_cpu(c, key, slot);
41013f157a2fSChris Mason 		else {
41023f157a2fSChris Mason 			u64 blockptr = btrfs_node_blockptr(c, slot);
41033f157a2fSChris Mason 			u64 gen = btrfs_node_ptr_generation(c, slot);
41043f157a2fSChris Mason 
41053f157a2fSChris Mason 			if (cache_only) {
41063f157a2fSChris Mason 				struct extent_buffer *cur;
41073f157a2fSChris Mason 				cur = btrfs_find_tree_block(root, blockptr,
41083f157a2fSChris Mason 					    btrfs_level_size(root, level - 1));
41093f157a2fSChris Mason 				if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
41103f157a2fSChris Mason 					slot++;
41113f157a2fSChris Mason 					if (cur)
41123f157a2fSChris Mason 						free_extent_buffer(cur);
41133f157a2fSChris Mason 					goto next;
41143f157a2fSChris Mason 				}
41153f157a2fSChris Mason 				free_extent_buffer(cur);
41163f157a2fSChris Mason 			}
41173f157a2fSChris Mason 			if (gen < min_trans) {
41183f157a2fSChris Mason 				slot++;
41193f157a2fSChris Mason 				goto next;
41203f157a2fSChris Mason 			}
4121e7a84565SChris Mason 			btrfs_node_key_to_cpu(c, key, slot);
41223f157a2fSChris Mason 		}
4123e7a84565SChris Mason 		return 0;
4124e7a84565SChris Mason 	}
4125e7a84565SChris Mason 	return 1;
4126e7a84565SChris Mason }
4127e7a84565SChris Mason 
41287bb86316SChris Mason /*
4129925baeddSChris Mason  * search the tree again to find a leaf with greater keys
41300f70abe2SChris Mason  * returns 0 if it found something or 1 if there are no greater leaves.
41310f70abe2SChris Mason  * returns < 0 on io errors.
413297571fd0SChris Mason  */
4133234b63a0SChris Mason int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
4134d97e63b6SChris Mason {
4135d97e63b6SChris Mason 	int slot;
41368e73f275SChris Mason 	int level;
41375f39d397SChris Mason 	struct extent_buffer *c;
41388e73f275SChris Mason 	struct extent_buffer *next;
4139925baeddSChris Mason 	struct btrfs_key key;
4140925baeddSChris Mason 	u32 nritems;
4141925baeddSChris Mason 	int ret;
41428e73f275SChris Mason 	int old_spinning = path->leave_spinning;
4143bd681513SChris Mason 	int next_rw_lock = 0;
4144925baeddSChris Mason 
4145925baeddSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
4146d397712bSChris Mason 	if (nritems == 0)
4147925baeddSChris Mason 		return 1;
4148925baeddSChris Mason 
41498e73f275SChris Mason 	btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
41508e73f275SChris Mason again:
41518e73f275SChris Mason 	level = 1;
41528e73f275SChris Mason 	next = NULL;
4153bd681513SChris Mason 	next_rw_lock = 0;
4154b3b4aa74SDavid Sterba 	btrfs_release_path(path);
41558e73f275SChris Mason 
4156a2135011SChris Mason 	path->keep_locks = 1;
41578e73f275SChris Mason 	path->leave_spinning = 1;
41588e73f275SChris Mason 
4159925baeddSChris Mason 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4160925baeddSChris Mason 	path->keep_locks = 0;
4161925baeddSChris Mason 
4162925baeddSChris Mason 	if (ret < 0)
4163925baeddSChris Mason 		return ret;
4164925baeddSChris Mason 
4165a2135011SChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
4166168fd7d2SChris Mason 	/*
4167168fd7d2SChris Mason 	 * by releasing the path above we dropped all our locks.  A balance
4168168fd7d2SChris Mason 	 * could have added more items next to the key that used to be
4169168fd7d2SChris Mason 	 * at the very end of the block.  So, check again here and
4170168fd7d2SChris Mason 	 * advance the path if there are now more items available.
4171168fd7d2SChris Mason 	 */
4172a2135011SChris Mason 	if (nritems > 0 && path->slots[0] < nritems - 1) {
4173e457afecSYan Zheng 		if (ret == 0)
4174168fd7d2SChris Mason 			path->slots[0]++;
41758e73f275SChris Mason 		ret = 0;
4176925baeddSChris Mason 		goto done;
4177925baeddSChris Mason 	}
4178d97e63b6SChris Mason 
4179234b63a0SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
41808e73f275SChris Mason 		if (!path->nodes[level]) {
41818e73f275SChris Mason 			ret = 1;
41828e73f275SChris Mason 			goto done;
41838e73f275SChris Mason 		}
41845f39d397SChris Mason 
4185d97e63b6SChris Mason 		slot = path->slots[level] + 1;
4186d97e63b6SChris Mason 		c = path->nodes[level];
41875f39d397SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
4188d97e63b6SChris Mason 			level++;
41898e73f275SChris Mason 			if (level == BTRFS_MAX_LEVEL) {
41908e73f275SChris Mason 				ret = 1;
41918e73f275SChris Mason 				goto done;
41928e73f275SChris Mason 			}
4193d97e63b6SChris Mason 			continue;
4194d97e63b6SChris Mason 		}
41955f39d397SChris Mason 
4196925baeddSChris Mason 		if (next) {
4197bd681513SChris Mason 			btrfs_tree_unlock_rw(next, next_rw_lock);
41985f39d397SChris Mason 			free_extent_buffer(next);
4199925baeddSChris Mason 		}
42005f39d397SChris Mason 
42018e73f275SChris Mason 		next = c;
4202bd681513SChris Mason 		next_rw_lock = path->locks[level];
42038e73f275SChris Mason 		ret = read_block_for_search(NULL, root, path, &next, level,
42048e73f275SChris Mason 					    slot, &key);
42058e73f275SChris Mason 		if (ret == -EAGAIN)
42068e73f275SChris Mason 			goto again;
42075f39d397SChris Mason 
420876a05b35SChris Mason 		if (ret < 0) {
4209b3b4aa74SDavid Sterba 			btrfs_release_path(path);
421076a05b35SChris Mason 			goto done;
421176a05b35SChris Mason 		}
421276a05b35SChris Mason 
42135cd57b2cSChris Mason 		if (!path->skip_locking) {
4214bd681513SChris Mason 			ret = btrfs_try_tree_read_lock(next);
42158e73f275SChris Mason 			if (!ret) {
42168e73f275SChris Mason 				btrfs_set_path_blocking(path);
4217bd681513SChris Mason 				btrfs_tree_read_lock(next);
4218bd681513SChris Mason 				btrfs_clear_path_blocking(path, next,
4219bd681513SChris Mason 							  BTRFS_READ_LOCK);
42208e73f275SChris Mason 			}
4221bd681513SChris Mason 			next_rw_lock = BTRFS_READ_LOCK;
4222bd681513SChris Mason 		}
4223d97e63b6SChris Mason 		break;
4224d97e63b6SChris Mason 	}
4225d97e63b6SChris Mason 	path->slots[level] = slot;
4226d97e63b6SChris Mason 	while (1) {
4227d97e63b6SChris Mason 		level--;
4228d97e63b6SChris Mason 		c = path->nodes[level];
4229925baeddSChris Mason 		if (path->locks[level])
4230bd681513SChris Mason 			btrfs_tree_unlock_rw(c, path->locks[level]);
42318e73f275SChris Mason 
42325f39d397SChris Mason 		free_extent_buffer(c);
4233d97e63b6SChris Mason 		path->nodes[level] = next;
4234d97e63b6SChris Mason 		path->slots[level] = 0;
4235a74a4b97SChris Mason 		if (!path->skip_locking)
4236bd681513SChris Mason 			path->locks[level] = next_rw_lock;
4237d97e63b6SChris Mason 		if (!level)
4238d97e63b6SChris Mason 			break;
4239b4ce94deSChris Mason 
42408e73f275SChris Mason 		ret = read_block_for_search(NULL, root, path, &next, level,
42418e73f275SChris Mason 					    0, &key);
42428e73f275SChris Mason 		if (ret == -EAGAIN)
42438e73f275SChris Mason 			goto again;
42448e73f275SChris Mason 
424576a05b35SChris Mason 		if (ret < 0) {
4246b3b4aa74SDavid Sterba 			btrfs_release_path(path);
424776a05b35SChris Mason 			goto done;
424876a05b35SChris Mason 		}
424976a05b35SChris Mason 
42505cd57b2cSChris Mason 		if (!path->skip_locking) {
4251bd681513SChris Mason 			ret = btrfs_try_tree_read_lock(next);
42528e73f275SChris Mason 			if (!ret) {
42538e73f275SChris Mason 				btrfs_set_path_blocking(path);
4254bd681513SChris Mason 				btrfs_tree_read_lock(next);
4255bd681513SChris Mason 				btrfs_clear_path_blocking(path, next,
4256bd681513SChris Mason 							  BTRFS_READ_LOCK);
42578e73f275SChris Mason 			}
4258bd681513SChris Mason 			next_rw_lock = BTRFS_READ_LOCK;
4259bd681513SChris Mason 		}
4260d97e63b6SChris Mason 	}
42618e73f275SChris Mason 	ret = 0;
4262925baeddSChris Mason done:
4263925baeddSChris Mason 	unlock_up(path, 0, 1);
42648e73f275SChris Mason 	path->leave_spinning = old_spinning;
42658e73f275SChris Mason 	if (!old_spinning)
42668e73f275SChris Mason 		btrfs_set_path_blocking(path);
42678e73f275SChris Mason 
42688e73f275SChris Mason 	return ret;
4269d97e63b6SChris Mason }
42700b86a832SChris Mason 
42713f157a2fSChris Mason /*
42723f157a2fSChris Mason  * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
42733f157a2fSChris Mason  * searching until it gets past min_objectid or finds an item of 'type'
42743f157a2fSChris Mason  *
42753f157a2fSChris Mason  * returns 0 if something is found, 1 if nothing was found and < 0 on error
42763f157a2fSChris Mason  */
42770b86a832SChris Mason int btrfs_previous_item(struct btrfs_root *root,
42780b86a832SChris Mason 			struct btrfs_path *path, u64 min_objectid,
42790b86a832SChris Mason 			int type)
42800b86a832SChris Mason {
42810b86a832SChris Mason 	struct btrfs_key found_key;
42820b86a832SChris Mason 	struct extent_buffer *leaf;
4283e02119d5SChris Mason 	u32 nritems;
42840b86a832SChris Mason 	int ret;
42850b86a832SChris Mason 
42860b86a832SChris Mason 	while (1) {
42870b86a832SChris Mason 		if (path->slots[0] == 0) {
4288b4ce94deSChris Mason 			btrfs_set_path_blocking(path);
42890b86a832SChris Mason 			ret = btrfs_prev_leaf(root, path);
42900b86a832SChris Mason 			if (ret != 0)
42910b86a832SChris Mason 				return ret;
42920b86a832SChris Mason 		} else {
42930b86a832SChris Mason 			path->slots[0]--;
42940b86a832SChris Mason 		}
42950b86a832SChris Mason 		leaf = path->nodes[0];
4296e02119d5SChris Mason 		nritems = btrfs_header_nritems(leaf);
4297e02119d5SChris Mason 		if (nritems == 0)
4298e02119d5SChris Mason 			return 1;
4299e02119d5SChris Mason 		if (path->slots[0] == nritems)
4300e02119d5SChris Mason 			path->slots[0]--;
4301e02119d5SChris Mason 
43020b86a832SChris Mason 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4303e02119d5SChris Mason 		if (found_key.objectid < min_objectid)
4304e02119d5SChris Mason 			break;
43050a4eefbbSYan Zheng 		if (found_key.type == type)
43060a4eefbbSYan Zheng 			return 0;
4307e02119d5SChris Mason 		if (found_key.objectid == min_objectid &&
4308e02119d5SChris Mason 		    found_key.type < type)
4309e02119d5SChris Mason 			break;
43100b86a832SChris Mason 	}
43110b86a832SChris Mason 	return 1;
43120b86a832SChris Mason }
4313