xref: /openbmc/linux/fs/btrfs/relocation.c (revision 92a7cc42)
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"
21581bb050SLi Zefan #include "inode-map.h"
2262b99540SQu Wenruo #include "qgroup.h"
23cdccee99SLiu Bo #include "print-tree.h"
2486736342SJosef Bacik #include "delalloc-space.h"
25aac0023cSJosef Bacik #include "block-group.h"
2619b546d7SQu Wenruo #include "backref.h"
27e9a28dc5SQu Wenruo #include "misc.h"
285d4f98a2SYan Zheng 
295d4f98a2SYan Zheng /*
300c891389SQu Wenruo  * Relocation overview
310c891389SQu Wenruo  *
320c891389SQu Wenruo  * [What does relocation do]
330c891389SQu Wenruo  *
340c891389SQu Wenruo  * The objective of relocation is to relocate all extents of the target block
350c891389SQu Wenruo  * group to other block groups.
360c891389SQu Wenruo  * This is utilized by resize (shrink only), profile converting, compacting
370c891389SQu Wenruo  * space, or balance routine to spread chunks over devices.
380c891389SQu Wenruo  *
390c891389SQu Wenruo  * 		Before		|		After
400c891389SQu Wenruo  * ------------------------------------------------------------------
410c891389SQu Wenruo  *  BG A: 10 data extents	| BG A: deleted
420c891389SQu Wenruo  *  BG B:  2 data extents	| BG B: 10 data extents (2 old + 8 relocated)
430c891389SQu Wenruo  *  BG C:  1 extents		| BG C:  3 data extents (1 old + 2 relocated)
440c891389SQu Wenruo  *
450c891389SQu Wenruo  * [How does relocation work]
460c891389SQu Wenruo  *
470c891389SQu Wenruo  * 1.   Mark the target block group read-only
480c891389SQu Wenruo  *      New extents won't be allocated from the target block group.
490c891389SQu Wenruo  *
500c891389SQu Wenruo  * 2.1  Record each extent in the target block group
510c891389SQu Wenruo  *      To build a proper map of extents to be relocated.
520c891389SQu Wenruo  *
530c891389SQu Wenruo  * 2.2  Build data reloc tree and reloc trees
540c891389SQu Wenruo  *      Data reloc tree will contain an inode, recording all newly relocated
550c891389SQu Wenruo  *      data extents.
560c891389SQu Wenruo  *      There will be only one data reloc tree for one data block group.
570c891389SQu Wenruo  *
580c891389SQu Wenruo  *      Reloc tree will be a special snapshot of its source tree, containing
590c891389SQu Wenruo  *      relocated tree blocks.
600c891389SQu Wenruo  *      Each tree referring to a tree block in target block group will get its
610c891389SQu Wenruo  *      reloc tree built.
620c891389SQu Wenruo  *
630c891389SQu Wenruo  * 2.3  Swap source tree with its corresponding reloc tree
640c891389SQu Wenruo  *      Each involved tree only refers to new extents after swap.
650c891389SQu Wenruo  *
660c891389SQu Wenruo  * 3.   Cleanup reloc trees and data reloc tree.
670c891389SQu Wenruo  *      As old extents in the target block group are still referenced by reloc
680c891389SQu Wenruo  *      trees, we need to clean them up before really freeing the target block
690c891389SQu Wenruo  *      group.
700c891389SQu Wenruo  *
710c891389SQu Wenruo  * The main complexity is in steps 2.2 and 2.3.
720c891389SQu Wenruo  *
730c891389SQu Wenruo  * The entry point of relocation is relocate_block_group() function.
740c891389SQu Wenruo  */
750c891389SQu Wenruo 
760647bf56SWang Shilong #define RELOCATION_RESERVED_NODES	256
772a979612SQu Wenruo /*
785d4f98a2SYan Zheng  * map address of tree root to tree
795d4f98a2SYan Zheng  */
805d4f98a2SYan Zheng struct mapping_node {
81e9a28dc5SQu Wenruo 	struct {
825d4f98a2SYan Zheng 		struct rb_node rb_node;
835d4f98a2SYan Zheng 		u64 bytenr;
84e9a28dc5SQu Wenruo 	}; /* Use rb_simle_node for search/insert */
855d4f98a2SYan Zheng 	void *data;
865d4f98a2SYan Zheng };
875d4f98a2SYan Zheng 
885d4f98a2SYan Zheng struct mapping_tree {
895d4f98a2SYan Zheng 	struct rb_root rb_root;
905d4f98a2SYan Zheng 	spinlock_t lock;
915d4f98a2SYan Zheng };
925d4f98a2SYan Zheng 
935d4f98a2SYan Zheng /*
945d4f98a2SYan Zheng  * present a tree block to process
955d4f98a2SYan Zheng  */
965d4f98a2SYan Zheng struct tree_block {
97e9a28dc5SQu Wenruo 	struct {
985d4f98a2SYan Zheng 		struct rb_node rb_node;
995d4f98a2SYan Zheng 		u64 bytenr;
100e9a28dc5SQu Wenruo 	}; /* Use rb_simple_node for search/insert */
1015d4f98a2SYan Zheng 	struct btrfs_key key;
1025d4f98a2SYan Zheng 	unsigned int level:8;
1035d4f98a2SYan Zheng 	unsigned int key_ready:1;
1045d4f98a2SYan Zheng };
1055d4f98a2SYan Zheng 
1060257bb82SYan, Zheng #define MAX_EXTENTS 128
1070257bb82SYan, Zheng 
1080257bb82SYan, Zheng struct file_extent_cluster {
1090257bb82SYan, Zheng 	u64 start;
1100257bb82SYan, Zheng 	u64 end;
1110257bb82SYan, Zheng 	u64 boundary[MAX_EXTENTS];
1120257bb82SYan, Zheng 	unsigned int nr;
1130257bb82SYan, Zheng };
1140257bb82SYan, Zheng 
1155d4f98a2SYan Zheng struct reloc_control {
1165d4f98a2SYan Zheng 	/* block group to relocate */
11732da5386SDavid Sterba 	struct btrfs_block_group *block_group;
1185d4f98a2SYan Zheng 	/* extent tree */
1195d4f98a2SYan Zheng 	struct btrfs_root *extent_root;
1205d4f98a2SYan Zheng 	/* inode for moving data */
1215d4f98a2SYan Zheng 	struct inode *data_inode;
1223fd0a558SYan, Zheng 
1233fd0a558SYan, Zheng 	struct btrfs_block_rsv *block_rsv;
1243fd0a558SYan, Zheng 
125a26195a5SQu Wenruo 	struct btrfs_backref_cache backref_cache;
1263fd0a558SYan, Zheng 
1273fd0a558SYan, Zheng 	struct file_extent_cluster cluster;
1285d4f98a2SYan Zheng 	/* tree blocks have been processed */
1295d4f98a2SYan Zheng 	struct extent_io_tree processed_blocks;
1305d4f98a2SYan Zheng 	/* map start of tree root to corresponding reloc tree */
1315d4f98a2SYan Zheng 	struct mapping_tree reloc_root_tree;
1325d4f98a2SYan Zheng 	/* list of reloc trees */
1335d4f98a2SYan Zheng 	struct list_head reloc_roots;
134d2311e69SQu Wenruo 	/* list of subvolume trees that get relocated */
135d2311e69SQu Wenruo 	struct list_head dirty_subvol_roots;
1363fd0a558SYan, Zheng 	/* size of metadata reservation for merging reloc trees */
1373fd0a558SYan, Zheng 	u64 merging_rsv_size;
1383fd0a558SYan, Zheng 	/* size of relocated tree nodes */
1393fd0a558SYan, Zheng 	u64 nodes_relocated;
1400647bf56SWang Shilong 	/* reserved size for block group relocation*/
1410647bf56SWang Shilong 	u64 reserved_bytes;
1423fd0a558SYan, Zheng 
1435d4f98a2SYan Zheng 	u64 search_start;
1445d4f98a2SYan Zheng 	u64 extents_found;
1453fd0a558SYan, Zheng 
1463fd0a558SYan, Zheng 	unsigned int stage:8;
1473fd0a558SYan, Zheng 	unsigned int create_reloc_tree:1;
1483fd0a558SYan, Zheng 	unsigned int merge_reloc_tree:1;
1495d4f98a2SYan Zheng 	unsigned int found_file_extent:1;
1505d4f98a2SYan Zheng };
1515d4f98a2SYan Zheng 
1525d4f98a2SYan Zheng /* stages of data relocation */
1535d4f98a2SYan Zheng #define MOVE_DATA_EXTENTS	0
1545d4f98a2SYan Zheng #define UPDATE_DATA_PTRS	1
1555d4f98a2SYan Zheng 
1569569cc20SQu Wenruo static void mark_block_processed(struct reloc_control *rc,
157a26195a5SQu Wenruo 				 struct btrfs_backref_node *node)
1589569cc20SQu Wenruo {
1599569cc20SQu Wenruo 	u32 blocksize;
1609569cc20SQu Wenruo 
1619569cc20SQu Wenruo 	if (node->level == 0 ||
1629569cc20SQu Wenruo 	    in_range(node->bytenr, rc->block_group->start,
1639569cc20SQu Wenruo 		     rc->block_group->length)) {
1649569cc20SQu Wenruo 		blocksize = rc->extent_root->fs_info->nodesize;
1659569cc20SQu Wenruo 		set_extent_bits(&rc->processed_blocks, node->bytenr,
1669569cc20SQu Wenruo 				node->bytenr + blocksize - 1, EXTENT_DIRTY);
1679569cc20SQu Wenruo 	}
1689569cc20SQu Wenruo 	node->processed = 1;
1699569cc20SQu Wenruo }
1709569cc20SQu Wenruo 
1715d4f98a2SYan Zheng 
1725d4f98a2SYan Zheng static void mapping_tree_init(struct mapping_tree *tree)
1735d4f98a2SYan Zheng {
1746bef4d31SEric Paris 	tree->rb_root = RB_ROOT;
1755d4f98a2SYan Zheng 	spin_lock_init(&tree->lock);
1765d4f98a2SYan Zheng }
1775d4f98a2SYan Zheng 
1785d4f98a2SYan Zheng /*
1795d4f98a2SYan Zheng  * walk up backref nodes until reach node presents tree root
1805d4f98a2SYan Zheng  */
181a26195a5SQu Wenruo static struct btrfs_backref_node *walk_up_backref(
182a26195a5SQu Wenruo 		struct btrfs_backref_node *node,
183a26195a5SQu Wenruo 		struct btrfs_backref_edge *edges[], int *index)
1845d4f98a2SYan Zheng {
185a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
1865d4f98a2SYan Zheng 	int idx = *index;
1875d4f98a2SYan Zheng 
1885d4f98a2SYan Zheng 	while (!list_empty(&node->upper)) {
1895d4f98a2SYan Zheng 		edge = list_entry(node->upper.next,
190a26195a5SQu Wenruo 				  struct btrfs_backref_edge, list[LOWER]);
1915d4f98a2SYan Zheng 		edges[idx++] = edge;
1925d4f98a2SYan Zheng 		node = edge->node[UPPER];
1935d4f98a2SYan Zheng 	}
1943fd0a558SYan, Zheng 	BUG_ON(node->detached);
1955d4f98a2SYan Zheng 	*index = idx;
1965d4f98a2SYan Zheng 	return node;
1975d4f98a2SYan Zheng }
1985d4f98a2SYan Zheng 
1995d4f98a2SYan Zheng /*
2005d4f98a2SYan Zheng  * walk down backref nodes to find start of next reference path
2015d4f98a2SYan Zheng  */
202a26195a5SQu Wenruo static struct btrfs_backref_node *walk_down_backref(
203a26195a5SQu Wenruo 		struct btrfs_backref_edge *edges[], int *index)
2045d4f98a2SYan Zheng {
205a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
206a26195a5SQu Wenruo 	struct btrfs_backref_node *lower;
2075d4f98a2SYan Zheng 	int idx = *index;
2085d4f98a2SYan Zheng 
2095d4f98a2SYan Zheng 	while (idx > 0) {
2105d4f98a2SYan Zheng 		edge = edges[idx - 1];
2115d4f98a2SYan Zheng 		lower = edge->node[LOWER];
2125d4f98a2SYan Zheng 		if (list_is_last(&edge->list[LOWER], &lower->upper)) {
2135d4f98a2SYan Zheng 			idx--;
2145d4f98a2SYan Zheng 			continue;
2155d4f98a2SYan Zheng 		}
2165d4f98a2SYan Zheng 		edge = list_entry(edge->list[LOWER].next,
217a26195a5SQu Wenruo 				  struct btrfs_backref_edge, list[LOWER]);
2185d4f98a2SYan Zheng 		edges[idx - 1] = edge;
2195d4f98a2SYan Zheng 		*index = idx;
2205d4f98a2SYan Zheng 		return edge->node[UPPER];
2215d4f98a2SYan Zheng 	}
2225d4f98a2SYan Zheng 	*index = 0;
2235d4f98a2SYan Zheng 	return NULL;
2245d4f98a2SYan Zheng }
2255d4f98a2SYan Zheng 
226a26195a5SQu Wenruo static void update_backref_node(struct btrfs_backref_cache *cache,
227a26195a5SQu Wenruo 				struct btrfs_backref_node *node, u64 bytenr)
2283fd0a558SYan, Zheng {
2293fd0a558SYan, Zheng 	struct rb_node *rb_node;
2303fd0a558SYan, Zheng 	rb_erase(&node->rb_node, &cache->rb_root);
2313fd0a558SYan, Zheng 	node->bytenr = bytenr;
232e9a28dc5SQu Wenruo 	rb_node = rb_simple_insert(&cache->rb_root, node->bytenr, &node->rb_node);
23343c04fb1SJeff Mahoney 	if (rb_node)
234982c92cbSQu Wenruo 		btrfs_backref_panic(cache->fs_info, bytenr, -EEXIST);
2353fd0a558SYan, Zheng }
2363fd0a558SYan, Zheng 
2373fd0a558SYan, Zheng /*
2383fd0a558SYan, Zheng  * update backref cache after a transaction commit
2393fd0a558SYan, Zheng  */
2403fd0a558SYan, Zheng static int update_backref_cache(struct btrfs_trans_handle *trans,
241a26195a5SQu Wenruo 				struct btrfs_backref_cache *cache)
2423fd0a558SYan, Zheng {
243a26195a5SQu Wenruo 	struct btrfs_backref_node *node;
2443fd0a558SYan, Zheng 	int level = 0;
2453fd0a558SYan, Zheng 
2463fd0a558SYan, Zheng 	if (cache->last_trans == 0) {
2473fd0a558SYan, Zheng 		cache->last_trans = trans->transid;
2483fd0a558SYan, Zheng 		return 0;
2493fd0a558SYan, Zheng 	}
2503fd0a558SYan, Zheng 
2513fd0a558SYan, Zheng 	if (cache->last_trans == trans->transid)
2523fd0a558SYan, Zheng 		return 0;
2533fd0a558SYan, Zheng 
2543fd0a558SYan, Zheng 	/*
2553fd0a558SYan, Zheng 	 * detached nodes are used to avoid unnecessary backref
2563fd0a558SYan, Zheng 	 * lookup. transaction commit changes the extent tree.
2573fd0a558SYan, Zheng 	 * so the detached nodes are no longer useful.
2583fd0a558SYan, Zheng 	 */
2593fd0a558SYan, Zheng 	while (!list_empty(&cache->detached)) {
2603fd0a558SYan, Zheng 		node = list_entry(cache->detached.next,
261a26195a5SQu Wenruo 				  struct btrfs_backref_node, list);
262023acb07SQu Wenruo 		btrfs_backref_cleanup_node(cache, node);
2633fd0a558SYan, Zheng 	}
2643fd0a558SYan, Zheng 
2653fd0a558SYan, Zheng 	while (!list_empty(&cache->changed)) {
2663fd0a558SYan, Zheng 		node = list_entry(cache->changed.next,
267a26195a5SQu Wenruo 				  struct btrfs_backref_node, list);
2683fd0a558SYan, Zheng 		list_del_init(&node->list);
2693fd0a558SYan, Zheng 		BUG_ON(node->pending);
2703fd0a558SYan, Zheng 		update_backref_node(cache, node, node->new_bytenr);
2713fd0a558SYan, Zheng 	}
2723fd0a558SYan, Zheng 
2733fd0a558SYan, Zheng 	/*
2743fd0a558SYan, Zheng 	 * some nodes can be left in the pending list if there were
2753fd0a558SYan, Zheng 	 * errors during processing the pending nodes.
2763fd0a558SYan, Zheng 	 */
2773fd0a558SYan, Zheng 	for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2783fd0a558SYan, Zheng 		list_for_each_entry(node, &cache->pending[level], list) {
2793fd0a558SYan, Zheng 			BUG_ON(!node->pending);
2803fd0a558SYan, Zheng 			if (node->bytenr == node->new_bytenr)
2813fd0a558SYan, Zheng 				continue;
2823fd0a558SYan, Zheng 			update_backref_node(cache, node, node->new_bytenr);
2833fd0a558SYan, Zheng 		}
2843fd0a558SYan, Zheng 	}
2853fd0a558SYan, Zheng 
2863fd0a558SYan, Zheng 	cache->last_trans = 0;
2873fd0a558SYan, Zheng 	return 1;
2883fd0a558SYan, Zheng }
2893fd0a558SYan, Zheng 
2906282675eSQu Wenruo static bool reloc_root_is_dead(struct btrfs_root *root)
2916282675eSQu Wenruo {
2926282675eSQu Wenruo 	/*
2936282675eSQu Wenruo 	 * Pair with set_bit/clear_bit in clean_dirty_subvols and
2946282675eSQu Wenruo 	 * btrfs_update_reloc_root. We need to see the updated bit before
2956282675eSQu Wenruo 	 * trying to access reloc_root
2966282675eSQu Wenruo 	 */
2976282675eSQu Wenruo 	smp_rmb();
2986282675eSQu Wenruo 	if (test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state))
2996282675eSQu Wenruo 		return true;
3006282675eSQu Wenruo 	return false;
3016282675eSQu Wenruo }
3026282675eSQu Wenruo 
3036282675eSQu Wenruo /*
3046282675eSQu Wenruo  * Check if this subvolume tree has valid reloc tree.
3056282675eSQu Wenruo  *
3066282675eSQu Wenruo  * Reloc tree after swap is considered dead, thus not considered as valid.
3076282675eSQu Wenruo  * This is enough for most callers, as they don't distinguish dead reloc root
30855465730SQu Wenruo  * from no reloc root.  But btrfs_should_ignore_reloc_root() below is a
30955465730SQu Wenruo  * special case.
3106282675eSQu Wenruo  */
3116282675eSQu Wenruo static bool have_reloc_root(struct btrfs_root *root)
3126282675eSQu Wenruo {
3136282675eSQu Wenruo 	if (reloc_root_is_dead(root))
3146282675eSQu Wenruo 		return false;
3156282675eSQu Wenruo 	if (!root->reloc_root)
3166282675eSQu Wenruo 		return false;
3176282675eSQu Wenruo 	return true;
3186282675eSQu Wenruo }
319f2a97a9dSDavid Sterba 
32055465730SQu Wenruo int btrfs_should_ignore_reloc_root(struct btrfs_root *root)
3213fd0a558SYan, Zheng {
3223fd0a558SYan, Zheng 	struct btrfs_root *reloc_root;
3233fd0a558SYan, Zheng 
32492a7cc42SQu Wenruo 	if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
3253fd0a558SYan, Zheng 		return 0;
3263fd0a558SYan, Zheng 
3276282675eSQu Wenruo 	/* This root has been merged with its reloc tree, we can ignore it */
3286282675eSQu Wenruo 	if (reloc_root_is_dead(root))
3296282675eSQu Wenruo 		return 1;
3306282675eSQu Wenruo 
3313fd0a558SYan, Zheng 	reloc_root = root->reloc_root;
3323fd0a558SYan, Zheng 	if (!reloc_root)
3333fd0a558SYan, Zheng 		return 0;
3343fd0a558SYan, Zheng 
3354d4225fcSJosef Bacik 	if (btrfs_header_generation(reloc_root->commit_root) ==
3364d4225fcSJosef Bacik 	    root->fs_info->running_transaction->transid)
3373fd0a558SYan, Zheng 		return 0;
3383fd0a558SYan, Zheng 	/*
3393fd0a558SYan, Zheng 	 * if there is reloc tree and it was created in previous
3403fd0a558SYan, Zheng 	 * transaction backref lookup can find the reloc tree,
3413fd0a558SYan, Zheng 	 * so backref node for the fs tree root is useless for
3423fd0a558SYan, Zheng 	 * relocation.
3433fd0a558SYan, Zheng 	 */
3443fd0a558SYan, Zheng 	return 1;
3453fd0a558SYan, Zheng }
34655465730SQu Wenruo 
3475d4f98a2SYan Zheng /*
3485d4f98a2SYan Zheng  * find reloc tree by address of tree root
3495d4f98a2SYan Zheng  */
3502433bea5SQu Wenruo struct btrfs_root *find_reloc_root(struct btrfs_fs_info *fs_info, u64 bytenr)
3515d4f98a2SYan Zheng {
3522433bea5SQu Wenruo 	struct reloc_control *rc = fs_info->reloc_ctl;
3535d4f98a2SYan Zheng 	struct rb_node *rb_node;
3545d4f98a2SYan Zheng 	struct mapping_node *node;
3555d4f98a2SYan Zheng 	struct btrfs_root *root = NULL;
3565d4f98a2SYan Zheng 
3572433bea5SQu Wenruo 	ASSERT(rc);
3585d4f98a2SYan Zheng 	spin_lock(&rc->reloc_root_tree.lock);
359e9a28dc5SQu Wenruo 	rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root, bytenr);
3605d4f98a2SYan Zheng 	if (rb_node) {
3615d4f98a2SYan Zheng 		node = rb_entry(rb_node, struct mapping_node, rb_node);
3625d4f98a2SYan Zheng 		root = (struct btrfs_root *)node->data;
3635d4f98a2SYan Zheng 	}
3645d4f98a2SYan Zheng 	spin_unlock(&rc->reloc_root_tree.lock);
36500246528SJosef Bacik 	return btrfs_grab_root(root);
3665d4f98a2SYan Zheng }
3675d4f98a2SYan Zheng 
3685d4f98a2SYan Zheng static struct btrfs_root *read_fs_root(struct btrfs_fs_info *fs_info,
3695d4f98a2SYan Zheng 					u64 root_objectid)
3705d4f98a2SYan Zheng {
3715d4f98a2SYan Zheng 	struct btrfs_key key;
3725d4f98a2SYan Zheng 
3735d4f98a2SYan Zheng 	key.objectid = root_objectid;
3745d4f98a2SYan Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
3755d4f98a2SYan Zheng 	key.offset = (u64)-1;
3765d4f98a2SYan Zheng 
377bc44d7c4SJosef Bacik 	return btrfs_get_fs_root(fs_info, &key, false);
3785d4f98a2SYan Zheng }
3795d4f98a2SYan Zheng 
3805d4f98a2SYan Zheng /*
38129db137bSQu Wenruo  * For useless nodes, do two major clean ups:
38229db137bSQu Wenruo  *
38329db137bSQu Wenruo  * - Cleanup the children edges and nodes
38429db137bSQu Wenruo  *   If child node is also orphan (no parent) during cleanup, then the child
38529db137bSQu Wenruo  *   node will also be cleaned up.
38629db137bSQu Wenruo  *
38729db137bSQu Wenruo  * - Freeing up leaves (level 0), keeps nodes detached
38829db137bSQu Wenruo  *   For nodes, the node is still cached as "detached"
38929db137bSQu Wenruo  *
39029db137bSQu Wenruo  * Return false if @node is not in the @useless_nodes list.
39129db137bSQu Wenruo  * Return true if @node is in the @useless_nodes list.
39229db137bSQu Wenruo  */
39329db137bSQu Wenruo static bool handle_useless_nodes(struct reloc_control *rc,
394a26195a5SQu Wenruo 				 struct btrfs_backref_node *node)
39529db137bSQu Wenruo {
396a26195a5SQu Wenruo 	struct btrfs_backref_cache *cache = &rc->backref_cache;
39729db137bSQu Wenruo 	struct list_head *useless_node = &cache->useless_node;
39829db137bSQu Wenruo 	bool ret = false;
39929db137bSQu Wenruo 
40029db137bSQu Wenruo 	while (!list_empty(useless_node)) {
401a26195a5SQu Wenruo 		struct btrfs_backref_node *cur;
40229db137bSQu Wenruo 
403a26195a5SQu Wenruo 		cur = list_first_entry(useless_node, struct btrfs_backref_node,
40429db137bSQu Wenruo 				 list);
40529db137bSQu Wenruo 		list_del_init(&cur->list);
40629db137bSQu Wenruo 
40729db137bSQu Wenruo 		/* Only tree root nodes can be added to @useless_nodes */
40829db137bSQu Wenruo 		ASSERT(list_empty(&cur->upper));
40929db137bSQu Wenruo 
41029db137bSQu Wenruo 		if (cur == node)
41129db137bSQu Wenruo 			ret = true;
41229db137bSQu Wenruo 
41329db137bSQu Wenruo 		/* The node is the lowest node */
41429db137bSQu Wenruo 		if (cur->lowest) {
41529db137bSQu Wenruo 			list_del_init(&cur->lower);
41629db137bSQu Wenruo 			cur->lowest = 0;
41729db137bSQu Wenruo 		}
41829db137bSQu Wenruo 
41929db137bSQu Wenruo 		/* Cleanup the lower edges */
42029db137bSQu Wenruo 		while (!list_empty(&cur->lower)) {
421a26195a5SQu Wenruo 			struct btrfs_backref_edge *edge;
422a26195a5SQu Wenruo 			struct btrfs_backref_node *lower;
42329db137bSQu Wenruo 
42429db137bSQu Wenruo 			edge = list_entry(cur->lower.next,
425a26195a5SQu Wenruo 					struct btrfs_backref_edge, list[UPPER]);
42629db137bSQu Wenruo 			list_del(&edge->list[UPPER]);
42729db137bSQu Wenruo 			list_del(&edge->list[LOWER]);
42829db137bSQu Wenruo 			lower = edge->node[LOWER];
429741188d3SQu Wenruo 			btrfs_backref_free_edge(cache, edge);
43029db137bSQu Wenruo 
43129db137bSQu Wenruo 			/* Child node is also orphan, queue for cleanup */
43229db137bSQu Wenruo 			if (list_empty(&lower->upper))
43329db137bSQu Wenruo 				list_add(&lower->list, useless_node);
43429db137bSQu Wenruo 		}
43529db137bSQu Wenruo 		/* Mark this block processed for relocation */
43629db137bSQu Wenruo 		mark_block_processed(rc, cur);
43729db137bSQu Wenruo 
43829db137bSQu Wenruo 		/*
43929db137bSQu Wenruo 		 * Backref nodes for tree leaves are deleted from the cache.
44029db137bSQu Wenruo 		 * Backref nodes for upper level tree blocks are left in the
44129db137bSQu Wenruo 		 * cache to avoid unnecessary backref lookup.
44229db137bSQu Wenruo 		 */
44329db137bSQu Wenruo 		if (cur->level > 0) {
44429db137bSQu Wenruo 			list_add(&cur->list, &cache->detached);
44529db137bSQu Wenruo 			cur->detached = 1;
44629db137bSQu Wenruo 		} else {
44729db137bSQu Wenruo 			rb_erase(&cur->rb_node, &cache->rb_root);
448741188d3SQu Wenruo 			btrfs_backref_free_node(cache, cur);
44929db137bSQu Wenruo 		}
45029db137bSQu Wenruo 	}
45129db137bSQu Wenruo 	return ret;
45229db137bSQu Wenruo }
45329db137bSQu Wenruo 
45429db137bSQu Wenruo /*
455e7d571c7SQu Wenruo  * Build backref tree for a given tree block. Root of the backref tree
456e7d571c7SQu Wenruo  * corresponds the tree block, leaves of the backref tree correspond roots of
457e7d571c7SQu Wenruo  * b-trees that reference the tree block.
458e7d571c7SQu Wenruo  *
459e7d571c7SQu Wenruo  * The basic idea of this function is check backrefs of a given block to find
460e7d571c7SQu Wenruo  * upper level blocks that reference the block, and then check backrefs of
461e7d571c7SQu Wenruo  * these upper level blocks recursively. The recursion stops when tree root is
462e7d571c7SQu Wenruo  * reached or backrefs for the block is cached.
463e7d571c7SQu Wenruo  *
464e7d571c7SQu Wenruo  * NOTE: if we find that backrefs for a block are cached, we know backrefs for
465e7d571c7SQu Wenruo  * all upper level blocks that directly/indirectly reference the block are also
466e7d571c7SQu Wenruo  * cached.
467e7d571c7SQu Wenruo  */
468a26195a5SQu Wenruo static noinline_for_stack struct btrfs_backref_node *build_backref_tree(
469e7d571c7SQu Wenruo 			struct reloc_control *rc, struct btrfs_key *node_key,
470e7d571c7SQu Wenruo 			int level, u64 bytenr)
471e7d571c7SQu Wenruo {
472e7d571c7SQu Wenruo 	struct btrfs_backref_iter *iter;
473a26195a5SQu Wenruo 	struct btrfs_backref_cache *cache = &rc->backref_cache;
474e7d571c7SQu Wenruo 	/* For searching parent of TREE_BLOCK_REF */
475e7d571c7SQu Wenruo 	struct btrfs_path *path;
476a26195a5SQu Wenruo 	struct btrfs_backref_node *cur;
477a26195a5SQu Wenruo 	struct btrfs_backref_node *node = NULL;
478a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
479e7d571c7SQu Wenruo 	int ret;
480e7d571c7SQu Wenruo 	int err = 0;
481e7d571c7SQu Wenruo 
482e7d571c7SQu Wenruo 	iter = btrfs_backref_iter_alloc(rc->extent_root->fs_info, GFP_NOFS);
483e7d571c7SQu Wenruo 	if (!iter)
484e7d571c7SQu Wenruo 		return ERR_PTR(-ENOMEM);
485e7d571c7SQu Wenruo 	path = btrfs_alloc_path();
486e7d571c7SQu Wenruo 	if (!path) {
487e7d571c7SQu Wenruo 		err = -ENOMEM;
488e7d571c7SQu Wenruo 		goto out;
489e7d571c7SQu Wenruo 	}
490e7d571c7SQu Wenruo 
491b1818dabSQu Wenruo 	node = btrfs_backref_alloc_node(cache, bytenr, level);
492e7d571c7SQu Wenruo 	if (!node) {
493e7d571c7SQu Wenruo 		err = -ENOMEM;
494e7d571c7SQu Wenruo 		goto out;
495e7d571c7SQu Wenruo 	}
496e7d571c7SQu Wenruo 
497e7d571c7SQu Wenruo 	node->lowest = 1;
498e7d571c7SQu Wenruo 	cur = node;
499e7d571c7SQu Wenruo 
500e7d571c7SQu Wenruo 	/* Breadth-first search to build backref cache */
501e7d571c7SQu Wenruo 	do {
5021b60d2ecSQu Wenruo 		ret = btrfs_backref_add_tree_node(cache, path, iter, node_key,
5031b60d2ecSQu Wenruo 						  cur);
504e7d571c7SQu Wenruo 		if (ret < 0) {
505e7d571c7SQu Wenruo 			err = ret;
506e7d571c7SQu Wenruo 			goto out;
507e7d571c7SQu Wenruo 		}
508e7d571c7SQu Wenruo 		edge = list_first_entry_or_null(&cache->pending_edge,
509a26195a5SQu Wenruo 				struct btrfs_backref_edge, list[UPPER]);
510e7d571c7SQu Wenruo 		/*
511e7d571c7SQu Wenruo 		 * The pending list isn't empty, take the first block to
512e7d571c7SQu Wenruo 		 * process
513e7d571c7SQu Wenruo 		 */
514e7d571c7SQu Wenruo 		if (edge) {
5155d4f98a2SYan Zheng 			list_del_init(&edge->list[UPPER]);
5165d4f98a2SYan Zheng 			cur = edge->node[UPPER];
5175d4f98a2SYan Zheng 		}
518e7d571c7SQu Wenruo 	} while (edge);
5195d4f98a2SYan Zheng 
5201f872924SQu Wenruo 	/* Finish the upper linkage of newly added edges/nodes */
521fc997ed0SQu Wenruo 	ret = btrfs_backref_finish_upper_links(cache, node);
5221f872924SQu Wenruo 	if (ret < 0) {
5231f872924SQu Wenruo 		err = ret;
52475bfb9afSJosef Bacik 		goto out;
52575bfb9afSJosef Bacik 	}
52675bfb9afSJosef Bacik 
52729db137bSQu Wenruo 	if (handle_useless_nodes(rc, node))
5283fd0a558SYan, Zheng 		node = NULL;
5295d4f98a2SYan Zheng out:
53071f572a9SQu Wenruo 	btrfs_backref_iter_free(iter);
53171f572a9SQu Wenruo 	btrfs_free_path(path);
5325d4f98a2SYan Zheng 	if (err) {
5331b23ea18SQu Wenruo 		btrfs_backref_error_cleanup(cache, node);
5345d4f98a2SYan Zheng 		return ERR_PTR(err);
5355d4f98a2SYan Zheng 	}
53675bfb9afSJosef Bacik 	ASSERT(!node || !node->detached);
53784780289SQu Wenruo 	ASSERT(list_empty(&cache->useless_node) &&
53884780289SQu Wenruo 	       list_empty(&cache->pending_edge));
5395d4f98a2SYan Zheng 	return node;
5405d4f98a2SYan Zheng }
5415d4f98a2SYan Zheng 
5425d4f98a2SYan Zheng /*
5433fd0a558SYan, Zheng  * helper to add backref node for the newly created snapshot.
5443fd0a558SYan, Zheng  * the backref node is created by cloning backref node that
5453fd0a558SYan, Zheng  * corresponds to root of source tree
5463fd0a558SYan, Zheng  */
5473fd0a558SYan, Zheng static int clone_backref_node(struct btrfs_trans_handle *trans,
5483fd0a558SYan, Zheng 			      struct reloc_control *rc,
5493fd0a558SYan, Zheng 			      struct btrfs_root *src,
5503fd0a558SYan, Zheng 			      struct btrfs_root *dest)
5513fd0a558SYan, Zheng {
5523fd0a558SYan, Zheng 	struct btrfs_root *reloc_root = src->reloc_root;
553a26195a5SQu Wenruo 	struct btrfs_backref_cache *cache = &rc->backref_cache;
554a26195a5SQu Wenruo 	struct btrfs_backref_node *node = NULL;
555a26195a5SQu Wenruo 	struct btrfs_backref_node *new_node;
556a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
557a26195a5SQu Wenruo 	struct btrfs_backref_edge *new_edge;
5583fd0a558SYan, Zheng 	struct rb_node *rb_node;
5593fd0a558SYan, Zheng 
5603fd0a558SYan, Zheng 	if (cache->last_trans > 0)
5613fd0a558SYan, Zheng 		update_backref_cache(trans, cache);
5623fd0a558SYan, Zheng 
563e9a28dc5SQu Wenruo 	rb_node = rb_simple_search(&cache->rb_root, src->commit_root->start);
5643fd0a558SYan, Zheng 	if (rb_node) {
565a26195a5SQu Wenruo 		node = rb_entry(rb_node, struct btrfs_backref_node, rb_node);
5663fd0a558SYan, Zheng 		if (node->detached)
5673fd0a558SYan, Zheng 			node = NULL;
5683fd0a558SYan, Zheng 		else
5693fd0a558SYan, Zheng 			BUG_ON(node->new_bytenr != reloc_root->node->start);
5703fd0a558SYan, Zheng 	}
5713fd0a558SYan, Zheng 
5723fd0a558SYan, Zheng 	if (!node) {
573e9a28dc5SQu Wenruo 		rb_node = rb_simple_search(&cache->rb_root,
5743fd0a558SYan, Zheng 					   reloc_root->commit_root->start);
5753fd0a558SYan, Zheng 		if (rb_node) {
576a26195a5SQu Wenruo 			node = rb_entry(rb_node, struct btrfs_backref_node,
5773fd0a558SYan, Zheng 					rb_node);
5783fd0a558SYan, Zheng 			BUG_ON(node->detached);
5793fd0a558SYan, Zheng 		}
5803fd0a558SYan, Zheng 	}
5813fd0a558SYan, Zheng 
5823fd0a558SYan, Zheng 	if (!node)
5833fd0a558SYan, Zheng 		return 0;
5843fd0a558SYan, Zheng 
585b1818dabSQu Wenruo 	new_node = btrfs_backref_alloc_node(cache, dest->node->start,
586b1818dabSQu Wenruo 					    node->level);
5873fd0a558SYan, Zheng 	if (!new_node)
5883fd0a558SYan, Zheng 		return -ENOMEM;
5893fd0a558SYan, Zheng 
5903fd0a558SYan, Zheng 	new_node->lowest = node->lowest;
5916848ad64SYan, Zheng 	new_node->checked = 1;
59200246528SJosef Bacik 	new_node->root = btrfs_grab_root(dest);
5930b530bc5SJosef Bacik 	ASSERT(new_node->root);
5943fd0a558SYan, Zheng 
5953fd0a558SYan, Zheng 	if (!node->lowest) {
5963fd0a558SYan, Zheng 		list_for_each_entry(edge, &node->lower, list[UPPER]) {
59747254d07SQu Wenruo 			new_edge = btrfs_backref_alloc_edge(cache);
5983fd0a558SYan, Zheng 			if (!new_edge)
5993fd0a558SYan, Zheng 				goto fail;
6003fd0a558SYan, Zheng 
601f39911e5SQu Wenruo 			btrfs_backref_link_edge(new_edge, edge->node[LOWER],
602f39911e5SQu Wenruo 						new_node, LINK_UPPER);
6033fd0a558SYan, Zheng 		}
60476b9e23dSMiao Xie 	} else {
60576b9e23dSMiao Xie 		list_add_tail(&new_node->lower, &cache->leaves);
6063fd0a558SYan, Zheng 	}
6073fd0a558SYan, Zheng 
608e9a28dc5SQu Wenruo 	rb_node = rb_simple_insert(&cache->rb_root, new_node->bytenr,
6093fd0a558SYan, Zheng 				   &new_node->rb_node);
61043c04fb1SJeff Mahoney 	if (rb_node)
611982c92cbSQu Wenruo 		btrfs_backref_panic(trans->fs_info, new_node->bytenr, -EEXIST);
6123fd0a558SYan, Zheng 
6133fd0a558SYan, Zheng 	if (!new_node->lowest) {
6143fd0a558SYan, Zheng 		list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) {
6153fd0a558SYan, Zheng 			list_add_tail(&new_edge->list[LOWER],
6163fd0a558SYan, Zheng 				      &new_edge->node[LOWER]->upper);
6173fd0a558SYan, Zheng 		}
6183fd0a558SYan, Zheng 	}
6193fd0a558SYan, Zheng 	return 0;
6203fd0a558SYan, Zheng fail:
6213fd0a558SYan, Zheng 	while (!list_empty(&new_node->lower)) {
6223fd0a558SYan, Zheng 		new_edge = list_entry(new_node->lower.next,
623a26195a5SQu Wenruo 				      struct btrfs_backref_edge, list[UPPER]);
6243fd0a558SYan, Zheng 		list_del(&new_edge->list[UPPER]);
625741188d3SQu Wenruo 		btrfs_backref_free_edge(cache, new_edge);
6263fd0a558SYan, Zheng 	}
627741188d3SQu Wenruo 	btrfs_backref_free_node(cache, new_node);
6283fd0a558SYan, Zheng 	return -ENOMEM;
6293fd0a558SYan, Zheng }
6303fd0a558SYan, Zheng 
6313fd0a558SYan, Zheng /*
6325d4f98a2SYan Zheng  * helper to add 'address of tree root -> reloc tree' mapping
6335d4f98a2SYan Zheng  */
634ffd7b339SJeff Mahoney static int __must_check __add_reloc_root(struct btrfs_root *root)
6355d4f98a2SYan Zheng {
6360b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
6375d4f98a2SYan Zheng 	struct rb_node *rb_node;
6385d4f98a2SYan Zheng 	struct mapping_node *node;
6390b246afaSJeff Mahoney 	struct reloc_control *rc = fs_info->reloc_ctl;
6405d4f98a2SYan Zheng 
6415d4f98a2SYan Zheng 	node = kmalloc(sizeof(*node), GFP_NOFS);
642ffd7b339SJeff Mahoney 	if (!node)
643ffd7b339SJeff Mahoney 		return -ENOMEM;
6445d4f98a2SYan Zheng 
645ea287ab1SJosef Bacik 	node->bytenr = root->commit_root->start;
6465d4f98a2SYan Zheng 	node->data = root;
6475d4f98a2SYan Zheng 
6485d4f98a2SYan Zheng 	spin_lock(&rc->reloc_root_tree.lock);
649e9a28dc5SQu Wenruo 	rb_node = rb_simple_insert(&rc->reloc_root_tree.rb_root,
6505d4f98a2SYan Zheng 				   node->bytenr, &node->rb_node);
6515d4f98a2SYan Zheng 	spin_unlock(&rc->reloc_root_tree.lock);
652ffd7b339SJeff Mahoney 	if (rb_node) {
6530b246afaSJeff Mahoney 		btrfs_panic(fs_info, -EEXIST,
6545d163e0eSJeff Mahoney 			    "Duplicate root found for start=%llu while inserting into relocation tree",
6555d163e0eSJeff Mahoney 			    node->bytenr);
656ffd7b339SJeff Mahoney 	}
6575d4f98a2SYan Zheng 
6585d4f98a2SYan Zheng 	list_add_tail(&root->root_list, &rc->reloc_roots);
6595d4f98a2SYan Zheng 	return 0;
6605d4f98a2SYan Zheng }
6615d4f98a2SYan Zheng 
6625d4f98a2SYan Zheng /*
663c974c464SWang Shilong  * helper to delete the 'address of tree root -> reloc tree'
6645d4f98a2SYan Zheng  * mapping
6655d4f98a2SYan Zheng  */
666c974c464SWang Shilong static void __del_reloc_root(struct btrfs_root *root)
6675d4f98a2SYan Zheng {
6680b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
6695d4f98a2SYan Zheng 	struct rb_node *rb_node;
6705d4f98a2SYan Zheng 	struct mapping_node *node = NULL;
6710b246afaSJeff Mahoney 	struct reloc_control *rc = fs_info->reloc_ctl;
672f44deb74SJosef Bacik 	bool put_ref = false;
6735d4f98a2SYan Zheng 
67465c6e82bSQu Wenruo 	if (rc && root->node) {
6755d4f98a2SYan Zheng 		spin_lock(&rc->reloc_root_tree.lock);
676e9a28dc5SQu Wenruo 		rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root,
677ea287ab1SJosef Bacik 					   root->commit_root->start);
678c974c464SWang Shilong 		if (rb_node) {
679c974c464SWang Shilong 			node = rb_entry(rb_node, struct mapping_node, rb_node);
680c974c464SWang Shilong 			rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
681ea287ab1SJosef Bacik 			RB_CLEAR_NODE(&node->rb_node);
682c974c464SWang Shilong 		}
683c974c464SWang Shilong 		spin_unlock(&rc->reloc_root_tree.lock);
684c974c464SWang Shilong 		if (!node)
685c974c464SWang Shilong 			return;
686c974c464SWang Shilong 		BUG_ON((struct btrfs_root *)node->data != root);
687389305b2SQu Wenruo 	}
688c974c464SWang Shilong 
689f44deb74SJosef Bacik 	/*
690f44deb74SJosef Bacik 	 * We only put the reloc root here if it's on the list.  There's a lot
691f44deb74SJosef Bacik 	 * of places where the pattern is to splice the rc->reloc_roots, process
692f44deb74SJosef Bacik 	 * the reloc roots, and then add the reloc root back onto
693f44deb74SJosef Bacik 	 * rc->reloc_roots.  If we call __del_reloc_root while it's off of the
694f44deb74SJosef Bacik 	 * list we don't want the reference being dropped, because the guy
695f44deb74SJosef Bacik 	 * messing with the list is in charge of the reference.
696f44deb74SJosef Bacik 	 */
6970b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
698f44deb74SJosef Bacik 	if (!list_empty(&root->root_list)) {
699f44deb74SJosef Bacik 		put_ref = true;
700c974c464SWang Shilong 		list_del_init(&root->root_list);
701f44deb74SJosef Bacik 	}
7020b246afaSJeff Mahoney 	spin_unlock(&fs_info->trans_lock);
703f44deb74SJosef Bacik 	if (put_ref)
704f44deb74SJosef Bacik 		btrfs_put_root(root);
705c974c464SWang Shilong 	kfree(node);
706c974c464SWang Shilong }
707c974c464SWang Shilong 
708c974c464SWang Shilong /*
709c974c464SWang Shilong  * helper to update the 'address of tree root -> reloc tree'
710c974c464SWang Shilong  * mapping
711c974c464SWang Shilong  */
712ea287ab1SJosef Bacik static int __update_reloc_root(struct btrfs_root *root)
713c974c464SWang Shilong {
7140b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
715c974c464SWang Shilong 	struct rb_node *rb_node;
716c974c464SWang Shilong 	struct mapping_node *node = NULL;
7170b246afaSJeff Mahoney 	struct reloc_control *rc = fs_info->reloc_ctl;
718c974c464SWang Shilong 
719c974c464SWang Shilong 	spin_lock(&rc->reloc_root_tree.lock);
720e9a28dc5SQu Wenruo 	rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root,
721ea287ab1SJosef Bacik 				   root->commit_root->start);
7225d4f98a2SYan Zheng 	if (rb_node) {
7235d4f98a2SYan Zheng 		node = rb_entry(rb_node, struct mapping_node, rb_node);
7245d4f98a2SYan Zheng 		rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
7255d4f98a2SYan Zheng 	}
7265d4f98a2SYan Zheng 	spin_unlock(&rc->reloc_root_tree.lock);
7275d4f98a2SYan Zheng 
7288f71f3e0SLiu Bo 	if (!node)
7298f71f3e0SLiu Bo 		return 0;
7305d4f98a2SYan Zheng 	BUG_ON((struct btrfs_root *)node->data != root);
7315d4f98a2SYan Zheng 
7325d4f98a2SYan Zheng 	spin_lock(&rc->reloc_root_tree.lock);
733ea287ab1SJosef Bacik 	node->bytenr = root->node->start;
734e9a28dc5SQu Wenruo 	rb_node = rb_simple_insert(&rc->reloc_root_tree.rb_root,
7355d4f98a2SYan Zheng 				   node->bytenr, &node->rb_node);
7365d4f98a2SYan Zheng 	spin_unlock(&rc->reloc_root_tree.lock);
73743c04fb1SJeff Mahoney 	if (rb_node)
738982c92cbSQu Wenruo 		btrfs_backref_panic(fs_info, node->bytenr, -EEXIST);
7395d4f98a2SYan Zheng 	return 0;
7405d4f98a2SYan Zheng }
7415d4f98a2SYan Zheng 
7423fd0a558SYan, Zheng static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
7433fd0a558SYan, Zheng 					struct btrfs_root *root, u64 objectid)
7445d4f98a2SYan Zheng {
7450b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
7465d4f98a2SYan Zheng 	struct btrfs_root *reloc_root;
7475d4f98a2SYan Zheng 	struct extent_buffer *eb;
7485d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
7495d4f98a2SYan Zheng 	struct btrfs_key root_key;
7505d4f98a2SYan Zheng 	int ret;
7515d4f98a2SYan Zheng 
7525d4f98a2SYan Zheng 	root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
7535d4f98a2SYan Zheng 	BUG_ON(!root_item);
7545d4f98a2SYan Zheng 
7555d4f98a2SYan Zheng 	root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
7565d4f98a2SYan Zheng 	root_key.type = BTRFS_ROOT_ITEM_KEY;
7573fd0a558SYan, Zheng 	root_key.offset = objectid;
7585d4f98a2SYan Zheng 
7593fd0a558SYan, Zheng 	if (root->root_key.objectid == objectid) {
760054570a1SFilipe Manana 		u64 commit_root_gen;
761054570a1SFilipe Manana 
7623fd0a558SYan, Zheng 		/* called by btrfs_init_reloc_root */
7635d4f98a2SYan Zheng 		ret = btrfs_copy_root(trans, root, root->commit_root, &eb,
7645d4f98a2SYan Zheng 				      BTRFS_TREE_RELOC_OBJECTID);
7655d4f98a2SYan Zheng 		BUG_ON(ret);
766054570a1SFilipe Manana 		/*
767054570a1SFilipe Manana 		 * Set the last_snapshot field to the generation of the commit
768054570a1SFilipe Manana 		 * root - like this ctree.c:btrfs_block_can_be_shared() behaves
769054570a1SFilipe Manana 		 * correctly (returns true) when the relocation root is created
770054570a1SFilipe Manana 		 * either inside the critical section of a transaction commit
771054570a1SFilipe Manana 		 * (through transaction.c:qgroup_account_snapshot()) and when
772054570a1SFilipe Manana 		 * it's created before the transaction commit is started.
773054570a1SFilipe Manana 		 */
774054570a1SFilipe Manana 		commit_root_gen = btrfs_header_generation(root->commit_root);
775054570a1SFilipe Manana 		btrfs_set_root_last_snapshot(&root->root_item, commit_root_gen);
7763fd0a558SYan, Zheng 	} else {
7773fd0a558SYan, Zheng 		/*
7783fd0a558SYan, Zheng 		 * called by btrfs_reloc_post_snapshot_hook.
7793fd0a558SYan, Zheng 		 * the source tree is a reloc tree, all tree blocks
7803fd0a558SYan, Zheng 		 * modified after it was created have RELOC flag
7813fd0a558SYan, Zheng 		 * set in their headers. so it's OK to not update
7823fd0a558SYan, Zheng 		 * the 'last_snapshot'.
7833fd0a558SYan, Zheng 		 */
7843fd0a558SYan, Zheng 		ret = btrfs_copy_root(trans, root, root->node, &eb,
7853fd0a558SYan, Zheng 				      BTRFS_TREE_RELOC_OBJECTID);
7863fd0a558SYan, Zheng 		BUG_ON(ret);
7873fd0a558SYan, Zheng 	}
7883fd0a558SYan, Zheng 
7895d4f98a2SYan Zheng 	memcpy(root_item, &root->root_item, sizeof(*root_item));
7905d4f98a2SYan Zheng 	btrfs_set_root_bytenr(root_item, eb->start);
7915d4f98a2SYan Zheng 	btrfs_set_root_level(root_item, btrfs_header_level(eb));
7925d4f98a2SYan Zheng 	btrfs_set_root_generation(root_item, trans->transid);
7933fd0a558SYan, Zheng 
7943fd0a558SYan, Zheng 	if (root->root_key.objectid == objectid) {
7953fd0a558SYan, Zheng 		btrfs_set_root_refs(root_item, 0);
7963fd0a558SYan, Zheng 		memset(&root_item->drop_progress, 0,
7973fd0a558SYan, Zheng 		       sizeof(struct btrfs_disk_key));
7985d4f98a2SYan Zheng 		root_item->drop_level = 0;
7993fd0a558SYan, Zheng 	}
8005d4f98a2SYan Zheng 
8015d4f98a2SYan Zheng 	btrfs_tree_unlock(eb);
8025d4f98a2SYan Zheng 	free_extent_buffer(eb);
8035d4f98a2SYan Zheng 
8040b246afaSJeff Mahoney 	ret = btrfs_insert_root(trans, fs_info->tree_root,
8055d4f98a2SYan Zheng 				&root_key, root_item);
8065d4f98a2SYan Zheng 	BUG_ON(ret);
8075d4f98a2SYan Zheng 	kfree(root_item);
8085d4f98a2SYan Zheng 
8093dbf1738SJosef Bacik 	reloc_root = btrfs_read_tree_root(fs_info->tree_root, &root_key);
8105d4f98a2SYan Zheng 	BUG_ON(IS_ERR(reloc_root));
81192a7cc42SQu Wenruo 	set_bit(BTRFS_ROOT_SHAREABLE, &reloc_root->state);
8125d4f98a2SYan Zheng 	reloc_root->last_trans = trans->transid;
8133fd0a558SYan, Zheng 	return reloc_root;
8143fd0a558SYan, Zheng }
8153fd0a558SYan, Zheng 
8163fd0a558SYan, Zheng /*
8173fd0a558SYan, Zheng  * create reloc tree for a given fs tree. reloc tree is just a
8183fd0a558SYan, Zheng  * snapshot of the fs tree with special root objectid.
819f44deb74SJosef Bacik  *
820f44deb74SJosef Bacik  * The reloc_root comes out of here with two references, one for
821f44deb74SJosef Bacik  * root->reloc_root, and another for being on the rc->reloc_roots list.
8223fd0a558SYan, Zheng  */
8233fd0a558SYan, Zheng int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
8243fd0a558SYan, Zheng 			  struct btrfs_root *root)
8253fd0a558SYan, Zheng {
8260b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
8273fd0a558SYan, Zheng 	struct btrfs_root *reloc_root;
8280b246afaSJeff Mahoney 	struct reloc_control *rc = fs_info->reloc_ctl;
82920dd2cbfSMiao Xie 	struct btrfs_block_rsv *rsv;
8303fd0a558SYan, Zheng 	int clear_rsv = 0;
831ffd7b339SJeff Mahoney 	int ret;
8323fd0a558SYan, Zheng 
833aec7db3bSJosef Bacik 	if (!rc)
8342abc726aSJosef Bacik 		return 0;
8352abc726aSJosef Bacik 
8361fac4a54SQu Wenruo 	/*
8371fac4a54SQu Wenruo 	 * The subvolume has reloc tree but the swap is finished, no need to
8381fac4a54SQu Wenruo 	 * create/update the dead reloc tree
8391fac4a54SQu Wenruo 	 */
8406282675eSQu Wenruo 	if (reloc_root_is_dead(root))
8411fac4a54SQu Wenruo 		return 0;
8421fac4a54SQu Wenruo 
843aec7db3bSJosef Bacik 	/*
844aec7db3bSJosef Bacik 	 * This is subtle but important.  We do not do
845aec7db3bSJosef Bacik 	 * record_root_in_transaction for reloc roots, instead we record their
846aec7db3bSJosef Bacik 	 * corresponding fs root, and then here we update the last trans for the
847aec7db3bSJosef Bacik 	 * reloc root.  This means that we have to do this for the entire life
848aec7db3bSJosef Bacik 	 * of the reloc root, regardless of which stage of the relocation we are
849aec7db3bSJosef Bacik 	 * in.
850aec7db3bSJosef Bacik 	 */
8513fd0a558SYan, Zheng 	if (root->reloc_root) {
8523fd0a558SYan, Zheng 		reloc_root = root->reloc_root;
8533fd0a558SYan, Zheng 		reloc_root->last_trans = trans->transid;
8543fd0a558SYan, Zheng 		return 0;
8553fd0a558SYan, Zheng 	}
8563fd0a558SYan, Zheng 
857aec7db3bSJosef Bacik 	/*
858aec7db3bSJosef Bacik 	 * We are merging reloc roots, we do not need new reloc trees.  Also
859aec7db3bSJosef Bacik 	 * reloc trees never need their own reloc tree.
860aec7db3bSJosef Bacik 	 */
861aec7db3bSJosef Bacik 	if (!rc->create_reloc_tree ||
862aec7db3bSJosef Bacik 	    root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
863aec7db3bSJosef Bacik 		return 0;
864aec7db3bSJosef Bacik 
86520dd2cbfSMiao Xie 	if (!trans->reloc_reserved) {
86620dd2cbfSMiao Xie 		rsv = trans->block_rsv;
8673fd0a558SYan, Zheng 		trans->block_rsv = rc->block_rsv;
8683fd0a558SYan, Zheng 		clear_rsv = 1;
8693fd0a558SYan, Zheng 	}
8703fd0a558SYan, Zheng 	reloc_root = create_reloc_root(trans, root, root->root_key.objectid);
8713fd0a558SYan, Zheng 	if (clear_rsv)
87220dd2cbfSMiao Xie 		trans->block_rsv = rsv;
8735d4f98a2SYan Zheng 
874ffd7b339SJeff Mahoney 	ret = __add_reloc_root(reloc_root);
875ffd7b339SJeff Mahoney 	BUG_ON(ret < 0);
876f44deb74SJosef Bacik 	root->reloc_root = btrfs_grab_root(reloc_root);
8775d4f98a2SYan Zheng 	return 0;
8785d4f98a2SYan Zheng }
8795d4f98a2SYan Zheng 
8805d4f98a2SYan Zheng /*
8815d4f98a2SYan Zheng  * update root item of reloc tree
8825d4f98a2SYan Zheng  */
8835d4f98a2SYan Zheng int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
8845d4f98a2SYan Zheng 			    struct btrfs_root *root)
8855d4f98a2SYan Zheng {
8860b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
8875d4f98a2SYan Zheng 	struct btrfs_root *reloc_root;
8885d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
8895d4f98a2SYan Zheng 	int ret;
8905d4f98a2SYan Zheng 
8916282675eSQu Wenruo 	if (!have_reloc_root(root))
8927585717fSChris Mason 		goto out;
8935d4f98a2SYan Zheng 
8945d4f98a2SYan Zheng 	reloc_root = root->reloc_root;
8955d4f98a2SYan Zheng 	root_item = &reloc_root->root_item;
8965d4f98a2SYan Zheng 
897f44deb74SJosef Bacik 	/*
898f44deb74SJosef Bacik 	 * We are probably ok here, but __del_reloc_root() will drop its ref of
899f44deb74SJosef Bacik 	 * the root.  We have the ref for root->reloc_root, but just in case
900f44deb74SJosef Bacik 	 * hold it while we update the reloc root.
901f44deb74SJosef Bacik 	 */
902f44deb74SJosef Bacik 	btrfs_grab_root(reloc_root);
903f44deb74SJosef Bacik 
904d2311e69SQu Wenruo 	/* root->reloc_root will stay until current relocation finished */
9050b246afaSJeff Mahoney 	if (fs_info->reloc_ctl->merge_reloc_tree &&
9063fd0a558SYan, Zheng 	    btrfs_root_refs(root_item) == 0) {
907d2311e69SQu Wenruo 		set_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
9086282675eSQu Wenruo 		/*
9096282675eSQu Wenruo 		 * Mark the tree as dead before we change reloc_root so
9106282675eSQu Wenruo 		 * have_reloc_root will not touch it from now on.
9116282675eSQu Wenruo 		 */
9126282675eSQu Wenruo 		smp_wmb();
913c974c464SWang Shilong 		__del_reloc_root(reloc_root);
9145d4f98a2SYan Zheng 	}
9155d4f98a2SYan Zheng 
9165d4f98a2SYan Zheng 	if (reloc_root->commit_root != reloc_root->node) {
917ea287ab1SJosef Bacik 		__update_reloc_root(reloc_root);
9185d4f98a2SYan Zheng 		btrfs_set_root_node(root_item, reloc_root->node);
9195d4f98a2SYan Zheng 		free_extent_buffer(reloc_root->commit_root);
9205d4f98a2SYan Zheng 		reloc_root->commit_root = btrfs_root_node(reloc_root);
9215d4f98a2SYan Zheng 	}
9225d4f98a2SYan Zheng 
9230b246afaSJeff Mahoney 	ret = btrfs_update_root(trans, fs_info->tree_root,
9245d4f98a2SYan Zheng 				&reloc_root->root_key, root_item);
9255d4f98a2SYan Zheng 	BUG_ON(ret);
926f44deb74SJosef Bacik 	btrfs_put_root(reloc_root);
9277585717fSChris Mason out:
9285d4f98a2SYan Zheng 	return 0;
9295d4f98a2SYan Zheng }
9305d4f98a2SYan Zheng 
9315d4f98a2SYan Zheng /*
9325d4f98a2SYan Zheng  * helper to find first cached inode with inode number >= objectid
9335d4f98a2SYan Zheng  * in a subvolume
9345d4f98a2SYan Zheng  */
9355d4f98a2SYan Zheng static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid)
9365d4f98a2SYan Zheng {
9375d4f98a2SYan Zheng 	struct rb_node *node;
9385d4f98a2SYan Zheng 	struct rb_node *prev;
9395d4f98a2SYan Zheng 	struct btrfs_inode *entry;
9405d4f98a2SYan Zheng 	struct inode *inode;
9415d4f98a2SYan Zheng 
9425d4f98a2SYan Zheng 	spin_lock(&root->inode_lock);
9435d4f98a2SYan Zheng again:
9445d4f98a2SYan Zheng 	node = root->inode_tree.rb_node;
9455d4f98a2SYan Zheng 	prev = NULL;
9465d4f98a2SYan Zheng 	while (node) {
9475d4f98a2SYan Zheng 		prev = node;
9485d4f98a2SYan Zheng 		entry = rb_entry(node, struct btrfs_inode, rb_node);
9495d4f98a2SYan Zheng 
9504a0cc7caSNikolay Borisov 		if (objectid < btrfs_ino(entry))
9515d4f98a2SYan Zheng 			node = node->rb_left;
9524a0cc7caSNikolay Borisov 		else if (objectid > btrfs_ino(entry))
9535d4f98a2SYan Zheng 			node = node->rb_right;
9545d4f98a2SYan Zheng 		else
9555d4f98a2SYan Zheng 			break;
9565d4f98a2SYan Zheng 	}
9575d4f98a2SYan Zheng 	if (!node) {
9585d4f98a2SYan Zheng 		while (prev) {
9595d4f98a2SYan Zheng 			entry = rb_entry(prev, struct btrfs_inode, rb_node);
9604a0cc7caSNikolay Borisov 			if (objectid <= btrfs_ino(entry)) {
9615d4f98a2SYan Zheng 				node = prev;
9625d4f98a2SYan Zheng 				break;
9635d4f98a2SYan Zheng 			}
9645d4f98a2SYan Zheng 			prev = rb_next(prev);
9655d4f98a2SYan Zheng 		}
9665d4f98a2SYan Zheng 	}
9675d4f98a2SYan Zheng 	while (node) {
9685d4f98a2SYan Zheng 		entry = rb_entry(node, struct btrfs_inode, rb_node);
9695d4f98a2SYan Zheng 		inode = igrab(&entry->vfs_inode);
9705d4f98a2SYan Zheng 		if (inode) {
9715d4f98a2SYan Zheng 			spin_unlock(&root->inode_lock);
9725d4f98a2SYan Zheng 			return inode;
9735d4f98a2SYan Zheng 		}
9745d4f98a2SYan Zheng 
9754a0cc7caSNikolay Borisov 		objectid = btrfs_ino(entry) + 1;
9765d4f98a2SYan Zheng 		if (cond_resched_lock(&root->inode_lock))
9775d4f98a2SYan Zheng 			goto again;
9785d4f98a2SYan Zheng 
9795d4f98a2SYan Zheng 		node = rb_next(node);
9805d4f98a2SYan Zheng 	}
9815d4f98a2SYan Zheng 	spin_unlock(&root->inode_lock);
9825d4f98a2SYan Zheng 	return NULL;
9835d4f98a2SYan Zheng }
9845d4f98a2SYan Zheng 
9855d4f98a2SYan Zheng /*
9865d4f98a2SYan Zheng  * get new location of data
9875d4f98a2SYan Zheng  */
9885d4f98a2SYan Zheng static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr,
9895d4f98a2SYan Zheng 			    u64 bytenr, u64 num_bytes)
9905d4f98a2SYan Zheng {
9915d4f98a2SYan Zheng 	struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
9925d4f98a2SYan Zheng 	struct btrfs_path *path;
9935d4f98a2SYan Zheng 	struct btrfs_file_extent_item *fi;
9945d4f98a2SYan Zheng 	struct extent_buffer *leaf;
9955d4f98a2SYan Zheng 	int ret;
9965d4f98a2SYan Zheng 
9975d4f98a2SYan Zheng 	path = btrfs_alloc_path();
9985d4f98a2SYan Zheng 	if (!path)
9995d4f98a2SYan Zheng 		return -ENOMEM;
10005d4f98a2SYan Zheng 
10015d4f98a2SYan Zheng 	bytenr -= BTRFS_I(reloc_inode)->index_cnt;
1002f85b7379SDavid Sterba 	ret = btrfs_lookup_file_extent(NULL, root, path,
1003f85b7379SDavid Sterba 			btrfs_ino(BTRFS_I(reloc_inode)), bytenr, 0);
10045d4f98a2SYan Zheng 	if (ret < 0)
10055d4f98a2SYan Zheng 		goto out;
10065d4f98a2SYan Zheng 	if (ret > 0) {
10075d4f98a2SYan Zheng 		ret = -ENOENT;
10085d4f98a2SYan Zheng 		goto out;
10095d4f98a2SYan Zheng 	}
10105d4f98a2SYan Zheng 
10115d4f98a2SYan Zheng 	leaf = path->nodes[0];
10125d4f98a2SYan Zheng 	fi = btrfs_item_ptr(leaf, path->slots[0],
10135d4f98a2SYan Zheng 			    struct btrfs_file_extent_item);
10145d4f98a2SYan Zheng 
10155d4f98a2SYan Zheng 	BUG_ON(btrfs_file_extent_offset(leaf, fi) ||
10165d4f98a2SYan Zheng 	       btrfs_file_extent_compression(leaf, fi) ||
10175d4f98a2SYan Zheng 	       btrfs_file_extent_encryption(leaf, fi) ||
10185d4f98a2SYan Zheng 	       btrfs_file_extent_other_encoding(leaf, fi));
10195d4f98a2SYan Zheng 
10205d4f98a2SYan Zheng 	if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
102183d4cfd4SJosef Bacik 		ret = -EINVAL;
10225d4f98a2SYan Zheng 		goto out;
10235d4f98a2SYan Zheng 	}
10245d4f98a2SYan Zheng 
10255d4f98a2SYan Zheng 	*new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
10265d4f98a2SYan Zheng 	ret = 0;
10275d4f98a2SYan Zheng out:
10285d4f98a2SYan Zheng 	btrfs_free_path(path);
10295d4f98a2SYan Zheng 	return ret;
10305d4f98a2SYan Zheng }
10315d4f98a2SYan Zheng 
10325d4f98a2SYan Zheng /*
10335d4f98a2SYan Zheng  * update file extent items in the tree leaf to point to
10345d4f98a2SYan Zheng  * the new locations.
10355d4f98a2SYan Zheng  */
10363fd0a558SYan, Zheng static noinline_for_stack
10373fd0a558SYan, Zheng int replace_file_extents(struct btrfs_trans_handle *trans,
10385d4f98a2SYan Zheng 			 struct reloc_control *rc,
10395d4f98a2SYan Zheng 			 struct btrfs_root *root,
10403fd0a558SYan, Zheng 			 struct extent_buffer *leaf)
10415d4f98a2SYan Zheng {
10420b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
10435d4f98a2SYan Zheng 	struct btrfs_key key;
10445d4f98a2SYan Zheng 	struct btrfs_file_extent_item *fi;
10455d4f98a2SYan Zheng 	struct inode *inode = NULL;
10465d4f98a2SYan Zheng 	u64 parent;
10475d4f98a2SYan Zheng 	u64 bytenr;
10483fd0a558SYan, Zheng 	u64 new_bytenr = 0;
10495d4f98a2SYan Zheng 	u64 num_bytes;
10505d4f98a2SYan Zheng 	u64 end;
10515d4f98a2SYan Zheng 	u32 nritems;
10525d4f98a2SYan Zheng 	u32 i;
105383d4cfd4SJosef Bacik 	int ret = 0;
10545d4f98a2SYan Zheng 	int first = 1;
10555d4f98a2SYan Zheng 	int dirty = 0;
10565d4f98a2SYan Zheng 
10575d4f98a2SYan Zheng 	if (rc->stage != UPDATE_DATA_PTRS)
10585d4f98a2SYan Zheng 		return 0;
10595d4f98a2SYan Zheng 
10605d4f98a2SYan Zheng 	/* reloc trees always use full backref */
10615d4f98a2SYan Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
10625d4f98a2SYan Zheng 		parent = leaf->start;
10635d4f98a2SYan Zheng 	else
10645d4f98a2SYan Zheng 		parent = 0;
10655d4f98a2SYan Zheng 
10665d4f98a2SYan Zheng 	nritems = btrfs_header_nritems(leaf);
10675d4f98a2SYan Zheng 	for (i = 0; i < nritems; i++) {
106882fa113fSQu Wenruo 		struct btrfs_ref ref = { 0 };
106982fa113fSQu Wenruo 
10705d4f98a2SYan Zheng 		cond_resched();
10715d4f98a2SYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, i);
10725d4f98a2SYan Zheng 		if (key.type != BTRFS_EXTENT_DATA_KEY)
10735d4f98a2SYan Zheng 			continue;
10745d4f98a2SYan Zheng 		fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
10755d4f98a2SYan Zheng 		if (btrfs_file_extent_type(leaf, fi) ==
10765d4f98a2SYan Zheng 		    BTRFS_FILE_EXTENT_INLINE)
10775d4f98a2SYan Zheng 			continue;
10785d4f98a2SYan Zheng 		bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
10795d4f98a2SYan Zheng 		num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
10805d4f98a2SYan Zheng 		if (bytenr == 0)
10815d4f98a2SYan Zheng 			continue;
10829569cc20SQu Wenruo 		if (!in_range(bytenr, rc->block_group->start,
10839569cc20SQu Wenruo 			      rc->block_group->length))
10845d4f98a2SYan Zheng 			continue;
10855d4f98a2SYan Zheng 
10865d4f98a2SYan Zheng 		/*
10875d4f98a2SYan Zheng 		 * if we are modifying block in fs tree, wait for readpage
10885d4f98a2SYan Zheng 		 * to complete and drop the extent cache
10895d4f98a2SYan Zheng 		 */
10905d4f98a2SYan Zheng 		if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
10915d4f98a2SYan Zheng 			if (first) {
10925d4f98a2SYan Zheng 				inode = find_next_inode(root, key.objectid);
10935d4f98a2SYan Zheng 				first = 0;
10944a0cc7caSNikolay Borisov 			} else if (inode && btrfs_ino(BTRFS_I(inode)) < key.objectid) {
10953fd0a558SYan, Zheng 				btrfs_add_delayed_iput(inode);
10965d4f98a2SYan Zheng 				inode = find_next_inode(root, key.objectid);
10975d4f98a2SYan Zheng 			}
10984a0cc7caSNikolay Borisov 			if (inode && btrfs_ino(BTRFS_I(inode)) == key.objectid) {
10995d4f98a2SYan Zheng 				end = key.offset +
11005d4f98a2SYan Zheng 				      btrfs_file_extent_num_bytes(leaf, fi);
11015d4f98a2SYan Zheng 				WARN_ON(!IS_ALIGNED(key.offset,
11020b246afaSJeff Mahoney 						    fs_info->sectorsize));
11030b246afaSJeff Mahoney 				WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize));
11045d4f98a2SYan Zheng 				end--;
11055d4f98a2SYan Zheng 				ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
1106d0082371SJeff Mahoney 						      key.offset, end);
11075d4f98a2SYan Zheng 				if (!ret)
11085d4f98a2SYan Zheng 					continue;
11095d4f98a2SYan Zheng 
1110dcdbc059SNikolay Borisov 				btrfs_drop_extent_cache(BTRFS_I(inode),
1111dcdbc059SNikolay Borisov 						key.offset,	end, 1);
11125d4f98a2SYan Zheng 				unlock_extent(&BTRFS_I(inode)->io_tree,
1113d0082371SJeff Mahoney 					      key.offset, end);
11145d4f98a2SYan Zheng 			}
11155d4f98a2SYan Zheng 		}
11165d4f98a2SYan Zheng 
11175d4f98a2SYan Zheng 		ret = get_new_location(rc->data_inode, &new_bytenr,
11185d4f98a2SYan Zheng 				       bytenr, num_bytes);
111983d4cfd4SJosef Bacik 		if (ret) {
112083d4cfd4SJosef Bacik 			/*
112183d4cfd4SJosef Bacik 			 * Don't have to abort since we've not changed anything
112283d4cfd4SJosef Bacik 			 * in the file extent yet.
112383d4cfd4SJosef Bacik 			 */
112483d4cfd4SJosef Bacik 			break;
11253fd0a558SYan, Zheng 		}
11265d4f98a2SYan Zheng 
11275d4f98a2SYan Zheng 		btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr);
11285d4f98a2SYan Zheng 		dirty = 1;
11295d4f98a2SYan Zheng 
11305d4f98a2SYan Zheng 		key.offset -= btrfs_file_extent_offset(leaf, fi);
113182fa113fSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
113282fa113fSQu Wenruo 				       num_bytes, parent);
113382fa113fSQu Wenruo 		ref.real_root = root->root_key.objectid;
113482fa113fSQu Wenruo 		btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
1135b06c4bf5SFilipe Manana 				    key.objectid, key.offset);
113682fa113fSQu Wenruo 		ret = btrfs_inc_extent_ref(trans, &ref);
113783d4cfd4SJosef Bacik 		if (ret) {
113866642832SJeff Mahoney 			btrfs_abort_transaction(trans, ret);
113983d4cfd4SJosef Bacik 			break;
114083d4cfd4SJosef Bacik 		}
11415d4f98a2SYan Zheng 
1142ffd4bb2aSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
1143ffd4bb2aSQu Wenruo 				       num_bytes, parent);
1144ffd4bb2aSQu Wenruo 		ref.real_root = root->root_key.objectid;
1145ffd4bb2aSQu Wenruo 		btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
1146b06c4bf5SFilipe Manana 				    key.objectid, key.offset);
1147ffd4bb2aSQu Wenruo 		ret = btrfs_free_extent(trans, &ref);
114883d4cfd4SJosef Bacik 		if (ret) {
114966642832SJeff Mahoney 			btrfs_abort_transaction(trans, ret);
115083d4cfd4SJosef Bacik 			break;
115183d4cfd4SJosef Bacik 		}
11525d4f98a2SYan Zheng 	}
11535d4f98a2SYan Zheng 	if (dirty)
11545d4f98a2SYan Zheng 		btrfs_mark_buffer_dirty(leaf);
11553fd0a558SYan, Zheng 	if (inode)
11563fd0a558SYan, Zheng 		btrfs_add_delayed_iput(inode);
115783d4cfd4SJosef Bacik 	return ret;
11585d4f98a2SYan Zheng }
11595d4f98a2SYan Zheng 
11605d4f98a2SYan Zheng static noinline_for_stack
11615d4f98a2SYan Zheng int memcmp_node_keys(struct extent_buffer *eb, int slot,
11625d4f98a2SYan Zheng 		     struct btrfs_path *path, int level)
11635d4f98a2SYan Zheng {
11645d4f98a2SYan Zheng 	struct btrfs_disk_key key1;
11655d4f98a2SYan Zheng 	struct btrfs_disk_key key2;
11665d4f98a2SYan Zheng 	btrfs_node_key(eb, &key1, slot);
11675d4f98a2SYan Zheng 	btrfs_node_key(path->nodes[level], &key2, path->slots[level]);
11685d4f98a2SYan Zheng 	return memcmp(&key1, &key2, sizeof(key1));
11695d4f98a2SYan Zheng }
11705d4f98a2SYan Zheng 
11715d4f98a2SYan Zheng /*
11725d4f98a2SYan Zheng  * try to replace tree blocks in fs tree with the new blocks
11735d4f98a2SYan Zheng  * in reloc tree. tree blocks haven't been modified since the
11745d4f98a2SYan Zheng  * reloc tree was create can be replaced.
11755d4f98a2SYan Zheng  *
11765d4f98a2SYan Zheng  * if a block was replaced, level of the block + 1 is returned.
11775d4f98a2SYan Zheng  * if no block got replaced, 0 is returned. if there are other
11785d4f98a2SYan Zheng  * errors, a negative error number is returned.
11795d4f98a2SYan Zheng  */
11803fd0a558SYan, Zheng static noinline_for_stack
11813d0174f7SQu Wenruo int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc,
11825d4f98a2SYan Zheng 		 struct btrfs_root *dest, struct btrfs_root *src,
11835d4f98a2SYan Zheng 		 struct btrfs_path *path, struct btrfs_key *next_key,
11845d4f98a2SYan Zheng 		 int lowest_level, int max_level)
11855d4f98a2SYan Zheng {
11860b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = dest->fs_info;
11875d4f98a2SYan Zheng 	struct extent_buffer *eb;
11885d4f98a2SYan Zheng 	struct extent_buffer *parent;
118982fa113fSQu Wenruo 	struct btrfs_ref ref = { 0 };
11905d4f98a2SYan Zheng 	struct btrfs_key key;
11915d4f98a2SYan Zheng 	u64 old_bytenr;
11925d4f98a2SYan Zheng 	u64 new_bytenr;
11935d4f98a2SYan Zheng 	u64 old_ptr_gen;
11945d4f98a2SYan Zheng 	u64 new_ptr_gen;
11955d4f98a2SYan Zheng 	u64 last_snapshot;
11965d4f98a2SYan Zheng 	u32 blocksize;
11973fd0a558SYan, Zheng 	int cow = 0;
11985d4f98a2SYan Zheng 	int level;
11995d4f98a2SYan Zheng 	int ret;
12005d4f98a2SYan Zheng 	int slot;
12015d4f98a2SYan Zheng 
12025d4f98a2SYan Zheng 	BUG_ON(src->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
12035d4f98a2SYan Zheng 	BUG_ON(dest->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID);
12045d4f98a2SYan Zheng 
12055d4f98a2SYan Zheng 	last_snapshot = btrfs_root_last_snapshot(&src->root_item);
12063fd0a558SYan, Zheng again:
12075d4f98a2SYan Zheng 	slot = path->slots[lowest_level];
12085d4f98a2SYan Zheng 	btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot);
12095d4f98a2SYan Zheng 
12105d4f98a2SYan Zheng 	eb = btrfs_lock_root_node(dest);
12118bead258SDavid Sterba 	btrfs_set_lock_blocking_write(eb);
12125d4f98a2SYan Zheng 	level = btrfs_header_level(eb);
12135d4f98a2SYan Zheng 
12145d4f98a2SYan Zheng 	if (level < lowest_level) {
12155d4f98a2SYan Zheng 		btrfs_tree_unlock(eb);
12165d4f98a2SYan Zheng 		free_extent_buffer(eb);
12175d4f98a2SYan Zheng 		return 0;
12185d4f98a2SYan Zheng 	}
12195d4f98a2SYan Zheng 
12203fd0a558SYan, Zheng 	if (cow) {
12215d4f98a2SYan Zheng 		ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb);
12225d4f98a2SYan Zheng 		BUG_ON(ret);
12233fd0a558SYan, Zheng 	}
12248bead258SDavid Sterba 	btrfs_set_lock_blocking_write(eb);
12255d4f98a2SYan Zheng 
12265d4f98a2SYan Zheng 	if (next_key) {
12275d4f98a2SYan Zheng 		next_key->objectid = (u64)-1;
12285d4f98a2SYan Zheng 		next_key->type = (u8)-1;
12295d4f98a2SYan Zheng 		next_key->offset = (u64)-1;
12305d4f98a2SYan Zheng 	}
12315d4f98a2SYan Zheng 
12325d4f98a2SYan Zheng 	parent = eb;
12335d4f98a2SYan Zheng 	while (1) {
1234581c1760SQu Wenruo 		struct btrfs_key first_key;
1235581c1760SQu Wenruo 
12365d4f98a2SYan Zheng 		level = btrfs_header_level(parent);
12375d4f98a2SYan Zheng 		BUG_ON(level < lowest_level);
12385d4f98a2SYan Zheng 
1239e3b83361SQu Wenruo 		ret = btrfs_bin_search(parent, &key, &slot);
1240cbca7d59SFilipe Manana 		if (ret < 0)
1241cbca7d59SFilipe Manana 			break;
12425d4f98a2SYan Zheng 		if (ret && slot > 0)
12435d4f98a2SYan Zheng 			slot--;
12445d4f98a2SYan Zheng 
12455d4f98a2SYan Zheng 		if (next_key && slot + 1 < btrfs_header_nritems(parent))
12465d4f98a2SYan Zheng 			btrfs_node_key_to_cpu(parent, next_key, slot + 1);
12475d4f98a2SYan Zheng 
12485d4f98a2SYan Zheng 		old_bytenr = btrfs_node_blockptr(parent, slot);
12490b246afaSJeff Mahoney 		blocksize = fs_info->nodesize;
12505d4f98a2SYan Zheng 		old_ptr_gen = btrfs_node_ptr_generation(parent, slot);
125117515f1bSQu Wenruo 		btrfs_node_key_to_cpu(parent, &first_key, slot);
12525d4f98a2SYan Zheng 
12535d4f98a2SYan Zheng 		if (level <= max_level) {
12545d4f98a2SYan Zheng 			eb = path->nodes[level];
12555d4f98a2SYan Zheng 			new_bytenr = btrfs_node_blockptr(eb,
12565d4f98a2SYan Zheng 							path->slots[level]);
12575d4f98a2SYan Zheng 			new_ptr_gen = btrfs_node_ptr_generation(eb,
12585d4f98a2SYan Zheng 							path->slots[level]);
12595d4f98a2SYan Zheng 		} else {
12605d4f98a2SYan Zheng 			new_bytenr = 0;
12615d4f98a2SYan Zheng 			new_ptr_gen = 0;
12625d4f98a2SYan Zheng 		}
12635d4f98a2SYan Zheng 
1264fae7f21cSDulshani Gunawardhana 		if (WARN_ON(new_bytenr > 0 && new_bytenr == old_bytenr)) {
12655d4f98a2SYan Zheng 			ret = level;
12665d4f98a2SYan Zheng 			break;
12675d4f98a2SYan Zheng 		}
12685d4f98a2SYan Zheng 
12695d4f98a2SYan Zheng 		if (new_bytenr == 0 || old_ptr_gen > last_snapshot ||
12705d4f98a2SYan Zheng 		    memcmp_node_keys(parent, slot, path, level)) {
12713fd0a558SYan, Zheng 			if (level <= lowest_level) {
12725d4f98a2SYan Zheng 				ret = 0;
12735d4f98a2SYan Zheng 				break;
12745d4f98a2SYan Zheng 			}
12755d4f98a2SYan Zheng 
1276581c1760SQu Wenruo 			eb = read_tree_block(fs_info, old_bytenr, old_ptr_gen,
1277581c1760SQu Wenruo 					     level - 1, &first_key);
127864c043deSLiu Bo 			if (IS_ERR(eb)) {
127964c043deSLiu Bo 				ret = PTR_ERR(eb);
1280264813acSLiu Bo 				break;
128164c043deSLiu Bo 			} else if (!extent_buffer_uptodate(eb)) {
128264c043deSLiu Bo 				ret = -EIO;
1283416bc658SJosef Bacik 				free_extent_buffer(eb);
1284379cde74SStefan Behrens 				break;
1285416bc658SJosef Bacik 			}
12865d4f98a2SYan Zheng 			btrfs_tree_lock(eb);
12873fd0a558SYan, Zheng 			if (cow) {
12885d4f98a2SYan Zheng 				ret = btrfs_cow_block(trans, dest, eb, parent,
12895d4f98a2SYan Zheng 						      slot, &eb);
12905d4f98a2SYan Zheng 				BUG_ON(ret);
12915d4f98a2SYan Zheng 			}
12928bead258SDavid Sterba 			btrfs_set_lock_blocking_write(eb);
12935d4f98a2SYan Zheng 
12945d4f98a2SYan Zheng 			btrfs_tree_unlock(parent);
12955d4f98a2SYan Zheng 			free_extent_buffer(parent);
12965d4f98a2SYan Zheng 
12975d4f98a2SYan Zheng 			parent = eb;
12985d4f98a2SYan Zheng 			continue;
12995d4f98a2SYan Zheng 		}
13005d4f98a2SYan Zheng 
13013fd0a558SYan, Zheng 		if (!cow) {
13023fd0a558SYan, Zheng 			btrfs_tree_unlock(parent);
13033fd0a558SYan, Zheng 			free_extent_buffer(parent);
13043fd0a558SYan, Zheng 			cow = 1;
13053fd0a558SYan, Zheng 			goto again;
13063fd0a558SYan, Zheng 		}
13073fd0a558SYan, Zheng 
13085d4f98a2SYan Zheng 		btrfs_node_key_to_cpu(path->nodes[level], &key,
13095d4f98a2SYan Zheng 				      path->slots[level]);
1310b3b4aa74SDavid Sterba 		btrfs_release_path(path);
13115d4f98a2SYan Zheng 
13125d4f98a2SYan Zheng 		path->lowest_level = level;
13135d4f98a2SYan Zheng 		ret = btrfs_search_slot(trans, src, &key, path, 0, 1);
13145d4f98a2SYan Zheng 		path->lowest_level = 0;
13155d4f98a2SYan Zheng 		BUG_ON(ret);
13165d4f98a2SYan Zheng 
13175d4f98a2SYan Zheng 		/*
1318824d8dffSQu Wenruo 		 * Info qgroup to trace both subtrees.
1319824d8dffSQu Wenruo 		 *
1320824d8dffSQu Wenruo 		 * We must trace both trees.
1321824d8dffSQu Wenruo 		 * 1) Tree reloc subtree
1322824d8dffSQu Wenruo 		 *    If not traced, we will leak data numbers
1323824d8dffSQu Wenruo 		 * 2) Fs subtree
1324824d8dffSQu Wenruo 		 *    If not traced, we will double count old data
1325f616f5cdSQu Wenruo 		 *
1326f616f5cdSQu Wenruo 		 * We don't scan the subtree right now, but only record
1327f616f5cdSQu Wenruo 		 * the swapped tree blocks.
1328f616f5cdSQu Wenruo 		 * The real subtree rescan is delayed until we have new
1329f616f5cdSQu Wenruo 		 * CoW on the subtree root node before transaction commit.
1330824d8dffSQu Wenruo 		 */
1331370a11b8SQu Wenruo 		ret = btrfs_qgroup_add_swapped_blocks(trans, dest,
1332370a11b8SQu Wenruo 				rc->block_group, parent, slot,
1333370a11b8SQu Wenruo 				path->nodes[level], path->slots[level],
1334370a11b8SQu Wenruo 				last_snapshot);
1335370a11b8SQu Wenruo 		if (ret < 0)
1336370a11b8SQu Wenruo 			break;
1337824d8dffSQu Wenruo 		/*
13385d4f98a2SYan Zheng 		 * swap blocks in fs tree and reloc tree.
13395d4f98a2SYan Zheng 		 */
13405d4f98a2SYan Zheng 		btrfs_set_node_blockptr(parent, slot, new_bytenr);
13415d4f98a2SYan Zheng 		btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen);
13425d4f98a2SYan Zheng 		btrfs_mark_buffer_dirty(parent);
13435d4f98a2SYan Zheng 
13445d4f98a2SYan Zheng 		btrfs_set_node_blockptr(path->nodes[level],
13455d4f98a2SYan Zheng 					path->slots[level], old_bytenr);
13465d4f98a2SYan Zheng 		btrfs_set_node_ptr_generation(path->nodes[level],
13475d4f98a2SYan Zheng 					      path->slots[level], old_ptr_gen);
13485d4f98a2SYan Zheng 		btrfs_mark_buffer_dirty(path->nodes[level]);
13495d4f98a2SYan Zheng 
135082fa113fSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, old_bytenr,
135182fa113fSQu Wenruo 				       blocksize, path->nodes[level]->start);
135282fa113fSQu Wenruo 		ref.skip_qgroup = true;
135382fa113fSQu Wenruo 		btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid);
135482fa113fSQu Wenruo 		ret = btrfs_inc_extent_ref(trans, &ref);
13555d4f98a2SYan Zheng 		BUG_ON(ret);
135682fa113fSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
135782fa113fSQu Wenruo 				       blocksize, 0);
135882fa113fSQu Wenruo 		ref.skip_qgroup = true;
135982fa113fSQu Wenruo 		btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid);
136082fa113fSQu Wenruo 		ret = btrfs_inc_extent_ref(trans, &ref);
13615d4f98a2SYan Zheng 		BUG_ON(ret);
13625d4f98a2SYan Zheng 
1363ffd4bb2aSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, new_bytenr,
1364ffd4bb2aSQu Wenruo 				       blocksize, path->nodes[level]->start);
1365ffd4bb2aSQu Wenruo 		btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid);
1366ffd4bb2aSQu Wenruo 		ref.skip_qgroup = true;
1367ffd4bb2aSQu Wenruo 		ret = btrfs_free_extent(trans, &ref);
13685d4f98a2SYan Zheng 		BUG_ON(ret);
13695d4f98a2SYan Zheng 
1370ffd4bb2aSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, old_bytenr,
1371ffd4bb2aSQu Wenruo 				       blocksize, 0);
1372ffd4bb2aSQu Wenruo 		btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid);
1373ffd4bb2aSQu Wenruo 		ref.skip_qgroup = true;
1374ffd4bb2aSQu Wenruo 		ret = btrfs_free_extent(trans, &ref);
13755d4f98a2SYan Zheng 		BUG_ON(ret);
13765d4f98a2SYan Zheng 
13775d4f98a2SYan Zheng 		btrfs_unlock_up_safe(path, 0);
13785d4f98a2SYan Zheng 
13795d4f98a2SYan Zheng 		ret = level;
13805d4f98a2SYan Zheng 		break;
13815d4f98a2SYan Zheng 	}
13825d4f98a2SYan Zheng 	btrfs_tree_unlock(parent);
13835d4f98a2SYan Zheng 	free_extent_buffer(parent);
13845d4f98a2SYan Zheng 	return ret;
13855d4f98a2SYan Zheng }
13865d4f98a2SYan Zheng 
13875d4f98a2SYan Zheng /*
13885d4f98a2SYan Zheng  * helper to find next relocated block in reloc tree
13895d4f98a2SYan Zheng  */
13905d4f98a2SYan Zheng static noinline_for_stack
13915d4f98a2SYan Zheng int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
13925d4f98a2SYan Zheng 		       int *level)
13935d4f98a2SYan Zheng {
13945d4f98a2SYan Zheng 	struct extent_buffer *eb;
13955d4f98a2SYan Zheng 	int i;
13965d4f98a2SYan Zheng 	u64 last_snapshot;
13975d4f98a2SYan Zheng 	u32 nritems;
13985d4f98a2SYan Zheng 
13995d4f98a2SYan Zheng 	last_snapshot = btrfs_root_last_snapshot(&root->root_item);
14005d4f98a2SYan Zheng 
14015d4f98a2SYan Zheng 	for (i = 0; i < *level; i++) {
14025d4f98a2SYan Zheng 		free_extent_buffer(path->nodes[i]);
14035d4f98a2SYan Zheng 		path->nodes[i] = NULL;
14045d4f98a2SYan Zheng 	}
14055d4f98a2SYan Zheng 
14065d4f98a2SYan Zheng 	for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
14075d4f98a2SYan Zheng 		eb = path->nodes[i];
14085d4f98a2SYan Zheng 		nritems = btrfs_header_nritems(eb);
14095d4f98a2SYan Zheng 		while (path->slots[i] + 1 < nritems) {
14105d4f98a2SYan Zheng 			path->slots[i]++;
14115d4f98a2SYan Zheng 			if (btrfs_node_ptr_generation(eb, path->slots[i]) <=
14125d4f98a2SYan Zheng 			    last_snapshot)
14135d4f98a2SYan Zheng 				continue;
14145d4f98a2SYan Zheng 
14155d4f98a2SYan Zheng 			*level = i;
14165d4f98a2SYan Zheng 			return 0;
14175d4f98a2SYan Zheng 		}
14185d4f98a2SYan Zheng 		free_extent_buffer(path->nodes[i]);
14195d4f98a2SYan Zheng 		path->nodes[i] = NULL;
14205d4f98a2SYan Zheng 	}
14215d4f98a2SYan Zheng 	return 1;
14225d4f98a2SYan Zheng }
14235d4f98a2SYan Zheng 
14245d4f98a2SYan Zheng /*
14255d4f98a2SYan Zheng  * walk down reloc tree to find relocated block of lowest level
14265d4f98a2SYan Zheng  */
14275d4f98a2SYan Zheng static noinline_for_stack
14285d4f98a2SYan Zheng int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
14295d4f98a2SYan Zheng 			 int *level)
14305d4f98a2SYan Zheng {
14312ff7e61eSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
14325d4f98a2SYan Zheng 	struct extent_buffer *eb = NULL;
14335d4f98a2SYan Zheng 	int i;
14345d4f98a2SYan Zheng 	u64 bytenr;
14355d4f98a2SYan Zheng 	u64 ptr_gen = 0;
14365d4f98a2SYan Zheng 	u64 last_snapshot;
14375d4f98a2SYan Zheng 	u32 nritems;
14385d4f98a2SYan Zheng 
14395d4f98a2SYan Zheng 	last_snapshot = btrfs_root_last_snapshot(&root->root_item);
14405d4f98a2SYan Zheng 
14415d4f98a2SYan Zheng 	for (i = *level; i > 0; i--) {
1442581c1760SQu Wenruo 		struct btrfs_key first_key;
1443581c1760SQu Wenruo 
14445d4f98a2SYan Zheng 		eb = path->nodes[i];
14455d4f98a2SYan Zheng 		nritems = btrfs_header_nritems(eb);
14465d4f98a2SYan Zheng 		while (path->slots[i] < nritems) {
14475d4f98a2SYan Zheng 			ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]);
14485d4f98a2SYan Zheng 			if (ptr_gen > last_snapshot)
14495d4f98a2SYan Zheng 				break;
14505d4f98a2SYan Zheng 			path->slots[i]++;
14515d4f98a2SYan Zheng 		}
14525d4f98a2SYan Zheng 		if (path->slots[i] >= nritems) {
14535d4f98a2SYan Zheng 			if (i == *level)
14545d4f98a2SYan Zheng 				break;
14555d4f98a2SYan Zheng 			*level = i + 1;
14565d4f98a2SYan Zheng 			return 0;
14575d4f98a2SYan Zheng 		}
14585d4f98a2SYan Zheng 		if (i == 1) {
14595d4f98a2SYan Zheng 			*level = i;
14605d4f98a2SYan Zheng 			return 0;
14615d4f98a2SYan Zheng 		}
14625d4f98a2SYan Zheng 
14635d4f98a2SYan Zheng 		bytenr = btrfs_node_blockptr(eb, path->slots[i]);
1464581c1760SQu Wenruo 		btrfs_node_key_to_cpu(eb, &first_key, path->slots[i]);
1465581c1760SQu Wenruo 		eb = read_tree_block(fs_info, bytenr, ptr_gen, i - 1,
1466581c1760SQu Wenruo 				     &first_key);
146764c043deSLiu Bo 		if (IS_ERR(eb)) {
146864c043deSLiu Bo 			return PTR_ERR(eb);
146964c043deSLiu Bo 		} else if (!extent_buffer_uptodate(eb)) {
1470416bc658SJosef Bacik 			free_extent_buffer(eb);
1471416bc658SJosef Bacik 			return -EIO;
1472416bc658SJosef Bacik 		}
14735d4f98a2SYan Zheng 		BUG_ON(btrfs_header_level(eb) != i - 1);
14745d4f98a2SYan Zheng 		path->nodes[i - 1] = eb;
14755d4f98a2SYan Zheng 		path->slots[i - 1] = 0;
14765d4f98a2SYan Zheng 	}
14775d4f98a2SYan Zheng 	return 1;
14785d4f98a2SYan Zheng }
14795d4f98a2SYan Zheng 
14805d4f98a2SYan Zheng /*
14815d4f98a2SYan Zheng  * invalidate extent cache for file extents whose key in range of
14825d4f98a2SYan Zheng  * [min_key, max_key)
14835d4f98a2SYan Zheng  */
14845d4f98a2SYan Zheng static int invalidate_extent_cache(struct btrfs_root *root,
14855d4f98a2SYan Zheng 				   struct btrfs_key *min_key,
14865d4f98a2SYan Zheng 				   struct btrfs_key *max_key)
14875d4f98a2SYan Zheng {
14880b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
14895d4f98a2SYan Zheng 	struct inode *inode = NULL;
14905d4f98a2SYan Zheng 	u64 objectid;
14915d4f98a2SYan Zheng 	u64 start, end;
149233345d01SLi Zefan 	u64 ino;
14935d4f98a2SYan Zheng 
14945d4f98a2SYan Zheng 	objectid = min_key->objectid;
14955d4f98a2SYan Zheng 	while (1) {
14965d4f98a2SYan Zheng 		cond_resched();
14975d4f98a2SYan Zheng 		iput(inode);
14985d4f98a2SYan Zheng 
14995d4f98a2SYan Zheng 		if (objectid > max_key->objectid)
15005d4f98a2SYan Zheng 			break;
15015d4f98a2SYan Zheng 
15025d4f98a2SYan Zheng 		inode = find_next_inode(root, objectid);
15035d4f98a2SYan Zheng 		if (!inode)
15045d4f98a2SYan Zheng 			break;
15054a0cc7caSNikolay Borisov 		ino = btrfs_ino(BTRFS_I(inode));
15065d4f98a2SYan Zheng 
150733345d01SLi Zefan 		if (ino > max_key->objectid) {
15085d4f98a2SYan Zheng 			iput(inode);
15095d4f98a2SYan Zheng 			break;
15105d4f98a2SYan Zheng 		}
15115d4f98a2SYan Zheng 
151233345d01SLi Zefan 		objectid = ino + 1;
15135d4f98a2SYan Zheng 		if (!S_ISREG(inode->i_mode))
15145d4f98a2SYan Zheng 			continue;
15155d4f98a2SYan Zheng 
151633345d01SLi Zefan 		if (unlikely(min_key->objectid == ino)) {
15175d4f98a2SYan Zheng 			if (min_key->type > BTRFS_EXTENT_DATA_KEY)
15185d4f98a2SYan Zheng 				continue;
15195d4f98a2SYan Zheng 			if (min_key->type < BTRFS_EXTENT_DATA_KEY)
15205d4f98a2SYan Zheng 				start = 0;
15215d4f98a2SYan Zheng 			else {
15225d4f98a2SYan Zheng 				start = min_key->offset;
15230b246afaSJeff Mahoney 				WARN_ON(!IS_ALIGNED(start, fs_info->sectorsize));
15245d4f98a2SYan Zheng 			}
15255d4f98a2SYan Zheng 		} else {
15265d4f98a2SYan Zheng 			start = 0;
15275d4f98a2SYan Zheng 		}
15285d4f98a2SYan Zheng 
152933345d01SLi Zefan 		if (unlikely(max_key->objectid == ino)) {
15305d4f98a2SYan Zheng 			if (max_key->type < BTRFS_EXTENT_DATA_KEY)
15315d4f98a2SYan Zheng 				continue;
15325d4f98a2SYan Zheng 			if (max_key->type > BTRFS_EXTENT_DATA_KEY) {
15335d4f98a2SYan Zheng 				end = (u64)-1;
15345d4f98a2SYan Zheng 			} else {
15355d4f98a2SYan Zheng 				if (max_key->offset == 0)
15365d4f98a2SYan Zheng 					continue;
15375d4f98a2SYan Zheng 				end = max_key->offset;
15380b246afaSJeff Mahoney 				WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize));
15395d4f98a2SYan Zheng 				end--;
15405d4f98a2SYan Zheng 			}
15415d4f98a2SYan Zheng 		} else {
15425d4f98a2SYan Zheng 			end = (u64)-1;
15435d4f98a2SYan Zheng 		}
15445d4f98a2SYan Zheng 
15455d4f98a2SYan Zheng 		/* the lock_extent waits for readpage to complete */
1546d0082371SJeff Mahoney 		lock_extent(&BTRFS_I(inode)->io_tree, start, end);
1547dcdbc059SNikolay Borisov 		btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 1);
1548d0082371SJeff Mahoney 		unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
15495d4f98a2SYan Zheng 	}
15505d4f98a2SYan Zheng 	return 0;
15515d4f98a2SYan Zheng }
15525d4f98a2SYan Zheng 
15535d4f98a2SYan Zheng static int find_next_key(struct btrfs_path *path, int level,
15545d4f98a2SYan Zheng 			 struct btrfs_key *key)
15555d4f98a2SYan Zheng 
15565d4f98a2SYan Zheng {
15575d4f98a2SYan Zheng 	while (level < BTRFS_MAX_LEVEL) {
15585d4f98a2SYan Zheng 		if (!path->nodes[level])
15595d4f98a2SYan Zheng 			break;
15605d4f98a2SYan Zheng 		if (path->slots[level] + 1 <
15615d4f98a2SYan Zheng 		    btrfs_header_nritems(path->nodes[level])) {
15625d4f98a2SYan Zheng 			btrfs_node_key_to_cpu(path->nodes[level], key,
15635d4f98a2SYan Zheng 					      path->slots[level] + 1);
15645d4f98a2SYan Zheng 			return 0;
15655d4f98a2SYan Zheng 		}
15665d4f98a2SYan Zheng 		level++;
15675d4f98a2SYan Zheng 	}
15685d4f98a2SYan Zheng 	return 1;
15695d4f98a2SYan Zheng }
15705d4f98a2SYan Zheng 
15715d4f98a2SYan Zheng /*
1572d2311e69SQu Wenruo  * Insert current subvolume into reloc_control::dirty_subvol_roots
1573d2311e69SQu Wenruo  */
1574d2311e69SQu Wenruo static void insert_dirty_subvol(struct btrfs_trans_handle *trans,
1575d2311e69SQu Wenruo 				struct reloc_control *rc,
1576d2311e69SQu Wenruo 				struct btrfs_root *root)
1577d2311e69SQu Wenruo {
1578d2311e69SQu Wenruo 	struct btrfs_root *reloc_root = root->reloc_root;
1579d2311e69SQu Wenruo 	struct btrfs_root_item *reloc_root_item;
1580d2311e69SQu Wenruo 
1581d2311e69SQu Wenruo 	/* @root must be a subvolume tree root with a valid reloc tree */
1582d2311e69SQu Wenruo 	ASSERT(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
1583d2311e69SQu Wenruo 	ASSERT(reloc_root);
1584d2311e69SQu Wenruo 
1585d2311e69SQu Wenruo 	reloc_root_item = &reloc_root->root_item;
1586d2311e69SQu Wenruo 	memset(&reloc_root_item->drop_progress, 0,
1587d2311e69SQu Wenruo 		sizeof(reloc_root_item->drop_progress));
1588d2311e69SQu Wenruo 	reloc_root_item->drop_level = 0;
1589d2311e69SQu Wenruo 	btrfs_set_root_refs(reloc_root_item, 0);
1590d2311e69SQu Wenruo 	btrfs_update_reloc_root(trans, root);
1591d2311e69SQu Wenruo 
1592d2311e69SQu Wenruo 	if (list_empty(&root->reloc_dirty_list)) {
159300246528SJosef Bacik 		btrfs_grab_root(root);
1594d2311e69SQu Wenruo 		list_add_tail(&root->reloc_dirty_list, &rc->dirty_subvol_roots);
1595d2311e69SQu Wenruo 	}
1596d2311e69SQu Wenruo }
1597d2311e69SQu Wenruo 
1598d2311e69SQu Wenruo static int clean_dirty_subvols(struct reloc_control *rc)
1599d2311e69SQu Wenruo {
1600d2311e69SQu Wenruo 	struct btrfs_root *root;
1601d2311e69SQu Wenruo 	struct btrfs_root *next;
1602d2311e69SQu Wenruo 	int ret = 0;
160330d40577SQu Wenruo 	int ret2;
1604d2311e69SQu Wenruo 
1605d2311e69SQu Wenruo 	list_for_each_entry_safe(root, next, &rc->dirty_subvol_roots,
1606d2311e69SQu Wenruo 				 reloc_dirty_list) {
160730d40577SQu Wenruo 		if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
160830d40577SQu Wenruo 			/* Merged subvolume, cleanup its reloc root */
1609d2311e69SQu Wenruo 			struct btrfs_root *reloc_root = root->reloc_root;
1610d2311e69SQu Wenruo 
1611d2311e69SQu Wenruo 			list_del_init(&root->reloc_dirty_list);
1612d2311e69SQu Wenruo 			root->reloc_root = NULL;
16136282675eSQu Wenruo 			/*
16146282675eSQu Wenruo 			 * Need barrier to ensure clear_bit() only happens after
16156282675eSQu Wenruo 			 * root->reloc_root = NULL. Pairs with have_reloc_root.
16166282675eSQu Wenruo 			 */
16176282675eSQu Wenruo 			smp_wmb();
16181fac4a54SQu Wenruo 			clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
1619f28de8d8SJosef Bacik 			if (reloc_root) {
1620f44deb74SJosef Bacik 				/*
1621f44deb74SJosef Bacik 				 * btrfs_drop_snapshot drops our ref we hold for
1622f44deb74SJosef Bacik 				 * ->reloc_root.  If it fails however we must
1623f44deb74SJosef Bacik 				 * drop the ref ourselves.
1624f44deb74SJosef Bacik 				 */
1625f28de8d8SJosef Bacik 				ret2 = btrfs_drop_snapshot(reloc_root, 0, 1);
1626f44deb74SJosef Bacik 				if (ret2 < 0) {
1627f44deb74SJosef Bacik 					btrfs_put_root(reloc_root);
1628f44deb74SJosef Bacik 					if (!ret)
1629f28de8d8SJosef Bacik 						ret = ret2;
1630f28de8d8SJosef Bacik 				}
1631f44deb74SJosef Bacik 			}
163200246528SJosef Bacik 			btrfs_put_root(root);
163330d40577SQu Wenruo 		} else {
163430d40577SQu Wenruo 			/* Orphan reloc tree, just clean it up */
16350078a9f9SNikolay Borisov 			ret2 = btrfs_drop_snapshot(root, 0, 1);
1636f44deb74SJosef Bacik 			if (ret2 < 0) {
1637f44deb74SJosef Bacik 				btrfs_put_root(root);
1638f44deb74SJosef Bacik 				if (!ret)
163930d40577SQu Wenruo 					ret = ret2;
164030d40577SQu Wenruo 			}
1641d2311e69SQu Wenruo 		}
1642f44deb74SJosef Bacik 	}
1643d2311e69SQu Wenruo 	return ret;
1644d2311e69SQu Wenruo }
1645d2311e69SQu Wenruo 
1646d2311e69SQu Wenruo /*
16475d4f98a2SYan Zheng  * merge the relocated tree blocks in reloc tree with corresponding
16485d4f98a2SYan Zheng  * fs tree.
16495d4f98a2SYan Zheng  */
16505d4f98a2SYan Zheng static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
16515d4f98a2SYan Zheng 					       struct btrfs_root *root)
16525d4f98a2SYan Zheng {
16530b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
16545d4f98a2SYan Zheng 	struct btrfs_key key;
16555d4f98a2SYan Zheng 	struct btrfs_key next_key;
16569e6a0c52SJosef Bacik 	struct btrfs_trans_handle *trans = NULL;
16575d4f98a2SYan Zheng 	struct btrfs_root *reloc_root;
16585d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
16595d4f98a2SYan Zheng 	struct btrfs_path *path;
16603fd0a558SYan, Zheng 	struct extent_buffer *leaf;
16615d4f98a2SYan Zheng 	int level;
16625d4f98a2SYan Zheng 	int max_level;
16635d4f98a2SYan Zheng 	int replaced = 0;
16645d4f98a2SYan Zheng 	int ret;
16655d4f98a2SYan Zheng 	int err = 0;
16663fd0a558SYan, Zheng 	u32 min_reserved;
16675d4f98a2SYan Zheng 
16685d4f98a2SYan Zheng 	path = btrfs_alloc_path();
16695d4f98a2SYan Zheng 	if (!path)
16705d4f98a2SYan Zheng 		return -ENOMEM;
1671e4058b54SDavid Sterba 	path->reada = READA_FORWARD;
16725d4f98a2SYan Zheng 
16735d4f98a2SYan Zheng 	reloc_root = root->reloc_root;
16745d4f98a2SYan Zheng 	root_item = &reloc_root->root_item;
16755d4f98a2SYan Zheng 
16765d4f98a2SYan Zheng 	if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
16775d4f98a2SYan Zheng 		level = btrfs_root_level(root_item);
167867439dadSDavid Sterba 		atomic_inc(&reloc_root->node->refs);
16795d4f98a2SYan Zheng 		path->nodes[level] = reloc_root->node;
16805d4f98a2SYan Zheng 		path->slots[level] = 0;
16815d4f98a2SYan Zheng 	} else {
16825d4f98a2SYan Zheng 		btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
16835d4f98a2SYan Zheng 
16845d4f98a2SYan Zheng 		level = root_item->drop_level;
16855d4f98a2SYan Zheng 		BUG_ON(level == 0);
16865d4f98a2SYan Zheng 		path->lowest_level = level;
16875d4f98a2SYan Zheng 		ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0);
168833c66f43SYan Zheng 		path->lowest_level = 0;
16895d4f98a2SYan Zheng 		if (ret < 0) {
16905d4f98a2SYan Zheng 			btrfs_free_path(path);
16915d4f98a2SYan Zheng 			return ret;
16925d4f98a2SYan Zheng 		}
16935d4f98a2SYan Zheng 
16945d4f98a2SYan Zheng 		btrfs_node_key_to_cpu(path->nodes[level], &next_key,
16955d4f98a2SYan Zheng 				      path->slots[level]);
16965d4f98a2SYan Zheng 		WARN_ON(memcmp(&key, &next_key, sizeof(key)));
16975d4f98a2SYan Zheng 
16985d4f98a2SYan Zheng 		btrfs_unlock_up_safe(path, 0);
16995d4f98a2SYan Zheng 	}
17005d4f98a2SYan Zheng 
17010b246afaSJeff Mahoney 	min_reserved = fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
17025d4f98a2SYan Zheng 	memset(&next_key, 0, sizeof(next_key));
17035d4f98a2SYan Zheng 
17045d4f98a2SYan Zheng 	while (1) {
170508e007d2SMiao Xie 		ret = btrfs_block_rsv_refill(root, rc->block_rsv, min_reserved,
170608e007d2SMiao Xie 					     BTRFS_RESERVE_FLUSH_ALL);
17073fd0a558SYan, Zheng 		if (ret) {
17089e6a0c52SJosef Bacik 			err = ret;
17099e6a0c52SJosef Bacik 			goto out;
17103fd0a558SYan, Zheng 		}
17119e6a0c52SJosef Bacik 		trans = btrfs_start_transaction(root, 0);
17129e6a0c52SJosef Bacik 		if (IS_ERR(trans)) {
17139e6a0c52SJosef Bacik 			err = PTR_ERR(trans);
17149e6a0c52SJosef Bacik 			trans = NULL;
17159e6a0c52SJosef Bacik 			goto out;
17169e6a0c52SJosef Bacik 		}
17172abc726aSJosef Bacik 
17182abc726aSJosef Bacik 		/*
17192abc726aSJosef Bacik 		 * At this point we no longer have a reloc_control, so we can't
17202abc726aSJosef Bacik 		 * depend on btrfs_init_reloc_root to update our last_trans.
17212abc726aSJosef Bacik 		 *
17222abc726aSJosef Bacik 		 * But that's ok, we started the trans handle on our
17232abc726aSJosef Bacik 		 * corresponding fs_root, which means it's been added to the
17242abc726aSJosef Bacik 		 * dirty list.  At commit time we'll still call
17252abc726aSJosef Bacik 		 * btrfs_update_reloc_root() and update our root item
17262abc726aSJosef Bacik 		 * appropriately.
17272abc726aSJosef Bacik 		 */
17282abc726aSJosef Bacik 		reloc_root->last_trans = trans->transid;
17299e6a0c52SJosef Bacik 		trans->block_rsv = rc->block_rsv;
17303fd0a558SYan, Zheng 
17313fd0a558SYan, Zheng 		replaced = 0;
17325d4f98a2SYan Zheng 		max_level = level;
17335d4f98a2SYan Zheng 
17345d4f98a2SYan Zheng 		ret = walk_down_reloc_tree(reloc_root, path, &level);
17355d4f98a2SYan Zheng 		if (ret < 0) {
17365d4f98a2SYan Zheng 			err = ret;
17375d4f98a2SYan Zheng 			goto out;
17385d4f98a2SYan Zheng 		}
17395d4f98a2SYan Zheng 		if (ret > 0)
17405d4f98a2SYan Zheng 			break;
17415d4f98a2SYan Zheng 
17425d4f98a2SYan Zheng 		if (!find_next_key(path, level, &key) &&
17435d4f98a2SYan Zheng 		    btrfs_comp_cpu_keys(&next_key, &key) >= 0) {
17445d4f98a2SYan Zheng 			ret = 0;
17455d4f98a2SYan Zheng 		} else {
17463d0174f7SQu Wenruo 			ret = replace_path(trans, rc, root, reloc_root, path,
17473fd0a558SYan, Zheng 					   &next_key, level, max_level);
17485d4f98a2SYan Zheng 		}
17495d4f98a2SYan Zheng 		if (ret < 0) {
17505d4f98a2SYan Zheng 			err = ret;
17515d4f98a2SYan Zheng 			goto out;
17525d4f98a2SYan Zheng 		}
17535d4f98a2SYan Zheng 
17545d4f98a2SYan Zheng 		if (ret > 0) {
17555d4f98a2SYan Zheng 			level = ret;
17565d4f98a2SYan Zheng 			btrfs_node_key_to_cpu(path->nodes[level], &key,
17575d4f98a2SYan Zheng 					      path->slots[level]);
17585d4f98a2SYan Zheng 			replaced = 1;
17595d4f98a2SYan Zheng 		}
17605d4f98a2SYan Zheng 
17615d4f98a2SYan Zheng 		ret = walk_up_reloc_tree(reloc_root, path, &level);
17625d4f98a2SYan Zheng 		if (ret > 0)
17635d4f98a2SYan Zheng 			break;
17645d4f98a2SYan Zheng 
17655d4f98a2SYan Zheng 		BUG_ON(level == 0);
17665d4f98a2SYan Zheng 		/*
17675d4f98a2SYan Zheng 		 * save the merging progress in the drop_progress.
17685d4f98a2SYan Zheng 		 * this is OK since root refs == 1 in this case.
17695d4f98a2SYan Zheng 		 */
17705d4f98a2SYan Zheng 		btrfs_node_key(path->nodes[level], &root_item->drop_progress,
17715d4f98a2SYan Zheng 			       path->slots[level]);
17725d4f98a2SYan Zheng 		root_item->drop_level = level;
17735d4f98a2SYan Zheng 
17743a45bb20SJeff Mahoney 		btrfs_end_transaction_throttle(trans);
17759e6a0c52SJosef Bacik 		trans = NULL;
17765d4f98a2SYan Zheng 
17772ff7e61eSJeff Mahoney 		btrfs_btree_balance_dirty(fs_info);
17785d4f98a2SYan Zheng 
17795d4f98a2SYan Zheng 		if (replaced && rc->stage == UPDATE_DATA_PTRS)
17805d4f98a2SYan Zheng 			invalidate_extent_cache(root, &key, &next_key);
17815d4f98a2SYan Zheng 	}
17825d4f98a2SYan Zheng 
17835d4f98a2SYan Zheng 	/*
17845d4f98a2SYan Zheng 	 * handle the case only one block in the fs tree need to be
17855d4f98a2SYan Zheng 	 * relocated and the block is tree root.
17865d4f98a2SYan Zheng 	 */
17875d4f98a2SYan Zheng 	leaf = btrfs_lock_root_node(root);
17885d4f98a2SYan Zheng 	ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf);
17895d4f98a2SYan Zheng 	btrfs_tree_unlock(leaf);
17905d4f98a2SYan Zheng 	free_extent_buffer(leaf);
17915d4f98a2SYan Zheng 	if (ret < 0)
17925d4f98a2SYan Zheng 		err = ret;
17935d4f98a2SYan Zheng out:
17945d4f98a2SYan Zheng 	btrfs_free_path(path);
17955d4f98a2SYan Zheng 
1796d2311e69SQu Wenruo 	if (err == 0)
1797d2311e69SQu Wenruo 		insert_dirty_subvol(trans, rc, root);
17985d4f98a2SYan Zheng 
17999e6a0c52SJosef Bacik 	if (trans)
18003a45bb20SJeff Mahoney 		btrfs_end_transaction_throttle(trans);
18015d4f98a2SYan Zheng 
18022ff7e61eSJeff Mahoney 	btrfs_btree_balance_dirty(fs_info);
18035d4f98a2SYan Zheng 
18045d4f98a2SYan Zheng 	if (replaced && rc->stage == UPDATE_DATA_PTRS)
18055d4f98a2SYan Zheng 		invalidate_extent_cache(root, &key, &next_key);
18065d4f98a2SYan Zheng 
18075d4f98a2SYan Zheng 	return err;
18085d4f98a2SYan Zheng }
18095d4f98a2SYan Zheng 
18103fd0a558SYan, Zheng static noinline_for_stack
18113fd0a558SYan, Zheng int prepare_to_merge(struct reloc_control *rc, int err)
18125d4f98a2SYan Zheng {
18133fd0a558SYan, Zheng 	struct btrfs_root *root = rc->extent_root;
18140b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
18153fd0a558SYan, Zheng 	struct btrfs_root *reloc_root;
18165d4f98a2SYan Zheng 	struct btrfs_trans_handle *trans;
18173fd0a558SYan, Zheng 	LIST_HEAD(reloc_roots);
18183fd0a558SYan, Zheng 	u64 num_bytes = 0;
18193fd0a558SYan, Zheng 	int ret;
18203fd0a558SYan, Zheng 
18210b246afaSJeff Mahoney 	mutex_lock(&fs_info->reloc_mutex);
18220b246afaSJeff Mahoney 	rc->merging_rsv_size += fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
18233fd0a558SYan, Zheng 	rc->merging_rsv_size += rc->nodes_relocated * 2;
18240b246afaSJeff Mahoney 	mutex_unlock(&fs_info->reloc_mutex);
18257585717fSChris Mason 
18263fd0a558SYan, Zheng again:
18273fd0a558SYan, Zheng 	if (!err) {
18283fd0a558SYan, Zheng 		num_bytes = rc->merging_rsv_size;
182908e007d2SMiao Xie 		ret = btrfs_block_rsv_add(root, rc->block_rsv, num_bytes,
183008e007d2SMiao Xie 					  BTRFS_RESERVE_FLUSH_ALL);
18313fd0a558SYan, Zheng 		if (ret)
18323fd0a558SYan, Zheng 			err = ret;
18333fd0a558SYan, Zheng 	}
18343fd0a558SYan, Zheng 
18357a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(rc->extent_root);
18363612b495STsutomu Itoh 	if (IS_ERR(trans)) {
18373612b495STsutomu Itoh 		if (!err)
18382ff7e61eSJeff Mahoney 			btrfs_block_rsv_release(fs_info, rc->block_rsv,
183963f018beSNikolay Borisov 						num_bytes, NULL);
18403612b495STsutomu Itoh 		return PTR_ERR(trans);
18413612b495STsutomu Itoh 	}
18423fd0a558SYan, Zheng 
18433fd0a558SYan, Zheng 	if (!err) {
18443fd0a558SYan, Zheng 		if (num_bytes != rc->merging_rsv_size) {
18453a45bb20SJeff Mahoney 			btrfs_end_transaction(trans);
18462ff7e61eSJeff Mahoney 			btrfs_block_rsv_release(fs_info, rc->block_rsv,
184763f018beSNikolay Borisov 						num_bytes, NULL);
18483fd0a558SYan, Zheng 			goto again;
18493fd0a558SYan, Zheng 		}
18503fd0a558SYan, Zheng 	}
18513fd0a558SYan, Zheng 
18523fd0a558SYan, Zheng 	rc->merge_reloc_tree = 1;
18533fd0a558SYan, Zheng 
18543fd0a558SYan, Zheng 	while (!list_empty(&rc->reloc_roots)) {
18553fd0a558SYan, Zheng 		reloc_root = list_entry(rc->reloc_roots.next,
18563fd0a558SYan, Zheng 					struct btrfs_root, root_list);
18573fd0a558SYan, Zheng 		list_del_init(&reloc_root->root_list);
18583fd0a558SYan, Zheng 
18590b246afaSJeff Mahoney 		root = read_fs_root(fs_info, reloc_root->root_key.offset);
18603fd0a558SYan, Zheng 		BUG_ON(IS_ERR(root));
18613fd0a558SYan, Zheng 		BUG_ON(root->reloc_root != reloc_root);
18623fd0a558SYan, Zheng 
18633fd0a558SYan, Zheng 		/*
18643fd0a558SYan, Zheng 		 * set reference count to 1, so btrfs_recover_relocation
18653fd0a558SYan, Zheng 		 * knows it should resumes merging
18663fd0a558SYan, Zheng 		 */
18673fd0a558SYan, Zheng 		if (!err)
18683fd0a558SYan, Zheng 			btrfs_set_root_refs(&reloc_root->root_item, 1);
18693fd0a558SYan, Zheng 		btrfs_update_reloc_root(trans, root);
18703fd0a558SYan, Zheng 
18713fd0a558SYan, Zheng 		list_add(&reloc_root->root_list, &reloc_roots);
187200246528SJosef Bacik 		btrfs_put_root(root);
18733fd0a558SYan, Zheng 	}
18743fd0a558SYan, Zheng 
18753fd0a558SYan, Zheng 	list_splice(&reloc_roots, &rc->reloc_roots);
18763fd0a558SYan, Zheng 
18773fd0a558SYan, Zheng 	if (!err)
18783a45bb20SJeff Mahoney 		btrfs_commit_transaction(trans);
18793fd0a558SYan, Zheng 	else
18803a45bb20SJeff Mahoney 		btrfs_end_transaction(trans);
18813fd0a558SYan, Zheng 	return err;
18823fd0a558SYan, Zheng }
18833fd0a558SYan, Zheng 
18843fd0a558SYan, Zheng static noinline_for_stack
1885aca1bba6SLiu Bo void free_reloc_roots(struct list_head *list)
1886aca1bba6SLiu Bo {
1887a7571232SNikolay Borisov 	struct btrfs_root *reloc_root, *tmp;
1888aca1bba6SLiu Bo 
1889a7571232SNikolay Borisov 	list_for_each_entry_safe(reloc_root, tmp, list, root_list)
1890bb166d72SNaohiro Aota 		__del_reloc_root(reloc_root);
1891aca1bba6SLiu Bo }
1892aca1bba6SLiu Bo 
1893aca1bba6SLiu Bo static noinline_for_stack
189494404e82SDavid Sterba void merge_reloc_roots(struct reloc_control *rc)
18953fd0a558SYan, Zheng {
18960b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
18975d4f98a2SYan Zheng 	struct btrfs_root *root;
18985d4f98a2SYan Zheng 	struct btrfs_root *reloc_root;
18993fd0a558SYan, Zheng 	LIST_HEAD(reloc_roots);
19003fd0a558SYan, Zheng 	int found = 0;
1901aca1bba6SLiu Bo 	int ret = 0;
19023fd0a558SYan, Zheng again:
19033fd0a558SYan, Zheng 	root = rc->extent_root;
19047585717fSChris Mason 
19057585717fSChris Mason 	/*
19067585717fSChris Mason 	 * this serializes us with btrfs_record_root_in_transaction,
19077585717fSChris Mason 	 * we have to make sure nobody is in the middle of
19087585717fSChris Mason 	 * adding their roots to the list while we are
19097585717fSChris Mason 	 * doing this splice
19107585717fSChris Mason 	 */
19110b246afaSJeff Mahoney 	mutex_lock(&fs_info->reloc_mutex);
19123fd0a558SYan, Zheng 	list_splice_init(&rc->reloc_roots, &reloc_roots);
19130b246afaSJeff Mahoney 	mutex_unlock(&fs_info->reloc_mutex);
19145d4f98a2SYan Zheng 
19153fd0a558SYan, Zheng 	while (!list_empty(&reloc_roots)) {
19163fd0a558SYan, Zheng 		found = 1;
19173fd0a558SYan, Zheng 		reloc_root = list_entry(reloc_roots.next,
19183fd0a558SYan, Zheng 					struct btrfs_root, root_list);
19195d4f98a2SYan Zheng 
19205d4f98a2SYan Zheng 		if (btrfs_root_refs(&reloc_root->root_item) > 0) {
19210b246afaSJeff Mahoney 			root = read_fs_root(fs_info,
19225d4f98a2SYan Zheng 					    reloc_root->root_key.offset);
19235d4f98a2SYan Zheng 			BUG_ON(IS_ERR(root));
19245d4f98a2SYan Zheng 			BUG_ON(root->reloc_root != reloc_root);
19255d4f98a2SYan Zheng 
19263fd0a558SYan, Zheng 			ret = merge_reloc_root(rc, root);
192700246528SJosef Bacik 			btrfs_put_root(root);
1928b37b39cdSJosef Bacik 			if (ret) {
192925e293c2SWang Shilong 				if (list_empty(&reloc_root->root_list))
193025e293c2SWang Shilong 					list_add_tail(&reloc_root->root_list,
193125e293c2SWang Shilong 						      &reloc_roots);
1932aca1bba6SLiu Bo 				goto out;
1933b37b39cdSJosef Bacik 			}
19343fd0a558SYan, Zheng 		} else {
19353fd0a558SYan, Zheng 			list_del_init(&reloc_root->root_list);
193630d40577SQu Wenruo 			/* Don't forget to queue this reloc root for cleanup */
193730d40577SQu Wenruo 			list_add_tail(&reloc_root->reloc_dirty_list,
193830d40577SQu Wenruo 				      &rc->dirty_subvol_roots);
19393fd0a558SYan, Zheng 		}
19405d4f98a2SYan Zheng 	}
19415d4f98a2SYan Zheng 
19423fd0a558SYan, Zheng 	if (found) {
19433fd0a558SYan, Zheng 		found = 0;
19443fd0a558SYan, Zheng 		goto again;
19455d4f98a2SYan Zheng 	}
1946aca1bba6SLiu Bo out:
1947aca1bba6SLiu Bo 	if (ret) {
19480b246afaSJeff Mahoney 		btrfs_handle_fs_error(fs_info, ret, NULL);
1949aca1bba6SLiu Bo 		free_reloc_roots(&reloc_roots);
1950467bb1d2SWang Shilong 
1951467bb1d2SWang Shilong 		/* new reloc root may be added */
19520b246afaSJeff Mahoney 		mutex_lock(&fs_info->reloc_mutex);
1953467bb1d2SWang Shilong 		list_splice_init(&rc->reloc_roots, &reloc_roots);
19540b246afaSJeff Mahoney 		mutex_unlock(&fs_info->reloc_mutex);
1955467bb1d2SWang Shilong 		free_reloc_roots(&reloc_roots);
1956aca1bba6SLiu Bo 	}
1957aca1bba6SLiu Bo 
19587b7b7431SJosef Bacik 	/*
19597b7b7431SJosef Bacik 	 * We used to have
19607b7b7431SJosef Bacik 	 *
19617b7b7431SJosef Bacik 	 * BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
19627b7b7431SJosef Bacik 	 *
19637b7b7431SJosef Bacik 	 * here, but it's wrong.  If we fail to start the transaction in
19647b7b7431SJosef Bacik 	 * prepare_to_merge() we will have only 0 ref reloc roots, none of which
19657b7b7431SJosef Bacik 	 * have actually been removed from the reloc_root_tree rb tree.  This is
19667b7b7431SJosef Bacik 	 * fine because we're bailing here, and we hold a reference on the root
19677b7b7431SJosef Bacik 	 * for the list that holds it, so these roots will be cleaned up when we
19687b7b7431SJosef Bacik 	 * do the reloc_dirty_list afterwards.  Meanwhile the root->reloc_root
19697b7b7431SJosef Bacik 	 * will be cleaned up on unmount.
19707b7b7431SJosef Bacik 	 *
19717b7b7431SJosef Bacik 	 * The remaining nodes will be cleaned up by free_reloc_control.
19727b7b7431SJosef Bacik 	 */
19735d4f98a2SYan Zheng }
19745d4f98a2SYan Zheng 
19755d4f98a2SYan Zheng static void free_block_list(struct rb_root *blocks)
19765d4f98a2SYan Zheng {
19775d4f98a2SYan Zheng 	struct tree_block *block;
19785d4f98a2SYan Zheng 	struct rb_node *rb_node;
19795d4f98a2SYan Zheng 	while ((rb_node = rb_first(blocks))) {
19805d4f98a2SYan Zheng 		block = rb_entry(rb_node, struct tree_block, rb_node);
19815d4f98a2SYan Zheng 		rb_erase(rb_node, blocks);
19825d4f98a2SYan Zheng 		kfree(block);
19835d4f98a2SYan Zheng 	}
19845d4f98a2SYan Zheng }
19855d4f98a2SYan Zheng 
19865d4f98a2SYan Zheng static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans,
19875d4f98a2SYan Zheng 				      struct btrfs_root *reloc_root)
19885d4f98a2SYan Zheng {
19890b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = reloc_root->fs_info;
19905d4f98a2SYan Zheng 	struct btrfs_root *root;
1991442b1ac5SJosef Bacik 	int ret;
19925d4f98a2SYan Zheng 
19935d4f98a2SYan Zheng 	if (reloc_root->last_trans == trans->transid)
19945d4f98a2SYan Zheng 		return 0;
19955d4f98a2SYan Zheng 
19960b246afaSJeff Mahoney 	root = read_fs_root(fs_info, reloc_root->root_key.offset);
19975d4f98a2SYan Zheng 	BUG_ON(IS_ERR(root));
19985d4f98a2SYan Zheng 	BUG_ON(root->reloc_root != reloc_root);
1999442b1ac5SJosef Bacik 	ret = btrfs_record_root_in_trans(trans, root);
200000246528SJosef Bacik 	btrfs_put_root(root);
20015d4f98a2SYan Zheng 
2002442b1ac5SJosef Bacik 	return ret;
20035d4f98a2SYan Zheng }
20045d4f98a2SYan Zheng 
20053fd0a558SYan, Zheng static noinline_for_stack
20063fd0a558SYan, Zheng struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,
20073fd0a558SYan, Zheng 				     struct reloc_control *rc,
2008a26195a5SQu Wenruo 				     struct btrfs_backref_node *node,
2009a26195a5SQu Wenruo 				     struct btrfs_backref_edge *edges[])
20105d4f98a2SYan Zheng {
2011a26195a5SQu Wenruo 	struct btrfs_backref_node *next;
20125d4f98a2SYan Zheng 	struct btrfs_root *root;
20133fd0a558SYan, Zheng 	int index = 0;
20143fd0a558SYan, Zheng 
20155d4f98a2SYan Zheng 	next = node;
20165d4f98a2SYan Zheng 	while (1) {
20175d4f98a2SYan Zheng 		cond_resched();
20185d4f98a2SYan Zheng 		next = walk_up_backref(next, edges, &index);
20195d4f98a2SYan Zheng 		root = next->root;
20203fd0a558SYan, Zheng 		BUG_ON(!root);
202192a7cc42SQu Wenruo 		BUG_ON(!test_bit(BTRFS_ROOT_SHAREABLE, &root->state));
20225d4f98a2SYan Zheng 
20235d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
20245d4f98a2SYan Zheng 			record_reloc_root_in_trans(trans, root);
20255d4f98a2SYan Zheng 			break;
20265d4f98a2SYan Zheng 		}
20275d4f98a2SYan Zheng 
20285d4f98a2SYan Zheng 		btrfs_record_root_in_trans(trans, root);
20293fd0a558SYan, Zheng 		root = root->reloc_root;
20303fd0a558SYan, Zheng 
20313fd0a558SYan, Zheng 		if (next->new_bytenr != root->node->start) {
20323fd0a558SYan, Zheng 			BUG_ON(next->new_bytenr);
20333fd0a558SYan, Zheng 			BUG_ON(!list_empty(&next->list));
20343fd0a558SYan, Zheng 			next->new_bytenr = root->node->start;
203500246528SJosef Bacik 			btrfs_put_root(next->root);
203600246528SJosef Bacik 			next->root = btrfs_grab_root(root);
20370b530bc5SJosef Bacik 			ASSERT(next->root);
20383fd0a558SYan, Zheng 			list_add_tail(&next->list,
20393fd0a558SYan, Zheng 				      &rc->backref_cache.changed);
20409569cc20SQu Wenruo 			mark_block_processed(rc, next);
20415d4f98a2SYan Zheng 			break;
20425d4f98a2SYan Zheng 		}
20435d4f98a2SYan Zheng 
20443fd0a558SYan, Zheng 		WARN_ON(1);
20455d4f98a2SYan Zheng 		root = NULL;
20465d4f98a2SYan Zheng 		next = walk_down_backref(edges, &index);
20475d4f98a2SYan Zheng 		if (!next || next->level <= node->level)
20485d4f98a2SYan Zheng 			break;
20495d4f98a2SYan Zheng 	}
20503fd0a558SYan, Zheng 	if (!root)
20513fd0a558SYan, Zheng 		return NULL;
20525d4f98a2SYan Zheng 
20533fd0a558SYan, Zheng 	next = node;
20543fd0a558SYan, Zheng 	/* setup backref node path for btrfs_reloc_cow_block */
20553fd0a558SYan, Zheng 	while (1) {
20563fd0a558SYan, Zheng 		rc->backref_cache.path[next->level] = next;
20573fd0a558SYan, Zheng 		if (--index < 0)
20583fd0a558SYan, Zheng 			break;
20593fd0a558SYan, Zheng 		next = edges[index]->node[UPPER];
20603fd0a558SYan, Zheng 	}
20615d4f98a2SYan Zheng 	return root;
20625d4f98a2SYan Zheng }
20635d4f98a2SYan Zheng 
20643fd0a558SYan, Zheng /*
206592a7cc42SQu Wenruo  * Select a tree root for relocation.
206692a7cc42SQu Wenruo  *
206792a7cc42SQu Wenruo  * Return NULL if the block is not shareable. We should use do_relocation() in
206892a7cc42SQu Wenruo  * this case.
206992a7cc42SQu Wenruo  *
207092a7cc42SQu Wenruo  * Return a tree root pointer if the block is shareable.
207192a7cc42SQu Wenruo  * Return -ENOENT if the block is root of reloc tree.
20723fd0a558SYan, Zheng  */
20735d4f98a2SYan Zheng static noinline_for_stack
2074a26195a5SQu Wenruo struct btrfs_root *select_one_root(struct btrfs_backref_node *node)
20755d4f98a2SYan Zheng {
2076a26195a5SQu Wenruo 	struct btrfs_backref_node *next;
20773fd0a558SYan, Zheng 	struct btrfs_root *root;
20783fd0a558SYan, Zheng 	struct btrfs_root *fs_root = NULL;
2079a26195a5SQu Wenruo 	struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
20803fd0a558SYan, Zheng 	int index = 0;
20813fd0a558SYan, Zheng 
20823fd0a558SYan, Zheng 	next = node;
20833fd0a558SYan, Zheng 	while (1) {
20843fd0a558SYan, Zheng 		cond_resched();
20853fd0a558SYan, Zheng 		next = walk_up_backref(next, edges, &index);
20863fd0a558SYan, Zheng 		root = next->root;
20873fd0a558SYan, Zheng 		BUG_ON(!root);
20883fd0a558SYan, Zheng 
208992a7cc42SQu Wenruo 		/* No other choice for non-shareable tree */
209092a7cc42SQu Wenruo 		if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
20913fd0a558SYan, Zheng 			return root;
20923fd0a558SYan, Zheng 
20933fd0a558SYan, Zheng 		if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)
20943fd0a558SYan, Zheng 			fs_root = root;
20953fd0a558SYan, Zheng 
20963fd0a558SYan, Zheng 		if (next != node)
20973fd0a558SYan, Zheng 			return NULL;
20983fd0a558SYan, Zheng 
20993fd0a558SYan, Zheng 		next = walk_down_backref(edges, &index);
21003fd0a558SYan, Zheng 		if (!next || next->level <= node->level)
21013fd0a558SYan, Zheng 			break;
21023fd0a558SYan, Zheng 	}
21033fd0a558SYan, Zheng 
21043fd0a558SYan, Zheng 	if (!fs_root)
21053fd0a558SYan, Zheng 		return ERR_PTR(-ENOENT);
21063fd0a558SYan, Zheng 	return fs_root;
21075d4f98a2SYan Zheng }
21085d4f98a2SYan Zheng 
21095d4f98a2SYan Zheng static noinline_for_stack
21103fd0a558SYan, Zheng u64 calcu_metadata_size(struct reloc_control *rc,
2111a26195a5SQu Wenruo 			struct btrfs_backref_node *node, int reserve)
21125d4f98a2SYan Zheng {
21130b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2114a26195a5SQu Wenruo 	struct btrfs_backref_node *next = node;
2115a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
2116a26195a5SQu Wenruo 	struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
21173fd0a558SYan, Zheng 	u64 num_bytes = 0;
21183fd0a558SYan, Zheng 	int index = 0;
21195d4f98a2SYan Zheng 
21203fd0a558SYan, Zheng 	BUG_ON(reserve && node->processed);
21213fd0a558SYan, Zheng 
21223fd0a558SYan, Zheng 	while (next) {
21233fd0a558SYan, Zheng 		cond_resched();
21245d4f98a2SYan Zheng 		while (1) {
21253fd0a558SYan, Zheng 			if (next->processed && (reserve || next != node))
21265d4f98a2SYan Zheng 				break;
21275d4f98a2SYan Zheng 
21280b246afaSJeff Mahoney 			num_bytes += fs_info->nodesize;
21293fd0a558SYan, Zheng 
21303fd0a558SYan, Zheng 			if (list_empty(&next->upper))
21313fd0a558SYan, Zheng 				break;
21323fd0a558SYan, Zheng 
21333fd0a558SYan, Zheng 			edge = list_entry(next->upper.next,
2134a26195a5SQu Wenruo 					struct btrfs_backref_edge, list[LOWER]);
21353fd0a558SYan, Zheng 			edges[index++] = edge;
21363fd0a558SYan, Zheng 			next = edge->node[UPPER];
21375d4f98a2SYan Zheng 		}
21383fd0a558SYan, Zheng 		next = walk_down_backref(edges, &index);
21393fd0a558SYan, Zheng 	}
21403fd0a558SYan, Zheng 	return num_bytes;
21413fd0a558SYan, Zheng }
21423fd0a558SYan, Zheng 
21433fd0a558SYan, Zheng static int reserve_metadata_space(struct btrfs_trans_handle *trans,
21443fd0a558SYan, Zheng 				  struct reloc_control *rc,
2145a26195a5SQu Wenruo 				  struct btrfs_backref_node *node)
21463fd0a558SYan, Zheng {
21473fd0a558SYan, Zheng 	struct btrfs_root *root = rc->extent_root;
2148da17066cSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
21493fd0a558SYan, Zheng 	u64 num_bytes;
21503fd0a558SYan, Zheng 	int ret;
21510647bf56SWang Shilong 	u64 tmp;
21523fd0a558SYan, Zheng 
21533fd0a558SYan, Zheng 	num_bytes = calcu_metadata_size(rc, node, 1) * 2;
21543fd0a558SYan, Zheng 
21553fd0a558SYan, Zheng 	trans->block_rsv = rc->block_rsv;
21560647bf56SWang Shilong 	rc->reserved_bytes += num_bytes;
21578ca17f0fSJosef Bacik 
21588ca17f0fSJosef Bacik 	/*
21598ca17f0fSJosef Bacik 	 * We are under a transaction here so we can only do limited flushing.
21608ca17f0fSJosef Bacik 	 * If we get an enospc just kick back -EAGAIN so we know to drop the
21618ca17f0fSJosef Bacik 	 * transaction and try to refill when we can flush all the things.
21628ca17f0fSJosef Bacik 	 */
21630647bf56SWang Shilong 	ret = btrfs_block_rsv_refill(root, rc->block_rsv, num_bytes,
21648ca17f0fSJosef Bacik 				BTRFS_RESERVE_FLUSH_LIMIT);
21653fd0a558SYan, Zheng 	if (ret) {
2166da17066cSJeff Mahoney 		tmp = fs_info->nodesize * RELOCATION_RESERVED_NODES;
21670647bf56SWang Shilong 		while (tmp <= rc->reserved_bytes)
21680647bf56SWang Shilong 			tmp <<= 1;
21690647bf56SWang Shilong 		/*
21700647bf56SWang Shilong 		 * only one thread can access block_rsv at this point,
21710647bf56SWang Shilong 		 * so we don't need hold lock to protect block_rsv.
21720647bf56SWang Shilong 		 * we expand more reservation size here to allow enough
217352042d8eSAndrea Gelmini 		 * space for relocation and we will return earlier in
21740647bf56SWang Shilong 		 * enospc case.
21750647bf56SWang Shilong 		 */
2176da17066cSJeff Mahoney 		rc->block_rsv->size = tmp + fs_info->nodesize *
21770647bf56SWang Shilong 				      RELOCATION_RESERVED_NODES;
21788ca17f0fSJosef Bacik 		return -EAGAIN;
21793fd0a558SYan, Zheng 	}
21803fd0a558SYan, Zheng 
21813fd0a558SYan, Zheng 	return 0;
21823fd0a558SYan, Zheng }
21833fd0a558SYan, Zheng 
21845d4f98a2SYan Zheng /*
21855d4f98a2SYan Zheng  * relocate a block tree, and then update pointers in upper level
21865d4f98a2SYan Zheng  * blocks that reference the block to point to the new location.
21875d4f98a2SYan Zheng  *
21885d4f98a2SYan Zheng  * if called by link_to_upper, the block has already been relocated.
21895d4f98a2SYan Zheng  * in that case this function just updates pointers.
21905d4f98a2SYan Zheng  */
21915d4f98a2SYan Zheng static int do_relocation(struct btrfs_trans_handle *trans,
21923fd0a558SYan, Zheng 			 struct reloc_control *rc,
2193a26195a5SQu Wenruo 			 struct btrfs_backref_node *node,
21945d4f98a2SYan Zheng 			 struct btrfs_key *key,
21955d4f98a2SYan Zheng 			 struct btrfs_path *path, int lowest)
21965d4f98a2SYan Zheng {
21972ff7e61eSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2198a26195a5SQu Wenruo 	struct btrfs_backref_node *upper;
2199a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
2200a26195a5SQu Wenruo 	struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
22015d4f98a2SYan Zheng 	struct btrfs_root *root;
22025d4f98a2SYan Zheng 	struct extent_buffer *eb;
22035d4f98a2SYan Zheng 	u32 blocksize;
22045d4f98a2SYan Zheng 	u64 bytenr;
22055d4f98a2SYan Zheng 	u64 generation;
22065d4f98a2SYan Zheng 	int slot;
22075d4f98a2SYan Zheng 	int ret;
22085d4f98a2SYan Zheng 	int err = 0;
22095d4f98a2SYan Zheng 
22105d4f98a2SYan Zheng 	BUG_ON(lowest && node->eb);
22115d4f98a2SYan Zheng 
22125d4f98a2SYan Zheng 	path->lowest_level = node->level + 1;
22133fd0a558SYan, Zheng 	rc->backref_cache.path[node->level] = node;
22145d4f98a2SYan Zheng 	list_for_each_entry(edge, &node->upper, list[LOWER]) {
2215581c1760SQu Wenruo 		struct btrfs_key first_key;
221682fa113fSQu Wenruo 		struct btrfs_ref ref = { 0 };
2217581c1760SQu Wenruo 
22185d4f98a2SYan Zheng 		cond_resched();
22195d4f98a2SYan Zheng 
22205d4f98a2SYan Zheng 		upper = edge->node[UPPER];
2221dc4103f9SWang Shilong 		root = select_reloc_root(trans, rc, upper, edges);
22223fd0a558SYan, Zheng 		BUG_ON(!root);
22235d4f98a2SYan Zheng 
22243fd0a558SYan, Zheng 		if (upper->eb && !upper->locked) {
22253fd0a558SYan, Zheng 			if (!lowest) {
2226e3b83361SQu Wenruo 				ret = btrfs_bin_search(upper->eb, key, &slot);
2227cbca7d59SFilipe Manana 				if (ret < 0) {
2228cbca7d59SFilipe Manana 					err = ret;
2229cbca7d59SFilipe Manana 					goto next;
2230cbca7d59SFilipe Manana 				}
22313fd0a558SYan, Zheng 				BUG_ON(ret);
22323fd0a558SYan, Zheng 				bytenr = btrfs_node_blockptr(upper->eb, slot);
22333fd0a558SYan, Zheng 				if (node->eb->start == bytenr)
22343fd0a558SYan, Zheng 					goto next;
22353fd0a558SYan, Zheng 			}
2236b0fe7078SQu Wenruo 			btrfs_backref_drop_node_buffer(upper);
22373fd0a558SYan, Zheng 		}
22385d4f98a2SYan Zheng 
22395d4f98a2SYan Zheng 		if (!upper->eb) {
22405d4f98a2SYan Zheng 			ret = btrfs_search_slot(trans, root, key, path, 0, 1);
22413561b9dbSLiu Bo 			if (ret) {
22423561b9dbSLiu Bo 				if (ret < 0)
22435d4f98a2SYan Zheng 					err = ret;
22443561b9dbSLiu Bo 				else
22453561b9dbSLiu Bo 					err = -ENOENT;
22463561b9dbSLiu Bo 
22473561b9dbSLiu Bo 				btrfs_release_path(path);
22485d4f98a2SYan Zheng 				break;
22495d4f98a2SYan Zheng 			}
22505d4f98a2SYan Zheng 
22513fd0a558SYan, Zheng 			if (!upper->eb) {
22523fd0a558SYan, Zheng 				upper->eb = path->nodes[upper->level];
22533fd0a558SYan, Zheng 				path->nodes[upper->level] = NULL;
22543fd0a558SYan, Zheng 			} else {
22553fd0a558SYan, Zheng 				BUG_ON(upper->eb != path->nodes[upper->level]);
22563fd0a558SYan, Zheng 			}
22573fd0a558SYan, Zheng 
22583fd0a558SYan, Zheng 			upper->locked = 1;
22593fd0a558SYan, Zheng 			path->locks[upper->level] = 0;
22603fd0a558SYan, Zheng 
22615d4f98a2SYan Zheng 			slot = path->slots[upper->level];
2262b3b4aa74SDavid Sterba 			btrfs_release_path(path);
22635d4f98a2SYan Zheng 		} else {
2264e3b83361SQu Wenruo 			ret = btrfs_bin_search(upper->eb, key, &slot);
2265cbca7d59SFilipe Manana 			if (ret < 0) {
2266cbca7d59SFilipe Manana 				err = ret;
2267cbca7d59SFilipe Manana 				goto next;
2268cbca7d59SFilipe Manana 			}
22695d4f98a2SYan Zheng 			BUG_ON(ret);
22705d4f98a2SYan Zheng 		}
22715d4f98a2SYan Zheng 
22725d4f98a2SYan Zheng 		bytenr = btrfs_node_blockptr(upper->eb, slot);
22733fd0a558SYan, Zheng 		if (lowest) {
22744547f4d8SLiu Bo 			if (bytenr != node->bytenr) {
22754547f4d8SLiu Bo 				btrfs_err(root->fs_info,
22764547f4d8SLiu Bo 		"lowest leaf/node mismatch: bytenr %llu node->bytenr %llu slot %d upper %llu",
22774547f4d8SLiu Bo 					  bytenr, node->bytenr, slot,
22784547f4d8SLiu Bo 					  upper->eb->start);
22794547f4d8SLiu Bo 				err = -EIO;
22804547f4d8SLiu Bo 				goto next;
22814547f4d8SLiu Bo 			}
22825d4f98a2SYan Zheng 		} else {
22833fd0a558SYan, Zheng 			if (node->eb->start == bytenr)
22843fd0a558SYan, Zheng 				goto next;
22855d4f98a2SYan Zheng 		}
22865d4f98a2SYan Zheng 
2287da17066cSJeff Mahoney 		blocksize = root->fs_info->nodesize;
22885d4f98a2SYan Zheng 		generation = btrfs_node_ptr_generation(upper->eb, slot);
2289581c1760SQu Wenruo 		btrfs_node_key_to_cpu(upper->eb, &first_key, slot);
2290581c1760SQu Wenruo 		eb = read_tree_block(fs_info, bytenr, generation,
2291581c1760SQu Wenruo 				     upper->level - 1, &first_key);
229264c043deSLiu Bo 		if (IS_ERR(eb)) {
229364c043deSLiu Bo 			err = PTR_ERR(eb);
229464c043deSLiu Bo 			goto next;
229564c043deSLiu Bo 		} else if (!extent_buffer_uptodate(eb)) {
2296416bc658SJosef Bacik 			free_extent_buffer(eb);
229797d9a8a4STsutomu Itoh 			err = -EIO;
229897d9a8a4STsutomu Itoh 			goto next;
229997d9a8a4STsutomu Itoh 		}
23005d4f98a2SYan Zheng 		btrfs_tree_lock(eb);
23018bead258SDavid Sterba 		btrfs_set_lock_blocking_write(eb);
23025d4f98a2SYan Zheng 
23035d4f98a2SYan Zheng 		if (!node->eb) {
23045d4f98a2SYan Zheng 			ret = btrfs_cow_block(trans, root, eb, upper->eb,
23055d4f98a2SYan Zheng 					      slot, &eb);
23063fd0a558SYan, Zheng 			btrfs_tree_unlock(eb);
23073fd0a558SYan, Zheng 			free_extent_buffer(eb);
23085d4f98a2SYan Zheng 			if (ret < 0) {
23095d4f98a2SYan Zheng 				err = ret;
23103fd0a558SYan, Zheng 				goto next;
23115d4f98a2SYan Zheng 			}
23123fd0a558SYan, Zheng 			BUG_ON(node->eb != eb);
23135d4f98a2SYan Zheng 		} else {
23145d4f98a2SYan Zheng 			btrfs_set_node_blockptr(upper->eb, slot,
23155d4f98a2SYan Zheng 						node->eb->start);
23165d4f98a2SYan Zheng 			btrfs_set_node_ptr_generation(upper->eb, slot,
23175d4f98a2SYan Zheng 						      trans->transid);
23185d4f98a2SYan Zheng 			btrfs_mark_buffer_dirty(upper->eb);
23195d4f98a2SYan Zheng 
232082fa113fSQu Wenruo 			btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF,
23215d4f98a2SYan Zheng 					       node->eb->start, blocksize,
232282fa113fSQu Wenruo 					       upper->eb->start);
232382fa113fSQu Wenruo 			ref.real_root = root->root_key.objectid;
232482fa113fSQu Wenruo 			btrfs_init_tree_ref(&ref, node->level,
232582fa113fSQu Wenruo 					    btrfs_header_owner(upper->eb));
232682fa113fSQu Wenruo 			ret = btrfs_inc_extent_ref(trans, &ref);
23275d4f98a2SYan Zheng 			BUG_ON(ret);
23285d4f98a2SYan Zheng 
23295d4f98a2SYan Zheng 			ret = btrfs_drop_subtree(trans, root, eb, upper->eb);
23305d4f98a2SYan Zheng 			BUG_ON(ret);
23315d4f98a2SYan Zheng 		}
23323fd0a558SYan, Zheng next:
23333fd0a558SYan, Zheng 		if (!upper->pending)
2334b0fe7078SQu Wenruo 			btrfs_backref_drop_node_buffer(upper);
23353fd0a558SYan, Zheng 		else
2336b0fe7078SQu Wenruo 			btrfs_backref_unlock_node_buffer(upper);
23373fd0a558SYan, Zheng 		if (err)
23383fd0a558SYan, Zheng 			break;
23395d4f98a2SYan Zheng 	}
23403fd0a558SYan, Zheng 
23413fd0a558SYan, Zheng 	if (!err && node->pending) {
2342b0fe7078SQu Wenruo 		btrfs_backref_drop_node_buffer(node);
23433fd0a558SYan, Zheng 		list_move_tail(&node->list, &rc->backref_cache.changed);
23443fd0a558SYan, Zheng 		node->pending = 0;
23455d4f98a2SYan Zheng 	}
23463fd0a558SYan, Zheng 
23475d4f98a2SYan Zheng 	path->lowest_level = 0;
23483fd0a558SYan, Zheng 	BUG_ON(err == -ENOSPC);
23495d4f98a2SYan Zheng 	return err;
23505d4f98a2SYan Zheng }
23515d4f98a2SYan Zheng 
23525d4f98a2SYan Zheng static int link_to_upper(struct btrfs_trans_handle *trans,
23533fd0a558SYan, Zheng 			 struct reloc_control *rc,
2354a26195a5SQu Wenruo 			 struct btrfs_backref_node *node,
23555d4f98a2SYan Zheng 			 struct btrfs_path *path)
23565d4f98a2SYan Zheng {
23575d4f98a2SYan Zheng 	struct btrfs_key key;
23585d4f98a2SYan Zheng 
23595d4f98a2SYan Zheng 	btrfs_node_key_to_cpu(node->eb, &key, 0);
23603fd0a558SYan, Zheng 	return do_relocation(trans, rc, node, &key, path, 0);
23615d4f98a2SYan Zheng }
23625d4f98a2SYan Zheng 
23635d4f98a2SYan Zheng static int finish_pending_nodes(struct btrfs_trans_handle *trans,
23643fd0a558SYan, Zheng 				struct reloc_control *rc,
23653fd0a558SYan, Zheng 				struct btrfs_path *path, int err)
23665d4f98a2SYan Zheng {
23673fd0a558SYan, Zheng 	LIST_HEAD(list);
2368a26195a5SQu Wenruo 	struct btrfs_backref_cache *cache = &rc->backref_cache;
2369a26195a5SQu Wenruo 	struct btrfs_backref_node *node;
23705d4f98a2SYan Zheng 	int level;
23715d4f98a2SYan Zheng 	int ret;
23725d4f98a2SYan Zheng 
23735d4f98a2SYan Zheng 	for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
23745d4f98a2SYan Zheng 		while (!list_empty(&cache->pending[level])) {
23755d4f98a2SYan Zheng 			node = list_entry(cache->pending[level].next,
2376a26195a5SQu Wenruo 					  struct btrfs_backref_node, list);
23773fd0a558SYan, Zheng 			list_move_tail(&node->list, &list);
23783fd0a558SYan, Zheng 			BUG_ON(!node->pending);
23795d4f98a2SYan Zheng 
23803fd0a558SYan, Zheng 			if (!err) {
23813fd0a558SYan, Zheng 				ret = link_to_upper(trans, rc, node, path);
23825d4f98a2SYan Zheng 				if (ret < 0)
23835d4f98a2SYan Zheng 					err = ret;
23845d4f98a2SYan Zheng 			}
23855d4f98a2SYan Zheng 		}
23863fd0a558SYan, Zheng 		list_splice_init(&list, &cache->pending[level]);
23873fd0a558SYan, Zheng 	}
23885d4f98a2SYan Zheng 	return err;
23895d4f98a2SYan Zheng }
23905d4f98a2SYan Zheng 
23915d4f98a2SYan Zheng /*
23925d4f98a2SYan Zheng  * mark a block and all blocks directly/indirectly reference the block
23935d4f98a2SYan Zheng  * as processed.
23945d4f98a2SYan Zheng  */
23955d4f98a2SYan Zheng static void update_processed_blocks(struct reloc_control *rc,
2396a26195a5SQu Wenruo 				    struct btrfs_backref_node *node)
23975d4f98a2SYan Zheng {
2398a26195a5SQu Wenruo 	struct btrfs_backref_node *next = node;
2399a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
2400a26195a5SQu Wenruo 	struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
24015d4f98a2SYan Zheng 	int index = 0;
24025d4f98a2SYan Zheng 
24035d4f98a2SYan Zheng 	while (next) {
24045d4f98a2SYan Zheng 		cond_resched();
24055d4f98a2SYan Zheng 		while (1) {
24065d4f98a2SYan Zheng 			if (next->processed)
24075d4f98a2SYan Zheng 				break;
24085d4f98a2SYan Zheng 
24099569cc20SQu Wenruo 			mark_block_processed(rc, next);
24105d4f98a2SYan Zheng 
24115d4f98a2SYan Zheng 			if (list_empty(&next->upper))
24125d4f98a2SYan Zheng 				break;
24135d4f98a2SYan Zheng 
24145d4f98a2SYan Zheng 			edge = list_entry(next->upper.next,
2415a26195a5SQu Wenruo 					struct btrfs_backref_edge, list[LOWER]);
24165d4f98a2SYan Zheng 			edges[index++] = edge;
24175d4f98a2SYan Zheng 			next = edge->node[UPPER];
24185d4f98a2SYan Zheng 		}
24195d4f98a2SYan Zheng 		next = walk_down_backref(edges, &index);
24205d4f98a2SYan Zheng 	}
24215d4f98a2SYan Zheng }
24225d4f98a2SYan Zheng 
24237476dfdaSDavid Sterba static int tree_block_processed(u64 bytenr, struct reloc_control *rc)
24245d4f98a2SYan Zheng {
2425da17066cSJeff Mahoney 	u32 blocksize = rc->extent_root->fs_info->nodesize;
24267476dfdaSDavid Sterba 
24275d4f98a2SYan Zheng 	if (test_range_bit(&rc->processed_blocks, bytenr,
24289655d298SChris Mason 			   bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL))
24295d4f98a2SYan Zheng 		return 1;
24305d4f98a2SYan Zheng 	return 0;
24315d4f98a2SYan Zheng }
24325d4f98a2SYan Zheng 
24332ff7e61eSJeff Mahoney static int get_tree_block_key(struct btrfs_fs_info *fs_info,
24345d4f98a2SYan Zheng 			      struct tree_block *block)
24355d4f98a2SYan Zheng {
24365d4f98a2SYan Zheng 	struct extent_buffer *eb;
24375d4f98a2SYan Zheng 
2438581c1760SQu Wenruo 	eb = read_tree_block(fs_info, block->bytenr, block->key.offset,
2439581c1760SQu Wenruo 			     block->level, NULL);
244064c043deSLiu Bo 	if (IS_ERR(eb)) {
244164c043deSLiu Bo 		return PTR_ERR(eb);
244264c043deSLiu Bo 	} else if (!extent_buffer_uptodate(eb)) {
2443416bc658SJosef Bacik 		free_extent_buffer(eb);
2444416bc658SJosef Bacik 		return -EIO;
2445416bc658SJosef Bacik 	}
24465d4f98a2SYan Zheng 	if (block->level == 0)
24475d4f98a2SYan Zheng 		btrfs_item_key_to_cpu(eb, &block->key, 0);
24485d4f98a2SYan Zheng 	else
24495d4f98a2SYan Zheng 		btrfs_node_key_to_cpu(eb, &block->key, 0);
24505d4f98a2SYan Zheng 	free_extent_buffer(eb);
24515d4f98a2SYan Zheng 	block->key_ready = 1;
24525d4f98a2SYan Zheng 	return 0;
24535d4f98a2SYan Zheng }
24545d4f98a2SYan Zheng 
24555d4f98a2SYan Zheng /*
24565d4f98a2SYan Zheng  * helper function to relocate a tree block
24575d4f98a2SYan Zheng  */
24585d4f98a2SYan Zheng static int relocate_tree_block(struct btrfs_trans_handle *trans,
24595d4f98a2SYan Zheng 				struct reloc_control *rc,
2460a26195a5SQu Wenruo 				struct btrfs_backref_node *node,
24615d4f98a2SYan Zheng 				struct btrfs_key *key,
24625d4f98a2SYan Zheng 				struct btrfs_path *path)
24635d4f98a2SYan Zheng {
24645d4f98a2SYan Zheng 	struct btrfs_root *root;
24653fd0a558SYan, Zheng 	int ret = 0;
24665d4f98a2SYan Zheng 
24673fd0a558SYan, Zheng 	if (!node)
24685d4f98a2SYan Zheng 		return 0;
24693fd0a558SYan, Zheng 
24705f6b2e5cSJosef Bacik 	/*
24715f6b2e5cSJosef Bacik 	 * If we fail here we want to drop our backref_node because we are going
24725f6b2e5cSJosef Bacik 	 * to start over and regenerate the tree for it.
24735f6b2e5cSJosef Bacik 	 */
24745f6b2e5cSJosef Bacik 	ret = reserve_metadata_space(trans, rc, node);
24755f6b2e5cSJosef Bacik 	if (ret)
24765f6b2e5cSJosef Bacik 		goto out;
24775f6b2e5cSJosef Bacik 
24783fd0a558SYan, Zheng 	BUG_ON(node->processed);
2479147d256eSZhaolei 	root = select_one_root(node);
24803fd0a558SYan, Zheng 	if (root == ERR_PTR(-ENOENT)) {
24813fd0a558SYan, Zheng 		update_processed_blocks(rc, node);
24823fd0a558SYan, Zheng 		goto out;
24835d4f98a2SYan Zheng 	}
24845d4f98a2SYan Zheng 
24853fd0a558SYan, Zheng 	if (root) {
248692a7cc42SQu Wenruo 		if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
24873fd0a558SYan, Zheng 			BUG_ON(node->new_bytenr);
24883fd0a558SYan, Zheng 			BUG_ON(!list_empty(&node->list));
24893fd0a558SYan, Zheng 			btrfs_record_root_in_trans(trans, root);
24903fd0a558SYan, Zheng 			root = root->reloc_root;
24913fd0a558SYan, Zheng 			node->new_bytenr = root->node->start;
249200246528SJosef Bacik 			btrfs_put_root(node->root);
249300246528SJosef Bacik 			node->root = btrfs_grab_root(root);
24940b530bc5SJosef Bacik 			ASSERT(node->root);
24953fd0a558SYan, Zheng 			list_add_tail(&node->list, &rc->backref_cache.changed);
24963fd0a558SYan, Zheng 		} else {
24975d4f98a2SYan Zheng 			path->lowest_level = node->level;
24985d4f98a2SYan Zheng 			ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2499b3b4aa74SDavid Sterba 			btrfs_release_path(path);
25003fd0a558SYan, Zheng 			if (ret > 0)
25015d4f98a2SYan Zheng 				ret = 0;
25023fd0a558SYan, Zheng 		}
25033fd0a558SYan, Zheng 		if (!ret)
25043fd0a558SYan, Zheng 			update_processed_blocks(rc, node);
25053fd0a558SYan, Zheng 	} else {
25063fd0a558SYan, Zheng 		ret = do_relocation(trans, rc, node, key, path, 1);
25073fd0a558SYan, Zheng 	}
25085d4f98a2SYan Zheng out:
25090647bf56SWang Shilong 	if (ret || node->level == 0 || node->cowonly)
2510023acb07SQu Wenruo 		btrfs_backref_cleanup_node(&rc->backref_cache, node);
25115d4f98a2SYan Zheng 	return ret;
25125d4f98a2SYan Zheng }
25135d4f98a2SYan Zheng 
25145d4f98a2SYan Zheng /*
25155d4f98a2SYan Zheng  * relocate a list of blocks
25165d4f98a2SYan Zheng  */
25175d4f98a2SYan Zheng static noinline_for_stack
25185d4f98a2SYan Zheng int relocate_tree_blocks(struct btrfs_trans_handle *trans,
25195d4f98a2SYan Zheng 			 struct reloc_control *rc, struct rb_root *blocks)
25205d4f98a2SYan Zheng {
25212ff7e61eSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2522a26195a5SQu Wenruo 	struct btrfs_backref_node *node;
25235d4f98a2SYan Zheng 	struct btrfs_path *path;
25245d4f98a2SYan Zheng 	struct tree_block *block;
252598ff7b94SQu Wenruo 	struct tree_block *next;
25265d4f98a2SYan Zheng 	int ret;
25275d4f98a2SYan Zheng 	int err = 0;
25285d4f98a2SYan Zheng 
25295d4f98a2SYan Zheng 	path = btrfs_alloc_path();
2530e1a12670SLiu Bo 	if (!path) {
2531e1a12670SLiu Bo 		err = -ENOMEM;
253234c2b290SDavid Sterba 		goto out_free_blocks;
2533e1a12670SLiu Bo 	}
25345d4f98a2SYan Zheng 
253598ff7b94SQu Wenruo 	/* Kick in readahead for tree blocks with missing keys */
253698ff7b94SQu Wenruo 	rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
25375d4f98a2SYan Zheng 		if (!block->key_ready)
25382ff7e61eSJeff Mahoney 			readahead_tree_block(fs_info, block->bytenr);
25395d4f98a2SYan Zheng 	}
25405d4f98a2SYan Zheng 
254198ff7b94SQu Wenruo 	/* Get first keys */
254298ff7b94SQu Wenruo 	rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
254334c2b290SDavid Sterba 		if (!block->key_ready) {
25442ff7e61eSJeff Mahoney 			err = get_tree_block_key(fs_info, block);
254534c2b290SDavid Sterba 			if (err)
254634c2b290SDavid Sterba 				goto out_free_path;
254734c2b290SDavid Sterba 		}
25485d4f98a2SYan Zheng 	}
25495d4f98a2SYan Zheng 
255098ff7b94SQu Wenruo 	/* Do tree relocation */
255198ff7b94SQu Wenruo 	rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
25523fd0a558SYan, Zheng 		node = build_backref_tree(rc, &block->key,
25535d4f98a2SYan Zheng 					  block->level, block->bytenr);
25545d4f98a2SYan Zheng 		if (IS_ERR(node)) {
25555d4f98a2SYan Zheng 			err = PTR_ERR(node);
25565d4f98a2SYan Zheng 			goto out;
25575d4f98a2SYan Zheng 		}
25585d4f98a2SYan Zheng 
25595d4f98a2SYan Zheng 		ret = relocate_tree_block(trans, rc, node, &block->key,
25605d4f98a2SYan Zheng 					  path);
25615d4f98a2SYan Zheng 		if (ret < 0) {
25625d4f98a2SYan Zheng 			err = ret;
256350dbbb71SJosef Bacik 			break;
25645d4f98a2SYan Zheng 		}
25655d4f98a2SYan Zheng 	}
25665d4f98a2SYan Zheng out:
25673fd0a558SYan, Zheng 	err = finish_pending_nodes(trans, rc, path, err);
25685d4f98a2SYan Zheng 
256934c2b290SDavid Sterba out_free_path:
25705d4f98a2SYan Zheng 	btrfs_free_path(path);
257134c2b290SDavid Sterba out_free_blocks:
2572e1a12670SLiu Bo 	free_block_list(blocks);
25735d4f98a2SYan Zheng 	return err;
25745d4f98a2SYan Zheng }
25755d4f98a2SYan Zheng 
25765d4f98a2SYan Zheng static noinline_for_stack
2577efa56464SYan, Zheng int prealloc_file_extent_cluster(struct inode *inode,
2578efa56464SYan, Zheng 				 struct file_extent_cluster *cluster)
2579efa56464SYan, Zheng {
2580efa56464SYan, Zheng 	u64 alloc_hint = 0;
2581efa56464SYan, Zheng 	u64 start;
2582efa56464SYan, Zheng 	u64 end;
2583efa56464SYan, Zheng 	u64 offset = BTRFS_I(inode)->index_cnt;
2584efa56464SYan, Zheng 	u64 num_bytes;
2585efa56464SYan, Zheng 	int nr = 0;
2586efa56464SYan, Zheng 	int ret = 0;
2587dcb40c19SWang Xiaoguang 	u64 prealloc_start = cluster->start - offset;
2588dcb40c19SWang Xiaoguang 	u64 prealloc_end = cluster->end - offset;
258918513091SWang Xiaoguang 	u64 cur_offset;
2590364ecf36SQu Wenruo 	struct extent_changeset *data_reserved = NULL;
2591efa56464SYan, Zheng 
2592efa56464SYan, Zheng 	BUG_ON(cluster->start != cluster->boundary[0]);
25935955102cSAl Viro 	inode_lock(inode);
2594efa56464SYan, Zheng 
2595364ecf36SQu Wenruo 	ret = btrfs_check_data_free_space(inode, &data_reserved, prealloc_start,
2596dcb40c19SWang Xiaoguang 					  prealloc_end + 1 - prealloc_start);
2597efa56464SYan, Zheng 	if (ret)
2598efa56464SYan, Zheng 		goto out;
2599efa56464SYan, Zheng 
260018513091SWang Xiaoguang 	cur_offset = prealloc_start;
2601efa56464SYan, Zheng 	while (nr < cluster->nr) {
2602efa56464SYan, Zheng 		start = cluster->boundary[nr] - offset;
2603efa56464SYan, Zheng 		if (nr + 1 < cluster->nr)
2604efa56464SYan, Zheng 			end = cluster->boundary[nr + 1] - 1 - offset;
2605efa56464SYan, Zheng 		else
2606efa56464SYan, Zheng 			end = cluster->end - offset;
2607efa56464SYan, Zheng 
2608d0082371SJeff Mahoney 		lock_extent(&BTRFS_I(inode)->io_tree, start, end);
2609efa56464SYan, Zheng 		num_bytes = end + 1 - start;
261018513091SWang Xiaoguang 		if (cur_offset < start)
2611bc42bda2SQu Wenruo 			btrfs_free_reserved_data_space(inode, data_reserved,
2612bc42bda2SQu Wenruo 					cur_offset, start - cur_offset);
2613efa56464SYan, Zheng 		ret = btrfs_prealloc_file_range(inode, 0, start,
2614efa56464SYan, Zheng 						num_bytes, num_bytes,
2615efa56464SYan, Zheng 						end + 1, &alloc_hint);
261618513091SWang Xiaoguang 		cur_offset = end + 1;
2617d0082371SJeff Mahoney 		unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
2618efa56464SYan, Zheng 		if (ret)
2619efa56464SYan, Zheng 			break;
2620efa56464SYan, Zheng 		nr++;
2621efa56464SYan, Zheng 	}
262218513091SWang Xiaoguang 	if (cur_offset < prealloc_end)
2623bc42bda2SQu Wenruo 		btrfs_free_reserved_data_space(inode, data_reserved,
2624bc42bda2SQu Wenruo 				cur_offset, prealloc_end + 1 - cur_offset);
2625efa56464SYan, Zheng out:
26265955102cSAl Viro 	inode_unlock(inode);
2627364ecf36SQu Wenruo 	extent_changeset_free(data_reserved);
2628efa56464SYan, Zheng 	return ret;
2629efa56464SYan, Zheng }
2630efa56464SYan, Zheng 
2631efa56464SYan, Zheng static noinline_for_stack
26320257bb82SYan, Zheng int setup_extent_mapping(struct inode *inode, u64 start, u64 end,
26330257bb82SYan, Zheng 			 u64 block_start)
26345d4f98a2SYan Zheng {
26355d4f98a2SYan Zheng 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
26365d4f98a2SYan Zheng 	struct extent_map *em;
26370257bb82SYan, Zheng 	int ret = 0;
26385d4f98a2SYan Zheng 
2639172ddd60SDavid Sterba 	em = alloc_extent_map();
26400257bb82SYan, Zheng 	if (!em)
26410257bb82SYan, Zheng 		return -ENOMEM;
26420257bb82SYan, Zheng 
26435d4f98a2SYan Zheng 	em->start = start;
26440257bb82SYan, Zheng 	em->len = end + 1 - start;
26450257bb82SYan, Zheng 	em->block_len = em->len;
26460257bb82SYan, Zheng 	em->block_start = block_start;
26475d4f98a2SYan Zheng 	set_bit(EXTENT_FLAG_PINNED, &em->flags);
26485d4f98a2SYan Zheng 
2649d0082371SJeff Mahoney 	lock_extent(&BTRFS_I(inode)->io_tree, start, end);
26505d4f98a2SYan Zheng 	while (1) {
2651890871beSChris Mason 		write_lock(&em_tree->lock);
265209a2a8f9SJosef Bacik 		ret = add_extent_mapping(em_tree, em, 0);
2653890871beSChris Mason 		write_unlock(&em_tree->lock);
26545d4f98a2SYan Zheng 		if (ret != -EEXIST) {
26555d4f98a2SYan Zheng 			free_extent_map(em);
26565d4f98a2SYan Zheng 			break;
26575d4f98a2SYan Zheng 		}
2658dcdbc059SNikolay Borisov 		btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
26595d4f98a2SYan Zheng 	}
2660d0082371SJeff Mahoney 	unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
26610257bb82SYan, Zheng 	return ret;
26620257bb82SYan, Zheng }
26635d4f98a2SYan Zheng 
2664726a3421SQu Wenruo /*
2665726a3421SQu Wenruo  * Allow error injection to test balance cancellation
2666726a3421SQu Wenruo  */
2667726a3421SQu Wenruo int btrfs_should_cancel_balance(struct btrfs_fs_info *fs_info)
2668726a3421SQu Wenruo {
2669726a3421SQu Wenruo 	return atomic_read(&fs_info->balance_cancel_req);
2670726a3421SQu Wenruo }
2671726a3421SQu Wenruo ALLOW_ERROR_INJECTION(btrfs_should_cancel_balance, TRUE);
2672726a3421SQu Wenruo 
26730257bb82SYan, Zheng static int relocate_file_extent_cluster(struct inode *inode,
26740257bb82SYan, Zheng 					struct file_extent_cluster *cluster)
26750257bb82SYan, Zheng {
26762ff7e61eSJeff Mahoney 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
26770257bb82SYan, Zheng 	u64 page_start;
26780257bb82SYan, Zheng 	u64 page_end;
26790257bb82SYan, Zheng 	u64 offset = BTRFS_I(inode)->index_cnt;
26800257bb82SYan, Zheng 	unsigned long index;
26810257bb82SYan, Zheng 	unsigned long last_index;
26820257bb82SYan, Zheng 	struct page *page;
26830257bb82SYan, Zheng 	struct file_ra_state *ra;
26843b16a4e3SJosef Bacik 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
26850257bb82SYan, Zheng 	int nr = 0;
26860257bb82SYan, Zheng 	int ret = 0;
26870257bb82SYan, Zheng 
26880257bb82SYan, Zheng 	if (!cluster->nr)
26890257bb82SYan, Zheng 		return 0;
26900257bb82SYan, Zheng 
26910257bb82SYan, Zheng 	ra = kzalloc(sizeof(*ra), GFP_NOFS);
26920257bb82SYan, Zheng 	if (!ra)
26930257bb82SYan, Zheng 		return -ENOMEM;
26940257bb82SYan, Zheng 
2695efa56464SYan, Zheng 	ret = prealloc_file_extent_cluster(inode, cluster);
26960257bb82SYan, Zheng 	if (ret)
2697efa56464SYan, Zheng 		goto out;
26980257bb82SYan, Zheng 
26990257bb82SYan, Zheng 	file_ra_state_init(ra, inode->i_mapping);
27000257bb82SYan, Zheng 
2701efa56464SYan, Zheng 	ret = setup_extent_mapping(inode, cluster->start - offset,
2702efa56464SYan, Zheng 				   cluster->end - offset, cluster->start);
2703efa56464SYan, Zheng 	if (ret)
2704efa56464SYan, Zheng 		goto out;
2705efa56464SYan, Zheng 
270609cbfeafSKirill A. Shutemov 	index = (cluster->start - offset) >> PAGE_SHIFT;
270709cbfeafSKirill A. Shutemov 	last_index = (cluster->end - offset) >> PAGE_SHIFT;
27080257bb82SYan, Zheng 	while (index <= last_index) {
27099f3db423SNikolay Borisov 		ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode),
27109f3db423SNikolay Borisov 				PAGE_SIZE);
2711efa56464SYan, Zheng 		if (ret)
2712efa56464SYan, Zheng 			goto out;
2713efa56464SYan, Zheng 
27140257bb82SYan, Zheng 		page = find_lock_page(inode->i_mapping, index);
27150257bb82SYan, Zheng 		if (!page) {
27160257bb82SYan, Zheng 			page_cache_sync_readahead(inode->i_mapping,
27170257bb82SYan, Zheng 						  ra, NULL, index,
27180257bb82SYan, Zheng 						  last_index + 1 - index);
2719a94733d0SJosef Bacik 			page = find_or_create_page(inode->i_mapping, index,
27203b16a4e3SJosef Bacik 						   mask);
27210257bb82SYan, Zheng 			if (!page) {
2722691fa059SNikolay Borisov 				btrfs_delalloc_release_metadata(BTRFS_I(inode),
272343b18595SQu Wenruo 							PAGE_SIZE, true);
272444db1216SFilipe Manana 				btrfs_delalloc_release_extents(BTRFS_I(inode),
27258702ba93SQu Wenruo 							PAGE_SIZE);
27260257bb82SYan, Zheng 				ret = -ENOMEM;
2727efa56464SYan, Zheng 				goto out;
27280257bb82SYan, Zheng 			}
27290257bb82SYan, Zheng 		}
27300257bb82SYan, Zheng 
27310257bb82SYan, Zheng 		if (PageReadahead(page)) {
27320257bb82SYan, Zheng 			page_cache_async_readahead(inode->i_mapping,
27330257bb82SYan, Zheng 						   ra, NULL, page, index,
27340257bb82SYan, Zheng 						   last_index + 1 - index);
27350257bb82SYan, Zheng 		}
27360257bb82SYan, Zheng 
27370257bb82SYan, Zheng 		if (!PageUptodate(page)) {
27380257bb82SYan, Zheng 			btrfs_readpage(NULL, page);
27390257bb82SYan, Zheng 			lock_page(page);
27400257bb82SYan, Zheng 			if (!PageUptodate(page)) {
27410257bb82SYan, Zheng 				unlock_page(page);
274209cbfeafSKirill A. Shutemov 				put_page(page);
2743691fa059SNikolay Borisov 				btrfs_delalloc_release_metadata(BTRFS_I(inode),
274443b18595SQu Wenruo 							PAGE_SIZE, true);
27458b62f87bSJosef Bacik 				btrfs_delalloc_release_extents(BTRFS_I(inode),
27468702ba93SQu Wenruo 							       PAGE_SIZE);
27470257bb82SYan, Zheng 				ret = -EIO;
2748efa56464SYan, Zheng 				goto out;
27490257bb82SYan, Zheng 			}
27500257bb82SYan, Zheng 		}
27510257bb82SYan, Zheng 
27524eee4fa4SMiao Xie 		page_start = page_offset(page);
275309cbfeafSKirill A. Shutemov 		page_end = page_start + PAGE_SIZE - 1;
27540257bb82SYan, Zheng 
2755d0082371SJeff Mahoney 		lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end);
27560257bb82SYan, Zheng 
27570257bb82SYan, Zheng 		set_page_extent_mapped(page);
27580257bb82SYan, Zheng 
27590257bb82SYan, Zheng 		if (nr < cluster->nr &&
27600257bb82SYan, Zheng 		    page_start + offset == cluster->boundary[nr]) {
27610257bb82SYan, Zheng 			set_extent_bits(&BTRFS_I(inode)->io_tree,
27620257bb82SYan, Zheng 					page_start, page_end,
2763ceeb0ae7SDavid Sterba 					EXTENT_BOUNDARY);
27640257bb82SYan, Zheng 			nr++;
27650257bb82SYan, Zheng 		}
27660257bb82SYan, Zheng 
2767765f3cebSNikolay Borisov 		ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2768330a5827SNikolay Borisov 						NULL);
2769765f3cebSNikolay Borisov 		if (ret) {
2770765f3cebSNikolay Borisov 			unlock_page(page);
2771765f3cebSNikolay Borisov 			put_page(page);
2772765f3cebSNikolay Borisov 			btrfs_delalloc_release_metadata(BTRFS_I(inode),
277343b18595SQu Wenruo 							 PAGE_SIZE, true);
2774765f3cebSNikolay Borisov 			btrfs_delalloc_release_extents(BTRFS_I(inode),
27758702ba93SQu Wenruo 			                               PAGE_SIZE);
2776765f3cebSNikolay Borisov 
2777765f3cebSNikolay Borisov 			clear_extent_bits(&BTRFS_I(inode)->io_tree,
2778765f3cebSNikolay Borisov 					  page_start, page_end,
2779765f3cebSNikolay Borisov 					  EXTENT_LOCKED | EXTENT_BOUNDARY);
2780765f3cebSNikolay Borisov 			goto out;
2781765f3cebSNikolay Borisov 
2782765f3cebSNikolay Borisov 		}
27830257bb82SYan, Zheng 		set_page_dirty(page);
27840257bb82SYan, Zheng 
27850257bb82SYan, Zheng 		unlock_extent(&BTRFS_I(inode)->io_tree,
2786d0082371SJeff Mahoney 			      page_start, page_end);
27870257bb82SYan, Zheng 		unlock_page(page);
278809cbfeafSKirill A. Shutemov 		put_page(page);
27890257bb82SYan, Zheng 
27900257bb82SYan, Zheng 		index++;
27918702ba93SQu Wenruo 		btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
2792efa56464SYan, Zheng 		balance_dirty_pages_ratelimited(inode->i_mapping);
27932ff7e61eSJeff Mahoney 		btrfs_throttle(fs_info);
27947f913c7cSQu Wenruo 		if (btrfs_should_cancel_balance(fs_info)) {
27957f913c7cSQu Wenruo 			ret = -ECANCELED;
27967f913c7cSQu Wenruo 			goto out;
27977f913c7cSQu Wenruo 		}
27980257bb82SYan, Zheng 	}
27990257bb82SYan, Zheng 	WARN_ON(nr != cluster->nr);
2800efa56464SYan, Zheng out:
28010257bb82SYan, Zheng 	kfree(ra);
28020257bb82SYan, Zheng 	return ret;
28030257bb82SYan, Zheng }
28040257bb82SYan, Zheng 
28050257bb82SYan, Zheng static noinline_for_stack
28060257bb82SYan, Zheng int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key,
28070257bb82SYan, Zheng 			 struct file_extent_cluster *cluster)
28080257bb82SYan, Zheng {
28090257bb82SYan, Zheng 	int ret;
28100257bb82SYan, Zheng 
28110257bb82SYan, Zheng 	if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) {
28120257bb82SYan, Zheng 		ret = relocate_file_extent_cluster(inode, cluster);
28130257bb82SYan, Zheng 		if (ret)
28140257bb82SYan, Zheng 			return ret;
28150257bb82SYan, Zheng 		cluster->nr = 0;
28160257bb82SYan, Zheng 	}
28170257bb82SYan, Zheng 
28180257bb82SYan, Zheng 	if (!cluster->nr)
28190257bb82SYan, Zheng 		cluster->start = extent_key->objectid;
28200257bb82SYan, Zheng 	else
28210257bb82SYan, Zheng 		BUG_ON(cluster->nr >= MAX_EXTENTS);
28220257bb82SYan, Zheng 	cluster->end = extent_key->objectid + extent_key->offset - 1;
28230257bb82SYan, Zheng 	cluster->boundary[cluster->nr] = extent_key->objectid;
28240257bb82SYan, Zheng 	cluster->nr++;
28250257bb82SYan, Zheng 
28260257bb82SYan, Zheng 	if (cluster->nr >= MAX_EXTENTS) {
28270257bb82SYan, Zheng 		ret = relocate_file_extent_cluster(inode, cluster);
28280257bb82SYan, Zheng 		if (ret)
28290257bb82SYan, Zheng 			return ret;
28300257bb82SYan, Zheng 		cluster->nr = 0;
28310257bb82SYan, Zheng 	}
28320257bb82SYan, Zheng 	return 0;
28335d4f98a2SYan Zheng }
28345d4f98a2SYan Zheng 
28355d4f98a2SYan Zheng /*
28365d4f98a2SYan Zheng  * helper to add a tree block to the list.
28375d4f98a2SYan Zheng  * the major work is getting the generation and level of the block
28385d4f98a2SYan Zheng  */
28395d4f98a2SYan Zheng static int add_tree_block(struct reloc_control *rc,
28405d4f98a2SYan Zheng 			  struct btrfs_key *extent_key,
28415d4f98a2SYan Zheng 			  struct btrfs_path *path,
28425d4f98a2SYan Zheng 			  struct rb_root *blocks)
28435d4f98a2SYan Zheng {
28445d4f98a2SYan Zheng 	struct extent_buffer *eb;
28455d4f98a2SYan Zheng 	struct btrfs_extent_item *ei;
28465d4f98a2SYan Zheng 	struct btrfs_tree_block_info *bi;
28475d4f98a2SYan Zheng 	struct tree_block *block;
28485d4f98a2SYan Zheng 	struct rb_node *rb_node;
28495d4f98a2SYan Zheng 	u32 item_size;
28505d4f98a2SYan Zheng 	int level = -1;
28517fdf4b60SWang Shilong 	u64 generation;
28525d4f98a2SYan Zheng 
28535d4f98a2SYan Zheng 	eb =  path->nodes[0];
28545d4f98a2SYan Zheng 	item_size = btrfs_item_size_nr(eb, path->slots[0]);
28555d4f98a2SYan Zheng 
28563173a18fSJosef Bacik 	if (extent_key->type == BTRFS_METADATA_ITEM_KEY ||
28573173a18fSJosef Bacik 	    item_size >= sizeof(*ei) + sizeof(*bi)) {
28585d4f98a2SYan Zheng 		ei = btrfs_item_ptr(eb, path->slots[0],
28595d4f98a2SYan Zheng 				struct btrfs_extent_item);
28603173a18fSJosef Bacik 		if (extent_key->type == BTRFS_EXTENT_ITEM_KEY) {
28615d4f98a2SYan Zheng 			bi = (struct btrfs_tree_block_info *)(ei + 1);
28625d4f98a2SYan Zheng 			level = btrfs_tree_block_level(eb, bi);
28635d4f98a2SYan Zheng 		} else {
28643173a18fSJosef Bacik 			level = (int)extent_key->offset;
28653173a18fSJosef Bacik 		}
28663173a18fSJosef Bacik 		generation = btrfs_extent_generation(eb, ei);
28676d8ff4e4SDavid Sterba 	} else if (unlikely(item_size == sizeof(struct btrfs_extent_item_v0))) {
2868ba3c2b19SNikolay Borisov 		btrfs_print_v0_err(eb->fs_info);
2869ba3c2b19SNikolay Borisov 		btrfs_handle_fs_error(eb->fs_info, -EINVAL, NULL);
2870ba3c2b19SNikolay Borisov 		return -EINVAL;
28713173a18fSJosef Bacik 	} else {
28725d4f98a2SYan Zheng 		BUG();
28735d4f98a2SYan Zheng 	}
28745d4f98a2SYan Zheng 
2875b3b4aa74SDavid Sterba 	btrfs_release_path(path);
28765d4f98a2SYan Zheng 
28775d4f98a2SYan Zheng 	BUG_ON(level == -1);
28785d4f98a2SYan Zheng 
28795d4f98a2SYan Zheng 	block = kmalloc(sizeof(*block), GFP_NOFS);
28805d4f98a2SYan Zheng 	if (!block)
28815d4f98a2SYan Zheng 		return -ENOMEM;
28825d4f98a2SYan Zheng 
28835d4f98a2SYan Zheng 	block->bytenr = extent_key->objectid;
2884da17066cSJeff Mahoney 	block->key.objectid = rc->extent_root->fs_info->nodesize;
28855d4f98a2SYan Zheng 	block->key.offset = generation;
28865d4f98a2SYan Zheng 	block->level = level;
28875d4f98a2SYan Zheng 	block->key_ready = 0;
28885d4f98a2SYan Zheng 
2889e9a28dc5SQu Wenruo 	rb_node = rb_simple_insert(blocks, block->bytenr, &block->rb_node);
289043c04fb1SJeff Mahoney 	if (rb_node)
2891982c92cbSQu Wenruo 		btrfs_backref_panic(rc->extent_root->fs_info, block->bytenr,
2892982c92cbSQu Wenruo 				    -EEXIST);
28935d4f98a2SYan Zheng 
28945d4f98a2SYan Zheng 	return 0;
28955d4f98a2SYan Zheng }
28965d4f98a2SYan Zheng 
28975d4f98a2SYan Zheng /*
28985d4f98a2SYan Zheng  * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
28995d4f98a2SYan Zheng  */
29005d4f98a2SYan Zheng static int __add_tree_block(struct reloc_control *rc,
29015d4f98a2SYan Zheng 			    u64 bytenr, u32 blocksize,
29025d4f98a2SYan Zheng 			    struct rb_root *blocks)
29035d4f98a2SYan Zheng {
29040b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
29055d4f98a2SYan Zheng 	struct btrfs_path *path;
29065d4f98a2SYan Zheng 	struct btrfs_key key;
29075d4f98a2SYan Zheng 	int ret;
29080b246afaSJeff Mahoney 	bool skinny = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
29095d4f98a2SYan Zheng 
29107476dfdaSDavid Sterba 	if (tree_block_processed(bytenr, rc))
29115d4f98a2SYan Zheng 		return 0;
29125d4f98a2SYan Zheng 
2913e9a28dc5SQu Wenruo 	if (rb_simple_search(blocks, bytenr))
29145d4f98a2SYan Zheng 		return 0;
29155d4f98a2SYan Zheng 
29165d4f98a2SYan Zheng 	path = btrfs_alloc_path();
29175d4f98a2SYan Zheng 	if (!path)
29185d4f98a2SYan Zheng 		return -ENOMEM;
2919aee68ee5SJosef Bacik again:
29205d4f98a2SYan Zheng 	key.objectid = bytenr;
2921aee68ee5SJosef Bacik 	if (skinny) {
2922aee68ee5SJosef Bacik 		key.type = BTRFS_METADATA_ITEM_KEY;
2923aee68ee5SJosef Bacik 		key.offset = (u64)-1;
2924aee68ee5SJosef Bacik 	} else {
29255d4f98a2SYan Zheng 		key.type = BTRFS_EXTENT_ITEM_KEY;
29265d4f98a2SYan Zheng 		key.offset = blocksize;
2927aee68ee5SJosef Bacik 	}
29285d4f98a2SYan Zheng 
29295d4f98a2SYan Zheng 	path->search_commit_root = 1;
29305d4f98a2SYan Zheng 	path->skip_locking = 1;
29315d4f98a2SYan Zheng 	ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0);
29325d4f98a2SYan Zheng 	if (ret < 0)
29335d4f98a2SYan Zheng 		goto out;
29345d4f98a2SYan Zheng 
2935aee68ee5SJosef Bacik 	if (ret > 0 && skinny) {
2936aee68ee5SJosef Bacik 		if (path->slots[0]) {
2937aee68ee5SJosef Bacik 			path->slots[0]--;
2938aee68ee5SJosef Bacik 			btrfs_item_key_to_cpu(path->nodes[0], &key,
2939aee68ee5SJosef Bacik 					      path->slots[0]);
29403173a18fSJosef Bacik 			if (key.objectid == bytenr &&
2941aee68ee5SJosef Bacik 			    (key.type == BTRFS_METADATA_ITEM_KEY ||
2942aee68ee5SJosef Bacik 			     (key.type == BTRFS_EXTENT_ITEM_KEY &&
2943aee68ee5SJosef Bacik 			      key.offset == blocksize)))
29443173a18fSJosef Bacik 				ret = 0;
29453173a18fSJosef Bacik 		}
2946aee68ee5SJosef Bacik 
2947aee68ee5SJosef Bacik 		if (ret) {
2948aee68ee5SJosef Bacik 			skinny = false;
2949aee68ee5SJosef Bacik 			btrfs_release_path(path);
2950aee68ee5SJosef Bacik 			goto again;
2951aee68ee5SJosef Bacik 		}
2952aee68ee5SJosef Bacik 	}
2953cdccee99SLiu Bo 	if (ret) {
2954cdccee99SLiu Bo 		ASSERT(ret == 1);
2955cdccee99SLiu Bo 		btrfs_print_leaf(path->nodes[0]);
2956cdccee99SLiu Bo 		btrfs_err(fs_info,
2957cdccee99SLiu Bo 	     "tree block extent item (%llu) is not found in extent tree",
2958cdccee99SLiu Bo 		     bytenr);
2959cdccee99SLiu Bo 		WARN_ON(1);
2960cdccee99SLiu Bo 		ret = -EINVAL;
2961cdccee99SLiu Bo 		goto out;
2962cdccee99SLiu Bo 	}
29633173a18fSJosef Bacik 
29645d4f98a2SYan Zheng 	ret = add_tree_block(rc, &key, path, blocks);
29655d4f98a2SYan Zheng out:
29665d4f98a2SYan Zheng 	btrfs_free_path(path);
29675d4f98a2SYan Zheng 	return ret;
29685d4f98a2SYan Zheng }
29695d4f98a2SYan Zheng 
29700af3d00bSJosef Bacik static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
297132da5386SDavid Sterba 				    struct btrfs_block_group *block_group,
29721bbc621eSChris Mason 				    struct inode *inode,
29731bbc621eSChris Mason 				    u64 ino)
29740af3d00bSJosef Bacik {
29750af3d00bSJosef Bacik 	struct btrfs_key key;
29760af3d00bSJosef Bacik 	struct btrfs_root *root = fs_info->tree_root;
29770af3d00bSJosef Bacik 	struct btrfs_trans_handle *trans;
29780af3d00bSJosef Bacik 	int ret = 0;
29790af3d00bSJosef Bacik 
29800af3d00bSJosef Bacik 	if (inode)
29810af3d00bSJosef Bacik 		goto truncate;
29820af3d00bSJosef Bacik 
29830af3d00bSJosef Bacik 	key.objectid = ino;
29840af3d00bSJosef Bacik 	key.type = BTRFS_INODE_ITEM_KEY;
29850af3d00bSJosef Bacik 	key.offset = 0;
29860af3d00bSJosef Bacik 
29874c66e0d4SDavid Sterba 	inode = btrfs_iget(fs_info->sb, &key, root);
29882e19f1f9SAl Viro 	if (IS_ERR(inode))
29890af3d00bSJosef Bacik 		return -ENOENT;
29900af3d00bSJosef Bacik 
29910af3d00bSJosef Bacik truncate:
29922ff7e61eSJeff Mahoney 	ret = btrfs_check_trunc_cache_free_space(fs_info,
29937b61cd92SMiao Xie 						 &fs_info->global_block_rsv);
29947b61cd92SMiao Xie 	if (ret)
29957b61cd92SMiao Xie 		goto out;
29967b61cd92SMiao Xie 
29977a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(root);
29980af3d00bSJosef Bacik 	if (IS_ERR(trans)) {
29993612b495STsutomu Itoh 		ret = PTR_ERR(trans);
30000af3d00bSJosef Bacik 		goto out;
30010af3d00bSJosef Bacik 	}
30020af3d00bSJosef Bacik 
300377ab86bfSJeff Mahoney 	ret = btrfs_truncate_free_space_cache(trans, block_group, inode);
30040af3d00bSJosef Bacik 
30053a45bb20SJeff Mahoney 	btrfs_end_transaction(trans);
30062ff7e61eSJeff Mahoney 	btrfs_btree_balance_dirty(fs_info);
30070af3d00bSJosef Bacik out:
30080af3d00bSJosef Bacik 	iput(inode);
30090af3d00bSJosef Bacik 	return ret;
30100af3d00bSJosef Bacik }
30110af3d00bSJosef Bacik 
30125d4f98a2SYan Zheng /*
301319b546d7SQu Wenruo  * Locate the free space cache EXTENT_DATA in root tree leaf and delete the
301419b546d7SQu Wenruo  * cache inode, to avoid free space cache data extent blocking data relocation.
30155d4f98a2SYan Zheng  */
301619b546d7SQu Wenruo static int delete_v1_space_cache(struct extent_buffer *leaf,
301719b546d7SQu Wenruo 				 struct btrfs_block_group *block_group,
301819b546d7SQu Wenruo 				 u64 data_bytenr)
30195d4f98a2SYan Zheng {
302019b546d7SQu Wenruo 	u64 space_cache_ino;
302119b546d7SQu Wenruo 	struct btrfs_file_extent_item *ei;
30225d4f98a2SYan Zheng 	struct btrfs_key key;
302319b546d7SQu Wenruo 	bool found = false;
302419b546d7SQu Wenruo 	int i;
30255d4f98a2SYan Zheng 	int ret;
30265d4f98a2SYan Zheng 
302719b546d7SQu Wenruo 	if (btrfs_header_owner(leaf) != BTRFS_ROOT_TREE_OBJECTID)
302819b546d7SQu Wenruo 		return 0;
30295d4f98a2SYan Zheng 
303019b546d7SQu Wenruo 	for (i = 0; i < btrfs_header_nritems(leaf); i++) {
303119b546d7SQu Wenruo 		btrfs_item_key_to_cpu(leaf, &key, i);
303219b546d7SQu Wenruo 		if (key.type != BTRFS_EXTENT_DATA_KEY)
303319b546d7SQu Wenruo 			continue;
303419b546d7SQu Wenruo 		ei = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
303519b546d7SQu Wenruo 		if (btrfs_file_extent_type(leaf, ei) == BTRFS_FILE_EXTENT_REG &&
303619b546d7SQu Wenruo 		    btrfs_file_extent_disk_bytenr(leaf, ei) == data_bytenr) {
303719b546d7SQu Wenruo 			found = true;
303819b546d7SQu Wenruo 			space_cache_ino = key.objectid;
303919b546d7SQu Wenruo 			break;
304019b546d7SQu Wenruo 		}
304119b546d7SQu Wenruo 	}
304219b546d7SQu Wenruo 	if (!found)
304319b546d7SQu Wenruo 		return -ENOENT;
304419b546d7SQu Wenruo 	ret = delete_block_group_cache(leaf->fs_info, block_group, NULL,
304519b546d7SQu Wenruo 					space_cache_ino);
30460af3d00bSJosef Bacik 	return ret;
30475d4f98a2SYan Zheng }
30485d4f98a2SYan Zheng 
30495d4f98a2SYan Zheng /*
30502c016dc2SLiu Bo  * helper to find all tree blocks that reference a given data extent
30515d4f98a2SYan Zheng  */
30525d4f98a2SYan Zheng static noinline_for_stack
30535d4f98a2SYan Zheng int add_data_references(struct reloc_control *rc,
30545d4f98a2SYan Zheng 			struct btrfs_key *extent_key,
30555d4f98a2SYan Zheng 			struct btrfs_path *path,
30565d4f98a2SYan Zheng 			struct rb_root *blocks)
30575d4f98a2SYan Zheng {
305819b546d7SQu Wenruo 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
305919b546d7SQu Wenruo 	struct ulist *leaves = NULL;
306019b546d7SQu Wenruo 	struct ulist_iterator leaf_uiter;
306119b546d7SQu Wenruo 	struct ulist_node *ref_node = NULL;
306219b546d7SQu Wenruo 	const u32 blocksize = fs_info->nodesize;
3063647f63bdSFilipe David Borba Manana 	int ret = 0;
30645d4f98a2SYan Zheng 
3065b3b4aa74SDavid Sterba 	btrfs_release_path(path);
306619b546d7SQu Wenruo 	ret = btrfs_find_all_leafs(NULL, fs_info, extent_key->objectid,
306719b546d7SQu Wenruo 				   0, &leaves, NULL, true);
306819b546d7SQu Wenruo 	if (ret < 0)
306919b546d7SQu Wenruo 		return ret;
307019b546d7SQu Wenruo 
307119b546d7SQu Wenruo 	ULIST_ITER_INIT(&leaf_uiter);
307219b546d7SQu Wenruo 	while ((ref_node = ulist_next(leaves, &leaf_uiter))) {
307319b546d7SQu Wenruo 		struct extent_buffer *eb;
307419b546d7SQu Wenruo 
307519b546d7SQu Wenruo 		eb = read_tree_block(fs_info, ref_node->val, 0, 0, NULL);
307619b546d7SQu Wenruo 		if (IS_ERR(eb)) {
307719b546d7SQu Wenruo 			ret = PTR_ERR(eb);
307819b546d7SQu Wenruo 			break;
307919b546d7SQu Wenruo 		}
308019b546d7SQu Wenruo 		ret = delete_v1_space_cache(eb, rc->block_group,
308119b546d7SQu Wenruo 					    extent_key->objectid);
308219b546d7SQu Wenruo 		free_extent_buffer(eb);
308319b546d7SQu Wenruo 		if (ret < 0)
308419b546d7SQu Wenruo 			break;
308519b546d7SQu Wenruo 		ret = __add_tree_block(rc, ref_node->val, blocksize, blocks);
308619b546d7SQu Wenruo 		if (ret < 0)
308719b546d7SQu Wenruo 			break;
308819b546d7SQu Wenruo 	}
308919b546d7SQu Wenruo 	if (ret < 0)
30905d4f98a2SYan Zheng 		free_block_list(blocks);
309119b546d7SQu Wenruo 	ulist_free(leaves);
309219b546d7SQu Wenruo 	return ret;
30935d4f98a2SYan Zheng }
30945d4f98a2SYan Zheng 
30955d4f98a2SYan Zheng /*
30962c016dc2SLiu Bo  * helper to find next unprocessed extent
30975d4f98a2SYan Zheng  */
30985d4f98a2SYan Zheng static noinline_for_stack
3099147d256eSZhaolei int find_next_extent(struct reloc_control *rc, struct btrfs_path *path,
31003fd0a558SYan, Zheng 		     struct btrfs_key *extent_key)
31015d4f98a2SYan Zheng {
31020b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
31035d4f98a2SYan Zheng 	struct btrfs_key key;
31045d4f98a2SYan Zheng 	struct extent_buffer *leaf;
31055d4f98a2SYan Zheng 	u64 start, end, last;
31065d4f98a2SYan Zheng 	int ret;
31075d4f98a2SYan Zheng 
3108b3470b5dSDavid Sterba 	last = rc->block_group->start + rc->block_group->length;
31095d4f98a2SYan Zheng 	while (1) {
31105d4f98a2SYan Zheng 		cond_resched();
31115d4f98a2SYan Zheng 		if (rc->search_start >= last) {
31125d4f98a2SYan Zheng 			ret = 1;
31135d4f98a2SYan Zheng 			break;
31145d4f98a2SYan Zheng 		}
31155d4f98a2SYan Zheng 
31165d4f98a2SYan Zheng 		key.objectid = rc->search_start;
31175d4f98a2SYan Zheng 		key.type = BTRFS_EXTENT_ITEM_KEY;
31185d4f98a2SYan Zheng 		key.offset = 0;
31195d4f98a2SYan Zheng 
31205d4f98a2SYan Zheng 		path->search_commit_root = 1;
31215d4f98a2SYan Zheng 		path->skip_locking = 1;
31225d4f98a2SYan Zheng 		ret = btrfs_search_slot(NULL, rc->extent_root, &key, path,
31235d4f98a2SYan Zheng 					0, 0);
31245d4f98a2SYan Zheng 		if (ret < 0)
31255d4f98a2SYan Zheng 			break;
31265d4f98a2SYan Zheng next:
31275d4f98a2SYan Zheng 		leaf = path->nodes[0];
31285d4f98a2SYan Zheng 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
31295d4f98a2SYan Zheng 			ret = btrfs_next_leaf(rc->extent_root, path);
31305d4f98a2SYan Zheng 			if (ret != 0)
31315d4f98a2SYan Zheng 				break;
31325d4f98a2SYan Zheng 			leaf = path->nodes[0];
31335d4f98a2SYan Zheng 		}
31345d4f98a2SYan Zheng 
31355d4f98a2SYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
31365d4f98a2SYan Zheng 		if (key.objectid >= last) {
31375d4f98a2SYan Zheng 			ret = 1;
31385d4f98a2SYan Zheng 			break;
31395d4f98a2SYan Zheng 		}
31405d4f98a2SYan Zheng 
31413173a18fSJosef Bacik 		if (key.type != BTRFS_EXTENT_ITEM_KEY &&
31423173a18fSJosef Bacik 		    key.type != BTRFS_METADATA_ITEM_KEY) {
31433173a18fSJosef Bacik 			path->slots[0]++;
31443173a18fSJosef Bacik 			goto next;
31453173a18fSJosef Bacik 		}
31463173a18fSJosef Bacik 
31473173a18fSJosef Bacik 		if (key.type == BTRFS_EXTENT_ITEM_KEY &&
31485d4f98a2SYan Zheng 		    key.objectid + key.offset <= rc->search_start) {
31495d4f98a2SYan Zheng 			path->slots[0]++;
31505d4f98a2SYan Zheng 			goto next;
31515d4f98a2SYan Zheng 		}
31525d4f98a2SYan Zheng 
31533173a18fSJosef Bacik 		if (key.type == BTRFS_METADATA_ITEM_KEY &&
31540b246afaSJeff Mahoney 		    key.objectid + fs_info->nodesize <=
31553173a18fSJosef Bacik 		    rc->search_start) {
31563173a18fSJosef Bacik 			path->slots[0]++;
31573173a18fSJosef Bacik 			goto next;
31583173a18fSJosef Bacik 		}
31593173a18fSJosef Bacik 
31605d4f98a2SYan Zheng 		ret = find_first_extent_bit(&rc->processed_blocks,
31615d4f98a2SYan Zheng 					    key.objectid, &start, &end,
3162e6138876SJosef Bacik 					    EXTENT_DIRTY, NULL);
31635d4f98a2SYan Zheng 
31645d4f98a2SYan Zheng 		if (ret == 0 && start <= key.objectid) {
3165b3b4aa74SDavid Sterba 			btrfs_release_path(path);
31665d4f98a2SYan Zheng 			rc->search_start = end + 1;
31675d4f98a2SYan Zheng 		} else {
31683173a18fSJosef Bacik 			if (key.type == BTRFS_EXTENT_ITEM_KEY)
31695d4f98a2SYan Zheng 				rc->search_start = key.objectid + key.offset;
31703173a18fSJosef Bacik 			else
31713173a18fSJosef Bacik 				rc->search_start = key.objectid +
31720b246afaSJeff Mahoney 					fs_info->nodesize;
31733fd0a558SYan, Zheng 			memcpy(extent_key, &key, sizeof(key));
31745d4f98a2SYan Zheng 			return 0;
31755d4f98a2SYan Zheng 		}
31765d4f98a2SYan Zheng 	}
3177b3b4aa74SDavid Sterba 	btrfs_release_path(path);
31785d4f98a2SYan Zheng 	return ret;
31795d4f98a2SYan Zheng }
31805d4f98a2SYan Zheng 
31815d4f98a2SYan Zheng static void set_reloc_control(struct reloc_control *rc)
31825d4f98a2SYan Zheng {
31835d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
31847585717fSChris Mason 
31857585717fSChris Mason 	mutex_lock(&fs_info->reloc_mutex);
31865d4f98a2SYan Zheng 	fs_info->reloc_ctl = rc;
31877585717fSChris Mason 	mutex_unlock(&fs_info->reloc_mutex);
31885d4f98a2SYan Zheng }
31895d4f98a2SYan Zheng 
31905d4f98a2SYan Zheng static void unset_reloc_control(struct reloc_control *rc)
31915d4f98a2SYan Zheng {
31925d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
31937585717fSChris Mason 
31947585717fSChris Mason 	mutex_lock(&fs_info->reloc_mutex);
31955d4f98a2SYan Zheng 	fs_info->reloc_ctl = NULL;
31967585717fSChris Mason 	mutex_unlock(&fs_info->reloc_mutex);
31975d4f98a2SYan Zheng }
31985d4f98a2SYan Zheng 
31995d4f98a2SYan Zheng static int check_extent_flags(u64 flags)
32005d4f98a2SYan Zheng {
32015d4f98a2SYan Zheng 	if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
32025d4f98a2SYan Zheng 	    (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
32035d4f98a2SYan Zheng 		return 1;
32045d4f98a2SYan Zheng 	if (!(flags & BTRFS_EXTENT_FLAG_DATA) &&
32055d4f98a2SYan Zheng 	    !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
32065d4f98a2SYan Zheng 		return 1;
32075d4f98a2SYan Zheng 	if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
32085d4f98a2SYan Zheng 	    (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
32095d4f98a2SYan Zheng 		return 1;
32105d4f98a2SYan Zheng 	return 0;
32115d4f98a2SYan Zheng }
32125d4f98a2SYan Zheng 
32133fd0a558SYan, Zheng static noinline_for_stack
32143fd0a558SYan, Zheng int prepare_to_relocate(struct reloc_control *rc)
32153fd0a558SYan, Zheng {
32163fd0a558SYan, Zheng 	struct btrfs_trans_handle *trans;
3217ac2fabacSJosef Bacik 	int ret;
32183fd0a558SYan, Zheng 
32192ff7e61eSJeff Mahoney 	rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root->fs_info,
322066d8f3ddSMiao Xie 					      BTRFS_BLOCK_RSV_TEMP);
32213fd0a558SYan, Zheng 	if (!rc->block_rsv)
32223fd0a558SYan, Zheng 		return -ENOMEM;
32233fd0a558SYan, Zheng 
32243fd0a558SYan, Zheng 	memset(&rc->cluster, 0, sizeof(rc->cluster));
3225b3470b5dSDavid Sterba 	rc->search_start = rc->block_group->start;
32263fd0a558SYan, Zheng 	rc->extents_found = 0;
32273fd0a558SYan, Zheng 	rc->nodes_relocated = 0;
32283fd0a558SYan, Zheng 	rc->merging_rsv_size = 0;
32290647bf56SWang Shilong 	rc->reserved_bytes = 0;
3230da17066cSJeff Mahoney 	rc->block_rsv->size = rc->extent_root->fs_info->nodesize *
32310647bf56SWang Shilong 			      RELOCATION_RESERVED_NODES;
3232ac2fabacSJosef Bacik 	ret = btrfs_block_rsv_refill(rc->extent_root,
3233ac2fabacSJosef Bacik 				     rc->block_rsv, rc->block_rsv->size,
3234ac2fabacSJosef Bacik 				     BTRFS_RESERVE_FLUSH_ALL);
3235ac2fabacSJosef Bacik 	if (ret)
3236ac2fabacSJosef Bacik 		return ret;
32373fd0a558SYan, Zheng 
32383fd0a558SYan, Zheng 	rc->create_reloc_tree = 1;
32393fd0a558SYan, Zheng 	set_reloc_control(rc);
32403fd0a558SYan, Zheng 
32417a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(rc->extent_root);
324228818947SLiu Bo 	if (IS_ERR(trans)) {
324328818947SLiu Bo 		unset_reloc_control(rc);
324428818947SLiu Bo 		/*
324528818947SLiu Bo 		 * extent tree is not a ref_cow tree and has no reloc_root to
324628818947SLiu Bo 		 * cleanup.  And callers are responsible to free the above
324728818947SLiu Bo 		 * block rsv.
324828818947SLiu Bo 		 */
324928818947SLiu Bo 		return PTR_ERR(trans);
325028818947SLiu Bo 	}
32513a45bb20SJeff Mahoney 	btrfs_commit_transaction(trans);
32523fd0a558SYan, Zheng 	return 0;
32533fd0a558SYan, Zheng }
325476dda93cSYan, Zheng 
32555d4f98a2SYan Zheng static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
32565d4f98a2SYan Zheng {
32572ff7e61eSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
32585d4f98a2SYan Zheng 	struct rb_root blocks = RB_ROOT;
32595d4f98a2SYan Zheng 	struct btrfs_key key;
32605d4f98a2SYan Zheng 	struct btrfs_trans_handle *trans = NULL;
32615d4f98a2SYan Zheng 	struct btrfs_path *path;
32625d4f98a2SYan Zheng 	struct btrfs_extent_item *ei;
32635d4f98a2SYan Zheng 	u64 flags;
32645d4f98a2SYan Zheng 	u32 item_size;
32655d4f98a2SYan Zheng 	int ret;
32665d4f98a2SYan Zheng 	int err = 0;
3267c87f08caSChris Mason 	int progress = 0;
32685d4f98a2SYan Zheng 
32695d4f98a2SYan Zheng 	path = btrfs_alloc_path();
32703fd0a558SYan, Zheng 	if (!path)
32715d4f98a2SYan Zheng 		return -ENOMEM;
3272e4058b54SDavid Sterba 	path->reada = READA_FORWARD;
32733fd0a558SYan, Zheng 
32743fd0a558SYan, Zheng 	ret = prepare_to_relocate(rc);
32753fd0a558SYan, Zheng 	if (ret) {
32763fd0a558SYan, Zheng 		err = ret;
32773fd0a558SYan, Zheng 		goto out_free;
32782423fdfbSJiri Slaby 	}
32795d4f98a2SYan Zheng 
32805d4f98a2SYan Zheng 	while (1) {
32810647bf56SWang Shilong 		rc->reserved_bytes = 0;
32820647bf56SWang Shilong 		ret = btrfs_block_rsv_refill(rc->extent_root,
32830647bf56SWang Shilong 					rc->block_rsv, rc->block_rsv->size,
32840647bf56SWang Shilong 					BTRFS_RESERVE_FLUSH_ALL);
32850647bf56SWang Shilong 		if (ret) {
32860647bf56SWang Shilong 			err = ret;
32870647bf56SWang Shilong 			break;
32880647bf56SWang Shilong 		}
3289c87f08caSChris Mason 		progress++;
3290a22285a6SYan, Zheng 		trans = btrfs_start_transaction(rc->extent_root, 0);
32910f788c58SLiu Bo 		if (IS_ERR(trans)) {
32920f788c58SLiu Bo 			err = PTR_ERR(trans);
32930f788c58SLiu Bo 			trans = NULL;
32940f788c58SLiu Bo 			break;
32950f788c58SLiu Bo 		}
3296c87f08caSChris Mason restart:
32973fd0a558SYan, Zheng 		if (update_backref_cache(trans, &rc->backref_cache)) {
32983a45bb20SJeff Mahoney 			btrfs_end_transaction(trans);
329942a657f5SPan Bian 			trans = NULL;
33003fd0a558SYan, Zheng 			continue;
33013fd0a558SYan, Zheng 		}
33023fd0a558SYan, Zheng 
3303147d256eSZhaolei 		ret = find_next_extent(rc, path, &key);
33045d4f98a2SYan Zheng 		if (ret < 0)
33055d4f98a2SYan Zheng 			err = ret;
33065d4f98a2SYan Zheng 		if (ret != 0)
33075d4f98a2SYan Zheng 			break;
33085d4f98a2SYan Zheng 
33095d4f98a2SYan Zheng 		rc->extents_found++;
33105d4f98a2SYan Zheng 
33115d4f98a2SYan Zheng 		ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
33125d4f98a2SYan Zheng 				    struct btrfs_extent_item);
33133fd0a558SYan, Zheng 		item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
33145d4f98a2SYan Zheng 		if (item_size >= sizeof(*ei)) {
33155d4f98a2SYan Zheng 			flags = btrfs_extent_flags(path->nodes[0], ei);
33165d4f98a2SYan Zheng 			ret = check_extent_flags(flags);
33175d4f98a2SYan Zheng 			BUG_ON(ret);
33186d8ff4e4SDavid Sterba 		} else if (unlikely(item_size == sizeof(struct btrfs_extent_item_v0))) {
3319ba3c2b19SNikolay Borisov 			err = -EINVAL;
3320ba3c2b19SNikolay Borisov 			btrfs_print_v0_err(trans->fs_info);
3321ba3c2b19SNikolay Borisov 			btrfs_abort_transaction(trans, err);
3322ba3c2b19SNikolay Borisov 			break;
33235d4f98a2SYan Zheng 		} else {
33245d4f98a2SYan Zheng 			BUG();
33255d4f98a2SYan Zheng 		}
33265d4f98a2SYan Zheng 
33275d4f98a2SYan Zheng 		if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
33285d4f98a2SYan Zheng 			ret = add_tree_block(rc, &key, path, &blocks);
33295d4f98a2SYan Zheng 		} else if (rc->stage == UPDATE_DATA_PTRS &&
33305d4f98a2SYan Zheng 			   (flags & BTRFS_EXTENT_FLAG_DATA)) {
33315d4f98a2SYan Zheng 			ret = add_data_references(rc, &key, path, &blocks);
33325d4f98a2SYan Zheng 		} else {
3333b3b4aa74SDavid Sterba 			btrfs_release_path(path);
33345d4f98a2SYan Zheng 			ret = 0;
33355d4f98a2SYan Zheng 		}
33365d4f98a2SYan Zheng 		if (ret < 0) {
33373fd0a558SYan, Zheng 			err = ret;
33385d4f98a2SYan Zheng 			break;
33395d4f98a2SYan Zheng 		}
33405d4f98a2SYan Zheng 
33415d4f98a2SYan Zheng 		if (!RB_EMPTY_ROOT(&blocks)) {
33425d4f98a2SYan Zheng 			ret = relocate_tree_blocks(trans, rc, &blocks);
33435d4f98a2SYan Zheng 			if (ret < 0) {
33443fd0a558SYan, Zheng 				if (ret != -EAGAIN) {
33455d4f98a2SYan Zheng 					err = ret;
33465d4f98a2SYan Zheng 					break;
33475d4f98a2SYan Zheng 				}
33483fd0a558SYan, Zheng 				rc->extents_found--;
33493fd0a558SYan, Zheng 				rc->search_start = key.objectid;
33503fd0a558SYan, Zheng 			}
33515d4f98a2SYan Zheng 		}
33525d4f98a2SYan Zheng 
33533a45bb20SJeff Mahoney 		btrfs_end_transaction_throttle(trans);
33542ff7e61eSJeff Mahoney 		btrfs_btree_balance_dirty(fs_info);
33553fd0a558SYan, Zheng 		trans = NULL;
33565d4f98a2SYan Zheng 
33575d4f98a2SYan Zheng 		if (rc->stage == MOVE_DATA_EXTENTS &&
33585d4f98a2SYan Zheng 		    (flags & BTRFS_EXTENT_FLAG_DATA)) {
33595d4f98a2SYan Zheng 			rc->found_file_extent = 1;
33600257bb82SYan, Zheng 			ret = relocate_data_extent(rc->data_inode,
33613fd0a558SYan, Zheng 						   &key, &rc->cluster);
33625d4f98a2SYan Zheng 			if (ret < 0) {
33635d4f98a2SYan Zheng 				err = ret;
33645d4f98a2SYan Zheng 				break;
33655d4f98a2SYan Zheng 			}
33665d4f98a2SYan Zheng 		}
3367f31ea088SQu Wenruo 		if (btrfs_should_cancel_balance(fs_info)) {
3368f31ea088SQu Wenruo 			err = -ECANCELED;
3369f31ea088SQu Wenruo 			break;
3370f31ea088SQu Wenruo 		}
33715d4f98a2SYan Zheng 	}
3372c87f08caSChris Mason 	if (trans && progress && err == -ENOSPC) {
337343a7e99dSNikolay Borisov 		ret = btrfs_force_chunk_alloc(trans, rc->block_group->flags);
33749689457bSShilong Wang 		if (ret == 1) {
3375c87f08caSChris Mason 			err = 0;
3376c87f08caSChris Mason 			progress = 0;
3377c87f08caSChris Mason 			goto restart;
3378c87f08caSChris Mason 		}
3379c87f08caSChris Mason 	}
33803fd0a558SYan, Zheng 
3381b3b4aa74SDavid Sterba 	btrfs_release_path(path);
338291166212SDavid Sterba 	clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY);
33835d4f98a2SYan Zheng 
33845d4f98a2SYan Zheng 	if (trans) {
33853a45bb20SJeff Mahoney 		btrfs_end_transaction_throttle(trans);
33862ff7e61eSJeff Mahoney 		btrfs_btree_balance_dirty(fs_info);
33875d4f98a2SYan Zheng 	}
33885d4f98a2SYan Zheng 
33890257bb82SYan, Zheng 	if (!err) {
33903fd0a558SYan, Zheng 		ret = relocate_file_extent_cluster(rc->data_inode,
33913fd0a558SYan, Zheng 						   &rc->cluster);
33920257bb82SYan, Zheng 		if (ret < 0)
33930257bb82SYan, Zheng 			err = ret;
33940257bb82SYan, Zheng 	}
33950257bb82SYan, Zheng 
33963fd0a558SYan, Zheng 	rc->create_reloc_tree = 0;
33973fd0a558SYan, Zheng 	set_reloc_control(rc);
33980257bb82SYan, Zheng 
339913fe1bdbSQu Wenruo 	btrfs_backref_release_cache(&rc->backref_cache);
340063f018beSNikolay Borisov 	btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL);
34015d4f98a2SYan Zheng 
34027f913c7cSQu Wenruo 	/*
34037f913c7cSQu Wenruo 	 * Even in the case when the relocation is cancelled, we should all go
34047f913c7cSQu Wenruo 	 * through prepare_to_merge() and merge_reloc_roots().
34057f913c7cSQu Wenruo 	 *
34067f913c7cSQu Wenruo 	 * For error (including cancelled balance), prepare_to_merge() will
34077f913c7cSQu Wenruo 	 * mark all reloc trees orphan, then queue them for cleanup in
34087f913c7cSQu Wenruo 	 * merge_reloc_roots()
34097f913c7cSQu Wenruo 	 */
34103fd0a558SYan, Zheng 	err = prepare_to_merge(rc, err);
34115d4f98a2SYan Zheng 
34125d4f98a2SYan Zheng 	merge_reloc_roots(rc);
34135d4f98a2SYan Zheng 
34143fd0a558SYan, Zheng 	rc->merge_reloc_tree = 0;
34155d4f98a2SYan Zheng 	unset_reloc_control(rc);
341663f018beSNikolay Borisov 	btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL);
34175d4f98a2SYan Zheng 
34185d4f98a2SYan Zheng 	/* get rid of pinned extents */
34197a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(rc->extent_root);
342062b99540SQu Wenruo 	if (IS_ERR(trans)) {
34213612b495STsutomu Itoh 		err = PTR_ERR(trans);
342262b99540SQu Wenruo 		goto out_free;
342362b99540SQu Wenruo 	}
34243a45bb20SJeff Mahoney 	btrfs_commit_transaction(trans);
34256217b0faSJosef Bacik out_free:
3426d2311e69SQu Wenruo 	ret = clean_dirty_subvols(rc);
3427d2311e69SQu Wenruo 	if (ret < 0 && !err)
3428d2311e69SQu Wenruo 		err = ret;
34292ff7e61eSJeff Mahoney 	btrfs_free_block_rsv(fs_info, rc->block_rsv);
34303fd0a558SYan, Zheng 	btrfs_free_path(path);
34315d4f98a2SYan Zheng 	return err;
34325d4f98a2SYan Zheng }
34335d4f98a2SYan Zheng 
34345d4f98a2SYan Zheng static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
34350257bb82SYan, Zheng 				 struct btrfs_root *root, u64 objectid)
34365d4f98a2SYan Zheng {
34375d4f98a2SYan Zheng 	struct btrfs_path *path;
34385d4f98a2SYan Zheng 	struct btrfs_inode_item *item;
34395d4f98a2SYan Zheng 	struct extent_buffer *leaf;
34405d4f98a2SYan Zheng 	int ret;
34415d4f98a2SYan Zheng 
34425d4f98a2SYan Zheng 	path = btrfs_alloc_path();
34435d4f98a2SYan Zheng 	if (!path)
34445d4f98a2SYan Zheng 		return -ENOMEM;
34455d4f98a2SYan Zheng 
34465d4f98a2SYan Zheng 	ret = btrfs_insert_empty_inode(trans, root, path, objectid);
34475d4f98a2SYan Zheng 	if (ret)
34485d4f98a2SYan Zheng 		goto out;
34495d4f98a2SYan Zheng 
34505d4f98a2SYan Zheng 	leaf = path->nodes[0];
34515d4f98a2SYan Zheng 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
3452b159fa28SDavid Sterba 	memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
34535d4f98a2SYan Zheng 	btrfs_set_inode_generation(leaf, item, 1);
34540257bb82SYan, Zheng 	btrfs_set_inode_size(leaf, item, 0);
34555d4f98a2SYan Zheng 	btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
34563fd0a558SYan, Zheng 	btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS |
34573fd0a558SYan, Zheng 					  BTRFS_INODE_PREALLOC);
34585d4f98a2SYan Zheng 	btrfs_mark_buffer_dirty(leaf);
34595d4f98a2SYan Zheng out:
34605d4f98a2SYan Zheng 	btrfs_free_path(path);
34615d4f98a2SYan Zheng 	return ret;
34625d4f98a2SYan Zheng }
34635d4f98a2SYan Zheng 
34645d4f98a2SYan Zheng /*
34655d4f98a2SYan Zheng  * helper to create inode for data relocation.
34665d4f98a2SYan Zheng  * the inode is in data relocation tree and its link count is 0
34675d4f98a2SYan Zheng  */
34683fd0a558SYan, Zheng static noinline_for_stack
34693fd0a558SYan, Zheng struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
347032da5386SDavid Sterba 				 struct btrfs_block_group *group)
34715d4f98a2SYan Zheng {
34725d4f98a2SYan Zheng 	struct inode *inode = NULL;
34735d4f98a2SYan Zheng 	struct btrfs_trans_handle *trans;
34745d4f98a2SYan Zheng 	struct btrfs_root *root;
34755d4f98a2SYan Zheng 	struct btrfs_key key;
34764624900dSZhaolei 	u64 objectid;
34775d4f98a2SYan Zheng 	int err = 0;
34785d4f98a2SYan Zheng 
34795d4f98a2SYan Zheng 	root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID);
34805d4f98a2SYan Zheng 	if (IS_ERR(root))
34815d4f98a2SYan Zheng 		return ERR_CAST(root);
34825d4f98a2SYan Zheng 
3483a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 6);
348476deacf0SJosef Bacik 	if (IS_ERR(trans)) {
348500246528SJosef Bacik 		btrfs_put_root(root);
34863fd0a558SYan, Zheng 		return ERR_CAST(trans);
348776deacf0SJosef Bacik 	}
34885d4f98a2SYan Zheng 
3489581bb050SLi Zefan 	err = btrfs_find_free_objectid(root, &objectid);
34905d4f98a2SYan Zheng 	if (err)
34915d4f98a2SYan Zheng 		goto out;
34925d4f98a2SYan Zheng 
34930257bb82SYan, Zheng 	err = __insert_orphan_inode(trans, root, objectid);
34945d4f98a2SYan Zheng 	BUG_ON(err);
34955d4f98a2SYan Zheng 
34965d4f98a2SYan Zheng 	key.objectid = objectid;
34975d4f98a2SYan Zheng 	key.type = BTRFS_INODE_ITEM_KEY;
34985d4f98a2SYan Zheng 	key.offset = 0;
34994c66e0d4SDavid Sterba 	inode = btrfs_iget(fs_info->sb, &key, root);
35002e19f1f9SAl Viro 	BUG_ON(IS_ERR(inode));
3501b3470b5dSDavid Sterba 	BTRFS_I(inode)->index_cnt = group->start;
35025d4f98a2SYan Zheng 
350373f2e545SNikolay Borisov 	err = btrfs_orphan_add(trans, BTRFS_I(inode));
35045d4f98a2SYan Zheng out:
350500246528SJosef Bacik 	btrfs_put_root(root);
35063a45bb20SJeff Mahoney 	btrfs_end_transaction(trans);
35072ff7e61eSJeff Mahoney 	btrfs_btree_balance_dirty(fs_info);
35085d4f98a2SYan Zheng 	if (err) {
35095d4f98a2SYan Zheng 		if (inode)
35105d4f98a2SYan Zheng 			iput(inode);
35115d4f98a2SYan Zheng 		inode = ERR_PTR(err);
35125d4f98a2SYan Zheng 	}
35135d4f98a2SYan Zheng 	return inode;
35145d4f98a2SYan Zheng }
35155d4f98a2SYan Zheng 
3516c258d6e3SQu Wenruo static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info)
35173fd0a558SYan, Zheng {
35183fd0a558SYan, Zheng 	struct reloc_control *rc;
35193fd0a558SYan, Zheng 
35203fd0a558SYan, Zheng 	rc = kzalloc(sizeof(*rc), GFP_NOFS);
35213fd0a558SYan, Zheng 	if (!rc)
35223fd0a558SYan, Zheng 		return NULL;
35233fd0a558SYan, Zheng 
35243fd0a558SYan, Zheng 	INIT_LIST_HEAD(&rc->reloc_roots);
3525d2311e69SQu Wenruo 	INIT_LIST_HEAD(&rc->dirty_subvol_roots);
3526584fb121SQu Wenruo 	btrfs_backref_init_cache(fs_info, &rc->backref_cache, 1);
35273fd0a558SYan, Zheng 	mapping_tree_init(&rc->reloc_root_tree);
352843eb5f29SQu Wenruo 	extent_io_tree_init(fs_info, &rc->processed_blocks,
352943eb5f29SQu Wenruo 			    IO_TREE_RELOC_BLOCKS, NULL);
35303fd0a558SYan, Zheng 	return rc;
35313fd0a558SYan, Zheng }
35323fd0a558SYan, Zheng 
35331a0afa0eSJosef Bacik static void free_reloc_control(struct reloc_control *rc)
35341a0afa0eSJosef Bacik {
35351a0afa0eSJosef Bacik 	struct mapping_node *node, *tmp;
35361a0afa0eSJosef Bacik 
35371a0afa0eSJosef Bacik 	free_reloc_roots(&rc->reloc_roots);
35381a0afa0eSJosef Bacik 	rbtree_postorder_for_each_entry_safe(node, tmp,
35391a0afa0eSJosef Bacik 			&rc->reloc_root_tree.rb_root, rb_node)
35401a0afa0eSJosef Bacik 		kfree(node);
35411a0afa0eSJosef Bacik 
35421a0afa0eSJosef Bacik 	kfree(rc);
35431a0afa0eSJosef Bacik }
35441a0afa0eSJosef Bacik 
35455d4f98a2SYan Zheng /*
3546ebce0e01SAdam Borowski  * Print the block group being relocated
3547ebce0e01SAdam Borowski  */
3548ebce0e01SAdam Borowski static void describe_relocation(struct btrfs_fs_info *fs_info,
354932da5386SDavid Sterba 				struct btrfs_block_group *block_group)
3550ebce0e01SAdam Borowski {
3551f89e09cfSAnand Jain 	char buf[128] = {'\0'};
3552ebce0e01SAdam Borowski 
3553f89e09cfSAnand Jain 	btrfs_describe_block_groups(block_group->flags, buf, sizeof(buf));
3554ebce0e01SAdam Borowski 
3555ebce0e01SAdam Borowski 	btrfs_info(fs_info,
3556ebce0e01SAdam Borowski 		   "relocating block group %llu flags %s",
3557b3470b5dSDavid Sterba 		   block_group->start, buf);
3558ebce0e01SAdam Borowski }
3559ebce0e01SAdam Borowski 
3560430640e3SQu Wenruo static const char *stage_to_string(int stage)
3561430640e3SQu Wenruo {
3562430640e3SQu Wenruo 	if (stage == MOVE_DATA_EXTENTS)
3563430640e3SQu Wenruo 		return "move data extents";
3564430640e3SQu Wenruo 	if (stage == UPDATE_DATA_PTRS)
3565430640e3SQu Wenruo 		return "update data pointers";
3566430640e3SQu Wenruo 	return "unknown";
3567430640e3SQu Wenruo }
3568430640e3SQu Wenruo 
3569ebce0e01SAdam Borowski /*
35705d4f98a2SYan Zheng  * function to relocate all extents in a block group.
35715d4f98a2SYan Zheng  */
35726bccf3abSJeff Mahoney int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start)
35735d4f98a2SYan Zheng {
357432da5386SDavid Sterba 	struct btrfs_block_group *bg;
35756bccf3abSJeff Mahoney 	struct btrfs_root *extent_root = fs_info->extent_root;
35765d4f98a2SYan Zheng 	struct reloc_control *rc;
35770af3d00bSJosef Bacik 	struct inode *inode;
35780af3d00bSJosef Bacik 	struct btrfs_path *path;
35795d4f98a2SYan Zheng 	int ret;
3580f0486c68SYan, Zheng 	int rw = 0;
35815d4f98a2SYan Zheng 	int err = 0;
35825d4f98a2SYan Zheng 
3583eede2bf3SOmar Sandoval 	bg = btrfs_lookup_block_group(fs_info, group_start);
3584eede2bf3SOmar Sandoval 	if (!bg)
3585eede2bf3SOmar Sandoval 		return -ENOENT;
3586eede2bf3SOmar Sandoval 
3587eede2bf3SOmar Sandoval 	if (btrfs_pinned_by_swapfile(fs_info, bg)) {
3588eede2bf3SOmar Sandoval 		btrfs_put_block_group(bg);
3589eede2bf3SOmar Sandoval 		return -ETXTBSY;
3590eede2bf3SOmar Sandoval 	}
3591eede2bf3SOmar Sandoval 
3592c258d6e3SQu Wenruo 	rc = alloc_reloc_control(fs_info);
3593eede2bf3SOmar Sandoval 	if (!rc) {
3594eede2bf3SOmar Sandoval 		btrfs_put_block_group(bg);
35955d4f98a2SYan Zheng 		return -ENOMEM;
3596eede2bf3SOmar Sandoval 	}
35975d4f98a2SYan Zheng 
3598f0486c68SYan, Zheng 	rc->extent_root = extent_root;
3599eede2bf3SOmar Sandoval 	rc->block_group = bg;
36005d4f98a2SYan Zheng 
3601b12de528SQu Wenruo 	ret = btrfs_inc_block_group_ro(rc->block_group, true);
3602f0486c68SYan, Zheng 	if (ret) {
3603f0486c68SYan, Zheng 		err = ret;
3604f0486c68SYan, Zheng 		goto out;
3605f0486c68SYan, Zheng 	}
3606f0486c68SYan, Zheng 	rw = 1;
3607f0486c68SYan, Zheng 
36080af3d00bSJosef Bacik 	path = btrfs_alloc_path();
36090af3d00bSJosef Bacik 	if (!path) {
36100af3d00bSJosef Bacik 		err = -ENOMEM;
36110af3d00bSJosef Bacik 		goto out;
36120af3d00bSJosef Bacik 	}
36130af3d00bSJosef Bacik 
36147949f339SDavid Sterba 	inode = lookup_free_space_inode(rc->block_group, path);
36150af3d00bSJosef Bacik 	btrfs_free_path(path);
36160af3d00bSJosef Bacik 
36170af3d00bSJosef Bacik 	if (!IS_ERR(inode))
36181bbc621eSChris Mason 		ret = delete_block_group_cache(fs_info, rc->block_group, inode, 0);
36190af3d00bSJosef Bacik 	else
36200af3d00bSJosef Bacik 		ret = PTR_ERR(inode);
36210af3d00bSJosef Bacik 
36220af3d00bSJosef Bacik 	if (ret && ret != -ENOENT) {
36230af3d00bSJosef Bacik 		err = ret;
36240af3d00bSJosef Bacik 		goto out;
36250af3d00bSJosef Bacik 	}
36260af3d00bSJosef Bacik 
36275d4f98a2SYan Zheng 	rc->data_inode = create_reloc_inode(fs_info, rc->block_group);
36285d4f98a2SYan Zheng 	if (IS_ERR(rc->data_inode)) {
36295d4f98a2SYan Zheng 		err = PTR_ERR(rc->data_inode);
36305d4f98a2SYan Zheng 		rc->data_inode = NULL;
36315d4f98a2SYan Zheng 		goto out;
36325d4f98a2SYan Zheng 	}
36335d4f98a2SYan Zheng 
36340b246afaSJeff Mahoney 	describe_relocation(fs_info, rc->block_group);
36355d4f98a2SYan Zheng 
36369cfa3e34SFilipe Manana 	btrfs_wait_block_group_reservations(rc->block_group);
3637f78c436cSFilipe Manana 	btrfs_wait_nocow_writers(rc->block_group);
36386374e57aSChris Mason 	btrfs_wait_ordered_roots(fs_info, U64_MAX,
3639b3470b5dSDavid Sterba 				 rc->block_group->start,
3640b3470b5dSDavid Sterba 				 rc->block_group->length);
36415d4f98a2SYan Zheng 
36425d4f98a2SYan Zheng 	while (1) {
3643430640e3SQu Wenruo 		int finishes_stage;
3644430640e3SQu Wenruo 
364576dda93cSYan, Zheng 		mutex_lock(&fs_info->cleaner_mutex);
36465d4f98a2SYan Zheng 		ret = relocate_block_group(rc);
364776dda93cSYan, Zheng 		mutex_unlock(&fs_info->cleaner_mutex);
3648ff612ba7SJosef Bacik 		if (ret < 0)
36495d4f98a2SYan Zheng 			err = ret;
3650ff612ba7SJosef Bacik 
3651430640e3SQu Wenruo 		finishes_stage = rc->stage;
3652ff612ba7SJosef Bacik 		/*
3653ff612ba7SJosef Bacik 		 * We may have gotten ENOSPC after we already dirtied some
3654ff612ba7SJosef Bacik 		 * extents.  If writeout happens while we're relocating a
3655ff612ba7SJosef Bacik 		 * different block group we could end up hitting the
3656ff612ba7SJosef Bacik 		 * BUG_ON(rc->stage == UPDATE_DATA_PTRS) in
3657ff612ba7SJosef Bacik 		 * btrfs_reloc_cow_block.  Make sure we write everything out
3658ff612ba7SJosef Bacik 		 * properly so we don't trip over this problem, and then break
3659ff612ba7SJosef Bacik 		 * out of the loop if we hit an error.
3660ff612ba7SJosef Bacik 		 */
3661ff612ba7SJosef Bacik 		if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) {
3662ff612ba7SJosef Bacik 			ret = btrfs_wait_ordered_range(rc->data_inode, 0,
3663ff612ba7SJosef Bacik 						       (u64)-1);
3664ff612ba7SJosef Bacik 			if (ret)
3665ff612ba7SJosef Bacik 				err = ret;
3666ff612ba7SJosef Bacik 			invalidate_mapping_pages(rc->data_inode->i_mapping,
3667ff612ba7SJosef Bacik 						 0, -1);
3668ff612ba7SJosef Bacik 			rc->stage = UPDATE_DATA_PTRS;
36695d4f98a2SYan Zheng 		}
36705d4f98a2SYan Zheng 
3671ff612ba7SJosef Bacik 		if (err < 0)
3672ff612ba7SJosef Bacik 			goto out;
3673ff612ba7SJosef Bacik 
36745d4f98a2SYan Zheng 		if (rc->extents_found == 0)
36755d4f98a2SYan Zheng 			break;
36765d4f98a2SYan Zheng 
3677430640e3SQu Wenruo 		btrfs_info(fs_info, "found %llu extents, stage: %s",
3678430640e3SQu Wenruo 			   rc->extents_found, stage_to_string(finishes_stage));
36795d4f98a2SYan Zheng 	}
36805d4f98a2SYan Zheng 
36815d4f98a2SYan Zheng 	WARN_ON(rc->block_group->pinned > 0);
36825d4f98a2SYan Zheng 	WARN_ON(rc->block_group->reserved > 0);
3683bf38be65SDavid Sterba 	WARN_ON(rc->block_group->used > 0);
36845d4f98a2SYan Zheng out:
3685f0486c68SYan, Zheng 	if (err && rw)
36862ff7e61eSJeff Mahoney 		btrfs_dec_block_group_ro(rc->block_group);
36875d4f98a2SYan Zheng 	iput(rc->data_inode);
36885d4f98a2SYan Zheng 	btrfs_put_block_group(rc->block_group);
36891a0afa0eSJosef Bacik 	free_reloc_control(rc);
36905d4f98a2SYan Zheng 	return err;
36915d4f98a2SYan Zheng }
36925d4f98a2SYan Zheng 
369376dda93cSYan, Zheng static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
369476dda93cSYan, Zheng {
36950b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
369676dda93cSYan, Zheng 	struct btrfs_trans_handle *trans;
369779787eaaSJeff Mahoney 	int ret, err;
369876dda93cSYan, Zheng 
36990b246afaSJeff Mahoney 	trans = btrfs_start_transaction(fs_info->tree_root, 0);
370079787eaaSJeff Mahoney 	if (IS_ERR(trans))
370179787eaaSJeff Mahoney 		return PTR_ERR(trans);
370276dda93cSYan, Zheng 
370376dda93cSYan, Zheng 	memset(&root->root_item.drop_progress, 0,
370476dda93cSYan, Zheng 		sizeof(root->root_item.drop_progress));
370576dda93cSYan, Zheng 	root->root_item.drop_level = 0;
370676dda93cSYan, Zheng 	btrfs_set_root_refs(&root->root_item, 0);
37070b246afaSJeff Mahoney 	ret = btrfs_update_root(trans, fs_info->tree_root,
370876dda93cSYan, Zheng 				&root->root_key, &root->root_item);
370976dda93cSYan, Zheng 
37103a45bb20SJeff Mahoney 	err = btrfs_end_transaction(trans);
371179787eaaSJeff Mahoney 	if (err)
371279787eaaSJeff Mahoney 		return err;
371379787eaaSJeff Mahoney 	return ret;
371476dda93cSYan, Zheng }
371576dda93cSYan, Zheng 
37165d4f98a2SYan Zheng /*
37175d4f98a2SYan Zheng  * recover relocation interrupted by system crash.
37185d4f98a2SYan Zheng  *
37195d4f98a2SYan Zheng  * this function resumes merging reloc trees with corresponding fs trees.
37205d4f98a2SYan Zheng  * this is important for keeping the sharing of tree blocks
37215d4f98a2SYan Zheng  */
37225d4f98a2SYan Zheng int btrfs_recover_relocation(struct btrfs_root *root)
37235d4f98a2SYan Zheng {
37240b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
37255d4f98a2SYan Zheng 	LIST_HEAD(reloc_roots);
37265d4f98a2SYan Zheng 	struct btrfs_key key;
37275d4f98a2SYan Zheng 	struct btrfs_root *fs_root;
37285d4f98a2SYan Zheng 	struct btrfs_root *reloc_root;
37295d4f98a2SYan Zheng 	struct btrfs_path *path;
37305d4f98a2SYan Zheng 	struct extent_buffer *leaf;
37315d4f98a2SYan Zheng 	struct reloc_control *rc = NULL;
37325d4f98a2SYan Zheng 	struct btrfs_trans_handle *trans;
37335d4f98a2SYan Zheng 	int ret;
37345d4f98a2SYan Zheng 	int err = 0;
37355d4f98a2SYan Zheng 
37365d4f98a2SYan Zheng 	path = btrfs_alloc_path();
37375d4f98a2SYan Zheng 	if (!path)
37385d4f98a2SYan Zheng 		return -ENOMEM;
3739e4058b54SDavid Sterba 	path->reada = READA_BACK;
37405d4f98a2SYan Zheng 
37415d4f98a2SYan Zheng 	key.objectid = BTRFS_TREE_RELOC_OBJECTID;
37425d4f98a2SYan Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
37435d4f98a2SYan Zheng 	key.offset = (u64)-1;
37445d4f98a2SYan Zheng 
37455d4f98a2SYan Zheng 	while (1) {
37460b246afaSJeff Mahoney 		ret = btrfs_search_slot(NULL, fs_info->tree_root, &key,
37475d4f98a2SYan Zheng 					path, 0, 0);
37485d4f98a2SYan Zheng 		if (ret < 0) {
37495d4f98a2SYan Zheng 			err = ret;
37505d4f98a2SYan Zheng 			goto out;
37515d4f98a2SYan Zheng 		}
37525d4f98a2SYan Zheng 		if (ret > 0) {
37535d4f98a2SYan Zheng 			if (path->slots[0] == 0)
37545d4f98a2SYan Zheng 				break;
37555d4f98a2SYan Zheng 			path->slots[0]--;
37565d4f98a2SYan Zheng 		}
37575d4f98a2SYan Zheng 		leaf = path->nodes[0];
37585d4f98a2SYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3759b3b4aa74SDavid Sterba 		btrfs_release_path(path);
37605d4f98a2SYan Zheng 
37615d4f98a2SYan Zheng 		if (key.objectid != BTRFS_TREE_RELOC_OBJECTID ||
37625d4f98a2SYan Zheng 		    key.type != BTRFS_ROOT_ITEM_KEY)
37635d4f98a2SYan Zheng 			break;
37645d4f98a2SYan Zheng 
37653dbf1738SJosef Bacik 		reloc_root = btrfs_read_tree_root(root, &key);
37665d4f98a2SYan Zheng 		if (IS_ERR(reloc_root)) {
37675d4f98a2SYan Zheng 			err = PTR_ERR(reloc_root);
37685d4f98a2SYan Zheng 			goto out;
37695d4f98a2SYan Zheng 		}
37705d4f98a2SYan Zheng 
377192a7cc42SQu Wenruo 		set_bit(BTRFS_ROOT_SHAREABLE, &reloc_root->state);
37725d4f98a2SYan Zheng 		list_add(&reloc_root->root_list, &reloc_roots);
37735d4f98a2SYan Zheng 
37745d4f98a2SYan Zheng 		if (btrfs_root_refs(&reloc_root->root_item) > 0) {
37750b246afaSJeff Mahoney 			fs_root = read_fs_root(fs_info,
37765d4f98a2SYan Zheng 					       reloc_root->root_key.offset);
37775d4f98a2SYan Zheng 			if (IS_ERR(fs_root)) {
377876dda93cSYan, Zheng 				ret = PTR_ERR(fs_root);
377976dda93cSYan, Zheng 				if (ret != -ENOENT) {
378076dda93cSYan, Zheng 					err = ret;
37815d4f98a2SYan Zheng 					goto out;
37825d4f98a2SYan Zheng 				}
378379787eaaSJeff Mahoney 				ret = mark_garbage_root(reloc_root);
378479787eaaSJeff Mahoney 				if (ret < 0) {
378579787eaaSJeff Mahoney 					err = ret;
378679787eaaSJeff Mahoney 					goto out;
378779787eaaSJeff Mahoney 				}
3788932fd26dSJosef Bacik 			} else {
378900246528SJosef Bacik 				btrfs_put_root(fs_root);
379076dda93cSYan, Zheng 			}
37915d4f98a2SYan Zheng 		}
37925d4f98a2SYan Zheng 
37935d4f98a2SYan Zheng 		if (key.offset == 0)
37945d4f98a2SYan Zheng 			break;
37955d4f98a2SYan Zheng 
37965d4f98a2SYan Zheng 		key.offset--;
37975d4f98a2SYan Zheng 	}
3798b3b4aa74SDavid Sterba 	btrfs_release_path(path);
37995d4f98a2SYan Zheng 
38005d4f98a2SYan Zheng 	if (list_empty(&reloc_roots))
38015d4f98a2SYan Zheng 		goto out;
38025d4f98a2SYan Zheng 
3803c258d6e3SQu Wenruo 	rc = alloc_reloc_control(fs_info);
38045d4f98a2SYan Zheng 	if (!rc) {
38055d4f98a2SYan Zheng 		err = -ENOMEM;
38065d4f98a2SYan Zheng 		goto out;
38075d4f98a2SYan Zheng 	}
38085d4f98a2SYan Zheng 
38090b246afaSJeff Mahoney 	rc->extent_root = fs_info->extent_root;
38105d4f98a2SYan Zheng 
38115d4f98a2SYan Zheng 	set_reloc_control(rc);
38125d4f98a2SYan Zheng 
38137a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(rc->extent_root);
38143612b495STsutomu Itoh 	if (IS_ERR(trans)) {
38153612b495STsutomu Itoh 		err = PTR_ERR(trans);
3816fb2d83eeSJosef Bacik 		goto out_unset;
38173612b495STsutomu Itoh 	}
38183fd0a558SYan, Zheng 
38193fd0a558SYan, Zheng 	rc->merge_reloc_tree = 1;
38203fd0a558SYan, Zheng 
38215d4f98a2SYan Zheng 	while (!list_empty(&reloc_roots)) {
38225d4f98a2SYan Zheng 		reloc_root = list_entry(reloc_roots.next,
38235d4f98a2SYan Zheng 					struct btrfs_root, root_list);
38245d4f98a2SYan Zheng 		list_del(&reloc_root->root_list);
38255d4f98a2SYan Zheng 
38265d4f98a2SYan Zheng 		if (btrfs_root_refs(&reloc_root->root_item) == 0) {
38275d4f98a2SYan Zheng 			list_add_tail(&reloc_root->root_list,
38285d4f98a2SYan Zheng 				      &rc->reloc_roots);
38295d4f98a2SYan Zheng 			continue;
38305d4f98a2SYan Zheng 		}
38315d4f98a2SYan Zheng 
38320b246afaSJeff Mahoney 		fs_root = read_fs_root(fs_info, reloc_root->root_key.offset);
383379787eaaSJeff Mahoney 		if (IS_ERR(fs_root)) {
383479787eaaSJeff Mahoney 			err = PTR_ERR(fs_root);
3835ca1aa281SJosef Bacik 			list_add_tail(&reloc_root->root_list, &reloc_roots);
38361402d17dSXiyu Yang 			btrfs_end_transaction(trans);
3837fb2d83eeSJosef Bacik 			goto out_unset;
383879787eaaSJeff Mahoney 		}
38395d4f98a2SYan Zheng 
3840ffd7b339SJeff Mahoney 		err = __add_reloc_root(reloc_root);
384179787eaaSJeff Mahoney 		BUG_ON(err < 0); /* -ENOMEM or logic error */
3842f44deb74SJosef Bacik 		fs_root->reloc_root = btrfs_grab_root(reloc_root);
384300246528SJosef Bacik 		btrfs_put_root(fs_root);
38445d4f98a2SYan Zheng 	}
38455d4f98a2SYan Zheng 
38463a45bb20SJeff Mahoney 	err = btrfs_commit_transaction(trans);
384779787eaaSJeff Mahoney 	if (err)
3848fb2d83eeSJosef Bacik 		goto out_unset;
38495d4f98a2SYan Zheng 
38505d4f98a2SYan Zheng 	merge_reloc_roots(rc);
38515d4f98a2SYan Zheng 
38525d4f98a2SYan Zheng 	unset_reloc_control(rc);
38535d4f98a2SYan Zheng 
38547a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(rc->extent_root);
385562b99540SQu Wenruo 	if (IS_ERR(trans)) {
38563612b495STsutomu Itoh 		err = PTR_ERR(trans);
38576217b0faSJosef Bacik 		goto out_clean;
385862b99540SQu Wenruo 	}
38593a45bb20SJeff Mahoney 	err = btrfs_commit_transaction(trans);
38606217b0faSJosef Bacik out_clean:
3861d2311e69SQu Wenruo 	ret = clean_dirty_subvols(rc);
3862d2311e69SQu Wenruo 	if (ret < 0 && !err)
3863d2311e69SQu Wenruo 		err = ret;
3864fb2d83eeSJosef Bacik out_unset:
3865fb2d83eeSJosef Bacik 	unset_reloc_control(rc);
38661a0afa0eSJosef Bacik 	free_reloc_control(rc);
38673612b495STsutomu Itoh out:
3868aca1bba6SLiu Bo 	free_reloc_roots(&reloc_roots);
3869aca1bba6SLiu Bo 
38705d4f98a2SYan Zheng 	btrfs_free_path(path);
38715d4f98a2SYan Zheng 
38725d4f98a2SYan Zheng 	if (err == 0) {
38735d4f98a2SYan Zheng 		/* cleanup orphan inode in data relocation tree */
38740b246afaSJeff Mahoney 		fs_root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID);
3875932fd26dSJosef Bacik 		if (IS_ERR(fs_root)) {
38765d4f98a2SYan Zheng 			err = PTR_ERR(fs_root);
3877932fd26dSJosef Bacik 		} else {
387866b4ffd1SJosef Bacik 			err = btrfs_orphan_cleanup(fs_root);
387900246528SJosef Bacik 			btrfs_put_root(fs_root);
3880932fd26dSJosef Bacik 		}
3881932fd26dSJosef Bacik 	}
38825d4f98a2SYan Zheng 	return err;
38835d4f98a2SYan Zheng }
38845d4f98a2SYan Zheng 
38855d4f98a2SYan Zheng /*
38865d4f98a2SYan Zheng  * helper to add ordered checksum for data relocation.
38875d4f98a2SYan Zheng  *
38885d4f98a2SYan Zheng  * cloning checksum properly handles the nodatasum extents.
38895d4f98a2SYan Zheng  * it also saves CPU time to re-calculate the checksum.
38905d4f98a2SYan Zheng  */
38915d4f98a2SYan Zheng int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
38925d4f98a2SYan Zheng {
38930b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
38945d4f98a2SYan Zheng 	struct btrfs_ordered_sum *sums;
38955d4f98a2SYan Zheng 	struct btrfs_ordered_extent *ordered;
38965d4f98a2SYan Zheng 	int ret;
38975d4f98a2SYan Zheng 	u64 disk_bytenr;
38984577b014SJosef Bacik 	u64 new_bytenr;
38995d4f98a2SYan Zheng 	LIST_HEAD(list);
39005d4f98a2SYan Zheng 
39015d4f98a2SYan Zheng 	ordered = btrfs_lookup_ordered_extent(inode, file_pos);
3902bffe633eSOmar Sandoval 	BUG_ON(ordered->file_offset != file_pos || ordered->num_bytes != len);
39035d4f98a2SYan Zheng 
39045d4f98a2SYan Zheng 	disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
39050b246afaSJeff Mahoney 	ret = btrfs_lookup_csums_range(fs_info->csum_root, disk_bytenr,
3906a2de733cSArne Jansen 				       disk_bytenr + len - 1, &list, 0);
390779787eaaSJeff Mahoney 	if (ret)
390879787eaaSJeff Mahoney 		goto out;
39095d4f98a2SYan Zheng 
39105d4f98a2SYan Zheng 	while (!list_empty(&list)) {
39115d4f98a2SYan Zheng 		sums = list_entry(list.next, struct btrfs_ordered_sum, list);
39125d4f98a2SYan Zheng 		list_del_init(&sums->list);
39135d4f98a2SYan Zheng 
39144577b014SJosef Bacik 		/*
39154577b014SJosef Bacik 		 * We need to offset the new_bytenr based on where the csum is.
39164577b014SJosef Bacik 		 * We need to do this because we will read in entire prealloc
39174577b014SJosef Bacik 		 * extents but we may have written to say the middle of the
39184577b014SJosef Bacik 		 * prealloc extent, so we need to make sure the csum goes with
39194577b014SJosef Bacik 		 * the right disk offset.
39204577b014SJosef Bacik 		 *
39214577b014SJosef Bacik 		 * We can do this because the data reloc inode refers strictly
39224577b014SJosef Bacik 		 * to the on disk bytes, so we don't have to worry about
39234577b014SJosef Bacik 		 * disk_len vs real len like with real inodes since it's all
39244577b014SJosef Bacik 		 * disk length.
39254577b014SJosef Bacik 		 */
3926bffe633eSOmar Sandoval 		new_bytenr = ordered->disk_bytenr + sums->bytenr - disk_bytenr;
39274577b014SJosef Bacik 		sums->bytenr = new_bytenr;
39285d4f98a2SYan Zheng 
3929f9756261SNikolay Borisov 		btrfs_add_ordered_sum(ordered, sums);
39305d4f98a2SYan Zheng 	}
393179787eaaSJeff Mahoney out:
39325d4f98a2SYan Zheng 	btrfs_put_ordered_extent(ordered);
3933411fc6bcSAndi Kleen 	return ret;
39345d4f98a2SYan Zheng }
39353fd0a558SYan, Zheng 
393683d4cfd4SJosef Bacik int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
39373fd0a558SYan, Zheng 			  struct btrfs_root *root, struct extent_buffer *buf,
39383fd0a558SYan, Zheng 			  struct extent_buffer *cow)
39393fd0a558SYan, Zheng {
39400b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
39413fd0a558SYan, Zheng 	struct reloc_control *rc;
3942a26195a5SQu Wenruo 	struct btrfs_backref_node *node;
39433fd0a558SYan, Zheng 	int first_cow = 0;
39443fd0a558SYan, Zheng 	int level;
394583d4cfd4SJosef Bacik 	int ret = 0;
39463fd0a558SYan, Zheng 
39470b246afaSJeff Mahoney 	rc = fs_info->reloc_ctl;
39483fd0a558SYan, Zheng 	if (!rc)
394983d4cfd4SJosef Bacik 		return 0;
39503fd0a558SYan, Zheng 
39513fd0a558SYan, Zheng 	BUG_ON(rc->stage == UPDATE_DATA_PTRS &&
39523fd0a558SYan, Zheng 	       root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID);
39533fd0a558SYan, Zheng 
39543fd0a558SYan, Zheng 	level = btrfs_header_level(buf);
39553fd0a558SYan, Zheng 	if (btrfs_header_generation(buf) <=
39563fd0a558SYan, Zheng 	    btrfs_root_last_snapshot(&root->root_item))
39573fd0a558SYan, Zheng 		first_cow = 1;
39583fd0a558SYan, Zheng 
39593fd0a558SYan, Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID &&
39603fd0a558SYan, Zheng 	    rc->create_reloc_tree) {
39613fd0a558SYan, Zheng 		WARN_ON(!first_cow && level == 0);
39623fd0a558SYan, Zheng 
39633fd0a558SYan, Zheng 		node = rc->backref_cache.path[level];
39643fd0a558SYan, Zheng 		BUG_ON(node->bytenr != buf->start &&
39653fd0a558SYan, Zheng 		       node->new_bytenr != buf->start);
39663fd0a558SYan, Zheng 
3967b0fe7078SQu Wenruo 		btrfs_backref_drop_node_buffer(node);
396867439dadSDavid Sterba 		atomic_inc(&cow->refs);
39693fd0a558SYan, Zheng 		node->eb = cow;
39703fd0a558SYan, Zheng 		node->new_bytenr = cow->start;
39713fd0a558SYan, Zheng 
39723fd0a558SYan, Zheng 		if (!node->pending) {
39733fd0a558SYan, Zheng 			list_move_tail(&node->list,
39743fd0a558SYan, Zheng 				       &rc->backref_cache.pending[level]);
39753fd0a558SYan, Zheng 			node->pending = 1;
39763fd0a558SYan, Zheng 		}
39773fd0a558SYan, Zheng 
39783fd0a558SYan, Zheng 		if (first_cow)
39799569cc20SQu Wenruo 			mark_block_processed(rc, node);
39803fd0a558SYan, Zheng 
39813fd0a558SYan, Zheng 		if (first_cow && level > 0)
39823fd0a558SYan, Zheng 			rc->nodes_relocated += buf->len;
39833fd0a558SYan, Zheng 	}
39843fd0a558SYan, Zheng 
398583d4cfd4SJosef Bacik 	if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS)
39863fd0a558SYan, Zheng 		ret = replace_file_extents(trans, rc, root, cow);
398783d4cfd4SJosef Bacik 	return ret;
39883fd0a558SYan, Zheng }
39893fd0a558SYan, Zheng 
39903fd0a558SYan, Zheng /*
39913fd0a558SYan, Zheng  * called before creating snapshot. it calculates metadata reservation
399201327610SNicholas D Steeves  * required for relocating tree blocks in the snapshot
39933fd0a558SYan, Zheng  */
3994147d256eSZhaolei void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending,
39953fd0a558SYan, Zheng 			      u64 *bytes_to_reserve)
39963fd0a558SYan, Zheng {
399710995c04SQu Wenruo 	struct btrfs_root *root = pending->root;
399810995c04SQu Wenruo 	struct reloc_control *rc = root->fs_info->reloc_ctl;
39993fd0a558SYan, Zheng 
40006282675eSQu Wenruo 	if (!rc || !have_reloc_root(root))
40013fd0a558SYan, Zheng 		return;
40023fd0a558SYan, Zheng 
40033fd0a558SYan, Zheng 	if (!rc->merge_reloc_tree)
40043fd0a558SYan, Zheng 		return;
40053fd0a558SYan, Zheng 
40063fd0a558SYan, Zheng 	root = root->reloc_root;
40073fd0a558SYan, Zheng 	BUG_ON(btrfs_root_refs(&root->root_item) == 0);
40083fd0a558SYan, Zheng 	/*
40093fd0a558SYan, Zheng 	 * relocation is in the stage of merging trees. the space
40103fd0a558SYan, Zheng 	 * used by merging a reloc tree is twice the size of
40113fd0a558SYan, Zheng 	 * relocated tree nodes in the worst case. half for cowing
40123fd0a558SYan, Zheng 	 * the reloc tree, half for cowing the fs tree. the space
40133fd0a558SYan, Zheng 	 * used by cowing the reloc tree will be freed after the
40143fd0a558SYan, Zheng 	 * tree is dropped. if we create snapshot, cowing the fs
40153fd0a558SYan, Zheng 	 * tree may use more space than it frees. so we need
40163fd0a558SYan, Zheng 	 * reserve extra space.
40173fd0a558SYan, Zheng 	 */
40183fd0a558SYan, Zheng 	*bytes_to_reserve += rc->nodes_relocated;
40193fd0a558SYan, Zheng }
40203fd0a558SYan, Zheng 
40213fd0a558SYan, Zheng /*
40223fd0a558SYan, Zheng  * called after snapshot is created. migrate block reservation
40233fd0a558SYan, Zheng  * and create reloc root for the newly created snapshot
4024f44deb74SJosef Bacik  *
4025f44deb74SJosef Bacik  * This is similar to btrfs_init_reloc_root(), we come out of here with two
4026f44deb74SJosef Bacik  * references held on the reloc_root, one for root->reloc_root and one for
4027f44deb74SJosef Bacik  * rc->reloc_roots.
40283fd0a558SYan, Zheng  */
402949b25e05SJeff Mahoney int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
40303fd0a558SYan, Zheng 			       struct btrfs_pending_snapshot *pending)
40313fd0a558SYan, Zheng {
40323fd0a558SYan, Zheng 	struct btrfs_root *root = pending->root;
40333fd0a558SYan, Zheng 	struct btrfs_root *reloc_root;
40343fd0a558SYan, Zheng 	struct btrfs_root *new_root;
403510995c04SQu Wenruo 	struct reloc_control *rc = root->fs_info->reloc_ctl;
40363fd0a558SYan, Zheng 	int ret;
40373fd0a558SYan, Zheng 
40386282675eSQu Wenruo 	if (!rc || !have_reloc_root(root))
403949b25e05SJeff Mahoney 		return 0;
40403fd0a558SYan, Zheng 
40413fd0a558SYan, Zheng 	rc = root->fs_info->reloc_ctl;
40423fd0a558SYan, Zheng 	rc->merging_rsv_size += rc->nodes_relocated;
40433fd0a558SYan, Zheng 
40443fd0a558SYan, Zheng 	if (rc->merge_reloc_tree) {
40453fd0a558SYan, Zheng 		ret = btrfs_block_rsv_migrate(&pending->block_rsv,
40463fd0a558SYan, Zheng 					      rc->block_rsv,
40473a584174SLu Fengqi 					      rc->nodes_relocated, true);
404849b25e05SJeff Mahoney 		if (ret)
404949b25e05SJeff Mahoney 			return ret;
40503fd0a558SYan, Zheng 	}
40513fd0a558SYan, Zheng 
40523fd0a558SYan, Zheng 	new_root = pending->snap;
40533fd0a558SYan, Zheng 	reloc_root = create_reloc_root(trans, root->reloc_root,
40543fd0a558SYan, Zheng 				       new_root->root_key.objectid);
405549b25e05SJeff Mahoney 	if (IS_ERR(reloc_root))
405649b25e05SJeff Mahoney 		return PTR_ERR(reloc_root);
40573fd0a558SYan, Zheng 
4058ffd7b339SJeff Mahoney 	ret = __add_reloc_root(reloc_root);
4059ffd7b339SJeff Mahoney 	BUG_ON(ret < 0);
4060f44deb74SJosef Bacik 	new_root->reloc_root = btrfs_grab_root(reloc_root);
40613fd0a558SYan, Zheng 
406249b25e05SJeff Mahoney 	if (rc->create_reloc_tree)
40633fd0a558SYan, Zheng 		ret = clone_backref_node(trans, rc, root, reloc_root);
406449b25e05SJeff Mahoney 	return ret;
40653fd0a558SYan, Zheng }
4066