xref: /openbmc/linux/fs/btrfs/ctree.c (revision eb96e221937af3c7bb8a63208dbab813ca5d3d7e)
1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
26cbd5570SChris Mason /*
3d352ac68SChris Mason  * Copyright (C) 2007,2008 Oracle.  All rights reserved.
46cbd5570SChris Mason  */
56cbd5570SChris Mason 
6a6b6e75eSChris Mason #include <linux/sched.h>
75a0e3ad6STejun Heo #include <linux/slab.h>
8bd989ba3SJan Schmidt #include <linux/rbtree.h>
9adf02123SDavid Sterba #include <linux/mm.h>
10e41d12f5SChristoph Hellwig #include <linux/error-injection.h>
119b569ea0SJosef Bacik #include "messages.h"
12eb60ceacSChris Mason #include "ctree.h"
13eb60ceacSChris Mason #include "disk-io.h"
147f5c1516SChris Mason #include "transaction.h"
155f39d397SChris Mason #include "print-tree.h"
16925baeddSChris Mason #include "locking.h"
17de37aa51SNikolay Borisov #include "volumes.h"
18f616f5cdSQu Wenruo #include "qgroup.h"
19f3a84ccdSFilipe Manana #include "tree-mod-log.h"
2088c602abSQu Wenruo #include "tree-checker.h"
21ec8eb376SJosef Bacik #include "fs.h"
22ad1ac501SJosef Bacik #include "accessors.h"
23a0231804SJosef Bacik #include "extent-tree.h"
2467707479SJosef Bacik #include "relocation.h"
256bfd0ffaSJosef Bacik #include "file-item.h"
269a8dd150SChris Mason 
27226463d7SJosef Bacik static struct kmem_cache *btrfs_path_cachep;
28226463d7SJosef Bacik 
29e089f05cSChris Mason static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
30e089f05cSChris Mason 		      *root, struct btrfs_path *path, int level);
31310712b2SOmar Sandoval static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
32310712b2SOmar Sandoval 		      const struct btrfs_key *ins_key, struct btrfs_path *path,
33310712b2SOmar Sandoval 		      int data_size, int extend);
345f39d397SChris Mason static int push_node_left(struct btrfs_trans_handle *trans,
352ff7e61eSJeff Mahoney 			  struct extent_buffer *dst,
36971a1f66SChris Mason 			  struct extent_buffer *src, int empty);
375f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans,
385f39d397SChris Mason 			      struct extent_buffer *dst_buf,
395f39d397SChris Mason 			      struct extent_buffer *src_buf);
40d97e63b6SChris Mason 
41af024ed2SJohannes Thumshirn static const struct btrfs_csums {
42af024ed2SJohannes Thumshirn 	u16		size;
4359a0fcdbSDavid Sterba 	const char	name[10];
4459a0fcdbSDavid Sterba 	const char	driver[12];
45af024ed2SJohannes Thumshirn } btrfs_csums[] = {
46af024ed2SJohannes Thumshirn 	[BTRFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" },
473951e7f0SJohannes Thumshirn 	[BTRFS_CSUM_TYPE_XXHASH] = { .size = 8, .name = "xxhash64" },
483831bf00SJohannes Thumshirn 	[BTRFS_CSUM_TYPE_SHA256] = { .size = 32, .name = "sha256" },
49352ae07bSDavid Sterba 	[BTRFS_CSUM_TYPE_BLAKE2] = { .size = 32, .name = "blake2b",
50352ae07bSDavid Sterba 				     .driver = "blake2b-256" },
51af024ed2SJohannes Thumshirn };
52af024ed2SJohannes Thumshirn 
533a3178c7SJosef Bacik /*
543a3178c7SJosef Bacik  * The leaf data grows from end-to-front in the node.  this returns the address
553a3178c7SJosef Bacik  * of the start of the last item, which is the stop of the leaf data stack.
563a3178c7SJosef Bacik  */
573a3178c7SJosef Bacik static unsigned int leaf_data_end(const struct extent_buffer *leaf)
583a3178c7SJosef Bacik {
593a3178c7SJosef Bacik 	u32 nr = btrfs_header_nritems(leaf);
603a3178c7SJosef Bacik 
613a3178c7SJosef Bacik 	if (nr == 0)
623a3178c7SJosef Bacik 		return BTRFS_LEAF_DATA_SIZE(leaf->fs_info);
633a3178c7SJosef Bacik 	return btrfs_item_offset(leaf, nr - 1);
643a3178c7SJosef Bacik }
653a3178c7SJosef Bacik 
66637e3b48SJosef Bacik /*
67637e3b48SJosef Bacik  * Move data in a @leaf (using memmove, safe for overlapping ranges).
68637e3b48SJosef Bacik  *
69637e3b48SJosef Bacik  * @leaf:	leaf that we're doing a memmove on
70637e3b48SJosef Bacik  * @dst_offset:	item data offset we're moving to
71637e3b48SJosef Bacik  * @src_offset:	item data offset were' moving from
72637e3b48SJosef Bacik  * @len:	length of the data we're moving
73637e3b48SJosef Bacik  *
74637e3b48SJosef Bacik  * Wrapper around memmove_extent_buffer() that takes into account the header on
75637e3b48SJosef Bacik  * the leaf.  The btrfs_item offset's start directly after the header, so we
76637e3b48SJosef Bacik  * have to adjust any offsets to account for the header in the leaf.  This
77637e3b48SJosef Bacik  * handles that math to simplify the callers.
78637e3b48SJosef Bacik  */
79637e3b48SJosef Bacik static inline void memmove_leaf_data(const struct extent_buffer *leaf,
80637e3b48SJosef Bacik 				     unsigned long dst_offset,
81637e3b48SJosef Bacik 				     unsigned long src_offset,
82637e3b48SJosef Bacik 				     unsigned long len)
83637e3b48SJosef Bacik {
848009adf3SJosef Bacik 	memmove_extent_buffer(leaf, btrfs_item_nr_offset(leaf, 0) + dst_offset,
858009adf3SJosef Bacik 			      btrfs_item_nr_offset(leaf, 0) + src_offset, len);
86637e3b48SJosef Bacik }
87637e3b48SJosef Bacik 
88637e3b48SJosef Bacik /*
89637e3b48SJosef Bacik  * Copy item data from @src into @dst at the given @offset.
90637e3b48SJosef Bacik  *
91637e3b48SJosef Bacik  * @dst:	destination leaf that we're copying into
92637e3b48SJosef Bacik  * @src:	source leaf that we're copying from
93637e3b48SJosef Bacik  * @dst_offset:	item data offset we're copying to
94637e3b48SJosef Bacik  * @src_offset:	item data offset were' copying from
95637e3b48SJosef Bacik  * @len:	length of the data we're copying
96637e3b48SJosef Bacik  *
97637e3b48SJosef Bacik  * Wrapper around copy_extent_buffer() that takes into account the header on
98637e3b48SJosef Bacik  * the leaf.  The btrfs_item offset's start directly after the header, so we
99637e3b48SJosef Bacik  * have to adjust any offsets to account for the header in the leaf.  This
100637e3b48SJosef Bacik  * handles that math to simplify the callers.
101637e3b48SJosef Bacik  */
102637e3b48SJosef Bacik static inline void copy_leaf_data(const struct extent_buffer *dst,
103637e3b48SJosef Bacik 				  const struct extent_buffer *src,
104637e3b48SJosef Bacik 				  unsigned long dst_offset,
105637e3b48SJosef Bacik 				  unsigned long src_offset, unsigned long len)
106637e3b48SJosef Bacik {
1078009adf3SJosef Bacik 	copy_extent_buffer(dst, src, btrfs_item_nr_offset(dst, 0) + dst_offset,
1088009adf3SJosef Bacik 			   btrfs_item_nr_offset(src, 0) + src_offset, len);
109637e3b48SJosef Bacik }
110637e3b48SJosef Bacik 
111637e3b48SJosef Bacik /*
112637e3b48SJosef Bacik  * Move items in a @leaf (using memmove).
113637e3b48SJosef Bacik  *
114637e3b48SJosef Bacik  * @dst:	destination leaf for the items
115637e3b48SJosef Bacik  * @dst_item:	the item nr we're copying into
116637e3b48SJosef Bacik  * @src_item:	the item nr we're copying from
117637e3b48SJosef Bacik  * @nr_items:	the number of items to copy
118637e3b48SJosef Bacik  *
119637e3b48SJosef Bacik  * Wrapper around memmove_extent_buffer() that does the math to get the
120637e3b48SJosef Bacik  * appropriate offsets into the leaf from the item numbers.
121637e3b48SJosef Bacik  */
122637e3b48SJosef Bacik static inline void memmove_leaf_items(const struct extent_buffer *leaf,
123637e3b48SJosef Bacik 				      int dst_item, int src_item, int nr_items)
124637e3b48SJosef Bacik {
125637e3b48SJosef Bacik 	memmove_extent_buffer(leaf, btrfs_item_nr_offset(leaf, dst_item),
126637e3b48SJosef Bacik 			      btrfs_item_nr_offset(leaf, src_item),
127637e3b48SJosef Bacik 			      nr_items * sizeof(struct btrfs_item));
128637e3b48SJosef Bacik }
129637e3b48SJosef Bacik 
130637e3b48SJosef Bacik /*
131637e3b48SJosef Bacik  * Copy items from @src into @dst at the given @offset.
132637e3b48SJosef Bacik  *
133637e3b48SJosef Bacik  * @dst:	destination leaf for the items
134637e3b48SJosef Bacik  * @src:	source leaf for the items
135637e3b48SJosef Bacik  * @dst_item:	the item nr we're copying into
136637e3b48SJosef Bacik  * @src_item:	the item nr we're copying from
137637e3b48SJosef Bacik  * @nr_items:	the number of items to copy
138637e3b48SJosef Bacik  *
139637e3b48SJosef Bacik  * Wrapper around copy_extent_buffer() that does the math to get the
140637e3b48SJosef Bacik  * appropriate offsets into the leaf from the item numbers.
141637e3b48SJosef Bacik  */
142637e3b48SJosef Bacik static inline void copy_leaf_items(const struct extent_buffer *dst,
143637e3b48SJosef Bacik 				   const struct extent_buffer *src,
144637e3b48SJosef Bacik 				   int dst_item, int src_item, int nr_items)
145637e3b48SJosef Bacik {
146637e3b48SJosef Bacik 	copy_extent_buffer(dst, src, btrfs_item_nr_offset(dst, dst_item),
147637e3b48SJosef Bacik 			      btrfs_item_nr_offset(src, src_item),
148637e3b48SJosef Bacik 			      nr_items * sizeof(struct btrfs_item));
149637e3b48SJosef Bacik }
150637e3b48SJosef Bacik 
151b3cbfb0dSJosef Bacik /* This exists for btrfs-progs usages. */
152b3cbfb0dSJosef Bacik u16 btrfs_csum_type_size(u16 type)
153b3cbfb0dSJosef Bacik {
154b3cbfb0dSJosef Bacik 	return btrfs_csums[type].size;
155b3cbfb0dSJosef Bacik }
156b3cbfb0dSJosef Bacik 
157af024ed2SJohannes Thumshirn int btrfs_super_csum_size(const struct btrfs_super_block *s)
158af024ed2SJohannes Thumshirn {
159af024ed2SJohannes Thumshirn 	u16 t = btrfs_super_csum_type(s);
160af024ed2SJohannes Thumshirn 	/*
161af024ed2SJohannes Thumshirn 	 * csum type is validated at mount time
162af024ed2SJohannes Thumshirn 	 */
163b3cbfb0dSJosef Bacik 	return btrfs_csum_type_size(t);
164af024ed2SJohannes Thumshirn }
165af024ed2SJohannes Thumshirn 
166af024ed2SJohannes Thumshirn const char *btrfs_super_csum_name(u16 csum_type)
167af024ed2SJohannes Thumshirn {
168af024ed2SJohannes Thumshirn 	/* csum type is validated at mount time */
169af024ed2SJohannes Thumshirn 	return btrfs_csums[csum_type].name;
170af024ed2SJohannes Thumshirn }
171af024ed2SJohannes Thumshirn 
172b4e967beSDavid Sterba /*
173b4e967beSDavid Sterba  * Return driver name if defined, otherwise the name that's also a valid driver
174b4e967beSDavid Sterba  * name
175b4e967beSDavid Sterba  */
176b4e967beSDavid Sterba const char *btrfs_super_csum_driver(u16 csum_type)
177b4e967beSDavid Sterba {
178b4e967beSDavid Sterba 	/* csum type is validated at mount time */
17959a0fcdbSDavid Sterba 	return btrfs_csums[csum_type].driver[0] ?
18059a0fcdbSDavid Sterba 		btrfs_csums[csum_type].driver :
181b4e967beSDavid Sterba 		btrfs_csums[csum_type].name;
182b4e967beSDavid Sterba }
183b4e967beSDavid Sterba 
184604997b4SDavid Sterba size_t __attribute_const__ btrfs_get_num_csums(void)
185f7cea56cSDavid Sterba {
186f7cea56cSDavid Sterba 	return ARRAY_SIZE(btrfs_csums);
187f7cea56cSDavid Sterba }
188f7cea56cSDavid Sterba 
1892c90e5d6SChris Mason struct btrfs_path *btrfs_alloc_path(void)
1902c90e5d6SChris Mason {
191a4c853afSChenXiaoSong 	might_sleep();
192a4c853afSChenXiaoSong 
193e2c89907SMasahiro Yamada 	return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
1942c90e5d6SChris Mason }
1952c90e5d6SChris Mason 
196d352ac68SChris Mason /* this also releases the path */
1972c90e5d6SChris Mason void btrfs_free_path(struct btrfs_path *p)
1982c90e5d6SChris Mason {
199ff175d57SJesper Juhl 	if (!p)
200ff175d57SJesper Juhl 		return;
201b3b4aa74SDavid Sterba 	btrfs_release_path(p);
2022c90e5d6SChris Mason 	kmem_cache_free(btrfs_path_cachep, p);
2032c90e5d6SChris Mason }
2042c90e5d6SChris Mason 
205d352ac68SChris Mason /*
206d352ac68SChris Mason  * path release drops references on the extent buffers in the path
207d352ac68SChris Mason  * and it drops any locks held by this path
208d352ac68SChris Mason  *
209d352ac68SChris Mason  * It is safe to call this on paths that no locks or extent buffers held.
210d352ac68SChris Mason  */
211b3b4aa74SDavid Sterba noinline void btrfs_release_path(struct btrfs_path *p)
212eb60ceacSChris Mason {
213eb60ceacSChris Mason 	int i;
214a2135011SChris Mason 
215234b63a0SChris Mason 	for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
2163f157a2fSChris Mason 		p->slots[i] = 0;
217eb60ceacSChris Mason 		if (!p->nodes[i])
218925baeddSChris Mason 			continue;
219925baeddSChris Mason 		if (p->locks[i]) {
220bd681513SChris Mason 			btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
221925baeddSChris Mason 			p->locks[i] = 0;
222925baeddSChris Mason 		}
2235f39d397SChris Mason 		free_extent_buffer(p->nodes[i]);
2243f157a2fSChris Mason 		p->nodes[i] = NULL;
225eb60ceacSChris Mason 	}
226eb60ceacSChris Mason }
227eb60ceacSChris Mason 
228d352ac68SChris Mason /*
2298bb808c6SDavid Sterba  * We want the transaction abort to print stack trace only for errors where the
2308bb808c6SDavid Sterba  * cause could be a bug, eg. due to ENOSPC, and not for common errors that are
2318bb808c6SDavid Sterba  * caused by external factors.
2328bb808c6SDavid Sterba  */
2338bb808c6SDavid Sterba bool __cold abort_should_print_stack(int errno)
2348bb808c6SDavid Sterba {
2358bb808c6SDavid Sterba 	switch (errno) {
2368bb808c6SDavid Sterba 	case -EIO:
2378bb808c6SDavid Sterba 	case -EROFS:
2388bb808c6SDavid Sterba 	case -ENOMEM:
2398bb808c6SDavid Sterba 		return false;
2408bb808c6SDavid Sterba 	}
2418bb808c6SDavid Sterba 	return true;
2428bb808c6SDavid Sterba }
2438bb808c6SDavid Sterba 
2448bb808c6SDavid Sterba /*
245d352ac68SChris Mason  * safely gets a reference on the root node of a tree.  A lock
246d352ac68SChris Mason  * is not taken, so a concurrent writer may put a different node
247d352ac68SChris Mason  * at the root of the tree.  See btrfs_lock_root_node for the
248d352ac68SChris Mason  * looping required.
249d352ac68SChris Mason  *
250d352ac68SChris Mason  * The extent buffer returned by this has a reference taken, so
251d352ac68SChris Mason  * it won't disappear.  It may stop being the root of the tree
252d352ac68SChris Mason  * at any time because there are no locks held.
253d352ac68SChris Mason  */
254925baeddSChris Mason struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
255925baeddSChris Mason {
256925baeddSChris Mason 	struct extent_buffer *eb;
257240f62c8SChris Mason 
2583083ee2eSJosef Bacik 	while (1) {
259240f62c8SChris Mason 		rcu_read_lock();
260240f62c8SChris Mason 		eb = rcu_dereference(root->node);
2613083ee2eSJosef Bacik 
2623083ee2eSJosef Bacik 		/*
2633083ee2eSJosef Bacik 		 * RCU really hurts here, we could free up the root node because
26401327610SNicholas D Steeves 		 * it was COWed but we may not get the new root node yet so do
2653083ee2eSJosef Bacik 		 * the inc_not_zero dance and if it doesn't work then
2663083ee2eSJosef Bacik 		 * synchronize_rcu and try again.
2673083ee2eSJosef Bacik 		 */
2683083ee2eSJosef Bacik 		if (atomic_inc_not_zero(&eb->refs)) {
269240f62c8SChris Mason 			rcu_read_unlock();
2703083ee2eSJosef Bacik 			break;
2713083ee2eSJosef Bacik 		}
2723083ee2eSJosef Bacik 		rcu_read_unlock();
2733083ee2eSJosef Bacik 		synchronize_rcu();
2743083ee2eSJosef Bacik 	}
275925baeddSChris Mason 	return eb;
276925baeddSChris Mason }
277925baeddSChris Mason 
27892a7cc42SQu Wenruo /*
27992a7cc42SQu Wenruo  * Cowonly root (not-shareable trees, everything not subvolume or reloc roots),
28092a7cc42SQu Wenruo  * just get put onto a simple dirty list.  Transaction walks this list to make
28192a7cc42SQu Wenruo  * sure they get properly updated on disk.
282d352ac68SChris Mason  */
2830b86a832SChris Mason static void add_root_to_dirty_list(struct btrfs_root *root)
2840b86a832SChris Mason {
2850b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
2860b246afaSJeff Mahoney 
287e7070be1SJosef Bacik 	if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
288e7070be1SJosef Bacik 	    !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
289e7070be1SJosef Bacik 		return;
290e7070be1SJosef Bacik 
2910b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
292e7070be1SJosef Bacik 	if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
293e7070be1SJosef Bacik 		/* Want the extent tree to be the last on the list */
2944fd786e6SMisono Tomohiro 		if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID)
295e7070be1SJosef Bacik 			list_move_tail(&root->dirty_list,
2960b246afaSJeff Mahoney 				       &fs_info->dirty_cowonly_roots);
297e7070be1SJosef Bacik 		else
298e7070be1SJosef Bacik 			list_move(&root->dirty_list,
2990b246afaSJeff Mahoney 				  &fs_info->dirty_cowonly_roots);
3000b86a832SChris Mason 	}
3010b246afaSJeff Mahoney 	spin_unlock(&fs_info->trans_lock);
3020b86a832SChris Mason }
3030b86a832SChris Mason 
304d352ac68SChris Mason /*
305d352ac68SChris Mason  * used by snapshot creation to make a copy of a root for a tree with
306d352ac68SChris Mason  * a given objectid.  The buffer with the new root node is returned in
307d352ac68SChris Mason  * cow_ret, and this func returns zero on success or a negative error code.
308d352ac68SChris Mason  */
309be20aa9dSChris Mason int btrfs_copy_root(struct btrfs_trans_handle *trans,
310be20aa9dSChris Mason 		      struct btrfs_root *root,
311be20aa9dSChris Mason 		      struct extent_buffer *buf,
312be20aa9dSChris Mason 		      struct extent_buffer **cow_ret, u64 new_root_objectid)
313be20aa9dSChris Mason {
3140b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
315be20aa9dSChris Mason 	struct extent_buffer *cow;
316be20aa9dSChris Mason 	int ret = 0;
317be20aa9dSChris Mason 	int level;
3185d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
319be20aa9dSChris Mason 
32092a7cc42SQu Wenruo 	WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
3210b246afaSJeff Mahoney 		trans->transid != fs_info->running_transaction->transid);
32292a7cc42SQu Wenruo 	WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
32327cdeb70SMiao Xie 		trans->transid != root->last_trans);
324be20aa9dSChris Mason 
325be20aa9dSChris Mason 	level = btrfs_header_level(buf);
3265d4f98a2SYan Zheng 	if (level == 0)
3275d4f98a2SYan Zheng 		btrfs_item_key(buf, &disk_key, 0);
3285d4f98a2SYan Zheng 	else
3295d4f98a2SYan Zheng 		btrfs_node_key(buf, &disk_key, 0);
33031840ae1SZheng Yan 
3314d75f8a9SDavid Sterba 	cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
332cf6f34aaSJosef Bacik 				     &disk_key, level, buf->start, 0,
333cf6f34aaSJosef Bacik 				     BTRFS_NESTING_NEW_ROOT);
3345d4f98a2SYan Zheng 	if (IS_ERR(cow))
335be20aa9dSChris Mason 		return PTR_ERR(cow);
336be20aa9dSChris Mason 
33758e8012cSDavid Sterba 	copy_extent_buffer_full(cow, buf);
338be20aa9dSChris Mason 	btrfs_set_header_bytenr(cow, cow->start);
339be20aa9dSChris Mason 	btrfs_set_header_generation(cow, trans->transid);
3405d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
3415d4f98a2SYan Zheng 	btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
3425d4f98a2SYan Zheng 				     BTRFS_HEADER_FLAG_RELOC);
3435d4f98a2SYan Zheng 	if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
3445d4f98a2SYan Zheng 		btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
3455d4f98a2SYan Zheng 	else
346be20aa9dSChris Mason 		btrfs_set_header_owner(cow, new_root_objectid);
347be20aa9dSChris Mason 
348de37aa51SNikolay Borisov 	write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
3492b82032cSYan Zheng 
350be20aa9dSChris Mason 	WARN_ON(btrfs_header_generation(buf) > trans->transid);
3515d4f98a2SYan Zheng 	if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
352e339a6b0SJosef Bacik 		ret = btrfs_inc_ref(trans, root, cow, 1);
3535d4f98a2SYan Zheng 	else
354e339a6b0SJosef Bacik 		ret = btrfs_inc_ref(trans, root, cow, 0);
355867ed321SJosef Bacik 	if (ret) {
35672c9925fSFilipe Manana 		btrfs_tree_unlock(cow);
35772c9925fSFilipe Manana 		free_extent_buffer(cow);
358867ed321SJosef Bacik 		btrfs_abort_transaction(trans, ret);
359be20aa9dSChris Mason 		return ret;
360867ed321SJosef Bacik 	}
361be20aa9dSChris Mason 
362be20aa9dSChris Mason 	btrfs_mark_buffer_dirty(cow);
363be20aa9dSChris Mason 	*cow_ret = cow;
364be20aa9dSChris Mason 	return 0;
365be20aa9dSChris Mason }
366be20aa9dSChris Mason 
367d352ac68SChris Mason /*
3685d4f98a2SYan Zheng  * check if the tree block can be shared by multiple trees
3695d4f98a2SYan Zheng  */
370*eb96e221SFilipe Manana int btrfs_block_can_be_shared(struct btrfs_trans_handle *trans,
371*eb96e221SFilipe Manana 			      struct btrfs_root *root,
3725d4f98a2SYan Zheng 			      struct extent_buffer *buf)
3735d4f98a2SYan Zheng {
3745d4f98a2SYan Zheng 	/*
37592a7cc42SQu Wenruo 	 * Tree blocks not in shareable trees and tree roots are never shared.
37692a7cc42SQu Wenruo 	 * If a block was allocated after the last snapshot and the block was
37792a7cc42SQu Wenruo 	 * not allocated by tree relocation, we know the block is not shared.
3785d4f98a2SYan Zheng 	 */
37992a7cc42SQu Wenruo 	if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
380*eb96e221SFilipe Manana 	    buf != root->node &&
3815d4f98a2SYan Zheng 	    (btrfs_header_generation(buf) <=
3825d4f98a2SYan Zheng 	     btrfs_root_last_snapshot(&root->root_item) ||
383*eb96e221SFilipe Manana 	     btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) {
384*eb96e221SFilipe Manana 		if (buf != root->commit_root)
3855d4f98a2SYan Zheng 			return 1;
386*eb96e221SFilipe Manana 		/*
387*eb96e221SFilipe Manana 		 * An extent buffer that used to be the commit root may still be
388*eb96e221SFilipe Manana 		 * shared because the tree height may have increased and it
389*eb96e221SFilipe Manana 		 * became a child of a higher level root. This can happen when
390*eb96e221SFilipe Manana 		 * snapshotting a subvolume created in the current transaction.
391*eb96e221SFilipe Manana 		 */
392*eb96e221SFilipe Manana 		if (btrfs_header_generation(buf) == trans->transid)
393*eb96e221SFilipe Manana 			return 1;
394*eb96e221SFilipe Manana 	}
395a79865c6SNikolay Borisov 
3965d4f98a2SYan Zheng 	return 0;
3975d4f98a2SYan Zheng }
3985d4f98a2SYan Zheng 
3995d4f98a2SYan Zheng static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
4005d4f98a2SYan Zheng 				       struct btrfs_root *root,
4015d4f98a2SYan Zheng 				       struct extent_buffer *buf,
402f0486c68SYan, Zheng 				       struct extent_buffer *cow,
403f0486c68SYan, Zheng 				       int *last_ref)
4045d4f98a2SYan Zheng {
4050b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
4065d4f98a2SYan Zheng 	u64 refs;
4075d4f98a2SYan Zheng 	u64 owner;
4085d4f98a2SYan Zheng 	u64 flags;
4095d4f98a2SYan Zheng 	u64 new_flags = 0;
4105d4f98a2SYan Zheng 	int ret;
4115d4f98a2SYan Zheng 
4125d4f98a2SYan Zheng 	/*
4135d4f98a2SYan Zheng 	 * Backrefs update rules:
4145d4f98a2SYan Zheng 	 *
4155d4f98a2SYan Zheng 	 * Always use full backrefs for extent pointers in tree block
4165d4f98a2SYan Zheng 	 * allocated by tree relocation.
4175d4f98a2SYan Zheng 	 *
4185d4f98a2SYan Zheng 	 * If a shared tree block is no longer referenced by its owner
4195d4f98a2SYan Zheng 	 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
4205d4f98a2SYan Zheng 	 * use full backrefs for extent pointers in tree block.
4215d4f98a2SYan Zheng 	 *
4225d4f98a2SYan Zheng 	 * If a tree block is been relocating
4235d4f98a2SYan Zheng 	 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
4245d4f98a2SYan Zheng 	 * use full backrefs for extent pointers in tree block.
4255d4f98a2SYan Zheng 	 * The reason for this is some operations (such as drop tree)
4265d4f98a2SYan Zheng 	 * are only allowed for blocks use full backrefs.
4275d4f98a2SYan Zheng 	 */
4285d4f98a2SYan Zheng 
429*eb96e221SFilipe Manana 	if (btrfs_block_can_be_shared(trans, root, buf)) {
4302ff7e61eSJeff Mahoney 		ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
4313173a18fSJosef Bacik 					       btrfs_header_level(buf), 1,
4323173a18fSJosef Bacik 					       &refs, &flags);
433be1a5564SMark Fasheh 		if (ret)
434be1a5564SMark Fasheh 			return ret;
435eced687eSFilipe Manana 		if (unlikely(refs == 0)) {
436eced687eSFilipe Manana 			btrfs_crit(fs_info,
437eced687eSFilipe Manana 		"found 0 references for tree block at bytenr %llu level %d root %llu",
438eced687eSFilipe Manana 				   buf->start, btrfs_header_level(buf),
439eced687eSFilipe Manana 				   btrfs_root_id(root));
440eced687eSFilipe Manana 			ret = -EUCLEAN;
441eced687eSFilipe Manana 			btrfs_abort_transaction(trans, ret);
442e5df9573SMark Fasheh 			return ret;
443e5df9573SMark Fasheh 		}
4445d4f98a2SYan Zheng 	} else {
4455d4f98a2SYan Zheng 		refs = 1;
4465d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
4475d4f98a2SYan Zheng 		    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
4485d4f98a2SYan Zheng 			flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
4495d4f98a2SYan Zheng 		else
4505d4f98a2SYan Zheng 			flags = 0;
4515d4f98a2SYan Zheng 	}
4525d4f98a2SYan Zheng 
4535d4f98a2SYan Zheng 	owner = btrfs_header_owner(buf);
4545d4f98a2SYan Zheng 	BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
4555d4f98a2SYan Zheng 	       !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4565d4f98a2SYan Zheng 
4575d4f98a2SYan Zheng 	if (refs > 1) {
4585d4f98a2SYan Zheng 		if ((owner == root->root_key.objectid ||
4595d4f98a2SYan Zheng 		     root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
4605d4f98a2SYan Zheng 		    !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
461e339a6b0SJosef Bacik 			ret = btrfs_inc_ref(trans, root, buf, 1);
462692826b2SJeff Mahoney 			if (ret)
463692826b2SJeff Mahoney 				return ret;
4645d4f98a2SYan Zheng 
4655d4f98a2SYan Zheng 			if (root->root_key.objectid ==
4665d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID) {
467e339a6b0SJosef Bacik 				ret = btrfs_dec_ref(trans, root, buf, 0);
468692826b2SJeff Mahoney 				if (ret)
469692826b2SJeff Mahoney 					return ret;
470e339a6b0SJosef Bacik 				ret = btrfs_inc_ref(trans, root, cow, 1);
471692826b2SJeff Mahoney 				if (ret)
472692826b2SJeff Mahoney 					return ret;
4735d4f98a2SYan Zheng 			}
4745d4f98a2SYan Zheng 			new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4755d4f98a2SYan Zheng 		} else {
4765d4f98a2SYan Zheng 
4775d4f98a2SYan Zheng 			if (root->root_key.objectid ==
4785d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID)
479e339a6b0SJosef Bacik 				ret = btrfs_inc_ref(trans, root, cow, 1);
4805d4f98a2SYan Zheng 			else
481e339a6b0SJosef Bacik 				ret = btrfs_inc_ref(trans, root, cow, 0);
482692826b2SJeff Mahoney 			if (ret)
483692826b2SJeff Mahoney 				return ret;
4845d4f98a2SYan Zheng 		}
4855d4f98a2SYan Zheng 		if (new_flags != 0) {
4864aec05faSJosef Bacik 			ret = btrfs_set_disk_extent_flags(trans, buf, new_flags);
487be1a5564SMark Fasheh 			if (ret)
488be1a5564SMark Fasheh 				return ret;
4895d4f98a2SYan Zheng 		}
4905d4f98a2SYan Zheng 	} else {
4915d4f98a2SYan Zheng 		if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
4925d4f98a2SYan Zheng 			if (root->root_key.objectid ==
4935d4f98a2SYan Zheng 			    BTRFS_TREE_RELOC_OBJECTID)
494e339a6b0SJosef Bacik 				ret = btrfs_inc_ref(trans, root, cow, 1);
4955d4f98a2SYan Zheng 			else
496e339a6b0SJosef Bacik 				ret = btrfs_inc_ref(trans, root, cow, 0);
497692826b2SJeff Mahoney 			if (ret)
498692826b2SJeff Mahoney 				return ret;
499e339a6b0SJosef Bacik 			ret = btrfs_dec_ref(trans, root, buf, 1);
500692826b2SJeff Mahoney 			if (ret)
501692826b2SJeff Mahoney 				return ret;
5025d4f98a2SYan Zheng 		}
503190a8339SJosef Bacik 		btrfs_clear_buffer_dirty(trans, buf);
504f0486c68SYan, Zheng 		*last_ref = 1;
5055d4f98a2SYan Zheng 	}
5065d4f98a2SYan Zheng 	return 0;
5075d4f98a2SYan Zheng }
5085d4f98a2SYan Zheng 
5095d4f98a2SYan Zheng /*
510d397712bSChris Mason  * does the dirty work in cow of a single block.  The parent block (if
511d397712bSChris Mason  * supplied) is updated to point to the new cow copy.  The new buffer is marked
512d397712bSChris Mason  * dirty and returned locked.  If you modify the block it needs to be marked
513d397712bSChris Mason  * dirty again.
514d352ac68SChris Mason  *
515d352ac68SChris Mason  * search_start -- an allocation hint for the new block
516d352ac68SChris Mason  *
517d397712bSChris Mason  * empty_size -- a hint that you plan on doing more cow.  This is the size in
518d397712bSChris Mason  * bytes the allocator should try to find free next to the block it returns.
519d397712bSChris Mason  * This is just a hint and may be ignored by the allocator.
520d352ac68SChris Mason  */
521d397712bSChris Mason static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
5225f39d397SChris Mason 			     struct btrfs_root *root,
5235f39d397SChris Mason 			     struct extent_buffer *buf,
5245f39d397SChris Mason 			     struct extent_buffer *parent, int parent_slot,
5255f39d397SChris Mason 			     struct extent_buffer **cow_ret,
5269631e4ccSJosef Bacik 			     u64 search_start, u64 empty_size,
5279631e4ccSJosef Bacik 			     enum btrfs_lock_nesting nest)
5286702ed49SChris Mason {
5290b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
5305d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
5315f39d397SChris Mason 	struct extent_buffer *cow;
532be1a5564SMark Fasheh 	int level, ret;
533f0486c68SYan, Zheng 	int last_ref = 0;
534925baeddSChris Mason 	int unlock_orig = 0;
5350f5053ebSGoldwyn Rodrigues 	u64 parent_start = 0;
5366702ed49SChris Mason 
537925baeddSChris Mason 	if (*cow_ret == buf)
538925baeddSChris Mason 		unlock_orig = 1;
539925baeddSChris Mason 
54049d0c642SFilipe Manana 	btrfs_assert_tree_write_locked(buf);
541925baeddSChris Mason 
54292a7cc42SQu Wenruo 	WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
5430b246afaSJeff Mahoney 		trans->transid != fs_info->running_transaction->transid);
54492a7cc42SQu Wenruo 	WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
54527cdeb70SMiao Xie 		trans->transid != root->last_trans);
5465f39d397SChris Mason 
5477bb86316SChris Mason 	level = btrfs_header_level(buf);
54831840ae1SZheng Yan 
5495d4f98a2SYan Zheng 	if (level == 0)
5505d4f98a2SYan Zheng 		btrfs_item_key(buf, &disk_key, 0);
5515d4f98a2SYan Zheng 	else
5525d4f98a2SYan Zheng 		btrfs_node_key(buf, &disk_key, 0);
5535d4f98a2SYan Zheng 
5540f5053ebSGoldwyn Rodrigues 	if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
5555d4f98a2SYan Zheng 		parent_start = parent->start;
5565d4f98a2SYan Zheng 
55779bd3712SFilipe Manana 	cow = btrfs_alloc_tree_block(trans, root, parent_start,
55879bd3712SFilipe Manana 				     root->root_key.objectid, &disk_key, level,
55979bd3712SFilipe Manana 				     search_start, empty_size, nest);
5606702ed49SChris Mason 	if (IS_ERR(cow))
5616702ed49SChris Mason 		return PTR_ERR(cow);
5626702ed49SChris Mason 
563b4ce94deSChris Mason 	/* cow is set to blocking by btrfs_init_new_buffer */
564b4ce94deSChris Mason 
56558e8012cSDavid Sterba 	copy_extent_buffer_full(cow, buf);
566db94535dSChris Mason 	btrfs_set_header_bytenr(cow, cow->start);
5675f39d397SChris Mason 	btrfs_set_header_generation(cow, trans->transid);
5685d4f98a2SYan Zheng 	btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
5695d4f98a2SYan Zheng 	btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
5705d4f98a2SYan Zheng 				     BTRFS_HEADER_FLAG_RELOC);
5715d4f98a2SYan Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
5725d4f98a2SYan Zheng 		btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
5735d4f98a2SYan Zheng 	else
5745f39d397SChris Mason 		btrfs_set_header_owner(cow, root->root_key.objectid);
5756702ed49SChris Mason 
576de37aa51SNikolay Borisov 	write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
5772b82032cSYan Zheng 
578be1a5564SMark Fasheh 	ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
579b68dc2a9SMark Fasheh 	if (ret) {
580572c83acSJosef Bacik 		btrfs_tree_unlock(cow);
581572c83acSJosef Bacik 		free_extent_buffer(cow);
58266642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
583b68dc2a9SMark Fasheh 		return ret;
584b68dc2a9SMark Fasheh 	}
5851a40e23bSZheng Yan 
58692a7cc42SQu Wenruo 	if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
58783d4cfd4SJosef Bacik 		ret = btrfs_reloc_cow_block(trans, root, buf, cow);
58893314e3bSZhaolei 		if (ret) {
589572c83acSJosef Bacik 			btrfs_tree_unlock(cow);
590572c83acSJosef Bacik 			free_extent_buffer(cow);
59166642832SJeff Mahoney 			btrfs_abort_transaction(trans, ret);
59283d4cfd4SJosef Bacik 			return ret;
59383d4cfd4SJosef Bacik 		}
59493314e3bSZhaolei 	}
5953fd0a558SYan, Zheng 
5966702ed49SChris Mason 	if (buf == root->node) {
597925baeddSChris Mason 		WARN_ON(parent && parent != buf);
5985d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
5995d4f98a2SYan Zheng 		    btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
6005d4f98a2SYan Zheng 			parent_start = buf->start;
601925baeddSChris Mason 
602406808abSFilipe Manana 		ret = btrfs_tree_mod_log_insert_root(root->node, cow, true);
60340b0a749SFilipe Manana 		if (ret < 0) {
60440b0a749SFilipe Manana 			btrfs_tree_unlock(cow);
60540b0a749SFilipe Manana 			free_extent_buffer(cow);
60640b0a749SFilipe Manana 			btrfs_abort_transaction(trans, ret);
60740b0a749SFilipe Manana 			return ret;
60840b0a749SFilipe Manana 		}
60940b0a749SFilipe Manana 		atomic_inc(&cow->refs);
610240f62c8SChris Mason 		rcu_assign_pointer(root->node, cow);
611925baeddSChris Mason 
6127a163608SFilipe Manana 		btrfs_free_tree_block(trans, btrfs_root_id(root), buf,
6137a163608SFilipe Manana 				      parent_start, last_ref);
6145f39d397SChris Mason 		free_extent_buffer(buf);
6150b86a832SChris Mason 		add_root_to_dirty_list(root);
6166702ed49SChris Mason 	} else {
6175d4f98a2SYan Zheng 		WARN_ON(trans->transid != btrfs_header_generation(parent));
618d09c5152SFilipe Manana 		ret = btrfs_tree_mod_log_insert_key(parent, parent_slot,
61933cff222SFilipe Manana 						    BTRFS_MOD_LOG_KEY_REPLACE);
620d09c5152SFilipe Manana 		if (ret) {
621d09c5152SFilipe Manana 			btrfs_tree_unlock(cow);
622d09c5152SFilipe Manana 			free_extent_buffer(cow);
623d09c5152SFilipe Manana 			btrfs_abort_transaction(trans, ret);
624d09c5152SFilipe Manana 			return ret;
625d09c5152SFilipe Manana 		}
6265f39d397SChris Mason 		btrfs_set_node_blockptr(parent, parent_slot,
627db94535dSChris Mason 					cow->start);
62874493f7aSChris Mason 		btrfs_set_node_ptr_generation(parent, parent_slot,
62974493f7aSChris Mason 					      trans->transid);
6306702ed49SChris Mason 		btrfs_mark_buffer_dirty(parent);
6315de865eeSFilipe David Borba Manana 		if (last_ref) {
632f3a84ccdSFilipe Manana 			ret = btrfs_tree_mod_log_free_eb(buf);
6335de865eeSFilipe David Borba Manana 			if (ret) {
634572c83acSJosef Bacik 				btrfs_tree_unlock(cow);
635572c83acSJosef Bacik 				free_extent_buffer(cow);
63666642832SJeff Mahoney 				btrfs_abort_transaction(trans, ret);
6375de865eeSFilipe David Borba Manana 				return ret;
6385de865eeSFilipe David Borba Manana 			}
6395de865eeSFilipe David Borba Manana 		}
6407a163608SFilipe Manana 		btrfs_free_tree_block(trans, btrfs_root_id(root), buf,
6417a163608SFilipe Manana 				      parent_start, last_ref);
6426702ed49SChris Mason 	}
643925baeddSChris Mason 	if (unlock_orig)
644925baeddSChris Mason 		btrfs_tree_unlock(buf);
6453083ee2eSJosef Bacik 	free_extent_buffer_stale(buf);
6466702ed49SChris Mason 	btrfs_mark_buffer_dirty(cow);
6476702ed49SChris Mason 	*cow_ret = cow;
6486702ed49SChris Mason 	return 0;
6496702ed49SChris Mason }
6506702ed49SChris Mason 
6515d4f98a2SYan Zheng static inline int should_cow_block(struct btrfs_trans_handle *trans,
6525d4f98a2SYan Zheng 				   struct btrfs_root *root,
6535d4f98a2SYan Zheng 				   struct extent_buffer *buf)
6545d4f98a2SYan Zheng {
655f5ee5c9aSJeff Mahoney 	if (btrfs_is_testing(root->fs_info))
656faa2dbf0SJosef Bacik 		return 0;
657fccb84c9SDavid Sterba 
658d1980131SDavid Sterba 	/* Ensure we can see the FORCE_COW bit */
659d1980131SDavid Sterba 	smp_mb__before_atomic();
660f1ebcc74SLiu Bo 
661f1ebcc74SLiu Bo 	/*
662f1ebcc74SLiu Bo 	 * We do not need to cow a block if
663f1ebcc74SLiu Bo 	 * 1) this block is not created or changed in this transaction;
664f1ebcc74SLiu Bo 	 * 2) this block does not belong to TREE_RELOC tree;
665f1ebcc74SLiu Bo 	 * 3) the root is not forced COW.
666f1ebcc74SLiu Bo 	 *
667f1ebcc74SLiu Bo 	 * What is forced COW:
66801327610SNicholas D Steeves 	 *    when we create snapshot during committing the transaction,
66952042d8eSAndrea Gelmini 	 *    after we've finished copying src root, we must COW the shared
670f1ebcc74SLiu Bo 	 *    block to ensure the metadata consistency.
671f1ebcc74SLiu Bo 	 */
6725d4f98a2SYan Zheng 	if (btrfs_header_generation(buf) == trans->transid &&
6735d4f98a2SYan Zheng 	    !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
6745d4f98a2SYan Zheng 	    !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
675f1ebcc74SLiu Bo 	      btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
67627cdeb70SMiao Xie 	    !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
6775d4f98a2SYan Zheng 		return 0;
6785d4f98a2SYan Zheng 	return 1;
6795d4f98a2SYan Zheng }
6805d4f98a2SYan Zheng 
681d352ac68SChris Mason /*
682d352ac68SChris Mason  * cows a single block, see __btrfs_cow_block for the real work.
68301327610SNicholas D Steeves  * This version of it has extra checks so that a block isn't COWed more than
684d352ac68SChris Mason  * once per transaction, as long as it hasn't been written yet
685d352ac68SChris Mason  */
686d397712bSChris Mason noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
6875f39d397SChris Mason 		    struct btrfs_root *root, struct extent_buffer *buf,
6885f39d397SChris Mason 		    struct extent_buffer *parent, int parent_slot,
6899631e4ccSJosef Bacik 		    struct extent_buffer **cow_ret,
6909631e4ccSJosef Bacik 		    enum btrfs_lock_nesting nest)
69102217ed2SChris Mason {
6920b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
6936702ed49SChris Mason 	u64 search_start;
694f510cfecSChris Mason 	int ret;
695dc17ff8fSChris Mason 
696a2caab29SFilipe Manana 	if (unlikely(test_bit(BTRFS_ROOT_DELETING, &root->state))) {
697a2caab29SFilipe Manana 		btrfs_abort_transaction(trans, -EUCLEAN);
698a2caab29SFilipe Manana 		btrfs_crit(fs_info,
699a2caab29SFilipe Manana 		   "attempt to COW block %llu on root %llu that is being deleted",
700a2caab29SFilipe Manana 			   buf->start, btrfs_root_id(root));
701a2caab29SFilipe Manana 		return -EUCLEAN;
702a2caab29SFilipe Manana 	}
70383354f07SJosef Bacik 
70448774f3bSFilipe Manana 	/*
70548774f3bSFilipe Manana 	 * COWing must happen through a running transaction, which always
70648774f3bSFilipe Manana 	 * matches the current fs generation (it's a transaction with a state
70748774f3bSFilipe Manana 	 * less than TRANS_STATE_UNBLOCKED). If it doesn't, then turn the fs
70848774f3bSFilipe Manana 	 * into error state to prevent the commit of any transaction.
70948774f3bSFilipe Manana 	 */
71048774f3bSFilipe Manana 	if (unlikely(trans->transaction != fs_info->running_transaction ||
71148774f3bSFilipe Manana 		     trans->transid != fs_info->generation)) {
71248774f3bSFilipe Manana 		btrfs_abort_transaction(trans, -EUCLEAN);
71348774f3bSFilipe Manana 		btrfs_crit(fs_info,
71448774f3bSFilipe Manana "unexpected transaction when attempting to COW block %llu on root %llu, transaction %llu running transaction %llu fs generation %llu",
71548774f3bSFilipe Manana 			   buf->start, btrfs_root_id(root), trans->transid,
71648774f3bSFilipe Manana 			   fs_info->running_transaction->transid,
71748774f3bSFilipe Manana 			   fs_info->generation);
71848774f3bSFilipe Manana 		return -EUCLEAN;
71948774f3bSFilipe Manana 	}
720dc17ff8fSChris Mason 
7215d4f98a2SYan Zheng 	if (!should_cow_block(trans, root, buf)) {
72202217ed2SChris Mason 		*cow_ret = buf;
72302217ed2SChris Mason 		return 0;
72402217ed2SChris Mason 	}
725c487685dSChris Mason 
726ee22184bSByongho Lee 	search_start = buf->start & ~((u64)SZ_1G - 1);
727b4ce94deSChris Mason 
728f616f5cdSQu Wenruo 	/*
729f616f5cdSQu Wenruo 	 * Before CoWing this block for later modification, check if it's
730f616f5cdSQu Wenruo 	 * the subtree root and do the delayed subtree trace if needed.
731f616f5cdSQu Wenruo 	 *
732f616f5cdSQu Wenruo 	 * Also We don't care about the error, as it's handled internally.
733f616f5cdSQu Wenruo 	 */
734f616f5cdSQu Wenruo 	btrfs_qgroup_trace_subtree_after_cow(trans, root, buf);
735f510cfecSChris Mason 	ret = __btrfs_cow_block(trans, root, buf, parent,
7369631e4ccSJosef Bacik 				 parent_slot, cow_ret, search_start, 0, nest);
7371abe9b8aSliubo 
7381abe9b8aSliubo 	trace_btrfs_cow_block(root, buf, *cow_ret);
7391abe9b8aSliubo 
740f510cfecSChris Mason 	return ret;
7412c90e5d6SChris Mason }
742f75e2b79SJosef Bacik ALLOW_ERROR_INJECTION(btrfs_cow_block, ERRNO);
7436702ed49SChris Mason 
744d352ac68SChris Mason /*
745d352ac68SChris Mason  * helper function for defrag to decide if two blocks pointed to by a
746d352ac68SChris Mason  * node are actually close by
747d352ac68SChris Mason  */
7486b80053dSChris Mason static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
7496702ed49SChris Mason {
7506b80053dSChris Mason 	if (blocknr < other && other - (blocknr + blocksize) < 32768)
7516702ed49SChris Mason 		return 1;
7526b80053dSChris Mason 	if (blocknr > other && blocknr - (other + blocksize) < 32768)
7536702ed49SChris Mason 		return 1;
75402217ed2SChris Mason 	return 0;
75502217ed2SChris Mason }
75602217ed2SChris Mason 
757ce6ef5abSDavid Sterba #ifdef __LITTLE_ENDIAN
758ce6ef5abSDavid Sterba 
759ce6ef5abSDavid Sterba /*
760ce6ef5abSDavid Sterba  * Compare two keys, on little-endian the disk order is same as CPU order and
761ce6ef5abSDavid Sterba  * we can avoid the conversion.
762ce6ef5abSDavid Sterba  */
763ce6ef5abSDavid Sterba static int comp_keys(const struct btrfs_disk_key *disk_key,
764ce6ef5abSDavid Sterba 		     const struct btrfs_key *k2)
765ce6ef5abSDavid Sterba {
766ce6ef5abSDavid Sterba 	const struct btrfs_key *k1 = (const struct btrfs_key *)disk_key;
767ce6ef5abSDavid Sterba 
768ce6ef5abSDavid Sterba 	return btrfs_comp_cpu_keys(k1, k2);
769ce6ef5abSDavid Sterba }
770ce6ef5abSDavid Sterba 
771ce6ef5abSDavid Sterba #else
772ce6ef5abSDavid Sterba 
773081e9573SChris Mason /*
774081e9573SChris Mason  * compare two keys in a memcmp fashion
775081e9573SChris Mason  */
776310712b2SOmar Sandoval static int comp_keys(const struct btrfs_disk_key *disk,
777310712b2SOmar Sandoval 		     const struct btrfs_key *k2)
778081e9573SChris Mason {
779081e9573SChris Mason 	struct btrfs_key k1;
780081e9573SChris Mason 
781081e9573SChris Mason 	btrfs_disk_key_to_cpu(&k1, disk);
782081e9573SChris Mason 
78320736abaSDiego Calleja 	return btrfs_comp_cpu_keys(&k1, k2);
784081e9573SChris Mason }
785ce6ef5abSDavid Sterba #endif
786081e9573SChris Mason 
787f3465ca4SJosef Bacik /*
788f3465ca4SJosef Bacik  * same as comp_keys only with two btrfs_key's
789f3465ca4SJosef Bacik  */
790e1f60a65SDavid Sterba int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
791f3465ca4SJosef Bacik {
792f3465ca4SJosef Bacik 	if (k1->objectid > k2->objectid)
793f3465ca4SJosef Bacik 		return 1;
794f3465ca4SJosef Bacik 	if (k1->objectid < k2->objectid)
795f3465ca4SJosef Bacik 		return -1;
796f3465ca4SJosef Bacik 	if (k1->type > k2->type)
797f3465ca4SJosef Bacik 		return 1;
798f3465ca4SJosef Bacik 	if (k1->type < k2->type)
799f3465ca4SJosef Bacik 		return -1;
800f3465ca4SJosef Bacik 	if (k1->offset > k2->offset)
801f3465ca4SJosef Bacik 		return 1;
802f3465ca4SJosef Bacik 	if (k1->offset < k2->offset)
803f3465ca4SJosef Bacik 		return -1;
804f3465ca4SJosef Bacik 	return 0;
805f3465ca4SJosef Bacik }
806081e9573SChris Mason 
807d352ac68SChris Mason /*
808d352ac68SChris Mason  * this is used by the defrag code to go through all the
809d352ac68SChris Mason  * leaves pointed to by a node and reallocate them so that
810d352ac68SChris Mason  * disk order is close to key order
811d352ac68SChris Mason  */
8126702ed49SChris Mason int btrfs_realloc_node(struct btrfs_trans_handle *trans,
8135f39d397SChris Mason 		       struct btrfs_root *root, struct extent_buffer *parent,
814de78b51aSEric Sandeen 		       int start_slot, u64 *last_ret,
815a6b6e75eSChris Mason 		       struct btrfs_key *progress)
8166702ed49SChris Mason {
8170b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
8186b80053dSChris Mason 	struct extent_buffer *cur;
8196702ed49SChris Mason 	u64 blocknr;
820e9d0b13bSChris Mason 	u64 search_start = *last_ret;
821e9d0b13bSChris Mason 	u64 last_block = 0;
8226702ed49SChris Mason 	u64 other;
8236702ed49SChris Mason 	u32 parent_nritems;
8246702ed49SChris Mason 	int end_slot;
8256702ed49SChris Mason 	int i;
8266702ed49SChris Mason 	int err = 0;
8276b80053dSChris Mason 	u32 blocksize;
828081e9573SChris Mason 	int progress_passed = 0;
829081e9573SChris Mason 	struct btrfs_disk_key disk_key;
8306702ed49SChris Mason 
831e36f9491SFilipe Manana 	/*
832e36f9491SFilipe Manana 	 * COWing must happen through a running transaction, which always
833e36f9491SFilipe Manana 	 * matches the current fs generation (it's a transaction with a state
834e36f9491SFilipe Manana 	 * less than TRANS_STATE_UNBLOCKED). If it doesn't, then turn the fs
835e36f9491SFilipe Manana 	 * into error state to prevent the commit of any transaction.
836e36f9491SFilipe Manana 	 */
837e36f9491SFilipe Manana 	if (unlikely(trans->transaction != fs_info->running_transaction ||
838e36f9491SFilipe Manana 		     trans->transid != fs_info->generation)) {
839e36f9491SFilipe Manana 		btrfs_abort_transaction(trans, -EUCLEAN);
840e36f9491SFilipe Manana 		btrfs_crit(fs_info,
841e36f9491SFilipe Manana "unexpected transaction when attempting to reallocate parent %llu for root %llu, transaction %llu running transaction %llu fs generation %llu",
842e36f9491SFilipe Manana 			   parent->start, btrfs_root_id(root), trans->transid,
843e36f9491SFilipe Manana 			   fs_info->running_transaction->transid,
844e36f9491SFilipe Manana 			   fs_info->generation);
845e36f9491SFilipe Manana 		return -EUCLEAN;
846e36f9491SFilipe Manana 	}
84786479a04SChris Mason 
8486b80053dSChris Mason 	parent_nritems = btrfs_header_nritems(parent);
8490b246afaSJeff Mahoney 	blocksize = fs_info->nodesize;
8505dfe2be7SFilipe Manana 	end_slot = parent_nritems - 1;
8516702ed49SChris Mason 
8525dfe2be7SFilipe Manana 	if (parent_nritems <= 1)
8536702ed49SChris Mason 		return 0;
8546702ed49SChris Mason 
8555dfe2be7SFilipe Manana 	for (i = start_slot; i <= end_slot; i++) {
8566702ed49SChris Mason 		int close = 1;
857a6b6e75eSChris Mason 
858081e9573SChris Mason 		btrfs_node_key(parent, &disk_key, i);
859081e9573SChris Mason 		if (!progress_passed && comp_keys(&disk_key, progress) < 0)
860081e9573SChris Mason 			continue;
861081e9573SChris Mason 
862081e9573SChris Mason 		progress_passed = 1;
8636b80053dSChris Mason 		blocknr = btrfs_node_blockptr(parent, i);
864e9d0b13bSChris Mason 		if (last_block == 0)
865e9d0b13bSChris Mason 			last_block = blocknr;
8665708b959SChris Mason 
8676702ed49SChris Mason 		if (i > 0) {
8686b80053dSChris Mason 			other = btrfs_node_blockptr(parent, i - 1);
8696b80053dSChris Mason 			close = close_blocks(blocknr, other, blocksize);
8706702ed49SChris Mason 		}
8715dfe2be7SFilipe Manana 		if (!close && i < end_slot) {
8726b80053dSChris Mason 			other = btrfs_node_blockptr(parent, i + 1);
8736b80053dSChris Mason 			close = close_blocks(blocknr, other, blocksize);
8746702ed49SChris Mason 		}
875e9d0b13bSChris Mason 		if (close) {
876e9d0b13bSChris Mason 			last_block = blocknr;
8776702ed49SChris Mason 			continue;
878e9d0b13bSChris Mason 		}
8796702ed49SChris Mason 
880206983b7SJosef Bacik 		cur = btrfs_read_node_slot(parent, i);
881206983b7SJosef Bacik 		if (IS_ERR(cur))
88264c043deSLiu Bo 			return PTR_ERR(cur);
883e9d0b13bSChris Mason 		if (search_start == 0)
8846b80053dSChris Mason 			search_start = last_block;
885e9d0b13bSChris Mason 
886e7a84565SChris Mason 		btrfs_tree_lock(cur);
8876b80053dSChris Mason 		err = __btrfs_cow_block(trans, root, cur, parent, i,
888e7a84565SChris Mason 					&cur, search_start,
8896b80053dSChris Mason 					min(16 * blocksize,
8909631e4ccSJosef Bacik 					    (end_slot - i) * blocksize),
8919631e4ccSJosef Bacik 					BTRFS_NESTING_COW);
892252c38f0SYan 		if (err) {
893e7a84565SChris Mason 			btrfs_tree_unlock(cur);
8946b80053dSChris Mason 			free_extent_buffer(cur);
8956702ed49SChris Mason 			break;
896252c38f0SYan 		}
897e7a84565SChris Mason 		search_start = cur->start;
898e7a84565SChris Mason 		last_block = cur->start;
899f2183bdeSChris Mason 		*last_ret = search_start;
900e7a84565SChris Mason 		btrfs_tree_unlock(cur);
901e7a84565SChris Mason 		free_extent_buffer(cur);
9026702ed49SChris Mason 	}
9036702ed49SChris Mason 	return err;
9046702ed49SChris Mason }
9056702ed49SChris Mason 
90674123bd7SChris Mason /*
907fb81212cSFilipe Manana  * Search for a key in the given extent_buffer.
9085f39d397SChris Mason  *
909a724f313SFilipe Manana  * The lower boundary for the search is specified by the slot number @first_slot.
910fdf8d595SAnand Jain  * Use a value of 0 to search over the whole extent buffer. Works for both
911fdf8d595SAnand Jain  * leaves and nodes.
91274123bd7SChris Mason  *
913fb81212cSFilipe Manana  * The slot in the extent buffer is returned via @slot. If the key exists in the
914fb81212cSFilipe Manana  * extent buffer, then @slot will point to the slot where the key is, otherwise
915fb81212cSFilipe Manana  * it points to the slot where you would insert the key.
916fb81212cSFilipe Manana  *
917fb81212cSFilipe Manana  * Slot may point to the total number of items (i.e. one position beyond the last
918fb81212cSFilipe Manana  * key) if the key is bigger than the last key in the extent buffer.
91974123bd7SChris Mason  */
920fdf8d595SAnand Jain int btrfs_bin_search(struct extent_buffer *eb, int first_slot,
92167d5e289SMarcos Paulo de Souza 		     const struct btrfs_key *key, int *slot)
922be0e5c09SChris Mason {
923fb81212cSFilipe Manana 	unsigned long p;
924fb81212cSFilipe Manana 	int item_size;
925a724f313SFilipe Manana 	/*
926a724f313SFilipe Manana 	 * Use unsigned types for the low and high slots, so that we get a more
927a724f313SFilipe Manana 	 * efficient division in the search loop below.
928a724f313SFilipe Manana 	 */
929a724f313SFilipe Manana 	u32 low = first_slot;
930a724f313SFilipe Manana 	u32 high = btrfs_header_nritems(eb);
931be0e5c09SChris Mason 	int ret;
9325cd17f34SDavid Sterba 	const int key_size = sizeof(struct btrfs_disk_key);
933be0e5c09SChris Mason 
934a724f313SFilipe Manana 	if (unlikely(low > high)) {
9355e24e9afSLiu Bo 		btrfs_err(eb->fs_info,
936a724f313SFilipe Manana 		 "%s: low (%u) > high (%u) eb %llu owner %llu level %d",
9375e24e9afSLiu Bo 			  __func__, low, high, eb->start,
9385e24e9afSLiu Bo 			  btrfs_header_owner(eb), btrfs_header_level(eb));
9395e24e9afSLiu Bo 		return -EINVAL;
9405e24e9afSLiu Bo 	}
9415e24e9afSLiu Bo 
942fb81212cSFilipe Manana 	if (btrfs_header_level(eb) == 0) {
943fb81212cSFilipe Manana 		p = offsetof(struct btrfs_leaf, items);
944fb81212cSFilipe Manana 		item_size = sizeof(struct btrfs_item);
945fb81212cSFilipe Manana 	} else {
946fb81212cSFilipe Manana 		p = offsetof(struct btrfs_node, ptrs);
947fb81212cSFilipe Manana 		item_size = sizeof(struct btrfs_key_ptr);
948fb81212cSFilipe Manana 	}
949fb81212cSFilipe Manana 
950be0e5c09SChris Mason 	while (low < high) {
9515cd17f34SDavid Sterba 		unsigned long oip;
9525cd17f34SDavid Sterba 		unsigned long offset;
9535cd17f34SDavid Sterba 		struct btrfs_disk_key *tmp;
9545cd17f34SDavid Sterba 		struct btrfs_disk_key unaligned;
9555cd17f34SDavid Sterba 		int mid;
9565cd17f34SDavid Sterba 
957be0e5c09SChris Mason 		mid = (low + high) / 2;
9585f39d397SChris Mason 		offset = p + mid * item_size;
9595cd17f34SDavid Sterba 		oip = offset_in_page(offset);
9605f39d397SChris Mason 
9615cd17f34SDavid Sterba 		if (oip + key_size <= PAGE_SIZE) {
962884b07d0SQu Wenruo 			const unsigned long idx = get_eb_page_index(offset);
9635cd17f34SDavid Sterba 			char *kaddr = page_address(eb->pages[idx]);
964934d375bSChris Mason 
965884b07d0SQu Wenruo 			oip = get_eb_offset_in_page(eb, offset);
9665cd17f34SDavid Sterba 			tmp = (struct btrfs_disk_key *)(kaddr + oip);
9675cd17f34SDavid Sterba 		} else {
9685cd17f34SDavid Sterba 			read_extent_buffer(eb, &unaligned, offset, key_size);
9695f39d397SChris Mason 			tmp = &unaligned;
970479965d6SChris Mason 		}
971479965d6SChris Mason 
972be0e5c09SChris Mason 		ret = comp_keys(tmp, key);
973be0e5c09SChris Mason 
974be0e5c09SChris Mason 		if (ret < 0)
975be0e5c09SChris Mason 			low = mid + 1;
976be0e5c09SChris Mason 		else if (ret > 0)
977be0e5c09SChris Mason 			high = mid;
978be0e5c09SChris Mason 		else {
979be0e5c09SChris Mason 			*slot = mid;
980be0e5c09SChris Mason 			return 0;
981be0e5c09SChris Mason 		}
982be0e5c09SChris Mason 	}
983be0e5c09SChris Mason 	*slot = low;
984be0e5c09SChris Mason 	return 1;
985be0e5c09SChris Mason }
986be0e5c09SChris Mason 
987f0486c68SYan, Zheng static void root_add_used(struct btrfs_root *root, u32 size)
988f0486c68SYan, Zheng {
989f0486c68SYan, Zheng 	spin_lock(&root->accounting_lock);
990f0486c68SYan, Zheng 	btrfs_set_root_used(&root->root_item,
991f0486c68SYan, Zheng 			    btrfs_root_used(&root->root_item) + size);
992f0486c68SYan, Zheng 	spin_unlock(&root->accounting_lock);
993f0486c68SYan, Zheng }
994f0486c68SYan, Zheng 
995f0486c68SYan, Zheng static void root_sub_used(struct btrfs_root *root, u32 size)
996f0486c68SYan, Zheng {
997f0486c68SYan, Zheng 	spin_lock(&root->accounting_lock);
998f0486c68SYan, Zheng 	btrfs_set_root_used(&root->root_item,
999f0486c68SYan, Zheng 			    btrfs_root_used(&root->root_item) - size);
1000f0486c68SYan, Zheng 	spin_unlock(&root->accounting_lock);
1001f0486c68SYan, Zheng }
1002f0486c68SYan, Zheng 
1003d352ac68SChris Mason /* given a node and slot number, this reads the blocks it points to.  The
1004d352ac68SChris Mason  * extent buffer is returned with a reference taken (but unlocked).
1005d352ac68SChris Mason  */
10064b231ae4SDavid Sterba struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
10074b231ae4SDavid Sterba 					   int slot)
1008bb803951SChris Mason {
1009ca7a79adSChris Mason 	int level = btrfs_header_level(parent);
1010789d6a3aSQu Wenruo 	struct btrfs_tree_parent_check check = { 0 };
1011416bc658SJosef Bacik 	struct extent_buffer *eb;
1012416bc658SJosef Bacik 
1013fb770ae4SLiu Bo 	if (slot < 0 || slot >= btrfs_header_nritems(parent))
1014fb770ae4SLiu Bo 		return ERR_PTR(-ENOENT);
1015ca7a79adSChris Mason 
1016d4694728SJosef Bacik 	ASSERT(level);
1017ca7a79adSChris Mason 
1018789d6a3aSQu Wenruo 	check.level = level - 1;
1019789d6a3aSQu Wenruo 	check.transid = btrfs_node_ptr_generation(parent, slot);
1020789d6a3aSQu Wenruo 	check.owner_root = btrfs_header_owner(parent);
1021789d6a3aSQu Wenruo 	check.has_first_key = true;
1022789d6a3aSQu Wenruo 	btrfs_node_key_to_cpu(parent, &check.first_key, slot);
1023789d6a3aSQu Wenruo 
1024d0d20b0fSDavid Sterba 	eb = read_tree_block(parent->fs_info, btrfs_node_blockptr(parent, slot),
1025789d6a3aSQu Wenruo 			     &check);
10264eb150d6SQu Wenruo 	if (IS_ERR(eb))
10274eb150d6SQu Wenruo 		return eb;
10284eb150d6SQu Wenruo 	if (!extent_buffer_uptodate(eb)) {
1029416bc658SJosef Bacik 		free_extent_buffer(eb);
10304eb150d6SQu Wenruo 		return ERR_PTR(-EIO);
1031416bc658SJosef Bacik 	}
1032416bc658SJosef Bacik 
1033416bc658SJosef Bacik 	return eb;
1034bb803951SChris Mason }
1035bb803951SChris Mason 
1036d352ac68SChris Mason /*
1037d352ac68SChris Mason  * node level balancing, used to make sure nodes are in proper order for
1038d352ac68SChris Mason  * item deletion.  We balance from the top down, so we have to make sure
1039d352ac68SChris Mason  * that a deletion won't leave an node completely empty later on.
1040d352ac68SChris Mason  */
1041e02119d5SChris Mason static noinline int balance_level(struct btrfs_trans_handle *trans,
104298ed5174SChris Mason 			 struct btrfs_root *root,
104398ed5174SChris Mason 			 struct btrfs_path *path, int level)
1044bb803951SChris Mason {
10450b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
10465f39d397SChris Mason 	struct extent_buffer *right = NULL;
10475f39d397SChris Mason 	struct extent_buffer *mid;
10485f39d397SChris Mason 	struct extent_buffer *left = NULL;
10495f39d397SChris Mason 	struct extent_buffer *parent = NULL;
1050bb803951SChris Mason 	int ret = 0;
1051bb803951SChris Mason 	int wret;
1052bb803951SChris Mason 	int pslot;
1053bb803951SChris Mason 	int orig_slot = path->slots[level];
105479f95c82SChris Mason 	u64 orig_ptr;
1055bb803951SChris Mason 
105698e6b1ebSLiu Bo 	ASSERT(level > 0);
1057bb803951SChris Mason 
10585f39d397SChris Mason 	mid = path->nodes[level];
1059b4ce94deSChris Mason 
1060ac5887c8SJosef Bacik 	WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK);
10617bb86316SChris Mason 	WARN_ON(btrfs_header_generation(mid) != trans->transid);
10627bb86316SChris Mason 
10631d4f8a0cSChris Mason 	orig_ptr = btrfs_node_blockptr(mid, orig_slot);
106479f95c82SChris Mason 
1065a05a9bb1SLi Zefan 	if (level < BTRFS_MAX_LEVEL - 1) {
10665f39d397SChris Mason 		parent = path->nodes[level + 1];
1067bb803951SChris Mason 		pslot = path->slots[level + 1];
1068a05a9bb1SLi Zefan 	}
1069bb803951SChris Mason 
107040689478SChris Mason 	/*
107140689478SChris Mason 	 * deal with the case where there is only one pointer in the root
107240689478SChris Mason 	 * by promoting the node below to a root
107340689478SChris Mason 	 */
10745f39d397SChris Mason 	if (!parent) {
10755f39d397SChris Mason 		struct extent_buffer *child;
1076bb803951SChris Mason 
10775f39d397SChris Mason 		if (btrfs_header_nritems(mid) != 1)
1078bb803951SChris Mason 			return 0;
1079bb803951SChris Mason 
1080bb803951SChris Mason 		/* promote the child to a root */
10814b231ae4SDavid Sterba 		child = btrfs_read_node_slot(mid, 0);
1082fb770ae4SLiu Bo 		if (IS_ERR(child)) {
1083fb770ae4SLiu Bo 			ret = PTR_ERR(child);
1084daefe4d4SFilipe Manana 			goto out;
1085305a26afSMark Fasheh 		}
1086305a26afSMark Fasheh 
1087925baeddSChris Mason 		btrfs_tree_lock(child);
10889631e4ccSJosef Bacik 		ret = btrfs_cow_block(trans, root, child, mid, 0, &child,
10899631e4ccSJosef Bacik 				      BTRFS_NESTING_COW);
1090f0486c68SYan, Zheng 		if (ret) {
1091f0486c68SYan, Zheng 			btrfs_tree_unlock(child);
1092f0486c68SYan, Zheng 			free_extent_buffer(child);
1093daefe4d4SFilipe Manana 			goto out;
1094f0486c68SYan, Zheng 		}
10952f375ab9SYan 
1096406808abSFilipe Manana 		ret = btrfs_tree_mod_log_insert_root(root->node, child, true);
109739020d8aSFilipe Manana 		if (ret < 0) {
109839020d8aSFilipe Manana 			btrfs_tree_unlock(child);
109939020d8aSFilipe Manana 			free_extent_buffer(child);
110039020d8aSFilipe Manana 			btrfs_abort_transaction(trans, ret);
1101daefe4d4SFilipe Manana 			goto out;
110239020d8aSFilipe Manana 		}
1103240f62c8SChris Mason 		rcu_assign_pointer(root->node, child);
1104925baeddSChris Mason 
11050b86a832SChris Mason 		add_root_to_dirty_list(root);
1106925baeddSChris Mason 		btrfs_tree_unlock(child);
1107b4ce94deSChris Mason 
1108925baeddSChris Mason 		path->locks[level] = 0;
1109bb803951SChris Mason 		path->nodes[level] = NULL;
1110190a8339SJosef Bacik 		btrfs_clear_buffer_dirty(trans, mid);
1111925baeddSChris Mason 		btrfs_tree_unlock(mid);
1112bb803951SChris Mason 		/* once for the path */
11135f39d397SChris Mason 		free_extent_buffer(mid);
1114f0486c68SYan, Zheng 
1115f0486c68SYan, Zheng 		root_sub_used(root, mid->len);
11167a163608SFilipe Manana 		btrfs_free_tree_block(trans, btrfs_root_id(root), mid, 0, 1);
1117bb803951SChris Mason 		/* once for the root ptr */
11183083ee2eSJosef Bacik 		free_extent_buffer_stale(mid);
1119f0486c68SYan, Zheng 		return 0;
1120bb803951SChris Mason 	}
11215f39d397SChris Mason 	if (btrfs_header_nritems(mid) >
11220b246afaSJeff Mahoney 	    BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
1123bb803951SChris Mason 		return 0;
1124bb803951SChris Mason 
11259cf14029SJosef Bacik 	if (pslot) {
11264b231ae4SDavid Sterba 		left = btrfs_read_node_slot(parent, pslot - 1);
11279cf14029SJosef Bacik 		if (IS_ERR(left)) {
11289cf14029SJosef Bacik 			ret = PTR_ERR(left);
1129fb770ae4SLiu Bo 			left = NULL;
1130daefe4d4SFilipe Manana 			goto out;
11319cf14029SJosef Bacik 		}
1132fb770ae4SLiu Bo 
1133bf77467aSJosef Bacik 		__btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
11345f39d397SChris Mason 		wret = btrfs_cow_block(trans, root, left,
11359631e4ccSJosef Bacik 				       parent, pslot - 1, &left,
1136bf59a5a2SJosef Bacik 				       BTRFS_NESTING_LEFT_COW);
113754aa1f4dSChris Mason 		if (wret) {
113854aa1f4dSChris Mason 			ret = wret;
1139daefe4d4SFilipe Manana 			goto out;
114054aa1f4dSChris Mason 		}
11412cc58cf2SChris Mason 	}
1142fb770ae4SLiu Bo 
11439cf14029SJosef Bacik 	if (pslot + 1 < btrfs_header_nritems(parent)) {
11444b231ae4SDavid Sterba 		right = btrfs_read_node_slot(parent, pslot + 1);
11459cf14029SJosef Bacik 		if (IS_ERR(right)) {
11469cf14029SJosef Bacik 			ret = PTR_ERR(right);
1147fb770ae4SLiu Bo 			right = NULL;
1148daefe4d4SFilipe Manana 			goto out;
11499cf14029SJosef Bacik 		}
1150fb770ae4SLiu Bo 
1151bf77467aSJosef Bacik 		__btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
11525f39d397SChris Mason 		wret = btrfs_cow_block(trans, root, right,
11539631e4ccSJosef Bacik 				       parent, pslot + 1, &right,
1154bf59a5a2SJosef Bacik 				       BTRFS_NESTING_RIGHT_COW);
11552cc58cf2SChris Mason 		if (wret) {
11562cc58cf2SChris Mason 			ret = wret;
1157daefe4d4SFilipe Manana 			goto out;
11582cc58cf2SChris Mason 		}
11592cc58cf2SChris Mason 	}
11602cc58cf2SChris Mason 
11612cc58cf2SChris Mason 	/* first, try to make some room in the middle buffer */
11625f39d397SChris Mason 	if (left) {
11635f39d397SChris Mason 		orig_slot += btrfs_header_nritems(left);
1164d30a668fSDavid Sterba 		wret = push_node_left(trans, left, mid, 1);
116579f95c82SChris Mason 		if (wret < 0)
116679f95c82SChris Mason 			ret = wret;
1167bb803951SChris Mason 	}
116879f95c82SChris Mason 
116979f95c82SChris Mason 	/*
117079f95c82SChris Mason 	 * then try to empty the right most buffer into the middle
117179f95c82SChris Mason 	 */
11725f39d397SChris Mason 	if (right) {
1173d30a668fSDavid Sterba 		wret = push_node_left(trans, mid, right, 1);
117454aa1f4dSChris Mason 		if (wret < 0 && wret != -ENOSPC)
117579f95c82SChris Mason 			ret = wret;
11765f39d397SChris Mason 		if (btrfs_header_nritems(right) == 0) {
1177190a8339SJosef Bacik 			btrfs_clear_buffer_dirty(trans, right);
1178925baeddSChris Mason 			btrfs_tree_unlock(right);
1179751a2761SFilipe Manana 			ret = btrfs_del_ptr(trans, root, path, level + 1, pslot + 1);
1180751a2761SFilipe Manana 			if (ret < 0) {
1181751a2761SFilipe Manana 				free_extent_buffer_stale(right);
1182751a2761SFilipe Manana 				right = NULL;
1183751a2761SFilipe Manana 				goto out;
1184751a2761SFilipe Manana 			}
1185f0486c68SYan, Zheng 			root_sub_used(root, right->len);
11867a163608SFilipe Manana 			btrfs_free_tree_block(trans, btrfs_root_id(root), right,
11877a163608SFilipe Manana 					      0, 1);
11883083ee2eSJosef Bacik 			free_extent_buffer_stale(right);
1189f0486c68SYan, Zheng 			right = NULL;
1190bb803951SChris Mason 		} else {
11915f39d397SChris Mason 			struct btrfs_disk_key right_key;
11925f39d397SChris Mason 			btrfs_node_key(right, &right_key, 0);
1193f3a84ccdSFilipe Manana 			ret = btrfs_tree_mod_log_insert_key(parent, pslot + 1,
119433cff222SFilipe Manana 					BTRFS_MOD_LOG_KEY_REPLACE);
119539020d8aSFilipe Manana 			if (ret < 0) {
119639020d8aSFilipe Manana 				btrfs_abort_transaction(trans, ret);
1197daefe4d4SFilipe Manana 				goto out;
119839020d8aSFilipe Manana 			}
11995f39d397SChris Mason 			btrfs_set_node_key(parent, &right_key, pslot + 1);
12005f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
1201bb803951SChris Mason 		}
1202bb803951SChris Mason 	}
12035f39d397SChris Mason 	if (btrfs_header_nritems(mid) == 1) {
120479f95c82SChris Mason 		/*
120579f95c82SChris Mason 		 * we're not allowed to leave a node with one item in the
120679f95c82SChris Mason 		 * tree during a delete.  A deletion from lower in the tree
120779f95c82SChris Mason 		 * could try to delete the only pointer in this node.
120879f95c82SChris Mason 		 * So, pull some keys from the left.
120979f95c82SChris Mason 		 * There has to be a left pointer at this point because
121079f95c82SChris Mason 		 * otherwise we would have pulled some pointers from the
121179f95c82SChris Mason 		 * right
121279f95c82SChris Mason 		 */
1213725026edSFilipe Manana 		if (unlikely(!left)) {
1214725026edSFilipe Manana 			btrfs_crit(fs_info,
1215725026edSFilipe Manana "missing left child when middle child only has 1 item, parent bytenr %llu level %d mid bytenr %llu root %llu",
1216725026edSFilipe Manana 				   parent->start, btrfs_header_level(parent),
1217725026edSFilipe Manana 				   mid->start, btrfs_root_id(root));
1218725026edSFilipe Manana 			ret = -EUCLEAN;
1219725026edSFilipe Manana 			btrfs_abort_transaction(trans, ret);
1220daefe4d4SFilipe Manana 			goto out;
1221305a26afSMark Fasheh 		}
122255d32ed8SDavid Sterba 		wret = balance_node_right(trans, mid, left);
122354aa1f4dSChris Mason 		if (wret < 0) {
122479f95c82SChris Mason 			ret = wret;
1225daefe4d4SFilipe Manana 			goto out;
122654aa1f4dSChris Mason 		}
1227bce4eae9SChris Mason 		if (wret == 1) {
1228d30a668fSDavid Sterba 			wret = push_node_left(trans, left, mid, 1);
1229bce4eae9SChris Mason 			if (wret < 0)
1230bce4eae9SChris Mason 				ret = wret;
1231bce4eae9SChris Mason 		}
123279f95c82SChris Mason 		BUG_ON(wret == 1);
123379f95c82SChris Mason 	}
12345f39d397SChris Mason 	if (btrfs_header_nritems(mid) == 0) {
1235190a8339SJosef Bacik 		btrfs_clear_buffer_dirty(trans, mid);
1236925baeddSChris Mason 		btrfs_tree_unlock(mid);
1237751a2761SFilipe Manana 		ret = btrfs_del_ptr(trans, root, path, level + 1, pslot);
1238751a2761SFilipe Manana 		if (ret < 0) {
1239751a2761SFilipe Manana 			free_extent_buffer_stale(mid);
1240751a2761SFilipe Manana 			mid = NULL;
1241751a2761SFilipe Manana 			goto out;
1242751a2761SFilipe Manana 		}
1243f0486c68SYan, Zheng 		root_sub_used(root, mid->len);
12447a163608SFilipe Manana 		btrfs_free_tree_block(trans, btrfs_root_id(root), mid, 0, 1);
12453083ee2eSJosef Bacik 		free_extent_buffer_stale(mid);
1246f0486c68SYan, Zheng 		mid = NULL;
124779f95c82SChris Mason 	} else {
124879f95c82SChris Mason 		/* update the parent key to reflect our changes */
12495f39d397SChris Mason 		struct btrfs_disk_key mid_key;
12505f39d397SChris Mason 		btrfs_node_key(mid, &mid_key, 0);
1251f3a84ccdSFilipe Manana 		ret = btrfs_tree_mod_log_insert_key(parent, pslot,
125233cff222SFilipe Manana 						    BTRFS_MOD_LOG_KEY_REPLACE);
125339020d8aSFilipe Manana 		if (ret < 0) {
125439020d8aSFilipe Manana 			btrfs_abort_transaction(trans, ret);
1255daefe4d4SFilipe Manana 			goto out;
125639020d8aSFilipe Manana 		}
12575f39d397SChris Mason 		btrfs_set_node_key(parent, &mid_key, pslot);
12585f39d397SChris Mason 		btrfs_mark_buffer_dirty(parent);
125979f95c82SChris Mason 	}
1260bb803951SChris Mason 
126179f95c82SChris Mason 	/* update the path */
12625f39d397SChris Mason 	if (left) {
12635f39d397SChris Mason 		if (btrfs_header_nritems(left) > orig_slot) {
126467439dadSDavid Sterba 			atomic_inc(&left->refs);
1265925baeddSChris Mason 			/* left was locked after cow */
12665f39d397SChris Mason 			path->nodes[level] = left;
1267bb803951SChris Mason 			path->slots[level + 1] -= 1;
1268bb803951SChris Mason 			path->slots[level] = orig_slot;
1269925baeddSChris Mason 			if (mid) {
1270925baeddSChris Mason 				btrfs_tree_unlock(mid);
12715f39d397SChris Mason 				free_extent_buffer(mid);
1272925baeddSChris Mason 			}
1273bb803951SChris Mason 		} else {
12745f39d397SChris Mason 			orig_slot -= btrfs_header_nritems(left);
1275bb803951SChris Mason 			path->slots[level] = orig_slot;
1276bb803951SChris Mason 		}
1277bb803951SChris Mason 	}
127879f95c82SChris Mason 	/* double check we haven't messed things up */
1279e20d96d6SChris Mason 	if (orig_ptr !=
12805f39d397SChris Mason 	    btrfs_node_blockptr(path->nodes[level], path->slots[level]))
128179f95c82SChris Mason 		BUG();
1282daefe4d4SFilipe Manana out:
1283925baeddSChris Mason 	if (right) {
1284925baeddSChris Mason 		btrfs_tree_unlock(right);
12855f39d397SChris Mason 		free_extent_buffer(right);
1286925baeddSChris Mason 	}
1287925baeddSChris Mason 	if (left) {
1288925baeddSChris Mason 		if (path->nodes[level] != left)
1289925baeddSChris Mason 			btrfs_tree_unlock(left);
12905f39d397SChris Mason 		free_extent_buffer(left);
1291925baeddSChris Mason 	}
1292bb803951SChris Mason 	return ret;
1293bb803951SChris Mason }
1294bb803951SChris Mason 
1295d352ac68SChris Mason /* Node balancing for insertion.  Here we only split or push nodes around
1296d352ac68SChris Mason  * when they are completely full.  This is also done top down, so we
1297d352ac68SChris Mason  * have to be pessimistic.
1298d352ac68SChris Mason  */
1299d397712bSChris Mason static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1300e66f709bSChris Mason 					  struct btrfs_root *root,
1301e66f709bSChris Mason 					  struct btrfs_path *path, int level)
1302e66f709bSChris Mason {
13030b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
13045f39d397SChris Mason 	struct extent_buffer *right = NULL;
13055f39d397SChris Mason 	struct extent_buffer *mid;
13065f39d397SChris Mason 	struct extent_buffer *left = NULL;
13075f39d397SChris Mason 	struct extent_buffer *parent = NULL;
1308e66f709bSChris Mason 	int ret = 0;
1309e66f709bSChris Mason 	int wret;
1310e66f709bSChris Mason 	int pslot;
1311e66f709bSChris Mason 	int orig_slot = path->slots[level];
1312e66f709bSChris Mason 
1313e66f709bSChris Mason 	if (level == 0)
1314e66f709bSChris Mason 		return 1;
1315e66f709bSChris Mason 
13165f39d397SChris Mason 	mid = path->nodes[level];
13177bb86316SChris Mason 	WARN_ON(btrfs_header_generation(mid) != trans->transid);
1318e66f709bSChris Mason 
1319a05a9bb1SLi Zefan 	if (level < BTRFS_MAX_LEVEL - 1) {
13205f39d397SChris Mason 		parent = path->nodes[level + 1];
1321e66f709bSChris Mason 		pslot = path->slots[level + 1];
1322a05a9bb1SLi Zefan 	}
1323e66f709bSChris Mason 
13245f39d397SChris Mason 	if (!parent)
1325e66f709bSChris Mason 		return 1;
1326e66f709bSChris Mason 
13279cf14029SJosef Bacik 	/* first, try to make some room in the middle buffer */
13289cf14029SJosef Bacik 	if (pslot) {
13299cf14029SJosef Bacik 		u32 left_nr;
13309cf14029SJosef Bacik 
13314b231ae4SDavid Sterba 		left = btrfs_read_node_slot(parent, pslot - 1);
1332fb770ae4SLiu Bo 		if (IS_ERR(left))
13339cf14029SJosef Bacik 			return PTR_ERR(left);
1334925baeddSChris Mason 
1335bf77467aSJosef Bacik 		__btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
1336b4ce94deSChris Mason 
13375f39d397SChris Mason 		left_nr = btrfs_header_nritems(left);
13380b246afaSJeff Mahoney 		if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
133933ade1f8SChris Mason 			wret = 1;
134033ade1f8SChris Mason 		} else {
13415f39d397SChris Mason 			ret = btrfs_cow_block(trans, root, left, parent,
13429631e4ccSJosef Bacik 					      pslot - 1, &left,
1343bf59a5a2SJosef Bacik 					      BTRFS_NESTING_LEFT_COW);
134454aa1f4dSChris Mason 			if (ret)
134554aa1f4dSChris Mason 				wret = 1;
134654aa1f4dSChris Mason 			else {
1347d30a668fSDavid Sterba 				wret = push_node_left(trans, left, mid, 0);
134854aa1f4dSChris Mason 			}
134933ade1f8SChris Mason 		}
1350e66f709bSChris Mason 		if (wret < 0)
1351e66f709bSChris Mason 			ret = wret;
1352e66f709bSChris Mason 		if (wret == 0) {
13535f39d397SChris Mason 			struct btrfs_disk_key disk_key;
1354e66f709bSChris Mason 			orig_slot += left_nr;
13555f39d397SChris Mason 			btrfs_node_key(mid, &disk_key, 0);
1356f3a84ccdSFilipe Manana 			ret = btrfs_tree_mod_log_insert_key(parent, pslot,
135733cff222SFilipe Manana 					BTRFS_MOD_LOG_KEY_REPLACE);
135811d6ae03SFilipe Manana 			if (ret < 0) {
135911d6ae03SFilipe Manana 				btrfs_tree_unlock(left);
136011d6ae03SFilipe Manana 				free_extent_buffer(left);
136111d6ae03SFilipe Manana 				btrfs_abort_transaction(trans, ret);
136211d6ae03SFilipe Manana 				return ret;
136311d6ae03SFilipe Manana 			}
13645f39d397SChris Mason 			btrfs_set_node_key(parent, &disk_key, pslot);
13655f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
13665f39d397SChris Mason 			if (btrfs_header_nritems(left) > orig_slot) {
13675f39d397SChris Mason 				path->nodes[level] = left;
1368e66f709bSChris Mason 				path->slots[level + 1] -= 1;
1369e66f709bSChris Mason 				path->slots[level] = orig_slot;
1370925baeddSChris Mason 				btrfs_tree_unlock(mid);
13715f39d397SChris Mason 				free_extent_buffer(mid);
1372e66f709bSChris Mason 			} else {
1373e66f709bSChris Mason 				orig_slot -=
13745f39d397SChris Mason 					btrfs_header_nritems(left);
1375e66f709bSChris Mason 				path->slots[level] = orig_slot;
1376925baeddSChris Mason 				btrfs_tree_unlock(left);
13775f39d397SChris Mason 				free_extent_buffer(left);
1378e66f709bSChris Mason 			}
1379e66f709bSChris Mason 			return 0;
1380e66f709bSChris Mason 		}
1381925baeddSChris Mason 		btrfs_tree_unlock(left);
13825f39d397SChris Mason 		free_extent_buffer(left);
1383e66f709bSChris Mason 	}
1384e66f709bSChris Mason 
1385e66f709bSChris Mason 	/*
1386e66f709bSChris Mason 	 * then try to empty the right most buffer into the middle
1387e66f709bSChris Mason 	 */
13889cf14029SJosef Bacik 	if (pslot + 1 < btrfs_header_nritems(parent)) {
138933ade1f8SChris Mason 		u32 right_nr;
1390b4ce94deSChris Mason 
13919cf14029SJosef Bacik 		right = btrfs_read_node_slot(parent, pslot + 1);
13929cf14029SJosef Bacik 		if (IS_ERR(right))
13939cf14029SJosef Bacik 			return PTR_ERR(right);
13949cf14029SJosef Bacik 
1395bf77467aSJosef Bacik 		__btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
1396b4ce94deSChris Mason 
13975f39d397SChris Mason 		right_nr = btrfs_header_nritems(right);
13980b246afaSJeff Mahoney 		if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
139933ade1f8SChris Mason 			wret = 1;
140033ade1f8SChris Mason 		} else {
14015f39d397SChris Mason 			ret = btrfs_cow_block(trans, root, right,
14025f39d397SChris Mason 					      parent, pslot + 1,
1403bf59a5a2SJosef Bacik 					      &right, BTRFS_NESTING_RIGHT_COW);
140454aa1f4dSChris Mason 			if (ret)
140554aa1f4dSChris Mason 				wret = 1;
140654aa1f4dSChris Mason 			else {
140755d32ed8SDavid Sterba 				wret = balance_node_right(trans, right, mid);
140833ade1f8SChris Mason 			}
140954aa1f4dSChris Mason 		}
1410e66f709bSChris Mason 		if (wret < 0)
1411e66f709bSChris Mason 			ret = wret;
1412e66f709bSChris Mason 		if (wret == 0) {
14135f39d397SChris Mason 			struct btrfs_disk_key disk_key;
14145f39d397SChris Mason 
14155f39d397SChris Mason 			btrfs_node_key(right, &disk_key, 0);
1416f3a84ccdSFilipe Manana 			ret = btrfs_tree_mod_log_insert_key(parent, pslot + 1,
141733cff222SFilipe Manana 					BTRFS_MOD_LOG_KEY_REPLACE);
141811d6ae03SFilipe Manana 			if (ret < 0) {
141911d6ae03SFilipe Manana 				btrfs_tree_unlock(right);
142011d6ae03SFilipe Manana 				free_extent_buffer(right);
142111d6ae03SFilipe Manana 				btrfs_abort_transaction(trans, ret);
142211d6ae03SFilipe Manana 				return ret;
142311d6ae03SFilipe Manana 			}
14245f39d397SChris Mason 			btrfs_set_node_key(parent, &disk_key, pslot + 1);
14255f39d397SChris Mason 			btrfs_mark_buffer_dirty(parent);
14265f39d397SChris Mason 
14275f39d397SChris Mason 			if (btrfs_header_nritems(mid) <= orig_slot) {
14285f39d397SChris Mason 				path->nodes[level] = right;
1429e66f709bSChris Mason 				path->slots[level + 1] += 1;
1430e66f709bSChris Mason 				path->slots[level] = orig_slot -
14315f39d397SChris Mason 					btrfs_header_nritems(mid);
1432925baeddSChris Mason 				btrfs_tree_unlock(mid);
14335f39d397SChris Mason 				free_extent_buffer(mid);
1434e66f709bSChris Mason 			} else {
1435925baeddSChris Mason 				btrfs_tree_unlock(right);
14365f39d397SChris Mason 				free_extent_buffer(right);
1437e66f709bSChris Mason 			}
1438e66f709bSChris Mason 			return 0;
1439e66f709bSChris Mason 		}
1440925baeddSChris Mason 		btrfs_tree_unlock(right);
14415f39d397SChris Mason 		free_extent_buffer(right);
1442e66f709bSChris Mason 	}
1443e66f709bSChris Mason 	return 1;
1444e66f709bSChris Mason }
1445e66f709bSChris Mason 
144674123bd7SChris Mason /*
1447d352ac68SChris Mason  * readahead one full node of leaves, finding things that are close
1448d352ac68SChris Mason  * to the block in 'slot', and triggering ra on them.
14493c69faecSChris Mason  */
14502ff7e61eSJeff Mahoney static void reada_for_search(struct btrfs_fs_info *fs_info,
1451e02119d5SChris Mason 			     struct btrfs_path *path,
145201f46658SChris Mason 			     int level, int slot, u64 objectid)
14533c69faecSChris Mason {
14545f39d397SChris Mason 	struct extent_buffer *node;
145501f46658SChris Mason 	struct btrfs_disk_key disk_key;
14563c69faecSChris Mason 	u32 nritems;
14573c69faecSChris Mason 	u64 search;
1458a7175319SChris Mason 	u64 target;
14596b80053dSChris Mason 	u64 nread = 0;
1460ace75066SFilipe Manana 	u64 nread_max;
14616b80053dSChris Mason 	u32 nr;
14626b80053dSChris Mason 	u32 blocksize;
14636b80053dSChris Mason 	u32 nscan = 0;
1464db94535dSChris Mason 
1465ace75066SFilipe Manana 	if (level != 1 && path->reada != READA_FORWARD_ALWAYS)
14663c69faecSChris Mason 		return;
14673c69faecSChris Mason 
14686702ed49SChris Mason 	if (!path->nodes[level])
14696702ed49SChris Mason 		return;
14706702ed49SChris Mason 
14715f39d397SChris Mason 	node = path->nodes[level];
1472925baeddSChris Mason 
1473ace75066SFilipe Manana 	/*
1474ace75066SFilipe Manana 	 * Since the time between visiting leaves is much shorter than the time
1475ace75066SFilipe Manana 	 * between visiting nodes, limit read ahead of nodes to 1, to avoid too
1476ace75066SFilipe Manana 	 * much IO at once (possibly random).
1477ace75066SFilipe Manana 	 */
1478ace75066SFilipe Manana 	if (path->reada == READA_FORWARD_ALWAYS) {
1479ace75066SFilipe Manana 		if (level > 1)
1480ace75066SFilipe Manana 			nread_max = node->fs_info->nodesize;
1481ace75066SFilipe Manana 		else
1482ace75066SFilipe Manana 			nread_max = SZ_128K;
1483ace75066SFilipe Manana 	} else {
1484ace75066SFilipe Manana 		nread_max = SZ_64K;
1485ace75066SFilipe Manana 	}
1486ace75066SFilipe Manana 
14873c69faecSChris Mason 	search = btrfs_node_blockptr(node, slot);
14880b246afaSJeff Mahoney 	blocksize = fs_info->nodesize;
1489069a2e37SFilipe Manana 	if (path->reada != READA_FORWARD_ALWAYS) {
1490069a2e37SFilipe Manana 		struct extent_buffer *eb;
1491069a2e37SFilipe Manana 
14920b246afaSJeff Mahoney 		eb = find_extent_buffer(fs_info, search);
14935f39d397SChris Mason 		if (eb) {
14945f39d397SChris Mason 			free_extent_buffer(eb);
14953c69faecSChris Mason 			return;
14963c69faecSChris Mason 		}
1497069a2e37SFilipe Manana 	}
14983c69faecSChris Mason 
1499a7175319SChris Mason 	target = search;
15006b80053dSChris Mason 
15015f39d397SChris Mason 	nritems = btrfs_header_nritems(node);
15026b80053dSChris Mason 	nr = slot;
150325b8b936SJosef Bacik 
15043c69faecSChris Mason 	while (1) {
1505e4058b54SDavid Sterba 		if (path->reada == READA_BACK) {
15066b80053dSChris Mason 			if (nr == 0)
15073c69faecSChris Mason 				break;
15086b80053dSChris Mason 			nr--;
1509ace75066SFilipe Manana 		} else if (path->reada == READA_FORWARD ||
1510ace75066SFilipe Manana 			   path->reada == READA_FORWARD_ALWAYS) {
15116b80053dSChris Mason 			nr++;
15126b80053dSChris Mason 			if (nr >= nritems)
15136b80053dSChris Mason 				break;
15143c69faecSChris Mason 		}
1515e4058b54SDavid Sterba 		if (path->reada == READA_BACK && objectid) {
151601f46658SChris Mason 			btrfs_node_key(node, &disk_key, nr);
151701f46658SChris Mason 			if (btrfs_disk_key_objectid(&disk_key) != objectid)
151801f46658SChris Mason 				break;
151901f46658SChris Mason 		}
15206b80053dSChris Mason 		search = btrfs_node_blockptr(node, nr);
1521ace75066SFilipe Manana 		if (path->reada == READA_FORWARD_ALWAYS ||
1522ace75066SFilipe Manana 		    (search <= target && target - search <= 65536) ||
1523a7175319SChris Mason 		    (search > target && search - target <= 65536)) {
1524bfb484d9SJosef Bacik 			btrfs_readahead_node_child(node, nr);
15256b80053dSChris Mason 			nread += blocksize;
15263c69faecSChris Mason 		}
15276b80053dSChris Mason 		nscan++;
1528ace75066SFilipe Manana 		if (nread > nread_max || nscan > 32)
15296b80053dSChris Mason 			break;
15303c69faecSChris Mason 	}
15313c69faecSChris Mason }
1532925baeddSChris Mason 
1533bfb484d9SJosef Bacik static noinline void reada_for_balance(struct btrfs_path *path, int level)
1534b4ce94deSChris Mason {
1535bfb484d9SJosef Bacik 	struct extent_buffer *parent;
1536b4ce94deSChris Mason 	int slot;
1537b4ce94deSChris Mason 	int nritems;
1538b4ce94deSChris Mason 
15398c594ea8SChris Mason 	parent = path->nodes[level + 1];
1540b4ce94deSChris Mason 	if (!parent)
15410b08851fSJosef Bacik 		return;
1542b4ce94deSChris Mason 
1543b4ce94deSChris Mason 	nritems = btrfs_header_nritems(parent);
15448c594ea8SChris Mason 	slot = path->slots[level + 1];
1545b4ce94deSChris Mason 
1546bfb484d9SJosef Bacik 	if (slot > 0)
1547bfb484d9SJosef Bacik 		btrfs_readahead_node_child(parent, slot - 1);
1548bfb484d9SJosef Bacik 	if (slot + 1 < nritems)
1549bfb484d9SJosef Bacik 		btrfs_readahead_node_child(parent, slot + 1);
1550b4ce94deSChris Mason }
1551b4ce94deSChris Mason 
1552b4ce94deSChris Mason 
1553b4ce94deSChris Mason /*
1554d397712bSChris Mason  * when we walk down the tree, it is usually safe to unlock the higher layers
1555d397712bSChris Mason  * in the tree.  The exceptions are when our path goes through slot 0, because
1556d397712bSChris Mason  * operations on the tree might require changing key pointers higher up in the
1557d397712bSChris Mason  * tree.
1558d352ac68SChris Mason  *
1559d397712bSChris Mason  * callers might also have set path->keep_locks, which tells this code to keep
1560d397712bSChris Mason  * the lock if the path points to the last slot in the block.  This is part of
1561d397712bSChris Mason  * walking through the tree, and selecting the next slot in the higher block.
1562d352ac68SChris Mason  *
1563d397712bSChris Mason  * lowest_unlock sets the lowest level in the tree we're allowed to unlock.  so
1564d397712bSChris Mason  * if lowest_unlock is 1, level 0 won't be unlocked
1565d352ac68SChris Mason  */
1566e02119d5SChris Mason static noinline void unlock_up(struct btrfs_path *path, int level,
1567f7c79f30SChris Mason 			       int lowest_unlock, int min_write_lock_level,
1568f7c79f30SChris Mason 			       int *write_lock_level)
1569925baeddSChris Mason {
1570925baeddSChris Mason 	int i;
1571925baeddSChris Mason 	int skip_level = level;
1572c1227996SNikolay Borisov 	bool check_skip = true;
1573925baeddSChris Mason 
1574925baeddSChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1575925baeddSChris Mason 		if (!path->nodes[i])
1576925baeddSChris Mason 			break;
1577925baeddSChris Mason 		if (!path->locks[i])
1578925baeddSChris Mason 			break;
1579c1227996SNikolay Borisov 
1580c1227996SNikolay Borisov 		if (check_skip) {
1581c1227996SNikolay Borisov 			if (path->slots[i] == 0) {
1582925baeddSChris Mason 				skip_level = i + 1;
1583925baeddSChris Mason 				continue;
1584925baeddSChris Mason 			}
1585c1227996SNikolay Borisov 
1586c1227996SNikolay Borisov 			if (path->keep_locks) {
1587925baeddSChris Mason 				u32 nritems;
1588c1227996SNikolay Borisov 
1589c1227996SNikolay Borisov 				nritems = btrfs_header_nritems(path->nodes[i]);
1590051e1b9fSChris Mason 				if (nritems < 1 || path->slots[i] >= nritems - 1) {
1591925baeddSChris Mason 					skip_level = i + 1;
1592925baeddSChris Mason 					continue;
1593925baeddSChris Mason 				}
1594925baeddSChris Mason 			}
1595c1227996SNikolay Borisov 		}
1596051e1b9fSChris Mason 
1597d80bb3f9SLiu Bo 		if (i >= lowest_unlock && i > skip_level) {
1598c1227996SNikolay Borisov 			check_skip = false;
1599c1227996SNikolay Borisov 			btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
1600925baeddSChris Mason 			path->locks[i] = 0;
1601f7c79f30SChris Mason 			if (write_lock_level &&
1602f7c79f30SChris Mason 			    i > min_write_lock_level &&
1603f7c79f30SChris Mason 			    i <= *write_lock_level) {
1604f7c79f30SChris Mason 				*write_lock_level = i - 1;
1605f7c79f30SChris Mason 			}
1606925baeddSChris Mason 		}
1607925baeddSChris Mason 	}
1608925baeddSChris Mason }
1609925baeddSChris Mason 
16103c69faecSChris Mason /*
1611376a21d7SFilipe Manana  * Helper function for btrfs_search_slot() and other functions that do a search
1612376a21d7SFilipe Manana  * on a btree. The goal is to find a tree block in the cache (the radix tree at
1613376a21d7SFilipe Manana  * fs_info->buffer_radix), but if we can't find it, or it's not up to date, read
1614376a21d7SFilipe Manana  * its pages from disk.
1615c8c42864SChris Mason  *
1616376a21d7SFilipe Manana  * Returns -EAGAIN, with the path unlocked, if the caller needs to repeat the
1617376a21d7SFilipe Manana  * whole btree search, starting again from the current root node.
1618c8c42864SChris Mason  */
1619c8c42864SChris Mason static int
1620d07b8528SLiu Bo read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
1621c8c42864SChris Mason 		      struct extent_buffer **eb_ret, int level, int slot,
1622cda79c54SDavid Sterba 		      const struct btrfs_key *key)
1623c8c42864SChris Mason {
16240b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
1625789d6a3aSQu Wenruo 	struct btrfs_tree_parent_check check = { 0 };
1626c8c42864SChris Mason 	u64 blocknr;
1627c8c42864SChris Mason 	u64 gen;
1628c8c42864SChris Mason 	struct extent_buffer *tmp;
162976a05b35SChris Mason 	int ret;
1630581c1760SQu Wenruo 	int parent_level;
1631b246666eSFilipe Manana 	bool unlock_up;
1632c8c42864SChris Mason 
1633b246666eSFilipe Manana 	unlock_up = ((level + 1 < BTRFS_MAX_LEVEL) && p->locks[level + 1]);
1634213ff4b7SNikolay Borisov 	blocknr = btrfs_node_blockptr(*eb_ret, slot);
1635213ff4b7SNikolay Borisov 	gen = btrfs_node_ptr_generation(*eb_ret, slot);
1636213ff4b7SNikolay Borisov 	parent_level = btrfs_header_level(*eb_ret);
1637789d6a3aSQu Wenruo 	btrfs_node_key_to_cpu(*eb_ret, &check.first_key, slot);
1638789d6a3aSQu Wenruo 	check.has_first_key = true;
1639789d6a3aSQu Wenruo 	check.level = parent_level - 1;
1640789d6a3aSQu Wenruo 	check.transid = gen;
1641789d6a3aSQu Wenruo 	check.owner_root = root->root_key.objectid;
1642c8c42864SChris Mason 
1643b246666eSFilipe Manana 	/*
1644b246666eSFilipe Manana 	 * If we need to read an extent buffer from disk and we are holding locks
1645b246666eSFilipe Manana 	 * on upper level nodes, we unlock all the upper nodes before reading the
1646b246666eSFilipe Manana 	 * extent buffer, and then return -EAGAIN to the caller as it needs to
1647b246666eSFilipe Manana 	 * restart the search. We don't release the lock on the current level
1648b246666eSFilipe Manana 	 * because we need to walk this node to figure out which blocks to read.
1649b246666eSFilipe Manana 	 */
16500b246afaSJeff Mahoney 	tmp = find_extent_buffer(fs_info, blocknr);
1651cb44921aSChris Mason 	if (tmp) {
1652ace75066SFilipe Manana 		if (p->reada == READA_FORWARD_ALWAYS)
1653ace75066SFilipe Manana 			reada_for_search(fs_info, p, level, slot, key->objectid);
1654ace75066SFilipe Manana 
1655b9fab919SChris Mason 		/* first we do an atomic uptodate check */
1656b9fab919SChris Mason 		if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
1657448de471SQu Wenruo 			/*
1658448de471SQu Wenruo 			 * Do extra check for first_key, eb can be stale due to
1659448de471SQu Wenruo 			 * being cached, read from scrub, or have multiple
1660448de471SQu Wenruo 			 * parents (shared tree blocks).
1661448de471SQu Wenruo 			 */
1662e064d5e9SDavid Sterba 			if (btrfs_verify_level_key(tmp,
1663789d6a3aSQu Wenruo 					parent_level - 1, &check.first_key, gen)) {
1664448de471SQu Wenruo 				free_extent_buffer(tmp);
1665448de471SQu Wenruo 				return -EUCLEAN;
1666448de471SQu Wenruo 			}
1667c8c42864SChris Mason 			*eb_ret = tmp;
1668c8c42864SChris Mason 			return 0;
1669c8c42864SChris Mason 		}
1670bdf7c00eSJosef Bacik 
1671857bc13fSJosef Bacik 		if (p->nowait) {
1672857bc13fSJosef Bacik 			free_extent_buffer(tmp);
1673857bc13fSJosef Bacik 			return -EAGAIN;
1674857bc13fSJosef Bacik 		}
1675857bc13fSJosef Bacik 
1676b246666eSFilipe Manana 		if (unlock_up)
1677b246666eSFilipe Manana 			btrfs_unlock_up_safe(p, level + 1);
1678b246666eSFilipe Manana 
1679b9fab919SChris Mason 		/* now we're allowed to do a blocking uptodate check */
1680789d6a3aSQu Wenruo 		ret = btrfs_read_extent_buffer(tmp, &check);
16819a4ffa1bSQu Wenruo 		if (ret) {
1682cb44921aSChris Mason 			free_extent_buffer(tmp);
1683b3b4aa74SDavid Sterba 			btrfs_release_path(p);
1684cb44921aSChris Mason 			return -EIO;
1685cb44921aSChris Mason 		}
168688c602abSQu Wenruo 		if (btrfs_check_eb_owner(tmp, root->root_key.objectid)) {
168788c602abSQu Wenruo 			free_extent_buffer(tmp);
168888c602abSQu Wenruo 			btrfs_release_path(p);
168988c602abSQu Wenruo 			return -EUCLEAN;
169088c602abSQu Wenruo 		}
1691b246666eSFilipe Manana 
1692b246666eSFilipe Manana 		if (unlock_up)
1693b246666eSFilipe Manana 			ret = -EAGAIN;
1694b246666eSFilipe Manana 
1695b246666eSFilipe Manana 		goto out;
1696857bc13fSJosef Bacik 	} else if (p->nowait) {
1697857bc13fSJosef Bacik 		return -EAGAIN;
16989a4ffa1bSQu Wenruo 	}
1699c8c42864SChris Mason 
1700b246666eSFilipe Manana 	if (unlock_up) {
17018c594ea8SChris Mason 		btrfs_unlock_up_safe(p, level + 1);
17024bb59055SFilipe Manana 		ret = -EAGAIN;
17034bb59055SFilipe Manana 	} else {
17044bb59055SFilipe Manana 		ret = 0;
17054bb59055SFilipe Manana 	}
17068c594ea8SChris Mason 
1707e4058b54SDavid Sterba 	if (p->reada != READA_NONE)
17082ff7e61eSJeff Mahoney 		reada_for_search(fs_info, p, level, slot, key->objectid);
1709c8c42864SChris Mason 
1710789d6a3aSQu Wenruo 	tmp = read_tree_block(fs_info, blocknr, &check);
17114eb150d6SQu Wenruo 	if (IS_ERR(tmp)) {
17124eb150d6SQu Wenruo 		btrfs_release_path(p);
17134eb150d6SQu Wenruo 		return PTR_ERR(tmp);
17144eb150d6SQu Wenruo 	}
171576a05b35SChris Mason 	/*
171676a05b35SChris Mason 	 * If the read above didn't mark this buffer up to date,
171776a05b35SChris Mason 	 * it will never end up being up to date.  Set ret to EIO now
171876a05b35SChris Mason 	 * and give up so that our caller doesn't loop forever
171976a05b35SChris Mason 	 * on our EAGAINs.
172076a05b35SChris Mason 	 */
1721e6a1d6fdSLiu Bo 	if (!extent_buffer_uptodate(tmp))
172276a05b35SChris Mason 		ret = -EIO;
172302a3307aSLiu Bo 
1724b246666eSFilipe Manana out:
17254bb59055SFilipe Manana 	if (ret == 0) {
17264bb59055SFilipe Manana 		*eb_ret = tmp;
17274bb59055SFilipe Manana 	} else {
17284bb59055SFilipe Manana 		free_extent_buffer(tmp);
172902a3307aSLiu Bo 		btrfs_release_path(p);
17304bb59055SFilipe Manana 	}
17314bb59055SFilipe Manana 
173276a05b35SChris Mason 	return ret;
1733c8c42864SChris Mason }
1734c8c42864SChris Mason 
1735c8c42864SChris Mason /*
1736c8c42864SChris Mason  * helper function for btrfs_search_slot.  This does all of the checks
1737c8c42864SChris Mason  * for node-level blocks and does any balancing required based on
1738c8c42864SChris Mason  * the ins_len.
1739c8c42864SChris Mason  *
1740c8c42864SChris Mason  * If no extra work was required, zero is returned.  If we had to
1741c8c42864SChris Mason  * drop the path, -EAGAIN is returned and btrfs_search_slot must
1742c8c42864SChris Mason  * start over
1743c8c42864SChris Mason  */
1744c8c42864SChris Mason static int
1745c8c42864SChris Mason setup_nodes_for_search(struct btrfs_trans_handle *trans,
1746c8c42864SChris Mason 		       struct btrfs_root *root, struct btrfs_path *p,
1747bd681513SChris Mason 		       struct extent_buffer *b, int level, int ins_len,
1748bd681513SChris Mason 		       int *write_lock_level)
1749c8c42864SChris Mason {
17500b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
175195b982deSNikolay Borisov 	int ret = 0;
17520b246afaSJeff Mahoney 
1753c8c42864SChris Mason 	if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
17540b246afaSJeff Mahoney 	    BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
1755c8c42864SChris Mason 
1756bd681513SChris Mason 		if (*write_lock_level < level + 1) {
1757bd681513SChris Mason 			*write_lock_level = level + 1;
1758bd681513SChris Mason 			btrfs_release_path(p);
175995b982deSNikolay Borisov 			return -EAGAIN;
1760bd681513SChris Mason 		}
1761bd681513SChris Mason 
1762bfb484d9SJosef Bacik 		reada_for_balance(p, level);
176395b982deSNikolay Borisov 		ret = split_node(trans, root, p, level);
1764c8c42864SChris Mason 
1765c8c42864SChris Mason 		b = p->nodes[level];
1766c8c42864SChris Mason 	} else if (ins_len < 0 && btrfs_header_nritems(b) <
17670b246afaSJeff Mahoney 		   BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
1768c8c42864SChris Mason 
1769bd681513SChris Mason 		if (*write_lock_level < level + 1) {
1770bd681513SChris Mason 			*write_lock_level = level + 1;
1771bd681513SChris Mason 			btrfs_release_path(p);
177295b982deSNikolay Borisov 			return -EAGAIN;
1773bd681513SChris Mason 		}
1774bd681513SChris Mason 
1775bfb484d9SJosef Bacik 		reada_for_balance(p, level);
177695b982deSNikolay Borisov 		ret = balance_level(trans, root, p, level);
177795b982deSNikolay Borisov 		if (ret)
177895b982deSNikolay Borisov 			return ret;
1779c8c42864SChris Mason 
1780c8c42864SChris Mason 		b = p->nodes[level];
1781c8c42864SChris Mason 		if (!b) {
1782b3b4aa74SDavid Sterba 			btrfs_release_path(p);
178395b982deSNikolay Borisov 			return -EAGAIN;
1784c8c42864SChris Mason 		}
1785c8c42864SChris Mason 		BUG_ON(btrfs_header_nritems(b) == 1);
1786c8c42864SChris Mason 	}
1787c8c42864SChris Mason 	return ret;
1788c8c42864SChris Mason }
1789c8c42864SChris Mason 
1790381cf658SDavid Sterba int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
1791e33d5c3dSKelley Nielsen 		u64 iobjectid, u64 ioff, u8 key_type,
1792e33d5c3dSKelley Nielsen 		struct btrfs_key *found_key)
1793e33d5c3dSKelley Nielsen {
1794e33d5c3dSKelley Nielsen 	int ret;
1795e33d5c3dSKelley Nielsen 	struct btrfs_key key;
1796e33d5c3dSKelley Nielsen 	struct extent_buffer *eb;
1797381cf658SDavid Sterba 
1798381cf658SDavid Sterba 	ASSERT(path);
17991d4c08e0SDavid Sterba 	ASSERT(found_key);
1800e33d5c3dSKelley Nielsen 
1801e33d5c3dSKelley Nielsen 	key.type = key_type;
1802e33d5c3dSKelley Nielsen 	key.objectid = iobjectid;
1803e33d5c3dSKelley Nielsen 	key.offset = ioff;
1804e33d5c3dSKelley Nielsen 
1805e33d5c3dSKelley Nielsen 	ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
18061d4c08e0SDavid Sterba 	if (ret < 0)
1807e33d5c3dSKelley Nielsen 		return ret;
1808e33d5c3dSKelley Nielsen 
1809e33d5c3dSKelley Nielsen 	eb = path->nodes[0];
1810e33d5c3dSKelley Nielsen 	if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
1811e33d5c3dSKelley Nielsen 		ret = btrfs_next_leaf(fs_root, path);
1812e33d5c3dSKelley Nielsen 		if (ret)
1813e33d5c3dSKelley Nielsen 			return ret;
1814e33d5c3dSKelley Nielsen 		eb = path->nodes[0];
1815e33d5c3dSKelley Nielsen 	}
1816e33d5c3dSKelley Nielsen 
1817e33d5c3dSKelley Nielsen 	btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
1818e33d5c3dSKelley Nielsen 	if (found_key->type != key.type ||
1819e33d5c3dSKelley Nielsen 			found_key->objectid != key.objectid)
1820e33d5c3dSKelley Nielsen 		return 1;
1821e33d5c3dSKelley Nielsen 
1822e33d5c3dSKelley Nielsen 	return 0;
1823e33d5c3dSKelley Nielsen }
1824e33d5c3dSKelley Nielsen 
18251fc28d8eSLiu Bo static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,
18261fc28d8eSLiu Bo 							struct btrfs_path *p,
18271fc28d8eSLiu Bo 							int write_lock_level)
18281fc28d8eSLiu Bo {
18291fc28d8eSLiu Bo 	struct extent_buffer *b;
1830120de408SJosef Bacik 	int root_lock = 0;
18311fc28d8eSLiu Bo 	int level = 0;
18321fc28d8eSLiu Bo 
18331fc28d8eSLiu Bo 	if (p->search_commit_root) {
18341fc28d8eSLiu Bo 		b = root->commit_root;
183567439dadSDavid Sterba 		atomic_inc(&b->refs);
18361fc28d8eSLiu Bo 		level = btrfs_header_level(b);
1837f9ddfd05SLiu Bo 		/*
1838f9ddfd05SLiu Bo 		 * Ensure that all callers have set skip_locking when
1839f9ddfd05SLiu Bo 		 * p->search_commit_root = 1.
1840f9ddfd05SLiu Bo 		 */
1841f9ddfd05SLiu Bo 		ASSERT(p->skip_locking == 1);
18421fc28d8eSLiu Bo 
18431fc28d8eSLiu Bo 		goto out;
18441fc28d8eSLiu Bo 	}
18451fc28d8eSLiu Bo 
18461fc28d8eSLiu Bo 	if (p->skip_locking) {
18471fc28d8eSLiu Bo 		b = btrfs_root_node(root);
18481fc28d8eSLiu Bo 		level = btrfs_header_level(b);
18491fc28d8eSLiu Bo 		goto out;
18501fc28d8eSLiu Bo 	}
18511fc28d8eSLiu Bo 
1852120de408SJosef Bacik 	/* We try very hard to do read locks on the root */
1853120de408SJosef Bacik 	root_lock = BTRFS_READ_LOCK;
1854120de408SJosef Bacik 
18551fc28d8eSLiu Bo 	/*
1856662c653bSLiu Bo 	 * If the level is set to maximum, we can skip trying to get the read
1857662c653bSLiu Bo 	 * lock.
1858662c653bSLiu Bo 	 */
1859662c653bSLiu Bo 	if (write_lock_level < BTRFS_MAX_LEVEL) {
1860662c653bSLiu Bo 		/*
1861662c653bSLiu Bo 		 * We don't know the level of the root node until we actually
1862662c653bSLiu Bo 		 * have it read locked
18631fc28d8eSLiu Bo 		 */
1864857bc13fSJosef Bacik 		if (p->nowait) {
1865857bc13fSJosef Bacik 			b = btrfs_try_read_lock_root_node(root);
1866857bc13fSJosef Bacik 			if (IS_ERR(b))
1867857bc13fSJosef Bacik 				return b;
1868857bc13fSJosef Bacik 		} else {
18691bb96598SJosef Bacik 			b = btrfs_read_lock_root_node(root);
1870857bc13fSJosef Bacik 		}
18711fc28d8eSLiu Bo 		level = btrfs_header_level(b);
18721fc28d8eSLiu Bo 		if (level > write_lock_level)
18731fc28d8eSLiu Bo 			goto out;
18741fc28d8eSLiu Bo 
1875662c653bSLiu Bo 		/* Whoops, must trade for write lock */
18761fc28d8eSLiu Bo 		btrfs_tree_read_unlock(b);
18771fc28d8eSLiu Bo 		free_extent_buffer(b);
1878662c653bSLiu Bo 	}
1879662c653bSLiu Bo 
18801fc28d8eSLiu Bo 	b = btrfs_lock_root_node(root);
18811fc28d8eSLiu Bo 	root_lock = BTRFS_WRITE_LOCK;
18821fc28d8eSLiu Bo 
18831fc28d8eSLiu Bo 	/* The level might have changed, check again */
18841fc28d8eSLiu Bo 	level = btrfs_header_level(b);
18851fc28d8eSLiu Bo 
18861fc28d8eSLiu Bo out:
1887120de408SJosef Bacik 	/*
1888120de408SJosef Bacik 	 * The root may have failed to write out at some point, and thus is no
1889120de408SJosef Bacik 	 * longer valid, return an error in this case.
1890120de408SJosef Bacik 	 */
1891120de408SJosef Bacik 	if (!extent_buffer_uptodate(b)) {
1892120de408SJosef Bacik 		if (root_lock)
1893120de408SJosef Bacik 			btrfs_tree_unlock_rw(b, root_lock);
1894120de408SJosef Bacik 		free_extent_buffer(b);
1895120de408SJosef Bacik 		return ERR_PTR(-EIO);
1896120de408SJosef Bacik 	}
1897120de408SJosef Bacik 
18981fc28d8eSLiu Bo 	p->nodes[level] = b;
18991fc28d8eSLiu Bo 	if (!p->skip_locking)
19001fc28d8eSLiu Bo 		p->locks[level] = root_lock;
19011fc28d8eSLiu Bo 	/*
19021fc28d8eSLiu Bo 	 * Callers are responsible for dropping b's references.
19031fc28d8eSLiu Bo 	 */
19041fc28d8eSLiu Bo 	return b;
19051fc28d8eSLiu Bo }
19061fc28d8eSLiu Bo 
1907d96b3424SFilipe Manana /*
1908d96b3424SFilipe Manana  * Replace the extent buffer at the lowest level of the path with a cloned
1909d96b3424SFilipe Manana  * version. The purpose is to be able to use it safely, after releasing the
1910d96b3424SFilipe Manana  * commit root semaphore, even if relocation is happening in parallel, the
1911d96b3424SFilipe Manana  * transaction used for relocation is committed and the extent buffer is
1912d96b3424SFilipe Manana  * reallocated in the next transaction.
1913d96b3424SFilipe Manana  *
1914d96b3424SFilipe Manana  * This is used in a context where the caller does not prevent transaction
1915d96b3424SFilipe Manana  * commits from happening, either by holding a transaction handle or holding
1916d96b3424SFilipe Manana  * some lock, while it's doing searches through a commit root.
1917d96b3424SFilipe Manana  * At the moment it's only used for send operations.
1918d96b3424SFilipe Manana  */
1919d96b3424SFilipe Manana static int finish_need_commit_sem_search(struct btrfs_path *path)
1920d96b3424SFilipe Manana {
1921d96b3424SFilipe Manana 	const int i = path->lowest_level;
1922d96b3424SFilipe Manana 	const int slot = path->slots[i];
1923d96b3424SFilipe Manana 	struct extent_buffer *lowest = path->nodes[i];
1924d96b3424SFilipe Manana 	struct extent_buffer *clone;
1925d96b3424SFilipe Manana 
1926d96b3424SFilipe Manana 	ASSERT(path->need_commit_sem);
1927d96b3424SFilipe Manana 
1928d96b3424SFilipe Manana 	if (!lowest)
1929d96b3424SFilipe Manana 		return 0;
1930d96b3424SFilipe Manana 
1931d96b3424SFilipe Manana 	lockdep_assert_held_read(&lowest->fs_info->commit_root_sem);
1932d96b3424SFilipe Manana 
1933d96b3424SFilipe Manana 	clone = btrfs_clone_extent_buffer(lowest);
1934d96b3424SFilipe Manana 	if (!clone)
1935d96b3424SFilipe Manana 		return -ENOMEM;
1936d96b3424SFilipe Manana 
1937d96b3424SFilipe Manana 	btrfs_release_path(path);
1938d96b3424SFilipe Manana 	path->nodes[i] = clone;
1939d96b3424SFilipe Manana 	path->slots[i] = slot;
1940d96b3424SFilipe Manana 
1941d96b3424SFilipe Manana 	return 0;
1942d96b3424SFilipe Manana }
19431fc28d8eSLiu Bo 
1944e2e58d0fSFilipe Manana static inline int search_for_key_slot(struct extent_buffer *eb,
1945e2e58d0fSFilipe Manana 				      int search_low_slot,
1946e2e58d0fSFilipe Manana 				      const struct btrfs_key *key,
1947e2e58d0fSFilipe Manana 				      int prev_cmp,
1948e2e58d0fSFilipe Manana 				      int *slot)
1949e2e58d0fSFilipe Manana {
1950e2e58d0fSFilipe Manana 	/*
1951e2e58d0fSFilipe Manana 	 * If a previous call to btrfs_bin_search() on a parent node returned an
1952e2e58d0fSFilipe Manana 	 * exact match (prev_cmp == 0), we can safely assume the target key will
1953e2e58d0fSFilipe Manana 	 * always be at slot 0 on lower levels, since each key pointer
1954e2e58d0fSFilipe Manana 	 * (struct btrfs_key_ptr) refers to the lowest key accessible from the
1955e2e58d0fSFilipe Manana 	 * subtree it points to. Thus we can skip searching lower levels.
1956e2e58d0fSFilipe Manana 	 */
1957e2e58d0fSFilipe Manana 	if (prev_cmp == 0) {
1958e2e58d0fSFilipe Manana 		*slot = 0;
1959e2e58d0fSFilipe Manana 		return 0;
1960e2e58d0fSFilipe Manana 	}
1961e2e58d0fSFilipe Manana 
1962fdf8d595SAnand Jain 	return btrfs_bin_search(eb, search_low_slot, key, slot);
1963e2e58d0fSFilipe Manana }
1964e2e58d0fSFilipe Manana 
1965109324cfSFilipe Manana static int search_leaf(struct btrfs_trans_handle *trans,
1966109324cfSFilipe Manana 		       struct btrfs_root *root,
1967109324cfSFilipe Manana 		       const struct btrfs_key *key,
1968109324cfSFilipe Manana 		       struct btrfs_path *path,
1969109324cfSFilipe Manana 		       int ins_len,
1970109324cfSFilipe Manana 		       int prev_cmp)
1971109324cfSFilipe Manana {
1972109324cfSFilipe Manana 	struct extent_buffer *leaf = path->nodes[0];
1973109324cfSFilipe Manana 	int leaf_free_space = -1;
1974109324cfSFilipe Manana 	int search_low_slot = 0;
1975109324cfSFilipe Manana 	int ret;
1976109324cfSFilipe Manana 	bool do_bin_search = true;
1977109324cfSFilipe Manana 
1978109324cfSFilipe Manana 	/*
1979109324cfSFilipe Manana 	 * If we are doing an insertion, the leaf has enough free space and the
1980109324cfSFilipe Manana 	 * destination slot for the key is not slot 0, then we can unlock our
1981109324cfSFilipe Manana 	 * write lock on the parent, and any other upper nodes, before doing the
1982109324cfSFilipe Manana 	 * binary search on the leaf (with search_for_key_slot()), allowing other
1983109324cfSFilipe Manana 	 * tasks to lock the parent and any other upper nodes.
1984109324cfSFilipe Manana 	 */
1985109324cfSFilipe Manana 	if (ins_len > 0) {
1986109324cfSFilipe Manana 		/*
1987109324cfSFilipe Manana 		 * Cache the leaf free space, since we will need it later and it
1988109324cfSFilipe Manana 		 * will not change until then.
1989109324cfSFilipe Manana 		 */
1990109324cfSFilipe Manana 		leaf_free_space = btrfs_leaf_free_space(leaf);
1991109324cfSFilipe Manana 
1992109324cfSFilipe Manana 		/*
1993109324cfSFilipe Manana 		 * !path->locks[1] means we have a single node tree, the leaf is
1994109324cfSFilipe Manana 		 * the root of the tree.
1995109324cfSFilipe Manana 		 */
1996109324cfSFilipe Manana 		if (path->locks[1] && leaf_free_space >= ins_len) {
1997109324cfSFilipe Manana 			struct btrfs_disk_key first_key;
1998109324cfSFilipe Manana 
1999109324cfSFilipe Manana 			ASSERT(btrfs_header_nritems(leaf) > 0);
2000109324cfSFilipe Manana 			btrfs_item_key(leaf, &first_key, 0);
2001109324cfSFilipe Manana 
2002109324cfSFilipe Manana 			/*
2003109324cfSFilipe Manana 			 * Doing the extra comparison with the first key is cheap,
2004109324cfSFilipe Manana 			 * taking into account that the first key is very likely
2005109324cfSFilipe Manana 			 * already in a cache line because it immediately follows
2006109324cfSFilipe Manana 			 * the extent buffer's header and we have recently accessed
2007109324cfSFilipe Manana 			 * the header's level field.
2008109324cfSFilipe Manana 			 */
2009109324cfSFilipe Manana 			ret = comp_keys(&first_key, key);
2010109324cfSFilipe Manana 			if (ret < 0) {
2011109324cfSFilipe Manana 				/*
2012109324cfSFilipe Manana 				 * The first key is smaller than the key we want
2013109324cfSFilipe Manana 				 * to insert, so we are safe to unlock all upper
2014109324cfSFilipe Manana 				 * nodes and we have to do the binary search.
2015109324cfSFilipe Manana 				 *
2016109324cfSFilipe Manana 				 * We do use btrfs_unlock_up_safe() and not
2017109324cfSFilipe Manana 				 * unlock_up() because the later does not unlock
2018109324cfSFilipe Manana 				 * nodes with a slot of 0 - we can safely unlock
2019109324cfSFilipe Manana 				 * any node even if its slot is 0 since in this
2020109324cfSFilipe Manana 				 * case the key does not end up at slot 0 of the
2021109324cfSFilipe Manana 				 * leaf and there's no need to split the leaf.
2022109324cfSFilipe Manana 				 */
2023109324cfSFilipe Manana 				btrfs_unlock_up_safe(path, 1);
2024109324cfSFilipe Manana 				search_low_slot = 1;
2025109324cfSFilipe Manana 			} else {
2026109324cfSFilipe Manana 				/*
2027109324cfSFilipe Manana 				 * The first key is >= then the key we want to
2028109324cfSFilipe Manana 				 * insert, so we can skip the binary search as
2029109324cfSFilipe Manana 				 * the target key will be at slot 0.
2030109324cfSFilipe Manana 				 *
2031109324cfSFilipe Manana 				 * We can not unlock upper nodes when the key is
2032109324cfSFilipe Manana 				 * less than the first key, because we will need
2033109324cfSFilipe Manana 				 * to update the key at slot 0 of the parent node
2034109324cfSFilipe Manana 				 * and possibly of other upper nodes too.
2035109324cfSFilipe Manana 				 * If the key matches the first key, then we can
2036109324cfSFilipe Manana 				 * unlock all the upper nodes, using
2037109324cfSFilipe Manana 				 * btrfs_unlock_up_safe() instead of unlock_up()
2038109324cfSFilipe Manana 				 * as stated above.
2039109324cfSFilipe Manana 				 */
2040109324cfSFilipe Manana 				if (ret == 0)
2041109324cfSFilipe Manana 					btrfs_unlock_up_safe(path, 1);
2042109324cfSFilipe Manana 				/*
2043109324cfSFilipe Manana 				 * ret is already 0 or 1, matching the result of
2044109324cfSFilipe Manana 				 * a btrfs_bin_search() call, so there is no need
2045109324cfSFilipe Manana 				 * to adjust it.
2046109324cfSFilipe Manana 				 */
2047109324cfSFilipe Manana 				do_bin_search = false;
2048109324cfSFilipe Manana 				path->slots[0] = 0;
2049109324cfSFilipe Manana 			}
2050109324cfSFilipe Manana 		}
2051109324cfSFilipe Manana 	}
2052109324cfSFilipe Manana 
2053109324cfSFilipe Manana 	if (do_bin_search) {
2054109324cfSFilipe Manana 		ret = search_for_key_slot(leaf, search_low_slot, key,
2055109324cfSFilipe Manana 					  prev_cmp, &path->slots[0]);
2056109324cfSFilipe Manana 		if (ret < 0)
2057109324cfSFilipe Manana 			return ret;
2058109324cfSFilipe Manana 	}
2059109324cfSFilipe Manana 
2060109324cfSFilipe Manana 	if (ins_len > 0) {
2061109324cfSFilipe Manana 		/*
2062109324cfSFilipe Manana 		 * Item key already exists. In this case, if we are allowed to
2063109324cfSFilipe Manana 		 * insert the item (for example, in dir_item case, item key
2064109324cfSFilipe Manana 		 * collision is allowed), it will be merged with the original
2065109324cfSFilipe Manana 		 * item. Only the item size grows, no new btrfs item will be
2066109324cfSFilipe Manana 		 * added. If search_for_extension is not set, ins_len already
2067109324cfSFilipe Manana 		 * accounts the size btrfs_item, deduct it here so leaf space
2068109324cfSFilipe Manana 		 * check will be correct.
2069109324cfSFilipe Manana 		 */
2070109324cfSFilipe Manana 		if (ret == 0 && !path->search_for_extension) {
2071109324cfSFilipe Manana 			ASSERT(ins_len >= sizeof(struct btrfs_item));
2072109324cfSFilipe Manana 			ins_len -= sizeof(struct btrfs_item);
2073109324cfSFilipe Manana 		}
2074109324cfSFilipe Manana 
2075109324cfSFilipe Manana 		ASSERT(leaf_free_space >= 0);
2076109324cfSFilipe Manana 
2077109324cfSFilipe Manana 		if (leaf_free_space < ins_len) {
2078109324cfSFilipe Manana 			int err;
2079109324cfSFilipe Manana 
2080109324cfSFilipe Manana 			err = split_leaf(trans, root, key, path, ins_len,
2081109324cfSFilipe Manana 					 (ret == 0));
2082bb8e9a60SFilipe Manana 			ASSERT(err <= 0);
2083bb8e9a60SFilipe Manana 			if (WARN_ON(err > 0))
2084bb8e9a60SFilipe Manana 				err = -EUCLEAN;
2085109324cfSFilipe Manana 			if (err)
2086109324cfSFilipe Manana 				ret = err;
2087109324cfSFilipe Manana 		}
2088109324cfSFilipe Manana 	}
2089109324cfSFilipe Manana 
2090109324cfSFilipe Manana 	return ret;
2091109324cfSFilipe Manana }
2092109324cfSFilipe Manana 
2093c8c42864SChris Mason /*
20944271eceaSNikolay Borisov  * btrfs_search_slot - look for a key in a tree and perform necessary
20954271eceaSNikolay Borisov  * modifications to preserve tree invariants.
209674123bd7SChris Mason  *
20974271eceaSNikolay Borisov  * @trans:	Handle of transaction, used when modifying the tree
20984271eceaSNikolay Borisov  * @p:		Holds all btree nodes along the search path
20994271eceaSNikolay Borisov  * @root:	The root node of the tree
21004271eceaSNikolay Borisov  * @key:	The key we are looking for
21019a664971Sethanwu  * @ins_len:	Indicates purpose of search:
21029a664971Sethanwu  *              >0  for inserts it's size of item inserted (*)
21039a664971Sethanwu  *              <0  for deletions
21049a664971Sethanwu  *               0  for plain searches, not modifying the tree
21059a664971Sethanwu  *
21069a664971Sethanwu  *              (*) If size of item inserted doesn't include
21079a664971Sethanwu  *              sizeof(struct btrfs_item), then p->search_for_extension must
21089a664971Sethanwu  *              be set.
21094271eceaSNikolay Borisov  * @cow:	boolean should CoW operations be performed. Must always be 1
21104271eceaSNikolay Borisov  *		when modifying the tree.
211197571fd0SChris Mason  *
21124271eceaSNikolay Borisov  * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
21134271eceaSNikolay Borisov  * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
21144271eceaSNikolay Borisov  *
21154271eceaSNikolay Borisov  * If @key is found, 0 is returned and you can find the item in the leaf level
21164271eceaSNikolay Borisov  * of the path (level 0)
21174271eceaSNikolay Borisov  *
21184271eceaSNikolay Borisov  * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
21194271eceaSNikolay Borisov  * points to the slot where it should be inserted
21204271eceaSNikolay Borisov  *
21214271eceaSNikolay Borisov  * If an error is encountered while searching the tree a negative error number
21224271eceaSNikolay Borisov  * is returned
212374123bd7SChris Mason  */
2124310712b2SOmar Sandoval int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2125310712b2SOmar Sandoval 		      const struct btrfs_key *key, struct btrfs_path *p,
2126310712b2SOmar Sandoval 		      int ins_len, int cow)
2127be0e5c09SChris Mason {
2128d96b3424SFilipe Manana 	struct btrfs_fs_info *fs_info = root->fs_info;
21295f39d397SChris Mason 	struct extent_buffer *b;
2130be0e5c09SChris Mason 	int slot;
2131be0e5c09SChris Mason 	int ret;
213233c66f43SYan Zheng 	int err;
2133be0e5c09SChris Mason 	int level;
2134925baeddSChris Mason 	int lowest_unlock = 1;
2135bd681513SChris Mason 	/* everything at write_lock_level or lower must be write locked */
2136bd681513SChris Mason 	int write_lock_level = 0;
21379f3a7427SChris Mason 	u8 lowest_level = 0;
2138f7c79f30SChris Mason 	int min_write_lock_level;
2139d7396f07SFilipe David Borba Manana 	int prev_cmp;
21409f3a7427SChris Mason 
2141a4c853afSChenXiaoSong 	might_sleep();
2142a4c853afSChenXiaoSong 
21436702ed49SChris Mason 	lowest_level = p->lowest_level;
2144323ac95bSChris Mason 	WARN_ON(lowest_level && ins_len > 0);
214522b0ebdaSChris Mason 	WARN_ON(p->nodes[0] != NULL);
2146eb653de1SFilipe David Borba Manana 	BUG_ON(!cow && ins_len);
214725179201SJosef Bacik 
2148857bc13fSJosef Bacik 	/*
2149857bc13fSJosef Bacik 	 * For now only allow nowait for read only operations.  There's no
2150857bc13fSJosef Bacik 	 * strict reason why we can't, we just only need it for reads so it's
2151857bc13fSJosef Bacik 	 * only implemented for reads.
2152857bc13fSJosef Bacik 	 */
2153857bc13fSJosef Bacik 	ASSERT(!p->nowait || !cow);
2154857bc13fSJosef Bacik 
2155bd681513SChris Mason 	if (ins_len < 0) {
2156925baeddSChris Mason 		lowest_unlock = 2;
215765b51a00SChris Mason 
2158bd681513SChris Mason 		/* when we are removing items, we might have to go up to level
2159bd681513SChris Mason 		 * two as we update tree pointers  Make sure we keep write
2160bd681513SChris Mason 		 * for those levels as well
2161bd681513SChris Mason 		 */
2162bd681513SChris Mason 		write_lock_level = 2;
2163bd681513SChris Mason 	} else if (ins_len > 0) {
2164bd681513SChris Mason 		/*
2165bd681513SChris Mason 		 * for inserting items, make sure we have a write lock on
2166bd681513SChris Mason 		 * level 1 so we can update keys
2167bd681513SChris Mason 		 */
2168bd681513SChris Mason 		write_lock_level = 1;
2169bd681513SChris Mason 	}
2170bd681513SChris Mason 
2171bd681513SChris Mason 	if (!cow)
2172bd681513SChris Mason 		write_lock_level = -1;
2173bd681513SChris Mason 
217409a2a8f9SJosef Bacik 	if (cow && (p->keep_locks || p->lowest_level))
2175bd681513SChris Mason 		write_lock_level = BTRFS_MAX_LEVEL;
2176bd681513SChris Mason 
2177f7c79f30SChris Mason 	min_write_lock_level = write_lock_level;
2178f7c79f30SChris Mason 
2179d96b3424SFilipe Manana 	if (p->need_commit_sem) {
2180d96b3424SFilipe Manana 		ASSERT(p->search_commit_root);
2181857bc13fSJosef Bacik 		if (p->nowait) {
2182857bc13fSJosef Bacik 			if (!down_read_trylock(&fs_info->commit_root_sem))
2183857bc13fSJosef Bacik 				return -EAGAIN;
2184857bc13fSJosef Bacik 		} else {
2185d96b3424SFilipe Manana 			down_read(&fs_info->commit_root_sem);
2186d96b3424SFilipe Manana 		}
2187857bc13fSJosef Bacik 	}
2188d96b3424SFilipe Manana 
2189bb803951SChris Mason again:
2190d7396f07SFilipe David Borba Manana 	prev_cmp = -1;
21911fc28d8eSLiu Bo 	b = btrfs_search_slot_get_root(root, p, write_lock_level);
2192be6821f8SFilipe Manana 	if (IS_ERR(b)) {
2193be6821f8SFilipe Manana 		ret = PTR_ERR(b);
2194be6821f8SFilipe Manana 		goto done;
2195be6821f8SFilipe Manana 	}
2196925baeddSChris Mason 
2197eb60ceacSChris Mason 	while (b) {
2198f624d976SQu Wenruo 		int dec = 0;
2199f624d976SQu Wenruo 
22005f39d397SChris Mason 		level = btrfs_header_level(b);
220165b51a00SChris Mason 
220202217ed2SChris Mason 		if (cow) {
22039ea2c7c9SNikolay Borisov 			bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
22049ea2c7c9SNikolay Borisov 
2205c8c42864SChris Mason 			/*
2206c8c42864SChris Mason 			 * if we don't really need to cow this block
2207c8c42864SChris Mason 			 * then we don't want to set the path blocking,
2208c8c42864SChris Mason 			 * so we test it here
2209c8c42864SChris Mason 			 */
22105963ffcaSJosef Bacik 			if (!should_cow_block(trans, root, b))
221165b51a00SChris Mason 				goto cow_done;
22125d4f98a2SYan Zheng 
2213bd681513SChris Mason 			/*
2214bd681513SChris Mason 			 * must have write locks on this node and the
2215bd681513SChris Mason 			 * parent
2216bd681513SChris Mason 			 */
22175124e00eSJosef Bacik 			if (level > write_lock_level ||
22185124e00eSJosef Bacik 			    (level + 1 > write_lock_level &&
22195124e00eSJosef Bacik 			    level + 1 < BTRFS_MAX_LEVEL &&
22205124e00eSJosef Bacik 			    p->nodes[level + 1])) {
2221bd681513SChris Mason 				write_lock_level = level + 1;
2222bd681513SChris Mason 				btrfs_release_path(p);
2223bd681513SChris Mason 				goto again;
2224bd681513SChris Mason 			}
2225bd681513SChris Mason 
22269ea2c7c9SNikolay Borisov 			if (last_level)
22279ea2c7c9SNikolay Borisov 				err = btrfs_cow_block(trans, root, b, NULL, 0,
22289631e4ccSJosef Bacik 						      &b,
22299631e4ccSJosef Bacik 						      BTRFS_NESTING_COW);
22309ea2c7c9SNikolay Borisov 			else
223133c66f43SYan Zheng 				err = btrfs_cow_block(trans, root, b,
2232e20d96d6SChris Mason 						      p->nodes[level + 1],
22339631e4ccSJosef Bacik 						      p->slots[level + 1], &b,
22349631e4ccSJosef Bacik 						      BTRFS_NESTING_COW);
223533c66f43SYan Zheng 			if (err) {
223633c66f43SYan Zheng 				ret = err;
223765b51a00SChris Mason 				goto done;
223854aa1f4dSChris Mason 			}
223902217ed2SChris Mason 		}
224065b51a00SChris Mason cow_done:
2241eb60ceacSChris Mason 		p->nodes[level] = b;
2242b4ce94deSChris Mason 
2243b4ce94deSChris Mason 		/*
2244b4ce94deSChris Mason 		 * we have a lock on b and as long as we aren't changing
2245b4ce94deSChris Mason 		 * the tree, there is no way to for the items in b to change.
2246b4ce94deSChris Mason 		 * It is safe to drop the lock on our parent before we
2247b4ce94deSChris Mason 		 * go through the expensive btree search on b.
2248b4ce94deSChris Mason 		 *
2249eb653de1SFilipe David Borba Manana 		 * If we're inserting or deleting (ins_len != 0), then we might
2250eb653de1SFilipe David Borba Manana 		 * be changing slot zero, which may require changing the parent.
2251eb653de1SFilipe David Borba Manana 		 * So, we can't drop the lock until after we know which slot
2252eb653de1SFilipe David Borba Manana 		 * we're operating on.
2253b4ce94deSChris Mason 		 */
2254eb653de1SFilipe David Borba Manana 		if (!ins_len && !p->keep_locks) {
2255eb653de1SFilipe David Borba Manana 			int u = level + 1;
2256eb653de1SFilipe David Borba Manana 
2257eb653de1SFilipe David Borba Manana 			if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2258eb653de1SFilipe David Borba Manana 				btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2259eb653de1SFilipe David Borba Manana 				p->locks[u] = 0;
2260eb653de1SFilipe David Borba Manana 			}
2261eb653de1SFilipe David Borba Manana 		}
2262b4ce94deSChris Mason 
2263e2e58d0fSFilipe Manana 		if (level == 0) {
2264109324cfSFilipe Manana 			if (ins_len > 0)
2265e5e1c174SFilipe Manana 				ASSERT(write_lock_level >= 1);
2266bd681513SChris Mason 
2267109324cfSFilipe Manana 			ret = search_leaf(trans, root, key, p, ins_len, prev_cmp);
2268459931ecSChris Mason 			if (!p->search_for_split)
2269f7c79f30SChris Mason 				unlock_up(p, level, lowest_unlock,
22704b6f8e96SLiu Bo 					  min_write_lock_level, NULL);
227165b51a00SChris Mason 			goto done;
227265b51a00SChris Mason 		}
2273e2e58d0fSFilipe Manana 
2274e2e58d0fSFilipe Manana 		ret = search_for_key_slot(b, 0, key, prev_cmp, &slot);
2275e2e58d0fSFilipe Manana 		if (ret < 0)
2276e2e58d0fSFilipe Manana 			goto done;
2277e2e58d0fSFilipe Manana 		prev_cmp = ret;
2278e2e58d0fSFilipe Manana 
2279f624d976SQu Wenruo 		if (ret && slot > 0) {
2280f624d976SQu Wenruo 			dec = 1;
2281f624d976SQu Wenruo 			slot--;
2282f624d976SQu Wenruo 		}
2283f624d976SQu Wenruo 		p->slots[level] = slot;
2284f624d976SQu Wenruo 		err = setup_nodes_for_search(trans, root, p, b, level, ins_len,
2285f624d976SQu Wenruo 					     &write_lock_level);
2286f624d976SQu Wenruo 		if (err == -EAGAIN)
2287f624d976SQu Wenruo 			goto again;
2288f624d976SQu Wenruo 		if (err) {
2289f624d976SQu Wenruo 			ret = err;
2290f624d976SQu Wenruo 			goto done;
2291f624d976SQu Wenruo 		}
2292f624d976SQu Wenruo 		b = p->nodes[level];
2293f624d976SQu Wenruo 		slot = p->slots[level];
2294f624d976SQu Wenruo 
2295f624d976SQu Wenruo 		/*
2296f624d976SQu Wenruo 		 * Slot 0 is special, if we change the key we have to update
2297f624d976SQu Wenruo 		 * the parent pointer which means we must have a write lock on
2298f624d976SQu Wenruo 		 * the parent
2299f624d976SQu Wenruo 		 */
2300f624d976SQu Wenruo 		if (slot == 0 && ins_len && write_lock_level < level + 1) {
2301f624d976SQu Wenruo 			write_lock_level = level + 1;
2302f624d976SQu Wenruo 			btrfs_release_path(p);
2303f624d976SQu Wenruo 			goto again;
2304f624d976SQu Wenruo 		}
2305f624d976SQu Wenruo 
2306f624d976SQu Wenruo 		unlock_up(p, level, lowest_unlock, min_write_lock_level,
2307f624d976SQu Wenruo 			  &write_lock_level);
2308f624d976SQu Wenruo 
2309f624d976SQu Wenruo 		if (level == lowest_level) {
2310f624d976SQu Wenruo 			if (dec)
2311f624d976SQu Wenruo 				p->slots[level]++;
2312f624d976SQu Wenruo 			goto done;
2313f624d976SQu Wenruo 		}
2314f624d976SQu Wenruo 
2315f624d976SQu Wenruo 		err = read_block_for_search(root, p, &b, level, slot, key);
2316f624d976SQu Wenruo 		if (err == -EAGAIN)
2317f624d976SQu Wenruo 			goto again;
2318f624d976SQu Wenruo 		if (err) {
2319f624d976SQu Wenruo 			ret = err;
2320f624d976SQu Wenruo 			goto done;
2321f624d976SQu Wenruo 		}
2322f624d976SQu Wenruo 
2323f624d976SQu Wenruo 		if (!p->skip_locking) {
2324f624d976SQu Wenruo 			level = btrfs_header_level(b);
2325b40130b2SJosef Bacik 
2326b40130b2SJosef Bacik 			btrfs_maybe_reset_lockdep_class(root, b);
2327b40130b2SJosef Bacik 
2328f624d976SQu Wenruo 			if (level <= write_lock_level) {
2329f624d976SQu Wenruo 				btrfs_tree_lock(b);
2330f624d976SQu Wenruo 				p->locks[level] = BTRFS_WRITE_LOCK;
2331f624d976SQu Wenruo 			} else {
2332857bc13fSJosef Bacik 				if (p->nowait) {
2333857bc13fSJosef Bacik 					if (!btrfs_try_tree_read_lock(b)) {
2334857bc13fSJosef Bacik 						free_extent_buffer(b);
2335857bc13fSJosef Bacik 						ret = -EAGAIN;
2336857bc13fSJosef Bacik 						goto done;
2337857bc13fSJosef Bacik 					}
2338857bc13fSJosef Bacik 				} else {
2339fe596ca3SJosef Bacik 					btrfs_tree_read_lock(b);
2340857bc13fSJosef Bacik 				}
2341f624d976SQu Wenruo 				p->locks[level] = BTRFS_READ_LOCK;
2342f624d976SQu Wenruo 			}
2343f624d976SQu Wenruo 			p->nodes[level] = b;
2344f624d976SQu Wenruo 		}
234565b51a00SChris Mason 	}
234665b51a00SChris Mason 	ret = 1;
234765b51a00SChris Mason done:
23485f5bc6b1SFilipe Manana 	if (ret < 0 && !p->skip_release_on_error)
2349b3b4aa74SDavid Sterba 		btrfs_release_path(p);
2350d96b3424SFilipe Manana 
2351d96b3424SFilipe Manana 	if (p->need_commit_sem) {
2352d96b3424SFilipe Manana 		int ret2;
2353d96b3424SFilipe Manana 
2354d96b3424SFilipe Manana 		ret2 = finish_need_commit_sem_search(p);
2355d96b3424SFilipe Manana 		up_read(&fs_info->commit_root_sem);
2356d96b3424SFilipe Manana 		if (ret2)
2357d96b3424SFilipe Manana 			ret = ret2;
2358d96b3424SFilipe Manana 	}
2359d96b3424SFilipe Manana 
2360be0e5c09SChris Mason 	return ret;
2361be0e5c09SChris Mason }
2362f75e2b79SJosef Bacik ALLOW_ERROR_INJECTION(btrfs_search_slot, ERRNO);
2363be0e5c09SChris Mason 
236474123bd7SChris Mason /*
23655d9e75c4SJan Schmidt  * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
23665d9e75c4SJan Schmidt  * current state of the tree together with the operations recorded in the tree
23675d9e75c4SJan Schmidt  * modification log to search for the key in a previous version of this tree, as
23685d9e75c4SJan Schmidt  * denoted by the time_seq parameter.
23695d9e75c4SJan Schmidt  *
23705d9e75c4SJan Schmidt  * Naturally, there is no support for insert, delete or cow operations.
23715d9e75c4SJan Schmidt  *
23725d9e75c4SJan Schmidt  * The resulting path and return value will be set up as if we called
23735d9e75c4SJan Schmidt  * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
23745d9e75c4SJan Schmidt  */
2375310712b2SOmar Sandoval int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
23765d9e75c4SJan Schmidt 			  struct btrfs_path *p, u64 time_seq)
23775d9e75c4SJan Schmidt {
23780b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
23795d9e75c4SJan Schmidt 	struct extent_buffer *b;
23805d9e75c4SJan Schmidt 	int slot;
23815d9e75c4SJan Schmidt 	int ret;
23825d9e75c4SJan Schmidt 	int err;
23835d9e75c4SJan Schmidt 	int level;
23845d9e75c4SJan Schmidt 	int lowest_unlock = 1;
23855d9e75c4SJan Schmidt 	u8 lowest_level = 0;
23865d9e75c4SJan Schmidt 
23875d9e75c4SJan Schmidt 	lowest_level = p->lowest_level;
23885d9e75c4SJan Schmidt 	WARN_ON(p->nodes[0] != NULL);
2389c922b016SStefan Roesch 	ASSERT(!p->nowait);
23905d9e75c4SJan Schmidt 
23915d9e75c4SJan Schmidt 	if (p->search_commit_root) {
23925d9e75c4SJan Schmidt 		BUG_ON(time_seq);
23935d9e75c4SJan Schmidt 		return btrfs_search_slot(NULL, root, key, p, 0, 0);
23945d9e75c4SJan Schmidt 	}
23955d9e75c4SJan Schmidt 
23965d9e75c4SJan Schmidt again:
2397f3a84ccdSFilipe Manana 	b = btrfs_get_old_root(root, time_seq);
2398315bed43SNikolay Borisov 	if (!b) {
2399315bed43SNikolay Borisov 		ret = -EIO;
2400315bed43SNikolay Borisov 		goto done;
2401315bed43SNikolay Borisov 	}
24025d9e75c4SJan Schmidt 	level = btrfs_header_level(b);
24035d9e75c4SJan Schmidt 	p->locks[level] = BTRFS_READ_LOCK;
24045d9e75c4SJan Schmidt 
24055d9e75c4SJan Schmidt 	while (b) {
2406abe9339dSQu Wenruo 		int dec = 0;
2407abe9339dSQu Wenruo 
24085d9e75c4SJan Schmidt 		level = btrfs_header_level(b);
24095d9e75c4SJan Schmidt 		p->nodes[level] = b;
24105d9e75c4SJan Schmidt 
24115d9e75c4SJan Schmidt 		/*
24125d9e75c4SJan Schmidt 		 * we have a lock on b and as long as we aren't changing
24135d9e75c4SJan Schmidt 		 * the tree, there is no way to for the items in b to change.
24145d9e75c4SJan Schmidt 		 * It is safe to drop the lock on our parent before we
24155d9e75c4SJan Schmidt 		 * go through the expensive btree search on b.
24165d9e75c4SJan Schmidt 		 */
24175d9e75c4SJan Schmidt 		btrfs_unlock_up_safe(p, level + 1);
24185d9e75c4SJan Schmidt 
2419fdf8d595SAnand Jain 		ret = btrfs_bin_search(b, 0, key, &slot);
2420cbca7d59SFilipe Manana 		if (ret < 0)
2421cbca7d59SFilipe Manana 			goto done;
24225d9e75c4SJan Schmidt 
2423abe9339dSQu Wenruo 		if (level == 0) {
2424abe9339dSQu Wenruo 			p->slots[level] = slot;
2425abe9339dSQu Wenruo 			unlock_up(p, level, lowest_unlock, 0, NULL);
2426abe9339dSQu Wenruo 			goto done;
2427abe9339dSQu Wenruo 		}
2428abe9339dSQu Wenruo 
24295d9e75c4SJan Schmidt 		if (ret && slot > 0) {
24305d9e75c4SJan Schmidt 			dec = 1;
2431abe9339dSQu Wenruo 			slot--;
24325d9e75c4SJan Schmidt 		}
24335d9e75c4SJan Schmidt 		p->slots[level] = slot;
24345d9e75c4SJan Schmidt 		unlock_up(p, level, lowest_unlock, 0, NULL);
24355d9e75c4SJan Schmidt 
24365d9e75c4SJan Schmidt 		if (level == lowest_level) {
24375d9e75c4SJan Schmidt 			if (dec)
24385d9e75c4SJan Schmidt 				p->slots[level]++;
24395d9e75c4SJan Schmidt 			goto done;
24405d9e75c4SJan Schmidt 		}
24415d9e75c4SJan Schmidt 
2442abe9339dSQu Wenruo 		err = read_block_for_search(root, p, &b, level, slot, key);
24435d9e75c4SJan Schmidt 		if (err == -EAGAIN)
24445d9e75c4SJan Schmidt 			goto again;
24455d9e75c4SJan Schmidt 		if (err) {
24465d9e75c4SJan Schmidt 			ret = err;
24475d9e75c4SJan Schmidt 			goto done;
24485d9e75c4SJan Schmidt 		}
24495d9e75c4SJan Schmidt 
24505d9e75c4SJan Schmidt 		level = btrfs_header_level(b);
24515d9e75c4SJan Schmidt 		btrfs_tree_read_lock(b);
2452f3a84ccdSFilipe Manana 		b = btrfs_tree_mod_log_rewind(fs_info, p, b, time_seq);
2453db7f3436SJosef Bacik 		if (!b) {
2454db7f3436SJosef Bacik 			ret = -ENOMEM;
2455db7f3436SJosef Bacik 			goto done;
2456db7f3436SJosef Bacik 		}
24575d9e75c4SJan Schmidt 		p->locks[level] = BTRFS_READ_LOCK;
24585d9e75c4SJan Schmidt 		p->nodes[level] = b;
24595d9e75c4SJan Schmidt 	}
24605d9e75c4SJan Schmidt 	ret = 1;
24615d9e75c4SJan Schmidt done:
24625d9e75c4SJan Schmidt 	if (ret < 0)
24635d9e75c4SJan Schmidt 		btrfs_release_path(p);
24645d9e75c4SJan Schmidt 
24655d9e75c4SJan Schmidt 	return ret;
24665d9e75c4SJan Schmidt }
24675d9e75c4SJan Schmidt 
24685d9e75c4SJan Schmidt /*
2469f469c8bdSFilipe Manana  * Search the tree again to find a leaf with smaller keys.
2470f469c8bdSFilipe Manana  * Returns 0 if it found something.
2471f469c8bdSFilipe Manana  * Returns 1 if there are no smaller keys.
2472f469c8bdSFilipe Manana  * Returns < 0 on error.
2473f469c8bdSFilipe Manana  *
2474f469c8bdSFilipe Manana  * This may release the path, and so you may lose any locks held at the
2475f469c8bdSFilipe Manana  * time you call it.
2476f469c8bdSFilipe Manana  */
2477f469c8bdSFilipe Manana static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
2478f469c8bdSFilipe Manana {
2479f469c8bdSFilipe Manana 	struct btrfs_key key;
2480f469c8bdSFilipe Manana 	struct btrfs_key orig_key;
2481f469c8bdSFilipe Manana 	struct btrfs_disk_key found_key;
2482f469c8bdSFilipe Manana 	int ret;
2483f469c8bdSFilipe Manana 
2484f469c8bdSFilipe Manana 	btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
2485f469c8bdSFilipe Manana 	orig_key = key;
2486f469c8bdSFilipe Manana 
2487f469c8bdSFilipe Manana 	if (key.offset > 0) {
2488f469c8bdSFilipe Manana 		key.offset--;
2489f469c8bdSFilipe Manana 	} else if (key.type > 0) {
2490f469c8bdSFilipe Manana 		key.type--;
2491f469c8bdSFilipe Manana 		key.offset = (u64)-1;
2492f469c8bdSFilipe Manana 	} else if (key.objectid > 0) {
2493f469c8bdSFilipe Manana 		key.objectid--;
2494f469c8bdSFilipe Manana 		key.type = (u8)-1;
2495f469c8bdSFilipe Manana 		key.offset = (u64)-1;
2496f469c8bdSFilipe Manana 	} else {
2497f469c8bdSFilipe Manana 		return 1;
2498f469c8bdSFilipe Manana 	}
2499f469c8bdSFilipe Manana 
2500f469c8bdSFilipe Manana 	btrfs_release_path(path);
2501f469c8bdSFilipe Manana 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2502f469c8bdSFilipe Manana 	if (ret <= 0)
2503f469c8bdSFilipe Manana 		return ret;
2504f469c8bdSFilipe Manana 
2505f469c8bdSFilipe Manana 	/*
2506f469c8bdSFilipe Manana 	 * Previous key not found. Even if we were at slot 0 of the leaf we had
2507f469c8bdSFilipe Manana 	 * before releasing the path and calling btrfs_search_slot(), we now may
2508f469c8bdSFilipe Manana 	 * be in a slot pointing to the same original key - this can happen if
2509f469c8bdSFilipe Manana 	 * after we released the path, one of more items were moved from a
2510f469c8bdSFilipe Manana 	 * sibling leaf into the front of the leaf we had due to an insertion
2511f469c8bdSFilipe Manana 	 * (see push_leaf_right()).
2512f469c8bdSFilipe Manana 	 * If we hit this case and our slot is > 0 and just decrement the slot
2513f469c8bdSFilipe Manana 	 * so that the caller does not process the same key again, which may or
2514f469c8bdSFilipe Manana 	 * may not break the caller, depending on its logic.
2515f469c8bdSFilipe Manana 	 */
2516f469c8bdSFilipe Manana 	if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
2517f469c8bdSFilipe Manana 		btrfs_item_key(path->nodes[0], &found_key, path->slots[0]);
2518f469c8bdSFilipe Manana 		ret = comp_keys(&found_key, &orig_key);
2519f469c8bdSFilipe Manana 		if (ret == 0) {
2520f469c8bdSFilipe Manana 			if (path->slots[0] > 0) {
2521f469c8bdSFilipe Manana 				path->slots[0]--;
2522f469c8bdSFilipe Manana 				return 0;
2523f469c8bdSFilipe Manana 			}
2524f469c8bdSFilipe Manana 			/*
2525f469c8bdSFilipe Manana 			 * At slot 0, same key as before, it means orig_key is
2526f469c8bdSFilipe Manana 			 * the lowest, leftmost, key in the tree. We're done.
2527f469c8bdSFilipe Manana 			 */
2528f469c8bdSFilipe Manana 			return 1;
2529f469c8bdSFilipe Manana 		}
2530f469c8bdSFilipe Manana 	}
2531f469c8bdSFilipe Manana 
2532f469c8bdSFilipe Manana 	btrfs_item_key(path->nodes[0], &found_key, 0);
2533f469c8bdSFilipe Manana 	ret = comp_keys(&found_key, &key);
2534f469c8bdSFilipe Manana 	/*
2535f469c8bdSFilipe Manana 	 * We might have had an item with the previous key in the tree right
2536f469c8bdSFilipe Manana 	 * before we released our path. And after we released our path, that
2537f469c8bdSFilipe Manana 	 * item might have been pushed to the first slot (0) of the leaf we
2538f469c8bdSFilipe Manana 	 * were holding due to a tree balance. Alternatively, an item with the
2539f469c8bdSFilipe Manana 	 * previous key can exist as the only element of a leaf (big fat item).
2540f469c8bdSFilipe Manana 	 * Therefore account for these 2 cases, so that our callers (like
2541f469c8bdSFilipe Manana 	 * btrfs_previous_item) don't miss an existing item with a key matching
2542f469c8bdSFilipe Manana 	 * the previous key we computed above.
2543f469c8bdSFilipe Manana 	 */
2544f469c8bdSFilipe Manana 	if (ret <= 0)
2545f469c8bdSFilipe Manana 		return 0;
2546f469c8bdSFilipe Manana 	return 1;
2547f469c8bdSFilipe Manana }
2548f469c8bdSFilipe Manana 
2549f469c8bdSFilipe Manana /*
25502f38b3e1SArne Jansen  * helper to use instead of search slot if no exact match is needed but
25512f38b3e1SArne Jansen  * instead the next or previous item should be returned.
25522f38b3e1SArne Jansen  * When find_higher is true, the next higher item is returned, the next lower
25532f38b3e1SArne Jansen  * otherwise.
25542f38b3e1SArne Jansen  * When return_any and find_higher are both true, and no higher item is found,
25552f38b3e1SArne Jansen  * return the next lower instead.
25562f38b3e1SArne Jansen  * When return_any is true and find_higher is false, and no lower item is found,
25572f38b3e1SArne Jansen  * return the next higher instead.
25582f38b3e1SArne Jansen  * It returns 0 if any item is found, 1 if none is found (tree empty), and
25592f38b3e1SArne Jansen  * < 0 on error
25602f38b3e1SArne Jansen  */
25612f38b3e1SArne Jansen int btrfs_search_slot_for_read(struct btrfs_root *root,
2562310712b2SOmar Sandoval 			       const struct btrfs_key *key,
2563310712b2SOmar Sandoval 			       struct btrfs_path *p, int find_higher,
2564310712b2SOmar Sandoval 			       int return_any)
25652f38b3e1SArne Jansen {
25662f38b3e1SArne Jansen 	int ret;
25672f38b3e1SArne Jansen 	struct extent_buffer *leaf;
25682f38b3e1SArne Jansen 
25692f38b3e1SArne Jansen again:
25702f38b3e1SArne Jansen 	ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
25712f38b3e1SArne Jansen 	if (ret <= 0)
25722f38b3e1SArne Jansen 		return ret;
25732f38b3e1SArne Jansen 	/*
25742f38b3e1SArne Jansen 	 * a return value of 1 means the path is at the position where the
25752f38b3e1SArne Jansen 	 * item should be inserted. Normally this is the next bigger item,
25762f38b3e1SArne Jansen 	 * but in case the previous item is the last in a leaf, path points
25772f38b3e1SArne Jansen 	 * to the first free slot in the previous leaf, i.e. at an invalid
25782f38b3e1SArne Jansen 	 * item.
25792f38b3e1SArne Jansen 	 */
25802f38b3e1SArne Jansen 	leaf = p->nodes[0];
25812f38b3e1SArne Jansen 
25822f38b3e1SArne Jansen 	if (find_higher) {
25832f38b3e1SArne Jansen 		if (p->slots[0] >= btrfs_header_nritems(leaf)) {
25842f38b3e1SArne Jansen 			ret = btrfs_next_leaf(root, p);
25852f38b3e1SArne Jansen 			if (ret <= 0)
25862f38b3e1SArne Jansen 				return ret;
25872f38b3e1SArne Jansen 			if (!return_any)
25882f38b3e1SArne Jansen 				return 1;
25892f38b3e1SArne Jansen 			/*
25902f38b3e1SArne Jansen 			 * no higher item found, return the next
25912f38b3e1SArne Jansen 			 * lower instead
25922f38b3e1SArne Jansen 			 */
25932f38b3e1SArne Jansen 			return_any = 0;
25942f38b3e1SArne Jansen 			find_higher = 0;
25952f38b3e1SArne Jansen 			btrfs_release_path(p);
25962f38b3e1SArne Jansen 			goto again;
25972f38b3e1SArne Jansen 		}
25982f38b3e1SArne Jansen 	} else {
25992f38b3e1SArne Jansen 		if (p->slots[0] == 0) {
26002f38b3e1SArne Jansen 			ret = btrfs_prev_leaf(root, p);
2601e6793769SArne Jansen 			if (ret < 0)
26022f38b3e1SArne Jansen 				return ret;
2603e6793769SArne Jansen 			if (!ret) {
260423c6bf6aSFilipe David Borba Manana 				leaf = p->nodes[0];
260523c6bf6aSFilipe David Borba Manana 				if (p->slots[0] == btrfs_header_nritems(leaf))
260623c6bf6aSFilipe David Borba Manana 					p->slots[0]--;
2607e6793769SArne Jansen 				return 0;
2608e6793769SArne Jansen 			}
26092f38b3e1SArne Jansen 			if (!return_any)
26102f38b3e1SArne Jansen 				return 1;
26112f38b3e1SArne Jansen 			/*
26122f38b3e1SArne Jansen 			 * no lower item found, return the next
26132f38b3e1SArne Jansen 			 * higher instead
26142f38b3e1SArne Jansen 			 */
26152f38b3e1SArne Jansen 			return_any = 0;
26162f38b3e1SArne Jansen 			find_higher = 1;
26172f38b3e1SArne Jansen 			btrfs_release_path(p);
26182f38b3e1SArne Jansen 			goto again;
2619e6793769SArne Jansen 		} else {
26202f38b3e1SArne Jansen 			--p->slots[0];
26212f38b3e1SArne Jansen 		}
26222f38b3e1SArne Jansen 	}
26232f38b3e1SArne Jansen 	return 0;
26242f38b3e1SArne Jansen }
26252f38b3e1SArne Jansen 
26262f38b3e1SArne Jansen /*
26270ff40a91SMarcos Paulo de Souza  * Execute search and call btrfs_previous_item to traverse backwards if the item
26280ff40a91SMarcos Paulo de Souza  * was not found.
26290ff40a91SMarcos Paulo de Souza  *
26300ff40a91SMarcos Paulo de Souza  * Return 0 if found, 1 if not found and < 0 if error.
26310ff40a91SMarcos Paulo de Souza  */
26320ff40a91SMarcos Paulo de Souza int btrfs_search_backwards(struct btrfs_root *root, struct btrfs_key *key,
26330ff40a91SMarcos Paulo de Souza 			   struct btrfs_path *path)
26340ff40a91SMarcos Paulo de Souza {
26350ff40a91SMarcos Paulo de Souza 	int ret;
26360ff40a91SMarcos Paulo de Souza 
26370ff40a91SMarcos Paulo de Souza 	ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
26380ff40a91SMarcos Paulo de Souza 	if (ret > 0)
26390ff40a91SMarcos Paulo de Souza 		ret = btrfs_previous_item(root, path, key->objectid, key->type);
26400ff40a91SMarcos Paulo de Souza 
26410ff40a91SMarcos Paulo de Souza 	if (ret == 0)
26420ff40a91SMarcos Paulo de Souza 		btrfs_item_key_to_cpu(path->nodes[0], key, path->slots[0]);
26430ff40a91SMarcos Paulo de Souza 
26440ff40a91SMarcos Paulo de Souza 	return ret;
26450ff40a91SMarcos Paulo de Souza }
26460ff40a91SMarcos Paulo de Souza 
264743dd529aSDavid Sterba /*
264862142be3SGabriel Niebler  * Search for a valid slot for the given path.
264962142be3SGabriel Niebler  *
265062142be3SGabriel Niebler  * @root:	The root node of the tree.
265162142be3SGabriel Niebler  * @key:	Will contain a valid item if found.
265262142be3SGabriel Niebler  * @path:	The starting point to validate the slot.
265362142be3SGabriel Niebler  *
265462142be3SGabriel Niebler  * Return: 0  if the item is valid
265562142be3SGabriel Niebler  *         1  if not found
265662142be3SGabriel Niebler  *         <0 if error.
265762142be3SGabriel Niebler  */
265862142be3SGabriel Niebler int btrfs_get_next_valid_item(struct btrfs_root *root, struct btrfs_key *key,
265962142be3SGabriel Niebler 			      struct btrfs_path *path)
266062142be3SGabriel Niebler {
2661524f14bbSFilipe Manana 	if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
266262142be3SGabriel Niebler 		int ret;
266362142be3SGabriel Niebler 
266462142be3SGabriel Niebler 		ret = btrfs_next_leaf(root, path);
266562142be3SGabriel Niebler 		if (ret)
266662142be3SGabriel Niebler 			return ret;
266762142be3SGabriel Niebler 	}
2668524f14bbSFilipe Manana 
2669524f14bbSFilipe Manana 	btrfs_item_key_to_cpu(path->nodes[0], key, path->slots[0]);
267062142be3SGabriel Niebler 	return 0;
267162142be3SGabriel Niebler }
267262142be3SGabriel Niebler 
26730ff40a91SMarcos Paulo de Souza /*
267474123bd7SChris Mason  * adjust the pointers going up the tree, starting at level
267574123bd7SChris Mason  * making sure the right key of each node is points to 'key'.
267674123bd7SChris Mason  * This is used after shifting pointers to the left, so it stops
267774123bd7SChris Mason  * fixing up pointers when a given leaf/node is not in slot 0 of the
267874123bd7SChris Mason  * higher levels
2679aa5d6bedSChris Mason  *
268074123bd7SChris Mason  */
2681b167fa91SNikolay Borisov static void fixup_low_keys(struct btrfs_path *path,
26825f39d397SChris Mason 			   struct btrfs_disk_key *key, int level)
2683be0e5c09SChris Mason {
2684be0e5c09SChris Mason 	int i;
26855f39d397SChris Mason 	struct extent_buffer *t;
26860e82bcfeSDavid Sterba 	int ret;
26875f39d397SChris Mason 
2688234b63a0SChris Mason 	for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2689be0e5c09SChris Mason 		int tslot = path->slots[i];
26900e82bcfeSDavid Sterba 
2691eb60ceacSChris Mason 		if (!path->nodes[i])
2692be0e5c09SChris Mason 			break;
26935f39d397SChris Mason 		t = path->nodes[i];
2694f3a84ccdSFilipe Manana 		ret = btrfs_tree_mod_log_insert_key(t, tslot,
269533cff222SFilipe Manana 						    BTRFS_MOD_LOG_KEY_REPLACE);
26960e82bcfeSDavid Sterba 		BUG_ON(ret < 0);
26975f39d397SChris Mason 		btrfs_set_node_key(t, key, tslot);
2698d6025579SChris Mason 		btrfs_mark_buffer_dirty(path->nodes[i]);
2699be0e5c09SChris Mason 		if (tslot != 0)
2700be0e5c09SChris Mason 			break;
2701be0e5c09SChris Mason 	}
2702be0e5c09SChris Mason }
2703be0e5c09SChris Mason 
270474123bd7SChris Mason /*
270531840ae1SZheng Yan  * update item key.
270631840ae1SZheng Yan  *
270731840ae1SZheng Yan  * This function isn't completely safe. It's the caller's responsibility
270831840ae1SZheng Yan  * that the new key won't break the order
270931840ae1SZheng Yan  */
2710b7a0365eSDaniel Dressler void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
2711b7a0365eSDaniel Dressler 			     struct btrfs_path *path,
2712310712b2SOmar Sandoval 			     const struct btrfs_key *new_key)
271331840ae1SZheng Yan {
271431840ae1SZheng Yan 	struct btrfs_disk_key disk_key;
271531840ae1SZheng Yan 	struct extent_buffer *eb;
271631840ae1SZheng Yan 	int slot;
271731840ae1SZheng Yan 
271831840ae1SZheng Yan 	eb = path->nodes[0];
271931840ae1SZheng Yan 	slot = path->slots[0];
272031840ae1SZheng Yan 	if (slot > 0) {
272131840ae1SZheng Yan 		btrfs_item_key(eb, &disk_key, slot - 1);
27227c15d410SQu Wenruo 		if (unlikely(comp_keys(&disk_key, new_key) >= 0)) {
2723eee3b811SQu Wenruo 			btrfs_print_leaf(eb);
27247c15d410SQu Wenruo 			btrfs_crit(fs_info,
27257c15d410SQu Wenruo 		"slot %u key (%llu %u %llu) new key (%llu %u %llu)",
27267c15d410SQu Wenruo 				   slot, btrfs_disk_key_objectid(&disk_key),
27277c15d410SQu Wenruo 				   btrfs_disk_key_type(&disk_key),
27287c15d410SQu Wenruo 				   btrfs_disk_key_offset(&disk_key),
27297c15d410SQu Wenruo 				   new_key->objectid, new_key->type,
27307c15d410SQu Wenruo 				   new_key->offset);
27317c15d410SQu Wenruo 			BUG();
27327c15d410SQu Wenruo 		}
273331840ae1SZheng Yan 	}
273431840ae1SZheng Yan 	if (slot < btrfs_header_nritems(eb) - 1) {
273531840ae1SZheng Yan 		btrfs_item_key(eb, &disk_key, slot + 1);
27367c15d410SQu Wenruo 		if (unlikely(comp_keys(&disk_key, new_key) <= 0)) {
2737eee3b811SQu Wenruo 			btrfs_print_leaf(eb);
27387c15d410SQu Wenruo 			btrfs_crit(fs_info,
27397c15d410SQu Wenruo 		"slot %u key (%llu %u %llu) new key (%llu %u %llu)",
27407c15d410SQu Wenruo 				   slot, btrfs_disk_key_objectid(&disk_key),
27417c15d410SQu Wenruo 				   btrfs_disk_key_type(&disk_key),
27427c15d410SQu Wenruo 				   btrfs_disk_key_offset(&disk_key),
27437c15d410SQu Wenruo 				   new_key->objectid, new_key->type,
27447c15d410SQu Wenruo 				   new_key->offset);
27457c15d410SQu Wenruo 			BUG();
27467c15d410SQu Wenruo 		}
274731840ae1SZheng Yan 	}
274831840ae1SZheng Yan 
274931840ae1SZheng Yan 	btrfs_cpu_key_to_disk(&disk_key, new_key);
275031840ae1SZheng Yan 	btrfs_set_item_key(eb, &disk_key, slot);
275131840ae1SZheng Yan 	btrfs_mark_buffer_dirty(eb);
275231840ae1SZheng Yan 	if (slot == 0)
2753b167fa91SNikolay Borisov 		fixup_low_keys(path, &disk_key, 1);
275431840ae1SZheng Yan }
275531840ae1SZheng Yan 
275631840ae1SZheng Yan /*
2757d16c702fSQu Wenruo  * Check key order of two sibling extent buffers.
2758d16c702fSQu Wenruo  *
2759d16c702fSQu Wenruo  * Return true if something is wrong.
2760d16c702fSQu Wenruo  * Return false if everything is fine.
2761d16c702fSQu Wenruo  *
2762d16c702fSQu Wenruo  * Tree-checker only works inside one tree block, thus the following
2763d16c702fSQu Wenruo  * corruption can not be detected by tree-checker:
2764d16c702fSQu Wenruo  *
2765d16c702fSQu Wenruo  * Leaf @left			| Leaf @right
2766d16c702fSQu Wenruo  * --------------------------------------------------------------
2767d16c702fSQu Wenruo  * | 1 | 2 | 3 | 4 | 5 | f6 |   | 7 | 8 |
2768d16c702fSQu Wenruo  *
2769d16c702fSQu Wenruo  * Key f6 in leaf @left itself is valid, but not valid when the next
2770d16c702fSQu Wenruo  * key in leaf @right is 7.
2771d16c702fSQu Wenruo  * This can only be checked at tree block merge time.
2772d16c702fSQu Wenruo  * And since tree checker has ensured all key order in each tree block
2773d16c702fSQu Wenruo  * is correct, we only need to bother the last key of @left and the first
2774d16c702fSQu Wenruo  * key of @right.
2775d16c702fSQu Wenruo  */
2776d16c702fSQu Wenruo static bool check_sibling_keys(struct extent_buffer *left,
2777d16c702fSQu Wenruo 			       struct extent_buffer *right)
2778d16c702fSQu Wenruo {
2779d16c702fSQu Wenruo 	struct btrfs_key left_last;
2780d16c702fSQu Wenruo 	struct btrfs_key right_first;
2781d16c702fSQu Wenruo 	int level = btrfs_header_level(left);
2782d16c702fSQu Wenruo 	int nr_left = btrfs_header_nritems(left);
2783d16c702fSQu Wenruo 	int nr_right = btrfs_header_nritems(right);
2784d16c702fSQu Wenruo 
2785d16c702fSQu Wenruo 	/* No key to check in one of the tree blocks */
2786d16c702fSQu Wenruo 	if (!nr_left || !nr_right)
2787d16c702fSQu Wenruo 		return false;
2788d16c702fSQu Wenruo 
2789d16c702fSQu Wenruo 	if (level) {
2790d16c702fSQu Wenruo 		btrfs_node_key_to_cpu(left, &left_last, nr_left - 1);
2791d16c702fSQu Wenruo 		btrfs_node_key_to_cpu(right, &right_first, 0);
2792d16c702fSQu Wenruo 	} else {
2793d16c702fSQu Wenruo 		btrfs_item_key_to_cpu(left, &left_last, nr_left - 1);
2794d16c702fSQu Wenruo 		btrfs_item_key_to_cpu(right, &right_first, 0);
2795d16c702fSQu Wenruo 	}
2796d16c702fSQu Wenruo 
279788ad95b0SFilipe Manana 	if (unlikely(btrfs_comp_cpu_keys(&left_last, &right_first) >= 0)) {
2798a2cea677SFilipe Manana 		btrfs_crit(left->fs_info, "left extent buffer:");
2799a2cea677SFilipe Manana 		btrfs_print_tree(left, false);
2800a2cea677SFilipe Manana 		btrfs_crit(left->fs_info, "right extent buffer:");
2801a2cea677SFilipe Manana 		btrfs_print_tree(right, false);
2802d16c702fSQu Wenruo 		btrfs_crit(left->fs_info,
2803d16c702fSQu Wenruo "bad key order, sibling blocks, left last (%llu %u %llu) right first (%llu %u %llu)",
2804d16c702fSQu Wenruo 			   left_last.objectid, left_last.type,
2805d16c702fSQu Wenruo 			   left_last.offset, right_first.objectid,
2806d16c702fSQu Wenruo 			   right_first.type, right_first.offset);
2807d16c702fSQu Wenruo 		return true;
2808d16c702fSQu Wenruo 	}
2809d16c702fSQu Wenruo 	return false;
2810d16c702fSQu Wenruo }
2811d16c702fSQu Wenruo 
2812d16c702fSQu Wenruo /*
281374123bd7SChris Mason  * try to push data from one node into the next node left in the
281479f95c82SChris Mason  * tree.
2815aa5d6bedSChris Mason  *
2816aa5d6bedSChris Mason  * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2817aa5d6bedSChris Mason  * error, and > 0 if there was no room in the left hand block.
281874123bd7SChris Mason  */
281998ed5174SChris Mason static int push_node_left(struct btrfs_trans_handle *trans,
28202ff7e61eSJeff Mahoney 			  struct extent_buffer *dst,
2821971a1f66SChris Mason 			  struct extent_buffer *src, int empty)
2822be0e5c09SChris Mason {
2823d30a668fSDavid Sterba 	struct btrfs_fs_info *fs_info = trans->fs_info;
2824be0e5c09SChris Mason 	int push_items = 0;
2825bb803951SChris Mason 	int src_nritems;
2826bb803951SChris Mason 	int dst_nritems;
2827aa5d6bedSChris Mason 	int ret = 0;
2828be0e5c09SChris Mason 
28295f39d397SChris Mason 	src_nritems = btrfs_header_nritems(src);
28305f39d397SChris Mason 	dst_nritems = btrfs_header_nritems(dst);
28310b246afaSJeff Mahoney 	push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
28327bb86316SChris Mason 	WARN_ON(btrfs_header_generation(src) != trans->transid);
28337bb86316SChris Mason 	WARN_ON(btrfs_header_generation(dst) != trans->transid);
283454aa1f4dSChris Mason 
2835bce4eae9SChris Mason 	if (!empty && src_nritems <= 8)
2836971a1f66SChris Mason 		return 1;
2837971a1f66SChris Mason 
2838d397712bSChris Mason 	if (push_items <= 0)
2839be0e5c09SChris Mason 		return 1;
2840be0e5c09SChris Mason 
2841bce4eae9SChris Mason 	if (empty) {
2842971a1f66SChris Mason 		push_items = min(src_nritems, push_items);
2843bce4eae9SChris Mason 		if (push_items < src_nritems) {
2844bce4eae9SChris Mason 			/* leave at least 8 pointers in the node if
2845bce4eae9SChris Mason 			 * we aren't going to empty it
2846bce4eae9SChris Mason 			 */
2847bce4eae9SChris Mason 			if (src_nritems - push_items < 8) {
2848bce4eae9SChris Mason 				if (push_items <= 8)
2849bce4eae9SChris Mason 					return 1;
2850bce4eae9SChris Mason 				push_items -= 8;
2851bce4eae9SChris Mason 			}
2852bce4eae9SChris Mason 		}
2853bce4eae9SChris Mason 	} else
2854bce4eae9SChris Mason 		push_items = min(src_nritems - 8, push_items);
285579f95c82SChris Mason 
2856d16c702fSQu Wenruo 	/* dst is the left eb, src is the middle eb */
2857d16c702fSQu Wenruo 	if (check_sibling_keys(dst, src)) {
2858d16c702fSQu Wenruo 		ret = -EUCLEAN;
2859d16c702fSQu Wenruo 		btrfs_abort_transaction(trans, ret);
2860d16c702fSQu Wenruo 		return ret;
2861d16c702fSQu Wenruo 	}
2862f3a84ccdSFilipe Manana 	ret = btrfs_tree_mod_log_eb_copy(dst, src, dst_nritems, 0, push_items);
28635de865eeSFilipe David Borba Manana 	if (ret) {
286466642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
28655de865eeSFilipe David Borba Manana 		return ret;
28665de865eeSFilipe David Borba Manana 	}
28675f39d397SChris Mason 	copy_extent_buffer(dst, src,
2868e23efd8eSJosef Bacik 			   btrfs_node_key_ptr_offset(dst, dst_nritems),
2869e23efd8eSJosef Bacik 			   btrfs_node_key_ptr_offset(src, 0),
2870123abc88SChris Mason 			   push_items * sizeof(struct btrfs_key_ptr));
28715f39d397SChris Mason 
2872bb803951SChris Mason 	if (push_items < src_nritems) {
287357911b8bSJan Schmidt 		/*
28745cead542SBoris Burkov 		 * btrfs_tree_mod_log_eb_copy handles logging the move, so we
28755cead542SBoris Burkov 		 * don't need to do an explicit tree mod log operation for it.
287657911b8bSJan Schmidt 		 */
2877e23efd8eSJosef Bacik 		memmove_extent_buffer(src, btrfs_node_key_ptr_offset(src, 0),
2878e23efd8eSJosef Bacik 				      btrfs_node_key_ptr_offset(src, push_items),
2879e2fa7227SChris Mason 				      (src_nritems - push_items) *
2880123abc88SChris Mason 				      sizeof(struct btrfs_key_ptr));
2881bb803951SChris Mason 	}
28825f39d397SChris Mason 	btrfs_set_header_nritems(src, src_nritems - push_items);
28835f39d397SChris Mason 	btrfs_set_header_nritems(dst, dst_nritems + push_items);
28845f39d397SChris Mason 	btrfs_mark_buffer_dirty(src);
28855f39d397SChris Mason 	btrfs_mark_buffer_dirty(dst);
288631840ae1SZheng Yan 
2887bb803951SChris Mason 	return ret;
2888be0e5c09SChris Mason }
2889be0e5c09SChris Mason 
289097571fd0SChris Mason /*
289179f95c82SChris Mason  * try to push data from one node into the next node right in the
289279f95c82SChris Mason  * tree.
289379f95c82SChris Mason  *
289479f95c82SChris Mason  * returns 0 if some ptrs were pushed, < 0 if there was some horrible
289579f95c82SChris Mason  * error, and > 0 if there was no room in the right hand block.
289679f95c82SChris Mason  *
289779f95c82SChris Mason  * this will  only push up to 1/2 the contents of the left node over
289879f95c82SChris Mason  */
28995f39d397SChris Mason static int balance_node_right(struct btrfs_trans_handle *trans,
29005f39d397SChris Mason 			      struct extent_buffer *dst,
29015f39d397SChris Mason 			      struct extent_buffer *src)
290279f95c82SChris Mason {
290355d32ed8SDavid Sterba 	struct btrfs_fs_info *fs_info = trans->fs_info;
290479f95c82SChris Mason 	int push_items = 0;
290579f95c82SChris Mason 	int max_push;
290679f95c82SChris Mason 	int src_nritems;
290779f95c82SChris Mason 	int dst_nritems;
290879f95c82SChris Mason 	int ret = 0;
290979f95c82SChris Mason 
29107bb86316SChris Mason 	WARN_ON(btrfs_header_generation(src) != trans->transid);
29117bb86316SChris Mason 	WARN_ON(btrfs_header_generation(dst) != trans->transid);
29127bb86316SChris Mason 
29135f39d397SChris Mason 	src_nritems = btrfs_header_nritems(src);
29145f39d397SChris Mason 	dst_nritems = btrfs_header_nritems(dst);
29150b246afaSJeff Mahoney 	push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
2916d397712bSChris Mason 	if (push_items <= 0)
291779f95c82SChris Mason 		return 1;
2918bce4eae9SChris Mason 
2919d397712bSChris Mason 	if (src_nritems < 4)
2920bce4eae9SChris Mason 		return 1;
292179f95c82SChris Mason 
292279f95c82SChris Mason 	max_push = src_nritems / 2 + 1;
292379f95c82SChris Mason 	/* don't try to empty the node */
2924d397712bSChris Mason 	if (max_push >= src_nritems)
292579f95c82SChris Mason 		return 1;
2926252c38f0SYan 
292779f95c82SChris Mason 	if (max_push < push_items)
292879f95c82SChris Mason 		push_items = max_push;
292979f95c82SChris Mason 
2930d16c702fSQu Wenruo 	/* dst is the right eb, src is the middle eb */
2931d16c702fSQu Wenruo 	if (check_sibling_keys(src, dst)) {
2932d16c702fSQu Wenruo 		ret = -EUCLEAN;
2933d16c702fSQu Wenruo 		btrfs_abort_transaction(trans, ret);
2934d16c702fSQu Wenruo 		return ret;
2935d16c702fSQu Wenruo 	}
29365cead542SBoris Burkov 
29375cead542SBoris Burkov 	/*
29385cead542SBoris Burkov 	 * btrfs_tree_mod_log_eb_copy handles logging the move, so we don't
29395cead542SBoris Burkov 	 * need to do an explicit tree mod log operation for it.
29405cead542SBoris Burkov 	 */
2941e23efd8eSJosef Bacik 	memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(dst, push_items),
2942e23efd8eSJosef Bacik 				      btrfs_node_key_ptr_offset(dst, 0),
29435f39d397SChris Mason 				      (dst_nritems) *
29445f39d397SChris Mason 				      sizeof(struct btrfs_key_ptr));
2945d6025579SChris Mason 
2946f3a84ccdSFilipe Manana 	ret = btrfs_tree_mod_log_eb_copy(dst, src, 0, src_nritems - push_items,
2947ed874f0dSDavid Sterba 					 push_items);
29485de865eeSFilipe David Borba Manana 	if (ret) {
294966642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
29505de865eeSFilipe David Borba Manana 		return ret;
29515de865eeSFilipe David Borba Manana 	}
29525f39d397SChris Mason 	copy_extent_buffer(dst, src,
2953e23efd8eSJosef Bacik 			   btrfs_node_key_ptr_offset(dst, 0),
2954e23efd8eSJosef Bacik 			   btrfs_node_key_ptr_offset(src, src_nritems - push_items),
2955123abc88SChris Mason 			   push_items * sizeof(struct btrfs_key_ptr));
295679f95c82SChris Mason 
29575f39d397SChris Mason 	btrfs_set_header_nritems(src, src_nritems - push_items);
29585f39d397SChris Mason 	btrfs_set_header_nritems(dst, dst_nritems + push_items);
295979f95c82SChris Mason 
29605f39d397SChris Mason 	btrfs_mark_buffer_dirty(src);
29615f39d397SChris Mason 	btrfs_mark_buffer_dirty(dst);
296231840ae1SZheng Yan 
296379f95c82SChris Mason 	return ret;
296479f95c82SChris Mason }
296579f95c82SChris Mason 
296679f95c82SChris Mason /*
296797571fd0SChris Mason  * helper function to insert a new root level in the tree.
296897571fd0SChris Mason  * A new node is allocated, and a single item is inserted to
296997571fd0SChris Mason  * point to the existing root
2970aa5d6bedSChris Mason  *
2971aa5d6bedSChris Mason  * returns zero on success or < 0 on failure.
297297571fd0SChris Mason  */
2973d397712bSChris Mason static noinline int insert_new_root(struct btrfs_trans_handle *trans,
29745f39d397SChris Mason 			   struct btrfs_root *root,
2975fdd99c72SLiu Bo 			   struct btrfs_path *path, int level)
297674123bd7SChris Mason {
29770b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
29787bb86316SChris Mason 	u64 lower_gen;
29795f39d397SChris Mason 	struct extent_buffer *lower;
29805f39d397SChris Mason 	struct extent_buffer *c;
2981925baeddSChris Mason 	struct extent_buffer *old;
29825f39d397SChris Mason 	struct btrfs_disk_key lower_key;
2983d9d19a01SDavid Sterba 	int ret;
29845c680ed6SChris Mason 
29855c680ed6SChris Mason 	BUG_ON(path->nodes[level]);
29865c680ed6SChris Mason 	BUG_ON(path->nodes[level-1] != root->node);
29875c680ed6SChris Mason 
29887bb86316SChris Mason 	lower = path->nodes[level-1];
29897bb86316SChris Mason 	if (level == 1)
29907bb86316SChris Mason 		btrfs_item_key(lower, &lower_key, 0);
29917bb86316SChris Mason 	else
29927bb86316SChris Mason 		btrfs_node_key(lower, &lower_key, 0);
29937bb86316SChris Mason 
299479bd3712SFilipe Manana 	c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
299579bd3712SFilipe Manana 				   &lower_key, level, root->node->start, 0,
2996cf6f34aaSJosef Bacik 				   BTRFS_NESTING_NEW_ROOT);
29975f39d397SChris Mason 	if (IS_ERR(c))
29985f39d397SChris Mason 		return PTR_ERR(c);
2999925baeddSChris Mason 
30000b246afaSJeff Mahoney 	root_add_used(root, fs_info->nodesize);
3001f0486c68SYan, Zheng 
30025f39d397SChris Mason 	btrfs_set_header_nritems(c, 1);
30035f39d397SChris Mason 	btrfs_set_node_key(c, &lower_key, 0);
3004db94535dSChris Mason 	btrfs_set_node_blockptr(c, 0, lower->start);
30057bb86316SChris Mason 	lower_gen = btrfs_header_generation(lower);
300631840ae1SZheng Yan 	WARN_ON(lower_gen != trans->transid);
30077bb86316SChris Mason 
30087bb86316SChris Mason 	btrfs_set_node_ptr_generation(c, 0, lower_gen);
30095f39d397SChris Mason 
30105f39d397SChris Mason 	btrfs_mark_buffer_dirty(c);
3011d5719762SChris Mason 
3012925baeddSChris Mason 	old = root->node;
3013406808abSFilipe Manana 	ret = btrfs_tree_mod_log_insert_root(root->node, c, false);
3014f61aa7baSFilipe Manana 	if (ret < 0) {
3015f61aa7baSFilipe Manana 		btrfs_free_tree_block(trans, btrfs_root_id(root), c, 0, 1);
3016f61aa7baSFilipe Manana 		btrfs_tree_unlock(c);
3017f61aa7baSFilipe Manana 		free_extent_buffer(c);
3018f61aa7baSFilipe Manana 		return ret;
3019f61aa7baSFilipe Manana 	}
3020240f62c8SChris Mason 	rcu_assign_pointer(root->node, c);
3021925baeddSChris Mason 
3022925baeddSChris Mason 	/* the super has an extra ref to root->node */
3023925baeddSChris Mason 	free_extent_buffer(old);
3024925baeddSChris Mason 
30250b86a832SChris Mason 	add_root_to_dirty_list(root);
302667439dadSDavid Sterba 	atomic_inc(&c->refs);
30275f39d397SChris Mason 	path->nodes[level] = c;
3028ac5887c8SJosef Bacik 	path->locks[level] = BTRFS_WRITE_LOCK;
302974123bd7SChris Mason 	path->slots[level] = 0;
303074123bd7SChris Mason 	return 0;
303174123bd7SChris Mason }
30325c680ed6SChris Mason 
30335c680ed6SChris Mason /*
30345c680ed6SChris Mason  * worker function to insert a single pointer in a node.
30355c680ed6SChris Mason  * the node should have enough room for the pointer already
303697571fd0SChris Mason  *
30375c680ed6SChris Mason  * slot and level indicate where you want the key to go, and
30385c680ed6SChris Mason  * blocknr is the block the key points to.
30395c680ed6SChris Mason  */
304050b5d1fcSFilipe Manana static int insert_ptr(struct btrfs_trans_handle *trans,
30416ad3cf6dSDavid Sterba 		      struct btrfs_path *path,
3042143bede5SJeff Mahoney 		      struct btrfs_disk_key *key, u64 bytenr,
3043c3e06965SJan Schmidt 		      int slot, int level)
30445c680ed6SChris Mason {
30455f39d397SChris Mason 	struct extent_buffer *lower;
30465c680ed6SChris Mason 	int nritems;
3047f3ea38daSJan Schmidt 	int ret;
30485c680ed6SChris Mason 
30495c680ed6SChris Mason 	BUG_ON(!path->nodes[level]);
305049d0c642SFilipe Manana 	btrfs_assert_tree_write_locked(path->nodes[level]);
30515f39d397SChris Mason 	lower = path->nodes[level];
30525f39d397SChris Mason 	nritems = btrfs_header_nritems(lower);
3053c293498bSStoyan Gaydarov 	BUG_ON(slot > nritems);
30546ad3cf6dSDavid Sterba 	BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(trans->fs_info));
305574123bd7SChris Mason 	if (slot != nritems) {
3056bf1d3425SDavid Sterba 		if (level) {
3057f3a84ccdSFilipe Manana 			ret = btrfs_tree_mod_log_insert_move(lower, slot + 1,
3058f3a84ccdSFilipe Manana 					slot, nritems - slot);
305950b5d1fcSFilipe Manana 			if (ret < 0) {
306050b5d1fcSFilipe Manana 				btrfs_abort_transaction(trans, ret);
306150b5d1fcSFilipe Manana 				return ret;
306250b5d1fcSFilipe Manana 			}
3063bf1d3425SDavid Sterba 		}
30645f39d397SChris Mason 		memmove_extent_buffer(lower,
3065e23efd8eSJosef Bacik 			      btrfs_node_key_ptr_offset(lower, slot + 1),
3066e23efd8eSJosef Bacik 			      btrfs_node_key_ptr_offset(lower, slot),
3067123abc88SChris Mason 			      (nritems - slot) * sizeof(struct btrfs_key_ptr));
306874123bd7SChris Mason 	}
3069c3e06965SJan Schmidt 	if (level) {
3070f3a84ccdSFilipe Manana 		ret = btrfs_tree_mod_log_insert_key(lower, slot,
307133cff222SFilipe Manana 						    BTRFS_MOD_LOG_KEY_ADD);
307250b5d1fcSFilipe Manana 		if (ret < 0) {
307350b5d1fcSFilipe Manana 			btrfs_abort_transaction(trans, ret);
307450b5d1fcSFilipe Manana 			return ret;
307550b5d1fcSFilipe Manana 		}
3076f3ea38daSJan Schmidt 	}
30775f39d397SChris Mason 	btrfs_set_node_key(lower, key, slot);
3078db94535dSChris Mason 	btrfs_set_node_blockptr(lower, slot, bytenr);
307974493f7aSChris Mason 	WARN_ON(trans->transid == 0);
308074493f7aSChris Mason 	btrfs_set_node_ptr_generation(lower, slot, trans->transid);
30815f39d397SChris Mason 	btrfs_set_header_nritems(lower, nritems + 1);
30825f39d397SChris Mason 	btrfs_mark_buffer_dirty(lower);
308350b5d1fcSFilipe Manana 
308450b5d1fcSFilipe Manana 	return 0;
308574123bd7SChris Mason }
308674123bd7SChris Mason 
308797571fd0SChris Mason /*
308897571fd0SChris Mason  * split the node at the specified level in path in two.
308997571fd0SChris Mason  * The path is corrected to point to the appropriate node after the split
309097571fd0SChris Mason  *
309197571fd0SChris Mason  * Before splitting this tries to make some room in the node by pushing
309297571fd0SChris Mason  * left and right, if either one works, it returns right away.
3093aa5d6bedSChris Mason  *
3094aa5d6bedSChris Mason  * returns 0 on success and < 0 on failure
309597571fd0SChris Mason  */
3096e02119d5SChris Mason static noinline int split_node(struct btrfs_trans_handle *trans,
3097e02119d5SChris Mason 			       struct btrfs_root *root,
3098e02119d5SChris Mason 			       struct btrfs_path *path, int level)
3099be0e5c09SChris Mason {
31000b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
31015f39d397SChris Mason 	struct extent_buffer *c;
31025f39d397SChris Mason 	struct extent_buffer *split;
31035f39d397SChris Mason 	struct btrfs_disk_key disk_key;
3104be0e5c09SChris Mason 	int mid;
31055c680ed6SChris Mason 	int ret;
31067518a238SChris Mason 	u32 c_nritems;
3107be0e5c09SChris Mason 
31085f39d397SChris Mason 	c = path->nodes[level];
31097bb86316SChris Mason 	WARN_ON(btrfs_header_generation(c) != trans->transid);
31105f39d397SChris Mason 	if (c == root->node) {
3111d9abbf1cSJan Schmidt 		/*
311290f8d62eSJan Schmidt 		 * trying to split the root, lets make a new one
311390f8d62eSJan Schmidt 		 *
3114fdd99c72SLiu Bo 		 * tree mod log: We don't log_removal old root in
311590f8d62eSJan Schmidt 		 * insert_new_root, because that root buffer will be kept as a
311690f8d62eSJan Schmidt 		 * normal node. We are going to log removal of half of the
3117f3a84ccdSFilipe Manana 		 * elements below with btrfs_tree_mod_log_eb_copy(). We're
3118f3a84ccdSFilipe Manana 		 * holding a tree lock on the buffer, which is why we cannot
3119f3a84ccdSFilipe Manana 		 * race with other tree_mod_log users.
3120d9abbf1cSJan Schmidt 		 */
3121fdd99c72SLiu Bo 		ret = insert_new_root(trans, root, path, level + 1);
31225c680ed6SChris Mason 		if (ret)
31235c680ed6SChris Mason 			return ret;
3124b3612421SChris Mason 	} else {
3125e66f709bSChris Mason 		ret = push_nodes_for_insert(trans, root, path, level);
31265f39d397SChris Mason 		c = path->nodes[level];
31275f39d397SChris Mason 		if (!ret && btrfs_header_nritems(c) <
31280b246afaSJeff Mahoney 		    BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
3129e66f709bSChris Mason 			return 0;
313054aa1f4dSChris Mason 		if (ret < 0)
313154aa1f4dSChris Mason 			return ret;
31325c680ed6SChris Mason 	}
3133e66f709bSChris Mason 
31345f39d397SChris Mason 	c_nritems = btrfs_header_nritems(c);
31355d4f98a2SYan Zheng 	mid = (c_nritems + 1) / 2;
31365d4f98a2SYan Zheng 	btrfs_node_key(c, &disk_key, mid);
31377bb86316SChris Mason 
313879bd3712SFilipe Manana 	split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
313979bd3712SFilipe Manana 				       &disk_key, level, c->start, 0,
314079bd3712SFilipe Manana 				       BTRFS_NESTING_SPLIT);
31415f39d397SChris Mason 	if (IS_ERR(split))
31425f39d397SChris Mason 		return PTR_ERR(split);
314354aa1f4dSChris Mason 
31440b246afaSJeff Mahoney 	root_add_used(root, fs_info->nodesize);
3145bc877d28SNikolay Borisov 	ASSERT(btrfs_header_level(c) == level);
31465f39d397SChris Mason 
3147f3a84ccdSFilipe Manana 	ret = btrfs_tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid);
31485de865eeSFilipe David Borba Manana 	if (ret) {
3149ede600e4SFilipe Manana 		btrfs_tree_unlock(split);
3150ede600e4SFilipe Manana 		free_extent_buffer(split);
315166642832SJeff Mahoney 		btrfs_abort_transaction(trans, ret);
31525de865eeSFilipe David Borba Manana 		return ret;
31535de865eeSFilipe David Borba Manana 	}
31545f39d397SChris Mason 	copy_extent_buffer(split, c,
3155e23efd8eSJosef Bacik 			   btrfs_node_key_ptr_offset(split, 0),
3156e23efd8eSJosef Bacik 			   btrfs_node_key_ptr_offset(c, mid),
3157123abc88SChris Mason 			   (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
31585f39d397SChris Mason 	btrfs_set_header_nritems(split, c_nritems - mid);
31595f39d397SChris Mason 	btrfs_set_header_nritems(c, mid);
3160aa5d6bedSChris Mason 
31615f39d397SChris Mason 	btrfs_mark_buffer_dirty(c);
31625f39d397SChris Mason 	btrfs_mark_buffer_dirty(split);
31635f39d397SChris Mason 
316450b5d1fcSFilipe Manana 	ret = insert_ptr(trans, path, &disk_key, split->start,
3165c3e06965SJan Schmidt 			 path->slots[level + 1] + 1, level + 1);
316650b5d1fcSFilipe Manana 	if (ret < 0) {
316750b5d1fcSFilipe Manana 		btrfs_tree_unlock(split);
316850b5d1fcSFilipe Manana 		free_extent_buffer(split);
316950b5d1fcSFilipe Manana 		return ret;
317050b5d1fcSFilipe Manana 	}
3171aa5d6bedSChris Mason 
31725de08d7dSChris Mason 	if (path->slots[level] >= mid) {
31735c680ed6SChris Mason 		path->slots[level] -= mid;
3174925baeddSChris Mason 		btrfs_tree_unlock(c);
31755f39d397SChris Mason 		free_extent_buffer(c);
31765f39d397SChris Mason 		path->nodes[level] = split;
31775c680ed6SChris Mason 		path->slots[level + 1] += 1;
3178eb60ceacSChris Mason 	} else {
3179925baeddSChris Mason 		btrfs_tree_unlock(split);
31805f39d397SChris Mason 		free_extent_buffer(split);
3181be0e5c09SChris Mason 	}
3182d5286a92SNikolay Borisov 	return 0;
3183be0e5c09SChris Mason }
3184be0e5c09SChris Mason 
318574123bd7SChris Mason /*
318674123bd7SChris Mason  * how many bytes are required to store the items in a leaf.  start
318774123bd7SChris Mason  * and nr indicate which items in the leaf to check.  This totals up the
318874123bd7SChris Mason  * space used both by the item structs and the item data
318974123bd7SChris Mason  */
31906c75a589SQu Wenruo static int leaf_space_used(const struct extent_buffer *l, int start, int nr)
3191be0e5c09SChris Mason {
3192be0e5c09SChris Mason 	int data_len;
31935f39d397SChris Mason 	int nritems = btrfs_header_nritems(l);
3194d4dbff95SChris Mason 	int end = min(nritems, start + nr) - 1;
3195be0e5c09SChris Mason 
3196be0e5c09SChris Mason 	if (!nr)
3197be0e5c09SChris Mason 		return 0;
31983212fa14SJosef Bacik 	data_len = btrfs_item_offset(l, start) + btrfs_item_size(l, start);
31993212fa14SJosef Bacik 	data_len = data_len - btrfs_item_offset(l, end);
32000783fcfcSChris Mason 	data_len += sizeof(struct btrfs_item) * nr;
3201d4dbff95SChris Mason 	WARN_ON(data_len < 0);
3202be0e5c09SChris Mason 	return data_len;
3203be0e5c09SChris Mason }
3204be0e5c09SChris Mason 
320574123bd7SChris Mason /*
3206d4dbff95SChris Mason  * The space between the end of the leaf items and
3207d4dbff95SChris Mason  * the start of the leaf data.  IOW, how much room
3208d4dbff95SChris Mason  * the leaf has left for both items and data
3209d4dbff95SChris Mason  */
32106c75a589SQu Wenruo int btrfs_leaf_free_space(const struct extent_buffer *leaf)
3211d4dbff95SChris Mason {
3212e902baacSDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
32135f39d397SChris Mason 	int nritems = btrfs_header_nritems(leaf);
32145f39d397SChris Mason 	int ret;
32150b246afaSJeff Mahoney 
32160b246afaSJeff Mahoney 	ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
32175f39d397SChris Mason 	if (ret < 0) {
32180b246afaSJeff Mahoney 		btrfs_crit(fs_info,
3219efe120a0SFrank Holton 			   "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
3220da17066cSJeff Mahoney 			   ret,
32210b246afaSJeff Mahoney 			   (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
32225f39d397SChris Mason 			   leaf_space_used(leaf, 0, nritems), nritems);
32235f39d397SChris Mason 	}
32245f39d397SChris Mason 	return ret;
3225d4dbff95SChris Mason }
3226d4dbff95SChris Mason 
322799d8f83cSChris Mason /*
322899d8f83cSChris Mason  * min slot controls the lowest index we're willing to push to the
322999d8f83cSChris Mason  * right.  We'll push up to and including min_slot, but no lower
323099d8f83cSChris Mason  */
3231ed25dab3SJosef Bacik static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3232ed25dab3SJosef Bacik 				      struct btrfs_path *path,
323344871b1bSChris Mason 				      int data_size, int empty,
323444871b1bSChris Mason 				      struct extent_buffer *right,
323599d8f83cSChris Mason 				      int free_space, u32 left_nritems,
323699d8f83cSChris Mason 				      u32 min_slot)
323700ec4c51SChris Mason {
3238f72f0010SDavid Sterba 	struct btrfs_fs_info *fs_info = right->fs_info;
32395f39d397SChris Mason 	struct extent_buffer *left = path->nodes[0];
324044871b1bSChris Mason 	struct extent_buffer *upper = path->nodes[1];
3241cfed81a0SChris Mason 	struct btrfs_map_token token;
32425f39d397SChris Mason 	struct btrfs_disk_key disk_key;
324300ec4c51SChris Mason 	int slot;
324434a38218SChris Mason 	u32 i;
324500ec4c51SChris Mason 	int push_space = 0;
324600ec4c51SChris Mason 	int push_items = 0;
324734a38218SChris Mason 	u32 nr;
32487518a238SChris Mason 	u32 right_nritems;
32495f39d397SChris Mason 	u32 data_end;
3250db94535dSChris Mason 	u32 this_item_size;
325100ec4c51SChris Mason 
325234a38218SChris Mason 	if (empty)
325334a38218SChris Mason 		nr = 0;
325434a38218SChris Mason 	else
325599d8f83cSChris Mason 		nr = max_t(u32, 1, min_slot);
325634a38218SChris Mason 
325731840ae1SZheng Yan 	if (path->slots[0] >= left_nritems)
325887b29b20SYan Zheng 		push_space += data_size;
325931840ae1SZheng Yan 
326044871b1bSChris Mason 	slot = path->slots[1];
326134a38218SChris Mason 	i = left_nritems - 1;
326234a38218SChris Mason 	while (i >= nr) {
326331840ae1SZheng Yan 		if (!empty && push_items > 0) {
326431840ae1SZheng Yan 			if (path->slots[0] > i)
326531840ae1SZheng Yan 				break;
326631840ae1SZheng Yan 			if (path->slots[0] == i) {
3267e902baacSDavid Sterba 				int space = btrfs_leaf_free_space(left);
3268e902baacSDavid Sterba 
326931840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
327031840ae1SZheng Yan 					break;
327131840ae1SZheng Yan 			}
327231840ae1SZheng Yan 		}
327331840ae1SZheng Yan 
327400ec4c51SChris Mason 		if (path->slots[0] == i)
327587b29b20SYan Zheng 			push_space += data_size;
3276db94535dSChris Mason 
32773212fa14SJosef Bacik 		this_item_size = btrfs_item_size(left, i);
327874794207SJosef Bacik 		if (this_item_size + sizeof(struct btrfs_item) +
327974794207SJosef Bacik 		    push_space > free_space)
328000ec4c51SChris Mason 			break;
328131840ae1SZheng Yan 
328200ec4c51SChris Mason 		push_items++;
328374794207SJosef Bacik 		push_space += this_item_size + sizeof(struct btrfs_item);
328434a38218SChris Mason 		if (i == 0)
328534a38218SChris Mason 			break;
328634a38218SChris Mason 		i--;
3287db94535dSChris Mason 	}
32885f39d397SChris Mason 
3289925baeddSChris Mason 	if (push_items == 0)
3290925baeddSChris Mason 		goto out_unlock;
32915f39d397SChris Mason 
32926c1500f2SJulia Lawall 	WARN_ON(!empty && push_items == left_nritems);
32935f39d397SChris Mason 
329400ec4c51SChris Mason 	/* push left to right */
32955f39d397SChris Mason 	right_nritems = btrfs_header_nritems(right);
329634a38218SChris Mason 
3297dc2e724eSJosef Bacik 	push_space = btrfs_item_data_end(left, left_nritems - push_items);
32988f881e8cSDavid Sterba 	push_space -= leaf_data_end(left);
32995f39d397SChris Mason 
330000ec4c51SChris Mason 	/* make room in the right data area */
33018f881e8cSDavid Sterba 	data_end = leaf_data_end(right);
3302637e3b48SJosef Bacik 	memmove_leaf_data(right, data_end - push_space, data_end,
33030b246afaSJeff Mahoney 			  BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
33045f39d397SChris Mason 
330500ec4c51SChris Mason 	/* copy from the left data area */
3306637e3b48SJosef Bacik 	copy_leaf_data(right, left, BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
3307637e3b48SJosef Bacik 		       leaf_data_end(left), push_space);
33085f39d397SChris Mason 
3309637e3b48SJosef Bacik 	memmove_leaf_items(right, push_items, 0, right_nritems);
33105f39d397SChris Mason 
331100ec4c51SChris Mason 	/* copy the items from left to right */
3312637e3b48SJosef Bacik 	copy_leaf_items(right, left, 0, left_nritems - push_items, push_items);
331300ec4c51SChris Mason 
331400ec4c51SChris Mason 	/* update the item pointers */
3315c82f823cSDavid Sterba 	btrfs_init_map_token(&token, right);
33167518a238SChris Mason 	right_nritems += push_items;
33175f39d397SChris Mason 	btrfs_set_header_nritems(right, right_nritems);
33180b246afaSJeff Mahoney 	push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
33197518a238SChris Mason 	for (i = 0; i < right_nritems; i++) {
33203212fa14SJosef Bacik 		push_space -= btrfs_token_item_size(&token, i);
33213212fa14SJosef Bacik 		btrfs_set_token_item_offset(&token, i, push_space);
3322db94535dSChris Mason 	}
3323db94535dSChris Mason 
33247518a238SChris Mason 	left_nritems -= push_items;
33255f39d397SChris Mason 	btrfs_set_header_nritems(left, left_nritems);
332600ec4c51SChris Mason 
332734a38218SChris Mason 	if (left_nritems)
33285f39d397SChris Mason 		btrfs_mark_buffer_dirty(left);
3329f0486c68SYan, Zheng 	else
3330190a8339SJosef Bacik 		btrfs_clear_buffer_dirty(trans, left);
3331f0486c68SYan, Zheng 
33325f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
3333a429e513SChris Mason 
33345f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
33355f39d397SChris Mason 	btrfs_set_node_key(upper, &disk_key, slot + 1);
3336d6025579SChris Mason 	btrfs_mark_buffer_dirty(upper);
333702217ed2SChris Mason 
333800ec4c51SChris Mason 	/* then fixup the leaf pointer in the path */
33397518a238SChris Mason 	if (path->slots[0] >= left_nritems) {
33407518a238SChris Mason 		path->slots[0] -= left_nritems;
3341925baeddSChris Mason 		if (btrfs_header_nritems(path->nodes[0]) == 0)
3342190a8339SJosef Bacik 			btrfs_clear_buffer_dirty(trans, path->nodes[0]);
3343925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
33445f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
33455f39d397SChris Mason 		path->nodes[0] = right;
334600ec4c51SChris Mason 		path->slots[1] += 1;
334700ec4c51SChris Mason 	} else {
3348925baeddSChris Mason 		btrfs_tree_unlock(right);
33495f39d397SChris Mason 		free_extent_buffer(right);
335000ec4c51SChris Mason 	}
335100ec4c51SChris Mason 	return 0;
3352925baeddSChris Mason 
3353925baeddSChris Mason out_unlock:
3354925baeddSChris Mason 	btrfs_tree_unlock(right);
3355925baeddSChris Mason 	free_extent_buffer(right);
3356925baeddSChris Mason 	return 1;
335700ec4c51SChris Mason }
3358925baeddSChris Mason 
335900ec4c51SChris Mason /*
336044871b1bSChris Mason  * push some data in the path leaf to the right, trying to free up at
336174123bd7SChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
336244871b1bSChris Mason  *
336344871b1bSChris Mason  * returns 1 if the push failed because the other node didn't have enough
336444871b1bSChris Mason  * room, 0 if everything worked out and < 0 if there were major errors.
336599d8f83cSChris Mason  *
336699d8f83cSChris Mason  * this will push starting from min_slot to the end of the leaf.  It won't
336799d8f83cSChris Mason  * push any slot lower than min_slot
336874123bd7SChris Mason  */
336944871b1bSChris Mason static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
337099d8f83cSChris Mason 			   *root, struct btrfs_path *path,
337199d8f83cSChris Mason 			   int min_data_size, int data_size,
337299d8f83cSChris Mason 			   int empty, u32 min_slot)
3373be0e5c09SChris Mason {
337444871b1bSChris Mason 	struct extent_buffer *left = path->nodes[0];
337544871b1bSChris Mason 	struct extent_buffer *right;
337644871b1bSChris Mason 	struct extent_buffer *upper;
337744871b1bSChris Mason 	int slot;
337844871b1bSChris Mason 	int free_space;
337944871b1bSChris Mason 	u32 left_nritems;
338044871b1bSChris Mason 	int ret;
338144871b1bSChris Mason 
338244871b1bSChris Mason 	if (!path->nodes[1])
338344871b1bSChris Mason 		return 1;
338444871b1bSChris Mason 
338544871b1bSChris Mason 	slot = path->slots[1];
338644871b1bSChris Mason 	upper = path->nodes[1];
338744871b1bSChris Mason 	if (slot >= btrfs_header_nritems(upper) - 1)
338844871b1bSChris Mason 		return 1;
338944871b1bSChris Mason 
339049d0c642SFilipe Manana 	btrfs_assert_tree_write_locked(path->nodes[1]);
339144871b1bSChris Mason 
33924b231ae4SDavid Sterba 	right = btrfs_read_node_slot(upper, slot + 1);
3393fb770ae4SLiu Bo 	if (IS_ERR(right))
33949cf14029SJosef Bacik 		return PTR_ERR(right);
339591ca338dSTsutomu Itoh 
3396bf77467aSJosef Bacik 	__btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
339744871b1bSChris Mason 
3398e902baacSDavid Sterba 	free_space = btrfs_leaf_free_space(right);
339944871b1bSChris Mason 	if (free_space < data_size)
340044871b1bSChris Mason 		goto out_unlock;
340144871b1bSChris Mason 
340244871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, right, upper,
3403bf59a5a2SJosef Bacik 			      slot + 1, &right, BTRFS_NESTING_RIGHT_COW);
340444871b1bSChris Mason 	if (ret)
340544871b1bSChris Mason 		goto out_unlock;
340644871b1bSChris Mason 
340744871b1bSChris Mason 	left_nritems = btrfs_header_nritems(left);
340844871b1bSChris Mason 	if (left_nritems == 0)
340944871b1bSChris Mason 		goto out_unlock;
341044871b1bSChris Mason 
3411d16c702fSQu Wenruo 	if (check_sibling_keys(left, right)) {
3412d16c702fSQu Wenruo 		ret = -EUCLEAN;
34139ae5afd0SFilipe Manana 		btrfs_abort_transaction(trans, ret);
3414d16c702fSQu Wenruo 		btrfs_tree_unlock(right);
3415d16c702fSQu Wenruo 		free_extent_buffer(right);
3416d16c702fSQu Wenruo 		return ret;
3417d16c702fSQu Wenruo 	}
34182ef1fed2SFilipe David Borba Manana 	if (path->slots[0] == left_nritems && !empty) {
34192ef1fed2SFilipe David Borba Manana 		/* Key greater than all keys in the leaf, right neighbor has
34202ef1fed2SFilipe David Borba Manana 		 * enough room for it and we're not emptying our leaf to delete
34212ef1fed2SFilipe David Borba Manana 		 * it, therefore use right neighbor to insert the new item and
342252042d8eSAndrea Gelmini 		 * no need to touch/dirty our left leaf. */
34232ef1fed2SFilipe David Borba Manana 		btrfs_tree_unlock(left);
34242ef1fed2SFilipe David Borba Manana 		free_extent_buffer(left);
34252ef1fed2SFilipe David Borba Manana 		path->nodes[0] = right;
34262ef1fed2SFilipe David Borba Manana 		path->slots[0] = 0;
34272ef1fed2SFilipe David Borba Manana 		path->slots[1]++;
34282ef1fed2SFilipe David Borba Manana 		return 0;
34292ef1fed2SFilipe David Borba Manana 	}
34302ef1fed2SFilipe David Borba Manana 
3431ed25dab3SJosef Bacik 	return __push_leaf_right(trans, path, min_data_size, empty, right,
3432ed25dab3SJosef Bacik 				 free_space, left_nritems, min_slot);
343344871b1bSChris Mason out_unlock:
343444871b1bSChris Mason 	btrfs_tree_unlock(right);
343544871b1bSChris Mason 	free_extent_buffer(right);
343644871b1bSChris Mason 	return 1;
343744871b1bSChris Mason }
343844871b1bSChris Mason 
343944871b1bSChris Mason /*
344044871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
344144871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
344299d8f83cSChris Mason  *
344399d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
344499d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us do all the
344599d8f83cSChris Mason  * items
344644871b1bSChris Mason  */
3447ed25dab3SJosef Bacik static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3448ed25dab3SJosef Bacik 				     struct btrfs_path *path, int data_size,
344944871b1bSChris Mason 				     int empty, struct extent_buffer *left,
345099d8f83cSChris Mason 				     int free_space, u32 right_nritems,
345199d8f83cSChris Mason 				     u32 max_slot)
345244871b1bSChris Mason {
34538087c193SDavid Sterba 	struct btrfs_fs_info *fs_info = left->fs_info;
34545f39d397SChris Mason 	struct btrfs_disk_key disk_key;
34555f39d397SChris Mason 	struct extent_buffer *right = path->nodes[0];
3456be0e5c09SChris Mason 	int i;
3457be0e5c09SChris Mason 	int push_space = 0;
3458be0e5c09SChris Mason 	int push_items = 0;
34597518a238SChris Mason 	u32 old_left_nritems;
346034a38218SChris Mason 	u32 nr;
3461aa5d6bedSChris Mason 	int ret = 0;
3462db94535dSChris Mason 	u32 this_item_size;
3463db94535dSChris Mason 	u32 old_left_item_size;
3464cfed81a0SChris Mason 	struct btrfs_map_token token;
3465cfed81a0SChris Mason 
346634a38218SChris Mason 	if (empty)
346799d8f83cSChris Mason 		nr = min(right_nritems, max_slot);
346834a38218SChris Mason 	else
346999d8f83cSChris Mason 		nr = min(right_nritems - 1, max_slot);
347034a38218SChris Mason 
347134a38218SChris Mason 	for (i = 0; i < nr; i++) {
347231840ae1SZheng Yan 		if (!empty && push_items > 0) {
347331840ae1SZheng Yan 			if (path->slots[0] < i)
347431840ae1SZheng Yan 				break;
347531840ae1SZheng Yan 			if (path->slots[0] == i) {
3476e902baacSDavid Sterba 				int space = btrfs_leaf_free_space(right);
3477e902baacSDavid Sterba 
347831840ae1SZheng Yan 				if (space + push_space * 2 > free_space)
347931840ae1SZheng Yan 					break;
348031840ae1SZheng Yan 			}
348131840ae1SZheng Yan 		}
348231840ae1SZheng Yan 
3483be0e5c09SChris Mason 		if (path->slots[0] == i)
348487b29b20SYan Zheng 			push_space += data_size;
3485db94535dSChris Mason 
34863212fa14SJosef Bacik 		this_item_size = btrfs_item_size(right, i);
348774794207SJosef Bacik 		if (this_item_size + sizeof(struct btrfs_item) + push_space >
348874794207SJosef Bacik 		    free_space)
3489be0e5c09SChris Mason 			break;
3490db94535dSChris Mason 
3491be0e5c09SChris Mason 		push_items++;
349274794207SJosef Bacik 		push_space += this_item_size + sizeof(struct btrfs_item);
3493be0e5c09SChris Mason 	}
3494db94535dSChris Mason 
3495be0e5c09SChris Mason 	if (push_items == 0) {
3496925baeddSChris Mason 		ret = 1;
3497925baeddSChris Mason 		goto out;
3498be0e5c09SChris Mason 	}
3499fae7f21cSDulshani Gunawardhana 	WARN_ON(!empty && push_items == btrfs_header_nritems(right));
35005f39d397SChris Mason 
3501be0e5c09SChris Mason 	/* push data from right to left */
3502637e3b48SJosef Bacik 	copy_leaf_items(left, right, btrfs_header_nritems(left), 0, push_items);
35035f39d397SChris Mason 
35040b246afaSJeff Mahoney 	push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
35053212fa14SJosef Bacik 		     btrfs_item_offset(right, push_items - 1);
35065f39d397SChris Mason 
3507637e3b48SJosef Bacik 	copy_leaf_data(left, right, leaf_data_end(left) - push_space,
3508637e3b48SJosef Bacik 		       btrfs_item_offset(right, push_items - 1), push_space);
35095f39d397SChris Mason 	old_left_nritems = btrfs_header_nritems(left);
351087b29b20SYan Zheng 	BUG_ON(old_left_nritems <= 0);
3511eb60ceacSChris Mason 
3512c82f823cSDavid Sterba 	btrfs_init_map_token(&token, left);
35133212fa14SJosef Bacik 	old_left_item_size = btrfs_item_offset(left, old_left_nritems - 1);
3514be0e5c09SChris Mason 	for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
35155f39d397SChris Mason 		u32 ioff;
3516db94535dSChris Mason 
35173212fa14SJosef Bacik 		ioff = btrfs_token_item_offset(&token, i);
35183212fa14SJosef Bacik 		btrfs_set_token_item_offset(&token, i,
3519cc4c13d5SDavid Sterba 		      ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size));
3520be0e5c09SChris Mason 	}
35215f39d397SChris Mason 	btrfs_set_header_nritems(left, old_left_nritems + push_items);
3522be0e5c09SChris Mason 
3523be0e5c09SChris Mason 	/* fixup right node */
352431b1a2bdSJulia Lawall 	if (push_items > right_nritems)
352531b1a2bdSJulia Lawall 		WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
3526d397712bSChris Mason 		       right_nritems);
352734a38218SChris Mason 
352834a38218SChris Mason 	if (push_items < right_nritems) {
35293212fa14SJosef Bacik 		push_space = btrfs_item_offset(right, push_items - 1) -
35308f881e8cSDavid Sterba 						  leaf_data_end(right);
3531637e3b48SJosef Bacik 		memmove_leaf_data(right,
35320b246afaSJeff Mahoney 				  BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
35338f881e8cSDavid Sterba 				  leaf_data_end(right), push_space);
35345f39d397SChris Mason 
3535637e3b48SJosef Bacik 		memmove_leaf_items(right, 0, push_items,
3536637e3b48SJosef Bacik 				   btrfs_header_nritems(right) - push_items);
353734a38218SChris Mason 	}
3538c82f823cSDavid Sterba 
3539c82f823cSDavid Sterba 	btrfs_init_map_token(&token, right);
3540eef1c494SYan 	right_nritems -= push_items;
3541eef1c494SYan 	btrfs_set_header_nritems(right, right_nritems);
35420b246afaSJeff Mahoney 	push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
35435f39d397SChris Mason 	for (i = 0; i < right_nritems; i++) {
35443212fa14SJosef Bacik 		push_space = push_space - btrfs_token_item_size(&token, i);
35453212fa14SJosef Bacik 		btrfs_set_token_item_offset(&token, i, push_space);
3546db94535dSChris Mason 	}
3547eb60ceacSChris Mason 
35485f39d397SChris Mason 	btrfs_mark_buffer_dirty(left);
354934a38218SChris Mason 	if (right_nritems)
35505f39d397SChris Mason 		btrfs_mark_buffer_dirty(right);
3551f0486c68SYan, Zheng 	else
3552190a8339SJosef Bacik 		btrfs_clear_buffer_dirty(trans, right);
3553098f59c2SChris Mason 
35545f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
3555b167fa91SNikolay Borisov 	fixup_low_keys(path, &disk_key, 1);
3556be0e5c09SChris Mason 
3557be0e5c09SChris Mason 	/* then fixup the leaf pointer in the path */
3558be0e5c09SChris Mason 	if (path->slots[0] < push_items) {
3559be0e5c09SChris Mason 		path->slots[0] += old_left_nritems;
3560925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
35615f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
35625f39d397SChris Mason 		path->nodes[0] = left;
3563be0e5c09SChris Mason 		path->slots[1] -= 1;
3564be0e5c09SChris Mason 	} else {
3565925baeddSChris Mason 		btrfs_tree_unlock(left);
35665f39d397SChris Mason 		free_extent_buffer(left);
3567be0e5c09SChris Mason 		path->slots[0] -= push_items;
3568be0e5c09SChris Mason 	}
3569eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
3570aa5d6bedSChris Mason 	return ret;
3571925baeddSChris Mason out:
3572925baeddSChris Mason 	btrfs_tree_unlock(left);
3573925baeddSChris Mason 	free_extent_buffer(left);
3574925baeddSChris Mason 	return ret;
3575be0e5c09SChris Mason }
3576be0e5c09SChris Mason 
357774123bd7SChris Mason /*
357844871b1bSChris Mason  * push some data in the path leaf to the left, trying to free up at
357944871b1bSChris Mason  * least data_size bytes.  returns zero if the push worked, nonzero otherwise
358099d8f83cSChris Mason  *
358199d8f83cSChris Mason  * max_slot can put a limit on how far into the leaf we'll push items.  The
358299d8f83cSChris Mason  * item at 'max_slot' won't be touched.  Use (u32)-1 to make us push all the
358399d8f83cSChris Mason  * items
358444871b1bSChris Mason  */
358544871b1bSChris Mason static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
358699d8f83cSChris Mason 			  *root, struct btrfs_path *path, int min_data_size,
358799d8f83cSChris Mason 			  int data_size, int empty, u32 max_slot)
358844871b1bSChris Mason {
358944871b1bSChris Mason 	struct extent_buffer *right = path->nodes[0];
359044871b1bSChris Mason 	struct extent_buffer *left;
359144871b1bSChris Mason 	int slot;
359244871b1bSChris Mason 	int free_space;
359344871b1bSChris Mason 	u32 right_nritems;
359444871b1bSChris Mason 	int ret = 0;
359544871b1bSChris Mason 
359644871b1bSChris Mason 	slot = path->slots[1];
359744871b1bSChris Mason 	if (slot == 0)
359844871b1bSChris Mason 		return 1;
359944871b1bSChris Mason 	if (!path->nodes[1])
360044871b1bSChris Mason 		return 1;
360144871b1bSChris Mason 
360244871b1bSChris Mason 	right_nritems = btrfs_header_nritems(right);
360344871b1bSChris Mason 	if (right_nritems == 0)
360444871b1bSChris Mason 		return 1;
360544871b1bSChris Mason 
360649d0c642SFilipe Manana 	btrfs_assert_tree_write_locked(path->nodes[1]);
360744871b1bSChris Mason 
36084b231ae4SDavid Sterba 	left = btrfs_read_node_slot(path->nodes[1], slot - 1);
3609fb770ae4SLiu Bo 	if (IS_ERR(left))
36109cf14029SJosef Bacik 		return PTR_ERR(left);
361191ca338dSTsutomu Itoh 
3612bf77467aSJosef Bacik 	__btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
361344871b1bSChris Mason 
3614e902baacSDavid Sterba 	free_space = btrfs_leaf_free_space(left);
361544871b1bSChris Mason 	if (free_space < data_size) {
361644871b1bSChris Mason 		ret = 1;
361744871b1bSChris Mason 		goto out;
361844871b1bSChris Mason 	}
361944871b1bSChris Mason 
362044871b1bSChris Mason 	ret = btrfs_cow_block(trans, root, left,
36219631e4ccSJosef Bacik 			      path->nodes[1], slot - 1, &left,
3622bf59a5a2SJosef Bacik 			      BTRFS_NESTING_LEFT_COW);
362344871b1bSChris Mason 	if (ret) {
362444871b1bSChris Mason 		/* we hit -ENOSPC, but it isn't fatal here */
362579787eaaSJeff Mahoney 		if (ret == -ENOSPC)
362644871b1bSChris Mason 			ret = 1;
362744871b1bSChris Mason 		goto out;
362844871b1bSChris Mason 	}
362944871b1bSChris Mason 
3630d16c702fSQu Wenruo 	if (check_sibling_keys(left, right)) {
3631d16c702fSQu Wenruo 		ret = -EUCLEAN;
36329ae5afd0SFilipe Manana 		btrfs_abort_transaction(trans, ret);
3633d16c702fSQu Wenruo 		goto out;
3634d16c702fSQu Wenruo 	}
3635ed25dab3SJosef Bacik 	return __push_leaf_left(trans, path, min_data_size, empty, left,
3636ed25dab3SJosef Bacik 				free_space, right_nritems, max_slot);
363744871b1bSChris Mason out:
363844871b1bSChris Mason 	btrfs_tree_unlock(left);
363944871b1bSChris Mason 	free_extent_buffer(left);
364044871b1bSChris Mason 	return ret;
364144871b1bSChris Mason }
364244871b1bSChris Mason 
364344871b1bSChris Mason /*
364474123bd7SChris Mason  * split the path's leaf in two, making sure there is at least data_size
364574123bd7SChris Mason  * available for the resulting leaf level of the path.
364674123bd7SChris Mason  */
364750b5d1fcSFilipe Manana static noinline int copy_for_split(struct btrfs_trans_handle *trans,
364844871b1bSChris Mason 				   struct btrfs_path *path,
364944871b1bSChris Mason 				   struct extent_buffer *l,
365044871b1bSChris Mason 				   struct extent_buffer *right,
365144871b1bSChris Mason 				   int slot, int mid, int nritems)
3652be0e5c09SChris Mason {
365394f94ad9SDavid Sterba 	struct btrfs_fs_info *fs_info = trans->fs_info;
3654be0e5c09SChris Mason 	int data_copy_size;
3655be0e5c09SChris Mason 	int rt_data_off;
3656be0e5c09SChris Mason 	int i;
365750b5d1fcSFilipe Manana 	int ret;
3658d4dbff95SChris Mason 	struct btrfs_disk_key disk_key;
3659cfed81a0SChris Mason 	struct btrfs_map_token token;
3660cfed81a0SChris Mason 
36615f39d397SChris Mason 	nritems = nritems - mid;
36625f39d397SChris Mason 	btrfs_set_header_nritems(right, nritems);
3663dc2e724eSJosef Bacik 	data_copy_size = btrfs_item_data_end(l, mid) - leaf_data_end(l);
36645f39d397SChris Mason 
3665637e3b48SJosef Bacik 	copy_leaf_items(right, l, 0, mid, nritems);
36665f39d397SChris Mason 
3667637e3b48SJosef Bacik 	copy_leaf_data(right, l, BTRFS_LEAF_DATA_SIZE(fs_info) - data_copy_size,
36688f881e8cSDavid Sterba 		       leaf_data_end(l), data_copy_size);
366974123bd7SChris Mason 
3670dc2e724eSJosef Bacik 	rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_data_end(l, mid);
36715f39d397SChris Mason 
3672c82f823cSDavid Sterba 	btrfs_init_map_token(&token, right);
36735f39d397SChris Mason 	for (i = 0; i < nritems; i++) {
3674db94535dSChris Mason 		u32 ioff;
3675db94535dSChris Mason 
36763212fa14SJosef Bacik 		ioff = btrfs_token_item_offset(&token, i);
36773212fa14SJosef Bacik 		btrfs_set_token_item_offset(&token, i, ioff + rt_data_off);
36780783fcfcSChris Mason 	}
367974123bd7SChris Mason 
36805f39d397SChris Mason 	btrfs_set_header_nritems(l, mid);
36815f39d397SChris Mason 	btrfs_item_key(right, &disk_key, 0);
368250b5d1fcSFilipe Manana 	ret = insert_ptr(trans, path, &disk_key, right->start, path->slots[1] + 1, 1);
368350b5d1fcSFilipe Manana 	if (ret < 0)
368450b5d1fcSFilipe Manana 		return ret;
36855f39d397SChris Mason 
36865f39d397SChris Mason 	btrfs_mark_buffer_dirty(right);
36875f39d397SChris Mason 	btrfs_mark_buffer_dirty(l);
3688eb60ceacSChris Mason 	BUG_ON(path->slots[0] != slot);
36895f39d397SChris Mason 
3690be0e5c09SChris Mason 	if (mid <= slot) {
3691925baeddSChris Mason 		btrfs_tree_unlock(path->nodes[0]);
36925f39d397SChris Mason 		free_extent_buffer(path->nodes[0]);
36935f39d397SChris Mason 		path->nodes[0] = right;
3694be0e5c09SChris Mason 		path->slots[0] -= mid;
3695be0e5c09SChris Mason 		path->slots[1] += 1;
3696925baeddSChris Mason 	} else {
3697925baeddSChris Mason 		btrfs_tree_unlock(right);
36985f39d397SChris Mason 		free_extent_buffer(right);
3699925baeddSChris Mason 	}
37005f39d397SChris Mason 
3701eb60ceacSChris Mason 	BUG_ON(path->slots[0] < 0);
370250b5d1fcSFilipe Manana 
370350b5d1fcSFilipe Manana 	return 0;
370444871b1bSChris Mason }
370544871b1bSChris Mason 
370644871b1bSChris Mason /*
370799d8f83cSChris Mason  * double splits happen when we need to insert a big item in the middle
370899d8f83cSChris Mason  * of a leaf.  A double split can leave us with 3 mostly empty leaves:
370999d8f83cSChris Mason  * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
371099d8f83cSChris Mason  *          A                 B                 C
371199d8f83cSChris Mason  *
371299d8f83cSChris Mason  * We avoid this by trying to push the items on either side of our target
371399d8f83cSChris Mason  * into the adjacent leaves.  If all goes well we can avoid the double split
371499d8f83cSChris Mason  * completely.
371599d8f83cSChris Mason  */
371699d8f83cSChris Mason static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
371799d8f83cSChris Mason 					  struct btrfs_root *root,
371899d8f83cSChris Mason 					  struct btrfs_path *path,
371999d8f83cSChris Mason 					  int data_size)
372099d8f83cSChris Mason {
372199d8f83cSChris Mason 	int ret;
372299d8f83cSChris Mason 	int progress = 0;
372399d8f83cSChris Mason 	int slot;
372499d8f83cSChris Mason 	u32 nritems;
37255a4267caSFilipe David Borba Manana 	int space_needed = data_size;
372699d8f83cSChris Mason 
372799d8f83cSChris Mason 	slot = path->slots[0];
37285a4267caSFilipe David Borba Manana 	if (slot < btrfs_header_nritems(path->nodes[0]))
3729e902baacSDavid Sterba 		space_needed -= btrfs_leaf_free_space(path->nodes[0]);
373099d8f83cSChris Mason 
373199d8f83cSChris Mason 	/*
373299d8f83cSChris Mason 	 * try to push all the items after our slot into the
373399d8f83cSChris Mason 	 * right leaf
373499d8f83cSChris Mason 	 */
37355a4267caSFilipe David Borba Manana 	ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
373699d8f83cSChris Mason 	if (ret < 0)
373799d8f83cSChris Mason 		return ret;
373899d8f83cSChris Mason 
373999d8f83cSChris Mason 	if (ret == 0)
374099d8f83cSChris Mason 		progress++;
374199d8f83cSChris Mason 
374299d8f83cSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
374399d8f83cSChris Mason 	/*
374499d8f83cSChris Mason 	 * our goal is to get our slot at the start or end of a leaf.  If
374599d8f83cSChris Mason 	 * we've done so we're done
374699d8f83cSChris Mason 	 */
374799d8f83cSChris Mason 	if (path->slots[0] == 0 || path->slots[0] == nritems)
374899d8f83cSChris Mason 		return 0;
374999d8f83cSChris Mason 
3750e902baacSDavid Sterba 	if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
375199d8f83cSChris Mason 		return 0;
375299d8f83cSChris Mason 
375399d8f83cSChris Mason 	/* try to push all the items before our slot into the next leaf */
375499d8f83cSChris Mason 	slot = path->slots[0];
3755263d3995SFilipe Manana 	space_needed = data_size;
3756263d3995SFilipe Manana 	if (slot > 0)
3757e902baacSDavid Sterba 		space_needed -= btrfs_leaf_free_space(path->nodes[0]);
37585a4267caSFilipe David Borba Manana 	ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
375999d8f83cSChris Mason 	if (ret < 0)
376099d8f83cSChris Mason 		return ret;
376199d8f83cSChris Mason 
376299d8f83cSChris Mason 	if (ret == 0)
376399d8f83cSChris Mason 		progress++;
376499d8f83cSChris Mason 
376599d8f83cSChris Mason 	if (progress)
376699d8f83cSChris Mason 		return 0;
376799d8f83cSChris Mason 	return 1;
376899d8f83cSChris Mason }
376999d8f83cSChris Mason 
377099d8f83cSChris Mason /*
377144871b1bSChris Mason  * split the path's leaf in two, making sure there is at least data_size
377244871b1bSChris Mason  * available for the resulting leaf level of the path.
377344871b1bSChris Mason  *
377444871b1bSChris Mason  * returns 0 if all went well and < 0 on failure.
377544871b1bSChris Mason  */
377644871b1bSChris Mason static noinline int split_leaf(struct btrfs_trans_handle *trans,
377744871b1bSChris Mason 			       struct btrfs_root *root,
3778310712b2SOmar Sandoval 			       const struct btrfs_key *ins_key,
377944871b1bSChris Mason 			       struct btrfs_path *path, int data_size,
378044871b1bSChris Mason 			       int extend)
378144871b1bSChris Mason {
37825d4f98a2SYan Zheng 	struct btrfs_disk_key disk_key;
378344871b1bSChris Mason 	struct extent_buffer *l;
378444871b1bSChris Mason 	u32 nritems;
378544871b1bSChris Mason 	int mid;
378644871b1bSChris Mason 	int slot;
378744871b1bSChris Mason 	struct extent_buffer *right;
3788b7a0365eSDaniel Dressler 	struct btrfs_fs_info *fs_info = root->fs_info;
378944871b1bSChris Mason 	int ret = 0;
379044871b1bSChris Mason 	int wret;
37915d4f98a2SYan Zheng 	int split;
379244871b1bSChris Mason 	int num_doubles = 0;
379399d8f83cSChris Mason 	int tried_avoid_double = 0;
379444871b1bSChris Mason 
3795a5719521SYan, Zheng 	l = path->nodes[0];
3796a5719521SYan, Zheng 	slot = path->slots[0];
37973212fa14SJosef Bacik 	if (extend && data_size + btrfs_item_size(l, slot) +
37980b246afaSJeff Mahoney 	    sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
3799a5719521SYan, Zheng 		return -EOVERFLOW;
3800a5719521SYan, Zheng 
380144871b1bSChris Mason 	/* first try to make some room by pushing left and right */
380233157e05SLiu Bo 	if (data_size && path->nodes[1]) {
38035a4267caSFilipe David Borba Manana 		int space_needed = data_size;
38045a4267caSFilipe David Borba Manana 
38055a4267caSFilipe David Borba Manana 		if (slot < btrfs_header_nritems(l))
3806e902baacSDavid Sterba 			space_needed -= btrfs_leaf_free_space(l);
38075a4267caSFilipe David Borba Manana 
38085a4267caSFilipe David Borba Manana 		wret = push_leaf_right(trans, root, path, space_needed,
38095a4267caSFilipe David Borba Manana 				       space_needed, 0, 0);
381044871b1bSChris Mason 		if (wret < 0)
381144871b1bSChris Mason 			return wret;
381244871b1bSChris Mason 		if (wret) {
3813263d3995SFilipe Manana 			space_needed = data_size;
3814263d3995SFilipe Manana 			if (slot > 0)
3815e902baacSDavid Sterba 				space_needed -= btrfs_leaf_free_space(l);
38165a4267caSFilipe David Borba Manana 			wret = push_leaf_left(trans, root, path, space_needed,
38175a4267caSFilipe David Borba Manana 					      space_needed, 0, (u32)-1);
381844871b1bSChris Mason 			if (wret < 0)
381944871b1bSChris Mason 				return wret;
382044871b1bSChris Mason 		}
382144871b1bSChris Mason 		l = path->nodes[0];
382244871b1bSChris Mason 
382344871b1bSChris Mason 		/* did the pushes work? */
3824e902baacSDavid Sterba 		if (btrfs_leaf_free_space(l) >= data_size)
382544871b1bSChris Mason 			return 0;
382644871b1bSChris Mason 	}
382744871b1bSChris Mason 
382844871b1bSChris Mason 	if (!path->nodes[1]) {
3829fdd99c72SLiu Bo 		ret = insert_new_root(trans, root, path, 1);
383044871b1bSChris Mason 		if (ret)
383144871b1bSChris Mason 			return ret;
383244871b1bSChris Mason 	}
383344871b1bSChris Mason again:
38345d4f98a2SYan Zheng 	split = 1;
383544871b1bSChris Mason 	l = path->nodes[0];
383644871b1bSChris Mason 	slot = path->slots[0];
383744871b1bSChris Mason 	nritems = btrfs_header_nritems(l);
383844871b1bSChris Mason 	mid = (nritems + 1) / 2;
383944871b1bSChris Mason 
38405d4f98a2SYan Zheng 	if (mid <= slot) {
38415d4f98a2SYan Zheng 		if (nritems == 1 ||
38425d4f98a2SYan Zheng 		    leaf_space_used(l, mid, nritems - mid) + data_size >
38430b246afaSJeff Mahoney 			BTRFS_LEAF_DATA_SIZE(fs_info)) {
38445d4f98a2SYan Zheng 			if (slot >= nritems) {
38455d4f98a2SYan Zheng 				split = 0;
38465d4f98a2SYan Zheng 			} else {
38475d4f98a2SYan Zheng 				mid = slot;
38485d4f98a2SYan Zheng 				if (mid != nritems &&
38495d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
38500b246afaSJeff Mahoney 				    data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
385199d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
385299d8f83cSChris Mason 						goto push_for_double;
38535d4f98a2SYan Zheng 					split = 2;
38545d4f98a2SYan Zheng 				}
38555d4f98a2SYan Zheng 			}
38565d4f98a2SYan Zheng 		}
38575d4f98a2SYan Zheng 	} else {
38585d4f98a2SYan Zheng 		if (leaf_space_used(l, 0, mid) + data_size >
38590b246afaSJeff Mahoney 			BTRFS_LEAF_DATA_SIZE(fs_info)) {
38605d4f98a2SYan Zheng 			if (!extend && data_size && slot == 0) {
38615d4f98a2SYan Zheng 				split = 0;
38625d4f98a2SYan Zheng 			} else if ((extend || !data_size) && slot == 0) {
38635d4f98a2SYan Zheng 				mid = 1;
38645d4f98a2SYan Zheng 			} else {
38655d4f98a2SYan Zheng 				mid = slot;
38665d4f98a2SYan Zheng 				if (mid != nritems &&
38675d4f98a2SYan Zheng 				    leaf_space_used(l, mid, nritems - mid) +
38680b246afaSJeff Mahoney 				    data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
386999d8f83cSChris Mason 					if (data_size && !tried_avoid_double)
387099d8f83cSChris Mason 						goto push_for_double;
38715d4f98a2SYan Zheng 					split = 2;
38725d4f98a2SYan Zheng 				}
38735d4f98a2SYan Zheng 			}
38745d4f98a2SYan Zheng 		}
38755d4f98a2SYan Zheng 	}
38765d4f98a2SYan Zheng 
38775d4f98a2SYan Zheng 	if (split == 0)
38785d4f98a2SYan Zheng 		btrfs_cpu_key_to_disk(&disk_key, ins_key);
38795d4f98a2SYan Zheng 	else
38805d4f98a2SYan Zheng 		btrfs_item_key(l, &disk_key, mid);
38815d4f98a2SYan Zheng 
3882ca9d473aSJosef Bacik 	/*
3883ca9d473aSJosef Bacik 	 * We have to about BTRFS_NESTING_NEW_ROOT here if we've done a double
3884ca9d473aSJosef Bacik 	 * split, because we're only allowed to have MAX_LOCKDEP_SUBCLASSES
3885ca9d473aSJosef Bacik 	 * subclasses, which is 8 at the time of this patch, and we've maxed it
3886ca9d473aSJosef Bacik 	 * out.  In the future we could add a
3887ca9d473aSJosef Bacik 	 * BTRFS_NESTING_SPLIT_THE_SPLITTENING if we need to, but for now just
3888ca9d473aSJosef Bacik 	 * use BTRFS_NESTING_NEW_ROOT.
3889ca9d473aSJosef Bacik 	 */
389079bd3712SFilipe Manana 	right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
389179bd3712SFilipe Manana 				       &disk_key, 0, l->start, 0,
389279bd3712SFilipe Manana 				       num_doubles ? BTRFS_NESTING_NEW_ROOT :
3893ca9d473aSJosef Bacik 				       BTRFS_NESTING_SPLIT);
3894f0486c68SYan, Zheng 	if (IS_ERR(right))
389544871b1bSChris Mason 		return PTR_ERR(right);
3896f0486c68SYan, Zheng 
38970b246afaSJeff Mahoney 	root_add_used(root, fs_info->nodesize);
389844871b1bSChris Mason 
38995d4f98a2SYan Zheng 	if (split == 0) {
390044871b1bSChris Mason 		if (mid <= slot) {
390144871b1bSChris Mason 			btrfs_set_header_nritems(right, 0);
390250b5d1fcSFilipe Manana 			ret = insert_ptr(trans, path, &disk_key,
39032ff7e61eSJeff Mahoney 					 right->start, path->slots[1] + 1, 1);
390450b5d1fcSFilipe Manana 			if (ret < 0) {
390550b5d1fcSFilipe Manana 				btrfs_tree_unlock(right);
390650b5d1fcSFilipe Manana 				free_extent_buffer(right);
390750b5d1fcSFilipe Manana 				return ret;
390850b5d1fcSFilipe Manana 			}
390944871b1bSChris Mason 			btrfs_tree_unlock(path->nodes[0]);
391044871b1bSChris Mason 			free_extent_buffer(path->nodes[0]);
391144871b1bSChris Mason 			path->nodes[0] = right;
391244871b1bSChris Mason 			path->slots[0] = 0;
391344871b1bSChris Mason 			path->slots[1] += 1;
391444871b1bSChris Mason 		} else {
391544871b1bSChris Mason 			btrfs_set_header_nritems(right, 0);
391650b5d1fcSFilipe Manana 			ret = insert_ptr(trans, path, &disk_key,
39172ff7e61eSJeff Mahoney 					 right->start, path->slots[1], 1);
391850b5d1fcSFilipe Manana 			if (ret < 0) {
391950b5d1fcSFilipe Manana 				btrfs_tree_unlock(right);
392050b5d1fcSFilipe Manana 				free_extent_buffer(right);
392150b5d1fcSFilipe Manana 				return ret;
392250b5d1fcSFilipe Manana 			}
392344871b1bSChris Mason 			btrfs_tree_unlock(path->nodes[0]);
392444871b1bSChris Mason 			free_extent_buffer(path->nodes[0]);
392544871b1bSChris Mason 			path->nodes[0] = right;
392644871b1bSChris Mason 			path->slots[0] = 0;
3927143bede5SJeff Mahoney 			if (path->slots[1] == 0)
3928b167fa91SNikolay Borisov 				fixup_low_keys(path, &disk_key, 1);
39295d4f98a2SYan Zheng 		}
3930196e0249SLiu Bo 		/*
3931196e0249SLiu Bo 		 * We create a new leaf 'right' for the required ins_len and
3932196e0249SLiu Bo 		 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
3933196e0249SLiu Bo 		 * the content of ins_len to 'right'.
3934196e0249SLiu Bo 		 */
393544871b1bSChris Mason 		return ret;
393644871b1bSChris Mason 	}
393744871b1bSChris Mason 
393850b5d1fcSFilipe Manana 	ret = copy_for_split(trans, path, l, right, slot, mid, nritems);
393950b5d1fcSFilipe Manana 	if (ret < 0) {
394050b5d1fcSFilipe Manana 		btrfs_tree_unlock(right);
394150b5d1fcSFilipe Manana 		free_extent_buffer(right);
394250b5d1fcSFilipe Manana 		return ret;
394350b5d1fcSFilipe Manana 	}
394444871b1bSChris Mason 
39455d4f98a2SYan Zheng 	if (split == 2) {
3946cc0c5538SChris Mason 		BUG_ON(num_doubles != 0);
3947cc0c5538SChris Mason 		num_doubles++;
3948cc0c5538SChris Mason 		goto again;
39493326d1b0SChris Mason 	}
395044871b1bSChris Mason 
3951143bede5SJeff Mahoney 	return 0;
395299d8f83cSChris Mason 
395399d8f83cSChris Mason push_for_double:
395499d8f83cSChris Mason 	push_for_double_split(trans, root, path, data_size);
395599d8f83cSChris Mason 	tried_avoid_double = 1;
3956e902baacSDavid Sterba 	if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
395799d8f83cSChris Mason 		return 0;
395899d8f83cSChris Mason 	goto again;
3959be0e5c09SChris Mason }
3960be0e5c09SChris Mason 
3961ad48fd75SYan, Zheng static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3962ad48fd75SYan, Zheng 					 struct btrfs_root *root,
3963ad48fd75SYan, Zheng 					 struct btrfs_path *path, int ins_len)
3964ad48fd75SYan, Zheng {
3965ad48fd75SYan, Zheng 	struct btrfs_key key;
3966ad48fd75SYan, Zheng 	struct extent_buffer *leaf;
3967ad48fd75SYan, Zheng 	struct btrfs_file_extent_item *fi;
3968ad48fd75SYan, Zheng 	u64 extent_len = 0;
3969ad48fd75SYan, Zheng 	u32 item_size;
3970ad48fd75SYan, Zheng 	int ret;
3971ad48fd75SYan, Zheng 
3972ad48fd75SYan, Zheng 	leaf = path->nodes[0];
3973ad48fd75SYan, Zheng 	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3974ad48fd75SYan, Zheng 
3975ad48fd75SYan, Zheng 	BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3976ad48fd75SYan, Zheng 	       key.type != BTRFS_EXTENT_CSUM_KEY);
3977ad48fd75SYan, Zheng 
3978e902baacSDavid Sterba 	if (btrfs_leaf_free_space(leaf) >= ins_len)
3979ad48fd75SYan, Zheng 		return 0;
3980ad48fd75SYan, Zheng 
39813212fa14SJosef Bacik 	item_size = btrfs_item_size(leaf, path->slots[0]);
3982ad48fd75SYan, Zheng 	if (key.type == BTRFS_EXTENT_DATA_KEY) {
3983ad48fd75SYan, Zheng 		fi = btrfs_item_ptr(leaf, path->slots[0],
3984ad48fd75SYan, Zheng 				    struct btrfs_file_extent_item);
3985ad48fd75SYan, Zheng 		extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3986ad48fd75SYan, Zheng 	}
3987b3b4aa74SDavid Sterba 	btrfs_release_path(path);
3988ad48fd75SYan, Zheng 
3989ad48fd75SYan, Zheng 	path->keep_locks = 1;
3990ad48fd75SYan, Zheng 	path->search_for_split = 1;
3991ad48fd75SYan, Zheng 	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3992ad48fd75SYan, Zheng 	path->search_for_split = 0;
3993a8df6fe6SFilipe Manana 	if (ret > 0)
3994a8df6fe6SFilipe Manana 		ret = -EAGAIN;
3995ad48fd75SYan, Zheng 	if (ret < 0)
3996ad48fd75SYan, Zheng 		goto err;
3997ad48fd75SYan, Zheng 
3998ad48fd75SYan, Zheng 	ret = -EAGAIN;
3999ad48fd75SYan, Zheng 	leaf = path->nodes[0];
4000a8df6fe6SFilipe Manana 	/* if our item isn't there, return now */
40013212fa14SJosef Bacik 	if (item_size != btrfs_item_size(leaf, path->slots[0]))
4002ad48fd75SYan, Zheng 		goto err;
4003ad48fd75SYan, Zheng 
4004109f6aefSChris Mason 	/* the leaf has  changed, it now has room.  return now */
4005e902baacSDavid Sterba 	if (btrfs_leaf_free_space(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 	ret = split_leaf(trans, root, &key, path, ins_len, 1);
4016f0486c68SYan, Zheng 	if (ret)
4017f0486c68SYan, Zheng 		goto err;
4018ad48fd75SYan, Zheng 
4019ad48fd75SYan, Zheng 	path->keep_locks = 0;
4020ad48fd75SYan, Zheng 	btrfs_unlock_up_safe(path, 1);
4021ad48fd75SYan, Zheng 	return 0;
4022ad48fd75SYan, Zheng err:
4023ad48fd75SYan, Zheng 	path->keep_locks = 0;
4024ad48fd75SYan, Zheng 	return ret;
4025ad48fd75SYan, Zheng }
4026ad48fd75SYan, Zheng 
402725263cd7SDavid Sterba static noinline int split_item(struct btrfs_path *path,
4028310712b2SOmar Sandoval 			       const struct btrfs_key *new_key,
4029459931ecSChris Mason 			       unsigned long split_offset)
4030459931ecSChris Mason {
4031459931ecSChris Mason 	struct extent_buffer *leaf;
4032c91666b1SJosef Bacik 	int orig_slot, slot;
4033ad48fd75SYan, Zheng 	char *buf;
4034459931ecSChris Mason 	u32 nritems;
4035ad48fd75SYan, Zheng 	u32 item_size;
4036459931ecSChris Mason 	u32 orig_offset;
4037459931ecSChris Mason 	struct btrfs_disk_key disk_key;
4038459931ecSChris Mason 
4039459931ecSChris Mason 	leaf = path->nodes[0];
40407569141eSFilipe Manana 	/*
40417569141eSFilipe Manana 	 * Shouldn't happen because the caller must have previously called
40427569141eSFilipe Manana 	 * setup_leaf_for_split() to make room for the new item in the leaf.
40437569141eSFilipe Manana 	 */
40447569141eSFilipe Manana 	if (WARN_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item)))
40457569141eSFilipe Manana 		return -ENOSPC;
4046b9473439SChris Mason 
4047c91666b1SJosef Bacik 	orig_slot = path->slots[0];
40483212fa14SJosef Bacik 	orig_offset = btrfs_item_offset(leaf, path->slots[0]);
40493212fa14SJosef Bacik 	item_size = btrfs_item_size(leaf, path->slots[0]);
4050459931ecSChris Mason 
4051459931ecSChris Mason 	buf = kmalloc(item_size, GFP_NOFS);
4052ad48fd75SYan, Zheng 	if (!buf)
4053ad48fd75SYan, Zheng 		return -ENOMEM;
4054ad48fd75SYan, Zheng 
4055459931ecSChris Mason 	read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4056459931ecSChris Mason 			    path->slots[0]), item_size);
4057ad48fd75SYan, Zheng 
4058459931ecSChris Mason 	slot = path->slots[0] + 1;
4059459931ecSChris Mason 	nritems = btrfs_header_nritems(leaf);
4060459931ecSChris Mason 	if (slot != nritems) {
4061459931ecSChris Mason 		/* shift the items */
4062637e3b48SJosef Bacik 		memmove_leaf_items(leaf, slot + 1, slot, nritems - slot);
4063459931ecSChris Mason 	}
4064459931ecSChris Mason 
4065459931ecSChris Mason 	btrfs_cpu_key_to_disk(&disk_key, new_key);
4066459931ecSChris Mason 	btrfs_set_item_key(leaf, &disk_key, slot);
4067459931ecSChris Mason 
40683212fa14SJosef Bacik 	btrfs_set_item_offset(leaf, slot, orig_offset);
40693212fa14SJosef Bacik 	btrfs_set_item_size(leaf, slot, item_size - split_offset);
4070459931ecSChris Mason 
40713212fa14SJosef Bacik 	btrfs_set_item_offset(leaf, orig_slot,
4072459931ecSChris Mason 				 orig_offset + item_size - split_offset);
40733212fa14SJosef Bacik 	btrfs_set_item_size(leaf, orig_slot, split_offset);
4074459931ecSChris Mason 
4075459931ecSChris Mason 	btrfs_set_header_nritems(leaf, nritems + 1);
4076459931ecSChris Mason 
4077459931ecSChris Mason 	/* write the data for the start of the original item */
4078459931ecSChris Mason 	write_extent_buffer(leaf, buf,
4079459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, path->slots[0]),
4080459931ecSChris Mason 			    split_offset);
4081459931ecSChris Mason 
4082459931ecSChris Mason 	/* write the data for the new item */
4083459931ecSChris Mason 	write_extent_buffer(leaf, buf + split_offset,
4084459931ecSChris Mason 			    btrfs_item_ptr_offset(leaf, slot),
4085459931ecSChris Mason 			    item_size - split_offset);
4086459931ecSChris Mason 	btrfs_mark_buffer_dirty(leaf);
4087459931ecSChris Mason 
4088e902baacSDavid Sterba 	BUG_ON(btrfs_leaf_free_space(leaf) < 0);
4089459931ecSChris Mason 	kfree(buf);
4090ad48fd75SYan, Zheng 	return 0;
4091ad48fd75SYan, Zheng }
4092ad48fd75SYan, Zheng 
4093ad48fd75SYan, Zheng /*
4094ad48fd75SYan, Zheng  * This function splits a single item into two items,
4095ad48fd75SYan, Zheng  * giving 'new_key' to the new item and splitting the
4096ad48fd75SYan, Zheng  * old one at split_offset (from the start of the item).
4097ad48fd75SYan, Zheng  *
4098ad48fd75SYan, Zheng  * The path may be released by this operation.  After
4099ad48fd75SYan, Zheng  * the split, the path is pointing to the old item.  The
4100ad48fd75SYan, Zheng  * new item is going to be in the same node as the old one.
4101ad48fd75SYan, Zheng  *
4102ad48fd75SYan, Zheng  * Note, the item being split must be smaller enough to live alone on
4103ad48fd75SYan, Zheng  * a tree block with room for one extra struct btrfs_item
4104ad48fd75SYan, Zheng  *
4105ad48fd75SYan, Zheng  * This allows us to split the item in place, keeping a lock on the
4106ad48fd75SYan, Zheng  * leaf the entire time.
4107ad48fd75SYan, Zheng  */
4108ad48fd75SYan, Zheng int btrfs_split_item(struct btrfs_trans_handle *trans,
4109ad48fd75SYan, Zheng 		     struct btrfs_root *root,
4110ad48fd75SYan, Zheng 		     struct btrfs_path *path,
4111310712b2SOmar Sandoval 		     const struct btrfs_key *new_key,
4112ad48fd75SYan, Zheng 		     unsigned long split_offset)
4113ad48fd75SYan, Zheng {
4114ad48fd75SYan, Zheng 	int ret;
4115ad48fd75SYan, Zheng 	ret = setup_leaf_for_split(trans, root, path,
4116ad48fd75SYan, Zheng 				   sizeof(struct btrfs_item));
4117ad48fd75SYan, Zheng 	if (ret)
4118459931ecSChris Mason 		return ret;
4119ad48fd75SYan, Zheng 
412025263cd7SDavid Sterba 	ret = split_item(path, new_key, split_offset);
4121ad48fd75SYan, Zheng 	return ret;
4122ad48fd75SYan, Zheng }
4123ad48fd75SYan, Zheng 
4124ad48fd75SYan, Zheng /*
4125d352ac68SChris Mason  * make the item pointed to by the path smaller.  new_size indicates
4126d352ac68SChris Mason  * how small to make it, and from_end tells us if we just chop bytes
4127d352ac68SChris Mason  * off the end of the item or if we shift the item to chop bytes off
4128d352ac68SChris Mason  * the front.
4129d352ac68SChris Mason  */
413078ac4f9eSDavid Sterba void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end)
4131b18c6685SChris Mason {
4132b18c6685SChris Mason 	int slot;
41335f39d397SChris Mason 	struct extent_buffer *leaf;
4134b18c6685SChris Mason 	u32 nritems;
4135b18c6685SChris Mason 	unsigned int data_end;
4136b18c6685SChris Mason 	unsigned int old_data_start;
4137b18c6685SChris Mason 	unsigned int old_size;
4138b18c6685SChris Mason 	unsigned int size_diff;
4139b18c6685SChris Mason 	int i;
4140cfed81a0SChris Mason 	struct btrfs_map_token token;
4141cfed81a0SChris Mason 
41425f39d397SChris Mason 	leaf = path->nodes[0];
4143179e29e4SChris Mason 	slot = path->slots[0];
4144179e29e4SChris Mason 
41453212fa14SJosef Bacik 	old_size = btrfs_item_size(leaf, slot);
4146179e29e4SChris Mason 	if (old_size == new_size)
4147143bede5SJeff Mahoney 		return;
4148b18c6685SChris Mason 
41495f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
41508f881e8cSDavid Sterba 	data_end = leaf_data_end(leaf);
4151b18c6685SChris Mason 
41523212fa14SJosef Bacik 	old_data_start = btrfs_item_offset(leaf, slot);
4153179e29e4SChris Mason 
4154b18c6685SChris Mason 	size_diff = old_size - new_size;
4155b18c6685SChris Mason 
4156b18c6685SChris Mason 	BUG_ON(slot < 0);
4157b18c6685SChris Mason 	BUG_ON(slot >= nritems);
4158b18c6685SChris Mason 
4159b18c6685SChris Mason 	/*
4160b18c6685SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4161b18c6685SChris Mason 	 */
4162b18c6685SChris Mason 	/* first correct the data pointers */
4163c82f823cSDavid Sterba 	btrfs_init_map_token(&token, leaf);
4164b18c6685SChris Mason 	for (i = slot; i < nritems; i++) {
41655f39d397SChris Mason 		u32 ioff;
4166db94535dSChris Mason 
41673212fa14SJosef Bacik 		ioff = btrfs_token_item_offset(&token, i);
41683212fa14SJosef Bacik 		btrfs_set_token_item_offset(&token, i, ioff + size_diff);
4169b18c6685SChris Mason 	}
4170db94535dSChris Mason 
4171b18c6685SChris Mason 	/* shift the data */
4172179e29e4SChris Mason 	if (from_end) {
4173637e3b48SJosef Bacik 		memmove_leaf_data(leaf, data_end + size_diff, data_end,
4174637e3b48SJosef Bacik 				  old_data_start + new_size - data_end);
4175179e29e4SChris Mason 	} else {
4176179e29e4SChris Mason 		struct btrfs_disk_key disk_key;
4177179e29e4SChris Mason 		u64 offset;
4178179e29e4SChris Mason 
4179179e29e4SChris Mason 		btrfs_item_key(leaf, &disk_key, slot);
4180179e29e4SChris Mason 
4181179e29e4SChris Mason 		if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4182179e29e4SChris Mason 			unsigned long ptr;
4183179e29e4SChris Mason 			struct btrfs_file_extent_item *fi;
4184179e29e4SChris Mason 
4185179e29e4SChris Mason 			fi = btrfs_item_ptr(leaf, slot,
4186179e29e4SChris Mason 					    struct btrfs_file_extent_item);
4187179e29e4SChris Mason 			fi = (struct btrfs_file_extent_item *)(
4188179e29e4SChris Mason 			     (unsigned long)fi - size_diff);
4189179e29e4SChris Mason 
4190179e29e4SChris Mason 			if (btrfs_file_extent_type(leaf, fi) ==
4191179e29e4SChris Mason 			    BTRFS_FILE_EXTENT_INLINE) {
4192179e29e4SChris Mason 				ptr = btrfs_item_ptr_offset(leaf, slot);
4193179e29e4SChris Mason 				memmove_extent_buffer(leaf, ptr,
4194179e29e4SChris Mason 				      (unsigned long)fi,
41957ec20afbSDavid Sterba 				      BTRFS_FILE_EXTENT_INLINE_DATA_START);
4196179e29e4SChris Mason 			}
4197179e29e4SChris Mason 		}
4198179e29e4SChris Mason 
4199637e3b48SJosef Bacik 		memmove_leaf_data(leaf, data_end + size_diff, data_end,
4200637e3b48SJosef Bacik 				  old_data_start - data_end);
4201179e29e4SChris Mason 
4202179e29e4SChris Mason 		offset = btrfs_disk_key_offset(&disk_key);
4203179e29e4SChris Mason 		btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4204179e29e4SChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot);
4205179e29e4SChris Mason 		if (slot == 0)
4206b167fa91SNikolay Borisov 			fixup_low_keys(path, &disk_key, 1);
4207179e29e4SChris Mason 	}
42085f39d397SChris Mason 
42093212fa14SJosef Bacik 	btrfs_set_item_size(leaf, slot, new_size);
42105f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
4211b18c6685SChris Mason 
4212e902baacSDavid Sterba 	if (btrfs_leaf_free_space(leaf) < 0) {
4213a4f78750SDavid Sterba 		btrfs_print_leaf(leaf);
4214b18c6685SChris Mason 		BUG();
42155f39d397SChris Mason 	}
4216b18c6685SChris Mason }
4217b18c6685SChris Mason 
4218d352ac68SChris Mason /*
42198f69dbd2SStefan Behrens  * make the item pointed to by the path bigger, data_size is the added size.
4220d352ac68SChris Mason  */
4221c71dd880SDavid Sterba void btrfs_extend_item(struct btrfs_path *path, u32 data_size)
42226567e837SChris Mason {
42236567e837SChris Mason 	int slot;
42245f39d397SChris Mason 	struct extent_buffer *leaf;
42256567e837SChris Mason 	u32 nritems;
42266567e837SChris Mason 	unsigned int data_end;
42276567e837SChris Mason 	unsigned int old_data;
42286567e837SChris Mason 	unsigned int old_size;
42296567e837SChris Mason 	int i;
4230cfed81a0SChris Mason 	struct btrfs_map_token token;
4231cfed81a0SChris Mason 
42325f39d397SChris Mason 	leaf = path->nodes[0];
42336567e837SChris Mason 
42345f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
42358f881e8cSDavid Sterba 	data_end = leaf_data_end(leaf);
42366567e837SChris Mason 
4237e902baacSDavid Sterba 	if (btrfs_leaf_free_space(leaf) < data_size) {
4238a4f78750SDavid Sterba 		btrfs_print_leaf(leaf);
42396567e837SChris Mason 		BUG();
42405f39d397SChris Mason 	}
42416567e837SChris Mason 	slot = path->slots[0];
4242dc2e724eSJosef Bacik 	old_data = btrfs_item_data_end(leaf, slot);
42436567e837SChris Mason 
42446567e837SChris Mason 	BUG_ON(slot < 0);
42453326d1b0SChris Mason 	if (slot >= nritems) {
4246a4f78750SDavid Sterba 		btrfs_print_leaf(leaf);
4247c71dd880SDavid Sterba 		btrfs_crit(leaf->fs_info, "slot %d too large, nritems %d",
4248d397712bSChris Mason 			   slot, nritems);
4249290342f6SArnd Bergmann 		BUG();
42503326d1b0SChris Mason 	}
42516567e837SChris Mason 
42526567e837SChris Mason 	/*
42536567e837SChris Mason 	 * item0..itemN ... dataN.offset..dataN.size .. data0.size
42546567e837SChris Mason 	 */
42556567e837SChris Mason 	/* first correct the data pointers */
4256c82f823cSDavid Sterba 	btrfs_init_map_token(&token, leaf);
42576567e837SChris Mason 	for (i = slot; i < nritems; i++) {
42585f39d397SChris Mason 		u32 ioff;
4259db94535dSChris Mason 
42603212fa14SJosef Bacik 		ioff = btrfs_token_item_offset(&token, i);
42613212fa14SJosef Bacik 		btrfs_set_token_item_offset(&token, i, ioff - data_size);
42626567e837SChris Mason 	}
42635f39d397SChris Mason 
42646567e837SChris Mason 	/* shift the data */
4265637e3b48SJosef Bacik 	memmove_leaf_data(leaf, data_end - data_size, data_end,
4266637e3b48SJosef Bacik 			  old_data - data_end);
42675f39d397SChris Mason 
42686567e837SChris Mason 	data_end = old_data;
42693212fa14SJosef Bacik 	old_size = btrfs_item_size(leaf, slot);
42703212fa14SJosef Bacik 	btrfs_set_item_size(leaf, slot, old_size + data_size);
42715f39d397SChris Mason 	btrfs_mark_buffer_dirty(leaf);
42726567e837SChris Mason 
4273e902baacSDavid Sterba 	if (btrfs_leaf_free_space(leaf) < 0) {
4274a4f78750SDavid Sterba 		btrfs_print_leaf(leaf);
42756567e837SChris Mason 		BUG();
42765f39d397SChris Mason 	}
42776567e837SChris Mason }
42786567e837SChris Mason 
427943dd529aSDavid Sterba /*
428043dd529aSDavid Sterba  * Make space in the node before inserting one or more items.
4281da9ffb24SNikolay Borisov  *
4282da9ffb24SNikolay Borisov  * @root:	root we are inserting items to
4283da9ffb24SNikolay Borisov  * @path:	points to the leaf/slot where we are going to insert new items
4284b7ef5f3aSFilipe Manana  * @batch:      information about the batch of items to insert
428543dd529aSDavid Sterba  *
428643dd529aSDavid Sterba  * Main purpose is to save stack depth by doing the bulk of the work in a
428743dd529aSDavid Sterba  * function that doesn't call btrfs_search_slot
428874123bd7SChris Mason  */
4289f0641656SFilipe Manana static void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
4290b7ef5f3aSFilipe Manana 				   const struct btrfs_item_batch *batch)
4291be0e5c09SChris Mason {
42920b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
42939c58309dSChris Mason 	int i;
42947518a238SChris Mason 	u32 nritems;
4295be0e5c09SChris Mason 	unsigned int data_end;
4296e2fa7227SChris Mason 	struct btrfs_disk_key disk_key;
429744871b1bSChris Mason 	struct extent_buffer *leaf;
429844871b1bSChris Mason 	int slot;
4299cfed81a0SChris Mason 	struct btrfs_map_token token;
4300fc0d82e1SNikolay Borisov 	u32 total_size;
4301fc0d82e1SNikolay Borisov 
4302b7ef5f3aSFilipe Manana 	/*
4303b7ef5f3aSFilipe Manana 	 * Before anything else, update keys in the parent and other ancestors
4304b7ef5f3aSFilipe Manana 	 * if needed, then release the write locks on them, so that other tasks
4305b7ef5f3aSFilipe Manana 	 * can use them while we modify the leaf.
4306b7ef5f3aSFilipe Manana 	 */
430724cdc847SFilipe Manana 	if (path->slots[0] == 0) {
4308b7ef5f3aSFilipe Manana 		btrfs_cpu_key_to_disk(&disk_key, &batch->keys[0]);
4309b167fa91SNikolay Borisov 		fixup_low_keys(path, &disk_key, 1);
431024cdc847SFilipe Manana 	}
431124cdc847SFilipe Manana 	btrfs_unlock_up_safe(path, 1);
431224cdc847SFilipe Manana 
43135f39d397SChris Mason 	leaf = path->nodes[0];
431444871b1bSChris Mason 	slot = path->slots[0];
431574123bd7SChris Mason 
43165f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
43178f881e8cSDavid Sterba 	data_end = leaf_data_end(leaf);
4318b7ef5f3aSFilipe Manana 	total_size = batch->total_data_size + (batch->nr * sizeof(struct btrfs_item));
4319eb60ceacSChris Mason 
4320e902baacSDavid Sterba 	if (btrfs_leaf_free_space(leaf) < total_size) {
4321a4f78750SDavid Sterba 		btrfs_print_leaf(leaf);
43220b246afaSJeff Mahoney 		btrfs_crit(fs_info, "not enough freespace need %u have %d",
4323e902baacSDavid Sterba 			   total_size, btrfs_leaf_free_space(leaf));
4324be0e5c09SChris Mason 		BUG();
4325d4dbff95SChris Mason 	}
43265f39d397SChris Mason 
4327c82f823cSDavid Sterba 	btrfs_init_map_token(&token, leaf);
4328be0e5c09SChris Mason 	if (slot != nritems) {
4329dc2e724eSJosef Bacik 		unsigned int old_data = btrfs_item_data_end(leaf, slot);
4330be0e5c09SChris Mason 
43315f39d397SChris Mason 		if (old_data < data_end) {
4332a4f78750SDavid Sterba 			btrfs_print_leaf(leaf);
43337269ddd2SNikolay Borisov 			btrfs_crit(fs_info,
43347269ddd2SNikolay Borisov 		"item at slot %d with data offset %u beyond data end of leaf %u",
43355f39d397SChris Mason 				   slot, old_data, data_end);
4336290342f6SArnd Bergmann 			BUG();
43375f39d397SChris Mason 		}
4338be0e5c09SChris Mason 		/*
4339be0e5c09SChris Mason 		 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4340be0e5c09SChris Mason 		 */
4341be0e5c09SChris Mason 		/* first correct the data pointers */
43420783fcfcSChris Mason 		for (i = slot; i < nritems; i++) {
43435f39d397SChris Mason 			u32 ioff;
4344db94535dSChris Mason 
43453212fa14SJosef Bacik 			ioff = btrfs_token_item_offset(&token, i);
43463212fa14SJosef Bacik 			btrfs_set_token_item_offset(&token, i,
4347b7ef5f3aSFilipe Manana 						       ioff - batch->total_data_size);
43480783fcfcSChris Mason 		}
4349be0e5c09SChris Mason 		/* shift the items */
4350637e3b48SJosef Bacik 		memmove_leaf_items(leaf, slot + batch->nr, slot, nritems - slot);
4351be0e5c09SChris Mason 
4352be0e5c09SChris Mason 		/* shift the data */
4353637e3b48SJosef Bacik 		memmove_leaf_data(leaf, data_end - batch->total_data_size,
4354637e3b48SJosef Bacik 				  data_end, old_data - data_end);
4355be0e5c09SChris Mason 		data_end = old_data;
4356be0e5c09SChris Mason 	}
43575f39d397SChris Mason 
435862e2749eSChris Mason 	/* setup the item for the new data */
4359b7ef5f3aSFilipe Manana 	for (i = 0; i < batch->nr; i++) {
4360b7ef5f3aSFilipe Manana 		btrfs_cpu_key_to_disk(&disk_key, &batch->keys[i]);
43619c58309dSChris Mason 		btrfs_set_item_key(leaf, &disk_key, slot + i);
4362b7ef5f3aSFilipe Manana 		data_end -= batch->data_sizes[i];
43633212fa14SJosef Bacik 		btrfs_set_token_item_offset(&token, slot + i, data_end);
43643212fa14SJosef Bacik 		btrfs_set_token_item_size(&token, slot + i, batch->data_sizes[i]);
43659c58309dSChris Mason 	}
436644871b1bSChris Mason 
4367b7ef5f3aSFilipe Manana 	btrfs_set_header_nritems(leaf, nritems + batch->nr);
4368b9473439SChris Mason 	btrfs_mark_buffer_dirty(leaf);
4369aa5d6bedSChris Mason 
4370e902baacSDavid Sterba 	if (btrfs_leaf_free_space(leaf) < 0) {
4371a4f78750SDavid Sterba 		btrfs_print_leaf(leaf);
4372be0e5c09SChris Mason 		BUG();
43735f39d397SChris Mason 	}
437444871b1bSChris Mason }
437544871b1bSChris Mason 
437644871b1bSChris Mason /*
4377f0641656SFilipe Manana  * Insert a new item into a leaf.
4378f0641656SFilipe Manana  *
4379f0641656SFilipe Manana  * @root:      The root of the btree.
4380f0641656SFilipe Manana  * @path:      A path pointing to the target leaf and slot.
4381f0641656SFilipe Manana  * @key:       The key of the new item.
4382f0641656SFilipe Manana  * @data_size: The size of the data associated with the new key.
4383f0641656SFilipe Manana  */
4384f0641656SFilipe Manana void btrfs_setup_item_for_insert(struct btrfs_root *root,
4385f0641656SFilipe Manana 				 struct btrfs_path *path,
4386f0641656SFilipe Manana 				 const struct btrfs_key *key,
4387f0641656SFilipe Manana 				 u32 data_size)
4388f0641656SFilipe Manana {
4389f0641656SFilipe Manana 	struct btrfs_item_batch batch;
4390f0641656SFilipe Manana 
4391f0641656SFilipe Manana 	batch.keys = key;
4392f0641656SFilipe Manana 	batch.data_sizes = &data_size;
4393f0641656SFilipe Manana 	batch.total_data_size = data_size;
4394f0641656SFilipe Manana 	batch.nr = 1;
4395f0641656SFilipe Manana 
4396f0641656SFilipe Manana 	setup_items_for_insert(root, path, &batch);
4397f0641656SFilipe Manana }
4398f0641656SFilipe Manana 
4399f0641656SFilipe Manana /*
440044871b1bSChris Mason  * Given a key and some data, insert items into the tree.
440144871b1bSChris Mason  * This does all the path init required, making room in the tree if needed.
440244871b1bSChris Mason  */
440344871b1bSChris Mason int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
440444871b1bSChris Mason 			    struct btrfs_root *root,
440544871b1bSChris Mason 			    struct btrfs_path *path,
4406b7ef5f3aSFilipe Manana 			    const struct btrfs_item_batch *batch)
440744871b1bSChris Mason {
440844871b1bSChris Mason 	int ret = 0;
440944871b1bSChris Mason 	int slot;
4410b7ef5f3aSFilipe Manana 	u32 total_size;
441144871b1bSChris Mason 
4412b7ef5f3aSFilipe Manana 	total_size = batch->total_data_size + (batch->nr * sizeof(struct btrfs_item));
4413b7ef5f3aSFilipe Manana 	ret = btrfs_search_slot(trans, root, &batch->keys[0], path, total_size, 1);
441444871b1bSChris Mason 	if (ret == 0)
441544871b1bSChris Mason 		return -EEXIST;
441644871b1bSChris Mason 	if (ret < 0)
4417143bede5SJeff Mahoney 		return ret;
441844871b1bSChris Mason 
441944871b1bSChris Mason 	slot = path->slots[0];
442044871b1bSChris Mason 	BUG_ON(slot < 0);
442144871b1bSChris Mason 
4422b7ef5f3aSFilipe Manana 	setup_items_for_insert(root, path, batch);
4423143bede5SJeff Mahoney 	return 0;
442462e2749eSChris Mason }
442562e2749eSChris Mason 
442662e2749eSChris Mason /*
442762e2749eSChris Mason  * Given a key and some data, insert an item into the tree.
442862e2749eSChris Mason  * This does all the path init required, making room in the tree if needed.
442962e2749eSChris Mason  */
4430310712b2SOmar Sandoval int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4431310712b2SOmar Sandoval 		      const struct btrfs_key *cpu_key, void *data,
4432310712b2SOmar Sandoval 		      u32 data_size)
443362e2749eSChris Mason {
443462e2749eSChris Mason 	int ret = 0;
44352c90e5d6SChris Mason 	struct btrfs_path *path;
44365f39d397SChris Mason 	struct extent_buffer *leaf;
44375f39d397SChris Mason 	unsigned long ptr;
443862e2749eSChris Mason 
44392c90e5d6SChris Mason 	path = btrfs_alloc_path();
4440db5b493aSTsutomu Itoh 	if (!path)
4441db5b493aSTsutomu Itoh 		return -ENOMEM;
44422c90e5d6SChris Mason 	ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
444362e2749eSChris Mason 	if (!ret) {
44445f39d397SChris Mason 		leaf = path->nodes[0];
44455f39d397SChris Mason 		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
44465f39d397SChris Mason 		write_extent_buffer(leaf, data, ptr, data_size);
44475f39d397SChris Mason 		btrfs_mark_buffer_dirty(leaf);
444862e2749eSChris Mason 	}
44492c90e5d6SChris Mason 	btrfs_free_path(path);
4450aa5d6bedSChris Mason 	return ret;
4451be0e5c09SChris Mason }
4452be0e5c09SChris Mason 
445374123bd7SChris Mason /*
4454f0641656SFilipe Manana  * This function duplicates an item, giving 'new_key' to the new item.
4455f0641656SFilipe Manana  * It guarantees both items live in the same tree leaf and the new item is
4456f0641656SFilipe Manana  * contiguous with the original item.
4457f0641656SFilipe Manana  *
4458f0641656SFilipe Manana  * This allows us to split a file extent in place, keeping a lock on the leaf
4459f0641656SFilipe Manana  * the entire time.
4460f0641656SFilipe Manana  */
4461f0641656SFilipe Manana int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4462f0641656SFilipe Manana 			 struct btrfs_root *root,
4463f0641656SFilipe Manana 			 struct btrfs_path *path,
4464f0641656SFilipe Manana 			 const struct btrfs_key *new_key)
4465f0641656SFilipe Manana {
4466f0641656SFilipe Manana 	struct extent_buffer *leaf;
4467f0641656SFilipe Manana 	int ret;
4468f0641656SFilipe Manana 	u32 item_size;
4469f0641656SFilipe Manana 
4470f0641656SFilipe Manana 	leaf = path->nodes[0];
44713212fa14SJosef Bacik 	item_size = btrfs_item_size(leaf, path->slots[0]);
4472f0641656SFilipe Manana 	ret = setup_leaf_for_split(trans, root, path,
4473f0641656SFilipe Manana 				   item_size + sizeof(struct btrfs_item));
4474f0641656SFilipe Manana 	if (ret)
4475f0641656SFilipe Manana 		return ret;
4476f0641656SFilipe Manana 
4477f0641656SFilipe Manana 	path->slots[0]++;
4478f0641656SFilipe Manana 	btrfs_setup_item_for_insert(root, path, new_key, item_size);
4479f0641656SFilipe Manana 	leaf = path->nodes[0];
4480f0641656SFilipe Manana 	memcpy_extent_buffer(leaf,
4481f0641656SFilipe Manana 			     btrfs_item_ptr_offset(leaf, path->slots[0]),
4482f0641656SFilipe Manana 			     btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4483f0641656SFilipe Manana 			     item_size);
4484f0641656SFilipe Manana 	return 0;
4485f0641656SFilipe Manana }
4486f0641656SFilipe Manana 
4487f0641656SFilipe Manana /*
44885de08d7dSChris Mason  * delete the pointer from a given node.
448974123bd7SChris Mason  *
4490d352ac68SChris Mason  * the tree should have been previously balanced so the deletion does not
4491d352ac68SChris Mason  * empty a node.
4492016f9d0bSJosef Bacik  *
4493016f9d0bSJosef Bacik  * This is exported for use inside btrfs-progs, don't un-export it.
449474123bd7SChris Mason  */
4495751a2761SFilipe Manana int btrfs_del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4496751a2761SFilipe Manana 		  struct btrfs_path *path, int level, int slot)
4497be0e5c09SChris Mason {
44985f39d397SChris Mason 	struct extent_buffer *parent = path->nodes[level];
44997518a238SChris Mason 	u32 nritems;
4500f3ea38daSJan Schmidt 	int ret;
4501be0e5c09SChris Mason 
45025f39d397SChris Mason 	nritems = btrfs_header_nritems(parent);
4503be0e5c09SChris Mason 	if (slot != nritems - 1) {
4504bf1d3425SDavid Sterba 		if (level) {
4505f3a84ccdSFilipe Manana 			ret = btrfs_tree_mod_log_insert_move(parent, slot,
4506f3a84ccdSFilipe Manana 					slot + 1, nritems - slot - 1);
4507751a2761SFilipe Manana 			if (ret < 0) {
4508751a2761SFilipe Manana 				btrfs_abort_transaction(trans, ret);
4509751a2761SFilipe Manana 				return ret;
4510751a2761SFilipe Manana 			}
4511bf1d3425SDavid Sterba 		}
45125f39d397SChris Mason 		memmove_extent_buffer(parent,
4513e23efd8eSJosef Bacik 			      btrfs_node_key_ptr_offset(parent, slot),
4514e23efd8eSJosef Bacik 			      btrfs_node_key_ptr_offset(parent, slot + 1),
4515d6025579SChris Mason 			      sizeof(struct btrfs_key_ptr) *
4516d6025579SChris Mason 			      (nritems - slot - 1));
451757ba86c0SChris Mason 	} else if (level) {
4518f3a84ccdSFilipe Manana 		ret = btrfs_tree_mod_log_insert_key(parent, slot,
451933cff222SFilipe Manana 						    BTRFS_MOD_LOG_KEY_REMOVE);
4520751a2761SFilipe Manana 		if (ret < 0) {
4521751a2761SFilipe Manana 			btrfs_abort_transaction(trans, ret);
4522751a2761SFilipe Manana 			return ret;
4523751a2761SFilipe Manana 		}
4524be0e5c09SChris Mason 	}
4525f3ea38daSJan Schmidt 
45267518a238SChris Mason 	nritems--;
45275f39d397SChris Mason 	btrfs_set_header_nritems(parent, nritems);
45287518a238SChris Mason 	if (nritems == 0 && parent == root->node) {
45295f39d397SChris Mason 		BUG_ON(btrfs_header_level(root->node) != 1);
4530eb60ceacSChris Mason 		/* just turn the root into a leaf and break */
45315f39d397SChris Mason 		btrfs_set_header_level(root->node, 0);
4532bb803951SChris Mason 	} else if (slot == 0) {
45335f39d397SChris Mason 		struct btrfs_disk_key disk_key;
45345f39d397SChris Mason 
45355f39d397SChris Mason 		btrfs_node_key(parent, &disk_key, 0);
4536b167fa91SNikolay Borisov 		fixup_low_keys(path, &disk_key, level + 1);
4537be0e5c09SChris Mason 	}
4538d6025579SChris Mason 	btrfs_mark_buffer_dirty(parent);
4539751a2761SFilipe Manana 	return 0;
4540be0e5c09SChris Mason }
4541be0e5c09SChris Mason 
454274123bd7SChris Mason /*
4543323ac95bSChris Mason  * a helper function to delete the leaf pointed to by path->slots[1] and
45445d4f98a2SYan Zheng  * path->nodes[1].
4545323ac95bSChris Mason  *
4546323ac95bSChris Mason  * This deletes the pointer in path->nodes[1] and frees the leaf
4547323ac95bSChris Mason  * block extent.  zero is returned if it all worked out, < 0 otherwise.
4548323ac95bSChris Mason  *
4549323ac95bSChris Mason  * The path must have already been setup for deleting the leaf, including
4550323ac95bSChris Mason  * all the proper balancing.  path->nodes[1] must be locked.
4551323ac95bSChris Mason  */
4552751a2761SFilipe Manana static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
4553323ac95bSChris Mason 				   struct btrfs_root *root,
45545d4f98a2SYan Zheng 				   struct btrfs_path *path,
45555d4f98a2SYan Zheng 				   struct extent_buffer *leaf)
4556323ac95bSChris Mason {
4557751a2761SFilipe Manana 	int ret;
4558751a2761SFilipe Manana 
45595d4f98a2SYan Zheng 	WARN_ON(btrfs_header_generation(leaf) != trans->transid);
4560751a2761SFilipe Manana 	ret = btrfs_del_ptr(trans, root, path, 1, path->slots[1]);
4561751a2761SFilipe Manana 	if (ret < 0)
4562751a2761SFilipe Manana 		return ret;
4563323ac95bSChris Mason 
45644d081c41SChris Mason 	/*
45654d081c41SChris Mason 	 * btrfs_free_extent is expensive, we want to make sure we
45664d081c41SChris Mason 	 * aren't holding any locks when we call it
45674d081c41SChris Mason 	 */
45684d081c41SChris Mason 	btrfs_unlock_up_safe(path, 0);
45694d081c41SChris Mason 
4570f0486c68SYan, Zheng 	root_sub_used(root, leaf->len);
4571f0486c68SYan, Zheng 
457267439dadSDavid Sterba 	atomic_inc(&leaf->refs);
45737a163608SFilipe Manana 	btrfs_free_tree_block(trans, btrfs_root_id(root), leaf, 0, 1);
45743083ee2eSJosef Bacik 	free_extent_buffer_stale(leaf);
4575751a2761SFilipe Manana 	return 0;
4576323ac95bSChris Mason }
4577323ac95bSChris Mason /*
457874123bd7SChris Mason  * delete the item at the leaf level in path.  If that empties
457974123bd7SChris Mason  * the leaf, remove it from the tree
458074123bd7SChris Mason  */
458185e21bacSChris Mason int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
458285e21bacSChris Mason 		    struct btrfs_path *path, int slot, int nr)
4583be0e5c09SChris Mason {
45840b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
45855f39d397SChris Mason 	struct extent_buffer *leaf;
4586aa5d6bedSChris Mason 	int ret = 0;
4587aa5d6bedSChris Mason 	int wret;
45887518a238SChris Mason 	u32 nritems;
4589be0e5c09SChris Mason 
45905f39d397SChris Mason 	leaf = path->nodes[0];
45915f39d397SChris Mason 	nritems = btrfs_header_nritems(leaf);
4592be0e5c09SChris Mason 
459385e21bacSChris Mason 	if (slot + nr != nritems) {
45940cae23b6SFilipe Manana 		const u32 last_off = btrfs_item_offset(leaf, slot + nr - 1);
45950cae23b6SFilipe Manana 		const int data_end = leaf_data_end(leaf);
4596c82f823cSDavid Sterba 		struct btrfs_map_token token;
45970cae23b6SFilipe Manana 		u32 dsize = 0;
45980cae23b6SFilipe Manana 		int i;
45990cae23b6SFilipe Manana 
46000cae23b6SFilipe Manana 		for (i = 0; i < nr; i++)
46010cae23b6SFilipe Manana 			dsize += btrfs_item_size(leaf, slot + i);
46025f39d397SChris Mason 
4603637e3b48SJosef Bacik 		memmove_leaf_data(leaf, data_end + dsize, data_end,
460485e21bacSChris Mason 				  last_off - data_end);
46055f39d397SChris Mason 
4606c82f823cSDavid Sterba 		btrfs_init_map_token(&token, leaf);
460785e21bacSChris Mason 		for (i = slot + nr; i < nritems; i++) {
46085f39d397SChris Mason 			u32 ioff;
4609db94535dSChris Mason 
46103212fa14SJosef Bacik 			ioff = btrfs_token_item_offset(&token, i);
46113212fa14SJosef Bacik 			btrfs_set_token_item_offset(&token, i, ioff + dsize);
46120783fcfcSChris Mason 		}
4613db94535dSChris Mason 
4614637e3b48SJosef Bacik 		memmove_leaf_items(leaf, slot, slot + nr, nritems - slot - nr);
4615be0e5c09SChris Mason 	}
461685e21bacSChris Mason 	btrfs_set_header_nritems(leaf, nritems - nr);
461785e21bacSChris Mason 	nritems -= nr;
46185f39d397SChris Mason 
461974123bd7SChris Mason 	/* delete the leaf if we've emptied it */
46207518a238SChris Mason 	if (nritems == 0) {
46215f39d397SChris Mason 		if (leaf == root->node) {
46225f39d397SChris Mason 			btrfs_set_header_level(leaf, 0);
46239a8dd150SChris Mason 		} else {
4624190a8339SJosef Bacik 			btrfs_clear_buffer_dirty(trans, leaf);
4625751a2761SFilipe Manana 			ret = btrfs_del_leaf(trans, root, path, leaf);
4626751a2761SFilipe Manana 			if (ret < 0)
4627751a2761SFilipe Manana 				return ret;
46289a8dd150SChris Mason 		}
4629be0e5c09SChris Mason 	} else {
46307518a238SChris Mason 		int used = leaf_space_used(leaf, 0, nritems);
4631aa5d6bedSChris Mason 		if (slot == 0) {
46325f39d397SChris Mason 			struct btrfs_disk_key disk_key;
46335f39d397SChris Mason 
46345f39d397SChris Mason 			btrfs_item_key(leaf, &disk_key, 0);
4635b167fa91SNikolay Borisov 			fixup_low_keys(path, &disk_key, 1);
4636aa5d6bedSChris Mason 		}
4637aa5d6bedSChris Mason 
46387c4063d1SFilipe Manana 		/*
46397c4063d1SFilipe Manana 		 * Try to delete the leaf if it is mostly empty. We do this by
46407c4063d1SFilipe Manana 		 * trying to move all its items into its left and right neighbours.
46417c4063d1SFilipe Manana 		 * If we can't move all the items, then we don't delete it - it's
46427c4063d1SFilipe Manana 		 * not ideal, but future insertions might fill the leaf with more
46437c4063d1SFilipe Manana 		 * items, or items from other leaves might be moved later into our
46447c4063d1SFilipe Manana 		 * leaf due to deletions on those leaves.
46457c4063d1SFilipe Manana 		 */
46460b246afaSJeff Mahoney 		if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
46477c4063d1SFilipe Manana 			u32 min_push_space;
46487c4063d1SFilipe Manana 
4649be0e5c09SChris Mason 			/* push_leaf_left fixes the path.
4650be0e5c09SChris Mason 			 * make sure the path still points to our leaf
4651016f9d0bSJosef Bacik 			 * for possible call to btrfs_del_ptr below
4652be0e5c09SChris Mason 			 */
46534920c9acSChris Mason 			slot = path->slots[1];
465467439dadSDavid Sterba 			atomic_inc(&leaf->refs);
46557c4063d1SFilipe Manana 			/*
46567c4063d1SFilipe Manana 			 * We want to be able to at least push one item to the
46577c4063d1SFilipe Manana 			 * left neighbour leaf, and that's the first item.
46587c4063d1SFilipe Manana 			 */
46597c4063d1SFilipe Manana 			min_push_space = sizeof(struct btrfs_item) +
46607c4063d1SFilipe Manana 				btrfs_item_size(leaf, 0);
46617c4063d1SFilipe Manana 			wret = push_leaf_left(trans, root, path, 0,
46627c4063d1SFilipe Manana 					      min_push_space, 1, (u32)-1);
466354aa1f4dSChris Mason 			if (wret < 0 && wret != -ENOSPC)
4664aa5d6bedSChris Mason 				ret = wret;
46655f39d397SChris Mason 
46665f39d397SChris Mason 			if (path->nodes[0] == leaf &&
46675f39d397SChris Mason 			    btrfs_header_nritems(leaf)) {
46687c4063d1SFilipe Manana 				/*
46697c4063d1SFilipe Manana 				 * If we were not able to push all items from our
46707c4063d1SFilipe Manana 				 * leaf to its left neighbour, then attempt to
46717c4063d1SFilipe Manana 				 * either push all the remaining items to the
46727c4063d1SFilipe Manana 				 * right neighbour or none. There's no advantage
46737c4063d1SFilipe Manana 				 * in pushing only some items, instead of all, as
46747c4063d1SFilipe Manana 				 * it's pointless to end up with a leaf having
46757c4063d1SFilipe Manana 				 * too few items while the neighbours can be full
46767c4063d1SFilipe Manana 				 * or nearly full.
46777c4063d1SFilipe Manana 				 */
46787c4063d1SFilipe Manana 				nritems = btrfs_header_nritems(leaf);
46797c4063d1SFilipe Manana 				min_push_space = leaf_space_used(leaf, 0, nritems);
46807c4063d1SFilipe Manana 				wret = push_leaf_right(trans, root, path, 0,
46817c4063d1SFilipe Manana 						       min_push_space, 1, 0);
468254aa1f4dSChris Mason 				if (wret < 0 && wret != -ENOSPC)
4683aa5d6bedSChris Mason 					ret = wret;
4684aa5d6bedSChris Mason 			}
46855f39d397SChris Mason 
46865f39d397SChris Mason 			if (btrfs_header_nritems(leaf) == 0) {
4687323ac95bSChris Mason 				path->slots[1] = slot;
4688751a2761SFilipe Manana 				ret = btrfs_del_leaf(trans, root, path, leaf);
4689751a2761SFilipe Manana 				if (ret < 0)
4690751a2761SFilipe Manana 					return ret;
46915f39d397SChris Mason 				free_extent_buffer(leaf);
4692143bede5SJeff Mahoney 				ret = 0;
46935de08d7dSChris Mason 			} else {
4694925baeddSChris Mason 				/* if we're still in the path, make sure
4695925baeddSChris Mason 				 * we're dirty.  Otherwise, one of the
4696925baeddSChris Mason 				 * push_leaf functions must have already
4697925baeddSChris Mason 				 * dirtied this buffer
4698925baeddSChris Mason 				 */
4699925baeddSChris Mason 				if (path->nodes[0] == leaf)
47005f39d397SChris Mason 					btrfs_mark_buffer_dirty(leaf);
47015f39d397SChris Mason 				free_extent_buffer(leaf);
4702be0e5c09SChris Mason 			}
4703d5719762SChris Mason 		} else {
47045f39d397SChris Mason 			btrfs_mark_buffer_dirty(leaf);
4705be0e5c09SChris Mason 		}
47069a8dd150SChris Mason 	}
4707aa5d6bedSChris Mason 	return ret;
47089a8dd150SChris Mason }
47099a8dd150SChris Mason 
471097571fd0SChris Mason /*
47113f157a2fSChris Mason  * A helper function to walk down the tree starting at min_key, and looking
4712de78b51aSEric Sandeen  * for nodes or leaves that are have a minimum transaction id.
4713de78b51aSEric Sandeen  * This is used by the btree defrag code, and tree logging
47143f157a2fSChris Mason  *
47153f157a2fSChris Mason  * This does not cow, but it does stuff the starting key it finds back
47163f157a2fSChris Mason  * into min_key, so you can call btrfs_search_slot with cow=1 on the
47173f157a2fSChris Mason  * key and get a writable path.
47183f157a2fSChris Mason  *
47193f157a2fSChris Mason  * This honors path->lowest_level to prevent descent past a given level
47203f157a2fSChris Mason  * of the tree.
47213f157a2fSChris Mason  *
4722d352ac68SChris Mason  * min_trans indicates the oldest transaction that you are interested
4723d352ac68SChris Mason  * in walking through.  Any nodes or leaves older than min_trans are
4724d352ac68SChris Mason  * skipped over (without reading them).
4725d352ac68SChris Mason  *
47263f157a2fSChris Mason  * returns zero if something useful was found, < 0 on error and 1 if there
47273f157a2fSChris Mason  * was nothing in the tree that matched the search criteria.
47283f157a2fSChris Mason  */
47293f157a2fSChris Mason int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
4730de78b51aSEric Sandeen 			 struct btrfs_path *path,
47313f157a2fSChris Mason 			 u64 min_trans)
47323f157a2fSChris Mason {
47333f157a2fSChris Mason 	struct extent_buffer *cur;
47343f157a2fSChris Mason 	struct btrfs_key found_key;
47353f157a2fSChris Mason 	int slot;
47369652480bSYan 	int sret;
47373f157a2fSChris Mason 	u32 nritems;
47383f157a2fSChris Mason 	int level;
47393f157a2fSChris Mason 	int ret = 1;
4740f98de9b9SFilipe Manana 	int keep_locks = path->keep_locks;
47413f157a2fSChris Mason 
4742c922b016SStefan Roesch 	ASSERT(!path->nowait);
4743f98de9b9SFilipe Manana 	path->keep_locks = 1;
47443f157a2fSChris Mason again:
4745bd681513SChris Mason 	cur = btrfs_read_lock_root_node(root);
47463f157a2fSChris Mason 	level = btrfs_header_level(cur);
4747e02119d5SChris Mason 	WARN_ON(path->nodes[level]);
47483f157a2fSChris Mason 	path->nodes[level] = cur;
4749bd681513SChris Mason 	path->locks[level] = BTRFS_READ_LOCK;
47503f157a2fSChris Mason 
47513f157a2fSChris Mason 	if (btrfs_header_generation(cur) < min_trans) {
47523f157a2fSChris Mason 		ret = 1;
47533f157a2fSChris Mason 		goto out;
47543f157a2fSChris Mason 	}
47553f157a2fSChris Mason 	while (1) {
47563f157a2fSChris Mason 		nritems = btrfs_header_nritems(cur);
47573f157a2fSChris Mason 		level = btrfs_header_level(cur);
4758fdf8d595SAnand Jain 		sret = btrfs_bin_search(cur, 0, min_key, &slot);
4759cbca7d59SFilipe Manana 		if (sret < 0) {
4760cbca7d59SFilipe Manana 			ret = sret;
4761cbca7d59SFilipe Manana 			goto out;
4762cbca7d59SFilipe Manana 		}
47633f157a2fSChris Mason 
4764323ac95bSChris Mason 		/* at the lowest level, we're done, setup the path and exit */
4765323ac95bSChris Mason 		if (level == path->lowest_level) {
4766e02119d5SChris Mason 			if (slot >= nritems)
4767e02119d5SChris Mason 				goto find_next_key;
47683f157a2fSChris Mason 			ret = 0;
47693f157a2fSChris Mason 			path->slots[level] = slot;
47703f157a2fSChris Mason 			btrfs_item_key_to_cpu(cur, &found_key, slot);
47713f157a2fSChris Mason 			goto out;
47723f157a2fSChris Mason 		}
47739652480bSYan 		if (sret && slot > 0)
47749652480bSYan 			slot--;
47753f157a2fSChris Mason 		/*
4776de78b51aSEric Sandeen 		 * check this node pointer against the min_trans parameters.
4777260db43cSRandy Dunlap 		 * If it is too old, skip to the next one.
47783f157a2fSChris Mason 		 */
47793f157a2fSChris Mason 		while (slot < nritems) {
47803f157a2fSChris Mason 			u64 gen;
4781e02119d5SChris Mason 
47823f157a2fSChris Mason 			gen = btrfs_node_ptr_generation(cur, slot);
47833f157a2fSChris Mason 			if (gen < min_trans) {
47843f157a2fSChris Mason 				slot++;
47853f157a2fSChris Mason 				continue;
47863f157a2fSChris Mason 			}
47873f157a2fSChris Mason 			break;
47883f157a2fSChris Mason 		}
4789e02119d5SChris Mason find_next_key:
47903f157a2fSChris Mason 		/*
47913f157a2fSChris Mason 		 * we didn't find a candidate key in this node, walk forward
47923f157a2fSChris Mason 		 * and find another one
47933f157a2fSChris Mason 		 */
47943f157a2fSChris Mason 		if (slot >= nritems) {
4795e02119d5SChris Mason 			path->slots[level] = slot;
4796e02119d5SChris Mason 			sret = btrfs_find_next_key(root, path, min_key, level,
4797de78b51aSEric Sandeen 						  min_trans);
4798e02119d5SChris Mason 			if (sret == 0) {
4799b3b4aa74SDavid Sterba 				btrfs_release_path(path);
48003f157a2fSChris Mason 				goto again;
48013f157a2fSChris Mason 			} else {
48023f157a2fSChris Mason 				goto out;
48033f157a2fSChris Mason 			}
48043f157a2fSChris Mason 		}
48053f157a2fSChris Mason 		/* save our key for returning back */
48063f157a2fSChris Mason 		btrfs_node_key_to_cpu(cur, &found_key, slot);
48073f157a2fSChris Mason 		path->slots[level] = slot;
48083f157a2fSChris Mason 		if (level == path->lowest_level) {
48093f157a2fSChris Mason 			ret = 0;
48103f157a2fSChris Mason 			goto out;
48113f157a2fSChris Mason 		}
48124b231ae4SDavid Sterba 		cur = btrfs_read_node_slot(cur, slot);
4813fb770ae4SLiu Bo 		if (IS_ERR(cur)) {
4814fb770ae4SLiu Bo 			ret = PTR_ERR(cur);
4815fb770ae4SLiu Bo 			goto out;
4816fb770ae4SLiu Bo 		}
48173f157a2fSChris Mason 
4818bd681513SChris Mason 		btrfs_tree_read_lock(cur);
4819b4ce94deSChris Mason 
4820bd681513SChris Mason 		path->locks[level - 1] = BTRFS_READ_LOCK;
48213f157a2fSChris Mason 		path->nodes[level - 1] = cur;
4822f7c79f30SChris Mason 		unlock_up(path, level, 1, 0, NULL);
48233f157a2fSChris Mason 	}
48243f157a2fSChris Mason out:
4825f98de9b9SFilipe Manana 	path->keep_locks = keep_locks;
4826f98de9b9SFilipe Manana 	if (ret == 0) {
4827f98de9b9SFilipe Manana 		btrfs_unlock_up_safe(path, path->lowest_level + 1);
4828f98de9b9SFilipe Manana 		memcpy(min_key, &found_key, sizeof(found_key));
4829f98de9b9SFilipe Manana 	}
48303f157a2fSChris Mason 	return ret;
48313f157a2fSChris Mason }
48323f157a2fSChris Mason 
48333f157a2fSChris Mason /*
48343f157a2fSChris Mason  * this is similar to btrfs_next_leaf, but does not try to preserve
48353f157a2fSChris Mason  * and fixup the path.  It looks for and returns the next key in the
4836de78b51aSEric Sandeen  * tree based on the current path and the min_trans parameters.
48373f157a2fSChris Mason  *
48383f157a2fSChris Mason  * 0 is returned if another key is found, < 0 if there are any errors
48393f157a2fSChris Mason  * and 1 is returned if there are no higher keys in the tree
48403f157a2fSChris Mason  *
48413f157a2fSChris Mason  * path->keep_locks should be set to 1 on the search made before
48423f157a2fSChris Mason  * calling this function.
48433f157a2fSChris Mason  */
4844e7a84565SChris Mason int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
4845de78b51aSEric Sandeen 			struct btrfs_key *key, int level, u64 min_trans)
4846e7a84565SChris Mason {
4847e7a84565SChris Mason 	int slot;
4848e7a84565SChris Mason 	struct extent_buffer *c;
4849e7a84565SChris Mason 
48506a9fb468SJosef Bacik 	WARN_ON(!path->keep_locks && !path->skip_locking);
4851e7a84565SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
4852e7a84565SChris Mason 		if (!path->nodes[level])
4853e7a84565SChris Mason 			return 1;
4854e7a84565SChris Mason 
4855e7a84565SChris Mason 		slot = path->slots[level] + 1;
4856e7a84565SChris Mason 		c = path->nodes[level];
48573f157a2fSChris Mason next:
4858e7a84565SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
485933c66f43SYan Zheng 			int ret;
486033c66f43SYan Zheng 			int orig_lowest;
486133c66f43SYan Zheng 			struct btrfs_key cur_key;
486233c66f43SYan Zheng 			if (level + 1 >= BTRFS_MAX_LEVEL ||
486333c66f43SYan Zheng 			    !path->nodes[level + 1])
4864e7a84565SChris Mason 				return 1;
486533c66f43SYan Zheng 
48666a9fb468SJosef Bacik 			if (path->locks[level + 1] || path->skip_locking) {
486733c66f43SYan Zheng 				level++;
4868e7a84565SChris Mason 				continue;
4869e7a84565SChris Mason 			}
487033c66f43SYan Zheng 
487133c66f43SYan Zheng 			slot = btrfs_header_nritems(c) - 1;
487233c66f43SYan Zheng 			if (level == 0)
487333c66f43SYan Zheng 				btrfs_item_key_to_cpu(c, &cur_key, slot);
487433c66f43SYan Zheng 			else
487533c66f43SYan Zheng 				btrfs_node_key_to_cpu(c, &cur_key, slot);
487633c66f43SYan Zheng 
487733c66f43SYan Zheng 			orig_lowest = path->lowest_level;
4878b3b4aa74SDavid Sterba 			btrfs_release_path(path);
487933c66f43SYan Zheng 			path->lowest_level = level;
488033c66f43SYan Zheng 			ret = btrfs_search_slot(NULL, root, &cur_key, path,
488133c66f43SYan Zheng 						0, 0);
488233c66f43SYan Zheng 			path->lowest_level = orig_lowest;
488333c66f43SYan Zheng 			if (ret < 0)
488433c66f43SYan Zheng 				return ret;
488533c66f43SYan Zheng 
488633c66f43SYan Zheng 			c = path->nodes[level];
488733c66f43SYan Zheng 			slot = path->slots[level];
488833c66f43SYan Zheng 			if (ret == 0)
488933c66f43SYan Zheng 				slot++;
489033c66f43SYan Zheng 			goto next;
489133c66f43SYan Zheng 		}
489233c66f43SYan Zheng 
4893e7a84565SChris Mason 		if (level == 0)
4894e7a84565SChris Mason 			btrfs_item_key_to_cpu(c, key, slot);
48953f157a2fSChris Mason 		else {
48963f157a2fSChris Mason 			u64 gen = btrfs_node_ptr_generation(c, slot);
48973f157a2fSChris Mason 
48983f157a2fSChris Mason 			if (gen < min_trans) {
48993f157a2fSChris Mason 				slot++;
49003f157a2fSChris Mason 				goto next;
49013f157a2fSChris Mason 			}
4902e7a84565SChris Mason 			btrfs_node_key_to_cpu(c, key, slot);
49033f157a2fSChris Mason 		}
4904e7a84565SChris Mason 		return 0;
4905e7a84565SChris Mason 	}
4906e7a84565SChris Mason 	return 1;
4907e7a84565SChris Mason }
4908e7a84565SChris Mason 
49093d7806ecSJan Schmidt int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
49103d7806ecSJan Schmidt 			u64 time_seq)
49113d7806ecSJan Schmidt {
4912d97e63b6SChris Mason 	int slot;
49138e73f275SChris Mason 	int level;
49145f39d397SChris Mason 	struct extent_buffer *c;
49158e73f275SChris Mason 	struct extent_buffer *next;
4916d96b3424SFilipe Manana 	struct btrfs_fs_info *fs_info = root->fs_info;
4917925baeddSChris Mason 	struct btrfs_key key;
4918d96b3424SFilipe Manana 	bool need_commit_sem = false;
4919925baeddSChris Mason 	u32 nritems;
4920925baeddSChris Mason 	int ret;
49210e46318dSJosef Bacik 	int i;
4922925baeddSChris Mason 
4923bdcdd86cSFilipe Manana 	/*
4924bdcdd86cSFilipe Manana 	 * The nowait semantics are used only for write paths, where we don't
4925bdcdd86cSFilipe Manana 	 * use the tree mod log and sequence numbers.
4926bdcdd86cSFilipe Manana 	 */
4927bdcdd86cSFilipe Manana 	if (time_seq)
4928c922b016SStefan Roesch 		ASSERT(!path->nowait);
4929c922b016SStefan Roesch 
4930925baeddSChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
4931d397712bSChris Mason 	if (nritems == 0)
4932925baeddSChris Mason 		return 1;
4933925baeddSChris Mason 
49348e73f275SChris Mason 	btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
49358e73f275SChris Mason again:
49368e73f275SChris Mason 	level = 1;
49378e73f275SChris Mason 	next = NULL;
4938b3b4aa74SDavid Sterba 	btrfs_release_path(path);
49398e73f275SChris Mason 
4940a2135011SChris Mason 	path->keep_locks = 1;
49418e73f275SChris Mason 
4942d96b3424SFilipe Manana 	if (time_seq) {
49433d7806ecSJan Schmidt 		ret = btrfs_search_old_slot(root, &key, path, time_seq);
4944d96b3424SFilipe Manana 	} else {
4945d96b3424SFilipe Manana 		if (path->need_commit_sem) {
4946d96b3424SFilipe Manana 			path->need_commit_sem = 0;
4947d96b3424SFilipe Manana 			need_commit_sem = true;
4948bdcdd86cSFilipe Manana 			if (path->nowait) {
4949bdcdd86cSFilipe Manana 				if (!down_read_trylock(&fs_info->commit_root_sem)) {
4950bdcdd86cSFilipe Manana 					ret = -EAGAIN;
4951bdcdd86cSFilipe Manana 					goto done;
4952bdcdd86cSFilipe Manana 				}
4953bdcdd86cSFilipe Manana 			} else {
4954d96b3424SFilipe Manana 				down_read(&fs_info->commit_root_sem);
4955d96b3424SFilipe Manana 			}
4956bdcdd86cSFilipe Manana 		}
4957925baeddSChris Mason 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4958d96b3424SFilipe Manana 	}
4959925baeddSChris Mason 	path->keep_locks = 0;
4960925baeddSChris Mason 
4961925baeddSChris Mason 	if (ret < 0)
4962d96b3424SFilipe Manana 		goto done;
4963925baeddSChris Mason 
4964a2135011SChris Mason 	nritems = btrfs_header_nritems(path->nodes[0]);
4965168fd7d2SChris Mason 	/*
4966168fd7d2SChris Mason 	 * by releasing the path above we dropped all our locks.  A balance
4967168fd7d2SChris Mason 	 * could have added more items next to the key that used to be
4968168fd7d2SChris Mason 	 * at the very end of the block.  So, check again here and
4969168fd7d2SChris Mason 	 * advance the path if there are now more items available.
4970168fd7d2SChris Mason 	 */
4971a2135011SChris Mason 	if (nritems > 0 && path->slots[0] < nritems - 1) {
4972e457afecSYan Zheng 		if (ret == 0)
4973168fd7d2SChris Mason 			path->slots[0]++;
49748e73f275SChris Mason 		ret = 0;
4975925baeddSChris Mason 		goto done;
4976925baeddSChris Mason 	}
49770b43e04fSLiu Bo 	/*
49780b43e04fSLiu Bo 	 * So the above check misses one case:
49790b43e04fSLiu Bo 	 * - after releasing the path above, someone has removed the item that
49800b43e04fSLiu Bo 	 *   used to be at the very end of the block, and balance between leafs
49810b43e04fSLiu Bo 	 *   gets another one with bigger key.offset to replace it.
49820b43e04fSLiu Bo 	 *
49830b43e04fSLiu Bo 	 * This one should be returned as well, or we can get leaf corruption
49840b43e04fSLiu Bo 	 * later(esp. in __btrfs_drop_extents()).
49850b43e04fSLiu Bo 	 *
49860b43e04fSLiu Bo 	 * And a bit more explanation about this check,
49870b43e04fSLiu Bo 	 * with ret > 0, the key isn't found, the path points to the slot
49880b43e04fSLiu Bo 	 * where it should be inserted, so the path->slots[0] item must be the
49890b43e04fSLiu Bo 	 * bigger one.
49900b43e04fSLiu Bo 	 */
49910b43e04fSLiu Bo 	if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
49920b43e04fSLiu Bo 		ret = 0;
49930b43e04fSLiu Bo 		goto done;
49940b43e04fSLiu Bo 	}
4995d97e63b6SChris Mason 
4996234b63a0SChris Mason 	while (level < BTRFS_MAX_LEVEL) {
49978e73f275SChris Mason 		if (!path->nodes[level]) {
49988e73f275SChris Mason 			ret = 1;
49998e73f275SChris Mason 			goto done;
50008e73f275SChris Mason 		}
50015f39d397SChris Mason 
5002d97e63b6SChris Mason 		slot = path->slots[level] + 1;
5003d97e63b6SChris Mason 		c = path->nodes[level];
50045f39d397SChris Mason 		if (slot >= btrfs_header_nritems(c)) {
5005d97e63b6SChris Mason 			level++;
50068e73f275SChris Mason 			if (level == BTRFS_MAX_LEVEL) {
50078e73f275SChris Mason 				ret = 1;
50088e73f275SChris Mason 				goto done;
50098e73f275SChris Mason 			}
5010d97e63b6SChris Mason 			continue;
5011d97e63b6SChris Mason 		}
50125f39d397SChris Mason 
50130e46318dSJosef Bacik 
50140e46318dSJosef Bacik 		/*
50150e46318dSJosef Bacik 		 * Our current level is where we're going to start from, and to
50160e46318dSJosef Bacik 		 * make sure lockdep doesn't complain we need to drop our locks
50170e46318dSJosef Bacik 		 * and nodes from 0 to our current level.
50180e46318dSJosef Bacik 		 */
50190e46318dSJosef Bacik 		for (i = 0; i < level; i++) {
50200e46318dSJosef Bacik 			if (path->locks[level]) {
50210e46318dSJosef Bacik 				btrfs_tree_read_unlock(path->nodes[i]);
50220e46318dSJosef Bacik 				path->locks[i] = 0;
50230e46318dSJosef Bacik 			}
50240e46318dSJosef Bacik 			free_extent_buffer(path->nodes[i]);
50250e46318dSJosef Bacik 			path->nodes[i] = NULL;
5026925baeddSChris Mason 		}
50275f39d397SChris Mason 
50288e73f275SChris Mason 		next = c;
5029d07b8528SLiu Bo 		ret = read_block_for_search(root, path, &next, level,
5030cda79c54SDavid Sterba 					    slot, &key);
5031bdcdd86cSFilipe Manana 		if (ret == -EAGAIN && !path->nowait)
50328e73f275SChris Mason 			goto again;
50335f39d397SChris Mason 
503476a05b35SChris Mason 		if (ret < 0) {
5035b3b4aa74SDavid Sterba 			btrfs_release_path(path);
503676a05b35SChris Mason 			goto done;
503776a05b35SChris Mason 		}
503876a05b35SChris Mason 
50395cd57b2cSChris Mason 		if (!path->skip_locking) {
5040bd681513SChris Mason 			ret = btrfs_try_tree_read_lock(next);
5041bdcdd86cSFilipe Manana 			if (!ret && path->nowait) {
5042bdcdd86cSFilipe Manana 				ret = -EAGAIN;
5043bdcdd86cSFilipe Manana 				goto done;
5044bdcdd86cSFilipe Manana 			}
5045d42244a0SJan Schmidt 			if (!ret && time_seq) {
5046d42244a0SJan Schmidt 				/*
5047d42244a0SJan Schmidt 				 * If we don't get the lock, we may be racing
5048d42244a0SJan Schmidt 				 * with push_leaf_left, holding that lock while
5049d42244a0SJan Schmidt 				 * itself waiting for the leaf we've currently
5050d42244a0SJan Schmidt 				 * locked. To solve this situation, we give up
5051d42244a0SJan Schmidt 				 * on our lock and cycle.
5052d42244a0SJan Schmidt 				 */
5053cf538830SJan Schmidt 				free_extent_buffer(next);
5054d42244a0SJan Schmidt 				btrfs_release_path(path);
5055d42244a0SJan Schmidt 				cond_resched();
5056d42244a0SJan Schmidt 				goto again;
5057d42244a0SJan Schmidt 			}
50580e46318dSJosef Bacik 			if (!ret)
50590e46318dSJosef Bacik 				btrfs_tree_read_lock(next);
5060bd681513SChris Mason 		}
5061d97e63b6SChris Mason 		break;
5062d97e63b6SChris Mason 	}
5063d97e63b6SChris Mason 	path->slots[level] = slot;
5064d97e63b6SChris Mason 	while (1) {
5065d97e63b6SChris Mason 		level--;
5066d97e63b6SChris Mason 		path->nodes[level] = next;
5067d97e63b6SChris Mason 		path->slots[level] = 0;
5068a74a4b97SChris Mason 		if (!path->skip_locking)
5069ffeb03cfSJosef Bacik 			path->locks[level] = BTRFS_READ_LOCK;
5070d97e63b6SChris Mason 		if (!level)
5071d97e63b6SChris Mason 			break;
5072b4ce94deSChris Mason 
5073d07b8528SLiu Bo 		ret = read_block_for_search(root, path, &next, level,
5074cda79c54SDavid Sterba 					    0, &key);
5075bdcdd86cSFilipe Manana 		if (ret == -EAGAIN && !path->nowait)
50768e73f275SChris Mason 			goto again;
50778e73f275SChris Mason 
507876a05b35SChris Mason 		if (ret < 0) {
5079b3b4aa74SDavid Sterba 			btrfs_release_path(path);
508076a05b35SChris Mason 			goto done;
508176a05b35SChris Mason 		}
508276a05b35SChris Mason 
5083bdcdd86cSFilipe Manana 		if (!path->skip_locking) {
5084bdcdd86cSFilipe Manana 			if (path->nowait) {
5085bdcdd86cSFilipe Manana 				if (!btrfs_try_tree_read_lock(next)) {
5086bdcdd86cSFilipe Manana 					ret = -EAGAIN;
5087bdcdd86cSFilipe Manana 					goto done;
5088bdcdd86cSFilipe Manana 				}
5089bdcdd86cSFilipe Manana 			} else {
50900e46318dSJosef Bacik 				btrfs_tree_read_lock(next);
5091d97e63b6SChris Mason 			}
5092bdcdd86cSFilipe Manana 		}
5093bdcdd86cSFilipe Manana 	}
50948e73f275SChris Mason 	ret = 0;
5095925baeddSChris Mason done:
5096f7c79f30SChris Mason 	unlock_up(path, 0, 1, 0, NULL);
5097d96b3424SFilipe Manana 	if (need_commit_sem) {
5098d96b3424SFilipe Manana 		int ret2;
5099d96b3424SFilipe Manana 
5100d96b3424SFilipe Manana 		path->need_commit_sem = 1;
5101d96b3424SFilipe Manana 		ret2 = finish_need_commit_sem_search(path);
5102d96b3424SFilipe Manana 		up_read(&fs_info->commit_root_sem);
5103d96b3424SFilipe Manana 		if (ret2)
5104d96b3424SFilipe Manana 			ret = ret2;
5105d96b3424SFilipe Manana 	}
51068e73f275SChris Mason 
51078e73f275SChris Mason 	return ret;
5108d97e63b6SChris Mason }
51090b86a832SChris Mason 
5110890d2b1aSJosef Bacik int btrfs_next_old_item(struct btrfs_root *root, struct btrfs_path *path, u64 time_seq)
5111890d2b1aSJosef Bacik {
5112890d2b1aSJosef Bacik 	path->slots[0]++;
5113890d2b1aSJosef Bacik 	if (path->slots[0] >= btrfs_header_nritems(path->nodes[0]))
5114890d2b1aSJosef Bacik 		return btrfs_next_old_leaf(root, path, time_seq);
5115890d2b1aSJosef Bacik 	return 0;
5116890d2b1aSJosef Bacik }
5117890d2b1aSJosef Bacik 
51183f157a2fSChris Mason /*
51193f157a2fSChris Mason  * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
51203f157a2fSChris Mason  * searching until it gets past min_objectid or finds an item of 'type'
51213f157a2fSChris Mason  *
51223f157a2fSChris Mason  * returns 0 if something is found, 1 if nothing was found and < 0 on error
51233f157a2fSChris Mason  */
51240b86a832SChris Mason int btrfs_previous_item(struct btrfs_root *root,
51250b86a832SChris Mason 			struct btrfs_path *path, u64 min_objectid,
51260b86a832SChris Mason 			int type)
51270b86a832SChris Mason {
51280b86a832SChris Mason 	struct btrfs_key found_key;
51290b86a832SChris Mason 	struct extent_buffer *leaf;
5130e02119d5SChris Mason 	u32 nritems;
51310b86a832SChris Mason 	int ret;
51320b86a832SChris Mason 
51330b86a832SChris Mason 	while (1) {
51340b86a832SChris Mason 		if (path->slots[0] == 0) {
51350b86a832SChris Mason 			ret = btrfs_prev_leaf(root, path);
51360b86a832SChris Mason 			if (ret != 0)
51370b86a832SChris Mason 				return ret;
51380b86a832SChris Mason 		} else {
51390b86a832SChris Mason 			path->slots[0]--;
51400b86a832SChris Mason 		}
51410b86a832SChris Mason 		leaf = path->nodes[0];
5142e02119d5SChris Mason 		nritems = btrfs_header_nritems(leaf);
5143e02119d5SChris Mason 		if (nritems == 0)
5144e02119d5SChris Mason 			return 1;
5145e02119d5SChris Mason 		if (path->slots[0] == nritems)
5146e02119d5SChris Mason 			path->slots[0]--;
5147e02119d5SChris Mason 
51480b86a832SChris Mason 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5149e02119d5SChris Mason 		if (found_key.objectid < min_objectid)
5150e02119d5SChris Mason 			break;
51510a4eefbbSYan Zheng 		if (found_key.type == type)
51520a4eefbbSYan Zheng 			return 0;
5153e02119d5SChris Mason 		if (found_key.objectid == min_objectid &&
5154e02119d5SChris Mason 		    found_key.type < type)
5155e02119d5SChris Mason 			break;
51560b86a832SChris Mason 	}
51570b86a832SChris Mason 	return 1;
51580b86a832SChris Mason }
5159ade2e0b3SWang Shilong 
5160ade2e0b3SWang Shilong /*
5161ade2e0b3SWang Shilong  * search in extent tree to find a previous Metadata/Data extent item with
5162ade2e0b3SWang Shilong  * min objecitd.
5163ade2e0b3SWang Shilong  *
5164ade2e0b3SWang Shilong  * returns 0 if something is found, 1 if nothing was found and < 0 on error
5165ade2e0b3SWang Shilong  */
5166ade2e0b3SWang Shilong int btrfs_previous_extent_item(struct btrfs_root *root,
5167ade2e0b3SWang Shilong 			struct btrfs_path *path, u64 min_objectid)
5168ade2e0b3SWang Shilong {
5169ade2e0b3SWang Shilong 	struct btrfs_key found_key;
5170ade2e0b3SWang Shilong 	struct extent_buffer *leaf;
5171ade2e0b3SWang Shilong 	u32 nritems;
5172ade2e0b3SWang Shilong 	int ret;
5173ade2e0b3SWang Shilong 
5174ade2e0b3SWang Shilong 	while (1) {
5175ade2e0b3SWang Shilong 		if (path->slots[0] == 0) {
5176ade2e0b3SWang Shilong 			ret = btrfs_prev_leaf(root, path);
5177ade2e0b3SWang Shilong 			if (ret != 0)
5178ade2e0b3SWang Shilong 				return ret;
5179ade2e0b3SWang Shilong 		} else {
5180ade2e0b3SWang Shilong 			path->slots[0]--;
5181ade2e0b3SWang Shilong 		}
5182ade2e0b3SWang Shilong 		leaf = path->nodes[0];
5183ade2e0b3SWang Shilong 		nritems = btrfs_header_nritems(leaf);
5184ade2e0b3SWang Shilong 		if (nritems == 0)
5185ade2e0b3SWang Shilong 			return 1;
5186ade2e0b3SWang Shilong 		if (path->slots[0] == nritems)
5187ade2e0b3SWang Shilong 			path->slots[0]--;
5188ade2e0b3SWang Shilong 
5189ade2e0b3SWang Shilong 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5190ade2e0b3SWang Shilong 		if (found_key.objectid < min_objectid)
5191ade2e0b3SWang Shilong 			break;
5192ade2e0b3SWang Shilong 		if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5193ade2e0b3SWang Shilong 		    found_key.type == BTRFS_METADATA_ITEM_KEY)
5194ade2e0b3SWang Shilong 			return 0;
5195ade2e0b3SWang Shilong 		if (found_key.objectid == min_objectid &&
5196ade2e0b3SWang Shilong 		    found_key.type < BTRFS_EXTENT_ITEM_KEY)
5197ade2e0b3SWang Shilong 			break;
5198ade2e0b3SWang Shilong 	}
5199ade2e0b3SWang Shilong 	return 1;
5200ade2e0b3SWang Shilong }
5201226463d7SJosef Bacik 
5202226463d7SJosef Bacik int __init btrfs_ctree_init(void)
5203226463d7SJosef Bacik {
5204226463d7SJosef Bacik 	btrfs_path_cachep = kmem_cache_create("btrfs_path",
5205226463d7SJosef Bacik 			sizeof(struct btrfs_path), 0,
5206226463d7SJosef Bacik 			SLAB_MEM_SPREAD, NULL);
5207226463d7SJosef Bacik 	if (!btrfs_path_cachep)
5208226463d7SJosef Bacik 		return -ENOMEM;
5209226463d7SJosef Bacik 	return 0;
5210226463d7SJosef Bacik }
5211226463d7SJosef Bacik 
5212226463d7SJosef Bacik void __cold btrfs_ctree_exit(void)
5213226463d7SJosef Bacik {
5214226463d7SJosef Bacik 	kmem_cache_destroy(btrfs_path_cachep);
5215226463d7SJosef Bacik }
5216