xref: /openbmc/linux/fs/btrfs/relocation.c (revision 1fec12a5)
1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
25d4f98a2SYan Zheng /*
35d4f98a2SYan Zheng  * Copyright (C) 2009 Oracle.  All rights reserved.
45d4f98a2SYan Zheng  */
55d4f98a2SYan Zheng 
65d4f98a2SYan Zheng #include <linux/sched.h>
75d4f98a2SYan Zheng #include <linux/pagemap.h>
85d4f98a2SYan Zheng #include <linux/writeback.h>
95d4f98a2SYan Zheng #include <linux/blkdev.h>
105d4f98a2SYan Zheng #include <linux/rbtree.h>
115a0e3ad6STejun Heo #include <linux/slab.h>
12726a3421SQu Wenruo #include <linux/error-injection.h>
135d4f98a2SYan Zheng #include "ctree.h"
145d4f98a2SYan Zheng #include "disk-io.h"
155d4f98a2SYan Zheng #include "transaction.h"
165d4f98a2SYan Zheng #include "volumes.h"
175d4f98a2SYan Zheng #include "locking.h"
185d4f98a2SYan Zheng #include "btrfs_inode.h"
195d4f98a2SYan Zheng #include "async-thread.h"
200af3d00bSJosef Bacik #include "free-space-cache.h"
2162b99540SQu Wenruo #include "qgroup.h"
22cdccee99SLiu Bo #include "print-tree.h"
2386736342SJosef Bacik #include "delalloc-space.h"
24aac0023cSJosef Bacik #include "block-group.h"
2519b546d7SQu Wenruo #include "backref.h"
26e9a28dc5SQu Wenruo #include "misc.h"
275d4f98a2SYan Zheng 
285d4f98a2SYan Zheng /*
290c891389SQu Wenruo  * Relocation overview
300c891389SQu Wenruo  *
310c891389SQu Wenruo  * [What does relocation do]
320c891389SQu Wenruo  *
330c891389SQu Wenruo  * The objective of relocation is to relocate all extents of the target block
340c891389SQu Wenruo  * group to other block groups.
350c891389SQu Wenruo  * This is utilized by resize (shrink only), profile converting, compacting
360c891389SQu Wenruo  * space, or balance routine to spread chunks over devices.
370c891389SQu Wenruo  *
380c891389SQu Wenruo  * 		Before		|		After
390c891389SQu Wenruo  * ------------------------------------------------------------------
400c891389SQu Wenruo  *  BG A: 10 data extents	| BG A: deleted
410c891389SQu Wenruo  *  BG B:  2 data extents	| BG B: 10 data extents (2 old + 8 relocated)
420c891389SQu Wenruo  *  BG C:  1 extents		| BG C:  3 data extents (1 old + 2 relocated)
430c891389SQu Wenruo  *
440c891389SQu Wenruo  * [How does relocation work]
450c891389SQu Wenruo  *
460c891389SQu Wenruo  * 1.   Mark the target block group read-only
470c891389SQu Wenruo  *      New extents won't be allocated from the target block group.
480c891389SQu Wenruo  *
490c891389SQu Wenruo  * 2.1  Record each extent in the target block group
500c891389SQu Wenruo  *      To build a proper map of extents to be relocated.
510c891389SQu Wenruo  *
520c891389SQu Wenruo  * 2.2  Build data reloc tree and reloc trees
530c891389SQu Wenruo  *      Data reloc tree will contain an inode, recording all newly relocated
540c891389SQu Wenruo  *      data extents.
550c891389SQu Wenruo  *      There will be only one data reloc tree for one data block group.
560c891389SQu Wenruo  *
570c891389SQu Wenruo  *      Reloc tree will be a special snapshot of its source tree, containing
580c891389SQu Wenruo  *      relocated tree blocks.
590c891389SQu Wenruo  *      Each tree referring to a tree block in target block group will get its
600c891389SQu Wenruo  *      reloc tree built.
610c891389SQu Wenruo  *
620c891389SQu Wenruo  * 2.3  Swap source tree with its corresponding reloc tree
630c891389SQu Wenruo  *      Each involved tree only refers to new extents after swap.
640c891389SQu Wenruo  *
650c891389SQu Wenruo  * 3.   Cleanup reloc trees and data reloc tree.
660c891389SQu Wenruo  *      As old extents in the target block group are still referenced by reloc
670c891389SQu Wenruo  *      trees, we need to clean them up before really freeing the target block
680c891389SQu Wenruo  *      group.
690c891389SQu Wenruo  *
700c891389SQu Wenruo  * The main complexity is in steps 2.2 and 2.3.
710c891389SQu Wenruo  *
720c891389SQu Wenruo  * The entry point of relocation is relocate_block_group() function.
730c891389SQu Wenruo  */
740c891389SQu Wenruo 
750647bf56SWang Shilong #define RELOCATION_RESERVED_NODES	256
762a979612SQu Wenruo /*
775d4f98a2SYan Zheng  * map address of tree root to tree
785d4f98a2SYan Zheng  */
795d4f98a2SYan Zheng struct mapping_node {
80e9a28dc5SQu Wenruo 	struct {
815d4f98a2SYan Zheng 		struct rb_node rb_node;
825d4f98a2SYan Zheng 		u64 bytenr;
83e9a28dc5SQu Wenruo 	}; /* Use rb_simle_node for search/insert */
845d4f98a2SYan Zheng 	void *data;
855d4f98a2SYan Zheng };
865d4f98a2SYan Zheng 
875d4f98a2SYan Zheng struct mapping_tree {
885d4f98a2SYan Zheng 	struct rb_root rb_root;
895d4f98a2SYan Zheng 	spinlock_t lock;
905d4f98a2SYan Zheng };
915d4f98a2SYan Zheng 
925d4f98a2SYan Zheng /*
935d4f98a2SYan Zheng  * present a tree block to process
945d4f98a2SYan Zheng  */
955d4f98a2SYan Zheng struct tree_block {
96e9a28dc5SQu Wenruo 	struct {
975d4f98a2SYan Zheng 		struct rb_node rb_node;
985d4f98a2SYan Zheng 		u64 bytenr;
99e9a28dc5SQu Wenruo 	}; /* Use rb_simple_node for search/insert */
1005d4f98a2SYan Zheng 	struct btrfs_key key;
1015d4f98a2SYan Zheng 	unsigned int level:8;
1025d4f98a2SYan Zheng 	unsigned int key_ready:1;
1035d4f98a2SYan Zheng };
1045d4f98a2SYan Zheng 
1050257bb82SYan, Zheng #define MAX_EXTENTS 128
1060257bb82SYan, Zheng 
1070257bb82SYan, Zheng struct file_extent_cluster {
1080257bb82SYan, Zheng 	u64 start;
1090257bb82SYan, Zheng 	u64 end;
1100257bb82SYan, Zheng 	u64 boundary[MAX_EXTENTS];
1110257bb82SYan, Zheng 	unsigned int nr;
1120257bb82SYan, Zheng };
1130257bb82SYan, Zheng 
1145d4f98a2SYan Zheng struct reloc_control {
1155d4f98a2SYan Zheng 	/* block group to relocate */
11632da5386SDavid Sterba 	struct btrfs_block_group *block_group;
1175d4f98a2SYan Zheng 	/* extent tree */
1185d4f98a2SYan Zheng 	struct btrfs_root *extent_root;
1195d4f98a2SYan Zheng 	/* inode for moving data */
1205d4f98a2SYan Zheng 	struct inode *data_inode;
1213fd0a558SYan, Zheng 
1223fd0a558SYan, Zheng 	struct btrfs_block_rsv *block_rsv;
1233fd0a558SYan, Zheng 
124a26195a5SQu Wenruo 	struct btrfs_backref_cache backref_cache;
1253fd0a558SYan, Zheng 
1263fd0a558SYan, Zheng 	struct file_extent_cluster cluster;
1275d4f98a2SYan Zheng 	/* tree blocks have been processed */
1285d4f98a2SYan Zheng 	struct extent_io_tree processed_blocks;
1295d4f98a2SYan Zheng 	/* map start of tree root to corresponding reloc tree */
1305d4f98a2SYan Zheng 	struct mapping_tree reloc_root_tree;
1315d4f98a2SYan Zheng 	/* list of reloc trees */
1325d4f98a2SYan Zheng 	struct list_head reloc_roots;
133d2311e69SQu Wenruo 	/* list of subvolume trees that get relocated */
134d2311e69SQu Wenruo 	struct list_head dirty_subvol_roots;
1353fd0a558SYan, Zheng 	/* size of metadata reservation for merging reloc trees */
1363fd0a558SYan, Zheng 	u64 merging_rsv_size;
1373fd0a558SYan, Zheng 	/* size of relocated tree nodes */
1383fd0a558SYan, Zheng 	u64 nodes_relocated;
1390647bf56SWang Shilong 	/* reserved size for block group relocation*/
1400647bf56SWang Shilong 	u64 reserved_bytes;
1413fd0a558SYan, Zheng 
1425d4f98a2SYan Zheng 	u64 search_start;
1435d4f98a2SYan Zheng 	u64 extents_found;
1443fd0a558SYan, Zheng 
1453fd0a558SYan, Zheng 	unsigned int stage:8;
1463fd0a558SYan, Zheng 	unsigned int create_reloc_tree:1;
1473fd0a558SYan, Zheng 	unsigned int merge_reloc_tree:1;
1485d4f98a2SYan Zheng 	unsigned int found_file_extent:1;
1495d4f98a2SYan Zheng };
1505d4f98a2SYan Zheng 
1515d4f98a2SYan Zheng /* stages of data relocation */
1525d4f98a2SYan Zheng #define MOVE_DATA_EXTENTS	0
1535d4f98a2SYan Zheng #define UPDATE_DATA_PTRS	1
1545d4f98a2SYan Zheng 
1559569cc20SQu Wenruo static void mark_block_processed(struct reloc_control *rc,
156a26195a5SQu Wenruo 				 struct btrfs_backref_node *node)
1579569cc20SQu Wenruo {
1589569cc20SQu Wenruo 	u32 blocksize;
1599569cc20SQu Wenruo 
1609569cc20SQu Wenruo 	if (node->level == 0 ||
1619569cc20SQu Wenruo 	    in_range(node->bytenr, rc->block_group->start,
1629569cc20SQu Wenruo 		     rc->block_group->length)) {
1639569cc20SQu Wenruo 		blocksize = rc->extent_root->fs_info->nodesize;
1649569cc20SQu Wenruo 		set_extent_bits(&rc->processed_blocks, node->bytenr,
1659569cc20SQu Wenruo 				node->bytenr + blocksize - 1, EXTENT_DIRTY);
1669569cc20SQu Wenruo 	}
1679569cc20SQu Wenruo 	node->processed = 1;
1689569cc20SQu Wenruo }
1699569cc20SQu Wenruo 
1705d4f98a2SYan Zheng 
1715d4f98a2SYan Zheng static void mapping_tree_init(struct mapping_tree *tree)
1725d4f98a2SYan Zheng {
1736bef4d31SEric Paris 	tree->rb_root = RB_ROOT;
1745d4f98a2SYan Zheng 	spin_lock_init(&tree->lock);
1755d4f98a2SYan Zheng }
1765d4f98a2SYan Zheng 
1775d4f98a2SYan Zheng /*
1785d4f98a2SYan Zheng  * walk up backref nodes until reach node presents tree root
1795d4f98a2SYan Zheng  */
180a26195a5SQu Wenruo static struct btrfs_backref_node *walk_up_backref(
181a26195a5SQu Wenruo 		struct btrfs_backref_node *node,
182a26195a5SQu Wenruo 		struct btrfs_backref_edge *edges[], int *index)
1835d4f98a2SYan Zheng {
184a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
1855d4f98a2SYan Zheng 	int idx = *index;
1865d4f98a2SYan Zheng 
1875d4f98a2SYan Zheng 	while (!list_empty(&node->upper)) {
1885d4f98a2SYan Zheng 		edge = list_entry(node->upper.next,
189a26195a5SQu Wenruo 				  struct btrfs_backref_edge, list[LOWER]);
1905d4f98a2SYan Zheng 		edges[idx++] = edge;
1915d4f98a2SYan Zheng 		node = edge->node[UPPER];
1925d4f98a2SYan Zheng 	}
1933fd0a558SYan, Zheng 	BUG_ON(node->detached);
1945d4f98a2SYan Zheng 	*index = idx;
1955d4f98a2SYan Zheng 	return node;
1965d4f98a2SYan Zheng }
1975d4f98a2SYan Zheng 
1985d4f98a2SYan Zheng /*
1995d4f98a2SYan Zheng  * walk down backref nodes to find start of next reference path
2005d4f98a2SYan Zheng  */
201a26195a5SQu Wenruo static struct btrfs_backref_node *walk_down_backref(
202a26195a5SQu Wenruo 		struct btrfs_backref_edge *edges[], int *index)
2035d4f98a2SYan Zheng {
204a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
205a26195a5SQu Wenruo 	struct btrfs_backref_node *lower;
2065d4f98a2SYan Zheng 	int idx = *index;
2075d4f98a2SYan Zheng 
2085d4f98a2SYan Zheng 	while (idx > 0) {
2095d4f98a2SYan Zheng 		edge = edges[idx - 1];
2105d4f98a2SYan Zheng 		lower = edge->node[LOWER];
2115d4f98a2SYan Zheng 		if (list_is_last(&edge->list[LOWER], &lower->upper)) {
2125d4f98a2SYan Zheng 			idx--;
2135d4f98a2SYan Zheng 			continue;
2145d4f98a2SYan Zheng 		}
2155d4f98a2SYan Zheng 		edge = list_entry(edge->list[LOWER].next,
216a26195a5SQu Wenruo 				  struct btrfs_backref_edge, list[LOWER]);
2175d4f98a2SYan Zheng 		edges[idx - 1] = edge;
2185d4f98a2SYan Zheng 		*index = idx;
2195d4f98a2SYan Zheng 		return edge->node[UPPER];
2205d4f98a2SYan Zheng 	}
2215d4f98a2SYan Zheng 	*index = 0;
2225d4f98a2SYan Zheng 	return NULL;
2235d4f98a2SYan Zheng }
2245d4f98a2SYan Zheng 
225a26195a5SQu Wenruo static void update_backref_node(struct btrfs_backref_cache *cache,
226a26195a5SQu Wenruo 				struct btrfs_backref_node *node, u64 bytenr)
2273fd0a558SYan, Zheng {
2283fd0a558SYan, Zheng 	struct rb_node *rb_node;
2293fd0a558SYan, Zheng 	rb_erase(&node->rb_node, &cache->rb_root);
2303fd0a558SYan, Zheng 	node->bytenr = bytenr;
231e9a28dc5SQu Wenruo 	rb_node = rb_simple_insert(&cache->rb_root, node->bytenr, &node->rb_node);
23243c04fb1SJeff Mahoney 	if (rb_node)
233982c92cbSQu Wenruo 		btrfs_backref_panic(cache->fs_info, bytenr, -EEXIST);
2343fd0a558SYan, Zheng }
2353fd0a558SYan, Zheng 
2363fd0a558SYan, Zheng /*
2373fd0a558SYan, Zheng  * update backref cache after a transaction commit
2383fd0a558SYan, Zheng  */
2393fd0a558SYan, Zheng static int update_backref_cache(struct btrfs_trans_handle *trans,
240a26195a5SQu Wenruo 				struct btrfs_backref_cache *cache)
2413fd0a558SYan, Zheng {
242a26195a5SQu Wenruo 	struct btrfs_backref_node *node;
2433fd0a558SYan, Zheng 	int level = 0;
2443fd0a558SYan, Zheng 
2453fd0a558SYan, Zheng 	if (cache->last_trans == 0) {
2463fd0a558SYan, Zheng 		cache->last_trans = trans->transid;
2473fd0a558SYan, Zheng 		return 0;
2483fd0a558SYan, Zheng 	}
2493fd0a558SYan, Zheng 
2503fd0a558SYan, Zheng 	if (cache->last_trans == trans->transid)
2513fd0a558SYan, Zheng 		return 0;
2523fd0a558SYan, Zheng 
2533fd0a558SYan, Zheng 	/*
2543fd0a558SYan, Zheng 	 * detached nodes are used to avoid unnecessary backref
2553fd0a558SYan, Zheng 	 * lookup. transaction commit changes the extent tree.
2563fd0a558SYan, Zheng 	 * so the detached nodes are no longer useful.
2573fd0a558SYan, Zheng 	 */
2583fd0a558SYan, Zheng 	while (!list_empty(&cache->detached)) {
2593fd0a558SYan, Zheng 		node = list_entry(cache->detached.next,
260a26195a5SQu Wenruo 				  struct btrfs_backref_node, list);
261023acb07SQu Wenruo 		btrfs_backref_cleanup_node(cache, node);
2623fd0a558SYan, Zheng 	}
2633fd0a558SYan, Zheng 
2643fd0a558SYan, Zheng 	while (!list_empty(&cache->changed)) {
2653fd0a558SYan, Zheng 		node = list_entry(cache->changed.next,
266a26195a5SQu Wenruo 				  struct btrfs_backref_node, list);
2673fd0a558SYan, Zheng 		list_del_init(&node->list);
2683fd0a558SYan, Zheng 		BUG_ON(node->pending);
2693fd0a558SYan, Zheng 		update_backref_node(cache, node, node->new_bytenr);
2703fd0a558SYan, Zheng 	}
2713fd0a558SYan, Zheng 
2723fd0a558SYan, Zheng 	/*
2733fd0a558SYan, Zheng 	 * some nodes can be left in the pending list if there were
2743fd0a558SYan, Zheng 	 * errors during processing the pending nodes.
2753fd0a558SYan, Zheng 	 */
2763fd0a558SYan, Zheng 	for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2773fd0a558SYan, Zheng 		list_for_each_entry(node, &cache->pending[level], list) {
2783fd0a558SYan, Zheng 			BUG_ON(!node->pending);
2793fd0a558SYan, Zheng 			if (node->bytenr == node->new_bytenr)
2803fd0a558SYan, Zheng 				continue;
2813fd0a558SYan, Zheng 			update_backref_node(cache, node, node->new_bytenr);
2823fd0a558SYan, Zheng 		}
2833fd0a558SYan, Zheng 	}
2843fd0a558SYan, Zheng 
2853fd0a558SYan, Zheng 	cache->last_trans = 0;
2863fd0a558SYan, Zheng 	return 1;
2873fd0a558SYan, Zheng }
2883fd0a558SYan, Zheng 
2896282675eSQu Wenruo static bool reloc_root_is_dead(struct btrfs_root *root)
2906282675eSQu Wenruo {
2916282675eSQu Wenruo 	/*
2926282675eSQu Wenruo 	 * Pair with set_bit/clear_bit in clean_dirty_subvols and
2936282675eSQu Wenruo 	 * btrfs_update_reloc_root. We need to see the updated bit before
2946282675eSQu Wenruo 	 * trying to access reloc_root
2956282675eSQu Wenruo 	 */
2966282675eSQu Wenruo 	smp_rmb();
2976282675eSQu Wenruo 	if (test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state))
2986282675eSQu Wenruo 		return true;
2996282675eSQu Wenruo 	return false;
3006282675eSQu Wenruo }
3016282675eSQu Wenruo 
3026282675eSQu Wenruo /*
3036282675eSQu Wenruo  * Check if this subvolume tree has valid reloc tree.
3046282675eSQu Wenruo  *
3056282675eSQu Wenruo  * Reloc tree after swap is considered dead, thus not considered as valid.
3066282675eSQu Wenruo  * This is enough for most callers, as they don't distinguish dead reloc root
30755465730SQu Wenruo  * from no reloc root.  But btrfs_should_ignore_reloc_root() below is a
30855465730SQu Wenruo  * special case.
3096282675eSQu Wenruo  */
3106282675eSQu Wenruo static bool have_reloc_root(struct btrfs_root *root)
3116282675eSQu Wenruo {
3126282675eSQu Wenruo 	if (reloc_root_is_dead(root))
3136282675eSQu Wenruo 		return false;
3146282675eSQu Wenruo 	if (!root->reloc_root)
3156282675eSQu Wenruo 		return false;
3166282675eSQu Wenruo 	return true;
3176282675eSQu Wenruo }
318f2a97a9dSDavid Sterba 
31955465730SQu Wenruo int btrfs_should_ignore_reloc_root(struct btrfs_root *root)
3203fd0a558SYan, Zheng {
3213fd0a558SYan, Zheng 	struct btrfs_root *reloc_root;
3223fd0a558SYan, Zheng 
32392a7cc42SQu Wenruo 	if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
3243fd0a558SYan, Zheng 		return 0;
3253fd0a558SYan, Zheng 
3266282675eSQu Wenruo 	/* This root has been merged with its reloc tree, we can ignore it */
3276282675eSQu Wenruo 	if (reloc_root_is_dead(root))
3286282675eSQu Wenruo 		return 1;
3296282675eSQu Wenruo 
3303fd0a558SYan, Zheng 	reloc_root = root->reloc_root;
3313fd0a558SYan, Zheng 	if (!reloc_root)
3323fd0a558SYan, Zheng 		return 0;
3333fd0a558SYan, Zheng 
3344d4225fcSJosef Bacik 	if (btrfs_header_generation(reloc_root->commit_root) ==
3354d4225fcSJosef Bacik 	    root->fs_info->running_transaction->transid)
3363fd0a558SYan, Zheng 		return 0;
3373fd0a558SYan, Zheng 	/*
3383fd0a558SYan, Zheng 	 * if there is reloc tree and it was created in previous
3393fd0a558SYan, Zheng 	 * transaction backref lookup can find the reloc tree,
3403fd0a558SYan, Zheng 	 * so backref node for the fs tree root is useless for
3413fd0a558SYan, Zheng 	 * relocation.
3423fd0a558SYan, Zheng 	 */
3433fd0a558SYan, Zheng 	return 1;
3443fd0a558SYan, Zheng }
34555465730SQu Wenruo 
3465d4f98a2SYan Zheng /*
3475d4f98a2SYan Zheng  * find reloc tree by address of tree root
3485d4f98a2SYan Zheng  */
3492433bea5SQu Wenruo struct btrfs_root *find_reloc_root(struct btrfs_fs_info *fs_info, u64 bytenr)
3505d4f98a2SYan Zheng {
3512433bea5SQu Wenruo 	struct reloc_control *rc = fs_info->reloc_ctl;
3525d4f98a2SYan Zheng 	struct rb_node *rb_node;
3535d4f98a2SYan Zheng 	struct mapping_node *node;
3545d4f98a2SYan Zheng 	struct btrfs_root *root = NULL;
3555d4f98a2SYan Zheng 
3562433bea5SQu Wenruo 	ASSERT(rc);
3575d4f98a2SYan Zheng 	spin_lock(&rc->reloc_root_tree.lock);
358e9a28dc5SQu Wenruo 	rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root, bytenr);
3595d4f98a2SYan Zheng 	if (rb_node) {
3605d4f98a2SYan Zheng 		node = rb_entry(rb_node, struct mapping_node, rb_node);
3615d4f98a2SYan Zheng 		root = (struct btrfs_root *)node->data;
3625d4f98a2SYan Zheng 	}
3635d4f98a2SYan Zheng 	spin_unlock(&rc->reloc_root_tree.lock);
36400246528SJosef Bacik 	return btrfs_grab_root(root);
3655d4f98a2SYan Zheng }
3665d4f98a2SYan Zheng 
3675d4f98a2SYan Zheng /*
36829db137bSQu Wenruo  * For useless nodes, do two major clean ups:
36929db137bSQu Wenruo  *
37029db137bSQu Wenruo  * - Cleanup the children edges and nodes
37129db137bSQu Wenruo  *   If child node is also orphan (no parent) during cleanup, then the child
37229db137bSQu Wenruo  *   node will also be cleaned up.
37329db137bSQu Wenruo  *
37429db137bSQu Wenruo  * - Freeing up leaves (level 0), keeps nodes detached
37529db137bSQu Wenruo  *   For nodes, the node is still cached as "detached"
37629db137bSQu Wenruo  *
37729db137bSQu Wenruo  * Return false if @node is not in the @useless_nodes list.
37829db137bSQu Wenruo  * Return true if @node is in the @useless_nodes list.
37929db137bSQu Wenruo  */
38029db137bSQu Wenruo static bool handle_useless_nodes(struct reloc_control *rc,
381a26195a5SQu Wenruo 				 struct btrfs_backref_node *node)
38229db137bSQu Wenruo {
383a26195a5SQu Wenruo 	struct btrfs_backref_cache *cache = &rc->backref_cache;
38429db137bSQu Wenruo 	struct list_head *useless_node = &cache->useless_node;
38529db137bSQu Wenruo 	bool ret = false;
38629db137bSQu Wenruo 
38729db137bSQu Wenruo 	while (!list_empty(useless_node)) {
388a26195a5SQu Wenruo 		struct btrfs_backref_node *cur;
38929db137bSQu Wenruo 
390a26195a5SQu Wenruo 		cur = list_first_entry(useless_node, struct btrfs_backref_node,
39129db137bSQu Wenruo 				 list);
39229db137bSQu Wenruo 		list_del_init(&cur->list);
39329db137bSQu Wenruo 
39429db137bSQu Wenruo 		/* Only tree root nodes can be added to @useless_nodes */
39529db137bSQu Wenruo 		ASSERT(list_empty(&cur->upper));
39629db137bSQu Wenruo 
39729db137bSQu Wenruo 		if (cur == node)
39829db137bSQu Wenruo 			ret = true;
39929db137bSQu Wenruo 
40029db137bSQu Wenruo 		/* The node is the lowest node */
40129db137bSQu Wenruo 		if (cur->lowest) {
40229db137bSQu Wenruo 			list_del_init(&cur->lower);
40329db137bSQu Wenruo 			cur->lowest = 0;
40429db137bSQu Wenruo 		}
40529db137bSQu Wenruo 
40629db137bSQu Wenruo 		/* Cleanup the lower edges */
40729db137bSQu Wenruo 		while (!list_empty(&cur->lower)) {
408a26195a5SQu Wenruo 			struct btrfs_backref_edge *edge;
409a26195a5SQu Wenruo 			struct btrfs_backref_node *lower;
41029db137bSQu Wenruo 
41129db137bSQu Wenruo 			edge = list_entry(cur->lower.next,
412a26195a5SQu Wenruo 					struct btrfs_backref_edge, list[UPPER]);
41329db137bSQu Wenruo 			list_del(&edge->list[UPPER]);
41429db137bSQu Wenruo 			list_del(&edge->list[LOWER]);
41529db137bSQu Wenruo 			lower = edge->node[LOWER];
416741188d3SQu Wenruo 			btrfs_backref_free_edge(cache, edge);
41729db137bSQu Wenruo 
41829db137bSQu Wenruo 			/* Child node is also orphan, queue for cleanup */
41929db137bSQu Wenruo 			if (list_empty(&lower->upper))
42029db137bSQu Wenruo 				list_add(&lower->list, useless_node);
42129db137bSQu Wenruo 		}
42229db137bSQu Wenruo 		/* Mark this block processed for relocation */
42329db137bSQu Wenruo 		mark_block_processed(rc, cur);
42429db137bSQu Wenruo 
42529db137bSQu Wenruo 		/*
42629db137bSQu Wenruo 		 * Backref nodes for tree leaves are deleted from the cache.
42729db137bSQu Wenruo 		 * Backref nodes for upper level tree blocks are left in the
42829db137bSQu Wenruo 		 * cache to avoid unnecessary backref lookup.
42929db137bSQu Wenruo 		 */
43029db137bSQu Wenruo 		if (cur->level > 0) {
43129db137bSQu Wenruo 			list_add(&cur->list, &cache->detached);
43229db137bSQu Wenruo 			cur->detached = 1;
43329db137bSQu Wenruo 		} else {
43429db137bSQu Wenruo 			rb_erase(&cur->rb_node, &cache->rb_root);
435741188d3SQu Wenruo 			btrfs_backref_free_node(cache, cur);
43629db137bSQu Wenruo 		}
43729db137bSQu Wenruo 	}
43829db137bSQu Wenruo 	return ret;
43929db137bSQu Wenruo }
44029db137bSQu Wenruo 
44129db137bSQu Wenruo /*
442e7d571c7SQu Wenruo  * Build backref tree for a given tree block. Root of the backref tree
443e7d571c7SQu Wenruo  * corresponds the tree block, leaves of the backref tree correspond roots of
444e7d571c7SQu Wenruo  * b-trees that reference the tree block.
445e7d571c7SQu Wenruo  *
446e7d571c7SQu Wenruo  * The basic idea of this function is check backrefs of a given block to find
447e7d571c7SQu Wenruo  * upper level blocks that reference the block, and then check backrefs of
448e7d571c7SQu Wenruo  * these upper level blocks recursively. The recursion stops when tree root is
449e7d571c7SQu Wenruo  * reached or backrefs for the block is cached.
450e7d571c7SQu Wenruo  *
451e7d571c7SQu Wenruo  * NOTE: if we find that backrefs for a block are cached, we know backrefs for
452e7d571c7SQu Wenruo  * all upper level blocks that directly/indirectly reference the block are also
453e7d571c7SQu Wenruo  * cached.
454e7d571c7SQu Wenruo  */
455a26195a5SQu Wenruo static noinline_for_stack struct btrfs_backref_node *build_backref_tree(
456e7d571c7SQu Wenruo 			struct reloc_control *rc, struct btrfs_key *node_key,
457e7d571c7SQu Wenruo 			int level, u64 bytenr)
458e7d571c7SQu Wenruo {
459e7d571c7SQu Wenruo 	struct btrfs_backref_iter *iter;
460a26195a5SQu Wenruo 	struct btrfs_backref_cache *cache = &rc->backref_cache;
461e7d571c7SQu Wenruo 	/* For searching parent of TREE_BLOCK_REF */
462e7d571c7SQu Wenruo 	struct btrfs_path *path;
463a26195a5SQu Wenruo 	struct btrfs_backref_node *cur;
464a26195a5SQu Wenruo 	struct btrfs_backref_node *node = NULL;
465a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
466e7d571c7SQu Wenruo 	int ret;
467e7d571c7SQu Wenruo 	int err = 0;
468e7d571c7SQu Wenruo 
469e7d571c7SQu Wenruo 	iter = btrfs_backref_iter_alloc(rc->extent_root->fs_info, GFP_NOFS);
470e7d571c7SQu Wenruo 	if (!iter)
471e7d571c7SQu Wenruo 		return ERR_PTR(-ENOMEM);
472e7d571c7SQu Wenruo 	path = btrfs_alloc_path();
473e7d571c7SQu Wenruo 	if (!path) {
474e7d571c7SQu Wenruo 		err = -ENOMEM;
475e7d571c7SQu Wenruo 		goto out;
476e7d571c7SQu Wenruo 	}
477e7d571c7SQu Wenruo 
478b1818dabSQu Wenruo 	node = btrfs_backref_alloc_node(cache, bytenr, level);
479e7d571c7SQu Wenruo 	if (!node) {
480e7d571c7SQu Wenruo 		err = -ENOMEM;
481e7d571c7SQu Wenruo 		goto out;
482e7d571c7SQu Wenruo 	}
483e7d571c7SQu Wenruo 
484e7d571c7SQu Wenruo 	node->lowest = 1;
485e7d571c7SQu Wenruo 	cur = node;
486e7d571c7SQu Wenruo 
487e7d571c7SQu Wenruo 	/* Breadth-first search to build backref cache */
488e7d571c7SQu Wenruo 	do {
4891b60d2ecSQu Wenruo 		ret = btrfs_backref_add_tree_node(cache, path, iter, node_key,
4901b60d2ecSQu Wenruo 						  cur);
491e7d571c7SQu Wenruo 		if (ret < 0) {
492e7d571c7SQu Wenruo 			err = ret;
493e7d571c7SQu Wenruo 			goto out;
494e7d571c7SQu Wenruo 		}
495e7d571c7SQu Wenruo 		edge = list_first_entry_or_null(&cache->pending_edge,
496a26195a5SQu Wenruo 				struct btrfs_backref_edge, list[UPPER]);
497e7d571c7SQu Wenruo 		/*
498e7d571c7SQu Wenruo 		 * The pending list isn't empty, take the first block to
499e7d571c7SQu Wenruo 		 * process
500e7d571c7SQu Wenruo 		 */
501e7d571c7SQu Wenruo 		if (edge) {
5025d4f98a2SYan Zheng 			list_del_init(&edge->list[UPPER]);
5035d4f98a2SYan Zheng 			cur = edge->node[UPPER];
5045d4f98a2SYan Zheng 		}
505e7d571c7SQu Wenruo 	} while (edge);
5065d4f98a2SYan Zheng 
5071f872924SQu Wenruo 	/* Finish the upper linkage of newly added edges/nodes */
508fc997ed0SQu Wenruo 	ret = btrfs_backref_finish_upper_links(cache, node);
5091f872924SQu Wenruo 	if (ret < 0) {
5101f872924SQu Wenruo 		err = ret;
51175bfb9afSJosef Bacik 		goto out;
51275bfb9afSJosef Bacik 	}
51375bfb9afSJosef Bacik 
51429db137bSQu Wenruo 	if (handle_useless_nodes(rc, node))
5153fd0a558SYan, Zheng 		node = NULL;
5165d4f98a2SYan Zheng out:
51771f572a9SQu Wenruo 	btrfs_backref_iter_free(iter);
51871f572a9SQu Wenruo 	btrfs_free_path(path);
5195d4f98a2SYan Zheng 	if (err) {
5201b23ea18SQu Wenruo 		btrfs_backref_error_cleanup(cache, node);
5215d4f98a2SYan Zheng 		return ERR_PTR(err);
5225d4f98a2SYan Zheng 	}
52375bfb9afSJosef Bacik 	ASSERT(!node || !node->detached);
52484780289SQu Wenruo 	ASSERT(list_empty(&cache->useless_node) &&
52584780289SQu Wenruo 	       list_empty(&cache->pending_edge));
5265d4f98a2SYan Zheng 	return node;
5275d4f98a2SYan Zheng }
5285d4f98a2SYan Zheng 
5295d4f98a2SYan Zheng /*
5303fd0a558SYan, Zheng  * helper to add backref node for the newly created snapshot.
5313fd0a558SYan, Zheng  * the backref node is created by cloning backref node that
5323fd0a558SYan, Zheng  * corresponds to root of source tree
5333fd0a558SYan, Zheng  */
5343fd0a558SYan, Zheng static int clone_backref_node(struct btrfs_trans_handle *trans,
5353fd0a558SYan, Zheng 			      struct reloc_control *rc,
5363fd0a558SYan, Zheng 			      struct btrfs_root *src,
5373fd0a558SYan, Zheng 			      struct btrfs_root *dest)
5383fd0a558SYan, Zheng {
5393fd0a558SYan, Zheng 	struct btrfs_root *reloc_root = src->reloc_root;
540a26195a5SQu Wenruo 	struct btrfs_backref_cache *cache = &rc->backref_cache;
541a26195a5SQu Wenruo 	struct btrfs_backref_node *node = NULL;
542a26195a5SQu Wenruo 	struct btrfs_backref_node *new_node;
543a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
544a26195a5SQu Wenruo 	struct btrfs_backref_edge *new_edge;
5453fd0a558SYan, Zheng 	struct rb_node *rb_node;
5463fd0a558SYan, Zheng 
5473fd0a558SYan, Zheng 	if (cache->last_trans > 0)
5483fd0a558SYan, Zheng 		update_backref_cache(trans, cache);
5493fd0a558SYan, Zheng 
550e9a28dc5SQu Wenruo 	rb_node = rb_simple_search(&cache->rb_root, src->commit_root->start);
5513fd0a558SYan, Zheng 	if (rb_node) {
552a26195a5SQu Wenruo 		node = rb_entry(rb_node, struct btrfs_backref_node, rb_node);
5533fd0a558SYan, Zheng 		if (node->detached)
5543fd0a558SYan, Zheng 			node = NULL;
5553fd0a558SYan, Zheng 		else
5563fd0a558SYan, Zheng 			BUG_ON(node->new_bytenr != reloc_root->node->start);
5573fd0a558SYan, Zheng 	}
5583fd0a558SYan, Zheng 
5593fd0a558SYan, Zheng 	if (!node) {
560e9a28dc5SQu Wenruo 		rb_node = rb_simple_search(&cache->rb_root,
5613fd0a558SYan, Zheng 					   reloc_root->commit_root->start);
5623fd0a558SYan, Zheng 		if (rb_node) {
563a26195a5SQu Wenruo 			node = rb_entry(rb_node, struct btrfs_backref_node,
5643fd0a558SYan, Zheng 					rb_node);
5653fd0a558SYan, Zheng 			BUG_ON(node->detached);
5663fd0a558SYan, Zheng 		}
5673fd0a558SYan, Zheng 	}
5683fd0a558SYan, Zheng 
5693fd0a558SYan, Zheng 	if (!node)
5703fd0a558SYan, Zheng 		return 0;
5713fd0a558SYan, Zheng 
572b1818dabSQu Wenruo 	new_node = btrfs_backref_alloc_node(cache, dest->node->start,
573b1818dabSQu Wenruo 					    node->level);
5743fd0a558SYan, Zheng 	if (!new_node)
5753fd0a558SYan, Zheng 		return -ENOMEM;
5763fd0a558SYan, Zheng 
5773fd0a558SYan, Zheng 	new_node->lowest = node->lowest;
5786848ad64SYan, Zheng 	new_node->checked = 1;
57900246528SJosef Bacik 	new_node->root = btrfs_grab_root(dest);
5800b530bc5SJosef Bacik 	ASSERT(new_node->root);
5813fd0a558SYan, Zheng 
5823fd0a558SYan, Zheng 	if (!node->lowest) {
5833fd0a558SYan, Zheng 		list_for_each_entry(edge, &node->lower, list[UPPER]) {
58447254d07SQu Wenruo 			new_edge = btrfs_backref_alloc_edge(cache);
5853fd0a558SYan, Zheng 			if (!new_edge)
5863fd0a558SYan, Zheng 				goto fail;
5873fd0a558SYan, Zheng 
588f39911e5SQu Wenruo 			btrfs_backref_link_edge(new_edge, edge->node[LOWER],
589f39911e5SQu Wenruo 						new_node, LINK_UPPER);
5903fd0a558SYan, Zheng 		}
59176b9e23dSMiao Xie 	} else {
59276b9e23dSMiao Xie 		list_add_tail(&new_node->lower, &cache->leaves);
5933fd0a558SYan, Zheng 	}
5943fd0a558SYan, Zheng 
595e9a28dc5SQu Wenruo 	rb_node = rb_simple_insert(&cache->rb_root, new_node->bytenr,
5963fd0a558SYan, Zheng 				   &new_node->rb_node);
59743c04fb1SJeff Mahoney 	if (rb_node)
598982c92cbSQu Wenruo 		btrfs_backref_panic(trans->fs_info, new_node->bytenr, -EEXIST);
5993fd0a558SYan, Zheng 
6003fd0a558SYan, Zheng 	if (!new_node->lowest) {
6013fd0a558SYan, Zheng 		list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) {
6023fd0a558SYan, Zheng 			list_add_tail(&new_edge->list[LOWER],
6033fd0a558SYan, Zheng 				      &new_edge->node[LOWER]->upper);
6043fd0a558SYan, Zheng 		}
6053fd0a558SYan, Zheng 	}
6063fd0a558SYan, Zheng 	return 0;
6073fd0a558SYan, Zheng fail:
6083fd0a558SYan, Zheng 	while (!list_empty(&new_node->lower)) {
6093fd0a558SYan, Zheng 		new_edge = list_entry(new_node->lower.next,
610a26195a5SQu Wenruo 				      struct btrfs_backref_edge, list[UPPER]);
6113fd0a558SYan, Zheng 		list_del(&new_edge->list[UPPER]);
612741188d3SQu Wenruo 		btrfs_backref_free_edge(cache, new_edge);
6133fd0a558SYan, Zheng 	}
614741188d3SQu Wenruo 	btrfs_backref_free_node(cache, new_node);
6153fd0a558SYan, Zheng 	return -ENOMEM;
6163fd0a558SYan, Zheng }
6173fd0a558SYan, Zheng 
6183fd0a558SYan, Zheng /*
6195d4f98a2SYan Zheng  * helper to add 'address of tree root -> reloc tree' mapping
6205d4f98a2SYan Zheng  */
621ffd7b339SJeff Mahoney static int __must_check __add_reloc_root(struct btrfs_root *root)
6225d4f98a2SYan Zheng {
6230b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
6245d4f98a2SYan Zheng 	struct rb_node *rb_node;
6255d4f98a2SYan Zheng 	struct mapping_node *node;
6260b246afaSJeff Mahoney 	struct reloc_control *rc = fs_info->reloc_ctl;
6275d4f98a2SYan Zheng 
6285d4f98a2SYan Zheng 	node = kmalloc(sizeof(*node), GFP_NOFS);
629ffd7b339SJeff Mahoney 	if (!node)
630ffd7b339SJeff Mahoney 		return -ENOMEM;
6315d4f98a2SYan Zheng 
632ea287ab1SJosef Bacik 	node->bytenr = root->commit_root->start;
6335d4f98a2SYan Zheng 	node->data = root;
6345d4f98a2SYan Zheng 
6355d4f98a2SYan Zheng 	spin_lock(&rc->reloc_root_tree.lock);
636e9a28dc5SQu Wenruo 	rb_node = rb_simple_insert(&rc->reloc_root_tree.rb_root,
6375d4f98a2SYan Zheng 				   node->bytenr, &node->rb_node);
6385d4f98a2SYan Zheng 	spin_unlock(&rc->reloc_root_tree.lock);
639ffd7b339SJeff Mahoney 	if (rb_node) {
6400b246afaSJeff Mahoney 		btrfs_panic(fs_info, -EEXIST,
6415d163e0eSJeff Mahoney 			    "Duplicate root found for start=%llu while inserting into relocation tree",
6425d163e0eSJeff Mahoney 			    node->bytenr);
643ffd7b339SJeff Mahoney 	}
6445d4f98a2SYan Zheng 
6455d4f98a2SYan Zheng 	list_add_tail(&root->root_list, &rc->reloc_roots);
6465d4f98a2SYan Zheng 	return 0;
6475d4f98a2SYan Zheng }
6485d4f98a2SYan Zheng 
6495d4f98a2SYan Zheng /*
650c974c464SWang Shilong  * helper to delete the 'address of tree root -> reloc tree'
6515d4f98a2SYan Zheng  * mapping
6525d4f98a2SYan Zheng  */
653c974c464SWang Shilong static void __del_reloc_root(struct btrfs_root *root)
6545d4f98a2SYan Zheng {
6550b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
6565d4f98a2SYan Zheng 	struct rb_node *rb_node;
6575d4f98a2SYan Zheng 	struct mapping_node *node = NULL;
6580b246afaSJeff Mahoney 	struct reloc_control *rc = fs_info->reloc_ctl;
659f44deb74SJosef Bacik 	bool put_ref = false;
6605d4f98a2SYan Zheng 
66165c6e82bSQu Wenruo 	if (rc && root->node) {
6625d4f98a2SYan Zheng 		spin_lock(&rc->reloc_root_tree.lock);
663e9a28dc5SQu Wenruo 		rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root,
664ea287ab1SJosef Bacik 					   root->commit_root->start);
665c974c464SWang Shilong 		if (rb_node) {
666c974c464SWang Shilong 			node = rb_entry(rb_node, struct mapping_node, rb_node);
667c974c464SWang Shilong 			rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
668ea287ab1SJosef Bacik 			RB_CLEAR_NODE(&node->rb_node);
669c974c464SWang Shilong 		}
670c974c464SWang Shilong 		spin_unlock(&rc->reloc_root_tree.lock);
671c974c464SWang Shilong 		if (!node)
672c974c464SWang Shilong 			return;
673c974c464SWang Shilong 		BUG_ON((struct btrfs_root *)node->data != root);
674389305b2SQu Wenruo 	}
675c974c464SWang Shilong 
676f44deb74SJosef Bacik 	/*
677f44deb74SJosef Bacik 	 * We only put the reloc root here if it's on the list.  There's a lot
678f44deb74SJosef Bacik 	 * of places where the pattern is to splice the rc->reloc_roots, process
679f44deb74SJosef Bacik 	 * the reloc roots, and then add the reloc root back onto
680f44deb74SJosef Bacik 	 * rc->reloc_roots.  If we call __del_reloc_root while it's off of the
681f44deb74SJosef Bacik 	 * list we don't want the reference being dropped, because the guy
682f44deb74SJosef Bacik 	 * messing with the list is in charge of the reference.
683f44deb74SJosef Bacik 	 */
6840b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
685f44deb74SJosef Bacik 	if (!list_empty(&root->root_list)) {
686f44deb74SJosef Bacik 		put_ref = true;
687c974c464SWang Shilong 		list_del_init(&root->root_list);
688f44deb74SJosef Bacik 	}
6890b246afaSJeff Mahoney 	spin_unlock(&fs_info->trans_lock);
690f44deb74SJosef Bacik 	if (put_ref)
691f44deb74SJosef Bacik 		btrfs_put_root(root);
692c974c464SWang Shilong 	kfree(node);
693c974c464SWang Shilong }
694c974c464SWang Shilong 
695c974c464SWang Shilong /*
696c974c464SWang Shilong  * helper to update the 'address of tree root -> reloc tree'
697c974c464SWang Shilong  * mapping
698c974c464SWang Shilong  */
699ea287ab1SJosef Bacik static int __update_reloc_root(struct btrfs_root *root)
700c974c464SWang Shilong {
7010b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
702c974c464SWang Shilong 	struct rb_node *rb_node;
703c974c464SWang Shilong 	struct mapping_node *node = NULL;
7040b246afaSJeff Mahoney 	struct reloc_control *rc = fs_info->reloc_ctl;
705c974c464SWang Shilong 
706c974c464SWang Shilong 	spin_lock(&rc->reloc_root_tree.lock);
707e9a28dc5SQu Wenruo 	rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root,
708ea287ab1SJosef Bacik 				   root->commit_root->start);
7095d4f98a2SYan Zheng 	if (rb_node) {
7105d4f98a2SYan Zheng 		node = rb_entry(rb_node, struct mapping_node, rb_node);
7115d4f98a2SYan Zheng 		rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
7125d4f98a2SYan Zheng 	}
7135d4f98a2SYan Zheng 	spin_unlock(&rc->reloc_root_tree.lock);
7145d4f98a2SYan Zheng 
7158f71f3e0SLiu Bo 	if (!node)
7168f71f3e0SLiu Bo 		return 0;
7175d4f98a2SYan Zheng 	BUG_ON((struct btrfs_root *)node->data != root);
7185d4f98a2SYan Zheng 
7195d4f98a2SYan Zheng 	spin_lock(&rc->reloc_root_tree.lock);
720ea287ab1SJosef Bacik 	node->bytenr = root->node->start;
721e9a28dc5SQu Wenruo 	rb_node = rb_simple_insert(&rc->reloc_root_tree.rb_root,
7225d4f98a2SYan Zheng 				   node->bytenr, &node->rb_node);
7235d4f98a2SYan Zheng 	spin_unlock(&rc->reloc_root_tree.lock);
72443c04fb1SJeff Mahoney 	if (rb_node)
725982c92cbSQu Wenruo 		btrfs_backref_panic(fs_info, node->bytenr, -EEXIST);
7265d4f98a2SYan Zheng 	return 0;
7275d4f98a2SYan Zheng }
7285d4f98a2SYan Zheng 
7293fd0a558SYan, Zheng static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
7303fd0a558SYan, Zheng 					struct btrfs_root *root, u64 objectid)
7315d4f98a2SYan Zheng {
7320b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
7335d4f98a2SYan Zheng 	struct btrfs_root *reloc_root;
7345d4f98a2SYan Zheng 	struct extent_buffer *eb;
7355d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
7365d4f98a2SYan Zheng 	struct btrfs_key root_key;
7375d4f98a2SYan Zheng 	int ret;
7385d4f98a2SYan Zheng 
7395d4f98a2SYan Zheng 	root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
7405d4f98a2SYan Zheng 	BUG_ON(!root_item);
7415d4f98a2SYan Zheng 
7425d4f98a2SYan Zheng 	root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
7435d4f98a2SYan Zheng 	root_key.type = BTRFS_ROOT_ITEM_KEY;
7443fd0a558SYan, Zheng 	root_key.offset = objectid;
7455d4f98a2SYan Zheng 
7463fd0a558SYan, Zheng 	if (root->root_key.objectid == objectid) {
747054570a1SFilipe Manana 		u64 commit_root_gen;
748054570a1SFilipe Manana 
7493fd0a558SYan, Zheng 		/* called by btrfs_init_reloc_root */
7505d4f98a2SYan Zheng 		ret = btrfs_copy_root(trans, root, root->commit_root, &eb,
7515d4f98a2SYan Zheng 				      BTRFS_TREE_RELOC_OBJECTID);
7525d4f98a2SYan Zheng 		BUG_ON(ret);
753054570a1SFilipe Manana 		/*
754054570a1SFilipe Manana 		 * Set the last_snapshot field to the generation of the commit
755054570a1SFilipe Manana 		 * root - like this ctree.c:btrfs_block_can_be_shared() behaves
756054570a1SFilipe Manana 		 * correctly (returns true) when the relocation root is created
757054570a1SFilipe Manana 		 * either inside the critical section of a transaction commit
758054570a1SFilipe Manana 		 * (through transaction.c:qgroup_account_snapshot()) and when
759054570a1SFilipe Manana 		 * it's created before the transaction commit is started.
760054570a1SFilipe Manana 		 */
761054570a1SFilipe Manana 		commit_root_gen = btrfs_header_generation(root->commit_root);
762054570a1SFilipe Manana 		btrfs_set_root_last_snapshot(&root->root_item, commit_root_gen);
7633fd0a558SYan, Zheng 	} else {
7643fd0a558SYan, Zheng 		/*
7653fd0a558SYan, Zheng 		 * called by btrfs_reloc_post_snapshot_hook.
7663fd0a558SYan, Zheng 		 * the source tree is a reloc tree, all tree blocks
7673fd0a558SYan, Zheng 		 * modified after it was created have RELOC flag
7683fd0a558SYan, Zheng 		 * set in their headers. so it's OK to not update
7693fd0a558SYan, Zheng 		 * the 'last_snapshot'.
7703fd0a558SYan, Zheng 		 */
7713fd0a558SYan, Zheng 		ret = btrfs_copy_root(trans, root, root->node, &eb,
7723fd0a558SYan, Zheng 				      BTRFS_TREE_RELOC_OBJECTID);
7733fd0a558SYan, Zheng 		BUG_ON(ret);
7743fd0a558SYan, Zheng 	}
7753fd0a558SYan, Zheng 
7765d4f98a2SYan Zheng 	memcpy(root_item, &root->root_item, sizeof(*root_item));
7775d4f98a2SYan Zheng 	btrfs_set_root_bytenr(root_item, eb->start);
7785d4f98a2SYan Zheng 	btrfs_set_root_level(root_item, btrfs_header_level(eb));
7795d4f98a2SYan Zheng 	btrfs_set_root_generation(root_item, trans->transid);
7803fd0a558SYan, Zheng 
7813fd0a558SYan, Zheng 	if (root->root_key.objectid == objectid) {
7823fd0a558SYan, Zheng 		btrfs_set_root_refs(root_item, 0);
7833fd0a558SYan, Zheng 		memset(&root_item->drop_progress, 0,
7843fd0a558SYan, Zheng 		       sizeof(struct btrfs_disk_key));
785c8422684SDavid Sterba 		btrfs_set_root_drop_level(root_item, 0);
7863fd0a558SYan, Zheng 	}
7875d4f98a2SYan Zheng 
7885d4f98a2SYan Zheng 	btrfs_tree_unlock(eb);
7895d4f98a2SYan Zheng 	free_extent_buffer(eb);
7905d4f98a2SYan Zheng 
7910b246afaSJeff Mahoney 	ret = btrfs_insert_root(trans, fs_info->tree_root,
7925d4f98a2SYan Zheng 				&root_key, root_item);
7935d4f98a2SYan Zheng 	BUG_ON(ret);
7945d4f98a2SYan Zheng 	kfree(root_item);
7955d4f98a2SYan Zheng 
7963dbf1738SJosef Bacik 	reloc_root = btrfs_read_tree_root(fs_info->tree_root, &root_key);
7975d4f98a2SYan Zheng 	BUG_ON(IS_ERR(reloc_root));
79892a7cc42SQu Wenruo 	set_bit(BTRFS_ROOT_SHAREABLE, &reloc_root->state);
7995d4f98a2SYan Zheng 	reloc_root->last_trans = trans->transid;
8003fd0a558SYan, Zheng 	return reloc_root;
8013fd0a558SYan, Zheng }
8023fd0a558SYan, Zheng 
8033fd0a558SYan, Zheng /*
8043fd0a558SYan, Zheng  * create reloc tree for a given fs tree. reloc tree is just a
8053fd0a558SYan, Zheng  * snapshot of the fs tree with special root objectid.
806f44deb74SJosef Bacik  *
807f44deb74SJosef Bacik  * The reloc_root comes out of here with two references, one for
808f44deb74SJosef Bacik  * root->reloc_root, and another for being on the rc->reloc_roots list.
8093fd0a558SYan, Zheng  */
8103fd0a558SYan, Zheng int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
8113fd0a558SYan, Zheng 			  struct btrfs_root *root)
8123fd0a558SYan, Zheng {
8130b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
8143fd0a558SYan, Zheng 	struct btrfs_root *reloc_root;
8150b246afaSJeff Mahoney 	struct reloc_control *rc = fs_info->reloc_ctl;
81620dd2cbfSMiao Xie 	struct btrfs_block_rsv *rsv;
8173fd0a558SYan, Zheng 	int clear_rsv = 0;
818ffd7b339SJeff Mahoney 	int ret;
8193fd0a558SYan, Zheng 
820aec7db3bSJosef Bacik 	if (!rc)
8212abc726aSJosef Bacik 		return 0;
8222abc726aSJosef Bacik 
8231fac4a54SQu Wenruo 	/*
8241fac4a54SQu Wenruo 	 * The subvolume has reloc tree but the swap is finished, no need to
8251fac4a54SQu Wenruo 	 * create/update the dead reloc tree
8261fac4a54SQu Wenruo 	 */
8276282675eSQu Wenruo 	if (reloc_root_is_dead(root))
8281fac4a54SQu Wenruo 		return 0;
8291fac4a54SQu Wenruo 
830aec7db3bSJosef Bacik 	/*
831aec7db3bSJosef Bacik 	 * This is subtle but important.  We do not do
832aec7db3bSJosef Bacik 	 * record_root_in_transaction for reloc roots, instead we record their
833aec7db3bSJosef Bacik 	 * corresponding fs root, and then here we update the last trans for the
834aec7db3bSJosef Bacik 	 * reloc root.  This means that we have to do this for the entire life
835aec7db3bSJosef Bacik 	 * of the reloc root, regardless of which stage of the relocation we are
836aec7db3bSJosef Bacik 	 * in.
837aec7db3bSJosef Bacik 	 */
8383fd0a558SYan, Zheng 	if (root->reloc_root) {
8393fd0a558SYan, Zheng 		reloc_root = root->reloc_root;
8403fd0a558SYan, Zheng 		reloc_root->last_trans = trans->transid;
8413fd0a558SYan, Zheng 		return 0;
8423fd0a558SYan, Zheng 	}
8433fd0a558SYan, Zheng 
844aec7db3bSJosef Bacik 	/*
845aec7db3bSJosef Bacik 	 * We are merging reloc roots, we do not need new reloc trees.  Also
846aec7db3bSJosef Bacik 	 * reloc trees never need their own reloc tree.
847aec7db3bSJosef Bacik 	 */
848aec7db3bSJosef Bacik 	if (!rc->create_reloc_tree ||
849aec7db3bSJosef Bacik 	    root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
850aec7db3bSJosef Bacik 		return 0;
851aec7db3bSJosef Bacik 
85220dd2cbfSMiao Xie 	if (!trans->reloc_reserved) {
85320dd2cbfSMiao Xie 		rsv = trans->block_rsv;
8543fd0a558SYan, Zheng 		trans->block_rsv = rc->block_rsv;
8553fd0a558SYan, Zheng 		clear_rsv = 1;
8563fd0a558SYan, Zheng 	}
8573fd0a558SYan, Zheng 	reloc_root = create_reloc_root(trans, root, root->root_key.objectid);
8583fd0a558SYan, Zheng 	if (clear_rsv)
85920dd2cbfSMiao Xie 		trans->block_rsv = rsv;
8605d4f98a2SYan Zheng 
861ffd7b339SJeff Mahoney 	ret = __add_reloc_root(reloc_root);
862ffd7b339SJeff Mahoney 	BUG_ON(ret < 0);
863f44deb74SJosef Bacik 	root->reloc_root = btrfs_grab_root(reloc_root);
8645d4f98a2SYan Zheng 	return 0;
8655d4f98a2SYan Zheng }
8665d4f98a2SYan Zheng 
8675d4f98a2SYan Zheng /*
8685d4f98a2SYan Zheng  * update root item of reloc tree
8695d4f98a2SYan Zheng  */
8705d4f98a2SYan Zheng int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
8715d4f98a2SYan Zheng 			    struct btrfs_root *root)
8725d4f98a2SYan Zheng {
8730b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
8745d4f98a2SYan Zheng 	struct btrfs_root *reloc_root;
8755d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
8765d4f98a2SYan Zheng 	int ret;
8775d4f98a2SYan Zheng 
8786282675eSQu Wenruo 	if (!have_reloc_root(root))
8797585717fSChris Mason 		goto out;
8805d4f98a2SYan Zheng 
8815d4f98a2SYan Zheng 	reloc_root = root->reloc_root;
8825d4f98a2SYan Zheng 	root_item = &reloc_root->root_item;
8835d4f98a2SYan Zheng 
884f44deb74SJosef Bacik 	/*
885f44deb74SJosef Bacik 	 * We are probably ok here, but __del_reloc_root() will drop its ref of
886f44deb74SJosef Bacik 	 * the root.  We have the ref for root->reloc_root, but just in case
887f44deb74SJosef Bacik 	 * hold it while we update the reloc root.
888f44deb74SJosef Bacik 	 */
889f44deb74SJosef Bacik 	btrfs_grab_root(reloc_root);
890f44deb74SJosef Bacik 
891d2311e69SQu Wenruo 	/* root->reloc_root will stay until current relocation finished */
8920b246afaSJeff Mahoney 	if (fs_info->reloc_ctl->merge_reloc_tree &&
8933fd0a558SYan, Zheng 	    btrfs_root_refs(root_item) == 0) {
894d2311e69SQu Wenruo 		set_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
8956282675eSQu Wenruo 		/*
8966282675eSQu Wenruo 		 * Mark the tree as dead before we change reloc_root so
8976282675eSQu Wenruo 		 * have_reloc_root will not touch it from now on.
8986282675eSQu Wenruo 		 */
8996282675eSQu Wenruo 		smp_wmb();
900c974c464SWang Shilong 		__del_reloc_root(reloc_root);
9015d4f98a2SYan Zheng 	}
9025d4f98a2SYan Zheng 
9035d4f98a2SYan Zheng 	if (reloc_root->commit_root != reloc_root->node) {
904ea287ab1SJosef Bacik 		__update_reloc_root(reloc_root);
9055d4f98a2SYan Zheng 		btrfs_set_root_node(root_item, reloc_root->node);
9065d4f98a2SYan Zheng 		free_extent_buffer(reloc_root->commit_root);
9075d4f98a2SYan Zheng 		reloc_root->commit_root = btrfs_root_node(reloc_root);
9085d4f98a2SYan Zheng 	}
9095d4f98a2SYan Zheng 
9100b246afaSJeff Mahoney 	ret = btrfs_update_root(trans, fs_info->tree_root,
9115d4f98a2SYan Zheng 				&reloc_root->root_key, root_item);
9125d4f98a2SYan Zheng 	BUG_ON(ret);
913f44deb74SJosef Bacik 	btrfs_put_root(reloc_root);
9147585717fSChris Mason out:
9155d4f98a2SYan Zheng 	return 0;
9165d4f98a2SYan Zheng }
9175d4f98a2SYan Zheng 
9185d4f98a2SYan Zheng /*
9195d4f98a2SYan Zheng  * helper to find first cached inode with inode number >= objectid
9205d4f98a2SYan Zheng  * in a subvolume
9215d4f98a2SYan Zheng  */
9225d4f98a2SYan Zheng static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid)
9235d4f98a2SYan Zheng {
9245d4f98a2SYan Zheng 	struct rb_node *node;
9255d4f98a2SYan Zheng 	struct rb_node *prev;
9265d4f98a2SYan Zheng 	struct btrfs_inode *entry;
9275d4f98a2SYan Zheng 	struct inode *inode;
9285d4f98a2SYan Zheng 
9295d4f98a2SYan Zheng 	spin_lock(&root->inode_lock);
9305d4f98a2SYan Zheng again:
9315d4f98a2SYan Zheng 	node = root->inode_tree.rb_node;
9325d4f98a2SYan Zheng 	prev = NULL;
9335d4f98a2SYan Zheng 	while (node) {
9345d4f98a2SYan Zheng 		prev = node;
9355d4f98a2SYan Zheng 		entry = rb_entry(node, struct btrfs_inode, rb_node);
9365d4f98a2SYan Zheng 
9374a0cc7caSNikolay Borisov 		if (objectid < btrfs_ino(entry))
9385d4f98a2SYan Zheng 			node = node->rb_left;
9394a0cc7caSNikolay Borisov 		else if (objectid > btrfs_ino(entry))
9405d4f98a2SYan Zheng 			node = node->rb_right;
9415d4f98a2SYan Zheng 		else
9425d4f98a2SYan Zheng 			break;
9435d4f98a2SYan Zheng 	}
9445d4f98a2SYan Zheng 	if (!node) {
9455d4f98a2SYan Zheng 		while (prev) {
9465d4f98a2SYan Zheng 			entry = rb_entry(prev, struct btrfs_inode, rb_node);
9474a0cc7caSNikolay Borisov 			if (objectid <= btrfs_ino(entry)) {
9485d4f98a2SYan Zheng 				node = prev;
9495d4f98a2SYan Zheng 				break;
9505d4f98a2SYan Zheng 			}
9515d4f98a2SYan Zheng 			prev = rb_next(prev);
9525d4f98a2SYan Zheng 		}
9535d4f98a2SYan Zheng 	}
9545d4f98a2SYan Zheng 	while (node) {
9555d4f98a2SYan Zheng 		entry = rb_entry(node, struct btrfs_inode, rb_node);
9565d4f98a2SYan Zheng 		inode = igrab(&entry->vfs_inode);
9575d4f98a2SYan Zheng 		if (inode) {
9585d4f98a2SYan Zheng 			spin_unlock(&root->inode_lock);
9595d4f98a2SYan Zheng 			return inode;
9605d4f98a2SYan Zheng 		}
9615d4f98a2SYan Zheng 
9624a0cc7caSNikolay Borisov 		objectid = btrfs_ino(entry) + 1;
9635d4f98a2SYan Zheng 		if (cond_resched_lock(&root->inode_lock))
9645d4f98a2SYan Zheng 			goto again;
9655d4f98a2SYan Zheng 
9665d4f98a2SYan Zheng 		node = rb_next(node);
9675d4f98a2SYan Zheng 	}
9685d4f98a2SYan Zheng 	spin_unlock(&root->inode_lock);
9695d4f98a2SYan Zheng 	return NULL;
9705d4f98a2SYan Zheng }
9715d4f98a2SYan Zheng 
9725d4f98a2SYan Zheng /*
9735d4f98a2SYan Zheng  * get new location of data
9745d4f98a2SYan Zheng  */
9755d4f98a2SYan Zheng static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr,
9765d4f98a2SYan Zheng 			    u64 bytenr, u64 num_bytes)
9775d4f98a2SYan Zheng {
9785d4f98a2SYan Zheng 	struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
9795d4f98a2SYan Zheng 	struct btrfs_path *path;
9805d4f98a2SYan Zheng 	struct btrfs_file_extent_item *fi;
9815d4f98a2SYan Zheng 	struct extent_buffer *leaf;
9825d4f98a2SYan Zheng 	int ret;
9835d4f98a2SYan Zheng 
9845d4f98a2SYan Zheng 	path = btrfs_alloc_path();
9855d4f98a2SYan Zheng 	if (!path)
9865d4f98a2SYan Zheng 		return -ENOMEM;
9875d4f98a2SYan Zheng 
9885d4f98a2SYan Zheng 	bytenr -= BTRFS_I(reloc_inode)->index_cnt;
989f85b7379SDavid Sterba 	ret = btrfs_lookup_file_extent(NULL, root, path,
990f85b7379SDavid Sterba 			btrfs_ino(BTRFS_I(reloc_inode)), bytenr, 0);
9915d4f98a2SYan Zheng 	if (ret < 0)
9925d4f98a2SYan Zheng 		goto out;
9935d4f98a2SYan Zheng 	if (ret > 0) {
9945d4f98a2SYan Zheng 		ret = -ENOENT;
9955d4f98a2SYan Zheng 		goto out;
9965d4f98a2SYan Zheng 	}
9975d4f98a2SYan Zheng 
9985d4f98a2SYan Zheng 	leaf = path->nodes[0];
9995d4f98a2SYan Zheng 	fi = btrfs_item_ptr(leaf, path->slots[0],
10005d4f98a2SYan Zheng 			    struct btrfs_file_extent_item);
10015d4f98a2SYan Zheng 
10025d4f98a2SYan Zheng 	BUG_ON(btrfs_file_extent_offset(leaf, fi) ||
10035d4f98a2SYan Zheng 	       btrfs_file_extent_compression(leaf, fi) ||
10045d4f98a2SYan Zheng 	       btrfs_file_extent_encryption(leaf, fi) ||
10055d4f98a2SYan Zheng 	       btrfs_file_extent_other_encoding(leaf, fi));
10065d4f98a2SYan Zheng 
10075d4f98a2SYan Zheng 	if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
100883d4cfd4SJosef Bacik 		ret = -EINVAL;
10095d4f98a2SYan Zheng 		goto out;
10105d4f98a2SYan Zheng 	}
10115d4f98a2SYan Zheng 
10125d4f98a2SYan Zheng 	*new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
10135d4f98a2SYan Zheng 	ret = 0;
10145d4f98a2SYan Zheng out:
10155d4f98a2SYan Zheng 	btrfs_free_path(path);
10165d4f98a2SYan Zheng 	return ret;
10175d4f98a2SYan Zheng }
10185d4f98a2SYan Zheng 
10195d4f98a2SYan Zheng /*
10205d4f98a2SYan Zheng  * update file extent items in the tree leaf to point to
10215d4f98a2SYan Zheng  * the new locations.
10225d4f98a2SYan Zheng  */
10233fd0a558SYan, Zheng static noinline_for_stack
10243fd0a558SYan, Zheng int replace_file_extents(struct btrfs_trans_handle *trans,
10255d4f98a2SYan Zheng 			 struct reloc_control *rc,
10265d4f98a2SYan Zheng 			 struct btrfs_root *root,
10273fd0a558SYan, Zheng 			 struct extent_buffer *leaf)
10285d4f98a2SYan Zheng {
10290b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
10305d4f98a2SYan Zheng 	struct btrfs_key key;
10315d4f98a2SYan Zheng 	struct btrfs_file_extent_item *fi;
10325d4f98a2SYan Zheng 	struct inode *inode = NULL;
10335d4f98a2SYan Zheng 	u64 parent;
10345d4f98a2SYan Zheng 	u64 bytenr;
10353fd0a558SYan, Zheng 	u64 new_bytenr = 0;
10365d4f98a2SYan Zheng 	u64 num_bytes;
10375d4f98a2SYan Zheng 	u64 end;
10385d4f98a2SYan Zheng 	u32 nritems;
10395d4f98a2SYan Zheng 	u32 i;
104083d4cfd4SJosef Bacik 	int ret = 0;
10415d4f98a2SYan Zheng 	int first = 1;
10425d4f98a2SYan Zheng 	int dirty = 0;
10435d4f98a2SYan Zheng 
10445d4f98a2SYan Zheng 	if (rc->stage != UPDATE_DATA_PTRS)
10455d4f98a2SYan Zheng 		return 0;
10465d4f98a2SYan Zheng 
10475d4f98a2SYan Zheng 	/* reloc trees always use full backref */
10485d4f98a2SYan Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
10495d4f98a2SYan Zheng 		parent = leaf->start;
10505d4f98a2SYan Zheng 	else
10515d4f98a2SYan Zheng 		parent = 0;
10525d4f98a2SYan Zheng 
10535d4f98a2SYan Zheng 	nritems = btrfs_header_nritems(leaf);
10545d4f98a2SYan Zheng 	for (i = 0; i < nritems; i++) {
105582fa113fSQu Wenruo 		struct btrfs_ref ref = { 0 };
105682fa113fSQu Wenruo 
10575d4f98a2SYan Zheng 		cond_resched();
10585d4f98a2SYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, i);
10595d4f98a2SYan Zheng 		if (key.type != BTRFS_EXTENT_DATA_KEY)
10605d4f98a2SYan Zheng 			continue;
10615d4f98a2SYan Zheng 		fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
10625d4f98a2SYan Zheng 		if (btrfs_file_extent_type(leaf, fi) ==
10635d4f98a2SYan Zheng 		    BTRFS_FILE_EXTENT_INLINE)
10645d4f98a2SYan Zheng 			continue;
10655d4f98a2SYan Zheng 		bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
10665d4f98a2SYan Zheng 		num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
10675d4f98a2SYan Zheng 		if (bytenr == 0)
10685d4f98a2SYan Zheng 			continue;
10699569cc20SQu Wenruo 		if (!in_range(bytenr, rc->block_group->start,
10709569cc20SQu Wenruo 			      rc->block_group->length))
10715d4f98a2SYan Zheng 			continue;
10725d4f98a2SYan Zheng 
10735d4f98a2SYan Zheng 		/*
10745d4f98a2SYan Zheng 		 * if we are modifying block in fs tree, wait for readpage
10755d4f98a2SYan Zheng 		 * to complete and drop the extent cache
10765d4f98a2SYan Zheng 		 */
10775d4f98a2SYan Zheng 		if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
10785d4f98a2SYan Zheng 			if (first) {
10795d4f98a2SYan Zheng 				inode = find_next_inode(root, key.objectid);
10805d4f98a2SYan Zheng 				first = 0;
10814a0cc7caSNikolay Borisov 			} else if (inode && btrfs_ino(BTRFS_I(inode)) < key.objectid) {
10823fd0a558SYan, Zheng 				btrfs_add_delayed_iput(inode);
10835d4f98a2SYan Zheng 				inode = find_next_inode(root, key.objectid);
10845d4f98a2SYan Zheng 			}
10854a0cc7caSNikolay Borisov 			if (inode && btrfs_ino(BTRFS_I(inode)) == key.objectid) {
10865d4f98a2SYan Zheng 				end = key.offset +
10875d4f98a2SYan Zheng 				      btrfs_file_extent_num_bytes(leaf, fi);
10885d4f98a2SYan Zheng 				WARN_ON(!IS_ALIGNED(key.offset,
10890b246afaSJeff Mahoney 						    fs_info->sectorsize));
10900b246afaSJeff Mahoney 				WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize));
10915d4f98a2SYan Zheng 				end--;
10925d4f98a2SYan Zheng 				ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
1093d0082371SJeff Mahoney 						      key.offset, end);
10945d4f98a2SYan Zheng 				if (!ret)
10955d4f98a2SYan Zheng 					continue;
10965d4f98a2SYan Zheng 
1097dcdbc059SNikolay Borisov 				btrfs_drop_extent_cache(BTRFS_I(inode),
1098dcdbc059SNikolay Borisov 						key.offset,	end, 1);
10995d4f98a2SYan Zheng 				unlock_extent(&BTRFS_I(inode)->io_tree,
1100d0082371SJeff Mahoney 					      key.offset, end);
11015d4f98a2SYan Zheng 			}
11025d4f98a2SYan Zheng 		}
11035d4f98a2SYan Zheng 
11045d4f98a2SYan Zheng 		ret = get_new_location(rc->data_inode, &new_bytenr,
11055d4f98a2SYan Zheng 				       bytenr, num_bytes);
110683d4cfd4SJosef Bacik 		if (ret) {
110783d4cfd4SJosef Bacik 			/*
110883d4cfd4SJosef Bacik 			 * Don't have to abort since we've not changed anything
110983d4cfd4SJosef Bacik 			 * in the file extent yet.
111083d4cfd4SJosef Bacik 			 */
111183d4cfd4SJosef Bacik 			break;
11123fd0a558SYan, Zheng 		}
11135d4f98a2SYan Zheng 
11145d4f98a2SYan Zheng 		btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr);
11155d4f98a2SYan Zheng 		dirty = 1;
11165d4f98a2SYan Zheng 
11175d4f98a2SYan Zheng 		key.offset -= btrfs_file_extent_offset(leaf, fi);
111882fa113fSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
111982fa113fSQu Wenruo 				       num_bytes, parent);
112082fa113fSQu Wenruo 		ref.real_root = root->root_key.objectid;
112182fa113fSQu Wenruo 		btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
1122b06c4bf5SFilipe Manana 				    key.objectid, key.offset);
112382fa113fSQu Wenruo 		ret = btrfs_inc_extent_ref(trans, &ref);
112483d4cfd4SJosef Bacik 		if (ret) {
112566642832SJeff Mahoney 			btrfs_abort_transaction(trans, ret);
112683d4cfd4SJosef Bacik 			break;
112783d4cfd4SJosef Bacik 		}
11285d4f98a2SYan Zheng 
1129ffd4bb2aSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
1130ffd4bb2aSQu Wenruo 				       num_bytes, parent);
1131ffd4bb2aSQu Wenruo 		ref.real_root = root->root_key.objectid;
1132ffd4bb2aSQu Wenruo 		btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
1133b06c4bf5SFilipe Manana 				    key.objectid, key.offset);
1134ffd4bb2aSQu Wenruo 		ret = btrfs_free_extent(trans, &ref);
113583d4cfd4SJosef Bacik 		if (ret) {
113666642832SJeff Mahoney 			btrfs_abort_transaction(trans, ret);
113783d4cfd4SJosef Bacik 			break;
113883d4cfd4SJosef Bacik 		}
11395d4f98a2SYan Zheng 	}
11405d4f98a2SYan Zheng 	if (dirty)
11415d4f98a2SYan Zheng 		btrfs_mark_buffer_dirty(leaf);
11423fd0a558SYan, Zheng 	if (inode)
11433fd0a558SYan, Zheng 		btrfs_add_delayed_iput(inode);
114483d4cfd4SJosef Bacik 	return ret;
11455d4f98a2SYan Zheng }
11465d4f98a2SYan Zheng 
11475d4f98a2SYan Zheng static noinline_for_stack
11485d4f98a2SYan Zheng int memcmp_node_keys(struct extent_buffer *eb, int slot,
11495d4f98a2SYan Zheng 		     struct btrfs_path *path, int level)
11505d4f98a2SYan Zheng {
11515d4f98a2SYan Zheng 	struct btrfs_disk_key key1;
11525d4f98a2SYan Zheng 	struct btrfs_disk_key key2;
11535d4f98a2SYan Zheng 	btrfs_node_key(eb, &key1, slot);
11545d4f98a2SYan Zheng 	btrfs_node_key(path->nodes[level], &key2, path->slots[level]);
11555d4f98a2SYan Zheng 	return memcmp(&key1, &key2, sizeof(key1));
11565d4f98a2SYan Zheng }
11575d4f98a2SYan Zheng 
11585d4f98a2SYan Zheng /*
11595d4f98a2SYan Zheng  * try to replace tree blocks in fs tree with the new blocks
11605d4f98a2SYan Zheng  * in reloc tree. tree blocks haven't been modified since the
11615d4f98a2SYan Zheng  * reloc tree was create can be replaced.
11625d4f98a2SYan Zheng  *
11635d4f98a2SYan Zheng  * if a block was replaced, level of the block + 1 is returned.
11645d4f98a2SYan Zheng  * if no block got replaced, 0 is returned. if there are other
11655d4f98a2SYan Zheng  * errors, a negative error number is returned.
11665d4f98a2SYan Zheng  */
11673fd0a558SYan, Zheng static noinline_for_stack
11683d0174f7SQu Wenruo int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc,
11695d4f98a2SYan Zheng 		 struct btrfs_root *dest, struct btrfs_root *src,
11705d4f98a2SYan Zheng 		 struct btrfs_path *path, struct btrfs_key *next_key,
11715d4f98a2SYan Zheng 		 int lowest_level, int max_level)
11725d4f98a2SYan Zheng {
11730b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = dest->fs_info;
11745d4f98a2SYan Zheng 	struct extent_buffer *eb;
11755d4f98a2SYan Zheng 	struct extent_buffer *parent;
117682fa113fSQu Wenruo 	struct btrfs_ref ref = { 0 };
11775d4f98a2SYan Zheng 	struct btrfs_key key;
11785d4f98a2SYan Zheng 	u64 old_bytenr;
11795d4f98a2SYan Zheng 	u64 new_bytenr;
11805d4f98a2SYan Zheng 	u64 old_ptr_gen;
11815d4f98a2SYan Zheng 	u64 new_ptr_gen;
11825d4f98a2SYan Zheng 	u64 last_snapshot;
11835d4f98a2SYan Zheng 	u32 blocksize;
11843fd0a558SYan, Zheng 	int cow = 0;
11855d4f98a2SYan Zheng 	int level;
11865d4f98a2SYan Zheng 	int ret;
11875d4f98a2SYan Zheng 	int slot;
11885d4f98a2SYan Zheng 
11895d4f98a2SYan Zheng 	BUG_ON(src->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
11905d4f98a2SYan Zheng 	BUG_ON(dest->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID);
11915d4f98a2SYan Zheng 
11925d4f98a2SYan Zheng 	last_snapshot = btrfs_root_last_snapshot(&src->root_item);
11933fd0a558SYan, Zheng again:
11945d4f98a2SYan Zheng 	slot = path->slots[lowest_level];
11955d4f98a2SYan Zheng 	btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot);
11965d4f98a2SYan Zheng 
11975d4f98a2SYan Zheng 	eb = btrfs_lock_root_node(dest);
11985d4f98a2SYan Zheng 	level = btrfs_header_level(eb);
11995d4f98a2SYan Zheng 
12005d4f98a2SYan Zheng 	if (level < lowest_level) {
12015d4f98a2SYan Zheng 		btrfs_tree_unlock(eb);
12025d4f98a2SYan Zheng 		free_extent_buffer(eb);
12035d4f98a2SYan Zheng 		return 0;
12045d4f98a2SYan Zheng 	}
12055d4f98a2SYan Zheng 
12063fd0a558SYan, Zheng 	if (cow) {
12079631e4ccSJosef Bacik 		ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb,
12089631e4ccSJosef Bacik 				      BTRFS_NESTING_COW);
12095d4f98a2SYan Zheng 		BUG_ON(ret);
12103fd0a558SYan, Zheng 	}
12115d4f98a2SYan Zheng 
12125d4f98a2SYan Zheng 	if (next_key) {
12135d4f98a2SYan Zheng 		next_key->objectid = (u64)-1;
12145d4f98a2SYan Zheng 		next_key->type = (u8)-1;
12155d4f98a2SYan Zheng 		next_key->offset = (u64)-1;
12165d4f98a2SYan Zheng 	}
12175d4f98a2SYan Zheng 
12185d4f98a2SYan Zheng 	parent = eb;
12195d4f98a2SYan Zheng 	while (1) {
12205d4f98a2SYan Zheng 		level = btrfs_header_level(parent);
12215d4f98a2SYan Zheng 		BUG_ON(level < lowest_level);
12225d4f98a2SYan Zheng 
1223e3b83361SQu Wenruo 		ret = btrfs_bin_search(parent, &key, &slot);
1224cbca7d59SFilipe Manana 		if (ret < 0)
1225cbca7d59SFilipe Manana 			break;
12265d4f98a2SYan Zheng 		if (ret && slot > 0)
12275d4f98a2SYan Zheng 			slot--;
12285d4f98a2SYan Zheng 
12295d4f98a2SYan Zheng 		if (next_key && slot + 1 < btrfs_header_nritems(parent))
12305d4f98a2SYan Zheng 			btrfs_node_key_to_cpu(parent, next_key, slot + 1);
12315d4f98a2SYan Zheng 
12325d4f98a2SYan Zheng 		old_bytenr = btrfs_node_blockptr(parent, slot);
12330b246afaSJeff Mahoney 		blocksize = fs_info->nodesize;
12345d4f98a2SYan Zheng 		old_ptr_gen = btrfs_node_ptr_generation(parent, slot);
12355d4f98a2SYan Zheng 
12365d4f98a2SYan Zheng 		if (level <= max_level) {
12375d4f98a2SYan Zheng 			eb = path->nodes[level];
12385d4f98a2SYan Zheng 			new_bytenr = btrfs_node_blockptr(eb,
12395d4f98a2SYan Zheng 							path->slots[level]);
12405d4f98a2SYan Zheng 			new_ptr_gen = btrfs_node_ptr_generation(eb,
12415d4f98a2SYan Zheng 							path->slots[level]);
12425d4f98a2SYan Zheng 		} else {
12435d4f98a2SYan Zheng 			new_bytenr = 0;
12445d4f98a2SYan Zheng 			new_ptr_gen = 0;
12455d4f98a2SYan Zheng 		}
12465d4f98a2SYan Zheng 
1247fae7f21cSDulshani Gunawardhana 		if (WARN_ON(new_bytenr > 0 && new_bytenr == old_bytenr)) {
12485d4f98a2SYan Zheng 			ret = level;
12495d4f98a2SYan Zheng 			break;
12505d4f98a2SYan Zheng 		}
12515d4f98a2SYan Zheng 
12525d4f98a2SYan Zheng 		if (new_bytenr == 0 || old_ptr_gen > last_snapshot ||
12535d4f98a2SYan Zheng 		    memcmp_node_keys(parent, slot, path, level)) {
12543fd0a558SYan, Zheng 			if (level <= lowest_level) {
12555d4f98a2SYan Zheng 				ret = 0;
12565d4f98a2SYan Zheng 				break;
12575d4f98a2SYan Zheng 			}
12585d4f98a2SYan Zheng 
12596b3426beSJosef Bacik 			eb = btrfs_read_node_slot(parent, slot);
126064c043deSLiu Bo 			if (IS_ERR(eb)) {
126164c043deSLiu Bo 				ret = PTR_ERR(eb);
1262264813acSLiu Bo 				break;
1263416bc658SJosef Bacik 			}
12645d4f98a2SYan Zheng 			btrfs_tree_lock(eb);
12653fd0a558SYan, Zheng 			if (cow) {
12665d4f98a2SYan Zheng 				ret = btrfs_cow_block(trans, dest, eb, parent,
12679631e4ccSJosef Bacik 						      slot, &eb,
12689631e4ccSJosef Bacik 						      BTRFS_NESTING_COW);
12695d4f98a2SYan Zheng 				BUG_ON(ret);
12705d4f98a2SYan Zheng 			}
12715d4f98a2SYan Zheng 
12725d4f98a2SYan Zheng 			btrfs_tree_unlock(parent);
12735d4f98a2SYan Zheng 			free_extent_buffer(parent);
12745d4f98a2SYan Zheng 
12755d4f98a2SYan Zheng 			parent = eb;
12765d4f98a2SYan Zheng 			continue;
12775d4f98a2SYan Zheng 		}
12785d4f98a2SYan Zheng 
12793fd0a558SYan, Zheng 		if (!cow) {
12803fd0a558SYan, Zheng 			btrfs_tree_unlock(parent);
12813fd0a558SYan, Zheng 			free_extent_buffer(parent);
12823fd0a558SYan, Zheng 			cow = 1;
12833fd0a558SYan, Zheng 			goto again;
12843fd0a558SYan, Zheng 		}
12853fd0a558SYan, Zheng 
12865d4f98a2SYan Zheng 		btrfs_node_key_to_cpu(path->nodes[level], &key,
12875d4f98a2SYan Zheng 				      path->slots[level]);
1288b3b4aa74SDavid Sterba 		btrfs_release_path(path);
12895d4f98a2SYan Zheng 
12905d4f98a2SYan Zheng 		path->lowest_level = level;
12915d4f98a2SYan Zheng 		ret = btrfs_search_slot(trans, src, &key, path, 0, 1);
12925d4f98a2SYan Zheng 		path->lowest_level = 0;
12935d4f98a2SYan Zheng 		BUG_ON(ret);
12945d4f98a2SYan Zheng 
12955d4f98a2SYan Zheng 		/*
1296824d8dffSQu Wenruo 		 * Info qgroup to trace both subtrees.
1297824d8dffSQu Wenruo 		 *
1298824d8dffSQu Wenruo 		 * We must trace both trees.
1299824d8dffSQu Wenruo 		 * 1) Tree reloc subtree
1300824d8dffSQu Wenruo 		 *    If not traced, we will leak data numbers
1301824d8dffSQu Wenruo 		 * 2) Fs subtree
1302824d8dffSQu Wenruo 		 *    If not traced, we will double count old data
1303f616f5cdSQu Wenruo 		 *
1304f616f5cdSQu Wenruo 		 * We don't scan the subtree right now, but only record
1305f616f5cdSQu Wenruo 		 * the swapped tree blocks.
1306f616f5cdSQu Wenruo 		 * The real subtree rescan is delayed until we have new
1307f616f5cdSQu Wenruo 		 * CoW on the subtree root node before transaction commit.
1308824d8dffSQu Wenruo 		 */
1309370a11b8SQu Wenruo 		ret = btrfs_qgroup_add_swapped_blocks(trans, dest,
1310370a11b8SQu Wenruo 				rc->block_group, parent, slot,
1311370a11b8SQu Wenruo 				path->nodes[level], path->slots[level],
1312370a11b8SQu Wenruo 				last_snapshot);
1313370a11b8SQu Wenruo 		if (ret < 0)
1314370a11b8SQu Wenruo 			break;
1315824d8dffSQu Wenruo 		/*
13165d4f98a2SYan Zheng 		 * swap blocks in fs tree and reloc tree.
13175d4f98a2SYan Zheng 		 */
13185d4f98a2SYan Zheng 		btrfs_set_node_blockptr(parent, slot, new_bytenr);
13195d4f98a2SYan Zheng 		btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen);
13205d4f98a2SYan Zheng 		btrfs_mark_buffer_dirty(parent);
13215d4f98a2SYan Zheng 
13225d4f98a2SYan Zheng 		btrfs_set_node_blockptr(path->nodes[level],
13235d4f98a2SYan Zheng 					path->slots[level], old_bytenr);
13245d4f98a2SYan Zheng 		btrfs_set_node_ptr_generation(path->nodes[level],
13255d4f98a2SYan Zheng 					      path->slots[level], old_ptr_gen);
13265d4f98a2SYan Zheng 		btrfs_mark_buffer_dirty(path->nodes[level]);
13275d4f98a2SYan Zheng 
132882fa113fSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, old_bytenr,
132982fa113fSQu Wenruo 				       blocksize, path->nodes[level]->start);
133082fa113fSQu Wenruo 		ref.skip_qgroup = true;
133182fa113fSQu Wenruo 		btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid);
133282fa113fSQu Wenruo 		ret = btrfs_inc_extent_ref(trans, &ref);
13335d4f98a2SYan Zheng 		BUG_ON(ret);
133482fa113fSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
133582fa113fSQu Wenruo 				       blocksize, 0);
133682fa113fSQu Wenruo 		ref.skip_qgroup = true;
133782fa113fSQu Wenruo 		btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid);
133882fa113fSQu Wenruo 		ret = btrfs_inc_extent_ref(trans, &ref);
13395d4f98a2SYan Zheng 		BUG_ON(ret);
13405d4f98a2SYan Zheng 
1341ffd4bb2aSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, new_bytenr,
1342ffd4bb2aSQu Wenruo 				       blocksize, path->nodes[level]->start);
1343ffd4bb2aSQu Wenruo 		btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid);
1344ffd4bb2aSQu Wenruo 		ref.skip_qgroup = true;
1345ffd4bb2aSQu Wenruo 		ret = btrfs_free_extent(trans, &ref);
13465d4f98a2SYan Zheng 		BUG_ON(ret);
13475d4f98a2SYan Zheng 
1348ffd4bb2aSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, old_bytenr,
1349ffd4bb2aSQu Wenruo 				       blocksize, 0);
1350ffd4bb2aSQu Wenruo 		btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid);
1351ffd4bb2aSQu Wenruo 		ref.skip_qgroup = true;
1352ffd4bb2aSQu Wenruo 		ret = btrfs_free_extent(trans, &ref);
13535d4f98a2SYan Zheng 		BUG_ON(ret);
13545d4f98a2SYan Zheng 
13555d4f98a2SYan Zheng 		btrfs_unlock_up_safe(path, 0);
13565d4f98a2SYan Zheng 
13575d4f98a2SYan Zheng 		ret = level;
13585d4f98a2SYan Zheng 		break;
13595d4f98a2SYan Zheng 	}
13605d4f98a2SYan Zheng 	btrfs_tree_unlock(parent);
13615d4f98a2SYan Zheng 	free_extent_buffer(parent);
13625d4f98a2SYan Zheng 	return ret;
13635d4f98a2SYan Zheng }
13645d4f98a2SYan Zheng 
13655d4f98a2SYan Zheng /*
13665d4f98a2SYan Zheng  * helper to find next relocated block in reloc tree
13675d4f98a2SYan Zheng  */
13685d4f98a2SYan Zheng static noinline_for_stack
13695d4f98a2SYan Zheng int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
13705d4f98a2SYan Zheng 		       int *level)
13715d4f98a2SYan Zheng {
13725d4f98a2SYan Zheng 	struct extent_buffer *eb;
13735d4f98a2SYan Zheng 	int i;
13745d4f98a2SYan Zheng 	u64 last_snapshot;
13755d4f98a2SYan Zheng 	u32 nritems;
13765d4f98a2SYan Zheng 
13775d4f98a2SYan Zheng 	last_snapshot = btrfs_root_last_snapshot(&root->root_item);
13785d4f98a2SYan Zheng 
13795d4f98a2SYan Zheng 	for (i = 0; i < *level; i++) {
13805d4f98a2SYan Zheng 		free_extent_buffer(path->nodes[i]);
13815d4f98a2SYan Zheng 		path->nodes[i] = NULL;
13825d4f98a2SYan Zheng 	}
13835d4f98a2SYan Zheng 
13845d4f98a2SYan Zheng 	for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
13855d4f98a2SYan Zheng 		eb = path->nodes[i];
13865d4f98a2SYan Zheng 		nritems = btrfs_header_nritems(eb);
13875d4f98a2SYan Zheng 		while (path->slots[i] + 1 < nritems) {
13885d4f98a2SYan Zheng 			path->slots[i]++;
13895d4f98a2SYan Zheng 			if (btrfs_node_ptr_generation(eb, path->slots[i]) <=
13905d4f98a2SYan Zheng 			    last_snapshot)
13915d4f98a2SYan Zheng 				continue;
13925d4f98a2SYan Zheng 
13935d4f98a2SYan Zheng 			*level = i;
13945d4f98a2SYan Zheng 			return 0;
13955d4f98a2SYan Zheng 		}
13965d4f98a2SYan Zheng 		free_extent_buffer(path->nodes[i]);
13975d4f98a2SYan Zheng 		path->nodes[i] = NULL;
13985d4f98a2SYan Zheng 	}
13995d4f98a2SYan Zheng 	return 1;
14005d4f98a2SYan Zheng }
14015d4f98a2SYan Zheng 
14025d4f98a2SYan Zheng /*
14035d4f98a2SYan Zheng  * walk down reloc tree to find relocated block of lowest level
14045d4f98a2SYan Zheng  */
14055d4f98a2SYan Zheng static noinline_for_stack
14065d4f98a2SYan Zheng int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
14075d4f98a2SYan Zheng 			 int *level)
14085d4f98a2SYan Zheng {
14095d4f98a2SYan Zheng 	struct extent_buffer *eb = NULL;
14105d4f98a2SYan Zheng 	int i;
14115d4f98a2SYan Zheng 	u64 ptr_gen = 0;
14125d4f98a2SYan Zheng 	u64 last_snapshot;
14135d4f98a2SYan Zheng 	u32 nritems;
14145d4f98a2SYan Zheng 
14155d4f98a2SYan Zheng 	last_snapshot = btrfs_root_last_snapshot(&root->root_item);
14165d4f98a2SYan Zheng 
14175d4f98a2SYan Zheng 	for (i = *level; i > 0; i--) {
14185d4f98a2SYan Zheng 		eb = path->nodes[i];
14195d4f98a2SYan Zheng 		nritems = btrfs_header_nritems(eb);
14205d4f98a2SYan Zheng 		while (path->slots[i] < nritems) {
14215d4f98a2SYan Zheng 			ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]);
14225d4f98a2SYan Zheng 			if (ptr_gen > last_snapshot)
14235d4f98a2SYan Zheng 				break;
14245d4f98a2SYan Zheng 			path->slots[i]++;
14255d4f98a2SYan Zheng 		}
14265d4f98a2SYan Zheng 		if (path->slots[i] >= nritems) {
14275d4f98a2SYan Zheng 			if (i == *level)
14285d4f98a2SYan Zheng 				break;
14295d4f98a2SYan Zheng 			*level = i + 1;
14305d4f98a2SYan Zheng 			return 0;
14315d4f98a2SYan Zheng 		}
14325d4f98a2SYan Zheng 		if (i == 1) {
14335d4f98a2SYan Zheng 			*level = i;
14345d4f98a2SYan Zheng 			return 0;
14355d4f98a2SYan Zheng 		}
14365d4f98a2SYan Zheng 
14378ef385bbSJosef Bacik 		eb = btrfs_read_node_slot(eb, path->slots[i]);
14388ef385bbSJosef Bacik 		if (IS_ERR(eb))
143964c043deSLiu Bo 			return PTR_ERR(eb);
14405d4f98a2SYan Zheng 		BUG_ON(btrfs_header_level(eb) != i - 1);
14415d4f98a2SYan Zheng 		path->nodes[i - 1] = eb;
14425d4f98a2SYan Zheng 		path->slots[i - 1] = 0;
14435d4f98a2SYan Zheng 	}
14445d4f98a2SYan Zheng 	return 1;
14455d4f98a2SYan Zheng }
14465d4f98a2SYan Zheng 
14475d4f98a2SYan Zheng /*
14485d4f98a2SYan Zheng  * invalidate extent cache for file extents whose key in range of
14495d4f98a2SYan Zheng  * [min_key, max_key)
14505d4f98a2SYan Zheng  */
14515d4f98a2SYan Zheng static int invalidate_extent_cache(struct btrfs_root *root,
14525d4f98a2SYan Zheng 				   struct btrfs_key *min_key,
14535d4f98a2SYan Zheng 				   struct btrfs_key *max_key)
14545d4f98a2SYan Zheng {
14550b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
14565d4f98a2SYan Zheng 	struct inode *inode = NULL;
14575d4f98a2SYan Zheng 	u64 objectid;
14585d4f98a2SYan Zheng 	u64 start, end;
145933345d01SLi Zefan 	u64 ino;
14605d4f98a2SYan Zheng 
14615d4f98a2SYan Zheng 	objectid = min_key->objectid;
14625d4f98a2SYan Zheng 	while (1) {
14635d4f98a2SYan Zheng 		cond_resched();
14645d4f98a2SYan Zheng 		iput(inode);
14655d4f98a2SYan Zheng 
14665d4f98a2SYan Zheng 		if (objectid > max_key->objectid)
14675d4f98a2SYan Zheng 			break;
14685d4f98a2SYan Zheng 
14695d4f98a2SYan Zheng 		inode = find_next_inode(root, objectid);
14705d4f98a2SYan Zheng 		if (!inode)
14715d4f98a2SYan Zheng 			break;
14724a0cc7caSNikolay Borisov 		ino = btrfs_ino(BTRFS_I(inode));
14735d4f98a2SYan Zheng 
147433345d01SLi Zefan 		if (ino > max_key->objectid) {
14755d4f98a2SYan Zheng 			iput(inode);
14765d4f98a2SYan Zheng 			break;
14775d4f98a2SYan Zheng 		}
14785d4f98a2SYan Zheng 
147933345d01SLi Zefan 		objectid = ino + 1;
14805d4f98a2SYan Zheng 		if (!S_ISREG(inode->i_mode))
14815d4f98a2SYan Zheng 			continue;
14825d4f98a2SYan Zheng 
148333345d01SLi Zefan 		if (unlikely(min_key->objectid == ino)) {
14845d4f98a2SYan Zheng 			if (min_key->type > BTRFS_EXTENT_DATA_KEY)
14855d4f98a2SYan Zheng 				continue;
14865d4f98a2SYan Zheng 			if (min_key->type < BTRFS_EXTENT_DATA_KEY)
14875d4f98a2SYan Zheng 				start = 0;
14885d4f98a2SYan Zheng 			else {
14895d4f98a2SYan Zheng 				start = min_key->offset;
14900b246afaSJeff Mahoney 				WARN_ON(!IS_ALIGNED(start, fs_info->sectorsize));
14915d4f98a2SYan Zheng 			}
14925d4f98a2SYan Zheng 		} else {
14935d4f98a2SYan Zheng 			start = 0;
14945d4f98a2SYan Zheng 		}
14955d4f98a2SYan Zheng 
149633345d01SLi Zefan 		if (unlikely(max_key->objectid == ino)) {
14975d4f98a2SYan Zheng 			if (max_key->type < BTRFS_EXTENT_DATA_KEY)
14985d4f98a2SYan Zheng 				continue;
14995d4f98a2SYan Zheng 			if (max_key->type > BTRFS_EXTENT_DATA_KEY) {
15005d4f98a2SYan Zheng 				end = (u64)-1;
15015d4f98a2SYan Zheng 			} else {
15025d4f98a2SYan Zheng 				if (max_key->offset == 0)
15035d4f98a2SYan Zheng 					continue;
15045d4f98a2SYan Zheng 				end = max_key->offset;
15050b246afaSJeff Mahoney 				WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize));
15065d4f98a2SYan Zheng 				end--;
15075d4f98a2SYan Zheng 			}
15085d4f98a2SYan Zheng 		} else {
15095d4f98a2SYan Zheng 			end = (u64)-1;
15105d4f98a2SYan Zheng 		}
15115d4f98a2SYan Zheng 
15125d4f98a2SYan Zheng 		/* the lock_extent waits for readpage to complete */
1513d0082371SJeff Mahoney 		lock_extent(&BTRFS_I(inode)->io_tree, start, end);
1514dcdbc059SNikolay Borisov 		btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 1);
1515d0082371SJeff Mahoney 		unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
15165d4f98a2SYan Zheng 	}
15175d4f98a2SYan Zheng 	return 0;
15185d4f98a2SYan Zheng }
15195d4f98a2SYan Zheng 
15205d4f98a2SYan Zheng static int find_next_key(struct btrfs_path *path, int level,
15215d4f98a2SYan Zheng 			 struct btrfs_key *key)
15225d4f98a2SYan Zheng 
15235d4f98a2SYan Zheng {
15245d4f98a2SYan Zheng 	while (level < BTRFS_MAX_LEVEL) {
15255d4f98a2SYan Zheng 		if (!path->nodes[level])
15265d4f98a2SYan Zheng 			break;
15275d4f98a2SYan Zheng 		if (path->slots[level] + 1 <
15285d4f98a2SYan Zheng 		    btrfs_header_nritems(path->nodes[level])) {
15295d4f98a2SYan Zheng 			btrfs_node_key_to_cpu(path->nodes[level], key,
15305d4f98a2SYan Zheng 					      path->slots[level] + 1);
15315d4f98a2SYan Zheng 			return 0;
15325d4f98a2SYan Zheng 		}
15335d4f98a2SYan Zheng 		level++;
15345d4f98a2SYan Zheng 	}
15355d4f98a2SYan Zheng 	return 1;
15365d4f98a2SYan Zheng }
15375d4f98a2SYan Zheng 
15385d4f98a2SYan Zheng /*
1539d2311e69SQu Wenruo  * Insert current subvolume into reloc_control::dirty_subvol_roots
1540d2311e69SQu Wenruo  */
1541d2311e69SQu Wenruo static void insert_dirty_subvol(struct btrfs_trans_handle *trans,
1542d2311e69SQu Wenruo 				struct reloc_control *rc,
1543d2311e69SQu Wenruo 				struct btrfs_root *root)
1544d2311e69SQu Wenruo {
1545d2311e69SQu Wenruo 	struct btrfs_root *reloc_root = root->reloc_root;
1546d2311e69SQu Wenruo 	struct btrfs_root_item *reloc_root_item;
1547d2311e69SQu Wenruo 
1548d2311e69SQu Wenruo 	/* @root must be a subvolume tree root with a valid reloc tree */
1549d2311e69SQu Wenruo 	ASSERT(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
1550d2311e69SQu Wenruo 	ASSERT(reloc_root);
1551d2311e69SQu Wenruo 
1552d2311e69SQu Wenruo 	reloc_root_item = &reloc_root->root_item;
1553d2311e69SQu Wenruo 	memset(&reloc_root_item->drop_progress, 0,
1554d2311e69SQu Wenruo 		sizeof(reloc_root_item->drop_progress));
1555c8422684SDavid Sterba 	btrfs_set_root_drop_level(reloc_root_item, 0);
1556d2311e69SQu Wenruo 	btrfs_set_root_refs(reloc_root_item, 0);
1557d2311e69SQu Wenruo 	btrfs_update_reloc_root(trans, root);
1558d2311e69SQu Wenruo 
1559d2311e69SQu Wenruo 	if (list_empty(&root->reloc_dirty_list)) {
156000246528SJosef Bacik 		btrfs_grab_root(root);
1561d2311e69SQu Wenruo 		list_add_tail(&root->reloc_dirty_list, &rc->dirty_subvol_roots);
1562d2311e69SQu Wenruo 	}
1563d2311e69SQu Wenruo }
1564d2311e69SQu Wenruo 
1565d2311e69SQu Wenruo static int clean_dirty_subvols(struct reloc_control *rc)
1566d2311e69SQu Wenruo {
1567d2311e69SQu Wenruo 	struct btrfs_root *root;
1568d2311e69SQu Wenruo 	struct btrfs_root *next;
1569d2311e69SQu Wenruo 	int ret = 0;
157030d40577SQu Wenruo 	int ret2;
1571d2311e69SQu Wenruo 
1572d2311e69SQu Wenruo 	list_for_each_entry_safe(root, next, &rc->dirty_subvol_roots,
1573d2311e69SQu Wenruo 				 reloc_dirty_list) {
157430d40577SQu Wenruo 		if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
157530d40577SQu Wenruo 			/* Merged subvolume, cleanup its reloc root */
1576d2311e69SQu Wenruo 			struct btrfs_root *reloc_root = root->reloc_root;
1577d2311e69SQu Wenruo 
1578d2311e69SQu Wenruo 			list_del_init(&root->reloc_dirty_list);
1579d2311e69SQu Wenruo 			root->reloc_root = NULL;
15806282675eSQu Wenruo 			/*
15816282675eSQu Wenruo 			 * Need barrier to ensure clear_bit() only happens after
15826282675eSQu Wenruo 			 * root->reloc_root = NULL. Pairs with have_reloc_root.
15836282675eSQu Wenruo 			 */
15846282675eSQu Wenruo 			smp_wmb();
15851fac4a54SQu Wenruo 			clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
1586f28de8d8SJosef Bacik 			if (reloc_root) {
1587f44deb74SJosef Bacik 				/*
1588f44deb74SJosef Bacik 				 * btrfs_drop_snapshot drops our ref we hold for
1589f44deb74SJosef Bacik 				 * ->reloc_root.  If it fails however we must
1590f44deb74SJosef Bacik 				 * drop the ref ourselves.
1591f44deb74SJosef Bacik 				 */
1592f28de8d8SJosef Bacik 				ret2 = btrfs_drop_snapshot(reloc_root, 0, 1);
1593f44deb74SJosef Bacik 				if (ret2 < 0) {
1594f44deb74SJosef Bacik 					btrfs_put_root(reloc_root);
1595f44deb74SJosef Bacik 					if (!ret)
1596f28de8d8SJosef Bacik 						ret = ret2;
1597f28de8d8SJosef Bacik 				}
1598f44deb74SJosef Bacik 			}
159900246528SJosef Bacik 			btrfs_put_root(root);
160030d40577SQu Wenruo 		} else {
160130d40577SQu Wenruo 			/* Orphan reloc tree, just clean it up */
16020078a9f9SNikolay Borisov 			ret2 = btrfs_drop_snapshot(root, 0, 1);
1603f44deb74SJosef Bacik 			if (ret2 < 0) {
1604f44deb74SJosef Bacik 				btrfs_put_root(root);
1605f44deb74SJosef Bacik 				if (!ret)
160630d40577SQu Wenruo 					ret = ret2;
160730d40577SQu Wenruo 			}
1608d2311e69SQu Wenruo 		}
1609f44deb74SJosef Bacik 	}
1610d2311e69SQu Wenruo 	return ret;
1611d2311e69SQu Wenruo }
1612d2311e69SQu Wenruo 
1613d2311e69SQu Wenruo /*
16145d4f98a2SYan Zheng  * merge the relocated tree blocks in reloc tree with corresponding
16155d4f98a2SYan Zheng  * fs tree.
16165d4f98a2SYan Zheng  */
16175d4f98a2SYan Zheng static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
16185d4f98a2SYan Zheng 					       struct btrfs_root *root)
16195d4f98a2SYan Zheng {
16200b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
16215d4f98a2SYan Zheng 	struct btrfs_key key;
16225d4f98a2SYan Zheng 	struct btrfs_key next_key;
16239e6a0c52SJosef Bacik 	struct btrfs_trans_handle *trans = NULL;
16245d4f98a2SYan Zheng 	struct btrfs_root *reloc_root;
16255d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
16265d4f98a2SYan Zheng 	struct btrfs_path *path;
16273fd0a558SYan, Zheng 	struct extent_buffer *leaf;
1628fca3a45dSJosef Bacik 	int reserve_level;
16295d4f98a2SYan Zheng 	int level;
16305d4f98a2SYan Zheng 	int max_level;
16315d4f98a2SYan Zheng 	int replaced = 0;
1632c6a592f2SNikolay Borisov 	int ret = 0;
16333fd0a558SYan, Zheng 	u32 min_reserved;
16345d4f98a2SYan Zheng 
16355d4f98a2SYan Zheng 	path = btrfs_alloc_path();
16365d4f98a2SYan Zheng 	if (!path)
16375d4f98a2SYan Zheng 		return -ENOMEM;
1638e4058b54SDavid Sterba 	path->reada = READA_FORWARD;
16395d4f98a2SYan Zheng 
16405d4f98a2SYan Zheng 	reloc_root = root->reloc_root;
16415d4f98a2SYan Zheng 	root_item = &reloc_root->root_item;
16425d4f98a2SYan Zheng 
16435d4f98a2SYan Zheng 	if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
16445d4f98a2SYan Zheng 		level = btrfs_root_level(root_item);
164567439dadSDavid Sterba 		atomic_inc(&reloc_root->node->refs);
16465d4f98a2SYan Zheng 		path->nodes[level] = reloc_root->node;
16475d4f98a2SYan Zheng 		path->slots[level] = 0;
16485d4f98a2SYan Zheng 	} else {
16495d4f98a2SYan Zheng 		btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
16505d4f98a2SYan Zheng 
1651c8422684SDavid Sterba 		level = btrfs_root_drop_level(root_item);
16525d4f98a2SYan Zheng 		BUG_ON(level == 0);
16535d4f98a2SYan Zheng 		path->lowest_level = level;
16545d4f98a2SYan Zheng 		ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0);
165533c66f43SYan Zheng 		path->lowest_level = 0;
16565d4f98a2SYan Zheng 		if (ret < 0) {
16575d4f98a2SYan Zheng 			btrfs_free_path(path);
16585d4f98a2SYan Zheng 			return ret;
16595d4f98a2SYan Zheng 		}
16605d4f98a2SYan Zheng 
16615d4f98a2SYan Zheng 		btrfs_node_key_to_cpu(path->nodes[level], &next_key,
16625d4f98a2SYan Zheng 				      path->slots[level]);
16635d4f98a2SYan Zheng 		WARN_ON(memcmp(&key, &next_key, sizeof(key)));
16645d4f98a2SYan Zheng 
16655d4f98a2SYan Zheng 		btrfs_unlock_up_safe(path, 0);
16665d4f98a2SYan Zheng 	}
16675d4f98a2SYan Zheng 
166844d354abSQu Wenruo 	/*
166944d354abSQu Wenruo 	 * In merge_reloc_root(), we modify the upper level pointer to swap the
167044d354abSQu Wenruo 	 * tree blocks between reloc tree and subvolume tree.  Thus for tree
167144d354abSQu Wenruo 	 * block COW, we COW at most from level 1 to root level for each tree.
167244d354abSQu Wenruo 	 *
167344d354abSQu Wenruo 	 * Thus the needed metadata size is at most root_level * nodesize,
167444d354abSQu Wenruo 	 * and * 2 since we have two trees to COW.
167544d354abSQu Wenruo 	 */
1676fca3a45dSJosef Bacik 	reserve_level = max_t(int, 1, btrfs_root_level(root_item));
1677fca3a45dSJosef Bacik 	min_reserved = fs_info->nodesize * reserve_level * 2;
16785d4f98a2SYan Zheng 	memset(&next_key, 0, sizeof(next_key));
16795d4f98a2SYan Zheng 
16805d4f98a2SYan Zheng 	while (1) {
168108e007d2SMiao Xie 		ret = btrfs_block_rsv_refill(root, rc->block_rsv, min_reserved,
168244d354abSQu Wenruo 					     BTRFS_RESERVE_FLUSH_LIMIT);
1683c6a592f2SNikolay Borisov 		if (ret)
16849e6a0c52SJosef Bacik 			goto out;
16859e6a0c52SJosef Bacik 		trans = btrfs_start_transaction(root, 0);
16869e6a0c52SJosef Bacik 		if (IS_ERR(trans)) {
1687c6a592f2SNikolay Borisov 			ret = PTR_ERR(trans);
16889e6a0c52SJosef Bacik 			trans = NULL;
16899e6a0c52SJosef Bacik 			goto out;
16909e6a0c52SJosef Bacik 		}
16912abc726aSJosef Bacik 
16922abc726aSJosef Bacik 		/*
16932abc726aSJosef Bacik 		 * At this point we no longer have a reloc_control, so we can't
16942abc726aSJosef Bacik 		 * depend on btrfs_init_reloc_root to update our last_trans.
16952abc726aSJosef Bacik 		 *
16962abc726aSJosef Bacik 		 * But that's ok, we started the trans handle on our
16972abc726aSJosef Bacik 		 * corresponding fs_root, which means it's been added to the
16982abc726aSJosef Bacik 		 * dirty list.  At commit time we'll still call
16992abc726aSJosef Bacik 		 * btrfs_update_reloc_root() and update our root item
17002abc726aSJosef Bacik 		 * appropriately.
17012abc726aSJosef Bacik 		 */
17022abc726aSJosef Bacik 		reloc_root->last_trans = trans->transid;
17039e6a0c52SJosef Bacik 		trans->block_rsv = rc->block_rsv;
17043fd0a558SYan, Zheng 
17053fd0a558SYan, Zheng 		replaced = 0;
17065d4f98a2SYan Zheng 		max_level = level;
17075d4f98a2SYan Zheng 
17085d4f98a2SYan Zheng 		ret = walk_down_reloc_tree(reloc_root, path, &level);
1709c6a592f2SNikolay Borisov 		if (ret < 0)
17105d4f98a2SYan Zheng 			goto out;
17115d4f98a2SYan Zheng 		if (ret > 0)
17125d4f98a2SYan Zheng 			break;
17135d4f98a2SYan Zheng 
17145d4f98a2SYan Zheng 		if (!find_next_key(path, level, &key) &&
17155d4f98a2SYan Zheng 		    btrfs_comp_cpu_keys(&next_key, &key) >= 0) {
17165d4f98a2SYan Zheng 			ret = 0;
17175d4f98a2SYan Zheng 		} else {
17183d0174f7SQu Wenruo 			ret = replace_path(trans, rc, root, reloc_root, path,
17193fd0a558SYan, Zheng 					   &next_key, level, max_level);
17205d4f98a2SYan Zheng 		}
1721c6a592f2SNikolay Borisov 		if (ret < 0)
17225d4f98a2SYan Zheng 			goto out;
17235d4f98a2SYan Zheng 		if (ret > 0) {
17245d4f98a2SYan Zheng 			level = ret;
17255d4f98a2SYan Zheng 			btrfs_node_key_to_cpu(path->nodes[level], &key,
17265d4f98a2SYan Zheng 					      path->slots[level]);
17275d4f98a2SYan Zheng 			replaced = 1;
17285d4f98a2SYan Zheng 		}
17295d4f98a2SYan Zheng 
17305d4f98a2SYan Zheng 		ret = walk_up_reloc_tree(reloc_root, path, &level);
17315d4f98a2SYan Zheng 		if (ret > 0)
17325d4f98a2SYan Zheng 			break;
17335d4f98a2SYan Zheng 
17345d4f98a2SYan Zheng 		BUG_ON(level == 0);
17355d4f98a2SYan Zheng 		/*
17365d4f98a2SYan Zheng 		 * save the merging progress in the drop_progress.
17375d4f98a2SYan Zheng 		 * this is OK since root refs == 1 in this case.
17385d4f98a2SYan Zheng 		 */
17395d4f98a2SYan Zheng 		btrfs_node_key(path->nodes[level], &root_item->drop_progress,
17405d4f98a2SYan Zheng 			       path->slots[level]);
1741c8422684SDavid Sterba 		btrfs_set_root_drop_level(root_item, level);
17425d4f98a2SYan Zheng 
17433a45bb20SJeff Mahoney 		btrfs_end_transaction_throttle(trans);
17449e6a0c52SJosef Bacik 		trans = NULL;
17455d4f98a2SYan Zheng 
17462ff7e61eSJeff Mahoney 		btrfs_btree_balance_dirty(fs_info);
17475d4f98a2SYan Zheng 
17485d4f98a2SYan Zheng 		if (replaced && rc->stage == UPDATE_DATA_PTRS)
17495d4f98a2SYan Zheng 			invalidate_extent_cache(root, &key, &next_key);
17505d4f98a2SYan Zheng 	}
17515d4f98a2SYan Zheng 
17525d4f98a2SYan Zheng 	/*
17535d4f98a2SYan Zheng 	 * handle the case only one block in the fs tree need to be
17545d4f98a2SYan Zheng 	 * relocated and the block is tree root.
17555d4f98a2SYan Zheng 	 */
17565d4f98a2SYan Zheng 	leaf = btrfs_lock_root_node(root);
17579631e4ccSJosef Bacik 	ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf,
17589631e4ccSJosef Bacik 			      BTRFS_NESTING_COW);
17595d4f98a2SYan Zheng 	btrfs_tree_unlock(leaf);
17605d4f98a2SYan Zheng 	free_extent_buffer(leaf);
17615d4f98a2SYan Zheng out:
17625d4f98a2SYan Zheng 	btrfs_free_path(path);
17635d4f98a2SYan Zheng 
1764c6a592f2SNikolay Borisov 	if (ret == 0)
1765d2311e69SQu Wenruo 		insert_dirty_subvol(trans, rc, root);
17665d4f98a2SYan Zheng 
17679e6a0c52SJosef Bacik 	if (trans)
17683a45bb20SJeff Mahoney 		btrfs_end_transaction_throttle(trans);
17695d4f98a2SYan Zheng 
17702ff7e61eSJeff Mahoney 	btrfs_btree_balance_dirty(fs_info);
17715d4f98a2SYan Zheng 
17725d4f98a2SYan Zheng 	if (replaced && rc->stage == UPDATE_DATA_PTRS)
17735d4f98a2SYan Zheng 		invalidate_extent_cache(root, &key, &next_key);
17745d4f98a2SYan Zheng 
1775c6a592f2SNikolay Borisov 	return ret;
17765d4f98a2SYan Zheng }
17775d4f98a2SYan Zheng 
17783fd0a558SYan, Zheng static noinline_for_stack
17793fd0a558SYan, Zheng int prepare_to_merge(struct reloc_control *rc, int err)
17805d4f98a2SYan Zheng {
17813fd0a558SYan, Zheng 	struct btrfs_root *root = rc->extent_root;
17820b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
17833fd0a558SYan, Zheng 	struct btrfs_root *reloc_root;
17845d4f98a2SYan Zheng 	struct btrfs_trans_handle *trans;
17853fd0a558SYan, Zheng 	LIST_HEAD(reloc_roots);
17863fd0a558SYan, Zheng 	u64 num_bytes = 0;
17873fd0a558SYan, Zheng 	int ret;
17883fd0a558SYan, Zheng 
17890b246afaSJeff Mahoney 	mutex_lock(&fs_info->reloc_mutex);
17900b246afaSJeff Mahoney 	rc->merging_rsv_size += fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
17913fd0a558SYan, Zheng 	rc->merging_rsv_size += rc->nodes_relocated * 2;
17920b246afaSJeff Mahoney 	mutex_unlock(&fs_info->reloc_mutex);
17937585717fSChris Mason 
17943fd0a558SYan, Zheng again:
17953fd0a558SYan, Zheng 	if (!err) {
17963fd0a558SYan, Zheng 		num_bytes = rc->merging_rsv_size;
179708e007d2SMiao Xie 		ret = btrfs_block_rsv_add(root, rc->block_rsv, num_bytes,
179808e007d2SMiao Xie 					  BTRFS_RESERVE_FLUSH_ALL);
17993fd0a558SYan, Zheng 		if (ret)
18003fd0a558SYan, Zheng 			err = ret;
18013fd0a558SYan, Zheng 	}
18023fd0a558SYan, Zheng 
18037a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(rc->extent_root);
18043612b495STsutomu Itoh 	if (IS_ERR(trans)) {
18053612b495STsutomu Itoh 		if (!err)
18062ff7e61eSJeff Mahoney 			btrfs_block_rsv_release(fs_info, rc->block_rsv,
180763f018beSNikolay Borisov 						num_bytes, NULL);
18083612b495STsutomu Itoh 		return PTR_ERR(trans);
18093612b495STsutomu Itoh 	}
18103fd0a558SYan, Zheng 
18113fd0a558SYan, Zheng 	if (!err) {
18123fd0a558SYan, Zheng 		if (num_bytes != rc->merging_rsv_size) {
18133a45bb20SJeff Mahoney 			btrfs_end_transaction(trans);
18142ff7e61eSJeff Mahoney 			btrfs_block_rsv_release(fs_info, rc->block_rsv,
181563f018beSNikolay Borisov 						num_bytes, NULL);
18163fd0a558SYan, Zheng 			goto again;
18173fd0a558SYan, Zheng 		}
18183fd0a558SYan, Zheng 	}
18193fd0a558SYan, Zheng 
18203fd0a558SYan, Zheng 	rc->merge_reloc_tree = 1;
18213fd0a558SYan, Zheng 
18223fd0a558SYan, Zheng 	while (!list_empty(&rc->reloc_roots)) {
18233fd0a558SYan, Zheng 		reloc_root = list_entry(rc->reloc_roots.next,
18243fd0a558SYan, Zheng 					struct btrfs_root, root_list);
18253fd0a558SYan, Zheng 		list_del_init(&reloc_root->root_list);
18263fd0a558SYan, Zheng 
1827a820feb5SDavid Sterba 		root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
1828a820feb5SDavid Sterba 				false);
18293fd0a558SYan, Zheng 		BUG_ON(IS_ERR(root));
18303fd0a558SYan, Zheng 		BUG_ON(root->reloc_root != reloc_root);
18313fd0a558SYan, Zheng 
18323fd0a558SYan, Zheng 		/*
18333fd0a558SYan, Zheng 		 * set reference count to 1, so btrfs_recover_relocation
18343fd0a558SYan, Zheng 		 * knows it should resumes merging
18353fd0a558SYan, Zheng 		 */
18363fd0a558SYan, Zheng 		if (!err)
18373fd0a558SYan, Zheng 			btrfs_set_root_refs(&reloc_root->root_item, 1);
18383fd0a558SYan, Zheng 		btrfs_update_reloc_root(trans, root);
18393fd0a558SYan, Zheng 
18403fd0a558SYan, Zheng 		list_add(&reloc_root->root_list, &reloc_roots);
184100246528SJosef Bacik 		btrfs_put_root(root);
18423fd0a558SYan, Zheng 	}
18433fd0a558SYan, Zheng 
18443fd0a558SYan, Zheng 	list_splice(&reloc_roots, &rc->reloc_roots);
18453fd0a558SYan, Zheng 
18463fd0a558SYan, Zheng 	if (!err)
18473a45bb20SJeff Mahoney 		btrfs_commit_transaction(trans);
18483fd0a558SYan, Zheng 	else
18493a45bb20SJeff Mahoney 		btrfs_end_transaction(trans);
18503fd0a558SYan, Zheng 	return err;
18513fd0a558SYan, Zheng }
18523fd0a558SYan, Zheng 
18533fd0a558SYan, Zheng static noinline_for_stack
1854aca1bba6SLiu Bo void free_reloc_roots(struct list_head *list)
1855aca1bba6SLiu Bo {
1856a7571232SNikolay Borisov 	struct btrfs_root *reloc_root, *tmp;
1857aca1bba6SLiu Bo 
1858a7571232SNikolay Borisov 	list_for_each_entry_safe(reloc_root, tmp, list, root_list)
1859bb166d72SNaohiro Aota 		__del_reloc_root(reloc_root);
1860aca1bba6SLiu Bo }
1861aca1bba6SLiu Bo 
1862aca1bba6SLiu Bo static noinline_for_stack
186394404e82SDavid Sterba void merge_reloc_roots(struct reloc_control *rc)
18643fd0a558SYan, Zheng {
18650b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
18665d4f98a2SYan Zheng 	struct btrfs_root *root;
18675d4f98a2SYan Zheng 	struct btrfs_root *reloc_root;
18683fd0a558SYan, Zheng 	LIST_HEAD(reloc_roots);
18693fd0a558SYan, Zheng 	int found = 0;
1870aca1bba6SLiu Bo 	int ret = 0;
18713fd0a558SYan, Zheng again:
18723fd0a558SYan, Zheng 	root = rc->extent_root;
18737585717fSChris Mason 
18747585717fSChris Mason 	/*
18757585717fSChris Mason 	 * this serializes us with btrfs_record_root_in_transaction,
18767585717fSChris Mason 	 * we have to make sure nobody is in the middle of
18777585717fSChris Mason 	 * adding their roots to the list while we are
18787585717fSChris Mason 	 * doing this splice
18797585717fSChris Mason 	 */
18800b246afaSJeff Mahoney 	mutex_lock(&fs_info->reloc_mutex);
18813fd0a558SYan, Zheng 	list_splice_init(&rc->reloc_roots, &reloc_roots);
18820b246afaSJeff Mahoney 	mutex_unlock(&fs_info->reloc_mutex);
18835d4f98a2SYan Zheng 
18843fd0a558SYan, Zheng 	while (!list_empty(&reloc_roots)) {
18853fd0a558SYan, Zheng 		found = 1;
18863fd0a558SYan, Zheng 		reloc_root = list_entry(reloc_roots.next,
18873fd0a558SYan, Zheng 					struct btrfs_root, root_list);
18885d4f98a2SYan Zheng 
1889a820feb5SDavid Sterba 		root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
1890a820feb5SDavid Sterba 					 false);
18915d4f98a2SYan Zheng 		if (btrfs_root_refs(&reloc_root->root_item) > 0) {
18925d4f98a2SYan Zheng 			BUG_ON(IS_ERR(root));
18935d4f98a2SYan Zheng 			BUG_ON(root->reloc_root != reloc_root);
18943fd0a558SYan, Zheng 			ret = merge_reloc_root(rc, root);
189500246528SJosef Bacik 			btrfs_put_root(root);
1896b37b39cdSJosef Bacik 			if (ret) {
189725e293c2SWang Shilong 				if (list_empty(&reloc_root->root_list))
189825e293c2SWang Shilong 					list_add_tail(&reloc_root->root_list,
189925e293c2SWang Shilong 						      &reloc_roots);
1900aca1bba6SLiu Bo 				goto out;
1901b37b39cdSJosef Bacik 			}
19023fd0a558SYan, Zheng 		} else {
190351415b6cSQu Wenruo 			if (!IS_ERR(root)) {
190451415b6cSQu Wenruo 				if (root->reloc_root == reloc_root) {
190551415b6cSQu Wenruo 					root->reloc_root = NULL;
190651415b6cSQu Wenruo 					btrfs_put_root(reloc_root);
190751415b6cSQu Wenruo 				}
19081dae7e0eSQu Wenruo 				clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE,
19091dae7e0eSQu Wenruo 					  &root->state);
191051415b6cSQu Wenruo 				btrfs_put_root(root);
191151415b6cSQu Wenruo 			}
191251415b6cSQu Wenruo 
19133fd0a558SYan, Zheng 			list_del_init(&reloc_root->root_list);
191430d40577SQu Wenruo 			/* Don't forget to queue this reloc root for cleanup */
191530d40577SQu Wenruo 			list_add_tail(&reloc_root->reloc_dirty_list,
191630d40577SQu Wenruo 				      &rc->dirty_subvol_roots);
19173fd0a558SYan, Zheng 		}
19185d4f98a2SYan Zheng 	}
19195d4f98a2SYan Zheng 
19203fd0a558SYan, Zheng 	if (found) {
19213fd0a558SYan, Zheng 		found = 0;
19223fd0a558SYan, Zheng 		goto again;
19235d4f98a2SYan Zheng 	}
1924aca1bba6SLiu Bo out:
1925aca1bba6SLiu Bo 	if (ret) {
19260b246afaSJeff Mahoney 		btrfs_handle_fs_error(fs_info, ret, NULL);
1927aca1bba6SLiu Bo 		free_reloc_roots(&reloc_roots);
1928467bb1d2SWang Shilong 
1929467bb1d2SWang Shilong 		/* new reloc root may be added */
19300b246afaSJeff Mahoney 		mutex_lock(&fs_info->reloc_mutex);
1931467bb1d2SWang Shilong 		list_splice_init(&rc->reloc_roots, &reloc_roots);
19320b246afaSJeff Mahoney 		mutex_unlock(&fs_info->reloc_mutex);
1933467bb1d2SWang Shilong 		free_reloc_roots(&reloc_roots);
1934aca1bba6SLiu Bo 	}
1935aca1bba6SLiu Bo 
19367b7b7431SJosef Bacik 	/*
19377b7b7431SJosef Bacik 	 * We used to have
19387b7b7431SJosef Bacik 	 *
19397b7b7431SJosef Bacik 	 * BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
19407b7b7431SJosef Bacik 	 *
19417b7b7431SJosef Bacik 	 * here, but it's wrong.  If we fail to start the transaction in
19427b7b7431SJosef Bacik 	 * prepare_to_merge() we will have only 0 ref reloc roots, none of which
19437b7b7431SJosef Bacik 	 * have actually been removed from the reloc_root_tree rb tree.  This is
19447b7b7431SJosef Bacik 	 * fine because we're bailing here, and we hold a reference on the root
19457b7b7431SJosef Bacik 	 * for the list that holds it, so these roots will be cleaned up when we
19467b7b7431SJosef Bacik 	 * do the reloc_dirty_list afterwards.  Meanwhile the root->reloc_root
19477b7b7431SJosef Bacik 	 * will be cleaned up on unmount.
19487b7b7431SJosef Bacik 	 *
19497b7b7431SJosef Bacik 	 * The remaining nodes will be cleaned up by free_reloc_control.
19507b7b7431SJosef Bacik 	 */
19515d4f98a2SYan Zheng }
19525d4f98a2SYan Zheng 
19535d4f98a2SYan Zheng static void free_block_list(struct rb_root *blocks)
19545d4f98a2SYan Zheng {
19555d4f98a2SYan Zheng 	struct tree_block *block;
19565d4f98a2SYan Zheng 	struct rb_node *rb_node;
19575d4f98a2SYan Zheng 	while ((rb_node = rb_first(blocks))) {
19585d4f98a2SYan Zheng 		block = rb_entry(rb_node, struct tree_block, rb_node);
19595d4f98a2SYan Zheng 		rb_erase(rb_node, blocks);
19605d4f98a2SYan Zheng 		kfree(block);
19615d4f98a2SYan Zheng 	}
19625d4f98a2SYan Zheng }
19635d4f98a2SYan Zheng 
19645d4f98a2SYan Zheng static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans,
19655d4f98a2SYan Zheng 				      struct btrfs_root *reloc_root)
19665d4f98a2SYan Zheng {
19670b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = reloc_root->fs_info;
19685d4f98a2SYan Zheng 	struct btrfs_root *root;
1969442b1ac5SJosef Bacik 	int ret;
19705d4f98a2SYan Zheng 
19715d4f98a2SYan Zheng 	if (reloc_root->last_trans == trans->transid)
19725d4f98a2SYan Zheng 		return 0;
19735d4f98a2SYan Zheng 
1974a820feb5SDavid Sterba 	root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset, false);
19755d4f98a2SYan Zheng 	BUG_ON(IS_ERR(root));
19765d4f98a2SYan Zheng 	BUG_ON(root->reloc_root != reloc_root);
1977442b1ac5SJosef Bacik 	ret = btrfs_record_root_in_trans(trans, root);
197800246528SJosef Bacik 	btrfs_put_root(root);
19795d4f98a2SYan Zheng 
1980442b1ac5SJosef Bacik 	return ret;
19815d4f98a2SYan Zheng }
19825d4f98a2SYan Zheng 
19833fd0a558SYan, Zheng static noinline_for_stack
19843fd0a558SYan, Zheng struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,
19853fd0a558SYan, Zheng 				     struct reloc_control *rc,
1986a26195a5SQu Wenruo 				     struct btrfs_backref_node *node,
1987a26195a5SQu Wenruo 				     struct btrfs_backref_edge *edges[])
19885d4f98a2SYan Zheng {
1989a26195a5SQu Wenruo 	struct btrfs_backref_node *next;
19905d4f98a2SYan Zheng 	struct btrfs_root *root;
19913fd0a558SYan, Zheng 	int index = 0;
19923fd0a558SYan, Zheng 
19935d4f98a2SYan Zheng 	next = node;
19945d4f98a2SYan Zheng 	while (1) {
19955d4f98a2SYan Zheng 		cond_resched();
19965d4f98a2SYan Zheng 		next = walk_up_backref(next, edges, &index);
19975d4f98a2SYan Zheng 		root = next->root;
19983fd0a558SYan, Zheng 		BUG_ON(!root);
199992a7cc42SQu Wenruo 		BUG_ON(!test_bit(BTRFS_ROOT_SHAREABLE, &root->state));
20005d4f98a2SYan Zheng 
20015d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
20025d4f98a2SYan Zheng 			record_reloc_root_in_trans(trans, root);
20035d4f98a2SYan Zheng 			break;
20045d4f98a2SYan Zheng 		}
20055d4f98a2SYan Zheng 
20065d4f98a2SYan Zheng 		btrfs_record_root_in_trans(trans, root);
20073fd0a558SYan, Zheng 		root = root->reloc_root;
20083fd0a558SYan, Zheng 
20093fd0a558SYan, Zheng 		if (next->new_bytenr != root->node->start) {
20103fd0a558SYan, Zheng 			BUG_ON(next->new_bytenr);
20113fd0a558SYan, Zheng 			BUG_ON(!list_empty(&next->list));
20123fd0a558SYan, Zheng 			next->new_bytenr = root->node->start;
201300246528SJosef Bacik 			btrfs_put_root(next->root);
201400246528SJosef Bacik 			next->root = btrfs_grab_root(root);
20150b530bc5SJosef Bacik 			ASSERT(next->root);
20163fd0a558SYan, Zheng 			list_add_tail(&next->list,
20173fd0a558SYan, Zheng 				      &rc->backref_cache.changed);
20189569cc20SQu Wenruo 			mark_block_processed(rc, next);
20195d4f98a2SYan Zheng 			break;
20205d4f98a2SYan Zheng 		}
20215d4f98a2SYan Zheng 
20223fd0a558SYan, Zheng 		WARN_ON(1);
20235d4f98a2SYan Zheng 		root = NULL;
20245d4f98a2SYan Zheng 		next = walk_down_backref(edges, &index);
20255d4f98a2SYan Zheng 		if (!next || next->level <= node->level)
20265d4f98a2SYan Zheng 			break;
20275d4f98a2SYan Zheng 	}
20283fd0a558SYan, Zheng 	if (!root)
20293fd0a558SYan, Zheng 		return NULL;
20305d4f98a2SYan Zheng 
20313fd0a558SYan, Zheng 	next = node;
20323fd0a558SYan, Zheng 	/* setup backref node path for btrfs_reloc_cow_block */
20333fd0a558SYan, Zheng 	while (1) {
20343fd0a558SYan, Zheng 		rc->backref_cache.path[next->level] = next;
20353fd0a558SYan, Zheng 		if (--index < 0)
20363fd0a558SYan, Zheng 			break;
20373fd0a558SYan, Zheng 		next = edges[index]->node[UPPER];
20383fd0a558SYan, Zheng 	}
20395d4f98a2SYan Zheng 	return root;
20405d4f98a2SYan Zheng }
20415d4f98a2SYan Zheng 
20423fd0a558SYan, Zheng /*
204392a7cc42SQu Wenruo  * Select a tree root for relocation.
204492a7cc42SQu Wenruo  *
204592a7cc42SQu Wenruo  * Return NULL if the block is not shareable. We should use do_relocation() in
204692a7cc42SQu Wenruo  * this case.
204792a7cc42SQu Wenruo  *
204892a7cc42SQu Wenruo  * Return a tree root pointer if the block is shareable.
204992a7cc42SQu Wenruo  * Return -ENOENT if the block is root of reloc tree.
20503fd0a558SYan, Zheng  */
20515d4f98a2SYan Zheng static noinline_for_stack
2052a26195a5SQu Wenruo struct btrfs_root *select_one_root(struct btrfs_backref_node *node)
20535d4f98a2SYan Zheng {
2054a26195a5SQu Wenruo 	struct btrfs_backref_node *next;
20553fd0a558SYan, Zheng 	struct btrfs_root *root;
20563fd0a558SYan, Zheng 	struct btrfs_root *fs_root = NULL;
2057a26195a5SQu Wenruo 	struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
20583fd0a558SYan, Zheng 	int index = 0;
20593fd0a558SYan, Zheng 
20603fd0a558SYan, Zheng 	next = node;
20613fd0a558SYan, Zheng 	while (1) {
20623fd0a558SYan, Zheng 		cond_resched();
20633fd0a558SYan, Zheng 		next = walk_up_backref(next, edges, &index);
20643fd0a558SYan, Zheng 		root = next->root;
20653fd0a558SYan, Zheng 		BUG_ON(!root);
20663fd0a558SYan, Zheng 
206792a7cc42SQu Wenruo 		/* No other choice for non-shareable tree */
206892a7cc42SQu Wenruo 		if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
20693fd0a558SYan, Zheng 			return root;
20703fd0a558SYan, Zheng 
20713fd0a558SYan, Zheng 		if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)
20723fd0a558SYan, Zheng 			fs_root = root;
20733fd0a558SYan, Zheng 
20743fd0a558SYan, Zheng 		if (next != node)
20753fd0a558SYan, Zheng 			return NULL;
20763fd0a558SYan, Zheng 
20773fd0a558SYan, Zheng 		next = walk_down_backref(edges, &index);
20783fd0a558SYan, Zheng 		if (!next || next->level <= node->level)
20793fd0a558SYan, Zheng 			break;
20803fd0a558SYan, Zheng 	}
20813fd0a558SYan, Zheng 
20823fd0a558SYan, Zheng 	if (!fs_root)
20833fd0a558SYan, Zheng 		return ERR_PTR(-ENOENT);
20843fd0a558SYan, Zheng 	return fs_root;
20855d4f98a2SYan Zheng }
20865d4f98a2SYan Zheng 
20875d4f98a2SYan Zheng static noinline_for_stack
20883fd0a558SYan, Zheng u64 calcu_metadata_size(struct reloc_control *rc,
2089a26195a5SQu Wenruo 			struct btrfs_backref_node *node, int reserve)
20905d4f98a2SYan Zheng {
20910b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2092a26195a5SQu Wenruo 	struct btrfs_backref_node *next = node;
2093a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
2094a26195a5SQu Wenruo 	struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
20953fd0a558SYan, Zheng 	u64 num_bytes = 0;
20963fd0a558SYan, Zheng 	int index = 0;
20975d4f98a2SYan Zheng 
20983fd0a558SYan, Zheng 	BUG_ON(reserve && node->processed);
20993fd0a558SYan, Zheng 
21003fd0a558SYan, Zheng 	while (next) {
21013fd0a558SYan, Zheng 		cond_resched();
21025d4f98a2SYan Zheng 		while (1) {
21033fd0a558SYan, Zheng 			if (next->processed && (reserve || next != node))
21045d4f98a2SYan Zheng 				break;
21055d4f98a2SYan Zheng 
21060b246afaSJeff Mahoney 			num_bytes += fs_info->nodesize;
21073fd0a558SYan, Zheng 
21083fd0a558SYan, Zheng 			if (list_empty(&next->upper))
21093fd0a558SYan, Zheng 				break;
21103fd0a558SYan, Zheng 
21113fd0a558SYan, Zheng 			edge = list_entry(next->upper.next,
2112a26195a5SQu Wenruo 					struct btrfs_backref_edge, list[LOWER]);
21133fd0a558SYan, Zheng 			edges[index++] = edge;
21143fd0a558SYan, Zheng 			next = edge->node[UPPER];
21155d4f98a2SYan Zheng 		}
21163fd0a558SYan, Zheng 		next = walk_down_backref(edges, &index);
21173fd0a558SYan, Zheng 	}
21183fd0a558SYan, Zheng 	return num_bytes;
21193fd0a558SYan, Zheng }
21203fd0a558SYan, Zheng 
21213fd0a558SYan, Zheng static int reserve_metadata_space(struct btrfs_trans_handle *trans,
21223fd0a558SYan, Zheng 				  struct reloc_control *rc,
2123a26195a5SQu Wenruo 				  struct btrfs_backref_node *node)
21243fd0a558SYan, Zheng {
21253fd0a558SYan, Zheng 	struct btrfs_root *root = rc->extent_root;
2126da17066cSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
21273fd0a558SYan, Zheng 	u64 num_bytes;
21283fd0a558SYan, Zheng 	int ret;
21290647bf56SWang Shilong 	u64 tmp;
21303fd0a558SYan, Zheng 
21313fd0a558SYan, Zheng 	num_bytes = calcu_metadata_size(rc, node, 1) * 2;
21323fd0a558SYan, Zheng 
21333fd0a558SYan, Zheng 	trans->block_rsv = rc->block_rsv;
21340647bf56SWang Shilong 	rc->reserved_bytes += num_bytes;
21358ca17f0fSJosef Bacik 
21368ca17f0fSJosef Bacik 	/*
21378ca17f0fSJosef Bacik 	 * We are under a transaction here so we can only do limited flushing.
21388ca17f0fSJosef Bacik 	 * If we get an enospc just kick back -EAGAIN so we know to drop the
21398ca17f0fSJosef Bacik 	 * transaction and try to refill when we can flush all the things.
21408ca17f0fSJosef Bacik 	 */
21410647bf56SWang Shilong 	ret = btrfs_block_rsv_refill(root, rc->block_rsv, num_bytes,
21428ca17f0fSJosef Bacik 				BTRFS_RESERVE_FLUSH_LIMIT);
21433fd0a558SYan, Zheng 	if (ret) {
2144da17066cSJeff Mahoney 		tmp = fs_info->nodesize * RELOCATION_RESERVED_NODES;
21450647bf56SWang Shilong 		while (tmp <= rc->reserved_bytes)
21460647bf56SWang Shilong 			tmp <<= 1;
21470647bf56SWang Shilong 		/*
21480647bf56SWang Shilong 		 * only one thread can access block_rsv at this point,
21490647bf56SWang Shilong 		 * so we don't need hold lock to protect block_rsv.
21500647bf56SWang Shilong 		 * we expand more reservation size here to allow enough
215152042d8eSAndrea Gelmini 		 * space for relocation and we will return earlier in
21520647bf56SWang Shilong 		 * enospc case.
21530647bf56SWang Shilong 		 */
2154da17066cSJeff Mahoney 		rc->block_rsv->size = tmp + fs_info->nodesize *
21550647bf56SWang Shilong 				      RELOCATION_RESERVED_NODES;
21568ca17f0fSJosef Bacik 		return -EAGAIN;
21573fd0a558SYan, Zheng 	}
21583fd0a558SYan, Zheng 
21593fd0a558SYan, Zheng 	return 0;
21603fd0a558SYan, Zheng }
21613fd0a558SYan, Zheng 
21625d4f98a2SYan Zheng /*
21635d4f98a2SYan Zheng  * relocate a block tree, and then update pointers in upper level
21645d4f98a2SYan Zheng  * blocks that reference the block to point to the new location.
21655d4f98a2SYan Zheng  *
21665d4f98a2SYan Zheng  * if called by link_to_upper, the block has already been relocated.
21675d4f98a2SYan Zheng  * in that case this function just updates pointers.
21685d4f98a2SYan Zheng  */
21695d4f98a2SYan Zheng static int do_relocation(struct btrfs_trans_handle *trans,
21703fd0a558SYan, Zheng 			 struct reloc_control *rc,
2171a26195a5SQu Wenruo 			 struct btrfs_backref_node *node,
21725d4f98a2SYan Zheng 			 struct btrfs_key *key,
21735d4f98a2SYan Zheng 			 struct btrfs_path *path, int lowest)
21745d4f98a2SYan Zheng {
2175a26195a5SQu Wenruo 	struct btrfs_backref_node *upper;
2176a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
2177a26195a5SQu Wenruo 	struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
21785d4f98a2SYan Zheng 	struct btrfs_root *root;
21795d4f98a2SYan Zheng 	struct extent_buffer *eb;
21805d4f98a2SYan Zheng 	u32 blocksize;
21815d4f98a2SYan Zheng 	u64 bytenr;
21825d4f98a2SYan Zheng 	int slot;
21838df01fddSNikolay Borisov 	int ret = 0;
21845d4f98a2SYan Zheng 
21855d4f98a2SYan Zheng 	BUG_ON(lowest && node->eb);
21865d4f98a2SYan Zheng 
21875d4f98a2SYan Zheng 	path->lowest_level = node->level + 1;
21883fd0a558SYan, Zheng 	rc->backref_cache.path[node->level] = node;
21895d4f98a2SYan Zheng 	list_for_each_entry(edge, &node->upper, list[LOWER]) {
219082fa113fSQu Wenruo 		struct btrfs_ref ref = { 0 };
2191581c1760SQu Wenruo 
21925d4f98a2SYan Zheng 		cond_resched();
21935d4f98a2SYan Zheng 
21945d4f98a2SYan Zheng 		upper = edge->node[UPPER];
2195dc4103f9SWang Shilong 		root = select_reloc_root(trans, rc, upper, edges);
21963fd0a558SYan, Zheng 		BUG_ON(!root);
21975d4f98a2SYan Zheng 
21983fd0a558SYan, Zheng 		if (upper->eb && !upper->locked) {
21993fd0a558SYan, Zheng 			if (!lowest) {
2200e3b83361SQu Wenruo 				ret = btrfs_bin_search(upper->eb, key, &slot);
22018df01fddSNikolay Borisov 				if (ret < 0)
2202cbca7d59SFilipe Manana 					goto next;
22033fd0a558SYan, Zheng 				BUG_ON(ret);
22043fd0a558SYan, Zheng 				bytenr = btrfs_node_blockptr(upper->eb, slot);
22053fd0a558SYan, Zheng 				if (node->eb->start == bytenr)
22063fd0a558SYan, Zheng 					goto next;
22073fd0a558SYan, Zheng 			}
2208b0fe7078SQu Wenruo 			btrfs_backref_drop_node_buffer(upper);
22093fd0a558SYan, Zheng 		}
22105d4f98a2SYan Zheng 
22115d4f98a2SYan Zheng 		if (!upper->eb) {
22125d4f98a2SYan Zheng 			ret = btrfs_search_slot(trans, root, key, path, 0, 1);
22133561b9dbSLiu Bo 			if (ret) {
22148df01fddSNikolay Borisov 				if (ret > 0)
22158df01fddSNikolay Borisov 					ret = -ENOENT;
22163561b9dbSLiu Bo 
22173561b9dbSLiu Bo 				btrfs_release_path(path);
22185d4f98a2SYan Zheng 				break;
22195d4f98a2SYan Zheng 			}
22205d4f98a2SYan Zheng 
22213fd0a558SYan, Zheng 			if (!upper->eb) {
22223fd0a558SYan, Zheng 				upper->eb = path->nodes[upper->level];
22233fd0a558SYan, Zheng 				path->nodes[upper->level] = NULL;
22243fd0a558SYan, Zheng 			} else {
22253fd0a558SYan, Zheng 				BUG_ON(upper->eb != path->nodes[upper->level]);
22263fd0a558SYan, Zheng 			}
22273fd0a558SYan, Zheng 
22283fd0a558SYan, Zheng 			upper->locked = 1;
22293fd0a558SYan, Zheng 			path->locks[upper->level] = 0;
22303fd0a558SYan, Zheng 
22315d4f98a2SYan Zheng 			slot = path->slots[upper->level];
2232b3b4aa74SDavid Sterba 			btrfs_release_path(path);
22335d4f98a2SYan Zheng 		} else {
2234e3b83361SQu Wenruo 			ret = btrfs_bin_search(upper->eb, key, &slot);
22358df01fddSNikolay Borisov 			if (ret < 0)
2236cbca7d59SFilipe Manana 				goto next;
22375d4f98a2SYan Zheng 			BUG_ON(ret);
22385d4f98a2SYan Zheng 		}
22395d4f98a2SYan Zheng 
22405d4f98a2SYan Zheng 		bytenr = btrfs_node_blockptr(upper->eb, slot);
22413fd0a558SYan, Zheng 		if (lowest) {
22424547f4d8SLiu Bo 			if (bytenr != node->bytenr) {
22434547f4d8SLiu Bo 				btrfs_err(root->fs_info,
22444547f4d8SLiu Bo 		"lowest leaf/node mismatch: bytenr %llu node->bytenr %llu slot %d upper %llu",
22454547f4d8SLiu Bo 					  bytenr, node->bytenr, slot,
22464547f4d8SLiu Bo 					  upper->eb->start);
22478df01fddSNikolay Borisov 				ret = -EIO;
22484547f4d8SLiu Bo 				goto next;
22494547f4d8SLiu Bo 			}
22505d4f98a2SYan Zheng 		} else {
22513fd0a558SYan, Zheng 			if (node->eb->start == bytenr)
22523fd0a558SYan, Zheng 				goto next;
22535d4f98a2SYan Zheng 		}
22545d4f98a2SYan Zheng 
2255da17066cSJeff Mahoney 		blocksize = root->fs_info->nodesize;
2256c9752536SJosef Bacik 		eb = btrfs_read_node_slot(upper->eb, slot);
225764c043deSLiu Bo 		if (IS_ERR(eb)) {
22588df01fddSNikolay Borisov 			ret = PTR_ERR(eb);
225964c043deSLiu Bo 			goto next;
226097d9a8a4STsutomu Itoh 		}
22615d4f98a2SYan Zheng 		btrfs_tree_lock(eb);
22625d4f98a2SYan Zheng 
22635d4f98a2SYan Zheng 		if (!node->eb) {
22645d4f98a2SYan Zheng 			ret = btrfs_cow_block(trans, root, eb, upper->eb,
22659631e4ccSJosef Bacik 					      slot, &eb, BTRFS_NESTING_COW);
22663fd0a558SYan, Zheng 			btrfs_tree_unlock(eb);
22673fd0a558SYan, Zheng 			free_extent_buffer(eb);
22688df01fddSNikolay Borisov 			if (ret < 0)
22693fd0a558SYan, Zheng 				goto next;
22703fd0a558SYan, Zheng 			BUG_ON(node->eb != eb);
22715d4f98a2SYan Zheng 		} else {
22725d4f98a2SYan Zheng 			btrfs_set_node_blockptr(upper->eb, slot,
22735d4f98a2SYan Zheng 						node->eb->start);
22745d4f98a2SYan Zheng 			btrfs_set_node_ptr_generation(upper->eb, slot,
22755d4f98a2SYan Zheng 						      trans->transid);
22765d4f98a2SYan Zheng 			btrfs_mark_buffer_dirty(upper->eb);
22775d4f98a2SYan Zheng 
227882fa113fSQu Wenruo 			btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF,
22795d4f98a2SYan Zheng 					       node->eb->start, blocksize,
228082fa113fSQu Wenruo 					       upper->eb->start);
228182fa113fSQu Wenruo 			ref.real_root = root->root_key.objectid;
228282fa113fSQu Wenruo 			btrfs_init_tree_ref(&ref, node->level,
228382fa113fSQu Wenruo 					    btrfs_header_owner(upper->eb));
228482fa113fSQu Wenruo 			ret = btrfs_inc_extent_ref(trans, &ref);
22855d4f98a2SYan Zheng 			BUG_ON(ret);
22865d4f98a2SYan Zheng 
22875d4f98a2SYan Zheng 			ret = btrfs_drop_subtree(trans, root, eb, upper->eb);
22885d4f98a2SYan Zheng 			BUG_ON(ret);
22895d4f98a2SYan Zheng 		}
22903fd0a558SYan, Zheng next:
22913fd0a558SYan, Zheng 		if (!upper->pending)
2292b0fe7078SQu Wenruo 			btrfs_backref_drop_node_buffer(upper);
22933fd0a558SYan, Zheng 		else
2294b0fe7078SQu Wenruo 			btrfs_backref_unlock_node_buffer(upper);
22958df01fddSNikolay Borisov 		if (ret)
22963fd0a558SYan, Zheng 			break;
22975d4f98a2SYan Zheng 	}
22983fd0a558SYan, Zheng 
22998df01fddSNikolay Borisov 	if (!ret && node->pending) {
2300b0fe7078SQu Wenruo 		btrfs_backref_drop_node_buffer(node);
23013fd0a558SYan, Zheng 		list_move_tail(&node->list, &rc->backref_cache.changed);
23023fd0a558SYan, Zheng 		node->pending = 0;
23035d4f98a2SYan Zheng 	}
23043fd0a558SYan, Zheng 
23055d4f98a2SYan Zheng 	path->lowest_level = 0;
23068df01fddSNikolay Borisov 	BUG_ON(ret == -ENOSPC);
23078df01fddSNikolay Borisov 	return ret;
23085d4f98a2SYan Zheng }
23095d4f98a2SYan Zheng 
23105d4f98a2SYan Zheng static int link_to_upper(struct btrfs_trans_handle *trans,
23113fd0a558SYan, Zheng 			 struct reloc_control *rc,
2312a26195a5SQu Wenruo 			 struct btrfs_backref_node *node,
23135d4f98a2SYan Zheng 			 struct btrfs_path *path)
23145d4f98a2SYan Zheng {
23155d4f98a2SYan Zheng 	struct btrfs_key key;
23165d4f98a2SYan Zheng 
23175d4f98a2SYan Zheng 	btrfs_node_key_to_cpu(node->eb, &key, 0);
23183fd0a558SYan, Zheng 	return do_relocation(trans, rc, node, &key, path, 0);
23195d4f98a2SYan Zheng }
23205d4f98a2SYan Zheng 
23215d4f98a2SYan Zheng static int finish_pending_nodes(struct btrfs_trans_handle *trans,
23223fd0a558SYan, Zheng 				struct reloc_control *rc,
23233fd0a558SYan, Zheng 				struct btrfs_path *path, int err)
23245d4f98a2SYan Zheng {
23253fd0a558SYan, Zheng 	LIST_HEAD(list);
2326a26195a5SQu Wenruo 	struct btrfs_backref_cache *cache = &rc->backref_cache;
2327a26195a5SQu Wenruo 	struct btrfs_backref_node *node;
23285d4f98a2SYan Zheng 	int level;
23295d4f98a2SYan Zheng 	int ret;
23305d4f98a2SYan Zheng 
23315d4f98a2SYan Zheng 	for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
23325d4f98a2SYan Zheng 		while (!list_empty(&cache->pending[level])) {
23335d4f98a2SYan Zheng 			node = list_entry(cache->pending[level].next,
2334a26195a5SQu Wenruo 					  struct btrfs_backref_node, list);
23353fd0a558SYan, Zheng 			list_move_tail(&node->list, &list);
23363fd0a558SYan, Zheng 			BUG_ON(!node->pending);
23375d4f98a2SYan Zheng 
23383fd0a558SYan, Zheng 			if (!err) {
23393fd0a558SYan, Zheng 				ret = link_to_upper(trans, rc, node, path);
23405d4f98a2SYan Zheng 				if (ret < 0)
23415d4f98a2SYan Zheng 					err = ret;
23425d4f98a2SYan Zheng 			}
23435d4f98a2SYan Zheng 		}
23443fd0a558SYan, Zheng 		list_splice_init(&list, &cache->pending[level]);
23453fd0a558SYan, Zheng 	}
23465d4f98a2SYan Zheng 	return err;
23475d4f98a2SYan Zheng }
23485d4f98a2SYan Zheng 
23495d4f98a2SYan Zheng /*
23505d4f98a2SYan Zheng  * mark a block and all blocks directly/indirectly reference the block
23515d4f98a2SYan Zheng  * as processed.
23525d4f98a2SYan Zheng  */
23535d4f98a2SYan Zheng static void update_processed_blocks(struct reloc_control *rc,
2354a26195a5SQu Wenruo 				    struct btrfs_backref_node *node)
23555d4f98a2SYan Zheng {
2356a26195a5SQu Wenruo 	struct btrfs_backref_node *next = node;
2357a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
2358a26195a5SQu Wenruo 	struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
23595d4f98a2SYan Zheng 	int index = 0;
23605d4f98a2SYan Zheng 
23615d4f98a2SYan Zheng 	while (next) {
23625d4f98a2SYan Zheng 		cond_resched();
23635d4f98a2SYan Zheng 		while (1) {
23645d4f98a2SYan Zheng 			if (next->processed)
23655d4f98a2SYan Zheng 				break;
23665d4f98a2SYan Zheng 
23679569cc20SQu Wenruo 			mark_block_processed(rc, next);
23685d4f98a2SYan Zheng 
23695d4f98a2SYan Zheng 			if (list_empty(&next->upper))
23705d4f98a2SYan Zheng 				break;
23715d4f98a2SYan Zheng 
23725d4f98a2SYan Zheng 			edge = list_entry(next->upper.next,
2373a26195a5SQu Wenruo 					struct btrfs_backref_edge, list[LOWER]);
23745d4f98a2SYan Zheng 			edges[index++] = edge;
23755d4f98a2SYan Zheng 			next = edge->node[UPPER];
23765d4f98a2SYan Zheng 		}
23775d4f98a2SYan Zheng 		next = walk_down_backref(edges, &index);
23785d4f98a2SYan Zheng 	}
23795d4f98a2SYan Zheng }
23805d4f98a2SYan Zheng 
23817476dfdaSDavid Sterba static int tree_block_processed(u64 bytenr, struct reloc_control *rc)
23825d4f98a2SYan Zheng {
2383da17066cSJeff Mahoney 	u32 blocksize = rc->extent_root->fs_info->nodesize;
23847476dfdaSDavid Sterba 
23855d4f98a2SYan Zheng 	if (test_range_bit(&rc->processed_blocks, bytenr,
23869655d298SChris Mason 			   bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL))
23875d4f98a2SYan Zheng 		return 1;
23885d4f98a2SYan Zheng 	return 0;
23895d4f98a2SYan Zheng }
23905d4f98a2SYan Zheng 
23912ff7e61eSJeff Mahoney static int get_tree_block_key(struct btrfs_fs_info *fs_info,
23925d4f98a2SYan Zheng 			      struct tree_block *block)
23935d4f98a2SYan Zheng {
23945d4f98a2SYan Zheng 	struct extent_buffer *eb;
23955d4f98a2SYan Zheng 
23961b7ec85eSJosef Bacik 	eb = read_tree_block(fs_info, block->bytenr, 0, block->key.offset,
2397581c1760SQu Wenruo 			     block->level, NULL);
239864c043deSLiu Bo 	if (IS_ERR(eb)) {
239964c043deSLiu Bo 		return PTR_ERR(eb);
240064c043deSLiu Bo 	} else if (!extent_buffer_uptodate(eb)) {
2401416bc658SJosef Bacik 		free_extent_buffer(eb);
2402416bc658SJosef Bacik 		return -EIO;
2403416bc658SJosef Bacik 	}
24045d4f98a2SYan Zheng 	if (block->level == 0)
24055d4f98a2SYan Zheng 		btrfs_item_key_to_cpu(eb, &block->key, 0);
24065d4f98a2SYan Zheng 	else
24075d4f98a2SYan Zheng 		btrfs_node_key_to_cpu(eb, &block->key, 0);
24085d4f98a2SYan Zheng 	free_extent_buffer(eb);
24095d4f98a2SYan Zheng 	block->key_ready = 1;
24105d4f98a2SYan Zheng 	return 0;
24115d4f98a2SYan Zheng }
24125d4f98a2SYan Zheng 
24135d4f98a2SYan Zheng /*
24145d4f98a2SYan Zheng  * helper function to relocate a tree block
24155d4f98a2SYan Zheng  */
24165d4f98a2SYan Zheng static int relocate_tree_block(struct btrfs_trans_handle *trans,
24175d4f98a2SYan Zheng 				struct reloc_control *rc,
2418a26195a5SQu Wenruo 				struct btrfs_backref_node *node,
24195d4f98a2SYan Zheng 				struct btrfs_key *key,
24205d4f98a2SYan Zheng 				struct btrfs_path *path)
24215d4f98a2SYan Zheng {
24225d4f98a2SYan Zheng 	struct btrfs_root *root;
24233fd0a558SYan, Zheng 	int ret = 0;
24245d4f98a2SYan Zheng 
24253fd0a558SYan, Zheng 	if (!node)
24265d4f98a2SYan Zheng 		return 0;
24273fd0a558SYan, Zheng 
24285f6b2e5cSJosef Bacik 	/*
24295f6b2e5cSJosef Bacik 	 * If we fail here we want to drop our backref_node because we are going
24305f6b2e5cSJosef Bacik 	 * to start over and regenerate the tree for it.
24315f6b2e5cSJosef Bacik 	 */
24325f6b2e5cSJosef Bacik 	ret = reserve_metadata_space(trans, rc, node);
24335f6b2e5cSJosef Bacik 	if (ret)
24345f6b2e5cSJosef Bacik 		goto out;
24355f6b2e5cSJosef Bacik 
24363fd0a558SYan, Zheng 	BUG_ON(node->processed);
2437147d256eSZhaolei 	root = select_one_root(node);
24383fd0a558SYan, Zheng 	if (root == ERR_PTR(-ENOENT)) {
24393fd0a558SYan, Zheng 		update_processed_blocks(rc, node);
24403fd0a558SYan, Zheng 		goto out;
24415d4f98a2SYan Zheng 	}
24425d4f98a2SYan Zheng 
24433fd0a558SYan, Zheng 	if (root) {
244492a7cc42SQu Wenruo 		if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
24453fd0a558SYan, Zheng 			BUG_ON(node->new_bytenr);
24463fd0a558SYan, Zheng 			BUG_ON(!list_empty(&node->list));
24473fd0a558SYan, Zheng 			btrfs_record_root_in_trans(trans, root);
24483fd0a558SYan, Zheng 			root = root->reloc_root;
24493fd0a558SYan, Zheng 			node->new_bytenr = root->node->start;
245000246528SJosef Bacik 			btrfs_put_root(node->root);
245100246528SJosef Bacik 			node->root = btrfs_grab_root(root);
24520b530bc5SJosef Bacik 			ASSERT(node->root);
24533fd0a558SYan, Zheng 			list_add_tail(&node->list, &rc->backref_cache.changed);
24543fd0a558SYan, Zheng 		} else {
24555d4f98a2SYan Zheng 			path->lowest_level = node->level;
24565d4f98a2SYan Zheng 			ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2457b3b4aa74SDavid Sterba 			btrfs_release_path(path);
24583fd0a558SYan, Zheng 			if (ret > 0)
24595d4f98a2SYan Zheng 				ret = 0;
24603fd0a558SYan, Zheng 		}
24613fd0a558SYan, Zheng 		if (!ret)
24623fd0a558SYan, Zheng 			update_processed_blocks(rc, node);
24633fd0a558SYan, Zheng 	} else {
24643fd0a558SYan, Zheng 		ret = do_relocation(trans, rc, node, key, path, 1);
24653fd0a558SYan, Zheng 	}
24665d4f98a2SYan Zheng out:
24670647bf56SWang Shilong 	if (ret || node->level == 0 || node->cowonly)
2468023acb07SQu Wenruo 		btrfs_backref_cleanup_node(&rc->backref_cache, node);
24695d4f98a2SYan Zheng 	return ret;
24705d4f98a2SYan Zheng }
24715d4f98a2SYan Zheng 
24725d4f98a2SYan Zheng /*
24735d4f98a2SYan Zheng  * relocate a list of blocks
24745d4f98a2SYan Zheng  */
24755d4f98a2SYan Zheng static noinline_for_stack
24765d4f98a2SYan Zheng int relocate_tree_blocks(struct btrfs_trans_handle *trans,
24775d4f98a2SYan Zheng 			 struct reloc_control *rc, struct rb_root *blocks)
24785d4f98a2SYan Zheng {
24792ff7e61eSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2480a26195a5SQu Wenruo 	struct btrfs_backref_node *node;
24815d4f98a2SYan Zheng 	struct btrfs_path *path;
24825d4f98a2SYan Zheng 	struct tree_block *block;
248398ff7b94SQu Wenruo 	struct tree_block *next;
24845d4f98a2SYan Zheng 	int ret;
24855d4f98a2SYan Zheng 	int err = 0;
24865d4f98a2SYan Zheng 
24875d4f98a2SYan Zheng 	path = btrfs_alloc_path();
2488e1a12670SLiu Bo 	if (!path) {
2489e1a12670SLiu Bo 		err = -ENOMEM;
249034c2b290SDavid Sterba 		goto out_free_blocks;
2491e1a12670SLiu Bo 	}
24925d4f98a2SYan Zheng 
249398ff7b94SQu Wenruo 	/* Kick in readahead for tree blocks with missing keys */
249498ff7b94SQu Wenruo 	rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
24955d4f98a2SYan Zheng 		if (!block->key_ready)
24963fbaf258SJosef Bacik 			btrfs_readahead_tree_block(fs_info, block->bytenr, 0, 0,
24973fbaf258SJosef Bacik 						   block->level);
24985d4f98a2SYan Zheng 	}
24995d4f98a2SYan Zheng 
250098ff7b94SQu Wenruo 	/* Get first keys */
250198ff7b94SQu Wenruo 	rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
250234c2b290SDavid Sterba 		if (!block->key_ready) {
25032ff7e61eSJeff Mahoney 			err = get_tree_block_key(fs_info, block);
250434c2b290SDavid Sterba 			if (err)
250534c2b290SDavid Sterba 				goto out_free_path;
250634c2b290SDavid Sterba 		}
25075d4f98a2SYan Zheng 	}
25085d4f98a2SYan Zheng 
250998ff7b94SQu Wenruo 	/* Do tree relocation */
251098ff7b94SQu Wenruo 	rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
25113fd0a558SYan, Zheng 		node = build_backref_tree(rc, &block->key,
25125d4f98a2SYan Zheng 					  block->level, block->bytenr);
25135d4f98a2SYan Zheng 		if (IS_ERR(node)) {
25145d4f98a2SYan Zheng 			err = PTR_ERR(node);
25155d4f98a2SYan Zheng 			goto out;
25165d4f98a2SYan Zheng 		}
25175d4f98a2SYan Zheng 
25185d4f98a2SYan Zheng 		ret = relocate_tree_block(trans, rc, node, &block->key,
25195d4f98a2SYan Zheng 					  path);
25205d4f98a2SYan Zheng 		if (ret < 0) {
25215d4f98a2SYan Zheng 			err = ret;
252250dbbb71SJosef Bacik 			break;
25235d4f98a2SYan Zheng 		}
25245d4f98a2SYan Zheng 	}
25255d4f98a2SYan Zheng out:
25263fd0a558SYan, Zheng 	err = finish_pending_nodes(trans, rc, path, err);
25275d4f98a2SYan Zheng 
252834c2b290SDavid Sterba out_free_path:
25295d4f98a2SYan Zheng 	btrfs_free_path(path);
253034c2b290SDavid Sterba out_free_blocks:
2531e1a12670SLiu Bo 	free_block_list(blocks);
25325d4f98a2SYan Zheng 	return err;
25335d4f98a2SYan Zheng }
25345d4f98a2SYan Zheng 
2535056d9becSNikolay Borisov static noinline_for_stack int prealloc_file_extent_cluster(
2536056d9becSNikolay Borisov 				struct btrfs_inode *inode,
2537efa56464SYan, Zheng 				struct file_extent_cluster *cluster)
2538efa56464SYan, Zheng {
2539efa56464SYan, Zheng 	u64 alloc_hint = 0;
2540efa56464SYan, Zheng 	u64 start;
2541efa56464SYan, Zheng 	u64 end;
2542056d9becSNikolay Borisov 	u64 offset = inode->index_cnt;
2543efa56464SYan, Zheng 	u64 num_bytes;
25444e9d0d01SNikolay Borisov 	int nr;
2545efa56464SYan, Zheng 	int ret = 0;
2546dcb40c19SWang Xiaoguang 	u64 prealloc_start = cluster->start - offset;
2547dcb40c19SWang Xiaoguang 	u64 prealloc_end = cluster->end - offset;
2548214e61d0SNikolay Borisov 	u64 cur_offset = prealloc_start;
2549efa56464SYan, Zheng 
2550efa56464SYan, Zheng 	BUG_ON(cluster->start != cluster->boundary[0]);
2551056d9becSNikolay Borisov 	ret = btrfs_alloc_data_chunk_ondemand(inode,
2552dcb40c19SWang Xiaoguang 					      prealloc_end + 1 - prealloc_start);
2553efa56464SYan, Zheng 	if (ret)
2554214e61d0SNikolay Borisov 		return ret;
2555efa56464SYan, Zheng 
2556056d9becSNikolay Borisov 	inode_lock(&inode->vfs_inode);
25574e9d0d01SNikolay Borisov 	for (nr = 0; nr < cluster->nr; nr++) {
2558efa56464SYan, Zheng 		start = cluster->boundary[nr] - offset;
2559efa56464SYan, Zheng 		if (nr + 1 < cluster->nr)
2560efa56464SYan, Zheng 			end = cluster->boundary[nr + 1] - 1 - offset;
2561efa56464SYan, Zheng 		else
2562efa56464SYan, Zheng 			end = cluster->end - offset;
2563efa56464SYan, Zheng 
2564056d9becSNikolay Borisov 		lock_extent(&inode->io_tree, start, end);
2565efa56464SYan, Zheng 		num_bytes = end + 1 - start;
2566056d9becSNikolay Borisov 		ret = btrfs_prealloc_file_range(&inode->vfs_inode, 0, start,
2567efa56464SYan, Zheng 						num_bytes, num_bytes,
2568efa56464SYan, Zheng 						end + 1, &alloc_hint);
256918513091SWang Xiaoguang 		cur_offset = end + 1;
2570056d9becSNikolay Borisov 		unlock_extent(&inode->io_tree, start, end);
2571efa56464SYan, Zheng 		if (ret)
2572efa56464SYan, Zheng 			break;
2573efa56464SYan, Zheng 	}
2574056d9becSNikolay Borisov 	inode_unlock(&inode->vfs_inode);
2575214e61d0SNikolay Borisov 
257618513091SWang Xiaoguang 	if (cur_offset < prealloc_end)
2577056d9becSNikolay Borisov 		btrfs_free_reserved_data_space_noquota(inode->root->fs_info,
2578a89ef455SFilipe Manana 					       prealloc_end + 1 - cur_offset);
2579efa56464SYan, Zheng 	return ret;
2580efa56464SYan, Zheng }
2581efa56464SYan, Zheng 
2582efa56464SYan, Zheng static noinline_for_stack
25830257bb82SYan, Zheng int setup_extent_mapping(struct inode *inode, u64 start, u64 end,
25840257bb82SYan, Zheng 			 u64 block_start)
25855d4f98a2SYan Zheng {
25865d4f98a2SYan Zheng 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
25875d4f98a2SYan Zheng 	struct extent_map *em;
25880257bb82SYan, Zheng 	int ret = 0;
25895d4f98a2SYan Zheng 
2590172ddd60SDavid Sterba 	em = alloc_extent_map();
25910257bb82SYan, Zheng 	if (!em)
25920257bb82SYan, Zheng 		return -ENOMEM;
25930257bb82SYan, Zheng 
25945d4f98a2SYan Zheng 	em->start = start;
25950257bb82SYan, Zheng 	em->len = end + 1 - start;
25960257bb82SYan, Zheng 	em->block_len = em->len;
25970257bb82SYan, Zheng 	em->block_start = block_start;
25985d4f98a2SYan Zheng 	set_bit(EXTENT_FLAG_PINNED, &em->flags);
25995d4f98a2SYan Zheng 
2600d0082371SJeff Mahoney 	lock_extent(&BTRFS_I(inode)->io_tree, start, end);
26015d4f98a2SYan Zheng 	while (1) {
2602890871beSChris Mason 		write_lock(&em_tree->lock);
260309a2a8f9SJosef Bacik 		ret = add_extent_mapping(em_tree, em, 0);
2604890871beSChris Mason 		write_unlock(&em_tree->lock);
26055d4f98a2SYan Zheng 		if (ret != -EEXIST) {
26065d4f98a2SYan Zheng 			free_extent_map(em);
26075d4f98a2SYan Zheng 			break;
26085d4f98a2SYan Zheng 		}
2609dcdbc059SNikolay Borisov 		btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
26105d4f98a2SYan Zheng 	}
2611d0082371SJeff Mahoney 	unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
26120257bb82SYan, Zheng 	return ret;
26130257bb82SYan, Zheng }
26145d4f98a2SYan Zheng 
2615726a3421SQu Wenruo /*
2616726a3421SQu Wenruo  * Allow error injection to test balance cancellation
2617726a3421SQu Wenruo  */
2618*1fec12a5SJosef Bacik noinline int btrfs_should_cancel_balance(struct btrfs_fs_info *fs_info)
2619726a3421SQu Wenruo {
26205cb502f4SQu Wenruo 	return atomic_read(&fs_info->balance_cancel_req) ||
26215cb502f4SQu Wenruo 		fatal_signal_pending(current);
2622726a3421SQu Wenruo }
2623726a3421SQu Wenruo ALLOW_ERROR_INJECTION(btrfs_should_cancel_balance, TRUE);
2624726a3421SQu Wenruo 
26250257bb82SYan, Zheng static int relocate_file_extent_cluster(struct inode *inode,
26260257bb82SYan, Zheng 					struct file_extent_cluster *cluster)
26270257bb82SYan, Zheng {
26282ff7e61eSJeff Mahoney 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
26290257bb82SYan, Zheng 	u64 page_start;
26300257bb82SYan, Zheng 	u64 page_end;
26310257bb82SYan, Zheng 	u64 offset = BTRFS_I(inode)->index_cnt;
26320257bb82SYan, Zheng 	unsigned long index;
26330257bb82SYan, Zheng 	unsigned long last_index;
26340257bb82SYan, Zheng 	struct page *page;
26350257bb82SYan, Zheng 	struct file_ra_state *ra;
26363b16a4e3SJosef Bacik 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
26370257bb82SYan, Zheng 	int nr = 0;
26380257bb82SYan, Zheng 	int ret = 0;
26390257bb82SYan, Zheng 
26400257bb82SYan, Zheng 	if (!cluster->nr)
26410257bb82SYan, Zheng 		return 0;
26420257bb82SYan, Zheng 
26430257bb82SYan, Zheng 	ra = kzalloc(sizeof(*ra), GFP_NOFS);
26440257bb82SYan, Zheng 	if (!ra)
26450257bb82SYan, Zheng 		return -ENOMEM;
26460257bb82SYan, Zheng 
2647056d9becSNikolay Borisov 	ret = prealloc_file_extent_cluster(BTRFS_I(inode), cluster);
26480257bb82SYan, Zheng 	if (ret)
2649efa56464SYan, Zheng 		goto out;
26500257bb82SYan, Zheng 
26510257bb82SYan, Zheng 	file_ra_state_init(ra, inode->i_mapping);
26520257bb82SYan, Zheng 
2653efa56464SYan, Zheng 	ret = setup_extent_mapping(inode, cluster->start - offset,
2654efa56464SYan, Zheng 				   cluster->end - offset, cluster->start);
2655efa56464SYan, Zheng 	if (ret)
2656efa56464SYan, Zheng 		goto out;
2657efa56464SYan, Zheng 
265809cbfeafSKirill A. Shutemov 	index = (cluster->start - offset) >> PAGE_SHIFT;
265909cbfeafSKirill A. Shutemov 	last_index = (cluster->end - offset) >> PAGE_SHIFT;
26600257bb82SYan, Zheng 	while (index <= last_index) {
26619f3db423SNikolay Borisov 		ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode),
26629f3db423SNikolay Borisov 				PAGE_SIZE);
2663efa56464SYan, Zheng 		if (ret)
2664efa56464SYan, Zheng 			goto out;
2665efa56464SYan, Zheng 
26660257bb82SYan, Zheng 		page = find_lock_page(inode->i_mapping, index);
26670257bb82SYan, Zheng 		if (!page) {
26680257bb82SYan, Zheng 			page_cache_sync_readahead(inode->i_mapping,
26690257bb82SYan, Zheng 						  ra, NULL, index,
26700257bb82SYan, Zheng 						  last_index + 1 - index);
2671a94733d0SJosef Bacik 			page = find_or_create_page(inode->i_mapping, index,
26723b16a4e3SJosef Bacik 						   mask);
26730257bb82SYan, Zheng 			if (!page) {
2674691fa059SNikolay Borisov 				btrfs_delalloc_release_metadata(BTRFS_I(inode),
267543b18595SQu Wenruo 							PAGE_SIZE, true);
267644db1216SFilipe Manana 				btrfs_delalloc_release_extents(BTRFS_I(inode),
26778702ba93SQu Wenruo 							PAGE_SIZE);
26780257bb82SYan, Zheng 				ret = -ENOMEM;
2679efa56464SYan, Zheng 				goto out;
26800257bb82SYan, Zheng 			}
26810257bb82SYan, Zheng 		}
26820257bb82SYan, Zheng 
26830257bb82SYan, Zheng 		if (PageReadahead(page)) {
26840257bb82SYan, Zheng 			page_cache_async_readahead(inode->i_mapping,
26850257bb82SYan, Zheng 						   ra, NULL, page, index,
26860257bb82SYan, Zheng 						   last_index + 1 - index);
26870257bb82SYan, Zheng 		}
26880257bb82SYan, Zheng 
26890257bb82SYan, Zheng 		if (!PageUptodate(page)) {
26900257bb82SYan, Zheng 			btrfs_readpage(NULL, page);
26910257bb82SYan, Zheng 			lock_page(page);
26920257bb82SYan, Zheng 			if (!PageUptodate(page)) {
26930257bb82SYan, Zheng 				unlock_page(page);
269409cbfeafSKirill A. Shutemov 				put_page(page);
2695691fa059SNikolay Borisov 				btrfs_delalloc_release_metadata(BTRFS_I(inode),
269643b18595SQu Wenruo 							PAGE_SIZE, true);
26978b62f87bSJosef Bacik 				btrfs_delalloc_release_extents(BTRFS_I(inode),
26988702ba93SQu Wenruo 							       PAGE_SIZE);
26990257bb82SYan, Zheng 				ret = -EIO;
2700efa56464SYan, Zheng 				goto out;
27010257bb82SYan, Zheng 			}
27020257bb82SYan, Zheng 		}
27030257bb82SYan, Zheng 
27044eee4fa4SMiao Xie 		page_start = page_offset(page);
270509cbfeafSKirill A. Shutemov 		page_end = page_start + PAGE_SIZE - 1;
27060257bb82SYan, Zheng 
2707d0082371SJeff Mahoney 		lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end);
27080257bb82SYan, Zheng 
27090257bb82SYan, Zheng 		set_page_extent_mapped(page);
27100257bb82SYan, Zheng 
27110257bb82SYan, Zheng 		if (nr < cluster->nr &&
27120257bb82SYan, Zheng 		    page_start + offset == cluster->boundary[nr]) {
27130257bb82SYan, Zheng 			set_extent_bits(&BTRFS_I(inode)->io_tree,
27140257bb82SYan, Zheng 					page_start, page_end,
2715ceeb0ae7SDavid Sterba 					EXTENT_BOUNDARY);
27160257bb82SYan, Zheng 			nr++;
27170257bb82SYan, Zheng 		}
27180257bb82SYan, Zheng 
2719c2566f22SNikolay Borisov 		ret = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start,
2720c2566f22SNikolay Borisov 						page_end, 0, NULL);
2721765f3cebSNikolay Borisov 		if (ret) {
2722765f3cebSNikolay Borisov 			unlock_page(page);
2723765f3cebSNikolay Borisov 			put_page(page);
2724765f3cebSNikolay Borisov 			btrfs_delalloc_release_metadata(BTRFS_I(inode),
272543b18595SQu Wenruo 							 PAGE_SIZE, true);
2726765f3cebSNikolay Borisov 			btrfs_delalloc_release_extents(BTRFS_I(inode),
27278702ba93SQu Wenruo 			                               PAGE_SIZE);
2728765f3cebSNikolay Borisov 
2729765f3cebSNikolay Borisov 			clear_extent_bits(&BTRFS_I(inode)->io_tree,
2730765f3cebSNikolay Borisov 					  page_start, page_end,
2731765f3cebSNikolay Borisov 					  EXTENT_LOCKED | EXTENT_BOUNDARY);
2732765f3cebSNikolay Borisov 			goto out;
2733765f3cebSNikolay Borisov 
2734765f3cebSNikolay Borisov 		}
27350257bb82SYan, Zheng 		set_page_dirty(page);
27360257bb82SYan, Zheng 
27370257bb82SYan, Zheng 		unlock_extent(&BTRFS_I(inode)->io_tree,
2738d0082371SJeff Mahoney 			      page_start, page_end);
27390257bb82SYan, Zheng 		unlock_page(page);
274009cbfeafSKirill A. Shutemov 		put_page(page);
27410257bb82SYan, Zheng 
27420257bb82SYan, Zheng 		index++;
27438702ba93SQu Wenruo 		btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
2744efa56464SYan, Zheng 		balance_dirty_pages_ratelimited(inode->i_mapping);
27452ff7e61eSJeff Mahoney 		btrfs_throttle(fs_info);
27467f913c7cSQu Wenruo 		if (btrfs_should_cancel_balance(fs_info)) {
27477f913c7cSQu Wenruo 			ret = -ECANCELED;
27487f913c7cSQu Wenruo 			goto out;
27497f913c7cSQu Wenruo 		}
27500257bb82SYan, Zheng 	}
27510257bb82SYan, Zheng 	WARN_ON(nr != cluster->nr);
2752efa56464SYan, Zheng out:
27530257bb82SYan, Zheng 	kfree(ra);
27540257bb82SYan, Zheng 	return ret;
27550257bb82SYan, Zheng }
27560257bb82SYan, Zheng 
27570257bb82SYan, Zheng static noinline_for_stack
27580257bb82SYan, Zheng int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key,
27590257bb82SYan, Zheng 			 struct file_extent_cluster *cluster)
27600257bb82SYan, Zheng {
27610257bb82SYan, Zheng 	int ret;
27620257bb82SYan, Zheng 
27630257bb82SYan, Zheng 	if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) {
27640257bb82SYan, Zheng 		ret = relocate_file_extent_cluster(inode, cluster);
27650257bb82SYan, Zheng 		if (ret)
27660257bb82SYan, Zheng 			return ret;
27670257bb82SYan, Zheng 		cluster->nr = 0;
27680257bb82SYan, Zheng 	}
27690257bb82SYan, Zheng 
27700257bb82SYan, Zheng 	if (!cluster->nr)
27710257bb82SYan, Zheng 		cluster->start = extent_key->objectid;
27720257bb82SYan, Zheng 	else
27730257bb82SYan, Zheng 		BUG_ON(cluster->nr >= MAX_EXTENTS);
27740257bb82SYan, Zheng 	cluster->end = extent_key->objectid + extent_key->offset - 1;
27750257bb82SYan, Zheng 	cluster->boundary[cluster->nr] = extent_key->objectid;
27760257bb82SYan, Zheng 	cluster->nr++;
27770257bb82SYan, Zheng 
27780257bb82SYan, Zheng 	if (cluster->nr >= MAX_EXTENTS) {
27790257bb82SYan, Zheng 		ret = relocate_file_extent_cluster(inode, cluster);
27800257bb82SYan, Zheng 		if (ret)
27810257bb82SYan, Zheng 			return ret;
27820257bb82SYan, Zheng 		cluster->nr = 0;
27830257bb82SYan, Zheng 	}
27840257bb82SYan, Zheng 	return 0;
27855d4f98a2SYan Zheng }
27865d4f98a2SYan Zheng 
27875d4f98a2SYan Zheng /*
27885d4f98a2SYan Zheng  * helper to add a tree block to the list.
27895d4f98a2SYan Zheng  * the major work is getting the generation and level of the block
27905d4f98a2SYan Zheng  */
27915d4f98a2SYan Zheng static int add_tree_block(struct reloc_control *rc,
27925d4f98a2SYan Zheng 			  struct btrfs_key *extent_key,
27935d4f98a2SYan Zheng 			  struct btrfs_path *path,
27945d4f98a2SYan Zheng 			  struct rb_root *blocks)
27955d4f98a2SYan Zheng {
27965d4f98a2SYan Zheng 	struct extent_buffer *eb;
27975d4f98a2SYan Zheng 	struct btrfs_extent_item *ei;
27985d4f98a2SYan Zheng 	struct btrfs_tree_block_info *bi;
27995d4f98a2SYan Zheng 	struct tree_block *block;
28005d4f98a2SYan Zheng 	struct rb_node *rb_node;
28015d4f98a2SYan Zheng 	u32 item_size;
28025d4f98a2SYan Zheng 	int level = -1;
28037fdf4b60SWang Shilong 	u64 generation;
28045d4f98a2SYan Zheng 
28055d4f98a2SYan Zheng 	eb =  path->nodes[0];
28065d4f98a2SYan Zheng 	item_size = btrfs_item_size_nr(eb, path->slots[0]);
28075d4f98a2SYan Zheng 
28083173a18fSJosef Bacik 	if (extent_key->type == BTRFS_METADATA_ITEM_KEY ||
28093173a18fSJosef Bacik 	    item_size >= sizeof(*ei) + sizeof(*bi)) {
28105d4f98a2SYan Zheng 		ei = btrfs_item_ptr(eb, path->slots[0],
28115d4f98a2SYan Zheng 				struct btrfs_extent_item);
28123173a18fSJosef Bacik 		if (extent_key->type == BTRFS_EXTENT_ITEM_KEY) {
28135d4f98a2SYan Zheng 			bi = (struct btrfs_tree_block_info *)(ei + 1);
28145d4f98a2SYan Zheng 			level = btrfs_tree_block_level(eb, bi);
28155d4f98a2SYan Zheng 		} else {
28163173a18fSJosef Bacik 			level = (int)extent_key->offset;
28173173a18fSJosef Bacik 		}
28183173a18fSJosef Bacik 		generation = btrfs_extent_generation(eb, ei);
28196d8ff4e4SDavid Sterba 	} else if (unlikely(item_size == sizeof(struct btrfs_extent_item_v0))) {
2820ba3c2b19SNikolay Borisov 		btrfs_print_v0_err(eb->fs_info);
2821ba3c2b19SNikolay Borisov 		btrfs_handle_fs_error(eb->fs_info, -EINVAL, NULL);
2822ba3c2b19SNikolay Borisov 		return -EINVAL;
28233173a18fSJosef Bacik 	} else {
28245d4f98a2SYan Zheng 		BUG();
28255d4f98a2SYan Zheng 	}
28265d4f98a2SYan Zheng 
2827b3b4aa74SDavid Sterba 	btrfs_release_path(path);
28285d4f98a2SYan Zheng 
28295d4f98a2SYan Zheng 	BUG_ON(level == -1);
28305d4f98a2SYan Zheng 
28315d4f98a2SYan Zheng 	block = kmalloc(sizeof(*block), GFP_NOFS);
28325d4f98a2SYan Zheng 	if (!block)
28335d4f98a2SYan Zheng 		return -ENOMEM;
28345d4f98a2SYan Zheng 
28355d4f98a2SYan Zheng 	block->bytenr = extent_key->objectid;
2836da17066cSJeff Mahoney 	block->key.objectid = rc->extent_root->fs_info->nodesize;
28375d4f98a2SYan Zheng 	block->key.offset = generation;
28385d4f98a2SYan Zheng 	block->level = level;
28395d4f98a2SYan Zheng 	block->key_ready = 0;
28405d4f98a2SYan Zheng 
2841e9a28dc5SQu Wenruo 	rb_node = rb_simple_insert(blocks, block->bytenr, &block->rb_node);
284243c04fb1SJeff Mahoney 	if (rb_node)
2843982c92cbSQu Wenruo 		btrfs_backref_panic(rc->extent_root->fs_info, block->bytenr,
2844982c92cbSQu Wenruo 				    -EEXIST);
28455d4f98a2SYan Zheng 
28465d4f98a2SYan Zheng 	return 0;
28475d4f98a2SYan Zheng }
28485d4f98a2SYan Zheng 
28495d4f98a2SYan Zheng /*
28505d4f98a2SYan Zheng  * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
28515d4f98a2SYan Zheng  */
28525d4f98a2SYan Zheng static int __add_tree_block(struct reloc_control *rc,
28535d4f98a2SYan Zheng 			    u64 bytenr, u32 blocksize,
28545d4f98a2SYan Zheng 			    struct rb_root *blocks)
28555d4f98a2SYan Zheng {
28560b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
28575d4f98a2SYan Zheng 	struct btrfs_path *path;
28585d4f98a2SYan Zheng 	struct btrfs_key key;
28595d4f98a2SYan Zheng 	int ret;
28600b246afaSJeff Mahoney 	bool skinny = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
28615d4f98a2SYan Zheng 
28627476dfdaSDavid Sterba 	if (tree_block_processed(bytenr, rc))
28635d4f98a2SYan Zheng 		return 0;
28645d4f98a2SYan Zheng 
2865e9a28dc5SQu Wenruo 	if (rb_simple_search(blocks, bytenr))
28665d4f98a2SYan Zheng 		return 0;
28675d4f98a2SYan Zheng 
28685d4f98a2SYan Zheng 	path = btrfs_alloc_path();
28695d4f98a2SYan Zheng 	if (!path)
28705d4f98a2SYan Zheng 		return -ENOMEM;
2871aee68ee5SJosef Bacik again:
28725d4f98a2SYan Zheng 	key.objectid = bytenr;
2873aee68ee5SJosef Bacik 	if (skinny) {
2874aee68ee5SJosef Bacik 		key.type = BTRFS_METADATA_ITEM_KEY;
2875aee68ee5SJosef Bacik 		key.offset = (u64)-1;
2876aee68ee5SJosef Bacik 	} else {
28775d4f98a2SYan Zheng 		key.type = BTRFS_EXTENT_ITEM_KEY;
28785d4f98a2SYan Zheng 		key.offset = blocksize;
2879aee68ee5SJosef Bacik 	}
28805d4f98a2SYan Zheng 
28815d4f98a2SYan Zheng 	path->search_commit_root = 1;
28825d4f98a2SYan Zheng 	path->skip_locking = 1;
28835d4f98a2SYan Zheng 	ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0);
28845d4f98a2SYan Zheng 	if (ret < 0)
28855d4f98a2SYan Zheng 		goto out;
28865d4f98a2SYan Zheng 
2887aee68ee5SJosef Bacik 	if (ret > 0 && skinny) {
2888aee68ee5SJosef Bacik 		if (path->slots[0]) {
2889aee68ee5SJosef Bacik 			path->slots[0]--;
2890aee68ee5SJosef Bacik 			btrfs_item_key_to_cpu(path->nodes[0], &key,
2891aee68ee5SJosef Bacik 					      path->slots[0]);
28923173a18fSJosef Bacik 			if (key.objectid == bytenr &&
2893aee68ee5SJosef Bacik 			    (key.type == BTRFS_METADATA_ITEM_KEY ||
2894aee68ee5SJosef Bacik 			     (key.type == BTRFS_EXTENT_ITEM_KEY &&
2895aee68ee5SJosef Bacik 			      key.offset == blocksize)))
28963173a18fSJosef Bacik 				ret = 0;
28973173a18fSJosef Bacik 		}
2898aee68ee5SJosef Bacik 
2899aee68ee5SJosef Bacik 		if (ret) {
2900aee68ee5SJosef Bacik 			skinny = false;
2901aee68ee5SJosef Bacik 			btrfs_release_path(path);
2902aee68ee5SJosef Bacik 			goto again;
2903aee68ee5SJosef Bacik 		}
2904aee68ee5SJosef Bacik 	}
2905cdccee99SLiu Bo 	if (ret) {
2906cdccee99SLiu Bo 		ASSERT(ret == 1);
2907cdccee99SLiu Bo 		btrfs_print_leaf(path->nodes[0]);
2908cdccee99SLiu Bo 		btrfs_err(fs_info,
2909cdccee99SLiu Bo 	     "tree block extent item (%llu) is not found in extent tree",
2910cdccee99SLiu Bo 		     bytenr);
2911cdccee99SLiu Bo 		WARN_ON(1);
2912cdccee99SLiu Bo 		ret = -EINVAL;
2913cdccee99SLiu Bo 		goto out;
2914cdccee99SLiu Bo 	}
29153173a18fSJosef Bacik 
29165d4f98a2SYan Zheng 	ret = add_tree_block(rc, &key, path, blocks);
29175d4f98a2SYan Zheng out:
29185d4f98a2SYan Zheng 	btrfs_free_path(path);
29195d4f98a2SYan Zheng 	return ret;
29205d4f98a2SYan Zheng }
29215d4f98a2SYan Zheng 
29220af3d00bSJosef Bacik static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
292332da5386SDavid Sterba 				    struct btrfs_block_group *block_group,
29241bbc621eSChris Mason 				    struct inode *inode,
29251bbc621eSChris Mason 				    u64 ino)
29260af3d00bSJosef Bacik {
29270af3d00bSJosef Bacik 	struct btrfs_root *root = fs_info->tree_root;
29280af3d00bSJosef Bacik 	struct btrfs_trans_handle *trans;
29290af3d00bSJosef Bacik 	int ret = 0;
29300af3d00bSJosef Bacik 
29310af3d00bSJosef Bacik 	if (inode)
29320af3d00bSJosef Bacik 		goto truncate;
29330af3d00bSJosef Bacik 
29340202e83fSDavid Sterba 	inode = btrfs_iget(fs_info->sb, ino, root);
29352e19f1f9SAl Viro 	if (IS_ERR(inode))
29360af3d00bSJosef Bacik 		return -ENOENT;
29370af3d00bSJosef Bacik 
29380af3d00bSJosef Bacik truncate:
29392ff7e61eSJeff Mahoney 	ret = btrfs_check_trunc_cache_free_space(fs_info,
29407b61cd92SMiao Xie 						 &fs_info->global_block_rsv);
29417b61cd92SMiao Xie 	if (ret)
29427b61cd92SMiao Xie 		goto out;
29437b61cd92SMiao Xie 
29447a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(root);
29450af3d00bSJosef Bacik 	if (IS_ERR(trans)) {
29463612b495STsutomu Itoh 		ret = PTR_ERR(trans);
29470af3d00bSJosef Bacik 		goto out;
29480af3d00bSJosef Bacik 	}
29490af3d00bSJosef Bacik 
295077ab86bfSJeff Mahoney 	ret = btrfs_truncate_free_space_cache(trans, block_group, inode);
29510af3d00bSJosef Bacik 
29523a45bb20SJeff Mahoney 	btrfs_end_transaction(trans);
29532ff7e61eSJeff Mahoney 	btrfs_btree_balance_dirty(fs_info);
29540af3d00bSJosef Bacik out:
29550af3d00bSJosef Bacik 	iput(inode);
29560af3d00bSJosef Bacik 	return ret;
29570af3d00bSJosef Bacik }
29580af3d00bSJosef Bacik 
29595d4f98a2SYan Zheng /*
296019b546d7SQu Wenruo  * Locate the free space cache EXTENT_DATA in root tree leaf and delete the
296119b546d7SQu Wenruo  * cache inode, to avoid free space cache data extent blocking data relocation.
29625d4f98a2SYan Zheng  */
296319b546d7SQu Wenruo static int delete_v1_space_cache(struct extent_buffer *leaf,
296419b546d7SQu Wenruo 				 struct btrfs_block_group *block_group,
296519b546d7SQu Wenruo 				 u64 data_bytenr)
29665d4f98a2SYan Zheng {
296719b546d7SQu Wenruo 	u64 space_cache_ino;
296819b546d7SQu Wenruo 	struct btrfs_file_extent_item *ei;
29695d4f98a2SYan Zheng 	struct btrfs_key key;
297019b546d7SQu Wenruo 	bool found = false;
297119b546d7SQu Wenruo 	int i;
29725d4f98a2SYan Zheng 	int ret;
29735d4f98a2SYan Zheng 
297419b546d7SQu Wenruo 	if (btrfs_header_owner(leaf) != BTRFS_ROOT_TREE_OBJECTID)
297519b546d7SQu Wenruo 		return 0;
29765d4f98a2SYan Zheng 
297719b546d7SQu Wenruo 	for (i = 0; i < btrfs_header_nritems(leaf); i++) {
297850e31ef4SQu Wenruo 		u8 type;
297950e31ef4SQu Wenruo 
298019b546d7SQu Wenruo 		btrfs_item_key_to_cpu(leaf, &key, i);
298119b546d7SQu Wenruo 		if (key.type != BTRFS_EXTENT_DATA_KEY)
298219b546d7SQu Wenruo 			continue;
298319b546d7SQu Wenruo 		ei = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
298450e31ef4SQu Wenruo 		type = btrfs_file_extent_type(leaf, ei);
298550e31ef4SQu Wenruo 
298650e31ef4SQu Wenruo 		if ((type == BTRFS_FILE_EXTENT_REG ||
298750e31ef4SQu Wenruo 		     type == BTRFS_FILE_EXTENT_PREALLOC) &&
298819b546d7SQu Wenruo 		    btrfs_file_extent_disk_bytenr(leaf, ei) == data_bytenr) {
298919b546d7SQu Wenruo 			found = true;
299019b546d7SQu Wenruo 			space_cache_ino = key.objectid;
299119b546d7SQu Wenruo 			break;
299219b546d7SQu Wenruo 		}
299319b546d7SQu Wenruo 	}
299419b546d7SQu Wenruo 	if (!found)
299519b546d7SQu Wenruo 		return -ENOENT;
299619b546d7SQu Wenruo 	ret = delete_block_group_cache(leaf->fs_info, block_group, NULL,
299719b546d7SQu Wenruo 					space_cache_ino);
29980af3d00bSJosef Bacik 	return ret;
29995d4f98a2SYan Zheng }
30005d4f98a2SYan Zheng 
30015d4f98a2SYan Zheng /*
30022c016dc2SLiu Bo  * helper to find all tree blocks that reference a given data extent
30035d4f98a2SYan Zheng  */
30045d4f98a2SYan Zheng static noinline_for_stack
30055d4f98a2SYan Zheng int add_data_references(struct reloc_control *rc,
30065d4f98a2SYan Zheng 			struct btrfs_key *extent_key,
30075d4f98a2SYan Zheng 			struct btrfs_path *path,
30085d4f98a2SYan Zheng 			struct rb_root *blocks)
30095d4f98a2SYan Zheng {
301019b546d7SQu Wenruo 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
301119b546d7SQu Wenruo 	struct ulist *leaves = NULL;
301219b546d7SQu Wenruo 	struct ulist_iterator leaf_uiter;
301319b546d7SQu Wenruo 	struct ulist_node *ref_node = NULL;
301419b546d7SQu Wenruo 	const u32 blocksize = fs_info->nodesize;
3015647f63bdSFilipe David Borba Manana 	int ret = 0;
30165d4f98a2SYan Zheng 
3017b3b4aa74SDavid Sterba 	btrfs_release_path(path);
301819b546d7SQu Wenruo 	ret = btrfs_find_all_leafs(NULL, fs_info, extent_key->objectid,
301919b546d7SQu Wenruo 				   0, &leaves, NULL, true);
302019b546d7SQu Wenruo 	if (ret < 0)
302119b546d7SQu Wenruo 		return ret;
302219b546d7SQu Wenruo 
302319b546d7SQu Wenruo 	ULIST_ITER_INIT(&leaf_uiter);
302419b546d7SQu Wenruo 	while ((ref_node = ulist_next(leaves, &leaf_uiter))) {
302519b546d7SQu Wenruo 		struct extent_buffer *eb;
302619b546d7SQu Wenruo 
30271b7ec85eSJosef Bacik 		eb = read_tree_block(fs_info, ref_node->val, 0, 0, 0, NULL);
302819b546d7SQu Wenruo 		if (IS_ERR(eb)) {
302919b546d7SQu Wenruo 			ret = PTR_ERR(eb);
303019b546d7SQu Wenruo 			break;
303119b546d7SQu Wenruo 		}
303219b546d7SQu Wenruo 		ret = delete_v1_space_cache(eb, rc->block_group,
303319b546d7SQu Wenruo 					    extent_key->objectid);
303419b546d7SQu Wenruo 		free_extent_buffer(eb);
303519b546d7SQu Wenruo 		if (ret < 0)
303619b546d7SQu Wenruo 			break;
303719b546d7SQu Wenruo 		ret = __add_tree_block(rc, ref_node->val, blocksize, blocks);
303819b546d7SQu Wenruo 		if (ret < 0)
303919b546d7SQu Wenruo 			break;
304019b546d7SQu Wenruo 	}
304119b546d7SQu Wenruo 	if (ret < 0)
30425d4f98a2SYan Zheng 		free_block_list(blocks);
304319b546d7SQu Wenruo 	ulist_free(leaves);
304419b546d7SQu Wenruo 	return ret;
30455d4f98a2SYan Zheng }
30465d4f98a2SYan Zheng 
30475d4f98a2SYan Zheng /*
30482c016dc2SLiu Bo  * helper to find next unprocessed extent
30495d4f98a2SYan Zheng  */
30505d4f98a2SYan Zheng static noinline_for_stack
3051147d256eSZhaolei int find_next_extent(struct reloc_control *rc, struct btrfs_path *path,
30523fd0a558SYan, Zheng 		     struct btrfs_key *extent_key)
30535d4f98a2SYan Zheng {
30540b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
30555d4f98a2SYan Zheng 	struct btrfs_key key;
30565d4f98a2SYan Zheng 	struct extent_buffer *leaf;
30575d4f98a2SYan Zheng 	u64 start, end, last;
30585d4f98a2SYan Zheng 	int ret;
30595d4f98a2SYan Zheng 
3060b3470b5dSDavid Sterba 	last = rc->block_group->start + rc->block_group->length;
30615d4f98a2SYan Zheng 	while (1) {
30625d4f98a2SYan Zheng 		cond_resched();
30635d4f98a2SYan Zheng 		if (rc->search_start >= last) {
30645d4f98a2SYan Zheng 			ret = 1;
30655d4f98a2SYan Zheng 			break;
30665d4f98a2SYan Zheng 		}
30675d4f98a2SYan Zheng 
30685d4f98a2SYan Zheng 		key.objectid = rc->search_start;
30695d4f98a2SYan Zheng 		key.type = BTRFS_EXTENT_ITEM_KEY;
30705d4f98a2SYan Zheng 		key.offset = 0;
30715d4f98a2SYan Zheng 
30725d4f98a2SYan Zheng 		path->search_commit_root = 1;
30735d4f98a2SYan Zheng 		path->skip_locking = 1;
30745d4f98a2SYan Zheng 		ret = btrfs_search_slot(NULL, rc->extent_root, &key, path,
30755d4f98a2SYan Zheng 					0, 0);
30765d4f98a2SYan Zheng 		if (ret < 0)
30775d4f98a2SYan Zheng 			break;
30785d4f98a2SYan Zheng next:
30795d4f98a2SYan Zheng 		leaf = path->nodes[0];
30805d4f98a2SYan Zheng 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
30815d4f98a2SYan Zheng 			ret = btrfs_next_leaf(rc->extent_root, path);
30825d4f98a2SYan Zheng 			if (ret != 0)
30835d4f98a2SYan Zheng 				break;
30845d4f98a2SYan Zheng 			leaf = path->nodes[0];
30855d4f98a2SYan Zheng 		}
30865d4f98a2SYan Zheng 
30875d4f98a2SYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
30885d4f98a2SYan Zheng 		if (key.objectid >= last) {
30895d4f98a2SYan Zheng 			ret = 1;
30905d4f98a2SYan Zheng 			break;
30915d4f98a2SYan Zheng 		}
30925d4f98a2SYan Zheng 
30933173a18fSJosef Bacik 		if (key.type != BTRFS_EXTENT_ITEM_KEY &&
30943173a18fSJosef Bacik 		    key.type != BTRFS_METADATA_ITEM_KEY) {
30953173a18fSJosef Bacik 			path->slots[0]++;
30963173a18fSJosef Bacik 			goto next;
30973173a18fSJosef Bacik 		}
30983173a18fSJosef Bacik 
30993173a18fSJosef Bacik 		if (key.type == BTRFS_EXTENT_ITEM_KEY &&
31005d4f98a2SYan Zheng 		    key.objectid + key.offset <= rc->search_start) {
31015d4f98a2SYan Zheng 			path->slots[0]++;
31025d4f98a2SYan Zheng 			goto next;
31035d4f98a2SYan Zheng 		}
31045d4f98a2SYan Zheng 
31053173a18fSJosef Bacik 		if (key.type == BTRFS_METADATA_ITEM_KEY &&
31060b246afaSJeff Mahoney 		    key.objectid + fs_info->nodesize <=
31073173a18fSJosef Bacik 		    rc->search_start) {
31083173a18fSJosef Bacik 			path->slots[0]++;
31093173a18fSJosef Bacik 			goto next;
31103173a18fSJosef Bacik 		}
31113173a18fSJosef Bacik 
31125d4f98a2SYan Zheng 		ret = find_first_extent_bit(&rc->processed_blocks,
31135d4f98a2SYan Zheng 					    key.objectid, &start, &end,
3114e6138876SJosef Bacik 					    EXTENT_DIRTY, NULL);
31155d4f98a2SYan Zheng 
31165d4f98a2SYan Zheng 		if (ret == 0 && start <= key.objectid) {
3117b3b4aa74SDavid Sterba 			btrfs_release_path(path);
31185d4f98a2SYan Zheng 			rc->search_start = end + 1;
31195d4f98a2SYan Zheng 		} else {
31203173a18fSJosef Bacik 			if (key.type == BTRFS_EXTENT_ITEM_KEY)
31215d4f98a2SYan Zheng 				rc->search_start = key.objectid + key.offset;
31223173a18fSJosef Bacik 			else
31233173a18fSJosef Bacik 				rc->search_start = key.objectid +
31240b246afaSJeff Mahoney 					fs_info->nodesize;
31253fd0a558SYan, Zheng 			memcpy(extent_key, &key, sizeof(key));
31265d4f98a2SYan Zheng 			return 0;
31275d4f98a2SYan Zheng 		}
31285d4f98a2SYan Zheng 	}
3129b3b4aa74SDavid Sterba 	btrfs_release_path(path);
31305d4f98a2SYan Zheng 	return ret;
31315d4f98a2SYan Zheng }
31325d4f98a2SYan Zheng 
31335d4f98a2SYan Zheng static void set_reloc_control(struct reloc_control *rc)
31345d4f98a2SYan Zheng {
31355d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
31367585717fSChris Mason 
31377585717fSChris Mason 	mutex_lock(&fs_info->reloc_mutex);
31385d4f98a2SYan Zheng 	fs_info->reloc_ctl = rc;
31397585717fSChris Mason 	mutex_unlock(&fs_info->reloc_mutex);
31405d4f98a2SYan Zheng }
31415d4f98a2SYan Zheng 
31425d4f98a2SYan Zheng static void unset_reloc_control(struct reloc_control *rc)
31435d4f98a2SYan Zheng {
31445d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
31457585717fSChris Mason 
31467585717fSChris Mason 	mutex_lock(&fs_info->reloc_mutex);
31475d4f98a2SYan Zheng 	fs_info->reloc_ctl = NULL;
31487585717fSChris Mason 	mutex_unlock(&fs_info->reloc_mutex);
31495d4f98a2SYan Zheng }
31505d4f98a2SYan Zheng 
31515d4f98a2SYan Zheng static int check_extent_flags(u64 flags)
31525d4f98a2SYan Zheng {
31535d4f98a2SYan Zheng 	if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
31545d4f98a2SYan Zheng 	    (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
31555d4f98a2SYan Zheng 		return 1;
31565d4f98a2SYan Zheng 	if (!(flags & BTRFS_EXTENT_FLAG_DATA) &&
31575d4f98a2SYan Zheng 	    !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
31585d4f98a2SYan Zheng 		return 1;
31595d4f98a2SYan Zheng 	if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
31605d4f98a2SYan Zheng 	    (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
31615d4f98a2SYan Zheng 		return 1;
31625d4f98a2SYan Zheng 	return 0;
31635d4f98a2SYan Zheng }
31645d4f98a2SYan Zheng 
31653fd0a558SYan, Zheng static noinline_for_stack
31663fd0a558SYan, Zheng int prepare_to_relocate(struct reloc_control *rc)
31673fd0a558SYan, Zheng {
31683fd0a558SYan, Zheng 	struct btrfs_trans_handle *trans;
3169ac2fabacSJosef Bacik 	int ret;
31703fd0a558SYan, Zheng 
31712ff7e61eSJeff Mahoney 	rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root->fs_info,
317266d8f3ddSMiao Xie 					      BTRFS_BLOCK_RSV_TEMP);
31733fd0a558SYan, Zheng 	if (!rc->block_rsv)
31743fd0a558SYan, Zheng 		return -ENOMEM;
31753fd0a558SYan, Zheng 
31763fd0a558SYan, Zheng 	memset(&rc->cluster, 0, sizeof(rc->cluster));
3177b3470b5dSDavid Sterba 	rc->search_start = rc->block_group->start;
31783fd0a558SYan, Zheng 	rc->extents_found = 0;
31793fd0a558SYan, Zheng 	rc->nodes_relocated = 0;
31803fd0a558SYan, Zheng 	rc->merging_rsv_size = 0;
31810647bf56SWang Shilong 	rc->reserved_bytes = 0;
3182da17066cSJeff Mahoney 	rc->block_rsv->size = rc->extent_root->fs_info->nodesize *
31830647bf56SWang Shilong 			      RELOCATION_RESERVED_NODES;
3184ac2fabacSJosef Bacik 	ret = btrfs_block_rsv_refill(rc->extent_root,
3185ac2fabacSJosef Bacik 				     rc->block_rsv, rc->block_rsv->size,
3186ac2fabacSJosef Bacik 				     BTRFS_RESERVE_FLUSH_ALL);
3187ac2fabacSJosef Bacik 	if (ret)
3188ac2fabacSJosef Bacik 		return ret;
31893fd0a558SYan, Zheng 
31903fd0a558SYan, Zheng 	rc->create_reloc_tree = 1;
31913fd0a558SYan, Zheng 	set_reloc_control(rc);
31923fd0a558SYan, Zheng 
31937a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(rc->extent_root);
319428818947SLiu Bo 	if (IS_ERR(trans)) {
319528818947SLiu Bo 		unset_reloc_control(rc);
319628818947SLiu Bo 		/*
319728818947SLiu Bo 		 * extent tree is not a ref_cow tree and has no reloc_root to
319828818947SLiu Bo 		 * cleanup.  And callers are responsible to free the above
319928818947SLiu Bo 		 * block rsv.
320028818947SLiu Bo 		 */
320128818947SLiu Bo 		return PTR_ERR(trans);
320228818947SLiu Bo 	}
32033a45bb20SJeff Mahoney 	btrfs_commit_transaction(trans);
32043fd0a558SYan, Zheng 	return 0;
32053fd0a558SYan, Zheng }
320676dda93cSYan, Zheng 
32075d4f98a2SYan Zheng static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
32085d4f98a2SYan Zheng {
32092ff7e61eSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
32105d4f98a2SYan Zheng 	struct rb_root blocks = RB_ROOT;
32115d4f98a2SYan Zheng 	struct btrfs_key key;
32125d4f98a2SYan Zheng 	struct btrfs_trans_handle *trans = NULL;
32135d4f98a2SYan Zheng 	struct btrfs_path *path;
32145d4f98a2SYan Zheng 	struct btrfs_extent_item *ei;
32155d4f98a2SYan Zheng 	u64 flags;
32165d4f98a2SYan Zheng 	u32 item_size;
32175d4f98a2SYan Zheng 	int ret;
32185d4f98a2SYan Zheng 	int err = 0;
3219c87f08caSChris Mason 	int progress = 0;
32205d4f98a2SYan Zheng 
32215d4f98a2SYan Zheng 	path = btrfs_alloc_path();
32223fd0a558SYan, Zheng 	if (!path)
32235d4f98a2SYan Zheng 		return -ENOMEM;
3224e4058b54SDavid Sterba 	path->reada = READA_FORWARD;
32253fd0a558SYan, Zheng 
32263fd0a558SYan, Zheng 	ret = prepare_to_relocate(rc);
32273fd0a558SYan, Zheng 	if (ret) {
32283fd0a558SYan, Zheng 		err = ret;
32293fd0a558SYan, Zheng 		goto out_free;
32302423fdfbSJiri Slaby 	}
32315d4f98a2SYan Zheng 
32325d4f98a2SYan Zheng 	while (1) {
32330647bf56SWang Shilong 		rc->reserved_bytes = 0;
32340647bf56SWang Shilong 		ret = btrfs_block_rsv_refill(rc->extent_root,
32350647bf56SWang Shilong 					rc->block_rsv, rc->block_rsv->size,
32360647bf56SWang Shilong 					BTRFS_RESERVE_FLUSH_ALL);
32370647bf56SWang Shilong 		if (ret) {
32380647bf56SWang Shilong 			err = ret;
32390647bf56SWang Shilong 			break;
32400647bf56SWang Shilong 		}
3241c87f08caSChris Mason 		progress++;
3242a22285a6SYan, Zheng 		trans = btrfs_start_transaction(rc->extent_root, 0);
32430f788c58SLiu Bo 		if (IS_ERR(trans)) {
32440f788c58SLiu Bo 			err = PTR_ERR(trans);
32450f788c58SLiu Bo 			trans = NULL;
32460f788c58SLiu Bo 			break;
32470f788c58SLiu Bo 		}
3248c87f08caSChris Mason restart:
32493fd0a558SYan, Zheng 		if (update_backref_cache(trans, &rc->backref_cache)) {
32503a45bb20SJeff Mahoney 			btrfs_end_transaction(trans);
325142a657f5SPan Bian 			trans = NULL;
32523fd0a558SYan, Zheng 			continue;
32533fd0a558SYan, Zheng 		}
32543fd0a558SYan, Zheng 
3255147d256eSZhaolei 		ret = find_next_extent(rc, path, &key);
32565d4f98a2SYan Zheng 		if (ret < 0)
32575d4f98a2SYan Zheng 			err = ret;
32585d4f98a2SYan Zheng 		if (ret != 0)
32595d4f98a2SYan Zheng 			break;
32605d4f98a2SYan Zheng 
32615d4f98a2SYan Zheng 		rc->extents_found++;
32625d4f98a2SYan Zheng 
32635d4f98a2SYan Zheng 		ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
32645d4f98a2SYan Zheng 				    struct btrfs_extent_item);
32653fd0a558SYan, Zheng 		item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
32665d4f98a2SYan Zheng 		if (item_size >= sizeof(*ei)) {
32675d4f98a2SYan Zheng 			flags = btrfs_extent_flags(path->nodes[0], ei);
32685d4f98a2SYan Zheng 			ret = check_extent_flags(flags);
32695d4f98a2SYan Zheng 			BUG_ON(ret);
32706d8ff4e4SDavid Sterba 		} else if (unlikely(item_size == sizeof(struct btrfs_extent_item_v0))) {
3271ba3c2b19SNikolay Borisov 			err = -EINVAL;
3272ba3c2b19SNikolay Borisov 			btrfs_print_v0_err(trans->fs_info);
3273ba3c2b19SNikolay Borisov 			btrfs_abort_transaction(trans, err);
3274ba3c2b19SNikolay Borisov 			break;
32755d4f98a2SYan Zheng 		} else {
32765d4f98a2SYan Zheng 			BUG();
32775d4f98a2SYan Zheng 		}
32785d4f98a2SYan Zheng 
32795d4f98a2SYan Zheng 		if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
32805d4f98a2SYan Zheng 			ret = add_tree_block(rc, &key, path, &blocks);
32815d4f98a2SYan Zheng 		} else if (rc->stage == UPDATE_DATA_PTRS &&
32825d4f98a2SYan Zheng 			   (flags & BTRFS_EXTENT_FLAG_DATA)) {
32835d4f98a2SYan Zheng 			ret = add_data_references(rc, &key, path, &blocks);
32845d4f98a2SYan Zheng 		} else {
3285b3b4aa74SDavid Sterba 			btrfs_release_path(path);
32865d4f98a2SYan Zheng 			ret = 0;
32875d4f98a2SYan Zheng 		}
32885d4f98a2SYan Zheng 		if (ret < 0) {
32893fd0a558SYan, Zheng 			err = ret;
32905d4f98a2SYan Zheng 			break;
32915d4f98a2SYan Zheng 		}
32925d4f98a2SYan Zheng 
32935d4f98a2SYan Zheng 		if (!RB_EMPTY_ROOT(&blocks)) {
32945d4f98a2SYan Zheng 			ret = relocate_tree_blocks(trans, rc, &blocks);
32955d4f98a2SYan Zheng 			if (ret < 0) {
32963fd0a558SYan, Zheng 				if (ret != -EAGAIN) {
32975d4f98a2SYan Zheng 					err = ret;
32985d4f98a2SYan Zheng 					break;
32995d4f98a2SYan Zheng 				}
33003fd0a558SYan, Zheng 				rc->extents_found--;
33013fd0a558SYan, Zheng 				rc->search_start = key.objectid;
33023fd0a558SYan, Zheng 			}
33035d4f98a2SYan Zheng 		}
33045d4f98a2SYan Zheng 
33053a45bb20SJeff Mahoney 		btrfs_end_transaction_throttle(trans);
33062ff7e61eSJeff Mahoney 		btrfs_btree_balance_dirty(fs_info);
33073fd0a558SYan, Zheng 		trans = NULL;
33085d4f98a2SYan Zheng 
33095d4f98a2SYan Zheng 		if (rc->stage == MOVE_DATA_EXTENTS &&
33105d4f98a2SYan Zheng 		    (flags & BTRFS_EXTENT_FLAG_DATA)) {
33115d4f98a2SYan Zheng 			rc->found_file_extent = 1;
33120257bb82SYan, Zheng 			ret = relocate_data_extent(rc->data_inode,
33133fd0a558SYan, Zheng 						   &key, &rc->cluster);
33145d4f98a2SYan Zheng 			if (ret < 0) {
33155d4f98a2SYan Zheng 				err = ret;
33165d4f98a2SYan Zheng 				break;
33175d4f98a2SYan Zheng 			}
33185d4f98a2SYan Zheng 		}
3319f31ea088SQu Wenruo 		if (btrfs_should_cancel_balance(fs_info)) {
3320f31ea088SQu Wenruo 			err = -ECANCELED;
3321f31ea088SQu Wenruo 			break;
3322f31ea088SQu Wenruo 		}
33235d4f98a2SYan Zheng 	}
3324c87f08caSChris Mason 	if (trans && progress && err == -ENOSPC) {
332543a7e99dSNikolay Borisov 		ret = btrfs_force_chunk_alloc(trans, rc->block_group->flags);
33269689457bSShilong Wang 		if (ret == 1) {
3327c87f08caSChris Mason 			err = 0;
3328c87f08caSChris Mason 			progress = 0;
3329c87f08caSChris Mason 			goto restart;
3330c87f08caSChris Mason 		}
3331c87f08caSChris Mason 	}
33323fd0a558SYan, Zheng 
3333b3b4aa74SDavid Sterba 	btrfs_release_path(path);
333491166212SDavid Sterba 	clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY);
33355d4f98a2SYan Zheng 
33365d4f98a2SYan Zheng 	if (trans) {
33373a45bb20SJeff Mahoney 		btrfs_end_transaction_throttle(trans);
33382ff7e61eSJeff Mahoney 		btrfs_btree_balance_dirty(fs_info);
33395d4f98a2SYan Zheng 	}
33405d4f98a2SYan Zheng 
33410257bb82SYan, Zheng 	if (!err) {
33423fd0a558SYan, Zheng 		ret = relocate_file_extent_cluster(rc->data_inode,
33433fd0a558SYan, Zheng 						   &rc->cluster);
33440257bb82SYan, Zheng 		if (ret < 0)
33450257bb82SYan, Zheng 			err = ret;
33460257bb82SYan, Zheng 	}
33470257bb82SYan, Zheng 
33483fd0a558SYan, Zheng 	rc->create_reloc_tree = 0;
33493fd0a558SYan, Zheng 	set_reloc_control(rc);
33500257bb82SYan, Zheng 
335113fe1bdbSQu Wenruo 	btrfs_backref_release_cache(&rc->backref_cache);
335263f018beSNikolay Borisov 	btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL);
33535d4f98a2SYan Zheng 
33547f913c7cSQu Wenruo 	/*
33557f913c7cSQu Wenruo 	 * Even in the case when the relocation is cancelled, we should all go
33567f913c7cSQu Wenruo 	 * through prepare_to_merge() and merge_reloc_roots().
33577f913c7cSQu Wenruo 	 *
33587f913c7cSQu Wenruo 	 * For error (including cancelled balance), prepare_to_merge() will
33597f913c7cSQu Wenruo 	 * mark all reloc trees orphan, then queue them for cleanup in
33607f913c7cSQu Wenruo 	 * merge_reloc_roots()
33617f913c7cSQu Wenruo 	 */
33623fd0a558SYan, Zheng 	err = prepare_to_merge(rc, err);
33635d4f98a2SYan Zheng 
33645d4f98a2SYan Zheng 	merge_reloc_roots(rc);
33655d4f98a2SYan Zheng 
33663fd0a558SYan, Zheng 	rc->merge_reloc_tree = 0;
33675d4f98a2SYan Zheng 	unset_reloc_control(rc);
336863f018beSNikolay Borisov 	btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL);
33695d4f98a2SYan Zheng 
33705d4f98a2SYan Zheng 	/* get rid of pinned extents */
33717a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(rc->extent_root);
337262b99540SQu Wenruo 	if (IS_ERR(trans)) {
33733612b495STsutomu Itoh 		err = PTR_ERR(trans);
337462b99540SQu Wenruo 		goto out_free;
337562b99540SQu Wenruo 	}
33763a45bb20SJeff Mahoney 	btrfs_commit_transaction(trans);
33776217b0faSJosef Bacik out_free:
3378d2311e69SQu Wenruo 	ret = clean_dirty_subvols(rc);
3379d2311e69SQu Wenruo 	if (ret < 0 && !err)
3380d2311e69SQu Wenruo 		err = ret;
33812ff7e61eSJeff Mahoney 	btrfs_free_block_rsv(fs_info, rc->block_rsv);
33823fd0a558SYan, Zheng 	btrfs_free_path(path);
33835d4f98a2SYan Zheng 	return err;
33845d4f98a2SYan Zheng }
33855d4f98a2SYan Zheng 
33865d4f98a2SYan Zheng static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
33870257bb82SYan, Zheng 				 struct btrfs_root *root, u64 objectid)
33885d4f98a2SYan Zheng {
33895d4f98a2SYan Zheng 	struct btrfs_path *path;
33905d4f98a2SYan Zheng 	struct btrfs_inode_item *item;
33915d4f98a2SYan Zheng 	struct extent_buffer *leaf;
33925d4f98a2SYan Zheng 	int ret;
33935d4f98a2SYan Zheng 
33945d4f98a2SYan Zheng 	path = btrfs_alloc_path();
33955d4f98a2SYan Zheng 	if (!path)
33965d4f98a2SYan Zheng 		return -ENOMEM;
33975d4f98a2SYan Zheng 
33985d4f98a2SYan Zheng 	ret = btrfs_insert_empty_inode(trans, root, path, objectid);
33995d4f98a2SYan Zheng 	if (ret)
34005d4f98a2SYan Zheng 		goto out;
34015d4f98a2SYan Zheng 
34025d4f98a2SYan Zheng 	leaf = path->nodes[0];
34035d4f98a2SYan Zheng 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
3404b159fa28SDavid Sterba 	memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
34055d4f98a2SYan Zheng 	btrfs_set_inode_generation(leaf, item, 1);
34060257bb82SYan, Zheng 	btrfs_set_inode_size(leaf, item, 0);
34075d4f98a2SYan Zheng 	btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
34083fd0a558SYan, Zheng 	btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS |
34093fd0a558SYan, Zheng 					  BTRFS_INODE_PREALLOC);
34105d4f98a2SYan Zheng 	btrfs_mark_buffer_dirty(leaf);
34115d4f98a2SYan Zheng out:
34125d4f98a2SYan Zheng 	btrfs_free_path(path);
34135d4f98a2SYan Zheng 	return ret;
34145d4f98a2SYan Zheng }
34155d4f98a2SYan Zheng 
34165d4f98a2SYan Zheng /*
34175d4f98a2SYan Zheng  * helper to create inode for data relocation.
34185d4f98a2SYan Zheng  * the inode is in data relocation tree and its link count is 0
34195d4f98a2SYan Zheng  */
34203fd0a558SYan, Zheng static noinline_for_stack
34213fd0a558SYan, Zheng struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
342232da5386SDavid Sterba 				 struct btrfs_block_group *group)
34235d4f98a2SYan Zheng {
34245d4f98a2SYan Zheng 	struct inode *inode = NULL;
34255d4f98a2SYan Zheng 	struct btrfs_trans_handle *trans;
34265d4f98a2SYan Zheng 	struct btrfs_root *root;
34274624900dSZhaolei 	u64 objectid;
34285d4f98a2SYan Zheng 	int err = 0;
34295d4f98a2SYan Zheng 
3430aeb935a4SQu Wenruo 	root = btrfs_grab_root(fs_info->data_reloc_root);
3431a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 6);
343276deacf0SJosef Bacik 	if (IS_ERR(trans)) {
343300246528SJosef Bacik 		btrfs_put_root(root);
34343fd0a558SYan, Zheng 		return ERR_CAST(trans);
343576deacf0SJosef Bacik 	}
34365d4f98a2SYan Zheng 
3437543068a2SNikolay Borisov 	err = btrfs_get_free_objectid(root, &objectid);
34385d4f98a2SYan Zheng 	if (err)
34395d4f98a2SYan Zheng 		goto out;
34405d4f98a2SYan Zheng 
34410257bb82SYan, Zheng 	err = __insert_orphan_inode(trans, root, objectid);
34425d4f98a2SYan Zheng 	BUG_ON(err);
34435d4f98a2SYan Zheng 
34440202e83fSDavid Sterba 	inode = btrfs_iget(fs_info->sb, objectid, root);
34452e19f1f9SAl Viro 	BUG_ON(IS_ERR(inode));
3446b3470b5dSDavid Sterba 	BTRFS_I(inode)->index_cnt = group->start;
34475d4f98a2SYan Zheng 
344873f2e545SNikolay Borisov 	err = btrfs_orphan_add(trans, BTRFS_I(inode));
34495d4f98a2SYan Zheng out:
345000246528SJosef Bacik 	btrfs_put_root(root);
34513a45bb20SJeff Mahoney 	btrfs_end_transaction(trans);
34522ff7e61eSJeff Mahoney 	btrfs_btree_balance_dirty(fs_info);
34535d4f98a2SYan Zheng 	if (err) {
34545d4f98a2SYan Zheng 		if (inode)
34555d4f98a2SYan Zheng 			iput(inode);
34565d4f98a2SYan Zheng 		inode = ERR_PTR(err);
34575d4f98a2SYan Zheng 	}
34585d4f98a2SYan Zheng 	return inode;
34595d4f98a2SYan Zheng }
34605d4f98a2SYan Zheng 
3461c258d6e3SQu Wenruo static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info)
34623fd0a558SYan, Zheng {
34633fd0a558SYan, Zheng 	struct reloc_control *rc;
34643fd0a558SYan, Zheng 
34653fd0a558SYan, Zheng 	rc = kzalloc(sizeof(*rc), GFP_NOFS);
34663fd0a558SYan, Zheng 	if (!rc)
34673fd0a558SYan, Zheng 		return NULL;
34683fd0a558SYan, Zheng 
34693fd0a558SYan, Zheng 	INIT_LIST_HEAD(&rc->reloc_roots);
3470d2311e69SQu Wenruo 	INIT_LIST_HEAD(&rc->dirty_subvol_roots);
3471584fb121SQu Wenruo 	btrfs_backref_init_cache(fs_info, &rc->backref_cache, 1);
34723fd0a558SYan, Zheng 	mapping_tree_init(&rc->reloc_root_tree);
347343eb5f29SQu Wenruo 	extent_io_tree_init(fs_info, &rc->processed_blocks,
347443eb5f29SQu Wenruo 			    IO_TREE_RELOC_BLOCKS, NULL);
34753fd0a558SYan, Zheng 	return rc;
34763fd0a558SYan, Zheng }
34773fd0a558SYan, Zheng 
34781a0afa0eSJosef Bacik static void free_reloc_control(struct reloc_control *rc)
34791a0afa0eSJosef Bacik {
34801a0afa0eSJosef Bacik 	struct mapping_node *node, *tmp;
34811a0afa0eSJosef Bacik 
34821a0afa0eSJosef Bacik 	free_reloc_roots(&rc->reloc_roots);
34831a0afa0eSJosef Bacik 	rbtree_postorder_for_each_entry_safe(node, tmp,
34841a0afa0eSJosef Bacik 			&rc->reloc_root_tree.rb_root, rb_node)
34851a0afa0eSJosef Bacik 		kfree(node);
34861a0afa0eSJosef Bacik 
34871a0afa0eSJosef Bacik 	kfree(rc);
34881a0afa0eSJosef Bacik }
34891a0afa0eSJosef Bacik 
34905d4f98a2SYan Zheng /*
3491ebce0e01SAdam Borowski  * Print the block group being relocated
3492ebce0e01SAdam Borowski  */
3493ebce0e01SAdam Borowski static void describe_relocation(struct btrfs_fs_info *fs_info,
349432da5386SDavid Sterba 				struct btrfs_block_group *block_group)
3495ebce0e01SAdam Borowski {
3496f89e09cfSAnand Jain 	char buf[128] = {'\0'};
3497ebce0e01SAdam Borowski 
3498f89e09cfSAnand Jain 	btrfs_describe_block_groups(block_group->flags, buf, sizeof(buf));
3499ebce0e01SAdam Borowski 
3500ebce0e01SAdam Borowski 	btrfs_info(fs_info,
3501ebce0e01SAdam Borowski 		   "relocating block group %llu flags %s",
3502b3470b5dSDavid Sterba 		   block_group->start, buf);
3503ebce0e01SAdam Borowski }
3504ebce0e01SAdam Borowski 
3505430640e3SQu Wenruo static const char *stage_to_string(int stage)
3506430640e3SQu Wenruo {
3507430640e3SQu Wenruo 	if (stage == MOVE_DATA_EXTENTS)
3508430640e3SQu Wenruo 		return "move data extents";
3509430640e3SQu Wenruo 	if (stage == UPDATE_DATA_PTRS)
3510430640e3SQu Wenruo 		return "update data pointers";
3511430640e3SQu Wenruo 	return "unknown";
3512430640e3SQu Wenruo }
3513430640e3SQu Wenruo 
3514ebce0e01SAdam Borowski /*
35155d4f98a2SYan Zheng  * function to relocate all extents in a block group.
35165d4f98a2SYan Zheng  */
35176bccf3abSJeff Mahoney int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start)
35185d4f98a2SYan Zheng {
351932da5386SDavid Sterba 	struct btrfs_block_group *bg;
35206bccf3abSJeff Mahoney 	struct btrfs_root *extent_root = fs_info->extent_root;
35215d4f98a2SYan Zheng 	struct reloc_control *rc;
35220af3d00bSJosef Bacik 	struct inode *inode;
35230af3d00bSJosef Bacik 	struct btrfs_path *path;
35245d4f98a2SYan Zheng 	int ret;
3525f0486c68SYan, Zheng 	int rw = 0;
35265d4f98a2SYan Zheng 	int err = 0;
35275d4f98a2SYan Zheng 
3528eede2bf3SOmar Sandoval 	bg = btrfs_lookup_block_group(fs_info, group_start);
3529eede2bf3SOmar Sandoval 	if (!bg)
3530eede2bf3SOmar Sandoval 		return -ENOENT;
3531eede2bf3SOmar Sandoval 
3532eede2bf3SOmar Sandoval 	if (btrfs_pinned_by_swapfile(fs_info, bg)) {
3533eede2bf3SOmar Sandoval 		btrfs_put_block_group(bg);
3534eede2bf3SOmar Sandoval 		return -ETXTBSY;
3535eede2bf3SOmar Sandoval 	}
3536eede2bf3SOmar Sandoval 
3537c258d6e3SQu Wenruo 	rc = alloc_reloc_control(fs_info);
3538eede2bf3SOmar Sandoval 	if (!rc) {
3539eede2bf3SOmar Sandoval 		btrfs_put_block_group(bg);
35405d4f98a2SYan Zheng 		return -ENOMEM;
3541eede2bf3SOmar Sandoval 	}
35425d4f98a2SYan Zheng 
3543f0486c68SYan, Zheng 	rc->extent_root = extent_root;
3544eede2bf3SOmar Sandoval 	rc->block_group = bg;
35455d4f98a2SYan Zheng 
3546b12de528SQu Wenruo 	ret = btrfs_inc_block_group_ro(rc->block_group, true);
3547f0486c68SYan, Zheng 	if (ret) {
3548f0486c68SYan, Zheng 		err = ret;
3549f0486c68SYan, Zheng 		goto out;
3550f0486c68SYan, Zheng 	}
3551f0486c68SYan, Zheng 	rw = 1;
3552f0486c68SYan, Zheng 
35530af3d00bSJosef Bacik 	path = btrfs_alloc_path();
35540af3d00bSJosef Bacik 	if (!path) {
35550af3d00bSJosef Bacik 		err = -ENOMEM;
35560af3d00bSJosef Bacik 		goto out;
35570af3d00bSJosef Bacik 	}
35580af3d00bSJosef Bacik 
35597949f339SDavid Sterba 	inode = lookup_free_space_inode(rc->block_group, path);
35600af3d00bSJosef Bacik 	btrfs_free_path(path);
35610af3d00bSJosef Bacik 
35620af3d00bSJosef Bacik 	if (!IS_ERR(inode))
35631bbc621eSChris Mason 		ret = delete_block_group_cache(fs_info, rc->block_group, inode, 0);
35640af3d00bSJosef Bacik 	else
35650af3d00bSJosef Bacik 		ret = PTR_ERR(inode);
35660af3d00bSJosef Bacik 
35670af3d00bSJosef Bacik 	if (ret && ret != -ENOENT) {
35680af3d00bSJosef Bacik 		err = ret;
35690af3d00bSJosef Bacik 		goto out;
35700af3d00bSJosef Bacik 	}
35710af3d00bSJosef Bacik 
35725d4f98a2SYan Zheng 	rc->data_inode = create_reloc_inode(fs_info, rc->block_group);
35735d4f98a2SYan Zheng 	if (IS_ERR(rc->data_inode)) {
35745d4f98a2SYan Zheng 		err = PTR_ERR(rc->data_inode);
35755d4f98a2SYan Zheng 		rc->data_inode = NULL;
35765d4f98a2SYan Zheng 		goto out;
35775d4f98a2SYan Zheng 	}
35785d4f98a2SYan Zheng 
35790b246afaSJeff Mahoney 	describe_relocation(fs_info, rc->block_group);
35805d4f98a2SYan Zheng 
35819cfa3e34SFilipe Manana 	btrfs_wait_block_group_reservations(rc->block_group);
3582f78c436cSFilipe Manana 	btrfs_wait_nocow_writers(rc->block_group);
35836374e57aSChris Mason 	btrfs_wait_ordered_roots(fs_info, U64_MAX,
3584b3470b5dSDavid Sterba 				 rc->block_group->start,
3585b3470b5dSDavid Sterba 				 rc->block_group->length);
35865d4f98a2SYan Zheng 
35875d4f98a2SYan Zheng 	while (1) {
3588430640e3SQu Wenruo 		int finishes_stage;
3589430640e3SQu Wenruo 
359076dda93cSYan, Zheng 		mutex_lock(&fs_info->cleaner_mutex);
35915d4f98a2SYan Zheng 		ret = relocate_block_group(rc);
359276dda93cSYan, Zheng 		mutex_unlock(&fs_info->cleaner_mutex);
3593ff612ba7SJosef Bacik 		if (ret < 0)
35945d4f98a2SYan Zheng 			err = ret;
3595ff612ba7SJosef Bacik 
3596430640e3SQu Wenruo 		finishes_stage = rc->stage;
3597ff612ba7SJosef Bacik 		/*
3598ff612ba7SJosef Bacik 		 * We may have gotten ENOSPC after we already dirtied some
3599ff612ba7SJosef Bacik 		 * extents.  If writeout happens while we're relocating a
3600ff612ba7SJosef Bacik 		 * different block group we could end up hitting the
3601ff612ba7SJosef Bacik 		 * BUG_ON(rc->stage == UPDATE_DATA_PTRS) in
3602ff612ba7SJosef Bacik 		 * btrfs_reloc_cow_block.  Make sure we write everything out
3603ff612ba7SJosef Bacik 		 * properly so we don't trip over this problem, and then break
3604ff612ba7SJosef Bacik 		 * out of the loop if we hit an error.
3605ff612ba7SJosef Bacik 		 */
3606ff612ba7SJosef Bacik 		if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) {
3607ff612ba7SJosef Bacik 			ret = btrfs_wait_ordered_range(rc->data_inode, 0,
3608ff612ba7SJosef Bacik 						       (u64)-1);
3609ff612ba7SJosef Bacik 			if (ret)
3610ff612ba7SJosef Bacik 				err = ret;
3611ff612ba7SJosef Bacik 			invalidate_mapping_pages(rc->data_inode->i_mapping,
3612ff612ba7SJosef Bacik 						 0, -1);
3613ff612ba7SJosef Bacik 			rc->stage = UPDATE_DATA_PTRS;
36145d4f98a2SYan Zheng 		}
36155d4f98a2SYan Zheng 
3616ff612ba7SJosef Bacik 		if (err < 0)
3617ff612ba7SJosef Bacik 			goto out;
3618ff612ba7SJosef Bacik 
36195d4f98a2SYan Zheng 		if (rc->extents_found == 0)
36205d4f98a2SYan Zheng 			break;
36215d4f98a2SYan Zheng 
3622430640e3SQu Wenruo 		btrfs_info(fs_info, "found %llu extents, stage: %s",
3623430640e3SQu Wenruo 			   rc->extents_found, stage_to_string(finishes_stage));
36245d4f98a2SYan Zheng 	}
36255d4f98a2SYan Zheng 
36265d4f98a2SYan Zheng 	WARN_ON(rc->block_group->pinned > 0);
36275d4f98a2SYan Zheng 	WARN_ON(rc->block_group->reserved > 0);
3628bf38be65SDavid Sterba 	WARN_ON(rc->block_group->used > 0);
36295d4f98a2SYan Zheng out:
3630f0486c68SYan, Zheng 	if (err && rw)
36312ff7e61eSJeff Mahoney 		btrfs_dec_block_group_ro(rc->block_group);
36325d4f98a2SYan Zheng 	iput(rc->data_inode);
36335d4f98a2SYan Zheng 	btrfs_put_block_group(rc->block_group);
36341a0afa0eSJosef Bacik 	free_reloc_control(rc);
36355d4f98a2SYan Zheng 	return err;
36365d4f98a2SYan Zheng }
36375d4f98a2SYan Zheng 
363876dda93cSYan, Zheng static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
363976dda93cSYan, Zheng {
36400b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
364176dda93cSYan, Zheng 	struct btrfs_trans_handle *trans;
364279787eaaSJeff Mahoney 	int ret, err;
364376dda93cSYan, Zheng 
36440b246afaSJeff Mahoney 	trans = btrfs_start_transaction(fs_info->tree_root, 0);
364579787eaaSJeff Mahoney 	if (IS_ERR(trans))
364679787eaaSJeff Mahoney 		return PTR_ERR(trans);
364776dda93cSYan, Zheng 
364876dda93cSYan, Zheng 	memset(&root->root_item.drop_progress, 0,
364976dda93cSYan, Zheng 		sizeof(root->root_item.drop_progress));
3650c8422684SDavid Sterba 	btrfs_set_root_drop_level(&root->root_item, 0);
365176dda93cSYan, Zheng 	btrfs_set_root_refs(&root->root_item, 0);
36520b246afaSJeff Mahoney 	ret = btrfs_update_root(trans, fs_info->tree_root,
365376dda93cSYan, Zheng 				&root->root_key, &root->root_item);
365476dda93cSYan, Zheng 
36553a45bb20SJeff Mahoney 	err = btrfs_end_transaction(trans);
365679787eaaSJeff Mahoney 	if (err)
365779787eaaSJeff Mahoney 		return err;
365879787eaaSJeff Mahoney 	return ret;
365976dda93cSYan, Zheng }
366076dda93cSYan, Zheng 
36615d4f98a2SYan Zheng /*
36625d4f98a2SYan Zheng  * recover relocation interrupted by system crash.
36635d4f98a2SYan Zheng  *
36645d4f98a2SYan Zheng  * this function resumes merging reloc trees with corresponding fs trees.
36655d4f98a2SYan Zheng  * this is important for keeping the sharing of tree blocks
36665d4f98a2SYan Zheng  */
36675d4f98a2SYan Zheng int btrfs_recover_relocation(struct btrfs_root *root)
36685d4f98a2SYan Zheng {
36690b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
36705d4f98a2SYan Zheng 	LIST_HEAD(reloc_roots);
36715d4f98a2SYan Zheng 	struct btrfs_key key;
36725d4f98a2SYan Zheng 	struct btrfs_root *fs_root;
36735d4f98a2SYan Zheng 	struct btrfs_root *reloc_root;
36745d4f98a2SYan Zheng 	struct btrfs_path *path;
36755d4f98a2SYan Zheng 	struct extent_buffer *leaf;
36765d4f98a2SYan Zheng 	struct reloc_control *rc = NULL;
36775d4f98a2SYan Zheng 	struct btrfs_trans_handle *trans;
36785d4f98a2SYan Zheng 	int ret;
36795d4f98a2SYan Zheng 	int err = 0;
36805d4f98a2SYan Zheng 
36815d4f98a2SYan Zheng 	path = btrfs_alloc_path();
36825d4f98a2SYan Zheng 	if (!path)
36835d4f98a2SYan Zheng 		return -ENOMEM;
3684e4058b54SDavid Sterba 	path->reada = READA_BACK;
36855d4f98a2SYan Zheng 
36865d4f98a2SYan Zheng 	key.objectid = BTRFS_TREE_RELOC_OBJECTID;
36875d4f98a2SYan Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
36885d4f98a2SYan Zheng 	key.offset = (u64)-1;
36895d4f98a2SYan Zheng 
36905d4f98a2SYan Zheng 	while (1) {
36910b246afaSJeff Mahoney 		ret = btrfs_search_slot(NULL, fs_info->tree_root, &key,
36925d4f98a2SYan Zheng 					path, 0, 0);
36935d4f98a2SYan Zheng 		if (ret < 0) {
36945d4f98a2SYan Zheng 			err = ret;
36955d4f98a2SYan Zheng 			goto out;
36965d4f98a2SYan Zheng 		}
36975d4f98a2SYan Zheng 		if (ret > 0) {
36985d4f98a2SYan Zheng 			if (path->slots[0] == 0)
36995d4f98a2SYan Zheng 				break;
37005d4f98a2SYan Zheng 			path->slots[0]--;
37015d4f98a2SYan Zheng 		}
37025d4f98a2SYan Zheng 		leaf = path->nodes[0];
37035d4f98a2SYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3704b3b4aa74SDavid Sterba 		btrfs_release_path(path);
37055d4f98a2SYan Zheng 
37065d4f98a2SYan Zheng 		if (key.objectid != BTRFS_TREE_RELOC_OBJECTID ||
37075d4f98a2SYan Zheng 		    key.type != BTRFS_ROOT_ITEM_KEY)
37085d4f98a2SYan Zheng 			break;
37095d4f98a2SYan Zheng 
37103dbf1738SJosef Bacik 		reloc_root = btrfs_read_tree_root(root, &key);
37115d4f98a2SYan Zheng 		if (IS_ERR(reloc_root)) {
37125d4f98a2SYan Zheng 			err = PTR_ERR(reloc_root);
37135d4f98a2SYan Zheng 			goto out;
37145d4f98a2SYan Zheng 		}
37155d4f98a2SYan Zheng 
371692a7cc42SQu Wenruo 		set_bit(BTRFS_ROOT_SHAREABLE, &reloc_root->state);
37175d4f98a2SYan Zheng 		list_add(&reloc_root->root_list, &reloc_roots);
37185d4f98a2SYan Zheng 
37195d4f98a2SYan Zheng 		if (btrfs_root_refs(&reloc_root->root_item) > 0) {
3720a820feb5SDavid Sterba 			fs_root = btrfs_get_fs_root(fs_info,
3721a820feb5SDavid Sterba 					reloc_root->root_key.offset, false);
37225d4f98a2SYan Zheng 			if (IS_ERR(fs_root)) {
372376dda93cSYan, Zheng 				ret = PTR_ERR(fs_root);
372476dda93cSYan, Zheng 				if (ret != -ENOENT) {
372576dda93cSYan, Zheng 					err = ret;
37265d4f98a2SYan Zheng 					goto out;
37275d4f98a2SYan Zheng 				}
372879787eaaSJeff Mahoney 				ret = mark_garbage_root(reloc_root);
372979787eaaSJeff Mahoney 				if (ret < 0) {
373079787eaaSJeff Mahoney 					err = ret;
373179787eaaSJeff Mahoney 					goto out;
373279787eaaSJeff Mahoney 				}
3733932fd26dSJosef Bacik 			} else {
373400246528SJosef Bacik 				btrfs_put_root(fs_root);
373576dda93cSYan, Zheng 			}
37365d4f98a2SYan Zheng 		}
37375d4f98a2SYan Zheng 
37385d4f98a2SYan Zheng 		if (key.offset == 0)
37395d4f98a2SYan Zheng 			break;
37405d4f98a2SYan Zheng 
37415d4f98a2SYan Zheng 		key.offset--;
37425d4f98a2SYan Zheng 	}
3743b3b4aa74SDavid Sterba 	btrfs_release_path(path);
37445d4f98a2SYan Zheng 
37455d4f98a2SYan Zheng 	if (list_empty(&reloc_roots))
37465d4f98a2SYan Zheng 		goto out;
37475d4f98a2SYan Zheng 
3748c258d6e3SQu Wenruo 	rc = alloc_reloc_control(fs_info);
37495d4f98a2SYan Zheng 	if (!rc) {
37505d4f98a2SYan Zheng 		err = -ENOMEM;
37515d4f98a2SYan Zheng 		goto out;
37525d4f98a2SYan Zheng 	}
37535d4f98a2SYan Zheng 
37540b246afaSJeff Mahoney 	rc->extent_root = fs_info->extent_root;
37555d4f98a2SYan Zheng 
37565d4f98a2SYan Zheng 	set_reloc_control(rc);
37575d4f98a2SYan Zheng 
37587a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(rc->extent_root);
37593612b495STsutomu Itoh 	if (IS_ERR(trans)) {
37603612b495STsutomu Itoh 		err = PTR_ERR(trans);
3761fb2d83eeSJosef Bacik 		goto out_unset;
37623612b495STsutomu Itoh 	}
37633fd0a558SYan, Zheng 
37643fd0a558SYan, Zheng 	rc->merge_reloc_tree = 1;
37653fd0a558SYan, Zheng 
37665d4f98a2SYan Zheng 	while (!list_empty(&reloc_roots)) {
37675d4f98a2SYan Zheng 		reloc_root = list_entry(reloc_roots.next,
37685d4f98a2SYan Zheng 					struct btrfs_root, root_list);
37695d4f98a2SYan Zheng 		list_del(&reloc_root->root_list);
37705d4f98a2SYan Zheng 
37715d4f98a2SYan Zheng 		if (btrfs_root_refs(&reloc_root->root_item) == 0) {
37725d4f98a2SYan Zheng 			list_add_tail(&reloc_root->root_list,
37735d4f98a2SYan Zheng 				      &rc->reloc_roots);
37745d4f98a2SYan Zheng 			continue;
37755d4f98a2SYan Zheng 		}
37765d4f98a2SYan Zheng 
3777a820feb5SDavid Sterba 		fs_root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
3778a820feb5SDavid Sterba 					    false);
377979787eaaSJeff Mahoney 		if (IS_ERR(fs_root)) {
378079787eaaSJeff Mahoney 			err = PTR_ERR(fs_root);
3781ca1aa281SJosef Bacik 			list_add_tail(&reloc_root->root_list, &reloc_roots);
37821402d17dSXiyu Yang 			btrfs_end_transaction(trans);
3783fb2d83eeSJosef Bacik 			goto out_unset;
378479787eaaSJeff Mahoney 		}
37855d4f98a2SYan Zheng 
3786ffd7b339SJeff Mahoney 		err = __add_reloc_root(reloc_root);
378779787eaaSJeff Mahoney 		BUG_ON(err < 0); /* -ENOMEM or logic error */
3788f44deb74SJosef Bacik 		fs_root->reloc_root = btrfs_grab_root(reloc_root);
378900246528SJosef Bacik 		btrfs_put_root(fs_root);
37905d4f98a2SYan Zheng 	}
37915d4f98a2SYan Zheng 
37923a45bb20SJeff Mahoney 	err = btrfs_commit_transaction(trans);
379379787eaaSJeff Mahoney 	if (err)
3794fb2d83eeSJosef Bacik 		goto out_unset;
37955d4f98a2SYan Zheng 
37965d4f98a2SYan Zheng 	merge_reloc_roots(rc);
37975d4f98a2SYan Zheng 
37985d4f98a2SYan Zheng 	unset_reloc_control(rc);
37995d4f98a2SYan Zheng 
38007a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(rc->extent_root);
380162b99540SQu Wenruo 	if (IS_ERR(trans)) {
38023612b495STsutomu Itoh 		err = PTR_ERR(trans);
38036217b0faSJosef Bacik 		goto out_clean;
380462b99540SQu Wenruo 	}
38053a45bb20SJeff Mahoney 	err = btrfs_commit_transaction(trans);
38066217b0faSJosef Bacik out_clean:
3807d2311e69SQu Wenruo 	ret = clean_dirty_subvols(rc);
3808d2311e69SQu Wenruo 	if (ret < 0 && !err)
3809d2311e69SQu Wenruo 		err = ret;
3810fb2d83eeSJosef Bacik out_unset:
3811fb2d83eeSJosef Bacik 	unset_reloc_control(rc);
38121a0afa0eSJosef Bacik 	free_reloc_control(rc);
38133612b495STsutomu Itoh out:
3814aca1bba6SLiu Bo 	free_reloc_roots(&reloc_roots);
3815aca1bba6SLiu Bo 
38165d4f98a2SYan Zheng 	btrfs_free_path(path);
38175d4f98a2SYan Zheng 
38185d4f98a2SYan Zheng 	if (err == 0) {
38195d4f98a2SYan Zheng 		/* cleanup orphan inode in data relocation tree */
3820aeb935a4SQu Wenruo 		fs_root = btrfs_grab_root(fs_info->data_reloc_root);
3821aeb935a4SQu Wenruo 		ASSERT(fs_root);
382266b4ffd1SJosef Bacik 		err = btrfs_orphan_cleanup(fs_root);
382300246528SJosef Bacik 		btrfs_put_root(fs_root);
3824932fd26dSJosef Bacik 	}
38255d4f98a2SYan Zheng 	return err;
38265d4f98a2SYan Zheng }
38275d4f98a2SYan Zheng 
38285d4f98a2SYan Zheng /*
38295d4f98a2SYan Zheng  * helper to add ordered checksum for data relocation.
38305d4f98a2SYan Zheng  *
38315d4f98a2SYan Zheng  * cloning checksum properly handles the nodatasum extents.
38325d4f98a2SYan Zheng  * it also saves CPU time to re-calculate the checksum.
38335d4f98a2SYan Zheng  */
38347bfa9535SNikolay Borisov int btrfs_reloc_clone_csums(struct btrfs_inode *inode, u64 file_pos, u64 len)
38355d4f98a2SYan Zheng {
38367bfa9535SNikolay Borisov 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
38375d4f98a2SYan Zheng 	struct btrfs_ordered_sum *sums;
38385d4f98a2SYan Zheng 	struct btrfs_ordered_extent *ordered;
38395d4f98a2SYan Zheng 	int ret;
38405d4f98a2SYan Zheng 	u64 disk_bytenr;
38414577b014SJosef Bacik 	u64 new_bytenr;
38425d4f98a2SYan Zheng 	LIST_HEAD(list);
38435d4f98a2SYan Zheng 
38447bfa9535SNikolay Borisov 	ordered = btrfs_lookup_ordered_extent(inode, file_pos);
3845bffe633eSOmar Sandoval 	BUG_ON(ordered->file_offset != file_pos || ordered->num_bytes != len);
38465d4f98a2SYan Zheng 
38477bfa9535SNikolay Borisov 	disk_bytenr = file_pos + inode->index_cnt;
38480b246afaSJeff Mahoney 	ret = btrfs_lookup_csums_range(fs_info->csum_root, disk_bytenr,
3849a2de733cSArne Jansen 				       disk_bytenr + len - 1, &list, 0);
385079787eaaSJeff Mahoney 	if (ret)
385179787eaaSJeff Mahoney 		goto out;
38525d4f98a2SYan Zheng 
38535d4f98a2SYan Zheng 	while (!list_empty(&list)) {
38545d4f98a2SYan Zheng 		sums = list_entry(list.next, struct btrfs_ordered_sum, list);
38555d4f98a2SYan Zheng 		list_del_init(&sums->list);
38565d4f98a2SYan Zheng 
38574577b014SJosef Bacik 		/*
38584577b014SJosef Bacik 		 * We need to offset the new_bytenr based on where the csum is.
38594577b014SJosef Bacik 		 * We need to do this because we will read in entire prealloc
38604577b014SJosef Bacik 		 * extents but we may have written to say the middle of the
38614577b014SJosef Bacik 		 * prealloc extent, so we need to make sure the csum goes with
38624577b014SJosef Bacik 		 * the right disk offset.
38634577b014SJosef Bacik 		 *
38644577b014SJosef Bacik 		 * We can do this because the data reloc inode refers strictly
38654577b014SJosef Bacik 		 * to the on disk bytes, so we don't have to worry about
38664577b014SJosef Bacik 		 * disk_len vs real len like with real inodes since it's all
38674577b014SJosef Bacik 		 * disk length.
38684577b014SJosef Bacik 		 */
3869bffe633eSOmar Sandoval 		new_bytenr = ordered->disk_bytenr + sums->bytenr - disk_bytenr;
38704577b014SJosef Bacik 		sums->bytenr = new_bytenr;
38715d4f98a2SYan Zheng 
3872f9756261SNikolay Borisov 		btrfs_add_ordered_sum(ordered, sums);
38735d4f98a2SYan Zheng 	}
387479787eaaSJeff Mahoney out:
38755d4f98a2SYan Zheng 	btrfs_put_ordered_extent(ordered);
3876411fc6bcSAndi Kleen 	return ret;
38775d4f98a2SYan Zheng }
38783fd0a558SYan, Zheng 
387983d4cfd4SJosef Bacik int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
38803fd0a558SYan, Zheng 			  struct btrfs_root *root, struct extent_buffer *buf,
38813fd0a558SYan, Zheng 			  struct extent_buffer *cow)
38823fd0a558SYan, Zheng {
38830b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
38843fd0a558SYan, Zheng 	struct reloc_control *rc;
3885a26195a5SQu Wenruo 	struct btrfs_backref_node *node;
38863fd0a558SYan, Zheng 	int first_cow = 0;
38873fd0a558SYan, Zheng 	int level;
388883d4cfd4SJosef Bacik 	int ret = 0;
38893fd0a558SYan, Zheng 
38900b246afaSJeff Mahoney 	rc = fs_info->reloc_ctl;
38913fd0a558SYan, Zheng 	if (!rc)
389283d4cfd4SJosef Bacik 		return 0;
38933fd0a558SYan, Zheng 
38943fd0a558SYan, Zheng 	BUG_ON(rc->stage == UPDATE_DATA_PTRS &&
38953fd0a558SYan, Zheng 	       root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID);
38963fd0a558SYan, Zheng 
38973fd0a558SYan, Zheng 	level = btrfs_header_level(buf);
38983fd0a558SYan, Zheng 	if (btrfs_header_generation(buf) <=
38993fd0a558SYan, Zheng 	    btrfs_root_last_snapshot(&root->root_item))
39003fd0a558SYan, Zheng 		first_cow = 1;
39013fd0a558SYan, Zheng 
39023fd0a558SYan, Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID &&
39033fd0a558SYan, Zheng 	    rc->create_reloc_tree) {
39043fd0a558SYan, Zheng 		WARN_ON(!first_cow && level == 0);
39053fd0a558SYan, Zheng 
39063fd0a558SYan, Zheng 		node = rc->backref_cache.path[level];
39073fd0a558SYan, Zheng 		BUG_ON(node->bytenr != buf->start &&
39083fd0a558SYan, Zheng 		       node->new_bytenr != buf->start);
39093fd0a558SYan, Zheng 
3910b0fe7078SQu Wenruo 		btrfs_backref_drop_node_buffer(node);
391167439dadSDavid Sterba 		atomic_inc(&cow->refs);
39123fd0a558SYan, Zheng 		node->eb = cow;
39133fd0a558SYan, Zheng 		node->new_bytenr = cow->start;
39143fd0a558SYan, Zheng 
39153fd0a558SYan, Zheng 		if (!node->pending) {
39163fd0a558SYan, Zheng 			list_move_tail(&node->list,
39173fd0a558SYan, Zheng 				       &rc->backref_cache.pending[level]);
39183fd0a558SYan, Zheng 			node->pending = 1;
39193fd0a558SYan, Zheng 		}
39203fd0a558SYan, Zheng 
39213fd0a558SYan, Zheng 		if (first_cow)
39229569cc20SQu Wenruo 			mark_block_processed(rc, node);
39233fd0a558SYan, Zheng 
39243fd0a558SYan, Zheng 		if (first_cow && level > 0)
39253fd0a558SYan, Zheng 			rc->nodes_relocated += buf->len;
39263fd0a558SYan, Zheng 	}
39273fd0a558SYan, Zheng 
392883d4cfd4SJosef Bacik 	if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS)
39293fd0a558SYan, Zheng 		ret = replace_file_extents(trans, rc, root, cow);
393083d4cfd4SJosef Bacik 	return ret;
39313fd0a558SYan, Zheng }
39323fd0a558SYan, Zheng 
39333fd0a558SYan, Zheng /*
39343fd0a558SYan, Zheng  * called before creating snapshot. it calculates metadata reservation
393501327610SNicholas D Steeves  * required for relocating tree blocks in the snapshot
39363fd0a558SYan, Zheng  */
3937147d256eSZhaolei void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending,
39383fd0a558SYan, Zheng 			      u64 *bytes_to_reserve)
39393fd0a558SYan, Zheng {
394010995c04SQu Wenruo 	struct btrfs_root *root = pending->root;
394110995c04SQu Wenruo 	struct reloc_control *rc = root->fs_info->reloc_ctl;
39423fd0a558SYan, Zheng 
39436282675eSQu Wenruo 	if (!rc || !have_reloc_root(root))
39443fd0a558SYan, Zheng 		return;
39453fd0a558SYan, Zheng 
39463fd0a558SYan, Zheng 	if (!rc->merge_reloc_tree)
39473fd0a558SYan, Zheng 		return;
39483fd0a558SYan, Zheng 
39493fd0a558SYan, Zheng 	root = root->reloc_root;
39503fd0a558SYan, Zheng 	BUG_ON(btrfs_root_refs(&root->root_item) == 0);
39513fd0a558SYan, Zheng 	/*
39523fd0a558SYan, Zheng 	 * relocation is in the stage of merging trees. the space
39533fd0a558SYan, Zheng 	 * used by merging a reloc tree is twice the size of
39543fd0a558SYan, Zheng 	 * relocated tree nodes in the worst case. half for cowing
39553fd0a558SYan, Zheng 	 * the reloc tree, half for cowing the fs tree. the space
39563fd0a558SYan, Zheng 	 * used by cowing the reloc tree will be freed after the
39573fd0a558SYan, Zheng 	 * tree is dropped. if we create snapshot, cowing the fs
39583fd0a558SYan, Zheng 	 * tree may use more space than it frees. so we need
39593fd0a558SYan, Zheng 	 * reserve extra space.
39603fd0a558SYan, Zheng 	 */
39613fd0a558SYan, Zheng 	*bytes_to_reserve += rc->nodes_relocated;
39623fd0a558SYan, Zheng }
39633fd0a558SYan, Zheng 
39643fd0a558SYan, Zheng /*
39653fd0a558SYan, Zheng  * called after snapshot is created. migrate block reservation
39663fd0a558SYan, Zheng  * and create reloc root for the newly created snapshot
3967f44deb74SJosef Bacik  *
3968f44deb74SJosef Bacik  * This is similar to btrfs_init_reloc_root(), we come out of here with two
3969f44deb74SJosef Bacik  * references held on the reloc_root, one for root->reloc_root and one for
3970f44deb74SJosef Bacik  * rc->reloc_roots.
39713fd0a558SYan, Zheng  */
397249b25e05SJeff Mahoney int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
39733fd0a558SYan, Zheng 			       struct btrfs_pending_snapshot *pending)
39743fd0a558SYan, Zheng {
39753fd0a558SYan, Zheng 	struct btrfs_root *root = pending->root;
39763fd0a558SYan, Zheng 	struct btrfs_root *reloc_root;
39773fd0a558SYan, Zheng 	struct btrfs_root *new_root;
397810995c04SQu Wenruo 	struct reloc_control *rc = root->fs_info->reloc_ctl;
39793fd0a558SYan, Zheng 	int ret;
39803fd0a558SYan, Zheng 
39816282675eSQu Wenruo 	if (!rc || !have_reloc_root(root))
398249b25e05SJeff Mahoney 		return 0;
39833fd0a558SYan, Zheng 
39843fd0a558SYan, Zheng 	rc = root->fs_info->reloc_ctl;
39853fd0a558SYan, Zheng 	rc->merging_rsv_size += rc->nodes_relocated;
39863fd0a558SYan, Zheng 
39873fd0a558SYan, Zheng 	if (rc->merge_reloc_tree) {
39883fd0a558SYan, Zheng 		ret = btrfs_block_rsv_migrate(&pending->block_rsv,
39893fd0a558SYan, Zheng 					      rc->block_rsv,
39903a584174SLu Fengqi 					      rc->nodes_relocated, true);
399149b25e05SJeff Mahoney 		if (ret)
399249b25e05SJeff Mahoney 			return ret;
39933fd0a558SYan, Zheng 	}
39943fd0a558SYan, Zheng 
39953fd0a558SYan, Zheng 	new_root = pending->snap;
39963fd0a558SYan, Zheng 	reloc_root = create_reloc_root(trans, root->reloc_root,
39973fd0a558SYan, Zheng 				       new_root->root_key.objectid);
399849b25e05SJeff Mahoney 	if (IS_ERR(reloc_root))
399949b25e05SJeff Mahoney 		return PTR_ERR(reloc_root);
40003fd0a558SYan, Zheng 
4001ffd7b339SJeff Mahoney 	ret = __add_reloc_root(reloc_root);
4002ffd7b339SJeff Mahoney 	BUG_ON(ret < 0);
4003f44deb74SJosef Bacik 	new_root->reloc_root = btrfs_grab_root(reloc_root);
40043fd0a558SYan, Zheng 
400549b25e05SJeff Mahoney 	if (rc->create_reloc_tree)
40063fd0a558SYan, Zheng 		ret = clone_backref_node(trans, rc, root, reloc_root);
400749b25e05SJeff Mahoney 	return ret;
40083fd0a558SYan, Zheng }
4009