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"
36 * backref_node, mapping_node and tree_block start with this
39 struct rb_node rb_node;
44 * present a tree block in the backref cache
47 struct rb_node rb_node;
51 /* objectid of tree block owner, can be not uptodate */
53 /* link to pending, changed or detached list */
54 struct list_head list;
55 /* list of upper level blocks reference this block */
56 struct list_head upper;
57 /* list of child blocks in the cache */
58 struct list_head lower;
59 /* NULL if this node is not tree root */
60 struct btrfs_root *root;
61 /* extent buffer got by COW the block */
62 struct extent_buffer *eb;
63 /* level of tree block */
65 /* is the block in non-reference counted tree */
66 unsigned int cowonly:1;
67 /* 1 if no child node in the cache */
68 unsigned int lowest:1;
69 /* is the extent buffer locked */
70 unsigned int locked:1;
71 /* has the block been processed */
72 unsigned int processed:1;
73 /* have backrefs of this block been checked */
74 unsigned int checked:1;
76 * 1 if corresponding block has been cowed but some upper
77 * level block pointers may not point to the new location
79 unsigned int pending:1;
81 * 1 if the backref node isn't connected to any other
84 unsigned int detached:1;
88 * present a block pointer in the backref cache
91 struct list_head list[2];
92 struct backref_node *node[2];
98 struct backref_cache {
99 /* red black tree of all backref nodes in the cache */
100 struct rb_root rb_root;
101 /* for passing backref nodes to btrfs_reloc_cow_block */
102 struct backref_node *path[BTRFS_MAX_LEVEL];
104 * list of blocks that have been cowed but some block
105 * pointers in upper level blocks may not reflect the
108 struct list_head pending[BTRFS_MAX_LEVEL];
109 /* list of backref nodes with no child node */
110 struct list_head leaves;
111 /* list of blocks that have been cowed in current transaction */
112 struct list_head changed;
113 /* list of detached backref node. */
114 struct list_head detached;
123 * map address of tree root to tree
125 struct mapping_node {
126 struct rb_node rb_node;
131 struct mapping_tree {
132 struct rb_root rb_root;
137 * present a tree block to process
140 struct rb_node rb_node;
142 struct btrfs_key key;
143 unsigned int level:8;
144 unsigned int key_ready:1;
147 #define MAX_EXTENTS 128
149 struct file_extent_cluster {
152 u64 boundary[MAX_EXTENTS];
156 struct reloc_control {
157 /* block group to relocate */
158 struct btrfs_block_group_cache *block_group;
160 struct btrfs_root *extent_root;
161 /* inode for moving data */
162 struct inode *data_inode;
164 struct btrfs_block_rsv *block_rsv;
166 struct backref_cache backref_cache;
168 struct file_extent_cluster cluster;
169 /* tree blocks have been processed */
170 struct extent_io_tree processed_blocks;
171 /* map start of tree root to corresponding reloc tree */
172 struct mapping_tree reloc_root_tree;
173 /* list of reloc trees */
174 struct list_head reloc_roots;
175 /* size of metadata reservation for merging reloc trees */
176 u64 merging_rsv_size;
177 /* size of relocated tree nodes */
183 unsigned int stage:8;
184 unsigned int create_reloc_tree:1;
185 unsigned int merge_reloc_tree:1;
186 unsigned int found_file_extent:1;
187 unsigned int commit_transaction:1;
190 /* stages of data relocation */
191 #define MOVE_DATA_EXTENTS 0
192 #define UPDATE_DATA_PTRS 1
194 static void remove_backref_node(struct backref_cache *cache,
195 struct backref_node *node);
196 static void __mark_block_processed(struct reloc_control *rc,
197 struct backref_node *node);
199 static void mapping_tree_init(struct mapping_tree *tree)
201 tree->rb_root = RB_ROOT;
202 spin_lock_init(&tree->lock);
205 static void backref_cache_init(struct backref_cache *cache)
208 cache->rb_root = RB_ROOT;
209 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
210 INIT_LIST_HEAD(&cache->pending[i]);
211 INIT_LIST_HEAD(&cache->changed);
212 INIT_LIST_HEAD(&cache->detached);
213 INIT_LIST_HEAD(&cache->leaves);
216 static void backref_cache_cleanup(struct backref_cache *cache)
218 struct backref_node *node;
221 while (!list_empty(&cache->detached)) {
222 node = list_entry(cache->detached.next,
223 struct backref_node, list);
224 remove_backref_node(cache, node);
227 while (!list_empty(&cache->leaves)) {
228 node = list_entry(cache->leaves.next,
229 struct backref_node, lower);
230 remove_backref_node(cache, node);
233 cache->last_trans = 0;
235 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
236 BUG_ON(!list_empty(&cache->pending[i]));
237 BUG_ON(!list_empty(&cache->changed));
238 BUG_ON(!list_empty(&cache->detached));
239 BUG_ON(!RB_EMPTY_ROOT(&cache->rb_root));
240 BUG_ON(cache->nr_nodes);
241 BUG_ON(cache->nr_edges);
244 static struct backref_node *alloc_backref_node(struct backref_cache *cache)
246 struct backref_node *node;
248 node = kzalloc(sizeof(*node), GFP_NOFS);
250 INIT_LIST_HEAD(&node->list);
251 INIT_LIST_HEAD(&node->upper);
252 INIT_LIST_HEAD(&node->lower);
253 RB_CLEAR_NODE(&node->rb_node);
259 static void free_backref_node(struct backref_cache *cache,
260 struct backref_node *node)
268 static struct backref_edge *alloc_backref_edge(struct backref_cache *cache)
270 struct backref_edge *edge;
272 edge = kzalloc(sizeof(*edge), GFP_NOFS);
278 static void free_backref_edge(struct backref_cache *cache,
279 struct backref_edge *edge)
287 static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr,
288 struct rb_node *node)
290 struct rb_node **p = &root->rb_node;
291 struct rb_node *parent = NULL;
292 struct tree_entry *entry;
296 entry = rb_entry(parent, struct tree_entry, rb_node);
298 if (bytenr < entry->bytenr)
300 else if (bytenr > entry->bytenr)
306 rb_link_node(node, parent, p);
307 rb_insert_color(node, root);
311 static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
313 struct rb_node *n = root->rb_node;
314 struct tree_entry *entry;
317 entry = rb_entry(n, struct tree_entry, rb_node);
319 if (bytenr < entry->bytenr)
321 else if (bytenr > entry->bytenr)
329 static void backref_tree_panic(struct rb_node *rb_node, int errno, u64 bytenr)
332 struct btrfs_fs_info *fs_info = NULL;
333 struct backref_node *bnode = rb_entry(rb_node, struct backref_node,
336 fs_info = bnode->root->fs_info;
337 btrfs_panic(fs_info, errno, "Inconsistency in backref cache "
338 "found at offset %llu\n", bytenr);
342 * walk up backref nodes until reach node presents tree root
344 static struct backref_node *walk_up_backref(struct backref_node *node,
345 struct backref_edge *edges[],
348 struct backref_edge *edge;
351 while (!list_empty(&node->upper)) {
352 edge = list_entry(node->upper.next,
353 struct backref_edge, list[LOWER]);
355 node = edge->node[UPPER];
357 BUG_ON(node->detached);
363 * walk down backref nodes to find start of next reference path
365 static struct backref_node *walk_down_backref(struct backref_edge *edges[],
368 struct backref_edge *edge;
369 struct backref_node *lower;
373 edge = edges[idx - 1];
374 lower = edge->node[LOWER];
375 if (list_is_last(&edge->list[LOWER], &lower->upper)) {
379 edge = list_entry(edge->list[LOWER].next,
380 struct backref_edge, list[LOWER]);
381 edges[idx - 1] = edge;
383 return edge->node[UPPER];
389 static void unlock_node_buffer(struct backref_node *node)
392 btrfs_tree_unlock(node->eb);
397 static void drop_node_buffer(struct backref_node *node)
400 unlock_node_buffer(node);
401 free_extent_buffer(node->eb);
406 static void drop_backref_node(struct backref_cache *tree,
407 struct backref_node *node)
409 BUG_ON(!list_empty(&node->upper));
411 drop_node_buffer(node);
412 list_del(&node->list);
413 list_del(&node->lower);
414 if (!RB_EMPTY_NODE(&node->rb_node))
415 rb_erase(&node->rb_node, &tree->rb_root);
416 free_backref_node(tree, node);
420 * remove a backref node from the backref cache
422 static void remove_backref_node(struct backref_cache *cache,
423 struct backref_node *node)
425 struct backref_node *upper;
426 struct backref_edge *edge;
431 BUG_ON(!node->lowest && !node->detached);
432 while (!list_empty(&node->upper)) {
433 edge = list_entry(node->upper.next, struct backref_edge,
435 upper = edge->node[UPPER];
436 list_del(&edge->list[LOWER]);
437 list_del(&edge->list[UPPER]);
438 free_backref_edge(cache, edge);
440 if (RB_EMPTY_NODE(&upper->rb_node)) {
441 BUG_ON(!list_empty(&node->upper));
442 drop_backref_node(cache, node);
448 * add the node to leaf node list if no other
449 * child block cached.
451 if (list_empty(&upper->lower)) {
452 list_add_tail(&upper->lower, &cache->leaves);
457 drop_backref_node(cache, node);
460 static void update_backref_node(struct backref_cache *cache,
461 struct backref_node *node, u64 bytenr)
463 struct rb_node *rb_node;
464 rb_erase(&node->rb_node, &cache->rb_root);
465 node->bytenr = bytenr;
466 rb_node = tree_insert(&cache->rb_root, node->bytenr, &node->rb_node);
468 backref_tree_panic(rb_node, -EEXIST, bytenr);
472 * update backref cache after a transaction commit
474 static int update_backref_cache(struct btrfs_trans_handle *trans,
475 struct backref_cache *cache)
477 struct backref_node *node;
480 if (cache->last_trans == 0) {
481 cache->last_trans = trans->transid;
485 if (cache->last_trans == trans->transid)
489 * detached nodes are used to avoid unnecessary backref
490 * lookup. transaction commit changes the extent tree.
491 * so the detached nodes are no longer useful.
493 while (!list_empty(&cache->detached)) {
494 node = list_entry(cache->detached.next,
495 struct backref_node, list);
496 remove_backref_node(cache, node);
499 while (!list_empty(&cache->changed)) {
500 node = list_entry(cache->changed.next,
501 struct backref_node, list);
502 list_del_init(&node->list);
503 BUG_ON(node->pending);
504 update_backref_node(cache, node, node->new_bytenr);
508 * some nodes can be left in the pending list if there were
509 * errors during processing the pending nodes.
511 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
512 list_for_each_entry(node, &cache->pending[level], list) {
513 BUG_ON(!node->pending);
514 if (node->bytenr == node->new_bytenr)
516 update_backref_node(cache, node, node->new_bytenr);
520 cache->last_trans = 0;
525 static int should_ignore_root(struct btrfs_root *root)
527 struct btrfs_root *reloc_root;
532 reloc_root = root->reloc_root;
536 if (btrfs_root_last_snapshot(&reloc_root->root_item) ==
537 root->fs_info->running_transaction->transid - 1)
540 * if there is reloc tree and it was created in previous
541 * transaction backref lookup can find the reloc tree,
542 * so backref node for the fs tree root is useless for
548 * find reloc tree by address of tree root
550 static struct btrfs_root *find_reloc_root(struct reloc_control *rc,
553 struct rb_node *rb_node;
554 struct mapping_node *node;
555 struct btrfs_root *root = NULL;
557 spin_lock(&rc->reloc_root_tree.lock);
558 rb_node = tree_search(&rc->reloc_root_tree.rb_root, bytenr);
560 node = rb_entry(rb_node, struct mapping_node, rb_node);
561 root = (struct btrfs_root *)node->data;
563 spin_unlock(&rc->reloc_root_tree.lock);
567 static int is_cowonly_root(u64 root_objectid)
569 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
570 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
571 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
572 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
573 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
574 root_objectid == BTRFS_CSUM_TREE_OBJECTID ||
575 root_objectid == BTRFS_UUID_TREE_OBJECTID ||
576 root_objectid == BTRFS_QUOTA_TREE_OBJECTID)
581 static struct btrfs_root *read_fs_root(struct btrfs_fs_info *fs_info,
584 struct btrfs_key key;
586 key.objectid = root_objectid;
587 key.type = BTRFS_ROOT_ITEM_KEY;
588 if (is_cowonly_root(root_objectid))
591 key.offset = (u64)-1;
593 return btrfs_get_fs_root(fs_info, &key, false);
596 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
597 static noinline_for_stack
598 struct btrfs_root *find_tree_root(struct reloc_control *rc,
599 struct extent_buffer *leaf,
600 struct btrfs_extent_ref_v0 *ref0)
602 struct btrfs_root *root;
603 u64 root_objectid = btrfs_ref_root_v0(leaf, ref0);
604 u64 generation = btrfs_ref_generation_v0(leaf, ref0);
606 BUG_ON(root_objectid == BTRFS_TREE_RELOC_OBJECTID);
608 root = read_fs_root(rc->extent_root->fs_info, root_objectid);
609 BUG_ON(IS_ERR(root));
611 if (root->ref_cows &&
612 generation != btrfs_root_generation(&root->root_item))
619 static noinline_for_stack
620 int find_inline_backref(struct extent_buffer *leaf, int slot,
621 unsigned long *ptr, unsigned long *end)
623 struct btrfs_key key;
624 struct btrfs_extent_item *ei;
625 struct btrfs_tree_block_info *bi;
628 btrfs_item_key_to_cpu(leaf, &key, slot);
630 item_size = btrfs_item_size_nr(leaf, slot);
631 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
632 if (item_size < sizeof(*ei)) {
633 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
637 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
638 WARN_ON(!(btrfs_extent_flags(leaf, ei) &
639 BTRFS_EXTENT_FLAG_TREE_BLOCK));
641 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
642 item_size <= sizeof(*ei) + sizeof(*bi)) {
643 WARN_ON(item_size < sizeof(*ei) + sizeof(*bi));
646 if (key.type == BTRFS_METADATA_ITEM_KEY &&
647 item_size <= sizeof(*ei)) {
648 WARN_ON(item_size < sizeof(*ei));
652 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
653 bi = (struct btrfs_tree_block_info *)(ei + 1);
654 *ptr = (unsigned long)(bi + 1);
656 *ptr = (unsigned long)(ei + 1);
658 *end = (unsigned long)ei + item_size;
663 * build backref tree for a given tree block. root of the backref tree
664 * corresponds the tree block, leaves of the backref tree correspond
665 * roots of b-trees that reference the tree block.
667 * the basic idea of this function is check backrefs of a given block
668 * to find upper level blocks that refernece the block, and then check
669 * bakcrefs of these upper level blocks recursively. the recursion stop
670 * when tree root is reached or backrefs for the block is cached.
672 * NOTE: if we find backrefs for a block are cached, we know backrefs
673 * for all upper level blocks that directly/indirectly reference the
674 * block are also cached.
676 static noinline_for_stack
677 struct backref_node *build_backref_tree(struct reloc_control *rc,
678 struct btrfs_key *node_key,
679 int level, u64 bytenr)
681 struct backref_cache *cache = &rc->backref_cache;
682 struct btrfs_path *path1;
683 struct btrfs_path *path2;
684 struct extent_buffer *eb;
685 struct btrfs_root *root;
686 struct backref_node *cur;
687 struct backref_node *upper;
688 struct backref_node *lower;
689 struct backref_node *node = NULL;
690 struct backref_node *exist = NULL;
691 struct backref_edge *edge;
692 struct rb_node *rb_node;
693 struct btrfs_key key;
701 bool need_check = true;
703 path1 = btrfs_alloc_path();
704 path2 = btrfs_alloc_path();
705 if (!path1 || !path2) {
712 node = alloc_backref_node(cache);
718 node->bytenr = bytenr;
725 key.objectid = cur->bytenr;
726 key.type = BTRFS_METADATA_ITEM_KEY;
727 key.offset = (u64)-1;
729 path1->search_commit_root = 1;
730 path1->skip_locking = 1;
731 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path1,
737 BUG_ON(!ret || !path1->slots[0]);
741 WARN_ON(cur->checked);
742 if (!list_empty(&cur->upper)) {
744 * the backref was added previously when processing
745 * backref of type BTRFS_TREE_BLOCK_REF_KEY
747 BUG_ON(!list_is_singular(&cur->upper));
748 edge = list_entry(cur->upper.next, struct backref_edge,
750 BUG_ON(!list_empty(&edge->list[UPPER]));
751 exist = edge->node[UPPER];
753 * add the upper level block to pending list if we need
757 list_add_tail(&edge->list[UPPER], &list);
764 eb = path1->nodes[0];
767 if (path1->slots[0] >= btrfs_header_nritems(eb)) {
768 ret = btrfs_next_leaf(rc->extent_root, path1);
775 eb = path1->nodes[0];
778 btrfs_item_key_to_cpu(eb, &key, path1->slots[0]);
779 if (key.objectid != cur->bytenr) {
784 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
785 key.type == BTRFS_METADATA_ITEM_KEY) {
786 ret = find_inline_backref(eb, path1->slots[0],
794 /* update key for inline back ref */
795 struct btrfs_extent_inline_ref *iref;
796 iref = (struct btrfs_extent_inline_ref *)ptr;
797 key.type = btrfs_extent_inline_ref_type(eb, iref);
798 key.offset = btrfs_extent_inline_ref_offset(eb, iref);
799 WARN_ON(key.type != BTRFS_TREE_BLOCK_REF_KEY &&
800 key.type != BTRFS_SHARED_BLOCK_REF_KEY);
804 ((key.type == BTRFS_TREE_BLOCK_REF_KEY &&
805 exist->owner == key.offset) ||
806 (key.type == BTRFS_SHARED_BLOCK_REF_KEY &&
807 exist->bytenr == key.offset))) {
812 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
813 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY ||
814 key.type == BTRFS_EXTENT_REF_V0_KEY) {
815 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
816 struct btrfs_extent_ref_v0 *ref0;
817 ref0 = btrfs_item_ptr(eb, path1->slots[0],
818 struct btrfs_extent_ref_v0);
819 if (key.objectid == key.offset) {
820 root = find_tree_root(rc, eb, ref0);
821 if (root && !should_ignore_root(root))
824 list_add(&cur->list, &useless);
827 if (is_cowonly_root(btrfs_ref_root_v0(eb,
832 BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
833 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
835 if (key.objectid == key.offset) {
837 * only root blocks of reloc trees use
838 * backref of this type.
840 root = find_reloc_root(rc, cur->bytenr);
846 edge = alloc_backref_edge(cache);
851 rb_node = tree_search(&cache->rb_root, key.offset);
853 upper = alloc_backref_node(cache);
855 free_backref_edge(cache, edge);
859 upper->bytenr = key.offset;
860 upper->level = cur->level + 1;
862 * backrefs for the upper level block isn't
863 * cached, add the block to pending list
865 list_add_tail(&edge->list[UPPER], &list);
867 upper = rb_entry(rb_node, struct backref_node,
869 BUG_ON(!upper->checked);
870 INIT_LIST_HEAD(&edge->list[UPPER]);
872 list_add_tail(&edge->list[LOWER], &cur->upper);
873 edge->node[LOWER] = cur;
874 edge->node[UPPER] = upper;
877 } else if (key.type != BTRFS_TREE_BLOCK_REF_KEY) {
881 /* key.type == BTRFS_TREE_BLOCK_REF_KEY */
882 root = read_fs_root(rc->extent_root->fs_info, key.offset);
891 if (btrfs_root_level(&root->root_item) == cur->level) {
893 BUG_ON(btrfs_root_bytenr(&root->root_item) !=
895 if (should_ignore_root(root))
896 list_add(&cur->list, &useless);
902 level = cur->level + 1;
905 * searching the tree to find upper level blocks
906 * reference the block.
908 path2->search_commit_root = 1;
909 path2->skip_locking = 1;
910 path2->lowest_level = level;
911 ret = btrfs_search_slot(NULL, root, node_key, path2, 0, 0);
912 path2->lowest_level = 0;
917 if (ret > 0 && path2->slots[level] > 0)
918 path2->slots[level]--;
920 eb = path2->nodes[level];
921 WARN_ON(btrfs_node_blockptr(eb, path2->slots[level]) !=
926 for (; level < BTRFS_MAX_LEVEL; level++) {
927 if (!path2->nodes[level]) {
928 BUG_ON(btrfs_root_bytenr(&root->root_item) !=
930 if (should_ignore_root(root))
931 list_add(&lower->list, &useless);
937 edge = alloc_backref_edge(cache);
943 eb = path2->nodes[level];
944 rb_node = tree_search(&cache->rb_root, eb->start);
946 upper = alloc_backref_node(cache);
948 free_backref_edge(cache, edge);
952 upper->bytenr = eb->start;
953 upper->owner = btrfs_header_owner(eb);
954 upper->level = lower->level + 1;
959 * if we know the block isn't shared
960 * we can void checking its backrefs.
962 if (btrfs_block_can_be_shared(root, eb))
968 * add the block to pending list if we
969 * need check its backrefs, we only do this once
970 * while walking up a tree as we will catch
971 * anything else later on.
973 if (!upper->checked && need_check) {
975 list_add_tail(&edge->list[UPPER],
978 INIT_LIST_HEAD(&edge->list[UPPER]);
980 upper = rb_entry(rb_node, struct backref_node,
982 BUG_ON(!upper->checked);
983 INIT_LIST_HEAD(&edge->list[UPPER]);
985 upper->owner = btrfs_header_owner(eb);
987 list_add_tail(&edge->list[LOWER], &lower->upper);
988 edge->node[LOWER] = lower;
989 edge->node[UPPER] = upper;
996 btrfs_release_path(path2);
999 ptr += btrfs_extent_inline_ref_size(key.type);
1009 btrfs_release_path(path1);
1014 /* the pending list isn't empty, take the first block to process */
1015 if (!list_empty(&list)) {
1016 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
1017 list_del_init(&edge->list[UPPER]);
1018 cur = edge->node[UPPER];
1023 * everything goes well, connect backref nodes and insert backref nodes
1026 BUG_ON(!node->checked);
1027 cowonly = node->cowonly;
1029 rb_node = tree_insert(&cache->rb_root, node->bytenr,
1032 backref_tree_panic(rb_node, -EEXIST, node->bytenr);
1033 list_add_tail(&node->lower, &cache->leaves);
1036 list_for_each_entry(edge, &node->upper, list[LOWER])
1037 list_add_tail(&edge->list[UPPER], &list);
1039 while (!list_empty(&list)) {
1040 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
1041 list_del_init(&edge->list[UPPER]);
1042 upper = edge->node[UPPER];
1043 if (upper->detached) {
1044 list_del(&edge->list[LOWER]);
1045 lower = edge->node[LOWER];
1046 free_backref_edge(cache, edge);
1047 if (list_empty(&lower->upper))
1048 list_add(&lower->list, &useless);
1052 if (!RB_EMPTY_NODE(&upper->rb_node)) {
1053 if (upper->lowest) {
1054 list_del_init(&upper->lower);
1058 list_add_tail(&edge->list[UPPER], &upper->lower);
1062 BUG_ON(!upper->checked);
1063 BUG_ON(cowonly != upper->cowonly);
1065 rb_node = tree_insert(&cache->rb_root, upper->bytenr,
1068 backref_tree_panic(rb_node, -EEXIST,
1072 list_add_tail(&edge->list[UPPER], &upper->lower);
1074 list_for_each_entry(edge, &upper->upper, list[LOWER])
1075 list_add_tail(&edge->list[UPPER], &list);
1078 * process useless backref nodes. backref nodes for tree leaves
1079 * are deleted from the cache. backref nodes for upper level
1080 * tree blocks are left in the cache to avoid unnecessary backref
1083 while (!list_empty(&useless)) {
1084 upper = list_entry(useless.next, struct backref_node, list);
1085 list_del_init(&upper->list);
1086 BUG_ON(!list_empty(&upper->upper));
1089 if (upper->lowest) {
1090 list_del_init(&upper->lower);
1093 while (!list_empty(&upper->lower)) {
1094 edge = list_entry(upper->lower.next,
1095 struct backref_edge, list[UPPER]);
1096 list_del(&edge->list[UPPER]);
1097 list_del(&edge->list[LOWER]);
1098 lower = edge->node[LOWER];
1099 free_backref_edge(cache, edge);
1101 if (list_empty(&lower->upper))
1102 list_add(&lower->list, &useless);
1104 __mark_block_processed(rc, upper);
1105 if (upper->level > 0) {
1106 list_add(&upper->list, &cache->detached);
1107 upper->detached = 1;
1109 rb_erase(&upper->rb_node, &cache->rb_root);
1110 free_backref_node(cache, upper);
1114 btrfs_free_path(path1);
1115 btrfs_free_path(path2);
1117 while (!list_empty(&useless)) {
1118 lower = list_entry(useless.next,
1119 struct backref_node, upper);
1120 list_del_init(&lower->upper);
1123 INIT_LIST_HEAD(&list);
1125 if (RB_EMPTY_NODE(&upper->rb_node)) {
1126 list_splice_tail(&upper->upper, &list);
1127 free_backref_node(cache, upper);
1130 if (list_empty(&list))
1133 edge = list_entry(list.next, struct backref_edge,
1135 list_del(&edge->list[LOWER]);
1136 upper = edge->node[UPPER];
1137 free_backref_edge(cache, edge);
1139 return ERR_PTR(err);
1141 BUG_ON(node && node->detached);
1146 * helper to add backref node for the newly created snapshot.
1147 * the backref node is created by cloning backref node that
1148 * corresponds to root of source tree
1150 static int clone_backref_node(struct btrfs_trans_handle *trans,
1151 struct reloc_control *rc,
1152 struct btrfs_root *src,
1153 struct btrfs_root *dest)
1155 struct btrfs_root *reloc_root = src->reloc_root;
1156 struct backref_cache *cache = &rc->backref_cache;
1157 struct backref_node *node = NULL;
1158 struct backref_node *new_node;
1159 struct backref_edge *edge;
1160 struct backref_edge *new_edge;
1161 struct rb_node *rb_node;
1163 if (cache->last_trans > 0)
1164 update_backref_cache(trans, cache);
1166 rb_node = tree_search(&cache->rb_root, src->commit_root->start);
1168 node = rb_entry(rb_node, struct backref_node, rb_node);
1172 BUG_ON(node->new_bytenr != reloc_root->node->start);
1176 rb_node = tree_search(&cache->rb_root,
1177 reloc_root->commit_root->start);
1179 node = rb_entry(rb_node, struct backref_node,
1181 BUG_ON(node->detached);
1188 new_node = alloc_backref_node(cache);
1192 new_node->bytenr = dest->node->start;
1193 new_node->level = node->level;
1194 new_node->lowest = node->lowest;
1195 new_node->checked = 1;
1196 new_node->root = dest;
1198 if (!node->lowest) {
1199 list_for_each_entry(edge, &node->lower, list[UPPER]) {
1200 new_edge = alloc_backref_edge(cache);
1204 new_edge->node[UPPER] = new_node;
1205 new_edge->node[LOWER] = edge->node[LOWER];
1206 list_add_tail(&new_edge->list[UPPER],
1210 list_add_tail(&new_node->lower, &cache->leaves);
1213 rb_node = tree_insert(&cache->rb_root, new_node->bytenr,
1214 &new_node->rb_node);
1216 backref_tree_panic(rb_node, -EEXIST, new_node->bytenr);
1218 if (!new_node->lowest) {
1219 list_for_each_entry(new_edge, &new_node->lower, list[UPPER]) {
1220 list_add_tail(&new_edge->list[LOWER],
1221 &new_edge->node[LOWER]->upper);
1226 while (!list_empty(&new_node->lower)) {
1227 new_edge = list_entry(new_node->lower.next,
1228 struct backref_edge, list[UPPER]);
1229 list_del(&new_edge->list[UPPER]);
1230 free_backref_edge(cache, new_edge);
1232 free_backref_node(cache, new_node);
1237 * helper to add 'address of tree root -> reloc tree' mapping
1239 static int __must_check __add_reloc_root(struct btrfs_root *root)
1241 struct rb_node *rb_node;
1242 struct mapping_node *node;
1243 struct reloc_control *rc = root->fs_info->reloc_ctl;
1245 node = kmalloc(sizeof(*node), GFP_NOFS);
1249 node->bytenr = root->node->start;
1252 spin_lock(&rc->reloc_root_tree.lock);
1253 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
1254 node->bytenr, &node->rb_node);
1255 spin_unlock(&rc->reloc_root_tree.lock);
1257 btrfs_panic(root->fs_info, -EEXIST, "Duplicate root found "
1258 "for start=%llu while inserting into relocation "
1259 "tree\n", node->bytenr);
1264 list_add_tail(&root->root_list, &rc->reloc_roots);
1269 * helper to delete the 'address of tree root -> reloc tree'
1272 static void __del_reloc_root(struct btrfs_root *root)
1274 struct rb_node *rb_node;
1275 struct mapping_node *node = NULL;
1276 struct reloc_control *rc = root->fs_info->reloc_ctl;
1278 spin_lock(&rc->reloc_root_tree.lock);
1279 rb_node = tree_search(&rc->reloc_root_tree.rb_root,
1282 node = rb_entry(rb_node, struct mapping_node, rb_node);
1283 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
1285 spin_unlock(&rc->reloc_root_tree.lock);
1289 BUG_ON((struct btrfs_root *)node->data != root);
1291 spin_lock(&root->fs_info->trans_lock);
1292 list_del_init(&root->root_list);
1293 spin_unlock(&root->fs_info->trans_lock);
1298 * helper to update the 'address of tree root -> reloc tree'
1301 static int __update_reloc_root(struct btrfs_root *root, u64 new_bytenr)
1303 struct rb_node *rb_node;
1304 struct mapping_node *node = NULL;
1305 struct reloc_control *rc = root->fs_info->reloc_ctl;
1307 spin_lock(&rc->reloc_root_tree.lock);
1308 rb_node = tree_search(&rc->reloc_root_tree.rb_root,
1311 node = rb_entry(rb_node, struct mapping_node, rb_node);
1312 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
1314 spin_unlock(&rc->reloc_root_tree.lock);
1318 BUG_ON((struct btrfs_root *)node->data != root);
1320 spin_lock(&rc->reloc_root_tree.lock);
1321 node->bytenr = new_bytenr;
1322 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
1323 node->bytenr, &node->rb_node);
1324 spin_unlock(&rc->reloc_root_tree.lock);
1326 backref_tree_panic(rb_node, -EEXIST, node->bytenr);
1330 static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
1331 struct btrfs_root *root, u64 objectid)
1333 struct btrfs_root *reloc_root;
1334 struct extent_buffer *eb;
1335 struct btrfs_root_item *root_item;
1336 struct btrfs_key root_key;
1340 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
1343 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
1344 root_key.type = BTRFS_ROOT_ITEM_KEY;
1345 root_key.offset = objectid;
1347 if (root->root_key.objectid == objectid) {
1348 /* called by btrfs_init_reloc_root */
1349 ret = btrfs_copy_root(trans, root, root->commit_root, &eb,
1350 BTRFS_TREE_RELOC_OBJECTID);
1353 last_snap = btrfs_root_last_snapshot(&root->root_item);
1354 btrfs_set_root_last_snapshot(&root->root_item,
1355 trans->transid - 1);
1358 * called by btrfs_reloc_post_snapshot_hook.
1359 * the source tree is a reloc tree, all tree blocks
1360 * modified after it was created have RELOC flag
1361 * set in their headers. so it's OK to not update
1362 * the 'last_snapshot'.
1364 ret = btrfs_copy_root(trans, root, root->node, &eb,
1365 BTRFS_TREE_RELOC_OBJECTID);
1369 memcpy(root_item, &root->root_item, sizeof(*root_item));
1370 btrfs_set_root_bytenr(root_item, eb->start);
1371 btrfs_set_root_level(root_item, btrfs_header_level(eb));
1372 btrfs_set_root_generation(root_item, trans->transid);
1374 if (root->root_key.objectid == objectid) {
1375 btrfs_set_root_refs(root_item, 0);
1376 memset(&root_item->drop_progress, 0,
1377 sizeof(struct btrfs_disk_key));
1378 root_item->drop_level = 0;
1380 * abuse rtransid, it is safe because it is impossible to
1381 * receive data into a relocation tree.
1383 btrfs_set_root_rtransid(root_item, last_snap);
1384 btrfs_set_root_otransid(root_item, trans->transid);
1387 btrfs_tree_unlock(eb);
1388 free_extent_buffer(eb);
1390 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
1391 &root_key, root_item);
1395 reloc_root = btrfs_read_fs_root(root->fs_info->tree_root, &root_key);
1396 BUG_ON(IS_ERR(reloc_root));
1397 reloc_root->last_trans = trans->transid;
1402 * create reloc tree for a given fs tree. reloc tree is just a
1403 * snapshot of the fs tree with special root objectid.
1405 int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
1406 struct btrfs_root *root)
1408 struct btrfs_root *reloc_root;
1409 struct reloc_control *rc = root->fs_info->reloc_ctl;
1410 struct btrfs_block_rsv *rsv;
1414 if (root->reloc_root) {
1415 reloc_root = root->reloc_root;
1416 reloc_root->last_trans = trans->transid;
1420 if (!rc || !rc->create_reloc_tree ||
1421 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1424 if (!trans->reloc_reserved) {
1425 rsv = trans->block_rsv;
1426 trans->block_rsv = rc->block_rsv;
1429 reloc_root = create_reloc_root(trans, root, root->root_key.objectid);
1431 trans->block_rsv = rsv;
1433 ret = __add_reloc_root(reloc_root);
1435 root->reloc_root = reloc_root;
1440 * update root item of reloc tree
1442 int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
1443 struct btrfs_root *root)
1445 struct btrfs_root *reloc_root;
1446 struct btrfs_root_item *root_item;
1449 if (!root->reloc_root)
1452 reloc_root = root->reloc_root;
1453 root_item = &reloc_root->root_item;
1455 if (root->fs_info->reloc_ctl->merge_reloc_tree &&
1456 btrfs_root_refs(root_item) == 0) {
1457 root->reloc_root = NULL;
1458 __del_reloc_root(reloc_root);
1461 if (reloc_root->commit_root != reloc_root->node) {
1462 btrfs_set_root_node(root_item, reloc_root->node);
1463 free_extent_buffer(reloc_root->commit_root);
1464 reloc_root->commit_root = btrfs_root_node(reloc_root);
1467 ret = btrfs_update_root(trans, root->fs_info->tree_root,
1468 &reloc_root->root_key, root_item);
1476 * helper to find first cached inode with inode number >= objectid
1479 static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid)
1481 struct rb_node *node;
1482 struct rb_node *prev;
1483 struct btrfs_inode *entry;
1484 struct inode *inode;
1486 spin_lock(&root->inode_lock);
1488 node = root->inode_tree.rb_node;
1492 entry = rb_entry(node, struct btrfs_inode, rb_node);
1494 if (objectid < btrfs_ino(&entry->vfs_inode))
1495 node = node->rb_left;
1496 else if (objectid > btrfs_ino(&entry->vfs_inode))
1497 node = node->rb_right;
1503 entry = rb_entry(prev, struct btrfs_inode, rb_node);
1504 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
1508 prev = rb_next(prev);
1512 entry = rb_entry(node, struct btrfs_inode, rb_node);
1513 inode = igrab(&entry->vfs_inode);
1515 spin_unlock(&root->inode_lock);
1519 objectid = btrfs_ino(&entry->vfs_inode) + 1;
1520 if (cond_resched_lock(&root->inode_lock))
1523 node = rb_next(node);
1525 spin_unlock(&root->inode_lock);
1529 static int in_block_group(u64 bytenr,
1530 struct btrfs_block_group_cache *block_group)
1532 if (bytenr >= block_group->key.objectid &&
1533 bytenr < block_group->key.objectid + block_group->key.offset)
1539 * get new location of data
1541 static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr,
1542 u64 bytenr, u64 num_bytes)
1544 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
1545 struct btrfs_path *path;
1546 struct btrfs_file_extent_item *fi;
1547 struct extent_buffer *leaf;
1550 path = btrfs_alloc_path();
1554 bytenr -= BTRFS_I(reloc_inode)->index_cnt;
1555 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(reloc_inode),
1564 leaf = path->nodes[0];
1565 fi = btrfs_item_ptr(leaf, path->slots[0],
1566 struct btrfs_file_extent_item);
1568 BUG_ON(btrfs_file_extent_offset(leaf, fi) ||
1569 btrfs_file_extent_compression(leaf, fi) ||
1570 btrfs_file_extent_encryption(leaf, fi) ||
1571 btrfs_file_extent_other_encoding(leaf, fi));
1573 if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
1578 *new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1581 btrfs_free_path(path);
1586 * update file extent items in the tree leaf to point to
1587 * the new locations.
1589 static noinline_for_stack
1590 int replace_file_extents(struct btrfs_trans_handle *trans,
1591 struct reloc_control *rc,
1592 struct btrfs_root *root,
1593 struct extent_buffer *leaf)
1595 struct btrfs_key key;
1596 struct btrfs_file_extent_item *fi;
1597 struct inode *inode = NULL;
1609 if (rc->stage != UPDATE_DATA_PTRS)
1612 /* reloc trees always use full backref */
1613 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1614 parent = leaf->start;
1618 nritems = btrfs_header_nritems(leaf);
1619 for (i = 0; i < nritems; i++) {
1621 btrfs_item_key_to_cpu(leaf, &key, i);
1622 if (key.type != BTRFS_EXTENT_DATA_KEY)
1624 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1625 if (btrfs_file_extent_type(leaf, fi) ==
1626 BTRFS_FILE_EXTENT_INLINE)
1628 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1629 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1632 if (!in_block_group(bytenr, rc->block_group))
1636 * if we are modifying block in fs tree, wait for readpage
1637 * to complete and drop the extent cache
1639 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1641 inode = find_next_inode(root, key.objectid);
1643 } else if (inode && btrfs_ino(inode) < key.objectid) {
1644 btrfs_add_delayed_iput(inode);
1645 inode = find_next_inode(root, key.objectid);
1647 if (inode && btrfs_ino(inode) == key.objectid) {
1649 btrfs_file_extent_num_bytes(leaf, fi);
1650 WARN_ON(!IS_ALIGNED(key.offset,
1652 WARN_ON(!IS_ALIGNED(end, root->sectorsize));
1654 ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
1659 btrfs_drop_extent_cache(inode, key.offset, end,
1661 unlock_extent(&BTRFS_I(inode)->io_tree,
1666 ret = get_new_location(rc->data_inode, &new_bytenr,
1670 * Don't have to abort since we've not changed anything
1671 * in the file extent yet.
1676 btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr);
1679 key.offset -= btrfs_file_extent_offset(leaf, fi);
1680 ret = btrfs_inc_extent_ref(trans, root, new_bytenr,
1682 btrfs_header_owner(leaf),
1683 key.objectid, key.offset, 1);
1685 btrfs_abort_transaction(trans, root, ret);
1689 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1690 parent, btrfs_header_owner(leaf),
1691 key.objectid, key.offset, 1);
1693 btrfs_abort_transaction(trans, root, ret);
1698 btrfs_mark_buffer_dirty(leaf);
1700 btrfs_add_delayed_iput(inode);
1704 static noinline_for_stack
1705 int memcmp_node_keys(struct extent_buffer *eb, int slot,
1706 struct btrfs_path *path, int level)
1708 struct btrfs_disk_key key1;
1709 struct btrfs_disk_key key2;
1710 btrfs_node_key(eb, &key1, slot);
1711 btrfs_node_key(path->nodes[level], &key2, path->slots[level]);
1712 return memcmp(&key1, &key2, sizeof(key1));
1716 * try to replace tree blocks in fs tree with the new blocks
1717 * in reloc tree. tree blocks haven't been modified since the
1718 * reloc tree was create can be replaced.
1720 * if a block was replaced, level of the block + 1 is returned.
1721 * if no block got replaced, 0 is returned. if there are other
1722 * errors, a negative error number is returned.
1724 static noinline_for_stack
1725 int replace_path(struct btrfs_trans_handle *trans,
1726 struct btrfs_root *dest, struct btrfs_root *src,
1727 struct btrfs_path *path, struct btrfs_key *next_key,
1728 int lowest_level, int max_level)
1730 struct extent_buffer *eb;
1731 struct extent_buffer *parent;
1732 struct btrfs_key key;
1744 BUG_ON(src->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
1745 BUG_ON(dest->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID);
1747 last_snapshot = btrfs_root_last_snapshot(&src->root_item);
1749 slot = path->slots[lowest_level];
1750 btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot);
1752 eb = btrfs_lock_root_node(dest);
1753 btrfs_set_lock_blocking(eb);
1754 level = btrfs_header_level(eb);
1756 if (level < lowest_level) {
1757 btrfs_tree_unlock(eb);
1758 free_extent_buffer(eb);
1763 ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb);
1766 btrfs_set_lock_blocking(eb);
1769 next_key->objectid = (u64)-1;
1770 next_key->type = (u8)-1;
1771 next_key->offset = (u64)-1;
1776 level = btrfs_header_level(parent);
1777 BUG_ON(level < lowest_level);
1779 ret = btrfs_bin_search(parent, &key, level, &slot);
1780 if (ret && slot > 0)
1783 if (next_key && slot + 1 < btrfs_header_nritems(parent))
1784 btrfs_node_key_to_cpu(parent, next_key, slot + 1);
1786 old_bytenr = btrfs_node_blockptr(parent, slot);
1787 blocksize = btrfs_level_size(dest, level - 1);
1788 old_ptr_gen = btrfs_node_ptr_generation(parent, slot);
1790 if (level <= max_level) {
1791 eb = path->nodes[level];
1792 new_bytenr = btrfs_node_blockptr(eb,
1793 path->slots[level]);
1794 new_ptr_gen = btrfs_node_ptr_generation(eb,
1795 path->slots[level]);
1801 if (WARN_ON(new_bytenr > 0 && new_bytenr == old_bytenr)) {
1806 if (new_bytenr == 0 || old_ptr_gen > last_snapshot ||
1807 memcmp_node_keys(parent, slot, path, level)) {
1808 if (level <= lowest_level) {
1813 eb = read_tree_block(dest, old_bytenr, blocksize,
1815 if (!eb || !extent_buffer_uptodate(eb)) {
1816 ret = (!eb) ? -ENOMEM : -EIO;
1817 free_extent_buffer(eb);
1820 btrfs_tree_lock(eb);
1822 ret = btrfs_cow_block(trans, dest, eb, parent,
1826 btrfs_set_lock_blocking(eb);
1828 btrfs_tree_unlock(parent);
1829 free_extent_buffer(parent);
1836 btrfs_tree_unlock(parent);
1837 free_extent_buffer(parent);
1842 btrfs_node_key_to_cpu(path->nodes[level], &key,
1843 path->slots[level]);
1844 btrfs_release_path(path);
1846 path->lowest_level = level;
1847 ret = btrfs_search_slot(trans, src, &key, path, 0, 1);
1848 path->lowest_level = 0;
1852 * swap blocks in fs tree and reloc tree.
1854 btrfs_set_node_blockptr(parent, slot, new_bytenr);
1855 btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen);
1856 btrfs_mark_buffer_dirty(parent);
1858 btrfs_set_node_blockptr(path->nodes[level],
1859 path->slots[level], old_bytenr);
1860 btrfs_set_node_ptr_generation(path->nodes[level],
1861 path->slots[level], old_ptr_gen);
1862 btrfs_mark_buffer_dirty(path->nodes[level]);
1864 ret = btrfs_inc_extent_ref(trans, src, old_bytenr, blocksize,
1865 path->nodes[level]->start,
1866 src->root_key.objectid, level - 1, 0,
1869 ret = btrfs_inc_extent_ref(trans, dest, new_bytenr, blocksize,
1870 0, dest->root_key.objectid, level - 1,
1874 ret = btrfs_free_extent(trans, src, new_bytenr, blocksize,
1875 path->nodes[level]->start,
1876 src->root_key.objectid, level - 1, 0,
1880 ret = btrfs_free_extent(trans, dest, old_bytenr, blocksize,
1881 0, dest->root_key.objectid, level - 1,
1885 btrfs_unlock_up_safe(path, 0);
1890 btrfs_tree_unlock(parent);
1891 free_extent_buffer(parent);
1896 * helper to find next relocated block in reloc tree
1898 static noinline_for_stack
1899 int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1902 struct extent_buffer *eb;
1907 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1909 for (i = 0; i < *level; i++) {
1910 free_extent_buffer(path->nodes[i]);
1911 path->nodes[i] = NULL;
1914 for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
1915 eb = path->nodes[i];
1916 nritems = btrfs_header_nritems(eb);
1917 while (path->slots[i] + 1 < nritems) {
1919 if (btrfs_node_ptr_generation(eb, path->slots[i]) <=
1926 free_extent_buffer(path->nodes[i]);
1927 path->nodes[i] = NULL;
1933 * walk down reloc tree to find relocated block of lowest level
1935 static noinline_for_stack
1936 int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1939 struct extent_buffer *eb = NULL;
1947 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1949 for (i = *level; i > 0; i--) {
1950 eb = path->nodes[i];
1951 nritems = btrfs_header_nritems(eb);
1952 while (path->slots[i] < nritems) {
1953 ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]);
1954 if (ptr_gen > last_snapshot)
1958 if (path->slots[i] >= nritems) {
1969 bytenr = btrfs_node_blockptr(eb, path->slots[i]);
1970 blocksize = btrfs_level_size(root, i - 1);
1971 eb = read_tree_block(root, bytenr, blocksize, ptr_gen);
1972 if (!eb || !extent_buffer_uptodate(eb)) {
1973 free_extent_buffer(eb);
1976 BUG_ON(btrfs_header_level(eb) != i - 1);
1977 path->nodes[i - 1] = eb;
1978 path->slots[i - 1] = 0;
1984 * invalidate extent cache for file extents whose key in range of
1985 * [min_key, max_key)
1987 static int invalidate_extent_cache(struct btrfs_root *root,
1988 struct btrfs_key *min_key,
1989 struct btrfs_key *max_key)
1991 struct inode *inode = NULL;
1996 objectid = min_key->objectid;
2001 if (objectid > max_key->objectid)
2004 inode = find_next_inode(root, objectid);
2007 ino = btrfs_ino(inode);
2009 if (ino > max_key->objectid) {
2015 if (!S_ISREG(inode->i_mode))
2018 if (unlikely(min_key->objectid == ino)) {
2019 if (min_key->type > BTRFS_EXTENT_DATA_KEY)
2021 if (min_key->type < BTRFS_EXTENT_DATA_KEY)
2024 start = min_key->offset;
2025 WARN_ON(!IS_ALIGNED(start, root->sectorsize));
2031 if (unlikely(max_key->objectid == ino)) {
2032 if (max_key->type < BTRFS_EXTENT_DATA_KEY)
2034 if (max_key->type > BTRFS_EXTENT_DATA_KEY) {
2037 if (max_key->offset == 0)
2039 end = max_key->offset;
2040 WARN_ON(!IS_ALIGNED(end, root->sectorsize));
2047 /* the lock_extent waits for readpage to complete */
2048 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
2049 btrfs_drop_extent_cache(inode, start, end, 1);
2050 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
2055 static int find_next_key(struct btrfs_path *path, int level,
2056 struct btrfs_key *key)
2059 while (level < BTRFS_MAX_LEVEL) {
2060 if (!path->nodes[level])
2062 if (path->slots[level] + 1 <
2063 btrfs_header_nritems(path->nodes[level])) {
2064 btrfs_node_key_to_cpu(path->nodes[level], key,
2065 path->slots[level] + 1);
2074 * merge the relocated tree blocks in reloc tree with corresponding
2077 static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
2078 struct btrfs_root *root)
2080 LIST_HEAD(inode_list);
2081 struct btrfs_key key;
2082 struct btrfs_key next_key;
2083 struct btrfs_trans_handle *trans = NULL;
2084 struct btrfs_root *reloc_root;
2085 struct btrfs_root_item *root_item;
2086 struct btrfs_path *path;
2087 struct extent_buffer *leaf;
2095 path = btrfs_alloc_path();
2100 reloc_root = root->reloc_root;
2101 root_item = &reloc_root->root_item;
2103 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2104 level = btrfs_root_level(root_item);
2105 extent_buffer_get(reloc_root->node);
2106 path->nodes[level] = reloc_root->node;
2107 path->slots[level] = 0;
2109 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2111 level = root_item->drop_level;
2113 path->lowest_level = level;
2114 ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0);
2115 path->lowest_level = 0;
2117 btrfs_free_path(path);
2121 btrfs_node_key_to_cpu(path->nodes[level], &next_key,
2122 path->slots[level]);
2123 WARN_ON(memcmp(&key, &next_key, sizeof(key)));
2125 btrfs_unlock_up_safe(path, 0);
2128 min_reserved = root->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
2129 memset(&next_key, 0, sizeof(next_key));
2132 ret = btrfs_block_rsv_refill(root, rc->block_rsv, min_reserved,
2133 BTRFS_RESERVE_FLUSH_ALL);
2138 trans = btrfs_start_transaction(root, 0);
2139 if (IS_ERR(trans)) {
2140 err = PTR_ERR(trans);
2144 trans->block_rsv = rc->block_rsv;
2149 ret = walk_down_reloc_tree(reloc_root, path, &level);
2157 if (!find_next_key(path, level, &key) &&
2158 btrfs_comp_cpu_keys(&next_key, &key) >= 0) {
2161 ret = replace_path(trans, root, reloc_root, path,
2162 &next_key, level, max_level);
2171 btrfs_node_key_to_cpu(path->nodes[level], &key,
2172 path->slots[level]);
2176 ret = walk_up_reloc_tree(reloc_root, path, &level);
2182 * save the merging progress in the drop_progress.
2183 * this is OK since root refs == 1 in this case.
2185 btrfs_node_key(path->nodes[level], &root_item->drop_progress,
2186 path->slots[level]);
2187 root_item->drop_level = level;
2189 btrfs_end_transaction_throttle(trans, root);
2192 btrfs_btree_balance_dirty(root);
2194 if (replaced && rc->stage == UPDATE_DATA_PTRS)
2195 invalidate_extent_cache(root, &key, &next_key);
2199 * handle the case only one block in the fs tree need to be
2200 * relocated and the block is tree root.
2202 leaf = btrfs_lock_root_node(root);
2203 ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf);
2204 btrfs_tree_unlock(leaf);
2205 free_extent_buffer(leaf);
2209 btrfs_free_path(path);
2212 memset(&root_item->drop_progress, 0,
2213 sizeof(root_item->drop_progress));
2214 root_item->drop_level = 0;
2215 btrfs_set_root_refs(root_item, 0);
2216 btrfs_update_reloc_root(trans, root);
2220 btrfs_end_transaction_throttle(trans, root);
2222 btrfs_btree_balance_dirty(root);
2224 if (replaced && rc->stage == UPDATE_DATA_PTRS)
2225 invalidate_extent_cache(root, &key, &next_key);
2230 static noinline_for_stack
2231 int prepare_to_merge(struct reloc_control *rc, int err)
2233 struct btrfs_root *root = rc->extent_root;
2234 struct btrfs_root *reloc_root;
2235 struct btrfs_trans_handle *trans;
2236 LIST_HEAD(reloc_roots);
2240 mutex_lock(&root->fs_info->reloc_mutex);
2241 rc->merging_rsv_size += root->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
2242 rc->merging_rsv_size += rc->nodes_relocated * 2;
2243 mutex_unlock(&root->fs_info->reloc_mutex);
2247 num_bytes = rc->merging_rsv_size;
2248 ret = btrfs_block_rsv_add(root, rc->block_rsv, num_bytes,
2249 BTRFS_RESERVE_FLUSH_ALL);
2254 trans = btrfs_join_transaction(rc->extent_root);
2255 if (IS_ERR(trans)) {
2257 btrfs_block_rsv_release(rc->extent_root,
2258 rc->block_rsv, num_bytes);
2259 return PTR_ERR(trans);
2263 if (num_bytes != rc->merging_rsv_size) {
2264 btrfs_end_transaction(trans, rc->extent_root);
2265 btrfs_block_rsv_release(rc->extent_root,
2266 rc->block_rsv, num_bytes);
2271 rc->merge_reloc_tree = 1;
2273 while (!list_empty(&rc->reloc_roots)) {
2274 reloc_root = list_entry(rc->reloc_roots.next,
2275 struct btrfs_root, root_list);
2276 list_del_init(&reloc_root->root_list);
2278 root = read_fs_root(reloc_root->fs_info,
2279 reloc_root->root_key.offset);
2280 BUG_ON(IS_ERR(root));
2281 BUG_ON(root->reloc_root != reloc_root);
2284 * set reference count to 1, so btrfs_recover_relocation
2285 * knows it should resumes merging
2288 btrfs_set_root_refs(&reloc_root->root_item, 1);
2289 btrfs_update_reloc_root(trans, root);
2291 list_add(&reloc_root->root_list, &reloc_roots);
2294 list_splice(&reloc_roots, &rc->reloc_roots);
2297 btrfs_commit_transaction(trans, rc->extent_root);
2299 btrfs_end_transaction(trans, rc->extent_root);
2303 static noinline_for_stack
2304 void free_reloc_roots(struct list_head *list)
2306 struct btrfs_root *reloc_root;
2308 while (!list_empty(list)) {
2309 reloc_root = list_entry(list->next, struct btrfs_root,
2311 __del_reloc_root(reloc_root);
2312 free_extent_buffer(reloc_root->node);
2313 free_extent_buffer(reloc_root->commit_root);
2318 static noinline_for_stack
2319 int merge_reloc_roots(struct reloc_control *rc)
2321 struct btrfs_trans_handle *trans;
2322 struct btrfs_root *root;
2323 struct btrfs_root *reloc_root;
2327 LIST_HEAD(reloc_roots);
2331 root = rc->extent_root;
2334 * this serializes us with btrfs_record_root_in_transaction,
2335 * we have to make sure nobody is in the middle of
2336 * adding their roots to the list while we are
2339 mutex_lock(&root->fs_info->reloc_mutex);
2340 list_splice_init(&rc->reloc_roots, &reloc_roots);
2341 mutex_unlock(&root->fs_info->reloc_mutex);
2343 while (!list_empty(&reloc_roots)) {
2345 reloc_root = list_entry(reloc_roots.next,
2346 struct btrfs_root, root_list);
2348 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
2349 root = read_fs_root(reloc_root->fs_info,
2350 reloc_root->root_key.offset);
2351 BUG_ON(IS_ERR(root));
2352 BUG_ON(root->reloc_root != reloc_root);
2354 ret = merge_reloc_root(rc, root);
2356 __del_reloc_root(reloc_root);
2357 free_extent_buffer(reloc_root->node);
2358 free_extent_buffer(reloc_root->commit_root);
2363 list_del_init(&reloc_root->root_list);
2367 * we keep the old last snapshod transid in rtranid when we
2368 * created the relocation tree.
2370 last_snap = btrfs_root_rtransid(&reloc_root->root_item);
2371 otransid = btrfs_root_otransid(&reloc_root->root_item);
2372 objectid = reloc_root->root_key.offset;
2374 ret = btrfs_drop_snapshot(reloc_root, rc->block_rsv, 0, 1);
2376 if (list_empty(&reloc_root->root_list))
2377 list_add_tail(&reloc_root->root_list,
2382 * recover the last snapshot tranid to avoid
2383 * the space balance break NOCOW.
2385 root = read_fs_root(rc->extent_root->fs_info,
2390 trans = btrfs_join_transaction(root);
2391 BUG_ON(IS_ERR(trans));
2393 /* Check if the fs/file tree was snapshoted or not. */
2394 if (btrfs_root_last_snapshot(&root->root_item) ==
2396 btrfs_set_root_last_snapshot(&root->root_item,
2399 btrfs_end_transaction(trans, root);
2409 btrfs_std_error(root->fs_info, ret);
2410 if (!list_empty(&reloc_roots))
2411 free_reloc_roots(&reloc_roots);
2413 /* new reloc root may be added */
2414 mutex_lock(&root->fs_info->reloc_mutex);
2415 list_splice_init(&rc->reloc_roots, &reloc_roots);
2416 mutex_unlock(&root->fs_info->reloc_mutex);
2417 if (!list_empty(&reloc_roots))
2418 free_reloc_roots(&reloc_roots);
2421 BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
2425 static void free_block_list(struct rb_root *blocks)
2427 struct tree_block *block;
2428 struct rb_node *rb_node;
2429 while ((rb_node = rb_first(blocks))) {
2430 block = rb_entry(rb_node, struct tree_block, rb_node);
2431 rb_erase(rb_node, blocks);
2436 static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans,
2437 struct btrfs_root *reloc_root)
2439 struct btrfs_root *root;
2441 if (reloc_root->last_trans == trans->transid)
2444 root = read_fs_root(reloc_root->fs_info, reloc_root->root_key.offset);
2445 BUG_ON(IS_ERR(root));
2446 BUG_ON(root->reloc_root != reloc_root);
2448 return btrfs_record_root_in_trans(trans, root);
2451 static noinline_for_stack
2452 struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,
2453 struct reloc_control *rc,
2454 struct backref_node *node,
2455 struct backref_edge *edges[], int *nr)
2457 struct backref_node *next;
2458 struct btrfs_root *root;
2464 next = walk_up_backref(next, edges, &index);
2467 BUG_ON(!root->ref_cows);
2469 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
2470 record_reloc_root_in_trans(trans, root);
2474 btrfs_record_root_in_trans(trans, root);
2475 root = root->reloc_root;
2477 if (next->new_bytenr != root->node->start) {
2478 BUG_ON(next->new_bytenr);
2479 BUG_ON(!list_empty(&next->list));
2480 next->new_bytenr = root->node->start;
2482 list_add_tail(&next->list,
2483 &rc->backref_cache.changed);
2484 __mark_block_processed(rc, next);
2490 next = walk_down_backref(edges, &index);
2491 if (!next || next->level <= node->level)
2499 /* setup backref node path for btrfs_reloc_cow_block */
2501 rc->backref_cache.path[next->level] = next;
2504 next = edges[index]->node[UPPER];
2510 * select a tree root for relocation. return NULL if the block
2511 * is reference counted. we should use do_relocation() in this
2512 * case. return a tree root pointer if the block isn't reference
2513 * counted. return -ENOENT if the block is root of reloc tree.
2515 static noinline_for_stack
2516 struct btrfs_root *select_one_root(struct btrfs_trans_handle *trans,
2517 struct backref_node *node)
2519 struct backref_node *next;
2520 struct btrfs_root *root;
2521 struct btrfs_root *fs_root = NULL;
2522 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2528 next = walk_up_backref(next, edges, &index);
2532 /* no other choice for non-references counted tree */
2533 if (!root->ref_cows)
2536 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID)
2542 next = walk_down_backref(edges, &index);
2543 if (!next || next->level <= node->level)
2548 return ERR_PTR(-ENOENT);
2552 static noinline_for_stack
2553 u64 calcu_metadata_size(struct reloc_control *rc,
2554 struct backref_node *node, int reserve)
2556 struct backref_node *next = node;
2557 struct backref_edge *edge;
2558 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2562 BUG_ON(reserve && node->processed);
2567 if (next->processed && (reserve || next != node))
2570 num_bytes += btrfs_level_size(rc->extent_root,
2573 if (list_empty(&next->upper))
2576 edge = list_entry(next->upper.next,
2577 struct backref_edge, list[LOWER]);
2578 edges[index++] = edge;
2579 next = edge->node[UPPER];
2581 next = walk_down_backref(edges, &index);
2586 static int reserve_metadata_space(struct btrfs_trans_handle *trans,
2587 struct reloc_control *rc,
2588 struct backref_node *node)
2590 struct btrfs_root *root = rc->extent_root;
2594 num_bytes = calcu_metadata_size(rc, node, 1) * 2;
2596 trans->block_rsv = rc->block_rsv;
2597 ret = btrfs_block_rsv_add(root, rc->block_rsv, num_bytes,
2598 BTRFS_RESERVE_FLUSH_ALL);
2601 rc->commit_transaction = 1;
2608 static void release_metadata_space(struct reloc_control *rc,
2609 struct backref_node *node)
2611 u64 num_bytes = calcu_metadata_size(rc, node, 0) * 2;
2612 btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, num_bytes);
2616 * relocate a block tree, and then update pointers in upper level
2617 * blocks that reference the block to point to the new location.
2619 * if called by link_to_upper, the block has already been relocated.
2620 * in that case this function just updates pointers.
2622 static int do_relocation(struct btrfs_trans_handle *trans,
2623 struct reloc_control *rc,
2624 struct backref_node *node,
2625 struct btrfs_key *key,
2626 struct btrfs_path *path, int lowest)
2628 struct backref_node *upper;
2629 struct backref_edge *edge;
2630 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2631 struct btrfs_root *root;
2632 struct extent_buffer *eb;
2641 BUG_ON(lowest && node->eb);
2643 path->lowest_level = node->level + 1;
2644 rc->backref_cache.path[node->level] = node;
2645 list_for_each_entry(edge, &node->upper, list[LOWER]) {
2648 upper = edge->node[UPPER];
2649 root = select_reloc_root(trans, rc, upper, edges, &nr);
2652 if (upper->eb && !upper->locked) {
2654 ret = btrfs_bin_search(upper->eb, key,
2655 upper->level, &slot);
2657 bytenr = btrfs_node_blockptr(upper->eb, slot);
2658 if (node->eb->start == bytenr)
2661 drop_node_buffer(upper);
2665 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2673 upper->eb = path->nodes[upper->level];
2674 path->nodes[upper->level] = NULL;
2676 BUG_ON(upper->eb != path->nodes[upper->level]);
2680 path->locks[upper->level] = 0;
2682 slot = path->slots[upper->level];
2683 btrfs_release_path(path);
2685 ret = btrfs_bin_search(upper->eb, key, upper->level,
2690 bytenr = btrfs_node_blockptr(upper->eb, slot);
2692 BUG_ON(bytenr != node->bytenr);
2694 if (node->eb->start == bytenr)
2698 blocksize = btrfs_level_size(root, node->level);
2699 generation = btrfs_node_ptr_generation(upper->eb, slot);
2700 eb = read_tree_block(root, bytenr, blocksize, generation);
2701 if (!eb || !extent_buffer_uptodate(eb)) {
2702 free_extent_buffer(eb);
2706 btrfs_tree_lock(eb);
2707 btrfs_set_lock_blocking(eb);
2710 ret = btrfs_cow_block(trans, root, eb, upper->eb,
2712 btrfs_tree_unlock(eb);
2713 free_extent_buffer(eb);
2718 BUG_ON(node->eb != eb);
2720 btrfs_set_node_blockptr(upper->eb, slot,
2722 btrfs_set_node_ptr_generation(upper->eb, slot,
2724 btrfs_mark_buffer_dirty(upper->eb);
2726 ret = btrfs_inc_extent_ref(trans, root,
2727 node->eb->start, blocksize,
2729 btrfs_header_owner(upper->eb),
2733 ret = btrfs_drop_subtree(trans, root, eb, upper->eb);
2737 if (!upper->pending)
2738 drop_node_buffer(upper);
2740 unlock_node_buffer(upper);
2745 if (!err && node->pending) {
2746 drop_node_buffer(node);
2747 list_move_tail(&node->list, &rc->backref_cache.changed);
2751 path->lowest_level = 0;
2752 BUG_ON(err == -ENOSPC);
2756 static int link_to_upper(struct btrfs_trans_handle *trans,
2757 struct reloc_control *rc,
2758 struct backref_node *node,
2759 struct btrfs_path *path)
2761 struct btrfs_key key;
2763 btrfs_node_key_to_cpu(node->eb, &key, 0);
2764 return do_relocation(trans, rc, node, &key, path, 0);
2767 static int finish_pending_nodes(struct btrfs_trans_handle *trans,
2768 struct reloc_control *rc,
2769 struct btrfs_path *path, int err)
2772 struct backref_cache *cache = &rc->backref_cache;
2773 struct backref_node *node;
2777 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2778 while (!list_empty(&cache->pending[level])) {
2779 node = list_entry(cache->pending[level].next,
2780 struct backref_node, list);
2781 list_move_tail(&node->list, &list);
2782 BUG_ON(!node->pending);
2785 ret = link_to_upper(trans, rc, node, path);
2790 list_splice_init(&list, &cache->pending[level]);
2795 static void mark_block_processed(struct reloc_control *rc,
2796 u64 bytenr, u32 blocksize)
2798 set_extent_bits(&rc->processed_blocks, bytenr, bytenr + blocksize - 1,
2799 EXTENT_DIRTY, GFP_NOFS);
2802 static void __mark_block_processed(struct reloc_control *rc,
2803 struct backref_node *node)
2806 if (node->level == 0 ||
2807 in_block_group(node->bytenr, rc->block_group)) {
2808 blocksize = btrfs_level_size(rc->extent_root, node->level);
2809 mark_block_processed(rc, node->bytenr, blocksize);
2811 node->processed = 1;
2815 * mark a block and all blocks directly/indirectly reference the block
2818 static void update_processed_blocks(struct reloc_control *rc,
2819 struct backref_node *node)
2821 struct backref_node *next = node;
2822 struct backref_edge *edge;
2823 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2829 if (next->processed)
2832 __mark_block_processed(rc, next);
2834 if (list_empty(&next->upper))
2837 edge = list_entry(next->upper.next,
2838 struct backref_edge, list[LOWER]);
2839 edges[index++] = edge;
2840 next = edge->node[UPPER];
2842 next = walk_down_backref(edges, &index);
2846 static int tree_block_processed(u64 bytenr, u32 blocksize,
2847 struct reloc_control *rc)
2849 if (test_range_bit(&rc->processed_blocks, bytenr,
2850 bytenr + blocksize - 1, EXTENT_DIRTY, 1, NULL))
2855 static int get_tree_block_key(struct reloc_control *rc,
2856 struct tree_block *block)
2858 struct extent_buffer *eb;
2860 BUG_ON(block->key_ready);
2861 eb = read_tree_block(rc->extent_root, block->bytenr,
2862 block->key.objectid, block->key.offset);
2863 if (!eb || !extent_buffer_uptodate(eb)) {
2864 free_extent_buffer(eb);
2867 WARN_ON(btrfs_header_level(eb) != block->level);
2868 if (block->level == 0)
2869 btrfs_item_key_to_cpu(eb, &block->key, 0);
2871 btrfs_node_key_to_cpu(eb, &block->key, 0);
2872 free_extent_buffer(eb);
2873 block->key_ready = 1;
2877 static int reada_tree_block(struct reloc_control *rc,
2878 struct tree_block *block)
2880 BUG_ON(block->key_ready);
2881 if (block->key.type == BTRFS_METADATA_ITEM_KEY)
2882 readahead_tree_block(rc->extent_root, block->bytenr,
2883 block->key.objectid,
2884 rc->extent_root->leafsize);
2886 readahead_tree_block(rc->extent_root, block->bytenr,
2887 block->key.objectid, block->key.offset);
2892 * helper function to relocate a tree block
2894 static int relocate_tree_block(struct btrfs_trans_handle *trans,
2895 struct reloc_control *rc,
2896 struct backref_node *node,
2897 struct btrfs_key *key,
2898 struct btrfs_path *path)
2900 struct btrfs_root *root;
2907 BUG_ON(node->processed);
2908 root = select_one_root(trans, node);
2909 if (root == ERR_PTR(-ENOENT)) {
2910 update_processed_blocks(rc, node);
2914 if (!root || root->ref_cows) {
2915 ret = reserve_metadata_space(trans, rc, node);
2922 if (root->ref_cows) {
2923 BUG_ON(node->new_bytenr);
2924 BUG_ON(!list_empty(&node->list));
2925 btrfs_record_root_in_trans(trans, root);
2926 root = root->reloc_root;
2927 node->new_bytenr = root->node->start;
2929 list_add_tail(&node->list, &rc->backref_cache.changed);
2931 path->lowest_level = node->level;
2932 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2933 btrfs_release_path(path);
2938 update_processed_blocks(rc, node);
2940 ret = do_relocation(trans, rc, node, key, path, 1);
2943 if (ret || node->level == 0 || node->cowonly) {
2945 release_metadata_space(rc, node);
2946 remove_backref_node(&rc->backref_cache, node);
2952 * relocate a list of blocks
2954 static noinline_for_stack
2955 int relocate_tree_blocks(struct btrfs_trans_handle *trans,
2956 struct reloc_control *rc, struct rb_root *blocks)
2958 struct backref_node *node;
2959 struct btrfs_path *path;
2960 struct tree_block *block;
2961 struct rb_node *rb_node;
2965 path = btrfs_alloc_path();
2968 goto out_free_blocks;
2971 rb_node = rb_first(blocks);
2973 block = rb_entry(rb_node, struct tree_block, rb_node);
2974 if (!block->key_ready)
2975 reada_tree_block(rc, block);
2976 rb_node = rb_next(rb_node);
2979 rb_node = rb_first(blocks);
2981 block = rb_entry(rb_node, struct tree_block, rb_node);
2982 if (!block->key_ready) {
2983 err = get_tree_block_key(rc, block);
2987 rb_node = rb_next(rb_node);
2990 rb_node = rb_first(blocks);
2992 block = rb_entry(rb_node, struct tree_block, rb_node);
2994 node = build_backref_tree(rc, &block->key,
2995 block->level, block->bytenr);
2997 err = PTR_ERR(node);
3001 ret = relocate_tree_block(trans, rc, node, &block->key,
3004 if (ret != -EAGAIN || rb_node == rb_first(blocks))
3008 rb_node = rb_next(rb_node);
3011 err = finish_pending_nodes(trans, rc, path, err);
3014 btrfs_free_path(path);
3016 free_block_list(blocks);
3020 static noinline_for_stack
3021 int prealloc_file_extent_cluster(struct inode *inode,
3022 struct file_extent_cluster *cluster)
3027 u64 offset = BTRFS_I(inode)->index_cnt;
3032 BUG_ON(cluster->start != cluster->boundary[0]);
3033 mutex_lock(&inode->i_mutex);
3035 ret = btrfs_check_data_free_space(inode, cluster->end +
3036 1 - cluster->start);
3040 while (nr < cluster->nr) {
3041 start = cluster->boundary[nr] - offset;
3042 if (nr + 1 < cluster->nr)
3043 end = cluster->boundary[nr + 1] - 1 - offset;
3045 end = cluster->end - offset;
3047 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
3048 num_bytes = end + 1 - start;
3049 ret = btrfs_prealloc_file_range(inode, 0, start,
3050 num_bytes, num_bytes,
3051 end + 1, &alloc_hint);
3052 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
3057 btrfs_free_reserved_data_space(inode, cluster->end +
3058 1 - cluster->start);
3060 mutex_unlock(&inode->i_mutex);
3064 static noinline_for_stack
3065 int setup_extent_mapping(struct inode *inode, u64 start, u64 end,
3068 struct btrfs_root *root = BTRFS_I(inode)->root;
3069 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3070 struct extent_map *em;
3073 em = alloc_extent_map();
3078 em->len = end + 1 - start;
3079 em->block_len = em->len;
3080 em->block_start = block_start;
3081 em->bdev = root->fs_info->fs_devices->latest_bdev;
3082 set_bit(EXTENT_FLAG_PINNED, &em->flags);
3084 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
3086 write_lock(&em_tree->lock);
3087 ret = add_extent_mapping(em_tree, em, 0);
3088 write_unlock(&em_tree->lock);
3089 if (ret != -EEXIST) {
3090 free_extent_map(em);
3093 btrfs_drop_extent_cache(inode, start, end, 0);
3095 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
3099 static int relocate_file_extent_cluster(struct inode *inode,
3100 struct file_extent_cluster *cluster)
3104 u64 offset = BTRFS_I(inode)->index_cnt;
3105 unsigned long index;
3106 unsigned long last_index;
3108 struct file_ra_state *ra;
3109 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
3116 ra = kzalloc(sizeof(*ra), GFP_NOFS);
3120 ret = prealloc_file_extent_cluster(inode, cluster);
3124 file_ra_state_init(ra, inode->i_mapping);
3126 ret = setup_extent_mapping(inode, cluster->start - offset,
3127 cluster->end - offset, cluster->start);
3131 index = (cluster->start - offset) >> PAGE_CACHE_SHIFT;
3132 last_index = (cluster->end - offset) >> PAGE_CACHE_SHIFT;
3133 while (index <= last_index) {
3134 ret = btrfs_delalloc_reserve_metadata(inode, PAGE_CACHE_SIZE);
3138 page = find_lock_page(inode->i_mapping, index);
3140 page_cache_sync_readahead(inode->i_mapping,
3142 last_index + 1 - index);
3143 page = find_or_create_page(inode->i_mapping, index,
3146 btrfs_delalloc_release_metadata(inode,
3153 if (PageReadahead(page)) {
3154 page_cache_async_readahead(inode->i_mapping,
3155 ra, NULL, page, index,
3156 last_index + 1 - index);
3159 if (!PageUptodate(page)) {
3160 btrfs_readpage(NULL, page);
3162 if (!PageUptodate(page)) {
3164 page_cache_release(page);
3165 btrfs_delalloc_release_metadata(inode,
3172 page_start = page_offset(page);
3173 page_end = page_start + PAGE_CACHE_SIZE - 1;
3175 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end);
3177 set_page_extent_mapped(page);
3179 if (nr < cluster->nr &&
3180 page_start + offset == cluster->boundary[nr]) {
3181 set_extent_bits(&BTRFS_I(inode)->io_tree,
3182 page_start, page_end,
3183 EXTENT_BOUNDARY, GFP_NOFS);
3187 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
3188 set_page_dirty(page);
3190 unlock_extent(&BTRFS_I(inode)->io_tree,
3191 page_start, page_end);
3193 page_cache_release(page);
3196 balance_dirty_pages_ratelimited(inode->i_mapping);
3197 btrfs_throttle(BTRFS_I(inode)->root);
3199 WARN_ON(nr != cluster->nr);
3205 static noinline_for_stack
3206 int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key,
3207 struct file_extent_cluster *cluster)
3211 if (cluster->nr > 0 && extent_key->objectid != cluster->end + 1) {
3212 ret = relocate_file_extent_cluster(inode, cluster);
3219 cluster->start = extent_key->objectid;
3221 BUG_ON(cluster->nr >= MAX_EXTENTS);
3222 cluster->end = extent_key->objectid + extent_key->offset - 1;
3223 cluster->boundary[cluster->nr] = extent_key->objectid;
3226 if (cluster->nr >= MAX_EXTENTS) {
3227 ret = relocate_file_extent_cluster(inode, cluster);
3235 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3236 static int get_ref_objectid_v0(struct reloc_control *rc,
3237 struct btrfs_path *path,
3238 struct btrfs_key *extent_key,
3239 u64 *ref_objectid, int *path_change)
3241 struct btrfs_key key;
3242 struct extent_buffer *leaf;
3243 struct btrfs_extent_ref_v0 *ref0;
3247 leaf = path->nodes[0];
3248 slot = path->slots[0];
3250 if (slot >= btrfs_header_nritems(leaf)) {
3251 ret = btrfs_next_leaf(rc->extent_root, path);
3255 leaf = path->nodes[0];
3256 slot = path->slots[0];
3260 btrfs_item_key_to_cpu(leaf, &key, slot);
3261 if (key.objectid != extent_key->objectid)
3264 if (key.type != BTRFS_EXTENT_REF_V0_KEY) {
3268 ref0 = btrfs_item_ptr(leaf, slot,
3269 struct btrfs_extent_ref_v0);
3270 *ref_objectid = btrfs_ref_objectid_v0(leaf, ref0);
3278 * helper to add a tree block to the list.
3279 * the major work is getting the generation and level of the block
3281 static int add_tree_block(struct reloc_control *rc,
3282 struct btrfs_key *extent_key,
3283 struct btrfs_path *path,
3284 struct rb_root *blocks)
3286 struct extent_buffer *eb;
3287 struct btrfs_extent_item *ei;
3288 struct btrfs_tree_block_info *bi;
3289 struct tree_block *block;
3290 struct rb_node *rb_node;
3295 eb = path->nodes[0];
3296 item_size = btrfs_item_size_nr(eb, path->slots[0]);
3298 if (extent_key->type == BTRFS_METADATA_ITEM_KEY ||
3299 item_size >= sizeof(*ei) + sizeof(*bi)) {
3300 ei = btrfs_item_ptr(eb, path->slots[0],
3301 struct btrfs_extent_item);
3302 if (extent_key->type == BTRFS_EXTENT_ITEM_KEY) {
3303 bi = (struct btrfs_tree_block_info *)(ei + 1);
3304 level = btrfs_tree_block_level(eb, bi);
3306 level = (int)extent_key->offset;
3308 generation = btrfs_extent_generation(eb, ei);
3310 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3314 BUG_ON(item_size != sizeof(struct btrfs_extent_item_v0));
3315 ret = get_ref_objectid_v0(rc, path, extent_key,
3319 BUG_ON(ref_owner >= BTRFS_MAX_LEVEL);
3320 level = (int)ref_owner;
3321 /* FIXME: get real generation */
3328 btrfs_release_path(path);
3330 BUG_ON(level == -1);
3332 block = kmalloc(sizeof(*block), GFP_NOFS);
3336 block->bytenr = extent_key->objectid;
3337 block->key.objectid = rc->extent_root->leafsize;
3338 block->key.offset = generation;
3339 block->level = level;
3340 block->key_ready = 0;
3342 rb_node = tree_insert(blocks, block->bytenr, &block->rb_node);
3344 backref_tree_panic(rb_node, -EEXIST, block->bytenr);
3350 * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
3352 static int __add_tree_block(struct reloc_control *rc,
3353 u64 bytenr, u32 blocksize,
3354 struct rb_root *blocks)
3356 struct btrfs_path *path;
3357 struct btrfs_key key;
3359 bool skinny = btrfs_fs_incompat(rc->extent_root->fs_info,
3362 if (tree_block_processed(bytenr, blocksize, rc))
3365 if (tree_search(blocks, bytenr))
3368 path = btrfs_alloc_path();
3372 key.objectid = bytenr;
3374 key.type = BTRFS_METADATA_ITEM_KEY;
3375 key.offset = (u64)-1;
3377 key.type = BTRFS_EXTENT_ITEM_KEY;
3378 key.offset = blocksize;
3381 path->search_commit_root = 1;
3382 path->skip_locking = 1;
3383 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0);
3387 if (ret > 0 && skinny) {
3388 if (path->slots[0]) {
3390 btrfs_item_key_to_cpu(path->nodes[0], &key,
3392 if (key.objectid == bytenr &&
3393 (key.type == BTRFS_METADATA_ITEM_KEY ||
3394 (key.type == BTRFS_EXTENT_ITEM_KEY &&
3395 key.offset == blocksize)))
3401 btrfs_release_path(path);
3407 ret = add_tree_block(rc, &key, path, blocks);
3409 btrfs_free_path(path);
3414 * helper to check if the block use full backrefs for pointers in it
3416 static int block_use_full_backref(struct reloc_control *rc,
3417 struct extent_buffer *eb)
3422 if (btrfs_header_flag(eb, BTRFS_HEADER_FLAG_RELOC) ||
3423 btrfs_header_backref_rev(eb) < BTRFS_MIXED_BACKREF_REV)
3426 ret = btrfs_lookup_extent_info(NULL, rc->extent_root,
3427 eb->start, btrfs_header_level(eb), 1,
3431 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
3438 static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
3439 struct inode *inode, u64 ino)
3441 struct btrfs_key key;
3442 struct btrfs_root *root = fs_info->tree_root;
3443 struct btrfs_trans_handle *trans;
3450 key.type = BTRFS_INODE_ITEM_KEY;
3453 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
3454 if (IS_ERR(inode) || is_bad_inode(inode)) {
3461 ret = btrfs_check_trunc_cache_free_space(root,
3462 &fs_info->global_block_rsv);
3466 trans = btrfs_join_transaction(root);
3467 if (IS_ERR(trans)) {
3468 ret = PTR_ERR(trans);
3472 ret = btrfs_truncate_free_space_cache(root, trans, inode);
3474 btrfs_end_transaction(trans, root);
3475 btrfs_btree_balance_dirty(root);
3482 * helper to add tree blocks for backref of type BTRFS_EXTENT_DATA_REF_KEY
3483 * this function scans fs tree to find blocks reference the data extent
3485 static int find_data_references(struct reloc_control *rc,
3486 struct btrfs_key *extent_key,
3487 struct extent_buffer *leaf,
3488 struct btrfs_extent_data_ref *ref,
3489 struct rb_root *blocks)
3491 struct btrfs_path *path;
3492 struct tree_block *block;
3493 struct btrfs_root *root;
3494 struct btrfs_file_extent_item *fi;
3495 struct rb_node *rb_node;
3496 struct btrfs_key key;
3507 ref_root = btrfs_extent_data_ref_root(leaf, ref);
3508 ref_objectid = btrfs_extent_data_ref_objectid(leaf, ref);
3509 ref_offset = btrfs_extent_data_ref_offset(leaf, ref);
3510 ref_count = btrfs_extent_data_ref_count(leaf, ref);
3513 * This is an extent belonging to the free space cache, lets just delete
3514 * it and redo the search.
3516 if (ref_root == BTRFS_ROOT_TREE_OBJECTID) {
3517 ret = delete_block_group_cache(rc->extent_root->fs_info,
3518 NULL, ref_objectid);
3524 path = btrfs_alloc_path();
3529 root = read_fs_root(rc->extent_root->fs_info, ref_root);
3531 err = PTR_ERR(root);
3535 key.objectid = ref_objectid;
3536 key.type = BTRFS_EXTENT_DATA_KEY;
3537 if (ref_offset > ((u64)-1 << 32))
3540 key.offset = ref_offset;
3542 path->search_commit_root = 1;
3543 path->skip_locking = 1;
3544 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3550 leaf = path->nodes[0];
3551 nritems = btrfs_header_nritems(leaf);
3553 * the references in tree blocks that use full backrefs
3554 * are not counted in
3556 if (block_use_full_backref(rc, leaf))
3560 rb_node = tree_search(blocks, leaf->start);
3565 path->slots[0] = nritems;
3568 while (ref_count > 0) {
3569 while (path->slots[0] >= nritems) {
3570 ret = btrfs_next_leaf(root, path);
3575 if (WARN_ON(ret > 0))
3578 leaf = path->nodes[0];
3579 nritems = btrfs_header_nritems(leaf);
3582 if (block_use_full_backref(rc, leaf))
3586 rb_node = tree_search(blocks, leaf->start);
3591 path->slots[0] = nritems;
3595 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3596 if (WARN_ON(key.objectid != ref_objectid ||
3597 key.type != BTRFS_EXTENT_DATA_KEY))
3600 fi = btrfs_item_ptr(leaf, path->slots[0],
3601 struct btrfs_file_extent_item);
3603 if (btrfs_file_extent_type(leaf, fi) ==
3604 BTRFS_FILE_EXTENT_INLINE)
3607 if (btrfs_file_extent_disk_bytenr(leaf, fi) !=
3608 extent_key->objectid)
3611 key.offset -= btrfs_file_extent_offset(leaf, fi);
3612 if (key.offset != ref_offset)
3620 if (!tree_block_processed(leaf->start, leaf->len, rc)) {
3621 block = kmalloc(sizeof(*block), GFP_NOFS);
3626 block->bytenr = leaf->start;
3627 btrfs_item_key_to_cpu(leaf, &block->key, 0);
3629 block->key_ready = 1;
3630 rb_node = tree_insert(blocks, block->bytenr,
3633 backref_tree_panic(rb_node, -EEXIST,
3639 path->slots[0] = nritems;
3645 btrfs_free_path(path);
3650 * helper to find all tree blocks that reference a given data extent
3652 static noinline_for_stack
3653 int add_data_references(struct reloc_control *rc,
3654 struct btrfs_key *extent_key,
3655 struct btrfs_path *path,
3656 struct rb_root *blocks)
3658 struct btrfs_key key;
3659 struct extent_buffer *eb;
3660 struct btrfs_extent_data_ref *dref;
3661 struct btrfs_extent_inline_ref *iref;
3664 u32 blocksize = btrfs_level_size(rc->extent_root, 0);
3668 eb = path->nodes[0];
3669 ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
3670 end = ptr + btrfs_item_size_nr(eb, path->slots[0]);
3671 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3672 if (ptr + sizeof(struct btrfs_extent_item_v0) == end)
3676 ptr += sizeof(struct btrfs_extent_item);
3679 iref = (struct btrfs_extent_inline_ref *)ptr;
3680 key.type = btrfs_extent_inline_ref_type(eb, iref);
3681 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3682 key.offset = btrfs_extent_inline_ref_offset(eb, iref);
3683 ret = __add_tree_block(rc, key.offset, blocksize,
3685 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3686 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
3687 ret = find_data_references(rc, extent_key,
3696 ptr += btrfs_extent_inline_ref_size(key.type);
3702 eb = path->nodes[0];
3703 if (path->slots[0] >= btrfs_header_nritems(eb)) {
3704 ret = btrfs_next_leaf(rc->extent_root, path);
3711 eb = path->nodes[0];
3714 btrfs_item_key_to_cpu(eb, &key, path->slots[0]);
3715 if (key.objectid != extent_key->objectid)
3718 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3719 if (key.type == BTRFS_SHARED_DATA_REF_KEY ||
3720 key.type == BTRFS_EXTENT_REF_V0_KEY) {
3722 BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
3723 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3725 ret = __add_tree_block(rc, key.offset, blocksize,
3727 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3728 dref = btrfs_item_ptr(eb, path->slots[0],
3729 struct btrfs_extent_data_ref);
3730 ret = find_data_references(rc, extent_key,
3742 btrfs_release_path(path);
3744 free_block_list(blocks);
3749 * helper to find next unprocessed extent
3751 static noinline_for_stack
3752 int find_next_extent(struct btrfs_trans_handle *trans,
3753 struct reloc_control *rc, struct btrfs_path *path,
3754 struct btrfs_key *extent_key)
3756 struct btrfs_key key;
3757 struct extent_buffer *leaf;
3758 u64 start, end, last;
3761 last = rc->block_group->key.objectid + rc->block_group->key.offset;
3764 if (rc->search_start >= last) {
3769 key.objectid = rc->search_start;
3770 key.type = BTRFS_EXTENT_ITEM_KEY;
3773 path->search_commit_root = 1;
3774 path->skip_locking = 1;
3775 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path,
3780 leaf = path->nodes[0];
3781 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3782 ret = btrfs_next_leaf(rc->extent_root, path);
3785 leaf = path->nodes[0];
3788 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3789 if (key.objectid >= last) {
3794 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3795 key.type != BTRFS_METADATA_ITEM_KEY) {
3800 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3801 key.objectid + key.offset <= rc->search_start) {
3806 if (key.type == BTRFS_METADATA_ITEM_KEY &&
3807 key.objectid + rc->extent_root->leafsize <=
3813 ret = find_first_extent_bit(&rc->processed_blocks,
3814 key.objectid, &start, &end,
3815 EXTENT_DIRTY, NULL);
3817 if (ret == 0 && start <= key.objectid) {
3818 btrfs_release_path(path);
3819 rc->search_start = end + 1;
3821 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3822 rc->search_start = key.objectid + key.offset;
3824 rc->search_start = key.objectid +
3825 rc->extent_root->leafsize;
3826 memcpy(extent_key, &key, sizeof(key));
3830 btrfs_release_path(path);
3834 static void set_reloc_control(struct reloc_control *rc)
3836 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3838 mutex_lock(&fs_info->reloc_mutex);
3839 fs_info->reloc_ctl = rc;
3840 mutex_unlock(&fs_info->reloc_mutex);
3843 static void unset_reloc_control(struct reloc_control *rc)
3845 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3847 mutex_lock(&fs_info->reloc_mutex);
3848 fs_info->reloc_ctl = NULL;
3849 mutex_unlock(&fs_info->reloc_mutex);
3852 static int check_extent_flags(u64 flags)
3854 if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
3855 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
3857 if (!(flags & BTRFS_EXTENT_FLAG_DATA) &&
3858 !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
3860 if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
3861 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
3866 static noinline_for_stack
3867 int prepare_to_relocate(struct reloc_control *rc)
3869 struct btrfs_trans_handle *trans;
3872 rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root,
3873 BTRFS_BLOCK_RSV_TEMP);
3878 * reserve some space for creating reloc trees.
3879 * btrfs_init_reloc_root will use them when there
3880 * is no reservation in transaction handle.
3882 ret = btrfs_block_rsv_add(rc->extent_root, rc->block_rsv,
3883 rc->extent_root->nodesize * 256,
3884 BTRFS_RESERVE_FLUSH_ALL);
3888 memset(&rc->cluster, 0, sizeof(rc->cluster));
3889 rc->search_start = rc->block_group->key.objectid;
3890 rc->extents_found = 0;
3891 rc->nodes_relocated = 0;
3892 rc->merging_rsv_size = 0;
3894 rc->create_reloc_tree = 1;
3895 set_reloc_control(rc);
3897 trans = btrfs_join_transaction(rc->extent_root);
3898 if (IS_ERR(trans)) {
3899 unset_reloc_control(rc);
3901 * extent tree is not a ref_cow tree and has no reloc_root to
3902 * cleanup. And callers are responsible to free the above
3905 return PTR_ERR(trans);
3907 btrfs_commit_transaction(trans, rc->extent_root);
3911 static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
3913 struct rb_root blocks = RB_ROOT;
3914 struct btrfs_key key;
3915 struct btrfs_trans_handle *trans = NULL;
3916 struct btrfs_path *path;
3917 struct btrfs_extent_item *ei;
3924 path = btrfs_alloc_path();
3929 ret = prepare_to_relocate(rc);
3937 trans = btrfs_start_transaction(rc->extent_root, 0);
3938 if (IS_ERR(trans)) {
3939 err = PTR_ERR(trans);
3944 if (update_backref_cache(trans, &rc->backref_cache)) {
3945 btrfs_end_transaction(trans, rc->extent_root);
3949 ret = find_next_extent(trans, rc, path, &key);
3955 rc->extents_found++;
3957 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3958 struct btrfs_extent_item);
3959 item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
3960 if (item_size >= sizeof(*ei)) {
3961 flags = btrfs_extent_flags(path->nodes[0], ei);
3962 ret = check_extent_flags(flags);
3966 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3968 int path_change = 0;
3971 sizeof(struct btrfs_extent_item_v0));
3972 ret = get_ref_objectid_v0(rc, path, &key, &ref_owner,
3974 if (ref_owner < BTRFS_FIRST_FREE_OBJECTID)
3975 flags = BTRFS_EXTENT_FLAG_TREE_BLOCK;
3977 flags = BTRFS_EXTENT_FLAG_DATA;
3980 btrfs_release_path(path);
3982 path->search_commit_root = 1;
3983 path->skip_locking = 1;
3984 ret = btrfs_search_slot(NULL, rc->extent_root,
3997 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
3998 ret = add_tree_block(rc, &key, path, &blocks);
3999 } else if (rc->stage == UPDATE_DATA_PTRS &&
4000 (flags & BTRFS_EXTENT_FLAG_DATA)) {
4001 ret = add_data_references(rc, &key, path, &blocks);
4003 btrfs_release_path(path);
4011 if (!RB_EMPTY_ROOT(&blocks)) {
4012 ret = relocate_tree_blocks(trans, rc, &blocks);
4014 if (ret != -EAGAIN) {
4018 rc->extents_found--;
4019 rc->search_start = key.objectid;
4023 if (rc->commit_transaction) {
4024 rc->commit_transaction = 0;
4025 ret = btrfs_commit_transaction(trans, rc->extent_root);
4028 btrfs_end_transaction_throttle(trans, rc->extent_root);
4029 btrfs_btree_balance_dirty(rc->extent_root);
4033 if (rc->stage == MOVE_DATA_EXTENTS &&
4034 (flags & BTRFS_EXTENT_FLAG_DATA)) {
4035 rc->found_file_extent = 1;
4036 ret = relocate_data_extent(rc->data_inode,
4037 &key, &rc->cluster);
4044 if (trans && progress && err == -ENOSPC) {
4045 ret = btrfs_force_chunk_alloc(trans, rc->extent_root,
4046 rc->block_group->flags);
4054 btrfs_release_path(path);
4055 clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY,
4059 btrfs_end_transaction_throttle(trans, rc->extent_root);
4060 btrfs_btree_balance_dirty(rc->extent_root);
4064 ret = relocate_file_extent_cluster(rc->data_inode,
4070 rc->create_reloc_tree = 0;
4071 set_reloc_control(rc);
4073 backref_cache_cleanup(&rc->backref_cache);
4074 btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1);
4076 err = prepare_to_merge(rc, err);
4078 merge_reloc_roots(rc);
4080 rc->merge_reloc_tree = 0;
4081 unset_reloc_control(rc);
4082 btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1);
4084 /* get rid of pinned extents */
4085 trans = btrfs_join_transaction(rc->extent_root);
4087 err = PTR_ERR(trans);
4089 btrfs_commit_transaction(trans, rc->extent_root);
4091 btrfs_free_block_rsv(rc->extent_root, rc->block_rsv);
4092 btrfs_free_path(path);
4096 static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
4097 struct btrfs_root *root, u64 objectid)
4099 struct btrfs_path *path;
4100 struct btrfs_inode_item *item;
4101 struct extent_buffer *leaf;
4104 path = btrfs_alloc_path();
4108 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
4112 leaf = path->nodes[0];
4113 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
4114 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
4115 btrfs_set_inode_generation(leaf, item, 1);
4116 btrfs_set_inode_size(leaf, item, 0);
4117 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
4118 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS |
4119 BTRFS_INODE_PREALLOC);
4120 btrfs_mark_buffer_dirty(leaf);
4121 btrfs_release_path(path);
4123 btrfs_free_path(path);
4128 * helper to create inode for data relocation.
4129 * the inode is in data relocation tree and its link count is 0
4131 static noinline_for_stack
4132 struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
4133 struct btrfs_block_group_cache *group)
4135 struct inode *inode = NULL;
4136 struct btrfs_trans_handle *trans;
4137 struct btrfs_root *root;
4138 struct btrfs_key key;
4139 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
4142 root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID);
4144 return ERR_CAST(root);
4146 trans = btrfs_start_transaction(root, 6);
4148 return ERR_CAST(trans);
4150 err = btrfs_find_free_objectid(root, &objectid);
4154 err = __insert_orphan_inode(trans, root, objectid);
4157 key.objectid = objectid;
4158 key.type = BTRFS_INODE_ITEM_KEY;
4160 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
4161 BUG_ON(IS_ERR(inode) || is_bad_inode(inode));
4162 BTRFS_I(inode)->index_cnt = group->key.objectid;
4164 err = btrfs_orphan_add(trans, inode);
4166 btrfs_end_transaction(trans, root);
4167 btrfs_btree_balance_dirty(root);
4171 inode = ERR_PTR(err);
4176 static struct reloc_control *alloc_reloc_control(struct btrfs_fs_info *fs_info)
4178 struct reloc_control *rc;
4180 rc = kzalloc(sizeof(*rc), GFP_NOFS);
4184 INIT_LIST_HEAD(&rc->reloc_roots);
4185 backref_cache_init(&rc->backref_cache);
4186 mapping_tree_init(&rc->reloc_root_tree);
4187 extent_io_tree_init(&rc->processed_blocks,
4188 fs_info->btree_inode->i_mapping);
4193 * function to relocate all extents in a block group.
4195 int btrfs_relocate_block_group(struct btrfs_root *extent_root, u64 group_start)
4197 struct btrfs_fs_info *fs_info = extent_root->fs_info;
4198 struct reloc_control *rc;
4199 struct inode *inode;
4200 struct btrfs_path *path;
4205 rc = alloc_reloc_control(fs_info);
4209 rc->extent_root = extent_root;
4211 rc->block_group = btrfs_lookup_block_group(fs_info, group_start);
4212 BUG_ON(!rc->block_group);
4214 if (!rc->block_group->ro) {
4215 ret = btrfs_set_block_group_ro(extent_root, rc->block_group);
4223 path = btrfs_alloc_path();
4229 inode = lookup_free_space_inode(fs_info->tree_root, rc->block_group,
4231 btrfs_free_path(path);
4234 ret = delete_block_group_cache(fs_info, inode, 0);
4236 ret = PTR_ERR(inode);
4238 if (ret && ret != -ENOENT) {
4243 rc->data_inode = create_reloc_inode(fs_info, rc->block_group);
4244 if (IS_ERR(rc->data_inode)) {
4245 err = PTR_ERR(rc->data_inode);
4246 rc->data_inode = NULL;
4250 printk(KERN_INFO "btrfs: relocating block group %llu flags %llu\n",
4251 rc->block_group->key.objectid, rc->block_group->flags);
4253 ret = btrfs_start_delalloc_roots(fs_info, 0);
4258 btrfs_wait_ordered_roots(fs_info, -1);
4261 mutex_lock(&fs_info->cleaner_mutex);
4262 ret = relocate_block_group(rc);
4263 mutex_unlock(&fs_info->cleaner_mutex);
4269 if (rc->extents_found == 0)
4272 printk(KERN_INFO "btrfs: found %llu extents\n",
4275 if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) {
4276 ret = btrfs_wait_ordered_range(rc->data_inode, 0,
4282 invalidate_mapping_pages(rc->data_inode->i_mapping,
4284 rc->stage = UPDATE_DATA_PTRS;
4288 filemap_write_and_wait_range(fs_info->btree_inode->i_mapping,
4289 rc->block_group->key.objectid,
4290 rc->block_group->key.objectid +
4291 rc->block_group->key.offset - 1);
4293 WARN_ON(rc->block_group->pinned > 0);
4294 WARN_ON(rc->block_group->reserved > 0);
4295 WARN_ON(btrfs_block_group_used(&rc->block_group->item) > 0);
4298 btrfs_set_block_group_rw(extent_root, rc->block_group);
4299 iput(rc->data_inode);
4300 btrfs_put_block_group(rc->block_group);
4305 static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
4307 struct btrfs_trans_handle *trans;
4310 trans = btrfs_start_transaction(root->fs_info->tree_root, 0);
4312 return PTR_ERR(trans);
4314 memset(&root->root_item.drop_progress, 0,
4315 sizeof(root->root_item.drop_progress));
4316 root->root_item.drop_level = 0;
4317 btrfs_set_root_refs(&root->root_item, 0);
4318 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4319 &root->root_key, &root->root_item);
4321 err = btrfs_end_transaction(trans, root->fs_info->tree_root);
4328 * recover relocation interrupted by system crash.
4330 * this function resumes merging reloc trees with corresponding fs trees.
4331 * this is important for keeping the sharing of tree blocks
4333 int btrfs_recover_relocation(struct btrfs_root *root)
4335 LIST_HEAD(reloc_roots);
4336 struct btrfs_key key;
4337 struct btrfs_root *fs_root;
4338 struct btrfs_root *reloc_root;
4339 struct btrfs_path *path;
4340 struct extent_buffer *leaf;
4341 struct reloc_control *rc = NULL;
4342 struct btrfs_trans_handle *trans;
4346 path = btrfs_alloc_path();
4351 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4352 key.type = BTRFS_ROOT_ITEM_KEY;
4353 key.offset = (u64)-1;
4356 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key,
4363 if (path->slots[0] == 0)
4367 leaf = path->nodes[0];
4368 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4369 btrfs_release_path(path);
4371 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID ||
4372 key.type != BTRFS_ROOT_ITEM_KEY)
4375 reloc_root = btrfs_read_fs_root(root, &key);
4376 if (IS_ERR(reloc_root)) {
4377 err = PTR_ERR(reloc_root);
4381 list_add(&reloc_root->root_list, &reloc_roots);
4383 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
4384 fs_root = read_fs_root(root->fs_info,
4385 reloc_root->root_key.offset);
4386 if (IS_ERR(fs_root)) {
4387 ret = PTR_ERR(fs_root);
4388 if (ret != -ENOENT) {
4392 ret = mark_garbage_root(reloc_root);
4400 if (key.offset == 0)
4405 btrfs_release_path(path);
4407 if (list_empty(&reloc_roots))
4410 rc = alloc_reloc_control(root->fs_info);
4416 rc->extent_root = root->fs_info->extent_root;
4418 set_reloc_control(rc);
4420 trans = btrfs_join_transaction(rc->extent_root);
4421 if (IS_ERR(trans)) {
4422 unset_reloc_control(rc);
4423 err = PTR_ERR(trans);
4427 rc->merge_reloc_tree = 1;
4429 while (!list_empty(&reloc_roots)) {
4430 reloc_root = list_entry(reloc_roots.next,
4431 struct btrfs_root, root_list);
4432 list_del(&reloc_root->root_list);
4434 if (btrfs_root_refs(&reloc_root->root_item) == 0) {
4435 list_add_tail(&reloc_root->root_list,
4440 fs_root = read_fs_root(root->fs_info,
4441 reloc_root->root_key.offset);
4442 if (IS_ERR(fs_root)) {
4443 err = PTR_ERR(fs_root);
4447 err = __add_reloc_root(reloc_root);
4448 BUG_ON(err < 0); /* -ENOMEM or logic error */
4449 fs_root->reloc_root = reloc_root;
4452 err = btrfs_commit_transaction(trans, rc->extent_root);
4456 merge_reloc_roots(rc);
4458 unset_reloc_control(rc);
4460 trans = btrfs_join_transaction(rc->extent_root);
4462 err = PTR_ERR(trans);
4464 err = btrfs_commit_transaction(trans, rc->extent_root);
4468 if (!list_empty(&reloc_roots))
4469 free_reloc_roots(&reloc_roots);
4471 btrfs_free_path(path);
4474 /* cleanup orphan inode in data relocation tree */
4475 fs_root = read_fs_root(root->fs_info,
4476 BTRFS_DATA_RELOC_TREE_OBJECTID);
4477 if (IS_ERR(fs_root))
4478 err = PTR_ERR(fs_root);
4480 err = btrfs_orphan_cleanup(fs_root);
4486 * helper to add ordered checksum for data relocation.
4488 * cloning checksum properly handles the nodatasum extents.
4489 * it also saves CPU time to re-calculate the checksum.
4491 int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
4493 struct btrfs_ordered_sum *sums;
4494 struct btrfs_ordered_extent *ordered;
4495 struct btrfs_root *root = BTRFS_I(inode)->root;
4501 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
4502 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
4504 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
4505 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
4506 disk_bytenr + len - 1, &list, 0);
4510 while (!list_empty(&list)) {
4511 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
4512 list_del_init(&sums->list);
4515 * We need to offset the new_bytenr based on where the csum is.
4516 * We need to do this because we will read in entire prealloc
4517 * extents but we may have written to say the middle of the
4518 * prealloc extent, so we need to make sure the csum goes with
4519 * the right disk offset.
4521 * We can do this because the data reloc inode refers strictly
4522 * to the on disk bytes, so we don't have to worry about
4523 * disk_len vs real len like with real inodes since it's all
4526 new_bytenr = ordered->start + (sums->bytenr - disk_bytenr);
4527 sums->bytenr = new_bytenr;
4529 btrfs_add_ordered_sum(inode, ordered, sums);
4532 btrfs_put_ordered_extent(ordered);
4536 int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
4537 struct btrfs_root *root, struct extent_buffer *buf,
4538 struct extent_buffer *cow)
4540 struct reloc_control *rc;
4541 struct backref_node *node;
4546 rc = root->fs_info->reloc_ctl;
4550 BUG_ON(rc->stage == UPDATE_DATA_PTRS &&
4551 root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID);
4553 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
4554 if (buf == root->node)
4555 __update_reloc_root(root, cow->start);
4558 level = btrfs_header_level(buf);
4559 if (btrfs_header_generation(buf) <=
4560 btrfs_root_last_snapshot(&root->root_item))
4563 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID &&
4564 rc->create_reloc_tree) {
4565 WARN_ON(!first_cow && level == 0);
4567 node = rc->backref_cache.path[level];
4568 BUG_ON(node->bytenr != buf->start &&
4569 node->new_bytenr != buf->start);
4571 drop_node_buffer(node);
4572 extent_buffer_get(cow);
4574 node->new_bytenr = cow->start;
4576 if (!node->pending) {
4577 list_move_tail(&node->list,
4578 &rc->backref_cache.pending[level]);
4583 __mark_block_processed(rc, node);
4585 if (first_cow && level > 0)
4586 rc->nodes_relocated += buf->len;
4589 if (level == 0 && first_cow && rc->stage == UPDATE_DATA_PTRS)
4590 ret = replace_file_extents(trans, rc, root, cow);
4595 * called before creating snapshot. it calculates metadata reservation
4596 * requried for relocating tree blocks in the snapshot
4598 void btrfs_reloc_pre_snapshot(struct btrfs_trans_handle *trans,
4599 struct btrfs_pending_snapshot *pending,
4600 u64 *bytes_to_reserve)
4602 struct btrfs_root *root;
4603 struct reloc_control *rc;
4605 root = pending->root;
4606 if (!root->reloc_root)
4609 rc = root->fs_info->reloc_ctl;
4610 if (!rc->merge_reloc_tree)
4613 root = root->reloc_root;
4614 BUG_ON(btrfs_root_refs(&root->root_item) == 0);
4616 * relocation is in the stage of merging trees. the space
4617 * used by merging a reloc tree is twice the size of
4618 * relocated tree nodes in the worst case. half for cowing
4619 * the reloc tree, half for cowing the fs tree. the space
4620 * used by cowing the reloc tree will be freed after the
4621 * tree is dropped. if we create snapshot, cowing the fs
4622 * tree may use more space than it frees. so we need
4623 * reserve extra space.
4625 *bytes_to_reserve += rc->nodes_relocated;
4629 * called after snapshot is created. migrate block reservation
4630 * and create reloc root for the newly created snapshot
4632 int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
4633 struct btrfs_pending_snapshot *pending)
4635 struct btrfs_root *root = pending->root;
4636 struct btrfs_root *reloc_root;
4637 struct btrfs_root *new_root;
4638 struct reloc_control *rc;
4641 if (!root->reloc_root)
4644 rc = root->fs_info->reloc_ctl;
4645 rc->merging_rsv_size += rc->nodes_relocated;
4647 if (rc->merge_reloc_tree) {
4648 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
4650 rc->nodes_relocated);
4655 new_root = pending->snap;
4656 reloc_root = create_reloc_root(trans, root->reloc_root,
4657 new_root->root_key.objectid);
4658 if (IS_ERR(reloc_root))
4659 return PTR_ERR(reloc_root);
4661 ret = __add_reloc_root(reloc_root);
4663 new_root->reloc_root = reloc_root;
4665 if (rc->create_reloc_tree)
4666 ret = clone_backref_node(trans, rc, root, reloc_root);