xref: /openbmc/linux/fs/btrfs/ctree.c (revision e6793769)
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>
21bd989ba3SJan Schmidt #include <linux/rbtree.h>
22eb60ceacSChris Mason #include "ctree.h"
23eb60ceacSChris Mason #include "disk-io.h"
247f5c1516SChris Mason #include "transaction.h"
255f39d397SChris Mason #include "print-tree.h"
26925baeddSChris Mason #include "locking.h"
279a8dd150SChris Mason 
28e089f05cSChris Mason static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
29e089f05cSChris Mason 		      *root, struct btrfs_path *path, int level);
30e089f05cSChris Mason static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
31d4dbff95SChris Mason 		      *root, struct btrfs_key *ins_key,
32cc0c5538SChris Mason 		      struct btrfs_path *path, int data_size, int extend);
335f39d397SChris Mason static int push_node_left(struct btrfs_trans_handle *trans,
345f39d397SChris Mason 			  struct btrfs_root *root, struct extent_buffer *dst,
35971a1f66SChris Mason 			  struct extent_buffer *src, int empty);
365f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans,
375f39d397SChris Mason 			      struct btrfs_root *root,
385f39d397SChris Mason 			      struct extent_buffer *dst_buf,
395f39d397SChris Mason 			      struct extent_buffer *src_buf);
40143bede5SJeff Mahoney static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
41f3ea38daSJan Schmidt 		    struct btrfs_path *path, int level, int slot,
42f3ea38daSJan Schmidt 		    int tree_mod_log);
43f230475eSJan Schmidt static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
44f230475eSJan Schmidt 				 struct extent_buffer *eb);
45f230475eSJan Schmidt struct extent_buffer *read_old_tree_block(struct btrfs_root *root, u64 bytenr,
46f230475eSJan Schmidt 					  u32 blocksize, u64 parent_transid,
47f230475eSJan Schmidt 					  u64 time_seq);
48f230475eSJan Schmidt struct extent_buffer *btrfs_find_old_tree_block(struct btrfs_root *root,
49f230475eSJan Schmidt 						u64 bytenr, u32 blocksize,
50f230475eSJan Schmidt 						u64 time_seq);
51d97e63b6SChris Mason 
522c90e5d6SChris Mason struct btrfs_path *btrfs_alloc_path(void)
532c90e5d6SChris Mason {
54df24a2b9SChris Mason 	struct btrfs_path *path;
55e00f7308SJeff Mahoney 	path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
56df24a2b9SChris Mason 	return path;
572c90e5d6SChris Mason }
582c90e5d6SChris Mason 
59b4ce94deSChris Mason /*
60b4ce94deSChris Mason  * set all locked nodes in the path to blocking locks.  This should
61b4ce94deSChris Mason  * be done before scheduling
62b4ce94deSChris Mason  */
63b4ce94deSChris Mason noinline void btrfs_set_path_blocking(struct btrfs_path *p)
64b4ce94deSChris Mason {
65b4ce94deSChris Mason 	int i;
66b4ce94deSChris Mason 	for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
67bd681513SChris Mason 		if (!p->nodes[i] || !p->locks[i])
68bd681513SChris Mason 			continue;
69bd681513SChris Mason 		btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
70bd681513SChris Mason 		if (p->locks[i] == BTRFS_READ_LOCK)
71bd681513SChris Mason 			p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
72bd681513SChris Mason 		else if (p->locks[i] == BTRFS_WRITE_LOCK)
73bd681513SChris Mason 			p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
74b4ce94deSChris Mason 	}
75b4ce94deSChris Mason }
76b4ce94deSChris Mason 
77b4ce94deSChris Mason /*
78b4ce94deSChris Mason  * reset all the locked nodes in the patch to spinning locks.
794008c04aSChris Mason  *
804008c04aSChris Mason  * held is used to keep lockdep happy, when lockdep is enabled
814008c04aSChris Mason  * we set held to a blocking lock before we go around and
824008c04aSChris Mason  * retake all the spinlocks in the path.  You can safely use NULL
834008c04aSChris Mason  * for held
84b4ce94deSChris Mason  */
854008c04aSChris Mason noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
86bd681513SChris Mason 					struct extent_buffer *held, int held_rw)
87b4ce94deSChris Mason {
88b4ce94deSChris Mason 	int i;
894008c04aSChris Mason 
904008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC
914008c04aSChris Mason 	/* lockdep really cares that we take all of these spinlocks
924008c04aSChris Mason 	 * in the right order.  If any of the locks in the path are not
934008c04aSChris Mason 	 * currently blocking, it is going to complain.  So, make really
944008c04aSChris Mason 	 * really sure by forcing the path to blocking before we clear
954008c04aSChris Mason 	 * the path blocking.
964008c04aSChris Mason 	 */
97bd681513SChris Mason 	if (held) {
98bd681513SChris Mason 		btrfs_set_lock_blocking_rw(held, held_rw);
99bd681513SChris Mason 		if (held_rw == BTRFS_WRITE_LOCK)
100bd681513SChris Mason 			held_rw = BTRFS_WRITE_LOCK_BLOCKING;
101bd681513SChris Mason 		else if (held_rw == BTRFS_READ_LOCK)
102bd681513SChris Mason 			held_rw = BTRFS_READ_LOCK_BLOCKING;
103bd681513SChris Mason 	}
1044008c04aSChris Mason 	btrfs_set_path_blocking(p);
1054008c04aSChris Mason #endif
1064008c04aSChris Mason 
1074008c04aSChris Mason 	for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
108bd681513SChris Mason 		if (p->nodes[i] && p->locks[i]) {
109bd681513SChris Mason 			btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
110bd681513SChris Mason 			if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
111bd681513SChris Mason 				p->locks[i] = BTRFS_WRITE_LOCK;
112bd681513SChris Mason 			else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
113bd681513SChris Mason 				p->locks[i] = BTRFS_READ_LOCK;
114bd681513SChris Mason 		}
115b4ce94deSChris Mason 	}
1164008c04aSChris Mason 
1174008c04aSChris Mason #ifdef CONFIG_DEBUG_LOCK_ALLOC
1184008c04aSChris Mason 	if (held)
119bd681513SChris Mason 		btrfs_clear_lock_blocking_rw(held, held_rw);
1204008c04aSChris Mason #endif
121b4ce94deSChris Mason }
122b4ce94deSChris Mason 
123d352ac68SChris Mason /* this also releases the path */
1242c90e5d6SChris Mason void btrfs_free_path(struct btrfs_path *p)
1252c90e5d6SChris Mason {
126ff175d57SJesper Juhl 	if (!p)
127ff175d57SJesper Juhl 		return;
128b3b4aa74SDavid Sterba 	btrfs_release_path(p);
1292c90e5d6SChris Mason 	kmem_cache_free(btrfs_path_cachep, p);
1302c90e5d6SChris Mason }
1312c90e5d6SChris Mason 
132d352ac68SChris Mason /*
133d352ac68SChris Mason  * path release drops references on the extent buffers in the path
134d352ac68SChris Mason  * and it drops any locks held by this path
135d352ac68SChris Mason  *
136d352ac68SChris Mason  * It is safe to call this on paths that no locks or extent buffers held.
137d352ac68SChris Mason  */
138b3b4aa74SDavid Sterba noinline void btrfs_release_path(struct btrfs_path *p)
139eb60ceacSChris Mason {
140eb60ceacSChris Mason 	int i;
141a2135011SChris Mason 
142234b63a0SChris Mason 	for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
1433f157a2fSChris Mason 		p->slots[i] = 0;
144eb60ceacSChris Mason 		if (!p->nodes[i])
145925baeddSChris Mason 			continue;
146925baeddSChris Mason 		if (p->locks[i]) {
147bd681513SChris Mason 			btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
148925baeddSChris Mason 			p->locks[i] = 0;
149925baeddSChris Mason 		}
1505f39d397SChris Mason 		free_extent_buffer(p->nodes[i]);
1513f157a2fSChris Mason 		p->nodes[i] = NULL;
152eb60ceacSChris Mason 	}
153eb60ceacSChris Mason }
154eb60ceacSChris Mason 
155d352ac68SChris Mason /*
156d352ac68SChris Mason  * safely gets a reference on the root node of a tree.  A lock
157d352ac68SChris Mason  * is not taken, so a concurrent writer may put a different node
158d352ac68SChris Mason  * at the root of the tree.  See btrfs_lock_root_node for the
159d352ac68SChris Mason  * looping required.
160d352ac68SChris Mason  *
161d352ac68SChris Mason  * The extent buffer returned by this has a reference taken, so
162d352ac68SChris Mason  * it won't disappear.  It may stop being the root of the tree
163d352ac68SChris Mason  * at any time because there are no locks held.
164d352ac68SChris Mason  */
165925baeddSChris Mason struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
166925baeddSChris Mason {
167925baeddSChris Mason 	struct extent_buffer *eb;
168240f62c8SChris Mason 
1693083ee2eSJosef Bacik 	while (1) {
170240f62c8SChris Mason 		rcu_read_lock();
171240f62c8SChris Mason 		eb = rcu_dereference(root->node);
1723083ee2eSJosef Bacik 
1733083ee2eSJosef Bacik 		/*
1743083ee2eSJosef Bacik 		 * RCU really hurts here, we could free up the root node because
1753083ee2eSJosef Bacik 		 * it was cow'ed but we may not get the new root node yet so do
1763083ee2eSJosef Bacik 		 * the inc_not_zero dance and if it doesn't work then
1773083ee2eSJosef Bacik 		 * synchronize_rcu and try again.
1783083ee2eSJosef Bacik 		 */
1793083ee2eSJosef Bacik 		if (atomic_inc_not_zero(&eb->refs)) {
180240f62c8SChris Mason 			rcu_read_unlock();
1813083ee2eSJosef Bacik 			break;
1823083ee2eSJosef Bacik 		}
1833083ee2eSJosef Bacik 		rcu_read_unlock();
1843083ee2eSJosef Bacik 		synchronize_rcu();
1853083ee2eSJosef Bacik 	}
186925baeddSChris Mason 	return eb;
187925baeddSChris Mason }
188925baeddSChris Mason 
189d352ac68SChris Mason /* loop around taking references on and locking the root node of the
190d352ac68SChris Mason  * tree until you end up with a lock on the root.  A locked buffer
191d352ac68SChris Mason  * is returned, with a reference held.
192d352ac68SChris Mason  */
193925baeddSChris Mason struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
194925baeddSChris Mason {
195925baeddSChris Mason 	struct extent_buffer *eb;
196925baeddSChris Mason 
197925baeddSChris Mason 	while (1) {
198925baeddSChris Mason 		eb = btrfs_root_node(root);
199925baeddSChris Mason 		btrfs_tree_lock(eb);
200240f62c8SChris Mason 		if (eb == root->node)
201925baeddSChris Mason 			break;
202925baeddSChris Mason 		btrfs_tree_unlock(eb);
203925baeddSChris Mason 		free_extent_buffer(eb);
204925baeddSChris Mason 	}
205925baeddSChris Mason 	return eb;
206925baeddSChris Mason }
207925baeddSChris Mason 
208bd681513SChris Mason /* loop around taking references on and locking the root node of the
209bd681513SChris Mason  * tree until you end up with a lock on the root.  A locked buffer
210bd681513SChris Mason  * is returned, with a reference held.
211bd681513SChris Mason  */
212bd681513SChris Mason struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
213bd681513SChris Mason {
214bd681513SChris Mason 	struct extent_buffer *eb;
215bd681513SChris Mason 
216bd681513SChris Mason 	while (1) {
217bd681513SChris Mason 		eb = btrfs_root_node(root);
218bd681513SChris Mason 		btrfs_tree_read_lock(eb);
219bd681513SChris Mason 		if (eb == root->node)
220bd681513SChris Mason 			break;
221bd681513SChris Mason 		btrfs_tree_read_unlock(eb);
222bd681513SChris Mason 		free_extent_buffer(eb);
223bd681513SChris Mason 	}
224bd681513SChris Mason 	return eb;
225bd681513SChris Mason }
226bd681513SChris Mason 
227d352ac68SChris Mason /* cowonly root (everything not a reference counted cow subvolume), just get
228d352ac68SChris Mason  * put onto a simple dirty list.  transaction.c walks this to make sure they
229d352ac68SChris Mason  * get properly updated on disk.
230d352ac68SChris Mason  */
2310b86a832SChris Mason static void add_root_to_dirty_list(struct btrfs_root *root)
2320b86a832SChris Mason {
233e5846fc6SChris Mason 	spin_lock(&root->fs_info->trans_lock);
2340b86a832SChris Mason 	if (root->track_dirty && list_empty(&root->dirty_list)) {
2350b86a832SChris Mason 		list_add(&root->dirty_list,
2360b86a832SChris Mason 			 &root->fs_info->dirty_cowonly_roots);
2370b86a832SChris Mason 	}
238e5846fc6SChris Mason 	spin_unlock(&root->fs_info->trans_lock);
2390b86a832SChris Mason }
2400b86a832SChris Mason 
241d352ac68SChris Mason /*
242d352ac68SChris Mason  * used by snapshot creation to make a copy of a root for a tree with
243d352ac68SChris Mason  * a given objectid.  The buffer with the new root node is returned in
244d352ac68SChris Mason  * cow_ret, and this func returns zero on success or a negative error code.
245d352ac68SChris Mason  */
246be20aa9dSChris Mason int btrfs_copy_root(struct btrfs_trans_handle *trans,
247be20aa9dSChris Mason 		      struct btrfs_root *root,
248be20aa9dSChris Mason 		      struct extent_buffer *buf,
249be20aa9dSChris Mason 		      struct extent_buffer **cow_ret, u64 new_root_objectid)
250be20aa9dSChris Mason {
251be20aa9dSChris Mason 	struct extent_buffer *cow;
252be20aa9dSChris Mason 	int ret = 0;
253be20aa9dSChris Mason 	int level;
2545d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
255be20aa9dSChris Mason 
256be20aa9dSChris Mason 	WARN_ON(root->ref_cows && trans->transid !=
257be20aa9dSChris Mason 		root->fs_info->running_transaction->transid);
258be20aa9dSChris Mason 	WARN_ON(root->ref_cows && trans->transid != root->last_trans);
259be20aa9dSChris Mason 
260be20aa9dSChris Mason 	level = btrfs_header_level(buf);
2615d4f98a2SYan Zheng 	if (level == 0)
2625d4f98a2SYan Zheng 		btrfs_item_key(buf, &disk_key, 0);
2635d4f98a2SYan Zheng 	else
2645d4f98a2SYan Zheng 		btrfs_node_key(buf, &disk_key, 0);
26531840ae1SZheng Yan 
2665d4f98a2SYan Zheng 	cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
2675d4f98a2SYan Zheng 				     new_root_objectid, &disk_key, level,
2685581a51aSJan Schmidt 				     buf->start, 0);
2695d4f98a2SYan Zheng 	if (IS_ERR(cow))
270be20aa9dSChris Mason 		return PTR_ERR(cow);
271be20aa9dSChris Mason 
272be20aa9dSChris Mason 	copy_extent_buffer(cow, buf, 0, 0, cow->len);
273be20aa9dSChris Mason 	btrfs_set_header_bytenr(cow, cow->start);
274be20aa9dSChris Mason 	btrfs_set_header_generation(cow, trans->transid);
2755d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
2765d4f98a2SYan Zheng 	btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
2775d4f98a2SYan Zheng 				     BTRFS_HEADER_FLAG_RELOC);
2785d4f98a2SYan Zheng 	if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
2795d4f98a2SYan Zheng 		btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
2805d4f98a2SYan Zheng 	else
281be20aa9dSChris Mason 		btrfs_set_header_owner(cow, new_root_objectid);
282be20aa9dSChris Mason 
2832b82032cSYan Zheng 	write_extent_buffer(cow, root->fs_info->fsid,
2842b82032cSYan Zheng 			    (unsigned long)btrfs_header_fsid(cow),
2852b82032cSYan Zheng 			    BTRFS_FSID_SIZE);
2862b82032cSYan Zheng 
287be20aa9dSChris Mason 	WARN_ON(btrfs_header_generation(buf) > trans->transid);
2885d4f98a2SYan Zheng 	if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
28966d7e7f0SArne Jansen 		ret = btrfs_inc_ref(trans, root, cow, 1, 1);
2905d4f98a2SYan Zheng 	else
29166d7e7f0SArne Jansen 		ret = btrfs_inc_ref(trans, root, cow, 0, 1);
2924aec2b52SChris Mason 
293be20aa9dSChris Mason 	if (ret)
294be20aa9dSChris Mason 		return ret;
295be20aa9dSChris Mason 
296be20aa9dSChris Mason 	btrfs_mark_buffer_dirty(cow);
297be20aa9dSChris Mason 	*cow_ret = cow;
298be20aa9dSChris Mason 	return 0;
299be20aa9dSChris Mason }
300be20aa9dSChris Mason 
301bd989ba3SJan Schmidt enum mod_log_op {
302bd989ba3SJan Schmidt 	MOD_LOG_KEY_REPLACE,
303bd989ba3SJan Schmidt 	MOD_LOG_KEY_ADD,
304bd989ba3SJan Schmidt 	MOD_LOG_KEY_REMOVE,
305bd989ba3SJan Schmidt 	MOD_LOG_KEY_REMOVE_WHILE_FREEING,
306bd989ba3SJan Schmidt 	MOD_LOG_KEY_REMOVE_WHILE_MOVING,
307bd989ba3SJan Schmidt 	MOD_LOG_MOVE_KEYS,
308bd989ba3SJan Schmidt 	MOD_LOG_ROOT_REPLACE,
309bd989ba3SJan Schmidt };
310bd989ba3SJan Schmidt 
311bd989ba3SJan Schmidt struct tree_mod_move {
312bd989ba3SJan Schmidt 	int dst_slot;
313bd989ba3SJan Schmidt 	int nr_items;
314bd989ba3SJan Schmidt };
315bd989ba3SJan Schmidt 
316bd989ba3SJan Schmidt struct tree_mod_root {
317bd989ba3SJan Schmidt 	u64 logical;
318bd989ba3SJan Schmidt 	u8 level;
319bd989ba3SJan Schmidt };
320bd989ba3SJan Schmidt 
321bd989ba3SJan Schmidt struct tree_mod_elem {
322bd989ba3SJan Schmidt 	struct rb_node node;
323bd989ba3SJan Schmidt 	u64 index;		/* shifted logical */
324bd989ba3SJan Schmidt 	struct seq_list elem;
325bd989ba3SJan Schmidt 	enum mod_log_op op;
326bd989ba3SJan Schmidt 
327bd989ba3SJan Schmidt 	/* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
328bd989ba3SJan Schmidt 	int slot;
329bd989ba3SJan Schmidt 
330bd989ba3SJan Schmidt 	/* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
331bd989ba3SJan Schmidt 	u64 generation;
332bd989ba3SJan Schmidt 
333bd989ba3SJan Schmidt 	/* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
334bd989ba3SJan Schmidt 	struct btrfs_disk_key key;
335bd989ba3SJan Schmidt 	u64 blockptr;
336bd989ba3SJan Schmidt 
337bd989ba3SJan Schmidt 	/* this is used for op == MOD_LOG_MOVE_KEYS */
338bd989ba3SJan Schmidt 	struct tree_mod_move move;
339bd989ba3SJan Schmidt 
340bd989ba3SJan Schmidt 	/* this is used for op == MOD_LOG_ROOT_REPLACE */
341bd989ba3SJan Schmidt 	struct tree_mod_root old_root;
342bd989ba3SJan Schmidt };
343bd989ba3SJan Schmidt 
344bd989ba3SJan Schmidt static inline void
345bd989ba3SJan Schmidt __get_tree_mod_seq(struct btrfs_fs_info *fs_info, struct seq_list *elem)
346bd989ba3SJan Schmidt {
347bd989ba3SJan Schmidt 	elem->seq = atomic_inc_return(&fs_info->tree_mod_seq);
348bd989ba3SJan Schmidt 	list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
349bd989ba3SJan Schmidt }
350bd989ba3SJan Schmidt 
351bd989ba3SJan Schmidt void btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
352bd989ba3SJan Schmidt 			    struct seq_list *elem)
353bd989ba3SJan Schmidt {
354bd989ba3SJan Schmidt 	elem->flags = 1;
355bd989ba3SJan Schmidt 	spin_lock(&fs_info->tree_mod_seq_lock);
356bd989ba3SJan Schmidt 	__get_tree_mod_seq(fs_info, elem);
357bd989ba3SJan Schmidt 	spin_unlock(&fs_info->tree_mod_seq_lock);
358bd989ba3SJan Schmidt }
359bd989ba3SJan Schmidt 
360bd989ba3SJan Schmidt void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
361bd989ba3SJan Schmidt 			    struct seq_list *elem)
362bd989ba3SJan Schmidt {
363bd989ba3SJan Schmidt 	struct rb_root *tm_root;
364bd989ba3SJan Schmidt 	struct rb_node *node;
365bd989ba3SJan Schmidt 	struct rb_node *next;
366bd989ba3SJan Schmidt 	struct seq_list *cur_elem;
367bd989ba3SJan Schmidt 	struct tree_mod_elem *tm;
368bd989ba3SJan Schmidt 	u64 min_seq = (u64)-1;
369bd989ba3SJan Schmidt 	u64 seq_putting = elem->seq;
370bd989ba3SJan Schmidt 
371bd989ba3SJan Schmidt 	if (!seq_putting)
372bd989ba3SJan Schmidt 		return;
373bd989ba3SJan Schmidt 
374bd989ba3SJan Schmidt 	BUG_ON(!(elem->flags & 1));
375bd989ba3SJan Schmidt 	spin_lock(&fs_info->tree_mod_seq_lock);
376bd989ba3SJan Schmidt 	list_del(&elem->list);
377bd989ba3SJan Schmidt 
378bd989ba3SJan Schmidt 	list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
379bd989ba3SJan Schmidt 		if ((cur_elem->flags & 1) && cur_elem->seq < min_seq) {
380bd989ba3SJan Schmidt 			if (seq_putting > cur_elem->seq) {
381bd989ba3SJan Schmidt 				/*
382bd989ba3SJan Schmidt 				 * blocker with lower sequence number exists, we
383bd989ba3SJan Schmidt 				 * cannot remove anything from the log
384bd989ba3SJan Schmidt 				 */
385bd989ba3SJan Schmidt 				goto out;
386bd989ba3SJan Schmidt 			}
387bd989ba3SJan Schmidt 			min_seq = cur_elem->seq;
388bd989ba3SJan Schmidt 		}
389bd989ba3SJan Schmidt 	}
390bd989ba3SJan Schmidt 
391bd989ba3SJan Schmidt 	/*
392bd989ba3SJan Schmidt 	 * anything that's lower than the lowest existing (read: blocked)
393bd989ba3SJan Schmidt 	 * sequence number can be removed from the tree.
394bd989ba3SJan Schmidt 	 */
395bd989ba3SJan Schmidt 	write_lock(&fs_info->tree_mod_log_lock);
396bd989ba3SJan Schmidt 	tm_root = &fs_info->tree_mod_log;
397bd989ba3SJan Schmidt 	for (node = rb_first(tm_root); node; node = next) {
398bd989ba3SJan Schmidt 		next = rb_next(node);
399bd989ba3SJan Schmidt 		tm = container_of(node, struct tree_mod_elem, node);
400bd989ba3SJan Schmidt 		if (tm->elem.seq > min_seq)
401bd989ba3SJan Schmidt 			continue;
402bd989ba3SJan Schmidt 		rb_erase(node, tm_root);
403bd989ba3SJan Schmidt 		list_del(&tm->elem.list);
404bd989ba3SJan Schmidt 		kfree(tm);
405bd989ba3SJan Schmidt 	}
406bd989ba3SJan Schmidt 	write_unlock(&fs_info->tree_mod_log_lock);
407bd989ba3SJan Schmidt out:
408bd989ba3SJan Schmidt 	spin_unlock(&fs_info->tree_mod_seq_lock);
409bd989ba3SJan Schmidt }
410bd989ba3SJan Schmidt 
411bd989ba3SJan Schmidt /*
412bd989ba3SJan Schmidt  * key order of the log:
413bd989ba3SJan Schmidt  *       index -> sequence
414bd989ba3SJan Schmidt  *
415bd989ba3SJan Schmidt  * the index is the shifted logical of the *new* root node for root replace
416bd989ba3SJan Schmidt  * operations, or the shifted logical of the affected block for all other
417bd989ba3SJan Schmidt  * operations.
418bd989ba3SJan Schmidt  */
419bd989ba3SJan Schmidt static noinline int
420bd989ba3SJan Schmidt __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
421bd989ba3SJan Schmidt {
422bd989ba3SJan Schmidt 	struct rb_root *tm_root;
423bd989ba3SJan Schmidt 	struct rb_node **new;
424bd989ba3SJan Schmidt 	struct rb_node *parent = NULL;
425bd989ba3SJan Schmidt 	struct tree_mod_elem *cur;
426bd989ba3SJan Schmidt 	int ret = 0;
427bd989ba3SJan Schmidt 
428bd989ba3SJan Schmidt 	BUG_ON(!tm || !tm->elem.seq);
429bd989ba3SJan Schmidt 
430bd989ba3SJan Schmidt 	write_lock(&fs_info->tree_mod_log_lock);
431bd989ba3SJan Schmidt 	tm_root = &fs_info->tree_mod_log;
432bd989ba3SJan Schmidt 	new = &tm_root->rb_node;
433bd989ba3SJan Schmidt 	while (*new) {
434bd989ba3SJan Schmidt 		cur = container_of(*new, struct tree_mod_elem, node);
435bd989ba3SJan Schmidt 		parent = *new;
436bd989ba3SJan Schmidt 		if (cur->index < tm->index)
437bd989ba3SJan Schmidt 			new = &((*new)->rb_left);
438bd989ba3SJan Schmidt 		else if (cur->index > tm->index)
439bd989ba3SJan Schmidt 			new = &((*new)->rb_right);
440bd989ba3SJan Schmidt 		else if (cur->elem.seq < tm->elem.seq)
441bd989ba3SJan Schmidt 			new = &((*new)->rb_left);
442bd989ba3SJan Schmidt 		else if (cur->elem.seq > tm->elem.seq)
443bd989ba3SJan Schmidt 			new = &((*new)->rb_right);
444bd989ba3SJan Schmidt 		else {
445bd989ba3SJan Schmidt 			kfree(tm);
446bd989ba3SJan Schmidt 			ret = -EEXIST;
447bd989ba3SJan Schmidt 			goto unlock;
448bd989ba3SJan Schmidt 		}
449bd989ba3SJan Schmidt 	}
450bd989ba3SJan Schmidt 
451bd989ba3SJan Schmidt 	rb_link_node(&tm->node, parent, new);
452bd989ba3SJan Schmidt 	rb_insert_color(&tm->node, tm_root);
453bd989ba3SJan Schmidt unlock:
454bd989ba3SJan Schmidt 	write_unlock(&fs_info->tree_mod_log_lock);
455bd989ba3SJan Schmidt 	return ret;
456bd989ba3SJan Schmidt }
457bd989ba3SJan Schmidt 
458e9b7fd4dSJan Schmidt static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
459e9b7fd4dSJan Schmidt 				    struct extent_buffer *eb) {
460e9b7fd4dSJan Schmidt 	smp_mb();
461e9b7fd4dSJan Schmidt 	if (list_empty(&(fs_info)->tree_mod_seq_list))
462e9b7fd4dSJan Schmidt 		return 1;
463e9b7fd4dSJan Schmidt 	if (!eb)
464e9b7fd4dSJan Schmidt 		return 0;
465e9b7fd4dSJan Schmidt 	if (btrfs_header_level(eb) == 0)
466e9b7fd4dSJan Schmidt 		return 1;
467e9b7fd4dSJan Schmidt 	return 0;
468e9b7fd4dSJan Schmidt }
469e9b7fd4dSJan Schmidt 
4703310c36eSJan Schmidt /*
4713310c36eSJan Schmidt  * This allocates memory and gets a tree modification sequence number when
4723310c36eSJan Schmidt  * needed.
4733310c36eSJan Schmidt  *
4743310c36eSJan Schmidt  * Returns 0 when no sequence number is needed, < 0 on error.
4753310c36eSJan Schmidt  * Returns 1 when a sequence number was added. In this case,
4763310c36eSJan Schmidt  * fs_info->tree_mod_seq_lock was acquired and must be released by the caller
4773310c36eSJan Schmidt  * after inserting into the rb tree.
4783310c36eSJan Schmidt  */
479926dd8a6SJan Schmidt static inline int tree_mod_alloc(struct btrfs_fs_info *fs_info, gfp_t flags,
480bd989ba3SJan Schmidt 				 struct tree_mod_elem **tm_ret)
481bd989ba3SJan Schmidt {
482bd989ba3SJan Schmidt 	struct tree_mod_elem *tm;
483926dd8a6SJan Schmidt 	int seq;
484bd989ba3SJan Schmidt 
485e9b7fd4dSJan Schmidt 	if (tree_mod_dont_log(fs_info, NULL))
486bd989ba3SJan Schmidt 		return 0;
487bd989ba3SJan Schmidt 
488bd989ba3SJan Schmidt 	tm = *tm_ret = kzalloc(sizeof(*tm), flags);
489bd989ba3SJan Schmidt 	if (!tm)
490bd989ba3SJan Schmidt 		return -ENOMEM;
491bd989ba3SJan Schmidt 
492926dd8a6SJan Schmidt 	tm->elem.flags = 0;
493926dd8a6SJan Schmidt 	spin_lock(&fs_info->tree_mod_seq_lock);
494926dd8a6SJan Schmidt 	if (list_empty(&fs_info->tree_mod_seq_list)) {
495926dd8a6SJan Schmidt 		/*
496926dd8a6SJan Schmidt 		 * someone emptied the list while we were waiting for the lock.
497926dd8a6SJan Schmidt 		 * we must not add to the list, because no blocker exists. items
498926dd8a6SJan Schmidt 		 * are removed from the list only when the existing blocker is
499926dd8a6SJan Schmidt 		 * removed from the list.
500926dd8a6SJan Schmidt 		 */
501926dd8a6SJan Schmidt 		kfree(tm);
502926dd8a6SJan Schmidt 		seq = 0;
5033310c36eSJan Schmidt 		spin_unlock(&fs_info->tree_mod_seq_lock);
504926dd8a6SJan Schmidt 	} else {
505bd989ba3SJan Schmidt 		__get_tree_mod_seq(fs_info, &tm->elem);
506bd989ba3SJan Schmidt 		seq = tm->elem.seq;
507926dd8a6SJan Schmidt 	}
508bd989ba3SJan Schmidt 
509bd989ba3SJan Schmidt 	return seq;
510bd989ba3SJan Schmidt }
511bd989ba3SJan Schmidt 
512bd989ba3SJan Schmidt static noinline int
513bd989ba3SJan Schmidt tree_mod_log_insert_key_mask(struct btrfs_fs_info *fs_info,
514bd989ba3SJan Schmidt 			     struct extent_buffer *eb, int slot,
515bd989ba3SJan Schmidt 			     enum mod_log_op op, gfp_t flags)
516bd989ba3SJan Schmidt {
517bd989ba3SJan Schmidt 	struct tree_mod_elem *tm;
518bd989ba3SJan Schmidt 	int ret;
519bd989ba3SJan Schmidt 
520bd989ba3SJan Schmidt 	ret = tree_mod_alloc(fs_info, flags, &tm);
521bd989ba3SJan Schmidt 	if (ret <= 0)
522bd989ba3SJan Schmidt 		return ret;
523bd989ba3SJan Schmidt 
524bd989ba3SJan Schmidt 	tm->index = eb->start >> PAGE_CACHE_SHIFT;
525bd989ba3SJan Schmidt 	if (op != MOD_LOG_KEY_ADD) {
526bd989ba3SJan Schmidt 		btrfs_node_key(eb, &tm->key, slot);
527bd989ba3SJan Schmidt 		tm->blockptr = btrfs_node_blockptr(eb, slot);
528bd989ba3SJan Schmidt 	}
529bd989ba3SJan Schmidt 	tm->op = op;
530bd989ba3SJan Schmidt 	tm->slot = slot;
531bd989ba3SJan Schmidt 	tm->generation = btrfs_node_ptr_generation(eb, slot);
532bd989ba3SJan Schmidt 
5333310c36eSJan Schmidt 	ret = __tree_mod_log_insert(fs_info, tm);
5343310c36eSJan Schmidt 	spin_unlock(&fs_info->tree_mod_seq_lock);
5353310c36eSJan Schmidt 	return ret;
536bd989ba3SJan Schmidt }
537bd989ba3SJan Schmidt 
538bd989ba3SJan Schmidt static noinline int
539bd989ba3SJan Schmidt tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
540bd989ba3SJan Schmidt 			int slot, enum mod_log_op op)
541bd989ba3SJan Schmidt {
542bd989ba3SJan Schmidt 	return tree_mod_log_insert_key_mask(fs_info, eb, slot, op, GFP_NOFS);
543bd989ba3SJan Schmidt }
544bd989ba3SJan Schmidt 
545bd989ba3SJan Schmidt static noinline int
546bd989ba3SJan Schmidt tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
547bd989ba3SJan Schmidt 			 struct extent_buffer *eb, int dst_slot, int src_slot,
548bd989ba3SJan Schmidt 			 int nr_items, gfp_t flags)
549bd989ba3SJan Schmidt {
550bd989ba3SJan Schmidt 	struct tree_mod_elem *tm;
551bd989ba3SJan Schmidt 	int ret;
552bd989ba3SJan Schmidt 	int i;
553bd989ba3SJan Schmidt 
554f395694cSJan Schmidt 	if (tree_mod_dont_log(fs_info, eb))
555f395694cSJan Schmidt 		return 0;
556bd989ba3SJan Schmidt 
557bd989ba3SJan Schmidt 	for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
558bd989ba3SJan Schmidt 		ret = tree_mod_log_insert_key(fs_info, eb, i + dst_slot,
559bd989ba3SJan Schmidt 					      MOD_LOG_KEY_REMOVE_WHILE_MOVING);
560bd989ba3SJan Schmidt 		BUG_ON(ret < 0);
561bd989ba3SJan Schmidt 	}
562bd989ba3SJan Schmidt 
563f395694cSJan Schmidt 	ret = tree_mod_alloc(fs_info, flags, &tm);
564f395694cSJan Schmidt 	if (ret <= 0)
565f395694cSJan Schmidt 		return ret;
566f395694cSJan Schmidt 
567bd989ba3SJan Schmidt 	tm->index = eb->start >> PAGE_CACHE_SHIFT;
568bd989ba3SJan Schmidt 	tm->slot = src_slot;
569bd989ba3SJan Schmidt 	tm->move.dst_slot = dst_slot;
570bd989ba3SJan Schmidt 	tm->move.nr_items = nr_items;
571bd989ba3SJan Schmidt 	tm->op = MOD_LOG_MOVE_KEYS;
572bd989ba3SJan Schmidt 
5733310c36eSJan Schmidt 	ret = __tree_mod_log_insert(fs_info, tm);
5743310c36eSJan Schmidt 	spin_unlock(&fs_info->tree_mod_seq_lock);
5753310c36eSJan Schmidt 	return ret;
576bd989ba3SJan Schmidt }
577bd989ba3SJan Schmidt 
578bd989ba3SJan Schmidt static noinline int
579bd989ba3SJan Schmidt tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
580bd989ba3SJan Schmidt 			 struct extent_buffer *old_root,
581bd989ba3SJan Schmidt 			 struct extent_buffer *new_root, gfp_t flags)
582bd989ba3SJan Schmidt {
583bd989ba3SJan Schmidt 	struct tree_mod_elem *tm;
584bd989ba3SJan Schmidt 	int ret;
585bd989ba3SJan Schmidt 
586bd989ba3SJan Schmidt 	ret = tree_mod_alloc(fs_info, flags, &tm);
587bd989ba3SJan Schmidt 	if (ret <= 0)
588bd989ba3SJan Schmidt 		return ret;
589bd989ba3SJan Schmidt 
590bd989ba3SJan Schmidt 	tm->index = new_root->start >> PAGE_CACHE_SHIFT;
591bd989ba3SJan Schmidt 	tm->old_root.logical = old_root->start;
592bd989ba3SJan Schmidt 	tm->old_root.level = btrfs_header_level(old_root);
593bd989ba3SJan Schmidt 	tm->generation = btrfs_header_generation(old_root);
594bd989ba3SJan Schmidt 	tm->op = MOD_LOG_ROOT_REPLACE;
595bd989ba3SJan Schmidt 
5963310c36eSJan Schmidt 	ret = __tree_mod_log_insert(fs_info, tm);
5973310c36eSJan Schmidt 	spin_unlock(&fs_info->tree_mod_seq_lock);
5983310c36eSJan Schmidt 	return ret;
599bd989ba3SJan Schmidt }
600bd989ba3SJan Schmidt 
601bd989ba3SJan Schmidt static struct tree_mod_elem *
602bd989ba3SJan Schmidt __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
603bd989ba3SJan Schmidt 		      int smallest)
604bd989ba3SJan Schmidt {
605bd989ba3SJan Schmidt 	struct rb_root *tm_root;
606bd989ba3SJan Schmidt 	struct rb_node *node;
607bd989ba3SJan Schmidt 	struct tree_mod_elem *cur = NULL;
608bd989ba3SJan Schmidt 	struct tree_mod_elem *found = NULL;
609bd989ba3SJan Schmidt 	u64 index = start >> PAGE_CACHE_SHIFT;
610bd989ba3SJan Schmidt 
611bd989ba3SJan Schmidt 	read_lock(&fs_info->tree_mod_log_lock);
612bd989ba3SJan Schmidt 	tm_root = &fs_info->tree_mod_log;
613bd989ba3SJan Schmidt 	node = tm_root->rb_node;
614bd989ba3SJan Schmidt 	while (node) {
615bd989ba3SJan Schmidt 		cur = container_of(node, struct tree_mod_elem, node);
616bd989ba3SJan Schmidt 		if (cur->index < index) {
617bd989ba3SJan Schmidt 			node = node->rb_left;
618bd989ba3SJan Schmidt 		} else if (cur->index > index) {
619bd989ba3SJan Schmidt 			node = node->rb_right;
620bd989ba3SJan Schmidt 		} else if (cur->elem.seq < min_seq) {
621bd989ba3SJan Schmidt 			node = node->rb_left;
622bd989ba3SJan Schmidt 		} else if (!smallest) {
623bd989ba3SJan Schmidt 			/* we want the node with the highest seq */
624bd989ba3SJan Schmidt 			if (found)
625bd989ba3SJan Schmidt 				BUG_ON(found->elem.seq > cur->elem.seq);
626bd989ba3SJan Schmidt 			found = cur;
627bd989ba3SJan Schmidt 			node = node->rb_left;
628bd989ba3SJan Schmidt 		} else if (cur->elem.seq > min_seq) {
629bd989ba3SJan Schmidt 			/* we want the node with the smallest seq */
630bd989ba3SJan Schmidt 			if (found)
631bd989ba3SJan Schmidt 				BUG_ON(found->elem.seq < cur->elem.seq);
632bd989ba3SJan Schmidt 			found = cur;
633bd989ba3SJan Schmidt 			node = node->rb_right;
634bd989ba3SJan Schmidt 		} else {
635bd989ba3SJan Schmidt 			found = cur;
636bd989ba3SJan Schmidt 			break;
637bd989ba3SJan Schmidt 		}
638bd989ba3SJan Schmidt 	}
639bd989ba3SJan Schmidt 	read_unlock(&fs_info->tree_mod_log_lock);
640bd989ba3SJan Schmidt 
641bd989ba3SJan Schmidt 	return found;
642bd989ba3SJan Schmidt }
643bd989ba3SJan Schmidt 
644bd989ba3SJan Schmidt /*
645bd989ba3SJan Schmidt  * this returns the element from the log with the smallest time sequence
646bd989ba3SJan Schmidt  * value that's in the log (the oldest log item). any element with a time
647bd989ba3SJan Schmidt  * sequence lower than min_seq will be ignored.
648bd989ba3SJan Schmidt  */
649bd989ba3SJan Schmidt static struct tree_mod_elem *
650bd989ba3SJan Schmidt tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
651bd989ba3SJan Schmidt 			   u64 min_seq)
652bd989ba3SJan Schmidt {
653bd989ba3SJan Schmidt 	return __tree_mod_log_search(fs_info, start, min_seq, 1);
654bd989ba3SJan Schmidt }
655bd989ba3SJan Schmidt 
656bd989ba3SJan Schmidt /*
657bd989ba3SJan Schmidt  * this returns the element from the log with the largest time sequence
658bd989ba3SJan Schmidt  * value that's in the log (the most recent log item). any element with
659bd989ba3SJan Schmidt  * a time sequence lower than min_seq will be ignored.
660bd989ba3SJan Schmidt  */
661bd989ba3SJan Schmidt static struct tree_mod_elem *
662bd989ba3SJan Schmidt tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
663bd989ba3SJan Schmidt {
664bd989ba3SJan Schmidt 	return __tree_mod_log_search(fs_info, start, min_seq, 0);
665bd989ba3SJan Schmidt }
666bd989ba3SJan Schmidt 
667bd989ba3SJan Schmidt static inline void
668bd989ba3SJan Schmidt tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
669bd989ba3SJan Schmidt 		     struct extent_buffer *src, unsigned long dst_offset,
670bd989ba3SJan Schmidt 		     unsigned long src_offset, int nr_items)
671bd989ba3SJan Schmidt {
672bd989ba3SJan Schmidt 	int ret;
673bd989ba3SJan Schmidt 	int i;
674bd989ba3SJan Schmidt 
675e9b7fd4dSJan Schmidt 	if (tree_mod_dont_log(fs_info, NULL))
676bd989ba3SJan Schmidt 		return;
677bd989ba3SJan Schmidt 
678bd989ba3SJan Schmidt 	if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
679bd989ba3SJan Schmidt 		return;
680bd989ba3SJan Schmidt 
681bd989ba3SJan Schmidt 	/* speed this up by single seq for all operations? */
682bd989ba3SJan Schmidt 	for (i = 0; i < nr_items; i++) {
683bd989ba3SJan Schmidt 		ret = tree_mod_log_insert_key(fs_info, src, i + src_offset,
684bd989ba3SJan Schmidt 					      MOD_LOG_KEY_REMOVE);
685bd989ba3SJan Schmidt 		BUG_ON(ret < 0);
686bd989ba3SJan Schmidt 		ret = tree_mod_log_insert_key(fs_info, dst, i + dst_offset,
687bd989ba3SJan Schmidt 					      MOD_LOG_KEY_ADD);
688bd989ba3SJan Schmidt 		BUG_ON(ret < 0);
689bd989ba3SJan Schmidt 	}
690bd989ba3SJan Schmidt }
691bd989ba3SJan Schmidt 
692bd989ba3SJan Schmidt static inline void
693bd989ba3SJan Schmidt tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
694bd989ba3SJan Schmidt 		     int dst_offset, int src_offset, int nr_items)
695bd989ba3SJan Schmidt {
696bd989ba3SJan Schmidt 	int ret;
697bd989ba3SJan Schmidt 	ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
698bd989ba3SJan Schmidt 				       nr_items, GFP_NOFS);
699bd989ba3SJan Schmidt 	BUG_ON(ret < 0);
700bd989ba3SJan Schmidt }
701bd989ba3SJan Schmidt 
702bd989ba3SJan Schmidt static inline void
703bd989ba3SJan Schmidt tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
704bd989ba3SJan Schmidt 			  struct extent_buffer *eb,
705bd989ba3SJan Schmidt 			  struct btrfs_disk_key *disk_key, int slot, int atomic)
706bd989ba3SJan Schmidt {
707bd989ba3SJan Schmidt 	int ret;
708bd989ba3SJan Schmidt 
709bd989ba3SJan Schmidt 	ret = tree_mod_log_insert_key_mask(fs_info, eb, slot,
710bd989ba3SJan Schmidt 					   MOD_LOG_KEY_REPLACE,
711bd989ba3SJan Schmidt 					   atomic ? GFP_ATOMIC : GFP_NOFS);
712bd989ba3SJan Schmidt 	BUG_ON(ret < 0);
713bd989ba3SJan Schmidt }
714bd989ba3SJan Schmidt 
715bd989ba3SJan Schmidt static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
716bd989ba3SJan Schmidt 				 struct extent_buffer *eb)
717bd989ba3SJan Schmidt {
718bd989ba3SJan Schmidt 	int i;
719bd989ba3SJan Schmidt 	int ret;
720bd989ba3SJan Schmidt 	u32 nritems;
721bd989ba3SJan Schmidt 
722e9b7fd4dSJan Schmidt 	if (tree_mod_dont_log(fs_info, eb))
723bd989ba3SJan Schmidt 		return;
724bd989ba3SJan Schmidt 
725bd989ba3SJan Schmidt 	nritems = btrfs_header_nritems(eb);
726bd989ba3SJan Schmidt 	for (i = nritems - 1; i >= 0; i--) {
727bd989ba3SJan Schmidt 		ret = tree_mod_log_insert_key(fs_info, eb, i,
728bd989ba3SJan Schmidt 					      MOD_LOG_KEY_REMOVE_WHILE_FREEING);
729bd989ba3SJan Schmidt 		BUG_ON(ret < 0);
730bd989ba3SJan Schmidt 	}
731bd989ba3SJan Schmidt }
732bd989ba3SJan Schmidt 
733bd989ba3SJan Schmidt static inline void
734bd989ba3SJan Schmidt tree_mod_log_set_root_pointer(struct btrfs_root *root,
735bd989ba3SJan Schmidt 			      struct extent_buffer *new_root_node)
736bd989ba3SJan Schmidt {
737bd989ba3SJan Schmidt 	int ret;
738bd989ba3SJan Schmidt 	tree_mod_log_free_eb(root->fs_info, root->node);
739bd989ba3SJan Schmidt 	ret = tree_mod_log_insert_root(root->fs_info, root->node,
740bd989ba3SJan Schmidt 				       new_root_node, GFP_NOFS);
741bd989ba3SJan Schmidt 	BUG_ON(ret < 0);
742bd989ba3SJan Schmidt }
743bd989ba3SJan Schmidt 
744d352ac68SChris Mason /*
7455d4f98a2SYan Zheng  * check if the tree block can be shared by multiple trees
7465d4f98a2SYan Zheng  */
7475d4f98a2SYan Zheng int btrfs_block_can_be_shared(struct btrfs_root *root,
7485d4f98a2SYan Zheng 			      struct extent_buffer *buf)
7495d4f98a2SYan Zheng {
7505d4f98a2SYan Zheng 	/*
7515d4f98a2SYan Zheng 	 * Tree blocks not in refernece counted trees and tree roots
7525d4f98a2SYan Zheng 	 * are never shared. If a block was allocated after the last
7535d4f98a2SYan Zheng 	 * snapshot and the block was not allocated by tree relocation,
7545d4f98a2SYan Zheng 	 * we know the block is not shared.
7555d4f98a2SYan Zheng 	 */
7565d4f98a2SYan Zheng 	if (root->ref_cows &&
7575d4f98a2SYan Zheng 	    buf != root->node && buf != root->commit_root &&
7585d4f98a2SYan Zheng 	    (btrfs_header_generation(buf) <=
7595d4f98a2SYan Zheng 	     btrfs_root_last_snapshot(&root->root_item) ||
7605d4f98a2SYan Zheng 	     btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
7615d4f98a2SYan Zheng 		return 1;
7625d4f98a2SYan Zheng #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
7635d4f98a2SYan Zheng 	if (root->ref_cows &&
7645d4f98a2SYan Zheng 	    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
7655d4f98a2SYan Zheng 		return 1;
7665d4f98a2SYan Zheng #endif
7675d4f98a2SYan Zheng 	return 0;
7685d4f98a2SYan Zheng }
7695d4f98a2SYan Zheng 
7705d4f98a2SYan Zheng static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
7715d4f98a2SYan Zheng 				       struct btrfs_root *root,
7725d4f98a2SYan Zheng 				       struct extent_buffer *buf,
773f0486c68SYan, Zheng 				       struct extent_buffer *cow,
774f0486c68SYan, Zheng 				       int *last_ref)
7755d4f98a2SYan Zheng {
7765d4f98a2SYan Zheng 	u64 refs;
7775d4f98a2SYan Zheng 	u64 owner;
7785d4f98a2SYan Zheng 	u64 flags;
7795d4f98a2SYan Zheng 	u64 new_flags = 0;
7805d4f98a2SYan Zheng 	int ret;
7815d4f98a2SYan Zheng 
7825d4f98a2SYan Zheng 	/*
7835d4f98a2SYan Zheng 	 * Backrefs update rules:
7845d4f98a2SYan Zheng 	 *
7855d4f98a2SYan Zheng 	 * Always use full backrefs for extent pointers in tree block
7865d4f98a2SYan Zheng 	 * allocated by tree relocation.
7875d4f98a2SYan Zheng 	 *
7885d4f98a2SYan Zheng 	 * If a shared tree block is no longer referenced by its owner
7895d4f98a2SYan Zheng 	 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
7905d4f98a2SYan Zheng 	 * use full backrefs for extent pointers in tree block.
7915d4f98a2SYan Zheng 	 *
7925d4f98a2SYan Zheng 	 * If a tree block is been relocating
7935d4f98a2SYan Zheng 	 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
7945d4f98a2SYan Zheng 	 * use full backrefs for extent pointers in tree block.
7955d4f98a2SYan Zheng 	 * The reason for this is some operations (such as drop tree)
7965d4f98a2SYan Zheng 	 * are only allowed for blocks use full backrefs.
7975d4f98a2SYan Zheng 	 */
7985d4f98a2SYan Zheng 
7995d4f98a2SYan Zheng 	if (btrfs_block_can_be_shared(root, buf)) {
8005d4f98a2SYan Zheng 		ret = btrfs_lookup_extent_info(trans, root, buf->start,
8015d4f98a2SYan Zheng 					       buf->len, &refs, &flags);
802be1a5564SMark Fasheh 		if (ret)
803be1a5564SMark Fasheh 			return ret;
804e5df9573SMark Fasheh 		if (refs == 0) {
805e5df9573SMark Fasheh 			ret = -EROFS;
806e5df9573SMark Fasheh 			btrfs_std_error(root->fs_info, ret);
807e5df9573SMark Fasheh 			return ret;
808e5df9573SMark Fasheh 		}
8095d4f98a2SYan Zheng 	} else {
8105d4f98a2SYan Zheng 		refs = 1;
8115d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
8125d4f98a2SYan Zheng 		    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
8135d4f98a2SYan Zheng 			flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
8145d4f98a2SYan Zheng 		else
8155d4f98a2SYan Zheng 			flags = 0;
8165d4f98a2SYan Zheng 	}
8175d4f98a2SYan Zheng 
8185d4f98a2SYan Zheng 	owner = btrfs_header_owner(buf);
8195d4f98a2SYan Zheng 	BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
8205d4f98a2SYan Zheng 	       !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
8215d4f98a2SYan Zheng 
8225d4f98a2SYan Zheng 	if (refs > 1) {
8235d4f98a2SYan Zheng 		if ((owner == root->root_key.objectid ||
8245d4f98a2SYan Zheng 		     root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
8255d4f98a2SYan Zheng 		    !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
82666d7e7f0SArne Jansen 			ret = btrfs_inc_ref(trans, root, buf, 1, 1);
82779787eaaSJeff Mahoney 			BUG_ON(ret); /* -ENOMEM */
8285d4f98a2SYan Zheng 
8295d4f98a2SYan Zheng 			if (root->root_key.objectid ==
8305d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID) {
83166d7e7f0SArne Jansen 				ret = btrfs_dec_ref(trans, root, buf, 0, 1);
83279787eaaSJeff Mahoney 				BUG_ON(ret); /* -ENOMEM */
83366d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 1, 1);
83479787eaaSJeff Mahoney 				BUG_ON(ret); /* -ENOMEM */
8355d4f98a2SYan Zheng 			}
8365d4f98a2SYan Zheng 			new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
8375d4f98a2SYan Zheng 		} else {
8385d4f98a2SYan Zheng 
8395d4f98a2SYan Zheng 			if (root->root_key.objectid ==
8405d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID)
84166d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 1, 1);
8425d4f98a2SYan Zheng 			else
84366d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 0, 1);
84479787eaaSJeff Mahoney 			BUG_ON(ret); /* -ENOMEM */
8455d4f98a2SYan Zheng 		}
8465d4f98a2SYan Zheng 		if (new_flags != 0) {
8475d4f98a2SYan Zheng 			ret = btrfs_set_disk_extent_flags(trans, root,
8485d4f98a2SYan Zheng 							  buf->start,
8495d4f98a2SYan Zheng 							  buf->len,
8505d4f98a2SYan Zheng 							  new_flags, 0);
851be1a5564SMark Fasheh 			if (ret)
852be1a5564SMark Fasheh 				return ret;
8535d4f98a2SYan Zheng 		}
8545d4f98a2SYan Zheng 	} else {
8555d4f98a2SYan Zheng 		if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
8565d4f98a2SYan Zheng 			if (root->root_key.objectid ==
8575d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID)
85866d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 1, 1);
8595d4f98a2SYan Zheng 			else
86066d7e7f0SArne Jansen 				ret = btrfs_inc_ref(trans, root, cow, 0, 1);
86179787eaaSJeff Mahoney 			BUG_ON(ret); /* -ENOMEM */
86266d7e7f0SArne Jansen 			ret = btrfs_dec_ref(trans, root, buf, 1, 1);
86379787eaaSJeff Mahoney 			BUG_ON(ret); /* -ENOMEM */
8645d4f98a2SYan Zheng 		}
865f230475eSJan Schmidt 		/*
866f230475eSJan Schmidt 		 * don't log freeing in case we're freeing the root node, this
867f230475eSJan Schmidt 		 * is done by tree_mod_log_set_root_pointer later
868f230475eSJan Schmidt 		 */
869f230475eSJan Schmidt 		if (buf != root->node && btrfs_header_level(buf) != 0)
870f230475eSJan Schmidt 			tree_mod_log_free_eb(root->fs_info, buf);
8715d4f98a2SYan Zheng 		clean_tree_block(trans, root, buf);
872f0486c68SYan, Zheng 		*last_ref = 1;
8735d4f98a2SYan Zheng 	}
8745d4f98a2SYan Zheng 	return 0;
8755d4f98a2SYan Zheng }
8765d4f98a2SYan Zheng 
8775d4f98a2SYan Zheng /*
878d397712bSChris Mason  * does the dirty work in cow of a single block.  The parent block (if
879d397712bSChris Mason  * supplied) is updated to point to the new cow copy.  The new buffer is marked
880d397712bSChris Mason  * dirty and returned locked.  If you modify the block it needs to be marked
881d397712bSChris Mason  * dirty again.
882d352ac68SChris Mason  *
883d352ac68SChris Mason  * search_start -- an allocation hint for the new block
884d352ac68SChris Mason  *
885d397712bSChris Mason  * empty_size -- a hint that you plan on doing more cow.  This is the size in
886d397712bSChris Mason  * bytes the allocator should try to find free next to the block it returns.
887d397712bSChris Mason  * This is just a hint and may be ignored by the allocator.
888d352ac68SChris Mason  */
889d397712bSChris Mason static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
8905f39d397SChris Mason 			     struct btrfs_root *root,
8915f39d397SChris Mason 			     struct extent_buffer *buf,
8925f39d397SChris Mason 			     struct extent_buffer *parent, int parent_slot,
8935f39d397SChris Mason 			     struct extent_buffer **cow_ret,
8949fa8cfe7SChris Mason 			     u64 search_start, u64 empty_size)
8956702ed49SChris Mason {
8965d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
8975f39d397SChris Mason 	struct extent_buffer *cow;
898be1a5564SMark Fasheh 	int level, ret;
899f0486c68SYan, Zheng 	int last_ref = 0;
900925baeddSChris Mason 	int unlock_orig = 0;
9015d4f98a2SYan Zheng 	u64 parent_start;
9026702ed49SChris Mason 
903925baeddSChris Mason 	if (*cow_ret == buf)
904925baeddSChris Mason 		unlock_orig = 1;
905925baeddSChris Mason 
906b9447ef8SChris Mason 	btrfs_assert_tree_locked(buf);
907925baeddSChris Mason 
9087bb86316SChris Mason 	WARN_ON(root->ref_cows && trans->transid !=
9097bb86316SChris Mason 		root->fs_info->running_transaction->transid);
9106702ed49SChris Mason 	WARN_ON(root->ref_cows && trans->transid != root->last_trans);
9115f39d397SChris Mason 
9127bb86316SChris Mason 	level = btrfs_header_level(buf);
91331840ae1SZheng Yan 
9145d4f98a2SYan Zheng 	if (level == 0)
9155d4f98a2SYan Zheng 		btrfs_item_key(buf, &disk_key, 0);
9165d4f98a2SYan Zheng 	else
9175d4f98a2SYan Zheng 		btrfs_node_key(buf, &disk_key, 0);
9185d4f98a2SYan Zheng 
9195d4f98a2SYan Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
9205d4f98a2SYan Zheng 		if (parent)
9215d4f98a2SYan Zheng 			parent_start = parent->start;
9225d4f98a2SYan Zheng 		else
9235d4f98a2SYan Zheng 			parent_start = 0;
9245d4f98a2SYan Zheng 	} else
9255d4f98a2SYan Zheng 		parent_start = 0;
9265d4f98a2SYan Zheng 
9275d4f98a2SYan Zheng 	cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
9285d4f98a2SYan Zheng 				     root->root_key.objectid, &disk_key,
9295581a51aSJan Schmidt 				     level, search_start, empty_size);
9306702ed49SChris Mason 	if (IS_ERR(cow))
9316702ed49SChris Mason 		return PTR_ERR(cow);
9326702ed49SChris Mason 
933b4ce94deSChris Mason 	/* cow is set to blocking by btrfs_init_new_buffer */
934b4ce94deSChris Mason 
9355f39d397SChris Mason 	copy_extent_buffer(cow, buf, 0, 0, cow->len);
936db94535dSChris Mason 	btrfs_set_header_bytenr(cow, cow->start);
9375f39d397SChris Mason 	btrfs_set_header_generation(cow, trans->transid);
9385d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
9395d4f98a2SYan Zheng 	btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
9405d4f98a2SYan Zheng 				     BTRFS_HEADER_FLAG_RELOC);
9415d4f98a2SYan Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
9425d4f98a2SYan Zheng 		btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
9435d4f98a2SYan Zheng 	else
9445f39d397SChris Mason 		btrfs_set_header_owner(cow, root->root_key.objectid);
9456702ed49SChris Mason 
9462b82032cSYan Zheng 	write_extent_buffer(cow, root->fs_info->fsid,
9472b82032cSYan Zheng 			    (unsigned long)btrfs_header_fsid(cow),
9482b82032cSYan Zheng 			    BTRFS_FSID_SIZE);
9492b82032cSYan Zheng 
950be1a5564SMark Fasheh 	ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
951b68dc2a9SMark Fasheh 	if (ret) {
95279787eaaSJeff Mahoney 		btrfs_abort_transaction(trans, root, ret);
953b68dc2a9SMark Fasheh 		return ret;
954b68dc2a9SMark Fasheh 	}
9551a40e23bSZheng Yan 
9563fd0a558SYan, Zheng 	if (root->ref_cows)
9573fd0a558SYan, Zheng 		btrfs_reloc_cow_block(trans, root, buf, cow);
9583fd0a558SYan, Zheng 
9596702ed49SChris Mason 	if (buf == root->node) {
960925baeddSChris Mason 		WARN_ON(parent && parent != buf);
9615d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
9625d4f98a2SYan Zheng 		    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
9635d4f98a2SYan Zheng 			parent_start = buf->start;
9645d4f98a2SYan Zheng 		else
9655d4f98a2SYan Zheng 			parent_start = 0;
966925baeddSChris Mason 
9675f39d397SChris Mason 		extent_buffer_get(cow);
968f230475eSJan Schmidt 		tree_mod_log_set_root_pointer(root, cow);
969240f62c8SChris Mason 		rcu_assign_pointer(root->node, cow);
970925baeddSChris Mason 
971f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, buf, parent_start,
9725581a51aSJan Schmidt 				      last_ref);
9735f39d397SChris Mason 		free_extent_buffer(buf);
9740b86a832SChris Mason 		add_root_to_dirty_list(root);
9756702ed49SChris Mason 	} else {
9765d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
9775d4f98a2SYan Zheng 			parent_start = parent->start;
9785d4f98a2SYan Zheng 		else
9795d4f98a2SYan Zheng 			parent_start = 0;
9805d4f98a2SYan Zheng 
9815d4f98a2SYan Zheng 		WARN_ON(trans->transid != btrfs_header_generation(parent));
982f230475eSJan Schmidt 		tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
983f230475eSJan Schmidt 					MOD_LOG_KEY_REPLACE);
9845f39d397SChris Mason 		btrfs_set_node_blockptr(parent, parent_slot,
985db94535dSChris Mason 					cow->start);
98674493f7aSChris Mason 		btrfs_set_node_ptr_generation(parent, parent_slot,
98774493f7aSChris Mason 					      trans->transid);
9886702ed49SChris Mason 		btrfs_mark_buffer_dirty(parent);
989f0486c68SYan, Zheng 		btrfs_free_tree_block(trans, root, buf, parent_start,
9905581a51aSJan Schmidt 				      last_ref);
9916702ed49SChris Mason 	}
992925baeddSChris Mason 	if (unlock_orig)
993925baeddSChris Mason 		btrfs_tree_unlock(buf);
9943083ee2eSJosef Bacik 	free_extent_buffer_stale(buf);
9956702ed49SChris Mason 	btrfs_mark_buffer_dirty(cow);
9966702ed49SChris Mason 	*cow_ret = cow;
9976702ed49SChris Mason 	return 0;
9986702ed49SChris Mason }
9996702ed49SChris Mason 
10005d9e75c4SJan Schmidt /*
10015d9e75c4SJan Schmidt  * returns the logical address of the oldest predecessor of the given root.
10025d9e75c4SJan Schmidt  * entries older than time_seq are ignored.
10035d9e75c4SJan Schmidt  */
10045d9e75c4SJan Schmidt static struct tree_mod_elem *
10055d9e75c4SJan Schmidt __tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
10065d9e75c4SJan Schmidt 			   struct btrfs_root *root, u64 time_seq)
10075d9e75c4SJan Schmidt {
10085d9e75c4SJan Schmidt 	struct tree_mod_elem *tm;
10095d9e75c4SJan Schmidt 	struct tree_mod_elem *found = NULL;
10105d9e75c4SJan Schmidt 	u64 root_logical = root->node->start;
10115d9e75c4SJan Schmidt 	int looped = 0;
10125d9e75c4SJan Schmidt 
10135d9e75c4SJan Schmidt 	if (!time_seq)
10145d9e75c4SJan Schmidt 		return 0;
10155d9e75c4SJan Schmidt 
10165d9e75c4SJan Schmidt 	/*
10175d9e75c4SJan Schmidt 	 * the very last operation that's logged for a root is the replacement
10185d9e75c4SJan Schmidt 	 * operation (if it is replaced at all). this has the index of the *new*
10195d9e75c4SJan Schmidt 	 * root, making it the very first operation that's logged for this root.
10205d9e75c4SJan Schmidt 	 */
10215d9e75c4SJan Schmidt 	while (1) {
10225d9e75c4SJan Schmidt 		tm = tree_mod_log_search_oldest(fs_info, root_logical,
10235d9e75c4SJan Schmidt 						time_seq);
10245d9e75c4SJan Schmidt 		if (!looped && !tm)
10255d9e75c4SJan Schmidt 			return 0;
10265d9e75c4SJan Schmidt 		/*
102728da9fb4SJan Schmidt 		 * if there are no tree operation for the oldest root, we simply
102828da9fb4SJan Schmidt 		 * return it. this should only happen if that (old) root is at
102928da9fb4SJan Schmidt 		 * level 0.
10305d9e75c4SJan Schmidt 		 */
103128da9fb4SJan Schmidt 		if (!tm)
103228da9fb4SJan Schmidt 			break;
10335d9e75c4SJan Schmidt 
103428da9fb4SJan Schmidt 		/*
103528da9fb4SJan Schmidt 		 * if there's an operation that's not a root replacement, we
103628da9fb4SJan Schmidt 		 * found the oldest version of our root. normally, we'll find a
103728da9fb4SJan Schmidt 		 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
103828da9fb4SJan Schmidt 		 */
10395d9e75c4SJan Schmidt 		if (tm->op != MOD_LOG_ROOT_REPLACE)
10405d9e75c4SJan Schmidt 			break;
10415d9e75c4SJan Schmidt 
10425d9e75c4SJan Schmidt 		found = tm;
10435d9e75c4SJan Schmidt 		root_logical = tm->old_root.logical;
10445d9e75c4SJan Schmidt 		BUG_ON(root_logical == root->node->start);
10455d9e75c4SJan Schmidt 		looped = 1;
10465d9e75c4SJan Schmidt 	}
10475d9e75c4SJan Schmidt 
1048a95236d9SJan Schmidt 	/* if there's no old root to return, return what we found instead */
1049a95236d9SJan Schmidt 	if (!found)
1050a95236d9SJan Schmidt 		found = tm;
1051a95236d9SJan Schmidt 
10525d9e75c4SJan Schmidt 	return found;
10535d9e75c4SJan Schmidt }
10545d9e75c4SJan Schmidt 
10555d9e75c4SJan Schmidt /*
10565d9e75c4SJan Schmidt  * tm is a pointer to the first operation to rewind within eb. then, all
10575d9e75c4SJan Schmidt  * previous operations will be rewinded (until we reach something older than
10585d9e75c4SJan Schmidt  * time_seq).
10595d9e75c4SJan Schmidt  */
10605d9e75c4SJan Schmidt static void
10615d9e75c4SJan Schmidt __tree_mod_log_rewind(struct extent_buffer *eb, u64 time_seq,
10625d9e75c4SJan Schmidt 		      struct tree_mod_elem *first_tm)
10635d9e75c4SJan Schmidt {
10645d9e75c4SJan Schmidt 	u32 n;
10655d9e75c4SJan Schmidt 	struct rb_node *next;
10665d9e75c4SJan Schmidt 	struct tree_mod_elem *tm = first_tm;
10675d9e75c4SJan Schmidt 	unsigned long o_dst;
10685d9e75c4SJan Schmidt 	unsigned long o_src;
10695d9e75c4SJan Schmidt 	unsigned long p_size = sizeof(struct btrfs_key_ptr);
10705d9e75c4SJan Schmidt 
10715d9e75c4SJan Schmidt 	n = btrfs_header_nritems(eb);
10725d9e75c4SJan Schmidt 	while (tm && tm->elem.seq >= time_seq) {
10735d9e75c4SJan Schmidt 		/*
10745d9e75c4SJan Schmidt 		 * all the operations are recorded with the operator used for
10755d9e75c4SJan Schmidt 		 * the modification. as we're going backwards, we do the
10765d9e75c4SJan Schmidt 		 * opposite of each operation here.
10775d9e75c4SJan Schmidt 		 */
10785d9e75c4SJan Schmidt 		switch (tm->op) {
10795d9e75c4SJan Schmidt 		case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
10805d9e75c4SJan Schmidt 			BUG_ON(tm->slot < n);
10815d9e75c4SJan Schmidt 		case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
10825d9e75c4SJan Schmidt 		case MOD_LOG_KEY_REMOVE:
10835d9e75c4SJan Schmidt 			btrfs_set_node_key(eb, &tm->key, tm->slot);
10845d9e75c4SJan Schmidt 			btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
10855d9e75c4SJan Schmidt 			btrfs_set_node_ptr_generation(eb, tm->slot,
10865d9e75c4SJan Schmidt 						      tm->generation);
10875d9e75c4SJan Schmidt 			n++;
10885d9e75c4SJan Schmidt 			break;
10895d9e75c4SJan Schmidt 		case MOD_LOG_KEY_REPLACE:
10905d9e75c4SJan Schmidt 			BUG_ON(tm->slot >= n);
10915d9e75c4SJan Schmidt 			btrfs_set_node_key(eb, &tm->key, tm->slot);
10925d9e75c4SJan Schmidt 			btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
10935d9e75c4SJan Schmidt 			btrfs_set_node_ptr_generation(eb, tm->slot,
10945d9e75c4SJan Schmidt 						      tm->generation);
10955d9e75c4SJan Schmidt 			break;
10965d9e75c4SJan Schmidt 		case MOD_LOG_KEY_ADD:
109719956c7eSJan Schmidt 			/* if a move operation is needed it's in the log */
10985d9e75c4SJan Schmidt 			n--;
10995d9e75c4SJan Schmidt 			break;
11005d9e75c4SJan Schmidt 		case MOD_LOG_MOVE_KEYS:
1101c3193108SJan Schmidt 			o_dst = btrfs_node_key_ptr_offset(tm->slot);
1102c3193108SJan Schmidt 			o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1103c3193108SJan Schmidt 			memmove_extent_buffer(eb, o_dst, o_src,
11045d9e75c4SJan Schmidt 					      tm->move.nr_items * p_size);
11055d9e75c4SJan Schmidt 			break;
11065d9e75c4SJan Schmidt 		case MOD_LOG_ROOT_REPLACE:
11075d9e75c4SJan Schmidt 			/*
11085d9e75c4SJan Schmidt 			 * this operation is special. for roots, this must be
11095d9e75c4SJan Schmidt 			 * handled explicitly before rewinding.
11105d9e75c4SJan Schmidt 			 * for non-roots, this operation may exist if the node
11115d9e75c4SJan Schmidt 			 * was a root: root A -> child B; then A gets empty and
11125d9e75c4SJan Schmidt 			 * B is promoted to the new root. in the mod log, we'll
11135d9e75c4SJan Schmidt 			 * have a root-replace operation for B, a tree block
11145d9e75c4SJan Schmidt 			 * that is no root. we simply ignore that operation.
11155d9e75c4SJan Schmidt 			 */
11165d9e75c4SJan Schmidt 			break;
11175d9e75c4SJan Schmidt 		}
11185d9e75c4SJan Schmidt 		next = rb_next(&tm->node);
11195d9e75c4SJan Schmidt 		if (!next)
11205d9e75c4SJan Schmidt 			break;
11215d9e75c4SJan Schmidt 		tm = container_of(next, struct tree_mod_elem, node);
11225d9e75c4SJan Schmidt 		if (tm->index != first_tm->index)
11235d9e75c4SJan Schmidt 			break;
11245d9e75c4SJan Schmidt 	}
11255d9e75c4SJan Schmidt 	btrfs_set_header_nritems(eb, n);
11265d9e75c4SJan Schmidt }
11275d9e75c4SJan Schmidt 
11285d9e75c4SJan Schmidt static struct extent_buffer *
11295d9e75c4SJan Schmidt tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
11305d9e75c4SJan Schmidt 		    u64 time_seq)
11315d9e75c4SJan Schmidt {
11325d9e75c4SJan Schmidt 	struct extent_buffer *eb_rewin;
11335d9e75c4SJan Schmidt 	struct tree_mod_elem *tm;
11345d9e75c4SJan Schmidt 
11355d9e75c4SJan Schmidt 	if (!time_seq)
11365d9e75c4SJan Schmidt 		return eb;
11375d9e75c4SJan Schmidt 
11385d9e75c4SJan Schmidt 	if (btrfs_header_level(eb) == 0)
11395d9e75c4SJan Schmidt 		return eb;
11405d9e75c4SJan Schmidt 
11415d9e75c4SJan Schmidt 	tm = tree_mod_log_search(fs_info, eb->start, time_seq);
11425d9e75c4SJan Schmidt 	if (!tm)
11435d9e75c4SJan Schmidt 		return eb;
11445d9e75c4SJan Schmidt 
11455d9e75c4SJan Schmidt 	if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
11465d9e75c4SJan Schmidt 		BUG_ON(tm->slot != 0);
11475d9e75c4SJan Schmidt 		eb_rewin = alloc_dummy_extent_buffer(eb->start,
11485d9e75c4SJan Schmidt 						fs_info->tree_root->nodesize);
11495d9e75c4SJan Schmidt 		BUG_ON(!eb_rewin);
11505d9e75c4SJan Schmidt 		btrfs_set_header_bytenr(eb_rewin, eb->start);
11515d9e75c4SJan Schmidt 		btrfs_set_header_backref_rev(eb_rewin,
11525d9e75c4SJan Schmidt 					     btrfs_header_backref_rev(eb));
11535d9e75c4SJan Schmidt 		btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
1154c3193108SJan Schmidt 		btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
11555d9e75c4SJan Schmidt 	} else {
11565d9e75c4SJan Schmidt 		eb_rewin = btrfs_clone_extent_buffer(eb);
11575d9e75c4SJan Schmidt 		BUG_ON(!eb_rewin);
11585d9e75c4SJan Schmidt 	}
11595d9e75c4SJan Schmidt 
11605d9e75c4SJan Schmidt 	extent_buffer_get(eb_rewin);
11615d9e75c4SJan Schmidt 	free_extent_buffer(eb);
11625d9e75c4SJan Schmidt 
11635d9e75c4SJan Schmidt 	__tree_mod_log_rewind(eb_rewin, time_seq, tm);
11645d9e75c4SJan Schmidt 
11655d9e75c4SJan Schmidt 	return eb_rewin;
11665d9e75c4SJan Schmidt }
11675d9e75c4SJan Schmidt 
11688ba97a15SJan Schmidt /*
11698ba97a15SJan Schmidt  * get_old_root() rewinds the state of @root's root node to the given @time_seq
11708ba97a15SJan Schmidt  * value. If there are no changes, the current root->root_node is returned. If
11718ba97a15SJan Schmidt  * anything changed in between, there's a fresh buffer allocated on which the
11728ba97a15SJan Schmidt  * rewind operations are done. In any case, the returned buffer is read locked.
11738ba97a15SJan Schmidt  * Returns NULL on error (with no locks held).
11748ba97a15SJan Schmidt  */
11755d9e75c4SJan Schmidt static inline struct extent_buffer *
11765d9e75c4SJan Schmidt get_old_root(struct btrfs_root *root, u64 time_seq)
11775d9e75c4SJan Schmidt {
11785d9e75c4SJan Schmidt 	struct tree_mod_elem *tm;
11795d9e75c4SJan Schmidt 	struct extent_buffer *eb;
1180a95236d9SJan Schmidt 	struct tree_mod_root *old_root = NULL;
11814325edd0SChris Mason 	u64 old_generation = 0;
1182a95236d9SJan Schmidt 	u64 logical;
11835d9e75c4SJan Schmidt 
11848ba97a15SJan Schmidt 	eb = btrfs_read_lock_root_node(root);
11855d9e75c4SJan Schmidt 	tm = __tree_mod_log_oldest_root(root->fs_info, root, time_seq);
11865d9e75c4SJan Schmidt 	if (!tm)
11875d9e75c4SJan Schmidt 		return root->node;
11885d9e75c4SJan Schmidt 
1189a95236d9SJan Schmidt 	if (tm->op == MOD_LOG_ROOT_REPLACE) {
11905d9e75c4SJan Schmidt 		old_root = &tm->old_root;
11915d9e75c4SJan Schmidt 		old_generation = tm->generation;
1192a95236d9SJan Schmidt 		logical = old_root->logical;
1193a95236d9SJan Schmidt 	} else {
1194a95236d9SJan Schmidt 		logical = root->node->start;
1195a95236d9SJan Schmidt 	}
11965d9e75c4SJan Schmidt 
1197a95236d9SJan Schmidt 	tm = tree_mod_log_search(root->fs_info, logical, time_seq);
1198a95236d9SJan Schmidt 	if (old_root)
119928da9fb4SJan Schmidt 		eb = alloc_dummy_extent_buffer(logical, root->nodesize);
1200a95236d9SJan Schmidt 	else
1201a95236d9SJan Schmidt 		eb = btrfs_clone_extent_buffer(root->node);
12028ba97a15SJan Schmidt 	btrfs_tree_read_unlock(root->node);
12038ba97a15SJan Schmidt 	free_extent_buffer(root->node);
12048ba97a15SJan Schmidt 	if (!eb)
12058ba97a15SJan Schmidt 		return NULL;
12068ba97a15SJan Schmidt 	btrfs_tree_read_lock(eb);
1207a95236d9SJan Schmidt 	if (old_root) {
12085d9e75c4SJan Schmidt 		btrfs_set_header_bytenr(eb, eb->start);
12095d9e75c4SJan Schmidt 		btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
12105d9e75c4SJan Schmidt 		btrfs_set_header_owner(eb, root->root_key.objectid);
12115d9e75c4SJan Schmidt 		btrfs_set_header_level(eb, old_root->level);
12125d9e75c4SJan Schmidt 		btrfs_set_header_generation(eb, old_generation);
1213a95236d9SJan Schmidt 	}
121428da9fb4SJan Schmidt 	if (tm)
12155d9e75c4SJan Schmidt 		__tree_mod_log_rewind(eb, time_seq, tm);
121628da9fb4SJan Schmidt 	else
121728da9fb4SJan Schmidt 		WARN_ON(btrfs_header_level(eb) != 0);
12188ba97a15SJan Schmidt 	extent_buffer_get(eb);
12195d9e75c4SJan Schmidt 
12205d9e75c4SJan Schmidt 	return eb;
12215d9e75c4SJan Schmidt }
12225d9e75c4SJan Schmidt 
12235d4f98a2SYan Zheng static inline int should_cow_block(struct btrfs_trans_handle *trans,
12245d4f98a2SYan Zheng 				   struct btrfs_root *root,
12255d4f98a2SYan Zheng 				   struct extent_buffer *buf)
12265d4f98a2SYan Zheng {
1227f1ebcc74SLiu Bo 	/* ensure we can see the force_cow */
1228f1ebcc74SLiu Bo 	smp_rmb();
1229f1ebcc74SLiu Bo 
1230f1ebcc74SLiu Bo 	/*
1231f1ebcc74SLiu Bo 	 * We do not need to cow a block if
1232f1ebcc74SLiu Bo 	 * 1) this block is not created or changed in this transaction;
1233f1ebcc74SLiu Bo 	 * 2) this block does not belong to TREE_RELOC tree;
1234f1ebcc74SLiu Bo 	 * 3) the root is not forced COW.
1235f1ebcc74SLiu Bo 	 *
1236f1ebcc74SLiu Bo 	 * What is forced COW:
1237f1ebcc74SLiu Bo 	 *    when we create snapshot during commiting the transaction,
1238f1ebcc74SLiu Bo 	 *    after we've finished coping src root, we must COW the shared
1239f1ebcc74SLiu Bo 	 *    block to ensure the metadata consistency.
1240f1ebcc74SLiu Bo 	 */
12415d4f98a2SYan Zheng 	if (btrfs_header_generation(buf) == trans->transid &&
12425d4f98a2SYan Zheng 	    !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
12435d4f98a2SYan Zheng 	    !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
1244f1ebcc74SLiu Bo 	      btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1245f1ebcc74SLiu Bo 	    !root->force_cow)
12465d4f98a2SYan Zheng 		return 0;
12475d4f98a2SYan Zheng 	return 1;
12485d4f98a2SYan Zheng }
12495d4f98a2SYan Zheng 
1250d352ac68SChris Mason /*
1251d352ac68SChris Mason  * cows a single block, see __btrfs_cow_block for the real work.
1252d352ac68SChris Mason  * This version of it has extra checks so that a block isn't cow'd more than
1253d352ac68SChris Mason  * once per transaction, as long as it hasn't been written yet
1254d352ac68SChris Mason  */
1255d397712bSChris Mason noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
12565f39d397SChris Mason 		    struct btrfs_root *root, struct extent_buffer *buf,
12575f39d397SChris Mason 		    struct extent_buffer *parent, int parent_slot,
12589fa8cfe7SChris Mason 		    struct extent_buffer **cow_ret)
125902217ed2SChris Mason {
12606702ed49SChris Mason 	u64 search_start;
1261f510cfecSChris Mason 	int ret;
1262dc17ff8fSChris Mason 
1263ccd467d6SChris Mason 	if (trans->transaction != root->fs_info->running_transaction) {
1264d397712bSChris Mason 		printk(KERN_CRIT "trans %llu running %llu\n",
1265d397712bSChris Mason 		       (unsigned long long)trans->transid,
1266d397712bSChris Mason 		       (unsigned long long)
1267ccd467d6SChris Mason 		       root->fs_info->running_transaction->transid);
1268ccd467d6SChris Mason 		WARN_ON(1);
1269ccd467d6SChris Mason 	}
1270ccd467d6SChris Mason 	if (trans->transid != root->fs_info->generation) {
1271d397712bSChris Mason 		printk(KERN_CRIT "trans %llu running %llu\n",
1272d397712bSChris Mason 		       (unsigned long long)trans->transid,
1273d397712bSChris Mason 		       (unsigned long long)root->fs_info->generation);
1274ccd467d6SChris Mason 		WARN_ON(1);
1275ccd467d6SChris Mason 	}
1276dc17ff8fSChris Mason 
12775d4f98a2SYan Zheng 	if (!should_cow_block(trans, root, buf)) {
127802217ed2SChris Mason 		*cow_ret = buf;
127902217ed2SChris Mason 		return 0;
128002217ed2SChris Mason 	}
1281c487685dSChris Mason 
12820b86a832SChris Mason 	search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
1283b4ce94deSChris Mason 
1284b4ce94deSChris Mason 	if (parent)
1285b4ce94deSChris Mason 		btrfs_set_lock_blocking(parent);
1286b4ce94deSChris Mason 	btrfs_set_lock_blocking(buf);
1287b4ce94deSChris Mason 
1288f510cfecSChris Mason 	ret = __btrfs_cow_block(trans, root, buf, parent,
12899fa8cfe7SChris Mason 				 parent_slot, cow_ret, search_start, 0);
12901abe9b8aSliubo 
12911abe9b8aSliubo 	trace_btrfs_cow_block(root, buf, *cow_ret);
12921abe9b8aSliubo 
1293f510cfecSChris Mason 	return ret;
12942c90e5d6SChris Mason }
12956702ed49SChris Mason 
1296d352ac68SChris Mason /*
1297d352ac68SChris Mason  * helper function for defrag to decide if two blocks pointed to by a
1298d352ac68SChris Mason  * node are actually close by
1299d352ac68SChris Mason  */
13006b80053dSChris Mason static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
13016702ed49SChris Mason {
13026b80053dSChris Mason 	if (blocknr < other && other - (blocknr + blocksize) < 32768)
13036702ed49SChris Mason 		return 1;
13046b80053dSChris Mason 	if (blocknr > other && blocknr - (other + blocksize) < 32768)
13056702ed49SChris Mason 		return 1;
130602217ed2SChris Mason 	return 0;
130702217ed2SChris Mason }
130802217ed2SChris Mason 
1309081e9573SChris Mason /*
1310081e9573SChris Mason  * compare two keys in a memcmp fashion
1311081e9573SChris Mason  */
1312081e9573SChris Mason static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1313081e9573SChris Mason {
1314081e9573SChris Mason 	struct btrfs_key k1;
1315081e9573SChris Mason 
1316081e9573SChris Mason 	btrfs_disk_key_to_cpu(&k1, disk);
1317081e9573SChris Mason 
131820736abaSDiego Calleja 	return btrfs_comp_cpu_keys(&k1, k2);
1319081e9573SChris Mason }
1320081e9573SChris Mason 
1321f3465ca4SJosef Bacik /*
1322f3465ca4SJosef Bacik  * same as comp_keys only with two btrfs_key's
1323f3465ca4SJosef Bacik  */
13245d4f98a2SYan Zheng int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
1325f3465ca4SJosef Bacik {
1326f3465ca4SJosef Bacik 	if (k1->objectid > k2->objectid)
1327f3465ca4SJosef Bacik 		return 1;
1328f3465ca4SJosef Bacik 	if (k1->objectid < k2->objectid)
1329f3465ca4SJosef Bacik 		return -1;
1330f3465ca4SJosef Bacik 	if (k1->type > k2->type)
1331f3465ca4SJosef Bacik 		return 1;
1332f3465ca4SJosef Bacik 	if (k1->type < k2->type)
1333f3465ca4SJosef Bacik 		return -1;
1334f3465ca4SJosef Bacik 	if (k1->offset > k2->offset)
1335f3465ca4SJosef Bacik 		return 1;
1336f3465ca4SJosef Bacik 	if (k1->offset < k2->offset)
1337f3465ca4SJosef Bacik 		return -1;
1338f3465ca4SJosef Bacik 	return 0;
1339f3465ca4SJosef Bacik }
1340081e9573SChris Mason 
1341d352ac68SChris Mason /*
1342d352ac68SChris Mason  * this is used by the defrag code to go through all the
1343d352ac68SChris Mason  * leaves pointed to by a node and reallocate them so that
1344d352ac68SChris Mason  * disk order is close to key order
1345d352ac68SChris Mason  */
13466702ed49SChris Mason int btrfs_realloc_node(struct btrfs_trans_handle *trans,
13475f39d397SChris Mason 		       struct btrfs_root *root, struct extent_buffer *parent,
1348a6b6e75eSChris Mason 		       int start_slot, int cache_only, u64 *last_ret,
1349a6b6e75eSChris Mason 		       struct btrfs_key *progress)
13506702ed49SChris Mason {
13516b80053dSChris Mason 	struct extent_buffer *cur;
13526702ed49SChris Mason 	u64 blocknr;
1353ca7a79adSChris Mason 	u64 gen;
1354e9d0b13bSChris Mason 	u64 search_start = *last_ret;
1355e9d0b13bSChris Mason 	u64 last_block = 0;
13566702ed49SChris Mason 	u64 other;
13576702ed49SChris Mason 	u32 parent_nritems;
13586702ed49SChris Mason 	int end_slot;
13596702ed49SChris Mason 	int i;
13606702ed49SChris Mason 	int err = 0;
1361f2183bdeSChris Mason 	int parent_level;
13626b80053dSChris Mason 	int uptodate;
13636b80053dSChris Mason 	u32 blocksize;
1364081e9573SChris Mason 	int progress_passed = 0;
1365081e9573SChris Mason 	struct btrfs_disk_key disk_key;
13666702ed49SChris Mason 
13675708b959SChris Mason 	parent_level = btrfs_header_level(parent);
13685708b959SChris Mason 	if (cache_only && parent_level != 1)
13695708b959SChris Mason 		return 0;
13705708b959SChris Mason 
1371d397712bSChris Mason 	if (trans->transaction != root->fs_info->running_transaction)
13726702ed49SChris Mason 		WARN_ON(1);
1373d397712bSChris Mason 	if (trans->transid != root->fs_info->generation)
13746702ed49SChris Mason 		WARN_ON(1);
137586479a04SChris Mason 
13766b80053dSChris Mason 	parent_nritems = btrfs_header_nritems(parent);
13776b80053dSChris Mason 	blocksize = btrfs_level_size(root, parent_level - 1);
13786702ed49SChris Mason 	end_slot = parent_nritems;
13796702ed49SChris Mason 
13806702ed49SChris Mason 	if (parent_nritems == 1)
13816702ed49SChris Mason 		return 0;
13826702ed49SChris Mason 
1383b4ce94deSChris Mason 	btrfs_set_lock_blocking(parent);
1384b4ce94deSChris Mason 
13856702ed49SChris Mason 	for (i = start_slot; i < end_slot; i++) {
13866702ed49SChris Mason 		int close = 1;
1387a6b6e75eSChris Mason 
1388081e9573SChris Mason 		btrfs_node_key(parent, &disk_key, i);
1389081e9573SChris Mason 		if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1390081e9573SChris Mason 			continue;
1391081e9573SChris Mason 
1392081e9573SChris Mason 		progress_passed = 1;
13936b80053dSChris Mason 		blocknr = btrfs_node_blockptr(parent, i);
1394ca7a79adSChris Mason 		gen = btrfs_node_ptr_generation(parent, i);
1395e9d0b13bSChris Mason 		if (last_block == 0)
1396e9d0b13bSChris Mason 			last_block = blocknr;
13975708b959SChris Mason 
13986702ed49SChris Mason 		if (i > 0) {
13996b80053dSChris Mason 			other = btrfs_node_blockptr(parent, i - 1);
14006b80053dSChris Mason 			close = close_blocks(blocknr, other, blocksize);
14016702ed49SChris Mason 		}
14020ef3e66bSChris Mason 		if (!close && i < end_slot - 2) {
14036b80053dSChris Mason 			other = btrfs_node_blockptr(parent, i + 1);
14046b80053dSChris Mason 			close = close_blocks(blocknr, other, blocksize);
14056702ed49SChris Mason 		}
1406e9d0b13bSChris Mason 		if (close) {
1407e9d0b13bSChris Mason 			last_block = blocknr;
14086702ed49SChris Mason 			continue;
1409e9d0b13bSChris Mason 		}
14106702ed49SChris Mason 
14116b80053dSChris Mason 		cur = btrfs_find_tree_block(root, blocknr, blocksize);
14126b80053dSChris Mason 		if (cur)
1413b9fab919SChris Mason 			uptodate = btrfs_buffer_uptodate(cur, gen, 0);
14146b80053dSChris Mason 		else
14156b80053dSChris Mason 			uptodate = 0;
14165708b959SChris Mason 		if (!cur || !uptodate) {
14176702ed49SChris Mason 			if (cache_only) {
14186b80053dSChris Mason 				free_extent_buffer(cur);
14196702ed49SChris Mason 				continue;
14206702ed49SChris Mason 			}
14216b80053dSChris Mason 			if (!cur) {
14226b80053dSChris Mason 				cur = read_tree_block(root, blocknr,
1423ca7a79adSChris Mason 							 blocksize, gen);
142497d9a8a4STsutomu Itoh 				if (!cur)
142597d9a8a4STsutomu Itoh 					return -EIO;
14266b80053dSChris Mason 			} else if (!uptodate) {
1427018642a1STsutomu Itoh 				err = btrfs_read_buffer(cur, gen);
1428018642a1STsutomu Itoh 				if (err) {
1429018642a1STsutomu Itoh 					free_extent_buffer(cur);
1430018642a1STsutomu Itoh 					return err;
1431018642a1STsutomu Itoh 				}
14326702ed49SChris Mason 			}
1433f2183bdeSChris Mason 		}
1434e9d0b13bSChris Mason 		if (search_start == 0)
14356b80053dSChris Mason 			search_start = last_block;
1436e9d0b13bSChris Mason 
1437e7a84565SChris Mason 		btrfs_tree_lock(cur);
1438b4ce94deSChris Mason 		btrfs_set_lock_blocking(cur);
14396b80053dSChris Mason 		err = __btrfs_cow_block(trans, root, cur, parent, i,
1440e7a84565SChris Mason 					&cur, search_start,
14416b80053dSChris Mason 					min(16 * blocksize,
14429fa8cfe7SChris Mason 					    (end_slot - i) * blocksize));
1443252c38f0SYan 		if (err) {
1444e7a84565SChris Mason 			btrfs_tree_unlock(cur);
14456b80053dSChris Mason 			free_extent_buffer(cur);
14466702ed49SChris Mason 			break;
1447252c38f0SYan 		}
1448e7a84565SChris Mason 		search_start = cur->start;
1449e7a84565SChris Mason 		last_block = cur->start;
1450f2183bdeSChris Mason 		*last_ret = search_start;
1451e7a84565SChris Mason 		btrfs_tree_unlock(cur);
1452e7a84565SChris Mason 		free_extent_buffer(cur);
14536702ed49SChris Mason 	}
14546702ed49SChris Mason 	return err;
14556702ed49SChris Mason }
14566702ed49SChris Mason 
145774123bd7SChris Mason /*
145874123bd7SChris Mason  * The leaf data grows from end-to-front in the node.
145974123bd7SChris Mason  * this returns the address of the start of the last item,
146074123bd7SChris Mason  * which is the stop of the leaf data stack
146174123bd7SChris Mason  */
1462123abc88SChris Mason static inline unsigned int leaf_data_end(struct btrfs_root *root,
14635f39d397SChris Mason 					 struct extent_buffer *leaf)
1464be0e5c09SChris Mason {
14655f39d397SChris Mason 	u32 nr = btrfs_header_nritems(leaf);
1466be0e5c09SChris Mason 	if (nr == 0)
1467123abc88SChris Mason 		return BTRFS_LEAF_DATA_SIZE(root);
14685f39d397SChris Mason 	return btrfs_item_offset_nr(leaf, nr - 1);
1469be0e5c09SChris Mason }
1470be0e5c09SChris Mason 
1471aa5d6bedSChris Mason 
147274123bd7SChris Mason /*
14735f39d397SChris Mason  * search for key in the extent_buffer.  The items start at offset p,
14745f39d397SChris Mason  * and they are item_size apart.  There are 'max' items in p.
14755f39d397SChris Mason  *
147674123bd7SChris Mason  * the slot in the array is returned via slot, and it points to
147774123bd7SChris Mason  * the place where you would insert key if it is not found in
147874123bd7SChris Mason  * the array.
147974123bd7SChris Mason  *
148074123bd7SChris Mason  * slot may point to max if the key is bigger than all of the keys
148174123bd7SChris Mason  */
1482e02119d5SChris Mason static noinline int generic_bin_search(struct extent_buffer *eb,
1483e02119d5SChris Mason 				       unsigned long p,
14845f39d397SChris Mason 				       int item_size, struct btrfs_key *key,
1485be0e5c09SChris Mason 				       int max, int *slot)
1486be0e5c09SChris Mason {
1487be0e5c09SChris Mason 	int low = 0;
1488be0e5c09SChris Mason 	int high = max;
1489be0e5c09SChris Mason 	int mid;
1490be0e5c09SChris Mason 	int ret;
1491479965d6SChris Mason 	struct btrfs_disk_key *tmp = NULL;
14925f39d397SChris Mason 	struct btrfs_disk_key unaligned;
14935f39d397SChris Mason 	unsigned long offset;
14945f39d397SChris Mason 	char *kaddr = NULL;
14955f39d397SChris Mason 	unsigned long map_start = 0;
14965f39d397SChris Mason 	unsigned long map_len = 0;
1497479965d6SChris Mason 	int err;
1498be0e5c09SChris Mason 
1499be0e5c09SChris Mason 	while (low < high) {
1500be0e5c09SChris Mason 		mid = (low + high) / 2;
15015f39d397SChris Mason 		offset = p + mid * item_size;
15025f39d397SChris Mason 
1503a6591715SChris Mason 		if (!kaddr || offset < map_start ||
15045f39d397SChris Mason 		    (offset + sizeof(struct btrfs_disk_key)) >
15055f39d397SChris Mason 		    map_start + map_len) {
1506934d375bSChris Mason 
1507934d375bSChris Mason 			err = map_private_extent_buffer(eb, offset,
1508479965d6SChris Mason 						sizeof(struct btrfs_disk_key),
1509a6591715SChris Mason 						&kaddr, &map_start, &map_len);
15105f39d397SChris Mason 
1511479965d6SChris Mason 			if (!err) {
1512479965d6SChris Mason 				tmp = (struct btrfs_disk_key *)(kaddr + offset -
1513479965d6SChris Mason 							map_start);
1514479965d6SChris Mason 			} else {
15155f39d397SChris Mason 				read_extent_buffer(eb, &unaligned,
15165f39d397SChris Mason 						   offset, sizeof(unaligned));
15175f39d397SChris Mason 				tmp = &unaligned;
1518479965d6SChris Mason 			}
1519479965d6SChris Mason 
15205f39d397SChris Mason 		} else {
15215f39d397SChris Mason 			tmp = (struct btrfs_disk_key *)(kaddr + offset -
15225f39d397SChris Mason 							map_start);
15235f39d397SChris Mason 		}
1524be0e5c09SChris Mason 		ret = comp_keys(tmp, key);
1525be0e5c09SChris Mason 
1526be0e5c09SChris Mason 		if (ret < 0)
1527be0e5c09SChris Mason 			low = mid + 1;
1528be0e5c09SChris Mason 		else if (ret > 0)
1529be0e5c09SChris Mason 			high = mid;
1530be0e5c09SChris Mason 		else {
1531be0e5c09SChris Mason 			*slot = mid;
1532be0e5c09SChris Mason 			return 0;
1533be0e5c09SChris Mason 		}
1534be0e5c09SChris Mason 	}
1535be0e5c09SChris Mason 	*slot = low;
1536be0e5c09SChris Mason 	return 1;
1537be0e5c09SChris Mason }
1538be0e5c09SChris Mason 
153997571fd0SChris Mason /*
154097571fd0SChris Mason  * simple bin_search frontend that does the right thing for
154197571fd0SChris Mason  * leaves vs nodes
154297571fd0SChris Mason  */
15435f39d397SChris Mason static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
15445f39d397SChris Mason 		      int level, int *slot)
1545be0e5c09SChris Mason {
1546f775738fSWang Sheng-Hui 	if (level == 0)
15475f39d397SChris Mason 		return generic_bin_search(eb,
15485f39d397SChris Mason 					  offsetof(struct btrfs_leaf, items),
15490783fcfcSChris Mason 					  sizeof(struct btrfs_item),
15505f39d397SChris Mason 					  key, btrfs_header_nritems(eb),
15517518a238SChris Mason 					  slot);
1552f775738fSWang Sheng-Hui 	else
15535f39d397SChris Mason 		return generic_bin_search(eb,
15545f39d397SChris Mason 					  offsetof(struct btrfs_node, ptrs),
1555123abc88SChris Mason 					  sizeof(struct btrfs_key_ptr),
15565f39d397SChris Mason 					  key, btrfs_header_nritems(eb),
15577518a238SChris Mason 					  slot);
1558be0e5c09SChris Mason }
1559be0e5c09SChris Mason 
15605d4f98a2SYan Zheng int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
15615d4f98a2SYan Zheng 		     int level, int *slot)
15625d4f98a2SYan Zheng {
15635d4f98a2SYan Zheng 	return bin_search(eb, key, level, slot);
15645d4f98a2SYan Zheng }
15655d4f98a2SYan Zheng 
1566f0486c68SYan, Zheng static void root_add_used(struct btrfs_root *root, u32 size)
1567f0486c68SYan, Zheng {
1568f0486c68SYan, Zheng 	spin_lock(&root->accounting_lock);
1569f0486c68SYan, Zheng 	btrfs_set_root_used(&root->root_item,
1570f0486c68SYan, Zheng 			    btrfs_root_used(&root->root_item) + size);
1571f0486c68SYan, Zheng 	spin_unlock(&root->accounting_lock);
1572f0486c68SYan, Zheng }
1573f0486c68SYan, Zheng 
1574f0486c68SYan, Zheng static void root_sub_used(struct btrfs_root *root, u32 size)
1575f0486c68SYan, Zheng {
1576f0486c68SYan, Zheng 	spin_lock(&root->accounting_lock);
1577f0486c68SYan, Zheng 	btrfs_set_root_used(&root->root_item,
1578f0486c68SYan, Zheng 			    btrfs_root_used(&root->root_item) - size);
1579f0486c68SYan, Zheng 	spin_unlock(&root->accounting_lock);
1580f0486c68SYan, Zheng }
1581f0486c68SYan, Zheng 
1582d352ac68SChris Mason /* given a node and slot number, this reads the blocks it points to.  The
1583d352ac68SChris Mason  * extent buffer is returned with a reference taken (but unlocked).
1584d352ac68SChris Mason  * NULL is returned on error.
1585d352ac68SChris Mason  */
1586e02119d5SChris Mason static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
15875f39d397SChris Mason 				   struct extent_buffer *parent, int slot)
1588bb803951SChris Mason {
1589ca7a79adSChris Mason 	int level = btrfs_header_level(parent);
1590bb803951SChris Mason 	if (slot < 0)
1591bb803951SChris Mason 		return NULL;
15925f39d397SChris Mason 	if (slot >= btrfs_header_nritems(parent))
1593bb803951SChris Mason 		return NULL;
1594ca7a79adSChris Mason 
1595ca7a79adSChris Mason 	BUG_ON(level == 0);
1596ca7a79adSChris Mason 
1597db94535dSChris Mason 	return read_tree_block(root, btrfs_node_blockptr(parent, slot),
1598ca7a79adSChris Mason 		       btrfs_level_size(root, level - 1),
1599ca7a79adSChris Mason 		       btrfs_node_ptr_generation(parent, slot));
1600bb803951SChris Mason }
1601bb803951SChris Mason 
1602d352ac68SChris Mason /*
1603d352ac68SChris Mason  * node level balancing, used to make sure nodes are in proper order for
1604d352ac68SChris Mason  * item deletion.  We balance from the top down, so we have to make sure
1605d352ac68SChris Mason  * that a deletion won't leave an node completely empty later on.
1606d352ac68SChris Mason  */
1607e02119d5SChris Mason static noinline int balance_level(struct btrfs_trans_handle *trans,
160898ed5174SChris Mason 			 struct btrfs_root *root,
160998ed5174SChris Mason 			 struct btrfs_path *path, int level)
1610bb803951SChris Mason {
16115f39d397SChris Mason 	struct extent_buffer *right = NULL;
16125f39d397SChris Mason 	struct extent_buffer *mid;
16135f39d397SChris Mason 	struct extent_buffer *left = NULL;
16145f39d397SChris Mason 	struct extent_buffer *parent = NULL;
1615bb803951SChris Mason 	int ret = 0;
1616bb803951SChris Mason 	int wret;
1617bb803951SChris Mason 	int pslot;
1618bb803951SChris Mason 	int orig_slot = path->slots[level];
161979f95c82SChris Mason 	u64 orig_ptr;
1620bb803951SChris Mason 
1621bb803951SChris Mason 	if (level == 0)
1622bb803951SChris Mason 		return 0;
1623bb803951SChris Mason 
16245f39d397SChris Mason 	mid = path->nodes[level];
1625b4ce94deSChris Mason 
1626bd681513SChris Mason 	WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1627bd681513SChris Mason 		path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
16287bb86316SChris Mason 	WARN_ON(btrfs_header_generation(mid) != trans->transid);
16297bb86316SChris Mason 
16301d4f8a0cSChris Mason 	orig_ptr = btrfs_node_blockptr(mid, orig_slot);
163179f95c82SChris Mason 
1632a05a9bb1SLi Zefan 	if (level < BTRFS_MAX_LEVEL - 1) {
16335f39d397SChris Mason 		parent = path->nodes[level + 1];
1634bb803951SChris Mason 		pslot = path->slots[level + 1];
1635a05a9bb1SLi Zefan 	}
1636bb803951SChris Mason 
163740689478SChris Mason 	/*
163840689478SChris Mason 	 * deal with the case where there is only one pointer in the root
163940689478SChris Mason 	 * by promoting the node below to a root
164040689478SChris Mason 	 */
16415f39d397SChris Mason 	if (!parent) {
16425f39d397SChris Mason 		struct extent_buffer *child;
1643bb803951SChris Mason 
16445f39d397SChris Mason 		if (btrfs_header_nritems(mid) != 1)
1645bb803951SChris Mason 			return 0;
1646bb803951SChris Mason 
1647bb803951SChris Mason 		/* promote the child to a root */
16485f39d397SChris Mason 		child = read_node_slot(root, mid, 0);
1649305a26afSMark Fasheh 		if (!child) {
1650305a26afSMark Fasheh 			ret = -EROFS;
1651305a26afSMark Fasheh 			btrfs_std_error(root->fs_info, ret);
1652305a26afSMark Fasheh 			goto enospc;
1653305a26afSMark Fasheh 		}
1654305a26afSMark Fasheh 
1655925baeddSChris Mason 		btrfs_tree_lock(child);
1656b4ce94deSChris Mason 		btrfs_set_lock_blocking(child);
16579fa8cfe7SChris Mason 		ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
1658f0486c68SYan, Zheng 		if (ret) {
1659f0486c68SYan, Zheng 			btrfs_tree_unlock(child);
1660f0486c68SYan, Zheng 			free_extent_buffer(child);
1661f0486c68SYan, Zheng 			goto enospc;
1662f0486c68SYan, Zheng 		}
16632f375ab9SYan 
1664f230475eSJan Schmidt 		tree_mod_log_set_root_pointer(root, child);
1665240f62c8SChris Mason 		rcu_assign_pointer(root->node, child);
1666925baeddSChris Mason 
16670b86a832SChris Mason 		add_root_to_dirty_list(root);
1668925baeddSChris Mason 		btrfs_tree_unlock(child);
1669b4ce94deSChris Mason 
1670925baeddSChris Mason 		path->locks[level] = 0;
1671bb803951SChris Mason 		path->nodes[level] = NULL;
16725f39d397SChris Mason 		clean_tree_block(trans, root, mid);
1673925baeddSChris Mason 		btrfs_tree_unlock(mid);
1674bb803951SChris Mason 		/* once for the path */
16755f39d397SChris Mason 		free_extent_buffer(mid);
1676f0486c68SYan, Zheng 
1677f0486c68SYan, Zheng 		root_sub_used(root, mid->len);
16785581a51aSJan Schmidt 		btrfs_free_tree_block(trans, root, mid, 0, 1);
1679bb803951SChris Mason 		/* once for the root ptr */
16803083ee2eSJosef Bacik 		free_extent_buffer_stale(mid);
1681f0486c68SYan, Zheng 		return 0;
1682bb803951SChris Mason 	}
16835f39d397SChris Mason 	if (btrfs_header_nritems(mid) >
1684123abc88SChris Mason 	    BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
1685bb803951SChris Mason 		return 0;
1686bb803951SChris Mason 
16875f39d397SChris Mason 	left = read_node_slot(root, parent, pslot - 1);
16885f39d397SChris Mason 	if (left) {
1689925baeddSChris Mason 		btrfs_tree_lock(left);
1690b4ce94deSChris Mason 		btrfs_set_lock_blocking(left);
16915f39d397SChris Mason 		wret = btrfs_cow_block(trans, root, left,
16929fa8cfe7SChris Mason 				       parent, pslot - 1, &left);
169354aa1f4dSChris Mason 		if (wret) {
169454aa1f4dSChris Mason 			ret = wret;
169554aa1f4dSChris Mason 			goto enospc;
169654aa1f4dSChris Mason 		}
16972cc58cf2SChris Mason 	}
16985f39d397SChris Mason 	right = read_node_slot(root, parent, pslot + 1);
16995f39d397SChris Mason 	if (right) {
1700925baeddSChris Mason 		btrfs_tree_lock(right);
1701b4ce94deSChris Mason 		btrfs_set_lock_blocking(right);
17025f39d397SChris Mason 		wret = btrfs_cow_block(trans, root, right,
17039fa8cfe7SChris Mason 				       parent, pslot + 1, &right);
17042cc58cf2SChris Mason 		if (wret) {
17052cc58cf2SChris Mason 			ret = wret;
17062cc58cf2SChris Mason 			goto enospc;
17072cc58cf2SChris Mason 		}
17082cc58cf2SChris Mason 	}
17092cc58cf2SChris Mason 
17102cc58cf2SChris Mason 	/* first, try to make some room in the middle buffer */
17115f39d397SChris Mason 	if (left) {
17125f39d397SChris Mason 		orig_slot += btrfs_header_nritems(left);
1713bce4eae9SChris Mason 		wret = push_node_left(trans, root, left, mid, 1);
171479f95c82SChris Mason 		if (wret < 0)
171579f95c82SChris Mason 			ret = wret;
1716bb803951SChris Mason 	}
171779f95c82SChris Mason 
171879f95c82SChris Mason 	/*
171979f95c82SChris Mason 	 * then try to empty the right most buffer into the middle
172079f95c82SChris Mason 	 */
17215f39d397SChris Mason 	if (right) {
1722971a1f66SChris Mason 		wret = push_node_left(trans, root, mid, right, 1);
172354aa1f4dSChris Mason 		if (wret < 0 && wret != -ENOSPC)
172479f95c82SChris Mason 			ret = wret;
17255f39d397SChris Mason 		if (btrfs_header_nritems(right) == 0) {
17265f39d397SChris Mason 			clean_tree_block(trans, root, right);
1727925baeddSChris Mason 			btrfs_tree_unlock(right);
1728f3ea38daSJan Schmidt 			del_ptr(trans, root, path, level + 1, pslot + 1, 1);
1729f0486c68SYan, Zheng 			root_sub_used(root, right->len);
17305581a51aSJan Schmidt 			btrfs_free_tree_block(trans, root, right, 0, 1);
17313083ee2eSJosef Bacik 			free_extent_buffer_stale(right);
1732f0486c68SYan, Zheng 			right = NULL;
1733bb803951SChris Mason 		} else {
17345f39d397SChris Mason 			struct btrfs_disk_key right_key;
17355f39d397SChris Mason 			btrfs_node_key(right, &right_key, 0);
1736f230475eSJan Schmidt 			tree_mod_log_set_node_key(root->fs_info, parent,
1737f230475eSJan Schmidt 						  &right_key, pslot + 1, 0);
17385f39d397SChris Mason 			btrfs_set_node_key(parent, &right_key, pslot + 1);
17395f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
1740bb803951SChris Mason 		}
1741bb803951SChris Mason 	}
17425f39d397SChris Mason 	if (btrfs_header_nritems(mid) == 1) {
174379f95c82SChris Mason 		/*
174479f95c82SChris Mason 		 * we're not allowed to leave a node with one item in the
174579f95c82SChris Mason 		 * tree during a delete.  A deletion from lower in the tree
174679f95c82SChris Mason 		 * could try to delete the only pointer in this node.
174779f95c82SChris Mason 		 * So, pull some keys from the left.
174879f95c82SChris Mason 		 * There has to be a left pointer at this point because
174979f95c82SChris Mason 		 * otherwise we would have pulled some pointers from the
175079f95c82SChris Mason 		 * right
175179f95c82SChris Mason 		 */
1752305a26afSMark Fasheh 		if (!left) {
1753305a26afSMark Fasheh 			ret = -EROFS;
1754305a26afSMark Fasheh 			btrfs_std_error(root->fs_info, ret);
1755305a26afSMark Fasheh 			goto enospc;
1756305a26afSMark Fasheh 		}
17575f39d397SChris Mason 		wret = balance_node_right(trans, root, mid, left);
175854aa1f4dSChris Mason 		if (wret < 0) {
175979f95c82SChris Mason 			ret = wret;
176054aa1f4dSChris Mason 			goto enospc;
176154aa1f4dSChris Mason 		}
1762bce4eae9SChris Mason 		if (wret == 1) {
1763bce4eae9SChris Mason 			wret = push_node_left(trans, root, left, mid, 1);
1764bce4eae9SChris Mason 			if (wret < 0)
1765bce4eae9SChris Mason 				ret = wret;
1766bce4eae9SChris Mason 		}
176779f95c82SChris Mason 		BUG_ON(wret == 1);
176879f95c82SChris Mason 	}
17695f39d397SChris Mason 	if (btrfs_header_nritems(mid) == 0) {
17705f39d397SChris Mason 		clean_tree_block(trans, root, mid);
1771925baeddSChris Mason 		btrfs_tree_unlock(mid);
1772f3ea38daSJan Schmidt 		del_ptr(trans, root, path, level + 1, pslot, 1);
1773f0486c68SYan, Zheng 		root_sub_used(root, mid->len);
17745581a51aSJan Schmidt 		btrfs_free_tree_block(trans, root, mid, 0, 1);
17753083ee2eSJosef Bacik 		free_extent_buffer_stale(mid);
1776f0486c68SYan, Zheng 		mid = NULL;
177779f95c82SChris Mason 	} else {
177879f95c82SChris Mason 		/* update the parent key to reflect our changes */
17795f39d397SChris Mason 		struct btrfs_disk_key mid_key;
17805f39d397SChris Mason 		btrfs_node_key(mid, &mid_key, 0);
1781f230475eSJan Schmidt 		tree_mod_log_set_node_key(root->fs_info, parent, &mid_key,
1782f230475eSJan Schmidt 					  pslot, 0);
17835f39d397SChris Mason 		btrfs_set_node_key(parent, &mid_key, pslot);
17845f39d397SChris Mason 		btrfs_mark_buffer_dirty(parent);
178579f95c82SChris Mason 	}
1786bb803951SChris Mason 
178779f95c82SChris Mason 	/* update the path */
17885f39d397SChris Mason 	if (left) {
17895f39d397SChris Mason 		if (btrfs_header_nritems(left) > orig_slot) {
17905f39d397SChris Mason 			extent_buffer_get(left);
1791925baeddSChris Mason 			/* left was locked after cow */
17925f39d397SChris Mason 			path->nodes[level] = left;
1793bb803951SChris Mason 			path->slots[level + 1] -= 1;
1794bb803951SChris Mason 			path->slots[level] = orig_slot;
1795925baeddSChris Mason 			if (mid) {
1796925baeddSChris Mason 				btrfs_tree_unlock(mid);
17975f39d397SChris Mason 				free_extent_buffer(mid);
1798925baeddSChris Mason 			}
1799bb803951SChris Mason 		} else {
18005f39d397SChris Mason 			orig_slot -= btrfs_header_nritems(left);
1801bb803951SChris Mason 			path->slots[level] = orig_slot;
1802bb803951SChris Mason 		}
1803bb803951SChris Mason 	}
180479f95c82SChris Mason 	/* double check we haven't messed things up */
1805e20d96d6SChris Mason 	if (orig_ptr !=
18065f39d397SChris Mason 	    btrfs_node_blockptr(path->nodes[level], path->slots[level]))
180779f95c82SChris Mason 		BUG();
180854aa1f4dSChris Mason enospc:
1809925baeddSChris Mason 	if (right) {
1810925baeddSChris Mason 		btrfs_tree_unlock(right);
18115f39d397SChris Mason 		free_extent_buffer(right);
1812925baeddSChris Mason 	}
1813925baeddSChris Mason 	if (left) {
1814925baeddSChris Mason 		if (path->nodes[level] != left)
1815925baeddSChris Mason 			btrfs_tree_unlock(left);
18165f39d397SChris Mason 		free_extent_buffer(left);
1817925baeddSChris Mason 	}
1818bb803951SChris Mason 	return ret;
1819bb803951SChris Mason }
1820bb803951SChris Mason 
1821d352ac68SChris Mason /* Node balancing for insertion.  Here we only split or push nodes around
1822d352ac68SChris Mason  * when they are completely full.  This is also done top down, so we
1823d352ac68SChris Mason  * have to be pessimistic.
1824d352ac68SChris Mason  */
1825d397712bSChris Mason static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1826e66f709bSChris Mason 					  struct btrfs_root *root,
1827e66f709bSChris Mason 					  struct btrfs_path *path, int level)
1828e66f709bSChris Mason {
18295f39d397SChris Mason 	struct extent_buffer *right = NULL;
18305f39d397SChris Mason 	struct extent_buffer *mid;
18315f39d397SChris Mason 	struct extent_buffer *left = NULL;
18325f39d397SChris Mason 	struct extent_buffer *parent = NULL;
1833e66f709bSChris Mason 	int ret = 0;
1834e66f709bSChris Mason 	int wret;
1835e66f709bSChris Mason 	int pslot;
1836e66f709bSChris Mason 	int orig_slot = path->slots[level];
1837e66f709bSChris Mason 
1838e66f709bSChris Mason 	if (level == 0)
1839e66f709bSChris Mason 		return 1;
1840e66f709bSChris Mason 
18415f39d397SChris Mason 	mid = path->nodes[level];
18427bb86316SChris Mason 	WARN_ON(btrfs_header_generation(mid) != trans->transid);
1843e66f709bSChris Mason 
1844a05a9bb1SLi Zefan 	if (level < BTRFS_MAX_LEVEL - 1) {
18455f39d397SChris Mason 		parent = path->nodes[level + 1];
1846e66f709bSChris Mason 		pslot = path->slots[level + 1];
1847a05a9bb1SLi Zefan 	}
1848e66f709bSChris Mason 
18495f39d397SChris Mason 	if (!parent)
1850e66f709bSChris Mason 		return 1;
1851e66f709bSChris Mason 
18525f39d397SChris Mason 	left = read_node_slot(root, parent, pslot - 1);
1853e66f709bSChris Mason 
1854e66f709bSChris Mason 	/* first, try to make some room in the middle buffer */
18555f39d397SChris Mason 	if (left) {
1856e66f709bSChris Mason 		u32 left_nr;
1857925baeddSChris Mason 
1858925baeddSChris Mason 		btrfs_tree_lock(left);
1859b4ce94deSChris Mason 		btrfs_set_lock_blocking(left);
1860b4ce94deSChris Mason 
18615f39d397SChris Mason 		left_nr = btrfs_header_nritems(left);
186233ade1f8SChris Mason 		if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
186333ade1f8SChris Mason 			wret = 1;
186433ade1f8SChris Mason 		} else {
18655f39d397SChris Mason 			ret = btrfs_cow_block(trans, root, left, parent,
18669fa8cfe7SChris Mason 					      pslot - 1, &left);
186754aa1f4dSChris Mason 			if (ret)
186854aa1f4dSChris Mason 				wret = 1;
186954aa1f4dSChris Mason 			else {
187054aa1f4dSChris Mason 				wret = push_node_left(trans, root,
1871971a1f66SChris Mason 						      left, mid, 0);
187254aa1f4dSChris Mason 			}
187333ade1f8SChris Mason 		}
1874e66f709bSChris Mason 		if (wret < 0)
1875e66f709bSChris Mason 			ret = wret;
1876e66f709bSChris Mason 		if (wret == 0) {
18775f39d397SChris Mason 			struct btrfs_disk_key disk_key;
1878e66f709bSChris Mason 			orig_slot += left_nr;
18795f39d397SChris Mason 			btrfs_node_key(mid, &disk_key, 0);
1880f230475eSJan Schmidt 			tree_mod_log_set_node_key(root->fs_info, parent,
1881f230475eSJan Schmidt 						  &disk_key, pslot, 0);
18825f39d397SChris Mason 			btrfs_set_node_key(parent, &disk_key, pslot);
18835f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
18845f39d397SChris Mason 			if (btrfs_header_nritems(left) > orig_slot) {
18855f39d397SChris Mason 				path->nodes[level] = left;
1886e66f709bSChris Mason 				path->slots[level + 1] -= 1;
1887e66f709bSChris Mason 				path->slots[level] = orig_slot;
1888925baeddSChris Mason 				btrfs_tree_unlock(mid);
18895f39d397SChris Mason 				free_extent_buffer(mid);
1890e66f709bSChris Mason 			} else {
1891e66f709bSChris Mason 				orig_slot -=
18925f39d397SChris Mason 					btrfs_header_nritems(left);
1893e66f709bSChris Mason 				path->slots[level] = orig_slot;
1894925baeddSChris Mason 				btrfs_tree_unlock(left);
18955f39d397SChris Mason 				free_extent_buffer(left);
1896e66f709bSChris Mason 			}
1897e66f709bSChris Mason 			return 0;
1898e66f709bSChris Mason 		}
1899925baeddSChris Mason 		btrfs_tree_unlock(left);
19005f39d397SChris Mason 		free_extent_buffer(left);
1901e66f709bSChris Mason 	}
19025f39d397SChris Mason 	right = read_node_slot(root, parent, pslot + 1);
1903e66f709bSChris Mason 
1904e66f709bSChris Mason 	/*
1905e66f709bSChris Mason 	 * then try to empty the right most buffer into the middle
1906e66f709bSChris Mason 	 */
19075f39d397SChris Mason 	if (right) {
190833ade1f8SChris Mason 		u32 right_nr;
1909b4ce94deSChris Mason 
1910925baeddSChris Mason 		btrfs_tree_lock(right);
1911b4ce94deSChris Mason 		btrfs_set_lock_blocking(right);
1912b4ce94deSChris Mason 
19135f39d397SChris Mason 		right_nr = btrfs_header_nritems(right);
191433ade1f8SChris Mason 		if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
191533ade1f8SChris Mason 			wret = 1;
191633ade1f8SChris Mason 		} else {
19175f39d397SChris Mason 			ret = btrfs_cow_block(trans, root, right,
19185f39d397SChris Mason 					      parent, pslot + 1,
19199fa8cfe7SChris Mason 					      &right);
192054aa1f4dSChris Mason 			if (ret)
192154aa1f4dSChris Mason 				wret = 1;
192254aa1f4dSChris Mason 			else {
192333ade1f8SChris Mason 				wret = balance_node_right(trans, root,
19245f39d397SChris Mason 							  right, mid);
192533ade1f8SChris Mason 			}
192654aa1f4dSChris Mason 		}
1927e66f709bSChris Mason 		if (wret < 0)
1928e66f709bSChris Mason 			ret = wret;
1929e66f709bSChris Mason 		if (wret == 0) {
19305f39d397SChris Mason 			struct btrfs_disk_key disk_key;
19315f39d397SChris Mason 
19325f39d397SChris Mason 			btrfs_node_key(right, &disk_key, 0);
1933f230475eSJan Schmidt 			tree_mod_log_set_node_key(root->fs_info, parent,
1934f230475eSJan Schmidt 						  &disk_key, pslot + 1, 0);
19355f39d397SChris Mason 			btrfs_set_node_key(parent, &disk_key, pslot + 1);
19365f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
19375f39d397SChris Mason 
19385f39d397SChris Mason 			if (btrfs_header_nritems(mid) <= orig_slot) {
19395f39d397SChris Mason 				path->nodes[level] = right;
1940e66f709bSChris Mason 				path->slots[level + 1] += 1;
1941e66f709bSChris Mason 				path->slots[level] = orig_slot -
19425f39d397SChris Mason 					btrfs_header_nritems(mid);
1943925baeddSChris Mason 				btrfs_tree_unlock(mid);
19445f39d397SChris Mason 				free_extent_buffer(mid);
1945e66f709bSChris Mason 			} else {
1946925baeddSChris Mason 				btrfs_tree_unlock(right);
19475f39d397SChris Mason 				free_extent_buffer(right);
1948e66f709bSChris Mason 			}
1949e66f709bSChris Mason 			return 0;
1950e66f709bSChris Mason 		}
1951925baeddSChris Mason 		btrfs_tree_unlock(right);
19525f39d397SChris Mason 		free_extent_buffer(right);
1953e66f709bSChris Mason 	}
1954e66f709bSChris Mason 	return 1;
1955e66f709bSChris Mason }
1956e66f709bSChris Mason 
195774123bd7SChris Mason /*
1958d352ac68SChris Mason  * readahead one full node of leaves, finding things that are close
1959d352ac68SChris Mason  * to the block in 'slot', and triggering ra on them.
19603c69faecSChris Mason  */
1961c8c42864SChris Mason static void reada_for_search(struct btrfs_root *root,
1962e02119d5SChris Mason 			     struct btrfs_path *path,
196301f46658SChris Mason 			     int level, int slot, u64 objectid)
19643c69faecSChris Mason {
19655f39d397SChris Mason 	struct extent_buffer *node;
196601f46658SChris Mason 	struct btrfs_disk_key disk_key;
19673c69faecSChris Mason 	u32 nritems;
19683c69faecSChris Mason 	u64 search;
1969a7175319SChris Mason 	u64 target;
19706b80053dSChris Mason 	u64 nread = 0;
1971cb25c2eaSJosef Bacik 	u64 gen;
19723c69faecSChris Mason 	int direction = path->reada;
19735f39d397SChris Mason 	struct extent_buffer *eb;
19746b80053dSChris Mason 	u32 nr;
19756b80053dSChris Mason 	u32 blocksize;
19766b80053dSChris Mason 	u32 nscan = 0;
1977db94535dSChris Mason 
1978a6b6e75eSChris Mason 	if (level != 1)
19793c69faecSChris Mason 		return;
19803c69faecSChris Mason 
19816702ed49SChris Mason 	if (!path->nodes[level])
19826702ed49SChris Mason 		return;
19836702ed49SChris Mason 
19845f39d397SChris Mason 	node = path->nodes[level];
1985925baeddSChris Mason 
19863c69faecSChris Mason 	search = btrfs_node_blockptr(node, slot);
19876b80053dSChris Mason 	blocksize = btrfs_level_size(root, level - 1);
19886b80053dSChris Mason 	eb = btrfs_find_tree_block(root, search, blocksize);
19895f39d397SChris Mason 	if (eb) {
19905f39d397SChris Mason 		free_extent_buffer(eb);
19913c69faecSChris Mason 		return;
19923c69faecSChris Mason 	}
19933c69faecSChris Mason 
1994a7175319SChris Mason 	target = search;
19956b80053dSChris Mason 
19965f39d397SChris Mason 	nritems = btrfs_header_nritems(node);
19976b80053dSChris Mason 	nr = slot;
199825b8b936SJosef Bacik 
19993c69faecSChris Mason 	while (1) {
20006b80053dSChris Mason 		if (direction < 0) {
20016b80053dSChris Mason 			if (nr == 0)
20023c69faecSChris Mason 				break;
20036b80053dSChris Mason 			nr--;
20046b80053dSChris Mason 		} else if (direction > 0) {
20056b80053dSChris Mason 			nr++;
20066b80053dSChris Mason 			if (nr >= nritems)
20076b80053dSChris Mason 				break;
20083c69faecSChris Mason 		}
200901f46658SChris Mason 		if (path->reada < 0 && objectid) {
201001f46658SChris Mason 			btrfs_node_key(node, &disk_key, nr);
201101f46658SChris Mason 			if (btrfs_disk_key_objectid(&disk_key) != objectid)
201201f46658SChris Mason 				break;
201301f46658SChris Mason 		}
20146b80053dSChris Mason 		search = btrfs_node_blockptr(node, nr);
2015a7175319SChris Mason 		if ((search <= target && target - search <= 65536) ||
2016a7175319SChris Mason 		    (search > target && search - target <= 65536)) {
2017cb25c2eaSJosef Bacik 			gen = btrfs_node_ptr_generation(node, nr);
2018cb25c2eaSJosef Bacik 			readahead_tree_block(root, search, blocksize, gen);
20196b80053dSChris Mason 			nread += blocksize;
20203c69faecSChris Mason 		}
20216b80053dSChris Mason 		nscan++;
2022a7175319SChris Mason 		if ((nread > 65536 || nscan > 32))
20236b80053dSChris Mason 			break;
20243c69faecSChris Mason 	}
20253c69faecSChris Mason }
2026925baeddSChris Mason 
2027d352ac68SChris Mason /*
2028b4ce94deSChris Mason  * returns -EAGAIN if it had to drop the path, or zero if everything was in
2029b4ce94deSChris Mason  * cache
2030b4ce94deSChris Mason  */
2031b4ce94deSChris Mason static noinline int reada_for_balance(struct btrfs_root *root,
2032b4ce94deSChris Mason 				      struct btrfs_path *path, int level)
2033b4ce94deSChris Mason {
2034b4ce94deSChris Mason 	int slot;
2035b4ce94deSChris Mason 	int nritems;
2036b4ce94deSChris Mason 	struct extent_buffer *parent;
2037b4ce94deSChris Mason 	struct extent_buffer *eb;
2038b4ce94deSChris Mason 	u64 gen;
2039b4ce94deSChris Mason 	u64 block1 = 0;
2040b4ce94deSChris Mason 	u64 block2 = 0;
2041b4ce94deSChris Mason 	int ret = 0;
2042b4ce94deSChris Mason 	int blocksize;
2043b4ce94deSChris Mason 
20448c594ea8SChris Mason 	parent = path->nodes[level + 1];
2045b4ce94deSChris Mason 	if (!parent)
2046b4ce94deSChris Mason 		return 0;
2047b4ce94deSChris Mason 
2048b4ce94deSChris Mason 	nritems = btrfs_header_nritems(parent);
20498c594ea8SChris Mason 	slot = path->slots[level + 1];
2050b4ce94deSChris Mason 	blocksize = btrfs_level_size(root, level);
2051b4ce94deSChris Mason 
2052b4ce94deSChris Mason 	if (slot > 0) {
2053b4ce94deSChris Mason 		block1 = btrfs_node_blockptr(parent, slot - 1);
2054b4ce94deSChris Mason 		gen = btrfs_node_ptr_generation(parent, slot - 1);
2055b4ce94deSChris Mason 		eb = btrfs_find_tree_block(root, block1, blocksize);
2056b9fab919SChris Mason 		/*
2057b9fab919SChris Mason 		 * if we get -eagain from btrfs_buffer_uptodate, we
2058b9fab919SChris Mason 		 * don't want to return eagain here.  That will loop
2059b9fab919SChris Mason 		 * forever
2060b9fab919SChris Mason 		 */
2061b9fab919SChris Mason 		if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
2062b4ce94deSChris Mason 			block1 = 0;
2063b4ce94deSChris Mason 		free_extent_buffer(eb);
2064b4ce94deSChris Mason 	}
20658c594ea8SChris Mason 	if (slot + 1 < nritems) {
2066b4ce94deSChris Mason 		block2 = btrfs_node_blockptr(parent, slot + 1);
2067b4ce94deSChris Mason 		gen = btrfs_node_ptr_generation(parent, slot + 1);
2068b4ce94deSChris Mason 		eb = btrfs_find_tree_block(root, block2, blocksize);
2069b9fab919SChris Mason 		if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
2070b4ce94deSChris Mason 			block2 = 0;
2071b4ce94deSChris Mason 		free_extent_buffer(eb);
2072b4ce94deSChris Mason 	}
2073b4ce94deSChris Mason 	if (block1 || block2) {
2074b4ce94deSChris Mason 		ret = -EAGAIN;
20758c594ea8SChris Mason 
20768c594ea8SChris Mason 		/* release the whole path */
2077b3b4aa74SDavid Sterba 		btrfs_release_path(path);
20788c594ea8SChris Mason 
20798c594ea8SChris Mason 		/* read the blocks */
2080b4ce94deSChris Mason 		if (block1)
2081b4ce94deSChris Mason 			readahead_tree_block(root, block1, blocksize, 0);
2082b4ce94deSChris Mason 		if (block2)
2083b4ce94deSChris Mason 			readahead_tree_block(root, block2, blocksize, 0);
2084b4ce94deSChris Mason 
2085b4ce94deSChris Mason 		if (block1) {
2086b4ce94deSChris Mason 			eb = read_tree_block(root, block1, blocksize, 0);
2087b4ce94deSChris Mason 			free_extent_buffer(eb);
2088b4ce94deSChris Mason 		}
20898c594ea8SChris Mason 		if (block2) {
2090b4ce94deSChris Mason 			eb = read_tree_block(root, block2, blocksize, 0);
2091b4ce94deSChris Mason 			free_extent_buffer(eb);
2092b4ce94deSChris Mason 		}
2093b4ce94deSChris Mason 	}
2094b4ce94deSChris Mason 	return ret;
2095b4ce94deSChris Mason }
2096b4ce94deSChris Mason 
2097b4ce94deSChris Mason 
2098b4ce94deSChris Mason /*
2099d397712bSChris Mason  * when we walk down the tree, it is usually safe to unlock the higher layers
2100d397712bSChris Mason  * in the tree.  The exceptions are when our path goes through slot 0, because
2101d397712bSChris Mason  * operations on the tree might require changing key pointers higher up in the
2102d397712bSChris Mason  * tree.
2103d352ac68SChris Mason  *
2104d397712bSChris Mason  * callers might also have set path->keep_locks, which tells this code to keep
2105d397712bSChris Mason  * the lock if the path points to the last slot in the block.  This is part of
2106d397712bSChris Mason  * walking through the tree, and selecting the next slot in the higher block.
2107d352ac68SChris Mason  *
2108d397712bSChris Mason  * lowest_unlock sets the lowest level in the tree we're allowed to unlock.  so
2109d397712bSChris Mason  * if lowest_unlock is 1, level 0 won't be unlocked
2110d352ac68SChris Mason  */
2111e02119d5SChris Mason static noinline void unlock_up(struct btrfs_path *path, int level,
2112f7c79f30SChris Mason 			       int lowest_unlock, int min_write_lock_level,
2113f7c79f30SChris Mason 			       int *write_lock_level)
2114925baeddSChris Mason {
2115925baeddSChris Mason 	int i;
2116925baeddSChris Mason 	int skip_level = level;
2117051e1b9fSChris Mason 	int no_skips = 0;
2118925baeddSChris Mason 	struct extent_buffer *t;
2119925baeddSChris Mason 
2120925baeddSChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2121925baeddSChris Mason 		if (!path->nodes[i])
2122925baeddSChris Mason 			break;
2123925baeddSChris Mason 		if (!path->locks[i])
2124925baeddSChris Mason 			break;
2125051e1b9fSChris Mason 		if (!no_skips && path->slots[i] == 0) {
2126925baeddSChris Mason 			skip_level = i + 1;
2127925baeddSChris Mason 			continue;
2128925baeddSChris Mason 		}
2129051e1b9fSChris Mason 		if (!no_skips && path->keep_locks) {
2130925baeddSChris Mason 			u32 nritems;
2131925baeddSChris Mason 			t = path->nodes[i];
2132925baeddSChris Mason 			nritems = btrfs_header_nritems(t);
2133051e1b9fSChris Mason 			if (nritems < 1 || path->slots[i] >= nritems - 1) {
2134925baeddSChris Mason 				skip_level = i + 1;
2135925baeddSChris Mason 				continue;
2136925baeddSChris Mason 			}
2137925baeddSChris Mason 		}
2138051e1b9fSChris Mason 		if (skip_level < i && i >= lowest_unlock)
2139051e1b9fSChris Mason 			no_skips = 1;
2140051e1b9fSChris Mason 
2141925baeddSChris Mason 		t = path->nodes[i];
2142925baeddSChris Mason 		if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
2143bd681513SChris Mason 			btrfs_tree_unlock_rw(t, path->locks[i]);
2144925baeddSChris Mason 			path->locks[i] = 0;
2145f7c79f30SChris Mason 			if (write_lock_level &&
2146f7c79f30SChris Mason 			    i > min_write_lock_level &&
2147f7c79f30SChris Mason 			    i <= *write_lock_level) {
2148f7c79f30SChris Mason 				*write_lock_level = i - 1;
2149f7c79f30SChris Mason 			}
2150925baeddSChris Mason 		}
2151925baeddSChris Mason 	}
2152925baeddSChris Mason }
2153925baeddSChris Mason 
21543c69faecSChris Mason /*
2155b4ce94deSChris Mason  * This releases any locks held in the path starting at level and
2156b4ce94deSChris Mason  * going all the way up to the root.
2157b4ce94deSChris Mason  *
2158b4ce94deSChris Mason  * btrfs_search_slot will keep the lock held on higher nodes in a few
2159b4ce94deSChris Mason  * corner cases, such as COW of the block at slot zero in the node.  This
2160b4ce94deSChris Mason  * ignores those rules, and it should only be called when there are no
2161b4ce94deSChris Mason  * more updates to be done higher up in the tree.
2162b4ce94deSChris Mason  */
2163b4ce94deSChris Mason noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2164b4ce94deSChris Mason {
2165b4ce94deSChris Mason 	int i;
2166b4ce94deSChris Mason 
21675d4f98a2SYan Zheng 	if (path->keep_locks)
2168b4ce94deSChris Mason 		return;
2169b4ce94deSChris Mason 
2170b4ce94deSChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2171b4ce94deSChris Mason 		if (!path->nodes[i])
217212f4daccSChris Mason 			continue;
2173b4ce94deSChris Mason 		if (!path->locks[i])
217412f4daccSChris Mason 			continue;
2175bd681513SChris Mason 		btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
2176b4ce94deSChris Mason 		path->locks[i] = 0;
2177b4ce94deSChris Mason 	}
2178b4ce94deSChris Mason }
2179b4ce94deSChris Mason 
2180b4ce94deSChris Mason /*
2181c8c42864SChris Mason  * helper function for btrfs_search_slot.  The goal is to find a block
2182c8c42864SChris Mason  * in cache without setting the path to blocking.  If we find the block
2183c8c42864SChris Mason  * we return zero and the path is unchanged.
2184c8c42864SChris Mason  *
2185c8c42864SChris Mason  * If we can't find the block, we set the path blocking and do some
2186c8c42864SChris Mason  * reada.  -EAGAIN is returned and the search must be repeated.
2187c8c42864SChris Mason  */
2188c8c42864SChris Mason static int
2189c8c42864SChris Mason read_block_for_search(struct btrfs_trans_handle *trans,
2190c8c42864SChris Mason 		       struct btrfs_root *root, struct btrfs_path *p,
2191c8c42864SChris Mason 		       struct extent_buffer **eb_ret, int level, int slot,
21925d9e75c4SJan Schmidt 		       struct btrfs_key *key, u64 time_seq)
2193c8c42864SChris Mason {
2194c8c42864SChris Mason 	u64 blocknr;
2195c8c42864SChris Mason 	u64 gen;
2196c8c42864SChris Mason 	u32 blocksize;
2197c8c42864SChris Mason 	struct extent_buffer *b = *eb_ret;
2198c8c42864SChris Mason 	struct extent_buffer *tmp;
219976a05b35SChris Mason 	int ret;
2200c8c42864SChris Mason 
2201c8c42864SChris Mason 	blocknr = btrfs_node_blockptr(b, slot);
2202c8c42864SChris Mason 	gen = btrfs_node_ptr_generation(b, slot);
2203c8c42864SChris Mason 	blocksize = btrfs_level_size(root, level - 1);
2204c8c42864SChris Mason 
2205c8c42864SChris Mason 	tmp = btrfs_find_tree_block(root, blocknr, blocksize);
2206cb44921aSChris Mason 	if (tmp) {
2207b9fab919SChris Mason 		/* first we do an atomic uptodate check */
2208b9fab919SChris Mason 		if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
2209b9fab919SChris Mason 			if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
221076a05b35SChris Mason 				/*
2211cb44921aSChris Mason 				 * we found an up to date block without
2212cb44921aSChris Mason 				 * sleeping, return
221376a05b35SChris Mason 				 * right away
221476a05b35SChris Mason 				 */
2215c8c42864SChris Mason 				*eb_ret = tmp;
2216c8c42864SChris Mason 				return 0;
2217c8c42864SChris Mason 			}
2218cb44921aSChris Mason 			/* the pages were up to date, but we failed
2219cb44921aSChris Mason 			 * the generation number check.  Do a full
2220cb44921aSChris Mason 			 * read for the generation number that is correct.
2221cb44921aSChris Mason 			 * We must do this without dropping locks so
2222cb44921aSChris Mason 			 * we can trust our generation number
2223cb44921aSChris Mason 			 */
2224cb44921aSChris Mason 			free_extent_buffer(tmp);
2225bd681513SChris Mason 			btrfs_set_path_blocking(p);
2226bd681513SChris Mason 
2227b9fab919SChris Mason 			/* now we're allowed to do a blocking uptodate check */
2228cb44921aSChris Mason 			tmp = read_tree_block(root, blocknr, blocksize, gen);
2229b9fab919SChris Mason 			if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
2230cb44921aSChris Mason 				*eb_ret = tmp;
2231cb44921aSChris Mason 				return 0;
2232cb44921aSChris Mason 			}
2233cb44921aSChris Mason 			free_extent_buffer(tmp);
2234b3b4aa74SDavid Sterba 			btrfs_release_path(p);
2235cb44921aSChris Mason 			return -EIO;
2236cb44921aSChris Mason 		}
2237cb44921aSChris Mason 	}
2238c8c42864SChris Mason 
2239c8c42864SChris Mason 	/*
2240c8c42864SChris Mason 	 * reduce lock contention at high levels
2241c8c42864SChris Mason 	 * of the btree by dropping locks before
224276a05b35SChris Mason 	 * we read.  Don't release the lock on the current
224376a05b35SChris Mason 	 * level because we need to walk this node to figure
224476a05b35SChris Mason 	 * out which blocks to read.
2245c8c42864SChris Mason 	 */
22468c594ea8SChris Mason 	btrfs_unlock_up_safe(p, level + 1);
22478c594ea8SChris Mason 	btrfs_set_path_blocking(p);
22488c594ea8SChris Mason 
2249c8c42864SChris Mason 	free_extent_buffer(tmp);
2250c8c42864SChris Mason 	if (p->reada)
2251c8c42864SChris Mason 		reada_for_search(root, p, level, slot, key->objectid);
2252c8c42864SChris Mason 
2253b3b4aa74SDavid Sterba 	btrfs_release_path(p);
225476a05b35SChris Mason 
225576a05b35SChris Mason 	ret = -EAGAIN;
22565bdd3536SYan, Zheng 	tmp = read_tree_block(root, blocknr, blocksize, 0);
225776a05b35SChris Mason 	if (tmp) {
225876a05b35SChris Mason 		/*
225976a05b35SChris Mason 		 * If the read above didn't mark this buffer up to date,
226076a05b35SChris Mason 		 * it will never end up being up to date.  Set ret to EIO now
226176a05b35SChris Mason 		 * and give up so that our caller doesn't loop forever
226276a05b35SChris Mason 		 * on our EAGAINs.
226376a05b35SChris Mason 		 */
2264b9fab919SChris Mason 		if (!btrfs_buffer_uptodate(tmp, 0, 0))
226576a05b35SChris Mason 			ret = -EIO;
2266c8c42864SChris Mason 		free_extent_buffer(tmp);
226776a05b35SChris Mason 	}
226876a05b35SChris Mason 	return ret;
2269c8c42864SChris Mason }
2270c8c42864SChris Mason 
2271c8c42864SChris Mason /*
2272c8c42864SChris Mason  * helper function for btrfs_search_slot.  This does all of the checks
2273c8c42864SChris Mason  * for node-level blocks and does any balancing required based on
2274c8c42864SChris Mason  * the ins_len.
2275c8c42864SChris Mason  *
2276c8c42864SChris Mason  * If no extra work was required, zero is returned.  If we had to
2277c8c42864SChris Mason  * drop the path, -EAGAIN is returned and btrfs_search_slot must
2278c8c42864SChris Mason  * start over
2279c8c42864SChris Mason  */
2280c8c42864SChris Mason static int
2281c8c42864SChris Mason setup_nodes_for_search(struct btrfs_trans_handle *trans,
2282c8c42864SChris Mason 		       struct btrfs_root *root, struct btrfs_path *p,
2283bd681513SChris Mason 		       struct extent_buffer *b, int level, int ins_len,
2284bd681513SChris Mason 		       int *write_lock_level)
2285c8c42864SChris Mason {
2286c8c42864SChris Mason 	int ret;
2287c8c42864SChris Mason 	if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2288c8c42864SChris Mason 	    BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2289c8c42864SChris Mason 		int sret;
2290c8c42864SChris Mason 
2291bd681513SChris Mason 		if (*write_lock_level < level + 1) {
2292bd681513SChris Mason 			*write_lock_level = level + 1;
2293bd681513SChris Mason 			btrfs_release_path(p);
2294bd681513SChris Mason 			goto again;
2295bd681513SChris Mason 		}
2296bd681513SChris Mason 
2297c8c42864SChris Mason 		sret = reada_for_balance(root, p, level);
2298c8c42864SChris Mason 		if (sret)
2299c8c42864SChris Mason 			goto again;
2300c8c42864SChris Mason 
2301c8c42864SChris Mason 		btrfs_set_path_blocking(p);
2302c8c42864SChris Mason 		sret = split_node(trans, root, p, level);
2303bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
2304c8c42864SChris Mason 
2305c8c42864SChris Mason 		BUG_ON(sret > 0);
2306c8c42864SChris Mason 		if (sret) {
2307c8c42864SChris Mason 			ret = sret;
2308c8c42864SChris Mason 			goto done;
2309c8c42864SChris Mason 		}
2310c8c42864SChris Mason 		b = p->nodes[level];
2311c8c42864SChris Mason 	} else if (ins_len < 0 && btrfs_header_nritems(b) <
2312cfbb9308SChris Mason 		   BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
2313c8c42864SChris Mason 		int sret;
2314c8c42864SChris Mason 
2315bd681513SChris Mason 		if (*write_lock_level < level + 1) {
2316bd681513SChris Mason 			*write_lock_level = level + 1;
2317bd681513SChris Mason 			btrfs_release_path(p);
2318bd681513SChris Mason 			goto again;
2319bd681513SChris Mason 		}
2320bd681513SChris Mason 
2321c8c42864SChris Mason 		sret = reada_for_balance(root, p, level);
2322c8c42864SChris Mason 		if (sret)
2323c8c42864SChris Mason 			goto again;
2324c8c42864SChris Mason 
2325c8c42864SChris Mason 		btrfs_set_path_blocking(p);
2326c8c42864SChris Mason 		sret = balance_level(trans, root, p, level);
2327bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
2328c8c42864SChris Mason 
2329c8c42864SChris Mason 		if (sret) {
2330c8c42864SChris Mason 			ret = sret;
2331c8c42864SChris Mason 			goto done;
2332c8c42864SChris Mason 		}
2333c8c42864SChris Mason 		b = p->nodes[level];
2334c8c42864SChris Mason 		if (!b) {
2335b3b4aa74SDavid Sterba 			btrfs_release_path(p);
2336c8c42864SChris Mason 			goto again;
2337c8c42864SChris Mason 		}
2338c8c42864SChris Mason 		BUG_ON(btrfs_header_nritems(b) == 1);
2339c8c42864SChris Mason 	}
2340c8c42864SChris Mason 	return 0;
2341c8c42864SChris Mason 
2342c8c42864SChris Mason again:
2343c8c42864SChris Mason 	ret = -EAGAIN;
2344c8c42864SChris Mason done:
2345c8c42864SChris Mason 	return ret;
2346c8c42864SChris Mason }
2347c8c42864SChris Mason 
2348c8c42864SChris Mason /*
234974123bd7SChris Mason  * look for key in the tree.  path is filled in with nodes along the way
235074123bd7SChris Mason  * if key is found, we return zero and you can find the item in the leaf
235174123bd7SChris Mason  * level of the path (level 0)
235274123bd7SChris Mason  *
235374123bd7SChris Mason  * If the key isn't found, the path points to the slot where it should
2354aa5d6bedSChris Mason  * be inserted, and 1 is returned.  If there are other errors during the
2355aa5d6bedSChris Mason  * search a negative error number is returned.
235697571fd0SChris Mason  *
235797571fd0SChris Mason  * if ins_len > 0, nodes and leaves will be split as we walk down the
235897571fd0SChris Mason  * tree.  if ins_len < 0, nodes will be merged as we walk down the tree (if
235997571fd0SChris Mason  * possible)
236074123bd7SChris Mason  */
2361e089f05cSChris Mason int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2362e089f05cSChris Mason 		      *root, struct btrfs_key *key, struct btrfs_path *p, int
2363e089f05cSChris Mason 		      ins_len, int cow)
2364be0e5c09SChris Mason {
23655f39d397SChris Mason 	struct extent_buffer *b;
2366be0e5c09SChris Mason 	int slot;
2367be0e5c09SChris Mason 	int ret;
236833c66f43SYan Zheng 	int err;
2369be0e5c09SChris Mason 	int level;
2370925baeddSChris Mason 	int lowest_unlock = 1;
2371bd681513SChris Mason 	int root_lock;
2372bd681513SChris Mason 	/* everything at write_lock_level or lower must be write locked */
2373bd681513SChris Mason 	int write_lock_level = 0;
23749f3a7427SChris Mason 	u8 lowest_level = 0;
2375f7c79f30SChris Mason 	int min_write_lock_level;
23769f3a7427SChris Mason 
23776702ed49SChris Mason 	lowest_level = p->lowest_level;
2378323ac95bSChris Mason 	WARN_ON(lowest_level && ins_len > 0);
237922b0ebdaSChris Mason 	WARN_ON(p->nodes[0] != NULL);
238025179201SJosef Bacik 
2381bd681513SChris Mason 	if (ins_len < 0) {
2382925baeddSChris Mason 		lowest_unlock = 2;
238365b51a00SChris Mason 
2384bd681513SChris Mason 		/* when we are removing items, we might have to go up to level
2385bd681513SChris Mason 		 * two as we update tree pointers  Make sure we keep write
2386bd681513SChris Mason 		 * for those levels as well
2387bd681513SChris Mason 		 */
2388bd681513SChris Mason 		write_lock_level = 2;
2389bd681513SChris Mason 	} else if (ins_len > 0) {
2390bd681513SChris Mason 		/*
2391bd681513SChris Mason 		 * for inserting items, make sure we have a write lock on
2392bd681513SChris Mason 		 * level 1 so we can update keys
2393bd681513SChris Mason 		 */
2394bd681513SChris Mason 		write_lock_level = 1;
2395bd681513SChris Mason 	}
2396bd681513SChris Mason 
2397bd681513SChris Mason 	if (!cow)
2398bd681513SChris Mason 		write_lock_level = -1;
2399bd681513SChris Mason 
2400bd681513SChris Mason 	if (cow && (p->keep_locks || p->lowest_level))
2401bd681513SChris Mason 		write_lock_level = BTRFS_MAX_LEVEL;
2402bd681513SChris Mason 
2403f7c79f30SChris Mason 	min_write_lock_level = write_lock_level;
2404f7c79f30SChris Mason 
2405bb803951SChris Mason again:
2406bd681513SChris Mason 	/*
2407bd681513SChris Mason 	 * we try very hard to do read locks on the root
2408bd681513SChris Mason 	 */
2409bd681513SChris Mason 	root_lock = BTRFS_READ_LOCK;
2410bd681513SChris Mason 	level = 0;
24115d4f98a2SYan Zheng 	if (p->search_commit_root) {
2412bd681513SChris Mason 		/*
2413bd681513SChris Mason 		 * the commit roots are read only
2414bd681513SChris Mason 		 * so we always do read locks
2415bd681513SChris Mason 		 */
24165d4f98a2SYan Zheng 		b = root->commit_root;
24175d4f98a2SYan Zheng 		extent_buffer_get(b);
2418bd681513SChris Mason 		level = btrfs_header_level(b);
24195d4f98a2SYan Zheng 		if (!p->skip_locking)
2420bd681513SChris Mason 			btrfs_tree_read_lock(b);
24215d4f98a2SYan Zheng 	} else {
2422bd681513SChris Mason 		if (p->skip_locking) {
24235cd57b2cSChris Mason 			b = btrfs_root_node(root);
2424bd681513SChris Mason 			level = btrfs_header_level(b);
2425bd681513SChris Mason 		} else {
2426bd681513SChris Mason 			/* we don't know the level of the root node
2427bd681513SChris Mason 			 * until we actually have it read locked
2428bd681513SChris Mason 			 */
2429bd681513SChris Mason 			b = btrfs_read_lock_root_node(root);
2430bd681513SChris Mason 			level = btrfs_header_level(b);
2431bd681513SChris Mason 			if (level <= write_lock_level) {
2432bd681513SChris Mason 				/* whoops, must trade for write lock */
2433bd681513SChris Mason 				btrfs_tree_read_unlock(b);
2434bd681513SChris Mason 				free_extent_buffer(b);
2435925baeddSChris Mason 				b = btrfs_lock_root_node(root);
2436bd681513SChris Mason 				root_lock = BTRFS_WRITE_LOCK;
2437bd681513SChris Mason 
2438bd681513SChris Mason 				/* the level might have changed, check again */
2439bd681513SChris Mason 				level = btrfs_header_level(b);
24405d4f98a2SYan Zheng 			}
2441bd681513SChris Mason 		}
2442bd681513SChris Mason 	}
2443bd681513SChris Mason 	p->nodes[level] = b;
2444bd681513SChris Mason 	if (!p->skip_locking)
2445bd681513SChris Mason 		p->locks[level] = root_lock;
2446925baeddSChris Mason 
2447eb60ceacSChris Mason 	while (b) {
24485f39d397SChris Mason 		level = btrfs_header_level(b);
244965b51a00SChris Mason 
245065b51a00SChris Mason 		/*
245165b51a00SChris Mason 		 * setup the path here so we can release it under lock
245265b51a00SChris Mason 		 * contention with the cow code
245365b51a00SChris Mason 		 */
245402217ed2SChris Mason 		if (cow) {
2455c8c42864SChris Mason 			/*
2456c8c42864SChris Mason 			 * if we don't really need to cow this block
2457c8c42864SChris Mason 			 * then we don't want to set the path blocking,
2458c8c42864SChris Mason 			 * so we test it here
2459c8c42864SChris Mason 			 */
24605d4f98a2SYan Zheng 			if (!should_cow_block(trans, root, b))
246165b51a00SChris Mason 				goto cow_done;
24625d4f98a2SYan Zheng 
2463b4ce94deSChris Mason 			btrfs_set_path_blocking(p);
2464b4ce94deSChris Mason 
2465bd681513SChris Mason 			/*
2466bd681513SChris Mason 			 * must have write locks on this node and the
2467bd681513SChris Mason 			 * parent
2468bd681513SChris Mason 			 */
2469bd681513SChris Mason 			if (level + 1 > write_lock_level) {
2470bd681513SChris Mason 				write_lock_level = level + 1;
2471bd681513SChris Mason 				btrfs_release_path(p);
2472bd681513SChris Mason 				goto again;
2473bd681513SChris Mason 			}
2474bd681513SChris Mason 
247533c66f43SYan Zheng 			err = btrfs_cow_block(trans, root, b,
2476e20d96d6SChris Mason 					      p->nodes[level + 1],
24779fa8cfe7SChris Mason 					      p->slots[level + 1], &b);
247833c66f43SYan Zheng 			if (err) {
247933c66f43SYan Zheng 				ret = err;
248065b51a00SChris Mason 				goto done;
248154aa1f4dSChris Mason 			}
248202217ed2SChris Mason 		}
248365b51a00SChris Mason cow_done:
248402217ed2SChris Mason 		BUG_ON(!cow && ins_len);
248565b51a00SChris Mason 
2486eb60ceacSChris Mason 		p->nodes[level] = b;
2487bd681513SChris Mason 		btrfs_clear_path_blocking(p, NULL, 0);
2488b4ce94deSChris Mason 
2489b4ce94deSChris Mason 		/*
2490b4ce94deSChris Mason 		 * we have a lock on b and as long as we aren't changing
2491b4ce94deSChris Mason 		 * the tree, there is no way to for the items in b to change.
2492b4ce94deSChris Mason 		 * It is safe to drop the lock on our parent before we
2493b4ce94deSChris Mason 		 * go through the expensive btree search on b.
2494b4ce94deSChris Mason 		 *
2495b4ce94deSChris Mason 		 * If cow is true, then we might be changing slot zero,
2496b4ce94deSChris Mason 		 * which may require changing the parent.  So, we can't
2497b4ce94deSChris Mason 		 * drop the lock until after we know which slot we're
2498b4ce94deSChris Mason 		 * operating on.
2499b4ce94deSChris Mason 		 */
2500b4ce94deSChris Mason 		if (!cow)
2501b4ce94deSChris Mason 			btrfs_unlock_up_safe(p, level + 1);
2502b4ce94deSChris Mason 
25035f39d397SChris Mason 		ret = bin_search(b, key, level, &slot);
2504b4ce94deSChris Mason 
25055f39d397SChris Mason 		if (level != 0) {
250633c66f43SYan Zheng 			int dec = 0;
250733c66f43SYan Zheng 			if (ret && slot > 0) {
250833c66f43SYan Zheng 				dec = 1;
2509be0e5c09SChris Mason 				slot -= 1;
251033c66f43SYan Zheng 			}
2511be0e5c09SChris Mason 			p->slots[level] = slot;
251233c66f43SYan Zheng 			err = setup_nodes_for_search(trans, root, p, b, level,
2513bd681513SChris Mason 					     ins_len, &write_lock_level);
251433c66f43SYan Zheng 			if (err == -EAGAIN)
2515b4ce94deSChris Mason 				goto again;
251633c66f43SYan Zheng 			if (err) {
251733c66f43SYan Zheng 				ret = err;
251865b51a00SChris Mason 				goto done;
251933c66f43SYan Zheng 			}
25205c680ed6SChris Mason 			b = p->nodes[level];
25215c680ed6SChris Mason 			slot = p->slots[level];
2522b4ce94deSChris Mason 
2523bd681513SChris Mason 			/*
2524bd681513SChris Mason 			 * slot 0 is special, if we change the key
2525bd681513SChris Mason 			 * we have to update the parent pointer
2526bd681513SChris Mason 			 * which means we must have a write lock
2527bd681513SChris Mason 			 * on the parent
2528bd681513SChris Mason 			 */
2529bd681513SChris Mason 			if (slot == 0 && cow &&
2530bd681513SChris Mason 			    write_lock_level < level + 1) {
2531bd681513SChris Mason 				write_lock_level = level + 1;
2532bd681513SChris Mason 				btrfs_release_path(p);
2533bd681513SChris Mason 				goto again;
2534bd681513SChris Mason 			}
2535bd681513SChris Mason 
2536f7c79f30SChris Mason 			unlock_up(p, level, lowest_unlock,
2537f7c79f30SChris Mason 				  min_write_lock_level, &write_lock_level);
2538f9efa9c7SChris Mason 
2539925baeddSChris Mason 			if (level == lowest_level) {
254033c66f43SYan Zheng 				if (dec)
254133c66f43SYan Zheng 					p->slots[level]++;
25425b21f2edSZheng Yan 				goto done;
2543925baeddSChris Mason 			}
2544ca7a79adSChris Mason 
254533c66f43SYan Zheng 			err = read_block_for_search(trans, root, p,
25465d9e75c4SJan Schmidt 						    &b, level, slot, key, 0);
254733c66f43SYan Zheng 			if (err == -EAGAIN)
2548051e1b9fSChris Mason 				goto again;
254933c66f43SYan Zheng 			if (err) {
255033c66f43SYan Zheng 				ret = err;
255176a05b35SChris Mason 				goto done;
255233c66f43SYan Zheng 			}
255376a05b35SChris Mason 
2554b4ce94deSChris Mason 			if (!p->skip_locking) {
2555bd681513SChris Mason 				level = btrfs_header_level(b);
2556bd681513SChris Mason 				if (level <= write_lock_level) {
2557bd681513SChris Mason 					err = btrfs_try_tree_write_lock(b);
255833c66f43SYan Zheng 					if (!err) {
2559b4ce94deSChris Mason 						btrfs_set_path_blocking(p);
2560925baeddSChris Mason 						btrfs_tree_lock(b);
2561bd681513SChris Mason 						btrfs_clear_path_blocking(p, b,
2562bd681513SChris Mason 								  BTRFS_WRITE_LOCK);
2563b4ce94deSChris Mason 					}
2564bd681513SChris Mason 					p->locks[level] = BTRFS_WRITE_LOCK;
2565bd681513SChris Mason 				} else {
2566bd681513SChris Mason 					err = btrfs_try_tree_read_lock(b);
2567bd681513SChris Mason 					if (!err) {
2568bd681513SChris Mason 						btrfs_set_path_blocking(p);
2569bd681513SChris Mason 						btrfs_tree_read_lock(b);
2570bd681513SChris Mason 						btrfs_clear_path_blocking(p, b,
2571bd681513SChris Mason 								  BTRFS_READ_LOCK);
2572bd681513SChris Mason 					}
2573bd681513SChris Mason 					p->locks[level] = BTRFS_READ_LOCK;
2574bd681513SChris Mason 				}
2575bd681513SChris Mason 				p->nodes[level] = b;
2576b4ce94deSChris Mason 			}
2577be0e5c09SChris Mason 		} else {
2578be0e5c09SChris Mason 			p->slots[level] = slot;
257987b29b20SYan Zheng 			if (ins_len > 0 &&
258087b29b20SYan Zheng 			    btrfs_leaf_free_space(root, b) < ins_len) {
2581bd681513SChris Mason 				if (write_lock_level < 1) {
2582bd681513SChris Mason 					write_lock_level = 1;
2583bd681513SChris Mason 					btrfs_release_path(p);
2584bd681513SChris Mason 					goto again;
2585bd681513SChris Mason 				}
2586bd681513SChris Mason 
2587b4ce94deSChris Mason 				btrfs_set_path_blocking(p);
258833c66f43SYan Zheng 				err = split_leaf(trans, root, key,
2589cc0c5538SChris Mason 						 p, ins_len, ret == 0);
2590bd681513SChris Mason 				btrfs_clear_path_blocking(p, NULL, 0);
2591b4ce94deSChris Mason 
259233c66f43SYan Zheng 				BUG_ON(err > 0);
259333c66f43SYan Zheng 				if (err) {
259433c66f43SYan Zheng 					ret = err;
259565b51a00SChris Mason 					goto done;
259665b51a00SChris Mason 				}
25975c680ed6SChris Mason 			}
2598459931ecSChris Mason 			if (!p->search_for_split)
2599f7c79f30SChris Mason 				unlock_up(p, level, lowest_unlock,
2600f7c79f30SChris Mason 					  min_write_lock_level, &write_lock_level);
260165b51a00SChris Mason 			goto done;
260265b51a00SChris Mason 		}
260365b51a00SChris Mason 	}
260465b51a00SChris Mason 	ret = 1;
260565b51a00SChris Mason done:
2606b4ce94deSChris Mason 	/*
2607b4ce94deSChris Mason 	 * we don't really know what they plan on doing with the path
2608b4ce94deSChris Mason 	 * from here on, so for now just mark it as blocking
2609b4ce94deSChris Mason 	 */
2610b9473439SChris Mason 	if (!p->leave_spinning)
2611b4ce94deSChris Mason 		btrfs_set_path_blocking(p);
261276a05b35SChris Mason 	if (ret < 0)
2613b3b4aa74SDavid Sterba 		btrfs_release_path(p);
2614be0e5c09SChris Mason 	return ret;
2615be0e5c09SChris Mason }
2616be0e5c09SChris Mason 
261774123bd7SChris Mason /*
26185d9e75c4SJan Schmidt  * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
26195d9e75c4SJan Schmidt  * current state of the tree together with the operations recorded in the tree
26205d9e75c4SJan Schmidt  * modification log to search for the key in a previous version of this tree, as
26215d9e75c4SJan Schmidt  * denoted by the time_seq parameter.
26225d9e75c4SJan Schmidt  *
26235d9e75c4SJan Schmidt  * Naturally, there is no support for insert, delete or cow operations.
26245d9e75c4SJan Schmidt  *
26255d9e75c4SJan Schmidt  * The resulting path and return value will be set up as if we called
26265d9e75c4SJan Schmidt  * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
26275d9e75c4SJan Schmidt  */
26285d9e75c4SJan Schmidt int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
26295d9e75c4SJan Schmidt 			  struct btrfs_path *p, u64 time_seq)
26305d9e75c4SJan Schmidt {
26315d9e75c4SJan Schmidt 	struct extent_buffer *b;
26325d9e75c4SJan Schmidt 	int slot;
26335d9e75c4SJan Schmidt 	int ret;
26345d9e75c4SJan Schmidt 	int err;
26355d9e75c4SJan Schmidt 	int level;
26365d9e75c4SJan Schmidt 	int lowest_unlock = 1;
26375d9e75c4SJan Schmidt 	u8 lowest_level = 0;
26385d9e75c4SJan Schmidt 
26395d9e75c4SJan Schmidt 	lowest_level = p->lowest_level;
26405d9e75c4SJan Schmidt 	WARN_ON(p->nodes[0] != NULL);
26415d9e75c4SJan Schmidt 
26425d9e75c4SJan Schmidt 	if (p->search_commit_root) {
26435d9e75c4SJan Schmidt 		BUG_ON(time_seq);
26445d9e75c4SJan Schmidt 		return btrfs_search_slot(NULL, root, key, p, 0, 0);
26455d9e75c4SJan Schmidt 	}
26465d9e75c4SJan Schmidt 
26475d9e75c4SJan Schmidt again:
26485d9e75c4SJan Schmidt 	b = get_old_root(root, time_seq);
26495d9e75c4SJan Schmidt 	level = btrfs_header_level(b);
26505d9e75c4SJan Schmidt 	p->locks[level] = BTRFS_READ_LOCK;
26515d9e75c4SJan Schmidt 
26525d9e75c4SJan Schmidt 	while (b) {
26535d9e75c4SJan Schmidt 		level = btrfs_header_level(b);
26545d9e75c4SJan Schmidt 		p->nodes[level] = b;
26555d9e75c4SJan Schmidt 		btrfs_clear_path_blocking(p, NULL, 0);
26565d9e75c4SJan Schmidt 
26575d9e75c4SJan Schmidt 		/*
26585d9e75c4SJan Schmidt 		 * we have a lock on b and as long as we aren't changing
26595d9e75c4SJan Schmidt 		 * the tree, there is no way to for the items in b to change.
26605d9e75c4SJan Schmidt 		 * It is safe to drop the lock on our parent before we
26615d9e75c4SJan Schmidt 		 * go through the expensive btree search on b.
26625d9e75c4SJan Schmidt 		 */
26635d9e75c4SJan Schmidt 		btrfs_unlock_up_safe(p, level + 1);
26645d9e75c4SJan Schmidt 
26655d9e75c4SJan Schmidt 		ret = bin_search(b, key, level, &slot);
26665d9e75c4SJan Schmidt 
26675d9e75c4SJan Schmidt 		if (level != 0) {
26685d9e75c4SJan Schmidt 			int dec = 0;
26695d9e75c4SJan Schmidt 			if (ret && slot > 0) {
26705d9e75c4SJan Schmidt 				dec = 1;
26715d9e75c4SJan Schmidt 				slot -= 1;
26725d9e75c4SJan Schmidt 			}
26735d9e75c4SJan Schmidt 			p->slots[level] = slot;
26745d9e75c4SJan Schmidt 			unlock_up(p, level, lowest_unlock, 0, NULL);
26755d9e75c4SJan Schmidt 
26765d9e75c4SJan Schmidt 			if (level == lowest_level) {
26775d9e75c4SJan Schmidt 				if (dec)
26785d9e75c4SJan Schmidt 					p->slots[level]++;
26795d9e75c4SJan Schmidt 				goto done;
26805d9e75c4SJan Schmidt 			}
26815d9e75c4SJan Schmidt 
26825d9e75c4SJan Schmidt 			err = read_block_for_search(NULL, root, p, &b, level,
26835d9e75c4SJan Schmidt 						    slot, key, time_seq);
26845d9e75c4SJan Schmidt 			if (err == -EAGAIN)
26855d9e75c4SJan Schmidt 				goto again;
26865d9e75c4SJan Schmidt 			if (err) {
26875d9e75c4SJan Schmidt 				ret = err;
26885d9e75c4SJan Schmidt 				goto done;
26895d9e75c4SJan Schmidt 			}
26905d9e75c4SJan Schmidt 
26915d9e75c4SJan Schmidt 			level = btrfs_header_level(b);
26925d9e75c4SJan Schmidt 			err = btrfs_try_tree_read_lock(b);
26935d9e75c4SJan Schmidt 			if (!err) {
26945d9e75c4SJan Schmidt 				btrfs_set_path_blocking(p);
26955d9e75c4SJan Schmidt 				btrfs_tree_read_lock(b);
26965d9e75c4SJan Schmidt 				btrfs_clear_path_blocking(p, b,
26975d9e75c4SJan Schmidt 							  BTRFS_READ_LOCK);
26985d9e75c4SJan Schmidt 			}
26995d9e75c4SJan Schmidt 			p->locks[level] = BTRFS_READ_LOCK;
27005d9e75c4SJan Schmidt 			p->nodes[level] = b;
27015d9e75c4SJan Schmidt 			b = tree_mod_log_rewind(root->fs_info, b, time_seq);
27025d9e75c4SJan Schmidt 			if (b != p->nodes[level]) {
27035d9e75c4SJan Schmidt 				btrfs_tree_unlock_rw(p->nodes[level],
27045d9e75c4SJan Schmidt 						     p->locks[level]);
27055d9e75c4SJan Schmidt 				p->locks[level] = 0;
27065d9e75c4SJan Schmidt 				p->nodes[level] = b;
27075d9e75c4SJan Schmidt 			}
27085d9e75c4SJan Schmidt 		} else {
27095d9e75c4SJan Schmidt 			p->slots[level] = slot;
27105d9e75c4SJan Schmidt 			unlock_up(p, level, lowest_unlock, 0, NULL);
27115d9e75c4SJan Schmidt 			goto done;
27125d9e75c4SJan Schmidt 		}
27135d9e75c4SJan Schmidt 	}
27145d9e75c4SJan Schmidt 	ret = 1;
27155d9e75c4SJan Schmidt done:
27165d9e75c4SJan Schmidt 	if (!p->leave_spinning)
27175d9e75c4SJan Schmidt 		btrfs_set_path_blocking(p);
27185d9e75c4SJan Schmidt 	if (ret < 0)
27195d9e75c4SJan Schmidt 		btrfs_release_path(p);
27205d9e75c4SJan Schmidt 
27215d9e75c4SJan Schmidt 	return ret;
27225d9e75c4SJan Schmidt }
27235d9e75c4SJan Schmidt 
27245d9e75c4SJan Schmidt /*
2725e6793769SArne Jansen  * helper to use instead of search slot if no exact match is needed but
2726e6793769SArne Jansen  * instead the next or previous item should be returned.
2727e6793769SArne Jansen  * When find_higher is true, the next higher item is returned, the next lower
2728e6793769SArne Jansen  * otherwise.
2729e6793769SArne Jansen  * When return_any and find_higher are both true, and no higher item is found,
2730e6793769SArne Jansen  * return the next lower instead.
2731e6793769SArne Jansen  * When return_any is true and find_higher is false, and no lower item is found,
2732e6793769SArne Jansen  * return the next higher instead.
2733e6793769SArne Jansen  * It returns 0 if any item is found, 1 if none is found (tree empty), and
2734e6793769SArne Jansen  * < 0 on error
2735e6793769SArne Jansen  */
2736e6793769SArne Jansen int btrfs_search_slot_for_read(struct btrfs_root *root,
2737e6793769SArne Jansen 			       struct btrfs_key *key, struct btrfs_path *p,
2738e6793769SArne Jansen 			       int find_higher, int return_any)
2739e6793769SArne Jansen {
2740e6793769SArne Jansen 	int ret;
2741e6793769SArne Jansen 	struct extent_buffer *leaf;
2742e6793769SArne Jansen 
2743e6793769SArne Jansen again:
2744e6793769SArne Jansen 	ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
2745e6793769SArne Jansen 	if (ret <= 0)
2746e6793769SArne Jansen 		return ret;
2747e6793769SArne Jansen 	/*
2748e6793769SArne Jansen 	 * a return value of 1 means the path is at the position where the
2749e6793769SArne Jansen 	 * item should be inserted. Normally this is the next bigger item,
2750e6793769SArne Jansen 	 * but in case the previous item is the last in a leaf, path points
2751e6793769SArne Jansen 	 * to the first free slot in the previous leaf, i.e. at an invalid
2752e6793769SArne Jansen 	 * item.
2753e6793769SArne Jansen 	 */
2754e6793769SArne Jansen 	leaf = p->nodes[0];
2755e6793769SArne Jansen 
2756e6793769SArne Jansen 	if (find_higher) {
2757e6793769SArne Jansen 		if (p->slots[0] >= btrfs_header_nritems(leaf)) {
2758e6793769SArne Jansen 			ret = btrfs_next_leaf(root, p);
2759e6793769SArne Jansen 			if (ret <= 0)
2760e6793769SArne Jansen 				return ret;
2761e6793769SArne Jansen 			if (!return_any)
2762e6793769SArne Jansen 				return 1;
2763e6793769SArne Jansen 			/*
2764e6793769SArne Jansen 			 * no higher item found, return the next
2765e6793769SArne Jansen 			 * lower instead
2766e6793769SArne Jansen 			 */
2767e6793769SArne Jansen 			return_any = 0;
2768e6793769SArne Jansen 			find_higher = 0;
2769e6793769SArne Jansen 			btrfs_release_path(p);
2770e6793769SArne Jansen 			goto again;
2771e6793769SArne Jansen 		}
2772e6793769SArne Jansen 	} else {
2773e6793769SArne Jansen 		if (p->slots[0] == 0) {
2774e6793769SArne Jansen 			ret = btrfs_prev_leaf(root, p);
2775e6793769SArne Jansen 			if (ret < 0)
2776e6793769SArne Jansen 				return ret;
2777e6793769SArne Jansen 			if (!ret) {
2778e6793769SArne Jansen 				p->slots[0] = btrfs_header_nritems(leaf) - 1;
2779e6793769SArne Jansen 				return 0;
2780e6793769SArne Jansen 			}
2781e6793769SArne Jansen 			if (!return_any)
2782e6793769SArne Jansen 				return 1;
2783e6793769SArne Jansen 			/*
2784e6793769SArne Jansen 			 * no lower item found, return the next
2785e6793769SArne Jansen 			 * higher instead
2786e6793769SArne Jansen 			 */
2787e6793769SArne Jansen 			return_any = 0;
2788e6793769SArne Jansen 			find_higher = 1;
2789e6793769SArne Jansen 			btrfs_release_path(p);
2790e6793769SArne Jansen 			goto again;
2791e6793769SArne Jansen 		} else {
2792e6793769SArne Jansen 			--p->slots[0];
2793e6793769SArne Jansen 		}
2794e6793769SArne Jansen 	}
2795e6793769SArne Jansen 	return 0;
2796e6793769SArne Jansen }
2797e6793769SArne Jansen 
2798e6793769SArne Jansen /*
279974123bd7SChris Mason  * adjust the pointers going up the tree, starting at level
280074123bd7SChris Mason  * making sure the right key of each node is points to 'key'.
280174123bd7SChris Mason  * This is used after shifting pointers to the left, so it stops
280274123bd7SChris Mason  * fixing up pointers when a given leaf/node is not in slot 0 of the
280374123bd7SChris Mason  * higher levels
2804aa5d6bedSChris Mason  *
280574123bd7SChris Mason  */
2806143bede5SJeff Mahoney static void fixup_low_keys(struct btrfs_trans_handle *trans,
28075f39d397SChris Mason 			   struct btrfs_root *root, struct btrfs_path *path,
28085f39d397SChris Mason 			   struct btrfs_disk_key *key, int level)
2809be0e5c09SChris Mason {
2810be0e5c09SChris Mason 	int i;
28115f39d397SChris Mason 	struct extent_buffer *t;
28125f39d397SChris Mason 
2813234b63a0SChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2814be0e5c09SChris Mason 		int tslot = path->slots[i];
2815eb60ceacSChris Mason 		if (!path->nodes[i])
2816be0e5c09SChris Mason 			break;
28175f39d397SChris Mason 		t = path->nodes[i];
2818f230475eSJan Schmidt 		tree_mod_log_set_node_key(root->fs_info, t, key, tslot, 1);
28195f39d397SChris Mason 		btrfs_set_node_key(t, key, tslot);
2820d6025579SChris Mason 		btrfs_mark_buffer_dirty(path->nodes[i]);
2821be0e5c09SChris Mason 		if (tslot != 0)
2822be0e5c09SChris Mason 			break;
2823be0e5c09SChris Mason 	}
2824be0e5c09SChris Mason }
2825be0e5c09SChris Mason 
282674123bd7SChris Mason /*
282731840ae1SZheng Yan  * update item key.
282831840ae1SZheng Yan  *
282931840ae1SZheng Yan  * This function isn't completely safe. It's the caller's responsibility
283031840ae1SZheng Yan  * that the new key won't break the order
283131840ae1SZheng Yan  */
2832143bede5SJeff Mahoney void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
283331840ae1SZheng Yan 			     struct btrfs_root *root, struct btrfs_path *path,
283431840ae1SZheng Yan 			     struct btrfs_key *new_key)
283531840ae1SZheng Yan {
283631840ae1SZheng Yan 	struct btrfs_disk_key disk_key;
283731840ae1SZheng Yan 	struct extent_buffer *eb;
283831840ae1SZheng Yan 	int slot;
283931840ae1SZheng Yan 
284031840ae1SZheng Yan 	eb = path->nodes[0];
284131840ae1SZheng Yan 	slot = path->slots[0];
284231840ae1SZheng Yan 	if (slot > 0) {
284331840ae1SZheng Yan 		btrfs_item_key(eb, &disk_key, slot - 1);
2844143bede5SJeff Mahoney 		BUG_ON(comp_keys(&disk_key, new_key) >= 0);
284531840ae1SZheng Yan 	}
284631840ae1SZheng Yan 	if (slot < btrfs_header_nritems(eb) - 1) {
284731840ae1SZheng Yan 		btrfs_item_key(eb, &disk_key, slot + 1);
2848143bede5SJeff Mahoney 		BUG_ON(comp_keys(&disk_key, new_key) <= 0);
284931840ae1SZheng Yan 	}
285031840ae1SZheng Yan 
285131840ae1SZheng Yan 	btrfs_cpu_key_to_disk(&disk_key, new_key);
285231840ae1SZheng Yan 	btrfs_set_item_key(eb, &disk_key, slot);
285331840ae1SZheng Yan 	btrfs_mark_buffer_dirty(eb);
285431840ae1SZheng Yan 	if (slot == 0)
285531840ae1SZheng Yan 		fixup_low_keys(trans, root, path, &disk_key, 1);
285631840ae1SZheng Yan }
285731840ae1SZheng Yan 
285831840ae1SZheng Yan /*
285974123bd7SChris Mason  * try to push data from one node into the next node left in the
286079f95c82SChris Mason  * tree.
2861aa5d6bedSChris Mason  *
2862aa5d6bedSChris Mason  * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2863aa5d6bedSChris Mason  * error, and > 0 if there was no room in the left hand block.
286474123bd7SChris Mason  */
286598ed5174SChris Mason static int push_node_left(struct btrfs_trans_handle *trans,
286698ed5174SChris Mason 			  struct btrfs_root *root, struct extent_buffer *dst,
2867971a1f66SChris Mason 			  struct extent_buffer *src, int empty)
2868be0e5c09SChris Mason {
2869be0e5c09SChris Mason 	int push_items = 0;
2870bb803951SChris Mason 	int src_nritems;
2871bb803951SChris Mason 	int dst_nritems;
2872aa5d6bedSChris Mason 	int ret = 0;
2873be0e5c09SChris Mason 
28745f39d397SChris Mason 	src_nritems = btrfs_header_nritems(src);
28755f39d397SChris Mason 	dst_nritems = btrfs_header_nritems(dst);
2876123abc88SChris Mason 	push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
28777bb86316SChris Mason 	WARN_ON(btrfs_header_generation(src) != trans->transid);
28787bb86316SChris Mason 	WARN_ON(btrfs_header_generation(dst) != trans->transid);
287954aa1f4dSChris Mason 
2880bce4eae9SChris Mason 	if (!empty && src_nritems <= 8)
2881971a1f66SChris Mason 		return 1;
2882971a1f66SChris Mason 
2883d397712bSChris Mason 	if (push_items <= 0)
2884be0e5c09SChris Mason 		return 1;
2885be0e5c09SChris Mason 
2886bce4eae9SChris Mason 	if (empty) {
2887971a1f66SChris Mason 		push_items = min(src_nritems, push_items);
2888bce4eae9SChris Mason 		if (push_items < src_nritems) {
2889bce4eae9SChris Mason 			/* leave at least 8 pointers in the node if
2890bce4eae9SChris Mason 			 * we aren't going to empty it
2891bce4eae9SChris Mason 			 */
2892bce4eae9SChris Mason 			if (src_nritems - push_items < 8) {
2893bce4eae9SChris Mason 				if (push_items <= 8)
2894bce4eae9SChris Mason 					return 1;
2895bce4eae9SChris Mason 				push_items -= 8;
2896bce4eae9SChris Mason 			}
2897bce4eae9SChris Mason 		}
2898bce4eae9SChris Mason 	} else
2899bce4eae9SChris Mason 		push_items = min(src_nritems - 8, push_items);
290079f95c82SChris Mason 
2901f230475eSJan Schmidt 	tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
2902f230475eSJan Schmidt 			     push_items);
29035f39d397SChris Mason 	copy_extent_buffer(dst, src,
29045f39d397SChris Mason 			   btrfs_node_key_ptr_offset(dst_nritems),
29055f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
2906123abc88SChris Mason 			   push_items * sizeof(struct btrfs_key_ptr));
29075f39d397SChris Mason 
2908bb803951SChris Mason 	if (push_items < src_nritems) {
2909f230475eSJan Schmidt 		tree_mod_log_eb_move(root->fs_info, src, 0, push_items,
2910f230475eSJan Schmidt 				     src_nritems - push_items);
29115f39d397SChris Mason 		memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
29125f39d397SChris Mason 				      btrfs_node_key_ptr_offset(push_items),
2913e2fa7227SChris Mason 				      (src_nritems - push_items) *
2914123abc88SChris Mason 				      sizeof(struct btrfs_key_ptr));
2915bb803951SChris Mason 	}
29165f39d397SChris Mason 	btrfs_set_header_nritems(src, src_nritems - push_items);
29175f39d397SChris Mason 	btrfs_set_header_nritems(dst, dst_nritems + push_items);
29185f39d397SChris Mason 	btrfs_mark_buffer_dirty(src);
29195f39d397SChris Mason 	btrfs_mark_buffer_dirty(dst);
292031840ae1SZheng Yan 
2921bb803951SChris Mason 	return ret;
2922be0e5c09SChris Mason }
2923be0e5c09SChris Mason 
292497571fd0SChris Mason /*
292579f95c82SChris Mason  * try to push data from one node into the next node right in the
292679f95c82SChris Mason  * tree.
292779f95c82SChris Mason  *
292879f95c82SChris Mason  * returns 0 if some ptrs were pushed, < 0 if there was some horrible
292979f95c82SChris Mason  * error, and > 0 if there was no room in the right hand block.
293079f95c82SChris Mason  *
293179f95c82SChris Mason  * this will  only push up to 1/2 the contents of the left node over
293279f95c82SChris Mason  */
29335f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans,
29345f39d397SChris Mason 			      struct btrfs_root *root,
29355f39d397SChris Mason 			      struct extent_buffer *dst,
29365f39d397SChris Mason 			      struct extent_buffer *src)
293779f95c82SChris Mason {
293879f95c82SChris Mason 	int push_items = 0;
293979f95c82SChris Mason 	int max_push;
294079f95c82SChris Mason 	int src_nritems;
294179f95c82SChris Mason 	int dst_nritems;
294279f95c82SChris Mason 	int ret = 0;
294379f95c82SChris Mason 
29447bb86316SChris Mason 	WARN_ON(btrfs_header_generation(src) != trans->transid);
29457bb86316SChris Mason 	WARN_ON(btrfs_header_generation(dst) != trans->transid);
29467bb86316SChris Mason 
29475f39d397SChris Mason 	src_nritems = btrfs_header_nritems(src);
29485f39d397SChris Mason 	dst_nritems = btrfs_header_nritems(dst);
2949123abc88SChris Mason 	push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
2950d397712bSChris Mason 	if (push_items <= 0)
295179f95c82SChris Mason 		return 1;
2952bce4eae9SChris Mason 
2953d397712bSChris Mason 	if (src_nritems < 4)
2954bce4eae9SChris Mason 		return 1;
295579f95c82SChris Mason 
295679f95c82SChris Mason 	max_push = src_nritems / 2 + 1;
295779f95c82SChris Mason 	/* don't try to empty the node */
2958d397712bSChris Mason 	if (max_push >= src_nritems)
295979f95c82SChris Mason 		return 1;
2960252c38f0SYan 
296179f95c82SChris Mason 	if (max_push < push_items)
296279f95c82SChris Mason 		push_items = max_push;
296379f95c82SChris Mason 
2964f230475eSJan Schmidt 	tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
29655f39d397SChris Mason 	memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
29665f39d397SChris Mason 				      btrfs_node_key_ptr_offset(0),
29675f39d397SChris Mason 				      (dst_nritems) *
29685f39d397SChris Mason 				      sizeof(struct btrfs_key_ptr));
2969d6025579SChris Mason 
2970f230475eSJan Schmidt 	tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
2971f230475eSJan Schmidt 			     src_nritems - push_items, push_items);
29725f39d397SChris Mason 	copy_extent_buffer(dst, src,
29735f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
29745f39d397SChris Mason 			   btrfs_node_key_ptr_offset(src_nritems - push_items),
2975123abc88SChris Mason 			   push_items * sizeof(struct btrfs_key_ptr));
297679f95c82SChris Mason 
29775f39d397SChris Mason 	btrfs_set_header_nritems(src, src_nritems - push_items);
29785f39d397SChris Mason 	btrfs_set_header_nritems(dst, dst_nritems + push_items);
297979f95c82SChris Mason 
29805f39d397SChris Mason 	btrfs_mark_buffer_dirty(src);
29815f39d397SChris Mason 	btrfs_mark_buffer_dirty(dst);
298231840ae1SZheng Yan 
298379f95c82SChris Mason 	return ret;
298479f95c82SChris Mason }
298579f95c82SChris Mason 
298679f95c82SChris Mason /*
298797571fd0SChris Mason  * helper function to insert a new root level in the tree.
298897571fd0SChris Mason  * A new node is allocated, and a single item is inserted to
298997571fd0SChris Mason  * point to the existing root
2990aa5d6bedSChris Mason  *
2991aa5d6bedSChris Mason  * returns zero on success or < 0 on failure.
299297571fd0SChris Mason  */
2993d397712bSChris Mason static noinline int insert_new_root(struct btrfs_trans_handle *trans,
29945f39d397SChris Mason 			   struct btrfs_root *root,
29955f39d397SChris Mason 			   struct btrfs_path *path, int level)
299674123bd7SChris Mason {
29977bb86316SChris Mason 	u64 lower_gen;
29985f39d397SChris Mason 	struct extent_buffer *lower;
29995f39d397SChris Mason 	struct extent_buffer *c;
3000925baeddSChris Mason 	struct extent_buffer *old;
30015f39d397SChris Mason 	struct btrfs_disk_key lower_key;
30025c680ed6SChris Mason 
30035c680ed6SChris Mason 	BUG_ON(path->nodes[level]);
30045c680ed6SChris Mason 	BUG_ON(path->nodes[level-1] != root->node);
30055c680ed6SChris Mason 
30067bb86316SChris Mason 	lower = path->nodes[level-1];
30077bb86316SChris Mason 	if (level == 1)
30087bb86316SChris Mason 		btrfs_item_key(lower, &lower_key, 0);
30097bb86316SChris Mason 	else
30107bb86316SChris Mason 		btrfs_node_key(lower, &lower_key, 0);
30117bb86316SChris Mason 
301231840ae1SZheng Yan 	c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
30135d4f98a2SYan Zheng 				   root->root_key.objectid, &lower_key,
30145581a51aSJan Schmidt 				   level, root->node->start, 0);
30155f39d397SChris Mason 	if (IS_ERR(c))
30165f39d397SChris Mason 		return PTR_ERR(c);
3017925baeddSChris Mason 
3018f0486c68SYan, Zheng 	root_add_used(root, root->nodesize);
3019f0486c68SYan, Zheng 
30205d4f98a2SYan Zheng 	memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
30215f39d397SChris Mason 	btrfs_set_header_nritems(c, 1);
30225f39d397SChris Mason 	btrfs_set_header_level(c, level);
3023db94535dSChris Mason 	btrfs_set_header_bytenr(c, c->start);
30245f39d397SChris Mason 	btrfs_set_header_generation(c, trans->transid);
30255d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
30265f39d397SChris Mason 	btrfs_set_header_owner(c, root->root_key.objectid);
3027d5719762SChris Mason 
30285f39d397SChris Mason 	write_extent_buffer(c, root->fs_info->fsid,
30295f39d397SChris Mason 			    (unsigned long)btrfs_header_fsid(c),
30305f39d397SChris Mason 			    BTRFS_FSID_SIZE);
3031e17cade2SChris Mason 
3032e17cade2SChris Mason 	write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
3033e17cade2SChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(c),
3034e17cade2SChris Mason 			    BTRFS_UUID_SIZE);
3035e17cade2SChris Mason 
30365f39d397SChris Mason 	btrfs_set_node_key(c, &lower_key, 0);
3037db94535dSChris Mason 	btrfs_set_node_blockptr(c, 0, lower->start);
30387bb86316SChris Mason 	lower_gen = btrfs_header_generation(lower);
303931840ae1SZheng Yan 	WARN_ON(lower_gen != trans->transid);
30407bb86316SChris Mason 
30417bb86316SChris Mason 	btrfs_set_node_ptr_generation(c, 0, lower_gen);
30425f39d397SChris Mason 
30435f39d397SChris Mason 	btrfs_mark_buffer_dirty(c);
3044d5719762SChris Mason 
3045925baeddSChris Mason 	old = root->node;
3046f230475eSJan Schmidt 	tree_mod_log_set_root_pointer(root, c);
3047240f62c8SChris Mason 	rcu_assign_pointer(root->node, c);
3048925baeddSChris Mason 
3049925baeddSChris Mason 	/* the super has an extra ref to root->node */
3050925baeddSChris Mason 	free_extent_buffer(old);
3051925baeddSChris Mason 
30520b86a832SChris Mason 	add_root_to_dirty_list(root);
30535f39d397SChris Mason 	extent_buffer_get(c);
30545f39d397SChris Mason 	path->nodes[level] = c;
3055bd681513SChris Mason 	path->locks[level] = BTRFS_WRITE_LOCK;
305674123bd7SChris Mason 	path->slots[level] = 0;
305774123bd7SChris Mason 	return 0;
305874123bd7SChris Mason }
30595c680ed6SChris Mason 
30605c680ed6SChris Mason /*
30615c680ed6SChris Mason  * worker function to insert a single pointer in a node.
30625c680ed6SChris Mason  * the node should have enough room for the pointer already
306397571fd0SChris Mason  *
30645c680ed6SChris Mason  * slot and level indicate where you want the key to go, and
30655c680ed6SChris Mason  * blocknr is the block the key points to.
30665c680ed6SChris Mason  */
3067143bede5SJeff Mahoney static void insert_ptr(struct btrfs_trans_handle *trans,
3068143bede5SJeff Mahoney 		       struct btrfs_root *root, struct btrfs_path *path,
3069143bede5SJeff Mahoney 		       struct btrfs_disk_key *key, u64 bytenr,
3070c3e06965SJan Schmidt 		       int slot, int level)
30715c680ed6SChris Mason {
30725f39d397SChris Mason 	struct extent_buffer *lower;
30735c680ed6SChris Mason 	int nritems;
3074f3ea38daSJan Schmidt 	int ret;
30755c680ed6SChris Mason 
30765c680ed6SChris Mason 	BUG_ON(!path->nodes[level]);
3077f0486c68SYan, Zheng 	btrfs_assert_tree_locked(path->nodes[level]);
30785f39d397SChris Mason 	lower = path->nodes[level];
30795f39d397SChris Mason 	nritems = btrfs_header_nritems(lower);
3080c293498bSStoyan Gaydarov 	BUG_ON(slot > nritems);
3081143bede5SJeff Mahoney 	BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
308274123bd7SChris Mason 	if (slot != nritems) {
3083c3e06965SJan Schmidt 		if (level)
3084f3ea38daSJan Schmidt 			tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3085f3ea38daSJan Schmidt 					     slot, nritems - slot);
30865f39d397SChris Mason 		memmove_extent_buffer(lower,
30875f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot + 1),
30885f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot),
3089123abc88SChris Mason 			      (nritems - slot) * sizeof(struct btrfs_key_ptr));
309074123bd7SChris Mason 	}
3091c3e06965SJan Schmidt 	if (level) {
3092f3ea38daSJan Schmidt 		ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
3093f3ea38daSJan Schmidt 					      MOD_LOG_KEY_ADD);
3094f3ea38daSJan Schmidt 		BUG_ON(ret < 0);
3095f3ea38daSJan Schmidt 	}
30965f39d397SChris Mason 	btrfs_set_node_key(lower, key, slot);
3097db94535dSChris Mason 	btrfs_set_node_blockptr(lower, slot, bytenr);
309874493f7aSChris Mason 	WARN_ON(trans->transid == 0);
309974493f7aSChris Mason 	btrfs_set_node_ptr_generation(lower, slot, trans->transid);
31005f39d397SChris Mason 	btrfs_set_header_nritems(lower, nritems + 1);
31015f39d397SChris Mason 	btrfs_mark_buffer_dirty(lower);
310274123bd7SChris Mason }
310374123bd7SChris Mason 
310497571fd0SChris Mason /*
310597571fd0SChris Mason  * split the node at the specified level in path in two.
310697571fd0SChris Mason  * The path is corrected to point to the appropriate node after the split
310797571fd0SChris Mason  *
310897571fd0SChris Mason  * Before splitting this tries to make some room in the node by pushing
310997571fd0SChris Mason  * left and right, if either one works, it returns right away.
3110aa5d6bedSChris Mason  *
3111aa5d6bedSChris Mason  * returns 0 on success and < 0 on failure
311297571fd0SChris Mason  */
3113e02119d5SChris Mason static noinline int split_node(struct btrfs_trans_handle *trans,
3114e02119d5SChris Mason 			       struct btrfs_root *root,
3115e02119d5SChris Mason 			       struct btrfs_path *path, int level)
3116be0e5c09SChris Mason {
31175f39d397SChris Mason 	struct extent_buffer *c;
31185f39d397SChris Mason 	struct extent_buffer *split;
31195f39d397SChris Mason 	struct btrfs_disk_key disk_key;
3120be0e5c09SChris Mason 	int mid;
31215c680ed6SChris Mason 	int ret;
31227518a238SChris Mason 	u32 c_nritems;
3123be0e5c09SChris Mason 
31245f39d397SChris Mason 	c = path->nodes[level];
31257bb86316SChris Mason 	WARN_ON(btrfs_header_generation(c) != trans->transid);
31265f39d397SChris Mason 	if (c == root->node) {
31275c680ed6SChris Mason 		/* trying to split the root, lets make a new one */
3128e089f05cSChris Mason 		ret = insert_new_root(trans, root, path, level + 1);
31295c680ed6SChris Mason 		if (ret)
31305c680ed6SChris Mason 			return ret;
3131b3612421SChris Mason 	} else {
3132e66f709bSChris Mason 		ret = push_nodes_for_insert(trans, root, path, level);
31335f39d397SChris Mason 		c = path->nodes[level];
31345f39d397SChris Mason 		if (!ret && btrfs_header_nritems(c) <
3135c448acf0SChris Mason 		    BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
3136e66f709bSChris Mason 			return 0;
313754aa1f4dSChris Mason 		if (ret < 0)
313854aa1f4dSChris Mason 			return ret;
31395c680ed6SChris Mason 	}
3140e66f709bSChris Mason 
31415f39d397SChris Mason 	c_nritems = btrfs_header_nritems(c);
31425d4f98a2SYan Zheng 	mid = (c_nritems + 1) / 2;
31435d4f98a2SYan Zheng 	btrfs_node_key(c, &disk_key, mid);
31447bb86316SChris Mason 
31455d4f98a2SYan Zheng 	split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
31467bb86316SChris Mason 					root->root_key.objectid,
31475581a51aSJan Schmidt 					&disk_key, level, c->start, 0);
31485f39d397SChris Mason 	if (IS_ERR(split))
31495f39d397SChris Mason 		return PTR_ERR(split);
315054aa1f4dSChris Mason 
3151f0486c68SYan, Zheng 	root_add_used(root, root->nodesize);
3152f0486c68SYan, Zheng 
31535d4f98a2SYan Zheng 	memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
31545f39d397SChris Mason 	btrfs_set_header_level(split, btrfs_header_level(c));
3155db94535dSChris Mason 	btrfs_set_header_bytenr(split, split->start);
31565f39d397SChris Mason 	btrfs_set_header_generation(split, trans->transid);
31575d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
31585f39d397SChris Mason 	btrfs_set_header_owner(split, root->root_key.objectid);
31595f39d397SChris Mason 	write_extent_buffer(split, root->fs_info->fsid,
31605f39d397SChris Mason 			    (unsigned long)btrfs_header_fsid(split),
31615f39d397SChris Mason 			    BTRFS_FSID_SIZE);
3162e17cade2SChris Mason 	write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
3163e17cade2SChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(split),
3164e17cade2SChris Mason 			    BTRFS_UUID_SIZE);
31655f39d397SChris Mason 
3166f230475eSJan Schmidt 	tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
31675f39d397SChris Mason 	copy_extent_buffer(split, c,
31685f39d397SChris Mason 			   btrfs_node_key_ptr_offset(0),
31695f39d397SChris Mason 			   btrfs_node_key_ptr_offset(mid),
3170123abc88SChris Mason 			   (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
31715f39d397SChris Mason 	btrfs_set_header_nritems(split, c_nritems - mid);
31725f39d397SChris Mason 	btrfs_set_header_nritems(c, mid);
3173aa5d6bedSChris Mason 	ret = 0;
3174aa5d6bedSChris Mason 
31755f39d397SChris Mason 	btrfs_mark_buffer_dirty(c);
31765f39d397SChris Mason 	btrfs_mark_buffer_dirty(split);
31775f39d397SChris Mason 
3178143bede5SJeff Mahoney 	insert_ptr(trans, root, path, &disk_key, split->start,
3179c3e06965SJan Schmidt 		   path->slots[level + 1] + 1, level + 1);
3180aa5d6bedSChris Mason 
31815de08d7dSChris Mason 	if (path->slots[level] >= mid) {
31825c680ed6SChris Mason 		path->slots[level] -= mid;
3183925baeddSChris Mason 		btrfs_tree_unlock(c);
31845f39d397SChris Mason 		free_extent_buffer(c);
31855f39d397SChris Mason 		path->nodes[level] = split;
31865c680ed6SChris Mason 		path->slots[level + 1] += 1;
3187eb60ceacSChris Mason 	} else {
3188925baeddSChris Mason 		btrfs_tree_unlock(split);
31895f39d397SChris Mason 		free_extent_buffer(split);
3190be0e5c09SChris Mason 	}
3191aa5d6bedSChris Mason 	return ret;
3192be0e5c09SChris Mason }
3193be0e5c09SChris Mason 
319474123bd7SChris Mason /*
319574123bd7SChris Mason  * how many bytes are required to store the items in a leaf.  start
319674123bd7SChris Mason  * and nr indicate which items in the leaf to check.  This totals up the
319774123bd7SChris Mason  * space used both by the item structs and the item data
319874123bd7SChris Mason  */
31995f39d397SChris Mason static int leaf_space_used(struct extent_buffer *l, int start, int nr)
3200be0e5c09SChris Mason {
3201be0e5c09SChris Mason 	int data_len;
32025f39d397SChris Mason 	int nritems = btrfs_header_nritems(l);
3203d4dbff95SChris Mason 	int end = min(nritems, start + nr) - 1;
3204be0e5c09SChris Mason 
3205be0e5c09SChris Mason 	if (!nr)
3206be0e5c09SChris Mason 		return 0;
32075f39d397SChris Mason 	data_len = btrfs_item_end_nr(l, start);
32085f39d397SChris Mason 	data_len = data_len - btrfs_item_offset_nr(l, end);
32090783fcfcSChris Mason 	data_len += sizeof(struct btrfs_item) * nr;
3210d4dbff95SChris Mason 	WARN_ON(data_len < 0);
3211be0e5c09SChris Mason 	return data_len;
3212be0e5c09SChris Mason }
3213be0e5c09SChris Mason 
321474123bd7SChris Mason /*
3215d4dbff95SChris Mason  * The space between the end of the leaf items and
3216d4dbff95SChris Mason  * the start of the leaf data.  IOW, how much room
3217d4dbff95SChris Mason  * the leaf has left for both items and data
3218d4dbff95SChris Mason  */
3219d397712bSChris Mason noinline int btrfs_leaf_free_space(struct btrfs_root *root,
3220e02119d5SChris Mason 				   struct extent_buffer *leaf)
3221d4dbff95SChris Mason {
32225f39d397SChris Mason 	int nritems = btrfs_header_nritems(leaf);
32235f39d397SChris Mason 	int ret;
32245f39d397SChris Mason 	ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
32255f39d397SChris Mason 	if (ret < 0) {
3226d397712bSChris Mason 		printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3227d397712bSChris Mason 		       "used %d nritems %d\n",
3228ae2f5411SJens Axboe 		       ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
32295f39d397SChris Mason 		       leaf_space_used(leaf, 0, nritems), nritems);
32305f39d397SChris Mason 	}
32315f39d397SChris Mason 	return ret;
3232d4dbff95SChris Mason }
3233d4dbff95SChris Mason 
323499d8f83cSChris Mason /*
323599d8f83cSChris Mason  * min slot controls the lowest index we're willing to push to the
323699d8f83cSChris Mason  * right.  We'll push up to and including min_slot, but no lower
323799d8f83cSChris Mason  */
323844871b1bSChris Mason static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
323944871b1bSChris Mason 				      struct btrfs_root *root,
324044871b1bSChris Mason 				      struct btrfs_path *path,
324144871b1bSChris Mason 				      int data_size, int empty,
324244871b1bSChris Mason 				      struct extent_buffer *right,
324399d8f83cSChris Mason 				      int free_space, u32 left_nritems,
324499d8f83cSChris Mason 				      u32 min_slot)
324500ec4c51SChris Mason {
32465f39d397SChris Mason 	struct extent_buffer *left = path->nodes[0];
324744871b1bSChris Mason 	struct extent_buffer *upper = path->nodes[1];
3248cfed81a0SChris Mason 	struct btrfs_map_token token;
32495f39d397SChris Mason 	struct btrfs_disk_key disk_key;
325000ec4c51SChris Mason 	int slot;
325134a38218SChris Mason 	u32 i;
325200ec4c51SChris Mason 	int push_space = 0;
325300ec4c51SChris Mason 	int push_items = 0;
32540783fcfcSChris Mason 	struct btrfs_item *item;
325534a38218SChris Mason 	u32 nr;
32567518a238SChris Mason 	u32 right_nritems;
32575f39d397SChris Mason 	u32 data_end;
3258db94535dSChris Mason 	u32 this_item_size;
325900ec4c51SChris Mason 
3260cfed81a0SChris Mason 	btrfs_init_map_token(&token);
3261cfed81a0SChris Mason 
326234a38218SChris Mason 	if (empty)
326334a38218SChris Mason 		nr = 0;
326434a38218SChris Mason 	else
326599d8f83cSChris Mason 		nr = max_t(u32, 1, min_slot);
326634a38218SChris Mason 
326731840ae1SZheng Yan 	if (path->slots[0] >= left_nritems)
326887b29b20SYan Zheng 		push_space += data_size;
326931840ae1SZheng Yan 
327044871b1bSChris Mason 	slot = path->slots[1];
327134a38218SChris Mason 	i = left_nritems - 1;
327234a38218SChris Mason 	while (i >= nr) {
32735f39d397SChris Mason 		item = btrfs_item_nr(left, i);
3274db94535dSChris Mason 
327531840ae1SZheng Yan 		if (!empty && push_items > 0) {
327631840ae1SZheng Yan 			if (path->slots[0] > i)
327731840ae1SZheng Yan 				break;
327831840ae1SZheng Yan 			if (path->slots[0] == i) {
327931840ae1SZheng Yan 				int space = btrfs_leaf_free_space(root, left);
328031840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
328131840ae1SZheng Yan 					break;
328231840ae1SZheng Yan 			}
328331840ae1SZheng Yan 		}
328431840ae1SZheng Yan 
328500ec4c51SChris Mason 		if (path->slots[0] == i)
328687b29b20SYan Zheng 			push_space += data_size;
3287db94535dSChris Mason 
3288db94535dSChris Mason 		this_item_size = btrfs_item_size(left, item);
3289db94535dSChris Mason 		if (this_item_size + sizeof(*item) + push_space > free_space)
329000ec4c51SChris Mason 			break;
329131840ae1SZheng Yan 
329200ec4c51SChris Mason 		push_items++;
3293db94535dSChris Mason 		push_space += this_item_size + sizeof(*item);
329434a38218SChris Mason 		if (i == 0)
329534a38218SChris Mason 			break;
329634a38218SChris Mason 		i--;
3297db94535dSChris Mason 	}
32985f39d397SChris Mason 
3299925baeddSChris Mason 	if (push_items == 0)
3300925baeddSChris Mason 		goto out_unlock;
33015f39d397SChris Mason 
330234a38218SChris Mason 	if (!empty && push_items == left_nritems)
3303a429e513SChris Mason 		WARN_ON(1);
33045f39d397SChris Mason 
330500ec4c51SChris Mason 	/* push left to right */
33065f39d397SChris Mason 	right_nritems = btrfs_header_nritems(right);
330734a38218SChris Mason 
33085f39d397SChris Mason 	push_space = btrfs_item_end_nr(left, left_nritems - push_items);
3309123abc88SChris Mason 	push_space -= leaf_data_end(root, left);
33105f39d397SChris Mason 
331100ec4c51SChris Mason 	/* make room in the right data area */
33125f39d397SChris Mason 	data_end = leaf_data_end(root, right);
33135f39d397SChris Mason 	memmove_extent_buffer(right,
33145f39d397SChris Mason 			      btrfs_leaf_data(right) + data_end - push_space,
33155f39d397SChris Mason 			      btrfs_leaf_data(right) + data_end,
33165f39d397SChris Mason 			      BTRFS_LEAF_DATA_SIZE(root) - data_end);
33175f39d397SChris Mason 
331800ec4c51SChris Mason 	/* copy from the left data area */
33195f39d397SChris Mason 	copy_extent_buffer(right, left, btrfs_leaf_data(right) +
3320d6025579SChris Mason 		     BTRFS_LEAF_DATA_SIZE(root) - push_space,
3321d6025579SChris Mason 		     btrfs_leaf_data(left) + leaf_data_end(root, left),
3322d6025579SChris Mason 		     push_space);
33235f39d397SChris Mason 
33245f39d397SChris Mason 	memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
33255f39d397SChris Mason 			      btrfs_item_nr_offset(0),
33260783fcfcSChris Mason 			      right_nritems * sizeof(struct btrfs_item));
33275f39d397SChris Mason 
332800ec4c51SChris Mason 	/* copy the items from left to right */
33295f39d397SChris Mason 	copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
33305f39d397SChris Mason 		   btrfs_item_nr_offset(left_nritems - push_items),
33310783fcfcSChris Mason 		   push_items * sizeof(struct btrfs_item));
333200ec4c51SChris Mason 
333300ec4c51SChris Mason 	/* update the item pointers */
33347518a238SChris Mason 	right_nritems += push_items;
33355f39d397SChris Mason 	btrfs_set_header_nritems(right, right_nritems);
3336123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root);
33377518a238SChris Mason 	for (i = 0; i < right_nritems; i++) {
33385f39d397SChris Mason 		item = btrfs_item_nr(right, i);
3339cfed81a0SChris Mason 		push_space -= btrfs_token_item_size(right, item, &token);
3340cfed81a0SChris Mason 		btrfs_set_token_item_offset(right, item, push_space, &token);
3341db94535dSChris Mason 	}
3342db94535dSChris Mason 
33437518a238SChris Mason 	left_nritems -= push_items;
33445f39d397SChris Mason 	btrfs_set_header_nritems(left, left_nritems);
334500ec4c51SChris Mason 
334634a38218SChris Mason 	if (left_nritems)
33475f39d397SChris Mason 		btrfs_mark_buffer_dirty(left);
3348f0486c68SYan, Zheng 	else
3349f0486c68SYan, Zheng 		clean_tree_block(trans, root, left);
3350f0486c68SYan, Zheng 
33515f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
3352a429e513SChris Mason 
33535f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
33545f39d397SChris Mason 	btrfs_set_node_key(upper, &disk_key, slot + 1);
3355d6025579SChris Mason 	btrfs_mark_buffer_dirty(upper);
335602217ed2SChris Mason 
335700ec4c51SChris Mason 	/* then fixup the leaf pointer in the path */
33587518a238SChris Mason 	if (path->slots[0] >= left_nritems) {
33597518a238SChris Mason 		path->slots[0] -= left_nritems;
3360925baeddSChris Mason 		if (btrfs_header_nritems(path->nodes[0]) == 0)
3361925baeddSChris Mason 			clean_tree_block(trans, root, path->nodes[0]);
3362925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
33635f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
33645f39d397SChris Mason 		path->nodes[0] = right;
336500ec4c51SChris Mason 		path->slots[1] += 1;
336600ec4c51SChris Mason 	} else {
3367925baeddSChris Mason 		btrfs_tree_unlock(right);
33685f39d397SChris Mason 		free_extent_buffer(right);
336900ec4c51SChris Mason 	}
337000ec4c51SChris Mason 	return 0;
3371925baeddSChris Mason 
3372925baeddSChris Mason out_unlock:
3373925baeddSChris Mason 	btrfs_tree_unlock(right);
3374925baeddSChris Mason 	free_extent_buffer(right);
3375925baeddSChris Mason 	return 1;
337600ec4c51SChris Mason }
3377925baeddSChris Mason 
337800ec4c51SChris Mason /*
337944871b1bSChris Mason  * push some data in the path leaf to the right, trying to free up at
338074123bd7SChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
338144871b1bSChris Mason  *
338244871b1bSChris Mason  * returns 1 if the push failed because the other node didn't have enough
338344871b1bSChris Mason  * room, 0 if everything worked out and < 0 if there were major errors.
338499d8f83cSChris Mason  *
338599d8f83cSChris Mason  * this will push starting from min_slot to the end of the leaf.  It won't
338699d8f83cSChris Mason  * push any slot lower than min_slot
338774123bd7SChris Mason  */
338844871b1bSChris Mason static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
338999d8f83cSChris Mason 			   *root, struct btrfs_path *path,
339099d8f83cSChris Mason 			   int min_data_size, int data_size,
339199d8f83cSChris Mason 			   int empty, u32 min_slot)
3392be0e5c09SChris Mason {
339344871b1bSChris Mason 	struct extent_buffer *left = path->nodes[0];
339444871b1bSChris Mason 	struct extent_buffer *right;
339544871b1bSChris Mason 	struct extent_buffer *upper;
339644871b1bSChris Mason 	int slot;
339744871b1bSChris Mason 	int free_space;
339844871b1bSChris Mason 	u32 left_nritems;
339944871b1bSChris Mason 	int ret;
340044871b1bSChris Mason 
340144871b1bSChris Mason 	if (!path->nodes[1])
340244871b1bSChris Mason 		return 1;
340344871b1bSChris Mason 
340444871b1bSChris Mason 	slot = path->slots[1];
340544871b1bSChris Mason 	upper = path->nodes[1];
340644871b1bSChris Mason 	if (slot >= btrfs_header_nritems(upper) - 1)
340744871b1bSChris Mason 		return 1;
340844871b1bSChris Mason 
340944871b1bSChris Mason 	btrfs_assert_tree_locked(path->nodes[1]);
341044871b1bSChris Mason 
341144871b1bSChris Mason 	right = read_node_slot(root, upper, slot + 1);
341291ca338dSTsutomu Itoh 	if (right == NULL)
341391ca338dSTsutomu Itoh 		return 1;
341491ca338dSTsutomu Itoh 
341544871b1bSChris Mason 	btrfs_tree_lock(right);
341644871b1bSChris Mason 	btrfs_set_lock_blocking(right);
341744871b1bSChris Mason 
341844871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, right);
341944871b1bSChris Mason 	if (free_space < data_size)
342044871b1bSChris Mason 		goto out_unlock;
342144871b1bSChris Mason 
342244871b1bSChris Mason 	/* cow and double check */
342344871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, right, upper,
342444871b1bSChris Mason 			      slot + 1, &right);
342544871b1bSChris Mason 	if (ret)
342644871b1bSChris Mason 		goto out_unlock;
342744871b1bSChris Mason 
342844871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, right);
342944871b1bSChris Mason 	if (free_space < data_size)
343044871b1bSChris Mason 		goto out_unlock;
343144871b1bSChris Mason 
343244871b1bSChris Mason 	left_nritems = btrfs_header_nritems(left);
343344871b1bSChris Mason 	if (left_nritems == 0)
343444871b1bSChris Mason 		goto out_unlock;
343544871b1bSChris Mason 
343699d8f83cSChris Mason 	return __push_leaf_right(trans, root, path, min_data_size, empty,
343799d8f83cSChris Mason 				right, free_space, left_nritems, min_slot);
343844871b1bSChris Mason out_unlock:
343944871b1bSChris Mason 	btrfs_tree_unlock(right);
344044871b1bSChris Mason 	free_extent_buffer(right);
344144871b1bSChris Mason 	return 1;
344244871b1bSChris Mason }
344344871b1bSChris Mason 
344444871b1bSChris Mason /*
344544871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
344644871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
344799d8f83cSChris Mason  *
344899d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
344999d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us do all the
345099d8f83cSChris Mason  * items
345144871b1bSChris Mason  */
345244871b1bSChris Mason static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
345344871b1bSChris Mason 				     struct btrfs_root *root,
345444871b1bSChris Mason 				     struct btrfs_path *path, int data_size,
345544871b1bSChris Mason 				     int empty, struct extent_buffer *left,
345699d8f83cSChris Mason 				     int free_space, u32 right_nritems,
345799d8f83cSChris Mason 				     u32 max_slot)
345844871b1bSChris Mason {
34595f39d397SChris Mason 	struct btrfs_disk_key disk_key;
34605f39d397SChris Mason 	struct extent_buffer *right = path->nodes[0];
3461be0e5c09SChris Mason 	int i;
3462be0e5c09SChris Mason 	int push_space = 0;
3463be0e5c09SChris Mason 	int push_items = 0;
34640783fcfcSChris Mason 	struct btrfs_item *item;
34657518a238SChris Mason 	u32 old_left_nritems;
346634a38218SChris Mason 	u32 nr;
3467aa5d6bedSChris Mason 	int ret = 0;
3468db94535dSChris Mason 	u32 this_item_size;
3469db94535dSChris Mason 	u32 old_left_item_size;
3470cfed81a0SChris Mason 	struct btrfs_map_token token;
3471cfed81a0SChris Mason 
3472cfed81a0SChris Mason 	btrfs_init_map_token(&token);
3473be0e5c09SChris Mason 
347434a38218SChris Mason 	if (empty)
347599d8f83cSChris Mason 		nr = min(right_nritems, max_slot);
347634a38218SChris Mason 	else
347799d8f83cSChris Mason 		nr = min(right_nritems - 1, max_slot);
347834a38218SChris Mason 
347934a38218SChris Mason 	for (i = 0; i < nr; i++) {
34805f39d397SChris Mason 		item = btrfs_item_nr(right, i);
3481db94535dSChris Mason 
348231840ae1SZheng Yan 		if (!empty && push_items > 0) {
348331840ae1SZheng Yan 			if (path->slots[0] < i)
348431840ae1SZheng Yan 				break;
348531840ae1SZheng Yan 			if (path->slots[0] == i) {
348631840ae1SZheng Yan 				int space = btrfs_leaf_free_space(root, right);
348731840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
348831840ae1SZheng Yan 					break;
348931840ae1SZheng Yan 			}
349031840ae1SZheng Yan 		}
349131840ae1SZheng Yan 
3492be0e5c09SChris Mason 		if (path->slots[0] == i)
349387b29b20SYan Zheng 			push_space += data_size;
3494db94535dSChris Mason 
3495db94535dSChris Mason 		this_item_size = btrfs_item_size(right, item);
3496db94535dSChris Mason 		if (this_item_size + sizeof(*item) + push_space > free_space)
3497be0e5c09SChris Mason 			break;
3498db94535dSChris Mason 
3499be0e5c09SChris Mason 		push_items++;
3500db94535dSChris Mason 		push_space += this_item_size + sizeof(*item);
3501be0e5c09SChris Mason 	}
3502db94535dSChris Mason 
3503be0e5c09SChris Mason 	if (push_items == 0) {
3504925baeddSChris Mason 		ret = 1;
3505925baeddSChris Mason 		goto out;
3506be0e5c09SChris Mason 	}
350734a38218SChris Mason 	if (!empty && push_items == btrfs_header_nritems(right))
3508a429e513SChris Mason 		WARN_ON(1);
35095f39d397SChris Mason 
3510be0e5c09SChris Mason 	/* push data from right to left */
35115f39d397SChris Mason 	copy_extent_buffer(left, right,
35125f39d397SChris Mason 			   btrfs_item_nr_offset(btrfs_header_nritems(left)),
35135f39d397SChris Mason 			   btrfs_item_nr_offset(0),
35145f39d397SChris Mason 			   push_items * sizeof(struct btrfs_item));
35155f39d397SChris Mason 
3516123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root) -
35175f39d397SChris Mason 		     btrfs_item_offset_nr(right, push_items - 1);
35185f39d397SChris Mason 
35195f39d397SChris Mason 	copy_extent_buffer(left, right, btrfs_leaf_data(left) +
3520d6025579SChris Mason 		     leaf_data_end(root, left) - push_space,
3521123abc88SChris Mason 		     btrfs_leaf_data(right) +
35225f39d397SChris Mason 		     btrfs_item_offset_nr(right, push_items - 1),
3523be0e5c09SChris Mason 		     push_space);
35245f39d397SChris Mason 	old_left_nritems = btrfs_header_nritems(left);
352587b29b20SYan Zheng 	BUG_ON(old_left_nritems <= 0);
3526eb60ceacSChris Mason 
3527db94535dSChris Mason 	old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
3528be0e5c09SChris Mason 	for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
35295f39d397SChris Mason 		u32 ioff;
3530db94535dSChris Mason 
35315f39d397SChris Mason 		item = btrfs_item_nr(left, i);
3532db94535dSChris Mason 
3533cfed81a0SChris Mason 		ioff = btrfs_token_item_offset(left, item, &token);
3534cfed81a0SChris Mason 		btrfs_set_token_item_offset(left, item,
3535cfed81a0SChris Mason 		      ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3536cfed81a0SChris Mason 		      &token);
3537be0e5c09SChris Mason 	}
35385f39d397SChris Mason 	btrfs_set_header_nritems(left, old_left_nritems + push_items);
3539be0e5c09SChris Mason 
3540be0e5c09SChris Mason 	/* fixup right node */
354134a38218SChris Mason 	if (push_items > right_nritems) {
3542d397712bSChris Mason 		printk(KERN_CRIT "push items %d nr %u\n", push_items,
3543d397712bSChris Mason 		       right_nritems);
354434a38218SChris Mason 		WARN_ON(1);
354534a38218SChris Mason 	}
354634a38218SChris Mason 
354734a38218SChris Mason 	if (push_items < right_nritems) {
35485f39d397SChris Mason 		push_space = btrfs_item_offset_nr(right, push_items - 1) -
3549123abc88SChris Mason 						  leaf_data_end(root, right);
35505f39d397SChris Mason 		memmove_extent_buffer(right, btrfs_leaf_data(right) +
3551d6025579SChris Mason 				      BTRFS_LEAF_DATA_SIZE(root) - push_space,
3552d6025579SChris Mason 				      btrfs_leaf_data(right) +
3553123abc88SChris Mason 				      leaf_data_end(root, right), push_space);
35545f39d397SChris Mason 
35555f39d397SChris Mason 		memmove_extent_buffer(right, btrfs_item_nr_offset(0),
35565f39d397SChris Mason 			      btrfs_item_nr_offset(push_items),
35575f39d397SChris Mason 			     (btrfs_header_nritems(right) - push_items) *
35580783fcfcSChris Mason 			     sizeof(struct btrfs_item));
355934a38218SChris Mason 	}
3560eef1c494SYan 	right_nritems -= push_items;
3561eef1c494SYan 	btrfs_set_header_nritems(right, right_nritems);
3562123abc88SChris Mason 	push_space = BTRFS_LEAF_DATA_SIZE(root);
35635f39d397SChris Mason 	for (i = 0; i < right_nritems; i++) {
35645f39d397SChris Mason 		item = btrfs_item_nr(right, i);
3565db94535dSChris Mason 
3566cfed81a0SChris Mason 		push_space = push_space - btrfs_token_item_size(right,
3567cfed81a0SChris Mason 								item, &token);
3568cfed81a0SChris Mason 		btrfs_set_token_item_offset(right, item, push_space, &token);
3569db94535dSChris Mason 	}
3570eb60ceacSChris Mason 
35715f39d397SChris Mason 	btrfs_mark_buffer_dirty(left);
357234a38218SChris Mason 	if (right_nritems)
35735f39d397SChris Mason 		btrfs_mark_buffer_dirty(right);
3574f0486c68SYan, Zheng 	else
3575f0486c68SYan, Zheng 		clean_tree_block(trans, root, right);
3576098f59c2SChris Mason 
35775f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
3578143bede5SJeff Mahoney 	fixup_low_keys(trans, root, path, &disk_key, 1);
3579be0e5c09SChris Mason 
3580be0e5c09SChris Mason 	/* then fixup the leaf pointer in the path */
3581be0e5c09SChris Mason 	if (path->slots[0] < push_items) {
3582be0e5c09SChris Mason 		path->slots[0] += old_left_nritems;
3583925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
35845f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
35855f39d397SChris Mason 		path->nodes[0] = left;
3586be0e5c09SChris Mason 		path->slots[1] -= 1;
3587be0e5c09SChris Mason 	} else {
3588925baeddSChris Mason 		btrfs_tree_unlock(left);
35895f39d397SChris Mason 		free_extent_buffer(left);
3590be0e5c09SChris Mason 		path->slots[0] -= push_items;
3591be0e5c09SChris Mason 	}
3592eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
3593aa5d6bedSChris Mason 	return ret;
3594925baeddSChris Mason out:
3595925baeddSChris Mason 	btrfs_tree_unlock(left);
3596925baeddSChris Mason 	free_extent_buffer(left);
3597925baeddSChris Mason 	return ret;
3598be0e5c09SChris Mason }
3599be0e5c09SChris Mason 
360074123bd7SChris Mason /*
360144871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
360244871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
360399d8f83cSChris Mason  *
360499d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
360599d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us push all the
360699d8f83cSChris Mason  * items
360744871b1bSChris Mason  */
360844871b1bSChris Mason static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
360999d8f83cSChris Mason 			  *root, struct btrfs_path *path, int min_data_size,
361099d8f83cSChris Mason 			  int data_size, int empty, u32 max_slot)
361144871b1bSChris Mason {
361244871b1bSChris Mason 	struct extent_buffer *right = path->nodes[0];
361344871b1bSChris Mason 	struct extent_buffer *left;
361444871b1bSChris Mason 	int slot;
361544871b1bSChris Mason 	int free_space;
361644871b1bSChris Mason 	u32 right_nritems;
361744871b1bSChris Mason 	int ret = 0;
361844871b1bSChris Mason 
361944871b1bSChris Mason 	slot = path->slots[1];
362044871b1bSChris Mason 	if (slot == 0)
362144871b1bSChris Mason 		return 1;
362244871b1bSChris Mason 	if (!path->nodes[1])
362344871b1bSChris Mason 		return 1;
362444871b1bSChris Mason 
362544871b1bSChris Mason 	right_nritems = btrfs_header_nritems(right);
362644871b1bSChris Mason 	if (right_nritems == 0)
362744871b1bSChris Mason 		return 1;
362844871b1bSChris Mason 
362944871b1bSChris Mason 	btrfs_assert_tree_locked(path->nodes[1]);
363044871b1bSChris Mason 
363144871b1bSChris Mason 	left = read_node_slot(root, path->nodes[1], slot - 1);
363291ca338dSTsutomu Itoh 	if (left == NULL)
363391ca338dSTsutomu Itoh 		return 1;
363491ca338dSTsutomu Itoh 
363544871b1bSChris Mason 	btrfs_tree_lock(left);
363644871b1bSChris Mason 	btrfs_set_lock_blocking(left);
363744871b1bSChris Mason 
363844871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, left);
363944871b1bSChris Mason 	if (free_space < data_size) {
364044871b1bSChris Mason 		ret = 1;
364144871b1bSChris Mason 		goto out;
364244871b1bSChris Mason 	}
364344871b1bSChris Mason 
364444871b1bSChris Mason 	/* cow and double check */
364544871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, left,
364644871b1bSChris Mason 			      path->nodes[1], slot - 1, &left);
364744871b1bSChris Mason 	if (ret) {
364844871b1bSChris Mason 		/* we hit -ENOSPC, but it isn't fatal here */
364979787eaaSJeff Mahoney 		if (ret == -ENOSPC)
365044871b1bSChris Mason 			ret = 1;
365144871b1bSChris Mason 		goto out;
365244871b1bSChris Mason 	}
365344871b1bSChris Mason 
365444871b1bSChris Mason 	free_space = btrfs_leaf_free_space(root, left);
365544871b1bSChris Mason 	if (free_space < data_size) {
365644871b1bSChris Mason 		ret = 1;
365744871b1bSChris Mason 		goto out;
365844871b1bSChris Mason 	}
365944871b1bSChris Mason 
366099d8f83cSChris Mason 	return __push_leaf_left(trans, root, path, min_data_size,
366199d8f83cSChris Mason 			       empty, left, free_space, right_nritems,
366299d8f83cSChris Mason 			       max_slot);
366344871b1bSChris Mason out:
366444871b1bSChris Mason 	btrfs_tree_unlock(left);
366544871b1bSChris Mason 	free_extent_buffer(left);
366644871b1bSChris Mason 	return ret;
366744871b1bSChris Mason }
366844871b1bSChris Mason 
366944871b1bSChris Mason /*
367074123bd7SChris Mason  * split the path's leaf in two, making sure there is at least data_size
367174123bd7SChris Mason  * available for the resulting leaf level of the path.
367274123bd7SChris Mason  */
3673143bede5SJeff Mahoney static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3674e02119d5SChris Mason 				    struct btrfs_root *root,
367544871b1bSChris Mason 				    struct btrfs_path *path,
367644871b1bSChris Mason 				    struct extent_buffer *l,
367744871b1bSChris Mason 				    struct extent_buffer *right,
367844871b1bSChris Mason 				    int slot, int mid, int nritems)
3679be0e5c09SChris Mason {
3680be0e5c09SChris Mason 	int data_copy_size;
3681be0e5c09SChris Mason 	int rt_data_off;
3682be0e5c09SChris Mason 	int i;
3683d4dbff95SChris Mason 	struct btrfs_disk_key disk_key;
3684cfed81a0SChris Mason 	struct btrfs_map_token token;
3685cfed81a0SChris Mason 
3686cfed81a0SChris Mason 	btrfs_init_map_token(&token);
3687be0e5c09SChris Mason 
36885f39d397SChris Mason 	nritems = nritems - mid;
36895f39d397SChris Mason 	btrfs_set_header_nritems(right, nritems);
36905f39d397SChris Mason 	data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
36915f39d397SChris Mason 
36925f39d397SChris Mason 	copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
36935f39d397SChris Mason 			   btrfs_item_nr_offset(mid),
36945f39d397SChris Mason 			   nritems * sizeof(struct btrfs_item));
36955f39d397SChris Mason 
36965f39d397SChris Mason 	copy_extent_buffer(right, l,
3697d6025579SChris Mason 		     btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3698123abc88SChris Mason 		     data_copy_size, btrfs_leaf_data(l) +
3699123abc88SChris Mason 		     leaf_data_end(root, l), data_copy_size);
370074123bd7SChris Mason 
37015f39d397SChris Mason 	rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
37025f39d397SChris Mason 		      btrfs_item_end_nr(l, mid);
37035f39d397SChris Mason 
37045f39d397SChris Mason 	for (i = 0; i < nritems; i++) {
37055f39d397SChris Mason 		struct btrfs_item *item = btrfs_item_nr(right, i);
3706db94535dSChris Mason 		u32 ioff;
3707db94535dSChris Mason 
3708cfed81a0SChris Mason 		ioff = btrfs_token_item_offset(right, item, &token);
3709cfed81a0SChris Mason 		btrfs_set_token_item_offset(right, item,
3710cfed81a0SChris Mason 					    ioff + rt_data_off, &token);
37110783fcfcSChris Mason 	}
371274123bd7SChris Mason 
37135f39d397SChris Mason 	btrfs_set_header_nritems(l, mid);
37145f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
3715143bede5SJeff Mahoney 	insert_ptr(trans, root, path, &disk_key, right->start,
3716c3e06965SJan Schmidt 		   path->slots[1] + 1, 1);
37175f39d397SChris Mason 
37185f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
37195f39d397SChris Mason 	btrfs_mark_buffer_dirty(l);
3720eb60ceacSChris Mason 	BUG_ON(path->slots[0] != slot);
37215f39d397SChris Mason 
3722be0e5c09SChris Mason 	if (mid <= slot) {
3723925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
37245f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
37255f39d397SChris Mason 		path->nodes[0] = right;
3726be0e5c09SChris Mason 		path->slots[0] -= mid;
3727be0e5c09SChris Mason 		path->slots[1] += 1;
3728925baeddSChris Mason 	} else {
3729925baeddSChris Mason 		btrfs_tree_unlock(right);
37305f39d397SChris Mason 		free_extent_buffer(right);
3731925baeddSChris Mason 	}
37325f39d397SChris Mason 
3733eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
373444871b1bSChris Mason }
373544871b1bSChris Mason 
373644871b1bSChris Mason /*
373799d8f83cSChris Mason  * double splits happen when we need to insert a big item in the middle
373899d8f83cSChris Mason  * of a leaf.  A double split can leave us with 3 mostly empty leaves:
373999d8f83cSChris Mason  * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
374099d8f83cSChris Mason  *          A                 B                 C
374199d8f83cSChris Mason  *
374299d8f83cSChris Mason  * We avoid this by trying to push the items on either side of our target
374399d8f83cSChris Mason  * into the adjacent leaves.  If all goes well we can avoid the double split
374499d8f83cSChris Mason  * completely.
374599d8f83cSChris Mason  */
374699d8f83cSChris Mason static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
374799d8f83cSChris Mason 					  struct btrfs_root *root,
374899d8f83cSChris Mason 					  struct btrfs_path *path,
374999d8f83cSChris Mason 					  int data_size)
375099d8f83cSChris Mason {
375199d8f83cSChris Mason 	int ret;
375299d8f83cSChris Mason 	int progress = 0;
375399d8f83cSChris Mason 	int slot;
375499d8f83cSChris Mason 	u32 nritems;
375599d8f83cSChris Mason 
375699d8f83cSChris Mason 	slot = path->slots[0];
375799d8f83cSChris Mason 
375899d8f83cSChris Mason 	/*
375999d8f83cSChris Mason 	 * try to push all the items after our slot into the
376099d8f83cSChris Mason 	 * right leaf
376199d8f83cSChris Mason 	 */
376299d8f83cSChris Mason 	ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
376399d8f83cSChris Mason 	if (ret < 0)
376499d8f83cSChris Mason 		return ret;
376599d8f83cSChris Mason 
376699d8f83cSChris Mason 	if (ret == 0)
376799d8f83cSChris Mason 		progress++;
376899d8f83cSChris Mason 
376999d8f83cSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
377099d8f83cSChris Mason 	/*
377199d8f83cSChris Mason 	 * our goal is to get our slot at the start or end of a leaf.  If
377299d8f83cSChris Mason 	 * we've done so we're done
377399d8f83cSChris Mason 	 */
377499d8f83cSChris Mason 	if (path->slots[0] == 0 || path->slots[0] == nritems)
377599d8f83cSChris Mason 		return 0;
377699d8f83cSChris Mason 
377799d8f83cSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
377899d8f83cSChris Mason 		return 0;
377999d8f83cSChris Mason 
378099d8f83cSChris Mason 	/* try to push all the items before our slot into the next leaf */
378199d8f83cSChris Mason 	slot = path->slots[0];
378299d8f83cSChris Mason 	ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
378399d8f83cSChris Mason 	if (ret < 0)
378499d8f83cSChris Mason 		return ret;
378599d8f83cSChris Mason 
378699d8f83cSChris Mason 	if (ret == 0)
378799d8f83cSChris Mason 		progress++;
378899d8f83cSChris Mason 
378999d8f83cSChris Mason 	if (progress)
379099d8f83cSChris Mason 		return 0;
379199d8f83cSChris Mason 	return 1;
379299d8f83cSChris Mason }
379399d8f83cSChris Mason 
379499d8f83cSChris Mason /*
379544871b1bSChris Mason  * split the path's leaf in two, making sure there is at least data_size
379644871b1bSChris Mason  * available for the resulting leaf level of the path.
379744871b1bSChris Mason  *
379844871b1bSChris Mason  * returns 0 if all went well and < 0 on failure.
379944871b1bSChris Mason  */
380044871b1bSChris Mason static noinline int split_leaf(struct btrfs_trans_handle *trans,
380144871b1bSChris Mason 			       struct btrfs_root *root,
380244871b1bSChris Mason 			       struct btrfs_key *ins_key,
380344871b1bSChris Mason 			       struct btrfs_path *path, int data_size,
380444871b1bSChris Mason 			       int extend)
380544871b1bSChris Mason {
38065d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
380744871b1bSChris Mason 	struct extent_buffer *l;
380844871b1bSChris Mason 	u32 nritems;
380944871b1bSChris Mason 	int mid;
381044871b1bSChris Mason 	int slot;
381144871b1bSChris Mason 	struct extent_buffer *right;
381244871b1bSChris Mason 	int ret = 0;
381344871b1bSChris Mason 	int wret;
38145d4f98a2SYan Zheng 	int split;
381544871b1bSChris Mason 	int num_doubles = 0;
381699d8f83cSChris Mason 	int tried_avoid_double = 0;
381744871b1bSChris Mason 
3818a5719521SYan, Zheng 	l = path->nodes[0];
3819a5719521SYan, Zheng 	slot = path->slots[0];
3820a5719521SYan, Zheng 	if (extend && data_size + btrfs_item_size_nr(l, slot) +
3821a5719521SYan, Zheng 	    sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3822a5719521SYan, Zheng 		return -EOVERFLOW;
3823a5719521SYan, Zheng 
382444871b1bSChris Mason 	/* first try to make some room by pushing left and right */
382599d8f83cSChris Mason 	if (data_size) {
382699d8f83cSChris Mason 		wret = push_leaf_right(trans, root, path, data_size,
382799d8f83cSChris Mason 				       data_size, 0, 0);
382844871b1bSChris Mason 		if (wret < 0)
382944871b1bSChris Mason 			return wret;
383044871b1bSChris Mason 		if (wret) {
383199d8f83cSChris Mason 			wret = push_leaf_left(trans, root, path, data_size,
383299d8f83cSChris Mason 					      data_size, 0, (u32)-1);
383344871b1bSChris Mason 			if (wret < 0)
383444871b1bSChris Mason 				return wret;
383544871b1bSChris Mason 		}
383644871b1bSChris Mason 		l = path->nodes[0];
383744871b1bSChris Mason 
383844871b1bSChris Mason 		/* did the pushes work? */
383944871b1bSChris Mason 		if (btrfs_leaf_free_space(root, l) >= data_size)
384044871b1bSChris Mason 			return 0;
384144871b1bSChris Mason 	}
384244871b1bSChris Mason 
384344871b1bSChris Mason 	if (!path->nodes[1]) {
384444871b1bSChris Mason 		ret = insert_new_root(trans, root, path, 1);
384544871b1bSChris Mason 		if (ret)
384644871b1bSChris Mason 			return ret;
384744871b1bSChris Mason 	}
384844871b1bSChris Mason again:
38495d4f98a2SYan Zheng 	split = 1;
385044871b1bSChris Mason 	l = path->nodes[0];
385144871b1bSChris Mason 	slot = path->slots[0];
385244871b1bSChris Mason 	nritems = btrfs_header_nritems(l);
385344871b1bSChris Mason 	mid = (nritems + 1) / 2;
385444871b1bSChris Mason 
38555d4f98a2SYan Zheng 	if (mid <= slot) {
38565d4f98a2SYan Zheng 		if (nritems == 1 ||
38575d4f98a2SYan Zheng 		    leaf_space_used(l, mid, nritems - mid) + data_size >
38585d4f98a2SYan Zheng 			BTRFS_LEAF_DATA_SIZE(root)) {
38595d4f98a2SYan Zheng 			if (slot >= nritems) {
38605d4f98a2SYan Zheng 				split = 0;
38615d4f98a2SYan Zheng 			} else {
38625d4f98a2SYan Zheng 				mid = slot;
38635d4f98a2SYan Zheng 				if (mid != nritems &&
38645d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
38655d4f98a2SYan Zheng 				    data_size > BTRFS_LEAF_DATA_SIZE(root)) {
386699d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
386799d8f83cSChris Mason 						goto push_for_double;
38685d4f98a2SYan Zheng 					split = 2;
38695d4f98a2SYan Zheng 				}
38705d4f98a2SYan Zheng 			}
38715d4f98a2SYan Zheng 		}
38725d4f98a2SYan Zheng 	} else {
38735d4f98a2SYan Zheng 		if (leaf_space_used(l, 0, mid) + data_size >
38745d4f98a2SYan Zheng 			BTRFS_LEAF_DATA_SIZE(root)) {
38755d4f98a2SYan Zheng 			if (!extend && data_size && slot == 0) {
38765d4f98a2SYan Zheng 				split = 0;
38775d4f98a2SYan Zheng 			} else if ((extend || !data_size) && slot == 0) {
38785d4f98a2SYan Zheng 				mid = 1;
38795d4f98a2SYan Zheng 			} else {
38805d4f98a2SYan Zheng 				mid = slot;
38815d4f98a2SYan Zheng 				if (mid != nritems &&
38825d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
38835d4f98a2SYan Zheng 				    data_size > BTRFS_LEAF_DATA_SIZE(root)) {
388499d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
388599d8f83cSChris Mason 						goto push_for_double;
38865d4f98a2SYan Zheng 					split = 2 ;
38875d4f98a2SYan Zheng 				}
38885d4f98a2SYan Zheng 			}
38895d4f98a2SYan Zheng 		}
38905d4f98a2SYan Zheng 	}
38915d4f98a2SYan Zheng 
38925d4f98a2SYan Zheng 	if (split == 0)
38935d4f98a2SYan Zheng 		btrfs_cpu_key_to_disk(&disk_key, ins_key);
38945d4f98a2SYan Zheng 	else
38955d4f98a2SYan Zheng 		btrfs_item_key(l, &disk_key, mid);
38965d4f98a2SYan Zheng 
38975d4f98a2SYan Zheng 	right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
389844871b1bSChris Mason 					root->root_key.objectid,
38995581a51aSJan Schmidt 					&disk_key, 0, l->start, 0);
3900f0486c68SYan, Zheng 	if (IS_ERR(right))
390144871b1bSChris Mason 		return PTR_ERR(right);
3902f0486c68SYan, Zheng 
3903f0486c68SYan, Zheng 	root_add_used(root, root->leafsize);
390444871b1bSChris Mason 
390544871b1bSChris Mason 	memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
390644871b1bSChris Mason 	btrfs_set_header_bytenr(right, right->start);
390744871b1bSChris Mason 	btrfs_set_header_generation(right, trans->transid);
39085d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
390944871b1bSChris Mason 	btrfs_set_header_owner(right, root->root_key.objectid);
391044871b1bSChris Mason 	btrfs_set_header_level(right, 0);
391144871b1bSChris Mason 	write_extent_buffer(right, root->fs_info->fsid,
391244871b1bSChris Mason 			    (unsigned long)btrfs_header_fsid(right),
391344871b1bSChris Mason 			    BTRFS_FSID_SIZE);
391444871b1bSChris Mason 
391544871b1bSChris Mason 	write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
391644871b1bSChris Mason 			    (unsigned long)btrfs_header_chunk_tree_uuid(right),
391744871b1bSChris Mason 			    BTRFS_UUID_SIZE);
391844871b1bSChris Mason 
39195d4f98a2SYan Zheng 	if (split == 0) {
392044871b1bSChris Mason 		if (mid <= slot) {
392144871b1bSChris Mason 			btrfs_set_header_nritems(right, 0);
3922143bede5SJeff Mahoney 			insert_ptr(trans, root, path, &disk_key, right->start,
3923c3e06965SJan Schmidt 				   path->slots[1] + 1, 1);
392444871b1bSChris Mason 			btrfs_tree_unlock(path->nodes[0]);
392544871b1bSChris Mason 			free_extent_buffer(path->nodes[0]);
392644871b1bSChris Mason 			path->nodes[0] = right;
392744871b1bSChris Mason 			path->slots[0] = 0;
392844871b1bSChris Mason 			path->slots[1] += 1;
392944871b1bSChris Mason 		} else {
393044871b1bSChris Mason 			btrfs_set_header_nritems(right, 0);
3931143bede5SJeff Mahoney 			insert_ptr(trans, root, path, &disk_key, right->start,
3932c3e06965SJan Schmidt 					  path->slots[1], 1);
393344871b1bSChris Mason 			btrfs_tree_unlock(path->nodes[0]);
393444871b1bSChris Mason 			free_extent_buffer(path->nodes[0]);
393544871b1bSChris Mason 			path->nodes[0] = right;
393644871b1bSChris Mason 			path->slots[0] = 0;
3937143bede5SJeff Mahoney 			if (path->slots[1] == 0)
3938143bede5SJeff Mahoney 				fixup_low_keys(trans, root, path,
3939143bede5SJeff Mahoney 					       &disk_key, 1);
39405d4f98a2SYan Zheng 		}
394144871b1bSChris Mason 		btrfs_mark_buffer_dirty(right);
394244871b1bSChris Mason 		return ret;
394344871b1bSChris Mason 	}
394444871b1bSChris Mason 
3945143bede5SJeff Mahoney 	copy_for_split(trans, root, path, l, right, slot, mid, nritems);
394644871b1bSChris Mason 
39475d4f98a2SYan Zheng 	if (split == 2) {
3948cc0c5538SChris Mason 		BUG_ON(num_doubles != 0);
3949cc0c5538SChris Mason 		num_doubles++;
3950cc0c5538SChris Mason 		goto again;
39513326d1b0SChris Mason 	}
395244871b1bSChris Mason 
3953143bede5SJeff Mahoney 	return 0;
395499d8f83cSChris Mason 
395599d8f83cSChris Mason push_for_double:
395699d8f83cSChris Mason 	push_for_double_split(trans, root, path, data_size);
395799d8f83cSChris Mason 	tried_avoid_double = 1;
395899d8f83cSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
395999d8f83cSChris Mason 		return 0;
396099d8f83cSChris Mason 	goto again;
3961be0e5c09SChris Mason }
3962be0e5c09SChris Mason 
3963ad48fd75SYan, Zheng static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3964ad48fd75SYan, Zheng 					 struct btrfs_root *root,
3965ad48fd75SYan, Zheng 					 struct btrfs_path *path, int ins_len)
3966ad48fd75SYan, Zheng {
3967ad48fd75SYan, Zheng 	struct btrfs_key key;
3968ad48fd75SYan, Zheng 	struct extent_buffer *leaf;
3969ad48fd75SYan, Zheng 	struct btrfs_file_extent_item *fi;
3970ad48fd75SYan, Zheng 	u64 extent_len = 0;
3971ad48fd75SYan, Zheng 	u32 item_size;
3972ad48fd75SYan, Zheng 	int ret;
3973ad48fd75SYan, Zheng 
3974ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3975ad48fd75SYan, Zheng 	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3976ad48fd75SYan, Zheng 
3977ad48fd75SYan, Zheng 	BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3978ad48fd75SYan, Zheng 	       key.type != BTRFS_EXTENT_CSUM_KEY);
3979ad48fd75SYan, Zheng 
3980ad48fd75SYan, Zheng 	if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3981ad48fd75SYan, Zheng 		return 0;
3982ad48fd75SYan, Zheng 
3983ad48fd75SYan, Zheng 	item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3984ad48fd75SYan, Zheng 	if (key.type == BTRFS_EXTENT_DATA_KEY) {
3985ad48fd75SYan, Zheng 		fi = btrfs_item_ptr(leaf, path->slots[0],
3986ad48fd75SYan, Zheng 				    struct btrfs_file_extent_item);
3987ad48fd75SYan, Zheng 		extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3988ad48fd75SYan, Zheng 	}
3989b3b4aa74SDavid Sterba 	btrfs_release_path(path);
3990ad48fd75SYan, Zheng 
3991ad48fd75SYan, Zheng 	path->keep_locks = 1;
3992ad48fd75SYan, Zheng 	path->search_for_split = 1;
3993ad48fd75SYan, Zheng 	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3994ad48fd75SYan, Zheng 	path->search_for_split = 0;
3995ad48fd75SYan, Zheng 	if (ret < 0)
3996ad48fd75SYan, Zheng 		goto err;
3997ad48fd75SYan, Zheng 
3998ad48fd75SYan, Zheng 	ret = -EAGAIN;
3999ad48fd75SYan, Zheng 	leaf = path->nodes[0];
4000ad48fd75SYan, Zheng 	/* if our item isn't there or got smaller, return now */
4001ad48fd75SYan, Zheng 	if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4002ad48fd75SYan, Zheng 		goto err;
4003ad48fd75SYan, Zheng 
4004109f6aefSChris Mason 	/* the leaf has  changed, it now has room.  return now */
4005109f6aefSChris Mason 	if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4006109f6aefSChris Mason 		goto err;
4007109f6aefSChris Mason 
4008ad48fd75SYan, Zheng 	if (key.type == BTRFS_EXTENT_DATA_KEY) {
4009ad48fd75SYan, Zheng 		fi = btrfs_item_ptr(leaf, path->slots[0],
4010ad48fd75SYan, Zheng 				    struct btrfs_file_extent_item);
4011ad48fd75SYan, Zheng 		if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4012ad48fd75SYan, Zheng 			goto err;
4013ad48fd75SYan, Zheng 	}
4014ad48fd75SYan, Zheng 
4015ad48fd75SYan, Zheng 	btrfs_set_path_blocking(path);
4016ad48fd75SYan, Zheng 	ret = split_leaf(trans, root, &key, path, ins_len, 1);
4017f0486c68SYan, Zheng 	if (ret)
4018f0486c68SYan, Zheng 		goto err;
4019ad48fd75SYan, Zheng 
4020ad48fd75SYan, Zheng 	path->keep_locks = 0;
4021ad48fd75SYan, Zheng 	btrfs_unlock_up_safe(path, 1);
4022ad48fd75SYan, Zheng 	return 0;
4023ad48fd75SYan, Zheng err:
4024ad48fd75SYan, Zheng 	path->keep_locks = 0;
4025ad48fd75SYan, Zheng 	return ret;
4026ad48fd75SYan, Zheng }
4027ad48fd75SYan, Zheng 
4028ad48fd75SYan, Zheng static noinline int split_item(struct btrfs_trans_handle *trans,
4029459931ecSChris Mason 			       struct btrfs_root *root,
4030459931ecSChris Mason 			       struct btrfs_path *path,
4031459931ecSChris Mason 			       struct btrfs_key *new_key,
4032459931ecSChris Mason 			       unsigned long split_offset)
4033459931ecSChris Mason {
4034459931ecSChris Mason 	struct extent_buffer *leaf;
4035459931ecSChris Mason 	struct btrfs_item *item;
4036459931ecSChris Mason 	struct btrfs_item *new_item;
4037459931ecSChris Mason 	int slot;
4038ad48fd75SYan, Zheng 	char *buf;
4039459931ecSChris Mason 	u32 nritems;
4040ad48fd75SYan, Zheng 	u32 item_size;
4041459931ecSChris Mason 	u32 orig_offset;
4042459931ecSChris Mason 	struct btrfs_disk_key disk_key;
4043459931ecSChris Mason 
4044459931ecSChris Mason 	leaf = path->nodes[0];
4045b9473439SChris Mason 	BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4046b9473439SChris Mason 
4047b4ce94deSChris Mason 	btrfs_set_path_blocking(path);
4048b4ce94deSChris Mason 
4049459931ecSChris Mason 	item = btrfs_item_nr(leaf, path->slots[0]);
4050459931ecSChris Mason 	orig_offset = btrfs_item_offset(leaf, item);
4051459931ecSChris Mason 	item_size = btrfs_item_size(leaf, item);
4052459931ecSChris Mason 
4053459931ecSChris Mason 	buf = kmalloc(item_size, GFP_NOFS);
4054ad48fd75SYan, Zheng 	if (!buf)
4055ad48fd75SYan, Zheng 		return -ENOMEM;
4056ad48fd75SYan, Zheng 
4057459931ecSChris Mason 	read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4058459931ecSChris Mason 			    path->slots[0]), item_size);
4059ad48fd75SYan, Zheng 
4060459931ecSChris Mason 	slot = path->slots[0] + 1;
4061459931ecSChris Mason 	nritems = btrfs_header_nritems(leaf);
4062459931ecSChris Mason 	if (slot != nritems) {
4063459931ecSChris Mason 		/* shift the items */
4064459931ecSChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
4065459931ecSChris Mason 				btrfs_item_nr_offset(slot),
4066459931ecSChris Mason 				(nritems - slot) * sizeof(struct btrfs_item));
4067459931ecSChris Mason 	}
4068459931ecSChris Mason 
4069459931ecSChris Mason 	btrfs_cpu_key_to_disk(&disk_key, new_key);
4070459931ecSChris Mason 	btrfs_set_item_key(leaf, &disk_key, slot);
4071459931ecSChris Mason 
4072459931ecSChris Mason 	new_item = btrfs_item_nr(leaf, slot);
4073459931ecSChris Mason 
4074459931ecSChris Mason 	btrfs_set_item_offset(leaf, new_item, orig_offset);
4075459931ecSChris Mason 	btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4076459931ecSChris Mason 
4077459931ecSChris Mason 	btrfs_set_item_offset(leaf, item,
4078459931ecSChris Mason 			      orig_offset + item_size - split_offset);
4079459931ecSChris Mason 	btrfs_set_item_size(leaf, item, split_offset);
4080459931ecSChris Mason 
4081459931ecSChris Mason 	btrfs_set_header_nritems(leaf, nritems + 1);
4082459931ecSChris Mason 
4083459931ecSChris Mason 	/* write the data for the start of the original item */
4084459931ecSChris Mason 	write_extent_buffer(leaf, buf,
4085459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, path->slots[0]),
4086459931ecSChris Mason 			    split_offset);
4087459931ecSChris Mason 
4088459931ecSChris Mason 	/* write the data for the new item */
4089459931ecSChris Mason 	write_extent_buffer(leaf, buf + split_offset,
4090459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, slot),
4091459931ecSChris Mason 			    item_size - split_offset);
4092459931ecSChris Mason 	btrfs_mark_buffer_dirty(leaf);
4093459931ecSChris Mason 
4094ad48fd75SYan, Zheng 	BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
4095459931ecSChris Mason 	kfree(buf);
4096ad48fd75SYan, Zheng 	return 0;
4097ad48fd75SYan, Zheng }
4098ad48fd75SYan, Zheng 
4099ad48fd75SYan, Zheng /*
4100ad48fd75SYan, Zheng  * This function splits a single item into two items,
4101ad48fd75SYan, Zheng  * giving 'new_key' to the new item and splitting the
4102ad48fd75SYan, Zheng  * old one at split_offset (from the start of the item).
4103ad48fd75SYan, Zheng  *
4104ad48fd75SYan, Zheng  * The path may be released by this operation.  After
4105ad48fd75SYan, Zheng  * the split, the path is pointing to the old item.  The
4106ad48fd75SYan, Zheng  * new item is going to be in the same node as the old one.
4107ad48fd75SYan, Zheng  *
4108ad48fd75SYan, Zheng  * Note, the item being split must be smaller enough to live alone on
4109ad48fd75SYan, Zheng  * a tree block with room for one extra struct btrfs_item
4110ad48fd75SYan, Zheng  *
4111ad48fd75SYan, Zheng  * This allows us to split the item in place, keeping a lock on the
4112ad48fd75SYan, Zheng  * leaf the entire time.
4113ad48fd75SYan, Zheng  */
4114ad48fd75SYan, Zheng int btrfs_split_item(struct btrfs_trans_handle *trans,
4115ad48fd75SYan, Zheng 		     struct btrfs_root *root,
4116ad48fd75SYan, Zheng 		     struct btrfs_path *path,
4117ad48fd75SYan, Zheng 		     struct btrfs_key *new_key,
4118ad48fd75SYan, Zheng 		     unsigned long split_offset)
4119ad48fd75SYan, Zheng {
4120ad48fd75SYan, Zheng 	int ret;
4121ad48fd75SYan, Zheng 	ret = setup_leaf_for_split(trans, root, path,
4122ad48fd75SYan, Zheng 				   sizeof(struct btrfs_item));
4123ad48fd75SYan, Zheng 	if (ret)
4124459931ecSChris Mason 		return ret;
4125ad48fd75SYan, Zheng 
4126ad48fd75SYan, Zheng 	ret = split_item(trans, root, path, new_key, split_offset);
4127ad48fd75SYan, Zheng 	return ret;
4128ad48fd75SYan, Zheng }
4129ad48fd75SYan, Zheng 
4130ad48fd75SYan, Zheng /*
4131ad48fd75SYan, Zheng  * This function duplicate a item, giving 'new_key' to the new item.
4132ad48fd75SYan, Zheng  * It guarantees both items live in the same tree leaf and the new item
4133ad48fd75SYan, Zheng  * is contiguous with the original item.
4134ad48fd75SYan, Zheng  *
4135ad48fd75SYan, Zheng  * This allows us to split file extent in place, keeping a lock on the
4136ad48fd75SYan, Zheng  * leaf the entire time.
4137ad48fd75SYan, Zheng  */
4138ad48fd75SYan, Zheng int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4139ad48fd75SYan, Zheng 			 struct btrfs_root *root,
4140ad48fd75SYan, Zheng 			 struct btrfs_path *path,
4141ad48fd75SYan, Zheng 			 struct btrfs_key *new_key)
4142ad48fd75SYan, Zheng {
4143ad48fd75SYan, Zheng 	struct extent_buffer *leaf;
4144ad48fd75SYan, Zheng 	int ret;
4145ad48fd75SYan, Zheng 	u32 item_size;
4146ad48fd75SYan, Zheng 
4147ad48fd75SYan, Zheng 	leaf = path->nodes[0];
4148ad48fd75SYan, Zheng 	item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4149ad48fd75SYan, Zheng 	ret = setup_leaf_for_split(trans, root, path,
4150ad48fd75SYan, Zheng 				   item_size + sizeof(struct btrfs_item));
4151ad48fd75SYan, Zheng 	if (ret)
4152ad48fd75SYan, Zheng 		return ret;
4153ad48fd75SYan, Zheng 
4154ad48fd75SYan, Zheng 	path->slots[0]++;
4155143bede5SJeff Mahoney 	setup_items_for_insert(trans, root, path, new_key, &item_size,
4156ad48fd75SYan, Zheng 			       item_size, item_size +
4157ad48fd75SYan, Zheng 			       sizeof(struct btrfs_item), 1);
4158ad48fd75SYan, Zheng 	leaf = path->nodes[0];
4159ad48fd75SYan, Zheng 	memcpy_extent_buffer(leaf,
4160ad48fd75SYan, Zheng 			     btrfs_item_ptr_offset(leaf, path->slots[0]),
4161ad48fd75SYan, Zheng 			     btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4162ad48fd75SYan, Zheng 			     item_size);
4163ad48fd75SYan, Zheng 	return 0;
4164459931ecSChris Mason }
4165459931ecSChris Mason 
4166459931ecSChris Mason /*
4167d352ac68SChris Mason  * make the item pointed to by the path smaller.  new_size indicates
4168d352ac68SChris Mason  * how small to make it, and from_end tells us if we just chop bytes
4169d352ac68SChris Mason  * off the end of the item or if we shift the item to chop bytes off
4170d352ac68SChris Mason  * the front.
4171d352ac68SChris Mason  */
4172143bede5SJeff Mahoney void btrfs_truncate_item(struct btrfs_trans_handle *trans,
4173b18c6685SChris Mason 			 struct btrfs_root *root,
4174b18c6685SChris Mason 			 struct btrfs_path *path,
4175179e29e4SChris Mason 			 u32 new_size, int from_end)
4176b18c6685SChris Mason {
4177b18c6685SChris Mason 	int slot;
41785f39d397SChris Mason 	struct extent_buffer *leaf;
41795f39d397SChris Mason 	struct btrfs_item *item;
4180b18c6685SChris Mason 	u32 nritems;
4181b18c6685SChris Mason 	unsigned int data_end;
4182b18c6685SChris Mason 	unsigned int old_data_start;
4183b18c6685SChris Mason 	unsigned int old_size;
4184b18c6685SChris Mason 	unsigned int size_diff;
4185b18c6685SChris Mason 	int i;
4186cfed81a0SChris Mason 	struct btrfs_map_token token;
4187cfed81a0SChris Mason 
4188cfed81a0SChris Mason 	btrfs_init_map_token(&token);
4189b18c6685SChris Mason 
41905f39d397SChris Mason 	leaf = path->nodes[0];
4191179e29e4SChris Mason 	slot = path->slots[0];
4192179e29e4SChris Mason 
4193179e29e4SChris Mason 	old_size = btrfs_item_size_nr(leaf, slot);
4194179e29e4SChris Mason 	if (old_size == new_size)
4195143bede5SJeff Mahoney 		return;
4196b18c6685SChris Mason 
41975f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
4198b18c6685SChris Mason 	data_end = leaf_data_end(root, leaf);
4199b18c6685SChris Mason 
42005f39d397SChris Mason 	old_data_start = btrfs_item_offset_nr(leaf, slot);
4201179e29e4SChris Mason 
4202b18c6685SChris Mason 	size_diff = old_size - new_size;
4203b18c6685SChris Mason 
4204b18c6685SChris Mason 	BUG_ON(slot < 0);
4205b18c6685SChris Mason 	BUG_ON(slot >= nritems);
4206b18c6685SChris Mason 
4207b18c6685SChris Mason 	/*
4208b18c6685SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4209b18c6685SChris Mason 	 */
4210b18c6685SChris Mason 	/* first correct the data pointers */
4211b18c6685SChris Mason 	for (i = slot; i < nritems; i++) {
42125f39d397SChris Mason 		u32 ioff;
42135f39d397SChris Mason 		item = btrfs_item_nr(leaf, i);
4214db94535dSChris Mason 
4215cfed81a0SChris Mason 		ioff = btrfs_token_item_offset(leaf, item, &token);
4216cfed81a0SChris Mason 		btrfs_set_token_item_offset(leaf, item,
4217cfed81a0SChris Mason 					    ioff + size_diff, &token);
4218b18c6685SChris Mason 	}
4219db94535dSChris Mason 
4220b18c6685SChris Mason 	/* shift the data */
4221179e29e4SChris Mason 	if (from_end) {
42225f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4223b18c6685SChris Mason 			      data_end + size_diff, btrfs_leaf_data(leaf) +
4224b18c6685SChris Mason 			      data_end, old_data_start + new_size - data_end);
4225179e29e4SChris Mason 	} else {
4226179e29e4SChris Mason 		struct btrfs_disk_key disk_key;
4227179e29e4SChris Mason 		u64 offset;
4228179e29e4SChris Mason 
4229179e29e4SChris Mason 		btrfs_item_key(leaf, &disk_key, slot);
4230179e29e4SChris Mason 
4231179e29e4SChris Mason 		if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4232179e29e4SChris Mason 			unsigned long ptr;
4233179e29e4SChris Mason 			struct btrfs_file_extent_item *fi;
4234179e29e4SChris Mason 
4235179e29e4SChris Mason 			fi = btrfs_item_ptr(leaf, slot,
4236179e29e4SChris Mason 					    struct btrfs_file_extent_item);
4237179e29e4SChris Mason 			fi = (struct btrfs_file_extent_item *)(
4238179e29e4SChris Mason 			     (unsigned long)fi - size_diff);
4239179e29e4SChris Mason 
4240179e29e4SChris Mason 			if (btrfs_file_extent_type(leaf, fi) ==
4241179e29e4SChris Mason 			    BTRFS_FILE_EXTENT_INLINE) {
4242179e29e4SChris Mason 				ptr = btrfs_item_ptr_offset(leaf, slot);
4243179e29e4SChris Mason 				memmove_extent_buffer(leaf, ptr,
4244179e29e4SChris Mason 				      (unsigned long)fi,
4245179e29e4SChris Mason 				      offsetof(struct btrfs_file_extent_item,
4246179e29e4SChris Mason 						 disk_bytenr));
4247179e29e4SChris Mason 			}
4248179e29e4SChris Mason 		}
4249179e29e4SChris Mason 
4250179e29e4SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4251179e29e4SChris Mason 			      data_end + size_diff, btrfs_leaf_data(leaf) +
4252179e29e4SChris Mason 			      data_end, old_data_start - data_end);
4253179e29e4SChris Mason 
4254179e29e4SChris Mason 		offset = btrfs_disk_key_offset(&disk_key);
4255179e29e4SChris Mason 		btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4256179e29e4SChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot);
4257179e29e4SChris Mason 		if (slot == 0)
4258179e29e4SChris Mason 			fixup_low_keys(trans, root, path, &disk_key, 1);
4259179e29e4SChris Mason 	}
42605f39d397SChris Mason 
42615f39d397SChris Mason 	item = btrfs_item_nr(leaf, slot);
42625f39d397SChris Mason 	btrfs_set_item_size(leaf, item, new_size);
42635f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
4264b18c6685SChris Mason 
42655f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
42665f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
4267b18c6685SChris Mason 		BUG();
42685f39d397SChris Mason 	}
4269b18c6685SChris Mason }
4270b18c6685SChris Mason 
4271d352ac68SChris Mason /*
4272d352ac68SChris Mason  * make the item pointed to by the path bigger, data_size is the new size.
4273d352ac68SChris Mason  */
4274143bede5SJeff Mahoney void btrfs_extend_item(struct btrfs_trans_handle *trans,
42755f39d397SChris Mason 		       struct btrfs_root *root, struct btrfs_path *path,
42765f39d397SChris Mason 		       u32 data_size)
42776567e837SChris Mason {
42786567e837SChris Mason 	int slot;
42795f39d397SChris Mason 	struct extent_buffer *leaf;
42805f39d397SChris Mason 	struct btrfs_item *item;
42816567e837SChris Mason 	u32 nritems;
42826567e837SChris Mason 	unsigned int data_end;
42836567e837SChris Mason 	unsigned int old_data;
42846567e837SChris Mason 	unsigned int old_size;
42856567e837SChris Mason 	int i;
4286cfed81a0SChris Mason 	struct btrfs_map_token token;
4287cfed81a0SChris Mason 
4288cfed81a0SChris Mason 	btrfs_init_map_token(&token);
42896567e837SChris Mason 
42905f39d397SChris Mason 	leaf = path->nodes[0];
42916567e837SChris Mason 
42925f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
42936567e837SChris Mason 	data_end = leaf_data_end(root, leaf);
42946567e837SChris Mason 
42955f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < data_size) {
42965f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
42976567e837SChris Mason 		BUG();
42985f39d397SChris Mason 	}
42996567e837SChris Mason 	slot = path->slots[0];
43005f39d397SChris Mason 	old_data = btrfs_item_end_nr(leaf, slot);
43016567e837SChris Mason 
43026567e837SChris Mason 	BUG_ON(slot < 0);
43033326d1b0SChris Mason 	if (slot >= nritems) {
43043326d1b0SChris Mason 		btrfs_print_leaf(root, leaf);
4305d397712bSChris Mason 		printk(KERN_CRIT "slot %d too large, nritems %d\n",
4306d397712bSChris Mason 		       slot, nritems);
43073326d1b0SChris Mason 		BUG_ON(1);
43083326d1b0SChris Mason 	}
43096567e837SChris Mason 
43106567e837SChris Mason 	/*
43116567e837SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
43126567e837SChris Mason 	 */
43136567e837SChris Mason 	/* first correct the data pointers */
43146567e837SChris Mason 	for (i = slot; i < nritems; i++) {
43155f39d397SChris Mason 		u32 ioff;
43165f39d397SChris Mason 		item = btrfs_item_nr(leaf, i);
4317db94535dSChris Mason 
4318cfed81a0SChris Mason 		ioff = btrfs_token_item_offset(leaf, item, &token);
4319cfed81a0SChris Mason 		btrfs_set_token_item_offset(leaf, item,
4320cfed81a0SChris Mason 					    ioff - data_size, &token);
43216567e837SChris Mason 	}
43225f39d397SChris Mason 
43236567e837SChris Mason 	/* shift the data */
43245f39d397SChris Mason 	memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
43256567e837SChris Mason 		      data_end - data_size, btrfs_leaf_data(leaf) +
43266567e837SChris Mason 		      data_end, old_data - data_end);
43275f39d397SChris Mason 
43286567e837SChris Mason 	data_end = old_data;
43295f39d397SChris Mason 	old_size = btrfs_item_size_nr(leaf, slot);
43305f39d397SChris Mason 	item = btrfs_item_nr(leaf, slot);
43315f39d397SChris Mason 	btrfs_set_item_size(leaf, item, old_size + data_size);
43325f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
43336567e837SChris Mason 
43345f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
43355f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
43366567e837SChris Mason 		BUG();
43375f39d397SChris Mason 	}
43386567e837SChris Mason }
43396567e837SChris Mason 
434074123bd7SChris Mason /*
4341d352ac68SChris Mason  * Given a key and some data, insert items into the tree.
434274123bd7SChris Mason  * This does all the path init required, making room in the tree if needed.
4343f3465ca4SJosef Bacik  * Returns the number of keys that were inserted.
4344f3465ca4SJosef Bacik  */
4345f3465ca4SJosef Bacik int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
4346f3465ca4SJosef Bacik 			    struct btrfs_root *root,
4347f3465ca4SJosef Bacik 			    struct btrfs_path *path,
4348f3465ca4SJosef Bacik 			    struct btrfs_key *cpu_key, u32 *data_size,
4349f3465ca4SJosef Bacik 			    int nr)
4350f3465ca4SJosef Bacik {
4351f3465ca4SJosef Bacik 	struct extent_buffer *leaf;
4352f3465ca4SJosef Bacik 	struct btrfs_item *item;
4353f3465ca4SJosef Bacik 	int ret = 0;
4354f3465ca4SJosef Bacik 	int slot;
4355f3465ca4SJosef Bacik 	int i;
4356f3465ca4SJosef Bacik 	u32 nritems;
4357f3465ca4SJosef Bacik 	u32 total_data = 0;
4358f3465ca4SJosef Bacik 	u32 total_size = 0;
4359f3465ca4SJosef Bacik 	unsigned int data_end;
4360f3465ca4SJosef Bacik 	struct btrfs_disk_key disk_key;
4361f3465ca4SJosef Bacik 	struct btrfs_key found_key;
4362cfed81a0SChris Mason 	struct btrfs_map_token token;
4363cfed81a0SChris Mason 
4364cfed81a0SChris Mason 	btrfs_init_map_token(&token);
4365f3465ca4SJosef Bacik 
436687b29b20SYan Zheng 	for (i = 0; i < nr; i++) {
436787b29b20SYan Zheng 		if (total_size + data_size[i] + sizeof(struct btrfs_item) >
436887b29b20SYan Zheng 		    BTRFS_LEAF_DATA_SIZE(root)) {
436987b29b20SYan Zheng 			break;
437087b29b20SYan Zheng 			nr = i;
437187b29b20SYan Zheng 		}
4372f3465ca4SJosef Bacik 		total_data += data_size[i];
437387b29b20SYan Zheng 		total_size += data_size[i] + sizeof(struct btrfs_item);
437487b29b20SYan Zheng 	}
437587b29b20SYan Zheng 	BUG_ON(nr == 0);
4376f3465ca4SJosef Bacik 
4377f3465ca4SJosef Bacik 	ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4378f3465ca4SJosef Bacik 	if (ret == 0)
4379f3465ca4SJosef Bacik 		return -EEXIST;
4380f3465ca4SJosef Bacik 	if (ret < 0)
4381f3465ca4SJosef Bacik 		goto out;
4382f3465ca4SJosef Bacik 
4383f3465ca4SJosef Bacik 	leaf = path->nodes[0];
4384f3465ca4SJosef Bacik 
4385f3465ca4SJosef Bacik 	nritems = btrfs_header_nritems(leaf);
4386f3465ca4SJosef Bacik 	data_end = leaf_data_end(root, leaf);
4387f3465ca4SJosef Bacik 
4388f3465ca4SJosef Bacik 	if (btrfs_leaf_free_space(root, leaf) < total_size) {
4389f3465ca4SJosef Bacik 		for (i = nr; i >= 0; i--) {
4390f3465ca4SJosef Bacik 			total_data -= data_size[i];
4391f3465ca4SJosef Bacik 			total_size -= data_size[i] + sizeof(struct btrfs_item);
4392f3465ca4SJosef Bacik 			if (total_size < btrfs_leaf_free_space(root, leaf))
4393f3465ca4SJosef Bacik 				break;
4394f3465ca4SJosef Bacik 		}
4395f3465ca4SJosef Bacik 		nr = i;
4396f3465ca4SJosef Bacik 	}
4397f3465ca4SJosef Bacik 
4398f3465ca4SJosef Bacik 	slot = path->slots[0];
4399f3465ca4SJosef Bacik 	BUG_ON(slot < 0);
4400f3465ca4SJosef Bacik 
4401f3465ca4SJosef Bacik 	if (slot != nritems) {
4402f3465ca4SJosef Bacik 		unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4403f3465ca4SJosef Bacik 
4404f3465ca4SJosef Bacik 		item = btrfs_item_nr(leaf, slot);
4405f3465ca4SJosef Bacik 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
4406f3465ca4SJosef Bacik 
4407f3465ca4SJosef Bacik 		/* figure out how many keys we can insert in here */
4408f3465ca4SJosef Bacik 		total_data = data_size[0];
4409f3465ca4SJosef Bacik 		for (i = 1; i < nr; i++) {
44105d4f98a2SYan Zheng 			if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
4411f3465ca4SJosef Bacik 				break;
4412f3465ca4SJosef Bacik 			total_data += data_size[i];
4413f3465ca4SJosef Bacik 		}
4414f3465ca4SJosef Bacik 		nr = i;
4415f3465ca4SJosef Bacik 
4416f3465ca4SJosef Bacik 		if (old_data < data_end) {
4417f3465ca4SJosef Bacik 			btrfs_print_leaf(root, leaf);
4418d397712bSChris Mason 			printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
4419f3465ca4SJosef Bacik 			       slot, old_data, data_end);
4420f3465ca4SJosef Bacik 			BUG_ON(1);
4421f3465ca4SJosef Bacik 		}
4422f3465ca4SJosef Bacik 		/*
4423f3465ca4SJosef Bacik 		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4424f3465ca4SJosef Bacik 		 */
4425f3465ca4SJosef Bacik 		/* first correct the data pointers */
4426f3465ca4SJosef Bacik 		for (i = slot; i < nritems; i++) {
4427f3465ca4SJosef Bacik 			u32 ioff;
4428f3465ca4SJosef Bacik 
4429f3465ca4SJosef Bacik 			item = btrfs_item_nr(leaf, i);
4430cfed81a0SChris Mason 			ioff = btrfs_token_item_offset(leaf, item, &token);
4431cfed81a0SChris Mason 			btrfs_set_token_item_offset(leaf, item,
4432cfed81a0SChris Mason 						    ioff - total_data, &token);
4433f3465ca4SJosef Bacik 		}
4434f3465ca4SJosef Bacik 		/* shift the items */
4435f3465ca4SJosef Bacik 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4436f3465ca4SJosef Bacik 			      btrfs_item_nr_offset(slot),
4437f3465ca4SJosef Bacik 			      (nritems - slot) * sizeof(struct btrfs_item));
4438f3465ca4SJosef Bacik 
4439f3465ca4SJosef Bacik 		/* shift the data */
4440f3465ca4SJosef Bacik 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4441f3465ca4SJosef Bacik 			      data_end - total_data, btrfs_leaf_data(leaf) +
4442f3465ca4SJosef Bacik 			      data_end, old_data - data_end);
4443f3465ca4SJosef Bacik 		data_end = old_data;
4444f3465ca4SJosef Bacik 	} else {
4445f3465ca4SJosef Bacik 		/*
4446f3465ca4SJosef Bacik 		 * this sucks but it has to be done, if we are inserting at
4447f3465ca4SJosef Bacik 		 * the end of the leaf only insert 1 of the items, since we
4448f3465ca4SJosef Bacik 		 * have no way of knowing whats on the next leaf and we'd have
4449f3465ca4SJosef Bacik 		 * to drop our current locks to figure it out
4450f3465ca4SJosef Bacik 		 */
4451f3465ca4SJosef Bacik 		nr = 1;
4452f3465ca4SJosef Bacik 	}
4453f3465ca4SJosef Bacik 
4454f3465ca4SJosef Bacik 	/* setup the item for the new data */
4455f3465ca4SJosef Bacik 	for (i = 0; i < nr; i++) {
4456f3465ca4SJosef Bacik 		btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4457f3465ca4SJosef Bacik 		btrfs_set_item_key(leaf, &disk_key, slot + i);
4458f3465ca4SJosef Bacik 		item = btrfs_item_nr(leaf, slot + i);
4459cfed81a0SChris Mason 		btrfs_set_token_item_offset(leaf, item,
4460cfed81a0SChris Mason 					    data_end - data_size[i], &token);
4461f3465ca4SJosef Bacik 		data_end -= data_size[i];
4462cfed81a0SChris Mason 		btrfs_set_token_item_size(leaf, item, data_size[i], &token);
4463f3465ca4SJosef Bacik 	}
4464f3465ca4SJosef Bacik 	btrfs_set_header_nritems(leaf, nritems + nr);
4465f3465ca4SJosef Bacik 	btrfs_mark_buffer_dirty(leaf);
4466f3465ca4SJosef Bacik 
4467f3465ca4SJosef Bacik 	ret = 0;
4468f3465ca4SJosef Bacik 	if (slot == 0) {
4469f3465ca4SJosef Bacik 		btrfs_cpu_key_to_disk(&disk_key, cpu_key);
4470143bede5SJeff Mahoney 		fixup_low_keys(trans, root, path, &disk_key, 1);
4471f3465ca4SJosef Bacik 	}
4472f3465ca4SJosef Bacik 
4473f3465ca4SJosef Bacik 	if (btrfs_leaf_free_space(root, leaf) < 0) {
4474f3465ca4SJosef Bacik 		btrfs_print_leaf(root, leaf);
4475f3465ca4SJosef Bacik 		BUG();
4476f3465ca4SJosef Bacik 	}
4477f3465ca4SJosef Bacik out:
4478f3465ca4SJosef Bacik 	if (!ret)
4479f3465ca4SJosef Bacik 		ret = nr;
4480f3465ca4SJosef Bacik 	return ret;
4481f3465ca4SJosef Bacik }
4482f3465ca4SJosef Bacik 
4483f3465ca4SJosef Bacik /*
448444871b1bSChris Mason  * this is a helper for btrfs_insert_empty_items, the main goal here is
448544871b1bSChris Mason  * to save stack depth by doing the bulk of the work in a function
448644871b1bSChris Mason  * that doesn't call btrfs_search_slot
448774123bd7SChris Mason  */
4488143bede5SJeff Mahoney void setup_items_for_insert(struct btrfs_trans_handle *trans,
448944871b1bSChris Mason 			    struct btrfs_root *root, struct btrfs_path *path,
44909c58309dSChris Mason 			    struct btrfs_key *cpu_key, u32 *data_size,
449144871b1bSChris Mason 			    u32 total_data, u32 total_size, int nr)
4492be0e5c09SChris Mason {
44935f39d397SChris Mason 	struct btrfs_item *item;
44949c58309dSChris Mason 	int i;
44957518a238SChris Mason 	u32 nritems;
4496be0e5c09SChris Mason 	unsigned int data_end;
4497e2fa7227SChris Mason 	struct btrfs_disk_key disk_key;
449844871b1bSChris Mason 	struct extent_buffer *leaf;
449944871b1bSChris Mason 	int slot;
4500cfed81a0SChris Mason 	struct btrfs_map_token token;
4501cfed81a0SChris Mason 
4502cfed81a0SChris Mason 	btrfs_init_map_token(&token);
4503e2fa7227SChris Mason 
45045f39d397SChris Mason 	leaf = path->nodes[0];
450544871b1bSChris Mason 	slot = path->slots[0];
450674123bd7SChris Mason 
45075f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
4508123abc88SChris Mason 	data_end = leaf_data_end(root, leaf);
4509eb60ceacSChris Mason 
4510f25956ccSChris Mason 	if (btrfs_leaf_free_space(root, leaf) < total_size) {
45113326d1b0SChris Mason 		btrfs_print_leaf(root, leaf);
4512d397712bSChris Mason 		printk(KERN_CRIT "not enough freespace need %u have %d\n",
45139c58309dSChris Mason 		       total_size, btrfs_leaf_free_space(root, leaf));
4514be0e5c09SChris Mason 		BUG();
4515d4dbff95SChris Mason 	}
45165f39d397SChris Mason 
4517be0e5c09SChris Mason 	if (slot != nritems) {
45185f39d397SChris Mason 		unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4519be0e5c09SChris Mason 
45205f39d397SChris Mason 		if (old_data < data_end) {
45215f39d397SChris Mason 			btrfs_print_leaf(root, leaf);
4522d397712bSChris Mason 			printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
45235f39d397SChris Mason 			       slot, old_data, data_end);
45245f39d397SChris Mason 			BUG_ON(1);
45255f39d397SChris Mason 		}
4526be0e5c09SChris Mason 		/*
4527be0e5c09SChris Mason 		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4528be0e5c09SChris Mason 		 */
4529be0e5c09SChris Mason 		/* first correct the data pointers */
45300783fcfcSChris Mason 		for (i = slot; i < nritems; i++) {
45315f39d397SChris Mason 			u32 ioff;
4532db94535dSChris Mason 
45335f39d397SChris Mason 			item = btrfs_item_nr(leaf, i);
4534cfed81a0SChris Mason 			ioff = btrfs_token_item_offset(leaf, item, &token);
4535cfed81a0SChris Mason 			btrfs_set_token_item_offset(leaf, item,
4536cfed81a0SChris Mason 						    ioff - total_data, &token);
45370783fcfcSChris Mason 		}
4538be0e5c09SChris Mason 		/* shift the items */
45399c58309dSChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
45405f39d397SChris Mason 			      btrfs_item_nr_offset(slot),
45410783fcfcSChris Mason 			      (nritems - slot) * sizeof(struct btrfs_item));
4542be0e5c09SChris Mason 
4543be0e5c09SChris Mason 		/* shift the data */
45445f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
45459c58309dSChris Mason 			      data_end - total_data, btrfs_leaf_data(leaf) +
4546be0e5c09SChris Mason 			      data_end, old_data - data_end);
4547be0e5c09SChris Mason 		data_end = old_data;
4548be0e5c09SChris Mason 	}
45495f39d397SChris Mason 
455062e2749eSChris Mason 	/* setup the item for the new data */
45519c58309dSChris Mason 	for (i = 0; i < nr; i++) {
45529c58309dSChris Mason 		btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
45539c58309dSChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot + i);
45549c58309dSChris Mason 		item = btrfs_item_nr(leaf, slot + i);
4555cfed81a0SChris Mason 		btrfs_set_token_item_offset(leaf, item,
4556cfed81a0SChris Mason 					    data_end - data_size[i], &token);
45579c58309dSChris Mason 		data_end -= data_size[i];
4558cfed81a0SChris Mason 		btrfs_set_token_item_size(leaf, item, data_size[i], &token);
45599c58309dSChris Mason 	}
456044871b1bSChris Mason 
45619c58309dSChris Mason 	btrfs_set_header_nritems(leaf, nritems + nr);
4562aa5d6bedSChris Mason 
45635a01a2e3SChris Mason 	if (slot == 0) {
45645a01a2e3SChris Mason 		btrfs_cpu_key_to_disk(&disk_key, cpu_key);
4565143bede5SJeff Mahoney 		fixup_low_keys(trans, root, path, &disk_key, 1);
45665a01a2e3SChris Mason 	}
4567b9473439SChris Mason 	btrfs_unlock_up_safe(path, 1);
4568b9473439SChris Mason 	btrfs_mark_buffer_dirty(leaf);
4569aa5d6bedSChris Mason 
45705f39d397SChris Mason 	if (btrfs_leaf_free_space(root, leaf) < 0) {
45715f39d397SChris Mason 		btrfs_print_leaf(root, leaf);
4572be0e5c09SChris Mason 		BUG();
45735f39d397SChris Mason 	}
457444871b1bSChris Mason }
457544871b1bSChris Mason 
457644871b1bSChris Mason /*
457744871b1bSChris Mason  * Given a key and some data, insert items into the tree.
457844871b1bSChris Mason  * This does all the path init required, making room in the tree if needed.
457944871b1bSChris Mason  */
458044871b1bSChris Mason int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
458144871b1bSChris Mason 			    struct btrfs_root *root,
458244871b1bSChris Mason 			    struct btrfs_path *path,
458344871b1bSChris Mason 			    struct btrfs_key *cpu_key, u32 *data_size,
458444871b1bSChris Mason 			    int nr)
458544871b1bSChris Mason {
458644871b1bSChris Mason 	int ret = 0;
458744871b1bSChris Mason 	int slot;
458844871b1bSChris Mason 	int i;
458944871b1bSChris Mason 	u32 total_size = 0;
459044871b1bSChris Mason 	u32 total_data = 0;
459144871b1bSChris Mason 
459244871b1bSChris Mason 	for (i = 0; i < nr; i++)
459344871b1bSChris Mason 		total_data += data_size[i];
459444871b1bSChris Mason 
459544871b1bSChris Mason 	total_size = total_data + (nr * sizeof(struct btrfs_item));
459644871b1bSChris Mason 	ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
459744871b1bSChris Mason 	if (ret == 0)
459844871b1bSChris Mason 		return -EEXIST;
459944871b1bSChris Mason 	if (ret < 0)
4600143bede5SJeff Mahoney 		return ret;
460144871b1bSChris Mason 
460244871b1bSChris Mason 	slot = path->slots[0];
460344871b1bSChris Mason 	BUG_ON(slot < 0);
460444871b1bSChris Mason 
4605143bede5SJeff Mahoney 	setup_items_for_insert(trans, root, path, cpu_key, data_size,
460644871b1bSChris Mason 			       total_data, total_size, nr);
4607143bede5SJeff Mahoney 	return 0;
460862e2749eSChris Mason }
460962e2749eSChris Mason 
461062e2749eSChris Mason /*
461162e2749eSChris Mason  * Given a key and some data, insert an item into the tree.
461262e2749eSChris Mason  * This does all the path init required, making room in the tree if needed.
461362e2749eSChris Mason  */
4614e089f05cSChris Mason int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4615e089f05cSChris Mason 		      *root, struct btrfs_key *cpu_key, void *data, u32
4616e089f05cSChris Mason 		      data_size)
461762e2749eSChris Mason {
461862e2749eSChris Mason 	int ret = 0;
46192c90e5d6SChris Mason 	struct btrfs_path *path;
46205f39d397SChris Mason 	struct extent_buffer *leaf;
46215f39d397SChris Mason 	unsigned long ptr;
462262e2749eSChris Mason 
46232c90e5d6SChris Mason 	path = btrfs_alloc_path();
4624db5b493aSTsutomu Itoh 	if (!path)
4625db5b493aSTsutomu Itoh 		return -ENOMEM;
46262c90e5d6SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
462762e2749eSChris Mason 	if (!ret) {
46285f39d397SChris Mason 		leaf = path->nodes[0];
46295f39d397SChris Mason 		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
46305f39d397SChris Mason 		write_extent_buffer(leaf, data, ptr, data_size);
46315f39d397SChris Mason 		btrfs_mark_buffer_dirty(leaf);
463262e2749eSChris Mason 	}
46332c90e5d6SChris Mason 	btrfs_free_path(path);
4634aa5d6bedSChris Mason 	return ret;
4635be0e5c09SChris Mason }
4636be0e5c09SChris Mason 
463774123bd7SChris Mason /*
46385de08d7dSChris Mason  * delete the pointer from a given node.
463974123bd7SChris Mason  *
4640d352ac68SChris Mason  * the tree should have been previously balanced so the deletion does not
4641d352ac68SChris Mason  * empty a node.
464274123bd7SChris Mason  */
4643143bede5SJeff Mahoney static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4644f3ea38daSJan Schmidt 		    struct btrfs_path *path, int level, int slot,
4645f3ea38daSJan Schmidt 		    int tree_mod_log)
4646be0e5c09SChris Mason {
46475f39d397SChris Mason 	struct extent_buffer *parent = path->nodes[level];
46487518a238SChris Mason 	u32 nritems;
4649f3ea38daSJan Schmidt 	int ret;
4650be0e5c09SChris Mason 
46515f39d397SChris Mason 	nritems = btrfs_header_nritems(parent);
4652be0e5c09SChris Mason 	if (slot != nritems - 1) {
4653f3ea38daSJan Schmidt 		if (tree_mod_log && level)
4654f3ea38daSJan Schmidt 			tree_mod_log_eb_move(root->fs_info, parent, slot,
4655f3ea38daSJan Schmidt 					     slot + 1, nritems - slot - 1);
46565f39d397SChris Mason 		memmove_extent_buffer(parent,
46575f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot),
46585f39d397SChris Mason 			      btrfs_node_key_ptr_offset(slot + 1),
4659d6025579SChris Mason 			      sizeof(struct btrfs_key_ptr) *
4660d6025579SChris Mason 			      (nritems - slot - 1));
4661f395694cSJan Schmidt 	} else if (tree_mod_log && level) {
4662f3ea38daSJan Schmidt 		ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
4663f3ea38daSJan Schmidt 					      MOD_LOG_KEY_REMOVE);
4664f3ea38daSJan Schmidt 		BUG_ON(ret < 0);
4665be0e5c09SChris Mason 	}
4666f3ea38daSJan Schmidt 
46677518a238SChris Mason 	nritems--;
46685f39d397SChris Mason 	btrfs_set_header_nritems(parent, nritems);
46697518a238SChris Mason 	if (nritems == 0 && parent == root->node) {
46705f39d397SChris Mason 		BUG_ON(btrfs_header_level(root->node) != 1);
4671eb60ceacSChris Mason 		/* just turn the root into a leaf and break */
46725f39d397SChris Mason 		btrfs_set_header_level(root->node, 0);
4673bb803951SChris Mason 	} else if (slot == 0) {
46745f39d397SChris Mason 		struct btrfs_disk_key disk_key;
46755f39d397SChris Mason 
46765f39d397SChris Mason 		btrfs_node_key(parent, &disk_key, 0);
4677143bede5SJeff Mahoney 		fixup_low_keys(trans, root, path, &disk_key, level + 1);
4678be0e5c09SChris Mason 	}
4679d6025579SChris Mason 	btrfs_mark_buffer_dirty(parent);
4680be0e5c09SChris Mason }
4681be0e5c09SChris Mason 
468274123bd7SChris Mason /*
4683323ac95bSChris Mason  * a helper function to delete the leaf pointed to by path->slots[1] and
46845d4f98a2SYan Zheng  * path->nodes[1].
4685323ac95bSChris Mason  *
4686323ac95bSChris Mason  * This deletes the pointer in path->nodes[1] and frees the leaf
4687323ac95bSChris Mason  * block extent.  zero is returned if it all worked out, < 0 otherwise.
4688323ac95bSChris Mason  *
4689323ac95bSChris Mason  * The path must have already been setup for deleting the leaf, including
4690323ac95bSChris Mason  * all the proper balancing.  path->nodes[1] must be locked.
4691323ac95bSChris Mason  */
4692143bede5SJeff Mahoney static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4693323ac95bSChris Mason 				    struct btrfs_root *root,
46945d4f98a2SYan Zheng 				    struct btrfs_path *path,
46955d4f98a2SYan Zheng 				    struct extent_buffer *leaf)
4696323ac95bSChris Mason {
46975d4f98a2SYan Zheng 	WARN_ON(btrfs_header_generation(leaf) != trans->transid);
4698f3ea38daSJan Schmidt 	del_ptr(trans, root, path, 1, path->slots[1], 1);
4699323ac95bSChris Mason 
47004d081c41SChris Mason 	/*
47014d081c41SChris Mason 	 * btrfs_free_extent is expensive, we want to make sure we
47024d081c41SChris Mason 	 * aren't holding any locks when we call it
47034d081c41SChris Mason 	 */
47044d081c41SChris Mason 	btrfs_unlock_up_safe(path, 0);
47054d081c41SChris Mason 
4706f0486c68SYan, Zheng 	root_sub_used(root, leaf->len);
4707f0486c68SYan, Zheng 
47083083ee2eSJosef Bacik 	extent_buffer_get(leaf);
47095581a51aSJan Schmidt 	btrfs_free_tree_block(trans, root, leaf, 0, 1);
47103083ee2eSJosef Bacik 	free_extent_buffer_stale(leaf);
4711323ac95bSChris Mason }
4712323ac95bSChris Mason /*
471374123bd7SChris Mason  * delete the item at the leaf level in path.  If that empties
471474123bd7SChris Mason  * the leaf, remove it from the tree
471574123bd7SChris Mason  */
471685e21bacSChris Mason int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
471785e21bacSChris Mason 		    struct btrfs_path *path, int slot, int nr)
4718be0e5c09SChris Mason {
47195f39d397SChris Mason 	struct extent_buffer *leaf;
47205f39d397SChris Mason 	struct btrfs_item *item;
472185e21bacSChris Mason 	int last_off;
472285e21bacSChris Mason 	int dsize = 0;
4723aa5d6bedSChris Mason 	int ret = 0;
4724aa5d6bedSChris Mason 	int wret;
472585e21bacSChris Mason 	int i;
47267518a238SChris Mason 	u32 nritems;
4727cfed81a0SChris Mason 	struct btrfs_map_token token;
4728cfed81a0SChris Mason 
4729cfed81a0SChris Mason 	btrfs_init_map_token(&token);
4730be0e5c09SChris Mason 
47315f39d397SChris Mason 	leaf = path->nodes[0];
473285e21bacSChris Mason 	last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
473385e21bacSChris Mason 
473485e21bacSChris Mason 	for (i = 0; i < nr; i++)
473585e21bacSChris Mason 		dsize += btrfs_item_size_nr(leaf, slot + i);
473685e21bacSChris Mason 
47375f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
4738be0e5c09SChris Mason 
473985e21bacSChris Mason 	if (slot + nr != nritems) {
4740123abc88SChris Mason 		int data_end = leaf_data_end(root, leaf);
47415f39d397SChris Mason 
47425f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4743d6025579SChris Mason 			      data_end + dsize,
4744123abc88SChris Mason 			      btrfs_leaf_data(leaf) + data_end,
474585e21bacSChris Mason 			      last_off - data_end);
47465f39d397SChris Mason 
474785e21bacSChris Mason 		for (i = slot + nr; i < nritems; i++) {
47485f39d397SChris Mason 			u32 ioff;
4749db94535dSChris Mason 
47505f39d397SChris Mason 			item = btrfs_item_nr(leaf, i);
4751cfed81a0SChris Mason 			ioff = btrfs_token_item_offset(leaf, item, &token);
4752cfed81a0SChris Mason 			btrfs_set_token_item_offset(leaf, item,
4753cfed81a0SChris Mason 						    ioff + dsize, &token);
47540783fcfcSChris Mason 		}
4755db94535dSChris Mason 
47565f39d397SChris Mason 		memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
475785e21bacSChris Mason 			      btrfs_item_nr_offset(slot + nr),
47580783fcfcSChris Mason 			      sizeof(struct btrfs_item) *
475985e21bacSChris Mason 			      (nritems - slot - nr));
4760be0e5c09SChris Mason 	}
476185e21bacSChris Mason 	btrfs_set_header_nritems(leaf, nritems - nr);
476285e21bacSChris Mason 	nritems -= nr;
47635f39d397SChris Mason 
476474123bd7SChris Mason 	/* delete the leaf if we've emptied it */
47657518a238SChris Mason 	if (nritems == 0) {
47665f39d397SChris Mason 		if (leaf == root->node) {
47675f39d397SChris Mason 			btrfs_set_header_level(leaf, 0);
47689a8dd150SChris Mason 		} else {
4769f0486c68SYan, Zheng 			btrfs_set_path_blocking(path);
4770f0486c68SYan, Zheng 			clean_tree_block(trans, root, leaf);
4771143bede5SJeff Mahoney 			btrfs_del_leaf(trans, root, path, leaf);
47729a8dd150SChris Mason 		}
4773be0e5c09SChris Mason 	} else {
47747518a238SChris Mason 		int used = leaf_space_used(leaf, 0, nritems);
4775aa5d6bedSChris Mason 		if (slot == 0) {
47765f39d397SChris Mason 			struct btrfs_disk_key disk_key;
47775f39d397SChris Mason 
47785f39d397SChris Mason 			btrfs_item_key(leaf, &disk_key, 0);
4779143bede5SJeff Mahoney 			fixup_low_keys(trans, root, path, &disk_key, 1);
4780aa5d6bedSChris Mason 		}
4781aa5d6bedSChris Mason 
478274123bd7SChris Mason 		/* delete the leaf if it is mostly empty */
4783d717aa1dSYan Zheng 		if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
4784be0e5c09SChris Mason 			/* push_leaf_left fixes the path.
4785be0e5c09SChris Mason 			 * make sure the path still points to our leaf
4786be0e5c09SChris Mason 			 * for possible call to del_ptr below
4787be0e5c09SChris Mason 			 */
47884920c9acSChris Mason 			slot = path->slots[1];
47895f39d397SChris Mason 			extent_buffer_get(leaf);
47905f39d397SChris Mason 
4791b9473439SChris Mason 			btrfs_set_path_blocking(path);
479299d8f83cSChris Mason 			wret = push_leaf_left(trans, root, path, 1, 1,
479399d8f83cSChris Mason 					      1, (u32)-1);
479454aa1f4dSChris Mason 			if (wret < 0 && wret != -ENOSPC)
4795aa5d6bedSChris Mason 				ret = wret;
47965f39d397SChris Mason 
47975f39d397SChris Mason 			if (path->nodes[0] == leaf &&
47985f39d397SChris Mason 			    btrfs_header_nritems(leaf)) {
479999d8f83cSChris Mason 				wret = push_leaf_right(trans, root, path, 1,
480099d8f83cSChris Mason 						       1, 1, 0);
480154aa1f4dSChris Mason 				if (wret < 0 && wret != -ENOSPC)
4802aa5d6bedSChris Mason 					ret = wret;
4803aa5d6bedSChris Mason 			}
48045f39d397SChris Mason 
48055f39d397SChris Mason 			if (btrfs_header_nritems(leaf) == 0) {
4806323ac95bSChris Mason 				path->slots[1] = slot;
4807143bede5SJeff Mahoney 				btrfs_del_leaf(trans, root, path, leaf);
48085f39d397SChris Mason 				free_extent_buffer(leaf);
4809143bede5SJeff Mahoney 				ret = 0;
48105de08d7dSChris Mason 			} else {
4811925baeddSChris Mason 				/* if we're still in the path, make sure
4812925baeddSChris Mason 				 * we're dirty.  Otherwise, one of the
4813925baeddSChris Mason 				 * push_leaf functions must have already
4814925baeddSChris Mason 				 * dirtied this buffer
4815925baeddSChris Mason 				 */
4816925baeddSChris Mason 				if (path->nodes[0] == leaf)
48175f39d397SChris Mason 					btrfs_mark_buffer_dirty(leaf);
48185f39d397SChris Mason 				free_extent_buffer(leaf);
4819be0e5c09SChris Mason 			}
4820d5719762SChris Mason 		} else {
48215f39d397SChris Mason 			btrfs_mark_buffer_dirty(leaf);
4822be0e5c09SChris Mason 		}
48239a8dd150SChris Mason 	}
4824aa5d6bedSChris Mason 	return ret;
48259a8dd150SChris Mason }
48269a8dd150SChris Mason 
482797571fd0SChris Mason /*
4828925baeddSChris Mason  * search the tree again to find a leaf with lesser keys
48297bb86316SChris Mason  * returns 0 if it found something or 1 if there are no lesser leaves.
48307bb86316SChris Mason  * returns < 0 on io errors.
4831d352ac68SChris Mason  *
4832d352ac68SChris Mason  * This may release the path, and so you may lose any locks held at the
4833d352ac68SChris Mason  * time you call it.
48347bb86316SChris Mason  */
48357bb86316SChris Mason int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
48367bb86316SChris Mason {
4837925baeddSChris Mason 	struct btrfs_key key;
4838925baeddSChris Mason 	struct btrfs_disk_key found_key;
4839925baeddSChris Mason 	int ret;
48407bb86316SChris Mason 
4841925baeddSChris Mason 	btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
4842925baeddSChris Mason 
4843925baeddSChris Mason 	if (key.offset > 0)
4844925baeddSChris Mason 		key.offset--;
4845925baeddSChris Mason 	else if (key.type > 0)
4846925baeddSChris Mason 		key.type--;
4847925baeddSChris Mason 	else if (key.objectid > 0)
4848925baeddSChris Mason 		key.objectid--;
4849925baeddSChris Mason 	else
48507bb86316SChris Mason 		return 1;
48517bb86316SChris Mason 
4852b3b4aa74SDavid Sterba 	btrfs_release_path(path);
4853925baeddSChris Mason 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4854925baeddSChris Mason 	if (ret < 0)
4855925baeddSChris Mason 		return ret;
4856925baeddSChris Mason 	btrfs_item_key(path->nodes[0], &found_key, 0);
4857925baeddSChris Mason 	ret = comp_keys(&found_key, &key);
4858925baeddSChris Mason 	if (ret < 0)
48597bb86316SChris Mason 		return 0;
4860925baeddSChris Mason 	return 1;
48617bb86316SChris Mason }
48627bb86316SChris Mason 
48633f157a2fSChris Mason /*
48643f157a2fSChris Mason  * A helper function to walk down the tree starting at min_key, and looking
48653f157a2fSChris Mason  * for nodes or leaves that are either in cache or have a minimum
4866d352ac68SChris Mason  * transaction id.  This is used by the btree defrag code, and tree logging
48673f157a2fSChris Mason  *
48683f157a2fSChris Mason  * This does not cow, but it does stuff the starting key it finds back
48693f157a2fSChris Mason  * into min_key, so you can call btrfs_search_slot with cow=1 on the
48703f157a2fSChris Mason  * key and get a writable path.
48713f157a2fSChris Mason  *
48723f157a2fSChris Mason  * This does lock as it descends, and path->keep_locks should be set
48733f157a2fSChris Mason  * to 1 by the caller.
48743f157a2fSChris Mason  *
48753f157a2fSChris Mason  * This honors path->lowest_level to prevent descent past a given level
48763f157a2fSChris Mason  * of the tree.
48773f157a2fSChris Mason  *
4878d352ac68SChris Mason  * min_trans indicates the oldest transaction that you are interested
4879d352ac68SChris Mason  * in walking through.  Any nodes or leaves older than min_trans are
4880d352ac68SChris Mason  * skipped over (without reading them).
4881d352ac68SChris Mason  *
48823f157a2fSChris Mason  * returns zero if something useful was found, < 0 on error and 1 if there
48833f157a2fSChris Mason  * was nothing in the tree that matched the search criteria.
48843f157a2fSChris Mason  */
48853f157a2fSChris Mason int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
4886e02119d5SChris Mason 			 struct btrfs_key *max_key,
48873f157a2fSChris Mason 			 struct btrfs_path *path, int cache_only,
48883f157a2fSChris Mason 			 u64 min_trans)
48893f157a2fSChris Mason {
48903f157a2fSChris Mason 	struct extent_buffer *cur;
48913f157a2fSChris Mason 	struct btrfs_key found_key;
48923f157a2fSChris Mason 	int slot;
48939652480bSYan 	int sret;
48943f157a2fSChris Mason 	u32 nritems;
48953f157a2fSChris Mason 	int level;
48963f157a2fSChris Mason 	int ret = 1;
48973f157a2fSChris Mason 
4898934d375bSChris Mason 	WARN_ON(!path->keep_locks);
48993f157a2fSChris Mason again:
4900bd681513SChris Mason 	cur = btrfs_read_lock_root_node(root);
49013f157a2fSChris Mason 	level = btrfs_header_level(cur);
4902e02119d5SChris Mason 	WARN_ON(path->nodes[level]);
49033f157a2fSChris Mason 	path->nodes[level] = cur;
4904bd681513SChris Mason 	path->locks[level] = BTRFS_READ_LOCK;
49053f157a2fSChris Mason 
49063f157a2fSChris Mason 	if (btrfs_header_generation(cur) < min_trans) {
49073f157a2fSChris Mason 		ret = 1;
49083f157a2fSChris Mason 		goto out;
49093f157a2fSChris Mason 	}
49103f157a2fSChris Mason 	while (1) {
49113f157a2fSChris Mason 		nritems = btrfs_header_nritems(cur);
49123f157a2fSChris Mason 		level = btrfs_header_level(cur);
49139652480bSYan 		sret = bin_search(cur, min_key, level, &slot);
49143f157a2fSChris Mason 
4915323ac95bSChris Mason 		/* at the lowest level, we're done, setup the path and exit */
4916323ac95bSChris Mason 		if (level == path->lowest_level) {
4917e02119d5SChris Mason 			if (slot >= nritems)
4918e02119d5SChris Mason 				goto find_next_key;
49193f157a2fSChris Mason 			ret = 0;
49203f157a2fSChris Mason 			path->slots[level] = slot;
49213f157a2fSChris Mason 			btrfs_item_key_to_cpu(cur, &found_key, slot);
49223f157a2fSChris Mason 			goto out;
49233f157a2fSChris Mason 		}
49249652480bSYan 		if (sret && slot > 0)
49259652480bSYan 			slot--;
49263f157a2fSChris Mason 		/*
49273f157a2fSChris Mason 		 * check this node pointer against the cache_only and
49283f157a2fSChris Mason 		 * min_trans parameters.  If it isn't in cache or is too
49293f157a2fSChris Mason 		 * old, skip to the next one.
49303f157a2fSChris Mason 		 */
49313f157a2fSChris Mason 		while (slot < nritems) {
49323f157a2fSChris Mason 			u64 blockptr;
49333f157a2fSChris Mason 			u64 gen;
49343f157a2fSChris Mason 			struct extent_buffer *tmp;
4935e02119d5SChris Mason 			struct btrfs_disk_key disk_key;
4936e02119d5SChris Mason 
49373f157a2fSChris Mason 			blockptr = btrfs_node_blockptr(cur, slot);
49383f157a2fSChris Mason 			gen = btrfs_node_ptr_generation(cur, slot);
49393f157a2fSChris Mason 			if (gen < min_trans) {
49403f157a2fSChris Mason 				slot++;
49413f157a2fSChris Mason 				continue;
49423f157a2fSChris Mason 			}
49433f157a2fSChris Mason 			if (!cache_only)
49443f157a2fSChris Mason 				break;
49453f157a2fSChris Mason 
4946e02119d5SChris Mason 			if (max_key) {
4947e02119d5SChris Mason 				btrfs_node_key(cur, &disk_key, slot);
4948e02119d5SChris Mason 				if (comp_keys(&disk_key, max_key) >= 0) {
4949e02119d5SChris Mason 					ret = 1;
4950e02119d5SChris Mason 					goto out;
4951e02119d5SChris Mason 				}
4952e02119d5SChris Mason 			}
4953e02119d5SChris Mason 
49543f157a2fSChris Mason 			tmp = btrfs_find_tree_block(root, blockptr,
49553f157a2fSChris Mason 					    btrfs_level_size(root, level - 1));
49563f157a2fSChris Mason 
4957b9fab919SChris Mason 			if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
49583f157a2fSChris Mason 				free_extent_buffer(tmp);
49593f157a2fSChris Mason 				break;
49603f157a2fSChris Mason 			}
49613f157a2fSChris Mason 			if (tmp)
49623f157a2fSChris Mason 				free_extent_buffer(tmp);
49633f157a2fSChris Mason 			slot++;
49643f157a2fSChris Mason 		}
4965e02119d5SChris Mason find_next_key:
49663f157a2fSChris Mason 		/*
49673f157a2fSChris Mason 		 * we didn't find a candidate key in this node, walk forward
49683f157a2fSChris Mason 		 * and find another one
49693f157a2fSChris Mason 		 */
49703f157a2fSChris Mason 		if (slot >= nritems) {
4971e02119d5SChris Mason 			path->slots[level] = slot;
4972b4ce94deSChris Mason 			btrfs_set_path_blocking(path);
4973e02119d5SChris Mason 			sret = btrfs_find_next_key(root, path, min_key, level,
49743f157a2fSChris Mason 						  cache_only, min_trans);
4975e02119d5SChris Mason 			if (sret == 0) {
4976b3b4aa74SDavid Sterba 				btrfs_release_path(path);
49773f157a2fSChris Mason 				goto again;
49783f157a2fSChris Mason 			} else {
49793f157a2fSChris Mason 				goto out;
49803f157a2fSChris Mason 			}
49813f157a2fSChris Mason 		}
49823f157a2fSChris Mason 		/* save our key for returning back */
49833f157a2fSChris Mason 		btrfs_node_key_to_cpu(cur, &found_key, slot);
49843f157a2fSChris Mason 		path->slots[level] = slot;
49853f157a2fSChris Mason 		if (level == path->lowest_level) {
49863f157a2fSChris Mason 			ret = 0;
4987f7c79f30SChris Mason 			unlock_up(path, level, 1, 0, NULL);
49883f157a2fSChris Mason 			goto out;
49893f157a2fSChris Mason 		}
4990b4ce94deSChris Mason 		btrfs_set_path_blocking(path);
49913f157a2fSChris Mason 		cur = read_node_slot(root, cur, slot);
499279787eaaSJeff Mahoney 		BUG_ON(!cur); /* -ENOMEM */
49933f157a2fSChris Mason 
4994bd681513SChris Mason 		btrfs_tree_read_lock(cur);
4995b4ce94deSChris Mason 
4996bd681513SChris Mason 		path->locks[level - 1] = BTRFS_READ_LOCK;
49973f157a2fSChris Mason 		path->nodes[level - 1] = cur;
4998f7c79f30SChris Mason 		unlock_up(path, level, 1, 0, NULL);
4999bd681513SChris Mason 		btrfs_clear_path_blocking(path, NULL, 0);
50003f157a2fSChris Mason 	}
50013f157a2fSChris Mason out:
50023f157a2fSChris Mason 	if (ret == 0)
50033f157a2fSChris Mason 		memcpy(min_key, &found_key, sizeof(found_key));
5004b4ce94deSChris Mason 	btrfs_set_path_blocking(path);
50053f157a2fSChris Mason 	return ret;
50063f157a2fSChris Mason }
50073f157a2fSChris Mason 
50083f157a2fSChris Mason /*
50093f157a2fSChris Mason  * this is similar to btrfs_next_leaf, but does not try to preserve
50103f157a2fSChris Mason  * and fixup the path.  It looks for and returns the next key in the
50113f157a2fSChris Mason  * tree based on the current path and the cache_only and min_trans
50123f157a2fSChris Mason  * parameters.
50133f157a2fSChris Mason  *
50143f157a2fSChris Mason  * 0 is returned if another key is found, < 0 if there are any errors
50153f157a2fSChris Mason  * and 1 is returned if there are no higher keys in the tree
50163f157a2fSChris Mason  *
50173f157a2fSChris Mason  * path->keep_locks should be set to 1 on the search made before
50183f157a2fSChris Mason  * calling this function.
50193f157a2fSChris Mason  */
5020e7a84565SChris Mason int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
502133c66f43SYan Zheng 			struct btrfs_key *key, int level,
50223f157a2fSChris Mason 			int cache_only, u64 min_trans)
5023e7a84565SChris Mason {
5024e7a84565SChris Mason 	int slot;
5025e7a84565SChris Mason 	struct extent_buffer *c;
5026e7a84565SChris Mason 
5027934d375bSChris Mason 	WARN_ON(!path->keep_locks);
5028e7a84565SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
5029e7a84565SChris Mason 		if (!path->nodes[level])
5030e7a84565SChris Mason 			return 1;
5031e7a84565SChris Mason 
5032e7a84565SChris Mason 		slot = path->slots[level] + 1;
5033e7a84565SChris Mason 		c = path->nodes[level];
50343f157a2fSChris Mason next:
5035e7a84565SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
503633c66f43SYan Zheng 			int ret;
503733c66f43SYan Zheng 			int orig_lowest;
503833c66f43SYan Zheng 			struct btrfs_key cur_key;
503933c66f43SYan Zheng 			if (level + 1 >= BTRFS_MAX_LEVEL ||
504033c66f43SYan Zheng 			    !path->nodes[level + 1])
5041e7a84565SChris Mason 				return 1;
504233c66f43SYan Zheng 
504333c66f43SYan Zheng 			if (path->locks[level + 1]) {
504433c66f43SYan Zheng 				level++;
5045e7a84565SChris Mason 				continue;
5046e7a84565SChris Mason 			}
504733c66f43SYan Zheng 
504833c66f43SYan Zheng 			slot = btrfs_header_nritems(c) - 1;
504933c66f43SYan Zheng 			if (level == 0)
505033c66f43SYan Zheng 				btrfs_item_key_to_cpu(c, &cur_key, slot);
505133c66f43SYan Zheng 			else
505233c66f43SYan Zheng 				btrfs_node_key_to_cpu(c, &cur_key, slot);
505333c66f43SYan Zheng 
505433c66f43SYan Zheng 			orig_lowest = path->lowest_level;
5055b3b4aa74SDavid Sterba 			btrfs_release_path(path);
505633c66f43SYan Zheng 			path->lowest_level = level;
505733c66f43SYan Zheng 			ret = btrfs_search_slot(NULL, root, &cur_key, path,
505833c66f43SYan Zheng 						0, 0);
505933c66f43SYan Zheng 			path->lowest_level = orig_lowest;
506033c66f43SYan Zheng 			if (ret < 0)
506133c66f43SYan Zheng 				return ret;
506233c66f43SYan Zheng 
506333c66f43SYan Zheng 			c = path->nodes[level];
506433c66f43SYan Zheng 			slot = path->slots[level];
506533c66f43SYan Zheng 			if (ret == 0)
506633c66f43SYan Zheng 				slot++;
506733c66f43SYan Zheng 			goto next;
506833c66f43SYan Zheng 		}
506933c66f43SYan Zheng 
5070e7a84565SChris Mason 		if (level == 0)
5071e7a84565SChris Mason 			btrfs_item_key_to_cpu(c, key, slot);
50723f157a2fSChris Mason 		else {
50733f157a2fSChris Mason 			u64 blockptr = btrfs_node_blockptr(c, slot);
50743f157a2fSChris Mason 			u64 gen = btrfs_node_ptr_generation(c, slot);
50753f157a2fSChris Mason 
50763f157a2fSChris Mason 			if (cache_only) {
50773f157a2fSChris Mason 				struct extent_buffer *cur;
50783f157a2fSChris Mason 				cur = btrfs_find_tree_block(root, blockptr,
50793f157a2fSChris Mason 					    btrfs_level_size(root, level - 1));
5080b9fab919SChris Mason 				if (!cur ||
5081b9fab919SChris Mason 				    btrfs_buffer_uptodate(cur, gen, 1) <= 0) {
50823f157a2fSChris Mason 					slot++;
50833f157a2fSChris Mason 					if (cur)
50843f157a2fSChris Mason 						free_extent_buffer(cur);
50853f157a2fSChris Mason 					goto next;
50863f157a2fSChris Mason 				}
50873f157a2fSChris Mason 				free_extent_buffer(cur);
50883f157a2fSChris Mason 			}
50893f157a2fSChris Mason 			if (gen < min_trans) {
50903f157a2fSChris Mason 				slot++;
50913f157a2fSChris Mason 				goto next;
50923f157a2fSChris Mason 			}
5093e7a84565SChris Mason 			btrfs_node_key_to_cpu(c, key, slot);
50943f157a2fSChris Mason 		}
5095e7a84565SChris Mason 		return 0;
5096e7a84565SChris Mason 	}
5097e7a84565SChris Mason 	return 1;
5098e7a84565SChris Mason }
5099e7a84565SChris Mason 
51007bb86316SChris Mason /*
5101925baeddSChris Mason  * search the tree again to find a leaf with greater keys
51020f70abe2SChris Mason  * returns 0 if it found something or 1 if there are no greater leaves.
51030f70abe2SChris Mason  * returns < 0 on io errors.
510497571fd0SChris Mason  */
5105234b63a0SChris Mason int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
5106d97e63b6SChris Mason {
51073d7806ecSJan Schmidt 	return btrfs_next_old_leaf(root, path, 0);
51083d7806ecSJan Schmidt }
51093d7806ecSJan Schmidt 
51103d7806ecSJan Schmidt int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
51113d7806ecSJan Schmidt 			u64 time_seq)
51123d7806ecSJan Schmidt {
5113d97e63b6SChris Mason 	int slot;
51148e73f275SChris Mason 	int level;
51155f39d397SChris Mason 	struct extent_buffer *c;
51168e73f275SChris Mason 	struct extent_buffer *next;
5117925baeddSChris Mason 	struct btrfs_key key;
5118925baeddSChris Mason 	u32 nritems;
5119925baeddSChris Mason 	int ret;
51208e73f275SChris Mason 	int old_spinning = path->leave_spinning;
5121bd681513SChris Mason 	int next_rw_lock = 0;
5122925baeddSChris Mason 
5123925baeddSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
5124d397712bSChris Mason 	if (nritems == 0)
5125925baeddSChris Mason 		return 1;
5126925baeddSChris Mason 
51278e73f275SChris Mason 	btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
51288e73f275SChris Mason again:
51298e73f275SChris Mason 	level = 1;
51308e73f275SChris Mason 	next = NULL;
5131bd681513SChris Mason 	next_rw_lock = 0;
5132b3b4aa74SDavid Sterba 	btrfs_release_path(path);
51338e73f275SChris Mason 
5134a2135011SChris Mason 	path->keep_locks = 1;
51358e73f275SChris Mason 	path->leave_spinning = 1;
51368e73f275SChris Mason 
51373d7806ecSJan Schmidt 	if (time_seq)
51383d7806ecSJan Schmidt 		ret = btrfs_search_old_slot(root, &key, path, time_seq);
51393d7806ecSJan Schmidt 	else
5140925baeddSChris Mason 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5141925baeddSChris Mason 	path->keep_locks = 0;
5142925baeddSChris Mason 
5143925baeddSChris Mason 	if (ret < 0)
5144925baeddSChris Mason 		return ret;
5145925baeddSChris Mason 
5146a2135011SChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
5147168fd7d2SChris Mason 	/*
5148168fd7d2SChris Mason 	 * by releasing the path above we dropped all our locks.  A balance
5149168fd7d2SChris Mason 	 * could have added more items next to the key that used to be
5150168fd7d2SChris Mason 	 * at the very end of the block.  So, check again here and
5151168fd7d2SChris Mason 	 * advance the path if there are now more items available.
5152168fd7d2SChris Mason 	 */
5153a2135011SChris Mason 	if (nritems > 0 && path->slots[0] < nritems - 1) {
5154e457afecSYan Zheng 		if (ret == 0)
5155168fd7d2SChris Mason 			path->slots[0]++;
51568e73f275SChris Mason 		ret = 0;
5157925baeddSChris Mason 		goto done;
5158925baeddSChris Mason 	}
5159d97e63b6SChris Mason 
5160234b63a0SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
51618e73f275SChris Mason 		if (!path->nodes[level]) {
51628e73f275SChris Mason 			ret = 1;
51638e73f275SChris Mason 			goto done;
51648e73f275SChris Mason 		}
51655f39d397SChris Mason 
5166d97e63b6SChris Mason 		slot = path->slots[level] + 1;
5167d97e63b6SChris Mason 		c = path->nodes[level];
51685f39d397SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
5169d97e63b6SChris Mason 			level++;
51708e73f275SChris Mason 			if (level == BTRFS_MAX_LEVEL) {
51718e73f275SChris Mason 				ret = 1;
51728e73f275SChris Mason 				goto done;
51738e73f275SChris Mason 			}
5174d97e63b6SChris Mason 			continue;
5175d97e63b6SChris Mason 		}
51765f39d397SChris Mason 
5177925baeddSChris Mason 		if (next) {
5178bd681513SChris Mason 			btrfs_tree_unlock_rw(next, next_rw_lock);
51795f39d397SChris Mason 			free_extent_buffer(next);
5180925baeddSChris Mason 		}
51815f39d397SChris Mason 
51828e73f275SChris Mason 		next = c;
5183bd681513SChris Mason 		next_rw_lock = path->locks[level];
51848e73f275SChris Mason 		ret = read_block_for_search(NULL, root, path, &next, level,
51855d9e75c4SJan Schmidt 					    slot, &key, 0);
51868e73f275SChris Mason 		if (ret == -EAGAIN)
51878e73f275SChris Mason 			goto again;
51885f39d397SChris Mason 
518976a05b35SChris Mason 		if (ret < 0) {
5190b3b4aa74SDavid Sterba 			btrfs_release_path(path);
519176a05b35SChris Mason 			goto done;
519276a05b35SChris Mason 		}
519376a05b35SChris Mason 
51945cd57b2cSChris Mason 		if (!path->skip_locking) {
5195bd681513SChris Mason 			ret = btrfs_try_tree_read_lock(next);
5196d42244a0SJan Schmidt 			if (!ret && time_seq) {
5197d42244a0SJan Schmidt 				/*
5198d42244a0SJan Schmidt 				 * If we don't get the lock, we may be racing
5199d42244a0SJan Schmidt 				 * with push_leaf_left, holding that lock while
5200d42244a0SJan Schmidt 				 * itself waiting for the leaf we've currently
5201d42244a0SJan Schmidt 				 * locked. To solve this situation, we give up
5202d42244a0SJan Schmidt 				 * on our lock and cycle.
5203d42244a0SJan Schmidt 				 */
5204d42244a0SJan Schmidt 				btrfs_release_path(path);
5205d42244a0SJan Schmidt 				cond_resched();
5206d42244a0SJan Schmidt 				goto again;
5207d42244a0SJan Schmidt 			}
52088e73f275SChris Mason 			if (!ret) {
52098e73f275SChris Mason 				btrfs_set_path_blocking(path);
5210bd681513SChris Mason 				btrfs_tree_read_lock(next);
5211bd681513SChris Mason 				btrfs_clear_path_blocking(path, next,
5212bd681513SChris Mason 							  BTRFS_READ_LOCK);
52138e73f275SChris Mason 			}
5214bd681513SChris Mason 			next_rw_lock = BTRFS_READ_LOCK;
5215bd681513SChris Mason 		}
5216d97e63b6SChris Mason 		break;
5217d97e63b6SChris Mason 	}
5218d97e63b6SChris Mason 	path->slots[level] = slot;
5219d97e63b6SChris Mason 	while (1) {
5220d97e63b6SChris Mason 		level--;
5221d97e63b6SChris Mason 		c = path->nodes[level];
5222925baeddSChris Mason 		if (path->locks[level])
5223bd681513SChris Mason 			btrfs_tree_unlock_rw(c, path->locks[level]);
52248e73f275SChris Mason 
52255f39d397SChris Mason 		free_extent_buffer(c);
5226d97e63b6SChris Mason 		path->nodes[level] = next;
5227d97e63b6SChris Mason 		path->slots[level] = 0;
5228a74a4b97SChris Mason 		if (!path->skip_locking)
5229bd681513SChris Mason 			path->locks[level] = next_rw_lock;
5230d97e63b6SChris Mason 		if (!level)
5231d97e63b6SChris Mason 			break;
5232b4ce94deSChris Mason 
52338e73f275SChris Mason 		ret = read_block_for_search(NULL, root, path, &next, level,
52345d9e75c4SJan Schmidt 					    0, &key, 0);
52358e73f275SChris Mason 		if (ret == -EAGAIN)
52368e73f275SChris Mason 			goto again;
52378e73f275SChris Mason 
523876a05b35SChris Mason 		if (ret < 0) {
5239b3b4aa74SDavid Sterba 			btrfs_release_path(path);
524076a05b35SChris Mason 			goto done;
524176a05b35SChris Mason 		}
524276a05b35SChris Mason 
52435cd57b2cSChris Mason 		if (!path->skip_locking) {
5244bd681513SChris Mason 			ret = btrfs_try_tree_read_lock(next);
52458e73f275SChris Mason 			if (!ret) {
52468e73f275SChris Mason 				btrfs_set_path_blocking(path);
5247bd681513SChris Mason 				btrfs_tree_read_lock(next);
5248bd681513SChris Mason 				btrfs_clear_path_blocking(path, next,
5249bd681513SChris Mason 							  BTRFS_READ_LOCK);
52508e73f275SChris Mason 			}
5251bd681513SChris Mason 			next_rw_lock = BTRFS_READ_LOCK;
5252bd681513SChris Mason 		}
5253d97e63b6SChris Mason 	}
52548e73f275SChris Mason 	ret = 0;
5255925baeddSChris Mason done:
5256f7c79f30SChris Mason 	unlock_up(path, 0, 1, 0, NULL);
52578e73f275SChris Mason 	path->leave_spinning = old_spinning;
52588e73f275SChris Mason 	if (!old_spinning)
52598e73f275SChris Mason 		btrfs_set_path_blocking(path);
52608e73f275SChris Mason 
52618e73f275SChris Mason 	return ret;
5262d97e63b6SChris Mason }
52630b86a832SChris Mason 
52643f157a2fSChris Mason /*
52653f157a2fSChris Mason  * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
52663f157a2fSChris Mason  * searching until it gets past min_objectid or finds an item of 'type'
52673f157a2fSChris Mason  *
52683f157a2fSChris Mason  * returns 0 if something is found, 1 if nothing was found and < 0 on error
52693f157a2fSChris Mason  */
52700b86a832SChris Mason int btrfs_previous_item(struct btrfs_root *root,
52710b86a832SChris Mason 			struct btrfs_path *path, u64 min_objectid,
52720b86a832SChris Mason 			int type)
52730b86a832SChris Mason {
52740b86a832SChris Mason 	struct btrfs_key found_key;
52750b86a832SChris Mason 	struct extent_buffer *leaf;
5276e02119d5SChris Mason 	u32 nritems;
52770b86a832SChris Mason 	int ret;
52780b86a832SChris Mason 
52790b86a832SChris Mason 	while (1) {
52800b86a832SChris Mason 		if (path->slots[0] == 0) {
5281b4ce94deSChris Mason 			btrfs_set_path_blocking(path);
52820b86a832SChris Mason 			ret = btrfs_prev_leaf(root, path);
52830b86a832SChris Mason 			if (ret != 0)
52840b86a832SChris Mason 				return ret;
52850b86a832SChris Mason 		} else {
52860b86a832SChris Mason 			path->slots[0]--;
52870b86a832SChris Mason 		}
52880b86a832SChris Mason 		leaf = path->nodes[0];
5289e02119d5SChris Mason 		nritems = btrfs_header_nritems(leaf);
5290e02119d5SChris Mason 		if (nritems == 0)
5291e02119d5SChris Mason 			return 1;
5292e02119d5SChris Mason 		if (path->slots[0] == nritems)
5293e02119d5SChris Mason 			path->slots[0]--;
5294e02119d5SChris Mason 
52950b86a832SChris Mason 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5296e02119d5SChris Mason 		if (found_key.objectid < min_objectid)
5297e02119d5SChris Mason 			break;
52980a4eefbbSYan Zheng 		if (found_key.type == type)
52990a4eefbbSYan Zheng 			return 0;
5300e02119d5SChris Mason 		if (found_key.objectid == min_objectid &&
5301e02119d5SChris Mason 		    found_key.type < type)
5302e02119d5SChris Mason 			break;
53030b86a832SChris Mason 	}
53040b86a832SChris Mason 	return 1;
53050b86a832SChris Mason }
5306