xref: /openbmc/linux/fs/btrfs/relocation.c (revision fc997ed0)
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 
32427cdeb70SMiao Xie 	if (!test_bit(BTRFS_ROOT_REF_COWS, &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 *upper;
478a26195a5SQu Wenruo 	struct btrfs_backref_node *lower;
479a26195a5SQu Wenruo 	struct btrfs_backref_node *node = NULL;
480a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
481e7d571c7SQu Wenruo 	int ret;
482e7d571c7SQu Wenruo 	int err = 0;
483e7d571c7SQu Wenruo 
484e7d571c7SQu Wenruo 	iter = btrfs_backref_iter_alloc(rc->extent_root->fs_info, GFP_NOFS);
485e7d571c7SQu Wenruo 	if (!iter)
486e7d571c7SQu Wenruo 		return ERR_PTR(-ENOMEM);
487e7d571c7SQu Wenruo 	path = btrfs_alloc_path();
488e7d571c7SQu Wenruo 	if (!path) {
489e7d571c7SQu Wenruo 		err = -ENOMEM;
490e7d571c7SQu Wenruo 		goto out;
491e7d571c7SQu Wenruo 	}
492e7d571c7SQu Wenruo 
493b1818dabSQu Wenruo 	node = btrfs_backref_alloc_node(cache, bytenr, level);
494e7d571c7SQu Wenruo 	if (!node) {
495e7d571c7SQu Wenruo 		err = -ENOMEM;
496e7d571c7SQu Wenruo 		goto out;
497e7d571c7SQu Wenruo 	}
498e7d571c7SQu Wenruo 
499e7d571c7SQu Wenruo 	node->lowest = 1;
500e7d571c7SQu Wenruo 	cur = node;
501e7d571c7SQu Wenruo 
502e7d571c7SQu Wenruo 	/* Breadth-first search to build backref cache */
503e7d571c7SQu Wenruo 	do {
5041b60d2ecSQu Wenruo 		ret = btrfs_backref_add_tree_node(cache, path, iter, node_key,
5051b60d2ecSQu Wenruo 						  cur);
506e7d571c7SQu Wenruo 		if (ret < 0) {
507e7d571c7SQu Wenruo 			err = ret;
508e7d571c7SQu Wenruo 			goto out;
509e7d571c7SQu Wenruo 		}
510e7d571c7SQu Wenruo 		edge = list_first_entry_or_null(&cache->pending_edge,
511a26195a5SQu Wenruo 				struct btrfs_backref_edge, list[UPPER]);
512e7d571c7SQu Wenruo 		/*
513e7d571c7SQu Wenruo 		 * The pending list isn't empty, take the first block to
514e7d571c7SQu Wenruo 		 * process
515e7d571c7SQu Wenruo 		 */
516e7d571c7SQu Wenruo 		if (edge) {
5175d4f98a2SYan Zheng 			list_del_init(&edge->list[UPPER]);
5185d4f98a2SYan Zheng 			cur = edge->node[UPPER];
5195d4f98a2SYan Zheng 		}
520e7d571c7SQu Wenruo 	} while (edge);
5215d4f98a2SYan Zheng 
5221f872924SQu Wenruo 	/* Finish the upper linkage of newly added edges/nodes */
523fc997ed0SQu Wenruo 	ret = btrfs_backref_finish_upper_links(cache, node);
5241f872924SQu Wenruo 	if (ret < 0) {
5251f872924SQu Wenruo 		err = ret;
52675bfb9afSJosef Bacik 		goto out;
52775bfb9afSJosef Bacik 	}
52875bfb9afSJosef Bacik 
52929db137bSQu Wenruo 	if (handle_useless_nodes(rc, node))
5303fd0a558SYan, Zheng 		node = NULL;
5315d4f98a2SYan Zheng out:
53271f572a9SQu Wenruo 	btrfs_backref_iter_free(iter);
53371f572a9SQu Wenruo 	btrfs_free_path(path);
5345d4f98a2SYan Zheng 	if (err) {
53584780289SQu Wenruo 		while (!list_empty(&cache->useless_node)) {
53684780289SQu Wenruo 			lower = list_first_entry(&cache->useless_node,
537a26195a5SQu Wenruo 					   struct btrfs_backref_node, list);
53875bfb9afSJosef Bacik 			list_del_init(&lower->list);
5393fd0a558SYan, Zheng 		}
54084780289SQu Wenruo 		while (!list_empty(&cache->pending_edge)) {
54184780289SQu Wenruo 			edge = list_first_entry(&cache->pending_edge,
542a26195a5SQu Wenruo 					struct btrfs_backref_edge, list[UPPER]);
54375bfb9afSJosef Bacik 			list_del(&edge->list[UPPER]);
5443fd0a558SYan, Zheng 			list_del(&edge->list[LOWER]);
54575bfb9afSJosef Bacik 			lower = edge->node[LOWER];
5465d4f98a2SYan Zheng 			upper = edge->node[UPPER];
547741188d3SQu Wenruo 			btrfs_backref_free_edge(cache, edge);
54875bfb9afSJosef Bacik 
54975bfb9afSJosef Bacik 			/*
55075bfb9afSJosef Bacik 			 * Lower is no longer linked to any upper backref nodes
55175bfb9afSJosef Bacik 			 * and isn't in the cache, we can free it ourselves.
55275bfb9afSJosef Bacik 			 */
55375bfb9afSJosef Bacik 			if (list_empty(&lower->upper) &&
55475bfb9afSJosef Bacik 			    RB_EMPTY_NODE(&lower->rb_node))
55584780289SQu Wenruo 				list_add(&lower->list, &cache->useless_node);
55675bfb9afSJosef Bacik 
55775bfb9afSJosef Bacik 			if (!RB_EMPTY_NODE(&upper->rb_node))
55875bfb9afSJosef Bacik 				continue;
55975bfb9afSJosef Bacik 
56001327610SNicholas D Steeves 			/* Add this guy's upper edges to the list to process */
56175bfb9afSJosef Bacik 			list_for_each_entry(edge, &upper->upper, list[LOWER])
56284780289SQu Wenruo 				list_add_tail(&edge->list[UPPER],
56384780289SQu Wenruo 					      &cache->pending_edge);
56475bfb9afSJosef Bacik 			if (list_empty(&upper->upper))
56584780289SQu Wenruo 				list_add(&upper->list, &cache->useless_node);
56675bfb9afSJosef Bacik 		}
56775bfb9afSJosef Bacik 
56884780289SQu Wenruo 		while (!list_empty(&cache->useless_node)) {
56984780289SQu Wenruo 			lower = list_first_entry(&cache->useless_node,
570a26195a5SQu Wenruo 					   struct btrfs_backref_node, list);
57175bfb9afSJosef Bacik 			list_del_init(&lower->list);
5720fd8c3daSLiu Bo 			if (lower == node)
5730fd8c3daSLiu Bo 				node = NULL;
574741188d3SQu Wenruo 			btrfs_backref_free_node(cache, lower);
5755d4f98a2SYan Zheng 		}
5760fd8c3daSLiu Bo 
577023acb07SQu Wenruo 		btrfs_backref_cleanup_node(cache, node);
57884780289SQu Wenruo 		ASSERT(list_empty(&cache->useless_node) &&
57984780289SQu Wenruo 		       list_empty(&cache->pending_edge));
5805d4f98a2SYan Zheng 		return ERR_PTR(err);
5815d4f98a2SYan Zheng 	}
58275bfb9afSJosef Bacik 	ASSERT(!node || !node->detached);
58384780289SQu Wenruo 	ASSERT(list_empty(&cache->useless_node) &&
58484780289SQu Wenruo 	       list_empty(&cache->pending_edge));
5855d4f98a2SYan Zheng 	return node;
5865d4f98a2SYan Zheng }
5875d4f98a2SYan Zheng 
5885d4f98a2SYan Zheng /*
5893fd0a558SYan, Zheng  * helper to add backref node for the newly created snapshot.
5903fd0a558SYan, Zheng  * the backref node is created by cloning backref node that
5913fd0a558SYan, Zheng  * corresponds to root of source tree
5923fd0a558SYan, Zheng  */
5933fd0a558SYan, Zheng static int clone_backref_node(struct btrfs_trans_handle *trans,
5943fd0a558SYan, Zheng 			      struct reloc_control *rc,
5953fd0a558SYan, Zheng 			      struct btrfs_root *src,
5963fd0a558SYan, Zheng 			      struct btrfs_root *dest)
5973fd0a558SYan, Zheng {
5983fd0a558SYan, Zheng 	struct btrfs_root *reloc_root = src->reloc_root;
599a26195a5SQu Wenruo 	struct btrfs_backref_cache *cache = &rc->backref_cache;
600a26195a5SQu Wenruo 	struct btrfs_backref_node *node = NULL;
601a26195a5SQu Wenruo 	struct btrfs_backref_node *new_node;
602a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
603a26195a5SQu Wenruo 	struct btrfs_backref_edge *new_edge;
6043fd0a558SYan, Zheng 	struct rb_node *rb_node;
6053fd0a558SYan, Zheng 
6063fd0a558SYan, Zheng 	if (cache->last_trans > 0)
6073fd0a558SYan, Zheng 		update_backref_cache(trans, cache);
6083fd0a558SYan, Zheng 
609e9a28dc5SQu Wenruo 	rb_node = rb_simple_search(&cache->rb_root, src->commit_root->start);
6103fd0a558SYan, Zheng 	if (rb_node) {
611a26195a5SQu Wenruo 		node = rb_entry(rb_node, struct btrfs_backref_node, rb_node);
6123fd0a558SYan, Zheng 		if (node->detached)
6133fd0a558SYan, Zheng 			node = NULL;
6143fd0a558SYan, Zheng 		else
6153fd0a558SYan, Zheng 			BUG_ON(node->new_bytenr != reloc_root->node->start);
6163fd0a558SYan, Zheng 	}
6173fd0a558SYan, Zheng 
6183fd0a558SYan, Zheng 	if (!node) {
619e9a28dc5SQu Wenruo 		rb_node = rb_simple_search(&cache->rb_root,
6203fd0a558SYan, Zheng 					   reloc_root->commit_root->start);
6213fd0a558SYan, Zheng 		if (rb_node) {
622a26195a5SQu Wenruo 			node = rb_entry(rb_node, struct btrfs_backref_node,
6233fd0a558SYan, Zheng 					rb_node);
6243fd0a558SYan, Zheng 			BUG_ON(node->detached);
6253fd0a558SYan, Zheng 		}
6263fd0a558SYan, Zheng 	}
6273fd0a558SYan, Zheng 
6283fd0a558SYan, Zheng 	if (!node)
6293fd0a558SYan, Zheng 		return 0;
6303fd0a558SYan, Zheng 
631b1818dabSQu Wenruo 	new_node = btrfs_backref_alloc_node(cache, dest->node->start,
632b1818dabSQu Wenruo 					    node->level);
6333fd0a558SYan, Zheng 	if (!new_node)
6343fd0a558SYan, Zheng 		return -ENOMEM;
6353fd0a558SYan, Zheng 
6363fd0a558SYan, Zheng 	new_node->lowest = node->lowest;
6376848ad64SYan, Zheng 	new_node->checked = 1;
63800246528SJosef Bacik 	new_node->root = btrfs_grab_root(dest);
6390b530bc5SJosef Bacik 	ASSERT(new_node->root);
6403fd0a558SYan, Zheng 
6413fd0a558SYan, Zheng 	if (!node->lowest) {
6423fd0a558SYan, Zheng 		list_for_each_entry(edge, &node->lower, list[UPPER]) {
64347254d07SQu Wenruo 			new_edge = btrfs_backref_alloc_edge(cache);
6443fd0a558SYan, Zheng 			if (!new_edge)
6453fd0a558SYan, Zheng 				goto fail;
6463fd0a558SYan, Zheng 
647f39911e5SQu Wenruo 			btrfs_backref_link_edge(new_edge, edge->node[LOWER],
648f39911e5SQu Wenruo 						new_node, LINK_UPPER);
6493fd0a558SYan, Zheng 		}
65076b9e23dSMiao Xie 	} else {
65176b9e23dSMiao Xie 		list_add_tail(&new_node->lower, &cache->leaves);
6523fd0a558SYan, Zheng 	}
6533fd0a558SYan, Zheng 
654e9a28dc5SQu Wenruo 	rb_node = rb_simple_insert(&cache->rb_root, new_node->bytenr,
6553fd0a558SYan, Zheng 				   &new_node->rb_node);
65643c04fb1SJeff Mahoney 	if (rb_node)
657982c92cbSQu Wenruo 		btrfs_backref_panic(trans->fs_info, new_node->bytenr, -EEXIST);
6583fd0a558SYan, Zheng 
6593fd0a558SYan, Zheng 	if (!new_node->lowest) {
6603fd0a558SYan, Zheng 		list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) {
6613fd0a558SYan, Zheng 			list_add_tail(&new_edge->list[LOWER],
6623fd0a558SYan, Zheng 				      &new_edge->node[LOWER]->upper);
6633fd0a558SYan, Zheng 		}
6643fd0a558SYan, Zheng 	}
6653fd0a558SYan, Zheng 	return 0;
6663fd0a558SYan, Zheng fail:
6673fd0a558SYan, Zheng 	while (!list_empty(&new_node->lower)) {
6683fd0a558SYan, Zheng 		new_edge = list_entry(new_node->lower.next,
669a26195a5SQu Wenruo 				      struct btrfs_backref_edge, list[UPPER]);
6703fd0a558SYan, Zheng 		list_del(&new_edge->list[UPPER]);
671741188d3SQu Wenruo 		btrfs_backref_free_edge(cache, new_edge);
6723fd0a558SYan, Zheng 	}
673741188d3SQu Wenruo 	btrfs_backref_free_node(cache, new_node);
6743fd0a558SYan, Zheng 	return -ENOMEM;
6753fd0a558SYan, Zheng }
6763fd0a558SYan, Zheng 
6773fd0a558SYan, Zheng /*
6785d4f98a2SYan Zheng  * helper to add 'address of tree root -> reloc tree' mapping
6795d4f98a2SYan Zheng  */
680ffd7b339SJeff Mahoney static int __must_check __add_reloc_root(struct btrfs_root *root)
6815d4f98a2SYan Zheng {
6820b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
6835d4f98a2SYan Zheng 	struct rb_node *rb_node;
6845d4f98a2SYan Zheng 	struct mapping_node *node;
6850b246afaSJeff Mahoney 	struct reloc_control *rc = fs_info->reloc_ctl;
6865d4f98a2SYan Zheng 
6875d4f98a2SYan Zheng 	node = kmalloc(sizeof(*node), GFP_NOFS);
688ffd7b339SJeff Mahoney 	if (!node)
689ffd7b339SJeff Mahoney 		return -ENOMEM;
6905d4f98a2SYan Zheng 
691ea287ab1SJosef Bacik 	node->bytenr = root->commit_root->start;
6925d4f98a2SYan Zheng 	node->data = root;
6935d4f98a2SYan Zheng 
6945d4f98a2SYan Zheng 	spin_lock(&rc->reloc_root_tree.lock);
695e9a28dc5SQu Wenruo 	rb_node = rb_simple_insert(&rc->reloc_root_tree.rb_root,
6965d4f98a2SYan Zheng 				   node->bytenr, &node->rb_node);
6975d4f98a2SYan Zheng 	spin_unlock(&rc->reloc_root_tree.lock);
698ffd7b339SJeff Mahoney 	if (rb_node) {
6990b246afaSJeff Mahoney 		btrfs_panic(fs_info, -EEXIST,
7005d163e0eSJeff Mahoney 			    "Duplicate root found for start=%llu while inserting into relocation tree",
7015d163e0eSJeff Mahoney 			    node->bytenr);
702ffd7b339SJeff Mahoney 	}
7035d4f98a2SYan Zheng 
7045d4f98a2SYan Zheng 	list_add_tail(&root->root_list, &rc->reloc_roots);
7055d4f98a2SYan Zheng 	return 0;
7065d4f98a2SYan Zheng }
7075d4f98a2SYan Zheng 
7085d4f98a2SYan Zheng /*
709c974c464SWang Shilong  * helper to delete the 'address of tree root -> reloc tree'
7105d4f98a2SYan Zheng  * mapping
7115d4f98a2SYan Zheng  */
712c974c464SWang Shilong static void __del_reloc_root(struct btrfs_root *root)
7135d4f98a2SYan Zheng {
7140b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
7155d4f98a2SYan Zheng 	struct rb_node *rb_node;
7165d4f98a2SYan Zheng 	struct mapping_node *node = NULL;
7170b246afaSJeff Mahoney 	struct reloc_control *rc = fs_info->reloc_ctl;
718f44deb74SJosef Bacik 	bool put_ref = false;
7195d4f98a2SYan Zheng 
72065c6e82bSQu Wenruo 	if (rc && root->node) {
7215d4f98a2SYan Zheng 		spin_lock(&rc->reloc_root_tree.lock);
722e9a28dc5SQu Wenruo 		rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root,
723ea287ab1SJosef Bacik 					   root->commit_root->start);
724c974c464SWang Shilong 		if (rb_node) {
725c974c464SWang Shilong 			node = rb_entry(rb_node, struct mapping_node, rb_node);
726c974c464SWang Shilong 			rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
727ea287ab1SJosef Bacik 			RB_CLEAR_NODE(&node->rb_node);
728c974c464SWang Shilong 		}
729c974c464SWang Shilong 		spin_unlock(&rc->reloc_root_tree.lock);
730c974c464SWang Shilong 		if (!node)
731c974c464SWang Shilong 			return;
732c974c464SWang Shilong 		BUG_ON((struct btrfs_root *)node->data != root);
733389305b2SQu Wenruo 	}
734c974c464SWang Shilong 
735f44deb74SJosef Bacik 	/*
736f44deb74SJosef Bacik 	 * We only put the reloc root here if it's on the list.  There's a lot
737f44deb74SJosef Bacik 	 * of places where the pattern is to splice the rc->reloc_roots, process
738f44deb74SJosef Bacik 	 * the reloc roots, and then add the reloc root back onto
739f44deb74SJosef Bacik 	 * rc->reloc_roots.  If we call __del_reloc_root while it's off of the
740f44deb74SJosef Bacik 	 * list we don't want the reference being dropped, because the guy
741f44deb74SJosef Bacik 	 * messing with the list is in charge of the reference.
742f44deb74SJosef Bacik 	 */
7430b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
744f44deb74SJosef Bacik 	if (!list_empty(&root->root_list)) {
745f44deb74SJosef Bacik 		put_ref = true;
746c974c464SWang Shilong 		list_del_init(&root->root_list);
747f44deb74SJosef Bacik 	}
7480b246afaSJeff Mahoney 	spin_unlock(&fs_info->trans_lock);
749f44deb74SJosef Bacik 	if (put_ref)
750f44deb74SJosef Bacik 		btrfs_put_root(root);
751c974c464SWang Shilong 	kfree(node);
752c974c464SWang Shilong }
753c974c464SWang Shilong 
754c974c464SWang Shilong /*
755c974c464SWang Shilong  * helper to update the 'address of tree root -> reloc tree'
756c974c464SWang Shilong  * mapping
757c974c464SWang Shilong  */
758ea287ab1SJosef Bacik static int __update_reloc_root(struct btrfs_root *root)
759c974c464SWang Shilong {
7600b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
761c974c464SWang Shilong 	struct rb_node *rb_node;
762c974c464SWang Shilong 	struct mapping_node *node = NULL;
7630b246afaSJeff Mahoney 	struct reloc_control *rc = fs_info->reloc_ctl;
764c974c464SWang Shilong 
765c974c464SWang Shilong 	spin_lock(&rc->reloc_root_tree.lock);
766e9a28dc5SQu Wenruo 	rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root,
767ea287ab1SJosef Bacik 				   root->commit_root->start);
7685d4f98a2SYan Zheng 	if (rb_node) {
7695d4f98a2SYan Zheng 		node = rb_entry(rb_node, struct mapping_node, rb_node);
7705d4f98a2SYan Zheng 		rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
7715d4f98a2SYan Zheng 	}
7725d4f98a2SYan Zheng 	spin_unlock(&rc->reloc_root_tree.lock);
7735d4f98a2SYan Zheng 
7748f71f3e0SLiu Bo 	if (!node)
7758f71f3e0SLiu Bo 		return 0;
7765d4f98a2SYan Zheng 	BUG_ON((struct btrfs_root *)node->data != root);
7775d4f98a2SYan Zheng 
7785d4f98a2SYan Zheng 	spin_lock(&rc->reloc_root_tree.lock);
779ea287ab1SJosef Bacik 	node->bytenr = root->node->start;
780e9a28dc5SQu Wenruo 	rb_node = rb_simple_insert(&rc->reloc_root_tree.rb_root,
7815d4f98a2SYan Zheng 				   node->bytenr, &node->rb_node);
7825d4f98a2SYan Zheng 	spin_unlock(&rc->reloc_root_tree.lock);
78343c04fb1SJeff Mahoney 	if (rb_node)
784982c92cbSQu Wenruo 		btrfs_backref_panic(fs_info, node->bytenr, -EEXIST);
7855d4f98a2SYan Zheng 	return 0;
7865d4f98a2SYan Zheng }
7875d4f98a2SYan Zheng 
7883fd0a558SYan, Zheng static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
7893fd0a558SYan, Zheng 					struct btrfs_root *root, u64 objectid)
7905d4f98a2SYan Zheng {
7910b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
7925d4f98a2SYan Zheng 	struct btrfs_root *reloc_root;
7935d4f98a2SYan Zheng 	struct extent_buffer *eb;
7945d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
7955d4f98a2SYan Zheng 	struct btrfs_key root_key;
7965d4f98a2SYan Zheng 	int ret;
7975d4f98a2SYan Zheng 
7985d4f98a2SYan Zheng 	root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
7995d4f98a2SYan Zheng 	BUG_ON(!root_item);
8005d4f98a2SYan Zheng 
8015d4f98a2SYan Zheng 	root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
8025d4f98a2SYan Zheng 	root_key.type = BTRFS_ROOT_ITEM_KEY;
8033fd0a558SYan, Zheng 	root_key.offset = objectid;
8045d4f98a2SYan Zheng 
8053fd0a558SYan, Zheng 	if (root->root_key.objectid == objectid) {
806054570a1SFilipe Manana 		u64 commit_root_gen;
807054570a1SFilipe Manana 
8083fd0a558SYan, Zheng 		/* called by btrfs_init_reloc_root */
8095d4f98a2SYan Zheng 		ret = btrfs_copy_root(trans, root, root->commit_root, &eb,
8105d4f98a2SYan Zheng 				      BTRFS_TREE_RELOC_OBJECTID);
8115d4f98a2SYan Zheng 		BUG_ON(ret);
812054570a1SFilipe Manana 		/*
813054570a1SFilipe Manana 		 * Set the last_snapshot field to the generation of the commit
814054570a1SFilipe Manana 		 * root - like this ctree.c:btrfs_block_can_be_shared() behaves
815054570a1SFilipe Manana 		 * correctly (returns true) when the relocation root is created
816054570a1SFilipe Manana 		 * either inside the critical section of a transaction commit
817054570a1SFilipe Manana 		 * (through transaction.c:qgroup_account_snapshot()) and when
818054570a1SFilipe Manana 		 * it's created before the transaction commit is started.
819054570a1SFilipe Manana 		 */
820054570a1SFilipe Manana 		commit_root_gen = btrfs_header_generation(root->commit_root);
821054570a1SFilipe Manana 		btrfs_set_root_last_snapshot(&root->root_item, commit_root_gen);
8223fd0a558SYan, Zheng 	} else {
8233fd0a558SYan, Zheng 		/*
8243fd0a558SYan, Zheng 		 * called by btrfs_reloc_post_snapshot_hook.
8253fd0a558SYan, Zheng 		 * the source tree is a reloc tree, all tree blocks
8263fd0a558SYan, Zheng 		 * modified after it was created have RELOC flag
8273fd0a558SYan, Zheng 		 * set in their headers. so it's OK to not update
8283fd0a558SYan, Zheng 		 * the 'last_snapshot'.
8293fd0a558SYan, Zheng 		 */
8303fd0a558SYan, Zheng 		ret = btrfs_copy_root(trans, root, root->node, &eb,
8313fd0a558SYan, Zheng 				      BTRFS_TREE_RELOC_OBJECTID);
8323fd0a558SYan, Zheng 		BUG_ON(ret);
8333fd0a558SYan, Zheng 	}
8343fd0a558SYan, Zheng 
8355d4f98a2SYan Zheng 	memcpy(root_item, &root->root_item, sizeof(*root_item));
8365d4f98a2SYan Zheng 	btrfs_set_root_bytenr(root_item, eb->start);
8375d4f98a2SYan Zheng 	btrfs_set_root_level(root_item, btrfs_header_level(eb));
8385d4f98a2SYan Zheng 	btrfs_set_root_generation(root_item, trans->transid);
8393fd0a558SYan, Zheng 
8403fd0a558SYan, Zheng 	if (root->root_key.objectid == objectid) {
8413fd0a558SYan, Zheng 		btrfs_set_root_refs(root_item, 0);
8423fd0a558SYan, Zheng 		memset(&root_item->drop_progress, 0,
8433fd0a558SYan, Zheng 		       sizeof(struct btrfs_disk_key));
8445d4f98a2SYan Zheng 		root_item->drop_level = 0;
8453fd0a558SYan, Zheng 	}
8465d4f98a2SYan Zheng 
8475d4f98a2SYan Zheng 	btrfs_tree_unlock(eb);
8485d4f98a2SYan Zheng 	free_extent_buffer(eb);
8495d4f98a2SYan Zheng 
8500b246afaSJeff Mahoney 	ret = btrfs_insert_root(trans, fs_info->tree_root,
8515d4f98a2SYan Zheng 				&root_key, root_item);
8525d4f98a2SYan Zheng 	BUG_ON(ret);
8535d4f98a2SYan Zheng 	kfree(root_item);
8545d4f98a2SYan Zheng 
8553dbf1738SJosef Bacik 	reloc_root = btrfs_read_tree_root(fs_info->tree_root, &root_key);
8565d4f98a2SYan Zheng 	BUG_ON(IS_ERR(reloc_root));
8573dbf1738SJosef Bacik 	set_bit(BTRFS_ROOT_REF_COWS, &reloc_root->state);
8585d4f98a2SYan Zheng 	reloc_root->last_trans = trans->transid;
8593fd0a558SYan, Zheng 	return reloc_root;
8603fd0a558SYan, Zheng }
8613fd0a558SYan, Zheng 
8623fd0a558SYan, Zheng /*
8633fd0a558SYan, Zheng  * create reloc tree for a given fs tree. reloc tree is just a
8643fd0a558SYan, Zheng  * snapshot of the fs tree with special root objectid.
865f44deb74SJosef Bacik  *
866f44deb74SJosef Bacik  * The reloc_root comes out of here with two references, one for
867f44deb74SJosef Bacik  * root->reloc_root, and another for being on the rc->reloc_roots list.
8683fd0a558SYan, Zheng  */
8693fd0a558SYan, Zheng int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
8703fd0a558SYan, Zheng 			  struct btrfs_root *root)
8713fd0a558SYan, Zheng {
8720b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
8733fd0a558SYan, Zheng 	struct btrfs_root *reloc_root;
8740b246afaSJeff Mahoney 	struct reloc_control *rc = fs_info->reloc_ctl;
87520dd2cbfSMiao Xie 	struct btrfs_block_rsv *rsv;
8763fd0a558SYan, Zheng 	int clear_rsv = 0;
877ffd7b339SJeff Mahoney 	int ret;
8783fd0a558SYan, Zheng 
879aec7db3bSJosef Bacik 	if (!rc)
8802abc726aSJosef Bacik 		return 0;
8812abc726aSJosef Bacik 
8821fac4a54SQu Wenruo 	/*
8831fac4a54SQu Wenruo 	 * The subvolume has reloc tree but the swap is finished, no need to
8841fac4a54SQu Wenruo 	 * create/update the dead reloc tree
8851fac4a54SQu Wenruo 	 */
8866282675eSQu Wenruo 	if (reloc_root_is_dead(root))
8871fac4a54SQu Wenruo 		return 0;
8881fac4a54SQu Wenruo 
889aec7db3bSJosef Bacik 	/*
890aec7db3bSJosef Bacik 	 * This is subtle but important.  We do not do
891aec7db3bSJosef Bacik 	 * record_root_in_transaction for reloc roots, instead we record their
892aec7db3bSJosef Bacik 	 * corresponding fs root, and then here we update the last trans for the
893aec7db3bSJosef Bacik 	 * reloc root.  This means that we have to do this for the entire life
894aec7db3bSJosef Bacik 	 * of the reloc root, regardless of which stage of the relocation we are
895aec7db3bSJosef Bacik 	 * in.
896aec7db3bSJosef Bacik 	 */
8973fd0a558SYan, Zheng 	if (root->reloc_root) {
8983fd0a558SYan, Zheng 		reloc_root = root->reloc_root;
8993fd0a558SYan, Zheng 		reloc_root->last_trans = trans->transid;
9003fd0a558SYan, Zheng 		return 0;
9013fd0a558SYan, Zheng 	}
9023fd0a558SYan, Zheng 
903aec7db3bSJosef Bacik 	/*
904aec7db3bSJosef Bacik 	 * We are merging reloc roots, we do not need new reloc trees.  Also
905aec7db3bSJosef Bacik 	 * reloc trees never need their own reloc tree.
906aec7db3bSJosef Bacik 	 */
907aec7db3bSJosef Bacik 	if (!rc->create_reloc_tree ||
908aec7db3bSJosef Bacik 	    root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
909aec7db3bSJosef Bacik 		return 0;
910aec7db3bSJosef Bacik 
91120dd2cbfSMiao Xie 	if (!trans->reloc_reserved) {
91220dd2cbfSMiao Xie 		rsv = trans->block_rsv;
9133fd0a558SYan, Zheng 		trans->block_rsv = rc->block_rsv;
9143fd0a558SYan, Zheng 		clear_rsv = 1;
9153fd0a558SYan, Zheng 	}
9163fd0a558SYan, Zheng 	reloc_root = create_reloc_root(trans, root, root->root_key.objectid);
9173fd0a558SYan, Zheng 	if (clear_rsv)
91820dd2cbfSMiao Xie 		trans->block_rsv = rsv;
9195d4f98a2SYan Zheng 
920ffd7b339SJeff Mahoney 	ret = __add_reloc_root(reloc_root);
921ffd7b339SJeff Mahoney 	BUG_ON(ret < 0);
922f44deb74SJosef Bacik 	root->reloc_root = btrfs_grab_root(reloc_root);
9235d4f98a2SYan Zheng 	return 0;
9245d4f98a2SYan Zheng }
9255d4f98a2SYan Zheng 
9265d4f98a2SYan Zheng /*
9275d4f98a2SYan Zheng  * update root item of reloc tree
9285d4f98a2SYan Zheng  */
9295d4f98a2SYan Zheng int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
9305d4f98a2SYan Zheng 			    struct btrfs_root *root)
9315d4f98a2SYan Zheng {
9320b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
9335d4f98a2SYan Zheng 	struct btrfs_root *reloc_root;
9345d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
9355d4f98a2SYan Zheng 	int ret;
9365d4f98a2SYan Zheng 
9376282675eSQu Wenruo 	if (!have_reloc_root(root))
9387585717fSChris Mason 		goto out;
9395d4f98a2SYan Zheng 
9405d4f98a2SYan Zheng 	reloc_root = root->reloc_root;
9415d4f98a2SYan Zheng 	root_item = &reloc_root->root_item;
9425d4f98a2SYan Zheng 
943f44deb74SJosef Bacik 	/*
944f44deb74SJosef Bacik 	 * We are probably ok here, but __del_reloc_root() will drop its ref of
945f44deb74SJosef Bacik 	 * the root.  We have the ref for root->reloc_root, but just in case
946f44deb74SJosef Bacik 	 * hold it while we update the reloc root.
947f44deb74SJosef Bacik 	 */
948f44deb74SJosef Bacik 	btrfs_grab_root(reloc_root);
949f44deb74SJosef Bacik 
950d2311e69SQu Wenruo 	/* root->reloc_root will stay until current relocation finished */
9510b246afaSJeff Mahoney 	if (fs_info->reloc_ctl->merge_reloc_tree &&
9523fd0a558SYan, Zheng 	    btrfs_root_refs(root_item) == 0) {
953d2311e69SQu Wenruo 		set_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
9546282675eSQu Wenruo 		/*
9556282675eSQu Wenruo 		 * Mark the tree as dead before we change reloc_root so
9566282675eSQu Wenruo 		 * have_reloc_root will not touch it from now on.
9576282675eSQu Wenruo 		 */
9586282675eSQu Wenruo 		smp_wmb();
959c974c464SWang Shilong 		__del_reloc_root(reloc_root);
9605d4f98a2SYan Zheng 	}
9615d4f98a2SYan Zheng 
9625d4f98a2SYan Zheng 	if (reloc_root->commit_root != reloc_root->node) {
963ea287ab1SJosef Bacik 		__update_reloc_root(reloc_root);
9645d4f98a2SYan Zheng 		btrfs_set_root_node(root_item, reloc_root->node);
9655d4f98a2SYan Zheng 		free_extent_buffer(reloc_root->commit_root);
9665d4f98a2SYan Zheng 		reloc_root->commit_root = btrfs_root_node(reloc_root);
9675d4f98a2SYan Zheng 	}
9685d4f98a2SYan Zheng 
9690b246afaSJeff Mahoney 	ret = btrfs_update_root(trans, fs_info->tree_root,
9705d4f98a2SYan Zheng 				&reloc_root->root_key, root_item);
9715d4f98a2SYan Zheng 	BUG_ON(ret);
972f44deb74SJosef Bacik 	btrfs_put_root(reloc_root);
9737585717fSChris Mason out:
9745d4f98a2SYan Zheng 	return 0;
9755d4f98a2SYan Zheng }
9765d4f98a2SYan Zheng 
9775d4f98a2SYan Zheng /*
9785d4f98a2SYan Zheng  * helper to find first cached inode with inode number >= objectid
9795d4f98a2SYan Zheng  * in a subvolume
9805d4f98a2SYan Zheng  */
9815d4f98a2SYan Zheng static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid)
9825d4f98a2SYan Zheng {
9835d4f98a2SYan Zheng 	struct rb_node *node;
9845d4f98a2SYan Zheng 	struct rb_node *prev;
9855d4f98a2SYan Zheng 	struct btrfs_inode *entry;
9865d4f98a2SYan Zheng 	struct inode *inode;
9875d4f98a2SYan Zheng 
9885d4f98a2SYan Zheng 	spin_lock(&root->inode_lock);
9895d4f98a2SYan Zheng again:
9905d4f98a2SYan Zheng 	node = root->inode_tree.rb_node;
9915d4f98a2SYan Zheng 	prev = NULL;
9925d4f98a2SYan Zheng 	while (node) {
9935d4f98a2SYan Zheng 		prev = node;
9945d4f98a2SYan Zheng 		entry = rb_entry(node, struct btrfs_inode, rb_node);
9955d4f98a2SYan Zheng 
9964a0cc7caSNikolay Borisov 		if (objectid < btrfs_ino(entry))
9975d4f98a2SYan Zheng 			node = node->rb_left;
9984a0cc7caSNikolay Borisov 		else if (objectid > btrfs_ino(entry))
9995d4f98a2SYan Zheng 			node = node->rb_right;
10005d4f98a2SYan Zheng 		else
10015d4f98a2SYan Zheng 			break;
10025d4f98a2SYan Zheng 	}
10035d4f98a2SYan Zheng 	if (!node) {
10045d4f98a2SYan Zheng 		while (prev) {
10055d4f98a2SYan Zheng 			entry = rb_entry(prev, struct btrfs_inode, rb_node);
10064a0cc7caSNikolay Borisov 			if (objectid <= btrfs_ino(entry)) {
10075d4f98a2SYan Zheng 				node = prev;
10085d4f98a2SYan Zheng 				break;
10095d4f98a2SYan Zheng 			}
10105d4f98a2SYan Zheng 			prev = rb_next(prev);
10115d4f98a2SYan Zheng 		}
10125d4f98a2SYan Zheng 	}
10135d4f98a2SYan Zheng 	while (node) {
10145d4f98a2SYan Zheng 		entry = rb_entry(node, struct btrfs_inode, rb_node);
10155d4f98a2SYan Zheng 		inode = igrab(&entry->vfs_inode);
10165d4f98a2SYan Zheng 		if (inode) {
10175d4f98a2SYan Zheng 			spin_unlock(&root->inode_lock);
10185d4f98a2SYan Zheng 			return inode;
10195d4f98a2SYan Zheng 		}
10205d4f98a2SYan Zheng 
10214a0cc7caSNikolay Borisov 		objectid = btrfs_ino(entry) + 1;
10225d4f98a2SYan Zheng 		if (cond_resched_lock(&root->inode_lock))
10235d4f98a2SYan Zheng 			goto again;
10245d4f98a2SYan Zheng 
10255d4f98a2SYan Zheng 		node = rb_next(node);
10265d4f98a2SYan Zheng 	}
10275d4f98a2SYan Zheng 	spin_unlock(&root->inode_lock);
10285d4f98a2SYan Zheng 	return NULL;
10295d4f98a2SYan Zheng }
10305d4f98a2SYan Zheng 
10315d4f98a2SYan Zheng /*
10325d4f98a2SYan Zheng  * get new location of data
10335d4f98a2SYan Zheng  */
10345d4f98a2SYan Zheng static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr,
10355d4f98a2SYan Zheng 			    u64 bytenr, u64 num_bytes)
10365d4f98a2SYan Zheng {
10375d4f98a2SYan Zheng 	struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
10385d4f98a2SYan Zheng 	struct btrfs_path *path;
10395d4f98a2SYan Zheng 	struct btrfs_file_extent_item *fi;
10405d4f98a2SYan Zheng 	struct extent_buffer *leaf;
10415d4f98a2SYan Zheng 	int ret;
10425d4f98a2SYan Zheng 
10435d4f98a2SYan Zheng 	path = btrfs_alloc_path();
10445d4f98a2SYan Zheng 	if (!path)
10455d4f98a2SYan Zheng 		return -ENOMEM;
10465d4f98a2SYan Zheng 
10475d4f98a2SYan Zheng 	bytenr -= BTRFS_I(reloc_inode)->index_cnt;
1048f85b7379SDavid Sterba 	ret = btrfs_lookup_file_extent(NULL, root, path,
1049f85b7379SDavid Sterba 			btrfs_ino(BTRFS_I(reloc_inode)), bytenr, 0);
10505d4f98a2SYan Zheng 	if (ret < 0)
10515d4f98a2SYan Zheng 		goto out;
10525d4f98a2SYan Zheng 	if (ret > 0) {
10535d4f98a2SYan Zheng 		ret = -ENOENT;
10545d4f98a2SYan Zheng 		goto out;
10555d4f98a2SYan Zheng 	}
10565d4f98a2SYan Zheng 
10575d4f98a2SYan Zheng 	leaf = path->nodes[0];
10585d4f98a2SYan Zheng 	fi = btrfs_item_ptr(leaf, path->slots[0],
10595d4f98a2SYan Zheng 			    struct btrfs_file_extent_item);
10605d4f98a2SYan Zheng 
10615d4f98a2SYan Zheng 	BUG_ON(btrfs_file_extent_offset(leaf, fi) ||
10625d4f98a2SYan Zheng 	       btrfs_file_extent_compression(leaf, fi) ||
10635d4f98a2SYan Zheng 	       btrfs_file_extent_encryption(leaf, fi) ||
10645d4f98a2SYan Zheng 	       btrfs_file_extent_other_encoding(leaf, fi));
10655d4f98a2SYan Zheng 
10665d4f98a2SYan Zheng 	if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
106783d4cfd4SJosef Bacik 		ret = -EINVAL;
10685d4f98a2SYan Zheng 		goto out;
10695d4f98a2SYan Zheng 	}
10705d4f98a2SYan Zheng 
10715d4f98a2SYan Zheng 	*new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
10725d4f98a2SYan Zheng 	ret = 0;
10735d4f98a2SYan Zheng out:
10745d4f98a2SYan Zheng 	btrfs_free_path(path);
10755d4f98a2SYan Zheng 	return ret;
10765d4f98a2SYan Zheng }
10775d4f98a2SYan Zheng 
10785d4f98a2SYan Zheng /*
10795d4f98a2SYan Zheng  * update file extent items in the tree leaf to point to
10805d4f98a2SYan Zheng  * the new locations.
10815d4f98a2SYan Zheng  */
10823fd0a558SYan, Zheng static noinline_for_stack
10833fd0a558SYan, Zheng int replace_file_extents(struct btrfs_trans_handle *trans,
10845d4f98a2SYan Zheng 			 struct reloc_control *rc,
10855d4f98a2SYan Zheng 			 struct btrfs_root *root,
10863fd0a558SYan, Zheng 			 struct extent_buffer *leaf)
10875d4f98a2SYan Zheng {
10880b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
10895d4f98a2SYan Zheng 	struct btrfs_key key;
10905d4f98a2SYan Zheng 	struct btrfs_file_extent_item *fi;
10915d4f98a2SYan Zheng 	struct inode *inode = NULL;
10925d4f98a2SYan Zheng 	u64 parent;
10935d4f98a2SYan Zheng 	u64 bytenr;
10943fd0a558SYan, Zheng 	u64 new_bytenr = 0;
10955d4f98a2SYan Zheng 	u64 num_bytes;
10965d4f98a2SYan Zheng 	u64 end;
10975d4f98a2SYan Zheng 	u32 nritems;
10985d4f98a2SYan Zheng 	u32 i;
109983d4cfd4SJosef Bacik 	int ret = 0;
11005d4f98a2SYan Zheng 	int first = 1;
11015d4f98a2SYan Zheng 	int dirty = 0;
11025d4f98a2SYan Zheng 
11035d4f98a2SYan Zheng 	if (rc->stage != UPDATE_DATA_PTRS)
11045d4f98a2SYan Zheng 		return 0;
11055d4f98a2SYan Zheng 
11065d4f98a2SYan Zheng 	/* reloc trees always use full backref */
11075d4f98a2SYan Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
11085d4f98a2SYan Zheng 		parent = leaf->start;
11095d4f98a2SYan Zheng 	else
11105d4f98a2SYan Zheng 		parent = 0;
11115d4f98a2SYan Zheng 
11125d4f98a2SYan Zheng 	nritems = btrfs_header_nritems(leaf);
11135d4f98a2SYan Zheng 	for (i = 0; i < nritems; i++) {
111482fa113fSQu Wenruo 		struct btrfs_ref ref = { 0 };
111582fa113fSQu Wenruo 
11165d4f98a2SYan Zheng 		cond_resched();
11175d4f98a2SYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, i);
11185d4f98a2SYan Zheng 		if (key.type != BTRFS_EXTENT_DATA_KEY)
11195d4f98a2SYan Zheng 			continue;
11205d4f98a2SYan Zheng 		fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
11215d4f98a2SYan Zheng 		if (btrfs_file_extent_type(leaf, fi) ==
11225d4f98a2SYan Zheng 		    BTRFS_FILE_EXTENT_INLINE)
11235d4f98a2SYan Zheng 			continue;
11245d4f98a2SYan Zheng 		bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
11255d4f98a2SYan Zheng 		num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
11265d4f98a2SYan Zheng 		if (bytenr == 0)
11275d4f98a2SYan Zheng 			continue;
11289569cc20SQu Wenruo 		if (!in_range(bytenr, rc->block_group->start,
11299569cc20SQu Wenruo 			      rc->block_group->length))
11305d4f98a2SYan Zheng 			continue;
11315d4f98a2SYan Zheng 
11325d4f98a2SYan Zheng 		/*
11335d4f98a2SYan Zheng 		 * if we are modifying block in fs tree, wait for readpage
11345d4f98a2SYan Zheng 		 * to complete and drop the extent cache
11355d4f98a2SYan Zheng 		 */
11365d4f98a2SYan Zheng 		if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
11375d4f98a2SYan Zheng 			if (first) {
11385d4f98a2SYan Zheng 				inode = find_next_inode(root, key.objectid);
11395d4f98a2SYan Zheng 				first = 0;
11404a0cc7caSNikolay Borisov 			} else if (inode && btrfs_ino(BTRFS_I(inode)) < key.objectid) {
11413fd0a558SYan, Zheng 				btrfs_add_delayed_iput(inode);
11425d4f98a2SYan Zheng 				inode = find_next_inode(root, key.objectid);
11435d4f98a2SYan Zheng 			}
11444a0cc7caSNikolay Borisov 			if (inode && btrfs_ino(BTRFS_I(inode)) == key.objectid) {
11455d4f98a2SYan Zheng 				end = key.offset +
11465d4f98a2SYan Zheng 				      btrfs_file_extent_num_bytes(leaf, fi);
11475d4f98a2SYan Zheng 				WARN_ON(!IS_ALIGNED(key.offset,
11480b246afaSJeff Mahoney 						    fs_info->sectorsize));
11490b246afaSJeff Mahoney 				WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize));
11505d4f98a2SYan Zheng 				end--;
11515d4f98a2SYan Zheng 				ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
1152d0082371SJeff Mahoney 						      key.offset, end);
11535d4f98a2SYan Zheng 				if (!ret)
11545d4f98a2SYan Zheng 					continue;
11555d4f98a2SYan Zheng 
1156dcdbc059SNikolay Borisov 				btrfs_drop_extent_cache(BTRFS_I(inode),
1157dcdbc059SNikolay Borisov 						key.offset,	end, 1);
11585d4f98a2SYan Zheng 				unlock_extent(&BTRFS_I(inode)->io_tree,
1159d0082371SJeff Mahoney 					      key.offset, end);
11605d4f98a2SYan Zheng 			}
11615d4f98a2SYan Zheng 		}
11625d4f98a2SYan Zheng 
11635d4f98a2SYan Zheng 		ret = get_new_location(rc->data_inode, &new_bytenr,
11645d4f98a2SYan Zheng 				       bytenr, num_bytes);
116583d4cfd4SJosef Bacik 		if (ret) {
116683d4cfd4SJosef Bacik 			/*
116783d4cfd4SJosef Bacik 			 * Don't have to abort since we've not changed anything
116883d4cfd4SJosef Bacik 			 * in the file extent yet.
116983d4cfd4SJosef Bacik 			 */
117083d4cfd4SJosef Bacik 			break;
11713fd0a558SYan, Zheng 		}
11725d4f98a2SYan Zheng 
11735d4f98a2SYan Zheng 		btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr);
11745d4f98a2SYan Zheng 		dirty = 1;
11755d4f98a2SYan Zheng 
11765d4f98a2SYan Zheng 		key.offset -= btrfs_file_extent_offset(leaf, fi);
117782fa113fSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
117882fa113fSQu Wenruo 				       num_bytes, parent);
117982fa113fSQu Wenruo 		ref.real_root = root->root_key.objectid;
118082fa113fSQu Wenruo 		btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
1181b06c4bf5SFilipe Manana 				    key.objectid, key.offset);
118282fa113fSQu Wenruo 		ret = btrfs_inc_extent_ref(trans, &ref);
118383d4cfd4SJosef Bacik 		if (ret) {
118466642832SJeff Mahoney 			btrfs_abort_transaction(trans, ret);
118583d4cfd4SJosef Bacik 			break;
118683d4cfd4SJosef Bacik 		}
11875d4f98a2SYan Zheng 
1188ffd4bb2aSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
1189ffd4bb2aSQu Wenruo 				       num_bytes, parent);
1190ffd4bb2aSQu Wenruo 		ref.real_root = root->root_key.objectid;
1191ffd4bb2aSQu Wenruo 		btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
1192b06c4bf5SFilipe Manana 				    key.objectid, key.offset);
1193ffd4bb2aSQu Wenruo 		ret = btrfs_free_extent(trans, &ref);
119483d4cfd4SJosef Bacik 		if (ret) {
119566642832SJeff Mahoney 			btrfs_abort_transaction(trans, ret);
119683d4cfd4SJosef Bacik 			break;
119783d4cfd4SJosef Bacik 		}
11985d4f98a2SYan Zheng 	}
11995d4f98a2SYan Zheng 	if (dirty)
12005d4f98a2SYan Zheng 		btrfs_mark_buffer_dirty(leaf);
12013fd0a558SYan, Zheng 	if (inode)
12023fd0a558SYan, Zheng 		btrfs_add_delayed_iput(inode);
120383d4cfd4SJosef Bacik 	return ret;
12045d4f98a2SYan Zheng }
12055d4f98a2SYan Zheng 
12065d4f98a2SYan Zheng static noinline_for_stack
12075d4f98a2SYan Zheng int memcmp_node_keys(struct extent_buffer *eb, int slot,
12085d4f98a2SYan Zheng 		     struct btrfs_path *path, int level)
12095d4f98a2SYan Zheng {
12105d4f98a2SYan Zheng 	struct btrfs_disk_key key1;
12115d4f98a2SYan Zheng 	struct btrfs_disk_key key2;
12125d4f98a2SYan Zheng 	btrfs_node_key(eb, &key1, slot);
12135d4f98a2SYan Zheng 	btrfs_node_key(path->nodes[level], &key2, path->slots[level]);
12145d4f98a2SYan Zheng 	return memcmp(&key1, &key2, sizeof(key1));
12155d4f98a2SYan Zheng }
12165d4f98a2SYan Zheng 
12175d4f98a2SYan Zheng /*
12185d4f98a2SYan Zheng  * try to replace tree blocks in fs tree with the new blocks
12195d4f98a2SYan Zheng  * in reloc tree. tree blocks haven't been modified since the
12205d4f98a2SYan Zheng  * reloc tree was create can be replaced.
12215d4f98a2SYan Zheng  *
12225d4f98a2SYan Zheng  * if a block was replaced, level of the block + 1 is returned.
12235d4f98a2SYan Zheng  * if no block got replaced, 0 is returned. if there are other
12245d4f98a2SYan Zheng  * errors, a negative error number is returned.
12255d4f98a2SYan Zheng  */
12263fd0a558SYan, Zheng static noinline_for_stack
12273d0174f7SQu Wenruo int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc,
12285d4f98a2SYan Zheng 		 struct btrfs_root *dest, struct btrfs_root *src,
12295d4f98a2SYan Zheng 		 struct btrfs_path *path, struct btrfs_key *next_key,
12305d4f98a2SYan Zheng 		 int lowest_level, int max_level)
12315d4f98a2SYan Zheng {
12320b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = dest->fs_info;
12335d4f98a2SYan Zheng 	struct extent_buffer *eb;
12345d4f98a2SYan Zheng 	struct extent_buffer *parent;
123582fa113fSQu Wenruo 	struct btrfs_ref ref = { 0 };
12365d4f98a2SYan Zheng 	struct btrfs_key key;
12375d4f98a2SYan Zheng 	u64 old_bytenr;
12385d4f98a2SYan Zheng 	u64 new_bytenr;
12395d4f98a2SYan Zheng 	u64 old_ptr_gen;
12405d4f98a2SYan Zheng 	u64 new_ptr_gen;
12415d4f98a2SYan Zheng 	u64 last_snapshot;
12425d4f98a2SYan Zheng 	u32 blocksize;
12433fd0a558SYan, Zheng 	int cow = 0;
12445d4f98a2SYan Zheng 	int level;
12455d4f98a2SYan Zheng 	int ret;
12465d4f98a2SYan Zheng 	int slot;
12475d4f98a2SYan Zheng 
12485d4f98a2SYan Zheng 	BUG_ON(src->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
12495d4f98a2SYan Zheng 	BUG_ON(dest->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID);
12505d4f98a2SYan Zheng 
12515d4f98a2SYan Zheng 	last_snapshot = btrfs_root_last_snapshot(&src->root_item);
12523fd0a558SYan, Zheng again:
12535d4f98a2SYan Zheng 	slot = path->slots[lowest_level];
12545d4f98a2SYan Zheng 	btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot);
12555d4f98a2SYan Zheng 
12565d4f98a2SYan Zheng 	eb = btrfs_lock_root_node(dest);
12578bead258SDavid Sterba 	btrfs_set_lock_blocking_write(eb);
12585d4f98a2SYan Zheng 	level = btrfs_header_level(eb);
12595d4f98a2SYan Zheng 
12605d4f98a2SYan Zheng 	if (level < lowest_level) {
12615d4f98a2SYan Zheng 		btrfs_tree_unlock(eb);
12625d4f98a2SYan Zheng 		free_extent_buffer(eb);
12635d4f98a2SYan Zheng 		return 0;
12645d4f98a2SYan Zheng 	}
12655d4f98a2SYan Zheng 
12663fd0a558SYan, Zheng 	if (cow) {
12675d4f98a2SYan Zheng 		ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb);
12685d4f98a2SYan Zheng 		BUG_ON(ret);
12693fd0a558SYan, Zheng 	}
12708bead258SDavid Sterba 	btrfs_set_lock_blocking_write(eb);
12715d4f98a2SYan Zheng 
12725d4f98a2SYan Zheng 	if (next_key) {
12735d4f98a2SYan Zheng 		next_key->objectid = (u64)-1;
12745d4f98a2SYan Zheng 		next_key->type = (u8)-1;
12755d4f98a2SYan Zheng 		next_key->offset = (u64)-1;
12765d4f98a2SYan Zheng 	}
12775d4f98a2SYan Zheng 
12785d4f98a2SYan Zheng 	parent = eb;
12795d4f98a2SYan Zheng 	while (1) {
1280581c1760SQu Wenruo 		struct btrfs_key first_key;
1281581c1760SQu Wenruo 
12825d4f98a2SYan Zheng 		level = btrfs_header_level(parent);
12835d4f98a2SYan Zheng 		BUG_ON(level < lowest_level);
12845d4f98a2SYan Zheng 
12855d4f98a2SYan Zheng 		ret = btrfs_bin_search(parent, &key, level, &slot);
1286cbca7d59SFilipe Manana 		if (ret < 0)
1287cbca7d59SFilipe Manana 			break;
12885d4f98a2SYan Zheng 		if (ret && slot > 0)
12895d4f98a2SYan Zheng 			slot--;
12905d4f98a2SYan Zheng 
12915d4f98a2SYan Zheng 		if (next_key && slot + 1 < btrfs_header_nritems(parent))
12925d4f98a2SYan Zheng 			btrfs_node_key_to_cpu(parent, next_key, slot + 1);
12935d4f98a2SYan Zheng 
12945d4f98a2SYan Zheng 		old_bytenr = btrfs_node_blockptr(parent, slot);
12950b246afaSJeff Mahoney 		blocksize = fs_info->nodesize;
12965d4f98a2SYan Zheng 		old_ptr_gen = btrfs_node_ptr_generation(parent, slot);
129717515f1bSQu Wenruo 		btrfs_node_key_to_cpu(parent, &first_key, slot);
12985d4f98a2SYan Zheng 
12995d4f98a2SYan Zheng 		if (level <= max_level) {
13005d4f98a2SYan Zheng 			eb = path->nodes[level];
13015d4f98a2SYan Zheng 			new_bytenr = btrfs_node_blockptr(eb,
13025d4f98a2SYan Zheng 							path->slots[level]);
13035d4f98a2SYan Zheng 			new_ptr_gen = btrfs_node_ptr_generation(eb,
13045d4f98a2SYan Zheng 							path->slots[level]);
13055d4f98a2SYan Zheng 		} else {
13065d4f98a2SYan Zheng 			new_bytenr = 0;
13075d4f98a2SYan Zheng 			new_ptr_gen = 0;
13085d4f98a2SYan Zheng 		}
13095d4f98a2SYan Zheng 
1310fae7f21cSDulshani Gunawardhana 		if (WARN_ON(new_bytenr > 0 && new_bytenr == old_bytenr)) {
13115d4f98a2SYan Zheng 			ret = level;
13125d4f98a2SYan Zheng 			break;
13135d4f98a2SYan Zheng 		}
13145d4f98a2SYan Zheng 
13155d4f98a2SYan Zheng 		if (new_bytenr == 0 || old_ptr_gen > last_snapshot ||
13165d4f98a2SYan Zheng 		    memcmp_node_keys(parent, slot, path, level)) {
13173fd0a558SYan, Zheng 			if (level <= lowest_level) {
13185d4f98a2SYan Zheng 				ret = 0;
13195d4f98a2SYan Zheng 				break;
13205d4f98a2SYan Zheng 			}
13215d4f98a2SYan Zheng 
1322581c1760SQu Wenruo 			eb = read_tree_block(fs_info, old_bytenr, old_ptr_gen,
1323581c1760SQu Wenruo 					     level - 1, &first_key);
132464c043deSLiu Bo 			if (IS_ERR(eb)) {
132564c043deSLiu Bo 				ret = PTR_ERR(eb);
1326264813acSLiu Bo 				break;
132764c043deSLiu Bo 			} else if (!extent_buffer_uptodate(eb)) {
132864c043deSLiu Bo 				ret = -EIO;
1329416bc658SJosef Bacik 				free_extent_buffer(eb);
1330379cde74SStefan Behrens 				break;
1331416bc658SJosef Bacik 			}
13325d4f98a2SYan Zheng 			btrfs_tree_lock(eb);
13333fd0a558SYan, Zheng 			if (cow) {
13345d4f98a2SYan Zheng 				ret = btrfs_cow_block(trans, dest, eb, parent,
13355d4f98a2SYan Zheng 						      slot, &eb);
13365d4f98a2SYan Zheng 				BUG_ON(ret);
13375d4f98a2SYan Zheng 			}
13388bead258SDavid Sterba 			btrfs_set_lock_blocking_write(eb);
13395d4f98a2SYan Zheng 
13405d4f98a2SYan Zheng 			btrfs_tree_unlock(parent);
13415d4f98a2SYan Zheng 			free_extent_buffer(parent);
13425d4f98a2SYan Zheng 
13435d4f98a2SYan Zheng 			parent = eb;
13445d4f98a2SYan Zheng 			continue;
13455d4f98a2SYan Zheng 		}
13465d4f98a2SYan Zheng 
13473fd0a558SYan, Zheng 		if (!cow) {
13483fd0a558SYan, Zheng 			btrfs_tree_unlock(parent);
13493fd0a558SYan, Zheng 			free_extent_buffer(parent);
13503fd0a558SYan, Zheng 			cow = 1;
13513fd0a558SYan, Zheng 			goto again;
13523fd0a558SYan, Zheng 		}
13533fd0a558SYan, Zheng 
13545d4f98a2SYan Zheng 		btrfs_node_key_to_cpu(path->nodes[level], &key,
13555d4f98a2SYan Zheng 				      path->slots[level]);
1356b3b4aa74SDavid Sterba 		btrfs_release_path(path);
13575d4f98a2SYan Zheng 
13585d4f98a2SYan Zheng 		path->lowest_level = level;
13595d4f98a2SYan Zheng 		ret = btrfs_search_slot(trans, src, &key, path, 0, 1);
13605d4f98a2SYan Zheng 		path->lowest_level = 0;
13615d4f98a2SYan Zheng 		BUG_ON(ret);
13625d4f98a2SYan Zheng 
13635d4f98a2SYan Zheng 		/*
1364824d8dffSQu Wenruo 		 * Info qgroup to trace both subtrees.
1365824d8dffSQu Wenruo 		 *
1366824d8dffSQu Wenruo 		 * We must trace both trees.
1367824d8dffSQu Wenruo 		 * 1) Tree reloc subtree
1368824d8dffSQu Wenruo 		 *    If not traced, we will leak data numbers
1369824d8dffSQu Wenruo 		 * 2) Fs subtree
1370824d8dffSQu Wenruo 		 *    If not traced, we will double count old data
1371f616f5cdSQu Wenruo 		 *
1372f616f5cdSQu Wenruo 		 * We don't scan the subtree right now, but only record
1373f616f5cdSQu Wenruo 		 * the swapped tree blocks.
1374f616f5cdSQu Wenruo 		 * The real subtree rescan is delayed until we have new
1375f616f5cdSQu Wenruo 		 * CoW on the subtree root node before transaction commit.
1376824d8dffSQu Wenruo 		 */
1377370a11b8SQu Wenruo 		ret = btrfs_qgroup_add_swapped_blocks(trans, dest,
1378370a11b8SQu Wenruo 				rc->block_group, parent, slot,
1379370a11b8SQu Wenruo 				path->nodes[level], path->slots[level],
1380370a11b8SQu Wenruo 				last_snapshot);
1381370a11b8SQu Wenruo 		if (ret < 0)
1382370a11b8SQu Wenruo 			break;
1383824d8dffSQu Wenruo 		/*
13845d4f98a2SYan Zheng 		 * swap blocks in fs tree and reloc tree.
13855d4f98a2SYan Zheng 		 */
13865d4f98a2SYan Zheng 		btrfs_set_node_blockptr(parent, slot, new_bytenr);
13875d4f98a2SYan Zheng 		btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen);
13885d4f98a2SYan Zheng 		btrfs_mark_buffer_dirty(parent);
13895d4f98a2SYan Zheng 
13905d4f98a2SYan Zheng 		btrfs_set_node_blockptr(path->nodes[level],
13915d4f98a2SYan Zheng 					path->slots[level], old_bytenr);
13925d4f98a2SYan Zheng 		btrfs_set_node_ptr_generation(path->nodes[level],
13935d4f98a2SYan Zheng 					      path->slots[level], old_ptr_gen);
13945d4f98a2SYan Zheng 		btrfs_mark_buffer_dirty(path->nodes[level]);
13955d4f98a2SYan Zheng 
139682fa113fSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, old_bytenr,
139782fa113fSQu Wenruo 				       blocksize, path->nodes[level]->start);
139882fa113fSQu Wenruo 		ref.skip_qgroup = true;
139982fa113fSQu Wenruo 		btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid);
140082fa113fSQu Wenruo 		ret = btrfs_inc_extent_ref(trans, &ref);
14015d4f98a2SYan Zheng 		BUG_ON(ret);
140282fa113fSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
140382fa113fSQu Wenruo 				       blocksize, 0);
140482fa113fSQu Wenruo 		ref.skip_qgroup = true;
140582fa113fSQu Wenruo 		btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid);
140682fa113fSQu Wenruo 		ret = btrfs_inc_extent_ref(trans, &ref);
14075d4f98a2SYan Zheng 		BUG_ON(ret);
14085d4f98a2SYan Zheng 
1409ffd4bb2aSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, new_bytenr,
1410ffd4bb2aSQu Wenruo 				       blocksize, path->nodes[level]->start);
1411ffd4bb2aSQu Wenruo 		btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid);
1412ffd4bb2aSQu Wenruo 		ref.skip_qgroup = true;
1413ffd4bb2aSQu Wenruo 		ret = btrfs_free_extent(trans, &ref);
14145d4f98a2SYan Zheng 		BUG_ON(ret);
14155d4f98a2SYan Zheng 
1416ffd4bb2aSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, old_bytenr,
1417ffd4bb2aSQu Wenruo 				       blocksize, 0);
1418ffd4bb2aSQu Wenruo 		btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid);
1419ffd4bb2aSQu Wenruo 		ref.skip_qgroup = true;
1420ffd4bb2aSQu Wenruo 		ret = btrfs_free_extent(trans, &ref);
14215d4f98a2SYan Zheng 		BUG_ON(ret);
14225d4f98a2SYan Zheng 
14235d4f98a2SYan Zheng 		btrfs_unlock_up_safe(path, 0);
14245d4f98a2SYan Zheng 
14255d4f98a2SYan Zheng 		ret = level;
14265d4f98a2SYan Zheng 		break;
14275d4f98a2SYan Zheng 	}
14285d4f98a2SYan Zheng 	btrfs_tree_unlock(parent);
14295d4f98a2SYan Zheng 	free_extent_buffer(parent);
14305d4f98a2SYan Zheng 	return ret;
14315d4f98a2SYan Zheng }
14325d4f98a2SYan Zheng 
14335d4f98a2SYan Zheng /*
14345d4f98a2SYan Zheng  * helper to find next relocated block in reloc tree
14355d4f98a2SYan Zheng  */
14365d4f98a2SYan Zheng static noinline_for_stack
14375d4f98a2SYan Zheng int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
14385d4f98a2SYan Zheng 		       int *level)
14395d4f98a2SYan Zheng {
14405d4f98a2SYan Zheng 	struct extent_buffer *eb;
14415d4f98a2SYan Zheng 	int i;
14425d4f98a2SYan Zheng 	u64 last_snapshot;
14435d4f98a2SYan Zheng 	u32 nritems;
14445d4f98a2SYan Zheng 
14455d4f98a2SYan Zheng 	last_snapshot = btrfs_root_last_snapshot(&root->root_item);
14465d4f98a2SYan Zheng 
14475d4f98a2SYan Zheng 	for (i = 0; i < *level; i++) {
14485d4f98a2SYan Zheng 		free_extent_buffer(path->nodes[i]);
14495d4f98a2SYan Zheng 		path->nodes[i] = NULL;
14505d4f98a2SYan Zheng 	}
14515d4f98a2SYan Zheng 
14525d4f98a2SYan Zheng 	for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
14535d4f98a2SYan Zheng 		eb = path->nodes[i];
14545d4f98a2SYan Zheng 		nritems = btrfs_header_nritems(eb);
14555d4f98a2SYan Zheng 		while (path->slots[i] + 1 < nritems) {
14565d4f98a2SYan Zheng 			path->slots[i]++;
14575d4f98a2SYan Zheng 			if (btrfs_node_ptr_generation(eb, path->slots[i]) <=
14585d4f98a2SYan Zheng 			    last_snapshot)
14595d4f98a2SYan Zheng 				continue;
14605d4f98a2SYan Zheng 
14615d4f98a2SYan Zheng 			*level = i;
14625d4f98a2SYan Zheng 			return 0;
14635d4f98a2SYan Zheng 		}
14645d4f98a2SYan Zheng 		free_extent_buffer(path->nodes[i]);
14655d4f98a2SYan Zheng 		path->nodes[i] = NULL;
14665d4f98a2SYan Zheng 	}
14675d4f98a2SYan Zheng 	return 1;
14685d4f98a2SYan Zheng }
14695d4f98a2SYan Zheng 
14705d4f98a2SYan Zheng /*
14715d4f98a2SYan Zheng  * walk down reloc tree to find relocated block of lowest level
14725d4f98a2SYan Zheng  */
14735d4f98a2SYan Zheng static noinline_for_stack
14745d4f98a2SYan Zheng int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
14755d4f98a2SYan Zheng 			 int *level)
14765d4f98a2SYan Zheng {
14772ff7e61eSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
14785d4f98a2SYan Zheng 	struct extent_buffer *eb = NULL;
14795d4f98a2SYan Zheng 	int i;
14805d4f98a2SYan Zheng 	u64 bytenr;
14815d4f98a2SYan Zheng 	u64 ptr_gen = 0;
14825d4f98a2SYan Zheng 	u64 last_snapshot;
14835d4f98a2SYan Zheng 	u32 nritems;
14845d4f98a2SYan Zheng 
14855d4f98a2SYan Zheng 	last_snapshot = btrfs_root_last_snapshot(&root->root_item);
14865d4f98a2SYan Zheng 
14875d4f98a2SYan Zheng 	for (i = *level; i > 0; i--) {
1488581c1760SQu Wenruo 		struct btrfs_key first_key;
1489581c1760SQu Wenruo 
14905d4f98a2SYan Zheng 		eb = path->nodes[i];
14915d4f98a2SYan Zheng 		nritems = btrfs_header_nritems(eb);
14925d4f98a2SYan Zheng 		while (path->slots[i] < nritems) {
14935d4f98a2SYan Zheng 			ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]);
14945d4f98a2SYan Zheng 			if (ptr_gen > last_snapshot)
14955d4f98a2SYan Zheng 				break;
14965d4f98a2SYan Zheng 			path->slots[i]++;
14975d4f98a2SYan Zheng 		}
14985d4f98a2SYan Zheng 		if (path->slots[i] >= nritems) {
14995d4f98a2SYan Zheng 			if (i == *level)
15005d4f98a2SYan Zheng 				break;
15015d4f98a2SYan Zheng 			*level = i + 1;
15025d4f98a2SYan Zheng 			return 0;
15035d4f98a2SYan Zheng 		}
15045d4f98a2SYan Zheng 		if (i == 1) {
15055d4f98a2SYan Zheng 			*level = i;
15065d4f98a2SYan Zheng 			return 0;
15075d4f98a2SYan Zheng 		}
15085d4f98a2SYan Zheng 
15095d4f98a2SYan Zheng 		bytenr = btrfs_node_blockptr(eb, path->slots[i]);
1510581c1760SQu Wenruo 		btrfs_node_key_to_cpu(eb, &first_key, path->slots[i]);
1511581c1760SQu Wenruo 		eb = read_tree_block(fs_info, bytenr, ptr_gen, i - 1,
1512581c1760SQu Wenruo 				     &first_key);
151364c043deSLiu Bo 		if (IS_ERR(eb)) {
151464c043deSLiu Bo 			return PTR_ERR(eb);
151564c043deSLiu Bo 		} else if (!extent_buffer_uptodate(eb)) {
1516416bc658SJosef Bacik 			free_extent_buffer(eb);
1517416bc658SJosef Bacik 			return -EIO;
1518416bc658SJosef Bacik 		}
15195d4f98a2SYan Zheng 		BUG_ON(btrfs_header_level(eb) != i - 1);
15205d4f98a2SYan Zheng 		path->nodes[i - 1] = eb;
15215d4f98a2SYan Zheng 		path->slots[i - 1] = 0;
15225d4f98a2SYan Zheng 	}
15235d4f98a2SYan Zheng 	return 1;
15245d4f98a2SYan Zheng }
15255d4f98a2SYan Zheng 
15265d4f98a2SYan Zheng /*
15275d4f98a2SYan Zheng  * invalidate extent cache for file extents whose key in range of
15285d4f98a2SYan Zheng  * [min_key, max_key)
15295d4f98a2SYan Zheng  */
15305d4f98a2SYan Zheng static int invalidate_extent_cache(struct btrfs_root *root,
15315d4f98a2SYan Zheng 				   struct btrfs_key *min_key,
15325d4f98a2SYan Zheng 				   struct btrfs_key *max_key)
15335d4f98a2SYan Zheng {
15340b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
15355d4f98a2SYan Zheng 	struct inode *inode = NULL;
15365d4f98a2SYan Zheng 	u64 objectid;
15375d4f98a2SYan Zheng 	u64 start, end;
153833345d01SLi Zefan 	u64 ino;
15395d4f98a2SYan Zheng 
15405d4f98a2SYan Zheng 	objectid = min_key->objectid;
15415d4f98a2SYan Zheng 	while (1) {
15425d4f98a2SYan Zheng 		cond_resched();
15435d4f98a2SYan Zheng 		iput(inode);
15445d4f98a2SYan Zheng 
15455d4f98a2SYan Zheng 		if (objectid > max_key->objectid)
15465d4f98a2SYan Zheng 			break;
15475d4f98a2SYan Zheng 
15485d4f98a2SYan Zheng 		inode = find_next_inode(root, objectid);
15495d4f98a2SYan Zheng 		if (!inode)
15505d4f98a2SYan Zheng 			break;
15514a0cc7caSNikolay Borisov 		ino = btrfs_ino(BTRFS_I(inode));
15525d4f98a2SYan Zheng 
155333345d01SLi Zefan 		if (ino > max_key->objectid) {
15545d4f98a2SYan Zheng 			iput(inode);
15555d4f98a2SYan Zheng 			break;
15565d4f98a2SYan Zheng 		}
15575d4f98a2SYan Zheng 
155833345d01SLi Zefan 		objectid = ino + 1;
15595d4f98a2SYan Zheng 		if (!S_ISREG(inode->i_mode))
15605d4f98a2SYan Zheng 			continue;
15615d4f98a2SYan Zheng 
156233345d01SLi Zefan 		if (unlikely(min_key->objectid == ino)) {
15635d4f98a2SYan Zheng 			if (min_key->type > BTRFS_EXTENT_DATA_KEY)
15645d4f98a2SYan Zheng 				continue;
15655d4f98a2SYan Zheng 			if (min_key->type < BTRFS_EXTENT_DATA_KEY)
15665d4f98a2SYan Zheng 				start = 0;
15675d4f98a2SYan Zheng 			else {
15685d4f98a2SYan Zheng 				start = min_key->offset;
15690b246afaSJeff Mahoney 				WARN_ON(!IS_ALIGNED(start, fs_info->sectorsize));
15705d4f98a2SYan Zheng 			}
15715d4f98a2SYan Zheng 		} else {
15725d4f98a2SYan Zheng 			start = 0;
15735d4f98a2SYan Zheng 		}
15745d4f98a2SYan Zheng 
157533345d01SLi Zefan 		if (unlikely(max_key->objectid == ino)) {
15765d4f98a2SYan Zheng 			if (max_key->type < BTRFS_EXTENT_DATA_KEY)
15775d4f98a2SYan Zheng 				continue;
15785d4f98a2SYan Zheng 			if (max_key->type > BTRFS_EXTENT_DATA_KEY) {
15795d4f98a2SYan Zheng 				end = (u64)-1;
15805d4f98a2SYan Zheng 			} else {
15815d4f98a2SYan Zheng 				if (max_key->offset == 0)
15825d4f98a2SYan Zheng 					continue;
15835d4f98a2SYan Zheng 				end = max_key->offset;
15840b246afaSJeff Mahoney 				WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize));
15855d4f98a2SYan Zheng 				end--;
15865d4f98a2SYan Zheng 			}
15875d4f98a2SYan Zheng 		} else {
15885d4f98a2SYan Zheng 			end = (u64)-1;
15895d4f98a2SYan Zheng 		}
15905d4f98a2SYan Zheng 
15915d4f98a2SYan Zheng 		/* the lock_extent waits for readpage to complete */
1592d0082371SJeff Mahoney 		lock_extent(&BTRFS_I(inode)->io_tree, start, end);
1593dcdbc059SNikolay Borisov 		btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 1);
1594d0082371SJeff Mahoney 		unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
15955d4f98a2SYan Zheng 	}
15965d4f98a2SYan Zheng 	return 0;
15975d4f98a2SYan Zheng }
15985d4f98a2SYan Zheng 
15995d4f98a2SYan Zheng static int find_next_key(struct btrfs_path *path, int level,
16005d4f98a2SYan Zheng 			 struct btrfs_key *key)
16015d4f98a2SYan Zheng 
16025d4f98a2SYan Zheng {
16035d4f98a2SYan Zheng 	while (level < BTRFS_MAX_LEVEL) {
16045d4f98a2SYan Zheng 		if (!path->nodes[level])
16055d4f98a2SYan Zheng 			break;
16065d4f98a2SYan Zheng 		if (path->slots[level] + 1 <
16075d4f98a2SYan Zheng 		    btrfs_header_nritems(path->nodes[level])) {
16085d4f98a2SYan Zheng 			btrfs_node_key_to_cpu(path->nodes[level], key,
16095d4f98a2SYan Zheng 					      path->slots[level] + 1);
16105d4f98a2SYan Zheng 			return 0;
16115d4f98a2SYan Zheng 		}
16125d4f98a2SYan Zheng 		level++;
16135d4f98a2SYan Zheng 	}
16145d4f98a2SYan Zheng 	return 1;
16155d4f98a2SYan Zheng }
16165d4f98a2SYan Zheng 
16175d4f98a2SYan Zheng /*
1618d2311e69SQu Wenruo  * Insert current subvolume into reloc_control::dirty_subvol_roots
1619d2311e69SQu Wenruo  */
1620d2311e69SQu Wenruo static void insert_dirty_subvol(struct btrfs_trans_handle *trans,
1621d2311e69SQu Wenruo 				struct reloc_control *rc,
1622d2311e69SQu Wenruo 				struct btrfs_root *root)
1623d2311e69SQu Wenruo {
1624d2311e69SQu Wenruo 	struct btrfs_root *reloc_root = root->reloc_root;
1625d2311e69SQu Wenruo 	struct btrfs_root_item *reloc_root_item;
1626d2311e69SQu Wenruo 
1627d2311e69SQu Wenruo 	/* @root must be a subvolume tree root with a valid reloc tree */
1628d2311e69SQu Wenruo 	ASSERT(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
1629d2311e69SQu Wenruo 	ASSERT(reloc_root);
1630d2311e69SQu Wenruo 
1631d2311e69SQu Wenruo 	reloc_root_item = &reloc_root->root_item;
1632d2311e69SQu Wenruo 	memset(&reloc_root_item->drop_progress, 0,
1633d2311e69SQu Wenruo 		sizeof(reloc_root_item->drop_progress));
1634d2311e69SQu Wenruo 	reloc_root_item->drop_level = 0;
1635d2311e69SQu Wenruo 	btrfs_set_root_refs(reloc_root_item, 0);
1636d2311e69SQu Wenruo 	btrfs_update_reloc_root(trans, root);
1637d2311e69SQu Wenruo 
1638d2311e69SQu Wenruo 	if (list_empty(&root->reloc_dirty_list)) {
163900246528SJosef Bacik 		btrfs_grab_root(root);
1640d2311e69SQu Wenruo 		list_add_tail(&root->reloc_dirty_list, &rc->dirty_subvol_roots);
1641d2311e69SQu Wenruo 	}
1642d2311e69SQu Wenruo }
1643d2311e69SQu Wenruo 
1644d2311e69SQu Wenruo static int clean_dirty_subvols(struct reloc_control *rc)
1645d2311e69SQu Wenruo {
1646d2311e69SQu Wenruo 	struct btrfs_root *root;
1647d2311e69SQu Wenruo 	struct btrfs_root *next;
1648d2311e69SQu Wenruo 	int ret = 0;
164930d40577SQu Wenruo 	int ret2;
1650d2311e69SQu Wenruo 
1651d2311e69SQu Wenruo 	list_for_each_entry_safe(root, next, &rc->dirty_subvol_roots,
1652d2311e69SQu Wenruo 				 reloc_dirty_list) {
165330d40577SQu Wenruo 		if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
165430d40577SQu Wenruo 			/* Merged subvolume, cleanup its reloc root */
1655d2311e69SQu Wenruo 			struct btrfs_root *reloc_root = root->reloc_root;
1656d2311e69SQu Wenruo 
1657d2311e69SQu Wenruo 			list_del_init(&root->reloc_dirty_list);
1658d2311e69SQu Wenruo 			root->reloc_root = NULL;
16596282675eSQu Wenruo 			/*
16606282675eSQu Wenruo 			 * Need barrier to ensure clear_bit() only happens after
16616282675eSQu Wenruo 			 * root->reloc_root = NULL. Pairs with have_reloc_root.
16626282675eSQu Wenruo 			 */
16636282675eSQu Wenruo 			smp_wmb();
16641fac4a54SQu Wenruo 			clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
1665f28de8d8SJosef Bacik 			if (reloc_root) {
1666f44deb74SJosef Bacik 				/*
1667f44deb74SJosef Bacik 				 * btrfs_drop_snapshot drops our ref we hold for
1668f44deb74SJosef Bacik 				 * ->reloc_root.  If it fails however we must
1669f44deb74SJosef Bacik 				 * drop the ref ourselves.
1670f44deb74SJosef Bacik 				 */
1671f28de8d8SJosef Bacik 				ret2 = btrfs_drop_snapshot(reloc_root, 0, 1);
1672f44deb74SJosef Bacik 				if (ret2 < 0) {
1673f44deb74SJosef Bacik 					btrfs_put_root(reloc_root);
1674f44deb74SJosef Bacik 					if (!ret)
1675f28de8d8SJosef Bacik 						ret = ret2;
1676f28de8d8SJosef Bacik 				}
1677f44deb74SJosef Bacik 			}
167800246528SJosef Bacik 			btrfs_put_root(root);
167930d40577SQu Wenruo 		} else {
168030d40577SQu Wenruo 			/* Orphan reloc tree, just clean it up */
16810078a9f9SNikolay Borisov 			ret2 = btrfs_drop_snapshot(root, 0, 1);
1682f44deb74SJosef Bacik 			if (ret2 < 0) {
1683f44deb74SJosef Bacik 				btrfs_put_root(root);
1684f44deb74SJosef Bacik 				if (!ret)
168530d40577SQu Wenruo 					ret = ret2;
168630d40577SQu Wenruo 			}
1687d2311e69SQu Wenruo 		}
1688f44deb74SJosef Bacik 	}
1689d2311e69SQu Wenruo 	return ret;
1690d2311e69SQu Wenruo }
1691d2311e69SQu Wenruo 
1692d2311e69SQu Wenruo /*
16935d4f98a2SYan Zheng  * merge the relocated tree blocks in reloc tree with corresponding
16945d4f98a2SYan Zheng  * fs tree.
16955d4f98a2SYan Zheng  */
16965d4f98a2SYan Zheng static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
16975d4f98a2SYan Zheng 					       struct btrfs_root *root)
16985d4f98a2SYan Zheng {
16990b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
17005d4f98a2SYan Zheng 	struct btrfs_key key;
17015d4f98a2SYan Zheng 	struct btrfs_key next_key;
17029e6a0c52SJosef Bacik 	struct btrfs_trans_handle *trans = NULL;
17035d4f98a2SYan Zheng 	struct btrfs_root *reloc_root;
17045d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
17055d4f98a2SYan Zheng 	struct btrfs_path *path;
17063fd0a558SYan, Zheng 	struct extent_buffer *leaf;
17075d4f98a2SYan Zheng 	int level;
17085d4f98a2SYan Zheng 	int max_level;
17095d4f98a2SYan Zheng 	int replaced = 0;
17105d4f98a2SYan Zheng 	int ret;
17115d4f98a2SYan Zheng 	int err = 0;
17123fd0a558SYan, Zheng 	u32 min_reserved;
17135d4f98a2SYan Zheng 
17145d4f98a2SYan Zheng 	path = btrfs_alloc_path();
17155d4f98a2SYan Zheng 	if (!path)
17165d4f98a2SYan Zheng 		return -ENOMEM;
1717e4058b54SDavid Sterba 	path->reada = READA_FORWARD;
17185d4f98a2SYan Zheng 
17195d4f98a2SYan Zheng 	reloc_root = root->reloc_root;
17205d4f98a2SYan Zheng 	root_item = &reloc_root->root_item;
17215d4f98a2SYan Zheng 
17225d4f98a2SYan Zheng 	if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
17235d4f98a2SYan Zheng 		level = btrfs_root_level(root_item);
172467439dadSDavid Sterba 		atomic_inc(&reloc_root->node->refs);
17255d4f98a2SYan Zheng 		path->nodes[level] = reloc_root->node;
17265d4f98a2SYan Zheng 		path->slots[level] = 0;
17275d4f98a2SYan Zheng 	} else {
17285d4f98a2SYan Zheng 		btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
17295d4f98a2SYan Zheng 
17305d4f98a2SYan Zheng 		level = root_item->drop_level;
17315d4f98a2SYan Zheng 		BUG_ON(level == 0);
17325d4f98a2SYan Zheng 		path->lowest_level = level;
17335d4f98a2SYan Zheng 		ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0);
173433c66f43SYan Zheng 		path->lowest_level = 0;
17355d4f98a2SYan Zheng 		if (ret < 0) {
17365d4f98a2SYan Zheng 			btrfs_free_path(path);
17375d4f98a2SYan Zheng 			return ret;
17385d4f98a2SYan Zheng 		}
17395d4f98a2SYan Zheng 
17405d4f98a2SYan Zheng 		btrfs_node_key_to_cpu(path->nodes[level], &next_key,
17415d4f98a2SYan Zheng 				      path->slots[level]);
17425d4f98a2SYan Zheng 		WARN_ON(memcmp(&key, &next_key, sizeof(key)));
17435d4f98a2SYan Zheng 
17445d4f98a2SYan Zheng 		btrfs_unlock_up_safe(path, 0);
17455d4f98a2SYan Zheng 	}
17465d4f98a2SYan Zheng 
17470b246afaSJeff Mahoney 	min_reserved = fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
17485d4f98a2SYan Zheng 	memset(&next_key, 0, sizeof(next_key));
17495d4f98a2SYan Zheng 
17505d4f98a2SYan Zheng 	while (1) {
175108e007d2SMiao Xie 		ret = btrfs_block_rsv_refill(root, rc->block_rsv, min_reserved,
175208e007d2SMiao Xie 					     BTRFS_RESERVE_FLUSH_ALL);
17533fd0a558SYan, Zheng 		if (ret) {
17549e6a0c52SJosef Bacik 			err = ret;
17559e6a0c52SJosef Bacik 			goto out;
17563fd0a558SYan, Zheng 		}
17579e6a0c52SJosef Bacik 		trans = btrfs_start_transaction(root, 0);
17589e6a0c52SJosef Bacik 		if (IS_ERR(trans)) {
17599e6a0c52SJosef Bacik 			err = PTR_ERR(trans);
17609e6a0c52SJosef Bacik 			trans = NULL;
17619e6a0c52SJosef Bacik 			goto out;
17629e6a0c52SJosef Bacik 		}
17632abc726aSJosef Bacik 
17642abc726aSJosef Bacik 		/*
17652abc726aSJosef Bacik 		 * At this point we no longer have a reloc_control, so we can't
17662abc726aSJosef Bacik 		 * depend on btrfs_init_reloc_root to update our last_trans.
17672abc726aSJosef Bacik 		 *
17682abc726aSJosef Bacik 		 * But that's ok, we started the trans handle on our
17692abc726aSJosef Bacik 		 * corresponding fs_root, which means it's been added to the
17702abc726aSJosef Bacik 		 * dirty list.  At commit time we'll still call
17712abc726aSJosef Bacik 		 * btrfs_update_reloc_root() and update our root item
17722abc726aSJosef Bacik 		 * appropriately.
17732abc726aSJosef Bacik 		 */
17742abc726aSJosef Bacik 		reloc_root->last_trans = trans->transid;
17759e6a0c52SJosef Bacik 		trans->block_rsv = rc->block_rsv;
17763fd0a558SYan, Zheng 
17773fd0a558SYan, Zheng 		replaced = 0;
17785d4f98a2SYan Zheng 		max_level = level;
17795d4f98a2SYan Zheng 
17805d4f98a2SYan Zheng 		ret = walk_down_reloc_tree(reloc_root, path, &level);
17815d4f98a2SYan Zheng 		if (ret < 0) {
17825d4f98a2SYan Zheng 			err = ret;
17835d4f98a2SYan Zheng 			goto out;
17845d4f98a2SYan Zheng 		}
17855d4f98a2SYan Zheng 		if (ret > 0)
17865d4f98a2SYan Zheng 			break;
17875d4f98a2SYan Zheng 
17885d4f98a2SYan Zheng 		if (!find_next_key(path, level, &key) &&
17895d4f98a2SYan Zheng 		    btrfs_comp_cpu_keys(&next_key, &key) >= 0) {
17905d4f98a2SYan Zheng 			ret = 0;
17915d4f98a2SYan Zheng 		} else {
17923d0174f7SQu Wenruo 			ret = replace_path(trans, rc, root, reloc_root, path,
17933fd0a558SYan, Zheng 					   &next_key, level, max_level);
17945d4f98a2SYan Zheng 		}
17955d4f98a2SYan Zheng 		if (ret < 0) {
17965d4f98a2SYan Zheng 			err = ret;
17975d4f98a2SYan Zheng 			goto out;
17985d4f98a2SYan Zheng 		}
17995d4f98a2SYan Zheng 
18005d4f98a2SYan Zheng 		if (ret > 0) {
18015d4f98a2SYan Zheng 			level = ret;
18025d4f98a2SYan Zheng 			btrfs_node_key_to_cpu(path->nodes[level], &key,
18035d4f98a2SYan Zheng 					      path->slots[level]);
18045d4f98a2SYan Zheng 			replaced = 1;
18055d4f98a2SYan Zheng 		}
18065d4f98a2SYan Zheng 
18075d4f98a2SYan Zheng 		ret = walk_up_reloc_tree(reloc_root, path, &level);
18085d4f98a2SYan Zheng 		if (ret > 0)
18095d4f98a2SYan Zheng 			break;
18105d4f98a2SYan Zheng 
18115d4f98a2SYan Zheng 		BUG_ON(level == 0);
18125d4f98a2SYan Zheng 		/*
18135d4f98a2SYan Zheng 		 * save the merging progress in the drop_progress.
18145d4f98a2SYan Zheng 		 * this is OK since root refs == 1 in this case.
18155d4f98a2SYan Zheng 		 */
18165d4f98a2SYan Zheng 		btrfs_node_key(path->nodes[level], &root_item->drop_progress,
18175d4f98a2SYan Zheng 			       path->slots[level]);
18185d4f98a2SYan Zheng 		root_item->drop_level = level;
18195d4f98a2SYan Zheng 
18203a45bb20SJeff Mahoney 		btrfs_end_transaction_throttle(trans);
18219e6a0c52SJosef Bacik 		trans = NULL;
18225d4f98a2SYan Zheng 
18232ff7e61eSJeff Mahoney 		btrfs_btree_balance_dirty(fs_info);
18245d4f98a2SYan Zheng 
18255d4f98a2SYan Zheng 		if (replaced && rc->stage == UPDATE_DATA_PTRS)
18265d4f98a2SYan Zheng 			invalidate_extent_cache(root, &key, &next_key);
18275d4f98a2SYan Zheng 	}
18285d4f98a2SYan Zheng 
18295d4f98a2SYan Zheng 	/*
18305d4f98a2SYan Zheng 	 * handle the case only one block in the fs tree need to be
18315d4f98a2SYan Zheng 	 * relocated and the block is tree root.
18325d4f98a2SYan Zheng 	 */
18335d4f98a2SYan Zheng 	leaf = btrfs_lock_root_node(root);
18345d4f98a2SYan Zheng 	ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf);
18355d4f98a2SYan Zheng 	btrfs_tree_unlock(leaf);
18365d4f98a2SYan Zheng 	free_extent_buffer(leaf);
18375d4f98a2SYan Zheng 	if (ret < 0)
18385d4f98a2SYan Zheng 		err = ret;
18395d4f98a2SYan Zheng out:
18405d4f98a2SYan Zheng 	btrfs_free_path(path);
18415d4f98a2SYan Zheng 
1842d2311e69SQu Wenruo 	if (err == 0)
1843d2311e69SQu Wenruo 		insert_dirty_subvol(trans, rc, root);
18445d4f98a2SYan Zheng 
18459e6a0c52SJosef Bacik 	if (trans)
18463a45bb20SJeff Mahoney 		btrfs_end_transaction_throttle(trans);
18475d4f98a2SYan Zheng 
18482ff7e61eSJeff Mahoney 	btrfs_btree_balance_dirty(fs_info);
18495d4f98a2SYan Zheng 
18505d4f98a2SYan Zheng 	if (replaced && rc->stage == UPDATE_DATA_PTRS)
18515d4f98a2SYan Zheng 		invalidate_extent_cache(root, &key, &next_key);
18525d4f98a2SYan Zheng 
18535d4f98a2SYan Zheng 	return err;
18545d4f98a2SYan Zheng }
18555d4f98a2SYan Zheng 
18563fd0a558SYan, Zheng static noinline_for_stack
18573fd0a558SYan, Zheng int prepare_to_merge(struct reloc_control *rc, int err)
18585d4f98a2SYan Zheng {
18593fd0a558SYan, Zheng 	struct btrfs_root *root = rc->extent_root;
18600b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
18613fd0a558SYan, Zheng 	struct btrfs_root *reloc_root;
18625d4f98a2SYan Zheng 	struct btrfs_trans_handle *trans;
18633fd0a558SYan, Zheng 	LIST_HEAD(reloc_roots);
18643fd0a558SYan, Zheng 	u64 num_bytes = 0;
18653fd0a558SYan, Zheng 	int ret;
18663fd0a558SYan, Zheng 
18670b246afaSJeff Mahoney 	mutex_lock(&fs_info->reloc_mutex);
18680b246afaSJeff Mahoney 	rc->merging_rsv_size += fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
18693fd0a558SYan, Zheng 	rc->merging_rsv_size += rc->nodes_relocated * 2;
18700b246afaSJeff Mahoney 	mutex_unlock(&fs_info->reloc_mutex);
18717585717fSChris Mason 
18723fd0a558SYan, Zheng again:
18733fd0a558SYan, Zheng 	if (!err) {
18743fd0a558SYan, Zheng 		num_bytes = rc->merging_rsv_size;
187508e007d2SMiao Xie 		ret = btrfs_block_rsv_add(root, rc->block_rsv, num_bytes,
187608e007d2SMiao Xie 					  BTRFS_RESERVE_FLUSH_ALL);
18773fd0a558SYan, Zheng 		if (ret)
18783fd0a558SYan, Zheng 			err = ret;
18793fd0a558SYan, Zheng 	}
18803fd0a558SYan, Zheng 
18817a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(rc->extent_root);
18823612b495STsutomu Itoh 	if (IS_ERR(trans)) {
18833612b495STsutomu Itoh 		if (!err)
18842ff7e61eSJeff Mahoney 			btrfs_block_rsv_release(fs_info, rc->block_rsv,
188563f018beSNikolay Borisov 						num_bytes, NULL);
18863612b495STsutomu Itoh 		return PTR_ERR(trans);
18873612b495STsutomu Itoh 	}
18883fd0a558SYan, Zheng 
18893fd0a558SYan, Zheng 	if (!err) {
18903fd0a558SYan, Zheng 		if (num_bytes != rc->merging_rsv_size) {
18913a45bb20SJeff Mahoney 			btrfs_end_transaction(trans);
18922ff7e61eSJeff Mahoney 			btrfs_block_rsv_release(fs_info, rc->block_rsv,
189363f018beSNikolay Borisov 						num_bytes, NULL);
18943fd0a558SYan, Zheng 			goto again;
18953fd0a558SYan, Zheng 		}
18963fd0a558SYan, Zheng 	}
18973fd0a558SYan, Zheng 
18983fd0a558SYan, Zheng 	rc->merge_reloc_tree = 1;
18993fd0a558SYan, Zheng 
19003fd0a558SYan, Zheng 	while (!list_empty(&rc->reloc_roots)) {
19013fd0a558SYan, Zheng 		reloc_root = list_entry(rc->reloc_roots.next,
19023fd0a558SYan, Zheng 					struct btrfs_root, root_list);
19033fd0a558SYan, Zheng 		list_del_init(&reloc_root->root_list);
19043fd0a558SYan, Zheng 
19050b246afaSJeff Mahoney 		root = read_fs_root(fs_info, reloc_root->root_key.offset);
19063fd0a558SYan, Zheng 		BUG_ON(IS_ERR(root));
19073fd0a558SYan, Zheng 		BUG_ON(root->reloc_root != reloc_root);
19083fd0a558SYan, Zheng 
19093fd0a558SYan, Zheng 		/*
19103fd0a558SYan, Zheng 		 * set reference count to 1, so btrfs_recover_relocation
19113fd0a558SYan, Zheng 		 * knows it should resumes merging
19123fd0a558SYan, Zheng 		 */
19133fd0a558SYan, Zheng 		if (!err)
19143fd0a558SYan, Zheng 			btrfs_set_root_refs(&reloc_root->root_item, 1);
19153fd0a558SYan, Zheng 		btrfs_update_reloc_root(trans, root);
19163fd0a558SYan, Zheng 
19173fd0a558SYan, Zheng 		list_add(&reloc_root->root_list, &reloc_roots);
191800246528SJosef Bacik 		btrfs_put_root(root);
19193fd0a558SYan, Zheng 	}
19203fd0a558SYan, Zheng 
19213fd0a558SYan, Zheng 	list_splice(&reloc_roots, &rc->reloc_roots);
19223fd0a558SYan, Zheng 
19233fd0a558SYan, Zheng 	if (!err)
19243a45bb20SJeff Mahoney 		btrfs_commit_transaction(trans);
19253fd0a558SYan, Zheng 	else
19263a45bb20SJeff Mahoney 		btrfs_end_transaction(trans);
19273fd0a558SYan, Zheng 	return err;
19283fd0a558SYan, Zheng }
19293fd0a558SYan, Zheng 
19303fd0a558SYan, Zheng static noinline_for_stack
1931aca1bba6SLiu Bo void free_reloc_roots(struct list_head *list)
1932aca1bba6SLiu Bo {
1933aca1bba6SLiu Bo 	struct btrfs_root *reloc_root;
1934aca1bba6SLiu Bo 
1935aca1bba6SLiu Bo 	while (!list_empty(list)) {
1936aca1bba6SLiu Bo 		reloc_root = list_entry(list->next, struct btrfs_root,
1937aca1bba6SLiu Bo 					root_list);
1938bb166d72SNaohiro Aota 		__del_reloc_root(reloc_root);
1939aca1bba6SLiu Bo 	}
1940aca1bba6SLiu Bo }
1941aca1bba6SLiu Bo 
1942aca1bba6SLiu Bo static noinline_for_stack
194394404e82SDavid Sterba void merge_reloc_roots(struct reloc_control *rc)
19443fd0a558SYan, Zheng {
19450b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
19465d4f98a2SYan Zheng 	struct btrfs_root *root;
19475d4f98a2SYan Zheng 	struct btrfs_root *reloc_root;
19483fd0a558SYan, Zheng 	LIST_HEAD(reloc_roots);
19493fd0a558SYan, Zheng 	int found = 0;
1950aca1bba6SLiu Bo 	int ret = 0;
19513fd0a558SYan, Zheng again:
19523fd0a558SYan, Zheng 	root = rc->extent_root;
19537585717fSChris Mason 
19547585717fSChris Mason 	/*
19557585717fSChris Mason 	 * this serializes us with btrfs_record_root_in_transaction,
19567585717fSChris Mason 	 * we have to make sure nobody is in the middle of
19577585717fSChris Mason 	 * adding their roots to the list while we are
19587585717fSChris Mason 	 * doing this splice
19597585717fSChris Mason 	 */
19600b246afaSJeff Mahoney 	mutex_lock(&fs_info->reloc_mutex);
19613fd0a558SYan, Zheng 	list_splice_init(&rc->reloc_roots, &reloc_roots);
19620b246afaSJeff Mahoney 	mutex_unlock(&fs_info->reloc_mutex);
19635d4f98a2SYan Zheng 
19643fd0a558SYan, Zheng 	while (!list_empty(&reloc_roots)) {
19653fd0a558SYan, Zheng 		found = 1;
19663fd0a558SYan, Zheng 		reloc_root = list_entry(reloc_roots.next,
19673fd0a558SYan, Zheng 					struct btrfs_root, root_list);
19685d4f98a2SYan Zheng 
19695d4f98a2SYan Zheng 		if (btrfs_root_refs(&reloc_root->root_item) > 0) {
19700b246afaSJeff Mahoney 			root = read_fs_root(fs_info,
19715d4f98a2SYan Zheng 					    reloc_root->root_key.offset);
19725d4f98a2SYan Zheng 			BUG_ON(IS_ERR(root));
19735d4f98a2SYan Zheng 			BUG_ON(root->reloc_root != reloc_root);
19745d4f98a2SYan Zheng 
19753fd0a558SYan, Zheng 			ret = merge_reloc_root(rc, root);
197600246528SJosef Bacik 			btrfs_put_root(root);
1977b37b39cdSJosef Bacik 			if (ret) {
197825e293c2SWang Shilong 				if (list_empty(&reloc_root->root_list))
197925e293c2SWang Shilong 					list_add_tail(&reloc_root->root_list,
198025e293c2SWang Shilong 						      &reloc_roots);
1981aca1bba6SLiu Bo 				goto out;
1982b37b39cdSJosef Bacik 			}
19833fd0a558SYan, Zheng 		} else {
19843fd0a558SYan, Zheng 			list_del_init(&reloc_root->root_list);
198530d40577SQu Wenruo 			/* Don't forget to queue this reloc root for cleanup */
198630d40577SQu Wenruo 			list_add_tail(&reloc_root->reloc_dirty_list,
198730d40577SQu Wenruo 				      &rc->dirty_subvol_roots);
19883fd0a558SYan, Zheng 		}
19895d4f98a2SYan Zheng 	}
19905d4f98a2SYan Zheng 
19913fd0a558SYan, Zheng 	if (found) {
19923fd0a558SYan, Zheng 		found = 0;
19933fd0a558SYan, Zheng 		goto again;
19945d4f98a2SYan Zheng 	}
1995aca1bba6SLiu Bo out:
1996aca1bba6SLiu Bo 	if (ret) {
19970b246afaSJeff Mahoney 		btrfs_handle_fs_error(fs_info, ret, NULL);
1998aca1bba6SLiu Bo 		if (!list_empty(&reloc_roots))
1999aca1bba6SLiu Bo 			free_reloc_roots(&reloc_roots);
2000467bb1d2SWang Shilong 
2001467bb1d2SWang Shilong 		/* new reloc root may be added */
20020b246afaSJeff Mahoney 		mutex_lock(&fs_info->reloc_mutex);
2003467bb1d2SWang Shilong 		list_splice_init(&rc->reloc_roots, &reloc_roots);
20040b246afaSJeff Mahoney 		mutex_unlock(&fs_info->reloc_mutex);
2005467bb1d2SWang Shilong 		if (!list_empty(&reloc_roots))
2006467bb1d2SWang Shilong 			free_reloc_roots(&reloc_roots);
2007aca1bba6SLiu Bo 	}
2008aca1bba6SLiu Bo 
20097b7b7431SJosef Bacik 	/*
20107b7b7431SJosef Bacik 	 * We used to have
20117b7b7431SJosef Bacik 	 *
20127b7b7431SJosef Bacik 	 * BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
20137b7b7431SJosef Bacik 	 *
20147b7b7431SJosef Bacik 	 * here, but it's wrong.  If we fail to start the transaction in
20157b7b7431SJosef Bacik 	 * prepare_to_merge() we will have only 0 ref reloc roots, none of which
20167b7b7431SJosef Bacik 	 * have actually been removed from the reloc_root_tree rb tree.  This is
20177b7b7431SJosef Bacik 	 * fine because we're bailing here, and we hold a reference on the root
20187b7b7431SJosef Bacik 	 * for the list that holds it, so these roots will be cleaned up when we
20197b7b7431SJosef Bacik 	 * do the reloc_dirty_list afterwards.  Meanwhile the root->reloc_root
20207b7b7431SJosef Bacik 	 * will be cleaned up on unmount.
20217b7b7431SJosef Bacik 	 *
20227b7b7431SJosef Bacik 	 * The remaining nodes will be cleaned up by free_reloc_control.
20237b7b7431SJosef Bacik 	 */
20245d4f98a2SYan Zheng }
20255d4f98a2SYan Zheng 
20265d4f98a2SYan Zheng static void free_block_list(struct rb_root *blocks)
20275d4f98a2SYan Zheng {
20285d4f98a2SYan Zheng 	struct tree_block *block;
20295d4f98a2SYan Zheng 	struct rb_node *rb_node;
20305d4f98a2SYan Zheng 	while ((rb_node = rb_first(blocks))) {
20315d4f98a2SYan Zheng 		block = rb_entry(rb_node, struct tree_block, rb_node);
20325d4f98a2SYan Zheng 		rb_erase(rb_node, blocks);
20335d4f98a2SYan Zheng 		kfree(block);
20345d4f98a2SYan Zheng 	}
20355d4f98a2SYan Zheng }
20365d4f98a2SYan Zheng 
20375d4f98a2SYan Zheng static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans,
20385d4f98a2SYan Zheng 				      struct btrfs_root *reloc_root)
20395d4f98a2SYan Zheng {
20400b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = reloc_root->fs_info;
20415d4f98a2SYan Zheng 	struct btrfs_root *root;
2042442b1ac5SJosef Bacik 	int ret;
20435d4f98a2SYan Zheng 
20445d4f98a2SYan Zheng 	if (reloc_root->last_trans == trans->transid)
20455d4f98a2SYan Zheng 		return 0;
20465d4f98a2SYan Zheng 
20470b246afaSJeff Mahoney 	root = read_fs_root(fs_info, reloc_root->root_key.offset);
20485d4f98a2SYan Zheng 	BUG_ON(IS_ERR(root));
20495d4f98a2SYan Zheng 	BUG_ON(root->reloc_root != reloc_root);
2050442b1ac5SJosef Bacik 	ret = btrfs_record_root_in_trans(trans, root);
205100246528SJosef Bacik 	btrfs_put_root(root);
20525d4f98a2SYan Zheng 
2053442b1ac5SJosef Bacik 	return ret;
20545d4f98a2SYan Zheng }
20555d4f98a2SYan Zheng 
20563fd0a558SYan, Zheng static noinline_for_stack
20573fd0a558SYan, Zheng struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,
20583fd0a558SYan, Zheng 				     struct reloc_control *rc,
2059a26195a5SQu Wenruo 				     struct btrfs_backref_node *node,
2060a26195a5SQu Wenruo 				     struct btrfs_backref_edge *edges[])
20615d4f98a2SYan Zheng {
2062a26195a5SQu Wenruo 	struct btrfs_backref_node *next;
20635d4f98a2SYan Zheng 	struct btrfs_root *root;
20643fd0a558SYan, Zheng 	int index = 0;
20653fd0a558SYan, Zheng 
20665d4f98a2SYan Zheng 	next = node;
20675d4f98a2SYan Zheng 	while (1) {
20685d4f98a2SYan Zheng 		cond_resched();
20695d4f98a2SYan Zheng 		next = walk_up_backref(next, edges, &index);
20705d4f98a2SYan Zheng 		root = next->root;
20713fd0a558SYan, Zheng 		BUG_ON(!root);
207227cdeb70SMiao Xie 		BUG_ON(!test_bit(BTRFS_ROOT_REF_COWS, &root->state));
20735d4f98a2SYan Zheng 
20745d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
20755d4f98a2SYan Zheng 			record_reloc_root_in_trans(trans, root);
20765d4f98a2SYan Zheng 			break;
20775d4f98a2SYan Zheng 		}
20785d4f98a2SYan Zheng 
20795d4f98a2SYan Zheng 		btrfs_record_root_in_trans(trans, root);
20803fd0a558SYan, Zheng 		root = root->reloc_root;
20813fd0a558SYan, Zheng 
20823fd0a558SYan, Zheng 		if (next->new_bytenr != root->node->start) {
20833fd0a558SYan, Zheng 			BUG_ON(next->new_bytenr);
20843fd0a558SYan, Zheng 			BUG_ON(!list_empty(&next->list));
20853fd0a558SYan, Zheng 			next->new_bytenr = root->node->start;
208600246528SJosef Bacik 			btrfs_put_root(next->root);
208700246528SJosef Bacik 			next->root = btrfs_grab_root(root);
20880b530bc5SJosef Bacik 			ASSERT(next->root);
20893fd0a558SYan, Zheng 			list_add_tail(&next->list,
20903fd0a558SYan, Zheng 				      &rc->backref_cache.changed);
20919569cc20SQu Wenruo 			mark_block_processed(rc, next);
20925d4f98a2SYan Zheng 			break;
20935d4f98a2SYan Zheng 		}
20945d4f98a2SYan Zheng 
20953fd0a558SYan, Zheng 		WARN_ON(1);
20965d4f98a2SYan Zheng 		root = NULL;
20975d4f98a2SYan Zheng 		next = walk_down_backref(edges, &index);
20985d4f98a2SYan Zheng 		if (!next || next->level <= node->level)
20995d4f98a2SYan Zheng 			break;
21005d4f98a2SYan Zheng 	}
21013fd0a558SYan, Zheng 	if (!root)
21023fd0a558SYan, Zheng 		return NULL;
21035d4f98a2SYan Zheng 
21043fd0a558SYan, Zheng 	next = node;
21053fd0a558SYan, Zheng 	/* setup backref node path for btrfs_reloc_cow_block */
21063fd0a558SYan, Zheng 	while (1) {
21073fd0a558SYan, Zheng 		rc->backref_cache.path[next->level] = next;
21083fd0a558SYan, Zheng 		if (--index < 0)
21093fd0a558SYan, Zheng 			break;
21103fd0a558SYan, Zheng 		next = edges[index]->node[UPPER];
21113fd0a558SYan, Zheng 	}
21125d4f98a2SYan Zheng 	return root;
21135d4f98a2SYan Zheng }
21145d4f98a2SYan Zheng 
21153fd0a558SYan, Zheng /*
21163fd0a558SYan, Zheng  * select a tree root for relocation. return NULL if the block
21173fd0a558SYan, Zheng  * is reference counted. we should use do_relocation() in this
21183fd0a558SYan, Zheng  * case. return a tree root pointer if the block isn't reference
21193fd0a558SYan, Zheng  * counted. return -ENOENT if the block is root of reloc tree.
21203fd0a558SYan, Zheng  */
21215d4f98a2SYan Zheng static noinline_for_stack
2122a26195a5SQu Wenruo struct btrfs_root *select_one_root(struct btrfs_backref_node *node)
21235d4f98a2SYan Zheng {
2124a26195a5SQu Wenruo 	struct btrfs_backref_node *next;
21253fd0a558SYan, Zheng 	struct btrfs_root *root;
21263fd0a558SYan, Zheng 	struct btrfs_root *fs_root = NULL;
2127a26195a5SQu Wenruo 	struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
21283fd0a558SYan, Zheng 	int index = 0;
21293fd0a558SYan, Zheng 
21303fd0a558SYan, Zheng 	next = node;
21313fd0a558SYan, Zheng 	while (1) {
21323fd0a558SYan, Zheng 		cond_resched();
21333fd0a558SYan, Zheng 		next = walk_up_backref(next, edges, &index);
21343fd0a558SYan, Zheng 		root = next->root;
21353fd0a558SYan, Zheng 		BUG_ON(!root);
21363fd0a558SYan, Zheng 
213725985edcSLucas De Marchi 		/* no other choice for non-references counted tree */
213827cdeb70SMiao Xie 		if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
21393fd0a558SYan, Zheng 			return root;
21403fd0a558SYan, Zheng 
21413fd0a558SYan, Zheng 		if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)
21423fd0a558SYan, Zheng 			fs_root = root;
21433fd0a558SYan, Zheng 
21443fd0a558SYan, Zheng 		if (next != node)
21453fd0a558SYan, Zheng 			return NULL;
21463fd0a558SYan, Zheng 
21473fd0a558SYan, Zheng 		next = walk_down_backref(edges, &index);
21483fd0a558SYan, Zheng 		if (!next || next->level <= node->level)
21493fd0a558SYan, Zheng 			break;
21503fd0a558SYan, Zheng 	}
21513fd0a558SYan, Zheng 
21523fd0a558SYan, Zheng 	if (!fs_root)
21533fd0a558SYan, Zheng 		return ERR_PTR(-ENOENT);
21543fd0a558SYan, Zheng 	return fs_root;
21555d4f98a2SYan Zheng }
21565d4f98a2SYan Zheng 
21575d4f98a2SYan Zheng static noinline_for_stack
21583fd0a558SYan, Zheng u64 calcu_metadata_size(struct reloc_control *rc,
2159a26195a5SQu Wenruo 			struct btrfs_backref_node *node, int reserve)
21605d4f98a2SYan Zheng {
21610b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2162a26195a5SQu Wenruo 	struct btrfs_backref_node *next = node;
2163a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
2164a26195a5SQu Wenruo 	struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
21653fd0a558SYan, Zheng 	u64 num_bytes = 0;
21663fd0a558SYan, Zheng 	int index = 0;
21675d4f98a2SYan Zheng 
21683fd0a558SYan, Zheng 	BUG_ON(reserve && node->processed);
21693fd0a558SYan, Zheng 
21703fd0a558SYan, Zheng 	while (next) {
21713fd0a558SYan, Zheng 		cond_resched();
21725d4f98a2SYan Zheng 		while (1) {
21733fd0a558SYan, Zheng 			if (next->processed && (reserve || next != node))
21745d4f98a2SYan Zheng 				break;
21755d4f98a2SYan Zheng 
21760b246afaSJeff Mahoney 			num_bytes += fs_info->nodesize;
21773fd0a558SYan, Zheng 
21783fd0a558SYan, Zheng 			if (list_empty(&next->upper))
21793fd0a558SYan, Zheng 				break;
21803fd0a558SYan, Zheng 
21813fd0a558SYan, Zheng 			edge = list_entry(next->upper.next,
2182a26195a5SQu Wenruo 					struct btrfs_backref_edge, list[LOWER]);
21833fd0a558SYan, Zheng 			edges[index++] = edge;
21843fd0a558SYan, Zheng 			next = edge->node[UPPER];
21855d4f98a2SYan Zheng 		}
21863fd0a558SYan, Zheng 		next = walk_down_backref(edges, &index);
21873fd0a558SYan, Zheng 	}
21883fd0a558SYan, Zheng 	return num_bytes;
21893fd0a558SYan, Zheng }
21903fd0a558SYan, Zheng 
21913fd0a558SYan, Zheng static int reserve_metadata_space(struct btrfs_trans_handle *trans,
21923fd0a558SYan, Zheng 				  struct reloc_control *rc,
2193a26195a5SQu Wenruo 				  struct btrfs_backref_node *node)
21943fd0a558SYan, Zheng {
21953fd0a558SYan, Zheng 	struct btrfs_root *root = rc->extent_root;
2196da17066cSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
21973fd0a558SYan, Zheng 	u64 num_bytes;
21983fd0a558SYan, Zheng 	int ret;
21990647bf56SWang Shilong 	u64 tmp;
22003fd0a558SYan, Zheng 
22013fd0a558SYan, Zheng 	num_bytes = calcu_metadata_size(rc, node, 1) * 2;
22023fd0a558SYan, Zheng 
22033fd0a558SYan, Zheng 	trans->block_rsv = rc->block_rsv;
22040647bf56SWang Shilong 	rc->reserved_bytes += num_bytes;
22058ca17f0fSJosef Bacik 
22068ca17f0fSJosef Bacik 	/*
22078ca17f0fSJosef Bacik 	 * We are under a transaction here so we can only do limited flushing.
22088ca17f0fSJosef Bacik 	 * If we get an enospc just kick back -EAGAIN so we know to drop the
22098ca17f0fSJosef Bacik 	 * transaction and try to refill when we can flush all the things.
22108ca17f0fSJosef Bacik 	 */
22110647bf56SWang Shilong 	ret = btrfs_block_rsv_refill(root, rc->block_rsv, num_bytes,
22128ca17f0fSJosef Bacik 				BTRFS_RESERVE_FLUSH_LIMIT);
22133fd0a558SYan, Zheng 	if (ret) {
2214da17066cSJeff Mahoney 		tmp = fs_info->nodesize * RELOCATION_RESERVED_NODES;
22150647bf56SWang Shilong 		while (tmp <= rc->reserved_bytes)
22160647bf56SWang Shilong 			tmp <<= 1;
22170647bf56SWang Shilong 		/*
22180647bf56SWang Shilong 		 * only one thread can access block_rsv at this point,
22190647bf56SWang Shilong 		 * so we don't need hold lock to protect block_rsv.
22200647bf56SWang Shilong 		 * we expand more reservation size here to allow enough
222152042d8eSAndrea Gelmini 		 * space for relocation and we will return earlier in
22220647bf56SWang Shilong 		 * enospc case.
22230647bf56SWang Shilong 		 */
2224da17066cSJeff Mahoney 		rc->block_rsv->size = tmp + fs_info->nodesize *
22250647bf56SWang Shilong 				      RELOCATION_RESERVED_NODES;
22268ca17f0fSJosef Bacik 		return -EAGAIN;
22273fd0a558SYan, Zheng 	}
22283fd0a558SYan, Zheng 
22293fd0a558SYan, Zheng 	return 0;
22303fd0a558SYan, Zheng }
22313fd0a558SYan, Zheng 
22325d4f98a2SYan Zheng /*
22335d4f98a2SYan Zheng  * relocate a block tree, and then update pointers in upper level
22345d4f98a2SYan Zheng  * blocks that reference the block to point to the new location.
22355d4f98a2SYan Zheng  *
22365d4f98a2SYan Zheng  * if called by link_to_upper, the block has already been relocated.
22375d4f98a2SYan Zheng  * in that case this function just updates pointers.
22385d4f98a2SYan Zheng  */
22395d4f98a2SYan Zheng static int do_relocation(struct btrfs_trans_handle *trans,
22403fd0a558SYan, Zheng 			 struct reloc_control *rc,
2241a26195a5SQu Wenruo 			 struct btrfs_backref_node *node,
22425d4f98a2SYan Zheng 			 struct btrfs_key *key,
22435d4f98a2SYan Zheng 			 struct btrfs_path *path, int lowest)
22445d4f98a2SYan Zheng {
22452ff7e61eSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2246a26195a5SQu Wenruo 	struct btrfs_backref_node *upper;
2247a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
2248a26195a5SQu Wenruo 	struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
22495d4f98a2SYan Zheng 	struct btrfs_root *root;
22505d4f98a2SYan Zheng 	struct extent_buffer *eb;
22515d4f98a2SYan Zheng 	u32 blocksize;
22525d4f98a2SYan Zheng 	u64 bytenr;
22535d4f98a2SYan Zheng 	u64 generation;
22545d4f98a2SYan Zheng 	int slot;
22555d4f98a2SYan Zheng 	int ret;
22565d4f98a2SYan Zheng 	int err = 0;
22575d4f98a2SYan Zheng 
22585d4f98a2SYan Zheng 	BUG_ON(lowest && node->eb);
22595d4f98a2SYan Zheng 
22605d4f98a2SYan Zheng 	path->lowest_level = node->level + 1;
22613fd0a558SYan, Zheng 	rc->backref_cache.path[node->level] = node;
22625d4f98a2SYan Zheng 	list_for_each_entry(edge, &node->upper, list[LOWER]) {
2263581c1760SQu Wenruo 		struct btrfs_key first_key;
226482fa113fSQu Wenruo 		struct btrfs_ref ref = { 0 };
2265581c1760SQu Wenruo 
22665d4f98a2SYan Zheng 		cond_resched();
22675d4f98a2SYan Zheng 
22685d4f98a2SYan Zheng 		upper = edge->node[UPPER];
2269dc4103f9SWang Shilong 		root = select_reloc_root(trans, rc, upper, edges);
22703fd0a558SYan, Zheng 		BUG_ON(!root);
22715d4f98a2SYan Zheng 
22723fd0a558SYan, Zheng 		if (upper->eb && !upper->locked) {
22733fd0a558SYan, Zheng 			if (!lowest) {
22743fd0a558SYan, Zheng 				ret = btrfs_bin_search(upper->eb, key,
22753fd0a558SYan, Zheng 						       upper->level, &slot);
2276cbca7d59SFilipe Manana 				if (ret < 0) {
2277cbca7d59SFilipe Manana 					err = ret;
2278cbca7d59SFilipe Manana 					goto next;
2279cbca7d59SFilipe Manana 				}
22803fd0a558SYan, Zheng 				BUG_ON(ret);
22813fd0a558SYan, Zheng 				bytenr = btrfs_node_blockptr(upper->eb, slot);
22823fd0a558SYan, Zheng 				if (node->eb->start == bytenr)
22833fd0a558SYan, Zheng 					goto next;
22843fd0a558SYan, Zheng 			}
2285b0fe7078SQu Wenruo 			btrfs_backref_drop_node_buffer(upper);
22863fd0a558SYan, Zheng 		}
22875d4f98a2SYan Zheng 
22885d4f98a2SYan Zheng 		if (!upper->eb) {
22895d4f98a2SYan Zheng 			ret = btrfs_search_slot(trans, root, key, path, 0, 1);
22903561b9dbSLiu Bo 			if (ret) {
22913561b9dbSLiu Bo 				if (ret < 0)
22925d4f98a2SYan Zheng 					err = ret;
22933561b9dbSLiu Bo 				else
22943561b9dbSLiu Bo 					err = -ENOENT;
22953561b9dbSLiu Bo 
22963561b9dbSLiu Bo 				btrfs_release_path(path);
22975d4f98a2SYan Zheng 				break;
22985d4f98a2SYan Zheng 			}
22995d4f98a2SYan Zheng 
23003fd0a558SYan, Zheng 			if (!upper->eb) {
23013fd0a558SYan, Zheng 				upper->eb = path->nodes[upper->level];
23023fd0a558SYan, Zheng 				path->nodes[upper->level] = NULL;
23033fd0a558SYan, Zheng 			} else {
23043fd0a558SYan, Zheng 				BUG_ON(upper->eb != path->nodes[upper->level]);
23053fd0a558SYan, Zheng 			}
23063fd0a558SYan, Zheng 
23073fd0a558SYan, Zheng 			upper->locked = 1;
23083fd0a558SYan, Zheng 			path->locks[upper->level] = 0;
23093fd0a558SYan, Zheng 
23105d4f98a2SYan Zheng 			slot = path->slots[upper->level];
2311b3b4aa74SDavid Sterba 			btrfs_release_path(path);
23125d4f98a2SYan Zheng 		} else {
23135d4f98a2SYan Zheng 			ret = btrfs_bin_search(upper->eb, key, upper->level,
23145d4f98a2SYan Zheng 					       &slot);
2315cbca7d59SFilipe Manana 			if (ret < 0) {
2316cbca7d59SFilipe Manana 				err = ret;
2317cbca7d59SFilipe Manana 				goto next;
2318cbca7d59SFilipe Manana 			}
23195d4f98a2SYan Zheng 			BUG_ON(ret);
23205d4f98a2SYan Zheng 		}
23215d4f98a2SYan Zheng 
23225d4f98a2SYan Zheng 		bytenr = btrfs_node_blockptr(upper->eb, slot);
23233fd0a558SYan, Zheng 		if (lowest) {
23244547f4d8SLiu Bo 			if (bytenr != node->bytenr) {
23254547f4d8SLiu Bo 				btrfs_err(root->fs_info,
23264547f4d8SLiu Bo 		"lowest leaf/node mismatch: bytenr %llu node->bytenr %llu slot %d upper %llu",
23274547f4d8SLiu Bo 					  bytenr, node->bytenr, slot,
23284547f4d8SLiu Bo 					  upper->eb->start);
23294547f4d8SLiu Bo 				err = -EIO;
23304547f4d8SLiu Bo 				goto next;
23314547f4d8SLiu Bo 			}
23325d4f98a2SYan Zheng 		} else {
23333fd0a558SYan, Zheng 			if (node->eb->start == bytenr)
23343fd0a558SYan, Zheng 				goto next;
23355d4f98a2SYan Zheng 		}
23365d4f98a2SYan Zheng 
2337da17066cSJeff Mahoney 		blocksize = root->fs_info->nodesize;
23385d4f98a2SYan Zheng 		generation = btrfs_node_ptr_generation(upper->eb, slot);
2339581c1760SQu Wenruo 		btrfs_node_key_to_cpu(upper->eb, &first_key, slot);
2340581c1760SQu Wenruo 		eb = read_tree_block(fs_info, bytenr, generation,
2341581c1760SQu Wenruo 				     upper->level - 1, &first_key);
234264c043deSLiu Bo 		if (IS_ERR(eb)) {
234364c043deSLiu Bo 			err = PTR_ERR(eb);
234464c043deSLiu Bo 			goto next;
234564c043deSLiu Bo 		} else if (!extent_buffer_uptodate(eb)) {
2346416bc658SJosef Bacik 			free_extent_buffer(eb);
234797d9a8a4STsutomu Itoh 			err = -EIO;
234897d9a8a4STsutomu Itoh 			goto next;
234997d9a8a4STsutomu Itoh 		}
23505d4f98a2SYan Zheng 		btrfs_tree_lock(eb);
23518bead258SDavid Sterba 		btrfs_set_lock_blocking_write(eb);
23525d4f98a2SYan Zheng 
23535d4f98a2SYan Zheng 		if (!node->eb) {
23545d4f98a2SYan Zheng 			ret = btrfs_cow_block(trans, root, eb, upper->eb,
23555d4f98a2SYan Zheng 					      slot, &eb);
23563fd0a558SYan, Zheng 			btrfs_tree_unlock(eb);
23573fd0a558SYan, Zheng 			free_extent_buffer(eb);
23585d4f98a2SYan Zheng 			if (ret < 0) {
23595d4f98a2SYan Zheng 				err = ret;
23603fd0a558SYan, Zheng 				goto next;
23615d4f98a2SYan Zheng 			}
23623fd0a558SYan, Zheng 			BUG_ON(node->eb != eb);
23635d4f98a2SYan Zheng 		} else {
23645d4f98a2SYan Zheng 			btrfs_set_node_blockptr(upper->eb, slot,
23655d4f98a2SYan Zheng 						node->eb->start);
23665d4f98a2SYan Zheng 			btrfs_set_node_ptr_generation(upper->eb, slot,
23675d4f98a2SYan Zheng 						      trans->transid);
23685d4f98a2SYan Zheng 			btrfs_mark_buffer_dirty(upper->eb);
23695d4f98a2SYan Zheng 
237082fa113fSQu Wenruo 			btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF,
23715d4f98a2SYan Zheng 					       node->eb->start, blocksize,
237282fa113fSQu Wenruo 					       upper->eb->start);
237382fa113fSQu Wenruo 			ref.real_root = root->root_key.objectid;
237482fa113fSQu Wenruo 			btrfs_init_tree_ref(&ref, node->level,
237582fa113fSQu Wenruo 					    btrfs_header_owner(upper->eb));
237682fa113fSQu Wenruo 			ret = btrfs_inc_extent_ref(trans, &ref);
23775d4f98a2SYan Zheng 			BUG_ON(ret);
23785d4f98a2SYan Zheng 
23795d4f98a2SYan Zheng 			ret = btrfs_drop_subtree(trans, root, eb, upper->eb);
23805d4f98a2SYan Zheng 			BUG_ON(ret);
23815d4f98a2SYan Zheng 		}
23823fd0a558SYan, Zheng next:
23833fd0a558SYan, Zheng 		if (!upper->pending)
2384b0fe7078SQu Wenruo 			btrfs_backref_drop_node_buffer(upper);
23853fd0a558SYan, Zheng 		else
2386b0fe7078SQu Wenruo 			btrfs_backref_unlock_node_buffer(upper);
23873fd0a558SYan, Zheng 		if (err)
23883fd0a558SYan, Zheng 			break;
23895d4f98a2SYan Zheng 	}
23903fd0a558SYan, Zheng 
23913fd0a558SYan, Zheng 	if (!err && node->pending) {
2392b0fe7078SQu Wenruo 		btrfs_backref_drop_node_buffer(node);
23933fd0a558SYan, Zheng 		list_move_tail(&node->list, &rc->backref_cache.changed);
23943fd0a558SYan, Zheng 		node->pending = 0;
23955d4f98a2SYan Zheng 	}
23963fd0a558SYan, Zheng 
23975d4f98a2SYan Zheng 	path->lowest_level = 0;
23983fd0a558SYan, Zheng 	BUG_ON(err == -ENOSPC);
23995d4f98a2SYan Zheng 	return err;
24005d4f98a2SYan Zheng }
24015d4f98a2SYan Zheng 
24025d4f98a2SYan Zheng static int link_to_upper(struct btrfs_trans_handle *trans,
24033fd0a558SYan, Zheng 			 struct reloc_control *rc,
2404a26195a5SQu Wenruo 			 struct btrfs_backref_node *node,
24055d4f98a2SYan Zheng 			 struct btrfs_path *path)
24065d4f98a2SYan Zheng {
24075d4f98a2SYan Zheng 	struct btrfs_key key;
24085d4f98a2SYan Zheng 
24095d4f98a2SYan Zheng 	btrfs_node_key_to_cpu(node->eb, &key, 0);
24103fd0a558SYan, Zheng 	return do_relocation(trans, rc, node, &key, path, 0);
24115d4f98a2SYan Zheng }
24125d4f98a2SYan Zheng 
24135d4f98a2SYan Zheng static int finish_pending_nodes(struct btrfs_trans_handle *trans,
24143fd0a558SYan, Zheng 				struct reloc_control *rc,
24153fd0a558SYan, Zheng 				struct btrfs_path *path, int err)
24165d4f98a2SYan Zheng {
24173fd0a558SYan, Zheng 	LIST_HEAD(list);
2418a26195a5SQu Wenruo 	struct btrfs_backref_cache *cache = &rc->backref_cache;
2419a26195a5SQu Wenruo 	struct btrfs_backref_node *node;
24205d4f98a2SYan Zheng 	int level;
24215d4f98a2SYan Zheng 	int ret;
24225d4f98a2SYan Zheng 
24235d4f98a2SYan Zheng 	for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
24245d4f98a2SYan Zheng 		while (!list_empty(&cache->pending[level])) {
24255d4f98a2SYan Zheng 			node = list_entry(cache->pending[level].next,
2426a26195a5SQu Wenruo 					  struct btrfs_backref_node, list);
24273fd0a558SYan, Zheng 			list_move_tail(&node->list, &list);
24283fd0a558SYan, Zheng 			BUG_ON(!node->pending);
24295d4f98a2SYan Zheng 
24303fd0a558SYan, Zheng 			if (!err) {
24313fd0a558SYan, Zheng 				ret = link_to_upper(trans, rc, node, path);
24325d4f98a2SYan Zheng 				if (ret < 0)
24335d4f98a2SYan Zheng 					err = ret;
24345d4f98a2SYan Zheng 			}
24355d4f98a2SYan Zheng 		}
24363fd0a558SYan, Zheng 		list_splice_init(&list, &cache->pending[level]);
24373fd0a558SYan, Zheng 	}
24385d4f98a2SYan Zheng 	return err;
24395d4f98a2SYan Zheng }
24405d4f98a2SYan Zheng 
24415d4f98a2SYan Zheng /*
24425d4f98a2SYan Zheng  * mark a block and all blocks directly/indirectly reference the block
24435d4f98a2SYan Zheng  * as processed.
24445d4f98a2SYan Zheng  */
24455d4f98a2SYan Zheng static void update_processed_blocks(struct reloc_control *rc,
2446a26195a5SQu Wenruo 				    struct btrfs_backref_node *node)
24475d4f98a2SYan Zheng {
2448a26195a5SQu Wenruo 	struct btrfs_backref_node *next = node;
2449a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
2450a26195a5SQu Wenruo 	struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
24515d4f98a2SYan Zheng 	int index = 0;
24525d4f98a2SYan Zheng 
24535d4f98a2SYan Zheng 	while (next) {
24545d4f98a2SYan Zheng 		cond_resched();
24555d4f98a2SYan Zheng 		while (1) {
24565d4f98a2SYan Zheng 			if (next->processed)
24575d4f98a2SYan Zheng 				break;
24585d4f98a2SYan Zheng 
24599569cc20SQu Wenruo 			mark_block_processed(rc, next);
24605d4f98a2SYan Zheng 
24615d4f98a2SYan Zheng 			if (list_empty(&next->upper))
24625d4f98a2SYan Zheng 				break;
24635d4f98a2SYan Zheng 
24645d4f98a2SYan Zheng 			edge = list_entry(next->upper.next,
2465a26195a5SQu Wenruo 					struct btrfs_backref_edge, list[LOWER]);
24665d4f98a2SYan Zheng 			edges[index++] = edge;
24675d4f98a2SYan Zheng 			next = edge->node[UPPER];
24685d4f98a2SYan Zheng 		}
24695d4f98a2SYan Zheng 		next = walk_down_backref(edges, &index);
24705d4f98a2SYan Zheng 	}
24715d4f98a2SYan Zheng }
24725d4f98a2SYan Zheng 
24737476dfdaSDavid Sterba static int tree_block_processed(u64 bytenr, struct reloc_control *rc)
24745d4f98a2SYan Zheng {
2475da17066cSJeff Mahoney 	u32 blocksize = rc->extent_root->fs_info->nodesize;
24767476dfdaSDavid Sterba 
24775d4f98a2SYan Zheng 	if (test_range_bit(&rc->processed_blocks, bytenr,
24789655d298SChris Mason 			   bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL))
24795d4f98a2SYan Zheng 		return 1;
24805d4f98a2SYan Zheng 	return 0;
24815d4f98a2SYan Zheng }
24825d4f98a2SYan Zheng 
24832ff7e61eSJeff Mahoney static int get_tree_block_key(struct btrfs_fs_info *fs_info,
24845d4f98a2SYan Zheng 			      struct tree_block *block)
24855d4f98a2SYan Zheng {
24865d4f98a2SYan Zheng 	struct extent_buffer *eb;
24875d4f98a2SYan Zheng 
2488581c1760SQu Wenruo 	eb = read_tree_block(fs_info, block->bytenr, block->key.offset,
2489581c1760SQu Wenruo 			     block->level, NULL);
249064c043deSLiu Bo 	if (IS_ERR(eb)) {
249164c043deSLiu Bo 		return PTR_ERR(eb);
249264c043deSLiu Bo 	} else if (!extent_buffer_uptodate(eb)) {
2493416bc658SJosef Bacik 		free_extent_buffer(eb);
2494416bc658SJosef Bacik 		return -EIO;
2495416bc658SJosef Bacik 	}
24965d4f98a2SYan Zheng 	if (block->level == 0)
24975d4f98a2SYan Zheng 		btrfs_item_key_to_cpu(eb, &block->key, 0);
24985d4f98a2SYan Zheng 	else
24995d4f98a2SYan Zheng 		btrfs_node_key_to_cpu(eb, &block->key, 0);
25005d4f98a2SYan Zheng 	free_extent_buffer(eb);
25015d4f98a2SYan Zheng 	block->key_ready = 1;
25025d4f98a2SYan Zheng 	return 0;
25035d4f98a2SYan Zheng }
25045d4f98a2SYan Zheng 
25055d4f98a2SYan Zheng /*
25065d4f98a2SYan Zheng  * helper function to relocate a tree block
25075d4f98a2SYan Zheng  */
25085d4f98a2SYan Zheng static int relocate_tree_block(struct btrfs_trans_handle *trans,
25095d4f98a2SYan Zheng 				struct reloc_control *rc,
2510a26195a5SQu Wenruo 				struct btrfs_backref_node *node,
25115d4f98a2SYan Zheng 				struct btrfs_key *key,
25125d4f98a2SYan Zheng 				struct btrfs_path *path)
25135d4f98a2SYan Zheng {
25145d4f98a2SYan Zheng 	struct btrfs_root *root;
25153fd0a558SYan, Zheng 	int ret = 0;
25165d4f98a2SYan Zheng 
25173fd0a558SYan, Zheng 	if (!node)
25185d4f98a2SYan Zheng 		return 0;
25193fd0a558SYan, Zheng 
25205f6b2e5cSJosef Bacik 	/*
25215f6b2e5cSJosef Bacik 	 * If we fail here we want to drop our backref_node because we are going
25225f6b2e5cSJosef Bacik 	 * to start over and regenerate the tree for it.
25235f6b2e5cSJosef Bacik 	 */
25245f6b2e5cSJosef Bacik 	ret = reserve_metadata_space(trans, rc, node);
25255f6b2e5cSJosef Bacik 	if (ret)
25265f6b2e5cSJosef Bacik 		goto out;
25275f6b2e5cSJosef Bacik 
25283fd0a558SYan, Zheng 	BUG_ON(node->processed);
2529147d256eSZhaolei 	root = select_one_root(node);
25303fd0a558SYan, Zheng 	if (root == ERR_PTR(-ENOENT)) {
25313fd0a558SYan, Zheng 		update_processed_blocks(rc, node);
25323fd0a558SYan, Zheng 		goto out;
25335d4f98a2SYan Zheng 	}
25345d4f98a2SYan Zheng 
25353fd0a558SYan, Zheng 	if (root) {
253627cdeb70SMiao Xie 		if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
25373fd0a558SYan, Zheng 			BUG_ON(node->new_bytenr);
25383fd0a558SYan, Zheng 			BUG_ON(!list_empty(&node->list));
25393fd0a558SYan, Zheng 			btrfs_record_root_in_trans(trans, root);
25403fd0a558SYan, Zheng 			root = root->reloc_root;
25413fd0a558SYan, Zheng 			node->new_bytenr = root->node->start;
254200246528SJosef Bacik 			btrfs_put_root(node->root);
254300246528SJosef Bacik 			node->root = btrfs_grab_root(root);
25440b530bc5SJosef Bacik 			ASSERT(node->root);
25453fd0a558SYan, Zheng 			list_add_tail(&node->list, &rc->backref_cache.changed);
25463fd0a558SYan, Zheng 		} else {
25475d4f98a2SYan Zheng 			path->lowest_level = node->level;
25485d4f98a2SYan Zheng 			ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2549b3b4aa74SDavid Sterba 			btrfs_release_path(path);
25503fd0a558SYan, Zheng 			if (ret > 0)
25515d4f98a2SYan Zheng 				ret = 0;
25523fd0a558SYan, Zheng 		}
25533fd0a558SYan, Zheng 		if (!ret)
25543fd0a558SYan, Zheng 			update_processed_blocks(rc, node);
25553fd0a558SYan, Zheng 	} else {
25563fd0a558SYan, Zheng 		ret = do_relocation(trans, rc, node, key, path, 1);
25573fd0a558SYan, Zheng 	}
25585d4f98a2SYan Zheng out:
25590647bf56SWang Shilong 	if (ret || node->level == 0 || node->cowonly)
2560023acb07SQu Wenruo 		btrfs_backref_cleanup_node(&rc->backref_cache, node);
25615d4f98a2SYan Zheng 	return ret;
25625d4f98a2SYan Zheng }
25635d4f98a2SYan Zheng 
25645d4f98a2SYan Zheng /*
25655d4f98a2SYan Zheng  * relocate a list of blocks
25665d4f98a2SYan Zheng  */
25675d4f98a2SYan Zheng static noinline_for_stack
25685d4f98a2SYan Zheng int relocate_tree_blocks(struct btrfs_trans_handle *trans,
25695d4f98a2SYan Zheng 			 struct reloc_control *rc, struct rb_root *blocks)
25705d4f98a2SYan Zheng {
25712ff7e61eSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2572a26195a5SQu Wenruo 	struct btrfs_backref_node *node;
25735d4f98a2SYan Zheng 	struct btrfs_path *path;
25745d4f98a2SYan Zheng 	struct tree_block *block;
257598ff7b94SQu Wenruo 	struct tree_block *next;
25765d4f98a2SYan Zheng 	int ret;
25775d4f98a2SYan Zheng 	int err = 0;
25785d4f98a2SYan Zheng 
25795d4f98a2SYan Zheng 	path = btrfs_alloc_path();
2580e1a12670SLiu Bo 	if (!path) {
2581e1a12670SLiu Bo 		err = -ENOMEM;
258234c2b290SDavid Sterba 		goto out_free_blocks;
2583e1a12670SLiu Bo 	}
25845d4f98a2SYan Zheng 
258598ff7b94SQu Wenruo 	/* Kick in readahead for tree blocks with missing keys */
258698ff7b94SQu Wenruo 	rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
25875d4f98a2SYan Zheng 		if (!block->key_ready)
25882ff7e61eSJeff Mahoney 			readahead_tree_block(fs_info, block->bytenr);
25895d4f98a2SYan Zheng 	}
25905d4f98a2SYan Zheng 
259198ff7b94SQu Wenruo 	/* Get first keys */
259298ff7b94SQu Wenruo 	rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
259334c2b290SDavid Sterba 		if (!block->key_ready) {
25942ff7e61eSJeff Mahoney 			err = get_tree_block_key(fs_info, block);
259534c2b290SDavid Sterba 			if (err)
259634c2b290SDavid Sterba 				goto out_free_path;
259734c2b290SDavid Sterba 		}
25985d4f98a2SYan Zheng 	}
25995d4f98a2SYan Zheng 
260098ff7b94SQu Wenruo 	/* Do tree relocation */
260198ff7b94SQu Wenruo 	rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
26023fd0a558SYan, Zheng 		node = build_backref_tree(rc, &block->key,
26035d4f98a2SYan Zheng 					  block->level, block->bytenr);
26045d4f98a2SYan Zheng 		if (IS_ERR(node)) {
26055d4f98a2SYan Zheng 			err = PTR_ERR(node);
26065d4f98a2SYan Zheng 			goto out;
26075d4f98a2SYan Zheng 		}
26085d4f98a2SYan Zheng 
26095d4f98a2SYan Zheng 		ret = relocate_tree_block(trans, rc, node, &block->key,
26105d4f98a2SYan Zheng 					  path);
26115d4f98a2SYan Zheng 		if (ret < 0) {
26125d4f98a2SYan Zheng 			err = ret;
261350dbbb71SJosef Bacik 			break;
26145d4f98a2SYan Zheng 		}
26155d4f98a2SYan Zheng 	}
26165d4f98a2SYan Zheng out:
26173fd0a558SYan, Zheng 	err = finish_pending_nodes(trans, rc, path, err);
26185d4f98a2SYan Zheng 
261934c2b290SDavid Sterba out_free_path:
26205d4f98a2SYan Zheng 	btrfs_free_path(path);
262134c2b290SDavid Sterba out_free_blocks:
2622e1a12670SLiu Bo 	free_block_list(blocks);
26235d4f98a2SYan Zheng 	return err;
26245d4f98a2SYan Zheng }
26255d4f98a2SYan Zheng 
26265d4f98a2SYan Zheng static noinline_for_stack
2627efa56464SYan, Zheng int prealloc_file_extent_cluster(struct inode *inode,
2628efa56464SYan, Zheng 				 struct file_extent_cluster *cluster)
2629efa56464SYan, Zheng {
2630efa56464SYan, Zheng 	u64 alloc_hint = 0;
2631efa56464SYan, Zheng 	u64 start;
2632efa56464SYan, Zheng 	u64 end;
2633efa56464SYan, Zheng 	u64 offset = BTRFS_I(inode)->index_cnt;
2634efa56464SYan, Zheng 	u64 num_bytes;
2635efa56464SYan, Zheng 	int nr = 0;
2636efa56464SYan, Zheng 	int ret = 0;
2637dcb40c19SWang Xiaoguang 	u64 prealloc_start = cluster->start - offset;
2638dcb40c19SWang Xiaoguang 	u64 prealloc_end = cluster->end - offset;
263918513091SWang Xiaoguang 	u64 cur_offset;
2640364ecf36SQu Wenruo 	struct extent_changeset *data_reserved = NULL;
2641efa56464SYan, Zheng 
2642efa56464SYan, Zheng 	BUG_ON(cluster->start != cluster->boundary[0]);
26435955102cSAl Viro 	inode_lock(inode);
2644efa56464SYan, Zheng 
2645364ecf36SQu Wenruo 	ret = btrfs_check_data_free_space(inode, &data_reserved, prealloc_start,
2646dcb40c19SWang Xiaoguang 					  prealloc_end + 1 - prealloc_start);
2647efa56464SYan, Zheng 	if (ret)
2648efa56464SYan, Zheng 		goto out;
2649efa56464SYan, Zheng 
265018513091SWang Xiaoguang 	cur_offset = prealloc_start;
2651efa56464SYan, Zheng 	while (nr < cluster->nr) {
2652efa56464SYan, Zheng 		start = cluster->boundary[nr] - offset;
2653efa56464SYan, Zheng 		if (nr + 1 < cluster->nr)
2654efa56464SYan, Zheng 			end = cluster->boundary[nr + 1] - 1 - offset;
2655efa56464SYan, Zheng 		else
2656efa56464SYan, Zheng 			end = cluster->end - offset;
2657efa56464SYan, Zheng 
2658d0082371SJeff Mahoney 		lock_extent(&BTRFS_I(inode)->io_tree, start, end);
2659efa56464SYan, Zheng 		num_bytes = end + 1 - start;
266018513091SWang Xiaoguang 		if (cur_offset < start)
2661bc42bda2SQu Wenruo 			btrfs_free_reserved_data_space(inode, data_reserved,
2662bc42bda2SQu Wenruo 					cur_offset, start - cur_offset);
2663efa56464SYan, Zheng 		ret = btrfs_prealloc_file_range(inode, 0, start,
2664efa56464SYan, Zheng 						num_bytes, num_bytes,
2665efa56464SYan, Zheng 						end + 1, &alloc_hint);
266618513091SWang Xiaoguang 		cur_offset = end + 1;
2667d0082371SJeff Mahoney 		unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
2668efa56464SYan, Zheng 		if (ret)
2669efa56464SYan, Zheng 			break;
2670efa56464SYan, Zheng 		nr++;
2671efa56464SYan, Zheng 	}
267218513091SWang Xiaoguang 	if (cur_offset < prealloc_end)
2673bc42bda2SQu Wenruo 		btrfs_free_reserved_data_space(inode, data_reserved,
2674bc42bda2SQu Wenruo 				cur_offset, prealloc_end + 1 - cur_offset);
2675efa56464SYan, Zheng out:
26765955102cSAl Viro 	inode_unlock(inode);
2677364ecf36SQu Wenruo 	extent_changeset_free(data_reserved);
2678efa56464SYan, Zheng 	return ret;
2679efa56464SYan, Zheng }
2680efa56464SYan, Zheng 
2681efa56464SYan, Zheng static noinline_for_stack
26820257bb82SYan, Zheng int setup_extent_mapping(struct inode *inode, u64 start, u64 end,
26830257bb82SYan, Zheng 			 u64 block_start)
26845d4f98a2SYan Zheng {
26855d4f98a2SYan Zheng 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
26865d4f98a2SYan Zheng 	struct extent_map *em;
26870257bb82SYan, Zheng 	int ret = 0;
26885d4f98a2SYan Zheng 
2689172ddd60SDavid Sterba 	em = alloc_extent_map();
26900257bb82SYan, Zheng 	if (!em)
26910257bb82SYan, Zheng 		return -ENOMEM;
26920257bb82SYan, Zheng 
26935d4f98a2SYan Zheng 	em->start = start;
26940257bb82SYan, Zheng 	em->len = end + 1 - start;
26950257bb82SYan, Zheng 	em->block_len = em->len;
26960257bb82SYan, Zheng 	em->block_start = block_start;
26975d4f98a2SYan Zheng 	set_bit(EXTENT_FLAG_PINNED, &em->flags);
26985d4f98a2SYan Zheng 
2699d0082371SJeff Mahoney 	lock_extent(&BTRFS_I(inode)->io_tree, start, end);
27005d4f98a2SYan Zheng 	while (1) {
2701890871beSChris Mason 		write_lock(&em_tree->lock);
270209a2a8f9SJosef Bacik 		ret = add_extent_mapping(em_tree, em, 0);
2703890871beSChris Mason 		write_unlock(&em_tree->lock);
27045d4f98a2SYan Zheng 		if (ret != -EEXIST) {
27055d4f98a2SYan Zheng 			free_extent_map(em);
27065d4f98a2SYan Zheng 			break;
27075d4f98a2SYan Zheng 		}
2708dcdbc059SNikolay Borisov 		btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
27095d4f98a2SYan Zheng 	}
2710d0082371SJeff Mahoney 	unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
27110257bb82SYan, Zheng 	return ret;
27120257bb82SYan, Zheng }
27135d4f98a2SYan Zheng 
2714726a3421SQu Wenruo /*
2715726a3421SQu Wenruo  * Allow error injection to test balance cancellation
2716726a3421SQu Wenruo  */
2717726a3421SQu Wenruo int btrfs_should_cancel_balance(struct btrfs_fs_info *fs_info)
2718726a3421SQu Wenruo {
2719726a3421SQu Wenruo 	return atomic_read(&fs_info->balance_cancel_req);
2720726a3421SQu Wenruo }
2721726a3421SQu Wenruo ALLOW_ERROR_INJECTION(btrfs_should_cancel_balance, TRUE);
2722726a3421SQu Wenruo 
27230257bb82SYan, Zheng static int relocate_file_extent_cluster(struct inode *inode,
27240257bb82SYan, Zheng 					struct file_extent_cluster *cluster)
27250257bb82SYan, Zheng {
27262ff7e61eSJeff Mahoney 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
27270257bb82SYan, Zheng 	u64 page_start;
27280257bb82SYan, Zheng 	u64 page_end;
27290257bb82SYan, Zheng 	u64 offset = BTRFS_I(inode)->index_cnt;
27300257bb82SYan, Zheng 	unsigned long index;
27310257bb82SYan, Zheng 	unsigned long last_index;
27320257bb82SYan, Zheng 	struct page *page;
27330257bb82SYan, Zheng 	struct file_ra_state *ra;
27343b16a4e3SJosef Bacik 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
27350257bb82SYan, Zheng 	int nr = 0;
27360257bb82SYan, Zheng 	int ret = 0;
27370257bb82SYan, Zheng 
27380257bb82SYan, Zheng 	if (!cluster->nr)
27390257bb82SYan, Zheng 		return 0;
27400257bb82SYan, Zheng 
27410257bb82SYan, Zheng 	ra = kzalloc(sizeof(*ra), GFP_NOFS);
27420257bb82SYan, Zheng 	if (!ra)
27430257bb82SYan, Zheng 		return -ENOMEM;
27440257bb82SYan, Zheng 
2745efa56464SYan, Zheng 	ret = prealloc_file_extent_cluster(inode, cluster);
27460257bb82SYan, Zheng 	if (ret)
2747efa56464SYan, Zheng 		goto out;
27480257bb82SYan, Zheng 
27490257bb82SYan, Zheng 	file_ra_state_init(ra, inode->i_mapping);
27500257bb82SYan, Zheng 
2751efa56464SYan, Zheng 	ret = setup_extent_mapping(inode, cluster->start - offset,
2752efa56464SYan, Zheng 				   cluster->end - offset, cluster->start);
2753efa56464SYan, Zheng 	if (ret)
2754efa56464SYan, Zheng 		goto out;
2755efa56464SYan, Zheng 
275609cbfeafSKirill A. Shutemov 	index = (cluster->start - offset) >> PAGE_SHIFT;
275709cbfeafSKirill A. Shutemov 	last_index = (cluster->end - offset) >> PAGE_SHIFT;
27580257bb82SYan, Zheng 	while (index <= last_index) {
27599f3db423SNikolay Borisov 		ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode),
27609f3db423SNikolay Borisov 				PAGE_SIZE);
2761efa56464SYan, Zheng 		if (ret)
2762efa56464SYan, Zheng 			goto out;
2763efa56464SYan, Zheng 
27640257bb82SYan, Zheng 		page = find_lock_page(inode->i_mapping, index);
27650257bb82SYan, Zheng 		if (!page) {
27660257bb82SYan, Zheng 			page_cache_sync_readahead(inode->i_mapping,
27670257bb82SYan, Zheng 						  ra, NULL, index,
27680257bb82SYan, Zheng 						  last_index + 1 - index);
2769a94733d0SJosef Bacik 			page = find_or_create_page(inode->i_mapping, index,
27703b16a4e3SJosef Bacik 						   mask);
27710257bb82SYan, Zheng 			if (!page) {
2772691fa059SNikolay Borisov 				btrfs_delalloc_release_metadata(BTRFS_I(inode),
277343b18595SQu Wenruo 							PAGE_SIZE, true);
277444db1216SFilipe Manana 				btrfs_delalloc_release_extents(BTRFS_I(inode),
27758702ba93SQu Wenruo 							PAGE_SIZE);
27760257bb82SYan, Zheng 				ret = -ENOMEM;
2777efa56464SYan, Zheng 				goto out;
27780257bb82SYan, Zheng 			}
27790257bb82SYan, Zheng 		}
27800257bb82SYan, Zheng 
27810257bb82SYan, Zheng 		if (PageReadahead(page)) {
27820257bb82SYan, Zheng 			page_cache_async_readahead(inode->i_mapping,
27830257bb82SYan, Zheng 						   ra, NULL, page, index,
27840257bb82SYan, Zheng 						   last_index + 1 - index);
27850257bb82SYan, Zheng 		}
27860257bb82SYan, Zheng 
27870257bb82SYan, Zheng 		if (!PageUptodate(page)) {
27880257bb82SYan, Zheng 			btrfs_readpage(NULL, page);
27890257bb82SYan, Zheng 			lock_page(page);
27900257bb82SYan, Zheng 			if (!PageUptodate(page)) {
27910257bb82SYan, Zheng 				unlock_page(page);
279209cbfeafSKirill A. Shutemov 				put_page(page);
2793691fa059SNikolay Borisov 				btrfs_delalloc_release_metadata(BTRFS_I(inode),
279443b18595SQu Wenruo 							PAGE_SIZE, true);
27958b62f87bSJosef Bacik 				btrfs_delalloc_release_extents(BTRFS_I(inode),
27968702ba93SQu Wenruo 							       PAGE_SIZE);
27970257bb82SYan, Zheng 				ret = -EIO;
2798efa56464SYan, Zheng 				goto out;
27990257bb82SYan, Zheng 			}
28000257bb82SYan, Zheng 		}
28010257bb82SYan, Zheng 
28024eee4fa4SMiao Xie 		page_start = page_offset(page);
280309cbfeafSKirill A. Shutemov 		page_end = page_start + PAGE_SIZE - 1;
28040257bb82SYan, Zheng 
2805d0082371SJeff Mahoney 		lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end);
28060257bb82SYan, Zheng 
28070257bb82SYan, Zheng 		set_page_extent_mapped(page);
28080257bb82SYan, Zheng 
28090257bb82SYan, Zheng 		if (nr < cluster->nr &&
28100257bb82SYan, Zheng 		    page_start + offset == cluster->boundary[nr]) {
28110257bb82SYan, Zheng 			set_extent_bits(&BTRFS_I(inode)->io_tree,
28120257bb82SYan, Zheng 					page_start, page_end,
2813ceeb0ae7SDavid Sterba 					EXTENT_BOUNDARY);
28140257bb82SYan, Zheng 			nr++;
28150257bb82SYan, Zheng 		}
28160257bb82SYan, Zheng 
2817765f3cebSNikolay Borisov 		ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2818330a5827SNikolay Borisov 						NULL);
2819765f3cebSNikolay Borisov 		if (ret) {
2820765f3cebSNikolay Borisov 			unlock_page(page);
2821765f3cebSNikolay Borisov 			put_page(page);
2822765f3cebSNikolay Borisov 			btrfs_delalloc_release_metadata(BTRFS_I(inode),
282343b18595SQu Wenruo 							 PAGE_SIZE, true);
2824765f3cebSNikolay Borisov 			btrfs_delalloc_release_extents(BTRFS_I(inode),
28258702ba93SQu Wenruo 			                               PAGE_SIZE);
2826765f3cebSNikolay Borisov 
2827765f3cebSNikolay Borisov 			clear_extent_bits(&BTRFS_I(inode)->io_tree,
2828765f3cebSNikolay Borisov 					  page_start, page_end,
2829765f3cebSNikolay Borisov 					  EXTENT_LOCKED | EXTENT_BOUNDARY);
2830765f3cebSNikolay Borisov 			goto out;
2831765f3cebSNikolay Borisov 
2832765f3cebSNikolay Borisov 		}
28330257bb82SYan, Zheng 		set_page_dirty(page);
28340257bb82SYan, Zheng 
28350257bb82SYan, Zheng 		unlock_extent(&BTRFS_I(inode)->io_tree,
2836d0082371SJeff Mahoney 			      page_start, page_end);
28370257bb82SYan, Zheng 		unlock_page(page);
283809cbfeafSKirill A. Shutemov 		put_page(page);
28390257bb82SYan, Zheng 
28400257bb82SYan, Zheng 		index++;
28418702ba93SQu Wenruo 		btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
2842efa56464SYan, Zheng 		balance_dirty_pages_ratelimited(inode->i_mapping);
28432ff7e61eSJeff Mahoney 		btrfs_throttle(fs_info);
28447f913c7cSQu Wenruo 		if (btrfs_should_cancel_balance(fs_info)) {
28457f913c7cSQu Wenruo 			ret = -ECANCELED;
28467f913c7cSQu Wenruo 			goto out;
28477f913c7cSQu Wenruo 		}
28480257bb82SYan, Zheng 	}
28490257bb82SYan, Zheng 	WARN_ON(nr != cluster->nr);
2850efa56464SYan, Zheng out:
28510257bb82SYan, Zheng 	kfree(ra);
28520257bb82SYan, Zheng 	return ret;
28530257bb82SYan, Zheng }
28540257bb82SYan, Zheng 
28550257bb82SYan, Zheng static noinline_for_stack
28560257bb82SYan, Zheng int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key,
28570257bb82SYan, Zheng 			 struct file_extent_cluster *cluster)
28580257bb82SYan, Zheng {
28590257bb82SYan, Zheng 	int ret;
28600257bb82SYan, Zheng 
28610257bb82SYan, Zheng 	if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) {
28620257bb82SYan, Zheng 		ret = relocate_file_extent_cluster(inode, cluster);
28630257bb82SYan, Zheng 		if (ret)
28640257bb82SYan, Zheng 			return ret;
28650257bb82SYan, Zheng 		cluster->nr = 0;
28660257bb82SYan, Zheng 	}
28670257bb82SYan, Zheng 
28680257bb82SYan, Zheng 	if (!cluster->nr)
28690257bb82SYan, Zheng 		cluster->start = extent_key->objectid;
28700257bb82SYan, Zheng 	else
28710257bb82SYan, Zheng 		BUG_ON(cluster->nr >= MAX_EXTENTS);
28720257bb82SYan, Zheng 	cluster->end = extent_key->objectid + extent_key->offset - 1;
28730257bb82SYan, Zheng 	cluster->boundary[cluster->nr] = extent_key->objectid;
28740257bb82SYan, Zheng 	cluster->nr++;
28750257bb82SYan, Zheng 
28760257bb82SYan, Zheng 	if (cluster->nr >= MAX_EXTENTS) {
28770257bb82SYan, Zheng 		ret = relocate_file_extent_cluster(inode, cluster);
28780257bb82SYan, Zheng 		if (ret)
28790257bb82SYan, Zheng 			return ret;
28800257bb82SYan, Zheng 		cluster->nr = 0;
28810257bb82SYan, Zheng 	}
28820257bb82SYan, Zheng 	return 0;
28835d4f98a2SYan Zheng }
28845d4f98a2SYan Zheng 
28855d4f98a2SYan Zheng /*
28865d4f98a2SYan Zheng  * helper to add a tree block to the list.
28875d4f98a2SYan Zheng  * the major work is getting the generation and level of the block
28885d4f98a2SYan Zheng  */
28895d4f98a2SYan Zheng static int add_tree_block(struct reloc_control *rc,
28905d4f98a2SYan Zheng 			  struct btrfs_key *extent_key,
28915d4f98a2SYan Zheng 			  struct btrfs_path *path,
28925d4f98a2SYan Zheng 			  struct rb_root *blocks)
28935d4f98a2SYan Zheng {
28945d4f98a2SYan Zheng 	struct extent_buffer *eb;
28955d4f98a2SYan Zheng 	struct btrfs_extent_item *ei;
28965d4f98a2SYan Zheng 	struct btrfs_tree_block_info *bi;
28975d4f98a2SYan Zheng 	struct tree_block *block;
28985d4f98a2SYan Zheng 	struct rb_node *rb_node;
28995d4f98a2SYan Zheng 	u32 item_size;
29005d4f98a2SYan Zheng 	int level = -1;
29017fdf4b60SWang Shilong 	u64 generation;
29025d4f98a2SYan Zheng 
29035d4f98a2SYan Zheng 	eb =  path->nodes[0];
29045d4f98a2SYan Zheng 	item_size = btrfs_item_size_nr(eb, path->slots[0]);
29055d4f98a2SYan Zheng 
29063173a18fSJosef Bacik 	if (extent_key->type == BTRFS_METADATA_ITEM_KEY ||
29073173a18fSJosef Bacik 	    item_size >= sizeof(*ei) + sizeof(*bi)) {
29085d4f98a2SYan Zheng 		ei = btrfs_item_ptr(eb, path->slots[0],
29095d4f98a2SYan Zheng 				struct btrfs_extent_item);
29103173a18fSJosef Bacik 		if (extent_key->type == BTRFS_EXTENT_ITEM_KEY) {
29115d4f98a2SYan Zheng 			bi = (struct btrfs_tree_block_info *)(ei + 1);
29125d4f98a2SYan Zheng 			level = btrfs_tree_block_level(eb, bi);
29135d4f98a2SYan Zheng 		} else {
29143173a18fSJosef Bacik 			level = (int)extent_key->offset;
29153173a18fSJosef Bacik 		}
29163173a18fSJosef Bacik 		generation = btrfs_extent_generation(eb, ei);
29176d8ff4e4SDavid Sterba 	} else if (unlikely(item_size == sizeof(struct btrfs_extent_item_v0))) {
2918ba3c2b19SNikolay Borisov 		btrfs_print_v0_err(eb->fs_info);
2919ba3c2b19SNikolay Borisov 		btrfs_handle_fs_error(eb->fs_info, -EINVAL, NULL);
2920ba3c2b19SNikolay Borisov 		return -EINVAL;
29213173a18fSJosef Bacik 	} else {
29225d4f98a2SYan Zheng 		BUG();
29235d4f98a2SYan Zheng 	}
29245d4f98a2SYan Zheng 
2925b3b4aa74SDavid Sterba 	btrfs_release_path(path);
29265d4f98a2SYan Zheng 
29275d4f98a2SYan Zheng 	BUG_ON(level == -1);
29285d4f98a2SYan Zheng 
29295d4f98a2SYan Zheng 	block = kmalloc(sizeof(*block), GFP_NOFS);
29305d4f98a2SYan Zheng 	if (!block)
29315d4f98a2SYan Zheng 		return -ENOMEM;
29325d4f98a2SYan Zheng 
29335d4f98a2SYan Zheng 	block->bytenr = extent_key->objectid;
2934da17066cSJeff Mahoney 	block->key.objectid = rc->extent_root->fs_info->nodesize;
29355d4f98a2SYan Zheng 	block->key.offset = generation;
29365d4f98a2SYan Zheng 	block->level = level;
29375d4f98a2SYan Zheng 	block->key_ready = 0;
29385d4f98a2SYan Zheng 
2939e9a28dc5SQu Wenruo 	rb_node = rb_simple_insert(blocks, block->bytenr, &block->rb_node);
294043c04fb1SJeff Mahoney 	if (rb_node)
2941982c92cbSQu Wenruo 		btrfs_backref_panic(rc->extent_root->fs_info, block->bytenr,
2942982c92cbSQu Wenruo 				    -EEXIST);
29435d4f98a2SYan Zheng 
29445d4f98a2SYan Zheng 	return 0;
29455d4f98a2SYan Zheng }
29465d4f98a2SYan Zheng 
29475d4f98a2SYan Zheng /*
29485d4f98a2SYan Zheng  * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
29495d4f98a2SYan Zheng  */
29505d4f98a2SYan Zheng static int __add_tree_block(struct reloc_control *rc,
29515d4f98a2SYan Zheng 			    u64 bytenr, u32 blocksize,
29525d4f98a2SYan Zheng 			    struct rb_root *blocks)
29535d4f98a2SYan Zheng {
29540b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
29555d4f98a2SYan Zheng 	struct btrfs_path *path;
29565d4f98a2SYan Zheng 	struct btrfs_key key;
29575d4f98a2SYan Zheng 	int ret;
29580b246afaSJeff Mahoney 	bool skinny = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
29595d4f98a2SYan Zheng 
29607476dfdaSDavid Sterba 	if (tree_block_processed(bytenr, rc))
29615d4f98a2SYan Zheng 		return 0;
29625d4f98a2SYan Zheng 
2963e9a28dc5SQu Wenruo 	if (rb_simple_search(blocks, bytenr))
29645d4f98a2SYan Zheng 		return 0;
29655d4f98a2SYan Zheng 
29665d4f98a2SYan Zheng 	path = btrfs_alloc_path();
29675d4f98a2SYan Zheng 	if (!path)
29685d4f98a2SYan Zheng 		return -ENOMEM;
2969aee68ee5SJosef Bacik again:
29705d4f98a2SYan Zheng 	key.objectid = bytenr;
2971aee68ee5SJosef Bacik 	if (skinny) {
2972aee68ee5SJosef Bacik 		key.type = BTRFS_METADATA_ITEM_KEY;
2973aee68ee5SJosef Bacik 		key.offset = (u64)-1;
2974aee68ee5SJosef Bacik 	} else {
29755d4f98a2SYan Zheng 		key.type = BTRFS_EXTENT_ITEM_KEY;
29765d4f98a2SYan Zheng 		key.offset = blocksize;
2977aee68ee5SJosef Bacik 	}
29785d4f98a2SYan Zheng 
29795d4f98a2SYan Zheng 	path->search_commit_root = 1;
29805d4f98a2SYan Zheng 	path->skip_locking = 1;
29815d4f98a2SYan Zheng 	ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0);
29825d4f98a2SYan Zheng 	if (ret < 0)
29835d4f98a2SYan Zheng 		goto out;
29845d4f98a2SYan Zheng 
2985aee68ee5SJosef Bacik 	if (ret > 0 && skinny) {
2986aee68ee5SJosef Bacik 		if (path->slots[0]) {
2987aee68ee5SJosef Bacik 			path->slots[0]--;
2988aee68ee5SJosef Bacik 			btrfs_item_key_to_cpu(path->nodes[0], &key,
2989aee68ee5SJosef Bacik 					      path->slots[0]);
29903173a18fSJosef Bacik 			if (key.objectid == bytenr &&
2991aee68ee5SJosef Bacik 			    (key.type == BTRFS_METADATA_ITEM_KEY ||
2992aee68ee5SJosef Bacik 			     (key.type == BTRFS_EXTENT_ITEM_KEY &&
2993aee68ee5SJosef Bacik 			      key.offset == blocksize)))
29943173a18fSJosef Bacik 				ret = 0;
29953173a18fSJosef Bacik 		}
2996aee68ee5SJosef Bacik 
2997aee68ee5SJosef Bacik 		if (ret) {
2998aee68ee5SJosef Bacik 			skinny = false;
2999aee68ee5SJosef Bacik 			btrfs_release_path(path);
3000aee68ee5SJosef Bacik 			goto again;
3001aee68ee5SJosef Bacik 		}
3002aee68ee5SJosef Bacik 	}
3003cdccee99SLiu Bo 	if (ret) {
3004cdccee99SLiu Bo 		ASSERT(ret == 1);
3005cdccee99SLiu Bo 		btrfs_print_leaf(path->nodes[0]);
3006cdccee99SLiu Bo 		btrfs_err(fs_info,
3007cdccee99SLiu Bo 	     "tree block extent item (%llu) is not found in extent tree",
3008cdccee99SLiu Bo 		     bytenr);
3009cdccee99SLiu Bo 		WARN_ON(1);
3010cdccee99SLiu Bo 		ret = -EINVAL;
3011cdccee99SLiu Bo 		goto out;
3012cdccee99SLiu Bo 	}
30133173a18fSJosef Bacik 
30145d4f98a2SYan Zheng 	ret = add_tree_block(rc, &key, path, blocks);
30155d4f98a2SYan Zheng out:
30165d4f98a2SYan Zheng 	btrfs_free_path(path);
30175d4f98a2SYan Zheng 	return ret;
30185d4f98a2SYan Zheng }
30195d4f98a2SYan Zheng 
30200af3d00bSJosef Bacik static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
302132da5386SDavid Sterba 				    struct btrfs_block_group *block_group,
30221bbc621eSChris Mason 				    struct inode *inode,
30231bbc621eSChris Mason 				    u64 ino)
30240af3d00bSJosef Bacik {
30250af3d00bSJosef Bacik 	struct btrfs_key key;
30260af3d00bSJosef Bacik 	struct btrfs_root *root = fs_info->tree_root;
30270af3d00bSJosef Bacik 	struct btrfs_trans_handle *trans;
30280af3d00bSJosef Bacik 	int ret = 0;
30290af3d00bSJosef Bacik 
30300af3d00bSJosef Bacik 	if (inode)
30310af3d00bSJosef Bacik 		goto truncate;
30320af3d00bSJosef Bacik 
30330af3d00bSJosef Bacik 	key.objectid = ino;
30340af3d00bSJosef Bacik 	key.type = BTRFS_INODE_ITEM_KEY;
30350af3d00bSJosef Bacik 	key.offset = 0;
30360af3d00bSJosef Bacik 
30374c66e0d4SDavid Sterba 	inode = btrfs_iget(fs_info->sb, &key, root);
30382e19f1f9SAl Viro 	if (IS_ERR(inode))
30390af3d00bSJosef Bacik 		return -ENOENT;
30400af3d00bSJosef Bacik 
30410af3d00bSJosef Bacik truncate:
30422ff7e61eSJeff Mahoney 	ret = btrfs_check_trunc_cache_free_space(fs_info,
30437b61cd92SMiao Xie 						 &fs_info->global_block_rsv);
30447b61cd92SMiao Xie 	if (ret)
30457b61cd92SMiao Xie 		goto out;
30467b61cd92SMiao Xie 
30477a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(root);
30480af3d00bSJosef Bacik 	if (IS_ERR(trans)) {
30493612b495STsutomu Itoh 		ret = PTR_ERR(trans);
30500af3d00bSJosef Bacik 		goto out;
30510af3d00bSJosef Bacik 	}
30520af3d00bSJosef Bacik 
305377ab86bfSJeff Mahoney 	ret = btrfs_truncate_free_space_cache(trans, block_group, inode);
30540af3d00bSJosef Bacik 
30553a45bb20SJeff Mahoney 	btrfs_end_transaction(trans);
30562ff7e61eSJeff Mahoney 	btrfs_btree_balance_dirty(fs_info);
30570af3d00bSJosef Bacik out:
30580af3d00bSJosef Bacik 	iput(inode);
30590af3d00bSJosef Bacik 	return ret;
30600af3d00bSJosef Bacik }
30610af3d00bSJosef Bacik 
30625d4f98a2SYan Zheng /*
306319b546d7SQu Wenruo  * Locate the free space cache EXTENT_DATA in root tree leaf and delete the
306419b546d7SQu Wenruo  * cache inode, to avoid free space cache data extent blocking data relocation.
30655d4f98a2SYan Zheng  */
306619b546d7SQu Wenruo static int delete_v1_space_cache(struct extent_buffer *leaf,
306719b546d7SQu Wenruo 				 struct btrfs_block_group *block_group,
306819b546d7SQu Wenruo 				 u64 data_bytenr)
30695d4f98a2SYan Zheng {
307019b546d7SQu Wenruo 	u64 space_cache_ino;
307119b546d7SQu Wenruo 	struct btrfs_file_extent_item *ei;
30725d4f98a2SYan Zheng 	struct btrfs_key key;
307319b546d7SQu Wenruo 	bool found = false;
307419b546d7SQu Wenruo 	int i;
30755d4f98a2SYan Zheng 	int ret;
30765d4f98a2SYan Zheng 
307719b546d7SQu Wenruo 	if (btrfs_header_owner(leaf) != BTRFS_ROOT_TREE_OBJECTID)
307819b546d7SQu Wenruo 		return 0;
30795d4f98a2SYan Zheng 
308019b546d7SQu Wenruo 	for (i = 0; i < btrfs_header_nritems(leaf); i++) {
308119b546d7SQu Wenruo 		btrfs_item_key_to_cpu(leaf, &key, i);
308219b546d7SQu Wenruo 		if (key.type != BTRFS_EXTENT_DATA_KEY)
308319b546d7SQu Wenruo 			continue;
308419b546d7SQu Wenruo 		ei = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
308519b546d7SQu Wenruo 		if (btrfs_file_extent_type(leaf, ei) == BTRFS_FILE_EXTENT_REG &&
308619b546d7SQu Wenruo 		    btrfs_file_extent_disk_bytenr(leaf, ei) == data_bytenr) {
308719b546d7SQu Wenruo 			found = true;
308819b546d7SQu Wenruo 			space_cache_ino = key.objectid;
308919b546d7SQu Wenruo 			break;
309019b546d7SQu Wenruo 		}
309119b546d7SQu Wenruo 	}
309219b546d7SQu Wenruo 	if (!found)
309319b546d7SQu Wenruo 		return -ENOENT;
309419b546d7SQu Wenruo 	ret = delete_block_group_cache(leaf->fs_info, block_group, NULL,
309519b546d7SQu Wenruo 					space_cache_ino);
30960af3d00bSJosef Bacik 	return ret;
30975d4f98a2SYan Zheng }
30985d4f98a2SYan Zheng 
30995d4f98a2SYan Zheng /*
31002c016dc2SLiu Bo  * helper to find all tree blocks that reference a given data extent
31015d4f98a2SYan Zheng  */
31025d4f98a2SYan Zheng static noinline_for_stack
31035d4f98a2SYan Zheng int add_data_references(struct reloc_control *rc,
31045d4f98a2SYan Zheng 			struct btrfs_key *extent_key,
31055d4f98a2SYan Zheng 			struct btrfs_path *path,
31065d4f98a2SYan Zheng 			struct rb_root *blocks)
31075d4f98a2SYan Zheng {
310819b546d7SQu Wenruo 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
310919b546d7SQu Wenruo 	struct ulist *leaves = NULL;
311019b546d7SQu Wenruo 	struct ulist_iterator leaf_uiter;
311119b546d7SQu Wenruo 	struct ulist_node *ref_node = NULL;
311219b546d7SQu Wenruo 	const u32 blocksize = fs_info->nodesize;
3113647f63bdSFilipe David Borba Manana 	int ret = 0;
31145d4f98a2SYan Zheng 
3115b3b4aa74SDavid Sterba 	btrfs_release_path(path);
311619b546d7SQu Wenruo 	ret = btrfs_find_all_leafs(NULL, fs_info, extent_key->objectid,
311719b546d7SQu Wenruo 				   0, &leaves, NULL, true);
311819b546d7SQu Wenruo 	if (ret < 0)
311919b546d7SQu Wenruo 		return ret;
312019b546d7SQu Wenruo 
312119b546d7SQu Wenruo 	ULIST_ITER_INIT(&leaf_uiter);
312219b546d7SQu Wenruo 	while ((ref_node = ulist_next(leaves, &leaf_uiter))) {
312319b546d7SQu Wenruo 		struct extent_buffer *eb;
312419b546d7SQu Wenruo 
312519b546d7SQu Wenruo 		eb = read_tree_block(fs_info, ref_node->val, 0, 0, NULL);
312619b546d7SQu Wenruo 		if (IS_ERR(eb)) {
312719b546d7SQu Wenruo 			ret = PTR_ERR(eb);
312819b546d7SQu Wenruo 			break;
312919b546d7SQu Wenruo 		}
313019b546d7SQu Wenruo 		ret = delete_v1_space_cache(eb, rc->block_group,
313119b546d7SQu Wenruo 					    extent_key->objectid);
313219b546d7SQu Wenruo 		free_extent_buffer(eb);
313319b546d7SQu Wenruo 		if (ret < 0)
313419b546d7SQu Wenruo 			break;
313519b546d7SQu Wenruo 		ret = __add_tree_block(rc, ref_node->val, blocksize, blocks);
313619b546d7SQu Wenruo 		if (ret < 0)
313719b546d7SQu Wenruo 			break;
313819b546d7SQu Wenruo 	}
313919b546d7SQu Wenruo 	if (ret < 0)
31405d4f98a2SYan Zheng 		free_block_list(blocks);
314119b546d7SQu Wenruo 	ulist_free(leaves);
314219b546d7SQu Wenruo 	return ret;
31435d4f98a2SYan Zheng }
31445d4f98a2SYan Zheng 
31455d4f98a2SYan Zheng /*
31462c016dc2SLiu Bo  * helper to find next unprocessed extent
31475d4f98a2SYan Zheng  */
31485d4f98a2SYan Zheng static noinline_for_stack
3149147d256eSZhaolei int find_next_extent(struct reloc_control *rc, struct btrfs_path *path,
31503fd0a558SYan, Zheng 		     struct btrfs_key *extent_key)
31515d4f98a2SYan Zheng {
31520b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
31535d4f98a2SYan Zheng 	struct btrfs_key key;
31545d4f98a2SYan Zheng 	struct extent_buffer *leaf;
31555d4f98a2SYan Zheng 	u64 start, end, last;
31565d4f98a2SYan Zheng 	int ret;
31575d4f98a2SYan Zheng 
3158b3470b5dSDavid Sterba 	last = rc->block_group->start + rc->block_group->length;
31595d4f98a2SYan Zheng 	while (1) {
31605d4f98a2SYan Zheng 		cond_resched();
31615d4f98a2SYan Zheng 		if (rc->search_start >= last) {
31625d4f98a2SYan Zheng 			ret = 1;
31635d4f98a2SYan Zheng 			break;
31645d4f98a2SYan Zheng 		}
31655d4f98a2SYan Zheng 
31665d4f98a2SYan Zheng 		key.objectid = rc->search_start;
31675d4f98a2SYan Zheng 		key.type = BTRFS_EXTENT_ITEM_KEY;
31685d4f98a2SYan Zheng 		key.offset = 0;
31695d4f98a2SYan Zheng 
31705d4f98a2SYan Zheng 		path->search_commit_root = 1;
31715d4f98a2SYan Zheng 		path->skip_locking = 1;
31725d4f98a2SYan Zheng 		ret = btrfs_search_slot(NULL, rc->extent_root, &key, path,
31735d4f98a2SYan Zheng 					0, 0);
31745d4f98a2SYan Zheng 		if (ret < 0)
31755d4f98a2SYan Zheng 			break;
31765d4f98a2SYan Zheng next:
31775d4f98a2SYan Zheng 		leaf = path->nodes[0];
31785d4f98a2SYan Zheng 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
31795d4f98a2SYan Zheng 			ret = btrfs_next_leaf(rc->extent_root, path);
31805d4f98a2SYan Zheng 			if (ret != 0)
31815d4f98a2SYan Zheng 				break;
31825d4f98a2SYan Zheng 			leaf = path->nodes[0];
31835d4f98a2SYan Zheng 		}
31845d4f98a2SYan Zheng 
31855d4f98a2SYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
31865d4f98a2SYan Zheng 		if (key.objectid >= last) {
31875d4f98a2SYan Zheng 			ret = 1;
31885d4f98a2SYan Zheng 			break;
31895d4f98a2SYan Zheng 		}
31905d4f98a2SYan Zheng 
31913173a18fSJosef Bacik 		if (key.type != BTRFS_EXTENT_ITEM_KEY &&
31923173a18fSJosef Bacik 		    key.type != BTRFS_METADATA_ITEM_KEY) {
31933173a18fSJosef Bacik 			path->slots[0]++;
31943173a18fSJosef Bacik 			goto next;
31953173a18fSJosef Bacik 		}
31963173a18fSJosef Bacik 
31973173a18fSJosef Bacik 		if (key.type == BTRFS_EXTENT_ITEM_KEY &&
31985d4f98a2SYan Zheng 		    key.objectid + key.offset <= rc->search_start) {
31995d4f98a2SYan Zheng 			path->slots[0]++;
32005d4f98a2SYan Zheng 			goto next;
32015d4f98a2SYan Zheng 		}
32025d4f98a2SYan Zheng 
32033173a18fSJosef Bacik 		if (key.type == BTRFS_METADATA_ITEM_KEY &&
32040b246afaSJeff Mahoney 		    key.objectid + fs_info->nodesize <=
32053173a18fSJosef Bacik 		    rc->search_start) {
32063173a18fSJosef Bacik 			path->slots[0]++;
32073173a18fSJosef Bacik 			goto next;
32083173a18fSJosef Bacik 		}
32093173a18fSJosef Bacik 
32105d4f98a2SYan Zheng 		ret = find_first_extent_bit(&rc->processed_blocks,
32115d4f98a2SYan Zheng 					    key.objectid, &start, &end,
3212e6138876SJosef Bacik 					    EXTENT_DIRTY, NULL);
32135d4f98a2SYan Zheng 
32145d4f98a2SYan Zheng 		if (ret == 0 && start <= key.objectid) {
3215b3b4aa74SDavid Sterba 			btrfs_release_path(path);
32165d4f98a2SYan Zheng 			rc->search_start = end + 1;
32175d4f98a2SYan Zheng 		} else {
32183173a18fSJosef Bacik 			if (key.type == BTRFS_EXTENT_ITEM_KEY)
32195d4f98a2SYan Zheng 				rc->search_start = key.objectid + key.offset;
32203173a18fSJosef Bacik 			else
32213173a18fSJosef Bacik 				rc->search_start = key.objectid +
32220b246afaSJeff Mahoney 					fs_info->nodesize;
32233fd0a558SYan, Zheng 			memcpy(extent_key, &key, sizeof(key));
32245d4f98a2SYan Zheng 			return 0;
32255d4f98a2SYan Zheng 		}
32265d4f98a2SYan Zheng 	}
3227b3b4aa74SDavid Sterba 	btrfs_release_path(path);
32285d4f98a2SYan Zheng 	return ret;
32295d4f98a2SYan Zheng }
32305d4f98a2SYan Zheng 
32315d4f98a2SYan Zheng static void set_reloc_control(struct reloc_control *rc)
32325d4f98a2SYan Zheng {
32335d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
32347585717fSChris Mason 
32357585717fSChris Mason 	mutex_lock(&fs_info->reloc_mutex);
32365d4f98a2SYan Zheng 	fs_info->reloc_ctl = rc;
32377585717fSChris Mason 	mutex_unlock(&fs_info->reloc_mutex);
32385d4f98a2SYan Zheng }
32395d4f98a2SYan Zheng 
32405d4f98a2SYan Zheng static void unset_reloc_control(struct reloc_control *rc)
32415d4f98a2SYan Zheng {
32425d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
32437585717fSChris Mason 
32447585717fSChris Mason 	mutex_lock(&fs_info->reloc_mutex);
32455d4f98a2SYan Zheng 	fs_info->reloc_ctl = NULL;
32467585717fSChris Mason 	mutex_unlock(&fs_info->reloc_mutex);
32475d4f98a2SYan Zheng }
32485d4f98a2SYan Zheng 
32495d4f98a2SYan Zheng static int check_extent_flags(u64 flags)
32505d4f98a2SYan Zheng {
32515d4f98a2SYan Zheng 	if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
32525d4f98a2SYan Zheng 	    (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
32535d4f98a2SYan Zheng 		return 1;
32545d4f98a2SYan Zheng 	if (!(flags & BTRFS_EXTENT_FLAG_DATA) &&
32555d4f98a2SYan Zheng 	    !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
32565d4f98a2SYan Zheng 		return 1;
32575d4f98a2SYan Zheng 	if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
32585d4f98a2SYan Zheng 	    (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
32595d4f98a2SYan Zheng 		return 1;
32605d4f98a2SYan Zheng 	return 0;
32615d4f98a2SYan Zheng }
32625d4f98a2SYan Zheng 
32633fd0a558SYan, Zheng static noinline_for_stack
32643fd0a558SYan, Zheng int prepare_to_relocate(struct reloc_control *rc)
32653fd0a558SYan, Zheng {
32663fd0a558SYan, Zheng 	struct btrfs_trans_handle *trans;
3267ac2fabacSJosef Bacik 	int ret;
32683fd0a558SYan, Zheng 
32692ff7e61eSJeff Mahoney 	rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root->fs_info,
327066d8f3ddSMiao Xie 					      BTRFS_BLOCK_RSV_TEMP);
32713fd0a558SYan, Zheng 	if (!rc->block_rsv)
32723fd0a558SYan, Zheng 		return -ENOMEM;
32733fd0a558SYan, Zheng 
32743fd0a558SYan, Zheng 	memset(&rc->cluster, 0, sizeof(rc->cluster));
3275b3470b5dSDavid Sterba 	rc->search_start = rc->block_group->start;
32763fd0a558SYan, Zheng 	rc->extents_found = 0;
32773fd0a558SYan, Zheng 	rc->nodes_relocated = 0;
32783fd0a558SYan, Zheng 	rc->merging_rsv_size = 0;
32790647bf56SWang Shilong 	rc->reserved_bytes = 0;
3280da17066cSJeff Mahoney 	rc->block_rsv->size = rc->extent_root->fs_info->nodesize *
32810647bf56SWang Shilong 			      RELOCATION_RESERVED_NODES;
3282ac2fabacSJosef Bacik 	ret = btrfs_block_rsv_refill(rc->extent_root,
3283ac2fabacSJosef Bacik 				     rc->block_rsv, rc->block_rsv->size,
3284ac2fabacSJosef Bacik 				     BTRFS_RESERVE_FLUSH_ALL);
3285ac2fabacSJosef Bacik 	if (ret)
3286ac2fabacSJosef Bacik 		return ret;
32873fd0a558SYan, Zheng 
32883fd0a558SYan, Zheng 	rc->create_reloc_tree = 1;
32893fd0a558SYan, Zheng 	set_reloc_control(rc);
32903fd0a558SYan, Zheng 
32917a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(rc->extent_root);
329228818947SLiu Bo 	if (IS_ERR(trans)) {
329328818947SLiu Bo 		unset_reloc_control(rc);
329428818947SLiu Bo 		/*
329528818947SLiu Bo 		 * extent tree is not a ref_cow tree and has no reloc_root to
329628818947SLiu Bo 		 * cleanup.  And callers are responsible to free the above
329728818947SLiu Bo 		 * block rsv.
329828818947SLiu Bo 		 */
329928818947SLiu Bo 		return PTR_ERR(trans);
330028818947SLiu Bo 	}
33013a45bb20SJeff Mahoney 	btrfs_commit_transaction(trans);
33023fd0a558SYan, Zheng 	return 0;
33033fd0a558SYan, Zheng }
330476dda93cSYan, Zheng 
33055d4f98a2SYan Zheng static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
33065d4f98a2SYan Zheng {
33072ff7e61eSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
33085d4f98a2SYan Zheng 	struct rb_root blocks = RB_ROOT;
33095d4f98a2SYan Zheng 	struct btrfs_key key;
33105d4f98a2SYan Zheng 	struct btrfs_trans_handle *trans = NULL;
33115d4f98a2SYan Zheng 	struct btrfs_path *path;
33125d4f98a2SYan Zheng 	struct btrfs_extent_item *ei;
33135d4f98a2SYan Zheng 	u64 flags;
33145d4f98a2SYan Zheng 	u32 item_size;
33155d4f98a2SYan Zheng 	int ret;
33165d4f98a2SYan Zheng 	int err = 0;
3317c87f08caSChris Mason 	int progress = 0;
33185d4f98a2SYan Zheng 
33195d4f98a2SYan Zheng 	path = btrfs_alloc_path();
33203fd0a558SYan, Zheng 	if (!path)
33215d4f98a2SYan Zheng 		return -ENOMEM;
3322e4058b54SDavid Sterba 	path->reada = READA_FORWARD;
33233fd0a558SYan, Zheng 
33243fd0a558SYan, Zheng 	ret = prepare_to_relocate(rc);
33253fd0a558SYan, Zheng 	if (ret) {
33263fd0a558SYan, Zheng 		err = ret;
33273fd0a558SYan, Zheng 		goto out_free;
33282423fdfbSJiri Slaby 	}
33295d4f98a2SYan Zheng 
33305d4f98a2SYan Zheng 	while (1) {
33310647bf56SWang Shilong 		rc->reserved_bytes = 0;
33320647bf56SWang Shilong 		ret = btrfs_block_rsv_refill(rc->extent_root,
33330647bf56SWang Shilong 					rc->block_rsv, rc->block_rsv->size,
33340647bf56SWang Shilong 					BTRFS_RESERVE_FLUSH_ALL);
33350647bf56SWang Shilong 		if (ret) {
33360647bf56SWang Shilong 			err = ret;
33370647bf56SWang Shilong 			break;
33380647bf56SWang Shilong 		}
3339c87f08caSChris Mason 		progress++;
3340a22285a6SYan, Zheng 		trans = btrfs_start_transaction(rc->extent_root, 0);
33410f788c58SLiu Bo 		if (IS_ERR(trans)) {
33420f788c58SLiu Bo 			err = PTR_ERR(trans);
33430f788c58SLiu Bo 			trans = NULL;
33440f788c58SLiu Bo 			break;
33450f788c58SLiu Bo 		}
3346c87f08caSChris Mason restart:
33473fd0a558SYan, Zheng 		if (update_backref_cache(trans, &rc->backref_cache)) {
33483a45bb20SJeff Mahoney 			btrfs_end_transaction(trans);
334942a657f5SPan Bian 			trans = NULL;
33503fd0a558SYan, Zheng 			continue;
33513fd0a558SYan, Zheng 		}
33523fd0a558SYan, Zheng 
3353147d256eSZhaolei 		ret = find_next_extent(rc, path, &key);
33545d4f98a2SYan Zheng 		if (ret < 0)
33555d4f98a2SYan Zheng 			err = ret;
33565d4f98a2SYan Zheng 		if (ret != 0)
33575d4f98a2SYan Zheng 			break;
33585d4f98a2SYan Zheng 
33595d4f98a2SYan Zheng 		rc->extents_found++;
33605d4f98a2SYan Zheng 
33615d4f98a2SYan Zheng 		ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
33625d4f98a2SYan Zheng 				    struct btrfs_extent_item);
33633fd0a558SYan, Zheng 		item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
33645d4f98a2SYan Zheng 		if (item_size >= sizeof(*ei)) {
33655d4f98a2SYan Zheng 			flags = btrfs_extent_flags(path->nodes[0], ei);
33665d4f98a2SYan Zheng 			ret = check_extent_flags(flags);
33675d4f98a2SYan Zheng 			BUG_ON(ret);
33686d8ff4e4SDavid Sterba 		} else if (unlikely(item_size == sizeof(struct btrfs_extent_item_v0))) {
3369ba3c2b19SNikolay Borisov 			err = -EINVAL;
3370ba3c2b19SNikolay Borisov 			btrfs_print_v0_err(trans->fs_info);
3371ba3c2b19SNikolay Borisov 			btrfs_abort_transaction(trans, err);
3372ba3c2b19SNikolay Borisov 			break;
33735d4f98a2SYan Zheng 		} else {
33745d4f98a2SYan Zheng 			BUG();
33755d4f98a2SYan Zheng 		}
33765d4f98a2SYan Zheng 
33775d4f98a2SYan Zheng 		if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
33785d4f98a2SYan Zheng 			ret = add_tree_block(rc, &key, path, &blocks);
33795d4f98a2SYan Zheng 		} else if (rc->stage == UPDATE_DATA_PTRS &&
33805d4f98a2SYan Zheng 			   (flags & BTRFS_EXTENT_FLAG_DATA)) {
33815d4f98a2SYan Zheng 			ret = add_data_references(rc, &key, path, &blocks);
33825d4f98a2SYan Zheng 		} else {
3383b3b4aa74SDavid Sterba 			btrfs_release_path(path);
33845d4f98a2SYan Zheng 			ret = 0;
33855d4f98a2SYan Zheng 		}
33865d4f98a2SYan Zheng 		if (ret < 0) {
33873fd0a558SYan, Zheng 			err = ret;
33885d4f98a2SYan Zheng 			break;
33895d4f98a2SYan Zheng 		}
33905d4f98a2SYan Zheng 
33915d4f98a2SYan Zheng 		if (!RB_EMPTY_ROOT(&blocks)) {
33925d4f98a2SYan Zheng 			ret = relocate_tree_blocks(trans, rc, &blocks);
33935d4f98a2SYan Zheng 			if (ret < 0) {
33943fd0a558SYan, Zheng 				if (ret != -EAGAIN) {
33955d4f98a2SYan Zheng 					err = ret;
33965d4f98a2SYan Zheng 					break;
33975d4f98a2SYan Zheng 				}
33983fd0a558SYan, Zheng 				rc->extents_found--;
33993fd0a558SYan, Zheng 				rc->search_start = key.objectid;
34003fd0a558SYan, Zheng 			}
34015d4f98a2SYan Zheng 		}
34025d4f98a2SYan Zheng 
34033a45bb20SJeff Mahoney 		btrfs_end_transaction_throttle(trans);
34042ff7e61eSJeff Mahoney 		btrfs_btree_balance_dirty(fs_info);
34053fd0a558SYan, Zheng 		trans = NULL;
34065d4f98a2SYan Zheng 
34075d4f98a2SYan Zheng 		if (rc->stage == MOVE_DATA_EXTENTS &&
34085d4f98a2SYan Zheng 		    (flags & BTRFS_EXTENT_FLAG_DATA)) {
34095d4f98a2SYan Zheng 			rc->found_file_extent = 1;
34100257bb82SYan, Zheng 			ret = relocate_data_extent(rc->data_inode,
34113fd0a558SYan, Zheng 						   &key, &rc->cluster);
34125d4f98a2SYan Zheng 			if (ret < 0) {
34135d4f98a2SYan Zheng 				err = ret;
34145d4f98a2SYan Zheng 				break;
34155d4f98a2SYan Zheng 			}
34165d4f98a2SYan Zheng 		}
3417f31ea088SQu Wenruo 		if (btrfs_should_cancel_balance(fs_info)) {
3418f31ea088SQu Wenruo 			err = -ECANCELED;
3419f31ea088SQu Wenruo 			break;
3420f31ea088SQu Wenruo 		}
34215d4f98a2SYan Zheng 	}
3422c87f08caSChris Mason 	if (trans && progress && err == -ENOSPC) {
342343a7e99dSNikolay Borisov 		ret = btrfs_force_chunk_alloc(trans, rc->block_group->flags);
34249689457bSShilong Wang 		if (ret == 1) {
3425c87f08caSChris Mason 			err = 0;
3426c87f08caSChris Mason 			progress = 0;
3427c87f08caSChris Mason 			goto restart;
3428c87f08caSChris Mason 		}
3429c87f08caSChris Mason 	}
34303fd0a558SYan, Zheng 
3431b3b4aa74SDavid Sterba 	btrfs_release_path(path);
343291166212SDavid Sterba 	clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY);
34335d4f98a2SYan Zheng 
34345d4f98a2SYan Zheng 	if (trans) {
34353a45bb20SJeff Mahoney 		btrfs_end_transaction_throttle(trans);
34362ff7e61eSJeff Mahoney 		btrfs_btree_balance_dirty(fs_info);
34375d4f98a2SYan Zheng 	}
34385d4f98a2SYan Zheng 
34390257bb82SYan, Zheng 	if (!err) {
34403fd0a558SYan, Zheng 		ret = relocate_file_extent_cluster(rc->data_inode,
34413fd0a558SYan, Zheng 						   &rc->cluster);
34420257bb82SYan, Zheng 		if (ret < 0)
34430257bb82SYan, Zheng 			err = ret;
34440257bb82SYan, Zheng 	}
34450257bb82SYan, Zheng 
34463fd0a558SYan, Zheng 	rc->create_reloc_tree = 0;
34473fd0a558SYan, Zheng 	set_reloc_control(rc);
34480257bb82SYan, Zheng 
344913fe1bdbSQu Wenruo 	btrfs_backref_release_cache(&rc->backref_cache);
345063f018beSNikolay Borisov 	btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL);
34515d4f98a2SYan Zheng 
34527f913c7cSQu Wenruo 	/*
34537f913c7cSQu Wenruo 	 * Even in the case when the relocation is cancelled, we should all go
34547f913c7cSQu Wenruo 	 * through prepare_to_merge() and merge_reloc_roots().
34557f913c7cSQu Wenruo 	 *
34567f913c7cSQu Wenruo 	 * For error (including cancelled balance), prepare_to_merge() will
34577f913c7cSQu Wenruo 	 * mark all reloc trees orphan, then queue them for cleanup in
34587f913c7cSQu Wenruo 	 * merge_reloc_roots()
34597f913c7cSQu Wenruo 	 */
34603fd0a558SYan, Zheng 	err = prepare_to_merge(rc, err);
34615d4f98a2SYan Zheng 
34625d4f98a2SYan Zheng 	merge_reloc_roots(rc);
34635d4f98a2SYan Zheng 
34643fd0a558SYan, Zheng 	rc->merge_reloc_tree = 0;
34655d4f98a2SYan Zheng 	unset_reloc_control(rc);
346663f018beSNikolay Borisov 	btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL);
34675d4f98a2SYan Zheng 
34685d4f98a2SYan Zheng 	/* get rid of pinned extents */
34697a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(rc->extent_root);
347062b99540SQu Wenruo 	if (IS_ERR(trans)) {
34713612b495STsutomu Itoh 		err = PTR_ERR(trans);
347262b99540SQu Wenruo 		goto out_free;
347362b99540SQu Wenruo 	}
34743a45bb20SJeff Mahoney 	btrfs_commit_transaction(trans);
34756217b0faSJosef Bacik out_free:
3476d2311e69SQu Wenruo 	ret = clean_dirty_subvols(rc);
3477d2311e69SQu Wenruo 	if (ret < 0 && !err)
3478d2311e69SQu Wenruo 		err = ret;
34792ff7e61eSJeff Mahoney 	btrfs_free_block_rsv(fs_info, rc->block_rsv);
34803fd0a558SYan, Zheng 	btrfs_free_path(path);
34815d4f98a2SYan Zheng 	return err;
34825d4f98a2SYan Zheng }
34835d4f98a2SYan Zheng 
34845d4f98a2SYan Zheng static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
34850257bb82SYan, Zheng 				 struct btrfs_root *root, u64 objectid)
34865d4f98a2SYan Zheng {
34875d4f98a2SYan Zheng 	struct btrfs_path *path;
34885d4f98a2SYan Zheng 	struct btrfs_inode_item *item;
34895d4f98a2SYan Zheng 	struct extent_buffer *leaf;
34905d4f98a2SYan Zheng 	int ret;
34915d4f98a2SYan Zheng 
34925d4f98a2SYan Zheng 	path = btrfs_alloc_path();
34935d4f98a2SYan Zheng 	if (!path)
34945d4f98a2SYan Zheng 		return -ENOMEM;
34955d4f98a2SYan Zheng 
34965d4f98a2SYan Zheng 	ret = btrfs_insert_empty_inode(trans, root, path, objectid);
34975d4f98a2SYan Zheng 	if (ret)
34985d4f98a2SYan Zheng 		goto out;
34995d4f98a2SYan Zheng 
35005d4f98a2SYan Zheng 	leaf = path->nodes[0];
35015d4f98a2SYan Zheng 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
3502b159fa28SDavid Sterba 	memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
35035d4f98a2SYan Zheng 	btrfs_set_inode_generation(leaf, item, 1);
35040257bb82SYan, Zheng 	btrfs_set_inode_size(leaf, item, 0);
35055d4f98a2SYan Zheng 	btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
35063fd0a558SYan, Zheng 	btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS |
35073fd0a558SYan, Zheng 					  BTRFS_INODE_PREALLOC);
35085d4f98a2SYan Zheng 	btrfs_mark_buffer_dirty(leaf);
35095d4f98a2SYan Zheng out:
35105d4f98a2SYan Zheng 	btrfs_free_path(path);
35115d4f98a2SYan Zheng 	return ret;
35125d4f98a2SYan Zheng }
35135d4f98a2SYan Zheng 
35145d4f98a2SYan Zheng /*
35155d4f98a2SYan Zheng  * helper to create inode for data relocation.
35165d4f98a2SYan Zheng  * the inode is in data relocation tree and its link count is 0
35175d4f98a2SYan Zheng  */
35183fd0a558SYan, Zheng static noinline_for_stack
35193fd0a558SYan, Zheng struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
352032da5386SDavid Sterba 				 struct btrfs_block_group *group)
35215d4f98a2SYan Zheng {
35225d4f98a2SYan Zheng 	struct inode *inode = NULL;
35235d4f98a2SYan Zheng 	struct btrfs_trans_handle *trans;
35245d4f98a2SYan Zheng 	struct btrfs_root *root;
35255d4f98a2SYan Zheng 	struct btrfs_key key;
35264624900dSZhaolei 	u64 objectid;
35275d4f98a2SYan Zheng 	int err = 0;
35285d4f98a2SYan Zheng 
35295d4f98a2SYan Zheng 	root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID);
35305d4f98a2SYan Zheng 	if (IS_ERR(root))
35315d4f98a2SYan Zheng 		return ERR_CAST(root);
35325d4f98a2SYan Zheng 
3533a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 6);
353476deacf0SJosef Bacik 	if (IS_ERR(trans)) {
353500246528SJosef Bacik 		btrfs_put_root(root);
35363fd0a558SYan, Zheng 		return ERR_CAST(trans);
353776deacf0SJosef Bacik 	}
35385d4f98a2SYan Zheng 
3539581bb050SLi Zefan 	err = btrfs_find_free_objectid(root, &objectid);
35405d4f98a2SYan Zheng 	if (err)
35415d4f98a2SYan Zheng 		goto out;
35425d4f98a2SYan Zheng 
35430257bb82SYan, Zheng 	err = __insert_orphan_inode(trans, root, objectid);
35445d4f98a2SYan Zheng 	BUG_ON(err);
35455d4f98a2SYan Zheng 
35465d4f98a2SYan Zheng 	key.objectid = objectid;
35475d4f98a2SYan Zheng 	key.type = BTRFS_INODE_ITEM_KEY;
35485d4f98a2SYan Zheng 	key.offset = 0;
35494c66e0d4SDavid Sterba 	inode = btrfs_iget(fs_info->sb, &key, root);
35502e19f1f9SAl Viro 	BUG_ON(IS_ERR(inode));
3551b3470b5dSDavid Sterba 	BTRFS_I(inode)->index_cnt = group->start;
35525d4f98a2SYan Zheng 
355373f2e545SNikolay Borisov 	err = btrfs_orphan_add(trans, BTRFS_I(inode));
35545d4f98a2SYan Zheng out:
355500246528SJosef Bacik 	btrfs_put_root(root);
35563a45bb20SJeff Mahoney 	btrfs_end_transaction(trans);
35572ff7e61eSJeff Mahoney 	btrfs_btree_balance_dirty(fs_info);
35585d4f98a2SYan Zheng 	if (err) {
35595d4f98a2SYan Zheng 		if (inode)
35605d4f98a2SYan Zheng 			iput(inode);
35615d4f98a2SYan Zheng 		inode = ERR_PTR(err);
35625d4f98a2SYan Zheng 	}
35635d4f98a2SYan Zheng 	return inode;
35645d4f98a2SYan Zheng }
35655d4f98a2SYan Zheng 
3566c258d6e3SQu Wenruo static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info)
35673fd0a558SYan, Zheng {
35683fd0a558SYan, Zheng 	struct reloc_control *rc;
35693fd0a558SYan, Zheng 
35703fd0a558SYan, Zheng 	rc = kzalloc(sizeof(*rc), GFP_NOFS);
35713fd0a558SYan, Zheng 	if (!rc)
35723fd0a558SYan, Zheng 		return NULL;
35733fd0a558SYan, Zheng 
35743fd0a558SYan, Zheng 	INIT_LIST_HEAD(&rc->reloc_roots);
3575d2311e69SQu Wenruo 	INIT_LIST_HEAD(&rc->dirty_subvol_roots);
3576584fb121SQu Wenruo 	btrfs_backref_init_cache(fs_info, &rc->backref_cache, 1);
35773fd0a558SYan, Zheng 	mapping_tree_init(&rc->reloc_root_tree);
357843eb5f29SQu Wenruo 	extent_io_tree_init(fs_info, &rc->processed_blocks,
357943eb5f29SQu Wenruo 			    IO_TREE_RELOC_BLOCKS, NULL);
35803fd0a558SYan, Zheng 	return rc;
35813fd0a558SYan, Zheng }
35823fd0a558SYan, Zheng 
35831a0afa0eSJosef Bacik static void free_reloc_control(struct reloc_control *rc)
35841a0afa0eSJosef Bacik {
35851a0afa0eSJosef Bacik 	struct mapping_node *node, *tmp;
35861a0afa0eSJosef Bacik 
35871a0afa0eSJosef Bacik 	free_reloc_roots(&rc->reloc_roots);
35881a0afa0eSJosef Bacik 	rbtree_postorder_for_each_entry_safe(node, tmp,
35891a0afa0eSJosef Bacik 			&rc->reloc_root_tree.rb_root, rb_node)
35901a0afa0eSJosef Bacik 		kfree(node);
35911a0afa0eSJosef Bacik 
35921a0afa0eSJosef Bacik 	kfree(rc);
35931a0afa0eSJosef Bacik }
35941a0afa0eSJosef Bacik 
35955d4f98a2SYan Zheng /*
3596ebce0e01SAdam Borowski  * Print the block group being relocated
3597ebce0e01SAdam Borowski  */
3598ebce0e01SAdam Borowski static void describe_relocation(struct btrfs_fs_info *fs_info,
359932da5386SDavid Sterba 				struct btrfs_block_group *block_group)
3600ebce0e01SAdam Borowski {
3601f89e09cfSAnand Jain 	char buf[128] = {'\0'};
3602ebce0e01SAdam Borowski 
3603f89e09cfSAnand Jain 	btrfs_describe_block_groups(block_group->flags, buf, sizeof(buf));
3604ebce0e01SAdam Borowski 
3605ebce0e01SAdam Borowski 	btrfs_info(fs_info,
3606ebce0e01SAdam Borowski 		   "relocating block group %llu flags %s",
3607b3470b5dSDavid Sterba 		   block_group->start, buf);
3608ebce0e01SAdam Borowski }
3609ebce0e01SAdam Borowski 
3610430640e3SQu Wenruo static const char *stage_to_string(int stage)
3611430640e3SQu Wenruo {
3612430640e3SQu Wenruo 	if (stage == MOVE_DATA_EXTENTS)
3613430640e3SQu Wenruo 		return "move data extents";
3614430640e3SQu Wenruo 	if (stage == UPDATE_DATA_PTRS)
3615430640e3SQu Wenruo 		return "update data pointers";
3616430640e3SQu Wenruo 	return "unknown";
3617430640e3SQu Wenruo }
3618430640e3SQu Wenruo 
3619ebce0e01SAdam Borowski /*
36205d4f98a2SYan Zheng  * function to relocate all extents in a block group.
36215d4f98a2SYan Zheng  */
36226bccf3abSJeff Mahoney int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start)
36235d4f98a2SYan Zheng {
362432da5386SDavid Sterba 	struct btrfs_block_group *bg;
36256bccf3abSJeff Mahoney 	struct btrfs_root *extent_root = fs_info->extent_root;
36265d4f98a2SYan Zheng 	struct reloc_control *rc;
36270af3d00bSJosef Bacik 	struct inode *inode;
36280af3d00bSJosef Bacik 	struct btrfs_path *path;
36295d4f98a2SYan Zheng 	int ret;
3630f0486c68SYan, Zheng 	int rw = 0;
36315d4f98a2SYan Zheng 	int err = 0;
36325d4f98a2SYan Zheng 
3633eede2bf3SOmar Sandoval 	bg = btrfs_lookup_block_group(fs_info, group_start);
3634eede2bf3SOmar Sandoval 	if (!bg)
3635eede2bf3SOmar Sandoval 		return -ENOENT;
3636eede2bf3SOmar Sandoval 
3637eede2bf3SOmar Sandoval 	if (btrfs_pinned_by_swapfile(fs_info, bg)) {
3638eede2bf3SOmar Sandoval 		btrfs_put_block_group(bg);
3639eede2bf3SOmar Sandoval 		return -ETXTBSY;
3640eede2bf3SOmar Sandoval 	}
3641eede2bf3SOmar Sandoval 
3642c258d6e3SQu Wenruo 	rc = alloc_reloc_control(fs_info);
3643eede2bf3SOmar Sandoval 	if (!rc) {
3644eede2bf3SOmar Sandoval 		btrfs_put_block_group(bg);
36455d4f98a2SYan Zheng 		return -ENOMEM;
3646eede2bf3SOmar Sandoval 	}
36475d4f98a2SYan Zheng 
3648f0486c68SYan, Zheng 	rc->extent_root = extent_root;
3649eede2bf3SOmar Sandoval 	rc->block_group = bg;
36505d4f98a2SYan Zheng 
3651b12de528SQu Wenruo 	ret = btrfs_inc_block_group_ro(rc->block_group, true);
3652f0486c68SYan, Zheng 	if (ret) {
3653f0486c68SYan, Zheng 		err = ret;
3654f0486c68SYan, Zheng 		goto out;
3655f0486c68SYan, Zheng 	}
3656f0486c68SYan, Zheng 	rw = 1;
3657f0486c68SYan, Zheng 
36580af3d00bSJosef Bacik 	path = btrfs_alloc_path();
36590af3d00bSJosef Bacik 	if (!path) {
36600af3d00bSJosef Bacik 		err = -ENOMEM;
36610af3d00bSJosef Bacik 		goto out;
36620af3d00bSJosef Bacik 	}
36630af3d00bSJosef Bacik 
36647949f339SDavid Sterba 	inode = lookup_free_space_inode(rc->block_group, path);
36650af3d00bSJosef Bacik 	btrfs_free_path(path);
36660af3d00bSJosef Bacik 
36670af3d00bSJosef Bacik 	if (!IS_ERR(inode))
36681bbc621eSChris Mason 		ret = delete_block_group_cache(fs_info, rc->block_group, inode, 0);
36690af3d00bSJosef Bacik 	else
36700af3d00bSJosef Bacik 		ret = PTR_ERR(inode);
36710af3d00bSJosef Bacik 
36720af3d00bSJosef Bacik 	if (ret && ret != -ENOENT) {
36730af3d00bSJosef Bacik 		err = ret;
36740af3d00bSJosef Bacik 		goto out;
36750af3d00bSJosef Bacik 	}
36760af3d00bSJosef Bacik 
36775d4f98a2SYan Zheng 	rc->data_inode = create_reloc_inode(fs_info, rc->block_group);
36785d4f98a2SYan Zheng 	if (IS_ERR(rc->data_inode)) {
36795d4f98a2SYan Zheng 		err = PTR_ERR(rc->data_inode);
36805d4f98a2SYan Zheng 		rc->data_inode = NULL;
36815d4f98a2SYan Zheng 		goto out;
36825d4f98a2SYan Zheng 	}
36835d4f98a2SYan Zheng 
36840b246afaSJeff Mahoney 	describe_relocation(fs_info, rc->block_group);
36855d4f98a2SYan Zheng 
36869cfa3e34SFilipe Manana 	btrfs_wait_block_group_reservations(rc->block_group);
3687f78c436cSFilipe Manana 	btrfs_wait_nocow_writers(rc->block_group);
36886374e57aSChris Mason 	btrfs_wait_ordered_roots(fs_info, U64_MAX,
3689b3470b5dSDavid Sterba 				 rc->block_group->start,
3690b3470b5dSDavid Sterba 				 rc->block_group->length);
36915d4f98a2SYan Zheng 
36925d4f98a2SYan Zheng 	while (1) {
3693430640e3SQu Wenruo 		int finishes_stage;
3694430640e3SQu Wenruo 
369576dda93cSYan, Zheng 		mutex_lock(&fs_info->cleaner_mutex);
36965d4f98a2SYan Zheng 		ret = relocate_block_group(rc);
369776dda93cSYan, Zheng 		mutex_unlock(&fs_info->cleaner_mutex);
3698ff612ba7SJosef Bacik 		if (ret < 0)
36995d4f98a2SYan Zheng 			err = ret;
3700ff612ba7SJosef Bacik 
3701430640e3SQu Wenruo 		finishes_stage = rc->stage;
3702ff612ba7SJosef Bacik 		/*
3703ff612ba7SJosef Bacik 		 * We may have gotten ENOSPC after we already dirtied some
3704ff612ba7SJosef Bacik 		 * extents.  If writeout happens while we're relocating a
3705ff612ba7SJosef Bacik 		 * different block group we could end up hitting the
3706ff612ba7SJosef Bacik 		 * BUG_ON(rc->stage == UPDATE_DATA_PTRS) in
3707ff612ba7SJosef Bacik 		 * btrfs_reloc_cow_block.  Make sure we write everything out
3708ff612ba7SJosef Bacik 		 * properly so we don't trip over this problem, and then break
3709ff612ba7SJosef Bacik 		 * out of the loop if we hit an error.
3710ff612ba7SJosef Bacik 		 */
3711ff612ba7SJosef Bacik 		if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) {
3712ff612ba7SJosef Bacik 			ret = btrfs_wait_ordered_range(rc->data_inode, 0,
3713ff612ba7SJosef Bacik 						       (u64)-1);
3714ff612ba7SJosef Bacik 			if (ret)
3715ff612ba7SJosef Bacik 				err = ret;
3716ff612ba7SJosef Bacik 			invalidate_mapping_pages(rc->data_inode->i_mapping,
3717ff612ba7SJosef Bacik 						 0, -1);
3718ff612ba7SJosef Bacik 			rc->stage = UPDATE_DATA_PTRS;
37195d4f98a2SYan Zheng 		}
37205d4f98a2SYan Zheng 
3721ff612ba7SJosef Bacik 		if (err < 0)
3722ff612ba7SJosef Bacik 			goto out;
3723ff612ba7SJosef Bacik 
37245d4f98a2SYan Zheng 		if (rc->extents_found == 0)
37255d4f98a2SYan Zheng 			break;
37265d4f98a2SYan Zheng 
3727430640e3SQu Wenruo 		btrfs_info(fs_info, "found %llu extents, stage: %s",
3728430640e3SQu Wenruo 			   rc->extents_found, stage_to_string(finishes_stage));
37295d4f98a2SYan Zheng 	}
37305d4f98a2SYan Zheng 
37315d4f98a2SYan Zheng 	WARN_ON(rc->block_group->pinned > 0);
37325d4f98a2SYan Zheng 	WARN_ON(rc->block_group->reserved > 0);
3733bf38be65SDavid Sterba 	WARN_ON(rc->block_group->used > 0);
37345d4f98a2SYan Zheng out:
3735f0486c68SYan, Zheng 	if (err && rw)
37362ff7e61eSJeff Mahoney 		btrfs_dec_block_group_ro(rc->block_group);
37375d4f98a2SYan Zheng 	iput(rc->data_inode);
37385d4f98a2SYan Zheng 	btrfs_put_block_group(rc->block_group);
37391a0afa0eSJosef Bacik 	free_reloc_control(rc);
37405d4f98a2SYan Zheng 	return err;
37415d4f98a2SYan Zheng }
37425d4f98a2SYan Zheng 
374376dda93cSYan, Zheng static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
374476dda93cSYan, Zheng {
37450b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
374676dda93cSYan, Zheng 	struct btrfs_trans_handle *trans;
374779787eaaSJeff Mahoney 	int ret, err;
374876dda93cSYan, Zheng 
37490b246afaSJeff Mahoney 	trans = btrfs_start_transaction(fs_info->tree_root, 0);
375079787eaaSJeff Mahoney 	if (IS_ERR(trans))
375179787eaaSJeff Mahoney 		return PTR_ERR(trans);
375276dda93cSYan, Zheng 
375376dda93cSYan, Zheng 	memset(&root->root_item.drop_progress, 0,
375476dda93cSYan, Zheng 		sizeof(root->root_item.drop_progress));
375576dda93cSYan, Zheng 	root->root_item.drop_level = 0;
375676dda93cSYan, Zheng 	btrfs_set_root_refs(&root->root_item, 0);
37570b246afaSJeff Mahoney 	ret = btrfs_update_root(trans, fs_info->tree_root,
375876dda93cSYan, Zheng 				&root->root_key, &root->root_item);
375976dda93cSYan, Zheng 
37603a45bb20SJeff Mahoney 	err = btrfs_end_transaction(trans);
376179787eaaSJeff Mahoney 	if (err)
376279787eaaSJeff Mahoney 		return err;
376379787eaaSJeff Mahoney 	return ret;
376476dda93cSYan, Zheng }
376576dda93cSYan, Zheng 
37665d4f98a2SYan Zheng /*
37675d4f98a2SYan Zheng  * recover relocation interrupted by system crash.
37685d4f98a2SYan Zheng  *
37695d4f98a2SYan Zheng  * this function resumes merging reloc trees with corresponding fs trees.
37705d4f98a2SYan Zheng  * this is important for keeping the sharing of tree blocks
37715d4f98a2SYan Zheng  */
37725d4f98a2SYan Zheng int btrfs_recover_relocation(struct btrfs_root *root)
37735d4f98a2SYan Zheng {
37740b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
37755d4f98a2SYan Zheng 	LIST_HEAD(reloc_roots);
37765d4f98a2SYan Zheng 	struct btrfs_key key;
37775d4f98a2SYan Zheng 	struct btrfs_root *fs_root;
37785d4f98a2SYan Zheng 	struct btrfs_root *reloc_root;
37795d4f98a2SYan Zheng 	struct btrfs_path *path;
37805d4f98a2SYan Zheng 	struct extent_buffer *leaf;
37815d4f98a2SYan Zheng 	struct reloc_control *rc = NULL;
37825d4f98a2SYan Zheng 	struct btrfs_trans_handle *trans;
37835d4f98a2SYan Zheng 	int ret;
37845d4f98a2SYan Zheng 	int err = 0;
37855d4f98a2SYan Zheng 
37865d4f98a2SYan Zheng 	path = btrfs_alloc_path();
37875d4f98a2SYan Zheng 	if (!path)
37885d4f98a2SYan Zheng 		return -ENOMEM;
3789e4058b54SDavid Sterba 	path->reada = READA_BACK;
37905d4f98a2SYan Zheng 
37915d4f98a2SYan Zheng 	key.objectid = BTRFS_TREE_RELOC_OBJECTID;
37925d4f98a2SYan Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
37935d4f98a2SYan Zheng 	key.offset = (u64)-1;
37945d4f98a2SYan Zheng 
37955d4f98a2SYan Zheng 	while (1) {
37960b246afaSJeff Mahoney 		ret = btrfs_search_slot(NULL, fs_info->tree_root, &key,
37975d4f98a2SYan Zheng 					path, 0, 0);
37985d4f98a2SYan Zheng 		if (ret < 0) {
37995d4f98a2SYan Zheng 			err = ret;
38005d4f98a2SYan Zheng 			goto out;
38015d4f98a2SYan Zheng 		}
38025d4f98a2SYan Zheng 		if (ret > 0) {
38035d4f98a2SYan Zheng 			if (path->slots[0] == 0)
38045d4f98a2SYan Zheng 				break;
38055d4f98a2SYan Zheng 			path->slots[0]--;
38065d4f98a2SYan Zheng 		}
38075d4f98a2SYan Zheng 		leaf = path->nodes[0];
38085d4f98a2SYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3809b3b4aa74SDavid Sterba 		btrfs_release_path(path);
38105d4f98a2SYan Zheng 
38115d4f98a2SYan Zheng 		if (key.objectid != BTRFS_TREE_RELOC_OBJECTID ||
38125d4f98a2SYan Zheng 		    key.type != BTRFS_ROOT_ITEM_KEY)
38135d4f98a2SYan Zheng 			break;
38145d4f98a2SYan Zheng 
38153dbf1738SJosef Bacik 		reloc_root = btrfs_read_tree_root(root, &key);
38165d4f98a2SYan Zheng 		if (IS_ERR(reloc_root)) {
38175d4f98a2SYan Zheng 			err = PTR_ERR(reloc_root);
38185d4f98a2SYan Zheng 			goto out;
38195d4f98a2SYan Zheng 		}
38205d4f98a2SYan Zheng 
38213dbf1738SJosef Bacik 		set_bit(BTRFS_ROOT_REF_COWS, &reloc_root->state);
38225d4f98a2SYan Zheng 		list_add(&reloc_root->root_list, &reloc_roots);
38235d4f98a2SYan Zheng 
38245d4f98a2SYan Zheng 		if (btrfs_root_refs(&reloc_root->root_item) > 0) {
38250b246afaSJeff Mahoney 			fs_root = read_fs_root(fs_info,
38265d4f98a2SYan Zheng 					       reloc_root->root_key.offset);
38275d4f98a2SYan Zheng 			if (IS_ERR(fs_root)) {
382876dda93cSYan, Zheng 				ret = PTR_ERR(fs_root);
382976dda93cSYan, Zheng 				if (ret != -ENOENT) {
383076dda93cSYan, Zheng 					err = ret;
38315d4f98a2SYan Zheng 					goto out;
38325d4f98a2SYan Zheng 				}
383379787eaaSJeff Mahoney 				ret = mark_garbage_root(reloc_root);
383479787eaaSJeff Mahoney 				if (ret < 0) {
383579787eaaSJeff Mahoney 					err = ret;
383679787eaaSJeff Mahoney 					goto out;
383779787eaaSJeff Mahoney 				}
3838932fd26dSJosef Bacik 			} else {
383900246528SJosef Bacik 				btrfs_put_root(fs_root);
384076dda93cSYan, Zheng 			}
38415d4f98a2SYan Zheng 		}
38425d4f98a2SYan Zheng 
38435d4f98a2SYan Zheng 		if (key.offset == 0)
38445d4f98a2SYan Zheng 			break;
38455d4f98a2SYan Zheng 
38465d4f98a2SYan Zheng 		key.offset--;
38475d4f98a2SYan Zheng 	}
3848b3b4aa74SDavid Sterba 	btrfs_release_path(path);
38495d4f98a2SYan Zheng 
38505d4f98a2SYan Zheng 	if (list_empty(&reloc_roots))
38515d4f98a2SYan Zheng 		goto out;
38525d4f98a2SYan Zheng 
3853c258d6e3SQu Wenruo 	rc = alloc_reloc_control(fs_info);
38545d4f98a2SYan Zheng 	if (!rc) {
38555d4f98a2SYan Zheng 		err = -ENOMEM;
38565d4f98a2SYan Zheng 		goto out;
38575d4f98a2SYan Zheng 	}
38585d4f98a2SYan Zheng 
38590b246afaSJeff Mahoney 	rc->extent_root = fs_info->extent_root;
38605d4f98a2SYan Zheng 
38615d4f98a2SYan Zheng 	set_reloc_control(rc);
38625d4f98a2SYan Zheng 
38637a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(rc->extent_root);
38643612b495STsutomu Itoh 	if (IS_ERR(trans)) {
38653612b495STsutomu Itoh 		err = PTR_ERR(trans);
3866fb2d83eeSJosef Bacik 		goto out_unset;
38673612b495STsutomu Itoh 	}
38683fd0a558SYan, Zheng 
38693fd0a558SYan, Zheng 	rc->merge_reloc_tree = 1;
38703fd0a558SYan, Zheng 
38715d4f98a2SYan Zheng 	while (!list_empty(&reloc_roots)) {
38725d4f98a2SYan Zheng 		reloc_root = list_entry(reloc_roots.next,
38735d4f98a2SYan Zheng 					struct btrfs_root, root_list);
38745d4f98a2SYan Zheng 		list_del(&reloc_root->root_list);
38755d4f98a2SYan Zheng 
38765d4f98a2SYan Zheng 		if (btrfs_root_refs(&reloc_root->root_item) == 0) {
38775d4f98a2SYan Zheng 			list_add_tail(&reloc_root->root_list,
38785d4f98a2SYan Zheng 				      &rc->reloc_roots);
38795d4f98a2SYan Zheng 			continue;
38805d4f98a2SYan Zheng 		}
38815d4f98a2SYan Zheng 
38820b246afaSJeff Mahoney 		fs_root = read_fs_root(fs_info, reloc_root->root_key.offset);
388379787eaaSJeff Mahoney 		if (IS_ERR(fs_root)) {
388479787eaaSJeff Mahoney 			err = PTR_ERR(fs_root);
3885ca1aa281SJosef Bacik 			list_add_tail(&reloc_root->root_list, &reloc_roots);
38861402d17dSXiyu Yang 			btrfs_end_transaction(trans);
3887fb2d83eeSJosef Bacik 			goto out_unset;
388879787eaaSJeff Mahoney 		}
38895d4f98a2SYan Zheng 
3890ffd7b339SJeff Mahoney 		err = __add_reloc_root(reloc_root);
389179787eaaSJeff Mahoney 		BUG_ON(err < 0); /* -ENOMEM or logic error */
3892f44deb74SJosef Bacik 		fs_root->reloc_root = btrfs_grab_root(reloc_root);
389300246528SJosef Bacik 		btrfs_put_root(fs_root);
38945d4f98a2SYan Zheng 	}
38955d4f98a2SYan Zheng 
38963a45bb20SJeff Mahoney 	err = btrfs_commit_transaction(trans);
389779787eaaSJeff Mahoney 	if (err)
3898fb2d83eeSJosef Bacik 		goto out_unset;
38995d4f98a2SYan Zheng 
39005d4f98a2SYan Zheng 	merge_reloc_roots(rc);
39015d4f98a2SYan Zheng 
39025d4f98a2SYan Zheng 	unset_reloc_control(rc);
39035d4f98a2SYan Zheng 
39047a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(rc->extent_root);
390562b99540SQu Wenruo 	if (IS_ERR(trans)) {
39063612b495STsutomu Itoh 		err = PTR_ERR(trans);
39076217b0faSJosef Bacik 		goto out_clean;
390862b99540SQu Wenruo 	}
39093a45bb20SJeff Mahoney 	err = btrfs_commit_transaction(trans);
39106217b0faSJosef Bacik out_clean:
3911d2311e69SQu Wenruo 	ret = clean_dirty_subvols(rc);
3912d2311e69SQu Wenruo 	if (ret < 0 && !err)
3913d2311e69SQu Wenruo 		err = ret;
3914fb2d83eeSJosef Bacik out_unset:
3915fb2d83eeSJosef Bacik 	unset_reloc_control(rc);
39161a0afa0eSJosef Bacik 	free_reloc_control(rc);
39173612b495STsutomu Itoh out:
3918aca1bba6SLiu Bo 	if (!list_empty(&reloc_roots))
3919aca1bba6SLiu Bo 		free_reloc_roots(&reloc_roots);
3920aca1bba6SLiu Bo 
39215d4f98a2SYan Zheng 	btrfs_free_path(path);
39225d4f98a2SYan Zheng 
39235d4f98a2SYan Zheng 	if (err == 0) {
39245d4f98a2SYan Zheng 		/* cleanup orphan inode in data relocation tree */
39250b246afaSJeff Mahoney 		fs_root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID);
3926932fd26dSJosef Bacik 		if (IS_ERR(fs_root)) {
39275d4f98a2SYan Zheng 			err = PTR_ERR(fs_root);
3928932fd26dSJosef Bacik 		} else {
392966b4ffd1SJosef Bacik 			err = btrfs_orphan_cleanup(fs_root);
393000246528SJosef Bacik 			btrfs_put_root(fs_root);
3931932fd26dSJosef Bacik 		}
3932932fd26dSJosef Bacik 	}
39335d4f98a2SYan Zheng 	return err;
39345d4f98a2SYan Zheng }
39355d4f98a2SYan Zheng 
39365d4f98a2SYan Zheng /*
39375d4f98a2SYan Zheng  * helper to add ordered checksum for data relocation.
39385d4f98a2SYan Zheng  *
39395d4f98a2SYan Zheng  * cloning checksum properly handles the nodatasum extents.
39405d4f98a2SYan Zheng  * it also saves CPU time to re-calculate the checksum.
39415d4f98a2SYan Zheng  */
39425d4f98a2SYan Zheng int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
39435d4f98a2SYan Zheng {
39440b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
39455d4f98a2SYan Zheng 	struct btrfs_ordered_sum *sums;
39465d4f98a2SYan Zheng 	struct btrfs_ordered_extent *ordered;
39475d4f98a2SYan Zheng 	int ret;
39485d4f98a2SYan Zheng 	u64 disk_bytenr;
39494577b014SJosef Bacik 	u64 new_bytenr;
39505d4f98a2SYan Zheng 	LIST_HEAD(list);
39515d4f98a2SYan Zheng 
39525d4f98a2SYan Zheng 	ordered = btrfs_lookup_ordered_extent(inode, file_pos);
3953bffe633eSOmar Sandoval 	BUG_ON(ordered->file_offset != file_pos || ordered->num_bytes != len);
39545d4f98a2SYan Zheng 
39555d4f98a2SYan Zheng 	disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
39560b246afaSJeff Mahoney 	ret = btrfs_lookup_csums_range(fs_info->csum_root, disk_bytenr,
3957a2de733cSArne Jansen 				       disk_bytenr + len - 1, &list, 0);
395879787eaaSJeff Mahoney 	if (ret)
395979787eaaSJeff Mahoney 		goto out;
39605d4f98a2SYan Zheng 
39615d4f98a2SYan Zheng 	while (!list_empty(&list)) {
39625d4f98a2SYan Zheng 		sums = list_entry(list.next, struct btrfs_ordered_sum, list);
39635d4f98a2SYan Zheng 		list_del_init(&sums->list);
39645d4f98a2SYan Zheng 
39654577b014SJosef Bacik 		/*
39664577b014SJosef Bacik 		 * We need to offset the new_bytenr based on where the csum is.
39674577b014SJosef Bacik 		 * We need to do this because we will read in entire prealloc
39684577b014SJosef Bacik 		 * extents but we may have written to say the middle of the
39694577b014SJosef Bacik 		 * prealloc extent, so we need to make sure the csum goes with
39704577b014SJosef Bacik 		 * the right disk offset.
39714577b014SJosef Bacik 		 *
39724577b014SJosef Bacik 		 * We can do this because the data reloc inode refers strictly
39734577b014SJosef Bacik 		 * to the on disk bytes, so we don't have to worry about
39744577b014SJosef Bacik 		 * disk_len vs real len like with real inodes since it's all
39754577b014SJosef Bacik 		 * disk length.
39764577b014SJosef Bacik 		 */
3977bffe633eSOmar Sandoval 		new_bytenr = ordered->disk_bytenr + sums->bytenr - disk_bytenr;
39784577b014SJosef Bacik 		sums->bytenr = new_bytenr;
39795d4f98a2SYan Zheng 
3980f9756261SNikolay Borisov 		btrfs_add_ordered_sum(ordered, sums);
39815d4f98a2SYan Zheng 	}
398279787eaaSJeff Mahoney out:
39835d4f98a2SYan Zheng 	btrfs_put_ordered_extent(ordered);
3984411fc6bcSAndi Kleen 	return ret;
39855d4f98a2SYan Zheng }
39863fd0a558SYan, Zheng 
398783d4cfd4SJosef Bacik int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
39883fd0a558SYan, Zheng 			  struct btrfs_root *root, struct extent_buffer *buf,
39893fd0a558SYan, Zheng 			  struct extent_buffer *cow)
39903fd0a558SYan, Zheng {
39910b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
39923fd0a558SYan, Zheng 	struct reloc_control *rc;
3993a26195a5SQu Wenruo 	struct btrfs_backref_node *node;
39943fd0a558SYan, Zheng 	int first_cow = 0;
39953fd0a558SYan, Zheng 	int level;
399683d4cfd4SJosef Bacik 	int ret = 0;
39973fd0a558SYan, Zheng 
39980b246afaSJeff Mahoney 	rc = fs_info->reloc_ctl;
39993fd0a558SYan, Zheng 	if (!rc)
400083d4cfd4SJosef Bacik 		return 0;
40013fd0a558SYan, Zheng 
40023fd0a558SYan, Zheng 	BUG_ON(rc->stage == UPDATE_DATA_PTRS &&
40033fd0a558SYan, Zheng 	       root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID);
40043fd0a558SYan, Zheng 
40053fd0a558SYan, Zheng 	level = btrfs_header_level(buf);
40063fd0a558SYan, Zheng 	if (btrfs_header_generation(buf) <=
40073fd0a558SYan, Zheng 	    btrfs_root_last_snapshot(&root->root_item))
40083fd0a558SYan, Zheng 		first_cow = 1;
40093fd0a558SYan, Zheng 
40103fd0a558SYan, Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID &&
40113fd0a558SYan, Zheng 	    rc->create_reloc_tree) {
40123fd0a558SYan, Zheng 		WARN_ON(!first_cow && level == 0);
40133fd0a558SYan, Zheng 
40143fd0a558SYan, Zheng 		node = rc->backref_cache.path[level];
40153fd0a558SYan, Zheng 		BUG_ON(node->bytenr != buf->start &&
40163fd0a558SYan, Zheng 		       node->new_bytenr != buf->start);
40173fd0a558SYan, Zheng 
4018b0fe7078SQu Wenruo 		btrfs_backref_drop_node_buffer(node);
401967439dadSDavid Sterba 		atomic_inc(&cow->refs);
40203fd0a558SYan, Zheng 		node->eb = cow;
40213fd0a558SYan, Zheng 		node->new_bytenr = cow->start;
40223fd0a558SYan, Zheng 
40233fd0a558SYan, Zheng 		if (!node->pending) {
40243fd0a558SYan, Zheng 			list_move_tail(&node->list,
40253fd0a558SYan, Zheng 				       &rc->backref_cache.pending[level]);
40263fd0a558SYan, Zheng 			node->pending = 1;
40273fd0a558SYan, Zheng 		}
40283fd0a558SYan, Zheng 
40293fd0a558SYan, Zheng 		if (first_cow)
40309569cc20SQu Wenruo 			mark_block_processed(rc, node);
40313fd0a558SYan, Zheng 
40323fd0a558SYan, Zheng 		if (first_cow && level > 0)
40333fd0a558SYan, Zheng 			rc->nodes_relocated += buf->len;
40343fd0a558SYan, Zheng 	}
40353fd0a558SYan, Zheng 
403683d4cfd4SJosef Bacik 	if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS)
40373fd0a558SYan, Zheng 		ret = replace_file_extents(trans, rc, root, cow);
403883d4cfd4SJosef Bacik 	return ret;
40393fd0a558SYan, Zheng }
40403fd0a558SYan, Zheng 
40413fd0a558SYan, Zheng /*
40423fd0a558SYan, Zheng  * called before creating snapshot. it calculates metadata reservation
404301327610SNicholas D Steeves  * required for relocating tree blocks in the snapshot
40443fd0a558SYan, Zheng  */
4045147d256eSZhaolei void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending,
40463fd0a558SYan, Zheng 			      u64 *bytes_to_reserve)
40473fd0a558SYan, Zheng {
404810995c04SQu Wenruo 	struct btrfs_root *root = pending->root;
404910995c04SQu Wenruo 	struct reloc_control *rc = root->fs_info->reloc_ctl;
40503fd0a558SYan, Zheng 
40516282675eSQu Wenruo 	if (!rc || !have_reloc_root(root))
40523fd0a558SYan, Zheng 		return;
40533fd0a558SYan, Zheng 
40543fd0a558SYan, Zheng 	if (!rc->merge_reloc_tree)
40553fd0a558SYan, Zheng 		return;
40563fd0a558SYan, Zheng 
40573fd0a558SYan, Zheng 	root = root->reloc_root;
40583fd0a558SYan, Zheng 	BUG_ON(btrfs_root_refs(&root->root_item) == 0);
40593fd0a558SYan, Zheng 	/*
40603fd0a558SYan, Zheng 	 * relocation is in the stage of merging trees. the space
40613fd0a558SYan, Zheng 	 * used by merging a reloc tree is twice the size of
40623fd0a558SYan, Zheng 	 * relocated tree nodes in the worst case. half for cowing
40633fd0a558SYan, Zheng 	 * the reloc tree, half for cowing the fs tree. the space
40643fd0a558SYan, Zheng 	 * used by cowing the reloc tree will be freed after the
40653fd0a558SYan, Zheng 	 * tree is dropped. if we create snapshot, cowing the fs
40663fd0a558SYan, Zheng 	 * tree may use more space than it frees. so we need
40673fd0a558SYan, Zheng 	 * reserve extra space.
40683fd0a558SYan, Zheng 	 */
40693fd0a558SYan, Zheng 	*bytes_to_reserve += rc->nodes_relocated;
40703fd0a558SYan, Zheng }
40713fd0a558SYan, Zheng 
40723fd0a558SYan, Zheng /*
40733fd0a558SYan, Zheng  * called after snapshot is created. migrate block reservation
40743fd0a558SYan, Zheng  * and create reloc root for the newly created snapshot
4075f44deb74SJosef Bacik  *
4076f44deb74SJosef Bacik  * This is similar to btrfs_init_reloc_root(), we come out of here with two
4077f44deb74SJosef Bacik  * references held on the reloc_root, one for root->reloc_root and one for
4078f44deb74SJosef Bacik  * rc->reloc_roots.
40793fd0a558SYan, Zheng  */
408049b25e05SJeff Mahoney int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
40813fd0a558SYan, Zheng 			       struct btrfs_pending_snapshot *pending)
40823fd0a558SYan, Zheng {
40833fd0a558SYan, Zheng 	struct btrfs_root *root = pending->root;
40843fd0a558SYan, Zheng 	struct btrfs_root *reloc_root;
40853fd0a558SYan, Zheng 	struct btrfs_root *new_root;
408610995c04SQu Wenruo 	struct reloc_control *rc = root->fs_info->reloc_ctl;
40873fd0a558SYan, Zheng 	int ret;
40883fd0a558SYan, Zheng 
40896282675eSQu Wenruo 	if (!rc || !have_reloc_root(root))
409049b25e05SJeff Mahoney 		return 0;
40913fd0a558SYan, Zheng 
40923fd0a558SYan, Zheng 	rc = root->fs_info->reloc_ctl;
40933fd0a558SYan, Zheng 	rc->merging_rsv_size += rc->nodes_relocated;
40943fd0a558SYan, Zheng 
40953fd0a558SYan, Zheng 	if (rc->merge_reloc_tree) {
40963fd0a558SYan, Zheng 		ret = btrfs_block_rsv_migrate(&pending->block_rsv,
40973fd0a558SYan, Zheng 					      rc->block_rsv,
40983a584174SLu Fengqi 					      rc->nodes_relocated, true);
409949b25e05SJeff Mahoney 		if (ret)
410049b25e05SJeff Mahoney 			return ret;
41013fd0a558SYan, Zheng 	}
41023fd0a558SYan, Zheng 
41033fd0a558SYan, Zheng 	new_root = pending->snap;
41043fd0a558SYan, Zheng 	reloc_root = create_reloc_root(trans, root->reloc_root,
41053fd0a558SYan, Zheng 				       new_root->root_key.objectid);
410649b25e05SJeff Mahoney 	if (IS_ERR(reloc_root))
410749b25e05SJeff Mahoney 		return PTR_ERR(reloc_root);
41083fd0a558SYan, Zheng 
4109ffd7b339SJeff Mahoney 	ret = __add_reloc_root(reloc_root);
4110ffd7b339SJeff Mahoney 	BUG_ON(ret < 0);
4111f44deb74SJosef Bacik 	new_root->reloc_root = btrfs_grab_root(reloc_root);
41123fd0a558SYan, Zheng 
411349b25e05SJeff Mahoney 	if (rc->create_reloc_tree)
41143fd0a558SYan, Zheng 		ret = clone_backref_node(trans, rc, root, reloc_root);
411549b25e05SJeff Mahoney 	return ret;
41163fd0a558SYan, Zheng }
4117