xref: /openbmc/linux/fs/btrfs/ctree.c (revision be1a5564fd39fa2ca6adbb41c75fb08f96a1ffcb)
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);
334*be1a5564SMark Fasheh 		if (ret)
335*be1a5564SMark Fasheh 			return ret;
3365d4f98a2SYan Zheng 		BUG_ON(refs == 0);
3375d4f98a2SYan Zheng 	} else {
3385d4f98a2SYan Zheng 		refs = 1;
3395d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
3405d4f98a2SYan Zheng 		    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
3415d4f98a2SYan Zheng 			flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
3425d4f98a2SYan Zheng 		else
3435d4f98a2SYan Zheng 			flags = 0;
3445d4f98a2SYan Zheng 	}
3455d4f98a2SYan Zheng 
3465d4f98a2SYan Zheng 	owner = btrfs_header_owner(buf);
3475d4f98a2SYan Zheng 	BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
3485d4f98a2SYan Zheng 	       !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
3495d4f98a2SYan Zheng 
3505d4f98a2SYan Zheng 	if (refs > 1) {
3515d4f98a2SYan Zheng 		if ((owner == root->root_key.objectid ||
3525d4f98a2SYan Zheng 		     root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
3535d4f98a2SYan Zheng 		    !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
35466d7e7f0SArne Jansen 			ret = btrfs_inc_ref(trans, root, buf, 1, 1);
3555d4f98a2SYan Zheng 			BUG_ON(ret);
3565d4f98a2SYan Zheng 
3575d4f98a2SYan Zheng 			if (root->root_key.objectid ==
3585d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID) {
35966d7e7f0SArne Jansen 				ret = btrfs_dec_ref(trans, root, buf, 0, 1);
3605d4f98a2SYan Zheng 				BUG_ON(ret);
36166d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 1, 1);
3625d4f98a2SYan Zheng 				BUG_ON(ret);
3635d4f98a2SYan Zheng 			}
3645d4f98a2SYan Zheng 			new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
3655d4f98a2SYan Zheng 		} else {
3665d4f98a2SYan Zheng 
3675d4f98a2SYan Zheng 			if (root->root_key.objectid ==
3685d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID)
36966d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 1, 1);
3705d4f98a2SYan Zheng 			else
37166d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 0, 1);
3725d4f98a2SYan Zheng 			BUG_ON(ret);
3735d4f98a2SYan Zheng 		}
3745d4f98a2SYan Zheng 		if (new_flags != 0) {
3755d4f98a2SYan Zheng 			ret = btrfs_set_disk_extent_flags(trans, root,
3765d4f98a2SYan Zheng 							  buf->start,
3775d4f98a2SYan Zheng 							  buf->len,
3785d4f98a2SYan Zheng 							  new_flags, 0);
379*be1a5564SMark Fasheh 			if (ret)
380*be1a5564SMark Fasheh 				return ret;
3815d4f98a2SYan Zheng 		}
3825d4f98a2SYan Zheng 	} else {
3835d4f98a2SYan Zheng 		if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
3845d4f98a2SYan Zheng 			if (root->root_key.objectid ==
3855d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID)
38666d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 1, 1);
3875d4f98a2SYan Zheng 			else
38866d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 0, 1);
3895d4f98a2SYan Zheng 			BUG_ON(ret);
39066d7e7f0SArne Jansen 			ret = btrfs_dec_ref(trans, root, buf, 1, 1);
3915d4f98a2SYan Zheng 			BUG_ON(ret);
3925d4f98a2SYan Zheng 		}
3935d4f98a2SYan Zheng 		clean_tree_block(trans, root, buf);
394f0486c68SYan, Zheng 		*last_ref = 1;
3955d4f98a2SYan Zheng 	}
3965d4f98a2SYan Zheng 	return 0;
3975d4f98a2SYan Zheng }
3985d4f98a2SYan Zheng 
3995d4f98a2SYan Zheng /*
400d397712bSChris Mason  * does the dirty work in cow of a single block.  The parent block (if
401d397712bSChris Mason  * supplied) is updated to point to the new cow copy.  The new buffer is marked
402d397712bSChris Mason  * dirty and returned locked.  If you modify the block it needs to be marked
403d397712bSChris Mason  * dirty again.
404d352ac68SChris Mason  *
405d352ac68SChris Mason  * search_start -- an allocation hint for the new block
406d352ac68SChris Mason  *
407d397712bSChris Mason  * empty_size -- a hint that you plan on doing more cow.  This is the size in
408d397712bSChris Mason  * bytes the allocator should try to find free next to the block it returns.
409d397712bSChris Mason  * This is just a hint and may be ignored by the allocator.
410d352ac68SChris Mason  */
411d397712bSChris Mason static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
4125f39d397SChris Mason 			     struct btrfs_root *root,
4135f39d397SChris Mason 			     struct extent_buffer *buf,
4145f39d397SChris Mason 			     struct extent_buffer *parent, int parent_slot,
4155f39d397SChris Mason 			     struct extent_buffer **cow_ret,
4169fa8cfe7SChris Mason 			     u64 search_start, u64 empty_size)
4176702ed49SChris Mason {
4185d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
4195f39d397SChris Mason 	struct extent_buffer *cow;
420*be1a5564SMark Fasheh 	int level, ret;
421f0486c68SYan, Zheng 	int last_ref = 0;
422925baeddSChris Mason 	int unlock_orig = 0;
4235d4f98a2SYan Zheng 	u64 parent_start;
4246702ed49SChris Mason 
425925baeddSChris Mason 	if (*cow_ret == buf)
426925baeddSChris Mason 		unlock_orig = 1;
427925baeddSChris Mason 
428b9447ef8SChris Mason 	btrfs_assert_tree_locked(buf);
429925baeddSChris Mason 
4307bb86316SChris Mason 	WARN_ON(root->ref_cows && trans->transid !=
4317bb86316SChris Mason 		root->fs_info->running_transaction->transid);
4326702ed49SChris Mason 	WARN_ON(root->ref_cows && trans->transid != root->last_trans);
4335f39d397SChris Mason 
4347bb86316SChris Mason 	level = btrfs_header_level(buf);
43531840ae1SZheng Yan 
4365d4f98a2SYan Zheng 	if (level == 0)
4375d4f98a2SYan Zheng 		btrfs_item_key(buf, &disk_key, 0);
4385d4f98a2SYan Zheng 	else
4395d4f98a2SYan Zheng 		btrfs_node_key(buf, &disk_key, 0);
4405d4f98a2SYan Zheng 
4415d4f98a2SYan Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
4425d4f98a2SYan Zheng 		if (parent)
4435d4f98a2SYan Zheng 			parent_start = parent->start;
4445d4f98a2SYan Zheng 		else
4455d4f98a2SYan Zheng 			parent_start = 0;
4465d4f98a2SYan Zheng 	} else
4475d4f98a2SYan Zheng 		parent_start = 0;
4485d4f98a2SYan Zheng 
4495d4f98a2SYan Zheng 	cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
4505d4f98a2SYan Zheng 				     root->root_key.objectid, &disk_key,
45166d7e7f0SArne Jansen 				     level, search_start, empty_size, 1);
4526702ed49SChris Mason 	if (IS_ERR(cow))
4536702ed49SChris Mason 		return PTR_ERR(cow);
4546702ed49SChris Mason 
455b4ce94deSChris Mason 	/* cow is set to blocking by btrfs_init_new_buffer */
456b4ce94deSChris Mason 
4575f39d397SChris Mason 	copy_extent_buffer(cow, buf, 0, 0, cow->len);
458db94535dSChris Mason 	btrfs_set_header_bytenr(cow, cow->start);
4595f39d397SChris Mason 	btrfs_set_header_generation(cow, trans->transid);
4605d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
4615d4f98a2SYan Zheng 	btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
4625d4f98a2SYan Zheng 				     BTRFS_HEADER_FLAG_RELOC);
4635d4f98a2SYan Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4645d4f98a2SYan Zheng 		btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
4655d4f98a2SYan Zheng 	else
4665f39d397SChris Mason 		btrfs_set_header_owner(cow, root->root_key.objectid);
4676702ed49SChris Mason 
4682b82032cSYan Zheng 	write_extent_buffer(cow, root->fs_info->fsid,
4692b82032cSYan Zheng 			    (unsigned long)btrfs_header_fsid(cow),
4702b82032cSYan Zheng 			    BTRFS_FSID_SIZE);
4712b82032cSYan Zheng 
472*be1a5564SMark Fasheh 	ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
473*be1a5564SMark Fasheh 	BUG_ON(ret);
4741a40e23bSZheng Yan 
4753fd0a558SYan, Zheng 	if (root->ref_cows)
4763fd0a558SYan, Zheng 		btrfs_reloc_cow_block(trans, root, buf, cow);
4773fd0a558SYan, Zheng 
4786702ed49SChris Mason 	if (buf == root->node) {
479925baeddSChris Mason 		WARN_ON(parent && parent != buf);
4805d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
4815d4f98a2SYan Zheng 		    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
4825d4f98a2SYan Zheng 			parent_start = buf->start;
4835d4f98a2SYan Zheng 		else
4845d4f98a2SYan Zheng 			parent_start = 0;
485925baeddSChris Mason 
4865f39d397SChris Mason 		extent_buffer_get(cow);
487240f62c8SChris Mason 		rcu_assign_pointer(root->node, cow);
488925baeddSChris Mason 
489f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, buf, parent_start,
49066d7e7f0SArne Jansen 				      last_ref, 1);
4915f39d397SChris Mason 		free_extent_buffer(buf);
4920b86a832SChris Mason 		add_root_to_dirty_list(root);
4936702ed49SChris Mason 	} else {
4945d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4955d4f98a2SYan Zheng 			parent_start = parent->start;
4965d4f98a2SYan Zheng 		else
4975d4f98a2SYan Zheng 			parent_start = 0;
4985d4f98a2SYan Zheng 
4995d4f98a2SYan Zheng 		WARN_ON(trans->transid != btrfs_header_generation(parent));
5005f39d397SChris Mason 		btrfs_set_node_blockptr(parent, parent_slot,
501db94535dSChris Mason 					cow->start);
50274493f7aSChris Mason 		btrfs_set_node_ptr_generation(parent, parent_slot,
50374493f7aSChris Mason 					      trans->transid);
5046702ed49SChris Mason 		btrfs_mark_buffer_dirty(parent);
505f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, buf, parent_start,
50666d7e7f0SArne Jansen 				      last_ref, 1);
5076702ed49SChris Mason 	}
508925baeddSChris Mason 	if (unlock_orig)
509925baeddSChris Mason 		btrfs_tree_unlock(buf);
5105f39d397SChris Mason 	free_extent_buffer(buf);
5116702ed49SChris Mason 	btrfs_mark_buffer_dirty(cow);
5126702ed49SChris Mason 	*cow_ret = cow;
5136702ed49SChris Mason 	return 0;
5146702ed49SChris Mason }
5156702ed49SChris Mason 
5165d4f98a2SYan Zheng static inline int should_cow_block(struct btrfs_trans_handle *trans,
5175d4f98a2SYan Zheng 				   struct btrfs_root *root,
5185d4f98a2SYan Zheng 				   struct extent_buffer *buf)
5195d4f98a2SYan Zheng {
520f1ebcc74SLiu Bo 	/* ensure we can see the force_cow */
521f1ebcc74SLiu Bo 	smp_rmb();
522f1ebcc74SLiu Bo 
523f1ebcc74SLiu Bo 	/*
524f1ebcc74SLiu Bo 	 * We do not need to cow a block if
525f1ebcc74SLiu Bo 	 * 1) this block is not created or changed in this transaction;
526f1ebcc74SLiu Bo 	 * 2) this block does not belong to TREE_RELOC tree;
527f1ebcc74SLiu Bo 	 * 3) the root is not forced COW.
528f1ebcc74SLiu Bo 	 *
529f1ebcc74SLiu Bo 	 * What is forced COW:
530f1ebcc74SLiu Bo 	 *    when we create snapshot during commiting the transaction,
531f1ebcc74SLiu Bo 	 *    after we've finished coping src root, we must COW the shared
532f1ebcc74SLiu Bo 	 *    block to ensure the metadata consistency.
533f1ebcc74SLiu Bo 	 */
5345d4f98a2SYan Zheng 	if (btrfs_header_generation(buf) == trans->transid &&
5355d4f98a2SYan Zheng 	    !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
5365d4f98a2SYan Zheng 	    !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
537f1ebcc74SLiu Bo 	      btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
538f1ebcc74SLiu Bo 	    !root->force_cow)
5395d4f98a2SYan Zheng 		return 0;
5405d4f98a2SYan Zheng 	return 1;
5415d4f98a2SYan Zheng }
5425d4f98a2SYan Zheng 
543d352ac68SChris Mason /*
544d352ac68SChris Mason  * cows a single block, see __btrfs_cow_block for the real work.
545d352ac68SChris Mason  * This version of it has extra checks so that a block isn't cow'd more than
546d352ac68SChris Mason  * once per transaction, as long as it hasn't been written yet
547d352ac68SChris Mason  */
548d397712bSChris Mason noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
5495f39d397SChris Mason 		    struct btrfs_root *root, struct extent_buffer *buf,
5505f39d397SChris Mason 		    struct extent_buffer *parent, int parent_slot,
5519fa8cfe7SChris Mason 		    struct extent_buffer **cow_ret)
55202217ed2SChris Mason {
5536702ed49SChris Mason 	u64 search_start;
554f510cfecSChris Mason 	int ret;
555dc17ff8fSChris Mason 
556ccd467d6SChris Mason 	if (trans->transaction != root->fs_info->running_transaction) {
557d397712bSChris Mason 		printk(KERN_CRIT "trans %llu running %llu\n",
558d397712bSChris Mason 		       (unsigned long long)trans->transid,
559d397712bSChris Mason 		       (unsigned long long)
560ccd467d6SChris Mason 		       root->fs_info->running_transaction->transid);
561ccd467d6SChris Mason 		WARN_ON(1);
562ccd467d6SChris Mason 	}
563ccd467d6SChris Mason 	if (trans->transid != root->fs_info->generation) {
564d397712bSChris Mason 		printk(KERN_CRIT "trans %llu running %llu\n",
565d397712bSChris Mason 		       (unsigned long long)trans->transid,
566d397712bSChris Mason 		       (unsigned long long)root->fs_info->generation);
567ccd467d6SChris Mason 		WARN_ON(1);
568ccd467d6SChris Mason 	}
569dc17ff8fSChris Mason 
5705d4f98a2SYan Zheng 	if (!should_cow_block(trans, root, buf)) {
57102217ed2SChris Mason 		*cow_ret = buf;
57202217ed2SChris Mason 		return 0;
57302217ed2SChris Mason 	}
574c487685dSChris Mason 
5750b86a832SChris Mason 	search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
576b4ce94deSChris Mason 
577b4ce94deSChris Mason 	if (parent)
578b4ce94deSChris Mason 		btrfs_set_lock_blocking(parent);
579b4ce94deSChris Mason 	btrfs_set_lock_blocking(buf);
580b4ce94deSChris Mason 
581f510cfecSChris Mason 	ret = __btrfs_cow_block(trans, root, buf, parent,
5829fa8cfe7SChris Mason 				 parent_slot, cow_ret, search_start, 0);
5831abe9b8aSliubo 
5841abe9b8aSliubo 	trace_btrfs_cow_block(root, buf, *cow_ret);
5851abe9b8aSliubo 
586f510cfecSChris Mason 	return ret;
5872c90e5d6SChris Mason }
5886702ed49SChris Mason 
589d352ac68SChris Mason /*
590d352ac68SChris Mason  * helper function for defrag to decide if two blocks pointed to by a
591d352ac68SChris Mason  * node are actually close by
592d352ac68SChris Mason  */
5936b80053dSChris Mason static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
5946702ed49SChris Mason {
5956b80053dSChris Mason 	if (blocknr < other && other - (blocknr + blocksize) < 32768)
5966702ed49SChris Mason 		return 1;
5976b80053dSChris Mason 	if (blocknr > other && blocknr - (other + blocksize) < 32768)
5986702ed49SChris Mason 		return 1;
59902217ed2SChris Mason 	return 0;
60002217ed2SChris Mason }
60102217ed2SChris Mason 
602081e9573SChris Mason /*
603081e9573SChris Mason  * compare two keys in a memcmp fashion
604081e9573SChris Mason  */
605081e9573SChris Mason static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
606081e9573SChris Mason {
607081e9573SChris Mason 	struct btrfs_key k1;
608081e9573SChris Mason 
609081e9573SChris Mason 	btrfs_disk_key_to_cpu(&k1, disk);
610081e9573SChris Mason 
61120736abaSDiego Calleja 	return btrfs_comp_cpu_keys(&k1, k2);
612081e9573SChris Mason }
613081e9573SChris Mason 
614f3465ca4SJosef Bacik /*
615f3465ca4SJosef Bacik  * same as comp_keys only with two btrfs_key's
616f3465ca4SJosef Bacik  */
6175d4f98a2SYan Zheng int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
618f3465ca4SJosef Bacik {
619f3465ca4SJosef Bacik 	if (k1->objectid > k2->objectid)
620f3465ca4SJosef Bacik 		return 1;
621f3465ca4SJosef Bacik 	if (k1->objectid < k2->objectid)
622f3465ca4SJosef Bacik 		return -1;
623f3465ca4SJosef Bacik 	if (k1->type > k2->type)
624f3465ca4SJosef Bacik 		return 1;
625f3465ca4SJosef Bacik 	if (k1->type < k2->type)
626f3465ca4SJosef Bacik 		return -1;
627f3465ca4SJosef Bacik 	if (k1->offset > k2->offset)
628f3465ca4SJosef Bacik 		return 1;
629f3465ca4SJosef Bacik 	if (k1->offset < k2->offset)
630f3465ca4SJosef Bacik 		return -1;
631f3465ca4SJosef Bacik 	return 0;
632f3465ca4SJosef Bacik }
633081e9573SChris Mason 
634d352ac68SChris Mason /*
635d352ac68SChris Mason  * this is used by the defrag code to go through all the
636d352ac68SChris Mason  * leaves pointed to by a node and reallocate them so that
637d352ac68SChris Mason  * disk order is close to key order
638d352ac68SChris Mason  */
6396702ed49SChris Mason int btrfs_realloc_node(struct btrfs_trans_handle *trans,
6405f39d397SChris Mason 		       struct btrfs_root *root, struct extent_buffer *parent,
641a6b6e75eSChris Mason 		       int start_slot, int cache_only, u64 *last_ret,
642a6b6e75eSChris Mason 		       struct btrfs_key *progress)
6436702ed49SChris Mason {
6446b80053dSChris Mason 	struct extent_buffer *cur;
6456702ed49SChris Mason 	u64 blocknr;
646ca7a79adSChris Mason 	u64 gen;
647e9d0b13bSChris Mason 	u64 search_start = *last_ret;
648e9d0b13bSChris Mason 	u64 last_block = 0;
6496702ed49SChris Mason 	u64 other;
6506702ed49SChris Mason 	u32 parent_nritems;
6516702ed49SChris Mason 	int end_slot;
6526702ed49SChris Mason 	int i;
6536702ed49SChris Mason 	int err = 0;
654f2183bdeSChris Mason 	int parent_level;
6556b80053dSChris Mason 	int uptodate;
6566b80053dSChris Mason 	u32 blocksize;
657081e9573SChris Mason 	int progress_passed = 0;
658081e9573SChris Mason 	struct btrfs_disk_key disk_key;
6596702ed49SChris Mason 
6605708b959SChris Mason 	parent_level = btrfs_header_level(parent);
6615708b959SChris Mason 	if (cache_only && parent_level != 1)
6625708b959SChris Mason 		return 0;
6635708b959SChris Mason 
664d397712bSChris Mason 	if (trans->transaction != root->fs_info->running_transaction)
6656702ed49SChris Mason 		WARN_ON(1);
666d397712bSChris Mason 	if (trans->transid != root->fs_info->generation)
6676702ed49SChris Mason 		WARN_ON(1);
66886479a04SChris Mason 
6696b80053dSChris Mason 	parent_nritems = btrfs_header_nritems(parent);
6706b80053dSChris Mason 	blocksize = btrfs_level_size(root, parent_level - 1);
6716702ed49SChris Mason 	end_slot = parent_nritems;
6726702ed49SChris Mason 
6736702ed49SChris Mason 	if (parent_nritems == 1)
6746702ed49SChris Mason 		return 0;
6756702ed49SChris Mason 
676b4ce94deSChris Mason 	btrfs_set_lock_blocking(parent);
677b4ce94deSChris Mason 
6786702ed49SChris Mason 	for (i = start_slot; i < end_slot; i++) {
6796702ed49SChris Mason 		int close = 1;
680a6b6e75eSChris Mason 
681081e9573SChris Mason 		btrfs_node_key(parent, &disk_key, i);
682081e9573SChris Mason 		if (!progress_passed && comp_keys(&disk_key, progress) < 0)
683081e9573SChris Mason 			continue;
684081e9573SChris Mason 
685081e9573SChris Mason 		progress_passed = 1;
6866b80053dSChris Mason 		blocknr = btrfs_node_blockptr(parent, i);
687ca7a79adSChris Mason 		gen = btrfs_node_ptr_generation(parent, i);
688e9d0b13bSChris Mason 		if (last_block == 0)
689e9d0b13bSChris Mason 			last_block = blocknr;
6905708b959SChris Mason 
6916702ed49SChris Mason 		if (i > 0) {
6926b80053dSChris Mason 			other = btrfs_node_blockptr(parent, i - 1);
6936b80053dSChris Mason 			close = close_blocks(blocknr, other, blocksize);
6946702ed49SChris Mason 		}
6950ef3e66bSChris Mason 		if (!close && i < end_slot - 2) {
6966b80053dSChris Mason 			other = btrfs_node_blockptr(parent, i + 1);
6976b80053dSChris Mason 			close = close_blocks(blocknr, other, blocksize);
6986702ed49SChris Mason 		}
699e9d0b13bSChris Mason 		if (close) {
700e9d0b13bSChris Mason 			last_block = blocknr;
7016702ed49SChris Mason 			continue;
702e9d0b13bSChris Mason 		}
7036702ed49SChris Mason 
7046b80053dSChris Mason 		cur = btrfs_find_tree_block(root, blocknr, blocksize);
7056b80053dSChris Mason 		if (cur)
7061259ab75SChris Mason 			uptodate = btrfs_buffer_uptodate(cur, gen);
7076b80053dSChris Mason 		else
7086b80053dSChris Mason 			uptodate = 0;
7095708b959SChris Mason 		if (!cur || !uptodate) {
7106702ed49SChris Mason 			if (cache_only) {
7116b80053dSChris Mason 				free_extent_buffer(cur);
7126702ed49SChris Mason 				continue;
7136702ed49SChris Mason 			}
7146b80053dSChris Mason 			if (!cur) {
7156b80053dSChris Mason 				cur = read_tree_block(root, blocknr,
716ca7a79adSChris Mason 							 blocksize, gen);
71797d9a8a4STsutomu Itoh 				if (!cur)
71897d9a8a4STsutomu Itoh 					return -EIO;
7196b80053dSChris Mason 			} else if (!uptodate) {
720ca7a79adSChris Mason 				btrfs_read_buffer(cur, gen);
7216702ed49SChris Mason 			}
722f2183bdeSChris Mason 		}
723e9d0b13bSChris Mason 		if (search_start == 0)
7246b80053dSChris Mason 			search_start = last_block;
725e9d0b13bSChris Mason 
726e7a84565SChris Mason 		btrfs_tree_lock(cur);
727b4ce94deSChris Mason 		btrfs_set_lock_blocking(cur);
7286b80053dSChris Mason 		err = __btrfs_cow_block(trans, root, cur, parent, i,
729e7a84565SChris Mason 					&cur, search_start,
7306b80053dSChris Mason 					min(16 * blocksize,
7319fa8cfe7SChris Mason 					    (end_slot - i) * blocksize));
732252c38f0SYan 		if (err) {
733e7a84565SChris Mason 			btrfs_tree_unlock(cur);
7346b80053dSChris Mason 			free_extent_buffer(cur);
7356702ed49SChris Mason 			break;
736252c38f0SYan 		}
737e7a84565SChris Mason 		search_start = cur->start;
738e7a84565SChris Mason 		last_block = cur->start;
739f2183bdeSChris Mason 		*last_ret = search_start;
740e7a84565SChris Mason 		btrfs_tree_unlock(cur);
741e7a84565SChris Mason 		free_extent_buffer(cur);
7426702ed49SChris Mason 	}
7436702ed49SChris Mason 	return err;
7446702ed49SChris Mason }
7456702ed49SChris Mason 
74674123bd7SChris Mason /*
74774123bd7SChris Mason  * The leaf data grows from end-to-front in the node.
74874123bd7SChris Mason  * this returns the address of the start of the last item,
74974123bd7SChris Mason  * which is the stop of the leaf data stack
75074123bd7SChris Mason  */
751123abc88SChris Mason static inline unsigned int leaf_data_end(struct btrfs_root *root,
7525f39d397SChris Mason 					 struct extent_buffer *leaf)
753be0e5c09SChris Mason {
7545f39d397SChris Mason 	u32 nr = btrfs_header_nritems(leaf);
755be0e5c09SChris Mason 	if (nr == 0)
756123abc88SChris Mason 		return BTRFS_LEAF_DATA_SIZE(root);
7575f39d397SChris Mason 	return btrfs_item_offset_nr(leaf, nr - 1);
758be0e5c09SChris Mason }
759be0e5c09SChris Mason 
760aa5d6bedSChris Mason 
76174123bd7SChris Mason /*
7625f39d397SChris Mason  * search for key in the extent_buffer.  The items start at offset p,
7635f39d397SChris Mason  * and they are item_size apart.  There are 'max' items in p.
7645f39d397SChris Mason  *
76574123bd7SChris Mason  * the slot in the array is returned via slot, and it points to
76674123bd7SChris Mason  * the place where you would insert key if it is not found in
76774123bd7SChris Mason  * the array.
76874123bd7SChris Mason  *
76974123bd7SChris Mason  * slot may point to max if the key is bigger than all of the keys
77074123bd7SChris Mason  */
771e02119d5SChris Mason static noinline int generic_bin_search(struct extent_buffer *eb,
772e02119d5SChris Mason 				       unsigned long p,
7735f39d397SChris Mason 				       int item_size, struct btrfs_key *key,
774be0e5c09SChris Mason 				       int max, int *slot)
775be0e5c09SChris Mason {
776be0e5c09SChris Mason 	int low = 0;
777be0e5c09SChris Mason 	int high = max;
778be0e5c09SChris Mason 	int mid;
779be0e5c09SChris Mason 	int ret;
780479965d6SChris Mason 	struct btrfs_disk_key *tmp = NULL;
7815f39d397SChris Mason 	struct btrfs_disk_key unaligned;
7825f39d397SChris Mason 	unsigned long offset;
7835f39d397SChris Mason 	char *kaddr = NULL;
7845f39d397SChris Mason 	unsigned long map_start = 0;
7855f39d397SChris Mason 	unsigned long map_len = 0;
786479965d6SChris Mason 	int err;
787be0e5c09SChris Mason 
788be0e5c09SChris Mason 	while (low < high) {
789be0e5c09SChris Mason 		mid = (low + high) / 2;
7905f39d397SChris Mason 		offset = p + mid * item_size;
7915f39d397SChris Mason 
792a6591715SChris Mason 		if (!kaddr || offset < map_start ||
7935f39d397SChris Mason 		    (offset + sizeof(struct btrfs_disk_key)) >
7945f39d397SChris Mason 		    map_start + map_len) {
795934d375bSChris Mason 
796934d375bSChris Mason 			err = map_private_extent_buffer(eb, offset,
797479965d6SChris Mason 						sizeof(struct btrfs_disk_key),
798a6591715SChris Mason 						&kaddr, &map_start, &map_len);
7995f39d397SChris Mason 
800479965d6SChris Mason 			if (!err) {
801479965d6SChris Mason 				tmp = (struct btrfs_disk_key *)(kaddr + offset -
802479965d6SChris Mason 							map_start);
803479965d6SChris Mason 			} else {
8045f39d397SChris Mason 				read_extent_buffer(eb, &unaligned,
8055f39d397SChris Mason 						   offset, sizeof(unaligned));
8065f39d397SChris Mason 				tmp = &unaligned;
807479965d6SChris Mason 			}
808479965d6SChris Mason 
8095f39d397SChris Mason 		} else {
8105f39d397SChris Mason 			tmp = (struct btrfs_disk_key *)(kaddr + offset -
8115f39d397SChris Mason 							map_start);
8125f39d397SChris Mason 		}
813be0e5c09SChris Mason 		ret = comp_keys(tmp, key);
814be0e5c09SChris Mason 
815be0e5c09SChris Mason 		if (ret < 0)
816be0e5c09SChris Mason 			low = mid + 1;
817be0e5c09SChris Mason 		else if (ret > 0)
818be0e5c09SChris Mason 			high = mid;
819be0e5c09SChris Mason 		else {
820be0e5c09SChris Mason 			*slot = mid;
821be0e5c09SChris Mason 			return 0;
822be0e5c09SChris Mason 		}
823be0e5c09SChris Mason 	}
824be0e5c09SChris Mason 	*slot = low;
825be0e5c09SChris Mason 	return 1;
826be0e5c09SChris Mason }
827be0e5c09SChris Mason 
82897571fd0SChris Mason /*
82997571fd0SChris Mason  * simple bin_search frontend that does the right thing for
83097571fd0SChris Mason  * leaves vs nodes
83197571fd0SChris Mason  */
8325f39d397SChris Mason static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
8335f39d397SChris Mason 		      int level, int *slot)
834be0e5c09SChris Mason {
8355f39d397SChris Mason 	if (level == 0) {
8365f39d397SChris Mason 		return generic_bin_search(eb,
8375f39d397SChris Mason 					  offsetof(struct btrfs_leaf, items),
8380783fcfcSChris Mason 					  sizeof(struct btrfs_item),
8395f39d397SChris Mason 					  key, btrfs_header_nritems(eb),
8407518a238SChris Mason 					  slot);
841be0e5c09SChris Mason 	} else {
8425f39d397SChris Mason 		return generic_bin_search(eb,
8435f39d397SChris Mason 					  offsetof(struct btrfs_node, ptrs),
844123abc88SChris Mason 					  sizeof(struct btrfs_key_ptr),
8455f39d397SChris Mason 					  key, btrfs_header_nritems(eb),
8467518a238SChris Mason 					  slot);
847be0e5c09SChris Mason 	}
848be0e5c09SChris Mason 	return -1;
849be0e5c09SChris Mason }
850be0e5c09SChris Mason 
8515d4f98a2SYan Zheng int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
8525d4f98a2SYan Zheng 		     int level, int *slot)
8535d4f98a2SYan Zheng {
8545d4f98a2SYan Zheng 	return bin_search(eb, key, level, slot);
8555d4f98a2SYan Zheng }
8565d4f98a2SYan Zheng 
857f0486c68SYan, Zheng static void root_add_used(struct btrfs_root *root, u32 size)
858f0486c68SYan, Zheng {
859f0486c68SYan, Zheng 	spin_lock(&root->accounting_lock);
860f0486c68SYan, Zheng 	btrfs_set_root_used(&root->root_item,
861f0486c68SYan, Zheng 			    btrfs_root_used(&root->root_item) + size);
862f0486c68SYan, Zheng 	spin_unlock(&root->accounting_lock);
863f0486c68SYan, Zheng }
864f0486c68SYan, Zheng 
865f0486c68SYan, Zheng static void root_sub_used(struct btrfs_root *root, u32 size)
866f0486c68SYan, Zheng {
867f0486c68SYan, Zheng 	spin_lock(&root->accounting_lock);
868f0486c68SYan, Zheng 	btrfs_set_root_used(&root->root_item,
869f0486c68SYan, Zheng 			    btrfs_root_used(&root->root_item) - size);
870f0486c68SYan, Zheng 	spin_unlock(&root->accounting_lock);
871f0486c68SYan, Zheng }
872f0486c68SYan, Zheng 
873d352ac68SChris Mason /* given a node and slot number, this reads the blocks it points to.  The
874d352ac68SChris Mason  * extent buffer is returned with a reference taken (but unlocked).
875d352ac68SChris Mason  * NULL is returned on error.
876d352ac68SChris Mason  */
877e02119d5SChris Mason static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
8785f39d397SChris Mason 				   struct extent_buffer *parent, int slot)
879bb803951SChris Mason {
880ca7a79adSChris Mason 	int level = btrfs_header_level(parent);
881bb803951SChris Mason 	if (slot < 0)
882bb803951SChris Mason 		return NULL;
8835f39d397SChris Mason 	if (slot >= btrfs_header_nritems(parent))
884bb803951SChris Mason 		return NULL;
885ca7a79adSChris Mason 
886ca7a79adSChris Mason 	BUG_ON(level == 0);
887ca7a79adSChris Mason 
888db94535dSChris Mason 	return read_tree_block(root, btrfs_node_blockptr(parent, slot),
889ca7a79adSChris Mason 		       btrfs_level_size(root, level - 1),
890ca7a79adSChris Mason 		       btrfs_node_ptr_generation(parent, slot));
891bb803951SChris Mason }
892bb803951SChris Mason 
893d352ac68SChris Mason /*
894d352ac68SChris Mason  * node level balancing, used to make sure nodes are in proper order for
895d352ac68SChris Mason  * item deletion.  We balance from the top down, so we have to make sure
896d352ac68SChris Mason  * that a deletion won't leave an node completely empty later on.
897d352ac68SChris Mason  */
898e02119d5SChris Mason static noinline int balance_level(struct btrfs_trans_handle *trans,
89998ed5174SChris Mason 			 struct btrfs_root *root,
90098ed5174SChris Mason 			 struct btrfs_path *path, int level)
901bb803951SChris Mason {
9025f39d397SChris Mason 	struct extent_buffer *right = NULL;
9035f39d397SChris Mason 	struct extent_buffer *mid;
9045f39d397SChris Mason 	struct extent_buffer *left = NULL;
9055f39d397SChris Mason 	struct extent_buffer *parent = NULL;
906bb803951SChris Mason 	int ret = 0;
907bb803951SChris Mason 	int wret;
908bb803951SChris Mason 	int pslot;
909bb803951SChris Mason 	int orig_slot = path->slots[level];
91079f95c82SChris Mason 	u64 orig_ptr;
911bb803951SChris Mason 
912bb803951SChris Mason 	if (level == 0)
913bb803951SChris Mason 		return 0;
914bb803951SChris Mason 
9155f39d397SChris Mason 	mid = path->nodes[level];
916b4ce94deSChris Mason 
917bd681513SChris Mason 	WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
918bd681513SChris Mason 		path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
9197bb86316SChris Mason 	WARN_ON(btrfs_header_generation(mid) != trans->transid);
9207bb86316SChris Mason 
9211d4f8a0cSChris Mason 	orig_ptr = btrfs_node_blockptr(mid, orig_slot);
92279f95c82SChris Mason 
923a05a9bb1SLi Zefan 	if (level < BTRFS_MAX_LEVEL - 1) {
9245f39d397SChris Mason 		parent = path->nodes[level + 1];
925bb803951SChris Mason 		pslot = path->slots[level + 1];
926a05a9bb1SLi Zefan 	}
927bb803951SChris Mason 
92840689478SChris Mason 	/*
92940689478SChris Mason 	 * deal with the case where there is only one pointer in the root
93040689478SChris Mason 	 * by promoting the node below to a root
93140689478SChris Mason 	 */
9325f39d397SChris Mason 	if (!parent) {
9335f39d397SChris Mason 		struct extent_buffer *child;
934bb803951SChris Mason 
9355f39d397SChris Mason 		if (btrfs_header_nritems(mid) != 1)
936bb803951SChris Mason 			return 0;
937bb803951SChris Mason 
938bb803951SChris Mason 		/* promote the child to a root */
9395f39d397SChris Mason 		child = read_node_slot(root, mid, 0);
9407951f3ceSJeff Mahoney 		BUG_ON(!child);
941925baeddSChris Mason 		btrfs_tree_lock(child);
942b4ce94deSChris Mason 		btrfs_set_lock_blocking(child);
9439fa8cfe7SChris Mason 		ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
944f0486c68SYan, Zheng 		if (ret) {
945f0486c68SYan, Zheng 			btrfs_tree_unlock(child);
946f0486c68SYan, Zheng 			free_extent_buffer(child);
947f0486c68SYan, Zheng 			goto enospc;
948f0486c68SYan, Zheng 		}
9492f375ab9SYan 
950240f62c8SChris Mason 		rcu_assign_pointer(root->node, child);
951925baeddSChris Mason 
9520b86a832SChris Mason 		add_root_to_dirty_list(root);
953925baeddSChris Mason 		btrfs_tree_unlock(child);
954b4ce94deSChris Mason 
955925baeddSChris Mason 		path->locks[level] = 0;
956bb803951SChris Mason 		path->nodes[level] = NULL;
9575f39d397SChris Mason 		clean_tree_block(trans, root, mid);
958925baeddSChris Mason 		btrfs_tree_unlock(mid);
959bb803951SChris Mason 		/* once for the path */
9605f39d397SChris Mason 		free_extent_buffer(mid);
961f0486c68SYan, Zheng 
962f0486c68SYan, Zheng 		root_sub_used(root, mid->len);
96366d7e7f0SArne Jansen 		btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
964bb803951SChris Mason 		/* once for the root ptr */
9655f39d397SChris Mason 		free_extent_buffer(mid);
966f0486c68SYan, Zheng 		return 0;
967bb803951SChris Mason 	}
9685f39d397SChris Mason 	if (btrfs_header_nritems(mid) >
969123abc88SChris Mason 	    BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
970bb803951SChris Mason 		return 0;
971bb803951SChris Mason 
972559af821SAndi Kleen 	btrfs_header_nritems(mid);
97354aa1f4dSChris Mason 
9745f39d397SChris Mason 	left = read_node_slot(root, parent, pslot - 1);
9755f39d397SChris Mason 	if (left) {
976925baeddSChris Mason 		btrfs_tree_lock(left);
977b4ce94deSChris Mason 		btrfs_set_lock_blocking(left);
9785f39d397SChris Mason 		wret = btrfs_cow_block(trans, root, left,
9799fa8cfe7SChris Mason 				       parent, pslot - 1, &left);
98054aa1f4dSChris Mason 		if (wret) {
98154aa1f4dSChris Mason 			ret = wret;
98254aa1f4dSChris Mason 			goto enospc;
98354aa1f4dSChris Mason 		}
9842cc58cf2SChris Mason 	}
9855f39d397SChris Mason 	right = read_node_slot(root, parent, pslot + 1);
9865f39d397SChris Mason 	if (right) {
987925baeddSChris Mason 		btrfs_tree_lock(right);
988b4ce94deSChris Mason 		btrfs_set_lock_blocking(right);
9895f39d397SChris Mason 		wret = btrfs_cow_block(trans, root, right,
9909fa8cfe7SChris Mason 				       parent, pslot + 1, &right);
9912cc58cf2SChris Mason 		if (wret) {
9922cc58cf2SChris Mason 			ret = wret;
9932cc58cf2SChris Mason 			goto enospc;
9942cc58cf2SChris Mason 		}
9952cc58cf2SChris Mason 	}
9962cc58cf2SChris Mason 
9972cc58cf2SChris Mason 	/* first, try to make some room in the middle buffer */
9985f39d397SChris Mason 	if (left) {
9995f39d397SChris Mason 		orig_slot += btrfs_header_nritems(left);
1000bce4eae9SChris Mason 		wret = push_node_left(trans, root, left, mid, 1);
100179f95c82SChris Mason 		if (wret < 0)
100279f95c82SChris Mason 			ret = wret;
1003559af821SAndi Kleen 		btrfs_header_nritems(mid);
1004bb803951SChris Mason 	}
100579f95c82SChris Mason 
100679f95c82SChris Mason 	/*
100779f95c82SChris Mason 	 * then try to empty the right most buffer into the middle
100879f95c82SChris Mason 	 */
10095f39d397SChris Mason 	if (right) {
1010971a1f66SChris Mason 		wret = push_node_left(trans, root, mid, right, 1);
101154aa1f4dSChris Mason 		if (wret < 0 && wret != -ENOSPC)
101279f95c82SChris Mason 			ret = wret;
10135f39d397SChris Mason 		if (btrfs_header_nritems(right) == 0) {
10145f39d397SChris Mason 			clean_tree_block(trans, root, right);
1015925baeddSChris Mason 			btrfs_tree_unlock(right);
1016143bede5SJeff Mahoney 			del_ptr(trans, root, path, level + 1, pslot + 1);
1017f0486c68SYan, Zheng 			root_sub_used(root, right->len);
101866d7e7f0SArne Jansen 			btrfs_free_tree_block(trans, root, right, 0, 1, 0);
1019f0486c68SYan, Zheng 			free_extent_buffer(right);
1020f0486c68SYan, Zheng 			right = NULL;
1021bb803951SChris Mason 		} else {
10225f39d397SChris Mason 			struct btrfs_disk_key right_key;
10235f39d397SChris Mason 			btrfs_node_key(right, &right_key, 0);
10245f39d397SChris Mason 			btrfs_set_node_key(parent, &right_key, pslot + 1);
10255f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
1026bb803951SChris Mason 		}
1027bb803951SChris Mason 	}
10285f39d397SChris Mason 	if (btrfs_header_nritems(mid) == 1) {
102979f95c82SChris Mason 		/*
103079f95c82SChris Mason 		 * we're not allowed to leave a node with one item in the
103179f95c82SChris Mason 		 * tree during a delete.  A deletion from lower in the tree
103279f95c82SChris Mason 		 * could try to delete the only pointer in this node.
103379f95c82SChris Mason 		 * So, pull some keys from the left.
103479f95c82SChris Mason 		 * There has to be a left pointer at this point because
103579f95c82SChris Mason 		 * otherwise we would have pulled some pointers from the
103679f95c82SChris Mason 		 * right
103779f95c82SChris Mason 		 */
10385f39d397SChris Mason 		BUG_ON(!left);
10395f39d397SChris Mason 		wret = balance_node_right(trans, root, mid, left);
104054aa1f4dSChris Mason 		if (wret < 0) {
104179f95c82SChris Mason 			ret = wret;
104254aa1f4dSChris Mason 			goto enospc;
104354aa1f4dSChris Mason 		}
1044bce4eae9SChris Mason 		if (wret == 1) {
1045bce4eae9SChris Mason 			wret = push_node_left(trans, root, left, mid, 1);
1046bce4eae9SChris Mason 			if (wret < 0)
1047bce4eae9SChris Mason 				ret = wret;
1048bce4eae9SChris Mason 		}
104979f95c82SChris Mason 		BUG_ON(wret == 1);
105079f95c82SChris Mason 	}
10515f39d397SChris Mason 	if (btrfs_header_nritems(mid) == 0) {
10525f39d397SChris Mason 		clean_tree_block(trans, root, mid);
1053925baeddSChris Mason 		btrfs_tree_unlock(mid);
1054143bede5SJeff Mahoney 		del_ptr(trans, root, path, level + 1, pslot);
1055f0486c68SYan, Zheng 		root_sub_used(root, mid->len);
105666d7e7f0SArne Jansen 		btrfs_free_tree_block(trans, root, mid, 0, 1, 0);
1057f0486c68SYan, Zheng 		free_extent_buffer(mid);
1058f0486c68SYan, Zheng 		mid = NULL;
105979f95c82SChris Mason 	} else {
106079f95c82SChris Mason 		/* update the parent key to reflect our changes */
10615f39d397SChris Mason 		struct btrfs_disk_key mid_key;
10625f39d397SChris Mason 		btrfs_node_key(mid, &mid_key, 0);
10635f39d397SChris Mason 		btrfs_set_node_key(parent, &mid_key, pslot);
10645f39d397SChris Mason 		btrfs_mark_buffer_dirty(parent);
106579f95c82SChris Mason 	}
1066bb803951SChris Mason 
106779f95c82SChris Mason 	/* update the path */
10685f39d397SChris Mason 	if (left) {
10695f39d397SChris Mason 		if (btrfs_header_nritems(left) > orig_slot) {
10705f39d397SChris Mason 			extent_buffer_get(left);
1071925baeddSChris Mason 			/* left was locked after cow */
10725f39d397SChris Mason 			path->nodes[level] = left;
1073bb803951SChris Mason 			path->slots[level + 1] -= 1;
1074bb803951SChris Mason 			path->slots[level] = orig_slot;
1075925baeddSChris Mason 			if (mid) {
1076925baeddSChris Mason 				btrfs_tree_unlock(mid);
10775f39d397SChris Mason 				free_extent_buffer(mid);
1078925baeddSChris Mason 			}
1079bb803951SChris Mason 		} else {
10805f39d397SChris Mason 			orig_slot -= btrfs_header_nritems(left);
1081bb803951SChris Mason 			path->slots[level] = orig_slot;
1082bb803951SChris Mason 		}
1083bb803951SChris Mason 	}
108479f95c82SChris Mason 	/* double check we haven't messed things up */
1085e20d96d6SChris Mason 	if (orig_ptr !=
10865f39d397SChris Mason 	    btrfs_node_blockptr(path->nodes[level], path->slots[level]))
108779f95c82SChris Mason 		BUG();
108854aa1f4dSChris Mason enospc:
1089925baeddSChris Mason 	if (right) {
1090925baeddSChris Mason 		btrfs_tree_unlock(right);
10915f39d397SChris Mason 		free_extent_buffer(right);
1092925baeddSChris Mason 	}
1093925baeddSChris Mason 	if (left) {
1094925baeddSChris Mason 		if (path->nodes[level] != left)
1095925baeddSChris Mason 			btrfs_tree_unlock(left);
10965f39d397SChris Mason 		free_extent_buffer(left);
1097925baeddSChris Mason 	}
1098bb803951SChris Mason 	return ret;
1099bb803951SChris Mason }
1100bb803951SChris Mason 
1101d352ac68SChris Mason /* Node balancing for insertion.  Here we only split or push nodes around
1102d352ac68SChris Mason  * when they are completely full.  This is also done top down, so we
1103d352ac68SChris Mason  * have to be pessimistic.
1104d352ac68SChris Mason  */
1105d397712bSChris Mason static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1106e66f709bSChris Mason 					  struct btrfs_root *root,
1107e66f709bSChris Mason 					  struct btrfs_path *path, int level)
1108e66f709bSChris Mason {
11095f39d397SChris Mason 	struct extent_buffer *right = NULL;
11105f39d397SChris Mason 	struct extent_buffer *mid;
11115f39d397SChris Mason 	struct extent_buffer *left = NULL;
11125f39d397SChris Mason 	struct extent_buffer *parent = NULL;
1113e66f709bSChris Mason 	int ret = 0;
1114e66f709bSChris Mason 	int wret;
1115e66f709bSChris Mason 	int pslot;
1116e66f709bSChris Mason 	int orig_slot = path->slots[level];
1117e66f709bSChris Mason 
1118e66f709bSChris Mason 	if (level == 0)
1119e66f709bSChris Mason 		return 1;
1120e66f709bSChris Mason 
11215f39d397SChris Mason 	mid = path->nodes[level];
11227bb86316SChris Mason 	WARN_ON(btrfs_header_generation(mid) != trans->transid);
1123e66f709bSChris Mason 
1124a05a9bb1SLi Zefan 	if (level < BTRFS_MAX_LEVEL - 1) {
11255f39d397SChris Mason 		parent = path->nodes[level + 1];
1126e66f709bSChris Mason 		pslot = path->slots[level + 1];
1127a05a9bb1SLi Zefan 	}
1128e66f709bSChris Mason 
11295f39d397SChris Mason 	if (!parent)
1130e66f709bSChris Mason 		return 1;
1131e66f709bSChris Mason 
11325f39d397SChris Mason 	left = read_node_slot(root, parent, pslot - 1);
1133e66f709bSChris Mason 
1134e66f709bSChris Mason 	/* first, try to make some room in the middle buffer */
11355f39d397SChris Mason 	if (left) {
1136e66f709bSChris Mason 		u32 left_nr;
1137925baeddSChris Mason 
1138925baeddSChris Mason 		btrfs_tree_lock(left);
1139b4ce94deSChris Mason 		btrfs_set_lock_blocking(left);
1140b4ce94deSChris Mason 
11415f39d397SChris Mason 		left_nr = btrfs_header_nritems(left);
114233ade1f8SChris Mason 		if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
114333ade1f8SChris Mason 			wret = 1;
114433ade1f8SChris Mason 		} else {
11455f39d397SChris Mason 			ret = btrfs_cow_block(trans, root, left, parent,
11469fa8cfe7SChris Mason 					      pslot - 1, &left);
114754aa1f4dSChris Mason 			if (ret)
114854aa1f4dSChris Mason 				wret = 1;
114954aa1f4dSChris Mason 			else {
115054aa1f4dSChris Mason 				wret = push_node_left(trans, root,
1151971a1f66SChris Mason 						      left, mid, 0);
115254aa1f4dSChris Mason 			}
115333ade1f8SChris Mason 		}
1154e66f709bSChris Mason 		if (wret < 0)
1155e66f709bSChris Mason 			ret = wret;
1156e66f709bSChris Mason 		if (wret == 0) {
11575f39d397SChris Mason 			struct btrfs_disk_key disk_key;
1158e66f709bSChris Mason 			orig_slot += left_nr;
11595f39d397SChris Mason 			btrfs_node_key(mid, &disk_key, 0);
11605f39d397SChris Mason 			btrfs_set_node_key(parent, &disk_key, pslot);
11615f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
11625f39d397SChris Mason 			if (btrfs_header_nritems(left) > orig_slot) {
11635f39d397SChris Mason 				path->nodes[level] = left;
1164e66f709bSChris Mason 				path->slots[level + 1] -= 1;
1165e66f709bSChris Mason 				path->slots[level] = orig_slot;
1166925baeddSChris Mason 				btrfs_tree_unlock(mid);
11675f39d397SChris Mason 				free_extent_buffer(mid);
1168e66f709bSChris Mason 			} else {
1169e66f709bSChris Mason 				orig_slot -=
11705f39d397SChris Mason 					btrfs_header_nritems(left);
1171e66f709bSChris Mason 				path->slots[level] = orig_slot;
1172925baeddSChris Mason 				btrfs_tree_unlock(left);
11735f39d397SChris Mason 				free_extent_buffer(left);
1174e66f709bSChris Mason 			}
1175e66f709bSChris Mason 			return 0;
1176e66f709bSChris Mason 		}
1177925baeddSChris Mason 		btrfs_tree_unlock(left);
11785f39d397SChris Mason 		free_extent_buffer(left);
1179e66f709bSChris Mason 	}
11805f39d397SChris Mason 	right = read_node_slot(root, parent, pslot + 1);
1181e66f709bSChris Mason 
1182e66f709bSChris Mason 	/*
1183e66f709bSChris Mason 	 * then try to empty the right most buffer into the middle
1184e66f709bSChris Mason 	 */
11855f39d397SChris Mason 	if (right) {
118633ade1f8SChris Mason 		u32 right_nr;
1187b4ce94deSChris Mason 
1188925baeddSChris Mason 		btrfs_tree_lock(right);
1189b4ce94deSChris Mason 		btrfs_set_lock_blocking(right);
1190b4ce94deSChris Mason 
11915f39d397SChris Mason 		right_nr = btrfs_header_nritems(right);
119233ade1f8SChris Mason 		if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
119333ade1f8SChris Mason 			wret = 1;
119433ade1f8SChris Mason 		} else {
11955f39d397SChris Mason 			ret = btrfs_cow_block(trans, root, right,
11965f39d397SChris Mason 					      parent, pslot + 1,
11979fa8cfe7SChris Mason 					      &right);
119854aa1f4dSChris Mason 			if (ret)
119954aa1f4dSChris Mason 				wret = 1;
120054aa1f4dSChris Mason 			else {
120133ade1f8SChris Mason 				wret = balance_node_right(trans, root,
12025f39d397SChris Mason 							  right, mid);
120333ade1f8SChris Mason 			}
120454aa1f4dSChris Mason 		}
1205e66f709bSChris Mason 		if (wret < 0)
1206e66f709bSChris Mason 			ret = wret;
1207e66f709bSChris Mason 		if (wret == 0) {
12085f39d397SChris Mason 			struct btrfs_disk_key disk_key;
12095f39d397SChris Mason 
12105f39d397SChris Mason 			btrfs_node_key(right, &disk_key, 0);
12115f39d397SChris Mason 			btrfs_set_node_key(parent, &disk_key, pslot + 1);
12125f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
12135f39d397SChris Mason 
12145f39d397SChris Mason 			if (btrfs_header_nritems(mid) <= orig_slot) {
12155f39d397SChris Mason 				path->nodes[level] = right;
1216e66f709bSChris Mason 				path->slots[level + 1] += 1;
1217e66f709bSChris Mason 				path->slots[level] = orig_slot -
12185f39d397SChris Mason 					btrfs_header_nritems(mid);
1219925baeddSChris Mason 				btrfs_tree_unlock(mid);
12205f39d397SChris Mason 				free_extent_buffer(mid);
1221e66f709bSChris Mason 			} else {
1222925baeddSChris Mason 				btrfs_tree_unlock(right);
12235f39d397SChris Mason 				free_extent_buffer(right);
1224e66f709bSChris Mason 			}
1225e66f709bSChris Mason 			return 0;
1226e66f709bSChris Mason 		}
1227925baeddSChris Mason 		btrfs_tree_unlock(right);
12285f39d397SChris Mason 		free_extent_buffer(right);
1229e66f709bSChris Mason 	}
1230e66f709bSChris Mason 	return 1;
1231e66f709bSChris Mason }
1232e66f709bSChris Mason 
123374123bd7SChris Mason /*
1234d352ac68SChris Mason  * readahead one full node of leaves, finding things that are close
1235d352ac68SChris Mason  * to the block in 'slot', and triggering ra on them.
12363c69faecSChris Mason  */
1237c8c42864SChris Mason static void reada_for_search(struct btrfs_root *root,
1238e02119d5SChris Mason 			     struct btrfs_path *path,
123901f46658SChris Mason 			     int level, int slot, u64 objectid)
12403c69faecSChris Mason {
12415f39d397SChris Mason 	struct extent_buffer *node;
124201f46658SChris Mason 	struct btrfs_disk_key disk_key;
12433c69faecSChris Mason 	u32 nritems;
12443c69faecSChris Mason 	u64 search;
1245a7175319SChris Mason 	u64 target;
12466b80053dSChris Mason 	u64 nread = 0;
1247cb25c2eaSJosef Bacik 	u64 gen;
12483c69faecSChris Mason 	int direction = path->reada;
12495f39d397SChris Mason 	struct extent_buffer *eb;
12506b80053dSChris Mason 	u32 nr;
12516b80053dSChris Mason 	u32 blocksize;
12526b80053dSChris Mason 	u32 nscan = 0;
1253db94535dSChris Mason 
1254a6b6e75eSChris Mason 	if (level != 1)
12553c69faecSChris Mason 		return;
12563c69faecSChris Mason 
12576702ed49SChris Mason 	if (!path->nodes[level])
12586702ed49SChris Mason 		return;
12596702ed49SChris Mason 
12605f39d397SChris Mason 	node = path->nodes[level];
1261925baeddSChris Mason 
12623c69faecSChris Mason 	search = btrfs_node_blockptr(node, slot);
12636b80053dSChris Mason 	blocksize = btrfs_level_size(root, level - 1);
12646b80053dSChris Mason 	eb = btrfs_find_tree_block(root, search, blocksize);
12655f39d397SChris Mason 	if (eb) {
12665f39d397SChris Mason 		free_extent_buffer(eb);
12673c69faecSChris Mason 		return;
12683c69faecSChris Mason 	}
12693c69faecSChris Mason 
1270a7175319SChris Mason 	target = search;
12716b80053dSChris Mason 
12725f39d397SChris Mason 	nritems = btrfs_header_nritems(node);
12736b80053dSChris Mason 	nr = slot;
127425b8b936SJosef Bacik 
12753c69faecSChris Mason 	while (1) {
12766b80053dSChris Mason 		if (direction < 0) {
12776b80053dSChris Mason 			if (nr == 0)
12783c69faecSChris Mason 				break;
12796b80053dSChris Mason 			nr--;
12806b80053dSChris Mason 		} else if (direction > 0) {
12816b80053dSChris Mason 			nr++;
12826b80053dSChris Mason 			if (nr >= nritems)
12836b80053dSChris Mason 				break;
12843c69faecSChris Mason 		}
128501f46658SChris Mason 		if (path->reada < 0 && objectid) {
128601f46658SChris Mason 			btrfs_node_key(node, &disk_key, nr);
128701f46658SChris Mason 			if (btrfs_disk_key_objectid(&disk_key) != objectid)
128801f46658SChris Mason 				break;
128901f46658SChris Mason 		}
12906b80053dSChris Mason 		search = btrfs_node_blockptr(node, nr);
1291a7175319SChris Mason 		if ((search <= target && target - search <= 65536) ||
1292a7175319SChris Mason 		    (search > target && search - target <= 65536)) {
1293cb25c2eaSJosef Bacik 			gen = btrfs_node_ptr_generation(node, nr);
1294cb25c2eaSJosef Bacik 			readahead_tree_block(root, search, blocksize, gen);
12956b80053dSChris Mason 			nread += blocksize;
12963c69faecSChris Mason 		}
12976b80053dSChris Mason 		nscan++;
1298a7175319SChris Mason 		if ((nread > 65536 || nscan > 32))
12996b80053dSChris Mason 			break;
13003c69faecSChris Mason 	}
13013c69faecSChris Mason }
1302925baeddSChris Mason 
1303d352ac68SChris Mason /*
1304b4ce94deSChris Mason  * returns -EAGAIN if it had to drop the path, or zero if everything was in
1305b4ce94deSChris Mason  * cache
1306b4ce94deSChris Mason  */
1307b4ce94deSChris Mason static noinline int reada_for_balance(struct btrfs_root *root,
1308b4ce94deSChris Mason 				      struct btrfs_path *path, int level)
1309b4ce94deSChris Mason {
1310b4ce94deSChris Mason 	int slot;
1311b4ce94deSChris Mason 	int nritems;
1312b4ce94deSChris Mason 	struct extent_buffer *parent;
1313b4ce94deSChris Mason 	struct extent_buffer *eb;
1314b4ce94deSChris Mason 	u64 gen;
1315b4ce94deSChris Mason 	u64 block1 = 0;
1316b4ce94deSChris Mason 	u64 block2 = 0;
1317b4ce94deSChris Mason 	int ret = 0;
1318b4ce94deSChris Mason 	int blocksize;
1319b4ce94deSChris Mason 
13208c594ea8SChris Mason 	parent = path->nodes[level + 1];
1321b4ce94deSChris Mason 	if (!parent)
1322b4ce94deSChris Mason 		return 0;
1323b4ce94deSChris Mason 
1324b4ce94deSChris Mason 	nritems = btrfs_header_nritems(parent);
13258c594ea8SChris Mason 	slot = path->slots[level + 1];
1326b4ce94deSChris Mason 	blocksize = btrfs_level_size(root, level);
1327b4ce94deSChris Mason 
1328b4ce94deSChris Mason 	if (slot > 0) {
1329b4ce94deSChris Mason 		block1 = btrfs_node_blockptr(parent, slot - 1);
1330b4ce94deSChris Mason 		gen = btrfs_node_ptr_generation(parent, slot - 1);
1331b4ce94deSChris Mason 		eb = btrfs_find_tree_block(root, block1, blocksize);
1332b4ce94deSChris Mason 		if (eb && btrfs_buffer_uptodate(eb, gen))
1333b4ce94deSChris Mason 			block1 = 0;
1334b4ce94deSChris Mason 		free_extent_buffer(eb);
1335b4ce94deSChris Mason 	}
13368c594ea8SChris Mason 	if (slot + 1 < nritems) {
1337b4ce94deSChris Mason 		block2 = btrfs_node_blockptr(parent, slot + 1);
1338b4ce94deSChris Mason 		gen = btrfs_node_ptr_generation(parent, slot + 1);
1339b4ce94deSChris Mason 		eb = btrfs_find_tree_block(root, block2, blocksize);
1340b4ce94deSChris Mason 		if (eb && btrfs_buffer_uptodate(eb, gen))
1341b4ce94deSChris Mason 			block2 = 0;
1342b4ce94deSChris Mason 		free_extent_buffer(eb);
1343b4ce94deSChris Mason 	}
1344b4ce94deSChris Mason 	if (block1 || block2) {
1345b4ce94deSChris Mason 		ret = -EAGAIN;
13468c594ea8SChris Mason 
13478c594ea8SChris Mason 		/* release the whole path */
1348b3b4aa74SDavid Sterba 		btrfs_release_path(path);
13498c594ea8SChris Mason 
13508c594ea8SChris Mason 		/* read the blocks */
1351b4ce94deSChris Mason 		if (block1)
1352b4ce94deSChris Mason 			readahead_tree_block(root, block1, blocksize, 0);
1353b4ce94deSChris Mason 		if (block2)
1354b4ce94deSChris Mason 			readahead_tree_block(root, block2, blocksize, 0);
1355b4ce94deSChris Mason 
1356b4ce94deSChris Mason 		if (block1) {
1357b4ce94deSChris Mason 			eb = read_tree_block(root, block1, blocksize, 0);
1358b4ce94deSChris Mason 			free_extent_buffer(eb);
1359b4ce94deSChris Mason 		}
13608c594ea8SChris Mason 		if (block2) {
1361b4ce94deSChris Mason 			eb = read_tree_block(root, block2, blocksize, 0);
1362b4ce94deSChris Mason 			free_extent_buffer(eb);
1363b4ce94deSChris Mason 		}
1364b4ce94deSChris Mason 	}
1365b4ce94deSChris Mason 	return ret;
1366b4ce94deSChris Mason }
1367b4ce94deSChris Mason 
1368b4ce94deSChris Mason 
1369b4ce94deSChris Mason /*
1370d397712bSChris Mason  * when we walk down the tree, it is usually safe to unlock the higher layers
1371d397712bSChris Mason  * in the tree.  The exceptions are when our path goes through slot 0, because
1372d397712bSChris Mason  * operations on the tree might require changing key pointers higher up in the
1373d397712bSChris Mason  * tree.
1374d352ac68SChris Mason  *
1375d397712bSChris Mason  * callers might also have set path->keep_locks, which tells this code to keep
1376d397712bSChris Mason  * the lock if the path points to the last slot in the block.  This is part of
1377d397712bSChris Mason  * walking through the tree, and selecting the next slot in the higher block.
1378d352ac68SChris Mason  *
1379d397712bSChris Mason  * lowest_unlock sets the lowest level in the tree we're allowed to unlock.  so
1380d397712bSChris Mason  * if lowest_unlock is 1, level 0 won't be unlocked
1381d352ac68SChris Mason  */
1382e02119d5SChris Mason static noinline void unlock_up(struct btrfs_path *path, int level,
1383e02119d5SChris Mason 			       int lowest_unlock)
1384925baeddSChris Mason {
1385925baeddSChris Mason 	int i;
1386925baeddSChris Mason 	int skip_level = level;
1387051e1b9fSChris Mason 	int no_skips = 0;
1388925baeddSChris Mason 	struct extent_buffer *t;
1389925baeddSChris Mason 
1390925baeddSChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1391925baeddSChris Mason 		if (!path->nodes[i])
1392925baeddSChris Mason 			break;
1393925baeddSChris Mason 		if (!path->locks[i])
1394925baeddSChris Mason 			break;
1395051e1b9fSChris Mason 		if (!no_skips && path->slots[i] == 0) {
1396925baeddSChris Mason 			skip_level = i + 1;
1397925baeddSChris Mason 			continue;
1398925baeddSChris Mason 		}
1399051e1b9fSChris Mason 		if (!no_skips && path->keep_locks) {
1400925baeddSChris Mason 			u32 nritems;
1401925baeddSChris Mason 			t = path->nodes[i];
1402925baeddSChris Mason 			nritems = btrfs_header_nritems(t);
1403051e1b9fSChris Mason 			if (nritems < 1 || path->slots[i] >= nritems - 1) {
1404925baeddSChris Mason 				skip_level = i + 1;
1405925baeddSChris Mason 				continue;
1406925baeddSChris Mason 			}
1407925baeddSChris Mason 		}
1408051e1b9fSChris Mason 		if (skip_level < i && i >= lowest_unlock)
1409051e1b9fSChris Mason 			no_skips = 1;
1410051e1b9fSChris Mason 
1411925baeddSChris Mason 		t = path->nodes[i];
1412925baeddSChris Mason 		if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1413bd681513SChris Mason 			btrfs_tree_unlock_rw(t, path->locks[i]);
1414925baeddSChris Mason 			path->locks[i] = 0;
1415925baeddSChris Mason 		}
1416925baeddSChris Mason 	}
1417925baeddSChris Mason }
1418925baeddSChris Mason 
14193c69faecSChris Mason /*
1420b4ce94deSChris Mason  * This releases any locks held in the path starting at level and
1421b4ce94deSChris Mason  * going all the way up to the root.
1422b4ce94deSChris Mason  *
1423b4ce94deSChris Mason  * btrfs_search_slot will keep the lock held on higher nodes in a few
1424b4ce94deSChris Mason  * corner cases, such as COW of the block at slot zero in the node.  This
1425b4ce94deSChris Mason  * ignores those rules, and it should only be called when there are no
1426b4ce94deSChris Mason  * more updates to be done higher up in the tree.
1427b4ce94deSChris Mason  */
1428b4ce94deSChris Mason noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1429b4ce94deSChris Mason {
1430b4ce94deSChris Mason 	int i;
1431b4ce94deSChris Mason 
14325d4f98a2SYan Zheng 	if (path->keep_locks)
1433b4ce94deSChris Mason 		return;
1434b4ce94deSChris Mason 
1435b4ce94deSChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1436b4ce94deSChris Mason 		if (!path->nodes[i])
143712f4daccSChris Mason 			continue;
1438b4ce94deSChris Mason 		if (!path->locks[i])
143912f4daccSChris Mason 			continue;
1440bd681513SChris Mason 		btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
1441b4ce94deSChris Mason 		path->locks[i] = 0;
1442b4ce94deSChris Mason 	}
1443b4ce94deSChris Mason }
1444b4ce94deSChris Mason 
1445b4ce94deSChris Mason /*
1446c8c42864SChris Mason  * helper function for btrfs_search_slot.  The goal is to find a block
1447c8c42864SChris Mason  * in cache without setting the path to blocking.  If we find the block
1448c8c42864SChris Mason  * we return zero and the path is unchanged.
1449c8c42864SChris Mason  *
1450c8c42864SChris Mason  * If we can't find the block, we set the path blocking and do some
1451c8c42864SChris Mason  * reada.  -EAGAIN is returned and the search must be repeated.
1452c8c42864SChris Mason  */
1453c8c42864SChris Mason static int
1454c8c42864SChris Mason read_block_for_search(struct btrfs_trans_handle *trans,
1455c8c42864SChris Mason 		       struct btrfs_root *root, struct btrfs_path *p,
1456c8c42864SChris Mason 		       struct extent_buffer **eb_ret, int level, int slot,
1457c8c42864SChris Mason 		       struct btrfs_key *key)
1458c8c42864SChris Mason {
1459c8c42864SChris Mason 	u64 blocknr;
1460c8c42864SChris Mason 	u64 gen;
1461c8c42864SChris Mason 	u32 blocksize;
1462c8c42864SChris Mason 	struct extent_buffer *b = *eb_ret;
1463c8c42864SChris Mason 	struct extent_buffer *tmp;
146476a05b35SChris Mason 	int ret;
1465c8c42864SChris Mason 
1466c8c42864SChris Mason 	blocknr = btrfs_node_blockptr(b, slot);
1467c8c42864SChris Mason 	gen = btrfs_node_ptr_generation(b, slot);
1468c8c42864SChris Mason 	blocksize = btrfs_level_size(root, level - 1);
1469c8c42864SChris Mason 
1470c8c42864SChris Mason 	tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1471cb44921aSChris Mason 	if (tmp) {
1472cb44921aSChris Mason 		if (btrfs_buffer_uptodate(tmp, 0)) {
1473cb44921aSChris Mason 			if (btrfs_buffer_uptodate(tmp, gen)) {
147476a05b35SChris Mason 				/*
1475cb44921aSChris Mason 				 * we found an up to date block without
1476cb44921aSChris Mason 				 * sleeping, return
147776a05b35SChris Mason 				 * right away
147876a05b35SChris Mason 				 */
1479c8c42864SChris Mason 				*eb_ret = tmp;
1480c8c42864SChris Mason 				return 0;
1481c8c42864SChris Mason 			}
1482cb44921aSChris Mason 			/* the pages were up to date, but we failed
1483cb44921aSChris Mason 			 * the generation number check.  Do a full
1484cb44921aSChris Mason 			 * read for the generation number that is correct.
1485cb44921aSChris Mason 			 * We must do this without dropping locks so
1486cb44921aSChris Mason 			 * we can trust our generation number
1487cb44921aSChris Mason 			 */
1488cb44921aSChris Mason 			free_extent_buffer(tmp);
1489bd681513SChris Mason 			btrfs_set_path_blocking(p);
1490bd681513SChris Mason 
1491cb44921aSChris Mason 			tmp = read_tree_block(root, blocknr, blocksize, gen);
1492cb44921aSChris Mason 			if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1493cb44921aSChris Mason 				*eb_ret = tmp;
1494cb44921aSChris Mason 				return 0;
1495cb44921aSChris Mason 			}
1496cb44921aSChris Mason 			free_extent_buffer(tmp);
1497b3b4aa74SDavid Sterba 			btrfs_release_path(p);
1498cb44921aSChris Mason 			return -EIO;
1499cb44921aSChris Mason 		}
1500cb44921aSChris Mason 	}
1501c8c42864SChris Mason 
1502c8c42864SChris Mason 	/*
1503c8c42864SChris Mason 	 * reduce lock contention at high levels
1504c8c42864SChris Mason 	 * of the btree by dropping locks before
150576a05b35SChris Mason 	 * we read.  Don't release the lock on the current
150676a05b35SChris Mason 	 * level because we need to walk this node to figure
150776a05b35SChris Mason 	 * out which blocks to read.
1508c8c42864SChris Mason 	 */
15098c594ea8SChris Mason 	btrfs_unlock_up_safe(p, level + 1);
15108c594ea8SChris Mason 	btrfs_set_path_blocking(p);
15118c594ea8SChris Mason 
1512c8c42864SChris Mason 	free_extent_buffer(tmp);
1513c8c42864SChris Mason 	if (p->reada)
1514c8c42864SChris Mason 		reada_for_search(root, p, level, slot, key->objectid);
1515c8c42864SChris Mason 
1516b3b4aa74SDavid Sterba 	btrfs_release_path(p);
151776a05b35SChris Mason 
151876a05b35SChris Mason 	ret = -EAGAIN;
15195bdd3536SYan, Zheng 	tmp = read_tree_block(root, blocknr, blocksize, 0);
152076a05b35SChris Mason 	if (tmp) {
152176a05b35SChris Mason 		/*
152276a05b35SChris Mason 		 * If the read above didn't mark this buffer up to date,
152376a05b35SChris Mason 		 * it will never end up being up to date.  Set ret to EIO now
152476a05b35SChris Mason 		 * and give up so that our caller doesn't loop forever
152576a05b35SChris Mason 		 * on our EAGAINs.
152676a05b35SChris Mason 		 */
152776a05b35SChris Mason 		if (!btrfs_buffer_uptodate(tmp, 0))
152876a05b35SChris Mason 			ret = -EIO;
1529c8c42864SChris Mason 		free_extent_buffer(tmp);
153076a05b35SChris Mason 	}
153176a05b35SChris Mason 	return ret;
1532c8c42864SChris Mason }
1533c8c42864SChris Mason 
1534c8c42864SChris Mason /*
1535c8c42864SChris Mason  * helper function for btrfs_search_slot.  This does all of the checks
1536c8c42864SChris Mason  * for node-level blocks and does any balancing required based on
1537c8c42864SChris Mason  * the ins_len.
1538c8c42864SChris Mason  *
1539c8c42864SChris Mason  * If no extra work was required, zero is returned.  If we had to
1540c8c42864SChris Mason  * drop the path, -EAGAIN is returned and btrfs_search_slot must
1541c8c42864SChris Mason  * start over
1542c8c42864SChris Mason  */
1543c8c42864SChris Mason static int
1544c8c42864SChris Mason setup_nodes_for_search(struct btrfs_trans_handle *trans,
1545c8c42864SChris Mason 		       struct btrfs_root *root, struct btrfs_path *p,
1546bd681513SChris Mason 		       struct extent_buffer *b, int level, int ins_len,
1547bd681513SChris Mason 		       int *write_lock_level)
1548c8c42864SChris Mason {
1549c8c42864SChris Mason 	int ret;
1550c8c42864SChris Mason 	if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1551c8c42864SChris Mason 	    BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1552c8c42864SChris Mason 		int sret;
1553c8c42864SChris Mason 
1554bd681513SChris Mason 		if (*write_lock_level < level + 1) {
1555bd681513SChris Mason 			*write_lock_level = level + 1;
1556bd681513SChris Mason 			btrfs_release_path(p);
1557bd681513SChris Mason 			goto again;
1558bd681513SChris Mason 		}
1559bd681513SChris Mason 
1560c8c42864SChris Mason 		sret = reada_for_balance(root, p, level);
1561c8c42864SChris Mason 		if (sret)
1562c8c42864SChris Mason 			goto again;
1563c8c42864SChris Mason 
1564c8c42864SChris Mason 		btrfs_set_path_blocking(p);
1565c8c42864SChris Mason 		sret = split_node(trans, root, p, level);
1566bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
1567c8c42864SChris Mason 
1568c8c42864SChris Mason 		BUG_ON(sret > 0);
1569c8c42864SChris Mason 		if (sret) {
1570c8c42864SChris Mason 			ret = sret;
1571c8c42864SChris Mason 			goto done;
1572c8c42864SChris Mason 		}
1573c8c42864SChris Mason 		b = p->nodes[level];
1574c8c42864SChris Mason 	} else if (ins_len < 0 && btrfs_header_nritems(b) <
1575cfbb9308SChris Mason 		   BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
1576c8c42864SChris Mason 		int sret;
1577c8c42864SChris Mason 
1578bd681513SChris Mason 		if (*write_lock_level < level + 1) {
1579bd681513SChris Mason 			*write_lock_level = level + 1;
1580bd681513SChris Mason 			btrfs_release_path(p);
1581bd681513SChris Mason 			goto again;
1582bd681513SChris Mason 		}
1583bd681513SChris Mason 
1584c8c42864SChris Mason 		sret = reada_for_balance(root, p, level);
1585c8c42864SChris Mason 		if (sret)
1586c8c42864SChris Mason 			goto again;
1587c8c42864SChris Mason 
1588c8c42864SChris Mason 		btrfs_set_path_blocking(p);
1589c8c42864SChris Mason 		sret = balance_level(trans, root, p, level);
1590bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
1591c8c42864SChris Mason 
1592c8c42864SChris Mason 		if (sret) {
1593c8c42864SChris Mason 			ret = sret;
1594c8c42864SChris Mason 			goto done;
1595c8c42864SChris Mason 		}
1596c8c42864SChris Mason 		b = p->nodes[level];
1597c8c42864SChris Mason 		if (!b) {
1598b3b4aa74SDavid Sterba 			btrfs_release_path(p);
1599c8c42864SChris Mason 			goto again;
1600c8c42864SChris Mason 		}
1601c8c42864SChris Mason 		BUG_ON(btrfs_header_nritems(b) == 1);
1602c8c42864SChris Mason 	}
1603c8c42864SChris Mason 	return 0;
1604c8c42864SChris Mason 
1605c8c42864SChris Mason again:
1606c8c42864SChris Mason 	ret = -EAGAIN;
1607c8c42864SChris Mason done:
1608c8c42864SChris Mason 	return ret;
1609c8c42864SChris Mason }
1610c8c42864SChris Mason 
1611c8c42864SChris Mason /*
161274123bd7SChris Mason  * look for key in the tree.  path is filled in with nodes along the way
161374123bd7SChris Mason  * if key is found, we return zero and you can find the item in the leaf
161474123bd7SChris Mason  * level of the path (level 0)
161574123bd7SChris Mason  *
161674123bd7SChris Mason  * If the key isn't found, the path points to the slot where it should
1617aa5d6bedSChris Mason  * be inserted, and 1 is returned.  If there are other errors during the
1618aa5d6bedSChris Mason  * search a negative error number is returned.
161997571fd0SChris Mason  *
162097571fd0SChris Mason  * if ins_len > 0, nodes and leaves will be split as we walk down the
162197571fd0SChris Mason  * tree.  if ins_len < 0, nodes will be merged as we walk down the tree (if
162297571fd0SChris Mason  * possible)
162374123bd7SChris Mason  */
1624e089f05cSChris Mason int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1625e089f05cSChris Mason 		      *root, struct btrfs_key *key, struct btrfs_path *p, int
1626e089f05cSChris Mason 		      ins_len, int cow)
1627be0e5c09SChris Mason {
16285f39d397SChris Mason 	struct extent_buffer *b;
1629be0e5c09SChris Mason 	int slot;
1630be0e5c09SChris Mason 	int ret;
163133c66f43SYan Zheng 	int err;
1632be0e5c09SChris Mason 	int level;
1633925baeddSChris Mason 	int lowest_unlock = 1;
1634bd681513SChris Mason 	int root_lock;
1635bd681513SChris Mason 	/* everything at write_lock_level or lower must be write locked */
1636bd681513SChris Mason 	int write_lock_level = 0;
16379f3a7427SChris Mason 	u8 lowest_level = 0;
16389f3a7427SChris Mason 
16396702ed49SChris Mason 	lowest_level = p->lowest_level;
1640323ac95bSChris Mason 	WARN_ON(lowest_level && ins_len > 0);
164122b0ebdaSChris Mason 	WARN_ON(p->nodes[0] != NULL);
164225179201SJosef Bacik 
1643bd681513SChris Mason 	if (ins_len < 0) {
1644925baeddSChris Mason 		lowest_unlock = 2;
164565b51a00SChris Mason 
1646bd681513SChris Mason 		/* when we are removing items, we might have to go up to level
1647bd681513SChris Mason 		 * two as we update tree pointers  Make sure we keep write
1648bd681513SChris Mason 		 * for those levels as well
1649bd681513SChris Mason 		 */
1650bd681513SChris Mason 		write_lock_level = 2;
1651bd681513SChris Mason 	} else if (ins_len > 0) {
1652bd681513SChris Mason 		/*
1653bd681513SChris Mason 		 * for inserting items, make sure we have a write lock on
1654bd681513SChris Mason 		 * level 1 so we can update keys
1655bd681513SChris Mason 		 */
1656bd681513SChris Mason 		write_lock_level = 1;
1657bd681513SChris Mason 	}
1658bd681513SChris Mason 
1659bd681513SChris Mason 	if (!cow)
1660bd681513SChris Mason 		write_lock_level = -1;
1661bd681513SChris Mason 
1662bd681513SChris Mason 	if (cow && (p->keep_locks || p->lowest_level))
1663bd681513SChris Mason 		write_lock_level = BTRFS_MAX_LEVEL;
1664bd681513SChris Mason 
1665bb803951SChris Mason again:
1666bd681513SChris Mason 	/*
1667bd681513SChris Mason 	 * we try very hard to do read locks on the root
1668bd681513SChris Mason 	 */
1669bd681513SChris Mason 	root_lock = BTRFS_READ_LOCK;
1670bd681513SChris Mason 	level = 0;
16715d4f98a2SYan Zheng 	if (p->search_commit_root) {
1672bd681513SChris Mason 		/*
1673bd681513SChris Mason 		 * the commit roots are read only
1674bd681513SChris Mason 		 * so we always do read locks
1675bd681513SChris Mason 		 */
16765d4f98a2SYan Zheng 		b = root->commit_root;
16775d4f98a2SYan Zheng 		extent_buffer_get(b);
1678bd681513SChris Mason 		level = btrfs_header_level(b);
16795d4f98a2SYan Zheng 		if (!p->skip_locking)
1680bd681513SChris Mason 			btrfs_tree_read_lock(b);
16815d4f98a2SYan Zheng 	} else {
1682bd681513SChris Mason 		if (p->skip_locking) {
16835cd57b2cSChris Mason 			b = btrfs_root_node(root);
1684bd681513SChris Mason 			level = btrfs_header_level(b);
1685bd681513SChris Mason 		} else {
1686bd681513SChris Mason 			/* we don't know the level of the root node
1687bd681513SChris Mason 			 * until we actually have it read locked
1688bd681513SChris Mason 			 */
1689bd681513SChris Mason 			b = btrfs_read_lock_root_node(root);
1690bd681513SChris Mason 			level = btrfs_header_level(b);
1691bd681513SChris Mason 			if (level <= write_lock_level) {
1692bd681513SChris Mason 				/* whoops, must trade for write lock */
1693bd681513SChris Mason 				btrfs_tree_read_unlock(b);
1694bd681513SChris Mason 				free_extent_buffer(b);
1695925baeddSChris Mason 				b = btrfs_lock_root_node(root);
1696bd681513SChris Mason 				root_lock = BTRFS_WRITE_LOCK;
1697bd681513SChris Mason 
1698bd681513SChris Mason 				/* the level might have changed, check again */
1699bd681513SChris Mason 				level = btrfs_header_level(b);
17005d4f98a2SYan Zheng 			}
1701bd681513SChris Mason 		}
1702bd681513SChris Mason 	}
1703bd681513SChris Mason 	p->nodes[level] = b;
1704bd681513SChris Mason 	if (!p->skip_locking)
1705bd681513SChris Mason 		p->locks[level] = root_lock;
1706925baeddSChris Mason 
1707eb60ceacSChris Mason 	while (b) {
17085f39d397SChris Mason 		level = btrfs_header_level(b);
170965b51a00SChris Mason 
171065b51a00SChris Mason 		/*
171165b51a00SChris Mason 		 * setup the path here so we can release it under lock
171265b51a00SChris Mason 		 * contention with the cow code
171365b51a00SChris Mason 		 */
171402217ed2SChris Mason 		if (cow) {
1715c8c42864SChris Mason 			/*
1716c8c42864SChris Mason 			 * if we don't really need to cow this block
1717c8c42864SChris Mason 			 * then we don't want to set the path blocking,
1718c8c42864SChris Mason 			 * so we test it here
1719c8c42864SChris Mason 			 */
17205d4f98a2SYan Zheng 			if (!should_cow_block(trans, root, b))
172165b51a00SChris Mason 				goto cow_done;
17225d4f98a2SYan Zheng 
1723b4ce94deSChris Mason 			btrfs_set_path_blocking(p);
1724b4ce94deSChris Mason 
1725bd681513SChris Mason 			/*
1726bd681513SChris Mason 			 * must have write locks on this node and the
1727bd681513SChris Mason 			 * parent
1728bd681513SChris Mason 			 */
1729bd681513SChris Mason 			if (level + 1 > write_lock_level) {
1730bd681513SChris Mason 				write_lock_level = level + 1;
1731bd681513SChris Mason 				btrfs_release_path(p);
1732bd681513SChris Mason 				goto again;
1733bd681513SChris Mason 			}
1734bd681513SChris Mason 
173533c66f43SYan Zheng 			err = btrfs_cow_block(trans, root, b,
1736e20d96d6SChris Mason 					      p->nodes[level + 1],
17379fa8cfe7SChris Mason 					      p->slots[level + 1], &b);
173833c66f43SYan Zheng 			if (err) {
173933c66f43SYan Zheng 				ret = err;
174065b51a00SChris Mason 				goto done;
174154aa1f4dSChris Mason 			}
174202217ed2SChris Mason 		}
174365b51a00SChris Mason cow_done:
174402217ed2SChris Mason 		BUG_ON(!cow && ins_len);
174565b51a00SChris Mason 
1746eb60ceacSChris Mason 		p->nodes[level] = b;
1747bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
1748b4ce94deSChris Mason 
1749b4ce94deSChris Mason 		/*
1750b4ce94deSChris Mason 		 * we have a lock on b and as long as we aren't changing
1751b4ce94deSChris Mason 		 * the tree, there is no way to for the items in b to change.
1752b4ce94deSChris Mason 		 * It is safe to drop the lock on our parent before we
1753b4ce94deSChris Mason 		 * go through the expensive btree search on b.
1754b4ce94deSChris Mason 		 *
1755b4ce94deSChris Mason 		 * If cow is true, then we might be changing slot zero,
1756b4ce94deSChris Mason 		 * which may require changing the parent.  So, we can't
1757b4ce94deSChris Mason 		 * drop the lock until after we know which slot we're
1758b4ce94deSChris Mason 		 * operating on.
1759b4ce94deSChris Mason 		 */
1760b4ce94deSChris Mason 		if (!cow)
1761b4ce94deSChris Mason 			btrfs_unlock_up_safe(p, level + 1);
1762b4ce94deSChris Mason 
17635f39d397SChris Mason 		ret = bin_search(b, key, level, &slot);
1764b4ce94deSChris Mason 
17655f39d397SChris Mason 		if (level != 0) {
176633c66f43SYan Zheng 			int dec = 0;
176733c66f43SYan Zheng 			if (ret && slot > 0) {
176833c66f43SYan Zheng 				dec = 1;
1769be0e5c09SChris Mason 				slot -= 1;
177033c66f43SYan Zheng 			}
1771be0e5c09SChris Mason 			p->slots[level] = slot;
177233c66f43SYan Zheng 			err = setup_nodes_for_search(trans, root, p, b, level,
1773bd681513SChris Mason 					     ins_len, &write_lock_level);
177433c66f43SYan Zheng 			if (err == -EAGAIN)
1775b4ce94deSChris Mason 				goto again;
177633c66f43SYan Zheng 			if (err) {
177733c66f43SYan Zheng 				ret = err;
177865b51a00SChris Mason 				goto done;
177933c66f43SYan Zheng 			}
17805c680ed6SChris Mason 			b = p->nodes[level];
17815c680ed6SChris Mason 			slot = p->slots[level];
1782b4ce94deSChris Mason 
1783bd681513SChris Mason 			/*
1784bd681513SChris Mason 			 * slot 0 is special, if we change the key
1785bd681513SChris Mason 			 * we have to update the parent pointer
1786bd681513SChris Mason 			 * which means we must have a write lock
1787bd681513SChris Mason 			 * on the parent
1788bd681513SChris Mason 			 */
1789bd681513SChris Mason 			if (slot == 0 && cow &&
1790bd681513SChris Mason 			    write_lock_level < level + 1) {
1791bd681513SChris Mason 				write_lock_level = level + 1;
1792bd681513SChris Mason 				btrfs_release_path(p);
1793bd681513SChris Mason 				goto again;
1794bd681513SChris Mason 			}
1795bd681513SChris Mason 
1796f9efa9c7SChris Mason 			unlock_up(p, level, lowest_unlock);
1797f9efa9c7SChris Mason 
1798925baeddSChris Mason 			if (level == lowest_level) {
179933c66f43SYan Zheng 				if (dec)
180033c66f43SYan Zheng 					p->slots[level]++;
18015b21f2edSZheng Yan 				goto done;
1802925baeddSChris Mason 			}
1803ca7a79adSChris Mason 
180433c66f43SYan Zheng 			err = read_block_for_search(trans, root, p,
1805c8c42864SChris Mason 						    &b, level, slot, key);
180633c66f43SYan Zheng 			if (err == -EAGAIN)
1807051e1b9fSChris Mason 				goto again;
180833c66f43SYan Zheng 			if (err) {
180933c66f43SYan Zheng 				ret = err;
181076a05b35SChris Mason 				goto done;
181133c66f43SYan Zheng 			}
181276a05b35SChris Mason 
1813b4ce94deSChris Mason 			if (!p->skip_locking) {
1814bd681513SChris Mason 				level = btrfs_header_level(b);
1815bd681513SChris Mason 				if (level <= write_lock_level) {
1816bd681513SChris Mason 					err = btrfs_try_tree_write_lock(b);
181733c66f43SYan Zheng 					if (!err) {
1818b4ce94deSChris Mason 						btrfs_set_path_blocking(p);
1819925baeddSChris Mason 						btrfs_tree_lock(b);
1820bd681513SChris Mason 						btrfs_clear_path_blocking(p, b,
1821bd681513SChris Mason 								  BTRFS_WRITE_LOCK);
1822b4ce94deSChris Mason 					}
1823bd681513SChris Mason 					p->locks[level] = BTRFS_WRITE_LOCK;
1824bd681513SChris Mason 				} else {
1825bd681513SChris Mason 					err = btrfs_try_tree_read_lock(b);
1826bd681513SChris Mason 					if (!err) {
1827bd681513SChris Mason 						btrfs_set_path_blocking(p);
1828bd681513SChris Mason 						btrfs_tree_read_lock(b);
1829bd681513SChris Mason 						btrfs_clear_path_blocking(p, b,
1830bd681513SChris Mason 								  BTRFS_READ_LOCK);
1831bd681513SChris Mason 					}
1832bd681513SChris Mason 					p->locks[level] = BTRFS_READ_LOCK;
1833bd681513SChris Mason 				}
1834bd681513SChris Mason 				p->nodes[level] = b;
1835b4ce94deSChris Mason 			}
1836be0e5c09SChris Mason 		} else {
1837be0e5c09SChris Mason 			p->slots[level] = slot;
183887b29b20SYan Zheng 			if (ins_len > 0 &&
183987b29b20SYan Zheng 			    btrfs_leaf_free_space(root, b) < ins_len) {
1840bd681513SChris Mason 				if (write_lock_level < 1) {
1841bd681513SChris Mason 					write_lock_level = 1;
1842bd681513SChris Mason 					btrfs_release_path(p);
1843bd681513SChris Mason 					goto again;
1844bd681513SChris Mason 				}
1845bd681513SChris Mason 
1846b4ce94deSChris Mason 				btrfs_set_path_blocking(p);
184733c66f43SYan Zheng 				err = split_leaf(trans, root, key,
1848cc0c5538SChris Mason 						 p, ins_len, ret == 0);
1849bd681513SChris Mason 				btrfs_clear_path_blocking(p, NULL, 0);
1850b4ce94deSChris Mason 
185133c66f43SYan Zheng 				BUG_ON(err > 0);
185233c66f43SYan Zheng 				if (err) {
185333c66f43SYan Zheng 					ret = err;
185465b51a00SChris Mason 					goto done;
185565b51a00SChris Mason 				}
18565c680ed6SChris Mason 			}
1857459931ecSChris Mason 			if (!p->search_for_split)
1858925baeddSChris Mason 				unlock_up(p, level, lowest_unlock);
185965b51a00SChris Mason 			goto done;
186065b51a00SChris Mason 		}
186165b51a00SChris Mason 	}
186265b51a00SChris Mason 	ret = 1;
186365b51a00SChris Mason done:
1864b4ce94deSChris Mason 	/*
1865b4ce94deSChris Mason 	 * we don't really know what they plan on doing with the path
1866b4ce94deSChris Mason 	 * from here on, so for now just mark it as blocking
1867b4ce94deSChris Mason 	 */
1868b9473439SChris Mason 	if (!p->leave_spinning)
1869b4ce94deSChris Mason 		btrfs_set_path_blocking(p);
187076a05b35SChris Mason 	if (ret < 0)
1871b3b4aa74SDavid Sterba 		btrfs_release_path(p);
1872be0e5c09SChris Mason 	return ret;
1873be0e5c09SChris Mason }
1874be0e5c09SChris Mason 
187574123bd7SChris Mason /*
187674123bd7SChris Mason  * adjust the pointers going up the tree, starting at level
187774123bd7SChris Mason  * making sure the right key of each node is points to 'key'.
187874123bd7SChris Mason  * This is used after shifting pointers to the left, so it stops
187974123bd7SChris Mason  * fixing up pointers when a given leaf/node is not in slot 0 of the
188074123bd7SChris Mason  * higher levels
1881aa5d6bedSChris Mason  *
188274123bd7SChris Mason  */
1883143bede5SJeff Mahoney static void fixup_low_keys(struct btrfs_trans_handle *trans,
18845f39d397SChris Mason 			   struct btrfs_root *root, struct btrfs_path *path,
18855f39d397SChris Mason 			   struct btrfs_disk_key *key, int level)
1886be0e5c09SChris Mason {
1887be0e5c09SChris Mason 	int i;
18885f39d397SChris Mason 	struct extent_buffer *t;
18895f39d397SChris Mason 
1890234b63a0SChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1891be0e5c09SChris Mason 		int tslot = path->slots[i];
1892eb60ceacSChris Mason 		if (!path->nodes[i])
1893be0e5c09SChris Mason 			break;
18945f39d397SChris Mason 		t = path->nodes[i];
18955f39d397SChris Mason 		btrfs_set_node_key(t, key, tslot);
1896d6025579SChris Mason 		btrfs_mark_buffer_dirty(path->nodes[i]);
1897be0e5c09SChris Mason 		if (tslot != 0)
1898be0e5c09SChris Mason 			break;
1899be0e5c09SChris Mason 	}
1900be0e5c09SChris Mason }
1901be0e5c09SChris Mason 
190274123bd7SChris Mason /*
190331840ae1SZheng Yan  * update item key.
190431840ae1SZheng Yan  *
190531840ae1SZheng Yan  * This function isn't completely safe. It's the caller's responsibility
190631840ae1SZheng Yan  * that the new key won't break the order
190731840ae1SZheng Yan  */
1908143bede5SJeff Mahoney void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
190931840ae1SZheng Yan 			     struct btrfs_root *root, struct btrfs_path *path,
191031840ae1SZheng Yan 			     struct btrfs_key *new_key)
191131840ae1SZheng Yan {
191231840ae1SZheng Yan 	struct btrfs_disk_key disk_key;
191331840ae1SZheng Yan 	struct extent_buffer *eb;
191431840ae1SZheng Yan 	int slot;
191531840ae1SZheng Yan 
191631840ae1SZheng Yan 	eb = path->nodes[0];
191731840ae1SZheng Yan 	slot = path->slots[0];
191831840ae1SZheng Yan 	if (slot > 0) {
191931840ae1SZheng Yan 		btrfs_item_key(eb, &disk_key, slot - 1);
1920143bede5SJeff Mahoney 		BUG_ON(comp_keys(&disk_key, new_key) >= 0);
192131840ae1SZheng Yan 	}
192231840ae1SZheng Yan 	if (slot < btrfs_header_nritems(eb) - 1) {
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 
192731840ae1SZheng Yan 	btrfs_cpu_key_to_disk(&disk_key, new_key);
192831840ae1SZheng Yan 	btrfs_set_item_key(eb, &disk_key, slot);
192931840ae1SZheng Yan 	btrfs_mark_buffer_dirty(eb);
193031840ae1SZheng Yan 	if (slot == 0)
193131840ae1SZheng Yan 		fixup_low_keys(trans, root, path, &disk_key, 1);
193231840ae1SZheng Yan }
193331840ae1SZheng Yan 
193431840ae1SZheng Yan /*
193574123bd7SChris Mason  * try to push data from one node into the next node left in the
193679f95c82SChris Mason  * tree.
1937aa5d6bedSChris Mason  *
1938aa5d6bedSChris Mason  * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1939aa5d6bedSChris Mason  * error, and > 0 if there was no room in the left hand block.
194074123bd7SChris Mason  */
194198ed5174SChris Mason static int push_node_left(struct btrfs_trans_handle *trans,
194298ed5174SChris Mason 			  struct btrfs_root *root, struct extent_buffer *dst,
1943971a1f66SChris Mason 			  struct extent_buffer *src, int empty)
1944be0e5c09SChris Mason {
1945be0e5c09SChris Mason 	int push_items = 0;
1946bb803951SChris Mason 	int src_nritems;
1947bb803951SChris Mason 	int dst_nritems;
1948aa5d6bedSChris Mason 	int ret = 0;
1949be0e5c09SChris Mason 
19505f39d397SChris Mason 	src_nritems = btrfs_header_nritems(src);
19515f39d397SChris Mason 	dst_nritems = btrfs_header_nritems(dst);
1952123abc88SChris Mason 	push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
19537bb86316SChris Mason 	WARN_ON(btrfs_header_generation(src) != trans->transid);
19547bb86316SChris Mason 	WARN_ON(btrfs_header_generation(dst) != trans->transid);
195554aa1f4dSChris Mason 
1956bce4eae9SChris Mason 	if (!empty && src_nritems <= 8)
1957971a1f66SChris Mason 		return 1;
1958971a1f66SChris Mason 
1959d397712bSChris Mason 	if (push_items <= 0)
1960be0e5c09SChris Mason 		return 1;
1961be0e5c09SChris Mason 
1962bce4eae9SChris Mason 	if (empty) {
1963971a1f66SChris Mason 		push_items = min(src_nritems, push_items);
1964bce4eae9SChris Mason 		if (push_items < src_nritems) {
1965bce4eae9SChris Mason 			/* leave at least 8 pointers in the node if
1966bce4eae9SChris Mason 			 * we aren't going to empty it
1967bce4eae9SChris Mason 			 */
1968bce4eae9SChris Mason 			if (src_nritems - push_items < 8) {
1969bce4eae9SChris Mason 				if (push_items <= 8)
1970bce4eae9SChris Mason 					return 1;
1971bce4eae9SChris Mason 				push_items -= 8;
1972bce4eae9SChris Mason 			}
1973bce4eae9SChris Mason 		}
1974bce4eae9SChris Mason 	} else
1975bce4eae9SChris Mason 		push_items = min(src_nritems - 8, push_items);
197679f95c82SChris Mason 
19775f39d397SChris Mason 	copy_extent_buffer(dst, src,
19785f39d397SChris Mason 			   btrfs_node_key_ptr_offset(dst_nritems),
19795f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
1980123abc88SChris Mason 			   push_items * sizeof(struct btrfs_key_ptr));
19815f39d397SChris Mason 
1982bb803951SChris Mason 	if (push_items < src_nritems) {
19835f39d397SChris Mason 		memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
19845f39d397SChris Mason 				      btrfs_node_key_ptr_offset(push_items),
1985e2fa7227SChris Mason 				      (src_nritems - push_items) *
1986123abc88SChris Mason 				      sizeof(struct btrfs_key_ptr));
1987bb803951SChris Mason 	}
19885f39d397SChris Mason 	btrfs_set_header_nritems(src, src_nritems - push_items);
19895f39d397SChris Mason 	btrfs_set_header_nritems(dst, dst_nritems + push_items);
19905f39d397SChris Mason 	btrfs_mark_buffer_dirty(src);
19915f39d397SChris Mason 	btrfs_mark_buffer_dirty(dst);
199231840ae1SZheng Yan 
1993bb803951SChris Mason 	return ret;
1994be0e5c09SChris Mason }
1995be0e5c09SChris Mason 
199697571fd0SChris Mason /*
199779f95c82SChris Mason  * try to push data from one node into the next node right in the
199879f95c82SChris Mason  * tree.
199979f95c82SChris Mason  *
200079f95c82SChris Mason  * returns 0 if some ptrs were pushed, < 0 if there was some horrible
200179f95c82SChris Mason  * error, and > 0 if there was no room in the right hand block.
200279f95c82SChris Mason  *
200379f95c82SChris Mason  * this will  only push up to 1/2 the contents of the left node over
200479f95c82SChris Mason  */
20055f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans,
20065f39d397SChris Mason 			      struct btrfs_root *root,
20075f39d397SChris Mason 			      struct extent_buffer *dst,
20085f39d397SChris Mason 			      struct extent_buffer *src)
200979f95c82SChris Mason {
201079f95c82SChris Mason 	int push_items = 0;
201179f95c82SChris Mason 	int max_push;
201279f95c82SChris Mason 	int src_nritems;
201379f95c82SChris Mason 	int dst_nritems;
201479f95c82SChris Mason 	int ret = 0;
201579f95c82SChris Mason 
20167bb86316SChris Mason 	WARN_ON(btrfs_header_generation(src) != trans->transid);
20177bb86316SChris Mason 	WARN_ON(btrfs_header_generation(dst) != trans->transid);
20187bb86316SChris Mason 
20195f39d397SChris Mason 	src_nritems = btrfs_header_nritems(src);
20205f39d397SChris Mason 	dst_nritems = btrfs_header_nritems(dst);
2021123abc88SChris Mason 	push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
2022d397712bSChris Mason 	if (push_items <= 0)
202379f95c82SChris Mason 		return 1;
2024bce4eae9SChris Mason 
2025d397712bSChris Mason 	if (src_nritems < 4)
2026bce4eae9SChris Mason 		return 1;
202779f95c82SChris Mason 
202879f95c82SChris Mason 	max_push = src_nritems / 2 + 1;
202979f95c82SChris Mason 	/* don't try to empty the node */
2030d397712bSChris Mason 	if (max_push >= src_nritems)
203179f95c82SChris Mason 		return 1;
2032252c38f0SYan 
203379f95c82SChris Mason 	if (max_push < push_items)
203479f95c82SChris Mason 		push_items = max_push;
203579f95c82SChris Mason 
20365f39d397SChris Mason 	memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
20375f39d397SChris Mason 				      btrfs_node_key_ptr_offset(0),
20385f39d397SChris Mason 				      (dst_nritems) *
20395f39d397SChris Mason 				      sizeof(struct btrfs_key_ptr));
2040d6025579SChris Mason 
20415f39d397SChris Mason 	copy_extent_buffer(dst, src,
20425f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
20435f39d397SChris Mason 			   btrfs_node_key_ptr_offset(src_nritems - push_items),
2044123abc88SChris Mason 			   push_items * sizeof(struct btrfs_key_ptr));
204579f95c82SChris Mason 
20465f39d397SChris Mason 	btrfs_set_header_nritems(src, src_nritems - push_items);
20475f39d397SChris Mason 	btrfs_set_header_nritems(dst, dst_nritems + push_items);
204879f95c82SChris Mason 
20495f39d397SChris Mason 	btrfs_mark_buffer_dirty(src);
20505f39d397SChris Mason 	btrfs_mark_buffer_dirty(dst);
205131840ae1SZheng Yan 
205279f95c82SChris Mason 	return ret;
205379f95c82SChris Mason }
205479f95c82SChris Mason 
205579f95c82SChris Mason /*
205697571fd0SChris Mason  * helper function to insert a new root level in the tree.
205797571fd0SChris Mason  * A new node is allocated, and a single item is inserted to
205897571fd0SChris Mason  * point to the existing root
2059aa5d6bedSChris Mason  *
2060aa5d6bedSChris Mason  * returns zero on success or < 0 on failure.
206197571fd0SChris Mason  */
2062d397712bSChris Mason static noinline int insert_new_root(struct btrfs_trans_handle *trans,
20635f39d397SChris Mason 			   struct btrfs_root *root,
20645f39d397SChris Mason 			   struct btrfs_path *path, int level)
206574123bd7SChris Mason {
20667bb86316SChris Mason 	u64 lower_gen;
20675f39d397SChris Mason 	struct extent_buffer *lower;
20685f39d397SChris Mason 	struct extent_buffer *c;
2069925baeddSChris Mason 	struct extent_buffer *old;
20705f39d397SChris Mason 	struct btrfs_disk_key lower_key;
20715c680ed6SChris Mason 
20725c680ed6SChris Mason 	BUG_ON(path->nodes[level]);
20735c680ed6SChris Mason 	BUG_ON(path->nodes[level-1] != root->node);
20745c680ed6SChris Mason 
20757bb86316SChris Mason 	lower = path->nodes[level-1];
20767bb86316SChris Mason 	if (level == 1)
20777bb86316SChris Mason 		btrfs_item_key(lower, &lower_key, 0);
20787bb86316SChris Mason 	else
20797bb86316SChris Mason 		btrfs_node_key(lower, &lower_key, 0);
20807bb86316SChris Mason 
208131840ae1SZheng Yan 	c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
20825d4f98a2SYan Zheng 				   root->root_key.objectid, &lower_key,
208366d7e7f0SArne Jansen 				   level, root->node->start, 0, 0);
20845f39d397SChris Mason 	if (IS_ERR(c))
20855f39d397SChris Mason 		return PTR_ERR(c);
2086925baeddSChris Mason 
2087f0486c68SYan, Zheng 	root_add_used(root, root->nodesize);
2088f0486c68SYan, Zheng 
20895d4f98a2SYan Zheng 	memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
20905f39d397SChris Mason 	btrfs_set_header_nritems(c, 1);
20915f39d397SChris Mason 	btrfs_set_header_level(c, level);
2092db94535dSChris Mason 	btrfs_set_header_bytenr(c, c->start);
20935f39d397SChris Mason 	btrfs_set_header_generation(c, trans->transid);
20945d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
20955f39d397SChris Mason 	btrfs_set_header_owner(c, root->root_key.objectid);
2096d5719762SChris Mason 
20975f39d397SChris Mason 	write_extent_buffer(c, root->fs_info->fsid,
20985f39d397SChris Mason 			    (unsigned long)btrfs_header_fsid(c),
20995f39d397SChris Mason 			    BTRFS_FSID_SIZE);
2100e17cade2SChris Mason 
2101e17cade2SChris Mason 	write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2102e17cade2SChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(c),
2103e17cade2SChris Mason 			    BTRFS_UUID_SIZE);
2104e17cade2SChris Mason 
21055f39d397SChris Mason 	btrfs_set_node_key(c, &lower_key, 0);
2106db94535dSChris Mason 	btrfs_set_node_blockptr(c, 0, lower->start);
21077bb86316SChris Mason 	lower_gen = btrfs_header_generation(lower);
210831840ae1SZheng Yan 	WARN_ON(lower_gen != trans->transid);
21097bb86316SChris Mason 
21107bb86316SChris Mason 	btrfs_set_node_ptr_generation(c, 0, lower_gen);
21115f39d397SChris Mason 
21125f39d397SChris Mason 	btrfs_mark_buffer_dirty(c);
2113d5719762SChris Mason 
2114925baeddSChris Mason 	old = root->node;
2115240f62c8SChris Mason 	rcu_assign_pointer(root->node, c);
2116925baeddSChris Mason 
2117925baeddSChris Mason 	/* the super has an extra ref to root->node */
2118925baeddSChris Mason 	free_extent_buffer(old);
2119925baeddSChris Mason 
21200b86a832SChris Mason 	add_root_to_dirty_list(root);
21215f39d397SChris Mason 	extent_buffer_get(c);
21225f39d397SChris Mason 	path->nodes[level] = c;
2123bd681513SChris Mason 	path->locks[level] = BTRFS_WRITE_LOCK;
212474123bd7SChris Mason 	path->slots[level] = 0;
212574123bd7SChris Mason 	return 0;
212674123bd7SChris Mason }
21275c680ed6SChris Mason 
21285c680ed6SChris Mason /*
21295c680ed6SChris Mason  * worker function to insert a single pointer in a node.
21305c680ed6SChris Mason  * the node should have enough room for the pointer already
213197571fd0SChris Mason  *
21325c680ed6SChris Mason  * slot and level indicate where you want the key to go, and
21335c680ed6SChris Mason  * blocknr is the block the key points to.
21345c680ed6SChris Mason  */
2135143bede5SJeff Mahoney static void insert_ptr(struct btrfs_trans_handle *trans,
2136143bede5SJeff Mahoney 		       struct btrfs_root *root, struct btrfs_path *path,
2137143bede5SJeff Mahoney 		       struct btrfs_disk_key *key, u64 bytenr,
2138143bede5SJeff Mahoney 		       int slot, int level)
21395c680ed6SChris Mason {
21405f39d397SChris Mason 	struct extent_buffer *lower;
21415c680ed6SChris Mason 	int nritems;
21425c680ed6SChris Mason 
21435c680ed6SChris Mason 	BUG_ON(!path->nodes[level]);
2144f0486c68SYan, Zheng 	btrfs_assert_tree_locked(path->nodes[level]);
21455f39d397SChris Mason 	lower = path->nodes[level];
21465f39d397SChris Mason 	nritems = btrfs_header_nritems(lower);
2147c293498bSStoyan Gaydarov 	BUG_ON(slot > nritems);
2148143bede5SJeff Mahoney 	BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
214974123bd7SChris Mason 	if (slot != nritems) {
21505f39d397SChris Mason 		memmove_extent_buffer(lower,
21515f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot + 1),
21525f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot),
2153123abc88SChris Mason 			      (nritems - slot) * sizeof(struct btrfs_key_ptr));
215474123bd7SChris Mason 	}
21555f39d397SChris Mason 	btrfs_set_node_key(lower, key, slot);
2156db94535dSChris Mason 	btrfs_set_node_blockptr(lower, slot, bytenr);
215774493f7aSChris Mason 	WARN_ON(trans->transid == 0);
215874493f7aSChris Mason 	btrfs_set_node_ptr_generation(lower, slot, trans->transid);
21595f39d397SChris Mason 	btrfs_set_header_nritems(lower, nritems + 1);
21605f39d397SChris Mason 	btrfs_mark_buffer_dirty(lower);
216174123bd7SChris Mason }
216274123bd7SChris Mason 
216397571fd0SChris Mason /*
216497571fd0SChris Mason  * split the node at the specified level in path in two.
216597571fd0SChris Mason  * The path is corrected to point to the appropriate node after the split
216697571fd0SChris Mason  *
216797571fd0SChris Mason  * Before splitting this tries to make some room in the node by pushing
216897571fd0SChris Mason  * left and right, if either one works, it returns right away.
2169aa5d6bedSChris Mason  *
2170aa5d6bedSChris Mason  * returns 0 on success and < 0 on failure
217197571fd0SChris Mason  */
2172e02119d5SChris Mason static noinline int split_node(struct btrfs_trans_handle *trans,
2173e02119d5SChris Mason 			       struct btrfs_root *root,
2174e02119d5SChris Mason 			       struct btrfs_path *path, int level)
2175be0e5c09SChris Mason {
21765f39d397SChris Mason 	struct extent_buffer *c;
21775f39d397SChris Mason 	struct extent_buffer *split;
21785f39d397SChris Mason 	struct btrfs_disk_key disk_key;
2179be0e5c09SChris Mason 	int mid;
21805c680ed6SChris Mason 	int ret;
21817518a238SChris Mason 	u32 c_nritems;
2182be0e5c09SChris Mason 
21835f39d397SChris Mason 	c = path->nodes[level];
21847bb86316SChris Mason 	WARN_ON(btrfs_header_generation(c) != trans->transid);
21855f39d397SChris Mason 	if (c == root->node) {
21865c680ed6SChris Mason 		/* trying to split the root, lets make a new one */
2187e089f05cSChris Mason 		ret = insert_new_root(trans, root, path, level + 1);
21885c680ed6SChris Mason 		if (ret)
21895c680ed6SChris Mason 			return ret;
2190b3612421SChris Mason 	} else {
2191e66f709bSChris Mason 		ret = push_nodes_for_insert(trans, root, path, level);
21925f39d397SChris Mason 		c = path->nodes[level];
21935f39d397SChris Mason 		if (!ret && btrfs_header_nritems(c) <
2194c448acf0SChris Mason 		    BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
2195e66f709bSChris Mason 			return 0;
219654aa1f4dSChris Mason 		if (ret < 0)
219754aa1f4dSChris Mason 			return ret;
21985c680ed6SChris Mason 	}
2199e66f709bSChris Mason 
22005f39d397SChris Mason 	c_nritems = btrfs_header_nritems(c);
22015d4f98a2SYan Zheng 	mid = (c_nritems + 1) / 2;
22025d4f98a2SYan Zheng 	btrfs_node_key(c, &disk_key, mid);
22037bb86316SChris Mason 
22045d4f98a2SYan Zheng 	split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
22057bb86316SChris Mason 					root->root_key.objectid,
220666d7e7f0SArne Jansen 					&disk_key, level, c->start, 0, 0);
22075f39d397SChris Mason 	if (IS_ERR(split))
22085f39d397SChris Mason 		return PTR_ERR(split);
220954aa1f4dSChris Mason 
2210f0486c68SYan, Zheng 	root_add_used(root, root->nodesize);
2211f0486c68SYan, Zheng 
22125d4f98a2SYan Zheng 	memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
22135f39d397SChris Mason 	btrfs_set_header_level(split, btrfs_header_level(c));
2214db94535dSChris Mason 	btrfs_set_header_bytenr(split, split->start);
22155f39d397SChris Mason 	btrfs_set_header_generation(split, trans->transid);
22165d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
22175f39d397SChris Mason 	btrfs_set_header_owner(split, root->root_key.objectid);
22185f39d397SChris Mason 	write_extent_buffer(split, root->fs_info->fsid,
22195f39d397SChris Mason 			    (unsigned long)btrfs_header_fsid(split),
22205f39d397SChris Mason 			    BTRFS_FSID_SIZE);
2221e17cade2SChris Mason 	write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2222e17cade2SChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(split),
2223e17cade2SChris Mason 			    BTRFS_UUID_SIZE);
22245f39d397SChris Mason 
22255f39d397SChris Mason 
22265f39d397SChris Mason 	copy_extent_buffer(split, c,
22275f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
22285f39d397SChris Mason 			   btrfs_node_key_ptr_offset(mid),
2229123abc88SChris Mason 			   (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
22305f39d397SChris Mason 	btrfs_set_header_nritems(split, c_nritems - mid);
22315f39d397SChris Mason 	btrfs_set_header_nritems(c, mid);
2232aa5d6bedSChris Mason 	ret = 0;
2233aa5d6bedSChris Mason 
22345f39d397SChris Mason 	btrfs_mark_buffer_dirty(c);
22355f39d397SChris Mason 	btrfs_mark_buffer_dirty(split);
22365f39d397SChris Mason 
2237143bede5SJeff Mahoney 	insert_ptr(trans, root, path, &disk_key, split->start,
2238143bede5SJeff Mahoney 		   path->slots[level + 1] + 1, level + 1);
2239aa5d6bedSChris Mason 
22405de08d7dSChris Mason 	if (path->slots[level] >= mid) {
22415c680ed6SChris Mason 		path->slots[level] -= mid;
2242925baeddSChris Mason 		btrfs_tree_unlock(c);
22435f39d397SChris Mason 		free_extent_buffer(c);
22445f39d397SChris Mason 		path->nodes[level] = split;
22455c680ed6SChris Mason 		path->slots[level + 1] += 1;
2246eb60ceacSChris Mason 	} else {
2247925baeddSChris Mason 		btrfs_tree_unlock(split);
22485f39d397SChris Mason 		free_extent_buffer(split);
2249be0e5c09SChris Mason 	}
2250aa5d6bedSChris Mason 	return ret;
2251be0e5c09SChris Mason }
2252be0e5c09SChris Mason 
225374123bd7SChris Mason /*
225474123bd7SChris Mason  * how many bytes are required to store the items in a leaf.  start
225574123bd7SChris Mason  * and nr indicate which items in the leaf to check.  This totals up the
225674123bd7SChris Mason  * space used both by the item structs and the item data
225774123bd7SChris Mason  */
22585f39d397SChris Mason static int leaf_space_used(struct extent_buffer *l, int start, int nr)
2259be0e5c09SChris Mason {
2260be0e5c09SChris Mason 	int data_len;
22615f39d397SChris Mason 	int nritems = btrfs_header_nritems(l);
2262d4dbff95SChris Mason 	int end = min(nritems, start + nr) - 1;
2263be0e5c09SChris Mason 
2264be0e5c09SChris Mason 	if (!nr)
2265be0e5c09SChris Mason 		return 0;
22665f39d397SChris Mason 	data_len = btrfs_item_end_nr(l, start);
22675f39d397SChris Mason 	data_len = data_len - btrfs_item_offset_nr(l, end);
22680783fcfcSChris Mason 	data_len += sizeof(struct btrfs_item) * nr;
2269d4dbff95SChris Mason 	WARN_ON(data_len < 0);
2270be0e5c09SChris Mason 	return data_len;
2271be0e5c09SChris Mason }
2272be0e5c09SChris Mason 
227374123bd7SChris Mason /*
2274d4dbff95SChris Mason  * The space between the end of the leaf items and
2275d4dbff95SChris Mason  * the start of the leaf data.  IOW, how much room
2276d4dbff95SChris Mason  * the leaf has left for both items and data
2277d4dbff95SChris Mason  */
2278d397712bSChris Mason noinline int btrfs_leaf_free_space(struct btrfs_root *root,
2279e02119d5SChris Mason 				   struct extent_buffer *leaf)
2280d4dbff95SChris Mason {
22815f39d397SChris Mason 	int nritems = btrfs_header_nritems(leaf);
22825f39d397SChris Mason 	int ret;
22835f39d397SChris Mason 	ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
22845f39d397SChris Mason 	if (ret < 0) {
2285d397712bSChris Mason 		printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2286d397712bSChris Mason 		       "used %d nritems %d\n",
2287ae2f5411SJens Axboe 		       ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
22885f39d397SChris Mason 		       leaf_space_used(leaf, 0, nritems), nritems);
22895f39d397SChris Mason 	}
22905f39d397SChris Mason 	return ret;
2291d4dbff95SChris Mason }
2292d4dbff95SChris Mason 
229399d8f83cSChris Mason /*
229499d8f83cSChris Mason  * min slot controls the lowest index we're willing to push to the
229599d8f83cSChris Mason  * right.  We'll push up to and including min_slot, but no lower
229699d8f83cSChris Mason  */
229744871b1bSChris Mason static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
229844871b1bSChris Mason 				      struct btrfs_root *root,
229944871b1bSChris Mason 				      struct btrfs_path *path,
230044871b1bSChris Mason 				      int data_size, int empty,
230144871b1bSChris Mason 				      struct extent_buffer *right,
230299d8f83cSChris Mason 				      int free_space, u32 left_nritems,
230399d8f83cSChris Mason 				      u32 min_slot)
230400ec4c51SChris Mason {
23055f39d397SChris Mason 	struct extent_buffer *left = path->nodes[0];
230644871b1bSChris Mason 	struct extent_buffer *upper = path->nodes[1];
23075f39d397SChris Mason 	struct btrfs_disk_key disk_key;
230800ec4c51SChris Mason 	int slot;
230934a38218SChris Mason 	u32 i;
231000ec4c51SChris Mason 	int push_space = 0;
231100ec4c51SChris Mason 	int push_items = 0;
23120783fcfcSChris Mason 	struct btrfs_item *item;
231334a38218SChris Mason 	u32 nr;
23147518a238SChris Mason 	u32 right_nritems;
23155f39d397SChris Mason 	u32 data_end;
2316db94535dSChris Mason 	u32 this_item_size;
231700ec4c51SChris Mason 
231834a38218SChris Mason 	if (empty)
231934a38218SChris Mason 		nr = 0;
232034a38218SChris Mason 	else
232199d8f83cSChris Mason 		nr = max_t(u32, 1, min_slot);
232234a38218SChris Mason 
232331840ae1SZheng Yan 	if (path->slots[0] >= left_nritems)
232487b29b20SYan Zheng 		push_space += data_size;
232531840ae1SZheng Yan 
232644871b1bSChris Mason 	slot = path->slots[1];
232734a38218SChris Mason 	i = left_nritems - 1;
232834a38218SChris Mason 	while (i >= nr) {
23295f39d397SChris Mason 		item = btrfs_item_nr(left, i);
2330db94535dSChris Mason 
233131840ae1SZheng Yan 		if (!empty && push_items > 0) {
233231840ae1SZheng Yan 			if (path->slots[0] > i)
233331840ae1SZheng Yan 				break;
233431840ae1SZheng Yan 			if (path->slots[0] == i) {
233531840ae1SZheng Yan 				int space = btrfs_leaf_free_space(root, left);
233631840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
233731840ae1SZheng Yan 					break;
233831840ae1SZheng Yan 			}
233931840ae1SZheng Yan 		}
234031840ae1SZheng Yan 
234100ec4c51SChris Mason 		if (path->slots[0] == i)
234287b29b20SYan Zheng 			push_space += data_size;
2343db94535dSChris Mason 
2344db94535dSChris Mason 		this_item_size = btrfs_item_size(left, item);
2345db94535dSChris Mason 		if (this_item_size + sizeof(*item) + push_space > free_space)
234600ec4c51SChris Mason 			break;
234731840ae1SZheng Yan 
234800ec4c51SChris Mason 		push_items++;
2349db94535dSChris Mason 		push_space += this_item_size + sizeof(*item);
235034a38218SChris Mason 		if (i == 0)
235134a38218SChris Mason 			break;
235234a38218SChris Mason 		i--;
2353db94535dSChris Mason 	}
23545f39d397SChris Mason 
2355925baeddSChris Mason 	if (push_items == 0)
2356925baeddSChris Mason 		goto out_unlock;
23575f39d397SChris Mason 
235834a38218SChris Mason 	if (!empty && push_items == left_nritems)
2359a429e513SChris Mason 		WARN_ON(1);
23605f39d397SChris Mason 
236100ec4c51SChris Mason 	/* push left to right */
23625f39d397SChris Mason 	right_nritems = btrfs_header_nritems(right);
236334a38218SChris Mason 
23645f39d397SChris Mason 	push_space = btrfs_item_end_nr(left, left_nritems - push_items);
2365123abc88SChris Mason 	push_space -= leaf_data_end(root, left);
23665f39d397SChris Mason 
236700ec4c51SChris Mason 	/* make room in the right data area */
23685f39d397SChris Mason 	data_end = leaf_data_end(root, right);
23695f39d397SChris Mason 	memmove_extent_buffer(right,
23705f39d397SChris Mason 			      btrfs_leaf_data(right) + data_end - push_space,
23715f39d397SChris Mason 			      btrfs_leaf_data(right) + data_end,
23725f39d397SChris Mason 			      BTRFS_LEAF_DATA_SIZE(root) - data_end);
23735f39d397SChris Mason 
237400ec4c51SChris Mason 	/* copy from the left data area */
23755f39d397SChris Mason 	copy_extent_buffer(right, left, btrfs_leaf_data(right) +
2376d6025579SChris Mason 		     BTRFS_LEAF_DATA_SIZE(root) - push_space,
2377d6025579SChris Mason 		     btrfs_leaf_data(left) + leaf_data_end(root, left),
2378d6025579SChris Mason 		     push_space);
23795f39d397SChris Mason 
23805f39d397SChris Mason 	memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
23815f39d397SChris Mason 			      btrfs_item_nr_offset(0),
23820783fcfcSChris Mason 			      right_nritems * sizeof(struct btrfs_item));
23835f39d397SChris Mason 
238400ec4c51SChris Mason 	/* copy the items from left to right */
23855f39d397SChris Mason 	copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
23865f39d397SChris Mason 		   btrfs_item_nr_offset(left_nritems - push_items),
23870783fcfcSChris Mason 		   push_items * sizeof(struct btrfs_item));
238800ec4c51SChris Mason 
238900ec4c51SChris Mason 	/* update the item pointers */
23907518a238SChris Mason 	right_nritems += push_items;
23915f39d397SChris Mason 	btrfs_set_header_nritems(right, right_nritems);
2392123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root);
23937518a238SChris Mason 	for (i = 0; i < right_nritems; i++) {
23945f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2395db94535dSChris Mason 		push_space -= btrfs_item_size(right, item);
2396db94535dSChris Mason 		btrfs_set_item_offset(right, item, push_space);
2397db94535dSChris Mason 	}
2398db94535dSChris Mason 
23997518a238SChris Mason 	left_nritems -= push_items;
24005f39d397SChris Mason 	btrfs_set_header_nritems(left, left_nritems);
240100ec4c51SChris Mason 
240234a38218SChris Mason 	if (left_nritems)
24035f39d397SChris Mason 		btrfs_mark_buffer_dirty(left);
2404f0486c68SYan, Zheng 	else
2405f0486c68SYan, Zheng 		clean_tree_block(trans, root, left);
2406f0486c68SYan, Zheng 
24075f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
2408a429e513SChris Mason 
24095f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
24105f39d397SChris Mason 	btrfs_set_node_key(upper, &disk_key, slot + 1);
2411d6025579SChris Mason 	btrfs_mark_buffer_dirty(upper);
241202217ed2SChris Mason 
241300ec4c51SChris Mason 	/* then fixup the leaf pointer in the path */
24147518a238SChris Mason 	if (path->slots[0] >= left_nritems) {
24157518a238SChris Mason 		path->slots[0] -= left_nritems;
2416925baeddSChris Mason 		if (btrfs_header_nritems(path->nodes[0]) == 0)
2417925baeddSChris Mason 			clean_tree_block(trans, root, path->nodes[0]);
2418925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
24195f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
24205f39d397SChris Mason 		path->nodes[0] = right;
242100ec4c51SChris Mason 		path->slots[1] += 1;
242200ec4c51SChris Mason 	} else {
2423925baeddSChris Mason 		btrfs_tree_unlock(right);
24245f39d397SChris Mason 		free_extent_buffer(right);
242500ec4c51SChris Mason 	}
242600ec4c51SChris Mason 	return 0;
2427925baeddSChris Mason 
2428925baeddSChris Mason out_unlock:
2429925baeddSChris Mason 	btrfs_tree_unlock(right);
2430925baeddSChris Mason 	free_extent_buffer(right);
2431925baeddSChris Mason 	return 1;
243200ec4c51SChris Mason }
2433925baeddSChris Mason 
243400ec4c51SChris Mason /*
243544871b1bSChris Mason  * push some data in the path leaf to the right, trying to free up at
243674123bd7SChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
243744871b1bSChris Mason  *
243844871b1bSChris Mason  * returns 1 if the push failed because the other node didn't have enough
243944871b1bSChris Mason  * room, 0 if everything worked out and < 0 if there were major errors.
244099d8f83cSChris Mason  *
244199d8f83cSChris Mason  * this will push starting from min_slot to the end of the leaf.  It won't
244299d8f83cSChris Mason  * push any slot lower than min_slot
244374123bd7SChris Mason  */
244444871b1bSChris Mason static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
244599d8f83cSChris Mason 			   *root, struct btrfs_path *path,
244699d8f83cSChris Mason 			   int min_data_size, int data_size,
244799d8f83cSChris Mason 			   int empty, u32 min_slot)
2448be0e5c09SChris Mason {
244944871b1bSChris Mason 	struct extent_buffer *left = path->nodes[0];
245044871b1bSChris Mason 	struct extent_buffer *right;
245144871b1bSChris Mason 	struct extent_buffer *upper;
245244871b1bSChris Mason 	int slot;
245344871b1bSChris Mason 	int free_space;
245444871b1bSChris Mason 	u32 left_nritems;
245544871b1bSChris Mason 	int ret;
245644871b1bSChris Mason 
245744871b1bSChris Mason 	if (!path->nodes[1])
245844871b1bSChris Mason 		return 1;
245944871b1bSChris Mason 
246044871b1bSChris Mason 	slot = path->slots[1];
246144871b1bSChris Mason 	upper = path->nodes[1];
246244871b1bSChris Mason 	if (slot >= btrfs_header_nritems(upper) - 1)
246344871b1bSChris Mason 		return 1;
246444871b1bSChris Mason 
246544871b1bSChris Mason 	btrfs_assert_tree_locked(path->nodes[1]);
246644871b1bSChris Mason 
246744871b1bSChris Mason 	right = read_node_slot(root, upper, slot + 1);
246891ca338dSTsutomu Itoh 	if (right == NULL)
246991ca338dSTsutomu Itoh 		return 1;
247091ca338dSTsutomu Itoh 
247144871b1bSChris Mason 	btrfs_tree_lock(right);
247244871b1bSChris Mason 	btrfs_set_lock_blocking(right);
247344871b1bSChris Mason 
247444871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, right);
247544871b1bSChris Mason 	if (free_space < data_size)
247644871b1bSChris Mason 		goto out_unlock;
247744871b1bSChris Mason 
247844871b1bSChris Mason 	/* cow and double check */
247944871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, right, upper,
248044871b1bSChris Mason 			      slot + 1, &right);
248144871b1bSChris Mason 	if (ret)
248244871b1bSChris Mason 		goto out_unlock;
248344871b1bSChris Mason 
248444871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, right);
248544871b1bSChris Mason 	if (free_space < data_size)
248644871b1bSChris Mason 		goto out_unlock;
248744871b1bSChris Mason 
248844871b1bSChris Mason 	left_nritems = btrfs_header_nritems(left);
248944871b1bSChris Mason 	if (left_nritems == 0)
249044871b1bSChris Mason 		goto out_unlock;
249144871b1bSChris Mason 
249299d8f83cSChris Mason 	return __push_leaf_right(trans, root, path, min_data_size, empty,
249399d8f83cSChris Mason 				right, free_space, left_nritems, min_slot);
249444871b1bSChris Mason out_unlock:
249544871b1bSChris Mason 	btrfs_tree_unlock(right);
249644871b1bSChris Mason 	free_extent_buffer(right);
249744871b1bSChris Mason 	return 1;
249844871b1bSChris Mason }
249944871b1bSChris Mason 
250044871b1bSChris Mason /*
250144871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
250244871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
250399d8f83cSChris Mason  *
250499d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
250599d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us do all the
250699d8f83cSChris Mason  * items
250744871b1bSChris Mason  */
250844871b1bSChris Mason static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
250944871b1bSChris Mason 				     struct btrfs_root *root,
251044871b1bSChris Mason 				     struct btrfs_path *path, int data_size,
251144871b1bSChris Mason 				     int empty, struct extent_buffer *left,
251299d8f83cSChris Mason 				     int free_space, u32 right_nritems,
251399d8f83cSChris Mason 				     u32 max_slot)
251444871b1bSChris Mason {
25155f39d397SChris Mason 	struct btrfs_disk_key disk_key;
25165f39d397SChris Mason 	struct extent_buffer *right = path->nodes[0];
2517be0e5c09SChris Mason 	int i;
2518be0e5c09SChris Mason 	int push_space = 0;
2519be0e5c09SChris Mason 	int push_items = 0;
25200783fcfcSChris Mason 	struct btrfs_item *item;
25217518a238SChris Mason 	u32 old_left_nritems;
252234a38218SChris Mason 	u32 nr;
2523aa5d6bedSChris Mason 	int ret = 0;
2524db94535dSChris Mason 	u32 this_item_size;
2525db94535dSChris Mason 	u32 old_left_item_size;
2526be0e5c09SChris Mason 
252734a38218SChris Mason 	if (empty)
252899d8f83cSChris Mason 		nr = min(right_nritems, max_slot);
252934a38218SChris Mason 	else
253099d8f83cSChris Mason 		nr = min(right_nritems - 1, max_slot);
253134a38218SChris Mason 
253234a38218SChris Mason 	for (i = 0; i < nr; i++) {
25335f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2534db94535dSChris Mason 
253531840ae1SZheng Yan 		if (!empty && push_items > 0) {
253631840ae1SZheng Yan 			if (path->slots[0] < i)
253731840ae1SZheng Yan 				break;
253831840ae1SZheng Yan 			if (path->slots[0] == i) {
253931840ae1SZheng Yan 				int space = btrfs_leaf_free_space(root, right);
254031840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
254131840ae1SZheng Yan 					break;
254231840ae1SZheng Yan 			}
254331840ae1SZheng Yan 		}
254431840ae1SZheng Yan 
2545be0e5c09SChris Mason 		if (path->slots[0] == i)
254687b29b20SYan Zheng 			push_space += data_size;
2547db94535dSChris Mason 
2548db94535dSChris Mason 		this_item_size = btrfs_item_size(right, item);
2549db94535dSChris Mason 		if (this_item_size + sizeof(*item) + push_space > free_space)
2550be0e5c09SChris Mason 			break;
2551db94535dSChris Mason 
2552be0e5c09SChris Mason 		push_items++;
2553db94535dSChris Mason 		push_space += this_item_size + sizeof(*item);
2554be0e5c09SChris Mason 	}
2555db94535dSChris Mason 
2556be0e5c09SChris Mason 	if (push_items == 0) {
2557925baeddSChris Mason 		ret = 1;
2558925baeddSChris Mason 		goto out;
2559be0e5c09SChris Mason 	}
256034a38218SChris Mason 	if (!empty && push_items == btrfs_header_nritems(right))
2561a429e513SChris Mason 		WARN_ON(1);
25625f39d397SChris Mason 
2563be0e5c09SChris Mason 	/* push data from right to left */
25645f39d397SChris Mason 	copy_extent_buffer(left, right,
25655f39d397SChris Mason 			   btrfs_item_nr_offset(btrfs_header_nritems(left)),
25665f39d397SChris Mason 			   btrfs_item_nr_offset(0),
25675f39d397SChris Mason 			   push_items * sizeof(struct btrfs_item));
25685f39d397SChris Mason 
2569123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root) -
25705f39d397SChris Mason 		     btrfs_item_offset_nr(right, push_items - 1);
25715f39d397SChris Mason 
25725f39d397SChris Mason 	copy_extent_buffer(left, right, btrfs_leaf_data(left) +
2573d6025579SChris Mason 		     leaf_data_end(root, left) - push_space,
2574123abc88SChris Mason 		     btrfs_leaf_data(right) +
25755f39d397SChris Mason 		     btrfs_item_offset_nr(right, push_items - 1),
2576be0e5c09SChris Mason 		     push_space);
25775f39d397SChris Mason 	old_left_nritems = btrfs_header_nritems(left);
257887b29b20SYan Zheng 	BUG_ON(old_left_nritems <= 0);
2579eb60ceacSChris Mason 
2580db94535dSChris Mason 	old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
2581be0e5c09SChris Mason 	for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
25825f39d397SChris Mason 		u32 ioff;
2583db94535dSChris Mason 
25845f39d397SChris Mason 		item = btrfs_item_nr(left, i);
2585db94535dSChris Mason 
25865f39d397SChris Mason 		ioff = btrfs_item_offset(left, item);
25875f39d397SChris Mason 		btrfs_set_item_offset(left, item,
2588db94535dSChris Mason 		      ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
2589be0e5c09SChris Mason 	}
25905f39d397SChris Mason 	btrfs_set_header_nritems(left, old_left_nritems + push_items);
2591be0e5c09SChris Mason 
2592be0e5c09SChris Mason 	/* fixup right node */
259334a38218SChris Mason 	if (push_items > right_nritems) {
2594d397712bSChris Mason 		printk(KERN_CRIT "push items %d nr %u\n", push_items,
2595d397712bSChris Mason 		       right_nritems);
259634a38218SChris Mason 		WARN_ON(1);
259734a38218SChris Mason 	}
259834a38218SChris Mason 
259934a38218SChris Mason 	if (push_items < right_nritems) {
26005f39d397SChris Mason 		push_space = btrfs_item_offset_nr(right, push_items - 1) -
2601123abc88SChris Mason 						  leaf_data_end(root, right);
26025f39d397SChris Mason 		memmove_extent_buffer(right, btrfs_leaf_data(right) +
2603d6025579SChris Mason 				      BTRFS_LEAF_DATA_SIZE(root) - push_space,
2604d6025579SChris Mason 				      btrfs_leaf_data(right) +
2605123abc88SChris Mason 				      leaf_data_end(root, right), push_space);
26065f39d397SChris Mason 
26075f39d397SChris Mason 		memmove_extent_buffer(right, btrfs_item_nr_offset(0),
26085f39d397SChris Mason 			      btrfs_item_nr_offset(push_items),
26095f39d397SChris Mason 			     (btrfs_header_nritems(right) - push_items) *
26100783fcfcSChris Mason 			     sizeof(struct btrfs_item));
261134a38218SChris Mason 	}
2612eef1c494SYan 	right_nritems -= push_items;
2613eef1c494SYan 	btrfs_set_header_nritems(right, right_nritems);
2614123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root);
26155f39d397SChris Mason 	for (i = 0; i < right_nritems; i++) {
26165f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2617db94535dSChris Mason 
2618db94535dSChris Mason 		push_space = push_space - btrfs_item_size(right, item);
2619db94535dSChris Mason 		btrfs_set_item_offset(right, item, push_space);
2620db94535dSChris Mason 	}
2621eb60ceacSChris Mason 
26225f39d397SChris Mason 	btrfs_mark_buffer_dirty(left);
262334a38218SChris Mason 	if (right_nritems)
26245f39d397SChris Mason 		btrfs_mark_buffer_dirty(right);
2625f0486c68SYan, Zheng 	else
2626f0486c68SYan, Zheng 		clean_tree_block(trans, root, right);
2627098f59c2SChris Mason 
26285f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
2629143bede5SJeff Mahoney 	fixup_low_keys(trans, root, path, &disk_key, 1);
2630be0e5c09SChris Mason 
2631be0e5c09SChris Mason 	/* then fixup the leaf pointer in the path */
2632be0e5c09SChris Mason 	if (path->slots[0] < push_items) {
2633be0e5c09SChris Mason 		path->slots[0] += old_left_nritems;
2634925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
26355f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
26365f39d397SChris Mason 		path->nodes[0] = left;
2637be0e5c09SChris Mason 		path->slots[1] -= 1;
2638be0e5c09SChris Mason 	} else {
2639925baeddSChris Mason 		btrfs_tree_unlock(left);
26405f39d397SChris Mason 		free_extent_buffer(left);
2641be0e5c09SChris Mason 		path->slots[0] -= push_items;
2642be0e5c09SChris Mason 	}
2643eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
2644aa5d6bedSChris Mason 	return ret;
2645925baeddSChris Mason out:
2646925baeddSChris Mason 	btrfs_tree_unlock(left);
2647925baeddSChris Mason 	free_extent_buffer(left);
2648925baeddSChris Mason 	return ret;
2649be0e5c09SChris Mason }
2650be0e5c09SChris Mason 
265174123bd7SChris Mason /*
265244871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
265344871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
265499d8f83cSChris Mason  *
265599d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
265699d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us push all the
265799d8f83cSChris Mason  * items
265844871b1bSChris Mason  */
265944871b1bSChris Mason static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
266099d8f83cSChris Mason 			  *root, struct btrfs_path *path, int min_data_size,
266199d8f83cSChris Mason 			  int data_size, int empty, u32 max_slot)
266244871b1bSChris Mason {
266344871b1bSChris Mason 	struct extent_buffer *right = path->nodes[0];
266444871b1bSChris Mason 	struct extent_buffer *left;
266544871b1bSChris Mason 	int slot;
266644871b1bSChris Mason 	int free_space;
266744871b1bSChris Mason 	u32 right_nritems;
266844871b1bSChris Mason 	int ret = 0;
266944871b1bSChris Mason 
267044871b1bSChris Mason 	slot = path->slots[1];
267144871b1bSChris Mason 	if (slot == 0)
267244871b1bSChris Mason 		return 1;
267344871b1bSChris Mason 	if (!path->nodes[1])
267444871b1bSChris Mason 		return 1;
267544871b1bSChris Mason 
267644871b1bSChris Mason 	right_nritems = btrfs_header_nritems(right);
267744871b1bSChris Mason 	if (right_nritems == 0)
267844871b1bSChris Mason 		return 1;
267944871b1bSChris Mason 
268044871b1bSChris Mason 	btrfs_assert_tree_locked(path->nodes[1]);
268144871b1bSChris Mason 
268244871b1bSChris Mason 	left = read_node_slot(root, path->nodes[1], slot - 1);
268391ca338dSTsutomu Itoh 	if (left == NULL)
268491ca338dSTsutomu Itoh 		return 1;
268591ca338dSTsutomu Itoh 
268644871b1bSChris Mason 	btrfs_tree_lock(left);
268744871b1bSChris Mason 	btrfs_set_lock_blocking(left);
268844871b1bSChris Mason 
268944871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, left);
269044871b1bSChris Mason 	if (free_space < data_size) {
269144871b1bSChris Mason 		ret = 1;
269244871b1bSChris Mason 		goto out;
269344871b1bSChris Mason 	}
269444871b1bSChris Mason 
269544871b1bSChris Mason 	/* cow and double check */
269644871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, left,
269744871b1bSChris Mason 			      path->nodes[1], slot - 1, &left);
269844871b1bSChris Mason 	if (ret) {
269944871b1bSChris Mason 		/* we hit -ENOSPC, but it isn't fatal here */
270044871b1bSChris Mason 		ret = 1;
270144871b1bSChris Mason 		goto out;
270244871b1bSChris Mason 	}
270344871b1bSChris Mason 
270444871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, left);
270544871b1bSChris Mason 	if (free_space < data_size) {
270644871b1bSChris Mason 		ret = 1;
270744871b1bSChris Mason 		goto out;
270844871b1bSChris Mason 	}
270944871b1bSChris Mason 
271099d8f83cSChris Mason 	return __push_leaf_left(trans, root, path, min_data_size,
271199d8f83cSChris Mason 			       empty, left, free_space, right_nritems,
271299d8f83cSChris Mason 			       max_slot);
271344871b1bSChris Mason out:
271444871b1bSChris Mason 	btrfs_tree_unlock(left);
271544871b1bSChris Mason 	free_extent_buffer(left);
271644871b1bSChris Mason 	return ret;
271744871b1bSChris Mason }
271844871b1bSChris Mason 
271944871b1bSChris Mason /*
272074123bd7SChris Mason  * split the path's leaf in two, making sure there is at least data_size
272174123bd7SChris Mason  * available for the resulting leaf level of the path.
272274123bd7SChris Mason  */
2723143bede5SJeff Mahoney static noinline void copy_for_split(struct btrfs_trans_handle *trans,
2724e02119d5SChris Mason 				    struct btrfs_root *root,
272544871b1bSChris Mason 				    struct btrfs_path *path,
272644871b1bSChris Mason 				    struct extent_buffer *l,
272744871b1bSChris Mason 				    struct extent_buffer *right,
272844871b1bSChris Mason 				    int slot, int mid, int nritems)
2729be0e5c09SChris Mason {
2730be0e5c09SChris Mason 	int data_copy_size;
2731be0e5c09SChris Mason 	int rt_data_off;
2732be0e5c09SChris Mason 	int i;
2733d4dbff95SChris Mason 	struct btrfs_disk_key disk_key;
2734be0e5c09SChris Mason 
27355f39d397SChris Mason 	nritems = nritems - mid;
27365f39d397SChris Mason 	btrfs_set_header_nritems(right, nritems);
27375f39d397SChris Mason 	data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
27385f39d397SChris Mason 
27395f39d397SChris Mason 	copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
27405f39d397SChris Mason 			   btrfs_item_nr_offset(mid),
27415f39d397SChris Mason 			   nritems * sizeof(struct btrfs_item));
27425f39d397SChris Mason 
27435f39d397SChris Mason 	copy_extent_buffer(right, l,
2744d6025579SChris Mason 		     btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2745123abc88SChris Mason 		     data_copy_size, btrfs_leaf_data(l) +
2746123abc88SChris Mason 		     leaf_data_end(root, l), data_copy_size);
274774123bd7SChris Mason 
27485f39d397SChris Mason 	rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
27495f39d397SChris Mason 		      btrfs_item_end_nr(l, mid);
27505f39d397SChris Mason 
27515f39d397SChris Mason 	for (i = 0; i < nritems; i++) {
27525f39d397SChris Mason 		struct btrfs_item *item = btrfs_item_nr(right, i);
2753db94535dSChris Mason 		u32 ioff;
2754db94535dSChris Mason 
2755db94535dSChris Mason 		ioff = btrfs_item_offset(right, item);
27565f39d397SChris Mason 		btrfs_set_item_offset(right, item, ioff + rt_data_off);
27570783fcfcSChris Mason 	}
275874123bd7SChris Mason 
27595f39d397SChris Mason 	btrfs_set_header_nritems(l, mid);
27605f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
2761143bede5SJeff Mahoney 	insert_ptr(trans, root, path, &disk_key, right->start,
2762db94535dSChris Mason 		   path->slots[1] + 1, 1);
27635f39d397SChris Mason 
27645f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
27655f39d397SChris Mason 	btrfs_mark_buffer_dirty(l);
2766eb60ceacSChris Mason 	BUG_ON(path->slots[0] != slot);
27675f39d397SChris Mason 
2768be0e5c09SChris Mason 	if (mid <= slot) {
2769925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
27705f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
27715f39d397SChris Mason 		path->nodes[0] = right;
2772be0e5c09SChris Mason 		path->slots[0] -= mid;
2773be0e5c09SChris Mason 		path->slots[1] += 1;
2774925baeddSChris Mason 	} else {
2775925baeddSChris Mason 		btrfs_tree_unlock(right);
27765f39d397SChris Mason 		free_extent_buffer(right);
2777925baeddSChris Mason 	}
27785f39d397SChris Mason 
2779eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
278044871b1bSChris Mason }
278144871b1bSChris Mason 
278244871b1bSChris Mason /*
278399d8f83cSChris Mason  * double splits happen when we need to insert a big item in the middle
278499d8f83cSChris Mason  * of a leaf.  A double split can leave us with 3 mostly empty leaves:
278599d8f83cSChris Mason  * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
278699d8f83cSChris Mason  *          A                 B                 C
278799d8f83cSChris Mason  *
278899d8f83cSChris Mason  * We avoid this by trying to push the items on either side of our target
278999d8f83cSChris Mason  * into the adjacent leaves.  If all goes well we can avoid the double split
279099d8f83cSChris Mason  * completely.
279199d8f83cSChris Mason  */
279299d8f83cSChris Mason static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
279399d8f83cSChris Mason 					  struct btrfs_root *root,
279499d8f83cSChris Mason 					  struct btrfs_path *path,
279599d8f83cSChris Mason 					  int data_size)
279699d8f83cSChris Mason {
279799d8f83cSChris Mason 	int ret;
279899d8f83cSChris Mason 	int progress = 0;
279999d8f83cSChris Mason 	int slot;
280099d8f83cSChris Mason 	u32 nritems;
280199d8f83cSChris Mason 
280299d8f83cSChris Mason 	slot = path->slots[0];
280399d8f83cSChris Mason 
280499d8f83cSChris Mason 	/*
280599d8f83cSChris Mason 	 * try to push all the items after our slot into the
280699d8f83cSChris Mason 	 * right leaf
280799d8f83cSChris Mason 	 */
280899d8f83cSChris Mason 	ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
280999d8f83cSChris Mason 	if (ret < 0)
281099d8f83cSChris Mason 		return ret;
281199d8f83cSChris Mason 
281299d8f83cSChris Mason 	if (ret == 0)
281399d8f83cSChris Mason 		progress++;
281499d8f83cSChris Mason 
281599d8f83cSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
281699d8f83cSChris Mason 	/*
281799d8f83cSChris Mason 	 * our goal is to get our slot at the start or end of a leaf.  If
281899d8f83cSChris Mason 	 * we've done so we're done
281999d8f83cSChris Mason 	 */
282099d8f83cSChris Mason 	if (path->slots[0] == 0 || path->slots[0] == nritems)
282199d8f83cSChris Mason 		return 0;
282299d8f83cSChris Mason 
282399d8f83cSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
282499d8f83cSChris Mason 		return 0;
282599d8f83cSChris Mason 
282699d8f83cSChris Mason 	/* try to push all the items before our slot into the next leaf */
282799d8f83cSChris Mason 	slot = path->slots[0];
282899d8f83cSChris Mason 	ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
282999d8f83cSChris Mason 	if (ret < 0)
283099d8f83cSChris Mason 		return ret;
283199d8f83cSChris Mason 
283299d8f83cSChris Mason 	if (ret == 0)
283399d8f83cSChris Mason 		progress++;
283499d8f83cSChris Mason 
283599d8f83cSChris Mason 	if (progress)
283699d8f83cSChris Mason 		return 0;
283799d8f83cSChris Mason 	return 1;
283899d8f83cSChris Mason }
283999d8f83cSChris Mason 
284099d8f83cSChris Mason /*
284144871b1bSChris Mason  * split the path's leaf in two, making sure there is at least data_size
284244871b1bSChris Mason  * available for the resulting leaf level of the path.
284344871b1bSChris Mason  *
284444871b1bSChris Mason  * returns 0 if all went well and < 0 on failure.
284544871b1bSChris Mason  */
284644871b1bSChris Mason static noinline int split_leaf(struct btrfs_trans_handle *trans,
284744871b1bSChris Mason 			       struct btrfs_root *root,
284844871b1bSChris Mason 			       struct btrfs_key *ins_key,
284944871b1bSChris Mason 			       struct btrfs_path *path, int data_size,
285044871b1bSChris Mason 			       int extend)
285144871b1bSChris Mason {
28525d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
285344871b1bSChris Mason 	struct extent_buffer *l;
285444871b1bSChris Mason 	u32 nritems;
285544871b1bSChris Mason 	int mid;
285644871b1bSChris Mason 	int slot;
285744871b1bSChris Mason 	struct extent_buffer *right;
285844871b1bSChris Mason 	int ret = 0;
285944871b1bSChris Mason 	int wret;
28605d4f98a2SYan Zheng 	int split;
286144871b1bSChris Mason 	int num_doubles = 0;
286299d8f83cSChris Mason 	int tried_avoid_double = 0;
286344871b1bSChris Mason 
2864a5719521SYan, Zheng 	l = path->nodes[0];
2865a5719521SYan, Zheng 	slot = path->slots[0];
2866a5719521SYan, Zheng 	if (extend && data_size + btrfs_item_size_nr(l, slot) +
2867a5719521SYan, Zheng 	    sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2868a5719521SYan, Zheng 		return -EOVERFLOW;
2869a5719521SYan, Zheng 
287044871b1bSChris Mason 	/* first try to make some room by pushing left and right */
287199d8f83cSChris Mason 	if (data_size) {
287299d8f83cSChris Mason 		wret = push_leaf_right(trans, root, path, data_size,
287399d8f83cSChris Mason 				       data_size, 0, 0);
287444871b1bSChris Mason 		if (wret < 0)
287544871b1bSChris Mason 			return wret;
287644871b1bSChris Mason 		if (wret) {
287799d8f83cSChris Mason 			wret = push_leaf_left(trans, root, path, data_size,
287899d8f83cSChris Mason 					      data_size, 0, (u32)-1);
287944871b1bSChris Mason 			if (wret < 0)
288044871b1bSChris Mason 				return wret;
288144871b1bSChris Mason 		}
288244871b1bSChris Mason 		l = path->nodes[0];
288344871b1bSChris Mason 
288444871b1bSChris Mason 		/* did the pushes work? */
288544871b1bSChris Mason 		if (btrfs_leaf_free_space(root, l) >= data_size)
288644871b1bSChris Mason 			return 0;
288744871b1bSChris Mason 	}
288844871b1bSChris Mason 
288944871b1bSChris Mason 	if (!path->nodes[1]) {
289044871b1bSChris Mason 		ret = insert_new_root(trans, root, path, 1);
289144871b1bSChris Mason 		if (ret)
289244871b1bSChris Mason 			return ret;
289344871b1bSChris Mason 	}
289444871b1bSChris Mason again:
28955d4f98a2SYan Zheng 	split = 1;
289644871b1bSChris Mason 	l = path->nodes[0];
289744871b1bSChris Mason 	slot = path->slots[0];
289844871b1bSChris Mason 	nritems = btrfs_header_nritems(l);
289944871b1bSChris Mason 	mid = (nritems + 1) / 2;
290044871b1bSChris Mason 
29015d4f98a2SYan Zheng 	if (mid <= slot) {
29025d4f98a2SYan Zheng 		if (nritems == 1 ||
29035d4f98a2SYan Zheng 		    leaf_space_used(l, mid, nritems - mid) + data_size >
29045d4f98a2SYan Zheng 			BTRFS_LEAF_DATA_SIZE(root)) {
29055d4f98a2SYan Zheng 			if (slot >= nritems) {
29065d4f98a2SYan Zheng 				split = 0;
29075d4f98a2SYan Zheng 			} else {
29085d4f98a2SYan Zheng 				mid = slot;
29095d4f98a2SYan Zheng 				if (mid != nritems &&
29105d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
29115d4f98a2SYan Zheng 				    data_size > BTRFS_LEAF_DATA_SIZE(root)) {
291299d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
291399d8f83cSChris Mason 						goto push_for_double;
29145d4f98a2SYan Zheng 					split = 2;
29155d4f98a2SYan Zheng 				}
29165d4f98a2SYan Zheng 			}
29175d4f98a2SYan Zheng 		}
29185d4f98a2SYan Zheng 	} else {
29195d4f98a2SYan Zheng 		if (leaf_space_used(l, 0, mid) + data_size >
29205d4f98a2SYan Zheng 			BTRFS_LEAF_DATA_SIZE(root)) {
29215d4f98a2SYan Zheng 			if (!extend && data_size && slot == 0) {
29225d4f98a2SYan Zheng 				split = 0;
29235d4f98a2SYan Zheng 			} else if ((extend || !data_size) && slot == 0) {
29245d4f98a2SYan Zheng 				mid = 1;
29255d4f98a2SYan Zheng 			} else {
29265d4f98a2SYan Zheng 				mid = slot;
29275d4f98a2SYan Zheng 				if (mid != nritems &&
29285d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
29295d4f98a2SYan Zheng 				    data_size > BTRFS_LEAF_DATA_SIZE(root)) {
293099d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
293199d8f83cSChris Mason 						goto push_for_double;
29325d4f98a2SYan Zheng 					split = 2 ;
29335d4f98a2SYan Zheng 				}
29345d4f98a2SYan Zheng 			}
29355d4f98a2SYan Zheng 		}
29365d4f98a2SYan Zheng 	}
29375d4f98a2SYan Zheng 
29385d4f98a2SYan Zheng 	if (split == 0)
29395d4f98a2SYan Zheng 		btrfs_cpu_key_to_disk(&disk_key, ins_key);
29405d4f98a2SYan Zheng 	else
29415d4f98a2SYan Zheng 		btrfs_item_key(l, &disk_key, mid);
29425d4f98a2SYan Zheng 
29435d4f98a2SYan Zheng 	right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
294444871b1bSChris Mason 					root->root_key.objectid,
294566d7e7f0SArne Jansen 					&disk_key, 0, l->start, 0, 0);
2946f0486c68SYan, Zheng 	if (IS_ERR(right))
294744871b1bSChris Mason 		return PTR_ERR(right);
2948f0486c68SYan, Zheng 
2949f0486c68SYan, Zheng 	root_add_used(root, root->leafsize);
295044871b1bSChris Mason 
295144871b1bSChris Mason 	memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
295244871b1bSChris Mason 	btrfs_set_header_bytenr(right, right->start);
295344871b1bSChris Mason 	btrfs_set_header_generation(right, trans->transid);
29545d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
295544871b1bSChris Mason 	btrfs_set_header_owner(right, root->root_key.objectid);
295644871b1bSChris Mason 	btrfs_set_header_level(right, 0);
295744871b1bSChris Mason 	write_extent_buffer(right, root->fs_info->fsid,
295844871b1bSChris Mason 			    (unsigned long)btrfs_header_fsid(right),
295944871b1bSChris Mason 			    BTRFS_FSID_SIZE);
296044871b1bSChris Mason 
296144871b1bSChris Mason 	write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
296244871b1bSChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(right),
296344871b1bSChris Mason 			    BTRFS_UUID_SIZE);
296444871b1bSChris Mason 
29655d4f98a2SYan Zheng 	if (split == 0) {
296644871b1bSChris Mason 		if (mid <= slot) {
296744871b1bSChris Mason 			btrfs_set_header_nritems(right, 0);
2968143bede5SJeff Mahoney 			insert_ptr(trans, root, path, &disk_key, right->start,
296944871b1bSChris Mason 				   path->slots[1] + 1, 1);
297044871b1bSChris Mason 			btrfs_tree_unlock(path->nodes[0]);
297144871b1bSChris Mason 			free_extent_buffer(path->nodes[0]);
297244871b1bSChris Mason 			path->nodes[0] = right;
297344871b1bSChris Mason 			path->slots[0] = 0;
297444871b1bSChris Mason 			path->slots[1] += 1;
297544871b1bSChris Mason 		} else {
297644871b1bSChris Mason 			btrfs_set_header_nritems(right, 0);
2977143bede5SJeff Mahoney 			insert_ptr(trans, root, path, &disk_key, right->start,
297844871b1bSChris Mason 					  path->slots[1], 1);
297944871b1bSChris Mason 			btrfs_tree_unlock(path->nodes[0]);
298044871b1bSChris Mason 			free_extent_buffer(path->nodes[0]);
298144871b1bSChris Mason 			path->nodes[0] = right;
298244871b1bSChris Mason 			path->slots[0] = 0;
2983143bede5SJeff Mahoney 			if (path->slots[1] == 0)
2984143bede5SJeff Mahoney 				fixup_low_keys(trans, root, path,
2985143bede5SJeff Mahoney 					       &disk_key, 1);
29865d4f98a2SYan Zheng 		}
298744871b1bSChris Mason 		btrfs_mark_buffer_dirty(right);
298844871b1bSChris Mason 		return ret;
298944871b1bSChris Mason 	}
299044871b1bSChris Mason 
2991143bede5SJeff Mahoney 	copy_for_split(trans, root, path, l, right, slot, mid, nritems);
299244871b1bSChris Mason 
29935d4f98a2SYan Zheng 	if (split == 2) {
2994cc0c5538SChris Mason 		BUG_ON(num_doubles != 0);
2995cc0c5538SChris Mason 		num_doubles++;
2996cc0c5538SChris Mason 		goto again;
29973326d1b0SChris Mason 	}
299844871b1bSChris Mason 
2999143bede5SJeff Mahoney 	return 0;
300099d8f83cSChris Mason 
300199d8f83cSChris Mason push_for_double:
300299d8f83cSChris Mason 	push_for_double_split(trans, root, path, data_size);
300399d8f83cSChris Mason 	tried_avoid_double = 1;
300499d8f83cSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
300599d8f83cSChris Mason 		return 0;
300699d8f83cSChris Mason 	goto again;
3007be0e5c09SChris Mason }
3008be0e5c09SChris Mason 
3009ad48fd75SYan, Zheng static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3010ad48fd75SYan, Zheng 					 struct btrfs_root *root,
3011ad48fd75SYan, Zheng 					 struct btrfs_path *path, int ins_len)
3012ad48fd75SYan, Zheng {
3013ad48fd75SYan, Zheng 	struct btrfs_key key;
3014ad48fd75SYan, Zheng 	struct extent_buffer *leaf;
3015ad48fd75SYan, Zheng 	struct btrfs_file_extent_item *fi;
3016ad48fd75SYan, Zheng 	u64 extent_len = 0;
3017ad48fd75SYan, Zheng 	u32 item_size;
3018ad48fd75SYan, Zheng 	int ret;
3019ad48fd75SYan, Zheng 
3020ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3021ad48fd75SYan, Zheng 	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3022ad48fd75SYan, Zheng 
3023ad48fd75SYan, Zheng 	BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3024ad48fd75SYan, Zheng 	       key.type != BTRFS_EXTENT_CSUM_KEY);
3025ad48fd75SYan, Zheng 
3026ad48fd75SYan, Zheng 	if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3027ad48fd75SYan, Zheng 		return 0;
3028ad48fd75SYan, Zheng 
3029ad48fd75SYan, Zheng 	item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3030ad48fd75SYan, Zheng 	if (key.type == BTRFS_EXTENT_DATA_KEY) {
3031ad48fd75SYan, Zheng 		fi = btrfs_item_ptr(leaf, path->slots[0],
3032ad48fd75SYan, Zheng 				    struct btrfs_file_extent_item);
3033ad48fd75SYan, Zheng 		extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3034ad48fd75SYan, Zheng 	}
3035b3b4aa74SDavid Sterba 	btrfs_release_path(path);
3036ad48fd75SYan, Zheng 
3037ad48fd75SYan, Zheng 	path->keep_locks = 1;
3038ad48fd75SYan, Zheng 	path->search_for_split = 1;
3039ad48fd75SYan, Zheng 	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3040ad48fd75SYan, Zheng 	path->search_for_split = 0;
3041ad48fd75SYan, Zheng 	if (ret < 0)
3042ad48fd75SYan, Zheng 		goto err;
3043ad48fd75SYan, Zheng 
3044ad48fd75SYan, Zheng 	ret = -EAGAIN;
3045ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3046ad48fd75SYan, Zheng 	/* if our item isn't there or got smaller, return now */
3047ad48fd75SYan, Zheng 	if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3048ad48fd75SYan, Zheng 		goto err;
3049ad48fd75SYan, Zheng 
3050109f6aefSChris Mason 	/* the leaf has  changed, it now has room.  return now */
3051109f6aefSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3052109f6aefSChris Mason 		goto err;
3053109f6aefSChris Mason 
3054ad48fd75SYan, Zheng 	if (key.type == BTRFS_EXTENT_DATA_KEY) {
3055ad48fd75SYan, Zheng 		fi = btrfs_item_ptr(leaf, path->slots[0],
3056ad48fd75SYan, Zheng 				    struct btrfs_file_extent_item);
3057ad48fd75SYan, Zheng 		if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3058ad48fd75SYan, Zheng 			goto err;
3059ad48fd75SYan, Zheng 	}
3060ad48fd75SYan, Zheng 
3061ad48fd75SYan, Zheng 	btrfs_set_path_blocking(path);
3062ad48fd75SYan, Zheng 	ret = split_leaf(trans, root, &key, path, ins_len, 1);
3063f0486c68SYan, Zheng 	if (ret)
3064f0486c68SYan, Zheng 		goto err;
3065ad48fd75SYan, Zheng 
3066ad48fd75SYan, Zheng 	path->keep_locks = 0;
3067ad48fd75SYan, Zheng 	btrfs_unlock_up_safe(path, 1);
3068ad48fd75SYan, Zheng 	return 0;
3069ad48fd75SYan, Zheng err:
3070ad48fd75SYan, Zheng 	path->keep_locks = 0;
3071ad48fd75SYan, Zheng 	return ret;
3072ad48fd75SYan, Zheng }
3073ad48fd75SYan, Zheng 
3074ad48fd75SYan, Zheng static noinline int split_item(struct btrfs_trans_handle *trans,
3075459931ecSChris Mason 			       struct btrfs_root *root,
3076459931ecSChris Mason 			       struct btrfs_path *path,
3077459931ecSChris Mason 			       struct btrfs_key *new_key,
3078459931ecSChris Mason 			       unsigned long split_offset)
3079459931ecSChris Mason {
3080459931ecSChris Mason 	struct extent_buffer *leaf;
3081459931ecSChris Mason 	struct btrfs_item *item;
3082459931ecSChris Mason 	struct btrfs_item *new_item;
3083459931ecSChris Mason 	int slot;
3084ad48fd75SYan, Zheng 	char *buf;
3085459931ecSChris Mason 	u32 nritems;
3086ad48fd75SYan, Zheng 	u32 item_size;
3087459931ecSChris Mason 	u32 orig_offset;
3088459931ecSChris Mason 	struct btrfs_disk_key disk_key;
3089459931ecSChris Mason 
3090459931ecSChris Mason 	leaf = path->nodes[0];
3091b9473439SChris Mason 	BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3092b9473439SChris Mason 
3093b4ce94deSChris Mason 	btrfs_set_path_blocking(path);
3094b4ce94deSChris Mason 
3095459931ecSChris Mason 	item = btrfs_item_nr(leaf, path->slots[0]);
3096459931ecSChris Mason 	orig_offset = btrfs_item_offset(leaf, item);
3097459931ecSChris Mason 	item_size = btrfs_item_size(leaf, item);
3098459931ecSChris Mason 
3099459931ecSChris Mason 	buf = kmalloc(item_size, GFP_NOFS);
3100ad48fd75SYan, Zheng 	if (!buf)
3101ad48fd75SYan, Zheng 		return -ENOMEM;
3102ad48fd75SYan, Zheng 
3103459931ecSChris Mason 	read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3104459931ecSChris Mason 			    path->slots[0]), item_size);
3105ad48fd75SYan, Zheng 
3106459931ecSChris Mason 	slot = path->slots[0] + 1;
3107459931ecSChris Mason 	nritems = btrfs_header_nritems(leaf);
3108459931ecSChris Mason 	if (slot != nritems) {
3109459931ecSChris Mason 		/* shift the items */
3110459931ecSChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3111459931ecSChris Mason 				btrfs_item_nr_offset(slot),
3112459931ecSChris Mason 				(nritems - slot) * sizeof(struct btrfs_item));
3113459931ecSChris Mason 	}
3114459931ecSChris Mason 
3115459931ecSChris Mason 	btrfs_cpu_key_to_disk(&disk_key, new_key);
3116459931ecSChris Mason 	btrfs_set_item_key(leaf, &disk_key, slot);
3117459931ecSChris Mason 
3118459931ecSChris Mason 	new_item = btrfs_item_nr(leaf, slot);
3119459931ecSChris Mason 
3120459931ecSChris Mason 	btrfs_set_item_offset(leaf, new_item, orig_offset);
3121459931ecSChris Mason 	btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3122459931ecSChris Mason 
3123459931ecSChris Mason 	btrfs_set_item_offset(leaf, item,
3124459931ecSChris Mason 			      orig_offset + item_size - split_offset);
3125459931ecSChris Mason 	btrfs_set_item_size(leaf, item, split_offset);
3126459931ecSChris Mason 
3127459931ecSChris Mason 	btrfs_set_header_nritems(leaf, nritems + 1);
3128459931ecSChris Mason 
3129459931ecSChris Mason 	/* write the data for the start of the original item */
3130459931ecSChris Mason 	write_extent_buffer(leaf, buf,
3131459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, path->slots[0]),
3132459931ecSChris Mason 			    split_offset);
3133459931ecSChris Mason 
3134459931ecSChris Mason 	/* write the data for the new item */
3135459931ecSChris Mason 	write_extent_buffer(leaf, buf + split_offset,
3136459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, slot),
3137459931ecSChris Mason 			    item_size - split_offset);
3138459931ecSChris Mason 	btrfs_mark_buffer_dirty(leaf);
3139459931ecSChris Mason 
3140ad48fd75SYan, Zheng 	BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
3141459931ecSChris Mason 	kfree(buf);
3142ad48fd75SYan, Zheng 	return 0;
3143ad48fd75SYan, Zheng }
3144ad48fd75SYan, Zheng 
3145ad48fd75SYan, Zheng /*
3146ad48fd75SYan, Zheng  * This function splits a single item into two items,
3147ad48fd75SYan, Zheng  * giving 'new_key' to the new item and splitting the
3148ad48fd75SYan, Zheng  * old one at split_offset (from the start of the item).
3149ad48fd75SYan, Zheng  *
3150ad48fd75SYan, Zheng  * The path may be released by this operation.  After
3151ad48fd75SYan, Zheng  * the split, the path is pointing to the old item.  The
3152ad48fd75SYan, Zheng  * new item is going to be in the same node as the old one.
3153ad48fd75SYan, Zheng  *
3154ad48fd75SYan, Zheng  * Note, the item being split must be smaller enough to live alone on
3155ad48fd75SYan, Zheng  * a tree block with room for one extra struct btrfs_item
3156ad48fd75SYan, Zheng  *
3157ad48fd75SYan, Zheng  * This allows us to split the item in place, keeping a lock on the
3158ad48fd75SYan, Zheng  * leaf the entire time.
3159ad48fd75SYan, Zheng  */
3160ad48fd75SYan, Zheng int btrfs_split_item(struct btrfs_trans_handle *trans,
3161ad48fd75SYan, Zheng 		     struct btrfs_root *root,
3162ad48fd75SYan, Zheng 		     struct btrfs_path *path,
3163ad48fd75SYan, Zheng 		     struct btrfs_key *new_key,
3164ad48fd75SYan, Zheng 		     unsigned long split_offset)
3165ad48fd75SYan, Zheng {
3166ad48fd75SYan, Zheng 	int ret;
3167ad48fd75SYan, Zheng 	ret = setup_leaf_for_split(trans, root, path,
3168ad48fd75SYan, Zheng 				   sizeof(struct btrfs_item));
3169ad48fd75SYan, Zheng 	if (ret)
3170459931ecSChris Mason 		return ret;
3171ad48fd75SYan, Zheng 
3172ad48fd75SYan, Zheng 	ret = split_item(trans, root, path, new_key, split_offset);
3173ad48fd75SYan, Zheng 	return ret;
3174ad48fd75SYan, Zheng }
3175ad48fd75SYan, Zheng 
3176ad48fd75SYan, Zheng /*
3177ad48fd75SYan, Zheng  * This function duplicate a item, giving 'new_key' to the new item.
3178ad48fd75SYan, Zheng  * It guarantees both items live in the same tree leaf and the new item
3179ad48fd75SYan, Zheng  * is contiguous with the original item.
3180ad48fd75SYan, Zheng  *
3181ad48fd75SYan, Zheng  * This allows us to split file extent in place, keeping a lock on the
3182ad48fd75SYan, Zheng  * leaf the entire time.
3183ad48fd75SYan, Zheng  */
3184ad48fd75SYan, Zheng int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3185ad48fd75SYan, Zheng 			 struct btrfs_root *root,
3186ad48fd75SYan, Zheng 			 struct btrfs_path *path,
3187ad48fd75SYan, Zheng 			 struct btrfs_key *new_key)
3188ad48fd75SYan, Zheng {
3189ad48fd75SYan, Zheng 	struct extent_buffer *leaf;
3190ad48fd75SYan, Zheng 	int ret;
3191ad48fd75SYan, Zheng 	u32 item_size;
3192ad48fd75SYan, Zheng 
3193ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3194ad48fd75SYan, Zheng 	item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3195ad48fd75SYan, Zheng 	ret = setup_leaf_for_split(trans, root, path,
3196ad48fd75SYan, Zheng 				   item_size + sizeof(struct btrfs_item));
3197ad48fd75SYan, Zheng 	if (ret)
3198ad48fd75SYan, Zheng 		return ret;
3199ad48fd75SYan, Zheng 
3200ad48fd75SYan, Zheng 	path->slots[0]++;
3201143bede5SJeff Mahoney 	setup_items_for_insert(trans, root, path, new_key, &item_size,
3202ad48fd75SYan, Zheng 			       item_size, item_size +
3203ad48fd75SYan, Zheng 			       sizeof(struct btrfs_item), 1);
3204ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3205ad48fd75SYan, Zheng 	memcpy_extent_buffer(leaf,
3206ad48fd75SYan, Zheng 			     btrfs_item_ptr_offset(leaf, path->slots[0]),
3207ad48fd75SYan, Zheng 			     btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3208ad48fd75SYan, Zheng 			     item_size);
3209ad48fd75SYan, Zheng 	return 0;
3210459931ecSChris Mason }
3211459931ecSChris Mason 
3212459931ecSChris Mason /*
3213d352ac68SChris Mason  * make the item pointed to by the path smaller.  new_size indicates
3214d352ac68SChris Mason  * how small to make it, and from_end tells us if we just chop bytes
3215d352ac68SChris Mason  * off the end of the item or if we shift the item to chop bytes off
3216d352ac68SChris Mason  * the front.
3217d352ac68SChris Mason  */
3218143bede5SJeff Mahoney void btrfs_truncate_item(struct btrfs_trans_handle *trans,
3219b18c6685SChris Mason 			 struct btrfs_root *root,
3220b18c6685SChris Mason 			 struct btrfs_path *path,
3221179e29e4SChris Mason 			 u32 new_size, int from_end)
3222b18c6685SChris Mason {
3223b18c6685SChris Mason 	int slot;
32245f39d397SChris Mason 	struct extent_buffer *leaf;
32255f39d397SChris Mason 	struct btrfs_item *item;
3226b18c6685SChris Mason 	u32 nritems;
3227b18c6685SChris Mason 	unsigned int data_end;
3228b18c6685SChris Mason 	unsigned int old_data_start;
3229b18c6685SChris Mason 	unsigned int old_size;
3230b18c6685SChris Mason 	unsigned int size_diff;
3231b18c6685SChris Mason 	int i;
3232b18c6685SChris Mason 
32335f39d397SChris Mason 	leaf = path->nodes[0];
3234179e29e4SChris Mason 	slot = path->slots[0];
3235179e29e4SChris Mason 
3236179e29e4SChris Mason 	old_size = btrfs_item_size_nr(leaf, slot);
3237179e29e4SChris Mason 	if (old_size == new_size)
3238143bede5SJeff Mahoney 		return;
3239b18c6685SChris Mason 
32405f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3241b18c6685SChris Mason 	data_end = leaf_data_end(root, leaf);
3242b18c6685SChris Mason 
32435f39d397SChris Mason 	old_data_start = btrfs_item_offset_nr(leaf, slot);
3244179e29e4SChris Mason 
3245b18c6685SChris Mason 	size_diff = old_size - new_size;
3246b18c6685SChris Mason 
3247b18c6685SChris Mason 	BUG_ON(slot < 0);
3248b18c6685SChris Mason 	BUG_ON(slot >= nritems);
3249b18c6685SChris Mason 
3250b18c6685SChris Mason 	/*
3251b18c6685SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3252b18c6685SChris Mason 	 */
3253b18c6685SChris Mason 	/* first correct the data pointers */
3254b18c6685SChris Mason 	for (i = slot; i < nritems; i++) {
32555f39d397SChris Mason 		u32 ioff;
32565f39d397SChris Mason 		item = btrfs_item_nr(leaf, i);
3257db94535dSChris Mason 
32585f39d397SChris Mason 		ioff = btrfs_item_offset(leaf, item);
32595f39d397SChris Mason 		btrfs_set_item_offset(leaf, item, ioff + size_diff);
3260b18c6685SChris Mason 	}
3261db94535dSChris Mason 
3262b18c6685SChris Mason 	/* shift the data */
3263179e29e4SChris Mason 	if (from_end) {
32645f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3265b18c6685SChris Mason 			      data_end + size_diff, btrfs_leaf_data(leaf) +
3266b18c6685SChris Mason 			      data_end, old_data_start + new_size - data_end);
3267179e29e4SChris Mason 	} else {
3268179e29e4SChris Mason 		struct btrfs_disk_key disk_key;
3269179e29e4SChris Mason 		u64 offset;
3270179e29e4SChris Mason 
3271179e29e4SChris Mason 		btrfs_item_key(leaf, &disk_key, slot);
3272179e29e4SChris Mason 
3273179e29e4SChris Mason 		if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3274179e29e4SChris Mason 			unsigned long ptr;
3275179e29e4SChris Mason 			struct btrfs_file_extent_item *fi;
3276179e29e4SChris Mason 
3277179e29e4SChris Mason 			fi = btrfs_item_ptr(leaf, slot,
3278179e29e4SChris Mason 					    struct btrfs_file_extent_item);
3279179e29e4SChris Mason 			fi = (struct btrfs_file_extent_item *)(
3280179e29e4SChris Mason 			     (unsigned long)fi - size_diff);
3281179e29e4SChris Mason 
3282179e29e4SChris Mason 			if (btrfs_file_extent_type(leaf, fi) ==
3283179e29e4SChris Mason 			    BTRFS_FILE_EXTENT_INLINE) {
3284179e29e4SChris Mason 				ptr = btrfs_item_ptr_offset(leaf, slot);
3285179e29e4SChris Mason 				memmove_extent_buffer(leaf, ptr,
3286179e29e4SChris Mason 				      (unsigned long)fi,
3287179e29e4SChris Mason 				      offsetof(struct btrfs_file_extent_item,
3288179e29e4SChris Mason 						 disk_bytenr));
3289179e29e4SChris Mason 			}
3290179e29e4SChris Mason 		}
3291179e29e4SChris Mason 
3292179e29e4SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3293179e29e4SChris Mason 			      data_end + size_diff, btrfs_leaf_data(leaf) +
3294179e29e4SChris Mason 			      data_end, old_data_start - data_end);
3295179e29e4SChris Mason 
3296179e29e4SChris Mason 		offset = btrfs_disk_key_offset(&disk_key);
3297179e29e4SChris Mason 		btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3298179e29e4SChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot);
3299179e29e4SChris Mason 		if (slot == 0)
3300179e29e4SChris Mason 			fixup_low_keys(trans, root, path, &disk_key, 1);
3301179e29e4SChris Mason 	}
33025f39d397SChris Mason 
33035f39d397SChris Mason 	item = btrfs_item_nr(leaf, slot);
33045f39d397SChris Mason 	btrfs_set_item_size(leaf, item, new_size);
33055f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
3306b18c6685SChris Mason 
33075f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
33085f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
3309b18c6685SChris Mason 		BUG();
33105f39d397SChris Mason 	}
3311b18c6685SChris Mason }
3312b18c6685SChris Mason 
3313d352ac68SChris Mason /*
3314d352ac68SChris Mason  * make the item pointed to by the path bigger, data_size is the new size.
3315d352ac68SChris Mason  */
3316143bede5SJeff Mahoney void btrfs_extend_item(struct btrfs_trans_handle *trans,
33175f39d397SChris Mason 		       struct btrfs_root *root, struct btrfs_path *path,
33185f39d397SChris Mason 		       u32 data_size)
33196567e837SChris Mason {
33206567e837SChris Mason 	int slot;
33215f39d397SChris Mason 	struct extent_buffer *leaf;
33225f39d397SChris Mason 	struct btrfs_item *item;
33236567e837SChris Mason 	u32 nritems;
33246567e837SChris Mason 	unsigned int data_end;
33256567e837SChris Mason 	unsigned int old_data;
33266567e837SChris Mason 	unsigned int old_size;
33276567e837SChris Mason 	int i;
33286567e837SChris Mason 
33295f39d397SChris Mason 	leaf = path->nodes[0];
33306567e837SChris Mason 
33315f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
33326567e837SChris Mason 	data_end = leaf_data_end(root, leaf);
33336567e837SChris Mason 
33345f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < data_size) {
33355f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
33366567e837SChris Mason 		BUG();
33375f39d397SChris Mason 	}
33386567e837SChris Mason 	slot = path->slots[0];
33395f39d397SChris Mason 	old_data = btrfs_item_end_nr(leaf, slot);
33406567e837SChris Mason 
33416567e837SChris Mason 	BUG_ON(slot < 0);
33423326d1b0SChris Mason 	if (slot >= nritems) {
33433326d1b0SChris Mason 		btrfs_print_leaf(root, leaf);
3344d397712bSChris Mason 		printk(KERN_CRIT "slot %d too large, nritems %d\n",
3345d397712bSChris Mason 		       slot, nritems);
33463326d1b0SChris Mason 		BUG_ON(1);
33473326d1b0SChris Mason 	}
33486567e837SChris Mason 
33496567e837SChris Mason 	/*
33506567e837SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
33516567e837SChris Mason 	 */
33526567e837SChris Mason 	/* first correct the data pointers */
33536567e837SChris Mason 	for (i = slot; i < nritems; i++) {
33545f39d397SChris Mason 		u32 ioff;
33555f39d397SChris Mason 		item = btrfs_item_nr(leaf, i);
3356db94535dSChris Mason 
33575f39d397SChris Mason 		ioff = btrfs_item_offset(leaf, item);
33585f39d397SChris Mason 		btrfs_set_item_offset(leaf, item, ioff - data_size);
33596567e837SChris Mason 	}
33605f39d397SChris Mason 
33616567e837SChris Mason 	/* shift the data */
33625f39d397SChris Mason 	memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
33636567e837SChris Mason 		      data_end - data_size, btrfs_leaf_data(leaf) +
33646567e837SChris Mason 		      data_end, old_data - data_end);
33655f39d397SChris Mason 
33666567e837SChris Mason 	data_end = old_data;
33675f39d397SChris Mason 	old_size = btrfs_item_size_nr(leaf, slot);
33685f39d397SChris Mason 	item = btrfs_item_nr(leaf, slot);
33695f39d397SChris Mason 	btrfs_set_item_size(leaf, item, old_size + data_size);
33705f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
33716567e837SChris Mason 
33725f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
33735f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
33746567e837SChris Mason 		BUG();
33755f39d397SChris Mason 	}
33766567e837SChris Mason }
33776567e837SChris Mason 
337874123bd7SChris Mason /*
3379d352ac68SChris Mason  * Given a key and some data, insert items into the tree.
338074123bd7SChris Mason  * This does all the path init required, making room in the tree if needed.
3381f3465ca4SJosef Bacik  * Returns the number of keys that were inserted.
3382f3465ca4SJosef Bacik  */
3383f3465ca4SJosef Bacik int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3384f3465ca4SJosef Bacik 			    struct btrfs_root *root,
3385f3465ca4SJosef Bacik 			    struct btrfs_path *path,
3386f3465ca4SJosef Bacik 			    struct btrfs_key *cpu_key, u32 *data_size,
3387f3465ca4SJosef Bacik 			    int nr)
3388f3465ca4SJosef Bacik {
3389f3465ca4SJosef Bacik 	struct extent_buffer *leaf;
3390f3465ca4SJosef Bacik 	struct btrfs_item *item;
3391f3465ca4SJosef Bacik 	int ret = 0;
3392f3465ca4SJosef Bacik 	int slot;
3393f3465ca4SJosef Bacik 	int i;
3394f3465ca4SJosef Bacik 	u32 nritems;
3395f3465ca4SJosef Bacik 	u32 total_data = 0;
3396f3465ca4SJosef Bacik 	u32 total_size = 0;
3397f3465ca4SJosef Bacik 	unsigned int data_end;
3398f3465ca4SJosef Bacik 	struct btrfs_disk_key disk_key;
3399f3465ca4SJosef Bacik 	struct btrfs_key found_key;
3400f3465ca4SJosef Bacik 
340187b29b20SYan Zheng 	for (i = 0; i < nr; i++) {
340287b29b20SYan Zheng 		if (total_size + data_size[i] + sizeof(struct btrfs_item) >
340387b29b20SYan Zheng 		    BTRFS_LEAF_DATA_SIZE(root)) {
340487b29b20SYan Zheng 			break;
340587b29b20SYan Zheng 			nr = i;
340687b29b20SYan Zheng 		}
3407f3465ca4SJosef Bacik 		total_data += data_size[i];
340887b29b20SYan Zheng 		total_size += data_size[i] + sizeof(struct btrfs_item);
340987b29b20SYan Zheng 	}
341087b29b20SYan Zheng 	BUG_ON(nr == 0);
3411f3465ca4SJosef Bacik 
3412f3465ca4SJosef Bacik 	ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3413f3465ca4SJosef Bacik 	if (ret == 0)
3414f3465ca4SJosef Bacik 		return -EEXIST;
3415f3465ca4SJosef Bacik 	if (ret < 0)
3416f3465ca4SJosef Bacik 		goto out;
3417f3465ca4SJosef Bacik 
3418f3465ca4SJosef Bacik 	leaf = path->nodes[0];
3419f3465ca4SJosef Bacik 
3420f3465ca4SJosef Bacik 	nritems = btrfs_header_nritems(leaf);
3421f3465ca4SJosef Bacik 	data_end = leaf_data_end(root, leaf);
3422f3465ca4SJosef Bacik 
3423f3465ca4SJosef Bacik 	if (btrfs_leaf_free_space(root, leaf) < total_size) {
3424f3465ca4SJosef Bacik 		for (i = nr; i >= 0; i--) {
3425f3465ca4SJosef Bacik 			total_data -= data_size[i];
3426f3465ca4SJosef Bacik 			total_size -= data_size[i] + sizeof(struct btrfs_item);
3427f3465ca4SJosef Bacik 			if (total_size < btrfs_leaf_free_space(root, leaf))
3428f3465ca4SJosef Bacik 				break;
3429f3465ca4SJosef Bacik 		}
3430f3465ca4SJosef Bacik 		nr = i;
3431f3465ca4SJosef Bacik 	}
3432f3465ca4SJosef Bacik 
3433f3465ca4SJosef Bacik 	slot = path->slots[0];
3434f3465ca4SJosef Bacik 	BUG_ON(slot < 0);
3435f3465ca4SJosef Bacik 
3436f3465ca4SJosef Bacik 	if (slot != nritems) {
3437f3465ca4SJosef Bacik 		unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3438f3465ca4SJosef Bacik 
3439f3465ca4SJosef Bacik 		item = btrfs_item_nr(leaf, slot);
3440f3465ca4SJosef Bacik 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
3441f3465ca4SJosef Bacik 
3442f3465ca4SJosef Bacik 		/* figure out how many keys we can insert in here */
3443f3465ca4SJosef Bacik 		total_data = data_size[0];
3444f3465ca4SJosef Bacik 		for (i = 1; i < nr; i++) {
34455d4f98a2SYan Zheng 			if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3446f3465ca4SJosef Bacik 				break;
3447f3465ca4SJosef Bacik 			total_data += data_size[i];
3448f3465ca4SJosef Bacik 		}
3449f3465ca4SJosef Bacik 		nr = i;
3450f3465ca4SJosef Bacik 
3451f3465ca4SJosef Bacik 		if (old_data < data_end) {
3452f3465ca4SJosef Bacik 			btrfs_print_leaf(root, leaf);
3453d397712bSChris Mason 			printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3454f3465ca4SJosef Bacik 			       slot, old_data, data_end);
3455f3465ca4SJosef Bacik 			BUG_ON(1);
3456f3465ca4SJosef Bacik 		}
3457f3465ca4SJosef Bacik 		/*
3458f3465ca4SJosef Bacik 		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3459f3465ca4SJosef Bacik 		 */
3460f3465ca4SJosef Bacik 		/* first correct the data pointers */
3461f3465ca4SJosef Bacik 		for (i = slot; i < nritems; i++) {
3462f3465ca4SJosef Bacik 			u32 ioff;
3463f3465ca4SJosef Bacik 
3464f3465ca4SJosef Bacik 			item = btrfs_item_nr(leaf, i);
3465f3465ca4SJosef Bacik 			ioff = btrfs_item_offset(leaf, item);
3466f3465ca4SJosef Bacik 			btrfs_set_item_offset(leaf, item, ioff - total_data);
3467f3465ca4SJosef Bacik 		}
3468f3465ca4SJosef Bacik 		/* shift the items */
3469f3465ca4SJosef Bacik 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3470f3465ca4SJosef Bacik 			      btrfs_item_nr_offset(slot),
3471f3465ca4SJosef Bacik 			      (nritems - slot) * sizeof(struct btrfs_item));
3472f3465ca4SJosef Bacik 
3473f3465ca4SJosef Bacik 		/* shift the data */
3474f3465ca4SJosef Bacik 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3475f3465ca4SJosef Bacik 			      data_end - total_data, btrfs_leaf_data(leaf) +
3476f3465ca4SJosef Bacik 			      data_end, old_data - data_end);
3477f3465ca4SJosef Bacik 		data_end = old_data;
3478f3465ca4SJosef Bacik 	} else {
3479f3465ca4SJosef Bacik 		/*
3480f3465ca4SJosef Bacik 		 * this sucks but it has to be done, if we are inserting at
3481f3465ca4SJosef Bacik 		 * the end of the leaf only insert 1 of the items, since we
3482f3465ca4SJosef Bacik 		 * have no way of knowing whats on the next leaf and we'd have
3483f3465ca4SJosef Bacik 		 * to drop our current locks to figure it out
3484f3465ca4SJosef Bacik 		 */
3485f3465ca4SJosef Bacik 		nr = 1;
3486f3465ca4SJosef Bacik 	}
3487f3465ca4SJosef Bacik 
3488f3465ca4SJosef Bacik 	/* setup the item for the new data */
3489f3465ca4SJosef Bacik 	for (i = 0; i < nr; i++) {
3490f3465ca4SJosef Bacik 		btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3491f3465ca4SJosef Bacik 		btrfs_set_item_key(leaf, &disk_key, slot + i);
3492f3465ca4SJosef Bacik 		item = btrfs_item_nr(leaf, slot + i);
3493f3465ca4SJosef Bacik 		btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3494f3465ca4SJosef Bacik 		data_end -= data_size[i];
3495f3465ca4SJosef Bacik 		btrfs_set_item_size(leaf, item, data_size[i]);
3496f3465ca4SJosef Bacik 	}
3497f3465ca4SJosef Bacik 	btrfs_set_header_nritems(leaf, nritems + nr);
3498f3465ca4SJosef Bacik 	btrfs_mark_buffer_dirty(leaf);
3499f3465ca4SJosef Bacik 
3500f3465ca4SJosef Bacik 	ret = 0;
3501f3465ca4SJosef Bacik 	if (slot == 0) {
3502f3465ca4SJosef Bacik 		btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3503143bede5SJeff Mahoney 		fixup_low_keys(trans, root, path, &disk_key, 1);
3504f3465ca4SJosef Bacik 	}
3505f3465ca4SJosef Bacik 
3506f3465ca4SJosef Bacik 	if (btrfs_leaf_free_space(root, leaf) < 0) {
3507f3465ca4SJosef Bacik 		btrfs_print_leaf(root, leaf);
3508f3465ca4SJosef Bacik 		BUG();
3509f3465ca4SJosef Bacik 	}
3510f3465ca4SJosef Bacik out:
3511f3465ca4SJosef Bacik 	if (!ret)
3512f3465ca4SJosef Bacik 		ret = nr;
3513f3465ca4SJosef Bacik 	return ret;
3514f3465ca4SJosef Bacik }
3515f3465ca4SJosef Bacik 
3516f3465ca4SJosef Bacik /*
351744871b1bSChris Mason  * this is a helper for btrfs_insert_empty_items, the main goal here is
351844871b1bSChris Mason  * to save stack depth by doing the bulk of the work in a function
351944871b1bSChris Mason  * that doesn't call btrfs_search_slot
352074123bd7SChris Mason  */
3521143bede5SJeff Mahoney void setup_items_for_insert(struct btrfs_trans_handle *trans,
352244871b1bSChris Mason 			    struct btrfs_root *root, struct btrfs_path *path,
35239c58309dSChris Mason 			    struct btrfs_key *cpu_key, u32 *data_size,
352444871b1bSChris Mason 			    u32 total_data, u32 total_size, int nr)
3525be0e5c09SChris Mason {
35265f39d397SChris Mason 	struct btrfs_item *item;
35279c58309dSChris Mason 	int i;
35287518a238SChris Mason 	u32 nritems;
3529be0e5c09SChris Mason 	unsigned int data_end;
3530e2fa7227SChris Mason 	struct btrfs_disk_key disk_key;
353144871b1bSChris Mason 	struct extent_buffer *leaf;
353244871b1bSChris Mason 	int slot;
3533e2fa7227SChris Mason 
35345f39d397SChris Mason 	leaf = path->nodes[0];
353544871b1bSChris Mason 	slot = path->slots[0];
353674123bd7SChris Mason 
35375f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3538123abc88SChris Mason 	data_end = leaf_data_end(root, leaf);
3539eb60ceacSChris Mason 
3540f25956ccSChris Mason 	if (btrfs_leaf_free_space(root, leaf) < total_size) {
35413326d1b0SChris Mason 		btrfs_print_leaf(root, leaf);
3542d397712bSChris Mason 		printk(KERN_CRIT "not enough freespace need %u have %d\n",
35439c58309dSChris Mason 		       total_size, btrfs_leaf_free_space(root, leaf));
3544be0e5c09SChris Mason 		BUG();
3545d4dbff95SChris Mason 	}
35465f39d397SChris Mason 
3547be0e5c09SChris Mason 	if (slot != nritems) {
35485f39d397SChris Mason 		unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3549be0e5c09SChris Mason 
35505f39d397SChris Mason 		if (old_data < data_end) {
35515f39d397SChris Mason 			btrfs_print_leaf(root, leaf);
3552d397712bSChris Mason 			printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
35535f39d397SChris Mason 			       slot, old_data, data_end);
35545f39d397SChris Mason 			BUG_ON(1);
35555f39d397SChris Mason 		}
3556be0e5c09SChris Mason 		/*
3557be0e5c09SChris Mason 		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3558be0e5c09SChris Mason 		 */
3559be0e5c09SChris Mason 		/* first correct the data pointers */
35600783fcfcSChris Mason 		for (i = slot; i < nritems; i++) {
35615f39d397SChris Mason 			u32 ioff;
3562db94535dSChris Mason 
35635f39d397SChris Mason 			item = btrfs_item_nr(leaf, i);
35645f39d397SChris Mason 			ioff = btrfs_item_offset(leaf, item);
35659c58309dSChris Mason 			btrfs_set_item_offset(leaf, item, ioff - total_data);
35660783fcfcSChris Mason 		}
3567be0e5c09SChris Mason 		/* shift the items */
35689c58309dSChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
35695f39d397SChris Mason 			      btrfs_item_nr_offset(slot),
35700783fcfcSChris Mason 			      (nritems - slot) * sizeof(struct btrfs_item));
3571be0e5c09SChris Mason 
3572be0e5c09SChris Mason 		/* shift the data */
35735f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
35749c58309dSChris Mason 			      data_end - total_data, btrfs_leaf_data(leaf) +
3575be0e5c09SChris Mason 			      data_end, old_data - data_end);
3576be0e5c09SChris Mason 		data_end = old_data;
3577be0e5c09SChris Mason 	}
35785f39d397SChris Mason 
357962e2749eSChris Mason 	/* setup the item for the new data */
35809c58309dSChris Mason 	for (i = 0; i < nr; i++) {
35819c58309dSChris Mason 		btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
35829c58309dSChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot + i);
35839c58309dSChris Mason 		item = btrfs_item_nr(leaf, slot + i);
35849c58309dSChris Mason 		btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
35859c58309dSChris Mason 		data_end -= data_size[i];
35869c58309dSChris Mason 		btrfs_set_item_size(leaf, item, data_size[i]);
35879c58309dSChris Mason 	}
358844871b1bSChris Mason 
35899c58309dSChris Mason 	btrfs_set_header_nritems(leaf, nritems + nr);
3590aa5d6bedSChris Mason 
35915a01a2e3SChris Mason 	if (slot == 0) {
35925a01a2e3SChris Mason 		btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3593143bede5SJeff Mahoney 		fixup_low_keys(trans, root, path, &disk_key, 1);
35945a01a2e3SChris Mason 	}
3595b9473439SChris Mason 	btrfs_unlock_up_safe(path, 1);
3596b9473439SChris Mason 	btrfs_mark_buffer_dirty(leaf);
3597aa5d6bedSChris Mason 
35985f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
35995f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
3600be0e5c09SChris Mason 		BUG();
36015f39d397SChris Mason 	}
360244871b1bSChris Mason }
360344871b1bSChris Mason 
360444871b1bSChris Mason /*
360544871b1bSChris Mason  * Given a key and some data, insert items into the tree.
360644871b1bSChris Mason  * This does all the path init required, making room in the tree if needed.
360744871b1bSChris Mason  */
360844871b1bSChris Mason int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
360944871b1bSChris Mason 			    struct btrfs_root *root,
361044871b1bSChris Mason 			    struct btrfs_path *path,
361144871b1bSChris Mason 			    struct btrfs_key *cpu_key, u32 *data_size,
361244871b1bSChris Mason 			    int nr)
361344871b1bSChris Mason {
361444871b1bSChris Mason 	int ret = 0;
361544871b1bSChris Mason 	int slot;
361644871b1bSChris Mason 	int i;
361744871b1bSChris Mason 	u32 total_size = 0;
361844871b1bSChris Mason 	u32 total_data = 0;
361944871b1bSChris Mason 
362044871b1bSChris Mason 	for (i = 0; i < nr; i++)
362144871b1bSChris Mason 		total_data += data_size[i];
362244871b1bSChris Mason 
362344871b1bSChris Mason 	total_size = total_data + (nr * sizeof(struct btrfs_item));
362444871b1bSChris Mason 	ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
362544871b1bSChris Mason 	if (ret == 0)
362644871b1bSChris Mason 		return -EEXIST;
362744871b1bSChris Mason 	if (ret < 0)
3628143bede5SJeff Mahoney 		return ret;
362944871b1bSChris Mason 
363044871b1bSChris Mason 	slot = path->slots[0];
363144871b1bSChris Mason 	BUG_ON(slot < 0);
363244871b1bSChris Mason 
3633143bede5SJeff Mahoney 	setup_items_for_insert(trans, root, path, cpu_key, data_size,
363444871b1bSChris Mason 			       total_data, total_size, nr);
3635143bede5SJeff Mahoney 	return 0;
363662e2749eSChris Mason }
363762e2749eSChris Mason 
363862e2749eSChris Mason /*
363962e2749eSChris Mason  * Given a key and some data, insert an item into the tree.
364062e2749eSChris Mason  * This does all the path init required, making room in the tree if needed.
364162e2749eSChris Mason  */
3642e089f05cSChris Mason int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3643e089f05cSChris Mason 		      *root, struct btrfs_key *cpu_key, void *data, u32
3644e089f05cSChris Mason 		      data_size)
364562e2749eSChris Mason {
364662e2749eSChris Mason 	int ret = 0;
36472c90e5d6SChris Mason 	struct btrfs_path *path;
36485f39d397SChris Mason 	struct extent_buffer *leaf;
36495f39d397SChris Mason 	unsigned long ptr;
365062e2749eSChris Mason 
36512c90e5d6SChris Mason 	path = btrfs_alloc_path();
3652db5b493aSTsutomu Itoh 	if (!path)
3653db5b493aSTsutomu Itoh 		return -ENOMEM;
36542c90e5d6SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
365562e2749eSChris Mason 	if (!ret) {
36565f39d397SChris Mason 		leaf = path->nodes[0];
36575f39d397SChris Mason 		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
36585f39d397SChris Mason 		write_extent_buffer(leaf, data, ptr, data_size);
36595f39d397SChris Mason 		btrfs_mark_buffer_dirty(leaf);
366062e2749eSChris Mason 	}
36612c90e5d6SChris Mason 	btrfs_free_path(path);
3662aa5d6bedSChris Mason 	return ret;
3663be0e5c09SChris Mason }
3664be0e5c09SChris Mason 
366574123bd7SChris Mason /*
36665de08d7dSChris Mason  * delete the pointer from a given node.
366774123bd7SChris Mason  *
3668d352ac68SChris Mason  * the tree should have been previously balanced so the deletion does not
3669d352ac68SChris Mason  * empty a node.
367074123bd7SChris Mason  */
3671143bede5SJeff Mahoney static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3672e089f05cSChris Mason 		    struct btrfs_path *path, int level, int slot)
3673be0e5c09SChris Mason {
36745f39d397SChris Mason 	struct extent_buffer *parent = path->nodes[level];
36757518a238SChris Mason 	u32 nritems;
3676be0e5c09SChris Mason 
36775f39d397SChris Mason 	nritems = btrfs_header_nritems(parent);
3678be0e5c09SChris Mason 	if (slot != nritems - 1) {
36795f39d397SChris Mason 		memmove_extent_buffer(parent,
36805f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot),
36815f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot + 1),
3682d6025579SChris Mason 			      sizeof(struct btrfs_key_ptr) *
3683d6025579SChris Mason 			      (nritems - slot - 1));
3684be0e5c09SChris Mason 	}
36857518a238SChris Mason 	nritems--;
36865f39d397SChris Mason 	btrfs_set_header_nritems(parent, nritems);
36877518a238SChris Mason 	if (nritems == 0 && parent == root->node) {
36885f39d397SChris Mason 		BUG_ON(btrfs_header_level(root->node) != 1);
3689eb60ceacSChris Mason 		/* just turn the root into a leaf and break */
36905f39d397SChris Mason 		btrfs_set_header_level(root->node, 0);
3691bb803951SChris Mason 	} else if (slot == 0) {
36925f39d397SChris Mason 		struct btrfs_disk_key disk_key;
36935f39d397SChris Mason 
36945f39d397SChris Mason 		btrfs_node_key(parent, &disk_key, 0);
3695143bede5SJeff Mahoney 		fixup_low_keys(trans, root, path, &disk_key, level + 1);
3696be0e5c09SChris Mason 	}
3697d6025579SChris Mason 	btrfs_mark_buffer_dirty(parent);
3698be0e5c09SChris Mason }
3699be0e5c09SChris Mason 
370074123bd7SChris Mason /*
3701323ac95bSChris Mason  * a helper function to delete the leaf pointed to by path->slots[1] and
37025d4f98a2SYan Zheng  * path->nodes[1].
3703323ac95bSChris Mason  *
3704323ac95bSChris Mason  * This deletes the pointer in path->nodes[1] and frees the leaf
3705323ac95bSChris Mason  * block extent.  zero is returned if it all worked out, < 0 otherwise.
3706323ac95bSChris Mason  *
3707323ac95bSChris Mason  * The path must have already been setup for deleting the leaf, including
3708323ac95bSChris Mason  * all the proper balancing.  path->nodes[1] must be locked.
3709323ac95bSChris Mason  */
3710143bede5SJeff Mahoney static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
3711323ac95bSChris Mason 				    struct btrfs_root *root,
37125d4f98a2SYan Zheng 				    struct btrfs_path *path,
37135d4f98a2SYan Zheng 				    struct extent_buffer *leaf)
3714323ac95bSChris Mason {
37155d4f98a2SYan Zheng 	WARN_ON(btrfs_header_generation(leaf) != trans->transid);
3716143bede5SJeff Mahoney 	del_ptr(trans, root, path, 1, path->slots[1]);
3717323ac95bSChris Mason 
37184d081c41SChris Mason 	/*
37194d081c41SChris Mason 	 * btrfs_free_extent is expensive, we want to make sure we
37204d081c41SChris Mason 	 * aren't holding any locks when we call it
37214d081c41SChris Mason 	 */
37224d081c41SChris Mason 	btrfs_unlock_up_safe(path, 0);
37234d081c41SChris Mason 
3724f0486c68SYan, Zheng 	root_sub_used(root, leaf->len);
3725f0486c68SYan, Zheng 
372666d7e7f0SArne Jansen 	btrfs_free_tree_block(trans, root, leaf, 0, 1, 0);
3727323ac95bSChris Mason }
3728323ac95bSChris Mason /*
372974123bd7SChris Mason  * delete the item at the leaf level in path.  If that empties
373074123bd7SChris Mason  * the leaf, remove it from the tree
373174123bd7SChris Mason  */
373285e21bacSChris Mason int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
373385e21bacSChris Mason 		    struct btrfs_path *path, int slot, int nr)
3734be0e5c09SChris Mason {
37355f39d397SChris Mason 	struct extent_buffer *leaf;
37365f39d397SChris Mason 	struct btrfs_item *item;
373785e21bacSChris Mason 	int last_off;
373885e21bacSChris Mason 	int dsize = 0;
3739aa5d6bedSChris Mason 	int ret = 0;
3740aa5d6bedSChris Mason 	int wret;
374185e21bacSChris Mason 	int i;
37427518a238SChris Mason 	u32 nritems;
3743be0e5c09SChris Mason 
37445f39d397SChris Mason 	leaf = path->nodes[0];
374585e21bacSChris Mason 	last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
374685e21bacSChris Mason 
374785e21bacSChris Mason 	for (i = 0; i < nr; i++)
374885e21bacSChris Mason 		dsize += btrfs_item_size_nr(leaf, slot + i);
374985e21bacSChris Mason 
37505f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3751be0e5c09SChris Mason 
375285e21bacSChris Mason 	if (slot + nr != nritems) {
3753123abc88SChris Mason 		int data_end = leaf_data_end(root, leaf);
37545f39d397SChris Mason 
37555f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3756d6025579SChris Mason 			      data_end + dsize,
3757123abc88SChris Mason 			      btrfs_leaf_data(leaf) + data_end,
375885e21bacSChris Mason 			      last_off - data_end);
37595f39d397SChris Mason 
376085e21bacSChris Mason 		for (i = slot + nr; i < nritems; i++) {
37615f39d397SChris Mason 			u32 ioff;
3762db94535dSChris Mason 
37635f39d397SChris Mason 			item = btrfs_item_nr(leaf, i);
37645f39d397SChris Mason 			ioff = btrfs_item_offset(leaf, item);
37655f39d397SChris Mason 			btrfs_set_item_offset(leaf, item, ioff + dsize);
37660783fcfcSChris Mason 		}
3767db94535dSChris Mason 
37685f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
376985e21bacSChris Mason 			      btrfs_item_nr_offset(slot + nr),
37700783fcfcSChris Mason 			      sizeof(struct btrfs_item) *
377185e21bacSChris Mason 			      (nritems - slot - nr));
3772be0e5c09SChris Mason 	}
377385e21bacSChris Mason 	btrfs_set_header_nritems(leaf, nritems - nr);
377485e21bacSChris Mason 	nritems -= nr;
37755f39d397SChris Mason 
377674123bd7SChris Mason 	/* delete the leaf if we've emptied it */
37777518a238SChris Mason 	if (nritems == 0) {
37785f39d397SChris Mason 		if (leaf == root->node) {
37795f39d397SChris Mason 			btrfs_set_header_level(leaf, 0);
37809a8dd150SChris Mason 		} else {
3781f0486c68SYan, Zheng 			btrfs_set_path_blocking(path);
3782f0486c68SYan, Zheng 			clean_tree_block(trans, root, leaf);
3783143bede5SJeff Mahoney 			btrfs_del_leaf(trans, root, path, leaf);
37849a8dd150SChris Mason 		}
3785be0e5c09SChris Mason 	} else {
37867518a238SChris Mason 		int used = leaf_space_used(leaf, 0, nritems);
3787aa5d6bedSChris Mason 		if (slot == 0) {
37885f39d397SChris Mason 			struct btrfs_disk_key disk_key;
37895f39d397SChris Mason 
37905f39d397SChris Mason 			btrfs_item_key(leaf, &disk_key, 0);
3791143bede5SJeff Mahoney 			fixup_low_keys(trans, root, path, &disk_key, 1);
3792aa5d6bedSChris Mason 		}
3793aa5d6bedSChris Mason 
379474123bd7SChris Mason 		/* delete the leaf if it is mostly empty */
3795d717aa1dSYan Zheng 		if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
3796be0e5c09SChris Mason 			/* push_leaf_left fixes the path.
3797be0e5c09SChris Mason 			 * make sure the path still points to our leaf
3798be0e5c09SChris Mason 			 * for possible call to del_ptr below
3799be0e5c09SChris Mason 			 */
38004920c9acSChris Mason 			slot = path->slots[1];
38015f39d397SChris Mason 			extent_buffer_get(leaf);
38025f39d397SChris Mason 
3803b9473439SChris Mason 			btrfs_set_path_blocking(path);
380499d8f83cSChris Mason 			wret = push_leaf_left(trans, root, path, 1, 1,
380599d8f83cSChris Mason 					      1, (u32)-1);
380654aa1f4dSChris Mason 			if (wret < 0 && wret != -ENOSPC)
3807aa5d6bedSChris Mason 				ret = wret;
38085f39d397SChris Mason 
38095f39d397SChris Mason 			if (path->nodes[0] == leaf &&
38105f39d397SChris Mason 			    btrfs_header_nritems(leaf)) {
381199d8f83cSChris Mason 				wret = push_leaf_right(trans, root, path, 1,
381299d8f83cSChris Mason 						       1, 1, 0);
381354aa1f4dSChris Mason 				if (wret < 0 && wret != -ENOSPC)
3814aa5d6bedSChris Mason 					ret = wret;
3815aa5d6bedSChris Mason 			}
38165f39d397SChris Mason 
38175f39d397SChris Mason 			if (btrfs_header_nritems(leaf) == 0) {
3818323ac95bSChris Mason 				path->slots[1] = slot;
3819143bede5SJeff Mahoney 				btrfs_del_leaf(trans, root, path, leaf);
38205f39d397SChris Mason 				free_extent_buffer(leaf);
3821143bede5SJeff Mahoney 				ret = 0;
38225de08d7dSChris Mason 			} else {
3823925baeddSChris Mason 				/* if we're still in the path, make sure
3824925baeddSChris Mason 				 * we're dirty.  Otherwise, one of the
3825925baeddSChris Mason 				 * push_leaf functions must have already
3826925baeddSChris Mason 				 * dirtied this buffer
3827925baeddSChris Mason 				 */
3828925baeddSChris Mason 				if (path->nodes[0] == leaf)
38295f39d397SChris Mason 					btrfs_mark_buffer_dirty(leaf);
38305f39d397SChris Mason 				free_extent_buffer(leaf);
3831be0e5c09SChris Mason 			}
3832d5719762SChris Mason 		} else {
38335f39d397SChris Mason 			btrfs_mark_buffer_dirty(leaf);
3834be0e5c09SChris Mason 		}
38359a8dd150SChris Mason 	}
3836aa5d6bedSChris Mason 	return ret;
38379a8dd150SChris Mason }
38389a8dd150SChris Mason 
383997571fd0SChris Mason /*
3840925baeddSChris Mason  * search the tree again to find a leaf with lesser keys
38417bb86316SChris Mason  * returns 0 if it found something or 1 if there are no lesser leaves.
38427bb86316SChris Mason  * returns < 0 on io errors.
3843d352ac68SChris Mason  *
3844d352ac68SChris Mason  * This may release the path, and so you may lose any locks held at the
3845d352ac68SChris Mason  * time you call it.
38467bb86316SChris Mason  */
38477bb86316SChris Mason int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
38487bb86316SChris Mason {
3849925baeddSChris Mason 	struct btrfs_key key;
3850925baeddSChris Mason 	struct btrfs_disk_key found_key;
3851925baeddSChris Mason 	int ret;
38527bb86316SChris Mason 
3853925baeddSChris Mason 	btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
3854925baeddSChris Mason 
3855925baeddSChris Mason 	if (key.offset > 0)
3856925baeddSChris Mason 		key.offset--;
3857925baeddSChris Mason 	else if (key.type > 0)
3858925baeddSChris Mason 		key.type--;
3859925baeddSChris Mason 	else if (key.objectid > 0)
3860925baeddSChris Mason 		key.objectid--;
3861925baeddSChris Mason 	else
38627bb86316SChris Mason 		return 1;
38637bb86316SChris Mason 
3864b3b4aa74SDavid Sterba 	btrfs_release_path(path);
3865925baeddSChris Mason 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3866925baeddSChris Mason 	if (ret < 0)
3867925baeddSChris Mason 		return ret;
3868925baeddSChris Mason 	btrfs_item_key(path->nodes[0], &found_key, 0);
3869925baeddSChris Mason 	ret = comp_keys(&found_key, &key);
3870925baeddSChris Mason 	if (ret < 0)
38717bb86316SChris Mason 		return 0;
3872925baeddSChris Mason 	return 1;
38737bb86316SChris Mason }
38747bb86316SChris Mason 
38753f157a2fSChris Mason /*
38763f157a2fSChris Mason  * A helper function to walk down the tree starting at min_key, and looking
38773f157a2fSChris Mason  * for nodes or leaves that are either in cache or have a minimum
3878d352ac68SChris Mason  * transaction id.  This is used by the btree defrag code, and tree logging
38793f157a2fSChris Mason  *
38803f157a2fSChris Mason  * This does not cow, but it does stuff the starting key it finds back
38813f157a2fSChris Mason  * into min_key, so you can call btrfs_search_slot with cow=1 on the
38823f157a2fSChris Mason  * key and get a writable path.
38833f157a2fSChris Mason  *
38843f157a2fSChris Mason  * This does lock as it descends, and path->keep_locks should be set
38853f157a2fSChris Mason  * to 1 by the caller.
38863f157a2fSChris Mason  *
38873f157a2fSChris Mason  * This honors path->lowest_level to prevent descent past a given level
38883f157a2fSChris Mason  * of the tree.
38893f157a2fSChris Mason  *
3890d352ac68SChris Mason  * min_trans indicates the oldest transaction that you are interested
3891d352ac68SChris Mason  * in walking through.  Any nodes or leaves older than min_trans are
3892d352ac68SChris Mason  * skipped over (without reading them).
3893d352ac68SChris Mason  *
38943f157a2fSChris Mason  * returns zero if something useful was found, < 0 on error and 1 if there
38953f157a2fSChris Mason  * was nothing in the tree that matched the search criteria.
38963f157a2fSChris Mason  */
38973f157a2fSChris Mason int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
3898e02119d5SChris Mason 			 struct btrfs_key *max_key,
38993f157a2fSChris Mason 			 struct btrfs_path *path, int cache_only,
39003f157a2fSChris Mason 			 u64 min_trans)
39013f157a2fSChris Mason {
39023f157a2fSChris Mason 	struct extent_buffer *cur;
39033f157a2fSChris Mason 	struct btrfs_key found_key;
39043f157a2fSChris Mason 	int slot;
39059652480bSYan 	int sret;
39063f157a2fSChris Mason 	u32 nritems;
39073f157a2fSChris Mason 	int level;
39083f157a2fSChris Mason 	int ret = 1;
39093f157a2fSChris Mason 
3910934d375bSChris Mason 	WARN_ON(!path->keep_locks);
39113f157a2fSChris Mason again:
3912bd681513SChris Mason 	cur = btrfs_read_lock_root_node(root);
39133f157a2fSChris Mason 	level = btrfs_header_level(cur);
3914e02119d5SChris Mason 	WARN_ON(path->nodes[level]);
39153f157a2fSChris Mason 	path->nodes[level] = cur;
3916bd681513SChris Mason 	path->locks[level] = BTRFS_READ_LOCK;
39173f157a2fSChris Mason 
39183f157a2fSChris Mason 	if (btrfs_header_generation(cur) < min_trans) {
39193f157a2fSChris Mason 		ret = 1;
39203f157a2fSChris Mason 		goto out;
39213f157a2fSChris Mason 	}
39223f157a2fSChris Mason 	while (1) {
39233f157a2fSChris Mason 		nritems = btrfs_header_nritems(cur);
39243f157a2fSChris Mason 		level = btrfs_header_level(cur);
39259652480bSYan 		sret = bin_search(cur, min_key, level, &slot);
39263f157a2fSChris Mason 
3927323ac95bSChris Mason 		/* at the lowest level, we're done, setup the path and exit */
3928323ac95bSChris Mason 		if (level == path->lowest_level) {
3929e02119d5SChris Mason 			if (slot >= nritems)
3930e02119d5SChris Mason 				goto find_next_key;
39313f157a2fSChris Mason 			ret = 0;
39323f157a2fSChris Mason 			path->slots[level] = slot;
39333f157a2fSChris Mason 			btrfs_item_key_to_cpu(cur, &found_key, slot);
39343f157a2fSChris Mason 			goto out;
39353f157a2fSChris Mason 		}
39369652480bSYan 		if (sret && slot > 0)
39379652480bSYan 			slot--;
39383f157a2fSChris Mason 		/*
39393f157a2fSChris Mason 		 * check this node pointer against the cache_only and
39403f157a2fSChris Mason 		 * min_trans parameters.  If it isn't in cache or is too
39413f157a2fSChris Mason 		 * old, skip to the next one.
39423f157a2fSChris Mason 		 */
39433f157a2fSChris Mason 		while (slot < nritems) {
39443f157a2fSChris Mason 			u64 blockptr;
39453f157a2fSChris Mason 			u64 gen;
39463f157a2fSChris Mason 			struct extent_buffer *tmp;
3947e02119d5SChris Mason 			struct btrfs_disk_key disk_key;
3948e02119d5SChris Mason 
39493f157a2fSChris Mason 			blockptr = btrfs_node_blockptr(cur, slot);
39503f157a2fSChris Mason 			gen = btrfs_node_ptr_generation(cur, slot);
39513f157a2fSChris Mason 			if (gen < min_trans) {
39523f157a2fSChris Mason 				slot++;
39533f157a2fSChris Mason 				continue;
39543f157a2fSChris Mason 			}
39553f157a2fSChris Mason 			if (!cache_only)
39563f157a2fSChris Mason 				break;
39573f157a2fSChris Mason 
3958e02119d5SChris Mason 			if (max_key) {
3959e02119d5SChris Mason 				btrfs_node_key(cur, &disk_key, slot);
3960e02119d5SChris Mason 				if (comp_keys(&disk_key, max_key) >= 0) {
3961e02119d5SChris Mason 					ret = 1;
3962e02119d5SChris Mason 					goto out;
3963e02119d5SChris Mason 				}
3964e02119d5SChris Mason 			}
3965e02119d5SChris Mason 
39663f157a2fSChris Mason 			tmp = btrfs_find_tree_block(root, blockptr,
39673f157a2fSChris Mason 					    btrfs_level_size(root, level - 1));
39683f157a2fSChris Mason 
39693f157a2fSChris Mason 			if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
39703f157a2fSChris Mason 				free_extent_buffer(tmp);
39713f157a2fSChris Mason 				break;
39723f157a2fSChris Mason 			}
39733f157a2fSChris Mason 			if (tmp)
39743f157a2fSChris Mason 				free_extent_buffer(tmp);
39753f157a2fSChris Mason 			slot++;
39763f157a2fSChris Mason 		}
3977e02119d5SChris Mason find_next_key:
39783f157a2fSChris Mason 		/*
39793f157a2fSChris Mason 		 * we didn't find a candidate key in this node, walk forward
39803f157a2fSChris Mason 		 * and find another one
39813f157a2fSChris Mason 		 */
39823f157a2fSChris Mason 		if (slot >= nritems) {
3983e02119d5SChris Mason 			path->slots[level] = slot;
3984b4ce94deSChris Mason 			btrfs_set_path_blocking(path);
3985e02119d5SChris Mason 			sret = btrfs_find_next_key(root, path, min_key, level,
39863f157a2fSChris Mason 						  cache_only, min_trans);
3987e02119d5SChris Mason 			if (sret == 0) {
3988b3b4aa74SDavid Sterba 				btrfs_release_path(path);
39893f157a2fSChris Mason 				goto again;
39903f157a2fSChris Mason 			} else {
39913f157a2fSChris Mason 				goto out;
39923f157a2fSChris Mason 			}
39933f157a2fSChris Mason 		}
39943f157a2fSChris Mason 		/* save our key for returning back */
39953f157a2fSChris Mason 		btrfs_node_key_to_cpu(cur, &found_key, slot);
39963f157a2fSChris Mason 		path->slots[level] = slot;
39973f157a2fSChris Mason 		if (level == path->lowest_level) {
39983f157a2fSChris Mason 			ret = 0;
39993f157a2fSChris Mason 			unlock_up(path, level, 1);
40003f157a2fSChris Mason 			goto out;
40013f157a2fSChris Mason 		}
4002b4ce94deSChris Mason 		btrfs_set_path_blocking(path);
40033f157a2fSChris Mason 		cur = read_node_slot(root, cur, slot);
400497d9a8a4STsutomu Itoh 		BUG_ON(!cur);
40053f157a2fSChris Mason 
4006bd681513SChris Mason 		btrfs_tree_read_lock(cur);
4007b4ce94deSChris Mason 
4008bd681513SChris Mason 		path->locks[level - 1] = BTRFS_READ_LOCK;
40093f157a2fSChris Mason 		path->nodes[level - 1] = cur;
40103f157a2fSChris Mason 		unlock_up(path, level, 1);
4011bd681513SChris Mason 		btrfs_clear_path_blocking(path, NULL, 0);
40123f157a2fSChris Mason 	}
40133f157a2fSChris Mason out:
40143f157a2fSChris Mason 	if (ret == 0)
40153f157a2fSChris Mason 		memcpy(min_key, &found_key, sizeof(found_key));
4016b4ce94deSChris Mason 	btrfs_set_path_blocking(path);
40173f157a2fSChris Mason 	return ret;
40183f157a2fSChris Mason }
40193f157a2fSChris Mason 
40203f157a2fSChris Mason /*
40213f157a2fSChris Mason  * this is similar to btrfs_next_leaf, but does not try to preserve
40223f157a2fSChris Mason  * and fixup the path.  It looks for and returns the next key in the
40233f157a2fSChris Mason  * tree based on the current path and the cache_only and min_trans
40243f157a2fSChris Mason  * parameters.
40253f157a2fSChris Mason  *
40263f157a2fSChris Mason  * 0 is returned if another key is found, < 0 if there are any errors
40273f157a2fSChris Mason  * and 1 is returned if there are no higher keys in the tree
40283f157a2fSChris Mason  *
40293f157a2fSChris Mason  * path->keep_locks should be set to 1 on the search made before
40303f157a2fSChris Mason  * calling this function.
40313f157a2fSChris Mason  */
4032e7a84565SChris Mason int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
403333c66f43SYan Zheng 			struct btrfs_key *key, int level,
40343f157a2fSChris Mason 			int cache_only, u64 min_trans)
4035e7a84565SChris Mason {
4036e7a84565SChris Mason 	int slot;
4037e7a84565SChris Mason 	struct extent_buffer *c;
4038e7a84565SChris Mason 
4039934d375bSChris Mason 	WARN_ON(!path->keep_locks);
4040e7a84565SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
4041e7a84565SChris Mason 		if (!path->nodes[level])
4042e7a84565SChris Mason 			return 1;
4043e7a84565SChris Mason 
4044e7a84565SChris Mason 		slot = path->slots[level] + 1;
4045e7a84565SChris Mason 		c = path->nodes[level];
40463f157a2fSChris Mason next:
4047e7a84565SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
404833c66f43SYan Zheng 			int ret;
404933c66f43SYan Zheng 			int orig_lowest;
405033c66f43SYan Zheng 			struct btrfs_key cur_key;
405133c66f43SYan Zheng 			if (level + 1 >= BTRFS_MAX_LEVEL ||
405233c66f43SYan Zheng 			    !path->nodes[level + 1])
4053e7a84565SChris Mason 				return 1;
405433c66f43SYan Zheng 
405533c66f43SYan Zheng 			if (path->locks[level + 1]) {
405633c66f43SYan Zheng 				level++;
4057e7a84565SChris Mason 				continue;
4058e7a84565SChris Mason 			}
405933c66f43SYan Zheng 
406033c66f43SYan Zheng 			slot = btrfs_header_nritems(c) - 1;
406133c66f43SYan Zheng 			if (level == 0)
406233c66f43SYan Zheng 				btrfs_item_key_to_cpu(c, &cur_key, slot);
406333c66f43SYan Zheng 			else
406433c66f43SYan Zheng 				btrfs_node_key_to_cpu(c, &cur_key, slot);
406533c66f43SYan Zheng 
406633c66f43SYan Zheng 			orig_lowest = path->lowest_level;
4067b3b4aa74SDavid Sterba 			btrfs_release_path(path);
406833c66f43SYan Zheng 			path->lowest_level = level;
406933c66f43SYan Zheng 			ret = btrfs_search_slot(NULL, root, &cur_key, path,
407033c66f43SYan Zheng 						0, 0);
407133c66f43SYan Zheng 			path->lowest_level = orig_lowest;
407233c66f43SYan Zheng 			if (ret < 0)
407333c66f43SYan Zheng 				return ret;
407433c66f43SYan Zheng 
407533c66f43SYan Zheng 			c = path->nodes[level];
407633c66f43SYan Zheng 			slot = path->slots[level];
407733c66f43SYan Zheng 			if (ret == 0)
407833c66f43SYan Zheng 				slot++;
407933c66f43SYan Zheng 			goto next;
408033c66f43SYan Zheng 		}
408133c66f43SYan Zheng 
4082e7a84565SChris Mason 		if (level == 0)
4083e7a84565SChris Mason 			btrfs_item_key_to_cpu(c, key, slot);
40843f157a2fSChris Mason 		else {
40853f157a2fSChris Mason 			u64 blockptr = btrfs_node_blockptr(c, slot);
40863f157a2fSChris Mason 			u64 gen = btrfs_node_ptr_generation(c, slot);
40873f157a2fSChris Mason 
40883f157a2fSChris Mason 			if (cache_only) {
40893f157a2fSChris Mason 				struct extent_buffer *cur;
40903f157a2fSChris Mason 				cur = btrfs_find_tree_block(root, blockptr,
40913f157a2fSChris Mason 					    btrfs_level_size(root, level - 1));
40923f157a2fSChris Mason 				if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
40933f157a2fSChris Mason 					slot++;
40943f157a2fSChris Mason 					if (cur)
40953f157a2fSChris Mason 						free_extent_buffer(cur);
40963f157a2fSChris Mason 					goto next;
40973f157a2fSChris Mason 				}
40983f157a2fSChris Mason 				free_extent_buffer(cur);
40993f157a2fSChris Mason 			}
41003f157a2fSChris Mason 			if (gen < min_trans) {
41013f157a2fSChris Mason 				slot++;
41023f157a2fSChris Mason 				goto next;
41033f157a2fSChris Mason 			}
4104e7a84565SChris Mason 			btrfs_node_key_to_cpu(c, key, slot);
41053f157a2fSChris Mason 		}
4106e7a84565SChris Mason 		return 0;
4107e7a84565SChris Mason 	}
4108e7a84565SChris Mason 	return 1;
4109e7a84565SChris Mason }
4110e7a84565SChris Mason 
41117bb86316SChris Mason /*
4112925baeddSChris Mason  * search the tree again to find a leaf with greater keys
41130f70abe2SChris Mason  * returns 0 if it found something or 1 if there are no greater leaves.
41140f70abe2SChris Mason  * returns < 0 on io errors.
411597571fd0SChris Mason  */
4116234b63a0SChris Mason int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
4117d97e63b6SChris Mason {
4118d97e63b6SChris Mason 	int slot;
41198e73f275SChris Mason 	int level;
41205f39d397SChris Mason 	struct extent_buffer *c;
41218e73f275SChris Mason 	struct extent_buffer *next;
4122925baeddSChris Mason 	struct btrfs_key key;
4123925baeddSChris Mason 	u32 nritems;
4124925baeddSChris Mason 	int ret;
41258e73f275SChris Mason 	int old_spinning = path->leave_spinning;
4126bd681513SChris Mason 	int next_rw_lock = 0;
4127925baeddSChris Mason 
4128925baeddSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
4129d397712bSChris Mason 	if (nritems == 0)
4130925baeddSChris Mason 		return 1;
4131925baeddSChris Mason 
41328e73f275SChris Mason 	btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
41338e73f275SChris Mason again:
41348e73f275SChris Mason 	level = 1;
41358e73f275SChris Mason 	next = NULL;
4136bd681513SChris Mason 	next_rw_lock = 0;
4137b3b4aa74SDavid Sterba 	btrfs_release_path(path);
41388e73f275SChris Mason 
4139a2135011SChris Mason 	path->keep_locks = 1;
41408e73f275SChris Mason 	path->leave_spinning = 1;
41418e73f275SChris Mason 
4142925baeddSChris Mason 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4143925baeddSChris Mason 	path->keep_locks = 0;
4144925baeddSChris Mason 
4145925baeddSChris Mason 	if (ret < 0)
4146925baeddSChris Mason 		return ret;
4147925baeddSChris Mason 
4148a2135011SChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
4149168fd7d2SChris Mason 	/*
4150168fd7d2SChris Mason 	 * by releasing the path above we dropped all our locks.  A balance
4151168fd7d2SChris Mason 	 * could have added more items next to the key that used to be
4152168fd7d2SChris Mason 	 * at the very end of the block.  So, check again here and
4153168fd7d2SChris Mason 	 * advance the path if there are now more items available.
4154168fd7d2SChris Mason 	 */
4155a2135011SChris Mason 	if (nritems > 0 && path->slots[0] < nritems - 1) {
4156e457afecSYan Zheng 		if (ret == 0)
4157168fd7d2SChris Mason 			path->slots[0]++;
41588e73f275SChris Mason 		ret = 0;
4159925baeddSChris Mason 		goto done;
4160925baeddSChris Mason 	}
4161d97e63b6SChris Mason 
4162234b63a0SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
41638e73f275SChris Mason 		if (!path->nodes[level]) {
41648e73f275SChris Mason 			ret = 1;
41658e73f275SChris Mason 			goto done;
41668e73f275SChris Mason 		}
41675f39d397SChris Mason 
4168d97e63b6SChris Mason 		slot = path->slots[level] + 1;
4169d97e63b6SChris Mason 		c = path->nodes[level];
41705f39d397SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
4171d97e63b6SChris Mason 			level++;
41728e73f275SChris Mason 			if (level == BTRFS_MAX_LEVEL) {
41738e73f275SChris Mason 				ret = 1;
41748e73f275SChris Mason 				goto done;
41758e73f275SChris Mason 			}
4176d97e63b6SChris Mason 			continue;
4177d97e63b6SChris Mason 		}
41785f39d397SChris Mason 
4179925baeddSChris Mason 		if (next) {
4180bd681513SChris Mason 			btrfs_tree_unlock_rw(next, next_rw_lock);
41815f39d397SChris Mason 			free_extent_buffer(next);
4182925baeddSChris Mason 		}
41835f39d397SChris Mason 
41848e73f275SChris Mason 		next = c;
4185bd681513SChris Mason 		next_rw_lock = path->locks[level];
41868e73f275SChris Mason 		ret = read_block_for_search(NULL, root, path, &next, level,
41878e73f275SChris Mason 					    slot, &key);
41888e73f275SChris Mason 		if (ret == -EAGAIN)
41898e73f275SChris Mason 			goto again;
41905f39d397SChris Mason 
419176a05b35SChris Mason 		if (ret < 0) {
4192b3b4aa74SDavid Sterba 			btrfs_release_path(path);
419376a05b35SChris Mason 			goto done;
419476a05b35SChris Mason 		}
419576a05b35SChris Mason 
41965cd57b2cSChris Mason 		if (!path->skip_locking) {
4197bd681513SChris Mason 			ret = btrfs_try_tree_read_lock(next);
41988e73f275SChris Mason 			if (!ret) {
41998e73f275SChris Mason 				btrfs_set_path_blocking(path);
4200bd681513SChris Mason 				btrfs_tree_read_lock(next);
4201bd681513SChris Mason 				btrfs_clear_path_blocking(path, next,
4202bd681513SChris Mason 							  BTRFS_READ_LOCK);
42038e73f275SChris Mason 			}
4204bd681513SChris Mason 			next_rw_lock = BTRFS_READ_LOCK;
4205bd681513SChris Mason 		}
4206d97e63b6SChris Mason 		break;
4207d97e63b6SChris Mason 	}
4208d97e63b6SChris Mason 	path->slots[level] = slot;
4209d97e63b6SChris Mason 	while (1) {
4210d97e63b6SChris Mason 		level--;
4211d97e63b6SChris Mason 		c = path->nodes[level];
4212925baeddSChris Mason 		if (path->locks[level])
4213bd681513SChris Mason 			btrfs_tree_unlock_rw(c, path->locks[level]);
42148e73f275SChris Mason 
42155f39d397SChris Mason 		free_extent_buffer(c);
4216d97e63b6SChris Mason 		path->nodes[level] = next;
4217d97e63b6SChris Mason 		path->slots[level] = 0;
4218a74a4b97SChris Mason 		if (!path->skip_locking)
4219bd681513SChris Mason 			path->locks[level] = next_rw_lock;
4220d97e63b6SChris Mason 		if (!level)
4221d97e63b6SChris Mason 			break;
4222b4ce94deSChris Mason 
42238e73f275SChris Mason 		ret = read_block_for_search(NULL, root, path, &next, level,
42248e73f275SChris Mason 					    0, &key);
42258e73f275SChris Mason 		if (ret == -EAGAIN)
42268e73f275SChris Mason 			goto again;
42278e73f275SChris Mason 
422876a05b35SChris Mason 		if (ret < 0) {
4229b3b4aa74SDavid Sterba 			btrfs_release_path(path);
423076a05b35SChris Mason 			goto done;
423176a05b35SChris Mason 		}
423276a05b35SChris Mason 
42335cd57b2cSChris Mason 		if (!path->skip_locking) {
4234bd681513SChris Mason 			ret = btrfs_try_tree_read_lock(next);
42358e73f275SChris Mason 			if (!ret) {
42368e73f275SChris Mason 				btrfs_set_path_blocking(path);
4237bd681513SChris Mason 				btrfs_tree_read_lock(next);
4238bd681513SChris Mason 				btrfs_clear_path_blocking(path, next,
4239bd681513SChris Mason 							  BTRFS_READ_LOCK);
42408e73f275SChris Mason 			}
4241bd681513SChris Mason 			next_rw_lock = BTRFS_READ_LOCK;
4242bd681513SChris Mason 		}
4243d97e63b6SChris Mason 	}
42448e73f275SChris Mason 	ret = 0;
4245925baeddSChris Mason done:
4246925baeddSChris Mason 	unlock_up(path, 0, 1);
42478e73f275SChris Mason 	path->leave_spinning = old_spinning;
42488e73f275SChris Mason 	if (!old_spinning)
42498e73f275SChris Mason 		btrfs_set_path_blocking(path);
42508e73f275SChris Mason 
42518e73f275SChris Mason 	return ret;
4252d97e63b6SChris Mason }
42530b86a832SChris Mason 
42543f157a2fSChris Mason /*
42553f157a2fSChris Mason  * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
42563f157a2fSChris Mason  * searching until it gets past min_objectid or finds an item of 'type'
42573f157a2fSChris Mason  *
42583f157a2fSChris Mason  * returns 0 if something is found, 1 if nothing was found and < 0 on error
42593f157a2fSChris Mason  */
42600b86a832SChris Mason int btrfs_previous_item(struct btrfs_root *root,
42610b86a832SChris Mason 			struct btrfs_path *path, u64 min_objectid,
42620b86a832SChris Mason 			int type)
42630b86a832SChris Mason {
42640b86a832SChris Mason 	struct btrfs_key found_key;
42650b86a832SChris Mason 	struct extent_buffer *leaf;
4266e02119d5SChris Mason 	u32 nritems;
42670b86a832SChris Mason 	int ret;
42680b86a832SChris Mason 
42690b86a832SChris Mason 	while (1) {
42700b86a832SChris Mason 		if (path->slots[0] == 0) {
4271b4ce94deSChris Mason 			btrfs_set_path_blocking(path);
42720b86a832SChris Mason 			ret = btrfs_prev_leaf(root, path);
42730b86a832SChris Mason 			if (ret != 0)
42740b86a832SChris Mason 				return ret;
42750b86a832SChris Mason 		} else {
42760b86a832SChris Mason 			path->slots[0]--;
42770b86a832SChris Mason 		}
42780b86a832SChris Mason 		leaf = path->nodes[0];
4279e02119d5SChris Mason 		nritems = btrfs_header_nritems(leaf);
4280e02119d5SChris Mason 		if (nritems == 0)
4281e02119d5SChris Mason 			return 1;
4282e02119d5SChris Mason 		if (path->slots[0] == nritems)
4283e02119d5SChris Mason 			path->slots[0]--;
4284e02119d5SChris Mason 
42850b86a832SChris Mason 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4286e02119d5SChris Mason 		if (found_key.objectid < min_objectid)
4287e02119d5SChris Mason 			break;
42880a4eefbbSYan Zheng 		if (found_key.type == type)
42890a4eefbbSYan Zheng 			return 0;
4290e02119d5SChris Mason 		if (found_key.objectid == min_objectid &&
4291e02119d5SChris Mason 		    found_key.type < type)
4292e02119d5SChris Mason 			break;
42930b86a832SChris Mason 	}
42940b86a832SChris Mason 	return 1;
42950b86a832SChris Mason }
4296