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"
21 #include "inode-map.h"
23 #include "print-tree.h"
24 #include "delalloc-space.h"
25 #include "block-group.h"
31 * [What does relocation do]
33 * The objective of relocation is to relocate all extents of the target block
34 * group to other block groups.
35 * This is utilized by resize (shrink only), profile converting, compacting
36 * space, or balance routine to spread chunks over devices.
39 * ------------------------------------------------------------------
40 * BG A: 10 data extents | BG A: deleted
41 * BG B: 2 data extents | BG B: 10 data extents (2 old + 8 relocated)
42 * BG C: 1 extents | BG C: 3 data extents (1 old + 2 relocated)
44 * [How does relocation work]
46 * 1. Mark the target block group read-only
47 * New extents won't be allocated from the target block group.
49 * 2.1 Record each extent in the target block group
50 * To build a proper map of extents to be relocated.
52 * 2.2 Build data reloc tree and reloc trees
53 * Data reloc tree will contain an inode, recording all newly relocated
55 * There will be only one data reloc tree for one data block group.
57 * Reloc tree will be a special snapshot of its source tree, containing
58 * relocated tree blocks.
59 * Each tree referring to a tree block in target block group will get its
62 * 2.3 Swap source tree with its corresponding reloc tree
63 * Each involved tree only refers to new extents after swap.
65 * 3. Cleanup reloc trees and data reloc tree.
66 * As old extents in the target block group are still referenced by reloc
67 * trees, we need to clean them up before really freeing the target block
70 * The main complexity is in steps 2.2 and 2.3.
72 * The entry point of relocation is relocate_block_group() function.
76 * backref_node, mapping_node and tree_block start with this
79 struct rb_node rb_node;
84 * present a tree block in the backref cache
87 struct rb_node rb_node;
91 /* objectid of tree block owner, can be not uptodate */
93 /* link to pending, changed or detached list */
94 struct list_head list;
95 /* list of upper level blocks reference this block */
96 struct list_head upper;
97 /* list of child blocks in the cache */
98 struct list_head lower;
99 /* NULL if this node is not tree root */
100 struct btrfs_root *root;
101 /* extent buffer got by COW the block */
102 struct extent_buffer *eb;
103 /* level of tree block */
104 unsigned int level:8;
105 /* is the block in non-reference counted tree */
106 unsigned int cowonly:1;
107 /* 1 if no child node in the cache */
108 unsigned int lowest:1;
109 /* is the extent buffer locked */
110 unsigned int locked:1;
111 /* has the block been processed */
112 unsigned int processed:1;
113 /* have backrefs of this block been checked */
114 unsigned int checked:1;
116 * 1 if corresponding block has been cowed but some upper
117 * level block pointers may not point to the new location
119 unsigned int pending:1;
121 * 1 if the backref node isn't connected to any other
124 unsigned int detached:1;
128 * present a block pointer in the backref cache
130 struct backref_edge {
131 struct list_head list[2];
132 struct backref_node *node[2];
137 #define RELOCATION_RESERVED_NODES 256
139 struct backref_cache {
140 /* red black tree of all backref nodes in the cache */
141 struct rb_root rb_root;
142 /* for passing backref nodes to btrfs_reloc_cow_block */
143 struct backref_node *path[BTRFS_MAX_LEVEL];
145 * list of blocks that have been cowed but some block
146 * pointers in upper level blocks may not reflect the
149 struct list_head pending[BTRFS_MAX_LEVEL];
150 /* list of backref nodes with no child node */
151 struct list_head leaves;
152 /* list of blocks that have been cowed in current transaction */
153 struct list_head changed;
154 /* list of detached backref node. */
155 struct list_head detached;
164 * map address of tree root to tree
166 struct mapping_node {
167 struct rb_node rb_node;
172 struct mapping_tree {
173 struct rb_root rb_root;
178 * present a tree block to process
181 struct rb_node rb_node;
183 struct btrfs_key key;
184 unsigned int level:8;
185 unsigned int key_ready:1;
188 #define MAX_EXTENTS 128
190 struct file_extent_cluster {
193 u64 boundary[MAX_EXTENTS];
197 struct reloc_control {
198 /* block group to relocate */
199 struct btrfs_block_group *block_group;
201 struct btrfs_root *extent_root;
202 /* inode for moving data */
203 struct inode *data_inode;
205 struct btrfs_block_rsv *block_rsv;
207 struct backref_cache backref_cache;
209 struct file_extent_cluster cluster;
210 /* tree blocks have been processed */
211 struct extent_io_tree processed_blocks;
212 /* map start of tree root to corresponding reloc tree */
213 struct mapping_tree reloc_root_tree;
214 /* list of reloc trees */
215 struct list_head reloc_roots;
216 /* list of subvolume trees that get relocated */
217 struct list_head dirty_subvol_roots;
218 /* size of metadata reservation for merging reloc trees */
219 u64 merging_rsv_size;
220 /* size of relocated tree nodes */
222 /* reserved size for block group relocation*/
228 unsigned int stage:8;
229 unsigned int create_reloc_tree:1;
230 unsigned int merge_reloc_tree:1;
231 unsigned int found_file_extent:1;
234 /* stages of data relocation */
235 #define MOVE_DATA_EXTENTS 0
236 #define UPDATE_DATA_PTRS 1
238 static void remove_backref_node(struct backref_cache *cache,
239 struct backref_node *node);
240 static void __mark_block_processed(struct reloc_control *rc,
241 struct backref_node *node);
243 static void mapping_tree_init(struct mapping_tree *tree)
245 tree->rb_root = RB_ROOT;
246 spin_lock_init(&tree->lock);
249 static void backref_cache_init(struct backref_cache *cache)
252 cache->rb_root = RB_ROOT;
253 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
254 INIT_LIST_HEAD(&cache->pending[i]);
255 INIT_LIST_HEAD(&cache->changed);
256 INIT_LIST_HEAD(&cache->detached);
257 INIT_LIST_HEAD(&cache->leaves);
260 static void backref_cache_cleanup(struct backref_cache *cache)
262 struct backref_node *node;
265 while (!list_empty(&cache->detached)) {
266 node = list_entry(cache->detached.next,
267 struct backref_node, list);
268 remove_backref_node(cache, node);
271 while (!list_empty(&cache->leaves)) {
272 node = list_entry(cache->leaves.next,
273 struct backref_node, lower);
274 remove_backref_node(cache, node);
277 cache->last_trans = 0;
279 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
280 ASSERT(list_empty(&cache->pending[i]));
281 ASSERT(list_empty(&cache->changed));
282 ASSERT(list_empty(&cache->detached));
283 ASSERT(RB_EMPTY_ROOT(&cache->rb_root));
284 ASSERT(!cache->nr_nodes);
285 ASSERT(!cache->nr_edges);
288 static struct backref_node *alloc_backref_node(struct backref_cache *cache)
290 struct backref_node *node;
292 node = kzalloc(sizeof(*node), GFP_NOFS);
294 INIT_LIST_HEAD(&node->list);
295 INIT_LIST_HEAD(&node->upper);
296 INIT_LIST_HEAD(&node->lower);
297 RB_CLEAR_NODE(&node->rb_node);
303 static void free_backref_node(struct backref_cache *cache,
304 struct backref_node *node)
308 btrfs_put_root(node->root);
313 static struct backref_edge *alloc_backref_edge(struct backref_cache *cache)
315 struct backref_edge *edge;
317 edge = kzalloc(sizeof(*edge), GFP_NOFS);
323 static void free_backref_edge(struct backref_cache *cache,
324 struct backref_edge *edge)
332 static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr,
333 struct rb_node *node)
335 struct rb_node **p = &root->rb_node;
336 struct rb_node *parent = NULL;
337 struct tree_entry *entry;
341 entry = rb_entry(parent, struct tree_entry, rb_node);
343 if (bytenr < entry->bytenr)
345 else if (bytenr > entry->bytenr)
351 rb_link_node(node, parent, p);
352 rb_insert_color(node, root);
356 static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
358 struct rb_node *n = root->rb_node;
359 struct tree_entry *entry;
362 entry = rb_entry(n, struct tree_entry, rb_node);
364 if (bytenr < entry->bytenr)
366 else if (bytenr > entry->bytenr)
374 static void backref_tree_panic(struct rb_node *rb_node, int errno, u64 bytenr)
377 struct btrfs_fs_info *fs_info = NULL;
378 struct backref_node *bnode = rb_entry(rb_node, struct backref_node,
381 fs_info = bnode->root->fs_info;
382 btrfs_panic(fs_info, errno,
383 "Inconsistency in backref cache found at offset %llu",
388 * walk up backref nodes until reach node presents tree root
390 static struct backref_node *walk_up_backref(struct backref_node *node,
391 struct backref_edge *edges[],
394 struct backref_edge *edge;
397 while (!list_empty(&node->upper)) {
398 edge = list_entry(node->upper.next,
399 struct backref_edge, list[LOWER]);
401 node = edge->node[UPPER];
403 BUG_ON(node->detached);
409 * walk down backref nodes to find start of next reference path
411 static struct backref_node *walk_down_backref(struct backref_edge *edges[],
414 struct backref_edge *edge;
415 struct backref_node *lower;
419 edge = edges[idx - 1];
420 lower = edge->node[LOWER];
421 if (list_is_last(&edge->list[LOWER], &lower->upper)) {
425 edge = list_entry(edge->list[LOWER].next,
426 struct backref_edge, list[LOWER]);
427 edges[idx - 1] = edge;
429 return edge->node[UPPER];
435 static void unlock_node_buffer(struct backref_node *node)
438 btrfs_tree_unlock(node->eb);
443 static void drop_node_buffer(struct backref_node *node)
446 unlock_node_buffer(node);
447 free_extent_buffer(node->eb);
452 static void drop_backref_node(struct backref_cache *tree,
453 struct backref_node *node)
455 BUG_ON(!list_empty(&node->upper));
457 drop_node_buffer(node);
458 list_del(&node->list);
459 list_del(&node->lower);
460 if (!RB_EMPTY_NODE(&node->rb_node))
461 rb_erase(&node->rb_node, &tree->rb_root);
462 free_backref_node(tree, node);
466 * remove a backref node from the backref cache
468 static void remove_backref_node(struct backref_cache *cache,
469 struct backref_node *node)
471 struct backref_node *upper;
472 struct backref_edge *edge;
477 BUG_ON(!node->lowest && !node->detached);
478 while (!list_empty(&node->upper)) {
479 edge = list_entry(node->upper.next, struct backref_edge,
481 upper = edge->node[UPPER];
482 list_del(&edge->list[LOWER]);
483 list_del(&edge->list[UPPER]);
484 free_backref_edge(cache, edge);
486 if (RB_EMPTY_NODE(&upper->rb_node)) {
487 BUG_ON(!list_empty(&node->upper));
488 drop_backref_node(cache, node);
494 * add the node to leaf node list if no other
495 * child block cached.
497 if (list_empty(&upper->lower)) {
498 list_add_tail(&upper->lower, &cache->leaves);
503 drop_backref_node(cache, node);
506 static void update_backref_node(struct backref_cache *cache,
507 struct backref_node *node, u64 bytenr)
509 struct rb_node *rb_node;
510 rb_erase(&node->rb_node, &cache->rb_root);
511 node->bytenr = bytenr;
512 rb_node = tree_insert(&cache->rb_root, node->bytenr, &node->rb_node);
514 backref_tree_panic(rb_node, -EEXIST, bytenr);
518 * update backref cache after a transaction commit
520 static int update_backref_cache(struct btrfs_trans_handle *trans,
521 struct backref_cache *cache)
523 struct backref_node *node;
526 if (cache->last_trans == 0) {
527 cache->last_trans = trans->transid;
531 if (cache->last_trans == trans->transid)
535 * detached nodes are used to avoid unnecessary backref
536 * lookup. transaction commit changes the extent tree.
537 * so the detached nodes are no longer useful.
539 while (!list_empty(&cache->detached)) {
540 node = list_entry(cache->detached.next,
541 struct backref_node, list);
542 remove_backref_node(cache, node);
545 while (!list_empty(&cache->changed)) {
546 node = list_entry(cache->changed.next,
547 struct backref_node, list);
548 list_del_init(&node->list);
549 BUG_ON(node->pending);
550 update_backref_node(cache, node, node->new_bytenr);
554 * some nodes can be left in the pending list if there were
555 * errors during processing the pending nodes.
557 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
558 list_for_each_entry(node, &cache->pending[level], list) {
559 BUG_ON(!node->pending);
560 if (node->bytenr == node->new_bytenr)
562 update_backref_node(cache, node, node->new_bytenr);
566 cache->last_trans = 0;
570 static bool reloc_root_is_dead(struct btrfs_root *root)
573 * Pair with set_bit/clear_bit in clean_dirty_subvols and
574 * btrfs_update_reloc_root. We need to see the updated bit before
575 * trying to access reloc_root
578 if (test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state))
584 * Check if this subvolume tree has valid reloc tree.
586 * Reloc tree after swap is considered dead, thus not considered as valid.
587 * This is enough for most callers, as they don't distinguish dead reloc root
588 * from no reloc root. But should_ignore_root() below is a special case.
590 static bool have_reloc_root(struct btrfs_root *root)
592 if (reloc_root_is_dead(root))
594 if (!root->reloc_root)
599 static int should_ignore_root(struct btrfs_root *root)
601 struct btrfs_root *reloc_root;
603 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
606 /* This root has been merged with its reloc tree, we can ignore it */
607 if (reloc_root_is_dead(root))
610 reloc_root = root->reloc_root;
614 if (btrfs_header_generation(reloc_root->commit_root) ==
615 root->fs_info->running_transaction->transid)
618 * if there is reloc tree and it was created in previous
619 * transaction backref lookup can find the reloc tree,
620 * so backref node for the fs tree root is useless for
626 * find reloc tree by address of tree root
628 static struct btrfs_root *find_reloc_root(struct reloc_control *rc,
631 struct rb_node *rb_node;
632 struct mapping_node *node;
633 struct btrfs_root *root = NULL;
635 spin_lock(&rc->reloc_root_tree.lock);
636 rb_node = tree_search(&rc->reloc_root_tree.rb_root, bytenr);
638 node = rb_entry(rb_node, struct mapping_node, rb_node);
639 root = (struct btrfs_root *)node->data;
641 spin_unlock(&rc->reloc_root_tree.lock);
642 return btrfs_grab_root(root);
645 static struct btrfs_root *read_fs_root(struct btrfs_fs_info *fs_info,
648 struct btrfs_key key;
650 key.objectid = root_objectid;
651 key.type = BTRFS_ROOT_ITEM_KEY;
652 key.offset = (u64)-1;
654 return btrfs_get_fs_root(fs_info, &key, false);
657 static noinline_for_stack
658 int find_inline_backref(struct extent_buffer *leaf, int slot,
659 unsigned long *ptr, unsigned long *end)
661 struct btrfs_key key;
662 struct btrfs_extent_item *ei;
663 struct btrfs_tree_block_info *bi;
666 btrfs_item_key_to_cpu(leaf, &key, slot);
668 item_size = btrfs_item_size_nr(leaf, slot);
669 if (item_size < sizeof(*ei)) {
670 btrfs_print_v0_err(leaf->fs_info);
671 btrfs_handle_fs_error(leaf->fs_info, -EINVAL, NULL);
674 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
675 WARN_ON(!(btrfs_extent_flags(leaf, ei) &
676 BTRFS_EXTENT_FLAG_TREE_BLOCK));
678 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
679 item_size <= sizeof(*ei) + sizeof(*bi)) {
680 WARN_ON(item_size < sizeof(*ei) + sizeof(*bi));
683 if (key.type == BTRFS_METADATA_ITEM_KEY &&
684 item_size <= sizeof(*ei)) {
685 WARN_ON(item_size < sizeof(*ei));
689 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
690 bi = (struct btrfs_tree_block_info *)(ei + 1);
691 *ptr = (unsigned long)(bi + 1);
693 *ptr = (unsigned long)(ei + 1);
695 *end = (unsigned long)ei + item_size;
700 * build backref tree for a given tree block. root of the backref tree
701 * corresponds the tree block, leaves of the backref tree correspond
702 * roots of b-trees that reference the tree block.
704 * the basic idea of this function is check backrefs of a given block
705 * to find upper level blocks that reference the block, and then check
706 * backrefs of these upper level blocks recursively. the recursion stop
707 * when tree root is reached or backrefs for the block is cached.
709 * NOTE: if we find backrefs for a block are cached, we know backrefs
710 * for all upper level blocks that directly/indirectly reference the
711 * block are also cached.
713 static noinline_for_stack
714 struct backref_node *build_backref_tree(struct reloc_control *rc,
715 struct btrfs_key *node_key,
716 int level, u64 bytenr)
718 struct backref_cache *cache = &rc->backref_cache;
719 struct btrfs_path *path1; /* For searching extent root */
720 struct btrfs_path *path2; /* For searching parent of TREE_BLOCK_REF */
721 struct extent_buffer *eb;
722 struct btrfs_root *root;
723 struct backref_node *cur;
724 struct backref_node *upper;
725 struct backref_node *lower;
726 struct backref_node *node = NULL;
727 struct backref_node *exist = NULL;
728 struct backref_edge *edge;
729 struct rb_node *rb_node;
730 struct btrfs_key key;
733 LIST_HEAD(list); /* Pending edge list, upper node needs to be checked */
738 bool need_check = true;
740 path1 = btrfs_alloc_path();
741 path2 = btrfs_alloc_path();
742 if (!path1 || !path2) {
747 node = alloc_backref_node(cache);
753 node->bytenr = bytenr;
760 key.objectid = cur->bytenr;
761 key.type = BTRFS_METADATA_ITEM_KEY;
762 key.offset = (u64)-1;
764 path1->search_commit_root = 1;
765 path1->skip_locking = 1;
766 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path1,
773 ASSERT(path1->slots[0]);
777 WARN_ON(cur->checked);
778 if (!list_empty(&cur->upper)) {
780 * the backref was added previously when processing
781 * backref of type BTRFS_TREE_BLOCK_REF_KEY
783 ASSERT(list_is_singular(&cur->upper));
784 edge = list_entry(cur->upper.next, struct backref_edge,
786 ASSERT(list_empty(&edge->list[UPPER]));
787 exist = edge->node[UPPER];
789 * add the upper level block to pending list if we need
793 list_add_tail(&edge->list[UPPER], &list);
800 eb = path1->nodes[0];
803 if (path1->slots[0] >= btrfs_header_nritems(eb)) {
804 ret = btrfs_next_leaf(rc->extent_root, path1);
811 eb = path1->nodes[0];
814 btrfs_item_key_to_cpu(eb, &key, path1->slots[0]);
815 if (key.objectid != cur->bytenr) {
820 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
821 key.type == BTRFS_METADATA_ITEM_KEY) {
822 ret = find_inline_backref(eb, path1->slots[0],
830 /* update key for inline back ref */
831 struct btrfs_extent_inline_ref *iref;
833 iref = (struct btrfs_extent_inline_ref *)ptr;
834 type = btrfs_get_extent_inline_ref_type(eb, iref,
835 BTRFS_REF_TYPE_BLOCK);
836 if (type == BTRFS_REF_TYPE_INVALID) {
841 key.offset = btrfs_extent_inline_ref_offset(eb, iref);
843 WARN_ON(key.type != BTRFS_TREE_BLOCK_REF_KEY &&
844 key.type != BTRFS_SHARED_BLOCK_REF_KEY);
848 * Parent node found and matches current inline ref, no need to
849 * rebuild this node for this inline ref.
852 ((key.type == BTRFS_TREE_BLOCK_REF_KEY &&
853 exist->owner == key.offset) ||
854 (key.type == BTRFS_SHARED_BLOCK_REF_KEY &&
855 exist->bytenr == key.offset))) {
860 /* SHARED_BLOCK_REF means key.offset is the parent bytenr */
861 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
862 if (key.objectid == key.offset) {
864 * Only root blocks of reloc trees use backref
865 * pointing to itself.
867 root = find_reloc_root(rc, cur->bytenr);
873 edge = alloc_backref_edge(cache);
878 rb_node = tree_search(&cache->rb_root, key.offset);
880 upper = alloc_backref_node(cache);
882 free_backref_edge(cache, edge);
886 upper->bytenr = key.offset;
887 upper->level = cur->level + 1;
889 * backrefs for the upper level block isn't
890 * cached, add the block to pending list
892 list_add_tail(&edge->list[UPPER], &list);
894 upper = rb_entry(rb_node, struct backref_node,
896 ASSERT(upper->checked);
897 INIT_LIST_HEAD(&edge->list[UPPER]);
899 list_add_tail(&edge->list[LOWER], &cur->upper);
900 edge->node[LOWER] = cur;
901 edge->node[UPPER] = upper;
904 } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) {
906 btrfs_print_v0_err(rc->extent_root->fs_info);
907 btrfs_handle_fs_error(rc->extent_root->fs_info, err,
910 } else if (key.type != BTRFS_TREE_BLOCK_REF_KEY) {
915 * key.type == BTRFS_TREE_BLOCK_REF_KEY, inline ref offset
916 * means the root objectid. We need to search the tree to get
919 root = read_fs_root(rc->extent_root->fs_info, key.offset);
925 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
928 if (btrfs_root_level(&root->root_item) == cur->level) {
930 ASSERT(btrfs_root_bytenr(&root->root_item) ==
932 if (should_ignore_root(root)) {
933 btrfs_put_root(root);
934 list_add(&cur->list, &useless);
941 level = cur->level + 1;
943 /* Search the tree to find parent blocks referring the block. */
944 path2->search_commit_root = 1;
945 path2->skip_locking = 1;
946 path2->lowest_level = level;
947 ret = btrfs_search_slot(NULL, root, node_key, path2, 0, 0);
948 path2->lowest_level = 0;
950 btrfs_put_root(root);
954 if (ret > 0 && path2->slots[level] > 0)
955 path2->slots[level]--;
957 eb = path2->nodes[level];
958 if (btrfs_node_blockptr(eb, path2->slots[level]) !=
960 btrfs_err(root->fs_info,
961 "couldn't find block (%llu) (level %d) in tree (%llu) with key (%llu %u %llu)",
962 cur->bytenr, level - 1,
963 root->root_key.objectid,
964 node_key->objectid, node_key->type,
966 btrfs_put_root(root);
973 /* Add all nodes and edges in the path */
974 for (; level < BTRFS_MAX_LEVEL; level++) {
975 if (!path2->nodes[level]) {
976 ASSERT(btrfs_root_bytenr(&root->root_item) ==
978 if (should_ignore_root(root)) {
979 btrfs_put_root(root);
980 list_add(&lower->list, &useless);
987 edge = alloc_backref_edge(cache);
989 btrfs_put_root(root);
994 eb = path2->nodes[level];
995 rb_node = tree_search(&cache->rb_root, eb->start);
997 upper = alloc_backref_node(cache);
999 btrfs_put_root(root);
1000 free_backref_edge(cache, edge);
1004 upper->bytenr = eb->start;
1005 upper->owner = btrfs_header_owner(eb);
1006 upper->level = lower->level + 1;
1007 if (!test_bit(BTRFS_ROOT_REF_COWS,
1012 * if we know the block isn't shared
1013 * we can void checking its backrefs.
1015 if (btrfs_block_can_be_shared(root, eb))
1021 * add the block to pending list if we
1022 * need check its backrefs, we only do this once
1023 * while walking up a tree as we will catch
1024 * anything else later on.
1026 if (!upper->checked && need_check) {
1028 list_add_tail(&edge->list[UPPER],
1033 INIT_LIST_HEAD(&edge->list[UPPER]);
1036 upper = rb_entry(rb_node, struct backref_node,
1038 ASSERT(upper->checked);
1039 INIT_LIST_HEAD(&edge->list[UPPER]);
1041 upper->owner = btrfs_header_owner(eb);
1043 list_add_tail(&edge->list[LOWER], &lower->upper);
1044 edge->node[LOWER] = lower;
1045 edge->node[UPPER] = upper;
1048 btrfs_put_root(root);
1054 btrfs_release_path(path2);
1057 ptr += btrfs_extent_inline_ref_size(key.type);
1067 btrfs_release_path(path1);
1072 /* the pending list isn't empty, take the first block to process */
1073 if (!list_empty(&list)) {
1074 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
1075 list_del_init(&edge->list[UPPER]);
1076 cur = edge->node[UPPER];
1081 * everything goes well, connect backref nodes and insert backref nodes
1084 ASSERT(node->checked);
1085 cowonly = node->cowonly;
1087 rb_node = tree_insert(&cache->rb_root, node->bytenr,
1090 backref_tree_panic(rb_node, -EEXIST, node->bytenr);
1091 list_add_tail(&node->lower, &cache->leaves);
1094 list_for_each_entry(edge, &node->upper, list[LOWER])
1095 list_add_tail(&edge->list[UPPER], &list);
1097 while (!list_empty(&list)) {
1098 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
1099 list_del_init(&edge->list[UPPER]);
1100 upper = edge->node[UPPER];
1101 if (upper->detached) {
1102 list_del(&edge->list[LOWER]);
1103 lower = edge->node[LOWER];
1104 free_backref_edge(cache, edge);
1105 if (list_empty(&lower->upper))
1106 list_add(&lower->list, &useless);
1110 if (!RB_EMPTY_NODE(&upper->rb_node)) {
1111 if (upper->lowest) {
1112 list_del_init(&upper->lower);
1116 list_add_tail(&edge->list[UPPER], &upper->lower);
1120 if (!upper->checked) {
1122 * Still want to blow up for developers since this is a
1129 if (cowonly != upper->cowonly) {
1136 rb_node = tree_insert(&cache->rb_root, upper->bytenr,
1139 backref_tree_panic(rb_node, -EEXIST,
1143 list_add_tail(&edge->list[UPPER], &upper->lower);
1145 list_for_each_entry(edge, &upper->upper, list[LOWER])
1146 list_add_tail(&edge->list[UPPER], &list);
1149 * process useless backref nodes. backref nodes for tree leaves
1150 * are deleted from the cache. backref nodes for upper level
1151 * tree blocks are left in the cache to avoid unnecessary backref
1154 while (!list_empty(&useless)) {
1155 upper = list_entry(useless.next, struct backref_node, list);
1156 list_del_init(&upper->list);
1157 ASSERT(list_empty(&upper->upper));
1160 if (upper->lowest) {
1161 list_del_init(&upper->lower);
1164 while (!list_empty(&upper->lower)) {
1165 edge = list_entry(upper->lower.next,
1166 struct backref_edge, list[UPPER]);
1167 list_del(&edge->list[UPPER]);
1168 list_del(&edge->list[LOWER]);
1169 lower = edge->node[LOWER];
1170 free_backref_edge(cache, edge);
1172 if (list_empty(&lower->upper))
1173 list_add(&lower->list, &useless);
1175 __mark_block_processed(rc, upper);
1176 if (upper->level > 0) {
1177 list_add(&upper->list, &cache->detached);
1178 upper->detached = 1;
1180 rb_erase(&upper->rb_node, &cache->rb_root);
1181 free_backref_node(cache, upper);
1185 btrfs_free_path(path1);
1186 btrfs_free_path(path2);
1188 while (!list_empty(&useless)) {
1189 lower = list_entry(useless.next,
1190 struct backref_node, list);
1191 list_del_init(&lower->list);
1193 while (!list_empty(&list)) {
1194 edge = list_first_entry(&list, struct backref_edge,
1196 list_del(&edge->list[UPPER]);
1197 list_del(&edge->list[LOWER]);
1198 lower = edge->node[LOWER];
1199 upper = edge->node[UPPER];
1200 free_backref_edge(cache, edge);
1203 * Lower is no longer linked to any upper backref nodes
1204 * and isn't in the cache, we can free it ourselves.
1206 if (list_empty(&lower->upper) &&
1207 RB_EMPTY_NODE(&lower->rb_node))
1208 list_add(&lower->list, &useless);
1210 if (!RB_EMPTY_NODE(&upper->rb_node))
1213 /* Add this guy's upper edges to the list to process */
1214 list_for_each_entry(edge, &upper->upper, list[LOWER])
1215 list_add_tail(&edge->list[UPPER], &list);
1216 if (list_empty(&upper->upper))
1217 list_add(&upper->list, &useless);
1220 while (!list_empty(&useless)) {
1221 lower = list_entry(useless.next,
1222 struct backref_node, list);
1223 list_del_init(&lower->list);
1226 free_backref_node(cache, lower);
1229 remove_backref_node(cache, node);
1230 return ERR_PTR(err);
1232 ASSERT(!node || !node->detached);
1237 * helper to add backref node for the newly created snapshot.
1238 * the backref node is created by cloning backref node that
1239 * corresponds to root of source tree
1241 static int clone_backref_node(struct btrfs_trans_handle *trans,
1242 struct reloc_control *rc,
1243 struct btrfs_root *src,
1244 struct btrfs_root *dest)
1246 struct btrfs_root *reloc_root = src->reloc_root;
1247 struct backref_cache *cache = &rc->backref_cache;
1248 struct backref_node *node = NULL;
1249 struct backref_node *new_node;
1250 struct backref_edge *edge;
1251 struct backref_edge *new_edge;
1252 struct rb_node *rb_node;
1254 if (cache->last_trans > 0)
1255 update_backref_cache(trans, cache);
1257 rb_node = tree_search(&cache->rb_root, src->commit_root->start);
1259 node = rb_entry(rb_node, struct backref_node, rb_node);
1263 BUG_ON(node->new_bytenr != reloc_root->node->start);
1267 rb_node = tree_search(&cache->rb_root,
1268 reloc_root->commit_root->start);
1270 node = rb_entry(rb_node, struct backref_node,
1272 BUG_ON(node->detached);
1279 new_node = alloc_backref_node(cache);
1283 new_node->bytenr = dest->node->start;
1284 new_node->level = node->level;
1285 new_node->lowest = node->lowest;
1286 new_node->checked = 1;
1287 new_node->root = btrfs_grab_root(dest);
1288 ASSERT(new_node->root);
1290 if (!node->lowest) {
1291 list_for_each_entry(edge, &node->lower, list[UPPER]) {
1292 new_edge = alloc_backref_edge(cache);
1296 new_edge->node[UPPER] = new_node;
1297 new_edge->node[LOWER] = edge->node[LOWER];
1298 list_add_tail(&new_edge->list[UPPER],
1302 list_add_tail(&new_node->lower, &cache->leaves);
1305 rb_node = tree_insert(&cache->rb_root, new_node->bytenr,
1306 &new_node->rb_node);
1308 backref_tree_panic(rb_node, -EEXIST, new_node->bytenr);
1310 if (!new_node->lowest) {
1311 list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) {
1312 list_add_tail(&new_edge->list[LOWER],
1313 &new_edge->node[LOWER]->upper);
1318 while (!list_empty(&new_node->lower)) {
1319 new_edge = list_entry(new_node->lower.next,
1320 struct backref_edge, list[UPPER]);
1321 list_del(&new_edge->list[UPPER]);
1322 free_backref_edge(cache, new_edge);
1324 free_backref_node(cache, new_node);
1329 * helper to add 'address of tree root -> reloc tree' mapping
1331 static int __must_check __add_reloc_root(struct btrfs_root *root)
1333 struct btrfs_fs_info *fs_info = root->fs_info;
1334 struct rb_node *rb_node;
1335 struct mapping_node *node;
1336 struct reloc_control *rc = fs_info->reloc_ctl;
1338 node = kmalloc(sizeof(*node), GFP_NOFS);
1342 node->bytenr = root->commit_root->start;
1345 spin_lock(&rc->reloc_root_tree.lock);
1346 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
1347 node->bytenr, &node->rb_node);
1348 spin_unlock(&rc->reloc_root_tree.lock);
1350 btrfs_panic(fs_info, -EEXIST,
1351 "Duplicate root found for start=%llu while inserting into relocation tree",
1355 list_add_tail(&root->root_list, &rc->reloc_roots);
1360 * helper to delete the 'address of tree root -> reloc tree'
1363 static void __del_reloc_root(struct btrfs_root *root)
1365 struct btrfs_fs_info *fs_info = root->fs_info;
1366 struct rb_node *rb_node;
1367 struct mapping_node *node = NULL;
1368 struct reloc_control *rc = fs_info->reloc_ctl;
1369 bool put_ref = false;
1371 if (rc && root->node) {
1372 spin_lock(&rc->reloc_root_tree.lock);
1373 rb_node = tree_search(&rc->reloc_root_tree.rb_root,
1374 root->commit_root->start);
1376 node = rb_entry(rb_node, struct mapping_node, rb_node);
1377 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
1378 RB_CLEAR_NODE(&node->rb_node);
1380 spin_unlock(&rc->reloc_root_tree.lock);
1383 BUG_ON((struct btrfs_root *)node->data != root);
1387 * We only put the reloc root here if it's on the list. There's a lot
1388 * of places where the pattern is to splice the rc->reloc_roots, process
1389 * the reloc roots, and then add the reloc root back onto
1390 * rc->reloc_roots. If we call __del_reloc_root while it's off of the
1391 * list we don't want the reference being dropped, because the guy
1392 * messing with the list is in charge of the reference.
1394 spin_lock(&fs_info->trans_lock);
1395 if (!list_empty(&root->root_list)) {
1397 list_del_init(&root->root_list);
1399 spin_unlock(&fs_info->trans_lock);
1401 btrfs_put_root(root);
1406 * helper to update the 'address of tree root -> reloc tree'
1409 static int __update_reloc_root(struct btrfs_root *root)
1411 struct btrfs_fs_info *fs_info = root->fs_info;
1412 struct rb_node *rb_node;
1413 struct mapping_node *node = NULL;
1414 struct reloc_control *rc = fs_info->reloc_ctl;
1416 spin_lock(&rc->reloc_root_tree.lock);
1417 rb_node = tree_search(&rc->reloc_root_tree.rb_root,
1418 root->commit_root->start);
1420 node = rb_entry(rb_node, struct mapping_node, rb_node);
1421 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
1423 spin_unlock(&rc->reloc_root_tree.lock);
1427 BUG_ON((struct btrfs_root *)node->data != root);
1429 spin_lock(&rc->reloc_root_tree.lock);
1430 node->bytenr = root->node->start;
1431 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
1432 node->bytenr, &node->rb_node);
1433 spin_unlock(&rc->reloc_root_tree.lock);
1435 backref_tree_panic(rb_node, -EEXIST, node->bytenr);
1439 static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
1440 struct btrfs_root *root, u64 objectid)
1442 struct btrfs_fs_info *fs_info = root->fs_info;
1443 struct btrfs_root *reloc_root;
1444 struct extent_buffer *eb;
1445 struct btrfs_root_item *root_item;
1446 struct btrfs_key root_key;
1449 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
1452 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
1453 root_key.type = BTRFS_ROOT_ITEM_KEY;
1454 root_key.offset = objectid;
1456 if (root->root_key.objectid == objectid) {
1457 u64 commit_root_gen;
1459 /* called by btrfs_init_reloc_root */
1460 ret = btrfs_copy_root(trans, root, root->commit_root, &eb,
1461 BTRFS_TREE_RELOC_OBJECTID);
1464 * Set the last_snapshot field to the generation of the commit
1465 * root - like this ctree.c:btrfs_block_can_be_shared() behaves
1466 * correctly (returns true) when the relocation root is created
1467 * either inside the critical section of a transaction commit
1468 * (through transaction.c:qgroup_account_snapshot()) and when
1469 * it's created before the transaction commit is started.
1471 commit_root_gen = btrfs_header_generation(root->commit_root);
1472 btrfs_set_root_last_snapshot(&root->root_item, commit_root_gen);
1475 * called by btrfs_reloc_post_snapshot_hook.
1476 * the source tree is a reloc tree, all tree blocks
1477 * modified after it was created have RELOC flag
1478 * set in their headers. so it's OK to not update
1479 * the 'last_snapshot'.
1481 ret = btrfs_copy_root(trans, root, root->node, &eb,
1482 BTRFS_TREE_RELOC_OBJECTID);
1486 memcpy(root_item, &root->root_item, sizeof(*root_item));
1487 btrfs_set_root_bytenr(root_item, eb->start);
1488 btrfs_set_root_level(root_item, btrfs_header_level(eb));
1489 btrfs_set_root_generation(root_item, trans->transid);
1491 if (root->root_key.objectid == objectid) {
1492 btrfs_set_root_refs(root_item, 0);
1493 memset(&root_item->drop_progress, 0,
1494 sizeof(struct btrfs_disk_key));
1495 root_item->drop_level = 0;
1498 btrfs_tree_unlock(eb);
1499 free_extent_buffer(eb);
1501 ret = btrfs_insert_root(trans, fs_info->tree_root,
1502 &root_key, root_item);
1506 reloc_root = btrfs_read_tree_root(fs_info->tree_root, &root_key);
1507 BUG_ON(IS_ERR(reloc_root));
1508 set_bit(BTRFS_ROOT_REF_COWS, &reloc_root->state);
1509 reloc_root->last_trans = trans->transid;
1514 * create reloc tree for a given fs tree. reloc tree is just a
1515 * snapshot of the fs tree with special root objectid.
1517 * The reloc_root comes out of here with two references, one for
1518 * root->reloc_root, and another for being on the rc->reloc_roots list.
1520 int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
1521 struct btrfs_root *root)
1523 struct btrfs_fs_info *fs_info = root->fs_info;
1524 struct btrfs_root *reloc_root;
1525 struct reloc_control *rc = fs_info->reloc_ctl;
1526 struct btrfs_block_rsv *rsv;
1530 if (!rc || !rc->create_reloc_tree ||
1531 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1535 * The subvolume has reloc tree but the swap is finished, no need to
1536 * create/update the dead reloc tree
1538 if (reloc_root_is_dead(root))
1541 if (root->reloc_root) {
1542 reloc_root = root->reloc_root;
1543 reloc_root->last_trans = trans->transid;
1547 if (!trans->reloc_reserved) {
1548 rsv = trans->block_rsv;
1549 trans->block_rsv = rc->block_rsv;
1552 reloc_root = create_reloc_root(trans, root, root->root_key.objectid);
1554 trans->block_rsv = rsv;
1556 ret = __add_reloc_root(reloc_root);
1558 root->reloc_root = btrfs_grab_root(reloc_root);
1563 * update root item of reloc tree
1565 int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
1566 struct btrfs_root *root)
1568 struct btrfs_fs_info *fs_info = root->fs_info;
1569 struct btrfs_root *reloc_root;
1570 struct btrfs_root_item *root_item;
1573 if (!have_reloc_root(root))
1576 reloc_root = root->reloc_root;
1577 root_item = &reloc_root->root_item;
1580 * We are probably ok here, but __del_reloc_root() will drop its ref of
1581 * the root. We have the ref for root->reloc_root, but just in case
1582 * hold it while we update the reloc root.
1584 btrfs_grab_root(reloc_root);
1586 /* root->reloc_root will stay until current relocation finished */
1587 if (fs_info->reloc_ctl->merge_reloc_tree &&
1588 btrfs_root_refs(root_item) == 0) {
1589 set_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
1591 * Mark the tree as dead before we change reloc_root so
1592 * have_reloc_root will not touch it from now on.
1595 __del_reloc_root(reloc_root);
1598 if (reloc_root->commit_root != reloc_root->node) {
1599 __update_reloc_root(reloc_root);
1600 btrfs_set_root_node(root_item, reloc_root->node);
1601 free_extent_buffer(reloc_root->commit_root);
1602 reloc_root->commit_root = btrfs_root_node(reloc_root);
1605 ret = btrfs_update_root(trans, fs_info->tree_root,
1606 &reloc_root->root_key, root_item);
1608 btrfs_put_root(reloc_root);
1614 * helper to find first cached inode with inode number >= objectid
1617 static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid)
1619 struct rb_node *node;
1620 struct rb_node *prev;
1621 struct btrfs_inode *entry;
1622 struct inode *inode;
1624 spin_lock(&root->inode_lock);
1626 node = root->inode_tree.rb_node;
1630 entry = rb_entry(node, struct btrfs_inode, rb_node);
1632 if (objectid < btrfs_ino(entry))
1633 node = node->rb_left;
1634 else if (objectid > btrfs_ino(entry))
1635 node = node->rb_right;
1641 entry = rb_entry(prev, struct btrfs_inode, rb_node);
1642 if (objectid <= btrfs_ino(entry)) {
1646 prev = rb_next(prev);
1650 entry = rb_entry(node, struct btrfs_inode, rb_node);
1651 inode = igrab(&entry->vfs_inode);
1653 spin_unlock(&root->inode_lock);
1657 objectid = btrfs_ino(entry) + 1;
1658 if (cond_resched_lock(&root->inode_lock))
1661 node = rb_next(node);
1663 spin_unlock(&root->inode_lock);
1667 static int in_block_group(u64 bytenr, struct btrfs_block_group *block_group)
1669 if (bytenr >= block_group->start &&
1670 bytenr < block_group->start + block_group->length)
1676 * get new location of data
1678 static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr,
1679 u64 bytenr, u64 num_bytes)
1681 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
1682 struct btrfs_path *path;
1683 struct btrfs_file_extent_item *fi;
1684 struct extent_buffer *leaf;
1687 path = btrfs_alloc_path();
1691 bytenr -= BTRFS_I(reloc_inode)->index_cnt;
1692 ret = btrfs_lookup_file_extent(NULL, root, path,
1693 btrfs_ino(BTRFS_I(reloc_inode)), bytenr, 0);
1701 leaf = path->nodes[0];
1702 fi = btrfs_item_ptr(leaf, path->slots[0],
1703 struct btrfs_file_extent_item);
1705 BUG_ON(btrfs_file_extent_offset(leaf, fi) ||
1706 btrfs_file_extent_compression(leaf, fi) ||
1707 btrfs_file_extent_encryption(leaf, fi) ||
1708 btrfs_file_extent_other_encoding(leaf, fi));
1710 if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
1715 *new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1718 btrfs_free_path(path);
1723 * update file extent items in the tree leaf to point to
1724 * the new locations.
1726 static noinline_for_stack
1727 int replace_file_extents(struct btrfs_trans_handle *trans,
1728 struct reloc_control *rc,
1729 struct btrfs_root *root,
1730 struct extent_buffer *leaf)
1732 struct btrfs_fs_info *fs_info = root->fs_info;
1733 struct btrfs_key key;
1734 struct btrfs_file_extent_item *fi;
1735 struct inode *inode = NULL;
1747 if (rc->stage != UPDATE_DATA_PTRS)
1750 /* reloc trees always use full backref */
1751 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1752 parent = leaf->start;
1756 nritems = btrfs_header_nritems(leaf);
1757 for (i = 0; i < nritems; i++) {
1758 struct btrfs_ref ref = { 0 };
1761 btrfs_item_key_to_cpu(leaf, &key, i);
1762 if (key.type != BTRFS_EXTENT_DATA_KEY)
1764 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1765 if (btrfs_file_extent_type(leaf, fi) ==
1766 BTRFS_FILE_EXTENT_INLINE)
1768 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1769 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1772 if (!in_block_group(bytenr, rc->block_group))
1776 * if we are modifying block in fs tree, wait for readpage
1777 * to complete and drop the extent cache
1779 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1781 inode = find_next_inode(root, key.objectid);
1783 } else if (inode && btrfs_ino(BTRFS_I(inode)) < key.objectid) {
1784 btrfs_add_delayed_iput(inode);
1785 inode = find_next_inode(root, key.objectid);
1787 if (inode && btrfs_ino(BTRFS_I(inode)) == key.objectid) {
1789 btrfs_file_extent_num_bytes(leaf, fi);
1790 WARN_ON(!IS_ALIGNED(key.offset,
1791 fs_info->sectorsize));
1792 WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize));
1794 ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
1799 btrfs_drop_extent_cache(BTRFS_I(inode),
1800 key.offset, end, 1);
1801 unlock_extent(&BTRFS_I(inode)->io_tree,
1806 ret = get_new_location(rc->data_inode, &new_bytenr,
1810 * Don't have to abort since we've not changed anything
1811 * in the file extent yet.
1816 btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr);
1819 key.offset -= btrfs_file_extent_offset(leaf, fi);
1820 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
1822 ref.real_root = root->root_key.objectid;
1823 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
1824 key.objectid, key.offset);
1825 ret = btrfs_inc_extent_ref(trans, &ref);
1827 btrfs_abort_transaction(trans, ret);
1831 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
1833 ref.real_root = root->root_key.objectid;
1834 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
1835 key.objectid, key.offset);
1836 ret = btrfs_free_extent(trans, &ref);
1838 btrfs_abort_transaction(trans, ret);
1843 btrfs_mark_buffer_dirty(leaf);
1845 btrfs_add_delayed_iput(inode);
1849 static noinline_for_stack
1850 int memcmp_node_keys(struct extent_buffer *eb, int slot,
1851 struct btrfs_path *path, int level)
1853 struct btrfs_disk_key key1;
1854 struct btrfs_disk_key key2;
1855 btrfs_node_key(eb, &key1, slot);
1856 btrfs_node_key(path->nodes[level], &key2, path->slots[level]);
1857 return memcmp(&key1, &key2, sizeof(key1));
1861 * try to replace tree blocks in fs tree with the new blocks
1862 * in reloc tree. tree blocks haven't been modified since the
1863 * reloc tree was create can be replaced.
1865 * if a block was replaced, level of the block + 1 is returned.
1866 * if no block got replaced, 0 is returned. if there are other
1867 * errors, a negative error number is returned.
1869 static noinline_for_stack
1870 int replace_path(struct btrfs_trans_handle *trans, struct reloc_control *rc,
1871 struct btrfs_root *dest, struct btrfs_root *src,
1872 struct btrfs_path *path, struct btrfs_key *next_key,
1873 int lowest_level, int max_level)
1875 struct btrfs_fs_info *fs_info = dest->fs_info;
1876 struct extent_buffer *eb;
1877 struct extent_buffer *parent;
1878 struct btrfs_ref ref = { 0 };
1879 struct btrfs_key key;
1891 BUG_ON(src->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
1892 BUG_ON(dest->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID);
1894 last_snapshot = btrfs_root_last_snapshot(&src->root_item);
1896 slot = path->slots[lowest_level];
1897 btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot);
1899 eb = btrfs_lock_root_node(dest);
1900 btrfs_set_lock_blocking_write(eb);
1901 level = btrfs_header_level(eb);
1903 if (level < lowest_level) {
1904 btrfs_tree_unlock(eb);
1905 free_extent_buffer(eb);
1910 ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb);
1913 btrfs_set_lock_blocking_write(eb);
1916 next_key->objectid = (u64)-1;
1917 next_key->type = (u8)-1;
1918 next_key->offset = (u64)-1;
1923 struct btrfs_key first_key;
1925 level = btrfs_header_level(parent);
1926 BUG_ON(level < lowest_level);
1928 ret = btrfs_bin_search(parent, &key, level, &slot);
1931 if (ret && slot > 0)
1934 if (next_key && slot + 1 < btrfs_header_nritems(parent))
1935 btrfs_node_key_to_cpu(parent, next_key, slot + 1);
1937 old_bytenr = btrfs_node_blockptr(parent, slot);
1938 blocksize = fs_info->nodesize;
1939 old_ptr_gen = btrfs_node_ptr_generation(parent, slot);
1940 btrfs_node_key_to_cpu(parent, &first_key, slot);
1942 if (level <= max_level) {
1943 eb = path->nodes[level];
1944 new_bytenr = btrfs_node_blockptr(eb,
1945 path->slots[level]);
1946 new_ptr_gen = btrfs_node_ptr_generation(eb,
1947 path->slots[level]);
1953 if (WARN_ON(new_bytenr > 0 && new_bytenr == old_bytenr)) {
1958 if (new_bytenr == 0 || old_ptr_gen > last_snapshot ||
1959 memcmp_node_keys(parent, slot, path, level)) {
1960 if (level <= lowest_level) {
1965 eb = read_tree_block(fs_info, old_bytenr, old_ptr_gen,
1966 level - 1, &first_key);
1970 } else if (!extent_buffer_uptodate(eb)) {
1972 free_extent_buffer(eb);
1975 btrfs_tree_lock(eb);
1977 ret = btrfs_cow_block(trans, dest, eb, parent,
1981 btrfs_set_lock_blocking_write(eb);
1983 btrfs_tree_unlock(parent);
1984 free_extent_buffer(parent);
1991 btrfs_tree_unlock(parent);
1992 free_extent_buffer(parent);
1997 btrfs_node_key_to_cpu(path->nodes[level], &key,
1998 path->slots[level]);
1999 btrfs_release_path(path);
2001 path->lowest_level = level;
2002 ret = btrfs_search_slot(trans, src, &key, path, 0, 1);
2003 path->lowest_level = 0;
2007 * Info qgroup to trace both subtrees.
2009 * We must trace both trees.
2010 * 1) Tree reloc subtree
2011 * If not traced, we will leak data numbers
2013 * If not traced, we will double count old data
2015 * We don't scan the subtree right now, but only record
2016 * the swapped tree blocks.
2017 * The real subtree rescan is delayed until we have new
2018 * CoW on the subtree root node before transaction commit.
2020 ret = btrfs_qgroup_add_swapped_blocks(trans, dest,
2021 rc->block_group, parent, slot,
2022 path->nodes[level], path->slots[level],
2027 * swap blocks in fs tree and reloc tree.
2029 btrfs_set_node_blockptr(parent, slot, new_bytenr);
2030 btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen);
2031 btrfs_mark_buffer_dirty(parent);
2033 btrfs_set_node_blockptr(path->nodes[level],
2034 path->slots[level], old_bytenr);
2035 btrfs_set_node_ptr_generation(path->nodes[level],
2036 path->slots[level], old_ptr_gen);
2037 btrfs_mark_buffer_dirty(path->nodes[level]);
2039 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, old_bytenr,
2040 blocksize, path->nodes[level]->start);
2041 ref.skip_qgroup = true;
2042 btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid);
2043 ret = btrfs_inc_extent_ref(trans, &ref);
2045 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new_bytenr,
2047 ref.skip_qgroup = true;
2048 btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid);
2049 ret = btrfs_inc_extent_ref(trans, &ref);
2052 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, new_bytenr,
2053 blocksize, path->nodes[level]->start);
2054 btrfs_init_tree_ref(&ref, level - 1, src->root_key.objectid);
2055 ref.skip_qgroup = true;
2056 ret = btrfs_free_extent(trans, &ref);
2059 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, old_bytenr,
2061 btrfs_init_tree_ref(&ref, level - 1, dest->root_key.objectid);
2062 ref.skip_qgroup = true;
2063 ret = btrfs_free_extent(trans, &ref);
2066 btrfs_unlock_up_safe(path, 0);
2071 btrfs_tree_unlock(parent);
2072 free_extent_buffer(parent);
2077 * helper to find next relocated block in reloc tree
2079 static noinline_for_stack
2080 int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
2083 struct extent_buffer *eb;
2088 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2090 for (i = 0; i < *level; i++) {
2091 free_extent_buffer(path->nodes[i]);
2092 path->nodes[i] = NULL;
2095 for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
2096 eb = path->nodes[i];
2097 nritems = btrfs_header_nritems(eb);
2098 while (path->slots[i] + 1 < nritems) {
2100 if (btrfs_node_ptr_generation(eb, path->slots[i]) <=
2107 free_extent_buffer(path->nodes[i]);
2108 path->nodes[i] = NULL;
2114 * walk down reloc tree to find relocated block of lowest level
2116 static noinline_for_stack
2117 int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
2120 struct btrfs_fs_info *fs_info = root->fs_info;
2121 struct extent_buffer *eb = NULL;
2128 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2130 for (i = *level; i > 0; i--) {
2131 struct btrfs_key first_key;
2133 eb = path->nodes[i];
2134 nritems = btrfs_header_nritems(eb);
2135 while (path->slots[i] < nritems) {
2136 ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]);
2137 if (ptr_gen > last_snapshot)
2141 if (path->slots[i] >= nritems) {
2152 bytenr = btrfs_node_blockptr(eb, path->slots[i]);
2153 btrfs_node_key_to_cpu(eb, &first_key, path->slots[i]);
2154 eb = read_tree_block(fs_info, bytenr, ptr_gen, i - 1,
2158 } else if (!extent_buffer_uptodate(eb)) {
2159 free_extent_buffer(eb);
2162 BUG_ON(btrfs_header_level(eb) != i - 1);
2163 path->nodes[i - 1] = eb;
2164 path->slots[i - 1] = 0;
2170 * invalidate extent cache for file extents whose key in range of
2171 * [min_key, max_key)
2173 static int invalidate_extent_cache(struct btrfs_root *root,
2174 struct btrfs_key *min_key,
2175 struct btrfs_key *max_key)
2177 struct btrfs_fs_info *fs_info = root->fs_info;
2178 struct inode *inode = NULL;
2183 objectid = min_key->objectid;
2188 if (objectid > max_key->objectid)
2191 inode = find_next_inode(root, objectid);
2194 ino = btrfs_ino(BTRFS_I(inode));
2196 if (ino > max_key->objectid) {
2202 if (!S_ISREG(inode->i_mode))
2205 if (unlikely(min_key->objectid == ino)) {
2206 if (min_key->type > BTRFS_EXTENT_DATA_KEY)
2208 if (min_key->type < BTRFS_EXTENT_DATA_KEY)
2211 start = min_key->offset;
2212 WARN_ON(!IS_ALIGNED(start, fs_info->sectorsize));
2218 if (unlikely(max_key->objectid == ino)) {
2219 if (max_key->type < BTRFS_EXTENT_DATA_KEY)
2221 if (max_key->type > BTRFS_EXTENT_DATA_KEY) {
2224 if (max_key->offset == 0)
2226 end = max_key->offset;
2227 WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize));
2234 /* the lock_extent waits for readpage to complete */
2235 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
2236 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 1);
2237 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
2242 static int find_next_key(struct btrfs_path *path, int level,
2243 struct btrfs_key *key)
2246 while (level < BTRFS_MAX_LEVEL) {
2247 if (!path->nodes[level])
2249 if (path->slots[level] + 1 <
2250 btrfs_header_nritems(path->nodes[level])) {
2251 btrfs_node_key_to_cpu(path->nodes[level], key,
2252 path->slots[level] + 1);
2261 * Insert current subvolume into reloc_control::dirty_subvol_roots
2263 static void insert_dirty_subvol(struct btrfs_trans_handle *trans,
2264 struct reloc_control *rc,
2265 struct btrfs_root *root)
2267 struct btrfs_root *reloc_root = root->reloc_root;
2268 struct btrfs_root_item *reloc_root_item;
2270 /* @root must be a subvolume tree root with a valid reloc tree */
2271 ASSERT(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
2274 reloc_root_item = &reloc_root->root_item;
2275 memset(&reloc_root_item->drop_progress, 0,
2276 sizeof(reloc_root_item->drop_progress));
2277 reloc_root_item->drop_level = 0;
2278 btrfs_set_root_refs(reloc_root_item, 0);
2279 btrfs_update_reloc_root(trans, root);
2281 if (list_empty(&root->reloc_dirty_list)) {
2282 btrfs_grab_root(root);
2283 list_add_tail(&root->reloc_dirty_list, &rc->dirty_subvol_roots);
2287 static int clean_dirty_subvols(struct reloc_control *rc)
2289 struct btrfs_root *root;
2290 struct btrfs_root *next;
2294 list_for_each_entry_safe(root, next, &rc->dirty_subvol_roots,
2296 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2297 /* Merged subvolume, cleanup its reloc root */
2298 struct btrfs_root *reloc_root = root->reloc_root;
2300 list_del_init(&root->reloc_dirty_list);
2301 root->reloc_root = NULL;
2303 * Need barrier to ensure clear_bit() only happens after
2304 * root->reloc_root = NULL. Pairs with have_reloc_root.
2307 clear_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state);
2310 * btrfs_drop_snapshot drops our ref we hold for
2311 * ->reloc_root. If it fails however we must
2312 * drop the ref ourselves.
2314 ret2 = btrfs_drop_snapshot(reloc_root, 0, 1);
2316 btrfs_put_root(reloc_root);
2321 btrfs_put_root(root);
2323 /* Orphan reloc tree, just clean it up */
2324 ret2 = btrfs_drop_snapshot(root, 0, 1);
2326 btrfs_put_root(root);
2336 * merge the relocated tree blocks in reloc tree with corresponding
2339 static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
2340 struct btrfs_root *root)
2342 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2343 struct btrfs_key key;
2344 struct btrfs_key next_key;
2345 struct btrfs_trans_handle *trans = NULL;
2346 struct btrfs_root *reloc_root;
2347 struct btrfs_root_item *root_item;
2348 struct btrfs_path *path;
2349 struct extent_buffer *leaf;
2357 path = btrfs_alloc_path();
2360 path->reada = READA_FORWARD;
2362 reloc_root = root->reloc_root;
2363 root_item = &reloc_root->root_item;
2365 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2366 level = btrfs_root_level(root_item);
2367 atomic_inc(&reloc_root->node->refs);
2368 path->nodes[level] = reloc_root->node;
2369 path->slots[level] = 0;
2371 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2373 level = root_item->drop_level;
2375 path->lowest_level = level;
2376 ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0);
2377 path->lowest_level = 0;
2379 btrfs_free_path(path);
2383 btrfs_node_key_to_cpu(path->nodes[level], &next_key,
2384 path->slots[level]);
2385 WARN_ON(memcmp(&key, &next_key, sizeof(key)));
2387 btrfs_unlock_up_safe(path, 0);
2390 min_reserved = fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
2391 memset(&next_key, 0, sizeof(next_key));
2394 ret = btrfs_block_rsv_refill(root, rc->block_rsv, min_reserved,
2395 BTRFS_RESERVE_FLUSH_ALL);
2400 trans = btrfs_start_transaction(root, 0);
2401 if (IS_ERR(trans)) {
2402 err = PTR_ERR(trans);
2408 * At this point we no longer have a reloc_control, so we can't
2409 * depend on btrfs_init_reloc_root to update our last_trans.
2411 * But that's ok, we started the trans handle on our
2412 * corresponding fs_root, which means it's been added to the
2413 * dirty list. At commit time we'll still call
2414 * btrfs_update_reloc_root() and update our root item
2417 reloc_root->last_trans = trans->transid;
2418 trans->block_rsv = rc->block_rsv;
2423 ret = walk_down_reloc_tree(reloc_root, path, &level);
2431 if (!find_next_key(path, level, &key) &&
2432 btrfs_comp_cpu_keys(&next_key, &key) >= 0) {
2435 ret = replace_path(trans, rc, root, reloc_root, path,
2436 &next_key, level, max_level);
2445 btrfs_node_key_to_cpu(path->nodes[level], &key,
2446 path->slots[level]);
2450 ret = walk_up_reloc_tree(reloc_root, path, &level);
2456 * save the merging progress in the drop_progress.
2457 * this is OK since root refs == 1 in this case.
2459 btrfs_node_key(path->nodes[level], &root_item->drop_progress,
2460 path->slots[level]);
2461 root_item->drop_level = level;
2463 btrfs_end_transaction_throttle(trans);
2466 btrfs_btree_balance_dirty(fs_info);
2468 if (replaced && rc->stage == UPDATE_DATA_PTRS)
2469 invalidate_extent_cache(root, &key, &next_key);
2473 * handle the case only one block in the fs tree need to be
2474 * relocated and the block is tree root.
2476 leaf = btrfs_lock_root_node(root);
2477 ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf);
2478 btrfs_tree_unlock(leaf);
2479 free_extent_buffer(leaf);
2483 btrfs_free_path(path);
2486 insert_dirty_subvol(trans, rc, root);
2489 btrfs_end_transaction_throttle(trans);
2491 btrfs_btree_balance_dirty(fs_info);
2493 if (replaced && rc->stage == UPDATE_DATA_PTRS)
2494 invalidate_extent_cache(root, &key, &next_key);
2499 static noinline_for_stack
2500 int prepare_to_merge(struct reloc_control *rc, int err)
2502 struct btrfs_root *root = rc->extent_root;
2503 struct btrfs_fs_info *fs_info = root->fs_info;
2504 struct btrfs_root *reloc_root;
2505 struct btrfs_trans_handle *trans;
2506 LIST_HEAD(reloc_roots);
2510 mutex_lock(&fs_info->reloc_mutex);
2511 rc->merging_rsv_size += fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
2512 rc->merging_rsv_size += rc->nodes_relocated * 2;
2513 mutex_unlock(&fs_info->reloc_mutex);
2517 num_bytes = rc->merging_rsv_size;
2518 ret = btrfs_block_rsv_add(root, rc->block_rsv, num_bytes,
2519 BTRFS_RESERVE_FLUSH_ALL);
2524 trans = btrfs_join_transaction(rc->extent_root);
2525 if (IS_ERR(trans)) {
2527 btrfs_block_rsv_release(fs_info, rc->block_rsv,
2529 return PTR_ERR(trans);
2533 if (num_bytes != rc->merging_rsv_size) {
2534 btrfs_end_transaction(trans);
2535 btrfs_block_rsv_release(fs_info, rc->block_rsv,
2541 rc->merge_reloc_tree = 1;
2543 while (!list_empty(&rc->reloc_roots)) {
2544 reloc_root = list_entry(rc->reloc_roots.next,
2545 struct btrfs_root, root_list);
2546 list_del_init(&reloc_root->root_list);
2548 root = read_fs_root(fs_info, reloc_root->root_key.offset);
2549 BUG_ON(IS_ERR(root));
2550 BUG_ON(root->reloc_root != reloc_root);
2553 * set reference count to 1, so btrfs_recover_relocation
2554 * knows it should resumes merging
2557 btrfs_set_root_refs(&reloc_root->root_item, 1);
2558 btrfs_update_reloc_root(trans, root);
2560 list_add(&reloc_root->root_list, &reloc_roots);
2561 btrfs_put_root(root);
2564 list_splice(&reloc_roots, &rc->reloc_roots);
2567 btrfs_commit_transaction(trans);
2569 btrfs_end_transaction(trans);
2573 static noinline_for_stack
2574 void free_reloc_roots(struct list_head *list)
2576 struct btrfs_root *reloc_root;
2578 while (!list_empty(list)) {
2579 reloc_root = list_entry(list->next, struct btrfs_root,
2581 __del_reloc_root(reloc_root);
2585 static noinline_for_stack
2586 void merge_reloc_roots(struct reloc_control *rc)
2588 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2589 struct btrfs_root *root;
2590 struct btrfs_root *reloc_root;
2591 LIST_HEAD(reloc_roots);
2595 root = rc->extent_root;
2598 * this serializes us with btrfs_record_root_in_transaction,
2599 * we have to make sure nobody is in the middle of
2600 * adding their roots to the list while we are
2603 mutex_lock(&fs_info->reloc_mutex);
2604 list_splice_init(&rc->reloc_roots, &reloc_roots);
2605 mutex_unlock(&fs_info->reloc_mutex);
2607 while (!list_empty(&reloc_roots)) {
2609 reloc_root = list_entry(reloc_roots.next,
2610 struct btrfs_root, root_list);
2612 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
2613 root = read_fs_root(fs_info,
2614 reloc_root->root_key.offset);
2615 BUG_ON(IS_ERR(root));
2616 BUG_ON(root->reloc_root != reloc_root);
2618 ret = merge_reloc_root(rc, root);
2619 btrfs_put_root(root);
2621 if (list_empty(&reloc_root->root_list))
2622 list_add_tail(&reloc_root->root_list,
2627 list_del_init(&reloc_root->root_list);
2628 /* Don't forget to queue this reloc root for cleanup */
2629 list_add_tail(&reloc_root->reloc_dirty_list,
2630 &rc->dirty_subvol_roots);
2640 btrfs_handle_fs_error(fs_info, ret, NULL);
2641 if (!list_empty(&reloc_roots))
2642 free_reloc_roots(&reloc_roots);
2644 /* new reloc root may be added */
2645 mutex_lock(&fs_info->reloc_mutex);
2646 list_splice_init(&rc->reloc_roots, &reloc_roots);
2647 mutex_unlock(&fs_info->reloc_mutex);
2648 if (!list_empty(&reloc_roots))
2649 free_reloc_roots(&reloc_roots);
2655 * BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
2657 * here, but it's wrong. If we fail to start the transaction in
2658 * prepare_to_merge() we will have only 0 ref reloc roots, none of which
2659 * have actually been removed from the reloc_root_tree rb tree. This is
2660 * fine because we're bailing here, and we hold a reference on the root
2661 * for the list that holds it, so these roots will be cleaned up when we
2662 * do the reloc_dirty_list afterwards. Meanwhile the root->reloc_root
2663 * will be cleaned up on unmount.
2665 * The remaining nodes will be cleaned up by free_reloc_control.
2669 static void free_block_list(struct rb_root *blocks)
2671 struct tree_block *block;
2672 struct rb_node *rb_node;
2673 while ((rb_node = rb_first(blocks))) {
2674 block = rb_entry(rb_node, struct tree_block, rb_node);
2675 rb_erase(rb_node, blocks);
2680 static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans,
2681 struct btrfs_root *reloc_root)
2683 struct btrfs_fs_info *fs_info = reloc_root->fs_info;
2684 struct btrfs_root *root;
2687 if (reloc_root->last_trans == trans->transid)
2690 root = read_fs_root(fs_info, reloc_root->root_key.offset);
2691 BUG_ON(IS_ERR(root));
2692 BUG_ON(root->reloc_root != reloc_root);
2693 ret = btrfs_record_root_in_trans(trans, root);
2694 btrfs_put_root(root);
2699 static noinline_for_stack
2700 struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,
2701 struct reloc_control *rc,
2702 struct backref_node *node,
2703 struct backref_edge *edges[])
2705 struct backref_node *next;
2706 struct btrfs_root *root;
2712 next = walk_up_backref(next, edges, &index);
2715 BUG_ON(!test_bit(BTRFS_ROOT_REF_COWS, &root->state));
2717 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
2718 record_reloc_root_in_trans(trans, root);
2722 btrfs_record_root_in_trans(trans, root);
2723 root = root->reloc_root;
2725 if (next->new_bytenr != root->node->start) {
2726 BUG_ON(next->new_bytenr);
2727 BUG_ON(!list_empty(&next->list));
2728 next->new_bytenr = root->node->start;
2729 btrfs_put_root(next->root);
2730 next->root = btrfs_grab_root(root);
2732 list_add_tail(&next->list,
2733 &rc->backref_cache.changed);
2734 __mark_block_processed(rc, next);
2740 next = walk_down_backref(edges, &index);
2741 if (!next || next->level <= node->level)
2748 /* setup backref node path for btrfs_reloc_cow_block */
2750 rc->backref_cache.path[next->level] = next;
2753 next = edges[index]->node[UPPER];
2759 * select a tree root for relocation. return NULL if the block
2760 * is reference counted. we should use do_relocation() in this
2761 * case. return a tree root pointer if the block isn't reference
2762 * counted. return -ENOENT if the block is root of reloc tree.
2764 static noinline_for_stack
2765 struct btrfs_root *select_one_root(struct backref_node *node)
2767 struct backref_node *next;
2768 struct btrfs_root *root;
2769 struct btrfs_root *fs_root = NULL;
2770 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2776 next = walk_up_backref(next, edges, &index);
2780 /* no other choice for non-references counted tree */
2781 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
2784 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)
2790 next = walk_down_backref(edges, &index);
2791 if (!next || next->level <= node->level)
2796 return ERR_PTR(-ENOENT);
2800 static noinline_for_stack
2801 u64 calcu_metadata_size(struct reloc_control *rc,
2802 struct backref_node *node, int reserve)
2804 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2805 struct backref_node *next = node;
2806 struct backref_edge *edge;
2807 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2811 BUG_ON(reserve && node->processed);
2816 if (next->processed && (reserve || next != node))
2819 num_bytes += fs_info->nodesize;
2821 if (list_empty(&next->upper))
2824 edge = list_entry(next->upper.next,
2825 struct backref_edge, list[LOWER]);
2826 edges[index++] = edge;
2827 next = edge->node[UPPER];
2829 next = walk_down_backref(edges, &index);
2834 static int reserve_metadata_space(struct btrfs_trans_handle *trans,
2835 struct reloc_control *rc,
2836 struct backref_node *node)
2838 struct btrfs_root *root = rc->extent_root;
2839 struct btrfs_fs_info *fs_info = root->fs_info;
2844 num_bytes = calcu_metadata_size(rc, node, 1) * 2;
2846 trans->block_rsv = rc->block_rsv;
2847 rc->reserved_bytes += num_bytes;
2850 * We are under a transaction here so we can only do limited flushing.
2851 * If we get an enospc just kick back -EAGAIN so we know to drop the
2852 * transaction and try to refill when we can flush all the things.
2854 ret = btrfs_block_rsv_refill(root, rc->block_rsv, num_bytes,
2855 BTRFS_RESERVE_FLUSH_LIMIT);
2857 tmp = fs_info->nodesize * RELOCATION_RESERVED_NODES;
2858 while (tmp <= rc->reserved_bytes)
2861 * only one thread can access block_rsv at this point,
2862 * so we don't need hold lock to protect block_rsv.
2863 * we expand more reservation size here to allow enough
2864 * space for relocation and we will return earlier in
2867 rc->block_rsv->size = tmp + fs_info->nodesize *
2868 RELOCATION_RESERVED_NODES;
2876 * relocate a block tree, and then update pointers in upper level
2877 * blocks that reference the block to point to the new location.
2879 * if called by link_to_upper, the block has already been relocated.
2880 * in that case this function just updates pointers.
2882 static int do_relocation(struct btrfs_trans_handle *trans,
2883 struct reloc_control *rc,
2884 struct backref_node *node,
2885 struct btrfs_key *key,
2886 struct btrfs_path *path, int lowest)
2888 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2889 struct backref_node *upper;
2890 struct backref_edge *edge;
2891 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2892 struct btrfs_root *root;
2893 struct extent_buffer *eb;
2901 BUG_ON(lowest && node->eb);
2903 path->lowest_level = node->level + 1;
2904 rc->backref_cache.path[node->level] = node;
2905 list_for_each_entry(edge, &node->upper, list[LOWER]) {
2906 struct btrfs_key first_key;
2907 struct btrfs_ref ref = { 0 };
2911 upper = edge->node[UPPER];
2912 root = select_reloc_root(trans, rc, upper, edges);
2915 if (upper->eb && !upper->locked) {
2917 ret = btrfs_bin_search(upper->eb, key,
2918 upper->level, &slot);
2924 bytenr = btrfs_node_blockptr(upper->eb, slot);
2925 if (node->eb->start == bytenr)
2928 drop_node_buffer(upper);
2932 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2939 btrfs_release_path(path);
2944 upper->eb = path->nodes[upper->level];
2945 path->nodes[upper->level] = NULL;
2947 BUG_ON(upper->eb != path->nodes[upper->level]);
2951 path->locks[upper->level] = 0;
2953 slot = path->slots[upper->level];
2954 btrfs_release_path(path);
2956 ret = btrfs_bin_search(upper->eb, key, upper->level,
2965 bytenr = btrfs_node_blockptr(upper->eb, slot);
2967 if (bytenr != node->bytenr) {
2968 btrfs_err(root->fs_info,
2969 "lowest leaf/node mismatch: bytenr %llu node->bytenr %llu slot %d upper %llu",
2970 bytenr, node->bytenr, slot,
2976 if (node->eb->start == bytenr)
2980 blocksize = root->fs_info->nodesize;
2981 generation = btrfs_node_ptr_generation(upper->eb, slot);
2982 btrfs_node_key_to_cpu(upper->eb, &first_key, slot);
2983 eb = read_tree_block(fs_info, bytenr, generation,
2984 upper->level - 1, &first_key);
2988 } else if (!extent_buffer_uptodate(eb)) {
2989 free_extent_buffer(eb);
2993 btrfs_tree_lock(eb);
2994 btrfs_set_lock_blocking_write(eb);
2997 ret = btrfs_cow_block(trans, root, eb, upper->eb,
2999 btrfs_tree_unlock(eb);
3000 free_extent_buffer(eb);
3005 BUG_ON(node->eb != eb);
3007 btrfs_set_node_blockptr(upper->eb, slot,
3009 btrfs_set_node_ptr_generation(upper->eb, slot,
3011 btrfs_mark_buffer_dirty(upper->eb);
3013 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF,
3014 node->eb->start, blocksize,
3016 ref.real_root = root->root_key.objectid;
3017 btrfs_init_tree_ref(&ref, node->level,
3018 btrfs_header_owner(upper->eb));
3019 ret = btrfs_inc_extent_ref(trans, &ref);
3022 ret = btrfs_drop_subtree(trans, root, eb, upper->eb);
3026 if (!upper->pending)
3027 drop_node_buffer(upper);
3029 unlock_node_buffer(upper);
3034 if (!err && node->pending) {
3035 drop_node_buffer(node);
3036 list_move_tail(&node->list, &rc->backref_cache.changed);
3040 path->lowest_level = 0;
3041 BUG_ON(err == -ENOSPC);
3045 static int link_to_upper(struct btrfs_trans_handle *trans,
3046 struct reloc_control *rc,
3047 struct backref_node *node,
3048 struct btrfs_path *path)
3050 struct btrfs_key key;
3052 btrfs_node_key_to_cpu(node->eb, &key, 0);
3053 return do_relocation(trans, rc, node, &key, path, 0);
3056 static int finish_pending_nodes(struct btrfs_trans_handle *trans,
3057 struct reloc_control *rc,
3058 struct btrfs_path *path, int err)
3061 struct backref_cache *cache = &rc->backref_cache;
3062 struct backref_node *node;
3066 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
3067 while (!list_empty(&cache->pending[level])) {
3068 node = list_entry(cache->pending[level].next,
3069 struct backref_node, list);
3070 list_move_tail(&node->list, &list);
3071 BUG_ON(!node->pending);
3074 ret = link_to_upper(trans, rc, node, path);
3079 list_splice_init(&list, &cache->pending[level]);
3084 static void mark_block_processed(struct reloc_control *rc,
3085 u64 bytenr, u32 blocksize)
3087 set_extent_bits(&rc->processed_blocks, bytenr, bytenr + blocksize - 1,
3091 static void __mark_block_processed(struct reloc_control *rc,
3092 struct backref_node *node)
3095 if (node->level == 0 ||
3096 in_block_group(node->bytenr, rc->block_group)) {
3097 blocksize = rc->extent_root->fs_info->nodesize;
3098 mark_block_processed(rc, node->bytenr, blocksize);
3100 node->processed = 1;
3104 * mark a block and all blocks directly/indirectly reference the block
3107 static void update_processed_blocks(struct reloc_control *rc,
3108 struct backref_node *node)
3110 struct backref_node *next = node;
3111 struct backref_edge *edge;
3112 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
3118 if (next->processed)
3121 __mark_block_processed(rc, next);
3123 if (list_empty(&next->upper))
3126 edge = list_entry(next->upper.next,
3127 struct backref_edge, list[LOWER]);
3128 edges[index++] = edge;
3129 next = edge->node[UPPER];
3131 next = walk_down_backref(edges, &index);
3135 static int tree_block_processed(u64 bytenr, struct reloc_control *rc)
3137 u32 blocksize = rc->extent_root->fs_info->nodesize;
3139 if (test_range_bit(&rc->processed_blocks, bytenr,
3140 bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL))
3145 static int get_tree_block_key(struct btrfs_fs_info *fs_info,
3146 struct tree_block *block)
3148 struct extent_buffer *eb;
3150 eb = read_tree_block(fs_info, block->bytenr, block->key.offset,
3151 block->level, NULL);
3154 } else if (!extent_buffer_uptodate(eb)) {
3155 free_extent_buffer(eb);
3158 if (block->level == 0)
3159 btrfs_item_key_to_cpu(eb, &block->key, 0);
3161 btrfs_node_key_to_cpu(eb, &block->key, 0);
3162 free_extent_buffer(eb);
3163 block->key_ready = 1;
3168 * helper function to relocate a tree block
3170 static int relocate_tree_block(struct btrfs_trans_handle *trans,
3171 struct reloc_control *rc,
3172 struct backref_node *node,
3173 struct btrfs_key *key,
3174 struct btrfs_path *path)
3176 struct btrfs_root *root;
3183 * If we fail here we want to drop our backref_node because we are going
3184 * to start over and regenerate the tree for it.
3186 ret = reserve_metadata_space(trans, rc, node);
3190 BUG_ON(node->processed);
3191 root = select_one_root(node);
3192 if (root == ERR_PTR(-ENOENT)) {
3193 update_processed_blocks(rc, node);
3198 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
3199 BUG_ON(node->new_bytenr);
3200 BUG_ON(!list_empty(&node->list));
3201 btrfs_record_root_in_trans(trans, root);
3202 root = root->reloc_root;
3203 node->new_bytenr = root->node->start;
3204 btrfs_put_root(node->root);
3205 node->root = btrfs_grab_root(root);
3207 list_add_tail(&node->list, &rc->backref_cache.changed);
3209 path->lowest_level = node->level;
3210 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
3211 btrfs_release_path(path);
3216 update_processed_blocks(rc, node);
3218 ret = do_relocation(trans, rc, node, key, path, 1);
3221 if (ret || node->level == 0 || node->cowonly)
3222 remove_backref_node(&rc->backref_cache, node);
3227 * relocate a list of blocks
3229 static noinline_for_stack
3230 int relocate_tree_blocks(struct btrfs_trans_handle *trans,
3231 struct reloc_control *rc, struct rb_root *blocks)
3233 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3234 struct backref_node *node;
3235 struct btrfs_path *path;
3236 struct tree_block *block;
3237 struct tree_block *next;
3241 path = btrfs_alloc_path();
3244 goto out_free_blocks;
3247 /* Kick in readahead for tree blocks with missing keys */
3248 rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
3249 if (!block->key_ready)
3250 readahead_tree_block(fs_info, block->bytenr);
3253 /* Get first keys */
3254 rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
3255 if (!block->key_ready) {
3256 err = get_tree_block_key(fs_info, block);
3262 /* Do tree relocation */
3263 rbtree_postorder_for_each_entry_safe(block, next, blocks, rb_node) {
3264 node = build_backref_tree(rc, &block->key,
3265 block->level, block->bytenr);
3267 err = PTR_ERR(node);
3271 ret = relocate_tree_block(trans, rc, node, &block->key,
3279 err = finish_pending_nodes(trans, rc, path, err);
3282 btrfs_free_path(path);
3284 free_block_list(blocks);
3288 static noinline_for_stack
3289 int prealloc_file_extent_cluster(struct inode *inode,
3290 struct file_extent_cluster *cluster)
3295 u64 offset = BTRFS_I(inode)->index_cnt;
3299 u64 prealloc_start = cluster->start - offset;
3300 u64 prealloc_end = cluster->end - offset;
3302 struct extent_changeset *data_reserved = NULL;
3304 BUG_ON(cluster->start != cluster->boundary[0]);
3307 ret = btrfs_check_data_free_space(inode, &data_reserved, prealloc_start,
3308 prealloc_end + 1 - prealloc_start);
3312 cur_offset = prealloc_start;
3313 while (nr < cluster->nr) {
3314 start = cluster->boundary[nr] - offset;
3315 if (nr + 1 < cluster->nr)
3316 end = cluster->boundary[nr + 1] - 1 - offset;
3318 end = cluster->end - offset;
3320 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
3321 num_bytes = end + 1 - start;
3322 if (cur_offset < start)
3323 btrfs_free_reserved_data_space(inode, data_reserved,
3324 cur_offset, start - cur_offset);
3325 ret = btrfs_prealloc_file_range(inode, 0, start,
3326 num_bytes, num_bytes,
3327 end + 1, &alloc_hint);
3328 cur_offset = end + 1;
3329 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
3334 if (cur_offset < prealloc_end)
3335 btrfs_free_reserved_data_space(inode, data_reserved,
3336 cur_offset, prealloc_end + 1 - cur_offset);
3338 inode_unlock(inode);
3339 extent_changeset_free(data_reserved);
3343 static noinline_for_stack
3344 int setup_extent_mapping(struct inode *inode, u64 start, u64 end,
3347 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3348 struct extent_map *em;
3351 em = alloc_extent_map();
3356 em->len = end + 1 - start;
3357 em->block_len = em->len;
3358 em->block_start = block_start;
3359 set_bit(EXTENT_FLAG_PINNED, &em->flags);
3361 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
3363 write_lock(&em_tree->lock);
3364 ret = add_extent_mapping(em_tree, em, 0);
3365 write_unlock(&em_tree->lock);
3366 if (ret != -EEXIST) {
3367 free_extent_map(em);
3370 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
3372 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
3377 * Allow error injection to test balance cancellation
3379 int btrfs_should_cancel_balance(struct btrfs_fs_info *fs_info)
3381 return atomic_read(&fs_info->balance_cancel_req);
3383 ALLOW_ERROR_INJECTION(btrfs_should_cancel_balance, TRUE);
3385 static int relocate_file_extent_cluster(struct inode *inode,
3386 struct file_extent_cluster *cluster)
3388 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3391 u64 offset = BTRFS_I(inode)->index_cnt;
3392 unsigned long index;
3393 unsigned long last_index;
3395 struct file_ra_state *ra;
3396 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
3403 ra = kzalloc(sizeof(*ra), GFP_NOFS);
3407 ret = prealloc_file_extent_cluster(inode, cluster);
3411 file_ra_state_init(ra, inode->i_mapping);
3413 ret = setup_extent_mapping(inode, cluster->start - offset,
3414 cluster->end - offset, cluster->start);
3418 index = (cluster->start - offset) >> PAGE_SHIFT;
3419 last_index = (cluster->end - offset) >> PAGE_SHIFT;
3420 while (index <= last_index) {
3421 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode),
3426 page = find_lock_page(inode->i_mapping, index);
3428 page_cache_sync_readahead(inode->i_mapping,
3430 last_index + 1 - index);
3431 page = find_or_create_page(inode->i_mapping, index,
3434 btrfs_delalloc_release_metadata(BTRFS_I(inode),
3436 btrfs_delalloc_release_extents(BTRFS_I(inode),
3443 if (PageReadahead(page)) {
3444 page_cache_async_readahead(inode->i_mapping,
3445 ra, NULL, page, index,
3446 last_index + 1 - index);
3449 if (!PageUptodate(page)) {
3450 btrfs_readpage(NULL, page);
3452 if (!PageUptodate(page)) {
3455 btrfs_delalloc_release_metadata(BTRFS_I(inode),
3457 btrfs_delalloc_release_extents(BTRFS_I(inode),
3464 page_start = page_offset(page);
3465 page_end = page_start + PAGE_SIZE - 1;
3467 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end);
3469 set_page_extent_mapped(page);
3471 if (nr < cluster->nr &&
3472 page_start + offset == cluster->boundary[nr]) {
3473 set_extent_bits(&BTRFS_I(inode)->io_tree,
3474 page_start, page_end,
3479 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
3484 btrfs_delalloc_release_metadata(BTRFS_I(inode),
3486 btrfs_delalloc_release_extents(BTRFS_I(inode),
3489 clear_extent_bits(&BTRFS_I(inode)->io_tree,
3490 page_start, page_end,
3491 EXTENT_LOCKED | EXTENT_BOUNDARY);
3495 set_page_dirty(page);
3497 unlock_extent(&BTRFS_I(inode)->io_tree,
3498 page_start, page_end);
3503 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
3504 balance_dirty_pages_ratelimited(inode->i_mapping);
3505 btrfs_throttle(fs_info);
3506 if (btrfs_should_cancel_balance(fs_info)) {
3511 WARN_ON(nr != cluster->nr);
3517 static noinline_for_stack
3518 int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key,
3519 struct file_extent_cluster *cluster)
3523 if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) {
3524 ret = relocate_file_extent_cluster(inode, cluster);
3531 cluster->start = extent_key->objectid;
3533 BUG_ON(cluster->nr >= MAX_EXTENTS);
3534 cluster->end = extent_key->objectid + extent_key->offset - 1;
3535 cluster->boundary[cluster->nr] = extent_key->objectid;
3538 if (cluster->nr >= MAX_EXTENTS) {
3539 ret = relocate_file_extent_cluster(inode, cluster);
3548 * helper to add a tree block to the list.
3549 * the major work is getting the generation and level of the block
3551 static int add_tree_block(struct reloc_control *rc,
3552 struct btrfs_key *extent_key,
3553 struct btrfs_path *path,
3554 struct rb_root *blocks)
3556 struct extent_buffer *eb;
3557 struct btrfs_extent_item *ei;
3558 struct btrfs_tree_block_info *bi;
3559 struct tree_block *block;
3560 struct rb_node *rb_node;
3565 eb = path->nodes[0];
3566 item_size = btrfs_item_size_nr(eb, path->slots[0]);
3568 if (extent_key->type == BTRFS_METADATA_ITEM_KEY ||
3569 item_size >= sizeof(*ei) + sizeof(*bi)) {
3570 ei = btrfs_item_ptr(eb, path->slots[0],
3571 struct btrfs_extent_item);
3572 if (extent_key->type == BTRFS_EXTENT_ITEM_KEY) {
3573 bi = (struct btrfs_tree_block_info *)(ei + 1);
3574 level = btrfs_tree_block_level(eb, bi);
3576 level = (int)extent_key->offset;
3578 generation = btrfs_extent_generation(eb, ei);
3579 } else if (unlikely(item_size == sizeof(struct btrfs_extent_item_v0))) {
3580 btrfs_print_v0_err(eb->fs_info);
3581 btrfs_handle_fs_error(eb->fs_info, -EINVAL, NULL);
3587 btrfs_release_path(path);
3589 BUG_ON(level == -1);
3591 block = kmalloc(sizeof(*block), GFP_NOFS);
3595 block->bytenr = extent_key->objectid;
3596 block->key.objectid = rc->extent_root->fs_info->nodesize;
3597 block->key.offset = generation;
3598 block->level = level;
3599 block->key_ready = 0;
3601 rb_node = tree_insert(blocks, block->bytenr, &block->rb_node);
3603 backref_tree_panic(rb_node, -EEXIST, block->bytenr);
3609 * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
3611 static int __add_tree_block(struct reloc_control *rc,
3612 u64 bytenr, u32 blocksize,
3613 struct rb_root *blocks)
3615 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3616 struct btrfs_path *path;
3617 struct btrfs_key key;
3619 bool skinny = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
3621 if (tree_block_processed(bytenr, rc))
3624 if (tree_search(blocks, bytenr))
3627 path = btrfs_alloc_path();
3631 key.objectid = bytenr;
3633 key.type = BTRFS_METADATA_ITEM_KEY;
3634 key.offset = (u64)-1;
3636 key.type = BTRFS_EXTENT_ITEM_KEY;
3637 key.offset = blocksize;
3640 path->search_commit_root = 1;
3641 path->skip_locking = 1;
3642 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0);
3646 if (ret > 0 && skinny) {
3647 if (path->slots[0]) {
3649 btrfs_item_key_to_cpu(path->nodes[0], &key,
3651 if (key.objectid == bytenr &&
3652 (key.type == BTRFS_METADATA_ITEM_KEY ||
3653 (key.type == BTRFS_EXTENT_ITEM_KEY &&
3654 key.offset == blocksize)))
3660 btrfs_release_path(path);
3666 btrfs_print_leaf(path->nodes[0]);
3668 "tree block extent item (%llu) is not found in extent tree",
3675 ret = add_tree_block(rc, &key, path, blocks);
3677 btrfs_free_path(path);
3681 static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
3682 struct btrfs_block_group *block_group,
3683 struct inode *inode,
3686 struct btrfs_key key;
3687 struct btrfs_root *root = fs_info->tree_root;
3688 struct btrfs_trans_handle *trans;
3695 key.type = BTRFS_INODE_ITEM_KEY;
3698 inode = btrfs_iget(fs_info->sb, &key, root);
3703 ret = btrfs_check_trunc_cache_free_space(fs_info,
3704 &fs_info->global_block_rsv);
3708 trans = btrfs_join_transaction(root);
3709 if (IS_ERR(trans)) {
3710 ret = PTR_ERR(trans);
3714 ret = btrfs_truncate_free_space_cache(trans, block_group, inode);
3716 btrfs_end_transaction(trans);
3717 btrfs_btree_balance_dirty(fs_info);
3724 * Locate the free space cache EXTENT_DATA in root tree leaf and delete the
3725 * cache inode, to avoid free space cache data extent blocking data relocation.
3727 static int delete_v1_space_cache(struct extent_buffer *leaf,
3728 struct btrfs_block_group *block_group,
3731 u64 space_cache_ino;
3732 struct btrfs_file_extent_item *ei;
3733 struct btrfs_key key;
3738 if (btrfs_header_owner(leaf) != BTRFS_ROOT_TREE_OBJECTID)
3741 for (i = 0; i < btrfs_header_nritems(leaf); i++) {
3742 btrfs_item_key_to_cpu(leaf, &key, i);
3743 if (key.type != BTRFS_EXTENT_DATA_KEY)
3745 ei = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
3746 if (btrfs_file_extent_type(leaf, ei) == BTRFS_FILE_EXTENT_REG &&
3747 btrfs_file_extent_disk_bytenr(leaf, ei) == data_bytenr) {
3749 space_cache_ino = key.objectid;
3755 ret = delete_block_group_cache(leaf->fs_info, block_group, NULL,
3761 * helper to find all tree blocks that reference a given data extent
3763 static noinline_for_stack
3764 int add_data_references(struct reloc_control *rc,
3765 struct btrfs_key *extent_key,
3766 struct btrfs_path *path,
3767 struct rb_root *blocks)
3769 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3770 struct ulist *leaves = NULL;
3771 struct ulist_iterator leaf_uiter;
3772 struct ulist_node *ref_node = NULL;
3773 const u32 blocksize = fs_info->nodesize;
3776 btrfs_release_path(path);
3777 ret = btrfs_find_all_leafs(NULL, fs_info, extent_key->objectid,
3778 0, &leaves, NULL, true);
3782 ULIST_ITER_INIT(&leaf_uiter);
3783 while ((ref_node = ulist_next(leaves, &leaf_uiter))) {
3784 struct extent_buffer *eb;
3786 eb = read_tree_block(fs_info, ref_node->val, 0, 0, NULL);
3791 ret = delete_v1_space_cache(eb, rc->block_group,
3792 extent_key->objectid);
3793 free_extent_buffer(eb);
3796 ret = __add_tree_block(rc, ref_node->val, blocksize, blocks);
3801 free_block_list(blocks);
3807 * helper to find next unprocessed extent
3809 static noinline_for_stack
3810 int find_next_extent(struct reloc_control *rc, struct btrfs_path *path,
3811 struct btrfs_key *extent_key)
3813 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3814 struct btrfs_key key;
3815 struct extent_buffer *leaf;
3816 u64 start, end, last;
3819 last = rc->block_group->start + rc->block_group->length;
3822 if (rc->search_start >= last) {
3827 key.objectid = rc->search_start;
3828 key.type = BTRFS_EXTENT_ITEM_KEY;
3831 path->search_commit_root = 1;
3832 path->skip_locking = 1;
3833 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path,
3838 leaf = path->nodes[0];
3839 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3840 ret = btrfs_next_leaf(rc->extent_root, path);
3843 leaf = path->nodes[0];
3846 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3847 if (key.objectid >= last) {
3852 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3853 key.type != BTRFS_METADATA_ITEM_KEY) {
3858 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3859 key.objectid + key.offset <= rc->search_start) {
3864 if (key.type == BTRFS_METADATA_ITEM_KEY &&
3865 key.objectid + fs_info->nodesize <=
3871 ret = find_first_extent_bit(&rc->processed_blocks,
3872 key.objectid, &start, &end,
3873 EXTENT_DIRTY, NULL);
3875 if (ret == 0 && start <= key.objectid) {
3876 btrfs_release_path(path);
3877 rc->search_start = end + 1;
3879 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3880 rc->search_start = key.objectid + key.offset;
3882 rc->search_start = key.objectid +
3884 memcpy(extent_key, &key, sizeof(key));
3888 btrfs_release_path(path);
3892 static void set_reloc_control(struct reloc_control *rc)
3894 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3896 mutex_lock(&fs_info->reloc_mutex);
3897 fs_info->reloc_ctl = rc;
3898 mutex_unlock(&fs_info->reloc_mutex);
3901 static void unset_reloc_control(struct reloc_control *rc)
3903 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3905 mutex_lock(&fs_info->reloc_mutex);
3906 fs_info->reloc_ctl = NULL;
3907 mutex_unlock(&fs_info->reloc_mutex);
3910 static int check_extent_flags(u64 flags)
3912 if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
3913 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
3915 if (!(flags & BTRFS_EXTENT_FLAG_DATA) &&
3916 !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
3918 if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
3919 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
3924 static noinline_for_stack
3925 int prepare_to_relocate(struct reloc_control *rc)
3927 struct btrfs_trans_handle *trans;
3930 rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root->fs_info,
3931 BTRFS_BLOCK_RSV_TEMP);
3935 memset(&rc->cluster, 0, sizeof(rc->cluster));
3936 rc->search_start = rc->block_group->start;
3937 rc->extents_found = 0;
3938 rc->nodes_relocated = 0;
3939 rc->merging_rsv_size = 0;
3940 rc->reserved_bytes = 0;
3941 rc->block_rsv->size = rc->extent_root->fs_info->nodesize *
3942 RELOCATION_RESERVED_NODES;
3943 ret = btrfs_block_rsv_refill(rc->extent_root,
3944 rc->block_rsv, rc->block_rsv->size,
3945 BTRFS_RESERVE_FLUSH_ALL);
3949 rc->create_reloc_tree = 1;
3950 set_reloc_control(rc);
3952 trans = btrfs_join_transaction(rc->extent_root);
3953 if (IS_ERR(trans)) {
3954 unset_reloc_control(rc);
3956 * extent tree is not a ref_cow tree and has no reloc_root to
3957 * cleanup. And callers are responsible to free the above
3960 return PTR_ERR(trans);
3962 btrfs_commit_transaction(trans);
3966 static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
3968 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3969 struct rb_root blocks = RB_ROOT;
3970 struct btrfs_key key;
3971 struct btrfs_trans_handle *trans = NULL;
3972 struct btrfs_path *path;
3973 struct btrfs_extent_item *ei;
3980 path = btrfs_alloc_path();
3983 path->reada = READA_FORWARD;
3985 ret = prepare_to_relocate(rc);
3992 rc->reserved_bytes = 0;
3993 ret = btrfs_block_rsv_refill(rc->extent_root,
3994 rc->block_rsv, rc->block_rsv->size,
3995 BTRFS_RESERVE_FLUSH_ALL);
4001 trans = btrfs_start_transaction(rc->extent_root, 0);
4002 if (IS_ERR(trans)) {
4003 err = PTR_ERR(trans);
4008 if (update_backref_cache(trans, &rc->backref_cache)) {
4009 btrfs_end_transaction(trans);
4014 ret = find_next_extent(rc, path, &key);
4020 rc->extents_found++;
4022 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4023 struct btrfs_extent_item);
4024 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
4025 if (item_size >= sizeof(*ei)) {
4026 flags = btrfs_extent_flags(path->nodes[0], ei);
4027 ret = check_extent_flags(flags);
4029 } else if (unlikely(item_size == sizeof(struct btrfs_extent_item_v0))) {
4031 btrfs_print_v0_err(trans->fs_info);
4032 btrfs_abort_transaction(trans, err);
4038 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
4039 ret = add_tree_block(rc, &key, path, &blocks);
4040 } else if (rc->stage == UPDATE_DATA_PTRS &&
4041 (flags & BTRFS_EXTENT_FLAG_DATA)) {
4042 ret = add_data_references(rc, &key, path, &blocks);
4044 btrfs_release_path(path);
4052 if (!RB_EMPTY_ROOT(&blocks)) {
4053 ret = relocate_tree_blocks(trans, rc, &blocks);
4055 if (ret != -EAGAIN) {
4059 rc->extents_found--;
4060 rc->search_start = key.objectid;
4064 btrfs_end_transaction_throttle(trans);
4065 btrfs_btree_balance_dirty(fs_info);
4068 if (rc->stage == MOVE_DATA_EXTENTS &&
4069 (flags & BTRFS_EXTENT_FLAG_DATA)) {
4070 rc->found_file_extent = 1;
4071 ret = relocate_data_extent(rc->data_inode,
4072 &key, &rc->cluster);
4078 if (btrfs_should_cancel_balance(fs_info)) {
4083 if (trans && progress && err == -ENOSPC) {
4084 ret = btrfs_force_chunk_alloc(trans, rc->block_group->flags);
4092 btrfs_release_path(path);
4093 clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY);
4096 btrfs_end_transaction_throttle(trans);
4097 btrfs_btree_balance_dirty(fs_info);
4101 ret = relocate_file_extent_cluster(rc->data_inode,
4107 rc->create_reloc_tree = 0;
4108 set_reloc_control(rc);
4110 backref_cache_cleanup(&rc->backref_cache);
4111 btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL);
4114 * Even in the case when the relocation is cancelled, we should all go
4115 * through prepare_to_merge() and merge_reloc_roots().
4117 * For error (including cancelled balance), prepare_to_merge() will
4118 * mark all reloc trees orphan, then queue them for cleanup in
4119 * merge_reloc_roots()
4121 err = prepare_to_merge(rc, err);
4123 merge_reloc_roots(rc);
4125 rc->merge_reloc_tree = 0;
4126 unset_reloc_control(rc);
4127 btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1, NULL);
4129 /* get rid of pinned extents */
4130 trans = btrfs_join_transaction(rc->extent_root);
4131 if (IS_ERR(trans)) {
4132 err = PTR_ERR(trans);
4135 btrfs_commit_transaction(trans);
4137 ret = clean_dirty_subvols(rc);
4138 if (ret < 0 && !err)
4140 btrfs_free_block_rsv(fs_info, rc->block_rsv);
4141 btrfs_free_path(path);
4145 static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
4146 struct btrfs_root *root, u64 objectid)
4148 struct btrfs_path *path;
4149 struct btrfs_inode_item *item;
4150 struct extent_buffer *leaf;
4153 path = btrfs_alloc_path();
4157 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
4161 leaf = path->nodes[0];
4162 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
4163 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
4164 btrfs_set_inode_generation(leaf, item, 1);
4165 btrfs_set_inode_size(leaf, item, 0);
4166 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
4167 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS |
4168 BTRFS_INODE_PREALLOC);
4169 btrfs_mark_buffer_dirty(leaf);
4171 btrfs_free_path(path);
4176 * helper to create inode for data relocation.
4177 * the inode is in data relocation tree and its link count is 0
4179 static noinline_for_stack
4180 struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
4181 struct btrfs_block_group *group)
4183 struct inode *inode = NULL;
4184 struct btrfs_trans_handle *trans;
4185 struct btrfs_root *root;
4186 struct btrfs_key key;
4190 root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID);
4192 return ERR_CAST(root);
4194 trans = btrfs_start_transaction(root, 6);
4195 if (IS_ERR(trans)) {
4196 btrfs_put_root(root);
4197 return ERR_CAST(trans);
4200 err = btrfs_find_free_objectid(root, &objectid);
4204 err = __insert_orphan_inode(trans, root, objectid);
4207 key.objectid = objectid;
4208 key.type = BTRFS_INODE_ITEM_KEY;
4210 inode = btrfs_iget(fs_info->sb, &key, root);
4211 BUG_ON(IS_ERR(inode));
4212 BTRFS_I(inode)->index_cnt = group->start;
4214 err = btrfs_orphan_add(trans, BTRFS_I(inode));
4216 btrfs_put_root(root);
4217 btrfs_end_transaction(trans);
4218 btrfs_btree_balance_dirty(fs_info);
4222 inode = ERR_PTR(err);
4227 static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info)
4229 struct reloc_control *rc;
4231 rc = kzalloc(sizeof(*rc), GFP_NOFS);
4235 INIT_LIST_HEAD(&rc->reloc_roots);
4236 INIT_LIST_HEAD(&rc->dirty_subvol_roots);
4237 backref_cache_init(&rc->backref_cache);
4238 mapping_tree_init(&rc->reloc_root_tree);
4239 extent_io_tree_init(fs_info, &rc->processed_blocks,
4240 IO_TREE_RELOC_BLOCKS, NULL);
4244 static void free_reloc_control(struct reloc_control *rc)
4246 struct mapping_node *node, *tmp;
4248 free_reloc_roots(&rc->reloc_roots);
4249 rbtree_postorder_for_each_entry_safe(node, tmp,
4250 &rc->reloc_root_tree.rb_root, rb_node)
4257 * Print the block group being relocated
4259 static void describe_relocation(struct btrfs_fs_info *fs_info,
4260 struct btrfs_block_group *block_group)
4262 char buf[128] = {'\0'};
4264 btrfs_describe_block_groups(block_group->flags, buf, sizeof(buf));
4267 "relocating block group %llu flags %s",
4268 block_group->start, buf);
4271 static const char *stage_to_string(int stage)
4273 if (stage == MOVE_DATA_EXTENTS)
4274 return "move data extents";
4275 if (stage == UPDATE_DATA_PTRS)
4276 return "update data pointers";
4281 * function to relocate all extents in a block group.
4283 int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start)
4285 struct btrfs_block_group *bg;
4286 struct btrfs_root *extent_root = fs_info->extent_root;
4287 struct reloc_control *rc;
4288 struct inode *inode;
4289 struct btrfs_path *path;
4294 bg = btrfs_lookup_block_group(fs_info, group_start);
4298 if (btrfs_pinned_by_swapfile(fs_info, bg)) {
4299 btrfs_put_block_group(bg);
4303 rc = alloc_reloc_control(fs_info);
4305 btrfs_put_block_group(bg);
4309 rc->extent_root = extent_root;
4310 rc->block_group = bg;
4312 ret = btrfs_inc_block_group_ro(rc->block_group, true);
4319 path = btrfs_alloc_path();
4325 inode = lookup_free_space_inode(rc->block_group, path);
4326 btrfs_free_path(path);
4329 ret = delete_block_group_cache(fs_info, rc->block_group, inode, 0);
4331 ret = PTR_ERR(inode);
4333 if (ret && ret != -ENOENT) {
4338 rc->data_inode = create_reloc_inode(fs_info, rc->block_group);
4339 if (IS_ERR(rc->data_inode)) {
4340 err = PTR_ERR(rc->data_inode);
4341 rc->data_inode = NULL;
4345 describe_relocation(fs_info, rc->block_group);
4347 btrfs_wait_block_group_reservations(rc->block_group);
4348 btrfs_wait_nocow_writers(rc->block_group);
4349 btrfs_wait_ordered_roots(fs_info, U64_MAX,
4350 rc->block_group->start,
4351 rc->block_group->length);
4356 mutex_lock(&fs_info->cleaner_mutex);
4357 ret = relocate_block_group(rc);
4358 mutex_unlock(&fs_info->cleaner_mutex);
4362 finishes_stage = rc->stage;
4364 * We may have gotten ENOSPC after we already dirtied some
4365 * extents. If writeout happens while we're relocating a
4366 * different block group we could end up hitting the
4367 * BUG_ON(rc->stage == UPDATE_DATA_PTRS) in
4368 * btrfs_reloc_cow_block. Make sure we write everything out
4369 * properly so we don't trip over this problem, and then break
4370 * out of the loop if we hit an error.
4372 if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) {
4373 ret = btrfs_wait_ordered_range(rc->data_inode, 0,
4377 invalidate_mapping_pages(rc->data_inode->i_mapping,
4379 rc->stage = UPDATE_DATA_PTRS;
4385 if (rc->extents_found == 0)
4388 btrfs_info(fs_info, "found %llu extents, stage: %s",
4389 rc->extents_found, stage_to_string(finishes_stage));
4392 WARN_ON(rc->block_group->pinned > 0);
4393 WARN_ON(rc->block_group->reserved > 0);
4394 WARN_ON(rc->block_group->used > 0);
4397 btrfs_dec_block_group_ro(rc->block_group);
4398 iput(rc->data_inode);
4399 btrfs_put_block_group(rc->block_group);
4400 free_reloc_control(rc);
4404 static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
4406 struct btrfs_fs_info *fs_info = root->fs_info;
4407 struct btrfs_trans_handle *trans;
4410 trans = btrfs_start_transaction(fs_info->tree_root, 0);
4412 return PTR_ERR(trans);
4414 memset(&root->root_item.drop_progress, 0,
4415 sizeof(root->root_item.drop_progress));
4416 root->root_item.drop_level = 0;
4417 btrfs_set_root_refs(&root->root_item, 0);
4418 ret = btrfs_update_root(trans, fs_info->tree_root,
4419 &root->root_key, &root->root_item);
4421 err = btrfs_end_transaction(trans);
4428 * recover relocation interrupted by system crash.
4430 * this function resumes merging reloc trees with corresponding fs trees.
4431 * this is important for keeping the sharing of tree blocks
4433 int btrfs_recover_relocation(struct btrfs_root *root)
4435 struct btrfs_fs_info *fs_info = root->fs_info;
4436 LIST_HEAD(reloc_roots);
4437 struct btrfs_key key;
4438 struct btrfs_root *fs_root;
4439 struct btrfs_root *reloc_root;
4440 struct btrfs_path *path;
4441 struct extent_buffer *leaf;
4442 struct reloc_control *rc = NULL;
4443 struct btrfs_trans_handle *trans;
4447 path = btrfs_alloc_path();
4450 path->reada = READA_BACK;
4452 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4453 key.type = BTRFS_ROOT_ITEM_KEY;
4454 key.offset = (u64)-1;
4457 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key,
4464 if (path->slots[0] == 0)
4468 leaf = path->nodes[0];
4469 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4470 btrfs_release_path(path);
4472 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID ||
4473 key.type != BTRFS_ROOT_ITEM_KEY)
4476 reloc_root = btrfs_read_tree_root(root, &key);
4477 if (IS_ERR(reloc_root)) {
4478 err = PTR_ERR(reloc_root);
4482 set_bit(BTRFS_ROOT_REF_COWS, &reloc_root->state);
4483 list_add(&reloc_root->root_list, &reloc_roots);
4485 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
4486 fs_root = read_fs_root(fs_info,
4487 reloc_root->root_key.offset);
4488 if (IS_ERR(fs_root)) {
4489 ret = PTR_ERR(fs_root);
4490 if (ret != -ENOENT) {
4494 ret = mark_garbage_root(reloc_root);
4500 btrfs_put_root(fs_root);
4504 if (key.offset == 0)
4509 btrfs_release_path(path);
4511 if (list_empty(&reloc_roots))
4514 rc = alloc_reloc_control(fs_info);
4520 rc->extent_root = fs_info->extent_root;
4522 set_reloc_control(rc);
4524 trans = btrfs_join_transaction(rc->extent_root);
4525 if (IS_ERR(trans)) {
4526 err = PTR_ERR(trans);
4530 rc->merge_reloc_tree = 1;
4532 while (!list_empty(&reloc_roots)) {
4533 reloc_root = list_entry(reloc_roots.next,
4534 struct btrfs_root, root_list);
4535 list_del(&reloc_root->root_list);
4537 if (btrfs_root_refs(&reloc_root->root_item) == 0) {
4538 list_add_tail(&reloc_root->root_list,
4543 fs_root = read_fs_root(fs_info, reloc_root->root_key.offset);
4544 if (IS_ERR(fs_root)) {
4545 err = PTR_ERR(fs_root);
4546 list_add_tail(&reloc_root->root_list, &reloc_roots);
4550 err = __add_reloc_root(reloc_root);
4551 BUG_ON(err < 0); /* -ENOMEM or logic error */
4552 fs_root->reloc_root = btrfs_grab_root(reloc_root);
4553 btrfs_put_root(fs_root);
4556 err = btrfs_commit_transaction(trans);
4560 merge_reloc_roots(rc);
4562 unset_reloc_control(rc);
4564 trans = btrfs_join_transaction(rc->extent_root);
4565 if (IS_ERR(trans)) {
4566 err = PTR_ERR(trans);
4569 err = btrfs_commit_transaction(trans);
4571 ret = clean_dirty_subvols(rc);
4572 if (ret < 0 && !err)
4575 unset_reloc_control(rc);
4576 free_reloc_control(rc);
4578 if (!list_empty(&reloc_roots))
4579 free_reloc_roots(&reloc_roots);
4581 btrfs_free_path(path);
4584 /* cleanup orphan inode in data relocation tree */
4585 fs_root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID);
4586 if (IS_ERR(fs_root)) {
4587 err = PTR_ERR(fs_root);
4589 err = btrfs_orphan_cleanup(fs_root);
4590 btrfs_put_root(fs_root);
4597 * helper to add ordered checksum for data relocation.
4599 * cloning checksum properly handles the nodatasum extents.
4600 * it also saves CPU time to re-calculate the checksum.
4602 int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
4604 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4605 struct btrfs_ordered_sum *sums;
4606 struct btrfs_ordered_extent *ordered;
4612 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
4613 BUG_ON(ordered->file_offset != file_pos || ordered->num_bytes != len);
4615 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
4616 ret = btrfs_lookup_csums_range(fs_info->csum_root, disk_bytenr,
4617 disk_bytenr + len - 1, &list, 0);
4621 while (!list_empty(&list)) {
4622 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
4623 list_del_init(&sums->list);
4626 * We need to offset the new_bytenr based on where the csum is.
4627 * We need to do this because we will read in entire prealloc
4628 * extents but we may have written to say the middle of the
4629 * prealloc extent, so we need to make sure the csum goes with
4630 * the right disk offset.
4632 * We can do this because the data reloc inode refers strictly
4633 * to the on disk bytes, so we don't have to worry about
4634 * disk_len vs real len like with real inodes since it's all
4637 new_bytenr = ordered->disk_bytenr + sums->bytenr - disk_bytenr;
4638 sums->bytenr = new_bytenr;
4640 btrfs_add_ordered_sum(ordered, sums);
4643 btrfs_put_ordered_extent(ordered);
4647 int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
4648 struct btrfs_root *root, struct extent_buffer *buf,
4649 struct extent_buffer *cow)
4651 struct btrfs_fs_info *fs_info = root->fs_info;
4652 struct reloc_control *rc;
4653 struct backref_node *node;
4658 rc = fs_info->reloc_ctl;
4662 BUG_ON(rc->stage == UPDATE_DATA_PTRS &&
4663 root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID);
4665 level = btrfs_header_level(buf);
4666 if (btrfs_header_generation(buf) <=
4667 btrfs_root_last_snapshot(&root->root_item))
4670 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID &&
4671 rc->create_reloc_tree) {
4672 WARN_ON(!first_cow && level == 0);
4674 node = rc->backref_cache.path[level];
4675 BUG_ON(node->bytenr != buf->start &&
4676 node->new_bytenr != buf->start);
4678 drop_node_buffer(node);
4679 atomic_inc(&cow->refs);
4681 node->new_bytenr = cow->start;
4683 if (!node->pending) {
4684 list_move_tail(&node->list,
4685 &rc->backref_cache.pending[level]);
4690 __mark_block_processed(rc, node);
4692 if (first_cow && level > 0)
4693 rc->nodes_relocated += buf->len;
4696 if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS)
4697 ret = replace_file_extents(trans, rc, root, cow);
4702 * called before creating snapshot. it calculates metadata reservation
4703 * required for relocating tree blocks in the snapshot
4705 void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending,
4706 u64 *bytes_to_reserve)
4708 struct btrfs_root *root = pending->root;
4709 struct reloc_control *rc = root->fs_info->reloc_ctl;
4711 if (!rc || !have_reloc_root(root))
4714 if (!rc->merge_reloc_tree)
4717 root = root->reloc_root;
4718 BUG_ON(btrfs_root_refs(&root->root_item) == 0);
4720 * relocation is in the stage of merging trees. the space
4721 * used by merging a reloc tree is twice the size of
4722 * relocated tree nodes in the worst case. half for cowing
4723 * the reloc tree, half for cowing the fs tree. the space
4724 * used by cowing the reloc tree will be freed after the
4725 * tree is dropped. if we create snapshot, cowing the fs
4726 * tree may use more space than it frees. so we need
4727 * reserve extra space.
4729 *bytes_to_reserve += rc->nodes_relocated;
4733 * called after snapshot is created. migrate block reservation
4734 * and create reloc root for the newly created snapshot
4736 * This is similar to btrfs_init_reloc_root(), we come out of here with two
4737 * references held on the reloc_root, one for root->reloc_root and one for
4740 int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
4741 struct btrfs_pending_snapshot *pending)
4743 struct btrfs_root *root = pending->root;
4744 struct btrfs_root *reloc_root;
4745 struct btrfs_root *new_root;
4746 struct reloc_control *rc = root->fs_info->reloc_ctl;
4749 if (!rc || !have_reloc_root(root))
4752 rc = root->fs_info->reloc_ctl;
4753 rc->merging_rsv_size += rc->nodes_relocated;
4755 if (rc->merge_reloc_tree) {
4756 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
4758 rc->nodes_relocated, true);
4763 new_root = pending->snap;
4764 reloc_root = create_reloc_root(trans, root->reloc_root,
4765 new_root->root_key.objectid);
4766 if (IS_ERR(reloc_root))
4767 return PTR_ERR(reloc_root);
4769 ret = __add_reloc_root(reloc_root);
4771 new_root->reloc_root = btrfs_grab_root(reloc_root);
4773 if (rc->create_reloc_tree)
4774 ret = clone_backref_node(trans, rc, root, reloc_root);