xref: /openbmc/linux/fs/btrfs/ctree.c (revision 25b8b936ed44814a5ce6fc3b2a21401f33cd56f6)
16cbd5570SChris Mason /*
2d352ac68SChris Mason  * Copyright (C) 2007,2008 Oracle.  All rights reserved.
36cbd5570SChris Mason  *
46cbd5570SChris Mason  * This program is free software; you can redistribute it and/or
56cbd5570SChris Mason  * modify it under the terms of the GNU General Public
66cbd5570SChris Mason  * License v2 as published by the Free Software Foundation.
76cbd5570SChris Mason  *
86cbd5570SChris Mason  * This program is distributed in the hope that it will be useful,
96cbd5570SChris Mason  * but WITHOUT ANY WARRANTY; without even the implied warranty of
106cbd5570SChris Mason  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
116cbd5570SChris Mason  * General Public License for more details.
126cbd5570SChris Mason  *
136cbd5570SChris Mason  * You should have received a copy of the GNU General Public
146cbd5570SChris Mason  * License along with this program; if not, write to the
156cbd5570SChris Mason  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
166cbd5570SChris Mason  * Boston, MA 021110-1307, USA.
176cbd5570SChris Mason  */
186cbd5570SChris Mason 
19a6b6e75eSChris Mason #include <linux/sched.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
21eb60ceacSChris Mason #include "ctree.h"
22eb60ceacSChris Mason #include "disk-io.h"
237f5c1516SChris Mason #include "transaction.h"
245f39d397SChris Mason #include "print-tree.h"
25925baeddSChris Mason #include "locking.h"
269a8dd150SChris Mason 
27e089f05cSChris Mason static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
28e089f05cSChris Mason 		      *root, struct btrfs_path *path, int level);
29e089f05cSChris Mason static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
30d4dbff95SChris Mason 		      *root, struct btrfs_key *ins_key,
31cc0c5538SChris Mason 		      struct btrfs_path *path, int data_size, int extend);
325f39d397SChris Mason static int push_node_left(struct btrfs_trans_handle *trans,
335f39d397SChris Mason 			  struct btrfs_root *root, struct extent_buffer *dst,
34971a1f66SChris Mason 			  struct extent_buffer *src, int empty);
355f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans,
365f39d397SChris Mason 			      struct btrfs_root *root,
375f39d397SChris Mason 			      struct extent_buffer *dst_buf,
385f39d397SChris Mason 			      struct extent_buffer *src_buf);
39e089f05cSChris Mason static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
40e089f05cSChris Mason 		   struct btrfs_path *path, int level, int slot);
41d97e63b6SChris Mason 
422c90e5d6SChris Mason struct btrfs_path *btrfs_alloc_path(void)
432c90e5d6SChris Mason {
44df24a2b9SChris Mason 	struct btrfs_path *path;
45e00f7308SJeff Mahoney 	path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
46df24a2b9SChris Mason 	return path;
472c90e5d6SChris Mason }
482c90e5d6SChris Mason 
49b4ce94deSChris Mason /*
50b4ce94deSChris Mason  * set all locked nodes in the path to blocking locks.  This should
51b4ce94deSChris Mason  * be done before scheduling
52b4ce94deSChris Mason  */
53b4ce94deSChris Mason noinline void btrfs_set_path_blocking(struct btrfs_path *p)
54b4ce94deSChris Mason {
55b4ce94deSChris Mason 	int i;
56b4ce94deSChris Mason 	for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
57b4ce94deSChris Mason 		if (p->nodes[i] && p->locks[i])
58b4ce94deSChris Mason 			btrfs_set_lock_blocking(p->nodes[i]);
59b4ce94deSChris Mason 	}
60b4ce94deSChris Mason }
61b4ce94deSChris Mason 
62b4ce94deSChris Mason /*
63b4ce94deSChris Mason  * reset all the locked nodes in the patch to spinning locks.
644008c04aSChris Mason  *
654008c04aSChris Mason  * held is used to keep lockdep happy, when lockdep is enabled
664008c04aSChris Mason  * we set held to a blocking lock before we go around and
674008c04aSChris Mason  * retake all the spinlocks in the path.  You can safely use NULL
684008c04aSChris Mason  * for held
69b4ce94deSChris Mason  */
704008c04aSChris Mason noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
714008c04aSChris Mason 					struct extent_buffer *held)
72b4ce94deSChris Mason {
73b4ce94deSChris Mason 	int i;
744008c04aSChris Mason 
754008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC
764008c04aSChris Mason 	/* lockdep really cares that we take all of these spinlocks
774008c04aSChris Mason 	 * in the right order.  If any of the locks in the path are not
784008c04aSChris Mason 	 * currently blocking, it is going to complain.  So, make really
794008c04aSChris Mason 	 * really sure by forcing the path to blocking before we clear
804008c04aSChris Mason 	 * the path blocking.
814008c04aSChris Mason 	 */
824008c04aSChris Mason 	if (held)
834008c04aSChris Mason 		btrfs_set_lock_blocking(held);
844008c04aSChris Mason 	btrfs_set_path_blocking(p);
854008c04aSChris Mason #endif
864008c04aSChris Mason 
874008c04aSChris Mason 	for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
88b4ce94deSChris Mason 		if (p->nodes[i] && p->locks[i])
89b4ce94deSChris Mason 			btrfs_clear_lock_blocking(p->nodes[i]);
90b4ce94deSChris Mason 	}
914008c04aSChris Mason 
924008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC
934008c04aSChris Mason 	if (held)
944008c04aSChris Mason 		btrfs_clear_lock_blocking(held);
954008c04aSChris Mason #endif
96b4ce94deSChris Mason }
97b4ce94deSChris Mason 
98d352ac68SChris Mason /* this also releases the path */
992c90e5d6SChris Mason void btrfs_free_path(struct btrfs_path *p)
1002c90e5d6SChris Mason {
101ff175d57SJesper Juhl 	if (!p)
102ff175d57SJesper Juhl 		return;
103b3b4aa74SDavid Sterba 	btrfs_release_path(p);
1042c90e5d6SChris Mason 	kmem_cache_free(btrfs_path_cachep, p);
1052c90e5d6SChris Mason }
1062c90e5d6SChris Mason 
107d352ac68SChris Mason /*
108d352ac68SChris Mason  * path release drops references on the extent buffers in the path
109d352ac68SChris Mason  * and it drops any locks held by this path
110d352ac68SChris Mason  *
111d352ac68SChris Mason  * It is safe to call this on paths that no locks or extent buffers held.
112d352ac68SChris Mason  */
113b3b4aa74SDavid Sterba noinline void btrfs_release_path(struct btrfs_path *p)
114eb60ceacSChris Mason {
115eb60ceacSChris Mason 	int i;
116a2135011SChris Mason 
117234b63a0SChris Mason 	for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
1183f157a2fSChris Mason 		p->slots[i] = 0;
119eb60ceacSChris Mason 		if (!p->nodes[i])
120925baeddSChris Mason 			continue;
121925baeddSChris Mason 		if (p->locks[i]) {
122925baeddSChris Mason 			btrfs_tree_unlock(p->nodes[i]);
123925baeddSChris Mason 			p->locks[i] = 0;
124925baeddSChris Mason 		}
1255f39d397SChris Mason 		free_extent_buffer(p->nodes[i]);
1263f157a2fSChris Mason 		p->nodes[i] = NULL;
127eb60ceacSChris Mason 	}
128eb60ceacSChris Mason }
129eb60ceacSChris Mason 
130d352ac68SChris Mason /*
131d352ac68SChris Mason  * safely gets a reference on the root node of a tree.  A lock
132d352ac68SChris Mason  * is not taken, so a concurrent writer may put a different node
133d352ac68SChris Mason  * at the root of the tree.  See btrfs_lock_root_node for the
134d352ac68SChris Mason  * looping required.
135d352ac68SChris Mason  *
136d352ac68SChris Mason  * The extent buffer returned by this has a reference taken, so
137d352ac68SChris Mason  * it won't disappear.  It may stop being the root of the tree
138d352ac68SChris Mason  * at any time because there are no locks held.
139d352ac68SChris Mason  */
140925baeddSChris Mason struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
141925baeddSChris Mason {
142925baeddSChris Mason 	struct extent_buffer *eb;
143240f62c8SChris Mason 
144240f62c8SChris Mason 	rcu_read_lock();
145240f62c8SChris Mason 	eb = rcu_dereference(root->node);
146925baeddSChris Mason 	extent_buffer_get(eb);
147240f62c8SChris Mason 	rcu_read_unlock();
148925baeddSChris Mason 	return eb;
149925baeddSChris Mason }
150925baeddSChris Mason 
151d352ac68SChris Mason /* loop around taking references on and locking the root node of the
152d352ac68SChris Mason  * tree until you end up with a lock on the root.  A locked buffer
153d352ac68SChris Mason  * is returned, with a reference held.
154d352ac68SChris Mason  */
155925baeddSChris Mason struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
156925baeddSChris Mason {
157925baeddSChris Mason 	struct extent_buffer *eb;
158925baeddSChris Mason 
159925baeddSChris Mason 	while (1) {
160925baeddSChris Mason 		eb = btrfs_root_node(root);
161925baeddSChris Mason 		btrfs_tree_lock(eb);
162240f62c8SChris Mason 		if (eb == root->node)
163925baeddSChris Mason 			break;
164925baeddSChris Mason 		btrfs_tree_unlock(eb);
165925baeddSChris Mason 		free_extent_buffer(eb);
166925baeddSChris Mason 	}
167925baeddSChris Mason 	return eb;
168925baeddSChris Mason }
169925baeddSChris Mason 
170d352ac68SChris Mason /* cowonly root (everything not a reference counted cow subvolume), just get
171d352ac68SChris Mason  * put onto a simple dirty list.  transaction.c walks this to make sure they
172d352ac68SChris Mason  * get properly updated on disk.
173d352ac68SChris Mason  */
1740b86a832SChris Mason static void add_root_to_dirty_list(struct btrfs_root *root)
1750b86a832SChris Mason {
1760b86a832SChris Mason 	if (root->track_dirty && list_empty(&root->dirty_list)) {
1770b86a832SChris Mason 		list_add(&root->dirty_list,
1780b86a832SChris Mason 			 &root->fs_info->dirty_cowonly_roots);
1790b86a832SChris Mason 	}
1800b86a832SChris Mason }
1810b86a832SChris Mason 
182d352ac68SChris Mason /*
183d352ac68SChris Mason  * used by snapshot creation to make a copy of a root for a tree with
184d352ac68SChris Mason  * a given objectid.  The buffer with the new root node is returned in
185d352ac68SChris Mason  * cow_ret, and this func returns zero on success or a negative error code.
186d352ac68SChris Mason  */
187be20aa9dSChris Mason int btrfs_copy_root(struct btrfs_trans_handle *trans,
188be20aa9dSChris Mason 		      struct btrfs_root *root,
189be20aa9dSChris Mason 		      struct extent_buffer *buf,
190be20aa9dSChris Mason 		      struct extent_buffer **cow_ret, u64 new_root_objectid)
191be20aa9dSChris Mason {
192be20aa9dSChris Mason 	struct extent_buffer *cow;
193be20aa9dSChris Mason 	int ret = 0;
194be20aa9dSChris Mason 	int level;
1955d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
196be20aa9dSChris Mason 
197be20aa9dSChris Mason 	WARN_ON(root->ref_cows && trans->transid !=
198be20aa9dSChris Mason 		root->fs_info->running_transaction->transid);
199be20aa9dSChris Mason 	WARN_ON(root->ref_cows && trans->transid != root->last_trans);
200be20aa9dSChris Mason 
201be20aa9dSChris Mason 	level = btrfs_header_level(buf);
2025d4f98a2SYan Zheng 	if (level == 0)
2035d4f98a2SYan Zheng 		btrfs_item_key(buf, &disk_key, 0);
2045d4f98a2SYan Zheng 	else
2055d4f98a2SYan Zheng 		btrfs_node_key(buf, &disk_key, 0);
20631840ae1SZheng Yan 
2075d4f98a2SYan Zheng 	cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
2085d4f98a2SYan Zheng 				     new_root_objectid, &disk_key, level,
2095d4f98a2SYan Zheng 				     buf->start, 0);
2105d4f98a2SYan Zheng 	if (IS_ERR(cow))
211be20aa9dSChris Mason 		return PTR_ERR(cow);
212be20aa9dSChris Mason 
213be20aa9dSChris Mason 	copy_extent_buffer(cow, buf, 0, 0, cow->len);
214be20aa9dSChris Mason 	btrfs_set_header_bytenr(cow, cow->start);
215be20aa9dSChris Mason 	btrfs_set_header_generation(cow, trans->transid);
2165d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
2175d4f98a2SYan Zheng 	btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
2185d4f98a2SYan Zheng 				     BTRFS_HEADER_FLAG_RELOC);
2195d4f98a2SYan Zheng 	if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
2205d4f98a2SYan Zheng 		btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
2215d4f98a2SYan Zheng 	else
222be20aa9dSChris Mason 		btrfs_set_header_owner(cow, new_root_objectid);
223be20aa9dSChris Mason 
2242b82032cSYan Zheng 	write_extent_buffer(cow, root->fs_info->fsid,
2252b82032cSYan Zheng 			    (unsigned long)btrfs_header_fsid(cow),
2262b82032cSYan Zheng 			    BTRFS_FSID_SIZE);
2272b82032cSYan Zheng 
228be20aa9dSChris Mason 	WARN_ON(btrfs_header_generation(buf) > trans->transid);
2295d4f98a2SYan Zheng 	if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
2305d4f98a2SYan Zheng 		ret = btrfs_inc_ref(trans, root, cow, 1);
2315d4f98a2SYan Zheng 	else
2325d4f98a2SYan Zheng 		ret = btrfs_inc_ref(trans, root, cow, 0);
2334aec2b52SChris Mason 
234be20aa9dSChris Mason 	if (ret)
235be20aa9dSChris Mason 		return ret;
236be20aa9dSChris Mason 
237be20aa9dSChris Mason 	btrfs_mark_buffer_dirty(cow);
238be20aa9dSChris Mason 	*cow_ret = cow;
239be20aa9dSChris Mason 	return 0;
240be20aa9dSChris Mason }
241be20aa9dSChris Mason 
242d352ac68SChris Mason /*
2435d4f98a2SYan Zheng  * check if the tree block can be shared by multiple trees
2445d4f98a2SYan Zheng  */
2455d4f98a2SYan Zheng int btrfs_block_can_be_shared(struct btrfs_root *root,
2465d4f98a2SYan Zheng 			      struct extent_buffer *buf)
2475d4f98a2SYan Zheng {
2485d4f98a2SYan Zheng 	/*
2495d4f98a2SYan Zheng 	 * Tree blocks not in refernece counted trees and tree roots
2505d4f98a2SYan Zheng 	 * are never shared. If a block was allocated after the last
2515d4f98a2SYan Zheng 	 * snapshot and the block was not allocated by tree relocation,
2525d4f98a2SYan Zheng 	 * we know the block is not shared.
2535d4f98a2SYan Zheng 	 */
2545d4f98a2SYan Zheng 	if (root->ref_cows &&
2555d4f98a2SYan Zheng 	    buf != root->node && buf != root->commit_root &&
2565d4f98a2SYan Zheng 	    (btrfs_header_generation(buf) <=
2575d4f98a2SYan Zheng 	     btrfs_root_last_snapshot(&root->root_item) ||
2585d4f98a2SYan Zheng 	     btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
2595d4f98a2SYan Zheng 		return 1;
2605d4f98a2SYan Zheng #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2615d4f98a2SYan Zheng 	if (root->ref_cows &&
2625d4f98a2SYan Zheng 	    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
2635d4f98a2SYan Zheng 		return 1;
2645d4f98a2SYan Zheng #endif
2655d4f98a2SYan Zheng 	return 0;
2665d4f98a2SYan Zheng }
2675d4f98a2SYan Zheng 
2685d4f98a2SYan Zheng static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
2695d4f98a2SYan Zheng 				       struct btrfs_root *root,
2705d4f98a2SYan Zheng 				       struct extent_buffer *buf,
271f0486c68SYan, Zheng 				       struct extent_buffer *cow,
272f0486c68SYan, Zheng 				       int *last_ref)
2735d4f98a2SYan Zheng {
2745d4f98a2SYan Zheng 	u64 refs;
2755d4f98a2SYan Zheng 	u64 owner;
2765d4f98a2SYan Zheng 	u64 flags;
2775d4f98a2SYan Zheng 	u64 new_flags = 0;
2785d4f98a2SYan Zheng 	int ret;
2795d4f98a2SYan Zheng 
2805d4f98a2SYan Zheng 	/*
2815d4f98a2SYan Zheng 	 * Backrefs update rules:
2825d4f98a2SYan Zheng 	 *
2835d4f98a2SYan Zheng 	 * Always use full backrefs for extent pointers in tree block
2845d4f98a2SYan Zheng 	 * allocated by tree relocation.
2855d4f98a2SYan Zheng 	 *
2865d4f98a2SYan Zheng 	 * If a shared tree block is no longer referenced by its owner
2875d4f98a2SYan Zheng 	 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
2885d4f98a2SYan Zheng 	 * use full backrefs for extent pointers in tree block.
2895d4f98a2SYan Zheng 	 *
2905d4f98a2SYan Zheng 	 * If a tree block is been relocating
2915d4f98a2SYan Zheng 	 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
2925d4f98a2SYan Zheng 	 * use full backrefs for extent pointers in tree block.
2935d4f98a2SYan Zheng 	 * The reason for this is some operations (such as drop tree)
2945d4f98a2SYan Zheng 	 * are only allowed for blocks use full backrefs.
2955d4f98a2SYan Zheng 	 */
2965d4f98a2SYan Zheng 
2975d4f98a2SYan Zheng 	if (btrfs_block_can_be_shared(root, buf)) {
2985d4f98a2SYan Zheng 		ret = btrfs_lookup_extent_info(trans, root, buf->start,
2995d4f98a2SYan Zheng 					       buf->len, &refs, &flags);
3005d4f98a2SYan Zheng 		BUG_ON(ret);
3015d4f98a2SYan Zheng 		BUG_ON(refs == 0);
3025d4f98a2SYan Zheng 	} else {
3035d4f98a2SYan Zheng 		refs = 1;
3045d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
3055d4f98a2SYan Zheng 		    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
3065d4f98a2SYan Zheng 			flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
3075d4f98a2SYan Zheng 		else
3085d4f98a2SYan Zheng 			flags = 0;
3095d4f98a2SYan Zheng 	}
3105d4f98a2SYan Zheng 
3115d4f98a2SYan Zheng 	owner = btrfs_header_owner(buf);
3125d4f98a2SYan Zheng 	BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
3135d4f98a2SYan Zheng 	       !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
3145d4f98a2SYan Zheng 
3155d4f98a2SYan Zheng 	if (refs > 1) {
3165d4f98a2SYan Zheng 		if ((owner == root->root_key.objectid ||
3175d4f98a2SYan Zheng 		     root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
3185d4f98a2SYan Zheng 		    !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
3195d4f98a2SYan Zheng 			ret = btrfs_inc_ref(trans, root, buf, 1);
3205d4f98a2SYan Zheng 			BUG_ON(ret);
3215d4f98a2SYan Zheng 
3225d4f98a2SYan Zheng 			if (root->root_key.objectid ==
3235d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID) {
3245d4f98a2SYan Zheng 				ret = btrfs_dec_ref(trans, root, buf, 0);
3255d4f98a2SYan Zheng 				BUG_ON(ret);
3265d4f98a2SYan Zheng 				ret = btrfs_inc_ref(trans, root, cow, 1);
3275d4f98a2SYan Zheng 				BUG_ON(ret);
3285d4f98a2SYan Zheng 			}
3295d4f98a2SYan Zheng 			new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
3305d4f98a2SYan Zheng 		} else {
3315d4f98a2SYan Zheng 
3325d4f98a2SYan Zheng 			if (root->root_key.objectid ==
3335d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID)
3345d4f98a2SYan Zheng 				ret = btrfs_inc_ref(trans, root, cow, 1);
3355d4f98a2SYan Zheng 			else
3365d4f98a2SYan Zheng 				ret = btrfs_inc_ref(trans, root, cow, 0);
3375d4f98a2SYan Zheng 			BUG_ON(ret);
3385d4f98a2SYan Zheng 		}
3395d4f98a2SYan Zheng 		if (new_flags != 0) {
3405d4f98a2SYan Zheng 			ret = btrfs_set_disk_extent_flags(trans, root,
3415d4f98a2SYan Zheng 							  buf->start,
3425d4f98a2SYan Zheng 							  buf->len,
3435d4f98a2SYan Zheng 							  new_flags, 0);
3445d4f98a2SYan Zheng 			BUG_ON(ret);
3455d4f98a2SYan Zheng 		}
3465d4f98a2SYan Zheng 	} else {
3475d4f98a2SYan Zheng 		if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
3485d4f98a2SYan Zheng 			if (root->root_key.objectid ==
3495d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID)
3505d4f98a2SYan Zheng 				ret = btrfs_inc_ref(trans, root, cow, 1);
3515d4f98a2SYan Zheng 			else
3525d4f98a2SYan Zheng 				ret = btrfs_inc_ref(trans, root, cow, 0);
3535d4f98a2SYan Zheng 			BUG_ON(ret);
3545d4f98a2SYan Zheng 			ret = btrfs_dec_ref(trans, root, buf, 1);
3555d4f98a2SYan Zheng 			BUG_ON(ret);
3565d4f98a2SYan Zheng 		}
3575d4f98a2SYan Zheng 		clean_tree_block(trans, root, buf);
358f0486c68SYan, Zheng 		*last_ref = 1;
3595d4f98a2SYan Zheng 	}
3605d4f98a2SYan Zheng 	return 0;
3615d4f98a2SYan Zheng }
3625d4f98a2SYan Zheng 
3635d4f98a2SYan Zheng /*
364d397712bSChris Mason  * does the dirty work in cow of a single block.  The parent block (if
365d397712bSChris Mason  * supplied) is updated to point to the new cow copy.  The new buffer is marked
366d397712bSChris Mason  * dirty and returned locked.  If you modify the block it needs to be marked
367d397712bSChris Mason  * dirty again.
368d352ac68SChris Mason  *
369d352ac68SChris Mason  * search_start -- an allocation hint for the new block
370d352ac68SChris Mason  *
371d397712bSChris Mason  * empty_size -- a hint that you plan on doing more cow.  This is the size in
372d397712bSChris Mason  * bytes the allocator should try to find free next to the block it returns.
373d397712bSChris Mason  * This is just a hint and may be ignored by the allocator.
374d352ac68SChris Mason  */
375d397712bSChris Mason static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
3765f39d397SChris Mason 			     struct btrfs_root *root,
3775f39d397SChris Mason 			     struct extent_buffer *buf,
3785f39d397SChris Mason 			     struct extent_buffer *parent, int parent_slot,
3795f39d397SChris Mason 			     struct extent_buffer **cow_ret,
3809fa8cfe7SChris Mason 			     u64 search_start, u64 empty_size)
3816702ed49SChris Mason {
3825d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
3835f39d397SChris Mason 	struct extent_buffer *cow;
3847bb86316SChris Mason 	int level;
385f0486c68SYan, Zheng 	int last_ref = 0;
386925baeddSChris Mason 	int unlock_orig = 0;
3875d4f98a2SYan Zheng 	u64 parent_start;
3886702ed49SChris Mason 
389925baeddSChris Mason 	if (*cow_ret == buf)
390925baeddSChris Mason 		unlock_orig = 1;
391925baeddSChris Mason 
392b9447ef8SChris Mason 	btrfs_assert_tree_locked(buf);
393925baeddSChris Mason 
3947bb86316SChris Mason 	WARN_ON(root->ref_cows && trans->transid !=
3957bb86316SChris Mason 		root->fs_info->running_transaction->transid);
3966702ed49SChris Mason 	WARN_ON(root->ref_cows && trans->transid != root->last_trans);
3975f39d397SChris Mason 
3987bb86316SChris Mason 	level = btrfs_header_level(buf);
39931840ae1SZheng Yan 
4005d4f98a2SYan Zheng 	if (level == 0)
4015d4f98a2SYan Zheng 		btrfs_item_key(buf, &disk_key, 0);
4025d4f98a2SYan Zheng 	else
4035d4f98a2SYan Zheng 		btrfs_node_key(buf, &disk_key, 0);
4045d4f98a2SYan Zheng 
4055d4f98a2SYan Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
4065d4f98a2SYan Zheng 		if (parent)
4075d4f98a2SYan Zheng 			parent_start = parent->start;
4085d4f98a2SYan Zheng 		else
4095d4f98a2SYan Zheng 			parent_start = 0;
4105d4f98a2SYan Zheng 	} else
4115d4f98a2SYan Zheng 		parent_start = 0;
4125d4f98a2SYan Zheng 
4135d4f98a2SYan Zheng 	cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
4145d4f98a2SYan Zheng 				     root->root_key.objectid, &disk_key,
4155d4f98a2SYan Zheng 				     level, search_start, empty_size);
4166702ed49SChris Mason 	if (IS_ERR(cow))
4176702ed49SChris Mason 		return PTR_ERR(cow);
4186702ed49SChris Mason 
419b4ce94deSChris Mason 	/* cow is set to blocking by btrfs_init_new_buffer */
420b4ce94deSChris Mason 
4215f39d397SChris Mason 	copy_extent_buffer(cow, buf, 0, 0, cow->len);
422db94535dSChris Mason 	btrfs_set_header_bytenr(cow, cow->start);
4235f39d397SChris Mason 	btrfs_set_header_generation(cow, trans->transid);
4245d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
4255d4f98a2SYan Zheng 	btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
4265d4f98a2SYan Zheng 				     BTRFS_HEADER_FLAG_RELOC);
4275d4f98a2SYan Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4285d4f98a2SYan Zheng 		btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
4295d4f98a2SYan Zheng 	else
4305f39d397SChris Mason 		btrfs_set_header_owner(cow, root->root_key.objectid);
4316702ed49SChris Mason 
4322b82032cSYan Zheng 	write_extent_buffer(cow, root->fs_info->fsid,
4332b82032cSYan Zheng 			    (unsigned long)btrfs_header_fsid(cow),
4342b82032cSYan Zheng 			    BTRFS_FSID_SIZE);
4352b82032cSYan Zheng 
436f0486c68SYan, Zheng 	update_ref_for_cow(trans, root, buf, cow, &last_ref);
4371a40e23bSZheng Yan 
4383fd0a558SYan, Zheng 	if (root->ref_cows)
4393fd0a558SYan, Zheng 		btrfs_reloc_cow_block(trans, root, buf, cow);
4403fd0a558SYan, Zheng 
4416702ed49SChris Mason 	if (buf == root->node) {
442925baeddSChris Mason 		WARN_ON(parent && parent != buf);
4435d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
4445d4f98a2SYan Zheng 		    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
4455d4f98a2SYan Zheng 			parent_start = buf->start;
4465d4f98a2SYan Zheng 		else
4475d4f98a2SYan Zheng 			parent_start = 0;
448925baeddSChris Mason 
4495f39d397SChris Mason 		extent_buffer_get(cow);
450240f62c8SChris Mason 		rcu_assign_pointer(root->node, cow);
451925baeddSChris Mason 
452f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, buf, parent_start,
453f0486c68SYan, Zheng 				      last_ref);
4545f39d397SChris Mason 		free_extent_buffer(buf);
4550b86a832SChris Mason 		add_root_to_dirty_list(root);
4566702ed49SChris Mason 	} else {
4575d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4585d4f98a2SYan Zheng 			parent_start = parent->start;
4595d4f98a2SYan Zheng 		else
4605d4f98a2SYan Zheng 			parent_start = 0;
4615d4f98a2SYan Zheng 
4625d4f98a2SYan Zheng 		WARN_ON(trans->transid != btrfs_header_generation(parent));
4635f39d397SChris Mason 		btrfs_set_node_blockptr(parent, parent_slot,
464db94535dSChris Mason 					cow->start);
46574493f7aSChris Mason 		btrfs_set_node_ptr_generation(parent, parent_slot,
46674493f7aSChris Mason 					      trans->transid);
4676702ed49SChris Mason 		btrfs_mark_buffer_dirty(parent);
468f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, buf, parent_start,
469f0486c68SYan, Zheng 				      last_ref);
4706702ed49SChris Mason 	}
471925baeddSChris Mason 	if (unlock_orig)
472925baeddSChris Mason 		btrfs_tree_unlock(buf);
4735f39d397SChris Mason 	free_extent_buffer(buf);
4746702ed49SChris Mason 	btrfs_mark_buffer_dirty(cow);
4756702ed49SChris Mason 	*cow_ret = cow;
4766702ed49SChris Mason 	return 0;
4776702ed49SChris Mason }
4786702ed49SChris Mason 
4795d4f98a2SYan Zheng static inline int should_cow_block(struct btrfs_trans_handle *trans,
4805d4f98a2SYan Zheng 				   struct btrfs_root *root,
4815d4f98a2SYan Zheng 				   struct extent_buffer *buf)
4825d4f98a2SYan Zheng {
4835d4f98a2SYan Zheng 	if (btrfs_header_generation(buf) == trans->transid &&
4845d4f98a2SYan Zheng 	    !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
4855d4f98a2SYan Zheng 	    !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
4865d4f98a2SYan Zheng 	      btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
4875d4f98a2SYan Zheng 		return 0;
4885d4f98a2SYan Zheng 	return 1;
4895d4f98a2SYan Zheng }
4905d4f98a2SYan Zheng 
491d352ac68SChris Mason /*
492d352ac68SChris Mason  * cows a single block, see __btrfs_cow_block for the real work.
493d352ac68SChris Mason  * This version of it has extra checks so that a block isn't cow'd more than
494d352ac68SChris Mason  * once per transaction, as long as it hasn't been written yet
495d352ac68SChris Mason  */
496d397712bSChris Mason noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
4975f39d397SChris Mason 		    struct btrfs_root *root, struct extent_buffer *buf,
4985f39d397SChris Mason 		    struct extent_buffer *parent, int parent_slot,
4999fa8cfe7SChris Mason 		    struct extent_buffer **cow_ret)
50002217ed2SChris Mason {
5016702ed49SChris Mason 	u64 search_start;
502f510cfecSChris Mason 	int ret;
503dc17ff8fSChris Mason 
504ccd467d6SChris Mason 	if (trans->transaction != root->fs_info->running_transaction) {
505d397712bSChris Mason 		printk(KERN_CRIT "trans %llu running %llu\n",
506d397712bSChris Mason 		       (unsigned long long)trans->transid,
507d397712bSChris Mason 		       (unsigned long long)
508ccd467d6SChris Mason 		       root->fs_info->running_transaction->transid);
509ccd467d6SChris Mason 		WARN_ON(1);
510ccd467d6SChris Mason 	}
511ccd467d6SChris Mason 	if (trans->transid != root->fs_info->generation) {
512d397712bSChris Mason 		printk(KERN_CRIT "trans %llu running %llu\n",
513d397712bSChris Mason 		       (unsigned long long)trans->transid,
514d397712bSChris Mason 		       (unsigned long long)root->fs_info->generation);
515ccd467d6SChris Mason 		WARN_ON(1);
516ccd467d6SChris Mason 	}
517dc17ff8fSChris Mason 
5185d4f98a2SYan Zheng 	if (!should_cow_block(trans, root, buf)) {
51902217ed2SChris Mason 		*cow_ret = buf;
52002217ed2SChris Mason 		return 0;
52102217ed2SChris Mason 	}
522c487685dSChris Mason 
5230b86a832SChris Mason 	search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
524b4ce94deSChris Mason 
525b4ce94deSChris Mason 	if (parent)
526b4ce94deSChris Mason 		btrfs_set_lock_blocking(parent);
527b4ce94deSChris Mason 	btrfs_set_lock_blocking(buf);
528b4ce94deSChris Mason 
529f510cfecSChris Mason 	ret = __btrfs_cow_block(trans, root, buf, parent,
5309fa8cfe7SChris Mason 				 parent_slot, cow_ret, search_start, 0);
5311abe9b8aSliubo 
5321abe9b8aSliubo 	trace_btrfs_cow_block(root, buf, *cow_ret);
5331abe9b8aSliubo 
534f510cfecSChris Mason 	return ret;
5352c90e5d6SChris Mason }
5366702ed49SChris Mason 
537d352ac68SChris Mason /*
538d352ac68SChris Mason  * helper function for defrag to decide if two blocks pointed to by a
539d352ac68SChris Mason  * node are actually close by
540d352ac68SChris Mason  */
5416b80053dSChris Mason static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
5426702ed49SChris Mason {
5436b80053dSChris Mason 	if (blocknr < other && other - (blocknr + blocksize) < 32768)
5446702ed49SChris Mason 		return 1;
5456b80053dSChris Mason 	if (blocknr > other && blocknr - (other + blocksize) < 32768)
5466702ed49SChris Mason 		return 1;
54702217ed2SChris Mason 	return 0;
54802217ed2SChris Mason }
54902217ed2SChris Mason 
550081e9573SChris Mason /*
551081e9573SChris Mason  * compare two keys in a memcmp fashion
552081e9573SChris Mason  */
553081e9573SChris Mason static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
554081e9573SChris Mason {
555081e9573SChris Mason 	struct btrfs_key k1;
556081e9573SChris Mason 
557081e9573SChris Mason 	btrfs_disk_key_to_cpu(&k1, disk);
558081e9573SChris Mason 
55920736abaSDiego Calleja 	return btrfs_comp_cpu_keys(&k1, k2);
560081e9573SChris Mason }
561081e9573SChris Mason 
562f3465ca4SJosef Bacik /*
563f3465ca4SJosef Bacik  * same as comp_keys only with two btrfs_key's
564f3465ca4SJosef Bacik  */
5655d4f98a2SYan Zheng int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
566f3465ca4SJosef Bacik {
567f3465ca4SJosef Bacik 	if (k1->objectid > k2->objectid)
568f3465ca4SJosef Bacik 		return 1;
569f3465ca4SJosef Bacik 	if (k1->objectid < k2->objectid)
570f3465ca4SJosef Bacik 		return -1;
571f3465ca4SJosef Bacik 	if (k1->type > k2->type)
572f3465ca4SJosef Bacik 		return 1;
573f3465ca4SJosef Bacik 	if (k1->type < k2->type)
574f3465ca4SJosef Bacik 		return -1;
575f3465ca4SJosef Bacik 	if (k1->offset > k2->offset)
576f3465ca4SJosef Bacik 		return 1;
577f3465ca4SJosef Bacik 	if (k1->offset < k2->offset)
578f3465ca4SJosef Bacik 		return -1;
579f3465ca4SJosef Bacik 	return 0;
580f3465ca4SJosef Bacik }
581081e9573SChris Mason 
582d352ac68SChris Mason /*
583d352ac68SChris Mason  * this is used by the defrag code to go through all the
584d352ac68SChris Mason  * leaves pointed to by a node and reallocate them so that
585d352ac68SChris Mason  * disk order is close to key order
586d352ac68SChris Mason  */
5876702ed49SChris Mason int btrfs_realloc_node(struct btrfs_trans_handle *trans,
5885f39d397SChris Mason 		       struct btrfs_root *root, struct extent_buffer *parent,
589a6b6e75eSChris Mason 		       int start_slot, int cache_only, u64 *last_ret,
590a6b6e75eSChris Mason 		       struct btrfs_key *progress)
5916702ed49SChris Mason {
5926b80053dSChris Mason 	struct extent_buffer *cur;
5936702ed49SChris Mason 	u64 blocknr;
594ca7a79adSChris Mason 	u64 gen;
595e9d0b13bSChris Mason 	u64 search_start = *last_ret;
596e9d0b13bSChris Mason 	u64 last_block = 0;
5976702ed49SChris Mason 	u64 other;
5986702ed49SChris Mason 	u32 parent_nritems;
5996702ed49SChris Mason 	int end_slot;
6006702ed49SChris Mason 	int i;
6016702ed49SChris Mason 	int err = 0;
602f2183bdeSChris Mason 	int parent_level;
6036b80053dSChris Mason 	int uptodate;
6046b80053dSChris Mason 	u32 blocksize;
605081e9573SChris Mason 	int progress_passed = 0;
606081e9573SChris Mason 	struct btrfs_disk_key disk_key;
6076702ed49SChris Mason 
6085708b959SChris Mason 	parent_level = btrfs_header_level(parent);
6095708b959SChris Mason 	if (cache_only && parent_level != 1)
6105708b959SChris Mason 		return 0;
6115708b959SChris Mason 
612d397712bSChris Mason 	if (trans->transaction != root->fs_info->running_transaction)
6136702ed49SChris Mason 		WARN_ON(1);
614d397712bSChris Mason 	if (trans->transid != root->fs_info->generation)
6156702ed49SChris Mason 		WARN_ON(1);
61686479a04SChris Mason 
6176b80053dSChris Mason 	parent_nritems = btrfs_header_nritems(parent);
6186b80053dSChris Mason 	blocksize = btrfs_level_size(root, parent_level - 1);
6196702ed49SChris Mason 	end_slot = parent_nritems;
6206702ed49SChris Mason 
6216702ed49SChris Mason 	if (parent_nritems == 1)
6226702ed49SChris Mason 		return 0;
6236702ed49SChris Mason 
624b4ce94deSChris Mason 	btrfs_set_lock_blocking(parent);
625b4ce94deSChris Mason 
6266702ed49SChris Mason 	for (i = start_slot; i < end_slot; i++) {
6276702ed49SChris Mason 		int close = 1;
628a6b6e75eSChris Mason 
6295708b959SChris Mason 		if (!parent->map_token) {
6305708b959SChris Mason 			map_extent_buffer(parent,
6315708b959SChris Mason 					btrfs_node_key_ptr_offset(i),
6325708b959SChris Mason 					sizeof(struct btrfs_key_ptr),
6335708b959SChris Mason 					&parent->map_token, &parent->kaddr,
6345708b959SChris Mason 					&parent->map_start, &parent->map_len,
6355708b959SChris Mason 					KM_USER1);
6365708b959SChris Mason 		}
637081e9573SChris Mason 		btrfs_node_key(parent, &disk_key, i);
638081e9573SChris Mason 		if (!progress_passed && comp_keys(&disk_key, progress) < 0)
639081e9573SChris Mason 			continue;
640081e9573SChris Mason 
641081e9573SChris Mason 		progress_passed = 1;
6426b80053dSChris Mason 		blocknr = btrfs_node_blockptr(parent, i);
643ca7a79adSChris Mason 		gen = btrfs_node_ptr_generation(parent, i);
644e9d0b13bSChris Mason 		if (last_block == 0)
645e9d0b13bSChris Mason 			last_block = blocknr;
6465708b959SChris Mason 
6476702ed49SChris Mason 		if (i > 0) {
6486b80053dSChris Mason 			other = btrfs_node_blockptr(parent, i - 1);
6496b80053dSChris Mason 			close = close_blocks(blocknr, other, blocksize);
6506702ed49SChris Mason 		}
6510ef3e66bSChris Mason 		if (!close && i < end_slot - 2) {
6526b80053dSChris Mason 			other = btrfs_node_blockptr(parent, i + 1);
6536b80053dSChris Mason 			close = close_blocks(blocknr, other, blocksize);
6546702ed49SChris Mason 		}
655e9d0b13bSChris Mason 		if (close) {
656e9d0b13bSChris Mason 			last_block = blocknr;
6576702ed49SChris Mason 			continue;
658e9d0b13bSChris Mason 		}
6595708b959SChris Mason 		if (parent->map_token) {
6605708b959SChris Mason 			unmap_extent_buffer(parent, parent->map_token,
6615708b959SChris Mason 					    KM_USER1);
6625708b959SChris Mason 			parent->map_token = NULL;
6635708b959SChris Mason 		}
6646702ed49SChris Mason 
6656b80053dSChris Mason 		cur = btrfs_find_tree_block(root, blocknr, blocksize);
6666b80053dSChris Mason 		if (cur)
6671259ab75SChris Mason 			uptodate = btrfs_buffer_uptodate(cur, gen);
6686b80053dSChris Mason 		else
6696b80053dSChris Mason 			uptodate = 0;
6705708b959SChris Mason 		if (!cur || !uptodate) {
6716702ed49SChris Mason 			if (cache_only) {
6726b80053dSChris Mason 				free_extent_buffer(cur);
6736702ed49SChris Mason 				continue;
6746702ed49SChris Mason 			}
6756b80053dSChris Mason 			if (!cur) {
6766b80053dSChris Mason 				cur = read_tree_block(root, blocknr,
677ca7a79adSChris Mason 							 blocksize, gen);
67897d9a8a4STsutomu Itoh 				if (!cur)
67997d9a8a4STsutomu Itoh 					return -EIO;
6806b80053dSChris Mason 			} else if (!uptodate) {
681ca7a79adSChris Mason 				btrfs_read_buffer(cur, gen);
6826702ed49SChris Mason 			}
683f2183bdeSChris Mason 		}
684e9d0b13bSChris Mason 		if (search_start == 0)
6856b80053dSChris Mason 			search_start = last_block;
686e9d0b13bSChris Mason 
687e7a84565SChris Mason 		btrfs_tree_lock(cur);
688b4ce94deSChris Mason 		btrfs_set_lock_blocking(cur);
6896b80053dSChris Mason 		err = __btrfs_cow_block(trans, root, cur, parent, i,
690e7a84565SChris Mason 					&cur, search_start,
6916b80053dSChris Mason 					min(16 * blocksize,
6929fa8cfe7SChris Mason 					    (end_slot - i) * blocksize));
693252c38f0SYan 		if (err) {
694e7a84565SChris Mason 			btrfs_tree_unlock(cur);
6956b80053dSChris Mason 			free_extent_buffer(cur);
6966702ed49SChris Mason 			break;
697252c38f0SYan 		}
698e7a84565SChris Mason 		search_start = cur->start;
699e7a84565SChris Mason 		last_block = cur->start;
700f2183bdeSChris Mason 		*last_ret = search_start;
701e7a84565SChris Mason 		btrfs_tree_unlock(cur);
702e7a84565SChris Mason 		free_extent_buffer(cur);
7036702ed49SChris Mason 	}
7045708b959SChris Mason 	if (parent->map_token) {
7055708b959SChris Mason 		unmap_extent_buffer(parent, parent->map_token,
7065708b959SChris Mason 				    KM_USER1);
7075708b959SChris Mason 		parent->map_token = NULL;
7085708b959SChris Mason 	}
7096702ed49SChris Mason 	return err;
7106702ed49SChris Mason }
7116702ed49SChris Mason 
71274123bd7SChris Mason /*
71374123bd7SChris Mason  * The leaf data grows from end-to-front in the node.
71474123bd7SChris Mason  * this returns the address of the start of the last item,
71574123bd7SChris Mason  * which is the stop of the leaf data stack
71674123bd7SChris Mason  */
717123abc88SChris Mason static inline unsigned int leaf_data_end(struct btrfs_root *root,
7185f39d397SChris Mason 					 struct extent_buffer *leaf)
719be0e5c09SChris Mason {
7205f39d397SChris Mason 	u32 nr = btrfs_header_nritems(leaf);
721be0e5c09SChris Mason 	if (nr == 0)
722123abc88SChris Mason 		return BTRFS_LEAF_DATA_SIZE(root);
7235f39d397SChris Mason 	return btrfs_item_offset_nr(leaf, nr - 1);
724be0e5c09SChris Mason }
725be0e5c09SChris Mason 
726aa5d6bedSChris Mason 
72774123bd7SChris Mason /*
7285f39d397SChris Mason  * search for key in the extent_buffer.  The items start at offset p,
7295f39d397SChris Mason  * and they are item_size apart.  There are 'max' items in p.
7305f39d397SChris Mason  *
73174123bd7SChris Mason  * the slot in the array is returned via slot, and it points to
73274123bd7SChris Mason  * the place where you would insert key if it is not found in
73374123bd7SChris Mason  * the array.
73474123bd7SChris Mason  *
73574123bd7SChris Mason  * slot may point to max if the key is bigger than all of the keys
73674123bd7SChris Mason  */
737e02119d5SChris Mason static noinline int generic_bin_search(struct extent_buffer *eb,
738e02119d5SChris Mason 				       unsigned long p,
7395f39d397SChris Mason 				       int item_size, struct btrfs_key *key,
740be0e5c09SChris Mason 				       int max, int *slot)
741be0e5c09SChris Mason {
742be0e5c09SChris Mason 	int low = 0;
743be0e5c09SChris Mason 	int high = max;
744be0e5c09SChris Mason 	int mid;
745be0e5c09SChris Mason 	int ret;
746479965d6SChris Mason 	struct btrfs_disk_key *tmp = NULL;
7475f39d397SChris Mason 	struct btrfs_disk_key unaligned;
7485f39d397SChris Mason 	unsigned long offset;
7495f39d397SChris Mason 	char *map_token = NULL;
7505f39d397SChris Mason 	char *kaddr = NULL;
7515f39d397SChris Mason 	unsigned long map_start = 0;
7525f39d397SChris Mason 	unsigned long map_len = 0;
753479965d6SChris Mason 	int err;
754be0e5c09SChris Mason 
755be0e5c09SChris Mason 	while (low < high) {
756be0e5c09SChris Mason 		mid = (low + high) / 2;
7575f39d397SChris Mason 		offset = p + mid * item_size;
7585f39d397SChris Mason 
7595f39d397SChris Mason 		if (!map_token || offset < map_start ||
7605f39d397SChris Mason 		    (offset + sizeof(struct btrfs_disk_key)) >
7615f39d397SChris Mason 		    map_start + map_len) {
762479965d6SChris Mason 			if (map_token) {
7635f39d397SChris Mason 				unmap_extent_buffer(eb, map_token, KM_USER0);
764479965d6SChris Mason 				map_token = NULL;
765479965d6SChris Mason 			}
766934d375bSChris Mason 
767934d375bSChris Mason 			err = map_private_extent_buffer(eb, offset,
768479965d6SChris Mason 						sizeof(struct btrfs_disk_key),
769479965d6SChris Mason 						&map_token, &kaddr,
7705f39d397SChris Mason 						&map_start, &map_len, KM_USER0);
7715f39d397SChris Mason 
772479965d6SChris Mason 			if (!err) {
773479965d6SChris Mason 				tmp = (struct btrfs_disk_key *)(kaddr + offset -
774479965d6SChris Mason 							map_start);
775479965d6SChris Mason 			} else {
7765f39d397SChris Mason 				read_extent_buffer(eb, &unaligned,
7775f39d397SChris Mason 						   offset, sizeof(unaligned));
7785f39d397SChris Mason 				tmp = &unaligned;
779479965d6SChris Mason 			}
780479965d6SChris Mason 
7815f39d397SChris Mason 		} else {
7825f39d397SChris Mason 			tmp = (struct btrfs_disk_key *)(kaddr + offset -
7835f39d397SChris Mason 							map_start);
7845f39d397SChris Mason 		}
785be0e5c09SChris Mason 		ret = comp_keys(tmp, key);
786be0e5c09SChris Mason 
787be0e5c09SChris Mason 		if (ret < 0)
788be0e5c09SChris Mason 			low = mid + 1;
789be0e5c09SChris Mason 		else if (ret > 0)
790be0e5c09SChris Mason 			high = mid;
791be0e5c09SChris Mason 		else {
792be0e5c09SChris Mason 			*slot = mid;
793479965d6SChris Mason 			if (map_token)
7945f39d397SChris Mason 				unmap_extent_buffer(eb, map_token, KM_USER0);
795be0e5c09SChris Mason 			return 0;
796be0e5c09SChris Mason 		}
797be0e5c09SChris Mason 	}
798be0e5c09SChris Mason 	*slot = low;
7995f39d397SChris Mason 	if (map_token)
8005f39d397SChris Mason 		unmap_extent_buffer(eb, map_token, KM_USER0);
801be0e5c09SChris Mason 	return 1;
802be0e5c09SChris Mason }
803be0e5c09SChris Mason 
80497571fd0SChris Mason /*
80597571fd0SChris Mason  * simple bin_search frontend that does the right thing for
80697571fd0SChris Mason  * leaves vs nodes
80797571fd0SChris Mason  */
8085f39d397SChris Mason static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
8095f39d397SChris Mason 		      int level, int *slot)
810be0e5c09SChris Mason {
8115f39d397SChris Mason 	if (level == 0) {
8125f39d397SChris Mason 		return generic_bin_search(eb,
8135f39d397SChris Mason 					  offsetof(struct btrfs_leaf, items),
8140783fcfcSChris Mason 					  sizeof(struct btrfs_item),
8155f39d397SChris Mason 					  key, btrfs_header_nritems(eb),
8167518a238SChris Mason 					  slot);
817be0e5c09SChris Mason 	} else {
8185f39d397SChris Mason 		return generic_bin_search(eb,
8195f39d397SChris Mason 					  offsetof(struct btrfs_node, ptrs),
820123abc88SChris Mason 					  sizeof(struct btrfs_key_ptr),
8215f39d397SChris Mason 					  key, btrfs_header_nritems(eb),
8227518a238SChris Mason 					  slot);
823be0e5c09SChris Mason 	}
824be0e5c09SChris Mason 	return -1;
825be0e5c09SChris Mason }
826be0e5c09SChris Mason 
8275d4f98a2SYan Zheng int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
8285d4f98a2SYan Zheng 		     int level, int *slot)
8295d4f98a2SYan Zheng {
8305d4f98a2SYan Zheng 	return bin_search(eb, key, level, slot);
8315d4f98a2SYan Zheng }
8325d4f98a2SYan Zheng 
833f0486c68SYan, Zheng static void root_add_used(struct btrfs_root *root, u32 size)
834f0486c68SYan, Zheng {
835f0486c68SYan, Zheng 	spin_lock(&root->accounting_lock);
836f0486c68SYan, Zheng 	btrfs_set_root_used(&root->root_item,
837f0486c68SYan, Zheng 			    btrfs_root_used(&root->root_item) + size);
838f0486c68SYan, Zheng 	spin_unlock(&root->accounting_lock);
839f0486c68SYan, Zheng }
840f0486c68SYan, Zheng 
841f0486c68SYan, Zheng static void root_sub_used(struct btrfs_root *root, u32 size)
842f0486c68SYan, Zheng {
843f0486c68SYan, Zheng 	spin_lock(&root->accounting_lock);
844f0486c68SYan, Zheng 	btrfs_set_root_used(&root->root_item,
845f0486c68SYan, Zheng 			    btrfs_root_used(&root->root_item) - size);
846f0486c68SYan, Zheng 	spin_unlock(&root->accounting_lock);
847f0486c68SYan, Zheng }
848f0486c68SYan, Zheng 
849d352ac68SChris Mason /* given a node and slot number, this reads the blocks it points to.  The
850d352ac68SChris Mason  * extent buffer is returned with a reference taken (but unlocked).
851d352ac68SChris Mason  * NULL is returned on error.
852d352ac68SChris Mason  */
853e02119d5SChris Mason static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
8545f39d397SChris Mason 				   struct extent_buffer *parent, int slot)
855bb803951SChris Mason {
856ca7a79adSChris Mason 	int level = btrfs_header_level(parent);
857bb803951SChris Mason 	if (slot < 0)
858bb803951SChris Mason 		return NULL;
8595f39d397SChris Mason 	if (slot >= btrfs_header_nritems(parent))
860bb803951SChris Mason 		return NULL;
861ca7a79adSChris Mason 
862ca7a79adSChris Mason 	BUG_ON(level == 0);
863ca7a79adSChris Mason 
864db94535dSChris Mason 	return read_tree_block(root, btrfs_node_blockptr(parent, slot),
865ca7a79adSChris Mason 		       btrfs_level_size(root, level - 1),
866ca7a79adSChris Mason 		       btrfs_node_ptr_generation(parent, slot));
867bb803951SChris Mason }
868bb803951SChris Mason 
869d352ac68SChris Mason /*
870d352ac68SChris Mason  * node level balancing, used to make sure nodes are in proper order for
871d352ac68SChris Mason  * item deletion.  We balance from the top down, so we have to make sure
872d352ac68SChris Mason  * that a deletion won't leave an node completely empty later on.
873d352ac68SChris Mason  */
874e02119d5SChris Mason static noinline int balance_level(struct btrfs_trans_handle *trans,
87598ed5174SChris Mason 			 struct btrfs_root *root,
87698ed5174SChris Mason 			 struct btrfs_path *path, int level)
877bb803951SChris Mason {
8785f39d397SChris Mason 	struct extent_buffer *right = NULL;
8795f39d397SChris Mason 	struct extent_buffer *mid;
8805f39d397SChris Mason 	struct extent_buffer *left = NULL;
8815f39d397SChris Mason 	struct extent_buffer *parent = NULL;
882bb803951SChris Mason 	int ret = 0;
883bb803951SChris Mason 	int wret;
884bb803951SChris Mason 	int pslot;
885bb803951SChris Mason 	int orig_slot = path->slots[level];
88679f95c82SChris Mason 	u64 orig_ptr;
887bb803951SChris Mason 
888bb803951SChris Mason 	if (level == 0)
889bb803951SChris Mason 		return 0;
890bb803951SChris Mason 
8915f39d397SChris Mason 	mid = path->nodes[level];
892b4ce94deSChris Mason 
893925baeddSChris Mason 	WARN_ON(!path->locks[level]);
8947bb86316SChris Mason 	WARN_ON(btrfs_header_generation(mid) != trans->transid);
8957bb86316SChris Mason 
8961d4f8a0cSChris Mason 	orig_ptr = btrfs_node_blockptr(mid, orig_slot);
89779f95c82SChris Mason 
898234b63a0SChris Mason 	if (level < BTRFS_MAX_LEVEL - 1)
8995f39d397SChris Mason 		parent = path->nodes[level + 1];
900bb803951SChris Mason 	pslot = path->slots[level + 1];
901bb803951SChris Mason 
90240689478SChris Mason 	/*
90340689478SChris Mason 	 * deal with the case where there is only one pointer in the root
90440689478SChris Mason 	 * by promoting the node below to a root
90540689478SChris Mason 	 */
9065f39d397SChris Mason 	if (!parent) {
9075f39d397SChris Mason 		struct extent_buffer *child;
908bb803951SChris Mason 
9095f39d397SChris Mason 		if (btrfs_header_nritems(mid) != 1)
910bb803951SChris Mason 			return 0;
911bb803951SChris Mason 
912bb803951SChris Mason 		/* promote the child to a root */
9135f39d397SChris Mason 		child = read_node_slot(root, mid, 0);
9147951f3ceSJeff Mahoney 		BUG_ON(!child);
915925baeddSChris Mason 		btrfs_tree_lock(child);
916b4ce94deSChris Mason 		btrfs_set_lock_blocking(child);
9179fa8cfe7SChris Mason 		ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
918f0486c68SYan, Zheng 		if (ret) {
919f0486c68SYan, Zheng 			btrfs_tree_unlock(child);
920f0486c68SYan, Zheng 			free_extent_buffer(child);
921f0486c68SYan, Zheng 			goto enospc;
922f0486c68SYan, Zheng 		}
9232f375ab9SYan 
924240f62c8SChris Mason 		rcu_assign_pointer(root->node, child);
925925baeddSChris Mason 
9260b86a832SChris Mason 		add_root_to_dirty_list(root);
927925baeddSChris Mason 		btrfs_tree_unlock(child);
928b4ce94deSChris Mason 
929925baeddSChris Mason 		path->locks[level] = 0;
930bb803951SChris Mason 		path->nodes[level] = NULL;
9315f39d397SChris Mason 		clean_tree_block(trans, root, mid);
932925baeddSChris Mason 		btrfs_tree_unlock(mid);
933bb803951SChris Mason 		/* once for the path */
9345f39d397SChris Mason 		free_extent_buffer(mid);
935f0486c68SYan, Zheng 
936f0486c68SYan, Zheng 		root_sub_used(root, mid->len);
937f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, mid, 0, 1);
938bb803951SChris Mason 		/* once for the root ptr */
9395f39d397SChris Mason 		free_extent_buffer(mid);
940f0486c68SYan, Zheng 		return 0;
941bb803951SChris Mason 	}
9425f39d397SChris Mason 	if (btrfs_header_nritems(mid) >
943123abc88SChris Mason 	    BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
944bb803951SChris Mason 		return 0;
945bb803951SChris Mason 
946559af821SAndi Kleen 	btrfs_header_nritems(mid);
94754aa1f4dSChris Mason 
9485f39d397SChris Mason 	left = read_node_slot(root, parent, pslot - 1);
9495f39d397SChris Mason 	if (left) {
950925baeddSChris Mason 		btrfs_tree_lock(left);
951b4ce94deSChris Mason 		btrfs_set_lock_blocking(left);
9525f39d397SChris Mason 		wret = btrfs_cow_block(trans, root, left,
9539fa8cfe7SChris Mason 				       parent, pslot - 1, &left);
95454aa1f4dSChris Mason 		if (wret) {
95554aa1f4dSChris Mason 			ret = wret;
95654aa1f4dSChris Mason 			goto enospc;
95754aa1f4dSChris Mason 		}
9582cc58cf2SChris Mason 	}
9595f39d397SChris Mason 	right = read_node_slot(root, parent, pslot + 1);
9605f39d397SChris Mason 	if (right) {
961925baeddSChris Mason 		btrfs_tree_lock(right);
962b4ce94deSChris Mason 		btrfs_set_lock_blocking(right);
9635f39d397SChris Mason 		wret = btrfs_cow_block(trans, root, right,
9649fa8cfe7SChris Mason 				       parent, pslot + 1, &right);
9652cc58cf2SChris Mason 		if (wret) {
9662cc58cf2SChris Mason 			ret = wret;
9672cc58cf2SChris Mason 			goto enospc;
9682cc58cf2SChris Mason 		}
9692cc58cf2SChris Mason 	}
9702cc58cf2SChris Mason 
9712cc58cf2SChris Mason 	/* first, try to make some room in the middle buffer */
9725f39d397SChris Mason 	if (left) {
9735f39d397SChris Mason 		orig_slot += btrfs_header_nritems(left);
974bce4eae9SChris Mason 		wret = push_node_left(trans, root, left, mid, 1);
97579f95c82SChris Mason 		if (wret < 0)
97679f95c82SChris Mason 			ret = wret;
977559af821SAndi Kleen 		btrfs_header_nritems(mid);
978bb803951SChris Mason 	}
97979f95c82SChris Mason 
98079f95c82SChris Mason 	/*
98179f95c82SChris Mason 	 * then try to empty the right most buffer into the middle
98279f95c82SChris Mason 	 */
9835f39d397SChris Mason 	if (right) {
984971a1f66SChris Mason 		wret = push_node_left(trans, root, mid, right, 1);
98554aa1f4dSChris Mason 		if (wret < 0 && wret != -ENOSPC)
98679f95c82SChris Mason 			ret = wret;
9875f39d397SChris Mason 		if (btrfs_header_nritems(right) == 0) {
9885f39d397SChris Mason 			clean_tree_block(trans, root, right);
989925baeddSChris Mason 			btrfs_tree_unlock(right);
990e089f05cSChris Mason 			wret = del_ptr(trans, root, path, level + 1, pslot +
991e089f05cSChris Mason 				       1);
992bb803951SChris Mason 			if (wret)
993bb803951SChris Mason 				ret = wret;
994f0486c68SYan, Zheng 			root_sub_used(root, right->len);
995f0486c68SYan, Zheng 			btrfs_free_tree_block(trans, root, right, 0, 1);
996f0486c68SYan, Zheng 			free_extent_buffer(right);
997f0486c68SYan, Zheng 			right = NULL;
998bb803951SChris Mason 		} else {
9995f39d397SChris Mason 			struct btrfs_disk_key right_key;
10005f39d397SChris Mason 			btrfs_node_key(right, &right_key, 0);
10015f39d397SChris Mason 			btrfs_set_node_key(parent, &right_key, pslot + 1);
10025f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
1003bb803951SChris Mason 		}
1004bb803951SChris Mason 	}
10055f39d397SChris Mason 	if (btrfs_header_nritems(mid) == 1) {
100679f95c82SChris Mason 		/*
100779f95c82SChris Mason 		 * we're not allowed to leave a node with one item in the
100879f95c82SChris Mason 		 * tree during a delete.  A deletion from lower in the tree
100979f95c82SChris Mason 		 * could try to delete the only pointer in this node.
101079f95c82SChris Mason 		 * So, pull some keys from the left.
101179f95c82SChris Mason 		 * There has to be a left pointer at this point because
101279f95c82SChris Mason 		 * otherwise we would have pulled some pointers from the
101379f95c82SChris Mason 		 * right
101479f95c82SChris Mason 		 */
10155f39d397SChris Mason 		BUG_ON(!left);
10165f39d397SChris Mason 		wret = balance_node_right(trans, root, mid, left);
101754aa1f4dSChris Mason 		if (wret < 0) {
101879f95c82SChris Mason 			ret = wret;
101954aa1f4dSChris Mason 			goto enospc;
102054aa1f4dSChris Mason 		}
1021bce4eae9SChris Mason 		if (wret == 1) {
1022bce4eae9SChris Mason 			wret = push_node_left(trans, root, left, mid, 1);
1023bce4eae9SChris Mason 			if (wret < 0)
1024bce4eae9SChris Mason 				ret = wret;
1025bce4eae9SChris Mason 		}
102679f95c82SChris Mason 		BUG_ON(wret == 1);
102779f95c82SChris Mason 	}
10285f39d397SChris Mason 	if (btrfs_header_nritems(mid) == 0) {
10295f39d397SChris Mason 		clean_tree_block(trans, root, mid);
1030925baeddSChris Mason 		btrfs_tree_unlock(mid);
1031e089f05cSChris Mason 		wret = del_ptr(trans, root, path, level + 1, pslot);
1032bb803951SChris Mason 		if (wret)
1033bb803951SChris Mason 			ret = wret;
1034f0486c68SYan, Zheng 		root_sub_used(root, mid->len);
1035f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, mid, 0, 1);
1036f0486c68SYan, Zheng 		free_extent_buffer(mid);
1037f0486c68SYan, Zheng 		mid = NULL;
103879f95c82SChris Mason 	} else {
103979f95c82SChris Mason 		/* update the parent key to reflect our changes */
10405f39d397SChris Mason 		struct btrfs_disk_key mid_key;
10415f39d397SChris Mason 		btrfs_node_key(mid, &mid_key, 0);
10425f39d397SChris Mason 		btrfs_set_node_key(parent, &mid_key, pslot);
10435f39d397SChris Mason 		btrfs_mark_buffer_dirty(parent);
104479f95c82SChris Mason 	}
1045bb803951SChris Mason 
104679f95c82SChris Mason 	/* update the path */
10475f39d397SChris Mason 	if (left) {
10485f39d397SChris Mason 		if (btrfs_header_nritems(left) > orig_slot) {
10495f39d397SChris Mason 			extent_buffer_get(left);
1050925baeddSChris Mason 			/* left was locked after cow */
10515f39d397SChris Mason 			path->nodes[level] = left;
1052bb803951SChris Mason 			path->slots[level + 1] -= 1;
1053bb803951SChris Mason 			path->slots[level] = orig_slot;
1054925baeddSChris Mason 			if (mid) {
1055925baeddSChris Mason 				btrfs_tree_unlock(mid);
10565f39d397SChris Mason 				free_extent_buffer(mid);
1057925baeddSChris Mason 			}
1058bb803951SChris Mason 		} else {
10595f39d397SChris Mason 			orig_slot -= btrfs_header_nritems(left);
1060bb803951SChris Mason 			path->slots[level] = orig_slot;
1061bb803951SChris Mason 		}
1062bb803951SChris Mason 	}
106379f95c82SChris Mason 	/* double check we haven't messed things up */
1064e20d96d6SChris Mason 	if (orig_ptr !=
10655f39d397SChris Mason 	    btrfs_node_blockptr(path->nodes[level], path->slots[level]))
106679f95c82SChris Mason 		BUG();
106754aa1f4dSChris Mason enospc:
1068925baeddSChris Mason 	if (right) {
1069925baeddSChris Mason 		btrfs_tree_unlock(right);
10705f39d397SChris Mason 		free_extent_buffer(right);
1071925baeddSChris Mason 	}
1072925baeddSChris Mason 	if (left) {
1073925baeddSChris Mason 		if (path->nodes[level] != left)
1074925baeddSChris Mason 			btrfs_tree_unlock(left);
10755f39d397SChris Mason 		free_extent_buffer(left);
1076925baeddSChris Mason 	}
1077bb803951SChris Mason 	return ret;
1078bb803951SChris Mason }
1079bb803951SChris Mason 
1080d352ac68SChris Mason /* Node balancing for insertion.  Here we only split or push nodes around
1081d352ac68SChris Mason  * when they are completely full.  This is also done top down, so we
1082d352ac68SChris Mason  * have to be pessimistic.
1083d352ac68SChris Mason  */
1084d397712bSChris Mason static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1085e66f709bSChris Mason 					  struct btrfs_root *root,
1086e66f709bSChris Mason 					  struct btrfs_path *path, int level)
1087e66f709bSChris Mason {
10885f39d397SChris Mason 	struct extent_buffer *right = NULL;
10895f39d397SChris Mason 	struct extent_buffer *mid;
10905f39d397SChris Mason 	struct extent_buffer *left = NULL;
10915f39d397SChris Mason 	struct extent_buffer *parent = NULL;
1092e66f709bSChris Mason 	int ret = 0;
1093e66f709bSChris Mason 	int wret;
1094e66f709bSChris Mason 	int pslot;
1095e66f709bSChris Mason 	int orig_slot = path->slots[level];
1096e66f709bSChris Mason 
1097e66f709bSChris Mason 	if (level == 0)
1098e66f709bSChris Mason 		return 1;
1099e66f709bSChris Mason 
11005f39d397SChris Mason 	mid = path->nodes[level];
11017bb86316SChris Mason 	WARN_ON(btrfs_header_generation(mid) != trans->transid);
1102e66f709bSChris Mason 
1103e66f709bSChris Mason 	if (level < BTRFS_MAX_LEVEL - 1)
11045f39d397SChris Mason 		parent = path->nodes[level + 1];
1105e66f709bSChris Mason 	pslot = path->slots[level + 1];
1106e66f709bSChris Mason 
11075f39d397SChris Mason 	if (!parent)
1108e66f709bSChris Mason 		return 1;
1109e66f709bSChris Mason 
11105f39d397SChris Mason 	left = read_node_slot(root, parent, pslot - 1);
1111e66f709bSChris Mason 
1112e66f709bSChris Mason 	/* first, try to make some room in the middle buffer */
11135f39d397SChris Mason 	if (left) {
1114e66f709bSChris Mason 		u32 left_nr;
1115925baeddSChris Mason 
1116925baeddSChris Mason 		btrfs_tree_lock(left);
1117b4ce94deSChris Mason 		btrfs_set_lock_blocking(left);
1118b4ce94deSChris Mason 
11195f39d397SChris Mason 		left_nr = btrfs_header_nritems(left);
112033ade1f8SChris Mason 		if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
112133ade1f8SChris Mason 			wret = 1;
112233ade1f8SChris Mason 		} else {
11235f39d397SChris Mason 			ret = btrfs_cow_block(trans, root, left, parent,
11249fa8cfe7SChris Mason 					      pslot - 1, &left);
112554aa1f4dSChris Mason 			if (ret)
112654aa1f4dSChris Mason 				wret = 1;
112754aa1f4dSChris Mason 			else {
112854aa1f4dSChris Mason 				wret = push_node_left(trans, root,
1129971a1f66SChris Mason 						      left, mid, 0);
113054aa1f4dSChris Mason 			}
113133ade1f8SChris Mason 		}
1132e66f709bSChris Mason 		if (wret < 0)
1133e66f709bSChris Mason 			ret = wret;
1134e66f709bSChris Mason 		if (wret == 0) {
11355f39d397SChris Mason 			struct btrfs_disk_key disk_key;
1136e66f709bSChris Mason 			orig_slot += left_nr;
11375f39d397SChris Mason 			btrfs_node_key(mid, &disk_key, 0);
11385f39d397SChris Mason 			btrfs_set_node_key(parent, &disk_key, pslot);
11395f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
11405f39d397SChris Mason 			if (btrfs_header_nritems(left) > orig_slot) {
11415f39d397SChris Mason 				path->nodes[level] = left;
1142e66f709bSChris Mason 				path->slots[level + 1] -= 1;
1143e66f709bSChris Mason 				path->slots[level] = orig_slot;
1144925baeddSChris Mason 				btrfs_tree_unlock(mid);
11455f39d397SChris Mason 				free_extent_buffer(mid);
1146e66f709bSChris Mason 			} else {
1147e66f709bSChris Mason 				orig_slot -=
11485f39d397SChris Mason 					btrfs_header_nritems(left);
1149e66f709bSChris Mason 				path->slots[level] = orig_slot;
1150925baeddSChris Mason 				btrfs_tree_unlock(left);
11515f39d397SChris Mason 				free_extent_buffer(left);
1152e66f709bSChris Mason 			}
1153e66f709bSChris Mason 			return 0;
1154e66f709bSChris Mason 		}
1155925baeddSChris Mason 		btrfs_tree_unlock(left);
11565f39d397SChris Mason 		free_extent_buffer(left);
1157e66f709bSChris Mason 	}
11585f39d397SChris Mason 	right = read_node_slot(root, parent, pslot + 1);
1159e66f709bSChris Mason 
1160e66f709bSChris Mason 	/*
1161e66f709bSChris Mason 	 * then try to empty the right most buffer into the middle
1162e66f709bSChris Mason 	 */
11635f39d397SChris Mason 	if (right) {
116433ade1f8SChris Mason 		u32 right_nr;
1165b4ce94deSChris Mason 
1166925baeddSChris Mason 		btrfs_tree_lock(right);
1167b4ce94deSChris Mason 		btrfs_set_lock_blocking(right);
1168b4ce94deSChris Mason 
11695f39d397SChris Mason 		right_nr = btrfs_header_nritems(right);
117033ade1f8SChris Mason 		if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
117133ade1f8SChris Mason 			wret = 1;
117233ade1f8SChris Mason 		} else {
11735f39d397SChris Mason 			ret = btrfs_cow_block(trans, root, right,
11745f39d397SChris Mason 					      parent, pslot + 1,
11759fa8cfe7SChris Mason 					      &right);
117654aa1f4dSChris Mason 			if (ret)
117754aa1f4dSChris Mason 				wret = 1;
117854aa1f4dSChris Mason 			else {
117933ade1f8SChris Mason 				wret = balance_node_right(trans, root,
11805f39d397SChris Mason 							  right, mid);
118133ade1f8SChris Mason 			}
118254aa1f4dSChris Mason 		}
1183e66f709bSChris Mason 		if (wret < 0)
1184e66f709bSChris Mason 			ret = wret;
1185e66f709bSChris Mason 		if (wret == 0) {
11865f39d397SChris Mason 			struct btrfs_disk_key disk_key;
11875f39d397SChris Mason 
11885f39d397SChris Mason 			btrfs_node_key(right, &disk_key, 0);
11895f39d397SChris Mason 			btrfs_set_node_key(parent, &disk_key, pslot + 1);
11905f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
11915f39d397SChris Mason 
11925f39d397SChris Mason 			if (btrfs_header_nritems(mid) <= orig_slot) {
11935f39d397SChris Mason 				path->nodes[level] = right;
1194e66f709bSChris Mason 				path->slots[level + 1] += 1;
1195e66f709bSChris Mason 				path->slots[level] = orig_slot -
11965f39d397SChris Mason 					btrfs_header_nritems(mid);
1197925baeddSChris Mason 				btrfs_tree_unlock(mid);
11985f39d397SChris Mason 				free_extent_buffer(mid);
1199e66f709bSChris Mason 			} else {
1200925baeddSChris Mason 				btrfs_tree_unlock(right);
12015f39d397SChris Mason 				free_extent_buffer(right);
1202e66f709bSChris Mason 			}
1203e66f709bSChris Mason 			return 0;
1204e66f709bSChris Mason 		}
1205925baeddSChris Mason 		btrfs_tree_unlock(right);
12065f39d397SChris Mason 		free_extent_buffer(right);
1207e66f709bSChris Mason 	}
1208e66f709bSChris Mason 	return 1;
1209e66f709bSChris Mason }
1210e66f709bSChris Mason 
121174123bd7SChris Mason /*
1212d352ac68SChris Mason  * readahead one full node of leaves, finding things that are close
1213d352ac68SChris Mason  * to the block in 'slot', and triggering ra on them.
12143c69faecSChris Mason  */
1215c8c42864SChris Mason static void reada_for_search(struct btrfs_root *root,
1216e02119d5SChris Mason 			     struct btrfs_path *path,
121701f46658SChris Mason 			     int level, int slot, u64 objectid)
12183c69faecSChris Mason {
12195f39d397SChris Mason 	struct extent_buffer *node;
122001f46658SChris Mason 	struct btrfs_disk_key disk_key;
12213c69faecSChris Mason 	u32 nritems;
12223c69faecSChris Mason 	u64 search;
1223a7175319SChris Mason 	u64 target;
12246b80053dSChris Mason 	u64 nread = 0;
1225cb25c2eaSJosef Bacik 	u64 gen;
12263c69faecSChris Mason 	int direction = path->reada;
12275f39d397SChris Mason 	struct extent_buffer *eb;
12286b80053dSChris Mason 	u32 nr;
12296b80053dSChris Mason 	u32 blocksize;
12306b80053dSChris Mason 	u32 nscan = 0;
1231*25b8b936SJosef Bacik 	bool map = true;
1232db94535dSChris Mason 
1233a6b6e75eSChris Mason 	if (level != 1)
12343c69faecSChris Mason 		return;
12353c69faecSChris Mason 
12366702ed49SChris Mason 	if (!path->nodes[level])
12376702ed49SChris Mason 		return;
12386702ed49SChris Mason 
12395f39d397SChris Mason 	node = path->nodes[level];
1240925baeddSChris Mason 
12413c69faecSChris Mason 	search = btrfs_node_blockptr(node, slot);
12426b80053dSChris Mason 	blocksize = btrfs_level_size(root, level - 1);
12436b80053dSChris Mason 	eb = btrfs_find_tree_block(root, search, blocksize);
12445f39d397SChris Mason 	if (eb) {
12455f39d397SChris Mason 		free_extent_buffer(eb);
12463c69faecSChris Mason 		return;
12473c69faecSChris Mason 	}
12483c69faecSChris Mason 
1249a7175319SChris Mason 	target = search;
12506b80053dSChris Mason 
12515f39d397SChris Mason 	nritems = btrfs_header_nritems(node);
12526b80053dSChris Mason 	nr = slot;
1253*25b8b936SJosef Bacik 	if (node->map_token || path->skip_locking)
1254*25b8b936SJosef Bacik 		map = false;
1255*25b8b936SJosef Bacik 
12563c69faecSChris Mason 	while (1) {
1257*25b8b936SJosef Bacik 		if (map && !node->map_token) {
1258cb25c2eaSJosef Bacik 			unsigned long offset = btrfs_node_key_ptr_offset(nr);
1259cb25c2eaSJosef Bacik 			map_private_extent_buffer(node, offset,
1260cb25c2eaSJosef Bacik 						  sizeof(struct btrfs_key_ptr),
1261cb25c2eaSJosef Bacik 						  &node->map_token,
1262cb25c2eaSJosef Bacik 						  &node->kaddr,
1263cb25c2eaSJosef Bacik 						  &node->map_start,
1264cb25c2eaSJosef Bacik 						  &node->map_len, KM_USER1);
1265cb25c2eaSJosef Bacik 		}
12666b80053dSChris Mason 		if (direction < 0) {
12676b80053dSChris Mason 			if (nr == 0)
12683c69faecSChris Mason 				break;
12696b80053dSChris Mason 			nr--;
12706b80053dSChris Mason 		} else if (direction > 0) {
12716b80053dSChris Mason 			nr++;
12726b80053dSChris Mason 			if (nr >= nritems)
12736b80053dSChris Mason 				break;
12743c69faecSChris Mason 		}
127501f46658SChris Mason 		if (path->reada < 0 && objectid) {
127601f46658SChris Mason 			btrfs_node_key(node, &disk_key, nr);
127701f46658SChris Mason 			if (btrfs_disk_key_objectid(&disk_key) != objectid)
127801f46658SChris Mason 				break;
127901f46658SChris Mason 		}
12806b80053dSChris Mason 		search = btrfs_node_blockptr(node, nr);
1281a7175319SChris Mason 		if ((search <= target && target - search <= 65536) ||
1282a7175319SChris Mason 		    (search > target && search - target <= 65536)) {
1283cb25c2eaSJosef Bacik 			gen = btrfs_node_ptr_generation(node, nr);
1284*25b8b936SJosef Bacik 			if (map && node->map_token) {
1285cb25c2eaSJosef Bacik 				unmap_extent_buffer(node, node->map_token,
1286cb25c2eaSJosef Bacik 						    KM_USER1);
1287cb25c2eaSJosef Bacik 				node->map_token = NULL;
1288cb25c2eaSJosef Bacik 			}
1289cb25c2eaSJosef Bacik 			readahead_tree_block(root, search, blocksize, gen);
12906b80053dSChris Mason 			nread += blocksize;
12913c69faecSChris Mason 		}
12926b80053dSChris Mason 		nscan++;
1293a7175319SChris Mason 		if ((nread > 65536 || nscan > 32))
12946b80053dSChris Mason 			break;
12953c69faecSChris Mason 	}
1296*25b8b936SJosef Bacik 	if (map && node->map_token) {
1297cb25c2eaSJosef Bacik 		unmap_extent_buffer(node, node->map_token, KM_USER1);
1298cb25c2eaSJosef Bacik 		node->map_token = NULL;
1299cb25c2eaSJosef Bacik 	}
13003c69faecSChris Mason }
1301925baeddSChris Mason 
1302d352ac68SChris Mason /*
1303b4ce94deSChris Mason  * returns -EAGAIN if it had to drop the path, or zero if everything was in
1304b4ce94deSChris Mason  * cache
1305b4ce94deSChris Mason  */
1306b4ce94deSChris Mason static noinline int reada_for_balance(struct btrfs_root *root,
1307b4ce94deSChris Mason 				      struct btrfs_path *path, int level)
1308b4ce94deSChris Mason {
1309b4ce94deSChris Mason 	int slot;
1310b4ce94deSChris Mason 	int nritems;
1311b4ce94deSChris Mason 	struct extent_buffer *parent;
1312b4ce94deSChris Mason 	struct extent_buffer *eb;
1313b4ce94deSChris Mason 	u64 gen;
1314b4ce94deSChris Mason 	u64 block1 = 0;
1315b4ce94deSChris Mason 	u64 block2 = 0;
1316b4ce94deSChris Mason 	int ret = 0;
1317b4ce94deSChris Mason 	int blocksize;
1318b4ce94deSChris Mason 
13198c594ea8SChris Mason 	parent = path->nodes[level + 1];
1320b4ce94deSChris Mason 	if (!parent)
1321b4ce94deSChris Mason 		return 0;
1322b4ce94deSChris Mason 
1323b4ce94deSChris Mason 	nritems = btrfs_header_nritems(parent);
13248c594ea8SChris Mason 	slot = path->slots[level + 1];
1325b4ce94deSChris Mason 	blocksize = btrfs_level_size(root, level);
1326b4ce94deSChris Mason 
1327b4ce94deSChris Mason 	if (slot > 0) {
1328b4ce94deSChris Mason 		block1 = btrfs_node_blockptr(parent, slot - 1);
1329b4ce94deSChris Mason 		gen = btrfs_node_ptr_generation(parent, slot - 1);
1330b4ce94deSChris Mason 		eb = btrfs_find_tree_block(root, block1, blocksize);
1331b4ce94deSChris Mason 		if (eb && btrfs_buffer_uptodate(eb, gen))
1332b4ce94deSChris Mason 			block1 = 0;
1333b4ce94deSChris Mason 		free_extent_buffer(eb);
1334b4ce94deSChris Mason 	}
13358c594ea8SChris Mason 	if (slot + 1 < nritems) {
1336b4ce94deSChris Mason 		block2 = btrfs_node_blockptr(parent, slot + 1);
1337b4ce94deSChris Mason 		gen = btrfs_node_ptr_generation(parent, slot + 1);
1338b4ce94deSChris Mason 		eb = btrfs_find_tree_block(root, block2, blocksize);
1339b4ce94deSChris Mason 		if (eb && btrfs_buffer_uptodate(eb, gen))
1340b4ce94deSChris Mason 			block2 = 0;
1341b4ce94deSChris Mason 		free_extent_buffer(eb);
1342b4ce94deSChris Mason 	}
1343b4ce94deSChris Mason 	if (block1 || block2) {
1344b4ce94deSChris Mason 		ret = -EAGAIN;
13458c594ea8SChris Mason 
13468c594ea8SChris Mason 		/* release the whole path */
1347b3b4aa74SDavid Sterba 		btrfs_release_path(path);
13488c594ea8SChris Mason 
13498c594ea8SChris Mason 		/* read the blocks */
1350b4ce94deSChris Mason 		if (block1)
1351b4ce94deSChris Mason 			readahead_tree_block(root, block1, blocksize, 0);
1352b4ce94deSChris Mason 		if (block2)
1353b4ce94deSChris Mason 			readahead_tree_block(root, block2, blocksize, 0);
1354b4ce94deSChris Mason 
1355b4ce94deSChris Mason 		if (block1) {
1356b4ce94deSChris Mason 			eb = read_tree_block(root, block1, blocksize, 0);
1357b4ce94deSChris Mason 			free_extent_buffer(eb);
1358b4ce94deSChris Mason 		}
13598c594ea8SChris Mason 		if (block2) {
1360b4ce94deSChris Mason 			eb = read_tree_block(root, block2, blocksize, 0);
1361b4ce94deSChris Mason 			free_extent_buffer(eb);
1362b4ce94deSChris Mason 		}
1363b4ce94deSChris Mason 	}
1364b4ce94deSChris Mason 	return ret;
1365b4ce94deSChris Mason }
1366b4ce94deSChris Mason 
1367b4ce94deSChris Mason 
1368b4ce94deSChris Mason /*
1369d397712bSChris Mason  * when we walk down the tree, it is usually safe to unlock the higher layers
1370d397712bSChris Mason  * in the tree.  The exceptions are when our path goes through slot 0, because
1371d397712bSChris Mason  * operations on the tree might require changing key pointers higher up in the
1372d397712bSChris Mason  * tree.
1373d352ac68SChris Mason  *
1374d397712bSChris Mason  * callers might also have set path->keep_locks, which tells this code to keep
1375d397712bSChris Mason  * the lock if the path points to the last slot in the block.  This is part of
1376d397712bSChris Mason  * walking through the tree, and selecting the next slot in the higher block.
1377d352ac68SChris Mason  *
1378d397712bSChris Mason  * lowest_unlock sets the lowest level in the tree we're allowed to unlock.  so
1379d397712bSChris Mason  * if lowest_unlock is 1, level 0 won't be unlocked
1380d352ac68SChris Mason  */
1381e02119d5SChris Mason static noinline void unlock_up(struct btrfs_path *path, int level,
1382e02119d5SChris Mason 			       int lowest_unlock)
1383925baeddSChris Mason {
1384925baeddSChris Mason 	int i;
1385925baeddSChris Mason 	int skip_level = level;
1386051e1b9fSChris Mason 	int no_skips = 0;
1387925baeddSChris Mason 	struct extent_buffer *t;
1388925baeddSChris Mason 
1389925baeddSChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1390925baeddSChris Mason 		if (!path->nodes[i])
1391925baeddSChris Mason 			break;
1392925baeddSChris Mason 		if (!path->locks[i])
1393925baeddSChris Mason 			break;
1394051e1b9fSChris Mason 		if (!no_skips && path->slots[i] == 0) {
1395925baeddSChris Mason 			skip_level = i + 1;
1396925baeddSChris Mason 			continue;
1397925baeddSChris Mason 		}
1398051e1b9fSChris Mason 		if (!no_skips && path->keep_locks) {
1399925baeddSChris Mason 			u32 nritems;
1400925baeddSChris Mason 			t = path->nodes[i];
1401925baeddSChris Mason 			nritems = btrfs_header_nritems(t);
1402051e1b9fSChris Mason 			if (nritems < 1 || path->slots[i] >= nritems - 1) {
1403925baeddSChris Mason 				skip_level = i + 1;
1404925baeddSChris Mason 				continue;
1405925baeddSChris Mason 			}
1406925baeddSChris Mason 		}
1407051e1b9fSChris Mason 		if (skip_level < i && i >= lowest_unlock)
1408051e1b9fSChris Mason 			no_skips = 1;
1409051e1b9fSChris Mason 
1410925baeddSChris Mason 		t = path->nodes[i];
1411925baeddSChris Mason 		if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1412925baeddSChris Mason 			btrfs_tree_unlock(t);
1413925baeddSChris Mason 			path->locks[i] = 0;
1414925baeddSChris Mason 		}
1415925baeddSChris Mason 	}
1416925baeddSChris Mason }
1417925baeddSChris Mason 
14183c69faecSChris Mason /*
1419b4ce94deSChris Mason  * This releases any locks held in the path starting at level and
1420b4ce94deSChris Mason  * going all the way up to the root.
1421b4ce94deSChris Mason  *
1422b4ce94deSChris Mason  * btrfs_search_slot will keep the lock held on higher nodes in a few
1423b4ce94deSChris Mason  * corner cases, such as COW of the block at slot zero in the node.  This
1424b4ce94deSChris Mason  * ignores those rules, and it should only be called when there are no
1425b4ce94deSChris Mason  * more updates to be done higher up in the tree.
1426b4ce94deSChris Mason  */
1427b4ce94deSChris Mason noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1428b4ce94deSChris Mason {
1429b4ce94deSChris Mason 	int i;
1430b4ce94deSChris Mason 
14315d4f98a2SYan Zheng 	if (path->keep_locks)
1432b4ce94deSChris Mason 		return;
1433b4ce94deSChris Mason 
1434b4ce94deSChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1435b4ce94deSChris Mason 		if (!path->nodes[i])
143612f4daccSChris Mason 			continue;
1437b4ce94deSChris Mason 		if (!path->locks[i])
143812f4daccSChris Mason 			continue;
1439b4ce94deSChris Mason 		btrfs_tree_unlock(path->nodes[i]);
1440b4ce94deSChris Mason 		path->locks[i] = 0;
1441b4ce94deSChris Mason 	}
1442b4ce94deSChris Mason }
1443b4ce94deSChris Mason 
1444b4ce94deSChris Mason /*
1445c8c42864SChris Mason  * helper function for btrfs_search_slot.  The goal is to find a block
1446c8c42864SChris Mason  * in cache without setting the path to blocking.  If we find the block
1447c8c42864SChris Mason  * we return zero and the path is unchanged.
1448c8c42864SChris Mason  *
1449c8c42864SChris Mason  * If we can't find the block, we set the path blocking and do some
1450c8c42864SChris Mason  * reada.  -EAGAIN is returned and the search must be repeated.
1451c8c42864SChris Mason  */
1452c8c42864SChris Mason static int
1453c8c42864SChris Mason read_block_for_search(struct btrfs_trans_handle *trans,
1454c8c42864SChris Mason 		       struct btrfs_root *root, struct btrfs_path *p,
1455c8c42864SChris Mason 		       struct extent_buffer **eb_ret, int level, int slot,
1456c8c42864SChris Mason 		       struct btrfs_key *key)
1457c8c42864SChris Mason {
1458c8c42864SChris Mason 	u64 blocknr;
1459c8c42864SChris Mason 	u64 gen;
1460c8c42864SChris Mason 	u32 blocksize;
1461c8c42864SChris Mason 	struct extent_buffer *b = *eb_ret;
1462c8c42864SChris Mason 	struct extent_buffer *tmp;
146376a05b35SChris Mason 	int ret;
1464c8c42864SChris Mason 
1465c8c42864SChris Mason 	blocknr = btrfs_node_blockptr(b, slot);
1466c8c42864SChris Mason 	gen = btrfs_node_ptr_generation(b, slot);
1467c8c42864SChris Mason 	blocksize = btrfs_level_size(root, level - 1);
1468c8c42864SChris Mason 
1469c8c42864SChris Mason 	tmp = btrfs_find_tree_block(root, blocknr, blocksize);
1470cb44921aSChris Mason 	if (tmp) {
1471cb44921aSChris Mason 		if (btrfs_buffer_uptodate(tmp, 0)) {
1472cb44921aSChris Mason 			if (btrfs_buffer_uptodate(tmp, gen)) {
147376a05b35SChris Mason 				/*
1474cb44921aSChris Mason 				 * we found an up to date block without
1475cb44921aSChris Mason 				 * sleeping, return
147676a05b35SChris Mason 				 * right away
147776a05b35SChris Mason 				 */
1478c8c42864SChris Mason 				*eb_ret = tmp;
1479c8c42864SChris Mason 				return 0;
1480c8c42864SChris Mason 			}
1481cb44921aSChris Mason 			/* the pages were up to date, but we failed
1482cb44921aSChris Mason 			 * the generation number check.  Do a full
1483cb44921aSChris Mason 			 * read for the generation number that is correct.
1484cb44921aSChris Mason 			 * We must do this without dropping locks so
1485cb44921aSChris Mason 			 * we can trust our generation number
1486cb44921aSChris Mason 			 */
1487cb44921aSChris Mason 			free_extent_buffer(tmp);
1488cb44921aSChris Mason 			tmp = read_tree_block(root, blocknr, blocksize, gen);
1489cb44921aSChris Mason 			if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
1490cb44921aSChris Mason 				*eb_ret = tmp;
1491cb44921aSChris Mason 				return 0;
1492cb44921aSChris Mason 			}
1493cb44921aSChris Mason 			free_extent_buffer(tmp);
1494b3b4aa74SDavid Sterba 			btrfs_release_path(p);
1495cb44921aSChris Mason 			return -EIO;
1496cb44921aSChris Mason 		}
1497cb44921aSChris Mason 	}
1498c8c42864SChris Mason 
1499c8c42864SChris Mason 	/*
1500c8c42864SChris Mason 	 * reduce lock contention at high levels
1501c8c42864SChris Mason 	 * of the btree by dropping locks before
150276a05b35SChris Mason 	 * we read.  Don't release the lock on the current
150376a05b35SChris Mason 	 * level because we need to walk this node to figure
150476a05b35SChris Mason 	 * out which blocks to read.
1505c8c42864SChris Mason 	 */
15068c594ea8SChris Mason 	btrfs_unlock_up_safe(p, level + 1);
15078c594ea8SChris Mason 	btrfs_set_path_blocking(p);
15088c594ea8SChris Mason 
1509c8c42864SChris Mason 	free_extent_buffer(tmp);
1510c8c42864SChris Mason 	if (p->reada)
1511c8c42864SChris Mason 		reada_for_search(root, p, level, slot, key->objectid);
1512c8c42864SChris Mason 
1513b3b4aa74SDavid Sterba 	btrfs_release_path(p);
151476a05b35SChris Mason 
151576a05b35SChris Mason 	ret = -EAGAIN;
15165bdd3536SYan, Zheng 	tmp = read_tree_block(root, blocknr, blocksize, 0);
151776a05b35SChris Mason 	if (tmp) {
151876a05b35SChris Mason 		/*
151976a05b35SChris Mason 		 * If the read above didn't mark this buffer up to date,
152076a05b35SChris Mason 		 * it will never end up being up to date.  Set ret to EIO now
152176a05b35SChris Mason 		 * and give up so that our caller doesn't loop forever
152276a05b35SChris Mason 		 * on our EAGAINs.
152376a05b35SChris Mason 		 */
152476a05b35SChris Mason 		if (!btrfs_buffer_uptodate(tmp, 0))
152576a05b35SChris Mason 			ret = -EIO;
1526c8c42864SChris Mason 		free_extent_buffer(tmp);
152776a05b35SChris Mason 	}
152876a05b35SChris Mason 	return ret;
1529c8c42864SChris Mason }
1530c8c42864SChris Mason 
1531c8c42864SChris Mason /*
1532c8c42864SChris Mason  * helper function for btrfs_search_slot.  This does all of the checks
1533c8c42864SChris Mason  * for node-level blocks and does any balancing required based on
1534c8c42864SChris Mason  * the ins_len.
1535c8c42864SChris Mason  *
1536c8c42864SChris Mason  * If no extra work was required, zero is returned.  If we had to
1537c8c42864SChris Mason  * drop the path, -EAGAIN is returned and btrfs_search_slot must
1538c8c42864SChris Mason  * start over
1539c8c42864SChris Mason  */
1540c8c42864SChris Mason static int
1541c8c42864SChris Mason setup_nodes_for_search(struct btrfs_trans_handle *trans,
1542c8c42864SChris Mason 		       struct btrfs_root *root, struct btrfs_path *p,
1543c8c42864SChris Mason 		       struct extent_buffer *b, int level, int ins_len)
1544c8c42864SChris Mason {
1545c8c42864SChris Mason 	int ret;
1546c8c42864SChris Mason 	if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
1547c8c42864SChris Mason 	    BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1548c8c42864SChris Mason 		int sret;
1549c8c42864SChris Mason 
1550c8c42864SChris Mason 		sret = reada_for_balance(root, p, level);
1551c8c42864SChris Mason 		if (sret)
1552c8c42864SChris Mason 			goto again;
1553c8c42864SChris Mason 
1554c8c42864SChris Mason 		btrfs_set_path_blocking(p);
1555c8c42864SChris Mason 		sret = split_node(trans, root, p, level);
1556c8c42864SChris Mason 		btrfs_clear_path_blocking(p, NULL);
1557c8c42864SChris Mason 
1558c8c42864SChris Mason 		BUG_ON(sret > 0);
1559c8c42864SChris Mason 		if (sret) {
1560c8c42864SChris Mason 			ret = sret;
1561c8c42864SChris Mason 			goto done;
1562c8c42864SChris Mason 		}
1563c8c42864SChris Mason 		b = p->nodes[level];
1564c8c42864SChris Mason 	} else if (ins_len < 0 && btrfs_header_nritems(b) <
1565cfbb9308SChris Mason 		   BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
1566c8c42864SChris Mason 		int sret;
1567c8c42864SChris Mason 
1568c8c42864SChris Mason 		sret = reada_for_balance(root, p, level);
1569c8c42864SChris Mason 		if (sret)
1570c8c42864SChris Mason 			goto again;
1571c8c42864SChris Mason 
1572c8c42864SChris Mason 		btrfs_set_path_blocking(p);
1573c8c42864SChris Mason 		sret = balance_level(trans, root, p, level);
1574c8c42864SChris Mason 		btrfs_clear_path_blocking(p, NULL);
1575c8c42864SChris Mason 
1576c8c42864SChris Mason 		if (sret) {
1577c8c42864SChris Mason 			ret = sret;
1578c8c42864SChris Mason 			goto done;
1579c8c42864SChris Mason 		}
1580c8c42864SChris Mason 		b = p->nodes[level];
1581c8c42864SChris Mason 		if (!b) {
1582b3b4aa74SDavid Sterba 			btrfs_release_path(p);
1583c8c42864SChris Mason 			goto again;
1584c8c42864SChris Mason 		}
1585c8c42864SChris Mason 		BUG_ON(btrfs_header_nritems(b) == 1);
1586c8c42864SChris Mason 	}
1587c8c42864SChris Mason 	return 0;
1588c8c42864SChris Mason 
1589c8c42864SChris Mason again:
1590c8c42864SChris Mason 	ret = -EAGAIN;
1591c8c42864SChris Mason done:
1592c8c42864SChris Mason 	return ret;
1593c8c42864SChris Mason }
1594c8c42864SChris Mason 
1595c8c42864SChris Mason /*
159674123bd7SChris Mason  * look for key in the tree.  path is filled in with nodes along the way
159774123bd7SChris Mason  * if key is found, we return zero and you can find the item in the leaf
159874123bd7SChris Mason  * level of the path (level 0)
159974123bd7SChris Mason  *
160074123bd7SChris Mason  * If the key isn't found, the path points to the slot where it should
1601aa5d6bedSChris Mason  * be inserted, and 1 is returned.  If there are other errors during the
1602aa5d6bedSChris Mason  * search a negative error number is returned.
160397571fd0SChris Mason  *
160497571fd0SChris Mason  * if ins_len > 0, nodes and leaves will be split as we walk down the
160597571fd0SChris Mason  * tree.  if ins_len < 0, nodes will be merged as we walk down the tree (if
160697571fd0SChris Mason  * possible)
160774123bd7SChris Mason  */
1608e089f05cSChris Mason int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1609e089f05cSChris Mason 		      *root, struct btrfs_key *key, struct btrfs_path *p, int
1610e089f05cSChris Mason 		      ins_len, int cow)
1611be0e5c09SChris Mason {
16125f39d397SChris Mason 	struct extent_buffer *b;
1613be0e5c09SChris Mason 	int slot;
1614be0e5c09SChris Mason 	int ret;
161533c66f43SYan Zheng 	int err;
1616be0e5c09SChris Mason 	int level;
1617925baeddSChris Mason 	int lowest_unlock = 1;
16189f3a7427SChris Mason 	u8 lowest_level = 0;
16199f3a7427SChris Mason 
16206702ed49SChris Mason 	lowest_level = p->lowest_level;
1621323ac95bSChris Mason 	WARN_ON(lowest_level && ins_len > 0);
162222b0ebdaSChris Mason 	WARN_ON(p->nodes[0] != NULL);
162325179201SJosef Bacik 
1624925baeddSChris Mason 	if (ins_len < 0)
1625925baeddSChris Mason 		lowest_unlock = 2;
162665b51a00SChris Mason 
1627bb803951SChris Mason again:
16285d4f98a2SYan Zheng 	if (p->search_commit_root) {
16295d4f98a2SYan Zheng 		b = root->commit_root;
16305d4f98a2SYan Zheng 		extent_buffer_get(b);
16315d4f98a2SYan Zheng 		if (!p->skip_locking)
16325d4f98a2SYan Zheng 			btrfs_tree_lock(b);
16335d4f98a2SYan Zheng 	} else {
16345cd57b2cSChris Mason 		if (p->skip_locking)
16355cd57b2cSChris Mason 			b = btrfs_root_node(root);
16365cd57b2cSChris Mason 		else
1637925baeddSChris Mason 			b = btrfs_lock_root_node(root);
16385d4f98a2SYan Zheng 	}
1639925baeddSChris Mason 
1640eb60ceacSChris Mason 	while (b) {
16415f39d397SChris Mason 		level = btrfs_header_level(b);
164265b51a00SChris Mason 
164365b51a00SChris Mason 		/*
164465b51a00SChris Mason 		 * setup the path here so we can release it under lock
164565b51a00SChris Mason 		 * contention with the cow code
164665b51a00SChris Mason 		 */
164765b51a00SChris Mason 		p->nodes[level] = b;
164865b51a00SChris Mason 		if (!p->skip_locking)
164965b51a00SChris Mason 			p->locks[level] = 1;
165065b51a00SChris Mason 
165102217ed2SChris Mason 		if (cow) {
1652c8c42864SChris Mason 			/*
1653c8c42864SChris Mason 			 * if we don't really need to cow this block
1654c8c42864SChris Mason 			 * then we don't want to set the path blocking,
1655c8c42864SChris Mason 			 * so we test it here
1656c8c42864SChris Mason 			 */
16575d4f98a2SYan Zheng 			if (!should_cow_block(trans, root, b))
165865b51a00SChris Mason 				goto cow_done;
16595d4f98a2SYan Zheng 
1660b4ce94deSChris Mason 			btrfs_set_path_blocking(p);
1661b4ce94deSChris Mason 
166233c66f43SYan Zheng 			err = btrfs_cow_block(trans, root, b,
1663e20d96d6SChris Mason 					      p->nodes[level + 1],
16649fa8cfe7SChris Mason 					      p->slots[level + 1], &b);
166533c66f43SYan Zheng 			if (err) {
166633c66f43SYan Zheng 				ret = err;
166765b51a00SChris Mason 				goto done;
166854aa1f4dSChris Mason 			}
166902217ed2SChris Mason 		}
167065b51a00SChris Mason cow_done:
167102217ed2SChris Mason 		BUG_ON(!cow && ins_len);
167265b51a00SChris Mason 
1673eb60ceacSChris Mason 		p->nodes[level] = b;
16745cd57b2cSChris Mason 		if (!p->skip_locking)
1675925baeddSChris Mason 			p->locks[level] = 1;
167665b51a00SChris Mason 
16774008c04aSChris Mason 		btrfs_clear_path_blocking(p, NULL);
1678b4ce94deSChris Mason 
1679b4ce94deSChris Mason 		/*
1680b4ce94deSChris Mason 		 * we have a lock on b and as long as we aren't changing
1681b4ce94deSChris Mason 		 * the tree, there is no way to for the items in b to change.
1682b4ce94deSChris Mason 		 * It is safe to drop the lock on our parent before we
1683b4ce94deSChris Mason 		 * go through the expensive btree search on b.
1684b4ce94deSChris Mason 		 *
1685b4ce94deSChris Mason 		 * If cow is true, then we might be changing slot zero,
1686b4ce94deSChris Mason 		 * which may require changing the parent.  So, we can't
1687b4ce94deSChris Mason 		 * drop the lock until after we know which slot we're
1688b4ce94deSChris Mason 		 * operating on.
1689b4ce94deSChris Mason 		 */
1690b4ce94deSChris Mason 		if (!cow)
1691b4ce94deSChris Mason 			btrfs_unlock_up_safe(p, level + 1);
1692b4ce94deSChris Mason 
16935f39d397SChris Mason 		ret = bin_search(b, key, level, &slot);
1694b4ce94deSChris Mason 
16955f39d397SChris Mason 		if (level != 0) {
169633c66f43SYan Zheng 			int dec = 0;
169733c66f43SYan Zheng 			if (ret && slot > 0) {
169833c66f43SYan Zheng 				dec = 1;
1699be0e5c09SChris Mason 				slot -= 1;
170033c66f43SYan Zheng 			}
1701be0e5c09SChris Mason 			p->slots[level] = slot;
170233c66f43SYan Zheng 			err = setup_nodes_for_search(trans, root, p, b, level,
1703c8c42864SChris Mason 						     ins_len);
170433c66f43SYan Zheng 			if (err == -EAGAIN)
1705b4ce94deSChris Mason 				goto again;
170633c66f43SYan Zheng 			if (err) {
170733c66f43SYan Zheng 				ret = err;
170865b51a00SChris Mason 				goto done;
170933c66f43SYan Zheng 			}
17105c680ed6SChris Mason 			b = p->nodes[level];
17115c680ed6SChris Mason 			slot = p->slots[level];
1712b4ce94deSChris Mason 
1713f9efa9c7SChris Mason 			unlock_up(p, level, lowest_unlock);
1714f9efa9c7SChris Mason 
1715925baeddSChris Mason 			if (level == lowest_level) {
171633c66f43SYan Zheng 				if (dec)
171733c66f43SYan Zheng 					p->slots[level]++;
17185b21f2edSZheng Yan 				goto done;
1719925baeddSChris Mason 			}
1720ca7a79adSChris Mason 
172133c66f43SYan Zheng 			err = read_block_for_search(trans, root, p,
1722c8c42864SChris Mason 						    &b, level, slot, key);
172333c66f43SYan Zheng 			if (err == -EAGAIN)
1724051e1b9fSChris Mason 				goto again;
172533c66f43SYan Zheng 			if (err) {
172633c66f43SYan Zheng 				ret = err;
172776a05b35SChris Mason 				goto done;
172833c66f43SYan Zheng 			}
172976a05b35SChris Mason 
1730b4ce94deSChris Mason 			if (!p->skip_locking) {
17314008c04aSChris Mason 				btrfs_clear_path_blocking(p, NULL);
173233c66f43SYan Zheng 				err = btrfs_try_spin_lock(b);
1733b4ce94deSChris Mason 
173433c66f43SYan Zheng 				if (!err) {
1735b4ce94deSChris Mason 					btrfs_set_path_blocking(p);
1736925baeddSChris Mason 					btrfs_tree_lock(b);
17374008c04aSChris Mason 					btrfs_clear_path_blocking(p, b);
1738b4ce94deSChris Mason 				}
1739b4ce94deSChris Mason 			}
1740be0e5c09SChris Mason 		} else {
1741be0e5c09SChris Mason 			p->slots[level] = slot;
174287b29b20SYan Zheng 			if (ins_len > 0 &&
174387b29b20SYan Zheng 			    btrfs_leaf_free_space(root, b) < ins_len) {
1744b4ce94deSChris Mason 				btrfs_set_path_blocking(p);
174533c66f43SYan Zheng 				err = split_leaf(trans, root, key,
1746cc0c5538SChris Mason 						 p, ins_len, ret == 0);
17474008c04aSChris Mason 				btrfs_clear_path_blocking(p, NULL);
1748b4ce94deSChris Mason 
174933c66f43SYan Zheng 				BUG_ON(err > 0);
175033c66f43SYan Zheng 				if (err) {
175133c66f43SYan Zheng 					ret = err;
175265b51a00SChris Mason 					goto done;
175365b51a00SChris Mason 				}
17545c680ed6SChris Mason 			}
1755459931ecSChris Mason 			if (!p->search_for_split)
1756925baeddSChris Mason 				unlock_up(p, level, lowest_unlock);
175765b51a00SChris Mason 			goto done;
175865b51a00SChris Mason 		}
175965b51a00SChris Mason 	}
176065b51a00SChris Mason 	ret = 1;
176165b51a00SChris Mason done:
1762b4ce94deSChris Mason 	/*
1763b4ce94deSChris Mason 	 * we don't really know what they plan on doing with the path
1764b4ce94deSChris Mason 	 * from here on, so for now just mark it as blocking
1765b4ce94deSChris Mason 	 */
1766b9473439SChris Mason 	if (!p->leave_spinning)
1767b4ce94deSChris Mason 		btrfs_set_path_blocking(p);
176876a05b35SChris Mason 	if (ret < 0)
1769b3b4aa74SDavid Sterba 		btrfs_release_path(p);
1770be0e5c09SChris Mason 	return ret;
1771be0e5c09SChris Mason }
1772be0e5c09SChris Mason 
177374123bd7SChris Mason /*
177474123bd7SChris Mason  * adjust the pointers going up the tree, starting at level
177574123bd7SChris Mason  * making sure the right key of each node is points to 'key'.
177674123bd7SChris Mason  * This is used after shifting pointers to the left, so it stops
177774123bd7SChris Mason  * fixing up pointers when a given leaf/node is not in slot 0 of the
177874123bd7SChris Mason  * higher levels
1779aa5d6bedSChris Mason  *
1780aa5d6bedSChris Mason  * If this fails to write a tree block, it returns -1, but continues
1781aa5d6bedSChris Mason  * fixing up the blocks in ram so the tree is consistent.
178274123bd7SChris Mason  */
17835f39d397SChris Mason static int fixup_low_keys(struct btrfs_trans_handle *trans,
17845f39d397SChris Mason 			  struct btrfs_root *root, struct btrfs_path *path,
17855f39d397SChris Mason 			  struct btrfs_disk_key *key, int level)
1786be0e5c09SChris Mason {
1787be0e5c09SChris Mason 	int i;
1788aa5d6bedSChris Mason 	int ret = 0;
17895f39d397SChris Mason 	struct extent_buffer *t;
17905f39d397SChris Mason 
1791234b63a0SChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1792be0e5c09SChris Mason 		int tslot = path->slots[i];
1793eb60ceacSChris Mason 		if (!path->nodes[i])
1794be0e5c09SChris Mason 			break;
17955f39d397SChris Mason 		t = path->nodes[i];
17965f39d397SChris Mason 		btrfs_set_node_key(t, key, tslot);
1797d6025579SChris Mason 		btrfs_mark_buffer_dirty(path->nodes[i]);
1798be0e5c09SChris Mason 		if (tslot != 0)
1799be0e5c09SChris Mason 			break;
1800be0e5c09SChris Mason 	}
1801aa5d6bedSChris Mason 	return ret;
1802be0e5c09SChris Mason }
1803be0e5c09SChris Mason 
180474123bd7SChris Mason /*
180531840ae1SZheng Yan  * update item key.
180631840ae1SZheng Yan  *
180731840ae1SZheng Yan  * This function isn't completely safe. It's the caller's responsibility
180831840ae1SZheng Yan  * that the new key won't break the order
180931840ae1SZheng Yan  */
181031840ae1SZheng Yan int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
181131840ae1SZheng Yan 			    struct btrfs_root *root, struct btrfs_path *path,
181231840ae1SZheng Yan 			    struct btrfs_key *new_key)
181331840ae1SZheng Yan {
181431840ae1SZheng Yan 	struct btrfs_disk_key disk_key;
181531840ae1SZheng Yan 	struct extent_buffer *eb;
181631840ae1SZheng Yan 	int slot;
181731840ae1SZheng Yan 
181831840ae1SZheng Yan 	eb = path->nodes[0];
181931840ae1SZheng Yan 	slot = path->slots[0];
182031840ae1SZheng Yan 	if (slot > 0) {
182131840ae1SZheng Yan 		btrfs_item_key(eb, &disk_key, slot - 1);
182231840ae1SZheng Yan 		if (comp_keys(&disk_key, new_key) >= 0)
182331840ae1SZheng Yan 			return -1;
182431840ae1SZheng Yan 	}
182531840ae1SZheng Yan 	if (slot < btrfs_header_nritems(eb) - 1) {
182631840ae1SZheng Yan 		btrfs_item_key(eb, &disk_key, slot + 1);
182731840ae1SZheng Yan 		if (comp_keys(&disk_key, new_key) <= 0)
182831840ae1SZheng Yan 			return -1;
182931840ae1SZheng Yan 	}
183031840ae1SZheng Yan 
183131840ae1SZheng Yan 	btrfs_cpu_key_to_disk(&disk_key, new_key);
183231840ae1SZheng Yan 	btrfs_set_item_key(eb, &disk_key, slot);
183331840ae1SZheng Yan 	btrfs_mark_buffer_dirty(eb);
183431840ae1SZheng Yan 	if (slot == 0)
183531840ae1SZheng Yan 		fixup_low_keys(trans, root, path, &disk_key, 1);
183631840ae1SZheng Yan 	return 0;
183731840ae1SZheng Yan }
183831840ae1SZheng Yan 
183931840ae1SZheng Yan /*
184074123bd7SChris Mason  * try to push data from one node into the next node left in the
184179f95c82SChris Mason  * tree.
1842aa5d6bedSChris Mason  *
1843aa5d6bedSChris Mason  * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
1844aa5d6bedSChris Mason  * error, and > 0 if there was no room in the left hand block.
184574123bd7SChris Mason  */
184698ed5174SChris Mason static int push_node_left(struct btrfs_trans_handle *trans,
184798ed5174SChris Mason 			  struct btrfs_root *root, struct extent_buffer *dst,
1848971a1f66SChris Mason 			  struct extent_buffer *src, int empty)
1849be0e5c09SChris Mason {
1850be0e5c09SChris Mason 	int push_items = 0;
1851bb803951SChris Mason 	int src_nritems;
1852bb803951SChris Mason 	int dst_nritems;
1853aa5d6bedSChris Mason 	int ret = 0;
1854be0e5c09SChris Mason 
18555f39d397SChris Mason 	src_nritems = btrfs_header_nritems(src);
18565f39d397SChris Mason 	dst_nritems = btrfs_header_nritems(dst);
1857123abc88SChris Mason 	push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
18587bb86316SChris Mason 	WARN_ON(btrfs_header_generation(src) != trans->transid);
18597bb86316SChris Mason 	WARN_ON(btrfs_header_generation(dst) != trans->transid);
186054aa1f4dSChris Mason 
1861bce4eae9SChris Mason 	if (!empty && src_nritems <= 8)
1862971a1f66SChris Mason 		return 1;
1863971a1f66SChris Mason 
1864d397712bSChris Mason 	if (push_items <= 0)
1865be0e5c09SChris Mason 		return 1;
1866be0e5c09SChris Mason 
1867bce4eae9SChris Mason 	if (empty) {
1868971a1f66SChris Mason 		push_items = min(src_nritems, push_items);
1869bce4eae9SChris Mason 		if (push_items < src_nritems) {
1870bce4eae9SChris Mason 			/* leave at least 8 pointers in the node if
1871bce4eae9SChris Mason 			 * we aren't going to empty it
1872bce4eae9SChris Mason 			 */
1873bce4eae9SChris Mason 			if (src_nritems - push_items < 8) {
1874bce4eae9SChris Mason 				if (push_items <= 8)
1875bce4eae9SChris Mason 					return 1;
1876bce4eae9SChris Mason 				push_items -= 8;
1877bce4eae9SChris Mason 			}
1878bce4eae9SChris Mason 		}
1879bce4eae9SChris Mason 	} else
1880bce4eae9SChris Mason 		push_items = min(src_nritems - 8, push_items);
188179f95c82SChris Mason 
18825f39d397SChris Mason 	copy_extent_buffer(dst, src,
18835f39d397SChris Mason 			   btrfs_node_key_ptr_offset(dst_nritems),
18845f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
1885123abc88SChris Mason 			   push_items * sizeof(struct btrfs_key_ptr));
18865f39d397SChris Mason 
1887bb803951SChris Mason 	if (push_items < src_nritems) {
18885f39d397SChris Mason 		memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
18895f39d397SChris Mason 				      btrfs_node_key_ptr_offset(push_items),
1890e2fa7227SChris Mason 				      (src_nritems - push_items) *
1891123abc88SChris Mason 				      sizeof(struct btrfs_key_ptr));
1892bb803951SChris Mason 	}
18935f39d397SChris Mason 	btrfs_set_header_nritems(src, src_nritems - push_items);
18945f39d397SChris Mason 	btrfs_set_header_nritems(dst, dst_nritems + push_items);
18955f39d397SChris Mason 	btrfs_mark_buffer_dirty(src);
18965f39d397SChris Mason 	btrfs_mark_buffer_dirty(dst);
189731840ae1SZheng Yan 
1898bb803951SChris Mason 	return ret;
1899be0e5c09SChris Mason }
1900be0e5c09SChris Mason 
190197571fd0SChris Mason /*
190279f95c82SChris Mason  * try to push data from one node into the next node right in the
190379f95c82SChris Mason  * tree.
190479f95c82SChris Mason  *
190579f95c82SChris Mason  * returns 0 if some ptrs were pushed, < 0 if there was some horrible
190679f95c82SChris Mason  * error, and > 0 if there was no room in the right hand block.
190779f95c82SChris Mason  *
190879f95c82SChris Mason  * this will  only push up to 1/2 the contents of the left node over
190979f95c82SChris Mason  */
19105f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans,
19115f39d397SChris Mason 			      struct btrfs_root *root,
19125f39d397SChris Mason 			      struct extent_buffer *dst,
19135f39d397SChris Mason 			      struct extent_buffer *src)
191479f95c82SChris Mason {
191579f95c82SChris Mason 	int push_items = 0;
191679f95c82SChris Mason 	int max_push;
191779f95c82SChris Mason 	int src_nritems;
191879f95c82SChris Mason 	int dst_nritems;
191979f95c82SChris Mason 	int ret = 0;
192079f95c82SChris Mason 
19217bb86316SChris Mason 	WARN_ON(btrfs_header_generation(src) != trans->transid);
19227bb86316SChris Mason 	WARN_ON(btrfs_header_generation(dst) != trans->transid);
19237bb86316SChris Mason 
19245f39d397SChris Mason 	src_nritems = btrfs_header_nritems(src);
19255f39d397SChris Mason 	dst_nritems = btrfs_header_nritems(dst);
1926123abc88SChris Mason 	push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
1927d397712bSChris Mason 	if (push_items <= 0)
192879f95c82SChris Mason 		return 1;
1929bce4eae9SChris Mason 
1930d397712bSChris Mason 	if (src_nritems < 4)
1931bce4eae9SChris Mason 		return 1;
193279f95c82SChris Mason 
193379f95c82SChris Mason 	max_push = src_nritems / 2 + 1;
193479f95c82SChris Mason 	/* don't try to empty the node */
1935d397712bSChris Mason 	if (max_push >= src_nritems)
193679f95c82SChris Mason 		return 1;
1937252c38f0SYan 
193879f95c82SChris Mason 	if (max_push < push_items)
193979f95c82SChris Mason 		push_items = max_push;
194079f95c82SChris Mason 
19415f39d397SChris Mason 	memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
19425f39d397SChris Mason 				      btrfs_node_key_ptr_offset(0),
19435f39d397SChris Mason 				      (dst_nritems) *
19445f39d397SChris Mason 				      sizeof(struct btrfs_key_ptr));
1945d6025579SChris Mason 
19465f39d397SChris Mason 	copy_extent_buffer(dst, src,
19475f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
19485f39d397SChris Mason 			   btrfs_node_key_ptr_offset(src_nritems - push_items),
1949123abc88SChris Mason 			   push_items * sizeof(struct btrfs_key_ptr));
195079f95c82SChris Mason 
19515f39d397SChris Mason 	btrfs_set_header_nritems(src, src_nritems - push_items);
19525f39d397SChris Mason 	btrfs_set_header_nritems(dst, dst_nritems + push_items);
195379f95c82SChris Mason 
19545f39d397SChris Mason 	btrfs_mark_buffer_dirty(src);
19555f39d397SChris Mason 	btrfs_mark_buffer_dirty(dst);
195631840ae1SZheng Yan 
195779f95c82SChris Mason 	return ret;
195879f95c82SChris Mason }
195979f95c82SChris Mason 
196079f95c82SChris Mason /*
196197571fd0SChris Mason  * helper function to insert a new root level in the tree.
196297571fd0SChris Mason  * A new node is allocated, and a single item is inserted to
196397571fd0SChris Mason  * point to the existing root
1964aa5d6bedSChris Mason  *
1965aa5d6bedSChris Mason  * returns zero on success or < 0 on failure.
196697571fd0SChris Mason  */
1967d397712bSChris Mason static noinline int insert_new_root(struct btrfs_trans_handle *trans,
19685f39d397SChris Mason 			   struct btrfs_root *root,
19695f39d397SChris Mason 			   struct btrfs_path *path, int level)
197074123bd7SChris Mason {
19717bb86316SChris Mason 	u64 lower_gen;
19725f39d397SChris Mason 	struct extent_buffer *lower;
19735f39d397SChris Mason 	struct extent_buffer *c;
1974925baeddSChris Mason 	struct extent_buffer *old;
19755f39d397SChris Mason 	struct btrfs_disk_key lower_key;
19765c680ed6SChris Mason 
19775c680ed6SChris Mason 	BUG_ON(path->nodes[level]);
19785c680ed6SChris Mason 	BUG_ON(path->nodes[level-1] != root->node);
19795c680ed6SChris Mason 
19807bb86316SChris Mason 	lower = path->nodes[level-1];
19817bb86316SChris Mason 	if (level == 1)
19827bb86316SChris Mason 		btrfs_item_key(lower, &lower_key, 0);
19837bb86316SChris Mason 	else
19847bb86316SChris Mason 		btrfs_node_key(lower, &lower_key, 0);
19857bb86316SChris Mason 
198631840ae1SZheng Yan 	c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
19875d4f98a2SYan Zheng 				   root->root_key.objectid, &lower_key,
1988ad3d81baSChristoph Hellwig 				   level, root->node->start, 0);
19895f39d397SChris Mason 	if (IS_ERR(c))
19905f39d397SChris Mason 		return PTR_ERR(c);
1991925baeddSChris Mason 
1992f0486c68SYan, Zheng 	root_add_used(root, root->nodesize);
1993f0486c68SYan, Zheng 
19945d4f98a2SYan Zheng 	memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
19955f39d397SChris Mason 	btrfs_set_header_nritems(c, 1);
19965f39d397SChris Mason 	btrfs_set_header_level(c, level);
1997db94535dSChris Mason 	btrfs_set_header_bytenr(c, c->start);
19985f39d397SChris Mason 	btrfs_set_header_generation(c, trans->transid);
19995d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
20005f39d397SChris Mason 	btrfs_set_header_owner(c, root->root_key.objectid);
2001d5719762SChris Mason 
20025f39d397SChris Mason 	write_extent_buffer(c, root->fs_info->fsid,
20035f39d397SChris Mason 			    (unsigned long)btrfs_header_fsid(c),
20045f39d397SChris Mason 			    BTRFS_FSID_SIZE);
2005e17cade2SChris Mason 
2006e17cade2SChris Mason 	write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2007e17cade2SChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(c),
2008e17cade2SChris Mason 			    BTRFS_UUID_SIZE);
2009e17cade2SChris Mason 
20105f39d397SChris Mason 	btrfs_set_node_key(c, &lower_key, 0);
2011db94535dSChris Mason 	btrfs_set_node_blockptr(c, 0, lower->start);
20127bb86316SChris Mason 	lower_gen = btrfs_header_generation(lower);
201331840ae1SZheng Yan 	WARN_ON(lower_gen != trans->transid);
20147bb86316SChris Mason 
20157bb86316SChris Mason 	btrfs_set_node_ptr_generation(c, 0, lower_gen);
20165f39d397SChris Mason 
20175f39d397SChris Mason 	btrfs_mark_buffer_dirty(c);
2018d5719762SChris Mason 
2019925baeddSChris Mason 	old = root->node;
2020240f62c8SChris Mason 	rcu_assign_pointer(root->node, c);
2021925baeddSChris Mason 
2022925baeddSChris Mason 	/* the super has an extra ref to root->node */
2023925baeddSChris Mason 	free_extent_buffer(old);
2024925baeddSChris Mason 
20250b86a832SChris Mason 	add_root_to_dirty_list(root);
20265f39d397SChris Mason 	extent_buffer_get(c);
20275f39d397SChris Mason 	path->nodes[level] = c;
2028925baeddSChris Mason 	path->locks[level] = 1;
202974123bd7SChris Mason 	path->slots[level] = 0;
203074123bd7SChris Mason 	return 0;
203174123bd7SChris Mason }
20325c680ed6SChris Mason 
20335c680ed6SChris Mason /*
20345c680ed6SChris Mason  * worker function to insert a single pointer in a node.
20355c680ed6SChris Mason  * the node should have enough room for the pointer already
203697571fd0SChris Mason  *
20375c680ed6SChris Mason  * slot and level indicate where you want the key to go, and
20385c680ed6SChris Mason  * blocknr is the block the key points to.
2039aa5d6bedSChris Mason  *
2040aa5d6bedSChris Mason  * returns zero on success and < 0 on any error
20415c680ed6SChris Mason  */
2042e089f05cSChris Mason static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
2043e089f05cSChris Mason 		      *root, struct btrfs_path *path, struct btrfs_disk_key
2044db94535dSChris Mason 		      *key, u64 bytenr, int slot, int level)
20455c680ed6SChris Mason {
20465f39d397SChris Mason 	struct extent_buffer *lower;
20475c680ed6SChris Mason 	int nritems;
20485c680ed6SChris Mason 
20495c680ed6SChris Mason 	BUG_ON(!path->nodes[level]);
2050f0486c68SYan, Zheng 	btrfs_assert_tree_locked(path->nodes[level]);
20515f39d397SChris Mason 	lower = path->nodes[level];
20525f39d397SChris Mason 	nritems = btrfs_header_nritems(lower);
2053c293498bSStoyan Gaydarov 	BUG_ON(slot > nritems);
2054123abc88SChris Mason 	if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
205574123bd7SChris Mason 		BUG();
205674123bd7SChris Mason 	if (slot != nritems) {
20575f39d397SChris Mason 		memmove_extent_buffer(lower,
20585f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot + 1),
20595f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot),
2060123abc88SChris Mason 			      (nritems - slot) * sizeof(struct btrfs_key_ptr));
206174123bd7SChris Mason 	}
20625f39d397SChris Mason 	btrfs_set_node_key(lower, key, slot);
2063db94535dSChris Mason 	btrfs_set_node_blockptr(lower, slot, bytenr);
206474493f7aSChris Mason 	WARN_ON(trans->transid == 0);
206574493f7aSChris Mason 	btrfs_set_node_ptr_generation(lower, slot, trans->transid);
20665f39d397SChris Mason 	btrfs_set_header_nritems(lower, nritems + 1);
20675f39d397SChris Mason 	btrfs_mark_buffer_dirty(lower);
206874123bd7SChris Mason 	return 0;
206974123bd7SChris Mason }
207074123bd7SChris Mason 
207197571fd0SChris Mason /*
207297571fd0SChris Mason  * split the node at the specified level in path in two.
207397571fd0SChris Mason  * The path is corrected to point to the appropriate node after the split
207497571fd0SChris Mason  *
207597571fd0SChris Mason  * Before splitting this tries to make some room in the node by pushing
207697571fd0SChris Mason  * left and right, if either one works, it returns right away.
2077aa5d6bedSChris Mason  *
2078aa5d6bedSChris Mason  * returns 0 on success and < 0 on failure
207997571fd0SChris Mason  */
2080e02119d5SChris Mason static noinline int split_node(struct btrfs_trans_handle *trans,
2081e02119d5SChris Mason 			       struct btrfs_root *root,
2082e02119d5SChris Mason 			       struct btrfs_path *path, int level)
2083be0e5c09SChris Mason {
20845f39d397SChris Mason 	struct extent_buffer *c;
20855f39d397SChris Mason 	struct extent_buffer *split;
20865f39d397SChris Mason 	struct btrfs_disk_key disk_key;
2087be0e5c09SChris Mason 	int mid;
20885c680ed6SChris Mason 	int ret;
2089aa5d6bedSChris Mason 	int wret;
20907518a238SChris Mason 	u32 c_nritems;
2091be0e5c09SChris Mason 
20925f39d397SChris Mason 	c = path->nodes[level];
20937bb86316SChris Mason 	WARN_ON(btrfs_header_generation(c) != trans->transid);
20945f39d397SChris Mason 	if (c == root->node) {
20955c680ed6SChris Mason 		/* trying to split the root, lets make a new one */
2096e089f05cSChris Mason 		ret = insert_new_root(trans, root, path, level + 1);
20975c680ed6SChris Mason 		if (ret)
20985c680ed6SChris Mason 			return ret;
2099b3612421SChris Mason 	} else {
2100e66f709bSChris Mason 		ret = push_nodes_for_insert(trans, root, path, level);
21015f39d397SChris Mason 		c = path->nodes[level];
21025f39d397SChris Mason 		if (!ret && btrfs_header_nritems(c) <
2103c448acf0SChris Mason 		    BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
2104e66f709bSChris Mason 			return 0;
210554aa1f4dSChris Mason 		if (ret < 0)
210654aa1f4dSChris Mason 			return ret;
21075c680ed6SChris Mason 	}
2108e66f709bSChris Mason 
21095f39d397SChris Mason 	c_nritems = btrfs_header_nritems(c);
21105d4f98a2SYan Zheng 	mid = (c_nritems + 1) / 2;
21115d4f98a2SYan Zheng 	btrfs_node_key(c, &disk_key, mid);
21127bb86316SChris Mason 
21135d4f98a2SYan Zheng 	split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
21147bb86316SChris Mason 					root->root_key.objectid,
21155d4f98a2SYan Zheng 					&disk_key, level, c->start, 0);
21165f39d397SChris Mason 	if (IS_ERR(split))
21175f39d397SChris Mason 		return PTR_ERR(split);
211854aa1f4dSChris Mason 
2119f0486c68SYan, Zheng 	root_add_used(root, root->nodesize);
2120f0486c68SYan, Zheng 
21215d4f98a2SYan Zheng 	memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
21225f39d397SChris Mason 	btrfs_set_header_level(split, btrfs_header_level(c));
2123db94535dSChris Mason 	btrfs_set_header_bytenr(split, split->start);
21245f39d397SChris Mason 	btrfs_set_header_generation(split, trans->transid);
21255d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
21265f39d397SChris Mason 	btrfs_set_header_owner(split, root->root_key.objectid);
21275f39d397SChris Mason 	write_extent_buffer(split, root->fs_info->fsid,
21285f39d397SChris Mason 			    (unsigned long)btrfs_header_fsid(split),
21295f39d397SChris Mason 			    BTRFS_FSID_SIZE);
2130e17cade2SChris Mason 	write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2131e17cade2SChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(split),
2132e17cade2SChris Mason 			    BTRFS_UUID_SIZE);
21335f39d397SChris Mason 
21345f39d397SChris Mason 
21355f39d397SChris Mason 	copy_extent_buffer(split, c,
21365f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
21375f39d397SChris Mason 			   btrfs_node_key_ptr_offset(mid),
2138123abc88SChris Mason 			   (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
21395f39d397SChris Mason 	btrfs_set_header_nritems(split, c_nritems - mid);
21405f39d397SChris Mason 	btrfs_set_header_nritems(c, mid);
2141aa5d6bedSChris Mason 	ret = 0;
2142aa5d6bedSChris Mason 
21435f39d397SChris Mason 	btrfs_mark_buffer_dirty(c);
21445f39d397SChris Mason 	btrfs_mark_buffer_dirty(split);
21455f39d397SChris Mason 
2146db94535dSChris Mason 	wret = insert_ptr(trans, root, path, &disk_key, split->start,
21475f39d397SChris Mason 			  path->slots[level + 1] + 1,
2148123abc88SChris Mason 			  level + 1);
2149aa5d6bedSChris Mason 	if (wret)
2150aa5d6bedSChris Mason 		ret = wret;
2151aa5d6bedSChris Mason 
21525de08d7dSChris Mason 	if (path->slots[level] >= mid) {
21535c680ed6SChris Mason 		path->slots[level] -= mid;
2154925baeddSChris Mason 		btrfs_tree_unlock(c);
21555f39d397SChris Mason 		free_extent_buffer(c);
21565f39d397SChris Mason 		path->nodes[level] = split;
21575c680ed6SChris Mason 		path->slots[level + 1] += 1;
2158eb60ceacSChris Mason 	} else {
2159925baeddSChris Mason 		btrfs_tree_unlock(split);
21605f39d397SChris Mason 		free_extent_buffer(split);
2161be0e5c09SChris Mason 	}
2162aa5d6bedSChris Mason 	return ret;
2163be0e5c09SChris Mason }
2164be0e5c09SChris Mason 
216574123bd7SChris Mason /*
216674123bd7SChris Mason  * how many bytes are required to store the items in a leaf.  start
216774123bd7SChris Mason  * and nr indicate which items in the leaf to check.  This totals up the
216874123bd7SChris Mason  * space used both by the item structs and the item data
216974123bd7SChris Mason  */
21705f39d397SChris Mason static int leaf_space_used(struct extent_buffer *l, int start, int nr)
2171be0e5c09SChris Mason {
2172be0e5c09SChris Mason 	int data_len;
21735f39d397SChris Mason 	int nritems = btrfs_header_nritems(l);
2174d4dbff95SChris Mason 	int end = min(nritems, start + nr) - 1;
2175be0e5c09SChris Mason 
2176be0e5c09SChris Mason 	if (!nr)
2177be0e5c09SChris Mason 		return 0;
21785f39d397SChris Mason 	data_len = btrfs_item_end_nr(l, start);
21795f39d397SChris Mason 	data_len = data_len - btrfs_item_offset_nr(l, end);
21800783fcfcSChris Mason 	data_len += sizeof(struct btrfs_item) * nr;
2181d4dbff95SChris Mason 	WARN_ON(data_len < 0);
2182be0e5c09SChris Mason 	return data_len;
2183be0e5c09SChris Mason }
2184be0e5c09SChris Mason 
218574123bd7SChris Mason /*
2186d4dbff95SChris Mason  * The space between the end of the leaf items and
2187d4dbff95SChris Mason  * the start of the leaf data.  IOW, how much room
2188d4dbff95SChris Mason  * the leaf has left for both items and data
2189d4dbff95SChris Mason  */
2190d397712bSChris Mason noinline int btrfs_leaf_free_space(struct btrfs_root *root,
2191e02119d5SChris Mason 				   struct extent_buffer *leaf)
2192d4dbff95SChris Mason {
21935f39d397SChris Mason 	int nritems = btrfs_header_nritems(leaf);
21945f39d397SChris Mason 	int ret;
21955f39d397SChris Mason 	ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
21965f39d397SChris Mason 	if (ret < 0) {
2197d397712bSChris Mason 		printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2198d397712bSChris Mason 		       "used %d nritems %d\n",
2199ae2f5411SJens Axboe 		       ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
22005f39d397SChris Mason 		       leaf_space_used(leaf, 0, nritems), nritems);
22015f39d397SChris Mason 	}
22025f39d397SChris Mason 	return ret;
2203d4dbff95SChris Mason }
2204d4dbff95SChris Mason 
220599d8f83cSChris Mason /*
220699d8f83cSChris Mason  * min slot controls the lowest index we're willing to push to the
220799d8f83cSChris Mason  * right.  We'll push up to and including min_slot, but no lower
220899d8f83cSChris Mason  */
220944871b1bSChris Mason static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
221044871b1bSChris Mason 				      struct btrfs_root *root,
221144871b1bSChris Mason 				      struct btrfs_path *path,
221244871b1bSChris Mason 				      int data_size, int empty,
221344871b1bSChris Mason 				      struct extent_buffer *right,
221499d8f83cSChris Mason 				      int free_space, u32 left_nritems,
221599d8f83cSChris Mason 				      u32 min_slot)
221600ec4c51SChris Mason {
22175f39d397SChris Mason 	struct extent_buffer *left = path->nodes[0];
221844871b1bSChris Mason 	struct extent_buffer *upper = path->nodes[1];
22195f39d397SChris Mason 	struct btrfs_disk_key disk_key;
222000ec4c51SChris Mason 	int slot;
222134a38218SChris Mason 	u32 i;
222200ec4c51SChris Mason 	int push_space = 0;
222300ec4c51SChris Mason 	int push_items = 0;
22240783fcfcSChris Mason 	struct btrfs_item *item;
222534a38218SChris Mason 	u32 nr;
22267518a238SChris Mason 	u32 right_nritems;
22275f39d397SChris Mason 	u32 data_end;
2228db94535dSChris Mason 	u32 this_item_size;
222900ec4c51SChris Mason 
223034a38218SChris Mason 	if (empty)
223134a38218SChris Mason 		nr = 0;
223234a38218SChris Mason 	else
223399d8f83cSChris Mason 		nr = max_t(u32, 1, min_slot);
223434a38218SChris Mason 
223531840ae1SZheng Yan 	if (path->slots[0] >= left_nritems)
223687b29b20SYan Zheng 		push_space += data_size;
223731840ae1SZheng Yan 
223844871b1bSChris Mason 	slot = path->slots[1];
223934a38218SChris Mason 	i = left_nritems - 1;
224034a38218SChris Mason 	while (i >= nr) {
22415f39d397SChris Mason 		item = btrfs_item_nr(left, i);
2242db94535dSChris Mason 
224331840ae1SZheng Yan 		if (!empty && push_items > 0) {
224431840ae1SZheng Yan 			if (path->slots[0] > i)
224531840ae1SZheng Yan 				break;
224631840ae1SZheng Yan 			if (path->slots[0] == i) {
224731840ae1SZheng Yan 				int space = btrfs_leaf_free_space(root, left);
224831840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
224931840ae1SZheng Yan 					break;
225031840ae1SZheng Yan 			}
225131840ae1SZheng Yan 		}
225231840ae1SZheng Yan 
225300ec4c51SChris Mason 		if (path->slots[0] == i)
225487b29b20SYan Zheng 			push_space += data_size;
2255db94535dSChris Mason 
2256db94535dSChris Mason 		if (!left->map_token) {
2257db94535dSChris Mason 			map_extent_buffer(left, (unsigned long)item,
2258db94535dSChris Mason 					sizeof(struct btrfs_item),
2259db94535dSChris Mason 					&left->map_token, &left->kaddr,
2260db94535dSChris Mason 					&left->map_start, &left->map_len,
2261db94535dSChris Mason 					KM_USER1);
2262db94535dSChris Mason 		}
2263db94535dSChris Mason 
2264db94535dSChris Mason 		this_item_size = btrfs_item_size(left, item);
2265db94535dSChris Mason 		if (this_item_size + sizeof(*item) + push_space > free_space)
226600ec4c51SChris Mason 			break;
226731840ae1SZheng Yan 
226800ec4c51SChris Mason 		push_items++;
2269db94535dSChris Mason 		push_space += this_item_size + sizeof(*item);
227034a38218SChris Mason 		if (i == 0)
227134a38218SChris Mason 			break;
227234a38218SChris Mason 		i--;
2273db94535dSChris Mason 	}
2274db94535dSChris Mason 	if (left->map_token) {
2275db94535dSChris Mason 		unmap_extent_buffer(left, left->map_token, KM_USER1);
2276db94535dSChris Mason 		left->map_token = NULL;
227700ec4c51SChris Mason 	}
22785f39d397SChris Mason 
2279925baeddSChris Mason 	if (push_items == 0)
2280925baeddSChris Mason 		goto out_unlock;
22815f39d397SChris Mason 
228234a38218SChris Mason 	if (!empty && push_items == left_nritems)
2283a429e513SChris Mason 		WARN_ON(1);
22845f39d397SChris Mason 
228500ec4c51SChris Mason 	/* push left to right */
22865f39d397SChris Mason 	right_nritems = btrfs_header_nritems(right);
228734a38218SChris Mason 
22885f39d397SChris Mason 	push_space = btrfs_item_end_nr(left, left_nritems - push_items);
2289123abc88SChris Mason 	push_space -= leaf_data_end(root, left);
22905f39d397SChris Mason 
229100ec4c51SChris Mason 	/* make room in the right data area */
22925f39d397SChris Mason 	data_end = leaf_data_end(root, right);
22935f39d397SChris Mason 	memmove_extent_buffer(right,
22945f39d397SChris Mason 			      btrfs_leaf_data(right) + data_end - push_space,
22955f39d397SChris Mason 			      btrfs_leaf_data(right) + data_end,
22965f39d397SChris Mason 			      BTRFS_LEAF_DATA_SIZE(root) - data_end);
22975f39d397SChris Mason 
229800ec4c51SChris Mason 	/* copy from the left data area */
22995f39d397SChris Mason 	copy_extent_buffer(right, left, btrfs_leaf_data(right) +
2300d6025579SChris Mason 		     BTRFS_LEAF_DATA_SIZE(root) - push_space,
2301d6025579SChris Mason 		     btrfs_leaf_data(left) + leaf_data_end(root, left),
2302d6025579SChris Mason 		     push_space);
23035f39d397SChris Mason 
23045f39d397SChris Mason 	memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
23055f39d397SChris Mason 			      btrfs_item_nr_offset(0),
23060783fcfcSChris Mason 			      right_nritems * sizeof(struct btrfs_item));
23075f39d397SChris Mason 
230800ec4c51SChris Mason 	/* copy the items from left to right */
23095f39d397SChris Mason 	copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
23105f39d397SChris Mason 		   btrfs_item_nr_offset(left_nritems - push_items),
23110783fcfcSChris Mason 		   push_items * sizeof(struct btrfs_item));
231200ec4c51SChris Mason 
231300ec4c51SChris Mason 	/* update the item pointers */
23147518a238SChris Mason 	right_nritems += push_items;
23155f39d397SChris Mason 	btrfs_set_header_nritems(right, right_nritems);
2316123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root);
23177518a238SChris Mason 	for (i = 0; i < right_nritems; i++) {
23185f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2319db94535dSChris Mason 		if (!right->map_token) {
2320db94535dSChris Mason 			map_extent_buffer(right, (unsigned long)item,
2321db94535dSChris Mason 					sizeof(struct btrfs_item),
2322db94535dSChris Mason 					&right->map_token, &right->kaddr,
2323db94535dSChris Mason 					&right->map_start, &right->map_len,
2324db94535dSChris Mason 					KM_USER1);
2325db94535dSChris Mason 		}
2326db94535dSChris Mason 		push_space -= btrfs_item_size(right, item);
2327db94535dSChris Mason 		btrfs_set_item_offset(right, item, push_space);
2328db94535dSChris Mason 	}
2329db94535dSChris Mason 
2330db94535dSChris Mason 	if (right->map_token) {
2331db94535dSChris Mason 		unmap_extent_buffer(right, right->map_token, KM_USER1);
2332db94535dSChris Mason 		right->map_token = NULL;
233300ec4c51SChris Mason 	}
23347518a238SChris Mason 	left_nritems -= push_items;
23355f39d397SChris Mason 	btrfs_set_header_nritems(left, left_nritems);
233600ec4c51SChris Mason 
233734a38218SChris Mason 	if (left_nritems)
23385f39d397SChris Mason 		btrfs_mark_buffer_dirty(left);
2339f0486c68SYan, Zheng 	else
2340f0486c68SYan, Zheng 		clean_tree_block(trans, root, left);
2341f0486c68SYan, Zheng 
23425f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
2343a429e513SChris Mason 
23445f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
23455f39d397SChris Mason 	btrfs_set_node_key(upper, &disk_key, slot + 1);
2346d6025579SChris Mason 	btrfs_mark_buffer_dirty(upper);
234702217ed2SChris Mason 
234800ec4c51SChris Mason 	/* then fixup the leaf pointer in the path */
23497518a238SChris Mason 	if (path->slots[0] >= left_nritems) {
23507518a238SChris Mason 		path->slots[0] -= left_nritems;
2351925baeddSChris Mason 		if (btrfs_header_nritems(path->nodes[0]) == 0)
2352925baeddSChris Mason 			clean_tree_block(trans, root, path->nodes[0]);
2353925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
23545f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
23555f39d397SChris Mason 		path->nodes[0] = right;
235600ec4c51SChris Mason 		path->slots[1] += 1;
235700ec4c51SChris Mason 	} else {
2358925baeddSChris Mason 		btrfs_tree_unlock(right);
23595f39d397SChris Mason 		free_extent_buffer(right);
236000ec4c51SChris Mason 	}
236100ec4c51SChris Mason 	return 0;
2362925baeddSChris Mason 
2363925baeddSChris Mason out_unlock:
2364925baeddSChris Mason 	btrfs_tree_unlock(right);
2365925baeddSChris Mason 	free_extent_buffer(right);
2366925baeddSChris Mason 	return 1;
236700ec4c51SChris Mason }
2368925baeddSChris Mason 
236900ec4c51SChris Mason /*
237044871b1bSChris Mason  * push some data in the path leaf to the right, trying to free up at
237174123bd7SChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
237244871b1bSChris Mason  *
237344871b1bSChris Mason  * returns 1 if the push failed because the other node didn't have enough
237444871b1bSChris Mason  * room, 0 if everything worked out and < 0 if there were major errors.
237599d8f83cSChris Mason  *
237699d8f83cSChris Mason  * this will push starting from min_slot to the end of the leaf.  It won't
237799d8f83cSChris Mason  * push any slot lower than min_slot
237874123bd7SChris Mason  */
237944871b1bSChris Mason static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
238099d8f83cSChris Mason 			   *root, struct btrfs_path *path,
238199d8f83cSChris Mason 			   int min_data_size, int data_size,
238299d8f83cSChris Mason 			   int empty, u32 min_slot)
2383be0e5c09SChris Mason {
238444871b1bSChris Mason 	struct extent_buffer *left = path->nodes[0];
238544871b1bSChris Mason 	struct extent_buffer *right;
238644871b1bSChris Mason 	struct extent_buffer *upper;
238744871b1bSChris Mason 	int slot;
238844871b1bSChris Mason 	int free_space;
238944871b1bSChris Mason 	u32 left_nritems;
239044871b1bSChris Mason 	int ret;
239144871b1bSChris Mason 
239244871b1bSChris Mason 	if (!path->nodes[1])
239344871b1bSChris Mason 		return 1;
239444871b1bSChris Mason 
239544871b1bSChris Mason 	slot = path->slots[1];
239644871b1bSChris Mason 	upper = path->nodes[1];
239744871b1bSChris Mason 	if (slot >= btrfs_header_nritems(upper) - 1)
239844871b1bSChris Mason 		return 1;
239944871b1bSChris Mason 
240044871b1bSChris Mason 	btrfs_assert_tree_locked(path->nodes[1]);
240144871b1bSChris Mason 
240244871b1bSChris Mason 	right = read_node_slot(root, upper, slot + 1);
240391ca338dSTsutomu Itoh 	if (right == NULL)
240491ca338dSTsutomu Itoh 		return 1;
240591ca338dSTsutomu Itoh 
240644871b1bSChris Mason 	btrfs_tree_lock(right);
240744871b1bSChris Mason 	btrfs_set_lock_blocking(right);
240844871b1bSChris Mason 
240944871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, right);
241044871b1bSChris Mason 	if (free_space < data_size)
241144871b1bSChris Mason 		goto out_unlock;
241244871b1bSChris Mason 
241344871b1bSChris Mason 	/* cow and double check */
241444871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, right, upper,
241544871b1bSChris Mason 			      slot + 1, &right);
241644871b1bSChris Mason 	if (ret)
241744871b1bSChris Mason 		goto out_unlock;
241844871b1bSChris Mason 
241944871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, right);
242044871b1bSChris Mason 	if (free_space < data_size)
242144871b1bSChris Mason 		goto out_unlock;
242244871b1bSChris Mason 
242344871b1bSChris Mason 	left_nritems = btrfs_header_nritems(left);
242444871b1bSChris Mason 	if (left_nritems == 0)
242544871b1bSChris Mason 		goto out_unlock;
242644871b1bSChris Mason 
242799d8f83cSChris Mason 	return __push_leaf_right(trans, root, path, min_data_size, empty,
242899d8f83cSChris Mason 				right, free_space, left_nritems, min_slot);
242944871b1bSChris Mason out_unlock:
243044871b1bSChris Mason 	btrfs_tree_unlock(right);
243144871b1bSChris Mason 	free_extent_buffer(right);
243244871b1bSChris Mason 	return 1;
243344871b1bSChris Mason }
243444871b1bSChris Mason 
243544871b1bSChris Mason /*
243644871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
243744871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
243899d8f83cSChris Mason  *
243999d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
244099d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us do all the
244199d8f83cSChris Mason  * items
244244871b1bSChris Mason  */
244344871b1bSChris Mason static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
244444871b1bSChris Mason 				     struct btrfs_root *root,
244544871b1bSChris Mason 				     struct btrfs_path *path, int data_size,
244644871b1bSChris Mason 				     int empty, struct extent_buffer *left,
244799d8f83cSChris Mason 				     int free_space, u32 right_nritems,
244899d8f83cSChris Mason 				     u32 max_slot)
244944871b1bSChris Mason {
24505f39d397SChris Mason 	struct btrfs_disk_key disk_key;
24515f39d397SChris Mason 	struct extent_buffer *right = path->nodes[0];
2452be0e5c09SChris Mason 	int i;
2453be0e5c09SChris Mason 	int push_space = 0;
2454be0e5c09SChris Mason 	int push_items = 0;
24550783fcfcSChris Mason 	struct btrfs_item *item;
24567518a238SChris Mason 	u32 old_left_nritems;
245734a38218SChris Mason 	u32 nr;
2458aa5d6bedSChris Mason 	int ret = 0;
2459aa5d6bedSChris Mason 	int wret;
2460db94535dSChris Mason 	u32 this_item_size;
2461db94535dSChris Mason 	u32 old_left_item_size;
2462be0e5c09SChris Mason 
246334a38218SChris Mason 	if (empty)
246499d8f83cSChris Mason 		nr = min(right_nritems, max_slot);
246534a38218SChris Mason 	else
246699d8f83cSChris Mason 		nr = min(right_nritems - 1, max_slot);
246734a38218SChris Mason 
246834a38218SChris Mason 	for (i = 0; i < nr; i++) {
24695f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2470db94535dSChris Mason 		if (!right->map_token) {
2471db94535dSChris Mason 			map_extent_buffer(right, (unsigned long)item,
2472db94535dSChris Mason 					sizeof(struct btrfs_item),
2473db94535dSChris Mason 					&right->map_token, &right->kaddr,
2474db94535dSChris Mason 					&right->map_start, &right->map_len,
2475db94535dSChris Mason 					KM_USER1);
2476db94535dSChris Mason 		}
2477db94535dSChris Mason 
247831840ae1SZheng Yan 		if (!empty && push_items > 0) {
247931840ae1SZheng Yan 			if (path->slots[0] < i)
248031840ae1SZheng Yan 				break;
248131840ae1SZheng Yan 			if (path->slots[0] == i) {
248231840ae1SZheng Yan 				int space = btrfs_leaf_free_space(root, right);
248331840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
248431840ae1SZheng Yan 					break;
248531840ae1SZheng Yan 			}
248631840ae1SZheng Yan 		}
248731840ae1SZheng Yan 
2488be0e5c09SChris Mason 		if (path->slots[0] == i)
248987b29b20SYan Zheng 			push_space += data_size;
2490db94535dSChris Mason 
2491db94535dSChris Mason 		this_item_size = btrfs_item_size(right, item);
2492db94535dSChris Mason 		if (this_item_size + sizeof(*item) + push_space > free_space)
2493be0e5c09SChris Mason 			break;
2494db94535dSChris Mason 
2495be0e5c09SChris Mason 		push_items++;
2496db94535dSChris Mason 		push_space += this_item_size + sizeof(*item);
2497be0e5c09SChris Mason 	}
2498db94535dSChris Mason 
2499db94535dSChris Mason 	if (right->map_token) {
2500db94535dSChris Mason 		unmap_extent_buffer(right, right->map_token, KM_USER1);
2501db94535dSChris Mason 		right->map_token = NULL;
2502db94535dSChris Mason 	}
2503db94535dSChris Mason 
2504be0e5c09SChris Mason 	if (push_items == 0) {
2505925baeddSChris Mason 		ret = 1;
2506925baeddSChris Mason 		goto out;
2507be0e5c09SChris Mason 	}
250834a38218SChris Mason 	if (!empty && push_items == btrfs_header_nritems(right))
2509a429e513SChris Mason 		WARN_ON(1);
25105f39d397SChris Mason 
2511be0e5c09SChris Mason 	/* push data from right to left */
25125f39d397SChris Mason 	copy_extent_buffer(left, right,
25135f39d397SChris Mason 			   btrfs_item_nr_offset(btrfs_header_nritems(left)),
25145f39d397SChris Mason 			   btrfs_item_nr_offset(0),
25155f39d397SChris Mason 			   push_items * sizeof(struct btrfs_item));
25165f39d397SChris Mason 
2517123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root) -
25185f39d397SChris Mason 		     btrfs_item_offset_nr(right, push_items - 1);
25195f39d397SChris Mason 
25205f39d397SChris Mason 	copy_extent_buffer(left, right, btrfs_leaf_data(left) +
2521d6025579SChris Mason 		     leaf_data_end(root, left) - push_space,
2522123abc88SChris Mason 		     btrfs_leaf_data(right) +
25235f39d397SChris Mason 		     btrfs_item_offset_nr(right, push_items - 1),
2524be0e5c09SChris Mason 		     push_space);
25255f39d397SChris Mason 	old_left_nritems = btrfs_header_nritems(left);
252687b29b20SYan Zheng 	BUG_ON(old_left_nritems <= 0);
2527eb60ceacSChris Mason 
2528db94535dSChris Mason 	old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
2529be0e5c09SChris Mason 	for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
25305f39d397SChris Mason 		u32 ioff;
2531db94535dSChris Mason 
25325f39d397SChris Mason 		item = btrfs_item_nr(left, i);
2533db94535dSChris Mason 		if (!left->map_token) {
2534db94535dSChris Mason 			map_extent_buffer(left, (unsigned long)item,
2535db94535dSChris Mason 					sizeof(struct btrfs_item),
2536db94535dSChris Mason 					&left->map_token, &left->kaddr,
2537db94535dSChris Mason 					&left->map_start, &left->map_len,
2538db94535dSChris Mason 					KM_USER1);
2539db94535dSChris Mason 		}
2540db94535dSChris Mason 
25415f39d397SChris Mason 		ioff = btrfs_item_offset(left, item);
25425f39d397SChris Mason 		btrfs_set_item_offset(left, item,
2543db94535dSChris Mason 		      ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size));
2544be0e5c09SChris Mason 	}
25455f39d397SChris Mason 	btrfs_set_header_nritems(left, old_left_nritems + push_items);
2546db94535dSChris Mason 	if (left->map_token) {
2547db94535dSChris Mason 		unmap_extent_buffer(left, left->map_token, KM_USER1);
2548db94535dSChris Mason 		left->map_token = NULL;
2549db94535dSChris Mason 	}
2550be0e5c09SChris Mason 
2551be0e5c09SChris Mason 	/* fixup right node */
255234a38218SChris Mason 	if (push_items > right_nritems) {
2553d397712bSChris Mason 		printk(KERN_CRIT "push items %d nr %u\n", push_items,
2554d397712bSChris Mason 		       right_nritems);
255534a38218SChris Mason 		WARN_ON(1);
255634a38218SChris Mason 	}
255734a38218SChris Mason 
255834a38218SChris Mason 	if (push_items < right_nritems) {
25595f39d397SChris Mason 		push_space = btrfs_item_offset_nr(right, push_items - 1) -
2560123abc88SChris Mason 						  leaf_data_end(root, right);
25615f39d397SChris Mason 		memmove_extent_buffer(right, btrfs_leaf_data(right) +
2562d6025579SChris Mason 				      BTRFS_LEAF_DATA_SIZE(root) - push_space,
2563d6025579SChris Mason 				      btrfs_leaf_data(right) +
2564123abc88SChris Mason 				      leaf_data_end(root, right), push_space);
25655f39d397SChris Mason 
25665f39d397SChris Mason 		memmove_extent_buffer(right, btrfs_item_nr_offset(0),
25675f39d397SChris Mason 			      btrfs_item_nr_offset(push_items),
25685f39d397SChris Mason 			     (btrfs_header_nritems(right) - push_items) *
25690783fcfcSChris Mason 			     sizeof(struct btrfs_item));
257034a38218SChris Mason 	}
2571eef1c494SYan 	right_nritems -= push_items;
2572eef1c494SYan 	btrfs_set_header_nritems(right, right_nritems);
2573123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root);
25745f39d397SChris Mason 	for (i = 0; i < right_nritems; i++) {
25755f39d397SChris Mason 		item = btrfs_item_nr(right, i);
2576db94535dSChris Mason 
2577db94535dSChris Mason 		if (!right->map_token) {
2578db94535dSChris Mason 			map_extent_buffer(right, (unsigned long)item,
2579db94535dSChris Mason 					sizeof(struct btrfs_item),
2580db94535dSChris Mason 					&right->map_token, &right->kaddr,
2581db94535dSChris Mason 					&right->map_start, &right->map_len,
2582db94535dSChris Mason 					KM_USER1);
2583db94535dSChris Mason 		}
2584db94535dSChris Mason 
2585db94535dSChris Mason 		push_space = push_space - btrfs_item_size(right, item);
2586db94535dSChris Mason 		btrfs_set_item_offset(right, item, push_space);
2587db94535dSChris Mason 	}
2588db94535dSChris Mason 	if (right->map_token) {
2589db94535dSChris Mason 		unmap_extent_buffer(right, right->map_token, KM_USER1);
2590db94535dSChris Mason 		right->map_token = NULL;
2591be0e5c09SChris Mason 	}
2592eb60ceacSChris Mason 
25935f39d397SChris Mason 	btrfs_mark_buffer_dirty(left);
259434a38218SChris Mason 	if (right_nritems)
25955f39d397SChris Mason 		btrfs_mark_buffer_dirty(right);
2596f0486c68SYan, Zheng 	else
2597f0486c68SYan, Zheng 		clean_tree_block(trans, root, right);
2598098f59c2SChris Mason 
25995f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
26005f39d397SChris Mason 	wret = fixup_low_keys(trans, root, path, &disk_key, 1);
2601aa5d6bedSChris Mason 	if (wret)
2602aa5d6bedSChris Mason 		ret = wret;
2603be0e5c09SChris Mason 
2604be0e5c09SChris Mason 	/* then fixup the leaf pointer in the path */
2605be0e5c09SChris Mason 	if (path->slots[0] < push_items) {
2606be0e5c09SChris Mason 		path->slots[0] += old_left_nritems;
2607925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
26085f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
26095f39d397SChris Mason 		path->nodes[0] = left;
2610be0e5c09SChris Mason 		path->slots[1] -= 1;
2611be0e5c09SChris Mason 	} else {
2612925baeddSChris Mason 		btrfs_tree_unlock(left);
26135f39d397SChris Mason 		free_extent_buffer(left);
2614be0e5c09SChris Mason 		path->slots[0] -= push_items;
2615be0e5c09SChris Mason 	}
2616eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
2617aa5d6bedSChris Mason 	return ret;
2618925baeddSChris Mason out:
2619925baeddSChris Mason 	btrfs_tree_unlock(left);
2620925baeddSChris Mason 	free_extent_buffer(left);
2621925baeddSChris Mason 	return ret;
2622be0e5c09SChris Mason }
2623be0e5c09SChris Mason 
262474123bd7SChris Mason /*
262544871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
262644871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
262799d8f83cSChris Mason  *
262899d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
262999d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us push all the
263099d8f83cSChris Mason  * items
263144871b1bSChris Mason  */
263244871b1bSChris Mason static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
263399d8f83cSChris Mason 			  *root, struct btrfs_path *path, int min_data_size,
263499d8f83cSChris Mason 			  int data_size, int empty, u32 max_slot)
263544871b1bSChris Mason {
263644871b1bSChris Mason 	struct extent_buffer *right = path->nodes[0];
263744871b1bSChris Mason 	struct extent_buffer *left;
263844871b1bSChris Mason 	int slot;
263944871b1bSChris Mason 	int free_space;
264044871b1bSChris Mason 	u32 right_nritems;
264144871b1bSChris Mason 	int ret = 0;
264244871b1bSChris Mason 
264344871b1bSChris Mason 	slot = path->slots[1];
264444871b1bSChris Mason 	if (slot == 0)
264544871b1bSChris Mason 		return 1;
264644871b1bSChris Mason 	if (!path->nodes[1])
264744871b1bSChris Mason 		return 1;
264844871b1bSChris Mason 
264944871b1bSChris Mason 	right_nritems = btrfs_header_nritems(right);
265044871b1bSChris Mason 	if (right_nritems == 0)
265144871b1bSChris Mason 		return 1;
265244871b1bSChris Mason 
265344871b1bSChris Mason 	btrfs_assert_tree_locked(path->nodes[1]);
265444871b1bSChris Mason 
265544871b1bSChris Mason 	left = read_node_slot(root, path->nodes[1], slot - 1);
265691ca338dSTsutomu Itoh 	if (left == NULL)
265791ca338dSTsutomu Itoh 		return 1;
265891ca338dSTsutomu Itoh 
265944871b1bSChris Mason 	btrfs_tree_lock(left);
266044871b1bSChris Mason 	btrfs_set_lock_blocking(left);
266144871b1bSChris Mason 
266244871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, left);
266344871b1bSChris Mason 	if (free_space < data_size) {
266444871b1bSChris Mason 		ret = 1;
266544871b1bSChris Mason 		goto out;
266644871b1bSChris Mason 	}
266744871b1bSChris Mason 
266844871b1bSChris Mason 	/* cow and double check */
266944871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, left,
267044871b1bSChris Mason 			      path->nodes[1], slot - 1, &left);
267144871b1bSChris Mason 	if (ret) {
267244871b1bSChris Mason 		/* we hit -ENOSPC, but it isn't fatal here */
267344871b1bSChris Mason 		ret = 1;
267444871b1bSChris Mason 		goto out;
267544871b1bSChris Mason 	}
267644871b1bSChris Mason 
267744871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, left);
267844871b1bSChris Mason 	if (free_space < data_size) {
267944871b1bSChris Mason 		ret = 1;
268044871b1bSChris Mason 		goto out;
268144871b1bSChris Mason 	}
268244871b1bSChris Mason 
268399d8f83cSChris Mason 	return __push_leaf_left(trans, root, path, min_data_size,
268499d8f83cSChris Mason 			       empty, left, free_space, right_nritems,
268599d8f83cSChris Mason 			       max_slot);
268644871b1bSChris Mason out:
268744871b1bSChris Mason 	btrfs_tree_unlock(left);
268844871b1bSChris Mason 	free_extent_buffer(left);
268944871b1bSChris Mason 	return ret;
269044871b1bSChris Mason }
269144871b1bSChris Mason 
269244871b1bSChris Mason /*
269374123bd7SChris Mason  * split the path's leaf in two, making sure there is at least data_size
269474123bd7SChris Mason  * available for the resulting leaf level of the path.
2695aa5d6bedSChris Mason  *
2696aa5d6bedSChris Mason  * returns 0 if all went well and < 0 on failure.
269774123bd7SChris Mason  */
269844871b1bSChris Mason static noinline int copy_for_split(struct btrfs_trans_handle *trans,
2699e02119d5SChris Mason 			       struct btrfs_root *root,
270044871b1bSChris Mason 			       struct btrfs_path *path,
270144871b1bSChris Mason 			       struct extent_buffer *l,
270244871b1bSChris Mason 			       struct extent_buffer *right,
270344871b1bSChris Mason 			       int slot, int mid, int nritems)
2704be0e5c09SChris Mason {
2705be0e5c09SChris Mason 	int data_copy_size;
2706be0e5c09SChris Mason 	int rt_data_off;
2707be0e5c09SChris Mason 	int i;
2708d4dbff95SChris Mason 	int ret = 0;
2709aa5d6bedSChris Mason 	int wret;
2710d4dbff95SChris Mason 	struct btrfs_disk_key disk_key;
2711be0e5c09SChris Mason 
27125f39d397SChris Mason 	nritems = nritems - mid;
27135f39d397SChris Mason 	btrfs_set_header_nritems(right, nritems);
27145f39d397SChris Mason 	data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
27155f39d397SChris Mason 
27165f39d397SChris Mason 	copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
27175f39d397SChris Mason 			   btrfs_item_nr_offset(mid),
27185f39d397SChris Mason 			   nritems * sizeof(struct btrfs_item));
27195f39d397SChris Mason 
27205f39d397SChris Mason 	copy_extent_buffer(right, l,
2721d6025579SChris Mason 		     btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
2722123abc88SChris Mason 		     data_copy_size, btrfs_leaf_data(l) +
2723123abc88SChris Mason 		     leaf_data_end(root, l), data_copy_size);
272474123bd7SChris Mason 
27255f39d397SChris Mason 	rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
27265f39d397SChris Mason 		      btrfs_item_end_nr(l, mid);
27275f39d397SChris Mason 
27285f39d397SChris Mason 	for (i = 0; i < nritems; i++) {
27295f39d397SChris Mason 		struct btrfs_item *item = btrfs_item_nr(right, i);
2730db94535dSChris Mason 		u32 ioff;
2731db94535dSChris Mason 
2732db94535dSChris Mason 		if (!right->map_token) {
2733db94535dSChris Mason 			map_extent_buffer(right, (unsigned long)item,
2734db94535dSChris Mason 					sizeof(struct btrfs_item),
2735db94535dSChris Mason 					&right->map_token, &right->kaddr,
2736db94535dSChris Mason 					&right->map_start, &right->map_len,
2737db94535dSChris Mason 					KM_USER1);
2738db94535dSChris Mason 		}
2739db94535dSChris Mason 
2740db94535dSChris Mason 		ioff = btrfs_item_offset(right, item);
27415f39d397SChris Mason 		btrfs_set_item_offset(right, item, ioff + rt_data_off);
27420783fcfcSChris Mason 	}
274374123bd7SChris Mason 
2744db94535dSChris Mason 	if (right->map_token) {
2745db94535dSChris Mason 		unmap_extent_buffer(right, right->map_token, KM_USER1);
2746db94535dSChris Mason 		right->map_token = NULL;
2747db94535dSChris Mason 	}
2748db94535dSChris Mason 
27495f39d397SChris Mason 	btrfs_set_header_nritems(l, mid);
2750aa5d6bedSChris Mason 	ret = 0;
27515f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
2752db94535dSChris Mason 	wret = insert_ptr(trans, root, path, &disk_key, right->start,
2753db94535dSChris Mason 			  path->slots[1] + 1, 1);
2754aa5d6bedSChris Mason 	if (wret)
2755aa5d6bedSChris Mason 		ret = wret;
27565f39d397SChris Mason 
27575f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
27585f39d397SChris Mason 	btrfs_mark_buffer_dirty(l);
2759eb60ceacSChris Mason 	BUG_ON(path->slots[0] != slot);
27605f39d397SChris Mason 
2761be0e5c09SChris Mason 	if (mid <= slot) {
2762925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
27635f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
27645f39d397SChris Mason 		path->nodes[0] = right;
2765be0e5c09SChris Mason 		path->slots[0] -= mid;
2766be0e5c09SChris Mason 		path->slots[1] += 1;
2767925baeddSChris Mason 	} else {
2768925baeddSChris Mason 		btrfs_tree_unlock(right);
27695f39d397SChris Mason 		free_extent_buffer(right);
2770925baeddSChris Mason 	}
27715f39d397SChris Mason 
2772eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
2773d4dbff95SChris Mason 
277444871b1bSChris Mason 	return ret;
277544871b1bSChris Mason }
277644871b1bSChris Mason 
277744871b1bSChris Mason /*
277899d8f83cSChris Mason  * double splits happen when we need to insert a big item in the middle
277999d8f83cSChris Mason  * of a leaf.  A double split can leave us with 3 mostly empty leaves:
278099d8f83cSChris Mason  * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
278199d8f83cSChris Mason  *          A                 B                 C
278299d8f83cSChris Mason  *
278399d8f83cSChris Mason  * We avoid this by trying to push the items on either side of our target
278499d8f83cSChris Mason  * into the adjacent leaves.  If all goes well we can avoid the double split
278599d8f83cSChris Mason  * completely.
278699d8f83cSChris Mason  */
278799d8f83cSChris Mason static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
278899d8f83cSChris Mason 					  struct btrfs_root *root,
278999d8f83cSChris Mason 					  struct btrfs_path *path,
279099d8f83cSChris Mason 					  int data_size)
279199d8f83cSChris Mason {
279299d8f83cSChris Mason 	int ret;
279399d8f83cSChris Mason 	int progress = 0;
279499d8f83cSChris Mason 	int slot;
279599d8f83cSChris Mason 	u32 nritems;
279699d8f83cSChris Mason 
279799d8f83cSChris Mason 	slot = path->slots[0];
279899d8f83cSChris Mason 
279999d8f83cSChris Mason 	/*
280099d8f83cSChris Mason 	 * try to push all the items after our slot into the
280199d8f83cSChris Mason 	 * right leaf
280299d8f83cSChris Mason 	 */
280399d8f83cSChris Mason 	ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
280499d8f83cSChris Mason 	if (ret < 0)
280599d8f83cSChris Mason 		return ret;
280699d8f83cSChris Mason 
280799d8f83cSChris Mason 	if (ret == 0)
280899d8f83cSChris Mason 		progress++;
280999d8f83cSChris Mason 
281099d8f83cSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
281199d8f83cSChris Mason 	/*
281299d8f83cSChris Mason 	 * our goal is to get our slot at the start or end of a leaf.  If
281399d8f83cSChris Mason 	 * we've done so we're done
281499d8f83cSChris Mason 	 */
281599d8f83cSChris Mason 	if (path->slots[0] == 0 || path->slots[0] == nritems)
281699d8f83cSChris Mason 		return 0;
281799d8f83cSChris Mason 
281899d8f83cSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
281999d8f83cSChris Mason 		return 0;
282099d8f83cSChris Mason 
282199d8f83cSChris Mason 	/* try to push all the items before our slot into the next leaf */
282299d8f83cSChris Mason 	slot = path->slots[0];
282399d8f83cSChris Mason 	ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
282499d8f83cSChris Mason 	if (ret < 0)
282599d8f83cSChris Mason 		return ret;
282699d8f83cSChris Mason 
282799d8f83cSChris Mason 	if (ret == 0)
282899d8f83cSChris Mason 		progress++;
282999d8f83cSChris Mason 
283099d8f83cSChris Mason 	if (progress)
283199d8f83cSChris Mason 		return 0;
283299d8f83cSChris Mason 	return 1;
283399d8f83cSChris Mason }
283499d8f83cSChris Mason 
283599d8f83cSChris Mason /*
283644871b1bSChris Mason  * split the path's leaf in two, making sure there is at least data_size
283744871b1bSChris Mason  * available for the resulting leaf level of the path.
283844871b1bSChris Mason  *
283944871b1bSChris Mason  * returns 0 if all went well and < 0 on failure.
284044871b1bSChris Mason  */
284144871b1bSChris Mason static noinline int split_leaf(struct btrfs_trans_handle *trans,
284244871b1bSChris Mason 			       struct btrfs_root *root,
284344871b1bSChris Mason 			       struct btrfs_key *ins_key,
284444871b1bSChris Mason 			       struct btrfs_path *path, int data_size,
284544871b1bSChris Mason 			       int extend)
284644871b1bSChris Mason {
28475d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
284844871b1bSChris Mason 	struct extent_buffer *l;
284944871b1bSChris Mason 	u32 nritems;
285044871b1bSChris Mason 	int mid;
285144871b1bSChris Mason 	int slot;
285244871b1bSChris Mason 	struct extent_buffer *right;
285344871b1bSChris Mason 	int ret = 0;
285444871b1bSChris Mason 	int wret;
28555d4f98a2SYan Zheng 	int split;
285644871b1bSChris Mason 	int num_doubles = 0;
285799d8f83cSChris Mason 	int tried_avoid_double = 0;
285844871b1bSChris Mason 
2859a5719521SYan, Zheng 	l = path->nodes[0];
2860a5719521SYan, Zheng 	slot = path->slots[0];
2861a5719521SYan, Zheng 	if (extend && data_size + btrfs_item_size_nr(l, slot) +
2862a5719521SYan, Zheng 	    sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
2863a5719521SYan, Zheng 		return -EOVERFLOW;
2864a5719521SYan, Zheng 
286544871b1bSChris Mason 	/* first try to make some room by pushing left and right */
286699d8f83cSChris Mason 	if (data_size) {
286799d8f83cSChris Mason 		wret = push_leaf_right(trans, root, path, data_size,
286899d8f83cSChris Mason 				       data_size, 0, 0);
286944871b1bSChris Mason 		if (wret < 0)
287044871b1bSChris Mason 			return wret;
287144871b1bSChris Mason 		if (wret) {
287299d8f83cSChris Mason 			wret = push_leaf_left(trans, root, path, data_size,
287399d8f83cSChris Mason 					      data_size, 0, (u32)-1);
287444871b1bSChris Mason 			if (wret < 0)
287544871b1bSChris Mason 				return wret;
287644871b1bSChris Mason 		}
287744871b1bSChris Mason 		l = path->nodes[0];
287844871b1bSChris Mason 
287944871b1bSChris Mason 		/* did the pushes work? */
288044871b1bSChris Mason 		if (btrfs_leaf_free_space(root, l) >= data_size)
288144871b1bSChris Mason 			return 0;
288244871b1bSChris Mason 	}
288344871b1bSChris Mason 
288444871b1bSChris Mason 	if (!path->nodes[1]) {
288544871b1bSChris Mason 		ret = insert_new_root(trans, root, path, 1);
288644871b1bSChris Mason 		if (ret)
288744871b1bSChris Mason 			return ret;
288844871b1bSChris Mason 	}
288944871b1bSChris Mason again:
28905d4f98a2SYan Zheng 	split = 1;
289144871b1bSChris Mason 	l = path->nodes[0];
289244871b1bSChris Mason 	slot = path->slots[0];
289344871b1bSChris Mason 	nritems = btrfs_header_nritems(l);
289444871b1bSChris Mason 	mid = (nritems + 1) / 2;
289544871b1bSChris Mason 
28965d4f98a2SYan Zheng 	if (mid <= slot) {
28975d4f98a2SYan Zheng 		if (nritems == 1 ||
28985d4f98a2SYan Zheng 		    leaf_space_used(l, mid, nritems - mid) + data_size >
28995d4f98a2SYan Zheng 			BTRFS_LEAF_DATA_SIZE(root)) {
29005d4f98a2SYan Zheng 			if (slot >= nritems) {
29015d4f98a2SYan Zheng 				split = 0;
29025d4f98a2SYan Zheng 			} else {
29035d4f98a2SYan Zheng 				mid = slot;
29045d4f98a2SYan Zheng 				if (mid != nritems &&
29055d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
29065d4f98a2SYan Zheng 				    data_size > BTRFS_LEAF_DATA_SIZE(root)) {
290799d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
290899d8f83cSChris Mason 						goto push_for_double;
29095d4f98a2SYan Zheng 					split = 2;
29105d4f98a2SYan Zheng 				}
29115d4f98a2SYan Zheng 			}
29125d4f98a2SYan Zheng 		}
29135d4f98a2SYan Zheng 	} else {
29145d4f98a2SYan Zheng 		if (leaf_space_used(l, 0, mid) + data_size >
29155d4f98a2SYan Zheng 			BTRFS_LEAF_DATA_SIZE(root)) {
29165d4f98a2SYan Zheng 			if (!extend && data_size && slot == 0) {
29175d4f98a2SYan Zheng 				split = 0;
29185d4f98a2SYan Zheng 			} else if ((extend || !data_size) && slot == 0) {
29195d4f98a2SYan Zheng 				mid = 1;
29205d4f98a2SYan Zheng 			} else {
29215d4f98a2SYan Zheng 				mid = slot;
29225d4f98a2SYan Zheng 				if (mid != nritems &&
29235d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
29245d4f98a2SYan Zheng 				    data_size > BTRFS_LEAF_DATA_SIZE(root)) {
292599d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
292699d8f83cSChris Mason 						goto push_for_double;
29275d4f98a2SYan Zheng 					split = 2 ;
29285d4f98a2SYan Zheng 				}
29295d4f98a2SYan Zheng 			}
29305d4f98a2SYan Zheng 		}
29315d4f98a2SYan Zheng 	}
29325d4f98a2SYan Zheng 
29335d4f98a2SYan Zheng 	if (split == 0)
29345d4f98a2SYan Zheng 		btrfs_cpu_key_to_disk(&disk_key, ins_key);
29355d4f98a2SYan Zheng 	else
29365d4f98a2SYan Zheng 		btrfs_item_key(l, &disk_key, mid);
29375d4f98a2SYan Zheng 
29385d4f98a2SYan Zheng 	right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
293944871b1bSChris Mason 					root->root_key.objectid,
29405d4f98a2SYan Zheng 					&disk_key, 0, l->start, 0);
2941f0486c68SYan, Zheng 	if (IS_ERR(right))
294244871b1bSChris Mason 		return PTR_ERR(right);
2943f0486c68SYan, Zheng 
2944f0486c68SYan, Zheng 	root_add_used(root, root->leafsize);
294544871b1bSChris Mason 
294644871b1bSChris Mason 	memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
294744871b1bSChris Mason 	btrfs_set_header_bytenr(right, right->start);
294844871b1bSChris Mason 	btrfs_set_header_generation(right, trans->transid);
29495d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
295044871b1bSChris Mason 	btrfs_set_header_owner(right, root->root_key.objectid);
295144871b1bSChris Mason 	btrfs_set_header_level(right, 0);
295244871b1bSChris Mason 	write_extent_buffer(right, root->fs_info->fsid,
295344871b1bSChris Mason 			    (unsigned long)btrfs_header_fsid(right),
295444871b1bSChris Mason 			    BTRFS_FSID_SIZE);
295544871b1bSChris Mason 
295644871b1bSChris Mason 	write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
295744871b1bSChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(right),
295844871b1bSChris Mason 			    BTRFS_UUID_SIZE);
295944871b1bSChris Mason 
29605d4f98a2SYan Zheng 	if (split == 0) {
296144871b1bSChris Mason 		if (mid <= slot) {
296244871b1bSChris Mason 			btrfs_set_header_nritems(right, 0);
296344871b1bSChris Mason 			wret = insert_ptr(trans, root, path,
296444871b1bSChris Mason 					  &disk_key, right->start,
296544871b1bSChris Mason 					  path->slots[1] + 1, 1);
296644871b1bSChris Mason 			if (wret)
296744871b1bSChris Mason 				ret = wret;
296844871b1bSChris Mason 
296944871b1bSChris Mason 			btrfs_tree_unlock(path->nodes[0]);
297044871b1bSChris Mason 			free_extent_buffer(path->nodes[0]);
297144871b1bSChris Mason 			path->nodes[0] = right;
297244871b1bSChris Mason 			path->slots[0] = 0;
297344871b1bSChris Mason 			path->slots[1] += 1;
297444871b1bSChris Mason 		} else {
297544871b1bSChris Mason 			btrfs_set_header_nritems(right, 0);
297644871b1bSChris Mason 			wret = insert_ptr(trans, root, path,
297744871b1bSChris Mason 					  &disk_key,
297844871b1bSChris Mason 					  right->start,
297944871b1bSChris Mason 					  path->slots[1], 1);
298044871b1bSChris Mason 			if (wret)
298144871b1bSChris Mason 				ret = wret;
298244871b1bSChris Mason 			btrfs_tree_unlock(path->nodes[0]);
298344871b1bSChris Mason 			free_extent_buffer(path->nodes[0]);
298444871b1bSChris Mason 			path->nodes[0] = right;
298544871b1bSChris Mason 			path->slots[0] = 0;
298644871b1bSChris Mason 			if (path->slots[1] == 0) {
298744871b1bSChris Mason 				wret = fixup_low_keys(trans, root,
298844871b1bSChris Mason 						path, &disk_key, 1);
298944871b1bSChris Mason 				if (wret)
299044871b1bSChris Mason 					ret = wret;
299144871b1bSChris Mason 			}
29925d4f98a2SYan Zheng 		}
299344871b1bSChris Mason 		btrfs_mark_buffer_dirty(right);
299444871b1bSChris Mason 		return ret;
299544871b1bSChris Mason 	}
299644871b1bSChris Mason 
299744871b1bSChris Mason 	ret = copy_for_split(trans, root, path, l, right, slot, mid, nritems);
299844871b1bSChris Mason 	BUG_ON(ret);
299944871b1bSChris Mason 
30005d4f98a2SYan Zheng 	if (split == 2) {
3001cc0c5538SChris Mason 		BUG_ON(num_doubles != 0);
3002cc0c5538SChris Mason 		num_doubles++;
3003cc0c5538SChris Mason 		goto again;
30043326d1b0SChris Mason 	}
300544871b1bSChris Mason 
3006be0e5c09SChris Mason 	return ret;
300799d8f83cSChris Mason 
300899d8f83cSChris Mason push_for_double:
300999d8f83cSChris Mason 	push_for_double_split(trans, root, path, data_size);
301099d8f83cSChris Mason 	tried_avoid_double = 1;
301199d8f83cSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
301299d8f83cSChris Mason 		return 0;
301399d8f83cSChris Mason 	goto again;
3014be0e5c09SChris Mason }
3015be0e5c09SChris Mason 
3016ad48fd75SYan, Zheng static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3017ad48fd75SYan, Zheng 					 struct btrfs_root *root,
3018ad48fd75SYan, Zheng 					 struct btrfs_path *path, int ins_len)
3019ad48fd75SYan, Zheng {
3020ad48fd75SYan, Zheng 	struct btrfs_key key;
3021ad48fd75SYan, Zheng 	struct extent_buffer *leaf;
3022ad48fd75SYan, Zheng 	struct btrfs_file_extent_item *fi;
3023ad48fd75SYan, Zheng 	u64 extent_len = 0;
3024ad48fd75SYan, Zheng 	u32 item_size;
3025ad48fd75SYan, Zheng 	int ret;
3026ad48fd75SYan, Zheng 
3027ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3028ad48fd75SYan, Zheng 	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3029ad48fd75SYan, Zheng 
3030ad48fd75SYan, Zheng 	BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3031ad48fd75SYan, Zheng 	       key.type != BTRFS_EXTENT_CSUM_KEY);
3032ad48fd75SYan, Zheng 
3033ad48fd75SYan, Zheng 	if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3034ad48fd75SYan, Zheng 		return 0;
3035ad48fd75SYan, Zheng 
3036ad48fd75SYan, Zheng 	item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3037ad48fd75SYan, Zheng 	if (key.type == BTRFS_EXTENT_DATA_KEY) {
3038ad48fd75SYan, Zheng 		fi = btrfs_item_ptr(leaf, path->slots[0],
3039ad48fd75SYan, Zheng 				    struct btrfs_file_extent_item);
3040ad48fd75SYan, Zheng 		extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3041ad48fd75SYan, Zheng 	}
3042b3b4aa74SDavid Sterba 	btrfs_release_path(path);
3043ad48fd75SYan, Zheng 
3044ad48fd75SYan, Zheng 	path->keep_locks = 1;
3045ad48fd75SYan, Zheng 	path->search_for_split = 1;
3046ad48fd75SYan, Zheng 	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3047ad48fd75SYan, Zheng 	path->search_for_split = 0;
3048ad48fd75SYan, Zheng 	if (ret < 0)
3049ad48fd75SYan, Zheng 		goto err;
3050ad48fd75SYan, Zheng 
3051ad48fd75SYan, Zheng 	ret = -EAGAIN;
3052ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3053ad48fd75SYan, Zheng 	/* if our item isn't there or got smaller, return now */
3054ad48fd75SYan, Zheng 	if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3055ad48fd75SYan, Zheng 		goto err;
3056ad48fd75SYan, Zheng 
3057109f6aefSChris Mason 	/* the leaf has  changed, it now has room.  return now */
3058109f6aefSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3059109f6aefSChris Mason 		goto err;
3060109f6aefSChris Mason 
3061ad48fd75SYan, Zheng 	if (key.type == BTRFS_EXTENT_DATA_KEY) {
3062ad48fd75SYan, Zheng 		fi = btrfs_item_ptr(leaf, path->slots[0],
3063ad48fd75SYan, Zheng 				    struct btrfs_file_extent_item);
3064ad48fd75SYan, Zheng 		if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3065ad48fd75SYan, Zheng 			goto err;
3066ad48fd75SYan, Zheng 	}
3067ad48fd75SYan, Zheng 
3068ad48fd75SYan, Zheng 	btrfs_set_path_blocking(path);
3069ad48fd75SYan, Zheng 	ret = split_leaf(trans, root, &key, path, ins_len, 1);
3070f0486c68SYan, Zheng 	if (ret)
3071f0486c68SYan, Zheng 		goto err;
3072ad48fd75SYan, Zheng 
3073ad48fd75SYan, Zheng 	path->keep_locks = 0;
3074ad48fd75SYan, Zheng 	btrfs_unlock_up_safe(path, 1);
3075ad48fd75SYan, Zheng 	return 0;
3076ad48fd75SYan, Zheng err:
3077ad48fd75SYan, Zheng 	path->keep_locks = 0;
3078ad48fd75SYan, Zheng 	return ret;
3079ad48fd75SYan, Zheng }
3080ad48fd75SYan, Zheng 
3081ad48fd75SYan, Zheng static noinline int split_item(struct btrfs_trans_handle *trans,
3082459931ecSChris Mason 			       struct btrfs_root *root,
3083459931ecSChris Mason 			       struct btrfs_path *path,
3084459931ecSChris Mason 			       struct btrfs_key *new_key,
3085459931ecSChris Mason 			       unsigned long split_offset)
3086459931ecSChris Mason {
3087459931ecSChris Mason 	struct extent_buffer *leaf;
3088459931ecSChris Mason 	struct btrfs_item *item;
3089459931ecSChris Mason 	struct btrfs_item *new_item;
3090459931ecSChris Mason 	int slot;
3091ad48fd75SYan, Zheng 	char *buf;
3092459931ecSChris Mason 	u32 nritems;
3093ad48fd75SYan, Zheng 	u32 item_size;
3094459931ecSChris Mason 	u32 orig_offset;
3095459931ecSChris Mason 	struct btrfs_disk_key disk_key;
3096459931ecSChris Mason 
3097459931ecSChris Mason 	leaf = path->nodes[0];
3098b9473439SChris Mason 	BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3099b9473439SChris Mason 
3100b4ce94deSChris Mason 	btrfs_set_path_blocking(path);
3101b4ce94deSChris Mason 
3102459931ecSChris Mason 	item = btrfs_item_nr(leaf, path->slots[0]);
3103459931ecSChris Mason 	orig_offset = btrfs_item_offset(leaf, item);
3104459931ecSChris Mason 	item_size = btrfs_item_size(leaf, item);
3105459931ecSChris Mason 
3106459931ecSChris Mason 	buf = kmalloc(item_size, GFP_NOFS);
3107ad48fd75SYan, Zheng 	if (!buf)
3108ad48fd75SYan, Zheng 		return -ENOMEM;
3109ad48fd75SYan, Zheng 
3110459931ecSChris Mason 	read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3111459931ecSChris Mason 			    path->slots[0]), item_size);
3112ad48fd75SYan, Zheng 
3113459931ecSChris Mason 	slot = path->slots[0] + 1;
3114459931ecSChris Mason 	nritems = btrfs_header_nritems(leaf);
3115459931ecSChris Mason 	if (slot != nritems) {
3116459931ecSChris Mason 		/* shift the items */
3117459931ecSChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
3118459931ecSChris Mason 				btrfs_item_nr_offset(slot),
3119459931ecSChris Mason 				(nritems - slot) * sizeof(struct btrfs_item));
3120459931ecSChris Mason 	}
3121459931ecSChris Mason 
3122459931ecSChris Mason 	btrfs_cpu_key_to_disk(&disk_key, new_key);
3123459931ecSChris Mason 	btrfs_set_item_key(leaf, &disk_key, slot);
3124459931ecSChris Mason 
3125459931ecSChris Mason 	new_item = btrfs_item_nr(leaf, slot);
3126459931ecSChris Mason 
3127459931ecSChris Mason 	btrfs_set_item_offset(leaf, new_item, orig_offset);
3128459931ecSChris Mason 	btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3129459931ecSChris Mason 
3130459931ecSChris Mason 	btrfs_set_item_offset(leaf, item,
3131459931ecSChris Mason 			      orig_offset + item_size - split_offset);
3132459931ecSChris Mason 	btrfs_set_item_size(leaf, item, split_offset);
3133459931ecSChris Mason 
3134459931ecSChris Mason 	btrfs_set_header_nritems(leaf, nritems + 1);
3135459931ecSChris Mason 
3136459931ecSChris Mason 	/* write the data for the start of the original item */
3137459931ecSChris Mason 	write_extent_buffer(leaf, buf,
3138459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, path->slots[0]),
3139459931ecSChris Mason 			    split_offset);
3140459931ecSChris Mason 
3141459931ecSChris Mason 	/* write the data for the new item */
3142459931ecSChris Mason 	write_extent_buffer(leaf, buf + split_offset,
3143459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, slot),
3144459931ecSChris Mason 			    item_size - split_offset);
3145459931ecSChris Mason 	btrfs_mark_buffer_dirty(leaf);
3146459931ecSChris Mason 
3147ad48fd75SYan, Zheng 	BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
3148459931ecSChris Mason 	kfree(buf);
3149ad48fd75SYan, Zheng 	return 0;
3150ad48fd75SYan, Zheng }
3151ad48fd75SYan, Zheng 
3152ad48fd75SYan, Zheng /*
3153ad48fd75SYan, Zheng  * This function splits a single item into two items,
3154ad48fd75SYan, Zheng  * giving 'new_key' to the new item and splitting the
3155ad48fd75SYan, Zheng  * old one at split_offset (from the start of the item).
3156ad48fd75SYan, Zheng  *
3157ad48fd75SYan, Zheng  * The path may be released by this operation.  After
3158ad48fd75SYan, Zheng  * the split, the path is pointing to the old item.  The
3159ad48fd75SYan, Zheng  * new item is going to be in the same node as the old one.
3160ad48fd75SYan, Zheng  *
3161ad48fd75SYan, Zheng  * Note, the item being split must be smaller enough to live alone on
3162ad48fd75SYan, Zheng  * a tree block with room for one extra struct btrfs_item
3163ad48fd75SYan, Zheng  *
3164ad48fd75SYan, Zheng  * This allows us to split the item in place, keeping a lock on the
3165ad48fd75SYan, Zheng  * leaf the entire time.
3166ad48fd75SYan, Zheng  */
3167ad48fd75SYan, Zheng int btrfs_split_item(struct btrfs_trans_handle *trans,
3168ad48fd75SYan, Zheng 		     struct btrfs_root *root,
3169ad48fd75SYan, Zheng 		     struct btrfs_path *path,
3170ad48fd75SYan, Zheng 		     struct btrfs_key *new_key,
3171ad48fd75SYan, Zheng 		     unsigned long split_offset)
3172ad48fd75SYan, Zheng {
3173ad48fd75SYan, Zheng 	int ret;
3174ad48fd75SYan, Zheng 	ret = setup_leaf_for_split(trans, root, path,
3175ad48fd75SYan, Zheng 				   sizeof(struct btrfs_item));
3176ad48fd75SYan, Zheng 	if (ret)
3177459931ecSChris Mason 		return ret;
3178ad48fd75SYan, Zheng 
3179ad48fd75SYan, Zheng 	ret = split_item(trans, root, path, new_key, split_offset);
3180ad48fd75SYan, Zheng 	return ret;
3181ad48fd75SYan, Zheng }
3182ad48fd75SYan, Zheng 
3183ad48fd75SYan, Zheng /*
3184ad48fd75SYan, Zheng  * This function duplicate a item, giving 'new_key' to the new item.
3185ad48fd75SYan, Zheng  * It guarantees both items live in the same tree leaf and the new item
3186ad48fd75SYan, Zheng  * is contiguous with the original item.
3187ad48fd75SYan, Zheng  *
3188ad48fd75SYan, Zheng  * This allows us to split file extent in place, keeping a lock on the
3189ad48fd75SYan, Zheng  * leaf the entire time.
3190ad48fd75SYan, Zheng  */
3191ad48fd75SYan, Zheng int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3192ad48fd75SYan, Zheng 			 struct btrfs_root *root,
3193ad48fd75SYan, Zheng 			 struct btrfs_path *path,
3194ad48fd75SYan, Zheng 			 struct btrfs_key *new_key)
3195ad48fd75SYan, Zheng {
3196ad48fd75SYan, Zheng 	struct extent_buffer *leaf;
3197ad48fd75SYan, Zheng 	int ret;
3198ad48fd75SYan, Zheng 	u32 item_size;
3199ad48fd75SYan, Zheng 
3200ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3201ad48fd75SYan, Zheng 	item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3202ad48fd75SYan, Zheng 	ret = setup_leaf_for_split(trans, root, path,
3203ad48fd75SYan, Zheng 				   item_size + sizeof(struct btrfs_item));
3204ad48fd75SYan, Zheng 	if (ret)
3205ad48fd75SYan, Zheng 		return ret;
3206ad48fd75SYan, Zheng 
3207ad48fd75SYan, Zheng 	path->slots[0]++;
3208ad48fd75SYan, Zheng 	ret = setup_items_for_insert(trans, root, path, new_key, &item_size,
3209ad48fd75SYan, Zheng 				     item_size, item_size +
3210ad48fd75SYan, Zheng 				     sizeof(struct btrfs_item), 1);
3211ad48fd75SYan, Zheng 	BUG_ON(ret);
3212ad48fd75SYan, Zheng 
3213ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3214ad48fd75SYan, Zheng 	memcpy_extent_buffer(leaf,
3215ad48fd75SYan, Zheng 			     btrfs_item_ptr_offset(leaf, path->slots[0]),
3216ad48fd75SYan, Zheng 			     btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3217ad48fd75SYan, Zheng 			     item_size);
3218ad48fd75SYan, Zheng 	return 0;
3219459931ecSChris Mason }
3220459931ecSChris Mason 
3221459931ecSChris Mason /*
3222d352ac68SChris Mason  * make the item pointed to by the path smaller.  new_size indicates
3223d352ac68SChris Mason  * how small to make it, and from_end tells us if we just chop bytes
3224d352ac68SChris Mason  * off the end of the item or if we shift the item to chop bytes off
3225d352ac68SChris Mason  * the front.
3226d352ac68SChris Mason  */
3227b18c6685SChris Mason int btrfs_truncate_item(struct btrfs_trans_handle *trans,
3228b18c6685SChris Mason 			struct btrfs_root *root,
3229b18c6685SChris Mason 			struct btrfs_path *path,
3230179e29e4SChris Mason 			u32 new_size, int from_end)
3231b18c6685SChris Mason {
3232b18c6685SChris Mason 	int slot;
32335f39d397SChris Mason 	struct extent_buffer *leaf;
32345f39d397SChris Mason 	struct btrfs_item *item;
3235b18c6685SChris Mason 	u32 nritems;
3236b18c6685SChris Mason 	unsigned int data_end;
3237b18c6685SChris Mason 	unsigned int old_data_start;
3238b18c6685SChris Mason 	unsigned int old_size;
3239b18c6685SChris Mason 	unsigned int size_diff;
3240b18c6685SChris Mason 	int i;
3241b18c6685SChris Mason 
32425f39d397SChris Mason 	leaf = path->nodes[0];
3243179e29e4SChris Mason 	slot = path->slots[0];
3244179e29e4SChris Mason 
3245179e29e4SChris Mason 	old_size = btrfs_item_size_nr(leaf, slot);
3246179e29e4SChris Mason 	if (old_size == new_size)
3247179e29e4SChris Mason 		return 0;
3248b18c6685SChris Mason 
32495f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3250b18c6685SChris Mason 	data_end = leaf_data_end(root, leaf);
3251b18c6685SChris Mason 
32525f39d397SChris Mason 	old_data_start = btrfs_item_offset_nr(leaf, slot);
3253179e29e4SChris Mason 
3254b18c6685SChris Mason 	size_diff = old_size - new_size;
3255b18c6685SChris Mason 
3256b18c6685SChris Mason 	BUG_ON(slot < 0);
3257b18c6685SChris Mason 	BUG_ON(slot >= nritems);
3258b18c6685SChris Mason 
3259b18c6685SChris Mason 	/*
3260b18c6685SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3261b18c6685SChris Mason 	 */
3262b18c6685SChris Mason 	/* first correct the data pointers */
3263b18c6685SChris Mason 	for (i = slot; i < nritems; i++) {
32645f39d397SChris Mason 		u32 ioff;
32655f39d397SChris Mason 		item = btrfs_item_nr(leaf, i);
3266db94535dSChris Mason 
3267db94535dSChris Mason 		if (!leaf->map_token) {
3268db94535dSChris Mason 			map_extent_buffer(leaf, (unsigned long)item,
3269db94535dSChris Mason 					sizeof(struct btrfs_item),
3270db94535dSChris Mason 					&leaf->map_token, &leaf->kaddr,
3271db94535dSChris Mason 					&leaf->map_start, &leaf->map_len,
3272db94535dSChris Mason 					KM_USER1);
3273db94535dSChris Mason 		}
3274db94535dSChris Mason 
32755f39d397SChris Mason 		ioff = btrfs_item_offset(leaf, item);
32765f39d397SChris Mason 		btrfs_set_item_offset(leaf, item, ioff + size_diff);
3277b18c6685SChris Mason 	}
3278db94535dSChris Mason 
3279db94535dSChris Mason 	if (leaf->map_token) {
3280db94535dSChris Mason 		unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3281db94535dSChris Mason 		leaf->map_token = NULL;
3282db94535dSChris Mason 	}
3283db94535dSChris Mason 
3284b18c6685SChris Mason 	/* shift the data */
3285179e29e4SChris Mason 	if (from_end) {
32865f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3287b18c6685SChris Mason 			      data_end + size_diff, btrfs_leaf_data(leaf) +
3288b18c6685SChris Mason 			      data_end, old_data_start + new_size - data_end);
3289179e29e4SChris Mason 	} else {
3290179e29e4SChris Mason 		struct btrfs_disk_key disk_key;
3291179e29e4SChris Mason 		u64 offset;
3292179e29e4SChris Mason 
3293179e29e4SChris Mason 		btrfs_item_key(leaf, &disk_key, slot);
3294179e29e4SChris Mason 
3295179e29e4SChris Mason 		if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3296179e29e4SChris Mason 			unsigned long ptr;
3297179e29e4SChris Mason 			struct btrfs_file_extent_item *fi;
3298179e29e4SChris Mason 
3299179e29e4SChris Mason 			fi = btrfs_item_ptr(leaf, slot,
3300179e29e4SChris Mason 					    struct btrfs_file_extent_item);
3301179e29e4SChris Mason 			fi = (struct btrfs_file_extent_item *)(
3302179e29e4SChris Mason 			     (unsigned long)fi - size_diff);
3303179e29e4SChris Mason 
3304179e29e4SChris Mason 			if (btrfs_file_extent_type(leaf, fi) ==
3305179e29e4SChris Mason 			    BTRFS_FILE_EXTENT_INLINE) {
3306179e29e4SChris Mason 				ptr = btrfs_item_ptr_offset(leaf, slot);
3307179e29e4SChris Mason 				memmove_extent_buffer(leaf, ptr,
3308179e29e4SChris Mason 				      (unsigned long)fi,
3309179e29e4SChris Mason 				      offsetof(struct btrfs_file_extent_item,
3310179e29e4SChris Mason 						 disk_bytenr));
3311179e29e4SChris Mason 			}
3312179e29e4SChris Mason 		}
3313179e29e4SChris Mason 
3314179e29e4SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3315179e29e4SChris Mason 			      data_end + size_diff, btrfs_leaf_data(leaf) +
3316179e29e4SChris Mason 			      data_end, old_data_start - data_end);
3317179e29e4SChris Mason 
3318179e29e4SChris Mason 		offset = btrfs_disk_key_offset(&disk_key);
3319179e29e4SChris Mason 		btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3320179e29e4SChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot);
3321179e29e4SChris Mason 		if (slot == 0)
3322179e29e4SChris Mason 			fixup_low_keys(trans, root, path, &disk_key, 1);
3323179e29e4SChris Mason 	}
33245f39d397SChris Mason 
33255f39d397SChris Mason 	item = btrfs_item_nr(leaf, slot);
33265f39d397SChris Mason 	btrfs_set_item_size(leaf, item, new_size);
33275f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
3328b18c6685SChris Mason 
33295f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
33305f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
3331b18c6685SChris Mason 		BUG();
33325f39d397SChris Mason 	}
33331cd30799STsutomu Itoh 	return 0;
3334b18c6685SChris Mason }
3335b18c6685SChris Mason 
3336d352ac68SChris Mason /*
3337d352ac68SChris Mason  * make the item pointed to by the path bigger, data_size is the new size.
3338d352ac68SChris Mason  */
33395f39d397SChris Mason int btrfs_extend_item(struct btrfs_trans_handle *trans,
33405f39d397SChris Mason 		      struct btrfs_root *root, struct btrfs_path *path,
33415f39d397SChris Mason 		      u32 data_size)
33426567e837SChris Mason {
33436567e837SChris Mason 	int slot;
33445f39d397SChris Mason 	struct extent_buffer *leaf;
33455f39d397SChris Mason 	struct btrfs_item *item;
33466567e837SChris Mason 	u32 nritems;
33476567e837SChris Mason 	unsigned int data_end;
33486567e837SChris Mason 	unsigned int old_data;
33496567e837SChris Mason 	unsigned int old_size;
33506567e837SChris Mason 	int i;
33516567e837SChris Mason 
33525f39d397SChris Mason 	leaf = path->nodes[0];
33536567e837SChris Mason 
33545f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
33556567e837SChris Mason 	data_end = leaf_data_end(root, leaf);
33566567e837SChris Mason 
33575f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < data_size) {
33585f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
33596567e837SChris Mason 		BUG();
33605f39d397SChris Mason 	}
33616567e837SChris Mason 	slot = path->slots[0];
33625f39d397SChris Mason 	old_data = btrfs_item_end_nr(leaf, slot);
33636567e837SChris Mason 
33646567e837SChris Mason 	BUG_ON(slot < 0);
33653326d1b0SChris Mason 	if (slot >= nritems) {
33663326d1b0SChris Mason 		btrfs_print_leaf(root, leaf);
3367d397712bSChris Mason 		printk(KERN_CRIT "slot %d too large, nritems %d\n",
3368d397712bSChris Mason 		       slot, nritems);
33693326d1b0SChris Mason 		BUG_ON(1);
33703326d1b0SChris Mason 	}
33716567e837SChris Mason 
33726567e837SChris Mason 	/*
33736567e837SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
33746567e837SChris Mason 	 */
33756567e837SChris Mason 	/* first correct the data pointers */
33766567e837SChris Mason 	for (i = slot; i < nritems; i++) {
33775f39d397SChris Mason 		u32 ioff;
33785f39d397SChris Mason 		item = btrfs_item_nr(leaf, i);
3379db94535dSChris Mason 
3380db94535dSChris Mason 		if (!leaf->map_token) {
3381db94535dSChris Mason 			map_extent_buffer(leaf, (unsigned long)item,
3382db94535dSChris Mason 					sizeof(struct btrfs_item),
3383db94535dSChris Mason 					&leaf->map_token, &leaf->kaddr,
3384db94535dSChris Mason 					&leaf->map_start, &leaf->map_len,
3385db94535dSChris Mason 					KM_USER1);
3386db94535dSChris Mason 		}
33875f39d397SChris Mason 		ioff = btrfs_item_offset(leaf, item);
33885f39d397SChris Mason 		btrfs_set_item_offset(leaf, item, ioff - data_size);
33896567e837SChris Mason 	}
33905f39d397SChris Mason 
3391db94535dSChris Mason 	if (leaf->map_token) {
3392db94535dSChris Mason 		unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3393db94535dSChris Mason 		leaf->map_token = NULL;
3394db94535dSChris Mason 	}
3395db94535dSChris Mason 
33966567e837SChris Mason 	/* shift the data */
33975f39d397SChris Mason 	memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
33986567e837SChris Mason 		      data_end - data_size, btrfs_leaf_data(leaf) +
33996567e837SChris Mason 		      data_end, old_data - data_end);
34005f39d397SChris Mason 
34016567e837SChris Mason 	data_end = old_data;
34025f39d397SChris Mason 	old_size = btrfs_item_size_nr(leaf, slot);
34035f39d397SChris Mason 	item = btrfs_item_nr(leaf, slot);
34045f39d397SChris Mason 	btrfs_set_item_size(leaf, item, old_size + data_size);
34055f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
34066567e837SChris Mason 
34075f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
34085f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
34096567e837SChris Mason 		BUG();
34105f39d397SChris Mason 	}
34111cd30799STsutomu Itoh 	return 0;
34126567e837SChris Mason }
34136567e837SChris Mason 
341474123bd7SChris Mason /*
3415d352ac68SChris Mason  * Given a key and some data, insert items into the tree.
341674123bd7SChris Mason  * This does all the path init required, making room in the tree if needed.
3417f3465ca4SJosef Bacik  * Returns the number of keys that were inserted.
3418f3465ca4SJosef Bacik  */
3419f3465ca4SJosef Bacik int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3420f3465ca4SJosef Bacik 			    struct btrfs_root *root,
3421f3465ca4SJosef Bacik 			    struct btrfs_path *path,
3422f3465ca4SJosef Bacik 			    struct btrfs_key *cpu_key, u32 *data_size,
3423f3465ca4SJosef Bacik 			    int nr)
3424f3465ca4SJosef Bacik {
3425f3465ca4SJosef Bacik 	struct extent_buffer *leaf;
3426f3465ca4SJosef Bacik 	struct btrfs_item *item;
3427f3465ca4SJosef Bacik 	int ret = 0;
3428f3465ca4SJosef Bacik 	int slot;
3429f3465ca4SJosef Bacik 	int i;
3430f3465ca4SJosef Bacik 	u32 nritems;
3431f3465ca4SJosef Bacik 	u32 total_data = 0;
3432f3465ca4SJosef Bacik 	u32 total_size = 0;
3433f3465ca4SJosef Bacik 	unsigned int data_end;
3434f3465ca4SJosef Bacik 	struct btrfs_disk_key disk_key;
3435f3465ca4SJosef Bacik 	struct btrfs_key found_key;
3436f3465ca4SJosef Bacik 
343787b29b20SYan Zheng 	for (i = 0; i < nr; i++) {
343887b29b20SYan Zheng 		if (total_size + data_size[i] + sizeof(struct btrfs_item) >
343987b29b20SYan Zheng 		    BTRFS_LEAF_DATA_SIZE(root)) {
344087b29b20SYan Zheng 			break;
344187b29b20SYan Zheng 			nr = i;
344287b29b20SYan Zheng 		}
3443f3465ca4SJosef Bacik 		total_data += data_size[i];
344487b29b20SYan Zheng 		total_size += data_size[i] + sizeof(struct btrfs_item);
344587b29b20SYan Zheng 	}
344687b29b20SYan Zheng 	BUG_ON(nr == 0);
3447f3465ca4SJosef Bacik 
3448f3465ca4SJosef Bacik 	ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3449f3465ca4SJosef Bacik 	if (ret == 0)
3450f3465ca4SJosef Bacik 		return -EEXIST;
3451f3465ca4SJosef Bacik 	if (ret < 0)
3452f3465ca4SJosef Bacik 		goto out;
3453f3465ca4SJosef Bacik 
3454f3465ca4SJosef Bacik 	leaf = path->nodes[0];
3455f3465ca4SJosef Bacik 
3456f3465ca4SJosef Bacik 	nritems = btrfs_header_nritems(leaf);
3457f3465ca4SJosef Bacik 	data_end = leaf_data_end(root, leaf);
3458f3465ca4SJosef Bacik 
3459f3465ca4SJosef Bacik 	if (btrfs_leaf_free_space(root, leaf) < total_size) {
3460f3465ca4SJosef Bacik 		for (i = nr; i >= 0; i--) {
3461f3465ca4SJosef Bacik 			total_data -= data_size[i];
3462f3465ca4SJosef Bacik 			total_size -= data_size[i] + sizeof(struct btrfs_item);
3463f3465ca4SJosef Bacik 			if (total_size < btrfs_leaf_free_space(root, leaf))
3464f3465ca4SJosef Bacik 				break;
3465f3465ca4SJosef Bacik 		}
3466f3465ca4SJosef Bacik 		nr = i;
3467f3465ca4SJosef Bacik 	}
3468f3465ca4SJosef Bacik 
3469f3465ca4SJosef Bacik 	slot = path->slots[0];
3470f3465ca4SJosef Bacik 	BUG_ON(slot < 0);
3471f3465ca4SJosef Bacik 
3472f3465ca4SJosef Bacik 	if (slot != nritems) {
3473f3465ca4SJosef Bacik 		unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3474f3465ca4SJosef Bacik 
3475f3465ca4SJosef Bacik 		item = btrfs_item_nr(leaf, slot);
3476f3465ca4SJosef Bacik 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
3477f3465ca4SJosef Bacik 
3478f3465ca4SJosef Bacik 		/* figure out how many keys we can insert in here */
3479f3465ca4SJosef Bacik 		total_data = data_size[0];
3480f3465ca4SJosef Bacik 		for (i = 1; i < nr; i++) {
34815d4f98a2SYan Zheng 			if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
3482f3465ca4SJosef Bacik 				break;
3483f3465ca4SJosef Bacik 			total_data += data_size[i];
3484f3465ca4SJosef Bacik 		}
3485f3465ca4SJosef Bacik 		nr = i;
3486f3465ca4SJosef Bacik 
3487f3465ca4SJosef Bacik 		if (old_data < data_end) {
3488f3465ca4SJosef Bacik 			btrfs_print_leaf(root, leaf);
3489d397712bSChris Mason 			printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
3490f3465ca4SJosef Bacik 			       slot, old_data, data_end);
3491f3465ca4SJosef Bacik 			BUG_ON(1);
3492f3465ca4SJosef Bacik 		}
3493f3465ca4SJosef Bacik 		/*
3494f3465ca4SJosef Bacik 		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3495f3465ca4SJosef Bacik 		 */
3496f3465ca4SJosef Bacik 		/* first correct the data pointers */
3497f3465ca4SJosef Bacik 		WARN_ON(leaf->map_token);
3498f3465ca4SJosef Bacik 		for (i = slot; i < nritems; i++) {
3499f3465ca4SJosef Bacik 			u32 ioff;
3500f3465ca4SJosef Bacik 
3501f3465ca4SJosef Bacik 			item = btrfs_item_nr(leaf, i);
3502f3465ca4SJosef Bacik 			if (!leaf->map_token) {
3503f3465ca4SJosef Bacik 				map_extent_buffer(leaf, (unsigned long)item,
3504f3465ca4SJosef Bacik 					sizeof(struct btrfs_item),
3505f3465ca4SJosef Bacik 					&leaf->map_token, &leaf->kaddr,
3506f3465ca4SJosef Bacik 					&leaf->map_start, &leaf->map_len,
3507f3465ca4SJosef Bacik 					KM_USER1);
3508f3465ca4SJosef Bacik 			}
3509f3465ca4SJosef Bacik 
3510f3465ca4SJosef Bacik 			ioff = btrfs_item_offset(leaf, item);
3511f3465ca4SJosef Bacik 			btrfs_set_item_offset(leaf, item, ioff - total_data);
3512f3465ca4SJosef Bacik 		}
3513f3465ca4SJosef Bacik 		if (leaf->map_token) {
3514f3465ca4SJosef Bacik 			unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3515f3465ca4SJosef Bacik 			leaf->map_token = NULL;
3516f3465ca4SJosef Bacik 		}
3517f3465ca4SJosef Bacik 
3518f3465ca4SJosef Bacik 		/* shift the items */
3519f3465ca4SJosef Bacik 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3520f3465ca4SJosef Bacik 			      btrfs_item_nr_offset(slot),
3521f3465ca4SJosef Bacik 			      (nritems - slot) * sizeof(struct btrfs_item));
3522f3465ca4SJosef Bacik 
3523f3465ca4SJosef Bacik 		/* shift the data */
3524f3465ca4SJosef Bacik 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3525f3465ca4SJosef Bacik 			      data_end - total_data, btrfs_leaf_data(leaf) +
3526f3465ca4SJosef Bacik 			      data_end, old_data - data_end);
3527f3465ca4SJosef Bacik 		data_end = old_data;
3528f3465ca4SJosef Bacik 	} else {
3529f3465ca4SJosef Bacik 		/*
3530f3465ca4SJosef Bacik 		 * this sucks but it has to be done, if we are inserting at
3531f3465ca4SJosef Bacik 		 * the end of the leaf only insert 1 of the items, since we
3532f3465ca4SJosef Bacik 		 * have no way of knowing whats on the next leaf and we'd have
3533f3465ca4SJosef Bacik 		 * to drop our current locks to figure it out
3534f3465ca4SJosef Bacik 		 */
3535f3465ca4SJosef Bacik 		nr = 1;
3536f3465ca4SJosef Bacik 	}
3537f3465ca4SJosef Bacik 
3538f3465ca4SJosef Bacik 	/* setup the item for the new data */
3539f3465ca4SJosef Bacik 	for (i = 0; i < nr; i++) {
3540f3465ca4SJosef Bacik 		btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3541f3465ca4SJosef Bacik 		btrfs_set_item_key(leaf, &disk_key, slot + i);
3542f3465ca4SJosef Bacik 		item = btrfs_item_nr(leaf, slot + i);
3543f3465ca4SJosef Bacik 		btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
3544f3465ca4SJosef Bacik 		data_end -= data_size[i];
3545f3465ca4SJosef Bacik 		btrfs_set_item_size(leaf, item, data_size[i]);
3546f3465ca4SJosef Bacik 	}
3547f3465ca4SJosef Bacik 	btrfs_set_header_nritems(leaf, nritems + nr);
3548f3465ca4SJosef Bacik 	btrfs_mark_buffer_dirty(leaf);
3549f3465ca4SJosef Bacik 
3550f3465ca4SJosef Bacik 	ret = 0;
3551f3465ca4SJosef Bacik 	if (slot == 0) {
3552f3465ca4SJosef Bacik 		btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3553f3465ca4SJosef Bacik 		ret = fixup_low_keys(trans, root, path, &disk_key, 1);
3554f3465ca4SJosef Bacik 	}
3555f3465ca4SJosef Bacik 
3556f3465ca4SJosef Bacik 	if (btrfs_leaf_free_space(root, leaf) < 0) {
3557f3465ca4SJosef Bacik 		btrfs_print_leaf(root, leaf);
3558f3465ca4SJosef Bacik 		BUG();
3559f3465ca4SJosef Bacik 	}
3560f3465ca4SJosef Bacik out:
3561f3465ca4SJosef Bacik 	if (!ret)
3562f3465ca4SJosef Bacik 		ret = nr;
3563f3465ca4SJosef Bacik 	return ret;
3564f3465ca4SJosef Bacik }
3565f3465ca4SJosef Bacik 
3566f3465ca4SJosef Bacik /*
356744871b1bSChris Mason  * this is a helper for btrfs_insert_empty_items, the main goal here is
356844871b1bSChris Mason  * to save stack depth by doing the bulk of the work in a function
356944871b1bSChris Mason  * that doesn't call btrfs_search_slot
357074123bd7SChris Mason  */
357116cdcec7SMiao Xie int setup_items_for_insert(struct btrfs_trans_handle *trans,
357244871b1bSChris Mason 			   struct btrfs_root *root, struct btrfs_path *path,
35739c58309dSChris Mason 			   struct btrfs_key *cpu_key, u32 *data_size,
357444871b1bSChris Mason 			   u32 total_data, u32 total_size, int nr)
3575be0e5c09SChris Mason {
35765f39d397SChris Mason 	struct btrfs_item *item;
35779c58309dSChris Mason 	int i;
35787518a238SChris Mason 	u32 nritems;
3579be0e5c09SChris Mason 	unsigned int data_end;
3580e2fa7227SChris Mason 	struct btrfs_disk_key disk_key;
358144871b1bSChris Mason 	int ret;
358244871b1bSChris Mason 	struct extent_buffer *leaf;
358344871b1bSChris Mason 	int slot;
3584e2fa7227SChris Mason 
35855f39d397SChris Mason 	leaf = path->nodes[0];
358644871b1bSChris Mason 	slot = path->slots[0];
358774123bd7SChris Mason 
35885f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3589123abc88SChris Mason 	data_end = leaf_data_end(root, leaf);
3590eb60ceacSChris Mason 
3591f25956ccSChris Mason 	if (btrfs_leaf_free_space(root, leaf) < total_size) {
35923326d1b0SChris Mason 		btrfs_print_leaf(root, leaf);
3593d397712bSChris Mason 		printk(KERN_CRIT "not enough freespace need %u have %d\n",
35949c58309dSChris Mason 		       total_size, btrfs_leaf_free_space(root, leaf));
3595be0e5c09SChris Mason 		BUG();
3596d4dbff95SChris Mason 	}
35975f39d397SChris Mason 
3598be0e5c09SChris Mason 	if (slot != nritems) {
35995f39d397SChris Mason 		unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3600be0e5c09SChris Mason 
36015f39d397SChris Mason 		if (old_data < data_end) {
36025f39d397SChris Mason 			btrfs_print_leaf(root, leaf);
3603d397712bSChris Mason 			printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
36045f39d397SChris Mason 			       slot, old_data, data_end);
36055f39d397SChris Mason 			BUG_ON(1);
36065f39d397SChris Mason 		}
3607be0e5c09SChris Mason 		/*
3608be0e5c09SChris Mason 		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3609be0e5c09SChris Mason 		 */
3610be0e5c09SChris Mason 		/* first correct the data pointers */
3611db94535dSChris Mason 		WARN_ON(leaf->map_token);
36120783fcfcSChris Mason 		for (i = slot; i < nritems; i++) {
36135f39d397SChris Mason 			u32 ioff;
3614db94535dSChris Mason 
36155f39d397SChris Mason 			item = btrfs_item_nr(leaf, i);
3616db94535dSChris Mason 			if (!leaf->map_token) {
3617db94535dSChris Mason 				map_extent_buffer(leaf, (unsigned long)item,
3618db94535dSChris Mason 					sizeof(struct btrfs_item),
3619db94535dSChris Mason 					&leaf->map_token, &leaf->kaddr,
3620db94535dSChris Mason 					&leaf->map_start, &leaf->map_len,
3621db94535dSChris Mason 					KM_USER1);
3622db94535dSChris Mason 			}
3623db94535dSChris Mason 
36245f39d397SChris Mason 			ioff = btrfs_item_offset(leaf, item);
36259c58309dSChris Mason 			btrfs_set_item_offset(leaf, item, ioff - total_data);
36260783fcfcSChris Mason 		}
3627db94535dSChris Mason 		if (leaf->map_token) {
3628db94535dSChris Mason 			unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3629db94535dSChris Mason 			leaf->map_token = NULL;
3630db94535dSChris Mason 		}
3631be0e5c09SChris Mason 
3632be0e5c09SChris Mason 		/* shift the items */
36339c58309dSChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
36345f39d397SChris Mason 			      btrfs_item_nr_offset(slot),
36350783fcfcSChris Mason 			      (nritems - slot) * sizeof(struct btrfs_item));
3636be0e5c09SChris Mason 
3637be0e5c09SChris Mason 		/* shift the data */
36385f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
36399c58309dSChris Mason 			      data_end - total_data, btrfs_leaf_data(leaf) +
3640be0e5c09SChris Mason 			      data_end, old_data - data_end);
3641be0e5c09SChris Mason 		data_end = old_data;
3642be0e5c09SChris Mason 	}
36435f39d397SChris Mason 
364462e2749eSChris Mason 	/* setup the item for the new data */
36459c58309dSChris Mason 	for (i = 0; i < nr; i++) {
36469c58309dSChris Mason 		btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
36479c58309dSChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot + i);
36489c58309dSChris Mason 		item = btrfs_item_nr(leaf, slot + i);
36499c58309dSChris Mason 		btrfs_set_item_offset(leaf, item, data_end - data_size[i]);
36509c58309dSChris Mason 		data_end -= data_size[i];
36519c58309dSChris Mason 		btrfs_set_item_size(leaf, item, data_size[i]);
36529c58309dSChris Mason 	}
365344871b1bSChris Mason 
36549c58309dSChris Mason 	btrfs_set_header_nritems(leaf, nritems + nr);
3655aa5d6bedSChris Mason 
3656aa5d6bedSChris Mason 	ret = 0;
36575a01a2e3SChris Mason 	if (slot == 0) {
36585a01a2e3SChris Mason 		btrfs_cpu_key_to_disk(&disk_key, cpu_key);
3659e089f05cSChris Mason 		ret = fixup_low_keys(trans, root, path, &disk_key, 1);
36605a01a2e3SChris Mason 	}
3661b9473439SChris Mason 	btrfs_unlock_up_safe(path, 1);
3662b9473439SChris Mason 	btrfs_mark_buffer_dirty(leaf);
3663aa5d6bedSChris Mason 
36645f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
36655f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
3666be0e5c09SChris Mason 		BUG();
36675f39d397SChris Mason 	}
366844871b1bSChris Mason 	return ret;
366944871b1bSChris Mason }
367044871b1bSChris Mason 
367144871b1bSChris Mason /*
367244871b1bSChris Mason  * Given a key and some data, insert items into the tree.
367344871b1bSChris Mason  * This does all the path init required, making room in the tree if needed.
367444871b1bSChris Mason  */
367544871b1bSChris Mason int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
367644871b1bSChris Mason 			    struct btrfs_root *root,
367744871b1bSChris Mason 			    struct btrfs_path *path,
367844871b1bSChris Mason 			    struct btrfs_key *cpu_key, u32 *data_size,
367944871b1bSChris Mason 			    int nr)
368044871b1bSChris Mason {
368144871b1bSChris Mason 	int ret = 0;
368244871b1bSChris Mason 	int slot;
368344871b1bSChris Mason 	int i;
368444871b1bSChris Mason 	u32 total_size = 0;
368544871b1bSChris Mason 	u32 total_data = 0;
368644871b1bSChris Mason 
368744871b1bSChris Mason 	for (i = 0; i < nr; i++)
368844871b1bSChris Mason 		total_data += data_size[i];
368944871b1bSChris Mason 
369044871b1bSChris Mason 	total_size = total_data + (nr * sizeof(struct btrfs_item));
369144871b1bSChris Mason 	ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
369244871b1bSChris Mason 	if (ret == 0)
369344871b1bSChris Mason 		return -EEXIST;
369444871b1bSChris Mason 	if (ret < 0)
369544871b1bSChris Mason 		goto out;
369644871b1bSChris Mason 
369744871b1bSChris Mason 	slot = path->slots[0];
369844871b1bSChris Mason 	BUG_ON(slot < 0);
369944871b1bSChris Mason 
370044871b1bSChris Mason 	ret = setup_items_for_insert(trans, root, path, cpu_key, data_size,
370144871b1bSChris Mason 			       total_data, total_size, nr);
370244871b1bSChris Mason 
3703ed2ff2cbSChris Mason out:
370462e2749eSChris Mason 	return ret;
370562e2749eSChris Mason }
370662e2749eSChris Mason 
370762e2749eSChris Mason /*
370862e2749eSChris Mason  * Given a key and some data, insert an item into the tree.
370962e2749eSChris Mason  * This does all the path init required, making room in the tree if needed.
371062e2749eSChris Mason  */
3711e089f05cSChris Mason int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
3712e089f05cSChris Mason 		      *root, struct btrfs_key *cpu_key, void *data, u32
3713e089f05cSChris Mason 		      data_size)
371462e2749eSChris Mason {
371562e2749eSChris Mason 	int ret = 0;
37162c90e5d6SChris Mason 	struct btrfs_path *path;
37175f39d397SChris Mason 	struct extent_buffer *leaf;
37185f39d397SChris Mason 	unsigned long ptr;
371962e2749eSChris Mason 
37202c90e5d6SChris Mason 	path = btrfs_alloc_path();
3721db5b493aSTsutomu Itoh 	if (!path)
3722db5b493aSTsutomu Itoh 		return -ENOMEM;
37232c90e5d6SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
372462e2749eSChris Mason 	if (!ret) {
37255f39d397SChris Mason 		leaf = path->nodes[0];
37265f39d397SChris Mason 		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
37275f39d397SChris Mason 		write_extent_buffer(leaf, data, ptr, data_size);
37285f39d397SChris Mason 		btrfs_mark_buffer_dirty(leaf);
372962e2749eSChris Mason 	}
37302c90e5d6SChris Mason 	btrfs_free_path(path);
3731aa5d6bedSChris Mason 	return ret;
3732be0e5c09SChris Mason }
3733be0e5c09SChris Mason 
373474123bd7SChris Mason /*
37355de08d7dSChris Mason  * delete the pointer from a given node.
373674123bd7SChris Mason  *
3737d352ac68SChris Mason  * the tree should have been previously balanced so the deletion does not
3738d352ac68SChris Mason  * empty a node.
373974123bd7SChris Mason  */
3740e089f05cSChris Mason static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3741e089f05cSChris Mason 		   struct btrfs_path *path, int level, int slot)
3742be0e5c09SChris Mason {
37435f39d397SChris Mason 	struct extent_buffer *parent = path->nodes[level];
37447518a238SChris Mason 	u32 nritems;
3745aa5d6bedSChris Mason 	int ret = 0;
3746bb803951SChris Mason 	int wret;
3747be0e5c09SChris Mason 
37485f39d397SChris Mason 	nritems = btrfs_header_nritems(parent);
3749be0e5c09SChris Mason 	if (slot != nritems - 1) {
37505f39d397SChris Mason 		memmove_extent_buffer(parent,
37515f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot),
37525f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot + 1),
3753d6025579SChris Mason 			      sizeof(struct btrfs_key_ptr) *
3754d6025579SChris Mason 			      (nritems - slot - 1));
3755be0e5c09SChris Mason 	}
37567518a238SChris Mason 	nritems--;
37575f39d397SChris Mason 	btrfs_set_header_nritems(parent, nritems);
37587518a238SChris Mason 	if (nritems == 0 && parent == root->node) {
37595f39d397SChris Mason 		BUG_ON(btrfs_header_level(root->node) != 1);
3760eb60ceacSChris Mason 		/* just turn the root into a leaf and break */
37615f39d397SChris Mason 		btrfs_set_header_level(root->node, 0);
3762bb803951SChris Mason 	} else if (slot == 0) {
37635f39d397SChris Mason 		struct btrfs_disk_key disk_key;
37645f39d397SChris Mason 
37655f39d397SChris Mason 		btrfs_node_key(parent, &disk_key, 0);
37665f39d397SChris Mason 		wret = fixup_low_keys(trans, root, path, &disk_key, level + 1);
37670f70abe2SChris Mason 		if (wret)
37680f70abe2SChris Mason 			ret = wret;
3769be0e5c09SChris Mason 	}
3770d6025579SChris Mason 	btrfs_mark_buffer_dirty(parent);
3771aa5d6bedSChris Mason 	return ret;
3772be0e5c09SChris Mason }
3773be0e5c09SChris Mason 
377474123bd7SChris Mason /*
3775323ac95bSChris Mason  * a helper function to delete the leaf pointed to by path->slots[1] and
37765d4f98a2SYan Zheng  * path->nodes[1].
3777323ac95bSChris Mason  *
3778323ac95bSChris Mason  * This deletes the pointer in path->nodes[1] and frees the leaf
3779323ac95bSChris Mason  * block extent.  zero is returned if it all worked out, < 0 otherwise.
3780323ac95bSChris Mason  *
3781323ac95bSChris Mason  * The path must have already been setup for deleting the leaf, including
3782323ac95bSChris Mason  * all the proper balancing.  path->nodes[1] must be locked.
3783323ac95bSChris Mason  */
37845d4f98a2SYan Zheng static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3785323ac95bSChris Mason 				   struct btrfs_root *root,
37865d4f98a2SYan Zheng 				   struct btrfs_path *path,
37875d4f98a2SYan Zheng 				   struct extent_buffer *leaf)
3788323ac95bSChris Mason {
3789323ac95bSChris Mason 	int ret;
3790323ac95bSChris Mason 
37915d4f98a2SYan Zheng 	WARN_ON(btrfs_header_generation(leaf) != trans->transid);
3792323ac95bSChris Mason 	ret = del_ptr(trans, root, path, 1, path->slots[1]);
3793323ac95bSChris Mason 	if (ret)
3794323ac95bSChris Mason 		return ret;
3795323ac95bSChris Mason 
37964d081c41SChris Mason 	/*
37974d081c41SChris Mason 	 * btrfs_free_extent is expensive, we want to make sure we
37984d081c41SChris Mason 	 * aren't holding any locks when we call it
37994d081c41SChris Mason 	 */
38004d081c41SChris Mason 	btrfs_unlock_up_safe(path, 0);
38014d081c41SChris Mason 
3802f0486c68SYan, Zheng 	root_sub_used(root, leaf->len);
3803f0486c68SYan, Zheng 
3804f0486c68SYan, Zheng 	btrfs_free_tree_block(trans, root, leaf, 0, 1);
3805f0486c68SYan, Zheng 	return 0;
3806323ac95bSChris Mason }
3807323ac95bSChris Mason /*
380874123bd7SChris Mason  * delete the item at the leaf level in path.  If that empties
380974123bd7SChris Mason  * the leaf, remove it from the tree
381074123bd7SChris Mason  */
381185e21bacSChris Mason int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
381285e21bacSChris Mason 		    struct btrfs_path *path, int slot, int nr)
3813be0e5c09SChris Mason {
38145f39d397SChris Mason 	struct extent_buffer *leaf;
38155f39d397SChris Mason 	struct btrfs_item *item;
381685e21bacSChris Mason 	int last_off;
381785e21bacSChris Mason 	int dsize = 0;
3818aa5d6bedSChris Mason 	int ret = 0;
3819aa5d6bedSChris Mason 	int wret;
382085e21bacSChris Mason 	int i;
38217518a238SChris Mason 	u32 nritems;
3822be0e5c09SChris Mason 
38235f39d397SChris Mason 	leaf = path->nodes[0];
382485e21bacSChris Mason 	last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
382585e21bacSChris Mason 
382685e21bacSChris Mason 	for (i = 0; i < nr; i++)
382785e21bacSChris Mason 		dsize += btrfs_item_size_nr(leaf, slot + i);
382885e21bacSChris Mason 
38295f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
3830be0e5c09SChris Mason 
383185e21bacSChris Mason 	if (slot + nr != nritems) {
3832123abc88SChris Mason 		int data_end = leaf_data_end(root, leaf);
38335f39d397SChris Mason 
38345f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3835d6025579SChris Mason 			      data_end + dsize,
3836123abc88SChris Mason 			      btrfs_leaf_data(leaf) + data_end,
383785e21bacSChris Mason 			      last_off - data_end);
38385f39d397SChris Mason 
383985e21bacSChris Mason 		for (i = slot + nr; i < nritems; i++) {
38405f39d397SChris Mason 			u32 ioff;
3841db94535dSChris Mason 
38425f39d397SChris Mason 			item = btrfs_item_nr(leaf, i);
3843db94535dSChris Mason 			if (!leaf->map_token) {
3844db94535dSChris Mason 				map_extent_buffer(leaf, (unsigned long)item,
3845db94535dSChris Mason 					sizeof(struct btrfs_item),
3846db94535dSChris Mason 					&leaf->map_token, &leaf->kaddr,
3847db94535dSChris Mason 					&leaf->map_start, &leaf->map_len,
3848db94535dSChris Mason 					KM_USER1);
3849db94535dSChris Mason 			}
38505f39d397SChris Mason 			ioff = btrfs_item_offset(leaf, item);
38515f39d397SChris Mason 			btrfs_set_item_offset(leaf, item, ioff + dsize);
38520783fcfcSChris Mason 		}
3853db94535dSChris Mason 
3854db94535dSChris Mason 		if (leaf->map_token) {
3855db94535dSChris Mason 			unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
3856db94535dSChris Mason 			leaf->map_token = NULL;
3857db94535dSChris Mason 		}
3858db94535dSChris Mason 
38595f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
386085e21bacSChris Mason 			      btrfs_item_nr_offset(slot + nr),
38610783fcfcSChris Mason 			      sizeof(struct btrfs_item) *
386285e21bacSChris Mason 			      (nritems - slot - nr));
3863be0e5c09SChris Mason 	}
386485e21bacSChris Mason 	btrfs_set_header_nritems(leaf, nritems - nr);
386585e21bacSChris Mason 	nritems -= nr;
38665f39d397SChris Mason 
386774123bd7SChris Mason 	/* delete the leaf if we've emptied it */
38687518a238SChris Mason 	if (nritems == 0) {
38695f39d397SChris Mason 		if (leaf == root->node) {
38705f39d397SChris Mason 			btrfs_set_header_level(leaf, 0);
38719a8dd150SChris Mason 		} else {
3872f0486c68SYan, Zheng 			btrfs_set_path_blocking(path);
3873f0486c68SYan, Zheng 			clean_tree_block(trans, root, leaf);
38745d4f98a2SYan Zheng 			ret = btrfs_del_leaf(trans, root, path, leaf);
3875323ac95bSChris Mason 			BUG_ON(ret);
38769a8dd150SChris Mason 		}
3877be0e5c09SChris Mason 	} else {
38787518a238SChris Mason 		int used = leaf_space_used(leaf, 0, nritems);
3879aa5d6bedSChris Mason 		if (slot == 0) {
38805f39d397SChris Mason 			struct btrfs_disk_key disk_key;
38815f39d397SChris Mason 
38825f39d397SChris Mason 			btrfs_item_key(leaf, &disk_key, 0);
3883e089f05cSChris Mason 			wret = fixup_low_keys(trans, root, path,
38845f39d397SChris Mason 					      &disk_key, 1);
3885aa5d6bedSChris Mason 			if (wret)
3886aa5d6bedSChris Mason 				ret = wret;
3887aa5d6bedSChris Mason 		}
3888aa5d6bedSChris Mason 
388974123bd7SChris Mason 		/* delete the leaf if it is mostly empty */
3890d717aa1dSYan Zheng 		if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
3891be0e5c09SChris Mason 			/* push_leaf_left fixes the path.
3892be0e5c09SChris Mason 			 * make sure the path still points to our leaf
3893be0e5c09SChris Mason 			 * for possible call to del_ptr below
3894be0e5c09SChris Mason 			 */
38954920c9acSChris Mason 			slot = path->slots[1];
38965f39d397SChris Mason 			extent_buffer_get(leaf);
38975f39d397SChris Mason 
3898b9473439SChris Mason 			btrfs_set_path_blocking(path);
389999d8f83cSChris Mason 			wret = push_leaf_left(trans, root, path, 1, 1,
390099d8f83cSChris Mason 					      1, (u32)-1);
390154aa1f4dSChris Mason 			if (wret < 0 && wret != -ENOSPC)
3902aa5d6bedSChris Mason 				ret = wret;
39035f39d397SChris Mason 
39045f39d397SChris Mason 			if (path->nodes[0] == leaf &&
39055f39d397SChris Mason 			    btrfs_header_nritems(leaf)) {
390699d8f83cSChris Mason 				wret = push_leaf_right(trans, root, path, 1,
390799d8f83cSChris Mason 						       1, 1, 0);
390854aa1f4dSChris Mason 				if (wret < 0 && wret != -ENOSPC)
3909aa5d6bedSChris Mason 					ret = wret;
3910aa5d6bedSChris Mason 			}
39115f39d397SChris Mason 
39125f39d397SChris Mason 			if (btrfs_header_nritems(leaf) == 0) {
3913323ac95bSChris Mason 				path->slots[1] = slot;
39145d4f98a2SYan Zheng 				ret = btrfs_del_leaf(trans, root, path, leaf);
3915323ac95bSChris Mason 				BUG_ON(ret);
39165f39d397SChris Mason 				free_extent_buffer(leaf);
39175de08d7dSChris Mason 			} else {
3918925baeddSChris Mason 				/* if we're still in the path, make sure
3919925baeddSChris Mason 				 * we're dirty.  Otherwise, one of the
3920925baeddSChris Mason 				 * push_leaf functions must have already
3921925baeddSChris Mason 				 * dirtied this buffer
3922925baeddSChris Mason 				 */
3923925baeddSChris Mason 				if (path->nodes[0] == leaf)
39245f39d397SChris Mason 					btrfs_mark_buffer_dirty(leaf);
39255f39d397SChris Mason 				free_extent_buffer(leaf);
3926be0e5c09SChris Mason 			}
3927d5719762SChris Mason 		} else {
39285f39d397SChris Mason 			btrfs_mark_buffer_dirty(leaf);
3929be0e5c09SChris Mason 		}
39309a8dd150SChris Mason 	}
3931aa5d6bedSChris Mason 	return ret;
39329a8dd150SChris Mason }
39339a8dd150SChris Mason 
393497571fd0SChris Mason /*
3935925baeddSChris Mason  * search the tree again to find a leaf with lesser keys
39367bb86316SChris Mason  * returns 0 if it found something or 1 if there are no lesser leaves.
39377bb86316SChris Mason  * returns < 0 on io errors.
3938d352ac68SChris Mason  *
3939d352ac68SChris Mason  * This may release the path, and so you may lose any locks held at the
3940d352ac68SChris Mason  * time you call it.
39417bb86316SChris Mason  */
39427bb86316SChris Mason int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
39437bb86316SChris Mason {
3944925baeddSChris Mason 	struct btrfs_key key;
3945925baeddSChris Mason 	struct btrfs_disk_key found_key;
3946925baeddSChris Mason 	int ret;
39477bb86316SChris Mason 
3948925baeddSChris Mason 	btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
3949925baeddSChris Mason 
3950925baeddSChris Mason 	if (key.offset > 0)
3951925baeddSChris Mason 		key.offset--;
3952925baeddSChris Mason 	else if (key.type > 0)
3953925baeddSChris Mason 		key.type--;
3954925baeddSChris Mason 	else if (key.objectid > 0)
3955925baeddSChris Mason 		key.objectid--;
3956925baeddSChris Mason 	else
39577bb86316SChris Mason 		return 1;
39587bb86316SChris Mason 
3959b3b4aa74SDavid Sterba 	btrfs_release_path(path);
3960925baeddSChris Mason 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3961925baeddSChris Mason 	if (ret < 0)
3962925baeddSChris Mason 		return ret;
3963925baeddSChris Mason 	btrfs_item_key(path->nodes[0], &found_key, 0);
3964925baeddSChris Mason 	ret = comp_keys(&found_key, &key);
3965925baeddSChris Mason 	if (ret < 0)
39667bb86316SChris Mason 		return 0;
3967925baeddSChris Mason 	return 1;
39687bb86316SChris Mason }
39697bb86316SChris Mason 
39703f157a2fSChris Mason /*
39713f157a2fSChris Mason  * A helper function to walk down the tree starting at min_key, and looking
39723f157a2fSChris Mason  * for nodes or leaves that are either in cache or have a minimum
3973d352ac68SChris Mason  * transaction id.  This is used by the btree defrag code, and tree logging
39743f157a2fSChris Mason  *
39753f157a2fSChris Mason  * This does not cow, but it does stuff the starting key it finds back
39763f157a2fSChris Mason  * into min_key, so you can call btrfs_search_slot with cow=1 on the
39773f157a2fSChris Mason  * key and get a writable path.
39783f157a2fSChris Mason  *
39793f157a2fSChris Mason  * This does lock as it descends, and path->keep_locks should be set
39803f157a2fSChris Mason  * to 1 by the caller.
39813f157a2fSChris Mason  *
39823f157a2fSChris Mason  * This honors path->lowest_level to prevent descent past a given level
39833f157a2fSChris Mason  * of the tree.
39843f157a2fSChris Mason  *
3985d352ac68SChris Mason  * min_trans indicates the oldest transaction that you are interested
3986d352ac68SChris Mason  * in walking through.  Any nodes or leaves older than min_trans are
3987d352ac68SChris Mason  * skipped over (without reading them).
3988d352ac68SChris Mason  *
39893f157a2fSChris Mason  * returns zero if something useful was found, < 0 on error and 1 if there
39903f157a2fSChris Mason  * was nothing in the tree that matched the search criteria.
39913f157a2fSChris Mason  */
39923f157a2fSChris Mason int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
3993e02119d5SChris Mason 			 struct btrfs_key *max_key,
39943f157a2fSChris Mason 			 struct btrfs_path *path, int cache_only,
39953f157a2fSChris Mason 			 u64 min_trans)
39963f157a2fSChris Mason {
39973f157a2fSChris Mason 	struct extent_buffer *cur;
39983f157a2fSChris Mason 	struct btrfs_key found_key;
39993f157a2fSChris Mason 	int slot;
40009652480bSYan 	int sret;
40013f157a2fSChris Mason 	u32 nritems;
40023f157a2fSChris Mason 	int level;
40033f157a2fSChris Mason 	int ret = 1;
40043f157a2fSChris Mason 
4005934d375bSChris Mason 	WARN_ON(!path->keep_locks);
40063f157a2fSChris Mason again:
40073f157a2fSChris Mason 	cur = btrfs_lock_root_node(root);
40083f157a2fSChris Mason 	level = btrfs_header_level(cur);
4009e02119d5SChris Mason 	WARN_ON(path->nodes[level]);
40103f157a2fSChris Mason 	path->nodes[level] = cur;
40113f157a2fSChris Mason 	path->locks[level] = 1;
40123f157a2fSChris Mason 
40133f157a2fSChris Mason 	if (btrfs_header_generation(cur) < min_trans) {
40143f157a2fSChris Mason 		ret = 1;
40153f157a2fSChris Mason 		goto out;
40163f157a2fSChris Mason 	}
40173f157a2fSChris Mason 	while (1) {
40183f157a2fSChris Mason 		nritems = btrfs_header_nritems(cur);
40193f157a2fSChris Mason 		level = btrfs_header_level(cur);
40209652480bSYan 		sret = bin_search(cur, min_key, level, &slot);
40213f157a2fSChris Mason 
4022323ac95bSChris Mason 		/* at the lowest level, we're done, setup the path and exit */
4023323ac95bSChris Mason 		if (level == path->lowest_level) {
4024e02119d5SChris Mason 			if (slot >= nritems)
4025e02119d5SChris Mason 				goto find_next_key;
40263f157a2fSChris Mason 			ret = 0;
40273f157a2fSChris Mason 			path->slots[level] = slot;
40283f157a2fSChris Mason 			btrfs_item_key_to_cpu(cur, &found_key, slot);
40293f157a2fSChris Mason 			goto out;
40303f157a2fSChris Mason 		}
40319652480bSYan 		if (sret && slot > 0)
40329652480bSYan 			slot--;
40333f157a2fSChris Mason 		/*
40343f157a2fSChris Mason 		 * check this node pointer against the cache_only and
40353f157a2fSChris Mason 		 * min_trans parameters.  If it isn't in cache or is too
40363f157a2fSChris Mason 		 * old, skip to the next one.
40373f157a2fSChris Mason 		 */
40383f157a2fSChris Mason 		while (slot < nritems) {
40393f157a2fSChris Mason 			u64 blockptr;
40403f157a2fSChris Mason 			u64 gen;
40413f157a2fSChris Mason 			struct extent_buffer *tmp;
4042e02119d5SChris Mason 			struct btrfs_disk_key disk_key;
4043e02119d5SChris Mason 
40443f157a2fSChris Mason 			blockptr = btrfs_node_blockptr(cur, slot);
40453f157a2fSChris Mason 			gen = btrfs_node_ptr_generation(cur, slot);
40463f157a2fSChris Mason 			if (gen < min_trans) {
40473f157a2fSChris Mason 				slot++;
40483f157a2fSChris Mason 				continue;
40493f157a2fSChris Mason 			}
40503f157a2fSChris Mason 			if (!cache_only)
40513f157a2fSChris Mason 				break;
40523f157a2fSChris Mason 
4053e02119d5SChris Mason 			if (max_key) {
4054e02119d5SChris Mason 				btrfs_node_key(cur, &disk_key, slot);
4055e02119d5SChris Mason 				if (comp_keys(&disk_key, max_key) >= 0) {
4056e02119d5SChris Mason 					ret = 1;
4057e02119d5SChris Mason 					goto out;
4058e02119d5SChris Mason 				}
4059e02119d5SChris Mason 			}
4060e02119d5SChris Mason 
40613f157a2fSChris Mason 			tmp = btrfs_find_tree_block(root, blockptr,
40623f157a2fSChris Mason 					    btrfs_level_size(root, level - 1));
40633f157a2fSChris Mason 
40643f157a2fSChris Mason 			if (tmp && btrfs_buffer_uptodate(tmp, gen)) {
40653f157a2fSChris Mason 				free_extent_buffer(tmp);
40663f157a2fSChris Mason 				break;
40673f157a2fSChris Mason 			}
40683f157a2fSChris Mason 			if (tmp)
40693f157a2fSChris Mason 				free_extent_buffer(tmp);
40703f157a2fSChris Mason 			slot++;
40713f157a2fSChris Mason 		}
4072e02119d5SChris Mason find_next_key:
40733f157a2fSChris Mason 		/*
40743f157a2fSChris Mason 		 * we didn't find a candidate key in this node, walk forward
40753f157a2fSChris Mason 		 * and find another one
40763f157a2fSChris Mason 		 */
40773f157a2fSChris Mason 		if (slot >= nritems) {
4078e02119d5SChris Mason 			path->slots[level] = slot;
4079b4ce94deSChris Mason 			btrfs_set_path_blocking(path);
4080e02119d5SChris Mason 			sret = btrfs_find_next_key(root, path, min_key, level,
40813f157a2fSChris Mason 						  cache_only, min_trans);
4082e02119d5SChris Mason 			if (sret == 0) {
4083b3b4aa74SDavid Sterba 				btrfs_release_path(path);
40843f157a2fSChris Mason 				goto again;
40853f157a2fSChris Mason 			} else {
40863f157a2fSChris Mason 				goto out;
40873f157a2fSChris Mason 			}
40883f157a2fSChris Mason 		}
40893f157a2fSChris Mason 		/* save our key for returning back */
40903f157a2fSChris Mason 		btrfs_node_key_to_cpu(cur, &found_key, slot);
40913f157a2fSChris Mason 		path->slots[level] = slot;
40923f157a2fSChris Mason 		if (level == path->lowest_level) {
40933f157a2fSChris Mason 			ret = 0;
40943f157a2fSChris Mason 			unlock_up(path, level, 1);
40953f157a2fSChris Mason 			goto out;
40963f157a2fSChris Mason 		}
4097b4ce94deSChris Mason 		btrfs_set_path_blocking(path);
40983f157a2fSChris Mason 		cur = read_node_slot(root, cur, slot);
409997d9a8a4STsutomu Itoh 		BUG_ON(!cur);
41003f157a2fSChris Mason 
41013f157a2fSChris Mason 		btrfs_tree_lock(cur);
4102b4ce94deSChris Mason 
41033f157a2fSChris Mason 		path->locks[level - 1] = 1;
41043f157a2fSChris Mason 		path->nodes[level - 1] = cur;
41053f157a2fSChris Mason 		unlock_up(path, level, 1);
41064008c04aSChris Mason 		btrfs_clear_path_blocking(path, NULL);
41073f157a2fSChris Mason 	}
41083f157a2fSChris Mason out:
41093f157a2fSChris Mason 	if (ret == 0)
41103f157a2fSChris Mason 		memcpy(min_key, &found_key, sizeof(found_key));
4111b4ce94deSChris Mason 	btrfs_set_path_blocking(path);
41123f157a2fSChris Mason 	return ret;
41133f157a2fSChris Mason }
41143f157a2fSChris Mason 
41153f157a2fSChris Mason /*
41163f157a2fSChris Mason  * this is similar to btrfs_next_leaf, but does not try to preserve
41173f157a2fSChris Mason  * and fixup the path.  It looks for and returns the next key in the
41183f157a2fSChris Mason  * tree based on the current path and the cache_only and min_trans
41193f157a2fSChris Mason  * parameters.
41203f157a2fSChris Mason  *
41213f157a2fSChris Mason  * 0 is returned if another key is found, < 0 if there are any errors
41223f157a2fSChris Mason  * and 1 is returned if there are no higher keys in the tree
41233f157a2fSChris Mason  *
41243f157a2fSChris Mason  * path->keep_locks should be set to 1 on the search made before
41253f157a2fSChris Mason  * calling this function.
41263f157a2fSChris Mason  */
4127e7a84565SChris Mason int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
412833c66f43SYan Zheng 			struct btrfs_key *key, int level,
41293f157a2fSChris Mason 			int cache_only, u64 min_trans)
4130e7a84565SChris Mason {
4131e7a84565SChris Mason 	int slot;
4132e7a84565SChris Mason 	struct extent_buffer *c;
4133e7a84565SChris Mason 
4134934d375bSChris Mason 	WARN_ON(!path->keep_locks);
4135e7a84565SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
4136e7a84565SChris Mason 		if (!path->nodes[level])
4137e7a84565SChris Mason 			return 1;
4138e7a84565SChris Mason 
4139e7a84565SChris Mason 		slot = path->slots[level] + 1;
4140e7a84565SChris Mason 		c = path->nodes[level];
41413f157a2fSChris Mason next:
4142e7a84565SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
414333c66f43SYan Zheng 			int ret;
414433c66f43SYan Zheng 			int orig_lowest;
414533c66f43SYan Zheng 			struct btrfs_key cur_key;
414633c66f43SYan Zheng 			if (level + 1 >= BTRFS_MAX_LEVEL ||
414733c66f43SYan Zheng 			    !path->nodes[level + 1])
4148e7a84565SChris Mason 				return 1;
414933c66f43SYan Zheng 
415033c66f43SYan Zheng 			if (path->locks[level + 1]) {
415133c66f43SYan Zheng 				level++;
4152e7a84565SChris Mason 				continue;
4153e7a84565SChris Mason 			}
415433c66f43SYan Zheng 
415533c66f43SYan Zheng 			slot = btrfs_header_nritems(c) - 1;
415633c66f43SYan Zheng 			if (level == 0)
415733c66f43SYan Zheng 				btrfs_item_key_to_cpu(c, &cur_key, slot);
415833c66f43SYan Zheng 			else
415933c66f43SYan Zheng 				btrfs_node_key_to_cpu(c, &cur_key, slot);
416033c66f43SYan Zheng 
416133c66f43SYan Zheng 			orig_lowest = path->lowest_level;
4162b3b4aa74SDavid Sterba 			btrfs_release_path(path);
416333c66f43SYan Zheng 			path->lowest_level = level;
416433c66f43SYan Zheng 			ret = btrfs_search_slot(NULL, root, &cur_key, path,
416533c66f43SYan Zheng 						0, 0);
416633c66f43SYan Zheng 			path->lowest_level = orig_lowest;
416733c66f43SYan Zheng 			if (ret < 0)
416833c66f43SYan Zheng 				return ret;
416933c66f43SYan Zheng 
417033c66f43SYan Zheng 			c = path->nodes[level];
417133c66f43SYan Zheng 			slot = path->slots[level];
417233c66f43SYan Zheng 			if (ret == 0)
417333c66f43SYan Zheng 				slot++;
417433c66f43SYan Zheng 			goto next;
417533c66f43SYan Zheng 		}
417633c66f43SYan Zheng 
4177e7a84565SChris Mason 		if (level == 0)
4178e7a84565SChris Mason 			btrfs_item_key_to_cpu(c, key, slot);
41793f157a2fSChris Mason 		else {
41803f157a2fSChris Mason 			u64 blockptr = btrfs_node_blockptr(c, slot);
41813f157a2fSChris Mason 			u64 gen = btrfs_node_ptr_generation(c, slot);
41823f157a2fSChris Mason 
41833f157a2fSChris Mason 			if (cache_only) {
41843f157a2fSChris Mason 				struct extent_buffer *cur;
41853f157a2fSChris Mason 				cur = btrfs_find_tree_block(root, blockptr,
41863f157a2fSChris Mason 					    btrfs_level_size(root, level - 1));
41873f157a2fSChris Mason 				if (!cur || !btrfs_buffer_uptodate(cur, gen)) {
41883f157a2fSChris Mason 					slot++;
41893f157a2fSChris Mason 					if (cur)
41903f157a2fSChris Mason 						free_extent_buffer(cur);
41913f157a2fSChris Mason 					goto next;
41923f157a2fSChris Mason 				}
41933f157a2fSChris Mason 				free_extent_buffer(cur);
41943f157a2fSChris Mason 			}
41953f157a2fSChris Mason 			if (gen < min_trans) {
41963f157a2fSChris Mason 				slot++;
41973f157a2fSChris Mason 				goto next;
41983f157a2fSChris Mason 			}
4199e7a84565SChris Mason 			btrfs_node_key_to_cpu(c, key, slot);
42003f157a2fSChris Mason 		}
4201e7a84565SChris Mason 		return 0;
4202e7a84565SChris Mason 	}
4203e7a84565SChris Mason 	return 1;
4204e7a84565SChris Mason }
4205e7a84565SChris Mason 
42067bb86316SChris Mason /*
4207925baeddSChris Mason  * search the tree again to find a leaf with greater keys
42080f70abe2SChris Mason  * returns 0 if it found something or 1 if there are no greater leaves.
42090f70abe2SChris Mason  * returns < 0 on io errors.
421097571fd0SChris Mason  */
4211234b63a0SChris Mason int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
4212d97e63b6SChris Mason {
4213d97e63b6SChris Mason 	int slot;
42148e73f275SChris Mason 	int level;
42155f39d397SChris Mason 	struct extent_buffer *c;
42168e73f275SChris Mason 	struct extent_buffer *next;
4217925baeddSChris Mason 	struct btrfs_key key;
4218925baeddSChris Mason 	u32 nritems;
4219925baeddSChris Mason 	int ret;
42208e73f275SChris Mason 	int old_spinning = path->leave_spinning;
42218e73f275SChris Mason 	int force_blocking = 0;
4222925baeddSChris Mason 
4223925baeddSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
4224d397712bSChris Mason 	if (nritems == 0)
4225925baeddSChris Mason 		return 1;
4226925baeddSChris Mason 
42278e73f275SChris Mason 	/*
42288e73f275SChris Mason 	 * we take the blocks in an order that upsets lockdep.  Using
42298e73f275SChris Mason 	 * blocking mode is the only way around it.
42308e73f275SChris Mason 	 */
42318e73f275SChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC
42328e73f275SChris Mason 	force_blocking = 1;
42338e73f275SChris Mason #endif
4234925baeddSChris Mason 
42358e73f275SChris Mason 	btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
42368e73f275SChris Mason again:
42378e73f275SChris Mason 	level = 1;
42388e73f275SChris Mason 	next = NULL;
4239b3b4aa74SDavid Sterba 	btrfs_release_path(path);
42408e73f275SChris Mason 
4241a2135011SChris Mason 	path->keep_locks = 1;
42428e73f275SChris Mason 
42438e73f275SChris Mason 	if (!force_blocking)
42448e73f275SChris Mason 		path->leave_spinning = 1;
42458e73f275SChris Mason 
4246925baeddSChris Mason 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4247925baeddSChris Mason 	path->keep_locks = 0;
4248925baeddSChris Mason 
4249925baeddSChris Mason 	if (ret < 0)
4250925baeddSChris Mason 		return ret;
4251925baeddSChris Mason 
4252a2135011SChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
4253168fd7d2SChris Mason 	/*
4254168fd7d2SChris Mason 	 * by releasing the path above we dropped all our locks.  A balance
4255168fd7d2SChris Mason 	 * could have added more items next to the key that used to be
4256168fd7d2SChris Mason 	 * at the very end of the block.  So, check again here and
4257168fd7d2SChris Mason 	 * advance the path if there are now more items available.
4258168fd7d2SChris Mason 	 */
4259a2135011SChris Mason 	if (nritems > 0 && path->slots[0] < nritems - 1) {
4260e457afecSYan Zheng 		if (ret == 0)
4261168fd7d2SChris Mason 			path->slots[0]++;
42628e73f275SChris Mason 		ret = 0;
4263925baeddSChris Mason 		goto done;
4264925baeddSChris Mason 	}
4265d97e63b6SChris Mason 
4266234b63a0SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
42678e73f275SChris Mason 		if (!path->nodes[level]) {
42688e73f275SChris Mason 			ret = 1;
42698e73f275SChris Mason 			goto done;
42708e73f275SChris Mason 		}
42715f39d397SChris Mason 
4272d97e63b6SChris Mason 		slot = path->slots[level] + 1;
4273d97e63b6SChris Mason 		c = path->nodes[level];
42745f39d397SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
4275d97e63b6SChris Mason 			level++;
42768e73f275SChris Mason 			if (level == BTRFS_MAX_LEVEL) {
42778e73f275SChris Mason 				ret = 1;
42788e73f275SChris Mason 				goto done;
42798e73f275SChris Mason 			}
4280d97e63b6SChris Mason 			continue;
4281d97e63b6SChris Mason 		}
42825f39d397SChris Mason 
4283925baeddSChris Mason 		if (next) {
4284925baeddSChris Mason 			btrfs_tree_unlock(next);
42855f39d397SChris Mason 			free_extent_buffer(next);
4286925baeddSChris Mason 		}
42875f39d397SChris Mason 
42888e73f275SChris Mason 		next = c;
42898e73f275SChris Mason 		ret = read_block_for_search(NULL, root, path, &next, level,
42908e73f275SChris Mason 					    slot, &key);
42918e73f275SChris Mason 		if (ret == -EAGAIN)
42928e73f275SChris Mason 			goto again;
42935f39d397SChris Mason 
429476a05b35SChris Mason 		if (ret < 0) {
4295b3b4aa74SDavid Sterba 			btrfs_release_path(path);
429676a05b35SChris Mason 			goto done;
429776a05b35SChris Mason 		}
429876a05b35SChris Mason 
42995cd57b2cSChris Mason 		if (!path->skip_locking) {
43008e73f275SChris Mason 			ret = btrfs_try_spin_lock(next);
43018e73f275SChris Mason 			if (!ret) {
43028e73f275SChris Mason 				btrfs_set_path_blocking(path);
4303925baeddSChris Mason 				btrfs_tree_lock(next);
43048e73f275SChris Mason 				if (!force_blocking)
43058e73f275SChris Mason 					btrfs_clear_path_blocking(path, next);
43068e73f275SChris Mason 			}
43078e73f275SChris Mason 			if (force_blocking)
4308b4ce94deSChris Mason 				btrfs_set_lock_blocking(next);
43095cd57b2cSChris Mason 		}
4310d97e63b6SChris Mason 		break;
4311d97e63b6SChris Mason 	}
4312d97e63b6SChris Mason 	path->slots[level] = slot;
4313d97e63b6SChris Mason 	while (1) {
4314d97e63b6SChris Mason 		level--;
4315d97e63b6SChris Mason 		c = path->nodes[level];
4316925baeddSChris Mason 		if (path->locks[level])
4317925baeddSChris Mason 			btrfs_tree_unlock(c);
43188e73f275SChris Mason 
43195f39d397SChris Mason 		free_extent_buffer(c);
4320d97e63b6SChris Mason 		path->nodes[level] = next;
4321d97e63b6SChris Mason 		path->slots[level] = 0;
4322a74a4b97SChris Mason 		if (!path->skip_locking)
4323925baeddSChris Mason 			path->locks[level] = 1;
43248e73f275SChris Mason 
4325d97e63b6SChris Mason 		if (!level)
4326d97e63b6SChris Mason 			break;
4327b4ce94deSChris Mason 
43288e73f275SChris Mason 		ret = read_block_for_search(NULL, root, path, &next, level,
43298e73f275SChris Mason 					    0, &key);
43308e73f275SChris Mason 		if (ret == -EAGAIN)
43318e73f275SChris Mason 			goto again;
43328e73f275SChris Mason 
433376a05b35SChris Mason 		if (ret < 0) {
4334b3b4aa74SDavid Sterba 			btrfs_release_path(path);
433576a05b35SChris Mason 			goto done;
433676a05b35SChris Mason 		}
433776a05b35SChris Mason 
43385cd57b2cSChris Mason 		if (!path->skip_locking) {
4339b9447ef8SChris Mason 			btrfs_assert_tree_locked(path->nodes[level]);
43408e73f275SChris Mason 			ret = btrfs_try_spin_lock(next);
43418e73f275SChris Mason 			if (!ret) {
43428e73f275SChris Mason 				btrfs_set_path_blocking(path);
4343925baeddSChris Mason 				btrfs_tree_lock(next);
43448e73f275SChris Mason 				if (!force_blocking)
43458e73f275SChris Mason 					btrfs_clear_path_blocking(path, next);
43468e73f275SChris Mason 			}
43478e73f275SChris Mason 			if (force_blocking)
4348b4ce94deSChris Mason 				btrfs_set_lock_blocking(next);
4349d97e63b6SChris Mason 		}
43505cd57b2cSChris Mason 	}
43518e73f275SChris Mason 	ret = 0;
4352925baeddSChris Mason done:
4353925baeddSChris Mason 	unlock_up(path, 0, 1);
43548e73f275SChris Mason 	path->leave_spinning = old_spinning;
43558e73f275SChris Mason 	if (!old_spinning)
43568e73f275SChris Mason 		btrfs_set_path_blocking(path);
43578e73f275SChris Mason 
43588e73f275SChris Mason 	return ret;
4359d97e63b6SChris Mason }
43600b86a832SChris Mason 
43613f157a2fSChris Mason /*
43623f157a2fSChris Mason  * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
43633f157a2fSChris Mason  * searching until it gets past min_objectid or finds an item of 'type'
43643f157a2fSChris Mason  *
43653f157a2fSChris Mason  * returns 0 if something is found, 1 if nothing was found and < 0 on error
43663f157a2fSChris Mason  */
43670b86a832SChris Mason int btrfs_previous_item(struct btrfs_root *root,
43680b86a832SChris Mason 			struct btrfs_path *path, u64 min_objectid,
43690b86a832SChris Mason 			int type)
43700b86a832SChris Mason {
43710b86a832SChris Mason 	struct btrfs_key found_key;
43720b86a832SChris Mason 	struct extent_buffer *leaf;
4373e02119d5SChris Mason 	u32 nritems;
43740b86a832SChris Mason 	int ret;
43750b86a832SChris Mason 
43760b86a832SChris Mason 	while (1) {
43770b86a832SChris Mason 		if (path->slots[0] == 0) {
4378b4ce94deSChris Mason 			btrfs_set_path_blocking(path);
43790b86a832SChris Mason 			ret = btrfs_prev_leaf(root, path);
43800b86a832SChris Mason 			if (ret != 0)
43810b86a832SChris Mason 				return ret;
43820b86a832SChris Mason 		} else {
43830b86a832SChris Mason 			path->slots[0]--;
43840b86a832SChris Mason 		}
43850b86a832SChris Mason 		leaf = path->nodes[0];
4386e02119d5SChris Mason 		nritems = btrfs_header_nritems(leaf);
4387e02119d5SChris Mason 		if (nritems == 0)
4388e02119d5SChris Mason 			return 1;
4389e02119d5SChris Mason 		if (path->slots[0] == nritems)
4390e02119d5SChris Mason 			path->slots[0]--;
4391e02119d5SChris Mason 
43920b86a832SChris Mason 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4393e02119d5SChris Mason 		if (found_key.objectid < min_objectid)
4394e02119d5SChris Mason 			break;
43950a4eefbbSYan Zheng 		if (found_key.type == type)
43960a4eefbbSYan Zheng 			return 0;
4397e02119d5SChris Mason 		if (found_key.objectid == min_objectid &&
4398e02119d5SChris Mason 		    found_key.type < type)
4399e02119d5SChris Mason 			break;
44000b86a832SChris Mason 	}
44010b86a832SChris Mason 	return 1;
44020b86a832SChris Mason }
4403