1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0 25d4f98a2SYan Zheng /* 35d4f98a2SYan Zheng * Copyright (C) 2009 Oracle. All rights reserved. 45d4f98a2SYan Zheng */ 55d4f98a2SYan Zheng 65d4f98a2SYan Zheng #include <linux/sched.h> 75d4f98a2SYan Zheng #include <linux/pagemap.h> 85d4f98a2SYan Zheng #include <linux/writeback.h> 95d4f98a2SYan Zheng #include <linux/blkdev.h> 105d4f98a2SYan Zheng #include <linux/rbtree.h> 115a0e3ad6STejun Heo #include <linux/slab.h> 12726a3421SQu Wenruo #include <linux/error-injection.h> 135d4f98a2SYan Zheng #include "ctree.h" 145d4f98a2SYan Zheng #include "disk-io.h" 155d4f98a2SYan Zheng #include "transaction.h" 165d4f98a2SYan Zheng #include "volumes.h" 175d4f98a2SYan Zheng #include "locking.h" 185d4f98a2SYan Zheng #include "btrfs_inode.h" 195d4f98a2SYan Zheng #include "async-thread.h" 200af3d00bSJosef Bacik #include "free-space-cache.h" 2162b99540SQu Wenruo #include "qgroup.h" 22cdccee99SLiu Bo #include "print-tree.h" 2386736342SJosef Bacik #include "delalloc-space.h" 24aac0023cSJosef Bacik #include "block-group.h" 2519b546d7SQu Wenruo #include "backref.h" 26e9a28dc5SQu Wenruo #include "misc.h" 275d4f98a2SYan Zheng 285d4f98a2SYan Zheng /* 290c891389SQu Wenruo * Relocation overview 300c891389SQu Wenruo * 310c891389SQu Wenruo * [What does relocation do] 320c891389SQu Wenruo * 330c891389SQu Wenruo * The objective of relocation is to relocate all extents of the target block 340c891389SQu Wenruo * group to other block groups. 350c891389SQu Wenruo * This is utilized by resize (shrink only), profile converting, compacting 360c891389SQu Wenruo * space, or balance routine to spread chunks over devices. 370c891389SQu Wenruo * 380c891389SQu Wenruo * Before | After 390c891389SQu Wenruo * ------------------------------------------------------------------ 400c891389SQu Wenruo * BG A: 10 data extents | BG A: deleted 410c891389SQu Wenruo * BG B: 2 data extents | BG B: 10 data extents (2 old + 8 relocated) 420c891389SQu Wenruo * BG C: 1 extents | BG C: 3 data extents (1 old + 2 relocated) 430c891389SQu Wenruo * 440c891389SQu Wenruo * [How does relocation work] 450c891389SQu Wenruo * 460c891389SQu Wenruo * 1. Mark the target block group read-only 470c891389SQu Wenruo * New extents won't be allocated from the target block group. 480c891389SQu Wenruo * 490c891389SQu Wenruo * 2.1 Record each extent in the target block group 500c891389SQu Wenruo * To build a proper map of extents to be relocated. 510c891389SQu Wenruo * 520c891389SQu Wenruo * 2.2 Build data reloc tree and reloc trees 530c891389SQu Wenruo * Data reloc tree will contain an inode, recording all newly relocated 540c891389SQu Wenruo * data extents. 550c891389SQu Wenruo * There will be only one data reloc tree for one data block group. 560c891389SQu Wenruo * 570c891389SQu Wenruo * Reloc tree will be a special snapshot of its source tree, containing 580c891389SQu Wenruo * relocated tree blocks. 590c891389SQu Wenruo * Each tree referring to a tree block in target block group will get its 600c891389SQu Wenruo * reloc tree built. 610c891389SQu Wenruo * 620c891389SQu Wenruo * 2.3 Swap source tree with its corresponding reloc tree 630c891389SQu Wenruo * Each involved tree only refers to new extents after swap. 640c891389SQu Wenruo * 650c891389SQu Wenruo * 3. Cleanup reloc trees and data reloc tree. 660c891389SQu Wenruo * As old extents in the target block group are still referenced by reloc 670c891389SQu Wenruo * trees, we need to clean them up before really freeing the target block 680c891389SQu Wenruo * group. 690c891389SQu Wenruo * 700c891389SQu Wenruo * The main complexity is in steps 2.2 and 2.3. 710c891389SQu Wenruo * 720c891389SQu Wenruo * The entry point of relocation is relocate_block_group() function. 730c891389SQu Wenruo */ 740c891389SQu Wenruo 750647bf56SWang Shilong #define RELOCATION_RESERVED_NODES 256 762a979612SQu Wenruo /* 775d4f98a2SYan Zheng * map address of tree root to tree 785d4f98a2SYan Zheng */ 795d4f98a2SYan Zheng struct mapping_node { 80e9a28dc5SQu Wenruo struct { 815d4f98a2SYan Zheng struct rb_node rb_node; 825d4f98a2SYan Zheng u64 bytenr; 83e9a28dc5SQu Wenruo }; /* Use rb_simle_node for search/insert */ 845d4f98a2SYan Zheng void *data; 855d4f98a2SYan Zheng }; 865d4f98a2SYan Zheng 875d4f98a2SYan Zheng struct mapping_tree { 885d4f98a2SYan Zheng struct rb_root rb_root; 895d4f98a2SYan Zheng spinlock_t lock; 905d4f98a2SYan Zheng }; 915d4f98a2SYan Zheng 925d4f98a2SYan Zheng /* 935d4f98a2SYan Zheng * present a tree block to process 945d4f98a2SYan Zheng */ 955d4f98a2SYan Zheng struct tree_block { 96e9a28dc5SQu Wenruo struct { 975d4f98a2SYan Zheng struct rb_node rb_node; 985d4f98a2SYan Zheng u64 bytenr; 99e9a28dc5SQu Wenruo }; /* Use rb_simple_node for search/insert */ 100f7ba2d37SJosef Bacik u64 owner; 1015d4f98a2SYan Zheng struct btrfs_key key; 1025d4f98a2SYan Zheng unsigned int level:8; 1035d4f98a2SYan Zheng unsigned int key_ready:1; 1045d4f98a2SYan Zheng }; 1055d4f98a2SYan Zheng 1060257bb82SYan, Zheng #define MAX_EXTENTS 128 1070257bb82SYan, Zheng 1080257bb82SYan, Zheng struct file_extent_cluster { 1090257bb82SYan, Zheng u64 start; 1100257bb82SYan, Zheng u64 end; 1110257bb82SYan, Zheng u64 boundary[MAX_EXTENTS]; 1120257bb82SYan, Zheng unsigned int nr; 1130257bb82SYan, Zheng }; 1140257bb82SYan, Zheng 1155d4f98a2SYan Zheng struct reloc_control { 1165d4f98a2SYan Zheng /* block group to relocate */ 11732da5386SDavid Sterba struct btrfs_block_group *block_group; 1185d4f98a2SYan Zheng /* extent tree */ 1195d4f98a2SYan Zheng struct btrfs_root *extent_root; 1205d4f98a2SYan Zheng /* inode for moving data */ 1215d4f98a2SYan Zheng struct inode *data_inode; 1223fd0a558SYan, Zheng 1233fd0a558SYan, Zheng struct btrfs_block_rsv *block_rsv; 1243fd0a558SYan, Zheng 125a26195a5SQu Wenruo struct btrfs_backref_cache backref_cache; 1263fd0a558SYan, Zheng 1273fd0a558SYan, Zheng struct file_extent_cluster cluster; 1285d4f98a2SYan Zheng /* tree blocks have been processed */ 1295d4f98a2SYan Zheng struct extent_io_tree processed_blocks; 1305d4f98a2SYan Zheng /* map start of tree root to corresponding reloc tree */ 1315d4f98a2SYan Zheng struct mapping_tree reloc_root_tree; 1325d4f98a2SYan Zheng /* list of reloc trees */ 1335d4f98a2SYan Zheng struct list_head reloc_roots; 134d2311e69SQu Wenruo /* list of subvolume trees that get relocated */ 135d2311e69SQu Wenruo struct list_head dirty_subvol_roots; 1363fd0a558SYan, Zheng /* size of metadata reservation for merging reloc trees */ 1373fd0a558SYan, Zheng u64 merging_rsv_size; 1383fd0a558SYan, Zheng /* size of relocated tree nodes */ 1393fd0a558SYan, Zheng u64 nodes_relocated; 1400647bf56SWang Shilong /* reserved size for block group relocation*/ 1410647bf56SWang Shilong u64 reserved_bytes; 1423fd0a558SYan, Zheng 1435d4f98a2SYan Zheng u64 search_start; 1445d4f98a2SYan Zheng u64 extents_found; 1453fd0a558SYan, Zheng 1463fd0a558SYan, Zheng unsigned int stage:8; 1473fd0a558SYan, Zheng unsigned int create_reloc_tree:1; 1483fd0a558SYan, Zheng unsigned int merge_reloc_tree:1; 1495d4f98a2SYan Zheng unsigned int found_file_extent:1; 1505d4f98a2SYan Zheng }; 1515d4f98a2SYan Zheng 1525d4f98a2SYan Zheng /* stages of data relocation */ 1535d4f98a2SYan Zheng #define MOVE_DATA_EXTENTS 0 1545d4f98a2SYan Zheng #define UPDATE_DATA_PTRS 1 1555d4f98a2SYan Zheng 1569569cc20SQu Wenruo static void mark_block_processed(struct reloc_control *rc, 157a26195a5SQu Wenruo struct btrfs_backref_node *node) 1589569cc20SQu Wenruo { 1599569cc20SQu Wenruo u32 blocksize; 1609569cc20SQu Wenruo 1619569cc20SQu Wenruo if (node->level == 0 || 1629569cc20SQu Wenruo in_range(node->bytenr, rc->block_group->start, 1639569cc20SQu Wenruo rc->block_group->length)) { 1649569cc20SQu Wenruo blocksize = rc->extent_root->fs_info->nodesize; 1659569cc20SQu Wenruo set_extent_bits(&rc->processed_blocks, node->bytenr, 1669569cc20SQu Wenruo node->bytenr + blocksize - 1, EXTENT_DIRTY); 1679569cc20SQu Wenruo } 1689569cc20SQu Wenruo node->processed = 1; 1699569cc20SQu Wenruo } 1709569cc20SQu Wenruo 1715d4f98a2SYan Zheng 1725d4f98a2SYan Zheng static void mapping_tree_init(struct mapping_tree *tree) 1735d4f98a2SYan Zheng { 1746bef4d31SEric Paris tree->rb_root = RB_ROOT; 1755d4f98a2SYan Zheng spin_lock_init(&tree->lock); 1765d4f98a2SYan Zheng } 1775d4f98a2SYan Zheng 1785d4f98a2SYan Zheng /* 1795d4f98a2SYan Zheng * walk up backref nodes until reach node presents tree root 1805d4f98a2SYan Zheng */ 181a26195a5SQu Wenruo static struct btrfs_backref_node *walk_up_backref( 182a26195a5SQu Wenruo struct btrfs_backref_node *node, 183a26195a5SQu Wenruo struct btrfs_backref_edge *edges[], int *index) 1845d4f98a2SYan Zheng { 185a26195a5SQu Wenruo struct btrfs_backref_edge *edge; 1865d4f98a2SYan Zheng int idx = *index; 1875d4f98a2SYan Zheng 1885d4f98a2SYan Zheng while (!list_empty(&node->upper)) { 1895d4f98a2SYan Zheng edge = list_entry(node->upper.next, 190a26195a5SQu Wenruo struct btrfs_backref_edge, list[LOWER]); 1915d4f98a2SYan Zheng edges[idx++] = edge; 1925d4f98a2SYan Zheng node = edge->node[UPPER]; 1935d4f98a2SYan Zheng } 1943fd0a558SYan, Zheng BUG_ON(node->detached); 1955d4f98a2SYan Zheng *index = idx; 1965d4f98a2SYan Zheng return node; 1975d4f98a2SYan Zheng } 1985d4f98a2SYan Zheng 1995d4f98a2SYan Zheng /* 2005d4f98a2SYan Zheng * walk down backref nodes to find start of next reference path 2015d4f98a2SYan Zheng */ 202a26195a5SQu Wenruo static struct btrfs_backref_node *walk_down_backref( 203a26195a5SQu Wenruo struct btrfs_backref_edge *edges[], int *index) 2045d4f98a2SYan Zheng { 205a26195a5SQu Wenruo struct btrfs_backref_edge *edge; 206a26195a5SQu Wenruo struct btrfs_backref_node *lower; 2075d4f98a2SYan Zheng int idx = *index; 2085d4f98a2SYan Zheng 2095d4f98a2SYan Zheng while (idx > 0) { 2105d4f98a2SYan Zheng edge = edges[idx - 1]; 2115d4f98a2SYan Zheng lower = edge->node[LOWER]; 2125d4f98a2SYan Zheng if (list_is_last(&edge->list[LOWER], &lower->upper)) { 2135d4f98a2SYan Zheng idx--; 2145d4f98a2SYan Zheng continue; 2155d4f98a2SYan Zheng } 2165d4f98a2SYan Zheng edge = list_entry(edge->list[LOWER].next, 217a26195a5SQu Wenruo struct btrfs_backref_edge, list[LOWER]); 2185d4f98a2SYan Zheng edges[idx - 1] = edge; 2195d4f98a2SYan Zheng *index = idx; 2205d4f98a2SYan Zheng return edge->node[UPPER]; 2215d4f98a2SYan Zheng } 2225d4f98a2SYan Zheng *index = 0; 2235d4f98a2SYan Zheng return NULL; 2245d4f98a2SYan Zheng } 2255d4f98a2SYan Zheng 226a26195a5SQu Wenruo static void update_backref_node(struct btrfs_backref_cache *cache, 227a26195a5SQu Wenruo struct btrfs_backref_node *node, u64 bytenr) 2283fd0a558SYan, Zheng { 2293fd0a558SYan, Zheng struct rb_node *rb_node; 2303fd0a558SYan, Zheng rb_erase(&node->rb_node, &cache->rb_root); 2313fd0a558SYan, Zheng node->bytenr = bytenr; 232e9a28dc5SQu Wenruo rb_node = rb_simple_insert(&cache->rb_root, node->bytenr, &node->rb_node); 23343c04fb1SJeff Mahoney if (rb_node) 234982c92cbSQu Wenruo btrfs_backref_panic(cache->fs_info, bytenr, -EEXIST); 2353fd0a558SYan, Zheng } 2363fd0a558SYan, Zheng 2373fd0a558SYan, Zheng /* 2383fd0a558SYan, Zheng * update backref cache after a transaction commit 2393fd0a558SYan, Zheng */ 2403fd0a558SYan, Zheng static int update_backref_cache(struct btrfs_trans_handle *trans, 241a26195a5SQu Wenruo struct btrfs_backref_cache *cache) 2423fd0a558SYan, Zheng { 243a26195a5SQu Wenruo struct btrfs_backref_node *node; 2443fd0a558SYan, Zheng int level = 0; 2453fd0a558SYan, Zheng 2463fd0a558SYan, Zheng if (cache->last_trans == 0) { 2473fd0a558SYan, Zheng cache->last_trans = trans->transid; 2483fd0a558SYan, Zheng return 0; 2493fd0a558SYan, Zheng } 2503fd0a558SYan, Zheng 2513fd0a558SYan, Zheng if (cache->last_trans == trans->transid) 2523fd0a558SYan, Zheng return 0; 2533fd0a558SYan, Zheng 2543fd0a558SYan, Zheng /* 2553fd0a558SYan, Zheng * detached nodes are used to avoid unnecessary backref 2563fd0a558SYan, Zheng * lookup. transaction commit changes the extent tree. 2573fd0a558SYan, Zheng * so the detached nodes are no longer useful. 2583fd0a558SYan, Zheng */ 2593fd0a558SYan, Zheng while (!list_empty(&cache->detached)) { 2603fd0a558SYan, Zheng node = list_entry(cache->detached.next, 261a26195a5SQu Wenruo struct btrfs_backref_node, list); 262023acb07SQu Wenruo btrfs_backref_cleanup_node(cache, node); 2633fd0a558SYan, Zheng } 2643fd0a558SYan, Zheng 2653fd0a558SYan, Zheng while (!list_empty(&cache->changed)) { 2663fd0a558SYan, Zheng node = list_entry(cache->changed.next, 267a26195a5SQu Wenruo struct btrfs_backref_node, list); 2683fd0a558SYan, Zheng list_del_init(&node->list); 2693fd0a558SYan, Zheng BUG_ON(node->pending); 2703fd0a558SYan, Zheng update_backref_node(cache, node, node->new_bytenr); 2713fd0a558SYan, Zheng } 2723fd0a558SYan, Zheng 2733fd0a558SYan, Zheng /* 2743fd0a558SYan, Zheng * some nodes can be left in the pending list if there were 2753fd0a558SYan, Zheng * errors during processing the pending nodes. 2763fd0a558SYan, Zheng */ 2773fd0a558SYan, Zheng for (level = 0; level < BTRFS_MAX_LEVEL; level++) { 2783fd0a558SYan, Zheng list_for_each_entry(node, &cache->pending[level], list) { 2793fd0a558SYan, Zheng BUG_ON(!node->pending); 2803fd0a558SYan, Zheng if (node->bytenr == node->new_bytenr) 2813fd0a558SYan, Zheng continue; 2823fd0a558SYan, Zheng update_backref_node(cache, node, node->new_bytenr); 2833fd0a558SYan, Zheng } 2843fd0a558SYan, Zheng } 2853fd0a558SYan, Zheng 2863fd0a558SYan, Zheng cache->last_trans = 0; 2873fd0a558SYan, Zheng return 1; 2883fd0a558SYan, Zheng } 2893fd0a558SYan, Zheng 2906282675eSQu Wenruo static bool reloc_root_is_dead(struct btrfs_root *root) 2916282675eSQu Wenruo { 2926282675eSQu Wenruo /* 2936282675eSQu Wenruo * Pair with set_bit/clear_bit in clean_dirty_subvols and 2946282675eSQu Wenruo * btrfs_update_reloc_root. We need to see the updated bit before 2956282675eSQu Wenruo * trying to access reloc_root 2966282675eSQu Wenruo */ 2976282675eSQu Wenruo smp_rmb(); 2986282675eSQu Wenruo if (test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state)) 2996282675eSQu Wenruo return true; 3006282675eSQu Wenruo return false; 3016282675eSQu Wenruo } 3026282675eSQu Wenruo 3036282675eSQu Wenruo /* 3046282675eSQu Wenruo * Check if this subvolume tree has valid reloc tree. 3056282675eSQu Wenruo * 3066282675eSQu Wenruo * Reloc tree after swap is considered dead, thus not considered as valid. 3076282675eSQu Wenruo * This is enough for most callers, as they don't distinguish dead reloc root 30855465730SQu Wenruo * from no reloc root. But btrfs_should_ignore_reloc_root() below is a 30955465730SQu Wenruo * special case. 3106282675eSQu Wenruo */ 3116282675eSQu Wenruo static bool have_reloc_root(struct btrfs_root *root) 3126282675eSQu Wenruo { 3136282675eSQu Wenruo if (reloc_root_is_dead(root)) 3146282675eSQu Wenruo return false; 3156282675eSQu Wenruo if (!root->reloc_root) 3166282675eSQu Wenruo return false; 3176282675eSQu Wenruo return true; 3186282675eSQu Wenruo } 319f2a97a9dSDavid Sterba 32055465730SQu Wenruo int btrfs_should_ignore_reloc_root(struct btrfs_root *root) 3213fd0a558SYan, Zheng { 3223fd0a558SYan, Zheng struct btrfs_root *reloc_root; 3233fd0a558SYan, Zheng 32492a7cc42SQu Wenruo if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) 3253fd0a558SYan, Zheng return 0; 3263fd0a558SYan, Zheng 3276282675eSQu Wenruo /* This root has been merged with its reloc tree, we can ignore it */ 3286282675eSQu Wenruo if (reloc_root_is_dead(root)) 3296282675eSQu Wenruo return 1; 3306282675eSQu Wenruo 3313fd0a558SYan, Zheng reloc_root = root->reloc_root; 3323fd0a558SYan, Zheng if (!reloc_root) 3333fd0a558SYan, Zheng return 0; 3343fd0a558SYan, Zheng 3354d4225fcSJosef Bacik if (btrfs_header_generation(reloc_root->commit_root) == 3364d4225fcSJosef Bacik root->fs_info->running_transaction->transid) 3373fd0a558SYan, Zheng return 0; 3383fd0a558SYan, Zheng /* 3393fd0a558SYan, Zheng * if there is reloc tree and it was created in previous 3403fd0a558SYan, Zheng * transaction backref lookup can find the reloc tree, 3413fd0a558SYan, Zheng * so backref node for the fs tree root is useless for 3423fd0a558SYan, Zheng * relocation. 3433fd0a558SYan, Zheng */ 3443fd0a558SYan, Zheng return 1; 3453fd0a558SYan, Zheng } 34655465730SQu Wenruo 3475d4f98a2SYan Zheng /* 3485d4f98a2SYan Zheng * find reloc tree by address of tree root 3495d4f98a2SYan Zheng */ 3502433bea5SQu Wenruo struct btrfs_root *find_reloc_root(struct btrfs_fs_info *fs_info, u64 bytenr) 3515d4f98a2SYan Zheng { 3522433bea5SQu Wenruo struct reloc_control *rc = fs_info->reloc_ctl; 3535d4f98a2SYan Zheng struct rb_node *rb_node; 3545d4f98a2SYan Zheng struct mapping_node *node; 3555d4f98a2SYan Zheng struct btrfs_root *root = NULL; 3565d4f98a2SYan Zheng 3572433bea5SQu Wenruo ASSERT(rc); 3585d4f98a2SYan Zheng spin_lock(&rc->reloc_root_tree.lock); 359e9a28dc5SQu Wenruo rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root, bytenr); 3605d4f98a2SYan Zheng if (rb_node) { 3615d4f98a2SYan Zheng node = rb_entry(rb_node, struct mapping_node, rb_node); 3625d4f98a2SYan Zheng root = (struct btrfs_root *)node->data; 3635d4f98a2SYan Zheng } 3645d4f98a2SYan Zheng spin_unlock(&rc->reloc_root_tree.lock); 36500246528SJosef Bacik return btrfs_grab_root(root); 3665d4f98a2SYan Zheng } 3675d4f98a2SYan Zheng 3685d4f98a2SYan Zheng /* 36929db137bSQu Wenruo * For useless nodes, do two major clean ups: 37029db137bSQu Wenruo * 37129db137bSQu Wenruo * - Cleanup the children edges and nodes 37229db137bSQu Wenruo * If child node is also orphan (no parent) during cleanup, then the child 37329db137bSQu Wenruo * node will also be cleaned up. 37429db137bSQu Wenruo * 37529db137bSQu Wenruo * - Freeing up leaves (level 0), keeps nodes detached 37629db137bSQu Wenruo * For nodes, the node is still cached as "detached" 37729db137bSQu Wenruo * 37829db137bSQu Wenruo * Return false if @node is not in the @useless_nodes list. 37929db137bSQu Wenruo * Return true if @node is in the @useless_nodes list. 38029db137bSQu Wenruo */ 38129db137bSQu Wenruo static bool handle_useless_nodes(struct reloc_control *rc, 382a26195a5SQu Wenruo struct btrfs_backref_node *node) 38329db137bSQu Wenruo { 384a26195a5SQu Wenruo struct btrfs_backref_cache *cache = &rc->backref_cache; 38529db137bSQu Wenruo struct list_head *useless_node = &cache->useless_node; 38629db137bSQu Wenruo bool ret = false; 38729db137bSQu Wenruo 38829db137bSQu Wenruo while (!list_empty(useless_node)) { 389a26195a5SQu Wenruo struct btrfs_backref_node *cur; 39029db137bSQu Wenruo 391a26195a5SQu Wenruo cur = list_first_entry(useless_node, struct btrfs_backref_node, 39229db137bSQu Wenruo list); 39329db137bSQu Wenruo list_del_init(&cur->list); 39429db137bSQu Wenruo 39529db137bSQu Wenruo /* Only tree root nodes can be added to @useless_nodes */ 39629db137bSQu Wenruo ASSERT(list_empty(&cur->upper)); 39729db137bSQu Wenruo 39829db137bSQu Wenruo if (cur == node) 39929db137bSQu Wenruo ret = true; 40029db137bSQu Wenruo 40129db137bSQu Wenruo /* The node is the lowest node */ 40229db137bSQu Wenruo if (cur->lowest) { 40329db137bSQu Wenruo list_del_init(&cur->lower); 40429db137bSQu Wenruo cur->lowest = 0; 40529db137bSQu Wenruo } 40629db137bSQu Wenruo 40729db137bSQu Wenruo /* Cleanup the lower edges */ 40829db137bSQu Wenruo while (!list_empty(&cur->lower)) { 409a26195a5SQu Wenruo struct btrfs_backref_edge *edge; 410a26195a5SQu Wenruo struct btrfs_backref_node *lower; 41129db137bSQu Wenruo 41229db137bSQu Wenruo edge = list_entry(cur->lower.next, 413a26195a5SQu Wenruo struct btrfs_backref_edge, list[UPPER]); 41429db137bSQu Wenruo list_del(&edge->list[UPPER]); 41529db137bSQu Wenruo list_del(&edge->list[LOWER]); 41629db137bSQu Wenruo lower = edge->node[LOWER]; 417741188d3SQu Wenruo btrfs_backref_free_edge(cache, edge); 41829db137bSQu Wenruo 41929db137bSQu Wenruo /* Child node is also orphan, queue for cleanup */ 42029db137bSQu Wenruo if (list_empty(&lower->upper)) 42129db137bSQu Wenruo list_add(&lower->list, useless_node); 42229db137bSQu Wenruo } 42329db137bSQu Wenruo /* Mark this block processed for relocation */ 42429db137bSQu Wenruo mark_block_processed(rc, cur); 42529db137bSQu Wenruo 42629db137bSQu Wenruo /* 42729db137bSQu Wenruo * Backref nodes for tree leaves are deleted from the cache. 42829db137bSQu Wenruo * Backref nodes for upper level tree blocks are left in the 42929db137bSQu Wenruo * cache to avoid unnecessary backref lookup. 43029db137bSQu Wenruo */ 43129db137bSQu Wenruo if (cur->level > 0) { 43229db137bSQu Wenruo list_add(&cur->list, &cache->detached); 43329db137bSQu Wenruo cur->detached = 1; 43429db137bSQu Wenruo } else { 43529db137bSQu Wenruo rb_erase(&cur->rb_node, &cache->rb_root); 436741188d3SQu Wenruo btrfs_backref_free_node(cache, cur); 43729db137bSQu Wenruo } 43829db137bSQu Wenruo } 43929db137bSQu Wenruo return ret; 44029db137bSQu Wenruo } 44129db137bSQu Wenruo 44229db137bSQu Wenruo /* 443e7d571c7SQu Wenruo * Build backref tree for a given tree block. Root of the backref tree 444e7d571c7SQu Wenruo * corresponds the tree block, leaves of the backref tree correspond roots of 445e7d571c7SQu Wenruo * b-trees that reference the tree block. 446e7d571c7SQu Wenruo * 447e7d571c7SQu Wenruo * The basic idea of this function is check backrefs of a given block to find 448e7d571c7SQu Wenruo * upper level blocks that reference the block, and then check backrefs of 449e7d571c7SQu Wenruo * these upper level blocks recursively. The recursion stops when tree root is 450e7d571c7SQu Wenruo * reached or backrefs for the block is cached. 451e7d571c7SQu Wenruo * 452e7d571c7SQu Wenruo * NOTE: if we find that backrefs for a block are cached, we know backrefs for 453e7d571c7SQu Wenruo * all upper level blocks that directly/indirectly reference the block are also 454e7d571c7SQu Wenruo * cached. 455e7d571c7SQu Wenruo */ 456a26195a5SQu Wenruo static noinline_for_stack struct btrfs_backref_node *build_backref_tree( 457e7d571c7SQu Wenruo struct reloc_control *rc, struct btrfs_key *node_key, 458e7d571c7SQu Wenruo int level, u64 bytenr) 459e7d571c7SQu Wenruo { 460e7d571c7SQu Wenruo struct btrfs_backref_iter *iter; 461a26195a5SQu Wenruo struct btrfs_backref_cache *cache = &rc->backref_cache; 462e7d571c7SQu Wenruo /* For searching parent of TREE_BLOCK_REF */ 463e7d571c7SQu Wenruo struct btrfs_path *path; 464a26195a5SQu Wenruo struct btrfs_backref_node *cur; 465a26195a5SQu Wenruo struct btrfs_backref_node *node = NULL; 466a26195a5SQu Wenruo struct btrfs_backref_edge *edge; 467e7d571c7SQu Wenruo int ret; 468e7d571c7SQu Wenruo int err = 0; 469e7d571c7SQu Wenruo 470e7d571c7SQu Wenruo iter = btrfs_backref_iter_alloc(rc->extent_root->fs_info, GFP_NOFS); 471e7d571c7SQu Wenruo if (!iter) 472e7d571c7SQu Wenruo return ERR_PTR(-ENOMEM); 473e7d571c7SQu Wenruo path = btrfs_alloc_path(); 474e7d571c7SQu Wenruo if (!path) { 475e7d571c7SQu Wenruo err = -ENOMEM; 476e7d571c7SQu Wenruo goto out; 477e7d571c7SQu Wenruo } 478e7d571c7SQu Wenruo 479b1818dabSQu Wenruo node = btrfs_backref_alloc_node(cache, bytenr, level); 480e7d571c7SQu Wenruo if (!node) { 481e7d571c7SQu Wenruo err = -ENOMEM; 482e7d571c7SQu Wenruo goto out; 483e7d571c7SQu Wenruo } 484e7d571c7SQu Wenruo 485e7d571c7SQu Wenruo node->lowest = 1; 486e7d571c7SQu Wenruo cur = node; 487e7d571c7SQu Wenruo 488e7d571c7SQu Wenruo /* Breadth-first search to build backref cache */ 489e7d571c7SQu Wenruo do { 4901b60d2ecSQu Wenruo ret = btrfs_backref_add_tree_node(cache, path, iter, node_key, 4911b60d2ecSQu Wenruo cur); 492e7d571c7SQu Wenruo if (ret < 0) { 493e7d571c7SQu Wenruo err = ret; 494e7d571c7SQu Wenruo goto out; 495e7d571c7SQu Wenruo } 496e7d571c7SQu Wenruo edge = list_first_entry_or_null(&cache->pending_edge, 497a26195a5SQu Wenruo struct btrfs_backref_edge, list[UPPER]); 498e7d571c7SQu Wenruo /* 499e7d571c7SQu Wenruo * The pending list isn't empty, take the first block to 500e7d571c7SQu Wenruo * process 501e7d571c7SQu Wenruo */ 502e7d571c7SQu Wenruo if (edge) { 5035d4f98a2SYan Zheng list_del_init(&edge->list[UPPER]); 5045d4f98a2SYan Zheng cur = edge->node[UPPER]; 5055d4f98a2SYan Zheng } 506e7d571c7SQu Wenruo } while (edge); 5075d4f98a2SYan Zheng 5081f872924SQu Wenruo /* Finish the upper linkage of newly added edges/nodes */ 509fc997ed0SQu Wenruo ret = btrfs_backref_finish_upper_links(cache, node); 5101f872924SQu Wenruo if (ret < 0) { 5111f872924SQu Wenruo err = ret; 51275bfb9afSJosef Bacik goto out; 51375bfb9afSJosef Bacik } 51475bfb9afSJosef Bacik 51529db137bSQu Wenruo if (handle_useless_nodes(rc, node)) 5163fd0a558SYan, Zheng node = NULL; 5175d4f98a2SYan Zheng out: 51871f572a9SQu Wenruo btrfs_backref_iter_free(iter); 51971f572a9SQu Wenruo btrfs_free_path(path); 5205d4f98a2SYan Zheng if (err) { 5211b23ea18SQu Wenruo btrfs_backref_error_cleanup(cache, node); 5225d4f98a2SYan Zheng return ERR_PTR(err); 5235d4f98a2SYan Zheng } 52475bfb9afSJosef Bacik ASSERT(!node || !node->detached); 52584780289SQu Wenruo ASSERT(list_empty(&cache->useless_node) && 52684780289SQu Wenruo list_empty(&cache->pending_edge)); 5275d4f98a2SYan Zheng return node; 5285d4f98a2SYan Zheng } 5295d4f98a2SYan Zheng 5305d4f98a2SYan Zheng /* 5313fd0a558SYan, Zheng * helper to add backref node for the newly created snapshot. 5323fd0a558SYan, Zheng * the backref node is created by cloning backref node that 5333fd0a558SYan, Zheng * corresponds to root of source tree 5343fd0a558SYan, Zheng */ 5353fd0a558SYan, Zheng static int clone_backref_node(struct btrfs_trans_handle *trans, 5363fd0a558SYan, Zheng struct reloc_control *rc, 5373fd0a558SYan, Zheng struct btrfs_root *src, 5383fd0a558SYan, Zheng struct btrfs_root *dest) 5393fd0a558SYan, Zheng { 5403fd0a558SYan, Zheng struct btrfs_root *reloc_root = src->reloc_root; 541a26195a5SQu Wenruo struct btrfs_backref_cache *cache = &rc->backref_cache; 542a26195a5SQu Wenruo struct btrfs_backref_node *node = NULL; 543a26195a5SQu Wenruo struct btrfs_backref_node *new_node; 544a26195a5SQu Wenruo struct btrfs_backref_edge *edge; 545a26195a5SQu Wenruo struct btrfs_backref_edge *new_edge; 5463fd0a558SYan, Zheng struct rb_node *rb_node; 5473fd0a558SYan, Zheng 5483fd0a558SYan, Zheng if (cache->last_trans > 0) 5493fd0a558SYan, Zheng update_backref_cache(trans, cache); 5503fd0a558SYan, Zheng 551e9a28dc5SQu Wenruo rb_node = rb_simple_search(&cache->rb_root, src->commit_root->start); 5523fd0a558SYan, Zheng if (rb_node) { 553a26195a5SQu Wenruo node = rb_entry(rb_node, struct btrfs_backref_node, rb_node); 5543fd0a558SYan, Zheng if (node->detached) 5553fd0a558SYan, Zheng node = NULL; 5563fd0a558SYan, Zheng else 5573fd0a558SYan, Zheng BUG_ON(node->new_bytenr != reloc_root->node->start); 5583fd0a558SYan, Zheng } 5593fd0a558SYan, Zheng 5603fd0a558SYan, Zheng if (!node) { 561e9a28dc5SQu Wenruo rb_node = rb_simple_search(&cache->rb_root, 5623fd0a558SYan, Zheng reloc_root->commit_root->start); 5633fd0a558SYan, Zheng if (rb_node) { 564a26195a5SQu Wenruo node = rb_entry(rb_node, struct btrfs_backref_node, 5653fd0a558SYan, Zheng rb_node); 5663fd0a558SYan, Zheng BUG_ON(node->detached); 5673fd0a558SYan, Zheng } 5683fd0a558SYan, Zheng } 5693fd0a558SYan, Zheng 5703fd0a558SYan, Zheng if (!node) 5713fd0a558SYan, Zheng return 0; 5723fd0a558SYan, Zheng 573b1818dabSQu Wenruo new_node = btrfs_backref_alloc_node(cache, dest->node->start, 574b1818dabSQu Wenruo node->level); 5753fd0a558SYan, Zheng if (!new_node) 5763fd0a558SYan, Zheng return -ENOMEM; 5773fd0a558SYan, Zheng 5783fd0a558SYan, Zheng new_node->lowest = node->lowest; 5796848ad64SYan, Zheng new_node->checked = 1; 58000246528SJosef Bacik new_node->root = btrfs_grab_root(dest); 5810b530bc5SJosef Bacik ASSERT(new_node->root); 5823fd0a558SYan, Zheng 5833fd0a558SYan, Zheng if (!node->lowest) { 5843fd0a558SYan, Zheng list_for_each_entry(edge, &node->lower, list[UPPER]) { 58547254d07SQu Wenruo new_edge = btrfs_backref_alloc_edge(cache); 5863fd0a558SYan, Zheng if (!new_edge) 5873fd0a558SYan, Zheng goto fail; 5883fd0a558SYan, Zheng 589f39911e5SQu Wenruo btrfs_backref_link_edge(new_edge, edge->node[LOWER], 590f39911e5SQu Wenruo new_node, LINK_UPPER); 5913fd0a558SYan, Zheng } 59276b9e23dSMiao Xie } else { 59376b9e23dSMiao Xie list_add_tail(&new_node->lower, &cache->leaves); 5943fd0a558SYan, Zheng } 5953fd0a558SYan, Zheng 596e9a28dc5SQu Wenruo rb_node = rb_simple_insert(&cache->rb_root, new_node->bytenr, 5973fd0a558SYan, Zheng &new_node->rb_node); 59843c04fb1SJeff Mahoney if (rb_node) 599982c92cbSQu Wenruo btrfs_backref_panic(trans->fs_info, new_node->bytenr, -EEXIST); 6003fd0a558SYan, Zheng 6013fd0a558SYan, Zheng if (!new_node->lowest) { 6023fd0a558SYan, Zheng list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) { 6033fd0a558SYan, Zheng list_add_tail(&new_edge->list[LOWER], 6043fd0a558SYan, Zheng &new_edge->node[LOWER]->upper); 6053fd0a558SYan, Zheng } 6063fd0a558SYan, Zheng } 6073fd0a558SYan, Zheng return 0; 6083fd0a558SYan, Zheng fail: 6093fd0a558SYan, Zheng while (!list_empty(&new_node->lower)) { 6103fd0a558SYan, Zheng new_edge = list_entry(new_node->lower.next, 611a26195a5SQu Wenruo struct btrfs_backref_edge, list[UPPER]); 6123fd0a558SYan, Zheng list_del(&new_edge->list[UPPER]); 613741188d3SQu Wenruo btrfs_backref_free_edge(cache, new_edge); 6143fd0a558SYan, Zheng } 615741188d3SQu Wenruo btrfs_backref_free_node(cache, new_node); 6163fd0a558SYan, Zheng return -ENOMEM; 6173fd0a558SYan, Zheng } 6183fd0a558SYan, Zheng 6193fd0a558SYan, Zheng /* 6205d4f98a2SYan Zheng * helper to add 'address of tree root -> reloc tree' mapping 6215d4f98a2SYan Zheng */ 622ffd7b339SJeff Mahoney static int __must_check __add_reloc_root(struct btrfs_root *root) 6235d4f98a2SYan Zheng { 6240b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 6255d4f98a2SYan Zheng struct rb_node *rb_node; 6265d4f98a2SYan Zheng struct mapping_node *node; 6270b246afaSJeff Mahoney struct reloc_control *rc = fs_info->reloc_ctl; 6285d4f98a2SYan Zheng 6295d4f98a2SYan Zheng node = kmalloc(sizeof(*node), GFP_NOFS); 630ffd7b339SJeff Mahoney if (!node) 631ffd7b339SJeff Mahoney return -ENOMEM; 6325d4f98a2SYan Zheng 633ea287ab1SJosef Bacik node->bytenr = root->commit_root->start; 6345d4f98a2SYan Zheng node->data = root; 6355d4f98a2SYan Zheng 6365d4f98a2SYan Zheng spin_lock(&rc->reloc_root_tree.lock); 637e9a28dc5SQu Wenruo rb_node = rb_simple_insert(&rc->reloc_root_tree.rb_root, 6385d4f98a2SYan Zheng node->bytenr, &node->rb_node); 6395d4f98a2SYan Zheng spin_unlock(&rc->reloc_root_tree.lock); 640ffd7b339SJeff Mahoney if (rb_node) { 6410b246afaSJeff Mahoney btrfs_panic(fs_info, -EEXIST, 6425d163e0eSJeff Mahoney "Duplicate root found for start=%llu while inserting into relocation tree", 6435d163e0eSJeff Mahoney node->bytenr); 644ffd7b339SJeff Mahoney } 6455d4f98a2SYan Zheng 6465d4f98a2SYan Zheng list_add_tail(&root->root_list, &rc->reloc_roots); 6475d4f98a2SYan Zheng return 0; 6485d4f98a2SYan Zheng } 6495d4f98a2SYan Zheng 6505d4f98a2SYan Zheng /* 651c974c464SWang Shilong * helper to delete the 'address of tree root -> reloc tree' 6525d4f98a2SYan Zheng * mapping 6535d4f98a2SYan Zheng */ 654c974c464SWang Shilong static void __del_reloc_root(struct btrfs_root *root) 6555d4f98a2SYan Zheng { 6560b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 6575d4f98a2SYan Zheng struct rb_node *rb_node; 6585d4f98a2SYan Zheng struct mapping_node *node = NULL; 6590b246afaSJeff Mahoney struct reloc_control *rc = fs_info->reloc_ctl; 660f44deb74SJosef Bacik bool put_ref = false; 6615d4f98a2SYan Zheng 66265c6e82bSQu Wenruo if (rc && root->node) { 6635d4f98a2SYan Zheng spin_lock(&rc->reloc_root_tree.lock); 664e9a28dc5SQu Wenruo rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root, 665ea287ab1SJosef Bacik root->commit_root->start); 666c974c464SWang Shilong if (rb_node) { 667c974c464SWang Shilong node = rb_entry(rb_node, struct mapping_node, rb_node); 668c974c464SWang Shilong rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root); 669ea287ab1SJosef Bacik RB_CLEAR_NODE(&node->rb_node); 670c974c464SWang Shilong } 671c974c464SWang Shilong spin_unlock(&rc->reloc_root_tree.lock); 672c78a10aeSJosef Bacik ASSERT(!node || (struct btrfs_root *)node->data == root); 673389305b2SQu Wenruo } 674c974c464SWang Shilong 675f44deb74SJosef Bacik /* 676f44deb74SJosef Bacik * We only put the reloc root here if it's on the list. There's a lot 677f44deb74SJosef Bacik * of places where the pattern is to splice the rc->reloc_roots, process 678f44deb74SJosef Bacik * the reloc roots, and then add the reloc root back onto 679f44deb74SJosef Bacik * rc->reloc_roots. If we call __del_reloc_root while it's off of the 680f44deb74SJosef Bacik * list we don't want the reference being dropped, because the guy 681f44deb74SJosef Bacik * messing with the list is in charge of the reference. 682f44deb74SJosef Bacik */ 6830b246afaSJeff Mahoney spin_lock(&fs_info->trans_lock); 684f44deb74SJosef Bacik if (!list_empty(&root->root_list)) { 685f44deb74SJosef Bacik put_ref = true; 686c974c464SWang Shilong list_del_init(&root->root_list); 687f44deb74SJosef Bacik } 6880b246afaSJeff Mahoney spin_unlock(&fs_info->trans_lock); 689f44deb74SJosef Bacik if (put_ref) 690f44deb74SJosef Bacik btrfs_put_root(root); 691c974c464SWang Shilong kfree(node); 692c974c464SWang Shilong } 693c974c464SWang Shilong 694c974c464SWang Shilong /* 695c974c464SWang Shilong * helper to update the 'address of tree root -> reloc tree' 696c974c464SWang Shilong * mapping 697c974c464SWang Shilong */ 698ea287ab1SJosef Bacik static int __update_reloc_root(struct btrfs_root *root) 699c974c464SWang Shilong { 7000b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 701c974c464SWang Shilong struct rb_node *rb_node; 702c974c464SWang Shilong struct mapping_node *node = NULL; 7030b246afaSJeff Mahoney struct reloc_control *rc = fs_info->reloc_ctl; 704c974c464SWang Shilong 705c974c464SWang Shilong spin_lock(&rc->reloc_root_tree.lock); 706e9a28dc5SQu Wenruo rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root, 707ea287ab1SJosef Bacik root->commit_root->start); 7085d4f98a2SYan Zheng if (rb_node) { 7095d4f98a2SYan Zheng node = rb_entry(rb_node, struct mapping_node, rb_node); 7105d4f98a2SYan Zheng rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root); 7115d4f98a2SYan Zheng } 7125d4f98a2SYan Zheng spin_unlock(&rc->reloc_root_tree.lock); 7135d4f98a2SYan Zheng 7148f71f3e0SLiu Bo if (!node) 7158f71f3e0SLiu Bo return 0; 7165d4f98a2SYan Zheng BUG_ON((struct btrfs_root *)node->data != root); 7175d4f98a2SYan Zheng 7185d4f98a2SYan Zheng spin_lock(&rc->reloc_root_tree.lock); 719ea287ab1SJosef Bacik node->bytenr = root->node->start; 720e9a28dc5SQu Wenruo rb_node = rb_simple_insert(&rc->reloc_root_tree.rb_root, 7215d4f98a2SYan Zheng node->bytenr, &node->rb_node); 7225d4f98a2SYan Zheng spin_unlock(&rc->reloc_root_tree.lock); 72343c04fb1SJeff Mahoney if (rb_node) 724982c92cbSQu Wenruo btrfs_backref_panic(fs_info, node->bytenr, -EEXIST); 7255d4f98a2SYan Zheng return 0; 7265d4f98a2SYan Zheng } 7275d4f98a2SYan Zheng 7283fd0a558SYan, Zheng static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans, 7293fd0a558SYan, Zheng struct btrfs_root *root, u64 objectid) 7305d4f98a2SYan Zheng { 7310b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 7325d4f98a2SYan Zheng struct btrfs_root *reloc_root; 7335d4f98a2SYan Zheng struct extent_buffer *eb; 7345d4f98a2SYan Zheng struct btrfs_root_item *root_item; 7355d4f98a2SYan Zheng struct btrfs_key root_key; 73684c50ba5SJosef Bacik int ret = 0; 73784c50ba5SJosef Bacik bool must_abort = false; 7385d4f98a2SYan Zheng 7395d4f98a2SYan Zheng root_item = kmalloc(sizeof(*root_item), GFP_NOFS); 74084c50ba5SJosef Bacik if (!root_item) 74184c50ba5SJosef Bacik return ERR_PTR(-ENOMEM); 7425d4f98a2SYan Zheng 7435d4f98a2SYan Zheng root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; 7445d4f98a2SYan Zheng root_key.type = BTRFS_ROOT_ITEM_KEY; 7453fd0a558SYan, Zheng root_key.offset = objectid; 7465d4f98a2SYan Zheng 7473fd0a558SYan, Zheng if (root->root_key.objectid == objectid) { 748054570a1SFilipe Manana u64 commit_root_gen; 749054570a1SFilipe Manana 7503fd0a558SYan, Zheng /* called by btrfs_init_reloc_root */ 7515d4f98a2SYan Zheng ret = btrfs_copy_root(trans, root, root->commit_root, &eb, 7525d4f98a2SYan Zheng BTRFS_TREE_RELOC_OBJECTID); 75384c50ba5SJosef Bacik if (ret) 75484c50ba5SJosef Bacik goto fail; 75584c50ba5SJosef Bacik 756054570a1SFilipe Manana /* 757054570a1SFilipe Manana * Set the last_snapshot field to the generation of the commit 758054570a1SFilipe Manana * root - like this ctree.c:btrfs_block_can_be_shared() behaves 759054570a1SFilipe Manana * correctly (returns true) when the relocation root is created 760054570a1SFilipe Manana * either inside the critical section of a transaction commit 761054570a1SFilipe Manana * (through transaction.c:qgroup_account_snapshot()) and when 762054570a1SFilipe Manana * it's created before the transaction commit is started. 763054570a1SFilipe Manana */ 764054570a1SFilipe Manana commit_root_gen = btrfs_header_generation(root->commit_root); 765054570a1SFilipe Manana btrfs_set_root_last_snapshot(&root->root_item, commit_root_gen); 7663fd0a558SYan, Zheng } else { 7673fd0a558SYan, Zheng /* 7683fd0a558SYan, Zheng * called by btrfs_reloc_post_snapshot_hook. 7693fd0a558SYan, Zheng * the source tree is a reloc tree, all tree blocks 7703fd0a558SYan, Zheng * modified after it was created have RELOC flag 7713fd0a558SYan, Zheng * set in their headers. so it's OK to not update 7723fd0a558SYan, Zheng * the 'last_snapshot'. 7733fd0a558SYan, Zheng */ 7743fd0a558SYan, Zheng ret = btrfs_copy_root(trans, root, root->node, &eb, 7753fd0a558SYan, Zheng BTRFS_TREE_RELOC_OBJECTID); 77684c50ba5SJosef Bacik if (ret) 77784c50ba5SJosef Bacik goto fail; 7783fd0a558SYan, Zheng } 7793fd0a558SYan, Zheng 78084c50ba5SJosef Bacik /* 78184c50ba5SJosef Bacik * We have changed references at this point, we must abort the 78284c50ba5SJosef Bacik * transaction if anything fails. 78384c50ba5SJosef Bacik */ 78484c50ba5SJosef Bacik must_abort = true; 78584c50ba5SJosef Bacik 7865d4f98a2SYan Zheng memcpy(root_item, &root->root_item, sizeof(*root_item)); 7875d4f98a2SYan Zheng btrfs_set_root_bytenr(root_item, eb->start); 7885d4f98a2SYan Zheng btrfs_set_root_level(root_item, btrfs_header_level(eb)); 7895d4f98a2SYan Zheng btrfs_set_root_generation(root_item, trans->transid); 7903fd0a558SYan, Zheng 7913fd0a558SYan, Zheng if (root->root_key.objectid == objectid) { 7923fd0a558SYan, Zheng btrfs_set_root_refs(root_item, 0); 7933fd0a558SYan, Zheng memset(&root_item->drop_progress, 0, 7943fd0a558SYan, Zheng sizeof(struct btrfs_disk_key)); 795c8422684SDavid Sterba btrfs_set_root_drop_level(root_item, 0); 7963fd0a558SYan, Zheng } 7975d4f98a2SYan Zheng 7985d4f98a2SYan Zheng btrfs_tree_unlock(eb); 7995d4f98a2SYan Zheng free_extent_buffer(eb); 8005d4f98a2SYan Zheng 8010b246afaSJeff Mahoney ret = btrfs_insert_root(trans, fs_info->tree_root, 8025d4f98a2SYan Zheng &root_key, root_item); 80384c50ba5SJosef Bacik if (ret) 80484c50ba5SJosef Bacik goto fail; 80584c50ba5SJosef Bacik 8065d4f98a2SYan Zheng kfree(root_item); 8075d4f98a2SYan Zheng 8083dbf1738SJosef Bacik reloc_root = btrfs_read_tree_root(fs_info->tree_root, &root_key); 80984c50ba5SJosef Bacik if (IS_ERR(reloc_root)) { 81084c50ba5SJosef Bacik ret = PTR_ERR(reloc_root); 81184c50ba5SJosef Bacik goto abort; 81284c50ba5SJosef Bacik } 81392a7cc42SQu Wenruo set_bit(BTRFS_ROOT_SHAREABLE, &reloc_root->state); 8145d4f98a2SYan Zheng reloc_root->last_trans = trans->transid; 8153fd0a558SYan, Zheng return reloc_root; 81684c50ba5SJosef Bacik fail: 81784c50ba5SJosef Bacik kfree(root_item); 81884c50ba5SJosef Bacik abort: 81984c50ba5SJosef Bacik if (must_abort) 82084c50ba5SJosef Bacik btrfs_abort_transaction(trans, ret); 82184c50ba5SJosef Bacik return ERR_PTR(ret); 8223fd0a558SYan, Zheng } 8233fd0a558SYan, Zheng 8243fd0a558SYan, Zheng /* 8253fd0a558SYan, Zheng * create reloc tree for a given fs tree. reloc tree is just a 8263fd0a558SYan, Zheng * snapshot of the fs tree with special root objectid. 827f44deb74SJosef Bacik * 828f44deb74SJosef Bacik * The reloc_root comes out of here with two references, one for 829f44deb74SJosef Bacik * root->reloc_root, and another for being on the rc->reloc_roots list. 8303fd0a558SYan, Zheng */ 8313fd0a558SYan, Zheng int btrfs_init_reloc_root(struct btrfs_trans_handle *trans, 8323fd0a558SYan, Zheng struct btrfs_root *root) 8333fd0a558SYan, Zheng { 8340b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 8353fd0a558SYan, Zheng struct btrfs_root *reloc_root; 8360b246afaSJeff Mahoney struct reloc_control *rc = fs_info->reloc_ctl; 83720dd2cbfSMiao Xie struct btrfs_block_rsv *rsv; 8383fd0a558SYan, Zheng int clear_rsv = 0; 839ffd7b339SJeff Mahoney int ret; 8403fd0a558SYan, Zheng 841aec7db3bSJosef Bacik if (!rc) 8422abc726aSJosef Bacik return 0; 8432abc726aSJosef Bacik 8441fac4a54SQu Wenruo /* 8451fac4a54SQu Wenruo * The subvolume has reloc tree but the swap is finished, no need to 8461fac4a54SQu Wenruo * create/update the dead reloc tree 8471fac4a54SQu Wenruo */ 8486282675eSQu Wenruo if (reloc_root_is_dead(root)) 8491fac4a54SQu Wenruo return 0; 8501fac4a54SQu Wenruo 851aec7db3bSJosef Bacik /* 852aec7db3bSJosef Bacik * This is subtle but important. We do not do 853aec7db3bSJosef Bacik * record_root_in_transaction for reloc roots, instead we record their 854aec7db3bSJosef Bacik * corresponding fs root, and then here we update the last trans for the 855aec7db3bSJosef Bacik * reloc root. This means that we have to do this for the entire life 856aec7db3bSJosef Bacik * of the reloc root, regardless of which stage of the relocation we are 857aec7db3bSJosef Bacik * in. 858aec7db3bSJosef Bacik */ 8593fd0a558SYan, Zheng if (root->reloc_root) { 8603fd0a558SYan, Zheng reloc_root = root->reloc_root; 8613fd0a558SYan, Zheng reloc_root->last_trans = trans->transid; 8623fd0a558SYan, Zheng return 0; 8633fd0a558SYan, Zheng } 8643fd0a558SYan, Zheng 865aec7db3bSJosef Bacik /* 866aec7db3bSJosef Bacik * We are merging reloc roots, we do not need new reloc trees. Also 867aec7db3bSJosef Bacik * reloc trees never need their own reloc tree. 868aec7db3bSJosef Bacik */ 869aec7db3bSJosef Bacik if (!rc->create_reloc_tree || 870aec7db3bSJosef Bacik root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) 871aec7db3bSJosef Bacik return 0; 872aec7db3bSJosef Bacik 87320dd2cbfSMiao Xie if (!trans->reloc_reserved) { 87420dd2cbfSMiao Xie rsv = trans->block_rsv; 8753fd0a558SYan, Zheng trans->block_rsv = rc->block_rsv; 8763fd0a558SYan, Zheng clear_rsv = 1; 8773fd0a558SYan, Zheng } 8783fd0a558SYan, Zheng reloc_root = create_reloc_root(trans, root, root->root_key.objectid); 8793fd0a558SYan, Zheng if (clear_rsv) 88020dd2cbfSMiao Xie trans->block_rsv = rsv; 88100bb36a0SJosef Bacik if (IS_ERR(reloc_root)) 88200bb36a0SJosef Bacik return PTR_ERR(reloc_root); 8835d4f98a2SYan Zheng 884ffd7b339SJeff Mahoney ret = __add_reloc_root(reloc_root); 88500bb36a0SJosef Bacik if (ret) { 88600bb36a0SJosef Bacik /* Pairs with create_reloc_root */ 88700bb36a0SJosef Bacik btrfs_put_root(reloc_root); 88800bb36a0SJosef Bacik return ret; 88900bb36a0SJosef Bacik } 890f44deb74SJosef Bacik root->reloc_root = btrfs_grab_root(reloc_root); 8915d4f98a2SYan Zheng return 0; 8925d4f98a2SYan Zheng } 8935d4f98a2SYan Zheng 8945d4f98a2SYan Zheng /* 8955d4f98a2SYan Zheng * update root item of reloc tree 8965d4f98a2SYan Zheng */ 8975d4f98a2SYan Zheng int btrfs_update_reloc_root(struct btrfs_trans_handle *trans, 8985d4f98a2SYan Zheng struct btrfs_root *root) 8995d4f98a2SYan Zheng { 9000b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 9015d4f98a2SYan Zheng struct btrfs_root *reloc_root; 9025d4f98a2SYan Zheng struct btrfs_root_item *root_item; 9035d4f98a2SYan Zheng int ret; 9045d4f98a2SYan Zheng 9056282675eSQu Wenruo if (!have_reloc_root(root)) 906592fbcd5SJosef Bacik return 0; 9075d4f98a2SYan Zheng 9085d4f98a2SYan Zheng reloc_root = root->reloc_root; 9095d4f98a2SYan Zheng root_item = &reloc_root->root_item; 9105d4f98a2SYan Zheng 911f44deb74SJosef Bacik /* 912f44deb74SJosef Bacik * We are probably ok here, but __del_reloc_root() will drop its ref of 913f44deb74SJosef Bacik * the root. We have the ref for root->reloc_root, but just in case 914f44deb74SJosef Bacik * hold it while we update the reloc root. 915f44deb74SJosef Bacik */ 916f44deb74SJosef Bacik btrfs_grab_root(reloc_root); 917f44deb74SJosef Bacik 918d2311e69SQu Wenruo /* root->reloc_root will stay until current relocation finished */ 9190b246afaSJeff Mahoney if (fs_info->reloc_ctl->merge_reloc_tree && 9203fd0a558SYan, Zheng btrfs_root_refs(root_item) == 0) { 921d2311e69SQu Wenruo set_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state); 9226282675eSQu Wenruo /* 9236282675eSQu Wenruo * Mark the tree as dead before we change reloc_root so 9246282675eSQu Wenruo * have_reloc_root will not touch it from now on. 9256282675eSQu Wenruo */ 9266282675eSQu Wenruo smp_wmb(); 927c974c464SWang Shilong __del_reloc_root(reloc_root); 9285d4f98a2SYan Zheng } 9295d4f98a2SYan Zheng 9305d4f98a2SYan Zheng if (reloc_root->commit_root != reloc_root->node) { 931ea287ab1SJosef Bacik __update_reloc_root(reloc_root); 9325d4f98a2SYan Zheng btrfs_set_root_node(root_item, reloc_root->node); 9335d4f98a2SYan Zheng free_extent_buffer(reloc_root->commit_root); 9345d4f98a2SYan Zheng reloc_root->commit_root = btrfs_root_node(reloc_root); 9355d4f98a2SYan Zheng } 9365d4f98a2SYan Zheng 9370b246afaSJeff Mahoney ret = btrfs_update_root(trans, fs_info->tree_root, 9385d4f98a2SYan Zheng &reloc_root->root_key, root_item); 939f44deb74SJosef Bacik btrfs_put_root(reloc_root); 940592fbcd5SJosef Bacik return ret; 9415d4f98a2SYan Zheng } 9425d4f98a2SYan Zheng 9435d4f98a2SYan Zheng /* 9445d4f98a2SYan Zheng * helper to find first cached inode with inode number >= objectid 9455d4f98a2SYan Zheng * in a subvolume 9465d4f98a2SYan Zheng */ 9475d4f98a2SYan Zheng static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid) 9485d4f98a2SYan Zheng { 9495d4f98a2SYan Zheng struct rb_node *node; 9505d4f98a2SYan Zheng struct rb_node *prev; 9515d4f98a2SYan Zheng struct btrfs_inode *entry; 9525d4f98a2SYan Zheng struct inode *inode; 9535d4f98a2SYan Zheng 9545d4f98a2SYan Zheng spin_lock(&root->inode_lock); 9555d4f98a2SYan Zheng again: 9565d4f98a2SYan Zheng node = root->inode_tree.rb_node; 9575d4f98a2SYan Zheng prev = NULL; 9585d4f98a2SYan Zheng while (node) { 9595d4f98a2SYan Zheng prev = node; 9605d4f98a2SYan Zheng entry = rb_entry(node, struct btrfs_inode, rb_node); 9615d4f98a2SYan Zheng 9624a0cc7caSNikolay Borisov if (objectid < btrfs_ino(entry)) 9635d4f98a2SYan Zheng node = node->rb_left; 9644a0cc7caSNikolay Borisov else if (objectid > btrfs_ino(entry)) 9655d4f98a2SYan Zheng node = node->rb_right; 9665d4f98a2SYan Zheng else 9675d4f98a2SYan Zheng break; 9685d4f98a2SYan Zheng } 9695d4f98a2SYan Zheng if (!node) { 9705d4f98a2SYan Zheng while (prev) { 9715d4f98a2SYan Zheng entry = rb_entry(prev, struct btrfs_inode, rb_node); 9724a0cc7caSNikolay Borisov if (objectid <= btrfs_ino(entry)) { 9735d4f98a2SYan Zheng node = prev; 9745d4f98a2SYan Zheng break; 9755d4f98a2SYan Zheng } 9765d4f98a2SYan Zheng prev = rb_next(prev); 9775d4f98a2SYan Zheng } 9785d4f98a2SYan Zheng } 9795d4f98a2SYan Zheng while (node) { 9805d4f98a2SYan Zheng entry = rb_entry(node, struct btrfs_inode, rb_node); 9815d4f98a2SYan Zheng inode = igrab(&entry->vfs_inode); 9825d4f98a2SYan Zheng if (inode) { 9835d4f98a2SYan Zheng spin_unlock(&root->inode_lock); 9845d4f98a2SYan Zheng return inode; 9855d4f98a2SYan Zheng } 9865d4f98a2SYan Zheng 9874a0cc7caSNikolay Borisov objectid = btrfs_ino(entry) + 1; 9885d4f98a2SYan Zheng if (cond_resched_lock(&root->inode_lock)) 9895d4f98a2SYan Zheng goto again; 9905d4f98a2SYan Zheng 9915d4f98a2SYan Zheng node = rb_next(node); 9925d4f98a2SYan Zheng } 9935d4f98a2SYan Zheng spin_unlock(&root->inode_lock); 9945d4f98a2SYan Zheng return NULL; 9955d4f98a2SYan Zheng } 9965d4f98a2SYan Zheng 9975d4f98a2SYan Zheng /* 9985d4f98a2SYan Zheng * get new location of data 9995d4f98a2SYan Zheng */ 10005d4f98a2SYan Zheng static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr, 10015d4f98a2SYan Zheng u64 bytenr, u64 num_bytes) 10025d4f98a2SYan Zheng { 10035d4f98a2SYan Zheng struct btrfs_root *root = BTRFS_I(reloc_inode)->root; 10045d4f98a2SYan Zheng struct btrfs_path *path; 10055d4f98a2SYan Zheng struct btrfs_file_extent_item *fi; 10065d4f98a2SYan Zheng struct extent_buffer *leaf; 10075d4f98a2SYan Zheng int ret; 10085d4f98a2SYan Zheng 10095d4f98a2SYan Zheng path = btrfs_alloc_path(); 10105d4f98a2SYan Zheng if (!path) 10115d4f98a2SYan Zheng return -ENOMEM; 10125d4f98a2SYan Zheng 10135d4f98a2SYan Zheng bytenr -= BTRFS_I(reloc_inode)->index_cnt; 1014f85b7379SDavid Sterba ret = btrfs_lookup_file_extent(NULL, root, path, 1015f85b7379SDavid Sterba btrfs_ino(BTRFS_I(reloc_inode)), bytenr, 0); 10165d4f98a2SYan Zheng if (ret < 0) 10175d4f98a2SYan Zheng goto out; 10185d4f98a2SYan Zheng if (ret > 0) { 10195d4f98a2SYan Zheng ret = -ENOENT; 10205d4f98a2SYan Zheng goto out; 10215d4f98a2SYan Zheng } 10225d4f98a2SYan Zheng 10235d4f98a2SYan Zheng leaf = path->nodes[0]; 10245d4f98a2SYan Zheng fi = btrfs_item_ptr(leaf, path->slots[0], 10255d4f98a2SYan Zheng struct btrfs_file_extent_item); 10265d4f98a2SYan Zheng 10275d4f98a2SYan Zheng BUG_ON(btrfs_file_extent_offset(leaf, fi) || 10285d4f98a2SYan Zheng btrfs_file_extent_compression(leaf, fi) || 10295d4f98a2SYan Zheng btrfs_file_extent_encryption(leaf, fi) || 10305d4f98a2SYan Zheng btrfs_file_extent_other_encoding(leaf, fi)); 10315d4f98a2SYan Zheng 10325d4f98a2SYan Zheng if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) { 103383d4cfd4SJosef Bacik ret = -EINVAL; 10345d4f98a2SYan Zheng goto out; 10355d4f98a2SYan Zheng } 10365d4f98a2SYan Zheng 10375d4f98a2SYan Zheng *new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); 10385d4f98a2SYan Zheng ret = 0; 10395d4f98a2SYan Zheng out: 10405d4f98a2SYan Zheng btrfs_free_path(path); 10415d4f98a2SYan Zheng return ret; 10425d4f98a2SYan Zheng } 10435d4f98a2SYan Zheng 10445d4f98a2SYan Zheng /* 10455d4f98a2SYan Zheng * update file extent items in the tree leaf to point to 10465d4f98a2SYan Zheng * the new locations. 10475d4f98a2SYan Zheng */ 10483fd0a558SYan, Zheng static noinline_for_stack 10493fd0a558SYan, Zheng int replace_file_extents(struct btrfs_trans_handle *trans, 10505d4f98a2SYan Zheng struct reloc_control *rc, 10515d4f98a2SYan Zheng struct btrfs_root *root, 10523fd0a558SYan, Zheng struct extent_buffer *leaf) 10535d4f98a2SYan Zheng { 10540b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 10555d4f98a2SYan Zheng struct btrfs_key key; 10565d4f98a2SYan Zheng struct btrfs_file_extent_item *fi; 10575d4f98a2SYan Zheng struct inode *inode = NULL; 10585d4f98a2SYan Zheng u64 parent; 10595d4f98a2SYan Zheng u64 bytenr; 10603fd0a558SYan, Zheng u64 new_bytenr = 0; 10615d4f98a2SYan Zheng u64 num_bytes; 10625d4f98a2SYan Zheng u64 end; 10635d4f98a2SYan Zheng u32 nritems; 10645d4f98a2SYan Zheng u32 i; 106583d4cfd4SJosef Bacik int ret = 0; 10665d4f98a2SYan Zheng int first = 1; 10675d4f98a2SYan Zheng int dirty = 0; 10685d4f98a2SYan Zheng 10695d4f98a2SYan Zheng if (rc->stage != UPDATE_DATA_PTRS) 10705d4f98a2SYan Zheng return 0; 10715d4f98a2SYan Zheng 10725d4f98a2SYan Zheng /* reloc trees always use full backref */ 10735d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) 10745d4f98a2SYan Zheng parent = leaf->start; 10755d4f98a2SYan Zheng else 10765d4f98a2SYan Zheng parent = 0; 10775d4f98a2SYan Zheng 10785d4f98a2SYan Zheng nritems = btrfs_header_nritems(leaf); 10795d4f98a2SYan Zheng for (i = 0; i < nritems; i++) { 108082fa113fSQu Wenruo struct btrfs_ref ref = { 0 }; 108182fa113fSQu Wenruo 10825d4f98a2SYan Zheng cond_resched(); 10835d4f98a2SYan Zheng btrfs_item_key_to_cpu(leaf, &key, i); 10845d4f98a2SYan Zheng if (key.type != BTRFS_EXTENT_DATA_KEY) 10855d4f98a2SYan Zheng continue; 10865d4f98a2SYan Zheng fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); 10875d4f98a2SYan Zheng if (btrfs_file_extent_type(leaf, fi) == 10885d4f98a2SYan Zheng BTRFS_FILE_EXTENT_INLINE) 10895d4f98a2SYan Zheng continue; 10905d4f98a2SYan Zheng bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); 10915d4f98a2SYan Zheng num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); 10925d4f98a2SYan Zheng if (bytenr == 0) 10935d4f98a2SYan Zheng continue; 10949569cc20SQu Wenruo if (!in_range(bytenr, rc->block_group->start, 10959569cc20SQu Wenruo rc->block_group->length)) 10965d4f98a2SYan Zheng continue; 10975d4f98a2SYan Zheng 10985d4f98a2SYan Zheng /* 10995d4f98a2SYan Zheng * if we are modifying block in fs tree, wait for readpage 11005d4f98a2SYan Zheng * to complete and drop the extent cache 11015d4f98a2SYan Zheng */ 11025d4f98a2SYan Zheng if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { 11035d4f98a2SYan Zheng if (first) { 11045d4f98a2SYan Zheng inode = find_next_inode(root, key.objectid); 11055d4f98a2SYan Zheng first = 0; 11064a0cc7caSNikolay Borisov } else if (inode && btrfs_ino(BTRFS_I(inode)) < key.objectid) { 11073fd0a558SYan, Zheng btrfs_add_delayed_iput(inode); 11085d4f98a2SYan Zheng inode = find_next_inode(root, key.objectid); 11095d4f98a2SYan Zheng } 11104a0cc7caSNikolay Borisov if (inode && btrfs_ino(BTRFS_I(inode)) == key.objectid) { 11115d4f98a2SYan Zheng end = key.offset + 11125d4f98a2SYan Zheng btrfs_file_extent_num_bytes(leaf, fi); 11135d4f98a2SYan Zheng WARN_ON(!IS_ALIGNED(key.offset, 11140b246afaSJeff Mahoney fs_info->sectorsize)); 11150b246afaSJeff Mahoney WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize)); 11165d4f98a2SYan Zheng end--; 11175d4f98a2SYan Zheng ret = try_lock_extent(&BTRFS_I(inode)->io_tree, 1118d0082371SJeff Mahoney key.offset, end); 11195d4f98a2SYan Zheng if (!ret) 11205d4f98a2SYan Zheng continue; 11215d4f98a2SYan Zheng 1122dcdbc059SNikolay Borisov btrfs_drop_extent_cache(BTRFS_I(inode), 1123dcdbc059SNikolay Borisov key.offset, end, 1); 11245d4f98a2SYan Zheng unlock_extent(&BTRFS_I(inode)->io_tree, 1125d0082371SJeff Mahoney key.offset, end); 11265d4f98a2SYan Zheng } 11275d4f98a2SYan Zheng } 11285d4f98a2SYan Zheng 11295d4f98a2SYan Zheng ret = get_new_location(rc->data_inode, &new_bytenr, 11305d4f98a2SYan Zheng bytenr, num_bytes); 113183d4cfd4SJosef Bacik if (ret) { 113283d4cfd4SJosef Bacik /* 113383d4cfd4SJosef Bacik * Don't have to abort since we've not changed anything 113483d4cfd4SJosef Bacik * in the file extent yet. 113583d4cfd4SJosef Bacik */ 113683d4cfd4SJosef Bacik break; 11373fd0a558SYan, Zheng } 11385d4f98a2SYan Zheng 11395d4f98a2SYan Zheng btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr); 11405d4f98a2SYan Zheng dirty = 1; 11415d4f98a2SYan Zheng 11425d4f98a2SYan Zheng key.offset -= btrfs_file_extent_offset(leaf, fi); 114382fa113fSQu Wenruo btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr, 114482fa113fSQu Wenruo num_bytes, parent); 114582fa113fSQu Wenruo ref.real_root = root->root_key.objectid; 114682fa113fSQu Wenruo btrfs_init_data_ref(&ref, btrfs_header_owner(leaf), 1147b06c4bf5SFilipe Manana key.objectid, key.offset); 114882fa113fSQu Wenruo ret = btrfs_inc_extent_ref(trans, &ref); 114983d4cfd4SJosef Bacik if (ret) { 115066642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 115183d4cfd4SJosef Bacik break; 115283d4cfd4SJosef Bacik } 11535d4f98a2SYan Zheng 1154ffd4bb2aSQu Wenruo btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr, 1155ffd4bb2aSQu Wenruo num_bytes, parent); 1156ffd4bb2aSQu Wenruo ref.real_root = root->root_key.objectid; 1157ffd4bb2aSQu Wenruo btrfs_init_data_ref(&ref, btrfs_header_owner(leaf), 1158b06c4bf5SFilipe Manana key.objectid, key.offset); 1159ffd4bb2aSQu Wenruo ret = btrfs_free_extent(trans, &ref); 116083d4cfd4SJosef Bacik if (ret) { 116166642832SJeff Mahoney btrfs_abort_transaction(trans, ret); 116283d4cfd4SJosef Bacik break; 116383d4cfd4SJosef Bacik } 11645d4f98a2SYan Zheng } 11655d4f98a2SYan Zheng if (dirty) 11665d4f98a2SYan Zheng btrfs_mark_buffer_dirty(leaf); 11673fd0a558SYan, Zheng if (inode) 11683fd0a558SYan, Zheng btrfs_add_delayed_iput(inode); 116983d4cfd4SJosef Bacik return ret; 11705d4f98a2SYan Zheng } 11715d4f98a2SYan Zheng 11725d4f98a2SYan Zheng static noinline_for_stack 11735d4f98a2SYan Zheng int memcmp_node_keys(struct extent_buffer *eb, int slot, 11745d4f98a2SYan Zheng struct btrfs_path *path, int level) 11755d4f98a2SYan Zheng { 11765d4f98a2SYan Zheng struct btrfs_disk_key key1; 11775d4f98a2SYan Zheng struct btrfs_disk_key key2; 11785d4f98a2SYan Zheng btrfs_node_key(eb, &key1, slot); 11795d4f98a2SYan Zheng btrfs_node_key(path->nodes[level], &key2, path->slots[level]); 11805d4f98a2SYan Zheng return memcmp(&key1, &key2, sizeof(key1)); 11815d4f98a2SYan Zheng } 11825d4f98a2SYan Zheng 11835d4f98a2SYan Zheng /* 11845d4f98a2SYan Zheng * try to replace tree blocks in fs tree with the new blocks 11855d4f98a2SYan Zheng * in reloc tree. tree blocks haven't been modified since the 11865d4f98a2SYan Zheng * reloc tree was create can be replaced. 11875d4f98a2SYan Zheng * 11885d4f98a2SYan Zheng * if a block was replaced, level of the block + 1 is returned. 11895d4f98a2SYan Zheng * if no block got replaced, 0 is returned. if there are other 11905d4f98a2SYan Zheng * errors, a negative error number is returned. 11915d4f98a2SYan Zheng */ 11923fd0a558SYan, Zheng static noinline_for_stack 11933d0174f7SQu Wenruo int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc, 11945d4f98a2SYan Zheng struct btrfs_root *dest, struct btrfs_root *src, 11955d4f98a2SYan Zheng struct btrfs_path *path, struct btrfs_key *next_key, 11965d4f98a2SYan Zheng int lowest_level, int max_level) 11975d4f98a2SYan Zheng { 11980b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = dest->fs_info; 11995d4f98a2SYan Zheng struct extent_buffer *eb; 12005d4f98a2SYan Zheng struct extent_buffer *parent; 120182fa113fSQu Wenruo struct btrfs_ref ref = { 0 }; 12025d4f98a2SYan Zheng struct btrfs_key key; 12035d4f98a2SYan Zheng u64 old_bytenr; 12045d4f98a2SYan Zheng u64 new_bytenr; 12055d4f98a2SYan Zheng u64 old_ptr_gen; 12065d4f98a2SYan Zheng u64 new_ptr_gen; 12075d4f98a2SYan Zheng u64 last_snapshot; 12085d4f98a2SYan Zheng u32 blocksize; 12093fd0a558SYan, Zheng int cow = 0; 12105d4f98a2SYan Zheng int level; 12115d4f98a2SYan Zheng int ret; 12125d4f98a2SYan Zheng int slot; 12135d4f98a2SYan Zheng 12147a9213a9SJosef Bacik ASSERT(src->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID); 12157a9213a9SJosef Bacik ASSERT(dest->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); 12165d4f98a2SYan Zheng 12175d4f98a2SYan Zheng last_snapshot = btrfs_root_last_snapshot(&src->root_item); 12183fd0a558SYan, Zheng again: 12195d4f98a2SYan Zheng slot = path->slots[lowest_level]; 12205d4f98a2SYan Zheng btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot); 12215d4f98a2SYan Zheng 12225d4f98a2SYan Zheng eb = btrfs_lock_root_node(dest); 12235d4f98a2SYan Zheng level = btrfs_header_level(eb); 12245d4f98a2SYan Zheng 12255d4f98a2SYan Zheng if (level < lowest_level) { 12265d4f98a2SYan Zheng btrfs_tree_unlock(eb); 12275d4f98a2SYan Zheng free_extent_buffer(eb); 12285d4f98a2SYan Zheng return 0; 12295d4f98a2SYan Zheng } 12305d4f98a2SYan Zheng 12313fd0a558SYan, Zheng if (cow) { 12329631e4ccSJosef Bacik ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb, 12339631e4ccSJosef Bacik BTRFS_NESTING_COW); 1234*45b87c5dSJosef Bacik if (ret) { 1235*45b87c5dSJosef Bacik btrfs_tree_unlock(eb); 1236*45b87c5dSJosef Bacik free_extent_buffer(eb); 1237*45b87c5dSJosef Bacik return ret; 1238*45b87c5dSJosef Bacik } 12393fd0a558SYan, Zheng } 12405d4f98a2SYan Zheng 12415d4f98a2SYan Zheng if (next_key) { 12425d4f98a2SYan Zheng next_key->objectid = (u64)-1; 12435d4f98a2SYan Zheng next_key->type = (u8)-1; 12445d4f98a2SYan Zheng next_key->offset = (u64)-1; 12455d4f98a2SYan Zheng } 12465d4f98a2SYan Zheng 12475d4f98a2SYan Zheng parent = eb; 12485d4f98a2SYan Zheng while (1) { 12495d4f98a2SYan Zheng level = btrfs_header_level(parent); 12507a9213a9SJosef Bacik ASSERT(level >= lowest_level); 12515d4f98a2SYan Zheng 1252e3b83361SQu Wenruo ret = btrfs_bin_search(parent, &key, &slot); 1253cbca7d59SFilipe Manana if (ret < 0) 1254cbca7d59SFilipe Manana break; 12555d4f98a2SYan Zheng if (ret && slot > 0) 12565d4f98a2SYan Zheng slot--; 12575d4f98a2SYan Zheng 12585d4f98a2SYan Zheng if (next_key && slot + 1 < btrfs_header_nritems(parent)) 12595d4f98a2SYan Zheng btrfs_node_key_to_cpu(parent, next_key, slot + 1); 12605d4f98a2SYan Zheng 12615d4f98a2SYan Zheng old_bytenr = btrfs_node_blockptr(parent, slot); 12620b246afaSJeff Mahoney blocksize = fs_info->nodesize; 12635d4f98a2SYan Zheng old_ptr_gen = btrfs_node_ptr_generation(parent, slot); 12645d4f98a2SYan Zheng 12655d4f98a2SYan Zheng if (level <= max_level) { 12665d4f98a2SYan Zheng eb = path->nodes[level]; 12675d4f98a2SYan Zheng new_bytenr = btrfs_node_blockptr(eb, 12685d4f98a2SYan Zheng path->slots[level]); 12695d4f98a2SYan Zheng new_ptr_gen = btrfs_node_ptr_generation(eb, 12705d4f98a2SYan Zheng path->slots[level]); 12715d4f98a2SYan Zheng } else { 12725d4f98a2SYan Zheng new_bytenr = 0; 12735d4f98a2SYan Zheng new_ptr_gen = 0; 12745d4f98a2SYan Zheng } 12755d4f98a2SYan Zheng 1276fae7f21cSDulshani Gunawardhana if (WARN_ON(new_bytenr > 0 && new_bytenr == old_bytenr)) { 12775d4f98a2SYan Zheng ret = level; 12785d4f98a2SYan Zheng break; 12795d4f98a2SYan Zheng } 12805d4f98a2SYan Zheng 12815d4f98a2SYan Zheng if (new_bytenr == 0 || old_ptr_gen > last_snapshot || 12825d4f98a2SYan Zheng memcmp_node_keys(parent, slot, path, level)) { 12833fd0a558SYan, Zheng if (level <= lowest_level) { 12845d4f98a2SYan Zheng ret = 0; 12855d4f98a2SYan Zheng break; 12865d4f98a2SYan Zheng } 12875d4f98a2SYan Zheng 12886b3426beSJosef Bacik eb = btrfs_read_node_slot(parent, slot); 128964c043deSLiu Bo if (IS_ERR(eb)) { 129064c043deSLiu Bo ret = PTR_ERR(eb); 1291264813acSLiu Bo break; 1292416bc658SJosef Bacik } 12935d4f98a2SYan Zheng btrfs_tree_lock(eb); 12943fd0a558SYan, Zheng if (cow) { 12955d4f98a2SYan Zheng ret = btrfs_cow_block(trans, dest, eb, parent, 12969631e4ccSJosef Bacik slot, &eb, 12979631e4ccSJosef Bacik BTRFS_NESTING_COW); 1298*45b87c5dSJosef Bacik if (ret) { 1299*45b87c5dSJosef Bacik btrfs_tree_unlock(eb); 1300*45b87c5dSJosef Bacik free_extent_buffer(eb); 1301*45b87c5dSJosef Bacik break; 1302*45b87c5dSJosef Bacik } 13035d4f98a2SYan Zheng } 13045d4f98a2SYan Zheng 13055d4f98a2SYan Zheng btrfs_tree_unlock(parent); 13065d4f98a2SYan Zheng free_extent_buffer(parent); 13075d4f98a2SYan Zheng 13085d4f98a2SYan Zheng parent = eb; 13095d4f98a2SYan Zheng continue; 13105d4f98a2SYan Zheng } 13115d4f98a2SYan Zheng 13123fd0a558SYan, Zheng if (!cow) { 13133fd0a558SYan, Zheng btrfs_tree_unlock(parent); 13143fd0a558SYan, Zheng free_extent_buffer(parent); 13153fd0a558SYan, Zheng cow = 1; 13163fd0a558SYan, Zheng goto again; 13173fd0a558SYan, Zheng } 13183fd0a558SYan, Zheng 13195d4f98a2SYan Zheng btrfs_node_key_to_cpu(path->nodes[level], &key, 13205d4f98a2SYan Zheng path->slots[level]); 1321b3b4aa74SDavid Sterba btrfs_release_path(path); 13225d4f98a2SYan Zheng 13235d4f98a2SYan Zheng path->lowest_level = level; 13245d4f98a2SYan Zheng ret = btrfs_search_slot(trans, src, &key, path, 0, 1); 13255d4f98a2SYan Zheng path->lowest_level = 0; 13265d4f98a2SYan Zheng BUG_ON(ret); 13275d4f98a2SYan Zheng 13285d4f98a2SYan Zheng /* 1329824d8dffSQu Wenruo * Info qgroup to trace both subtrees. 1330824d8dffSQu Wenruo * 1331824d8dffSQu Wenruo * We must trace both trees. 1332824d8dffSQu Wenruo * 1) Tree reloc subtree 1333824d8dffSQu Wenruo * If not traced, we will leak data numbers 1334824d8dffSQu Wenruo * 2) Fs subtree 1335824d8dffSQu Wenruo * If not traced, we will double count old data 1336f616f5cdSQu Wenruo * 1337f616f5cdSQu Wenruo * We don't scan the subtree right now, but only record 1338f616f5cdSQu Wenruo * the swapped tree blocks. 1339f616f5cdSQu Wenruo * The real subtree rescan is delayed until we have new 1340f616f5cdSQu Wenruo * CoW on the subtree root node before transaction commit. 1341824d8dffSQu Wenruo */ 1342370a11b8SQu Wenruo ret = btrfs_qgroup_add_swapped_blocks(trans, dest, 1343370a11b8SQu Wenruo rc->block_group, parent, slot, 1344370a11b8SQu Wenruo path->nodes[level], path->slots[level], 1345370a11b8SQu Wenruo last_snapshot); 1346370a11b8SQu Wenruo if (ret < 0) 1347370a11b8SQu Wenruo break; 1348824d8dffSQu Wenruo /* 13495d4f98a2SYan Zheng * swap blocks in fs tree and reloc tree. 13505d4f98a2SYan Zheng */ 13515d4f98a2SYan Zheng btrfs_set_node_blockptr(parent, slot, new_bytenr); 13525d4f98a2SYan Zheng btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen); 13535d4f98a2SYan Zheng btrfs_mark_buffer_dirty(parent); 13545d4f98a2SYan Zheng 13555d4f98a2SYan Zheng btrfs_set_node_blockptr(path->nodes[level], 13565d4f98a2SYan Zheng path->slots[level], old_bytenr); 13575d4f98a2SYan Zheng btrfs_set_node_ptr_generation(path->nodes[level], 13585d4f98a2SYan Zheng path->slots[level], old_ptr_gen); 13595d4f98a2SYan Zheng btrfs_mark_buffer_dirty(path->nodes[level]); 13605d4f98a2SYan Zheng 136182fa113fSQu Wenruo btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, old_bytenr, 136282fa113fSQu Wenruo blocksize, path->nodes[level]->start); 136382fa113fSQu Wenruo ref.skip_qgroup = true; 136482fa113fSQu Wenruo btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid); 136582fa113fSQu Wenruo ret = btrfs_inc_extent_ref(trans, &ref); 13665d4f98a2SYan Zheng BUG_ON(ret); 136782fa113fSQu Wenruo btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr, 136882fa113fSQu Wenruo blocksize, 0); 136982fa113fSQu Wenruo ref.skip_qgroup = true; 137082fa113fSQu Wenruo btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid); 137182fa113fSQu Wenruo ret = btrfs_inc_extent_ref(trans, &ref); 13725d4f98a2SYan Zheng BUG_ON(ret); 13735d4f98a2SYan Zheng 1374ffd4bb2aSQu Wenruo btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, new_bytenr, 1375ffd4bb2aSQu Wenruo blocksize, path->nodes[level]->start); 1376ffd4bb2aSQu Wenruo btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid); 1377ffd4bb2aSQu Wenruo ref.skip_qgroup = true; 1378ffd4bb2aSQu Wenruo ret = btrfs_free_extent(trans, &ref); 13795d4f98a2SYan Zheng BUG_ON(ret); 13805d4f98a2SYan Zheng 1381ffd4bb2aSQu Wenruo btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, old_bytenr, 1382ffd4bb2aSQu Wenruo blocksize, 0); 1383ffd4bb2aSQu Wenruo btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid); 1384ffd4bb2aSQu Wenruo ref.skip_qgroup = true; 1385ffd4bb2aSQu Wenruo ret = btrfs_free_extent(trans, &ref); 13865d4f98a2SYan Zheng BUG_ON(ret); 13875d4f98a2SYan Zheng 13885d4f98a2SYan Zheng btrfs_unlock_up_safe(path, 0); 13895d4f98a2SYan Zheng 13905d4f98a2SYan Zheng ret = level; 13915d4f98a2SYan Zheng break; 13925d4f98a2SYan Zheng } 13935d4f98a2SYan Zheng btrfs_tree_unlock(parent); 13945d4f98a2SYan Zheng free_extent_buffer(parent); 13955d4f98a2SYan Zheng return ret; 13965d4f98a2SYan Zheng } 13975d4f98a2SYan Zheng 13985d4f98a2SYan Zheng /* 13995d4f98a2SYan Zheng * helper to find next relocated block in reloc tree 14005d4f98a2SYan Zheng */ 14015d4f98a2SYan Zheng static noinline_for_stack 14025d4f98a2SYan Zheng int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path, 14035d4f98a2SYan Zheng int *level) 14045d4f98a2SYan Zheng { 14055d4f98a2SYan Zheng struct extent_buffer *eb; 14065d4f98a2SYan Zheng int i; 14075d4f98a2SYan Zheng u64 last_snapshot; 14085d4f98a2SYan Zheng u32 nritems; 14095d4f98a2SYan Zheng 14105d4f98a2SYan Zheng last_snapshot = btrfs_root_last_snapshot(&root->root_item); 14115d4f98a2SYan Zheng 14125d4f98a2SYan Zheng for (i = 0; i < *level; i++) { 14135d4f98a2SYan Zheng free_extent_buffer(path->nodes[i]); 14145d4f98a2SYan Zheng path->nodes[i] = NULL; 14155d4f98a2SYan Zheng } 14165d4f98a2SYan Zheng 14175d4f98a2SYan Zheng for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) { 14185d4f98a2SYan Zheng eb = path->nodes[i]; 14195d4f98a2SYan Zheng nritems = btrfs_header_nritems(eb); 14205d4f98a2SYan Zheng while (path->slots[i] + 1 < nritems) { 14215d4f98a2SYan Zheng path->slots[i]++; 14225d4f98a2SYan Zheng if (btrfs_node_ptr_generation(eb, path->slots[i]) <= 14235d4f98a2SYan Zheng last_snapshot) 14245d4f98a2SYan Zheng continue; 14255d4f98a2SYan Zheng 14265d4f98a2SYan Zheng *level = i; 14275d4f98a2SYan Zheng return 0; 14285d4f98a2SYan Zheng } 14295d4f98a2SYan Zheng free_extent_buffer(path->nodes[i]); 14305d4f98a2SYan Zheng path->nodes[i] = NULL; 14315d4f98a2SYan Zheng } 14325d4f98a2SYan Zheng return 1; 14335d4f98a2SYan Zheng } 14345d4f98a2SYan Zheng 14355d4f98a2SYan Zheng /* 14365d4f98a2SYan Zheng * walk down reloc tree to find relocated block of lowest level 14375d4f98a2SYan Zheng */ 14385d4f98a2SYan Zheng static noinline_for_stack 14395d4f98a2SYan Zheng int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path, 14405d4f98a2SYan Zheng int *level) 14415d4f98a2SYan Zheng { 14425d4f98a2SYan Zheng struct extent_buffer *eb = NULL; 14435d4f98a2SYan Zheng int i; 14445d4f98a2SYan Zheng u64 ptr_gen = 0; 14455d4f98a2SYan Zheng u64 last_snapshot; 14465d4f98a2SYan Zheng u32 nritems; 14475d4f98a2SYan Zheng 14485d4f98a2SYan Zheng last_snapshot = btrfs_root_last_snapshot(&root->root_item); 14495d4f98a2SYan Zheng 14505d4f98a2SYan Zheng for (i = *level; i > 0; i--) { 14515d4f98a2SYan Zheng eb = path->nodes[i]; 14525d4f98a2SYan Zheng nritems = btrfs_header_nritems(eb); 14535d4f98a2SYan Zheng while (path->slots[i] < nritems) { 14545d4f98a2SYan Zheng ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]); 14555d4f98a2SYan Zheng if (ptr_gen > last_snapshot) 14565d4f98a2SYan Zheng break; 14575d4f98a2SYan Zheng path->slots[i]++; 14585d4f98a2SYan Zheng } 14595d4f98a2SYan Zheng if (path->slots[i] >= nritems) { 14605d4f98a2SYan Zheng if (i == *level) 14615d4f98a2SYan Zheng break; 14625d4f98a2SYan Zheng *level = i + 1; 14635d4f98a2SYan Zheng return 0; 14645d4f98a2SYan Zheng } 14655d4f98a2SYan Zheng if (i == 1) { 14665d4f98a2SYan Zheng *level = i; 14675d4f98a2SYan Zheng return 0; 14685d4f98a2SYan Zheng } 14695d4f98a2SYan Zheng 14708ef385bbSJosef Bacik eb = btrfs_read_node_slot(eb, path->slots[i]); 14718ef385bbSJosef Bacik if (IS_ERR(eb)) 147264c043deSLiu Bo return PTR_ERR(eb); 14735d4f98a2SYan Zheng BUG_ON(btrfs_header_level(eb) != i - 1); 14745d4f98a2SYan Zheng path->nodes[i - 1] = eb; 14755d4f98a2SYan Zheng path->slots[i - 1] = 0; 14765d4f98a2SYan Zheng } 14775d4f98a2SYan Zheng return 1; 14785d4f98a2SYan Zheng } 14795d4f98a2SYan Zheng 14805d4f98a2SYan Zheng /* 14815d4f98a2SYan Zheng * invalidate extent cache for file extents whose key in range of 14825d4f98a2SYan Zheng * [min_key, max_key) 14835d4f98a2SYan Zheng */ 14845d4f98a2SYan Zheng static int invalidate_extent_cache(struct btrfs_root *root, 14855d4f98a2SYan Zheng struct btrfs_key *min_key, 14865d4f98a2SYan Zheng struct btrfs_key *max_key) 14875d4f98a2SYan Zheng { 14880b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 14895d4f98a2SYan Zheng struct inode *inode = NULL; 14905d4f98a2SYan Zheng u64 objectid; 14915d4f98a2SYan Zheng u64 start, end; 149233345d01SLi Zefan u64 ino; 14935d4f98a2SYan Zheng 14945d4f98a2SYan Zheng objectid = min_key->objectid; 14955d4f98a2SYan Zheng while (1) { 14965d4f98a2SYan Zheng cond_resched(); 14975d4f98a2SYan Zheng iput(inode); 14985d4f98a2SYan Zheng 14995d4f98a2SYan Zheng if (objectid > max_key->objectid) 15005d4f98a2SYan Zheng break; 15015d4f98a2SYan Zheng 15025d4f98a2SYan Zheng inode = find_next_inode(root, objectid); 15035d4f98a2SYan Zheng if (!inode) 15045d4f98a2SYan Zheng break; 15054a0cc7caSNikolay Borisov ino = btrfs_ino(BTRFS_I(inode)); 15065d4f98a2SYan Zheng 150733345d01SLi Zefan if (ino > max_key->objectid) { 15085d4f98a2SYan Zheng iput(inode); 15095d4f98a2SYan Zheng break; 15105d4f98a2SYan Zheng } 15115d4f98a2SYan Zheng 151233345d01SLi Zefan objectid = ino + 1; 15135d4f98a2SYan Zheng if (!S_ISREG(inode->i_mode)) 15145d4f98a2SYan Zheng continue; 15155d4f98a2SYan Zheng 151633345d01SLi Zefan if (unlikely(min_key->objectid == ino)) { 15175d4f98a2SYan Zheng if (min_key->type > BTRFS_EXTENT_DATA_KEY) 15185d4f98a2SYan Zheng continue; 15195d4f98a2SYan Zheng if (min_key->type < BTRFS_EXTENT_DATA_KEY) 15205d4f98a2SYan Zheng start = 0; 15215d4f98a2SYan Zheng else { 15225d4f98a2SYan Zheng start = min_key->offset; 15230b246afaSJeff Mahoney WARN_ON(!IS_ALIGNED(start, fs_info->sectorsize)); 15245d4f98a2SYan Zheng } 15255d4f98a2SYan Zheng } else { 15265d4f98a2SYan Zheng start = 0; 15275d4f98a2SYan Zheng } 15285d4f98a2SYan Zheng 152933345d01SLi Zefan if (unlikely(max_key->objectid == ino)) { 15305d4f98a2SYan Zheng if (max_key->type < BTRFS_EXTENT_DATA_KEY) 15315d4f98a2SYan Zheng continue; 15325d4f98a2SYan Zheng if (max_key->type > BTRFS_EXTENT_DATA_KEY) { 15335d4f98a2SYan Zheng end = (u64)-1; 15345d4f98a2SYan Zheng } else { 15355d4f98a2SYan Zheng if (max_key->offset == 0) 15365d4f98a2SYan Zheng continue; 15375d4f98a2SYan Zheng end = max_key->offset; 15380b246afaSJeff Mahoney WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize)); 15395d4f98a2SYan Zheng end--; 15405d4f98a2SYan Zheng } 15415d4f98a2SYan Zheng } else { 15425d4f98a2SYan Zheng end = (u64)-1; 15435d4f98a2SYan Zheng } 15445d4f98a2SYan Zheng 15455d4f98a2SYan Zheng /* the lock_extent waits for readpage to complete */ 1546d0082371SJeff Mahoney lock_extent(&BTRFS_I(inode)->io_tree, start, end); 1547dcdbc059SNikolay Borisov btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 1); 1548d0082371SJeff Mahoney unlock_extent(&BTRFS_I(inode)->io_tree, start, end); 15495d4f98a2SYan Zheng } 15505d4f98a2SYan Zheng return 0; 15515d4f98a2SYan Zheng } 15525d4f98a2SYan Zheng 15535d4f98a2SYan Zheng static int find_next_key(struct btrfs_path *path, int level, 15545d4f98a2SYan Zheng struct btrfs_key *key) 15555d4f98a2SYan Zheng 15565d4f98a2SYan Zheng { 15575d4f98a2SYan Zheng while (level < BTRFS_MAX_LEVEL) { 15585d4f98a2SYan Zheng if (!path->nodes[level]) 15595d4f98a2SYan Zheng break; 15605d4f98a2SYan Zheng if (path->slots[level] + 1 < 15615d4f98a2SYan Zheng btrfs_header_nritems(path->nodes[level])) { 15625d4f98a2SYan Zheng btrfs_node_key_to_cpu(path->nodes[level], key, 15635d4f98a2SYan Zheng path->slots[level] + 1); 15645d4f98a2SYan Zheng return 0; 15655d4f98a2SYan Zheng } 15665d4f98a2SYan Zheng level++; 15675d4f98a2SYan Zheng } 15685d4f98a2SYan Zheng return 1; 15695d4f98a2SYan Zheng } 15705d4f98a2SYan Zheng 15715d4f98a2SYan Zheng /* 1572d2311e69SQu Wenruo * Insert current subvolume into reloc_control::dirty_subvol_roots 1573d2311e69SQu Wenruo */ 1574ac54da6cSJosef Bacik static int insert_dirty_subvol(struct btrfs_trans_handle *trans, 1575d2311e69SQu Wenruo struct reloc_control *rc, 1576d2311e69SQu Wenruo struct btrfs_root *root) 1577d2311e69SQu Wenruo { 1578d2311e69SQu Wenruo struct btrfs_root *reloc_root = root->reloc_root; 1579d2311e69SQu Wenruo struct btrfs_root_item *reloc_root_item; 15807934133fSJosef Bacik int ret; 1581d2311e69SQu Wenruo 1582d2311e69SQu Wenruo /* @root must be a subvolume tree root with a valid reloc tree */ 1583d2311e69SQu Wenruo ASSERT(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); 1584d2311e69SQu Wenruo ASSERT(reloc_root); 1585d2311e69SQu Wenruo 1586d2311e69SQu Wenruo reloc_root_item = &reloc_root->root_item; 1587d2311e69SQu Wenruo memset(&reloc_root_item->drop_progress, 0, 1588d2311e69SQu Wenruo sizeof(reloc_root_item->drop_progress)); 1589c8422684SDavid Sterba btrfs_set_root_drop_level(reloc_root_item, 0); 1590d2311e69SQu Wenruo btrfs_set_root_refs(reloc_root_item, 0); 15917934133fSJosef Bacik ret = btrfs_update_reloc_root(trans, root); 15927934133fSJosef Bacik if (ret) 15937934133fSJosef Bacik return ret; 1594d2311e69SQu Wenruo 1595d2311e69SQu Wenruo if (list_empty(&root->reloc_dirty_list)) { 159600246528SJosef Bacik btrfs_grab_root(root); 1597d2311e69SQu Wenruo list_add_tail(&root->reloc_dirty_list, &rc->dirty_subvol_roots); 1598d2311e69SQu Wenruo } 1599ac54da6cSJosef Bacik 1600ac54da6cSJosef Bacik return 0; 1601d2311e69SQu Wenruo } 1602d2311e69SQu Wenruo 1603d2311e69SQu Wenruo static int clean_dirty_subvols(struct reloc_control *rc) 1604d2311e69SQu Wenruo { 1605d2311e69SQu Wenruo struct btrfs_root *root; 1606d2311e69SQu Wenruo struct btrfs_root *next; 1607d2311e69SQu Wenruo int ret = 0; 160830d40577SQu Wenruo int ret2; 1609d2311e69SQu Wenruo 1610d2311e69SQu Wenruo list_for_each_entry_safe(root, next, &rc->dirty_subvol_roots, 1611d2311e69SQu Wenruo reloc_dirty_list) { 161230d40577SQu Wenruo if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { 161330d40577SQu Wenruo /* Merged subvolume, cleanup its reloc root */ 1614d2311e69SQu Wenruo struct btrfs_root *reloc_root = root->reloc_root; 1615d2311e69SQu Wenruo 1616d2311e69SQu Wenruo list_del_init(&root->reloc_dirty_list); 1617d2311e69SQu Wenruo root->reloc_root = NULL; 16186282675eSQu Wenruo /* 16196282675eSQu Wenruo * Need barrier to ensure clear_bit() only happens after 16206282675eSQu Wenruo * root->reloc_root = NULL. Pairs with have_reloc_root. 16216282675eSQu Wenruo */ 16226282675eSQu Wenruo smp_wmb(); 16231fac4a54SQu Wenruo clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state); 1624f28de8d8SJosef Bacik if (reloc_root) { 1625f44deb74SJosef Bacik /* 1626f44deb74SJosef Bacik * btrfs_drop_snapshot drops our ref we hold for 1627f44deb74SJosef Bacik * ->reloc_root. If it fails however we must 1628f44deb74SJosef Bacik * drop the ref ourselves. 1629f44deb74SJosef Bacik */ 1630f28de8d8SJosef Bacik ret2 = btrfs_drop_snapshot(reloc_root, 0, 1); 1631f44deb74SJosef Bacik if (ret2 < 0) { 1632f44deb74SJosef Bacik btrfs_put_root(reloc_root); 1633f44deb74SJosef Bacik if (!ret) 1634f28de8d8SJosef Bacik ret = ret2; 1635f28de8d8SJosef Bacik } 1636f44deb74SJosef Bacik } 163700246528SJosef Bacik btrfs_put_root(root); 163830d40577SQu Wenruo } else { 163930d40577SQu Wenruo /* Orphan reloc tree, just clean it up */ 16400078a9f9SNikolay Borisov ret2 = btrfs_drop_snapshot(root, 0, 1); 1641f44deb74SJosef Bacik if (ret2 < 0) { 1642f44deb74SJosef Bacik btrfs_put_root(root); 1643f44deb74SJosef Bacik if (!ret) 164430d40577SQu Wenruo ret = ret2; 164530d40577SQu Wenruo } 1646d2311e69SQu Wenruo } 1647f44deb74SJosef Bacik } 1648d2311e69SQu Wenruo return ret; 1649d2311e69SQu Wenruo } 1650d2311e69SQu Wenruo 1651d2311e69SQu Wenruo /* 16525d4f98a2SYan Zheng * merge the relocated tree blocks in reloc tree with corresponding 16535d4f98a2SYan Zheng * fs tree. 16545d4f98a2SYan Zheng */ 16555d4f98a2SYan Zheng static noinline_for_stack int merge_reloc_root(struct reloc_control *rc, 16565d4f98a2SYan Zheng struct btrfs_root *root) 16575d4f98a2SYan Zheng { 16580b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; 16595d4f98a2SYan Zheng struct btrfs_key key; 16605d4f98a2SYan Zheng struct btrfs_key next_key; 16619e6a0c52SJosef Bacik struct btrfs_trans_handle *trans = NULL; 16625d4f98a2SYan Zheng struct btrfs_root *reloc_root; 16635d4f98a2SYan Zheng struct btrfs_root_item *root_item; 16645d4f98a2SYan Zheng struct btrfs_path *path; 16653fd0a558SYan, Zheng struct extent_buffer *leaf; 1666fca3a45dSJosef Bacik int reserve_level; 16675d4f98a2SYan Zheng int level; 16685d4f98a2SYan Zheng int max_level; 16695d4f98a2SYan Zheng int replaced = 0; 1670c6a592f2SNikolay Borisov int ret = 0; 16713fd0a558SYan, Zheng u32 min_reserved; 16725d4f98a2SYan Zheng 16735d4f98a2SYan Zheng path = btrfs_alloc_path(); 16745d4f98a2SYan Zheng if (!path) 16755d4f98a2SYan Zheng return -ENOMEM; 1676e4058b54SDavid Sterba path->reada = READA_FORWARD; 16775d4f98a2SYan Zheng 16785d4f98a2SYan Zheng reloc_root = root->reloc_root; 16795d4f98a2SYan Zheng root_item = &reloc_root->root_item; 16805d4f98a2SYan Zheng 16815d4f98a2SYan Zheng if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { 16825d4f98a2SYan Zheng level = btrfs_root_level(root_item); 168367439dadSDavid Sterba atomic_inc(&reloc_root->node->refs); 16845d4f98a2SYan Zheng path->nodes[level] = reloc_root->node; 16855d4f98a2SYan Zheng path->slots[level] = 0; 16865d4f98a2SYan Zheng } else { 16875d4f98a2SYan Zheng btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); 16885d4f98a2SYan Zheng 1689c8422684SDavid Sterba level = btrfs_root_drop_level(root_item); 16905d4f98a2SYan Zheng BUG_ON(level == 0); 16915d4f98a2SYan Zheng path->lowest_level = level; 16925d4f98a2SYan Zheng ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0); 169333c66f43SYan Zheng path->lowest_level = 0; 16945d4f98a2SYan Zheng if (ret < 0) { 16955d4f98a2SYan Zheng btrfs_free_path(path); 16965d4f98a2SYan Zheng return ret; 16975d4f98a2SYan Zheng } 16985d4f98a2SYan Zheng 16995d4f98a2SYan Zheng btrfs_node_key_to_cpu(path->nodes[level], &next_key, 17005d4f98a2SYan Zheng path->slots[level]); 17015d4f98a2SYan Zheng WARN_ON(memcmp(&key, &next_key, sizeof(key))); 17025d4f98a2SYan Zheng 17035d4f98a2SYan Zheng btrfs_unlock_up_safe(path, 0); 17045d4f98a2SYan Zheng } 17055d4f98a2SYan Zheng 170644d354abSQu Wenruo /* 170744d354abSQu Wenruo * In merge_reloc_root(), we modify the upper level pointer to swap the 170844d354abSQu Wenruo * tree blocks between reloc tree and subvolume tree. Thus for tree 170944d354abSQu Wenruo * block COW, we COW at most from level 1 to root level for each tree. 171044d354abSQu Wenruo * 171144d354abSQu Wenruo * Thus the needed metadata size is at most root_level * nodesize, 171244d354abSQu Wenruo * and * 2 since we have two trees to COW. 171344d354abSQu Wenruo */ 1714fca3a45dSJosef Bacik reserve_level = max_t(int, 1, btrfs_root_level(root_item)); 1715fca3a45dSJosef Bacik min_reserved = fs_info->nodesize * reserve_level * 2; 17165d4f98a2SYan Zheng memset(&next_key, 0, sizeof(next_key)); 17175d4f98a2SYan Zheng 17185d4f98a2SYan Zheng while (1) { 171908e007d2SMiao Xie ret = btrfs_block_rsv_refill(root, rc->block_rsv, min_reserved, 172044d354abSQu Wenruo BTRFS_RESERVE_FLUSH_LIMIT); 1721c6a592f2SNikolay Borisov if (ret) 17229e6a0c52SJosef Bacik goto out; 17239e6a0c52SJosef Bacik trans = btrfs_start_transaction(root, 0); 17249e6a0c52SJosef Bacik if (IS_ERR(trans)) { 1725c6a592f2SNikolay Borisov ret = PTR_ERR(trans); 17269e6a0c52SJosef Bacik trans = NULL; 17279e6a0c52SJosef Bacik goto out; 17289e6a0c52SJosef Bacik } 17292abc726aSJosef Bacik 17302abc726aSJosef Bacik /* 17312abc726aSJosef Bacik * At this point we no longer have a reloc_control, so we can't 17322abc726aSJosef Bacik * depend on btrfs_init_reloc_root to update our last_trans. 17332abc726aSJosef Bacik * 17342abc726aSJosef Bacik * But that's ok, we started the trans handle on our 17352abc726aSJosef Bacik * corresponding fs_root, which means it's been added to the 17362abc726aSJosef Bacik * dirty list. At commit time we'll still call 17372abc726aSJosef Bacik * btrfs_update_reloc_root() and update our root item 17382abc726aSJosef Bacik * appropriately. 17392abc726aSJosef Bacik */ 17402abc726aSJosef Bacik reloc_root->last_trans = trans->transid; 17419e6a0c52SJosef Bacik trans->block_rsv = rc->block_rsv; 17423fd0a558SYan, Zheng 17433fd0a558SYan, Zheng replaced = 0; 17445d4f98a2SYan Zheng max_level = level; 17455d4f98a2SYan Zheng 17465d4f98a2SYan Zheng ret = walk_down_reloc_tree(reloc_root, path, &level); 1747c6a592f2SNikolay Borisov if (ret < 0) 17485d4f98a2SYan Zheng goto out; 17495d4f98a2SYan Zheng if (ret > 0) 17505d4f98a2SYan Zheng break; 17515d4f98a2SYan Zheng 17525d4f98a2SYan Zheng if (!find_next_key(path, level, &key) && 17535d4f98a2SYan Zheng btrfs_comp_cpu_keys(&next_key, &key) >= 0) { 17545d4f98a2SYan Zheng ret = 0; 17555d4f98a2SYan Zheng } else { 17563d0174f7SQu Wenruo ret = replace_path(trans, rc, root, reloc_root, path, 17573fd0a558SYan, Zheng &next_key, level, max_level); 17585d4f98a2SYan Zheng } 1759c6a592f2SNikolay Borisov if (ret < 0) 17605d4f98a2SYan Zheng goto out; 17615d4f98a2SYan Zheng if (ret > 0) { 17625d4f98a2SYan Zheng level = ret; 17635d4f98a2SYan Zheng btrfs_node_key_to_cpu(path->nodes[level], &key, 17645d4f98a2SYan Zheng path->slots[level]); 17655d4f98a2SYan Zheng replaced = 1; 17665d4f98a2SYan Zheng } 17675d4f98a2SYan Zheng 17685d4f98a2SYan Zheng ret = walk_up_reloc_tree(reloc_root, path, &level); 17695d4f98a2SYan Zheng if (ret > 0) 17705d4f98a2SYan Zheng break; 17715d4f98a2SYan Zheng 17725d4f98a2SYan Zheng BUG_ON(level == 0); 17735d4f98a2SYan Zheng /* 17745d4f98a2SYan Zheng * save the merging progress in the drop_progress. 17755d4f98a2SYan Zheng * this is OK since root refs == 1 in this case. 17765d4f98a2SYan Zheng */ 17775d4f98a2SYan Zheng btrfs_node_key(path->nodes[level], &root_item->drop_progress, 17785d4f98a2SYan Zheng path->slots[level]); 1779c8422684SDavid Sterba btrfs_set_root_drop_level(root_item, level); 17805d4f98a2SYan Zheng 17813a45bb20SJeff Mahoney btrfs_end_transaction_throttle(trans); 17829e6a0c52SJosef Bacik trans = NULL; 17835d4f98a2SYan Zheng 17842ff7e61eSJeff Mahoney btrfs_btree_balance_dirty(fs_info); 17855d4f98a2SYan Zheng 17865d4f98a2SYan Zheng if (replaced && rc->stage == UPDATE_DATA_PTRS) 17875d4f98a2SYan Zheng invalidate_extent_cache(root, &key, &next_key); 17885d4f98a2SYan Zheng } 17895d4f98a2SYan Zheng 17905d4f98a2SYan Zheng /* 17915d4f98a2SYan Zheng * handle the case only one block in the fs tree need to be 17925d4f98a2SYan Zheng * relocated and the block is tree root. 17935d4f98a2SYan Zheng */ 17945d4f98a2SYan Zheng leaf = btrfs_lock_root_node(root); 17959631e4ccSJosef Bacik ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf, 17969631e4ccSJosef Bacik BTRFS_NESTING_COW); 17975d4f98a2SYan Zheng btrfs_tree_unlock(leaf); 17985d4f98a2SYan Zheng free_extent_buffer(leaf); 17995d4f98a2SYan Zheng out: 18005d4f98a2SYan Zheng btrfs_free_path(path); 18015d4f98a2SYan Zheng 1802ac54da6cSJosef Bacik if (ret == 0) { 1803ac54da6cSJosef Bacik ret = insert_dirty_subvol(trans, rc, root); 1804ac54da6cSJosef Bacik if (ret) 1805ac54da6cSJosef Bacik btrfs_abort_transaction(trans, ret); 1806ac54da6cSJosef Bacik } 18075d4f98a2SYan Zheng 18089e6a0c52SJosef Bacik if (trans) 18093a45bb20SJeff Mahoney btrfs_end_transaction_throttle(trans); 18105d4f98a2SYan Zheng 18112ff7e61eSJeff Mahoney btrfs_btree_balance_dirty(fs_info); 18125d4f98a2SYan Zheng 18135d4f98a2SYan Zheng if (replaced && rc->stage == UPDATE_DATA_PTRS) 18145d4f98a2SYan Zheng invalidate_extent_cache(root, &key, &next_key); 18155d4f98a2SYan Zheng 1816c6a592f2SNikolay Borisov return ret; 18175d4f98a2SYan Zheng } 18185d4f98a2SYan Zheng 18193fd0a558SYan, Zheng static noinline_for_stack 18203fd0a558SYan, Zheng int prepare_to_merge(struct reloc_control *rc, int err) 18215d4f98a2SYan Zheng { 18223fd0a558SYan, Zheng struct btrfs_root *root = rc->extent_root; 18230b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 18243fd0a558SYan, Zheng struct btrfs_root *reloc_root; 18255d4f98a2SYan Zheng struct btrfs_trans_handle *trans; 18263fd0a558SYan, Zheng LIST_HEAD(reloc_roots); 18273fd0a558SYan, Zheng u64 num_bytes = 0; 18283fd0a558SYan, Zheng int ret; 18293fd0a558SYan, Zheng 18300b246afaSJeff Mahoney mutex_lock(&fs_info->reloc_mutex); 18310b246afaSJeff Mahoney rc->merging_rsv_size += fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2; 18323fd0a558SYan, Zheng rc->merging_rsv_size += rc->nodes_relocated * 2; 18330b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 18347585717fSChris Mason 18353fd0a558SYan, Zheng again: 18363fd0a558SYan, Zheng if (!err) { 18373fd0a558SYan, Zheng num_bytes = rc->merging_rsv_size; 183808e007d2SMiao Xie ret = btrfs_block_rsv_add(root, rc->block_rsv, num_bytes, 183908e007d2SMiao Xie BTRFS_RESERVE_FLUSH_ALL); 18403fd0a558SYan, Zheng if (ret) 18413fd0a558SYan, Zheng err = ret; 18423fd0a558SYan, Zheng } 18433fd0a558SYan, Zheng 18447a7eaa40SJosef Bacik trans = btrfs_join_transaction(rc->extent_root); 18453612b495STsutomu Itoh if (IS_ERR(trans)) { 18463612b495STsutomu Itoh if (!err) 18472ff7e61eSJeff Mahoney btrfs_block_rsv_release(fs_info, rc->block_rsv, 184863f018beSNikolay Borisov num_bytes, NULL); 18493612b495STsutomu Itoh return PTR_ERR(trans); 18503612b495STsutomu Itoh } 18513fd0a558SYan, Zheng 18523fd0a558SYan, Zheng if (!err) { 18533fd0a558SYan, Zheng if (num_bytes != rc->merging_rsv_size) { 18543a45bb20SJeff Mahoney btrfs_end_transaction(trans); 18552ff7e61eSJeff Mahoney btrfs_block_rsv_release(fs_info, rc->block_rsv, 185663f018beSNikolay Borisov num_bytes, NULL); 18573fd0a558SYan, Zheng goto again; 18583fd0a558SYan, Zheng } 18593fd0a558SYan, Zheng } 18603fd0a558SYan, Zheng 18613fd0a558SYan, Zheng rc->merge_reloc_tree = 1; 18623fd0a558SYan, Zheng 18633fd0a558SYan, Zheng while (!list_empty(&rc->reloc_roots)) { 18643fd0a558SYan, Zheng reloc_root = list_entry(rc->reloc_roots.next, 18653fd0a558SYan, Zheng struct btrfs_root, root_list); 18663fd0a558SYan, Zheng list_del_init(&reloc_root->root_list); 18673fd0a558SYan, Zheng 1868a820feb5SDavid Sterba root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset, 1869a820feb5SDavid Sterba false); 18703fd0a558SYan, Zheng BUG_ON(IS_ERR(root)); 18713fd0a558SYan, Zheng BUG_ON(root->reloc_root != reloc_root); 18723fd0a558SYan, Zheng 18733fd0a558SYan, Zheng /* 18743fd0a558SYan, Zheng * set reference count to 1, so btrfs_recover_relocation 18753fd0a558SYan, Zheng * knows it should resumes merging 18763fd0a558SYan, Zheng */ 18773fd0a558SYan, Zheng if (!err) 18783fd0a558SYan, Zheng btrfs_set_root_refs(&reloc_root->root_item, 1); 1879bbae13f8SJosef Bacik ret = btrfs_update_reloc_root(trans, root); 18803fd0a558SYan, Zheng 1881bbae13f8SJosef Bacik /* 1882bbae13f8SJosef Bacik * Even if we have an error we need this reloc root back on our 1883bbae13f8SJosef Bacik * list so we can clean up properly. 1884bbae13f8SJosef Bacik */ 18853fd0a558SYan, Zheng list_add(&reloc_root->root_list, &reloc_roots); 188600246528SJosef Bacik btrfs_put_root(root); 1887bbae13f8SJosef Bacik 1888bbae13f8SJosef Bacik if (ret) { 1889bbae13f8SJosef Bacik btrfs_abort_transaction(trans, ret); 1890bbae13f8SJosef Bacik if (!err) 1891bbae13f8SJosef Bacik err = ret; 1892bbae13f8SJosef Bacik break; 1893bbae13f8SJosef Bacik } 18943fd0a558SYan, Zheng } 18953fd0a558SYan, Zheng 18963fd0a558SYan, Zheng list_splice(&reloc_roots, &rc->reloc_roots); 18973fd0a558SYan, Zheng 18983fd0a558SYan, Zheng if (!err) 18993a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 19003fd0a558SYan, Zheng else 19013a45bb20SJeff Mahoney btrfs_end_transaction(trans); 19023fd0a558SYan, Zheng return err; 19033fd0a558SYan, Zheng } 19043fd0a558SYan, Zheng 19053fd0a558SYan, Zheng static noinline_for_stack 1906aca1bba6SLiu Bo void free_reloc_roots(struct list_head *list) 1907aca1bba6SLiu Bo { 1908a7571232SNikolay Borisov struct btrfs_root *reloc_root, *tmp; 1909aca1bba6SLiu Bo 1910a7571232SNikolay Borisov list_for_each_entry_safe(reloc_root, tmp, list, root_list) 1911bb166d72SNaohiro Aota __del_reloc_root(reloc_root); 1912aca1bba6SLiu Bo } 1913aca1bba6SLiu Bo 1914aca1bba6SLiu Bo static noinline_for_stack 191594404e82SDavid Sterba void merge_reloc_roots(struct reloc_control *rc) 19163fd0a558SYan, Zheng { 19170b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; 19185d4f98a2SYan Zheng struct btrfs_root *root; 19195d4f98a2SYan Zheng struct btrfs_root *reloc_root; 19203fd0a558SYan, Zheng LIST_HEAD(reloc_roots); 19213fd0a558SYan, Zheng int found = 0; 1922aca1bba6SLiu Bo int ret = 0; 19233fd0a558SYan, Zheng again: 19243fd0a558SYan, Zheng root = rc->extent_root; 19257585717fSChris Mason 19267585717fSChris Mason /* 19277585717fSChris Mason * this serializes us with btrfs_record_root_in_transaction, 19287585717fSChris Mason * we have to make sure nobody is in the middle of 19297585717fSChris Mason * adding their roots to the list while we are 19307585717fSChris Mason * doing this splice 19317585717fSChris Mason */ 19320b246afaSJeff Mahoney mutex_lock(&fs_info->reloc_mutex); 19333fd0a558SYan, Zheng list_splice_init(&rc->reloc_roots, &reloc_roots); 19340b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 19355d4f98a2SYan Zheng 19363fd0a558SYan, Zheng while (!list_empty(&reloc_roots)) { 19373fd0a558SYan, Zheng found = 1; 19383fd0a558SYan, Zheng reloc_root = list_entry(reloc_roots.next, 19393fd0a558SYan, Zheng struct btrfs_root, root_list); 19405d4f98a2SYan Zheng 1941a820feb5SDavid Sterba root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset, 1942a820feb5SDavid Sterba false); 19435d4f98a2SYan Zheng if (btrfs_root_refs(&reloc_root->root_item) > 0) { 19445d4f98a2SYan Zheng BUG_ON(IS_ERR(root)); 19455d4f98a2SYan Zheng BUG_ON(root->reloc_root != reloc_root); 19463fd0a558SYan, Zheng ret = merge_reloc_root(rc, root); 194700246528SJosef Bacik btrfs_put_root(root); 1948b37b39cdSJosef Bacik if (ret) { 194925e293c2SWang Shilong if (list_empty(&reloc_root->root_list)) 195025e293c2SWang Shilong list_add_tail(&reloc_root->root_list, 195125e293c2SWang Shilong &reloc_roots); 1952aca1bba6SLiu Bo goto out; 1953b37b39cdSJosef Bacik } 19543fd0a558SYan, Zheng } else { 195551415b6cSQu Wenruo if (!IS_ERR(root)) { 195651415b6cSQu Wenruo if (root->reloc_root == reloc_root) { 195751415b6cSQu Wenruo root->reloc_root = NULL; 195851415b6cSQu Wenruo btrfs_put_root(reloc_root); 195951415b6cSQu Wenruo } 19601dae7e0eSQu Wenruo clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE, 19611dae7e0eSQu Wenruo &root->state); 196251415b6cSQu Wenruo btrfs_put_root(root); 196351415b6cSQu Wenruo } 196451415b6cSQu Wenruo 19653fd0a558SYan, Zheng list_del_init(&reloc_root->root_list); 196630d40577SQu Wenruo /* Don't forget to queue this reloc root for cleanup */ 196730d40577SQu Wenruo list_add_tail(&reloc_root->reloc_dirty_list, 196830d40577SQu Wenruo &rc->dirty_subvol_roots); 19693fd0a558SYan, Zheng } 19705d4f98a2SYan Zheng } 19715d4f98a2SYan Zheng 19723fd0a558SYan, Zheng if (found) { 19733fd0a558SYan, Zheng found = 0; 19743fd0a558SYan, Zheng goto again; 19755d4f98a2SYan Zheng } 1976aca1bba6SLiu Bo out: 1977aca1bba6SLiu Bo if (ret) { 19780b246afaSJeff Mahoney btrfs_handle_fs_error(fs_info, ret, NULL); 1979aca1bba6SLiu Bo free_reloc_roots(&reloc_roots); 1980467bb1d2SWang Shilong 1981467bb1d2SWang Shilong /* new reloc root may be added */ 19820b246afaSJeff Mahoney mutex_lock(&fs_info->reloc_mutex); 1983467bb1d2SWang Shilong list_splice_init(&rc->reloc_roots, &reloc_roots); 19840b246afaSJeff Mahoney mutex_unlock(&fs_info->reloc_mutex); 1985467bb1d2SWang Shilong free_reloc_roots(&reloc_roots); 1986aca1bba6SLiu Bo } 1987aca1bba6SLiu Bo 19887b7b7431SJosef Bacik /* 19897b7b7431SJosef Bacik * We used to have 19907b7b7431SJosef Bacik * 19917b7b7431SJosef Bacik * BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root)); 19927b7b7431SJosef Bacik * 19937b7b7431SJosef Bacik * here, but it's wrong. If we fail to start the transaction in 19947b7b7431SJosef Bacik * prepare_to_merge() we will have only 0 ref reloc roots, none of which 19957b7b7431SJosef Bacik * have actually been removed from the reloc_root_tree rb tree. This is 19967b7b7431SJosef Bacik * fine because we're bailing here, and we hold a reference on the root 19977b7b7431SJosef Bacik * for the list that holds it, so these roots will be cleaned up when we 19987b7b7431SJosef Bacik * do the reloc_dirty_list afterwards. Meanwhile the root->reloc_root 19997b7b7431SJosef Bacik * will be cleaned up on unmount. 20007b7b7431SJosef Bacik * 20017b7b7431SJosef Bacik * The remaining nodes will be cleaned up by free_reloc_control. 20027b7b7431SJosef Bacik */ 20035d4f98a2SYan Zheng } 20045d4f98a2SYan Zheng 20055d4f98a2SYan Zheng static void free_block_list(struct rb_root *blocks) 20065d4f98a2SYan Zheng { 20075d4f98a2SYan Zheng struct tree_block *block; 20085d4f98a2SYan Zheng struct rb_node *rb_node; 20095d4f98a2SYan Zheng while ((rb_node = rb_first(blocks))) { 20105d4f98a2SYan Zheng block = rb_entry(rb_node, struct tree_block, rb_node); 20115d4f98a2SYan Zheng rb_erase(rb_node, blocks); 20125d4f98a2SYan Zheng kfree(block); 20135d4f98a2SYan Zheng } 20145d4f98a2SYan Zheng } 20155d4f98a2SYan Zheng 20165d4f98a2SYan Zheng static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans, 20175d4f98a2SYan Zheng struct btrfs_root *reloc_root) 20185d4f98a2SYan Zheng { 20190b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = reloc_root->fs_info; 20205d4f98a2SYan Zheng struct btrfs_root *root; 2021442b1ac5SJosef Bacik int ret; 20225d4f98a2SYan Zheng 20235d4f98a2SYan Zheng if (reloc_root->last_trans == trans->transid) 20245d4f98a2SYan Zheng return 0; 20255d4f98a2SYan Zheng 2026a820feb5SDavid Sterba root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset, false); 2027404bccbcSJosef Bacik 2028404bccbcSJosef Bacik /* 2029404bccbcSJosef Bacik * This should succeed, since we can't have a reloc root without having 2030404bccbcSJosef Bacik * already looked up the actual root and created the reloc root for this 2031404bccbcSJosef Bacik * root. 2032404bccbcSJosef Bacik * 2033404bccbcSJosef Bacik * However if there's some sort of corruption where we have a ref to a 2034404bccbcSJosef Bacik * reloc root without a corresponding root this could return ENOENT. 2035404bccbcSJosef Bacik */ 2036404bccbcSJosef Bacik if (IS_ERR(root)) { 2037404bccbcSJosef Bacik ASSERT(0); 2038404bccbcSJosef Bacik return PTR_ERR(root); 2039404bccbcSJosef Bacik } 2040404bccbcSJosef Bacik if (root->reloc_root != reloc_root) { 2041404bccbcSJosef Bacik ASSERT(0); 2042404bccbcSJosef Bacik btrfs_err(fs_info, 2043404bccbcSJosef Bacik "root %llu has two reloc roots associated with it", 2044404bccbcSJosef Bacik reloc_root->root_key.offset); 2045404bccbcSJosef Bacik btrfs_put_root(root); 2046404bccbcSJosef Bacik return -EUCLEAN; 2047404bccbcSJosef Bacik } 2048442b1ac5SJosef Bacik ret = btrfs_record_root_in_trans(trans, root); 204900246528SJosef Bacik btrfs_put_root(root); 20505d4f98a2SYan Zheng 2051442b1ac5SJosef Bacik return ret; 20525d4f98a2SYan Zheng } 20535d4f98a2SYan Zheng 20543fd0a558SYan, Zheng static noinline_for_stack 20553fd0a558SYan, Zheng struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans, 20563fd0a558SYan, Zheng struct reloc_control *rc, 2057a26195a5SQu Wenruo struct btrfs_backref_node *node, 2058a26195a5SQu Wenruo struct btrfs_backref_edge *edges[]) 20595d4f98a2SYan Zheng { 2060a26195a5SQu Wenruo struct btrfs_backref_node *next; 20615d4f98a2SYan Zheng struct btrfs_root *root; 20623fd0a558SYan, Zheng int index = 0; 206392de551bSJosef Bacik int ret; 20643fd0a558SYan, Zheng 20655d4f98a2SYan Zheng next = node; 20665d4f98a2SYan Zheng while (1) { 20675d4f98a2SYan Zheng cond_resched(); 20685d4f98a2SYan Zheng next = walk_up_backref(next, edges, &index); 20695d4f98a2SYan Zheng root = next->root; 20708ee66afeSJosef Bacik 20718ee66afeSJosef Bacik /* 20728ee66afeSJosef Bacik * If there is no root, then our references for this block are 20738ee66afeSJosef Bacik * incomplete, as we should be able to walk all the way up to a 20748ee66afeSJosef Bacik * block that is owned by a root. 20758ee66afeSJosef Bacik * 20768ee66afeSJosef Bacik * This path is only for SHAREABLE roots, so if we come upon a 20778ee66afeSJosef Bacik * non-SHAREABLE root then we have backrefs that resolve 20788ee66afeSJosef Bacik * improperly. 20798ee66afeSJosef Bacik * 20808ee66afeSJosef Bacik * Both of these cases indicate file system corruption, or a bug 20818ee66afeSJosef Bacik * in the backref walking code. 20828ee66afeSJosef Bacik */ 20838ee66afeSJosef Bacik if (!root) { 20848ee66afeSJosef Bacik ASSERT(0); 20858ee66afeSJosef Bacik btrfs_err(trans->fs_info, 20868ee66afeSJosef Bacik "bytenr %llu doesn't have a backref path ending in a root", 20878ee66afeSJosef Bacik node->bytenr); 20888ee66afeSJosef Bacik return ERR_PTR(-EUCLEAN); 20898ee66afeSJosef Bacik } 20908ee66afeSJosef Bacik if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) { 20918ee66afeSJosef Bacik ASSERT(0); 20928ee66afeSJosef Bacik btrfs_err(trans->fs_info, 20938ee66afeSJosef Bacik "bytenr %llu has multiple refs with one ending in a non-shareable root", 20948ee66afeSJosef Bacik node->bytenr); 20958ee66afeSJosef Bacik return ERR_PTR(-EUCLEAN); 20968ee66afeSJosef Bacik } 20975d4f98a2SYan Zheng 20985d4f98a2SYan Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) { 209992de551bSJosef Bacik ret = record_reloc_root_in_trans(trans, root); 210092de551bSJosef Bacik if (ret) 210192de551bSJosef Bacik return ERR_PTR(ret); 21025d4f98a2SYan Zheng break; 21035d4f98a2SYan Zheng } 21045d4f98a2SYan Zheng 210592de551bSJosef Bacik ret = btrfs_record_root_in_trans(trans, root); 210692de551bSJosef Bacik if (ret) 210792de551bSJosef Bacik return ERR_PTR(ret); 21083fd0a558SYan, Zheng root = root->reloc_root; 21093fd0a558SYan, Zheng 211039200e59SJosef Bacik /* 211139200e59SJosef Bacik * We could have raced with another thread which failed, so 211239200e59SJosef Bacik * root->reloc_root may not be set, return ENOENT in this case. 211339200e59SJosef Bacik */ 211439200e59SJosef Bacik if (!root) 211539200e59SJosef Bacik return ERR_PTR(-ENOENT); 211639200e59SJosef Bacik 21173fd0a558SYan, Zheng if (next->new_bytenr != root->node->start) { 21188ee66afeSJosef Bacik /* 21198ee66afeSJosef Bacik * We just created the reloc root, so we shouldn't have 21208ee66afeSJosef Bacik * ->new_bytenr set and this shouldn't be in the changed 21218ee66afeSJosef Bacik * list. If it is then we have multiple roots pointing 21228ee66afeSJosef Bacik * at the same bytenr which indicates corruption, or 21238ee66afeSJosef Bacik * we've made a mistake in the backref walking code. 21248ee66afeSJosef Bacik */ 21258ee66afeSJosef Bacik ASSERT(next->new_bytenr == 0); 21268ee66afeSJosef Bacik ASSERT(list_empty(&next->list)); 21278ee66afeSJosef Bacik if (next->new_bytenr || !list_empty(&next->list)) { 21288ee66afeSJosef Bacik btrfs_err(trans->fs_info, 21298ee66afeSJosef Bacik "bytenr %llu possibly has multiple roots pointing at the same bytenr %llu", 21308ee66afeSJosef Bacik node->bytenr, next->bytenr); 21318ee66afeSJosef Bacik return ERR_PTR(-EUCLEAN); 21328ee66afeSJosef Bacik } 21338ee66afeSJosef Bacik 21343fd0a558SYan, Zheng next->new_bytenr = root->node->start; 213500246528SJosef Bacik btrfs_put_root(next->root); 213600246528SJosef Bacik next->root = btrfs_grab_root(root); 21370b530bc5SJosef Bacik ASSERT(next->root); 21383fd0a558SYan, Zheng list_add_tail(&next->list, 21393fd0a558SYan, Zheng &rc->backref_cache.changed); 21409569cc20SQu Wenruo mark_block_processed(rc, next); 21415d4f98a2SYan Zheng break; 21425d4f98a2SYan Zheng } 21435d4f98a2SYan Zheng 21443fd0a558SYan, Zheng WARN_ON(1); 21455d4f98a2SYan Zheng root = NULL; 21465d4f98a2SYan Zheng next = walk_down_backref(edges, &index); 21475d4f98a2SYan Zheng if (!next || next->level <= node->level) 21485d4f98a2SYan Zheng break; 21495d4f98a2SYan Zheng } 2150cbdc2ebcSJosef Bacik if (!root) { 2151cbdc2ebcSJosef Bacik /* 2152cbdc2ebcSJosef Bacik * This can happen if there's fs corruption or if there's a bug 2153cbdc2ebcSJosef Bacik * in the backref lookup code. 2154cbdc2ebcSJosef Bacik */ 2155cbdc2ebcSJosef Bacik ASSERT(0); 2156cbdc2ebcSJosef Bacik return ERR_PTR(-ENOENT); 2157cbdc2ebcSJosef Bacik } 21585d4f98a2SYan Zheng 21593fd0a558SYan, Zheng next = node; 21603fd0a558SYan, Zheng /* setup backref node path for btrfs_reloc_cow_block */ 21613fd0a558SYan, Zheng while (1) { 21623fd0a558SYan, Zheng rc->backref_cache.path[next->level] = next; 21633fd0a558SYan, Zheng if (--index < 0) 21643fd0a558SYan, Zheng break; 21653fd0a558SYan, Zheng next = edges[index]->node[UPPER]; 21663fd0a558SYan, Zheng } 21675d4f98a2SYan Zheng return root; 21685d4f98a2SYan Zheng } 21695d4f98a2SYan Zheng 21703fd0a558SYan, Zheng /* 217192a7cc42SQu Wenruo * Select a tree root for relocation. 217292a7cc42SQu Wenruo * 217392a7cc42SQu Wenruo * Return NULL if the block is not shareable. We should use do_relocation() in 217492a7cc42SQu Wenruo * this case. 217592a7cc42SQu Wenruo * 217692a7cc42SQu Wenruo * Return a tree root pointer if the block is shareable. 217792a7cc42SQu Wenruo * Return -ENOENT if the block is root of reloc tree. 21783fd0a558SYan, Zheng */ 21795d4f98a2SYan Zheng static noinline_for_stack 2180a26195a5SQu Wenruo struct btrfs_root *select_one_root(struct btrfs_backref_node *node) 21815d4f98a2SYan Zheng { 2182a26195a5SQu Wenruo struct btrfs_backref_node *next; 21833fd0a558SYan, Zheng struct btrfs_root *root; 21843fd0a558SYan, Zheng struct btrfs_root *fs_root = NULL; 2185a26195a5SQu Wenruo struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1]; 21863fd0a558SYan, Zheng int index = 0; 21873fd0a558SYan, Zheng 21883fd0a558SYan, Zheng next = node; 21893fd0a558SYan, Zheng while (1) { 21903fd0a558SYan, Zheng cond_resched(); 21913fd0a558SYan, Zheng next = walk_up_backref(next, edges, &index); 21923fd0a558SYan, Zheng root = next->root; 21933fd0a558SYan, Zheng BUG_ON(!root); 21943fd0a558SYan, Zheng 219592a7cc42SQu Wenruo /* No other choice for non-shareable tree */ 219692a7cc42SQu Wenruo if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) 21973fd0a558SYan, Zheng return root; 21983fd0a558SYan, Zheng 21993fd0a558SYan, Zheng if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) 22003fd0a558SYan, Zheng fs_root = root; 22013fd0a558SYan, Zheng 22023fd0a558SYan, Zheng if (next != node) 22033fd0a558SYan, Zheng return NULL; 22043fd0a558SYan, Zheng 22053fd0a558SYan, Zheng next = walk_down_backref(edges, &index); 22063fd0a558SYan, Zheng if (!next || next->level <= node->level) 22073fd0a558SYan, Zheng break; 22083fd0a558SYan, Zheng } 22093fd0a558SYan, Zheng 22103fd0a558SYan, Zheng if (!fs_root) 22113fd0a558SYan, Zheng return ERR_PTR(-ENOENT); 22123fd0a558SYan, Zheng return fs_root; 22135d4f98a2SYan Zheng } 22145d4f98a2SYan Zheng 22155d4f98a2SYan Zheng static noinline_for_stack 22163fd0a558SYan, Zheng u64 calcu_metadata_size(struct reloc_control *rc, 2217a26195a5SQu Wenruo struct btrfs_backref_node *node, int reserve) 22185d4f98a2SYan Zheng { 22190b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; 2220a26195a5SQu Wenruo struct btrfs_backref_node *next = node; 2221a26195a5SQu Wenruo struct btrfs_backref_edge *edge; 2222a26195a5SQu Wenruo struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1]; 22233fd0a558SYan, Zheng u64 num_bytes = 0; 22243fd0a558SYan, Zheng int index = 0; 22255d4f98a2SYan Zheng 22263fd0a558SYan, Zheng BUG_ON(reserve && node->processed); 22273fd0a558SYan, Zheng 22283fd0a558SYan, Zheng while (next) { 22293fd0a558SYan, Zheng cond_resched(); 22305d4f98a2SYan Zheng while (1) { 22313fd0a558SYan, Zheng if (next->processed && (reserve || next != node)) 22325d4f98a2SYan Zheng break; 22335d4f98a2SYan Zheng 22340b246afaSJeff Mahoney num_bytes += fs_info->nodesize; 22353fd0a558SYan, Zheng 22363fd0a558SYan, Zheng if (list_empty(&next->upper)) 22373fd0a558SYan, Zheng break; 22383fd0a558SYan, Zheng 22393fd0a558SYan, Zheng edge = list_entry(next->upper.next, 2240a26195a5SQu Wenruo struct btrfs_backref_edge, list[LOWER]); 22413fd0a558SYan, Zheng edges[index++] = edge; 22423fd0a558SYan, Zheng next = edge->node[UPPER]; 22435d4f98a2SYan Zheng } 22443fd0a558SYan, Zheng next = walk_down_backref(edges, &index); 22453fd0a558SYan, Zheng } 22463fd0a558SYan, Zheng return num_bytes; 22473fd0a558SYan, Zheng } 22483fd0a558SYan, Zheng 22493fd0a558SYan, Zheng static int reserve_metadata_space(struct btrfs_trans_handle *trans, 22503fd0a558SYan, Zheng struct reloc_control *rc, 2251a26195a5SQu Wenruo struct btrfs_backref_node *node) 22523fd0a558SYan, Zheng { 22533fd0a558SYan, Zheng struct btrfs_root *root = rc->extent_root; 2254da17066cSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 22553fd0a558SYan, Zheng u64 num_bytes; 22563fd0a558SYan, Zheng int ret; 22570647bf56SWang Shilong u64 tmp; 22583fd0a558SYan, Zheng 22593fd0a558SYan, Zheng num_bytes = calcu_metadata_size(rc, node, 1) * 2; 22603fd0a558SYan, Zheng 22613fd0a558SYan, Zheng trans->block_rsv = rc->block_rsv; 22620647bf56SWang Shilong rc->reserved_bytes += num_bytes; 22638ca17f0fSJosef Bacik 22648ca17f0fSJosef Bacik /* 22658ca17f0fSJosef Bacik * We are under a transaction here so we can only do limited flushing. 22668ca17f0fSJosef Bacik * If we get an enospc just kick back -EAGAIN so we know to drop the 22678ca17f0fSJosef Bacik * transaction and try to refill when we can flush all the things. 22688ca17f0fSJosef Bacik */ 22690647bf56SWang Shilong ret = btrfs_block_rsv_refill(root, rc->block_rsv, num_bytes, 22708ca17f0fSJosef Bacik BTRFS_RESERVE_FLUSH_LIMIT); 22713fd0a558SYan, Zheng if (ret) { 2272da17066cSJeff Mahoney tmp = fs_info->nodesize * RELOCATION_RESERVED_NODES; 22730647bf56SWang Shilong while (tmp <= rc->reserved_bytes) 22740647bf56SWang Shilong tmp <<= 1; 22750647bf56SWang Shilong /* 22760647bf56SWang Shilong * only one thread can access block_rsv at this point, 22770647bf56SWang Shilong * so we don't need hold lock to protect block_rsv. 22780647bf56SWang Shilong * we expand more reservation size here to allow enough 227952042d8eSAndrea Gelmini * space for relocation and we will return earlier in 22800647bf56SWang Shilong * enospc case. 22810647bf56SWang Shilong */ 2282da17066cSJeff Mahoney rc->block_rsv->size = tmp + fs_info->nodesize * 22830647bf56SWang Shilong RELOCATION_RESERVED_NODES; 22848ca17f0fSJosef Bacik return -EAGAIN; 22853fd0a558SYan, Zheng } 22863fd0a558SYan, Zheng 22873fd0a558SYan, Zheng return 0; 22883fd0a558SYan, Zheng } 22893fd0a558SYan, Zheng 22905d4f98a2SYan Zheng /* 22915d4f98a2SYan Zheng * relocate a block tree, and then update pointers in upper level 22925d4f98a2SYan Zheng * blocks that reference the block to point to the new location. 22935d4f98a2SYan Zheng * 22945d4f98a2SYan Zheng * if called by link_to_upper, the block has already been relocated. 22955d4f98a2SYan Zheng * in that case this function just updates pointers. 22965d4f98a2SYan Zheng */ 22975d4f98a2SYan Zheng static int do_relocation(struct btrfs_trans_handle *trans, 22983fd0a558SYan, Zheng struct reloc_control *rc, 2299a26195a5SQu Wenruo struct btrfs_backref_node *node, 23005d4f98a2SYan Zheng struct btrfs_key *key, 23015d4f98a2SYan Zheng struct btrfs_path *path, int lowest) 23025d4f98a2SYan Zheng { 2303a26195a5SQu Wenruo struct btrfs_backref_node *upper; 2304a26195a5SQu Wenruo struct btrfs_backref_edge *edge; 2305a26195a5SQu Wenruo struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1]; 23065d4f98a2SYan Zheng struct btrfs_root *root; 23075d4f98a2SYan Zheng struct extent_buffer *eb; 23085d4f98a2SYan Zheng u32 blocksize; 23095d4f98a2SYan Zheng u64 bytenr; 23105d4f98a2SYan Zheng int slot; 23118df01fddSNikolay Borisov int ret = 0; 23125d4f98a2SYan Zheng 2313ffe30dd8SJosef Bacik /* 2314ffe30dd8SJosef Bacik * If we are lowest then this is the first time we're processing this 2315ffe30dd8SJosef Bacik * block, and thus shouldn't have an eb associated with it yet. 2316ffe30dd8SJosef Bacik */ 2317ffe30dd8SJosef Bacik ASSERT(!lowest || !node->eb); 23185d4f98a2SYan Zheng 23195d4f98a2SYan Zheng path->lowest_level = node->level + 1; 23203fd0a558SYan, Zheng rc->backref_cache.path[node->level] = node; 23215d4f98a2SYan Zheng list_for_each_entry(edge, &node->upper, list[LOWER]) { 232282fa113fSQu Wenruo struct btrfs_ref ref = { 0 }; 2323581c1760SQu Wenruo 23245d4f98a2SYan Zheng cond_resched(); 23255d4f98a2SYan Zheng 23265d4f98a2SYan Zheng upper = edge->node[UPPER]; 2327dc4103f9SWang Shilong root = select_reloc_root(trans, rc, upper, edges); 2328cbdc2ebcSJosef Bacik if (IS_ERR(root)) { 2329cbdc2ebcSJosef Bacik ret = PTR_ERR(root); 2330cbdc2ebcSJosef Bacik goto next; 2331cbdc2ebcSJosef Bacik } 23325d4f98a2SYan Zheng 23333fd0a558SYan, Zheng if (upper->eb && !upper->locked) { 23343fd0a558SYan, Zheng if (!lowest) { 2335e3b83361SQu Wenruo ret = btrfs_bin_search(upper->eb, key, &slot); 23368df01fddSNikolay Borisov if (ret < 0) 2337cbca7d59SFilipe Manana goto next; 23383fd0a558SYan, Zheng BUG_ON(ret); 23393fd0a558SYan, Zheng bytenr = btrfs_node_blockptr(upper->eb, slot); 23403fd0a558SYan, Zheng if (node->eb->start == bytenr) 23413fd0a558SYan, Zheng goto next; 23423fd0a558SYan, Zheng } 2343b0fe7078SQu Wenruo btrfs_backref_drop_node_buffer(upper); 23443fd0a558SYan, Zheng } 23455d4f98a2SYan Zheng 23465d4f98a2SYan Zheng if (!upper->eb) { 23475d4f98a2SYan Zheng ret = btrfs_search_slot(trans, root, key, path, 0, 1); 23483561b9dbSLiu Bo if (ret) { 23498df01fddSNikolay Borisov if (ret > 0) 23508df01fddSNikolay Borisov ret = -ENOENT; 23513561b9dbSLiu Bo 23523561b9dbSLiu Bo btrfs_release_path(path); 23535d4f98a2SYan Zheng break; 23545d4f98a2SYan Zheng } 23555d4f98a2SYan Zheng 23563fd0a558SYan, Zheng if (!upper->eb) { 23573fd0a558SYan, Zheng upper->eb = path->nodes[upper->level]; 23583fd0a558SYan, Zheng path->nodes[upper->level] = NULL; 23593fd0a558SYan, Zheng } else { 23603fd0a558SYan, Zheng BUG_ON(upper->eb != path->nodes[upper->level]); 23613fd0a558SYan, Zheng } 23623fd0a558SYan, Zheng 23633fd0a558SYan, Zheng upper->locked = 1; 23643fd0a558SYan, Zheng path->locks[upper->level] = 0; 23653fd0a558SYan, Zheng 23665d4f98a2SYan Zheng slot = path->slots[upper->level]; 2367b3b4aa74SDavid Sterba btrfs_release_path(path); 23685d4f98a2SYan Zheng } else { 2369e3b83361SQu Wenruo ret = btrfs_bin_search(upper->eb, key, &slot); 23708df01fddSNikolay Borisov if (ret < 0) 2371cbca7d59SFilipe Manana goto next; 23725d4f98a2SYan Zheng BUG_ON(ret); 23735d4f98a2SYan Zheng } 23745d4f98a2SYan Zheng 23755d4f98a2SYan Zheng bytenr = btrfs_node_blockptr(upper->eb, slot); 23763fd0a558SYan, Zheng if (lowest) { 23774547f4d8SLiu Bo if (bytenr != node->bytenr) { 23784547f4d8SLiu Bo btrfs_err(root->fs_info, 23794547f4d8SLiu Bo "lowest leaf/node mismatch: bytenr %llu node->bytenr %llu slot %d upper %llu", 23804547f4d8SLiu Bo bytenr, node->bytenr, slot, 23814547f4d8SLiu Bo upper->eb->start); 23828df01fddSNikolay Borisov ret = -EIO; 23834547f4d8SLiu Bo goto next; 23844547f4d8SLiu Bo } 23855d4f98a2SYan Zheng } else { 23863fd0a558SYan, Zheng if (node->eb->start == bytenr) 23873fd0a558SYan, Zheng goto next; 23885d4f98a2SYan Zheng } 23895d4f98a2SYan Zheng 2390da17066cSJeff Mahoney blocksize = root->fs_info->nodesize; 2391c9752536SJosef Bacik eb = btrfs_read_node_slot(upper->eb, slot); 239264c043deSLiu Bo if (IS_ERR(eb)) { 23938df01fddSNikolay Borisov ret = PTR_ERR(eb); 239464c043deSLiu Bo goto next; 239597d9a8a4STsutomu Itoh } 23965d4f98a2SYan Zheng btrfs_tree_lock(eb); 23975d4f98a2SYan Zheng 23985d4f98a2SYan Zheng if (!node->eb) { 23995d4f98a2SYan Zheng ret = btrfs_cow_block(trans, root, eb, upper->eb, 24009631e4ccSJosef Bacik slot, &eb, BTRFS_NESTING_COW); 24013fd0a558SYan, Zheng btrfs_tree_unlock(eb); 24023fd0a558SYan, Zheng free_extent_buffer(eb); 24038df01fddSNikolay Borisov if (ret < 0) 24043fd0a558SYan, Zheng goto next; 2405ffe30dd8SJosef Bacik /* 2406ffe30dd8SJosef Bacik * We've just COWed this block, it should have updated 2407ffe30dd8SJosef Bacik * the correct backref node entry. 2408ffe30dd8SJosef Bacik */ 2409ffe30dd8SJosef Bacik ASSERT(node->eb == eb); 24105d4f98a2SYan Zheng } else { 24115d4f98a2SYan Zheng btrfs_set_node_blockptr(upper->eb, slot, 24125d4f98a2SYan Zheng node->eb->start); 24135d4f98a2SYan Zheng btrfs_set_node_ptr_generation(upper->eb, slot, 24145d4f98a2SYan Zheng trans->transid); 24155d4f98a2SYan Zheng btrfs_mark_buffer_dirty(upper->eb); 24165d4f98a2SYan Zheng 241782fa113fSQu Wenruo btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, 24185d4f98a2SYan Zheng node->eb->start, blocksize, 241982fa113fSQu Wenruo upper->eb->start); 242082fa113fSQu Wenruo ref.real_root = root->root_key.objectid; 242182fa113fSQu Wenruo btrfs_init_tree_ref(&ref, node->level, 242282fa113fSQu Wenruo btrfs_header_owner(upper->eb)); 242382fa113fSQu Wenruo ret = btrfs_inc_extent_ref(trans, &ref); 24245d4f98a2SYan Zheng BUG_ON(ret); 24255d4f98a2SYan Zheng 24265d4f98a2SYan Zheng ret = btrfs_drop_subtree(trans, root, eb, upper->eb); 24275d4f98a2SYan Zheng BUG_ON(ret); 24285d4f98a2SYan Zheng } 24293fd0a558SYan, Zheng next: 24303fd0a558SYan, Zheng if (!upper->pending) 2431b0fe7078SQu Wenruo btrfs_backref_drop_node_buffer(upper); 24323fd0a558SYan, Zheng else 2433b0fe7078SQu Wenruo btrfs_backref_unlock_node_buffer(upper); 24348df01fddSNikolay Borisov if (ret) 24353fd0a558SYan, Zheng break; 24365d4f98a2SYan Zheng } 24373fd0a558SYan, Zheng 24388df01fddSNikolay Borisov if (!ret && node->pending) { 2439b0fe7078SQu Wenruo btrfs_backref_drop_node_buffer(node); 24403fd0a558SYan, Zheng list_move_tail(&node->list, &rc->backref_cache.changed); 24413fd0a558SYan, Zheng node->pending = 0; 24425d4f98a2SYan Zheng } 24433fd0a558SYan, Zheng 24445d4f98a2SYan Zheng path->lowest_level = 0; 2445ffe30dd8SJosef Bacik 2446ffe30dd8SJosef Bacik /* 2447ffe30dd8SJosef Bacik * We should have allocated all of our space in the block rsv and thus 2448ffe30dd8SJosef Bacik * shouldn't ENOSPC. 2449ffe30dd8SJosef Bacik */ 2450ffe30dd8SJosef Bacik ASSERT(ret != -ENOSPC); 24518df01fddSNikolay Borisov return ret; 24525d4f98a2SYan Zheng } 24535d4f98a2SYan Zheng 24545d4f98a2SYan Zheng static int link_to_upper(struct btrfs_trans_handle *trans, 24553fd0a558SYan, Zheng struct reloc_control *rc, 2456a26195a5SQu Wenruo struct btrfs_backref_node *node, 24575d4f98a2SYan Zheng struct btrfs_path *path) 24585d4f98a2SYan Zheng { 24595d4f98a2SYan Zheng struct btrfs_key key; 24605d4f98a2SYan Zheng 24615d4f98a2SYan Zheng btrfs_node_key_to_cpu(node->eb, &key, 0); 24623fd0a558SYan, Zheng return do_relocation(trans, rc, node, &key, path, 0); 24635d4f98a2SYan Zheng } 24645d4f98a2SYan Zheng 24655d4f98a2SYan Zheng static int finish_pending_nodes(struct btrfs_trans_handle *trans, 24663fd0a558SYan, Zheng struct reloc_control *rc, 24673fd0a558SYan, Zheng struct btrfs_path *path, int err) 24685d4f98a2SYan Zheng { 24693fd0a558SYan, Zheng LIST_HEAD(list); 2470a26195a5SQu Wenruo struct btrfs_backref_cache *cache = &rc->backref_cache; 2471a26195a5SQu Wenruo struct btrfs_backref_node *node; 24725d4f98a2SYan Zheng int level; 24735d4f98a2SYan Zheng int ret; 24745d4f98a2SYan Zheng 24755d4f98a2SYan Zheng for (level = 0; level < BTRFS_MAX_LEVEL; level++) { 24765d4f98a2SYan Zheng while (!list_empty(&cache->pending[level])) { 24775d4f98a2SYan Zheng node = list_entry(cache->pending[level].next, 2478a26195a5SQu Wenruo struct btrfs_backref_node, list); 24793fd0a558SYan, Zheng list_move_tail(&node->list, &list); 24803fd0a558SYan, Zheng BUG_ON(!node->pending); 24815d4f98a2SYan Zheng 24823fd0a558SYan, Zheng if (!err) { 24833fd0a558SYan, Zheng ret = link_to_upper(trans, rc, node, path); 24845d4f98a2SYan Zheng if (ret < 0) 24855d4f98a2SYan Zheng err = ret; 24865d4f98a2SYan Zheng } 24875d4f98a2SYan Zheng } 24883fd0a558SYan, Zheng list_splice_init(&list, &cache->pending[level]); 24893fd0a558SYan, Zheng } 24905d4f98a2SYan Zheng return err; 24915d4f98a2SYan Zheng } 24925d4f98a2SYan Zheng 24935d4f98a2SYan Zheng /* 24945d4f98a2SYan Zheng * mark a block and all blocks directly/indirectly reference the block 24955d4f98a2SYan Zheng * as processed. 24965d4f98a2SYan Zheng */ 24975d4f98a2SYan Zheng static void update_processed_blocks(struct reloc_control *rc, 2498a26195a5SQu Wenruo struct btrfs_backref_node *node) 24995d4f98a2SYan Zheng { 2500a26195a5SQu Wenruo struct btrfs_backref_node *next = node; 2501a26195a5SQu Wenruo struct btrfs_backref_edge *edge; 2502a26195a5SQu Wenruo struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1]; 25035d4f98a2SYan Zheng int index = 0; 25045d4f98a2SYan Zheng 25055d4f98a2SYan Zheng while (next) { 25065d4f98a2SYan Zheng cond_resched(); 25075d4f98a2SYan Zheng while (1) { 25085d4f98a2SYan Zheng if (next->processed) 25095d4f98a2SYan Zheng break; 25105d4f98a2SYan Zheng 25119569cc20SQu Wenruo mark_block_processed(rc, next); 25125d4f98a2SYan Zheng 25135d4f98a2SYan Zheng if (list_empty(&next->upper)) 25145d4f98a2SYan Zheng break; 25155d4f98a2SYan Zheng 25165d4f98a2SYan Zheng edge = list_entry(next->upper.next, 2517a26195a5SQu Wenruo struct btrfs_backref_edge, list[LOWER]); 25185d4f98a2SYan Zheng edges[index++] = edge; 25195d4f98a2SYan Zheng next = edge->node[UPPER]; 25205d4f98a2SYan Zheng } 25215d4f98a2SYan Zheng next = walk_down_backref(edges, &index); 25225d4f98a2SYan Zheng } 25235d4f98a2SYan Zheng } 25245d4f98a2SYan Zheng 25257476dfdaSDavid Sterba static int tree_block_processed(u64 bytenr, struct reloc_control *rc) 25265d4f98a2SYan Zheng { 2527da17066cSJeff Mahoney u32 blocksize = rc->extent_root->fs_info->nodesize; 25287476dfdaSDavid Sterba 25295d4f98a2SYan Zheng if (test_range_bit(&rc->processed_blocks, bytenr, 25309655d298SChris Mason bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL)) 25315d4f98a2SYan Zheng return 1; 25325d4f98a2SYan Zheng return 0; 25335d4f98a2SYan Zheng } 25345d4f98a2SYan Zheng 25352ff7e61eSJeff Mahoney static int get_tree_block_key(struct btrfs_fs_info *fs_info, 25365d4f98a2SYan Zheng struct tree_block *block) 25375d4f98a2SYan Zheng { 25385d4f98a2SYan Zheng struct extent_buffer *eb; 25395d4f98a2SYan Zheng 2540f7ba2d37SJosef Bacik eb = read_tree_block(fs_info, block->bytenr, block->owner, 2541f7ba2d37SJosef Bacik block->key.offset, block->level, NULL); 254264c043deSLiu Bo if (IS_ERR(eb)) { 254364c043deSLiu Bo return PTR_ERR(eb); 254464c043deSLiu Bo } else if (!extent_buffer_uptodate(eb)) { 2545416bc658SJosef Bacik free_extent_buffer(eb); 2546416bc658SJosef Bacik return -EIO; 2547416bc658SJosef Bacik } 25485d4f98a2SYan Zheng if (block->level == 0) 25495d4f98a2SYan Zheng btrfs_item_key_to_cpu(eb, &block->key, 0); 25505d4f98a2SYan Zheng else 25515d4f98a2SYan Zheng btrfs_node_key_to_cpu(eb, &block->key, 0); 25525d4f98a2SYan Zheng free_extent_buffer(eb); 25535d4f98a2SYan Zheng block->key_ready = 1; 25545d4f98a2SYan Zheng return 0; 25555d4f98a2SYan Zheng } 25565d4f98a2SYan Zheng 25575d4f98a2SYan Zheng /* 25585d4f98a2SYan Zheng * helper function to relocate a tree block 25595d4f98a2SYan Zheng */ 25605d4f98a2SYan Zheng static int relocate_tree_block(struct btrfs_trans_handle *trans, 25615d4f98a2SYan Zheng struct reloc_control *rc, 2562a26195a5SQu Wenruo struct btrfs_backref_node *node, 25635d4f98a2SYan Zheng struct btrfs_key *key, 25645d4f98a2SYan Zheng struct btrfs_path *path) 25655d4f98a2SYan Zheng { 25665d4f98a2SYan Zheng struct btrfs_root *root; 25673fd0a558SYan, Zheng int ret = 0; 25685d4f98a2SYan Zheng 25693fd0a558SYan, Zheng if (!node) 25705d4f98a2SYan Zheng return 0; 25713fd0a558SYan, Zheng 25725f6b2e5cSJosef Bacik /* 25735f6b2e5cSJosef Bacik * If we fail here we want to drop our backref_node because we are going 25745f6b2e5cSJosef Bacik * to start over and regenerate the tree for it. 25755f6b2e5cSJosef Bacik */ 25765f6b2e5cSJosef Bacik ret = reserve_metadata_space(trans, rc, node); 25775f6b2e5cSJosef Bacik if (ret) 25785f6b2e5cSJosef Bacik goto out; 25795f6b2e5cSJosef Bacik 25803fd0a558SYan, Zheng BUG_ON(node->processed); 2581147d256eSZhaolei root = select_one_root(node); 25823fd0a558SYan, Zheng if (root == ERR_PTR(-ENOENT)) { 25833fd0a558SYan, Zheng update_processed_blocks(rc, node); 25843fd0a558SYan, Zheng goto out; 25855d4f98a2SYan Zheng } 25865d4f98a2SYan Zheng 25873fd0a558SYan, Zheng if (root) { 258892a7cc42SQu Wenruo if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) { 25891c7bfa15SJosef Bacik /* 25901c7bfa15SJosef Bacik * This block was the root block of a root, and this is 25911c7bfa15SJosef Bacik * the first time we're processing the block and thus it 25921c7bfa15SJosef Bacik * should not have had the ->new_bytenr modified and 25931c7bfa15SJosef Bacik * should have not been included on the changed list. 25941c7bfa15SJosef Bacik * 25951c7bfa15SJosef Bacik * However in the case of corruption we could have 25961c7bfa15SJosef Bacik * multiple refs pointing to the same block improperly, 25971c7bfa15SJosef Bacik * and thus we would trip over these checks. ASSERT() 25981c7bfa15SJosef Bacik * for the developer case, because it could indicate a 25991c7bfa15SJosef Bacik * bug in the backref code, however error out for a 26001c7bfa15SJosef Bacik * normal user in the case of corruption. 26011c7bfa15SJosef Bacik */ 26021c7bfa15SJosef Bacik ASSERT(node->new_bytenr == 0); 26031c7bfa15SJosef Bacik ASSERT(list_empty(&node->list)); 26041c7bfa15SJosef Bacik if (node->new_bytenr || !list_empty(&node->list)) { 26051c7bfa15SJosef Bacik btrfs_err(root->fs_info, 26061c7bfa15SJosef Bacik "bytenr %llu has improper references to it", 26071c7bfa15SJosef Bacik node->bytenr); 26081c7bfa15SJosef Bacik ret = -EUCLEAN; 26091c7bfa15SJosef Bacik goto out; 26101c7bfa15SJosef Bacik } 2611d18c7bd9SJosef Bacik ret = btrfs_record_root_in_trans(trans, root); 2612d18c7bd9SJosef Bacik if (ret) 2613d18c7bd9SJosef Bacik goto out; 261439200e59SJosef Bacik /* 261539200e59SJosef Bacik * Another thread could have failed, need to check if we 261639200e59SJosef Bacik * have reloc_root actually set. 261739200e59SJosef Bacik */ 261839200e59SJosef Bacik if (!root->reloc_root) { 261939200e59SJosef Bacik ret = -ENOENT; 262039200e59SJosef Bacik goto out; 262139200e59SJosef Bacik } 26223fd0a558SYan, Zheng root = root->reloc_root; 26233fd0a558SYan, Zheng node->new_bytenr = root->node->start; 262400246528SJosef Bacik btrfs_put_root(node->root); 262500246528SJosef Bacik node->root = btrfs_grab_root(root); 26260b530bc5SJosef Bacik ASSERT(node->root); 26273fd0a558SYan, Zheng list_add_tail(&node->list, &rc->backref_cache.changed); 26283fd0a558SYan, Zheng } else { 26295d4f98a2SYan Zheng path->lowest_level = node->level; 26305d4f98a2SYan Zheng ret = btrfs_search_slot(trans, root, key, path, 0, 1); 2631b3b4aa74SDavid Sterba btrfs_release_path(path); 26323fd0a558SYan, Zheng if (ret > 0) 26335d4f98a2SYan Zheng ret = 0; 26343fd0a558SYan, Zheng } 26353fd0a558SYan, Zheng if (!ret) 26363fd0a558SYan, Zheng update_processed_blocks(rc, node); 26373fd0a558SYan, Zheng } else { 26383fd0a558SYan, Zheng ret = do_relocation(trans, rc, node, key, path, 1); 26393fd0a558SYan, Zheng } 26405d4f98a2SYan Zheng out: 26410647bf56SWang Shilong if (ret || node->level == 0 || node->cowonly) 2642023acb07SQu Wenruo btrfs_backref_cleanup_node(&rc->backref_cache, node); 26435d4f98a2SYan Zheng return ret; 26445d4f98a2SYan Zheng } 26455d4f98a2SYan Zheng 26465d4f98a2SYan Zheng /* 26475d4f98a2SYan Zheng * relocate a list of blocks 26485d4f98a2SYan Zheng */ 26495d4f98a2SYan Zheng static noinline_for_stack 26505d4f98a2SYan Zheng int relocate_tree_blocks(struct btrfs_trans_handle *trans, 26515d4f98a2SYan Zheng struct reloc_control *rc, struct rb_root *blocks) 26525d4f98a2SYan Zheng { 26532ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; 2654a26195a5SQu Wenruo struct btrfs_backref_node *node; 26555d4f98a2SYan Zheng struct btrfs_path *path; 26565d4f98a2SYan Zheng struct tree_block *block; 265798ff7b94SQu Wenruo struct tree_block *next; 26585d4f98a2SYan Zheng int ret; 26595d4f98a2SYan Zheng int err = 0; 26605d4f98a2SYan Zheng 26615d4f98a2SYan Zheng path = btrfs_alloc_path(); 2662e1a12670SLiu Bo if (!path) { 2663e1a12670SLiu Bo err = -ENOMEM; 266434c2b290SDavid Sterba goto out_free_blocks; 2665e1a12670SLiu Bo } 26665d4f98a2SYan Zheng 266798ff7b94SQu Wenruo /* Kick in readahead for tree blocks with missing keys */ 266898ff7b94SQu Wenruo rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) { 26695d4f98a2SYan Zheng if (!block->key_ready) 2670f7ba2d37SJosef Bacik btrfs_readahead_tree_block(fs_info, block->bytenr, 2671f7ba2d37SJosef Bacik block->owner, 0, 26723fbaf258SJosef Bacik block->level); 26735d4f98a2SYan Zheng } 26745d4f98a2SYan Zheng 267598ff7b94SQu Wenruo /* Get first keys */ 267698ff7b94SQu Wenruo rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) { 267734c2b290SDavid Sterba if (!block->key_ready) { 26782ff7e61eSJeff Mahoney err = get_tree_block_key(fs_info, block); 267934c2b290SDavid Sterba if (err) 268034c2b290SDavid Sterba goto out_free_path; 268134c2b290SDavid Sterba } 26825d4f98a2SYan Zheng } 26835d4f98a2SYan Zheng 268498ff7b94SQu Wenruo /* Do tree relocation */ 268598ff7b94SQu Wenruo rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) { 26863fd0a558SYan, Zheng node = build_backref_tree(rc, &block->key, 26875d4f98a2SYan Zheng block->level, block->bytenr); 26885d4f98a2SYan Zheng if (IS_ERR(node)) { 26895d4f98a2SYan Zheng err = PTR_ERR(node); 26905d4f98a2SYan Zheng goto out; 26915d4f98a2SYan Zheng } 26925d4f98a2SYan Zheng 26935d4f98a2SYan Zheng ret = relocate_tree_block(trans, rc, node, &block->key, 26945d4f98a2SYan Zheng path); 26955d4f98a2SYan Zheng if (ret < 0) { 26965d4f98a2SYan Zheng err = ret; 269750dbbb71SJosef Bacik break; 26985d4f98a2SYan Zheng } 26995d4f98a2SYan Zheng } 27005d4f98a2SYan Zheng out: 27013fd0a558SYan, Zheng err = finish_pending_nodes(trans, rc, path, err); 27025d4f98a2SYan Zheng 270334c2b290SDavid Sterba out_free_path: 27045d4f98a2SYan Zheng btrfs_free_path(path); 270534c2b290SDavid Sterba out_free_blocks: 2706e1a12670SLiu Bo free_block_list(blocks); 27075d4f98a2SYan Zheng return err; 27085d4f98a2SYan Zheng } 27095d4f98a2SYan Zheng 2710056d9becSNikolay Borisov static noinline_for_stack int prealloc_file_extent_cluster( 2711056d9becSNikolay Borisov struct btrfs_inode *inode, 2712efa56464SYan, Zheng struct file_extent_cluster *cluster) 2713efa56464SYan, Zheng { 2714efa56464SYan, Zheng u64 alloc_hint = 0; 2715efa56464SYan, Zheng u64 start; 2716efa56464SYan, Zheng u64 end; 2717056d9becSNikolay Borisov u64 offset = inode->index_cnt; 2718efa56464SYan, Zheng u64 num_bytes; 27194e9d0d01SNikolay Borisov int nr; 2720efa56464SYan, Zheng int ret = 0; 2721dcb40c19SWang Xiaoguang u64 prealloc_start = cluster->start - offset; 2722dcb40c19SWang Xiaoguang u64 prealloc_end = cluster->end - offset; 2723214e61d0SNikolay Borisov u64 cur_offset = prealloc_start; 2724efa56464SYan, Zheng 2725efa56464SYan, Zheng BUG_ON(cluster->start != cluster->boundary[0]); 2726056d9becSNikolay Borisov ret = btrfs_alloc_data_chunk_ondemand(inode, 2727dcb40c19SWang Xiaoguang prealloc_end + 1 - prealloc_start); 2728efa56464SYan, Zheng if (ret) 2729214e61d0SNikolay Borisov return ret; 2730efa56464SYan, Zheng 273132430c61SNaohiro Aota /* 273232430c61SNaohiro Aota * On a zoned filesystem, we cannot preallocate the file region. 273332430c61SNaohiro Aota * Instead, we dirty and fiemap_write the region. 273432430c61SNaohiro Aota */ 273532430c61SNaohiro Aota if (btrfs_is_zoned(inode->root->fs_info)) { 273632430c61SNaohiro Aota struct btrfs_root *root = inode->root; 273732430c61SNaohiro Aota struct btrfs_trans_handle *trans; 273832430c61SNaohiro Aota 273932430c61SNaohiro Aota end = cluster->end - offset + 1; 274032430c61SNaohiro Aota trans = btrfs_start_transaction(root, 1); 274132430c61SNaohiro Aota if (IS_ERR(trans)) 274232430c61SNaohiro Aota return PTR_ERR(trans); 274332430c61SNaohiro Aota 274432430c61SNaohiro Aota inode->vfs_inode.i_ctime = current_time(&inode->vfs_inode); 274532430c61SNaohiro Aota i_size_write(&inode->vfs_inode, end); 274632430c61SNaohiro Aota ret = btrfs_update_inode(trans, root, inode); 274732430c61SNaohiro Aota if (ret) { 274832430c61SNaohiro Aota btrfs_abort_transaction(trans, ret); 274932430c61SNaohiro Aota btrfs_end_transaction(trans); 275032430c61SNaohiro Aota return ret; 275132430c61SNaohiro Aota } 275232430c61SNaohiro Aota 275332430c61SNaohiro Aota return btrfs_end_transaction(trans); 275432430c61SNaohiro Aota } 275532430c61SNaohiro Aota 275664708539SJosef Bacik btrfs_inode_lock(&inode->vfs_inode, 0); 27574e9d0d01SNikolay Borisov for (nr = 0; nr < cluster->nr; nr++) { 2758efa56464SYan, Zheng start = cluster->boundary[nr] - offset; 2759efa56464SYan, Zheng if (nr + 1 < cluster->nr) 2760efa56464SYan, Zheng end = cluster->boundary[nr + 1] - 1 - offset; 2761efa56464SYan, Zheng else 2762efa56464SYan, Zheng end = cluster->end - offset; 2763efa56464SYan, Zheng 2764056d9becSNikolay Borisov lock_extent(&inode->io_tree, start, end); 2765efa56464SYan, Zheng num_bytes = end + 1 - start; 2766056d9becSNikolay Borisov ret = btrfs_prealloc_file_range(&inode->vfs_inode, 0, start, 2767efa56464SYan, Zheng num_bytes, num_bytes, 2768efa56464SYan, Zheng end + 1, &alloc_hint); 276918513091SWang Xiaoguang cur_offset = end + 1; 2770056d9becSNikolay Borisov unlock_extent(&inode->io_tree, start, end); 2771efa56464SYan, Zheng if (ret) 2772efa56464SYan, Zheng break; 2773efa56464SYan, Zheng } 277464708539SJosef Bacik btrfs_inode_unlock(&inode->vfs_inode, 0); 2775214e61d0SNikolay Borisov 277618513091SWang Xiaoguang if (cur_offset < prealloc_end) 2777056d9becSNikolay Borisov btrfs_free_reserved_data_space_noquota(inode->root->fs_info, 2778a89ef455SFilipe Manana prealloc_end + 1 - cur_offset); 2779efa56464SYan, Zheng return ret; 2780efa56464SYan, Zheng } 2781efa56464SYan, Zheng 2782efa56464SYan, Zheng static noinline_for_stack 27830257bb82SYan, Zheng int setup_extent_mapping(struct inode *inode, u64 start, u64 end, 27840257bb82SYan, Zheng u64 block_start) 27855d4f98a2SYan Zheng { 27865d4f98a2SYan Zheng struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 27875d4f98a2SYan Zheng struct extent_map *em; 27880257bb82SYan, Zheng int ret = 0; 27895d4f98a2SYan Zheng 2790172ddd60SDavid Sterba em = alloc_extent_map(); 27910257bb82SYan, Zheng if (!em) 27920257bb82SYan, Zheng return -ENOMEM; 27930257bb82SYan, Zheng 27945d4f98a2SYan Zheng em->start = start; 27950257bb82SYan, Zheng em->len = end + 1 - start; 27960257bb82SYan, Zheng em->block_len = em->len; 27970257bb82SYan, Zheng em->block_start = block_start; 27985d4f98a2SYan Zheng set_bit(EXTENT_FLAG_PINNED, &em->flags); 27995d4f98a2SYan Zheng 2800d0082371SJeff Mahoney lock_extent(&BTRFS_I(inode)->io_tree, start, end); 28015d4f98a2SYan Zheng while (1) { 2802890871beSChris Mason write_lock(&em_tree->lock); 280309a2a8f9SJosef Bacik ret = add_extent_mapping(em_tree, em, 0); 2804890871beSChris Mason write_unlock(&em_tree->lock); 28055d4f98a2SYan Zheng if (ret != -EEXIST) { 28065d4f98a2SYan Zheng free_extent_map(em); 28075d4f98a2SYan Zheng break; 28085d4f98a2SYan Zheng } 2809dcdbc059SNikolay Borisov btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0); 28105d4f98a2SYan Zheng } 2811d0082371SJeff Mahoney unlock_extent(&BTRFS_I(inode)->io_tree, start, end); 28120257bb82SYan, Zheng return ret; 28130257bb82SYan, Zheng } 28145d4f98a2SYan Zheng 2815726a3421SQu Wenruo /* 2816726a3421SQu Wenruo * Allow error injection to test balance cancellation 2817726a3421SQu Wenruo */ 28181fec12a5SJosef Bacik noinline int btrfs_should_cancel_balance(struct btrfs_fs_info *fs_info) 2819726a3421SQu Wenruo { 28205cb502f4SQu Wenruo return atomic_read(&fs_info->balance_cancel_req) || 28215cb502f4SQu Wenruo fatal_signal_pending(current); 2822726a3421SQu Wenruo } 2823726a3421SQu Wenruo ALLOW_ERROR_INJECTION(btrfs_should_cancel_balance, TRUE); 2824726a3421SQu Wenruo 28250257bb82SYan, Zheng static int relocate_file_extent_cluster(struct inode *inode, 28260257bb82SYan, Zheng struct file_extent_cluster *cluster) 28270257bb82SYan, Zheng { 28282ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 28290257bb82SYan, Zheng u64 page_start; 28300257bb82SYan, Zheng u64 page_end; 28310257bb82SYan, Zheng u64 offset = BTRFS_I(inode)->index_cnt; 28320257bb82SYan, Zheng unsigned long index; 28330257bb82SYan, Zheng unsigned long last_index; 28340257bb82SYan, Zheng struct page *page; 28350257bb82SYan, Zheng struct file_ra_state *ra; 28363b16a4e3SJosef Bacik gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); 28370257bb82SYan, Zheng int nr = 0; 28380257bb82SYan, Zheng int ret = 0; 28390257bb82SYan, Zheng 28400257bb82SYan, Zheng if (!cluster->nr) 28410257bb82SYan, Zheng return 0; 28420257bb82SYan, Zheng 28430257bb82SYan, Zheng ra = kzalloc(sizeof(*ra), GFP_NOFS); 28440257bb82SYan, Zheng if (!ra) 28450257bb82SYan, Zheng return -ENOMEM; 28460257bb82SYan, Zheng 2847056d9becSNikolay Borisov ret = prealloc_file_extent_cluster(BTRFS_I(inode), cluster); 28480257bb82SYan, Zheng if (ret) 2849efa56464SYan, Zheng goto out; 28500257bb82SYan, Zheng 28510257bb82SYan, Zheng file_ra_state_init(ra, inode->i_mapping); 28520257bb82SYan, Zheng 2853efa56464SYan, Zheng ret = setup_extent_mapping(inode, cluster->start - offset, 2854efa56464SYan, Zheng cluster->end - offset, cluster->start); 2855efa56464SYan, Zheng if (ret) 2856efa56464SYan, Zheng goto out; 2857efa56464SYan, Zheng 285809cbfeafSKirill A. Shutemov index = (cluster->start - offset) >> PAGE_SHIFT; 285909cbfeafSKirill A. Shutemov last_index = (cluster->end - offset) >> PAGE_SHIFT; 28600257bb82SYan, Zheng while (index <= last_index) { 28619f3db423SNikolay Borisov ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), 28629f3db423SNikolay Borisov PAGE_SIZE); 2863efa56464SYan, Zheng if (ret) 2864efa56464SYan, Zheng goto out; 2865efa56464SYan, Zheng 28660257bb82SYan, Zheng page = find_lock_page(inode->i_mapping, index); 28670257bb82SYan, Zheng if (!page) { 28680257bb82SYan, Zheng page_cache_sync_readahead(inode->i_mapping, 28690257bb82SYan, Zheng ra, NULL, index, 28700257bb82SYan, Zheng last_index + 1 - index); 2871a94733d0SJosef Bacik page = find_or_create_page(inode->i_mapping, index, 28723b16a4e3SJosef Bacik mask); 28730257bb82SYan, Zheng if (!page) { 2874691fa059SNikolay Borisov btrfs_delalloc_release_metadata(BTRFS_I(inode), 287543b18595SQu Wenruo PAGE_SIZE, true); 287644db1216SFilipe Manana btrfs_delalloc_release_extents(BTRFS_I(inode), 28778702ba93SQu Wenruo PAGE_SIZE); 28780257bb82SYan, Zheng ret = -ENOMEM; 2879efa56464SYan, Zheng goto out; 28800257bb82SYan, Zheng } 28810257bb82SYan, Zheng } 288232443de3SQu Wenruo ret = set_page_extent_mapped(page); 288332443de3SQu Wenruo if (ret < 0) { 288432443de3SQu Wenruo btrfs_delalloc_release_metadata(BTRFS_I(inode), 288532443de3SQu Wenruo PAGE_SIZE, true); 288632443de3SQu Wenruo btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); 288732443de3SQu Wenruo unlock_page(page); 288832443de3SQu Wenruo put_page(page); 288932443de3SQu Wenruo goto out; 289032443de3SQu Wenruo } 28910257bb82SYan, Zheng 28920257bb82SYan, Zheng if (PageReadahead(page)) { 28930257bb82SYan, Zheng page_cache_async_readahead(inode->i_mapping, 28940257bb82SYan, Zheng ra, NULL, page, index, 28950257bb82SYan, Zheng last_index + 1 - index); 28960257bb82SYan, Zheng } 28970257bb82SYan, Zheng 28980257bb82SYan, Zheng if (!PageUptodate(page)) { 28990257bb82SYan, Zheng btrfs_readpage(NULL, page); 29000257bb82SYan, Zheng lock_page(page); 29010257bb82SYan, Zheng if (!PageUptodate(page)) { 29020257bb82SYan, Zheng unlock_page(page); 290309cbfeafSKirill A. Shutemov put_page(page); 2904691fa059SNikolay Borisov btrfs_delalloc_release_metadata(BTRFS_I(inode), 290543b18595SQu Wenruo PAGE_SIZE, true); 29068b62f87bSJosef Bacik btrfs_delalloc_release_extents(BTRFS_I(inode), 29078702ba93SQu Wenruo PAGE_SIZE); 29080257bb82SYan, Zheng ret = -EIO; 2909efa56464SYan, Zheng goto out; 29100257bb82SYan, Zheng } 29110257bb82SYan, Zheng } 29120257bb82SYan, Zheng 29134eee4fa4SMiao Xie page_start = page_offset(page); 291409cbfeafSKirill A. Shutemov page_end = page_start + PAGE_SIZE - 1; 29150257bb82SYan, Zheng 2916d0082371SJeff Mahoney lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end); 29170257bb82SYan, Zheng 29180257bb82SYan, Zheng if (nr < cluster->nr && 29190257bb82SYan, Zheng page_start + offset == cluster->boundary[nr]) { 29200257bb82SYan, Zheng set_extent_bits(&BTRFS_I(inode)->io_tree, 29210257bb82SYan, Zheng page_start, page_end, 2922ceeb0ae7SDavid Sterba EXTENT_BOUNDARY); 29230257bb82SYan, Zheng nr++; 29240257bb82SYan, Zheng } 29250257bb82SYan, Zheng 2926c2566f22SNikolay Borisov ret = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, 2927c2566f22SNikolay Borisov page_end, 0, NULL); 2928765f3cebSNikolay Borisov if (ret) { 2929765f3cebSNikolay Borisov unlock_page(page); 2930765f3cebSNikolay Borisov put_page(page); 2931765f3cebSNikolay Borisov btrfs_delalloc_release_metadata(BTRFS_I(inode), 293243b18595SQu Wenruo PAGE_SIZE, true); 2933765f3cebSNikolay Borisov btrfs_delalloc_release_extents(BTRFS_I(inode), 29348702ba93SQu Wenruo PAGE_SIZE); 2935765f3cebSNikolay Borisov 2936765f3cebSNikolay Borisov clear_extent_bits(&BTRFS_I(inode)->io_tree, 2937765f3cebSNikolay Borisov page_start, page_end, 2938765f3cebSNikolay Borisov EXTENT_LOCKED | EXTENT_BOUNDARY); 2939765f3cebSNikolay Borisov goto out; 2940765f3cebSNikolay Borisov 2941765f3cebSNikolay Borisov } 29420257bb82SYan, Zheng set_page_dirty(page); 29430257bb82SYan, Zheng 29440257bb82SYan, Zheng unlock_extent(&BTRFS_I(inode)->io_tree, 2945d0082371SJeff Mahoney page_start, page_end); 29460257bb82SYan, Zheng unlock_page(page); 294709cbfeafSKirill A. Shutemov put_page(page); 29480257bb82SYan, Zheng 29490257bb82SYan, Zheng index++; 29508702ba93SQu Wenruo btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); 2951efa56464SYan, Zheng balance_dirty_pages_ratelimited(inode->i_mapping); 29522ff7e61eSJeff Mahoney btrfs_throttle(fs_info); 29537f913c7cSQu Wenruo if (btrfs_should_cancel_balance(fs_info)) { 29547f913c7cSQu Wenruo ret = -ECANCELED; 29557f913c7cSQu Wenruo goto out; 29567f913c7cSQu Wenruo } 29570257bb82SYan, Zheng } 29580257bb82SYan, Zheng WARN_ON(nr != cluster->nr); 295932430c61SNaohiro Aota if (btrfs_is_zoned(fs_info) && !ret) 296032430c61SNaohiro Aota ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); 2961efa56464SYan, Zheng out: 29620257bb82SYan, Zheng kfree(ra); 29630257bb82SYan, Zheng return ret; 29640257bb82SYan, Zheng } 29650257bb82SYan, Zheng 29660257bb82SYan, Zheng static noinline_for_stack 29670257bb82SYan, Zheng int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key, 29680257bb82SYan, Zheng struct file_extent_cluster *cluster) 29690257bb82SYan, Zheng { 29700257bb82SYan, Zheng int ret; 29710257bb82SYan, Zheng 29720257bb82SYan, Zheng if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) { 29730257bb82SYan, Zheng ret = relocate_file_extent_cluster(inode, cluster); 29740257bb82SYan, Zheng if (ret) 29750257bb82SYan, Zheng return ret; 29760257bb82SYan, Zheng cluster->nr = 0; 29770257bb82SYan, Zheng } 29780257bb82SYan, Zheng 29790257bb82SYan, Zheng if (!cluster->nr) 29800257bb82SYan, Zheng cluster->start = extent_key->objectid; 29810257bb82SYan, Zheng else 29820257bb82SYan, Zheng BUG_ON(cluster->nr >= MAX_EXTENTS); 29830257bb82SYan, Zheng cluster->end = extent_key->objectid + extent_key->offset - 1; 29840257bb82SYan, Zheng cluster->boundary[cluster->nr] = extent_key->objectid; 29850257bb82SYan, Zheng cluster->nr++; 29860257bb82SYan, Zheng 29870257bb82SYan, Zheng if (cluster->nr >= MAX_EXTENTS) { 29880257bb82SYan, Zheng ret = relocate_file_extent_cluster(inode, cluster); 29890257bb82SYan, Zheng if (ret) 29900257bb82SYan, Zheng return ret; 29910257bb82SYan, Zheng cluster->nr = 0; 29920257bb82SYan, Zheng } 29930257bb82SYan, Zheng return 0; 29945d4f98a2SYan Zheng } 29955d4f98a2SYan Zheng 29965d4f98a2SYan Zheng /* 29975d4f98a2SYan Zheng * helper to add a tree block to the list. 29985d4f98a2SYan Zheng * the major work is getting the generation and level of the block 29995d4f98a2SYan Zheng */ 30005d4f98a2SYan Zheng static int add_tree_block(struct reloc_control *rc, 30015d4f98a2SYan Zheng struct btrfs_key *extent_key, 30025d4f98a2SYan Zheng struct btrfs_path *path, 30035d4f98a2SYan Zheng struct rb_root *blocks) 30045d4f98a2SYan Zheng { 30055d4f98a2SYan Zheng struct extent_buffer *eb; 30065d4f98a2SYan Zheng struct btrfs_extent_item *ei; 30075d4f98a2SYan Zheng struct btrfs_tree_block_info *bi; 30085d4f98a2SYan Zheng struct tree_block *block; 30095d4f98a2SYan Zheng struct rb_node *rb_node; 30105d4f98a2SYan Zheng u32 item_size; 30115d4f98a2SYan Zheng int level = -1; 30127fdf4b60SWang Shilong u64 generation; 3013f7ba2d37SJosef Bacik u64 owner = 0; 30145d4f98a2SYan Zheng 30155d4f98a2SYan Zheng eb = path->nodes[0]; 30165d4f98a2SYan Zheng item_size = btrfs_item_size_nr(eb, path->slots[0]); 30175d4f98a2SYan Zheng 30183173a18fSJosef Bacik if (extent_key->type == BTRFS_METADATA_ITEM_KEY || 30193173a18fSJosef Bacik item_size >= sizeof(*ei) + sizeof(*bi)) { 3020f7ba2d37SJosef Bacik unsigned long ptr = 0, end; 3021f7ba2d37SJosef Bacik 30225d4f98a2SYan Zheng ei = btrfs_item_ptr(eb, path->slots[0], 30235d4f98a2SYan Zheng struct btrfs_extent_item); 3024f7ba2d37SJosef Bacik end = (unsigned long)ei + item_size; 30253173a18fSJosef Bacik if (extent_key->type == BTRFS_EXTENT_ITEM_KEY) { 30265d4f98a2SYan Zheng bi = (struct btrfs_tree_block_info *)(ei + 1); 30275d4f98a2SYan Zheng level = btrfs_tree_block_level(eb, bi); 3028f7ba2d37SJosef Bacik ptr = (unsigned long)(bi + 1); 30295d4f98a2SYan Zheng } else { 30303173a18fSJosef Bacik level = (int)extent_key->offset; 3031f7ba2d37SJosef Bacik ptr = (unsigned long)(ei + 1); 30323173a18fSJosef Bacik } 30333173a18fSJosef Bacik generation = btrfs_extent_generation(eb, ei); 3034f7ba2d37SJosef Bacik 3035f7ba2d37SJosef Bacik /* 3036f7ba2d37SJosef Bacik * We're reading random blocks without knowing their owner ahead 3037f7ba2d37SJosef Bacik * of time. This is ok most of the time, as all reloc roots and 3038f7ba2d37SJosef Bacik * fs roots have the same lock type. However normal trees do 3039f7ba2d37SJosef Bacik * not, and the only way to know ahead of time is to read the 3040f7ba2d37SJosef Bacik * inline ref offset. We know it's an fs root if 3041f7ba2d37SJosef Bacik * 3042f7ba2d37SJosef Bacik * 1. There's more than one ref. 3043f7ba2d37SJosef Bacik * 2. There's a SHARED_DATA_REF_KEY set. 3044f7ba2d37SJosef Bacik * 3. FULL_BACKREF is set on the flags. 3045f7ba2d37SJosef Bacik * 3046f7ba2d37SJosef Bacik * Otherwise it's safe to assume that the ref offset == the 3047f7ba2d37SJosef Bacik * owner of this block, so we can use that when calling 3048f7ba2d37SJosef Bacik * read_tree_block. 3049f7ba2d37SJosef Bacik */ 3050f7ba2d37SJosef Bacik if (btrfs_extent_refs(eb, ei) == 1 && 3051f7ba2d37SJosef Bacik !(btrfs_extent_flags(eb, ei) & 3052f7ba2d37SJosef Bacik BTRFS_BLOCK_FLAG_FULL_BACKREF) && 3053f7ba2d37SJosef Bacik ptr < end) { 3054f7ba2d37SJosef Bacik struct btrfs_extent_inline_ref *iref; 3055f7ba2d37SJosef Bacik int type; 3056f7ba2d37SJosef Bacik 3057f7ba2d37SJosef Bacik iref = (struct btrfs_extent_inline_ref *)ptr; 3058f7ba2d37SJosef Bacik type = btrfs_get_extent_inline_ref_type(eb, iref, 3059f7ba2d37SJosef Bacik BTRFS_REF_TYPE_BLOCK); 3060f7ba2d37SJosef Bacik if (type == BTRFS_REF_TYPE_INVALID) 3061f7ba2d37SJosef Bacik return -EINVAL; 3062f7ba2d37SJosef Bacik if (type == BTRFS_TREE_BLOCK_REF_KEY) 3063f7ba2d37SJosef Bacik owner = btrfs_extent_inline_ref_offset(eb, iref); 3064f7ba2d37SJosef Bacik } 30656d8ff4e4SDavid Sterba } else if (unlikely(item_size == sizeof(struct btrfs_extent_item_v0))) { 3066ba3c2b19SNikolay Borisov btrfs_print_v0_err(eb->fs_info); 3067ba3c2b19SNikolay Borisov btrfs_handle_fs_error(eb->fs_info, -EINVAL, NULL); 3068ba3c2b19SNikolay Borisov return -EINVAL; 30693173a18fSJosef Bacik } else { 30705d4f98a2SYan Zheng BUG(); 30715d4f98a2SYan Zheng } 30725d4f98a2SYan Zheng 3073b3b4aa74SDavid Sterba btrfs_release_path(path); 30745d4f98a2SYan Zheng 30755d4f98a2SYan Zheng BUG_ON(level == -1); 30765d4f98a2SYan Zheng 30775d4f98a2SYan Zheng block = kmalloc(sizeof(*block), GFP_NOFS); 30785d4f98a2SYan Zheng if (!block) 30795d4f98a2SYan Zheng return -ENOMEM; 30805d4f98a2SYan Zheng 30815d4f98a2SYan Zheng block->bytenr = extent_key->objectid; 3082da17066cSJeff Mahoney block->key.objectid = rc->extent_root->fs_info->nodesize; 30835d4f98a2SYan Zheng block->key.offset = generation; 30845d4f98a2SYan Zheng block->level = level; 30855d4f98a2SYan Zheng block->key_ready = 0; 3086f7ba2d37SJosef Bacik block->owner = owner; 30875d4f98a2SYan Zheng 3088e9a28dc5SQu Wenruo rb_node = rb_simple_insert(blocks, block->bytenr, &block->rb_node); 308943c04fb1SJeff Mahoney if (rb_node) 3090982c92cbSQu Wenruo btrfs_backref_panic(rc->extent_root->fs_info, block->bytenr, 3091982c92cbSQu Wenruo -EEXIST); 30925d4f98a2SYan Zheng 30935d4f98a2SYan Zheng return 0; 30945d4f98a2SYan Zheng } 30955d4f98a2SYan Zheng 30965d4f98a2SYan Zheng /* 30975d4f98a2SYan Zheng * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY 30985d4f98a2SYan Zheng */ 30995d4f98a2SYan Zheng static int __add_tree_block(struct reloc_control *rc, 31005d4f98a2SYan Zheng u64 bytenr, u32 blocksize, 31015d4f98a2SYan Zheng struct rb_root *blocks) 31025d4f98a2SYan Zheng { 31030b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; 31045d4f98a2SYan Zheng struct btrfs_path *path; 31055d4f98a2SYan Zheng struct btrfs_key key; 31065d4f98a2SYan Zheng int ret; 31070b246afaSJeff Mahoney bool skinny = btrfs_fs_incompat(fs_info, SKINNY_METADATA); 31085d4f98a2SYan Zheng 31097476dfdaSDavid Sterba if (tree_block_processed(bytenr, rc)) 31105d4f98a2SYan Zheng return 0; 31115d4f98a2SYan Zheng 3112e9a28dc5SQu Wenruo if (rb_simple_search(blocks, bytenr)) 31135d4f98a2SYan Zheng return 0; 31145d4f98a2SYan Zheng 31155d4f98a2SYan Zheng path = btrfs_alloc_path(); 31165d4f98a2SYan Zheng if (!path) 31175d4f98a2SYan Zheng return -ENOMEM; 3118aee68ee5SJosef Bacik again: 31195d4f98a2SYan Zheng key.objectid = bytenr; 3120aee68ee5SJosef Bacik if (skinny) { 3121aee68ee5SJosef Bacik key.type = BTRFS_METADATA_ITEM_KEY; 3122aee68ee5SJosef Bacik key.offset = (u64)-1; 3123aee68ee5SJosef Bacik } else { 31245d4f98a2SYan Zheng key.type = BTRFS_EXTENT_ITEM_KEY; 31255d4f98a2SYan Zheng key.offset = blocksize; 3126aee68ee5SJosef Bacik } 31275d4f98a2SYan Zheng 31285d4f98a2SYan Zheng path->search_commit_root = 1; 31295d4f98a2SYan Zheng path->skip_locking = 1; 31305d4f98a2SYan Zheng ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0); 31315d4f98a2SYan Zheng if (ret < 0) 31325d4f98a2SYan Zheng goto out; 31335d4f98a2SYan Zheng 3134aee68ee5SJosef Bacik if (ret > 0 && skinny) { 3135aee68ee5SJosef Bacik if (path->slots[0]) { 3136aee68ee5SJosef Bacik path->slots[0]--; 3137aee68ee5SJosef Bacik btrfs_item_key_to_cpu(path->nodes[0], &key, 3138aee68ee5SJosef Bacik path->slots[0]); 31393173a18fSJosef Bacik if (key.objectid == bytenr && 3140aee68ee5SJosef Bacik (key.type == BTRFS_METADATA_ITEM_KEY || 3141aee68ee5SJosef Bacik (key.type == BTRFS_EXTENT_ITEM_KEY && 3142aee68ee5SJosef Bacik key.offset == blocksize))) 31433173a18fSJosef Bacik ret = 0; 31443173a18fSJosef Bacik } 3145aee68ee5SJosef Bacik 3146aee68ee5SJosef Bacik if (ret) { 3147aee68ee5SJosef Bacik skinny = false; 3148aee68ee5SJosef Bacik btrfs_release_path(path); 3149aee68ee5SJosef Bacik goto again; 3150aee68ee5SJosef Bacik } 3151aee68ee5SJosef Bacik } 3152cdccee99SLiu Bo if (ret) { 3153cdccee99SLiu Bo ASSERT(ret == 1); 3154cdccee99SLiu Bo btrfs_print_leaf(path->nodes[0]); 3155cdccee99SLiu Bo btrfs_err(fs_info, 3156cdccee99SLiu Bo "tree block extent item (%llu) is not found in extent tree", 3157cdccee99SLiu Bo bytenr); 3158cdccee99SLiu Bo WARN_ON(1); 3159cdccee99SLiu Bo ret = -EINVAL; 3160cdccee99SLiu Bo goto out; 3161cdccee99SLiu Bo } 31623173a18fSJosef Bacik 31635d4f98a2SYan Zheng ret = add_tree_block(rc, &key, path, blocks); 31645d4f98a2SYan Zheng out: 31655d4f98a2SYan Zheng btrfs_free_path(path); 31665d4f98a2SYan Zheng return ret; 31675d4f98a2SYan Zheng } 31685d4f98a2SYan Zheng 31690af3d00bSJosef Bacik static int delete_block_group_cache(struct btrfs_fs_info *fs_info, 317032da5386SDavid Sterba struct btrfs_block_group *block_group, 31711bbc621eSChris Mason struct inode *inode, 31721bbc621eSChris Mason u64 ino) 31730af3d00bSJosef Bacik { 31740af3d00bSJosef Bacik struct btrfs_root *root = fs_info->tree_root; 31750af3d00bSJosef Bacik struct btrfs_trans_handle *trans; 31760af3d00bSJosef Bacik int ret = 0; 31770af3d00bSJosef Bacik 31780af3d00bSJosef Bacik if (inode) 31790af3d00bSJosef Bacik goto truncate; 31800af3d00bSJosef Bacik 31810202e83fSDavid Sterba inode = btrfs_iget(fs_info->sb, ino, root); 31822e19f1f9SAl Viro if (IS_ERR(inode)) 31830af3d00bSJosef Bacik return -ENOENT; 31840af3d00bSJosef Bacik 31850af3d00bSJosef Bacik truncate: 31862ff7e61eSJeff Mahoney ret = btrfs_check_trunc_cache_free_space(fs_info, 31877b61cd92SMiao Xie &fs_info->global_block_rsv); 31887b61cd92SMiao Xie if (ret) 31897b61cd92SMiao Xie goto out; 31907b61cd92SMiao Xie 31917a7eaa40SJosef Bacik trans = btrfs_join_transaction(root); 31920af3d00bSJosef Bacik if (IS_ERR(trans)) { 31933612b495STsutomu Itoh ret = PTR_ERR(trans); 31940af3d00bSJosef Bacik goto out; 31950af3d00bSJosef Bacik } 31960af3d00bSJosef Bacik 319777ab86bfSJeff Mahoney ret = btrfs_truncate_free_space_cache(trans, block_group, inode); 31980af3d00bSJosef Bacik 31993a45bb20SJeff Mahoney btrfs_end_transaction(trans); 32002ff7e61eSJeff Mahoney btrfs_btree_balance_dirty(fs_info); 32010af3d00bSJosef Bacik out: 32020af3d00bSJosef Bacik iput(inode); 32030af3d00bSJosef Bacik return ret; 32040af3d00bSJosef Bacik } 32050af3d00bSJosef Bacik 32065d4f98a2SYan Zheng /* 320719b546d7SQu Wenruo * Locate the free space cache EXTENT_DATA in root tree leaf and delete the 320819b546d7SQu Wenruo * cache inode, to avoid free space cache data extent blocking data relocation. 32095d4f98a2SYan Zheng */ 321019b546d7SQu Wenruo static int delete_v1_space_cache(struct extent_buffer *leaf, 321119b546d7SQu Wenruo struct btrfs_block_group *block_group, 321219b546d7SQu Wenruo u64 data_bytenr) 32135d4f98a2SYan Zheng { 321419b546d7SQu Wenruo u64 space_cache_ino; 321519b546d7SQu Wenruo struct btrfs_file_extent_item *ei; 32165d4f98a2SYan Zheng struct btrfs_key key; 321719b546d7SQu Wenruo bool found = false; 321819b546d7SQu Wenruo int i; 32195d4f98a2SYan Zheng int ret; 32205d4f98a2SYan Zheng 322119b546d7SQu Wenruo if (btrfs_header_owner(leaf) != BTRFS_ROOT_TREE_OBJECTID) 322219b546d7SQu Wenruo return 0; 32235d4f98a2SYan Zheng 322419b546d7SQu Wenruo for (i = 0; i < btrfs_header_nritems(leaf); i++) { 322550e31ef4SQu Wenruo u8 type; 322650e31ef4SQu Wenruo 322719b546d7SQu Wenruo btrfs_item_key_to_cpu(leaf, &key, i); 322819b546d7SQu Wenruo if (key.type != BTRFS_EXTENT_DATA_KEY) 322919b546d7SQu Wenruo continue; 323019b546d7SQu Wenruo ei = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); 323150e31ef4SQu Wenruo type = btrfs_file_extent_type(leaf, ei); 323250e31ef4SQu Wenruo 323350e31ef4SQu Wenruo if ((type == BTRFS_FILE_EXTENT_REG || 323450e31ef4SQu Wenruo type == BTRFS_FILE_EXTENT_PREALLOC) && 323519b546d7SQu Wenruo btrfs_file_extent_disk_bytenr(leaf, ei) == data_bytenr) { 323619b546d7SQu Wenruo found = true; 323719b546d7SQu Wenruo space_cache_ino = key.objectid; 323819b546d7SQu Wenruo break; 323919b546d7SQu Wenruo } 324019b546d7SQu Wenruo } 324119b546d7SQu Wenruo if (!found) 324219b546d7SQu Wenruo return -ENOENT; 324319b546d7SQu Wenruo ret = delete_block_group_cache(leaf->fs_info, block_group, NULL, 324419b546d7SQu Wenruo space_cache_ino); 32450af3d00bSJosef Bacik return ret; 32465d4f98a2SYan Zheng } 32475d4f98a2SYan Zheng 32485d4f98a2SYan Zheng /* 32492c016dc2SLiu Bo * helper to find all tree blocks that reference a given data extent 32505d4f98a2SYan Zheng */ 32515d4f98a2SYan Zheng static noinline_for_stack 32525d4f98a2SYan Zheng int add_data_references(struct reloc_control *rc, 32535d4f98a2SYan Zheng struct btrfs_key *extent_key, 32545d4f98a2SYan Zheng struct btrfs_path *path, 32555d4f98a2SYan Zheng struct rb_root *blocks) 32565d4f98a2SYan Zheng { 325719b546d7SQu Wenruo struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; 325819b546d7SQu Wenruo struct ulist *leaves = NULL; 325919b546d7SQu Wenruo struct ulist_iterator leaf_uiter; 326019b546d7SQu Wenruo struct ulist_node *ref_node = NULL; 326119b546d7SQu Wenruo const u32 blocksize = fs_info->nodesize; 3262647f63bdSFilipe David Borba Manana int ret = 0; 32635d4f98a2SYan Zheng 3264b3b4aa74SDavid Sterba btrfs_release_path(path); 326519b546d7SQu Wenruo ret = btrfs_find_all_leafs(NULL, fs_info, extent_key->objectid, 326619b546d7SQu Wenruo 0, &leaves, NULL, true); 326719b546d7SQu Wenruo if (ret < 0) 326819b546d7SQu Wenruo return ret; 326919b546d7SQu Wenruo 327019b546d7SQu Wenruo ULIST_ITER_INIT(&leaf_uiter); 327119b546d7SQu Wenruo while ((ref_node = ulist_next(leaves, &leaf_uiter))) { 327219b546d7SQu Wenruo struct extent_buffer *eb; 327319b546d7SQu Wenruo 32741b7ec85eSJosef Bacik eb = read_tree_block(fs_info, ref_node->val, 0, 0, 0, NULL); 327519b546d7SQu Wenruo if (IS_ERR(eb)) { 327619b546d7SQu Wenruo ret = PTR_ERR(eb); 327719b546d7SQu Wenruo break; 327819b546d7SQu Wenruo } 327919b546d7SQu Wenruo ret = delete_v1_space_cache(eb, rc->block_group, 328019b546d7SQu Wenruo extent_key->objectid); 328119b546d7SQu Wenruo free_extent_buffer(eb); 328219b546d7SQu Wenruo if (ret < 0) 328319b546d7SQu Wenruo break; 328419b546d7SQu Wenruo ret = __add_tree_block(rc, ref_node->val, blocksize, blocks); 328519b546d7SQu Wenruo if (ret < 0) 328619b546d7SQu Wenruo break; 328719b546d7SQu Wenruo } 328819b546d7SQu Wenruo if (ret < 0) 32895d4f98a2SYan Zheng free_block_list(blocks); 329019b546d7SQu Wenruo ulist_free(leaves); 329119b546d7SQu Wenruo return ret; 32925d4f98a2SYan Zheng } 32935d4f98a2SYan Zheng 32945d4f98a2SYan Zheng /* 32952c016dc2SLiu Bo * helper to find next unprocessed extent 32965d4f98a2SYan Zheng */ 32975d4f98a2SYan Zheng static noinline_for_stack 3298147d256eSZhaolei int find_next_extent(struct reloc_control *rc, struct btrfs_path *path, 32993fd0a558SYan, Zheng struct btrfs_key *extent_key) 33005d4f98a2SYan Zheng { 33010b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; 33025d4f98a2SYan Zheng struct btrfs_key key; 33035d4f98a2SYan Zheng struct extent_buffer *leaf; 33045d4f98a2SYan Zheng u64 start, end, last; 33055d4f98a2SYan Zheng int ret; 33065d4f98a2SYan Zheng 3307b3470b5dSDavid Sterba last = rc->block_group->start + rc->block_group->length; 33085d4f98a2SYan Zheng while (1) { 33095d4f98a2SYan Zheng cond_resched(); 33105d4f98a2SYan Zheng if (rc->search_start >= last) { 33115d4f98a2SYan Zheng ret = 1; 33125d4f98a2SYan Zheng break; 33135d4f98a2SYan Zheng } 33145d4f98a2SYan Zheng 33155d4f98a2SYan Zheng key.objectid = rc->search_start; 33165d4f98a2SYan Zheng key.type = BTRFS_EXTENT_ITEM_KEY; 33175d4f98a2SYan Zheng key.offset = 0; 33185d4f98a2SYan Zheng 33195d4f98a2SYan Zheng path->search_commit_root = 1; 33205d4f98a2SYan Zheng path->skip_locking = 1; 33215d4f98a2SYan Zheng ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 33225d4f98a2SYan Zheng 0, 0); 33235d4f98a2SYan Zheng if (ret < 0) 33245d4f98a2SYan Zheng break; 33255d4f98a2SYan Zheng next: 33265d4f98a2SYan Zheng leaf = path->nodes[0]; 33275d4f98a2SYan Zheng if (path->slots[0] >= btrfs_header_nritems(leaf)) { 33285d4f98a2SYan Zheng ret = btrfs_next_leaf(rc->extent_root, path); 33295d4f98a2SYan Zheng if (ret != 0) 33305d4f98a2SYan Zheng break; 33315d4f98a2SYan Zheng leaf = path->nodes[0]; 33325d4f98a2SYan Zheng } 33335d4f98a2SYan Zheng 33345d4f98a2SYan Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 33355d4f98a2SYan Zheng if (key.objectid >= last) { 33365d4f98a2SYan Zheng ret = 1; 33375d4f98a2SYan Zheng break; 33385d4f98a2SYan Zheng } 33395d4f98a2SYan Zheng 33403173a18fSJosef Bacik if (key.type != BTRFS_EXTENT_ITEM_KEY && 33413173a18fSJosef Bacik key.type != BTRFS_METADATA_ITEM_KEY) { 33423173a18fSJosef Bacik path->slots[0]++; 33433173a18fSJosef Bacik goto next; 33443173a18fSJosef Bacik } 33453173a18fSJosef Bacik 33463173a18fSJosef Bacik if (key.type == BTRFS_EXTENT_ITEM_KEY && 33475d4f98a2SYan Zheng key.objectid + key.offset <= rc->search_start) { 33485d4f98a2SYan Zheng path->slots[0]++; 33495d4f98a2SYan Zheng goto next; 33505d4f98a2SYan Zheng } 33515d4f98a2SYan Zheng 33523173a18fSJosef Bacik if (key.type == BTRFS_METADATA_ITEM_KEY && 33530b246afaSJeff Mahoney key.objectid + fs_info->nodesize <= 33543173a18fSJosef Bacik rc->search_start) { 33553173a18fSJosef Bacik path->slots[0]++; 33563173a18fSJosef Bacik goto next; 33573173a18fSJosef Bacik } 33583173a18fSJosef Bacik 33595d4f98a2SYan Zheng ret = find_first_extent_bit(&rc->processed_blocks, 33605d4f98a2SYan Zheng key.objectid, &start, &end, 3361e6138876SJosef Bacik EXTENT_DIRTY, NULL); 33625d4f98a2SYan Zheng 33635d4f98a2SYan Zheng if (ret == 0 && start <= key.objectid) { 3364b3b4aa74SDavid Sterba btrfs_release_path(path); 33655d4f98a2SYan Zheng rc->search_start = end + 1; 33665d4f98a2SYan Zheng } else { 33673173a18fSJosef Bacik if (key.type == BTRFS_EXTENT_ITEM_KEY) 33685d4f98a2SYan Zheng rc->search_start = key.objectid + key.offset; 33693173a18fSJosef Bacik else 33703173a18fSJosef Bacik rc->search_start = key.objectid + 33710b246afaSJeff Mahoney fs_info->nodesize; 33723fd0a558SYan, Zheng memcpy(extent_key, &key, sizeof(key)); 33735d4f98a2SYan Zheng return 0; 33745d4f98a2SYan Zheng } 33755d4f98a2SYan Zheng } 3376b3b4aa74SDavid Sterba btrfs_release_path(path); 33775d4f98a2SYan Zheng return ret; 33785d4f98a2SYan Zheng } 33795d4f98a2SYan Zheng 33805d4f98a2SYan Zheng static void set_reloc_control(struct reloc_control *rc) 33815d4f98a2SYan Zheng { 33825d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; 33837585717fSChris Mason 33847585717fSChris Mason mutex_lock(&fs_info->reloc_mutex); 33855d4f98a2SYan Zheng fs_info->reloc_ctl = rc; 33867585717fSChris Mason mutex_unlock(&fs_info->reloc_mutex); 33875d4f98a2SYan Zheng } 33885d4f98a2SYan Zheng 33895d4f98a2SYan Zheng static void unset_reloc_control(struct reloc_control *rc) 33905d4f98a2SYan Zheng { 33915d4f98a2SYan Zheng struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; 33927585717fSChris Mason 33937585717fSChris Mason mutex_lock(&fs_info->reloc_mutex); 33945d4f98a2SYan Zheng fs_info->reloc_ctl = NULL; 33957585717fSChris Mason mutex_unlock(&fs_info->reloc_mutex); 33965d4f98a2SYan Zheng } 33975d4f98a2SYan Zheng 33985d4f98a2SYan Zheng static int check_extent_flags(u64 flags) 33995d4f98a2SYan Zheng { 34005d4f98a2SYan Zheng if ((flags & BTRFS_EXTENT_FLAG_DATA) && 34015d4f98a2SYan Zheng (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) 34025d4f98a2SYan Zheng return 1; 34035d4f98a2SYan Zheng if (!(flags & BTRFS_EXTENT_FLAG_DATA) && 34045d4f98a2SYan Zheng !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) 34055d4f98a2SYan Zheng return 1; 34065d4f98a2SYan Zheng if ((flags & BTRFS_EXTENT_FLAG_DATA) && 34075d4f98a2SYan Zheng (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) 34085d4f98a2SYan Zheng return 1; 34095d4f98a2SYan Zheng return 0; 34105d4f98a2SYan Zheng } 34115d4f98a2SYan Zheng 34123fd0a558SYan, Zheng static noinline_for_stack 34133fd0a558SYan, Zheng int prepare_to_relocate(struct reloc_control *rc) 34143fd0a558SYan, Zheng { 34153fd0a558SYan, Zheng struct btrfs_trans_handle *trans; 3416ac2fabacSJosef Bacik int ret; 34173fd0a558SYan, Zheng 34182ff7e61eSJeff Mahoney rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root->fs_info, 341966d8f3ddSMiao Xie BTRFS_BLOCK_RSV_TEMP); 34203fd0a558SYan, Zheng if (!rc->block_rsv) 34213fd0a558SYan, Zheng return -ENOMEM; 34223fd0a558SYan, Zheng 34233fd0a558SYan, Zheng memset(&rc->cluster, 0, sizeof(rc->cluster)); 3424b3470b5dSDavid Sterba rc->search_start = rc->block_group->start; 34253fd0a558SYan, Zheng rc->extents_found = 0; 34263fd0a558SYan, Zheng rc->nodes_relocated = 0; 34273fd0a558SYan, Zheng rc->merging_rsv_size = 0; 34280647bf56SWang Shilong rc->reserved_bytes = 0; 3429da17066cSJeff Mahoney rc->block_rsv->size = rc->extent_root->fs_info->nodesize * 34300647bf56SWang Shilong RELOCATION_RESERVED_NODES; 3431ac2fabacSJosef Bacik ret = btrfs_block_rsv_refill(rc->extent_root, 3432ac2fabacSJosef Bacik rc->block_rsv, rc->block_rsv->size, 3433ac2fabacSJosef Bacik BTRFS_RESERVE_FLUSH_ALL); 3434ac2fabacSJosef Bacik if (ret) 3435ac2fabacSJosef Bacik return ret; 34363fd0a558SYan, Zheng 34373fd0a558SYan, Zheng rc->create_reloc_tree = 1; 34383fd0a558SYan, Zheng set_reloc_control(rc); 34393fd0a558SYan, Zheng 34407a7eaa40SJosef Bacik trans = btrfs_join_transaction(rc->extent_root); 344128818947SLiu Bo if (IS_ERR(trans)) { 344228818947SLiu Bo unset_reloc_control(rc); 344328818947SLiu Bo /* 344428818947SLiu Bo * extent tree is not a ref_cow tree and has no reloc_root to 344528818947SLiu Bo * cleanup. And callers are responsible to free the above 344628818947SLiu Bo * block rsv. 344728818947SLiu Bo */ 344828818947SLiu Bo return PTR_ERR(trans); 344928818947SLiu Bo } 34503a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 34513fd0a558SYan, Zheng return 0; 34523fd0a558SYan, Zheng } 345376dda93cSYan, Zheng 34545d4f98a2SYan Zheng static noinline_for_stack int relocate_block_group(struct reloc_control *rc) 34555d4f98a2SYan Zheng { 34562ff7e61eSJeff Mahoney struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; 34575d4f98a2SYan Zheng struct rb_root blocks = RB_ROOT; 34585d4f98a2SYan Zheng struct btrfs_key key; 34595d4f98a2SYan Zheng struct btrfs_trans_handle *trans = NULL; 34605d4f98a2SYan Zheng struct btrfs_path *path; 34615d4f98a2SYan Zheng struct btrfs_extent_item *ei; 34625d4f98a2SYan Zheng u64 flags; 34635d4f98a2SYan Zheng u32 item_size; 34645d4f98a2SYan Zheng int ret; 34655d4f98a2SYan Zheng int err = 0; 3466c87f08caSChris Mason int progress = 0; 34675d4f98a2SYan Zheng 34685d4f98a2SYan Zheng path = btrfs_alloc_path(); 34693fd0a558SYan, Zheng if (!path) 34705d4f98a2SYan Zheng return -ENOMEM; 3471e4058b54SDavid Sterba path->reada = READA_FORWARD; 34723fd0a558SYan, Zheng 34733fd0a558SYan, Zheng ret = prepare_to_relocate(rc); 34743fd0a558SYan, Zheng if (ret) { 34753fd0a558SYan, Zheng err = ret; 34763fd0a558SYan, Zheng goto out_free; 34772423fdfbSJiri Slaby } 34785d4f98a2SYan Zheng 34795d4f98a2SYan Zheng while (1) { 34800647bf56SWang Shilong rc->reserved_bytes = 0; 34810647bf56SWang Shilong ret = btrfs_block_rsv_refill(rc->extent_root, 34820647bf56SWang Shilong rc->block_rsv, rc->block_rsv->size, 34830647bf56SWang Shilong BTRFS_RESERVE_FLUSH_ALL); 34840647bf56SWang Shilong if (ret) { 34850647bf56SWang Shilong err = ret; 34860647bf56SWang Shilong break; 34870647bf56SWang Shilong } 3488c87f08caSChris Mason progress++; 3489a22285a6SYan, Zheng trans = btrfs_start_transaction(rc->extent_root, 0); 34900f788c58SLiu Bo if (IS_ERR(trans)) { 34910f788c58SLiu Bo err = PTR_ERR(trans); 34920f788c58SLiu Bo trans = NULL; 34930f788c58SLiu Bo break; 34940f788c58SLiu Bo } 3495c87f08caSChris Mason restart: 34963fd0a558SYan, Zheng if (update_backref_cache(trans, &rc->backref_cache)) { 34973a45bb20SJeff Mahoney btrfs_end_transaction(trans); 349842a657f5SPan Bian trans = NULL; 34993fd0a558SYan, Zheng continue; 35003fd0a558SYan, Zheng } 35013fd0a558SYan, Zheng 3502147d256eSZhaolei ret = find_next_extent(rc, path, &key); 35035d4f98a2SYan Zheng if (ret < 0) 35045d4f98a2SYan Zheng err = ret; 35055d4f98a2SYan Zheng if (ret != 0) 35065d4f98a2SYan Zheng break; 35075d4f98a2SYan Zheng 35085d4f98a2SYan Zheng rc->extents_found++; 35095d4f98a2SYan Zheng 35105d4f98a2SYan Zheng ei = btrfs_item_ptr(path->nodes[0], path->slots[0], 35115d4f98a2SYan Zheng struct btrfs_extent_item); 35123fd0a558SYan, Zheng item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]); 35135d4f98a2SYan Zheng if (item_size >= sizeof(*ei)) { 35145d4f98a2SYan Zheng flags = btrfs_extent_flags(path->nodes[0], ei); 35155d4f98a2SYan Zheng ret = check_extent_flags(flags); 35165d4f98a2SYan Zheng BUG_ON(ret); 35176d8ff4e4SDavid Sterba } else if (unlikely(item_size == sizeof(struct btrfs_extent_item_v0))) { 3518ba3c2b19SNikolay Borisov err = -EINVAL; 3519ba3c2b19SNikolay Borisov btrfs_print_v0_err(trans->fs_info); 3520ba3c2b19SNikolay Borisov btrfs_abort_transaction(trans, err); 3521ba3c2b19SNikolay Borisov break; 35225d4f98a2SYan Zheng } else { 35235d4f98a2SYan Zheng BUG(); 35245d4f98a2SYan Zheng } 35255d4f98a2SYan Zheng 35265d4f98a2SYan Zheng if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { 35275d4f98a2SYan Zheng ret = add_tree_block(rc, &key, path, &blocks); 35285d4f98a2SYan Zheng } else if (rc->stage == UPDATE_DATA_PTRS && 35295d4f98a2SYan Zheng (flags & BTRFS_EXTENT_FLAG_DATA)) { 35305d4f98a2SYan Zheng ret = add_data_references(rc, &key, path, &blocks); 35315d4f98a2SYan Zheng } else { 3532b3b4aa74SDavid Sterba btrfs_release_path(path); 35335d4f98a2SYan Zheng ret = 0; 35345d4f98a2SYan Zheng } 35355d4f98a2SYan Zheng if (ret < 0) { 35363fd0a558SYan, Zheng err = ret; 35375d4f98a2SYan Zheng break; 35385d4f98a2SYan Zheng } 35395d4f98a2SYan Zheng 35405d4f98a2SYan Zheng if (!RB_EMPTY_ROOT(&blocks)) { 35415d4f98a2SYan Zheng ret = relocate_tree_blocks(trans, rc, &blocks); 35425d4f98a2SYan Zheng if (ret < 0) { 35433fd0a558SYan, Zheng if (ret != -EAGAIN) { 35445d4f98a2SYan Zheng err = ret; 35455d4f98a2SYan Zheng break; 35465d4f98a2SYan Zheng } 35473fd0a558SYan, Zheng rc->extents_found--; 35483fd0a558SYan, Zheng rc->search_start = key.objectid; 35493fd0a558SYan, Zheng } 35505d4f98a2SYan Zheng } 35515d4f98a2SYan Zheng 35523a45bb20SJeff Mahoney btrfs_end_transaction_throttle(trans); 35532ff7e61eSJeff Mahoney btrfs_btree_balance_dirty(fs_info); 35543fd0a558SYan, Zheng trans = NULL; 35555d4f98a2SYan Zheng 35565d4f98a2SYan Zheng if (rc->stage == MOVE_DATA_EXTENTS && 35575d4f98a2SYan Zheng (flags & BTRFS_EXTENT_FLAG_DATA)) { 35585d4f98a2SYan Zheng rc->found_file_extent = 1; 35590257bb82SYan, Zheng ret = relocate_data_extent(rc->data_inode, 35603fd0a558SYan, Zheng &key, &rc->cluster); 35615d4f98a2SYan Zheng if (ret < 0) { 35625d4f98a2SYan Zheng err = ret; 35635d4f98a2SYan Zheng break; 35645d4f98a2SYan Zheng } 35655d4f98a2SYan Zheng } 3566f31ea088SQu Wenruo if (btrfs_should_cancel_balance(fs_info)) { 3567f31ea088SQu Wenruo err = -ECANCELED; 3568f31ea088SQu Wenruo break; 3569f31ea088SQu Wenruo } 35705d4f98a2SYan Zheng } 3571c87f08caSChris Mason if (trans && progress && err == -ENOSPC) { 357243a7e99dSNikolay Borisov ret = btrfs_force_chunk_alloc(trans, rc->block_group->flags); 35739689457bSShilong Wang if (ret == 1) { 3574c87f08caSChris Mason err = 0; 3575c87f08caSChris Mason progress = 0; 3576c87f08caSChris Mason goto restart; 3577c87f08caSChris Mason } 3578c87f08caSChris Mason } 35793fd0a558SYan, Zheng 3580b3b4aa74SDavid Sterba btrfs_release_path(path); 358191166212SDavid Sterba clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY); 35825d4f98a2SYan Zheng 35835d4f98a2SYan Zheng if (trans) { 35843a45bb20SJeff Mahoney btrfs_end_transaction_throttle(trans); 35852ff7e61eSJeff Mahoney btrfs_btree_balance_dirty(fs_info); 35865d4f98a2SYan Zheng } 35875d4f98a2SYan Zheng 35880257bb82SYan, Zheng if (!err) { 35893fd0a558SYan, Zheng ret = relocate_file_extent_cluster(rc->data_inode, 35903fd0a558SYan, Zheng &rc->cluster); 35910257bb82SYan, Zheng if (ret < 0) 35920257bb82SYan, Zheng err = ret; 35930257bb82SYan, Zheng } 35940257bb82SYan, Zheng 35953fd0a558SYan, Zheng rc->create_reloc_tree = 0; 35963fd0a558SYan, Zheng set_reloc_control(rc); 35970257bb82SYan, Zheng 359813fe1bdbSQu Wenruo btrfs_backref_release_cache(&rc->backref_cache); 359963f018beSNikolay Borisov btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL); 36005d4f98a2SYan Zheng 36017f913c7cSQu Wenruo /* 36027f913c7cSQu Wenruo * Even in the case when the relocation is cancelled, we should all go 36037f913c7cSQu Wenruo * through prepare_to_merge() and merge_reloc_roots(). 36047f913c7cSQu Wenruo * 36057f913c7cSQu Wenruo * For error (including cancelled balance), prepare_to_merge() will 36067f913c7cSQu Wenruo * mark all reloc trees orphan, then queue them for cleanup in 36077f913c7cSQu Wenruo * merge_reloc_roots() 36087f913c7cSQu Wenruo */ 36093fd0a558SYan, Zheng err = prepare_to_merge(rc, err); 36105d4f98a2SYan Zheng 36115d4f98a2SYan Zheng merge_reloc_roots(rc); 36125d4f98a2SYan Zheng 36133fd0a558SYan, Zheng rc->merge_reloc_tree = 0; 36145d4f98a2SYan Zheng unset_reloc_control(rc); 361563f018beSNikolay Borisov btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL); 36165d4f98a2SYan Zheng 36175d4f98a2SYan Zheng /* get rid of pinned extents */ 36187a7eaa40SJosef Bacik trans = btrfs_join_transaction(rc->extent_root); 361962b99540SQu Wenruo if (IS_ERR(trans)) { 36203612b495STsutomu Itoh err = PTR_ERR(trans); 362162b99540SQu Wenruo goto out_free; 362262b99540SQu Wenruo } 36233a45bb20SJeff Mahoney btrfs_commit_transaction(trans); 36246217b0faSJosef Bacik out_free: 3625d2311e69SQu Wenruo ret = clean_dirty_subvols(rc); 3626d2311e69SQu Wenruo if (ret < 0 && !err) 3627d2311e69SQu Wenruo err = ret; 36282ff7e61eSJeff Mahoney btrfs_free_block_rsv(fs_info, rc->block_rsv); 36293fd0a558SYan, Zheng btrfs_free_path(path); 36305d4f98a2SYan Zheng return err; 36315d4f98a2SYan Zheng } 36325d4f98a2SYan Zheng 36335d4f98a2SYan Zheng static int __insert_orphan_inode(struct btrfs_trans_handle *trans, 36340257bb82SYan, Zheng struct btrfs_root *root, u64 objectid) 36355d4f98a2SYan Zheng { 36365d4f98a2SYan Zheng struct btrfs_path *path; 36375d4f98a2SYan Zheng struct btrfs_inode_item *item; 36385d4f98a2SYan Zheng struct extent_buffer *leaf; 363932430c61SNaohiro Aota u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC; 36405d4f98a2SYan Zheng int ret; 36415d4f98a2SYan Zheng 364232430c61SNaohiro Aota if (btrfs_is_zoned(trans->fs_info)) 364332430c61SNaohiro Aota flags &= ~BTRFS_INODE_PREALLOC; 364432430c61SNaohiro Aota 36455d4f98a2SYan Zheng path = btrfs_alloc_path(); 36465d4f98a2SYan Zheng if (!path) 36475d4f98a2SYan Zheng return -ENOMEM; 36485d4f98a2SYan Zheng 36495d4f98a2SYan Zheng ret = btrfs_insert_empty_inode(trans, root, path, objectid); 36505d4f98a2SYan Zheng if (ret) 36515d4f98a2SYan Zheng goto out; 36525d4f98a2SYan Zheng 36535d4f98a2SYan Zheng leaf = path->nodes[0]; 36545d4f98a2SYan Zheng item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item); 3655b159fa28SDavid Sterba memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item)); 36565d4f98a2SYan Zheng btrfs_set_inode_generation(leaf, item, 1); 36570257bb82SYan, Zheng btrfs_set_inode_size(leaf, item, 0); 36585d4f98a2SYan Zheng btrfs_set_inode_mode(leaf, item, S_IFREG | 0600); 365932430c61SNaohiro Aota btrfs_set_inode_flags(leaf, item, flags); 36605d4f98a2SYan Zheng btrfs_mark_buffer_dirty(leaf); 36615d4f98a2SYan Zheng out: 36625d4f98a2SYan Zheng btrfs_free_path(path); 36635d4f98a2SYan Zheng return ret; 36645d4f98a2SYan Zheng } 36655d4f98a2SYan Zheng 36665d4f98a2SYan Zheng /* 36675d4f98a2SYan Zheng * helper to create inode for data relocation. 36685d4f98a2SYan Zheng * the inode is in data relocation tree and its link count is 0 36695d4f98a2SYan Zheng */ 36703fd0a558SYan, Zheng static noinline_for_stack 36713fd0a558SYan, Zheng struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info, 367232da5386SDavid Sterba struct btrfs_block_group *group) 36735d4f98a2SYan Zheng { 36745d4f98a2SYan Zheng struct inode *inode = NULL; 36755d4f98a2SYan Zheng struct btrfs_trans_handle *trans; 36765d4f98a2SYan Zheng struct btrfs_root *root; 36774624900dSZhaolei u64 objectid; 36785d4f98a2SYan Zheng int err = 0; 36795d4f98a2SYan Zheng 3680aeb935a4SQu Wenruo root = btrfs_grab_root(fs_info->data_reloc_root); 3681a22285a6SYan, Zheng trans = btrfs_start_transaction(root, 6); 368276deacf0SJosef Bacik if (IS_ERR(trans)) { 368300246528SJosef Bacik btrfs_put_root(root); 36843fd0a558SYan, Zheng return ERR_CAST(trans); 368576deacf0SJosef Bacik } 36865d4f98a2SYan Zheng 3687543068a2SNikolay Borisov err = btrfs_get_free_objectid(root, &objectid); 36885d4f98a2SYan Zheng if (err) 36895d4f98a2SYan Zheng goto out; 36905d4f98a2SYan Zheng 36910257bb82SYan, Zheng err = __insert_orphan_inode(trans, root, objectid); 36925d4f98a2SYan Zheng BUG_ON(err); 36935d4f98a2SYan Zheng 36940202e83fSDavid Sterba inode = btrfs_iget(fs_info->sb, objectid, root); 36952e19f1f9SAl Viro BUG_ON(IS_ERR(inode)); 3696b3470b5dSDavid Sterba BTRFS_I(inode)->index_cnt = group->start; 36975d4f98a2SYan Zheng 369873f2e545SNikolay Borisov err = btrfs_orphan_add(trans, BTRFS_I(inode)); 36995d4f98a2SYan Zheng out: 370000246528SJosef Bacik btrfs_put_root(root); 37013a45bb20SJeff Mahoney btrfs_end_transaction(trans); 37022ff7e61eSJeff Mahoney btrfs_btree_balance_dirty(fs_info); 37035d4f98a2SYan Zheng if (err) { 37045d4f98a2SYan Zheng if (inode) 37055d4f98a2SYan Zheng iput(inode); 37065d4f98a2SYan Zheng inode = ERR_PTR(err); 37075d4f98a2SYan Zheng } 37085d4f98a2SYan Zheng return inode; 37095d4f98a2SYan Zheng } 37105d4f98a2SYan Zheng 3711c258d6e3SQu Wenruo static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info) 37123fd0a558SYan, Zheng { 37133fd0a558SYan, Zheng struct reloc_control *rc; 37143fd0a558SYan, Zheng 37153fd0a558SYan, Zheng rc = kzalloc(sizeof(*rc), GFP_NOFS); 37163fd0a558SYan, Zheng if (!rc) 37173fd0a558SYan, Zheng return NULL; 37183fd0a558SYan, Zheng 37193fd0a558SYan, Zheng INIT_LIST_HEAD(&rc->reloc_roots); 3720d2311e69SQu Wenruo INIT_LIST_HEAD(&rc->dirty_subvol_roots); 3721584fb121SQu Wenruo btrfs_backref_init_cache(fs_info, &rc->backref_cache, 1); 37223fd0a558SYan, Zheng mapping_tree_init(&rc->reloc_root_tree); 372343eb5f29SQu Wenruo extent_io_tree_init(fs_info, &rc->processed_blocks, 372443eb5f29SQu Wenruo IO_TREE_RELOC_BLOCKS, NULL); 37253fd0a558SYan, Zheng return rc; 37263fd0a558SYan, Zheng } 37273fd0a558SYan, Zheng 37281a0afa0eSJosef Bacik static void free_reloc_control(struct reloc_control *rc) 37291a0afa0eSJosef Bacik { 37301a0afa0eSJosef Bacik struct mapping_node *node, *tmp; 37311a0afa0eSJosef Bacik 37321a0afa0eSJosef Bacik free_reloc_roots(&rc->reloc_roots); 37331a0afa0eSJosef Bacik rbtree_postorder_for_each_entry_safe(node, tmp, 37341a0afa0eSJosef Bacik &rc->reloc_root_tree.rb_root, rb_node) 37351a0afa0eSJosef Bacik kfree(node); 37361a0afa0eSJosef Bacik 37371a0afa0eSJosef Bacik kfree(rc); 37381a0afa0eSJosef Bacik } 37391a0afa0eSJosef Bacik 37405d4f98a2SYan Zheng /* 3741ebce0e01SAdam Borowski * Print the block group being relocated 3742ebce0e01SAdam Borowski */ 3743ebce0e01SAdam Borowski static void describe_relocation(struct btrfs_fs_info *fs_info, 374432da5386SDavid Sterba struct btrfs_block_group *block_group) 3745ebce0e01SAdam Borowski { 3746f89e09cfSAnand Jain char buf[128] = {'\0'}; 3747ebce0e01SAdam Borowski 3748f89e09cfSAnand Jain btrfs_describe_block_groups(block_group->flags, buf, sizeof(buf)); 3749ebce0e01SAdam Borowski 3750ebce0e01SAdam Borowski btrfs_info(fs_info, 3751ebce0e01SAdam Borowski "relocating block group %llu flags %s", 3752b3470b5dSDavid Sterba block_group->start, buf); 3753ebce0e01SAdam Borowski } 3754ebce0e01SAdam Borowski 3755430640e3SQu Wenruo static const char *stage_to_string(int stage) 3756430640e3SQu Wenruo { 3757430640e3SQu Wenruo if (stage == MOVE_DATA_EXTENTS) 3758430640e3SQu Wenruo return "move data extents"; 3759430640e3SQu Wenruo if (stage == UPDATE_DATA_PTRS) 3760430640e3SQu Wenruo return "update data pointers"; 3761430640e3SQu Wenruo return "unknown"; 3762430640e3SQu Wenruo } 3763430640e3SQu Wenruo 3764ebce0e01SAdam Borowski /* 37655d4f98a2SYan Zheng * function to relocate all extents in a block group. 37665d4f98a2SYan Zheng */ 37676bccf3abSJeff Mahoney int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start) 37685d4f98a2SYan Zheng { 376932da5386SDavid Sterba struct btrfs_block_group *bg; 37706bccf3abSJeff Mahoney struct btrfs_root *extent_root = fs_info->extent_root; 37715d4f98a2SYan Zheng struct reloc_control *rc; 37720af3d00bSJosef Bacik struct inode *inode; 37730af3d00bSJosef Bacik struct btrfs_path *path; 37745d4f98a2SYan Zheng int ret; 3775f0486c68SYan, Zheng int rw = 0; 37765d4f98a2SYan Zheng int err = 0; 37775d4f98a2SYan Zheng 3778eede2bf3SOmar Sandoval bg = btrfs_lookup_block_group(fs_info, group_start); 3779eede2bf3SOmar Sandoval if (!bg) 3780eede2bf3SOmar Sandoval return -ENOENT; 3781eede2bf3SOmar Sandoval 3782eede2bf3SOmar Sandoval if (btrfs_pinned_by_swapfile(fs_info, bg)) { 3783eede2bf3SOmar Sandoval btrfs_put_block_group(bg); 3784eede2bf3SOmar Sandoval return -ETXTBSY; 3785eede2bf3SOmar Sandoval } 3786eede2bf3SOmar Sandoval 3787c258d6e3SQu Wenruo rc = alloc_reloc_control(fs_info); 3788eede2bf3SOmar Sandoval if (!rc) { 3789eede2bf3SOmar Sandoval btrfs_put_block_group(bg); 37905d4f98a2SYan Zheng return -ENOMEM; 3791eede2bf3SOmar Sandoval } 37925d4f98a2SYan Zheng 3793f0486c68SYan, Zheng rc->extent_root = extent_root; 3794eede2bf3SOmar Sandoval rc->block_group = bg; 37955d4f98a2SYan Zheng 3796b12de528SQu Wenruo ret = btrfs_inc_block_group_ro(rc->block_group, true); 3797f0486c68SYan, Zheng if (ret) { 3798f0486c68SYan, Zheng err = ret; 3799f0486c68SYan, Zheng goto out; 3800f0486c68SYan, Zheng } 3801f0486c68SYan, Zheng rw = 1; 3802f0486c68SYan, Zheng 38030af3d00bSJosef Bacik path = btrfs_alloc_path(); 38040af3d00bSJosef Bacik if (!path) { 38050af3d00bSJosef Bacik err = -ENOMEM; 38060af3d00bSJosef Bacik goto out; 38070af3d00bSJosef Bacik } 38080af3d00bSJosef Bacik 38097949f339SDavid Sterba inode = lookup_free_space_inode(rc->block_group, path); 38100af3d00bSJosef Bacik btrfs_free_path(path); 38110af3d00bSJosef Bacik 38120af3d00bSJosef Bacik if (!IS_ERR(inode)) 38131bbc621eSChris Mason ret = delete_block_group_cache(fs_info, rc->block_group, inode, 0); 38140af3d00bSJosef Bacik else 38150af3d00bSJosef Bacik ret = PTR_ERR(inode); 38160af3d00bSJosef Bacik 38170af3d00bSJosef Bacik if (ret && ret != -ENOENT) { 38180af3d00bSJosef Bacik err = ret; 38190af3d00bSJosef Bacik goto out; 38200af3d00bSJosef Bacik } 38210af3d00bSJosef Bacik 38225d4f98a2SYan Zheng rc->data_inode = create_reloc_inode(fs_info, rc->block_group); 38235d4f98a2SYan Zheng if (IS_ERR(rc->data_inode)) { 38245d4f98a2SYan Zheng err = PTR_ERR(rc->data_inode); 38255d4f98a2SYan Zheng rc->data_inode = NULL; 38265d4f98a2SYan Zheng goto out; 38275d4f98a2SYan Zheng } 38285d4f98a2SYan Zheng 38290b246afaSJeff Mahoney describe_relocation(fs_info, rc->block_group); 38305d4f98a2SYan Zheng 38319cfa3e34SFilipe Manana btrfs_wait_block_group_reservations(rc->block_group); 3832f78c436cSFilipe Manana btrfs_wait_nocow_writers(rc->block_group); 38336374e57aSChris Mason btrfs_wait_ordered_roots(fs_info, U64_MAX, 3834b3470b5dSDavid Sterba rc->block_group->start, 3835b3470b5dSDavid Sterba rc->block_group->length); 38365d4f98a2SYan Zheng 38375d4f98a2SYan Zheng while (1) { 3838430640e3SQu Wenruo int finishes_stage; 3839430640e3SQu Wenruo 384076dda93cSYan, Zheng mutex_lock(&fs_info->cleaner_mutex); 38415d4f98a2SYan Zheng ret = relocate_block_group(rc); 384276dda93cSYan, Zheng mutex_unlock(&fs_info->cleaner_mutex); 3843ff612ba7SJosef Bacik if (ret < 0) 38445d4f98a2SYan Zheng err = ret; 3845ff612ba7SJosef Bacik 3846430640e3SQu Wenruo finishes_stage = rc->stage; 3847ff612ba7SJosef Bacik /* 3848ff612ba7SJosef Bacik * We may have gotten ENOSPC after we already dirtied some 3849ff612ba7SJosef Bacik * extents. If writeout happens while we're relocating a 3850ff612ba7SJosef Bacik * different block group we could end up hitting the 3851ff612ba7SJosef Bacik * BUG_ON(rc->stage == UPDATE_DATA_PTRS) in 3852ff612ba7SJosef Bacik * btrfs_reloc_cow_block. Make sure we write everything out 3853ff612ba7SJosef Bacik * properly so we don't trip over this problem, and then break 3854ff612ba7SJosef Bacik * out of the loop if we hit an error. 3855ff612ba7SJosef Bacik */ 3856ff612ba7SJosef Bacik if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) { 3857ff612ba7SJosef Bacik ret = btrfs_wait_ordered_range(rc->data_inode, 0, 3858ff612ba7SJosef Bacik (u64)-1); 3859ff612ba7SJosef Bacik if (ret) 3860ff612ba7SJosef Bacik err = ret; 3861ff612ba7SJosef Bacik invalidate_mapping_pages(rc->data_inode->i_mapping, 3862ff612ba7SJosef Bacik 0, -1); 3863ff612ba7SJosef Bacik rc->stage = UPDATE_DATA_PTRS; 38645d4f98a2SYan Zheng } 38655d4f98a2SYan Zheng 3866ff612ba7SJosef Bacik if (err < 0) 3867ff612ba7SJosef Bacik goto out; 3868ff612ba7SJosef Bacik 38695d4f98a2SYan Zheng if (rc->extents_found == 0) 38705d4f98a2SYan Zheng break; 38715d4f98a2SYan Zheng 3872430640e3SQu Wenruo btrfs_info(fs_info, "found %llu extents, stage: %s", 3873430640e3SQu Wenruo rc->extents_found, stage_to_string(finishes_stage)); 38745d4f98a2SYan Zheng } 38755d4f98a2SYan Zheng 38765d4f98a2SYan Zheng WARN_ON(rc->block_group->pinned > 0); 38775d4f98a2SYan Zheng WARN_ON(rc->block_group->reserved > 0); 3878bf38be65SDavid Sterba WARN_ON(rc->block_group->used > 0); 38795d4f98a2SYan Zheng out: 3880f0486c68SYan, Zheng if (err && rw) 38812ff7e61eSJeff Mahoney btrfs_dec_block_group_ro(rc->block_group); 38825d4f98a2SYan Zheng iput(rc->data_inode); 38835d4f98a2SYan Zheng btrfs_put_block_group(rc->block_group); 38841a0afa0eSJosef Bacik free_reloc_control(rc); 38855d4f98a2SYan Zheng return err; 38865d4f98a2SYan Zheng } 38875d4f98a2SYan Zheng 388876dda93cSYan, Zheng static noinline_for_stack int mark_garbage_root(struct btrfs_root *root) 388976dda93cSYan, Zheng { 38900b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 389176dda93cSYan, Zheng struct btrfs_trans_handle *trans; 389279787eaaSJeff Mahoney int ret, err; 389376dda93cSYan, Zheng 38940b246afaSJeff Mahoney trans = btrfs_start_transaction(fs_info->tree_root, 0); 389579787eaaSJeff Mahoney if (IS_ERR(trans)) 389679787eaaSJeff Mahoney return PTR_ERR(trans); 389776dda93cSYan, Zheng 389876dda93cSYan, Zheng memset(&root->root_item.drop_progress, 0, 389976dda93cSYan, Zheng sizeof(root->root_item.drop_progress)); 3900c8422684SDavid Sterba btrfs_set_root_drop_level(&root->root_item, 0); 390176dda93cSYan, Zheng btrfs_set_root_refs(&root->root_item, 0); 39020b246afaSJeff Mahoney ret = btrfs_update_root(trans, fs_info->tree_root, 390376dda93cSYan, Zheng &root->root_key, &root->root_item); 390476dda93cSYan, Zheng 39053a45bb20SJeff Mahoney err = btrfs_end_transaction(trans); 390679787eaaSJeff Mahoney if (err) 390779787eaaSJeff Mahoney return err; 390879787eaaSJeff Mahoney return ret; 390976dda93cSYan, Zheng } 391076dda93cSYan, Zheng 39115d4f98a2SYan Zheng /* 39125d4f98a2SYan Zheng * recover relocation interrupted by system crash. 39135d4f98a2SYan Zheng * 39145d4f98a2SYan Zheng * this function resumes merging reloc trees with corresponding fs trees. 39155d4f98a2SYan Zheng * this is important for keeping the sharing of tree blocks 39165d4f98a2SYan Zheng */ 39175d4f98a2SYan Zheng int btrfs_recover_relocation(struct btrfs_root *root) 39185d4f98a2SYan Zheng { 39190b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 39205d4f98a2SYan Zheng LIST_HEAD(reloc_roots); 39215d4f98a2SYan Zheng struct btrfs_key key; 39225d4f98a2SYan Zheng struct btrfs_root *fs_root; 39235d4f98a2SYan Zheng struct btrfs_root *reloc_root; 39245d4f98a2SYan Zheng struct btrfs_path *path; 39255d4f98a2SYan Zheng struct extent_buffer *leaf; 39265d4f98a2SYan Zheng struct reloc_control *rc = NULL; 39275d4f98a2SYan Zheng struct btrfs_trans_handle *trans; 39285d4f98a2SYan Zheng int ret; 39295d4f98a2SYan Zheng int err = 0; 39305d4f98a2SYan Zheng 39315d4f98a2SYan Zheng path = btrfs_alloc_path(); 39325d4f98a2SYan Zheng if (!path) 39335d4f98a2SYan Zheng return -ENOMEM; 3934e4058b54SDavid Sterba path->reada = READA_BACK; 39355d4f98a2SYan Zheng 39365d4f98a2SYan Zheng key.objectid = BTRFS_TREE_RELOC_OBJECTID; 39375d4f98a2SYan Zheng key.type = BTRFS_ROOT_ITEM_KEY; 39385d4f98a2SYan Zheng key.offset = (u64)-1; 39395d4f98a2SYan Zheng 39405d4f98a2SYan Zheng while (1) { 39410b246afaSJeff Mahoney ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, 39425d4f98a2SYan Zheng path, 0, 0); 39435d4f98a2SYan Zheng if (ret < 0) { 39445d4f98a2SYan Zheng err = ret; 39455d4f98a2SYan Zheng goto out; 39465d4f98a2SYan Zheng } 39475d4f98a2SYan Zheng if (ret > 0) { 39485d4f98a2SYan Zheng if (path->slots[0] == 0) 39495d4f98a2SYan Zheng break; 39505d4f98a2SYan Zheng path->slots[0]--; 39515d4f98a2SYan Zheng } 39525d4f98a2SYan Zheng leaf = path->nodes[0]; 39535d4f98a2SYan Zheng btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 3954b3b4aa74SDavid Sterba btrfs_release_path(path); 39555d4f98a2SYan Zheng 39565d4f98a2SYan Zheng if (key.objectid != BTRFS_TREE_RELOC_OBJECTID || 39575d4f98a2SYan Zheng key.type != BTRFS_ROOT_ITEM_KEY) 39585d4f98a2SYan Zheng break; 39595d4f98a2SYan Zheng 39603dbf1738SJosef Bacik reloc_root = btrfs_read_tree_root(root, &key); 39615d4f98a2SYan Zheng if (IS_ERR(reloc_root)) { 39625d4f98a2SYan Zheng err = PTR_ERR(reloc_root); 39635d4f98a2SYan Zheng goto out; 39645d4f98a2SYan Zheng } 39655d4f98a2SYan Zheng 396692a7cc42SQu Wenruo set_bit(BTRFS_ROOT_SHAREABLE, &reloc_root->state); 39675d4f98a2SYan Zheng list_add(&reloc_root->root_list, &reloc_roots); 39685d4f98a2SYan Zheng 39695d4f98a2SYan Zheng if (btrfs_root_refs(&reloc_root->root_item) > 0) { 3970a820feb5SDavid Sterba fs_root = btrfs_get_fs_root(fs_info, 3971a820feb5SDavid Sterba reloc_root->root_key.offset, false); 39725d4f98a2SYan Zheng if (IS_ERR(fs_root)) { 397376dda93cSYan, Zheng ret = PTR_ERR(fs_root); 397476dda93cSYan, Zheng if (ret != -ENOENT) { 397576dda93cSYan, Zheng err = ret; 39765d4f98a2SYan Zheng goto out; 39775d4f98a2SYan Zheng } 397879787eaaSJeff Mahoney ret = mark_garbage_root(reloc_root); 397979787eaaSJeff Mahoney if (ret < 0) { 398079787eaaSJeff Mahoney err = ret; 398179787eaaSJeff Mahoney goto out; 398279787eaaSJeff Mahoney } 3983932fd26dSJosef Bacik } else { 398400246528SJosef Bacik btrfs_put_root(fs_root); 398576dda93cSYan, Zheng } 39865d4f98a2SYan Zheng } 39875d4f98a2SYan Zheng 39885d4f98a2SYan Zheng if (key.offset == 0) 39895d4f98a2SYan Zheng break; 39905d4f98a2SYan Zheng 39915d4f98a2SYan Zheng key.offset--; 39925d4f98a2SYan Zheng } 3993b3b4aa74SDavid Sterba btrfs_release_path(path); 39945d4f98a2SYan Zheng 39955d4f98a2SYan Zheng if (list_empty(&reloc_roots)) 39965d4f98a2SYan Zheng goto out; 39975d4f98a2SYan Zheng 3998c258d6e3SQu Wenruo rc = alloc_reloc_control(fs_info); 39995d4f98a2SYan Zheng if (!rc) { 40005d4f98a2SYan Zheng err = -ENOMEM; 40015d4f98a2SYan Zheng goto out; 40025d4f98a2SYan Zheng } 40035d4f98a2SYan Zheng 40040b246afaSJeff Mahoney rc->extent_root = fs_info->extent_root; 40055d4f98a2SYan Zheng 40065d4f98a2SYan Zheng set_reloc_control(rc); 40075d4f98a2SYan Zheng 40087a7eaa40SJosef Bacik trans = btrfs_join_transaction(rc->extent_root); 40093612b495STsutomu Itoh if (IS_ERR(trans)) { 40103612b495STsutomu Itoh err = PTR_ERR(trans); 4011fb2d83eeSJosef Bacik goto out_unset; 40123612b495STsutomu Itoh } 40133fd0a558SYan, Zheng 40143fd0a558SYan, Zheng rc->merge_reloc_tree = 1; 40153fd0a558SYan, Zheng 40165d4f98a2SYan Zheng while (!list_empty(&reloc_roots)) { 40175d4f98a2SYan Zheng reloc_root = list_entry(reloc_roots.next, 40185d4f98a2SYan Zheng struct btrfs_root, root_list); 40195d4f98a2SYan Zheng list_del(&reloc_root->root_list); 40205d4f98a2SYan Zheng 40215d4f98a2SYan Zheng if (btrfs_root_refs(&reloc_root->root_item) == 0) { 40225d4f98a2SYan Zheng list_add_tail(&reloc_root->root_list, 40235d4f98a2SYan Zheng &rc->reloc_roots); 40245d4f98a2SYan Zheng continue; 40255d4f98a2SYan Zheng } 40265d4f98a2SYan Zheng 4027a820feb5SDavid Sterba fs_root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset, 4028a820feb5SDavid Sterba false); 402979787eaaSJeff Mahoney if (IS_ERR(fs_root)) { 403079787eaaSJeff Mahoney err = PTR_ERR(fs_root); 4031ca1aa281SJosef Bacik list_add_tail(&reloc_root->root_list, &reloc_roots); 40321402d17dSXiyu Yang btrfs_end_transaction(trans); 4033fb2d83eeSJosef Bacik goto out_unset; 403479787eaaSJeff Mahoney } 40355d4f98a2SYan Zheng 4036ffd7b339SJeff Mahoney err = __add_reloc_root(reloc_root); 403779787eaaSJeff Mahoney BUG_ON(err < 0); /* -ENOMEM or logic error */ 4038f44deb74SJosef Bacik fs_root->reloc_root = btrfs_grab_root(reloc_root); 403900246528SJosef Bacik btrfs_put_root(fs_root); 40405d4f98a2SYan Zheng } 40415d4f98a2SYan Zheng 40423a45bb20SJeff Mahoney err = btrfs_commit_transaction(trans); 404379787eaaSJeff Mahoney if (err) 4044fb2d83eeSJosef Bacik goto out_unset; 40455d4f98a2SYan Zheng 40465d4f98a2SYan Zheng merge_reloc_roots(rc); 40475d4f98a2SYan Zheng 40485d4f98a2SYan Zheng unset_reloc_control(rc); 40495d4f98a2SYan Zheng 40507a7eaa40SJosef Bacik trans = btrfs_join_transaction(rc->extent_root); 405162b99540SQu Wenruo if (IS_ERR(trans)) { 40523612b495STsutomu Itoh err = PTR_ERR(trans); 40536217b0faSJosef Bacik goto out_clean; 405462b99540SQu Wenruo } 40553a45bb20SJeff Mahoney err = btrfs_commit_transaction(trans); 40566217b0faSJosef Bacik out_clean: 4057d2311e69SQu Wenruo ret = clean_dirty_subvols(rc); 4058d2311e69SQu Wenruo if (ret < 0 && !err) 4059d2311e69SQu Wenruo err = ret; 4060fb2d83eeSJosef Bacik out_unset: 4061fb2d83eeSJosef Bacik unset_reloc_control(rc); 40621a0afa0eSJosef Bacik free_reloc_control(rc); 40633612b495STsutomu Itoh out: 4064aca1bba6SLiu Bo free_reloc_roots(&reloc_roots); 4065aca1bba6SLiu Bo 40665d4f98a2SYan Zheng btrfs_free_path(path); 40675d4f98a2SYan Zheng 40685d4f98a2SYan Zheng if (err == 0) { 40695d4f98a2SYan Zheng /* cleanup orphan inode in data relocation tree */ 4070aeb935a4SQu Wenruo fs_root = btrfs_grab_root(fs_info->data_reloc_root); 4071aeb935a4SQu Wenruo ASSERT(fs_root); 407266b4ffd1SJosef Bacik err = btrfs_orphan_cleanup(fs_root); 407300246528SJosef Bacik btrfs_put_root(fs_root); 4074932fd26dSJosef Bacik } 40755d4f98a2SYan Zheng return err; 40765d4f98a2SYan Zheng } 40775d4f98a2SYan Zheng 40785d4f98a2SYan Zheng /* 40795d4f98a2SYan Zheng * helper to add ordered checksum for data relocation. 40805d4f98a2SYan Zheng * 40815d4f98a2SYan Zheng * cloning checksum properly handles the nodatasum extents. 40825d4f98a2SYan Zheng * it also saves CPU time to re-calculate the checksum. 40835d4f98a2SYan Zheng */ 40847bfa9535SNikolay Borisov int btrfs_reloc_clone_csums(struct btrfs_inode *inode, u64 file_pos, u64 len) 40855d4f98a2SYan Zheng { 40867bfa9535SNikolay Borisov struct btrfs_fs_info *fs_info = inode->root->fs_info; 40875d4f98a2SYan Zheng struct btrfs_ordered_sum *sums; 40885d4f98a2SYan Zheng struct btrfs_ordered_extent *ordered; 40895d4f98a2SYan Zheng int ret; 40905d4f98a2SYan Zheng u64 disk_bytenr; 40914577b014SJosef Bacik u64 new_bytenr; 40925d4f98a2SYan Zheng LIST_HEAD(list); 40935d4f98a2SYan Zheng 40947bfa9535SNikolay Borisov ordered = btrfs_lookup_ordered_extent(inode, file_pos); 4095bffe633eSOmar Sandoval BUG_ON(ordered->file_offset != file_pos || ordered->num_bytes != len); 40965d4f98a2SYan Zheng 40977bfa9535SNikolay Borisov disk_bytenr = file_pos + inode->index_cnt; 40980b246afaSJeff Mahoney ret = btrfs_lookup_csums_range(fs_info->csum_root, disk_bytenr, 4099a2de733cSArne Jansen disk_bytenr + len - 1, &list, 0); 410079787eaaSJeff Mahoney if (ret) 410179787eaaSJeff Mahoney goto out; 41025d4f98a2SYan Zheng 41035d4f98a2SYan Zheng while (!list_empty(&list)) { 41045d4f98a2SYan Zheng sums = list_entry(list.next, struct btrfs_ordered_sum, list); 41055d4f98a2SYan Zheng list_del_init(&sums->list); 41065d4f98a2SYan Zheng 41074577b014SJosef Bacik /* 41084577b014SJosef Bacik * We need to offset the new_bytenr based on where the csum is. 41094577b014SJosef Bacik * We need to do this because we will read in entire prealloc 41104577b014SJosef Bacik * extents but we may have written to say the middle of the 41114577b014SJosef Bacik * prealloc extent, so we need to make sure the csum goes with 41124577b014SJosef Bacik * the right disk offset. 41134577b014SJosef Bacik * 41144577b014SJosef Bacik * We can do this because the data reloc inode refers strictly 41154577b014SJosef Bacik * to the on disk bytes, so we don't have to worry about 41164577b014SJosef Bacik * disk_len vs real len like with real inodes since it's all 41174577b014SJosef Bacik * disk length. 41184577b014SJosef Bacik */ 4119bffe633eSOmar Sandoval new_bytenr = ordered->disk_bytenr + sums->bytenr - disk_bytenr; 41204577b014SJosef Bacik sums->bytenr = new_bytenr; 41215d4f98a2SYan Zheng 4122f9756261SNikolay Borisov btrfs_add_ordered_sum(ordered, sums); 41235d4f98a2SYan Zheng } 412479787eaaSJeff Mahoney out: 41255d4f98a2SYan Zheng btrfs_put_ordered_extent(ordered); 4126411fc6bcSAndi Kleen return ret; 41275d4f98a2SYan Zheng } 41283fd0a558SYan, Zheng 412983d4cfd4SJosef Bacik int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans, 41303fd0a558SYan, Zheng struct btrfs_root *root, struct extent_buffer *buf, 41313fd0a558SYan, Zheng struct extent_buffer *cow) 41323fd0a558SYan, Zheng { 41330b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = root->fs_info; 41343fd0a558SYan, Zheng struct reloc_control *rc; 4135a26195a5SQu Wenruo struct btrfs_backref_node *node; 41363fd0a558SYan, Zheng int first_cow = 0; 41373fd0a558SYan, Zheng int level; 413883d4cfd4SJosef Bacik int ret = 0; 41393fd0a558SYan, Zheng 41400b246afaSJeff Mahoney rc = fs_info->reloc_ctl; 41413fd0a558SYan, Zheng if (!rc) 414283d4cfd4SJosef Bacik return 0; 41433fd0a558SYan, Zheng 41443fd0a558SYan, Zheng BUG_ON(rc->stage == UPDATE_DATA_PTRS && 41453fd0a558SYan, Zheng root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID); 41463fd0a558SYan, Zheng 41473fd0a558SYan, Zheng level = btrfs_header_level(buf); 41483fd0a558SYan, Zheng if (btrfs_header_generation(buf) <= 41493fd0a558SYan, Zheng btrfs_root_last_snapshot(&root->root_item)) 41503fd0a558SYan, Zheng first_cow = 1; 41513fd0a558SYan, Zheng 41523fd0a558SYan, Zheng if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID && 41533fd0a558SYan, Zheng rc->create_reloc_tree) { 41543fd0a558SYan, Zheng WARN_ON(!first_cow && level == 0); 41553fd0a558SYan, Zheng 41563fd0a558SYan, Zheng node = rc->backref_cache.path[level]; 41573fd0a558SYan, Zheng BUG_ON(node->bytenr != buf->start && 41583fd0a558SYan, Zheng node->new_bytenr != buf->start); 41593fd0a558SYan, Zheng 4160b0fe7078SQu Wenruo btrfs_backref_drop_node_buffer(node); 416167439dadSDavid Sterba atomic_inc(&cow->refs); 41623fd0a558SYan, Zheng node->eb = cow; 41633fd0a558SYan, Zheng node->new_bytenr = cow->start; 41643fd0a558SYan, Zheng 41653fd0a558SYan, Zheng if (!node->pending) { 41663fd0a558SYan, Zheng list_move_tail(&node->list, 41673fd0a558SYan, Zheng &rc->backref_cache.pending[level]); 41683fd0a558SYan, Zheng node->pending = 1; 41693fd0a558SYan, Zheng } 41703fd0a558SYan, Zheng 41713fd0a558SYan, Zheng if (first_cow) 41729569cc20SQu Wenruo mark_block_processed(rc, node); 41733fd0a558SYan, Zheng 41743fd0a558SYan, Zheng if (first_cow && level > 0) 41753fd0a558SYan, Zheng rc->nodes_relocated += buf->len; 41763fd0a558SYan, Zheng } 41773fd0a558SYan, Zheng 417883d4cfd4SJosef Bacik if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS) 41793fd0a558SYan, Zheng ret = replace_file_extents(trans, rc, root, cow); 418083d4cfd4SJosef Bacik return ret; 41813fd0a558SYan, Zheng } 41823fd0a558SYan, Zheng 41833fd0a558SYan, Zheng /* 41843fd0a558SYan, Zheng * called before creating snapshot. it calculates metadata reservation 418501327610SNicholas D Steeves * required for relocating tree blocks in the snapshot 41863fd0a558SYan, Zheng */ 4187147d256eSZhaolei void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending, 41883fd0a558SYan, Zheng u64 *bytes_to_reserve) 41893fd0a558SYan, Zheng { 419010995c04SQu Wenruo struct btrfs_root *root = pending->root; 419110995c04SQu Wenruo struct reloc_control *rc = root->fs_info->reloc_ctl; 41923fd0a558SYan, Zheng 41936282675eSQu Wenruo if (!rc || !have_reloc_root(root)) 41943fd0a558SYan, Zheng return; 41953fd0a558SYan, Zheng 41963fd0a558SYan, Zheng if (!rc->merge_reloc_tree) 41973fd0a558SYan, Zheng return; 41983fd0a558SYan, Zheng 41993fd0a558SYan, Zheng root = root->reloc_root; 42003fd0a558SYan, Zheng BUG_ON(btrfs_root_refs(&root->root_item) == 0); 42013fd0a558SYan, Zheng /* 42023fd0a558SYan, Zheng * relocation is in the stage of merging trees. the space 42033fd0a558SYan, Zheng * used by merging a reloc tree is twice the size of 42043fd0a558SYan, Zheng * relocated tree nodes in the worst case. half for cowing 42053fd0a558SYan, Zheng * the reloc tree, half for cowing the fs tree. the space 42063fd0a558SYan, Zheng * used by cowing the reloc tree will be freed after the 42073fd0a558SYan, Zheng * tree is dropped. if we create snapshot, cowing the fs 42083fd0a558SYan, Zheng * tree may use more space than it frees. so we need 42093fd0a558SYan, Zheng * reserve extra space. 42103fd0a558SYan, Zheng */ 42113fd0a558SYan, Zheng *bytes_to_reserve += rc->nodes_relocated; 42123fd0a558SYan, Zheng } 42133fd0a558SYan, Zheng 42143fd0a558SYan, Zheng /* 42153fd0a558SYan, Zheng * called after snapshot is created. migrate block reservation 42163fd0a558SYan, Zheng * and create reloc root for the newly created snapshot 4217f44deb74SJosef Bacik * 4218f44deb74SJosef Bacik * This is similar to btrfs_init_reloc_root(), we come out of here with two 4219f44deb74SJosef Bacik * references held on the reloc_root, one for root->reloc_root and one for 4220f44deb74SJosef Bacik * rc->reloc_roots. 42213fd0a558SYan, Zheng */ 422249b25e05SJeff Mahoney int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans, 42233fd0a558SYan, Zheng struct btrfs_pending_snapshot *pending) 42243fd0a558SYan, Zheng { 42253fd0a558SYan, Zheng struct btrfs_root *root = pending->root; 42263fd0a558SYan, Zheng struct btrfs_root *reloc_root; 42273fd0a558SYan, Zheng struct btrfs_root *new_root; 422810995c04SQu Wenruo struct reloc_control *rc = root->fs_info->reloc_ctl; 42293fd0a558SYan, Zheng int ret; 42303fd0a558SYan, Zheng 42316282675eSQu Wenruo if (!rc || !have_reloc_root(root)) 423249b25e05SJeff Mahoney return 0; 42333fd0a558SYan, Zheng 42343fd0a558SYan, Zheng rc = root->fs_info->reloc_ctl; 42353fd0a558SYan, Zheng rc->merging_rsv_size += rc->nodes_relocated; 42363fd0a558SYan, Zheng 42373fd0a558SYan, Zheng if (rc->merge_reloc_tree) { 42383fd0a558SYan, Zheng ret = btrfs_block_rsv_migrate(&pending->block_rsv, 42393fd0a558SYan, Zheng rc->block_rsv, 42403a584174SLu Fengqi rc->nodes_relocated, true); 424149b25e05SJeff Mahoney if (ret) 424249b25e05SJeff Mahoney return ret; 42433fd0a558SYan, Zheng } 42443fd0a558SYan, Zheng 42453fd0a558SYan, Zheng new_root = pending->snap; 42463fd0a558SYan, Zheng reloc_root = create_reloc_root(trans, root->reloc_root, 42473fd0a558SYan, Zheng new_root->root_key.objectid); 424849b25e05SJeff Mahoney if (IS_ERR(reloc_root)) 424949b25e05SJeff Mahoney return PTR_ERR(reloc_root); 42503fd0a558SYan, Zheng 4251ffd7b339SJeff Mahoney ret = __add_reloc_root(reloc_root); 4252ffd7b339SJeff Mahoney BUG_ON(ret < 0); 4253f44deb74SJosef Bacik new_root->reloc_root = btrfs_grab_root(reloc_root); 42543fd0a558SYan, Zheng 425549b25e05SJeff Mahoney if (rc->create_reloc_tree) 42563fd0a558SYan, Zheng ret = clone_backref_node(trans, rc, root, reloc_root); 425749b25e05SJeff Mahoney return ret; 42583fd0a558SYan, Zheng } 4259