xref: /openbmc/linux/fs/btrfs/ctree.c (revision bd681513fa6f2ff29aa391f01e413a2d1c59fd77)
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++) {
57*bd681513SChris Mason 		if (!p->nodes[i] || !p->locks[i])
58*bd681513SChris Mason 			continue;
59*bd681513SChris Mason 		btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
60*bd681513SChris Mason 		if (p->locks[i] == BTRFS_READ_LOCK)
61*bd681513SChris Mason 			p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
62*bd681513SChris Mason 		else if (p->locks[i] == BTRFS_WRITE_LOCK)
63*bd681513SChris 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,
76*bd681513SChris 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 	 */
87*bd681513SChris Mason 	if (held) {
88*bd681513SChris Mason 		btrfs_set_lock_blocking_rw(held, held_rw);
89*bd681513SChris Mason 		if (held_rw == BTRFS_WRITE_LOCK)
90*bd681513SChris Mason 			held_rw = BTRFS_WRITE_LOCK_BLOCKING;
91*bd681513SChris Mason 		else if (held_rw == BTRFS_READ_LOCK)
92*bd681513SChris Mason 			held_rw = BTRFS_READ_LOCK_BLOCKING;
93*bd681513SChris Mason 	}
944008c04aSChris Mason 	btrfs_set_path_blocking(p);
954008c04aSChris Mason #endif
964008c04aSChris Mason 
974008c04aSChris Mason 	for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
98*bd681513SChris Mason 		if (p->nodes[i] && p->locks[i]) {
99*bd681513SChris Mason 			btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
100*bd681513SChris Mason 			if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
101*bd681513SChris Mason 				p->locks[i] = BTRFS_WRITE_LOCK;
102*bd681513SChris Mason 			else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
103*bd681513SChris Mason 				p->locks[i] = BTRFS_READ_LOCK;
104*bd681513SChris Mason 		}
105b4ce94deSChris Mason 	}
1064008c04aSChris Mason 
1074008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC
1084008c04aSChris Mason 	if (held)
109*bd681513SChris 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]) {
137*bd681513SChris 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 
185*bd681513SChris Mason /* loop around taking references on and locking the root node of the
186*bd681513SChris Mason  * tree until you end up with a lock on the root.  A locked buffer
187*bd681513SChris Mason  * is returned, with a reference held.
188*bd681513SChris Mason  */
189*bd681513SChris Mason struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
190*bd681513SChris Mason {
191*bd681513SChris Mason 	struct extent_buffer *eb;
192*bd681513SChris Mason 
193*bd681513SChris Mason 	while (1) {
194*bd681513SChris Mason 		eb = btrfs_root_node(root);
195*bd681513SChris Mason 		btrfs_tree_read_lock(eb);
196*bd681513SChris Mason 		if (eb == root->node)
197*bd681513SChris Mason 			break;
198*bd681513SChris Mason 		btrfs_tree_read_unlock(eb);
199*bd681513SChris Mason 		free_extent_buffer(eb);
200*bd681513SChris Mason 	}
201*bd681513SChris Mason 	return eb;
202*bd681513SChris Mason }
203*bd681513SChris 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 
899*bd681513SChris Mason 	WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
900*bd681513SChris 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 
905234b63a0SChris Mason 	if (level < BTRFS_MAX_LEVEL - 1)
9065f39d397SChris Mason 		parent = path->nodes[level + 1];
907bb803951SChris Mason 	pslot = path->slots[level + 1];
908bb803951SChris Mason 
90940689478SChris Mason 	/*
91040689478SChris Mason 	 * deal with the case where there is only one pointer in the root
91140689478SChris Mason 	 * by promoting the node below to a root
91240689478SChris Mason 	 */
9135f39d397SChris Mason 	if (!parent) {
9145f39d397SChris Mason 		struct extent_buffer *child;
915bb803951SChris Mason 
9165f39d397SChris Mason 		if (btrfs_header_nritems(mid) != 1)
917bb803951SChris Mason 			return 0;
918bb803951SChris Mason 
919bb803951SChris Mason 		/* promote the child to a root */
9205f39d397SChris Mason 		child = read_node_slot(root, mid, 0);
9217951f3ceSJeff Mahoney 		BUG_ON(!child);
922925baeddSChris Mason 		btrfs_tree_lock(child);
923b4ce94deSChris Mason 		btrfs_set_lock_blocking(child);
9249fa8cfe7SChris Mason 		ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
925f0486c68SYan, Zheng 		if (ret) {
926f0486c68SYan, Zheng 			btrfs_tree_unlock(child);
927f0486c68SYan, Zheng 			free_extent_buffer(child);
928f0486c68SYan, Zheng 			goto enospc;
929f0486c68SYan, Zheng 		}
9302f375ab9SYan 
931240f62c8SChris Mason 		rcu_assign_pointer(root->node, child);
932925baeddSChris Mason 
9330b86a832SChris Mason 		add_root_to_dirty_list(root);
934925baeddSChris Mason 		btrfs_tree_unlock(child);
935b4ce94deSChris Mason 
936925baeddSChris Mason 		path->locks[level] = 0;
937bb803951SChris Mason 		path->nodes[level] = NULL;
9385f39d397SChris Mason 		clean_tree_block(trans, root, mid);
939925baeddSChris Mason 		btrfs_tree_unlock(mid);
940bb803951SChris Mason 		/* once for the path */
9415f39d397SChris Mason 		free_extent_buffer(mid);
942f0486c68SYan, Zheng 
943f0486c68SYan, Zheng 		root_sub_used(root, mid->len);
944f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, mid, 0, 1);
945bb803951SChris Mason 		/* once for the root ptr */
9465f39d397SChris Mason 		free_extent_buffer(mid);
947f0486c68SYan, Zheng 		return 0;
948bb803951SChris Mason 	}
9495f39d397SChris Mason 	if (btrfs_header_nritems(mid) >
950123abc88SChris Mason 	    BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
951bb803951SChris Mason 		return 0;
952bb803951SChris Mason 
953559af821SAndi Kleen 	btrfs_header_nritems(mid);
95454aa1f4dSChris Mason 
9555f39d397SChris Mason 	left = read_node_slot(root, parent, pslot - 1);
9565f39d397SChris Mason 	if (left) {
957925baeddSChris Mason 		btrfs_tree_lock(left);
958b4ce94deSChris Mason 		btrfs_set_lock_blocking(left);
9595f39d397SChris Mason 		wret = btrfs_cow_block(trans, root, left,
9609fa8cfe7SChris Mason 				       parent, pslot - 1, &left);
96154aa1f4dSChris Mason 		if (wret) {
96254aa1f4dSChris Mason 			ret = wret;
96354aa1f4dSChris Mason 			goto enospc;
96454aa1f4dSChris Mason 		}
9652cc58cf2SChris Mason 	}
9665f39d397SChris Mason 	right = read_node_slot(root, parent, pslot + 1);
9675f39d397SChris Mason 	if (right) {
968925baeddSChris Mason 		btrfs_tree_lock(right);
969b4ce94deSChris Mason 		btrfs_set_lock_blocking(right);
9705f39d397SChris Mason 		wret = btrfs_cow_block(trans, root, right,
9719fa8cfe7SChris Mason 				       parent, pslot + 1, &right);
9722cc58cf2SChris Mason 		if (wret) {
9732cc58cf2SChris Mason 			ret = wret;
9742cc58cf2SChris Mason 			goto enospc;
9752cc58cf2SChris Mason 		}
9762cc58cf2SChris Mason 	}
9772cc58cf2SChris Mason 
9782cc58cf2SChris Mason 	/* first, try to make some room in the middle buffer */
9795f39d397SChris Mason 	if (left) {
9805f39d397SChris Mason 		orig_slot += btrfs_header_nritems(left);
981bce4eae9SChris Mason 		wret = push_node_left(trans, root, left, mid, 1);
98279f95c82SChris Mason 		if (wret < 0)
98379f95c82SChris Mason 			ret = wret;
984559af821SAndi Kleen 		btrfs_header_nritems(mid);
985bb803951SChris Mason 	}
98679f95c82SChris Mason 
98779f95c82SChris Mason 	/*
98879f95c82SChris Mason 	 * then try to empty the right most buffer into the middle
98979f95c82SChris Mason 	 */
9905f39d397SChris Mason 	if (right) {
991971a1f66SChris Mason 		wret = push_node_left(trans, root, mid, right, 1);
99254aa1f4dSChris Mason 		if (wret < 0 && wret != -ENOSPC)
99379f95c82SChris Mason 			ret = wret;
9945f39d397SChris Mason 		if (btrfs_header_nritems(right) == 0) {
9955f39d397SChris Mason 			clean_tree_block(trans, root, right);
996925baeddSChris Mason 			btrfs_tree_unlock(right);
997e089f05cSChris Mason 			wret = del_ptr(trans, root, path, level + 1, pslot +
998e089f05cSChris Mason 				       1);
999bb803951SChris Mason 			if (wret)
1000bb803951SChris Mason 				ret = wret;
1001f0486c68SYan, Zheng 			root_sub_used(root, right->len);
1002f0486c68SYan, Zheng 			btrfs_free_tree_block(trans, root, right, 0, 1);
1003f0486c68SYan, Zheng 			free_extent_buffer(right);
1004f0486c68SYan, Zheng 			right = NULL;
1005bb803951SChris Mason 		} else {
10065f39d397SChris Mason 			struct btrfs_disk_key right_key;
10075f39d397SChris Mason 			btrfs_node_key(right, &right_key, 0);
10085f39d397SChris Mason 			btrfs_set_node_key(parent, &right_key, pslot + 1);
10095f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
1010bb803951SChris Mason 		}
1011bb803951SChris Mason 	}
10125f39d397SChris Mason 	if (btrfs_header_nritems(mid) == 1) {
101379f95c82SChris Mason 		/*
101479f95c82SChris Mason 		 * we're not allowed to leave a node with one item in the
101579f95c82SChris Mason 		 * tree during a delete.  A deletion from lower in the tree
101679f95c82SChris Mason 		 * could try to delete the only pointer in this node.
101779f95c82SChris Mason 		 * So, pull some keys from the left.
101879f95c82SChris Mason 		 * There has to be a left pointer at this point because
101979f95c82SChris Mason 		 * otherwise we would have pulled some pointers from the
102079f95c82SChris Mason 		 * right
102179f95c82SChris Mason 		 */
10225f39d397SChris Mason 		BUG_ON(!left);
10235f39d397SChris Mason 		wret = balance_node_right(trans, root, mid, left);
102454aa1f4dSChris Mason 		if (wret < 0) {
102579f95c82SChris Mason 			ret = wret;
102654aa1f4dSChris Mason 			goto enospc;
102754aa1f4dSChris Mason 		}
1028bce4eae9SChris Mason 		if (wret == 1) {
1029bce4eae9SChris Mason 			wret = push_node_left(trans, root, left, mid, 1);
1030bce4eae9SChris Mason 			if (wret < 0)
1031bce4eae9SChris Mason 				ret = wret;
1032bce4eae9SChris Mason 		}
103379f95c82SChris Mason 		BUG_ON(wret == 1);
103479f95c82SChris Mason 	}
10355f39d397SChris Mason 	if (btrfs_header_nritems(mid) == 0) {
10365f39d397SChris Mason 		clean_tree_block(trans, root, mid);
1037925baeddSChris Mason 		btrfs_tree_unlock(mid);
1038e089f05cSChris Mason 		wret = del_ptr(trans, root, path, level + 1, pslot);
1039bb803951SChris Mason 		if (wret)
1040bb803951SChris Mason 			ret = wret;
1041f0486c68SYan, Zheng 		root_sub_used(root, mid->len);
1042f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, mid, 0, 1);
1043f0486c68SYan, Zheng 		free_extent_buffer(mid);
1044f0486c68SYan, Zheng 		mid = NULL;
104579f95c82SChris Mason 	} else {
104679f95c82SChris Mason 		/* update the parent key to reflect our changes */
10475f39d397SChris Mason 		struct btrfs_disk_key mid_key;
10485f39d397SChris Mason 		btrfs_node_key(mid, &mid_key, 0);
10495f39d397SChris Mason 		btrfs_set_node_key(parent, &mid_key, pslot);
10505f39d397SChris Mason 		btrfs_mark_buffer_dirty(parent);
105179f95c82SChris Mason 	}
1052bb803951SChris Mason 
105379f95c82SChris Mason 	/* update the path */
10545f39d397SChris Mason 	if (left) {
10555f39d397SChris Mason 		if (btrfs_header_nritems(left) > orig_slot) {
10565f39d397SChris Mason 			extent_buffer_get(left);
1057925baeddSChris Mason 			/* left was locked after cow */
10585f39d397SChris Mason 			path->nodes[level] = left;
1059bb803951SChris Mason 			path->slots[level + 1] -= 1;
1060bb803951SChris Mason 			path->slots[level] = orig_slot;
1061925baeddSChris Mason 			if (mid) {
1062925baeddSChris Mason 				btrfs_tree_unlock(mid);
10635f39d397SChris Mason 				free_extent_buffer(mid);
1064925baeddSChris Mason 			}
1065bb803951SChris Mason 		} else {
10665f39d397SChris Mason 			orig_slot -= btrfs_header_nritems(left);
1067bb803951SChris Mason 			path->slots[level] = orig_slot;
1068bb803951SChris Mason 		}
1069bb803951SChris Mason 	}
107079f95c82SChris Mason 	/* double check we haven't messed things up */
1071e20d96d6SChris Mason 	if (orig_ptr !=
10725f39d397SChris Mason 	    btrfs_node_blockptr(path->nodes[level], path->slots[level]))
107379f95c82SChris Mason 		BUG();
107454aa1f4dSChris Mason enospc:
1075925baeddSChris Mason 	if (right) {
1076925baeddSChris Mason 		btrfs_tree_unlock(right);
10775f39d397SChris Mason 		free_extent_buffer(right);
1078925baeddSChris Mason 	}
1079925baeddSChris Mason 	if (left) {
1080925baeddSChris Mason 		if (path->nodes[level] != left)
1081925baeddSChris Mason 			btrfs_tree_unlock(left);
10825f39d397SChris Mason 		free_extent_buffer(left);
1083925baeddSChris Mason 	}
1084bb803951SChris Mason 	return ret;
1085bb803951SChris Mason }
1086bb803951SChris Mason 
1087d352ac68SChris Mason /* Node balancing for insertion.  Here we only split or push nodes around
1088d352ac68SChris Mason  * when they are completely full.  This is also done top down, so we
1089d352ac68SChris Mason  * have to be pessimistic.
1090d352ac68SChris Mason  */
1091d397712bSChris Mason static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1092e66f709bSChris Mason 					  struct btrfs_root *root,
1093e66f709bSChris Mason 					  struct btrfs_path *path, int level)
1094e66f709bSChris Mason {
10955f39d397SChris Mason 	struct extent_buffer *right = NULL;
10965f39d397SChris Mason 	struct extent_buffer *mid;
10975f39d397SChris Mason 	struct extent_buffer *left = NULL;
10985f39d397SChris Mason 	struct extent_buffer *parent = NULL;
1099e66f709bSChris Mason 	int ret = 0;
1100e66f709bSChris Mason 	int wret;
1101e66f709bSChris Mason 	int pslot;
1102e66f709bSChris Mason 	int orig_slot = path->slots[level];
1103e66f709bSChris Mason 
1104e66f709bSChris Mason 	if (level == 0)
1105e66f709bSChris Mason 		return 1;
1106e66f709bSChris Mason 
11075f39d397SChris Mason 	mid = path->nodes[level];
11087bb86316SChris Mason 	WARN_ON(btrfs_header_generation(mid) != trans->transid);
1109e66f709bSChris Mason 
1110e66f709bSChris Mason 	if (level < BTRFS_MAX_LEVEL - 1)
11115f39d397SChris Mason 		parent = path->nodes[level + 1];
1112e66f709bSChris Mason 	pslot = path->slots[level + 1];
1113e66f709bSChris Mason 
11145f39d397SChris Mason 	if (!parent)
1115e66f709bSChris Mason 		return 1;
1116e66f709bSChris Mason 
11175f39d397SChris Mason 	left = read_node_slot(root, parent, pslot - 1);
1118e66f709bSChris Mason 
1119e66f709bSChris Mason 	/* first, try to make some room in the middle buffer */
11205f39d397SChris Mason 	if (left) {
1121e66f709bSChris Mason 		u32 left_nr;
1122925baeddSChris Mason 
1123925baeddSChris Mason 		btrfs_tree_lock(left);
1124b4ce94deSChris Mason 		btrfs_set_lock_blocking(left);
1125b4ce94deSChris Mason 
11265f39d397SChris Mason 		left_nr = btrfs_header_nritems(left);
112733ade1f8SChris Mason 		if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
112833ade1f8SChris Mason 			wret = 1;
112933ade1f8SChris Mason 		} else {
11305f39d397SChris Mason 			ret = btrfs_cow_block(trans, root, left, parent,
11319fa8cfe7SChris Mason 					      pslot - 1, &left);
113254aa1f4dSChris Mason 			if (ret)
113354aa1f4dSChris Mason 				wret = 1;
113454aa1f4dSChris Mason 			else {
113554aa1f4dSChris Mason 				wret = push_node_left(trans, root,
1136971a1f66SChris Mason 						      left, mid, 0);
113754aa1f4dSChris Mason 			}
113833ade1f8SChris Mason 		}
1139e66f709bSChris Mason 		if (wret < 0)
1140e66f709bSChris Mason 			ret = wret;
1141e66f709bSChris Mason 		if (wret == 0) {
11425f39d397SChris Mason 			struct btrfs_disk_key disk_key;
1143e66f709bSChris Mason 			orig_slot += left_nr;
11445f39d397SChris Mason 			btrfs_node_key(mid, &disk_key, 0);
11455f39d397SChris Mason 			btrfs_set_node_key(parent, &disk_key, pslot);
11465f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
11475f39d397SChris Mason 			if (btrfs_header_nritems(left) > orig_slot) {
11485f39d397SChris Mason 				path->nodes[level] = left;
1149e66f709bSChris Mason 				path->slots[level + 1] -= 1;
1150e66f709bSChris Mason 				path->slots[level] = orig_slot;
1151925baeddSChris Mason 				btrfs_tree_unlock(mid);
11525f39d397SChris Mason 				free_extent_buffer(mid);
1153e66f709bSChris Mason 			} else {
1154e66f709bSChris Mason 				orig_slot -=
11555f39d397SChris Mason 					btrfs_header_nritems(left);
1156e66f709bSChris Mason 				path->slots[level] = orig_slot;
1157925baeddSChris Mason 				btrfs_tree_unlock(left);
11585f39d397SChris Mason 				free_extent_buffer(left);
1159e66f709bSChris Mason 			}
1160e66f709bSChris Mason 			return 0;
1161e66f709bSChris Mason 		}
1162925baeddSChris Mason 		btrfs_tree_unlock(left);
11635f39d397SChris Mason 		free_extent_buffer(left);
1164e66f709bSChris Mason 	}
11655f39d397SChris Mason 	right = read_node_slot(root, parent, pslot + 1);
1166e66f709bSChris Mason 
1167e66f709bSChris Mason 	/*
1168e66f709bSChris Mason 	 * then try to empty the right most buffer into the middle
1169e66f709bSChris Mason 	 */
11705f39d397SChris Mason 	if (right) {
117133ade1f8SChris Mason 		u32 right_nr;
1172b4ce94deSChris Mason 
1173925baeddSChris Mason 		btrfs_tree_lock(right);
1174b4ce94deSChris Mason 		btrfs_set_lock_blocking(right);
1175b4ce94deSChris Mason 
11765f39d397SChris Mason 		right_nr = btrfs_header_nritems(right);
117733ade1f8SChris Mason 		if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
117833ade1f8SChris Mason 			wret = 1;
117933ade1f8SChris Mason 		} else {
11805f39d397SChris Mason 			ret = btrfs_cow_block(trans, root, right,
11815f39d397SChris Mason 					      parent, pslot + 1,
11829fa8cfe7SChris Mason 					      &right);
118354aa1f4dSChris Mason 			if (ret)
118454aa1f4dSChris Mason 				wret = 1;
118554aa1f4dSChris Mason 			else {
118633ade1f8SChris Mason 				wret = balance_node_right(trans, root,
11875f39d397SChris Mason 							  right, mid);
118833ade1f8SChris Mason 			}
118954aa1f4dSChris Mason 		}
1190e66f709bSChris Mason 		if (wret < 0)
1191e66f709bSChris Mason 			ret = wret;
1192e66f709bSChris Mason 		if (wret == 0) {
11935f39d397SChris Mason 			struct btrfs_disk_key disk_key;
11945f39d397SChris Mason 
11955f39d397SChris Mason 			btrfs_node_key(right, &disk_key, 0);
11965f39d397SChris Mason 			btrfs_set_node_key(parent, &disk_key, pslot + 1);
11975f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
11985f39d397SChris Mason 
11995f39d397SChris Mason 			if (btrfs_header_nritems(mid) <= orig_slot) {
12005f39d397SChris Mason 				path->nodes[level] = right;
1201e66f709bSChris Mason 				path->slots[level + 1] += 1;
1202e66f709bSChris Mason 				path->slots[level] = orig_slot -
12035f39d397SChris Mason 					btrfs_header_nritems(mid);
1204925baeddSChris Mason 				btrfs_tree_unlock(mid);
12055f39d397SChris Mason 				free_extent_buffer(mid);
1206e66f709bSChris Mason 			} else {
1207925baeddSChris Mason 				btrfs_tree_unlock(right);
12085f39d397SChris Mason 				free_extent_buffer(right);
1209e66f709bSChris Mason 			}
1210e66f709bSChris Mason 			return 0;
1211e66f709bSChris Mason 		}
1212925baeddSChris Mason 		btrfs_tree_unlock(right);
12135f39d397SChris Mason 		free_extent_buffer(right);
1214e66f709bSChris Mason 	}
1215e66f709bSChris Mason 	return 1;
1216e66f709bSChris Mason }
1217e66f709bSChris Mason 
121874123bd7SChris Mason /*
1219d352ac68SChris Mason  * readahead one full node of leaves, finding things that are close
1220d352ac68SChris Mason  * to the block in 'slot', and triggering ra on them.
12213c69faecSChris Mason  */
1222c8c42864SChris Mason static void reada_for_search(struct btrfs_root *root,
1223e02119d5SChris Mason 			     struct btrfs_path *path,
122401f46658SChris Mason 			     int level, int slot, u64 objectid)
12253c69faecSChris Mason {
12265f39d397SChris Mason 	struct extent_buffer *node;
122701f46658SChris Mason 	struct btrfs_disk_key disk_key;
12283c69faecSChris Mason 	u32 nritems;
12293c69faecSChris Mason 	u64 search;
1230a7175319SChris Mason 	u64 target;
12316b80053dSChris Mason 	u64 nread = 0;
1232cb25c2eaSJosef Bacik 	u64 gen;
12333c69faecSChris Mason 	int direction = path->reada;
12345f39d397SChris Mason 	struct extent_buffer *eb;
12356b80053dSChris Mason 	u32 nr;
12366b80053dSChris Mason 	u32 blocksize;
12376b80053dSChris Mason 	u32 nscan = 0;
1238db94535dSChris Mason 
1239a6b6e75eSChris Mason 	if (level != 1)
12403c69faecSChris Mason 		return;
12413c69faecSChris Mason 
12426702ed49SChris Mason 	if (!path->nodes[level])
12436702ed49SChris Mason 		return;
12446702ed49SChris Mason 
12455f39d397SChris Mason 	node = path->nodes[level];
1246925baeddSChris Mason 
12473c69faecSChris Mason 	search = btrfs_node_blockptr(node, slot);
12486b80053dSChris Mason 	blocksize = btrfs_level_size(root, level - 1);
12496b80053dSChris Mason 	eb = btrfs_find_tree_block(root, search, blocksize);
12505f39d397SChris Mason 	if (eb) {
12515f39d397SChris Mason 		free_extent_buffer(eb);
12523c69faecSChris Mason 		return;
12533c69faecSChris Mason 	}
12543c69faecSChris Mason 
1255a7175319SChris Mason 	target = search;
12566b80053dSChris Mason 
12575f39d397SChris Mason 	nritems = btrfs_header_nritems(node);
12586b80053dSChris Mason 	nr = slot;
125925b8b936SJosef Bacik 
12603c69faecSChris Mason 	while (1) {
12616b80053dSChris Mason 		if (direction < 0) {
12626b80053dSChris Mason 			if (nr == 0)
12633c69faecSChris Mason 				break;
12646b80053dSChris Mason 			nr--;
12656b80053dSChris Mason 		} else if (direction > 0) {
12666b80053dSChris Mason 			nr++;
12676b80053dSChris Mason 			if (nr >= nritems)
12686b80053dSChris Mason 				break;
12693c69faecSChris Mason 		}
127001f46658SChris Mason 		if (path->reada < 0 && objectid) {
127101f46658SChris Mason 			btrfs_node_key(node, &disk_key, nr);
127201f46658SChris Mason 			if (btrfs_disk_key_objectid(&disk_key) != objectid)
127301f46658SChris Mason 				break;
127401f46658SChris Mason 		}
12756b80053dSChris Mason 		search = btrfs_node_blockptr(node, nr);
1276a7175319SChris Mason 		if ((search <= target && target - search <= 65536) ||
1277a7175319SChris Mason 		    (search > target && search - target <= 65536)) {
1278cb25c2eaSJosef Bacik 			gen = btrfs_node_ptr_generation(node, nr);
1279cb25c2eaSJosef Bacik 			readahead_tree_block(root, search, blocksize, gen);
12806b80053dSChris Mason 			nread += blocksize;
12813c69faecSChris Mason 		}
12826b80053dSChris Mason 		nscan++;
1283a7175319SChris Mason 		if ((nread > 65536 || nscan > 32))
12846b80053dSChris Mason 			break;
12853c69faecSChris Mason 	}
12863c69faecSChris Mason }
1287925baeddSChris Mason 
1288d352ac68SChris Mason /*
1289b4ce94deSChris Mason  * returns -EAGAIN if it had to drop the path, or zero if everything was in
1290b4ce94deSChris Mason  * cache
1291b4ce94deSChris Mason  */
1292b4ce94deSChris Mason static noinline int reada_for_balance(struct btrfs_root *root,
1293b4ce94deSChris Mason 				      struct btrfs_path *path, int level)
1294b4ce94deSChris Mason {
1295b4ce94deSChris Mason 	int slot;
1296b4ce94deSChris Mason 	int nritems;
1297b4ce94deSChris Mason 	struct extent_buffer *parent;
1298b4ce94deSChris Mason 	struct extent_buffer *eb;
1299b4ce94deSChris Mason 	u64 gen;
1300b4ce94deSChris Mason 	u64 block1 = 0;
1301b4ce94deSChris Mason 	u64 block2 = 0;
1302b4ce94deSChris Mason 	int ret = 0;
1303b4ce94deSChris Mason 	int blocksize;
1304b4ce94deSChris Mason 
13058c594ea8SChris Mason 	parent = path->nodes[level + 1];
1306b4ce94deSChris Mason 	if (!parent)
1307b4ce94deSChris Mason 		return 0;
1308b4ce94deSChris Mason 
1309b4ce94deSChris Mason 	nritems = btrfs_header_nritems(parent);
13108c594ea8SChris Mason 	slot = path->slots[level + 1];
1311b4ce94deSChris Mason 	blocksize = btrfs_level_size(root, level);
1312b4ce94deSChris Mason 
1313b4ce94deSChris Mason 	if (slot > 0) {
1314b4ce94deSChris Mason 		block1 = btrfs_node_blockptr(parent, slot - 1);
1315b4ce94deSChris Mason 		gen = btrfs_node_ptr_generation(parent, slot - 1);
1316b4ce94deSChris Mason 		eb = btrfs_find_tree_block(root, block1, blocksize);
1317b4ce94deSChris Mason 		if (eb && btrfs_buffer_uptodate(eb, gen))
1318b4ce94deSChris Mason 			block1 = 0;
1319b4ce94deSChris Mason 		free_extent_buffer(eb);
1320b4ce94deSChris Mason 	}
13218c594ea8SChris Mason 	if (slot + 1 < nritems) {
1322b4ce94deSChris Mason 		block2 = btrfs_node_blockptr(parent, slot + 1);
1323b4ce94deSChris Mason 		gen = btrfs_node_ptr_generation(parent, slot + 1);
1324b4ce94deSChris Mason 		eb = btrfs_find_tree_block(root, block2, blocksize);
1325b4ce94deSChris Mason 		if (eb && btrfs_buffer_uptodate(eb, gen))
1326b4ce94deSChris Mason 			block2 = 0;
1327b4ce94deSChris Mason 		free_extent_buffer(eb);
1328b4ce94deSChris Mason 	}
1329b4ce94deSChris Mason 	if (block1 || block2) {
1330b4ce94deSChris Mason 		ret = -EAGAIN;
13318c594ea8SChris Mason 
13328c594ea8SChris Mason 		/* release the whole path */
1333b3b4aa74SDavid Sterba 		btrfs_release_path(path);
13348c594ea8SChris Mason 
13358c594ea8SChris Mason 		/* read the blocks */
1336b4ce94deSChris Mason 		if (block1)
1337b4ce94deSChris Mason 			readahead_tree_block(root, block1, blocksize, 0);
1338b4ce94deSChris Mason 		if (block2)
1339b4ce94deSChris Mason 			readahead_tree_block(root, block2, blocksize, 0);
1340b4ce94deSChris Mason 
1341b4ce94deSChris Mason 		if (block1) {
1342b4ce94deSChris Mason 			eb = read_tree_block(root, block1, blocksize, 0);
1343b4ce94deSChris Mason 			free_extent_buffer(eb);
1344b4ce94deSChris Mason 		}
13458c594ea8SChris Mason 		if (block2) {
1346b4ce94deSChris Mason 			eb = read_tree_block(root, block2, blocksize, 0);
1347b4ce94deSChris Mason 			free_extent_buffer(eb);
1348b4ce94deSChris Mason 		}
1349b4ce94deSChris Mason 	}
1350b4ce94deSChris Mason 	return ret;
1351b4ce94deSChris Mason }
1352b4ce94deSChris Mason 
1353b4ce94deSChris Mason 
1354b4ce94deSChris Mason /*
1355d397712bSChris Mason  * when we walk down the tree, it is usually safe to unlock the higher layers
1356d397712bSChris Mason  * in the tree.  The exceptions are when our path goes through slot 0, because
1357d397712bSChris Mason  * operations on the tree might require changing key pointers higher up in the
1358d397712bSChris Mason  * tree.
1359d352ac68SChris Mason  *
1360d397712bSChris Mason  * callers might also have set path->keep_locks, which tells this code to keep
1361d397712bSChris Mason  * the lock if the path points to the last slot in the block.  This is part of
1362d397712bSChris Mason  * walking through the tree, and selecting the next slot in the higher block.
1363d352ac68SChris Mason  *
1364d397712bSChris Mason  * lowest_unlock sets the lowest level in the tree we're allowed to unlock.  so
1365d397712bSChris Mason  * if lowest_unlock is 1, level 0 won't be unlocked
1366d352ac68SChris Mason  */
1367e02119d5SChris Mason static noinline void unlock_up(struct btrfs_path *path, int level,
1368e02119d5SChris Mason 			       int lowest_unlock)
1369925baeddSChris Mason {
1370925baeddSChris Mason 	int i;
1371925baeddSChris Mason 	int skip_level = level;
1372051e1b9fSChris Mason 	int no_skips = 0;
1373925baeddSChris Mason 	struct extent_buffer *t;
1374925baeddSChris Mason 
1375925baeddSChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1376925baeddSChris Mason 		if (!path->nodes[i])
1377925baeddSChris Mason 			break;
1378925baeddSChris Mason 		if (!path->locks[i])
1379925baeddSChris Mason 			break;
1380051e1b9fSChris Mason 		if (!no_skips && path->slots[i] == 0) {
1381925baeddSChris Mason 			skip_level = i + 1;
1382925baeddSChris Mason 			continue;
1383925baeddSChris Mason 		}
1384051e1b9fSChris Mason 		if (!no_skips && path->keep_locks) {
1385925baeddSChris Mason 			u32 nritems;
1386925baeddSChris Mason 			t = path->nodes[i];
1387925baeddSChris Mason 			nritems = btrfs_header_nritems(t);
1388051e1b9fSChris Mason 			if (nritems < 1 || path->slots[i] >= nritems - 1) {
1389925baeddSChris Mason 				skip_level = i + 1;
1390925baeddSChris Mason 				continue;
1391925baeddSChris Mason 			}
1392925baeddSChris Mason 		}
1393051e1b9fSChris Mason 		if (skip_level < i && i >= lowest_unlock)
1394051e1b9fSChris Mason 			no_skips = 1;
1395051e1b9fSChris Mason 
1396925baeddSChris Mason 		t = path->nodes[i];
1397925baeddSChris Mason 		if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1398*bd681513SChris Mason 			btrfs_tree_unlock_rw(t, path->locks[i]);
1399925baeddSChris Mason 			path->locks[i] = 0;
1400925baeddSChris Mason 		}
1401925baeddSChris Mason 	}
1402925baeddSChris Mason }
1403925baeddSChris Mason 
14043c69faecSChris Mason /*
1405b4ce94deSChris Mason  * This releases any locks held in the path starting at level and
1406b4ce94deSChris Mason  * going all the way up to the root.
1407b4ce94deSChris Mason  *
1408b4ce94deSChris Mason  * btrfs_search_slot will keep the lock held on higher nodes in a few
1409b4ce94deSChris Mason  * corner cases, such as COW of the block at slot zero in the node.  This
1410b4ce94deSChris Mason  * ignores those rules, and it should only be called when there are no
1411b4ce94deSChris Mason  * more updates to be done higher up in the tree.
1412b4ce94deSChris Mason  */
1413b4ce94deSChris Mason noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1414b4ce94deSChris Mason {
1415b4ce94deSChris Mason 	int i;
1416b4ce94deSChris Mason 
14175d4f98a2SYan Zheng 	if (path->keep_locks)
1418b4ce94deSChris Mason 		return;
1419b4ce94deSChris Mason 
1420b4ce94deSChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1421b4ce94deSChris Mason 		if (!path->nodes[i])
142212f4daccSChris Mason 			continue;
1423b4ce94deSChris Mason 		if (!path->locks[i])
142412f4daccSChris Mason 			continue;
1425*bd681513SChris Mason 		btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
1426b4ce94deSChris Mason 		path->locks[i] = 0;
1427b4ce94deSChris Mason 	}
1428b4ce94deSChris Mason }
1429b4ce94deSChris Mason 
1430b4ce94deSChris Mason /*
1431c8c42864SChris Mason  * helper function for btrfs_search_slot.  The goal is to find a block
1432c8c42864SChris Mason  * in cache without setting the path to blocking.  If we find the block
1433c8c42864SChris Mason  * we return zero and the path is unchanged.
1434c8c42864SChris Mason  *
1435c8c42864SChris Mason  * If we can't find the block, we set the path blocking and do some
1436c8c42864SChris Mason  * reada.  -EAGAIN is returned and the search must be repeated.
1437c8c42864SChris Mason  */
1438c8c42864SChris Mason static int
1439c8c42864SChris Mason read_block_for_search(struct btrfs_trans_handle *trans,
1440c8c42864SChris Mason 		       struct btrfs_root *root, struct btrfs_path *p,
1441c8c42864SChris Mason 		       struct extent_buffer **eb_ret, int level, int slot,
1442c8c42864SChris Mason 		       struct btrfs_key *key)
1443c8c42864SChris Mason {
1444c8c42864SChris Mason 	u64 blocknr;
1445c8c42864SChris Mason 	u64 gen;
1446c8c42864SChris Mason 	u32 blocksize;
1447c8c42864SChris Mason 	struct extent_buffer *b = *eb_ret;
1448c8c42864SChris Mason 	struct extent_buffer *tmp;
144976a05b35SChris Mason 	int ret;
1450c8c42864SChris Mason 
1451c8c42864SChris Mason 	blocknr = btrfs_node_blockptr(b, slot);
1452c8c42864SChris Mason 	gen = btrfs_node_ptr_generation(b, slot);
1453c8c42864SChris Mason 	blocksize = btrfs_level_size(root, level - 1);
1454c8c42864SChris Mason 
1455c8c42864SChris Mason 	tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1456cb44921aSChris Mason 	if (tmp) {
1457cb44921aSChris Mason 		if (btrfs_buffer_uptodate(tmp, 0)) {
1458cb44921aSChris Mason 			if (btrfs_buffer_uptodate(tmp, gen)) {
145976a05b35SChris Mason 				/*
1460cb44921aSChris Mason 				 * we found an up to date block without
1461cb44921aSChris Mason 				 * sleeping, return
146276a05b35SChris Mason 				 * right away
146376a05b35SChris Mason 				 */
1464c8c42864SChris Mason 				*eb_ret = tmp;
1465c8c42864SChris Mason 				return 0;
1466c8c42864SChris Mason 			}
1467cb44921aSChris Mason 			/* the pages were up to date, but we failed
1468cb44921aSChris Mason 			 * the generation number check.  Do a full
1469cb44921aSChris Mason 			 * read for the generation number that is correct.
1470cb44921aSChris Mason 			 * We must do this without dropping locks so
1471cb44921aSChris Mason 			 * we can trust our generation number
1472cb44921aSChris Mason 			 */
1473cb44921aSChris Mason 			free_extent_buffer(tmp);
1474*bd681513SChris Mason 			btrfs_set_path_blocking(p);
1475*bd681513SChris Mason 
1476cb44921aSChris Mason 			tmp = read_tree_block(root, blocknr, blocksize, gen);
1477cb44921aSChris Mason 			if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1478cb44921aSChris Mason 				*eb_ret = tmp;
1479cb44921aSChris Mason 				return 0;
1480cb44921aSChris Mason 			}
1481cb44921aSChris Mason 			free_extent_buffer(tmp);
1482b3b4aa74SDavid Sterba 			btrfs_release_path(p);
1483cb44921aSChris Mason 			return -EIO;
1484cb44921aSChris Mason 		}
1485cb44921aSChris Mason 	}
1486c8c42864SChris Mason 
1487c8c42864SChris Mason 	/*
1488c8c42864SChris Mason 	 * reduce lock contention at high levels
1489c8c42864SChris Mason 	 * of the btree by dropping locks before
149076a05b35SChris Mason 	 * we read.  Don't release the lock on the current
149176a05b35SChris Mason 	 * level because we need to walk this node to figure
149276a05b35SChris Mason 	 * out which blocks to read.
1493c8c42864SChris Mason 	 */
14948c594ea8SChris Mason 	btrfs_unlock_up_safe(p, level + 1);
14958c594ea8SChris Mason 	btrfs_set_path_blocking(p);
14968c594ea8SChris Mason 
1497c8c42864SChris Mason 	free_extent_buffer(tmp);
1498c8c42864SChris Mason 	if (p->reada)
1499c8c42864SChris Mason 		reada_for_search(root, p, level, slot, key->objectid);
1500c8c42864SChris Mason 
1501b3b4aa74SDavid Sterba 	btrfs_release_path(p);
150276a05b35SChris Mason 
150376a05b35SChris Mason 	ret = -EAGAIN;
15045bdd3536SYan, Zheng 	tmp = read_tree_block(root, blocknr, blocksize, 0);
150576a05b35SChris Mason 	if (tmp) {
150676a05b35SChris Mason 		/*
150776a05b35SChris Mason 		 * If the read above didn't mark this buffer up to date,
150876a05b35SChris Mason 		 * it will never end up being up to date.  Set ret to EIO now
150976a05b35SChris Mason 		 * and give up so that our caller doesn't loop forever
151076a05b35SChris Mason 		 * on our EAGAINs.
151176a05b35SChris Mason 		 */
151276a05b35SChris Mason 		if (!btrfs_buffer_uptodate(tmp, 0))
151376a05b35SChris Mason 			ret = -EIO;
1514c8c42864SChris Mason 		free_extent_buffer(tmp);
151576a05b35SChris Mason 	}
151676a05b35SChris Mason 	return ret;
1517c8c42864SChris Mason }
1518c8c42864SChris Mason 
1519c8c42864SChris Mason /*
1520c8c42864SChris Mason  * helper function for btrfs_search_slot.  This does all of the checks
1521c8c42864SChris Mason  * for node-level blocks and does any balancing required based on
1522c8c42864SChris Mason  * the ins_len.
1523c8c42864SChris Mason  *
1524c8c42864SChris Mason  * If no extra work was required, zero is returned.  If we had to
1525c8c42864SChris Mason  * drop the path, -EAGAIN is returned and btrfs_search_slot must
1526c8c42864SChris Mason  * start over
1527c8c42864SChris Mason  */
1528c8c42864SChris Mason static int
1529c8c42864SChris Mason setup_nodes_for_search(struct btrfs_trans_handle *trans,
1530c8c42864SChris Mason 		       struct btrfs_root *root, struct btrfs_path *p,
1531*bd681513SChris Mason 		       struct extent_buffer *b, int level, int ins_len,
1532*bd681513SChris Mason 		       int *write_lock_level)
1533c8c42864SChris Mason {
1534c8c42864SChris Mason 	int ret;
1535c8c42864SChris Mason 	if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1536c8c42864SChris Mason 	    BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1537c8c42864SChris Mason 		int sret;
1538c8c42864SChris Mason 
1539*bd681513SChris Mason 		if (*write_lock_level < level + 1) {
1540*bd681513SChris Mason 			*write_lock_level = level + 1;
1541*bd681513SChris Mason 			btrfs_release_path(p);
1542*bd681513SChris Mason 			goto again;
1543*bd681513SChris Mason 		}
1544*bd681513SChris Mason 
1545c8c42864SChris Mason 		sret = reada_for_balance(root, p, level);
1546c8c42864SChris Mason 		if (sret)
1547c8c42864SChris Mason 			goto again;
1548c8c42864SChris Mason 
1549c8c42864SChris Mason 		btrfs_set_path_blocking(p);
1550c8c42864SChris Mason 		sret = split_node(trans, root, p, level);
1551*bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
1552c8c42864SChris Mason 
1553c8c42864SChris Mason 		BUG_ON(sret > 0);
1554c8c42864SChris Mason 		if (sret) {
1555c8c42864SChris Mason 			ret = sret;
1556c8c42864SChris Mason 			goto done;
1557c8c42864SChris Mason 		}
1558c8c42864SChris Mason 		b = p->nodes[level];
1559c8c42864SChris Mason 	} else if (ins_len < 0 && btrfs_header_nritems(b) <
1560cfbb9308SChris Mason 		   BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
1561c8c42864SChris Mason 		int sret;
1562c8c42864SChris Mason 
1563*bd681513SChris Mason 		if (*write_lock_level < level + 1) {
1564*bd681513SChris Mason 			*write_lock_level = level + 1;
1565*bd681513SChris Mason 			btrfs_release_path(p);
1566*bd681513SChris Mason 			goto again;
1567*bd681513SChris Mason 		}
1568*bd681513SChris Mason 
1569c8c42864SChris Mason 		sret = reada_for_balance(root, p, level);
1570c8c42864SChris Mason 		if (sret)
1571c8c42864SChris Mason 			goto again;
1572c8c42864SChris Mason 
1573c8c42864SChris Mason 		btrfs_set_path_blocking(p);
1574c8c42864SChris Mason 		sret = balance_level(trans, root, p, level);
1575*bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
1576c8c42864SChris Mason 
1577c8c42864SChris Mason 		if (sret) {
1578c8c42864SChris Mason 			ret = sret;
1579c8c42864SChris Mason 			goto done;
1580c8c42864SChris Mason 		}
1581c8c42864SChris Mason 		b = p->nodes[level];
1582c8c42864SChris Mason 		if (!b) {
1583b3b4aa74SDavid Sterba 			btrfs_release_path(p);
1584c8c42864SChris Mason 			goto again;
1585c8c42864SChris Mason 		}
1586c8c42864SChris Mason 		BUG_ON(btrfs_header_nritems(b) == 1);
1587c8c42864SChris Mason 	}
1588c8c42864SChris Mason 	return 0;
1589c8c42864SChris Mason 
1590c8c42864SChris Mason again:
1591c8c42864SChris Mason 	ret = -EAGAIN;
1592c8c42864SChris Mason done:
1593c8c42864SChris Mason 	return ret;
1594c8c42864SChris Mason }
1595c8c42864SChris Mason 
1596c8c42864SChris Mason /*
159774123bd7SChris Mason  * look for key in the tree.  path is filled in with nodes along the way
159874123bd7SChris Mason  * if key is found, we return zero and you can find the item in the leaf
159974123bd7SChris Mason  * level of the path (level 0)
160074123bd7SChris Mason  *
160174123bd7SChris Mason  * If the key isn't found, the path points to the slot where it should
1602aa5d6bedSChris Mason  * be inserted, and 1 is returned.  If there are other errors during the
1603aa5d6bedSChris Mason  * search a negative error number is returned.
160497571fd0SChris Mason  *
160597571fd0SChris Mason  * if ins_len > 0, nodes and leaves will be split as we walk down the
160697571fd0SChris Mason  * tree.  if ins_len < 0, nodes will be merged as we walk down the tree (if
160797571fd0SChris Mason  * possible)
160874123bd7SChris Mason  */
1609e089f05cSChris Mason int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1610e089f05cSChris Mason 		      *root, struct btrfs_key *key, struct btrfs_path *p, int
1611e089f05cSChris Mason 		      ins_len, int cow)
1612be0e5c09SChris Mason {
16135f39d397SChris Mason 	struct extent_buffer *b;
1614be0e5c09SChris Mason 	int slot;
1615be0e5c09SChris Mason 	int ret;
161633c66f43SYan Zheng 	int err;
1617be0e5c09SChris Mason 	int level;
1618925baeddSChris Mason 	int lowest_unlock = 1;
1619*bd681513SChris Mason 	int root_lock;
1620*bd681513SChris Mason 	/* everything at write_lock_level or lower must be write locked */
1621*bd681513SChris Mason 	int write_lock_level = 0;
16229f3a7427SChris Mason 	u8 lowest_level = 0;
16239f3a7427SChris Mason 
16246702ed49SChris Mason 	lowest_level = p->lowest_level;
1625323ac95bSChris Mason 	WARN_ON(lowest_level && ins_len > 0);
162622b0ebdaSChris Mason 	WARN_ON(p->nodes[0] != NULL);
162725179201SJosef Bacik 
1628*bd681513SChris Mason 	if (ins_len < 0) {
1629925baeddSChris Mason 		lowest_unlock = 2;
163065b51a00SChris Mason 
1631*bd681513SChris Mason 		/* when we are removing items, we might have to go up to level
1632*bd681513SChris Mason 		 * two as we update tree pointers  Make sure we keep write
1633*bd681513SChris Mason 		 * for those levels as well
1634*bd681513SChris Mason 		 */
1635*bd681513SChris Mason 		write_lock_level = 2;
1636*bd681513SChris Mason 	} else if (ins_len > 0) {
1637*bd681513SChris Mason 		/*
1638*bd681513SChris Mason 		 * for inserting items, make sure we have a write lock on
1639*bd681513SChris Mason 		 * level 1 so we can update keys
1640*bd681513SChris Mason 		 */
1641*bd681513SChris Mason 		write_lock_level = 1;
1642*bd681513SChris Mason 	}
1643*bd681513SChris Mason 
1644*bd681513SChris Mason 	if (!cow)
1645*bd681513SChris Mason 		write_lock_level = -1;
1646*bd681513SChris Mason 
1647*bd681513SChris Mason 	if (cow && (p->keep_locks || p->lowest_level))
1648*bd681513SChris Mason 		write_lock_level = BTRFS_MAX_LEVEL;
1649*bd681513SChris Mason 
1650bb803951SChris Mason again:
1651*bd681513SChris Mason 	/*
1652*bd681513SChris Mason 	 * we try very hard to do read locks on the root
1653*bd681513SChris Mason 	 */
1654*bd681513SChris Mason 	root_lock = BTRFS_READ_LOCK;
1655*bd681513SChris Mason 	level = 0;
16565d4f98a2SYan Zheng 	if (p->search_commit_root) {
1657*bd681513SChris Mason 		/*
1658*bd681513SChris Mason 		 * the commit roots are read only
1659*bd681513SChris Mason 		 * so we always do read locks
1660*bd681513SChris Mason 		 */
16615d4f98a2SYan Zheng 		b = root->commit_root;
16625d4f98a2SYan Zheng 		extent_buffer_get(b);
1663*bd681513SChris Mason 		level = btrfs_header_level(b);
16645d4f98a2SYan Zheng 		if (!p->skip_locking)
1665*bd681513SChris Mason 			btrfs_tree_read_lock(b);
16665d4f98a2SYan Zheng 	} else {
1667*bd681513SChris Mason 		if (p->skip_locking) {
16685cd57b2cSChris Mason 			b = btrfs_root_node(root);
1669*bd681513SChris Mason 			level = btrfs_header_level(b);
1670*bd681513SChris Mason 		} else {
1671*bd681513SChris Mason 			/* we don't know the level of the root node
1672*bd681513SChris Mason 			 * until we actually have it read locked
1673*bd681513SChris Mason 			 */
1674*bd681513SChris Mason 			b = btrfs_read_lock_root_node(root);
1675*bd681513SChris Mason 			level = btrfs_header_level(b);
1676*bd681513SChris Mason 			if (level <= write_lock_level) {
1677*bd681513SChris Mason 				/* whoops, must trade for write lock */
1678*bd681513SChris Mason 				btrfs_tree_read_unlock(b);
1679*bd681513SChris Mason 				free_extent_buffer(b);
1680925baeddSChris Mason 				b = btrfs_lock_root_node(root);
1681*bd681513SChris Mason 				root_lock = BTRFS_WRITE_LOCK;
1682*bd681513SChris Mason 
1683*bd681513SChris Mason 				/* the level might have changed, check again */
1684*bd681513SChris Mason 				level = btrfs_header_level(b);
16855d4f98a2SYan Zheng 			}
1686*bd681513SChris Mason 		}
1687*bd681513SChris Mason 	}
1688*bd681513SChris Mason 	p->nodes[level] = b;
1689*bd681513SChris Mason 	if (!p->skip_locking)
1690*bd681513SChris Mason 		p->locks[level] = root_lock;
1691925baeddSChris Mason 
1692eb60ceacSChris Mason 	while (b) {
16935f39d397SChris Mason 		level = btrfs_header_level(b);
169465b51a00SChris Mason 
169565b51a00SChris Mason 		/*
169665b51a00SChris Mason 		 * setup the path here so we can release it under lock
169765b51a00SChris Mason 		 * contention with the cow code
169865b51a00SChris Mason 		 */
169902217ed2SChris Mason 		if (cow) {
1700c8c42864SChris Mason 			/*
1701c8c42864SChris Mason 			 * if we don't really need to cow this block
1702c8c42864SChris Mason 			 * then we don't want to set the path blocking,
1703c8c42864SChris Mason 			 * so we test it here
1704c8c42864SChris Mason 			 */
17055d4f98a2SYan Zheng 			if (!should_cow_block(trans, root, b))
170665b51a00SChris Mason 				goto cow_done;
17075d4f98a2SYan Zheng 
1708b4ce94deSChris Mason 			btrfs_set_path_blocking(p);
1709b4ce94deSChris Mason 
1710*bd681513SChris Mason 			/*
1711*bd681513SChris Mason 			 * must have write locks on this node and the
1712*bd681513SChris Mason 			 * parent
1713*bd681513SChris Mason 			 */
1714*bd681513SChris Mason 			if (level + 1 > write_lock_level) {
1715*bd681513SChris Mason 				write_lock_level = level + 1;
1716*bd681513SChris Mason 				btrfs_release_path(p);
1717*bd681513SChris Mason 				goto again;
1718*bd681513SChris Mason 			}
1719*bd681513SChris Mason 
172033c66f43SYan Zheng 			err = btrfs_cow_block(trans, root, b,
1721e20d96d6SChris Mason 					      p->nodes[level + 1],
17229fa8cfe7SChris Mason 					      p->slots[level + 1], &b);
172333c66f43SYan Zheng 			if (err) {
172433c66f43SYan Zheng 				ret = err;
172565b51a00SChris Mason 				goto done;
172654aa1f4dSChris Mason 			}
172702217ed2SChris Mason 		}
172865b51a00SChris Mason cow_done:
172902217ed2SChris Mason 		BUG_ON(!cow && ins_len);
173065b51a00SChris Mason 
1731eb60ceacSChris Mason 		p->nodes[level] = b;
1732*bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
1733b4ce94deSChris Mason 
1734b4ce94deSChris Mason 		/*
1735b4ce94deSChris Mason 		 * we have a lock on b and as long as we aren't changing
1736b4ce94deSChris Mason 		 * the tree, there is no way to for the items in b to change.
1737b4ce94deSChris Mason 		 * It is safe to drop the lock on our parent before we
1738b4ce94deSChris Mason 		 * go through the expensive btree search on b.
1739b4ce94deSChris Mason 		 *
1740b4ce94deSChris Mason 		 * If cow is true, then we might be changing slot zero,
1741b4ce94deSChris Mason 		 * which may require changing the parent.  So, we can't
1742b4ce94deSChris Mason 		 * drop the lock until after we know which slot we're
1743b4ce94deSChris Mason 		 * operating on.
1744b4ce94deSChris Mason 		 */
1745b4ce94deSChris Mason 		if (!cow)
1746b4ce94deSChris Mason 			btrfs_unlock_up_safe(p, level + 1);
1747b4ce94deSChris Mason 
17485f39d397SChris Mason 		ret = bin_search(b, key, level, &slot);
1749b4ce94deSChris Mason 
17505f39d397SChris Mason 		if (level != 0) {
175133c66f43SYan Zheng 			int dec = 0;
175233c66f43SYan Zheng 			if (ret && slot > 0) {
175333c66f43SYan Zheng 				dec = 1;
1754be0e5c09SChris Mason 				slot -= 1;
175533c66f43SYan Zheng 			}
1756be0e5c09SChris Mason 			p->slots[level] = slot;
175733c66f43SYan Zheng 			err = setup_nodes_for_search(trans, root, p, b, level,
1758*bd681513SChris Mason 					     ins_len, &write_lock_level);
175933c66f43SYan Zheng 			if (err == -EAGAIN)
1760b4ce94deSChris Mason 				goto again;
176133c66f43SYan Zheng 			if (err) {
176233c66f43SYan Zheng 				ret = err;
176365b51a00SChris Mason 				goto done;
176433c66f43SYan Zheng 			}
17655c680ed6SChris Mason 			b = p->nodes[level];
17665c680ed6SChris Mason 			slot = p->slots[level];
1767b4ce94deSChris Mason 
1768*bd681513SChris Mason 			/*
1769*bd681513SChris Mason 			 * slot 0 is special, if we change the key
1770*bd681513SChris Mason 			 * we have to update the parent pointer
1771*bd681513SChris Mason 			 * which means we must have a write lock
1772*bd681513SChris Mason 			 * on the parent
1773*bd681513SChris Mason 			 */
1774*bd681513SChris Mason 			if (slot == 0 && cow &&
1775*bd681513SChris Mason 			    write_lock_level < level + 1) {
1776*bd681513SChris Mason 				write_lock_level = level + 1;
1777*bd681513SChris Mason 				btrfs_release_path(p);
1778*bd681513SChris Mason 				goto again;
1779*bd681513SChris Mason 			}
1780*bd681513SChris Mason 
1781f9efa9c7SChris Mason 			unlock_up(p, level, lowest_unlock);
1782f9efa9c7SChris Mason 
1783925baeddSChris Mason 			if (level == lowest_level) {
178433c66f43SYan Zheng 				if (dec)
178533c66f43SYan Zheng 					p->slots[level]++;
17865b21f2edSZheng Yan 				goto done;
1787925baeddSChris Mason 			}
1788ca7a79adSChris Mason 
178933c66f43SYan Zheng 			err = read_block_for_search(trans, root, p,
1790c8c42864SChris Mason 						    &b, level, slot, key);
179133c66f43SYan Zheng 			if (err == -EAGAIN)
1792051e1b9fSChris Mason 				goto again;
179333c66f43SYan Zheng 			if (err) {
179433c66f43SYan Zheng 				ret = err;
179576a05b35SChris Mason 				goto done;
179633c66f43SYan Zheng 			}
179776a05b35SChris Mason 
1798b4ce94deSChris Mason 			if (!p->skip_locking) {
1799*bd681513SChris Mason 				level = btrfs_header_level(b);
1800*bd681513SChris Mason 				if (level <= write_lock_level) {
1801*bd681513SChris Mason 					err = btrfs_try_tree_write_lock(b);
180233c66f43SYan Zheng 					if (!err) {
1803b4ce94deSChris Mason 						btrfs_set_path_blocking(p);
1804925baeddSChris Mason 						btrfs_tree_lock(b);
1805*bd681513SChris Mason 						btrfs_clear_path_blocking(p, b,
1806*bd681513SChris Mason 								  BTRFS_WRITE_LOCK);
1807b4ce94deSChris Mason 					}
1808*bd681513SChris Mason 					p->locks[level] = BTRFS_WRITE_LOCK;
1809*bd681513SChris Mason 				} else {
1810*bd681513SChris Mason 					err = btrfs_try_tree_read_lock(b);
1811*bd681513SChris Mason 					if (!err) {
1812*bd681513SChris Mason 						btrfs_set_path_blocking(p);
1813*bd681513SChris Mason 						btrfs_tree_read_lock(b);
1814*bd681513SChris Mason 						btrfs_clear_path_blocking(p, b,
1815*bd681513SChris Mason 								  BTRFS_READ_LOCK);
1816*bd681513SChris Mason 					}
1817*bd681513SChris Mason 					p->locks[level] = BTRFS_READ_LOCK;
1818*bd681513SChris Mason 				}
1819*bd681513SChris Mason 				p->nodes[level] = b;
1820b4ce94deSChris Mason 			}
1821be0e5c09SChris Mason 		} else {
1822be0e5c09SChris Mason 			p->slots[level] = slot;
182387b29b20SYan Zheng 			if (ins_len > 0 &&
182487b29b20SYan Zheng 			    btrfs_leaf_free_space(root, b) < ins_len) {
1825*bd681513SChris Mason 				if (write_lock_level < 1) {
1826*bd681513SChris Mason 					write_lock_level = 1;
1827*bd681513SChris Mason 					btrfs_release_path(p);
1828*bd681513SChris Mason 					goto again;
1829*bd681513SChris Mason 				}
1830*bd681513SChris Mason 
1831b4ce94deSChris Mason 				btrfs_set_path_blocking(p);
183233c66f43SYan Zheng 				err = split_leaf(trans, root, key,
1833cc0c5538SChris Mason 						 p, ins_len, ret == 0);
1834*bd681513SChris Mason 				btrfs_clear_path_blocking(p, NULL, 0);
1835b4ce94deSChris Mason 
183633c66f43SYan Zheng 				BUG_ON(err > 0);
183733c66f43SYan Zheng 				if (err) {
183833c66f43SYan Zheng 					ret = err;
183965b51a00SChris Mason 					goto done;
184065b51a00SChris Mason 				}
18415c680ed6SChris Mason 			}
1842459931ecSChris Mason 			if (!p->search_for_split)
1843925baeddSChris Mason 				unlock_up(p, level, lowest_unlock);
184465b51a00SChris Mason 			goto done;
184565b51a00SChris Mason 		}
184665b51a00SChris Mason 	}
184765b51a00SChris Mason 	ret = 1;
184865b51a00SChris Mason done:
1849b4ce94deSChris Mason 	/*
1850b4ce94deSChris Mason 	 * we don't really know what they plan on doing with the path
1851b4ce94deSChris Mason 	 * from here on, so for now just mark it as blocking
1852b4ce94deSChris Mason 	 */
1853b9473439SChris Mason 	if (!p->leave_spinning)
1854b4ce94deSChris Mason 		btrfs_set_path_blocking(p);
185576a05b35SChris Mason 	if (ret < 0)
1856b3b4aa74SDavid Sterba 		btrfs_release_path(p);
1857be0e5c09SChris Mason 	return ret;
1858be0e5c09SChris Mason }
1859be0e5c09SChris Mason 
186074123bd7SChris Mason /*
186174123bd7SChris Mason  * adjust the pointers going up the tree, starting at level
186274123bd7SChris Mason  * making sure the right key of each node is points to 'key'.
186374123bd7SChris Mason  * This is used after shifting pointers to the left, so it stops
186474123bd7SChris Mason  * fixing up pointers when a given leaf/node is not in slot 0 of the
186574123bd7SChris Mason  * higher levels
1866aa5d6bedSChris Mason  *
1867aa5d6bedSChris Mason  * If this fails to write a tree block, it returns -1, but continues
1868aa5d6bedSChris Mason  * fixing up the blocks in ram so the tree is consistent.
186974123bd7SChris Mason  */
18705f39d397SChris Mason static int fixup_low_keys(struct btrfs_trans_handle *trans,
18715f39d397SChris Mason 			  struct btrfs_root *root, struct btrfs_path *path,
18725f39d397SChris Mason 			  struct btrfs_disk_key *key, int level)
1873be0e5c09SChris Mason {
1874be0e5c09SChris Mason 	int i;
1875aa5d6bedSChris Mason 	int ret = 0;
18765f39d397SChris Mason 	struct extent_buffer *t;
18775f39d397SChris Mason 
1878234b63a0SChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1879be0e5c09SChris Mason 		int tslot = path->slots[i];
1880eb60ceacSChris Mason 		if (!path->nodes[i])
1881be0e5c09SChris Mason 			break;
18825f39d397SChris Mason 		t = path->nodes[i];
18835f39d397SChris Mason 		btrfs_set_node_key(t, key, tslot);
1884d6025579SChris Mason 		btrfs_mark_buffer_dirty(path->nodes[i]);
1885be0e5c09SChris Mason 		if (tslot != 0)
1886be0e5c09SChris Mason 			break;
1887be0e5c09SChris Mason 	}
1888aa5d6bedSChris Mason 	return ret;
1889be0e5c09SChris Mason }
1890be0e5c09SChris Mason 
189174123bd7SChris Mason /*
189231840ae1SZheng Yan  * update item key.
189331840ae1SZheng Yan  *
189431840ae1SZheng Yan  * This function isn't completely safe. It's the caller's responsibility
189531840ae1SZheng Yan  * that the new key won't break the order
189631840ae1SZheng Yan  */
189731840ae1SZheng Yan int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
189831840ae1SZheng Yan 			    struct btrfs_root *root, struct btrfs_path *path,
189931840ae1SZheng Yan 			    struct btrfs_key *new_key)
190031840ae1SZheng Yan {
190131840ae1SZheng Yan 	struct btrfs_disk_key disk_key;
190231840ae1SZheng Yan 	struct extent_buffer *eb;
190331840ae1SZheng Yan 	int slot;
190431840ae1SZheng Yan 
190531840ae1SZheng Yan 	eb = path->nodes[0];
190631840ae1SZheng Yan 	slot = path->slots[0];
190731840ae1SZheng Yan 	if (slot > 0) {
190831840ae1SZheng Yan 		btrfs_item_key(eb, &disk_key, slot - 1);
190931840ae1SZheng Yan 		if (comp_keys(&disk_key, new_key) >= 0)
191031840ae1SZheng Yan 			return -1;
191131840ae1SZheng Yan 	}
191231840ae1SZheng Yan 	if (slot < btrfs_header_nritems(eb) - 1) {
191331840ae1SZheng Yan 		btrfs_item_key(eb, &disk_key, slot + 1);
191431840ae1SZheng Yan 		if (comp_keys(&disk_key, new_key) <= 0)
191531840ae1SZheng Yan 			return -1;
191631840ae1SZheng Yan 	}
191731840ae1SZheng Yan 
191831840ae1SZheng Yan 	btrfs_cpu_key_to_disk(&disk_key, new_key);
191931840ae1SZheng Yan 	btrfs_set_item_key(eb, &disk_key, slot);
192031840ae1SZheng Yan 	btrfs_mark_buffer_dirty(eb);
192131840ae1SZheng Yan 	if (slot == 0)
192231840ae1SZheng Yan 		fixup_low_keys(trans, root, path, &disk_key, 1);
192331840ae1SZheng Yan 	return 0;
192431840ae1SZheng Yan }
192531840ae1SZheng Yan 
192631840ae1SZheng Yan /*
192774123bd7SChris Mason  * try to push data from one node into the next node left in the
192879f95c82SChris Mason  * tree.
1929aa5d6bedSChris Mason  *
1930aa5d6bedSChris Mason  * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1931aa5d6bedSChris Mason  * error, and > 0 if there was no room in the left hand block.
193274123bd7SChris Mason  */
193398ed5174SChris Mason static int push_node_left(struct btrfs_trans_handle *trans,
193498ed5174SChris Mason 			  struct btrfs_root *root, struct extent_buffer *dst,
1935971a1f66SChris Mason 			  struct extent_buffer *src, int empty)
1936be0e5c09SChris Mason {
1937be0e5c09SChris Mason 	int push_items = 0;
1938bb803951SChris Mason 	int src_nritems;
1939bb803951SChris Mason 	int dst_nritems;
1940aa5d6bedSChris Mason 	int ret = 0;
1941be0e5c09SChris Mason 
19425f39d397SChris Mason 	src_nritems = btrfs_header_nritems(src);
19435f39d397SChris Mason 	dst_nritems = btrfs_header_nritems(dst);
1944123abc88SChris Mason 	push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
19457bb86316SChris Mason 	WARN_ON(btrfs_header_generation(src) != trans->transid);
19467bb86316SChris Mason 	WARN_ON(btrfs_header_generation(dst) != trans->transid);
194754aa1f4dSChris Mason 
1948bce4eae9SChris Mason 	if (!empty && src_nritems <= 8)
1949971a1f66SChris Mason 		return 1;
1950971a1f66SChris Mason 
1951d397712bSChris Mason 	if (push_items <= 0)
1952be0e5c09SChris Mason 		return 1;
1953be0e5c09SChris Mason 
1954bce4eae9SChris Mason 	if (empty) {
1955971a1f66SChris Mason 		push_items = min(src_nritems, push_items);
1956bce4eae9SChris Mason 		if (push_items < src_nritems) {
1957bce4eae9SChris Mason 			/* leave at least 8 pointers in the node if
1958bce4eae9SChris Mason 			 * we aren't going to empty it
1959bce4eae9SChris Mason 			 */
1960bce4eae9SChris Mason 			if (src_nritems - push_items < 8) {
1961bce4eae9SChris Mason 				if (push_items <= 8)
1962bce4eae9SChris Mason 					return 1;
1963bce4eae9SChris Mason 				push_items -= 8;
1964bce4eae9SChris Mason 			}
1965bce4eae9SChris Mason 		}
1966bce4eae9SChris Mason 	} else
1967bce4eae9SChris Mason 		push_items = min(src_nritems - 8, push_items);
196879f95c82SChris Mason 
19695f39d397SChris Mason 	copy_extent_buffer(dst, src,
19705f39d397SChris Mason 			   btrfs_node_key_ptr_offset(dst_nritems),
19715f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
1972123abc88SChris Mason 			   push_items * sizeof(struct btrfs_key_ptr));
19735f39d397SChris Mason 
1974bb803951SChris Mason 	if (push_items < src_nritems) {
19755f39d397SChris Mason 		memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
19765f39d397SChris Mason 				      btrfs_node_key_ptr_offset(push_items),
1977e2fa7227SChris Mason 				      (src_nritems - push_items) *
1978123abc88SChris Mason 				      sizeof(struct btrfs_key_ptr));
1979bb803951SChris Mason 	}
19805f39d397SChris Mason 	btrfs_set_header_nritems(src, src_nritems - push_items);
19815f39d397SChris Mason 	btrfs_set_header_nritems(dst, dst_nritems + push_items);
19825f39d397SChris Mason 	btrfs_mark_buffer_dirty(src);
19835f39d397SChris Mason 	btrfs_mark_buffer_dirty(dst);
198431840ae1SZheng Yan 
1985bb803951SChris Mason 	return ret;
1986be0e5c09SChris Mason }
1987be0e5c09SChris Mason 
198897571fd0SChris Mason /*
198979f95c82SChris Mason  * try to push data from one node into the next node right in the
199079f95c82SChris Mason  * tree.
199179f95c82SChris Mason  *
199279f95c82SChris Mason  * returns 0 if some ptrs were pushed, < 0 if there was some horrible
199379f95c82SChris Mason  * error, and > 0 if there was no room in the right hand block.
199479f95c82SChris Mason  *
199579f95c82SChris Mason  * this will  only push up to 1/2 the contents of the left node over
199679f95c82SChris Mason  */
19975f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans,
19985f39d397SChris Mason 			      struct btrfs_root *root,
19995f39d397SChris Mason 			      struct extent_buffer *dst,
20005f39d397SChris Mason 			      struct extent_buffer *src)
200179f95c82SChris Mason {
200279f95c82SChris Mason 	int push_items = 0;
200379f95c82SChris Mason 	int max_push;
200479f95c82SChris Mason 	int src_nritems;
200579f95c82SChris Mason 	int dst_nritems;
200679f95c82SChris Mason 	int ret = 0;
200779f95c82SChris Mason 
20087bb86316SChris Mason 	WARN_ON(btrfs_header_generation(src) != trans->transid);
20097bb86316SChris Mason 	WARN_ON(btrfs_header_generation(dst) != trans->transid);
20107bb86316SChris Mason 
20115f39d397SChris Mason 	src_nritems = btrfs_header_nritems(src);
20125f39d397SChris Mason 	dst_nritems = btrfs_header_nritems(dst);
2013123abc88SChris Mason 	push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
2014d397712bSChris Mason 	if (push_items <= 0)
201579f95c82SChris Mason 		return 1;
2016bce4eae9SChris Mason 
2017d397712bSChris Mason 	if (src_nritems < 4)
2018bce4eae9SChris Mason 		return 1;
201979f95c82SChris Mason 
202079f95c82SChris Mason 	max_push = src_nritems / 2 + 1;
202179f95c82SChris Mason 	/* don't try to empty the node */
2022d397712bSChris Mason 	if (max_push >= src_nritems)
202379f95c82SChris Mason 		return 1;
2024252c38f0SYan 
202579f95c82SChris Mason 	if (max_push < push_items)
202679f95c82SChris Mason 		push_items = max_push;
202779f95c82SChris Mason 
20285f39d397SChris Mason 	memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
20295f39d397SChris Mason 				      btrfs_node_key_ptr_offset(0),
20305f39d397SChris Mason 				      (dst_nritems) *
20315f39d397SChris Mason 				      sizeof(struct btrfs_key_ptr));
2032d6025579SChris Mason 
20335f39d397SChris Mason 	copy_extent_buffer(dst, src,
20345f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
20355f39d397SChris Mason 			   btrfs_node_key_ptr_offset(src_nritems - push_items),
2036123abc88SChris Mason 			   push_items * sizeof(struct btrfs_key_ptr));
203779f95c82SChris Mason 
20385f39d397SChris Mason 	btrfs_set_header_nritems(src, src_nritems - push_items);
20395f39d397SChris Mason 	btrfs_set_header_nritems(dst, dst_nritems + push_items);
204079f95c82SChris Mason 
20415f39d397SChris Mason 	btrfs_mark_buffer_dirty(src);
20425f39d397SChris Mason 	btrfs_mark_buffer_dirty(dst);
204331840ae1SZheng Yan 
204479f95c82SChris Mason 	return ret;
204579f95c82SChris Mason }
204679f95c82SChris Mason 
204779f95c82SChris Mason /*
204897571fd0SChris Mason  * helper function to insert a new root level in the tree.
204997571fd0SChris Mason  * A new node is allocated, and a single item is inserted to
205097571fd0SChris Mason  * point to the existing root
2051aa5d6bedSChris Mason  *
2052aa5d6bedSChris Mason  * returns zero on success or < 0 on failure.
205397571fd0SChris Mason  */
2054d397712bSChris Mason static noinline int insert_new_root(struct btrfs_trans_handle *trans,
20555f39d397SChris Mason 			   struct btrfs_root *root,
20565f39d397SChris Mason 			   struct btrfs_path *path, int level)
205774123bd7SChris Mason {
20587bb86316SChris Mason 	u64 lower_gen;
20595f39d397SChris Mason 	struct extent_buffer *lower;
20605f39d397SChris Mason 	struct extent_buffer *c;
2061925baeddSChris Mason 	struct extent_buffer *old;
20625f39d397SChris Mason 	struct btrfs_disk_key lower_key;
20635c680ed6SChris Mason 
20645c680ed6SChris Mason 	BUG_ON(path->nodes[level]);
20655c680ed6SChris Mason 	BUG_ON(path->nodes[level-1] != root->node);
20665c680ed6SChris Mason 
20677bb86316SChris Mason 	lower = path->nodes[level-1];
20687bb86316SChris Mason 	if (level == 1)
20697bb86316SChris Mason 		btrfs_item_key(lower, &lower_key, 0);
20707bb86316SChris Mason 	else
20717bb86316SChris Mason 		btrfs_node_key(lower, &lower_key, 0);
20727bb86316SChris Mason 
207331840ae1SZheng Yan 	c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
20745d4f98a2SYan Zheng 				   root->root_key.objectid, &lower_key,
2075ad3d81baSChristoph Hellwig 				   level, root->node->start, 0);
20765f39d397SChris Mason 	if (IS_ERR(c))
20775f39d397SChris Mason 		return PTR_ERR(c);
2078925baeddSChris Mason 
2079f0486c68SYan, Zheng 	root_add_used(root, root->nodesize);
2080f0486c68SYan, Zheng 
20815d4f98a2SYan Zheng 	memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
20825f39d397SChris Mason 	btrfs_set_header_nritems(c, 1);
20835f39d397SChris Mason 	btrfs_set_header_level(c, level);
2084db94535dSChris Mason 	btrfs_set_header_bytenr(c, c->start);
20855f39d397SChris Mason 	btrfs_set_header_generation(c, trans->transid);
20865d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
20875f39d397SChris Mason 	btrfs_set_header_owner(c, root->root_key.objectid);
2088d5719762SChris Mason 
20895f39d397SChris Mason 	write_extent_buffer(c, root->fs_info->fsid,
20905f39d397SChris Mason 			    (unsigned long)btrfs_header_fsid(c),
20915f39d397SChris Mason 			    BTRFS_FSID_SIZE);
2092e17cade2SChris Mason 
2093e17cade2SChris Mason 	write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2094e17cade2SChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(c),
2095e17cade2SChris Mason 			    BTRFS_UUID_SIZE);
2096e17cade2SChris Mason 
20975f39d397SChris Mason 	btrfs_set_node_key(c, &lower_key, 0);
2098db94535dSChris Mason 	btrfs_set_node_blockptr(c, 0, lower->start);
20997bb86316SChris Mason 	lower_gen = btrfs_header_generation(lower);
210031840ae1SZheng Yan 	WARN_ON(lower_gen != trans->transid);
21017bb86316SChris Mason 
21027bb86316SChris Mason 	btrfs_set_node_ptr_generation(c, 0, lower_gen);
21035f39d397SChris Mason 
21045f39d397SChris Mason 	btrfs_mark_buffer_dirty(c);
2105d5719762SChris Mason 
2106925baeddSChris Mason 	old = root->node;
2107240f62c8SChris Mason 	rcu_assign_pointer(root->node, c);
2108925baeddSChris Mason 
2109925baeddSChris Mason 	/* the super has an extra ref to root->node */
2110925baeddSChris Mason 	free_extent_buffer(old);
2111925baeddSChris Mason 
21120b86a832SChris Mason 	add_root_to_dirty_list(root);
21135f39d397SChris Mason 	extent_buffer_get(c);
21145f39d397SChris Mason 	path->nodes[level] = c;
2115*bd681513SChris Mason 	path->locks[level] = BTRFS_WRITE_LOCK;
211674123bd7SChris Mason 	path->slots[level] = 0;
211774123bd7SChris Mason 	return 0;
211874123bd7SChris Mason }
21195c680ed6SChris Mason 
21205c680ed6SChris Mason /*
21215c680ed6SChris Mason  * worker function to insert a single pointer in a node.
21225c680ed6SChris Mason  * the node should have enough room for the pointer already
212397571fd0SChris Mason  *
21245c680ed6SChris Mason  * slot and level indicate where you want the key to go, and
21255c680ed6SChris Mason  * blocknr is the block the key points to.
2126aa5d6bedSChris Mason  *
2127aa5d6bedSChris Mason  * returns zero on success and < 0 on any error
21285c680ed6SChris Mason  */
2129e089f05cSChris Mason static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2130e089f05cSChris Mason 		      *root, struct btrfs_path *path, struct btrfs_disk_key
2131db94535dSChris Mason 		      *key, u64 bytenr, int slot, int level)
21325c680ed6SChris Mason {
21335f39d397SChris Mason 	struct extent_buffer *lower;
21345c680ed6SChris Mason 	int nritems;
21355c680ed6SChris Mason 
21365c680ed6SChris Mason 	BUG_ON(!path->nodes[level]);
2137f0486c68SYan, Zheng 	btrfs_assert_tree_locked(path->nodes[level]);
21385f39d397SChris Mason 	lower = path->nodes[level];
21395f39d397SChris Mason 	nritems = btrfs_header_nritems(lower);
2140c293498bSStoyan Gaydarov 	BUG_ON(slot > nritems);
2141123abc88SChris Mason 	if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
214274123bd7SChris Mason 		BUG();
214374123bd7SChris Mason 	if (slot != nritems) {
21445f39d397SChris Mason 		memmove_extent_buffer(lower,
21455f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot + 1),
21465f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot),
2147123abc88SChris Mason 			      (nritems - slot) * sizeof(struct btrfs_key_ptr));
214874123bd7SChris Mason 	}
21495f39d397SChris Mason 	btrfs_set_node_key(lower, key, slot);
2150db94535dSChris Mason 	btrfs_set_node_blockptr(lower, slot, bytenr);
215174493f7aSChris Mason 	WARN_ON(trans->transid == 0);
215274493f7aSChris Mason 	btrfs_set_node_ptr_generation(lower, slot, trans->transid);
21535f39d397SChris Mason 	btrfs_set_header_nritems(lower, nritems + 1);
21545f39d397SChris Mason 	btrfs_mark_buffer_dirty(lower);
215574123bd7SChris Mason 	return 0;
215674123bd7SChris Mason }
215774123bd7SChris Mason 
215897571fd0SChris Mason /*
215997571fd0SChris Mason  * split the node at the specified level in path in two.
216097571fd0SChris Mason  * The path is corrected to point to the appropriate node after the split
216197571fd0SChris Mason  *
216297571fd0SChris Mason  * Before splitting this tries to make some room in the node by pushing
216397571fd0SChris Mason  * left and right, if either one works, it returns right away.
2164aa5d6bedSChris Mason  *
2165aa5d6bedSChris Mason  * returns 0 on success and < 0 on failure
216697571fd0SChris Mason  */
2167e02119d5SChris Mason static noinline int split_node(struct btrfs_trans_handle *trans,
2168e02119d5SChris Mason 			       struct btrfs_root *root,
2169e02119d5SChris Mason 			       struct btrfs_path *path, int level)
2170be0e5c09SChris Mason {
21715f39d397SChris Mason 	struct extent_buffer *c;
21725f39d397SChris Mason 	struct extent_buffer *split;
21735f39d397SChris Mason 	struct btrfs_disk_key disk_key;
2174be0e5c09SChris Mason 	int mid;
21755c680ed6SChris Mason 	int ret;
2176aa5d6bedSChris Mason 	int wret;
21777518a238SChris Mason 	u32 c_nritems;
2178be0e5c09SChris Mason 
21795f39d397SChris Mason 	c = path->nodes[level];
21807bb86316SChris Mason 	WARN_ON(btrfs_header_generation(c) != trans->transid);
21815f39d397SChris Mason 	if (c == root->node) {
21825c680ed6SChris Mason 		/* trying to split the root, lets make a new one */
2183e089f05cSChris Mason 		ret = insert_new_root(trans, root, path, level + 1);
21845c680ed6SChris Mason 		if (ret)
21855c680ed6SChris Mason 			return ret;
2186b3612421SChris Mason 	} else {
2187e66f709bSChris Mason 		ret = push_nodes_for_insert(trans, root, path, level);
21885f39d397SChris Mason 		c = path->nodes[level];
21895f39d397SChris Mason 		if (!ret && btrfs_header_nritems(c) <
2190c448acf0SChris Mason 		    BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
2191e66f709bSChris Mason 			return 0;
219254aa1f4dSChris Mason 		if (ret < 0)
219354aa1f4dSChris Mason 			return ret;
21945c680ed6SChris Mason 	}
2195e66f709bSChris Mason 
21965f39d397SChris Mason 	c_nritems = btrfs_header_nritems(c);
21975d4f98a2SYan Zheng 	mid = (c_nritems + 1) / 2;
21985d4f98a2SYan Zheng 	btrfs_node_key(c, &disk_key, mid);
21997bb86316SChris Mason 
22005d4f98a2SYan Zheng 	split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
22017bb86316SChris Mason 					root->root_key.objectid,
22025d4f98a2SYan Zheng 					&disk_key, level, c->start, 0);
22035f39d397SChris Mason 	if (IS_ERR(split))
22045f39d397SChris Mason 		return PTR_ERR(split);
220554aa1f4dSChris Mason 
2206f0486c68SYan, Zheng 	root_add_used(root, root->nodesize);
2207f0486c68SYan, Zheng 
22085d4f98a2SYan Zheng 	memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
22095f39d397SChris Mason 	btrfs_set_header_level(split, btrfs_header_level(c));
2210db94535dSChris Mason 	btrfs_set_header_bytenr(split, split->start);
22115f39d397SChris Mason 	btrfs_set_header_generation(split, trans->transid);
22125d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
22135f39d397SChris Mason 	btrfs_set_header_owner(split, root->root_key.objectid);
22145f39d397SChris Mason 	write_extent_buffer(split, root->fs_info->fsid,
22155f39d397SChris Mason 			    (unsigned long)btrfs_header_fsid(split),
22165f39d397SChris Mason 			    BTRFS_FSID_SIZE);
2217e17cade2SChris Mason 	write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2218e17cade2SChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(split),
2219e17cade2SChris Mason 			    BTRFS_UUID_SIZE);
22205f39d397SChris Mason 
22215f39d397SChris Mason 
22225f39d397SChris Mason 	copy_extent_buffer(split, c,
22235f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
22245f39d397SChris Mason 			   btrfs_node_key_ptr_offset(mid),
2225123abc88SChris Mason 			   (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
22265f39d397SChris Mason 	btrfs_set_header_nritems(split, c_nritems - mid);
22275f39d397SChris Mason 	btrfs_set_header_nritems(c, mid);
2228aa5d6bedSChris Mason 	ret = 0;
2229aa5d6bedSChris Mason 
22305f39d397SChris Mason 	btrfs_mark_buffer_dirty(c);
22315f39d397SChris Mason 	btrfs_mark_buffer_dirty(split);
22325f39d397SChris Mason 
2233db94535dSChris Mason 	wret = insert_ptr(trans, root, path, &disk_key, split->start,
22345f39d397SChris Mason 			  path->slots[level + 1] + 1,
2235123abc88SChris Mason 			  level + 1);
2236aa5d6bedSChris Mason 	if (wret)
2237aa5d6bedSChris Mason 		ret = wret;
2238aa5d6bedSChris Mason 
22395de08d7dSChris Mason 	if (path->slots[level] >= mid) {
22405c680ed6SChris Mason 		path->slots[level] -= mid;
2241925baeddSChris Mason 		btrfs_tree_unlock(c);
22425f39d397SChris Mason 		free_extent_buffer(c);
22435f39d397SChris Mason 		path->nodes[level] = split;
22445c680ed6SChris Mason 		path->slots[level + 1] += 1;
2245eb60ceacSChris Mason 	} else {
2246925baeddSChris Mason 		btrfs_tree_unlock(split);
22475f39d397SChris Mason 		free_extent_buffer(split);
2248be0e5c09SChris Mason 	}
2249aa5d6bedSChris Mason 	return ret;
2250be0e5c09SChris Mason }
2251be0e5c09SChris Mason 
225274123bd7SChris Mason /*
225374123bd7SChris Mason  * how many bytes are required to store the items in a leaf.  start
225474123bd7SChris Mason  * and nr indicate which items in the leaf to check.  This totals up the
225574123bd7SChris Mason  * space used both by the item structs and the item data
225674123bd7SChris Mason  */
22575f39d397SChris Mason static int leaf_space_used(struct extent_buffer *l, int start, int nr)
2258be0e5c09SChris Mason {
2259be0e5c09SChris Mason 	int data_len;
22605f39d397SChris Mason 	int nritems = btrfs_header_nritems(l);
2261d4dbff95SChris Mason 	int end = min(nritems, start + nr) - 1;
2262be0e5c09SChris Mason 
2263be0e5c09SChris Mason 	if (!nr)
2264be0e5c09SChris Mason 		return 0;
22655f39d397SChris Mason 	data_len = btrfs_item_end_nr(l, start);
22665f39d397SChris Mason 	data_len = data_len - btrfs_item_offset_nr(l, end);
22670783fcfcSChris Mason 	data_len += sizeof(struct btrfs_item) * nr;
2268d4dbff95SChris Mason 	WARN_ON(data_len < 0);
2269be0e5c09SChris Mason 	return data_len;
2270be0e5c09SChris Mason }
2271be0e5c09SChris Mason 
227274123bd7SChris Mason /*
2273d4dbff95SChris Mason  * The space between the end of the leaf items and
2274d4dbff95SChris Mason  * the start of the leaf data.  IOW, how much room
2275d4dbff95SChris Mason  * the leaf has left for both items and data
2276d4dbff95SChris Mason  */
2277d397712bSChris Mason noinline int btrfs_leaf_free_space(struct btrfs_root *root,
2278e02119d5SChris Mason 				   struct extent_buffer *leaf)
2279d4dbff95SChris Mason {
22805f39d397SChris Mason 	int nritems = btrfs_header_nritems(leaf);
22815f39d397SChris Mason 	int ret;
22825f39d397SChris Mason 	ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
22835f39d397SChris Mason 	if (ret < 0) {
2284d397712bSChris Mason 		printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2285d397712bSChris Mason 		       "used %d nritems %d\n",
2286ae2f5411SJens Axboe 		       ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
22875f39d397SChris Mason 		       leaf_space_used(leaf, 0, nritems), nritems);
22885f39d397SChris Mason 	}
22895f39d397SChris Mason 	return ret;
2290d4dbff95SChris Mason }
2291d4dbff95SChris Mason 
229299d8f83cSChris Mason /*
229399d8f83cSChris Mason  * min slot controls the lowest index we're willing to push to the
229499d8f83cSChris Mason  * right.  We'll push up to and including min_slot, but no lower
229599d8f83cSChris Mason  */
229644871b1bSChris Mason static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
229744871b1bSChris Mason 				      struct btrfs_root *root,
229844871b1bSChris Mason 				      struct btrfs_path *path,
229944871b1bSChris Mason 				      int data_size, int empty,
230044871b1bSChris Mason 				      struct extent_buffer *right,
230199d8f83cSChris Mason 				      int free_space, u32 left_nritems,
230299d8f83cSChris Mason 				      u32 min_slot)
230300ec4c51SChris Mason {
23045f39d397SChris Mason 	struct extent_buffer *left = path->nodes[0];
230544871b1bSChris Mason 	struct extent_buffer *upper = path->nodes[1];
23065f39d397SChris Mason 	struct btrfs_disk_key disk_key;
230700ec4c51SChris Mason 	int slot;
230834a38218SChris Mason 	u32 i;
230900ec4c51SChris Mason 	int push_space = 0;
231000ec4c51SChris Mason 	int push_items = 0;
23110783fcfcSChris Mason 	struct btrfs_item *item;
231234a38218SChris Mason 	u32 nr;
23137518a238SChris Mason 	u32 right_nritems;
23145f39d397SChris Mason 	u32 data_end;
2315db94535dSChris Mason 	u32 this_item_size;
231600ec4c51SChris Mason 
231734a38218SChris Mason 	if (empty)
231834a38218SChris Mason 		nr = 0;
231934a38218SChris Mason 	else
232099d8f83cSChris Mason 		nr = max_t(u32, 1, min_slot);
232134a38218SChris Mason 
232231840ae1SZheng Yan 	if (path->slots[0] >= left_nritems)
232387b29b20SYan Zheng 		push_space += data_size;
232431840ae1SZheng Yan 
232544871b1bSChris Mason 	slot = path->slots[1];
232634a38218SChris Mason 	i = left_nritems - 1;
232734a38218SChris Mason 	while (i >= nr) {
23285f39d397SChris Mason 		item = btrfs_item_nr(left, i);
2329db94535dSChris Mason 
233031840ae1SZheng Yan 		if (!empty && push_items > 0) {
233131840ae1SZheng Yan 			if (path->slots[0] > i)
233231840ae1SZheng Yan 				break;
233331840ae1SZheng Yan 			if (path->slots[0] == i) {
233431840ae1SZheng Yan 				int space = btrfs_leaf_free_space(root, left);
233531840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
233631840ae1SZheng Yan 					break;
233731840ae1SZheng Yan 			}
233831840ae1SZheng Yan 		}
233931840ae1SZheng Yan 
234000ec4c51SChris Mason 		if (path->slots[0] == i)
234187b29b20SYan Zheng 			push_space += data_size;
2342db94535dSChris Mason 
2343db94535dSChris Mason 		this_item_size = btrfs_item_size(left, item);
2344db94535dSChris Mason 		if (this_item_size + sizeof(*item) + push_space > free_space)
234500ec4c51SChris Mason 			break;
234631840ae1SZheng Yan 
234700ec4c51SChris Mason 		push_items++;
2348db94535dSChris Mason 		push_space += this_item_size + sizeof(*item);
234934a38218SChris Mason 		if (i == 0)
235034a38218SChris Mason 			break;
235134a38218SChris Mason 		i--;
2352db94535dSChris Mason 	}
23535f39d397SChris Mason 
2354925baeddSChris Mason 	if (push_items == 0)
2355925baeddSChris Mason 		goto out_unlock;
23565f39d397SChris Mason 
235734a38218SChris Mason 	if (!empty && push_items == left_nritems)
2358a429e513SChris Mason 		WARN_ON(1);
23595f39d397SChris Mason 
236000ec4c51SChris Mason 	/* push left to right */
23615f39d397SChris Mason 	right_nritems = btrfs_header_nritems(right);
236234a38218SChris Mason 
23635f39d397SChris Mason 	push_space = btrfs_item_end_nr(left, left_nritems - push_items);
2364123abc88SChris Mason 	push_space -= leaf_data_end(root, left);
23655f39d397SChris Mason 
236600ec4c51SChris Mason 	/* make room in the right data area */
23675f39d397SChris Mason 	data_end = leaf_data_end(root, right);
23685f39d397SChris Mason 	memmove_extent_buffer(right,
23695f39d397SChris Mason 			      btrfs_leaf_data(right) + data_end - push_space,
23705f39d397SChris Mason 			      btrfs_leaf_data(right) + data_end,
23715f39d397SChris Mason 			      BTRFS_LEAF_DATA_SIZE(root) - data_end);
23725f39d397SChris Mason 
237300ec4c51SChris Mason 	/* copy from the left data area */
23745f39d397SChris Mason 	copy_extent_buffer(right, left, btrfs_leaf_data(right) +
2375d6025579SChris Mason 		     BTRFS_LEAF_DATA_SIZE(root) - push_space,
2376d6025579SChris Mason 		     btrfs_leaf_data(left) + leaf_data_end(root, left),
2377d6025579SChris Mason 		     push_space);
23785f39d397SChris Mason 
23795f39d397SChris Mason 	memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
23805f39d397SChris Mason 			      btrfs_item_nr_offset(0),
23810783fcfcSChris Mason 			      right_nritems * sizeof(struct btrfs_item));
23825f39d397SChris Mason 
238300ec4c51SChris Mason 	/* copy the items from left to right */
23845f39d397SChris Mason 	copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
23855f39d397SChris Mason 		   btrfs_item_nr_offset(left_nritems - push_items),
23860783fcfcSChris Mason 		   push_items * sizeof(struct btrfs_item));
238700ec4c51SChris Mason 
238800ec4c51SChris Mason 	/* update the item pointers */
23897518a238SChris Mason 	right_nritems += push_items;
23905f39d397SChris Mason 	btrfs_set_header_nritems(right, right_nritems);
2391123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root);
23927518a238SChris Mason 	for (i = 0; i < right_nritems; i++) {
23935f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2394db94535dSChris Mason 		push_space -= btrfs_item_size(right, item);
2395db94535dSChris Mason 		btrfs_set_item_offset(right, item, push_space);
2396db94535dSChris Mason 	}
2397db94535dSChris Mason 
23987518a238SChris Mason 	left_nritems -= push_items;
23995f39d397SChris Mason 	btrfs_set_header_nritems(left, left_nritems);
240000ec4c51SChris Mason 
240134a38218SChris Mason 	if (left_nritems)
24025f39d397SChris Mason 		btrfs_mark_buffer_dirty(left);
2403f0486c68SYan, Zheng 	else
2404f0486c68SYan, Zheng 		clean_tree_block(trans, root, left);
2405f0486c68SYan, Zheng 
24065f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
2407a429e513SChris Mason 
24085f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
24095f39d397SChris Mason 	btrfs_set_node_key(upper, &disk_key, slot + 1);
2410d6025579SChris Mason 	btrfs_mark_buffer_dirty(upper);
241102217ed2SChris Mason 
241200ec4c51SChris Mason 	/* then fixup the leaf pointer in the path */
24137518a238SChris Mason 	if (path->slots[0] >= left_nritems) {
24147518a238SChris Mason 		path->slots[0] -= left_nritems;
2415925baeddSChris Mason 		if (btrfs_header_nritems(path->nodes[0]) == 0)
2416925baeddSChris Mason 			clean_tree_block(trans, root, path->nodes[0]);
2417925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
24185f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
24195f39d397SChris Mason 		path->nodes[0] = right;
242000ec4c51SChris Mason 		path->slots[1] += 1;
242100ec4c51SChris Mason 	} else {
2422925baeddSChris Mason 		btrfs_tree_unlock(right);
24235f39d397SChris Mason 		free_extent_buffer(right);
242400ec4c51SChris Mason 	}
242500ec4c51SChris Mason 	return 0;
2426925baeddSChris Mason 
2427925baeddSChris Mason out_unlock:
2428925baeddSChris Mason 	btrfs_tree_unlock(right);
2429925baeddSChris Mason 	free_extent_buffer(right);
2430925baeddSChris Mason 	return 1;
243100ec4c51SChris Mason }
2432925baeddSChris Mason 
243300ec4c51SChris Mason /*
243444871b1bSChris Mason  * push some data in the path leaf to the right, trying to free up at
243574123bd7SChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
243644871b1bSChris Mason  *
243744871b1bSChris Mason  * returns 1 if the push failed because the other node didn't have enough
243844871b1bSChris Mason  * room, 0 if everything worked out and < 0 if there were major errors.
243999d8f83cSChris Mason  *
244099d8f83cSChris Mason  * this will push starting from min_slot to the end of the leaf.  It won't
244199d8f83cSChris Mason  * push any slot lower than min_slot
244274123bd7SChris Mason  */
244344871b1bSChris Mason static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
244499d8f83cSChris Mason 			   *root, struct btrfs_path *path,
244599d8f83cSChris Mason 			   int min_data_size, int data_size,
244699d8f83cSChris Mason 			   int empty, u32 min_slot)
2447be0e5c09SChris Mason {
244844871b1bSChris Mason 	struct extent_buffer *left = path->nodes[0];
244944871b1bSChris Mason 	struct extent_buffer *right;
245044871b1bSChris Mason 	struct extent_buffer *upper;
245144871b1bSChris Mason 	int slot;
245244871b1bSChris Mason 	int free_space;
245344871b1bSChris Mason 	u32 left_nritems;
245444871b1bSChris Mason 	int ret;
245544871b1bSChris Mason 
245644871b1bSChris Mason 	if (!path->nodes[1])
245744871b1bSChris Mason 		return 1;
245844871b1bSChris Mason 
245944871b1bSChris Mason 	slot = path->slots[1];
246044871b1bSChris Mason 	upper = path->nodes[1];
246144871b1bSChris Mason 	if (slot >= btrfs_header_nritems(upper) - 1)
246244871b1bSChris Mason 		return 1;
246344871b1bSChris Mason 
246444871b1bSChris Mason 	btrfs_assert_tree_locked(path->nodes[1]);
246544871b1bSChris Mason 
246644871b1bSChris Mason 	right = read_node_slot(root, upper, slot + 1);
246791ca338dSTsutomu Itoh 	if (right == NULL)
246891ca338dSTsutomu Itoh 		return 1;
246991ca338dSTsutomu Itoh 
247044871b1bSChris Mason 	btrfs_tree_lock(right);
247144871b1bSChris Mason 	btrfs_set_lock_blocking(right);
247244871b1bSChris Mason 
247344871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, right);
247444871b1bSChris Mason 	if (free_space < data_size)
247544871b1bSChris Mason 		goto out_unlock;
247644871b1bSChris Mason 
247744871b1bSChris Mason 	/* cow and double check */
247844871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, right, upper,
247944871b1bSChris Mason 			      slot + 1, &right);
248044871b1bSChris Mason 	if (ret)
248144871b1bSChris Mason 		goto out_unlock;
248244871b1bSChris Mason 
248344871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, right);
248444871b1bSChris Mason 	if (free_space < data_size)
248544871b1bSChris Mason 		goto out_unlock;
248644871b1bSChris Mason 
248744871b1bSChris Mason 	left_nritems = btrfs_header_nritems(left);
248844871b1bSChris Mason 	if (left_nritems == 0)
248944871b1bSChris Mason 		goto out_unlock;
249044871b1bSChris Mason 
249199d8f83cSChris Mason 	return __push_leaf_right(trans, root, path, min_data_size, empty,
249299d8f83cSChris Mason 				right, free_space, left_nritems, min_slot);
249344871b1bSChris Mason out_unlock:
249444871b1bSChris Mason 	btrfs_tree_unlock(right);
249544871b1bSChris Mason 	free_extent_buffer(right);
249644871b1bSChris Mason 	return 1;
249744871b1bSChris Mason }
249844871b1bSChris Mason 
249944871b1bSChris Mason /*
250044871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
250144871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
250299d8f83cSChris Mason  *
250399d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
250499d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us do all the
250599d8f83cSChris Mason  * items
250644871b1bSChris Mason  */
250744871b1bSChris Mason static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
250844871b1bSChris Mason 				     struct btrfs_root *root,
250944871b1bSChris Mason 				     struct btrfs_path *path, int data_size,
251044871b1bSChris Mason 				     int empty, struct extent_buffer *left,
251199d8f83cSChris Mason 				     int free_space, u32 right_nritems,
251299d8f83cSChris Mason 				     u32 max_slot)
251344871b1bSChris Mason {
25145f39d397SChris Mason 	struct btrfs_disk_key disk_key;
25155f39d397SChris Mason 	struct extent_buffer *right = path->nodes[0];
2516be0e5c09SChris Mason 	int i;
2517be0e5c09SChris Mason 	int push_space = 0;
2518be0e5c09SChris Mason 	int push_items = 0;
25190783fcfcSChris Mason 	struct btrfs_item *item;
25207518a238SChris Mason 	u32 old_left_nritems;
252134a38218SChris Mason 	u32 nr;
2522aa5d6bedSChris Mason 	int ret = 0;
2523aa5d6bedSChris Mason 	int wret;
2524db94535dSChris Mason 	u32 this_item_size;
2525db94535dSChris Mason 	u32 old_left_item_size;
2526be0e5c09SChris Mason 
252734a38218SChris Mason 	if (empty)
252899d8f83cSChris Mason 		nr = min(right_nritems, max_slot);
252934a38218SChris Mason 	else
253099d8f83cSChris Mason 		nr = min(right_nritems - 1, max_slot);
253134a38218SChris Mason 
253234a38218SChris Mason 	for (i = 0; i < nr; i++) {
25335f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2534db94535dSChris Mason 
253531840ae1SZheng Yan 		if (!empty && push_items > 0) {
253631840ae1SZheng Yan 			if (path->slots[0] < i)
253731840ae1SZheng Yan 				break;
253831840ae1SZheng Yan 			if (path->slots[0] == i) {
253931840ae1SZheng Yan 				int space = btrfs_leaf_free_space(root, right);
254031840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
254131840ae1SZheng Yan 					break;
254231840ae1SZheng Yan 			}
254331840ae1SZheng Yan 		}
254431840ae1SZheng Yan 
2545be0e5c09SChris Mason 		if (path->slots[0] == i)
254687b29b20SYan Zheng 			push_space += data_size;
2547db94535dSChris Mason 
2548db94535dSChris Mason 		this_item_size = btrfs_item_size(right, item);
2549db94535dSChris Mason 		if (this_item_size + sizeof(*item) + push_space > free_space)
2550be0e5c09SChris Mason 			break;
2551db94535dSChris Mason 
2552be0e5c09SChris Mason 		push_items++;
2553db94535dSChris Mason 		push_space += this_item_size + sizeof(*item);
2554be0e5c09SChris Mason 	}
2555db94535dSChris Mason 
2556be0e5c09SChris Mason 	if (push_items == 0) {
2557925baeddSChris Mason 		ret = 1;
2558925baeddSChris Mason 		goto out;
2559be0e5c09SChris Mason 	}
256034a38218SChris Mason 	if (!empty && push_items == btrfs_header_nritems(right))
2561a429e513SChris Mason 		WARN_ON(1);
25625f39d397SChris Mason 
2563be0e5c09SChris Mason 	/* push data from right to left */
25645f39d397SChris Mason 	copy_extent_buffer(left, right,
25655f39d397SChris Mason 			   btrfs_item_nr_offset(btrfs_header_nritems(left)),
25665f39d397SChris Mason 			   btrfs_item_nr_offset(0),
25675f39d397SChris Mason 			   push_items * sizeof(struct btrfs_item));
25685f39d397SChris Mason 
2569123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root) -
25705f39d397SChris Mason 		     btrfs_item_offset_nr(right, push_items - 1);
25715f39d397SChris Mason 
25725f39d397SChris Mason 	copy_extent_buffer(left, right, btrfs_leaf_data(left) +
2573d6025579SChris Mason 		     leaf_data_end(root, left) - push_space,
2574123abc88SChris Mason 		     btrfs_leaf_data(right) +
25755f39d397SChris Mason 		     btrfs_item_offset_nr(right, push_items - 1),
2576be0e5c09SChris Mason 		     push_space);
25775f39d397SChris Mason 	old_left_nritems = btrfs_header_nritems(left);
257887b29b20SYan Zheng 	BUG_ON(old_left_nritems <= 0);
2579eb60ceacSChris Mason 
2580db94535dSChris Mason 	old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
2581be0e5c09SChris Mason 	for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
25825f39d397SChris Mason 		u32 ioff;
2583db94535dSChris Mason 
25845f39d397SChris Mason 		item = btrfs_item_nr(left, i);
2585db94535dSChris Mason 
25865f39d397SChris Mason 		ioff = btrfs_item_offset(left, item);
25875f39d397SChris Mason 		btrfs_set_item_offset(left, item,
2588db94535dSChris Mason 		      ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
2589be0e5c09SChris Mason 	}
25905f39d397SChris Mason 	btrfs_set_header_nritems(left, old_left_nritems + push_items);
2591be0e5c09SChris Mason 
2592be0e5c09SChris Mason 	/* fixup right node */
259334a38218SChris Mason 	if (push_items > right_nritems) {
2594d397712bSChris Mason 		printk(KERN_CRIT "push items %d nr %u\n", push_items,
2595d397712bSChris Mason 		       right_nritems);
259634a38218SChris Mason 		WARN_ON(1);
259734a38218SChris Mason 	}
259834a38218SChris Mason 
259934a38218SChris Mason 	if (push_items < right_nritems) {
26005f39d397SChris Mason 		push_space = btrfs_item_offset_nr(right, push_items - 1) -
2601123abc88SChris Mason 						  leaf_data_end(root, right);
26025f39d397SChris Mason 		memmove_extent_buffer(right, btrfs_leaf_data(right) +
2603d6025579SChris Mason 				      BTRFS_LEAF_DATA_SIZE(root) - push_space,
2604d6025579SChris Mason 				      btrfs_leaf_data(right) +
2605123abc88SChris Mason 				      leaf_data_end(root, right), push_space);
26065f39d397SChris Mason 
26075f39d397SChris Mason 		memmove_extent_buffer(right, btrfs_item_nr_offset(0),
26085f39d397SChris Mason 			      btrfs_item_nr_offset(push_items),
26095f39d397SChris Mason 			     (btrfs_header_nritems(right) - push_items) *
26100783fcfcSChris Mason 			     sizeof(struct btrfs_item));
261134a38218SChris Mason 	}
2612eef1c494SYan 	right_nritems -= push_items;
2613eef1c494SYan 	btrfs_set_header_nritems(right, right_nritems);
2614123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root);
26155f39d397SChris Mason 	for (i = 0; i < right_nritems; i++) {
26165f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2617db94535dSChris Mason 
2618db94535dSChris Mason 		push_space = push_space - btrfs_item_size(right, item);
2619db94535dSChris Mason 		btrfs_set_item_offset(right, item, push_space);
2620db94535dSChris Mason 	}
2621eb60ceacSChris Mason 
26225f39d397SChris Mason 	btrfs_mark_buffer_dirty(left);
262334a38218SChris Mason 	if (right_nritems)
26245f39d397SChris Mason 		btrfs_mark_buffer_dirty(right);
2625f0486c68SYan, Zheng 	else
2626f0486c68SYan, Zheng 		clean_tree_block(trans, root, right);
2627098f59c2SChris Mason 
26285f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
26295f39d397SChris Mason 	wret = fixup_low_keys(trans, root, path, &disk_key, 1);
2630aa5d6bedSChris Mason 	if (wret)
2631aa5d6bedSChris Mason 		ret = wret;
2632be0e5c09SChris Mason 
2633be0e5c09SChris Mason 	/* then fixup the leaf pointer in the path */
2634be0e5c09SChris Mason 	if (path->slots[0] < push_items) {
2635be0e5c09SChris Mason 		path->slots[0] += old_left_nritems;
2636925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
26375f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
26385f39d397SChris Mason 		path->nodes[0] = left;
2639be0e5c09SChris Mason 		path->slots[1] -= 1;
2640be0e5c09SChris Mason 	} else {
2641925baeddSChris Mason 		btrfs_tree_unlock(left);
26425f39d397SChris Mason 		free_extent_buffer(left);
2643be0e5c09SChris Mason 		path->slots[0] -= push_items;
2644be0e5c09SChris Mason 	}
2645eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
2646aa5d6bedSChris Mason 	return ret;
2647925baeddSChris Mason out:
2648925baeddSChris Mason 	btrfs_tree_unlock(left);
2649925baeddSChris Mason 	free_extent_buffer(left);
2650925baeddSChris Mason 	return ret;
2651be0e5c09SChris Mason }
2652be0e5c09SChris Mason 
265374123bd7SChris Mason /*
265444871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
265544871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
265699d8f83cSChris Mason  *
265799d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
265899d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us push all the
265999d8f83cSChris Mason  * items
266044871b1bSChris Mason  */
266144871b1bSChris Mason static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
266299d8f83cSChris Mason 			  *root, struct btrfs_path *path, int min_data_size,
266399d8f83cSChris Mason 			  int data_size, int empty, u32 max_slot)
266444871b1bSChris Mason {
266544871b1bSChris Mason 	struct extent_buffer *right = path->nodes[0];
266644871b1bSChris Mason 	struct extent_buffer *left;
266744871b1bSChris Mason 	int slot;
266844871b1bSChris Mason 	int free_space;
266944871b1bSChris Mason 	u32 right_nritems;
267044871b1bSChris Mason 	int ret = 0;
267144871b1bSChris Mason 
267244871b1bSChris Mason 	slot = path->slots[1];
267344871b1bSChris Mason 	if (slot == 0)
267444871b1bSChris Mason 		return 1;
267544871b1bSChris Mason 	if (!path->nodes[1])
267644871b1bSChris Mason 		return 1;
267744871b1bSChris Mason 
267844871b1bSChris Mason 	right_nritems = btrfs_header_nritems(right);
267944871b1bSChris Mason 	if (right_nritems == 0)
268044871b1bSChris Mason 		return 1;
268144871b1bSChris Mason 
268244871b1bSChris Mason 	btrfs_assert_tree_locked(path->nodes[1]);
268344871b1bSChris Mason 
268444871b1bSChris Mason 	left = read_node_slot(root, path->nodes[1], slot - 1);
268591ca338dSTsutomu Itoh 	if (left == NULL)
268691ca338dSTsutomu Itoh 		return 1;
268791ca338dSTsutomu Itoh 
268844871b1bSChris Mason 	btrfs_tree_lock(left);
268944871b1bSChris Mason 	btrfs_set_lock_blocking(left);
269044871b1bSChris Mason 
269144871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, left);
269244871b1bSChris Mason 	if (free_space < data_size) {
269344871b1bSChris Mason 		ret = 1;
269444871b1bSChris Mason 		goto out;
269544871b1bSChris Mason 	}
269644871b1bSChris Mason 
269744871b1bSChris Mason 	/* cow and double check */
269844871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, left,
269944871b1bSChris Mason 			      path->nodes[1], slot - 1, &left);
270044871b1bSChris Mason 	if (ret) {
270144871b1bSChris Mason 		/* we hit -ENOSPC, but it isn't fatal here */
270244871b1bSChris Mason 		ret = 1;
270344871b1bSChris Mason 		goto out;
270444871b1bSChris Mason 	}
270544871b1bSChris Mason 
270644871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, left);
270744871b1bSChris Mason 	if (free_space < data_size) {
270844871b1bSChris Mason 		ret = 1;
270944871b1bSChris Mason 		goto out;
271044871b1bSChris Mason 	}
271144871b1bSChris Mason 
271299d8f83cSChris Mason 	return __push_leaf_left(trans, root, path, min_data_size,
271399d8f83cSChris Mason 			       empty, left, free_space, right_nritems,
271499d8f83cSChris Mason 			       max_slot);
271544871b1bSChris Mason out:
271644871b1bSChris Mason 	btrfs_tree_unlock(left);
271744871b1bSChris Mason 	free_extent_buffer(left);
271844871b1bSChris Mason 	return ret;
271944871b1bSChris Mason }
272044871b1bSChris Mason 
272144871b1bSChris Mason /*
272274123bd7SChris Mason  * split the path's leaf in two, making sure there is at least data_size
272374123bd7SChris Mason  * available for the resulting leaf level of the path.
2724aa5d6bedSChris Mason  *
2725aa5d6bedSChris Mason  * returns 0 if all went well and < 0 on failure.
272674123bd7SChris Mason  */
272744871b1bSChris Mason static noinline int copy_for_split(struct btrfs_trans_handle *trans,
2728e02119d5SChris Mason 			       struct btrfs_root *root,
272944871b1bSChris Mason 			       struct btrfs_path *path,
273044871b1bSChris Mason 			       struct extent_buffer *l,
273144871b1bSChris Mason 			       struct extent_buffer *right,
273244871b1bSChris Mason 			       int slot, int mid, int nritems)
2733be0e5c09SChris Mason {
2734be0e5c09SChris Mason 	int data_copy_size;
2735be0e5c09SChris Mason 	int rt_data_off;
2736be0e5c09SChris Mason 	int i;
2737d4dbff95SChris Mason 	int ret = 0;
2738aa5d6bedSChris Mason 	int wret;
2739d4dbff95SChris Mason 	struct btrfs_disk_key disk_key;
2740be0e5c09SChris Mason 
27415f39d397SChris Mason 	nritems = nritems - mid;
27425f39d397SChris Mason 	btrfs_set_header_nritems(right, nritems);
27435f39d397SChris Mason 	data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
27445f39d397SChris Mason 
27455f39d397SChris Mason 	copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
27465f39d397SChris Mason 			   btrfs_item_nr_offset(mid),
27475f39d397SChris Mason 			   nritems * sizeof(struct btrfs_item));
27485f39d397SChris Mason 
27495f39d397SChris Mason 	copy_extent_buffer(right, l,
2750d6025579SChris Mason 		     btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2751123abc88SChris Mason 		     data_copy_size, btrfs_leaf_data(l) +
2752123abc88SChris Mason 		     leaf_data_end(root, l), data_copy_size);
275374123bd7SChris Mason 
27545f39d397SChris Mason 	rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
27555f39d397SChris Mason 		      btrfs_item_end_nr(l, mid);
27565f39d397SChris Mason 
27575f39d397SChris Mason 	for (i = 0; i < nritems; i++) {
27585f39d397SChris Mason 		struct btrfs_item *item = btrfs_item_nr(right, i);
2759db94535dSChris Mason 		u32 ioff;
2760db94535dSChris Mason 
2761db94535dSChris Mason 		ioff = btrfs_item_offset(right, item);
27625f39d397SChris Mason 		btrfs_set_item_offset(right, item, ioff + rt_data_off);
27630783fcfcSChris Mason 	}
276474123bd7SChris Mason 
27655f39d397SChris Mason 	btrfs_set_header_nritems(l, mid);
2766aa5d6bedSChris Mason 	ret = 0;
27675f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
2768db94535dSChris Mason 	wret = insert_ptr(trans, root, path, &disk_key, right->start,
2769db94535dSChris Mason 			  path->slots[1] + 1, 1);
2770aa5d6bedSChris Mason 	if (wret)
2771aa5d6bedSChris Mason 		ret = wret;
27725f39d397SChris Mason 
27735f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
27745f39d397SChris Mason 	btrfs_mark_buffer_dirty(l);
2775eb60ceacSChris Mason 	BUG_ON(path->slots[0] != slot);
27765f39d397SChris Mason 
2777be0e5c09SChris Mason 	if (mid <= slot) {
2778925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
27795f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
27805f39d397SChris Mason 		path->nodes[0] = right;
2781be0e5c09SChris Mason 		path->slots[0] -= mid;
2782be0e5c09SChris Mason 		path->slots[1] += 1;
2783925baeddSChris Mason 	} else {
2784925baeddSChris Mason 		btrfs_tree_unlock(right);
27855f39d397SChris Mason 		free_extent_buffer(right);
2786925baeddSChris Mason 	}
27875f39d397SChris Mason 
2788eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
2789d4dbff95SChris Mason 
279044871b1bSChris Mason 	return ret;
279144871b1bSChris Mason }
279244871b1bSChris Mason 
279344871b1bSChris Mason /*
279499d8f83cSChris Mason  * double splits happen when we need to insert a big item in the middle
279599d8f83cSChris Mason  * of a leaf.  A double split can leave us with 3 mostly empty leaves:
279699d8f83cSChris Mason  * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
279799d8f83cSChris Mason  *          A                 B                 C
279899d8f83cSChris Mason  *
279999d8f83cSChris Mason  * We avoid this by trying to push the items on either side of our target
280099d8f83cSChris Mason  * into the adjacent leaves.  If all goes well we can avoid the double split
280199d8f83cSChris Mason  * completely.
280299d8f83cSChris Mason  */
280399d8f83cSChris Mason static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
280499d8f83cSChris Mason 					  struct btrfs_root *root,
280599d8f83cSChris Mason 					  struct btrfs_path *path,
280699d8f83cSChris Mason 					  int data_size)
280799d8f83cSChris Mason {
280899d8f83cSChris Mason 	int ret;
280999d8f83cSChris Mason 	int progress = 0;
281099d8f83cSChris Mason 	int slot;
281199d8f83cSChris Mason 	u32 nritems;
281299d8f83cSChris Mason 
281399d8f83cSChris Mason 	slot = path->slots[0];
281499d8f83cSChris Mason 
281599d8f83cSChris Mason 	/*
281699d8f83cSChris Mason 	 * try to push all the items after our slot into the
281799d8f83cSChris Mason 	 * right leaf
281899d8f83cSChris Mason 	 */
281999d8f83cSChris Mason 	ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
282099d8f83cSChris Mason 	if (ret < 0)
282199d8f83cSChris Mason 		return ret;
282299d8f83cSChris Mason 
282399d8f83cSChris Mason 	if (ret == 0)
282499d8f83cSChris Mason 		progress++;
282599d8f83cSChris Mason 
282699d8f83cSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
282799d8f83cSChris Mason 	/*
282899d8f83cSChris Mason 	 * our goal is to get our slot at the start or end of a leaf.  If
282999d8f83cSChris Mason 	 * we've done so we're done
283099d8f83cSChris Mason 	 */
283199d8f83cSChris Mason 	if (path->slots[0] == 0 || path->slots[0] == nritems)
283299d8f83cSChris Mason 		return 0;
283399d8f83cSChris Mason 
283499d8f83cSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
283599d8f83cSChris Mason 		return 0;
283699d8f83cSChris Mason 
283799d8f83cSChris Mason 	/* try to push all the items before our slot into the next leaf */
283899d8f83cSChris Mason 	slot = path->slots[0];
283999d8f83cSChris Mason 	ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
284099d8f83cSChris Mason 	if (ret < 0)
284199d8f83cSChris Mason 		return ret;
284299d8f83cSChris Mason 
284399d8f83cSChris Mason 	if (ret == 0)
284499d8f83cSChris Mason 		progress++;
284599d8f83cSChris Mason 
284699d8f83cSChris Mason 	if (progress)
284799d8f83cSChris Mason 		return 0;
284899d8f83cSChris Mason 	return 1;
284999d8f83cSChris Mason }
285099d8f83cSChris Mason 
285199d8f83cSChris Mason /*
285244871b1bSChris Mason  * split the path's leaf in two, making sure there is at least data_size
285344871b1bSChris Mason  * available for the resulting leaf level of the path.
285444871b1bSChris Mason  *
285544871b1bSChris Mason  * returns 0 if all went well and < 0 on failure.
285644871b1bSChris Mason  */
285744871b1bSChris Mason static noinline int split_leaf(struct btrfs_trans_handle *trans,
285844871b1bSChris Mason 			       struct btrfs_root *root,
285944871b1bSChris Mason 			       struct btrfs_key *ins_key,
286044871b1bSChris Mason 			       struct btrfs_path *path, int data_size,
286144871b1bSChris Mason 			       int extend)
286244871b1bSChris Mason {
28635d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
286444871b1bSChris Mason 	struct extent_buffer *l;
286544871b1bSChris Mason 	u32 nritems;
286644871b1bSChris Mason 	int mid;
286744871b1bSChris Mason 	int slot;
286844871b1bSChris Mason 	struct extent_buffer *right;
286944871b1bSChris Mason 	int ret = 0;
287044871b1bSChris Mason 	int wret;
28715d4f98a2SYan Zheng 	int split;
287244871b1bSChris Mason 	int num_doubles = 0;
287399d8f83cSChris Mason 	int tried_avoid_double = 0;
287444871b1bSChris Mason 
2875a5719521SYan, Zheng 	l = path->nodes[0];
2876a5719521SYan, Zheng 	slot = path->slots[0];
2877a5719521SYan, Zheng 	if (extend && data_size + btrfs_item_size_nr(l, slot) +
2878a5719521SYan, Zheng 	    sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2879a5719521SYan, Zheng 		return -EOVERFLOW;
2880a5719521SYan, Zheng 
288144871b1bSChris Mason 	/* first try to make some room by pushing left and right */
288299d8f83cSChris Mason 	if (data_size) {
288399d8f83cSChris Mason 		wret = push_leaf_right(trans, root, path, data_size,
288499d8f83cSChris Mason 				       data_size, 0, 0);
288544871b1bSChris Mason 		if (wret < 0)
288644871b1bSChris Mason 			return wret;
288744871b1bSChris Mason 		if (wret) {
288899d8f83cSChris Mason 			wret = push_leaf_left(trans, root, path, data_size,
288999d8f83cSChris Mason 					      data_size, 0, (u32)-1);
289044871b1bSChris Mason 			if (wret < 0)
289144871b1bSChris Mason 				return wret;
289244871b1bSChris Mason 		}
289344871b1bSChris Mason 		l = path->nodes[0];
289444871b1bSChris Mason 
289544871b1bSChris Mason 		/* did the pushes work? */
289644871b1bSChris Mason 		if (btrfs_leaf_free_space(root, l) >= data_size)
289744871b1bSChris Mason 			return 0;
289844871b1bSChris Mason 	}
289944871b1bSChris Mason 
290044871b1bSChris Mason 	if (!path->nodes[1]) {
290144871b1bSChris Mason 		ret = insert_new_root(trans, root, path, 1);
290244871b1bSChris Mason 		if (ret)
290344871b1bSChris Mason 			return ret;
290444871b1bSChris Mason 	}
290544871b1bSChris Mason again:
29065d4f98a2SYan Zheng 	split = 1;
290744871b1bSChris Mason 	l = path->nodes[0];
290844871b1bSChris Mason 	slot = path->slots[0];
290944871b1bSChris Mason 	nritems = btrfs_header_nritems(l);
291044871b1bSChris Mason 	mid = (nritems + 1) / 2;
291144871b1bSChris Mason 
29125d4f98a2SYan Zheng 	if (mid <= slot) {
29135d4f98a2SYan Zheng 		if (nritems == 1 ||
29145d4f98a2SYan Zheng 		    leaf_space_used(l, mid, nritems - mid) + data_size >
29155d4f98a2SYan Zheng 			BTRFS_LEAF_DATA_SIZE(root)) {
29165d4f98a2SYan Zheng 			if (slot >= nritems) {
29175d4f98a2SYan Zheng 				split = 0;
29185d4f98a2SYan Zheng 			} else {
29195d4f98a2SYan Zheng 				mid = slot;
29205d4f98a2SYan Zheng 				if (mid != nritems &&
29215d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
29225d4f98a2SYan Zheng 				    data_size > BTRFS_LEAF_DATA_SIZE(root)) {
292399d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
292499d8f83cSChris Mason 						goto push_for_double;
29255d4f98a2SYan Zheng 					split = 2;
29265d4f98a2SYan Zheng 				}
29275d4f98a2SYan Zheng 			}
29285d4f98a2SYan Zheng 		}
29295d4f98a2SYan Zheng 	} else {
29305d4f98a2SYan Zheng 		if (leaf_space_used(l, 0, mid) + data_size >
29315d4f98a2SYan Zheng 			BTRFS_LEAF_DATA_SIZE(root)) {
29325d4f98a2SYan Zheng 			if (!extend && data_size && slot == 0) {
29335d4f98a2SYan Zheng 				split = 0;
29345d4f98a2SYan Zheng 			} else if ((extend || !data_size) && slot == 0) {
29355d4f98a2SYan Zheng 				mid = 1;
29365d4f98a2SYan Zheng 			} else {
29375d4f98a2SYan Zheng 				mid = slot;
29385d4f98a2SYan Zheng 				if (mid != nritems &&
29395d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
29405d4f98a2SYan Zheng 				    data_size > BTRFS_LEAF_DATA_SIZE(root)) {
294199d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
294299d8f83cSChris Mason 						goto push_for_double;
29435d4f98a2SYan Zheng 					split = 2 ;
29445d4f98a2SYan Zheng 				}
29455d4f98a2SYan Zheng 			}
29465d4f98a2SYan Zheng 		}
29475d4f98a2SYan Zheng 	}
29485d4f98a2SYan Zheng 
29495d4f98a2SYan Zheng 	if (split == 0)
29505d4f98a2SYan Zheng 		btrfs_cpu_key_to_disk(&disk_key, ins_key);
29515d4f98a2SYan Zheng 	else
29525d4f98a2SYan Zheng 		btrfs_item_key(l, &disk_key, mid);
29535d4f98a2SYan Zheng 
29545d4f98a2SYan Zheng 	right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
295544871b1bSChris Mason 					root->root_key.objectid,
29565d4f98a2SYan Zheng 					&disk_key, 0, l->start, 0);
2957f0486c68SYan, Zheng 	if (IS_ERR(right))
295844871b1bSChris Mason 		return PTR_ERR(right);
2959f0486c68SYan, Zheng 
2960f0486c68SYan, Zheng 	root_add_used(root, root->leafsize);
296144871b1bSChris Mason 
296244871b1bSChris Mason 	memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
296344871b1bSChris Mason 	btrfs_set_header_bytenr(right, right->start);
296444871b1bSChris Mason 	btrfs_set_header_generation(right, trans->transid);
29655d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
296644871b1bSChris Mason 	btrfs_set_header_owner(right, root->root_key.objectid);
296744871b1bSChris Mason 	btrfs_set_header_level(right, 0);
296844871b1bSChris Mason 	write_extent_buffer(right, root->fs_info->fsid,
296944871b1bSChris Mason 			    (unsigned long)btrfs_header_fsid(right),
297044871b1bSChris Mason 			    BTRFS_FSID_SIZE);
297144871b1bSChris Mason 
297244871b1bSChris Mason 	write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
297344871b1bSChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(right),
297444871b1bSChris Mason 			    BTRFS_UUID_SIZE);
297544871b1bSChris Mason 
29765d4f98a2SYan Zheng 	if (split == 0) {
297744871b1bSChris Mason 		if (mid <= slot) {
297844871b1bSChris Mason 			btrfs_set_header_nritems(right, 0);
297944871b1bSChris Mason 			wret = insert_ptr(trans, root, path,
298044871b1bSChris Mason 					  &disk_key, right->start,
298144871b1bSChris Mason 					  path->slots[1] + 1, 1);
298244871b1bSChris Mason 			if (wret)
298344871b1bSChris Mason 				ret = wret;
298444871b1bSChris Mason 
298544871b1bSChris Mason 			btrfs_tree_unlock(path->nodes[0]);
298644871b1bSChris Mason 			free_extent_buffer(path->nodes[0]);
298744871b1bSChris Mason 			path->nodes[0] = right;
298844871b1bSChris Mason 			path->slots[0] = 0;
298944871b1bSChris Mason 			path->slots[1] += 1;
299044871b1bSChris Mason 		} else {
299144871b1bSChris Mason 			btrfs_set_header_nritems(right, 0);
299244871b1bSChris Mason 			wret = insert_ptr(trans, root, path,
299344871b1bSChris Mason 					  &disk_key,
299444871b1bSChris Mason 					  right->start,
299544871b1bSChris Mason 					  path->slots[1], 1);
299644871b1bSChris Mason 			if (wret)
299744871b1bSChris Mason 				ret = wret;
299844871b1bSChris Mason 			btrfs_tree_unlock(path->nodes[0]);
299944871b1bSChris Mason 			free_extent_buffer(path->nodes[0]);
300044871b1bSChris Mason 			path->nodes[0] = right;
300144871b1bSChris Mason 			path->slots[0] = 0;
300244871b1bSChris Mason 			if (path->slots[1] == 0) {
300344871b1bSChris Mason 				wret = fixup_low_keys(trans, root,
300444871b1bSChris Mason 						path, &disk_key, 1);
300544871b1bSChris Mason 				if (wret)
300644871b1bSChris Mason 					ret = wret;
300744871b1bSChris Mason 			}
30085d4f98a2SYan Zheng 		}
300944871b1bSChris Mason 		btrfs_mark_buffer_dirty(right);
301044871b1bSChris Mason 		return ret;
301144871b1bSChris Mason 	}
301244871b1bSChris Mason 
301344871b1bSChris Mason 	ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
301444871b1bSChris Mason 	BUG_ON(ret);
301544871b1bSChris Mason 
30165d4f98a2SYan Zheng 	if (split == 2) {
3017cc0c5538SChris Mason 		BUG_ON(num_doubles != 0);
3018cc0c5538SChris Mason 		num_doubles++;
3019cc0c5538SChris Mason 		goto again;
30203326d1b0SChris Mason 	}
302144871b1bSChris Mason 
3022be0e5c09SChris Mason 	return ret;
302399d8f83cSChris Mason 
302499d8f83cSChris Mason push_for_double:
302599d8f83cSChris Mason 	push_for_double_split(trans, root, path, data_size);
302699d8f83cSChris Mason 	tried_avoid_double = 1;
302799d8f83cSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
302899d8f83cSChris Mason 		return 0;
302999d8f83cSChris Mason 	goto again;
3030be0e5c09SChris Mason }
3031be0e5c09SChris Mason 
3032ad48fd75SYan, Zheng static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3033ad48fd75SYan, Zheng 					 struct btrfs_root *root,
3034ad48fd75SYan, Zheng 					 struct btrfs_path *path, int ins_len)
3035ad48fd75SYan, Zheng {
3036ad48fd75SYan, Zheng 	struct btrfs_key key;
3037ad48fd75SYan, Zheng 	struct extent_buffer *leaf;
3038ad48fd75SYan, Zheng 	struct btrfs_file_extent_item *fi;
3039ad48fd75SYan, Zheng 	u64 extent_len = 0;
3040ad48fd75SYan, Zheng 	u32 item_size;
3041ad48fd75SYan, Zheng 	int ret;
3042ad48fd75SYan, Zheng 
3043ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3044ad48fd75SYan, Zheng 	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3045ad48fd75SYan, Zheng 
3046ad48fd75SYan, Zheng 	BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3047ad48fd75SYan, Zheng 	       key.type != BTRFS_EXTENT_CSUM_KEY);
3048ad48fd75SYan, Zheng 
3049ad48fd75SYan, Zheng 	if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3050ad48fd75SYan, Zheng 		return 0;
3051ad48fd75SYan, Zheng 
3052ad48fd75SYan, Zheng 	item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3053ad48fd75SYan, Zheng 	if (key.type == BTRFS_EXTENT_DATA_KEY) {
3054ad48fd75SYan, Zheng 		fi = btrfs_item_ptr(leaf, path->slots[0],
3055ad48fd75SYan, Zheng 				    struct btrfs_file_extent_item);
3056ad48fd75SYan, Zheng 		extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3057ad48fd75SYan, Zheng 	}
3058b3b4aa74SDavid Sterba 	btrfs_release_path(path);
3059ad48fd75SYan, Zheng 
3060ad48fd75SYan, Zheng 	path->keep_locks = 1;
3061ad48fd75SYan, Zheng 	path->search_for_split = 1;
3062ad48fd75SYan, Zheng 	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3063ad48fd75SYan, Zheng 	path->search_for_split = 0;
3064ad48fd75SYan, Zheng 	if (ret < 0)
3065ad48fd75SYan, Zheng 		goto err;
3066ad48fd75SYan, Zheng 
3067ad48fd75SYan, Zheng 	ret = -EAGAIN;
3068ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3069ad48fd75SYan, Zheng 	/* if our item isn't there or got smaller, return now */
3070ad48fd75SYan, Zheng 	if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3071ad48fd75SYan, Zheng 		goto err;
3072ad48fd75SYan, Zheng 
3073109f6aefSChris Mason 	/* the leaf has  changed, it now has room.  return now */
3074109f6aefSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3075109f6aefSChris Mason 		goto err;
3076109f6aefSChris Mason 
3077ad48fd75SYan, Zheng 	if (key.type == BTRFS_EXTENT_DATA_KEY) {
3078ad48fd75SYan, Zheng 		fi = btrfs_item_ptr(leaf, path->slots[0],
3079ad48fd75SYan, Zheng 				    struct btrfs_file_extent_item);
3080ad48fd75SYan, Zheng 		if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3081ad48fd75SYan, Zheng 			goto err;
3082ad48fd75SYan, Zheng 	}
3083ad48fd75SYan, Zheng 
3084ad48fd75SYan, Zheng 	btrfs_set_path_blocking(path);
3085ad48fd75SYan, Zheng 	ret = split_leaf(trans, root, &key, path, ins_len, 1);
3086f0486c68SYan, Zheng 	if (ret)
3087f0486c68SYan, Zheng 		goto err;
3088ad48fd75SYan, Zheng 
3089ad48fd75SYan, Zheng 	path->keep_locks = 0;
3090ad48fd75SYan, Zheng 	btrfs_unlock_up_safe(path, 1);
3091ad48fd75SYan, Zheng 	return 0;
3092ad48fd75SYan, Zheng err:
3093ad48fd75SYan, Zheng 	path->keep_locks = 0;
3094ad48fd75SYan, Zheng 	return ret;
3095ad48fd75SYan, Zheng }
3096ad48fd75SYan, Zheng 
3097ad48fd75SYan, Zheng static noinline int split_item(struct btrfs_trans_handle *trans,
3098459931ecSChris Mason 			       struct btrfs_root *root,
3099459931ecSChris Mason 			       struct btrfs_path *path,
3100459931ecSChris Mason 			       struct btrfs_key *new_key,
3101459931ecSChris Mason 			       unsigned long split_offset)
3102459931ecSChris Mason {
3103459931ecSChris Mason 	struct extent_buffer *leaf;
3104459931ecSChris Mason 	struct btrfs_item *item;
3105459931ecSChris Mason 	struct btrfs_item *new_item;
3106459931ecSChris Mason 	int slot;
3107ad48fd75SYan, Zheng 	char *buf;
3108459931ecSChris Mason 	u32 nritems;
3109ad48fd75SYan, Zheng 	u32 item_size;
3110459931ecSChris Mason 	u32 orig_offset;
3111459931ecSChris Mason 	struct btrfs_disk_key disk_key;
3112459931ecSChris Mason 
3113459931ecSChris Mason 	leaf = path->nodes[0];
3114b9473439SChris Mason 	BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3115b9473439SChris Mason 
3116b4ce94deSChris Mason 	btrfs_set_path_blocking(path);
3117b4ce94deSChris Mason 
3118459931ecSChris Mason 	item = btrfs_item_nr(leaf, path->slots[0]);
3119459931ecSChris Mason 	orig_offset = btrfs_item_offset(leaf, item);
3120459931ecSChris Mason 	item_size = btrfs_item_size(leaf, item);
3121459931ecSChris Mason 
3122459931ecSChris Mason 	buf = kmalloc(item_size, GFP_NOFS);
3123ad48fd75SYan, Zheng 	if (!buf)
3124ad48fd75SYan, Zheng 		return -ENOMEM;
3125ad48fd75SYan, Zheng 
3126459931ecSChris Mason 	read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3127459931ecSChris Mason 			    path->slots[0]), item_size);
3128ad48fd75SYan, Zheng 
3129459931ecSChris Mason 	slot = path->slots[0] + 1;
3130459931ecSChris Mason 	nritems = btrfs_header_nritems(leaf);
3131459931ecSChris Mason 	if (slot != nritems) {
3132459931ecSChris Mason 		/* shift the items */
3133459931ecSChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3134459931ecSChris Mason 				btrfs_item_nr_offset(slot),
3135459931ecSChris Mason 				(nritems - slot) * sizeof(struct btrfs_item));
3136459931ecSChris Mason 	}
3137459931ecSChris Mason 
3138459931ecSChris Mason 	btrfs_cpu_key_to_disk(&disk_key, new_key);
3139459931ecSChris Mason 	btrfs_set_item_key(leaf, &disk_key, slot);
3140459931ecSChris Mason 
3141459931ecSChris Mason 	new_item = btrfs_item_nr(leaf, slot);
3142459931ecSChris Mason 
3143459931ecSChris Mason 	btrfs_set_item_offset(leaf, new_item, orig_offset);
3144459931ecSChris Mason 	btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3145459931ecSChris Mason 
3146459931ecSChris Mason 	btrfs_set_item_offset(leaf, item,
3147459931ecSChris Mason 			      orig_offset + item_size - split_offset);
3148459931ecSChris Mason 	btrfs_set_item_size(leaf, item, split_offset);
3149459931ecSChris Mason 
3150459931ecSChris Mason 	btrfs_set_header_nritems(leaf, nritems + 1);
3151459931ecSChris Mason 
3152459931ecSChris Mason 	/* write the data for the start of the original item */
3153459931ecSChris Mason 	write_extent_buffer(leaf, buf,
3154459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, path->slots[0]),
3155459931ecSChris Mason 			    split_offset);
3156459931ecSChris Mason 
3157459931ecSChris Mason 	/* write the data for the new item */
3158459931ecSChris Mason 	write_extent_buffer(leaf, buf + split_offset,
3159459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, slot),
3160459931ecSChris Mason 			    item_size - split_offset);
3161459931ecSChris Mason 	btrfs_mark_buffer_dirty(leaf);
3162459931ecSChris Mason 
3163ad48fd75SYan, Zheng 	BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
3164459931ecSChris Mason 	kfree(buf);
3165ad48fd75SYan, Zheng 	return 0;
3166ad48fd75SYan, Zheng }
3167ad48fd75SYan, Zheng 
3168ad48fd75SYan, Zheng /*
3169ad48fd75SYan, Zheng  * This function splits a single item into two items,
3170ad48fd75SYan, Zheng  * giving 'new_key' to the new item and splitting the
3171ad48fd75SYan, Zheng  * old one at split_offset (from the start of the item).
3172ad48fd75SYan, Zheng  *
3173ad48fd75SYan, Zheng  * The path may be released by this operation.  After
3174ad48fd75SYan, Zheng  * the split, the path is pointing to the old item.  The
3175ad48fd75SYan, Zheng  * new item is going to be in the same node as the old one.
3176ad48fd75SYan, Zheng  *
3177ad48fd75SYan, Zheng  * Note, the item being split must be smaller enough to live alone on
3178ad48fd75SYan, Zheng  * a tree block with room for one extra struct btrfs_item
3179ad48fd75SYan, Zheng  *
3180ad48fd75SYan, Zheng  * This allows us to split the item in place, keeping a lock on the
3181ad48fd75SYan, Zheng  * leaf the entire time.
3182ad48fd75SYan, Zheng  */
3183ad48fd75SYan, Zheng int btrfs_split_item(struct btrfs_trans_handle *trans,
3184ad48fd75SYan, Zheng 		     struct btrfs_root *root,
3185ad48fd75SYan, Zheng 		     struct btrfs_path *path,
3186ad48fd75SYan, Zheng 		     struct btrfs_key *new_key,
3187ad48fd75SYan, Zheng 		     unsigned long split_offset)
3188ad48fd75SYan, Zheng {
3189ad48fd75SYan, Zheng 	int ret;
3190ad48fd75SYan, Zheng 	ret = setup_leaf_for_split(trans, root, path,
3191ad48fd75SYan, Zheng 				   sizeof(struct btrfs_item));
3192ad48fd75SYan, Zheng 	if (ret)
3193459931ecSChris Mason 		return ret;
3194ad48fd75SYan, Zheng 
3195ad48fd75SYan, Zheng 	ret = split_item(trans, root, path, new_key, split_offset);
3196ad48fd75SYan, Zheng 	return ret;
3197ad48fd75SYan, Zheng }
3198ad48fd75SYan, Zheng 
3199ad48fd75SYan, Zheng /*
3200ad48fd75SYan, Zheng  * This function duplicate a item, giving 'new_key' to the new item.
3201ad48fd75SYan, Zheng  * It guarantees both items live in the same tree leaf and the new item
3202ad48fd75SYan, Zheng  * is contiguous with the original item.
3203ad48fd75SYan, Zheng  *
3204ad48fd75SYan, Zheng  * This allows us to split file extent in place, keeping a lock on the
3205ad48fd75SYan, Zheng  * leaf the entire time.
3206ad48fd75SYan, Zheng  */
3207ad48fd75SYan, Zheng int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3208ad48fd75SYan, Zheng 			 struct btrfs_root *root,
3209ad48fd75SYan, Zheng 			 struct btrfs_path *path,
3210ad48fd75SYan, Zheng 			 struct btrfs_key *new_key)
3211ad48fd75SYan, Zheng {
3212ad48fd75SYan, Zheng 	struct extent_buffer *leaf;
3213ad48fd75SYan, Zheng 	int ret;
3214ad48fd75SYan, Zheng 	u32 item_size;
3215ad48fd75SYan, Zheng 
3216ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3217ad48fd75SYan, Zheng 	item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3218ad48fd75SYan, Zheng 	ret = setup_leaf_for_split(trans, root, path,
3219ad48fd75SYan, Zheng 				   item_size + sizeof(struct btrfs_item));
3220ad48fd75SYan, Zheng 	if (ret)
3221ad48fd75SYan, Zheng 		return ret;
3222ad48fd75SYan, Zheng 
3223ad48fd75SYan, Zheng 	path->slots[0]++;
3224ad48fd75SYan, Zheng 	ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3225ad48fd75SYan, Zheng 				     item_size, item_size +
3226ad48fd75SYan, Zheng 				     sizeof(struct btrfs_item), 1);
3227ad48fd75SYan, Zheng 	BUG_ON(ret);
3228ad48fd75SYan, Zheng 
3229ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3230ad48fd75SYan, Zheng 	memcpy_extent_buffer(leaf,
3231ad48fd75SYan, Zheng 			     btrfs_item_ptr_offset(leaf, path->slots[0]),
3232ad48fd75SYan, Zheng 			     btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3233ad48fd75SYan, Zheng 			     item_size);
3234ad48fd75SYan, Zheng 	return 0;
3235459931ecSChris Mason }
3236459931ecSChris Mason 
3237459931ecSChris Mason /*
3238d352ac68SChris Mason  * make the item pointed to by the path smaller.  new_size indicates
3239d352ac68SChris Mason  * how small to make it, and from_end tells us if we just chop bytes
3240d352ac68SChris Mason  * off the end of the item or if we shift the item to chop bytes off
3241d352ac68SChris Mason  * the front.
3242d352ac68SChris Mason  */
3243b18c6685SChris Mason int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3244b18c6685SChris Mason 			struct btrfs_root *root,
3245b18c6685SChris Mason 			struct btrfs_path *path,
3246179e29e4SChris Mason 			u32 new_size, int from_end)
3247b18c6685SChris Mason {
3248b18c6685SChris Mason 	int slot;
32495f39d397SChris Mason 	struct extent_buffer *leaf;
32505f39d397SChris Mason 	struct btrfs_item *item;
3251b18c6685SChris Mason 	u32 nritems;
3252b18c6685SChris Mason 	unsigned int data_end;
3253b18c6685SChris Mason 	unsigned int old_data_start;
3254b18c6685SChris Mason 	unsigned int old_size;
3255b18c6685SChris Mason 	unsigned int size_diff;
3256b18c6685SChris Mason 	int i;
3257b18c6685SChris Mason 
32585f39d397SChris Mason 	leaf = path->nodes[0];
3259179e29e4SChris Mason 	slot = path->slots[0];
3260179e29e4SChris Mason 
3261179e29e4SChris Mason 	old_size = btrfs_item_size_nr(leaf, slot);
3262179e29e4SChris Mason 	if (old_size == new_size)
3263179e29e4SChris Mason 		return 0;
3264b18c6685SChris Mason 
32655f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3266b18c6685SChris Mason 	data_end = leaf_data_end(root, leaf);
3267b18c6685SChris Mason 
32685f39d397SChris Mason 	old_data_start = btrfs_item_offset_nr(leaf, slot);
3269179e29e4SChris Mason 
3270b18c6685SChris Mason 	size_diff = old_size - new_size;
3271b18c6685SChris Mason 
3272b18c6685SChris Mason 	BUG_ON(slot < 0);
3273b18c6685SChris Mason 	BUG_ON(slot >= nritems);
3274b18c6685SChris Mason 
3275b18c6685SChris Mason 	/*
3276b18c6685SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3277b18c6685SChris Mason 	 */
3278b18c6685SChris Mason 	/* first correct the data pointers */
3279b18c6685SChris Mason 	for (i = slot; i < nritems; i++) {
32805f39d397SChris Mason 		u32 ioff;
32815f39d397SChris Mason 		item = btrfs_item_nr(leaf, i);
3282db94535dSChris Mason 
32835f39d397SChris Mason 		ioff = btrfs_item_offset(leaf, item);
32845f39d397SChris Mason 		btrfs_set_item_offset(leaf, item, ioff + size_diff);
3285b18c6685SChris Mason 	}
3286db94535dSChris Mason 
3287b18c6685SChris Mason 	/* shift the data */
3288179e29e4SChris Mason 	if (from_end) {
32895f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3290b18c6685SChris Mason 			      data_end + size_diff, btrfs_leaf_data(leaf) +
3291b18c6685SChris Mason 			      data_end, old_data_start + new_size - data_end);
3292179e29e4SChris Mason 	} else {
3293179e29e4SChris Mason 		struct btrfs_disk_key disk_key;
3294179e29e4SChris Mason 		u64 offset;
3295179e29e4SChris Mason 
3296179e29e4SChris Mason 		btrfs_item_key(leaf, &disk_key, slot);
3297179e29e4SChris Mason 
3298179e29e4SChris Mason 		if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3299179e29e4SChris Mason 			unsigned long ptr;
3300179e29e4SChris Mason 			struct btrfs_file_extent_item *fi;
3301179e29e4SChris Mason 
3302179e29e4SChris Mason 			fi = btrfs_item_ptr(leaf, slot,
3303179e29e4SChris Mason 					    struct btrfs_file_extent_item);
3304179e29e4SChris Mason 			fi = (struct btrfs_file_extent_item *)(
3305179e29e4SChris Mason 			     (unsigned long)fi - size_diff);
3306179e29e4SChris Mason 
3307179e29e4SChris Mason 			if (btrfs_file_extent_type(leaf, fi) ==
3308179e29e4SChris Mason 			    BTRFS_FILE_EXTENT_INLINE) {
3309179e29e4SChris Mason 				ptr = btrfs_item_ptr_offset(leaf, slot);
3310179e29e4SChris Mason 				memmove_extent_buffer(leaf, ptr,
3311179e29e4SChris Mason 				      (unsigned long)fi,
3312179e29e4SChris Mason 				      offsetof(struct btrfs_file_extent_item,
3313179e29e4SChris Mason 						 disk_bytenr));
3314179e29e4SChris Mason 			}
3315179e29e4SChris Mason 		}
3316179e29e4SChris Mason 
3317179e29e4SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3318179e29e4SChris Mason 			      data_end + size_diff, btrfs_leaf_data(leaf) +
3319179e29e4SChris Mason 			      data_end, old_data_start - data_end);
3320179e29e4SChris Mason 
3321179e29e4SChris Mason 		offset = btrfs_disk_key_offset(&disk_key);
3322179e29e4SChris Mason 		btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3323179e29e4SChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot);
3324179e29e4SChris Mason 		if (slot == 0)
3325179e29e4SChris Mason 			fixup_low_keys(trans, root, path, &disk_key, 1);
3326179e29e4SChris Mason 	}
33275f39d397SChris Mason 
33285f39d397SChris Mason 	item = btrfs_item_nr(leaf, slot);
33295f39d397SChris Mason 	btrfs_set_item_size(leaf, item, new_size);
33305f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
3331b18c6685SChris Mason 
33325f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
33335f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
3334b18c6685SChris Mason 		BUG();
33355f39d397SChris Mason 	}
33361cd30799STsutomu Itoh 	return 0;
3337b18c6685SChris Mason }
3338b18c6685SChris Mason 
3339d352ac68SChris Mason /*
3340d352ac68SChris Mason  * make the item pointed to by the path bigger, data_size is the new size.
3341d352ac68SChris Mason  */
33425f39d397SChris Mason int btrfs_extend_item(struct btrfs_trans_handle *trans,
33435f39d397SChris Mason 		      struct btrfs_root *root, struct btrfs_path *path,
33445f39d397SChris Mason 		      u32 data_size)
33456567e837SChris Mason {
33466567e837SChris Mason 	int slot;
33475f39d397SChris Mason 	struct extent_buffer *leaf;
33485f39d397SChris Mason 	struct btrfs_item *item;
33496567e837SChris Mason 	u32 nritems;
33506567e837SChris Mason 	unsigned int data_end;
33516567e837SChris Mason 	unsigned int old_data;
33526567e837SChris Mason 	unsigned int old_size;
33536567e837SChris Mason 	int i;
33546567e837SChris Mason 
33555f39d397SChris Mason 	leaf = path->nodes[0];
33566567e837SChris Mason 
33575f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
33586567e837SChris Mason 	data_end = leaf_data_end(root, leaf);
33596567e837SChris Mason 
33605f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < data_size) {
33615f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
33626567e837SChris Mason 		BUG();
33635f39d397SChris Mason 	}
33646567e837SChris Mason 	slot = path->slots[0];
33655f39d397SChris Mason 	old_data = btrfs_item_end_nr(leaf, slot);
33666567e837SChris Mason 
33676567e837SChris Mason 	BUG_ON(slot < 0);
33683326d1b0SChris Mason 	if (slot >= nritems) {
33693326d1b0SChris Mason 		btrfs_print_leaf(root, leaf);
3370d397712bSChris Mason 		printk(KERN_CRIT "slot %d too large, nritems %d\n",
3371d397712bSChris Mason 		       slot, nritems);
33723326d1b0SChris Mason 		BUG_ON(1);
33733326d1b0SChris Mason 	}
33746567e837SChris Mason 
33756567e837SChris Mason 	/*
33766567e837SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
33776567e837SChris Mason 	 */
33786567e837SChris Mason 	/* first correct the data pointers */
33796567e837SChris Mason 	for (i = slot; i < nritems; i++) {
33805f39d397SChris Mason 		u32 ioff;
33815f39d397SChris Mason 		item = btrfs_item_nr(leaf, i);
3382db94535dSChris Mason 
33835f39d397SChris Mason 		ioff = btrfs_item_offset(leaf, item);
33845f39d397SChris Mason 		btrfs_set_item_offset(leaf, item, ioff - data_size);
33856567e837SChris Mason 	}
33865f39d397SChris Mason 
33876567e837SChris Mason 	/* shift the data */
33885f39d397SChris Mason 	memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
33896567e837SChris Mason 		      data_end - data_size, btrfs_leaf_data(leaf) +
33906567e837SChris Mason 		      data_end, old_data - data_end);
33915f39d397SChris Mason 
33926567e837SChris Mason 	data_end = old_data;
33935f39d397SChris Mason 	old_size = btrfs_item_size_nr(leaf, slot);
33945f39d397SChris Mason 	item = btrfs_item_nr(leaf, slot);
33955f39d397SChris Mason 	btrfs_set_item_size(leaf, item, old_size + data_size);
33965f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
33976567e837SChris Mason 
33985f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
33995f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
34006567e837SChris Mason 		BUG();
34015f39d397SChris Mason 	}
34021cd30799STsutomu Itoh 	return 0;
34036567e837SChris Mason }
34046567e837SChris Mason 
340574123bd7SChris Mason /*
3406d352ac68SChris Mason  * Given a key and some data, insert items into the tree.
340774123bd7SChris Mason  * This does all the path init required, making room in the tree if needed.
3408f3465ca4SJosef Bacik  * Returns the number of keys that were inserted.
3409f3465ca4SJosef Bacik  */
3410f3465ca4SJosef Bacik int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3411f3465ca4SJosef Bacik 			    struct btrfs_root *root,
3412f3465ca4SJosef Bacik 			    struct btrfs_path *path,
3413f3465ca4SJosef Bacik 			    struct btrfs_key *cpu_key, u32 *data_size,
3414f3465ca4SJosef Bacik 			    int nr)
3415f3465ca4SJosef Bacik {
3416f3465ca4SJosef Bacik 	struct extent_buffer *leaf;
3417f3465ca4SJosef Bacik 	struct btrfs_item *item;
3418f3465ca4SJosef Bacik 	int ret = 0;
3419f3465ca4SJosef Bacik 	int slot;
3420f3465ca4SJosef Bacik 	int i;
3421f3465ca4SJosef Bacik 	u32 nritems;
3422f3465ca4SJosef Bacik 	u32 total_data = 0;
3423f3465ca4SJosef Bacik 	u32 total_size = 0;
3424f3465ca4SJosef Bacik 	unsigned int data_end;
3425f3465ca4SJosef Bacik 	struct btrfs_disk_key disk_key;
3426f3465ca4SJosef Bacik 	struct btrfs_key found_key;
3427f3465ca4SJosef Bacik 
342887b29b20SYan Zheng 	for (i = 0; i < nr; i++) {
342987b29b20SYan Zheng 		if (total_size + data_size[i] + sizeof(struct btrfs_item) >
343087b29b20SYan Zheng 		    BTRFS_LEAF_DATA_SIZE(root)) {
343187b29b20SYan Zheng 			break;
343287b29b20SYan Zheng 			nr = i;
343387b29b20SYan Zheng 		}
3434f3465ca4SJosef Bacik 		total_data += data_size[i];
343587b29b20SYan Zheng 		total_size += data_size[i] + sizeof(struct btrfs_item);
343687b29b20SYan Zheng 	}
343787b29b20SYan Zheng 	BUG_ON(nr == 0);
3438f3465ca4SJosef Bacik 
3439f3465ca4SJosef Bacik 	ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3440f3465ca4SJosef Bacik 	if (ret == 0)
3441f3465ca4SJosef Bacik 		return -EEXIST;
3442f3465ca4SJosef Bacik 	if (ret < 0)
3443f3465ca4SJosef Bacik 		goto out;
3444f3465ca4SJosef Bacik 
3445f3465ca4SJosef Bacik 	leaf = path->nodes[0];
3446f3465ca4SJosef Bacik 
3447f3465ca4SJosef Bacik 	nritems = btrfs_header_nritems(leaf);
3448f3465ca4SJosef Bacik 	data_end = leaf_data_end(root, leaf);
3449f3465ca4SJosef Bacik 
3450f3465ca4SJosef Bacik 	if (btrfs_leaf_free_space(root, leaf) < total_size) {
3451f3465ca4SJosef Bacik 		for (i = nr; i >= 0; i--) {
3452f3465ca4SJosef Bacik 			total_data -= data_size[i];
3453f3465ca4SJosef Bacik 			total_size -= data_size[i] + sizeof(struct btrfs_item);
3454f3465ca4SJosef Bacik 			if (total_size < btrfs_leaf_free_space(root, leaf))
3455f3465ca4SJosef Bacik 				break;
3456f3465ca4SJosef Bacik 		}
3457f3465ca4SJosef Bacik 		nr = i;
3458f3465ca4SJosef Bacik 	}
3459f3465ca4SJosef Bacik 
3460f3465ca4SJosef Bacik 	slot = path->slots[0];
3461f3465ca4SJosef Bacik 	BUG_ON(slot < 0);
3462f3465ca4SJosef Bacik 
3463f3465ca4SJosef Bacik 	if (slot != nritems) {
3464f3465ca4SJosef Bacik 		unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3465f3465ca4SJosef Bacik 
3466f3465ca4SJosef Bacik 		item = btrfs_item_nr(leaf, slot);
3467f3465ca4SJosef Bacik 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
3468f3465ca4SJosef Bacik 
3469f3465ca4SJosef Bacik 		/* figure out how many keys we can insert in here */
3470f3465ca4SJosef Bacik 		total_data = data_size[0];
3471f3465ca4SJosef Bacik 		for (i = 1; i < nr; i++) {
34725d4f98a2SYan Zheng 			if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3473f3465ca4SJosef Bacik 				break;
3474f3465ca4SJosef Bacik 			total_data += data_size[i];
3475f3465ca4SJosef Bacik 		}
3476f3465ca4SJosef Bacik 		nr = i;
3477f3465ca4SJosef Bacik 
3478f3465ca4SJosef Bacik 		if (old_data < data_end) {
3479f3465ca4SJosef Bacik 			btrfs_print_leaf(root, leaf);
3480d397712bSChris Mason 			printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3481f3465ca4SJosef Bacik 			       slot, old_data, data_end);
3482f3465ca4SJosef Bacik 			BUG_ON(1);
3483f3465ca4SJosef Bacik 		}
3484f3465ca4SJosef Bacik 		/*
3485f3465ca4SJosef Bacik 		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3486f3465ca4SJosef Bacik 		 */
3487f3465ca4SJosef Bacik 		/* first correct the data pointers */
3488f3465ca4SJosef Bacik 		for (i = slot; i < nritems; i++) {
3489f3465ca4SJosef Bacik 			u32 ioff;
3490f3465ca4SJosef Bacik 
3491f3465ca4SJosef Bacik 			item = btrfs_item_nr(leaf, i);
3492f3465ca4SJosef Bacik 			ioff = btrfs_item_offset(leaf, item);
3493f3465ca4SJosef Bacik 			btrfs_set_item_offset(leaf, item, ioff - total_data);
3494f3465ca4SJosef Bacik 		}
3495f3465ca4SJosef Bacik 		/* shift the items */
3496f3465ca4SJosef Bacik 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3497f3465ca4SJosef Bacik 			      btrfs_item_nr_offset(slot),
3498f3465ca4SJosef Bacik 			      (nritems - slot) * sizeof(struct btrfs_item));
3499f3465ca4SJosef Bacik 
3500f3465ca4SJosef Bacik 		/* shift the data */
3501f3465ca4SJosef Bacik 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3502f3465ca4SJosef Bacik 			      data_end - total_data, btrfs_leaf_data(leaf) +
3503f3465ca4SJosef Bacik 			      data_end, old_data - data_end);
3504f3465ca4SJosef Bacik 		data_end = old_data;
3505f3465ca4SJosef Bacik 	} else {
3506f3465ca4SJosef Bacik 		/*
3507f3465ca4SJosef Bacik 		 * this sucks but it has to be done, if we are inserting at
3508f3465ca4SJosef Bacik 		 * the end of the leaf only insert 1 of the items, since we
3509f3465ca4SJosef Bacik 		 * have no way of knowing whats on the next leaf and we'd have
3510f3465ca4SJosef Bacik 		 * to drop our current locks to figure it out
3511f3465ca4SJosef Bacik 		 */
3512f3465ca4SJosef Bacik 		nr = 1;
3513f3465ca4SJosef Bacik 	}
3514f3465ca4SJosef Bacik 
3515f3465ca4SJosef Bacik 	/* setup the item for the new data */
3516f3465ca4SJosef Bacik 	for (i = 0; i < nr; i++) {
3517f3465ca4SJosef Bacik 		btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3518f3465ca4SJosef Bacik 		btrfs_set_item_key(leaf, &disk_key, slot + i);
3519f3465ca4SJosef Bacik 		item = btrfs_item_nr(leaf, slot + i);
3520f3465ca4SJosef Bacik 		btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3521f3465ca4SJosef Bacik 		data_end -= data_size[i];
3522f3465ca4SJosef Bacik 		btrfs_set_item_size(leaf, item, data_size[i]);
3523f3465ca4SJosef Bacik 	}
3524f3465ca4SJosef Bacik 	btrfs_set_header_nritems(leaf, nritems + nr);
3525f3465ca4SJosef Bacik 	btrfs_mark_buffer_dirty(leaf);
3526f3465ca4SJosef Bacik 
3527f3465ca4SJosef Bacik 	ret = 0;
3528f3465ca4SJosef Bacik 	if (slot == 0) {
3529f3465ca4SJosef Bacik 		btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3530f3465ca4SJosef Bacik 		ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3531f3465ca4SJosef Bacik 	}
3532f3465ca4SJosef Bacik 
3533f3465ca4SJosef Bacik 	if (btrfs_leaf_free_space(root, leaf) < 0) {
3534f3465ca4SJosef Bacik 		btrfs_print_leaf(root, leaf);
3535f3465ca4SJosef Bacik 		BUG();
3536f3465ca4SJosef Bacik 	}
3537f3465ca4SJosef Bacik out:
3538f3465ca4SJosef Bacik 	if (!ret)
3539f3465ca4SJosef Bacik 		ret = nr;
3540f3465ca4SJosef Bacik 	return ret;
3541f3465ca4SJosef Bacik }
3542f3465ca4SJosef Bacik 
3543f3465ca4SJosef Bacik /*
354444871b1bSChris Mason  * this is a helper for btrfs_insert_empty_items, the main goal here is
354544871b1bSChris Mason  * to save stack depth by doing the bulk of the work in a function
354644871b1bSChris Mason  * that doesn't call btrfs_search_slot
354774123bd7SChris Mason  */
354816cdcec7SMiao Xie int setup_items_for_insert(struct btrfs_trans_handle *trans,
354944871b1bSChris Mason 			   struct btrfs_root *root, struct btrfs_path *path,
35509c58309dSChris Mason 			   struct btrfs_key *cpu_key, u32 *data_size,
355144871b1bSChris Mason 			   u32 total_data, u32 total_size, int nr)
3552be0e5c09SChris Mason {
35535f39d397SChris Mason 	struct btrfs_item *item;
35549c58309dSChris Mason 	int i;
35557518a238SChris Mason 	u32 nritems;
3556be0e5c09SChris Mason 	unsigned int data_end;
3557e2fa7227SChris Mason 	struct btrfs_disk_key disk_key;
355844871b1bSChris Mason 	int ret;
355944871b1bSChris Mason 	struct extent_buffer *leaf;
356044871b1bSChris Mason 	int slot;
3561e2fa7227SChris Mason 
35625f39d397SChris Mason 	leaf = path->nodes[0];
356344871b1bSChris Mason 	slot = path->slots[0];
356474123bd7SChris Mason 
35655f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3566123abc88SChris Mason 	data_end = leaf_data_end(root, leaf);
3567eb60ceacSChris Mason 
3568f25956ccSChris Mason 	if (btrfs_leaf_free_space(root, leaf) < total_size) {
35693326d1b0SChris Mason 		btrfs_print_leaf(root, leaf);
3570d397712bSChris Mason 		printk(KERN_CRIT "not enough freespace need %u have %d\n",
35719c58309dSChris Mason 		       total_size, btrfs_leaf_free_space(root, leaf));
3572be0e5c09SChris Mason 		BUG();
3573d4dbff95SChris Mason 	}
35745f39d397SChris Mason 
3575be0e5c09SChris Mason 	if (slot != nritems) {
35765f39d397SChris Mason 		unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3577be0e5c09SChris Mason 
35785f39d397SChris Mason 		if (old_data < data_end) {
35795f39d397SChris Mason 			btrfs_print_leaf(root, leaf);
3580d397712bSChris Mason 			printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
35815f39d397SChris Mason 			       slot, old_data, data_end);
35825f39d397SChris Mason 			BUG_ON(1);
35835f39d397SChris Mason 		}
3584be0e5c09SChris Mason 		/*
3585be0e5c09SChris Mason 		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3586be0e5c09SChris Mason 		 */
3587be0e5c09SChris Mason 		/* first correct the data pointers */
35880783fcfcSChris Mason 		for (i = slot; i < nritems; i++) {
35895f39d397SChris Mason 			u32 ioff;
3590db94535dSChris Mason 
35915f39d397SChris Mason 			item = btrfs_item_nr(leaf, i);
35925f39d397SChris Mason 			ioff = btrfs_item_offset(leaf, item);
35939c58309dSChris Mason 			btrfs_set_item_offset(leaf, item, ioff - total_data);
35940783fcfcSChris Mason 		}
3595be0e5c09SChris Mason 		/* shift the items */
35969c58309dSChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
35975f39d397SChris Mason 			      btrfs_item_nr_offset(slot),
35980783fcfcSChris Mason 			      (nritems - slot) * sizeof(struct btrfs_item));
3599be0e5c09SChris Mason 
3600be0e5c09SChris Mason 		/* shift the data */
36015f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
36029c58309dSChris Mason 			      data_end - total_data, btrfs_leaf_data(leaf) +
3603be0e5c09SChris Mason 			      data_end, old_data - data_end);
3604be0e5c09SChris Mason 		data_end = old_data;
3605be0e5c09SChris Mason 	}
36065f39d397SChris Mason 
360762e2749eSChris Mason 	/* setup the item for the new data */
36089c58309dSChris Mason 	for (i = 0; i < nr; i++) {
36099c58309dSChris Mason 		btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
36109c58309dSChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot + i);
36119c58309dSChris Mason 		item = btrfs_item_nr(leaf, slot + i);
36129c58309dSChris Mason 		btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
36139c58309dSChris Mason 		data_end -= data_size[i];
36149c58309dSChris Mason 		btrfs_set_item_size(leaf, item, data_size[i]);
36159c58309dSChris Mason 	}
361644871b1bSChris Mason 
36179c58309dSChris Mason 	btrfs_set_header_nritems(leaf, nritems + nr);
3618aa5d6bedSChris Mason 
3619aa5d6bedSChris Mason 	ret = 0;
36205a01a2e3SChris Mason 	if (slot == 0) {
36215a01a2e3SChris Mason 		btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3622e089f05cSChris Mason 		ret = fixup_low_keys(trans, root, path, &disk_key, 1);
36235a01a2e3SChris Mason 	}
3624b9473439SChris Mason 	btrfs_unlock_up_safe(path, 1);
3625b9473439SChris Mason 	btrfs_mark_buffer_dirty(leaf);
3626aa5d6bedSChris Mason 
36275f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
36285f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
3629be0e5c09SChris Mason 		BUG();
36305f39d397SChris Mason 	}
363144871b1bSChris Mason 	return ret;
363244871b1bSChris Mason }
363344871b1bSChris Mason 
363444871b1bSChris Mason /*
363544871b1bSChris Mason  * Given a key and some data, insert items into the tree.
363644871b1bSChris Mason  * This does all the path init required, making room in the tree if needed.
363744871b1bSChris Mason  */
363844871b1bSChris Mason int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
363944871b1bSChris Mason 			    struct btrfs_root *root,
364044871b1bSChris Mason 			    struct btrfs_path *path,
364144871b1bSChris Mason 			    struct btrfs_key *cpu_key, u32 *data_size,
364244871b1bSChris Mason 			    int nr)
364344871b1bSChris Mason {
364444871b1bSChris Mason 	int ret = 0;
364544871b1bSChris Mason 	int slot;
364644871b1bSChris Mason 	int i;
364744871b1bSChris Mason 	u32 total_size = 0;
364844871b1bSChris Mason 	u32 total_data = 0;
364944871b1bSChris Mason 
365044871b1bSChris Mason 	for (i = 0; i < nr; i++)
365144871b1bSChris Mason 		total_data += data_size[i];
365244871b1bSChris Mason 
365344871b1bSChris Mason 	total_size = total_data + (nr * sizeof(struct btrfs_item));
365444871b1bSChris Mason 	ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
365544871b1bSChris Mason 	if (ret == 0)
365644871b1bSChris Mason 		return -EEXIST;
365744871b1bSChris Mason 	if (ret < 0)
365844871b1bSChris Mason 		goto out;
365944871b1bSChris Mason 
366044871b1bSChris Mason 	slot = path->slots[0];
366144871b1bSChris Mason 	BUG_ON(slot < 0);
366244871b1bSChris Mason 
366344871b1bSChris Mason 	ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
366444871b1bSChris Mason 			       total_data, total_size, nr);
366544871b1bSChris Mason 
3666ed2ff2cbSChris Mason out:
366762e2749eSChris Mason 	return ret;
366862e2749eSChris Mason }
366962e2749eSChris Mason 
367062e2749eSChris Mason /*
367162e2749eSChris Mason  * Given a key and some data, insert an item into the tree.
367262e2749eSChris Mason  * This does all the path init required, making room in the tree if needed.
367362e2749eSChris Mason  */
3674e089f05cSChris Mason int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3675e089f05cSChris Mason 		      *root, struct btrfs_key *cpu_key, void *data, u32
3676e089f05cSChris Mason 		      data_size)
367762e2749eSChris Mason {
367862e2749eSChris Mason 	int ret = 0;
36792c90e5d6SChris Mason 	struct btrfs_path *path;
36805f39d397SChris Mason 	struct extent_buffer *leaf;
36815f39d397SChris Mason 	unsigned long ptr;
368262e2749eSChris Mason 
36832c90e5d6SChris Mason 	path = btrfs_alloc_path();
3684db5b493aSTsutomu Itoh 	if (!path)
3685db5b493aSTsutomu Itoh 		return -ENOMEM;
36862c90e5d6SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
368762e2749eSChris Mason 	if (!ret) {
36885f39d397SChris Mason 		leaf = path->nodes[0];
36895f39d397SChris Mason 		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
36905f39d397SChris Mason 		write_extent_buffer(leaf, data, ptr, data_size);
36915f39d397SChris Mason 		btrfs_mark_buffer_dirty(leaf);
369262e2749eSChris Mason 	}
36932c90e5d6SChris Mason 	btrfs_free_path(path);
3694aa5d6bedSChris Mason 	return ret;
3695be0e5c09SChris Mason }
3696be0e5c09SChris Mason 
369774123bd7SChris Mason /*
36985de08d7dSChris Mason  * delete the pointer from a given node.
369974123bd7SChris Mason  *
3700d352ac68SChris Mason  * the tree should have been previously balanced so the deletion does not
3701d352ac68SChris Mason  * empty a node.
370274123bd7SChris Mason  */
3703e089f05cSChris Mason static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3704e089f05cSChris Mason 		   struct btrfs_path *path, int level, int slot)
3705be0e5c09SChris Mason {
37065f39d397SChris Mason 	struct extent_buffer *parent = path->nodes[level];
37077518a238SChris Mason 	u32 nritems;
3708aa5d6bedSChris Mason 	int ret = 0;
3709bb803951SChris Mason 	int wret;
3710be0e5c09SChris Mason 
37115f39d397SChris Mason 	nritems = btrfs_header_nritems(parent);
3712be0e5c09SChris Mason 	if (slot != nritems - 1) {
37135f39d397SChris Mason 		memmove_extent_buffer(parent,
37145f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot),
37155f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot + 1),
3716d6025579SChris Mason 			      sizeof(struct btrfs_key_ptr) *
3717d6025579SChris Mason 			      (nritems - slot - 1));
3718be0e5c09SChris Mason 	}
37197518a238SChris Mason 	nritems--;
37205f39d397SChris Mason 	btrfs_set_header_nritems(parent, nritems);
37217518a238SChris Mason 	if (nritems == 0 && parent == root->node) {
37225f39d397SChris Mason 		BUG_ON(btrfs_header_level(root->node) != 1);
3723eb60ceacSChris Mason 		/* just turn the root into a leaf and break */
37245f39d397SChris Mason 		btrfs_set_header_level(root->node, 0);
3725bb803951SChris Mason 	} else if (slot == 0) {
37265f39d397SChris Mason 		struct btrfs_disk_key disk_key;
37275f39d397SChris Mason 
37285f39d397SChris Mason 		btrfs_node_key(parent, &disk_key, 0);
37295f39d397SChris Mason 		wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
37300f70abe2SChris Mason 		if (wret)
37310f70abe2SChris Mason 			ret = wret;
3732be0e5c09SChris Mason 	}
3733d6025579SChris Mason 	btrfs_mark_buffer_dirty(parent);
3734aa5d6bedSChris Mason 	return ret;
3735be0e5c09SChris Mason }
3736be0e5c09SChris Mason 
373774123bd7SChris Mason /*
3738323ac95bSChris Mason  * a helper function to delete the leaf pointed to by path->slots[1] and
37395d4f98a2SYan Zheng  * path->nodes[1].
3740323ac95bSChris Mason  *
3741323ac95bSChris Mason  * This deletes the pointer in path->nodes[1] and frees the leaf
3742323ac95bSChris Mason  * block extent.  zero is returned if it all worked out, < 0 otherwise.
3743323ac95bSChris Mason  *
3744323ac95bSChris Mason  * The path must have already been setup for deleting the leaf, including
3745323ac95bSChris Mason  * all the proper balancing.  path->nodes[1] must be locked.
3746323ac95bSChris Mason  */
37475d4f98a2SYan Zheng static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3748323ac95bSChris Mason 				   struct btrfs_root *root,
37495d4f98a2SYan Zheng 				   struct btrfs_path *path,
37505d4f98a2SYan Zheng 				   struct extent_buffer *leaf)
3751323ac95bSChris Mason {
3752323ac95bSChris Mason 	int ret;
3753323ac95bSChris Mason 
37545d4f98a2SYan Zheng 	WARN_ON(btrfs_header_generation(leaf) != trans->transid);
3755323ac95bSChris Mason 	ret = del_ptr(trans, root, path, 1, path->slots[1]);
3756323ac95bSChris Mason 	if (ret)
3757323ac95bSChris Mason 		return ret;
3758323ac95bSChris Mason 
37594d081c41SChris Mason 	/*
37604d081c41SChris Mason 	 * btrfs_free_extent is expensive, we want to make sure we
37614d081c41SChris Mason 	 * aren't holding any locks when we call it
37624d081c41SChris Mason 	 */
37634d081c41SChris Mason 	btrfs_unlock_up_safe(path, 0);
37644d081c41SChris Mason 
3765f0486c68SYan, Zheng 	root_sub_used(root, leaf->len);
3766f0486c68SYan, Zheng 
3767f0486c68SYan, Zheng 	btrfs_free_tree_block(trans, root, leaf, 0, 1);
3768f0486c68SYan, Zheng 	return 0;
3769323ac95bSChris Mason }
3770323ac95bSChris Mason /*
377174123bd7SChris Mason  * delete the item at the leaf level in path.  If that empties
377274123bd7SChris Mason  * the leaf, remove it from the tree
377374123bd7SChris Mason  */
377485e21bacSChris Mason int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
377585e21bacSChris Mason 		    struct btrfs_path *path, int slot, int nr)
3776be0e5c09SChris Mason {
37775f39d397SChris Mason 	struct extent_buffer *leaf;
37785f39d397SChris Mason 	struct btrfs_item *item;
377985e21bacSChris Mason 	int last_off;
378085e21bacSChris Mason 	int dsize = 0;
3781aa5d6bedSChris Mason 	int ret = 0;
3782aa5d6bedSChris Mason 	int wret;
378385e21bacSChris Mason 	int i;
37847518a238SChris Mason 	u32 nritems;
3785be0e5c09SChris Mason 
37865f39d397SChris Mason 	leaf = path->nodes[0];
378785e21bacSChris Mason 	last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
378885e21bacSChris Mason 
378985e21bacSChris Mason 	for (i = 0; i < nr; i++)
379085e21bacSChris Mason 		dsize += btrfs_item_size_nr(leaf, slot + i);
379185e21bacSChris Mason 
37925f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3793be0e5c09SChris Mason 
379485e21bacSChris Mason 	if (slot + nr != nritems) {
3795123abc88SChris Mason 		int data_end = leaf_data_end(root, leaf);
37965f39d397SChris Mason 
37975f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3798d6025579SChris Mason 			      data_end + dsize,
3799123abc88SChris Mason 			      btrfs_leaf_data(leaf) + data_end,
380085e21bacSChris Mason 			      last_off - data_end);
38015f39d397SChris Mason 
380285e21bacSChris Mason 		for (i = slot + nr; i < nritems; i++) {
38035f39d397SChris Mason 			u32 ioff;
3804db94535dSChris Mason 
38055f39d397SChris Mason 			item = btrfs_item_nr(leaf, i);
38065f39d397SChris Mason 			ioff = btrfs_item_offset(leaf, item);
38075f39d397SChris Mason 			btrfs_set_item_offset(leaf, item, ioff + dsize);
38080783fcfcSChris Mason 		}
3809db94535dSChris Mason 
38105f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
381185e21bacSChris Mason 			      btrfs_item_nr_offset(slot + nr),
38120783fcfcSChris Mason 			      sizeof(struct btrfs_item) *
381385e21bacSChris Mason 			      (nritems - slot - nr));
3814be0e5c09SChris Mason 	}
381585e21bacSChris Mason 	btrfs_set_header_nritems(leaf, nritems - nr);
381685e21bacSChris Mason 	nritems -= nr;
38175f39d397SChris Mason 
381874123bd7SChris Mason 	/* delete the leaf if we've emptied it */
38197518a238SChris Mason 	if (nritems == 0) {
38205f39d397SChris Mason 		if (leaf == root->node) {
38215f39d397SChris Mason 			btrfs_set_header_level(leaf, 0);
38229a8dd150SChris Mason 		} else {
3823f0486c68SYan, Zheng 			btrfs_set_path_blocking(path);
3824f0486c68SYan, Zheng 			clean_tree_block(trans, root, leaf);
38255d4f98a2SYan Zheng 			ret = btrfs_del_leaf(trans, root, path, leaf);
3826323ac95bSChris Mason 			BUG_ON(ret);
38279a8dd150SChris Mason 		}
3828be0e5c09SChris Mason 	} else {
38297518a238SChris Mason 		int used = leaf_space_used(leaf, 0, nritems);
3830aa5d6bedSChris Mason 		if (slot == 0) {
38315f39d397SChris Mason 			struct btrfs_disk_key disk_key;
38325f39d397SChris Mason 
38335f39d397SChris Mason 			btrfs_item_key(leaf, &disk_key, 0);
3834e089f05cSChris Mason 			wret = fixup_low_keys(trans, root, path,
38355f39d397SChris Mason 					      &disk_key, 1);
3836aa5d6bedSChris Mason 			if (wret)
3837aa5d6bedSChris Mason 				ret = wret;
3838aa5d6bedSChris Mason 		}
3839aa5d6bedSChris Mason 
384074123bd7SChris Mason 		/* delete the leaf if it is mostly empty */
3841d717aa1dSYan Zheng 		if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
3842be0e5c09SChris Mason 			/* push_leaf_left fixes the path.
3843be0e5c09SChris Mason 			 * make sure the path still points to our leaf
3844be0e5c09SChris Mason 			 * for possible call to del_ptr below
3845be0e5c09SChris Mason 			 */
38464920c9acSChris Mason 			slot = path->slots[1];
38475f39d397SChris Mason 			extent_buffer_get(leaf);
38485f39d397SChris Mason 
3849b9473439SChris Mason 			btrfs_set_path_blocking(path);
385099d8f83cSChris Mason 			wret = push_leaf_left(trans, root, path, 1, 1,
385199d8f83cSChris Mason 					      1, (u32)-1);
385254aa1f4dSChris Mason 			if (wret < 0 && wret != -ENOSPC)
3853aa5d6bedSChris Mason 				ret = wret;
38545f39d397SChris Mason 
38555f39d397SChris Mason 			if (path->nodes[0] == leaf &&
38565f39d397SChris Mason 			    btrfs_header_nritems(leaf)) {
385799d8f83cSChris Mason 				wret = push_leaf_right(trans, root, path, 1,
385899d8f83cSChris Mason 						       1, 1, 0);
385954aa1f4dSChris Mason 				if (wret < 0 && wret != -ENOSPC)
3860aa5d6bedSChris Mason 					ret = wret;
3861aa5d6bedSChris Mason 			}
38625f39d397SChris Mason 
38635f39d397SChris Mason 			if (btrfs_header_nritems(leaf) == 0) {
3864323ac95bSChris Mason 				path->slots[1] = slot;
38655d4f98a2SYan Zheng 				ret = btrfs_del_leaf(trans, root, path, leaf);
3866323ac95bSChris Mason 				BUG_ON(ret);
38675f39d397SChris Mason 				free_extent_buffer(leaf);
38685de08d7dSChris Mason 			} else {
3869925baeddSChris Mason 				/* if we're still in the path, make sure
3870925baeddSChris Mason 				 * we're dirty.  Otherwise, one of the
3871925baeddSChris Mason 				 * push_leaf functions must have already
3872925baeddSChris Mason 				 * dirtied this buffer
3873925baeddSChris Mason 				 */
3874925baeddSChris Mason 				if (path->nodes[0] == leaf)
38755f39d397SChris Mason 					btrfs_mark_buffer_dirty(leaf);
38765f39d397SChris Mason 				free_extent_buffer(leaf);
3877be0e5c09SChris Mason 			}
3878d5719762SChris Mason 		} else {
38795f39d397SChris Mason 			btrfs_mark_buffer_dirty(leaf);
3880be0e5c09SChris Mason 		}
38819a8dd150SChris Mason 	}
3882aa5d6bedSChris Mason 	return ret;
38839a8dd150SChris Mason }
38849a8dd150SChris Mason 
388597571fd0SChris Mason /*
3886925baeddSChris Mason  * search the tree again to find a leaf with lesser keys
38877bb86316SChris Mason  * returns 0 if it found something or 1 if there are no lesser leaves.
38887bb86316SChris Mason  * returns < 0 on io errors.
3889d352ac68SChris Mason  *
3890d352ac68SChris Mason  * This may release the path, and so you may lose any locks held at the
3891d352ac68SChris Mason  * time you call it.
38927bb86316SChris Mason  */
38937bb86316SChris Mason int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
38947bb86316SChris Mason {
3895925baeddSChris Mason 	struct btrfs_key key;
3896925baeddSChris Mason 	struct btrfs_disk_key found_key;
3897925baeddSChris Mason 	int ret;
38987bb86316SChris Mason 
3899925baeddSChris Mason 	btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
3900925baeddSChris Mason 
3901925baeddSChris Mason 	if (key.offset > 0)
3902925baeddSChris Mason 		key.offset--;
3903925baeddSChris Mason 	else if (key.type > 0)
3904925baeddSChris Mason 		key.type--;
3905925baeddSChris Mason 	else if (key.objectid > 0)
3906925baeddSChris Mason 		key.objectid--;
3907925baeddSChris Mason 	else
39087bb86316SChris Mason 		return 1;
39097bb86316SChris Mason 
3910b3b4aa74SDavid Sterba 	btrfs_release_path(path);
3911925baeddSChris Mason 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3912925baeddSChris Mason 	if (ret < 0)
3913925baeddSChris Mason 		return ret;
3914925baeddSChris Mason 	btrfs_item_key(path->nodes[0], &found_key, 0);
3915925baeddSChris Mason 	ret = comp_keys(&found_key, &key);
3916925baeddSChris Mason 	if (ret < 0)
39177bb86316SChris Mason 		return 0;
3918925baeddSChris Mason 	return 1;
39197bb86316SChris Mason }
39207bb86316SChris Mason 
39213f157a2fSChris Mason /*
39223f157a2fSChris Mason  * A helper function to walk down the tree starting at min_key, and looking
39233f157a2fSChris Mason  * for nodes or leaves that are either in cache or have a minimum
3924d352ac68SChris Mason  * transaction id.  This is used by the btree defrag code, and tree logging
39253f157a2fSChris Mason  *
39263f157a2fSChris Mason  * This does not cow, but it does stuff the starting key it finds back
39273f157a2fSChris Mason  * into min_key, so you can call btrfs_search_slot with cow=1 on the
39283f157a2fSChris Mason  * key and get a writable path.
39293f157a2fSChris Mason  *
39303f157a2fSChris Mason  * This does lock as it descends, and path->keep_locks should be set
39313f157a2fSChris Mason  * to 1 by the caller.
39323f157a2fSChris Mason  *
39333f157a2fSChris Mason  * This honors path->lowest_level to prevent descent past a given level
39343f157a2fSChris Mason  * of the tree.
39353f157a2fSChris Mason  *
3936d352ac68SChris Mason  * min_trans indicates the oldest transaction that you are interested
3937d352ac68SChris Mason  * in walking through.  Any nodes or leaves older than min_trans are
3938d352ac68SChris Mason  * skipped over (without reading them).
3939d352ac68SChris Mason  *
39403f157a2fSChris Mason  * returns zero if something useful was found, < 0 on error and 1 if there
39413f157a2fSChris Mason  * was nothing in the tree that matched the search criteria.
39423f157a2fSChris Mason  */
39433f157a2fSChris Mason int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
3944e02119d5SChris Mason 			 struct btrfs_key *max_key,
39453f157a2fSChris Mason 			 struct btrfs_path *path, int cache_only,
39463f157a2fSChris Mason 			 u64 min_trans)
39473f157a2fSChris Mason {
39483f157a2fSChris Mason 	struct extent_buffer *cur;
39493f157a2fSChris Mason 	struct btrfs_key found_key;
39503f157a2fSChris Mason 	int slot;
39519652480bSYan 	int sret;
39523f157a2fSChris Mason 	u32 nritems;
39533f157a2fSChris Mason 	int level;
39543f157a2fSChris Mason 	int ret = 1;
39553f157a2fSChris Mason 
3956934d375bSChris Mason 	WARN_ON(!path->keep_locks);
39573f157a2fSChris Mason again:
3958*bd681513SChris Mason 	cur = btrfs_read_lock_root_node(root);
39593f157a2fSChris Mason 	level = btrfs_header_level(cur);
3960e02119d5SChris Mason 	WARN_ON(path->nodes[level]);
39613f157a2fSChris Mason 	path->nodes[level] = cur;
3962*bd681513SChris Mason 	path->locks[level] = BTRFS_READ_LOCK;
39633f157a2fSChris Mason 
39643f157a2fSChris Mason 	if (btrfs_header_generation(cur) < min_trans) {
39653f157a2fSChris Mason 		ret = 1;
39663f157a2fSChris Mason 		goto out;
39673f157a2fSChris Mason 	}
39683f157a2fSChris Mason 	while (1) {
39693f157a2fSChris Mason 		nritems = btrfs_header_nritems(cur);
39703f157a2fSChris Mason 		level = btrfs_header_level(cur);
39719652480bSYan 		sret = bin_search(cur, min_key, level, &slot);
39723f157a2fSChris Mason 
3973323ac95bSChris Mason 		/* at the lowest level, we're done, setup the path and exit */
3974323ac95bSChris Mason 		if (level == path->lowest_level) {
3975e02119d5SChris Mason 			if (slot >= nritems)
3976e02119d5SChris Mason 				goto find_next_key;
39773f157a2fSChris Mason 			ret = 0;
39783f157a2fSChris Mason 			path->slots[level] = slot;
39793f157a2fSChris Mason 			btrfs_item_key_to_cpu(cur, &found_key, slot);
39803f157a2fSChris Mason 			goto out;
39813f157a2fSChris Mason 		}
39829652480bSYan 		if (sret && slot > 0)
39839652480bSYan 			slot--;
39843f157a2fSChris Mason 		/*
39853f157a2fSChris Mason 		 * check this node pointer against the cache_only and
39863f157a2fSChris Mason 		 * min_trans parameters.  If it isn't in cache or is too
39873f157a2fSChris Mason 		 * old, skip to the next one.
39883f157a2fSChris Mason 		 */
39893f157a2fSChris Mason 		while (slot < nritems) {
39903f157a2fSChris Mason 			u64 blockptr;
39913f157a2fSChris Mason 			u64 gen;
39923f157a2fSChris Mason 			struct extent_buffer *tmp;
3993e02119d5SChris Mason 			struct btrfs_disk_key disk_key;
3994e02119d5SChris Mason 
39953f157a2fSChris Mason 			blockptr = btrfs_node_blockptr(cur, slot);
39963f157a2fSChris Mason 			gen = btrfs_node_ptr_generation(cur, slot);
39973f157a2fSChris Mason 			if (gen < min_trans) {
39983f157a2fSChris Mason 				slot++;
39993f157a2fSChris Mason 				continue;
40003f157a2fSChris Mason 			}
40013f157a2fSChris Mason 			if (!cache_only)
40023f157a2fSChris Mason 				break;
40033f157a2fSChris Mason 
4004e02119d5SChris Mason 			if (max_key) {
4005e02119d5SChris Mason 				btrfs_node_key(cur, &disk_key, slot);
4006e02119d5SChris Mason 				if (comp_keys(&disk_key, max_key) >= 0) {
4007e02119d5SChris Mason 					ret = 1;
4008e02119d5SChris Mason 					goto out;
4009e02119d5SChris Mason 				}
4010e02119d5SChris Mason 			}
4011e02119d5SChris Mason 
40123f157a2fSChris Mason 			tmp = btrfs_find_tree_block(root, blockptr,
40133f157a2fSChris Mason 					    btrfs_level_size(root, level - 1));
40143f157a2fSChris Mason 
40153f157a2fSChris Mason 			if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
40163f157a2fSChris Mason 				free_extent_buffer(tmp);
40173f157a2fSChris Mason 				break;
40183f157a2fSChris Mason 			}
40193f157a2fSChris Mason 			if (tmp)
40203f157a2fSChris Mason 				free_extent_buffer(tmp);
40213f157a2fSChris Mason 			slot++;
40223f157a2fSChris Mason 		}
4023e02119d5SChris Mason find_next_key:
40243f157a2fSChris Mason 		/*
40253f157a2fSChris Mason 		 * we didn't find a candidate key in this node, walk forward
40263f157a2fSChris Mason 		 * and find another one
40273f157a2fSChris Mason 		 */
40283f157a2fSChris Mason 		if (slot >= nritems) {
4029e02119d5SChris Mason 			path->slots[level] = slot;
4030b4ce94deSChris Mason 			btrfs_set_path_blocking(path);
4031e02119d5SChris Mason 			sret = btrfs_find_next_key(root, path, min_key, level,
40323f157a2fSChris Mason 						  cache_only, min_trans);
4033e02119d5SChris Mason 			if (sret == 0) {
4034b3b4aa74SDavid Sterba 				btrfs_release_path(path);
40353f157a2fSChris Mason 				goto again;
40363f157a2fSChris Mason 			} else {
40373f157a2fSChris Mason 				goto out;
40383f157a2fSChris Mason 			}
40393f157a2fSChris Mason 		}
40403f157a2fSChris Mason 		/* save our key for returning back */
40413f157a2fSChris Mason 		btrfs_node_key_to_cpu(cur, &found_key, slot);
40423f157a2fSChris Mason 		path->slots[level] = slot;
40433f157a2fSChris Mason 		if (level == path->lowest_level) {
40443f157a2fSChris Mason 			ret = 0;
40453f157a2fSChris Mason 			unlock_up(path, level, 1);
40463f157a2fSChris Mason 			goto out;
40473f157a2fSChris Mason 		}
4048b4ce94deSChris Mason 		btrfs_set_path_blocking(path);
40493f157a2fSChris Mason 		cur = read_node_slot(root, cur, slot);
405097d9a8a4STsutomu Itoh 		BUG_ON(!cur);
40513f157a2fSChris Mason 
4052*bd681513SChris Mason 		btrfs_tree_read_lock(cur);
4053b4ce94deSChris Mason 
4054*bd681513SChris Mason 		path->locks[level - 1] = BTRFS_READ_LOCK;
40553f157a2fSChris Mason 		path->nodes[level - 1] = cur;
40563f157a2fSChris Mason 		unlock_up(path, level, 1);
4057*bd681513SChris Mason 		btrfs_clear_path_blocking(path, NULL, 0);
40583f157a2fSChris Mason 	}
40593f157a2fSChris Mason out:
40603f157a2fSChris Mason 	if (ret == 0)
40613f157a2fSChris Mason 		memcpy(min_key, &found_key, sizeof(found_key));
4062b4ce94deSChris Mason 	btrfs_set_path_blocking(path);
40633f157a2fSChris Mason 	return ret;
40643f157a2fSChris Mason }
40653f157a2fSChris Mason 
40663f157a2fSChris Mason /*
40673f157a2fSChris Mason  * this is similar to btrfs_next_leaf, but does not try to preserve
40683f157a2fSChris Mason  * and fixup the path.  It looks for and returns the next key in the
40693f157a2fSChris Mason  * tree based on the current path and the cache_only and min_trans
40703f157a2fSChris Mason  * parameters.
40713f157a2fSChris Mason  *
40723f157a2fSChris Mason  * 0 is returned if another key is found, < 0 if there are any errors
40733f157a2fSChris Mason  * and 1 is returned if there are no higher keys in the tree
40743f157a2fSChris Mason  *
40753f157a2fSChris Mason  * path->keep_locks should be set to 1 on the search made before
40763f157a2fSChris Mason  * calling this function.
40773f157a2fSChris Mason  */
4078e7a84565SChris Mason int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
407933c66f43SYan Zheng 			struct btrfs_key *key, int level,
40803f157a2fSChris Mason 			int cache_only, u64 min_trans)
4081e7a84565SChris Mason {
4082e7a84565SChris Mason 	int slot;
4083e7a84565SChris Mason 	struct extent_buffer *c;
4084e7a84565SChris Mason 
4085934d375bSChris Mason 	WARN_ON(!path->keep_locks);
4086e7a84565SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
4087e7a84565SChris Mason 		if (!path->nodes[level])
4088e7a84565SChris Mason 			return 1;
4089e7a84565SChris Mason 
4090e7a84565SChris Mason 		slot = path->slots[level] + 1;
4091e7a84565SChris Mason 		c = path->nodes[level];
40923f157a2fSChris Mason next:
4093e7a84565SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
409433c66f43SYan Zheng 			int ret;
409533c66f43SYan Zheng 			int orig_lowest;
409633c66f43SYan Zheng 			struct btrfs_key cur_key;
409733c66f43SYan Zheng 			if (level + 1 >= BTRFS_MAX_LEVEL ||
409833c66f43SYan Zheng 			    !path->nodes[level + 1])
4099e7a84565SChris Mason 				return 1;
410033c66f43SYan Zheng 
410133c66f43SYan Zheng 			if (path->locks[level + 1]) {
410233c66f43SYan Zheng 				level++;
4103e7a84565SChris Mason 				continue;
4104e7a84565SChris Mason 			}
410533c66f43SYan Zheng 
410633c66f43SYan Zheng 			slot = btrfs_header_nritems(c) - 1;
410733c66f43SYan Zheng 			if (level == 0)
410833c66f43SYan Zheng 				btrfs_item_key_to_cpu(c, &cur_key, slot);
410933c66f43SYan Zheng 			else
411033c66f43SYan Zheng 				btrfs_node_key_to_cpu(c, &cur_key, slot);
411133c66f43SYan Zheng 
411233c66f43SYan Zheng 			orig_lowest = path->lowest_level;
4113b3b4aa74SDavid Sterba 			btrfs_release_path(path);
411433c66f43SYan Zheng 			path->lowest_level = level;
411533c66f43SYan Zheng 			ret = btrfs_search_slot(NULL, root, &cur_key, path,
411633c66f43SYan Zheng 						0, 0);
411733c66f43SYan Zheng 			path->lowest_level = orig_lowest;
411833c66f43SYan Zheng 			if (ret < 0)
411933c66f43SYan Zheng 				return ret;
412033c66f43SYan Zheng 
412133c66f43SYan Zheng 			c = path->nodes[level];
412233c66f43SYan Zheng 			slot = path->slots[level];
412333c66f43SYan Zheng 			if (ret == 0)
412433c66f43SYan Zheng 				slot++;
412533c66f43SYan Zheng 			goto next;
412633c66f43SYan Zheng 		}
412733c66f43SYan Zheng 
4128e7a84565SChris Mason 		if (level == 0)
4129e7a84565SChris Mason 			btrfs_item_key_to_cpu(c, key, slot);
41303f157a2fSChris Mason 		else {
41313f157a2fSChris Mason 			u64 blockptr = btrfs_node_blockptr(c, slot);
41323f157a2fSChris Mason 			u64 gen = btrfs_node_ptr_generation(c, slot);
41333f157a2fSChris Mason 
41343f157a2fSChris Mason 			if (cache_only) {
41353f157a2fSChris Mason 				struct extent_buffer *cur;
41363f157a2fSChris Mason 				cur = btrfs_find_tree_block(root, blockptr,
41373f157a2fSChris Mason 					    btrfs_level_size(root, level - 1));
41383f157a2fSChris Mason 				if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
41393f157a2fSChris Mason 					slot++;
41403f157a2fSChris Mason 					if (cur)
41413f157a2fSChris Mason 						free_extent_buffer(cur);
41423f157a2fSChris Mason 					goto next;
41433f157a2fSChris Mason 				}
41443f157a2fSChris Mason 				free_extent_buffer(cur);
41453f157a2fSChris Mason 			}
41463f157a2fSChris Mason 			if (gen < min_trans) {
41473f157a2fSChris Mason 				slot++;
41483f157a2fSChris Mason 				goto next;
41493f157a2fSChris Mason 			}
4150e7a84565SChris Mason 			btrfs_node_key_to_cpu(c, key, slot);
41513f157a2fSChris Mason 		}
4152e7a84565SChris Mason 		return 0;
4153e7a84565SChris Mason 	}
4154e7a84565SChris Mason 	return 1;
4155e7a84565SChris Mason }
4156e7a84565SChris Mason 
41577bb86316SChris Mason /*
4158925baeddSChris Mason  * search the tree again to find a leaf with greater keys
41590f70abe2SChris Mason  * returns 0 if it found something or 1 if there are no greater leaves.
41600f70abe2SChris Mason  * returns < 0 on io errors.
416197571fd0SChris Mason  */
4162234b63a0SChris Mason int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
4163d97e63b6SChris Mason {
4164d97e63b6SChris Mason 	int slot;
41658e73f275SChris Mason 	int level;
41665f39d397SChris Mason 	struct extent_buffer *c;
41678e73f275SChris Mason 	struct extent_buffer *next;
4168925baeddSChris Mason 	struct btrfs_key key;
4169925baeddSChris Mason 	u32 nritems;
4170925baeddSChris Mason 	int ret;
41718e73f275SChris Mason 	int old_spinning = path->leave_spinning;
41728e73f275SChris Mason 	int force_blocking = 0;
4173*bd681513SChris Mason 	int next_rw_lock = 0;
4174925baeddSChris Mason 
4175925baeddSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
4176d397712bSChris Mason 	if (nritems == 0)
4177925baeddSChris Mason 		return 1;
4178925baeddSChris Mason 
41798e73f275SChris Mason 	/*
41808e73f275SChris Mason 	 * we take the blocks in an order that upsets lockdep.  Using
41818e73f275SChris Mason 	 * blocking mode is the only way around it.
41828e73f275SChris Mason 	 */
41838e73f275SChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC
41848e73f275SChris Mason 	force_blocking = 1;
41858e73f275SChris Mason #endif
4186925baeddSChris Mason 
41878e73f275SChris Mason 	btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
41888e73f275SChris Mason again:
41898e73f275SChris Mason 	level = 1;
41908e73f275SChris Mason 	next = NULL;
4191*bd681513SChris Mason 	next_rw_lock = 0;
4192b3b4aa74SDavid Sterba 	btrfs_release_path(path);
41938e73f275SChris Mason 
4194a2135011SChris Mason 	path->keep_locks = 1;
41958e73f275SChris Mason 
41968e73f275SChris Mason 	if (!force_blocking)
41978e73f275SChris Mason 		path->leave_spinning = 1;
41988e73f275SChris Mason 
4199925baeddSChris Mason 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4200925baeddSChris Mason 	path->keep_locks = 0;
4201925baeddSChris Mason 
4202925baeddSChris Mason 	if (ret < 0)
4203925baeddSChris Mason 		return ret;
4204925baeddSChris Mason 
4205a2135011SChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
4206168fd7d2SChris Mason 	/*
4207168fd7d2SChris Mason 	 * by releasing the path above we dropped all our locks.  A balance
4208168fd7d2SChris Mason 	 * could have added more items next to the key that used to be
4209168fd7d2SChris Mason 	 * at the very end of the block.  So, check again here and
4210168fd7d2SChris Mason 	 * advance the path if there are now more items available.
4211168fd7d2SChris Mason 	 */
4212a2135011SChris Mason 	if (nritems > 0 && path->slots[0] < nritems - 1) {
4213e457afecSYan Zheng 		if (ret == 0)
4214168fd7d2SChris Mason 			path->slots[0]++;
42158e73f275SChris Mason 		ret = 0;
4216925baeddSChris Mason 		goto done;
4217925baeddSChris Mason 	}
4218d97e63b6SChris Mason 
4219234b63a0SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
42208e73f275SChris Mason 		if (!path->nodes[level]) {
42218e73f275SChris Mason 			ret = 1;
42228e73f275SChris Mason 			goto done;
42238e73f275SChris Mason 		}
42245f39d397SChris Mason 
4225d97e63b6SChris Mason 		slot = path->slots[level] + 1;
4226d97e63b6SChris Mason 		c = path->nodes[level];
42275f39d397SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
4228d97e63b6SChris Mason 			level++;
42298e73f275SChris Mason 			if (level == BTRFS_MAX_LEVEL) {
42308e73f275SChris Mason 				ret = 1;
42318e73f275SChris Mason 				goto done;
42328e73f275SChris Mason 			}
4233d97e63b6SChris Mason 			continue;
4234d97e63b6SChris Mason 		}
42355f39d397SChris Mason 
4236925baeddSChris Mason 		if (next) {
4237*bd681513SChris Mason 			btrfs_tree_unlock_rw(next, next_rw_lock);
42385f39d397SChris Mason 			free_extent_buffer(next);
4239925baeddSChris Mason 		}
42405f39d397SChris Mason 
42418e73f275SChris Mason 		next = c;
4242*bd681513SChris Mason 		next_rw_lock = path->locks[level];
42438e73f275SChris Mason 		ret = read_block_for_search(NULL, root, path, &next, level,
42448e73f275SChris Mason 					    slot, &key);
42458e73f275SChris Mason 		if (ret == -EAGAIN)
42468e73f275SChris Mason 			goto again;
42475f39d397SChris Mason 
424876a05b35SChris Mason 		if (ret < 0) {
4249b3b4aa74SDavid Sterba 			btrfs_release_path(path);
425076a05b35SChris Mason 			goto done;
425176a05b35SChris Mason 		}
425276a05b35SChris Mason 
42535cd57b2cSChris Mason 		if (!path->skip_locking) {
4254*bd681513SChris Mason 			ret = btrfs_try_tree_read_lock(next);
42558e73f275SChris Mason 			if (!ret) {
42568e73f275SChris Mason 				btrfs_set_path_blocking(path);
4257*bd681513SChris Mason 				btrfs_tree_read_lock(next);
4258*bd681513SChris Mason 				if (!force_blocking) {
4259*bd681513SChris Mason 					btrfs_clear_path_blocking(path, next,
4260*bd681513SChris Mason 							  BTRFS_READ_LOCK);
42618e73f275SChris Mason 				}
4262*bd681513SChris Mason 			}
4263*bd681513SChris Mason 			if (force_blocking) {
4264*bd681513SChris Mason 				btrfs_set_lock_blocking_rw(next,
4265*bd681513SChris Mason 							   BTRFS_READ_LOCK);
4266*bd681513SChris Mason 				next_rw_lock = BTRFS_READ_LOCK_BLOCKING;
4267*bd681513SChris Mason 			} else {
4268*bd681513SChris Mason 				next_rw_lock = BTRFS_READ_LOCK;
4269*bd681513SChris Mason 			}
42705cd57b2cSChris Mason 		}
4271d97e63b6SChris Mason 		break;
4272d97e63b6SChris Mason 	}
4273d97e63b6SChris Mason 	path->slots[level] = slot;
4274d97e63b6SChris Mason 	while (1) {
4275d97e63b6SChris Mason 		level--;
4276d97e63b6SChris Mason 		c = path->nodes[level];
4277925baeddSChris Mason 		if (path->locks[level])
4278*bd681513SChris Mason 			btrfs_tree_unlock_rw(c, path->locks[level]);
42798e73f275SChris Mason 
42805f39d397SChris Mason 		free_extent_buffer(c);
4281d97e63b6SChris Mason 		path->nodes[level] = next;
4282d97e63b6SChris Mason 		path->slots[level] = 0;
4283a74a4b97SChris Mason 		if (!path->skip_locking)
4284*bd681513SChris Mason 			path->locks[level] = next_rw_lock;
4285d97e63b6SChris Mason 		if (!level)
4286d97e63b6SChris Mason 			break;
4287b4ce94deSChris Mason 
42888e73f275SChris Mason 		ret = read_block_for_search(NULL, root, path, &next, level,
42898e73f275SChris Mason 					    0, &key);
42908e73f275SChris Mason 		if (ret == -EAGAIN)
42918e73f275SChris Mason 			goto again;
42928e73f275SChris Mason 
429376a05b35SChris Mason 		if (ret < 0) {
4294b3b4aa74SDavid Sterba 			btrfs_release_path(path);
429576a05b35SChris Mason 			goto done;
429676a05b35SChris Mason 		}
429776a05b35SChris Mason 
42985cd57b2cSChris Mason 		if (!path->skip_locking) {
4299*bd681513SChris Mason 			ret = btrfs_try_tree_read_lock(next);
43008e73f275SChris Mason 			if (!ret) {
43018e73f275SChris Mason 				btrfs_set_path_blocking(path);
4302*bd681513SChris Mason 				btrfs_tree_read_lock(next);
43038e73f275SChris Mason 				if (!force_blocking)
4304*bd681513SChris Mason 					btrfs_clear_path_blocking(path, next,
4305*bd681513SChris Mason 							  BTRFS_READ_LOCK);
43068e73f275SChris Mason 			}
4307*bd681513SChris Mason 			if (force_blocking) {
4308*bd681513SChris Mason 				btrfs_set_lock_blocking_rw(next,
4309*bd681513SChris Mason 						   BTRFS_READ_LOCK);
4310*bd681513SChris Mason 				next_rw_lock = BTRFS_READ_LOCK_BLOCKING;
4311*bd681513SChris Mason 			} else {
4312*bd681513SChris Mason 				next_rw_lock = BTRFS_READ_LOCK;
4313*bd681513SChris Mason 			}
4314d97e63b6SChris Mason 		}
43155cd57b2cSChris Mason 	}
43168e73f275SChris Mason 	ret = 0;
4317925baeddSChris Mason done:
4318925baeddSChris Mason 	unlock_up(path, 0, 1);
43198e73f275SChris Mason 	path->leave_spinning = old_spinning;
43208e73f275SChris Mason 	if (!old_spinning)
43218e73f275SChris Mason 		btrfs_set_path_blocking(path);
43228e73f275SChris Mason 
43238e73f275SChris Mason 	return ret;
4324d97e63b6SChris Mason }
43250b86a832SChris Mason 
43263f157a2fSChris Mason /*
43273f157a2fSChris Mason  * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
43283f157a2fSChris Mason  * searching until it gets past min_objectid or finds an item of 'type'
43293f157a2fSChris Mason  *
43303f157a2fSChris Mason  * returns 0 if something is found, 1 if nothing was found and < 0 on error
43313f157a2fSChris Mason  */
43320b86a832SChris Mason int btrfs_previous_item(struct btrfs_root *root,
43330b86a832SChris Mason 			struct btrfs_path *path, u64 min_objectid,
43340b86a832SChris Mason 			int type)
43350b86a832SChris Mason {
43360b86a832SChris Mason 	struct btrfs_key found_key;
43370b86a832SChris Mason 	struct extent_buffer *leaf;
4338e02119d5SChris Mason 	u32 nritems;
43390b86a832SChris Mason 	int ret;
43400b86a832SChris Mason 
43410b86a832SChris Mason 	while (1) {
43420b86a832SChris Mason 		if (path->slots[0] == 0) {
4343b4ce94deSChris Mason 			btrfs_set_path_blocking(path);
43440b86a832SChris Mason 			ret = btrfs_prev_leaf(root, path);
43450b86a832SChris Mason 			if (ret != 0)
43460b86a832SChris Mason 				return ret;
43470b86a832SChris Mason 		} else {
43480b86a832SChris Mason 			path->slots[0]--;
43490b86a832SChris Mason 		}
43500b86a832SChris Mason 		leaf = path->nodes[0];
4351e02119d5SChris Mason 		nritems = btrfs_header_nritems(leaf);
4352e02119d5SChris Mason 		if (nritems == 0)
4353e02119d5SChris Mason 			return 1;
4354e02119d5SChris Mason 		if (path->slots[0] == nritems)
4355e02119d5SChris Mason 			path->slots[0]--;
4356e02119d5SChris Mason 
43570b86a832SChris Mason 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4358e02119d5SChris Mason 		if (found_key.objectid < min_objectid)
4359e02119d5SChris Mason 			break;
43600a4eefbbSYan Zheng 		if (found_key.type == type)
43610a4eefbbSYan Zheng 			return 0;
4362e02119d5SChris Mason 		if (found_key.objectid == min_objectid &&
4363e02119d5SChris Mason 		    found_key.type < type)
4364e02119d5SChris Mason 			break;
43650b86a832SChris Mason 	}
43660b86a832SChris Mason 	return 1;
43670b86a832SChris Mason }
4368