1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2009 Oracle. All rights reserved.
6 #include <linux/sched.h>
7 #include <linux/pagemap.h>
8 #include <linux/writeback.h>
9 #include <linux/blkdev.h>
10 #include <linux/rbtree.h>
11 #include <linux/slab.h>
12 #include <linux/error-injection.h>
15 #include "transaction.h"
18 #include "btrfs_inode.h"
19 #include "async-thread.h"
20 #include "free-space-cache.h"
22 #include "print-tree.h"
23 #include "delalloc-space.h"
24 #include "block-group.h"
29 #include "inode-item.h"
34 * [What does relocation do]
36 * The objective of relocation is to relocate all extents of the target block
37 * group to other block groups.
38 * This is utilized by resize (shrink only), profile converting, compacting
39 * space, or balance routine to spread chunks over devices.
42 * ------------------------------------------------------------------
43 * BG A: 10 data extents | BG A: deleted
44 * BG B: 2 data extents | BG B: 10 data extents (2 old + 8 relocated)
45 * BG C: 1 extents | BG C: 3 data extents (1 old + 2 relocated)
47 * [How does relocation work]
49 * 1. Mark the target block group read-only
50 * New extents won't be allocated from the target block group.
52 * 2.1 Record each extent in the target block group
53 * To build a proper map of extents to be relocated.
55 * 2.2 Build data reloc tree and reloc trees
56 * Data reloc tree will contain an inode, recording all newly relocated
58 * There will be only one data reloc tree for one data block group.
60 * Reloc tree will be a special snapshot of its source tree, containing
61 * relocated tree blocks.
62 * Each tree referring to a tree block in target block group will get its
65 * 2.3 Swap source tree with its corresponding reloc tree
66 * Each involved tree only refers to new extents after swap.
68 * 3. Cleanup reloc trees and data reloc tree.
69 * As old extents in the target block group are still referenced by reloc
70 * trees, we need to clean them up before really freeing the target block
73 * The main complexity is in steps 2.2 and 2.3.
75 * The entry point of relocation is relocate_block_group() function.
78 #define RELOCATION_RESERVED_NODES 256
80 * map address of tree root to tree
84 struct rb_node rb_node;
86 }; /* Use rb_simle_node for search/insert */
91 struct rb_root rb_root;
96 * present a tree block to process
100 struct rb_node rb_node;
102 }; /* Use rb_simple_node for search/insert */
104 struct btrfs_key key;
105 unsigned int level:8;
106 unsigned int key_ready:1;
109 #define MAX_EXTENTS 128
111 struct file_extent_cluster {
114 u64 boundary[MAX_EXTENTS];
118 struct reloc_control {
119 /* block group to relocate */
120 struct btrfs_block_group *block_group;
122 struct btrfs_root *extent_root;
123 /* inode for moving data */
124 struct inode *data_inode;
126 struct btrfs_block_rsv *block_rsv;
128 struct btrfs_backref_cache backref_cache;
130 struct file_extent_cluster cluster;
131 /* tree blocks have been processed */
132 struct extent_io_tree processed_blocks;
133 /* map start of tree root to corresponding reloc tree */
134 struct mapping_tree reloc_root_tree;
135 /* list of reloc trees */
136 struct list_head reloc_roots;
137 /* list of subvolume trees that get relocated */
138 struct list_head dirty_subvol_roots;
139 /* size of metadata reservation for merging reloc trees */
140 u64 merging_rsv_size;
141 /* size of relocated tree nodes */
143 /* reserved size for block group relocation*/
149 unsigned int stage:8;
150 unsigned int create_reloc_tree:1;
151 unsigned int merge_reloc_tree:1;
152 unsigned int found_file_extent:1;
155 /* stages of data relocation */
156 #define MOVE_DATA_EXTENTS 0
157 #define UPDATE_DATA_PTRS 1
159 static void mark_block_processed(struct reloc_control *rc,
160 struct btrfs_backref_node *node)
164 if (node->level == 0 ||
165 in_range(node->bytenr, rc->block_group->start,
166 rc->block_group->length)) {
167 blocksize = rc->extent_root->fs_info->nodesize;
168 set_extent_bits(&rc->processed_blocks, node->bytenr,
169 node->bytenr + blocksize - 1, EXTENT_DIRTY);
175 static void mapping_tree_init(struct mapping_tree *tree)
177 tree->rb_root = RB_ROOT;
178 spin_lock_init(&tree->lock);
182 * walk up backref nodes until reach node presents tree root
184 static struct btrfs_backref_node *walk_up_backref(
185 struct btrfs_backref_node *node,
186 struct btrfs_backref_edge *edges[], int *index)
188 struct btrfs_backref_edge *edge;
191 while (!list_empty(&node->upper)) {
192 edge = list_entry(node->upper.next,
193 struct btrfs_backref_edge, list[LOWER]);
195 node = edge->node[UPPER];
197 BUG_ON(node->detached);
203 * walk down backref nodes to find start of next reference path
205 static struct btrfs_backref_node *walk_down_backref(
206 struct btrfs_backref_edge *edges[], int *index)
208 struct btrfs_backref_edge *edge;
209 struct btrfs_backref_node *lower;
213 edge = edges[idx - 1];
214 lower = edge->node[LOWER];
215 if (list_is_last(&edge->list[LOWER], &lower->upper)) {
219 edge = list_entry(edge->list[LOWER].next,
220 struct btrfs_backref_edge, list[LOWER]);
221 edges[idx - 1] = edge;
223 return edge->node[UPPER];
229 static void update_backref_node(struct btrfs_backref_cache *cache,
230 struct btrfs_backref_node *node, u64 bytenr)
232 struct rb_node *rb_node;
233 rb_erase(&node->rb_node, &cache->rb_root);
234 node->bytenr = bytenr;
235 rb_node = rb_simple_insert(&cache->rb_root, node->bytenr, &node->rb_node);
237 btrfs_backref_panic(cache->fs_info, bytenr, -EEXIST);
241 * update backref cache after a transaction commit
243 static int update_backref_cache(struct btrfs_trans_handle *trans,
244 struct btrfs_backref_cache *cache)
246 struct btrfs_backref_node *node;
249 if (cache->last_trans == 0) {
250 cache->last_trans = trans->transid;
254 if (cache->last_trans == trans->transid)
258 * detached nodes are used to avoid unnecessary backref
259 * lookup. transaction commit changes the extent tree.
260 * so the detached nodes are no longer useful.
262 while (!list_empty(&cache->detached)) {
263 node = list_entry(cache->detached.next,
264 struct btrfs_backref_node, list);
265 btrfs_backref_cleanup_node(cache, node);
268 while (!list_empty(&cache->changed)) {
269 node = list_entry(cache->changed.next,
270 struct btrfs_backref_node, list);
271 list_del_init(&node->list);
272 BUG_ON(node->pending);
273 update_backref_node(cache, node, node->new_bytenr);
277 * some nodes can be left in the pending list if there were
278 * errors during processing the pending nodes.
280 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
281 list_for_each_entry(node, &cache->pending[level], list) {
282 BUG_ON(!node->pending);
283 if (node->bytenr == node->new_bytenr)
285 update_backref_node(cache, node, node->new_bytenr);
289 cache->last_trans = 0;
293 static bool reloc_root_is_dead(struct btrfs_root *root)
296 * Pair with set_bit/clear_bit in clean_dirty_subvols and
297 * btrfs_update_reloc_root. We need to see the updated bit before
298 * trying to access reloc_root
301 if (test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state))
307 * Check if this subvolume tree has valid reloc tree.
309 * Reloc tree after swap is considered dead, thus not considered as valid.
310 * This is enough for most callers, as they don't distinguish dead reloc root
311 * from no reloc root. But btrfs_should_ignore_reloc_root() below is a
314 static bool have_reloc_root(struct btrfs_root *root)
316 if (reloc_root_is_dead(root))
318 if (!root->reloc_root)
323 int btrfs_should_ignore_reloc_root(struct btrfs_root *root)
325 struct btrfs_root *reloc_root;
327 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
330 /* This root has been merged with its reloc tree, we can ignore it */
331 if (reloc_root_is_dead(root))
334 reloc_root = root->reloc_root;
338 if (btrfs_header_generation(reloc_root->commit_root) ==
339 root->fs_info->running_transaction->transid)
342 * if there is reloc tree and it was created in previous
343 * transaction backref lookup can find the reloc tree,
344 * so backref node for the fs tree root is useless for
351 * find reloc tree by address of tree root
353 struct btrfs_root *find_reloc_root(struct btrfs_fs_info *fs_info, u64 bytenr)
355 struct reloc_control *rc = fs_info->reloc_ctl;
356 struct rb_node *rb_node;
357 struct mapping_node *node;
358 struct btrfs_root *root = NULL;
361 spin_lock(&rc->reloc_root_tree.lock);
362 rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root, bytenr);
364 node = rb_entry(rb_node, struct mapping_node, rb_node);
367 spin_unlock(&rc->reloc_root_tree.lock);
368 return btrfs_grab_root(root);
372 * For useless nodes, do two major clean ups:
374 * - Cleanup the children edges and nodes
375 * If child node is also orphan (no parent) during cleanup, then the child
376 * node will also be cleaned up.
378 * - Freeing up leaves (level 0), keeps nodes detached
379 * For nodes, the node is still cached as "detached"
381 * Return false if @node is not in the @useless_nodes list.
382 * Return true if @node is in the @useless_nodes list.
384 static bool handle_useless_nodes(struct reloc_control *rc,
385 struct btrfs_backref_node *node)
387 struct btrfs_backref_cache *cache = &rc->backref_cache;
388 struct list_head *useless_node = &cache->useless_node;
391 while (!list_empty(useless_node)) {
392 struct btrfs_backref_node *cur;
394 cur = list_first_entry(useless_node, struct btrfs_backref_node,
396 list_del_init(&cur->list);
398 /* Only tree root nodes can be added to @useless_nodes */
399 ASSERT(list_empty(&cur->upper));
404 /* The node is the lowest node */
406 list_del_init(&cur->lower);
410 /* Cleanup the lower edges */
411 while (!list_empty(&cur->lower)) {
412 struct btrfs_backref_edge *edge;
413 struct btrfs_backref_node *lower;
415 edge = list_entry(cur->lower.next,
416 struct btrfs_backref_edge, list[UPPER]);
417 list_del(&edge->list[UPPER]);
418 list_del(&edge->list[LOWER]);
419 lower = edge->node[LOWER];
420 btrfs_backref_free_edge(cache, edge);
422 /* Child node is also orphan, queue for cleanup */
423 if (list_empty(&lower->upper))
424 list_add(&lower->list, useless_node);
426 /* Mark this block processed for relocation */
427 mark_block_processed(rc, cur);
430 * Backref nodes for tree leaves are deleted from the cache.
431 * Backref nodes for upper level tree blocks are left in the
432 * cache to avoid unnecessary backref lookup.
434 if (cur->level > 0) {
435 list_add(&cur->list, &cache->detached);
438 rb_erase(&cur->rb_node, &cache->rb_root);
439 btrfs_backref_free_node(cache, cur);
446 * Build backref tree for a given tree block. Root of the backref tree
447 * corresponds the tree block, leaves of the backref tree correspond roots of
448 * b-trees that reference the tree block.
450 * The basic idea of this function is check backrefs of a given block to find
451 * upper level blocks that reference the block, and then check backrefs of
452 * these upper level blocks recursively. The recursion stops when tree root is
453 * reached or backrefs for the block is cached.
455 * NOTE: if we find that backrefs for a block are cached, we know backrefs for
456 * all upper level blocks that directly/indirectly reference the block are also
459 static noinline_for_stack struct btrfs_backref_node *build_backref_tree(
460 struct reloc_control *rc, struct btrfs_key *node_key,
461 int level, u64 bytenr)
463 struct btrfs_backref_iter *iter;
464 struct btrfs_backref_cache *cache = &rc->backref_cache;
465 /* For searching parent of TREE_BLOCK_REF */
466 struct btrfs_path *path;
467 struct btrfs_backref_node *cur;
468 struct btrfs_backref_node *node = NULL;
469 struct btrfs_backref_edge *edge;
473 iter = btrfs_backref_iter_alloc(rc->extent_root->fs_info, GFP_NOFS);
475 return ERR_PTR(-ENOMEM);
476 path = btrfs_alloc_path();
482 node = btrfs_backref_alloc_node(cache, bytenr, level);
491 /* Breadth-first search to build backref cache */
493 ret = btrfs_backref_add_tree_node(cache, path, iter, node_key,
499 edge = list_first_entry_or_null(&cache->pending_edge,
500 struct btrfs_backref_edge, list[UPPER]);
502 * The pending list isn't empty, take the first block to
506 list_del_init(&edge->list[UPPER]);
507 cur = edge->node[UPPER];
511 /* Finish the upper linkage of newly added edges/nodes */
512 ret = btrfs_backref_finish_upper_links(cache, node);
518 if (handle_useless_nodes(rc, node))
521 btrfs_backref_iter_free(iter);
522 btrfs_free_path(path);
524 btrfs_backref_error_cleanup(cache, node);
527 ASSERT(!node || !node->detached);
528 ASSERT(list_empty(&cache->useless_node) &&
529 list_empty(&cache->pending_edge));
534 * helper to add backref node for the newly created snapshot.
535 * the backref node is created by cloning backref node that
536 * corresponds to root of source tree
538 static int clone_backref_node(struct btrfs_trans_handle *trans,
539 struct reloc_control *rc,
540 struct btrfs_root *src,
541 struct btrfs_root *dest)
543 struct btrfs_root *reloc_root = src->reloc_root;
544 struct btrfs_backref_cache *cache = &rc->backref_cache;
545 struct btrfs_backref_node *node = NULL;
546 struct btrfs_backref_node *new_node;
547 struct btrfs_backref_edge *edge;
548 struct btrfs_backref_edge *new_edge;
549 struct rb_node *rb_node;
551 if (cache->last_trans > 0)
552 update_backref_cache(trans, cache);
554 rb_node = rb_simple_search(&cache->rb_root, src->commit_root->start);
556 node = rb_entry(rb_node, struct btrfs_backref_node, rb_node);
560 BUG_ON(node->new_bytenr != reloc_root->node->start);
564 rb_node = rb_simple_search(&cache->rb_root,
565 reloc_root->commit_root->start);
567 node = rb_entry(rb_node, struct btrfs_backref_node,
569 BUG_ON(node->detached);
576 new_node = btrfs_backref_alloc_node(cache, dest->node->start,
581 new_node->lowest = node->lowest;
582 new_node->checked = 1;
583 new_node->root = btrfs_grab_root(dest);
584 ASSERT(new_node->root);
587 list_for_each_entry(edge, &node->lower, list[UPPER]) {
588 new_edge = btrfs_backref_alloc_edge(cache);
592 btrfs_backref_link_edge(new_edge, edge->node[LOWER],
593 new_node, LINK_UPPER);
596 list_add_tail(&new_node->lower, &cache->leaves);
599 rb_node = rb_simple_insert(&cache->rb_root, new_node->bytenr,
602 btrfs_backref_panic(trans->fs_info, new_node->bytenr, -EEXIST);
604 if (!new_node->lowest) {
605 list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) {
606 list_add_tail(&new_edge->list[LOWER],
607 &new_edge->node[LOWER]->upper);
612 while (!list_empty(&new_node->lower)) {
613 new_edge = list_entry(new_node->lower.next,
614 struct btrfs_backref_edge, list[UPPER]);
615 list_del(&new_edge->list[UPPER]);
616 btrfs_backref_free_edge(cache, new_edge);
618 btrfs_backref_free_node(cache, new_node);
623 * helper to add 'address of tree root -> reloc tree' mapping
625 static int __must_check __add_reloc_root(struct btrfs_root *root)
627 struct btrfs_fs_info *fs_info = root->fs_info;
628 struct rb_node *rb_node;
629 struct mapping_node *node;
630 struct reloc_control *rc = fs_info->reloc_ctl;
632 node = kmalloc(sizeof(*node), GFP_NOFS);
636 node->bytenr = root->commit_root->start;
639 spin_lock(&rc->reloc_root_tree.lock);
640 rb_node = rb_simple_insert(&rc->reloc_root_tree.rb_root,
641 node->bytenr, &node->rb_node);
642 spin_unlock(&rc->reloc_root_tree.lock);
645 "Duplicate root found for start=%llu while inserting into relocation tree",
650 list_add_tail(&root->root_list, &rc->reloc_roots);
655 * helper to delete the 'address of tree root -> reloc tree'
658 static void __del_reloc_root(struct btrfs_root *root)
660 struct btrfs_fs_info *fs_info = root->fs_info;
661 struct rb_node *rb_node;
662 struct mapping_node *node = NULL;
663 struct reloc_control *rc = fs_info->reloc_ctl;
664 bool put_ref = false;
666 if (rc && root->node) {
667 spin_lock(&rc->reloc_root_tree.lock);
668 rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root,
669 root->commit_root->start);
671 node = rb_entry(rb_node, struct mapping_node, rb_node);
672 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
673 RB_CLEAR_NODE(&node->rb_node);
675 spin_unlock(&rc->reloc_root_tree.lock);
676 ASSERT(!node || (struct btrfs_root *)node->data == root);
680 * We only put the reloc root here if it's on the list. There's a lot
681 * of places where the pattern is to splice the rc->reloc_roots, process
682 * the reloc roots, and then add the reloc root back onto
683 * rc->reloc_roots. If we call __del_reloc_root while it's off of the
684 * list we don't want the reference being dropped, because the guy
685 * messing with the list is in charge of the reference.
687 spin_lock(&fs_info->trans_lock);
688 if (!list_empty(&root->root_list)) {
690 list_del_init(&root->root_list);
692 spin_unlock(&fs_info->trans_lock);
694 btrfs_put_root(root);
699 * helper to update the 'address of tree root -> reloc tree'
702 static int __update_reloc_root(struct btrfs_root *root)
704 struct btrfs_fs_info *fs_info = root->fs_info;
705 struct rb_node *rb_node;
706 struct mapping_node *node = NULL;
707 struct reloc_control *rc = fs_info->reloc_ctl;
709 spin_lock(&rc->reloc_root_tree.lock);
710 rb_node = rb_simple_search(&rc->reloc_root_tree.rb_root,
711 root->commit_root->start);
713 node = rb_entry(rb_node, struct mapping_node, rb_node);
714 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
716 spin_unlock(&rc->reloc_root_tree.lock);
720 BUG_ON((struct btrfs_root *)node->data != root);
722 spin_lock(&rc->reloc_root_tree.lock);
723 node->bytenr = root->node->start;
724 rb_node = rb_simple_insert(&rc->reloc_root_tree.rb_root,
725 node->bytenr, &node->rb_node);
726 spin_unlock(&rc->reloc_root_tree.lock);
728 btrfs_backref_panic(fs_info, node->bytenr, -EEXIST);
732 static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
733 struct btrfs_root *root, u64 objectid)
735 struct btrfs_fs_info *fs_info = root->fs_info;
736 struct btrfs_root *reloc_root;
737 struct extent_buffer *eb;
738 struct btrfs_root_item *root_item;
739 struct btrfs_key root_key;
741 bool must_abort = false;
743 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
745 return ERR_PTR(-ENOMEM);
747 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
748 root_key.type = BTRFS_ROOT_ITEM_KEY;
749 root_key.offset = objectid;
751 if (root->root_key.objectid == objectid) {
754 /* called by btrfs_init_reloc_root */
755 ret = btrfs_copy_root(trans, root, root->commit_root, &eb,
756 BTRFS_TREE_RELOC_OBJECTID);
761 * Set the last_snapshot field to the generation of the commit
762 * root - like this ctree.c:btrfs_block_can_be_shared() behaves
763 * correctly (returns true) when the relocation root is created
764 * either inside the critical section of a transaction commit
765 * (through transaction.c:qgroup_account_snapshot()) and when
766 * it's created before the transaction commit is started.
768 commit_root_gen = btrfs_header_generation(root->commit_root);
769 btrfs_set_root_last_snapshot(&root->root_item, commit_root_gen);
772 * called by btrfs_reloc_post_snapshot_hook.
773 * the source tree is a reloc tree, all tree blocks
774 * modified after it was created have RELOC flag
775 * set in their headers. so it's OK to not update
776 * the 'last_snapshot'.
778 ret = btrfs_copy_root(trans, root, root->node, &eb,
779 BTRFS_TREE_RELOC_OBJECTID);
785 * We have changed references at this point, we must abort the
786 * transaction if anything fails.
790 memcpy(root_item, &root->root_item, sizeof(*root_item));
791 btrfs_set_root_bytenr(root_item, eb->start);
792 btrfs_set_root_level(root_item, btrfs_header_level(eb));
793 btrfs_set_root_generation(root_item, trans->transid);
795 if (root->root_key.objectid == objectid) {
796 btrfs_set_root_refs(root_item, 0);
797 memset(&root_item->drop_progress, 0,
798 sizeof(struct btrfs_disk_key));
799 btrfs_set_root_drop_level(root_item, 0);
802 btrfs_tree_unlock(eb);
803 free_extent_buffer(eb);
805 ret = btrfs_insert_root(trans, fs_info->tree_root,
806 &root_key, root_item);
812 reloc_root = btrfs_read_tree_root(fs_info->tree_root, &root_key);
813 if (IS_ERR(reloc_root)) {
814 ret = PTR_ERR(reloc_root);
817 set_bit(BTRFS_ROOT_SHAREABLE, &reloc_root->state);
818 reloc_root->last_trans = trans->transid;
824 btrfs_abort_transaction(trans, ret);
829 * create reloc tree for a given fs tree. reloc tree is just a
830 * snapshot of the fs tree with special root objectid.
832 * The reloc_root comes out of here with two references, one for
833 * root->reloc_root, and another for being on the rc->reloc_roots list.
835 int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
836 struct btrfs_root *root)
838 struct btrfs_fs_info *fs_info = root->fs_info;
839 struct btrfs_root *reloc_root;
840 struct reloc_control *rc = fs_info->reloc_ctl;
841 struct btrfs_block_rsv *rsv;
849 * The subvolume has reloc tree but the swap is finished, no need to
850 * create/update the dead reloc tree
852 if (reloc_root_is_dead(root))
856 * This is subtle but important. We do not do
857 * record_root_in_transaction for reloc roots, instead we record their
858 * corresponding fs root, and then here we update the last trans for the
859 * reloc root. This means that we have to do this for the entire life
860 * of the reloc root, regardless of which stage of the relocation we are
863 if (root->reloc_root) {
864 reloc_root = root->reloc_root;
865 reloc_root->last_trans = trans->transid;
870 * We are merging reloc roots, we do not need new reloc trees. Also
871 * reloc trees never need their own reloc tree.
873 if (!rc->create_reloc_tree ||
874 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
877 if (!trans->reloc_reserved) {
878 rsv = trans->block_rsv;
879 trans->block_rsv = rc->block_rsv;
882 reloc_root = create_reloc_root(trans, root, root->root_key.objectid);
884 trans->block_rsv = rsv;
885 if (IS_ERR(reloc_root))
886 return PTR_ERR(reloc_root);
888 ret = __add_reloc_root(reloc_root);
889 ASSERT(ret != -EEXIST);
891 /* Pairs with create_reloc_root */
892 btrfs_put_root(reloc_root);
895 root->reloc_root = btrfs_grab_root(reloc_root);
900 * update root item of reloc tree
902 int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
903 struct btrfs_root *root)
905 struct btrfs_fs_info *fs_info = root->fs_info;
906 struct btrfs_root *reloc_root;
907 struct btrfs_root_item *root_item;
910 if (!have_reloc_root(root))
913 reloc_root = root->reloc_root;
914 root_item = &reloc_root->root_item;
917 * We are probably ok here, but __del_reloc_root() will drop its ref of
918 * the root. We have the ref for root->reloc_root, but just in case
919 * hold it while we update the reloc root.
921 btrfs_grab_root(reloc_root);
923 /* root->reloc_root will stay until current relocation finished */
924 if (fs_info->reloc_ctl->merge_reloc_tree &&
925 btrfs_root_refs(root_item) == 0) {
926 set_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
928 * Mark the tree as dead before we change reloc_root so
929 * have_reloc_root will not touch it from now on.
932 __del_reloc_root(reloc_root);
935 if (reloc_root->commit_root != reloc_root->node) {
936 __update_reloc_root(reloc_root);
937 btrfs_set_root_node(root_item, reloc_root->node);
938 free_extent_buffer(reloc_root->commit_root);
939 reloc_root->commit_root = btrfs_root_node(reloc_root);
942 ret = btrfs_update_root(trans, fs_info->tree_root,
943 &reloc_root->root_key, root_item);
944 btrfs_put_root(reloc_root);
949 * helper to find first cached inode with inode number >= objectid
952 static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid)
954 struct rb_node *node;
955 struct rb_node *prev;
956 struct btrfs_inode *entry;
959 spin_lock(&root->inode_lock);
961 node = root->inode_tree.rb_node;
965 entry = rb_entry(node, struct btrfs_inode, rb_node);
967 if (objectid < btrfs_ino(entry))
968 node = node->rb_left;
969 else if (objectid > btrfs_ino(entry))
970 node = node->rb_right;
976 entry = rb_entry(prev, struct btrfs_inode, rb_node);
977 if (objectid <= btrfs_ino(entry)) {
981 prev = rb_next(prev);
985 entry = rb_entry(node, struct btrfs_inode, rb_node);
986 inode = igrab(&entry->vfs_inode);
988 spin_unlock(&root->inode_lock);
992 objectid = btrfs_ino(entry) + 1;
993 if (cond_resched_lock(&root->inode_lock))
996 node = rb_next(node);
998 spin_unlock(&root->inode_lock);
1003 * get new location of data
1005 static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr,
1006 u64 bytenr, u64 num_bytes)
1008 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
1009 struct btrfs_path *path;
1010 struct btrfs_file_extent_item *fi;
1011 struct extent_buffer *leaf;
1014 path = btrfs_alloc_path();
1018 bytenr -= BTRFS_I(reloc_inode)->index_cnt;
1019 ret = btrfs_lookup_file_extent(NULL, root, path,
1020 btrfs_ino(BTRFS_I(reloc_inode)), bytenr, 0);
1028 leaf = path->nodes[0];
1029 fi = btrfs_item_ptr(leaf, path->slots[0],
1030 struct btrfs_file_extent_item);
1032 BUG_ON(btrfs_file_extent_offset(leaf, fi) ||
1033 btrfs_file_extent_compression(leaf, fi) ||
1034 btrfs_file_extent_encryption(leaf, fi) ||
1035 btrfs_file_extent_other_encoding(leaf, fi));
1037 if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
1042 *new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1045 btrfs_free_path(path);
1050 * update file extent items in the tree leaf to point to
1051 * the new locations.
1053 static noinline_for_stack
1054 int replace_file_extents(struct btrfs_trans_handle *trans,
1055 struct reloc_control *rc,
1056 struct btrfs_root *root,
1057 struct extent_buffer *leaf)
1059 struct btrfs_fs_info *fs_info = root->fs_info;
1060 struct btrfs_key key;
1061 struct btrfs_file_extent_item *fi;
1062 struct inode *inode = NULL;
1074 if (rc->stage != UPDATE_DATA_PTRS)
1077 /* reloc trees always use full backref */
1078 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1079 parent = leaf->start;
1083 nritems = btrfs_header_nritems(leaf);
1084 for (i = 0; i < nritems; i++) {
1085 struct btrfs_ref ref = { 0 };
1088 btrfs_item_key_to_cpu(leaf, &key, i);
1089 if (key.type != BTRFS_EXTENT_DATA_KEY)
1091 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1092 if (btrfs_file_extent_type(leaf, fi) ==
1093 BTRFS_FILE_EXTENT_INLINE)
1095 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1096 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1099 if (!in_range(bytenr, rc->block_group->start,
1100 rc->block_group->length))
1104 * if we are modifying block in fs tree, wait for read_folio
1105 * to complete and drop the extent cache
1107 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1109 inode = find_next_inode(root, key.objectid);
1111 } else if (inode && btrfs_ino(BTRFS_I(inode)) < key.objectid) {
1112 btrfs_add_delayed_iput(inode);
1113 inode = find_next_inode(root, key.objectid);
1115 if (inode && btrfs_ino(BTRFS_I(inode)) == key.objectid) {
1117 btrfs_file_extent_num_bytes(leaf, fi);
1118 WARN_ON(!IS_ALIGNED(key.offset,
1119 fs_info->sectorsize));
1120 WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize));
1122 ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
1127 btrfs_drop_extent_cache(BTRFS_I(inode),
1128 key.offset, end, 1);
1129 unlock_extent(&BTRFS_I(inode)->io_tree,
1134 ret = get_new_location(rc->data_inode, &new_bytenr,
1138 * Don't have to abort since we've not changed anything
1139 * in the file extent yet.
1144 btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr);
1147 key.offset -= btrfs_file_extent_offset(leaf, fi);
1148 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
1150 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
1151 key.objectid, key.offset,
1152 root->root_key.objectid, false);
1153 ret = btrfs_inc_extent_ref(trans, &ref);
1155 btrfs_abort_transaction(trans, ret);
1159 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
1161 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
1162 key.objectid, key.offset,
1163 root->root_key.objectid, false);
1164 ret = btrfs_free_extent(trans, &ref);
1166 btrfs_abort_transaction(trans, ret);
1171 btrfs_mark_buffer_dirty(leaf);
1173 btrfs_add_delayed_iput(inode);
1177 static noinline_for_stack
1178 int memcmp_node_keys(struct extent_buffer *eb, int slot,
1179 struct btrfs_path *path, int level)
1181 struct btrfs_disk_key key1;
1182 struct btrfs_disk_key key2;
1183 btrfs_node_key(eb, &key1, slot);
1184 btrfs_node_key(path->nodes[level], &key2, path->slots[level]);
1185 return memcmp(&key1, &key2, sizeof(key1));
1189 * try to replace tree blocks in fs tree with the new blocks
1190 * in reloc tree. tree blocks haven't been modified since the
1191 * reloc tree was create can be replaced.
1193 * if a block was replaced, level of the block + 1 is returned.
1194 * if no block got replaced, 0 is returned. if there are other
1195 * errors, a negative error number is returned.
1197 static noinline_for_stack
1198 int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc,
1199 struct btrfs_root *dest, struct btrfs_root *src,
1200 struct btrfs_path *path, struct btrfs_key *next_key,
1201 int lowest_level, int max_level)
1203 struct btrfs_fs_info *fs_info = dest->fs_info;
1204 struct extent_buffer *eb;
1205 struct extent_buffer *parent;
1206 struct btrfs_ref ref = { 0 };
1207 struct btrfs_key key;
1219 ASSERT(src->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID);
1220 ASSERT(dest->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
1222 last_snapshot = btrfs_root_last_snapshot(&src->root_item);
1224 slot = path->slots[lowest_level];
1225 btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot);
1227 eb = btrfs_lock_root_node(dest);
1228 level = btrfs_header_level(eb);
1230 if (level < lowest_level) {
1231 btrfs_tree_unlock(eb);
1232 free_extent_buffer(eb);
1237 ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb,
1240 btrfs_tree_unlock(eb);
1241 free_extent_buffer(eb);
1247 next_key->objectid = (u64)-1;
1248 next_key->type = (u8)-1;
1249 next_key->offset = (u64)-1;
1254 level = btrfs_header_level(parent);
1255 ASSERT(level >= lowest_level);
1257 ret = btrfs_bin_search(parent, &key, &slot);
1260 if (ret && slot > 0)
1263 if (next_key && slot + 1 < btrfs_header_nritems(parent))
1264 btrfs_node_key_to_cpu(parent, next_key, slot + 1);
1266 old_bytenr = btrfs_node_blockptr(parent, slot);
1267 blocksize = fs_info->nodesize;
1268 old_ptr_gen = btrfs_node_ptr_generation(parent, slot);
1270 if (level <= max_level) {
1271 eb = path->nodes[level];
1272 new_bytenr = btrfs_node_blockptr(eb,
1273 path->slots[level]);
1274 new_ptr_gen = btrfs_node_ptr_generation(eb,
1275 path->slots[level]);
1281 if (WARN_ON(new_bytenr > 0 && new_bytenr == old_bytenr)) {
1286 if (new_bytenr == 0 || old_ptr_gen > last_snapshot ||
1287 memcmp_node_keys(parent, slot, path, level)) {
1288 if (level <= lowest_level) {
1293 eb = btrfs_read_node_slot(parent, slot);
1298 btrfs_tree_lock(eb);
1300 ret = btrfs_cow_block(trans, dest, eb, parent,
1304 btrfs_tree_unlock(eb);
1305 free_extent_buffer(eb);
1310 btrfs_tree_unlock(parent);
1311 free_extent_buffer(parent);
1318 btrfs_tree_unlock(parent);
1319 free_extent_buffer(parent);
1324 btrfs_node_key_to_cpu(path->nodes[level], &key,
1325 path->slots[level]);
1326 btrfs_release_path(path);
1328 path->lowest_level = level;
1329 ret = btrfs_search_slot(trans, src, &key, path, 0, 1);
1330 path->lowest_level = 0;
1338 * Info qgroup to trace both subtrees.
1340 * We must trace both trees.
1341 * 1) Tree reloc subtree
1342 * If not traced, we will leak data numbers
1344 * If not traced, we will double count old data
1346 * We don't scan the subtree right now, but only record
1347 * the swapped tree blocks.
1348 * The real subtree rescan is delayed until we have new
1349 * CoW on the subtree root node before transaction commit.
1351 ret = btrfs_qgroup_add_swapped_blocks(trans, dest,
1352 rc->block_group, parent, slot,
1353 path->nodes[level], path->slots[level],
1358 * swap blocks in fs tree and reloc tree.
1360 btrfs_set_node_blockptr(parent, slot, new_bytenr);
1361 btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen);
1362 btrfs_mark_buffer_dirty(parent);
1364 btrfs_set_node_blockptr(path->nodes[level],
1365 path->slots[level], old_bytenr);
1366 btrfs_set_node_ptr_generation(path->nodes[level],
1367 path->slots[level], old_ptr_gen);
1368 btrfs_mark_buffer_dirty(path->nodes[level]);
1370 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, old_bytenr,
1371 blocksize, path->nodes[level]->start);
1372 btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid,
1374 ret = btrfs_inc_extent_ref(trans, &ref);
1376 btrfs_abort_transaction(trans, ret);
1379 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
1381 btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid, 0,
1383 ret = btrfs_inc_extent_ref(trans, &ref);
1385 btrfs_abort_transaction(trans, ret);
1389 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, new_bytenr,
1390 blocksize, path->nodes[level]->start);
1391 btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid,
1393 ret = btrfs_free_extent(trans, &ref);
1395 btrfs_abort_transaction(trans, ret);
1399 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, old_bytenr,
1401 btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid,
1403 ret = btrfs_free_extent(trans, &ref);
1405 btrfs_abort_transaction(trans, ret);
1409 btrfs_unlock_up_safe(path, 0);
1414 btrfs_tree_unlock(parent);
1415 free_extent_buffer(parent);
1420 * helper to find next relocated block in reloc tree
1422 static noinline_for_stack
1423 int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1426 struct extent_buffer *eb;
1431 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1433 for (i = 0; i < *level; i++) {
1434 free_extent_buffer(path->nodes[i]);
1435 path->nodes[i] = NULL;
1438 for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
1439 eb = path->nodes[i];
1440 nritems = btrfs_header_nritems(eb);
1441 while (path->slots[i] + 1 < nritems) {
1443 if (btrfs_node_ptr_generation(eb, path->slots[i]) <=
1450 free_extent_buffer(path->nodes[i]);
1451 path->nodes[i] = NULL;
1457 * walk down reloc tree to find relocated block of lowest level
1459 static noinline_for_stack
1460 int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1463 struct extent_buffer *eb = NULL;
1469 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1471 for (i = *level; i > 0; i--) {
1472 eb = path->nodes[i];
1473 nritems = btrfs_header_nritems(eb);
1474 while (path->slots[i] < nritems) {
1475 ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]);
1476 if (ptr_gen > last_snapshot)
1480 if (path->slots[i] >= nritems) {
1491 eb = btrfs_read_node_slot(eb, path->slots[i]);
1494 BUG_ON(btrfs_header_level(eb) != i - 1);
1495 path->nodes[i - 1] = eb;
1496 path->slots[i - 1] = 0;
1502 * invalidate extent cache for file extents whose key in range of
1503 * [min_key, max_key)
1505 static int invalidate_extent_cache(struct btrfs_root *root,
1506 struct btrfs_key *min_key,
1507 struct btrfs_key *max_key)
1509 struct btrfs_fs_info *fs_info = root->fs_info;
1510 struct inode *inode = NULL;
1515 objectid = min_key->objectid;
1520 if (objectid > max_key->objectid)
1523 inode = find_next_inode(root, objectid);
1526 ino = btrfs_ino(BTRFS_I(inode));
1528 if (ino > max_key->objectid) {
1534 if (!S_ISREG(inode->i_mode))
1537 if (unlikely(min_key->objectid == ino)) {
1538 if (min_key->type > BTRFS_EXTENT_DATA_KEY)
1540 if (min_key->type < BTRFS_EXTENT_DATA_KEY)
1543 start = min_key->offset;
1544 WARN_ON(!IS_ALIGNED(start, fs_info->sectorsize));
1550 if (unlikely(max_key->objectid == ino)) {
1551 if (max_key->type < BTRFS_EXTENT_DATA_KEY)
1553 if (max_key->type > BTRFS_EXTENT_DATA_KEY) {
1556 if (max_key->offset == 0)
1558 end = max_key->offset;
1559 WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize));
1566 /* the lock_extent waits for read_folio to complete */
1567 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
1568 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 1);
1569 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
1574 static int find_next_key(struct btrfs_path *path, int level,
1575 struct btrfs_key *key)
1578 while (level < BTRFS_MAX_LEVEL) {
1579 if (!path->nodes[level])
1581 if (path->slots[level] + 1 <
1582 btrfs_header_nritems(path->nodes[level])) {
1583 btrfs_node_key_to_cpu(path->nodes[level], key,
1584 path->slots[level] + 1);
1593 * Insert current subvolume into reloc_control::dirty_subvol_roots
1595 static int insert_dirty_subvol(struct btrfs_trans_handle *trans,
1596 struct reloc_control *rc,
1597 struct btrfs_root *root)
1599 struct btrfs_root *reloc_root = root->reloc_root;
1600 struct btrfs_root_item *reloc_root_item;
1603 /* @root must be a subvolume tree root with a valid reloc tree */
1604 ASSERT(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
1607 reloc_root_item = &reloc_root->root_item;
1608 memset(&reloc_root_item->drop_progress, 0,
1609 sizeof(reloc_root_item->drop_progress));
1610 btrfs_set_root_drop_level(reloc_root_item, 0);
1611 btrfs_set_root_refs(reloc_root_item, 0);
1612 ret = btrfs_update_reloc_root(trans, root);
1616 if (list_empty(&root->reloc_dirty_list)) {
1617 btrfs_grab_root(root);
1618 list_add_tail(&root->reloc_dirty_list, &rc->dirty_subvol_roots);
1624 static int clean_dirty_subvols(struct reloc_control *rc)
1626 struct btrfs_root *root;
1627 struct btrfs_root *next;
1631 list_for_each_entry_safe(root, next, &rc->dirty_subvol_roots,
1633 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1634 /* Merged subvolume, cleanup its reloc root */
1635 struct btrfs_root *reloc_root = root->reloc_root;
1637 list_del_init(&root->reloc_dirty_list);
1638 root->reloc_root = NULL;
1640 * Need barrier to ensure clear_bit() only happens after
1641 * root->reloc_root = NULL. Pairs with have_reloc_root.
1644 clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
1647 * btrfs_drop_snapshot drops our ref we hold for
1648 * ->reloc_root. If it fails however we must
1649 * drop the ref ourselves.
1651 ret2 = btrfs_drop_snapshot(reloc_root, 0, 1);
1653 btrfs_put_root(reloc_root);
1658 btrfs_put_root(root);
1660 /* Orphan reloc tree, just clean it up */
1661 ret2 = btrfs_drop_snapshot(root, 0, 1);
1663 btrfs_put_root(root);
1673 * merge the relocated tree blocks in reloc tree with corresponding
1676 static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
1677 struct btrfs_root *root)
1679 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
1680 struct btrfs_key key;
1681 struct btrfs_key next_key;
1682 struct btrfs_trans_handle *trans = NULL;
1683 struct btrfs_root *reloc_root;
1684 struct btrfs_root_item *root_item;
1685 struct btrfs_path *path;
1686 struct extent_buffer *leaf;
1694 path = btrfs_alloc_path();
1697 path->reada = READA_FORWARD;
1699 reloc_root = root->reloc_root;
1700 root_item = &reloc_root->root_item;
1702 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1703 level = btrfs_root_level(root_item);
1704 atomic_inc(&reloc_root->node->refs);
1705 path->nodes[level] = reloc_root->node;
1706 path->slots[level] = 0;
1708 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1710 level = btrfs_root_drop_level(root_item);
1712 path->lowest_level = level;
1713 ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0);
1714 path->lowest_level = 0;
1716 btrfs_free_path(path);
1720 btrfs_node_key_to_cpu(path->nodes[level], &next_key,
1721 path->slots[level]);
1722 WARN_ON(memcmp(&key, &next_key, sizeof(key)));
1724 btrfs_unlock_up_safe(path, 0);
1728 * In merge_reloc_root(), we modify the upper level pointer to swap the
1729 * tree blocks between reloc tree and subvolume tree. Thus for tree
1730 * block COW, we COW at most from level 1 to root level for each tree.
1732 * Thus the needed metadata size is at most root_level * nodesize,
1733 * and * 2 since we have two trees to COW.
1735 reserve_level = max_t(int, 1, btrfs_root_level(root_item));
1736 min_reserved = fs_info->nodesize * reserve_level * 2;
1737 memset(&next_key, 0, sizeof(next_key));
1740 ret = btrfs_block_rsv_refill(fs_info, rc->block_rsv,
1742 BTRFS_RESERVE_FLUSH_LIMIT);
1745 trans = btrfs_start_transaction(root, 0);
1746 if (IS_ERR(trans)) {
1747 ret = PTR_ERR(trans);
1753 * At this point we no longer have a reloc_control, so we can't
1754 * depend on btrfs_init_reloc_root to update our last_trans.
1756 * But that's ok, we started the trans handle on our
1757 * corresponding fs_root, which means it's been added to the
1758 * dirty list. At commit time we'll still call
1759 * btrfs_update_reloc_root() and update our root item
1762 reloc_root->last_trans = trans->transid;
1763 trans->block_rsv = rc->block_rsv;
1768 ret = walk_down_reloc_tree(reloc_root, path, &level);
1774 if (!find_next_key(path, level, &key) &&
1775 btrfs_comp_cpu_keys(&next_key, &key) >= 0) {
1778 ret = replace_path(trans, rc, root, reloc_root, path,
1779 &next_key, level, max_level);
1785 btrfs_node_key_to_cpu(path->nodes[level], &key,
1786 path->slots[level]);
1790 ret = walk_up_reloc_tree(reloc_root, path, &level);
1796 * save the merging progress in the drop_progress.
1797 * this is OK since root refs == 1 in this case.
1799 btrfs_node_key(path->nodes[level], &root_item->drop_progress,
1800 path->slots[level]);
1801 btrfs_set_root_drop_level(root_item, level);
1803 btrfs_end_transaction_throttle(trans);
1806 btrfs_btree_balance_dirty(fs_info);
1808 if (replaced && rc->stage == UPDATE_DATA_PTRS)
1809 invalidate_extent_cache(root, &key, &next_key);
1813 * handle the case only one block in the fs tree need to be
1814 * relocated and the block is tree root.
1816 leaf = btrfs_lock_root_node(root);
1817 ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf,
1819 btrfs_tree_unlock(leaf);
1820 free_extent_buffer(leaf);
1822 btrfs_free_path(path);
1825 ret = insert_dirty_subvol(trans, rc, root);
1827 btrfs_abort_transaction(trans, ret);
1831 btrfs_end_transaction_throttle(trans);
1833 btrfs_btree_balance_dirty(fs_info);
1835 if (replaced && rc->stage == UPDATE_DATA_PTRS)
1836 invalidate_extent_cache(root, &key, &next_key);
1841 static noinline_for_stack
1842 int prepare_to_merge(struct reloc_control *rc, int err)
1844 struct btrfs_root *root = rc->extent_root;
1845 struct btrfs_fs_info *fs_info = root->fs_info;
1846 struct btrfs_root *reloc_root;
1847 struct btrfs_trans_handle *trans;
1848 LIST_HEAD(reloc_roots);
1852 mutex_lock(&fs_info->reloc_mutex);
1853 rc->merging_rsv_size += fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
1854 rc->merging_rsv_size += rc->nodes_relocated * 2;
1855 mutex_unlock(&fs_info->reloc_mutex);
1859 num_bytes = rc->merging_rsv_size;
1860 ret = btrfs_block_rsv_add(fs_info, rc->block_rsv, num_bytes,
1861 BTRFS_RESERVE_FLUSH_ALL);
1866 trans = btrfs_join_transaction(rc->extent_root);
1867 if (IS_ERR(trans)) {
1869 btrfs_block_rsv_release(fs_info, rc->block_rsv,
1871 return PTR_ERR(trans);
1875 if (num_bytes != rc->merging_rsv_size) {
1876 btrfs_end_transaction(trans);
1877 btrfs_block_rsv_release(fs_info, rc->block_rsv,
1883 rc->merge_reloc_tree = 1;
1885 while (!list_empty(&rc->reloc_roots)) {
1886 reloc_root = list_entry(rc->reloc_roots.next,
1887 struct btrfs_root, root_list);
1888 list_del_init(&reloc_root->root_list);
1890 root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
1894 * Even if we have an error we need this reloc root
1895 * back on our list so we can clean up properly.
1897 list_add(&reloc_root->root_list, &reloc_roots);
1898 btrfs_abort_transaction(trans, (int)PTR_ERR(root));
1900 err = PTR_ERR(root);
1903 ASSERT(root->reloc_root == reloc_root);
1906 * set reference count to 1, so btrfs_recover_relocation
1907 * knows it should resumes merging
1910 btrfs_set_root_refs(&reloc_root->root_item, 1);
1911 ret = btrfs_update_reloc_root(trans, root);
1914 * Even if we have an error we need this reloc root back on our
1915 * list so we can clean up properly.
1917 list_add(&reloc_root->root_list, &reloc_roots);
1918 btrfs_put_root(root);
1921 btrfs_abort_transaction(trans, ret);
1928 list_splice(&reloc_roots, &rc->reloc_roots);
1931 err = btrfs_commit_transaction(trans);
1933 btrfs_end_transaction(trans);
1937 static noinline_for_stack
1938 void free_reloc_roots(struct list_head *list)
1940 struct btrfs_root *reloc_root, *tmp;
1942 list_for_each_entry_safe(reloc_root, tmp, list, root_list)
1943 __del_reloc_root(reloc_root);
1946 static noinline_for_stack
1947 void merge_reloc_roots(struct reloc_control *rc)
1949 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
1950 struct btrfs_root *root;
1951 struct btrfs_root *reloc_root;
1952 LIST_HEAD(reloc_roots);
1956 root = rc->extent_root;
1959 * this serializes us with btrfs_record_root_in_transaction,
1960 * we have to make sure nobody is in the middle of
1961 * adding their roots to the list while we are
1964 mutex_lock(&fs_info->reloc_mutex);
1965 list_splice_init(&rc->reloc_roots, &reloc_roots);
1966 mutex_unlock(&fs_info->reloc_mutex);
1968 while (!list_empty(&reloc_roots)) {
1970 reloc_root = list_entry(reloc_roots.next,
1971 struct btrfs_root, root_list);
1973 root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
1975 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
1978 * For recovery we read the fs roots on mount,
1979 * and if we didn't find the root then we marked
1980 * the reloc root as a garbage root. For normal
1981 * relocation obviously the root should exist in
1982 * memory. However there's no reason we can't
1983 * handle the error properly here just in case.
1986 ret = PTR_ERR(root);
1989 if (root->reloc_root != reloc_root) {
1991 * This is actually impossible without something
1992 * going really wrong (like weird race condition
1999 ret = merge_reloc_root(rc, root);
2000 btrfs_put_root(root);
2002 if (list_empty(&reloc_root->root_list))
2003 list_add_tail(&reloc_root->root_list,
2008 if (!IS_ERR(root)) {
2009 if (root->reloc_root == reloc_root) {
2010 root->reloc_root = NULL;
2011 btrfs_put_root(reloc_root);
2013 clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE,
2015 btrfs_put_root(root);
2018 list_del_init(&reloc_root->root_list);
2019 /* Don't forget to queue this reloc root for cleanup */
2020 list_add_tail(&reloc_root->reloc_dirty_list,
2021 &rc->dirty_subvol_roots);
2031 btrfs_handle_fs_error(fs_info, ret, NULL);
2032 free_reloc_roots(&reloc_roots);
2034 /* new reloc root may be added */
2035 mutex_lock(&fs_info->reloc_mutex);
2036 list_splice_init(&rc->reloc_roots, &reloc_roots);
2037 mutex_unlock(&fs_info->reloc_mutex);
2038 free_reloc_roots(&reloc_roots);
2044 * BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
2046 * here, but it's wrong. If we fail to start the transaction in
2047 * prepare_to_merge() we will have only 0 ref reloc roots, none of which
2048 * have actually been removed from the reloc_root_tree rb tree. This is
2049 * fine because we're bailing here, and we hold a reference on the root
2050 * for the list that holds it, so these roots will be cleaned up when we
2051 * do the reloc_dirty_list afterwards. Meanwhile the root->reloc_root
2052 * will be cleaned up on unmount.
2054 * The remaining nodes will be cleaned up by free_reloc_control.
2058 static void free_block_list(struct rb_root *blocks)
2060 struct tree_block *block;
2061 struct rb_node *rb_node;
2062 while ((rb_node = rb_first(blocks))) {
2063 block = rb_entry(rb_node, struct tree_block, rb_node);
2064 rb_erase(rb_node, blocks);
2069 static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans,
2070 struct btrfs_root *reloc_root)
2072 struct btrfs_fs_info *fs_info = reloc_root->fs_info;
2073 struct btrfs_root *root;
2076 if (reloc_root->last_trans == trans->transid)
2079 root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset, false);
2082 * This should succeed, since we can't have a reloc root without having
2083 * already looked up the actual root and created the reloc root for this
2086 * However if there's some sort of corruption where we have a ref to a
2087 * reloc root without a corresponding root this could return ENOENT.
2091 return PTR_ERR(root);
2093 if (root->reloc_root != reloc_root) {
2096 "root %llu has two reloc roots associated with it",
2097 reloc_root->root_key.offset);
2098 btrfs_put_root(root);
2101 ret = btrfs_record_root_in_trans(trans, root);
2102 btrfs_put_root(root);
2107 static noinline_for_stack
2108 struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,
2109 struct reloc_control *rc,
2110 struct btrfs_backref_node *node,
2111 struct btrfs_backref_edge *edges[])
2113 struct btrfs_backref_node *next;
2114 struct btrfs_root *root;
2121 next = walk_up_backref(next, edges, &index);
2125 * If there is no root, then our references for this block are
2126 * incomplete, as we should be able to walk all the way up to a
2127 * block that is owned by a root.
2129 * This path is only for SHAREABLE roots, so if we come upon a
2130 * non-SHAREABLE root then we have backrefs that resolve
2133 * Both of these cases indicate file system corruption, or a bug
2134 * in the backref walking code.
2138 btrfs_err(trans->fs_info,
2139 "bytenr %llu doesn't have a backref path ending in a root",
2141 return ERR_PTR(-EUCLEAN);
2143 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
2145 btrfs_err(trans->fs_info,
2146 "bytenr %llu has multiple refs with one ending in a non-shareable root",
2148 return ERR_PTR(-EUCLEAN);
2151 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
2152 ret = record_reloc_root_in_trans(trans, root);
2154 return ERR_PTR(ret);
2158 ret = btrfs_record_root_in_trans(trans, root);
2160 return ERR_PTR(ret);
2161 root = root->reloc_root;
2164 * We could have raced with another thread which failed, so
2165 * root->reloc_root may not be set, return ENOENT in this case.
2168 return ERR_PTR(-ENOENT);
2170 if (next->new_bytenr != root->node->start) {
2172 * We just created the reloc root, so we shouldn't have
2173 * ->new_bytenr set and this shouldn't be in the changed
2174 * list. If it is then we have multiple roots pointing
2175 * at the same bytenr which indicates corruption, or
2176 * we've made a mistake in the backref walking code.
2178 ASSERT(next->new_bytenr == 0);
2179 ASSERT(list_empty(&next->list));
2180 if (next->new_bytenr || !list_empty(&next->list)) {
2181 btrfs_err(trans->fs_info,
2182 "bytenr %llu possibly has multiple roots pointing at the same bytenr %llu",
2183 node->bytenr, next->bytenr);
2184 return ERR_PTR(-EUCLEAN);
2187 next->new_bytenr = root->node->start;
2188 btrfs_put_root(next->root);
2189 next->root = btrfs_grab_root(root);
2191 list_add_tail(&next->list,
2192 &rc->backref_cache.changed);
2193 mark_block_processed(rc, next);
2199 next = walk_down_backref(edges, &index);
2200 if (!next || next->level <= node->level)
2205 * This can happen if there's fs corruption or if there's a bug
2206 * in the backref lookup code.
2209 return ERR_PTR(-ENOENT);
2213 /* setup backref node path for btrfs_reloc_cow_block */
2215 rc->backref_cache.path[next->level] = next;
2218 next = edges[index]->node[UPPER];
2224 * Select a tree root for relocation.
2226 * Return NULL if the block is not shareable. We should use do_relocation() in
2229 * Return a tree root pointer if the block is shareable.
2230 * Return -ENOENT if the block is root of reloc tree.
2232 static noinline_for_stack
2233 struct btrfs_root *select_one_root(struct btrfs_backref_node *node)
2235 struct btrfs_backref_node *next;
2236 struct btrfs_root *root;
2237 struct btrfs_root *fs_root = NULL;
2238 struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2244 next = walk_up_backref(next, edges, &index);
2248 * This can occur if we have incomplete extent refs leading all
2249 * the way up a particular path, in this case return -EUCLEAN.
2252 return ERR_PTR(-EUCLEAN);
2254 /* No other choice for non-shareable tree */
2255 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
2258 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)
2264 next = walk_down_backref(edges, &index);
2265 if (!next || next->level <= node->level)
2270 return ERR_PTR(-ENOENT);
2274 static noinline_for_stack
2275 u64 calcu_metadata_size(struct reloc_control *rc,
2276 struct btrfs_backref_node *node, int reserve)
2278 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2279 struct btrfs_backref_node *next = node;
2280 struct btrfs_backref_edge *edge;
2281 struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2285 BUG_ON(reserve && node->processed);
2290 if (next->processed && (reserve || next != node))
2293 num_bytes += fs_info->nodesize;
2295 if (list_empty(&next->upper))
2298 edge = list_entry(next->upper.next,
2299 struct btrfs_backref_edge, list[LOWER]);
2300 edges[index++] = edge;
2301 next = edge->node[UPPER];
2303 next = walk_down_backref(edges, &index);
2308 static int reserve_metadata_space(struct btrfs_trans_handle *trans,
2309 struct reloc_control *rc,
2310 struct btrfs_backref_node *node)
2312 struct btrfs_root *root = rc->extent_root;
2313 struct btrfs_fs_info *fs_info = root->fs_info;
2318 num_bytes = calcu_metadata_size(rc, node, 1) * 2;
2320 trans->block_rsv = rc->block_rsv;
2321 rc->reserved_bytes += num_bytes;
2324 * We are under a transaction here so we can only do limited flushing.
2325 * If we get an enospc just kick back -EAGAIN so we know to drop the
2326 * transaction and try to refill when we can flush all the things.
2328 ret = btrfs_block_rsv_refill(fs_info, rc->block_rsv, num_bytes,
2329 BTRFS_RESERVE_FLUSH_LIMIT);
2331 tmp = fs_info->nodesize * RELOCATION_RESERVED_NODES;
2332 while (tmp <= rc->reserved_bytes)
2335 * only one thread can access block_rsv at this point,
2336 * so we don't need hold lock to protect block_rsv.
2337 * we expand more reservation size here to allow enough
2338 * space for relocation and we will return earlier in
2341 rc->block_rsv->size = tmp + fs_info->nodesize *
2342 RELOCATION_RESERVED_NODES;
2350 * relocate a block tree, and then update pointers in upper level
2351 * blocks that reference the block to point to the new location.
2353 * if called by link_to_upper, the block has already been relocated.
2354 * in that case this function just updates pointers.
2356 static int do_relocation(struct btrfs_trans_handle *trans,
2357 struct reloc_control *rc,
2358 struct btrfs_backref_node *node,
2359 struct btrfs_key *key,
2360 struct btrfs_path *path, int lowest)
2362 struct btrfs_backref_node *upper;
2363 struct btrfs_backref_edge *edge;
2364 struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2365 struct btrfs_root *root;
2366 struct extent_buffer *eb;
2373 * If we are lowest then this is the first time we're processing this
2374 * block, and thus shouldn't have an eb associated with it yet.
2376 ASSERT(!lowest || !node->eb);
2378 path->lowest_level = node->level + 1;
2379 rc->backref_cache.path[node->level] = node;
2380 list_for_each_entry(edge, &node->upper, list[LOWER]) {
2381 struct btrfs_ref ref = { 0 };
2385 upper = edge->node[UPPER];
2386 root = select_reloc_root(trans, rc, upper, edges);
2388 ret = PTR_ERR(root);
2392 if (upper->eb && !upper->locked) {
2394 ret = btrfs_bin_search(upper->eb, key, &slot);
2398 bytenr = btrfs_node_blockptr(upper->eb, slot);
2399 if (node->eb->start == bytenr)
2402 btrfs_backref_drop_node_buffer(upper);
2406 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2411 btrfs_release_path(path);
2416 upper->eb = path->nodes[upper->level];
2417 path->nodes[upper->level] = NULL;
2419 BUG_ON(upper->eb != path->nodes[upper->level]);
2423 path->locks[upper->level] = 0;
2425 slot = path->slots[upper->level];
2426 btrfs_release_path(path);
2428 ret = btrfs_bin_search(upper->eb, key, &slot);
2434 bytenr = btrfs_node_blockptr(upper->eb, slot);
2436 if (bytenr != node->bytenr) {
2437 btrfs_err(root->fs_info,
2438 "lowest leaf/node mismatch: bytenr %llu node->bytenr %llu slot %d upper %llu",
2439 bytenr, node->bytenr, slot,
2445 if (node->eb->start == bytenr)
2449 blocksize = root->fs_info->nodesize;
2450 eb = btrfs_read_node_slot(upper->eb, slot);
2455 btrfs_tree_lock(eb);
2458 ret = btrfs_cow_block(trans, root, eb, upper->eb,
2459 slot, &eb, BTRFS_NESTING_COW);
2460 btrfs_tree_unlock(eb);
2461 free_extent_buffer(eb);
2465 * We've just COWed this block, it should have updated
2466 * the correct backref node entry.
2468 ASSERT(node->eb == eb);
2470 btrfs_set_node_blockptr(upper->eb, slot,
2472 btrfs_set_node_ptr_generation(upper->eb, slot,
2474 btrfs_mark_buffer_dirty(upper->eb);
2476 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF,
2477 node->eb->start, blocksize,
2479 btrfs_init_tree_ref(&ref, node->level,
2480 btrfs_header_owner(upper->eb),
2481 root->root_key.objectid, false);
2482 ret = btrfs_inc_extent_ref(trans, &ref);
2484 ret = btrfs_drop_subtree(trans, root, eb,
2487 btrfs_abort_transaction(trans, ret);
2490 if (!upper->pending)
2491 btrfs_backref_drop_node_buffer(upper);
2493 btrfs_backref_unlock_node_buffer(upper);
2498 if (!ret && node->pending) {
2499 btrfs_backref_drop_node_buffer(node);
2500 list_move_tail(&node->list, &rc->backref_cache.changed);
2504 path->lowest_level = 0;
2507 * We should have allocated all of our space in the block rsv and thus
2510 ASSERT(ret != -ENOSPC);
2514 static int link_to_upper(struct btrfs_trans_handle *trans,
2515 struct reloc_control *rc,
2516 struct btrfs_backref_node *node,
2517 struct btrfs_path *path)
2519 struct btrfs_key key;
2521 btrfs_node_key_to_cpu(node->eb, &key, 0);
2522 return do_relocation(trans, rc, node, &key, path, 0);
2525 static int finish_pending_nodes(struct btrfs_trans_handle *trans,
2526 struct reloc_control *rc,
2527 struct btrfs_path *path, int err)
2530 struct btrfs_backref_cache *cache = &rc->backref_cache;
2531 struct btrfs_backref_node *node;
2535 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2536 while (!list_empty(&cache->pending[level])) {
2537 node = list_entry(cache->pending[level].next,
2538 struct btrfs_backref_node, list);
2539 list_move_tail(&node->list, &list);
2540 BUG_ON(!node->pending);
2543 ret = link_to_upper(trans, rc, node, path);
2548 list_splice_init(&list, &cache->pending[level]);
2554 * mark a block and all blocks directly/indirectly reference the block
2557 static void update_processed_blocks(struct reloc_control *rc,
2558 struct btrfs_backref_node *node)
2560 struct btrfs_backref_node *next = node;
2561 struct btrfs_backref_edge *edge;
2562 struct btrfs_backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2568 if (next->processed)
2571 mark_block_processed(rc, next);
2573 if (list_empty(&next->upper))
2576 edge = list_entry(next->upper.next,
2577 struct btrfs_backref_edge, list[LOWER]);
2578 edges[index++] = edge;
2579 next = edge->node[UPPER];
2581 next = walk_down_backref(edges, &index);
2585 static int tree_block_processed(u64 bytenr, struct reloc_control *rc)
2587 u32 blocksize = rc->extent_root->fs_info->nodesize;
2589 if (test_range_bit(&rc->processed_blocks, bytenr,
2590 bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL))
2595 static int get_tree_block_key(struct btrfs_fs_info *fs_info,
2596 struct tree_block *block)
2598 struct extent_buffer *eb;
2600 eb = read_tree_block(fs_info, block->bytenr, block->owner,
2601 block->key.offset, block->level, NULL);
2604 if (!extent_buffer_uptodate(eb)) {
2605 free_extent_buffer(eb);
2608 if (block->level == 0)
2609 btrfs_item_key_to_cpu(eb, &block->key, 0);
2611 btrfs_node_key_to_cpu(eb, &block->key, 0);
2612 free_extent_buffer(eb);
2613 block->key_ready = 1;
2618 * helper function to relocate a tree block
2620 static int relocate_tree_block(struct btrfs_trans_handle *trans,
2621 struct reloc_control *rc,
2622 struct btrfs_backref_node *node,
2623 struct btrfs_key *key,
2624 struct btrfs_path *path)
2626 struct btrfs_root *root;
2633 * If we fail here we want to drop our backref_node because we are going
2634 * to start over and regenerate the tree for it.
2636 ret = reserve_metadata_space(trans, rc, node);
2640 BUG_ON(node->processed);
2641 root = select_one_root(node);
2643 ret = PTR_ERR(root);
2645 /* See explanation in select_one_root for the -EUCLEAN case. */
2646 ASSERT(ret == -ENOENT);
2647 if (ret == -ENOENT) {
2649 update_processed_blocks(rc, node);
2655 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
2657 * This block was the root block of a root, and this is
2658 * the first time we're processing the block and thus it
2659 * should not have had the ->new_bytenr modified and
2660 * should have not been included on the changed list.
2662 * However in the case of corruption we could have
2663 * multiple refs pointing to the same block improperly,
2664 * and thus we would trip over these checks. ASSERT()
2665 * for the developer case, because it could indicate a
2666 * bug in the backref code, however error out for a
2667 * normal user in the case of corruption.
2669 ASSERT(node->new_bytenr == 0);
2670 ASSERT(list_empty(&node->list));
2671 if (node->new_bytenr || !list_empty(&node->list)) {
2672 btrfs_err(root->fs_info,
2673 "bytenr %llu has improper references to it",
2678 ret = btrfs_record_root_in_trans(trans, root);
2682 * Another thread could have failed, need to check if we
2683 * have reloc_root actually set.
2685 if (!root->reloc_root) {
2689 root = root->reloc_root;
2690 node->new_bytenr = root->node->start;
2691 btrfs_put_root(node->root);
2692 node->root = btrfs_grab_root(root);
2694 list_add_tail(&node->list, &rc->backref_cache.changed);
2696 path->lowest_level = node->level;
2697 if (root == root->fs_info->chunk_root)
2698 btrfs_reserve_chunk_metadata(trans, false);
2699 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2700 btrfs_release_path(path);
2701 if (root == root->fs_info->chunk_root)
2702 btrfs_trans_release_chunk_metadata(trans);
2707 update_processed_blocks(rc, node);
2709 ret = do_relocation(trans, rc, node, key, path, 1);
2712 if (ret || node->level == 0 || node->cowonly)
2713 btrfs_backref_cleanup_node(&rc->backref_cache, node);
2718 * relocate a list of blocks
2720 static noinline_for_stack
2721 int relocate_tree_blocks(struct btrfs_trans_handle *trans,
2722 struct reloc_control *rc, struct rb_root *blocks)
2724 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2725 struct btrfs_backref_node *node;
2726 struct btrfs_path *path;
2727 struct tree_block *block;
2728 struct tree_block *next;
2732 path = btrfs_alloc_path();
2735 goto out_free_blocks;
2738 /* Kick in readahead for tree blocks with missing keys */
2739 rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
2740 if (!block->key_ready)
2741 btrfs_readahead_tree_block(fs_info, block->bytenr,
2746 /* Get first keys */
2747 rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
2748 if (!block->key_ready) {
2749 err = get_tree_block_key(fs_info, block);
2755 /* Do tree relocation */
2756 rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
2757 node = build_backref_tree(rc, &block->key,
2758 block->level, block->bytenr);
2760 err = PTR_ERR(node);
2764 ret = relocate_tree_block(trans, rc, node, &block->key,
2772 err = finish_pending_nodes(trans, rc, path, err);
2775 btrfs_free_path(path);
2777 free_block_list(blocks);
2781 static noinline_for_stack int prealloc_file_extent_cluster(
2782 struct btrfs_inode *inode,
2783 struct file_extent_cluster *cluster)
2788 u64 offset = inode->index_cnt;
2792 u64 i_size = i_size_read(&inode->vfs_inode);
2793 u64 prealloc_start = cluster->start - offset;
2794 u64 prealloc_end = cluster->end - offset;
2795 u64 cur_offset = prealloc_start;
2798 * For subpage case, previous i_size may not be aligned to PAGE_SIZE.
2799 * This means the range [i_size, PAGE_END + 1) is filled with zeros by
2800 * btrfs_do_readpage() call of previously relocated file cluster.
2802 * If the current cluster starts in the above range, btrfs_do_readpage()
2803 * will skip the read, and relocate_one_page() will later writeback
2804 * the padding zeros as new data, causing data corruption.
2806 * Here we have to manually invalidate the range (i_size, PAGE_END + 1).
2808 if (!IS_ALIGNED(i_size, PAGE_SIZE)) {
2809 struct address_space *mapping = inode->vfs_inode.i_mapping;
2810 struct btrfs_fs_info *fs_info = inode->root->fs_info;
2811 const u32 sectorsize = fs_info->sectorsize;
2814 ASSERT(sectorsize < PAGE_SIZE);
2815 ASSERT(IS_ALIGNED(i_size, sectorsize));
2818 * Subpage can't handle page with DIRTY but without UPTODATE
2819 * bit as it can lead to the following deadlock:
2821 * btrfs_read_folio()
2822 * | Page already *locked*
2823 * |- btrfs_lock_and_flush_ordered_range()
2824 * |- btrfs_start_ordered_extent()
2825 * |- extent_write_cache_pages()
2827 * We try to lock the page we already hold.
2829 * Here we just writeback the whole data reloc inode, so that
2830 * we will be ensured to have no dirty range in the page, and
2831 * are safe to clear the uptodate bits.
2833 * This shouldn't cause too much overhead, as we need to write
2834 * the data back anyway.
2836 ret = filemap_write_and_wait(mapping);
2840 clear_extent_bits(&inode->io_tree, i_size,
2841 round_up(i_size, PAGE_SIZE) - 1,
2843 page = find_lock_page(mapping, i_size >> PAGE_SHIFT);
2845 * If page is freed we don't need to do anything then, as we
2846 * will re-read the whole page anyway.
2849 btrfs_subpage_clear_uptodate(fs_info, page, i_size,
2850 round_up(i_size, PAGE_SIZE) - i_size);
2856 BUG_ON(cluster->start != cluster->boundary[0]);
2857 ret = btrfs_alloc_data_chunk_ondemand(inode,
2858 prealloc_end + 1 - prealloc_start);
2862 btrfs_inode_lock(&inode->vfs_inode, 0);
2863 for (nr = 0; nr < cluster->nr; nr++) {
2864 start = cluster->boundary[nr] - offset;
2865 if (nr + 1 < cluster->nr)
2866 end = cluster->boundary[nr + 1] - 1 - offset;
2868 end = cluster->end - offset;
2870 lock_extent(&inode->io_tree, start, end);
2871 num_bytes = end + 1 - start;
2872 ret = btrfs_prealloc_file_range(&inode->vfs_inode, 0, start,
2873 num_bytes, num_bytes,
2874 end + 1, &alloc_hint);
2875 cur_offset = end + 1;
2876 unlock_extent(&inode->io_tree, start, end);
2880 btrfs_inode_unlock(&inode->vfs_inode, 0);
2882 if (cur_offset < prealloc_end)
2883 btrfs_free_reserved_data_space_noquota(inode->root->fs_info,
2884 prealloc_end + 1 - cur_offset);
2888 static noinline_for_stack int setup_relocation_extent_mapping(struct inode *inode,
2889 u64 start, u64 end, u64 block_start)
2891 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2892 struct extent_map *em;
2895 em = alloc_extent_map();
2900 em->len = end + 1 - start;
2901 em->block_len = em->len;
2902 em->block_start = block_start;
2903 set_bit(EXTENT_FLAG_PINNED, &em->flags);
2905 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
2907 write_lock(&em_tree->lock);
2908 ret = add_extent_mapping(em_tree, em, 0);
2909 write_unlock(&em_tree->lock);
2910 if (ret != -EEXIST) {
2911 free_extent_map(em);
2914 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
2916 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
2921 * Allow error injection to test balance/relocation cancellation
2923 noinline int btrfs_should_cancel_balance(struct btrfs_fs_info *fs_info)
2925 return atomic_read(&fs_info->balance_cancel_req) ||
2926 atomic_read(&fs_info->reloc_cancel_req) ||
2927 fatal_signal_pending(current);
2929 ALLOW_ERROR_INJECTION(btrfs_should_cancel_balance, TRUE);
2931 static u64 get_cluster_boundary_end(struct file_extent_cluster *cluster,
2934 /* Last extent, use cluster end directly */
2935 if (cluster_nr >= cluster->nr - 1)
2936 return cluster->end;
2938 /* Use next boundary start*/
2939 return cluster->boundary[cluster_nr + 1] - 1;
2942 static int relocate_one_page(struct inode *inode, struct file_ra_state *ra,
2943 struct file_extent_cluster *cluster,
2944 int *cluster_nr, unsigned long page_index)
2946 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2947 u64 offset = BTRFS_I(inode)->index_cnt;
2948 const unsigned long last_index = (cluster->end - offset) >> PAGE_SHIFT;
2949 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
2956 ASSERT(page_index <= last_index);
2957 page = find_lock_page(inode->i_mapping, page_index);
2959 page_cache_sync_readahead(inode->i_mapping, ra, NULL,
2960 page_index, last_index + 1 - page_index);
2961 page = find_or_create_page(inode->i_mapping, page_index, mask);
2965 ret = set_page_extent_mapped(page);
2969 if (PageReadahead(page))
2970 page_cache_async_readahead(inode->i_mapping, ra, NULL,
2971 page_folio(page), page_index,
2972 last_index + 1 - page_index);
2974 if (!PageUptodate(page)) {
2975 btrfs_read_folio(NULL, page_folio(page));
2977 if (!PageUptodate(page)) {
2983 page_start = page_offset(page);
2984 page_end = page_start + PAGE_SIZE - 1;
2987 * Start from the cluster, as for subpage case, the cluster can start
2990 cur = max(page_start, cluster->boundary[*cluster_nr] - offset);
2991 while (cur <= page_end) {
2992 u64 extent_start = cluster->boundary[*cluster_nr] - offset;
2993 u64 extent_end = get_cluster_boundary_end(cluster,
2994 *cluster_nr) - offset;
2995 u64 clamped_start = max(page_start, extent_start);
2996 u64 clamped_end = min(page_end, extent_end);
2997 u32 clamped_len = clamped_end + 1 - clamped_start;
2999 /* Reserve metadata for this range */
3000 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode),
3001 clamped_len, clamped_len,
3006 /* Mark the range delalloc and dirty for later writeback */
3007 lock_extent(&BTRFS_I(inode)->io_tree, clamped_start, clamped_end);
3008 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), clamped_start,
3009 clamped_end, 0, NULL);
3011 clear_extent_bits(&BTRFS_I(inode)->io_tree,
3012 clamped_start, clamped_end,
3013 EXTENT_LOCKED | EXTENT_BOUNDARY);
3014 btrfs_delalloc_release_metadata(BTRFS_I(inode),
3016 btrfs_delalloc_release_extents(BTRFS_I(inode),
3020 btrfs_page_set_dirty(fs_info, page, clamped_start, clamped_len);
3023 * Set the boundary if it's inside the page.
3024 * Data relocation requires the destination extents to have the
3025 * same size as the source.
3026 * EXTENT_BOUNDARY bit prevents current extent from being merged
3027 * with previous extent.
3029 if (in_range(cluster->boundary[*cluster_nr] - offset,
3030 page_start, PAGE_SIZE)) {
3031 u64 boundary_start = cluster->boundary[*cluster_nr] -
3033 u64 boundary_end = boundary_start +
3034 fs_info->sectorsize - 1;
3036 set_extent_bits(&BTRFS_I(inode)->io_tree,
3037 boundary_start, boundary_end,
3040 unlock_extent(&BTRFS_I(inode)->io_tree, clamped_start, clamped_end);
3041 btrfs_delalloc_release_extents(BTRFS_I(inode), clamped_len);
3044 /* Crossed extent end, go to next extent */
3045 if (cur >= extent_end) {
3047 /* Just finished the last extent of the cluster, exit. */
3048 if (*cluster_nr >= cluster->nr)
3055 balance_dirty_pages_ratelimited(inode->i_mapping);
3056 btrfs_throttle(fs_info);
3057 if (btrfs_should_cancel_balance(fs_info))
3067 static int relocate_file_extent_cluster(struct inode *inode,
3068 struct file_extent_cluster *cluster)
3070 u64 offset = BTRFS_I(inode)->index_cnt;
3071 unsigned long index;
3072 unsigned long last_index;
3073 struct file_ra_state *ra;
3080 ra = kzalloc(sizeof(*ra), GFP_NOFS);
3084 ret = prealloc_file_extent_cluster(BTRFS_I(inode), cluster);
3088 file_ra_state_init(ra, inode->i_mapping);
3090 ret = setup_relocation_extent_mapping(inode, cluster->start - offset,
3091 cluster->end - offset, cluster->start);
3095 last_index = (cluster->end - offset) >> PAGE_SHIFT;
3096 for (index = (cluster->start - offset) >> PAGE_SHIFT;
3097 index <= last_index && !ret; index++)
3098 ret = relocate_one_page(inode, ra, cluster, &cluster_nr, index);
3100 WARN_ON(cluster_nr != cluster->nr);
3106 static noinline_for_stack
3107 int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key,
3108 struct file_extent_cluster *cluster)
3112 if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) {
3113 ret = relocate_file_extent_cluster(inode, cluster);
3120 cluster->start = extent_key->objectid;
3122 BUG_ON(cluster->nr >= MAX_EXTENTS);
3123 cluster->end = extent_key->objectid + extent_key->offset - 1;
3124 cluster->boundary[cluster->nr] = extent_key->objectid;
3127 if (cluster->nr >= MAX_EXTENTS) {
3128 ret = relocate_file_extent_cluster(inode, cluster);
3137 * helper to add a tree block to the list.
3138 * the major work is getting the generation and level of the block
3140 static int add_tree_block(struct reloc_control *rc,
3141 struct btrfs_key *extent_key,
3142 struct btrfs_path *path,
3143 struct rb_root *blocks)
3145 struct extent_buffer *eb;
3146 struct btrfs_extent_item *ei;
3147 struct btrfs_tree_block_info *bi;
3148 struct tree_block *block;
3149 struct rb_node *rb_node;
3155 eb = path->nodes[0];
3156 item_size = btrfs_item_size(eb, path->slots[0]);
3158 if (extent_key->type == BTRFS_METADATA_ITEM_KEY ||
3159 item_size >= sizeof(*ei) + sizeof(*bi)) {
3160 unsigned long ptr = 0, end;
3162 ei = btrfs_item_ptr(eb, path->slots[0],
3163 struct btrfs_extent_item);
3164 end = (unsigned long)ei + item_size;
3165 if (extent_key->type == BTRFS_EXTENT_ITEM_KEY) {
3166 bi = (struct btrfs_tree_block_info *)(ei + 1);
3167 level = btrfs_tree_block_level(eb, bi);
3168 ptr = (unsigned long)(bi + 1);
3170 level = (int)extent_key->offset;
3171 ptr = (unsigned long)(ei + 1);
3173 generation = btrfs_extent_generation(eb, ei);
3176 * We're reading random blocks without knowing their owner ahead
3177 * of time. This is ok most of the time, as all reloc roots and
3178 * fs roots have the same lock type. However normal trees do
3179 * not, and the only way to know ahead of time is to read the
3180 * inline ref offset. We know it's an fs root if
3182 * 1. There's more than one ref.
3183 * 2. There's a SHARED_DATA_REF_KEY set.
3184 * 3. FULL_BACKREF is set on the flags.
3186 * Otherwise it's safe to assume that the ref offset == the
3187 * owner of this block, so we can use that when calling
3190 if (btrfs_extent_refs(eb, ei) == 1 &&
3191 !(btrfs_extent_flags(eb, ei) &
3192 BTRFS_BLOCK_FLAG_FULL_BACKREF) &&
3194 struct btrfs_extent_inline_ref *iref;
3197 iref = (struct btrfs_extent_inline_ref *)ptr;
3198 type = btrfs_get_extent_inline_ref_type(eb, iref,
3199 BTRFS_REF_TYPE_BLOCK);
3200 if (type == BTRFS_REF_TYPE_INVALID)
3202 if (type == BTRFS_TREE_BLOCK_REF_KEY)
3203 owner = btrfs_extent_inline_ref_offset(eb, iref);
3205 } else if (unlikely(item_size == sizeof(struct btrfs_extent_item_v0))) {
3206 btrfs_print_v0_err(eb->fs_info);
3207 btrfs_handle_fs_error(eb->fs_info, -EINVAL, NULL);
3213 btrfs_release_path(path);
3215 BUG_ON(level == -1);
3217 block = kmalloc(sizeof(*block), GFP_NOFS);
3221 block->bytenr = extent_key->objectid;
3222 block->key.objectid = rc->extent_root->fs_info->nodesize;
3223 block->key.offset = generation;
3224 block->level = level;
3225 block->key_ready = 0;
3226 block->owner = owner;
3228 rb_node = rb_simple_insert(blocks, block->bytenr, &block->rb_node);
3230 btrfs_backref_panic(rc->extent_root->fs_info, block->bytenr,
3237 * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
3239 static int __add_tree_block(struct reloc_control *rc,
3240 u64 bytenr, u32 blocksize,
3241 struct rb_root *blocks)
3243 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3244 struct btrfs_path *path;
3245 struct btrfs_key key;
3247 bool skinny = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
3249 if (tree_block_processed(bytenr, rc))
3252 if (rb_simple_search(blocks, bytenr))
3255 path = btrfs_alloc_path();
3259 key.objectid = bytenr;
3261 key.type = BTRFS_METADATA_ITEM_KEY;
3262 key.offset = (u64)-1;
3264 key.type = BTRFS_EXTENT_ITEM_KEY;
3265 key.offset = blocksize;
3268 path->search_commit_root = 1;
3269 path->skip_locking = 1;
3270 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0);
3274 if (ret > 0 && skinny) {
3275 if (path->slots[0]) {
3277 btrfs_item_key_to_cpu(path->nodes[0], &key,
3279 if (key.objectid == bytenr &&
3280 (key.type == BTRFS_METADATA_ITEM_KEY ||
3281 (key.type == BTRFS_EXTENT_ITEM_KEY &&
3282 key.offset == blocksize)))
3288 btrfs_release_path(path);
3294 btrfs_print_leaf(path->nodes[0]);
3296 "tree block extent item (%llu) is not found in extent tree",
3303 ret = add_tree_block(rc, &key, path, blocks);
3305 btrfs_free_path(path);
3309 static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
3310 struct btrfs_block_group *block_group,
3311 struct inode *inode,
3314 struct btrfs_root *root = fs_info->tree_root;
3315 struct btrfs_trans_handle *trans;
3321 inode = btrfs_iget(fs_info->sb, ino, root);
3326 ret = btrfs_check_trunc_cache_free_space(fs_info,
3327 &fs_info->global_block_rsv);
3331 trans = btrfs_join_transaction(root);
3332 if (IS_ERR(trans)) {
3333 ret = PTR_ERR(trans);
3337 ret = btrfs_truncate_free_space_cache(trans, block_group, inode);
3339 btrfs_end_transaction(trans);
3340 btrfs_btree_balance_dirty(fs_info);
3347 * Locate the free space cache EXTENT_DATA in root tree leaf and delete the
3348 * cache inode, to avoid free space cache data extent blocking data relocation.
3350 static int delete_v1_space_cache(struct extent_buffer *leaf,
3351 struct btrfs_block_group *block_group,
3354 u64 space_cache_ino;
3355 struct btrfs_file_extent_item *ei;
3356 struct btrfs_key key;
3361 if (btrfs_header_owner(leaf) != BTRFS_ROOT_TREE_OBJECTID)
3364 for (i = 0; i < btrfs_header_nritems(leaf); i++) {
3367 btrfs_item_key_to_cpu(leaf, &key, i);
3368 if (key.type != BTRFS_EXTENT_DATA_KEY)
3370 ei = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
3371 type = btrfs_file_extent_type(leaf, ei);
3373 if ((type == BTRFS_FILE_EXTENT_REG ||
3374 type == BTRFS_FILE_EXTENT_PREALLOC) &&
3375 btrfs_file_extent_disk_bytenr(leaf, ei) == data_bytenr) {
3377 space_cache_ino = key.objectid;
3383 ret = delete_block_group_cache(leaf->fs_info, block_group, NULL,
3389 * helper to find all tree blocks that reference a given data extent
3391 static noinline_for_stack
3392 int add_data_references(struct reloc_control *rc,
3393 struct btrfs_key *extent_key,
3394 struct btrfs_path *path,
3395 struct rb_root *blocks)
3397 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3398 struct ulist *leaves = NULL;
3399 struct ulist_iterator leaf_uiter;
3400 struct ulist_node *ref_node = NULL;
3401 const u32 blocksize = fs_info->nodesize;
3404 btrfs_release_path(path);
3405 ret = btrfs_find_all_leafs(NULL, fs_info, extent_key->objectid,
3406 0, &leaves, NULL, true);
3410 ULIST_ITER_INIT(&leaf_uiter);
3411 while ((ref_node = ulist_next(leaves, &leaf_uiter))) {
3412 struct extent_buffer *eb;
3414 eb = read_tree_block(fs_info, ref_node->val, 0, 0, 0, NULL);
3419 ret = delete_v1_space_cache(eb, rc->block_group,
3420 extent_key->objectid);
3421 free_extent_buffer(eb);
3424 ret = __add_tree_block(rc, ref_node->val, blocksize, blocks);
3429 free_block_list(blocks);
3435 * helper to find next unprocessed extent
3437 static noinline_for_stack
3438 int find_next_extent(struct reloc_control *rc, struct btrfs_path *path,
3439 struct btrfs_key *extent_key)
3441 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3442 struct btrfs_key key;
3443 struct extent_buffer *leaf;
3444 u64 start, end, last;
3447 last = rc->block_group->start + rc->block_group->length;
3450 if (rc->search_start >= last) {
3455 key.objectid = rc->search_start;
3456 key.type = BTRFS_EXTENT_ITEM_KEY;
3459 path->search_commit_root = 1;
3460 path->skip_locking = 1;
3461 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path,
3466 leaf = path->nodes[0];
3467 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3468 ret = btrfs_next_leaf(rc->extent_root, path);
3471 leaf = path->nodes[0];
3474 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3475 if (key.objectid >= last) {
3480 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3481 key.type != BTRFS_METADATA_ITEM_KEY) {
3486 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3487 key.objectid + key.offset <= rc->search_start) {
3492 if (key.type == BTRFS_METADATA_ITEM_KEY &&
3493 key.objectid + fs_info->nodesize <=
3499 ret = find_first_extent_bit(&rc->processed_blocks,
3500 key.objectid, &start, &end,
3501 EXTENT_DIRTY, NULL);
3503 if (ret == 0 && start <= key.objectid) {
3504 btrfs_release_path(path);
3505 rc->search_start = end + 1;
3507 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3508 rc->search_start = key.objectid + key.offset;
3510 rc->search_start = key.objectid +
3512 memcpy(extent_key, &key, sizeof(key));
3516 btrfs_release_path(path);
3520 static void set_reloc_control(struct reloc_control *rc)
3522 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3524 mutex_lock(&fs_info->reloc_mutex);
3525 fs_info->reloc_ctl = rc;
3526 mutex_unlock(&fs_info->reloc_mutex);
3529 static void unset_reloc_control(struct reloc_control *rc)
3531 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3533 mutex_lock(&fs_info->reloc_mutex);
3534 fs_info->reloc_ctl = NULL;
3535 mutex_unlock(&fs_info->reloc_mutex);
3538 static noinline_for_stack
3539 int prepare_to_relocate(struct reloc_control *rc)
3541 struct btrfs_trans_handle *trans;
3544 rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root->fs_info,
3545 BTRFS_BLOCK_RSV_TEMP);
3549 memset(&rc->cluster, 0, sizeof(rc->cluster));
3550 rc->search_start = rc->block_group->start;
3551 rc->extents_found = 0;
3552 rc->nodes_relocated = 0;
3553 rc->merging_rsv_size = 0;
3554 rc->reserved_bytes = 0;
3555 rc->block_rsv->size = rc->extent_root->fs_info->nodesize *
3556 RELOCATION_RESERVED_NODES;
3557 ret = btrfs_block_rsv_refill(rc->extent_root->fs_info,
3558 rc->block_rsv, rc->block_rsv->size,
3559 BTRFS_RESERVE_FLUSH_ALL);
3563 rc->create_reloc_tree = 1;
3564 set_reloc_control(rc);
3566 trans = btrfs_join_transaction(rc->extent_root);
3567 if (IS_ERR(trans)) {
3568 unset_reloc_control(rc);
3570 * extent tree is not a ref_cow tree and has no reloc_root to
3571 * cleanup. And callers are responsible to free the above
3574 return PTR_ERR(trans);
3576 return btrfs_commit_transaction(trans);
3579 static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
3581 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3582 struct rb_root blocks = RB_ROOT;
3583 struct btrfs_key key;
3584 struct btrfs_trans_handle *trans = NULL;
3585 struct btrfs_path *path;
3586 struct btrfs_extent_item *ei;
3592 path = btrfs_alloc_path();
3595 path->reada = READA_FORWARD;
3597 ret = prepare_to_relocate(rc);
3604 rc->reserved_bytes = 0;
3605 ret = btrfs_block_rsv_refill(fs_info, rc->block_rsv,
3606 rc->block_rsv->size,
3607 BTRFS_RESERVE_FLUSH_ALL);
3613 trans = btrfs_start_transaction(rc->extent_root, 0);
3614 if (IS_ERR(trans)) {
3615 err = PTR_ERR(trans);
3620 if (update_backref_cache(trans, &rc->backref_cache)) {
3621 btrfs_end_transaction(trans);
3626 ret = find_next_extent(rc, path, &key);
3632 rc->extents_found++;
3634 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3635 struct btrfs_extent_item);
3636 flags = btrfs_extent_flags(path->nodes[0], ei);
3638 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
3639 ret = add_tree_block(rc, &key, path, &blocks);
3640 } else if (rc->stage == UPDATE_DATA_PTRS &&
3641 (flags & BTRFS_EXTENT_FLAG_DATA)) {
3642 ret = add_data_references(rc, &key, path, &blocks);
3644 btrfs_release_path(path);
3652 if (!RB_EMPTY_ROOT(&blocks)) {
3653 ret = relocate_tree_blocks(trans, rc, &blocks);
3655 if (ret != -EAGAIN) {
3659 rc->extents_found--;
3660 rc->search_start = key.objectid;
3664 btrfs_end_transaction_throttle(trans);
3665 btrfs_btree_balance_dirty(fs_info);
3668 if (rc->stage == MOVE_DATA_EXTENTS &&
3669 (flags & BTRFS_EXTENT_FLAG_DATA)) {
3670 rc->found_file_extent = 1;
3671 ret = relocate_data_extent(rc->data_inode,
3672 &key, &rc->cluster);
3678 if (btrfs_should_cancel_balance(fs_info)) {
3683 if (trans && progress && err == -ENOSPC) {
3684 ret = btrfs_force_chunk_alloc(trans, rc->block_group->flags);
3692 btrfs_release_path(path);
3693 clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY);
3696 btrfs_end_transaction_throttle(trans);
3697 btrfs_btree_balance_dirty(fs_info);
3701 ret = relocate_file_extent_cluster(rc->data_inode,
3707 rc->create_reloc_tree = 0;
3708 set_reloc_control(rc);
3710 btrfs_backref_release_cache(&rc->backref_cache);
3711 btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL);
3714 * Even in the case when the relocation is cancelled, we should all go
3715 * through prepare_to_merge() and merge_reloc_roots().
3717 * For error (including cancelled balance), prepare_to_merge() will
3718 * mark all reloc trees orphan, then queue them for cleanup in
3719 * merge_reloc_roots()
3721 err = prepare_to_merge(rc, err);
3723 merge_reloc_roots(rc);
3725 rc->merge_reloc_tree = 0;
3726 unset_reloc_control(rc);
3727 btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL);
3729 /* get rid of pinned extents */
3730 trans = btrfs_join_transaction(rc->extent_root);
3731 if (IS_ERR(trans)) {
3732 err = PTR_ERR(trans);
3735 ret = btrfs_commit_transaction(trans);
3739 ret = clean_dirty_subvols(rc);
3740 if (ret < 0 && !err)
3742 btrfs_free_block_rsv(fs_info, rc->block_rsv);
3743 btrfs_free_path(path);
3747 static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
3748 struct btrfs_root *root, u64 objectid)
3750 struct btrfs_path *path;
3751 struct btrfs_inode_item *item;
3752 struct extent_buffer *leaf;
3755 path = btrfs_alloc_path();
3759 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
3763 leaf = path->nodes[0];
3764 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
3765 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
3766 btrfs_set_inode_generation(leaf, item, 1);
3767 btrfs_set_inode_size(leaf, item, 0);
3768 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
3769 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS |
3770 BTRFS_INODE_PREALLOC);
3771 btrfs_mark_buffer_dirty(leaf);
3773 btrfs_free_path(path);
3777 static void delete_orphan_inode(struct btrfs_trans_handle *trans,
3778 struct btrfs_root *root, u64 objectid)
3780 struct btrfs_path *path;
3781 struct btrfs_key key;
3784 path = btrfs_alloc_path();
3790 key.objectid = objectid;
3791 key.type = BTRFS_INODE_ITEM_KEY;
3793 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3799 ret = btrfs_del_item(trans, root, path);
3802 btrfs_abort_transaction(trans, ret);
3803 btrfs_free_path(path);
3807 * helper to create inode for data relocation.
3808 * the inode is in data relocation tree and its link count is 0
3810 static noinline_for_stack
3811 struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
3812 struct btrfs_block_group *group)
3814 struct inode *inode = NULL;
3815 struct btrfs_trans_handle *trans;
3816 struct btrfs_root *root;
3820 root = btrfs_grab_root(fs_info->data_reloc_root);
3821 trans = btrfs_start_transaction(root, 6);
3822 if (IS_ERR(trans)) {
3823 btrfs_put_root(root);
3824 return ERR_CAST(trans);
3827 err = btrfs_get_free_objectid(root, &objectid);
3831 err = __insert_orphan_inode(trans, root, objectid);
3835 inode = btrfs_iget(fs_info->sb, objectid, root);
3836 if (IS_ERR(inode)) {
3837 delete_orphan_inode(trans, root, objectid);
3838 err = PTR_ERR(inode);
3842 BTRFS_I(inode)->index_cnt = group->start;
3844 err = btrfs_orphan_add(trans, BTRFS_I(inode));
3846 btrfs_put_root(root);
3847 btrfs_end_transaction(trans);
3848 btrfs_btree_balance_dirty(fs_info);
3851 inode = ERR_PTR(err);
3857 * Mark start of chunk relocation that is cancellable. Check if the cancellation
3858 * has been requested meanwhile and don't start in that case.
3862 * -EINPROGRESS operation is already in progress, that's probably a bug
3863 * -ECANCELED cancellation request was set before the operation started
3865 static int reloc_chunk_start(struct btrfs_fs_info *fs_info)
3867 if (test_and_set_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags)) {
3868 /* This should not happen */
3869 btrfs_err(fs_info, "reloc already running, cannot start");
3870 return -EINPROGRESS;
3873 if (atomic_read(&fs_info->reloc_cancel_req) > 0) {
3874 btrfs_info(fs_info, "chunk relocation canceled on start");
3876 * On cancel, clear all requests but let the caller mark
3877 * the end after cleanup operations.
3879 atomic_set(&fs_info->reloc_cancel_req, 0);
3886 * Mark end of chunk relocation that is cancellable and wake any waiters.
3888 static void reloc_chunk_end(struct btrfs_fs_info *fs_info)
3890 /* Requested after start, clear bit first so any waiters can continue */
3891 if (atomic_read(&fs_info->reloc_cancel_req) > 0)
3892 btrfs_info(fs_info, "chunk relocation canceled during operation");
3893 clear_and_wake_up_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags);
3894 atomic_set(&fs_info->reloc_cancel_req, 0);
3897 static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info)
3899 struct reloc_control *rc;
3901 rc = kzalloc(sizeof(*rc), GFP_NOFS);
3905 INIT_LIST_HEAD(&rc->reloc_roots);
3906 INIT_LIST_HEAD(&rc->dirty_subvol_roots);
3907 btrfs_backref_init_cache(fs_info, &rc->backref_cache, 1);
3908 mapping_tree_init(&rc->reloc_root_tree);
3909 extent_io_tree_init(fs_info, &rc->processed_blocks,
3910 IO_TREE_RELOC_BLOCKS, NULL);
3914 static void free_reloc_control(struct reloc_control *rc)
3916 struct mapping_node *node, *tmp;
3918 free_reloc_roots(&rc->reloc_roots);
3919 rbtree_postorder_for_each_entry_safe(node, tmp,
3920 &rc->reloc_root_tree.rb_root, rb_node)
3927 * Print the block group being relocated
3929 static void describe_relocation(struct btrfs_fs_info *fs_info,
3930 struct btrfs_block_group *block_group)
3932 char buf[128] = {'\0'};
3934 btrfs_describe_block_groups(block_group->flags, buf, sizeof(buf));
3937 "relocating block group %llu flags %s",
3938 block_group->start, buf);
3941 static const char *stage_to_string(int stage)
3943 if (stage == MOVE_DATA_EXTENTS)
3944 return "move data extents";
3945 if (stage == UPDATE_DATA_PTRS)
3946 return "update data pointers";
3951 * function to relocate all extents in a block group.
3953 int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start)
3955 struct btrfs_block_group *bg;
3956 struct btrfs_root *extent_root = btrfs_extent_root(fs_info, group_start);
3957 struct reloc_control *rc;
3958 struct inode *inode;
3959 struct btrfs_path *path;
3965 * This only gets set if we had a half-deleted snapshot on mount. We
3966 * cannot allow relocation to start while we're still trying to clean up
3967 * these pending deletions.
3969 ret = wait_on_bit(&fs_info->flags, BTRFS_FS_UNFINISHED_DROPS, TASK_INTERRUPTIBLE);
3973 /* We may have been woken up by close_ctree, so bail if we're closing. */
3974 if (btrfs_fs_closing(fs_info))
3977 bg = btrfs_lookup_block_group(fs_info, group_start);
3982 * Relocation of a data block group creates ordered extents. Without
3983 * sb_start_write(), we can freeze the filesystem while unfinished
3984 * ordered extents are left. Such ordered extents can cause a deadlock
3985 * e.g. when syncfs() is waiting for their completion but they can't
3986 * finish because they block when joining a transaction, due to the
3987 * fact that the freeze locks are being held in write mode.
3989 if (bg->flags & BTRFS_BLOCK_GROUP_DATA)
3990 ASSERT(sb_write_started(fs_info->sb));
3992 if (btrfs_pinned_by_swapfile(fs_info, bg)) {
3993 btrfs_put_block_group(bg);
3997 rc = alloc_reloc_control(fs_info);
3999 btrfs_put_block_group(bg);
4003 ret = reloc_chunk_start(fs_info);
4009 rc->extent_root = extent_root;
4010 rc->block_group = bg;
4012 ret = btrfs_inc_block_group_ro(rc->block_group, true);
4019 path = btrfs_alloc_path();
4025 inode = lookup_free_space_inode(rc->block_group, path);
4026 btrfs_free_path(path);
4029 ret = delete_block_group_cache(fs_info, rc->block_group, inode, 0);
4031 ret = PTR_ERR(inode);
4033 if (ret && ret != -ENOENT) {
4038 rc->data_inode = create_reloc_inode(fs_info, rc->block_group);
4039 if (IS_ERR(rc->data_inode)) {
4040 err = PTR_ERR(rc->data_inode);
4041 rc->data_inode = NULL;
4045 describe_relocation(fs_info, rc->block_group);
4047 btrfs_wait_block_group_reservations(rc->block_group);
4048 btrfs_wait_nocow_writers(rc->block_group);
4049 btrfs_wait_ordered_roots(fs_info, U64_MAX,
4050 rc->block_group->start,
4051 rc->block_group->length);
4053 ret = btrfs_zone_finish(rc->block_group);
4054 WARN_ON(ret && ret != -EAGAIN);
4059 mutex_lock(&fs_info->cleaner_mutex);
4060 ret = relocate_block_group(rc);
4061 mutex_unlock(&fs_info->cleaner_mutex);
4065 finishes_stage = rc->stage;
4067 * We may have gotten ENOSPC after we already dirtied some
4068 * extents. If writeout happens while we're relocating a
4069 * different block group we could end up hitting the
4070 * BUG_ON(rc->stage == UPDATE_DATA_PTRS) in
4071 * btrfs_reloc_cow_block. Make sure we write everything out
4072 * properly so we don't trip over this problem, and then break
4073 * out of the loop if we hit an error.
4075 if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) {
4076 ret = btrfs_wait_ordered_range(rc->data_inode, 0,
4080 invalidate_mapping_pages(rc->data_inode->i_mapping,
4082 rc->stage = UPDATE_DATA_PTRS;
4088 if (rc->extents_found == 0)
4091 btrfs_info(fs_info, "found %llu extents, stage: %s",
4092 rc->extents_found, stage_to_string(finishes_stage));
4095 WARN_ON(rc->block_group->pinned > 0);
4096 WARN_ON(rc->block_group->reserved > 0);
4097 WARN_ON(rc->block_group->used > 0);
4100 btrfs_dec_block_group_ro(rc->block_group);
4101 iput(rc->data_inode);
4103 btrfs_put_block_group(bg);
4104 reloc_chunk_end(fs_info);
4105 free_reloc_control(rc);
4109 static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
4111 struct btrfs_fs_info *fs_info = root->fs_info;
4112 struct btrfs_trans_handle *trans;
4115 trans = btrfs_start_transaction(fs_info->tree_root, 0);
4117 return PTR_ERR(trans);
4119 memset(&root->root_item.drop_progress, 0,
4120 sizeof(root->root_item.drop_progress));
4121 btrfs_set_root_drop_level(&root->root_item, 0);
4122 btrfs_set_root_refs(&root->root_item, 0);
4123 ret = btrfs_update_root(trans, fs_info->tree_root,
4124 &root->root_key, &root->root_item);
4126 err = btrfs_end_transaction(trans);
4133 * recover relocation interrupted by system crash.
4135 * this function resumes merging reloc trees with corresponding fs trees.
4136 * this is important for keeping the sharing of tree blocks
4138 int btrfs_recover_relocation(struct btrfs_fs_info *fs_info)
4140 LIST_HEAD(reloc_roots);
4141 struct btrfs_key key;
4142 struct btrfs_root *fs_root;
4143 struct btrfs_root *reloc_root;
4144 struct btrfs_path *path;
4145 struct extent_buffer *leaf;
4146 struct reloc_control *rc = NULL;
4147 struct btrfs_trans_handle *trans;
4151 path = btrfs_alloc_path();
4154 path->reada = READA_BACK;
4156 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4157 key.type = BTRFS_ROOT_ITEM_KEY;
4158 key.offset = (u64)-1;
4161 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key,
4168 if (path->slots[0] == 0)
4172 leaf = path->nodes[0];
4173 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4174 btrfs_release_path(path);
4176 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID ||
4177 key.type != BTRFS_ROOT_ITEM_KEY)
4180 reloc_root = btrfs_read_tree_root(fs_info->tree_root, &key);
4181 if (IS_ERR(reloc_root)) {
4182 err = PTR_ERR(reloc_root);
4186 set_bit(BTRFS_ROOT_SHAREABLE, &reloc_root->state);
4187 list_add(&reloc_root->root_list, &reloc_roots);
4189 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
4190 fs_root = btrfs_get_fs_root(fs_info,
4191 reloc_root->root_key.offset, false);
4192 if (IS_ERR(fs_root)) {
4193 ret = PTR_ERR(fs_root);
4194 if (ret != -ENOENT) {
4198 ret = mark_garbage_root(reloc_root);
4204 btrfs_put_root(fs_root);
4208 if (key.offset == 0)
4213 btrfs_release_path(path);
4215 if (list_empty(&reloc_roots))
4218 rc = alloc_reloc_control(fs_info);
4224 ret = reloc_chunk_start(fs_info);
4230 rc->extent_root = btrfs_extent_root(fs_info, 0);
4232 set_reloc_control(rc);
4234 trans = btrfs_join_transaction(rc->extent_root);
4235 if (IS_ERR(trans)) {
4236 err = PTR_ERR(trans);
4240 rc->merge_reloc_tree = 1;
4242 while (!list_empty(&reloc_roots)) {
4243 reloc_root = list_entry(reloc_roots.next,
4244 struct btrfs_root, root_list);
4245 list_del(&reloc_root->root_list);
4247 if (btrfs_root_refs(&reloc_root->root_item) == 0) {
4248 list_add_tail(&reloc_root->root_list,
4253 fs_root = btrfs_get_fs_root(fs_info, reloc_root->root_key.offset,
4255 if (IS_ERR(fs_root)) {
4256 err = PTR_ERR(fs_root);
4257 list_add_tail(&reloc_root->root_list, &reloc_roots);
4258 btrfs_end_transaction(trans);
4262 err = __add_reloc_root(reloc_root);
4263 ASSERT(err != -EEXIST);
4265 list_add_tail(&reloc_root->root_list, &reloc_roots);
4266 btrfs_put_root(fs_root);
4267 btrfs_end_transaction(trans);
4270 fs_root->reloc_root = btrfs_grab_root(reloc_root);
4271 btrfs_put_root(fs_root);
4274 err = btrfs_commit_transaction(trans);
4278 merge_reloc_roots(rc);
4280 unset_reloc_control(rc);
4282 trans = btrfs_join_transaction(rc->extent_root);
4283 if (IS_ERR(trans)) {
4284 err = PTR_ERR(trans);
4287 err = btrfs_commit_transaction(trans);
4289 ret = clean_dirty_subvols(rc);
4290 if (ret < 0 && !err)
4293 unset_reloc_control(rc);
4295 reloc_chunk_end(fs_info);
4296 free_reloc_control(rc);
4298 free_reloc_roots(&reloc_roots);
4300 btrfs_free_path(path);
4303 /* cleanup orphan inode in data relocation tree */
4304 fs_root = btrfs_grab_root(fs_info->data_reloc_root);
4306 err = btrfs_orphan_cleanup(fs_root);
4307 btrfs_put_root(fs_root);
4313 * helper to add ordered checksum for data relocation.
4315 * cloning checksum properly handles the nodatasum extents.
4316 * it also saves CPU time to re-calculate the checksum.
4318 int btrfs_reloc_clone_csums(struct btrfs_inode *inode, u64 file_pos, u64 len)
4320 struct btrfs_fs_info *fs_info = inode->root->fs_info;
4321 struct btrfs_root *csum_root;
4322 struct btrfs_ordered_sum *sums;
4323 struct btrfs_ordered_extent *ordered;
4329 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
4330 BUG_ON(ordered->file_offset != file_pos || ordered->num_bytes != len);
4332 disk_bytenr = file_pos + inode->index_cnt;
4333 csum_root = btrfs_csum_root(fs_info, disk_bytenr);
4334 ret = btrfs_lookup_csums_range(csum_root, disk_bytenr,
4335 disk_bytenr + len - 1, &list, 0);
4339 while (!list_empty(&list)) {
4340 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
4341 list_del_init(&sums->list);
4344 * We need to offset the new_bytenr based on where the csum is.
4345 * We need to do this because we will read in entire prealloc
4346 * extents but we may have written to say the middle of the
4347 * prealloc extent, so we need to make sure the csum goes with
4348 * the right disk offset.
4350 * We can do this because the data reloc inode refers strictly
4351 * to the on disk bytes, so we don't have to worry about
4352 * disk_len vs real len like with real inodes since it's all
4355 new_bytenr = ordered->disk_bytenr + sums->bytenr - disk_bytenr;
4356 sums->bytenr = new_bytenr;
4358 btrfs_add_ordered_sum(ordered, sums);
4361 btrfs_put_ordered_extent(ordered);
4365 int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
4366 struct btrfs_root *root, struct extent_buffer *buf,
4367 struct extent_buffer *cow)
4369 struct btrfs_fs_info *fs_info = root->fs_info;
4370 struct reloc_control *rc;
4371 struct btrfs_backref_node *node;
4376 rc = fs_info->reloc_ctl;
4380 BUG_ON(rc->stage == UPDATE_DATA_PTRS && btrfs_is_data_reloc_root(root));
4382 level = btrfs_header_level(buf);
4383 if (btrfs_header_generation(buf) <=
4384 btrfs_root_last_snapshot(&root->root_item))
4387 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID &&
4388 rc->create_reloc_tree) {
4389 WARN_ON(!first_cow && level == 0);
4391 node = rc->backref_cache.path[level];
4392 BUG_ON(node->bytenr != buf->start &&
4393 node->new_bytenr != buf->start);
4395 btrfs_backref_drop_node_buffer(node);
4396 atomic_inc(&cow->refs);
4398 node->new_bytenr = cow->start;
4400 if (!node->pending) {
4401 list_move_tail(&node->list,
4402 &rc->backref_cache.pending[level]);
4407 mark_block_processed(rc, node);
4409 if (first_cow && level > 0)
4410 rc->nodes_relocated += buf->len;
4413 if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS)
4414 ret = replace_file_extents(trans, rc, root, cow);
4419 * called before creating snapshot. it calculates metadata reservation
4420 * required for relocating tree blocks in the snapshot
4422 void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending,
4423 u64 *bytes_to_reserve)
4425 struct btrfs_root *root = pending->root;
4426 struct reloc_control *rc = root->fs_info->reloc_ctl;
4428 if (!rc || !have_reloc_root(root))
4431 if (!rc->merge_reloc_tree)
4434 root = root->reloc_root;
4435 BUG_ON(btrfs_root_refs(&root->root_item) == 0);
4437 * relocation is in the stage of merging trees. the space
4438 * used by merging a reloc tree is twice the size of
4439 * relocated tree nodes in the worst case. half for cowing
4440 * the reloc tree, half for cowing the fs tree. the space
4441 * used by cowing the reloc tree will be freed after the
4442 * tree is dropped. if we create snapshot, cowing the fs
4443 * tree may use more space than it frees. so we need
4444 * reserve extra space.
4446 *bytes_to_reserve += rc->nodes_relocated;
4450 * called after snapshot is created. migrate block reservation
4451 * and create reloc root for the newly created snapshot
4453 * This is similar to btrfs_init_reloc_root(), we come out of here with two
4454 * references held on the reloc_root, one for root->reloc_root and one for
4457 int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
4458 struct btrfs_pending_snapshot *pending)
4460 struct btrfs_root *root = pending->root;
4461 struct btrfs_root *reloc_root;
4462 struct btrfs_root *new_root;
4463 struct reloc_control *rc = root->fs_info->reloc_ctl;
4466 if (!rc || !have_reloc_root(root))
4469 rc = root->fs_info->reloc_ctl;
4470 rc->merging_rsv_size += rc->nodes_relocated;
4472 if (rc->merge_reloc_tree) {
4473 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
4475 rc->nodes_relocated, true);
4480 new_root = pending->snap;
4481 reloc_root = create_reloc_root(trans, root->reloc_root,
4482 new_root->root_key.objectid);
4483 if (IS_ERR(reloc_root))
4484 return PTR_ERR(reloc_root);
4486 ret = __add_reloc_root(reloc_root);
4487 ASSERT(ret != -EEXIST);
4489 /* Pairs with create_reloc_root */
4490 btrfs_put_root(reloc_root);
4493 new_root->reloc_root = btrfs_grab_root(reloc_root);
4495 if (rc->create_reloc_tree)
4496 ret = clone_backref_node(trans, rc, root, reloc_root);