xref: /openbmc/linux/fs/btrfs/ctree.c (revision a05a9bb18ae0abec0b513b5fde876c47905fa13e)
16cbd5570SChris Mason /*
2d352ac68SChris Mason  * Copyright (C) 2007,2008 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
19a6b6e75eSChris Mason #include <linux/sched.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
21eb60ceacSChris Mason #include "ctree.h"
22eb60ceacSChris Mason #include "disk-io.h"
237f5c1516SChris Mason #include "transaction.h"
245f39d397SChris Mason #include "print-tree.h"
25925baeddSChris Mason #include "locking.h"
269a8dd150SChris Mason 
27e089f05cSChris Mason static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
28e089f05cSChris Mason 		      *root, struct btrfs_path *path, int level);
29e089f05cSChris Mason static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
30d4dbff95SChris Mason 		      *root, struct btrfs_key *ins_key,
31cc0c5538SChris Mason 		      struct btrfs_path *path, int data_size, int extend);
325f39d397SChris Mason static int push_node_left(struct btrfs_trans_handle *trans,
335f39d397SChris Mason 			  struct btrfs_root *root, struct extent_buffer *dst,
34971a1f66SChris Mason 			  struct extent_buffer *src, int empty);
355f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans,
365f39d397SChris Mason 			      struct btrfs_root *root,
375f39d397SChris Mason 			      struct extent_buffer *dst_buf,
385f39d397SChris Mason 			      struct extent_buffer *src_buf);
39e089f05cSChris Mason static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
40e089f05cSChris Mason 		   struct btrfs_path *path, int level, int slot);
41d97e63b6SChris Mason 
422c90e5d6SChris Mason struct btrfs_path *btrfs_alloc_path(void)
432c90e5d6SChris Mason {
44df24a2b9SChris Mason 	struct btrfs_path *path;
45e00f7308SJeff Mahoney 	path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
46df24a2b9SChris Mason 	return path;
472c90e5d6SChris Mason }
482c90e5d6SChris Mason 
49b4ce94deSChris Mason /*
50b4ce94deSChris Mason  * set all locked nodes in the path to blocking locks.  This should
51b4ce94deSChris Mason  * be done before scheduling
52b4ce94deSChris Mason  */
53b4ce94deSChris Mason noinline void btrfs_set_path_blocking(struct btrfs_path *p)
54b4ce94deSChris Mason {
55b4ce94deSChris Mason 	int i;
56b4ce94deSChris Mason 	for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
57bd681513SChris Mason 		if (!p->nodes[i] || !p->locks[i])
58bd681513SChris Mason 			continue;
59bd681513SChris Mason 		btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
60bd681513SChris Mason 		if (p->locks[i] == BTRFS_READ_LOCK)
61bd681513SChris Mason 			p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
62bd681513SChris Mason 		else if (p->locks[i] == BTRFS_WRITE_LOCK)
63bd681513SChris Mason 			p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
64b4ce94deSChris Mason 	}
65b4ce94deSChris Mason }
66b4ce94deSChris Mason 
67b4ce94deSChris Mason /*
68b4ce94deSChris Mason  * reset all the locked nodes in the patch to spinning locks.
694008c04aSChris Mason  *
704008c04aSChris Mason  * held is used to keep lockdep happy, when lockdep is enabled
714008c04aSChris Mason  * we set held to a blocking lock before we go around and
724008c04aSChris Mason  * retake all the spinlocks in the path.  You can safely use NULL
734008c04aSChris Mason  * for held
74b4ce94deSChris Mason  */
754008c04aSChris Mason noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
76bd681513SChris Mason 					struct extent_buffer *held, int held_rw)
77b4ce94deSChris Mason {
78b4ce94deSChris Mason 	int i;
794008c04aSChris Mason 
804008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC
814008c04aSChris Mason 	/* lockdep really cares that we take all of these spinlocks
824008c04aSChris Mason 	 * in the right order.  If any of the locks in the path are not
834008c04aSChris Mason 	 * currently blocking, it is going to complain.  So, make really
844008c04aSChris Mason 	 * really sure by forcing the path to blocking before we clear
854008c04aSChris Mason 	 * the path blocking.
864008c04aSChris Mason 	 */
87bd681513SChris Mason 	if (held) {
88bd681513SChris Mason 		btrfs_set_lock_blocking_rw(held, held_rw);
89bd681513SChris Mason 		if (held_rw == BTRFS_WRITE_LOCK)
90bd681513SChris Mason 			held_rw = BTRFS_WRITE_LOCK_BLOCKING;
91bd681513SChris Mason 		else if (held_rw == BTRFS_READ_LOCK)
92bd681513SChris Mason 			held_rw = BTRFS_READ_LOCK_BLOCKING;
93bd681513SChris Mason 	}
944008c04aSChris Mason 	btrfs_set_path_blocking(p);
954008c04aSChris Mason #endif
964008c04aSChris Mason 
974008c04aSChris Mason 	for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
98bd681513SChris Mason 		if (p->nodes[i] && p->locks[i]) {
99bd681513SChris Mason 			btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
100bd681513SChris Mason 			if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
101bd681513SChris Mason 				p->locks[i] = BTRFS_WRITE_LOCK;
102bd681513SChris Mason 			else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
103bd681513SChris Mason 				p->locks[i] = BTRFS_READ_LOCK;
104bd681513SChris Mason 		}
105b4ce94deSChris Mason 	}
1064008c04aSChris Mason 
1074008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC
1084008c04aSChris Mason 	if (held)
109bd681513SChris Mason 		btrfs_clear_lock_blocking_rw(held, held_rw);
1104008c04aSChris Mason #endif
111b4ce94deSChris Mason }
112b4ce94deSChris Mason 
113d352ac68SChris Mason /* this also releases the path */
1142c90e5d6SChris Mason void btrfs_free_path(struct btrfs_path *p)
1152c90e5d6SChris Mason {
116ff175d57SJesper Juhl 	if (!p)
117ff175d57SJesper Juhl 		return;
118b3b4aa74SDavid Sterba 	btrfs_release_path(p);
1192c90e5d6SChris Mason 	kmem_cache_free(btrfs_path_cachep, p);
1202c90e5d6SChris Mason }
1212c90e5d6SChris Mason 
122d352ac68SChris Mason /*
123d352ac68SChris Mason  * path release drops references on the extent buffers in the path
124d352ac68SChris Mason  * and it drops any locks held by this path
125d352ac68SChris Mason  *
126d352ac68SChris Mason  * It is safe to call this on paths that no locks or extent buffers held.
127d352ac68SChris Mason  */
128b3b4aa74SDavid Sterba noinline void btrfs_release_path(struct btrfs_path *p)
129eb60ceacSChris Mason {
130eb60ceacSChris Mason 	int i;
131a2135011SChris Mason 
132234b63a0SChris Mason 	for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
1333f157a2fSChris Mason 		p->slots[i] = 0;
134eb60ceacSChris Mason 		if (!p->nodes[i])
135925baeddSChris Mason 			continue;
136925baeddSChris Mason 		if (p->locks[i]) {
137bd681513SChris Mason 			btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
138925baeddSChris Mason 			p->locks[i] = 0;
139925baeddSChris Mason 		}
1405f39d397SChris Mason 		free_extent_buffer(p->nodes[i]);
1413f157a2fSChris Mason 		p->nodes[i] = NULL;
142eb60ceacSChris Mason 	}
143eb60ceacSChris Mason }
144eb60ceacSChris Mason 
145d352ac68SChris Mason /*
146d352ac68SChris Mason  * safely gets a reference on the root node of a tree.  A lock
147d352ac68SChris Mason  * is not taken, so a concurrent writer may put a different node
148d352ac68SChris Mason  * at the root of the tree.  See btrfs_lock_root_node for the
149d352ac68SChris Mason  * looping required.
150d352ac68SChris Mason  *
151d352ac68SChris Mason  * The extent buffer returned by this has a reference taken, so
152d352ac68SChris Mason  * it won't disappear.  It may stop being the root of the tree
153d352ac68SChris Mason  * at any time because there are no locks held.
154d352ac68SChris Mason  */
155925baeddSChris Mason struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
156925baeddSChris Mason {
157925baeddSChris Mason 	struct extent_buffer *eb;
158240f62c8SChris Mason 
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,
2435d4f98a2SYan Zheng 				     buf->start, 0);
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)
2645d4f98a2SYan Zheng 		ret = btrfs_inc_ref(trans, root, cow, 1);
2655d4f98a2SYan Zheng 	else
2665d4f98a2SYan Zheng 		ret = btrfs_inc_ref(trans, root, cow, 0);
2674aec2b52SChris Mason 
268be20aa9dSChris Mason 	if (ret)
269be20aa9dSChris Mason 		return ret;
270be20aa9dSChris Mason 
271be20aa9dSChris Mason 	btrfs_mark_buffer_dirty(cow);
272be20aa9dSChris Mason 	*cow_ret = cow;
273be20aa9dSChris Mason 	return 0;
274be20aa9dSChris Mason }
275be20aa9dSChris Mason 
276d352ac68SChris Mason /*
2775d4f98a2SYan Zheng  * check if the tree block can be shared by multiple trees
2785d4f98a2SYan Zheng  */
2795d4f98a2SYan Zheng int btrfs_block_can_be_shared(struct btrfs_root *root,
2805d4f98a2SYan Zheng 			      struct extent_buffer *buf)
2815d4f98a2SYan Zheng {
2825d4f98a2SYan Zheng 	/*
2835d4f98a2SYan Zheng 	 * Tree blocks not in refernece counted trees and tree roots
2845d4f98a2SYan Zheng 	 * are never shared. If a block was allocated after the last
2855d4f98a2SYan Zheng 	 * snapshot and the block was not allocated by tree relocation,
2865d4f98a2SYan Zheng 	 * we know the block is not shared.
2875d4f98a2SYan Zheng 	 */
2885d4f98a2SYan Zheng 	if (root->ref_cows &&
2895d4f98a2SYan Zheng 	    buf != root->node && buf != root->commit_root &&
2905d4f98a2SYan Zheng 	    (btrfs_header_generation(buf) <=
2915d4f98a2SYan Zheng 	     btrfs_root_last_snapshot(&root->root_item) ||
2925d4f98a2SYan Zheng 	     btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
2935d4f98a2SYan Zheng 		return 1;
2945d4f98a2SYan Zheng #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2955d4f98a2SYan Zheng 	if (root->ref_cows &&
2965d4f98a2SYan Zheng 	    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
2975d4f98a2SYan Zheng 		return 1;
2985d4f98a2SYan Zheng #endif
2995d4f98a2SYan Zheng 	return 0;
3005d4f98a2SYan Zheng }
3015d4f98a2SYan Zheng 
3025d4f98a2SYan Zheng static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
3035d4f98a2SYan Zheng 				       struct btrfs_root *root,
3045d4f98a2SYan Zheng 				       struct extent_buffer *buf,
305f0486c68SYan, Zheng 				       struct extent_buffer *cow,
306f0486c68SYan, Zheng 				       int *last_ref)
3075d4f98a2SYan Zheng {
3085d4f98a2SYan Zheng 	u64 refs;
3095d4f98a2SYan Zheng 	u64 owner;
3105d4f98a2SYan Zheng 	u64 flags;
3115d4f98a2SYan Zheng 	u64 new_flags = 0;
3125d4f98a2SYan Zheng 	int ret;
3135d4f98a2SYan Zheng 
3145d4f98a2SYan Zheng 	/*
3155d4f98a2SYan Zheng 	 * Backrefs update rules:
3165d4f98a2SYan Zheng 	 *
3175d4f98a2SYan Zheng 	 * Always use full backrefs for extent pointers in tree block
3185d4f98a2SYan Zheng 	 * allocated by tree relocation.
3195d4f98a2SYan Zheng 	 *
3205d4f98a2SYan Zheng 	 * If a shared tree block is no longer referenced by its owner
3215d4f98a2SYan Zheng 	 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
3225d4f98a2SYan Zheng 	 * use full backrefs for extent pointers in tree block.
3235d4f98a2SYan Zheng 	 *
3245d4f98a2SYan Zheng 	 * If a tree block is been relocating
3255d4f98a2SYan Zheng 	 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
3265d4f98a2SYan Zheng 	 * use full backrefs for extent pointers in tree block.
3275d4f98a2SYan Zheng 	 * The reason for this is some operations (such as drop tree)
3285d4f98a2SYan Zheng 	 * are only allowed for blocks use full backrefs.
3295d4f98a2SYan Zheng 	 */
3305d4f98a2SYan Zheng 
3315d4f98a2SYan Zheng 	if (btrfs_block_can_be_shared(root, buf)) {
3325d4f98a2SYan Zheng 		ret = btrfs_lookup_extent_info(trans, root, buf->start,
3335d4f98a2SYan Zheng 					       buf->len, &refs, &flags);
3345d4f98a2SYan Zheng 		BUG_ON(ret);
3355d4f98a2SYan Zheng 		BUG_ON(refs == 0);
3365d4f98a2SYan Zheng 	} else {
3375d4f98a2SYan Zheng 		refs = 1;
3385d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
3395d4f98a2SYan Zheng 		    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
3405d4f98a2SYan Zheng 			flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
3415d4f98a2SYan Zheng 		else
3425d4f98a2SYan Zheng 			flags = 0;
3435d4f98a2SYan Zheng 	}
3445d4f98a2SYan Zheng 
3455d4f98a2SYan Zheng 	owner = btrfs_header_owner(buf);
3465d4f98a2SYan Zheng 	BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
3475d4f98a2SYan Zheng 	       !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
3485d4f98a2SYan Zheng 
3495d4f98a2SYan Zheng 	if (refs > 1) {
3505d4f98a2SYan Zheng 		if ((owner == root->root_key.objectid ||
3515d4f98a2SYan Zheng 		     root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
3525d4f98a2SYan Zheng 		    !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
3535d4f98a2SYan Zheng 			ret = btrfs_inc_ref(trans, root, buf, 1);
3545d4f98a2SYan Zheng 			BUG_ON(ret);
3555d4f98a2SYan Zheng 
3565d4f98a2SYan Zheng 			if (root->root_key.objectid ==
3575d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID) {
3585d4f98a2SYan Zheng 				ret = btrfs_dec_ref(trans, root, buf, 0);
3595d4f98a2SYan Zheng 				BUG_ON(ret);
3605d4f98a2SYan Zheng 				ret = btrfs_inc_ref(trans, root, cow, 1);
3615d4f98a2SYan Zheng 				BUG_ON(ret);
3625d4f98a2SYan Zheng 			}
3635d4f98a2SYan Zheng 			new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
3645d4f98a2SYan Zheng 		} else {
3655d4f98a2SYan Zheng 
3665d4f98a2SYan Zheng 			if (root->root_key.objectid ==
3675d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID)
3685d4f98a2SYan Zheng 				ret = btrfs_inc_ref(trans, root, cow, 1);
3695d4f98a2SYan Zheng 			else
3705d4f98a2SYan Zheng 				ret = btrfs_inc_ref(trans, root, cow, 0);
3715d4f98a2SYan Zheng 			BUG_ON(ret);
3725d4f98a2SYan Zheng 		}
3735d4f98a2SYan Zheng 		if (new_flags != 0) {
3745d4f98a2SYan Zheng 			ret = btrfs_set_disk_extent_flags(trans, root,
3755d4f98a2SYan Zheng 							  buf->start,
3765d4f98a2SYan Zheng 							  buf->len,
3775d4f98a2SYan Zheng 							  new_flags, 0);
3785d4f98a2SYan Zheng 			BUG_ON(ret);
3795d4f98a2SYan Zheng 		}
3805d4f98a2SYan Zheng 	} else {
3815d4f98a2SYan Zheng 		if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
3825d4f98a2SYan Zheng 			if (root->root_key.objectid ==
3835d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID)
3845d4f98a2SYan Zheng 				ret = btrfs_inc_ref(trans, root, cow, 1);
3855d4f98a2SYan Zheng 			else
3865d4f98a2SYan Zheng 				ret = btrfs_inc_ref(trans, root, cow, 0);
3875d4f98a2SYan Zheng 			BUG_ON(ret);
3885d4f98a2SYan Zheng 			ret = btrfs_dec_ref(trans, root, buf, 1);
3895d4f98a2SYan Zheng 			BUG_ON(ret);
3905d4f98a2SYan Zheng 		}
3915d4f98a2SYan Zheng 		clean_tree_block(trans, root, buf);
392f0486c68SYan, Zheng 		*last_ref = 1;
3935d4f98a2SYan Zheng 	}
3945d4f98a2SYan Zheng 	return 0;
3955d4f98a2SYan Zheng }
3965d4f98a2SYan Zheng 
3975d4f98a2SYan Zheng /*
398d397712bSChris Mason  * does the dirty work in cow of a single block.  The parent block (if
399d397712bSChris Mason  * supplied) is updated to point to the new cow copy.  The new buffer is marked
400d397712bSChris Mason  * dirty and returned locked.  If you modify the block it needs to be marked
401d397712bSChris Mason  * dirty again.
402d352ac68SChris Mason  *
403d352ac68SChris Mason  * search_start -- an allocation hint for the new block
404d352ac68SChris Mason  *
405d397712bSChris Mason  * empty_size -- a hint that you plan on doing more cow.  This is the size in
406d397712bSChris Mason  * bytes the allocator should try to find free next to the block it returns.
407d397712bSChris Mason  * This is just a hint and may be ignored by the allocator.
408d352ac68SChris Mason  */
409d397712bSChris Mason static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
4105f39d397SChris Mason 			     struct btrfs_root *root,
4115f39d397SChris Mason 			     struct extent_buffer *buf,
4125f39d397SChris Mason 			     struct extent_buffer *parent, int parent_slot,
4135f39d397SChris Mason 			     struct extent_buffer **cow_ret,
4149fa8cfe7SChris Mason 			     u64 search_start, u64 empty_size)
4156702ed49SChris Mason {
4165d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
4175f39d397SChris Mason 	struct extent_buffer *cow;
4187bb86316SChris Mason 	int level;
419f0486c68SYan, Zheng 	int last_ref = 0;
420925baeddSChris Mason 	int unlock_orig = 0;
4215d4f98a2SYan Zheng 	u64 parent_start;
4226702ed49SChris Mason 
423925baeddSChris Mason 	if (*cow_ret == buf)
424925baeddSChris Mason 		unlock_orig = 1;
425925baeddSChris Mason 
426b9447ef8SChris Mason 	btrfs_assert_tree_locked(buf);
427925baeddSChris Mason 
4287bb86316SChris Mason 	WARN_ON(root->ref_cows && trans->transid !=
4297bb86316SChris Mason 		root->fs_info->running_transaction->transid);
4306702ed49SChris Mason 	WARN_ON(root->ref_cows && trans->transid != root->last_trans);
4315f39d397SChris Mason 
4327bb86316SChris Mason 	level = btrfs_header_level(buf);
43331840ae1SZheng Yan 
4345d4f98a2SYan Zheng 	if (level == 0)
4355d4f98a2SYan Zheng 		btrfs_item_key(buf, &disk_key, 0);
4365d4f98a2SYan Zheng 	else
4375d4f98a2SYan Zheng 		btrfs_node_key(buf, &disk_key, 0);
4385d4f98a2SYan Zheng 
4395d4f98a2SYan Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
4405d4f98a2SYan Zheng 		if (parent)
4415d4f98a2SYan Zheng 			parent_start = parent->start;
4425d4f98a2SYan Zheng 		else
4435d4f98a2SYan Zheng 			parent_start = 0;
4445d4f98a2SYan Zheng 	} else
4455d4f98a2SYan Zheng 		parent_start = 0;
4465d4f98a2SYan Zheng 
4475d4f98a2SYan Zheng 	cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
4485d4f98a2SYan Zheng 				     root->root_key.objectid, &disk_key,
4495d4f98a2SYan Zheng 				     level, search_start, empty_size);
4506702ed49SChris Mason 	if (IS_ERR(cow))
4516702ed49SChris Mason 		return PTR_ERR(cow);
4526702ed49SChris Mason 
453b4ce94deSChris Mason 	/* cow is set to blocking by btrfs_init_new_buffer */
454b4ce94deSChris Mason 
4555f39d397SChris Mason 	copy_extent_buffer(cow, buf, 0, 0, cow->len);
456db94535dSChris Mason 	btrfs_set_header_bytenr(cow, cow->start);
4575f39d397SChris Mason 	btrfs_set_header_generation(cow, trans->transid);
4585d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
4595d4f98a2SYan Zheng 	btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
4605d4f98a2SYan Zheng 				     BTRFS_HEADER_FLAG_RELOC);
4615d4f98a2SYan Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4625d4f98a2SYan Zheng 		btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
4635d4f98a2SYan Zheng 	else
4645f39d397SChris Mason 		btrfs_set_header_owner(cow, root->root_key.objectid);
4656702ed49SChris Mason 
4662b82032cSYan Zheng 	write_extent_buffer(cow, root->fs_info->fsid,
4672b82032cSYan Zheng 			    (unsigned long)btrfs_header_fsid(cow),
4682b82032cSYan Zheng 			    BTRFS_FSID_SIZE);
4692b82032cSYan Zheng 
470f0486c68SYan, Zheng 	update_ref_for_cow(trans, root, buf, cow, &last_ref);
4711a40e23bSZheng Yan 
4723fd0a558SYan, Zheng 	if (root->ref_cows)
4733fd0a558SYan, Zheng 		btrfs_reloc_cow_block(trans, root, buf, cow);
4743fd0a558SYan, Zheng 
4756702ed49SChris Mason 	if (buf == root->node) {
476925baeddSChris Mason 		WARN_ON(parent && parent != buf);
4775d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
4785d4f98a2SYan Zheng 		    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
4795d4f98a2SYan Zheng 			parent_start = buf->start;
4805d4f98a2SYan Zheng 		else
4815d4f98a2SYan Zheng 			parent_start = 0;
482925baeddSChris Mason 
4835f39d397SChris Mason 		extent_buffer_get(cow);
484240f62c8SChris Mason 		rcu_assign_pointer(root->node, cow);
485925baeddSChris Mason 
486f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, buf, parent_start,
487f0486c68SYan, Zheng 				      last_ref);
4885f39d397SChris Mason 		free_extent_buffer(buf);
4890b86a832SChris Mason 		add_root_to_dirty_list(root);
4906702ed49SChris Mason 	} else {
4915d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4925d4f98a2SYan Zheng 			parent_start = parent->start;
4935d4f98a2SYan Zheng 		else
4945d4f98a2SYan Zheng 			parent_start = 0;
4955d4f98a2SYan Zheng 
4965d4f98a2SYan Zheng 		WARN_ON(trans->transid != btrfs_header_generation(parent));
4975f39d397SChris Mason 		btrfs_set_node_blockptr(parent, parent_slot,
498db94535dSChris Mason 					cow->start);
49974493f7aSChris Mason 		btrfs_set_node_ptr_generation(parent, parent_slot,
50074493f7aSChris Mason 					      trans->transid);
5016702ed49SChris Mason 		btrfs_mark_buffer_dirty(parent);
502f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, buf, parent_start,
503f0486c68SYan, Zheng 				      last_ref);
5046702ed49SChris Mason 	}
505925baeddSChris Mason 	if (unlock_orig)
506925baeddSChris Mason 		btrfs_tree_unlock(buf);
5075f39d397SChris Mason 	free_extent_buffer(buf);
5086702ed49SChris Mason 	btrfs_mark_buffer_dirty(cow);
5096702ed49SChris Mason 	*cow_ret = cow;
5106702ed49SChris Mason 	return 0;
5116702ed49SChris Mason }
5126702ed49SChris Mason 
5135d4f98a2SYan Zheng static inline int should_cow_block(struct btrfs_trans_handle *trans,
5145d4f98a2SYan Zheng 				   struct btrfs_root *root,
5155d4f98a2SYan Zheng 				   struct extent_buffer *buf)
5165d4f98a2SYan Zheng {
5175d4f98a2SYan Zheng 	if (btrfs_header_generation(buf) == trans->transid &&
5185d4f98a2SYan Zheng 	    !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
5195d4f98a2SYan Zheng 	    !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
5205d4f98a2SYan Zheng 	      btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
5215d4f98a2SYan Zheng 		return 0;
5225d4f98a2SYan Zheng 	return 1;
5235d4f98a2SYan Zheng }
5245d4f98a2SYan Zheng 
525d352ac68SChris Mason /*
526d352ac68SChris Mason  * cows a single block, see __btrfs_cow_block for the real work.
527d352ac68SChris Mason  * This version of it has extra checks so that a block isn't cow'd more than
528d352ac68SChris Mason  * once per transaction, as long as it hasn't been written yet
529d352ac68SChris Mason  */
530d397712bSChris Mason noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
5315f39d397SChris Mason 		    struct btrfs_root *root, struct extent_buffer *buf,
5325f39d397SChris Mason 		    struct extent_buffer *parent, int parent_slot,
5339fa8cfe7SChris Mason 		    struct extent_buffer **cow_ret)
53402217ed2SChris Mason {
5356702ed49SChris Mason 	u64 search_start;
536f510cfecSChris Mason 	int ret;
537dc17ff8fSChris Mason 
538ccd467d6SChris Mason 	if (trans->transaction != root->fs_info->running_transaction) {
539d397712bSChris Mason 		printk(KERN_CRIT "trans %llu running %llu\n",
540d397712bSChris Mason 		       (unsigned long long)trans->transid,
541d397712bSChris Mason 		       (unsigned long long)
542ccd467d6SChris Mason 		       root->fs_info->running_transaction->transid);
543ccd467d6SChris Mason 		WARN_ON(1);
544ccd467d6SChris Mason 	}
545ccd467d6SChris Mason 	if (trans->transid != root->fs_info->generation) {
546d397712bSChris Mason 		printk(KERN_CRIT "trans %llu running %llu\n",
547d397712bSChris Mason 		       (unsigned long long)trans->transid,
548d397712bSChris Mason 		       (unsigned long long)root->fs_info->generation);
549ccd467d6SChris Mason 		WARN_ON(1);
550ccd467d6SChris Mason 	}
551dc17ff8fSChris Mason 
5525d4f98a2SYan Zheng 	if (!should_cow_block(trans, root, buf)) {
55302217ed2SChris Mason 		*cow_ret = buf;
55402217ed2SChris Mason 		return 0;
55502217ed2SChris Mason 	}
556c487685dSChris Mason 
5570b86a832SChris Mason 	search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
558b4ce94deSChris Mason 
559b4ce94deSChris Mason 	if (parent)
560b4ce94deSChris Mason 		btrfs_set_lock_blocking(parent);
561b4ce94deSChris Mason 	btrfs_set_lock_blocking(buf);
562b4ce94deSChris Mason 
563f510cfecSChris Mason 	ret = __btrfs_cow_block(trans, root, buf, parent,
5649fa8cfe7SChris Mason 				 parent_slot, cow_ret, search_start, 0);
5651abe9b8aSliubo 
5661abe9b8aSliubo 	trace_btrfs_cow_block(root, buf, *cow_ret);
5671abe9b8aSliubo 
568f510cfecSChris Mason 	return ret;
5692c90e5d6SChris Mason }
5706702ed49SChris Mason 
571d352ac68SChris Mason /*
572d352ac68SChris Mason  * helper function for defrag to decide if two blocks pointed to by a
573d352ac68SChris Mason  * node are actually close by
574d352ac68SChris Mason  */
5756b80053dSChris Mason static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
5766702ed49SChris Mason {
5776b80053dSChris Mason 	if (blocknr < other && other - (blocknr + blocksize) < 32768)
5786702ed49SChris Mason 		return 1;
5796b80053dSChris Mason 	if (blocknr > other && blocknr - (other + blocksize) < 32768)
5806702ed49SChris Mason 		return 1;
58102217ed2SChris Mason 	return 0;
58202217ed2SChris Mason }
58302217ed2SChris Mason 
584081e9573SChris Mason /*
585081e9573SChris Mason  * compare two keys in a memcmp fashion
586081e9573SChris Mason  */
587081e9573SChris Mason static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
588081e9573SChris Mason {
589081e9573SChris Mason 	struct btrfs_key k1;
590081e9573SChris Mason 
591081e9573SChris Mason 	btrfs_disk_key_to_cpu(&k1, disk);
592081e9573SChris Mason 
59320736abaSDiego Calleja 	return btrfs_comp_cpu_keys(&k1, k2);
594081e9573SChris Mason }
595081e9573SChris Mason 
596f3465ca4SJosef Bacik /*
597f3465ca4SJosef Bacik  * same as comp_keys only with two btrfs_key's
598f3465ca4SJosef Bacik  */
5995d4f98a2SYan Zheng int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
600f3465ca4SJosef Bacik {
601f3465ca4SJosef Bacik 	if (k1->objectid > k2->objectid)
602f3465ca4SJosef Bacik 		return 1;
603f3465ca4SJosef Bacik 	if (k1->objectid < k2->objectid)
604f3465ca4SJosef Bacik 		return -1;
605f3465ca4SJosef Bacik 	if (k1->type > k2->type)
606f3465ca4SJosef Bacik 		return 1;
607f3465ca4SJosef Bacik 	if (k1->type < k2->type)
608f3465ca4SJosef Bacik 		return -1;
609f3465ca4SJosef Bacik 	if (k1->offset > k2->offset)
610f3465ca4SJosef Bacik 		return 1;
611f3465ca4SJosef Bacik 	if (k1->offset < k2->offset)
612f3465ca4SJosef Bacik 		return -1;
613f3465ca4SJosef Bacik 	return 0;
614f3465ca4SJosef Bacik }
615081e9573SChris Mason 
616d352ac68SChris Mason /*
617d352ac68SChris Mason  * this is used by the defrag code to go through all the
618d352ac68SChris Mason  * leaves pointed to by a node and reallocate them so that
619d352ac68SChris Mason  * disk order is close to key order
620d352ac68SChris Mason  */
6216702ed49SChris Mason int btrfs_realloc_node(struct btrfs_trans_handle *trans,
6225f39d397SChris Mason 		       struct btrfs_root *root, struct extent_buffer *parent,
623a6b6e75eSChris Mason 		       int start_slot, int cache_only, u64 *last_ret,
624a6b6e75eSChris Mason 		       struct btrfs_key *progress)
6256702ed49SChris Mason {
6266b80053dSChris Mason 	struct extent_buffer *cur;
6276702ed49SChris Mason 	u64 blocknr;
628ca7a79adSChris Mason 	u64 gen;
629e9d0b13bSChris Mason 	u64 search_start = *last_ret;
630e9d0b13bSChris Mason 	u64 last_block = 0;
6316702ed49SChris Mason 	u64 other;
6326702ed49SChris Mason 	u32 parent_nritems;
6336702ed49SChris Mason 	int end_slot;
6346702ed49SChris Mason 	int i;
6356702ed49SChris Mason 	int err = 0;
636f2183bdeSChris Mason 	int parent_level;
6376b80053dSChris Mason 	int uptodate;
6386b80053dSChris Mason 	u32 blocksize;
639081e9573SChris Mason 	int progress_passed = 0;
640081e9573SChris Mason 	struct btrfs_disk_key disk_key;
6416702ed49SChris Mason 
6425708b959SChris Mason 	parent_level = btrfs_header_level(parent);
6435708b959SChris Mason 	if (cache_only && parent_level != 1)
6445708b959SChris Mason 		return 0;
6455708b959SChris Mason 
646d397712bSChris Mason 	if (trans->transaction != root->fs_info->running_transaction)
6476702ed49SChris Mason 		WARN_ON(1);
648d397712bSChris Mason 	if (trans->transid != root->fs_info->generation)
6496702ed49SChris Mason 		WARN_ON(1);
65086479a04SChris Mason 
6516b80053dSChris Mason 	parent_nritems = btrfs_header_nritems(parent);
6526b80053dSChris Mason 	blocksize = btrfs_level_size(root, parent_level - 1);
6536702ed49SChris Mason 	end_slot = parent_nritems;
6546702ed49SChris Mason 
6556702ed49SChris Mason 	if (parent_nritems == 1)
6566702ed49SChris Mason 		return 0;
6576702ed49SChris Mason 
658b4ce94deSChris Mason 	btrfs_set_lock_blocking(parent);
659b4ce94deSChris Mason 
6606702ed49SChris Mason 	for (i = start_slot; i < end_slot; i++) {
6616702ed49SChris Mason 		int close = 1;
662a6b6e75eSChris Mason 
663081e9573SChris Mason 		btrfs_node_key(parent, &disk_key, i);
664081e9573SChris Mason 		if (!progress_passed && comp_keys(&disk_key, progress) < 0)
665081e9573SChris Mason 			continue;
666081e9573SChris Mason 
667081e9573SChris Mason 		progress_passed = 1;
6686b80053dSChris Mason 		blocknr = btrfs_node_blockptr(parent, i);
669ca7a79adSChris Mason 		gen = btrfs_node_ptr_generation(parent, i);
670e9d0b13bSChris Mason 		if (last_block == 0)
671e9d0b13bSChris Mason 			last_block = blocknr;
6725708b959SChris Mason 
6736702ed49SChris Mason 		if (i > 0) {
6746b80053dSChris Mason 			other = btrfs_node_blockptr(parent, i - 1);
6756b80053dSChris Mason 			close = close_blocks(blocknr, other, blocksize);
6766702ed49SChris Mason 		}
6770ef3e66bSChris Mason 		if (!close && i < end_slot - 2) {
6786b80053dSChris Mason 			other = btrfs_node_blockptr(parent, i + 1);
6796b80053dSChris Mason 			close = close_blocks(blocknr, other, blocksize);
6806702ed49SChris Mason 		}
681e9d0b13bSChris Mason 		if (close) {
682e9d0b13bSChris Mason 			last_block = blocknr;
6836702ed49SChris Mason 			continue;
684e9d0b13bSChris Mason 		}
6856702ed49SChris Mason 
6866b80053dSChris Mason 		cur = btrfs_find_tree_block(root, blocknr, blocksize);
6876b80053dSChris Mason 		if (cur)
6881259ab75SChris Mason 			uptodate = btrfs_buffer_uptodate(cur, gen);
6896b80053dSChris Mason 		else
6906b80053dSChris Mason 			uptodate = 0;
6915708b959SChris Mason 		if (!cur || !uptodate) {
6926702ed49SChris Mason 			if (cache_only) {
6936b80053dSChris Mason 				free_extent_buffer(cur);
6946702ed49SChris Mason 				continue;
6956702ed49SChris Mason 			}
6966b80053dSChris Mason 			if (!cur) {
6976b80053dSChris Mason 				cur = read_tree_block(root, blocknr,
698ca7a79adSChris Mason 							 blocksize, gen);
69997d9a8a4STsutomu Itoh 				if (!cur)
70097d9a8a4STsutomu Itoh 					return -EIO;
7016b80053dSChris Mason 			} else if (!uptodate) {
702ca7a79adSChris Mason 				btrfs_read_buffer(cur, gen);
7036702ed49SChris Mason 			}
704f2183bdeSChris Mason 		}
705e9d0b13bSChris Mason 		if (search_start == 0)
7066b80053dSChris Mason 			search_start = last_block;
707e9d0b13bSChris Mason 
708e7a84565SChris Mason 		btrfs_tree_lock(cur);
709b4ce94deSChris Mason 		btrfs_set_lock_blocking(cur);
7106b80053dSChris Mason 		err = __btrfs_cow_block(trans, root, cur, parent, i,
711e7a84565SChris Mason 					&cur, search_start,
7126b80053dSChris Mason 					min(16 * blocksize,
7139fa8cfe7SChris Mason 					    (end_slot - i) * blocksize));
714252c38f0SYan 		if (err) {
715e7a84565SChris Mason 			btrfs_tree_unlock(cur);
7166b80053dSChris Mason 			free_extent_buffer(cur);
7176702ed49SChris Mason 			break;
718252c38f0SYan 		}
719e7a84565SChris Mason 		search_start = cur->start;
720e7a84565SChris Mason 		last_block = cur->start;
721f2183bdeSChris Mason 		*last_ret = search_start;
722e7a84565SChris Mason 		btrfs_tree_unlock(cur);
723e7a84565SChris Mason 		free_extent_buffer(cur);
7246702ed49SChris Mason 	}
7256702ed49SChris Mason 	return err;
7266702ed49SChris Mason }
7276702ed49SChris Mason 
72874123bd7SChris Mason /*
72974123bd7SChris Mason  * The leaf data grows from end-to-front in the node.
73074123bd7SChris Mason  * this returns the address of the start of the last item,
73174123bd7SChris Mason  * which is the stop of the leaf data stack
73274123bd7SChris Mason  */
733123abc88SChris Mason static inline unsigned int leaf_data_end(struct btrfs_root *root,
7345f39d397SChris Mason 					 struct extent_buffer *leaf)
735be0e5c09SChris Mason {
7365f39d397SChris Mason 	u32 nr = btrfs_header_nritems(leaf);
737be0e5c09SChris Mason 	if (nr == 0)
738123abc88SChris Mason 		return BTRFS_LEAF_DATA_SIZE(root);
7395f39d397SChris Mason 	return btrfs_item_offset_nr(leaf, nr - 1);
740be0e5c09SChris Mason }
741be0e5c09SChris Mason 
742aa5d6bedSChris Mason 
74374123bd7SChris Mason /*
7445f39d397SChris Mason  * search for key in the extent_buffer.  The items start at offset p,
7455f39d397SChris Mason  * and they are item_size apart.  There are 'max' items in p.
7465f39d397SChris Mason  *
74774123bd7SChris Mason  * the slot in the array is returned via slot, and it points to
74874123bd7SChris Mason  * the place where you would insert key if it is not found in
74974123bd7SChris Mason  * the array.
75074123bd7SChris Mason  *
75174123bd7SChris Mason  * slot may point to max if the key is bigger than all of the keys
75274123bd7SChris Mason  */
753e02119d5SChris Mason static noinline int generic_bin_search(struct extent_buffer *eb,
754e02119d5SChris Mason 				       unsigned long p,
7555f39d397SChris Mason 				       int item_size, struct btrfs_key *key,
756be0e5c09SChris Mason 				       int max, int *slot)
757be0e5c09SChris Mason {
758be0e5c09SChris Mason 	int low = 0;
759be0e5c09SChris Mason 	int high = max;
760be0e5c09SChris Mason 	int mid;
761be0e5c09SChris Mason 	int ret;
762479965d6SChris Mason 	struct btrfs_disk_key *tmp = NULL;
7635f39d397SChris Mason 	struct btrfs_disk_key unaligned;
7645f39d397SChris Mason 	unsigned long offset;
7655f39d397SChris Mason 	char *kaddr = NULL;
7665f39d397SChris Mason 	unsigned long map_start = 0;
7675f39d397SChris Mason 	unsigned long map_len = 0;
768479965d6SChris Mason 	int err;
769be0e5c09SChris Mason 
770be0e5c09SChris Mason 	while (low < high) {
771be0e5c09SChris Mason 		mid = (low + high) / 2;
7725f39d397SChris Mason 		offset = p + mid * item_size;
7735f39d397SChris Mason 
774a6591715SChris Mason 		if (!kaddr || offset < map_start ||
7755f39d397SChris Mason 		    (offset + sizeof(struct btrfs_disk_key)) >
7765f39d397SChris Mason 		    map_start + map_len) {
777934d375bSChris Mason 
778934d375bSChris Mason 			err = map_private_extent_buffer(eb, offset,
779479965d6SChris Mason 						sizeof(struct btrfs_disk_key),
780a6591715SChris Mason 						&kaddr, &map_start, &map_len);
7815f39d397SChris Mason 
782479965d6SChris Mason 			if (!err) {
783479965d6SChris Mason 				tmp = (struct btrfs_disk_key *)(kaddr + offset -
784479965d6SChris Mason 							map_start);
785479965d6SChris Mason 			} else {
7865f39d397SChris Mason 				read_extent_buffer(eb, &unaligned,
7875f39d397SChris Mason 						   offset, sizeof(unaligned));
7885f39d397SChris Mason 				tmp = &unaligned;
789479965d6SChris Mason 			}
790479965d6SChris Mason 
7915f39d397SChris Mason 		} else {
7925f39d397SChris Mason 			tmp = (struct btrfs_disk_key *)(kaddr + offset -
7935f39d397SChris Mason 							map_start);
7945f39d397SChris Mason 		}
795be0e5c09SChris Mason 		ret = comp_keys(tmp, key);
796be0e5c09SChris Mason 
797be0e5c09SChris Mason 		if (ret < 0)
798be0e5c09SChris Mason 			low = mid + 1;
799be0e5c09SChris Mason 		else if (ret > 0)
800be0e5c09SChris Mason 			high = mid;
801be0e5c09SChris Mason 		else {
802be0e5c09SChris Mason 			*slot = mid;
803be0e5c09SChris Mason 			return 0;
804be0e5c09SChris Mason 		}
805be0e5c09SChris Mason 	}
806be0e5c09SChris Mason 	*slot = low;
807be0e5c09SChris Mason 	return 1;
808be0e5c09SChris Mason }
809be0e5c09SChris Mason 
81097571fd0SChris Mason /*
81197571fd0SChris Mason  * simple bin_search frontend that does the right thing for
81297571fd0SChris Mason  * leaves vs nodes
81397571fd0SChris Mason  */
8145f39d397SChris Mason static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
8155f39d397SChris Mason 		      int level, int *slot)
816be0e5c09SChris Mason {
8175f39d397SChris Mason 	if (level == 0) {
8185f39d397SChris Mason 		return generic_bin_search(eb,
8195f39d397SChris Mason 					  offsetof(struct btrfs_leaf, items),
8200783fcfcSChris Mason 					  sizeof(struct btrfs_item),
8215f39d397SChris Mason 					  key, btrfs_header_nritems(eb),
8227518a238SChris Mason 					  slot);
823be0e5c09SChris Mason 	} else {
8245f39d397SChris Mason 		return generic_bin_search(eb,
8255f39d397SChris Mason 					  offsetof(struct btrfs_node, ptrs),
826123abc88SChris Mason 					  sizeof(struct btrfs_key_ptr),
8275f39d397SChris Mason 					  key, btrfs_header_nritems(eb),
8287518a238SChris Mason 					  slot);
829be0e5c09SChris Mason 	}
830be0e5c09SChris Mason 	return -1;
831be0e5c09SChris Mason }
832be0e5c09SChris Mason 
8335d4f98a2SYan Zheng int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
8345d4f98a2SYan Zheng 		     int level, int *slot)
8355d4f98a2SYan Zheng {
8365d4f98a2SYan Zheng 	return bin_search(eb, key, level, slot);
8375d4f98a2SYan Zheng }
8385d4f98a2SYan Zheng 
839f0486c68SYan, Zheng static void root_add_used(struct btrfs_root *root, u32 size)
840f0486c68SYan, Zheng {
841f0486c68SYan, Zheng 	spin_lock(&root->accounting_lock);
842f0486c68SYan, Zheng 	btrfs_set_root_used(&root->root_item,
843f0486c68SYan, Zheng 			    btrfs_root_used(&root->root_item) + size);
844f0486c68SYan, Zheng 	spin_unlock(&root->accounting_lock);
845f0486c68SYan, Zheng }
846f0486c68SYan, Zheng 
847f0486c68SYan, Zheng static void root_sub_used(struct btrfs_root *root, u32 size)
848f0486c68SYan, Zheng {
849f0486c68SYan, Zheng 	spin_lock(&root->accounting_lock);
850f0486c68SYan, Zheng 	btrfs_set_root_used(&root->root_item,
851f0486c68SYan, Zheng 			    btrfs_root_used(&root->root_item) - size);
852f0486c68SYan, Zheng 	spin_unlock(&root->accounting_lock);
853f0486c68SYan, Zheng }
854f0486c68SYan, Zheng 
855d352ac68SChris Mason /* given a node and slot number, this reads the blocks it points to.  The
856d352ac68SChris Mason  * extent buffer is returned with a reference taken (but unlocked).
857d352ac68SChris Mason  * NULL is returned on error.
858d352ac68SChris Mason  */
859e02119d5SChris Mason static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
8605f39d397SChris Mason 				   struct extent_buffer *parent, int slot)
861bb803951SChris Mason {
862ca7a79adSChris Mason 	int level = btrfs_header_level(parent);
863bb803951SChris Mason 	if (slot < 0)
864bb803951SChris Mason 		return NULL;
8655f39d397SChris Mason 	if (slot >= btrfs_header_nritems(parent))
866bb803951SChris Mason 		return NULL;
867ca7a79adSChris Mason 
868ca7a79adSChris Mason 	BUG_ON(level == 0);
869ca7a79adSChris Mason 
870db94535dSChris Mason 	return read_tree_block(root, btrfs_node_blockptr(parent, slot),
871ca7a79adSChris Mason 		       btrfs_level_size(root, level - 1),
872ca7a79adSChris Mason 		       btrfs_node_ptr_generation(parent, slot));
873bb803951SChris Mason }
874bb803951SChris Mason 
875d352ac68SChris Mason /*
876d352ac68SChris Mason  * node level balancing, used to make sure nodes are in proper order for
877d352ac68SChris Mason  * item deletion.  We balance from the top down, so we have to make sure
878d352ac68SChris Mason  * that a deletion won't leave an node completely empty later on.
879d352ac68SChris Mason  */
880e02119d5SChris Mason static noinline int balance_level(struct btrfs_trans_handle *trans,
88198ed5174SChris Mason 			 struct btrfs_root *root,
88298ed5174SChris Mason 			 struct btrfs_path *path, int level)
883bb803951SChris Mason {
8845f39d397SChris Mason 	struct extent_buffer *right = NULL;
8855f39d397SChris Mason 	struct extent_buffer *mid;
8865f39d397SChris Mason 	struct extent_buffer *left = NULL;
8875f39d397SChris Mason 	struct extent_buffer *parent = NULL;
888bb803951SChris Mason 	int ret = 0;
889bb803951SChris Mason 	int wret;
890bb803951SChris Mason 	int pslot;
891bb803951SChris Mason 	int orig_slot = path->slots[level];
89279f95c82SChris Mason 	u64 orig_ptr;
893bb803951SChris Mason 
894bb803951SChris Mason 	if (level == 0)
895bb803951SChris Mason 		return 0;
896bb803951SChris Mason 
8975f39d397SChris Mason 	mid = path->nodes[level];
898b4ce94deSChris Mason 
899bd681513SChris Mason 	WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
900bd681513SChris Mason 		path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
9017bb86316SChris Mason 	WARN_ON(btrfs_header_generation(mid) != trans->transid);
9027bb86316SChris Mason 
9031d4f8a0cSChris Mason 	orig_ptr = btrfs_node_blockptr(mid, orig_slot);
90479f95c82SChris Mason 
905*a05a9bb1SLi Zefan 	if (level < BTRFS_MAX_LEVEL - 1) {
9065f39d397SChris Mason 		parent = path->nodes[level + 1];
907bb803951SChris Mason 		pslot = path->slots[level + 1];
908*a05a9bb1SLi Zefan 	}
909bb803951SChris Mason 
91040689478SChris Mason 	/*
91140689478SChris Mason 	 * deal with the case where there is only one pointer in the root
91240689478SChris Mason 	 * by promoting the node below to a root
91340689478SChris Mason 	 */
9145f39d397SChris Mason 	if (!parent) {
9155f39d397SChris Mason 		struct extent_buffer *child;
916bb803951SChris Mason 
9175f39d397SChris Mason 		if (btrfs_header_nritems(mid) != 1)
918bb803951SChris Mason 			return 0;
919bb803951SChris Mason 
920bb803951SChris Mason 		/* promote the child to a root */
9215f39d397SChris Mason 		child = read_node_slot(root, mid, 0);
9227951f3ceSJeff Mahoney 		BUG_ON(!child);
923925baeddSChris Mason 		btrfs_tree_lock(child);
924b4ce94deSChris Mason 		btrfs_set_lock_blocking(child);
9259fa8cfe7SChris Mason 		ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
926f0486c68SYan, Zheng 		if (ret) {
927f0486c68SYan, Zheng 			btrfs_tree_unlock(child);
928f0486c68SYan, Zheng 			free_extent_buffer(child);
929f0486c68SYan, Zheng 			goto enospc;
930f0486c68SYan, Zheng 		}
9312f375ab9SYan 
932240f62c8SChris Mason 		rcu_assign_pointer(root->node, child);
933925baeddSChris Mason 
9340b86a832SChris Mason 		add_root_to_dirty_list(root);
935925baeddSChris Mason 		btrfs_tree_unlock(child);
936b4ce94deSChris Mason 
937925baeddSChris Mason 		path->locks[level] = 0;
938bb803951SChris Mason 		path->nodes[level] = NULL;
9395f39d397SChris Mason 		clean_tree_block(trans, root, mid);
940925baeddSChris Mason 		btrfs_tree_unlock(mid);
941bb803951SChris Mason 		/* once for the path */
9425f39d397SChris Mason 		free_extent_buffer(mid);
943f0486c68SYan, Zheng 
944f0486c68SYan, Zheng 		root_sub_used(root, mid->len);
945f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, mid, 0, 1);
946bb803951SChris Mason 		/* once for the root ptr */
9475f39d397SChris Mason 		free_extent_buffer(mid);
948f0486c68SYan, Zheng 		return 0;
949bb803951SChris Mason 	}
9505f39d397SChris Mason 	if (btrfs_header_nritems(mid) >
951123abc88SChris Mason 	    BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
952bb803951SChris Mason 		return 0;
953bb803951SChris Mason 
954559af821SAndi Kleen 	btrfs_header_nritems(mid);
95554aa1f4dSChris Mason 
9565f39d397SChris Mason 	left = read_node_slot(root, parent, pslot - 1);
9575f39d397SChris Mason 	if (left) {
958925baeddSChris Mason 		btrfs_tree_lock(left);
959b4ce94deSChris Mason 		btrfs_set_lock_blocking(left);
9605f39d397SChris Mason 		wret = btrfs_cow_block(trans, root, left,
9619fa8cfe7SChris Mason 				       parent, pslot - 1, &left);
96254aa1f4dSChris Mason 		if (wret) {
96354aa1f4dSChris Mason 			ret = wret;
96454aa1f4dSChris Mason 			goto enospc;
96554aa1f4dSChris Mason 		}
9662cc58cf2SChris Mason 	}
9675f39d397SChris Mason 	right = read_node_slot(root, parent, pslot + 1);
9685f39d397SChris Mason 	if (right) {
969925baeddSChris Mason 		btrfs_tree_lock(right);
970b4ce94deSChris Mason 		btrfs_set_lock_blocking(right);
9715f39d397SChris Mason 		wret = btrfs_cow_block(trans, root, right,
9729fa8cfe7SChris Mason 				       parent, pslot + 1, &right);
9732cc58cf2SChris Mason 		if (wret) {
9742cc58cf2SChris Mason 			ret = wret;
9752cc58cf2SChris Mason 			goto enospc;
9762cc58cf2SChris Mason 		}
9772cc58cf2SChris Mason 	}
9782cc58cf2SChris Mason 
9792cc58cf2SChris Mason 	/* first, try to make some room in the middle buffer */
9805f39d397SChris Mason 	if (left) {
9815f39d397SChris Mason 		orig_slot += btrfs_header_nritems(left);
982bce4eae9SChris Mason 		wret = push_node_left(trans, root, left, mid, 1);
98379f95c82SChris Mason 		if (wret < 0)
98479f95c82SChris Mason 			ret = wret;
985559af821SAndi Kleen 		btrfs_header_nritems(mid);
986bb803951SChris Mason 	}
98779f95c82SChris Mason 
98879f95c82SChris Mason 	/*
98979f95c82SChris Mason 	 * then try to empty the right most buffer into the middle
99079f95c82SChris Mason 	 */
9915f39d397SChris Mason 	if (right) {
992971a1f66SChris Mason 		wret = push_node_left(trans, root, mid, right, 1);
99354aa1f4dSChris Mason 		if (wret < 0 && wret != -ENOSPC)
99479f95c82SChris Mason 			ret = wret;
9955f39d397SChris Mason 		if (btrfs_header_nritems(right) == 0) {
9965f39d397SChris Mason 			clean_tree_block(trans, root, right);
997925baeddSChris Mason 			btrfs_tree_unlock(right);
998e089f05cSChris Mason 			wret = del_ptr(trans, root, path, level + 1, pslot +
999e089f05cSChris Mason 				       1);
1000bb803951SChris Mason 			if (wret)
1001bb803951SChris Mason 				ret = wret;
1002f0486c68SYan, Zheng 			root_sub_used(root, right->len);
1003f0486c68SYan, Zheng 			btrfs_free_tree_block(trans, root, right, 0, 1);
1004f0486c68SYan, Zheng 			free_extent_buffer(right);
1005f0486c68SYan, Zheng 			right = NULL;
1006bb803951SChris Mason 		} else {
10075f39d397SChris Mason 			struct btrfs_disk_key right_key;
10085f39d397SChris Mason 			btrfs_node_key(right, &right_key, 0);
10095f39d397SChris Mason 			btrfs_set_node_key(parent, &right_key, pslot + 1);
10105f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
1011bb803951SChris Mason 		}
1012bb803951SChris Mason 	}
10135f39d397SChris Mason 	if (btrfs_header_nritems(mid) == 1) {
101479f95c82SChris Mason 		/*
101579f95c82SChris Mason 		 * we're not allowed to leave a node with one item in the
101679f95c82SChris Mason 		 * tree during a delete.  A deletion from lower in the tree
101779f95c82SChris Mason 		 * could try to delete the only pointer in this node.
101879f95c82SChris Mason 		 * So, pull some keys from the left.
101979f95c82SChris Mason 		 * There has to be a left pointer at this point because
102079f95c82SChris Mason 		 * otherwise we would have pulled some pointers from the
102179f95c82SChris Mason 		 * right
102279f95c82SChris Mason 		 */
10235f39d397SChris Mason 		BUG_ON(!left);
10245f39d397SChris Mason 		wret = balance_node_right(trans, root, mid, left);
102554aa1f4dSChris Mason 		if (wret < 0) {
102679f95c82SChris Mason 			ret = wret;
102754aa1f4dSChris Mason 			goto enospc;
102854aa1f4dSChris Mason 		}
1029bce4eae9SChris Mason 		if (wret == 1) {
1030bce4eae9SChris Mason 			wret = push_node_left(trans, root, left, mid, 1);
1031bce4eae9SChris Mason 			if (wret < 0)
1032bce4eae9SChris Mason 				ret = wret;
1033bce4eae9SChris Mason 		}
103479f95c82SChris Mason 		BUG_ON(wret == 1);
103579f95c82SChris Mason 	}
10365f39d397SChris Mason 	if (btrfs_header_nritems(mid) == 0) {
10375f39d397SChris Mason 		clean_tree_block(trans, root, mid);
1038925baeddSChris Mason 		btrfs_tree_unlock(mid);
1039e089f05cSChris Mason 		wret = del_ptr(trans, root, path, level + 1, pslot);
1040bb803951SChris Mason 		if (wret)
1041bb803951SChris Mason 			ret = wret;
1042f0486c68SYan, Zheng 		root_sub_used(root, mid->len);
1043f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, mid, 0, 1);
1044f0486c68SYan, Zheng 		free_extent_buffer(mid);
1045f0486c68SYan, Zheng 		mid = NULL;
104679f95c82SChris Mason 	} else {
104779f95c82SChris Mason 		/* update the parent key to reflect our changes */
10485f39d397SChris Mason 		struct btrfs_disk_key mid_key;
10495f39d397SChris Mason 		btrfs_node_key(mid, &mid_key, 0);
10505f39d397SChris Mason 		btrfs_set_node_key(parent, &mid_key, pslot);
10515f39d397SChris Mason 		btrfs_mark_buffer_dirty(parent);
105279f95c82SChris Mason 	}
1053bb803951SChris Mason 
105479f95c82SChris Mason 	/* update the path */
10555f39d397SChris Mason 	if (left) {
10565f39d397SChris Mason 		if (btrfs_header_nritems(left) > orig_slot) {
10575f39d397SChris Mason 			extent_buffer_get(left);
1058925baeddSChris Mason 			/* left was locked after cow */
10595f39d397SChris Mason 			path->nodes[level] = left;
1060bb803951SChris Mason 			path->slots[level + 1] -= 1;
1061bb803951SChris Mason 			path->slots[level] = orig_slot;
1062925baeddSChris Mason 			if (mid) {
1063925baeddSChris Mason 				btrfs_tree_unlock(mid);
10645f39d397SChris Mason 				free_extent_buffer(mid);
1065925baeddSChris Mason 			}
1066bb803951SChris Mason 		} else {
10675f39d397SChris Mason 			orig_slot -= btrfs_header_nritems(left);
1068bb803951SChris Mason 			path->slots[level] = orig_slot;
1069bb803951SChris Mason 		}
1070bb803951SChris Mason 	}
107179f95c82SChris Mason 	/* double check we haven't messed things up */
1072e20d96d6SChris Mason 	if (orig_ptr !=
10735f39d397SChris Mason 	    btrfs_node_blockptr(path->nodes[level], path->slots[level]))
107479f95c82SChris Mason 		BUG();
107554aa1f4dSChris Mason enospc:
1076925baeddSChris Mason 	if (right) {
1077925baeddSChris Mason 		btrfs_tree_unlock(right);
10785f39d397SChris Mason 		free_extent_buffer(right);
1079925baeddSChris Mason 	}
1080925baeddSChris Mason 	if (left) {
1081925baeddSChris Mason 		if (path->nodes[level] != left)
1082925baeddSChris Mason 			btrfs_tree_unlock(left);
10835f39d397SChris Mason 		free_extent_buffer(left);
1084925baeddSChris Mason 	}
1085bb803951SChris Mason 	return ret;
1086bb803951SChris Mason }
1087bb803951SChris Mason 
1088d352ac68SChris Mason /* Node balancing for insertion.  Here we only split or push nodes around
1089d352ac68SChris Mason  * when they are completely full.  This is also done top down, so we
1090d352ac68SChris Mason  * have to be pessimistic.
1091d352ac68SChris Mason  */
1092d397712bSChris Mason static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1093e66f709bSChris Mason 					  struct btrfs_root *root,
1094e66f709bSChris Mason 					  struct btrfs_path *path, int level)
1095e66f709bSChris Mason {
10965f39d397SChris Mason 	struct extent_buffer *right = NULL;
10975f39d397SChris Mason 	struct extent_buffer *mid;
10985f39d397SChris Mason 	struct extent_buffer *left = NULL;
10995f39d397SChris Mason 	struct extent_buffer *parent = NULL;
1100e66f709bSChris Mason 	int ret = 0;
1101e66f709bSChris Mason 	int wret;
1102e66f709bSChris Mason 	int pslot;
1103e66f709bSChris Mason 	int orig_slot = path->slots[level];
1104e66f709bSChris Mason 
1105e66f709bSChris Mason 	if (level == 0)
1106e66f709bSChris Mason 		return 1;
1107e66f709bSChris Mason 
11085f39d397SChris Mason 	mid = path->nodes[level];
11097bb86316SChris Mason 	WARN_ON(btrfs_header_generation(mid) != trans->transid);
1110e66f709bSChris Mason 
1111*a05a9bb1SLi Zefan 	if (level < BTRFS_MAX_LEVEL - 1) {
11125f39d397SChris Mason 		parent = path->nodes[level + 1];
1113e66f709bSChris Mason 		pslot = path->slots[level + 1];
1114*a05a9bb1SLi Zefan 	}
1115e66f709bSChris Mason 
11165f39d397SChris Mason 	if (!parent)
1117e66f709bSChris Mason 		return 1;
1118e66f709bSChris Mason 
11195f39d397SChris Mason 	left = read_node_slot(root, parent, pslot - 1);
1120e66f709bSChris Mason 
1121e66f709bSChris Mason 	/* first, try to make some room in the middle buffer */
11225f39d397SChris Mason 	if (left) {
1123e66f709bSChris Mason 		u32 left_nr;
1124925baeddSChris Mason 
1125925baeddSChris Mason 		btrfs_tree_lock(left);
1126b4ce94deSChris Mason 		btrfs_set_lock_blocking(left);
1127b4ce94deSChris Mason 
11285f39d397SChris Mason 		left_nr = btrfs_header_nritems(left);
112933ade1f8SChris Mason 		if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
113033ade1f8SChris Mason 			wret = 1;
113133ade1f8SChris Mason 		} else {
11325f39d397SChris Mason 			ret = btrfs_cow_block(trans, root, left, parent,
11339fa8cfe7SChris Mason 					      pslot - 1, &left);
113454aa1f4dSChris Mason 			if (ret)
113554aa1f4dSChris Mason 				wret = 1;
113654aa1f4dSChris Mason 			else {
113754aa1f4dSChris Mason 				wret = push_node_left(trans, root,
1138971a1f66SChris Mason 						      left, mid, 0);
113954aa1f4dSChris Mason 			}
114033ade1f8SChris Mason 		}
1141e66f709bSChris Mason 		if (wret < 0)
1142e66f709bSChris Mason 			ret = wret;
1143e66f709bSChris Mason 		if (wret == 0) {
11445f39d397SChris Mason 			struct btrfs_disk_key disk_key;
1145e66f709bSChris Mason 			orig_slot += left_nr;
11465f39d397SChris Mason 			btrfs_node_key(mid, &disk_key, 0);
11475f39d397SChris Mason 			btrfs_set_node_key(parent, &disk_key, pslot);
11485f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
11495f39d397SChris Mason 			if (btrfs_header_nritems(left) > orig_slot) {
11505f39d397SChris Mason 				path->nodes[level] = left;
1151e66f709bSChris Mason 				path->slots[level + 1] -= 1;
1152e66f709bSChris Mason 				path->slots[level] = orig_slot;
1153925baeddSChris Mason 				btrfs_tree_unlock(mid);
11545f39d397SChris Mason 				free_extent_buffer(mid);
1155e66f709bSChris Mason 			} else {
1156e66f709bSChris Mason 				orig_slot -=
11575f39d397SChris Mason 					btrfs_header_nritems(left);
1158e66f709bSChris Mason 				path->slots[level] = orig_slot;
1159925baeddSChris Mason 				btrfs_tree_unlock(left);
11605f39d397SChris Mason 				free_extent_buffer(left);
1161e66f709bSChris Mason 			}
1162e66f709bSChris Mason 			return 0;
1163e66f709bSChris Mason 		}
1164925baeddSChris Mason 		btrfs_tree_unlock(left);
11655f39d397SChris Mason 		free_extent_buffer(left);
1166e66f709bSChris Mason 	}
11675f39d397SChris Mason 	right = read_node_slot(root, parent, pslot + 1);
1168e66f709bSChris Mason 
1169e66f709bSChris Mason 	/*
1170e66f709bSChris Mason 	 * then try to empty the right most buffer into the middle
1171e66f709bSChris Mason 	 */
11725f39d397SChris Mason 	if (right) {
117333ade1f8SChris Mason 		u32 right_nr;
1174b4ce94deSChris Mason 
1175925baeddSChris Mason 		btrfs_tree_lock(right);
1176b4ce94deSChris Mason 		btrfs_set_lock_blocking(right);
1177b4ce94deSChris Mason 
11785f39d397SChris Mason 		right_nr = btrfs_header_nritems(right);
117933ade1f8SChris Mason 		if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
118033ade1f8SChris Mason 			wret = 1;
118133ade1f8SChris Mason 		} else {
11825f39d397SChris Mason 			ret = btrfs_cow_block(trans, root, right,
11835f39d397SChris Mason 					      parent, pslot + 1,
11849fa8cfe7SChris Mason 					      &right);
118554aa1f4dSChris Mason 			if (ret)
118654aa1f4dSChris Mason 				wret = 1;
118754aa1f4dSChris Mason 			else {
118833ade1f8SChris Mason 				wret = balance_node_right(trans, root,
11895f39d397SChris Mason 							  right, mid);
119033ade1f8SChris Mason 			}
119154aa1f4dSChris Mason 		}
1192e66f709bSChris Mason 		if (wret < 0)
1193e66f709bSChris Mason 			ret = wret;
1194e66f709bSChris Mason 		if (wret == 0) {
11955f39d397SChris Mason 			struct btrfs_disk_key disk_key;
11965f39d397SChris Mason 
11975f39d397SChris Mason 			btrfs_node_key(right, &disk_key, 0);
11985f39d397SChris Mason 			btrfs_set_node_key(parent, &disk_key, pslot + 1);
11995f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
12005f39d397SChris Mason 
12015f39d397SChris Mason 			if (btrfs_header_nritems(mid) <= orig_slot) {
12025f39d397SChris Mason 				path->nodes[level] = right;
1203e66f709bSChris Mason 				path->slots[level + 1] += 1;
1204e66f709bSChris Mason 				path->slots[level] = orig_slot -
12055f39d397SChris Mason 					btrfs_header_nritems(mid);
1206925baeddSChris Mason 				btrfs_tree_unlock(mid);
12075f39d397SChris Mason 				free_extent_buffer(mid);
1208e66f709bSChris Mason 			} else {
1209925baeddSChris Mason 				btrfs_tree_unlock(right);
12105f39d397SChris Mason 				free_extent_buffer(right);
1211e66f709bSChris Mason 			}
1212e66f709bSChris Mason 			return 0;
1213e66f709bSChris Mason 		}
1214925baeddSChris Mason 		btrfs_tree_unlock(right);
12155f39d397SChris Mason 		free_extent_buffer(right);
1216e66f709bSChris Mason 	}
1217e66f709bSChris Mason 	return 1;
1218e66f709bSChris Mason }
1219e66f709bSChris Mason 
122074123bd7SChris Mason /*
1221d352ac68SChris Mason  * readahead one full node of leaves, finding things that are close
1222d352ac68SChris Mason  * to the block in 'slot', and triggering ra on them.
12233c69faecSChris Mason  */
1224c8c42864SChris Mason static void reada_for_search(struct btrfs_root *root,
1225e02119d5SChris Mason 			     struct btrfs_path *path,
122601f46658SChris Mason 			     int level, int slot, u64 objectid)
12273c69faecSChris Mason {
12285f39d397SChris Mason 	struct extent_buffer *node;
122901f46658SChris Mason 	struct btrfs_disk_key disk_key;
12303c69faecSChris Mason 	u32 nritems;
12313c69faecSChris Mason 	u64 search;
1232a7175319SChris Mason 	u64 target;
12336b80053dSChris Mason 	u64 nread = 0;
1234cb25c2eaSJosef Bacik 	u64 gen;
12353c69faecSChris Mason 	int direction = path->reada;
12365f39d397SChris Mason 	struct extent_buffer *eb;
12376b80053dSChris Mason 	u32 nr;
12386b80053dSChris Mason 	u32 blocksize;
12396b80053dSChris Mason 	u32 nscan = 0;
1240db94535dSChris Mason 
1241a6b6e75eSChris Mason 	if (level != 1)
12423c69faecSChris Mason 		return;
12433c69faecSChris Mason 
12446702ed49SChris Mason 	if (!path->nodes[level])
12456702ed49SChris Mason 		return;
12466702ed49SChris Mason 
12475f39d397SChris Mason 	node = path->nodes[level];
1248925baeddSChris Mason 
12493c69faecSChris Mason 	search = btrfs_node_blockptr(node, slot);
12506b80053dSChris Mason 	blocksize = btrfs_level_size(root, level - 1);
12516b80053dSChris Mason 	eb = btrfs_find_tree_block(root, search, blocksize);
12525f39d397SChris Mason 	if (eb) {
12535f39d397SChris Mason 		free_extent_buffer(eb);
12543c69faecSChris Mason 		return;
12553c69faecSChris Mason 	}
12563c69faecSChris Mason 
1257a7175319SChris Mason 	target = search;
12586b80053dSChris Mason 
12595f39d397SChris Mason 	nritems = btrfs_header_nritems(node);
12606b80053dSChris Mason 	nr = slot;
126125b8b936SJosef Bacik 
12623c69faecSChris Mason 	while (1) {
12636b80053dSChris Mason 		if (direction < 0) {
12646b80053dSChris Mason 			if (nr == 0)
12653c69faecSChris Mason 				break;
12666b80053dSChris Mason 			nr--;
12676b80053dSChris Mason 		} else if (direction > 0) {
12686b80053dSChris Mason 			nr++;
12696b80053dSChris Mason 			if (nr >= nritems)
12706b80053dSChris Mason 				break;
12713c69faecSChris Mason 		}
127201f46658SChris Mason 		if (path->reada < 0 && objectid) {
127301f46658SChris Mason 			btrfs_node_key(node, &disk_key, nr);
127401f46658SChris Mason 			if (btrfs_disk_key_objectid(&disk_key) != objectid)
127501f46658SChris Mason 				break;
127601f46658SChris Mason 		}
12776b80053dSChris Mason 		search = btrfs_node_blockptr(node, nr);
1278a7175319SChris Mason 		if ((search <= target && target - search <= 65536) ||
1279a7175319SChris Mason 		    (search > target && search - target <= 65536)) {
1280cb25c2eaSJosef Bacik 			gen = btrfs_node_ptr_generation(node, nr);
1281cb25c2eaSJosef Bacik 			readahead_tree_block(root, search, blocksize, gen);
12826b80053dSChris Mason 			nread += blocksize;
12833c69faecSChris Mason 		}
12846b80053dSChris Mason 		nscan++;
1285a7175319SChris Mason 		if ((nread > 65536 || nscan > 32))
12866b80053dSChris Mason 			break;
12873c69faecSChris Mason 	}
12883c69faecSChris Mason }
1289925baeddSChris Mason 
1290d352ac68SChris Mason /*
1291b4ce94deSChris Mason  * returns -EAGAIN if it had to drop the path, or zero if everything was in
1292b4ce94deSChris Mason  * cache
1293b4ce94deSChris Mason  */
1294b4ce94deSChris Mason static noinline int reada_for_balance(struct btrfs_root *root,
1295b4ce94deSChris Mason 				      struct btrfs_path *path, int level)
1296b4ce94deSChris Mason {
1297b4ce94deSChris Mason 	int slot;
1298b4ce94deSChris Mason 	int nritems;
1299b4ce94deSChris Mason 	struct extent_buffer *parent;
1300b4ce94deSChris Mason 	struct extent_buffer *eb;
1301b4ce94deSChris Mason 	u64 gen;
1302b4ce94deSChris Mason 	u64 block1 = 0;
1303b4ce94deSChris Mason 	u64 block2 = 0;
1304b4ce94deSChris Mason 	int ret = 0;
1305b4ce94deSChris Mason 	int blocksize;
1306b4ce94deSChris Mason 
13078c594ea8SChris Mason 	parent = path->nodes[level + 1];
1308b4ce94deSChris Mason 	if (!parent)
1309b4ce94deSChris Mason 		return 0;
1310b4ce94deSChris Mason 
1311b4ce94deSChris Mason 	nritems = btrfs_header_nritems(parent);
13128c594ea8SChris Mason 	slot = path->slots[level + 1];
1313b4ce94deSChris Mason 	blocksize = btrfs_level_size(root, level);
1314b4ce94deSChris Mason 
1315b4ce94deSChris Mason 	if (slot > 0) {
1316b4ce94deSChris Mason 		block1 = btrfs_node_blockptr(parent, slot - 1);
1317b4ce94deSChris Mason 		gen = btrfs_node_ptr_generation(parent, slot - 1);
1318b4ce94deSChris Mason 		eb = btrfs_find_tree_block(root, block1, blocksize);
1319b4ce94deSChris Mason 		if (eb && btrfs_buffer_uptodate(eb, gen))
1320b4ce94deSChris Mason 			block1 = 0;
1321b4ce94deSChris Mason 		free_extent_buffer(eb);
1322b4ce94deSChris Mason 	}
13238c594ea8SChris Mason 	if (slot + 1 < nritems) {
1324b4ce94deSChris Mason 		block2 = btrfs_node_blockptr(parent, slot + 1);
1325b4ce94deSChris Mason 		gen = btrfs_node_ptr_generation(parent, slot + 1);
1326b4ce94deSChris Mason 		eb = btrfs_find_tree_block(root, block2, blocksize);
1327b4ce94deSChris Mason 		if (eb && btrfs_buffer_uptodate(eb, gen))
1328b4ce94deSChris Mason 			block2 = 0;
1329b4ce94deSChris Mason 		free_extent_buffer(eb);
1330b4ce94deSChris Mason 	}
1331b4ce94deSChris Mason 	if (block1 || block2) {
1332b4ce94deSChris Mason 		ret = -EAGAIN;
13338c594ea8SChris Mason 
13348c594ea8SChris Mason 		/* release the whole path */
1335b3b4aa74SDavid Sterba 		btrfs_release_path(path);
13368c594ea8SChris Mason 
13378c594ea8SChris Mason 		/* read the blocks */
1338b4ce94deSChris Mason 		if (block1)
1339b4ce94deSChris Mason 			readahead_tree_block(root, block1, blocksize, 0);
1340b4ce94deSChris Mason 		if (block2)
1341b4ce94deSChris Mason 			readahead_tree_block(root, block2, blocksize, 0);
1342b4ce94deSChris Mason 
1343b4ce94deSChris Mason 		if (block1) {
1344b4ce94deSChris Mason 			eb = read_tree_block(root, block1, blocksize, 0);
1345b4ce94deSChris Mason 			free_extent_buffer(eb);
1346b4ce94deSChris Mason 		}
13478c594ea8SChris Mason 		if (block2) {
1348b4ce94deSChris Mason 			eb = read_tree_block(root, block2, blocksize, 0);
1349b4ce94deSChris Mason 			free_extent_buffer(eb);
1350b4ce94deSChris Mason 		}
1351b4ce94deSChris Mason 	}
1352b4ce94deSChris Mason 	return ret;
1353b4ce94deSChris Mason }
1354b4ce94deSChris Mason 
1355b4ce94deSChris Mason 
1356b4ce94deSChris Mason /*
1357d397712bSChris Mason  * when we walk down the tree, it is usually safe to unlock the higher layers
1358d397712bSChris Mason  * in the tree.  The exceptions are when our path goes through slot 0, because
1359d397712bSChris Mason  * operations on the tree might require changing key pointers higher up in the
1360d397712bSChris Mason  * tree.
1361d352ac68SChris Mason  *
1362d397712bSChris Mason  * callers might also have set path->keep_locks, which tells this code to keep
1363d397712bSChris Mason  * the lock if the path points to the last slot in the block.  This is part of
1364d397712bSChris Mason  * walking through the tree, and selecting the next slot in the higher block.
1365d352ac68SChris Mason  *
1366d397712bSChris Mason  * lowest_unlock sets the lowest level in the tree we're allowed to unlock.  so
1367d397712bSChris Mason  * if lowest_unlock is 1, level 0 won't be unlocked
1368d352ac68SChris Mason  */
1369e02119d5SChris Mason static noinline void unlock_up(struct btrfs_path *path, int level,
1370e02119d5SChris Mason 			       int lowest_unlock)
1371925baeddSChris Mason {
1372925baeddSChris Mason 	int i;
1373925baeddSChris Mason 	int skip_level = level;
1374051e1b9fSChris Mason 	int no_skips = 0;
1375925baeddSChris Mason 	struct extent_buffer *t;
1376925baeddSChris Mason 
1377925baeddSChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1378925baeddSChris Mason 		if (!path->nodes[i])
1379925baeddSChris Mason 			break;
1380925baeddSChris Mason 		if (!path->locks[i])
1381925baeddSChris Mason 			break;
1382051e1b9fSChris Mason 		if (!no_skips && path->slots[i] == 0) {
1383925baeddSChris Mason 			skip_level = i + 1;
1384925baeddSChris Mason 			continue;
1385925baeddSChris Mason 		}
1386051e1b9fSChris Mason 		if (!no_skips && path->keep_locks) {
1387925baeddSChris Mason 			u32 nritems;
1388925baeddSChris Mason 			t = path->nodes[i];
1389925baeddSChris Mason 			nritems = btrfs_header_nritems(t);
1390051e1b9fSChris Mason 			if (nritems < 1 || path->slots[i] >= nritems - 1) {
1391925baeddSChris Mason 				skip_level = i + 1;
1392925baeddSChris Mason 				continue;
1393925baeddSChris Mason 			}
1394925baeddSChris Mason 		}
1395051e1b9fSChris Mason 		if (skip_level < i && i >= lowest_unlock)
1396051e1b9fSChris Mason 			no_skips = 1;
1397051e1b9fSChris Mason 
1398925baeddSChris Mason 		t = path->nodes[i];
1399925baeddSChris Mason 		if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1400bd681513SChris Mason 			btrfs_tree_unlock_rw(t, path->locks[i]);
1401925baeddSChris Mason 			path->locks[i] = 0;
1402925baeddSChris Mason 		}
1403925baeddSChris Mason 	}
1404925baeddSChris Mason }
1405925baeddSChris Mason 
14063c69faecSChris Mason /*
1407b4ce94deSChris Mason  * This releases any locks held in the path starting at level and
1408b4ce94deSChris Mason  * going all the way up to the root.
1409b4ce94deSChris Mason  *
1410b4ce94deSChris Mason  * btrfs_search_slot will keep the lock held on higher nodes in a few
1411b4ce94deSChris Mason  * corner cases, such as COW of the block at slot zero in the node.  This
1412b4ce94deSChris Mason  * ignores those rules, and it should only be called when there are no
1413b4ce94deSChris Mason  * more updates to be done higher up in the tree.
1414b4ce94deSChris Mason  */
1415b4ce94deSChris Mason noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1416b4ce94deSChris Mason {
1417b4ce94deSChris Mason 	int i;
1418b4ce94deSChris Mason 
14195d4f98a2SYan Zheng 	if (path->keep_locks)
1420b4ce94deSChris Mason 		return;
1421b4ce94deSChris Mason 
1422b4ce94deSChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1423b4ce94deSChris Mason 		if (!path->nodes[i])
142412f4daccSChris Mason 			continue;
1425b4ce94deSChris Mason 		if (!path->locks[i])
142612f4daccSChris Mason 			continue;
1427bd681513SChris Mason 		btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
1428b4ce94deSChris Mason 		path->locks[i] = 0;
1429b4ce94deSChris Mason 	}
1430b4ce94deSChris Mason }
1431b4ce94deSChris Mason 
1432b4ce94deSChris Mason /*
1433c8c42864SChris Mason  * helper function for btrfs_search_slot.  The goal is to find a block
1434c8c42864SChris Mason  * in cache without setting the path to blocking.  If we find the block
1435c8c42864SChris Mason  * we return zero and the path is unchanged.
1436c8c42864SChris Mason  *
1437c8c42864SChris Mason  * If we can't find the block, we set the path blocking and do some
1438c8c42864SChris Mason  * reada.  -EAGAIN is returned and the search must be repeated.
1439c8c42864SChris Mason  */
1440c8c42864SChris Mason static int
1441c8c42864SChris Mason read_block_for_search(struct btrfs_trans_handle *trans,
1442c8c42864SChris Mason 		       struct btrfs_root *root, struct btrfs_path *p,
1443c8c42864SChris Mason 		       struct extent_buffer **eb_ret, int level, int slot,
1444c8c42864SChris Mason 		       struct btrfs_key *key)
1445c8c42864SChris Mason {
1446c8c42864SChris Mason 	u64 blocknr;
1447c8c42864SChris Mason 	u64 gen;
1448c8c42864SChris Mason 	u32 blocksize;
1449c8c42864SChris Mason 	struct extent_buffer *b = *eb_ret;
1450c8c42864SChris Mason 	struct extent_buffer *tmp;
145176a05b35SChris Mason 	int ret;
1452c8c42864SChris Mason 
1453c8c42864SChris Mason 	blocknr = btrfs_node_blockptr(b, slot);
1454c8c42864SChris Mason 	gen = btrfs_node_ptr_generation(b, slot);
1455c8c42864SChris Mason 	blocksize = btrfs_level_size(root, level - 1);
1456c8c42864SChris Mason 
1457c8c42864SChris Mason 	tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1458cb44921aSChris Mason 	if (tmp) {
1459cb44921aSChris Mason 		if (btrfs_buffer_uptodate(tmp, 0)) {
1460cb44921aSChris Mason 			if (btrfs_buffer_uptodate(tmp, gen)) {
146176a05b35SChris Mason 				/*
1462cb44921aSChris Mason 				 * we found an up to date block without
1463cb44921aSChris Mason 				 * sleeping, return
146476a05b35SChris Mason 				 * right away
146576a05b35SChris Mason 				 */
1466c8c42864SChris Mason 				*eb_ret = tmp;
1467c8c42864SChris Mason 				return 0;
1468c8c42864SChris Mason 			}
1469cb44921aSChris Mason 			/* the pages were up to date, but we failed
1470cb44921aSChris Mason 			 * the generation number check.  Do a full
1471cb44921aSChris Mason 			 * read for the generation number that is correct.
1472cb44921aSChris Mason 			 * We must do this without dropping locks so
1473cb44921aSChris Mason 			 * we can trust our generation number
1474cb44921aSChris Mason 			 */
1475cb44921aSChris Mason 			free_extent_buffer(tmp);
1476bd681513SChris Mason 			btrfs_set_path_blocking(p);
1477bd681513SChris Mason 
1478cb44921aSChris Mason 			tmp = read_tree_block(root, blocknr, blocksize, gen);
1479cb44921aSChris Mason 			if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1480cb44921aSChris Mason 				*eb_ret = tmp;
1481cb44921aSChris Mason 				return 0;
1482cb44921aSChris Mason 			}
1483cb44921aSChris Mason 			free_extent_buffer(tmp);
1484b3b4aa74SDavid Sterba 			btrfs_release_path(p);
1485cb44921aSChris Mason 			return -EIO;
1486cb44921aSChris Mason 		}
1487cb44921aSChris Mason 	}
1488c8c42864SChris Mason 
1489c8c42864SChris Mason 	/*
1490c8c42864SChris Mason 	 * reduce lock contention at high levels
1491c8c42864SChris Mason 	 * of the btree by dropping locks before
149276a05b35SChris Mason 	 * we read.  Don't release the lock on the current
149376a05b35SChris Mason 	 * level because we need to walk this node to figure
149476a05b35SChris Mason 	 * out which blocks to read.
1495c8c42864SChris Mason 	 */
14968c594ea8SChris Mason 	btrfs_unlock_up_safe(p, level + 1);
14978c594ea8SChris Mason 	btrfs_set_path_blocking(p);
14988c594ea8SChris Mason 
1499c8c42864SChris Mason 	free_extent_buffer(tmp);
1500c8c42864SChris Mason 	if (p->reada)
1501c8c42864SChris Mason 		reada_for_search(root, p, level, slot, key->objectid);
1502c8c42864SChris Mason 
1503b3b4aa74SDavid Sterba 	btrfs_release_path(p);
150476a05b35SChris Mason 
150576a05b35SChris Mason 	ret = -EAGAIN;
15065bdd3536SYan, Zheng 	tmp = read_tree_block(root, blocknr, blocksize, 0);
150776a05b35SChris Mason 	if (tmp) {
150876a05b35SChris Mason 		/*
150976a05b35SChris Mason 		 * If the read above didn't mark this buffer up to date,
151076a05b35SChris Mason 		 * it will never end up being up to date.  Set ret to EIO now
151176a05b35SChris Mason 		 * and give up so that our caller doesn't loop forever
151276a05b35SChris Mason 		 * on our EAGAINs.
151376a05b35SChris Mason 		 */
151476a05b35SChris Mason 		if (!btrfs_buffer_uptodate(tmp, 0))
151576a05b35SChris Mason 			ret = -EIO;
1516c8c42864SChris Mason 		free_extent_buffer(tmp);
151776a05b35SChris Mason 	}
151876a05b35SChris Mason 	return ret;
1519c8c42864SChris Mason }
1520c8c42864SChris Mason 
1521c8c42864SChris Mason /*
1522c8c42864SChris Mason  * helper function for btrfs_search_slot.  This does all of the checks
1523c8c42864SChris Mason  * for node-level blocks and does any balancing required based on
1524c8c42864SChris Mason  * the ins_len.
1525c8c42864SChris Mason  *
1526c8c42864SChris Mason  * If no extra work was required, zero is returned.  If we had to
1527c8c42864SChris Mason  * drop the path, -EAGAIN is returned and btrfs_search_slot must
1528c8c42864SChris Mason  * start over
1529c8c42864SChris Mason  */
1530c8c42864SChris Mason static int
1531c8c42864SChris Mason setup_nodes_for_search(struct btrfs_trans_handle *trans,
1532c8c42864SChris Mason 		       struct btrfs_root *root, struct btrfs_path *p,
1533bd681513SChris Mason 		       struct extent_buffer *b, int level, int ins_len,
1534bd681513SChris Mason 		       int *write_lock_level)
1535c8c42864SChris Mason {
1536c8c42864SChris Mason 	int ret;
1537c8c42864SChris Mason 	if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1538c8c42864SChris Mason 	    BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1539c8c42864SChris Mason 		int sret;
1540c8c42864SChris Mason 
1541bd681513SChris Mason 		if (*write_lock_level < level + 1) {
1542bd681513SChris Mason 			*write_lock_level = level + 1;
1543bd681513SChris Mason 			btrfs_release_path(p);
1544bd681513SChris Mason 			goto again;
1545bd681513SChris Mason 		}
1546bd681513SChris Mason 
1547c8c42864SChris Mason 		sret = reada_for_balance(root, p, level);
1548c8c42864SChris Mason 		if (sret)
1549c8c42864SChris Mason 			goto again;
1550c8c42864SChris Mason 
1551c8c42864SChris Mason 		btrfs_set_path_blocking(p);
1552c8c42864SChris Mason 		sret = split_node(trans, root, p, level);
1553bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
1554c8c42864SChris Mason 
1555c8c42864SChris Mason 		BUG_ON(sret > 0);
1556c8c42864SChris Mason 		if (sret) {
1557c8c42864SChris Mason 			ret = sret;
1558c8c42864SChris Mason 			goto done;
1559c8c42864SChris Mason 		}
1560c8c42864SChris Mason 		b = p->nodes[level];
1561c8c42864SChris Mason 	} else if (ins_len < 0 && btrfs_header_nritems(b) <
1562cfbb9308SChris Mason 		   BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
1563c8c42864SChris Mason 		int sret;
1564c8c42864SChris Mason 
1565bd681513SChris Mason 		if (*write_lock_level < level + 1) {
1566bd681513SChris Mason 			*write_lock_level = level + 1;
1567bd681513SChris Mason 			btrfs_release_path(p);
1568bd681513SChris Mason 			goto again;
1569bd681513SChris Mason 		}
1570bd681513SChris Mason 
1571c8c42864SChris Mason 		sret = reada_for_balance(root, p, level);
1572c8c42864SChris Mason 		if (sret)
1573c8c42864SChris Mason 			goto again;
1574c8c42864SChris Mason 
1575c8c42864SChris Mason 		btrfs_set_path_blocking(p);
1576c8c42864SChris Mason 		sret = balance_level(trans, root, p, level);
1577bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
1578c8c42864SChris Mason 
1579c8c42864SChris Mason 		if (sret) {
1580c8c42864SChris Mason 			ret = sret;
1581c8c42864SChris Mason 			goto done;
1582c8c42864SChris Mason 		}
1583c8c42864SChris Mason 		b = p->nodes[level];
1584c8c42864SChris Mason 		if (!b) {
1585b3b4aa74SDavid Sterba 			btrfs_release_path(p);
1586c8c42864SChris Mason 			goto again;
1587c8c42864SChris Mason 		}
1588c8c42864SChris Mason 		BUG_ON(btrfs_header_nritems(b) == 1);
1589c8c42864SChris Mason 	}
1590c8c42864SChris Mason 	return 0;
1591c8c42864SChris Mason 
1592c8c42864SChris Mason again:
1593c8c42864SChris Mason 	ret = -EAGAIN;
1594c8c42864SChris Mason done:
1595c8c42864SChris Mason 	return ret;
1596c8c42864SChris Mason }
1597c8c42864SChris Mason 
1598c8c42864SChris Mason /*
159974123bd7SChris Mason  * look for key in the tree.  path is filled in with nodes along the way
160074123bd7SChris Mason  * if key is found, we return zero and you can find the item in the leaf
160174123bd7SChris Mason  * level of the path (level 0)
160274123bd7SChris Mason  *
160374123bd7SChris Mason  * If the key isn't found, the path points to the slot where it should
1604aa5d6bedSChris Mason  * be inserted, and 1 is returned.  If there are other errors during the
1605aa5d6bedSChris Mason  * search a negative error number is returned.
160697571fd0SChris Mason  *
160797571fd0SChris Mason  * if ins_len > 0, nodes and leaves will be split as we walk down the
160897571fd0SChris Mason  * tree.  if ins_len < 0, nodes will be merged as we walk down the tree (if
160997571fd0SChris Mason  * possible)
161074123bd7SChris Mason  */
1611e089f05cSChris Mason int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1612e089f05cSChris Mason 		      *root, struct btrfs_key *key, struct btrfs_path *p, int
1613e089f05cSChris Mason 		      ins_len, int cow)
1614be0e5c09SChris Mason {
16155f39d397SChris Mason 	struct extent_buffer *b;
1616be0e5c09SChris Mason 	int slot;
1617be0e5c09SChris Mason 	int ret;
161833c66f43SYan Zheng 	int err;
1619be0e5c09SChris Mason 	int level;
1620925baeddSChris Mason 	int lowest_unlock = 1;
1621bd681513SChris Mason 	int root_lock;
1622bd681513SChris Mason 	/* everything at write_lock_level or lower must be write locked */
1623bd681513SChris Mason 	int write_lock_level = 0;
16249f3a7427SChris Mason 	u8 lowest_level = 0;
16259f3a7427SChris Mason 
16266702ed49SChris Mason 	lowest_level = p->lowest_level;
1627323ac95bSChris Mason 	WARN_ON(lowest_level && ins_len > 0);
162822b0ebdaSChris Mason 	WARN_ON(p->nodes[0] != NULL);
162925179201SJosef Bacik 
1630bd681513SChris Mason 	if (ins_len < 0) {
1631925baeddSChris Mason 		lowest_unlock = 2;
163265b51a00SChris Mason 
1633bd681513SChris Mason 		/* when we are removing items, we might have to go up to level
1634bd681513SChris Mason 		 * two as we update tree pointers  Make sure we keep write
1635bd681513SChris Mason 		 * for those levels as well
1636bd681513SChris Mason 		 */
1637bd681513SChris Mason 		write_lock_level = 2;
1638bd681513SChris Mason 	} else if (ins_len > 0) {
1639bd681513SChris Mason 		/*
1640bd681513SChris Mason 		 * for inserting items, make sure we have a write lock on
1641bd681513SChris Mason 		 * level 1 so we can update keys
1642bd681513SChris Mason 		 */
1643bd681513SChris Mason 		write_lock_level = 1;
1644bd681513SChris Mason 	}
1645bd681513SChris Mason 
1646bd681513SChris Mason 	if (!cow)
1647bd681513SChris Mason 		write_lock_level = -1;
1648bd681513SChris Mason 
1649bd681513SChris Mason 	if (cow && (p->keep_locks || p->lowest_level))
1650bd681513SChris Mason 		write_lock_level = BTRFS_MAX_LEVEL;
1651bd681513SChris Mason 
1652bb803951SChris Mason again:
1653bd681513SChris Mason 	/*
1654bd681513SChris Mason 	 * we try very hard to do read locks on the root
1655bd681513SChris Mason 	 */
1656bd681513SChris Mason 	root_lock = BTRFS_READ_LOCK;
1657bd681513SChris Mason 	level = 0;
16585d4f98a2SYan Zheng 	if (p->search_commit_root) {
1659bd681513SChris Mason 		/*
1660bd681513SChris Mason 		 * the commit roots are read only
1661bd681513SChris Mason 		 * so we always do read locks
1662bd681513SChris Mason 		 */
16635d4f98a2SYan Zheng 		b = root->commit_root;
16645d4f98a2SYan Zheng 		extent_buffer_get(b);
1665bd681513SChris Mason 		level = btrfs_header_level(b);
16665d4f98a2SYan Zheng 		if (!p->skip_locking)
1667bd681513SChris Mason 			btrfs_tree_read_lock(b);
16685d4f98a2SYan Zheng 	} else {
1669bd681513SChris Mason 		if (p->skip_locking) {
16705cd57b2cSChris Mason 			b = btrfs_root_node(root);
1671bd681513SChris Mason 			level = btrfs_header_level(b);
1672bd681513SChris Mason 		} else {
1673bd681513SChris Mason 			/* we don't know the level of the root node
1674bd681513SChris Mason 			 * until we actually have it read locked
1675bd681513SChris Mason 			 */
1676bd681513SChris Mason 			b = btrfs_read_lock_root_node(root);
1677bd681513SChris Mason 			level = btrfs_header_level(b);
1678bd681513SChris Mason 			if (level <= write_lock_level) {
1679bd681513SChris Mason 				/* whoops, must trade for write lock */
1680bd681513SChris Mason 				btrfs_tree_read_unlock(b);
1681bd681513SChris Mason 				free_extent_buffer(b);
1682925baeddSChris Mason 				b = btrfs_lock_root_node(root);
1683bd681513SChris Mason 				root_lock = BTRFS_WRITE_LOCK;
1684bd681513SChris Mason 
1685bd681513SChris Mason 				/* the level might have changed, check again */
1686bd681513SChris Mason 				level = btrfs_header_level(b);
16875d4f98a2SYan Zheng 			}
1688bd681513SChris Mason 		}
1689bd681513SChris Mason 	}
1690bd681513SChris Mason 	p->nodes[level] = b;
1691bd681513SChris Mason 	if (!p->skip_locking)
1692bd681513SChris Mason 		p->locks[level] = root_lock;
1693925baeddSChris Mason 
1694eb60ceacSChris Mason 	while (b) {
16955f39d397SChris Mason 		level = btrfs_header_level(b);
169665b51a00SChris Mason 
169765b51a00SChris Mason 		/*
169865b51a00SChris Mason 		 * setup the path here so we can release it under lock
169965b51a00SChris Mason 		 * contention with the cow code
170065b51a00SChris Mason 		 */
170102217ed2SChris Mason 		if (cow) {
1702c8c42864SChris Mason 			/*
1703c8c42864SChris Mason 			 * if we don't really need to cow this block
1704c8c42864SChris Mason 			 * then we don't want to set the path blocking,
1705c8c42864SChris Mason 			 * so we test it here
1706c8c42864SChris Mason 			 */
17075d4f98a2SYan Zheng 			if (!should_cow_block(trans, root, b))
170865b51a00SChris Mason 				goto cow_done;
17095d4f98a2SYan Zheng 
1710b4ce94deSChris Mason 			btrfs_set_path_blocking(p);
1711b4ce94deSChris Mason 
1712bd681513SChris Mason 			/*
1713bd681513SChris Mason 			 * must have write locks on this node and the
1714bd681513SChris Mason 			 * parent
1715bd681513SChris Mason 			 */
1716bd681513SChris Mason 			if (level + 1 > write_lock_level) {
1717bd681513SChris Mason 				write_lock_level = level + 1;
1718bd681513SChris Mason 				btrfs_release_path(p);
1719bd681513SChris Mason 				goto again;
1720bd681513SChris Mason 			}
1721bd681513SChris Mason 
172233c66f43SYan Zheng 			err = btrfs_cow_block(trans, root, b,
1723e20d96d6SChris Mason 					      p->nodes[level + 1],
17249fa8cfe7SChris Mason 					      p->slots[level + 1], &b);
172533c66f43SYan Zheng 			if (err) {
172633c66f43SYan Zheng 				ret = err;
172765b51a00SChris Mason 				goto done;
172854aa1f4dSChris Mason 			}
172902217ed2SChris Mason 		}
173065b51a00SChris Mason cow_done:
173102217ed2SChris Mason 		BUG_ON(!cow && ins_len);
173265b51a00SChris Mason 
1733eb60ceacSChris Mason 		p->nodes[level] = b;
1734bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
1735b4ce94deSChris Mason 
1736b4ce94deSChris Mason 		/*
1737b4ce94deSChris Mason 		 * we have a lock on b and as long as we aren't changing
1738b4ce94deSChris Mason 		 * the tree, there is no way to for the items in b to change.
1739b4ce94deSChris Mason 		 * It is safe to drop the lock on our parent before we
1740b4ce94deSChris Mason 		 * go through the expensive btree search on b.
1741b4ce94deSChris Mason 		 *
1742b4ce94deSChris Mason 		 * If cow is true, then we might be changing slot zero,
1743b4ce94deSChris Mason 		 * which may require changing the parent.  So, we can't
1744b4ce94deSChris Mason 		 * drop the lock until after we know which slot we're
1745b4ce94deSChris Mason 		 * operating on.
1746b4ce94deSChris Mason 		 */
1747b4ce94deSChris Mason 		if (!cow)
1748b4ce94deSChris Mason 			btrfs_unlock_up_safe(p, level + 1);
1749b4ce94deSChris Mason 
17505f39d397SChris Mason 		ret = bin_search(b, key, level, &slot);
1751b4ce94deSChris Mason 
17525f39d397SChris Mason 		if (level != 0) {
175333c66f43SYan Zheng 			int dec = 0;
175433c66f43SYan Zheng 			if (ret && slot > 0) {
175533c66f43SYan Zheng 				dec = 1;
1756be0e5c09SChris Mason 				slot -= 1;
175733c66f43SYan Zheng 			}
1758be0e5c09SChris Mason 			p->slots[level] = slot;
175933c66f43SYan Zheng 			err = setup_nodes_for_search(trans, root, p, b, level,
1760bd681513SChris Mason 					     ins_len, &write_lock_level);
176133c66f43SYan Zheng 			if (err == -EAGAIN)
1762b4ce94deSChris Mason 				goto again;
176333c66f43SYan Zheng 			if (err) {
176433c66f43SYan Zheng 				ret = err;
176565b51a00SChris Mason 				goto done;
176633c66f43SYan Zheng 			}
17675c680ed6SChris Mason 			b = p->nodes[level];
17685c680ed6SChris Mason 			slot = p->slots[level];
1769b4ce94deSChris Mason 
1770bd681513SChris Mason 			/*
1771bd681513SChris Mason 			 * slot 0 is special, if we change the key
1772bd681513SChris Mason 			 * we have to update the parent pointer
1773bd681513SChris Mason 			 * which means we must have a write lock
1774bd681513SChris Mason 			 * on the parent
1775bd681513SChris Mason 			 */
1776bd681513SChris Mason 			if (slot == 0 && cow &&
1777bd681513SChris Mason 			    write_lock_level < level + 1) {
1778bd681513SChris Mason 				write_lock_level = level + 1;
1779bd681513SChris Mason 				btrfs_release_path(p);
1780bd681513SChris Mason 				goto again;
1781bd681513SChris Mason 			}
1782bd681513SChris Mason 
1783f9efa9c7SChris Mason 			unlock_up(p, level, lowest_unlock);
1784f9efa9c7SChris Mason 
1785925baeddSChris Mason 			if (level == lowest_level) {
178633c66f43SYan Zheng 				if (dec)
178733c66f43SYan Zheng 					p->slots[level]++;
17885b21f2edSZheng Yan 				goto done;
1789925baeddSChris Mason 			}
1790ca7a79adSChris Mason 
179133c66f43SYan Zheng 			err = read_block_for_search(trans, root, p,
1792c8c42864SChris Mason 						    &b, level, slot, key);
179333c66f43SYan Zheng 			if (err == -EAGAIN)
1794051e1b9fSChris Mason 				goto again;
179533c66f43SYan Zheng 			if (err) {
179633c66f43SYan Zheng 				ret = err;
179776a05b35SChris Mason 				goto done;
179833c66f43SYan Zheng 			}
179976a05b35SChris Mason 
1800b4ce94deSChris Mason 			if (!p->skip_locking) {
1801bd681513SChris Mason 				level = btrfs_header_level(b);
1802bd681513SChris Mason 				if (level <= write_lock_level) {
1803bd681513SChris Mason 					err = btrfs_try_tree_write_lock(b);
180433c66f43SYan Zheng 					if (!err) {
1805b4ce94deSChris Mason 						btrfs_set_path_blocking(p);
1806925baeddSChris Mason 						btrfs_tree_lock(b);
1807bd681513SChris Mason 						btrfs_clear_path_blocking(p, b,
1808bd681513SChris Mason 								  BTRFS_WRITE_LOCK);
1809b4ce94deSChris Mason 					}
1810bd681513SChris Mason 					p->locks[level] = BTRFS_WRITE_LOCK;
1811bd681513SChris Mason 				} else {
1812bd681513SChris Mason 					err = btrfs_try_tree_read_lock(b);
1813bd681513SChris Mason 					if (!err) {
1814bd681513SChris Mason 						btrfs_set_path_blocking(p);
1815bd681513SChris Mason 						btrfs_tree_read_lock(b);
1816bd681513SChris Mason 						btrfs_clear_path_blocking(p, b,
1817bd681513SChris Mason 								  BTRFS_READ_LOCK);
1818bd681513SChris Mason 					}
1819bd681513SChris Mason 					p->locks[level] = BTRFS_READ_LOCK;
1820bd681513SChris Mason 				}
1821bd681513SChris Mason 				p->nodes[level] = b;
1822b4ce94deSChris Mason 			}
1823be0e5c09SChris Mason 		} else {
1824be0e5c09SChris Mason 			p->slots[level] = slot;
182587b29b20SYan Zheng 			if (ins_len > 0 &&
182687b29b20SYan Zheng 			    btrfs_leaf_free_space(root, b) < ins_len) {
1827bd681513SChris Mason 				if (write_lock_level < 1) {
1828bd681513SChris Mason 					write_lock_level = 1;
1829bd681513SChris Mason 					btrfs_release_path(p);
1830bd681513SChris Mason 					goto again;
1831bd681513SChris Mason 				}
1832bd681513SChris Mason 
1833b4ce94deSChris Mason 				btrfs_set_path_blocking(p);
183433c66f43SYan Zheng 				err = split_leaf(trans, root, key,
1835cc0c5538SChris Mason 						 p, ins_len, ret == 0);
1836bd681513SChris Mason 				btrfs_clear_path_blocking(p, NULL, 0);
1837b4ce94deSChris Mason 
183833c66f43SYan Zheng 				BUG_ON(err > 0);
183933c66f43SYan Zheng 				if (err) {
184033c66f43SYan Zheng 					ret = err;
184165b51a00SChris Mason 					goto done;
184265b51a00SChris Mason 				}
18435c680ed6SChris Mason 			}
1844459931ecSChris Mason 			if (!p->search_for_split)
1845925baeddSChris Mason 				unlock_up(p, level, lowest_unlock);
184665b51a00SChris Mason 			goto done;
184765b51a00SChris Mason 		}
184865b51a00SChris Mason 	}
184965b51a00SChris Mason 	ret = 1;
185065b51a00SChris Mason done:
1851b4ce94deSChris Mason 	/*
1852b4ce94deSChris Mason 	 * we don't really know what they plan on doing with the path
1853b4ce94deSChris Mason 	 * from here on, so for now just mark it as blocking
1854b4ce94deSChris Mason 	 */
1855b9473439SChris Mason 	if (!p->leave_spinning)
1856b4ce94deSChris Mason 		btrfs_set_path_blocking(p);
185776a05b35SChris Mason 	if (ret < 0)
1858b3b4aa74SDavid Sterba 		btrfs_release_path(p);
1859be0e5c09SChris Mason 	return ret;
1860be0e5c09SChris Mason }
1861be0e5c09SChris Mason 
186274123bd7SChris Mason /*
186374123bd7SChris Mason  * adjust the pointers going up the tree, starting at level
186474123bd7SChris Mason  * making sure the right key of each node is points to 'key'.
186574123bd7SChris Mason  * This is used after shifting pointers to the left, so it stops
186674123bd7SChris Mason  * fixing up pointers when a given leaf/node is not in slot 0 of the
186774123bd7SChris Mason  * higher levels
1868aa5d6bedSChris Mason  *
1869aa5d6bedSChris Mason  * If this fails to write a tree block, it returns -1, but continues
1870aa5d6bedSChris Mason  * fixing up the blocks in ram so the tree is consistent.
187174123bd7SChris Mason  */
18725f39d397SChris Mason static int fixup_low_keys(struct btrfs_trans_handle *trans,
18735f39d397SChris Mason 			  struct btrfs_root *root, struct btrfs_path *path,
18745f39d397SChris Mason 			  struct btrfs_disk_key *key, int level)
1875be0e5c09SChris Mason {
1876be0e5c09SChris Mason 	int i;
1877aa5d6bedSChris Mason 	int ret = 0;
18785f39d397SChris Mason 	struct extent_buffer *t;
18795f39d397SChris Mason 
1880234b63a0SChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1881be0e5c09SChris Mason 		int tslot = path->slots[i];
1882eb60ceacSChris Mason 		if (!path->nodes[i])
1883be0e5c09SChris Mason 			break;
18845f39d397SChris Mason 		t = path->nodes[i];
18855f39d397SChris Mason 		btrfs_set_node_key(t, key, tslot);
1886d6025579SChris Mason 		btrfs_mark_buffer_dirty(path->nodes[i]);
1887be0e5c09SChris Mason 		if (tslot != 0)
1888be0e5c09SChris Mason 			break;
1889be0e5c09SChris Mason 	}
1890aa5d6bedSChris Mason 	return ret;
1891be0e5c09SChris Mason }
1892be0e5c09SChris Mason 
189374123bd7SChris Mason /*
189431840ae1SZheng Yan  * update item key.
189531840ae1SZheng Yan  *
189631840ae1SZheng Yan  * This function isn't completely safe. It's the caller's responsibility
189731840ae1SZheng Yan  * that the new key won't break the order
189831840ae1SZheng Yan  */
189931840ae1SZheng Yan int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
190031840ae1SZheng Yan 			    struct btrfs_root *root, struct btrfs_path *path,
190131840ae1SZheng Yan 			    struct btrfs_key *new_key)
190231840ae1SZheng Yan {
190331840ae1SZheng Yan 	struct btrfs_disk_key disk_key;
190431840ae1SZheng Yan 	struct extent_buffer *eb;
190531840ae1SZheng Yan 	int slot;
190631840ae1SZheng Yan 
190731840ae1SZheng Yan 	eb = path->nodes[0];
190831840ae1SZheng Yan 	slot = path->slots[0];
190931840ae1SZheng Yan 	if (slot > 0) {
191031840ae1SZheng Yan 		btrfs_item_key(eb, &disk_key, slot - 1);
191131840ae1SZheng Yan 		if (comp_keys(&disk_key, new_key) >= 0)
191231840ae1SZheng Yan 			return -1;
191331840ae1SZheng Yan 	}
191431840ae1SZheng Yan 	if (slot < btrfs_header_nritems(eb) - 1) {
191531840ae1SZheng Yan 		btrfs_item_key(eb, &disk_key, slot + 1);
191631840ae1SZheng Yan 		if (comp_keys(&disk_key, new_key) <= 0)
191731840ae1SZheng Yan 			return -1;
191831840ae1SZheng Yan 	}
191931840ae1SZheng Yan 
192031840ae1SZheng Yan 	btrfs_cpu_key_to_disk(&disk_key, new_key);
192131840ae1SZheng Yan 	btrfs_set_item_key(eb, &disk_key, slot);
192231840ae1SZheng Yan 	btrfs_mark_buffer_dirty(eb);
192331840ae1SZheng Yan 	if (slot == 0)
192431840ae1SZheng Yan 		fixup_low_keys(trans, root, path, &disk_key, 1);
192531840ae1SZheng Yan 	return 0;
192631840ae1SZheng Yan }
192731840ae1SZheng Yan 
192831840ae1SZheng Yan /*
192974123bd7SChris Mason  * try to push data from one node into the next node left in the
193079f95c82SChris Mason  * tree.
1931aa5d6bedSChris Mason  *
1932aa5d6bedSChris Mason  * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1933aa5d6bedSChris Mason  * error, and > 0 if there was no room in the left hand block.
193474123bd7SChris Mason  */
193598ed5174SChris Mason static int push_node_left(struct btrfs_trans_handle *trans,
193698ed5174SChris Mason 			  struct btrfs_root *root, struct extent_buffer *dst,
1937971a1f66SChris Mason 			  struct extent_buffer *src, int empty)
1938be0e5c09SChris Mason {
1939be0e5c09SChris Mason 	int push_items = 0;
1940bb803951SChris Mason 	int src_nritems;
1941bb803951SChris Mason 	int dst_nritems;
1942aa5d6bedSChris Mason 	int ret = 0;
1943be0e5c09SChris Mason 
19445f39d397SChris Mason 	src_nritems = btrfs_header_nritems(src);
19455f39d397SChris Mason 	dst_nritems = btrfs_header_nritems(dst);
1946123abc88SChris Mason 	push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
19477bb86316SChris Mason 	WARN_ON(btrfs_header_generation(src) != trans->transid);
19487bb86316SChris Mason 	WARN_ON(btrfs_header_generation(dst) != trans->transid);
194954aa1f4dSChris Mason 
1950bce4eae9SChris Mason 	if (!empty && src_nritems <= 8)
1951971a1f66SChris Mason 		return 1;
1952971a1f66SChris Mason 
1953d397712bSChris Mason 	if (push_items <= 0)
1954be0e5c09SChris Mason 		return 1;
1955be0e5c09SChris Mason 
1956bce4eae9SChris Mason 	if (empty) {
1957971a1f66SChris Mason 		push_items = min(src_nritems, push_items);
1958bce4eae9SChris Mason 		if (push_items < src_nritems) {
1959bce4eae9SChris Mason 			/* leave at least 8 pointers in the node if
1960bce4eae9SChris Mason 			 * we aren't going to empty it
1961bce4eae9SChris Mason 			 */
1962bce4eae9SChris Mason 			if (src_nritems - push_items < 8) {
1963bce4eae9SChris Mason 				if (push_items <= 8)
1964bce4eae9SChris Mason 					return 1;
1965bce4eae9SChris Mason 				push_items -= 8;
1966bce4eae9SChris Mason 			}
1967bce4eae9SChris Mason 		}
1968bce4eae9SChris Mason 	} else
1969bce4eae9SChris Mason 		push_items = min(src_nritems - 8, push_items);
197079f95c82SChris Mason 
19715f39d397SChris Mason 	copy_extent_buffer(dst, src,
19725f39d397SChris Mason 			   btrfs_node_key_ptr_offset(dst_nritems),
19735f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
1974123abc88SChris Mason 			   push_items * sizeof(struct btrfs_key_ptr));
19755f39d397SChris Mason 
1976bb803951SChris Mason 	if (push_items < src_nritems) {
19775f39d397SChris Mason 		memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
19785f39d397SChris Mason 				      btrfs_node_key_ptr_offset(push_items),
1979e2fa7227SChris Mason 				      (src_nritems - push_items) *
1980123abc88SChris Mason 				      sizeof(struct btrfs_key_ptr));
1981bb803951SChris Mason 	}
19825f39d397SChris Mason 	btrfs_set_header_nritems(src, src_nritems - push_items);
19835f39d397SChris Mason 	btrfs_set_header_nritems(dst, dst_nritems + push_items);
19845f39d397SChris Mason 	btrfs_mark_buffer_dirty(src);
19855f39d397SChris Mason 	btrfs_mark_buffer_dirty(dst);
198631840ae1SZheng Yan 
1987bb803951SChris Mason 	return ret;
1988be0e5c09SChris Mason }
1989be0e5c09SChris Mason 
199097571fd0SChris Mason /*
199179f95c82SChris Mason  * try to push data from one node into the next node right in the
199279f95c82SChris Mason  * tree.
199379f95c82SChris Mason  *
199479f95c82SChris Mason  * returns 0 if some ptrs were pushed, < 0 if there was some horrible
199579f95c82SChris Mason  * error, and > 0 if there was no room in the right hand block.
199679f95c82SChris Mason  *
199779f95c82SChris Mason  * this will  only push up to 1/2 the contents of the left node over
199879f95c82SChris Mason  */
19995f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans,
20005f39d397SChris Mason 			      struct btrfs_root *root,
20015f39d397SChris Mason 			      struct extent_buffer *dst,
20025f39d397SChris Mason 			      struct extent_buffer *src)
200379f95c82SChris Mason {
200479f95c82SChris Mason 	int push_items = 0;
200579f95c82SChris Mason 	int max_push;
200679f95c82SChris Mason 	int src_nritems;
200779f95c82SChris Mason 	int dst_nritems;
200879f95c82SChris Mason 	int ret = 0;
200979f95c82SChris Mason 
20107bb86316SChris Mason 	WARN_ON(btrfs_header_generation(src) != trans->transid);
20117bb86316SChris Mason 	WARN_ON(btrfs_header_generation(dst) != trans->transid);
20127bb86316SChris Mason 
20135f39d397SChris Mason 	src_nritems = btrfs_header_nritems(src);
20145f39d397SChris Mason 	dst_nritems = btrfs_header_nritems(dst);
2015123abc88SChris Mason 	push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
2016d397712bSChris Mason 	if (push_items <= 0)
201779f95c82SChris Mason 		return 1;
2018bce4eae9SChris Mason 
2019d397712bSChris Mason 	if (src_nritems < 4)
2020bce4eae9SChris Mason 		return 1;
202179f95c82SChris Mason 
202279f95c82SChris Mason 	max_push = src_nritems / 2 + 1;
202379f95c82SChris Mason 	/* don't try to empty the node */
2024d397712bSChris Mason 	if (max_push >= src_nritems)
202579f95c82SChris Mason 		return 1;
2026252c38f0SYan 
202779f95c82SChris Mason 	if (max_push < push_items)
202879f95c82SChris Mason 		push_items = max_push;
202979f95c82SChris Mason 
20305f39d397SChris Mason 	memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
20315f39d397SChris Mason 				      btrfs_node_key_ptr_offset(0),
20325f39d397SChris Mason 				      (dst_nritems) *
20335f39d397SChris Mason 				      sizeof(struct btrfs_key_ptr));
2034d6025579SChris Mason 
20355f39d397SChris Mason 	copy_extent_buffer(dst, src,
20365f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
20375f39d397SChris Mason 			   btrfs_node_key_ptr_offset(src_nritems - push_items),
2038123abc88SChris Mason 			   push_items * sizeof(struct btrfs_key_ptr));
203979f95c82SChris Mason 
20405f39d397SChris Mason 	btrfs_set_header_nritems(src, src_nritems - push_items);
20415f39d397SChris Mason 	btrfs_set_header_nritems(dst, dst_nritems + push_items);
204279f95c82SChris Mason 
20435f39d397SChris Mason 	btrfs_mark_buffer_dirty(src);
20445f39d397SChris Mason 	btrfs_mark_buffer_dirty(dst);
204531840ae1SZheng Yan 
204679f95c82SChris Mason 	return ret;
204779f95c82SChris Mason }
204879f95c82SChris Mason 
204979f95c82SChris Mason /*
205097571fd0SChris Mason  * helper function to insert a new root level in the tree.
205197571fd0SChris Mason  * A new node is allocated, and a single item is inserted to
205297571fd0SChris Mason  * point to the existing root
2053aa5d6bedSChris Mason  *
2054aa5d6bedSChris Mason  * returns zero on success or < 0 on failure.
205597571fd0SChris Mason  */
2056d397712bSChris Mason static noinline int insert_new_root(struct btrfs_trans_handle *trans,
20575f39d397SChris Mason 			   struct btrfs_root *root,
20585f39d397SChris Mason 			   struct btrfs_path *path, int level)
205974123bd7SChris Mason {
20607bb86316SChris Mason 	u64 lower_gen;
20615f39d397SChris Mason 	struct extent_buffer *lower;
20625f39d397SChris Mason 	struct extent_buffer *c;
2063925baeddSChris Mason 	struct extent_buffer *old;
20645f39d397SChris Mason 	struct btrfs_disk_key lower_key;
20655c680ed6SChris Mason 
20665c680ed6SChris Mason 	BUG_ON(path->nodes[level]);
20675c680ed6SChris Mason 	BUG_ON(path->nodes[level-1] != root->node);
20685c680ed6SChris Mason 
20697bb86316SChris Mason 	lower = path->nodes[level-1];
20707bb86316SChris Mason 	if (level == 1)
20717bb86316SChris Mason 		btrfs_item_key(lower, &lower_key, 0);
20727bb86316SChris Mason 	else
20737bb86316SChris Mason 		btrfs_node_key(lower, &lower_key, 0);
20747bb86316SChris Mason 
207531840ae1SZheng Yan 	c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
20765d4f98a2SYan Zheng 				   root->root_key.objectid, &lower_key,
2077ad3d81baSChristoph Hellwig 				   level, root->node->start, 0);
20785f39d397SChris Mason 	if (IS_ERR(c))
20795f39d397SChris Mason 		return PTR_ERR(c);
2080925baeddSChris Mason 
2081f0486c68SYan, Zheng 	root_add_used(root, root->nodesize);
2082f0486c68SYan, Zheng 
20835d4f98a2SYan Zheng 	memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
20845f39d397SChris Mason 	btrfs_set_header_nritems(c, 1);
20855f39d397SChris Mason 	btrfs_set_header_level(c, level);
2086db94535dSChris Mason 	btrfs_set_header_bytenr(c, c->start);
20875f39d397SChris Mason 	btrfs_set_header_generation(c, trans->transid);
20885d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
20895f39d397SChris Mason 	btrfs_set_header_owner(c, root->root_key.objectid);
2090d5719762SChris Mason 
20915f39d397SChris Mason 	write_extent_buffer(c, root->fs_info->fsid,
20925f39d397SChris Mason 			    (unsigned long)btrfs_header_fsid(c),
20935f39d397SChris Mason 			    BTRFS_FSID_SIZE);
2094e17cade2SChris Mason 
2095e17cade2SChris Mason 	write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2096e17cade2SChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(c),
2097e17cade2SChris Mason 			    BTRFS_UUID_SIZE);
2098e17cade2SChris Mason 
20995f39d397SChris Mason 	btrfs_set_node_key(c, &lower_key, 0);
2100db94535dSChris Mason 	btrfs_set_node_blockptr(c, 0, lower->start);
21017bb86316SChris Mason 	lower_gen = btrfs_header_generation(lower);
210231840ae1SZheng Yan 	WARN_ON(lower_gen != trans->transid);
21037bb86316SChris Mason 
21047bb86316SChris Mason 	btrfs_set_node_ptr_generation(c, 0, lower_gen);
21055f39d397SChris Mason 
21065f39d397SChris Mason 	btrfs_mark_buffer_dirty(c);
2107d5719762SChris Mason 
2108925baeddSChris Mason 	old = root->node;
2109240f62c8SChris Mason 	rcu_assign_pointer(root->node, c);
2110925baeddSChris Mason 
2111925baeddSChris Mason 	/* the super has an extra ref to root->node */
2112925baeddSChris Mason 	free_extent_buffer(old);
2113925baeddSChris Mason 
21140b86a832SChris Mason 	add_root_to_dirty_list(root);
21155f39d397SChris Mason 	extent_buffer_get(c);
21165f39d397SChris Mason 	path->nodes[level] = c;
2117bd681513SChris Mason 	path->locks[level] = BTRFS_WRITE_LOCK;
211874123bd7SChris Mason 	path->slots[level] = 0;
211974123bd7SChris Mason 	return 0;
212074123bd7SChris Mason }
21215c680ed6SChris Mason 
21225c680ed6SChris Mason /*
21235c680ed6SChris Mason  * worker function to insert a single pointer in a node.
21245c680ed6SChris Mason  * the node should have enough room for the pointer already
212597571fd0SChris Mason  *
21265c680ed6SChris Mason  * slot and level indicate where you want the key to go, and
21275c680ed6SChris Mason  * blocknr is the block the key points to.
2128aa5d6bedSChris Mason  *
2129aa5d6bedSChris Mason  * returns zero on success and < 0 on any error
21305c680ed6SChris Mason  */
2131e089f05cSChris Mason static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2132e089f05cSChris Mason 		      *root, struct btrfs_path *path, struct btrfs_disk_key
2133db94535dSChris Mason 		      *key, u64 bytenr, int slot, int level)
21345c680ed6SChris Mason {
21355f39d397SChris Mason 	struct extent_buffer *lower;
21365c680ed6SChris Mason 	int nritems;
21375c680ed6SChris Mason 
21385c680ed6SChris Mason 	BUG_ON(!path->nodes[level]);
2139f0486c68SYan, Zheng 	btrfs_assert_tree_locked(path->nodes[level]);
21405f39d397SChris Mason 	lower = path->nodes[level];
21415f39d397SChris Mason 	nritems = btrfs_header_nritems(lower);
2142c293498bSStoyan Gaydarov 	BUG_ON(slot > nritems);
2143123abc88SChris Mason 	if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
214474123bd7SChris Mason 		BUG();
214574123bd7SChris Mason 	if (slot != nritems) {
21465f39d397SChris Mason 		memmove_extent_buffer(lower,
21475f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot + 1),
21485f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot),
2149123abc88SChris Mason 			      (nritems - slot) * sizeof(struct btrfs_key_ptr));
215074123bd7SChris Mason 	}
21515f39d397SChris Mason 	btrfs_set_node_key(lower, key, slot);
2152db94535dSChris Mason 	btrfs_set_node_blockptr(lower, slot, bytenr);
215374493f7aSChris Mason 	WARN_ON(trans->transid == 0);
215474493f7aSChris Mason 	btrfs_set_node_ptr_generation(lower, slot, trans->transid);
21555f39d397SChris Mason 	btrfs_set_header_nritems(lower, nritems + 1);
21565f39d397SChris Mason 	btrfs_mark_buffer_dirty(lower);
215774123bd7SChris Mason 	return 0;
215874123bd7SChris Mason }
215974123bd7SChris Mason 
216097571fd0SChris Mason /*
216197571fd0SChris Mason  * split the node at the specified level in path in two.
216297571fd0SChris Mason  * The path is corrected to point to the appropriate node after the split
216397571fd0SChris Mason  *
216497571fd0SChris Mason  * Before splitting this tries to make some room in the node by pushing
216597571fd0SChris Mason  * left and right, if either one works, it returns right away.
2166aa5d6bedSChris Mason  *
2167aa5d6bedSChris Mason  * returns 0 on success and < 0 on failure
216897571fd0SChris Mason  */
2169e02119d5SChris Mason static noinline int split_node(struct btrfs_trans_handle *trans,
2170e02119d5SChris Mason 			       struct btrfs_root *root,
2171e02119d5SChris Mason 			       struct btrfs_path *path, int level)
2172be0e5c09SChris Mason {
21735f39d397SChris Mason 	struct extent_buffer *c;
21745f39d397SChris Mason 	struct extent_buffer *split;
21755f39d397SChris Mason 	struct btrfs_disk_key disk_key;
2176be0e5c09SChris Mason 	int mid;
21775c680ed6SChris Mason 	int ret;
2178aa5d6bedSChris Mason 	int wret;
21797518a238SChris Mason 	u32 c_nritems;
2180be0e5c09SChris Mason 
21815f39d397SChris Mason 	c = path->nodes[level];
21827bb86316SChris Mason 	WARN_ON(btrfs_header_generation(c) != trans->transid);
21835f39d397SChris Mason 	if (c == root->node) {
21845c680ed6SChris Mason 		/* trying to split the root, lets make a new one */
2185e089f05cSChris Mason 		ret = insert_new_root(trans, root, path, level + 1);
21865c680ed6SChris Mason 		if (ret)
21875c680ed6SChris Mason 			return ret;
2188b3612421SChris Mason 	} else {
2189e66f709bSChris Mason 		ret = push_nodes_for_insert(trans, root, path, level);
21905f39d397SChris Mason 		c = path->nodes[level];
21915f39d397SChris Mason 		if (!ret && btrfs_header_nritems(c) <
2192c448acf0SChris Mason 		    BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
2193e66f709bSChris Mason 			return 0;
219454aa1f4dSChris Mason 		if (ret < 0)
219554aa1f4dSChris Mason 			return ret;
21965c680ed6SChris Mason 	}
2197e66f709bSChris Mason 
21985f39d397SChris Mason 	c_nritems = btrfs_header_nritems(c);
21995d4f98a2SYan Zheng 	mid = (c_nritems + 1) / 2;
22005d4f98a2SYan Zheng 	btrfs_node_key(c, &disk_key, mid);
22017bb86316SChris Mason 
22025d4f98a2SYan Zheng 	split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
22037bb86316SChris Mason 					root->root_key.objectid,
22045d4f98a2SYan Zheng 					&disk_key, level, c->start, 0);
22055f39d397SChris Mason 	if (IS_ERR(split))
22065f39d397SChris Mason 		return PTR_ERR(split);
220754aa1f4dSChris Mason 
2208f0486c68SYan, Zheng 	root_add_used(root, root->nodesize);
2209f0486c68SYan, Zheng 
22105d4f98a2SYan Zheng 	memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
22115f39d397SChris Mason 	btrfs_set_header_level(split, btrfs_header_level(c));
2212db94535dSChris Mason 	btrfs_set_header_bytenr(split, split->start);
22135f39d397SChris Mason 	btrfs_set_header_generation(split, trans->transid);
22145d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
22155f39d397SChris Mason 	btrfs_set_header_owner(split, root->root_key.objectid);
22165f39d397SChris Mason 	write_extent_buffer(split, root->fs_info->fsid,
22175f39d397SChris Mason 			    (unsigned long)btrfs_header_fsid(split),
22185f39d397SChris Mason 			    BTRFS_FSID_SIZE);
2219e17cade2SChris Mason 	write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2220e17cade2SChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(split),
2221e17cade2SChris Mason 			    BTRFS_UUID_SIZE);
22225f39d397SChris Mason 
22235f39d397SChris Mason 
22245f39d397SChris Mason 	copy_extent_buffer(split, c,
22255f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
22265f39d397SChris Mason 			   btrfs_node_key_ptr_offset(mid),
2227123abc88SChris Mason 			   (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
22285f39d397SChris Mason 	btrfs_set_header_nritems(split, c_nritems - mid);
22295f39d397SChris Mason 	btrfs_set_header_nritems(c, mid);
2230aa5d6bedSChris Mason 	ret = 0;
2231aa5d6bedSChris Mason 
22325f39d397SChris Mason 	btrfs_mark_buffer_dirty(c);
22335f39d397SChris Mason 	btrfs_mark_buffer_dirty(split);
22345f39d397SChris Mason 
2235db94535dSChris Mason 	wret = insert_ptr(trans, root, path, &disk_key, split->start,
22365f39d397SChris Mason 			  path->slots[level + 1] + 1,
2237123abc88SChris Mason 			  level + 1);
2238aa5d6bedSChris Mason 	if (wret)
2239aa5d6bedSChris Mason 		ret = wret;
2240aa5d6bedSChris Mason 
22415de08d7dSChris Mason 	if (path->slots[level] >= mid) {
22425c680ed6SChris Mason 		path->slots[level] -= mid;
2243925baeddSChris Mason 		btrfs_tree_unlock(c);
22445f39d397SChris Mason 		free_extent_buffer(c);
22455f39d397SChris Mason 		path->nodes[level] = split;
22465c680ed6SChris Mason 		path->slots[level + 1] += 1;
2247eb60ceacSChris Mason 	} else {
2248925baeddSChris Mason 		btrfs_tree_unlock(split);
22495f39d397SChris Mason 		free_extent_buffer(split);
2250be0e5c09SChris Mason 	}
2251aa5d6bedSChris Mason 	return ret;
2252be0e5c09SChris Mason }
2253be0e5c09SChris Mason 
225474123bd7SChris Mason /*
225574123bd7SChris Mason  * how many bytes are required to store the items in a leaf.  start
225674123bd7SChris Mason  * and nr indicate which items in the leaf to check.  This totals up the
225774123bd7SChris Mason  * space used both by the item structs and the item data
225874123bd7SChris Mason  */
22595f39d397SChris Mason static int leaf_space_used(struct extent_buffer *l, int start, int nr)
2260be0e5c09SChris Mason {
2261be0e5c09SChris Mason 	int data_len;
22625f39d397SChris Mason 	int nritems = btrfs_header_nritems(l);
2263d4dbff95SChris Mason 	int end = min(nritems, start + nr) - 1;
2264be0e5c09SChris Mason 
2265be0e5c09SChris Mason 	if (!nr)
2266be0e5c09SChris Mason 		return 0;
22675f39d397SChris Mason 	data_len = btrfs_item_end_nr(l, start);
22685f39d397SChris Mason 	data_len = data_len - btrfs_item_offset_nr(l, end);
22690783fcfcSChris Mason 	data_len += sizeof(struct btrfs_item) * nr;
2270d4dbff95SChris Mason 	WARN_ON(data_len < 0);
2271be0e5c09SChris Mason 	return data_len;
2272be0e5c09SChris Mason }
2273be0e5c09SChris Mason 
227474123bd7SChris Mason /*
2275d4dbff95SChris Mason  * The space between the end of the leaf items and
2276d4dbff95SChris Mason  * the start of the leaf data.  IOW, how much room
2277d4dbff95SChris Mason  * the leaf has left for both items and data
2278d4dbff95SChris Mason  */
2279d397712bSChris Mason noinline int btrfs_leaf_free_space(struct btrfs_root *root,
2280e02119d5SChris Mason 				   struct extent_buffer *leaf)
2281d4dbff95SChris Mason {
22825f39d397SChris Mason 	int nritems = btrfs_header_nritems(leaf);
22835f39d397SChris Mason 	int ret;
22845f39d397SChris Mason 	ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
22855f39d397SChris Mason 	if (ret < 0) {
2286d397712bSChris Mason 		printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2287d397712bSChris Mason 		       "used %d nritems %d\n",
2288ae2f5411SJens Axboe 		       ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
22895f39d397SChris Mason 		       leaf_space_used(leaf, 0, nritems), nritems);
22905f39d397SChris Mason 	}
22915f39d397SChris Mason 	return ret;
2292d4dbff95SChris Mason }
2293d4dbff95SChris Mason 
229499d8f83cSChris Mason /*
229599d8f83cSChris Mason  * min slot controls the lowest index we're willing to push to the
229699d8f83cSChris Mason  * right.  We'll push up to and including min_slot, but no lower
229799d8f83cSChris Mason  */
229844871b1bSChris Mason static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
229944871b1bSChris Mason 				      struct btrfs_root *root,
230044871b1bSChris Mason 				      struct btrfs_path *path,
230144871b1bSChris Mason 				      int data_size, int empty,
230244871b1bSChris Mason 				      struct extent_buffer *right,
230399d8f83cSChris Mason 				      int free_space, u32 left_nritems,
230499d8f83cSChris Mason 				      u32 min_slot)
230500ec4c51SChris Mason {
23065f39d397SChris Mason 	struct extent_buffer *left = path->nodes[0];
230744871b1bSChris Mason 	struct extent_buffer *upper = path->nodes[1];
23085f39d397SChris Mason 	struct btrfs_disk_key disk_key;
230900ec4c51SChris Mason 	int slot;
231034a38218SChris Mason 	u32 i;
231100ec4c51SChris Mason 	int push_space = 0;
231200ec4c51SChris Mason 	int push_items = 0;
23130783fcfcSChris Mason 	struct btrfs_item *item;
231434a38218SChris Mason 	u32 nr;
23157518a238SChris Mason 	u32 right_nritems;
23165f39d397SChris Mason 	u32 data_end;
2317db94535dSChris Mason 	u32 this_item_size;
231800ec4c51SChris Mason 
231934a38218SChris Mason 	if (empty)
232034a38218SChris Mason 		nr = 0;
232134a38218SChris Mason 	else
232299d8f83cSChris Mason 		nr = max_t(u32, 1, min_slot);
232334a38218SChris Mason 
232431840ae1SZheng Yan 	if (path->slots[0] >= left_nritems)
232587b29b20SYan Zheng 		push_space += data_size;
232631840ae1SZheng Yan 
232744871b1bSChris Mason 	slot = path->slots[1];
232834a38218SChris Mason 	i = left_nritems - 1;
232934a38218SChris Mason 	while (i >= nr) {
23305f39d397SChris Mason 		item = btrfs_item_nr(left, i);
2331db94535dSChris Mason 
233231840ae1SZheng Yan 		if (!empty && push_items > 0) {
233331840ae1SZheng Yan 			if (path->slots[0] > i)
233431840ae1SZheng Yan 				break;
233531840ae1SZheng Yan 			if (path->slots[0] == i) {
233631840ae1SZheng Yan 				int space = btrfs_leaf_free_space(root, left);
233731840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
233831840ae1SZheng Yan 					break;
233931840ae1SZheng Yan 			}
234031840ae1SZheng Yan 		}
234131840ae1SZheng Yan 
234200ec4c51SChris Mason 		if (path->slots[0] == i)
234387b29b20SYan Zheng 			push_space += data_size;
2344db94535dSChris Mason 
2345db94535dSChris Mason 		this_item_size = btrfs_item_size(left, item);
2346db94535dSChris Mason 		if (this_item_size + sizeof(*item) + push_space > free_space)
234700ec4c51SChris Mason 			break;
234831840ae1SZheng Yan 
234900ec4c51SChris Mason 		push_items++;
2350db94535dSChris Mason 		push_space += this_item_size + sizeof(*item);
235134a38218SChris Mason 		if (i == 0)
235234a38218SChris Mason 			break;
235334a38218SChris Mason 		i--;
2354db94535dSChris Mason 	}
23555f39d397SChris Mason 
2356925baeddSChris Mason 	if (push_items == 0)
2357925baeddSChris Mason 		goto out_unlock;
23585f39d397SChris Mason 
235934a38218SChris Mason 	if (!empty && push_items == left_nritems)
2360a429e513SChris Mason 		WARN_ON(1);
23615f39d397SChris Mason 
236200ec4c51SChris Mason 	/* push left to right */
23635f39d397SChris Mason 	right_nritems = btrfs_header_nritems(right);
236434a38218SChris Mason 
23655f39d397SChris Mason 	push_space = btrfs_item_end_nr(left, left_nritems - push_items);
2366123abc88SChris Mason 	push_space -= leaf_data_end(root, left);
23675f39d397SChris Mason 
236800ec4c51SChris Mason 	/* make room in the right data area */
23695f39d397SChris Mason 	data_end = leaf_data_end(root, right);
23705f39d397SChris Mason 	memmove_extent_buffer(right,
23715f39d397SChris Mason 			      btrfs_leaf_data(right) + data_end - push_space,
23725f39d397SChris Mason 			      btrfs_leaf_data(right) + data_end,
23735f39d397SChris Mason 			      BTRFS_LEAF_DATA_SIZE(root) - data_end);
23745f39d397SChris Mason 
237500ec4c51SChris Mason 	/* copy from the left data area */
23765f39d397SChris Mason 	copy_extent_buffer(right, left, btrfs_leaf_data(right) +
2377d6025579SChris Mason 		     BTRFS_LEAF_DATA_SIZE(root) - push_space,
2378d6025579SChris Mason 		     btrfs_leaf_data(left) + leaf_data_end(root, left),
2379d6025579SChris Mason 		     push_space);
23805f39d397SChris Mason 
23815f39d397SChris Mason 	memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
23825f39d397SChris Mason 			      btrfs_item_nr_offset(0),
23830783fcfcSChris Mason 			      right_nritems * sizeof(struct btrfs_item));
23845f39d397SChris Mason 
238500ec4c51SChris Mason 	/* copy the items from left to right */
23865f39d397SChris Mason 	copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
23875f39d397SChris Mason 		   btrfs_item_nr_offset(left_nritems - push_items),
23880783fcfcSChris Mason 		   push_items * sizeof(struct btrfs_item));
238900ec4c51SChris Mason 
239000ec4c51SChris Mason 	/* update the item pointers */
23917518a238SChris Mason 	right_nritems += push_items;
23925f39d397SChris Mason 	btrfs_set_header_nritems(right, right_nritems);
2393123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root);
23947518a238SChris Mason 	for (i = 0; i < right_nritems; i++) {
23955f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2396db94535dSChris Mason 		push_space -= btrfs_item_size(right, item);
2397db94535dSChris Mason 		btrfs_set_item_offset(right, item, push_space);
2398db94535dSChris Mason 	}
2399db94535dSChris Mason 
24007518a238SChris Mason 	left_nritems -= push_items;
24015f39d397SChris Mason 	btrfs_set_header_nritems(left, left_nritems);
240200ec4c51SChris Mason 
240334a38218SChris Mason 	if (left_nritems)
24045f39d397SChris Mason 		btrfs_mark_buffer_dirty(left);
2405f0486c68SYan, Zheng 	else
2406f0486c68SYan, Zheng 		clean_tree_block(trans, root, left);
2407f0486c68SYan, Zheng 
24085f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
2409a429e513SChris Mason 
24105f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
24115f39d397SChris Mason 	btrfs_set_node_key(upper, &disk_key, slot + 1);
2412d6025579SChris Mason 	btrfs_mark_buffer_dirty(upper);
241302217ed2SChris Mason 
241400ec4c51SChris Mason 	/* then fixup the leaf pointer in the path */
24157518a238SChris Mason 	if (path->slots[0] >= left_nritems) {
24167518a238SChris Mason 		path->slots[0] -= left_nritems;
2417925baeddSChris Mason 		if (btrfs_header_nritems(path->nodes[0]) == 0)
2418925baeddSChris Mason 			clean_tree_block(trans, root, path->nodes[0]);
2419925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
24205f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
24215f39d397SChris Mason 		path->nodes[0] = right;
242200ec4c51SChris Mason 		path->slots[1] += 1;
242300ec4c51SChris Mason 	} else {
2424925baeddSChris Mason 		btrfs_tree_unlock(right);
24255f39d397SChris Mason 		free_extent_buffer(right);
242600ec4c51SChris Mason 	}
242700ec4c51SChris Mason 	return 0;
2428925baeddSChris Mason 
2429925baeddSChris Mason out_unlock:
2430925baeddSChris Mason 	btrfs_tree_unlock(right);
2431925baeddSChris Mason 	free_extent_buffer(right);
2432925baeddSChris Mason 	return 1;
243300ec4c51SChris Mason }
2434925baeddSChris Mason 
243500ec4c51SChris Mason /*
243644871b1bSChris Mason  * push some data in the path leaf to the right, trying to free up at
243774123bd7SChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
243844871b1bSChris Mason  *
243944871b1bSChris Mason  * returns 1 if the push failed because the other node didn't have enough
244044871b1bSChris Mason  * room, 0 if everything worked out and < 0 if there were major errors.
244199d8f83cSChris Mason  *
244299d8f83cSChris Mason  * this will push starting from min_slot to the end of the leaf.  It won't
244399d8f83cSChris Mason  * push any slot lower than min_slot
244474123bd7SChris Mason  */
244544871b1bSChris Mason static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
244699d8f83cSChris Mason 			   *root, struct btrfs_path *path,
244799d8f83cSChris Mason 			   int min_data_size, int data_size,
244899d8f83cSChris Mason 			   int empty, u32 min_slot)
2449be0e5c09SChris Mason {
245044871b1bSChris Mason 	struct extent_buffer *left = path->nodes[0];
245144871b1bSChris Mason 	struct extent_buffer *right;
245244871b1bSChris Mason 	struct extent_buffer *upper;
245344871b1bSChris Mason 	int slot;
245444871b1bSChris Mason 	int free_space;
245544871b1bSChris Mason 	u32 left_nritems;
245644871b1bSChris Mason 	int ret;
245744871b1bSChris Mason 
245844871b1bSChris Mason 	if (!path->nodes[1])
245944871b1bSChris Mason 		return 1;
246044871b1bSChris Mason 
246144871b1bSChris Mason 	slot = path->slots[1];
246244871b1bSChris Mason 	upper = path->nodes[1];
246344871b1bSChris Mason 	if (slot >= btrfs_header_nritems(upper) - 1)
246444871b1bSChris Mason 		return 1;
246544871b1bSChris Mason 
246644871b1bSChris Mason 	btrfs_assert_tree_locked(path->nodes[1]);
246744871b1bSChris Mason 
246844871b1bSChris Mason 	right = read_node_slot(root, upper, slot + 1);
246991ca338dSTsutomu Itoh 	if (right == NULL)
247091ca338dSTsutomu Itoh 		return 1;
247191ca338dSTsutomu Itoh 
247244871b1bSChris Mason 	btrfs_tree_lock(right);
247344871b1bSChris Mason 	btrfs_set_lock_blocking(right);
247444871b1bSChris Mason 
247544871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, right);
247644871b1bSChris Mason 	if (free_space < data_size)
247744871b1bSChris Mason 		goto out_unlock;
247844871b1bSChris Mason 
247944871b1bSChris Mason 	/* cow and double check */
248044871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, right, upper,
248144871b1bSChris Mason 			      slot + 1, &right);
248244871b1bSChris Mason 	if (ret)
248344871b1bSChris Mason 		goto out_unlock;
248444871b1bSChris Mason 
248544871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, right);
248644871b1bSChris Mason 	if (free_space < data_size)
248744871b1bSChris Mason 		goto out_unlock;
248844871b1bSChris Mason 
248944871b1bSChris Mason 	left_nritems = btrfs_header_nritems(left);
249044871b1bSChris Mason 	if (left_nritems == 0)
249144871b1bSChris Mason 		goto out_unlock;
249244871b1bSChris Mason 
249399d8f83cSChris Mason 	return __push_leaf_right(trans, root, path, min_data_size, empty,
249499d8f83cSChris Mason 				right, free_space, left_nritems, min_slot);
249544871b1bSChris Mason out_unlock:
249644871b1bSChris Mason 	btrfs_tree_unlock(right);
249744871b1bSChris Mason 	free_extent_buffer(right);
249844871b1bSChris Mason 	return 1;
249944871b1bSChris Mason }
250044871b1bSChris Mason 
250144871b1bSChris Mason /*
250244871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
250344871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
250499d8f83cSChris Mason  *
250599d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
250699d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us do all the
250799d8f83cSChris Mason  * items
250844871b1bSChris Mason  */
250944871b1bSChris Mason static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
251044871b1bSChris Mason 				     struct btrfs_root *root,
251144871b1bSChris Mason 				     struct btrfs_path *path, int data_size,
251244871b1bSChris Mason 				     int empty, struct extent_buffer *left,
251399d8f83cSChris Mason 				     int free_space, u32 right_nritems,
251499d8f83cSChris Mason 				     u32 max_slot)
251544871b1bSChris Mason {
25165f39d397SChris Mason 	struct btrfs_disk_key disk_key;
25175f39d397SChris Mason 	struct extent_buffer *right = path->nodes[0];
2518be0e5c09SChris Mason 	int i;
2519be0e5c09SChris Mason 	int push_space = 0;
2520be0e5c09SChris Mason 	int push_items = 0;
25210783fcfcSChris Mason 	struct btrfs_item *item;
25227518a238SChris Mason 	u32 old_left_nritems;
252334a38218SChris Mason 	u32 nr;
2524aa5d6bedSChris Mason 	int ret = 0;
2525aa5d6bedSChris Mason 	int wret;
2526db94535dSChris Mason 	u32 this_item_size;
2527db94535dSChris Mason 	u32 old_left_item_size;
2528be0e5c09SChris Mason 
252934a38218SChris Mason 	if (empty)
253099d8f83cSChris Mason 		nr = min(right_nritems, max_slot);
253134a38218SChris Mason 	else
253299d8f83cSChris Mason 		nr = min(right_nritems - 1, max_slot);
253334a38218SChris Mason 
253434a38218SChris Mason 	for (i = 0; i < nr; i++) {
25355f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2536db94535dSChris Mason 
253731840ae1SZheng Yan 		if (!empty && push_items > 0) {
253831840ae1SZheng Yan 			if (path->slots[0] < i)
253931840ae1SZheng Yan 				break;
254031840ae1SZheng Yan 			if (path->slots[0] == i) {
254131840ae1SZheng Yan 				int space = btrfs_leaf_free_space(root, right);
254231840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
254331840ae1SZheng Yan 					break;
254431840ae1SZheng Yan 			}
254531840ae1SZheng Yan 		}
254631840ae1SZheng Yan 
2547be0e5c09SChris Mason 		if (path->slots[0] == i)
254887b29b20SYan Zheng 			push_space += data_size;
2549db94535dSChris Mason 
2550db94535dSChris Mason 		this_item_size = btrfs_item_size(right, item);
2551db94535dSChris Mason 		if (this_item_size + sizeof(*item) + push_space > free_space)
2552be0e5c09SChris Mason 			break;
2553db94535dSChris Mason 
2554be0e5c09SChris Mason 		push_items++;
2555db94535dSChris Mason 		push_space += this_item_size + sizeof(*item);
2556be0e5c09SChris Mason 	}
2557db94535dSChris Mason 
2558be0e5c09SChris Mason 	if (push_items == 0) {
2559925baeddSChris Mason 		ret = 1;
2560925baeddSChris Mason 		goto out;
2561be0e5c09SChris Mason 	}
256234a38218SChris Mason 	if (!empty && push_items == btrfs_header_nritems(right))
2563a429e513SChris Mason 		WARN_ON(1);
25645f39d397SChris Mason 
2565be0e5c09SChris Mason 	/* push data from right to left */
25665f39d397SChris Mason 	copy_extent_buffer(left, right,
25675f39d397SChris Mason 			   btrfs_item_nr_offset(btrfs_header_nritems(left)),
25685f39d397SChris Mason 			   btrfs_item_nr_offset(0),
25695f39d397SChris Mason 			   push_items * sizeof(struct btrfs_item));
25705f39d397SChris Mason 
2571123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root) -
25725f39d397SChris Mason 		     btrfs_item_offset_nr(right, push_items - 1);
25735f39d397SChris Mason 
25745f39d397SChris Mason 	copy_extent_buffer(left, right, btrfs_leaf_data(left) +
2575d6025579SChris Mason 		     leaf_data_end(root, left) - push_space,
2576123abc88SChris Mason 		     btrfs_leaf_data(right) +
25775f39d397SChris Mason 		     btrfs_item_offset_nr(right, push_items - 1),
2578be0e5c09SChris Mason 		     push_space);
25795f39d397SChris Mason 	old_left_nritems = btrfs_header_nritems(left);
258087b29b20SYan Zheng 	BUG_ON(old_left_nritems <= 0);
2581eb60ceacSChris Mason 
2582db94535dSChris Mason 	old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
2583be0e5c09SChris Mason 	for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
25845f39d397SChris Mason 		u32 ioff;
2585db94535dSChris Mason 
25865f39d397SChris Mason 		item = btrfs_item_nr(left, i);
2587db94535dSChris Mason 
25885f39d397SChris Mason 		ioff = btrfs_item_offset(left, item);
25895f39d397SChris Mason 		btrfs_set_item_offset(left, item,
2590db94535dSChris Mason 		      ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
2591be0e5c09SChris Mason 	}
25925f39d397SChris Mason 	btrfs_set_header_nritems(left, old_left_nritems + push_items);
2593be0e5c09SChris Mason 
2594be0e5c09SChris Mason 	/* fixup right node */
259534a38218SChris Mason 	if (push_items > right_nritems) {
2596d397712bSChris Mason 		printk(KERN_CRIT "push items %d nr %u\n", push_items,
2597d397712bSChris Mason 		       right_nritems);
259834a38218SChris Mason 		WARN_ON(1);
259934a38218SChris Mason 	}
260034a38218SChris Mason 
260134a38218SChris Mason 	if (push_items < right_nritems) {
26025f39d397SChris Mason 		push_space = btrfs_item_offset_nr(right, push_items - 1) -
2603123abc88SChris Mason 						  leaf_data_end(root, right);
26045f39d397SChris Mason 		memmove_extent_buffer(right, btrfs_leaf_data(right) +
2605d6025579SChris Mason 				      BTRFS_LEAF_DATA_SIZE(root) - push_space,
2606d6025579SChris Mason 				      btrfs_leaf_data(right) +
2607123abc88SChris Mason 				      leaf_data_end(root, right), push_space);
26085f39d397SChris Mason 
26095f39d397SChris Mason 		memmove_extent_buffer(right, btrfs_item_nr_offset(0),
26105f39d397SChris Mason 			      btrfs_item_nr_offset(push_items),
26115f39d397SChris Mason 			     (btrfs_header_nritems(right) - push_items) *
26120783fcfcSChris Mason 			     sizeof(struct btrfs_item));
261334a38218SChris Mason 	}
2614eef1c494SYan 	right_nritems -= push_items;
2615eef1c494SYan 	btrfs_set_header_nritems(right, right_nritems);
2616123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root);
26175f39d397SChris Mason 	for (i = 0; i < right_nritems; i++) {
26185f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2619db94535dSChris Mason 
2620db94535dSChris Mason 		push_space = push_space - btrfs_item_size(right, item);
2621db94535dSChris Mason 		btrfs_set_item_offset(right, item, push_space);
2622db94535dSChris Mason 	}
2623eb60ceacSChris Mason 
26245f39d397SChris Mason 	btrfs_mark_buffer_dirty(left);
262534a38218SChris Mason 	if (right_nritems)
26265f39d397SChris Mason 		btrfs_mark_buffer_dirty(right);
2627f0486c68SYan, Zheng 	else
2628f0486c68SYan, Zheng 		clean_tree_block(trans, root, right);
2629098f59c2SChris Mason 
26305f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
26315f39d397SChris Mason 	wret = fixup_low_keys(trans, root, path, &disk_key, 1);
2632aa5d6bedSChris Mason 	if (wret)
2633aa5d6bedSChris Mason 		ret = wret;
2634be0e5c09SChris Mason 
2635be0e5c09SChris Mason 	/* then fixup the leaf pointer in the path */
2636be0e5c09SChris Mason 	if (path->slots[0] < push_items) {
2637be0e5c09SChris Mason 		path->slots[0] += old_left_nritems;
2638925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
26395f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
26405f39d397SChris Mason 		path->nodes[0] = left;
2641be0e5c09SChris Mason 		path->slots[1] -= 1;
2642be0e5c09SChris Mason 	} else {
2643925baeddSChris Mason 		btrfs_tree_unlock(left);
26445f39d397SChris Mason 		free_extent_buffer(left);
2645be0e5c09SChris Mason 		path->slots[0] -= push_items;
2646be0e5c09SChris Mason 	}
2647eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
2648aa5d6bedSChris Mason 	return ret;
2649925baeddSChris Mason out:
2650925baeddSChris Mason 	btrfs_tree_unlock(left);
2651925baeddSChris Mason 	free_extent_buffer(left);
2652925baeddSChris Mason 	return ret;
2653be0e5c09SChris Mason }
2654be0e5c09SChris Mason 
265574123bd7SChris Mason /*
265644871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
265744871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
265899d8f83cSChris Mason  *
265999d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
266099d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us push all the
266199d8f83cSChris Mason  * items
266244871b1bSChris Mason  */
266344871b1bSChris Mason static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
266499d8f83cSChris Mason 			  *root, struct btrfs_path *path, int min_data_size,
266599d8f83cSChris Mason 			  int data_size, int empty, u32 max_slot)
266644871b1bSChris Mason {
266744871b1bSChris Mason 	struct extent_buffer *right = path->nodes[0];
266844871b1bSChris Mason 	struct extent_buffer *left;
266944871b1bSChris Mason 	int slot;
267044871b1bSChris Mason 	int free_space;
267144871b1bSChris Mason 	u32 right_nritems;
267244871b1bSChris Mason 	int ret = 0;
267344871b1bSChris Mason 
267444871b1bSChris Mason 	slot = path->slots[1];
267544871b1bSChris Mason 	if (slot == 0)
267644871b1bSChris Mason 		return 1;
267744871b1bSChris Mason 	if (!path->nodes[1])
267844871b1bSChris Mason 		return 1;
267944871b1bSChris Mason 
268044871b1bSChris Mason 	right_nritems = btrfs_header_nritems(right);
268144871b1bSChris Mason 	if (right_nritems == 0)
268244871b1bSChris Mason 		return 1;
268344871b1bSChris Mason 
268444871b1bSChris Mason 	btrfs_assert_tree_locked(path->nodes[1]);
268544871b1bSChris Mason 
268644871b1bSChris Mason 	left = read_node_slot(root, path->nodes[1], slot - 1);
268791ca338dSTsutomu Itoh 	if (left == NULL)
268891ca338dSTsutomu Itoh 		return 1;
268991ca338dSTsutomu Itoh 
269044871b1bSChris Mason 	btrfs_tree_lock(left);
269144871b1bSChris Mason 	btrfs_set_lock_blocking(left);
269244871b1bSChris Mason 
269344871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, left);
269444871b1bSChris Mason 	if (free_space < data_size) {
269544871b1bSChris Mason 		ret = 1;
269644871b1bSChris Mason 		goto out;
269744871b1bSChris Mason 	}
269844871b1bSChris Mason 
269944871b1bSChris Mason 	/* cow and double check */
270044871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, left,
270144871b1bSChris Mason 			      path->nodes[1], slot - 1, &left);
270244871b1bSChris Mason 	if (ret) {
270344871b1bSChris Mason 		/* we hit -ENOSPC, but it isn't fatal here */
270444871b1bSChris Mason 		ret = 1;
270544871b1bSChris Mason 		goto out;
270644871b1bSChris Mason 	}
270744871b1bSChris Mason 
270844871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, left);
270944871b1bSChris Mason 	if (free_space < data_size) {
271044871b1bSChris Mason 		ret = 1;
271144871b1bSChris Mason 		goto out;
271244871b1bSChris Mason 	}
271344871b1bSChris Mason 
271499d8f83cSChris Mason 	return __push_leaf_left(trans, root, path, min_data_size,
271599d8f83cSChris Mason 			       empty, left, free_space, right_nritems,
271699d8f83cSChris Mason 			       max_slot);
271744871b1bSChris Mason out:
271844871b1bSChris Mason 	btrfs_tree_unlock(left);
271944871b1bSChris Mason 	free_extent_buffer(left);
272044871b1bSChris Mason 	return ret;
272144871b1bSChris Mason }
272244871b1bSChris Mason 
272344871b1bSChris Mason /*
272474123bd7SChris Mason  * split the path's leaf in two, making sure there is at least data_size
272574123bd7SChris Mason  * available for the resulting leaf level of the path.
2726aa5d6bedSChris Mason  *
2727aa5d6bedSChris Mason  * returns 0 if all went well and < 0 on failure.
272874123bd7SChris Mason  */
272944871b1bSChris Mason static noinline int copy_for_split(struct btrfs_trans_handle *trans,
2730e02119d5SChris Mason 			       struct btrfs_root *root,
273144871b1bSChris Mason 			       struct btrfs_path *path,
273244871b1bSChris Mason 			       struct extent_buffer *l,
273344871b1bSChris Mason 			       struct extent_buffer *right,
273444871b1bSChris Mason 			       int slot, int mid, int nritems)
2735be0e5c09SChris Mason {
2736be0e5c09SChris Mason 	int data_copy_size;
2737be0e5c09SChris Mason 	int rt_data_off;
2738be0e5c09SChris Mason 	int i;
2739d4dbff95SChris Mason 	int ret = 0;
2740aa5d6bedSChris Mason 	int wret;
2741d4dbff95SChris Mason 	struct btrfs_disk_key disk_key;
2742be0e5c09SChris Mason 
27435f39d397SChris Mason 	nritems = nritems - mid;
27445f39d397SChris Mason 	btrfs_set_header_nritems(right, nritems);
27455f39d397SChris Mason 	data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
27465f39d397SChris Mason 
27475f39d397SChris Mason 	copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
27485f39d397SChris Mason 			   btrfs_item_nr_offset(mid),
27495f39d397SChris Mason 			   nritems * sizeof(struct btrfs_item));
27505f39d397SChris Mason 
27515f39d397SChris Mason 	copy_extent_buffer(right, l,
2752d6025579SChris Mason 		     btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2753123abc88SChris Mason 		     data_copy_size, btrfs_leaf_data(l) +
2754123abc88SChris Mason 		     leaf_data_end(root, l), data_copy_size);
275574123bd7SChris Mason 
27565f39d397SChris Mason 	rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
27575f39d397SChris Mason 		      btrfs_item_end_nr(l, mid);
27585f39d397SChris Mason 
27595f39d397SChris Mason 	for (i = 0; i < nritems; i++) {
27605f39d397SChris Mason 		struct btrfs_item *item = btrfs_item_nr(right, i);
2761db94535dSChris Mason 		u32 ioff;
2762db94535dSChris Mason 
2763db94535dSChris Mason 		ioff = btrfs_item_offset(right, item);
27645f39d397SChris Mason 		btrfs_set_item_offset(right, item, ioff + rt_data_off);
27650783fcfcSChris Mason 	}
276674123bd7SChris Mason 
27675f39d397SChris Mason 	btrfs_set_header_nritems(l, mid);
2768aa5d6bedSChris Mason 	ret = 0;
27695f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
2770db94535dSChris Mason 	wret = insert_ptr(trans, root, path, &disk_key, right->start,
2771db94535dSChris Mason 			  path->slots[1] + 1, 1);
2772aa5d6bedSChris Mason 	if (wret)
2773aa5d6bedSChris Mason 		ret = wret;
27745f39d397SChris Mason 
27755f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
27765f39d397SChris Mason 	btrfs_mark_buffer_dirty(l);
2777eb60ceacSChris Mason 	BUG_ON(path->slots[0] != slot);
27785f39d397SChris Mason 
2779be0e5c09SChris Mason 	if (mid <= slot) {
2780925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
27815f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
27825f39d397SChris Mason 		path->nodes[0] = right;
2783be0e5c09SChris Mason 		path->slots[0] -= mid;
2784be0e5c09SChris Mason 		path->slots[1] += 1;
2785925baeddSChris Mason 	} else {
2786925baeddSChris Mason 		btrfs_tree_unlock(right);
27875f39d397SChris Mason 		free_extent_buffer(right);
2788925baeddSChris Mason 	}
27895f39d397SChris Mason 
2790eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
2791d4dbff95SChris Mason 
279244871b1bSChris Mason 	return ret;
279344871b1bSChris Mason }
279444871b1bSChris Mason 
279544871b1bSChris Mason /*
279699d8f83cSChris Mason  * double splits happen when we need to insert a big item in the middle
279799d8f83cSChris Mason  * of a leaf.  A double split can leave us with 3 mostly empty leaves:
279899d8f83cSChris Mason  * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
279999d8f83cSChris Mason  *          A                 B                 C
280099d8f83cSChris Mason  *
280199d8f83cSChris Mason  * We avoid this by trying to push the items on either side of our target
280299d8f83cSChris Mason  * into the adjacent leaves.  If all goes well we can avoid the double split
280399d8f83cSChris Mason  * completely.
280499d8f83cSChris Mason  */
280599d8f83cSChris Mason static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
280699d8f83cSChris Mason 					  struct btrfs_root *root,
280799d8f83cSChris Mason 					  struct btrfs_path *path,
280899d8f83cSChris Mason 					  int data_size)
280999d8f83cSChris Mason {
281099d8f83cSChris Mason 	int ret;
281199d8f83cSChris Mason 	int progress = 0;
281299d8f83cSChris Mason 	int slot;
281399d8f83cSChris Mason 	u32 nritems;
281499d8f83cSChris Mason 
281599d8f83cSChris Mason 	slot = path->slots[0];
281699d8f83cSChris Mason 
281799d8f83cSChris Mason 	/*
281899d8f83cSChris Mason 	 * try to push all the items after our slot into the
281999d8f83cSChris Mason 	 * right leaf
282099d8f83cSChris Mason 	 */
282199d8f83cSChris Mason 	ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
282299d8f83cSChris Mason 	if (ret < 0)
282399d8f83cSChris Mason 		return ret;
282499d8f83cSChris Mason 
282599d8f83cSChris Mason 	if (ret == 0)
282699d8f83cSChris Mason 		progress++;
282799d8f83cSChris Mason 
282899d8f83cSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
282999d8f83cSChris Mason 	/*
283099d8f83cSChris Mason 	 * our goal is to get our slot at the start or end of a leaf.  If
283199d8f83cSChris Mason 	 * we've done so we're done
283299d8f83cSChris Mason 	 */
283399d8f83cSChris Mason 	if (path->slots[0] == 0 || path->slots[0] == nritems)
283499d8f83cSChris Mason 		return 0;
283599d8f83cSChris Mason 
283699d8f83cSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
283799d8f83cSChris Mason 		return 0;
283899d8f83cSChris Mason 
283999d8f83cSChris Mason 	/* try to push all the items before our slot into the next leaf */
284099d8f83cSChris Mason 	slot = path->slots[0];
284199d8f83cSChris Mason 	ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
284299d8f83cSChris Mason 	if (ret < 0)
284399d8f83cSChris Mason 		return ret;
284499d8f83cSChris Mason 
284599d8f83cSChris Mason 	if (ret == 0)
284699d8f83cSChris Mason 		progress++;
284799d8f83cSChris Mason 
284899d8f83cSChris Mason 	if (progress)
284999d8f83cSChris Mason 		return 0;
285099d8f83cSChris Mason 	return 1;
285199d8f83cSChris Mason }
285299d8f83cSChris Mason 
285399d8f83cSChris Mason /*
285444871b1bSChris Mason  * split the path's leaf in two, making sure there is at least data_size
285544871b1bSChris Mason  * available for the resulting leaf level of the path.
285644871b1bSChris Mason  *
285744871b1bSChris Mason  * returns 0 if all went well and < 0 on failure.
285844871b1bSChris Mason  */
285944871b1bSChris Mason static noinline int split_leaf(struct btrfs_trans_handle *trans,
286044871b1bSChris Mason 			       struct btrfs_root *root,
286144871b1bSChris Mason 			       struct btrfs_key *ins_key,
286244871b1bSChris Mason 			       struct btrfs_path *path, int data_size,
286344871b1bSChris Mason 			       int extend)
286444871b1bSChris Mason {
28655d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
286644871b1bSChris Mason 	struct extent_buffer *l;
286744871b1bSChris Mason 	u32 nritems;
286844871b1bSChris Mason 	int mid;
286944871b1bSChris Mason 	int slot;
287044871b1bSChris Mason 	struct extent_buffer *right;
287144871b1bSChris Mason 	int ret = 0;
287244871b1bSChris Mason 	int wret;
28735d4f98a2SYan Zheng 	int split;
287444871b1bSChris Mason 	int num_doubles = 0;
287599d8f83cSChris Mason 	int tried_avoid_double = 0;
287644871b1bSChris Mason 
2877a5719521SYan, Zheng 	l = path->nodes[0];
2878a5719521SYan, Zheng 	slot = path->slots[0];
2879a5719521SYan, Zheng 	if (extend && data_size + btrfs_item_size_nr(l, slot) +
2880a5719521SYan, Zheng 	    sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2881a5719521SYan, Zheng 		return -EOVERFLOW;
2882a5719521SYan, Zheng 
288344871b1bSChris Mason 	/* first try to make some room by pushing left and right */
288499d8f83cSChris Mason 	if (data_size) {
288599d8f83cSChris Mason 		wret = push_leaf_right(trans, root, path, data_size,
288699d8f83cSChris Mason 				       data_size, 0, 0);
288744871b1bSChris Mason 		if (wret < 0)
288844871b1bSChris Mason 			return wret;
288944871b1bSChris Mason 		if (wret) {
289099d8f83cSChris Mason 			wret = push_leaf_left(trans, root, path, data_size,
289199d8f83cSChris Mason 					      data_size, 0, (u32)-1);
289244871b1bSChris Mason 			if (wret < 0)
289344871b1bSChris Mason 				return wret;
289444871b1bSChris Mason 		}
289544871b1bSChris Mason 		l = path->nodes[0];
289644871b1bSChris Mason 
289744871b1bSChris Mason 		/* did the pushes work? */
289844871b1bSChris Mason 		if (btrfs_leaf_free_space(root, l) >= data_size)
289944871b1bSChris Mason 			return 0;
290044871b1bSChris Mason 	}
290144871b1bSChris Mason 
290244871b1bSChris Mason 	if (!path->nodes[1]) {
290344871b1bSChris Mason 		ret = insert_new_root(trans, root, path, 1);
290444871b1bSChris Mason 		if (ret)
290544871b1bSChris Mason 			return ret;
290644871b1bSChris Mason 	}
290744871b1bSChris Mason again:
29085d4f98a2SYan Zheng 	split = 1;
290944871b1bSChris Mason 	l = path->nodes[0];
291044871b1bSChris Mason 	slot = path->slots[0];
291144871b1bSChris Mason 	nritems = btrfs_header_nritems(l);
291244871b1bSChris Mason 	mid = (nritems + 1) / 2;
291344871b1bSChris Mason 
29145d4f98a2SYan Zheng 	if (mid <= slot) {
29155d4f98a2SYan Zheng 		if (nritems == 1 ||
29165d4f98a2SYan Zheng 		    leaf_space_used(l, mid, nritems - mid) + data_size >
29175d4f98a2SYan Zheng 			BTRFS_LEAF_DATA_SIZE(root)) {
29185d4f98a2SYan Zheng 			if (slot >= nritems) {
29195d4f98a2SYan Zheng 				split = 0;
29205d4f98a2SYan Zheng 			} else {
29215d4f98a2SYan Zheng 				mid = slot;
29225d4f98a2SYan Zheng 				if (mid != nritems &&
29235d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
29245d4f98a2SYan Zheng 				    data_size > BTRFS_LEAF_DATA_SIZE(root)) {
292599d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
292699d8f83cSChris Mason 						goto push_for_double;
29275d4f98a2SYan Zheng 					split = 2;
29285d4f98a2SYan Zheng 				}
29295d4f98a2SYan Zheng 			}
29305d4f98a2SYan Zheng 		}
29315d4f98a2SYan Zheng 	} else {
29325d4f98a2SYan Zheng 		if (leaf_space_used(l, 0, mid) + data_size >
29335d4f98a2SYan Zheng 			BTRFS_LEAF_DATA_SIZE(root)) {
29345d4f98a2SYan Zheng 			if (!extend && data_size && slot == 0) {
29355d4f98a2SYan Zheng 				split = 0;
29365d4f98a2SYan Zheng 			} else if ((extend || !data_size) && slot == 0) {
29375d4f98a2SYan Zheng 				mid = 1;
29385d4f98a2SYan Zheng 			} else {
29395d4f98a2SYan Zheng 				mid = slot;
29405d4f98a2SYan Zheng 				if (mid != nritems &&
29415d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
29425d4f98a2SYan Zheng 				    data_size > BTRFS_LEAF_DATA_SIZE(root)) {
294399d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
294499d8f83cSChris Mason 						goto push_for_double;
29455d4f98a2SYan Zheng 					split = 2 ;
29465d4f98a2SYan Zheng 				}
29475d4f98a2SYan Zheng 			}
29485d4f98a2SYan Zheng 		}
29495d4f98a2SYan Zheng 	}
29505d4f98a2SYan Zheng 
29515d4f98a2SYan Zheng 	if (split == 0)
29525d4f98a2SYan Zheng 		btrfs_cpu_key_to_disk(&disk_key, ins_key);
29535d4f98a2SYan Zheng 	else
29545d4f98a2SYan Zheng 		btrfs_item_key(l, &disk_key, mid);
29555d4f98a2SYan Zheng 
29565d4f98a2SYan Zheng 	right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
295744871b1bSChris Mason 					root->root_key.objectid,
29585d4f98a2SYan Zheng 					&disk_key, 0, l->start, 0);
2959f0486c68SYan, Zheng 	if (IS_ERR(right))
296044871b1bSChris Mason 		return PTR_ERR(right);
2961f0486c68SYan, Zheng 
2962f0486c68SYan, Zheng 	root_add_used(root, root->leafsize);
296344871b1bSChris Mason 
296444871b1bSChris Mason 	memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
296544871b1bSChris Mason 	btrfs_set_header_bytenr(right, right->start);
296644871b1bSChris Mason 	btrfs_set_header_generation(right, trans->transid);
29675d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
296844871b1bSChris Mason 	btrfs_set_header_owner(right, root->root_key.objectid);
296944871b1bSChris Mason 	btrfs_set_header_level(right, 0);
297044871b1bSChris Mason 	write_extent_buffer(right, root->fs_info->fsid,
297144871b1bSChris Mason 			    (unsigned long)btrfs_header_fsid(right),
297244871b1bSChris Mason 			    BTRFS_FSID_SIZE);
297344871b1bSChris Mason 
297444871b1bSChris Mason 	write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
297544871b1bSChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(right),
297644871b1bSChris Mason 			    BTRFS_UUID_SIZE);
297744871b1bSChris Mason 
29785d4f98a2SYan Zheng 	if (split == 0) {
297944871b1bSChris Mason 		if (mid <= slot) {
298044871b1bSChris Mason 			btrfs_set_header_nritems(right, 0);
298144871b1bSChris Mason 			wret = insert_ptr(trans, root, path,
298244871b1bSChris Mason 					  &disk_key, right->start,
298344871b1bSChris Mason 					  path->slots[1] + 1, 1);
298444871b1bSChris Mason 			if (wret)
298544871b1bSChris Mason 				ret = wret;
298644871b1bSChris Mason 
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);
299444871b1bSChris Mason 			wret = insert_ptr(trans, root, path,
299544871b1bSChris Mason 					  &disk_key,
299644871b1bSChris Mason 					  right->start,
299744871b1bSChris Mason 					  path->slots[1], 1);
299844871b1bSChris Mason 			if (wret)
299944871b1bSChris Mason 				ret = wret;
300044871b1bSChris Mason 			btrfs_tree_unlock(path->nodes[0]);
300144871b1bSChris Mason 			free_extent_buffer(path->nodes[0]);
300244871b1bSChris Mason 			path->nodes[0] = right;
300344871b1bSChris Mason 			path->slots[0] = 0;
300444871b1bSChris Mason 			if (path->slots[1] == 0) {
300544871b1bSChris Mason 				wret = fixup_low_keys(trans, root,
300644871b1bSChris Mason 						path, &disk_key, 1);
300744871b1bSChris Mason 				if (wret)
300844871b1bSChris Mason 					ret = wret;
300944871b1bSChris Mason 			}
30105d4f98a2SYan Zheng 		}
301144871b1bSChris Mason 		btrfs_mark_buffer_dirty(right);
301244871b1bSChris Mason 		return ret;
301344871b1bSChris Mason 	}
301444871b1bSChris Mason 
301544871b1bSChris Mason 	ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
301644871b1bSChris Mason 	BUG_ON(ret);
301744871b1bSChris Mason 
30185d4f98a2SYan Zheng 	if (split == 2) {
3019cc0c5538SChris Mason 		BUG_ON(num_doubles != 0);
3020cc0c5538SChris Mason 		num_doubles++;
3021cc0c5538SChris Mason 		goto again;
30223326d1b0SChris Mason 	}
302344871b1bSChris Mason 
3024be0e5c09SChris Mason 	return ret;
302599d8f83cSChris Mason 
302699d8f83cSChris Mason push_for_double:
302799d8f83cSChris Mason 	push_for_double_split(trans, root, path, data_size);
302899d8f83cSChris Mason 	tried_avoid_double = 1;
302999d8f83cSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
303099d8f83cSChris Mason 		return 0;
303199d8f83cSChris Mason 	goto again;
3032be0e5c09SChris Mason }
3033be0e5c09SChris Mason 
3034ad48fd75SYan, Zheng static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3035ad48fd75SYan, Zheng 					 struct btrfs_root *root,
3036ad48fd75SYan, Zheng 					 struct btrfs_path *path, int ins_len)
3037ad48fd75SYan, Zheng {
3038ad48fd75SYan, Zheng 	struct btrfs_key key;
3039ad48fd75SYan, Zheng 	struct extent_buffer *leaf;
3040ad48fd75SYan, Zheng 	struct btrfs_file_extent_item *fi;
3041ad48fd75SYan, Zheng 	u64 extent_len = 0;
3042ad48fd75SYan, Zheng 	u32 item_size;
3043ad48fd75SYan, Zheng 	int ret;
3044ad48fd75SYan, Zheng 
3045ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3046ad48fd75SYan, Zheng 	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3047ad48fd75SYan, Zheng 
3048ad48fd75SYan, Zheng 	BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3049ad48fd75SYan, Zheng 	       key.type != BTRFS_EXTENT_CSUM_KEY);
3050ad48fd75SYan, Zheng 
3051ad48fd75SYan, Zheng 	if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3052ad48fd75SYan, Zheng 		return 0;
3053ad48fd75SYan, Zheng 
3054ad48fd75SYan, Zheng 	item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3055ad48fd75SYan, Zheng 	if (key.type == BTRFS_EXTENT_DATA_KEY) {
3056ad48fd75SYan, Zheng 		fi = btrfs_item_ptr(leaf, path->slots[0],
3057ad48fd75SYan, Zheng 				    struct btrfs_file_extent_item);
3058ad48fd75SYan, Zheng 		extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3059ad48fd75SYan, Zheng 	}
3060b3b4aa74SDavid Sterba 	btrfs_release_path(path);
3061ad48fd75SYan, Zheng 
3062ad48fd75SYan, Zheng 	path->keep_locks = 1;
3063ad48fd75SYan, Zheng 	path->search_for_split = 1;
3064ad48fd75SYan, Zheng 	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3065ad48fd75SYan, Zheng 	path->search_for_split = 0;
3066ad48fd75SYan, Zheng 	if (ret < 0)
3067ad48fd75SYan, Zheng 		goto err;
3068ad48fd75SYan, Zheng 
3069ad48fd75SYan, Zheng 	ret = -EAGAIN;
3070ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3071ad48fd75SYan, Zheng 	/* if our item isn't there or got smaller, return now */
3072ad48fd75SYan, Zheng 	if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3073ad48fd75SYan, Zheng 		goto err;
3074ad48fd75SYan, Zheng 
3075109f6aefSChris Mason 	/* the leaf has  changed, it now has room.  return now */
3076109f6aefSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3077109f6aefSChris Mason 		goto err;
3078109f6aefSChris Mason 
3079ad48fd75SYan, Zheng 	if (key.type == BTRFS_EXTENT_DATA_KEY) {
3080ad48fd75SYan, Zheng 		fi = btrfs_item_ptr(leaf, path->slots[0],
3081ad48fd75SYan, Zheng 				    struct btrfs_file_extent_item);
3082ad48fd75SYan, Zheng 		if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3083ad48fd75SYan, Zheng 			goto err;
3084ad48fd75SYan, Zheng 	}
3085ad48fd75SYan, Zheng 
3086ad48fd75SYan, Zheng 	btrfs_set_path_blocking(path);
3087ad48fd75SYan, Zheng 	ret = split_leaf(trans, root, &key, path, ins_len, 1);
3088f0486c68SYan, Zheng 	if (ret)
3089f0486c68SYan, Zheng 		goto err;
3090ad48fd75SYan, Zheng 
3091ad48fd75SYan, Zheng 	path->keep_locks = 0;
3092ad48fd75SYan, Zheng 	btrfs_unlock_up_safe(path, 1);
3093ad48fd75SYan, Zheng 	return 0;
3094ad48fd75SYan, Zheng err:
3095ad48fd75SYan, Zheng 	path->keep_locks = 0;
3096ad48fd75SYan, Zheng 	return ret;
3097ad48fd75SYan, Zheng }
3098ad48fd75SYan, Zheng 
3099ad48fd75SYan, Zheng static noinline int split_item(struct btrfs_trans_handle *trans,
3100459931ecSChris Mason 			       struct btrfs_root *root,
3101459931ecSChris Mason 			       struct btrfs_path *path,
3102459931ecSChris Mason 			       struct btrfs_key *new_key,
3103459931ecSChris Mason 			       unsigned long split_offset)
3104459931ecSChris Mason {
3105459931ecSChris Mason 	struct extent_buffer *leaf;
3106459931ecSChris Mason 	struct btrfs_item *item;
3107459931ecSChris Mason 	struct btrfs_item *new_item;
3108459931ecSChris Mason 	int slot;
3109ad48fd75SYan, Zheng 	char *buf;
3110459931ecSChris Mason 	u32 nritems;
3111ad48fd75SYan, Zheng 	u32 item_size;
3112459931ecSChris Mason 	u32 orig_offset;
3113459931ecSChris Mason 	struct btrfs_disk_key disk_key;
3114459931ecSChris Mason 
3115459931ecSChris Mason 	leaf = path->nodes[0];
3116b9473439SChris Mason 	BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3117b9473439SChris Mason 
3118b4ce94deSChris Mason 	btrfs_set_path_blocking(path);
3119b4ce94deSChris Mason 
3120459931ecSChris Mason 	item = btrfs_item_nr(leaf, path->slots[0]);
3121459931ecSChris Mason 	orig_offset = btrfs_item_offset(leaf, item);
3122459931ecSChris Mason 	item_size = btrfs_item_size(leaf, item);
3123459931ecSChris Mason 
3124459931ecSChris Mason 	buf = kmalloc(item_size, GFP_NOFS);
3125ad48fd75SYan, Zheng 	if (!buf)
3126ad48fd75SYan, Zheng 		return -ENOMEM;
3127ad48fd75SYan, Zheng 
3128459931ecSChris Mason 	read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3129459931ecSChris Mason 			    path->slots[0]), item_size);
3130ad48fd75SYan, Zheng 
3131459931ecSChris Mason 	slot = path->slots[0] + 1;
3132459931ecSChris Mason 	nritems = btrfs_header_nritems(leaf);
3133459931ecSChris Mason 	if (slot != nritems) {
3134459931ecSChris Mason 		/* shift the items */
3135459931ecSChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3136459931ecSChris Mason 				btrfs_item_nr_offset(slot),
3137459931ecSChris Mason 				(nritems - slot) * sizeof(struct btrfs_item));
3138459931ecSChris Mason 	}
3139459931ecSChris Mason 
3140459931ecSChris Mason 	btrfs_cpu_key_to_disk(&disk_key, new_key);
3141459931ecSChris Mason 	btrfs_set_item_key(leaf, &disk_key, slot);
3142459931ecSChris Mason 
3143459931ecSChris Mason 	new_item = btrfs_item_nr(leaf, slot);
3144459931ecSChris Mason 
3145459931ecSChris Mason 	btrfs_set_item_offset(leaf, new_item, orig_offset);
3146459931ecSChris Mason 	btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3147459931ecSChris Mason 
3148459931ecSChris Mason 	btrfs_set_item_offset(leaf, item,
3149459931ecSChris Mason 			      orig_offset + item_size - split_offset);
3150459931ecSChris Mason 	btrfs_set_item_size(leaf, item, split_offset);
3151459931ecSChris Mason 
3152459931ecSChris Mason 	btrfs_set_header_nritems(leaf, nritems + 1);
3153459931ecSChris Mason 
3154459931ecSChris Mason 	/* write the data for the start of the original item */
3155459931ecSChris Mason 	write_extent_buffer(leaf, buf,
3156459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, path->slots[0]),
3157459931ecSChris Mason 			    split_offset);
3158459931ecSChris Mason 
3159459931ecSChris Mason 	/* write the data for the new item */
3160459931ecSChris Mason 	write_extent_buffer(leaf, buf + split_offset,
3161459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, slot),
3162459931ecSChris Mason 			    item_size - split_offset);
3163459931ecSChris Mason 	btrfs_mark_buffer_dirty(leaf);
3164459931ecSChris Mason 
3165ad48fd75SYan, Zheng 	BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
3166459931ecSChris Mason 	kfree(buf);
3167ad48fd75SYan, Zheng 	return 0;
3168ad48fd75SYan, Zheng }
3169ad48fd75SYan, Zheng 
3170ad48fd75SYan, Zheng /*
3171ad48fd75SYan, Zheng  * This function splits a single item into two items,
3172ad48fd75SYan, Zheng  * giving 'new_key' to the new item and splitting the
3173ad48fd75SYan, Zheng  * old one at split_offset (from the start of the item).
3174ad48fd75SYan, Zheng  *
3175ad48fd75SYan, Zheng  * The path may be released by this operation.  After
3176ad48fd75SYan, Zheng  * the split, the path is pointing to the old item.  The
3177ad48fd75SYan, Zheng  * new item is going to be in the same node as the old one.
3178ad48fd75SYan, Zheng  *
3179ad48fd75SYan, Zheng  * Note, the item being split must be smaller enough to live alone on
3180ad48fd75SYan, Zheng  * a tree block with room for one extra struct btrfs_item
3181ad48fd75SYan, Zheng  *
3182ad48fd75SYan, Zheng  * This allows us to split the item in place, keeping a lock on the
3183ad48fd75SYan, Zheng  * leaf the entire time.
3184ad48fd75SYan, Zheng  */
3185ad48fd75SYan, Zheng int btrfs_split_item(struct btrfs_trans_handle *trans,
3186ad48fd75SYan, Zheng 		     struct btrfs_root *root,
3187ad48fd75SYan, Zheng 		     struct btrfs_path *path,
3188ad48fd75SYan, Zheng 		     struct btrfs_key *new_key,
3189ad48fd75SYan, Zheng 		     unsigned long split_offset)
3190ad48fd75SYan, Zheng {
3191ad48fd75SYan, Zheng 	int ret;
3192ad48fd75SYan, Zheng 	ret = setup_leaf_for_split(trans, root, path,
3193ad48fd75SYan, Zheng 				   sizeof(struct btrfs_item));
3194ad48fd75SYan, Zheng 	if (ret)
3195459931ecSChris Mason 		return ret;
3196ad48fd75SYan, Zheng 
3197ad48fd75SYan, Zheng 	ret = split_item(trans, root, path, new_key, split_offset);
3198ad48fd75SYan, Zheng 	return ret;
3199ad48fd75SYan, Zheng }
3200ad48fd75SYan, Zheng 
3201ad48fd75SYan, Zheng /*
3202ad48fd75SYan, Zheng  * This function duplicate a item, giving 'new_key' to the new item.
3203ad48fd75SYan, Zheng  * It guarantees both items live in the same tree leaf and the new item
3204ad48fd75SYan, Zheng  * is contiguous with the original item.
3205ad48fd75SYan, Zheng  *
3206ad48fd75SYan, Zheng  * This allows us to split file extent in place, keeping a lock on the
3207ad48fd75SYan, Zheng  * leaf the entire time.
3208ad48fd75SYan, Zheng  */
3209ad48fd75SYan, Zheng int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3210ad48fd75SYan, Zheng 			 struct btrfs_root *root,
3211ad48fd75SYan, Zheng 			 struct btrfs_path *path,
3212ad48fd75SYan, Zheng 			 struct btrfs_key *new_key)
3213ad48fd75SYan, Zheng {
3214ad48fd75SYan, Zheng 	struct extent_buffer *leaf;
3215ad48fd75SYan, Zheng 	int ret;
3216ad48fd75SYan, Zheng 	u32 item_size;
3217ad48fd75SYan, Zheng 
3218ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3219ad48fd75SYan, Zheng 	item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3220ad48fd75SYan, Zheng 	ret = setup_leaf_for_split(trans, root, path,
3221ad48fd75SYan, Zheng 				   item_size + sizeof(struct btrfs_item));
3222ad48fd75SYan, Zheng 	if (ret)
3223ad48fd75SYan, Zheng 		return ret;
3224ad48fd75SYan, Zheng 
3225ad48fd75SYan, Zheng 	path->slots[0]++;
3226ad48fd75SYan, Zheng 	ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3227ad48fd75SYan, Zheng 				     item_size, item_size +
3228ad48fd75SYan, Zheng 				     sizeof(struct btrfs_item), 1);
3229ad48fd75SYan, Zheng 	BUG_ON(ret);
3230ad48fd75SYan, Zheng 
3231ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3232ad48fd75SYan, Zheng 	memcpy_extent_buffer(leaf,
3233ad48fd75SYan, Zheng 			     btrfs_item_ptr_offset(leaf, path->slots[0]),
3234ad48fd75SYan, Zheng 			     btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3235ad48fd75SYan, Zheng 			     item_size);
3236ad48fd75SYan, Zheng 	return 0;
3237459931ecSChris Mason }
3238459931ecSChris Mason 
3239459931ecSChris Mason /*
3240d352ac68SChris Mason  * make the item pointed to by the path smaller.  new_size indicates
3241d352ac68SChris Mason  * how small to make it, and from_end tells us if we just chop bytes
3242d352ac68SChris Mason  * off the end of the item or if we shift the item to chop bytes off
3243d352ac68SChris Mason  * the front.
3244d352ac68SChris Mason  */
3245b18c6685SChris Mason int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3246b18c6685SChris Mason 			struct btrfs_root *root,
3247b18c6685SChris Mason 			struct btrfs_path *path,
3248179e29e4SChris Mason 			u32 new_size, int from_end)
3249b18c6685SChris Mason {
3250b18c6685SChris Mason 	int slot;
32515f39d397SChris Mason 	struct extent_buffer *leaf;
32525f39d397SChris Mason 	struct btrfs_item *item;
3253b18c6685SChris Mason 	u32 nritems;
3254b18c6685SChris Mason 	unsigned int data_end;
3255b18c6685SChris Mason 	unsigned int old_data_start;
3256b18c6685SChris Mason 	unsigned int old_size;
3257b18c6685SChris Mason 	unsigned int size_diff;
3258b18c6685SChris Mason 	int i;
3259b18c6685SChris Mason 
32605f39d397SChris Mason 	leaf = path->nodes[0];
3261179e29e4SChris Mason 	slot = path->slots[0];
3262179e29e4SChris Mason 
3263179e29e4SChris Mason 	old_size = btrfs_item_size_nr(leaf, slot);
3264179e29e4SChris Mason 	if (old_size == new_size)
3265179e29e4SChris Mason 		return 0;
3266b18c6685SChris Mason 
32675f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3268b18c6685SChris Mason 	data_end = leaf_data_end(root, leaf);
3269b18c6685SChris Mason 
32705f39d397SChris Mason 	old_data_start = btrfs_item_offset_nr(leaf, slot);
3271179e29e4SChris Mason 
3272b18c6685SChris Mason 	size_diff = old_size - new_size;
3273b18c6685SChris Mason 
3274b18c6685SChris Mason 	BUG_ON(slot < 0);
3275b18c6685SChris Mason 	BUG_ON(slot >= nritems);
3276b18c6685SChris Mason 
3277b18c6685SChris Mason 	/*
3278b18c6685SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3279b18c6685SChris Mason 	 */
3280b18c6685SChris Mason 	/* first correct the data pointers */
3281b18c6685SChris Mason 	for (i = slot; i < nritems; i++) {
32825f39d397SChris Mason 		u32 ioff;
32835f39d397SChris Mason 		item = btrfs_item_nr(leaf, i);
3284db94535dSChris Mason 
32855f39d397SChris Mason 		ioff = btrfs_item_offset(leaf, item);
32865f39d397SChris Mason 		btrfs_set_item_offset(leaf, item, ioff + size_diff);
3287b18c6685SChris Mason 	}
3288db94535dSChris Mason 
3289b18c6685SChris Mason 	/* shift the data */
3290179e29e4SChris Mason 	if (from_end) {
32915f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3292b18c6685SChris Mason 			      data_end + size_diff, btrfs_leaf_data(leaf) +
3293b18c6685SChris Mason 			      data_end, old_data_start + new_size - data_end);
3294179e29e4SChris Mason 	} else {
3295179e29e4SChris Mason 		struct btrfs_disk_key disk_key;
3296179e29e4SChris Mason 		u64 offset;
3297179e29e4SChris Mason 
3298179e29e4SChris Mason 		btrfs_item_key(leaf, &disk_key, slot);
3299179e29e4SChris Mason 
3300179e29e4SChris Mason 		if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3301179e29e4SChris Mason 			unsigned long ptr;
3302179e29e4SChris Mason 			struct btrfs_file_extent_item *fi;
3303179e29e4SChris Mason 
3304179e29e4SChris Mason 			fi = btrfs_item_ptr(leaf, slot,
3305179e29e4SChris Mason 					    struct btrfs_file_extent_item);
3306179e29e4SChris Mason 			fi = (struct btrfs_file_extent_item *)(
3307179e29e4SChris Mason 			     (unsigned long)fi - size_diff);
3308179e29e4SChris Mason 
3309179e29e4SChris Mason 			if (btrfs_file_extent_type(leaf, fi) ==
3310179e29e4SChris Mason 			    BTRFS_FILE_EXTENT_INLINE) {
3311179e29e4SChris Mason 				ptr = btrfs_item_ptr_offset(leaf, slot);
3312179e29e4SChris Mason 				memmove_extent_buffer(leaf, ptr,
3313179e29e4SChris Mason 				      (unsigned long)fi,
3314179e29e4SChris Mason 				      offsetof(struct btrfs_file_extent_item,
3315179e29e4SChris Mason 						 disk_bytenr));
3316179e29e4SChris Mason 			}
3317179e29e4SChris Mason 		}
3318179e29e4SChris Mason 
3319179e29e4SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3320179e29e4SChris Mason 			      data_end + size_diff, btrfs_leaf_data(leaf) +
3321179e29e4SChris Mason 			      data_end, old_data_start - data_end);
3322179e29e4SChris Mason 
3323179e29e4SChris Mason 		offset = btrfs_disk_key_offset(&disk_key);
3324179e29e4SChris Mason 		btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3325179e29e4SChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot);
3326179e29e4SChris Mason 		if (slot == 0)
3327179e29e4SChris Mason 			fixup_low_keys(trans, root, path, &disk_key, 1);
3328179e29e4SChris Mason 	}
33295f39d397SChris Mason 
33305f39d397SChris Mason 	item = btrfs_item_nr(leaf, slot);
33315f39d397SChris Mason 	btrfs_set_item_size(leaf, item, new_size);
33325f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
3333b18c6685SChris Mason 
33345f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
33355f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
3336b18c6685SChris Mason 		BUG();
33375f39d397SChris Mason 	}
33381cd30799STsutomu Itoh 	return 0;
3339b18c6685SChris Mason }
3340b18c6685SChris Mason 
3341d352ac68SChris Mason /*
3342d352ac68SChris Mason  * make the item pointed to by the path bigger, data_size is the new size.
3343d352ac68SChris Mason  */
33445f39d397SChris Mason int btrfs_extend_item(struct btrfs_trans_handle *trans,
33455f39d397SChris Mason 		      struct btrfs_root *root, struct btrfs_path *path,
33465f39d397SChris Mason 		      u32 data_size)
33476567e837SChris Mason {
33486567e837SChris Mason 	int slot;
33495f39d397SChris Mason 	struct extent_buffer *leaf;
33505f39d397SChris Mason 	struct btrfs_item *item;
33516567e837SChris Mason 	u32 nritems;
33526567e837SChris Mason 	unsigned int data_end;
33536567e837SChris Mason 	unsigned int old_data;
33546567e837SChris Mason 	unsigned int old_size;
33556567e837SChris Mason 	int i;
33566567e837SChris Mason 
33575f39d397SChris Mason 	leaf = path->nodes[0];
33586567e837SChris Mason 
33595f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
33606567e837SChris Mason 	data_end = leaf_data_end(root, leaf);
33616567e837SChris Mason 
33625f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < data_size) {
33635f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
33646567e837SChris Mason 		BUG();
33655f39d397SChris Mason 	}
33666567e837SChris Mason 	slot = path->slots[0];
33675f39d397SChris Mason 	old_data = btrfs_item_end_nr(leaf, slot);
33686567e837SChris Mason 
33696567e837SChris Mason 	BUG_ON(slot < 0);
33703326d1b0SChris Mason 	if (slot >= nritems) {
33713326d1b0SChris Mason 		btrfs_print_leaf(root, leaf);
3372d397712bSChris Mason 		printk(KERN_CRIT "slot %d too large, nritems %d\n",
3373d397712bSChris Mason 		       slot, nritems);
33743326d1b0SChris Mason 		BUG_ON(1);
33753326d1b0SChris Mason 	}
33766567e837SChris Mason 
33776567e837SChris Mason 	/*
33786567e837SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
33796567e837SChris Mason 	 */
33806567e837SChris Mason 	/* first correct the data pointers */
33816567e837SChris Mason 	for (i = slot; i < nritems; i++) {
33825f39d397SChris Mason 		u32 ioff;
33835f39d397SChris Mason 		item = btrfs_item_nr(leaf, i);
3384db94535dSChris Mason 
33855f39d397SChris Mason 		ioff = btrfs_item_offset(leaf, item);
33865f39d397SChris Mason 		btrfs_set_item_offset(leaf, item, ioff - data_size);
33876567e837SChris Mason 	}
33885f39d397SChris Mason 
33896567e837SChris Mason 	/* shift the data */
33905f39d397SChris Mason 	memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
33916567e837SChris Mason 		      data_end - data_size, btrfs_leaf_data(leaf) +
33926567e837SChris Mason 		      data_end, old_data - data_end);
33935f39d397SChris Mason 
33946567e837SChris Mason 	data_end = old_data;
33955f39d397SChris Mason 	old_size = btrfs_item_size_nr(leaf, slot);
33965f39d397SChris Mason 	item = btrfs_item_nr(leaf, slot);
33975f39d397SChris Mason 	btrfs_set_item_size(leaf, item, old_size + data_size);
33985f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
33996567e837SChris Mason 
34005f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
34015f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
34026567e837SChris Mason 		BUG();
34035f39d397SChris Mason 	}
34041cd30799STsutomu Itoh 	return 0;
34056567e837SChris Mason }
34066567e837SChris Mason 
340774123bd7SChris Mason /*
3408d352ac68SChris Mason  * Given a key and some data, insert items into the tree.
340974123bd7SChris Mason  * This does all the path init required, making room in the tree if needed.
3410f3465ca4SJosef Bacik  * Returns the number of keys that were inserted.
3411f3465ca4SJosef Bacik  */
3412f3465ca4SJosef Bacik int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3413f3465ca4SJosef Bacik 			    struct btrfs_root *root,
3414f3465ca4SJosef Bacik 			    struct btrfs_path *path,
3415f3465ca4SJosef Bacik 			    struct btrfs_key *cpu_key, u32 *data_size,
3416f3465ca4SJosef Bacik 			    int nr)
3417f3465ca4SJosef Bacik {
3418f3465ca4SJosef Bacik 	struct extent_buffer *leaf;
3419f3465ca4SJosef Bacik 	struct btrfs_item *item;
3420f3465ca4SJosef Bacik 	int ret = 0;
3421f3465ca4SJosef Bacik 	int slot;
3422f3465ca4SJosef Bacik 	int i;
3423f3465ca4SJosef Bacik 	u32 nritems;
3424f3465ca4SJosef Bacik 	u32 total_data = 0;
3425f3465ca4SJosef Bacik 	u32 total_size = 0;
3426f3465ca4SJosef Bacik 	unsigned int data_end;
3427f3465ca4SJosef Bacik 	struct btrfs_disk_key disk_key;
3428f3465ca4SJosef Bacik 	struct btrfs_key found_key;
3429f3465ca4SJosef Bacik 
343087b29b20SYan Zheng 	for (i = 0; i < nr; i++) {
343187b29b20SYan Zheng 		if (total_size + data_size[i] + sizeof(struct btrfs_item) >
343287b29b20SYan Zheng 		    BTRFS_LEAF_DATA_SIZE(root)) {
343387b29b20SYan Zheng 			break;
343487b29b20SYan Zheng 			nr = i;
343587b29b20SYan Zheng 		}
3436f3465ca4SJosef Bacik 		total_data += data_size[i];
343787b29b20SYan Zheng 		total_size += data_size[i] + sizeof(struct btrfs_item);
343887b29b20SYan Zheng 	}
343987b29b20SYan Zheng 	BUG_ON(nr == 0);
3440f3465ca4SJosef Bacik 
3441f3465ca4SJosef Bacik 	ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3442f3465ca4SJosef Bacik 	if (ret == 0)
3443f3465ca4SJosef Bacik 		return -EEXIST;
3444f3465ca4SJosef Bacik 	if (ret < 0)
3445f3465ca4SJosef Bacik 		goto out;
3446f3465ca4SJosef Bacik 
3447f3465ca4SJosef Bacik 	leaf = path->nodes[0];
3448f3465ca4SJosef Bacik 
3449f3465ca4SJosef Bacik 	nritems = btrfs_header_nritems(leaf);
3450f3465ca4SJosef Bacik 	data_end = leaf_data_end(root, leaf);
3451f3465ca4SJosef Bacik 
3452f3465ca4SJosef Bacik 	if (btrfs_leaf_free_space(root, leaf) < total_size) {
3453f3465ca4SJosef Bacik 		for (i = nr; i >= 0; i--) {
3454f3465ca4SJosef Bacik 			total_data -= data_size[i];
3455f3465ca4SJosef Bacik 			total_size -= data_size[i] + sizeof(struct btrfs_item);
3456f3465ca4SJosef Bacik 			if (total_size < btrfs_leaf_free_space(root, leaf))
3457f3465ca4SJosef Bacik 				break;
3458f3465ca4SJosef Bacik 		}
3459f3465ca4SJosef Bacik 		nr = i;
3460f3465ca4SJosef Bacik 	}
3461f3465ca4SJosef Bacik 
3462f3465ca4SJosef Bacik 	slot = path->slots[0];
3463f3465ca4SJosef Bacik 	BUG_ON(slot < 0);
3464f3465ca4SJosef Bacik 
3465f3465ca4SJosef Bacik 	if (slot != nritems) {
3466f3465ca4SJosef Bacik 		unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3467f3465ca4SJosef Bacik 
3468f3465ca4SJosef Bacik 		item = btrfs_item_nr(leaf, slot);
3469f3465ca4SJosef Bacik 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
3470f3465ca4SJosef Bacik 
3471f3465ca4SJosef Bacik 		/* figure out how many keys we can insert in here */
3472f3465ca4SJosef Bacik 		total_data = data_size[0];
3473f3465ca4SJosef Bacik 		for (i = 1; i < nr; i++) {
34745d4f98a2SYan Zheng 			if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3475f3465ca4SJosef Bacik 				break;
3476f3465ca4SJosef Bacik 			total_data += data_size[i];
3477f3465ca4SJosef Bacik 		}
3478f3465ca4SJosef Bacik 		nr = i;
3479f3465ca4SJosef Bacik 
3480f3465ca4SJosef Bacik 		if (old_data < data_end) {
3481f3465ca4SJosef Bacik 			btrfs_print_leaf(root, leaf);
3482d397712bSChris Mason 			printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3483f3465ca4SJosef Bacik 			       slot, old_data, data_end);
3484f3465ca4SJosef Bacik 			BUG_ON(1);
3485f3465ca4SJosef Bacik 		}
3486f3465ca4SJosef Bacik 		/*
3487f3465ca4SJosef Bacik 		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3488f3465ca4SJosef Bacik 		 */
3489f3465ca4SJosef Bacik 		/* first correct the data pointers */
3490f3465ca4SJosef Bacik 		for (i = slot; i < nritems; i++) {
3491f3465ca4SJosef Bacik 			u32 ioff;
3492f3465ca4SJosef Bacik 
3493f3465ca4SJosef Bacik 			item = btrfs_item_nr(leaf, i);
3494f3465ca4SJosef Bacik 			ioff = btrfs_item_offset(leaf, item);
3495f3465ca4SJosef Bacik 			btrfs_set_item_offset(leaf, item, ioff - total_data);
3496f3465ca4SJosef Bacik 		}
3497f3465ca4SJosef Bacik 		/* shift the items */
3498f3465ca4SJosef Bacik 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3499f3465ca4SJosef Bacik 			      btrfs_item_nr_offset(slot),
3500f3465ca4SJosef Bacik 			      (nritems - slot) * sizeof(struct btrfs_item));
3501f3465ca4SJosef Bacik 
3502f3465ca4SJosef Bacik 		/* shift the data */
3503f3465ca4SJosef Bacik 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3504f3465ca4SJosef Bacik 			      data_end - total_data, btrfs_leaf_data(leaf) +
3505f3465ca4SJosef Bacik 			      data_end, old_data - data_end);
3506f3465ca4SJosef Bacik 		data_end = old_data;
3507f3465ca4SJosef Bacik 	} else {
3508f3465ca4SJosef Bacik 		/*
3509f3465ca4SJosef Bacik 		 * this sucks but it has to be done, if we are inserting at
3510f3465ca4SJosef Bacik 		 * the end of the leaf only insert 1 of the items, since we
3511f3465ca4SJosef Bacik 		 * have no way of knowing whats on the next leaf and we'd have
3512f3465ca4SJosef Bacik 		 * to drop our current locks to figure it out
3513f3465ca4SJosef Bacik 		 */
3514f3465ca4SJosef Bacik 		nr = 1;
3515f3465ca4SJosef Bacik 	}
3516f3465ca4SJosef Bacik 
3517f3465ca4SJosef Bacik 	/* setup the item for the new data */
3518f3465ca4SJosef Bacik 	for (i = 0; i < nr; i++) {
3519f3465ca4SJosef Bacik 		btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3520f3465ca4SJosef Bacik 		btrfs_set_item_key(leaf, &disk_key, slot + i);
3521f3465ca4SJosef Bacik 		item = btrfs_item_nr(leaf, slot + i);
3522f3465ca4SJosef Bacik 		btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3523f3465ca4SJosef Bacik 		data_end -= data_size[i];
3524f3465ca4SJosef Bacik 		btrfs_set_item_size(leaf, item, data_size[i]);
3525f3465ca4SJosef Bacik 	}
3526f3465ca4SJosef Bacik 	btrfs_set_header_nritems(leaf, nritems + nr);
3527f3465ca4SJosef Bacik 	btrfs_mark_buffer_dirty(leaf);
3528f3465ca4SJosef Bacik 
3529f3465ca4SJosef Bacik 	ret = 0;
3530f3465ca4SJosef Bacik 	if (slot == 0) {
3531f3465ca4SJosef Bacik 		btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3532f3465ca4SJosef Bacik 		ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3533f3465ca4SJosef Bacik 	}
3534f3465ca4SJosef Bacik 
3535f3465ca4SJosef Bacik 	if (btrfs_leaf_free_space(root, leaf) < 0) {
3536f3465ca4SJosef Bacik 		btrfs_print_leaf(root, leaf);
3537f3465ca4SJosef Bacik 		BUG();
3538f3465ca4SJosef Bacik 	}
3539f3465ca4SJosef Bacik out:
3540f3465ca4SJosef Bacik 	if (!ret)
3541f3465ca4SJosef Bacik 		ret = nr;
3542f3465ca4SJosef Bacik 	return ret;
3543f3465ca4SJosef Bacik }
3544f3465ca4SJosef Bacik 
3545f3465ca4SJosef Bacik /*
354644871b1bSChris Mason  * this is a helper for btrfs_insert_empty_items, the main goal here is
354744871b1bSChris Mason  * to save stack depth by doing the bulk of the work in a function
354844871b1bSChris Mason  * that doesn't call btrfs_search_slot
354974123bd7SChris Mason  */
355016cdcec7SMiao Xie int setup_items_for_insert(struct btrfs_trans_handle *trans,
355144871b1bSChris Mason 			   struct btrfs_root *root, struct btrfs_path *path,
35529c58309dSChris Mason 			   struct btrfs_key *cpu_key, u32 *data_size,
355344871b1bSChris Mason 			   u32 total_data, u32 total_size, int nr)
3554be0e5c09SChris Mason {
35555f39d397SChris Mason 	struct btrfs_item *item;
35569c58309dSChris Mason 	int i;
35577518a238SChris Mason 	u32 nritems;
3558be0e5c09SChris Mason 	unsigned int data_end;
3559e2fa7227SChris Mason 	struct btrfs_disk_key disk_key;
356044871b1bSChris Mason 	int ret;
356144871b1bSChris Mason 	struct extent_buffer *leaf;
356244871b1bSChris Mason 	int slot;
3563e2fa7227SChris Mason 
35645f39d397SChris Mason 	leaf = path->nodes[0];
356544871b1bSChris Mason 	slot = path->slots[0];
356674123bd7SChris Mason 
35675f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3568123abc88SChris Mason 	data_end = leaf_data_end(root, leaf);
3569eb60ceacSChris Mason 
3570f25956ccSChris Mason 	if (btrfs_leaf_free_space(root, leaf) < total_size) {
35713326d1b0SChris Mason 		btrfs_print_leaf(root, leaf);
3572d397712bSChris Mason 		printk(KERN_CRIT "not enough freespace need %u have %d\n",
35739c58309dSChris Mason 		       total_size, btrfs_leaf_free_space(root, leaf));
3574be0e5c09SChris Mason 		BUG();
3575d4dbff95SChris Mason 	}
35765f39d397SChris Mason 
3577be0e5c09SChris Mason 	if (slot != nritems) {
35785f39d397SChris Mason 		unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3579be0e5c09SChris Mason 
35805f39d397SChris Mason 		if (old_data < data_end) {
35815f39d397SChris Mason 			btrfs_print_leaf(root, leaf);
3582d397712bSChris Mason 			printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
35835f39d397SChris Mason 			       slot, old_data, data_end);
35845f39d397SChris Mason 			BUG_ON(1);
35855f39d397SChris Mason 		}
3586be0e5c09SChris Mason 		/*
3587be0e5c09SChris Mason 		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3588be0e5c09SChris Mason 		 */
3589be0e5c09SChris Mason 		/* first correct the data pointers */
35900783fcfcSChris Mason 		for (i = slot; i < nritems; i++) {
35915f39d397SChris Mason 			u32 ioff;
3592db94535dSChris Mason 
35935f39d397SChris Mason 			item = btrfs_item_nr(leaf, i);
35945f39d397SChris Mason 			ioff = btrfs_item_offset(leaf, item);
35959c58309dSChris Mason 			btrfs_set_item_offset(leaf, item, ioff - total_data);
35960783fcfcSChris Mason 		}
3597be0e5c09SChris Mason 		/* shift the items */
35989c58309dSChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
35995f39d397SChris Mason 			      btrfs_item_nr_offset(slot),
36000783fcfcSChris Mason 			      (nritems - slot) * sizeof(struct btrfs_item));
3601be0e5c09SChris Mason 
3602be0e5c09SChris Mason 		/* shift the data */
36035f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
36049c58309dSChris Mason 			      data_end - total_data, btrfs_leaf_data(leaf) +
3605be0e5c09SChris Mason 			      data_end, old_data - data_end);
3606be0e5c09SChris Mason 		data_end = old_data;
3607be0e5c09SChris Mason 	}
36085f39d397SChris Mason 
360962e2749eSChris Mason 	/* setup the item for the new data */
36109c58309dSChris Mason 	for (i = 0; i < nr; i++) {
36119c58309dSChris Mason 		btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
36129c58309dSChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot + i);
36139c58309dSChris Mason 		item = btrfs_item_nr(leaf, slot + i);
36149c58309dSChris Mason 		btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
36159c58309dSChris Mason 		data_end -= data_size[i];
36169c58309dSChris Mason 		btrfs_set_item_size(leaf, item, data_size[i]);
36179c58309dSChris Mason 	}
361844871b1bSChris Mason 
36199c58309dSChris Mason 	btrfs_set_header_nritems(leaf, nritems + nr);
3620aa5d6bedSChris Mason 
3621aa5d6bedSChris Mason 	ret = 0;
36225a01a2e3SChris Mason 	if (slot == 0) {
36235a01a2e3SChris Mason 		btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3624e089f05cSChris Mason 		ret = fixup_low_keys(trans, root, path, &disk_key, 1);
36255a01a2e3SChris Mason 	}
3626b9473439SChris Mason 	btrfs_unlock_up_safe(path, 1);
3627b9473439SChris Mason 	btrfs_mark_buffer_dirty(leaf);
3628aa5d6bedSChris Mason 
36295f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
36305f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
3631be0e5c09SChris Mason 		BUG();
36325f39d397SChris Mason 	}
363344871b1bSChris Mason 	return ret;
363444871b1bSChris Mason }
363544871b1bSChris Mason 
363644871b1bSChris Mason /*
363744871b1bSChris Mason  * Given a key and some data, insert items into the tree.
363844871b1bSChris Mason  * This does all the path init required, making room in the tree if needed.
363944871b1bSChris Mason  */
364044871b1bSChris Mason int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
364144871b1bSChris Mason 			    struct btrfs_root *root,
364244871b1bSChris Mason 			    struct btrfs_path *path,
364344871b1bSChris Mason 			    struct btrfs_key *cpu_key, u32 *data_size,
364444871b1bSChris Mason 			    int nr)
364544871b1bSChris Mason {
364644871b1bSChris Mason 	int ret = 0;
364744871b1bSChris Mason 	int slot;
364844871b1bSChris Mason 	int i;
364944871b1bSChris Mason 	u32 total_size = 0;
365044871b1bSChris Mason 	u32 total_data = 0;
365144871b1bSChris Mason 
365244871b1bSChris Mason 	for (i = 0; i < nr; i++)
365344871b1bSChris Mason 		total_data += data_size[i];
365444871b1bSChris Mason 
365544871b1bSChris Mason 	total_size = total_data + (nr * sizeof(struct btrfs_item));
365644871b1bSChris Mason 	ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
365744871b1bSChris Mason 	if (ret == 0)
365844871b1bSChris Mason 		return -EEXIST;
365944871b1bSChris Mason 	if (ret < 0)
366044871b1bSChris Mason 		goto out;
366144871b1bSChris Mason 
366244871b1bSChris Mason 	slot = path->slots[0];
366344871b1bSChris Mason 	BUG_ON(slot < 0);
366444871b1bSChris Mason 
366544871b1bSChris Mason 	ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
366644871b1bSChris Mason 			       total_data, total_size, nr);
366744871b1bSChris Mason 
3668ed2ff2cbSChris Mason out:
366962e2749eSChris Mason 	return ret;
367062e2749eSChris Mason }
367162e2749eSChris Mason 
367262e2749eSChris Mason /*
367362e2749eSChris Mason  * Given a key and some data, insert an item into the tree.
367462e2749eSChris Mason  * This does all the path init required, making room in the tree if needed.
367562e2749eSChris Mason  */
3676e089f05cSChris Mason int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3677e089f05cSChris Mason 		      *root, struct btrfs_key *cpu_key, void *data, u32
3678e089f05cSChris Mason 		      data_size)
367962e2749eSChris Mason {
368062e2749eSChris Mason 	int ret = 0;
36812c90e5d6SChris Mason 	struct btrfs_path *path;
36825f39d397SChris Mason 	struct extent_buffer *leaf;
36835f39d397SChris Mason 	unsigned long ptr;
368462e2749eSChris Mason 
36852c90e5d6SChris Mason 	path = btrfs_alloc_path();
3686db5b493aSTsutomu Itoh 	if (!path)
3687db5b493aSTsutomu Itoh 		return -ENOMEM;
36882c90e5d6SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
368962e2749eSChris Mason 	if (!ret) {
36905f39d397SChris Mason 		leaf = path->nodes[0];
36915f39d397SChris Mason 		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
36925f39d397SChris Mason 		write_extent_buffer(leaf, data, ptr, data_size);
36935f39d397SChris Mason 		btrfs_mark_buffer_dirty(leaf);
369462e2749eSChris Mason 	}
36952c90e5d6SChris Mason 	btrfs_free_path(path);
3696aa5d6bedSChris Mason 	return ret;
3697be0e5c09SChris Mason }
3698be0e5c09SChris Mason 
369974123bd7SChris Mason /*
37005de08d7dSChris Mason  * delete the pointer from a given node.
370174123bd7SChris Mason  *
3702d352ac68SChris Mason  * the tree should have been previously balanced so the deletion does not
3703d352ac68SChris Mason  * empty a node.
370474123bd7SChris Mason  */
3705e089f05cSChris Mason static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3706e089f05cSChris Mason 		   struct btrfs_path *path, int level, int slot)
3707be0e5c09SChris Mason {
37085f39d397SChris Mason 	struct extent_buffer *parent = path->nodes[level];
37097518a238SChris Mason 	u32 nritems;
3710aa5d6bedSChris Mason 	int ret = 0;
3711bb803951SChris Mason 	int wret;
3712be0e5c09SChris Mason 
37135f39d397SChris Mason 	nritems = btrfs_header_nritems(parent);
3714be0e5c09SChris Mason 	if (slot != nritems - 1) {
37155f39d397SChris Mason 		memmove_extent_buffer(parent,
37165f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot),
37175f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot + 1),
3718d6025579SChris Mason 			      sizeof(struct btrfs_key_ptr) *
3719d6025579SChris Mason 			      (nritems - slot - 1));
3720be0e5c09SChris Mason 	}
37217518a238SChris Mason 	nritems--;
37225f39d397SChris Mason 	btrfs_set_header_nritems(parent, nritems);
37237518a238SChris Mason 	if (nritems == 0 && parent == root->node) {
37245f39d397SChris Mason 		BUG_ON(btrfs_header_level(root->node) != 1);
3725eb60ceacSChris Mason 		/* just turn the root into a leaf and break */
37265f39d397SChris Mason 		btrfs_set_header_level(root->node, 0);
3727bb803951SChris Mason 	} else if (slot == 0) {
37285f39d397SChris Mason 		struct btrfs_disk_key disk_key;
37295f39d397SChris Mason 
37305f39d397SChris Mason 		btrfs_node_key(parent, &disk_key, 0);
37315f39d397SChris Mason 		wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
37320f70abe2SChris Mason 		if (wret)
37330f70abe2SChris Mason 			ret = wret;
3734be0e5c09SChris Mason 	}
3735d6025579SChris Mason 	btrfs_mark_buffer_dirty(parent);
3736aa5d6bedSChris Mason 	return ret;
3737be0e5c09SChris Mason }
3738be0e5c09SChris Mason 
373974123bd7SChris Mason /*
3740323ac95bSChris Mason  * a helper function to delete the leaf pointed to by path->slots[1] and
37415d4f98a2SYan Zheng  * path->nodes[1].
3742323ac95bSChris Mason  *
3743323ac95bSChris Mason  * This deletes the pointer in path->nodes[1] and frees the leaf
3744323ac95bSChris Mason  * block extent.  zero is returned if it all worked out, < 0 otherwise.
3745323ac95bSChris Mason  *
3746323ac95bSChris Mason  * The path must have already been setup for deleting the leaf, including
3747323ac95bSChris Mason  * all the proper balancing.  path->nodes[1] must be locked.
3748323ac95bSChris Mason  */
37495d4f98a2SYan Zheng static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3750323ac95bSChris Mason 				   struct btrfs_root *root,
37515d4f98a2SYan Zheng 				   struct btrfs_path *path,
37525d4f98a2SYan Zheng 				   struct extent_buffer *leaf)
3753323ac95bSChris Mason {
3754323ac95bSChris Mason 	int ret;
3755323ac95bSChris Mason 
37565d4f98a2SYan Zheng 	WARN_ON(btrfs_header_generation(leaf) != trans->transid);
3757323ac95bSChris Mason 	ret = del_ptr(trans, root, path, 1, path->slots[1]);
3758323ac95bSChris Mason 	if (ret)
3759323ac95bSChris Mason 		return ret;
3760323ac95bSChris Mason 
37614d081c41SChris Mason 	/*
37624d081c41SChris Mason 	 * btrfs_free_extent is expensive, we want to make sure we
37634d081c41SChris Mason 	 * aren't holding any locks when we call it
37644d081c41SChris Mason 	 */
37654d081c41SChris Mason 	btrfs_unlock_up_safe(path, 0);
37664d081c41SChris Mason 
3767f0486c68SYan, Zheng 	root_sub_used(root, leaf->len);
3768f0486c68SYan, Zheng 
3769f0486c68SYan, Zheng 	btrfs_free_tree_block(trans, root, leaf, 0, 1);
3770f0486c68SYan, Zheng 	return 0;
3771323ac95bSChris Mason }
3772323ac95bSChris Mason /*
377374123bd7SChris Mason  * delete the item at the leaf level in path.  If that empties
377474123bd7SChris Mason  * the leaf, remove it from the tree
377574123bd7SChris Mason  */
377685e21bacSChris Mason int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
377785e21bacSChris Mason 		    struct btrfs_path *path, int slot, int nr)
3778be0e5c09SChris Mason {
37795f39d397SChris Mason 	struct extent_buffer *leaf;
37805f39d397SChris Mason 	struct btrfs_item *item;
378185e21bacSChris Mason 	int last_off;
378285e21bacSChris Mason 	int dsize = 0;
3783aa5d6bedSChris Mason 	int ret = 0;
3784aa5d6bedSChris Mason 	int wret;
378585e21bacSChris Mason 	int i;
37867518a238SChris Mason 	u32 nritems;
3787be0e5c09SChris Mason 
37885f39d397SChris Mason 	leaf = path->nodes[0];
378985e21bacSChris Mason 	last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
379085e21bacSChris Mason 
379185e21bacSChris Mason 	for (i = 0; i < nr; i++)
379285e21bacSChris Mason 		dsize += btrfs_item_size_nr(leaf, slot + i);
379385e21bacSChris Mason 
37945f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3795be0e5c09SChris Mason 
379685e21bacSChris Mason 	if (slot + nr != nritems) {
3797123abc88SChris Mason 		int data_end = leaf_data_end(root, leaf);
37985f39d397SChris Mason 
37995f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3800d6025579SChris Mason 			      data_end + dsize,
3801123abc88SChris Mason 			      btrfs_leaf_data(leaf) + data_end,
380285e21bacSChris Mason 			      last_off - data_end);
38035f39d397SChris Mason 
380485e21bacSChris Mason 		for (i = slot + nr; i < nritems; i++) {
38055f39d397SChris Mason 			u32 ioff;
3806db94535dSChris Mason 
38075f39d397SChris Mason 			item = btrfs_item_nr(leaf, i);
38085f39d397SChris Mason 			ioff = btrfs_item_offset(leaf, item);
38095f39d397SChris Mason 			btrfs_set_item_offset(leaf, item, ioff + dsize);
38100783fcfcSChris Mason 		}
3811db94535dSChris Mason 
38125f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
381385e21bacSChris Mason 			      btrfs_item_nr_offset(slot + nr),
38140783fcfcSChris Mason 			      sizeof(struct btrfs_item) *
381585e21bacSChris Mason 			      (nritems - slot - nr));
3816be0e5c09SChris Mason 	}
381785e21bacSChris Mason 	btrfs_set_header_nritems(leaf, nritems - nr);
381885e21bacSChris Mason 	nritems -= nr;
38195f39d397SChris Mason 
382074123bd7SChris Mason 	/* delete the leaf if we've emptied it */
38217518a238SChris Mason 	if (nritems == 0) {
38225f39d397SChris Mason 		if (leaf == root->node) {
38235f39d397SChris Mason 			btrfs_set_header_level(leaf, 0);
38249a8dd150SChris Mason 		} else {
3825f0486c68SYan, Zheng 			btrfs_set_path_blocking(path);
3826f0486c68SYan, Zheng 			clean_tree_block(trans, root, leaf);
38275d4f98a2SYan Zheng 			ret = btrfs_del_leaf(trans, root, path, leaf);
3828323ac95bSChris Mason 			BUG_ON(ret);
38299a8dd150SChris Mason 		}
3830be0e5c09SChris Mason 	} else {
38317518a238SChris Mason 		int used = leaf_space_used(leaf, 0, nritems);
3832aa5d6bedSChris Mason 		if (slot == 0) {
38335f39d397SChris Mason 			struct btrfs_disk_key disk_key;
38345f39d397SChris Mason 
38355f39d397SChris Mason 			btrfs_item_key(leaf, &disk_key, 0);
3836e089f05cSChris Mason 			wret = fixup_low_keys(trans, root, path,
38375f39d397SChris Mason 					      &disk_key, 1);
3838aa5d6bedSChris Mason 			if (wret)
3839aa5d6bedSChris Mason 				ret = wret;
3840aa5d6bedSChris Mason 		}
3841aa5d6bedSChris Mason 
384274123bd7SChris Mason 		/* delete the leaf if it is mostly empty */
3843d717aa1dSYan Zheng 		if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
3844be0e5c09SChris Mason 			/* push_leaf_left fixes the path.
3845be0e5c09SChris Mason 			 * make sure the path still points to our leaf
3846be0e5c09SChris Mason 			 * for possible call to del_ptr below
3847be0e5c09SChris Mason 			 */
38484920c9acSChris Mason 			slot = path->slots[1];
38495f39d397SChris Mason 			extent_buffer_get(leaf);
38505f39d397SChris Mason 
3851b9473439SChris Mason 			btrfs_set_path_blocking(path);
385299d8f83cSChris Mason 			wret = push_leaf_left(trans, root, path, 1, 1,
385399d8f83cSChris Mason 					      1, (u32)-1);
385454aa1f4dSChris Mason 			if (wret < 0 && wret != -ENOSPC)
3855aa5d6bedSChris Mason 				ret = wret;
38565f39d397SChris Mason 
38575f39d397SChris Mason 			if (path->nodes[0] == leaf &&
38585f39d397SChris Mason 			    btrfs_header_nritems(leaf)) {
385999d8f83cSChris Mason 				wret = push_leaf_right(trans, root, path, 1,
386099d8f83cSChris Mason 						       1, 1, 0);
386154aa1f4dSChris Mason 				if (wret < 0 && wret != -ENOSPC)
3862aa5d6bedSChris Mason 					ret = wret;
3863aa5d6bedSChris Mason 			}
38645f39d397SChris Mason 
38655f39d397SChris Mason 			if (btrfs_header_nritems(leaf) == 0) {
3866323ac95bSChris Mason 				path->slots[1] = slot;
38675d4f98a2SYan Zheng 				ret = btrfs_del_leaf(trans, root, path, leaf);
3868323ac95bSChris Mason 				BUG_ON(ret);
38695f39d397SChris Mason 				free_extent_buffer(leaf);
38705de08d7dSChris Mason 			} else {
3871925baeddSChris Mason 				/* if we're still in the path, make sure
3872925baeddSChris Mason 				 * we're dirty.  Otherwise, one of the
3873925baeddSChris Mason 				 * push_leaf functions must have already
3874925baeddSChris Mason 				 * dirtied this buffer
3875925baeddSChris Mason 				 */
3876925baeddSChris Mason 				if (path->nodes[0] == leaf)
38775f39d397SChris Mason 					btrfs_mark_buffer_dirty(leaf);
38785f39d397SChris Mason 				free_extent_buffer(leaf);
3879be0e5c09SChris Mason 			}
3880d5719762SChris Mason 		} else {
38815f39d397SChris Mason 			btrfs_mark_buffer_dirty(leaf);
3882be0e5c09SChris Mason 		}
38839a8dd150SChris Mason 	}
3884aa5d6bedSChris Mason 	return ret;
38859a8dd150SChris Mason }
38869a8dd150SChris Mason 
388797571fd0SChris Mason /*
3888925baeddSChris Mason  * search the tree again to find a leaf with lesser keys
38897bb86316SChris Mason  * returns 0 if it found something or 1 if there are no lesser leaves.
38907bb86316SChris Mason  * returns < 0 on io errors.
3891d352ac68SChris Mason  *
3892d352ac68SChris Mason  * This may release the path, and so you may lose any locks held at the
3893d352ac68SChris Mason  * time you call it.
38947bb86316SChris Mason  */
38957bb86316SChris Mason int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
38967bb86316SChris Mason {
3897925baeddSChris Mason 	struct btrfs_key key;
3898925baeddSChris Mason 	struct btrfs_disk_key found_key;
3899925baeddSChris Mason 	int ret;
39007bb86316SChris Mason 
3901925baeddSChris Mason 	btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
3902925baeddSChris Mason 
3903925baeddSChris Mason 	if (key.offset > 0)
3904925baeddSChris Mason 		key.offset--;
3905925baeddSChris Mason 	else if (key.type > 0)
3906925baeddSChris Mason 		key.type--;
3907925baeddSChris Mason 	else if (key.objectid > 0)
3908925baeddSChris Mason 		key.objectid--;
3909925baeddSChris Mason 	else
39107bb86316SChris Mason 		return 1;
39117bb86316SChris Mason 
3912b3b4aa74SDavid Sterba 	btrfs_release_path(path);
3913925baeddSChris Mason 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3914925baeddSChris Mason 	if (ret < 0)
3915925baeddSChris Mason 		return ret;
3916925baeddSChris Mason 	btrfs_item_key(path->nodes[0], &found_key, 0);
3917925baeddSChris Mason 	ret = comp_keys(&found_key, &key);
3918925baeddSChris Mason 	if (ret < 0)
39197bb86316SChris Mason 		return 0;
3920925baeddSChris Mason 	return 1;
39217bb86316SChris Mason }
39227bb86316SChris Mason 
39233f157a2fSChris Mason /*
39243f157a2fSChris Mason  * A helper function to walk down the tree starting at min_key, and looking
39253f157a2fSChris Mason  * for nodes or leaves that are either in cache or have a minimum
3926d352ac68SChris Mason  * transaction id.  This is used by the btree defrag code, and tree logging
39273f157a2fSChris Mason  *
39283f157a2fSChris Mason  * This does not cow, but it does stuff the starting key it finds back
39293f157a2fSChris Mason  * into min_key, so you can call btrfs_search_slot with cow=1 on the
39303f157a2fSChris Mason  * key and get a writable path.
39313f157a2fSChris Mason  *
39323f157a2fSChris Mason  * This does lock as it descends, and path->keep_locks should be set
39333f157a2fSChris Mason  * to 1 by the caller.
39343f157a2fSChris Mason  *
39353f157a2fSChris Mason  * This honors path->lowest_level to prevent descent past a given level
39363f157a2fSChris Mason  * of the tree.
39373f157a2fSChris Mason  *
3938d352ac68SChris Mason  * min_trans indicates the oldest transaction that you are interested
3939d352ac68SChris Mason  * in walking through.  Any nodes or leaves older than min_trans are
3940d352ac68SChris Mason  * skipped over (without reading them).
3941d352ac68SChris Mason  *
39423f157a2fSChris Mason  * returns zero if something useful was found, < 0 on error and 1 if there
39433f157a2fSChris Mason  * was nothing in the tree that matched the search criteria.
39443f157a2fSChris Mason  */
39453f157a2fSChris Mason int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
3946e02119d5SChris Mason 			 struct btrfs_key *max_key,
39473f157a2fSChris Mason 			 struct btrfs_path *path, int cache_only,
39483f157a2fSChris Mason 			 u64 min_trans)
39493f157a2fSChris Mason {
39503f157a2fSChris Mason 	struct extent_buffer *cur;
39513f157a2fSChris Mason 	struct btrfs_key found_key;
39523f157a2fSChris Mason 	int slot;
39539652480bSYan 	int sret;
39543f157a2fSChris Mason 	u32 nritems;
39553f157a2fSChris Mason 	int level;
39563f157a2fSChris Mason 	int ret = 1;
39573f157a2fSChris Mason 
3958934d375bSChris Mason 	WARN_ON(!path->keep_locks);
39593f157a2fSChris Mason again:
3960bd681513SChris Mason 	cur = btrfs_read_lock_root_node(root);
39613f157a2fSChris Mason 	level = btrfs_header_level(cur);
3962e02119d5SChris Mason 	WARN_ON(path->nodes[level]);
39633f157a2fSChris Mason 	path->nodes[level] = cur;
3964bd681513SChris Mason 	path->locks[level] = BTRFS_READ_LOCK;
39653f157a2fSChris Mason 
39663f157a2fSChris Mason 	if (btrfs_header_generation(cur) < min_trans) {
39673f157a2fSChris Mason 		ret = 1;
39683f157a2fSChris Mason 		goto out;
39693f157a2fSChris Mason 	}
39703f157a2fSChris Mason 	while (1) {
39713f157a2fSChris Mason 		nritems = btrfs_header_nritems(cur);
39723f157a2fSChris Mason 		level = btrfs_header_level(cur);
39739652480bSYan 		sret = bin_search(cur, min_key, level, &slot);
39743f157a2fSChris Mason 
3975323ac95bSChris Mason 		/* at the lowest level, we're done, setup the path and exit */
3976323ac95bSChris Mason 		if (level == path->lowest_level) {
3977e02119d5SChris Mason 			if (slot >= nritems)
3978e02119d5SChris Mason 				goto find_next_key;
39793f157a2fSChris Mason 			ret = 0;
39803f157a2fSChris Mason 			path->slots[level] = slot;
39813f157a2fSChris Mason 			btrfs_item_key_to_cpu(cur, &found_key, slot);
39823f157a2fSChris Mason 			goto out;
39833f157a2fSChris Mason 		}
39849652480bSYan 		if (sret && slot > 0)
39859652480bSYan 			slot--;
39863f157a2fSChris Mason 		/*
39873f157a2fSChris Mason 		 * check this node pointer against the cache_only and
39883f157a2fSChris Mason 		 * min_trans parameters.  If it isn't in cache or is too
39893f157a2fSChris Mason 		 * old, skip to the next one.
39903f157a2fSChris Mason 		 */
39913f157a2fSChris Mason 		while (slot < nritems) {
39923f157a2fSChris Mason 			u64 blockptr;
39933f157a2fSChris Mason 			u64 gen;
39943f157a2fSChris Mason 			struct extent_buffer *tmp;
3995e02119d5SChris Mason 			struct btrfs_disk_key disk_key;
3996e02119d5SChris Mason 
39973f157a2fSChris Mason 			blockptr = btrfs_node_blockptr(cur, slot);
39983f157a2fSChris Mason 			gen = btrfs_node_ptr_generation(cur, slot);
39993f157a2fSChris Mason 			if (gen < min_trans) {
40003f157a2fSChris Mason 				slot++;
40013f157a2fSChris Mason 				continue;
40023f157a2fSChris Mason 			}
40033f157a2fSChris Mason 			if (!cache_only)
40043f157a2fSChris Mason 				break;
40053f157a2fSChris Mason 
4006e02119d5SChris Mason 			if (max_key) {
4007e02119d5SChris Mason 				btrfs_node_key(cur, &disk_key, slot);
4008e02119d5SChris Mason 				if (comp_keys(&disk_key, max_key) >= 0) {
4009e02119d5SChris Mason 					ret = 1;
4010e02119d5SChris Mason 					goto out;
4011e02119d5SChris Mason 				}
4012e02119d5SChris Mason 			}
4013e02119d5SChris Mason 
40143f157a2fSChris Mason 			tmp = btrfs_find_tree_block(root, blockptr,
40153f157a2fSChris Mason 					    btrfs_level_size(root, level - 1));
40163f157a2fSChris Mason 
40173f157a2fSChris Mason 			if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
40183f157a2fSChris Mason 				free_extent_buffer(tmp);
40193f157a2fSChris Mason 				break;
40203f157a2fSChris Mason 			}
40213f157a2fSChris Mason 			if (tmp)
40223f157a2fSChris Mason 				free_extent_buffer(tmp);
40233f157a2fSChris Mason 			slot++;
40243f157a2fSChris Mason 		}
4025e02119d5SChris Mason find_next_key:
40263f157a2fSChris Mason 		/*
40273f157a2fSChris Mason 		 * we didn't find a candidate key in this node, walk forward
40283f157a2fSChris Mason 		 * and find another one
40293f157a2fSChris Mason 		 */
40303f157a2fSChris Mason 		if (slot >= nritems) {
4031e02119d5SChris Mason 			path->slots[level] = slot;
4032b4ce94deSChris Mason 			btrfs_set_path_blocking(path);
4033e02119d5SChris Mason 			sret = btrfs_find_next_key(root, path, min_key, level,
40343f157a2fSChris Mason 						  cache_only, min_trans);
4035e02119d5SChris Mason 			if (sret == 0) {
4036b3b4aa74SDavid Sterba 				btrfs_release_path(path);
40373f157a2fSChris Mason 				goto again;
40383f157a2fSChris Mason 			} else {
40393f157a2fSChris Mason 				goto out;
40403f157a2fSChris Mason 			}
40413f157a2fSChris Mason 		}
40423f157a2fSChris Mason 		/* save our key for returning back */
40433f157a2fSChris Mason 		btrfs_node_key_to_cpu(cur, &found_key, slot);
40443f157a2fSChris Mason 		path->slots[level] = slot;
40453f157a2fSChris Mason 		if (level == path->lowest_level) {
40463f157a2fSChris Mason 			ret = 0;
40473f157a2fSChris Mason 			unlock_up(path, level, 1);
40483f157a2fSChris Mason 			goto out;
40493f157a2fSChris Mason 		}
4050b4ce94deSChris Mason 		btrfs_set_path_blocking(path);
40513f157a2fSChris Mason 		cur = read_node_slot(root, cur, slot);
405297d9a8a4STsutomu Itoh 		BUG_ON(!cur);
40533f157a2fSChris Mason 
4054bd681513SChris Mason 		btrfs_tree_read_lock(cur);
4055b4ce94deSChris Mason 
4056bd681513SChris Mason 		path->locks[level - 1] = BTRFS_READ_LOCK;
40573f157a2fSChris Mason 		path->nodes[level - 1] = cur;
40583f157a2fSChris Mason 		unlock_up(path, level, 1);
4059bd681513SChris Mason 		btrfs_clear_path_blocking(path, NULL, 0);
40603f157a2fSChris Mason 	}
40613f157a2fSChris Mason out:
40623f157a2fSChris Mason 	if (ret == 0)
40633f157a2fSChris Mason 		memcpy(min_key, &found_key, sizeof(found_key));
4064b4ce94deSChris Mason 	btrfs_set_path_blocking(path);
40653f157a2fSChris Mason 	return ret;
40663f157a2fSChris Mason }
40673f157a2fSChris Mason 
40683f157a2fSChris Mason /*
40693f157a2fSChris Mason  * this is similar to btrfs_next_leaf, but does not try to preserve
40703f157a2fSChris Mason  * and fixup the path.  It looks for and returns the next key in the
40713f157a2fSChris Mason  * tree based on the current path and the cache_only and min_trans
40723f157a2fSChris Mason  * parameters.
40733f157a2fSChris Mason  *
40743f157a2fSChris Mason  * 0 is returned if another key is found, < 0 if there are any errors
40753f157a2fSChris Mason  * and 1 is returned if there are no higher keys in the tree
40763f157a2fSChris Mason  *
40773f157a2fSChris Mason  * path->keep_locks should be set to 1 on the search made before
40783f157a2fSChris Mason  * calling this function.
40793f157a2fSChris Mason  */
4080e7a84565SChris Mason int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
408133c66f43SYan Zheng 			struct btrfs_key *key, int level,
40823f157a2fSChris Mason 			int cache_only, u64 min_trans)
4083e7a84565SChris Mason {
4084e7a84565SChris Mason 	int slot;
4085e7a84565SChris Mason 	struct extent_buffer *c;
4086e7a84565SChris Mason 
4087934d375bSChris Mason 	WARN_ON(!path->keep_locks);
4088e7a84565SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
4089e7a84565SChris Mason 		if (!path->nodes[level])
4090e7a84565SChris Mason 			return 1;
4091e7a84565SChris Mason 
4092e7a84565SChris Mason 		slot = path->slots[level] + 1;
4093e7a84565SChris Mason 		c = path->nodes[level];
40943f157a2fSChris Mason next:
4095e7a84565SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
409633c66f43SYan Zheng 			int ret;
409733c66f43SYan Zheng 			int orig_lowest;
409833c66f43SYan Zheng 			struct btrfs_key cur_key;
409933c66f43SYan Zheng 			if (level + 1 >= BTRFS_MAX_LEVEL ||
410033c66f43SYan Zheng 			    !path->nodes[level + 1])
4101e7a84565SChris Mason 				return 1;
410233c66f43SYan Zheng 
410333c66f43SYan Zheng 			if (path->locks[level + 1]) {
410433c66f43SYan Zheng 				level++;
4105e7a84565SChris Mason 				continue;
4106e7a84565SChris Mason 			}
410733c66f43SYan Zheng 
410833c66f43SYan Zheng 			slot = btrfs_header_nritems(c) - 1;
410933c66f43SYan Zheng 			if (level == 0)
411033c66f43SYan Zheng 				btrfs_item_key_to_cpu(c, &cur_key, slot);
411133c66f43SYan Zheng 			else
411233c66f43SYan Zheng 				btrfs_node_key_to_cpu(c, &cur_key, slot);
411333c66f43SYan Zheng 
411433c66f43SYan Zheng 			orig_lowest = path->lowest_level;
4115b3b4aa74SDavid Sterba 			btrfs_release_path(path);
411633c66f43SYan Zheng 			path->lowest_level = level;
411733c66f43SYan Zheng 			ret = btrfs_search_slot(NULL, root, &cur_key, path,
411833c66f43SYan Zheng 						0, 0);
411933c66f43SYan Zheng 			path->lowest_level = orig_lowest;
412033c66f43SYan Zheng 			if (ret < 0)
412133c66f43SYan Zheng 				return ret;
412233c66f43SYan Zheng 
412333c66f43SYan Zheng 			c = path->nodes[level];
412433c66f43SYan Zheng 			slot = path->slots[level];
412533c66f43SYan Zheng 			if (ret == 0)
412633c66f43SYan Zheng 				slot++;
412733c66f43SYan Zheng 			goto next;
412833c66f43SYan Zheng 		}
412933c66f43SYan Zheng 
4130e7a84565SChris Mason 		if (level == 0)
4131e7a84565SChris Mason 			btrfs_item_key_to_cpu(c, key, slot);
41323f157a2fSChris Mason 		else {
41333f157a2fSChris Mason 			u64 blockptr = btrfs_node_blockptr(c, slot);
41343f157a2fSChris Mason 			u64 gen = btrfs_node_ptr_generation(c, slot);
41353f157a2fSChris Mason 
41363f157a2fSChris Mason 			if (cache_only) {
41373f157a2fSChris Mason 				struct extent_buffer *cur;
41383f157a2fSChris Mason 				cur = btrfs_find_tree_block(root, blockptr,
41393f157a2fSChris Mason 					    btrfs_level_size(root, level - 1));
41403f157a2fSChris Mason 				if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
41413f157a2fSChris Mason 					slot++;
41423f157a2fSChris Mason 					if (cur)
41433f157a2fSChris Mason 						free_extent_buffer(cur);
41443f157a2fSChris Mason 					goto next;
41453f157a2fSChris Mason 				}
41463f157a2fSChris Mason 				free_extent_buffer(cur);
41473f157a2fSChris Mason 			}
41483f157a2fSChris Mason 			if (gen < min_trans) {
41493f157a2fSChris Mason 				slot++;
41503f157a2fSChris Mason 				goto next;
41513f157a2fSChris Mason 			}
4152e7a84565SChris Mason 			btrfs_node_key_to_cpu(c, key, slot);
41533f157a2fSChris Mason 		}
4154e7a84565SChris Mason 		return 0;
4155e7a84565SChris Mason 	}
4156e7a84565SChris Mason 	return 1;
4157e7a84565SChris Mason }
4158e7a84565SChris Mason 
41597bb86316SChris Mason /*
4160925baeddSChris Mason  * search the tree again to find a leaf with greater keys
41610f70abe2SChris Mason  * returns 0 if it found something or 1 if there are no greater leaves.
41620f70abe2SChris Mason  * returns < 0 on io errors.
416397571fd0SChris Mason  */
4164234b63a0SChris Mason int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
4165d97e63b6SChris Mason {
4166d97e63b6SChris Mason 	int slot;
41678e73f275SChris Mason 	int level;
41685f39d397SChris Mason 	struct extent_buffer *c;
41698e73f275SChris Mason 	struct extent_buffer *next;
4170925baeddSChris Mason 	struct btrfs_key key;
4171925baeddSChris Mason 	u32 nritems;
4172925baeddSChris Mason 	int ret;
41738e73f275SChris Mason 	int old_spinning = path->leave_spinning;
4174bd681513SChris Mason 	int next_rw_lock = 0;
4175925baeddSChris Mason 
4176925baeddSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
4177d397712bSChris Mason 	if (nritems == 0)
4178925baeddSChris Mason 		return 1;
4179925baeddSChris Mason 
41808e73f275SChris Mason 	btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
41818e73f275SChris Mason again:
41828e73f275SChris Mason 	level = 1;
41838e73f275SChris Mason 	next = NULL;
4184bd681513SChris Mason 	next_rw_lock = 0;
4185b3b4aa74SDavid Sterba 	btrfs_release_path(path);
41868e73f275SChris Mason 
4187a2135011SChris Mason 	path->keep_locks = 1;
41888e73f275SChris Mason 	path->leave_spinning = 1;
41898e73f275SChris Mason 
4190925baeddSChris Mason 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4191925baeddSChris Mason 	path->keep_locks = 0;
4192925baeddSChris Mason 
4193925baeddSChris Mason 	if (ret < 0)
4194925baeddSChris Mason 		return ret;
4195925baeddSChris Mason 
4196a2135011SChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
4197168fd7d2SChris Mason 	/*
4198168fd7d2SChris Mason 	 * by releasing the path above we dropped all our locks.  A balance
4199168fd7d2SChris Mason 	 * could have added more items next to the key that used to be
4200168fd7d2SChris Mason 	 * at the very end of the block.  So, check again here and
4201168fd7d2SChris Mason 	 * advance the path if there are now more items available.
4202168fd7d2SChris Mason 	 */
4203a2135011SChris Mason 	if (nritems > 0 && path->slots[0] < nritems - 1) {
4204e457afecSYan Zheng 		if (ret == 0)
4205168fd7d2SChris Mason 			path->slots[0]++;
42068e73f275SChris Mason 		ret = 0;
4207925baeddSChris Mason 		goto done;
4208925baeddSChris Mason 	}
4209d97e63b6SChris Mason 
4210234b63a0SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
42118e73f275SChris Mason 		if (!path->nodes[level]) {
42128e73f275SChris Mason 			ret = 1;
42138e73f275SChris Mason 			goto done;
42148e73f275SChris Mason 		}
42155f39d397SChris Mason 
4216d97e63b6SChris Mason 		slot = path->slots[level] + 1;
4217d97e63b6SChris Mason 		c = path->nodes[level];
42185f39d397SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
4219d97e63b6SChris Mason 			level++;
42208e73f275SChris Mason 			if (level == BTRFS_MAX_LEVEL) {
42218e73f275SChris Mason 				ret = 1;
42228e73f275SChris Mason 				goto done;
42238e73f275SChris Mason 			}
4224d97e63b6SChris Mason 			continue;
4225d97e63b6SChris Mason 		}
42265f39d397SChris Mason 
4227925baeddSChris Mason 		if (next) {
4228bd681513SChris Mason 			btrfs_tree_unlock_rw(next, next_rw_lock);
42295f39d397SChris Mason 			free_extent_buffer(next);
4230925baeddSChris Mason 		}
42315f39d397SChris Mason 
42328e73f275SChris Mason 		next = c;
4233bd681513SChris Mason 		next_rw_lock = path->locks[level];
42348e73f275SChris Mason 		ret = read_block_for_search(NULL, root, path, &next, level,
42358e73f275SChris Mason 					    slot, &key);
42368e73f275SChris Mason 		if (ret == -EAGAIN)
42378e73f275SChris Mason 			goto again;
42385f39d397SChris Mason 
423976a05b35SChris Mason 		if (ret < 0) {
4240b3b4aa74SDavid Sterba 			btrfs_release_path(path);
424176a05b35SChris Mason 			goto done;
424276a05b35SChris Mason 		}
424376a05b35SChris Mason 
42445cd57b2cSChris Mason 		if (!path->skip_locking) {
4245bd681513SChris Mason 			ret = btrfs_try_tree_read_lock(next);
42468e73f275SChris Mason 			if (!ret) {
42478e73f275SChris Mason 				btrfs_set_path_blocking(path);
4248bd681513SChris Mason 				btrfs_tree_read_lock(next);
4249bd681513SChris Mason 				btrfs_clear_path_blocking(path, next,
4250bd681513SChris Mason 							  BTRFS_READ_LOCK);
42518e73f275SChris Mason 			}
4252bd681513SChris Mason 			next_rw_lock = BTRFS_READ_LOCK;
4253bd681513SChris Mason 		}
4254d97e63b6SChris Mason 		break;
4255d97e63b6SChris Mason 	}
4256d97e63b6SChris Mason 	path->slots[level] = slot;
4257d97e63b6SChris Mason 	while (1) {
4258d97e63b6SChris Mason 		level--;
4259d97e63b6SChris Mason 		c = path->nodes[level];
4260925baeddSChris Mason 		if (path->locks[level])
4261bd681513SChris Mason 			btrfs_tree_unlock_rw(c, path->locks[level]);
42628e73f275SChris Mason 
42635f39d397SChris Mason 		free_extent_buffer(c);
4264d97e63b6SChris Mason 		path->nodes[level] = next;
4265d97e63b6SChris Mason 		path->slots[level] = 0;
4266a74a4b97SChris Mason 		if (!path->skip_locking)
4267bd681513SChris Mason 			path->locks[level] = next_rw_lock;
4268d97e63b6SChris Mason 		if (!level)
4269d97e63b6SChris Mason 			break;
4270b4ce94deSChris Mason 
42718e73f275SChris Mason 		ret = read_block_for_search(NULL, root, path, &next, level,
42728e73f275SChris Mason 					    0, &key);
42738e73f275SChris Mason 		if (ret == -EAGAIN)
42748e73f275SChris Mason 			goto again;
42758e73f275SChris Mason 
427676a05b35SChris Mason 		if (ret < 0) {
4277b3b4aa74SDavid Sterba 			btrfs_release_path(path);
427876a05b35SChris Mason 			goto done;
427976a05b35SChris Mason 		}
428076a05b35SChris Mason 
42815cd57b2cSChris Mason 		if (!path->skip_locking) {
4282bd681513SChris Mason 			ret = btrfs_try_tree_read_lock(next);
42838e73f275SChris Mason 			if (!ret) {
42848e73f275SChris Mason 				btrfs_set_path_blocking(path);
4285bd681513SChris Mason 				btrfs_tree_read_lock(next);
4286bd681513SChris Mason 				btrfs_clear_path_blocking(path, next,
4287bd681513SChris Mason 							  BTRFS_READ_LOCK);
42888e73f275SChris Mason 			}
4289bd681513SChris Mason 			next_rw_lock = BTRFS_READ_LOCK;
4290bd681513SChris Mason 		}
4291d97e63b6SChris Mason 	}
42928e73f275SChris Mason 	ret = 0;
4293925baeddSChris Mason done:
4294925baeddSChris Mason 	unlock_up(path, 0, 1);
42958e73f275SChris Mason 	path->leave_spinning = old_spinning;
42968e73f275SChris Mason 	if (!old_spinning)
42978e73f275SChris Mason 		btrfs_set_path_blocking(path);
42988e73f275SChris Mason 
42998e73f275SChris Mason 	return ret;
4300d97e63b6SChris Mason }
43010b86a832SChris Mason 
43023f157a2fSChris Mason /*
43033f157a2fSChris Mason  * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
43043f157a2fSChris Mason  * searching until it gets past min_objectid or finds an item of 'type'
43053f157a2fSChris Mason  *
43063f157a2fSChris Mason  * returns 0 if something is found, 1 if nothing was found and < 0 on error
43073f157a2fSChris Mason  */
43080b86a832SChris Mason int btrfs_previous_item(struct btrfs_root *root,
43090b86a832SChris Mason 			struct btrfs_path *path, u64 min_objectid,
43100b86a832SChris Mason 			int type)
43110b86a832SChris Mason {
43120b86a832SChris Mason 	struct btrfs_key found_key;
43130b86a832SChris Mason 	struct extent_buffer *leaf;
4314e02119d5SChris Mason 	u32 nritems;
43150b86a832SChris Mason 	int ret;
43160b86a832SChris Mason 
43170b86a832SChris Mason 	while (1) {
43180b86a832SChris Mason 		if (path->slots[0] == 0) {
4319b4ce94deSChris Mason 			btrfs_set_path_blocking(path);
43200b86a832SChris Mason 			ret = btrfs_prev_leaf(root, path);
43210b86a832SChris Mason 			if (ret != 0)
43220b86a832SChris Mason 				return ret;
43230b86a832SChris Mason 		} else {
43240b86a832SChris Mason 			path->slots[0]--;
43250b86a832SChris Mason 		}
43260b86a832SChris Mason 		leaf = path->nodes[0];
4327e02119d5SChris Mason 		nritems = btrfs_header_nritems(leaf);
4328e02119d5SChris Mason 		if (nritems == 0)
4329e02119d5SChris Mason 			return 1;
4330e02119d5SChris Mason 		if (path->slots[0] == nritems)
4331e02119d5SChris Mason 			path->slots[0]--;
4332e02119d5SChris Mason 
43330b86a832SChris Mason 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4334e02119d5SChris Mason 		if (found_key.objectid < min_objectid)
4335e02119d5SChris Mason 			break;
43360a4eefbbSYan Zheng 		if (found_key.type == type)
43370a4eefbbSYan Zheng 			return 0;
4338e02119d5SChris Mason 		if (found_key.objectid == min_objectid &&
4339e02119d5SChris Mason 		    found_key.type < type)
4340e02119d5SChris Mason 			break;
43410b86a832SChris Mason 	}
43420b86a832SChris Mason 	return 1;
43430b86a832SChris Mason }
4344