2 * Copyright (C) 2009 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
20 #include <linux/pagemap.h>
21 #include <linux/writeback.h>
22 #include <linux/blkdev.h>
23 #include <linux/rbtree.h>
24 #include <linux/slab.h>
27 #include "transaction.h"
30 #include "btrfs_inode.h"
31 #include "async-thread.h"
32 #include "free-space-cache.h"
33 #include "inode-map.h"
37 * backref_node, mapping_node and tree_block start with this
40 struct rb_node rb_node;
45 * present a tree block in the backref cache
48 struct rb_node rb_node;
52 /* objectid of tree block owner, can be not uptodate */
54 /* link to pending, changed or detached list */
55 struct list_head list;
56 /* list of upper level blocks reference this block */
57 struct list_head upper;
58 /* list of child blocks in the cache */
59 struct list_head lower;
60 /* NULL if this node is not tree root */
61 struct btrfs_root *root;
62 /* extent buffer got by COW the block */
63 struct extent_buffer *eb;
64 /* level of tree block */
66 /* is the block in non-reference counted tree */
67 unsigned int cowonly:1;
68 /* 1 if no child node in the cache */
69 unsigned int lowest:1;
70 /* is the extent buffer locked */
71 unsigned int locked:1;
72 /* has the block been processed */
73 unsigned int processed:1;
74 /* have backrefs of this block been checked */
75 unsigned int checked:1;
77 * 1 if corresponding block has been cowed but some upper
78 * level block pointers may not point to the new location
80 unsigned int pending:1;
82 * 1 if the backref node isn't connected to any other
85 unsigned int detached:1;
89 * present a block pointer in the backref cache
92 struct list_head list[2];
93 struct backref_node *node[2];
98 #define RELOCATION_RESERVED_NODES 256
100 struct backref_cache {
101 /* red black tree of all backref nodes in the cache */
102 struct rb_root rb_root;
103 /* for passing backref nodes to btrfs_reloc_cow_block */
104 struct backref_node *path[BTRFS_MAX_LEVEL];
106 * list of blocks that have been cowed but some block
107 * pointers in upper level blocks may not reflect the
110 struct list_head pending[BTRFS_MAX_LEVEL];
111 /* list of backref nodes with no child node */
112 struct list_head leaves;
113 /* list of blocks that have been cowed in current transaction */
114 struct list_head changed;
115 /* list of detached backref node. */
116 struct list_head detached;
125 * map address of tree root to tree
127 struct mapping_node {
128 struct rb_node rb_node;
133 struct mapping_tree {
134 struct rb_root rb_root;
139 * present a tree block to process
142 struct rb_node rb_node;
144 struct btrfs_key key;
145 unsigned int level:8;
146 unsigned int key_ready:1;
149 #define MAX_EXTENTS 128
151 struct file_extent_cluster {
154 u64 boundary[MAX_EXTENTS];
158 struct reloc_control {
159 /* block group to relocate */
160 struct btrfs_block_group_cache *block_group;
162 struct btrfs_root *extent_root;
163 /* inode for moving data */
164 struct inode *data_inode;
166 struct btrfs_block_rsv *block_rsv;
168 struct backref_cache backref_cache;
170 struct file_extent_cluster cluster;
171 /* tree blocks have been processed */
172 struct extent_io_tree processed_blocks;
173 /* map start of tree root to corresponding reloc tree */
174 struct mapping_tree reloc_root_tree;
175 /* list of reloc trees */
176 struct list_head reloc_roots;
177 /* size of metadata reservation for merging reloc trees */
178 u64 merging_rsv_size;
179 /* size of relocated tree nodes */
181 /* reserved size for block group relocation*/
187 unsigned int stage:8;
188 unsigned int create_reloc_tree:1;
189 unsigned int merge_reloc_tree:1;
190 unsigned int found_file_extent:1;
193 /* stages of data relocation */
194 #define MOVE_DATA_EXTENTS 0
195 #define UPDATE_DATA_PTRS 1
197 static void remove_backref_node(struct backref_cache *cache,
198 struct backref_node *node);
199 static void __mark_block_processed(struct reloc_control *rc,
200 struct backref_node *node);
202 static void mapping_tree_init(struct mapping_tree *tree)
204 tree->rb_root = RB_ROOT;
205 spin_lock_init(&tree->lock);
208 static void backref_cache_init(struct backref_cache *cache)
211 cache->rb_root = RB_ROOT;
212 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
213 INIT_LIST_HEAD(&cache->pending[i]);
214 INIT_LIST_HEAD(&cache->changed);
215 INIT_LIST_HEAD(&cache->detached);
216 INIT_LIST_HEAD(&cache->leaves);
219 static void backref_cache_cleanup(struct backref_cache *cache)
221 struct backref_node *node;
224 while (!list_empty(&cache->detached)) {
225 node = list_entry(cache->detached.next,
226 struct backref_node, list);
227 remove_backref_node(cache, node);
230 while (!list_empty(&cache->leaves)) {
231 node = list_entry(cache->leaves.next,
232 struct backref_node, lower);
233 remove_backref_node(cache, node);
236 cache->last_trans = 0;
238 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
239 ASSERT(list_empty(&cache->pending[i]));
240 ASSERT(list_empty(&cache->changed));
241 ASSERT(list_empty(&cache->detached));
242 ASSERT(RB_EMPTY_ROOT(&cache->rb_root));
243 ASSERT(!cache->nr_nodes);
244 ASSERT(!cache->nr_edges);
247 static struct backref_node *alloc_backref_node(struct backref_cache *cache)
249 struct backref_node *node;
251 node = kzalloc(sizeof(*node), GFP_NOFS);
253 INIT_LIST_HEAD(&node->list);
254 INIT_LIST_HEAD(&node->upper);
255 INIT_LIST_HEAD(&node->lower);
256 RB_CLEAR_NODE(&node->rb_node);
262 static void free_backref_node(struct backref_cache *cache,
263 struct backref_node *node)
271 static struct backref_edge *alloc_backref_edge(struct backref_cache *cache)
273 struct backref_edge *edge;
275 edge = kzalloc(sizeof(*edge), GFP_NOFS);
281 static void free_backref_edge(struct backref_cache *cache,
282 struct backref_edge *edge)
290 static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr,
291 struct rb_node *node)
293 struct rb_node **p = &root->rb_node;
294 struct rb_node *parent = NULL;
295 struct tree_entry *entry;
299 entry = rb_entry(parent, struct tree_entry, rb_node);
301 if (bytenr < entry->bytenr)
303 else if (bytenr > entry->bytenr)
309 rb_link_node(node, parent, p);
310 rb_insert_color(node, root);
314 static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
316 struct rb_node *n = root->rb_node;
317 struct tree_entry *entry;
320 entry = rb_entry(n, struct tree_entry, rb_node);
322 if (bytenr < entry->bytenr)
324 else if (bytenr > entry->bytenr)
332 static void backref_tree_panic(struct rb_node *rb_node, int errno, u64 bytenr)
335 struct btrfs_fs_info *fs_info = NULL;
336 struct backref_node *bnode = rb_entry(rb_node, struct backref_node,
339 fs_info = bnode->root->fs_info;
340 btrfs_panic(fs_info, errno, "Inconsistency in backref cache "
341 "found at offset %llu", bytenr);
345 * walk up backref nodes until reach node presents tree root
347 static struct backref_node *walk_up_backref(struct backref_node *node,
348 struct backref_edge *edges[],
351 struct backref_edge *edge;
354 while (!list_empty(&node->upper)) {
355 edge = list_entry(node->upper.next,
356 struct backref_edge, list[LOWER]);
358 node = edge->node[UPPER];
360 BUG_ON(node->detached);
366 * walk down backref nodes to find start of next reference path
368 static struct backref_node *walk_down_backref(struct backref_edge *edges[],
371 struct backref_edge *edge;
372 struct backref_node *lower;
376 edge = edges[idx - 1];
377 lower = edge->node[LOWER];
378 if (list_is_last(&edge->list[LOWER], &lower->upper)) {
382 edge = list_entry(edge->list[LOWER].next,
383 struct backref_edge, list[LOWER]);
384 edges[idx - 1] = edge;
386 return edge->node[UPPER];
392 static void unlock_node_buffer(struct backref_node *node)
395 btrfs_tree_unlock(node->eb);
400 static void drop_node_buffer(struct backref_node *node)
403 unlock_node_buffer(node);
404 free_extent_buffer(node->eb);
409 static void drop_backref_node(struct backref_cache *tree,
410 struct backref_node *node)
412 BUG_ON(!list_empty(&node->upper));
414 drop_node_buffer(node);
415 list_del(&node->list);
416 list_del(&node->lower);
417 if (!RB_EMPTY_NODE(&node->rb_node))
418 rb_erase(&node->rb_node, &tree->rb_root);
419 free_backref_node(tree, node);
423 * remove a backref node from the backref cache
425 static void remove_backref_node(struct backref_cache *cache,
426 struct backref_node *node)
428 struct backref_node *upper;
429 struct backref_edge *edge;
434 BUG_ON(!node->lowest && !node->detached);
435 while (!list_empty(&node->upper)) {
436 edge = list_entry(node->upper.next, struct backref_edge,
438 upper = edge->node[UPPER];
439 list_del(&edge->list[LOWER]);
440 list_del(&edge->list[UPPER]);
441 free_backref_edge(cache, edge);
443 if (RB_EMPTY_NODE(&upper->rb_node)) {
444 BUG_ON(!list_empty(&node->upper));
445 drop_backref_node(cache, node);
451 * add the node to leaf node list if no other
452 * child block cached.
454 if (list_empty(&upper->lower)) {
455 list_add_tail(&upper->lower, &cache->leaves);
460 drop_backref_node(cache, node);
463 static void update_backref_node(struct backref_cache *cache,
464 struct backref_node *node, u64 bytenr)
466 struct rb_node *rb_node;
467 rb_erase(&node->rb_node, &cache->rb_root);
468 node->bytenr = bytenr;
469 rb_node = tree_insert(&cache->rb_root, node->bytenr, &node->rb_node);
471 backref_tree_panic(rb_node, -EEXIST, bytenr);
475 * update backref cache after a transaction commit
477 static int update_backref_cache(struct btrfs_trans_handle *trans,
478 struct backref_cache *cache)
480 struct backref_node *node;
483 if (cache->last_trans == 0) {
484 cache->last_trans = trans->transid;
488 if (cache->last_trans == trans->transid)
492 * detached nodes are used to avoid unnecessary backref
493 * lookup. transaction commit changes the extent tree.
494 * so the detached nodes are no longer useful.
496 while (!list_empty(&cache->detached)) {
497 node = list_entry(cache->detached.next,
498 struct backref_node, list);
499 remove_backref_node(cache, node);
502 while (!list_empty(&cache->changed)) {
503 node = list_entry(cache->changed.next,
504 struct backref_node, list);
505 list_del_init(&node->list);
506 BUG_ON(node->pending);
507 update_backref_node(cache, node, node->new_bytenr);
511 * some nodes can be left in the pending list if there were
512 * errors during processing the pending nodes.
514 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
515 list_for_each_entry(node, &cache->pending[level], list) {
516 BUG_ON(!node->pending);
517 if (node->bytenr == node->new_bytenr)
519 update_backref_node(cache, node, node->new_bytenr);
523 cache->last_trans = 0;
528 static int should_ignore_root(struct btrfs_root *root)
530 struct btrfs_root *reloc_root;
532 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
535 reloc_root = root->reloc_root;
539 if (btrfs_root_last_snapshot(&reloc_root->root_item) ==
540 root->fs_info->running_transaction->transid - 1)
543 * if there is reloc tree and it was created in previous
544 * transaction backref lookup can find the reloc tree,
545 * so backref node for the fs tree root is useless for
551 * find reloc tree by address of tree root
553 static struct btrfs_root *find_reloc_root(struct reloc_control *rc,
556 struct rb_node *rb_node;
557 struct mapping_node *node;
558 struct btrfs_root *root = NULL;
560 spin_lock(&rc->reloc_root_tree.lock);
561 rb_node = tree_search(&rc->reloc_root_tree.rb_root, bytenr);
563 node = rb_entry(rb_node, struct mapping_node, rb_node);
564 root = (struct btrfs_root *)node->data;
566 spin_unlock(&rc->reloc_root_tree.lock);
570 static int is_cowonly_root(u64 root_objectid)
572 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
573 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
574 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
575 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
576 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
577 root_objectid == BTRFS_CSUM_TREE_OBJECTID ||
578 root_objectid == BTRFS_UUID_TREE_OBJECTID ||
579 root_objectid == BTRFS_QUOTA_TREE_OBJECTID ||
580 root_objectid == BTRFS_FREE_SPACE_TREE_OBJECTID)
585 static struct btrfs_root *read_fs_root(struct btrfs_fs_info *fs_info,
588 struct btrfs_key key;
590 key.objectid = root_objectid;
591 key.type = BTRFS_ROOT_ITEM_KEY;
592 if (is_cowonly_root(root_objectid))
595 key.offset = (u64)-1;
597 return btrfs_get_fs_root(fs_info, &key, false);
600 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
601 static noinline_for_stack
602 struct btrfs_root *find_tree_root(struct reloc_control *rc,
603 struct extent_buffer *leaf,
604 struct btrfs_extent_ref_v0 *ref0)
606 struct btrfs_root *root;
607 u64 root_objectid = btrfs_ref_root_v0(leaf, ref0);
608 u64 generation = btrfs_ref_generation_v0(leaf, ref0);
610 BUG_ON(root_objectid == BTRFS_TREE_RELOC_OBJECTID);
612 root = read_fs_root(rc->extent_root->fs_info, root_objectid);
613 BUG_ON(IS_ERR(root));
615 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
616 generation != btrfs_root_generation(&root->root_item))
623 static noinline_for_stack
624 int find_inline_backref(struct extent_buffer *leaf, int slot,
625 unsigned long *ptr, unsigned long *end)
627 struct btrfs_key key;
628 struct btrfs_extent_item *ei;
629 struct btrfs_tree_block_info *bi;
632 btrfs_item_key_to_cpu(leaf, &key, slot);
634 item_size = btrfs_item_size_nr(leaf, slot);
635 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
636 if (item_size < sizeof(*ei)) {
637 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
641 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
642 WARN_ON(!(btrfs_extent_flags(leaf, ei) &
643 BTRFS_EXTENT_FLAG_TREE_BLOCK));
645 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
646 item_size <= sizeof(*ei) + sizeof(*bi)) {
647 WARN_ON(item_size < sizeof(*ei) + sizeof(*bi));
650 if (key.type == BTRFS_METADATA_ITEM_KEY &&
651 item_size <= sizeof(*ei)) {
652 WARN_ON(item_size < sizeof(*ei));
656 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
657 bi = (struct btrfs_tree_block_info *)(ei + 1);
658 *ptr = (unsigned long)(bi + 1);
660 *ptr = (unsigned long)(ei + 1);
662 *end = (unsigned long)ei + item_size;
667 * build backref tree for a given tree block. root of the backref tree
668 * corresponds the tree block, leaves of the backref tree correspond
669 * roots of b-trees that reference the tree block.
671 * the basic idea of this function is check backrefs of a given block
672 * to find upper level blocks that reference the block, and then check
673 * backrefs of these upper level blocks recursively. the recursion stop
674 * when tree root is reached or backrefs for the block is cached.
676 * NOTE: if we find backrefs for a block are cached, we know backrefs
677 * for all upper level blocks that directly/indirectly reference the
678 * block are also cached.
680 static noinline_for_stack
681 struct backref_node *build_backref_tree(struct reloc_control *rc,
682 struct btrfs_key *node_key,
683 int level, u64 bytenr)
685 struct backref_cache *cache = &rc->backref_cache;
686 struct btrfs_path *path1;
687 struct btrfs_path *path2;
688 struct extent_buffer *eb;
689 struct btrfs_root *root;
690 struct backref_node *cur;
691 struct backref_node *upper;
692 struct backref_node *lower;
693 struct backref_node *node = NULL;
694 struct backref_node *exist = NULL;
695 struct backref_edge *edge;
696 struct rb_node *rb_node;
697 struct btrfs_key key;
705 bool need_check = true;
707 path1 = btrfs_alloc_path();
708 path2 = btrfs_alloc_path();
709 if (!path1 || !path2) {
713 path1->reada = READA_FORWARD;
714 path2->reada = READA_FORWARD;
716 node = alloc_backref_node(cache);
722 node->bytenr = bytenr;
729 key.objectid = cur->bytenr;
730 key.type = BTRFS_METADATA_ITEM_KEY;
731 key.offset = (u64)-1;
733 path1->search_commit_root = 1;
734 path1->skip_locking = 1;
735 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path1,
742 ASSERT(path1->slots[0]);
746 WARN_ON(cur->checked);
747 if (!list_empty(&cur->upper)) {
749 * the backref was added previously when processing
750 * backref of type BTRFS_TREE_BLOCK_REF_KEY
752 ASSERT(list_is_singular(&cur->upper));
753 edge = list_entry(cur->upper.next, struct backref_edge,
755 ASSERT(list_empty(&edge->list[UPPER]));
756 exist = edge->node[UPPER];
758 * add the upper level block to pending list if we need
762 list_add_tail(&edge->list[UPPER], &list);
769 eb = path1->nodes[0];
772 if (path1->slots[0] >= btrfs_header_nritems(eb)) {
773 ret = btrfs_next_leaf(rc->extent_root, path1);
780 eb = path1->nodes[0];
783 btrfs_item_key_to_cpu(eb, &key, path1->slots[0]);
784 if (key.objectid != cur->bytenr) {
789 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
790 key.type == BTRFS_METADATA_ITEM_KEY) {
791 ret = find_inline_backref(eb, path1->slots[0],
799 /* update key for inline back ref */
800 struct btrfs_extent_inline_ref *iref;
801 iref = (struct btrfs_extent_inline_ref *)ptr;
802 key.type = btrfs_extent_inline_ref_type(eb, iref);
803 key.offset = btrfs_extent_inline_ref_offset(eb, iref);
804 WARN_ON(key.type != BTRFS_TREE_BLOCK_REF_KEY &&
805 key.type != BTRFS_SHARED_BLOCK_REF_KEY);
809 ((key.type == BTRFS_TREE_BLOCK_REF_KEY &&
810 exist->owner == key.offset) ||
811 (key.type == BTRFS_SHARED_BLOCK_REF_KEY &&
812 exist->bytenr == key.offset))) {
817 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
818 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY ||
819 key.type == BTRFS_EXTENT_REF_V0_KEY) {
820 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
821 struct btrfs_extent_ref_v0 *ref0;
822 ref0 = btrfs_item_ptr(eb, path1->slots[0],
823 struct btrfs_extent_ref_v0);
824 if (key.objectid == key.offset) {
825 root = find_tree_root(rc, eb, ref0);
826 if (root && !should_ignore_root(root))
829 list_add(&cur->list, &useless);
832 if (is_cowonly_root(btrfs_ref_root_v0(eb,
837 ASSERT(key.type != BTRFS_EXTENT_REF_V0_KEY);
838 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
840 if (key.objectid == key.offset) {
842 * only root blocks of reloc trees use
843 * backref of this type.
845 root = find_reloc_root(rc, cur->bytenr);
851 edge = alloc_backref_edge(cache);
856 rb_node = tree_search(&cache->rb_root, key.offset);
858 upper = alloc_backref_node(cache);
860 free_backref_edge(cache, edge);
864 upper->bytenr = key.offset;
865 upper->level = cur->level + 1;
867 * backrefs for the upper level block isn't
868 * cached, add the block to pending list
870 list_add_tail(&edge->list[UPPER], &list);
872 upper = rb_entry(rb_node, struct backref_node,
874 ASSERT(upper->checked);
875 INIT_LIST_HEAD(&edge->list[UPPER]);
877 list_add_tail(&edge->list[LOWER], &cur->upper);
878 edge->node[LOWER] = cur;
879 edge->node[UPPER] = upper;
882 } else if (key.type != BTRFS_TREE_BLOCK_REF_KEY) {
886 /* key.type == BTRFS_TREE_BLOCK_REF_KEY */
887 root = read_fs_root(rc->extent_root->fs_info, key.offset);
893 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
896 if (btrfs_root_level(&root->root_item) == cur->level) {
898 ASSERT(btrfs_root_bytenr(&root->root_item) ==
900 if (should_ignore_root(root))
901 list_add(&cur->list, &useless);
907 level = cur->level + 1;
910 * searching the tree to find upper level blocks
911 * reference the block.
913 path2->search_commit_root = 1;
914 path2->skip_locking = 1;
915 path2->lowest_level = level;
916 ret = btrfs_search_slot(NULL, root, node_key, path2, 0, 0);
917 path2->lowest_level = 0;
922 if (ret > 0 && path2->slots[level] > 0)
923 path2->slots[level]--;
925 eb = path2->nodes[level];
926 WARN_ON(btrfs_node_blockptr(eb, path2->slots[level]) !=
931 for (; level < BTRFS_MAX_LEVEL; level++) {
932 if (!path2->nodes[level]) {
933 ASSERT(btrfs_root_bytenr(&root->root_item) ==
935 if (should_ignore_root(root))
936 list_add(&lower->list, &useless);
942 edge = alloc_backref_edge(cache);
948 eb = path2->nodes[level];
949 rb_node = tree_search(&cache->rb_root, eb->start);
951 upper = alloc_backref_node(cache);
953 free_backref_edge(cache, edge);
957 upper->bytenr = eb->start;
958 upper->owner = btrfs_header_owner(eb);
959 upper->level = lower->level + 1;
960 if (!test_bit(BTRFS_ROOT_REF_COWS,
965 * if we know the block isn't shared
966 * we can void checking its backrefs.
968 if (btrfs_block_can_be_shared(root, eb))
974 * add the block to pending list if we
975 * need check its backrefs, we only do this once
976 * while walking up a tree as we will catch
977 * anything else later on.
979 if (!upper->checked && need_check) {
981 list_add_tail(&edge->list[UPPER],
986 INIT_LIST_HEAD(&edge->list[UPPER]);
989 upper = rb_entry(rb_node, struct backref_node,
991 ASSERT(upper->checked);
992 INIT_LIST_HEAD(&edge->list[UPPER]);
994 upper->owner = btrfs_header_owner(eb);
996 list_add_tail(&edge->list[LOWER], &lower->upper);
997 edge->node[LOWER] = lower;
998 edge->node[UPPER] = upper;
1005 btrfs_release_path(path2);
1008 ptr += btrfs_extent_inline_ref_size(key.type);
1018 btrfs_release_path(path1);
1023 /* the pending list isn't empty, take the first block to process */
1024 if (!list_empty(&list)) {
1025 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
1026 list_del_init(&edge->list[UPPER]);
1027 cur = edge->node[UPPER];
1032 * everything goes well, connect backref nodes and insert backref nodes
1035 ASSERT(node->checked);
1036 cowonly = node->cowonly;
1038 rb_node = tree_insert(&cache->rb_root, node->bytenr,
1041 backref_tree_panic(rb_node, -EEXIST, node->bytenr);
1042 list_add_tail(&node->lower, &cache->leaves);
1045 list_for_each_entry(edge, &node->upper, list[LOWER])
1046 list_add_tail(&edge->list[UPPER], &list);
1048 while (!list_empty(&list)) {
1049 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
1050 list_del_init(&edge->list[UPPER]);
1051 upper = edge->node[UPPER];
1052 if (upper->detached) {
1053 list_del(&edge->list[LOWER]);
1054 lower = edge->node[LOWER];
1055 free_backref_edge(cache, edge);
1056 if (list_empty(&lower->upper))
1057 list_add(&lower->list, &useless);
1061 if (!RB_EMPTY_NODE(&upper->rb_node)) {
1062 if (upper->lowest) {
1063 list_del_init(&upper->lower);
1067 list_add_tail(&edge->list[UPPER], &upper->lower);
1071 if (!upper->checked) {
1073 * Still want to blow up for developers since this is a
1080 if (cowonly != upper->cowonly) {
1087 rb_node = tree_insert(&cache->rb_root, upper->bytenr,
1090 backref_tree_panic(rb_node, -EEXIST,
1094 list_add_tail(&edge->list[UPPER], &upper->lower);
1096 list_for_each_entry(edge, &upper->upper, list[LOWER])
1097 list_add_tail(&edge->list[UPPER], &list);
1100 * process useless backref nodes. backref nodes for tree leaves
1101 * are deleted from the cache. backref nodes for upper level
1102 * tree blocks are left in the cache to avoid unnecessary backref
1105 while (!list_empty(&useless)) {
1106 upper = list_entry(useless.next, struct backref_node, list);
1107 list_del_init(&upper->list);
1108 ASSERT(list_empty(&upper->upper));
1111 if (upper->lowest) {
1112 list_del_init(&upper->lower);
1115 while (!list_empty(&upper->lower)) {
1116 edge = list_entry(upper->lower.next,
1117 struct backref_edge, list[UPPER]);
1118 list_del(&edge->list[UPPER]);
1119 list_del(&edge->list[LOWER]);
1120 lower = edge->node[LOWER];
1121 free_backref_edge(cache, edge);
1123 if (list_empty(&lower->upper))
1124 list_add(&lower->list, &useless);
1126 __mark_block_processed(rc, upper);
1127 if (upper->level > 0) {
1128 list_add(&upper->list, &cache->detached);
1129 upper->detached = 1;
1131 rb_erase(&upper->rb_node, &cache->rb_root);
1132 free_backref_node(cache, upper);
1136 btrfs_free_path(path1);
1137 btrfs_free_path(path2);
1139 while (!list_empty(&useless)) {
1140 lower = list_entry(useless.next,
1141 struct backref_node, list);
1142 list_del_init(&lower->list);
1144 while (!list_empty(&list)) {
1145 edge = list_first_entry(&list, struct backref_edge,
1147 list_del(&edge->list[UPPER]);
1148 list_del(&edge->list[LOWER]);
1149 lower = edge->node[LOWER];
1150 upper = edge->node[UPPER];
1151 free_backref_edge(cache, edge);
1154 * Lower is no longer linked to any upper backref nodes
1155 * and isn't in the cache, we can free it ourselves.
1157 if (list_empty(&lower->upper) &&
1158 RB_EMPTY_NODE(&lower->rb_node))
1159 list_add(&lower->list, &useless);
1161 if (!RB_EMPTY_NODE(&upper->rb_node))
1164 /* Add this guy's upper edges to the list to process */
1165 list_for_each_entry(edge, &upper->upper, list[LOWER])
1166 list_add_tail(&edge->list[UPPER], &list);
1167 if (list_empty(&upper->upper))
1168 list_add(&upper->list, &useless);
1171 while (!list_empty(&useless)) {
1172 lower = list_entry(useless.next,
1173 struct backref_node, list);
1174 list_del_init(&lower->list);
1177 free_backref_node(cache, lower);
1180 free_backref_node(cache, node);
1181 return ERR_PTR(err);
1183 ASSERT(!node || !node->detached);
1188 * helper to add backref node for the newly created snapshot.
1189 * the backref node is created by cloning backref node that
1190 * corresponds to root of source tree
1192 static int clone_backref_node(struct btrfs_trans_handle *trans,
1193 struct reloc_control *rc,
1194 struct btrfs_root *src,
1195 struct btrfs_root *dest)
1197 struct btrfs_root *reloc_root = src->reloc_root;
1198 struct backref_cache *cache = &rc->backref_cache;
1199 struct backref_node *node = NULL;
1200 struct backref_node *new_node;
1201 struct backref_edge *edge;
1202 struct backref_edge *new_edge;
1203 struct rb_node *rb_node;
1205 if (cache->last_trans > 0)
1206 update_backref_cache(trans, cache);
1208 rb_node = tree_search(&cache->rb_root, src->commit_root->start);
1210 node = rb_entry(rb_node, struct backref_node, rb_node);
1214 BUG_ON(node->new_bytenr != reloc_root->node->start);
1218 rb_node = tree_search(&cache->rb_root,
1219 reloc_root->commit_root->start);
1221 node = rb_entry(rb_node, struct backref_node,
1223 BUG_ON(node->detached);
1230 new_node = alloc_backref_node(cache);
1234 new_node->bytenr = dest->node->start;
1235 new_node->level = node->level;
1236 new_node->lowest = node->lowest;
1237 new_node->checked = 1;
1238 new_node->root = dest;
1240 if (!node->lowest) {
1241 list_for_each_entry(edge, &node->lower, list[UPPER]) {
1242 new_edge = alloc_backref_edge(cache);
1246 new_edge->node[UPPER] = new_node;
1247 new_edge->node[LOWER] = edge->node[LOWER];
1248 list_add_tail(&new_edge->list[UPPER],
1252 list_add_tail(&new_node->lower, &cache->leaves);
1255 rb_node = tree_insert(&cache->rb_root, new_node->bytenr,
1256 &new_node->rb_node);
1258 backref_tree_panic(rb_node, -EEXIST, new_node->bytenr);
1260 if (!new_node->lowest) {
1261 list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) {
1262 list_add_tail(&new_edge->list[LOWER],
1263 &new_edge->node[LOWER]->upper);
1268 while (!list_empty(&new_node->lower)) {
1269 new_edge = list_entry(new_node->lower.next,
1270 struct backref_edge, list[UPPER]);
1271 list_del(&new_edge->list[UPPER]);
1272 free_backref_edge(cache, new_edge);
1274 free_backref_node(cache, new_node);
1279 * helper to add 'address of tree root -> reloc tree' mapping
1281 static int __must_check __add_reloc_root(struct btrfs_root *root)
1283 struct rb_node *rb_node;
1284 struct mapping_node *node;
1285 struct reloc_control *rc = root->fs_info->reloc_ctl;
1287 node = kmalloc(sizeof(*node), GFP_NOFS);
1291 node->bytenr = root->node->start;
1294 spin_lock(&rc->reloc_root_tree.lock);
1295 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
1296 node->bytenr, &node->rb_node);
1297 spin_unlock(&rc->reloc_root_tree.lock);
1299 btrfs_panic(root->fs_info, -EEXIST, "Duplicate root found "
1300 "for start=%llu while inserting into relocation "
1301 "tree", node->bytenr);
1306 list_add_tail(&root->root_list, &rc->reloc_roots);
1311 * helper to delete the 'address of tree root -> reloc tree'
1314 static void __del_reloc_root(struct btrfs_root *root)
1316 struct rb_node *rb_node;
1317 struct mapping_node *node = NULL;
1318 struct reloc_control *rc = root->fs_info->reloc_ctl;
1320 spin_lock(&rc->reloc_root_tree.lock);
1321 rb_node = tree_search(&rc->reloc_root_tree.rb_root,
1324 node = rb_entry(rb_node, struct mapping_node, rb_node);
1325 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
1327 spin_unlock(&rc->reloc_root_tree.lock);
1331 BUG_ON((struct btrfs_root *)node->data != root);
1333 spin_lock(&root->fs_info->trans_lock);
1334 list_del_init(&root->root_list);
1335 spin_unlock(&root->fs_info->trans_lock);
1340 * helper to update the 'address of tree root -> reloc tree'
1343 static int __update_reloc_root(struct btrfs_root *root, u64 new_bytenr)
1345 struct rb_node *rb_node;
1346 struct mapping_node *node = NULL;
1347 struct reloc_control *rc = root->fs_info->reloc_ctl;
1349 spin_lock(&rc->reloc_root_tree.lock);
1350 rb_node = tree_search(&rc->reloc_root_tree.rb_root,
1353 node = rb_entry(rb_node, struct mapping_node, rb_node);
1354 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
1356 spin_unlock(&rc->reloc_root_tree.lock);
1360 BUG_ON((struct btrfs_root *)node->data != root);
1362 spin_lock(&rc->reloc_root_tree.lock);
1363 node->bytenr = new_bytenr;
1364 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
1365 node->bytenr, &node->rb_node);
1366 spin_unlock(&rc->reloc_root_tree.lock);
1368 backref_tree_panic(rb_node, -EEXIST, node->bytenr);
1372 static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
1373 struct btrfs_root *root, u64 objectid)
1375 struct btrfs_root *reloc_root;
1376 struct extent_buffer *eb;
1377 struct btrfs_root_item *root_item;
1378 struct btrfs_key root_key;
1382 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
1385 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
1386 root_key.type = BTRFS_ROOT_ITEM_KEY;
1387 root_key.offset = objectid;
1389 if (root->root_key.objectid == objectid) {
1390 /* called by btrfs_init_reloc_root */
1391 ret = btrfs_copy_root(trans, root, root->commit_root, &eb,
1392 BTRFS_TREE_RELOC_OBJECTID);
1395 last_snap = btrfs_root_last_snapshot(&root->root_item);
1396 btrfs_set_root_last_snapshot(&root->root_item,
1397 trans->transid - 1);
1400 * called by btrfs_reloc_post_snapshot_hook.
1401 * the source tree is a reloc tree, all tree blocks
1402 * modified after it was created have RELOC flag
1403 * set in their headers. so it's OK to not update
1404 * the 'last_snapshot'.
1406 ret = btrfs_copy_root(trans, root, root->node, &eb,
1407 BTRFS_TREE_RELOC_OBJECTID);
1411 memcpy(root_item, &root->root_item, sizeof(*root_item));
1412 btrfs_set_root_bytenr(root_item, eb->start);
1413 btrfs_set_root_level(root_item, btrfs_header_level(eb));
1414 btrfs_set_root_generation(root_item, trans->transid);
1416 if (root->root_key.objectid == objectid) {
1417 btrfs_set_root_refs(root_item, 0);
1418 memset(&root_item->drop_progress, 0,
1419 sizeof(struct btrfs_disk_key));
1420 root_item->drop_level = 0;
1422 * abuse rtransid, it is safe because it is impossible to
1423 * receive data into a relocation tree.
1425 btrfs_set_root_rtransid(root_item, last_snap);
1426 btrfs_set_root_otransid(root_item, trans->transid);
1429 btrfs_tree_unlock(eb);
1430 free_extent_buffer(eb);
1432 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
1433 &root_key, root_item);
1437 reloc_root = btrfs_read_fs_root(root->fs_info->tree_root, &root_key);
1438 BUG_ON(IS_ERR(reloc_root));
1439 reloc_root->last_trans = trans->transid;
1444 * create reloc tree for a given fs tree. reloc tree is just a
1445 * snapshot of the fs tree with special root objectid.
1447 int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
1448 struct btrfs_root *root)
1450 struct btrfs_root *reloc_root;
1451 struct reloc_control *rc = root->fs_info->reloc_ctl;
1452 struct btrfs_block_rsv *rsv;
1456 if (root->reloc_root) {
1457 reloc_root = root->reloc_root;
1458 reloc_root->last_trans = trans->transid;
1462 if (!rc || !rc->create_reloc_tree ||
1463 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1466 if (!trans->reloc_reserved) {
1467 rsv = trans->block_rsv;
1468 trans->block_rsv = rc->block_rsv;
1471 reloc_root = create_reloc_root(trans, root, root->root_key.objectid);
1473 trans->block_rsv = rsv;
1475 ret = __add_reloc_root(reloc_root);
1477 root->reloc_root = reloc_root;
1482 * update root item of reloc tree
1484 int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
1485 struct btrfs_root *root)
1487 struct btrfs_root *reloc_root;
1488 struct btrfs_root_item *root_item;
1491 if (!root->reloc_root)
1494 reloc_root = root->reloc_root;
1495 root_item = &reloc_root->root_item;
1497 if (root->fs_info->reloc_ctl->merge_reloc_tree &&
1498 btrfs_root_refs(root_item) == 0) {
1499 root->reloc_root = NULL;
1500 __del_reloc_root(reloc_root);
1503 if (reloc_root->commit_root != reloc_root->node) {
1504 btrfs_set_root_node(root_item, reloc_root->node);
1505 free_extent_buffer(reloc_root->commit_root);
1506 reloc_root->commit_root = btrfs_root_node(reloc_root);
1509 ret = btrfs_update_root(trans, root->fs_info->tree_root,
1510 &reloc_root->root_key, root_item);
1518 * helper to find first cached inode with inode number >= objectid
1521 static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid)
1523 struct rb_node *node;
1524 struct rb_node *prev;
1525 struct btrfs_inode *entry;
1526 struct inode *inode;
1528 spin_lock(&root->inode_lock);
1530 node = root->inode_tree.rb_node;
1534 entry = rb_entry(node, struct btrfs_inode, rb_node);
1536 if (objectid < btrfs_ino(&entry->vfs_inode))
1537 node = node->rb_left;
1538 else if (objectid > btrfs_ino(&entry->vfs_inode))
1539 node = node->rb_right;
1545 entry = rb_entry(prev, struct btrfs_inode, rb_node);
1546 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
1550 prev = rb_next(prev);
1554 entry = rb_entry(node, struct btrfs_inode, rb_node);
1555 inode = igrab(&entry->vfs_inode);
1557 spin_unlock(&root->inode_lock);
1561 objectid = btrfs_ino(&entry->vfs_inode) + 1;
1562 if (cond_resched_lock(&root->inode_lock))
1565 node = rb_next(node);
1567 spin_unlock(&root->inode_lock);
1571 static int in_block_group(u64 bytenr,
1572 struct btrfs_block_group_cache *block_group)
1574 if (bytenr >= block_group->key.objectid &&
1575 bytenr < block_group->key.objectid + block_group->key.offset)
1581 * get new location of data
1583 static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr,
1584 u64 bytenr, u64 num_bytes)
1586 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
1587 struct btrfs_path *path;
1588 struct btrfs_file_extent_item *fi;
1589 struct extent_buffer *leaf;
1592 path = btrfs_alloc_path();
1596 bytenr -= BTRFS_I(reloc_inode)->index_cnt;
1597 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(reloc_inode),
1606 leaf = path->nodes[0];
1607 fi = btrfs_item_ptr(leaf, path->slots[0],
1608 struct btrfs_file_extent_item);
1610 BUG_ON(btrfs_file_extent_offset(leaf, fi) ||
1611 btrfs_file_extent_compression(leaf, fi) ||
1612 btrfs_file_extent_encryption(leaf, fi) ||
1613 btrfs_file_extent_other_encoding(leaf, fi));
1615 if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
1620 *new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1623 btrfs_free_path(path);
1628 * update file extent items in the tree leaf to point to
1629 * the new locations.
1631 static noinline_for_stack
1632 int replace_file_extents(struct btrfs_trans_handle *trans,
1633 struct reloc_control *rc,
1634 struct btrfs_root *root,
1635 struct extent_buffer *leaf)
1637 struct btrfs_key key;
1638 struct btrfs_file_extent_item *fi;
1639 struct inode *inode = NULL;
1651 if (rc->stage != UPDATE_DATA_PTRS)
1654 /* reloc trees always use full backref */
1655 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1656 parent = leaf->start;
1660 nritems = btrfs_header_nritems(leaf);
1661 for (i = 0; i < nritems; i++) {
1663 btrfs_item_key_to_cpu(leaf, &key, i);
1664 if (key.type != BTRFS_EXTENT_DATA_KEY)
1666 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1667 if (btrfs_file_extent_type(leaf, fi) ==
1668 BTRFS_FILE_EXTENT_INLINE)
1670 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1671 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1674 if (!in_block_group(bytenr, rc->block_group))
1678 * if we are modifying block in fs tree, wait for readpage
1679 * to complete and drop the extent cache
1681 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1683 inode = find_next_inode(root, key.objectid);
1685 } else if (inode && btrfs_ino(inode) < key.objectid) {
1686 btrfs_add_delayed_iput(inode);
1687 inode = find_next_inode(root, key.objectid);
1689 if (inode && btrfs_ino(inode) == key.objectid) {
1691 btrfs_file_extent_num_bytes(leaf, fi);
1692 WARN_ON(!IS_ALIGNED(key.offset,
1694 WARN_ON(!IS_ALIGNED(end, root->sectorsize));
1696 ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
1701 btrfs_drop_extent_cache(inode, key.offset, end,
1703 unlock_extent(&BTRFS_I(inode)->io_tree,
1708 ret = get_new_location(rc->data_inode, &new_bytenr,
1712 * Don't have to abort since we've not changed anything
1713 * in the file extent yet.
1718 btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr);
1721 key.offset -= btrfs_file_extent_offset(leaf, fi);
1722 ret = btrfs_inc_extent_ref(trans, root, new_bytenr,
1724 btrfs_header_owner(leaf),
1725 key.objectid, key.offset);
1727 btrfs_abort_transaction(trans, ret);
1731 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1732 parent, btrfs_header_owner(leaf),
1733 key.objectid, key.offset);
1735 btrfs_abort_transaction(trans, ret);
1740 btrfs_mark_buffer_dirty(leaf);
1742 btrfs_add_delayed_iput(inode);
1746 static noinline_for_stack
1747 int memcmp_node_keys(struct extent_buffer *eb, int slot,
1748 struct btrfs_path *path, int level)
1750 struct btrfs_disk_key key1;
1751 struct btrfs_disk_key key2;
1752 btrfs_node_key(eb, &key1, slot);
1753 btrfs_node_key(path->nodes[level], &key2, path->slots[level]);
1754 return memcmp(&key1, &key2, sizeof(key1));
1758 * try to replace tree blocks in fs tree with the new blocks
1759 * in reloc tree. tree blocks haven't been modified since the
1760 * reloc tree was create can be replaced.
1762 * if a block was replaced, level of the block + 1 is returned.
1763 * if no block got replaced, 0 is returned. if there are other
1764 * errors, a negative error number is returned.
1766 static noinline_for_stack
1767 int replace_path(struct btrfs_trans_handle *trans,
1768 struct btrfs_root *dest, struct btrfs_root *src,
1769 struct btrfs_path *path, struct btrfs_key *next_key,
1770 int lowest_level, int max_level)
1772 struct extent_buffer *eb;
1773 struct extent_buffer *parent;
1774 struct btrfs_key key;
1786 BUG_ON(src->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
1787 BUG_ON(dest->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID);
1789 last_snapshot = btrfs_root_last_snapshot(&src->root_item);
1791 slot = path->slots[lowest_level];
1792 btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot);
1794 eb = btrfs_lock_root_node(dest);
1795 btrfs_set_lock_blocking(eb);
1796 level = btrfs_header_level(eb);
1798 if (level < lowest_level) {
1799 btrfs_tree_unlock(eb);
1800 free_extent_buffer(eb);
1805 ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb);
1808 btrfs_set_lock_blocking(eb);
1811 next_key->objectid = (u64)-1;
1812 next_key->type = (u8)-1;
1813 next_key->offset = (u64)-1;
1818 level = btrfs_header_level(parent);
1819 BUG_ON(level < lowest_level);
1821 ret = btrfs_bin_search(parent, &key, level, &slot);
1822 if (ret && slot > 0)
1825 if (next_key && slot + 1 < btrfs_header_nritems(parent))
1826 btrfs_node_key_to_cpu(parent, next_key, slot + 1);
1828 old_bytenr = btrfs_node_blockptr(parent, slot);
1829 blocksize = dest->nodesize;
1830 old_ptr_gen = btrfs_node_ptr_generation(parent, slot);
1832 if (level <= max_level) {
1833 eb = path->nodes[level];
1834 new_bytenr = btrfs_node_blockptr(eb,
1835 path->slots[level]);
1836 new_ptr_gen = btrfs_node_ptr_generation(eb,
1837 path->slots[level]);
1843 if (WARN_ON(new_bytenr > 0 && new_bytenr == old_bytenr)) {
1848 if (new_bytenr == 0 || old_ptr_gen > last_snapshot ||
1849 memcmp_node_keys(parent, slot, path, level)) {
1850 if (level <= lowest_level) {
1855 eb = read_tree_block(dest, old_bytenr, old_ptr_gen);
1859 } else if (!extent_buffer_uptodate(eb)) {
1861 free_extent_buffer(eb);
1864 btrfs_tree_lock(eb);
1866 ret = btrfs_cow_block(trans, dest, eb, parent,
1870 btrfs_set_lock_blocking(eb);
1872 btrfs_tree_unlock(parent);
1873 free_extent_buffer(parent);
1880 btrfs_tree_unlock(parent);
1881 free_extent_buffer(parent);
1886 btrfs_node_key_to_cpu(path->nodes[level], &key,
1887 path->slots[level]);
1888 btrfs_release_path(path);
1890 path->lowest_level = level;
1891 ret = btrfs_search_slot(trans, src, &key, path, 0, 1);
1892 path->lowest_level = 0;
1896 * swap blocks in fs tree and reloc tree.
1898 btrfs_set_node_blockptr(parent, slot, new_bytenr);
1899 btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen);
1900 btrfs_mark_buffer_dirty(parent);
1902 btrfs_set_node_blockptr(path->nodes[level],
1903 path->slots[level], old_bytenr);
1904 btrfs_set_node_ptr_generation(path->nodes[level],
1905 path->slots[level], old_ptr_gen);
1906 btrfs_mark_buffer_dirty(path->nodes[level]);
1908 ret = btrfs_inc_extent_ref(trans, src, old_bytenr, blocksize,
1909 path->nodes[level]->start,
1910 src->root_key.objectid, level - 1, 0);
1912 ret = btrfs_inc_extent_ref(trans, dest, new_bytenr, blocksize,
1913 0, dest->root_key.objectid, level - 1,
1917 ret = btrfs_free_extent(trans, src, new_bytenr, blocksize,
1918 path->nodes[level]->start,
1919 src->root_key.objectid, level - 1, 0);
1922 ret = btrfs_free_extent(trans, dest, old_bytenr, blocksize,
1923 0, dest->root_key.objectid, level - 1,
1927 btrfs_unlock_up_safe(path, 0);
1932 btrfs_tree_unlock(parent);
1933 free_extent_buffer(parent);
1938 * helper to find next relocated block in reloc tree
1940 static noinline_for_stack
1941 int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1944 struct extent_buffer *eb;
1949 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1951 for (i = 0; i < *level; i++) {
1952 free_extent_buffer(path->nodes[i]);
1953 path->nodes[i] = NULL;
1956 for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
1957 eb = path->nodes[i];
1958 nritems = btrfs_header_nritems(eb);
1959 while (path->slots[i] + 1 < nritems) {
1961 if (btrfs_node_ptr_generation(eb, path->slots[i]) <=
1968 free_extent_buffer(path->nodes[i]);
1969 path->nodes[i] = NULL;
1975 * walk down reloc tree to find relocated block of lowest level
1977 static noinline_for_stack
1978 int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1981 struct extent_buffer *eb = NULL;
1988 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1990 for (i = *level; i > 0; i--) {
1991 eb = path->nodes[i];
1992 nritems = btrfs_header_nritems(eb);
1993 while (path->slots[i] < nritems) {
1994 ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]);
1995 if (ptr_gen > last_snapshot)
1999 if (path->slots[i] >= nritems) {
2010 bytenr = btrfs_node_blockptr(eb, path->slots[i]);
2011 eb = read_tree_block(root, bytenr, ptr_gen);
2014 } else if (!extent_buffer_uptodate(eb)) {
2015 free_extent_buffer(eb);
2018 BUG_ON(btrfs_header_level(eb) != i - 1);
2019 path->nodes[i - 1] = eb;
2020 path->slots[i - 1] = 0;
2026 * invalidate extent cache for file extents whose key in range of
2027 * [min_key, max_key)
2029 static int invalidate_extent_cache(struct btrfs_root *root,
2030 struct btrfs_key *min_key,
2031 struct btrfs_key *max_key)
2033 struct inode *inode = NULL;
2038 objectid = min_key->objectid;
2043 if (objectid > max_key->objectid)
2046 inode = find_next_inode(root, objectid);
2049 ino = btrfs_ino(inode);
2051 if (ino > max_key->objectid) {
2057 if (!S_ISREG(inode->i_mode))
2060 if (unlikely(min_key->objectid == ino)) {
2061 if (min_key->type > BTRFS_EXTENT_DATA_KEY)
2063 if (min_key->type < BTRFS_EXTENT_DATA_KEY)
2066 start = min_key->offset;
2067 WARN_ON(!IS_ALIGNED(start, root->sectorsize));
2073 if (unlikely(max_key->objectid == ino)) {
2074 if (max_key->type < BTRFS_EXTENT_DATA_KEY)
2076 if (max_key->type > BTRFS_EXTENT_DATA_KEY) {
2079 if (max_key->offset == 0)
2081 end = max_key->offset;
2082 WARN_ON(!IS_ALIGNED(end, root->sectorsize));
2089 /* the lock_extent waits for readpage to complete */
2090 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
2091 btrfs_drop_extent_cache(inode, start, end, 1);
2092 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
2097 static int find_next_key(struct btrfs_path *path, int level,
2098 struct btrfs_key *key)
2101 while (level < BTRFS_MAX_LEVEL) {
2102 if (!path->nodes[level])
2104 if (path->slots[level] + 1 <
2105 btrfs_header_nritems(path->nodes[level])) {
2106 btrfs_node_key_to_cpu(path->nodes[level], key,
2107 path->slots[level] + 1);
2116 * merge the relocated tree blocks in reloc tree with corresponding
2119 static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
2120 struct btrfs_root *root)
2122 LIST_HEAD(inode_list);
2123 struct btrfs_key key;
2124 struct btrfs_key next_key;
2125 struct btrfs_trans_handle *trans = NULL;
2126 struct btrfs_root *reloc_root;
2127 struct btrfs_root_item *root_item;
2128 struct btrfs_path *path;
2129 struct extent_buffer *leaf;
2137 path = btrfs_alloc_path();
2140 path->reada = READA_FORWARD;
2142 reloc_root = root->reloc_root;
2143 root_item = &reloc_root->root_item;
2145 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2146 level = btrfs_root_level(root_item);
2147 extent_buffer_get(reloc_root->node);
2148 path->nodes[level] = reloc_root->node;
2149 path->slots[level] = 0;
2151 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2153 level = root_item->drop_level;
2155 path->lowest_level = level;
2156 ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0);
2157 path->lowest_level = 0;
2159 btrfs_free_path(path);
2163 btrfs_node_key_to_cpu(path->nodes[level], &next_key,
2164 path->slots[level]);
2165 WARN_ON(memcmp(&key, &next_key, sizeof(key)));
2167 btrfs_unlock_up_safe(path, 0);
2170 min_reserved = root->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
2171 memset(&next_key, 0, sizeof(next_key));
2174 ret = btrfs_block_rsv_refill(root, rc->block_rsv, min_reserved,
2175 BTRFS_RESERVE_FLUSH_ALL);
2180 trans = btrfs_start_transaction(root, 0);
2181 if (IS_ERR(trans)) {
2182 err = PTR_ERR(trans);
2186 trans->block_rsv = rc->block_rsv;
2191 ret = walk_down_reloc_tree(reloc_root, path, &level);
2199 if (!find_next_key(path, level, &key) &&
2200 btrfs_comp_cpu_keys(&next_key, &key) >= 0) {
2203 ret = replace_path(trans, root, reloc_root, path,
2204 &next_key, level, max_level);
2213 btrfs_node_key_to_cpu(path->nodes[level], &key,
2214 path->slots[level]);
2218 ret = walk_up_reloc_tree(reloc_root, path, &level);
2224 * save the merging progress in the drop_progress.
2225 * this is OK since root refs == 1 in this case.
2227 btrfs_node_key(path->nodes[level], &root_item->drop_progress,
2228 path->slots[level]);
2229 root_item->drop_level = level;
2231 btrfs_end_transaction_throttle(trans, root);
2234 btrfs_btree_balance_dirty(root);
2236 if (replaced && rc->stage == UPDATE_DATA_PTRS)
2237 invalidate_extent_cache(root, &key, &next_key);
2241 * handle the case only one block in the fs tree need to be
2242 * relocated and the block is tree root.
2244 leaf = btrfs_lock_root_node(root);
2245 ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf);
2246 btrfs_tree_unlock(leaf);
2247 free_extent_buffer(leaf);
2251 btrfs_free_path(path);
2254 memset(&root_item->drop_progress, 0,
2255 sizeof(root_item->drop_progress));
2256 root_item->drop_level = 0;
2257 btrfs_set_root_refs(root_item, 0);
2258 btrfs_update_reloc_root(trans, root);
2262 btrfs_end_transaction_throttle(trans, root);
2264 btrfs_btree_balance_dirty(root);
2266 if (replaced && rc->stage == UPDATE_DATA_PTRS)
2267 invalidate_extent_cache(root, &key, &next_key);
2272 static noinline_for_stack
2273 int prepare_to_merge(struct reloc_control *rc, int err)
2275 struct btrfs_root *root = rc->extent_root;
2276 struct btrfs_root *reloc_root;
2277 struct btrfs_trans_handle *trans;
2278 LIST_HEAD(reloc_roots);
2282 mutex_lock(&root->fs_info->reloc_mutex);
2283 rc->merging_rsv_size += root->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
2284 rc->merging_rsv_size += rc->nodes_relocated * 2;
2285 mutex_unlock(&root->fs_info->reloc_mutex);
2289 num_bytes = rc->merging_rsv_size;
2290 ret = btrfs_block_rsv_add(root, rc->block_rsv, num_bytes,
2291 BTRFS_RESERVE_FLUSH_ALL);
2296 trans = btrfs_join_transaction(rc->extent_root);
2297 if (IS_ERR(trans)) {
2299 btrfs_block_rsv_release(rc->extent_root,
2300 rc->block_rsv, num_bytes);
2301 return PTR_ERR(trans);
2305 if (num_bytes != rc->merging_rsv_size) {
2306 btrfs_end_transaction(trans, rc->extent_root);
2307 btrfs_block_rsv_release(rc->extent_root,
2308 rc->block_rsv, num_bytes);
2313 rc->merge_reloc_tree = 1;
2315 while (!list_empty(&rc->reloc_roots)) {
2316 reloc_root = list_entry(rc->reloc_roots.next,
2317 struct btrfs_root, root_list);
2318 list_del_init(&reloc_root->root_list);
2320 root = read_fs_root(reloc_root->fs_info,
2321 reloc_root->root_key.offset);
2322 BUG_ON(IS_ERR(root));
2323 BUG_ON(root->reloc_root != reloc_root);
2326 * set reference count to 1, so btrfs_recover_relocation
2327 * knows it should resumes merging
2330 btrfs_set_root_refs(&reloc_root->root_item, 1);
2331 btrfs_update_reloc_root(trans, root);
2333 list_add(&reloc_root->root_list, &reloc_roots);
2336 list_splice(&reloc_roots, &rc->reloc_roots);
2339 btrfs_commit_transaction(trans, rc->extent_root);
2341 btrfs_end_transaction(trans, rc->extent_root);
2345 static noinline_for_stack
2346 void free_reloc_roots(struct list_head *list)
2348 struct btrfs_root *reloc_root;
2350 while (!list_empty(list)) {
2351 reloc_root = list_entry(list->next, struct btrfs_root,
2353 __del_reloc_root(reloc_root);
2357 static noinline_for_stack
2358 void merge_reloc_roots(struct reloc_control *rc)
2360 struct btrfs_root *root;
2361 struct btrfs_root *reloc_root;
2365 LIST_HEAD(reloc_roots);
2369 root = rc->extent_root;
2372 * this serializes us with btrfs_record_root_in_transaction,
2373 * we have to make sure nobody is in the middle of
2374 * adding their roots to the list while we are
2377 mutex_lock(&root->fs_info->reloc_mutex);
2378 list_splice_init(&rc->reloc_roots, &reloc_roots);
2379 mutex_unlock(&root->fs_info->reloc_mutex);
2381 while (!list_empty(&reloc_roots)) {
2383 reloc_root = list_entry(reloc_roots.next,
2384 struct btrfs_root, root_list);
2386 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
2387 root = read_fs_root(reloc_root->fs_info,
2388 reloc_root->root_key.offset);
2389 BUG_ON(IS_ERR(root));
2390 BUG_ON(root->reloc_root != reloc_root);
2392 ret = merge_reloc_root(rc, root);
2394 if (list_empty(&reloc_root->root_list))
2395 list_add_tail(&reloc_root->root_list,
2400 list_del_init(&reloc_root->root_list);
2404 * we keep the old last snapshot transid in rtranid when we
2405 * created the relocation tree.
2407 last_snap = btrfs_root_rtransid(&reloc_root->root_item);
2408 otransid = btrfs_root_otransid(&reloc_root->root_item);
2409 objectid = reloc_root->root_key.offset;
2411 ret = btrfs_drop_snapshot(reloc_root, rc->block_rsv, 0, 1);
2413 if (list_empty(&reloc_root->root_list))
2414 list_add_tail(&reloc_root->root_list,
2426 btrfs_handle_fs_error(root->fs_info, ret, NULL);
2427 if (!list_empty(&reloc_roots))
2428 free_reloc_roots(&reloc_roots);
2430 /* new reloc root may be added */
2431 mutex_lock(&root->fs_info->reloc_mutex);
2432 list_splice_init(&rc->reloc_roots, &reloc_roots);
2433 mutex_unlock(&root->fs_info->reloc_mutex);
2434 if (!list_empty(&reloc_roots))
2435 free_reloc_roots(&reloc_roots);
2438 BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
2441 static void free_block_list(struct rb_root *blocks)
2443 struct tree_block *block;
2444 struct rb_node *rb_node;
2445 while ((rb_node = rb_first(blocks))) {
2446 block = rb_entry(rb_node, struct tree_block, rb_node);
2447 rb_erase(rb_node, blocks);
2452 static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans,
2453 struct btrfs_root *reloc_root)
2455 struct btrfs_root *root;
2457 if (reloc_root->last_trans == trans->transid)
2460 root = read_fs_root(reloc_root->fs_info, reloc_root->root_key.offset);
2461 BUG_ON(IS_ERR(root));
2462 BUG_ON(root->reloc_root != reloc_root);
2464 return btrfs_record_root_in_trans(trans, root);
2467 static noinline_for_stack
2468 struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,
2469 struct reloc_control *rc,
2470 struct backref_node *node,
2471 struct backref_edge *edges[])
2473 struct backref_node *next;
2474 struct btrfs_root *root;
2480 next = walk_up_backref(next, edges, &index);
2483 BUG_ON(!test_bit(BTRFS_ROOT_REF_COWS, &root->state));
2485 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
2486 record_reloc_root_in_trans(trans, root);
2490 btrfs_record_root_in_trans(trans, root);
2491 root = root->reloc_root;
2493 if (next->new_bytenr != root->node->start) {
2494 BUG_ON(next->new_bytenr);
2495 BUG_ON(!list_empty(&next->list));
2496 next->new_bytenr = root->node->start;
2498 list_add_tail(&next->list,
2499 &rc->backref_cache.changed);
2500 __mark_block_processed(rc, next);
2506 next = walk_down_backref(edges, &index);
2507 if (!next || next->level <= node->level)
2514 /* setup backref node path for btrfs_reloc_cow_block */
2516 rc->backref_cache.path[next->level] = next;
2519 next = edges[index]->node[UPPER];
2525 * select a tree root for relocation. return NULL if the block
2526 * is reference counted. we should use do_relocation() in this
2527 * case. return a tree root pointer if the block isn't reference
2528 * counted. return -ENOENT if the block is root of reloc tree.
2530 static noinline_for_stack
2531 struct btrfs_root *select_one_root(struct backref_node *node)
2533 struct backref_node *next;
2534 struct btrfs_root *root;
2535 struct btrfs_root *fs_root = NULL;
2536 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2542 next = walk_up_backref(next, edges, &index);
2546 /* no other choice for non-references counted tree */
2547 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
2550 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)
2556 next = walk_down_backref(edges, &index);
2557 if (!next || next->level <= node->level)
2562 return ERR_PTR(-ENOENT);
2566 static noinline_for_stack
2567 u64 calcu_metadata_size(struct reloc_control *rc,
2568 struct backref_node *node, int reserve)
2570 struct backref_node *next = node;
2571 struct backref_edge *edge;
2572 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2576 BUG_ON(reserve && node->processed);
2581 if (next->processed && (reserve || next != node))
2584 num_bytes += rc->extent_root->nodesize;
2586 if (list_empty(&next->upper))
2589 edge = list_entry(next->upper.next,
2590 struct backref_edge, list[LOWER]);
2591 edges[index++] = edge;
2592 next = edge->node[UPPER];
2594 next = walk_down_backref(edges, &index);
2599 static int reserve_metadata_space(struct btrfs_trans_handle *trans,
2600 struct reloc_control *rc,
2601 struct backref_node *node)
2603 struct btrfs_root *root = rc->extent_root;
2608 num_bytes = calcu_metadata_size(rc, node, 1) * 2;
2610 trans->block_rsv = rc->block_rsv;
2611 rc->reserved_bytes += num_bytes;
2614 * We are under a transaction here so we can only do limited flushing.
2615 * If we get an enospc just kick back -EAGAIN so we know to drop the
2616 * transaction and try to refill when we can flush all the things.
2618 ret = btrfs_block_rsv_refill(root, rc->block_rsv, num_bytes,
2619 BTRFS_RESERVE_FLUSH_LIMIT);
2621 tmp = rc->extent_root->nodesize * RELOCATION_RESERVED_NODES;
2622 while (tmp <= rc->reserved_bytes)
2625 * only one thread can access block_rsv at this point,
2626 * so we don't need hold lock to protect block_rsv.
2627 * we expand more reservation size here to allow enough
2628 * space for relocation and we will return eailer in
2631 rc->block_rsv->size = tmp + rc->extent_root->nodesize *
2632 RELOCATION_RESERVED_NODES;
2640 * relocate a block tree, and then update pointers in upper level
2641 * blocks that reference the block to point to the new location.
2643 * if called by link_to_upper, the block has already been relocated.
2644 * in that case this function just updates pointers.
2646 static int do_relocation(struct btrfs_trans_handle *trans,
2647 struct reloc_control *rc,
2648 struct backref_node *node,
2649 struct btrfs_key *key,
2650 struct btrfs_path *path, int lowest)
2652 struct backref_node *upper;
2653 struct backref_edge *edge;
2654 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2655 struct btrfs_root *root;
2656 struct extent_buffer *eb;
2664 BUG_ON(lowest && node->eb);
2666 path->lowest_level = node->level + 1;
2667 rc->backref_cache.path[node->level] = node;
2668 list_for_each_entry(edge, &node->upper, list[LOWER]) {
2671 upper = edge->node[UPPER];
2672 root = select_reloc_root(trans, rc, upper, edges);
2675 if (upper->eb && !upper->locked) {
2677 ret = btrfs_bin_search(upper->eb, key,
2678 upper->level, &slot);
2680 bytenr = btrfs_node_blockptr(upper->eb, slot);
2681 if (node->eb->start == bytenr)
2684 drop_node_buffer(upper);
2688 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2696 upper->eb = path->nodes[upper->level];
2697 path->nodes[upper->level] = NULL;
2699 BUG_ON(upper->eb != path->nodes[upper->level]);
2703 path->locks[upper->level] = 0;
2705 slot = path->slots[upper->level];
2706 btrfs_release_path(path);
2708 ret = btrfs_bin_search(upper->eb, key, upper->level,
2713 bytenr = btrfs_node_blockptr(upper->eb, slot);
2715 BUG_ON(bytenr != node->bytenr);
2717 if (node->eb->start == bytenr)
2721 blocksize = root->nodesize;
2722 generation = btrfs_node_ptr_generation(upper->eb, slot);
2723 eb = read_tree_block(root, bytenr, generation);
2727 } else if (!extent_buffer_uptodate(eb)) {
2728 free_extent_buffer(eb);
2732 btrfs_tree_lock(eb);
2733 btrfs_set_lock_blocking(eb);
2736 ret = btrfs_cow_block(trans, root, eb, upper->eb,
2738 btrfs_tree_unlock(eb);
2739 free_extent_buffer(eb);
2744 BUG_ON(node->eb != eb);
2746 btrfs_set_node_blockptr(upper->eb, slot,
2748 btrfs_set_node_ptr_generation(upper->eb, slot,
2750 btrfs_mark_buffer_dirty(upper->eb);
2752 ret = btrfs_inc_extent_ref(trans, root,
2753 node->eb->start, blocksize,
2755 btrfs_header_owner(upper->eb),
2759 ret = btrfs_drop_subtree(trans, root, eb, upper->eb);
2763 if (!upper->pending)
2764 drop_node_buffer(upper);
2766 unlock_node_buffer(upper);
2771 if (!err && node->pending) {
2772 drop_node_buffer(node);
2773 list_move_tail(&node->list, &rc->backref_cache.changed);
2777 path->lowest_level = 0;
2778 BUG_ON(err == -ENOSPC);
2782 static int link_to_upper(struct btrfs_trans_handle *trans,
2783 struct reloc_control *rc,
2784 struct backref_node *node,
2785 struct btrfs_path *path)
2787 struct btrfs_key key;
2789 btrfs_node_key_to_cpu(node->eb, &key, 0);
2790 return do_relocation(trans, rc, node, &key, path, 0);
2793 static int finish_pending_nodes(struct btrfs_trans_handle *trans,
2794 struct reloc_control *rc,
2795 struct btrfs_path *path, int err)
2798 struct backref_cache *cache = &rc->backref_cache;
2799 struct backref_node *node;
2803 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2804 while (!list_empty(&cache->pending[level])) {
2805 node = list_entry(cache->pending[level].next,
2806 struct backref_node, list);
2807 list_move_tail(&node->list, &list);
2808 BUG_ON(!node->pending);
2811 ret = link_to_upper(trans, rc, node, path);
2816 list_splice_init(&list, &cache->pending[level]);
2821 static void mark_block_processed(struct reloc_control *rc,
2822 u64 bytenr, u32 blocksize)
2824 set_extent_bits(&rc->processed_blocks, bytenr, bytenr + blocksize - 1,
2828 static void __mark_block_processed(struct reloc_control *rc,
2829 struct backref_node *node)
2832 if (node->level == 0 ||
2833 in_block_group(node->bytenr, rc->block_group)) {
2834 blocksize = rc->extent_root->nodesize;
2835 mark_block_processed(rc, node->bytenr, blocksize);
2837 node->processed = 1;
2841 * mark a block and all blocks directly/indirectly reference the block
2844 static void update_processed_blocks(struct reloc_control *rc,
2845 struct backref_node *node)
2847 struct backref_node *next = node;
2848 struct backref_edge *edge;
2849 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2855 if (next->processed)
2858 __mark_block_processed(rc, next);
2860 if (list_empty(&next->upper))
2863 edge = list_entry(next->upper.next,
2864 struct backref_edge, list[LOWER]);
2865 edges[index++] = edge;
2866 next = edge->node[UPPER];
2868 next = walk_down_backref(edges, &index);
2872 static int tree_block_processed(u64 bytenr, struct reloc_control *rc)
2874 u32 blocksize = rc->extent_root->nodesize;
2876 if (test_range_bit(&rc->processed_blocks, bytenr,
2877 bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL))
2882 static int get_tree_block_key(struct reloc_control *rc,
2883 struct tree_block *block)
2885 struct extent_buffer *eb;
2887 BUG_ON(block->key_ready);
2888 eb = read_tree_block(rc->extent_root, block->bytenr,
2892 } else if (!extent_buffer_uptodate(eb)) {
2893 free_extent_buffer(eb);
2896 WARN_ON(btrfs_header_level(eb) != block->level);
2897 if (block->level == 0)
2898 btrfs_item_key_to_cpu(eb, &block->key, 0);
2900 btrfs_node_key_to_cpu(eb, &block->key, 0);
2901 free_extent_buffer(eb);
2902 block->key_ready = 1;
2907 * helper function to relocate a tree block
2909 static int relocate_tree_block(struct btrfs_trans_handle *trans,
2910 struct reloc_control *rc,
2911 struct backref_node *node,
2912 struct btrfs_key *key,
2913 struct btrfs_path *path)
2915 struct btrfs_root *root;
2921 BUG_ON(node->processed);
2922 root = select_one_root(node);
2923 if (root == ERR_PTR(-ENOENT)) {
2924 update_processed_blocks(rc, node);
2928 if (!root || test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
2929 ret = reserve_metadata_space(trans, rc, node);
2935 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
2936 BUG_ON(node->new_bytenr);
2937 BUG_ON(!list_empty(&node->list));
2938 btrfs_record_root_in_trans(trans, root);
2939 root = root->reloc_root;
2940 node->new_bytenr = root->node->start;
2942 list_add_tail(&node->list, &rc->backref_cache.changed);
2944 path->lowest_level = node->level;
2945 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2946 btrfs_release_path(path);
2951 update_processed_blocks(rc, node);
2953 ret = do_relocation(trans, rc, node, key, path, 1);
2956 if (ret || node->level == 0 || node->cowonly)
2957 remove_backref_node(&rc->backref_cache, node);
2962 * relocate a list of blocks
2964 static noinline_for_stack
2965 int relocate_tree_blocks(struct btrfs_trans_handle *trans,
2966 struct reloc_control *rc, struct rb_root *blocks)
2968 struct backref_node *node;
2969 struct btrfs_path *path;
2970 struct tree_block *block;
2971 struct rb_node *rb_node;
2975 path = btrfs_alloc_path();
2978 goto out_free_blocks;
2981 rb_node = rb_first(blocks);
2983 block = rb_entry(rb_node, struct tree_block, rb_node);
2984 if (!block->key_ready)
2985 readahead_tree_block(rc->extent_root, block->bytenr);
2986 rb_node = rb_next(rb_node);
2989 rb_node = rb_first(blocks);
2991 block = rb_entry(rb_node, struct tree_block, rb_node);
2992 if (!block->key_ready) {
2993 err = get_tree_block_key(rc, block);
2997 rb_node = rb_next(rb_node);
3000 rb_node = rb_first(blocks);
3002 block = rb_entry(rb_node, struct tree_block, rb_node);
3004 node = build_backref_tree(rc, &block->key,
3005 block->level, block->bytenr);
3007 err = PTR_ERR(node);
3011 ret = relocate_tree_block(trans, rc, node, &block->key,
3014 if (ret != -EAGAIN || rb_node == rb_first(blocks))
3018 rb_node = rb_next(rb_node);
3021 err = finish_pending_nodes(trans, rc, path, err);
3024 btrfs_free_path(path);
3026 free_block_list(blocks);
3030 static noinline_for_stack
3031 int prealloc_file_extent_cluster(struct inode *inode,
3032 struct file_extent_cluster *cluster)
3037 u64 offset = BTRFS_I(inode)->index_cnt;
3041 u64 prealloc_start = cluster->start - offset;
3042 u64 prealloc_end = cluster->end - offset;
3045 BUG_ON(cluster->start != cluster->boundary[0]);
3048 ret = btrfs_check_data_free_space(inode, prealloc_start,
3049 prealloc_end + 1 - prealloc_start);
3053 cur_offset = prealloc_start;
3054 while (nr < cluster->nr) {
3055 start = cluster->boundary[nr] - offset;
3056 if (nr + 1 < cluster->nr)
3057 end = cluster->boundary[nr + 1] - 1 - offset;
3059 end = cluster->end - offset;
3061 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
3062 num_bytes = end + 1 - start;
3063 if (cur_offset < start)
3064 btrfs_free_reserved_data_space(inode, cur_offset,
3065 start - cur_offset);
3066 ret = btrfs_prealloc_file_range(inode, 0, start,
3067 num_bytes, num_bytes,
3068 end + 1, &alloc_hint);
3069 cur_offset = end + 1;
3070 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
3075 if (cur_offset < prealloc_end)
3076 btrfs_free_reserved_data_space(inode, cur_offset,
3077 prealloc_end + 1 - cur_offset);
3079 inode_unlock(inode);
3083 static noinline_for_stack
3084 int setup_extent_mapping(struct inode *inode, u64 start, u64 end,
3087 struct btrfs_root *root = BTRFS_I(inode)->root;
3088 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3089 struct extent_map *em;
3092 em = alloc_extent_map();
3097 em->len = end + 1 - start;
3098 em->block_len = em->len;
3099 em->block_start = block_start;
3100 em->bdev = root->fs_info->fs_devices->latest_bdev;
3101 set_bit(EXTENT_FLAG_PINNED, &em->flags);
3103 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
3105 write_lock(&em_tree->lock);
3106 ret = add_extent_mapping(em_tree, em, 0);
3107 write_unlock(&em_tree->lock);
3108 if (ret != -EEXIST) {
3109 free_extent_map(em);
3112 btrfs_drop_extent_cache(inode, start, end, 0);
3114 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
3118 static int relocate_file_extent_cluster(struct inode *inode,
3119 struct file_extent_cluster *cluster)
3123 u64 offset = BTRFS_I(inode)->index_cnt;
3124 unsigned long index;
3125 unsigned long last_index;
3127 struct file_ra_state *ra;
3128 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
3135 ra = kzalloc(sizeof(*ra), GFP_NOFS);
3139 ret = prealloc_file_extent_cluster(inode, cluster);
3143 file_ra_state_init(ra, inode->i_mapping);
3145 ret = setup_extent_mapping(inode, cluster->start - offset,
3146 cluster->end - offset, cluster->start);
3150 index = (cluster->start - offset) >> PAGE_SHIFT;
3151 last_index = (cluster->end - offset) >> PAGE_SHIFT;
3152 while (index <= last_index) {
3153 ret = btrfs_delalloc_reserve_metadata(inode, PAGE_SIZE);
3157 page = find_lock_page(inode->i_mapping, index);
3159 page_cache_sync_readahead(inode->i_mapping,
3161 last_index + 1 - index);
3162 page = find_or_create_page(inode->i_mapping, index,
3165 btrfs_delalloc_release_metadata(inode,
3172 if (PageReadahead(page)) {
3173 page_cache_async_readahead(inode->i_mapping,
3174 ra, NULL, page, index,
3175 last_index + 1 - index);
3178 if (!PageUptodate(page)) {
3179 btrfs_readpage(NULL, page);
3181 if (!PageUptodate(page)) {
3184 btrfs_delalloc_release_metadata(inode,
3191 page_start = page_offset(page);
3192 page_end = page_start + PAGE_SIZE - 1;
3194 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end);
3196 set_page_extent_mapped(page);
3198 if (nr < cluster->nr &&
3199 page_start + offset == cluster->boundary[nr]) {
3200 set_extent_bits(&BTRFS_I(inode)->io_tree,
3201 page_start, page_end,
3206 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
3207 set_page_dirty(page);
3209 unlock_extent(&BTRFS_I(inode)->io_tree,
3210 page_start, page_end);
3215 balance_dirty_pages_ratelimited(inode->i_mapping);
3216 btrfs_throttle(BTRFS_I(inode)->root);
3218 WARN_ON(nr != cluster->nr);
3224 static noinline_for_stack
3225 int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key,
3226 struct file_extent_cluster *cluster)
3230 if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) {
3231 ret = relocate_file_extent_cluster(inode, cluster);
3238 cluster->start = extent_key->objectid;
3240 BUG_ON(cluster->nr >= MAX_EXTENTS);
3241 cluster->end = extent_key->objectid + extent_key->offset - 1;
3242 cluster->boundary[cluster->nr] = extent_key->objectid;
3245 if (cluster->nr >= MAX_EXTENTS) {
3246 ret = relocate_file_extent_cluster(inode, cluster);
3254 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3255 static int get_ref_objectid_v0(struct reloc_control *rc,
3256 struct btrfs_path *path,
3257 struct btrfs_key *extent_key,
3258 u64 *ref_objectid, int *path_change)
3260 struct btrfs_key key;
3261 struct extent_buffer *leaf;
3262 struct btrfs_extent_ref_v0 *ref0;
3266 leaf = path->nodes[0];
3267 slot = path->slots[0];
3269 if (slot >= btrfs_header_nritems(leaf)) {
3270 ret = btrfs_next_leaf(rc->extent_root, path);
3274 leaf = path->nodes[0];
3275 slot = path->slots[0];
3279 btrfs_item_key_to_cpu(leaf, &key, slot);
3280 if (key.objectid != extent_key->objectid)
3283 if (key.type != BTRFS_EXTENT_REF_V0_KEY) {
3287 ref0 = btrfs_item_ptr(leaf, slot,
3288 struct btrfs_extent_ref_v0);
3289 *ref_objectid = btrfs_ref_objectid_v0(leaf, ref0);
3297 * helper to add a tree block to the list.
3298 * the major work is getting the generation and level of the block
3300 static int add_tree_block(struct reloc_control *rc,
3301 struct btrfs_key *extent_key,
3302 struct btrfs_path *path,
3303 struct rb_root *blocks)
3305 struct extent_buffer *eb;
3306 struct btrfs_extent_item *ei;
3307 struct btrfs_tree_block_info *bi;
3308 struct tree_block *block;
3309 struct rb_node *rb_node;
3314 eb = path->nodes[0];
3315 item_size = btrfs_item_size_nr(eb, path->slots[0]);
3317 if (extent_key->type == BTRFS_METADATA_ITEM_KEY ||
3318 item_size >= sizeof(*ei) + sizeof(*bi)) {
3319 ei = btrfs_item_ptr(eb, path->slots[0],
3320 struct btrfs_extent_item);
3321 if (extent_key->type == BTRFS_EXTENT_ITEM_KEY) {
3322 bi = (struct btrfs_tree_block_info *)(ei + 1);
3323 level = btrfs_tree_block_level(eb, bi);
3325 level = (int)extent_key->offset;
3327 generation = btrfs_extent_generation(eb, ei);
3329 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3333 BUG_ON(item_size != sizeof(struct btrfs_extent_item_v0));
3334 ret = get_ref_objectid_v0(rc, path, extent_key,
3338 BUG_ON(ref_owner >= BTRFS_MAX_LEVEL);
3339 level = (int)ref_owner;
3340 /* FIXME: get real generation */
3347 btrfs_release_path(path);
3349 BUG_ON(level == -1);
3351 block = kmalloc(sizeof(*block), GFP_NOFS);
3355 block->bytenr = extent_key->objectid;
3356 block->key.objectid = rc->extent_root->nodesize;
3357 block->key.offset = generation;
3358 block->level = level;
3359 block->key_ready = 0;
3361 rb_node = tree_insert(blocks, block->bytenr, &block->rb_node);
3363 backref_tree_panic(rb_node, -EEXIST, block->bytenr);
3369 * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
3371 static int __add_tree_block(struct reloc_control *rc,
3372 u64 bytenr, u32 blocksize,
3373 struct rb_root *blocks)
3375 struct btrfs_path *path;
3376 struct btrfs_key key;
3378 bool skinny = btrfs_fs_incompat(rc->extent_root->fs_info,
3381 if (tree_block_processed(bytenr, rc))
3384 if (tree_search(blocks, bytenr))
3387 path = btrfs_alloc_path();
3391 key.objectid = bytenr;
3393 key.type = BTRFS_METADATA_ITEM_KEY;
3394 key.offset = (u64)-1;
3396 key.type = BTRFS_EXTENT_ITEM_KEY;
3397 key.offset = blocksize;
3400 path->search_commit_root = 1;
3401 path->skip_locking = 1;
3402 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0);
3406 if (ret > 0 && skinny) {
3407 if (path->slots[0]) {
3409 btrfs_item_key_to_cpu(path->nodes[0], &key,
3411 if (key.objectid == bytenr &&
3412 (key.type == BTRFS_METADATA_ITEM_KEY ||
3413 (key.type == BTRFS_EXTENT_ITEM_KEY &&
3414 key.offset == blocksize)))
3420 btrfs_release_path(path);
3426 ret = add_tree_block(rc, &key, path, blocks);
3428 btrfs_free_path(path);
3433 * helper to check if the block use full backrefs for pointers in it
3435 static int block_use_full_backref(struct reloc_control *rc,
3436 struct extent_buffer *eb)
3441 if (btrfs_header_flag(eb, BTRFS_HEADER_FLAG_RELOC) ||
3442 btrfs_header_backref_rev(eb) < BTRFS_MIXED_BACKREF_REV)
3445 ret = btrfs_lookup_extent_info(NULL, rc->extent_root,
3446 eb->start, btrfs_header_level(eb), 1,
3450 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
3457 static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
3458 struct btrfs_block_group_cache *block_group,
3459 struct inode *inode,
3462 struct btrfs_key key;
3463 struct btrfs_root *root = fs_info->tree_root;
3464 struct btrfs_trans_handle *trans;
3471 key.type = BTRFS_INODE_ITEM_KEY;
3474 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
3475 if (IS_ERR(inode) || is_bad_inode(inode)) {
3482 ret = btrfs_check_trunc_cache_free_space(root,
3483 &fs_info->global_block_rsv);
3487 trans = btrfs_join_transaction(root);
3488 if (IS_ERR(trans)) {
3489 ret = PTR_ERR(trans);
3493 ret = btrfs_truncate_free_space_cache(root, trans, block_group, inode);
3495 btrfs_end_transaction(trans, root);
3496 btrfs_btree_balance_dirty(root);
3503 * helper to add tree blocks for backref of type BTRFS_EXTENT_DATA_REF_KEY
3504 * this function scans fs tree to find blocks reference the data extent
3506 static int find_data_references(struct reloc_control *rc,
3507 struct btrfs_key *extent_key,
3508 struct extent_buffer *leaf,
3509 struct btrfs_extent_data_ref *ref,
3510 struct rb_root *blocks)
3512 struct btrfs_path *path;
3513 struct tree_block *block;
3514 struct btrfs_root *root;
3515 struct btrfs_file_extent_item *fi;
3516 struct rb_node *rb_node;
3517 struct btrfs_key key;
3528 ref_root = btrfs_extent_data_ref_root(leaf, ref);
3529 ref_objectid = btrfs_extent_data_ref_objectid(leaf, ref);
3530 ref_offset = btrfs_extent_data_ref_offset(leaf, ref);
3531 ref_count = btrfs_extent_data_ref_count(leaf, ref);
3534 * This is an extent belonging to the free space cache, lets just delete
3535 * it and redo the search.
3537 if (ref_root == BTRFS_ROOT_TREE_OBJECTID) {
3538 ret = delete_block_group_cache(rc->extent_root->fs_info,
3540 NULL, ref_objectid);
3546 path = btrfs_alloc_path();
3549 path->reada = READA_FORWARD;
3551 root = read_fs_root(rc->extent_root->fs_info, ref_root);
3553 err = PTR_ERR(root);
3557 key.objectid = ref_objectid;
3558 key.type = BTRFS_EXTENT_DATA_KEY;
3559 if (ref_offset > ((u64)-1 << 32))
3562 key.offset = ref_offset;
3564 path->search_commit_root = 1;
3565 path->skip_locking = 1;
3566 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3572 leaf = path->nodes[0];
3573 nritems = btrfs_header_nritems(leaf);
3575 * the references in tree blocks that use full backrefs
3576 * are not counted in
3578 if (block_use_full_backref(rc, leaf))
3582 rb_node = tree_search(blocks, leaf->start);
3587 path->slots[0] = nritems;
3590 while (ref_count > 0) {
3591 while (path->slots[0] >= nritems) {
3592 ret = btrfs_next_leaf(root, path);
3597 if (WARN_ON(ret > 0))
3600 leaf = path->nodes[0];
3601 nritems = btrfs_header_nritems(leaf);
3604 if (block_use_full_backref(rc, leaf))
3608 rb_node = tree_search(blocks, leaf->start);
3613 path->slots[0] = nritems;
3617 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3618 if (WARN_ON(key.objectid != ref_objectid ||
3619 key.type != BTRFS_EXTENT_DATA_KEY))
3622 fi = btrfs_item_ptr(leaf, path->slots[0],
3623 struct btrfs_file_extent_item);
3625 if (btrfs_file_extent_type(leaf, fi) ==
3626 BTRFS_FILE_EXTENT_INLINE)
3629 if (btrfs_file_extent_disk_bytenr(leaf, fi) !=
3630 extent_key->objectid)
3633 key.offset -= btrfs_file_extent_offset(leaf, fi);
3634 if (key.offset != ref_offset)
3642 if (!tree_block_processed(leaf->start, rc)) {
3643 block = kmalloc(sizeof(*block), GFP_NOFS);
3648 block->bytenr = leaf->start;
3649 btrfs_item_key_to_cpu(leaf, &block->key, 0);
3651 block->key_ready = 1;
3652 rb_node = tree_insert(blocks, block->bytenr,
3655 backref_tree_panic(rb_node, -EEXIST,
3661 path->slots[0] = nritems;
3667 btrfs_free_path(path);
3672 * helper to find all tree blocks that reference a given data extent
3674 static noinline_for_stack
3675 int add_data_references(struct reloc_control *rc,
3676 struct btrfs_key *extent_key,
3677 struct btrfs_path *path,
3678 struct rb_root *blocks)
3680 struct btrfs_key key;
3681 struct extent_buffer *eb;
3682 struct btrfs_extent_data_ref *dref;
3683 struct btrfs_extent_inline_ref *iref;
3686 u32 blocksize = rc->extent_root->nodesize;
3690 eb = path->nodes[0];
3691 ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
3692 end = ptr + btrfs_item_size_nr(eb, path->slots[0]);
3693 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3694 if (ptr + sizeof(struct btrfs_extent_item_v0) == end)
3698 ptr += sizeof(struct btrfs_extent_item);
3701 iref = (struct btrfs_extent_inline_ref *)ptr;
3702 key.type = btrfs_extent_inline_ref_type(eb, iref);
3703 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3704 key.offset = btrfs_extent_inline_ref_offset(eb, iref);
3705 ret = __add_tree_block(rc, key.offset, blocksize,
3707 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3708 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
3709 ret = find_data_references(rc, extent_key,
3718 ptr += btrfs_extent_inline_ref_size(key.type);
3724 eb = path->nodes[0];
3725 if (path->slots[0] >= btrfs_header_nritems(eb)) {
3726 ret = btrfs_next_leaf(rc->extent_root, path);
3733 eb = path->nodes[0];
3736 btrfs_item_key_to_cpu(eb, &key, path->slots[0]);
3737 if (key.objectid != extent_key->objectid)
3740 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3741 if (key.type == BTRFS_SHARED_DATA_REF_KEY ||
3742 key.type == BTRFS_EXTENT_REF_V0_KEY) {
3744 BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
3745 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3747 ret = __add_tree_block(rc, key.offset, blocksize,
3749 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3750 dref = btrfs_item_ptr(eb, path->slots[0],
3751 struct btrfs_extent_data_ref);
3752 ret = find_data_references(rc, extent_key,
3764 btrfs_release_path(path);
3766 free_block_list(blocks);
3771 * helper to find next unprocessed extent
3773 static noinline_for_stack
3774 int find_next_extent(struct reloc_control *rc, struct btrfs_path *path,
3775 struct btrfs_key *extent_key)
3777 struct btrfs_key key;
3778 struct extent_buffer *leaf;
3779 u64 start, end, last;
3782 last = rc->block_group->key.objectid + rc->block_group->key.offset;
3785 if (rc->search_start >= last) {
3790 key.objectid = rc->search_start;
3791 key.type = BTRFS_EXTENT_ITEM_KEY;
3794 path->search_commit_root = 1;
3795 path->skip_locking = 1;
3796 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path,
3801 leaf = path->nodes[0];
3802 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3803 ret = btrfs_next_leaf(rc->extent_root, path);
3806 leaf = path->nodes[0];
3809 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3810 if (key.objectid >= last) {
3815 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3816 key.type != BTRFS_METADATA_ITEM_KEY) {
3821 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3822 key.objectid + key.offset <= rc->search_start) {
3827 if (key.type == BTRFS_METADATA_ITEM_KEY &&
3828 key.objectid + rc->extent_root->nodesize <=
3834 ret = find_first_extent_bit(&rc->processed_blocks,
3835 key.objectid, &start, &end,
3836 EXTENT_DIRTY, NULL);
3838 if (ret == 0 && start <= key.objectid) {
3839 btrfs_release_path(path);
3840 rc->search_start = end + 1;
3842 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3843 rc->search_start = key.objectid + key.offset;
3845 rc->search_start = key.objectid +
3846 rc->extent_root->nodesize;
3847 memcpy(extent_key, &key, sizeof(key));
3851 btrfs_release_path(path);
3855 static void set_reloc_control(struct reloc_control *rc)
3857 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3859 mutex_lock(&fs_info->reloc_mutex);
3860 fs_info->reloc_ctl = rc;
3861 mutex_unlock(&fs_info->reloc_mutex);
3864 static void unset_reloc_control(struct reloc_control *rc)
3866 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3868 mutex_lock(&fs_info->reloc_mutex);
3869 fs_info->reloc_ctl = NULL;
3870 mutex_unlock(&fs_info->reloc_mutex);
3873 static int check_extent_flags(u64 flags)
3875 if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
3876 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
3878 if (!(flags & BTRFS_EXTENT_FLAG_DATA) &&
3879 !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
3881 if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
3882 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
3887 static noinline_for_stack
3888 int prepare_to_relocate(struct reloc_control *rc)
3890 struct btrfs_trans_handle *trans;
3893 rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root,
3894 BTRFS_BLOCK_RSV_TEMP);
3898 memset(&rc->cluster, 0, sizeof(rc->cluster));
3899 rc->search_start = rc->block_group->key.objectid;
3900 rc->extents_found = 0;
3901 rc->nodes_relocated = 0;
3902 rc->merging_rsv_size = 0;
3903 rc->reserved_bytes = 0;
3904 rc->block_rsv->size = rc->extent_root->nodesize *
3905 RELOCATION_RESERVED_NODES;
3906 ret = btrfs_block_rsv_refill(rc->extent_root,
3907 rc->block_rsv, rc->block_rsv->size,
3908 BTRFS_RESERVE_FLUSH_ALL);
3912 rc->create_reloc_tree = 1;
3913 set_reloc_control(rc);
3915 trans = btrfs_join_transaction(rc->extent_root);
3916 if (IS_ERR(trans)) {
3917 unset_reloc_control(rc);
3919 * extent tree is not a ref_cow tree and has no reloc_root to
3920 * cleanup. And callers are responsible to free the above
3923 return PTR_ERR(trans);
3925 btrfs_commit_transaction(trans, rc->extent_root);
3930 * Qgroup fixer for data chunk relocation.
3931 * The data relocation is done in the following steps
3932 * 1) Copy data extents into data reloc tree
3933 * 2) Create tree reloc tree(special snapshot) for related subvolumes
3934 * 3) Modify file extents in tree reloc tree
3935 * 4) Merge tree reloc tree with original fs tree, by swapping tree blocks
3937 * The problem is, data and tree reloc tree are not accounted to qgroup,
3938 * and 4) will only info qgroup to track tree blocks change, not file extents
3939 * in the tree blocks.
3941 * The good news is, related data extents are all in data reloc tree, so we
3942 * only need to info qgroup to track all file extents in data reloc tree
3943 * before commit trans.
3945 static int qgroup_fix_relocated_data_extents(struct btrfs_trans_handle *trans,
3946 struct reloc_control *rc)
3948 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3949 struct inode *inode = rc->data_inode;
3950 struct btrfs_root *data_reloc_root = BTRFS_I(inode)->root;
3951 struct btrfs_path *path;
3952 struct btrfs_key key;
3955 if (!fs_info->quota_enabled)
3959 * Only for stage where we update data pointers the qgroup fix is
3961 * For MOVING_DATA stage, we will miss the timing of swapping tree
3962 * blocks, and won't fix it.
3964 if (!(rc->stage == UPDATE_DATA_PTRS && rc->extents_found))
3967 path = btrfs_alloc_path();
3970 key.objectid = btrfs_ino(inode);
3971 key.type = BTRFS_EXTENT_DATA_KEY;
3974 ret = btrfs_search_slot(NULL, data_reloc_root, &key, path, 0, 0);
3978 lock_extent(&BTRFS_I(inode)->io_tree, 0, (u64)-1);
3980 struct btrfs_file_extent_item *fi;
3982 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3983 if (key.objectid > btrfs_ino(inode))
3985 if (key.type != BTRFS_EXTENT_DATA_KEY)
3987 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3988 struct btrfs_file_extent_item);
3989 if (btrfs_file_extent_type(path->nodes[0], fi) !=
3990 BTRFS_FILE_EXTENT_REG)
3992 ret = btrfs_qgroup_insert_dirty_extent(trans, fs_info,
3993 btrfs_file_extent_disk_bytenr(path->nodes[0], fi),
3994 btrfs_file_extent_disk_num_bytes(path->nodes[0], fi),
3999 ret = btrfs_next_item(data_reloc_root, path);
4007 unlock_extent(&BTRFS_I(inode)->io_tree, 0 , (u64)-1);
4009 btrfs_free_path(path);
4013 static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
4015 struct rb_root blocks = RB_ROOT;
4016 struct btrfs_key key;
4017 struct btrfs_trans_handle *trans = NULL;
4018 struct btrfs_path *path;
4019 struct btrfs_extent_item *ei;
4026 path = btrfs_alloc_path();
4029 path->reada = READA_FORWARD;
4031 ret = prepare_to_relocate(rc);
4038 rc->reserved_bytes = 0;
4039 ret = btrfs_block_rsv_refill(rc->extent_root,
4040 rc->block_rsv, rc->block_rsv->size,
4041 BTRFS_RESERVE_FLUSH_ALL);
4047 trans = btrfs_start_transaction(rc->extent_root, 0);
4048 if (IS_ERR(trans)) {
4049 err = PTR_ERR(trans);
4054 if (update_backref_cache(trans, &rc->backref_cache)) {
4055 btrfs_end_transaction(trans, rc->extent_root);
4059 ret = find_next_extent(rc, path, &key);
4065 rc->extents_found++;
4067 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
4068 struct btrfs_extent_item);
4069 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
4070 if (item_size >= sizeof(*ei)) {
4071 flags = btrfs_extent_flags(path->nodes[0], ei);
4072 ret = check_extent_flags(flags);
4076 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4078 int path_change = 0;
4081 sizeof(struct btrfs_extent_item_v0));
4082 ret = get_ref_objectid_v0(rc, path, &key, &ref_owner,
4088 if (ref_owner < BTRFS_FIRST_FREE_OBJECTID)
4089 flags = BTRFS_EXTENT_FLAG_TREE_BLOCK;
4091 flags = BTRFS_EXTENT_FLAG_DATA;
4094 btrfs_release_path(path);
4096 path->search_commit_root = 1;
4097 path->skip_locking = 1;
4098 ret = btrfs_search_slot(NULL, rc->extent_root,
4111 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
4112 ret = add_tree_block(rc, &key, path, &blocks);
4113 } else if (rc->stage == UPDATE_DATA_PTRS &&
4114 (flags & BTRFS_EXTENT_FLAG_DATA)) {
4115 ret = add_data_references(rc, &key, path, &blocks);
4117 btrfs_release_path(path);
4125 if (!RB_EMPTY_ROOT(&blocks)) {
4126 ret = relocate_tree_blocks(trans, rc, &blocks);
4129 * if we fail to relocate tree blocks, force to update
4130 * backref cache when committing transaction.
4132 rc->backref_cache.last_trans = trans->transid - 1;
4134 if (ret != -EAGAIN) {
4138 rc->extents_found--;
4139 rc->search_start = key.objectid;
4143 btrfs_end_transaction_throttle(trans, rc->extent_root);
4144 btrfs_btree_balance_dirty(rc->extent_root);
4147 if (rc->stage == MOVE_DATA_EXTENTS &&
4148 (flags & BTRFS_EXTENT_FLAG_DATA)) {
4149 rc->found_file_extent = 1;
4150 ret = relocate_data_extent(rc->data_inode,
4151 &key, &rc->cluster);
4158 if (trans && progress && err == -ENOSPC) {
4159 ret = btrfs_force_chunk_alloc(trans, rc->extent_root,
4160 rc->block_group->flags);
4168 btrfs_release_path(path);
4169 clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY);
4172 btrfs_end_transaction_throttle(trans, rc->extent_root);
4173 btrfs_btree_balance_dirty(rc->extent_root);
4177 ret = relocate_file_extent_cluster(rc->data_inode,
4183 rc->create_reloc_tree = 0;
4184 set_reloc_control(rc);
4186 backref_cache_cleanup(&rc->backref_cache);
4187 btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1);
4189 err = prepare_to_merge(rc, err);
4191 merge_reloc_roots(rc);
4193 rc->merge_reloc_tree = 0;
4194 unset_reloc_control(rc);
4195 btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1);
4197 /* get rid of pinned extents */
4198 trans = btrfs_join_transaction(rc->extent_root);
4199 if (IS_ERR(trans)) {
4200 err = PTR_ERR(trans);
4203 ret = qgroup_fix_relocated_data_extents(trans, rc);
4205 btrfs_abort_transaction(trans, ret);
4210 btrfs_commit_transaction(trans, rc->extent_root);
4212 btrfs_free_block_rsv(rc->extent_root, rc->block_rsv);
4213 btrfs_free_path(path);
4217 static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
4218 struct btrfs_root *root, u64 objectid)
4220 struct btrfs_path *path;
4221 struct btrfs_inode_item *item;
4222 struct extent_buffer *leaf;
4225 path = btrfs_alloc_path();
4229 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
4233 leaf = path->nodes[0];
4234 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
4235 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
4236 btrfs_set_inode_generation(leaf, item, 1);
4237 btrfs_set_inode_size(leaf, item, 0);
4238 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
4239 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS |
4240 BTRFS_INODE_PREALLOC);
4241 btrfs_mark_buffer_dirty(leaf);
4243 btrfs_free_path(path);
4248 * helper to create inode for data relocation.
4249 * the inode is in data relocation tree and its link count is 0
4251 static noinline_for_stack
4252 struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
4253 struct btrfs_block_group_cache *group)
4255 struct inode *inode = NULL;
4256 struct btrfs_trans_handle *trans;
4257 struct btrfs_root *root;
4258 struct btrfs_key key;
4262 root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID);
4264 return ERR_CAST(root);
4266 trans = btrfs_start_transaction(root, 6);
4268 return ERR_CAST(trans);
4270 err = btrfs_find_free_objectid(root, &objectid);
4274 err = __insert_orphan_inode(trans, root, objectid);
4277 key.objectid = objectid;
4278 key.type = BTRFS_INODE_ITEM_KEY;
4280 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
4281 BUG_ON(IS_ERR(inode) || is_bad_inode(inode));
4282 BTRFS_I(inode)->index_cnt = group->key.objectid;
4284 err = btrfs_orphan_add(trans, inode);
4286 btrfs_end_transaction(trans, root);
4287 btrfs_btree_balance_dirty(root);
4291 inode = ERR_PTR(err);
4296 static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info)
4298 struct reloc_control *rc;
4300 rc = kzalloc(sizeof(*rc), GFP_NOFS);
4304 INIT_LIST_HEAD(&rc->reloc_roots);
4305 backref_cache_init(&rc->backref_cache);
4306 mapping_tree_init(&rc->reloc_root_tree);
4307 extent_io_tree_init(&rc->processed_blocks,
4308 fs_info->btree_inode->i_mapping);
4313 * function to relocate all extents in a block group.
4315 int btrfs_relocate_block_group(struct btrfs_root *extent_root, u64 group_start)
4317 struct btrfs_fs_info *fs_info = extent_root->fs_info;
4318 struct reloc_control *rc;
4319 struct inode *inode;
4320 struct btrfs_path *path;
4325 rc = alloc_reloc_control(fs_info);
4329 rc->extent_root = extent_root;
4331 rc->block_group = btrfs_lookup_block_group(fs_info, group_start);
4332 BUG_ON(!rc->block_group);
4334 ret = btrfs_inc_block_group_ro(extent_root, rc->block_group);
4341 path = btrfs_alloc_path();
4347 inode = lookup_free_space_inode(fs_info->tree_root, rc->block_group,
4349 btrfs_free_path(path);
4352 ret = delete_block_group_cache(fs_info, rc->block_group, inode, 0);
4354 ret = PTR_ERR(inode);
4356 if (ret && ret != -ENOENT) {
4361 rc->data_inode = create_reloc_inode(fs_info, rc->block_group);
4362 if (IS_ERR(rc->data_inode)) {
4363 err = PTR_ERR(rc->data_inode);
4364 rc->data_inode = NULL;
4368 btrfs_info(extent_root->fs_info, "relocating block group %llu flags %llu",
4369 rc->block_group->key.objectid, rc->block_group->flags);
4371 btrfs_wait_block_group_reservations(rc->block_group);
4372 btrfs_wait_nocow_writers(rc->block_group);
4373 btrfs_wait_ordered_roots(fs_info, -1,
4374 rc->block_group->key.objectid,
4375 rc->block_group->key.offset);
4378 mutex_lock(&fs_info->cleaner_mutex);
4379 ret = relocate_block_group(rc);
4380 mutex_unlock(&fs_info->cleaner_mutex);
4386 if (rc->extents_found == 0)
4389 btrfs_info(extent_root->fs_info, "found %llu extents",
4392 if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) {
4393 ret = btrfs_wait_ordered_range(rc->data_inode, 0,
4399 invalidate_mapping_pages(rc->data_inode->i_mapping,
4401 rc->stage = UPDATE_DATA_PTRS;
4405 WARN_ON(rc->block_group->pinned > 0);
4406 WARN_ON(rc->block_group->reserved > 0);
4407 WARN_ON(btrfs_block_group_used(&rc->block_group->item) > 0);
4410 btrfs_dec_block_group_ro(extent_root, rc->block_group);
4411 iput(rc->data_inode);
4412 btrfs_put_block_group(rc->block_group);
4417 static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
4419 struct btrfs_trans_handle *trans;
4422 trans = btrfs_start_transaction(root->fs_info->tree_root, 0);
4424 return PTR_ERR(trans);
4426 memset(&root->root_item.drop_progress, 0,
4427 sizeof(root->root_item.drop_progress));
4428 root->root_item.drop_level = 0;
4429 btrfs_set_root_refs(&root->root_item, 0);
4430 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4431 &root->root_key, &root->root_item);
4433 err = btrfs_end_transaction(trans, root->fs_info->tree_root);
4440 * recover relocation interrupted by system crash.
4442 * this function resumes merging reloc trees with corresponding fs trees.
4443 * this is important for keeping the sharing of tree blocks
4445 int btrfs_recover_relocation(struct btrfs_root *root)
4447 LIST_HEAD(reloc_roots);
4448 struct btrfs_key key;
4449 struct btrfs_root *fs_root;
4450 struct btrfs_root *reloc_root;
4451 struct btrfs_path *path;
4452 struct extent_buffer *leaf;
4453 struct reloc_control *rc = NULL;
4454 struct btrfs_trans_handle *trans;
4458 path = btrfs_alloc_path();
4461 path->reada = READA_BACK;
4463 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4464 key.type = BTRFS_ROOT_ITEM_KEY;
4465 key.offset = (u64)-1;
4468 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key,
4475 if (path->slots[0] == 0)
4479 leaf = path->nodes[0];
4480 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4481 btrfs_release_path(path);
4483 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID ||
4484 key.type != BTRFS_ROOT_ITEM_KEY)
4487 reloc_root = btrfs_read_fs_root(root, &key);
4488 if (IS_ERR(reloc_root)) {
4489 err = PTR_ERR(reloc_root);
4493 list_add(&reloc_root->root_list, &reloc_roots);
4495 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
4496 fs_root = read_fs_root(root->fs_info,
4497 reloc_root->root_key.offset);
4498 if (IS_ERR(fs_root)) {
4499 ret = PTR_ERR(fs_root);
4500 if (ret != -ENOENT) {
4504 ret = mark_garbage_root(reloc_root);
4512 if (key.offset == 0)
4517 btrfs_release_path(path);
4519 if (list_empty(&reloc_roots))
4522 rc = alloc_reloc_control(root->fs_info);
4528 rc->extent_root = root->fs_info->extent_root;
4530 set_reloc_control(rc);
4532 trans = btrfs_join_transaction(rc->extent_root);
4533 if (IS_ERR(trans)) {
4534 unset_reloc_control(rc);
4535 err = PTR_ERR(trans);
4539 rc->merge_reloc_tree = 1;
4541 while (!list_empty(&reloc_roots)) {
4542 reloc_root = list_entry(reloc_roots.next,
4543 struct btrfs_root, root_list);
4544 list_del(&reloc_root->root_list);
4546 if (btrfs_root_refs(&reloc_root->root_item) == 0) {
4547 list_add_tail(&reloc_root->root_list,
4552 fs_root = read_fs_root(root->fs_info,
4553 reloc_root->root_key.offset);
4554 if (IS_ERR(fs_root)) {
4555 err = PTR_ERR(fs_root);
4559 err = __add_reloc_root(reloc_root);
4560 BUG_ON(err < 0); /* -ENOMEM or logic error */
4561 fs_root->reloc_root = reloc_root;
4564 err = btrfs_commit_transaction(trans, rc->extent_root);
4568 merge_reloc_roots(rc);
4570 unset_reloc_control(rc);
4572 trans = btrfs_join_transaction(rc->extent_root);
4573 if (IS_ERR(trans)) {
4574 err = PTR_ERR(trans);
4577 err = qgroup_fix_relocated_data_extents(trans, rc);
4579 btrfs_abort_transaction(trans, err);
4582 err = btrfs_commit_transaction(trans, rc->extent_root);
4586 if (!list_empty(&reloc_roots))
4587 free_reloc_roots(&reloc_roots);
4589 btrfs_free_path(path);
4592 /* cleanup orphan inode in data relocation tree */
4593 fs_root = read_fs_root(root->fs_info,
4594 BTRFS_DATA_RELOC_TREE_OBJECTID);
4595 if (IS_ERR(fs_root))
4596 err = PTR_ERR(fs_root);
4598 err = btrfs_orphan_cleanup(fs_root);
4604 * helper to add ordered checksum for data relocation.
4606 * cloning checksum properly handles the nodatasum extents.
4607 * it also saves CPU time to re-calculate the checksum.
4609 int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
4611 struct btrfs_ordered_sum *sums;
4612 struct btrfs_ordered_extent *ordered;
4613 struct btrfs_root *root = BTRFS_I(inode)->root;
4619 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
4620 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
4622 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
4623 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
4624 disk_bytenr + len - 1, &list, 0);
4628 while (!list_empty(&list)) {
4629 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
4630 list_del_init(&sums->list);
4633 * We need to offset the new_bytenr based on where the csum is.
4634 * We need to do this because we will read in entire prealloc
4635 * extents but we may have written to say the middle of the
4636 * prealloc extent, so we need to make sure the csum goes with
4637 * the right disk offset.
4639 * We can do this because the data reloc inode refers strictly
4640 * to the on disk bytes, so we don't have to worry about
4641 * disk_len vs real len like with real inodes since it's all
4644 new_bytenr = ordered->start + (sums->bytenr - disk_bytenr);
4645 sums->bytenr = new_bytenr;
4647 btrfs_add_ordered_sum(inode, ordered, sums);
4650 btrfs_put_ordered_extent(ordered);
4654 int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
4655 struct btrfs_root *root, struct extent_buffer *buf,
4656 struct extent_buffer *cow)
4658 struct reloc_control *rc;
4659 struct backref_node *node;
4664 rc = root->fs_info->reloc_ctl;
4668 BUG_ON(rc->stage == UPDATE_DATA_PTRS &&
4669 root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID);
4671 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
4672 if (buf == root->node)
4673 __update_reloc_root(root, cow->start);
4676 level = btrfs_header_level(buf);
4677 if (btrfs_header_generation(buf) <=
4678 btrfs_root_last_snapshot(&root->root_item))
4681 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID &&
4682 rc->create_reloc_tree) {
4683 WARN_ON(!first_cow && level == 0);
4685 node = rc->backref_cache.path[level];
4686 BUG_ON(node->bytenr != buf->start &&
4687 node->new_bytenr != buf->start);
4689 drop_node_buffer(node);
4690 extent_buffer_get(cow);
4692 node->new_bytenr = cow->start;
4694 if (!node->pending) {
4695 list_move_tail(&node->list,
4696 &rc->backref_cache.pending[level]);
4701 __mark_block_processed(rc, node);
4703 if (first_cow && level > 0)
4704 rc->nodes_relocated += buf->len;
4707 if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS)
4708 ret = replace_file_extents(trans, rc, root, cow);
4713 * called before creating snapshot. it calculates metadata reservation
4714 * required for relocating tree blocks in the snapshot
4716 void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending,
4717 u64 *bytes_to_reserve)
4719 struct btrfs_root *root;
4720 struct reloc_control *rc;
4722 root = pending->root;
4723 if (!root->reloc_root)
4726 rc = root->fs_info->reloc_ctl;
4727 if (!rc->merge_reloc_tree)
4730 root = root->reloc_root;
4731 BUG_ON(btrfs_root_refs(&root->root_item) == 0);
4733 * relocation is in the stage of merging trees. the space
4734 * used by merging a reloc tree is twice the size of
4735 * relocated tree nodes in the worst case. half for cowing
4736 * the reloc tree, half for cowing the fs tree. the space
4737 * used by cowing the reloc tree will be freed after the
4738 * tree is dropped. if we create snapshot, cowing the fs
4739 * tree may use more space than it frees. so we need
4740 * reserve extra space.
4742 *bytes_to_reserve += rc->nodes_relocated;
4746 * called after snapshot is created. migrate block reservation
4747 * and create reloc root for the newly created snapshot
4749 int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
4750 struct btrfs_pending_snapshot *pending)
4752 struct btrfs_root *root = pending->root;
4753 struct btrfs_root *reloc_root;
4754 struct btrfs_root *new_root;
4755 struct reloc_control *rc;
4758 if (!root->reloc_root)
4761 rc = root->fs_info->reloc_ctl;
4762 rc->merging_rsv_size += rc->nodes_relocated;
4764 if (rc->merge_reloc_tree) {
4765 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
4767 rc->nodes_relocated, 1);
4772 new_root = pending->snap;
4773 reloc_root = create_reloc_root(trans, root->reloc_root,
4774 new_root->root_key.objectid);
4775 if (IS_ERR(reloc_root))
4776 return PTR_ERR(reloc_root);
4778 ret = __add_reloc_root(reloc_root);
4780 new_root->reloc_root = reloc_root;
4782 if (rc->create_reloc_tree)
4783 ret = clone_backref_node(trans, rc, root, reloc_root);