xref: /openbmc/linux/fs/btrfs/relocation.c (revision d5e09e38)
1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
25d4f98a2SYan Zheng /*
35d4f98a2SYan Zheng  * Copyright (C) 2009 Oracle.  All rights reserved.
45d4f98a2SYan Zheng  */
55d4f98a2SYan Zheng 
65d4f98a2SYan Zheng #include <linux/sched.h>
75d4f98a2SYan Zheng #include <linux/pagemap.h>
85d4f98a2SYan Zheng #include <linux/writeback.h>
95d4f98a2SYan Zheng #include <linux/blkdev.h>
105d4f98a2SYan Zheng #include <linux/rbtree.h>
115a0e3ad6STejun Heo #include <linux/slab.h>
12726a3421SQu Wenruo #include <linux/error-injection.h>
135d4f98a2SYan Zheng #include "ctree.h"
145d4f98a2SYan Zheng #include "disk-io.h"
155d4f98a2SYan Zheng #include "transaction.h"
165d4f98a2SYan Zheng #include "volumes.h"
175d4f98a2SYan Zheng #include "locking.h"
185d4f98a2SYan Zheng #include "btrfs_inode.h"
195d4f98a2SYan Zheng #include "async-thread.h"
200af3d00bSJosef Bacik #include "free-space-cache.h"
2162b99540SQu Wenruo #include "qgroup.h"
22cdccee99SLiu Bo #include "print-tree.h"
2386736342SJosef Bacik #include "delalloc-space.h"
24aac0023cSJosef Bacik #include "block-group.h"
2519b546d7SQu Wenruo #include "backref.h"
26e9a28dc5SQu Wenruo #include "misc.h"
27c2832898SQu Wenruo #include "subpage.h"
287ae9bd18SNaohiro Aota #include "zoned.h"
2926c2c454SJosef Bacik #include "inode-item.h"
30f1e5c618SJosef Bacik #include "space-info.h"
31c7f13d42SJosef Bacik #include "fs.h"
3207e81dc9SJosef Bacik #include "accessors.h"
33a0231804SJosef Bacik #include "extent-tree.h"
3445c40c8fSJosef Bacik #include "root-tree.h"
357c8ede16SJosef Bacik #include "file-item.h"
3667707479SJosef Bacik #include "relocation.h"
377f0add25SJosef Bacik #include "super.h"
38103c1972SChristoph Hellwig #include "tree-checker.h"
395d4f98a2SYan Zheng 
405d4f98a2SYan Zheng /*
410c891389SQu Wenruo  * Relocation overview
420c891389SQu Wenruo  *
430c891389SQu Wenruo  * [What does relocation do]
440c891389SQu Wenruo  *
450c891389SQu Wenruo  * The objective of relocation is to relocate all extents of the target block
460c891389SQu Wenruo  * group to other block groups.
470c891389SQu Wenruo  * This is utilized by resize (shrink only), profile converting, compacting
480c891389SQu Wenruo  * space, or balance routine to spread chunks over devices.
490c891389SQu Wenruo  *
500c891389SQu Wenruo  * 		Before		|		After
510c891389SQu Wenruo  * ------------------------------------------------------------------
520c891389SQu Wenruo  *  BG A: 10 data extents	| BG A: deleted
530c891389SQu Wenruo  *  BG B:  2 data extents	| BG B: 10 data extents (2 old + 8 relocated)
540c891389SQu Wenruo  *  BG C:  1 extents		| BG C:  3 data extents (1 old + 2 relocated)
550c891389SQu Wenruo  *
560c891389SQu Wenruo  * [How does relocation work]
570c891389SQu Wenruo  *
580c891389SQu Wenruo  * 1.   Mark the target block group read-only
590c891389SQu Wenruo  *      New extents won't be allocated from the target block group.
600c891389SQu Wenruo  *
610c891389SQu Wenruo  * 2.1  Record each extent in the target block group
620c891389SQu Wenruo  *      To build a proper map of extents to be relocated.
630c891389SQu Wenruo  *
640c891389SQu Wenruo  * 2.2  Build data reloc tree and reloc trees
650c891389SQu Wenruo  *      Data reloc tree will contain an inode, recording all newly relocated
660c891389SQu Wenruo  *      data extents.
670c891389SQu Wenruo  *      There will be only one data reloc tree for one data block group.
680c891389SQu Wenruo  *
690c891389SQu Wenruo  *      Reloc tree will be a special snapshot of its source tree, containing
700c891389SQu Wenruo  *      relocated tree blocks.
710c891389SQu Wenruo  *      Each tree referring to a tree block in target block group will get its
720c891389SQu Wenruo  *      reloc tree built.
730c891389SQu Wenruo  *
740c891389SQu Wenruo  * 2.3  Swap source tree with its corresponding reloc tree
750c891389SQu Wenruo  *      Each involved tree only refers to new extents after swap.
760c891389SQu Wenruo  *
770c891389SQu Wenruo  * 3.   Cleanup reloc trees and data reloc tree.
780c891389SQu Wenruo  *      As old extents in the target block group are still referenced by reloc
790c891389SQu Wenruo  *      trees, we need to clean them up before really freeing the target block
800c891389SQu Wenruo  *      group.
810c891389SQu Wenruo  *
820c891389SQu Wenruo  * The main complexity is in steps 2.2 and 2.3.
830c891389SQu Wenruo  *
840c891389SQu Wenruo  * The entry point of relocation is relocate_block_group() function.
850c891389SQu Wenruo  */
860c891389SQu Wenruo 
870647bf56SWang Shilong #define RELOCATION_RESERVED_NODES	256
882a979612SQu Wenruo /*
895d4f98a2SYan Zheng  * map address of tree root to tree
905d4f98a2SYan Zheng  */
915d4f98a2SYan Zheng struct mapping_node {
92e9a28dc5SQu Wenruo 	struct {
935d4f98a2SYan Zheng 		struct rb_node rb_node;
945d4f98a2SYan Zheng 		u64 bytenr;
95e9a28dc5SQu Wenruo 	}; /* Use rb_simle_node for search/insert */
965d4f98a2SYan Zheng 	void *data;
975d4f98a2SYan Zheng };
985d4f98a2SYan Zheng 
995d4f98a2SYan Zheng struct mapping_tree {
1005d4f98a2SYan Zheng 	struct rb_root rb_root;
1015d4f98a2SYan Zheng 	spinlock_t lock;
1025d4f98a2SYan Zheng };
1035d4f98a2SYan Zheng 
1045d4f98a2SYan Zheng /*
1055d4f98a2SYan Zheng  * present a tree block to process
1065d4f98a2SYan Zheng  */
1075d4f98a2SYan Zheng struct tree_block {
108e9a28dc5SQu Wenruo 	struct {
1095d4f98a2SYan Zheng 		struct rb_node rb_node;
1105d4f98a2SYan Zheng 		u64 bytenr;
111e9a28dc5SQu Wenruo 	}; /* Use rb_simple_node for search/insert */
112f7ba2d37SJosef Bacik 	u64 owner;
1135d4f98a2SYan Zheng 	struct btrfs_key key;
1145d4f98a2SYan Zheng 	unsigned int level:8;
1155d4f98a2SYan Zheng 	unsigned int key_ready:1;
1165d4f98a2SYan Zheng };
1175d4f98a2SYan Zheng 
1180257bb82SYan, Zheng #define MAX_EXTENTS 128
1190257bb82SYan, Zheng 
1200257bb82SYan, Zheng struct file_extent_cluster {
1210257bb82SYan, Zheng 	u64 start;
1220257bb82SYan, Zheng 	u64 end;
1230257bb82SYan, Zheng 	u64 boundary[MAX_EXTENTS];
1240257bb82SYan, Zheng 	unsigned int nr;
1250257bb82SYan, Zheng };
1260257bb82SYan, Zheng 
1275d4f98a2SYan Zheng struct reloc_control {
1285d4f98a2SYan Zheng 	/* block group to relocate */
12932da5386SDavid Sterba 	struct btrfs_block_group *block_group;
1305d4f98a2SYan Zheng 	/* extent tree */
1315d4f98a2SYan Zheng 	struct btrfs_root *extent_root;
1325d4f98a2SYan Zheng 	/* inode for moving data */
1335d4f98a2SYan Zheng 	struct inode *data_inode;
1343fd0a558SYan, Zheng 
1353fd0a558SYan, Zheng 	struct btrfs_block_rsv *block_rsv;
1363fd0a558SYan, Zheng 
137a26195a5SQu Wenruo 	struct btrfs_backref_cache backref_cache;
1383fd0a558SYan, Zheng 
1393fd0a558SYan, Zheng 	struct file_extent_cluster cluster;
1405d4f98a2SYan Zheng 	/* tree blocks have been processed */
1415d4f98a2SYan Zheng 	struct extent_io_tree processed_blocks;
1425d4f98a2SYan Zheng 	/* map start of tree root to corresponding reloc tree */
1435d4f98a2SYan Zheng 	struct mapping_tree reloc_root_tree;
1445d4f98a2SYan Zheng 	/* list of reloc trees */
1455d4f98a2SYan Zheng 	struct list_head reloc_roots;
146d2311e69SQu Wenruo 	/* list of subvolume trees that get relocated */
147d2311e69SQu Wenruo 	struct list_head dirty_subvol_roots;
1483fd0a558SYan, Zheng 	/* size of metadata reservation for merging reloc trees */
1493fd0a558SYan, Zheng 	u64 merging_rsv_size;
1503fd0a558SYan, Zheng 	/* size of relocated tree nodes */
1513fd0a558SYan, Zheng 	u64 nodes_relocated;
1520647bf56SWang Shilong 	/* reserved size for block group relocation*/
1530647bf56SWang Shilong 	u64 reserved_bytes;
1543fd0a558SYan, Zheng 
1555d4f98a2SYan Zheng 	u64 search_start;
1565d4f98a2SYan Zheng 	u64 extents_found;
1573fd0a558SYan, Zheng 
1583fd0a558SYan, Zheng 	unsigned int stage:8;
1593fd0a558SYan, Zheng 	unsigned int create_reloc_tree:1;
1603fd0a558SYan, Zheng 	unsigned int merge_reloc_tree:1;
1615d4f98a2SYan Zheng 	unsigned int found_file_extent:1;
1625d4f98a2SYan Zheng };
1635d4f98a2SYan Zheng 
1645d4f98a2SYan Zheng /* stages of data relocation */
1655d4f98a2SYan Zheng #define MOVE_DATA_EXTENTS	0
1665d4f98a2SYan Zheng #define UPDATE_DATA_PTRS	1
1675d4f98a2SYan Zheng 
mark_block_processed(struct reloc_control * rc,struct btrfs_backref_node * node)1689569cc20SQu Wenruo static void mark_block_processed(struct reloc_control *rc,
169a26195a5SQu Wenruo 				 struct btrfs_backref_node *node)
1709569cc20SQu Wenruo {
1719569cc20SQu Wenruo 	u32 blocksize;
1729569cc20SQu Wenruo 
1739569cc20SQu Wenruo 	if (node->level == 0 ||
1749569cc20SQu Wenruo 	    in_range(node->bytenr, rc->block_group->start,
1759569cc20SQu Wenruo 		     rc->block_group->length)) {
1769569cc20SQu Wenruo 		blocksize = rc->extent_root->fs_info->nodesize;
1770acd32c2SDavid Sterba 		set_extent_bit(&rc->processed_blocks, node->bytenr,
1781d126800SDavid Sterba 			       node->bytenr + blocksize - 1, EXTENT_DIRTY, NULL);
1799569cc20SQu Wenruo 	}
1809569cc20SQu Wenruo 	node->processed = 1;
1819569cc20SQu Wenruo }
1829569cc20SQu Wenruo 
1835d4f98a2SYan Zheng 
mapping_tree_init(struct mapping_tree * tree)1845d4f98a2SYan Zheng static void mapping_tree_init(struct mapping_tree *tree)
1855d4f98a2SYan Zheng {
1866bef4d31SEric Paris 	tree->rb_root = RB_ROOT;
1875d4f98a2SYan Zheng 	spin_lock_init(&tree->lock);
1885d4f98a2SYan Zheng }
1895d4f98a2SYan Zheng 
1905d4f98a2SYan Zheng /*
1915d4f98a2SYan Zheng  * walk up backref nodes until reach node presents tree root
1925d4f98a2SYan Zheng  */
walk_up_backref(struct btrfs_backref_node * node,struct btrfs_backref_edge * edges[],int * index)193a26195a5SQu Wenruo static struct btrfs_backref_node *walk_up_backref(
194a26195a5SQu Wenruo 		struct btrfs_backref_node *node,
195a26195a5SQu Wenruo 		struct btrfs_backref_edge *edges[], int *index)
1965d4f98a2SYan Zheng {
197a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
1985d4f98a2SYan Zheng 	int idx = *index;
1995d4f98a2SYan Zheng 
2005d4f98a2SYan Zheng 	while (!list_empty(&node->upper)) {
2015d4f98a2SYan Zheng 		edge = list_entry(node->upper.next,
202a26195a5SQu Wenruo 				  struct btrfs_backref_edge, list[LOWER]);
2035d4f98a2SYan Zheng 		edges[idx++] = edge;
2045d4f98a2SYan Zheng 		node = edge->node[UPPER];
2055d4f98a2SYan Zheng 	}
2063fd0a558SYan, Zheng 	BUG_ON(node->detached);
2075d4f98a2SYan Zheng 	*index = idx;
2085d4f98a2SYan Zheng 	return node;
2095d4f98a2SYan Zheng }
2105d4f98a2SYan Zheng 
2115d4f98a2SYan Zheng /*
2125d4f98a2SYan Zheng  * walk down backref nodes to find start of next reference path
2135d4f98a2SYan Zheng  */
walk_down_backref(struct btrfs_backref_edge * edges[],int * index)214a26195a5SQu Wenruo static struct btrfs_backref_node *walk_down_backref(
215a26195a5SQu Wenruo 		struct btrfs_backref_edge *edges[], int *index)
2165d4f98a2SYan Zheng {
217a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
218a26195a5SQu Wenruo 	struct btrfs_backref_node *lower;
2195d4f98a2SYan Zheng 	int idx = *index;
2205d4f98a2SYan Zheng 
2215d4f98a2SYan Zheng 	while (idx > 0) {
2225d4f98a2SYan Zheng 		edge = edges[idx - 1];
2235d4f98a2SYan Zheng 		lower = edge->node[LOWER];
2245d4f98a2SYan Zheng 		if (list_is_last(&edge->list[LOWER], &lower->upper)) {
2255d4f98a2SYan Zheng 			idx--;
2265d4f98a2SYan Zheng 			continue;
2275d4f98a2SYan Zheng 		}
2285d4f98a2SYan Zheng 		edge = list_entry(edge->list[LOWER].next,
229a26195a5SQu Wenruo 				  struct btrfs_backref_edge, list[LOWER]);
2305d4f98a2SYan Zheng 		edges[idx - 1] = edge;
2315d4f98a2SYan Zheng 		*index = idx;
2325d4f98a2SYan Zheng 		return edge->node[UPPER];
2335d4f98a2SYan Zheng 	}
2345d4f98a2SYan Zheng 	*index = 0;
2355d4f98a2SYan Zheng 	return NULL;
2365d4f98a2SYan Zheng }
2375d4f98a2SYan Zheng 
update_backref_node(struct btrfs_backref_cache * cache,struct btrfs_backref_node * node,u64 bytenr)238a26195a5SQu Wenruo static void update_backref_node(struct btrfs_backref_cache *cache,
239a26195a5SQu Wenruo 				struct btrfs_backref_node *node, u64 bytenr)
2403fd0a558SYan, Zheng {
2413fd0a558SYan, Zheng 	struct rb_node *rb_node;
2423fd0a558SYan, Zheng 	rb_erase(&node->rb_node, &cache->rb_root);
2433fd0a558SYan, Zheng 	node->bytenr = bytenr;
244e9a28dc5SQu Wenruo 	rb_node = rb_simple_insert(&cache->rb_root, node->bytenr, &node->rb_node);
24543c04fb1SJeff Mahoney 	if (rb_node)
246982c92cbSQu Wenruo 		btrfs_backref_panic(cache->fs_info, bytenr, -EEXIST);
2473fd0a558SYan, Zheng }
2483fd0a558SYan, Zheng 
2493fd0a558SYan, Zheng /*
2503fd0a558SYan, Zheng  * update backref cache after a transaction commit
2513fd0a558SYan, Zheng  */
update_backref_cache(struct btrfs_trans_handle * trans,struct btrfs_backref_cache * cache)2523fd0a558SYan, Zheng static int update_backref_cache(struct btrfs_trans_handle *trans,
253a26195a5SQu Wenruo 				struct btrfs_backref_cache *cache)
2543fd0a558SYan, Zheng {
255a26195a5SQu Wenruo 	struct btrfs_backref_node *node;
2563fd0a558SYan, Zheng 	int level = 0;
2573fd0a558SYan, Zheng 
2583fd0a558SYan, Zheng 	if (cache->last_trans == 0) {
2593fd0a558SYan, Zheng 		cache->last_trans = trans->transid;
2603fd0a558SYan, Zheng 		return 0;
2613fd0a558SYan, Zheng 	}
2623fd0a558SYan, Zheng 
2633fd0a558SYan, Zheng 	if (cache->last_trans == trans->transid)
2643fd0a558SYan, Zheng 		return 0;
2653fd0a558SYan, Zheng 
2663fd0a558SYan, Zheng 	/*
2673fd0a558SYan, Zheng 	 * detached nodes are used to avoid unnecessary backref
2683fd0a558SYan, Zheng 	 * lookup. transaction commit changes the extent tree.
2693fd0a558SYan, Zheng 	 * so the detached nodes are no longer useful.
2703fd0a558SYan, Zheng 	 */
2713fd0a558SYan, Zheng 	while (!list_empty(&cache->detached)) {
2723fd0a558SYan, Zheng 		node = list_entry(cache->detached.next,
273a26195a5SQu Wenruo 				  struct btrfs_backref_node, list);
274023acb07SQu Wenruo 		btrfs_backref_cleanup_node(cache, node);
2753fd0a558SYan, Zheng 	}
2763fd0a558SYan, Zheng 
2773fd0a558SYan, Zheng 	while (!list_empty(&cache->changed)) {
2783fd0a558SYan, Zheng 		node = list_entry(cache->changed.next,
279a26195a5SQu Wenruo 				  struct btrfs_backref_node, list);
2803fd0a558SYan, Zheng 		list_del_init(&node->list);
2813fd0a558SYan, Zheng 		BUG_ON(node->pending);
2823fd0a558SYan, Zheng 		update_backref_node(cache, node, node->new_bytenr);
2833fd0a558SYan, Zheng 	}
2843fd0a558SYan, Zheng 
2853fd0a558SYan, Zheng 	/*
2863fd0a558SYan, Zheng 	 * some nodes can be left in the pending list if there were
2873fd0a558SYan, Zheng 	 * errors during processing the pending nodes.
2883fd0a558SYan, Zheng 	 */
2893fd0a558SYan, Zheng 	for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2903fd0a558SYan, Zheng 		list_for_each_entry(node, &cache->pending[level], list) {
2913fd0a558SYan, Zheng 			BUG_ON(!node->pending);
2923fd0a558SYan, Zheng 			if (node->bytenr == node->new_bytenr)
2933fd0a558SYan, Zheng 				continue;
2943fd0a558SYan, Zheng 			update_backref_node(cache, node, node->new_bytenr);
2953fd0a558SYan, Zheng 		}
2963fd0a558SYan, Zheng 	}
2973fd0a558SYan, Zheng 
2983fd0a558SYan, Zheng 	cache->last_trans = 0;
2993fd0a558SYan, Zheng 	return 1;
3003fd0a558SYan, Zheng }
3013fd0a558SYan, Zheng 
reloc_root_is_dead(struct btrfs_root * root)3026282675eSQu Wenruo static bool reloc_root_is_dead(struct btrfs_root *root)
3036282675eSQu Wenruo {
3046282675eSQu Wenruo 	/*
3056282675eSQu Wenruo 	 * Pair with set_bit/clear_bit in clean_dirty_subvols and
3066282675eSQu Wenruo 	 * btrfs_update_reloc_root. We need to see the updated bit before
3076282675eSQu Wenruo 	 * trying to access reloc_root
3086282675eSQu Wenruo 	 */
3096282675eSQu Wenruo 	smp_rmb();
3106282675eSQu Wenruo 	if (test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state))
3116282675eSQu Wenruo 		return true;
3126282675eSQu Wenruo 	return false;
3136282675eSQu Wenruo }
3146282675eSQu Wenruo 
3156282675eSQu Wenruo /*
3166282675eSQu Wenruo  * Check if this subvolume tree has valid reloc tree.
3176282675eSQu Wenruo  *
3186282675eSQu Wenruo  * Reloc tree after swap is considered dead, thus not considered as valid.
3196282675eSQu Wenruo  * This is enough for most callers, as they don't distinguish dead reloc root
32055465730SQu Wenruo  * from no reloc root.  But btrfs_should_ignore_reloc_root() below is a
32155465730SQu Wenruo  * special case.
3226282675eSQu Wenruo  */
have_reloc_root(struct btrfs_root * root)3236282675eSQu Wenruo static bool have_reloc_root(struct btrfs_root *root)
3246282675eSQu Wenruo {
3256282675eSQu Wenruo 	if (reloc_root_is_dead(root))
3266282675eSQu Wenruo 		return false;
3276282675eSQu Wenruo 	if (!root->reloc_root)
3286282675eSQu Wenruo 		return false;
3296282675eSQu Wenruo 	return true;
3306282675eSQu Wenruo }
331f2a97a9dSDavid Sterba 
btrfs_should_ignore_reloc_root(struct btrfs_root * root)33255465730SQu Wenruo int btrfs_should_ignore_reloc_root(struct btrfs_root *root)
3333fd0a558SYan, Zheng {
3343fd0a558SYan, Zheng 	struct btrfs_root *reloc_root;
3353fd0a558SYan, Zheng 
33692a7cc42SQu Wenruo 	if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
3373fd0a558SYan, Zheng 		return 0;
3383fd0a558SYan, Zheng 
3396282675eSQu Wenruo 	/* This root has been merged with its reloc tree, we can ignore it */
3406282675eSQu Wenruo 	if (reloc_root_is_dead(root))
3416282675eSQu Wenruo 		return 1;
3426282675eSQu Wenruo 
3433fd0a558SYan, Zheng 	reloc_root = root->reloc_root;
3443fd0a558SYan, Zheng 	if (!reloc_root)
3453fd0a558SYan, Zheng 		return 0;
3463fd0a558SYan, Zheng 
3474d4225fcSJosef Bacik 	if (btrfs_header_generation(reloc_root->commit_root) ==
3484d4225fcSJosef Bacik 	    root->fs_info->running_transaction->transid)
3493fd0a558SYan, Zheng 		return 0;
3503fd0a558SYan, Zheng 	/*
3513fd0a558SYan, Zheng 	 * if there is reloc tree and it was created in previous
3523fd0a558SYan, Zheng 	 * transaction backref lookup can find the reloc tree,
3533fd0a558SYan, Zheng 	 * so backref node for the fs tree root is useless for
3543fd0a558SYan, Zheng 	 * relocation.
3553fd0a558SYan, Zheng 	 */
3563fd0a558SYan, Zheng 	return 1;
3573fd0a558SYan, Zheng }
35855465730SQu Wenruo 
3595d4f98a2SYan Zheng /*
3605d4f98a2SYan Zheng  * find reloc tree by address of tree root
3615d4f98a2SYan Zheng  */
find_reloc_root(struct btrfs_fs_info * fs_info,u64 bytenr)3622433bea5SQu Wenruo struct btrfs_root *find_reloc_root(struct btrfs_fs_info *fs_info, u64 bytenr)
3635d4f98a2SYan Zheng {
3642433bea5SQu Wenruo 	struct reloc_control *rc = fs_info->reloc_ctl;
3655d4f98a2SYan Zheng 	struct rb_node *rb_node;
3665d4f98a2SYan Zheng 	struct mapping_node *node;
3675d4f98a2SYan Zheng 	struct btrfs_root *root = NULL;
3685d4f98a2SYan Zheng 
3692433bea5SQu Wenruo 	ASSERT(rc);
3705d4f98a2SYan Zheng 	spin_lock(&rc->reloc_root_tree.lock);
371e9a28dc5SQu Wenruo 	rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root, bytenr);
3725d4f98a2SYan Zheng 	if (rb_node) {
3735d4f98a2SYan Zheng 		node = rb_entry(rb_node, struct mapping_node, rb_node);
3740d031dc4SYu Zhe 		root = node->data;
3755d4f98a2SYan Zheng 	}
3765d4f98a2SYan Zheng 	spin_unlock(&rc->reloc_root_tree.lock);
37700246528SJosef Bacik 	return btrfs_grab_root(root);
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  */
handle_useless_nodes(struct reloc_control * rc,struct btrfs_backref_node * node)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  */
build_backref_tree(struct btrfs_trans_handle * trans,struct reloc_control * rc,struct btrfs_key * node_key,int level,u64 bytenr)468a26195a5SQu Wenruo static noinline_for_stack struct btrfs_backref_node *build_backref_tree(
469eb96e221SFilipe Manana 			struct btrfs_trans_handle *trans,
470e7d571c7SQu Wenruo 			struct reloc_control *rc, struct btrfs_key *node_key,
471e7d571c7SQu Wenruo 			int level, u64 bytenr)
472e7d571c7SQu Wenruo {
473e7d571c7SQu Wenruo 	struct btrfs_backref_iter *iter;
474a26195a5SQu Wenruo 	struct btrfs_backref_cache *cache = &rc->backref_cache;
475e7d571c7SQu Wenruo 	/* For searching parent of TREE_BLOCK_REF */
476e7d571c7SQu Wenruo 	struct btrfs_path *path;
477a26195a5SQu Wenruo 	struct btrfs_backref_node *cur;
478a26195a5SQu Wenruo 	struct btrfs_backref_node *node = NULL;
479a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
480e7d571c7SQu Wenruo 	int ret;
481e7d571c7SQu Wenruo 	int err = 0;
482e7d571c7SQu Wenruo 
483d68194b2SDavid Sterba 	iter = btrfs_backref_iter_alloc(rc->extent_root->fs_info);
484e7d571c7SQu Wenruo 	if (!iter)
485e7d571c7SQu Wenruo 		return ERR_PTR(-ENOMEM);
486e7d571c7SQu Wenruo 	path = btrfs_alloc_path();
487e7d571c7SQu Wenruo 	if (!path) {
488e7d571c7SQu Wenruo 		err = -ENOMEM;
489e7d571c7SQu Wenruo 		goto out;
490e7d571c7SQu Wenruo 	}
491e7d571c7SQu Wenruo 
492b1818dabSQu Wenruo 	node = btrfs_backref_alloc_node(cache, bytenr, level);
493e7d571c7SQu Wenruo 	if (!node) {
494e7d571c7SQu Wenruo 		err = -ENOMEM;
495e7d571c7SQu Wenruo 		goto out;
496e7d571c7SQu Wenruo 	}
497e7d571c7SQu Wenruo 
498e7d571c7SQu Wenruo 	node->lowest = 1;
499e7d571c7SQu Wenruo 	cur = node;
500e7d571c7SQu Wenruo 
501e7d571c7SQu Wenruo 	/* Breadth-first search to build backref cache */
502e7d571c7SQu Wenruo 	do {
503eb96e221SFilipe Manana 		ret = btrfs_backref_add_tree_node(trans, cache, path, iter,
504eb96e221SFilipe Manana 						  node_key, cur);
505e7d571c7SQu Wenruo 		if (ret < 0) {
506e7d571c7SQu Wenruo 			err = ret;
507e7d571c7SQu Wenruo 			goto out;
508e7d571c7SQu Wenruo 		}
509e7d571c7SQu Wenruo 		edge = list_first_entry_or_null(&cache->pending_edge,
510a26195a5SQu Wenruo 				struct btrfs_backref_edge, list[UPPER]);
511e7d571c7SQu Wenruo 		/*
512e7d571c7SQu Wenruo 		 * The pending list isn't empty, take the first block to
513e7d571c7SQu Wenruo 		 * process
514e7d571c7SQu Wenruo 		 */
515e7d571c7SQu Wenruo 		if (edge) {
5165d4f98a2SYan Zheng 			list_del_init(&edge->list[UPPER]);
5175d4f98a2SYan Zheng 			cur = edge->node[UPPER];
5185d4f98a2SYan Zheng 		}
519e7d571c7SQu Wenruo 	} while (edge);
5205d4f98a2SYan Zheng 
5211f872924SQu Wenruo 	/* Finish the upper linkage of newly added edges/nodes */
522fc997ed0SQu Wenruo 	ret = btrfs_backref_finish_upper_links(cache, node);
5231f872924SQu Wenruo 	if (ret < 0) {
5241f872924SQu Wenruo 		err = ret;
52575bfb9afSJosef Bacik 		goto out;
52675bfb9afSJosef Bacik 	}
52775bfb9afSJosef Bacik 
52829db137bSQu Wenruo 	if (handle_useless_nodes(rc, node))
5293fd0a558SYan, Zheng 		node = NULL;
5305d4f98a2SYan Zheng out:
53171f572a9SQu Wenruo 	btrfs_backref_iter_free(iter);
53271f572a9SQu Wenruo 	btrfs_free_path(path);
5335d4f98a2SYan Zheng 	if (err) {
5341b23ea18SQu Wenruo 		btrfs_backref_error_cleanup(cache, node);
5355d4f98a2SYan Zheng 		return ERR_PTR(err);
5365d4f98a2SYan Zheng 	}
53775bfb9afSJosef Bacik 	ASSERT(!node || !node->detached);
53884780289SQu Wenruo 	ASSERT(list_empty(&cache->useless_node) &&
53984780289SQu Wenruo 	       list_empty(&cache->pending_edge));
5405d4f98a2SYan Zheng 	return node;
5415d4f98a2SYan Zheng }
5425d4f98a2SYan Zheng 
5435d4f98a2SYan Zheng /*
5443fd0a558SYan, Zheng  * helper to add backref node for the newly created snapshot.
5453fd0a558SYan, Zheng  * the backref node is created by cloning backref node that
5463fd0a558SYan, Zheng  * corresponds to root of source tree
5473fd0a558SYan, Zheng  */
clone_backref_node(struct btrfs_trans_handle * trans,struct reloc_control * rc,struct btrfs_root * src,struct btrfs_root * dest)5483fd0a558SYan, Zheng static int clone_backref_node(struct btrfs_trans_handle *trans,
5493fd0a558SYan, Zheng 			      struct reloc_control *rc,
5503fd0a558SYan, Zheng 			      struct btrfs_root *src,
5513fd0a558SYan, Zheng 			      struct btrfs_root *dest)
5523fd0a558SYan, Zheng {
5533fd0a558SYan, Zheng 	struct btrfs_root *reloc_root = src->reloc_root;
554a26195a5SQu Wenruo 	struct btrfs_backref_cache *cache = &rc->backref_cache;
555a26195a5SQu Wenruo 	struct btrfs_backref_node *node = NULL;
556a26195a5SQu Wenruo 	struct btrfs_backref_node *new_node;
557a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
558a26195a5SQu Wenruo 	struct btrfs_backref_edge *new_edge;
5593fd0a558SYan, Zheng 	struct rb_node *rb_node;
5603fd0a558SYan, Zheng 
5613fd0a558SYan, Zheng 	if (cache->last_trans > 0)
5623fd0a558SYan, Zheng 		update_backref_cache(trans, cache);
5633fd0a558SYan, Zheng 
564e9a28dc5SQu Wenruo 	rb_node = rb_simple_search(&cache->rb_root, src->commit_root->start);
5653fd0a558SYan, Zheng 	if (rb_node) {
566a26195a5SQu Wenruo 		node = rb_entry(rb_node, struct btrfs_backref_node, rb_node);
5673fd0a558SYan, Zheng 		if (node->detached)
5683fd0a558SYan, Zheng 			node = NULL;
5693fd0a558SYan, Zheng 		else
5703fd0a558SYan, Zheng 			BUG_ON(node->new_bytenr != reloc_root->node->start);
5713fd0a558SYan, Zheng 	}
5723fd0a558SYan, Zheng 
5733fd0a558SYan, Zheng 	if (!node) {
574e9a28dc5SQu Wenruo 		rb_node = rb_simple_search(&cache->rb_root,
5753fd0a558SYan, Zheng 					   reloc_root->commit_root->start);
5763fd0a558SYan, Zheng 		if (rb_node) {
577a26195a5SQu Wenruo 			node = rb_entry(rb_node, struct btrfs_backref_node,
5783fd0a558SYan, Zheng 					rb_node);
5793fd0a558SYan, Zheng 			BUG_ON(node->detached);
5803fd0a558SYan, Zheng 		}
5813fd0a558SYan, Zheng 	}
5823fd0a558SYan, Zheng 
5833fd0a558SYan, Zheng 	if (!node)
5843fd0a558SYan, Zheng 		return 0;
5853fd0a558SYan, Zheng 
586b1818dabSQu Wenruo 	new_node = btrfs_backref_alloc_node(cache, dest->node->start,
587b1818dabSQu Wenruo 					    node->level);
5883fd0a558SYan, Zheng 	if (!new_node)
5893fd0a558SYan, Zheng 		return -ENOMEM;
5903fd0a558SYan, Zheng 
5913fd0a558SYan, Zheng 	new_node->lowest = node->lowest;
5926848ad64SYan, Zheng 	new_node->checked = 1;
59300246528SJosef Bacik 	new_node->root = btrfs_grab_root(dest);
5940b530bc5SJosef Bacik 	ASSERT(new_node->root);
5953fd0a558SYan, Zheng 
5963fd0a558SYan, Zheng 	if (!node->lowest) {
5973fd0a558SYan, Zheng 		list_for_each_entry(edge, &node->lower, list[UPPER]) {
59847254d07SQu Wenruo 			new_edge = btrfs_backref_alloc_edge(cache);
5993fd0a558SYan, Zheng 			if (!new_edge)
6003fd0a558SYan, Zheng 				goto fail;
6013fd0a558SYan, Zheng 
602f39911e5SQu Wenruo 			btrfs_backref_link_edge(new_edge, edge->node[LOWER],
603f39911e5SQu Wenruo 						new_node, LINK_UPPER);
6043fd0a558SYan, Zheng 		}
60576b9e23dSMiao Xie 	} else {
60676b9e23dSMiao Xie 		list_add_tail(&new_node->lower, &cache->leaves);
6073fd0a558SYan, Zheng 	}
6083fd0a558SYan, Zheng 
609e9a28dc5SQu Wenruo 	rb_node = rb_simple_insert(&cache->rb_root, new_node->bytenr,
6103fd0a558SYan, Zheng 				   &new_node->rb_node);
61143c04fb1SJeff Mahoney 	if (rb_node)
612982c92cbSQu Wenruo 		btrfs_backref_panic(trans->fs_info, new_node->bytenr, -EEXIST);
6133fd0a558SYan, Zheng 
6143fd0a558SYan, Zheng 	if (!new_node->lowest) {
6153fd0a558SYan, Zheng 		list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) {
6163fd0a558SYan, Zheng 			list_add_tail(&new_edge->list[LOWER],
6173fd0a558SYan, Zheng 				      &new_edge->node[LOWER]->upper);
6183fd0a558SYan, Zheng 		}
6193fd0a558SYan, Zheng 	}
6203fd0a558SYan, Zheng 	return 0;
6213fd0a558SYan, Zheng fail:
6223fd0a558SYan, Zheng 	while (!list_empty(&new_node->lower)) {
6233fd0a558SYan, Zheng 		new_edge = list_entry(new_node->lower.next,
624a26195a5SQu Wenruo 				      struct btrfs_backref_edge, list[UPPER]);
6253fd0a558SYan, Zheng 		list_del(&new_edge->list[UPPER]);
626741188d3SQu Wenruo 		btrfs_backref_free_edge(cache, new_edge);
6273fd0a558SYan, Zheng 	}
628741188d3SQu Wenruo 	btrfs_backref_free_node(cache, new_node);
6293fd0a558SYan, Zheng 	return -ENOMEM;
6303fd0a558SYan, Zheng }
6313fd0a558SYan, Zheng 
6323fd0a558SYan, Zheng /*
6335d4f98a2SYan Zheng  * helper to add 'address of tree root -> reloc tree' mapping
6345d4f98a2SYan Zheng  */
__add_reloc_root(struct btrfs_root * root)635ffd7b339SJeff Mahoney static int __must_check __add_reloc_root(struct btrfs_root *root)
6365d4f98a2SYan Zheng {
6370b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
6385d4f98a2SYan Zheng 	struct rb_node *rb_node;
6395d4f98a2SYan Zheng 	struct mapping_node *node;
6400b246afaSJeff Mahoney 	struct reloc_control *rc = fs_info->reloc_ctl;
6415d4f98a2SYan Zheng 
6425d4f98a2SYan Zheng 	node = kmalloc(sizeof(*node), GFP_NOFS);
643ffd7b339SJeff Mahoney 	if (!node)
644ffd7b339SJeff Mahoney 		return -ENOMEM;
6455d4f98a2SYan Zheng 
646ea287ab1SJosef Bacik 	node->bytenr = root->commit_root->start;
6475d4f98a2SYan Zheng 	node->data = root;
6485d4f98a2SYan Zheng 
6495d4f98a2SYan Zheng 	spin_lock(&rc->reloc_root_tree.lock);
650e9a28dc5SQu Wenruo 	rb_node = rb_simple_insert(&rc->reloc_root_tree.rb_root,
6515d4f98a2SYan Zheng 				   node->bytenr, &node->rb_node);
6525d4f98a2SYan Zheng 	spin_unlock(&rc->reloc_root_tree.lock);
653ffd7b339SJeff Mahoney 	if (rb_node) {
65457a304cfSJosef Bacik 		btrfs_err(fs_info,
6555d163e0eSJeff Mahoney 			    "Duplicate root found for start=%llu while inserting into relocation tree",
6565d163e0eSJeff Mahoney 			    node->bytenr);
65757a304cfSJosef Bacik 		return -EEXIST;
658ffd7b339SJeff Mahoney 	}
6595d4f98a2SYan Zheng 
6605d4f98a2SYan Zheng 	list_add_tail(&root->root_list, &rc->reloc_roots);
6615d4f98a2SYan Zheng 	return 0;
6625d4f98a2SYan Zheng }
6635d4f98a2SYan Zheng 
6645d4f98a2SYan Zheng /*
665c974c464SWang Shilong  * helper to delete the 'address of tree root -> reloc tree'
6665d4f98a2SYan Zheng  * mapping
6675d4f98a2SYan Zheng  */
__del_reloc_root(struct btrfs_root * root)668c974c464SWang Shilong static void __del_reloc_root(struct btrfs_root *root)
6695d4f98a2SYan Zheng {
6700b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
6715d4f98a2SYan Zheng 	struct rb_node *rb_node;
6725d4f98a2SYan Zheng 	struct mapping_node *node = NULL;
6730b246afaSJeff Mahoney 	struct reloc_control *rc = fs_info->reloc_ctl;
674f44deb74SJosef Bacik 	bool put_ref = false;
6755d4f98a2SYan Zheng 
67665c6e82bSQu Wenruo 	if (rc && root->node) {
6775d4f98a2SYan Zheng 		spin_lock(&rc->reloc_root_tree.lock);
678e9a28dc5SQu Wenruo 		rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root,
679ea287ab1SJosef Bacik 					   root->commit_root->start);
680c974c464SWang Shilong 		if (rb_node) {
681c974c464SWang Shilong 			node = rb_entry(rb_node, struct mapping_node, rb_node);
682c974c464SWang Shilong 			rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
683ea287ab1SJosef Bacik 			RB_CLEAR_NODE(&node->rb_node);
684c974c464SWang Shilong 		}
685c974c464SWang Shilong 		spin_unlock(&rc->reloc_root_tree.lock);
686c78a10aeSJosef Bacik 		ASSERT(!node || (struct btrfs_root *)node->data == root);
687389305b2SQu Wenruo 	}
688c974c464SWang Shilong 
689f44deb74SJosef Bacik 	/*
690f44deb74SJosef Bacik 	 * We only put the reloc root here if it's on the list.  There's a lot
691f44deb74SJosef Bacik 	 * of places where the pattern is to splice the rc->reloc_roots, process
692f44deb74SJosef Bacik 	 * the reloc roots, and then add the reloc root back onto
693f44deb74SJosef Bacik 	 * rc->reloc_roots.  If we call __del_reloc_root while it's off of the
694f44deb74SJosef Bacik 	 * list we don't want the reference being dropped, because the guy
695f44deb74SJosef Bacik 	 * messing with the list is in charge of the reference.
696f44deb74SJosef Bacik 	 */
6970b246afaSJeff Mahoney 	spin_lock(&fs_info->trans_lock);
698f44deb74SJosef Bacik 	if (!list_empty(&root->root_list)) {
699f44deb74SJosef Bacik 		put_ref = true;
700c974c464SWang Shilong 		list_del_init(&root->root_list);
701f44deb74SJosef Bacik 	}
7020b246afaSJeff Mahoney 	spin_unlock(&fs_info->trans_lock);
703f44deb74SJosef Bacik 	if (put_ref)
704f44deb74SJosef Bacik 		btrfs_put_root(root);
705c974c464SWang Shilong 	kfree(node);
706c974c464SWang Shilong }
707c974c464SWang Shilong 
708c974c464SWang Shilong /*
709c974c464SWang Shilong  * helper to update the 'address of tree root -> reloc tree'
710c974c464SWang Shilong  * mapping
711c974c464SWang Shilong  */
__update_reloc_root(struct btrfs_root * root)712ea287ab1SJosef Bacik static int __update_reloc_root(struct btrfs_root *root)
713c974c464SWang Shilong {
7140b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
715c974c464SWang Shilong 	struct rb_node *rb_node;
716c974c464SWang Shilong 	struct mapping_node *node = NULL;
7170b246afaSJeff Mahoney 	struct reloc_control *rc = fs_info->reloc_ctl;
718c974c464SWang Shilong 
719c974c464SWang Shilong 	spin_lock(&rc->reloc_root_tree.lock);
720e9a28dc5SQu Wenruo 	rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root,
721ea287ab1SJosef Bacik 				   root->commit_root->start);
7225d4f98a2SYan Zheng 	if (rb_node) {
7235d4f98a2SYan Zheng 		node = rb_entry(rb_node, struct mapping_node, rb_node);
7245d4f98a2SYan Zheng 		rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
7255d4f98a2SYan Zheng 	}
7265d4f98a2SYan Zheng 	spin_unlock(&rc->reloc_root_tree.lock);
7275d4f98a2SYan Zheng 
7288f71f3e0SLiu Bo 	if (!node)
7298f71f3e0SLiu Bo 		return 0;
7305d4f98a2SYan Zheng 	BUG_ON((struct btrfs_root *)node->data != root);
7315d4f98a2SYan Zheng 
7325d4f98a2SYan Zheng 	spin_lock(&rc->reloc_root_tree.lock);
733ea287ab1SJosef Bacik 	node->bytenr = root->node->start;
734e9a28dc5SQu Wenruo 	rb_node = rb_simple_insert(&rc->reloc_root_tree.rb_root,
7355d4f98a2SYan Zheng 				   node->bytenr, &node->rb_node);
7365d4f98a2SYan Zheng 	spin_unlock(&rc->reloc_root_tree.lock);
73743c04fb1SJeff Mahoney 	if (rb_node)
738982c92cbSQu Wenruo 		btrfs_backref_panic(fs_info, node->bytenr, -EEXIST);
7395d4f98a2SYan Zheng 	return 0;
7405d4f98a2SYan Zheng }
7415d4f98a2SYan Zheng 
create_reloc_root(struct btrfs_trans_handle * trans,struct btrfs_root * root,u64 objectid)7423fd0a558SYan, Zheng static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
7433fd0a558SYan, Zheng 					struct btrfs_root *root, u64 objectid)
7445d4f98a2SYan Zheng {
7450b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
7465d4f98a2SYan Zheng 	struct btrfs_root *reloc_root;
7475d4f98a2SYan Zheng 	struct extent_buffer *eb;
7485d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
7495d4f98a2SYan Zheng 	struct btrfs_key root_key;
75084c50ba5SJosef Bacik 	int ret = 0;
75184c50ba5SJosef Bacik 	bool must_abort = false;
7525d4f98a2SYan Zheng 
7535d4f98a2SYan Zheng 	root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
75484c50ba5SJosef Bacik 	if (!root_item)
75584c50ba5SJosef Bacik 		return ERR_PTR(-ENOMEM);
7565d4f98a2SYan Zheng 
7575d4f98a2SYan Zheng 	root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
7585d4f98a2SYan Zheng 	root_key.type = BTRFS_ROOT_ITEM_KEY;
7593fd0a558SYan, Zheng 	root_key.offset = objectid;
7605d4f98a2SYan Zheng 
7613fd0a558SYan, Zheng 	if (root->root_key.objectid == objectid) {
762054570a1SFilipe Manana 		u64 commit_root_gen;
763054570a1SFilipe Manana 
7643fd0a558SYan, Zheng 		/* called by btrfs_init_reloc_root */
7655d4f98a2SYan Zheng 		ret = btrfs_copy_root(trans, root, root->commit_root, &eb,
7665d4f98a2SYan Zheng 				      BTRFS_TREE_RELOC_OBJECTID);
76784c50ba5SJosef Bacik 		if (ret)
76884c50ba5SJosef Bacik 			goto fail;
76984c50ba5SJosef Bacik 
770054570a1SFilipe Manana 		/*
771054570a1SFilipe Manana 		 * Set the last_snapshot field to the generation of the commit
772054570a1SFilipe Manana 		 * root - like this ctree.c:btrfs_block_can_be_shared() behaves
773054570a1SFilipe Manana 		 * correctly (returns true) when the relocation root is created
774054570a1SFilipe Manana 		 * either inside the critical section of a transaction commit
775054570a1SFilipe Manana 		 * (through transaction.c:qgroup_account_snapshot()) and when
776054570a1SFilipe Manana 		 * it's created before the transaction commit is started.
777054570a1SFilipe Manana 		 */
778054570a1SFilipe Manana 		commit_root_gen = btrfs_header_generation(root->commit_root);
779054570a1SFilipe Manana 		btrfs_set_root_last_snapshot(&root->root_item, commit_root_gen);
7803fd0a558SYan, Zheng 	} else {
7813fd0a558SYan, Zheng 		/*
7823fd0a558SYan, Zheng 		 * called by btrfs_reloc_post_snapshot_hook.
7833fd0a558SYan, Zheng 		 * the source tree is a reloc tree, all tree blocks
7843fd0a558SYan, Zheng 		 * modified after it was created have RELOC flag
7853fd0a558SYan, Zheng 		 * set in their headers. so it's OK to not update
7863fd0a558SYan, Zheng 		 * the 'last_snapshot'.
7873fd0a558SYan, Zheng 		 */
7883fd0a558SYan, Zheng 		ret = btrfs_copy_root(trans, root, root->node, &eb,
7893fd0a558SYan, Zheng 				      BTRFS_TREE_RELOC_OBJECTID);
79084c50ba5SJosef Bacik 		if (ret)
79184c50ba5SJosef Bacik 			goto fail;
7923fd0a558SYan, Zheng 	}
7933fd0a558SYan, Zheng 
79484c50ba5SJosef Bacik 	/*
79584c50ba5SJosef Bacik 	 * We have changed references at this point, we must abort the
79684c50ba5SJosef Bacik 	 * transaction if anything fails.
79784c50ba5SJosef Bacik 	 */
79884c50ba5SJosef Bacik 	must_abort = true;
79984c50ba5SJosef Bacik 
8005d4f98a2SYan Zheng 	memcpy(root_item, &root->root_item, sizeof(*root_item));
8015d4f98a2SYan Zheng 	btrfs_set_root_bytenr(root_item, eb->start);
8025d4f98a2SYan Zheng 	btrfs_set_root_level(root_item, btrfs_header_level(eb));
8035d4f98a2SYan Zheng 	btrfs_set_root_generation(root_item, trans->transid);
8043fd0a558SYan, Zheng 
8053fd0a558SYan, Zheng 	if (root->root_key.objectid == objectid) {
8063fd0a558SYan, Zheng 		btrfs_set_root_refs(root_item, 0);
8073fd0a558SYan, Zheng 		memset(&root_item->drop_progress, 0,
8083fd0a558SYan, Zheng 		       sizeof(struct btrfs_disk_key));
809c8422684SDavid Sterba 		btrfs_set_root_drop_level(root_item, 0);
8103fd0a558SYan, Zheng 	}
8115d4f98a2SYan Zheng 
8125d4f98a2SYan Zheng 	btrfs_tree_unlock(eb);
8135d4f98a2SYan Zheng 	free_extent_buffer(eb);
8145d4f98a2SYan Zheng 
8150b246afaSJeff Mahoney 	ret = btrfs_insert_root(trans, fs_info->tree_root,
8165d4f98a2SYan Zheng 				&root_key, root_item);
81784c50ba5SJosef Bacik 	if (ret)
81884c50ba5SJosef Bacik 		goto fail;
81984c50ba5SJosef Bacik 
8205d4f98a2SYan Zheng 	kfree(root_item);
8215d4f98a2SYan Zheng 
8223dbf1738SJosef Bacik 	reloc_root = btrfs_read_tree_root(fs_info->tree_root, &root_key);
82384c50ba5SJosef Bacik 	if (IS_ERR(reloc_root)) {
82484c50ba5SJosef Bacik 		ret = PTR_ERR(reloc_root);
82584c50ba5SJosef Bacik 		goto abort;
82684c50ba5SJosef Bacik 	}
82792a7cc42SQu Wenruo 	set_bit(BTRFS_ROOT_SHAREABLE, &reloc_root->state);
8285d4f98a2SYan Zheng 	reloc_root->last_trans = trans->transid;
8293fd0a558SYan, Zheng 	return reloc_root;
83084c50ba5SJosef Bacik fail:
83184c50ba5SJosef Bacik 	kfree(root_item);
83284c50ba5SJosef Bacik abort:
83384c50ba5SJosef Bacik 	if (must_abort)
83484c50ba5SJosef Bacik 		btrfs_abort_transaction(trans, ret);
83584c50ba5SJosef Bacik 	return ERR_PTR(ret);
8363fd0a558SYan, Zheng }
8373fd0a558SYan, Zheng 
8383fd0a558SYan, Zheng /*
8393fd0a558SYan, Zheng  * create reloc tree for a given fs tree. reloc tree is just a
8403fd0a558SYan, Zheng  * snapshot of the fs tree with special root objectid.
841f44deb74SJosef Bacik  *
842f44deb74SJosef Bacik  * The reloc_root comes out of here with two references, one for
843f44deb74SJosef Bacik  * root->reloc_root, and another for being on the rc->reloc_roots list.
8443fd0a558SYan, Zheng  */
btrfs_init_reloc_root(struct btrfs_trans_handle * trans,struct btrfs_root * root)8453fd0a558SYan, Zheng int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
8463fd0a558SYan, Zheng 			  struct btrfs_root *root)
8473fd0a558SYan, Zheng {
8480b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
8493fd0a558SYan, Zheng 	struct btrfs_root *reloc_root;
8500b246afaSJeff Mahoney 	struct reloc_control *rc = fs_info->reloc_ctl;
85120dd2cbfSMiao Xie 	struct btrfs_block_rsv *rsv;
8523fd0a558SYan, Zheng 	int clear_rsv = 0;
853ffd7b339SJeff Mahoney 	int ret;
8543fd0a558SYan, Zheng 
855aec7db3bSJosef Bacik 	if (!rc)
8562abc726aSJosef Bacik 		return 0;
8572abc726aSJosef Bacik 
8581fac4a54SQu Wenruo 	/*
8591fac4a54SQu Wenruo 	 * The subvolume has reloc tree but the swap is finished, no need to
8601fac4a54SQu Wenruo 	 * create/update the dead reloc tree
8611fac4a54SQu Wenruo 	 */
8626282675eSQu Wenruo 	if (reloc_root_is_dead(root))
8631fac4a54SQu Wenruo 		return 0;
8641fac4a54SQu Wenruo 
865aec7db3bSJosef Bacik 	/*
866aec7db3bSJosef Bacik 	 * This is subtle but important.  We do not do
867aec7db3bSJosef Bacik 	 * record_root_in_transaction for reloc roots, instead we record their
868aec7db3bSJosef Bacik 	 * corresponding fs root, and then here we update the last trans for the
869aec7db3bSJosef Bacik 	 * reloc root.  This means that we have to do this for the entire life
870aec7db3bSJosef Bacik 	 * of the reloc root, regardless of which stage of the relocation we are
871aec7db3bSJosef Bacik 	 * in.
872aec7db3bSJosef Bacik 	 */
8733fd0a558SYan, Zheng 	if (root->reloc_root) {
8743fd0a558SYan, Zheng 		reloc_root = root->reloc_root;
8753fd0a558SYan, Zheng 		reloc_root->last_trans = trans->transid;
8763fd0a558SYan, Zheng 		return 0;
8773fd0a558SYan, Zheng 	}
8783fd0a558SYan, Zheng 
879aec7db3bSJosef Bacik 	/*
880aec7db3bSJosef Bacik 	 * We are merging reloc roots, we do not need new reloc trees.  Also
881aec7db3bSJosef Bacik 	 * reloc trees never need their own reloc tree.
882aec7db3bSJosef Bacik 	 */
883aec7db3bSJosef Bacik 	if (!rc->create_reloc_tree ||
884aec7db3bSJosef Bacik 	    root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
885aec7db3bSJosef Bacik 		return 0;
886aec7db3bSJosef Bacik 
88720dd2cbfSMiao Xie 	if (!trans->reloc_reserved) {
88820dd2cbfSMiao Xie 		rsv = trans->block_rsv;
8893fd0a558SYan, Zheng 		trans->block_rsv = rc->block_rsv;
8903fd0a558SYan, Zheng 		clear_rsv = 1;
8913fd0a558SYan, Zheng 	}
8923fd0a558SYan, Zheng 	reloc_root = create_reloc_root(trans, root, root->root_key.objectid);
8933fd0a558SYan, Zheng 	if (clear_rsv)
89420dd2cbfSMiao Xie 		trans->block_rsv = rsv;
89500bb36a0SJosef Bacik 	if (IS_ERR(reloc_root))
89600bb36a0SJosef Bacik 		return PTR_ERR(reloc_root);
8975d4f98a2SYan Zheng 
898ffd7b339SJeff Mahoney 	ret = __add_reloc_root(reloc_root);
89957a304cfSJosef Bacik 	ASSERT(ret != -EEXIST);
90000bb36a0SJosef Bacik 	if (ret) {
90100bb36a0SJosef Bacik 		/* Pairs with create_reloc_root */
90200bb36a0SJosef Bacik 		btrfs_put_root(reloc_root);
90300bb36a0SJosef Bacik 		return ret;
90400bb36a0SJosef Bacik 	}
905f44deb74SJosef Bacik 	root->reloc_root = btrfs_grab_root(reloc_root);
9065d4f98a2SYan Zheng 	return 0;
9075d4f98a2SYan Zheng }
9085d4f98a2SYan Zheng 
9095d4f98a2SYan Zheng /*
9105d4f98a2SYan Zheng  * update root item of reloc tree
9115d4f98a2SYan Zheng  */
btrfs_update_reloc_root(struct btrfs_trans_handle * trans,struct btrfs_root * root)9125d4f98a2SYan Zheng int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
9135d4f98a2SYan Zheng 			    struct btrfs_root *root)
9145d4f98a2SYan Zheng {
9150b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
9165d4f98a2SYan Zheng 	struct btrfs_root *reloc_root;
9175d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
9185d4f98a2SYan Zheng 	int ret;
9195d4f98a2SYan Zheng 
9206282675eSQu Wenruo 	if (!have_reloc_root(root))
921592fbcd5SJosef Bacik 		return 0;
9225d4f98a2SYan Zheng 
9235d4f98a2SYan Zheng 	reloc_root = root->reloc_root;
9245d4f98a2SYan Zheng 	root_item = &reloc_root->root_item;
9255d4f98a2SYan Zheng 
926f44deb74SJosef Bacik 	/*
927f44deb74SJosef Bacik 	 * We are probably ok here, but __del_reloc_root() will drop its ref of
928f44deb74SJosef Bacik 	 * the root.  We have the ref for root->reloc_root, but just in case
929f44deb74SJosef Bacik 	 * hold it while we update the reloc root.
930f44deb74SJosef Bacik 	 */
931f44deb74SJosef Bacik 	btrfs_grab_root(reloc_root);
932f44deb74SJosef Bacik 
933d2311e69SQu Wenruo 	/* root->reloc_root will stay until current relocation finished */
9340b246afaSJeff Mahoney 	if (fs_info->reloc_ctl->merge_reloc_tree &&
9353fd0a558SYan, Zheng 	    btrfs_root_refs(root_item) == 0) {
936d2311e69SQu Wenruo 		set_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
9376282675eSQu Wenruo 		/*
9386282675eSQu Wenruo 		 * Mark the tree as dead before we change reloc_root so
9396282675eSQu Wenruo 		 * have_reloc_root will not touch it from now on.
9406282675eSQu Wenruo 		 */
9416282675eSQu Wenruo 		smp_wmb();
942c974c464SWang Shilong 		__del_reloc_root(reloc_root);
9435d4f98a2SYan Zheng 	}
9445d4f98a2SYan Zheng 
9455d4f98a2SYan Zheng 	if (reloc_root->commit_root != reloc_root->node) {
946ea287ab1SJosef Bacik 		__update_reloc_root(reloc_root);
9475d4f98a2SYan Zheng 		btrfs_set_root_node(root_item, reloc_root->node);
9485d4f98a2SYan Zheng 		free_extent_buffer(reloc_root->commit_root);
9495d4f98a2SYan Zheng 		reloc_root->commit_root = btrfs_root_node(reloc_root);
9505d4f98a2SYan Zheng 	}
9515d4f98a2SYan Zheng 
9520b246afaSJeff Mahoney 	ret = btrfs_update_root(trans, fs_info->tree_root,
9535d4f98a2SYan Zheng 				&reloc_root->root_key, root_item);
954f44deb74SJosef Bacik 	btrfs_put_root(reloc_root);
955592fbcd5SJosef Bacik 	return ret;
9565d4f98a2SYan Zheng }
9575d4f98a2SYan Zheng 
9585d4f98a2SYan Zheng /*
9595d4f98a2SYan Zheng  * helper to find first cached inode with inode number >= objectid
9605d4f98a2SYan Zheng  * in a subvolume
9615d4f98a2SYan Zheng  */
find_next_inode(struct btrfs_root * root,u64 objectid)9625d4f98a2SYan Zheng static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid)
9635d4f98a2SYan Zheng {
9645d4f98a2SYan Zheng 	struct rb_node *node;
9655d4f98a2SYan Zheng 	struct rb_node *prev;
9665d4f98a2SYan Zheng 	struct btrfs_inode *entry;
9675d4f98a2SYan Zheng 	struct inode *inode;
9685d4f98a2SYan Zheng 
9695d4f98a2SYan Zheng 	spin_lock(&root->inode_lock);
9705d4f98a2SYan Zheng again:
9715d4f98a2SYan Zheng 	node = root->inode_tree.rb_node;
9725d4f98a2SYan Zheng 	prev = NULL;
9735d4f98a2SYan Zheng 	while (node) {
9745d4f98a2SYan Zheng 		prev = node;
9755d4f98a2SYan Zheng 		entry = rb_entry(node, struct btrfs_inode, rb_node);
9765d4f98a2SYan Zheng 
9774a0cc7caSNikolay Borisov 		if (objectid < btrfs_ino(entry))
9785d4f98a2SYan Zheng 			node = node->rb_left;
9794a0cc7caSNikolay Borisov 		else if (objectid > btrfs_ino(entry))
9805d4f98a2SYan Zheng 			node = node->rb_right;
9815d4f98a2SYan Zheng 		else
9825d4f98a2SYan Zheng 			break;
9835d4f98a2SYan Zheng 	}
9845d4f98a2SYan Zheng 	if (!node) {
9855d4f98a2SYan Zheng 		while (prev) {
9865d4f98a2SYan Zheng 			entry = rb_entry(prev, struct btrfs_inode, rb_node);
9874a0cc7caSNikolay Borisov 			if (objectid <= btrfs_ino(entry)) {
9885d4f98a2SYan Zheng 				node = prev;
9895d4f98a2SYan Zheng 				break;
9905d4f98a2SYan Zheng 			}
9915d4f98a2SYan Zheng 			prev = rb_next(prev);
9925d4f98a2SYan Zheng 		}
9935d4f98a2SYan Zheng 	}
9945d4f98a2SYan Zheng 	while (node) {
9955d4f98a2SYan Zheng 		entry = rb_entry(node, struct btrfs_inode, rb_node);
9965d4f98a2SYan Zheng 		inode = igrab(&entry->vfs_inode);
9975d4f98a2SYan Zheng 		if (inode) {
9985d4f98a2SYan Zheng 			spin_unlock(&root->inode_lock);
9995d4f98a2SYan Zheng 			return inode;
10005d4f98a2SYan Zheng 		}
10015d4f98a2SYan Zheng 
10024a0cc7caSNikolay Borisov 		objectid = btrfs_ino(entry) + 1;
10035d4f98a2SYan Zheng 		if (cond_resched_lock(&root->inode_lock))
10045d4f98a2SYan Zheng 			goto again;
10055d4f98a2SYan Zheng 
10065d4f98a2SYan Zheng 		node = rb_next(node);
10075d4f98a2SYan Zheng 	}
10085d4f98a2SYan Zheng 	spin_unlock(&root->inode_lock);
10095d4f98a2SYan Zheng 	return NULL;
10105d4f98a2SYan Zheng }
10115d4f98a2SYan Zheng 
10125d4f98a2SYan Zheng /*
10135d4f98a2SYan Zheng  * get new location of data
10145d4f98a2SYan Zheng  */
get_new_location(struct inode * reloc_inode,u64 * new_bytenr,u64 bytenr,u64 num_bytes)10155d4f98a2SYan Zheng static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr,
10165d4f98a2SYan Zheng 			    u64 bytenr, u64 num_bytes)
10175d4f98a2SYan Zheng {
10185d4f98a2SYan Zheng 	struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
10195d4f98a2SYan Zheng 	struct btrfs_path *path;
10205d4f98a2SYan Zheng 	struct btrfs_file_extent_item *fi;
10215d4f98a2SYan Zheng 	struct extent_buffer *leaf;
10225d4f98a2SYan Zheng 	int ret;
10235d4f98a2SYan Zheng 
10245d4f98a2SYan Zheng 	path = btrfs_alloc_path();
10255d4f98a2SYan Zheng 	if (!path)
10265d4f98a2SYan Zheng 		return -ENOMEM;
10275d4f98a2SYan Zheng 
10285d4f98a2SYan Zheng 	bytenr -= BTRFS_I(reloc_inode)->index_cnt;
1029f85b7379SDavid Sterba 	ret = btrfs_lookup_file_extent(NULL, root, path,
1030f85b7379SDavid Sterba 			btrfs_ino(BTRFS_I(reloc_inode)), bytenr, 0);
10315d4f98a2SYan Zheng 	if (ret < 0)
10325d4f98a2SYan Zheng 		goto out;
10335d4f98a2SYan Zheng 	if (ret > 0) {
10345d4f98a2SYan Zheng 		ret = -ENOENT;
10355d4f98a2SYan Zheng 		goto out;
10365d4f98a2SYan Zheng 	}
10375d4f98a2SYan Zheng 
10385d4f98a2SYan Zheng 	leaf = path->nodes[0];
10395d4f98a2SYan Zheng 	fi = btrfs_item_ptr(leaf, path->slots[0],
10405d4f98a2SYan Zheng 			    struct btrfs_file_extent_item);
10415d4f98a2SYan Zheng 
10425d4f98a2SYan Zheng 	BUG_ON(btrfs_file_extent_offset(leaf, fi) ||
10435d4f98a2SYan Zheng 	       btrfs_file_extent_compression(leaf, fi) ||
10445d4f98a2SYan Zheng 	       btrfs_file_extent_encryption(leaf, fi) ||
10455d4f98a2SYan Zheng 	       btrfs_file_extent_other_encoding(leaf, fi));
10465d4f98a2SYan Zheng 
10475d4f98a2SYan Zheng 	if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
104883d4cfd4SJosef Bacik 		ret = -EINVAL;
10495d4f98a2SYan Zheng 		goto out;
10505d4f98a2SYan Zheng 	}
10515d4f98a2SYan Zheng 
10525d4f98a2SYan Zheng 	*new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
10535d4f98a2SYan Zheng 	ret = 0;
10545d4f98a2SYan Zheng out:
10555d4f98a2SYan Zheng 	btrfs_free_path(path);
10565d4f98a2SYan Zheng 	return ret;
10575d4f98a2SYan Zheng }
10585d4f98a2SYan Zheng 
10595d4f98a2SYan Zheng /*
10605d4f98a2SYan Zheng  * update file extent items in the tree leaf to point to
10615d4f98a2SYan Zheng  * the new locations.
10625d4f98a2SYan Zheng  */
10633fd0a558SYan, Zheng static noinline_for_stack
replace_file_extents(struct btrfs_trans_handle * trans,struct reloc_control * rc,struct btrfs_root * root,struct extent_buffer * leaf)10643fd0a558SYan, Zheng int replace_file_extents(struct btrfs_trans_handle *trans,
10655d4f98a2SYan Zheng 			 struct reloc_control *rc,
10665d4f98a2SYan Zheng 			 struct btrfs_root *root,
10673fd0a558SYan, Zheng 			 struct extent_buffer *leaf)
10685d4f98a2SYan Zheng {
10690b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
10705d4f98a2SYan Zheng 	struct btrfs_key key;
10715d4f98a2SYan Zheng 	struct btrfs_file_extent_item *fi;
10725d4f98a2SYan Zheng 	struct inode *inode = NULL;
10735d4f98a2SYan Zheng 	u64 parent;
10745d4f98a2SYan Zheng 	u64 bytenr;
10753fd0a558SYan, Zheng 	u64 new_bytenr = 0;
10765d4f98a2SYan Zheng 	u64 num_bytes;
10775d4f98a2SYan Zheng 	u64 end;
10785d4f98a2SYan Zheng 	u32 nritems;
10795d4f98a2SYan Zheng 	u32 i;
108083d4cfd4SJosef Bacik 	int ret = 0;
10815d4f98a2SYan Zheng 	int first = 1;
10825d4f98a2SYan Zheng 	int dirty = 0;
10835d4f98a2SYan Zheng 
10845d4f98a2SYan Zheng 	if (rc->stage != UPDATE_DATA_PTRS)
10855d4f98a2SYan Zheng 		return 0;
10865d4f98a2SYan Zheng 
10875d4f98a2SYan Zheng 	/* reloc trees always use full backref */
10885d4f98a2SYan Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
10895d4f98a2SYan Zheng 		parent = leaf->start;
10905d4f98a2SYan Zheng 	else
10915d4f98a2SYan Zheng 		parent = 0;
10925d4f98a2SYan Zheng 
10935d4f98a2SYan Zheng 	nritems = btrfs_header_nritems(leaf);
10945d4f98a2SYan Zheng 	for (i = 0; i < nritems; i++) {
109582fa113fSQu Wenruo 		struct btrfs_ref ref = { 0 };
109682fa113fSQu Wenruo 
10975d4f98a2SYan Zheng 		cond_resched();
10985d4f98a2SYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, i);
10995d4f98a2SYan Zheng 		if (key.type != BTRFS_EXTENT_DATA_KEY)
11005d4f98a2SYan Zheng 			continue;
11015d4f98a2SYan Zheng 		fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
11025d4f98a2SYan Zheng 		if (btrfs_file_extent_type(leaf, fi) ==
11035d4f98a2SYan Zheng 		    BTRFS_FILE_EXTENT_INLINE)
11045d4f98a2SYan Zheng 			continue;
11055d4f98a2SYan Zheng 		bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
11065d4f98a2SYan Zheng 		num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
11075d4f98a2SYan Zheng 		if (bytenr == 0)
11085d4f98a2SYan Zheng 			continue;
11099569cc20SQu Wenruo 		if (!in_range(bytenr, rc->block_group->start,
11109569cc20SQu Wenruo 			      rc->block_group->length))
11115d4f98a2SYan Zheng 			continue;
11125d4f98a2SYan Zheng 
11135d4f98a2SYan Zheng 		/*
1114fb12489bSMatthew Wilcox (Oracle) 		 * if we are modifying block in fs tree, wait for read_folio
11155d4f98a2SYan Zheng 		 * to complete and drop the extent cache
11165d4f98a2SYan Zheng 		 */
11175d4f98a2SYan Zheng 		if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
11185d4f98a2SYan Zheng 			if (first) {
11195d4f98a2SYan Zheng 				inode = find_next_inode(root, key.objectid);
11205d4f98a2SYan Zheng 				first = 0;
11214a0cc7caSNikolay Borisov 			} else if (inode && btrfs_ino(BTRFS_I(inode)) < key.objectid) {
1122e55cf7caSDavid Sterba 				btrfs_add_delayed_iput(BTRFS_I(inode));
11235d4f98a2SYan Zheng 				inode = find_next_inode(root, key.objectid);
11245d4f98a2SYan Zheng 			}
11254a0cc7caSNikolay Borisov 			if (inode && btrfs_ino(BTRFS_I(inode)) == key.objectid) {
11269c5c9604SJosef Bacik 				struct extent_state *cached_state = NULL;
11279c5c9604SJosef Bacik 
11285d4f98a2SYan Zheng 				end = key.offset +
11295d4f98a2SYan Zheng 				      btrfs_file_extent_num_bytes(leaf, fi);
11305d4f98a2SYan Zheng 				WARN_ON(!IS_ALIGNED(key.offset,
11310b246afaSJeff Mahoney 						    fs_info->sectorsize));
11320b246afaSJeff Mahoney 				WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize));
11335d4f98a2SYan Zheng 				end--;
11345d4f98a2SYan Zheng 				ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
11359c5c9604SJosef Bacik 						      key.offset, end,
11369c5c9604SJosef Bacik 						      &cached_state);
11375d4f98a2SYan Zheng 				if (!ret)
11385d4f98a2SYan Zheng 					continue;
11395d4f98a2SYan Zheng 
11404c0c8cfcSFilipe Manana 				btrfs_drop_extent_map_range(BTRFS_I(inode),
11414c0c8cfcSFilipe Manana 							    key.offset, end, true);
11425d4f98a2SYan Zheng 				unlock_extent(&BTRFS_I(inode)->io_tree,
11439c5c9604SJosef Bacik 					      key.offset, end, &cached_state);
11445d4f98a2SYan Zheng 			}
11455d4f98a2SYan Zheng 		}
11465d4f98a2SYan Zheng 
11475d4f98a2SYan Zheng 		ret = get_new_location(rc->data_inode, &new_bytenr,
11485d4f98a2SYan Zheng 				       bytenr, num_bytes);
114983d4cfd4SJosef Bacik 		if (ret) {
115083d4cfd4SJosef Bacik 			/*
115183d4cfd4SJosef Bacik 			 * Don't have to abort since we've not changed anything
115283d4cfd4SJosef Bacik 			 * in the file extent yet.
115383d4cfd4SJosef Bacik 			 */
115483d4cfd4SJosef Bacik 			break;
11553fd0a558SYan, Zheng 		}
11565d4f98a2SYan Zheng 
11575d4f98a2SYan Zheng 		btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr);
11585d4f98a2SYan Zheng 		dirty = 1;
11595d4f98a2SYan Zheng 
11605d4f98a2SYan Zheng 		key.offset -= btrfs_file_extent_offset(leaf, fi);
116182fa113fSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
116282fa113fSQu Wenruo 				       num_bytes, parent);
116382fa113fSQu Wenruo 		btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
1164f42c5da6SNikolay Borisov 				    key.objectid, key.offset,
1165f42c5da6SNikolay Borisov 				    root->root_key.objectid, false);
116682fa113fSQu Wenruo 		ret = btrfs_inc_extent_ref(trans, &ref);
116783d4cfd4SJosef Bacik 		if (ret) {
116866642832SJeff Mahoney 			btrfs_abort_transaction(trans, ret);
116983d4cfd4SJosef Bacik 			break;
117083d4cfd4SJosef Bacik 		}
11715d4f98a2SYan Zheng 
1172ffd4bb2aSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
1173ffd4bb2aSQu Wenruo 				       num_bytes, parent);
1174ffd4bb2aSQu Wenruo 		btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
1175f42c5da6SNikolay Borisov 				    key.objectid, key.offset,
1176f42c5da6SNikolay Borisov 				    root->root_key.objectid, false);
1177ffd4bb2aSQu Wenruo 		ret = btrfs_free_extent(trans, &ref);
117883d4cfd4SJosef Bacik 		if (ret) {
117966642832SJeff Mahoney 			btrfs_abort_transaction(trans, ret);
118083d4cfd4SJosef Bacik 			break;
118183d4cfd4SJosef Bacik 		}
11825d4f98a2SYan Zheng 	}
11835d4f98a2SYan Zheng 	if (dirty)
1184*d5e09e38SFilipe Manana 		btrfs_mark_buffer_dirty(trans, leaf);
11853fd0a558SYan, Zheng 	if (inode)
1186e55cf7caSDavid Sterba 		btrfs_add_delayed_iput(BTRFS_I(inode));
118783d4cfd4SJosef Bacik 	return ret;
11885d4f98a2SYan Zheng }
11895d4f98a2SYan Zheng 
11905d4f98a2SYan Zheng static noinline_for_stack
memcmp_node_keys(struct extent_buffer * eb,int slot,struct btrfs_path * path,int level)11915d4f98a2SYan Zheng int memcmp_node_keys(struct extent_buffer *eb, int slot,
11925d4f98a2SYan Zheng 		     struct btrfs_path *path, int level)
11935d4f98a2SYan Zheng {
11945d4f98a2SYan Zheng 	struct btrfs_disk_key key1;
11955d4f98a2SYan Zheng 	struct btrfs_disk_key key2;
11965d4f98a2SYan Zheng 	btrfs_node_key(eb, &key1, slot);
11975d4f98a2SYan Zheng 	btrfs_node_key(path->nodes[level], &key2, path->slots[level]);
11985d4f98a2SYan Zheng 	return memcmp(&key1, &key2, sizeof(key1));
11995d4f98a2SYan Zheng }
12005d4f98a2SYan Zheng 
12015d4f98a2SYan Zheng /*
12025d4f98a2SYan Zheng  * try to replace tree blocks in fs tree with the new blocks
12035d4f98a2SYan Zheng  * in reloc tree. tree blocks haven't been modified since the
12045d4f98a2SYan Zheng  * reloc tree was create can be replaced.
12055d4f98a2SYan Zheng  *
12065d4f98a2SYan Zheng  * if a block was replaced, level of the block + 1 is returned.
12075d4f98a2SYan Zheng  * if no block got replaced, 0 is returned. if there are other
12085d4f98a2SYan Zheng  * errors, a negative error number is returned.
12095d4f98a2SYan Zheng  */
12103fd0a558SYan, Zheng static noinline_for_stack
replace_path(struct btrfs_trans_handle * trans,struct reloc_control * rc,struct btrfs_root * dest,struct btrfs_root * src,struct btrfs_path * path,struct btrfs_key * next_key,int lowest_level,int max_level)12113d0174f7SQu Wenruo int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc,
12125d4f98a2SYan Zheng 		 struct btrfs_root *dest, struct btrfs_root *src,
12135d4f98a2SYan Zheng 		 struct btrfs_path *path, struct btrfs_key *next_key,
12145d4f98a2SYan Zheng 		 int lowest_level, int max_level)
12155d4f98a2SYan Zheng {
12160b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = dest->fs_info;
12175d4f98a2SYan Zheng 	struct extent_buffer *eb;
12185d4f98a2SYan Zheng 	struct extent_buffer *parent;
121982fa113fSQu Wenruo 	struct btrfs_ref ref = { 0 };
12205d4f98a2SYan Zheng 	struct btrfs_key key;
12215d4f98a2SYan Zheng 	u64 old_bytenr;
12225d4f98a2SYan Zheng 	u64 new_bytenr;
12235d4f98a2SYan Zheng 	u64 old_ptr_gen;
12245d4f98a2SYan Zheng 	u64 new_ptr_gen;
12255d4f98a2SYan Zheng 	u64 last_snapshot;
12265d4f98a2SYan Zheng 	u32 blocksize;
12273fd0a558SYan, Zheng 	int cow = 0;
12285d4f98a2SYan Zheng 	int level;
12295d4f98a2SYan Zheng 	int ret;
12305d4f98a2SYan Zheng 	int slot;
12315d4f98a2SYan Zheng 
12327a9213a9SJosef Bacik 	ASSERT(src->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID);
12337a9213a9SJosef Bacik 	ASSERT(dest->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
12345d4f98a2SYan Zheng 
12355d4f98a2SYan Zheng 	last_snapshot = btrfs_root_last_snapshot(&src->root_item);
12363fd0a558SYan, Zheng again:
12375d4f98a2SYan Zheng 	slot = path->slots[lowest_level];
12385d4f98a2SYan Zheng 	btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot);
12395d4f98a2SYan Zheng 
12405d4f98a2SYan Zheng 	eb = btrfs_lock_root_node(dest);
12415d4f98a2SYan Zheng 	level = btrfs_header_level(eb);
12425d4f98a2SYan Zheng 
12435d4f98a2SYan Zheng 	if (level < lowest_level) {
12445d4f98a2SYan Zheng 		btrfs_tree_unlock(eb);
12455d4f98a2SYan Zheng 		free_extent_buffer(eb);
12465d4f98a2SYan Zheng 		return 0;
12475d4f98a2SYan Zheng 	}
12485d4f98a2SYan Zheng 
12493fd0a558SYan, Zheng 	if (cow) {
12509631e4ccSJosef Bacik 		ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb,
12519631e4ccSJosef Bacik 				      BTRFS_NESTING_COW);
125245b87c5dSJosef Bacik 		if (ret) {
125345b87c5dSJosef Bacik 			btrfs_tree_unlock(eb);
125445b87c5dSJosef Bacik 			free_extent_buffer(eb);
125545b87c5dSJosef Bacik 			return ret;
125645b87c5dSJosef Bacik 		}
12573fd0a558SYan, Zheng 	}
12585d4f98a2SYan Zheng 
12595d4f98a2SYan Zheng 	if (next_key) {
12605d4f98a2SYan Zheng 		next_key->objectid = (u64)-1;
12615d4f98a2SYan Zheng 		next_key->type = (u8)-1;
12625d4f98a2SYan Zheng 		next_key->offset = (u64)-1;
12635d4f98a2SYan Zheng 	}
12645d4f98a2SYan Zheng 
12655d4f98a2SYan Zheng 	parent = eb;
12665d4f98a2SYan Zheng 	while (1) {
12675d4f98a2SYan Zheng 		level = btrfs_header_level(parent);
12687a9213a9SJosef Bacik 		ASSERT(level >= lowest_level);
12695d4f98a2SYan Zheng 
1270fdf8d595SAnand Jain 		ret = btrfs_bin_search(parent, 0, &key, &slot);
1271cbca7d59SFilipe Manana 		if (ret < 0)
1272cbca7d59SFilipe Manana 			break;
12735d4f98a2SYan Zheng 		if (ret && slot > 0)
12745d4f98a2SYan Zheng 			slot--;
12755d4f98a2SYan Zheng 
12765d4f98a2SYan Zheng 		if (next_key && slot + 1 < btrfs_header_nritems(parent))
12775d4f98a2SYan Zheng 			btrfs_node_key_to_cpu(parent, next_key, slot + 1);
12785d4f98a2SYan Zheng 
12795d4f98a2SYan Zheng 		old_bytenr = btrfs_node_blockptr(parent, slot);
12800b246afaSJeff Mahoney 		blocksize = fs_info->nodesize;
12815d4f98a2SYan Zheng 		old_ptr_gen = btrfs_node_ptr_generation(parent, slot);
12825d4f98a2SYan Zheng 
12835d4f98a2SYan Zheng 		if (level <= max_level) {
12845d4f98a2SYan Zheng 			eb = path->nodes[level];
12855d4f98a2SYan Zheng 			new_bytenr = btrfs_node_blockptr(eb,
12865d4f98a2SYan Zheng 							path->slots[level]);
12875d4f98a2SYan Zheng 			new_ptr_gen = btrfs_node_ptr_generation(eb,
12885d4f98a2SYan Zheng 							path->slots[level]);
12895d4f98a2SYan Zheng 		} else {
12905d4f98a2SYan Zheng 			new_bytenr = 0;
12915d4f98a2SYan Zheng 			new_ptr_gen = 0;
12925d4f98a2SYan Zheng 		}
12935d4f98a2SYan Zheng 
1294fae7f21cSDulshani Gunawardhana 		if (WARN_ON(new_bytenr > 0 && new_bytenr == old_bytenr)) {
12955d4f98a2SYan Zheng 			ret = level;
12965d4f98a2SYan Zheng 			break;
12975d4f98a2SYan Zheng 		}
12985d4f98a2SYan Zheng 
12995d4f98a2SYan Zheng 		if (new_bytenr == 0 || old_ptr_gen > last_snapshot ||
13005d4f98a2SYan Zheng 		    memcmp_node_keys(parent, slot, path, level)) {
13013fd0a558SYan, Zheng 			if (level <= lowest_level) {
13025d4f98a2SYan Zheng 				ret = 0;
13035d4f98a2SYan Zheng 				break;
13045d4f98a2SYan Zheng 			}
13055d4f98a2SYan Zheng 
13066b3426beSJosef Bacik 			eb = btrfs_read_node_slot(parent, slot);
130764c043deSLiu Bo 			if (IS_ERR(eb)) {
130864c043deSLiu Bo 				ret = PTR_ERR(eb);
1309264813acSLiu Bo 				break;
1310416bc658SJosef Bacik 			}
13115d4f98a2SYan Zheng 			btrfs_tree_lock(eb);
13123fd0a558SYan, Zheng 			if (cow) {
13135d4f98a2SYan Zheng 				ret = btrfs_cow_block(trans, dest, eb, parent,
13149631e4ccSJosef Bacik 						      slot, &eb,
13159631e4ccSJosef Bacik 						      BTRFS_NESTING_COW);
131645b87c5dSJosef Bacik 				if (ret) {
131745b87c5dSJosef Bacik 					btrfs_tree_unlock(eb);
131845b87c5dSJosef Bacik 					free_extent_buffer(eb);
131945b87c5dSJosef Bacik 					break;
132045b87c5dSJosef Bacik 				}
13215d4f98a2SYan Zheng 			}
13225d4f98a2SYan Zheng 
13235d4f98a2SYan Zheng 			btrfs_tree_unlock(parent);
13245d4f98a2SYan Zheng 			free_extent_buffer(parent);
13255d4f98a2SYan Zheng 
13265d4f98a2SYan Zheng 			parent = eb;
13275d4f98a2SYan Zheng 			continue;
13285d4f98a2SYan Zheng 		}
13295d4f98a2SYan Zheng 
13303fd0a558SYan, Zheng 		if (!cow) {
13313fd0a558SYan, Zheng 			btrfs_tree_unlock(parent);
13323fd0a558SYan, Zheng 			free_extent_buffer(parent);
13333fd0a558SYan, Zheng 			cow = 1;
13343fd0a558SYan, Zheng 			goto again;
13353fd0a558SYan, Zheng 		}
13363fd0a558SYan, Zheng 
13375d4f98a2SYan Zheng 		btrfs_node_key_to_cpu(path->nodes[level], &key,
13385d4f98a2SYan Zheng 				      path->slots[level]);
1339b3b4aa74SDavid Sterba 		btrfs_release_path(path);
13405d4f98a2SYan Zheng 
13415d4f98a2SYan Zheng 		path->lowest_level = level;
1342b40130b2SJosef Bacik 		set_bit(BTRFS_ROOT_RESET_LOCKDEP_CLASS, &src->state);
13435d4f98a2SYan Zheng 		ret = btrfs_search_slot(trans, src, &key, path, 0, 1);
1344b40130b2SJosef Bacik 		clear_bit(BTRFS_ROOT_RESET_LOCKDEP_CLASS, &src->state);
13455d4f98a2SYan Zheng 		path->lowest_level = 0;
13460e9873e2SJosef Bacik 		if (ret) {
13470e9873e2SJosef Bacik 			if (ret > 0)
13480e9873e2SJosef Bacik 				ret = -ENOENT;
13490e9873e2SJosef Bacik 			break;
13500e9873e2SJosef Bacik 		}
13515d4f98a2SYan Zheng 
13525d4f98a2SYan Zheng 		/*
1353824d8dffSQu Wenruo 		 * Info qgroup to trace both subtrees.
1354824d8dffSQu Wenruo 		 *
1355824d8dffSQu Wenruo 		 * We must trace both trees.
1356824d8dffSQu Wenruo 		 * 1) Tree reloc subtree
1357824d8dffSQu Wenruo 		 *    If not traced, we will leak data numbers
1358824d8dffSQu Wenruo 		 * 2) Fs subtree
1359824d8dffSQu Wenruo 		 *    If not traced, we will double count old data
1360f616f5cdSQu Wenruo 		 *
1361f616f5cdSQu Wenruo 		 * We don't scan the subtree right now, but only record
1362f616f5cdSQu Wenruo 		 * the swapped tree blocks.
1363f616f5cdSQu Wenruo 		 * The real subtree rescan is delayed until we have new
1364f616f5cdSQu Wenruo 		 * CoW on the subtree root node before transaction commit.
1365824d8dffSQu Wenruo 		 */
1366370a11b8SQu Wenruo 		ret = btrfs_qgroup_add_swapped_blocks(trans, dest,
1367370a11b8SQu Wenruo 				rc->block_group, parent, slot,
1368370a11b8SQu Wenruo 				path->nodes[level], path->slots[level],
1369370a11b8SQu Wenruo 				last_snapshot);
1370370a11b8SQu Wenruo 		if (ret < 0)
1371370a11b8SQu Wenruo 			break;
1372824d8dffSQu Wenruo 		/*
13735d4f98a2SYan Zheng 		 * swap blocks in fs tree and reloc tree.
13745d4f98a2SYan Zheng 		 */
13755d4f98a2SYan Zheng 		btrfs_set_node_blockptr(parent, slot, new_bytenr);
13765d4f98a2SYan Zheng 		btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen);
1377*d5e09e38SFilipe Manana 		btrfs_mark_buffer_dirty(trans, parent);
13785d4f98a2SYan Zheng 
13795d4f98a2SYan Zheng 		btrfs_set_node_blockptr(path->nodes[level],
13805d4f98a2SYan Zheng 					path->slots[level], old_bytenr);
13815d4f98a2SYan Zheng 		btrfs_set_node_ptr_generation(path->nodes[level],
13825d4f98a2SYan Zheng 					      path->slots[level], old_ptr_gen);
1383*d5e09e38SFilipe Manana 		btrfs_mark_buffer_dirty(trans, path->nodes[level]);
13845d4f98a2SYan Zheng 
138582fa113fSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, old_bytenr,
138682fa113fSQu Wenruo 				       blocksize, path->nodes[level]->start);
1387f42c5da6SNikolay Borisov 		btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid,
1388f42c5da6SNikolay Borisov 				    0, true);
138982fa113fSQu Wenruo 		ret = btrfs_inc_extent_ref(trans, &ref);
1390253e258cSJosef Bacik 		if (ret) {
1391253e258cSJosef Bacik 			btrfs_abort_transaction(trans, ret);
1392253e258cSJosef Bacik 			break;
1393253e258cSJosef Bacik 		}
139482fa113fSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
139582fa113fSQu Wenruo 				       blocksize, 0);
1396f42c5da6SNikolay Borisov 		btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid, 0,
1397f42c5da6SNikolay Borisov 				    true);
139882fa113fSQu Wenruo 		ret = btrfs_inc_extent_ref(trans, &ref);
1399253e258cSJosef Bacik 		if (ret) {
1400253e258cSJosef Bacik 			btrfs_abort_transaction(trans, ret);
1401253e258cSJosef Bacik 			break;
1402253e258cSJosef Bacik 		}
14035d4f98a2SYan Zheng 
1404ffd4bb2aSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, new_bytenr,
1405ffd4bb2aSQu Wenruo 				       blocksize, path->nodes[level]->start);
1406f42c5da6SNikolay Borisov 		btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid,
1407f42c5da6SNikolay Borisov 				    0, true);
1408ffd4bb2aSQu Wenruo 		ret = btrfs_free_extent(trans, &ref);
1409253e258cSJosef Bacik 		if (ret) {
1410253e258cSJosef Bacik 			btrfs_abort_transaction(trans, ret);
1411253e258cSJosef Bacik 			break;
1412253e258cSJosef Bacik 		}
14135d4f98a2SYan Zheng 
1414ffd4bb2aSQu Wenruo 		btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, old_bytenr,
1415ffd4bb2aSQu Wenruo 				       blocksize, 0);
1416f42c5da6SNikolay Borisov 		btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid,
1417f42c5da6SNikolay Borisov 				    0, true);
1418ffd4bb2aSQu Wenruo 		ret = btrfs_free_extent(trans, &ref);
1419253e258cSJosef Bacik 		if (ret) {
1420253e258cSJosef Bacik 			btrfs_abort_transaction(trans, ret);
1421253e258cSJosef Bacik 			break;
1422253e258cSJosef Bacik 		}
14235d4f98a2SYan Zheng 
14245d4f98a2SYan Zheng 		btrfs_unlock_up_safe(path, 0);
14255d4f98a2SYan Zheng 
14265d4f98a2SYan Zheng 		ret = level;
14275d4f98a2SYan Zheng 		break;
14285d4f98a2SYan Zheng 	}
14295d4f98a2SYan Zheng 	btrfs_tree_unlock(parent);
14305d4f98a2SYan Zheng 	free_extent_buffer(parent);
14315d4f98a2SYan Zheng 	return ret;
14325d4f98a2SYan Zheng }
14335d4f98a2SYan Zheng 
14345d4f98a2SYan Zheng /*
14355d4f98a2SYan Zheng  * helper to find next relocated block in reloc tree
14365d4f98a2SYan Zheng  */
14375d4f98a2SYan Zheng static noinline_for_stack
walk_up_reloc_tree(struct btrfs_root * root,struct btrfs_path * path,int * level)14385d4f98a2SYan Zheng int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
14395d4f98a2SYan Zheng 		       int *level)
14405d4f98a2SYan Zheng {
14415d4f98a2SYan Zheng 	struct extent_buffer *eb;
14425d4f98a2SYan Zheng 	int i;
14435d4f98a2SYan Zheng 	u64 last_snapshot;
14445d4f98a2SYan Zheng 	u32 nritems;
14455d4f98a2SYan Zheng 
14465d4f98a2SYan Zheng 	last_snapshot = btrfs_root_last_snapshot(&root->root_item);
14475d4f98a2SYan Zheng 
14485d4f98a2SYan Zheng 	for (i = 0; i < *level; i++) {
14495d4f98a2SYan Zheng 		free_extent_buffer(path->nodes[i]);
14505d4f98a2SYan Zheng 		path->nodes[i] = NULL;
14515d4f98a2SYan Zheng 	}
14525d4f98a2SYan Zheng 
14535d4f98a2SYan Zheng 	for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
14545d4f98a2SYan Zheng 		eb = path->nodes[i];
14555d4f98a2SYan Zheng 		nritems = btrfs_header_nritems(eb);
14565d4f98a2SYan Zheng 		while (path->slots[i] + 1 < nritems) {
14575d4f98a2SYan Zheng 			path->slots[i]++;
14585d4f98a2SYan Zheng 			if (btrfs_node_ptr_generation(eb, path->slots[i]) <=
14595d4f98a2SYan Zheng 			    last_snapshot)
14605d4f98a2SYan Zheng 				continue;
14615d4f98a2SYan Zheng 
14625d4f98a2SYan Zheng 			*level = i;
14635d4f98a2SYan Zheng 			return 0;
14645d4f98a2SYan Zheng 		}
14655d4f98a2SYan Zheng 		free_extent_buffer(path->nodes[i]);
14665d4f98a2SYan Zheng 		path->nodes[i] = NULL;
14675d4f98a2SYan Zheng 	}
14685d4f98a2SYan Zheng 	return 1;
14695d4f98a2SYan Zheng }
14705d4f98a2SYan Zheng 
14715d4f98a2SYan Zheng /*
14725d4f98a2SYan Zheng  * walk down reloc tree to find relocated block of lowest level
14735d4f98a2SYan Zheng  */
14745d4f98a2SYan Zheng static noinline_for_stack
walk_down_reloc_tree(struct btrfs_root * root,struct btrfs_path * path,int * level)14755d4f98a2SYan Zheng int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
14765d4f98a2SYan Zheng 			 int *level)
14775d4f98a2SYan Zheng {
14785d4f98a2SYan Zheng 	struct extent_buffer *eb = NULL;
14795d4f98a2SYan Zheng 	int i;
14805d4f98a2SYan Zheng 	u64 ptr_gen = 0;
14815d4f98a2SYan Zheng 	u64 last_snapshot;
14825d4f98a2SYan Zheng 	u32 nritems;
14835d4f98a2SYan Zheng 
14845d4f98a2SYan Zheng 	last_snapshot = btrfs_root_last_snapshot(&root->root_item);
14855d4f98a2SYan Zheng 
14865d4f98a2SYan Zheng 	for (i = *level; i > 0; i--) {
14875d4f98a2SYan Zheng 		eb = path->nodes[i];
14885d4f98a2SYan Zheng 		nritems = btrfs_header_nritems(eb);
14895d4f98a2SYan Zheng 		while (path->slots[i] < nritems) {
14905d4f98a2SYan Zheng 			ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]);
14915d4f98a2SYan Zheng 			if (ptr_gen > last_snapshot)
14925d4f98a2SYan Zheng 				break;
14935d4f98a2SYan Zheng 			path->slots[i]++;
14945d4f98a2SYan Zheng 		}
14955d4f98a2SYan Zheng 		if (path->slots[i] >= nritems) {
14965d4f98a2SYan Zheng 			if (i == *level)
14975d4f98a2SYan Zheng 				break;
14985d4f98a2SYan Zheng 			*level = i + 1;
14995d4f98a2SYan Zheng 			return 0;
15005d4f98a2SYan Zheng 		}
15015d4f98a2SYan Zheng 		if (i == 1) {
15025d4f98a2SYan Zheng 			*level = i;
15035d4f98a2SYan Zheng 			return 0;
15045d4f98a2SYan Zheng 		}
15055d4f98a2SYan Zheng 
15068ef385bbSJosef Bacik 		eb = btrfs_read_node_slot(eb, path->slots[i]);
15078ef385bbSJosef Bacik 		if (IS_ERR(eb))
150864c043deSLiu Bo 			return PTR_ERR(eb);
15095d4f98a2SYan Zheng 		BUG_ON(btrfs_header_level(eb) != i - 1);
15105d4f98a2SYan Zheng 		path->nodes[i - 1] = eb;
15115d4f98a2SYan Zheng 		path->slots[i - 1] = 0;
15125d4f98a2SYan Zheng 	}
15135d4f98a2SYan Zheng 	return 1;
15145d4f98a2SYan Zheng }
15155d4f98a2SYan Zheng 
15165d4f98a2SYan Zheng /*
15175d4f98a2SYan Zheng  * invalidate extent cache for file extents whose key in range of
15185d4f98a2SYan Zheng  * [min_key, max_key)
15195d4f98a2SYan Zheng  */
invalidate_extent_cache(struct btrfs_root * root,struct btrfs_key * min_key,struct btrfs_key * max_key)15205d4f98a2SYan Zheng static int invalidate_extent_cache(struct btrfs_root *root,
15215d4f98a2SYan Zheng 				   struct btrfs_key *min_key,
15225d4f98a2SYan Zheng 				   struct btrfs_key *max_key)
15235d4f98a2SYan Zheng {
15240b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
15255d4f98a2SYan Zheng 	struct inode *inode = NULL;
15265d4f98a2SYan Zheng 	u64 objectid;
15275d4f98a2SYan Zheng 	u64 start, end;
152833345d01SLi Zefan 	u64 ino;
15295d4f98a2SYan Zheng 
15305d4f98a2SYan Zheng 	objectid = min_key->objectid;
15315d4f98a2SYan Zheng 	while (1) {
15329c5c9604SJosef Bacik 		struct extent_state *cached_state = NULL;
15339c5c9604SJosef Bacik 
15345d4f98a2SYan Zheng 		cond_resched();
15355d4f98a2SYan Zheng 		iput(inode);
15365d4f98a2SYan Zheng 
15375d4f98a2SYan Zheng 		if (objectid > max_key->objectid)
15385d4f98a2SYan Zheng 			break;
15395d4f98a2SYan Zheng 
15405d4f98a2SYan Zheng 		inode = find_next_inode(root, objectid);
15415d4f98a2SYan Zheng 		if (!inode)
15425d4f98a2SYan Zheng 			break;
15434a0cc7caSNikolay Borisov 		ino = btrfs_ino(BTRFS_I(inode));
15445d4f98a2SYan Zheng 
154533345d01SLi Zefan 		if (ino > max_key->objectid) {
15465d4f98a2SYan Zheng 			iput(inode);
15475d4f98a2SYan Zheng 			break;
15485d4f98a2SYan Zheng 		}
15495d4f98a2SYan Zheng 
155033345d01SLi Zefan 		objectid = ino + 1;
15515d4f98a2SYan Zheng 		if (!S_ISREG(inode->i_mode))
15525d4f98a2SYan Zheng 			continue;
15535d4f98a2SYan Zheng 
155433345d01SLi Zefan 		if (unlikely(min_key->objectid == ino)) {
15555d4f98a2SYan Zheng 			if (min_key->type > BTRFS_EXTENT_DATA_KEY)
15565d4f98a2SYan Zheng 				continue;
15575d4f98a2SYan Zheng 			if (min_key->type < BTRFS_EXTENT_DATA_KEY)
15585d4f98a2SYan Zheng 				start = 0;
15595d4f98a2SYan Zheng 			else {
15605d4f98a2SYan Zheng 				start = min_key->offset;
15610b246afaSJeff Mahoney 				WARN_ON(!IS_ALIGNED(start, fs_info->sectorsize));
15625d4f98a2SYan Zheng 			}
15635d4f98a2SYan Zheng 		} else {
15645d4f98a2SYan Zheng 			start = 0;
15655d4f98a2SYan Zheng 		}
15665d4f98a2SYan Zheng 
156733345d01SLi Zefan 		if (unlikely(max_key->objectid == ino)) {
15685d4f98a2SYan Zheng 			if (max_key->type < BTRFS_EXTENT_DATA_KEY)
15695d4f98a2SYan Zheng 				continue;
15705d4f98a2SYan Zheng 			if (max_key->type > BTRFS_EXTENT_DATA_KEY) {
15715d4f98a2SYan Zheng 				end = (u64)-1;
15725d4f98a2SYan Zheng 			} else {
15735d4f98a2SYan Zheng 				if (max_key->offset == 0)
15745d4f98a2SYan Zheng 					continue;
15755d4f98a2SYan Zheng 				end = max_key->offset;
15760b246afaSJeff Mahoney 				WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize));
15775d4f98a2SYan Zheng 				end--;
15785d4f98a2SYan Zheng 			}
15795d4f98a2SYan Zheng 		} else {
15805d4f98a2SYan Zheng 			end = (u64)-1;
15815d4f98a2SYan Zheng 		}
15825d4f98a2SYan Zheng 
1583fb12489bSMatthew Wilcox (Oracle) 		/* the lock_extent waits for read_folio to complete */
15849c5c9604SJosef Bacik 		lock_extent(&BTRFS_I(inode)->io_tree, start, end, &cached_state);
15854c0c8cfcSFilipe Manana 		btrfs_drop_extent_map_range(BTRFS_I(inode), start, end, true);
15869c5c9604SJosef Bacik 		unlock_extent(&BTRFS_I(inode)->io_tree, start, end, &cached_state);
15875d4f98a2SYan Zheng 	}
15885d4f98a2SYan Zheng 	return 0;
15895d4f98a2SYan Zheng }
15905d4f98a2SYan Zheng 
find_next_key(struct btrfs_path * path,int level,struct btrfs_key * key)15915d4f98a2SYan Zheng static int find_next_key(struct btrfs_path *path, int level,
15925d4f98a2SYan Zheng 			 struct btrfs_key *key)
15935d4f98a2SYan Zheng 
15945d4f98a2SYan Zheng {
15955d4f98a2SYan Zheng 	while (level < BTRFS_MAX_LEVEL) {
15965d4f98a2SYan Zheng 		if (!path->nodes[level])
15975d4f98a2SYan Zheng 			break;
15985d4f98a2SYan Zheng 		if (path->slots[level] + 1 <
15995d4f98a2SYan Zheng 		    btrfs_header_nritems(path->nodes[level])) {
16005d4f98a2SYan Zheng 			btrfs_node_key_to_cpu(path->nodes[level], key,
16015d4f98a2SYan Zheng 					      path->slots[level] + 1);
16025d4f98a2SYan Zheng 			return 0;
16035d4f98a2SYan Zheng 		}
16045d4f98a2SYan Zheng 		level++;
16055d4f98a2SYan Zheng 	}
16065d4f98a2SYan Zheng 	return 1;
16075d4f98a2SYan Zheng }
16085d4f98a2SYan Zheng 
16095d4f98a2SYan Zheng /*
1610d2311e69SQu Wenruo  * Insert current subvolume into reloc_control::dirty_subvol_roots
1611d2311e69SQu Wenruo  */
insert_dirty_subvol(struct btrfs_trans_handle * trans,struct reloc_control * rc,struct btrfs_root * root)1612ac54da6cSJosef Bacik static int insert_dirty_subvol(struct btrfs_trans_handle *trans,
1613d2311e69SQu Wenruo 			       struct reloc_control *rc,
1614d2311e69SQu Wenruo 			       struct btrfs_root *root)
1615d2311e69SQu Wenruo {
1616d2311e69SQu Wenruo 	struct btrfs_root *reloc_root = root->reloc_root;
1617d2311e69SQu Wenruo 	struct btrfs_root_item *reloc_root_item;
16187934133fSJosef Bacik 	int ret;
1619d2311e69SQu Wenruo 
1620d2311e69SQu Wenruo 	/* @root must be a subvolume tree root with a valid reloc tree */
1621d2311e69SQu Wenruo 	ASSERT(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
1622d2311e69SQu Wenruo 	ASSERT(reloc_root);
1623d2311e69SQu Wenruo 
1624d2311e69SQu Wenruo 	reloc_root_item = &reloc_root->root_item;
1625d2311e69SQu Wenruo 	memset(&reloc_root_item->drop_progress, 0,
1626d2311e69SQu Wenruo 		sizeof(reloc_root_item->drop_progress));
1627c8422684SDavid Sterba 	btrfs_set_root_drop_level(reloc_root_item, 0);
1628d2311e69SQu Wenruo 	btrfs_set_root_refs(reloc_root_item, 0);
16297934133fSJosef Bacik 	ret = btrfs_update_reloc_root(trans, root);
16307934133fSJosef Bacik 	if (ret)
16317934133fSJosef Bacik 		return ret;
1632d2311e69SQu Wenruo 
1633d2311e69SQu Wenruo 	if (list_empty(&root->reloc_dirty_list)) {
163400246528SJosef Bacik 		btrfs_grab_root(root);
1635d2311e69SQu Wenruo 		list_add_tail(&root->reloc_dirty_list, &rc->dirty_subvol_roots);
1636d2311e69SQu Wenruo 	}
1637ac54da6cSJosef Bacik 
1638ac54da6cSJosef Bacik 	return 0;
1639d2311e69SQu Wenruo }
1640d2311e69SQu Wenruo 
clean_dirty_subvols(struct reloc_control * rc)1641d2311e69SQu Wenruo static int clean_dirty_subvols(struct reloc_control *rc)
1642d2311e69SQu Wenruo {
1643d2311e69SQu Wenruo 	struct btrfs_root *root;
1644d2311e69SQu Wenruo 	struct btrfs_root *next;
1645d2311e69SQu Wenruo 	int ret = 0;
164630d40577SQu Wenruo 	int ret2;
1647d2311e69SQu Wenruo 
1648d2311e69SQu Wenruo 	list_for_each_entry_safe(root, next, &rc->dirty_subvol_roots,
1649d2311e69SQu Wenruo 				 reloc_dirty_list) {
165030d40577SQu Wenruo 		if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
165130d40577SQu Wenruo 			/* Merged subvolume, cleanup its reloc root */
1652d2311e69SQu Wenruo 			struct btrfs_root *reloc_root = root->reloc_root;
1653d2311e69SQu Wenruo 
1654d2311e69SQu Wenruo 			list_del_init(&root->reloc_dirty_list);
1655d2311e69SQu Wenruo 			root->reloc_root = NULL;
16566282675eSQu Wenruo 			/*
16576282675eSQu Wenruo 			 * Need barrier to ensure clear_bit() only happens after
16586282675eSQu Wenruo 			 * root->reloc_root = NULL. Pairs with have_reloc_root.
16596282675eSQu Wenruo 			 */
16606282675eSQu Wenruo 			smp_wmb();
16611fac4a54SQu Wenruo 			clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
1662f28de8d8SJosef Bacik 			if (reloc_root) {
1663f44deb74SJosef Bacik 				/*
1664f44deb74SJosef Bacik 				 * btrfs_drop_snapshot drops our ref we hold for
1665f44deb74SJosef Bacik 				 * ->reloc_root.  If it fails however we must
1666f44deb74SJosef Bacik 				 * drop the ref ourselves.
1667f44deb74SJosef Bacik 				 */
1668f28de8d8SJosef Bacik 				ret2 = btrfs_drop_snapshot(reloc_root, 0, 1);
1669f44deb74SJosef Bacik 				if (ret2 < 0) {
1670f44deb74SJosef Bacik 					btrfs_put_root(reloc_root);
1671f44deb74SJosef Bacik 					if (!ret)
1672f28de8d8SJosef Bacik 						ret = ret2;
1673f28de8d8SJosef Bacik 				}
1674f44deb74SJosef Bacik 			}
167500246528SJosef Bacik 			btrfs_put_root(root);
167630d40577SQu Wenruo 		} else {
167730d40577SQu Wenruo 			/* Orphan reloc tree, just clean it up */
16780078a9f9SNikolay Borisov 			ret2 = btrfs_drop_snapshot(root, 0, 1);
1679f44deb74SJosef Bacik 			if (ret2 < 0) {
1680f44deb74SJosef Bacik 				btrfs_put_root(root);
1681f44deb74SJosef Bacik 				if (!ret)
168230d40577SQu Wenruo 					ret = ret2;
168330d40577SQu Wenruo 			}
1684d2311e69SQu Wenruo 		}
1685f44deb74SJosef Bacik 	}
1686d2311e69SQu Wenruo 	return ret;
1687d2311e69SQu Wenruo }
1688d2311e69SQu Wenruo 
1689d2311e69SQu Wenruo /*
16905d4f98a2SYan Zheng  * merge the relocated tree blocks in reloc tree with corresponding
16915d4f98a2SYan Zheng  * fs tree.
16925d4f98a2SYan Zheng  */
merge_reloc_root(struct reloc_control * rc,struct btrfs_root * root)16935d4f98a2SYan Zheng static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
16945d4f98a2SYan Zheng 					       struct btrfs_root *root)
16955d4f98a2SYan Zheng {
16960b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
16975d4f98a2SYan Zheng 	struct btrfs_key key;
16985d4f98a2SYan Zheng 	struct btrfs_key next_key;
16999e6a0c52SJosef Bacik 	struct btrfs_trans_handle *trans = NULL;
17005d4f98a2SYan Zheng 	struct btrfs_root *reloc_root;
17015d4f98a2SYan Zheng 	struct btrfs_root_item *root_item;
17025d4f98a2SYan Zheng 	struct btrfs_path *path;
17033fd0a558SYan, Zheng 	struct extent_buffer *leaf;
1704fca3a45dSJosef Bacik 	int reserve_level;
17055d4f98a2SYan Zheng 	int level;
17065d4f98a2SYan Zheng 	int max_level;
17075d4f98a2SYan Zheng 	int replaced = 0;
1708c6a592f2SNikolay Borisov 	int ret = 0;
17093fd0a558SYan, Zheng 	u32 min_reserved;
17105d4f98a2SYan Zheng 
17115d4f98a2SYan Zheng 	path = btrfs_alloc_path();
17125d4f98a2SYan Zheng 	if (!path)
17135d4f98a2SYan Zheng 		return -ENOMEM;
1714e4058b54SDavid Sterba 	path->reada = READA_FORWARD;
17155d4f98a2SYan Zheng 
17165d4f98a2SYan Zheng 	reloc_root = root->reloc_root;
17175d4f98a2SYan Zheng 	root_item = &reloc_root->root_item;
17185d4f98a2SYan Zheng 
17195d4f98a2SYan Zheng 	if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
17205d4f98a2SYan Zheng 		level = btrfs_root_level(root_item);
172167439dadSDavid Sterba 		atomic_inc(&reloc_root->node->refs);
17225d4f98a2SYan Zheng 		path->nodes[level] = reloc_root->node;
17235d4f98a2SYan Zheng 		path->slots[level] = 0;
17245d4f98a2SYan Zheng 	} else {
17255d4f98a2SYan Zheng 		btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
17265d4f98a2SYan Zheng 
1727c8422684SDavid Sterba 		level = btrfs_root_drop_level(root_item);
17285d4f98a2SYan Zheng 		BUG_ON(level == 0);
17295d4f98a2SYan Zheng 		path->lowest_level = level;
17305d4f98a2SYan Zheng 		ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0);
173133c66f43SYan Zheng 		path->lowest_level = 0;
17325d4f98a2SYan Zheng 		if (ret < 0) {
17335d4f98a2SYan Zheng 			btrfs_free_path(path);
17345d4f98a2SYan Zheng 			return ret;
17355d4f98a2SYan Zheng 		}
17365d4f98a2SYan Zheng 
17375d4f98a2SYan Zheng 		btrfs_node_key_to_cpu(path->nodes[level], &next_key,
17385d4f98a2SYan Zheng 				      path->slots[level]);
17395d4f98a2SYan Zheng 		WARN_ON(memcmp(&key, &next_key, sizeof(key)));
17405d4f98a2SYan Zheng 
17415d4f98a2SYan Zheng 		btrfs_unlock_up_safe(path, 0);
17425d4f98a2SYan Zheng 	}
17435d4f98a2SYan Zheng 
174444d354abSQu Wenruo 	/*
174544d354abSQu Wenruo 	 * In merge_reloc_root(), we modify the upper level pointer to swap the
174644d354abSQu Wenruo 	 * tree blocks between reloc tree and subvolume tree.  Thus for tree
174744d354abSQu Wenruo 	 * block COW, we COW at most from level 1 to root level for each tree.
174844d354abSQu Wenruo 	 *
174944d354abSQu Wenruo 	 * Thus the needed metadata size is at most root_level * nodesize,
175044d354abSQu Wenruo 	 * and * 2 since we have two trees to COW.
175144d354abSQu Wenruo 	 */
1752fca3a45dSJosef Bacik 	reserve_level = max_t(int, 1, btrfs_root_level(root_item));
1753fca3a45dSJosef Bacik 	min_reserved = fs_info->nodesize * reserve_level * 2;
17545d4f98a2SYan Zheng 	memset(&next_key, 0, sizeof(next_key));
17555d4f98a2SYan Zheng 
17565d4f98a2SYan Zheng 	while (1) {
17579270501cSJosef Bacik 		ret = btrfs_block_rsv_refill(fs_info, rc->block_rsv,
17589270501cSJosef Bacik 					     min_reserved,
175944d354abSQu Wenruo 					     BTRFS_RESERVE_FLUSH_LIMIT);
1760c6a592f2SNikolay Borisov 		if (ret)
17619e6a0c52SJosef Bacik 			goto out;
17629e6a0c52SJosef Bacik 		trans = btrfs_start_transaction(root, 0);
17639e6a0c52SJosef Bacik 		if (IS_ERR(trans)) {
1764c6a592f2SNikolay Borisov 			ret = PTR_ERR(trans);
17659e6a0c52SJosef Bacik 			trans = NULL;
17669e6a0c52SJosef Bacik 			goto out;
17679e6a0c52SJosef Bacik 		}
17682abc726aSJosef Bacik 
17692abc726aSJosef Bacik 		/*
17702abc726aSJosef Bacik 		 * At this point we no longer have a reloc_control, so we can't
17712abc726aSJosef Bacik 		 * depend on btrfs_init_reloc_root to update our last_trans.
17722abc726aSJosef Bacik 		 *
17732abc726aSJosef Bacik 		 * But that's ok, we started the trans handle on our
17742abc726aSJosef Bacik 		 * corresponding fs_root, which means it's been added to the
17752abc726aSJosef Bacik 		 * dirty list.  At commit time we'll still call
17762abc726aSJosef Bacik 		 * btrfs_update_reloc_root() and update our root item
17772abc726aSJosef Bacik 		 * appropriately.
17782abc726aSJosef Bacik 		 */
17792abc726aSJosef Bacik 		reloc_root->last_trans = trans->transid;
17809e6a0c52SJosef Bacik 		trans->block_rsv = rc->block_rsv;
17813fd0a558SYan, Zheng 
17823fd0a558SYan, Zheng 		replaced = 0;
17835d4f98a2SYan Zheng 		max_level = level;
17845d4f98a2SYan Zheng 
17855d4f98a2SYan Zheng 		ret = walk_down_reloc_tree(reloc_root, path, &level);
1786c6a592f2SNikolay Borisov 		if (ret < 0)
17875d4f98a2SYan Zheng 			goto out;
17885d4f98a2SYan Zheng 		if (ret > 0)
17895d4f98a2SYan Zheng 			break;
17905d4f98a2SYan Zheng 
17915d4f98a2SYan Zheng 		if (!find_next_key(path, level, &key) &&
17925d4f98a2SYan Zheng 		    btrfs_comp_cpu_keys(&next_key, &key) >= 0) {
17935d4f98a2SYan Zheng 			ret = 0;
17945d4f98a2SYan Zheng 		} else {
17953d0174f7SQu Wenruo 			ret = replace_path(trans, rc, root, reloc_root, path,
17963fd0a558SYan, Zheng 					   &next_key, level, max_level);
17975d4f98a2SYan Zheng 		}
1798c6a592f2SNikolay Borisov 		if (ret < 0)
17995d4f98a2SYan Zheng 			goto out;
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]);
1818c8422684SDavid Sterba 		btrfs_set_root_drop_level(root_item, 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);
18349631e4ccSJosef Bacik 	ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf,
18359631e4ccSJosef Bacik 			      BTRFS_NESTING_COW);
18365d4f98a2SYan Zheng 	btrfs_tree_unlock(leaf);
18375d4f98a2SYan Zheng 	free_extent_buffer(leaf);
18385d4f98a2SYan Zheng out:
18395d4f98a2SYan Zheng 	btrfs_free_path(path);
18405d4f98a2SYan Zheng 
1841ac54da6cSJosef Bacik 	if (ret == 0) {
1842ac54da6cSJosef Bacik 		ret = insert_dirty_subvol(trans, rc, root);
1843ac54da6cSJosef Bacik 		if (ret)
1844ac54da6cSJosef Bacik 			btrfs_abort_transaction(trans, ret);
1845ac54da6cSJosef Bacik 	}
18465d4f98a2SYan Zheng 
18479e6a0c52SJosef Bacik 	if (trans)
18483a45bb20SJeff Mahoney 		btrfs_end_transaction_throttle(trans);
18495d4f98a2SYan Zheng 
18502ff7e61eSJeff Mahoney 	btrfs_btree_balance_dirty(fs_info);
18515d4f98a2SYan Zheng 
18525d4f98a2SYan Zheng 	if (replaced && rc->stage == UPDATE_DATA_PTRS)
18535d4f98a2SYan Zheng 		invalidate_extent_cache(root, &key, &next_key);
18545d4f98a2SYan Zheng 
1855c6a592f2SNikolay Borisov 	return ret;
18565d4f98a2SYan Zheng }
18575d4f98a2SYan Zheng 
18583fd0a558SYan, Zheng static noinline_for_stack
prepare_to_merge(struct reloc_control * rc,int err)18593fd0a558SYan, Zheng int prepare_to_merge(struct reloc_control *rc, int err)
18605d4f98a2SYan Zheng {
18613fd0a558SYan, Zheng 	struct btrfs_root *root = rc->extent_root;
18620b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
18633fd0a558SYan, Zheng 	struct btrfs_root *reloc_root;
18645d4f98a2SYan Zheng 	struct btrfs_trans_handle *trans;
18653fd0a558SYan, Zheng 	LIST_HEAD(reloc_roots);
18663fd0a558SYan, Zheng 	u64 num_bytes = 0;
18673fd0a558SYan, Zheng 	int ret;
18683fd0a558SYan, Zheng 
18690b246afaSJeff Mahoney 	mutex_lock(&fs_info->reloc_mutex);
18700b246afaSJeff Mahoney 	rc->merging_rsv_size += fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
18713fd0a558SYan, Zheng 	rc->merging_rsv_size += rc->nodes_relocated * 2;
18720b246afaSJeff Mahoney 	mutex_unlock(&fs_info->reloc_mutex);
18737585717fSChris Mason 
18743fd0a558SYan, Zheng again:
18753fd0a558SYan, Zheng 	if (!err) {
18763fd0a558SYan, Zheng 		num_bytes = rc->merging_rsv_size;
18779270501cSJosef Bacik 		ret = btrfs_block_rsv_add(fs_info, rc->block_rsv, num_bytes,
187808e007d2SMiao Xie 					  BTRFS_RESERVE_FLUSH_ALL);
18793fd0a558SYan, Zheng 		if (ret)
18803fd0a558SYan, Zheng 			err = ret;
18813fd0a558SYan, Zheng 	}
18823fd0a558SYan, Zheng 
18837a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(rc->extent_root);
18843612b495STsutomu Itoh 	if (IS_ERR(trans)) {
18853612b495STsutomu Itoh 		if (!err)
18862ff7e61eSJeff Mahoney 			btrfs_block_rsv_release(fs_info, rc->block_rsv,
188763f018beSNikolay Borisov 						num_bytes, NULL);
18883612b495STsutomu Itoh 		return PTR_ERR(trans);
18893612b495STsutomu Itoh 	}
18903fd0a558SYan, Zheng 
18913fd0a558SYan, Zheng 	if (!err) {
18923fd0a558SYan, Zheng 		if (num_bytes != rc->merging_rsv_size) {
18933a45bb20SJeff Mahoney 			btrfs_end_transaction(trans);
18942ff7e61eSJeff Mahoney 			btrfs_block_rsv_release(fs_info, rc->block_rsv,
189563f018beSNikolay Borisov 						num_bytes, NULL);
18963fd0a558SYan, Zheng 			goto again;
18973fd0a558SYan, Zheng 		}
18983fd0a558SYan, Zheng 	}
18993fd0a558SYan, Zheng 
19003fd0a558SYan, Zheng 	rc->merge_reloc_tree = 1;
19013fd0a558SYan, Zheng 
19023fd0a558SYan, Zheng 	while (!list_empty(&rc->reloc_roots)) {
19033fd0a558SYan, Zheng 		reloc_root = list_entry(rc->reloc_roots.next,
19043fd0a558SYan, Zheng 					struct btrfs_root, root_list);
19053fd0a558SYan, Zheng 		list_del_init(&reloc_root->root_list);
19063fd0a558SYan, Zheng 
1907a820feb5SDavid Sterba 		root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
1908a820feb5SDavid Sterba 				false);
1909e0b085b0SJosef Bacik 		if (IS_ERR(root)) {
1910e0b085b0SJosef Bacik 			/*
1911e0b085b0SJosef Bacik 			 * Even if we have an error we need this reloc root
1912e0b085b0SJosef Bacik 			 * back on our list so we can clean up properly.
1913e0b085b0SJosef Bacik 			 */
1914e0b085b0SJosef Bacik 			list_add(&reloc_root->root_list, &reloc_roots);
1915e0b085b0SJosef Bacik 			btrfs_abort_transaction(trans, (int)PTR_ERR(root));
1916e0b085b0SJosef Bacik 			if (!err)
1917e0b085b0SJosef Bacik 				err = PTR_ERR(root);
1918e0b085b0SJosef Bacik 			break;
1919e0b085b0SJosef Bacik 		}
192005d7ce50SQu Wenruo 
192105d7ce50SQu Wenruo 		if (unlikely(root->reloc_root != reloc_root)) {
192205d7ce50SQu Wenruo 			if (root->reloc_root) {
192305d7ce50SQu Wenruo 				btrfs_err(fs_info,
192405d7ce50SQu Wenruo "reloc tree mismatch, root %lld has reloc root key (%lld %u %llu) gen %llu, expect reloc root key (%lld %u %llu) gen %llu",
192505d7ce50SQu Wenruo 					  root->root_key.objectid,
192605d7ce50SQu Wenruo 					  root->reloc_root->root_key.objectid,
192705d7ce50SQu Wenruo 					  root->reloc_root->root_key.type,
192805d7ce50SQu Wenruo 					  root->reloc_root->root_key.offset,
192905d7ce50SQu Wenruo 					  btrfs_root_generation(
193005d7ce50SQu Wenruo 						  &root->reloc_root->root_item),
193105d7ce50SQu Wenruo 					  reloc_root->root_key.objectid,
193205d7ce50SQu Wenruo 					  reloc_root->root_key.type,
193305d7ce50SQu Wenruo 					  reloc_root->root_key.offset,
193405d7ce50SQu Wenruo 					  btrfs_root_generation(
193505d7ce50SQu Wenruo 						  &reloc_root->root_item));
193605d7ce50SQu Wenruo 			} else {
193705d7ce50SQu Wenruo 				btrfs_err(fs_info,
193805d7ce50SQu Wenruo "reloc tree mismatch, root %lld has no reloc root, expect reloc root key (%lld %u %llu) gen %llu",
193905d7ce50SQu Wenruo 					  root->root_key.objectid,
194005d7ce50SQu Wenruo 					  reloc_root->root_key.objectid,
194105d7ce50SQu Wenruo 					  reloc_root->root_key.type,
194205d7ce50SQu Wenruo 					  reloc_root->root_key.offset,
194305d7ce50SQu Wenruo 					  btrfs_root_generation(
194405d7ce50SQu Wenruo 						  &reloc_root->root_item));
194505d7ce50SQu Wenruo 			}
194605d7ce50SQu Wenruo 			list_add(&reloc_root->root_list, &reloc_roots);
194705d7ce50SQu Wenruo 			btrfs_put_root(root);
194805d7ce50SQu Wenruo 			btrfs_abort_transaction(trans, -EUCLEAN);
194905d7ce50SQu Wenruo 			if (!err)
195005d7ce50SQu Wenruo 				err = -EUCLEAN;
195105d7ce50SQu Wenruo 			break;
195205d7ce50SQu Wenruo 		}
19533fd0a558SYan, Zheng 
19543fd0a558SYan, Zheng 		/*
19553fd0a558SYan, Zheng 		 * set reference count to 1, so btrfs_recover_relocation
19563fd0a558SYan, Zheng 		 * knows it should resumes merging
19573fd0a558SYan, Zheng 		 */
19583fd0a558SYan, Zheng 		if (!err)
19593fd0a558SYan, Zheng 			btrfs_set_root_refs(&reloc_root->root_item, 1);
1960bbae13f8SJosef Bacik 		ret = btrfs_update_reloc_root(trans, root);
19613fd0a558SYan, Zheng 
1962bbae13f8SJosef Bacik 		/*
1963bbae13f8SJosef Bacik 		 * Even if we have an error we need this reloc root back on our
1964bbae13f8SJosef Bacik 		 * list so we can clean up properly.
1965bbae13f8SJosef Bacik 		 */
19663fd0a558SYan, Zheng 		list_add(&reloc_root->root_list, &reloc_roots);
196700246528SJosef Bacik 		btrfs_put_root(root);
1968bbae13f8SJosef Bacik 
1969bbae13f8SJosef Bacik 		if (ret) {
1970bbae13f8SJosef Bacik 			btrfs_abort_transaction(trans, ret);
1971bbae13f8SJosef Bacik 			if (!err)
1972bbae13f8SJosef Bacik 				err = ret;
1973bbae13f8SJosef Bacik 			break;
1974bbae13f8SJosef Bacik 		}
19753fd0a558SYan, Zheng 	}
19763fd0a558SYan, Zheng 
19773fd0a558SYan, Zheng 	list_splice(&reloc_roots, &rc->reloc_roots);
19783fd0a558SYan, Zheng 
19793fd0a558SYan, Zheng 	if (!err)
1980fb686c68SJosef Bacik 		err = btrfs_commit_transaction(trans);
19813fd0a558SYan, Zheng 	else
19823a45bb20SJeff Mahoney 		btrfs_end_transaction(trans);
19833fd0a558SYan, Zheng 	return err;
19843fd0a558SYan, Zheng }
19853fd0a558SYan, Zheng 
19863fd0a558SYan, Zheng static noinline_for_stack
free_reloc_roots(struct list_head * list)1987aca1bba6SLiu Bo void free_reloc_roots(struct list_head *list)
1988aca1bba6SLiu Bo {
1989a7571232SNikolay Borisov 	struct btrfs_root *reloc_root, *tmp;
1990aca1bba6SLiu Bo 
1991a7571232SNikolay Borisov 	list_for_each_entry_safe(reloc_root, tmp, list, root_list)
1992bb166d72SNaohiro Aota 		__del_reloc_root(reloc_root);
1993aca1bba6SLiu Bo }
1994aca1bba6SLiu Bo 
1995aca1bba6SLiu Bo static noinline_for_stack
merge_reloc_roots(struct reloc_control * rc)199694404e82SDavid Sterba void merge_reloc_roots(struct reloc_control *rc)
19973fd0a558SYan, Zheng {
19980b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
19995d4f98a2SYan Zheng 	struct btrfs_root *root;
20005d4f98a2SYan Zheng 	struct btrfs_root *reloc_root;
20013fd0a558SYan, Zheng 	LIST_HEAD(reloc_roots);
20023fd0a558SYan, Zheng 	int found = 0;
2003aca1bba6SLiu Bo 	int ret = 0;
20043fd0a558SYan, Zheng again:
20053fd0a558SYan, Zheng 	root = rc->extent_root;
20067585717fSChris Mason 
20077585717fSChris Mason 	/*
20087585717fSChris Mason 	 * this serializes us with btrfs_record_root_in_transaction,
20097585717fSChris Mason 	 * we have to make sure nobody is in the middle of
20107585717fSChris Mason 	 * adding their roots to the list while we are
20117585717fSChris Mason 	 * doing this splice
20127585717fSChris Mason 	 */
20130b246afaSJeff Mahoney 	mutex_lock(&fs_info->reloc_mutex);
20143fd0a558SYan, Zheng 	list_splice_init(&rc->reloc_roots, &reloc_roots);
20150b246afaSJeff Mahoney 	mutex_unlock(&fs_info->reloc_mutex);
20165d4f98a2SYan Zheng 
20173fd0a558SYan, Zheng 	while (!list_empty(&reloc_roots)) {
20183fd0a558SYan, Zheng 		found = 1;
20193fd0a558SYan, Zheng 		reloc_root = list_entry(reloc_roots.next,
20203fd0a558SYan, Zheng 					struct btrfs_root, root_list);
20215d4f98a2SYan Zheng 
2022a820feb5SDavid Sterba 		root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
2023a820feb5SDavid Sterba 					 false);
20245d4f98a2SYan Zheng 		if (btrfs_root_refs(&reloc_root->root_item) > 0) {
202505d7ce50SQu Wenruo 			if (WARN_ON(IS_ERR(root))) {
202624213fa4SJosef Bacik 				/*
202724213fa4SJosef Bacik 				 * For recovery we read the fs roots on mount,
202824213fa4SJosef Bacik 				 * and if we didn't find the root then we marked
202924213fa4SJosef Bacik 				 * the reloc root as a garbage root.  For normal
203024213fa4SJosef Bacik 				 * relocation obviously the root should exist in
203124213fa4SJosef Bacik 				 * memory.  However there's no reason we can't
203224213fa4SJosef Bacik 				 * handle the error properly here just in case.
203324213fa4SJosef Bacik 				 */
203424213fa4SJosef Bacik 				ret = PTR_ERR(root);
203524213fa4SJosef Bacik 				goto out;
203624213fa4SJosef Bacik 			}
203705d7ce50SQu Wenruo 			if (WARN_ON(root->reloc_root != reloc_root)) {
203824213fa4SJosef Bacik 				/*
203905d7ce50SQu Wenruo 				 * This can happen if on-disk metadata has some
204005d7ce50SQu Wenruo 				 * corruption, e.g. bad reloc tree key offset.
204124213fa4SJosef Bacik 				 */
204224213fa4SJosef Bacik 				ret = -EINVAL;
204324213fa4SJosef Bacik 				goto out;
204424213fa4SJosef Bacik 			}
20453fd0a558SYan, Zheng 			ret = merge_reloc_root(rc, root);
204600246528SJosef Bacik 			btrfs_put_root(root);
2047b37b39cdSJosef Bacik 			if (ret) {
204825e293c2SWang Shilong 				if (list_empty(&reloc_root->root_list))
204925e293c2SWang Shilong 					list_add_tail(&reloc_root->root_list,
205025e293c2SWang Shilong 						      &reloc_roots);
2051aca1bba6SLiu Bo 				goto out;
2052b37b39cdSJosef Bacik 			}
20533fd0a558SYan, Zheng 		} else {
205451415b6cSQu Wenruo 			if (!IS_ERR(root)) {
205551415b6cSQu Wenruo 				if (root->reloc_root == reloc_root) {
205651415b6cSQu Wenruo 					root->reloc_root = NULL;
205751415b6cSQu Wenruo 					btrfs_put_root(reloc_root);
205851415b6cSQu Wenruo 				}
20591dae7e0eSQu Wenruo 				clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE,
20601dae7e0eSQu Wenruo 					  &root->state);
206151415b6cSQu Wenruo 				btrfs_put_root(root);
206251415b6cSQu Wenruo 			}
206351415b6cSQu Wenruo 
20643fd0a558SYan, Zheng 			list_del_init(&reloc_root->root_list);
206530d40577SQu Wenruo 			/* Don't forget to queue this reloc root for cleanup */
206630d40577SQu Wenruo 			list_add_tail(&reloc_root->reloc_dirty_list,
206730d40577SQu Wenruo 				      &rc->dirty_subvol_roots);
20683fd0a558SYan, Zheng 		}
20695d4f98a2SYan Zheng 	}
20705d4f98a2SYan Zheng 
20713fd0a558SYan, Zheng 	if (found) {
20723fd0a558SYan, Zheng 		found = 0;
20733fd0a558SYan, Zheng 		goto again;
20745d4f98a2SYan Zheng 	}
2075aca1bba6SLiu Bo out:
2076aca1bba6SLiu Bo 	if (ret) {
20770b246afaSJeff Mahoney 		btrfs_handle_fs_error(fs_info, ret, NULL);
2078aca1bba6SLiu Bo 		free_reloc_roots(&reloc_roots);
2079467bb1d2SWang Shilong 
2080467bb1d2SWang Shilong 		/* new reloc root may be added */
20810b246afaSJeff Mahoney 		mutex_lock(&fs_info->reloc_mutex);
2082467bb1d2SWang Shilong 		list_splice_init(&rc->reloc_roots, &reloc_roots);
20830b246afaSJeff Mahoney 		mutex_unlock(&fs_info->reloc_mutex);
2084467bb1d2SWang Shilong 		free_reloc_roots(&reloc_roots);
2085aca1bba6SLiu Bo 	}
2086aca1bba6SLiu Bo 
20877b7b7431SJosef Bacik 	/*
20887b7b7431SJosef Bacik 	 * We used to have
20897b7b7431SJosef Bacik 	 *
20907b7b7431SJosef Bacik 	 * BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
20917b7b7431SJosef Bacik 	 *
20927b7b7431SJosef Bacik 	 * here, but it's wrong.  If we fail to start the transaction in
20937b7b7431SJosef Bacik 	 * prepare_to_merge() we will have only 0 ref reloc roots, none of which
20947b7b7431SJosef Bacik 	 * have actually been removed from the reloc_root_tree rb tree.  This is
20957b7b7431SJosef Bacik 	 * fine because we're bailing here, and we hold a reference on the root
20967b7b7431SJosef Bacik 	 * for the list that holds it, so these roots will be cleaned up when we
20977b7b7431SJosef Bacik 	 * do the reloc_dirty_list afterwards.  Meanwhile the root->reloc_root
20987b7b7431SJosef Bacik 	 * will be cleaned up on unmount.
20997b7b7431SJosef Bacik 	 *
21007b7b7431SJosef Bacik 	 * The remaining nodes will be cleaned up by free_reloc_control.
21017b7b7431SJosef Bacik 	 */
21025d4f98a2SYan Zheng }
21035d4f98a2SYan Zheng 
free_block_list(struct rb_root * blocks)21045d4f98a2SYan Zheng static void free_block_list(struct rb_root *blocks)
21055d4f98a2SYan Zheng {
21065d4f98a2SYan Zheng 	struct tree_block *block;
21075d4f98a2SYan Zheng 	struct rb_node *rb_node;
21085d4f98a2SYan Zheng 	while ((rb_node = rb_first(blocks))) {
21095d4f98a2SYan Zheng 		block = rb_entry(rb_node, struct tree_block, rb_node);
21105d4f98a2SYan Zheng 		rb_erase(rb_node, blocks);
21115d4f98a2SYan Zheng 		kfree(block);
21125d4f98a2SYan Zheng 	}
21135d4f98a2SYan Zheng }
21145d4f98a2SYan Zheng 
record_reloc_root_in_trans(struct btrfs_trans_handle * trans,struct btrfs_root * reloc_root)21155d4f98a2SYan Zheng static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans,
21165d4f98a2SYan Zheng 				      struct btrfs_root *reloc_root)
21175d4f98a2SYan Zheng {
21180b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = reloc_root->fs_info;
21195d4f98a2SYan Zheng 	struct btrfs_root *root;
2120442b1ac5SJosef Bacik 	int ret;
21215d4f98a2SYan Zheng 
21225d4f98a2SYan Zheng 	if (reloc_root->last_trans == trans->transid)
21235d4f98a2SYan Zheng 		return 0;
21245d4f98a2SYan Zheng 
2125a820feb5SDavid Sterba 	root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset, false);
2126404bccbcSJosef Bacik 
2127404bccbcSJosef Bacik 	/*
2128404bccbcSJosef Bacik 	 * This should succeed, since we can't have a reloc root without having
2129404bccbcSJosef Bacik 	 * already looked up the actual root and created the reloc root for this
2130404bccbcSJosef Bacik 	 * root.
2131404bccbcSJosef Bacik 	 *
2132404bccbcSJosef Bacik 	 * However if there's some sort of corruption where we have a ref to a
2133404bccbcSJosef Bacik 	 * reloc root without a corresponding root this could return ENOENT.
2134404bccbcSJosef Bacik 	 */
2135404bccbcSJosef Bacik 	if (IS_ERR(root)) {
2136404bccbcSJosef Bacik 		ASSERT(0);
2137404bccbcSJosef Bacik 		return PTR_ERR(root);
2138404bccbcSJosef Bacik 	}
2139404bccbcSJosef Bacik 	if (root->reloc_root != reloc_root) {
2140404bccbcSJosef Bacik 		ASSERT(0);
2141404bccbcSJosef Bacik 		btrfs_err(fs_info,
2142404bccbcSJosef Bacik 			  "root %llu has two reloc roots associated with it",
2143404bccbcSJosef Bacik 			  reloc_root->root_key.offset);
2144404bccbcSJosef Bacik 		btrfs_put_root(root);
2145404bccbcSJosef Bacik 		return -EUCLEAN;
2146404bccbcSJosef Bacik 	}
2147442b1ac5SJosef Bacik 	ret = btrfs_record_root_in_trans(trans, root);
214800246528SJosef Bacik 	btrfs_put_root(root);
21495d4f98a2SYan Zheng 
2150442b1ac5SJosef Bacik 	return ret;
21515d4f98a2SYan Zheng }
21525d4f98a2SYan Zheng 
21533fd0a558SYan, Zheng static noinline_for_stack
select_reloc_root(struct btrfs_trans_handle * trans,struct reloc_control * rc,struct btrfs_backref_node * node,struct btrfs_backref_edge * edges[])21543fd0a558SYan, Zheng struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,
21553fd0a558SYan, Zheng 				     struct reloc_control *rc,
2156a26195a5SQu Wenruo 				     struct btrfs_backref_node *node,
2157a26195a5SQu Wenruo 				     struct btrfs_backref_edge *edges[])
21585d4f98a2SYan Zheng {
2159a26195a5SQu Wenruo 	struct btrfs_backref_node *next;
21605d4f98a2SYan Zheng 	struct btrfs_root *root;
21613fd0a558SYan, Zheng 	int index = 0;
216292de551bSJosef Bacik 	int ret;
21633fd0a558SYan, Zheng 
21645d4f98a2SYan Zheng 	next = node;
21655d4f98a2SYan Zheng 	while (1) {
21665d4f98a2SYan Zheng 		cond_resched();
21675d4f98a2SYan Zheng 		next = walk_up_backref(next, edges, &index);
21685d4f98a2SYan Zheng 		root = next->root;
21698ee66afeSJosef Bacik 
21708ee66afeSJosef Bacik 		/*
21718ee66afeSJosef Bacik 		 * If there is no root, then our references for this block are
21728ee66afeSJosef Bacik 		 * incomplete, as we should be able to walk all the way up to a
21738ee66afeSJosef Bacik 		 * block that is owned by a root.
21748ee66afeSJosef Bacik 		 *
21758ee66afeSJosef Bacik 		 * This path is only for SHAREABLE roots, so if we come upon a
21768ee66afeSJosef Bacik 		 * non-SHAREABLE root then we have backrefs that resolve
21778ee66afeSJosef Bacik 		 * improperly.
21788ee66afeSJosef Bacik 		 *
21798ee66afeSJosef Bacik 		 * Both of these cases indicate file system corruption, or a bug
21808ee66afeSJosef Bacik 		 * in the backref walking code.
21818ee66afeSJosef Bacik 		 */
21828ee66afeSJosef Bacik 		if (!root) {
21838ee66afeSJosef Bacik 			ASSERT(0);
21848ee66afeSJosef Bacik 			btrfs_err(trans->fs_info,
21858ee66afeSJosef Bacik 		"bytenr %llu doesn't have a backref path ending in a root",
21868ee66afeSJosef Bacik 				  node->bytenr);
21878ee66afeSJosef Bacik 			return ERR_PTR(-EUCLEAN);
21888ee66afeSJosef Bacik 		}
21898ee66afeSJosef Bacik 		if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
21908ee66afeSJosef Bacik 			ASSERT(0);
21918ee66afeSJosef Bacik 			btrfs_err(trans->fs_info,
21928ee66afeSJosef Bacik 	"bytenr %llu has multiple refs with one ending in a non-shareable root",
21938ee66afeSJosef Bacik 				  node->bytenr);
21948ee66afeSJosef Bacik 			return ERR_PTR(-EUCLEAN);
21958ee66afeSJosef Bacik 		}
21965d4f98a2SYan Zheng 
21975d4f98a2SYan Zheng 		if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
219892de551bSJosef Bacik 			ret = record_reloc_root_in_trans(trans, root);
219992de551bSJosef Bacik 			if (ret)
220092de551bSJosef Bacik 				return ERR_PTR(ret);
22015d4f98a2SYan Zheng 			break;
22025d4f98a2SYan Zheng 		}
22035d4f98a2SYan Zheng 
220492de551bSJosef Bacik 		ret = btrfs_record_root_in_trans(trans, root);
220592de551bSJosef Bacik 		if (ret)
220692de551bSJosef Bacik 			return ERR_PTR(ret);
22073fd0a558SYan, Zheng 		root = root->reloc_root;
22083fd0a558SYan, Zheng 
220939200e59SJosef Bacik 		/*
221039200e59SJosef Bacik 		 * We could have raced with another thread which failed, so
221139200e59SJosef Bacik 		 * root->reloc_root may not be set, return ENOENT in this case.
221239200e59SJosef Bacik 		 */
221339200e59SJosef Bacik 		if (!root)
221439200e59SJosef Bacik 			return ERR_PTR(-ENOENT);
221539200e59SJosef Bacik 
22163fd0a558SYan, Zheng 		if (next->new_bytenr != root->node->start) {
22178ee66afeSJosef Bacik 			/*
22188ee66afeSJosef Bacik 			 * We just created the reloc root, so we shouldn't have
22198ee66afeSJosef Bacik 			 * ->new_bytenr set and this shouldn't be in the changed
22208ee66afeSJosef Bacik 			 *  list.  If it is then we have multiple roots pointing
22218ee66afeSJosef Bacik 			 *  at the same bytenr which indicates corruption, or
22228ee66afeSJosef Bacik 			 *  we've made a mistake in the backref walking code.
22238ee66afeSJosef Bacik 			 */
22248ee66afeSJosef Bacik 			ASSERT(next->new_bytenr == 0);
22258ee66afeSJosef Bacik 			ASSERT(list_empty(&next->list));
22268ee66afeSJosef Bacik 			if (next->new_bytenr || !list_empty(&next->list)) {
22278ee66afeSJosef Bacik 				btrfs_err(trans->fs_info,
22288ee66afeSJosef Bacik 	"bytenr %llu possibly has multiple roots pointing at the same bytenr %llu",
22298ee66afeSJosef Bacik 					  node->bytenr, next->bytenr);
22308ee66afeSJosef Bacik 				return ERR_PTR(-EUCLEAN);
22318ee66afeSJosef Bacik 			}
22328ee66afeSJosef Bacik 
22333fd0a558SYan, Zheng 			next->new_bytenr = root->node->start;
223400246528SJosef Bacik 			btrfs_put_root(next->root);
223500246528SJosef Bacik 			next->root = btrfs_grab_root(root);
22360b530bc5SJosef Bacik 			ASSERT(next->root);
22373fd0a558SYan, Zheng 			list_add_tail(&next->list,
22383fd0a558SYan, Zheng 				      &rc->backref_cache.changed);
22399569cc20SQu Wenruo 			mark_block_processed(rc, next);
22405d4f98a2SYan Zheng 			break;
22415d4f98a2SYan Zheng 		}
22425d4f98a2SYan Zheng 
22433fd0a558SYan, Zheng 		WARN_ON(1);
22445d4f98a2SYan Zheng 		root = NULL;
22455d4f98a2SYan Zheng 		next = walk_down_backref(edges, &index);
22465d4f98a2SYan Zheng 		if (!next || next->level <= node->level)
22475d4f98a2SYan Zheng 			break;
22485d4f98a2SYan Zheng 	}
2249cbdc2ebcSJosef Bacik 	if (!root) {
2250cbdc2ebcSJosef Bacik 		/*
2251cbdc2ebcSJosef Bacik 		 * This can happen if there's fs corruption or if there's a bug
2252cbdc2ebcSJosef Bacik 		 * in the backref lookup code.
2253cbdc2ebcSJosef Bacik 		 */
2254cbdc2ebcSJosef Bacik 		ASSERT(0);
2255cbdc2ebcSJosef Bacik 		return ERR_PTR(-ENOENT);
2256cbdc2ebcSJosef Bacik 	}
22575d4f98a2SYan Zheng 
22583fd0a558SYan, Zheng 	next = node;
22593fd0a558SYan, Zheng 	/* setup backref node path for btrfs_reloc_cow_block */
22603fd0a558SYan, Zheng 	while (1) {
22613fd0a558SYan, Zheng 		rc->backref_cache.path[next->level] = next;
22623fd0a558SYan, Zheng 		if (--index < 0)
22633fd0a558SYan, Zheng 			break;
22643fd0a558SYan, Zheng 		next = edges[index]->node[UPPER];
22653fd0a558SYan, Zheng 	}
22665d4f98a2SYan Zheng 	return root;
22675d4f98a2SYan Zheng }
22685d4f98a2SYan Zheng 
22693fd0a558SYan, Zheng /*
227092a7cc42SQu Wenruo  * Select a tree root for relocation.
227192a7cc42SQu Wenruo  *
227292a7cc42SQu Wenruo  * Return NULL if the block is not shareable. We should use do_relocation() in
227392a7cc42SQu Wenruo  * this case.
227492a7cc42SQu Wenruo  *
227592a7cc42SQu Wenruo  * Return a tree root pointer if the block is shareable.
227692a7cc42SQu Wenruo  * Return -ENOENT if the block is root of reloc tree.
22773fd0a558SYan, Zheng  */
22785d4f98a2SYan Zheng static noinline_for_stack
select_one_root(struct btrfs_backref_node * node)2279a26195a5SQu Wenruo struct btrfs_root *select_one_root(struct btrfs_backref_node *node)
22805d4f98a2SYan Zheng {
2281a26195a5SQu Wenruo 	struct btrfs_backref_node *next;
22823fd0a558SYan, Zheng 	struct btrfs_root *root;
22833fd0a558SYan, Zheng 	struct btrfs_root *fs_root = NULL;
2284a26195a5SQu Wenruo 	struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
22853fd0a558SYan, Zheng 	int index = 0;
22863fd0a558SYan, Zheng 
22873fd0a558SYan, Zheng 	next = node;
22883fd0a558SYan, Zheng 	while (1) {
22893fd0a558SYan, Zheng 		cond_resched();
22903fd0a558SYan, Zheng 		next = walk_up_backref(next, edges, &index);
22913fd0a558SYan, Zheng 		root = next->root;
22928717cf44SJosef Bacik 
22938717cf44SJosef Bacik 		/*
22948717cf44SJosef Bacik 		 * This can occur if we have incomplete extent refs leading all
22958717cf44SJosef Bacik 		 * the way up a particular path, in this case return -EUCLEAN.
22968717cf44SJosef Bacik 		 */
22978717cf44SJosef Bacik 		if (!root)
22988717cf44SJosef Bacik 			return ERR_PTR(-EUCLEAN);
22993fd0a558SYan, Zheng 
230092a7cc42SQu Wenruo 		/* No other choice for non-shareable tree */
230192a7cc42SQu Wenruo 		if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
23023fd0a558SYan, Zheng 			return root;
23033fd0a558SYan, Zheng 
23043fd0a558SYan, Zheng 		if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)
23053fd0a558SYan, Zheng 			fs_root = root;
23063fd0a558SYan, Zheng 
23073fd0a558SYan, Zheng 		if (next != node)
23083fd0a558SYan, Zheng 			return NULL;
23093fd0a558SYan, Zheng 
23103fd0a558SYan, Zheng 		next = walk_down_backref(edges, &index);
23113fd0a558SYan, Zheng 		if (!next || next->level <= node->level)
23123fd0a558SYan, Zheng 			break;
23133fd0a558SYan, Zheng 	}
23143fd0a558SYan, Zheng 
23153fd0a558SYan, Zheng 	if (!fs_root)
23163fd0a558SYan, Zheng 		return ERR_PTR(-ENOENT);
23173fd0a558SYan, Zheng 	return fs_root;
23185d4f98a2SYan Zheng }
23195d4f98a2SYan Zheng 
23205d4f98a2SYan Zheng static noinline_for_stack
calcu_metadata_size(struct reloc_control * rc,struct btrfs_backref_node * node,int reserve)23213fd0a558SYan, Zheng u64 calcu_metadata_size(struct reloc_control *rc,
2322a26195a5SQu Wenruo 			struct btrfs_backref_node *node, int reserve)
23235d4f98a2SYan Zheng {
23240b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2325a26195a5SQu Wenruo 	struct btrfs_backref_node *next = node;
2326a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
2327a26195a5SQu Wenruo 	struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
23283fd0a558SYan, Zheng 	u64 num_bytes = 0;
23293fd0a558SYan, Zheng 	int index = 0;
23305d4f98a2SYan Zheng 
23313fd0a558SYan, Zheng 	BUG_ON(reserve && node->processed);
23323fd0a558SYan, Zheng 
23333fd0a558SYan, Zheng 	while (next) {
23343fd0a558SYan, Zheng 		cond_resched();
23355d4f98a2SYan Zheng 		while (1) {
23363fd0a558SYan, Zheng 			if (next->processed && (reserve || next != node))
23375d4f98a2SYan Zheng 				break;
23385d4f98a2SYan Zheng 
23390b246afaSJeff Mahoney 			num_bytes += fs_info->nodesize;
23403fd0a558SYan, Zheng 
23413fd0a558SYan, Zheng 			if (list_empty(&next->upper))
23423fd0a558SYan, Zheng 				break;
23433fd0a558SYan, Zheng 
23443fd0a558SYan, Zheng 			edge = list_entry(next->upper.next,
2345a26195a5SQu Wenruo 					struct btrfs_backref_edge, list[LOWER]);
23463fd0a558SYan, Zheng 			edges[index++] = edge;
23473fd0a558SYan, Zheng 			next = edge->node[UPPER];
23485d4f98a2SYan Zheng 		}
23493fd0a558SYan, Zheng 		next = walk_down_backref(edges, &index);
23503fd0a558SYan, Zheng 	}
23513fd0a558SYan, Zheng 	return num_bytes;
23523fd0a558SYan, Zheng }
23533fd0a558SYan, Zheng 
reserve_metadata_space(struct btrfs_trans_handle * trans,struct reloc_control * rc,struct btrfs_backref_node * node)23543fd0a558SYan, Zheng static int reserve_metadata_space(struct btrfs_trans_handle *trans,
23553fd0a558SYan, Zheng 				  struct reloc_control *rc,
2356a26195a5SQu Wenruo 				  struct btrfs_backref_node *node)
23573fd0a558SYan, Zheng {
23583fd0a558SYan, Zheng 	struct btrfs_root *root = rc->extent_root;
2359da17066cSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
23603fd0a558SYan, Zheng 	u64 num_bytes;
23613fd0a558SYan, Zheng 	int ret;
23620647bf56SWang Shilong 	u64 tmp;
23633fd0a558SYan, Zheng 
23643fd0a558SYan, Zheng 	num_bytes = calcu_metadata_size(rc, node, 1) * 2;
23653fd0a558SYan, Zheng 
23663fd0a558SYan, Zheng 	trans->block_rsv = rc->block_rsv;
23670647bf56SWang Shilong 	rc->reserved_bytes += num_bytes;
23688ca17f0fSJosef Bacik 
23698ca17f0fSJosef Bacik 	/*
23708ca17f0fSJosef Bacik 	 * We are under a transaction here so we can only do limited flushing.
23718ca17f0fSJosef Bacik 	 * If we get an enospc just kick back -EAGAIN so we know to drop the
23728ca17f0fSJosef Bacik 	 * transaction and try to refill when we can flush all the things.
23738ca17f0fSJosef Bacik 	 */
23749270501cSJosef Bacik 	ret = btrfs_block_rsv_refill(fs_info, rc->block_rsv, num_bytes,
23758ca17f0fSJosef Bacik 				     BTRFS_RESERVE_FLUSH_LIMIT);
23763fd0a558SYan, Zheng 	if (ret) {
2377da17066cSJeff Mahoney 		tmp = fs_info->nodesize * RELOCATION_RESERVED_NODES;
23780647bf56SWang Shilong 		while (tmp <= rc->reserved_bytes)
23790647bf56SWang Shilong 			tmp <<= 1;
23800647bf56SWang Shilong 		/*
23810647bf56SWang Shilong 		 * only one thread can access block_rsv at this point,
23820647bf56SWang Shilong 		 * so we don't need hold lock to protect block_rsv.
23830647bf56SWang Shilong 		 * we expand more reservation size here to allow enough
238452042d8eSAndrea Gelmini 		 * space for relocation and we will return earlier in
23850647bf56SWang Shilong 		 * enospc case.
23860647bf56SWang Shilong 		 */
2387da17066cSJeff Mahoney 		rc->block_rsv->size = tmp + fs_info->nodesize *
23880647bf56SWang Shilong 				      RELOCATION_RESERVED_NODES;
23898ca17f0fSJosef Bacik 		return -EAGAIN;
23903fd0a558SYan, Zheng 	}
23913fd0a558SYan, Zheng 
23923fd0a558SYan, Zheng 	return 0;
23933fd0a558SYan, Zheng }
23943fd0a558SYan, Zheng 
23955d4f98a2SYan Zheng /*
23965d4f98a2SYan Zheng  * relocate a block tree, and then update pointers in upper level
23975d4f98a2SYan Zheng  * blocks that reference the block to point to the new location.
23985d4f98a2SYan Zheng  *
23995d4f98a2SYan Zheng  * if called by link_to_upper, the block has already been relocated.
24005d4f98a2SYan Zheng  * in that case this function just updates pointers.
24015d4f98a2SYan Zheng  */
do_relocation(struct btrfs_trans_handle * trans,struct reloc_control * rc,struct btrfs_backref_node * node,struct btrfs_key * key,struct btrfs_path * path,int lowest)24025d4f98a2SYan Zheng static int do_relocation(struct btrfs_trans_handle *trans,
24033fd0a558SYan, Zheng 			 struct reloc_control *rc,
2404a26195a5SQu Wenruo 			 struct btrfs_backref_node *node,
24055d4f98a2SYan Zheng 			 struct btrfs_key *key,
24065d4f98a2SYan Zheng 			 struct btrfs_path *path, int lowest)
24075d4f98a2SYan Zheng {
2408a26195a5SQu Wenruo 	struct btrfs_backref_node *upper;
2409a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
2410a26195a5SQu Wenruo 	struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
24115d4f98a2SYan Zheng 	struct btrfs_root *root;
24125d4f98a2SYan Zheng 	struct extent_buffer *eb;
24135d4f98a2SYan Zheng 	u32 blocksize;
24145d4f98a2SYan Zheng 	u64 bytenr;
24155d4f98a2SYan Zheng 	int slot;
24168df01fddSNikolay Borisov 	int ret = 0;
24175d4f98a2SYan Zheng 
2418ffe30dd8SJosef Bacik 	/*
2419ffe30dd8SJosef Bacik 	 * If we are lowest then this is the first time we're processing this
2420ffe30dd8SJosef Bacik 	 * block, and thus shouldn't have an eb associated with it yet.
2421ffe30dd8SJosef Bacik 	 */
2422ffe30dd8SJosef Bacik 	ASSERT(!lowest || !node->eb);
24235d4f98a2SYan Zheng 
24245d4f98a2SYan Zheng 	path->lowest_level = node->level + 1;
24253fd0a558SYan, Zheng 	rc->backref_cache.path[node->level] = node;
24265d4f98a2SYan Zheng 	list_for_each_entry(edge, &node->upper, list[LOWER]) {
242782fa113fSQu Wenruo 		struct btrfs_ref ref = { 0 };
2428581c1760SQu Wenruo 
24295d4f98a2SYan Zheng 		cond_resched();
24305d4f98a2SYan Zheng 
24315d4f98a2SYan Zheng 		upper = edge->node[UPPER];
2432dc4103f9SWang Shilong 		root = select_reloc_root(trans, rc, upper, edges);
2433cbdc2ebcSJosef Bacik 		if (IS_ERR(root)) {
2434cbdc2ebcSJosef Bacik 			ret = PTR_ERR(root);
2435cbdc2ebcSJosef Bacik 			goto next;
2436cbdc2ebcSJosef Bacik 		}
24375d4f98a2SYan Zheng 
24383fd0a558SYan, Zheng 		if (upper->eb && !upper->locked) {
24393fd0a558SYan, Zheng 			if (!lowest) {
2440fdf8d595SAnand Jain 				ret = btrfs_bin_search(upper->eb, 0, key, &slot);
24418df01fddSNikolay Borisov 				if (ret < 0)
2442cbca7d59SFilipe Manana 					goto next;
24433fd0a558SYan, Zheng 				BUG_ON(ret);
24443fd0a558SYan, Zheng 				bytenr = btrfs_node_blockptr(upper->eb, slot);
24453fd0a558SYan, Zheng 				if (node->eb->start == bytenr)
24463fd0a558SYan, Zheng 					goto next;
24473fd0a558SYan, Zheng 			}
2448b0fe7078SQu Wenruo 			btrfs_backref_drop_node_buffer(upper);
24493fd0a558SYan, Zheng 		}
24505d4f98a2SYan Zheng 
24515d4f98a2SYan Zheng 		if (!upper->eb) {
24525d4f98a2SYan Zheng 			ret = btrfs_search_slot(trans, root, key, path, 0, 1);
24533561b9dbSLiu Bo 			if (ret) {
24548df01fddSNikolay Borisov 				if (ret > 0)
24558df01fddSNikolay Borisov 					ret = -ENOENT;
24563561b9dbSLiu Bo 
24573561b9dbSLiu Bo 				btrfs_release_path(path);
24585d4f98a2SYan Zheng 				break;
24595d4f98a2SYan Zheng 			}
24605d4f98a2SYan Zheng 
24613fd0a558SYan, Zheng 			if (!upper->eb) {
24623fd0a558SYan, Zheng 				upper->eb = path->nodes[upper->level];
24633fd0a558SYan, Zheng 				path->nodes[upper->level] = NULL;
24643fd0a558SYan, Zheng 			} else {
24653fd0a558SYan, Zheng 				BUG_ON(upper->eb != path->nodes[upper->level]);
24663fd0a558SYan, Zheng 			}
24673fd0a558SYan, Zheng 
24683fd0a558SYan, Zheng 			upper->locked = 1;
24693fd0a558SYan, Zheng 			path->locks[upper->level] = 0;
24703fd0a558SYan, Zheng 
24715d4f98a2SYan Zheng 			slot = path->slots[upper->level];
2472b3b4aa74SDavid Sterba 			btrfs_release_path(path);
24735d4f98a2SYan Zheng 		} else {
2474fdf8d595SAnand Jain 			ret = btrfs_bin_search(upper->eb, 0, key, &slot);
24758df01fddSNikolay Borisov 			if (ret < 0)
2476cbca7d59SFilipe Manana 				goto next;
24775d4f98a2SYan Zheng 			BUG_ON(ret);
24785d4f98a2SYan Zheng 		}
24795d4f98a2SYan Zheng 
24805d4f98a2SYan Zheng 		bytenr = btrfs_node_blockptr(upper->eb, slot);
24813fd0a558SYan, Zheng 		if (lowest) {
24824547f4d8SLiu Bo 			if (bytenr != node->bytenr) {
24834547f4d8SLiu Bo 				btrfs_err(root->fs_info,
24844547f4d8SLiu Bo 		"lowest leaf/node mismatch: bytenr %llu node->bytenr %llu slot %d upper %llu",
24854547f4d8SLiu Bo 					  bytenr, node->bytenr, slot,
24864547f4d8SLiu Bo 					  upper->eb->start);
24878df01fddSNikolay Borisov 				ret = -EIO;
24884547f4d8SLiu Bo 				goto next;
24894547f4d8SLiu Bo 			}
24905d4f98a2SYan Zheng 		} else {
24913fd0a558SYan, Zheng 			if (node->eb->start == bytenr)
24923fd0a558SYan, Zheng 				goto next;
24935d4f98a2SYan Zheng 		}
24945d4f98a2SYan Zheng 
2495da17066cSJeff Mahoney 		blocksize = root->fs_info->nodesize;
2496c9752536SJosef Bacik 		eb = btrfs_read_node_slot(upper->eb, slot);
249764c043deSLiu Bo 		if (IS_ERR(eb)) {
24988df01fddSNikolay Borisov 			ret = PTR_ERR(eb);
249964c043deSLiu Bo 			goto next;
250097d9a8a4STsutomu Itoh 		}
25015d4f98a2SYan Zheng 		btrfs_tree_lock(eb);
25025d4f98a2SYan Zheng 
25035d4f98a2SYan Zheng 		if (!node->eb) {
25045d4f98a2SYan Zheng 			ret = btrfs_cow_block(trans, root, eb, upper->eb,
25059631e4ccSJosef Bacik 					      slot, &eb, BTRFS_NESTING_COW);
25063fd0a558SYan, Zheng 			btrfs_tree_unlock(eb);
25073fd0a558SYan, Zheng 			free_extent_buffer(eb);
25088df01fddSNikolay Borisov 			if (ret < 0)
25093fd0a558SYan, Zheng 				goto next;
2510ffe30dd8SJosef Bacik 			/*
2511ffe30dd8SJosef Bacik 			 * We've just COWed this block, it should have updated
2512ffe30dd8SJosef Bacik 			 * the correct backref node entry.
2513ffe30dd8SJosef Bacik 			 */
2514ffe30dd8SJosef Bacik 			ASSERT(node->eb == eb);
25155d4f98a2SYan Zheng 		} else {
25165d4f98a2SYan Zheng 			btrfs_set_node_blockptr(upper->eb, slot,
25175d4f98a2SYan Zheng 						node->eb->start);
25185d4f98a2SYan Zheng 			btrfs_set_node_ptr_generation(upper->eb, slot,
25195d4f98a2SYan Zheng 						      trans->transid);
2520*d5e09e38SFilipe Manana 			btrfs_mark_buffer_dirty(trans, upper->eb);
25215d4f98a2SYan Zheng 
252282fa113fSQu Wenruo 			btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF,
25235d4f98a2SYan Zheng 					       node->eb->start, blocksize,
252482fa113fSQu Wenruo 					       upper->eb->start);
252582fa113fSQu Wenruo 			btrfs_init_tree_ref(&ref, node->level,
2526f42c5da6SNikolay Borisov 					    btrfs_header_owner(upper->eb),
2527f42c5da6SNikolay Borisov 					    root->root_key.objectid, false);
252882fa113fSQu Wenruo 			ret = btrfs_inc_extent_ref(trans, &ref);
2529eb6b7fb4SJosef Bacik 			if (!ret)
2530eb6b7fb4SJosef Bacik 				ret = btrfs_drop_subtree(trans, root, eb,
2531eb6b7fb4SJosef Bacik 							 upper->eb);
2532eb6b7fb4SJosef Bacik 			if (ret)
2533eb6b7fb4SJosef Bacik 				btrfs_abort_transaction(trans, ret);
25345d4f98a2SYan Zheng 		}
25353fd0a558SYan, Zheng next:
25363fd0a558SYan, Zheng 		if (!upper->pending)
2537b0fe7078SQu Wenruo 			btrfs_backref_drop_node_buffer(upper);
25383fd0a558SYan, Zheng 		else
2539b0fe7078SQu Wenruo 			btrfs_backref_unlock_node_buffer(upper);
25408df01fddSNikolay Borisov 		if (ret)
25413fd0a558SYan, Zheng 			break;
25425d4f98a2SYan Zheng 	}
25433fd0a558SYan, Zheng 
25448df01fddSNikolay Borisov 	if (!ret && node->pending) {
2545b0fe7078SQu Wenruo 		btrfs_backref_drop_node_buffer(node);
25463fd0a558SYan, Zheng 		list_move_tail(&node->list, &rc->backref_cache.changed);
25473fd0a558SYan, Zheng 		node->pending = 0;
25485d4f98a2SYan Zheng 	}
25493fd0a558SYan, Zheng 
25505d4f98a2SYan Zheng 	path->lowest_level = 0;
2551ffe30dd8SJosef Bacik 
2552ffe30dd8SJosef Bacik 	/*
2553ffe30dd8SJosef Bacik 	 * We should have allocated all of our space in the block rsv and thus
2554ffe30dd8SJosef Bacik 	 * shouldn't ENOSPC.
2555ffe30dd8SJosef Bacik 	 */
2556ffe30dd8SJosef Bacik 	ASSERT(ret != -ENOSPC);
25578df01fddSNikolay Borisov 	return ret;
25585d4f98a2SYan Zheng }
25595d4f98a2SYan Zheng 
link_to_upper(struct btrfs_trans_handle * trans,struct reloc_control * rc,struct btrfs_backref_node * node,struct btrfs_path * path)25605d4f98a2SYan Zheng static int link_to_upper(struct btrfs_trans_handle *trans,
25613fd0a558SYan, Zheng 			 struct reloc_control *rc,
2562a26195a5SQu Wenruo 			 struct btrfs_backref_node *node,
25635d4f98a2SYan Zheng 			 struct btrfs_path *path)
25645d4f98a2SYan Zheng {
25655d4f98a2SYan Zheng 	struct btrfs_key key;
25665d4f98a2SYan Zheng 
25675d4f98a2SYan Zheng 	btrfs_node_key_to_cpu(node->eb, &key, 0);
25683fd0a558SYan, Zheng 	return do_relocation(trans, rc, node, &key, path, 0);
25695d4f98a2SYan Zheng }
25705d4f98a2SYan Zheng 
finish_pending_nodes(struct btrfs_trans_handle * trans,struct reloc_control * rc,struct btrfs_path * path,int err)25715d4f98a2SYan Zheng static int finish_pending_nodes(struct btrfs_trans_handle *trans,
25723fd0a558SYan, Zheng 				struct reloc_control *rc,
25733fd0a558SYan, Zheng 				struct btrfs_path *path, int err)
25745d4f98a2SYan Zheng {
25753fd0a558SYan, Zheng 	LIST_HEAD(list);
2576a26195a5SQu Wenruo 	struct btrfs_backref_cache *cache = &rc->backref_cache;
2577a26195a5SQu Wenruo 	struct btrfs_backref_node *node;
25785d4f98a2SYan Zheng 	int level;
25795d4f98a2SYan Zheng 	int ret;
25805d4f98a2SYan Zheng 
25815d4f98a2SYan Zheng 	for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
25825d4f98a2SYan Zheng 		while (!list_empty(&cache->pending[level])) {
25835d4f98a2SYan Zheng 			node = list_entry(cache->pending[level].next,
2584a26195a5SQu Wenruo 					  struct btrfs_backref_node, list);
25853fd0a558SYan, Zheng 			list_move_tail(&node->list, &list);
25863fd0a558SYan, Zheng 			BUG_ON(!node->pending);
25875d4f98a2SYan Zheng 
25883fd0a558SYan, Zheng 			if (!err) {
25893fd0a558SYan, Zheng 				ret = link_to_upper(trans, rc, node, path);
25905d4f98a2SYan Zheng 				if (ret < 0)
25915d4f98a2SYan Zheng 					err = ret;
25925d4f98a2SYan Zheng 			}
25935d4f98a2SYan Zheng 		}
25943fd0a558SYan, Zheng 		list_splice_init(&list, &cache->pending[level]);
25953fd0a558SYan, Zheng 	}
25965d4f98a2SYan Zheng 	return err;
25975d4f98a2SYan Zheng }
25985d4f98a2SYan Zheng 
25995d4f98a2SYan Zheng /*
26005d4f98a2SYan Zheng  * mark a block and all blocks directly/indirectly reference the block
26015d4f98a2SYan Zheng  * as processed.
26025d4f98a2SYan Zheng  */
update_processed_blocks(struct reloc_control * rc,struct btrfs_backref_node * node)26035d4f98a2SYan Zheng static void update_processed_blocks(struct reloc_control *rc,
2604a26195a5SQu Wenruo 				    struct btrfs_backref_node *node)
26055d4f98a2SYan Zheng {
2606a26195a5SQu Wenruo 	struct btrfs_backref_node *next = node;
2607a26195a5SQu Wenruo 	struct btrfs_backref_edge *edge;
2608a26195a5SQu Wenruo 	struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
26095d4f98a2SYan Zheng 	int index = 0;
26105d4f98a2SYan Zheng 
26115d4f98a2SYan Zheng 	while (next) {
26125d4f98a2SYan Zheng 		cond_resched();
26135d4f98a2SYan Zheng 		while (1) {
26145d4f98a2SYan Zheng 			if (next->processed)
26155d4f98a2SYan Zheng 				break;
26165d4f98a2SYan Zheng 
26179569cc20SQu Wenruo 			mark_block_processed(rc, next);
26185d4f98a2SYan Zheng 
26195d4f98a2SYan Zheng 			if (list_empty(&next->upper))
26205d4f98a2SYan Zheng 				break;
26215d4f98a2SYan Zheng 
26225d4f98a2SYan Zheng 			edge = list_entry(next->upper.next,
2623a26195a5SQu Wenruo 					struct btrfs_backref_edge, list[LOWER]);
26245d4f98a2SYan Zheng 			edges[index++] = edge;
26255d4f98a2SYan Zheng 			next = edge->node[UPPER];
26265d4f98a2SYan Zheng 		}
26275d4f98a2SYan Zheng 		next = walk_down_backref(edges, &index);
26285d4f98a2SYan Zheng 	}
26295d4f98a2SYan Zheng }
26305d4f98a2SYan Zheng 
tree_block_processed(u64 bytenr,struct reloc_control * rc)26317476dfdaSDavid Sterba static int tree_block_processed(u64 bytenr, struct reloc_control *rc)
26325d4f98a2SYan Zheng {
2633da17066cSJeff Mahoney 	u32 blocksize = rc->extent_root->fs_info->nodesize;
26347476dfdaSDavid Sterba 
26355d4f98a2SYan Zheng 	if (test_range_bit(&rc->processed_blocks, bytenr,
26369655d298SChris Mason 			   bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL))
26375d4f98a2SYan Zheng 		return 1;
26385d4f98a2SYan Zheng 	return 0;
26395d4f98a2SYan Zheng }
26405d4f98a2SYan Zheng 
get_tree_block_key(struct btrfs_fs_info * fs_info,struct tree_block * block)26412ff7e61eSJeff Mahoney static int get_tree_block_key(struct btrfs_fs_info *fs_info,
26425d4f98a2SYan Zheng 			      struct tree_block *block)
26435d4f98a2SYan Zheng {
2644789d6a3aSQu Wenruo 	struct btrfs_tree_parent_check check = {
2645789d6a3aSQu Wenruo 		.level = block->level,
2646789d6a3aSQu Wenruo 		.owner_root = block->owner,
2647789d6a3aSQu Wenruo 		.transid = block->key.offset
2648789d6a3aSQu Wenruo 	};
26495d4f98a2SYan Zheng 	struct extent_buffer *eb;
26505d4f98a2SYan Zheng 
2651789d6a3aSQu Wenruo 	eb = read_tree_block(fs_info, block->bytenr, &check);
26524eb150d6SQu Wenruo 	if (IS_ERR(eb))
265364c043deSLiu Bo 		return PTR_ERR(eb);
26544eb150d6SQu Wenruo 	if (!extent_buffer_uptodate(eb)) {
2655416bc658SJosef Bacik 		free_extent_buffer(eb);
2656416bc658SJosef Bacik 		return -EIO;
2657416bc658SJosef Bacik 	}
26585d4f98a2SYan Zheng 	if (block->level == 0)
26595d4f98a2SYan Zheng 		btrfs_item_key_to_cpu(eb, &block->key, 0);
26605d4f98a2SYan Zheng 	else
26615d4f98a2SYan Zheng 		btrfs_node_key_to_cpu(eb, &block->key, 0);
26625d4f98a2SYan Zheng 	free_extent_buffer(eb);
26635d4f98a2SYan Zheng 	block->key_ready = 1;
26645d4f98a2SYan Zheng 	return 0;
26655d4f98a2SYan Zheng }
26665d4f98a2SYan Zheng 
26675d4f98a2SYan Zheng /*
26685d4f98a2SYan Zheng  * helper function to relocate a tree block
26695d4f98a2SYan Zheng  */
relocate_tree_block(struct btrfs_trans_handle * trans,struct reloc_control * rc,struct btrfs_backref_node * node,struct btrfs_key * key,struct btrfs_path * path)26705d4f98a2SYan Zheng static int relocate_tree_block(struct btrfs_trans_handle *trans,
26715d4f98a2SYan Zheng 				struct reloc_control *rc,
2672a26195a5SQu Wenruo 				struct btrfs_backref_node *node,
26735d4f98a2SYan Zheng 				struct btrfs_key *key,
26745d4f98a2SYan Zheng 				struct btrfs_path *path)
26755d4f98a2SYan Zheng {
26765d4f98a2SYan Zheng 	struct btrfs_root *root;
26773fd0a558SYan, Zheng 	int ret = 0;
26785d4f98a2SYan Zheng 
26793fd0a558SYan, Zheng 	if (!node)
26805d4f98a2SYan Zheng 		return 0;
26813fd0a558SYan, Zheng 
26825f6b2e5cSJosef Bacik 	/*
26835f6b2e5cSJosef Bacik 	 * If we fail here we want to drop our backref_node because we are going
26845f6b2e5cSJosef Bacik 	 * to start over and regenerate the tree for it.
26855f6b2e5cSJosef Bacik 	 */
26865f6b2e5cSJosef Bacik 	ret = reserve_metadata_space(trans, rc, node);
26875f6b2e5cSJosef Bacik 	if (ret)
26885f6b2e5cSJosef Bacik 		goto out;
26895f6b2e5cSJosef Bacik 
26903fd0a558SYan, Zheng 	BUG_ON(node->processed);
2691147d256eSZhaolei 	root = select_one_root(node);
26928717cf44SJosef Bacik 	if (IS_ERR(root)) {
26938717cf44SJosef Bacik 		ret = PTR_ERR(root);
26948717cf44SJosef Bacik 
26958717cf44SJosef Bacik 		/* See explanation in select_one_root for the -EUCLEAN case. */
26968717cf44SJosef Bacik 		ASSERT(ret == -ENOENT);
26978717cf44SJosef Bacik 		if (ret == -ENOENT) {
26988717cf44SJosef Bacik 			ret = 0;
26993fd0a558SYan, Zheng 			update_processed_blocks(rc, node);
27008717cf44SJosef Bacik 		}
27013fd0a558SYan, Zheng 		goto out;
27025d4f98a2SYan Zheng 	}
27035d4f98a2SYan Zheng 
27043fd0a558SYan, Zheng 	if (root) {
270592a7cc42SQu Wenruo 		if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
27061c7bfa15SJosef Bacik 			/*
27071c7bfa15SJosef Bacik 			 * This block was the root block of a root, and this is
27081c7bfa15SJosef Bacik 			 * the first time we're processing the block and thus it
27091c7bfa15SJosef Bacik 			 * should not have had the ->new_bytenr modified and
27101c7bfa15SJosef Bacik 			 * should have not been included on the changed list.
27111c7bfa15SJosef Bacik 			 *
27121c7bfa15SJosef Bacik 			 * However in the case of corruption we could have
27131c7bfa15SJosef Bacik 			 * multiple refs pointing to the same block improperly,
27141c7bfa15SJosef Bacik 			 * and thus we would trip over these checks.  ASSERT()
27151c7bfa15SJosef Bacik 			 * for the developer case, because it could indicate a
27161c7bfa15SJosef Bacik 			 * bug in the backref code, however error out for a
27171c7bfa15SJosef Bacik 			 * normal user in the case of corruption.
27181c7bfa15SJosef Bacik 			 */
27191c7bfa15SJosef Bacik 			ASSERT(node->new_bytenr == 0);
27201c7bfa15SJosef Bacik 			ASSERT(list_empty(&node->list));
27211c7bfa15SJosef Bacik 			if (node->new_bytenr || !list_empty(&node->list)) {
27221c7bfa15SJosef Bacik 				btrfs_err(root->fs_info,
27231c7bfa15SJosef Bacik 				  "bytenr %llu has improper references to it",
27241c7bfa15SJosef Bacik 					  node->bytenr);
27251c7bfa15SJosef Bacik 				ret = -EUCLEAN;
27261c7bfa15SJosef Bacik 				goto out;
27271c7bfa15SJosef Bacik 			}
2728d18c7bd9SJosef Bacik 			ret = btrfs_record_root_in_trans(trans, root);
2729d18c7bd9SJosef Bacik 			if (ret)
2730d18c7bd9SJosef Bacik 				goto out;
273139200e59SJosef Bacik 			/*
273239200e59SJosef Bacik 			 * Another thread could have failed, need to check if we
273339200e59SJosef Bacik 			 * have reloc_root actually set.
273439200e59SJosef Bacik 			 */
273539200e59SJosef Bacik 			if (!root->reloc_root) {
273639200e59SJosef Bacik 				ret = -ENOENT;
273739200e59SJosef Bacik 				goto out;
273839200e59SJosef Bacik 			}
27393fd0a558SYan, Zheng 			root = root->reloc_root;
27403fd0a558SYan, Zheng 			node->new_bytenr = root->node->start;
274100246528SJosef Bacik 			btrfs_put_root(node->root);
274200246528SJosef Bacik 			node->root = btrfs_grab_root(root);
27430b530bc5SJosef Bacik 			ASSERT(node->root);
27443fd0a558SYan, Zheng 			list_add_tail(&node->list, &rc->backref_cache.changed);
27453fd0a558SYan, Zheng 		} else {
27465d4f98a2SYan Zheng 			path->lowest_level = node->level;
27472bb2e00eSFilipe Manana 			if (root == root->fs_info->chunk_root)
27482bb2e00eSFilipe Manana 				btrfs_reserve_chunk_metadata(trans, false);
27495d4f98a2SYan Zheng 			ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2750b3b4aa74SDavid Sterba 			btrfs_release_path(path);
27512bb2e00eSFilipe Manana 			if (root == root->fs_info->chunk_root)
27522bb2e00eSFilipe Manana 				btrfs_trans_release_chunk_metadata(trans);
27533fd0a558SYan, Zheng 			if (ret > 0)
27545d4f98a2SYan Zheng 				ret = 0;
27553fd0a558SYan, Zheng 		}
27563fd0a558SYan, Zheng 		if (!ret)
27573fd0a558SYan, Zheng 			update_processed_blocks(rc, node);
27583fd0a558SYan, Zheng 	} else {
27593fd0a558SYan, Zheng 		ret = do_relocation(trans, rc, node, key, path, 1);
27603fd0a558SYan, Zheng 	}
27615d4f98a2SYan Zheng out:
27620647bf56SWang Shilong 	if (ret || node->level == 0 || node->cowonly)
2763023acb07SQu Wenruo 		btrfs_backref_cleanup_node(&rc->backref_cache, node);
27645d4f98a2SYan Zheng 	return ret;
27655d4f98a2SYan Zheng }
27665d4f98a2SYan Zheng 
27675d4f98a2SYan Zheng /*
27685d4f98a2SYan Zheng  * relocate a list of blocks
27695d4f98a2SYan Zheng  */
27705d4f98a2SYan Zheng static noinline_for_stack
relocate_tree_blocks(struct btrfs_trans_handle * trans,struct reloc_control * rc,struct rb_root * blocks)27715d4f98a2SYan Zheng int relocate_tree_blocks(struct btrfs_trans_handle *trans,
27725d4f98a2SYan Zheng 			 struct reloc_control *rc, struct rb_root *blocks)
27735d4f98a2SYan Zheng {
27742ff7e61eSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2775a26195a5SQu Wenruo 	struct btrfs_backref_node *node;
27765d4f98a2SYan Zheng 	struct btrfs_path *path;
27775d4f98a2SYan Zheng 	struct tree_block *block;
277898ff7b94SQu Wenruo 	struct tree_block *next;
27795d4f98a2SYan Zheng 	int ret;
27805d4f98a2SYan Zheng 	int err = 0;
27815d4f98a2SYan Zheng 
27825d4f98a2SYan Zheng 	path = btrfs_alloc_path();
2783e1a12670SLiu Bo 	if (!path) {
2784e1a12670SLiu Bo 		err = -ENOMEM;
278534c2b290SDavid Sterba 		goto out_free_blocks;
2786e1a12670SLiu Bo 	}
27875d4f98a2SYan Zheng 
278898ff7b94SQu Wenruo 	/* Kick in readahead for tree blocks with missing keys */
278998ff7b94SQu Wenruo 	rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
27905d4f98a2SYan Zheng 		if (!block->key_ready)
2791f7ba2d37SJosef Bacik 			btrfs_readahead_tree_block(fs_info, block->bytenr,
2792f7ba2d37SJosef Bacik 						   block->owner, 0,
27933fbaf258SJosef Bacik 						   block->level);
27945d4f98a2SYan Zheng 	}
27955d4f98a2SYan Zheng 
279698ff7b94SQu Wenruo 	/* Get first keys */
279798ff7b94SQu Wenruo 	rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
279834c2b290SDavid Sterba 		if (!block->key_ready) {
27992ff7e61eSJeff Mahoney 			err = get_tree_block_key(fs_info, block);
280034c2b290SDavid Sterba 			if (err)
280134c2b290SDavid Sterba 				goto out_free_path;
280234c2b290SDavid Sterba 		}
28035d4f98a2SYan Zheng 	}
28045d4f98a2SYan Zheng 
280598ff7b94SQu Wenruo 	/* Do tree relocation */
280698ff7b94SQu Wenruo 	rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
2807eb96e221SFilipe Manana 		node = build_backref_tree(trans, rc, &block->key,
28085d4f98a2SYan Zheng 					  block->level, block->bytenr);
28095d4f98a2SYan Zheng 		if (IS_ERR(node)) {
28105d4f98a2SYan Zheng 			err = PTR_ERR(node);
28115d4f98a2SYan Zheng 			goto out;
28125d4f98a2SYan Zheng 		}
28135d4f98a2SYan Zheng 
28145d4f98a2SYan Zheng 		ret = relocate_tree_block(trans, rc, node, &block->key,
28155d4f98a2SYan Zheng 					  path);
28165d4f98a2SYan Zheng 		if (ret < 0) {
28175d4f98a2SYan Zheng 			err = ret;
281850dbbb71SJosef Bacik 			break;
28195d4f98a2SYan Zheng 		}
28205d4f98a2SYan Zheng 	}
28215d4f98a2SYan Zheng out:
28223fd0a558SYan, Zheng 	err = finish_pending_nodes(trans, rc, path, err);
28235d4f98a2SYan Zheng 
282434c2b290SDavid Sterba out_free_path:
28255d4f98a2SYan Zheng 	btrfs_free_path(path);
282634c2b290SDavid Sterba out_free_blocks:
2827e1a12670SLiu Bo 	free_block_list(blocks);
28285d4f98a2SYan Zheng 	return err;
28295d4f98a2SYan Zheng }
28305d4f98a2SYan Zheng 
prealloc_file_extent_cluster(struct btrfs_inode * inode,struct file_extent_cluster * cluster)2831056d9becSNikolay Borisov static noinline_for_stack int prealloc_file_extent_cluster(
2832056d9becSNikolay Borisov 				struct btrfs_inode *inode,
2833efa56464SYan, Zheng 				struct file_extent_cluster *cluster)
2834efa56464SYan, Zheng {
2835efa56464SYan, Zheng 	u64 alloc_hint = 0;
2836efa56464SYan, Zheng 	u64 start;
2837efa56464SYan, Zheng 	u64 end;
2838056d9becSNikolay Borisov 	u64 offset = inode->index_cnt;
2839efa56464SYan, Zheng 	u64 num_bytes;
28404e9d0d01SNikolay Borisov 	int nr;
2841efa56464SYan, Zheng 	int ret = 0;
28429d9ea1e6SQu Wenruo 	u64 i_size = i_size_read(&inode->vfs_inode);
2843dcb40c19SWang Xiaoguang 	u64 prealloc_start = cluster->start - offset;
2844dcb40c19SWang Xiaoguang 	u64 prealloc_end = cluster->end - offset;
2845214e61d0SNikolay Borisov 	u64 cur_offset = prealloc_start;
2846efa56464SYan, Zheng 
28479d9ea1e6SQu Wenruo 	/*
28489d9ea1e6SQu Wenruo 	 * For subpage case, previous i_size may not be aligned to PAGE_SIZE.
28499d9ea1e6SQu Wenruo 	 * This means the range [i_size, PAGE_END + 1) is filled with zeros by
28509d9ea1e6SQu Wenruo 	 * btrfs_do_readpage() call of previously relocated file cluster.
28519d9ea1e6SQu Wenruo 	 *
28529d9ea1e6SQu Wenruo 	 * If the current cluster starts in the above range, btrfs_do_readpage()
28539d9ea1e6SQu Wenruo 	 * will skip the read, and relocate_one_page() will later writeback
28549d9ea1e6SQu Wenruo 	 * the padding zeros as new data, causing data corruption.
28559d9ea1e6SQu Wenruo 	 *
28569d9ea1e6SQu Wenruo 	 * Here we have to manually invalidate the range (i_size, PAGE_END + 1).
28579d9ea1e6SQu Wenruo 	 */
2858ce394a7fSYushan Zhou 	if (!PAGE_ALIGNED(i_size)) {
28599d9ea1e6SQu Wenruo 		struct address_space *mapping = inode->vfs_inode.i_mapping;
28609d9ea1e6SQu Wenruo 		struct btrfs_fs_info *fs_info = inode->root->fs_info;
28619d9ea1e6SQu Wenruo 		const u32 sectorsize = fs_info->sectorsize;
28629d9ea1e6SQu Wenruo 		struct page *page;
28639d9ea1e6SQu Wenruo 
28649d9ea1e6SQu Wenruo 		ASSERT(sectorsize < PAGE_SIZE);
28659d9ea1e6SQu Wenruo 		ASSERT(IS_ALIGNED(i_size, sectorsize));
28669d9ea1e6SQu Wenruo 
28679d9ea1e6SQu Wenruo 		/*
28689d9ea1e6SQu Wenruo 		 * Subpage can't handle page with DIRTY but without UPTODATE
28699d9ea1e6SQu Wenruo 		 * bit as it can lead to the following deadlock:
28709d9ea1e6SQu Wenruo 		 *
2871fb12489bSMatthew Wilcox (Oracle) 		 * btrfs_read_folio()
28729d9ea1e6SQu Wenruo 		 * | Page already *locked*
28739d9ea1e6SQu Wenruo 		 * |- btrfs_lock_and_flush_ordered_range()
28749d9ea1e6SQu Wenruo 		 *    |- btrfs_start_ordered_extent()
28759d9ea1e6SQu Wenruo 		 *       |- extent_write_cache_pages()
28769d9ea1e6SQu Wenruo 		 *          |- lock_page()
28779d9ea1e6SQu Wenruo 		 *             We try to lock the page we already hold.
28789d9ea1e6SQu Wenruo 		 *
28799d9ea1e6SQu Wenruo 		 * Here we just writeback the whole data reloc inode, so that
28809d9ea1e6SQu Wenruo 		 * we will be ensured to have no dirty range in the page, and
28819d9ea1e6SQu Wenruo 		 * are safe to clear the uptodate bits.
28829d9ea1e6SQu Wenruo 		 *
28839d9ea1e6SQu Wenruo 		 * This shouldn't cause too much overhead, as we need to write
28849d9ea1e6SQu Wenruo 		 * the data back anyway.
28859d9ea1e6SQu Wenruo 		 */
28869d9ea1e6SQu Wenruo 		ret = filemap_write_and_wait(mapping);
28879d9ea1e6SQu Wenruo 		if (ret < 0)
28889d9ea1e6SQu Wenruo 			return ret;
28899d9ea1e6SQu Wenruo 
28909d9ea1e6SQu Wenruo 		clear_extent_bits(&inode->io_tree, i_size,
28919d9ea1e6SQu Wenruo 				  round_up(i_size, PAGE_SIZE) - 1,
28929d9ea1e6SQu Wenruo 				  EXTENT_UPTODATE);
28939d9ea1e6SQu Wenruo 		page = find_lock_page(mapping, i_size >> PAGE_SHIFT);
28949d9ea1e6SQu Wenruo 		/*
28959d9ea1e6SQu Wenruo 		 * If page is freed we don't need to do anything then, as we
28969d9ea1e6SQu Wenruo 		 * will re-read the whole page anyway.
28979d9ea1e6SQu Wenruo 		 */
28989d9ea1e6SQu Wenruo 		if (page) {
28999d9ea1e6SQu Wenruo 			btrfs_subpage_clear_uptodate(fs_info, page, i_size,
29009d9ea1e6SQu Wenruo 					round_up(i_size, PAGE_SIZE) - i_size);
29019d9ea1e6SQu Wenruo 			unlock_page(page);
29029d9ea1e6SQu Wenruo 			put_page(page);
29039d9ea1e6SQu Wenruo 		}
29049d9ea1e6SQu Wenruo 	}
29059d9ea1e6SQu Wenruo 
2906efa56464SYan, Zheng 	BUG_ON(cluster->start != cluster->boundary[0]);
2907056d9becSNikolay Borisov 	ret = btrfs_alloc_data_chunk_ondemand(inode,
2908dcb40c19SWang Xiaoguang 					      prealloc_end + 1 - prealloc_start);
2909efa56464SYan, Zheng 	if (ret)
2910214e61d0SNikolay Borisov 		return ret;
2911efa56464SYan, Zheng 
291229b6352bSDavid Sterba 	btrfs_inode_lock(inode, 0);
29134e9d0d01SNikolay Borisov 	for (nr = 0; nr < cluster->nr; nr++) {
29149c5c9604SJosef Bacik 		struct extent_state *cached_state = NULL;
29159c5c9604SJosef Bacik 
2916efa56464SYan, Zheng 		start = cluster->boundary[nr] - offset;
2917efa56464SYan, Zheng 		if (nr + 1 < cluster->nr)
2918efa56464SYan, Zheng 			end = cluster->boundary[nr + 1] - 1 - offset;
2919efa56464SYan, Zheng 		else
2920efa56464SYan, Zheng 			end = cluster->end - offset;
2921efa56464SYan, Zheng 
29229c5c9604SJosef Bacik 		lock_extent(&inode->io_tree, start, end, &cached_state);
2923efa56464SYan, Zheng 		num_bytes = end + 1 - start;
2924056d9becSNikolay Borisov 		ret = btrfs_prealloc_file_range(&inode->vfs_inode, 0, start,
2925efa56464SYan, Zheng 						num_bytes, num_bytes,
2926efa56464SYan, Zheng 						end + 1, &alloc_hint);
292718513091SWang Xiaoguang 		cur_offset = end + 1;
29289c5c9604SJosef Bacik 		unlock_extent(&inode->io_tree, start, end, &cached_state);
2929efa56464SYan, Zheng 		if (ret)
2930efa56464SYan, Zheng 			break;
2931efa56464SYan, Zheng 	}
2932e5d4d75bSDavid Sterba 	btrfs_inode_unlock(inode, 0);
2933214e61d0SNikolay Borisov 
293418513091SWang Xiaoguang 	if (cur_offset < prealloc_end)
2935056d9becSNikolay Borisov 		btrfs_free_reserved_data_space_noquota(inode->root->fs_info,
2936a89ef455SFilipe Manana 					       prealloc_end + 1 - cur_offset);
2937efa56464SYan, Zheng 	return ret;
2938efa56464SYan, Zheng }
2939efa56464SYan, Zheng 
setup_relocation_extent_mapping(struct inode * inode,u64 start,u64 end,u64 block_start)29404b01c44fSJohannes Thumshirn static noinline_for_stack int setup_relocation_extent_mapping(struct inode *inode,
29414b01c44fSJohannes Thumshirn 				u64 start, u64 end, u64 block_start)
29425d4f98a2SYan Zheng {
29435d4f98a2SYan Zheng 	struct extent_map *em;
29449c5c9604SJosef Bacik 	struct extent_state *cached_state = NULL;
29450257bb82SYan, Zheng 	int ret = 0;
29465d4f98a2SYan Zheng 
2947172ddd60SDavid Sterba 	em = alloc_extent_map();
29480257bb82SYan, Zheng 	if (!em)
29490257bb82SYan, Zheng 		return -ENOMEM;
29500257bb82SYan, Zheng 
29515d4f98a2SYan Zheng 	em->start = start;
29520257bb82SYan, Zheng 	em->len = end + 1 - start;
29530257bb82SYan, Zheng 	em->block_len = em->len;
29540257bb82SYan, Zheng 	em->block_start = block_start;
29555d4f98a2SYan Zheng 	set_bit(EXTENT_FLAG_PINNED, &em->flags);
29565d4f98a2SYan Zheng 
29579c5c9604SJosef Bacik 	lock_extent(&BTRFS_I(inode)->io_tree, start, end, &cached_state);
2958a1ba4c08SFilipe Manana 	ret = btrfs_replace_extent_map_range(BTRFS_I(inode), em, false);
29599c5c9604SJosef Bacik 	unlock_extent(&BTRFS_I(inode)->io_tree, start, end, &cached_state);
2960a1ba4c08SFilipe Manana 	free_extent_map(em);
2961a1ba4c08SFilipe Manana 
29620257bb82SYan, Zheng 	return ret;
29630257bb82SYan, Zheng }
29645d4f98a2SYan Zheng 
2965726a3421SQu Wenruo /*
2966907d2710SDavid Sterba  * Allow error injection to test balance/relocation cancellation
2967726a3421SQu Wenruo  */
btrfs_should_cancel_balance(struct btrfs_fs_info * fs_info)29681fec12a5SJosef Bacik noinline int btrfs_should_cancel_balance(struct btrfs_fs_info *fs_info)
2969726a3421SQu Wenruo {
29705cb502f4SQu Wenruo 	return atomic_read(&fs_info->balance_cancel_req) ||
2971907d2710SDavid Sterba 		atomic_read(&fs_info->reloc_cancel_req) ||
29725cb502f4SQu Wenruo 		fatal_signal_pending(current);
2973726a3421SQu Wenruo }
2974726a3421SQu Wenruo ALLOW_ERROR_INJECTION(btrfs_should_cancel_balance, TRUE);
2975726a3421SQu Wenruo 
get_cluster_boundary_end(struct file_extent_cluster * cluster,int cluster_nr)2976c2832898SQu Wenruo static u64 get_cluster_boundary_end(struct file_extent_cluster *cluster,
2977c2832898SQu Wenruo 				    int cluster_nr)
2978c2832898SQu Wenruo {
2979c2832898SQu Wenruo 	/* Last extent, use cluster end directly */
2980c2832898SQu Wenruo 	if (cluster_nr >= cluster->nr - 1)
2981c2832898SQu Wenruo 		return cluster->end;
2982c2832898SQu Wenruo 
2983c2832898SQu Wenruo 	/* Use next boundary start*/
2984c2832898SQu Wenruo 	return cluster->boundary[cluster_nr + 1] - 1;
2985c2832898SQu Wenruo }
2986c2832898SQu Wenruo 
relocate_one_page(struct inode * inode,struct file_ra_state * ra,struct file_extent_cluster * cluster,int * cluster_nr,unsigned long page_index)2987f47960f4SQu Wenruo static int relocate_one_page(struct inode *inode, struct file_ra_state *ra,
2988f47960f4SQu Wenruo 			     struct file_extent_cluster *cluster,
2989f47960f4SQu Wenruo 			     int *cluster_nr, unsigned long page_index)
2990f47960f4SQu Wenruo {
2991f47960f4SQu Wenruo 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2992f47960f4SQu Wenruo 	u64 offset = BTRFS_I(inode)->index_cnt;
2993f47960f4SQu Wenruo 	const unsigned long last_index = (cluster->end - offset) >> PAGE_SHIFT;
2994f47960f4SQu Wenruo 	gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
2995f47960f4SQu Wenruo 	struct page *page;
2996f47960f4SQu Wenruo 	u64 page_start;
2997f47960f4SQu Wenruo 	u64 page_end;
2998c2832898SQu Wenruo 	u64 cur;
2999f47960f4SQu Wenruo 	int ret;
3000f47960f4SQu Wenruo 
3001f47960f4SQu Wenruo 	ASSERT(page_index <= last_index);
3002f47960f4SQu Wenruo 	page = find_lock_page(inode->i_mapping, page_index);
3003f47960f4SQu Wenruo 	if (!page) {
3004f47960f4SQu Wenruo 		page_cache_sync_readahead(inode->i_mapping, ra, NULL,
3005f47960f4SQu Wenruo 				page_index, last_index + 1 - page_index);
3006f47960f4SQu Wenruo 		page = find_or_create_page(inode->i_mapping, page_index, mask);
3007c2832898SQu Wenruo 		if (!page)
3008c2832898SQu Wenruo 			return -ENOMEM;
3009f47960f4SQu Wenruo 	}
3010f47960f4SQu Wenruo 
3011f47960f4SQu Wenruo 	if (PageReadahead(page))
30122ebdd1dfSMatthew Wilcox (Oracle) 		page_cache_async_readahead(inode->i_mapping, ra, NULL,
30132ebdd1dfSMatthew Wilcox (Oracle) 				page_folio(page), page_index,
30142ebdd1dfSMatthew Wilcox (Oracle) 				last_index + 1 - page_index);
3015f47960f4SQu Wenruo 
3016f47960f4SQu Wenruo 	if (!PageUptodate(page)) {
3017fb12489bSMatthew Wilcox (Oracle) 		btrfs_read_folio(NULL, page_folio(page));
3018f47960f4SQu Wenruo 		lock_page(page);
3019f47960f4SQu Wenruo 		if (!PageUptodate(page)) {
3020f47960f4SQu Wenruo 			ret = -EIO;
3021f47960f4SQu Wenruo 			goto release_page;
3022f47960f4SQu Wenruo 		}
3023f47960f4SQu Wenruo 	}
3024f47960f4SQu Wenruo 
3025e7f1326cSJosef Bacik 	/*
3026e7f1326cSJosef Bacik 	 * We could have lost page private when we dropped the lock to read the
3027e7f1326cSJosef Bacik 	 * page above, make sure we set_page_extent_mapped here so we have any
3028e7f1326cSJosef Bacik 	 * of the subpage blocksize stuff we need in place.
3029e7f1326cSJosef Bacik 	 */
3030e7f1326cSJosef Bacik 	ret = set_page_extent_mapped(page);
3031e7f1326cSJosef Bacik 	if (ret < 0)
3032e7f1326cSJosef Bacik 		goto release_page;
3033e7f1326cSJosef Bacik 
3034f47960f4SQu Wenruo 	page_start = page_offset(page);
3035f47960f4SQu Wenruo 	page_end = page_start + PAGE_SIZE - 1;
3036f47960f4SQu Wenruo 
3037c2832898SQu Wenruo 	/*
3038c2832898SQu Wenruo 	 * Start from the cluster, as for subpage case, the cluster can start
3039c2832898SQu Wenruo 	 * inside the page.
3040c2832898SQu Wenruo 	 */
3041c2832898SQu Wenruo 	cur = max(page_start, cluster->boundary[*cluster_nr] - offset);
3042c2832898SQu Wenruo 	while (cur <= page_end) {
30439c5c9604SJosef Bacik 		struct extent_state *cached_state = NULL;
3044c2832898SQu Wenruo 		u64 extent_start = cluster->boundary[*cluster_nr] - offset;
3045c2832898SQu Wenruo 		u64 extent_end = get_cluster_boundary_end(cluster,
3046c2832898SQu Wenruo 						*cluster_nr) - offset;
3047c2832898SQu Wenruo 		u64 clamped_start = max(page_start, extent_start);
3048c2832898SQu Wenruo 		u64 clamped_end = min(page_end, extent_end);
3049c2832898SQu Wenruo 		u32 clamped_len = clamped_end + 1 - clamped_start;
3050f47960f4SQu Wenruo 
3051c2832898SQu Wenruo 		/* Reserve metadata for this range */
3052c2832898SQu Wenruo 		ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode),
3053d4135134SFilipe Manana 						      clamped_len, clamped_len,
3054d4135134SFilipe Manana 						      false);
3055c2832898SQu Wenruo 		if (ret)
3056f47960f4SQu Wenruo 			goto release_page;
3057f47960f4SQu Wenruo 
3058c2832898SQu Wenruo 		/* Mark the range delalloc and dirty for later writeback */
30599c5c9604SJosef Bacik 		lock_extent(&BTRFS_I(inode)->io_tree, clamped_start, clamped_end,
30609c5c9604SJosef Bacik 			    &cached_state);
3061c2832898SQu Wenruo 		ret = btrfs_set_extent_delalloc(BTRFS_I(inode), clamped_start,
30629c5c9604SJosef Bacik 						clamped_end, 0, &cached_state);
3063c2832898SQu Wenruo 		if (ret) {
30649c5c9604SJosef Bacik 			clear_extent_bit(&BTRFS_I(inode)->io_tree,
3065c2832898SQu Wenruo 					 clamped_start, clamped_end,
30669c5c9604SJosef Bacik 					 EXTENT_LOCKED | EXTENT_BOUNDARY,
30679c5c9604SJosef Bacik 					 &cached_state);
3068c2832898SQu Wenruo 			btrfs_delalloc_release_metadata(BTRFS_I(inode),
3069c2832898SQu Wenruo 							clamped_len, true);
3070c2832898SQu Wenruo 			btrfs_delalloc_release_extents(BTRFS_I(inode),
3071c2832898SQu Wenruo 						       clamped_len);
3072c2832898SQu Wenruo 			goto release_page;
3073f47960f4SQu Wenruo 		}
3074c2832898SQu Wenruo 		btrfs_page_set_dirty(fs_info, page, clamped_start, clamped_len);
3075f47960f4SQu Wenruo 
3076c2832898SQu Wenruo 		/*
3077c2832898SQu Wenruo 		 * Set the boundary if it's inside the page.
3078c2832898SQu Wenruo 		 * Data relocation requires the destination extents to have the
3079c2832898SQu Wenruo 		 * same size as the source.
3080c2832898SQu Wenruo 		 * EXTENT_BOUNDARY bit prevents current extent from being merged
3081c2832898SQu Wenruo 		 * with previous extent.
3082c2832898SQu Wenruo 		 */
3083c2832898SQu Wenruo 		if (in_range(cluster->boundary[*cluster_nr] - offset,
3084c2832898SQu Wenruo 			     page_start, PAGE_SIZE)) {
3085c2832898SQu Wenruo 			u64 boundary_start = cluster->boundary[*cluster_nr] -
3086c2832898SQu Wenruo 						offset;
3087c2832898SQu Wenruo 			u64 boundary_end = boundary_start +
3088c2832898SQu Wenruo 					   fs_info->sectorsize - 1;
3089c2832898SQu Wenruo 
30900acd32c2SDavid Sterba 			set_extent_bit(&BTRFS_I(inode)->io_tree,
3091c2832898SQu Wenruo 				       boundary_start, boundary_end,
30921d126800SDavid Sterba 				       EXTENT_BOUNDARY, NULL);
3093c2832898SQu Wenruo 		}
30949c5c9604SJosef Bacik 		unlock_extent(&BTRFS_I(inode)->io_tree, clamped_start, clamped_end,
30959c5c9604SJosef Bacik 			      &cached_state);
3096c2832898SQu Wenruo 		btrfs_delalloc_release_extents(BTRFS_I(inode), clamped_len);
3097c2832898SQu Wenruo 		cur += clamped_len;
3098c2832898SQu Wenruo 
3099c2832898SQu Wenruo 		/* Crossed extent end, go to next extent */
3100c2832898SQu Wenruo 		if (cur >= extent_end) {
3101c2832898SQu Wenruo 			(*cluster_nr)++;
3102c2832898SQu Wenruo 			/* Just finished the last extent of the cluster, exit. */
3103c2832898SQu Wenruo 			if (*cluster_nr >= cluster->nr)
3104c2832898SQu Wenruo 				break;
3105c2832898SQu Wenruo 		}
3106c2832898SQu Wenruo 	}
3107f47960f4SQu Wenruo 	unlock_page(page);
3108f47960f4SQu Wenruo 	put_page(page);
3109f47960f4SQu Wenruo 
3110f47960f4SQu Wenruo 	balance_dirty_pages_ratelimited(inode->i_mapping);
3111f47960f4SQu Wenruo 	btrfs_throttle(fs_info);
3112f47960f4SQu Wenruo 	if (btrfs_should_cancel_balance(fs_info))
3113f47960f4SQu Wenruo 		ret = -ECANCELED;
3114f47960f4SQu Wenruo 	return ret;
3115f47960f4SQu Wenruo 
3116f47960f4SQu Wenruo release_page:
3117f47960f4SQu Wenruo 	unlock_page(page);
3118f47960f4SQu Wenruo 	put_page(page);
3119f47960f4SQu Wenruo 	return ret;
3120f47960f4SQu Wenruo }
3121f47960f4SQu Wenruo 
relocate_file_extent_cluster(struct inode * inode,struct file_extent_cluster * cluster)31220257bb82SYan, Zheng static int relocate_file_extent_cluster(struct inode *inode,
31230257bb82SYan, Zheng 					struct file_extent_cluster *cluster)
31240257bb82SYan, Zheng {
31250257bb82SYan, Zheng 	u64 offset = BTRFS_I(inode)->index_cnt;
31260257bb82SYan, Zheng 	unsigned long index;
31270257bb82SYan, Zheng 	unsigned long last_index;
31280257bb82SYan, Zheng 	struct file_ra_state *ra;
3129f47960f4SQu Wenruo 	int cluster_nr = 0;
31300257bb82SYan, Zheng 	int ret = 0;
31310257bb82SYan, Zheng 
31320257bb82SYan, Zheng 	if (!cluster->nr)
31330257bb82SYan, Zheng 		return 0;
31340257bb82SYan, Zheng 
31350257bb82SYan, Zheng 	ra = kzalloc(sizeof(*ra), GFP_NOFS);
31360257bb82SYan, Zheng 	if (!ra)
31370257bb82SYan, Zheng 		return -ENOMEM;
31380257bb82SYan, Zheng 
3139056d9becSNikolay Borisov 	ret = prealloc_file_extent_cluster(BTRFS_I(inode), cluster);
31400257bb82SYan, Zheng 	if (ret)
3141efa56464SYan, Zheng 		goto out;
31420257bb82SYan, Zheng 
31430257bb82SYan, Zheng 	file_ra_state_init(ra, inode->i_mapping);
31440257bb82SYan, Zheng 
31454b01c44fSJohannes Thumshirn 	ret = setup_relocation_extent_mapping(inode, cluster->start - offset,
3146efa56464SYan, Zheng 				   cluster->end - offset, cluster->start);
3147efa56464SYan, Zheng 	if (ret)
3148efa56464SYan, Zheng 		goto out;
3149efa56464SYan, Zheng 
315009cbfeafSKirill A. Shutemov 	last_index = (cluster->end - offset) >> PAGE_SHIFT;
3151f47960f4SQu Wenruo 	for (index = (cluster->start - offset) >> PAGE_SHIFT;
3152f47960f4SQu Wenruo 	     index <= last_index && !ret; index++)
3153f47960f4SQu Wenruo 		ret = relocate_one_page(inode, ra, cluster, &cluster_nr, index);
3154f47960f4SQu Wenruo 	if (ret == 0)
3155f47960f4SQu Wenruo 		WARN_ON(cluster_nr != cluster->nr);
3156efa56464SYan, Zheng out:
31570257bb82SYan, Zheng 	kfree(ra);
31580257bb82SYan, Zheng 	return ret;
31590257bb82SYan, Zheng }
31600257bb82SYan, Zheng 
31610257bb82SYan, Zheng static noinline_for_stack
relocate_data_extent(struct inode * inode,struct btrfs_key * extent_key,struct file_extent_cluster * cluster)31620257bb82SYan, Zheng int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key,
31630257bb82SYan, Zheng 			 struct file_extent_cluster *cluster)
31640257bb82SYan, Zheng {
31650257bb82SYan, Zheng 	int ret;
31660257bb82SYan, Zheng 
31670257bb82SYan, Zheng 	if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) {
31680257bb82SYan, Zheng 		ret = relocate_file_extent_cluster(inode, cluster);
31690257bb82SYan, Zheng 		if (ret)
31700257bb82SYan, Zheng 			return ret;
31710257bb82SYan, Zheng 		cluster->nr = 0;
31720257bb82SYan, Zheng 	}
31730257bb82SYan, Zheng 
31740257bb82SYan, Zheng 	if (!cluster->nr)
31750257bb82SYan, Zheng 		cluster->start = extent_key->objectid;
31760257bb82SYan, Zheng 	else
31770257bb82SYan, Zheng 		BUG_ON(cluster->nr >= MAX_EXTENTS);
31780257bb82SYan, Zheng 	cluster->end = extent_key->objectid + extent_key->offset - 1;
31790257bb82SYan, Zheng 	cluster->boundary[cluster->nr] = extent_key->objectid;
31800257bb82SYan, Zheng 	cluster->nr++;
31810257bb82SYan, Zheng 
31820257bb82SYan, Zheng 	if (cluster->nr >= MAX_EXTENTS) {
31830257bb82SYan, Zheng 		ret = relocate_file_extent_cluster(inode, cluster);
31840257bb82SYan, Zheng 		if (ret)
31850257bb82SYan, Zheng 			return ret;
31860257bb82SYan, Zheng 		cluster->nr = 0;
31870257bb82SYan, Zheng 	}
31880257bb82SYan, Zheng 	return 0;
31895d4f98a2SYan Zheng }
31905d4f98a2SYan Zheng 
31915d4f98a2SYan Zheng /*
31925d4f98a2SYan Zheng  * helper to add a tree block to the list.
31935d4f98a2SYan Zheng  * the major work is getting the generation and level of the block
31945d4f98a2SYan Zheng  */
add_tree_block(struct reloc_control * rc,struct btrfs_key * extent_key,struct btrfs_path * path,struct rb_root * blocks)31955d4f98a2SYan Zheng static int add_tree_block(struct reloc_control *rc,
31965d4f98a2SYan Zheng 			  struct btrfs_key *extent_key,
31975d4f98a2SYan Zheng 			  struct btrfs_path *path,
31985d4f98a2SYan Zheng 			  struct rb_root *blocks)
31995d4f98a2SYan Zheng {
32005d4f98a2SYan Zheng 	struct extent_buffer *eb;
32015d4f98a2SYan Zheng 	struct btrfs_extent_item *ei;
32025d4f98a2SYan Zheng 	struct btrfs_tree_block_info *bi;
32035d4f98a2SYan Zheng 	struct tree_block *block;
32045d4f98a2SYan Zheng 	struct rb_node *rb_node;
32055d4f98a2SYan Zheng 	u32 item_size;
32065d4f98a2SYan Zheng 	int level = -1;
32077fdf4b60SWang Shilong 	u64 generation;
3208f7ba2d37SJosef Bacik 	u64 owner = 0;
32095d4f98a2SYan Zheng 
32105d4f98a2SYan Zheng 	eb =  path->nodes[0];
32113212fa14SJosef Bacik 	item_size = btrfs_item_size(eb, path->slots[0]);
32125d4f98a2SYan Zheng 
32133173a18fSJosef Bacik 	if (extent_key->type == BTRFS_METADATA_ITEM_KEY ||
32143173a18fSJosef Bacik 	    item_size >= sizeof(*ei) + sizeof(*bi)) {
3215f7ba2d37SJosef Bacik 		unsigned long ptr = 0, end;
3216f7ba2d37SJosef Bacik 
32175d4f98a2SYan Zheng 		ei = btrfs_item_ptr(eb, path->slots[0],
32185d4f98a2SYan Zheng 				struct btrfs_extent_item);
3219f7ba2d37SJosef Bacik 		end = (unsigned long)ei + item_size;
32203173a18fSJosef Bacik 		if (extent_key->type == BTRFS_EXTENT_ITEM_KEY) {
32215d4f98a2SYan Zheng 			bi = (struct btrfs_tree_block_info *)(ei + 1);
32225d4f98a2SYan Zheng 			level = btrfs_tree_block_level(eb, bi);
3223f7ba2d37SJosef Bacik 			ptr = (unsigned long)(bi + 1);
32245d4f98a2SYan Zheng 		} else {
32253173a18fSJosef Bacik 			level = (int)extent_key->offset;
3226f7ba2d37SJosef Bacik 			ptr = (unsigned long)(ei + 1);
32273173a18fSJosef Bacik 		}
32283173a18fSJosef Bacik 		generation = btrfs_extent_generation(eb, ei);
3229f7ba2d37SJosef Bacik 
3230f7ba2d37SJosef Bacik 		/*
3231f7ba2d37SJosef Bacik 		 * We're reading random blocks without knowing their owner ahead
3232f7ba2d37SJosef Bacik 		 * of time.  This is ok most of the time, as all reloc roots and
3233f7ba2d37SJosef Bacik 		 * fs roots have the same lock type.  However normal trees do
3234f7ba2d37SJosef Bacik 		 * not, and the only way to know ahead of time is to read the
3235f7ba2d37SJosef Bacik 		 * inline ref offset.  We know it's an fs root if
3236f7ba2d37SJosef Bacik 		 *
3237f7ba2d37SJosef Bacik 		 * 1. There's more than one ref.
3238f7ba2d37SJosef Bacik 		 * 2. There's a SHARED_DATA_REF_KEY set.
3239f7ba2d37SJosef Bacik 		 * 3. FULL_BACKREF is set on the flags.
3240f7ba2d37SJosef Bacik 		 *
3241f7ba2d37SJosef Bacik 		 * Otherwise it's safe to assume that the ref offset == the
3242f7ba2d37SJosef Bacik 		 * owner of this block, so we can use that when calling
3243f7ba2d37SJosef Bacik 		 * read_tree_block.
3244f7ba2d37SJosef Bacik 		 */
3245f7ba2d37SJosef Bacik 		if (btrfs_extent_refs(eb, ei) == 1 &&
3246f7ba2d37SJosef Bacik 		    !(btrfs_extent_flags(eb, ei) &
3247f7ba2d37SJosef Bacik 		      BTRFS_BLOCK_FLAG_FULL_BACKREF) &&
3248f7ba2d37SJosef Bacik 		    ptr < end) {
3249f7ba2d37SJosef Bacik 			struct btrfs_extent_inline_ref *iref;
3250f7ba2d37SJosef Bacik 			int type;
3251f7ba2d37SJosef Bacik 
3252f7ba2d37SJosef Bacik 			iref = (struct btrfs_extent_inline_ref *)ptr;
3253f7ba2d37SJosef Bacik 			type = btrfs_get_extent_inline_ref_type(eb, iref,
3254f7ba2d37SJosef Bacik 							BTRFS_REF_TYPE_BLOCK);
3255f7ba2d37SJosef Bacik 			if (type == BTRFS_REF_TYPE_INVALID)
3256f7ba2d37SJosef Bacik 				return -EINVAL;
3257f7ba2d37SJosef Bacik 			if (type == BTRFS_TREE_BLOCK_REF_KEY)
3258f7ba2d37SJosef Bacik 				owner = btrfs_extent_inline_ref_offset(eb, iref);
3259f7ba2d37SJosef Bacik 		}
32603173a18fSJosef Bacik 	} else {
3261182741d2SQu Wenruo 		btrfs_print_leaf(eb);
3262182741d2SQu Wenruo 		btrfs_err(rc->block_group->fs_info,
3263182741d2SQu Wenruo 			  "unrecognized tree backref at tree block %llu slot %u",
3264182741d2SQu Wenruo 			  eb->start, path->slots[0]);
3265182741d2SQu Wenruo 		btrfs_release_path(path);
3266182741d2SQu Wenruo 		return -EUCLEAN;
32675d4f98a2SYan Zheng 	}
32685d4f98a2SYan Zheng 
3269b3b4aa74SDavid Sterba 	btrfs_release_path(path);
32705d4f98a2SYan Zheng 
32715d4f98a2SYan Zheng 	BUG_ON(level == -1);
32725d4f98a2SYan Zheng 
32735d4f98a2SYan Zheng 	block = kmalloc(sizeof(*block), GFP_NOFS);
32745d4f98a2SYan Zheng 	if (!block)
32755d4f98a2SYan Zheng 		return -ENOMEM;
32765d4f98a2SYan Zheng 
32775d4f98a2SYan Zheng 	block->bytenr = extent_key->objectid;
3278da17066cSJeff Mahoney 	block->key.objectid = rc->extent_root->fs_info->nodesize;
32795d4f98a2SYan Zheng 	block->key.offset = generation;
32805d4f98a2SYan Zheng 	block->level = level;
32815d4f98a2SYan Zheng 	block->key_ready = 0;
3282f7ba2d37SJosef Bacik 	block->owner = owner;
32835d4f98a2SYan Zheng 
3284e9a28dc5SQu Wenruo 	rb_node = rb_simple_insert(blocks, block->bytenr, &block->rb_node);
328543c04fb1SJeff Mahoney 	if (rb_node)
3286982c92cbSQu Wenruo 		btrfs_backref_panic(rc->extent_root->fs_info, block->bytenr,
3287982c92cbSQu Wenruo 				    -EEXIST);
32885d4f98a2SYan Zheng 
32895d4f98a2SYan Zheng 	return 0;
32905d4f98a2SYan Zheng }
32915d4f98a2SYan Zheng 
32925d4f98a2SYan Zheng /*
32935d4f98a2SYan Zheng  * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
32945d4f98a2SYan Zheng  */
__add_tree_block(struct reloc_control * rc,u64 bytenr,u32 blocksize,struct rb_root * blocks)32955d4f98a2SYan Zheng static int __add_tree_block(struct reloc_control *rc,
32965d4f98a2SYan Zheng 			    u64 bytenr, u32 blocksize,
32975d4f98a2SYan Zheng 			    struct rb_root *blocks)
32985d4f98a2SYan Zheng {
32990b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
33005d4f98a2SYan Zheng 	struct btrfs_path *path;
33015d4f98a2SYan Zheng 	struct btrfs_key key;
33025d4f98a2SYan Zheng 	int ret;
33030b246afaSJeff Mahoney 	bool skinny = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
33045d4f98a2SYan Zheng 
33057476dfdaSDavid Sterba 	if (tree_block_processed(bytenr, rc))
33065d4f98a2SYan Zheng 		return 0;
33075d4f98a2SYan Zheng 
3308e9a28dc5SQu Wenruo 	if (rb_simple_search(blocks, bytenr))
33095d4f98a2SYan Zheng 		return 0;
33105d4f98a2SYan Zheng 
33115d4f98a2SYan Zheng 	path = btrfs_alloc_path();
33125d4f98a2SYan Zheng 	if (!path)
33135d4f98a2SYan Zheng 		return -ENOMEM;
3314aee68ee5SJosef Bacik again:
33155d4f98a2SYan Zheng 	key.objectid = bytenr;
3316aee68ee5SJosef Bacik 	if (skinny) {
3317aee68ee5SJosef Bacik 		key.type = BTRFS_METADATA_ITEM_KEY;
3318aee68ee5SJosef Bacik 		key.offset = (u64)-1;
3319aee68ee5SJosef Bacik 	} else {
33205d4f98a2SYan Zheng 		key.type = BTRFS_EXTENT_ITEM_KEY;
33215d4f98a2SYan Zheng 		key.offset = blocksize;
3322aee68ee5SJosef Bacik 	}
33235d4f98a2SYan Zheng 
33245d4f98a2SYan Zheng 	path->search_commit_root = 1;
33255d4f98a2SYan Zheng 	path->skip_locking = 1;
33265d4f98a2SYan Zheng 	ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0);
33275d4f98a2SYan Zheng 	if (ret < 0)
33285d4f98a2SYan Zheng 		goto out;
33295d4f98a2SYan Zheng 
3330aee68ee5SJosef Bacik 	if (ret > 0 && skinny) {
3331aee68ee5SJosef Bacik 		if (path->slots[0]) {
3332aee68ee5SJosef Bacik 			path->slots[0]--;
3333aee68ee5SJosef Bacik 			btrfs_item_key_to_cpu(path->nodes[0], &key,
3334aee68ee5SJosef Bacik 					      path->slots[0]);
33353173a18fSJosef Bacik 			if (key.objectid == bytenr &&
3336aee68ee5SJosef Bacik 			    (key.type == BTRFS_METADATA_ITEM_KEY ||
3337aee68ee5SJosef Bacik 			     (key.type == BTRFS_EXTENT_ITEM_KEY &&
3338aee68ee5SJosef Bacik 			      key.offset == blocksize)))
33393173a18fSJosef Bacik 				ret = 0;
33403173a18fSJosef Bacik 		}
3341aee68ee5SJosef Bacik 
3342aee68ee5SJosef Bacik 		if (ret) {
3343aee68ee5SJosef Bacik 			skinny = false;
3344aee68ee5SJosef Bacik 			btrfs_release_path(path);
3345aee68ee5SJosef Bacik 			goto again;
3346aee68ee5SJosef Bacik 		}
3347aee68ee5SJosef Bacik 	}
3348cdccee99SLiu Bo 	if (ret) {
3349cdccee99SLiu Bo 		ASSERT(ret == 1);
3350cdccee99SLiu Bo 		btrfs_print_leaf(path->nodes[0]);
3351cdccee99SLiu Bo 		btrfs_err(fs_info,
3352cdccee99SLiu Bo 	     "tree block extent item (%llu) is not found in extent tree",
3353cdccee99SLiu Bo 		     bytenr);
3354cdccee99SLiu Bo 		WARN_ON(1);
3355cdccee99SLiu Bo 		ret = -EINVAL;
3356cdccee99SLiu Bo 		goto out;
3357cdccee99SLiu Bo 	}
33583173a18fSJosef Bacik 
33595d4f98a2SYan Zheng 	ret = add_tree_block(rc, &key, path, blocks);
33605d4f98a2SYan Zheng out:
33615d4f98a2SYan Zheng 	btrfs_free_path(path);
33625d4f98a2SYan Zheng 	return ret;
33635d4f98a2SYan Zheng }
33645d4f98a2SYan Zheng 
delete_block_group_cache(struct btrfs_fs_info * fs_info,struct btrfs_block_group * block_group,struct inode * inode,u64 ino)33650af3d00bSJosef Bacik static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
336632da5386SDavid Sterba 				    struct btrfs_block_group *block_group,
33671bbc621eSChris Mason 				    struct inode *inode,
33681bbc621eSChris Mason 				    u64 ino)
33690af3d00bSJosef Bacik {
33700af3d00bSJosef Bacik 	struct btrfs_root *root = fs_info->tree_root;
33710af3d00bSJosef Bacik 	struct btrfs_trans_handle *trans;
33720af3d00bSJosef Bacik 	int ret = 0;
33730af3d00bSJosef Bacik 
33740af3d00bSJosef Bacik 	if (inode)
33750af3d00bSJosef Bacik 		goto truncate;
33760af3d00bSJosef Bacik 
33770202e83fSDavid Sterba 	inode = btrfs_iget(fs_info->sb, ino, root);
33782e19f1f9SAl Viro 	if (IS_ERR(inode))
33790af3d00bSJosef Bacik 		return -ENOENT;
33800af3d00bSJosef Bacik 
33810af3d00bSJosef Bacik truncate:
33822ff7e61eSJeff Mahoney 	ret = btrfs_check_trunc_cache_free_space(fs_info,
33837b61cd92SMiao Xie 						 &fs_info->global_block_rsv);
33847b61cd92SMiao Xie 	if (ret)
33857b61cd92SMiao Xie 		goto out;
33867b61cd92SMiao Xie 
33877a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(root);
33880af3d00bSJosef Bacik 	if (IS_ERR(trans)) {
33893612b495STsutomu Itoh 		ret = PTR_ERR(trans);
33900af3d00bSJosef Bacik 		goto out;
33910af3d00bSJosef Bacik 	}
33920af3d00bSJosef Bacik 
339377ab86bfSJeff Mahoney 	ret = btrfs_truncate_free_space_cache(trans, block_group, inode);
33940af3d00bSJosef Bacik 
33953a45bb20SJeff Mahoney 	btrfs_end_transaction(trans);
33962ff7e61eSJeff Mahoney 	btrfs_btree_balance_dirty(fs_info);
33970af3d00bSJosef Bacik out:
33980af3d00bSJosef Bacik 	iput(inode);
33990af3d00bSJosef Bacik 	return ret;
34000af3d00bSJosef Bacik }
34010af3d00bSJosef Bacik 
34025d4f98a2SYan Zheng /*
340319b546d7SQu Wenruo  * Locate the free space cache EXTENT_DATA in root tree leaf and delete the
340419b546d7SQu Wenruo  * cache inode, to avoid free space cache data extent blocking data relocation.
34055d4f98a2SYan Zheng  */
delete_v1_space_cache(struct extent_buffer * leaf,struct btrfs_block_group * block_group,u64 data_bytenr)340619b546d7SQu Wenruo static int delete_v1_space_cache(struct extent_buffer *leaf,
340719b546d7SQu Wenruo 				 struct btrfs_block_group *block_group,
340819b546d7SQu Wenruo 				 u64 data_bytenr)
34095d4f98a2SYan Zheng {
341019b546d7SQu Wenruo 	u64 space_cache_ino;
341119b546d7SQu Wenruo 	struct btrfs_file_extent_item *ei;
34125d4f98a2SYan Zheng 	struct btrfs_key key;
341319b546d7SQu Wenruo 	bool found = false;
341419b546d7SQu Wenruo 	int i;
34155d4f98a2SYan Zheng 	int ret;
34165d4f98a2SYan Zheng 
341719b546d7SQu Wenruo 	if (btrfs_header_owner(leaf) != BTRFS_ROOT_TREE_OBJECTID)
341819b546d7SQu Wenruo 		return 0;
34195d4f98a2SYan Zheng 
342019b546d7SQu Wenruo 	for (i = 0; i < btrfs_header_nritems(leaf); i++) {
342150e31ef4SQu Wenruo 		u8 type;
342250e31ef4SQu Wenruo 
342319b546d7SQu Wenruo 		btrfs_item_key_to_cpu(leaf, &key, i);
342419b546d7SQu Wenruo 		if (key.type != BTRFS_EXTENT_DATA_KEY)
342519b546d7SQu Wenruo 			continue;
342619b546d7SQu Wenruo 		ei = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
342750e31ef4SQu Wenruo 		type = btrfs_file_extent_type(leaf, ei);
342850e31ef4SQu Wenruo 
342950e31ef4SQu Wenruo 		if ((type == BTRFS_FILE_EXTENT_REG ||
343050e31ef4SQu Wenruo 		     type == BTRFS_FILE_EXTENT_PREALLOC) &&
343119b546d7SQu Wenruo 		    btrfs_file_extent_disk_bytenr(leaf, ei) == data_bytenr) {
343219b546d7SQu Wenruo 			found = true;
343319b546d7SQu Wenruo 			space_cache_ino = key.objectid;
343419b546d7SQu Wenruo 			break;
343519b546d7SQu Wenruo 		}
343619b546d7SQu Wenruo 	}
343719b546d7SQu Wenruo 	if (!found)
343819b546d7SQu Wenruo 		return -ENOENT;
343919b546d7SQu Wenruo 	ret = delete_block_group_cache(leaf->fs_info, block_group, NULL,
344019b546d7SQu Wenruo 					space_cache_ino);
34410af3d00bSJosef Bacik 	return ret;
34425d4f98a2SYan Zheng }
34435d4f98a2SYan Zheng 
34445d4f98a2SYan Zheng /*
34452c016dc2SLiu Bo  * helper to find all tree blocks that reference a given data extent
34465d4f98a2SYan Zheng  */
34475d4f98a2SYan Zheng static noinline_for_stack
add_data_references(struct reloc_control * rc,struct btrfs_key * extent_key,struct btrfs_path * path,struct rb_root * blocks)34485d4f98a2SYan Zheng int add_data_references(struct reloc_control *rc,
34495d4f98a2SYan Zheng 			struct btrfs_key *extent_key,
34505d4f98a2SYan Zheng 			struct btrfs_path *path,
34515d4f98a2SYan Zheng 			struct rb_root *blocks)
34525d4f98a2SYan Zheng {
3453a2c8d27eSFilipe Manana 	struct btrfs_backref_walk_ctx ctx = { 0 };
345419b546d7SQu Wenruo 	struct ulist_iterator leaf_uiter;
345519b546d7SQu Wenruo 	struct ulist_node *ref_node = NULL;
3456a2c8d27eSFilipe Manana 	const u32 blocksize = rc->extent_root->fs_info->nodesize;
3457647f63bdSFilipe David Borba Manana 	int ret = 0;
34585d4f98a2SYan Zheng 
3459b3b4aa74SDavid Sterba 	btrfs_release_path(path);
3460a2c8d27eSFilipe Manana 
3461a2c8d27eSFilipe Manana 	ctx.bytenr = extent_key->objectid;
34620cad8f14SFilipe Manana 	ctx.skip_inode_ref_list = true;
3463a2c8d27eSFilipe Manana 	ctx.fs_info = rc->extent_root->fs_info;
3464a2c8d27eSFilipe Manana 
3465a2c8d27eSFilipe Manana 	ret = btrfs_find_all_leafs(&ctx);
346619b546d7SQu Wenruo 	if (ret < 0)
346719b546d7SQu Wenruo 		return ret;
346819b546d7SQu Wenruo 
346919b546d7SQu Wenruo 	ULIST_ITER_INIT(&leaf_uiter);
3470a2c8d27eSFilipe Manana 	while ((ref_node = ulist_next(ctx.refs, &leaf_uiter))) {
3471789d6a3aSQu Wenruo 		struct btrfs_tree_parent_check check = { 0 };
347219b546d7SQu Wenruo 		struct extent_buffer *eb;
347319b546d7SQu Wenruo 
3474789d6a3aSQu Wenruo 		eb = read_tree_block(ctx.fs_info, ref_node->val, &check);
347519b546d7SQu Wenruo 		if (IS_ERR(eb)) {
347619b546d7SQu Wenruo 			ret = PTR_ERR(eb);
347719b546d7SQu Wenruo 			break;
347819b546d7SQu Wenruo 		}
347919b546d7SQu Wenruo 		ret = delete_v1_space_cache(eb, rc->block_group,
348019b546d7SQu Wenruo 					    extent_key->objectid);
348119b546d7SQu Wenruo 		free_extent_buffer(eb);
348219b546d7SQu Wenruo 		if (ret < 0)
348319b546d7SQu Wenruo 			break;
348419b546d7SQu Wenruo 		ret = __add_tree_block(rc, ref_node->val, blocksize, blocks);
348519b546d7SQu Wenruo 		if (ret < 0)
348619b546d7SQu Wenruo 			break;
348719b546d7SQu Wenruo 	}
348819b546d7SQu Wenruo 	if (ret < 0)
34895d4f98a2SYan Zheng 		free_block_list(blocks);
3490a2c8d27eSFilipe Manana 	ulist_free(ctx.refs);
349119b546d7SQu Wenruo 	return ret;
34925d4f98a2SYan Zheng }
34935d4f98a2SYan Zheng 
34945d4f98a2SYan Zheng /*
34952c016dc2SLiu Bo  * helper to find next unprocessed extent
34965d4f98a2SYan Zheng  */
34975d4f98a2SYan Zheng static noinline_for_stack
find_next_extent(struct reloc_control * rc,struct btrfs_path * path,struct btrfs_key * extent_key)3498147d256eSZhaolei int find_next_extent(struct reloc_control *rc, struct btrfs_path *path,
34993fd0a558SYan, Zheng 		     struct btrfs_key *extent_key)
35005d4f98a2SYan Zheng {
35010b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
35025d4f98a2SYan Zheng 	struct btrfs_key key;
35035d4f98a2SYan Zheng 	struct extent_buffer *leaf;
35045d4f98a2SYan Zheng 	u64 start, end, last;
35055d4f98a2SYan Zheng 	int ret;
35065d4f98a2SYan Zheng 
3507b3470b5dSDavid Sterba 	last = rc->block_group->start + rc->block_group->length;
35085d4f98a2SYan Zheng 	while (1) {
3509e5860f82SFilipe Manana 		bool block_found;
3510e5860f82SFilipe Manana 
35115d4f98a2SYan Zheng 		cond_resched();
35125d4f98a2SYan Zheng 		if (rc->search_start >= last) {
35135d4f98a2SYan Zheng 			ret = 1;
35145d4f98a2SYan Zheng 			break;
35155d4f98a2SYan Zheng 		}
35165d4f98a2SYan Zheng 
35175d4f98a2SYan Zheng 		key.objectid = rc->search_start;
35185d4f98a2SYan Zheng 		key.type = BTRFS_EXTENT_ITEM_KEY;
35195d4f98a2SYan Zheng 		key.offset = 0;
35205d4f98a2SYan Zheng 
35215d4f98a2SYan Zheng 		path->search_commit_root = 1;
35225d4f98a2SYan Zheng 		path->skip_locking = 1;
35235d4f98a2SYan Zheng 		ret = btrfs_search_slot(NULL, rc->extent_root, &key, path,
35245d4f98a2SYan Zheng 					0, 0);
35255d4f98a2SYan Zheng 		if (ret < 0)
35265d4f98a2SYan Zheng 			break;
35275d4f98a2SYan Zheng next:
35285d4f98a2SYan Zheng 		leaf = path->nodes[0];
35295d4f98a2SYan Zheng 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
35305d4f98a2SYan Zheng 			ret = btrfs_next_leaf(rc->extent_root, path);
35315d4f98a2SYan Zheng 			if (ret != 0)
35325d4f98a2SYan Zheng 				break;
35335d4f98a2SYan Zheng 			leaf = path->nodes[0];
35345d4f98a2SYan Zheng 		}
35355d4f98a2SYan Zheng 
35365d4f98a2SYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
35375d4f98a2SYan Zheng 		if (key.objectid >= last) {
35385d4f98a2SYan Zheng 			ret = 1;
35395d4f98a2SYan Zheng 			break;
35405d4f98a2SYan Zheng 		}
35415d4f98a2SYan Zheng 
35423173a18fSJosef Bacik 		if (key.type != BTRFS_EXTENT_ITEM_KEY &&
35433173a18fSJosef Bacik 		    key.type != BTRFS_METADATA_ITEM_KEY) {
35443173a18fSJosef Bacik 			path->slots[0]++;
35453173a18fSJosef Bacik 			goto next;
35463173a18fSJosef Bacik 		}
35473173a18fSJosef Bacik 
35483173a18fSJosef Bacik 		if (key.type == BTRFS_EXTENT_ITEM_KEY &&
35495d4f98a2SYan Zheng 		    key.objectid + key.offset <= rc->search_start) {
35505d4f98a2SYan Zheng 			path->slots[0]++;
35515d4f98a2SYan Zheng 			goto next;
35525d4f98a2SYan Zheng 		}
35535d4f98a2SYan Zheng 
35543173a18fSJosef Bacik 		if (key.type == BTRFS_METADATA_ITEM_KEY &&
35550b246afaSJeff Mahoney 		    key.objectid + fs_info->nodesize <=
35563173a18fSJosef Bacik 		    rc->search_start) {
35573173a18fSJosef Bacik 			path->slots[0]++;
35583173a18fSJosef Bacik 			goto next;
35593173a18fSJosef Bacik 		}
35603173a18fSJosef Bacik 
3561e5860f82SFilipe Manana 		block_found = find_first_extent_bit(&rc->processed_blocks,
35625d4f98a2SYan Zheng 						    key.objectid, &start, &end,
3563e6138876SJosef Bacik 						    EXTENT_DIRTY, NULL);
35645d4f98a2SYan Zheng 
3565e5860f82SFilipe Manana 		if (block_found && start <= key.objectid) {
3566b3b4aa74SDavid Sterba 			btrfs_release_path(path);
35675d4f98a2SYan Zheng 			rc->search_start = end + 1;
35685d4f98a2SYan Zheng 		} else {
35693173a18fSJosef Bacik 			if (key.type == BTRFS_EXTENT_ITEM_KEY)
35705d4f98a2SYan Zheng 				rc->search_start = key.objectid + key.offset;
35713173a18fSJosef Bacik 			else
35723173a18fSJosef Bacik 				rc->search_start = key.objectid +
35730b246afaSJeff Mahoney 					fs_info->nodesize;
35743fd0a558SYan, Zheng 			memcpy(extent_key, &key, sizeof(key));
35755d4f98a2SYan Zheng 			return 0;
35765d4f98a2SYan Zheng 		}
35775d4f98a2SYan Zheng 	}
3578b3b4aa74SDavid Sterba 	btrfs_release_path(path);
35795d4f98a2SYan Zheng 	return ret;
35805d4f98a2SYan Zheng }
35815d4f98a2SYan Zheng 
set_reloc_control(struct reloc_control * rc)35825d4f98a2SYan Zheng static void set_reloc_control(struct reloc_control *rc)
35835d4f98a2SYan Zheng {
35845d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
35857585717fSChris Mason 
35867585717fSChris Mason 	mutex_lock(&fs_info->reloc_mutex);
35875d4f98a2SYan Zheng 	fs_info->reloc_ctl = rc;
35887585717fSChris Mason 	mutex_unlock(&fs_info->reloc_mutex);
35895d4f98a2SYan Zheng }
35905d4f98a2SYan Zheng 
unset_reloc_control(struct reloc_control * rc)35915d4f98a2SYan Zheng static void unset_reloc_control(struct reloc_control *rc)
35925d4f98a2SYan Zheng {
35935d4f98a2SYan Zheng 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
35947585717fSChris Mason 
35957585717fSChris Mason 	mutex_lock(&fs_info->reloc_mutex);
35965d4f98a2SYan Zheng 	fs_info->reloc_ctl = NULL;
35977585717fSChris Mason 	mutex_unlock(&fs_info->reloc_mutex);
35985d4f98a2SYan Zheng }
35995d4f98a2SYan Zheng 
36003fd0a558SYan, Zheng static noinline_for_stack
prepare_to_relocate(struct reloc_control * rc)36013fd0a558SYan, Zheng int prepare_to_relocate(struct reloc_control *rc)
36023fd0a558SYan, Zheng {
36033fd0a558SYan, Zheng 	struct btrfs_trans_handle *trans;
3604ac2fabacSJosef Bacik 	int ret;
36053fd0a558SYan, Zheng 
36062ff7e61eSJeff Mahoney 	rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root->fs_info,
360766d8f3ddSMiao Xie 					      BTRFS_BLOCK_RSV_TEMP);
36083fd0a558SYan, Zheng 	if (!rc->block_rsv)
36093fd0a558SYan, Zheng 		return -ENOMEM;
36103fd0a558SYan, Zheng 
36113fd0a558SYan, Zheng 	memset(&rc->cluster, 0, sizeof(rc->cluster));
3612b3470b5dSDavid Sterba 	rc->search_start = rc->block_group->start;
36133fd0a558SYan, Zheng 	rc->extents_found = 0;
36143fd0a558SYan, Zheng 	rc->nodes_relocated = 0;
36153fd0a558SYan, Zheng 	rc->merging_rsv_size = 0;
36160647bf56SWang Shilong 	rc->reserved_bytes = 0;
3617da17066cSJeff Mahoney 	rc->block_rsv->size = rc->extent_root->fs_info->nodesize *
36180647bf56SWang Shilong 			      RELOCATION_RESERVED_NODES;
36199270501cSJosef Bacik 	ret = btrfs_block_rsv_refill(rc->extent_root->fs_info,
3620ac2fabacSJosef Bacik 				     rc->block_rsv, rc->block_rsv->size,
3621ac2fabacSJosef Bacik 				     BTRFS_RESERVE_FLUSH_ALL);
3622ac2fabacSJosef Bacik 	if (ret)
3623ac2fabacSJosef Bacik 		return ret;
36243fd0a558SYan, Zheng 
36253fd0a558SYan, Zheng 	rc->create_reloc_tree = 1;
36263fd0a558SYan, Zheng 	set_reloc_control(rc);
36273fd0a558SYan, Zheng 
36287a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(rc->extent_root);
362928818947SLiu Bo 	if (IS_ERR(trans)) {
363028818947SLiu Bo 		unset_reloc_control(rc);
363128818947SLiu Bo 		/*
363228818947SLiu Bo 		 * extent tree is not a ref_cow tree and has no reloc_root to
363328818947SLiu Bo 		 * cleanup.  And callers are responsible to free the above
363428818947SLiu Bo 		 * block rsv.
363528818947SLiu Bo 		 */
363628818947SLiu Bo 		return PTR_ERR(trans);
363728818947SLiu Bo 	}
363885f02d6cSZixuan Fu 
363985f02d6cSZixuan Fu 	ret = btrfs_commit_transaction(trans);
364085f02d6cSZixuan Fu 	if (ret)
364185f02d6cSZixuan Fu 		unset_reloc_control(rc);
364285f02d6cSZixuan Fu 
364385f02d6cSZixuan Fu 	return ret;
36443fd0a558SYan, Zheng }
364576dda93cSYan, Zheng 
relocate_block_group(struct reloc_control * rc)36465d4f98a2SYan Zheng static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
36475d4f98a2SYan Zheng {
36482ff7e61eSJeff Mahoney 	struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
36495d4f98a2SYan Zheng 	struct rb_root blocks = RB_ROOT;
36505d4f98a2SYan Zheng 	struct btrfs_key key;
36515d4f98a2SYan Zheng 	struct btrfs_trans_handle *trans = NULL;
36525d4f98a2SYan Zheng 	struct btrfs_path *path;
36535d4f98a2SYan Zheng 	struct btrfs_extent_item *ei;
36545d4f98a2SYan Zheng 	u64 flags;
36555d4f98a2SYan Zheng 	int ret;
36565d4f98a2SYan Zheng 	int err = 0;
3657c87f08caSChris Mason 	int progress = 0;
36585d4f98a2SYan Zheng 
36595d4f98a2SYan Zheng 	path = btrfs_alloc_path();
36603fd0a558SYan, Zheng 	if (!path)
36615d4f98a2SYan Zheng 		return -ENOMEM;
3662e4058b54SDavid Sterba 	path->reada = READA_FORWARD;
36633fd0a558SYan, Zheng 
36643fd0a558SYan, Zheng 	ret = prepare_to_relocate(rc);
36653fd0a558SYan, Zheng 	if (ret) {
36663fd0a558SYan, Zheng 		err = ret;
36673fd0a558SYan, Zheng 		goto out_free;
36682423fdfbSJiri Slaby 	}
36695d4f98a2SYan Zheng 
36705d4f98a2SYan Zheng 	while (1) {
36710647bf56SWang Shilong 		rc->reserved_bytes = 0;
36729270501cSJosef Bacik 		ret = btrfs_block_rsv_refill(fs_info, rc->block_rsv,
36739270501cSJosef Bacik 					     rc->block_rsv->size,
36740647bf56SWang Shilong 					     BTRFS_RESERVE_FLUSH_ALL);
36750647bf56SWang Shilong 		if (ret) {
36760647bf56SWang Shilong 			err = ret;
36770647bf56SWang Shilong 			break;
36780647bf56SWang Shilong 		}
3679c87f08caSChris Mason 		progress++;
3680a22285a6SYan, Zheng 		trans = btrfs_start_transaction(rc->extent_root, 0);
36810f788c58SLiu Bo 		if (IS_ERR(trans)) {
36820f788c58SLiu Bo 			err = PTR_ERR(trans);
36830f788c58SLiu Bo 			trans = NULL;
36840f788c58SLiu Bo 			break;
36850f788c58SLiu Bo 		}
3686c87f08caSChris Mason restart:
36873fd0a558SYan, Zheng 		if (update_backref_cache(trans, &rc->backref_cache)) {
36883a45bb20SJeff Mahoney 			btrfs_end_transaction(trans);
368942a657f5SPan Bian 			trans = NULL;
36903fd0a558SYan, Zheng 			continue;
36913fd0a558SYan, Zheng 		}
36923fd0a558SYan, Zheng 
3693147d256eSZhaolei 		ret = find_next_extent(rc, path, &key);
36945d4f98a2SYan Zheng 		if (ret < 0)
36955d4f98a2SYan Zheng 			err = ret;
36965d4f98a2SYan Zheng 		if (ret != 0)
36975d4f98a2SYan Zheng 			break;
36985d4f98a2SYan Zheng 
36995d4f98a2SYan Zheng 		rc->extents_found++;
37005d4f98a2SYan Zheng 
37015d4f98a2SYan Zheng 		ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
37025d4f98a2SYan Zheng 				    struct btrfs_extent_item);
37035d4f98a2SYan Zheng 		flags = btrfs_extent_flags(path->nodes[0], ei);
37045d4f98a2SYan Zheng 
37055d4f98a2SYan Zheng 		if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
37065d4f98a2SYan Zheng 			ret = add_tree_block(rc, &key, path, &blocks);
37075d4f98a2SYan Zheng 		} else if (rc->stage == UPDATE_DATA_PTRS &&
37085d4f98a2SYan Zheng 			   (flags & BTRFS_EXTENT_FLAG_DATA)) {
37095d4f98a2SYan Zheng 			ret = add_data_references(rc, &key, path, &blocks);
37105d4f98a2SYan Zheng 		} else {
3711b3b4aa74SDavid Sterba 			btrfs_release_path(path);
37125d4f98a2SYan Zheng 			ret = 0;
37135d4f98a2SYan Zheng 		}
37145d4f98a2SYan Zheng 		if (ret < 0) {
37153fd0a558SYan, Zheng 			err = ret;
37165d4f98a2SYan Zheng 			break;
37175d4f98a2SYan Zheng 		}
37185d4f98a2SYan Zheng 
37195d4f98a2SYan Zheng 		if (!RB_EMPTY_ROOT(&blocks)) {
37205d4f98a2SYan Zheng 			ret = relocate_tree_blocks(trans, rc, &blocks);
37215d4f98a2SYan Zheng 			if (ret < 0) {
37223fd0a558SYan, Zheng 				if (ret != -EAGAIN) {
37235d4f98a2SYan Zheng 					err = ret;
37245d4f98a2SYan Zheng 					break;
37255d4f98a2SYan Zheng 				}
37263fd0a558SYan, Zheng 				rc->extents_found--;
37273fd0a558SYan, Zheng 				rc->search_start = key.objectid;
37283fd0a558SYan, Zheng 			}
37295d4f98a2SYan Zheng 		}
37305d4f98a2SYan Zheng 
37313a45bb20SJeff Mahoney 		btrfs_end_transaction_throttle(trans);
37322ff7e61eSJeff Mahoney 		btrfs_btree_balance_dirty(fs_info);
37333fd0a558SYan, Zheng 		trans = NULL;
37345d4f98a2SYan Zheng 
37355d4f98a2SYan Zheng 		if (rc->stage == MOVE_DATA_EXTENTS &&
37365d4f98a2SYan Zheng 		    (flags & BTRFS_EXTENT_FLAG_DATA)) {
37375d4f98a2SYan Zheng 			rc->found_file_extent = 1;
37380257bb82SYan, Zheng 			ret = relocate_data_extent(rc->data_inode,
37393fd0a558SYan, Zheng 						   &key, &rc->cluster);
37405d4f98a2SYan Zheng 			if (ret < 0) {
37415d4f98a2SYan Zheng 				err = ret;
37425d4f98a2SYan Zheng 				break;
37435d4f98a2SYan Zheng 			}
37445d4f98a2SYan Zheng 		}
3745f31ea088SQu Wenruo 		if (btrfs_should_cancel_balance(fs_info)) {
3746f31ea088SQu Wenruo 			err = -ECANCELED;
3747f31ea088SQu Wenruo 			break;
3748f31ea088SQu Wenruo 		}
37495d4f98a2SYan Zheng 	}
3750c87f08caSChris Mason 	if (trans && progress && err == -ENOSPC) {
375143a7e99dSNikolay Borisov 		ret = btrfs_force_chunk_alloc(trans, rc->block_group->flags);
37529689457bSShilong Wang 		if (ret == 1) {
3753c87f08caSChris Mason 			err = 0;
3754c87f08caSChris Mason 			progress = 0;
3755c87f08caSChris Mason 			goto restart;
3756c87f08caSChris Mason 		}
3757c87f08caSChris Mason 	}
37583fd0a558SYan, Zheng 
3759b3b4aa74SDavid Sterba 	btrfs_release_path(path);
376091166212SDavid Sterba 	clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY);
37615d4f98a2SYan Zheng 
37625d4f98a2SYan Zheng 	if (trans) {
37633a45bb20SJeff Mahoney 		btrfs_end_transaction_throttle(trans);
37642ff7e61eSJeff Mahoney 		btrfs_btree_balance_dirty(fs_info);
37655d4f98a2SYan Zheng 	}
37665d4f98a2SYan Zheng 
37670257bb82SYan, Zheng 	if (!err) {
37683fd0a558SYan, Zheng 		ret = relocate_file_extent_cluster(rc->data_inode,
37693fd0a558SYan, Zheng 						   &rc->cluster);
37700257bb82SYan, Zheng 		if (ret < 0)
37710257bb82SYan, Zheng 			err = ret;
37720257bb82SYan, Zheng 	}
37730257bb82SYan, Zheng 
37743fd0a558SYan, Zheng 	rc->create_reloc_tree = 0;
37753fd0a558SYan, Zheng 	set_reloc_control(rc);
37760257bb82SYan, Zheng 
377713fe1bdbSQu Wenruo 	btrfs_backref_release_cache(&rc->backref_cache);
377863f018beSNikolay Borisov 	btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL);
37795d4f98a2SYan Zheng 
37807f913c7cSQu Wenruo 	/*
37817f913c7cSQu Wenruo 	 * Even in the case when the relocation is cancelled, we should all go
37827f913c7cSQu Wenruo 	 * through prepare_to_merge() and merge_reloc_roots().
37837f913c7cSQu Wenruo 	 *
37847f913c7cSQu Wenruo 	 * For error (including cancelled balance), prepare_to_merge() will
37857f913c7cSQu Wenruo 	 * mark all reloc trees orphan, then queue them for cleanup in
37867f913c7cSQu Wenruo 	 * merge_reloc_roots()
37877f913c7cSQu Wenruo 	 */
37883fd0a558SYan, Zheng 	err = prepare_to_merge(rc, err);
37895d4f98a2SYan Zheng 
37905d4f98a2SYan Zheng 	merge_reloc_roots(rc);
37915d4f98a2SYan Zheng 
37923fd0a558SYan, Zheng 	rc->merge_reloc_tree = 0;
37935d4f98a2SYan Zheng 	unset_reloc_control(rc);
379463f018beSNikolay Borisov 	btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL);
37955d4f98a2SYan Zheng 
37965d4f98a2SYan Zheng 	/* get rid of pinned extents */
37977a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(rc->extent_root);
379862b99540SQu Wenruo 	if (IS_ERR(trans)) {
37993612b495STsutomu Itoh 		err = PTR_ERR(trans);
380062b99540SQu Wenruo 		goto out_free;
380162b99540SQu Wenruo 	}
3802fb686c68SJosef Bacik 	ret = btrfs_commit_transaction(trans);
3803fb686c68SJosef Bacik 	if (ret && !err)
3804fb686c68SJosef Bacik 		err = ret;
38056217b0faSJosef Bacik out_free:
3806d2311e69SQu Wenruo 	ret = clean_dirty_subvols(rc);
3807d2311e69SQu Wenruo 	if (ret < 0 && !err)
3808d2311e69SQu Wenruo 		err = ret;
38092ff7e61eSJeff Mahoney 	btrfs_free_block_rsv(fs_info, rc->block_rsv);
38103fd0a558SYan, Zheng 	btrfs_free_path(path);
38115d4f98a2SYan Zheng 	return err;
38125d4f98a2SYan Zheng }
38135d4f98a2SYan Zheng 
__insert_orphan_inode(struct btrfs_trans_handle * trans,struct btrfs_root * root,u64 objectid)38145d4f98a2SYan Zheng static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
38150257bb82SYan, Zheng 				 struct btrfs_root *root, u64 objectid)
38165d4f98a2SYan Zheng {
38175d4f98a2SYan Zheng 	struct btrfs_path *path;
38185d4f98a2SYan Zheng 	struct btrfs_inode_item *item;
38195d4f98a2SYan Zheng 	struct extent_buffer *leaf;
38205d4f98a2SYan Zheng 	int ret;
38215d4f98a2SYan Zheng 
38225d4f98a2SYan Zheng 	path = btrfs_alloc_path();
38235d4f98a2SYan Zheng 	if (!path)
38245d4f98a2SYan Zheng 		return -ENOMEM;
38255d4f98a2SYan Zheng 
38265d4f98a2SYan Zheng 	ret = btrfs_insert_empty_inode(trans, root, path, objectid);
38275d4f98a2SYan Zheng 	if (ret)
38285d4f98a2SYan Zheng 		goto out;
38295d4f98a2SYan Zheng 
38305d4f98a2SYan Zheng 	leaf = path->nodes[0];
38315d4f98a2SYan Zheng 	item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
3832b159fa28SDavid Sterba 	memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
38335d4f98a2SYan Zheng 	btrfs_set_inode_generation(leaf, item, 1);
38340257bb82SYan, Zheng 	btrfs_set_inode_size(leaf, item, 0);
38355d4f98a2SYan Zheng 	btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
3836960a3166SJohannes Thumshirn 	btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS |
3837960a3166SJohannes Thumshirn 					  BTRFS_INODE_PREALLOC);
3838*d5e09e38SFilipe Manana 	btrfs_mark_buffer_dirty(trans, leaf);
38395d4f98a2SYan Zheng out:
38405d4f98a2SYan Zheng 	btrfs_free_path(path);
38415d4f98a2SYan Zheng 	return ret;
38425d4f98a2SYan Zheng }
38435d4f98a2SYan Zheng 
delete_orphan_inode(struct btrfs_trans_handle * trans,struct btrfs_root * root,u64 objectid)3844790c1b8cSJosef Bacik static void delete_orphan_inode(struct btrfs_trans_handle *trans,
3845790c1b8cSJosef Bacik 				struct btrfs_root *root, u64 objectid)
3846790c1b8cSJosef Bacik {
3847790c1b8cSJosef Bacik 	struct btrfs_path *path;
3848790c1b8cSJosef Bacik 	struct btrfs_key key;
3849790c1b8cSJosef Bacik 	int ret = 0;
3850790c1b8cSJosef Bacik 
3851790c1b8cSJosef Bacik 	path = btrfs_alloc_path();
3852790c1b8cSJosef Bacik 	if (!path) {
3853790c1b8cSJosef Bacik 		ret = -ENOMEM;
3854790c1b8cSJosef Bacik 		goto out;
3855790c1b8cSJosef Bacik 	}
3856790c1b8cSJosef Bacik 
3857790c1b8cSJosef Bacik 	key.objectid = objectid;
3858790c1b8cSJosef Bacik 	key.type = BTRFS_INODE_ITEM_KEY;
3859790c1b8cSJosef Bacik 	key.offset = 0;
3860790c1b8cSJosef Bacik 	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3861790c1b8cSJosef Bacik 	if (ret) {
3862790c1b8cSJosef Bacik 		if (ret > 0)
3863790c1b8cSJosef Bacik 			ret = -ENOENT;
3864790c1b8cSJosef Bacik 		goto out;
3865790c1b8cSJosef Bacik 	}
3866790c1b8cSJosef Bacik 	ret = btrfs_del_item(trans, root, path);
3867790c1b8cSJosef Bacik out:
3868790c1b8cSJosef Bacik 	if (ret)
3869790c1b8cSJosef Bacik 		btrfs_abort_transaction(trans, ret);
3870790c1b8cSJosef Bacik 	btrfs_free_path(path);
3871790c1b8cSJosef Bacik }
3872790c1b8cSJosef Bacik 
38735d4f98a2SYan Zheng /*
38745d4f98a2SYan Zheng  * helper to create inode for data relocation.
38755d4f98a2SYan Zheng  * the inode is in data relocation tree and its link count is 0
38765d4f98a2SYan Zheng  */
38773fd0a558SYan, Zheng static noinline_for_stack
create_reloc_inode(struct btrfs_fs_info * fs_info,struct btrfs_block_group * group)38783fd0a558SYan, Zheng struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
387932da5386SDavid Sterba 				 struct btrfs_block_group *group)
38805d4f98a2SYan Zheng {
38815d4f98a2SYan Zheng 	struct inode *inode = NULL;
38825d4f98a2SYan Zheng 	struct btrfs_trans_handle *trans;
38835d4f98a2SYan Zheng 	struct btrfs_root *root;
38844624900dSZhaolei 	u64 objectid;
38855d4f98a2SYan Zheng 	int err = 0;
38865d4f98a2SYan Zheng 
3887aeb935a4SQu Wenruo 	root = btrfs_grab_root(fs_info->data_reloc_root);
3888a22285a6SYan, Zheng 	trans = btrfs_start_transaction(root, 6);
388976deacf0SJosef Bacik 	if (IS_ERR(trans)) {
389000246528SJosef Bacik 		btrfs_put_root(root);
38913fd0a558SYan, Zheng 		return ERR_CAST(trans);
389276deacf0SJosef Bacik 	}
38935d4f98a2SYan Zheng 
3894543068a2SNikolay Borisov 	err = btrfs_get_free_objectid(root, &objectid);
38955d4f98a2SYan Zheng 	if (err)
38965d4f98a2SYan Zheng 		goto out;
38975d4f98a2SYan Zheng 
38980257bb82SYan, Zheng 	err = __insert_orphan_inode(trans, root, objectid);
3899790c1b8cSJosef Bacik 	if (err)
3900790c1b8cSJosef Bacik 		goto out;
39015d4f98a2SYan Zheng 
39020202e83fSDavid Sterba 	inode = btrfs_iget(fs_info->sb, objectid, root);
3903790c1b8cSJosef Bacik 	if (IS_ERR(inode)) {
3904790c1b8cSJosef Bacik 		delete_orphan_inode(trans, root, objectid);
3905790c1b8cSJosef Bacik 		err = PTR_ERR(inode);
3906790c1b8cSJosef Bacik 		inode = NULL;
3907790c1b8cSJosef Bacik 		goto out;
3908790c1b8cSJosef Bacik 	}
3909b3470b5dSDavid Sterba 	BTRFS_I(inode)->index_cnt = group->start;
39105d4f98a2SYan Zheng 
391173f2e545SNikolay Borisov 	err = btrfs_orphan_add(trans, BTRFS_I(inode));
39125d4f98a2SYan Zheng out:
391300246528SJosef Bacik 	btrfs_put_root(root);
39143a45bb20SJeff Mahoney 	btrfs_end_transaction(trans);
39152ff7e61eSJeff Mahoney 	btrfs_btree_balance_dirty(fs_info);
39165d4f98a2SYan Zheng 	if (err) {
39175d4f98a2SYan Zheng 		iput(inode);
39185d4f98a2SYan Zheng 		inode = ERR_PTR(err);
39195d4f98a2SYan Zheng 	}
39205d4f98a2SYan Zheng 	return inode;
39215d4f98a2SYan Zheng }
39225d4f98a2SYan Zheng 
3923907d2710SDavid Sterba /*
3924907d2710SDavid Sterba  * Mark start of chunk relocation that is cancellable. Check if the cancellation
3925907d2710SDavid Sterba  * has been requested meanwhile and don't start in that case.
3926907d2710SDavid Sterba  *
3927907d2710SDavid Sterba  * Return:
3928907d2710SDavid Sterba  *   0             success
3929907d2710SDavid Sterba  *   -EINPROGRESS  operation is already in progress, that's probably a bug
3930907d2710SDavid Sterba  *   -ECANCELED    cancellation request was set before the operation started
3931907d2710SDavid Sterba  */
reloc_chunk_start(struct btrfs_fs_info * fs_info)3932907d2710SDavid Sterba static int reloc_chunk_start(struct btrfs_fs_info *fs_info)
3933907d2710SDavid Sterba {
3934907d2710SDavid Sterba 	if (test_and_set_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags)) {
3935907d2710SDavid Sterba 		/* This should not happen */
3936907d2710SDavid Sterba 		btrfs_err(fs_info, "reloc already running, cannot start");
3937907d2710SDavid Sterba 		return -EINPROGRESS;
3938907d2710SDavid Sterba 	}
3939907d2710SDavid Sterba 
3940907d2710SDavid Sterba 	if (atomic_read(&fs_info->reloc_cancel_req) > 0) {
3941907d2710SDavid Sterba 		btrfs_info(fs_info, "chunk relocation canceled on start");
3942907d2710SDavid Sterba 		/*
3943907d2710SDavid Sterba 		 * On cancel, clear all requests but let the caller mark
3944907d2710SDavid Sterba 		 * the end after cleanup operations.
3945907d2710SDavid Sterba 		 */
3946907d2710SDavid Sterba 		atomic_set(&fs_info->reloc_cancel_req, 0);
3947907d2710SDavid Sterba 		return -ECANCELED;
3948907d2710SDavid Sterba 	}
3949907d2710SDavid Sterba 	return 0;
3950907d2710SDavid Sterba }
3951907d2710SDavid Sterba 
3952907d2710SDavid Sterba /*
3953907d2710SDavid Sterba  * Mark end of chunk relocation that is cancellable and wake any waiters.
3954907d2710SDavid Sterba  */
reloc_chunk_end(struct btrfs_fs_info * fs_info)3955907d2710SDavid Sterba static void reloc_chunk_end(struct btrfs_fs_info *fs_info)
3956907d2710SDavid Sterba {
3957907d2710SDavid Sterba 	/* Requested after start, clear bit first so any waiters can continue */
3958907d2710SDavid Sterba 	if (atomic_read(&fs_info->reloc_cancel_req) > 0)
3959907d2710SDavid Sterba 		btrfs_info(fs_info, "chunk relocation canceled during operation");
3960907d2710SDavid Sterba 	clear_and_wake_up_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags);
3961907d2710SDavid Sterba 	atomic_set(&fs_info->reloc_cancel_req, 0);
3962907d2710SDavid Sterba }
3963907d2710SDavid Sterba 
alloc_reloc_control(struct btrfs_fs_info * fs_info)3964c258d6e3SQu Wenruo static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info)
39653fd0a558SYan, Zheng {
39663fd0a558SYan, Zheng 	struct reloc_control *rc;
39673fd0a558SYan, Zheng 
39683fd0a558SYan, Zheng 	rc = kzalloc(sizeof(*rc), GFP_NOFS);
39693fd0a558SYan, Zheng 	if (!rc)
39703fd0a558SYan, Zheng 		return NULL;
39713fd0a558SYan, Zheng 
39723fd0a558SYan, Zheng 	INIT_LIST_HEAD(&rc->reloc_roots);
3973d2311e69SQu Wenruo 	INIT_LIST_HEAD(&rc->dirty_subvol_roots);
3974584fb121SQu Wenruo 	btrfs_backref_init_cache(fs_info, &rc->backref_cache, 1);
39753fd0a558SYan, Zheng 	mapping_tree_init(&rc->reloc_root_tree);
397635da5a7eSDavid Sterba 	extent_io_tree_init(fs_info, &rc->processed_blocks, IO_TREE_RELOC_BLOCKS);
39773fd0a558SYan, Zheng 	return rc;
39783fd0a558SYan, Zheng }
39793fd0a558SYan, Zheng 
free_reloc_control(struct reloc_control * rc)39801a0afa0eSJosef Bacik static void free_reloc_control(struct reloc_control *rc)
39811a0afa0eSJosef Bacik {
39821a0afa0eSJosef Bacik 	struct mapping_node *node, *tmp;
39831a0afa0eSJosef Bacik 
39841a0afa0eSJosef Bacik 	free_reloc_roots(&rc->reloc_roots);
39851a0afa0eSJosef Bacik 	rbtree_postorder_for_each_entry_safe(node, tmp,
39861a0afa0eSJosef Bacik 			&rc->reloc_root_tree.rb_root, rb_node)
39871a0afa0eSJosef Bacik 		kfree(node);
39881a0afa0eSJosef Bacik 
39891a0afa0eSJosef Bacik 	kfree(rc);
39901a0afa0eSJosef Bacik }
39911a0afa0eSJosef Bacik 
39925d4f98a2SYan Zheng /*
3993ebce0e01SAdam Borowski  * Print the block group being relocated
3994ebce0e01SAdam Borowski  */
describe_relocation(struct btrfs_fs_info * fs_info,struct btrfs_block_group * block_group)3995ebce0e01SAdam Borowski static void describe_relocation(struct btrfs_fs_info *fs_info,
399632da5386SDavid Sterba 				struct btrfs_block_group *block_group)
3997ebce0e01SAdam Borowski {
3998f89e09cfSAnand Jain 	char buf[128] = {'\0'};
3999ebce0e01SAdam Borowski 
4000f89e09cfSAnand Jain 	btrfs_describe_block_groups(block_group->flags, buf, sizeof(buf));
4001ebce0e01SAdam Borowski 
4002ebce0e01SAdam Borowski 	btrfs_info(fs_info,
4003ebce0e01SAdam Borowski 		   "relocating block group %llu flags %s",
4004b3470b5dSDavid Sterba 		   block_group->start, buf);
4005ebce0e01SAdam Borowski }
4006ebce0e01SAdam Borowski 
stage_to_string(int stage)4007430640e3SQu Wenruo static const char *stage_to_string(int stage)
4008430640e3SQu Wenruo {
4009430640e3SQu Wenruo 	if (stage == MOVE_DATA_EXTENTS)
4010430640e3SQu Wenruo 		return "move data extents";
4011430640e3SQu Wenruo 	if (stage == UPDATE_DATA_PTRS)
4012430640e3SQu Wenruo 		return "update data pointers";
4013430640e3SQu Wenruo 	return "unknown";
4014430640e3SQu Wenruo }
4015430640e3SQu Wenruo 
4016ebce0e01SAdam Borowski /*
40175d4f98a2SYan Zheng  * function to relocate all extents in a block group.
40185d4f98a2SYan Zheng  */
btrfs_relocate_block_group(struct btrfs_fs_info * fs_info,u64 group_start)40196bccf3abSJeff Mahoney int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start)
40205d4f98a2SYan Zheng {
402132da5386SDavid Sterba 	struct btrfs_block_group *bg;
402229cbcf40SJosef Bacik 	struct btrfs_root *extent_root = btrfs_extent_root(fs_info, group_start);
40235d4f98a2SYan Zheng 	struct reloc_control *rc;
40240af3d00bSJosef Bacik 	struct inode *inode;
40250af3d00bSJosef Bacik 	struct btrfs_path *path;
40265d4f98a2SYan Zheng 	int ret;
4027f0486c68SYan, Zheng 	int rw = 0;
40285d4f98a2SYan Zheng 	int err = 0;
40295d4f98a2SYan Zheng 
4030b4be6aefSJosef Bacik 	/*
4031b4be6aefSJosef Bacik 	 * This only gets set if we had a half-deleted snapshot on mount.  We
4032b4be6aefSJosef Bacik 	 * cannot allow relocation to start while we're still trying to clean up
4033b4be6aefSJosef Bacik 	 * these pending deletions.
4034b4be6aefSJosef Bacik 	 */
4035b4be6aefSJosef Bacik 	ret = wait_on_bit(&fs_info->flags, BTRFS_FS_UNFINISHED_DROPS, TASK_INTERRUPTIBLE);
4036b4be6aefSJosef Bacik 	if (ret)
4037b4be6aefSJosef Bacik 		return ret;
4038b4be6aefSJosef Bacik 
4039b4be6aefSJosef Bacik 	/* We may have been woken up by close_ctree, so bail if we're closing. */
4040b4be6aefSJosef Bacik 	if (btrfs_fs_closing(fs_info))
4041b4be6aefSJosef Bacik 		return -EINTR;
4042b4be6aefSJosef Bacik 
4043eede2bf3SOmar Sandoval 	bg = btrfs_lookup_block_group(fs_info, group_start);
4044eede2bf3SOmar Sandoval 	if (!bg)
4045eede2bf3SOmar Sandoval 		return -ENOENT;
4046eede2bf3SOmar Sandoval 
40470320b353SNaohiro Aota 	/*
40480320b353SNaohiro Aota 	 * Relocation of a data block group creates ordered extents.  Without
40490320b353SNaohiro Aota 	 * sb_start_write(), we can freeze the filesystem while unfinished
40500320b353SNaohiro Aota 	 * ordered extents are left. Such ordered extents can cause a deadlock
40510320b353SNaohiro Aota 	 * e.g. when syncfs() is waiting for their completion but they can't
40520320b353SNaohiro Aota 	 * finish because they block when joining a transaction, due to the
40530320b353SNaohiro Aota 	 * fact that the freeze locks are being held in write mode.
40540320b353SNaohiro Aota 	 */
40550320b353SNaohiro Aota 	if (bg->flags & BTRFS_BLOCK_GROUP_DATA)
40560320b353SNaohiro Aota 		ASSERT(sb_write_started(fs_info->sb));
40570320b353SNaohiro Aota 
4058eede2bf3SOmar Sandoval 	if (btrfs_pinned_by_swapfile(fs_info, bg)) {
4059eede2bf3SOmar Sandoval 		btrfs_put_block_group(bg);
4060eede2bf3SOmar Sandoval 		return -ETXTBSY;
4061eede2bf3SOmar Sandoval 	}
4062eede2bf3SOmar Sandoval 
4063c258d6e3SQu Wenruo 	rc = alloc_reloc_control(fs_info);
4064eede2bf3SOmar Sandoval 	if (!rc) {
4065eede2bf3SOmar Sandoval 		btrfs_put_block_group(bg);
40665d4f98a2SYan Zheng 		return -ENOMEM;
4067eede2bf3SOmar Sandoval 	}
40685d4f98a2SYan Zheng 
4069907d2710SDavid Sterba 	ret = reloc_chunk_start(fs_info);
4070907d2710SDavid Sterba 	if (ret < 0) {
4071907d2710SDavid Sterba 		err = ret;
4072907d2710SDavid Sterba 		goto out_put_bg;
4073907d2710SDavid Sterba 	}
4074907d2710SDavid Sterba 
4075f0486c68SYan, Zheng 	rc->extent_root = extent_root;
4076eede2bf3SOmar Sandoval 	rc->block_group = bg;
40775d4f98a2SYan Zheng 
4078b12de528SQu Wenruo 	ret = btrfs_inc_block_group_ro(rc->block_group, true);
4079f0486c68SYan, Zheng 	if (ret) {
4080f0486c68SYan, Zheng 		err = ret;
4081f0486c68SYan, Zheng 		goto out;
4082f0486c68SYan, Zheng 	}
4083f0486c68SYan, Zheng 	rw = 1;
4084f0486c68SYan, Zheng 
40850af3d00bSJosef Bacik 	path = btrfs_alloc_path();
40860af3d00bSJosef Bacik 	if (!path) {
40870af3d00bSJosef Bacik 		err = -ENOMEM;
40880af3d00bSJosef Bacik 		goto out;
40890af3d00bSJosef Bacik 	}
40900af3d00bSJosef Bacik 
40917949f339SDavid Sterba 	inode = lookup_free_space_inode(rc->block_group, path);
40920af3d00bSJosef Bacik 	btrfs_free_path(path);
40930af3d00bSJosef Bacik 
40940af3d00bSJosef Bacik 	if (!IS_ERR(inode))
40951bbc621eSChris Mason 		ret = delete_block_group_cache(fs_info, rc->block_group, inode, 0);
40960af3d00bSJosef Bacik 	else
40970af3d00bSJosef Bacik 		ret = PTR_ERR(inode);
40980af3d00bSJosef Bacik 
40990af3d00bSJosef Bacik 	if (ret && ret != -ENOENT) {
41000af3d00bSJosef Bacik 		err = ret;
41010af3d00bSJosef Bacik 		goto out;
41020af3d00bSJosef Bacik 	}
41030af3d00bSJosef Bacik 
41045d4f98a2SYan Zheng 	rc->data_inode = create_reloc_inode(fs_info, rc->block_group);
41055d4f98a2SYan Zheng 	if (IS_ERR(rc->data_inode)) {
41065d4f98a2SYan Zheng 		err = PTR_ERR(rc->data_inode);
41075d4f98a2SYan Zheng 		rc->data_inode = NULL;
41085d4f98a2SYan Zheng 		goto out;
41095d4f98a2SYan Zheng 	}
41105d4f98a2SYan Zheng 
41110b246afaSJeff Mahoney 	describe_relocation(fs_info, rc->block_group);
41125d4f98a2SYan Zheng 
41139cfa3e34SFilipe Manana 	btrfs_wait_block_group_reservations(rc->block_group);
4114f78c436cSFilipe Manana 	btrfs_wait_nocow_writers(rc->block_group);
41156374e57aSChris Mason 	btrfs_wait_ordered_roots(fs_info, U64_MAX,
4116b3470b5dSDavid Sterba 				 rc->block_group->start,
4117b3470b5dSDavid Sterba 				 rc->block_group->length);
41185d4f98a2SYan Zheng 
41197ae9bd18SNaohiro Aota 	ret = btrfs_zone_finish(rc->block_group);
41207ae9bd18SNaohiro Aota 	WARN_ON(ret && ret != -EAGAIN);
41217ae9bd18SNaohiro Aota 
41225d4f98a2SYan Zheng 	while (1) {
4123430640e3SQu Wenruo 		int finishes_stage;
4124430640e3SQu Wenruo 
412576dda93cSYan, Zheng 		mutex_lock(&fs_info->cleaner_mutex);
41265d4f98a2SYan Zheng 		ret = relocate_block_group(rc);
412776dda93cSYan, Zheng 		mutex_unlock(&fs_info->cleaner_mutex);
4128ff612ba7SJosef Bacik 		if (ret < 0)
41295d4f98a2SYan Zheng 			err = ret;
4130ff612ba7SJosef Bacik 
4131430640e3SQu Wenruo 		finishes_stage = rc->stage;
4132ff612ba7SJosef Bacik 		/*
4133ff612ba7SJosef Bacik 		 * We may have gotten ENOSPC after we already dirtied some
4134ff612ba7SJosef Bacik 		 * extents.  If writeout happens while we're relocating a
4135ff612ba7SJosef Bacik 		 * different block group we could end up hitting the
4136ff612ba7SJosef Bacik 		 * BUG_ON(rc->stage == UPDATE_DATA_PTRS) in
4137ff612ba7SJosef Bacik 		 * btrfs_reloc_cow_block.  Make sure we write everything out
4138ff612ba7SJosef Bacik 		 * properly so we don't trip over this problem, and then break
4139ff612ba7SJosef Bacik 		 * out of the loop if we hit an error.
4140ff612ba7SJosef Bacik 		 */
4141ff612ba7SJosef Bacik 		if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) {
4142ff612ba7SJosef Bacik 			ret = btrfs_wait_ordered_range(rc->data_inode, 0,
4143ff612ba7SJosef Bacik 						       (u64)-1);
4144ff612ba7SJosef Bacik 			if (ret)
4145ff612ba7SJosef Bacik 				err = ret;
4146ff612ba7SJosef Bacik 			invalidate_mapping_pages(rc->data_inode->i_mapping,
4147ff612ba7SJosef Bacik 						 0, -1);
4148ff612ba7SJosef Bacik 			rc->stage = UPDATE_DATA_PTRS;
41495d4f98a2SYan Zheng 		}
41505d4f98a2SYan Zheng 
4151ff612ba7SJosef Bacik 		if (err < 0)
4152ff612ba7SJosef Bacik 			goto out;
4153ff612ba7SJosef Bacik 
41545d4f98a2SYan Zheng 		if (rc->extents_found == 0)
41555d4f98a2SYan Zheng 			break;
41565d4f98a2SYan Zheng 
4157430640e3SQu Wenruo 		btrfs_info(fs_info, "found %llu extents, stage: %s",
4158430640e3SQu Wenruo 			   rc->extents_found, stage_to_string(finishes_stage));
41595d4f98a2SYan Zheng 	}
41605d4f98a2SYan Zheng 
41615d4f98a2SYan Zheng 	WARN_ON(rc->block_group->pinned > 0);
41625d4f98a2SYan Zheng 	WARN_ON(rc->block_group->reserved > 0);
4163bf38be65SDavid Sterba 	WARN_ON(rc->block_group->used > 0);
41645d4f98a2SYan Zheng out:
4165f0486c68SYan, Zheng 	if (err && rw)
41662ff7e61eSJeff Mahoney 		btrfs_dec_block_group_ro(rc->block_group);
41675d4f98a2SYan Zheng 	iput(rc->data_inode);
4168907d2710SDavid Sterba out_put_bg:
4169907d2710SDavid Sterba 	btrfs_put_block_group(bg);
4170907d2710SDavid Sterba 	reloc_chunk_end(fs_info);
41711a0afa0eSJosef Bacik 	free_reloc_control(rc);
41725d4f98a2SYan Zheng 	return err;
41735d4f98a2SYan Zheng }
41745d4f98a2SYan Zheng 
mark_garbage_root(struct btrfs_root * root)417576dda93cSYan, Zheng static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
417676dda93cSYan, Zheng {
41770b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
417876dda93cSYan, Zheng 	struct btrfs_trans_handle *trans;
417979787eaaSJeff Mahoney 	int ret, err;
418076dda93cSYan, Zheng 
41810b246afaSJeff Mahoney 	trans = btrfs_start_transaction(fs_info->tree_root, 0);
418279787eaaSJeff Mahoney 	if (IS_ERR(trans))
418379787eaaSJeff Mahoney 		return PTR_ERR(trans);
418476dda93cSYan, Zheng 
418576dda93cSYan, Zheng 	memset(&root->root_item.drop_progress, 0,
418676dda93cSYan, Zheng 		sizeof(root->root_item.drop_progress));
4187c8422684SDavid Sterba 	btrfs_set_root_drop_level(&root->root_item, 0);
418876dda93cSYan, Zheng 	btrfs_set_root_refs(&root->root_item, 0);
41890b246afaSJeff Mahoney 	ret = btrfs_update_root(trans, fs_info->tree_root,
419076dda93cSYan, Zheng 				&root->root_key, &root->root_item);
419176dda93cSYan, Zheng 
41923a45bb20SJeff Mahoney 	err = btrfs_end_transaction(trans);
419379787eaaSJeff Mahoney 	if (err)
419479787eaaSJeff Mahoney 		return err;
419579787eaaSJeff Mahoney 	return ret;
419676dda93cSYan, Zheng }
419776dda93cSYan, Zheng 
41985d4f98a2SYan Zheng /*
41995d4f98a2SYan Zheng  * recover relocation interrupted by system crash.
42005d4f98a2SYan Zheng  *
42015d4f98a2SYan Zheng  * this function resumes merging reloc trees with corresponding fs trees.
42025d4f98a2SYan Zheng  * this is important for keeping the sharing of tree blocks
42035d4f98a2SYan Zheng  */
btrfs_recover_relocation(struct btrfs_fs_info * fs_info)42047eefae6bSJosef Bacik int btrfs_recover_relocation(struct btrfs_fs_info *fs_info)
42055d4f98a2SYan Zheng {
42065d4f98a2SYan Zheng 	LIST_HEAD(reloc_roots);
42075d4f98a2SYan Zheng 	struct btrfs_key key;
42085d4f98a2SYan Zheng 	struct btrfs_root *fs_root;
42095d4f98a2SYan Zheng 	struct btrfs_root *reloc_root;
42105d4f98a2SYan Zheng 	struct btrfs_path *path;
42115d4f98a2SYan Zheng 	struct extent_buffer *leaf;
42125d4f98a2SYan Zheng 	struct reloc_control *rc = NULL;
42135d4f98a2SYan Zheng 	struct btrfs_trans_handle *trans;
42145d4f98a2SYan Zheng 	int ret;
42155d4f98a2SYan Zheng 	int err = 0;
42165d4f98a2SYan Zheng 
42175d4f98a2SYan Zheng 	path = btrfs_alloc_path();
42185d4f98a2SYan Zheng 	if (!path)
42195d4f98a2SYan Zheng 		return -ENOMEM;
4220e4058b54SDavid Sterba 	path->reada = READA_BACK;
42215d4f98a2SYan Zheng 
42225d4f98a2SYan Zheng 	key.objectid = BTRFS_TREE_RELOC_OBJECTID;
42235d4f98a2SYan Zheng 	key.type = BTRFS_ROOT_ITEM_KEY;
42245d4f98a2SYan Zheng 	key.offset = (u64)-1;
42255d4f98a2SYan Zheng 
42265d4f98a2SYan Zheng 	while (1) {
42270b246afaSJeff Mahoney 		ret = btrfs_search_slot(NULL, fs_info->tree_root, &key,
42285d4f98a2SYan Zheng 					path, 0, 0);
42295d4f98a2SYan Zheng 		if (ret < 0) {
42305d4f98a2SYan Zheng 			err = ret;
42315d4f98a2SYan Zheng 			goto out;
42325d4f98a2SYan Zheng 		}
42335d4f98a2SYan Zheng 		if (ret > 0) {
42345d4f98a2SYan Zheng 			if (path->slots[0] == 0)
42355d4f98a2SYan Zheng 				break;
42365d4f98a2SYan Zheng 			path->slots[0]--;
42375d4f98a2SYan Zheng 		}
42385d4f98a2SYan Zheng 		leaf = path->nodes[0];
42395d4f98a2SYan Zheng 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4240b3b4aa74SDavid Sterba 		btrfs_release_path(path);
42415d4f98a2SYan Zheng 
42425d4f98a2SYan Zheng 		if (key.objectid != BTRFS_TREE_RELOC_OBJECTID ||
42435d4f98a2SYan Zheng 		    key.type != BTRFS_ROOT_ITEM_KEY)
42445d4f98a2SYan Zheng 			break;
42455d4f98a2SYan Zheng 
42467eefae6bSJosef Bacik 		reloc_root = btrfs_read_tree_root(fs_info->tree_root, &key);
42475d4f98a2SYan Zheng 		if (IS_ERR(reloc_root)) {
42485d4f98a2SYan Zheng 			err = PTR_ERR(reloc_root);
42495d4f98a2SYan Zheng 			goto out;
42505d4f98a2SYan Zheng 		}
42515d4f98a2SYan Zheng 
425292a7cc42SQu Wenruo 		set_bit(BTRFS_ROOT_SHAREABLE, &reloc_root->state);
42535d4f98a2SYan Zheng 		list_add(&reloc_root->root_list, &reloc_roots);
42545d4f98a2SYan Zheng 
42555d4f98a2SYan Zheng 		if (btrfs_root_refs(&reloc_root->root_item) > 0) {
4256a820feb5SDavid Sterba 			fs_root = btrfs_get_fs_root(fs_info,
4257a820feb5SDavid Sterba 					reloc_root->root_key.offset, false);
42585d4f98a2SYan Zheng 			if (IS_ERR(fs_root)) {
425976dda93cSYan, Zheng 				ret = PTR_ERR(fs_root);
426076dda93cSYan, Zheng 				if (ret != -ENOENT) {
426176dda93cSYan, Zheng 					err = ret;
42625d4f98a2SYan Zheng 					goto out;
42635d4f98a2SYan Zheng 				}
426479787eaaSJeff Mahoney 				ret = mark_garbage_root(reloc_root);
426579787eaaSJeff Mahoney 				if (ret < 0) {
426679787eaaSJeff Mahoney 					err = ret;
426779787eaaSJeff Mahoney 					goto out;
426879787eaaSJeff Mahoney 				}
4269932fd26dSJosef Bacik 			} else {
427000246528SJosef Bacik 				btrfs_put_root(fs_root);
427176dda93cSYan, Zheng 			}
42725d4f98a2SYan Zheng 		}
42735d4f98a2SYan Zheng 
42745d4f98a2SYan Zheng 		if (key.offset == 0)
42755d4f98a2SYan Zheng 			break;
42765d4f98a2SYan Zheng 
42775d4f98a2SYan Zheng 		key.offset--;
42785d4f98a2SYan Zheng 	}
4279b3b4aa74SDavid Sterba 	btrfs_release_path(path);
42805d4f98a2SYan Zheng 
42815d4f98a2SYan Zheng 	if (list_empty(&reloc_roots))
42825d4f98a2SYan Zheng 		goto out;
42835d4f98a2SYan Zheng 
4284c258d6e3SQu Wenruo 	rc = alloc_reloc_control(fs_info);
42855d4f98a2SYan Zheng 	if (!rc) {
42865d4f98a2SYan Zheng 		err = -ENOMEM;
42875d4f98a2SYan Zheng 		goto out;
42885d4f98a2SYan Zheng 	}
42895d4f98a2SYan Zheng 
4290907d2710SDavid Sterba 	ret = reloc_chunk_start(fs_info);
4291907d2710SDavid Sterba 	if (ret < 0) {
4292907d2710SDavid Sterba 		err = ret;
4293907d2710SDavid Sterba 		goto out_end;
4294907d2710SDavid Sterba 	}
4295907d2710SDavid Sterba 
429629cbcf40SJosef Bacik 	rc->extent_root = btrfs_extent_root(fs_info, 0);
42975d4f98a2SYan Zheng 
42985d4f98a2SYan Zheng 	set_reloc_control(rc);
42995d4f98a2SYan Zheng 
43007a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(rc->extent_root);
43013612b495STsutomu Itoh 	if (IS_ERR(trans)) {
43023612b495STsutomu Itoh 		err = PTR_ERR(trans);
4303fb2d83eeSJosef Bacik 		goto out_unset;
43043612b495STsutomu Itoh 	}
43053fd0a558SYan, Zheng 
43063fd0a558SYan, Zheng 	rc->merge_reloc_tree = 1;
43073fd0a558SYan, Zheng 
43085d4f98a2SYan Zheng 	while (!list_empty(&reloc_roots)) {
43095d4f98a2SYan Zheng 		reloc_root = list_entry(reloc_roots.next,
43105d4f98a2SYan Zheng 					struct btrfs_root, root_list);
43115d4f98a2SYan Zheng 		list_del(&reloc_root->root_list);
43125d4f98a2SYan Zheng 
43135d4f98a2SYan Zheng 		if (btrfs_root_refs(&reloc_root->root_item) == 0) {
43145d4f98a2SYan Zheng 			list_add_tail(&reloc_root->root_list,
43155d4f98a2SYan Zheng 				      &rc->reloc_roots);
43165d4f98a2SYan Zheng 			continue;
43175d4f98a2SYan Zheng 		}
43185d4f98a2SYan Zheng 
4319a820feb5SDavid Sterba 		fs_root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
4320a820feb5SDavid Sterba 					    false);
432179787eaaSJeff Mahoney 		if (IS_ERR(fs_root)) {
432279787eaaSJeff Mahoney 			err = PTR_ERR(fs_root);
4323ca1aa281SJosef Bacik 			list_add_tail(&reloc_root->root_list, &reloc_roots);
43241402d17dSXiyu Yang 			btrfs_end_transaction(trans);
4325fb2d83eeSJosef Bacik 			goto out_unset;
432679787eaaSJeff Mahoney 		}
43275d4f98a2SYan Zheng 
4328ffd7b339SJeff Mahoney 		err = __add_reloc_root(reloc_root);
432957a304cfSJosef Bacik 		ASSERT(err != -EEXIST);
43303c925863SJosef Bacik 		if (err) {
43313c925863SJosef Bacik 			list_add_tail(&reloc_root->root_list, &reloc_roots);
43323c925863SJosef Bacik 			btrfs_put_root(fs_root);
43333c925863SJosef Bacik 			btrfs_end_transaction(trans);
43343c925863SJosef Bacik 			goto out_unset;
43353c925863SJosef Bacik 		}
4336f44deb74SJosef Bacik 		fs_root->reloc_root = btrfs_grab_root(reloc_root);
433700246528SJosef Bacik 		btrfs_put_root(fs_root);
43385d4f98a2SYan Zheng 	}
43395d4f98a2SYan Zheng 
43403a45bb20SJeff Mahoney 	err = btrfs_commit_transaction(trans);
434179787eaaSJeff Mahoney 	if (err)
4342fb2d83eeSJosef Bacik 		goto out_unset;
43435d4f98a2SYan Zheng 
43445d4f98a2SYan Zheng 	merge_reloc_roots(rc);
43455d4f98a2SYan Zheng 
43465d4f98a2SYan Zheng 	unset_reloc_control(rc);
43475d4f98a2SYan Zheng 
43487a7eaa40SJosef Bacik 	trans = btrfs_join_transaction(rc->extent_root);
434962b99540SQu Wenruo 	if (IS_ERR(trans)) {
43503612b495STsutomu Itoh 		err = PTR_ERR(trans);
43516217b0faSJosef Bacik 		goto out_clean;
435262b99540SQu Wenruo 	}
43533a45bb20SJeff Mahoney 	err = btrfs_commit_transaction(trans);
43546217b0faSJosef Bacik out_clean:
4355d2311e69SQu Wenruo 	ret = clean_dirty_subvols(rc);
4356d2311e69SQu Wenruo 	if (ret < 0 && !err)
4357d2311e69SQu Wenruo 		err = ret;
4358fb2d83eeSJosef Bacik out_unset:
4359fb2d83eeSJosef Bacik 	unset_reloc_control(rc);
4360907d2710SDavid Sterba out_end:
4361907d2710SDavid Sterba 	reloc_chunk_end(fs_info);
43621a0afa0eSJosef Bacik 	free_reloc_control(rc);
43633612b495STsutomu Itoh out:
4364aca1bba6SLiu Bo 	free_reloc_roots(&reloc_roots);
4365aca1bba6SLiu Bo 
43665d4f98a2SYan Zheng 	btrfs_free_path(path);
43675d4f98a2SYan Zheng 
43685d4f98a2SYan Zheng 	if (err == 0) {
43695d4f98a2SYan Zheng 		/* cleanup orphan inode in data relocation tree */
4370aeb935a4SQu Wenruo 		fs_root = btrfs_grab_root(fs_info->data_reloc_root);
4371aeb935a4SQu Wenruo 		ASSERT(fs_root);
437266b4ffd1SJosef Bacik 		err = btrfs_orphan_cleanup(fs_root);
437300246528SJosef Bacik 		btrfs_put_root(fs_root);
4374932fd26dSJosef Bacik 	}
43755d4f98a2SYan Zheng 	return err;
43765d4f98a2SYan Zheng }
43775d4f98a2SYan Zheng 
43785d4f98a2SYan Zheng /*
43795d4f98a2SYan Zheng  * helper to add ordered checksum for data relocation.
43805d4f98a2SYan Zheng  *
43815d4f98a2SYan Zheng  * cloning checksum properly handles the nodatasum extents.
43825d4f98a2SYan Zheng  * it also saves CPU time to re-calculate the checksum.
43835d4f98a2SYan Zheng  */
btrfs_reloc_clone_csums(struct btrfs_ordered_extent * ordered)438434bfaf15SChristoph Hellwig int btrfs_reloc_clone_csums(struct btrfs_ordered_extent *ordered)
43855d4f98a2SYan Zheng {
438634bfaf15SChristoph Hellwig 	struct btrfs_inode *inode = BTRFS_I(ordered->inode);
43877bfa9535SNikolay Borisov 	struct btrfs_fs_info *fs_info = inode->root->fs_info;
438834bfaf15SChristoph Hellwig 	u64 disk_bytenr = ordered->file_offset + inode->index_cnt;
438934bfaf15SChristoph Hellwig 	struct btrfs_root *csum_root = btrfs_csum_root(fs_info, disk_bytenr);
43905d4f98a2SYan Zheng 	LIST_HEAD(list);
439134bfaf15SChristoph Hellwig 	int ret;
43925d4f98a2SYan Zheng 
439397e38239SQu Wenruo 	ret = btrfs_lookup_csums_list(csum_root, disk_bytenr,
439434bfaf15SChristoph Hellwig 				      disk_bytenr + ordered->num_bytes - 1,
439534bfaf15SChristoph Hellwig 				      &list, 0, false);
439679787eaaSJeff Mahoney 	if (ret)
439734bfaf15SChristoph Hellwig 		return ret;
43985d4f98a2SYan Zheng 
43995d4f98a2SYan Zheng 	while (!list_empty(&list)) {
440034bfaf15SChristoph Hellwig 		struct btrfs_ordered_sum *sums =
440134bfaf15SChristoph Hellwig 			list_entry(list.next, struct btrfs_ordered_sum, list);
440234bfaf15SChristoph Hellwig 
44035d4f98a2SYan Zheng 		list_del_init(&sums->list);
44045d4f98a2SYan Zheng 
44054577b014SJosef Bacik 		/*
44064577b014SJosef Bacik 		 * We need to offset the new_bytenr based on where the csum is.
44074577b014SJosef Bacik 		 * We need to do this because we will read in entire prealloc
44084577b014SJosef Bacik 		 * extents but we may have written to say the middle of the
44094577b014SJosef Bacik 		 * prealloc extent, so we need to make sure the csum goes with
44104577b014SJosef Bacik 		 * the right disk offset.
44114577b014SJosef Bacik 		 *
44124577b014SJosef Bacik 		 * We can do this because the data reloc inode refers strictly
44134577b014SJosef Bacik 		 * to the on disk bytes, so we don't have to worry about
44144577b014SJosef Bacik 		 * disk_len vs real len like with real inodes since it's all
44154577b014SJosef Bacik 		 * disk length.
44164577b014SJosef Bacik 		 */
441734bfaf15SChristoph Hellwig 		sums->logical = ordered->disk_bytenr + sums->logical - disk_bytenr;
4418f9756261SNikolay Borisov 		btrfs_add_ordered_sum(ordered, sums);
44195d4f98a2SYan Zheng 	}
442034bfaf15SChristoph Hellwig 
442134bfaf15SChristoph Hellwig 	return 0;
44225d4f98a2SYan Zheng }
44233fd0a558SYan, Zheng 
btrfs_reloc_cow_block(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct extent_buffer * buf,struct extent_buffer * cow)442483d4cfd4SJosef Bacik int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
44253fd0a558SYan, Zheng 			  struct btrfs_root *root, struct extent_buffer *buf,
44263fd0a558SYan, Zheng 			  struct extent_buffer *cow)
44273fd0a558SYan, Zheng {
44280b246afaSJeff Mahoney 	struct btrfs_fs_info *fs_info = root->fs_info;
44293fd0a558SYan, Zheng 	struct reloc_control *rc;
4430a26195a5SQu Wenruo 	struct btrfs_backref_node *node;
44313fd0a558SYan, Zheng 	int first_cow = 0;
44323fd0a558SYan, Zheng 	int level;
443383d4cfd4SJosef Bacik 	int ret = 0;
44343fd0a558SYan, Zheng 
44350b246afaSJeff Mahoney 	rc = fs_info->reloc_ctl;
44363fd0a558SYan, Zheng 	if (!rc)
443783d4cfd4SJosef Bacik 		return 0;
44383fd0a558SYan, Zheng 
443937f00a6dSJohannes Thumshirn 	BUG_ON(rc->stage == UPDATE_DATA_PTRS && btrfs_is_data_reloc_root(root));
44403fd0a558SYan, Zheng 
44413fd0a558SYan, Zheng 	level = btrfs_header_level(buf);
44423fd0a558SYan, Zheng 	if (btrfs_header_generation(buf) <=
44433fd0a558SYan, Zheng 	    btrfs_root_last_snapshot(&root->root_item))
44443fd0a558SYan, Zheng 		first_cow = 1;
44453fd0a558SYan, Zheng 
44463fd0a558SYan, Zheng 	if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID &&
44473fd0a558SYan, Zheng 	    rc->create_reloc_tree) {
44483fd0a558SYan, Zheng 		WARN_ON(!first_cow && level == 0);
44493fd0a558SYan, Zheng 
44503fd0a558SYan, Zheng 		node = rc->backref_cache.path[level];
44513fd0a558SYan, Zheng 		BUG_ON(node->bytenr != buf->start &&
44523fd0a558SYan, Zheng 		       node->new_bytenr != buf->start);
44533fd0a558SYan, Zheng 
4454b0fe7078SQu Wenruo 		btrfs_backref_drop_node_buffer(node);
445567439dadSDavid Sterba 		atomic_inc(&cow->refs);
44563fd0a558SYan, Zheng 		node->eb = cow;
44573fd0a558SYan, Zheng 		node->new_bytenr = cow->start;
44583fd0a558SYan, Zheng 
44593fd0a558SYan, Zheng 		if (!node->pending) {
44603fd0a558SYan, Zheng 			list_move_tail(&node->list,
44613fd0a558SYan, Zheng 				       &rc->backref_cache.pending[level]);
44623fd0a558SYan, Zheng 			node->pending = 1;
44633fd0a558SYan, Zheng 		}
44643fd0a558SYan, Zheng 
44653fd0a558SYan, Zheng 		if (first_cow)
44669569cc20SQu Wenruo 			mark_block_processed(rc, node);
44673fd0a558SYan, Zheng 
44683fd0a558SYan, Zheng 		if (first_cow && level > 0)
44693fd0a558SYan, Zheng 			rc->nodes_relocated += buf->len;
44703fd0a558SYan, Zheng 	}
44713fd0a558SYan, Zheng 
447283d4cfd4SJosef Bacik 	if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS)
44733fd0a558SYan, Zheng 		ret = replace_file_extents(trans, rc, root, cow);
447483d4cfd4SJosef Bacik 	return ret;
44753fd0a558SYan, Zheng }
44763fd0a558SYan, Zheng 
44773fd0a558SYan, Zheng /*
44783fd0a558SYan, Zheng  * called before creating snapshot. it calculates metadata reservation
447901327610SNicholas D Steeves  * required for relocating tree blocks in the snapshot
44803fd0a558SYan, Zheng  */
btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot * pending,u64 * bytes_to_reserve)4481147d256eSZhaolei void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending,
44823fd0a558SYan, Zheng 			      u64 *bytes_to_reserve)
44833fd0a558SYan, Zheng {
448410995c04SQu Wenruo 	struct btrfs_root *root = pending->root;
448510995c04SQu Wenruo 	struct reloc_control *rc = root->fs_info->reloc_ctl;
44863fd0a558SYan, Zheng 
44876282675eSQu Wenruo 	if (!rc || !have_reloc_root(root))
44883fd0a558SYan, Zheng 		return;
44893fd0a558SYan, Zheng 
44903fd0a558SYan, Zheng 	if (!rc->merge_reloc_tree)
44913fd0a558SYan, Zheng 		return;
44923fd0a558SYan, Zheng 
44933fd0a558SYan, Zheng 	root = root->reloc_root;
44943fd0a558SYan, Zheng 	BUG_ON(btrfs_root_refs(&root->root_item) == 0);
44953fd0a558SYan, Zheng 	/*
44963fd0a558SYan, Zheng 	 * relocation is in the stage of merging trees. the space
44973fd0a558SYan, Zheng 	 * used by merging a reloc tree is twice the size of
44983fd0a558SYan, Zheng 	 * relocated tree nodes in the worst case. half for cowing
44993fd0a558SYan, Zheng 	 * the reloc tree, half for cowing the fs tree. the space
45003fd0a558SYan, Zheng 	 * used by cowing the reloc tree will be freed after the
45013fd0a558SYan, Zheng 	 * tree is dropped. if we create snapshot, cowing the fs
45023fd0a558SYan, Zheng 	 * tree may use more space than it frees. so we need
45033fd0a558SYan, Zheng 	 * reserve extra space.
45043fd0a558SYan, Zheng 	 */
45053fd0a558SYan, Zheng 	*bytes_to_reserve += rc->nodes_relocated;
45063fd0a558SYan, Zheng }
45073fd0a558SYan, Zheng 
45083fd0a558SYan, Zheng /*
45093fd0a558SYan, Zheng  * called after snapshot is created. migrate block reservation
45103fd0a558SYan, Zheng  * and create reloc root for the newly created snapshot
4511f44deb74SJosef Bacik  *
4512f44deb74SJosef Bacik  * This is similar to btrfs_init_reloc_root(), we come out of here with two
4513f44deb74SJosef Bacik  * references held on the reloc_root, one for root->reloc_root and one for
4514f44deb74SJosef Bacik  * rc->reloc_roots.
45153fd0a558SYan, Zheng  */
btrfs_reloc_post_snapshot(struct btrfs_trans_handle * trans,struct btrfs_pending_snapshot * pending)451649b25e05SJeff Mahoney int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
45173fd0a558SYan, Zheng 			       struct btrfs_pending_snapshot *pending)
45183fd0a558SYan, Zheng {
45193fd0a558SYan, Zheng 	struct btrfs_root *root = pending->root;
45203fd0a558SYan, Zheng 	struct btrfs_root *reloc_root;
45213fd0a558SYan, Zheng 	struct btrfs_root *new_root;
452210995c04SQu Wenruo 	struct reloc_control *rc = root->fs_info->reloc_ctl;
45233fd0a558SYan, Zheng 	int ret;
45243fd0a558SYan, Zheng 
45256282675eSQu Wenruo 	if (!rc || !have_reloc_root(root))
452649b25e05SJeff Mahoney 		return 0;
45273fd0a558SYan, Zheng 
45283fd0a558SYan, Zheng 	rc = root->fs_info->reloc_ctl;
45293fd0a558SYan, Zheng 	rc->merging_rsv_size += rc->nodes_relocated;
45303fd0a558SYan, Zheng 
45313fd0a558SYan, Zheng 	if (rc->merge_reloc_tree) {
45323fd0a558SYan, Zheng 		ret = btrfs_block_rsv_migrate(&pending->block_rsv,
45333fd0a558SYan, Zheng 					      rc->block_rsv,
45343a584174SLu Fengqi 					      rc->nodes_relocated, true);
453549b25e05SJeff Mahoney 		if (ret)
453649b25e05SJeff Mahoney 			return ret;
45373fd0a558SYan, Zheng 	}
45383fd0a558SYan, Zheng 
45393fd0a558SYan, Zheng 	new_root = pending->snap;
45403fd0a558SYan, Zheng 	reloc_root = create_reloc_root(trans, root->reloc_root,
45413fd0a558SYan, Zheng 				       new_root->root_key.objectid);
454249b25e05SJeff Mahoney 	if (IS_ERR(reloc_root))
454349b25e05SJeff Mahoney 		return PTR_ERR(reloc_root);
45443fd0a558SYan, Zheng 
4545ffd7b339SJeff Mahoney 	ret = __add_reloc_root(reloc_root);
454657a304cfSJosef Bacik 	ASSERT(ret != -EEXIST);
45473c925863SJosef Bacik 	if (ret) {
45483c925863SJosef Bacik 		/* Pairs with create_reloc_root */
45493c925863SJosef Bacik 		btrfs_put_root(reloc_root);
45503c925863SJosef Bacik 		return ret;
45513c925863SJosef Bacik 	}
4552f44deb74SJosef Bacik 	new_root->reloc_root = btrfs_grab_root(reloc_root);
45533fd0a558SYan, Zheng 
455449b25e05SJeff Mahoney 	if (rc->create_reloc_tree)
45553fd0a558SYan, Zheng 		ret = clone_backref_node(trans, rc, root, reloc_root);
455649b25e05SJeff Mahoney 	return ret;
45573fd0a558SYan, Zheng }
4558b9a9a850SQu Wenruo 
4559b9a9a850SQu Wenruo /*
4560b9a9a850SQu Wenruo  * Get the current bytenr for the block group which is being relocated.
4561b9a9a850SQu Wenruo  *
4562b9a9a850SQu Wenruo  * Return U64_MAX if no running relocation.
4563b9a9a850SQu Wenruo  */
btrfs_get_reloc_bg_bytenr(struct btrfs_fs_info * fs_info)4564b9a9a850SQu Wenruo u64 btrfs_get_reloc_bg_bytenr(struct btrfs_fs_info *fs_info)
4565b9a9a850SQu Wenruo {
4566b9a9a850SQu Wenruo 	u64 logical = U64_MAX;
4567b9a9a850SQu Wenruo 
4568b9a9a850SQu Wenruo 	lockdep_assert_held(&fs_info->reloc_mutex);
4569b9a9a850SQu Wenruo 
4570b9a9a850SQu Wenruo 	if (fs_info->reloc_ctl && fs_info->reloc_ctl->block_group)
4571b9a9a850SQu Wenruo 		logical = fs_info->reloc_ctl->block_group->start;
4572b9a9a850SQu Wenruo 	return logical;
4573b9a9a850SQu Wenruo }
4574