xref: /openbmc/linux/fs/btrfs/ctree.c (revision e5df957328b18baa731307c66cfe8e7a4981df65)
16cbd5570SChris Mason /*
2d352ac68SChris Mason  * Copyright (C) 2007,2008 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
19a6b6e75eSChris Mason #include <linux/sched.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
21eb60ceacSChris Mason #include "ctree.h"
22eb60ceacSChris Mason #include "disk-io.h"
237f5c1516SChris Mason #include "transaction.h"
245f39d397SChris Mason #include "print-tree.h"
25925baeddSChris Mason #include "locking.h"
269a8dd150SChris Mason 
27e089f05cSChris Mason static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
28e089f05cSChris Mason 		      *root, struct btrfs_path *path, int level);
29e089f05cSChris Mason static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
30d4dbff95SChris Mason 		      *root, struct btrfs_key *ins_key,
31cc0c5538SChris Mason 		      struct btrfs_path *path, int data_size, int extend);
325f39d397SChris Mason static int push_node_left(struct btrfs_trans_handle *trans,
335f39d397SChris Mason 			  struct btrfs_root *root, struct extent_buffer *dst,
34971a1f66SChris Mason 			  struct extent_buffer *src, int empty);
355f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans,
365f39d397SChris Mason 			      struct btrfs_root *root,
375f39d397SChris Mason 			      struct extent_buffer *dst_buf,
385f39d397SChris Mason 			      struct extent_buffer *src_buf);
39143bede5SJeff Mahoney static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
40e089f05cSChris Mason 		   struct btrfs_path *path, int level, int slot);
41d97e63b6SChris Mason 
422c90e5d6SChris Mason struct btrfs_path *btrfs_alloc_path(void)
432c90e5d6SChris Mason {
44df24a2b9SChris Mason 	struct btrfs_path *path;
45e00f7308SJeff Mahoney 	path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
46df24a2b9SChris Mason 	return path;
472c90e5d6SChris Mason }
482c90e5d6SChris Mason 
49b4ce94deSChris Mason /*
50b4ce94deSChris Mason  * set all locked nodes in the path to blocking locks.  This should
51b4ce94deSChris Mason  * be done before scheduling
52b4ce94deSChris Mason  */
53b4ce94deSChris Mason noinline void btrfs_set_path_blocking(struct btrfs_path *p)
54b4ce94deSChris Mason {
55b4ce94deSChris Mason 	int i;
56b4ce94deSChris Mason 	for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
57bd681513SChris Mason 		if (!p->nodes[i] || !p->locks[i])
58bd681513SChris Mason 			continue;
59bd681513SChris Mason 		btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
60bd681513SChris Mason 		if (p->locks[i] == BTRFS_READ_LOCK)
61bd681513SChris Mason 			p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
62bd681513SChris Mason 		else if (p->locks[i] == BTRFS_WRITE_LOCK)
63bd681513SChris Mason 			p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
64b4ce94deSChris Mason 	}
65b4ce94deSChris Mason }
66b4ce94deSChris Mason 
67b4ce94deSChris Mason /*
68b4ce94deSChris Mason  * reset all the locked nodes in the patch to spinning locks.
694008c04aSChris Mason  *
704008c04aSChris Mason  * held is used to keep lockdep happy, when lockdep is enabled
714008c04aSChris Mason  * we set held to a blocking lock before we go around and
724008c04aSChris Mason  * retake all the spinlocks in the path.  You can safely use NULL
734008c04aSChris Mason  * for held
74b4ce94deSChris Mason  */
754008c04aSChris Mason noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
76bd681513SChris Mason 					struct extent_buffer *held, int held_rw)
77b4ce94deSChris Mason {
78b4ce94deSChris Mason 	int i;
794008c04aSChris Mason 
804008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC
814008c04aSChris Mason 	/* lockdep really cares that we take all of these spinlocks
824008c04aSChris Mason 	 * in the right order.  If any of the locks in the path are not
834008c04aSChris Mason 	 * currently blocking, it is going to complain.  So, make really
844008c04aSChris Mason 	 * really sure by forcing the path to blocking before we clear
854008c04aSChris Mason 	 * the path blocking.
864008c04aSChris Mason 	 */
87bd681513SChris Mason 	if (held) {
88bd681513SChris Mason 		btrfs_set_lock_blocking_rw(held, held_rw);
89bd681513SChris Mason 		if (held_rw == BTRFS_WRITE_LOCK)
90bd681513SChris Mason 			held_rw = BTRFS_WRITE_LOCK_BLOCKING;
91bd681513SChris Mason 		else if (held_rw == BTRFS_READ_LOCK)
92bd681513SChris Mason 			held_rw = BTRFS_READ_LOCK_BLOCKING;
93bd681513SChris Mason 	}
944008c04aSChris Mason 	btrfs_set_path_blocking(p);
954008c04aSChris Mason #endif
964008c04aSChris Mason 
974008c04aSChris Mason 	for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
98bd681513SChris Mason 		if (p->nodes[i] && p->locks[i]) {
99bd681513SChris Mason 			btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
100bd681513SChris Mason 			if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
101bd681513SChris Mason 				p->locks[i] = BTRFS_WRITE_LOCK;
102bd681513SChris Mason 			else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
103bd681513SChris Mason 				p->locks[i] = BTRFS_READ_LOCK;
104bd681513SChris Mason 		}
105b4ce94deSChris Mason 	}
1064008c04aSChris Mason 
1074008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC
1084008c04aSChris Mason 	if (held)
109bd681513SChris Mason 		btrfs_clear_lock_blocking_rw(held, held_rw);
1104008c04aSChris Mason #endif
111b4ce94deSChris Mason }
112b4ce94deSChris Mason 
113d352ac68SChris Mason /* this also releases the path */
1142c90e5d6SChris Mason void btrfs_free_path(struct btrfs_path *p)
1152c90e5d6SChris Mason {
116ff175d57SJesper Juhl 	if (!p)
117ff175d57SJesper Juhl 		return;
118b3b4aa74SDavid Sterba 	btrfs_release_path(p);
1192c90e5d6SChris Mason 	kmem_cache_free(btrfs_path_cachep, p);
1202c90e5d6SChris Mason }
1212c90e5d6SChris Mason 
122d352ac68SChris Mason /*
123d352ac68SChris Mason  * path release drops references on the extent buffers in the path
124d352ac68SChris Mason  * and it drops any locks held by this path
125d352ac68SChris Mason  *
126d352ac68SChris Mason  * It is safe to call this on paths that no locks or extent buffers held.
127d352ac68SChris Mason  */
128b3b4aa74SDavid Sterba noinline void btrfs_release_path(struct btrfs_path *p)
129eb60ceacSChris Mason {
130eb60ceacSChris Mason 	int i;
131a2135011SChris Mason 
132234b63a0SChris Mason 	for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
1333f157a2fSChris Mason 		p->slots[i] = 0;
134eb60ceacSChris Mason 		if (!p->nodes[i])
135925baeddSChris Mason 			continue;
136925baeddSChris Mason 		if (p->locks[i]) {
137bd681513SChris Mason 			btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
138925baeddSChris Mason 			p->locks[i] = 0;
139925baeddSChris Mason 		}
1405f39d397SChris Mason 		free_extent_buffer(p->nodes[i]);
1413f157a2fSChris Mason 		p->nodes[i] = NULL;
142eb60ceacSChris Mason 	}
143eb60ceacSChris Mason }
144eb60ceacSChris Mason 
145d352ac68SChris Mason /*
146d352ac68SChris Mason  * safely gets a reference on the root node of a tree.  A lock
147d352ac68SChris Mason  * is not taken, so a concurrent writer may put a different node
148d352ac68SChris Mason  * at the root of the tree.  See btrfs_lock_root_node for the
149d352ac68SChris Mason  * looping required.
150d352ac68SChris Mason  *
151d352ac68SChris Mason  * The extent buffer returned by this has a reference taken, so
152d352ac68SChris Mason  * it won't disappear.  It may stop being the root of the tree
153d352ac68SChris Mason  * at any time because there are no locks held.
154d352ac68SChris Mason  */
155925baeddSChris Mason struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
156925baeddSChris Mason {
157925baeddSChris Mason 	struct extent_buffer *eb;
158240f62c8SChris Mason 
159240f62c8SChris Mason 	rcu_read_lock();
160240f62c8SChris Mason 	eb = rcu_dereference(root->node);
161925baeddSChris Mason 	extent_buffer_get(eb);
162240f62c8SChris Mason 	rcu_read_unlock();
163925baeddSChris Mason 	return eb;
164925baeddSChris Mason }
165925baeddSChris Mason 
166d352ac68SChris Mason /* loop around taking references on and locking the root node of the
167d352ac68SChris Mason  * tree until you end up with a lock on the root.  A locked buffer
168d352ac68SChris Mason  * is returned, with a reference held.
169d352ac68SChris Mason  */
170925baeddSChris Mason struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
171925baeddSChris Mason {
172925baeddSChris Mason 	struct extent_buffer *eb;
173925baeddSChris Mason 
174925baeddSChris Mason 	while (1) {
175925baeddSChris Mason 		eb = btrfs_root_node(root);
176925baeddSChris Mason 		btrfs_tree_lock(eb);
177240f62c8SChris Mason 		if (eb == root->node)
178925baeddSChris Mason 			break;
179925baeddSChris Mason 		btrfs_tree_unlock(eb);
180925baeddSChris Mason 		free_extent_buffer(eb);
181925baeddSChris Mason 	}
182925baeddSChris Mason 	return eb;
183925baeddSChris Mason }
184925baeddSChris Mason 
185bd681513SChris Mason /* loop around taking references on and locking the root node of the
186bd681513SChris Mason  * tree until you end up with a lock on the root.  A locked buffer
187bd681513SChris Mason  * is returned, with a reference held.
188bd681513SChris Mason  */
189bd681513SChris Mason struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
190bd681513SChris Mason {
191bd681513SChris Mason 	struct extent_buffer *eb;
192bd681513SChris Mason 
193bd681513SChris Mason 	while (1) {
194bd681513SChris Mason 		eb = btrfs_root_node(root);
195bd681513SChris Mason 		btrfs_tree_read_lock(eb);
196bd681513SChris Mason 		if (eb == root->node)
197bd681513SChris Mason 			break;
198bd681513SChris Mason 		btrfs_tree_read_unlock(eb);
199bd681513SChris Mason 		free_extent_buffer(eb);
200bd681513SChris Mason 	}
201bd681513SChris Mason 	return eb;
202bd681513SChris Mason }
203bd681513SChris Mason 
204d352ac68SChris Mason /* cowonly root (everything not a reference counted cow subvolume), just get
205d352ac68SChris Mason  * put onto a simple dirty list.  transaction.c walks this to make sure they
206d352ac68SChris Mason  * get properly updated on disk.
207d352ac68SChris Mason  */
2080b86a832SChris Mason static void add_root_to_dirty_list(struct btrfs_root *root)
2090b86a832SChris Mason {
2100b86a832SChris Mason 	if (root->track_dirty && list_empty(&root->dirty_list)) {
2110b86a832SChris Mason 		list_add(&root->dirty_list,
2120b86a832SChris Mason 			 &root->fs_info->dirty_cowonly_roots);
2130b86a832SChris Mason 	}
2140b86a832SChris Mason }
2150b86a832SChris Mason 
216d352ac68SChris Mason /*
217d352ac68SChris Mason  * used by snapshot creation to make a copy of a root for a tree with
218d352ac68SChris Mason  * a given objectid.  The buffer with the new root node is returned in
219d352ac68SChris Mason  * cow_ret, and this func returns zero on success or a negative error code.
220d352ac68SChris Mason  */
221be20aa9dSChris Mason int btrfs_copy_root(struct btrfs_trans_handle *trans,
222be20aa9dSChris Mason 		      struct btrfs_root *root,
223be20aa9dSChris Mason 		      struct extent_buffer *buf,
224be20aa9dSChris Mason 		      struct extent_buffer **cow_ret, u64 new_root_objectid)
225be20aa9dSChris Mason {
226be20aa9dSChris Mason 	struct extent_buffer *cow;
227be20aa9dSChris Mason 	int ret = 0;
228be20aa9dSChris Mason 	int level;
2295d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
230be20aa9dSChris Mason 
231be20aa9dSChris Mason 	WARN_ON(root->ref_cows && trans->transid !=
232be20aa9dSChris Mason 		root->fs_info->running_transaction->transid);
233be20aa9dSChris Mason 	WARN_ON(root->ref_cows && trans->transid != root->last_trans);
234be20aa9dSChris Mason 
235be20aa9dSChris Mason 	level = btrfs_header_level(buf);
2365d4f98a2SYan Zheng 	if (level == 0)
2375d4f98a2SYan Zheng 		btrfs_item_key(buf, &disk_key, 0);
2385d4f98a2SYan Zheng 	else
2395d4f98a2SYan Zheng 		btrfs_node_key(buf, &disk_key, 0);
24031840ae1SZheng Yan 
2415d4f98a2SYan Zheng 	cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
2425d4f98a2SYan Zheng 				     new_root_objectid, &disk_key, level,
24366d7e7f0SArne Jansen 				     buf->start, 0, 1);
2445d4f98a2SYan Zheng 	if (IS_ERR(cow))
245be20aa9dSChris Mason 		return PTR_ERR(cow);
246be20aa9dSChris Mason 
247be20aa9dSChris Mason 	copy_extent_buffer(cow, buf, 0, 0, cow->len);
248be20aa9dSChris Mason 	btrfs_set_header_bytenr(cow, cow->start);
249be20aa9dSChris Mason 	btrfs_set_header_generation(cow, trans->transid);
2505d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
2515d4f98a2SYan Zheng 	btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
2525d4f98a2SYan Zheng 				     BTRFS_HEADER_FLAG_RELOC);
2535d4f98a2SYan Zheng 	if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
2545d4f98a2SYan Zheng 		btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
2555d4f98a2SYan Zheng 	else
256be20aa9dSChris Mason 		btrfs_set_header_owner(cow, new_root_objectid);
257be20aa9dSChris Mason 
2582b82032cSYan Zheng 	write_extent_buffer(cow, root->fs_info->fsid,
2592b82032cSYan Zheng 			    (unsigned long)btrfs_header_fsid(cow),
2602b82032cSYan Zheng 			    BTRFS_FSID_SIZE);
2612b82032cSYan Zheng 
262be20aa9dSChris Mason 	WARN_ON(btrfs_header_generation(buf) > trans->transid);
2635d4f98a2SYan Zheng 	if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
26466d7e7f0SArne Jansen 		ret = btrfs_inc_ref(trans, root, cow, 1, 1);
2655d4f98a2SYan Zheng 	else
26666d7e7f0SArne Jansen 		ret = btrfs_inc_ref(trans, root, cow, 0, 1);
2674aec2b52SChris Mason 
268be20aa9dSChris Mason 	if (ret)
269be20aa9dSChris Mason 		return ret;
270be20aa9dSChris Mason 
271be20aa9dSChris Mason 	btrfs_mark_buffer_dirty(cow);
272be20aa9dSChris Mason 	*cow_ret = cow;
273be20aa9dSChris Mason 	return 0;
274be20aa9dSChris Mason }
275be20aa9dSChris Mason 
276d352ac68SChris Mason /*
2775d4f98a2SYan Zheng  * check if the tree block can be shared by multiple trees
2785d4f98a2SYan Zheng  */
2795d4f98a2SYan Zheng int btrfs_block_can_be_shared(struct btrfs_root *root,
2805d4f98a2SYan Zheng 			      struct extent_buffer *buf)
2815d4f98a2SYan Zheng {
2825d4f98a2SYan Zheng 	/*
2835d4f98a2SYan Zheng 	 * Tree blocks not in refernece counted trees and tree roots
2845d4f98a2SYan Zheng 	 * are never shared. If a block was allocated after the last
2855d4f98a2SYan Zheng 	 * snapshot and the block was not allocated by tree relocation,
2865d4f98a2SYan Zheng 	 * we know the block is not shared.
2875d4f98a2SYan Zheng 	 */
2885d4f98a2SYan Zheng 	if (root->ref_cows &&
2895d4f98a2SYan Zheng 	    buf != root->node && buf != root->commit_root &&
2905d4f98a2SYan Zheng 	    (btrfs_header_generation(buf) <=
2915d4f98a2SYan Zheng 	     btrfs_root_last_snapshot(&root->root_item) ||
2925d4f98a2SYan Zheng 	     btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
2935d4f98a2SYan Zheng 		return 1;
2945d4f98a2SYan Zheng #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2955d4f98a2SYan Zheng 	if (root->ref_cows &&
2965d4f98a2SYan Zheng 	    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
2975d4f98a2SYan Zheng 		return 1;
2985d4f98a2SYan Zheng #endif
2995d4f98a2SYan Zheng 	return 0;
3005d4f98a2SYan Zheng }
3015d4f98a2SYan Zheng 
3025d4f98a2SYan Zheng static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
3035d4f98a2SYan Zheng 				       struct btrfs_root *root,
3045d4f98a2SYan Zheng 				       struct extent_buffer *buf,
305f0486c68SYan, Zheng 				       struct extent_buffer *cow,
306f0486c68SYan, Zheng 				       int *last_ref)
3075d4f98a2SYan Zheng {
3085d4f98a2SYan Zheng 	u64 refs;
3095d4f98a2SYan Zheng 	u64 owner;
3105d4f98a2SYan Zheng 	u64 flags;
3115d4f98a2SYan Zheng 	u64 new_flags = 0;
3125d4f98a2SYan Zheng 	int ret;
3135d4f98a2SYan Zheng 
3145d4f98a2SYan Zheng 	/*
3155d4f98a2SYan Zheng 	 * Backrefs update rules:
3165d4f98a2SYan Zheng 	 *
3175d4f98a2SYan Zheng 	 * Always use full backrefs for extent pointers in tree block
3185d4f98a2SYan Zheng 	 * allocated by tree relocation.
3195d4f98a2SYan Zheng 	 *
3205d4f98a2SYan Zheng 	 * If a shared tree block is no longer referenced by its owner
3215d4f98a2SYan Zheng 	 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
3225d4f98a2SYan Zheng 	 * use full backrefs for extent pointers in tree block.
3235d4f98a2SYan Zheng 	 *
3245d4f98a2SYan Zheng 	 * If a tree block is been relocating
3255d4f98a2SYan Zheng 	 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
3265d4f98a2SYan Zheng 	 * use full backrefs for extent pointers in tree block.
3275d4f98a2SYan Zheng 	 * The reason for this is some operations (such as drop tree)
3285d4f98a2SYan Zheng 	 * are only allowed for blocks use full backrefs.
3295d4f98a2SYan Zheng 	 */
3305d4f98a2SYan Zheng 
3315d4f98a2SYan Zheng 	if (btrfs_block_can_be_shared(root, buf)) {
3325d4f98a2SYan Zheng 		ret = btrfs_lookup_extent_info(trans, root, buf->start,
3335d4f98a2SYan Zheng 					       buf->len, &refs, &flags);
334be1a5564SMark Fasheh 		if (ret)
335be1a5564SMark Fasheh 			return ret;
336*e5df9573SMark Fasheh 		if (refs == 0) {
337*e5df9573SMark Fasheh 			ret = -EROFS;
338*e5df9573SMark Fasheh 			btrfs_std_error(root->fs_info, ret);
339*e5df9573SMark Fasheh 			return ret;
340*e5df9573SMark Fasheh 		}
3415d4f98a2SYan Zheng 	} else {
3425d4f98a2SYan Zheng 		refs = 1;
3435d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
3445d4f98a2SYan Zheng 		    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
3455d4f98a2SYan Zheng 			flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
3465d4f98a2SYan Zheng 		else
3475d4f98a2SYan Zheng 			flags = 0;
3485d4f98a2SYan Zheng 	}
3495d4f98a2SYan Zheng 
3505d4f98a2SYan Zheng 	owner = btrfs_header_owner(buf);
3515d4f98a2SYan Zheng 	BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
3525d4f98a2SYan Zheng 	       !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
3535d4f98a2SYan Zheng 
3545d4f98a2SYan Zheng 	if (refs > 1) {
3555d4f98a2SYan Zheng 		if ((owner == root->root_key.objectid ||
3565d4f98a2SYan Zheng 		     root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
3575d4f98a2SYan Zheng 		    !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
35866d7e7f0SArne Jansen 			ret = btrfs_inc_ref(trans, root, buf, 1, 1);
3595d4f98a2SYan Zheng 			BUG_ON(ret);
3605d4f98a2SYan Zheng 
3615d4f98a2SYan Zheng 			if (root->root_key.objectid ==
3625d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID) {
36366d7e7f0SArne Jansen 				ret = btrfs_dec_ref(trans, root, buf, 0, 1);
3645d4f98a2SYan Zheng 				BUG_ON(ret);
36566d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 1, 1);
3665d4f98a2SYan Zheng 				BUG_ON(ret);
3675d4f98a2SYan Zheng 			}
3685d4f98a2SYan Zheng 			new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
3695d4f98a2SYan Zheng 		} else {
3705d4f98a2SYan Zheng 
3715d4f98a2SYan Zheng 			if (root->root_key.objectid ==
3725d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID)
37366d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 1, 1);
3745d4f98a2SYan Zheng 			else
37566d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 0, 1);
3765d4f98a2SYan Zheng 			BUG_ON(ret);
3775d4f98a2SYan Zheng 		}
3785d4f98a2SYan Zheng 		if (new_flags != 0) {
3795d4f98a2SYan Zheng 			ret = btrfs_set_disk_extent_flags(trans, root,
3805d4f98a2SYan Zheng 							  buf->start,
3815d4f98a2SYan Zheng 							  buf->len,
3825d4f98a2SYan Zheng 							  new_flags, 0);
383be1a5564SMark Fasheh 			if (ret)
384be1a5564SMark Fasheh 				return ret;
3855d4f98a2SYan Zheng 		}
3865d4f98a2SYan Zheng 	} else {
3875d4f98a2SYan Zheng 		if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
3885d4f98a2SYan Zheng 			if (root->root_key.objectid ==
3895d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID)
39066d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 1, 1);
3915d4f98a2SYan Zheng 			else
39266d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 0, 1);
3935d4f98a2SYan Zheng 			BUG_ON(ret);
39466d7e7f0SArne Jansen 			ret = btrfs_dec_ref(trans, root, buf, 1, 1);
3955d4f98a2SYan Zheng 			BUG_ON(ret);
3965d4f98a2SYan Zheng 		}
3975d4f98a2SYan Zheng 		clean_tree_block(trans, root, buf);
398f0486c68SYan, Zheng 		*last_ref = 1;
3995d4f98a2SYan Zheng 	}
4005d4f98a2SYan Zheng 	return 0;
4015d4f98a2SYan Zheng }
4025d4f98a2SYan Zheng 
4035d4f98a2SYan Zheng /*
404d397712bSChris Mason  * does the dirty work in cow of a single block.  The parent block (if
405d397712bSChris Mason  * supplied) is updated to point to the new cow copy.  The new buffer is marked
406d397712bSChris Mason  * dirty and returned locked.  If you modify the block it needs to be marked
407d397712bSChris Mason  * dirty again.
408d352ac68SChris Mason  *
409d352ac68SChris Mason  * search_start -- an allocation hint for the new block
410d352ac68SChris Mason  *
411d397712bSChris Mason  * empty_size -- a hint that you plan on doing more cow.  This is the size in
412d397712bSChris Mason  * bytes the allocator should try to find free next to the block it returns.
413d397712bSChris Mason  * This is just a hint and may be ignored by the allocator.
414d352ac68SChris Mason  */
415d397712bSChris Mason static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
4165f39d397SChris Mason 			     struct btrfs_root *root,
4175f39d397SChris Mason 			     struct extent_buffer *buf,
4185f39d397SChris Mason 			     struct extent_buffer *parent, int parent_slot,
4195f39d397SChris Mason 			     struct extent_buffer **cow_ret,
4209fa8cfe7SChris Mason 			     u64 search_start, u64 empty_size)
4216702ed49SChris Mason {
4225d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
4235f39d397SChris Mason 	struct extent_buffer *cow;
424be1a5564SMark Fasheh 	int level, ret;
425f0486c68SYan, Zheng 	int last_ref = 0;
426925baeddSChris Mason 	int unlock_orig = 0;
4275d4f98a2SYan Zheng 	u64 parent_start;
4286702ed49SChris Mason 
429925baeddSChris Mason 	if (*cow_ret == buf)
430925baeddSChris Mason 		unlock_orig = 1;
431925baeddSChris Mason 
432b9447ef8SChris Mason 	btrfs_assert_tree_locked(buf);
433925baeddSChris Mason 
4347bb86316SChris Mason 	WARN_ON(root->ref_cows && trans->transid !=
4357bb86316SChris Mason 		root->fs_info->running_transaction->transid);
4366702ed49SChris Mason 	WARN_ON(root->ref_cows && trans->transid != root->last_trans);
4375f39d397SChris Mason 
4387bb86316SChris Mason 	level = btrfs_header_level(buf);
43931840ae1SZheng Yan 
4405d4f98a2SYan Zheng 	if (level == 0)
4415d4f98a2SYan Zheng 		btrfs_item_key(buf, &disk_key, 0);
4425d4f98a2SYan Zheng 	else
4435d4f98a2SYan Zheng 		btrfs_node_key(buf, &disk_key, 0);
4445d4f98a2SYan Zheng 
4455d4f98a2SYan Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
4465d4f98a2SYan Zheng 		if (parent)
4475d4f98a2SYan Zheng 			parent_start = parent->start;
4485d4f98a2SYan Zheng 		else
4495d4f98a2SYan Zheng 			parent_start = 0;
4505d4f98a2SYan Zheng 	} else
4515d4f98a2SYan Zheng 		parent_start = 0;
4525d4f98a2SYan Zheng 
4535d4f98a2SYan Zheng 	cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
4545d4f98a2SYan Zheng 				     root->root_key.objectid, &disk_key,
45566d7e7f0SArne Jansen 				     level, search_start, empty_size, 1);
4566702ed49SChris Mason 	if (IS_ERR(cow))
4576702ed49SChris Mason 		return PTR_ERR(cow);
4586702ed49SChris Mason 
459b4ce94deSChris Mason 	/* cow is set to blocking by btrfs_init_new_buffer */
460b4ce94deSChris Mason 
4615f39d397SChris Mason 	copy_extent_buffer(cow, buf, 0, 0, cow->len);
462db94535dSChris Mason 	btrfs_set_header_bytenr(cow, cow->start);
4635f39d397SChris Mason 	btrfs_set_header_generation(cow, trans->transid);
4645d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
4655d4f98a2SYan Zheng 	btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
4665d4f98a2SYan Zheng 				     BTRFS_HEADER_FLAG_RELOC);
4675d4f98a2SYan Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4685d4f98a2SYan Zheng 		btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
4695d4f98a2SYan Zheng 	else
4705f39d397SChris Mason 		btrfs_set_header_owner(cow, root->root_key.objectid);
4716702ed49SChris Mason 
4722b82032cSYan Zheng 	write_extent_buffer(cow, root->fs_info->fsid,
4732b82032cSYan Zheng 			    (unsigned long)btrfs_header_fsid(cow),
4742b82032cSYan Zheng 			    BTRFS_FSID_SIZE);
4752b82032cSYan Zheng 
476be1a5564SMark Fasheh 	ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
477be1a5564SMark Fasheh 	BUG_ON(ret);
4781a40e23bSZheng Yan 
4793fd0a558SYan, Zheng 	if (root->ref_cows)
4803fd0a558SYan, Zheng 		btrfs_reloc_cow_block(trans, root, buf, cow);
4813fd0a558SYan, Zheng 
4826702ed49SChris Mason 	if (buf == root->node) {
483925baeddSChris Mason 		WARN_ON(parent && parent != buf);
4845d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
4855d4f98a2SYan Zheng 		    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
4865d4f98a2SYan Zheng 			parent_start = buf->start;
4875d4f98a2SYan Zheng 		else
4885d4f98a2SYan Zheng 			parent_start = 0;
489925baeddSChris Mason 
4905f39d397SChris Mason 		extent_buffer_get(cow);
491240f62c8SChris Mason 		rcu_assign_pointer(root->node, cow);
492925baeddSChris Mason 
493f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, buf, parent_start,
49466d7e7f0SArne Jansen 				      last_ref, 1);
4955f39d397SChris Mason 		free_extent_buffer(buf);
4960b86a832SChris Mason 		add_root_to_dirty_list(root);
4976702ed49SChris Mason 	} else {
4985d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4995d4f98a2SYan Zheng 			parent_start = parent->start;
5005d4f98a2SYan Zheng 		else
5015d4f98a2SYan Zheng 			parent_start = 0;
5025d4f98a2SYan Zheng 
5035d4f98a2SYan Zheng 		WARN_ON(trans->transid != btrfs_header_generation(parent));
5045f39d397SChris Mason 		btrfs_set_node_blockptr(parent, parent_slot,
505db94535dSChris Mason 					cow->start);
50674493f7aSChris Mason 		btrfs_set_node_ptr_generation(parent, parent_slot,
50774493f7aSChris Mason 					      trans->transid);
5086702ed49SChris Mason 		btrfs_mark_buffer_dirty(parent);
509f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, buf, parent_start,
51066d7e7f0SArne Jansen 				      last_ref, 1);
5116702ed49SChris Mason 	}
512925baeddSChris Mason 	if (unlock_orig)
513925baeddSChris Mason 		btrfs_tree_unlock(buf);
5145f39d397SChris Mason 	free_extent_buffer(buf);
5156702ed49SChris Mason 	btrfs_mark_buffer_dirty(cow);
5166702ed49SChris Mason 	*cow_ret = cow;
5176702ed49SChris Mason 	return 0;
5186702ed49SChris Mason }
5196702ed49SChris Mason 
5205d4f98a2SYan Zheng static inline int should_cow_block(struct btrfs_trans_handle *trans,
5215d4f98a2SYan Zheng 				   struct btrfs_root *root,
5225d4f98a2SYan Zheng 				   struct extent_buffer *buf)
5235d4f98a2SYan Zheng {
524f1ebcc74SLiu Bo 	/* ensure we can see the force_cow */
525f1ebcc74SLiu Bo 	smp_rmb();
526f1ebcc74SLiu Bo 
527f1ebcc74SLiu Bo 	/*
528f1ebcc74SLiu Bo 	 * We do not need to cow a block if
529f1ebcc74SLiu Bo 	 * 1) this block is not created or changed in this transaction;
530f1ebcc74SLiu Bo 	 * 2) this block does not belong to TREE_RELOC tree;
531f1ebcc74SLiu Bo 	 * 3) the root is not forced COW.
532f1ebcc74SLiu Bo 	 *
533f1ebcc74SLiu Bo 	 * What is forced COW:
534f1ebcc74SLiu Bo 	 *    when we create snapshot during commiting the transaction,
535f1ebcc74SLiu Bo 	 *    after we've finished coping src root, we must COW the shared
536f1ebcc74SLiu Bo 	 *    block to ensure the metadata consistency.
537f1ebcc74SLiu Bo 	 */
5385d4f98a2SYan Zheng 	if (btrfs_header_generation(buf) == trans->transid &&
5395d4f98a2SYan Zheng 	    !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
5405d4f98a2SYan Zheng 	    !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
541f1ebcc74SLiu Bo 	      btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
542f1ebcc74SLiu Bo 	    !root->force_cow)
5435d4f98a2SYan Zheng 		return 0;
5445d4f98a2SYan Zheng 	return 1;
5455d4f98a2SYan Zheng }
5465d4f98a2SYan Zheng 
547d352ac68SChris Mason /*
548d352ac68SChris Mason  * cows a single block, see __btrfs_cow_block for the real work.
549d352ac68SChris Mason  * This version of it has extra checks so that a block isn't cow'd more than
550d352ac68SChris Mason  * once per transaction, as long as it hasn't been written yet
551d352ac68SChris Mason  */
552d397712bSChris Mason noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
5535f39d397SChris Mason 		    struct btrfs_root *root, struct extent_buffer *buf,
5545f39d397SChris Mason 		    struct extent_buffer *parent, int parent_slot,
5559fa8cfe7SChris Mason 		    struct extent_buffer **cow_ret)
55602217ed2SChris Mason {
5576702ed49SChris Mason 	u64 search_start;
558f510cfecSChris Mason 	int ret;
559dc17ff8fSChris Mason 
560ccd467d6SChris Mason 	if (trans->transaction != root->fs_info->running_transaction) {
561d397712bSChris Mason 		printk(KERN_CRIT "trans %llu running %llu\n",
562d397712bSChris Mason 		       (unsigned long long)trans->transid,
563d397712bSChris Mason 		       (unsigned long long)
564ccd467d6SChris Mason 		       root->fs_info->running_transaction->transid);
565ccd467d6SChris Mason 		WARN_ON(1);
566ccd467d6SChris Mason 	}
567ccd467d6SChris Mason 	if (trans->transid != root->fs_info->generation) {
568d397712bSChris Mason 		printk(KERN_CRIT "trans %llu running %llu\n",
569d397712bSChris Mason 		       (unsigned long long)trans->transid,
570d397712bSChris Mason 		       (unsigned long long)root->fs_info->generation);
571ccd467d6SChris Mason 		WARN_ON(1);
572ccd467d6SChris Mason 	}
573dc17ff8fSChris Mason 
5745d4f98a2SYan Zheng 	if (!should_cow_block(trans, root, buf)) {
57502217ed2SChris Mason 		*cow_ret = buf;
57602217ed2SChris Mason 		return 0;
57702217ed2SChris Mason 	}
578c487685dSChris Mason 
5790b86a832SChris Mason 	search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
580b4ce94deSChris Mason 
581b4ce94deSChris Mason 	if (parent)
582b4ce94deSChris Mason 		btrfs_set_lock_blocking(parent);
583b4ce94deSChris Mason 	btrfs_set_lock_blocking(buf);
584b4ce94deSChris Mason 
585f510cfecSChris Mason 	ret = __btrfs_cow_block(trans, root, buf, parent,
5869fa8cfe7SChris Mason 				 parent_slot, cow_ret, search_start, 0);
5871abe9b8aSliubo 
5881abe9b8aSliubo 	trace_btrfs_cow_block(root, buf, *cow_ret);
5891abe9b8aSliubo 
590f510cfecSChris Mason 	return ret;
5912c90e5d6SChris Mason }
5926702ed49SChris Mason 
593d352ac68SChris Mason /*
594d352ac68SChris Mason  * helper function for defrag to decide if two blocks pointed to by a
595d352ac68SChris Mason  * node are actually close by
596d352ac68SChris Mason  */
5976b80053dSChris Mason static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
5986702ed49SChris Mason {
5996b80053dSChris Mason 	if (blocknr < other && other - (blocknr + blocksize) < 32768)
6006702ed49SChris Mason 		return 1;
6016b80053dSChris Mason 	if (blocknr > other && blocknr - (other + blocksize) < 32768)
6026702ed49SChris Mason 		return 1;
60302217ed2SChris Mason 	return 0;
60402217ed2SChris Mason }
60502217ed2SChris Mason 
606081e9573SChris Mason /*
607081e9573SChris Mason  * compare two keys in a memcmp fashion
608081e9573SChris Mason  */
609081e9573SChris Mason static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
610081e9573SChris Mason {
611081e9573SChris Mason 	struct btrfs_key k1;
612081e9573SChris Mason 
613081e9573SChris Mason 	btrfs_disk_key_to_cpu(&k1, disk);
614081e9573SChris Mason 
61520736abaSDiego Calleja 	return btrfs_comp_cpu_keys(&k1, k2);
616081e9573SChris Mason }
617081e9573SChris Mason 
618f3465ca4SJosef Bacik /*
619f3465ca4SJosef Bacik  * same as comp_keys only with two btrfs_key's
620f3465ca4SJosef Bacik  */
6215d4f98a2SYan Zheng int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
622f3465ca4SJosef Bacik {
623f3465ca4SJosef Bacik 	if (k1->objectid > k2->objectid)
624f3465ca4SJosef Bacik 		return 1;
625f3465ca4SJosef Bacik 	if (k1->objectid < k2->objectid)
626f3465ca4SJosef Bacik 		return -1;
627f3465ca4SJosef Bacik 	if (k1->type > k2->type)
628f3465ca4SJosef Bacik 		return 1;
629f3465ca4SJosef Bacik 	if (k1->type < k2->type)
630f3465ca4SJosef Bacik 		return -1;
631f3465ca4SJosef Bacik 	if (k1->offset > k2->offset)
632f3465ca4SJosef Bacik 		return 1;
633f3465ca4SJosef Bacik 	if (k1->offset < k2->offset)
634f3465ca4SJosef Bacik 		return -1;
635f3465ca4SJosef Bacik 	return 0;
636f3465ca4SJosef Bacik }
637081e9573SChris Mason 
638d352ac68SChris Mason /*
639d352ac68SChris Mason  * this is used by the defrag code to go through all the
640d352ac68SChris Mason  * leaves pointed to by a node and reallocate them so that
641d352ac68SChris Mason  * disk order is close to key order
642d352ac68SChris Mason  */
6436702ed49SChris Mason int btrfs_realloc_node(struct btrfs_trans_handle *trans,
6445f39d397SChris Mason 		       struct btrfs_root *root, struct extent_buffer *parent,
645a6b6e75eSChris Mason 		       int start_slot, int cache_only, u64 *last_ret,
646a6b6e75eSChris Mason 		       struct btrfs_key *progress)
6476702ed49SChris Mason {
6486b80053dSChris Mason 	struct extent_buffer *cur;
6496702ed49SChris Mason 	u64 blocknr;
650ca7a79adSChris Mason 	u64 gen;
651e9d0b13bSChris Mason 	u64 search_start = *last_ret;
652e9d0b13bSChris Mason 	u64 last_block = 0;
6536702ed49SChris Mason 	u64 other;
6546702ed49SChris Mason 	u32 parent_nritems;
6556702ed49SChris Mason 	int end_slot;
6566702ed49SChris Mason 	int i;
6576702ed49SChris Mason 	int err = 0;
658f2183bdeSChris Mason 	int parent_level;
6596b80053dSChris Mason 	int uptodate;
6606b80053dSChris Mason 	u32 blocksize;
661081e9573SChris Mason 	int progress_passed = 0;
662081e9573SChris Mason 	struct btrfs_disk_key disk_key;
6636702ed49SChris Mason 
6645708b959SChris Mason 	parent_level = btrfs_header_level(parent);
6655708b959SChris Mason 	if (cache_only && parent_level != 1)
6665708b959SChris Mason 		return 0;
6675708b959SChris Mason 
668d397712bSChris Mason 	if (trans->transaction != root->fs_info->running_transaction)
6696702ed49SChris Mason 		WARN_ON(1);
670d397712bSChris Mason 	if (trans->transid != root->fs_info->generation)
6716702ed49SChris Mason 		WARN_ON(1);
67286479a04SChris Mason 
6736b80053dSChris Mason 	parent_nritems = btrfs_header_nritems(parent);
6746b80053dSChris Mason 	blocksize = btrfs_level_size(root, parent_level - 1);
6756702ed49SChris Mason 	end_slot = parent_nritems;
6766702ed49SChris Mason 
6776702ed49SChris Mason 	if (parent_nritems == 1)
6786702ed49SChris Mason 		return 0;
6796702ed49SChris Mason 
680b4ce94deSChris Mason 	btrfs_set_lock_blocking(parent);
681b4ce94deSChris Mason 
6826702ed49SChris Mason 	for (i = start_slot; i < end_slot; i++) {
6836702ed49SChris Mason 		int close = 1;
684a6b6e75eSChris Mason 
685081e9573SChris Mason 		btrfs_node_key(parent, &disk_key, i);
686081e9573SChris Mason 		if (!progress_passed && comp_keys(&disk_key, progress) < 0)
687081e9573SChris Mason 			continue;
688081e9573SChris Mason 
689081e9573SChris Mason 		progress_passed = 1;
6906b80053dSChris Mason 		blocknr = btrfs_node_blockptr(parent, i);
691ca7a79adSChris Mason 		gen = btrfs_node_ptr_generation(parent, i);
692e9d0b13bSChris Mason 		if (last_block == 0)
693e9d0b13bSChris Mason 			last_block = blocknr;
6945708b959SChris Mason 
6956702ed49SChris Mason 		if (i > 0) {
6966b80053dSChris Mason 			other = btrfs_node_blockptr(parent, i - 1);
6976b80053dSChris Mason 			close = close_blocks(blocknr, other, blocksize);
6986702ed49SChris Mason 		}
6990ef3e66bSChris Mason 		if (!close && i < end_slot - 2) {
7006b80053dSChris Mason 			other = btrfs_node_blockptr(parent, i + 1);
7016b80053dSChris Mason 			close = close_blocks(blocknr, other, blocksize);
7026702ed49SChris Mason 		}
703e9d0b13bSChris Mason 		if (close) {
704e9d0b13bSChris Mason 			last_block = blocknr;
7056702ed49SChris Mason 			continue;
706e9d0b13bSChris Mason 		}
7076702ed49SChris Mason 
7086b80053dSChris Mason 		cur = btrfs_find_tree_block(root, blocknr, blocksize);
7096b80053dSChris Mason 		if (cur)
7101259ab75SChris Mason 			uptodate = btrfs_buffer_uptodate(cur, gen);
7116b80053dSChris Mason 		else
7126b80053dSChris Mason 			uptodate = 0;
7135708b959SChris Mason 		if (!cur || !uptodate) {
7146702ed49SChris Mason 			if (cache_only) {
7156b80053dSChris Mason 				free_extent_buffer(cur);
7166702ed49SChris Mason 				continue;
7176702ed49SChris Mason 			}
7186b80053dSChris Mason 			if (!cur) {
7196b80053dSChris Mason 				cur = read_tree_block(root, blocknr,
720ca7a79adSChris Mason 							 blocksize, gen);
72197d9a8a4STsutomu Itoh 				if (!cur)
72297d9a8a4STsutomu Itoh 					return -EIO;
7236b80053dSChris Mason 			} else if (!uptodate) {
724ca7a79adSChris Mason 				btrfs_read_buffer(cur, gen);
7256702ed49SChris Mason 			}
726f2183bdeSChris Mason 		}
727e9d0b13bSChris Mason 		if (search_start == 0)
7286b80053dSChris Mason 			search_start = last_block;
729e9d0b13bSChris Mason 
730e7a84565SChris Mason 		btrfs_tree_lock(cur);
731b4ce94deSChris Mason 		btrfs_set_lock_blocking(cur);
7326b80053dSChris Mason 		err = __btrfs_cow_block(trans, root, cur, parent, i,
733e7a84565SChris Mason 					&cur, search_start,
7346b80053dSChris Mason 					min(16 * blocksize,
7359fa8cfe7SChris Mason 					    (end_slot - i) * blocksize));
736252c38f0SYan 		if (err) {
737e7a84565SChris Mason 			btrfs_tree_unlock(cur);
7386b80053dSChris Mason 			free_extent_buffer(cur);
7396702ed49SChris Mason 			break;
740252c38f0SYan 		}
741e7a84565SChris Mason 		search_start = cur->start;
742e7a84565SChris Mason 		last_block = cur->start;
743f2183bdeSChris Mason 		*last_ret = search_start;
744e7a84565SChris Mason 		btrfs_tree_unlock(cur);
745e7a84565SChris Mason 		free_extent_buffer(cur);
7466702ed49SChris Mason 	}
7476702ed49SChris Mason 	return err;
7486702ed49SChris Mason }
7496702ed49SChris Mason 
75074123bd7SChris Mason /*
75174123bd7SChris Mason  * The leaf data grows from end-to-front in the node.
75274123bd7SChris Mason  * this returns the address of the start of the last item,
75374123bd7SChris Mason  * which is the stop of the leaf data stack
75474123bd7SChris Mason  */
755123abc88SChris Mason static inline unsigned int leaf_data_end(struct btrfs_root *root,
7565f39d397SChris Mason 					 struct extent_buffer *leaf)
757be0e5c09SChris Mason {
7585f39d397SChris Mason 	u32 nr = btrfs_header_nritems(leaf);
759be0e5c09SChris Mason 	if (nr == 0)
760123abc88SChris Mason 		return BTRFS_LEAF_DATA_SIZE(root);
7615f39d397SChris Mason 	return btrfs_item_offset_nr(leaf, nr - 1);
762be0e5c09SChris Mason }
763be0e5c09SChris Mason 
764aa5d6bedSChris Mason 
76574123bd7SChris Mason /*
7665f39d397SChris Mason  * search for key in the extent_buffer.  The items start at offset p,
7675f39d397SChris Mason  * and they are item_size apart.  There are 'max' items in p.
7685f39d397SChris Mason  *
76974123bd7SChris Mason  * the slot in the array is returned via slot, and it points to
77074123bd7SChris Mason  * the place where you would insert key if it is not found in
77174123bd7SChris Mason  * the array.
77274123bd7SChris Mason  *
77374123bd7SChris Mason  * slot may point to max if the key is bigger than all of the keys
77474123bd7SChris Mason  */
775e02119d5SChris Mason static noinline int generic_bin_search(struct extent_buffer *eb,
776e02119d5SChris Mason 				       unsigned long p,
7775f39d397SChris Mason 				       int item_size, struct btrfs_key *key,
778be0e5c09SChris Mason 				       int max, int *slot)
779be0e5c09SChris Mason {
780be0e5c09SChris Mason 	int low = 0;
781be0e5c09SChris Mason 	int high = max;
782be0e5c09SChris Mason 	int mid;
783be0e5c09SChris Mason 	int ret;
784479965d6SChris Mason 	struct btrfs_disk_key *tmp = NULL;
7855f39d397SChris Mason 	struct btrfs_disk_key unaligned;
7865f39d397SChris Mason 	unsigned long offset;
7875f39d397SChris Mason 	char *kaddr = NULL;
7885f39d397SChris Mason 	unsigned long map_start = 0;
7895f39d397SChris Mason 	unsigned long map_len = 0;
790479965d6SChris Mason 	int err;
791be0e5c09SChris Mason 
792be0e5c09SChris Mason 	while (low < high) {
793be0e5c09SChris Mason 		mid = (low + high) / 2;
7945f39d397SChris Mason 		offset = p + mid * item_size;
7955f39d397SChris Mason 
796a6591715SChris Mason 		if (!kaddr || offset < map_start ||
7975f39d397SChris Mason 		    (offset + sizeof(struct btrfs_disk_key)) >
7985f39d397SChris Mason 		    map_start + map_len) {
799934d375bSChris Mason 
800934d375bSChris Mason 			err = map_private_extent_buffer(eb, offset,
801479965d6SChris Mason 						sizeof(struct btrfs_disk_key),
802a6591715SChris Mason 						&kaddr, &map_start, &map_len);
8035f39d397SChris Mason 
804479965d6SChris Mason 			if (!err) {
805479965d6SChris Mason 				tmp = (struct btrfs_disk_key *)(kaddr + offset -
806479965d6SChris Mason 							map_start);
807479965d6SChris Mason 			} else {
8085f39d397SChris Mason 				read_extent_buffer(eb, &unaligned,
8095f39d397SChris Mason 						   offset, sizeof(unaligned));
8105f39d397SChris Mason 				tmp = &unaligned;
811479965d6SChris Mason 			}
812479965d6SChris Mason 
8135f39d397SChris Mason 		} else {
8145f39d397SChris Mason 			tmp = (struct btrfs_disk_key *)(kaddr + offset -
8155f39d397SChris Mason 							map_start);
8165f39d397SChris Mason 		}
817be0e5c09SChris Mason 		ret = comp_keys(tmp, key);
818be0e5c09SChris Mason 
819be0e5c09SChris Mason 		if (ret < 0)
820be0e5c09SChris Mason 			low = mid + 1;
821be0e5c09SChris Mason 		else if (ret > 0)
822be0e5c09SChris Mason 			high = mid;
823be0e5c09SChris Mason 		else {
824be0e5c09SChris Mason 			*slot = mid;
825be0e5c09SChris Mason 			return 0;
826be0e5c09SChris Mason 		}
827be0e5c09SChris Mason 	}
828be0e5c09SChris Mason 	*slot = low;
829be0e5c09SChris Mason 	return 1;
830be0e5c09SChris Mason }
831be0e5c09SChris Mason 
83297571fd0SChris Mason /*
83397571fd0SChris Mason  * simple bin_search frontend that does the right thing for
83497571fd0SChris Mason  * leaves vs nodes
83597571fd0SChris Mason  */
8365f39d397SChris Mason static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
8375f39d397SChris Mason 		      int level, int *slot)
838be0e5c09SChris Mason {
8395f39d397SChris Mason 	if (level == 0) {
8405f39d397SChris Mason 		return generic_bin_search(eb,
8415f39d397SChris Mason 					  offsetof(struct btrfs_leaf, items),
8420783fcfcSChris Mason 					  sizeof(struct btrfs_item),
8435f39d397SChris Mason 					  key, btrfs_header_nritems(eb),
8447518a238SChris Mason 					  slot);
845be0e5c09SChris Mason 	} else {
8465f39d397SChris Mason 		return generic_bin_search(eb,
8475f39d397SChris Mason 					  offsetof(struct btrfs_node, ptrs),
848123abc88SChris Mason 					  sizeof(struct btrfs_key_ptr),
8495f39d397SChris Mason 					  key, btrfs_header_nritems(eb),
8507518a238SChris Mason 					  slot);
851be0e5c09SChris Mason 	}
852be0e5c09SChris Mason 	return -1;
853be0e5c09SChris Mason }
854be0e5c09SChris Mason 
8555d4f98a2SYan Zheng int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
8565d4f98a2SYan Zheng 		     int level, int *slot)
8575d4f98a2SYan Zheng {
8585d4f98a2SYan Zheng 	return bin_search(eb, key, level, slot);
8595d4f98a2SYan Zheng }
8605d4f98a2SYan Zheng 
861f0486c68SYan, Zheng static void root_add_used(struct btrfs_root *root, u32 size)
862f0486c68SYan, Zheng {
863f0486c68SYan, Zheng 	spin_lock(&root->accounting_lock);
864f0486c68SYan, Zheng 	btrfs_set_root_used(&root->root_item,
865f0486c68SYan, Zheng 			    btrfs_root_used(&root->root_item) + size);
866f0486c68SYan, Zheng 	spin_unlock(&root->accounting_lock);
867f0486c68SYan, Zheng }
868f0486c68SYan, Zheng 
869f0486c68SYan, Zheng static void root_sub_used(struct btrfs_root *root, u32 size)
870f0486c68SYan, Zheng {
871f0486c68SYan, Zheng 	spin_lock(&root->accounting_lock);
872f0486c68SYan, Zheng 	btrfs_set_root_used(&root->root_item,
873f0486c68SYan, Zheng 			    btrfs_root_used(&root->root_item) - size);
874f0486c68SYan, Zheng 	spin_unlock(&root->accounting_lock);
875f0486c68SYan, Zheng }
876f0486c68SYan, Zheng 
877d352ac68SChris Mason /* given a node and slot number, this reads the blocks it points to.  The
878d352ac68SChris Mason  * extent buffer is returned with a reference taken (but unlocked).
879d352ac68SChris Mason  * NULL is returned on error.
880d352ac68SChris Mason  */
881e02119d5SChris Mason static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
8825f39d397SChris Mason 				   struct extent_buffer *parent, int slot)
883bb803951SChris Mason {
884ca7a79adSChris Mason 	int level = btrfs_header_level(parent);
885bb803951SChris Mason 	if (slot < 0)
886bb803951SChris Mason 		return NULL;
8875f39d397SChris Mason 	if (slot >= btrfs_header_nritems(parent))
888bb803951SChris Mason 		return NULL;
889ca7a79adSChris Mason 
890ca7a79adSChris Mason 	BUG_ON(level == 0);
891ca7a79adSChris Mason 
892db94535dSChris Mason 	return read_tree_block(root, btrfs_node_blockptr(parent, slot),
893ca7a79adSChris Mason 		       btrfs_level_size(root, level - 1),
894ca7a79adSChris Mason 		       btrfs_node_ptr_generation(parent, slot));
895bb803951SChris Mason }
896bb803951SChris Mason 
897d352ac68SChris Mason /*
898d352ac68SChris Mason  * node level balancing, used to make sure nodes are in proper order for
899d352ac68SChris Mason  * item deletion.  We balance from the top down, so we have to make sure
900d352ac68SChris Mason  * that a deletion won't leave an node completely empty later on.
901d352ac68SChris Mason  */
902e02119d5SChris Mason static noinline int balance_level(struct btrfs_trans_handle *trans,
90398ed5174SChris Mason 			 struct btrfs_root *root,
90498ed5174SChris Mason 			 struct btrfs_path *path, int level)
905bb803951SChris Mason {
9065f39d397SChris Mason 	struct extent_buffer *right = NULL;
9075f39d397SChris Mason 	struct extent_buffer *mid;
9085f39d397SChris Mason 	struct extent_buffer *left = NULL;
9095f39d397SChris Mason 	struct extent_buffer *parent = NULL;
910bb803951SChris Mason 	int ret = 0;
911bb803951SChris Mason 	int wret;
912bb803951SChris Mason 	int pslot;
913bb803951SChris Mason 	int orig_slot = path->slots[level];
91479f95c82SChris Mason 	u64 orig_ptr;
915bb803951SChris Mason 
916bb803951SChris Mason 	if (level == 0)
917bb803951SChris Mason 		return 0;
918bb803951SChris Mason 
9195f39d397SChris Mason 	mid = path->nodes[level];
920b4ce94deSChris Mason 
921bd681513SChris Mason 	WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
922bd681513SChris Mason 		path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
9237bb86316SChris Mason 	WARN_ON(btrfs_header_generation(mid) != trans->transid);
9247bb86316SChris Mason 
9251d4f8a0cSChris Mason 	orig_ptr = btrfs_node_blockptr(mid, orig_slot);
92679f95c82SChris Mason 
927a05a9bb1SLi Zefan 	if (level < BTRFS_MAX_LEVEL - 1) {
9285f39d397SChris Mason 		parent = path->nodes[level + 1];
929bb803951SChris Mason 		pslot = path->slots[level + 1];
930a05a9bb1SLi Zefan 	}
931bb803951SChris Mason 
93240689478SChris Mason 	/*
93340689478SChris Mason 	 * deal with the case where there is only one pointer in the root
93440689478SChris Mason 	 * by promoting the node below to a root
93540689478SChris Mason 	 */
9365f39d397SChris Mason 	if (!parent) {
9375f39d397SChris Mason 		struct extent_buffer *child;
938bb803951SChris Mason 
9395f39d397SChris Mason 		if (btrfs_header_nritems(mid) != 1)
940bb803951SChris Mason 			return 0;
941bb803951SChris Mason 
942bb803951SChris Mason 		/* promote the child to a root */
9435f39d397SChris Mason 		child = read_node_slot(root, mid, 0);
9447951f3ceSJeff Mahoney 		BUG_ON(!child);
945925baeddSChris Mason 		btrfs_tree_lock(child);
946b4ce94deSChris Mason 		btrfs_set_lock_blocking(child);
9479fa8cfe7SChris Mason 		ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
948f0486c68SYan, Zheng 		if (ret) {
949f0486c68SYan, Zheng 			btrfs_tree_unlock(child);
950f0486c68SYan, Zheng 			free_extent_buffer(child);
951f0486c68SYan, Zheng 			goto enospc;
952f0486c68SYan, Zheng 		}
9532f375ab9SYan 
954240f62c8SChris Mason 		rcu_assign_pointer(root->node, child);
955925baeddSChris Mason 
9560b86a832SChris Mason 		add_root_to_dirty_list(root);
957925baeddSChris Mason 		btrfs_tree_unlock(child);
958b4ce94deSChris Mason 
959925baeddSChris Mason 		path->locks[level] = 0;
960bb803951SChris Mason 		path->nodes[level] = NULL;
9615f39d397SChris Mason 		clean_tree_block(trans, root, mid);
962925baeddSChris Mason 		btrfs_tree_unlock(mid);
963bb803951SChris Mason 		/* once for the path */
9645f39d397SChris Mason 		free_extent_buffer(mid);
965f0486c68SYan, Zheng 
966f0486c68SYan, Zheng 		root_sub_used(root, mid->len);
96766d7e7f0SArne Jansen 		btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
968bb803951SChris Mason 		/* once for the root ptr */
9695f39d397SChris Mason 		free_extent_buffer(mid);
970f0486c68SYan, Zheng 		return 0;
971bb803951SChris Mason 	}
9725f39d397SChris Mason 	if (btrfs_header_nritems(mid) >
973123abc88SChris Mason 	    BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
974bb803951SChris Mason 		return 0;
975bb803951SChris Mason 
976559af821SAndi Kleen 	btrfs_header_nritems(mid);
97754aa1f4dSChris Mason 
9785f39d397SChris Mason 	left = read_node_slot(root, parent, pslot - 1);
9795f39d397SChris Mason 	if (left) {
980925baeddSChris Mason 		btrfs_tree_lock(left);
981b4ce94deSChris Mason 		btrfs_set_lock_blocking(left);
9825f39d397SChris Mason 		wret = btrfs_cow_block(trans, root, left,
9839fa8cfe7SChris Mason 				       parent, pslot - 1, &left);
98454aa1f4dSChris Mason 		if (wret) {
98554aa1f4dSChris Mason 			ret = wret;
98654aa1f4dSChris Mason 			goto enospc;
98754aa1f4dSChris Mason 		}
9882cc58cf2SChris Mason 	}
9895f39d397SChris Mason 	right = read_node_slot(root, parent, pslot + 1);
9905f39d397SChris Mason 	if (right) {
991925baeddSChris Mason 		btrfs_tree_lock(right);
992b4ce94deSChris Mason 		btrfs_set_lock_blocking(right);
9935f39d397SChris Mason 		wret = btrfs_cow_block(trans, root, right,
9949fa8cfe7SChris Mason 				       parent, pslot + 1, &right);
9952cc58cf2SChris Mason 		if (wret) {
9962cc58cf2SChris Mason 			ret = wret;
9972cc58cf2SChris Mason 			goto enospc;
9982cc58cf2SChris Mason 		}
9992cc58cf2SChris Mason 	}
10002cc58cf2SChris Mason 
10012cc58cf2SChris Mason 	/* first, try to make some room in the middle buffer */
10025f39d397SChris Mason 	if (left) {
10035f39d397SChris Mason 		orig_slot += btrfs_header_nritems(left);
1004bce4eae9SChris Mason 		wret = push_node_left(trans, root, left, mid, 1);
100579f95c82SChris Mason 		if (wret < 0)
100679f95c82SChris Mason 			ret = wret;
1007559af821SAndi Kleen 		btrfs_header_nritems(mid);
1008bb803951SChris Mason 	}
100979f95c82SChris Mason 
101079f95c82SChris Mason 	/*
101179f95c82SChris Mason 	 * then try to empty the right most buffer into the middle
101279f95c82SChris Mason 	 */
10135f39d397SChris Mason 	if (right) {
1014971a1f66SChris Mason 		wret = push_node_left(trans, root, mid, right, 1);
101554aa1f4dSChris Mason 		if (wret < 0 && wret != -ENOSPC)
101679f95c82SChris Mason 			ret = wret;
10175f39d397SChris Mason 		if (btrfs_header_nritems(right) == 0) {
10185f39d397SChris Mason 			clean_tree_block(trans, root, right);
1019925baeddSChris Mason 			btrfs_tree_unlock(right);
1020143bede5SJeff Mahoney 			del_ptr(trans, root, path, level + 1, pslot + 1);
1021f0486c68SYan, Zheng 			root_sub_used(root, right->len);
102266d7e7f0SArne Jansen 			btrfs_free_tree_block(trans, root, right, 0, 1, 0);
1023f0486c68SYan, Zheng 			free_extent_buffer(right);
1024f0486c68SYan, Zheng 			right = NULL;
1025bb803951SChris Mason 		} else {
10265f39d397SChris Mason 			struct btrfs_disk_key right_key;
10275f39d397SChris Mason 			btrfs_node_key(right, &right_key, 0);
10285f39d397SChris Mason 			btrfs_set_node_key(parent, &right_key, pslot + 1);
10295f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
1030bb803951SChris Mason 		}
1031bb803951SChris Mason 	}
10325f39d397SChris Mason 	if (btrfs_header_nritems(mid) == 1) {
103379f95c82SChris Mason 		/*
103479f95c82SChris Mason 		 * we're not allowed to leave a node with one item in the
103579f95c82SChris Mason 		 * tree during a delete.  A deletion from lower in the tree
103679f95c82SChris Mason 		 * could try to delete the only pointer in this node.
103779f95c82SChris Mason 		 * So, pull some keys from the left.
103879f95c82SChris Mason 		 * There has to be a left pointer at this point because
103979f95c82SChris Mason 		 * otherwise we would have pulled some pointers from the
104079f95c82SChris Mason 		 * right
104179f95c82SChris Mason 		 */
10425f39d397SChris Mason 		BUG_ON(!left);
10435f39d397SChris Mason 		wret = balance_node_right(trans, root, mid, left);
104454aa1f4dSChris Mason 		if (wret < 0) {
104579f95c82SChris Mason 			ret = wret;
104654aa1f4dSChris Mason 			goto enospc;
104754aa1f4dSChris Mason 		}
1048bce4eae9SChris Mason 		if (wret == 1) {
1049bce4eae9SChris Mason 			wret = push_node_left(trans, root, left, mid, 1);
1050bce4eae9SChris Mason 			if (wret < 0)
1051bce4eae9SChris Mason 				ret = wret;
1052bce4eae9SChris Mason 		}
105379f95c82SChris Mason 		BUG_ON(wret == 1);
105479f95c82SChris Mason 	}
10555f39d397SChris Mason 	if (btrfs_header_nritems(mid) == 0) {
10565f39d397SChris Mason 		clean_tree_block(trans, root, mid);
1057925baeddSChris Mason 		btrfs_tree_unlock(mid);
1058143bede5SJeff Mahoney 		del_ptr(trans, root, path, level + 1, pslot);
1059f0486c68SYan, Zheng 		root_sub_used(root, mid->len);
106066d7e7f0SArne Jansen 		btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
1061f0486c68SYan, Zheng 		free_extent_buffer(mid);
1062f0486c68SYan, Zheng 		mid = NULL;
106379f95c82SChris Mason 	} else {
106479f95c82SChris Mason 		/* update the parent key to reflect our changes */
10655f39d397SChris Mason 		struct btrfs_disk_key mid_key;
10665f39d397SChris Mason 		btrfs_node_key(mid, &mid_key, 0);
10675f39d397SChris Mason 		btrfs_set_node_key(parent, &mid_key, pslot);
10685f39d397SChris Mason 		btrfs_mark_buffer_dirty(parent);
106979f95c82SChris Mason 	}
1070bb803951SChris Mason 
107179f95c82SChris Mason 	/* update the path */
10725f39d397SChris Mason 	if (left) {
10735f39d397SChris Mason 		if (btrfs_header_nritems(left) > orig_slot) {
10745f39d397SChris Mason 			extent_buffer_get(left);
1075925baeddSChris Mason 			/* left was locked after cow */
10765f39d397SChris Mason 			path->nodes[level] = left;
1077bb803951SChris Mason 			path->slots[level + 1] -= 1;
1078bb803951SChris Mason 			path->slots[level] = orig_slot;
1079925baeddSChris Mason 			if (mid) {
1080925baeddSChris Mason 				btrfs_tree_unlock(mid);
10815f39d397SChris Mason 				free_extent_buffer(mid);
1082925baeddSChris Mason 			}
1083bb803951SChris Mason 		} else {
10845f39d397SChris Mason 			orig_slot -= btrfs_header_nritems(left);
1085bb803951SChris Mason 			path->slots[level] = orig_slot;
1086bb803951SChris Mason 		}
1087bb803951SChris Mason 	}
108879f95c82SChris Mason 	/* double check we haven't messed things up */
1089e20d96d6SChris Mason 	if (orig_ptr !=
10905f39d397SChris Mason 	    btrfs_node_blockptr(path->nodes[level], path->slots[level]))
109179f95c82SChris Mason 		BUG();
109254aa1f4dSChris Mason enospc:
1093925baeddSChris Mason 	if (right) {
1094925baeddSChris Mason 		btrfs_tree_unlock(right);
10955f39d397SChris Mason 		free_extent_buffer(right);
1096925baeddSChris Mason 	}
1097925baeddSChris Mason 	if (left) {
1098925baeddSChris Mason 		if (path->nodes[level] != left)
1099925baeddSChris Mason 			btrfs_tree_unlock(left);
11005f39d397SChris Mason 		free_extent_buffer(left);
1101925baeddSChris Mason 	}
1102bb803951SChris Mason 	return ret;
1103bb803951SChris Mason }
1104bb803951SChris Mason 
1105d352ac68SChris Mason /* Node balancing for insertion.  Here we only split or push nodes around
1106d352ac68SChris Mason  * when they are completely full.  This is also done top down, so we
1107d352ac68SChris Mason  * have to be pessimistic.
1108d352ac68SChris Mason  */
1109d397712bSChris Mason static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1110e66f709bSChris Mason 					  struct btrfs_root *root,
1111e66f709bSChris Mason 					  struct btrfs_path *path, int level)
1112e66f709bSChris Mason {
11135f39d397SChris Mason 	struct extent_buffer *right = NULL;
11145f39d397SChris Mason 	struct extent_buffer *mid;
11155f39d397SChris Mason 	struct extent_buffer *left = NULL;
11165f39d397SChris Mason 	struct extent_buffer *parent = NULL;
1117e66f709bSChris Mason 	int ret = 0;
1118e66f709bSChris Mason 	int wret;
1119e66f709bSChris Mason 	int pslot;
1120e66f709bSChris Mason 	int orig_slot = path->slots[level];
1121e66f709bSChris Mason 
1122e66f709bSChris Mason 	if (level == 0)
1123e66f709bSChris Mason 		return 1;
1124e66f709bSChris Mason 
11255f39d397SChris Mason 	mid = path->nodes[level];
11267bb86316SChris Mason 	WARN_ON(btrfs_header_generation(mid) != trans->transid);
1127e66f709bSChris Mason 
1128a05a9bb1SLi Zefan 	if (level < BTRFS_MAX_LEVEL - 1) {
11295f39d397SChris Mason 		parent = path->nodes[level + 1];
1130e66f709bSChris Mason 		pslot = path->slots[level + 1];
1131a05a9bb1SLi Zefan 	}
1132e66f709bSChris Mason 
11335f39d397SChris Mason 	if (!parent)
1134e66f709bSChris Mason 		return 1;
1135e66f709bSChris Mason 
11365f39d397SChris Mason 	left = read_node_slot(root, parent, pslot - 1);
1137e66f709bSChris Mason 
1138e66f709bSChris Mason 	/* first, try to make some room in the middle buffer */
11395f39d397SChris Mason 	if (left) {
1140e66f709bSChris Mason 		u32 left_nr;
1141925baeddSChris Mason 
1142925baeddSChris Mason 		btrfs_tree_lock(left);
1143b4ce94deSChris Mason 		btrfs_set_lock_blocking(left);
1144b4ce94deSChris Mason 
11455f39d397SChris Mason 		left_nr = btrfs_header_nritems(left);
114633ade1f8SChris Mason 		if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
114733ade1f8SChris Mason 			wret = 1;
114833ade1f8SChris Mason 		} else {
11495f39d397SChris Mason 			ret = btrfs_cow_block(trans, root, left, parent,
11509fa8cfe7SChris Mason 					      pslot - 1, &left);
115154aa1f4dSChris Mason 			if (ret)
115254aa1f4dSChris Mason 				wret = 1;
115354aa1f4dSChris Mason 			else {
115454aa1f4dSChris Mason 				wret = push_node_left(trans, root,
1155971a1f66SChris Mason 						      left, mid, 0);
115654aa1f4dSChris Mason 			}
115733ade1f8SChris Mason 		}
1158e66f709bSChris Mason 		if (wret < 0)
1159e66f709bSChris Mason 			ret = wret;
1160e66f709bSChris Mason 		if (wret == 0) {
11615f39d397SChris Mason 			struct btrfs_disk_key disk_key;
1162e66f709bSChris Mason 			orig_slot += left_nr;
11635f39d397SChris Mason 			btrfs_node_key(mid, &disk_key, 0);
11645f39d397SChris Mason 			btrfs_set_node_key(parent, &disk_key, pslot);
11655f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
11665f39d397SChris Mason 			if (btrfs_header_nritems(left) > orig_slot) {
11675f39d397SChris Mason 				path->nodes[level] = left;
1168e66f709bSChris Mason 				path->slots[level + 1] -= 1;
1169e66f709bSChris Mason 				path->slots[level] = orig_slot;
1170925baeddSChris Mason 				btrfs_tree_unlock(mid);
11715f39d397SChris Mason 				free_extent_buffer(mid);
1172e66f709bSChris Mason 			} else {
1173e66f709bSChris Mason 				orig_slot -=
11745f39d397SChris Mason 					btrfs_header_nritems(left);
1175e66f709bSChris Mason 				path->slots[level] = orig_slot;
1176925baeddSChris Mason 				btrfs_tree_unlock(left);
11775f39d397SChris Mason 				free_extent_buffer(left);
1178e66f709bSChris Mason 			}
1179e66f709bSChris Mason 			return 0;
1180e66f709bSChris Mason 		}
1181925baeddSChris Mason 		btrfs_tree_unlock(left);
11825f39d397SChris Mason 		free_extent_buffer(left);
1183e66f709bSChris Mason 	}
11845f39d397SChris Mason 	right = read_node_slot(root, parent, pslot + 1);
1185e66f709bSChris Mason 
1186e66f709bSChris Mason 	/*
1187e66f709bSChris Mason 	 * then try to empty the right most buffer into the middle
1188e66f709bSChris Mason 	 */
11895f39d397SChris Mason 	if (right) {
119033ade1f8SChris Mason 		u32 right_nr;
1191b4ce94deSChris Mason 
1192925baeddSChris Mason 		btrfs_tree_lock(right);
1193b4ce94deSChris Mason 		btrfs_set_lock_blocking(right);
1194b4ce94deSChris Mason 
11955f39d397SChris Mason 		right_nr = btrfs_header_nritems(right);
119633ade1f8SChris Mason 		if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
119733ade1f8SChris Mason 			wret = 1;
119833ade1f8SChris Mason 		} else {
11995f39d397SChris Mason 			ret = btrfs_cow_block(trans, root, right,
12005f39d397SChris Mason 					      parent, pslot + 1,
12019fa8cfe7SChris Mason 					      &right);
120254aa1f4dSChris Mason 			if (ret)
120354aa1f4dSChris Mason 				wret = 1;
120454aa1f4dSChris Mason 			else {
120533ade1f8SChris Mason 				wret = balance_node_right(trans, root,
12065f39d397SChris Mason 							  right, mid);
120733ade1f8SChris Mason 			}
120854aa1f4dSChris Mason 		}
1209e66f709bSChris Mason 		if (wret < 0)
1210e66f709bSChris Mason 			ret = wret;
1211e66f709bSChris Mason 		if (wret == 0) {
12125f39d397SChris Mason 			struct btrfs_disk_key disk_key;
12135f39d397SChris Mason 
12145f39d397SChris Mason 			btrfs_node_key(right, &disk_key, 0);
12155f39d397SChris Mason 			btrfs_set_node_key(parent, &disk_key, pslot + 1);
12165f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
12175f39d397SChris Mason 
12185f39d397SChris Mason 			if (btrfs_header_nritems(mid) <= orig_slot) {
12195f39d397SChris Mason 				path->nodes[level] = right;
1220e66f709bSChris Mason 				path->slots[level + 1] += 1;
1221e66f709bSChris Mason 				path->slots[level] = orig_slot -
12225f39d397SChris Mason 					btrfs_header_nritems(mid);
1223925baeddSChris Mason 				btrfs_tree_unlock(mid);
12245f39d397SChris Mason 				free_extent_buffer(mid);
1225e66f709bSChris Mason 			} else {
1226925baeddSChris Mason 				btrfs_tree_unlock(right);
12275f39d397SChris Mason 				free_extent_buffer(right);
1228e66f709bSChris Mason 			}
1229e66f709bSChris Mason 			return 0;
1230e66f709bSChris Mason 		}
1231925baeddSChris Mason 		btrfs_tree_unlock(right);
12325f39d397SChris Mason 		free_extent_buffer(right);
1233e66f709bSChris Mason 	}
1234e66f709bSChris Mason 	return 1;
1235e66f709bSChris Mason }
1236e66f709bSChris Mason 
123774123bd7SChris Mason /*
1238d352ac68SChris Mason  * readahead one full node of leaves, finding things that are close
1239d352ac68SChris Mason  * to the block in 'slot', and triggering ra on them.
12403c69faecSChris Mason  */
1241c8c42864SChris Mason static void reada_for_search(struct btrfs_root *root,
1242e02119d5SChris Mason 			     struct btrfs_path *path,
124301f46658SChris Mason 			     int level, int slot, u64 objectid)
12443c69faecSChris Mason {
12455f39d397SChris Mason 	struct extent_buffer *node;
124601f46658SChris Mason 	struct btrfs_disk_key disk_key;
12473c69faecSChris Mason 	u32 nritems;
12483c69faecSChris Mason 	u64 search;
1249a7175319SChris Mason 	u64 target;
12506b80053dSChris Mason 	u64 nread = 0;
1251cb25c2eaSJosef Bacik 	u64 gen;
12523c69faecSChris Mason 	int direction = path->reada;
12535f39d397SChris Mason 	struct extent_buffer *eb;
12546b80053dSChris Mason 	u32 nr;
12556b80053dSChris Mason 	u32 blocksize;
12566b80053dSChris Mason 	u32 nscan = 0;
1257db94535dSChris Mason 
1258a6b6e75eSChris Mason 	if (level != 1)
12593c69faecSChris Mason 		return;
12603c69faecSChris Mason 
12616702ed49SChris Mason 	if (!path->nodes[level])
12626702ed49SChris Mason 		return;
12636702ed49SChris Mason 
12645f39d397SChris Mason 	node = path->nodes[level];
1265925baeddSChris Mason 
12663c69faecSChris Mason 	search = btrfs_node_blockptr(node, slot);
12676b80053dSChris Mason 	blocksize = btrfs_level_size(root, level - 1);
12686b80053dSChris Mason 	eb = btrfs_find_tree_block(root, search, blocksize);
12695f39d397SChris Mason 	if (eb) {
12705f39d397SChris Mason 		free_extent_buffer(eb);
12713c69faecSChris Mason 		return;
12723c69faecSChris Mason 	}
12733c69faecSChris Mason 
1274a7175319SChris Mason 	target = search;
12756b80053dSChris Mason 
12765f39d397SChris Mason 	nritems = btrfs_header_nritems(node);
12776b80053dSChris Mason 	nr = slot;
127825b8b936SJosef Bacik 
12793c69faecSChris Mason 	while (1) {
12806b80053dSChris Mason 		if (direction < 0) {
12816b80053dSChris Mason 			if (nr == 0)
12823c69faecSChris Mason 				break;
12836b80053dSChris Mason 			nr--;
12846b80053dSChris Mason 		} else if (direction > 0) {
12856b80053dSChris Mason 			nr++;
12866b80053dSChris Mason 			if (nr >= nritems)
12876b80053dSChris Mason 				break;
12883c69faecSChris Mason 		}
128901f46658SChris Mason 		if (path->reada < 0 && objectid) {
129001f46658SChris Mason 			btrfs_node_key(node, &disk_key, nr);
129101f46658SChris Mason 			if (btrfs_disk_key_objectid(&disk_key) != objectid)
129201f46658SChris Mason 				break;
129301f46658SChris Mason 		}
12946b80053dSChris Mason 		search = btrfs_node_blockptr(node, nr);
1295a7175319SChris Mason 		if ((search <= target && target - search <= 65536) ||
1296a7175319SChris Mason 		    (search > target && search - target <= 65536)) {
1297cb25c2eaSJosef Bacik 			gen = btrfs_node_ptr_generation(node, nr);
1298cb25c2eaSJosef Bacik 			readahead_tree_block(root, search, blocksize, gen);
12996b80053dSChris Mason 			nread += blocksize;
13003c69faecSChris Mason 		}
13016b80053dSChris Mason 		nscan++;
1302a7175319SChris Mason 		if ((nread > 65536 || nscan > 32))
13036b80053dSChris Mason 			break;
13043c69faecSChris Mason 	}
13053c69faecSChris Mason }
1306925baeddSChris Mason 
1307d352ac68SChris Mason /*
1308b4ce94deSChris Mason  * returns -EAGAIN if it had to drop the path, or zero if everything was in
1309b4ce94deSChris Mason  * cache
1310b4ce94deSChris Mason  */
1311b4ce94deSChris Mason static noinline int reada_for_balance(struct btrfs_root *root,
1312b4ce94deSChris Mason 				      struct btrfs_path *path, int level)
1313b4ce94deSChris Mason {
1314b4ce94deSChris Mason 	int slot;
1315b4ce94deSChris Mason 	int nritems;
1316b4ce94deSChris Mason 	struct extent_buffer *parent;
1317b4ce94deSChris Mason 	struct extent_buffer *eb;
1318b4ce94deSChris Mason 	u64 gen;
1319b4ce94deSChris Mason 	u64 block1 = 0;
1320b4ce94deSChris Mason 	u64 block2 = 0;
1321b4ce94deSChris Mason 	int ret = 0;
1322b4ce94deSChris Mason 	int blocksize;
1323b4ce94deSChris Mason 
13248c594ea8SChris Mason 	parent = path->nodes[level + 1];
1325b4ce94deSChris Mason 	if (!parent)
1326b4ce94deSChris Mason 		return 0;
1327b4ce94deSChris Mason 
1328b4ce94deSChris Mason 	nritems = btrfs_header_nritems(parent);
13298c594ea8SChris Mason 	slot = path->slots[level + 1];
1330b4ce94deSChris Mason 	blocksize = btrfs_level_size(root, level);
1331b4ce94deSChris Mason 
1332b4ce94deSChris Mason 	if (slot > 0) {
1333b4ce94deSChris Mason 		block1 = btrfs_node_blockptr(parent, slot - 1);
1334b4ce94deSChris Mason 		gen = btrfs_node_ptr_generation(parent, slot - 1);
1335b4ce94deSChris Mason 		eb = btrfs_find_tree_block(root, block1, blocksize);
1336b4ce94deSChris Mason 		if (eb && btrfs_buffer_uptodate(eb, gen))
1337b4ce94deSChris Mason 			block1 = 0;
1338b4ce94deSChris Mason 		free_extent_buffer(eb);
1339b4ce94deSChris Mason 	}
13408c594ea8SChris Mason 	if (slot + 1 < nritems) {
1341b4ce94deSChris Mason 		block2 = btrfs_node_blockptr(parent, slot + 1);
1342b4ce94deSChris Mason 		gen = btrfs_node_ptr_generation(parent, slot + 1);
1343b4ce94deSChris Mason 		eb = btrfs_find_tree_block(root, block2, blocksize);
1344b4ce94deSChris Mason 		if (eb && btrfs_buffer_uptodate(eb, gen))
1345b4ce94deSChris Mason 			block2 = 0;
1346b4ce94deSChris Mason 		free_extent_buffer(eb);
1347b4ce94deSChris Mason 	}
1348b4ce94deSChris Mason 	if (block1 || block2) {
1349b4ce94deSChris Mason 		ret = -EAGAIN;
13508c594ea8SChris Mason 
13518c594ea8SChris Mason 		/* release the whole path */
1352b3b4aa74SDavid Sterba 		btrfs_release_path(path);
13538c594ea8SChris Mason 
13548c594ea8SChris Mason 		/* read the blocks */
1355b4ce94deSChris Mason 		if (block1)
1356b4ce94deSChris Mason 			readahead_tree_block(root, block1, blocksize, 0);
1357b4ce94deSChris Mason 		if (block2)
1358b4ce94deSChris Mason 			readahead_tree_block(root, block2, blocksize, 0);
1359b4ce94deSChris Mason 
1360b4ce94deSChris Mason 		if (block1) {
1361b4ce94deSChris Mason 			eb = read_tree_block(root, block1, blocksize, 0);
1362b4ce94deSChris Mason 			free_extent_buffer(eb);
1363b4ce94deSChris Mason 		}
13648c594ea8SChris Mason 		if (block2) {
1365b4ce94deSChris Mason 			eb = read_tree_block(root, block2, blocksize, 0);
1366b4ce94deSChris Mason 			free_extent_buffer(eb);
1367b4ce94deSChris Mason 		}
1368b4ce94deSChris Mason 	}
1369b4ce94deSChris Mason 	return ret;
1370b4ce94deSChris Mason }
1371b4ce94deSChris Mason 
1372b4ce94deSChris Mason 
1373b4ce94deSChris Mason /*
1374d397712bSChris Mason  * when we walk down the tree, it is usually safe to unlock the higher layers
1375d397712bSChris Mason  * in the tree.  The exceptions are when our path goes through slot 0, because
1376d397712bSChris Mason  * operations on the tree might require changing key pointers higher up in the
1377d397712bSChris Mason  * tree.
1378d352ac68SChris Mason  *
1379d397712bSChris Mason  * callers might also have set path->keep_locks, which tells this code to keep
1380d397712bSChris Mason  * the lock if the path points to the last slot in the block.  This is part of
1381d397712bSChris Mason  * walking through the tree, and selecting the next slot in the higher block.
1382d352ac68SChris Mason  *
1383d397712bSChris Mason  * lowest_unlock sets the lowest level in the tree we're allowed to unlock.  so
1384d397712bSChris Mason  * if lowest_unlock is 1, level 0 won't be unlocked
1385d352ac68SChris Mason  */
1386e02119d5SChris Mason static noinline void unlock_up(struct btrfs_path *path, int level,
1387e02119d5SChris Mason 			       int lowest_unlock)
1388925baeddSChris Mason {
1389925baeddSChris Mason 	int i;
1390925baeddSChris Mason 	int skip_level = level;
1391051e1b9fSChris Mason 	int no_skips = 0;
1392925baeddSChris Mason 	struct extent_buffer *t;
1393925baeddSChris Mason 
1394925baeddSChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1395925baeddSChris Mason 		if (!path->nodes[i])
1396925baeddSChris Mason 			break;
1397925baeddSChris Mason 		if (!path->locks[i])
1398925baeddSChris Mason 			break;
1399051e1b9fSChris Mason 		if (!no_skips && path->slots[i] == 0) {
1400925baeddSChris Mason 			skip_level = i + 1;
1401925baeddSChris Mason 			continue;
1402925baeddSChris Mason 		}
1403051e1b9fSChris Mason 		if (!no_skips && path->keep_locks) {
1404925baeddSChris Mason 			u32 nritems;
1405925baeddSChris Mason 			t = path->nodes[i];
1406925baeddSChris Mason 			nritems = btrfs_header_nritems(t);
1407051e1b9fSChris Mason 			if (nritems < 1 || path->slots[i] >= nritems - 1) {
1408925baeddSChris Mason 				skip_level = i + 1;
1409925baeddSChris Mason 				continue;
1410925baeddSChris Mason 			}
1411925baeddSChris Mason 		}
1412051e1b9fSChris Mason 		if (skip_level < i && i >= lowest_unlock)
1413051e1b9fSChris Mason 			no_skips = 1;
1414051e1b9fSChris Mason 
1415925baeddSChris Mason 		t = path->nodes[i];
1416925baeddSChris Mason 		if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1417bd681513SChris Mason 			btrfs_tree_unlock_rw(t, path->locks[i]);
1418925baeddSChris Mason 			path->locks[i] = 0;
1419925baeddSChris Mason 		}
1420925baeddSChris Mason 	}
1421925baeddSChris Mason }
1422925baeddSChris Mason 
14233c69faecSChris Mason /*
1424b4ce94deSChris Mason  * This releases any locks held in the path starting at level and
1425b4ce94deSChris Mason  * going all the way up to the root.
1426b4ce94deSChris Mason  *
1427b4ce94deSChris Mason  * btrfs_search_slot will keep the lock held on higher nodes in a few
1428b4ce94deSChris Mason  * corner cases, such as COW of the block at slot zero in the node.  This
1429b4ce94deSChris Mason  * ignores those rules, and it should only be called when there are no
1430b4ce94deSChris Mason  * more updates to be done higher up in the tree.
1431b4ce94deSChris Mason  */
1432b4ce94deSChris Mason noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1433b4ce94deSChris Mason {
1434b4ce94deSChris Mason 	int i;
1435b4ce94deSChris Mason 
14365d4f98a2SYan Zheng 	if (path->keep_locks)
1437b4ce94deSChris Mason 		return;
1438b4ce94deSChris Mason 
1439b4ce94deSChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1440b4ce94deSChris Mason 		if (!path->nodes[i])
144112f4daccSChris Mason 			continue;
1442b4ce94deSChris Mason 		if (!path->locks[i])
144312f4daccSChris Mason 			continue;
1444bd681513SChris Mason 		btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
1445b4ce94deSChris Mason 		path->locks[i] = 0;
1446b4ce94deSChris Mason 	}
1447b4ce94deSChris Mason }
1448b4ce94deSChris Mason 
1449b4ce94deSChris Mason /*
1450c8c42864SChris Mason  * helper function for btrfs_search_slot.  The goal is to find a block
1451c8c42864SChris Mason  * in cache without setting the path to blocking.  If we find the block
1452c8c42864SChris Mason  * we return zero and the path is unchanged.
1453c8c42864SChris Mason  *
1454c8c42864SChris Mason  * If we can't find the block, we set the path blocking and do some
1455c8c42864SChris Mason  * reada.  -EAGAIN is returned and the search must be repeated.
1456c8c42864SChris Mason  */
1457c8c42864SChris Mason static int
1458c8c42864SChris Mason read_block_for_search(struct btrfs_trans_handle *trans,
1459c8c42864SChris Mason 		       struct btrfs_root *root, struct btrfs_path *p,
1460c8c42864SChris Mason 		       struct extent_buffer **eb_ret, int level, int slot,
1461c8c42864SChris Mason 		       struct btrfs_key *key)
1462c8c42864SChris Mason {
1463c8c42864SChris Mason 	u64 blocknr;
1464c8c42864SChris Mason 	u64 gen;
1465c8c42864SChris Mason 	u32 blocksize;
1466c8c42864SChris Mason 	struct extent_buffer *b = *eb_ret;
1467c8c42864SChris Mason 	struct extent_buffer *tmp;
146876a05b35SChris Mason 	int ret;
1469c8c42864SChris Mason 
1470c8c42864SChris Mason 	blocknr = btrfs_node_blockptr(b, slot);
1471c8c42864SChris Mason 	gen = btrfs_node_ptr_generation(b, slot);
1472c8c42864SChris Mason 	blocksize = btrfs_level_size(root, level - 1);
1473c8c42864SChris Mason 
1474c8c42864SChris Mason 	tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1475cb44921aSChris Mason 	if (tmp) {
1476cb44921aSChris Mason 		if (btrfs_buffer_uptodate(tmp, 0)) {
1477cb44921aSChris Mason 			if (btrfs_buffer_uptodate(tmp, gen)) {
147876a05b35SChris Mason 				/*
1479cb44921aSChris Mason 				 * we found an up to date block without
1480cb44921aSChris Mason 				 * sleeping, return
148176a05b35SChris Mason 				 * right away
148276a05b35SChris Mason 				 */
1483c8c42864SChris Mason 				*eb_ret = tmp;
1484c8c42864SChris Mason 				return 0;
1485c8c42864SChris Mason 			}
1486cb44921aSChris Mason 			/* the pages were up to date, but we failed
1487cb44921aSChris Mason 			 * the generation number check.  Do a full
1488cb44921aSChris Mason 			 * read for the generation number that is correct.
1489cb44921aSChris Mason 			 * We must do this without dropping locks so
1490cb44921aSChris Mason 			 * we can trust our generation number
1491cb44921aSChris Mason 			 */
1492cb44921aSChris Mason 			free_extent_buffer(tmp);
1493bd681513SChris Mason 			btrfs_set_path_blocking(p);
1494bd681513SChris Mason 
1495cb44921aSChris Mason 			tmp = read_tree_block(root, blocknr, blocksize, gen);
1496cb44921aSChris Mason 			if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1497cb44921aSChris Mason 				*eb_ret = tmp;
1498cb44921aSChris Mason 				return 0;
1499cb44921aSChris Mason 			}
1500cb44921aSChris Mason 			free_extent_buffer(tmp);
1501b3b4aa74SDavid Sterba 			btrfs_release_path(p);
1502cb44921aSChris Mason 			return -EIO;
1503cb44921aSChris Mason 		}
1504cb44921aSChris Mason 	}
1505c8c42864SChris Mason 
1506c8c42864SChris Mason 	/*
1507c8c42864SChris Mason 	 * reduce lock contention at high levels
1508c8c42864SChris Mason 	 * of the btree by dropping locks before
150976a05b35SChris Mason 	 * we read.  Don't release the lock on the current
151076a05b35SChris Mason 	 * level because we need to walk this node to figure
151176a05b35SChris Mason 	 * out which blocks to read.
1512c8c42864SChris Mason 	 */
15138c594ea8SChris Mason 	btrfs_unlock_up_safe(p, level + 1);
15148c594ea8SChris Mason 	btrfs_set_path_blocking(p);
15158c594ea8SChris Mason 
1516c8c42864SChris Mason 	free_extent_buffer(tmp);
1517c8c42864SChris Mason 	if (p->reada)
1518c8c42864SChris Mason 		reada_for_search(root, p, level, slot, key->objectid);
1519c8c42864SChris Mason 
1520b3b4aa74SDavid Sterba 	btrfs_release_path(p);
152176a05b35SChris Mason 
152276a05b35SChris Mason 	ret = -EAGAIN;
15235bdd3536SYan, Zheng 	tmp = read_tree_block(root, blocknr, blocksize, 0);
152476a05b35SChris Mason 	if (tmp) {
152576a05b35SChris Mason 		/*
152676a05b35SChris Mason 		 * If the read above didn't mark this buffer up to date,
152776a05b35SChris Mason 		 * it will never end up being up to date.  Set ret to EIO now
152876a05b35SChris Mason 		 * and give up so that our caller doesn't loop forever
152976a05b35SChris Mason 		 * on our EAGAINs.
153076a05b35SChris Mason 		 */
153176a05b35SChris Mason 		if (!btrfs_buffer_uptodate(tmp, 0))
153276a05b35SChris Mason 			ret = -EIO;
1533c8c42864SChris Mason 		free_extent_buffer(tmp);
153476a05b35SChris Mason 	}
153576a05b35SChris Mason 	return ret;
1536c8c42864SChris Mason }
1537c8c42864SChris Mason 
1538c8c42864SChris Mason /*
1539c8c42864SChris Mason  * helper function for btrfs_search_slot.  This does all of the checks
1540c8c42864SChris Mason  * for node-level blocks and does any balancing required based on
1541c8c42864SChris Mason  * the ins_len.
1542c8c42864SChris Mason  *
1543c8c42864SChris Mason  * If no extra work was required, zero is returned.  If we had to
1544c8c42864SChris Mason  * drop the path, -EAGAIN is returned and btrfs_search_slot must
1545c8c42864SChris Mason  * start over
1546c8c42864SChris Mason  */
1547c8c42864SChris Mason static int
1548c8c42864SChris Mason setup_nodes_for_search(struct btrfs_trans_handle *trans,
1549c8c42864SChris Mason 		       struct btrfs_root *root, struct btrfs_path *p,
1550bd681513SChris Mason 		       struct extent_buffer *b, int level, int ins_len,
1551bd681513SChris Mason 		       int *write_lock_level)
1552c8c42864SChris Mason {
1553c8c42864SChris Mason 	int ret;
1554c8c42864SChris Mason 	if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1555c8c42864SChris Mason 	    BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1556c8c42864SChris Mason 		int sret;
1557c8c42864SChris Mason 
1558bd681513SChris Mason 		if (*write_lock_level < level + 1) {
1559bd681513SChris Mason 			*write_lock_level = level + 1;
1560bd681513SChris Mason 			btrfs_release_path(p);
1561bd681513SChris Mason 			goto again;
1562bd681513SChris Mason 		}
1563bd681513SChris Mason 
1564c8c42864SChris Mason 		sret = reada_for_balance(root, p, level);
1565c8c42864SChris Mason 		if (sret)
1566c8c42864SChris Mason 			goto again;
1567c8c42864SChris Mason 
1568c8c42864SChris Mason 		btrfs_set_path_blocking(p);
1569c8c42864SChris Mason 		sret = split_node(trans, root, p, level);
1570bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
1571c8c42864SChris Mason 
1572c8c42864SChris Mason 		BUG_ON(sret > 0);
1573c8c42864SChris Mason 		if (sret) {
1574c8c42864SChris Mason 			ret = sret;
1575c8c42864SChris Mason 			goto done;
1576c8c42864SChris Mason 		}
1577c8c42864SChris Mason 		b = p->nodes[level];
1578c8c42864SChris Mason 	} else if (ins_len < 0 && btrfs_header_nritems(b) <
1579cfbb9308SChris Mason 		   BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
1580c8c42864SChris Mason 		int sret;
1581c8c42864SChris Mason 
1582bd681513SChris Mason 		if (*write_lock_level < level + 1) {
1583bd681513SChris Mason 			*write_lock_level = level + 1;
1584bd681513SChris Mason 			btrfs_release_path(p);
1585bd681513SChris Mason 			goto again;
1586bd681513SChris Mason 		}
1587bd681513SChris Mason 
1588c8c42864SChris Mason 		sret = reada_for_balance(root, p, level);
1589c8c42864SChris Mason 		if (sret)
1590c8c42864SChris Mason 			goto again;
1591c8c42864SChris Mason 
1592c8c42864SChris Mason 		btrfs_set_path_blocking(p);
1593c8c42864SChris Mason 		sret = balance_level(trans, root, p, level);
1594bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
1595c8c42864SChris Mason 
1596c8c42864SChris Mason 		if (sret) {
1597c8c42864SChris Mason 			ret = sret;
1598c8c42864SChris Mason 			goto done;
1599c8c42864SChris Mason 		}
1600c8c42864SChris Mason 		b = p->nodes[level];
1601c8c42864SChris Mason 		if (!b) {
1602b3b4aa74SDavid Sterba 			btrfs_release_path(p);
1603c8c42864SChris Mason 			goto again;
1604c8c42864SChris Mason 		}
1605c8c42864SChris Mason 		BUG_ON(btrfs_header_nritems(b) == 1);
1606c8c42864SChris Mason 	}
1607c8c42864SChris Mason 	return 0;
1608c8c42864SChris Mason 
1609c8c42864SChris Mason again:
1610c8c42864SChris Mason 	ret = -EAGAIN;
1611c8c42864SChris Mason done:
1612c8c42864SChris Mason 	return ret;
1613c8c42864SChris Mason }
1614c8c42864SChris Mason 
1615c8c42864SChris Mason /*
161674123bd7SChris Mason  * look for key in the tree.  path is filled in with nodes along the way
161774123bd7SChris Mason  * if key is found, we return zero and you can find the item in the leaf
161874123bd7SChris Mason  * level of the path (level 0)
161974123bd7SChris Mason  *
162074123bd7SChris Mason  * If the key isn't found, the path points to the slot where it should
1621aa5d6bedSChris Mason  * be inserted, and 1 is returned.  If there are other errors during the
1622aa5d6bedSChris Mason  * search a negative error number is returned.
162397571fd0SChris Mason  *
162497571fd0SChris Mason  * if ins_len > 0, nodes and leaves will be split as we walk down the
162597571fd0SChris Mason  * tree.  if ins_len < 0, nodes will be merged as we walk down the tree (if
162697571fd0SChris Mason  * possible)
162774123bd7SChris Mason  */
1628e089f05cSChris Mason int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1629e089f05cSChris Mason 		      *root, struct btrfs_key *key, struct btrfs_path *p, int
1630e089f05cSChris Mason 		      ins_len, int cow)
1631be0e5c09SChris Mason {
16325f39d397SChris Mason 	struct extent_buffer *b;
1633be0e5c09SChris Mason 	int slot;
1634be0e5c09SChris Mason 	int ret;
163533c66f43SYan Zheng 	int err;
1636be0e5c09SChris Mason 	int level;
1637925baeddSChris Mason 	int lowest_unlock = 1;
1638bd681513SChris Mason 	int root_lock;
1639bd681513SChris Mason 	/* everything at write_lock_level or lower must be write locked */
1640bd681513SChris Mason 	int write_lock_level = 0;
16419f3a7427SChris Mason 	u8 lowest_level = 0;
16429f3a7427SChris Mason 
16436702ed49SChris Mason 	lowest_level = p->lowest_level;
1644323ac95bSChris Mason 	WARN_ON(lowest_level && ins_len > 0);
164522b0ebdaSChris Mason 	WARN_ON(p->nodes[0] != NULL);
164625179201SJosef Bacik 
1647bd681513SChris Mason 	if (ins_len < 0) {
1648925baeddSChris Mason 		lowest_unlock = 2;
164965b51a00SChris Mason 
1650bd681513SChris Mason 		/* when we are removing items, we might have to go up to level
1651bd681513SChris Mason 		 * two as we update tree pointers  Make sure we keep write
1652bd681513SChris Mason 		 * for those levels as well
1653bd681513SChris Mason 		 */
1654bd681513SChris Mason 		write_lock_level = 2;
1655bd681513SChris Mason 	} else if (ins_len > 0) {
1656bd681513SChris Mason 		/*
1657bd681513SChris Mason 		 * for inserting items, make sure we have a write lock on
1658bd681513SChris Mason 		 * level 1 so we can update keys
1659bd681513SChris Mason 		 */
1660bd681513SChris Mason 		write_lock_level = 1;
1661bd681513SChris Mason 	}
1662bd681513SChris Mason 
1663bd681513SChris Mason 	if (!cow)
1664bd681513SChris Mason 		write_lock_level = -1;
1665bd681513SChris Mason 
1666bd681513SChris Mason 	if (cow && (p->keep_locks || p->lowest_level))
1667bd681513SChris Mason 		write_lock_level = BTRFS_MAX_LEVEL;
1668bd681513SChris Mason 
1669bb803951SChris Mason again:
1670bd681513SChris Mason 	/*
1671bd681513SChris Mason 	 * we try very hard to do read locks on the root
1672bd681513SChris Mason 	 */
1673bd681513SChris Mason 	root_lock = BTRFS_READ_LOCK;
1674bd681513SChris Mason 	level = 0;
16755d4f98a2SYan Zheng 	if (p->search_commit_root) {
1676bd681513SChris Mason 		/*
1677bd681513SChris Mason 		 * the commit roots are read only
1678bd681513SChris Mason 		 * so we always do read locks
1679bd681513SChris Mason 		 */
16805d4f98a2SYan Zheng 		b = root->commit_root;
16815d4f98a2SYan Zheng 		extent_buffer_get(b);
1682bd681513SChris Mason 		level = btrfs_header_level(b);
16835d4f98a2SYan Zheng 		if (!p->skip_locking)
1684bd681513SChris Mason 			btrfs_tree_read_lock(b);
16855d4f98a2SYan Zheng 	} else {
1686bd681513SChris Mason 		if (p->skip_locking) {
16875cd57b2cSChris Mason 			b = btrfs_root_node(root);
1688bd681513SChris Mason 			level = btrfs_header_level(b);
1689bd681513SChris Mason 		} else {
1690bd681513SChris Mason 			/* we don't know the level of the root node
1691bd681513SChris Mason 			 * until we actually have it read locked
1692bd681513SChris Mason 			 */
1693bd681513SChris Mason 			b = btrfs_read_lock_root_node(root);
1694bd681513SChris Mason 			level = btrfs_header_level(b);
1695bd681513SChris Mason 			if (level <= write_lock_level) {
1696bd681513SChris Mason 				/* whoops, must trade for write lock */
1697bd681513SChris Mason 				btrfs_tree_read_unlock(b);
1698bd681513SChris Mason 				free_extent_buffer(b);
1699925baeddSChris Mason 				b = btrfs_lock_root_node(root);
1700bd681513SChris Mason 				root_lock = BTRFS_WRITE_LOCK;
1701bd681513SChris Mason 
1702bd681513SChris Mason 				/* the level might have changed, check again */
1703bd681513SChris Mason 				level = btrfs_header_level(b);
17045d4f98a2SYan Zheng 			}
1705bd681513SChris Mason 		}
1706bd681513SChris Mason 	}
1707bd681513SChris Mason 	p->nodes[level] = b;
1708bd681513SChris Mason 	if (!p->skip_locking)
1709bd681513SChris Mason 		p->locks[level] = root_lock;
1710925baeddSChris Mason 
1711eb60ceacSChris Mason 	while (b) {
17125f39d397SChris Mason 		level = btrfs_header_level(b);
171365b51a00SChris Mason 
171465b51a00SChris Mason 		/*
171565b51a00SChris Mason 		 * setup the path here so we can release it under lock
171665b51a00SChris Mason 		 * contention with the cow code
171765b51a00SChris Mason 		 */
171802217ed2SChris Mason 		if (cow) {
1719c8c42864SChris Mason 			/*
1720c8c42864SChris Mason 			 * if we don't really need to cow this block
1721c8c42864SChris Mason 			 * then we don't want to set the path blocking,
1722c8c42864SChris Mason 			 * so we test it here
1723c8c42864SChris Mason 			 */
17245d4f98a2SYan Zheng 			if (!should_cow_block(trans, root, b))
172565b51a00SChris Mason 				goto cow_done;
17265d4f98a2SYan Zheng 
1727b4ce94deSChris Mason 			btrfs_set_path_blocking(p);
1728b4ce94deSChris Mason 
1729bd681513SChris Mason 			/*
1730bd681513SChris Mason 			 * must have write locks on this node and the
1731bd681513SChris Mason 			 * parent
1732bd681513SChris Mason 			 */
1733bd681513SChris Mason 			if (level + 1 > write_lock_level) {
1734bd681513SChris Mason 				write_lock_level = level + 1;
1735bd681513SChris Mason 				btrfs_release_path(p);
1736bd681513SChris Mason 				goto again;
1737bd681513SChris Mason 			}
1738bd681513SChris Mason 
173933c66f43SYan Zheng 			err = btrfs_cow_block(trans, root, b,
1740e20d96d6SChris Mason 					      p->nodes[level + 1],
17419fa8cfe7SChris Mason 					      p->slots[level + 1], &b);
174233c66f43SYan Zheng 			if (err) {
174333c66f43SYan Zheng 				ret = err;
174465b51a00SChris Mason 				goto done;
174554aa1f4dSChris Mason 			}
174602217ed2SChris Mason 		}
174765b51a00SChris Mason cow_done:
174802217ed2SChris Mason 		BUG_ON(!cow && ins_len);
174965b51a00SChris Mason 
1750eb60ceacSChris Mason 		p->nodes[level] = b;
1751bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
1752b4ce94deSChris Mason 
1753b4ce94deSChris Mason 		/*
1754b4ce94deSChris Mason 		 * we have a lock on b and as long as we aren't changing
1755b4ce94deSChris Mason 		 * the tree, there is no way to for the items in b to change.
1756b4ce94deSChris Mason 		 * It is safe to drop the lock on our parent before we
1757b4ce94deSChris Mason 		 * go through the expensive btree search on b.
1758b4ce94deSChris Mason 		 *
1759b4ce94deSChris Mason 		 * If cow is true, then we might be changing slot zero,
1760b4ce94deSChris Mason 		 * which may require changing the parent.  So, we can't
1761b4ce94deSChris Mason 		 * drop the lock until after we know which slot we're
1762b4ce94deSChris Mason 		 * operating on.
1763b4ce94deSChris Mason 		 */
1764b4ce94deSChris Mason 		if (!cow)
1765b4ce94deSChris Mason 			btrfs_unlock_up_safe(p, level + 1);
1766b4ce94deSChris Mason 
17675f39d397SChris Mason 		ret = bin_search(b, key, level, &slot);
1768b4ce94deSChris Mason 
17695f39d397SChris Mason 		if (level != 0) {
177033c66f43SYan Zheng 			int dec = 0;
177133c66f43SYan Zheng 			if (ret && slot > 0) {
177233c66f43SYan Zheng 				dec = 1;
1773be0e5c09SChris Mason 				slot -= 1;
177433c66f43SYan Zheng 			}
1775be0e5c09SChris Mason 			p->slots[level] = slot;
177633c66f43SYan Zheng 			err = setup_nodes_for_search(trans, root, p, b, level,
1777bd681513SChris Mason 					     ins_len, &write_lock_level);
177833c66f43SYan Zheng 			if (err == -EAGAIN)
1779b4ce94deSChris Mason 				goto again;
178033c66f43SYan Zheng 			if (err) {
178133c66f43SYan Zheng 				ret = err;
178265b51a00SChris Mason 				goto done;
178333c66f43SYan Zheng 			}
17845c680ed6SChris Mason 			b = p->nodes[level];
17855c680ed6SChris Mason 			slot = p->slots[level];
1786b4ce94deSChris Mason 
1787bd681513SChris Mason 			/*
1788bd681513SChris Mason 			 * slot 0 is special, if we change the key
1789bd681513SChris Mason 			 * we have to update the parent pointer
1790bd681513SChris Mason 			 * which means we must have a write lock
1791bd681513SChris Mason 			 * on the parent
1792bd681513SChris Mason 			 */
1793bd681513SChris Mason 			if (slot == 0 && cow &&
1794bd681513SChris Mason 			    write_lock_level < level + 1) {
1795bd681513SChris Mason 				write_lock_level = level + 1;
1796bd681513SChris Mason 				btrfs_release_path(p);
1797bd681513SChris Mason 				goto again;
1798bd681513SChris Mason 			}
1799bd681513SChris Mason 
1800f9efa9c7SChris Mason 			unlock_up(p, level, lowest_unlock);
1801f9efa9c7SChris Mason 
1802925baeddSChris Mason 			if (level == lowest_level) {
180333c66f43SYan Zheng 				if (dec)
180433c66f43SYan Zheng 					p->slots[level]++;
18055b21f2edSZheng Yan 				goto done;
1806925baeddSChris Mason 			}
1807ca7a79adSChris Mason 
180833c66f43SYan Zheng 			err = read_block_for_search(trans, root, p,
1809c8c42864SChris Mason 						    &b, level, slot, key);
181033c66f43SYan Zheng 			if (err == -EAGAIN)
1811051e1b9fSChris Mason 				goto again;
181233c66f43SYan Zheng 			if (err) {
181333c66f43SYan Zheng 				ret = err;
181476a05b35SChris Mason 				goto done;
181533c66f43SYan Zheng 			}
181676a05b35SChris Mason 
1817b4ce94deSChris Mason 			if (!p->skip_locking) {
1818bd681513SChris Mason 				level = btrfs_header_level(b);
1819bd681513SChris Mason 				if (level <= write_lock_level) {
1820bd681513SChris Mason 					err = btrfs_try_tree_write_lock(b);
182133c66f43SYan Zheng 					if (!err) {
1822b4ce94deSChris Mason 						btrfs_set_path_blocking(p);
1823925baeddSChris Mason 						btrfs_tree_lock(b);
1824bd681513SChris Mason 						btrfs_clear_path_blocking(p, b,
1825bd681513SChris Mason 								  BTRFS_WRITE_LOCK);
1826b4ce94deSChris Mason 					}
1827bd681513SChris Mason 					p->locks[level] = BTRFS_WRITE_LOCK;
1828bd681513SChris Mason 				} else {
1829bd681513SChris Mason 					err = btrfs_try_tree_read_lock(b);
1830bd681513SChris Mason 					if (!err) {
1831bd681513SChris Mason 						btrfs_set_path_blocking(p);
1832bd681513SChris Mason 						btrfs_tree_read_lock(b);
1833bd681513SChris Mason 						btrfs_clear_path_blocking(p, b,
1834bd681513SChris Mason 								  BTRFS_READ_LOCK);
1835bd681513SChris Mason 					}
1836bd681513SChris Mason 					p->locks[level] = BTRFS_READ_LOCK;
1837bd681513SChris Mason 				}
1838bd681513SChris Mason 				p->nodes[level] = b;
1839b4ce94deSChris Mason 			}
1840be0e5c09SChris Mason 		} else {
1841be0e5c09SChris Mason 			p->slots[level] = slot;
184287b29b20SYan Zheng 			if (ins_len > 0 &&
184387b29b20SYan Zheng 			    btrfs_leaf_free_space(root, b) < ins_len) {
1844bd681513SChris Mason 				if (write_lock_level < 1) {
1845bd681513SChris Mason 					write_lock_level = 1;
1846bd681513SChris Mason 					btrfs_release_path(p);
1847bd681513SChris Mason 					goto again;
1848bd681513SChris Mason 				}
1849bd681513SChris Mason 
1850b4ce94deSChris Mason 				btrfs_set_path_blocking(p);
185133c66f43SYan Zheng 				err = split_leaf(trans, root, key,
1852cc0c5538SChris Mason 						 p, ins_len, ret == 0);
1853bd681513SChris Mason 				btrfs_clear_path_blocking(p, NULL, 0);
1854b4ce94deSChris Mason 
185533c66f43SYan Zheng 				BUG_ON(err > 0);
185633c66f43SYan Zheng 				if (err) {
185733c66f43SYan Zheng 					ret = err;
185865b51a00SChris Mason 					goto done;
185965b51a00SChris Mason 				}
18605c680ed6SChris Mason 			}
1861459931ecSChris Mason 			if (!p->search_for_split)
1862925baeddSChris Mason 				unlock_up(p, level, lowest_unlock);
186365b51a00SChris Mason 			goto done;
186465b51a00SChris Mason 		}
186565b51a00SChris Mason 	}
186665b51a00SChris Mason 	ret = 1;
186765b51a00SChris Mason done:
1868b4ce94deSChris Mason 	/*
1869b4ce94deSChris Mason 	 * we don't really know what they plan on doing with the path
1870b4ce94deSChris Mason 	 * from here on, so for now just mark it as blocking
1871b4ce94deSChris Mason 	 */
1872b9473439SChris Mason 	if (!p->leave_spinning)
1873b4ce94deSChris Mason 		btrfs_set_path_blocking(p);
187476a05b35SChris Mason 	if (ret < 0)
1875b3b4aa74SDavid Sterba 		btrfs_release_path(p);
1876be0e5c09SChris Mason 	return ret;
1877be0e5c09SChris Mason }
1878be0e5c09SChris Mason 
187974123bd7SChris Mason /*
188074123bd7SChris Mason  * adjust the pointers going up the tree, starting at level
188174123bd7SChris Mason  * making sure the right key of each node is points to 'key'.
188274123bd7SChris Mason  * This is used after shifting pointers to the left, so it stops
188374123bd7SChris Mason  * fixing up pointers when a given leaf/node is not in slot 0 of the
188474123bd7SChris Mason  * higher levels
1885aa5d6bedSChris Mason  *
188674123bd7SChris Mason  */
1887143bede5SJeff Mahoney static void fixup_low_keys(struct btrfs_trans_handle *trans,
18885f39d397SChris Mason 			   struct btrfs_root *root, struct btrfs_path *path,
18895f39d397SChris Mason 			   struct btrfs_disk_key *key, int level)
1890be0e5c09SChris Mason {
1891be0e5c09SChris Mason 	int i;
18925f39d397SChris Mason 	struct extent_buffer *t;
18935f39d397SChris Mason 
1894234b63a0SChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1895be0e5c09SChris Mason 		int tslot = path->slots[i];
1896eb60ceacSChris Mason 		if (!path->nodes[i])
1897be0e5c09SChris Mason 			break;
18985f39d397SChris Mason 		t = path->nodes[i];
18995f39d397SChris Mason 		btrfs_set_node_key(t, key, tslot);
1900d6025579SChris Mason 		btrfs_mark_buffer_dirty(path->nodes[i]);
1901be0e5c09SChris Mason 		if (tslot != 0)
1902be0e5c09SChris Mason 			break;
1903be0e5c09SChris Mason 	}
1904be0e5c09SChris Mason }
1905be0e5c09SChris Mason 
190674123bd7SChris Mason /*
190731840ae1SZheng Yan  * update item key.
190831840ae1SZheng Yan  *
190931840ae1SZheng Yan  * This function isn't completely safe. It's the caller's responsibility
191031840ae1SZheng Yan  * that the new key won't break the order
191131840ae1SZheng Yan  */
1912143bede5SJeff Mahoney void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
191331840ae1SZheng Yan 			     struct btrfs_root *root, struct btrfs_path *path,
191431840ae1SZheng Yan 			     struct btrfs_key *new_key)
191531840ae1SZheng Yan {
191631840ae1SZheng Yan 	struct btrfs_disk_key disk_key;
191731840ae1SZheng Yan 	struct extent_buffer *eb;
191831840ae1SZheng Yan 	int slot;
191931840ae1SZheng Yan 
192031840ae1SZheng Yan 	eb = path->nodes[0];
192131840ae1SZheng Yan 	slot = path->slots[0];
192231840ae1SZheng Yan 	if (slot > 0) {
192331840ae1SZheng Yan 		btrfs_item_key(eb, &disk_key, slot - 1);
1924143bede5SJeff Mahoney 		BUG_ON(comp_keys(&disk_key, new_key) >= 0);
192531840ae1SZheng Yan 	}
192631840ae1SZheng Yan 	if (slot < btrfs_header_nritems(eb) - 1) {
192731840ae1SZheng Yan 		btrfs_item_key(eb, &disk_key, slot + 1);
1928143bede5SJeff Mahoney 		BUG_ON(comp_keys(&disk_key, new_key) <= 0);
192931840ae1SZheng Yan 	}
193031840ae1SZheng Yan 
193131840ae1SZheng Yan 	btrfs_cpu_key_to_disk(&disk_key, new_key);
193231840ae1SZheng Yan 	btrfs_set_item_key(eb, &disk_key, slot);
193331840ae1SZheng Yan 	btrfs_mark_buffer_dirty(eb);
193431840ae1SZheng Yan 	if (slot == 0)
193531840ae1SZheng Yan 		fixup_low_keys(trans, root, path, &disk_key, 1);
193631840ae1SZheng Yan }
193731840ae1SZheng Yan 
193831840ae1SZheng Yan /*
193974123bd7SChris Mason  * try to push data from one node into the next node left in the
194079f95c82SChris Mason  * tree.
1941aa5d6bedSChris Mason  *
1942aa5d6bedSChris Mason  * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1943aa5d6bedSChris Mason  * error, and > 0 if there was no room in the left hand block.
194474123bd7SChris Mason  */
194598ed5174SChris Mason static int push_node_left(struct btrfs_trans_handle *trans,
194698ed5174SChris Mason 			  struct btrfs_root *root, struct extent_buffer *dst,
1947971a1f66SChris Mason 			  struct extent_buffer *src, int empty)
1948be0e5c09SChris Mason {
1949be0e5c09SChris Mason 	int push_items = 0;
1950bb803951SChris Mason 	int src_nritems;
1951bb803951SChris Mason 	int dst_nritems;
1952aa5d6bedSChris Mason 	int ret = 0;
1953be0e5c09SChris Mason 
19545f39d397SChris Mason 	src_nritems = btrfs_header_nritems(src);
19555f39d397SChris Mason 	dst_nritems = btrfs_header_nritems(dst);
1956123abc88SChris Mason 	push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
19577bb86316SChris Mason 	WARN_ON(btrfs_header_generation(src) != trans->transid);
19587bb86316SChris Mason 	WARN_ON(btrfs_header_generation(dst) != trans->transid);
195954aa1f4dSChris Mason 
1960bce4eae9SChris Mason 	if (!empty && src_nritems <= 8)
1961971a1f66SChris Mason 		return 1;
1962971a1f66SChris Mason 
1963d397712bSChris Mason 	if (push_items <= 0)
1964be0e5c09SChris Mason 		return 1;
1965be0e5c09SChris Mason 
1966bce4eae9SChris Mason 	if (empty) {
1967971a1f66SChris Mason 		push_items = min(src_nritems, push_items);
1968bce4eae9SChris Mason 		if (push_items < src_nritems) {
1969bce4eae9SChris Mason 			/* leave at least 8 pointers in the node if
1970bce4eae9SChris Mason 			 * we aren't going to empty it
1971bce4eae9SChris Mason 			 */
1972bce4eae9SChris Mason 			if (src_nritems - push_items < 8) {
1973bce4eae9SChris Mason 				if (push_items <= 8)
1974bce4eae9SChris Mason 					return 1;
1975bce4eae9SChris Mason 				push_items -= 8;
1976bce4eae9SChris Mason 			}
1977bce4eae9SChris Mason 		}
1978bce4eae9SChris Mason 	} else
1979bce4eae9SChris Mason 		push_items = min(src_nritems - 8, push_items);
198079f95c82SChris Mason 
19815f39d397SChris Mason 	copy_extent_buffer(dst, src,
19825f39d397SChris Mason 			   btrfs_node_key_ptr_offset(dst_nritems),
19835f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
1984123abc88SChris Mason 			   push_items * sizeof(struct btrfs_key_ptr));
19855f39d397SChris Mason 
1986bb803951SChris Mason 	if (push_items < src_nritems) {
19875f39d397SChris Mason 		memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
19885f39d397SChris Mason 				      btrfs_node_key_ptr_offset(push_items),
1989e2fa7227SChris Mason 				      (src_nritems - push_items) *
1990123abc88SChris Mason 				      sizeof(struct btrfs_key_ptr));
1991bb803951SChris Mason 	}
19925f39d397SChris Mason 	btrfs_set_header_nritems(src, src_nritems - push_items);
19935f39d397SChris Mason 	btrfs_set_header_nritems(dst, dst_nritems + push_items);
19945f39d397SChris Mason 	btrfs_mark_buffer_dirty(src);
19955f39d397SChris Mason 	btrfs_mark_buffer_dirty(dst);
199631840ae1SZheng Yan 
1997bb803951SChris Mason 	return ret;
1998be0e5c09SChris Mason }
1999be0e5c09SChris Mason 
200097571fd0SChris Mason /*
200179f95c82SChris Mason  * try to push data from one node into the next node right in the
200279f95c82SChris Mason  * tree.
200379f95c82SChris Mason  *
200479f95c82SChris Mason  * returns 0 if some ptrs were pushed, < 0 if there was some horrible
200579f95c82SChris Mason  * error, and > 0 if there was no room in the right hand block.
200679f95c82SChris Mason  *
200779f95c82SChris Mason  * this will  only push up to 1/2 the contents of the left node over
200879f95c82SChris Mason  */
20095f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans,
20105f39d397SChris Mason 			      struct btrfs_root *root,
20115f39d397SChris Mason 			      struct extent_buffer *dst,
20125f39d397SChris Mason 			      struct extent_buffer *src)
201379f95c82SChris Mason {
201479f95c82SChris Mason 	int push_items = 0;
201579f95c82SChris Mason 	int max_push;
201679f95c82SChris Mason 	int src_nritems;
201779f95c82SChris Mason 	int dst_nritems;
201879f95c82SChris Mason 	int ret = 0;
201979f95c82SChris Mason 
20207bb86316SChris Mason 	WARN_ON(btrfs_header_generation(src) != trans->transid);
20217bb86316SChris Mason 	WARN_ON(btrfs_header_generation(dst) != trans->transid);
20227bb86316SChris Mason 
20235f39d397SChris Mason 	src_nritems = btrfs_header_nritems(src);
20245f39d397SChris Mason 	dst_nritems = btrfs_header_nritems(dst);
2025123abc88SChris Mason 	push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
2026d397712bSChris Mason 	if (push_items <= 0)
202779f95c82SChris Mason 		return 1;
2028bce4eae9SChris Mason 
2029d397712bSChris Mason 	if (src_nritems < 4)
2030bce4eae9SChris Mason 		return 1;
203179f95c82SChris Mason 
203279f95c82SChris Mason 	max_push = src_nritems / 2 + 1;
203379f95c82SChris Mason 	/* don't try to empty the node */
2034d397712bSChris Mason 	if (max_push >= src_nritems)
203579f95c82SChris Mason 		return 1;
2036252c38f0SYan 
203779f95c82SChris Mason 	if (max_push < push_items)
203879f95c82SChris Mason 		push_items = max_push;
203979f95c82SChris Mason 
20405f39d397SChris Mason 	memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
20415f39d397SChris Mason 				      btrfs_node_key_ptr_offset(0),
20425f39d397SChris Mason 				      (dst_nritems) *
20435f39d397SChris Mason 				      sizeof(struct btrfs_key_ptr));
2044d6025579SChris Mason 
20455f39d397SChris Mason 	copy_extent_buffer(dst, src,
20465f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
20475f39d397SChris Mason 			   btrfs_node_key_ptr_offset(src_nritems - push_items),
2048123abc88SChris Mason 			   push_items * sizeof(struct btrfs_key_ptr));
204979f95c82SChris Mason 
20505f39d397SChris Mason 	btrfs_set_header_nritems(src, src_nritems - push_items);
20515f39d397SChris Mason 	btrfs_set_header_nritems(dst, dst_nritems + push_items);
205279f95c82SChris Mason 
20535f39d397SChris Mason 	btrfs_mark_buffer_dirty(src);
20545f39d397SChris Mason 	btrfs_mark_buffer_dirty(dst);
205531840ae1SZheng Yan 
205679f95c82SChris Mason 	return ret;
205779f95c82SChris Mason }
205879f95c82SChris Mason 
205979f95c82SChris Mason /*
206097571fd0SChris Mason  * helper function to insert a new root level in the tree.
206197571fd0SChris Mason  * A new node is allocated, and a single item is inserted to
206297571fd0SChris Mason  * point to the existing root
2063aa5d6bedSChris Mason  *
2064aa5d6bedSChris Mason  * returns zero on success or < 0 on failure.
206597571fd0SChris Mason  */
2066d397712bSChris Mason static noinline int insert_new_root(struct btrfs_trans_handle *trans,
20675f39d397SChris Mason 			   struct btrfs_root *root,
20685f39d397SChris Mason 			   struct btrfs_path *path, int level)
206974123bd7SChris Mason {
20707bb86316SChris Mason 	u64 lower_gen;
20715f39d397SChris Mason 	struct extent_buffer *lower;
20725f39d397SChris Mason 	struct extent_buffer *c;
2073925baeddSChris Mason 	struct extent_buffer *old;
20745f39d397SChris Mason 	struct btrfs_disk_key lower_key;
20755c680ed6SChris Mason 
20765c680ed6SChris Mason 	BUG_ON(path->nodes[level]);
20775c680ed6SChris Mason 	BUG_ON(path->nodes[level-1] != root->node);
20785c680ed6SChris Mason 
20797bb86316SChris Mason 	lower = path->nodes[level-1];
20807bb86316SChris Mason 	if (level == 1)
20817bb86316SChris Mason 		btrfs_item_key(lower, &lower_key, 0);
20827bb86316SChris Mason 	else
20837bb86316SChris Mason 		btrfs_node_key(lower, &lower_key, 0);
20847bb86316SChris Mason 
208531840ae1SZheng Yan 	c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
20865d4f98a2SYan Zheng 				   root->root_key.objectid, &lower_key,
208766d7e7f0SArne Jansen 				   level, root->node->start, 0, 0);
20885f39d397SChris Mason 	if (IS_ERR(c))
20895f39d397SChris Mason 		return PTR_ERR(c);
2090925baeddSChris Mason 
2091f0486c68SYan, Zheng 	root_add_used(root, root->nodesize);
2092f0486c68SYan, Zheng 
20935d4f98a2SYan Zheng 	memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
20945f39d397SChris Mason 	btrfs_set_header_nritems(c, 1);
20955f39d397SChris Mason 	btrfs_set_header_level(c, level);
2096db94535dSChris Mason 	btrfs_set_header_bytenr(c, c->start);
20975f39d397SChris Mason 	btrfs_set_header_generation(c, trans->transid);
20985d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
20995f39d397SChris Mason 	btrfs_set_header_owner(c, root->root_key.objectid);
2100d5719762SChris Mason 
21015f39d397SChris Mason 	write_extent_buffer(c, root->fs_info->fsid,
21025f39d397SChris Mason 			    (unsigned long)btrfs_header_fsid(c),
21035f39d397SChris Mason 			    BTRFS_FSID_SIZE);
2104e17cade2SChris Mason 
2105e17cade2SChris Mason 	write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2106e17cade2SChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(c),
2107e17cade2SChris Mason 			    BTRFS_UUID_SIZE);
2108e17cade2SChris Mason 
21095f39d397SChris Mason 	btrfs_set_node_key(c, &lower_key, 0);
2110db94535dSChris Mason 	btrfs_set_node_blockptr(c, 0, lower->start);
21117bb86316SChris Mason 	lower_gen = btrfs_header_generation(lower);
211231840ae1SZheng Yan 	WARN_ON(lower_gen != trans->transid);
21137bb86316SChris Mason 
21147bb86316SChris Mason 	btrfs_set_node_ptr_generation(c, 0, lower_gen);
21155f39d397SChris Mason 
21165f39d397SChris Mason 	btrfs_mark_buffer_dirty(c);
2117d5719762SChris Mason 
2118925baeddSChris Mason 	old = root->node;
2119240f62c8SChris Mason 	rcu_assign_pointer(root->node, c);
2120925baeddSChris Mason 
2121925baeddSChris Mason 	/* the super has an extra ref to root->node */
2122925baeddSChris Mason 	free_extent_buffer(old);
2123925baeddSChris Mason 
21240b86a832SChris Mason 	add_root_to_dirty_list(root);
21255f39d397SChris Mason 	extent_buffer_get(c);
21265f39d397SChris Mason 	path->nodes[level] = c;
2127bd681513SChris Mason 	path->locks[level] = BTRFS_WRITE_LOCK;
212874123bd7SChris Mason 	path->slots[level] = 0;
212974123bd7SChris Mason 	return 0;
213074123bd7SChris Mason }
21315c680ed6SChris Mason 
21325c680ed6SChris Mason /*
21335c680ed6SChris Mason  * worker function to insert a single pointer in a node.
21345c680ed6SChris Mason  * the node should have enough room for the pointer already
213597571fd0SChris Mason  *
21365c680ed6SChris Mason  * slot and level indicate where you want the key to go, and
21375c680ed6SChris Mason  * blocknr is the block the key points to.
21385c680ed6SChris Mason  */
2139143bede5SJeff Mahoney static void insert_ptr(struct btrfs_trans_handle *trans,
2140143bede5SJeff Mahoney 		       struct btrfs_root *root, struct btrfs_path *path,
2141143bede5SJeff Mahoney 		       struct btrfs_disk_key *key, u64 bytenr,
2142143bede5SJeff Mahoney 		       int slot, int level)
21435c680ed6SChris Mason {
21445f39d397SChris Mason 	struct extent_buffer *lower;
21455c680ed6SChris Mason 	int nritems;
21465c680ed6SChris Mason 
21475c680ed6SChris Mason 	BUG_ON(!path->nodes[level]);
2148f0486c68SYan, Zheng 	btrfs_assert_tree_locked(path->nodes[level]);
21495f39d397SChris Mason 	lower = path->nodes[level];
21505f39d397SChris Mason 	nritems = btrfs_header_nritems(lower);
2151c293498bSStoyan Gaydarov 	BUG_ON(slot > nritems);
2152143bede5SJeff Mahoney 	BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
215374123bd7SChris Mason 	if (slot != nritems) {
21545f39d397SChris Mason 		memmove_extent_buffer(lower,
21555f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot + 1),
21565f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot),
2157123abc88SChris Mason 			      (nritems - slot) * sizeof(struct btrfs_key_ptr));
215874123bd7SChris Mason 	}
21595f39d397SChris Mason 	btrfs_set_node_key(lower, key, slot);
2160db94535dSChris Mason 	btrfs_set_node_blockptr(lower, slot, bytenr);
216174493f7aSChris Mason 	WARN_ON(trans->transid == 0);
216274493f7aSChris Mason 	btrfs_set_node_ptr_generation(lower, slot, trans->transid);
21635f39d397SChris Mason 	btrfs_set_header_nritems(lower, nritems + 1);
21645f39d397SChris Mason 	btrfs_mark_buffer_dirty(lower);
216574123bd7SChris Mason }
216674123bd7SChris Mason 
216797571fd0SChris Mason /*
216897571fd0SChris Mason  * split the node at the specified level in path in two.
216997571fd0SChris Mason  * The path is corrected to point to the appropriate node after the split
217097571fd0SChris Mason  *
217197571fd0SChris Mason  * Before splitting this tries to make some room in the node by pushing
217297571fd0SChris Mason  * left and right, if either one works, it returns right away.
2173aa5d6bedSChris Mason  *
2174aa5d6bedSChris Mason  * returns 0 on success and < 0 on failure
217597571fd0SChris Mason  */
2176e02119d5SChris Mason static noinline int split_node(struct btrfs_trans_handle *trans,
2177e02119d5SChris Mason 			       struct btrfs_root *root,
2178e02119d5SChris Mason 			       struct btrfs_path *path, int level)
2179be0e5c09SChris Mason {
21805f39d397SChris Mason 	struct extent_buffer *c;
21815f39d397SChris Mason 	struct extent_buffer *split;
21825f39d397SChris Mason 	struct btrfs_disk_key disk_key;
2183be0e5c09SChris Mason 	int mid;
21845c680ed6SChris Mason 	int ret;
21857518a238SChris Mason 	u32 c_nritems;
2186be0e5c09SChris Mason 
21875f39d397SChris Mason 	c = path->nodes[level];
21887bb86316SChris Mason 	WARN_ON(btrfs_header_generation(c) != trans->transid);
21895f39d397SChris Mason 	if (c == root->node) {
21905c680ed6SChris Mason 		/* trying to split the root, lets make a new one */
2191e089f05cSChris Mason 		ret = insert_new_root(trans, root, path, level + 1);
21925c680ed6SChris Mason 		if (ret)
21935c680ed6SChris Mason 			return ret;
2194b3612421SChris Mason 	} else {
2195e66f709bSChris Mason 		ret = push_nodes_for_insert(trans, root, path, level);
21965f39d397SChris Mason 		c = path->nodes[level];
21975f39d397SChris Mason 		if (!ret && btrfs_header_nritems(c) <
2198c448acf0SChris Mason 		    BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
2199e66f709bSChris Mason 			return 0;
220054aa1f4dSChris Mason 		if (ret < 0)
220154aa1f4dSChris Mason 			return ret;
22025c680ed6SChris Mason 	}
2203e66f709bSChris Mason 
22045f39d397SChris Mason 	c_nritems = btrfs_header_nritems(c);
22055d4f98a2SYan Zheng 	mid = (c_nritems + 1) / 2;
22065d4f98a2SYan Zheng 	btrfs_node_key(c, &disk_key, mid);
22077bb86316SChris Mason 
22085d4f98a2SYan Zheng 	split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
22097bb86316SChris Mason 					root->root_key.objectid,
221066d7e7f0SArne Jansen 					&disk_key, level, c->start, 0, 0);
22115f39d397SChris Mason 	if (IS_ERR(split))
22125f39d397SChris Mason 		return PTR_ERR(split);
221354aa1f4dSChris Mason 
2214f0486c68SYan, Zheng 	root_add_used(root, root->nodesize);
2215f0486c68SYan, Zheng 
22165d4f98a2SYan Zheng 	memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
22175f39d397SChris Mason 	btrfs_set_header_level(split, btrfs_header_level(c));
2218db94535dSChris Mason 	btrfs_set_header_bytenr(split, split->start);
22195f39d397SChris Mason 	btrfs_set_header_generation(split, trans->transid);
22205d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
22215f39d397SChris Mason 	btrfs_set_header_owner(split, root->root_key.objectid);
22225f39d397SChris Mason 	write_extent_buffer(split, root->fs_info->fsid,
22235f39d397SChris Mason 			    (unsigned long)btrfs_header_fsid(split),
22245f39d397SChris Mason 			    BTRFS_FSID_SIZE);
2225e17cade2SChris Mason 	write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2226e17cade2SChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(split),
2227e17cade2SChris Mason 			    BTRFS_UUID_SIZE);
22285f39d397SChris Mason 
22295f39d397SChris Mason 
22305f39d397SChris Mason 	copy_extent_buffer(split, c,
22315f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
22325f39d397SChris Mason 			   btrfs_node_key_ptr_offset(mid),
2233123abc88SChris Mason 			   (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
22345f39d397SChris Mason 	btrfs_set_header_nritems(split, c_nritems - mid);
22355f39d397SChris Mason 	btrfs_set_header_nritems(c, mid);
2236aa5d6bedSChris Mason 	ret = 0;
2237aa5d6bedSChris Mason 
22385f39d397SChris Mason 	btrfs_mark_buffer_dirty(c);
22395f39d397SChris Mason 	btrfs_mark_buffer_dirty(split);
22405f39d397SChris Mason 
2241143bede5SJeff Mahoney 	insert_ptr(trans, root, path, &disk_key, split->start,
2242143bede5SJeff Mahoney 		   path->slots[level + 1] + 1, level + 1);
2243aa5d6bedSChris Mason 
22445de08d7dSChris Mason 	if (path->slots[level] >= mid) {
22455c680ed6SChris Mason 		path->slots[level] -= mid;
2246925baeddSChris Mason 		btrfs_tree_unlock(c);
22475f39d397SChris Mason 		free_extent_buffer(c);
22485f39d397SChris Mason 		path->nodes[level] = split;
22495c680ed6SChris Mason 		path->slots[level + 1] += 1;
2250eb60ceacSChris Mason 	} else {
2251925baeddSChris Mason 		btrfs_tree_unlock(split);
22525f39d397SChris Mason 		free_extent_buffer(split);
2253be0e5c09SChris Mason 	}
2254aa5d6bedSChris Mason 	return ret;
2255be0e5c09SChris Mason }
2256be0e5c09SChris Mason 
225774123bd7SChris Mason /*
225874123bd7SChris Mason  * how many bytes are required to store the items in a leaf.  start
225974123bd7SChris Mason  * and nr indicate which items in the leaf to check.  This totals up the
226074123bd7SChris Mason  * space used both by the item structs and the item data
226174123bd7SChris Mason  */
22625f39d397SChris Mason static int leaf_space_used(struct extent_buffer *l, int start, int nr)
2263be0e5c09SChris Mason {
2264be0e5c09SChris Mason 	int data_len;
22655f39d397SChris Mason 	int nritems = btrfs_header_nritems(l);
2266d4dbff95SChris Mason 	int end = min(nritems, start + nr) - 1;
2267be0e5c09SChris Mason 
2268be0e5c09SChris Mason 	if (!nr)
2269be0e5c09SChris Mason 		return 0;
22705f39d397SChris Mason 	data_len = btrfs_item_end_nr(l, start);
22715f39d397SChris Mason 	data_len = data_len - btrfs_item_offset_nr(l, end);
22720783fcfcSChris Mason 	data_len += sizeof(struct btrfs_item) * nr;
2273d4dbff95SChris Mason 	WARN_ON(data_len < 0);
2274be0e5c09SChris Mason 	return data_len;
2275be0e5c09SChris Mason }
2276be0e5c09SChris Mason 
227774123bd7SChris Mason /*
2278d4dbff95SChris Mason  * The space between the end of the leaf items and
2279d4dbff95SChris Mason  * the start of the leaf data.  IOW, how much room
2280d4dbff95SChris Mason  * the leaf has left for both items and data
2281d4dbff95SChris Mason  */
2282d397712bSChris Mason noinline int btrfs_leaf_free_space(struct btrfs_root *root,
2283e02119d5SChris Mason 				   struct extent_buffer *leaf)
2284d4dbff95SChris Mason {
22855f39d397SChris Mason 	int nritems = btrfs_header_nritems(leaf);
22865f39d397SChris Mason 	int ret;
22875f39d397SChris Mason 	ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
22885f39d397SChris Mason 	if (ret < 0) {
2289d397712bSChris Mason 		printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2290d397712bSChris Mason 		       "used %d nritems %d\n",
2291ae2f5411SJens Axboe 		       ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
22925f39d397SChris Mason 		       leaf_space_used(leaf, 0, nritems), nritems);
22935f39d397SChris Mason 	}
22945f39d397SChris Mason 	return ret;
2295d4dbff95SChris Mason }
2296d4dbff95SChris Mason 
229799d8f83cSChris Mason /*
229899d8f83cSChris Mason  * min slot controls the lowest index we're willing to push to the
229999d8f83cSChris Mason  * right.  We'll push up to and including min_slot, but no lower
230099d8f83cSChris Mason  */
230144871b1bSChris Mason static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
230244871b1bSChris Mason 				      struct btrfs_root *root,
230344871b1bSChris Mason 				      struct btrfs_path *path,
230444871b1bSChris Mason 				      int data_size, int empty,
230544871b1bSChris Mason 				      struct extent_buffer *right,
230699d8f83cSChris Mason 				      int free_space, u32 left_nritems,
230799d8f83cSChris Mason 				      u32 min_slot)
230800ec4c51SChris Mason {
23095f39d397SChris Mason 	struct extent_buffer *left = path->nodes[0];
231044871b1bSChris Mason 	struct extent_buffer *upper = path->nodes[1];
23115f39d397SChris Mason 	struct btrfs_disk_key disk_key;
231200ec4c51SChris Mason 	int slot;
231334a38218SChris Mason 	u32 i;
231400ec4c51SChris Mason 	int push_space = 0;
231500ec4c51SChris Mason 	int push_items = 0;
23160783fcfcSChris Mason 	struct btrfs_item *item;
231734a38218SChris Mason 	u32 nr;
23187518a238SChris Mason 	u32 right_nritems;
23195f39d397SChris Mason 	u32 data_end;
2320db94535dSChris Mason 	u32 this_item_size;
232100ec4c51SChris Mason 
232234a38218SChris Mason 	if (empty)
232334a38218SChris Mason 		nr = 0;
232434a38218SChris Mason 	else
232599d8f83cSChris Mason 		nr = max_t(u32, 1, min_slot);
232634a38218SChris Mason 
232731840ae1SZheng Yan 	if (path->slots[0] >= left_nritems)
232887b29b20SYan Zheng 		push_space += data_size;
232931840ae1SZheng Yan 
233044871b1bSChris Mason 	slot = path->slots[1];
233134a38218SChris Mason 	i = left_nritems - 1;
233234a38218SChris Mason 	while (i >= nr) {
23335f39d397SChris Mason 		item = btrfs_item_nr(left, i);
2334db94535dSChris Mason 
233531840ae1SZheng Yan 		if (!empty && push_items > 0) {
233631840ae1SZheng Yan 			if (path->slots[0] > i)
233731840ae1SZheng Yan 				break;
233831840ae1SZheng Yan 			if (path->slots[0] == i) {
233931840ae1SZheng Yan 				int space = btrfs_leaf_free_space(root, left);
234031840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
234131840ae1SZheng Yan 					break;
234231840ae1SZheng Yan 			}
234331840ae1SZheng Yan 		}
234431840ae1SZheng Yan 
234500ec4c51SChris Mason 		if (path->slots[0] == i)
234687b29b20SYan Zheng 			push_space += data_size;
2347db94535dSChris Mason 
2348db94535dSChris Mason 		this_item_size = btrfs_item_size(left, item);
2349db94535dSChris Mason 		if (this_item_size + sizeof(*item) + push_space > free_space)
235000ec4c51SChris Mason 			break;
235131840ae1SZheng Yan 
235200ec4c51SChris Mason 		push_items++;
2353db94535dSChris Mason 		push_space += this_item_size + sizeof(*item);
235434a38218SChris Mason 		if (i == 0)
235534a38218SChris Mason 			break;
235634a38218SChris Mason 		i--;
2357db94535dSChris Mason 	}
23585f39d397SChris Mason 
2359925baeddSChris Mason 	if (push_items == 0)
2360925baeddSChris Mason 		goto out_unlock;
23615f39d397SChris Mason 
236234a38218SChris Mason 	if (!empty && push_items == left_nritems)
2363a429e513SChris Mason 		WARN_ON(1);
23645f39d397SChris Mason 
236500ec4c51SChris Mason 	/* push left to right */
23665f39d397SChris Mason 	right_nritems = btrfs_header_nritems(right);
236734a38218SChris Mason 
23685f39d397SChris Mason 	push_space = btrfs_item_end_nr(left, left_nritems - push_items);
2369123abc88SChris Mason 	push_space -= leaf_data_end(root, left);
23705f39d397SChris Mason 
237100ec4c51SChris Mason 	/* make room in the right data area */
23725f39d397SChris Mason 	data_end = leaf_data_end(root, right);
23735f39d397SChris Mason 	memmove_extent_buffer(right,
23745f39d397SChris Mason 			      btrfs_leaf_data(right) + data_end - push_space,
23755f39d397SChris Mason 			      btrfs_leaf_data(right) + data_end,
23765f39d397SChris Mason 			      BTRFS_LEAF_DATA_SIZE(root) - data_end);
23775f39d397SChris Mason 
237800ec4c51SChris Mason 	/* copy from the left data area */
23795f39d397SChris Mason 	copy_extent_buffer(right, left, btrfs_leaf_data(right) +
2380d6025579SChris Mason 		     BTRFS_LEAF_DATA_SIZE(root) - push_space,
2381d6025579SChris Mason 		     btrfs_leaf_data(left) + leaf_data_end(root, left),
2382d6025579SChris Mason 		     push_space);
23835f39d397SChris Mason 
23845f39d397SChris Mason 	memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
23855f39d397SChris Mason 			      btrfs_item_nr_offset(0),
23860783fcfcSChris Mason 			      right_nritems * sizeof(struct btrfs_item));
23875f39d397SChris Mason 
238800ec4c51SChris Mason 	/* copy the items from left to right */
23895f39d397SChris Mason 	copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
23905f39d397SChris Mason 		   btrfs_item_nr_offset(left_nritems - push_items),
23910783fcfcSChris Mason 		   push_items * sizeof(struct btrfs_item));
239200ec4c51SChris Mason 
239300ec4c51SChris Mason 	/* update the item pointers */
23947518a238SChris Mason 	right_nritems += push_items;
23955f39d397SChris Mason 	btrfs_set_header_nritems(right, right_nritems);
2396123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root);
23977518a238SChris Mason 	for (i = 0; i < right_nritems; i++) {
23985f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2399db94535dSChris Mason 		push_space -= btrfs_item_size(right, item);
2400db94535dSChris Mason 		btrfs_set_item_offset(right, item, push_space);
2401db94535dSChris Mason 	}
2402db94535dSChris Mason 
24037518a238SChris Mason 	left_nritems -= push_items;
24045f39d397SChris Mason 	btrfs_set_header_nritems(left, left_nritems);
240500ec4c51SChris Mason 
240634a38218SChris Mason 	if (left_nritems)
24075f39d397SChris Mason 		btrfs_mark_buffer_dirty(left);
2408f0486c68SYan, Zheng 	else
2409f0486c68SYan, Zheng 		clean_tree_block(trans, root, left);
2410f0486c68SYan, Zheng 
24115f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
2412a429e513SChris Mason 
24135f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
24145f39d397SChris Mason 	btrfs_set_node_key(upper, &disk_key, slot + 1);
2415d6025579SChris Mason 	btrfs_mark_buffer_dirty(upper);
241602217ed2SChris Mason 
241700ec4c51SChris Mason 	/* then fixup the leaf pointer in the path */
24187518a238SChris Mason 	if (path->slots[0] >= left_nritems) {
24197518a238SChris Mason 		path->slots[0] -= left_nritems;
2420925baeddSChris Mason 		if (btrfs_header_nritems(path->nodes[0]) == 0)
2421925baeddSChris Mason 			clean_tree_block(trans, root, path->nodes[0]);
2422925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
24235f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
24245f39d397SChris Mason 		path->nodes[0] = right;
242500ec4c51SChris Mason 		path->slots[1] += 1;
242600ec4c51SChris Mason 	} else {
2427925baeddSChris Mason 		btrfs_tree_unlock(right);
24285f39d397SChris Mason 		free_extent_buffer(right);
242900ec4c51SChris Mason 	}
243000ec4c51SChris Mason 	return 0;
2431925baeddSChris Mason 
2432925baeddSChris Mason out_unlock:
2433925baeddSChris Mason 	btrfs_tree_unlock(right);
2434925baeddSChris Mason 	free_extent_buffer(right);
2435925baeddSChris Mason 	return 1;
243600ec4c51SChris Mason }
2437925baeddSChris Mason 
243800ec4c51SChris Mason /*
243944871b1bSChris Mason  * push some data in the path leaf to the right, trying to free up at
244074123bd7SChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
244144871b1bSChris Mason  *
244244871b1bSChris Mason  * returns 1 if the push failed because the other node didn't have enough
244344871b1bSChris Mason  * room, 0 if everything worked out and < 0 if there were major errors.
244499d8f83cSChris Mason  *
244599d8f83cSChris Mason  * this will push starting from min_slot to the end of the leaf.  It won't
244699d8f83cSChris Mason  * push any slot lower than min_slot
244774123bd7SChris Mason  */
244844871b1bSChris Mason static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
244999d8f83cSChris Mason 			   *root, struct btrfs_path *path,
245099d8f83cSChris Mason 			   int min_data_size, int data_size,
245199d8f83cSChris Mason 			   int empty, u32 min_slot)
2452be0e5c09SChris Mason {
245344871b1bSChris Mason 	struct extent_buffer *left = path->nodes[0];
245444871b1bSChris Mason 	struct extent_buffer *right;
245544871b1bSChris Mason 	struct extent_buffer *upper;
245644871b1bSChris Mason 	int slot;
245744871b1bSChris Mason 	int free_space;
245844871b1bSChris Mason 	u32 left_nritems;
245944871b1bSChris Mason 	int ret;
246044871b1bSChris Mason 
246144871b1bSChris Mason 	if (!path->nodes[1])
246244871b1bSChris Mason 		return 1;
246344871b1bSChris Mason 
246444871b1bSChris Mason 	slot = path->slots[1];
246544871b1bSChris Mason 	upper = path->nodes[1];
246644871b1bSChris Mason 	if (slot >= btrfs_header_nritems(upper) - 1)
246744871b1bSChris Mason 		return 1;
246844871b1bSChris Mason 
246944871b1bSChris Mason 	btrfs_assert_tree_locked(path->nodes[1]);
247044871b1bSChris Mason 
247144871b1bSChris Mason 	right = read_node_slot(root, upper, slot + 1);
247291ca338dSTsutomu Itoh 	if (right == NULL)
247391ca338dSTsutomu Itoh 		return 1;
247491ca338dSTsutomu Itoh 
247544871b1bSChris Mason 	btrfs_tree_lock(right);
247644871b1bSChris Mason 	btrfs_set_lock_blocking(right);
247744871b1bSChris Mason 
247844871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, right);
247944871b1bSChris Mason 	if (free_space < data_size)
248044871b1bSChris Mason 		goto out_unlock;
248144871b1bSChris Mason 
248244871b1bSChris Mason 	/* cow and double check */
248344871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, right, upper,
248444871b1bSChris Mason 			      slot + 1, &right);
248544871b1bSChris Mason 	if (ret)
248644871b1bSChris Mason 		goto out_unlock;
248744871b1bSChris Mason 
248844871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, right);
248944871b1bSChris Mason 	if (free_space < data_size)
249044871b1bSChris Mason 		goto out_unlock;
249144871b1bSChris Mason 
249244871b1bSChris Mason 	left_nritems = btrfs_header_nritems(left);
249344871b1bSChris Mason 	if (left_nritems == 0)
249444871b1bSChris Mason 		goto out_unlock;
249544871b1bSChris Mason 
249699d8f83cSChris Mason 	return __push_leaf_right(trans, root, path, min_data_size, empty,
249799d8f83cSChris Mason 				right, free_space, left_nritems, min_slot);
249844871b1bSChris Mason out_unlock:
249944871b1bSChris Mason 	btrfs_tree_unlock(right);
250044871b1bSChris Mason 	free_extent_buffer(right);
250144871b1bSChris Mason 	return 1;
250244871b1bSChris Mason }
250344871b1bSChris Mason 
250444871b1bSChris Mason /*
250544871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
250644871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
250799d8f83cSChris Mason  *
250899d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
250999d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us do all the
251099d8f83cSChris Mason  * items
251144871b1bSChris Mason  */
251244871b1bSChris Mason static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
251344871b1bSChris Mason 				     struct btrfs_root *root,
251444871b1bSChris Mason 				     struct btrfs_path *path, int data_size,
251544871b1bSChris Mason 				     int empty, struct extent_buffer *left,
251699d8f83cSChris Mason 				     int free_space, u32 right_nritems,
251799d8f83cSChris Mason 				     u32 max_slot)
251844871b1bSChris Mason {
25195f39d397SChris Mason 	struct btrfs_disk_key disk_key;
25205f39d397SChris Mason 	struct extent_buffer *right = path->nodes[0];
2521be0e5c09SChris Mason 	int i;
2522be0e5c09SChris Mason 	int push_space = 0;
2523be0e5c09SChris Mason 	int push_items = 0;
25240783fcfcSChris Mason 	struct btrfs_item *item;
25257518a238SChris Mason 	u32 old_left_nritems;
252634a38218SChris Mason 	u32 nr;
2527aa5d6bedSChris Mason 	int ret = 0;
2528db94535dSChris Mason 	u32 this_item_size;
2529db94535dSChris Mason 	u32 old_left_item_size;
2530be0e5c09SChris Mason 
253134a38218SChris Mason 	if (empty)
253299d8f83cSChris Mason 		nr = min(right_nritems, max_slot);
253334a38218SChris Mason 	else
253499d8f83cSChris Mason 		nr = min(right_nritems - 1, max_slot);
253534a38218SChris Mason 
253634a38218SChris Mason 	for (i = 0; i < nr; i++) {
25375f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2538db94535dSChris Mason 
253931840ae1SZheng Yan 		if (!empty && push_items > 0) {
254031840ae1SZheng Yan 			if (path->slots[0] < i)
254131840ae1SZheng Yan 				break;
254231840ae1SZheng Yan 			if (path->slots[0] == i) {
254331840ae1SZheng Yan 				int space = btrfs_leaf_free_space(root, right);
254431840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
254531840ae1SZheng Yan 					break;
254631840ae1SZheng Yan 			}
254731840ae1SZheng Yan 		}
254831840ae1SZheng Yan 
2549be0e5c09SChris Mason 		if (path->slots[0] == i)
255087b29b20SYan Zheng 			push_space += data_size;
2551db94535dSChris Mason 
2552db94535dSChris Mason 		this_item_size = btrfs_item_size(right, item);
2553db94535dSChris Mason 		if (this_item_size + sizeof(*item) + push_space > free_space)
2554be0e5c09SChris Mason 			break;
2555db94535dSChris Mason 
2556be0e5c09SChris Mason 		push_items++;
2557db94535dSChris Mason 		push_space += this_item_size + sizeof(*item);
2558be0e5c09SChris Mason 	}
2559db94535dSChris Mason 
2560be0e5c09SChris Mason 	if (push_items == 0) {
2561925baeddSChris Mason 		ret = 1;
2562925baeddSChris Mason 		goto out;
2563be0e5c09SChris Mason 	}
256434a38218SChris Mason 	if (!empty && push_items == btrfs_header_nritems(right))
2565a429e513SChris Mason 		WARN_ON(1);
25665f39d397SChris Mason 
2567be0e5c09SChris Mason 	/* push data from right to left */
25685f39d397SChris Mason 	copy_extent_buffer(left, right,
25695f39d397SChris Mason 			   btrfs_item_nr_offset(btrfs_header_nritems(left)),
25705f39d397SChris Mason 			   btrfs_item_nr_offset(0),
25715f39d397SChris Mason 			   push_items * sizeof(struct btrfs_item));
25725f39d397SChris Mason 
2573123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root) -
25745f39d397SChris Mason 		     btrfs_item_offset_nr(right, push_items - 1);
25755f39d397SChris Mason 
25765f39d397SChris Mason 	copy_extent_buffer(left, right, btrfs_leaf_data(left) +
2577d6025579SChris Mason 		     leaf_data_end(root, left) - push_space,
2578123abc88SChris Mason 		     btrfs_leaf_data(right) +
25795f39d397SChris Mason 		     btrfs_item_offset_nr(right, push_items - 1),
2580be0e5c09SChris Mason 		     push_space);
25815f39d397SChris Mason 	old_left_nritems = btrfs_header_nritems(left);
258287b29b20SYan Zheng 	BUG_ON(old_left_nritems <= 0);
2583eb60ceacSChris Mason 
2584db94535dSChris Mason 	old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
2585be0e5c09SChris Mason 	for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
25865f39d397SChris Mason 		u32 ioff;
2587db94535dSChris Mason 
25885f39d397SChris Mason 		item = btrfs_item_nr(left, i);
2589db94535dSChris Mason 
25905f39d397SChris Mason 		ioff = btrfs_item_offset(left, item);
25915f39d397SChris Mason 		btrfs_set_item_offset(left, item,
2592db94535dSChris Mason 		      ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
2593be0e5c09SChris Mason 	}
25945f39d397SChris Mason 	btrfs_set_header_nritems(left, old_left_nritems + push_items);
2595be0e5c09SChris Mason 
2596be0e5c09SChris Mason 	/* fixup right node */
259734a38218SChris Mason 	if (push_items > right_nritems) {
2598d397712bSChris Mason 		printk(KERN_CRIT "push items %d nr %u\n", push_items,
2599d397712bSChris Mason 		       right_nritems);
260034a38218SChris Mason 		WARN_ON(1);
260134a38218SChris Mason 	}
260234a38218SChris Mason 
260334a38218SChris Mason 	if (push_items < right_nritems) {
26045f39d397SChris Mason 		push_space = btrfs_item_offset_nr(right, push_items - 1) -
2605123abc88SChris Mason 						  leaf_data_end(root, right);
26065f39d397SChris Mason 		memmove_extent_buffer(right, btrfs_leaf_data(right) +
2607d6025579SChris Mason 				      BTRFS_LEAF_DATA_SIZE(root) - push_space,
2608d6025579SChris Mason 				      btrfs_leaf_data(right) +
2609123abc88SChris Mason 				      leaf_data_end(root, right), push_space);
26105f39d397SChris Mason 
26115f39d397SChris Mason 		memmove_extent_buffer(right, btrfs_item_nr_offset(0),
26125f39d397SChris Mason 			      btrfs_item_nr_offset(push_items),
26135f39d397SChris Mason 			     (btrfs_header_nritems(right) - push_items) *
26140783fcfcSChris Mason 			     sizeof(struct btrfs_item));
261534a38218SChris Mason 	}
2616eef1c494SYan 	right_nritems -= push_items;
2617eef1c494SYan 	btrfs_set_header_nritems(right, right_nritems);
2618123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root);
26195f39d397SChris Mason 	for (i = 0; i < right_nritems; i++) {
26205f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2621db94535dSChris Mason 
2622db94535dSChris Mason 		push_space = push_space - btrfs_item_size(right, item);
2623db94535dSChris Mason 		btrfs_set_item_offset(right, item, push_space);
2624db94535dSChris Mason 	}
2625eb60ceacSChris Mason 
26265f39d397SChris Mason 	btrfs_mark_buffer_dirty(left);
262734a38218SChris Mason 	if (right_nritems)
26285f39d397SChris Mason 		btrfs_mark_buffer_dirty(right);
2629f0486c68SYan, Zheng 	else
2630f0486c68SYan, Zheng 		clean_tree_block(trans, root, right);
2631098f59c2SChris Mason 
26325f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
2633143bede5SJeff Mahoney 	fixup_low_keys(trans, root, path, &disk_key, 1);
2634be0e5c09SChris Mason 
2635be0e5c09SChris Mason 	/* then fixup the leaf pointer in the path */
2636be0e5c09SChris Mason 	if (path->slots[0] < push_items) {
2637be0e5c09SChris Mason 		path->slots[0] += old_left_nritems;
2638925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
26395f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
26405f39d397SChris Mason 		path->nodes[0] = left;
2641be0e5c09SChris Mason 		path->slots[1] -= 1;
2642be0e5c09SChris Mason 	} else {
2643925baeddSChris Mason 		btrfs_tree_unlock(left);
26445f39d397SChris Mason 		free_extent_buffer(left);
2645be0e5c09SChris Mason 		path->slots[0] -= push_items;
2646be0e5c09SChris Mason 	}
2647eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
2648aa5d6bedSChris Mason 	return ret;
2649925baeddSChris Mason out:
2650925baeddSChris Mason 	btrfs_tree_unlock(left);
2651925baeddSChris Mason 	free_extent_buffer(left);
2652925baeddSChris Mason 	return ret;
2653be0e5c09SChris Mason }
2654be0e5c09SChris Mason 
265574123bd7SChris Mason /*
265644871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
265744871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
265899d8f83cSChris Mason  *
265999d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
266099d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us push all the
266199d8f83cSChris Mason  * items
266244871b1bSChris Mason  */
266344871b1bSChris Mason static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
266499d8f83cSChris Mason 			  *root, struct btrfs_path *path, int min_data_size,
266599d8f83cSChris Mason 			  int data_size, int empty, u32 max_slot)
266644871b1bSChris Mason {
266744871b1bSChris Mason 	struct extent_buffer *right = path->nodes[0];
266844871b1bSChris Mason 	struct extent_buffer *left;
266944871b1bSChris Mason 	int slot;
267044871b1bSChris Mason 	int free_space;
267144871b1bSChris Mason 	u32 right_nritems;
267244871b1bSChris Mason 	int ret = 0;
267344871b1bSChris Mason 
267444871b1bSChris Mason 	slot = path->slots[1];
267544871b1bSChris Mason 	if (slot == 0)
267644871b1bSChris Mason 		return 1;
267744871b1bSChris Mason 	if (!path->nodes[1])
267844871b1bSChris Mason 		return 1;
267944871b1bSChris Mason 
268044871b1bSChris Mason 	right_nritems = btrfs_header_nritems(right);
268144871b1bSChris Mason 	if (right_nritems == 0)
268244871b1bSChris Mason 		return 1;
268344871b1bSChris Mason 
268444871b1bSChris Mason 	btrfs_assert_tree_locked(path->nodes[1]);
268544871b1bSChris Mason 
268644871b1bSChris Mason 	left = read_node_slot(root, path->nodes[1], slot - 1);
268791ca338dSTsutomu Itoh 	if (left == NULL)
268891ca338dSTsutomu Itoh 		return 1;
268991ca338dSTsutomu Itoh 
269044871b1bSChris Mason 	btrfs_tree_lock(left);
269144871b1bSChris Mason 	btrfs_set_lock_blocking(left);
269244871b1bSChris Mason 
269344871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, left);
269444871b1bSChris Mason 	if (free_space < data_size) {
269544871b1bSChris Mason 		ret = 1;
269644871b1bSChris Mason 		goto out;
269744871b1bSChris Mason 	}
269844871b1bSChris Mason 
269944871b1bSChris Mason 	/* cow and double check */
270044871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, left,
270144871b1bSChris Mason 			      path->nodes[1], slot - 1, &left);
270244871b1bSChris Mason 	if (ret) {
270344871b1bSChris Mason 		/* we hit -ENOSPC, but it isn't fatal here */
270444871b1bSChris Mason 		ret = 1;
270544871b1bSChris Mason 		goto out;
270644871b1bSChris Mason 	}
270744871b1bSChris Mason 
270844871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, left);
270944871b1bSChris Mason 	if (free_space < data_size) {
271044871b1bSChris Mason 		ret = 1;
271144871b1bSChris Mason 		goto out;
271244871b1bSChris Mason 	}
271344871b1bSChris Mason 
271499d8f83cSChris Mason 	return __push_leaf_left(trans, root, path, min_data_size,
271599d8f83cSChris Mason 			       empty, left, free_space, right_nritems,
271699d8f83cSChris Mason 			       max_slot);
271744871b1bSChris Mason out:
271844871b1bSChris Mason 	btrfs_tree_unlock(left);
271944871b1bSChris Mason 	free_extent_buffer(left);
272044871b1bSChris Mason 	return ret;
272144871b1bSChris Mason }
272244871b1bSChris Mason 
272344871b1bSChris Mason /*
272474123bd7SChris Mason  * split the path's leaf in two, making sure there is at least data_size
272574123bd7SChris Mason  * available for the resulting leaf level of the path.
272674123bd7SChris Mason  */
2727143bede5SJeff Mahoney static noinline void 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 	struct btrfs_disk_key disk_key;
2738be0e5c09SChris Mason 
27395f39d397SChris Mason 	nritems = nritems - mid;
27405f39d397SChris Mason 	btrfs_set_header_nritems(right, nritems);
27415f39d397SChris Mason 	data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
27425f39d397SChris Mason 
27435f39d397SChris Mason 	copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
27445f39d397SChris Mason 			   btrfs_item_nr_offset(mid),
27455f39d397SChris Mason 			   nritems * sizeof(struct btrfs_item));
27465f39d397SChris Mason 
27475f39d397SChris Mason 	copy_extent_buffer(right, l,
2748d6025579SChris Mason 		     btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2749123abc88SChris Mason 		     data_copy_size, btrfs_leaf_data(l) +
2750123abc88SChris Mason 		     leaf_data_end(root, l), data_copy_size);
275174123bd7SChris Mason 
27525f39d397SChris Mason 	rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
27535f39d397SChris Mason 		      btrfs_item_end_nr(l, mid);
27545f39d397SChris Mason 
27555f39d397SChris Mason 	for (i = 0; i < nritems; i++) {
27565f39d397SChris Mason 		struct btrfs_item *item = btrfs_item_nr(right, i);
2757db94535dSChris Mason 		u32 ioff;
2758db94535dSChris Mason 
2759db94535dSChris Mason 		ioff = btrfs_item_offset(right, item);
27605f39d397SChris Mason 		btrfs_set_item_offset(right, item, ioff + rt_data_off);
27610783fcfcSChris Mason 	}
276274123bd7SChris Mason 
27635f39d397SChris Mason 	btrfs_set_header_nritems(l, mid);
27645f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
2765143bede5SJeff Mahoney 	insert_ptr(trans, root, path, &disk_key, right->start,
2766db94535dSChris Mason 		   path->slots[1] + 1, 1);
27675f39d397SChris Mason 
27685f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
27695f39d397SChris Mason 	btrfs_mark_buffer_dirty(l);
2770eb60ceacSChris Mason 	BUG_ON(path->slots[0] != slot);
27715f39d397SChris Mason 
2772be0e5c09SChris Mason 	if (mid <= slot) {
2773925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
27745f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
27755f39d397SChris Mason 		path->nodes[0] = right;
2776be0e5c09SChris Mason 		path->slots[0] -= mid;
2777be0e5c09SChris Mason 		path->slots[1] += 1;
2778925baeddSChris Mason 	} else {
2779925baeddSChris Mason 		btrfs_tree_unlock(right);
27805f39d397SChris Mason 		free_extent_buffer(right);
2781925baeddSChris Mason 	}
27825f39d397SChris Mason 
2783eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
278444871b1bSChris Mason }
278544871b1bSChris Mason 
278644871b1bSChris Mason /*
278799d8f83cSChris Mason  * double splits happen when we need to insert a big item in the middle
278899d8f83cSChris Mason  * of a leaf.  A double split can leave us with 3 mostly empty leaves:
278999d8f83cSChris Mason  * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
279099d8f83cSChris Mason  *          A                 B                 C
279199d8f83cSChris Mason  *
279299d8f83cSChris Mason  * We avoid this by trying to push the items on either side of our target
279399d8f83cSChris Mason  * into the adjacent leaves.  If all goes well we can avoid the double split
279499d8f83cSChris Mason  * completely.
279599d8f83cSChris Mason  */
279699d8f83cSChris Mason static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
279799d8f83cSChris Mason 					  struct btrfs_root *root,
279899d8f83cSChris Mason 					  struct btrfs_path *path,
279999d8f83cSChris Mason 					  int data_size)
280099d8f83cSChris Mason {
280199d8f83cSChris Mason 	int ret;
280299d8f83cSChris Mason 	int progress = 0;
280399d8f83cSChris Mason 	int slot;
280499d8f83cSChris Mason 	u32 nritems;
280599d8f83cSChris Mason 
280699d8f83cSChris Mason 	slot = path->slots[0];
280799d8f83cSChris Mason 
280899d8f83cSChris Mason 	/*
280999d8f83cSChris Mason 	 * try to push all the items after our slot into the
281099d8f83cSChris Mason 	 * right leaf
281199d8f83cSChris Mason 	 */
281299d8f83cSChris Mason 	ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
281399d8f83cSChris Mason 	if (ret < 0)
281499d8f83cSChris Mason 		return ret;
281599d8f83cSChris Mason 
281699d8f83cSChris Mason 	if (ret == 0)
281799d8f83cSChris Mason 		progress++;
281899d8f83cSChris Mason 
281999d8f83cSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
282099d8f83cSChris Mason 	/*
282199d8f83cSChris Mason 	 * our goal is to get our slot at the start or end of a leaf.  If
282299d8f83cSChris Mason 	 * we've done so we're done
282399d8f83cSChris Mason 	 */
282499d8f83cSChris Mason 	if (path->slots[0] == 0 || path->slots[0] == nritems)
282599d8f83cSChris Mason 		return 0;
282699d8f83cSChris Mason 
282799d8f83cSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
282899d8f83cSChris Mason 		return 0;
282999d8f83cSChris Mason 
283099d8f83cSChris Mason 	/* try to push all the items before our slot into the next leaf */
283199d8f83cSChris Mason 	slot = path->slots[0];
283299d8f83cSChris Mason 	ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
283399d8f83cSChris Mason 	if (ret < 0)
283499d8f83cSChris Mason 		return ret;
283599d8f83cSChris Mason 
283699d8f83cSChris Mason 	if (ret == 0)
283799d8f83cSChris Mason 		progress++;
283899d8f83cSChris Mason 
283999d8f83cSChris Mason 	if (progress)
284099d8f83cSChris Mason 		return 0;
284199d8f83cSChris Mason 	return 1;
284299d8f83cSChris Mason }
284399d8f83cSChris Mason 
284499d8f83cSChris Mason /*
284544871b1bSChris Mason  * split the path's leaf in two, making sure there is at least data_size
284644871b1bSChris Mason  * available for the resulting leaf level of the path.
284744871b1bSChris Mason  *
284844871b1bSChris Mason  * returns 0 if all went well and < 0 on failure.
284944871b1bSChris Mason  */
285044871b1bSChris Mason static noinline int split_leaf(struct btrfs_trans_handle *trans,
285144871b1bSChris Mason 			       struct btrfs_root *root,
285244871b1bSChris Mason 			       struct btrfs_key *ins_key,
285344871b1bSChris Mason 			       struct btrfs_path *path, int data_size,
285444871b1bSChris Mason 			       int extend)
285544871b1bSChris Mason {
28565d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
285744871b1bSChris Mason 	struct extent_buffer *l;
285844871b1bSChris Mason 	u32 nritems;
285944871b1bSChris Mason 	int mid;
286044871b1bSChris Mason 	int slot;
286144871b1bSChris Mason 	struct extent_buffer *right;
286244871b1bSChris Mason 	int ret = 0;
286344871b1bSChris Mason 	int wret;
28645d4f98a2SYan Zheng 	int split;
286544871b1bSChris Mason 	int num_doubles = 0;
286699d8f83cSChris Mason 	int tried_avoid_double = 0;
286744871b1bSChris Mason 
2868a5719521SYan, Zheng 	l = path->nodes[0];
2869a5719521SYan, Zheng 	slot = path->slots[0];
2870a5719521SYan, Zheng 	if (extend && data_size + btrfs_item_size_nr(l, slot) +
2871a5719521SYan, Zheng 	    sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2872a5719521SYan, Zheng 		return -EOVERFLOW;
2873a5719521SYan, Zheng 
287444871b1bSChris Mason 	/* first try to make some room by pushing left and right */
287599d8f83cSChris Mason 	if (data_size) {
287699d8f83cSChris Mason 		wret = push_leaf_right(trans, root, path, data_size,
287799d8f83cSChris Mason 				       data_size, 0, 0);
287844871b1bSChris Mason 		if (wret < 0)
287944871b1bSChris Mason 			return wret;
288044871b1bSChris Mason 		if (wret) {
288199d8f83cSChris Mason 			wret = push_leaf_left(trans, root, path, data_size,
288299d8f83cSChris Mason 					      data_size, 0, (u32)-1);
288344871b1bSChris Mason 			if (wret < 0)
288444871b1bSChris Mason 				return wret;
288544871b1bSChris Mason 		}
288644871b1bSChris Mason 		l = path->nodes[0];
288744871b1bSChris Mason 
288844871b1bSChris Mason 		/* did the pushes work? */
288944871b1bSChris Mason 		if (btrfs_leaf_free_space(root, l) >= data_size)
289044871b1bSChris Mason 			return 0;
289144871b1bSChris Mason 	}
289244871b1bSChris Mason 
289344871b1bSChris Mason 	if (!path->nodes[1]) {
289444871b1bSChris Mason 		ret = insert_new_root(trans, root, path, 1);
289544871b1bSChris Mason 		if (ret)
289644871b1bSChris Mason 			return ret;
289744871b1bSChris Mason 	}
289844871b1bSChris Mason again:
28995d4f98a2SYan Zheng 	split = 1;
290044871b1bSChris Mason 	l = path->nodes[0];
290144871b1bSChris Mason 	slot = path->slots[0];
290244871b1bSChris Mason 	nritems = btrfs_header_nritems(l);
290344871b1bSChris Mason 	mid = (nritems + 1) / 2;
290444871b1bSChris Mason 
29055d4f98a2SYan Zheng 	if (mid <= slot) {
29065d4f98a2SYan Zheng 		if (nritems == 1 ||
29075d4f98a2SYan Zheng 		    leaf_space_used(l, mid, nritems - mid) + data_size >
29085d4f98a2SYan Zheng 			BTRFS_LEAF_DATA_SIZE(root)) {
29095d4f98a2SYan Zheng 			if (slot >= nritems) {
29105d4f98a2SYan Zheng 				split = 0;
29115d4f98a2SYan Zheng 			} else {
29125d4f98a2SYan Zheng 				mid = slot;
29135d4f98a2SYan Zheng 				if (mid != nritems &&
29145d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
29155d4f98a2SYan Zheng 				    data_size > BTRFS_LEAF_DATA_SIZE(root)) {
291699d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
291799d8f83cSChris Mason 						goto push_for_double;
29185d4f98a2SYan Zheng 					split = 2;
29195d4f98a2SYan Zheng 				}
29205d4f98a2SYan Zheng 			}
29215d4f98a2SYan Zheng 		}
29225d4f98a2SYan Zheng 	} else {
29235d4f98a2SYan Zheng 		if (leaf_space_used(l, 0, mid) + data_size >
29245d4f98a2SYan Zheng 			BTRFS_LEAF_DATA_SIZE(root)) {
29255d4f98a2SYan Zheng 			if (!extend && data_size && slot == 0) {
29265d4f98a2SYan Zheng 				split = 0;
29275d4f98a2SYan Zheng 			} else if ((extend || !data_size) && slot == 0) {
29285d4f98a2SYan Zheng 				mid = 1;
29295d4f98a2SYan Zheng 			} else {
29305d4f98a2SYan Zheng 				mid = slot;
29315d4f98a2SYan Zheng 				if (mid != nritems &&
29325d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
29335d4f98a2SYan Zheng 				    data_size > BTRFS_LEAF_DATA_SIZE(root)) {
293499d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
293599d8f83cSChris Mason 						goto push_for_double;
29365d4f98a2SYan Zheng 					split = 2 ;
29375d4f98a2SYan Zheng 				}
29385d4f98a2SYan Zheng 			}
29395d4f98a2SYan Zheng 		}
29405d4f98a2SYan Zheng 	}
29415d4f98a2SYan Zheng 
29425d4f98a2SYan Zheng 	if (split == 0)
29435d4f98a2SYan Zheng 		btrfs_cpu_key_to_disk(&disk_key, ins_key);
29445d4f98a2SYan Zheng 	else
29455d4f98a2SYan Zheng 		btrfs_item_key(l, &disk_key, mid);
29465d4f98a2SYan Zheng 
29475d4f98a2SYan Zheng 	right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
294844871b1bSChris Mason 					root->root_key.objectid,
294966d7e7f0SArne Jansen 					&disk_key, 0, l->start, 0, 0);
2950f0486c68SYan, Zheng 	if (IS_ERR(right))
295144871b1bSChris Mason 		return PTR_ERR(right);
2952f0486c68SYan, Zheng 
2953f0486c68SYan, Zheng 	root_add_used(root, root->leafsize);
295444871b1bSChris Mason 
295544871b1bSChris Mason 	memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
295644871b1bSChris Mason 	btrfs_set_header_bytenr(right, right->start);
295744871b1bSChris Mason 	btrfs_set_header_generation(right, trans->transid);
29585d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
295944871b1bSChris Mason 	btrfs_set_header_owner(right, root->root_key.objectid);
296044871b1bSChris Mason 	btrfs_set_header_level(right, 0);
296144871b1bSChris Mason 	write_extent_buffer(right, root->fs_info->fsid,
296244871b1bSChris Mason 			    (unsigned long)btrfs_header_fsid(right),
296344871b1bSChris Mason 			    BTRFS_FSID_SIZE);
296444871b1bSChris Mason 
296544871b1bSChris Mason 	write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
296644871b1bSChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(right),
296744871b1bSChris Mason 			    BTRFS_UUID_SIZE);
296844871b1bSChris Mason 
29695d4f98a2SYan Zheng 	if (split == 0) {
297044871b1bSChris Mason 		if (mid <= slot) {
297144871b1bSChris Mason 			btrfs_set_header_nritems(right, 0);
2972143bede5SJeff Mahoney 			insert_ptr(trans, root, path, &disk_key, right->start,
297344871b1bSChris Mason 				   path->slots[1] + 1, 1);
297444871b1bSChris Mason 			btrfs_tree_unlock(path->nodes[0]);
297544871b1bSChris Mason 			free_extent_buffer(path->nodes[0]);
297644871b1bSChris Mason 			path->nodes[0] = right;
297744871b1bSChris Mason 			path->slots[0] = 0;
297844871b1bSChris Mason 			path->slots[1] += 1;
297944871b1bSChris Mason 		} else {
298044871b1bSChris Mason 			btrfs_set_header_nritems(right, 0);
2981143bede5SJeff Mahoney 			insert_ptr(trans, root, path, &disk_key, right->start,
298244871b1bSChris Mason 					  path->slots[1], 1);
298344871b1bSChris Mason 			btrfs_tree_unlock(path->nodes[0]);
298444871b1bSChris Mason 			free_extent_buffer(path->nodes[0]);
298544871b1bSChris Mason 			path->nodes[0] = right;
298644871b1bSChris Mason 			path->slots[0] = 0;
2987143bede5SJeff Mahoney 			if (path->slots[1] == 0)
2988143bede5SJeff Mahoney 				fixup_low_keys(trans, root, path,
2989143bede5SJeff Mahoney 					       &disk_key, 1);
29905d4f98a2SYan Zheng 		}
299144871b1bSChris Mason 		btrfs_mark_buffer_dirty(right);
299244871b1bSChris Mason 		return ret;
299344871b1bSChris Mason 	}
299444871b1bSChris Mason 
2995143bede5SJeff Mahoney 	copy_for_split(trans, root, path, l, right, slot, mid, nritems);
299644871b1bSChris Mason 
29975d4f98a2SYan Zheng 	if (split == 2) {
2998cc0c5538SChris Mason 		BUG_ON(num_doubles != 0);
2999cc0c5538SChris Mason 		num_doubles++;
3000cc0c5538SChris Mason 		goto again;
30013326d1b0SChris Mason 	}
300244871b1bSChris Mason 
3003143bede5SJeff Mahoney 	return 0;
300499d8f83cSChris Mason 
300599d8f83cSChris Mason push_for_double:
300699d8f83cSChris Mason 	push_for_double_split(trans, root, path, data_size);
300799d8f83cSChris Mason 	tried_avoid_double = 1;
300899d8f83cSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
300999d8f83cSChris Mason 		return 0;
301099d8f83cSChris Mason 	goto again;
3011be0e5c09SChris Mason }
3012be0e5c09SChris Mason 
3013ad48fd75SYan, Zheng static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3014ad48fd75SYan, Zheng 					 struct btrfs_root *root,
3015ad48fd75SYan, Zheng 					 struct btrfs_path *path, int ins_len)
3016ad48fd75SYan, Zheng {
3017ad48fd75SYan, Zheng 	struct btrfs_key key;
3018ad48fd75SYan, Zheng 	struct extent_buffer *leaf;
3019ad48fd75SYan, Zheng 	struct btrfs_file_extent_item *fi;
3020ad48fd75SYan, Zheng 	u64 extent_len = 0;
3021ad48fd75SYan, Zheng 	u32 item_size;
3022ad48fd75SYan, Zheng 	int ret;
3023ad48fd75SYan, Zheng 
3024ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3025ad48fd75SYan, Zheng 	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3026ad48fd75SYan, Zheng 
3027ad48fd75SYan, Zheng 	BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3028ad48fd75SYan, Zheng 	       key.type != BTRFS_EXTENT_CSUM_KEY);
3029ad48fd75SYan, Zheng 
3030ad48fd75SYan, Zheng 	if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3031ad48fd75SYan, Zheng 		return 0;
3032ad48fd75SYan, Zheng 
3033ad48fd75SYan, Zheng 	item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3034ad48fd75SYan, Zheng 	if (key.type == BTRFS_EXTENT_DATA_KEY) {
3035ad48fd75SYan, Zheng 		fi = btrfs_item_ptr(leaf, path->slots[0],
3036ad48fd75SYan, Zheng 				    struct btrfs_file_extent_item);
3037ad48fd75SYan, Zheng 		extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3038ad48fd75SYan, Zheng 	}
3039b3b4aa74SDavid Sterba 	btrfs_release_path(path);
3040ad48fd75SYan, Zheng 
3041ad48fd75SYan, Zheng 	path->keep_locks = 1;
3042ad48fd75SYan, Zheng 	path->search_for_split = 1;
3043ad48fd75SYan, Zheng 	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3044ad48fd75SYan, Zheng 	path->search_for_split = 0;
3045ad48fd75SYan, Zheng 	if (ret < 0)
3046ad48fd75SYan, Zheng 		goto err;
3047ad48fd75SYan, Zheng 
3048ad48fd75SYan, Zheng 	ret = -EAGAIN;
3049ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3050ad48fd75SYan, Zheng 	/* if our item isn't there or got smaller, return now */
3051ad48fd75SYan, Zheng 	if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3052ad48fd75SYan, Zheng 		goto err;
3053ad48fd75SYan, Zheng 
3054109f6aefSChris Mason 	/* the leaf has  changed, it now has room.  return now */
3055109f6aefSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3056109f6aefSChris Mason 		goto err;
3057109f6aefSChris Mason 
3058ad48fd75SYan, Zheng 	if (key.type == BTRFS_EXTENT_DATA_KEY) {
3059ad48fd75SYan, Zheng 		fi = btrfs_item_ptr(leaf, path->slots[0],
3060ad48fd75SYan, Zheng 				    struct btrfs_file_extent_item);
3061ad48fd75SYan, Zheng 		if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3062ad48fd75SYan, Zheng 			goto err;
3063ad48fd75SYan, Zheng 	}
3064ad48fd75SYan, Zheng 
3065ad48fd75SYan, Zheng 	btrfs_set_path_blocking(path);
3066ad48fd75SYan, Zheng 	ret = split_leaf(trans, root, &key, path, ins_len, 1);
3067f0486c68SYan, Zheng 	if (ret)
3068f0486c68SYan, Zheng 		goto err;
3069ad48fd75SYan, Zheng 
3070ad48fd75SYan, Zheng 	path->keep_locks = 0;
3071ad48fd75SYan, Zheng 	btrfs_unlock_up_safe(path, 1);
3072ad48fd75SYan, Zheng 	return 0;
3073ad48fd75SYan, Zheng err:
3074ad48fd75SYan, Zheng 	path->keep_locks = 0;
3075ad48fd75SYan, Zheng 	return ret;
3076ad48fd75SYan, Zheng }
3077ad48fd75SYan, Zheng 
3078ad48fd75SYan, Zheng static noinline int split_item(struct btrfs_trans_handle *trans,
3079459931ecSChris Mason 			       struct btrfs_root *root,
3080459931ecSChris Mason 			       struct btrfs_path *path,
3081459931ecSChris Mason 			       struct btrfs_key *new_key,
3082459931ecSChris Mason 			       unsigned long split_offset)
3083459931ecSChris Mason {
3084459931ecSChris Mason 	struct extent_buffer *leaf;
3085459931ecSChris Mason 	struct btrfs_item *item;
3086459931ecSChris Mason 	struct btrfs_item *new_item;
3087459931ecSChris Mason 	int slot;
3088ad48fd75SYan, Zheng 	char *buf;
3089459931ecSChris Mason 	u32 nritems;
3090ad48fd75SYan, Zheng 	u32 item_size;
3091459931ecSChris Mason 	u32 orig_offset;
3092459931ecSChris Mason 	struct btrfs_disk_key disk_key;
3093459931ecSChris Mason 
3094459931ecSChris Mason 	leaf = path->nodes[0];
3095b9473439SChris Mason 	BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3096b9473439SChris Mason 
3097b4ce94deSChris Mason 	btrfs_set_path_blocking(path);
3098b4ce94deSChris Mason 
3099459931ecSChris Mason 	item = btrfs_item_nr(leaf, path->slots[0]);
3100459931ecSChris Mason 	orig_offset = btrfs_item_offset(leaf, item);
3101459931ecSChris Mason 	item_size = btrfs_item_size(leaf, item);
3102459931ecSChris Mason 
3103459931ecSChris Mason 	buf = kmalloc(item_size, GFP_NOFS);
3104ad48fd75SYan, Zheng 	if (!buf)
3105ad48fd75SYan, Zheng 		return -ENOMEM;
3106ad48fd75SYan, Zheng 
3107459931ecSChris Mason 	read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3108459931ecSChris Mason 			    path->slots[0]), item_size);
3109ad48fd75SYan, Zheng 
3110459931ecSChris Mason 	slot = path->slots[0] + 1;
3111459931ecSChris Mason 	nritems = btrfs_header_nritems(leaf);
3112459931ecSChris Mason 	if (slot != nritems) {
3113459931ecSChris Mason 		/* shift the items */
3114459931ecSChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3115459931ecSChris Mason 				btrfs_item_nr_offset(slot),
3116459931ecSChris Mason 				(nritems - slot) * sizeof(struct btrfs_item));
3117459931ecSChris Mason 	}
3118459931ecSChris Mason 
3119459931ecSChris Mason 	btrfs_cpu_key_to_disk(&disk_key, new_key);
3120459931ecSChris Mason 	btrfs_set_item_key(leaf, &disk_key, slot);
3121459931ecSChris Mason 
3122459931ecSChris Mason 	new_item = btrfs_item_nr(leaf, slot);
3123459931ecSChris Mason 
3124459931ecSChris Mason 	btrfs_set_item_offset(leaf, new_item, orig_offset);
3125459931ecSChris Mason 	btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3126459931ecSChris Mason 
3127459931ecSChris Mason 	btrfs_set_item_offset(leaf, item,
3128459931ecSChris Mason 			      orig_offset + item_size - split_offset);
3129459931ecSChris Mason 	btrfs_set_item_size(leaf, item, split_offset);
3130459931ecSChris Mason 
3131459931ecSChris Mason 	btrfs_set_header_nritems(leaf, nritems + 1);
3132459931ecSChris Mason 
3133459931ecSChris Mason 	/* write the data for the start of the original item */
3134459931ecSChris Mason 	write_extent_buffer(leaf, buf,
3135459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, path->slots[0]),
3136459931ecSChris Mason 			    split_offset);
3137459931ecSChris Mason 
3138459931ecSChris Mason 	/* write the data for the new item */
3139459931ecSChris Mason 	write_extent_buffer(leaf, buf + split_offset,
3140459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, slot),
3141459931ecSChris Mason 			    item_size - split_offset);
3142459931ecSChris Mason 	btrfs_mark_buffer_dirty(leaf);
3143459931ecSChris Mason 
3144ad48fd75SYan, Zheng 	BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
3145459931ecSChris Mason 	kfree(buf);
3146ad48fd75SYan, Zheng 	return 0;
3147ad48fd75SYan, Zheng }
3148ad48fd75SYan, Zheng 
3149ad48fd75SYan, Zheng /*
3150ad48fd75SYan, Zheng  * This function splits a single item into two items,
3151ad48fd75SYan, Zheng  * giving 'new_key' to the new item and splitting the
3152ad48fd75SYan, Zheng  * old one at split_offset (from the start of the item).
3153ad48fd75SYan, Zheng  *
3154ad48fd75SYan, Zheng  * The path may be released by this operation.  After
3155ad48fd75SYan, Zheng  * the split, the path is pointing to the old item.  The
3156ad48fd75SYan, Zheng  * new item is going to be in the same node as the old one.
3157ad48fd75SYan, Zheng  *
3158ad48fd75SYan, Zheng  * Note, the item being split must be smaller enough to live alone on
3159ad48fd75SYan, Zheng  * a tree block with room for one extra struct btrfs_item
3160ad48fd75SYan, Zheng  *
3161ad48fd75SYan, Zheng  * This allows us to split the item in place, keeping a lock on the
3162ad48fd75SYan, Zheng  * leaf the entire time.
3163ad48fd75SYan, Zheng  */
3164ad48fd75SYan, Zheng int btrfs_split_item(struct btrfs_trans_handle *trans,
3165ad48fd75SYan, Zheng 		     struct btrfs_root *root,
3166ad48fd75SYan, Zheng 		     struct btrfs_path *path,
3167ad48fd75SYan, Zheng 		     struct btrfs_key *new_key,
3168ad48fd75SYan, Zheng 		     unsigned long split_offset)
3169ad48fd75SYan, Zheng {
3170ad48fd75SYan, Zheng 	int ret;
3171ad48fd75SYan, Zheng 	ret = setup_leaf_for_split(trans, root, path,
3172ad48fd75SYan, Zheng 				   sizeof(struct btrfs_item));
3173ad48fd75SYan, Zheng 	if (ret)
3174459931ecSChris Mason 		return ret;
3175ad48fd75SYan, Zheng 
3176ad48fd75SYan, Zheng 	ret = split_item(trans, root, path, new_key, split_offset);
3177ad48fd75SYan, Zheng 	return ret;
3178ad48fd75SYan, Zheng }
3179ad48fd75SYan, Zheng 
3180ad48fd75SYan, Zheng /*
3181ad48fd75SYan, Zheng  * This function duplicate a item, giving 'new_key' to the new item.
3182ad48fd75SYan, Zheng  * It guarantees both items live in the same tree leaf and the new item
3183ad48fd75SYan, Zheng  * is contiguous with the original item.
3184ad48fd75SYan, Zheng  *
3185ad48fd75SYan, Zheng  * This allows us to split file extent in place, keeping a lock on the
3186ad48fd75SYan, Zheng  * leaf the entire time.
3187ad48fd75SYan, Zheng  */
3188ad48fd75SYan, Zheng int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3189ad48fd75SYan, Zheng 			 struct btrfs_root *root,
3190ad48fd75SYan, Zheng 			 struct btrfs_path *path,
3191ad48fd75SYan, Zheng 			 struct btrfs_key *new_key)
3192ad48fd75SYan, Zheng {
3193ad48fd75SYan, Zheng 	struct extent_buffer *leaf;
3194ad48fd75SYan, Zheng 	int ret;
3195ad48fd75SYan, Zheng 	u32 item_size;
3196ad48fd75SYan, Zheng 
3197ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3198ad48fd75SYan, Zheng 	item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3199ad48fd75SYan, Zheng 	ret = setup_leaf_for_split(trans, root, path,
3200ad48fd75SYan, Zheng 				   item_size + sizeof(struct btrfs_item));
3201ad48fd75SYan, Zheng 	if (ret)
3202ad48fd75SYan, Zheng 		return ret;
3203ad48fd75SYan, Zheng 
3204ad48fd75SYan, Zheng 	path->slots[0]++;
3205143bede5SJeff Mahoney 	setup_items_for_insert(trans, root, path, new_key, &item_size,
3206ad48fd75SYan, Zheng 			       item_size, item_size +
3207ad48fd75SYan, Zheng 			       sizeof(struct btrfs_item), 1);
3208ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3209ad48fd75SYan, Zheng 	memcpy_extent_buffer(leaf,
3210ad48fd75SYan, Zheng 			     btrfs_item_ptr_offset(leaf, path->slots[0]),
3211ad48fd75SYan, Zheng 			     btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3212ad48fd75SYan, Zheng 			     item_size);
3213ad48fd75SYan, Zheng 	return 0;
3214459931ecSChris Mason }
3215459931ecSChris Mason 
3216459931ecSChris Mason /*
3217d352ac68SChris Mason  * make the item pointed to by the path smaller.  new_size indicates
3218d352ac68SChris Mason  * how small to make it, and from_end tells us if we just chop bytes
3219d352ac68SChris Mason  * off the end of the item or if we shift the item to chop bytes off
3220d352ac68SChris Mason  * the front.
3221d352ac68SChris Mason  */
3222143bede5SJeff Mahoney void btrfs_truncate_item(struct btrfs_trans_handle *trans,
3223b18c6685SChris Mason 			 struct btrfs_root *root,
3224b18c6685SChris Mason 			 struct btrfs_path *path,
3225179e29e4SChris Mason 			 u32 new_size, int from_end)
3226b18c6685SChris Mason {
3227b18c6685SChris Mason 	int slot;
32285f39d397SChris Mason 	struct extent_buffer *leaf;
32295f39d397SChris Mason 	struct btrfs_item *item;
3230b18c6685SChris Mason 	u32 nritems;
3231b18c6685SChris Mason 	unsigned int data_end;
3232b18c6685SChris Mason 	unsigned int old_data_start;
3233b18c6685SChris Mason 	unsigned int old_size;
3234b18c6685SChris Mason 	unsigned int size_diff;
3235b18c6685SChris Mason 	int i;
3236b18c6685SChris Mason 
32375f39d397SChris Mason 	leaf = path->nodes[0];
3238179e29e4SChris Mason 	slot = path->slots[0];
3239179e29e4SChris Mason 
3240179e29e4SChris Mason 	old_size = btrfs_item_size_nr(leaf, slot);
3241179e29e4SChris Mason 	if (old_size == new_size)
3242143bede5SJeff Mahoney 		return;
3243b18c6685SChris Mason 
32445f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3245b18c6685SChris Mason 	data_end = leaf_data_end(root, leaf);
3246b18c6685SChris Mason 
32475f39d397SChris Mason 	old_data_start = btrfs_item_offset_nr(leaf, slot);
3248179e29e4SChris Mason 
3249b18c6685SChris Mason 	size_diff = old_size - new_size;
3250b18c6685SChris Mason 
3251b18c6685SChris Mason 	BUG_ON(slot < 0);
3252b18c6685SChris Mason 	BUG_ON(slot >= nritems);
3253b18c6685SChris Mason 
3254b18c6685SChris Mason 	/*
3255b18c6685SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3256b18c6685SChris Mason 	 */
3257b18c6685SChris Mason 	/* first correct the data pointers */
3258b18c6685SChris Mason 	for (i = slot; i < nritems; i++) {
32595f39d397SChris Mason 		u32 ioff;
32605f39d397SChris Mason 		item = btrfs_item_nr(leaf, i);
3261db94535dSChris Mason 
32625f39d397SChris Mason 		ioff = btrfs_item_offset(leaf, item);
32635f39d397SChris Mason 		btrfs_set_item_offset(leaf, item, ioff + size_diff);
3264b18c6685SChris Mason 	}
3265db94535dSChris Mason 
3266b18c6685SChris Mason 	/* shift the data */
3267179e29e4SChris Mason 	if (from_end) {
32685f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3269b18c6685SChris Mason 			      data_end + size_diff, btrfs_leaf_data(leaf) +
3270b18c6685SChris Mason 			      data_end, old_data_start + new_size - data_end);
3271179e29e4SChris Mason 	} else {
3272179e29e4SChris Mason 		struct btrfs_disk_key disk_key;
3273179e29e4SChris Mason 		u64 offset;
3274179e29e4SChris Mason 
3275179e29e4SChris Mason 		btrfs_item_key(leaf, &disk_key, slot);
3276179e29e4SChris Mason 
3277179e29e4SChris Mason 		if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3278179e29e4SChris Mason 			unsigned long ptr;
3279179e29e4SChris Mason 			struct btrfs_file_extent_item *fi;
3280179e29e4SChris Mason 
3281179e29e4SChris Mason 			fi = btrfs_item_ptr(leaf, slot,
3282179e29e4SChris Mason 					    struct btrfs_file_extent_item);
3283179e29e4SChris Mason 			fi = (struct btrfs_file_extent_item *)(
3284179e29e4SChris Mason 			     (unsigned long)fi - size_diff);
3285179e29e4SChris Mason 
3286179e29e4SChris Mason 			if (btrfs_file_extent_type(leaf, fi) ==
3287179e29e4SChris Mason 			    BTRFS_FILE_EXTENT_INLINE) {
3288179e29e4SChris Mason 				ptr = btrfs_item_ptr_offset(leaf, slot);
3289179e29e4SChris Mason 				memmove_extent_buffer(leaf, ptr,
3290179e29e4SChris Mason 				      (unsigned long)fi,
3291179e29e4SChris Mason 				      offsetof(struct btrfs_file_extent_item,
3292179e29e4SChris Mason 						 disk_bytenr));
3293179e29e4SChris Mason 			}
3294179e29e4SChris Mason 		}
3295179e29e4SChris Mason 
3296179e29e4SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3297179e29e4SChris Mason 			      data_end + size_diff, btrfs_leaf_data(leaf) +
3298179e29e4SChris Mason 			      data_end, old_data_start - data_end);
3299179e29e4SChris Mason 
3300179e29e4SChris Mason 		offset = btrfs_disk_key_offset(&disk_key);
3301179e29e4SChris Mason 		btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3302179e29e4SChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot);
3303179e29e4SChris Mason 		if (slot == 0)
3304179e29e4SChris Mason 			fixup_low_keys(trans, root, path, &disk_key, 1);
3305179e29e4SChris Mason 	}
33065f39d397SChris Mason 
33075f39d397SChris Mason 	item = btrfs_item_nr(leaf, slot);
33085f39d397SChris Mason 	btrfs_set_item_size(leaf, item, new_size);
33095f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
3310b18c6685SChris Mason 
33115f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
33125f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
3313b18c6685SChris Mason 		BUG();
33145f39d397SChris Mason 	}
3315b18c6685SChris Mason }
3316b18c6685SChris Mason 
3317d352ac68SChris Mason /*
3318d352ac68SChris Mason  * make the item pointed to by the path bigger, data_size is the new size.
3319d352ac68SChris Mason  */
3320143bede5SJeff Mahoney void btrfs_extend_item(struct btrfs_trans_handle *trans,
33215f39d397SChris Mason 		       struct btrfs_root *root, struct btrfs_path *path,
33225f39d397SChris Mason 		       u32 data_size)
33236567e837SChris Mason {
33246567e837SChris Mason 	int slot;
33255f39d397SChris Mason 	struct extent_buffer *leaf;
33265f39d397SChris Mason 	struct btrfs_item *item;
33276567e837SChris Mason 	u32 nritems;
33286567e837SChris Mason 	unsigned int data_end;
33296567e837SChris Mason 	unsigned int old_data;
33306567e837SChris Mason 	unsigned int old_size;
33316567e837SChris Mason 	int i;
33326567e837SChris Mason 
33335f39d397SChris Mason 	leaf = path->nodes[0];
33346567e837SChris Mason 
33355f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
33366567e837SChris Mason 	data_end = leaf_data_end(root, leaf);
33376567e837SChris Mason 
33385f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < data_size) {
33395f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
33406567e837SChris Mason 		BUG();
33415f39d397SChris Mason 	}
33426567e837SChris Mason 	slot = path->slots[0];
33435f39d397SChris Mason 	old_data = btrfs_item_end_nr(leaf, slot);
33446567e837SChris Mason 
33456567e837SChris Mason 	BUG_ON(slot < 0);
33463326d1b0SChris Mason 	if (slot >= nritems) {
33473326d1b0SChris Mason 		btrfs_print_leaf(root, leaf);
3348d397712bSChris Mason 		printk(KERN_CRIT "slot %d too large, nritems %d\n",
3349d397712bSChris Mason 		       slot, nritems);
33503326d1b0SChris Mason 		BUG_ON(1);
33513326d1b0SChris Mason 	}
33526567e837SChris Mason 
33536567e837SChris Mason 	/*
33546567e837SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
33556567e837SChris Mason 	 */
33566567e837SChris Mason 	/* first correct the data pointers */
33576567e837SChris Mason 	for (i = slot; i < nritems; i++) {
33585f39d397SChris Mason 		u32 ioff;
33595f39d397SChris Mason 		item = btrfs_item_nr(leaf, i);
3360db94535dSChris Mason 
33615f39d397SChris Mason 		ioff = btrfs_item_offset(leaf, item);
33625f39d397SChris Mason 		btrfs_set_item_offset(leaf, item, ioff - data_size);
33636567e837SChris Mason 	}
33645f39d397SChris Mason 
33656567e837SChris Mason 	/* shift the data */
33665f39d397SChris Mason 	memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
33676567e837SChris Mason 		      data_end - data_size, btrfs_leaf_data(leaf) +
33686567e837SChris Mason 		      data_end, old_data - data_end);
33695f39d397SChris Mason 
33706567e837SChris Mason 	data_end = old_data;
33715f39d397SChris Mason 	old_size = btrfs_item_size_nr(leaf, slot);
33725f39d397SChris Mason 	item = btrfs_item_nr(leaf, slot);
33735f39d397SChris Mason 	btrfs_set_item_size(leaf, item, old_size + data_size);
33745f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
33756567e837SChris Mason 
33765f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
33775f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
33786567e837SChris Mason 		BUG();
33795f39d397SChris Mason 	}
33806567e837SChris Mason }
33816567e837SChris Mason 
338274123bd7SChris Mason /*
3383d352ac68SChris Mason  * Given a key and some data, insert items into the tree.
338474123bd7SChris Mason  * This does all the path init required, making room in the tree if needed.
3385f3465ca4SJosef Bacik  * Returns the number of keys that were inserted.
3386f3465ca4SJosef Bacik  */
3387f3465ca4SJosef Bacik int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3388f3465ca4SJosef Bacik 			    struct btrfs_root *root,
3389f3465ca4SJosef Bacik 			    struct btrfs_path *path,
3390f3465ca4SJosef Bacik 			    struct btrfs_key *cpu_key, u32 *data_size,
3391f3465ca4SJosef Bacik 			    int nr)
3392f3465ca4SJosef Bacik {
3393f3465ca4SJosef Bacik 	struct extent_buffer *leaf;
3394f3465ca4SJosef Bacik 	struct btrfs_item *item;
3395f3465ca4SJosef Bacik 	int ret = 0;
3396f3465ca4SJosef Bacik 	int slot;
3397f3465ca4SJosef Bacik 	int i;
3398f3465ca4SJosef Bacik 	u32 nritems;
3399f3465ca4SJosef Bacik 	u32 total_data = 0;
3400f3465ca4SJosef Bacik 	u32 total_size = 0;
3401f3465ca4SJosef Bacik 	unsigned int data_end;
3402f3465ca4SJosef Bacik 	struct btrfs_disk_key disk_key;
3403f3465ca4SJosef Bacik 	struct btrfs_key found_key;
3404f3465ca4SJosef Bacik 
340587b29b20SYan Zheng 	for (i = 0; i < nr; i++) {
340687b29b20SYan Zheng 		if (total_size + data_size[i] + sizeof(struct btrfs_item) >
340787b29b20SYan Zheng 		    BTRFS_LEAF_DATA_SIZE(root)) {
340887b29b20SYan Zheng 			break;
340987b29b20SYan Zheng 			nr = i;
341087b29b20SYan Zheng 		}
3411f3465ca4SJosef Bacik 		total_data += data_size[i];
341287b29b20SYan Zheng 		total_size += data_size[i] + sizeof(struct btrfs_item);
341387b29b20SYan Zheng 	}
341487b29b20SYan Zheng 	BUG_ON(nr == 0);
3415f3465ca4SJosef Bacik 
3416f3465ca4SJosef Bacik 	ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3417f3465ca4SJosef Bacik 	if (ret == 0)
3418f3465ca4SJosef Bacik 		return -EEXIST;
3419f3465ca4SJosef Bacik 	if (ret < 0)
3420f3465ca4SJosef Bacik 		goto out;
3421f3465ca4SJosef Bacik 
3422f3465ca4SJosef Bacik 	leaf = path->nodes[0];
3423f3465ca4SJosef Bacik 
3424f3465ca4SJosef Bacik 	nritems = btrfs_header_nritems(leaf);
3425f3465ca4SJosef Bacik 	data_end = leaf_data_end(root, leaf);
3426f3465ca4SJosef Bacik 
3427f3465ca4SJosef Bacik 	if (btrfs_leaf_free_space(root, leaf) < total_size) {
3428f3465ca4SJosef Bacik 		for (i = nr; i >= 0; i--) {
3429f3465ca4SJosef Bacik 			total_data -= data_size[i];
3430f3465ca4SJosef Bacik 			total_size -= data_size[i] + sizeof(struct btrfs_item);
3431f3465ca4SJosef Bacik 			if (total_size < btrfs_leaf_free_space(root, leaf))
3432f3465ca4SJosef Bacik 				break;
3433f3465ca4SJosef Bacik 		}
3434f3465ca4SJosef Bacik 		nr = i;
3435f3465ca4SJosef Bacik 	}
3436f3465ca4SJosef Bacik 
3437f3465ca4SJosef Bacik 	slot = path->slots[0];
3438f3465ca4SJosef Bacik 	BUG_ON(slot < 0);
3439f3465ca4SJosef Bacik 
3440f3465ca4SJosef Bacik 	if (slot != nritems) {
3441f3465ca4SJosef Bacik 		unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3442f3465ca4SJosef Bacik 
3443f3465ca4SJosef Bacik 		item = btrfs_item_nr(leaf, slot);
3444f3465ca4SJosef Bacik 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
3445f3465ca4SJosef Bacik 
3446f3465ca4SJosef Bacik 		/* figure out how many keys we can insert in here */
3447f3465ca4SJosef Bacik 		total_data = data_size[0];
3448f3465ca4SJosef Bacik 		for (i = 1; i < nr; i++) {
34495d4f98a2SYan Zheng 			if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3450f3465ca4SJosef Bacik 				break;
3451f3465ca4SJosef Bacik 			total_data += data_size[i];
3452f3465ca4SJosef Bacik 		}
3453f3465ca4SJosef Bacik 		nr = i;
3454f3465ca4SJosef Bacik 
3455f3465ca4SJosef Bacik 		if (old_data < data_end) {
3456f3465ca4SJosef Bacik 			btrfs_print_leaf(root, leaf);
3457d397712bSChris Mason 			printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3458f3465ca4SJosef Bacik 			       slot, old_data, data_end);
3459f3465ca4SJosef Bacik 			BUG_ON(1);
3460f3465ca4SJosef Bacik 		}
3461f3465ca4SJosef Bacik 		/*
3462f3465ca4SJosef Bacik 		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3463f3465ca4SJosef Bacik 		 */
3464f3465ca4SJosef Bacik 		/* first correct the data pointers */
3465f3465ca4SJosef Bacik 		for (i = slot; i < nritems; i++) {
3466f3465ca4SJosef Bacik 			u32 ioff;
3467f3465ca4SJosef Bacik 
3468f3465ca4SJosef Bacik 			item = btrfs_item_nr(leaf, i);
3469f3465ca4SJosef Bacik 			ioff = btrfs_item_offset(leaf, item);
3470f3465ca4SJosef Bacik 			btrfs_set_item_offset(leaf, item, ioff - total_data);
3471f3465ca4SJosef Bacik 		}
3472f3465ca4SJosef Bacik 		/* shift the items */
3473f3465ca4SJosef Bacik 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3474f3465ca4SJosef Bacik 			      btrfs_item_nr_offset(slot),
3475f3465ca4SJosef Bacik 			      (nritems - slot) * sizeof(struct btrfs_item));
3476f3465ca4SJosef Bacik 
3477f3465ca4SJosef Bacik 		/* shift the data */
3478f3465ca4SJosef Bacik 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3479f3465ca4SJosef Bacik 			      data_end - total_data, btrfs_leaf_data(leaf) +
3480f3465ca4SJosef Bacik 			      data_end, old_data - data_end);
3481f3465ca4SJosef Bacik 		data_end = old_data;
3482f3465ca4SJosef Bacik 	} else {
3483f3465ca4SJosef Bacik 		/*
3484f3465ca4SJosef Bacik 		 * this sucks but it has to be done, if we are inserting at
3485f3465ca4SJosef Bacik 		 * the end of the leaf only insert 1 of the items, since we
3486f3465ca4SJosef Bacik 		 * have no way of knowing whats on the next leaf and we'd have
3487f3465ca4SJosef Bacik 		 * to drop our current locks to figure it out
3488f3465ca4SJosef Bacik 		 */
3489f3465ca4SJosef Bacik 		nr = 1;
3490f3465ca4SJosef Bacik 	}
3491f3465ca4SJosef Bacik 
3492f3465ca4SJosef Bacik 	/* setup the item for the new data */
3493f3465ca4SJosef Bacik 	for (i = 0; i < nr; i++) {
3494f3465ca4SJosef Bacik 		btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3495f3465ca4SJosef Bacik 		btrfs_set_item_key(leaf, &disk_key, slot + i);
3496f3465ca4SJosef Bacik 		item = btrfs_item_nr(leaf, slot + i);
3497f3465ca4SJosef Bacik 		btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3498f3465ca4SJosef Bacik 		data_end -= data_size[i];
3499f3465ca4SJosef Bacik 		btrfs_set_item_size(leaf, item, data_size[i]);
3500f3465ca4SJosef Bacik 	}
3501f3465ca4SJosef Bacik 	btrfs_set_header_nritems(leaf, nritems + nr);
3502f3465ca4SJosef Bacik 	btrfs_mark_buffer_dirty(leaf);
3503f3465ca4SJosef Bacik 
3504f3465ca4SJosef Bacik 	ret = 0;
3505f3465ca4SJosef Bacik 	if (slot == 0) {
3506f3465ca4SJosef Bacik 		btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3507143bede5SJeff Mahoney 		fixup_low_keys(trans, root, path, &disk_key, 1);
3508f3465ca4SJosef Bacik 	}
3509f3465ca4SJosef Bacik 
3510f3465ca4SJosef Bacik 	if (btrfs_leaf_free_space(root, leaf) < 0) {
3511f3465ca4SJosef Bacik 		btrfs_print_leaf(root, leaf);
3512f3465ca4SJosef Bacik 		BUG();
3513f3465ca4SJosef Bacik 	}
3514f3465ca4SJosef Bacik out:
3515f3465ca4SJosef Bacik 	if (!ret)
3516f3465ca4SJosef Bacik 		ret = nr;
3517f3465ca4SJosef Bacik 	return ret;
3518f3465ca4SJosef Bacik }
3519f3465ca4SJosef Bacik 
3520f3465ca4SJosef Bacik /*
352144871b1bSChris Mason  * this is a helper for btrfs_insert_empty_items, the main goal here is
352244871b1bSChris Mason  * to save stack depth by doing the bulk of the work in a function
352344871b1bSChris Mason  * that doesn't call btrfs_search_slot
352474123bd7SChris Mason  */
3525143bede5SJeff Mahoney void setup_items_for_insert(struct btrfs_trans_handle *trans,
352644871b1bSChris Mason 			    struct btrfs_root *root, struct btrfs_path *path,
35279c58309dSChris Mason 			    struct btrfs_key *cpu_key, u32 *data_size,
352844871b1bSChris Mason 			    u32 total_data, u32 total_size, int nr)
3529be0e5c09SChris Mason {
35305f39d397SChris Mason 	struct btrfs_item *item;
35319c58309dSChris Mason 	int i;
35327518a238SChris Mason 	u32 nritems;
3533be0e5c09SChris Mason 	unsigned int data_end;
3534e2fa7227SChris Mason 	struct btrfs_disk_key disk_key;
353544871b1bSChris Mason 	struct extent_buffer *leaf;
353644871b1bSChris Mason 	int slot;
3537e2fa7227SChris Mason 
35385f39d397SChris Mason 	leaf = path->nodes[0];
353944871b1bSChris Mason 	slot = path->slots[0];
354074123bd7SChris Mason 
35415f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3542123abc88SChris Mason 	data_end = leaf_data_end(root, leaf);
3543eb60ceacSChris Mason 
3544f25956ccSChris Mason 	if (btrfs_leaf_free_space(root, leaf) < total_size) {
35453326d1b0SChris Mason 		btrfs_print_leaf(root, leaf);
3546d397712bSChris Mason 		printk(KERN_CRIT "not enough freespace need %u have %d\n",
35479c58309dSChris Mason 		       total_size, btrfs_leaf_free_space(root, leaf));
3548be0e5c09SChris Mason 		BUG();
3549d4dbff95SChris Mason 	}
35505f39d397SChris Mason 
3551be0e5c09SChris Mason 	if (slot != nritems) {
35525f39d397SChris Mason 		unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3553be0e5c09SChris Mason 
35545f39d397SChris Mason 		if (old_data < data_end) {
35555f39d397SChris Mason 			btrfs_print_leaf(root, leaf);
3556d397712bSChris Mason 			printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
35575f39d397SChris Mason 			       slot, old_data, data_end);
35585f39d397SChris Mason 			BUG_ON(1);
35595f39d397SChris Mason 		}
3560be0e5c09SChris Mason 		/*
3561be0e5c09SChris Mason 		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3562be0e5c09SChris Mason 		 */
3563be0e5c09SChris Mason 		/* first correct the data pointers */
35640783fcfcSChris Mason 		for (i = slot; i < nritems; i++) {
35655f39d397SChris Mason 			u32 ioff;
3566db94535dSChris Mason 
35675f39d397SChris Mason 			item = btrfs_item_nr(leaf, i);
35685f39d397SChris Mason 			ioff = btrfs_item_offset(leaf, item);
35699c58309dSChris Mason 			btrfs_set_item_offset(leaf, item, ioff - total_data);
35700783fcfcSChris Mason 		}
3571be0e5c09SChris Mason 		/* shift the items */
35729c58309dSChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
35735f39d397SChris Mason 			      btrfs_item_nr_offset(slot),
35740783fcfcSChris Mason 			      (nritems - slot) * sizeof(struct btrfs_item));
3575be0e5c09SChris Mason 
3576be0e5c09SChris Mason 		/* shift the data */
35775f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
35789c58309dSChris Mason 			      data_end - total_data, btrfs_leaf_data(leaf) +
3579be0e5c09SChris Mason 			      data_end, old_data - data_end);
3580be0e5c09SChris Mason 		data_end = old_data;
3581be0e5c09SChris Mason 	}
35825f39d397SChris Mason 
358362e2749eSChris Mason 	/* setup the item for the new data */
35849c58309dSChris Mason 	for (i = 0; i < nr; i++) {
35859c58309dSChris Mason 		btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
35869c58309dSChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot + i);
35879c58309dSChris Mason 		item = btrfs_item_nr(leaf, slot + i);
35889c58309dSChris Mason 		btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
35899c58309dSChris Mason 		data_end -= data_size[i];
35909c58309dSChris Mason 		btrfs_set_item_size(leaf, item, data_size[i]);
35919c58309dSChris Mason 	}
359244871b1bSChris Mason 
35939c58309dSChris Mason 	btrfs_set_header_nritems(leaf, nritems + nr);
3594aa5d6bedSChris Mason 
35955a01a2e3SChris Mason 	if (slot == 0) {
35965a01a2e3SChris Mason 		btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3597143bede5SJeff Mahoney 		fixup_low_keys(trans, root, path, &disk_key, 1);
35985a01a2e3SChris Mason 	}
3599b9473439SChris Mason 	btrfs_unlock_up_safe(path, 1);
3600b9473439SChris Mason 	btrfs_mark_buffer_dirty(leaf);
3601aa5d6bedSChris Mason 
36025f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
36035f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
3604be0e5c09SChris Mason 		BUG();
36055f39d397SChris Mason 	}
360644871b1bSChris Mason }
360744871b1bSChris Mason 
360844871b1bSChris Mason /*
360944871b1bSChris Mason  * Given a key and some data, insert items into the tree.
361044871b1bSChris Mason  * This does all the path init required, making room in the tree if needed.
361144871b1bSChris Mason  */
361244871b1bSChris Mason int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
361344871b1bSChris Mason 			    struct btrfs_root *root,
361444871b1bSChris Mason 			    struct btrfs_path *path,
361544871b1bSChris Mason 			    struct btrfs_key *cpu_key, u32 *data_size,
361644871b1bSChris Mason 			    int nr)
361744871b1bSChris Mason {
361844871b1bSChris Mason 	int ret = 0;
361944871b1bSChris Mason 	int slot;
362044871b1bSChris Mason 	int i;
362144871b1bSChris Mason 	u32 total_size = 0;
362244871b1bSChris Mason 	u32 total_data = 0;
362344871b1bSChris Mason 
362444871b1bSChris Mason 	for (i = 0; i < nr; i++)
362544871b1bSChris Mason 		total_data += data_size[i];
362644871b1bSChris Mason 
362744871b1bSChris Mason 	total_size = total_data + (nr * sizeof(struct btrfs_item));
362844871b1bSChris Mason 	ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
362944871b1bSChris Mason 	if (ret == 0)
363044871b1bSChris Mason 		return -EEXIST;
363144871b1bSChris Mason 	if (ret < 0)
3632143bede5SJeff Mahoney 		return ret;
363344871b1bSChris Mason 
363444871b1bSChris Mason 	slot = path->slots[0];
363544871b1bSChris Mason 	BUG_ON(slot < 0);
363644871b1bSChris Mason 
3637143bede5SJeff Mahoney 	setup_items_for_insert(trans, root, path, cpu_key, data_size,
363844871b1bSChris Mason 			       total_data, total_size, nr);
3639143bede5SJeff Mahoney 	return 0;
364062e2749eSChris Mason }
364162e2749eSChris Mason 
364262e2749eSChris Mason /*
364362e2749eSChris Mason  * Given a key and some data, insert an item into the tree.
364462e2749eSChris Mason  * This does all the path init required, making room in the tree if needed.
364562e2749eSChris Mason  */
3646e089f05cSChris Mason int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3647e089f05cSChris Mason 		      *root, struct btrfs_key *cpu_key, void *data, u32
3648e089f05cSChris Mason 		      data_size)
364962e2749eSChris Mason {
365062e2749eSChris Mason 	int ret = 0;
36512c90e5d6SChris Mason 	struct btrfs_path *path;
36525f39d397SChris Mason 	struct extent_buffer *leaf;
36535f39d397SChris Mason 	unsigned long ptr;
365462e2749eSChris Mason 
36552c90e5d6SChris Mason 	path = btrfs_alloc_path();
3656db5b493aSTsutomu Itoh 	if (!path)
3657db5b493aSTsutomu Itoh 		return -ENOMEM;
36582c90e5d6SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
365962e2749eSChris Mason 	if (!ret) {
36605f39d397SChris Mason 		leaf = path->nodes[0];
36615f39d397SChris Mason 		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
36625f39d397SChris Mason 		write_extent_buffer(leaf, data, ptr, data_size);
36635f39d397SChris Mason 		btrfs_mark_buffer_dirty(leaf);
366462e2749eSChris Mason 	}
36652c90e5d6SChris Mason 	btrfs_free_path(path);
3666aa5d6bedSChris Mason 	return ret;
3667be0e5c09SChris Mason }
3668be0e5c09SChris Mason 
366974123bd7SChris Mason /*
36705de08d7dSChris Mason  * delete the pointer from a given node.
367174123bd7SChris Mason  *
3672d352ac68SChris Mason  * the tree should have been previously balanced so the deletion does not
3673d352ac68SChris Mason  * empty a node.
367474123bd7SChris Mason  */
3675143bede5SJeff Mahoney static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3676e089f05cSChris Mason 		    struct btrfs_path *path, int level, int slot)
3677be0e5c09SChris Mason {
36785f39d397SChris Mason 	struct extent_buffer *parent = path->nodes[level];
36797518a238SChris Mason 	u32 nritems;
3680be0e5c09SChris Mason 
36815f39d397SChris Mason 	nritems = btrfs_header_nritems(parent);
3682be0e5c09SChris Mason 	if (slot != nritems - 1) {
36835f39d397SChris Mason 		memmove_extent_buffer(parent,
36845f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot),
36855f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot + 1),
3686d6025579SChris Mason 			      sizeof(struct btrfs_key_ptr) *
3687d6025579SChris Mason 			      (nritems - slot - 1));
3688be0e5c09SChris Mason 	}
36897518a238SChris Mason 	nritems--;
36905f39d397SChris Mason 	btrfs_set_header_nritems(parent, nritems);
36917518a238SChris Mason 	if (nritems == 0 && parent == root->node) {
36925f39d397SChris Mason 		BUG_ON(btrfs_header_level(root->node) != 1);
3693eb60ceacSChris Mason 		/* just turn the root into a leaf and break */
36945f39d397SChris Mason 		btrfs_set_header_level(root->node, 0);
3695bb803951SChris Mason 	} else if (slot == 0) {
36965f39d397SChris Mason 		struct btrfs_disk_key disk_key;
36975f39d397SChris Mason 
36985f39d397SChris Mason 		btrfs_node_key(parent, &disk_key, 0);
3699143bede5SJeff Mahoney 		fixup_low_keys(trans, root, path, &disk_key, level + 1);
3700be0e5c09SChris Mason 	}
3701d6025579SChris Mason 	btrfs_mark_buffer_dirty(parent);
3702be0e5c09SChris Mason }
3703be0e5c09SChris Mason 
370474123bd7SChris Mason /*
3705323ac95bSChris Mason  * a helper function to delete the leaf pointed to by path->slots[1] and
37065d4f98a2SYan Zheng  * path->nodes[1].
3707323ac95bSChris Mason  *
3708323ac95bSChris Mason  * This deletes the pointer in path->nodes[1] and frees the leaf
3709323ac95bSChris Mason  * block extent.  zero is returned if it all worked out, < 0 otherwise.
3710323ac95bSChris Mason  *
3711323ac95bSChris Mason  * The path must have already been setup for deleting the leaf, including
3712323ac95bSChris Mason  * all the proper balancing.  path->nodes[1] must be locked.
3713323ac95bSChris Mason  */
3714143bede5SJeff Mahoney static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
3715323ac95bSChris Mason 				    struct btrfs_root *root,
37165d4f98a2SYan Zheng 				    struct btrfs_path *path,
37175d4f98a2SYan Zheng 				    struct extent_buffer *leaf)
3718323ac95bSChris Mason {
37195d4f98a2SYan Zheng 	WARN_ON(btrfs_header_generation(leaf) != trans->transid);
3720143bede5SJeff Mahoney 	del_ptr(trans, root, path, 1, path->slots[1]);
3721323ac95bSChris Mason 
37224d081c41SChris Mason 	/*
37234d081c41SChris Mason 	 * btrfs_free_extent is expensive, we want to make sure we
37244d081c41SChris Mason 	 * aren't holding any locks when we call it
37254d081c41SChris Mason 	 */
37264d081c41SChris Mason 	btrfs_unlock_up_safe(path, 0);
37274d081c41SChris Mason 
3728f0486c68SYan, Zheng 	root_sub_used(root, leaf->len);
3729f0486c68SYan, Zheng 
373066d7e7f0SArne Jansen 	btrfs_free_tree_block(trans, root, leaf, 0, 1, 0);
3731323ac95bSChris Mason }
3732323ac95bSChris Mason /*
373374123bd7SChris Mason  * delete the item at the leaf level in path.  If that empties
373474123bd7SChris Mason  * the leaf, remove it from the tree
373574123bd7SChris Mason  */
373685e21bacSChris Mason int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
373785e21bacSChris Mason 		    struct btrfs_path *path, int slot, int nr)
3738be0e5c09SChris Mason {
37395f39d397SChris Mason 	struct extent_buffer *leaf;
37405f39d397SChris Mason 	struct btrfs_item *item;
374185e21bacSChris Mason 	int last_off;
374285e21bacSChris Mason 	int dsize = 0;
3743aa5d6bedSChris Mason 	int ret = 0;
3744aa5d6bedSChris Mason 	int wret;
374585e21bacSChris Mason 	int i;
37467518a238SChris Mason 	u32 nritems;
3747be0e5c09SChris Mason 
37485f39d397SChris Mason 	leaf = path->nodes[0];
374985e21bacSChris Mason 	last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
375085e21bacSChris Mason 
375185e21bacSChris Mason 	for (i = 0; i < nr; i++)
375285e21bacSChris Mason 		dsize += btrfs_item_size_nr(leaf, slot + i);
375385e21bacSChris Mason 
37545f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3755be0e5c09SChris Mason 
375685e21bacSChris Mason 	if (slot + nr != nritems) {
3757123abc88SChris Mason 		int data_end = leaf_data_end(root, leaf);
37585f39d397SChris Mason 
37595f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3760d6025579SChris Mason 			      data_end + dsize,
3761123abc88SChris Mason 			      btrfs_leaf_data(leaf) + data_end,
376285e21bacSChris Mason 			      last_off - data_end);
37635f39d397SChris Mason 
376485e21bacSChris Mason 		for (i = slot + nr; i < nritems; i++) {
37655f39d397SChris Mason 			u32 ioff;
3766db94535dSChris Mason 
37675f39d397SChris Mason 			item = btrfs_item_nr(leaf, i);
37685f39d397SChris Mason 			ioff = btrfs_item_offset(leaf, item);
37695f39d397SChris Mason 			btrfs_set_item_offset(leaf, item, ioff + dsize);
37700783fcfcSChris Mason 		}
3771db94535dSChris Mason 
37725f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
377385e21bacSChris Mason 			      btrfs_item_nr_offset(slot + nr),
37740783fcfcSChris Mason 			      sizeof(struct btrfs_item) *
377585e21bacSChris Mason 			      (nritems - slot - nr));
3776be0e5c09SChris Mason 	}
377785e21bacSChris Mason 	btrfs_set_header_nritems(leaf, nritems - nr);
377885e21bacSChris Mason 	nritems -= nr;
37795f39d397SChris Mason 
378074123bd7SChris Mason 	/* delete the leaf if we've emptied it */
37817518a238SChris Mason 	if (nritems == 0) {
37825f39d397SChris Mason 		if (leaf == root->node) {
37835f39d397SChris Mason 			btrfs_set_header_level(leaf, 0);
37849a8dd150SChris Mason 		} else {
3785f0486c68SYan, Zheng 			btrfs_set_path_blocking(path);
3786f0486c68SYan, Zheng 			clean_tree_block(trans, root, leaf);
3787143bede5SJeff Mahoney 			btrfs_del_leaf(trans, root, path, leaf);
37889a8dd150SChris Mason 		}
3789be0e5c09SChris Mason 	} else {
37907518a238SChris Mason 		int used = leaf_space_used(leaf, 0, nritems);
3791aa5d6bedSChris Mason 		if (slot == 0) {
37925f39d397SChris Mason 			struct btrfs_disk_key disk_key;
37935f39d397SChris Mason 
37945f39d397SChris Mason 			btrfs_item_key(leaf, &disk_key, 0);
3795143bede5SJeff Mahoney 			fixup_low_keys(trans, root, path, &disk_key, 1);
3796aa5d6bedSChris Mason 		}
3797aa5d6bedSChris Mason 
379874123bd7SChris Mason 		/* delete the leaf if it is mostly empty */
3799d717aa1dSYan Zheng 		if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
3800be0e5c09SChris Mason 			/* push_leaf_left fixes the path.
3801be0e5c09SChris Mason 			 * make sure the path still points to our leaf
3802be0e5c09SChris Mason 			 * for possible call to del_ptr below
3803be0e5c09SChris Mason 			 */
38044920c9acSChris Mason 			slot = path->slots[1];
38055f39d397SChris Mason 			extent_buffer_get(leaf);
38065f39d397SChris Mason 
3807b9473439SChris Mason 			btrfs_set_path_blocking(path);
380899d8f83cSChris Mason 			wret = push_leaf_left(trans, root, path, 1, 1,
380999d8f83cSChris Mason 					      1, (u32)-1);
381054aa1f4dSChris Mason 			if (wret < 0 && wret != -ENOSPC)
3811aa5d6bedSChris Mason 				ret = wret;
38125f39d397SChris Mason 
38135f39d397SChris Mason 			if (path->nodes[0] == leaf &&
38145f39d397SChris Mason 			    btrfs_header_nritems(leaf)) {
381599d8f83cSChris Mason 				wret = push_leaf_right(trans, root, path, 1,
381699d8f83cSChris Mason 						       1, 1, 0);
381754aa1f4dSChris Mason 				if (wret < 0 && wret != -ENOSPC)
3818aa5d6bedSChris Mason 					ret = wret;
3819aa5d6bedSChris Mason 			}
38205f39d397SChris Mason 
38215f39d397SChris Mason 			if (btrfs_header_nritems(leaf) == 0) {
3822323ac95bSChris Mason 				path->slots[1] = slot;
3823143bede5SJeff Mahoney 				btrfs_del_leaf(trans, root, path, leaf);
38245f39d397SChris Mason 				free_extent_buffer(leaf);
3825143bede5SJeff Mahoney 				ret = 0;
38265de08d7dSChris Mason 			} else {
3827925baeddSChris Mason 				/* if we're still in the path, make sure
3828925baeddSChris Mason 				 * we're dirty.  Otherwise, one of the
3829925baeddSChris Mason 				 * push_leaf functions must have already
3830925baeddSChris Mason 				 * dirtied this buffer
3831925baeddSChris Mason 				 */
3832925baeddSChris Mason 				if (path->nodes[0] == leaf)
38335f39d397SChris Mason 					btrfs_mark_buffer_dirty(leaf);
38345f39d397SChris Mason 				free_extent_buffer(leaf);
3835be0e5c09SChris Mason 			}
3836d5719762SChris Mason 		} else {
38375f39d397SChris Mason 			btrfs_mark_buffer_dirty(leaf);
3838be0e5c09SChris Mason 		}
38399a8dd150SChris Mason 	}
3840aa5d6bedSChris Mason 	return ret;
38419a8dd150SChris Mason }
38429a8dd150SChris Mason 
384397571fd0SChris Mason /*
3844925baeddSChris Mason  * search the tree again to find a leaf with lesser keys
38457bb86316SChris Mason  * returns 0 if it found something or 1 if there are no lesser leaves.
38467bb86316SChris Mason  * returns < 0 on io errors.
3847d352ac68SChris Mason  *
3848d352ac68SChris Mason  * This may release the path, and so you may lose any locks held at the
3849d352ac68SChris Mason  * time you call it.
38507bb86316SChris Mason  */
38517bb86316SChris Mason int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
38527bb86316SChris Mason {
3853925baeddSChris Mason 	struct btrfs_key key;
3854925baeddSChris Mason 	struct btrfs_disk_key found_key;
3855925baeddSChris Mason 	int ret;
38567bb86316SChris Mason 
3857925baeddSChris Mason 	btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
3858925baeddSChris Mason 
3859925baeddSChris Mason 	if (key.offset > 0)
3860925baeddSChris Mason 		key.offset--;
3861925baeddSChris Mason 	else if (key.type > 0)
3862925baeddSChris Mason 		key.type--;
3863925baeddSChris Mason 	else if (key.objectid > 0)
3864925baeddSChris Mason 		key.objectid--;
3865925baeddSChris Mason 	else
38667bb86316SChris Mason 		return 1;
38677bb86316SChris Mason 
3868b3b4aa74SDavid Sterba 	btrfs_release_path(path);
3869925baeddSChris Mason 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3870925baeddSChris Mason 	if (ret < 0)
3871925baeddSChris Mason 		return ret;
3872925baeddSChris Mason 	btrfs_item_key(path->nodes[0], &found_key, 0);
3873925baeddSChris Mason 	ret = comp_keys(&found_key, &key);
3874925baeddSChris Mason 	if (ret < 0)
38757bb86316SChris Mason 		return 0;
3876925baeddSChris Mason 	return 1;
38777bb86316SChris Mason }
38787bb86316SChris Mason 
38793f157a2fSChris Mason /*
38803f157a2fSChris Mason  * A helper function to walk down the tree starting at min_key, and looking
38813f157a2fSChris Mason  * for nodes or leaves that are either in cache or have a minimum
3882d352ac68SChris Mason  * transaction id.  This is used by the btree defrag code, and tree logging
38833f157a2fSChris Mason  *
38843f157a2fSChris Mason  * This does not cow, but it does stuff the starting key it finds back
38853f157a2fSChris Mason  * into min_key, so you can call btrfs_search_slot with cow=1 on the
38863f157a2fSChris Mason  * key and get a writable path.
38873f157a2fSChris Mason  *
38883f157a2fSChris Mason  * This does lock as it descends, and path->keep_locks should be set
38893f157a2fSChris Mason  * to 1 by the caller.
38903f157a2fSChris Mason  *
38913f157a2fSChris Mason  * This honors path->lowest_level to prevent descent past a given level
38923f157a2fSChris Mason  * of the tree.
38933f157a2fSChris Mason  *
3894d352ac68SChris Mason  * min_trans indicates the oldest transaction that you are interested
3895d352ac68SChris Mason  * in walking through.  Any nodes or leaves older than min_trans are
3896d352ac68SChris Mason  * skipped over (without reading them).
3897d352ac68SChris Mason  *
38983f157a2fSChris Mason  * returns zero if something useful was found, < 0 on error and 1 if there
38993f157a2fSChris Mason  * was nothing in the tree that matched the search criteria.
39003f157a2fSChris Mason  */
39013f157a2fSChris Mason int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
3902e02119d5SChris Mason 			 struct btrfs_key *max_key,
39033f157a2fSChris Mason 			 struct btrfs_path *path, int cache_only,
39043f157a2fSChris Mason 			 u64 min_trans)
39053f157a2fSChris Mason {
39063f157a2fSChris Mason 	struct extent_buffer *cur;
39073f157a2fSChris Mason 	struct btrfs_key found_key;
39083f157a2fSChris Mason 	int slot;
39099652480bSYan 	int sret;
39103f157a2fSChris Mason 	u32 nritems;
39113f157a2fSChris Mason 	int level;
39123f157a2fSChris Mason 	int ret = 1;
39133f157a2fSChris Mason 
3914934d375bSChris Mason 	WARN_ON(!path->keep_locks);
39153f157a2fSChris Mason again:
3916bd681513SChris Mason 	cur = btrfs_read_lock_root_node(root);
39173f157a2fSChris Mason 	level = btrfs_header_level(cur);
3918e02119d5SChris Mason 	WARN_ON(path->nodes[level]);
39193f157a2fSChris Mason 	path->nodes[level] = cur;
3920bd681513SChris Mason 	path->locks[level] = BTRFS_READ_LOCK;
39213f157a2fSChris Mason 
39223f157a2fSChris Mason 	if (btrfs_header_generation(cur) < min_trans) {
39233f157a2fSChris Mason 		ret = 1;
39243f157a2fSChris Mason 		goto out;
39253f157a2fSChris Mason 	}
39263f157a2fSChris Mason 	while (1) {
39273f157a2fSChris Mason 		nritems = btrfs_header_nritems(cur);
39283f157a2fSChris Mason 		level = btrfs_header_level(cur);
39299652480bSYan 		sret = bin_search(cur, min_key, level, &slot);
39303f157a2fSChris Mason 
3931323ac95bSChris Mason 		/* at the lowest level, we're done, setup the path and exit */
3932323ac95bSChris Mason 		if (level == path->lowest_level) {
3933e02119d5SChris Mason 			if (slot >= nritems)
3934e02119d5SChris Mason 				goto find_next_key;
39353f157a2fSChris Mason 			ret = 0;
39363f157a2fSChris Mason 			path->slots[level] = slot;
39373f157a2fSChris Mason 			btrfs_item_key_to_cpu(cur, &found_key, slot);
39383f157a2fSChris Mason 			goto out;
39393f157a2fSChris Mason 		}
39409652480bSYan 		if (sret && slot > 0)
39419652480bSYan 			slot--;
39423f157a2fSChris Mason 		/*
39433f157a2fSChris Mason 		 * check this node pointer against the cache_only and
39443f157a2fSChris Mason 		 * min_trans parameters.  If it isn't in cache or is too
39453f157a2fSChris Mason 		 * old, skip to the next one.
39463f157a2fSChris Mason 		 */
39473f157a2fSChris Mason 		while (slot < nritems) {
39483f157a2fSChris Mason 			u64 blockptr;
39493f157a2fSChris Mason 			u64 gen;
39503f157a2fSChris Mason 			struct extent_buffer *tmp;
3951e02119d5SChris Mason 			struct btrfs_disk_key disk_key;
3952e02119d5SChris Mason 
39533f157a2fSChris Mason 			blockptr = btrfs_node_blockptr(cur, slot);
39543f157a2fSChris Mason 			gen = btrfs_node_ptr_generation(cur, slot);
39553f157a2fSChris Mason 			if (gen < min_trans) {
39563f157a2fSChris Mason 				slot++;
39573f157a2fSChris Mason 				continue;
39583f157a2fSChris Mason 			}
39593f157a2fSChris Mason 			if (!cache_only)
39603f157a2fSChris Mason 				break;
39613f157a2fSChris Mason 
3962e02119d5SChris Mason 			if (max_key) {
3963e02119d5SChris Mason 				btrfs_node_key(cur, &disk_key, slot);
3964e02119d5SChris Mason 				if (comp_keys(&disk_key, max_key) >= 0) {
3965e02119d5SChris Mason 					ret = 1;
3966e02119d5SChris Mason 					goto out;
3967e02119d5SChris Mason 				}
3968e02119d5SChris Mason 			}
3969e02119d5SChris Mason 
39703f157a2fSChris Mason 			tmp = btrfs_find_tree_block(root, blockptr,
39713f157a2fSChris Mason 					    btrfs_level_size(root, level - 1));
39723f157a2fSChris Mason 
39733f157a2fSChris Mason 			if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
39743f157a2fSChris Mason 				free_extent_buffer(tmp);
39753f157a2fSChris Mason 				break;
39763f157a2fSChris Mason 			}
39773f157a2fSChris Mason 			if (tmp)
39783f157a2fSChris Mason 				free_extent_buffer(tmp);
39793f157a2fSChris Mason 			slot++;
39803f157a2fSChris Mason 		}
3981e02119d5SChris Mason find_next_key:
39823f157a2fSChris Mason 		/*
39833f157a2fSChris Mason 		 * we didn't find a candidate key in this node, walk forward
39843f157a2fSChris Mason 		 * and find another one
39853f157a2fSChris Mason 		 */
39863f157a2fSChris Mason 		if (slot >= nritems) {
3987e02119d5SChris Mason 			path->slots[level] = slot;
3988b4ce94deSChris Mason 			btrfs_set_path_blocking(path);
3989e02119d5SChris Mason 			sret = btrfs_find_next_key(root, path, min_key, level,
39903f157a2fSChris Mason 						  cache_only, min_trans);
3991e02119d5SChris Mason 			if (sret == 0) {
3992b3b4aa74SDavid Sterba 				btrfs_release_path(path);
39933f157a2fSChris Mason 				goto again;
39943f157a2fSChris Mason 			} else {
39953f157a2fSChris Mason 				goto out;
39963f157a2fSChris Mason 			}
39973f157a2fSChris Mason 		}
39983f157a2fSChris Mason 		/* save our key for returning back */
39993f157a2fSChris Mason 		btrfs_node_key_to_cpu(cur, &found_key, slot);
40003f157a2fSChris Mason 		path->slots[level] = slot;
40013f157a2fSChris Mason 		if (level == path->lowest_level) {
40023f157a2fSChris Mason 			ret = 0;
40033f157a2fSChris Mason 			unlock_up(path, level, 1);
40043f157a2fSChris Mason 			goto out;
40053f157a2fSChris Mason 		}
4006b4ce94deSChris Mason 		btrfs_set_path_blocking(path);
40073f157a2fSChris Mason 		cur = read_node_slot(root, cur, slot);
400897d9a8a4STsutomu Itoh 		BUG_ON(!cur);
40093f157a2fSChris Mason 
4010bd681513SChris Mason 		btrfs_tree_read_lock(cur);
4011b4ce94deSChris Mason 
4012bd681513SChris Mason 		path->locks[level - 1] = BTRFS_READ_LOCK;
40133f157a2fSChris Mason 		path->nodes[level - 1] = cur;
40143f157a2fSChris Mason 		unlock_up(path, level, 1);
4015bd681513SChris Mason 		btrfs_clear_path_blocking(path, NULL, 0);
40163f157a2fSChris Mason 	}
40173f157a2fSChris Mason out:
40183f157a2fSChris Mason 	if (ret == 0)
40193f157a2fSChris Mason 		memcpy(min_key, &found_key, sizeof(found_key));
4020b4ce94deSChris Mason 	btrfs_set_path_blocking(path);
40213f157a2fSChris Mason 	return ret;
40223f157a2fSChris Mason }
40233f157a2fSChris Mason 
40243f157a2fSChris Mason /*
40253f157a2fSChris Mason  * this is similar to btrfs_next_leaf, but does not try to preserve
40263f157a2fSChris Mason  * and fixup the path.  It looks for and returns the next key in the
40273f157a2fSChris Mason  * tree based on the current path and the cache_only and min_trans
40283f157a2fSChris Mason  * parameters.
40293f157a2fSChris Mason  *
40303f157a2fSChris Mason  * 0 is returned if another key is found, < 0 if there are any errors
40313f157a2fSChris Mason  * and 1 is returned if there are no higher keys in the tree
40323f157a2fSChris Mason  *
40333f157a2fSChris Mason  * path->keep_locks should be set to 1 on the search made before
40343f157a2fSChris Mason  * calling this function.
40353f157a2fSChris Mason  */
4036e7a84565SChris Mason int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
403733c66f43SYan Zheng 			struct btrfs_key *key, int level,
40383f157a2fSChris Mason 			int cache_only, u64 min_trans)
4039e7a84565SChris Mason {
4040e7a84565SChris Mason 	int slot;
4041e7a84565SChris Mason 	struct extent_buffer *c;
4042e7a84565SChris Mason 
4043934d375bSChris Mason 	WARN_ON(!path->keep_locks);
4044e7a84565SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
4045e7a84565SChris Mason 		if (!path->nodes[level])
4046e7a84565SChris Mason 			return 1;
4047e7a84565SChris Mason 
4048e7a84565SChris Mason 		slot = path->slots[level] + 1;
4049e7a84565SChris Mason 		c = path->nodes[level];
40503f157a2fSChris Mason next:
4051e7a84565SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
405233c66f43SYan Zheng 			int ret;
405333c66f43SYan Zheng 			int orig_lowest;
405433c66f43SYan Zheng 			struct btrfs_key cur_key;
405533c66f43SYan Zheng 			if (level + 1 >= BTRFS_MAX_LEVEL ||
405633c66f43SYan Zheng 			    !path->nodes[level + 1])
4057e7a84565SChris Mason 				return 1;
405833c66f43SYan Zheng 
405933c66f43SYan Zheng 			if (path->locks[level + 1]) {
406033c66f43SYan Zheng 				level++;
4061e7a84565SChris Mason 				continue;
4062e7a84565SChris Mason 			}
406333c66f43SYan Zheng 
406433c66f43SYan Zheng 			slot = btrfs_header_nritems(c) - 1;
406533c66f43SYan Zheng 			if (level == 0)
406633c66f43SYan Zheng 				btrfs_item_key_to_cpu(c, &cur_key, slot);
406733c66f43SYan Zheng 			else
406833c66f43SYan Zheng 				btrfs_node_key_to_cpu(c, &cur_key, slot);
406933c66f43SYan Zheng 
407033c66f43SYan Zheng 			orig_lowest = path->lowest_level;
4071b3b4aa74SDavid Sterba 			btrfs_release_path(path);
407233c66f43SYan Zheng 			path->lowest_level = level;
407333c66f43SYan Zheng 			ret = btrfs_search_slot(NULL, root, &cur_key, path,
407433c66f43SYan Zheng 						0, 0);
407533c66f43SYan Zheng 			path->lowest_level = orig_lowest;
407633c66f43SYan Zheng 			if (ret < 0)
407733c66f43SYan Zheng 				return ret;
407833c66f43SYan Zheng 
407933c66f43SYan Zheng 			c = path->nodes[level];
408033c66f43SYan Zheng 			slot = path->slots[level];
408133c66f43SYan Zheng 			if (ret == 0)
408233c66f43SYan Zheng 				slot++;
408333c66f43SYan Zheng 			goto next;
408433c66f43SYan Zheng 		}
408533c66f43SYan Zheng 
4086e7a84565SChris Mason 		if (level == 0)
4087e7a84565SChris Mason 			btrfs_item_key_to_cpu(c, key, slot);
40883f157a2fSChris Mason 		else {
40893f157a2fSChris Mason 			u64 blockptr = btrfs_node_blockptr(c, slot);
40903f157a2fSChris Mason 			u64 gen = btrfs_node_ptr_generation(c, slot);
40913f157a2fSChris Mason 
40923f157a2fSChris Mason 			if (cache_only) {
40933f157a2fSChris Mason 				struct extent_buffer *cur;
40943f157a2fSChris Mason 				cur = btrfs_find_tree_block(root, blockptr,
40953f157a2fSChris Mason 					    btrfs_level_size(root, level - 1));
40963f157a2fSChris Mason 				if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
40973f157a2fSChris Mason 					slot++;
40983f157a2fSChris Mason 					if (cur)
40993f157a2fSChris Mason 						free_extent_buffer(cur);
41003f157a2fSChris Mason 					goto next;
41013f157a2fSChris Mason 				}
41023f157a2fSChris Mason 				free_extent_buffer(cur);
41033f157a2fSChris Mason 			}
41043f157a2fSChris Mason 			if (gen < min_trans) {
41053f157a2fSChris Mason 				slot++;
41063f157a2fSChris Mason 				goto next;
41073f157a2fSChris Mason 			}
4108e7a84565SChris Mason 			btrfs_node_key_to_cpu(c, key, slot);
41093f157a2fSChris Mason 		}
4110e7a84565SChris Mason 		return 0;
4111e7a84565SChris Mason 	}
4112e7a84565SChris Mason 	return 1;
4113e7a84565SChris Mason }
4114e7a84565SChris Mason 
41157bb86316SChris Mason /*
4116925baeddSChris Mason  * search the tree again to find a leaf with greater keys
41170f70abe2SChris Mason  * returns 0 if it found something or 1 if there are no greater leaves.
41180f70abe2SChris Mason  * returns < 0 on io errors.
411997571fd0SChris Mason  */
4120234b63a0SChris Mason int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
4121d97e63b6SChris Mason {
4122d97e63b6SChris Mason 	int slot;
41238e73f275SChris Mason 	int level;
41245f39d397SChris Mason 	struct extent_buffer *c;
41258e73f275SChris Mason 	struct extent_buffer *next;
4126925baeddSChris Mason 	struct btrfs_key key;
4127925baeddSChris Mason 	u32 nritems;
4128925baeddSChris Mason 	int ret;
41298e73f275SChris Mason 	int old_spinning = path->leave_spinning;
4130bd681513SChris Mason 	int next_rw_lock = 0;
4131925baeddSChris Mason 
4132925baeddSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
4133d397712bSChris Mason 	if (nritems == 0)
4134925baeddSChris Mason 		return 1;
4135925baeddSChris Mason 
41368e73f275SChris Mason 	btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
41378e73f275SChris Mason again:
41388e73f275SChris Mason 	level = 1;
41398e73f275SChris Mason 	next = NULL;
4140bd681513SChris Mason 	next_rw_lock = 0;
4141b3b4aa74SDavid Sterba 	btrfs_release_path(path);
41428e73f275SChris Mason 
4143a2135011SChris Mason 	path->keep_locks = 1;
41448e73f275SChris Mason 	path->leave_spinning = 1;
41458e73f275SChris Mason 
4146925baeddSChris Mason 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4147925baeddSChris Mason 	path->keep_locks = 0;
4148925baeddSChris Mason 
4149925baeddSChris Mason 	if (ret < 0)
4150925baeddSChris Mason 		return ret;
4151925baeddSChris Mason 
4152a2135011SChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
4153168fd7d2SChris Mason 	/*
4154168fd7d2SChris Mason 	 * by releasing the path above we dropped all our locks.  A balance
4155168fd7d2SChris Mason 	 * could have added more items next to the key that used to be
4156168fd7d2SChris Mason 	 * at the very end of the block.  So, check again here and
4157168fd7d2SChris Mason 	 * advance the path if there are now more items available.
4158168fd7d2SChris Mason 	 */
4159a2135011SChris Mason 	if (nritems > 0 && path->slots[0] < nritems - 1) {
4160e457afecSYan Zheng 		if (ret == 0)
4161168fd7d2SChris Mason 			path->slots[0]++;
41628e73f275SChris Mason 		ret = 0;
4163925baeddSChris Mason 		goto done;
4164925baeddSChris Mason 	}
4165d97e63b6SChris Mason 
4166234b63a0SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
41678e73f275SChris Mason 		if (!path->nodes[level]) {
41688e73f275SChris Mason 			ret = 1;
41698e73f275SChris Mason 			goto done;
41708e73f275SChris Mason 		}
41715f39d397SChris Mason 
4172d97e63b6SChris Mason 		slot = path->slots[level] + 1;
4173d97e63b6SChris Mason 		c = path->nodes[level];
41745f39d397SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
4175d97e63b6SChris Mason 			level++;
41768e73f275SChris Mason 			if (level == BTRFS_MAX_LEVEL) {
41778e73f275SChris Mason 				ret = 1;
41788e73f275SChris Mason 				goto done;
41798e73f275SChris Mason 			}
4180d97e63b6SChris Mason 			continue;
4181d97e63b6SChris Mason 		}
41825f39d397SChris Mason 
4183925baeddSChris Mason 		if (next) {
4184bd681513SChris Mason 			btrfs_tree_unlock_rw(next, next_rw_lock);
41855f39d397SChris Mason 			free_extent_buffer(next);
4186925baeddSChris Mason 		}
41875f39d397SChris Mason 
41888e73f275SChris Mason 		next = c;
4189bd681513SChris Mason 		next_rw_lock = path->locks[level];
41908e73f275SChris Mason 		ret = read_block_for_search(NULL, root, path, &next, level,
41918e73f275SChris Mason 					    slot, &key);
41928e73f275SChris Mason 		if (ret == -EAGAIN)
41938e73f275SChris Mason 			goto again;
41945f39d397SChris Mason 
419576a05b35SChris Mason 		if (ret < 0) {
4196b3b4aa74SDavid Sterba 			btrfs_release_path(path);
419776a05b35SChris Mason 			goto done;
419876a05b35SChris Mason 		}
419976a05b35SChris Mason 
42005cd57b2cSChris Mason 		if (!path->skip_locking) {
4201bd681513SChris Mason 			ret = btrfs_try_tree_read_lock(next);
42028e73f275SChris Mason 			if (!ret) {
42038e73f275SChris Mason 				btrfs_set_path_blocking(path);
4204bd681513SChris Mason 				btrfs_tree_read_lock(next);
4205bd681513SChris Mason 				btrfs_clear_path_blocking(path, next,
4206bd681513SChris Mason 							  BTRFS_READ_LOCK);
42078e73f275SChris Mason 			}
4208bd681513SChris Mason 			next_rw_lock = BTRFS_READ_LOCK;
4209bd681513SChris Mason 		}
4210d97e63b6SChris Mason 		break;
4211d97e63b6SChris Mason 	}
4212d97e63b6SChris Mason 	path->slots[level] = slot;
4213d97e63b6SChris Mason 	while (1) {
4214d97e63b6SChris Mason 		level--;
4215d97e63b6SChris Mason 		c = path->nodes[level];
4216925baeddSChris Mason 		if (path->locks[level])
4217bd681513SChris Mason 			btrfs_tree_unlock_rw(c, path->locks[level]);
42188e73f275SChris Mason 
42195f39d397SChris Mason 		free_extent_buffer(c);
4220d97e63b6SChris Mason 		path->nodes[level] = next;
4221d97e63b6SChris Mason 		path->slots[level] = 0;
4222a74a4b97SChris Mason 		if (!path->skip_locking)
4223bd681513SChris Mason 			path->locks[level] = next_rw_lock;
4224d97e63b6SChris Mason 		if (!level)
4225d97e63b6SChris Mason 			break;
4226b4ce94deSChris Mason 
42278e73f275SChris Mason 		ret = read_block_for_search(NULL, root, path, &next, level,
42288e73f275SChris Mason 					    0, &key);
42298e73f275SChris Mason 		if (ret == -EAGAIN)
42308e73f275SChris Mason 			goto again;
42318e73f275SChris Mason 
423276a05b35SChris Mason 		if (ret < 0) {
4233b3b4aa74SDavid Sterba 			btrfs_release_path(path);
423476a05b35SChris Mason 			goto done;
423576a05b35SChris Mason 		}
423676a05b35SChris Mason 
42375cd57b2cSChris Mason 		if (!path->skip_locking) {
4238bd681513SChris Mason 			ret = btrfs_try_tree_read_lock(next);
42398e73f275SChris Mason 			if (!ret) {
42408e73f275SChris Mason 				btrfs_set_path_blocking(path);
4241bd681513SChris Mason 				btrfs_tree_read_lock(next);
4242bd681513SChris Mason 				btrfs_clear_path_blocking(path, next,
4243bd681513SChris Mason 							  BTRFS_READ_LOCK);
42448e73f275SChris Mason 			}
4245bd681513SChris Mason 			next_rw_lock = BTRFS_READ_LOCK;
4246bd681513SChris Mason 		}
4247d97e63b6SChris Mason 	}
42488e73f275SChris Mason 	ret = 0;
4249925baeddSChris Mason done:
4250925baeddSChris Mason 	unlock_up(path, 0, 1);
42518e73f275SChris Mason 	path->leave_spinning = old_spinning;
42528e73f275SChris Mason 	if (!old_spinning)
42538e73f275SChris Mason 		btrfs_set_path_blocking(path);
42548e73f275SChris Mason 
42558e73f275SChris Mason 	return ret;
4256d97e63b6SChris Mason }
42570b86a832SChris Mason 
42583f157a2fSChris Mason /*
42593f157a2fSChris Mason  * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
42603f157a2fSChris Mason  * searching until it gets past min_objectid or finds an item of 'type'
42613f157a2fSChris Mason  *
42623f157a2fSChris Mason  * returns 0 if something is found, 1 if nothing was found and < 0 on error
42633f157a2fSChris Mason  */
42640b86a832SChris Mason int btrfs_previous_item(struct btrfs_root *root,
42650b86a832SChris Mason 			struct btrfs_path *path, u64 min_objectid,
42660b86a832SChris Mason 			int type)
42670b86a832SChris Mason {
42680b86a832SChris Mason 	struct btrfs_key found_key;
42690b86a832SChris Mason 	struct extent_buffer *leaf;
4270e02119d5SChris Mason 	u32 nritems;
42710b86a832SChris Mason 	int ret;
42720b86a832SChris Mason 
42730b86a832SChris Mason 	while (1) {
42740b86a832SChris Mason 		if (path->slots[0] == 0) {
4275b4ce94deSChris Mason 			btrfs_set_path_blocking(path);
42760b86a832SChris Mason 			ret = btrfs_prev_leaf(root, path);
42770b86a832SChris Mason 			if (ret != 0)
42780b86a832SChris Mason 				return ret;
42790b86a832SChris Mason 		} else {
42800b86a832SChris Mason 			path->slots[0]--;
42810b86a832SChris Mason 		}
42820b86a832SChris Mason 		leaf = path->nodes[0];
4283e02119d5SChris Mason 		nritems = btrfs_header_nritems(leaf);
4284e02119d5SChris Mason 		if (nritems == 0)
4285e02119d5SChris Mason 			return 1;
4286e02119d5SChris Mason 		if (path->slots[0] == nritems)
4287e02119d5SChris Mason 			path->slots[0]--;
4288e02119d5SChris Mason 
42890b86a832SChris Mason 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4290e02119d5SChris Mason 		if (found_key.objectid < min_objectid)
4291e02119d5SChris Mason 			break;
42920a4eefbbSYan Zheng 		if (found_key.type == type)
42930a4eefbbSYan Zheng 			return 0;
4294e02119d5SChris Mason 		if (found_key.objectid == min_objectid &&
4295e02119d5SChris Mason 		    found_key.type < type)
4296e02119d5SChris Mason 			break;
42970b86a832SChris Mason 	}
42980b86a832SChris Mason 	return 1;
42990b86a832SChris Mason }
4300