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>
26 #include "transaction.h"
29 #include "btrfs_inode.h"
30 #include "async-thread.h"
33 * backref_node, mapping_node and tree_block start with this
36 struct rb_node rb_node;
41 * present a tree block in the backref cache
44 struct rb_node rb_node;
46 /* objectid tree block owner */
48 /* list of upper level blocks reference this block */
49 struct list_head upper;
50 /* list of child blocks in the cache */
51 struct list_head lower;
52 /* NULL if this node is not tree root */
53 struct btrfs_root *root;
54 /* extent buffer got by COW the block */
55 struct extent_buffer *eb;
56 /* level of tree block */
58 /* 1 if the block is root of old snapshot */
59 unsigned int old_root:1;
60 /* 1 if no child blocks in the cache */
61 unsigned int lowest:1;
62 /* is the extent buffer locked */
63 unsigned int locked:1;
64 /* has the block been processed */
65 unsigned int processed:1;
66 /* have backrefs of this block been checked */
67 unsigned int checked:1;
71 * present a block pointer in the backref cache
74 struct list_head list[2];
75 struct backref_node *node[2];
82 struct backref_cache {
83 /* red black tree of all backref nodes in the cache */
84 struct rb_root rb_root;
85 /* list of backref nodes with no child block in the cache */
86 struct list_head pending[BTRFS_MAX_LEVEL];
91 * map address of tree root to tree
94 struct rb_node rb_node;
100 struct rb_root rb_root;
105 * present a tree block to process
108 struct rb_node rb_node;
110 struct btrfs_key key;
111 unsigned int level:8;
112 unsigned int key_ready:1;
116 #define INODEVEC_SIZE 16
119 struct list_head list;
120 struct inode *inode[INODEVEC_SIZE];
124 struct reloc_control {
125 /* block group to relocate */
126 struct btrfs_block_group_cache *block_group;
128 struct btrfs_root *extent_root;
129 /* inode for moving data */
130 struct inode *data_inode;
131 struct btrfs_workers workers;
132 /* tree blocks have been processed */
133 struct extent_io_tree processed_blocks;
134 /* map start of tree root to corresponding reloc tree */
135 struct mapping_tree reloc_root_tree;
136 /* list of reloc trees */
137 struct list_head reloc_roots;
142 int create_reloc_root;
143 unsigned int found_file_extent:1;
144 unsigned int found_old_snapshot:1;
147 /* stages of data relocation */
148 #define MOVE_DATA_EXTENTS 0
149 #define UPDATE_DATA_PTRS 1
152 * merge reloc tree to corresponding fs tree in worker threads
155 struct btrfs_work work;
156 struct reloc_control *rc;
157 struct btrfs_root *root;
158 struct completion *done;
159 atomic_t *num_pending;
162 static void mapping_tree_init(struct mapping_tree *tree)
164 tree->rb_root.rb_node = NULL;
165 spin_lock_init(&tree->lock);
168 static void backref_cache_init(struct backref_cache *cache)
171 cache->rb_root.rb_node = NULL;
172 for (i = 0; i < BTRFS_MAX_LEVEL; i++)
173 INIT_LIST_HEAD(&cache->pending[i]);
174 spin_lock_init(&cache->lock);
177 static void backref_node_init(struct backref_node *node)
179 memset(node, 0, sizeof(*node));
180 INIT_LIST_HEAD(&node->upper);
181 INIT_LIST_HEAD(&node->lower);
182 RB_CLEAR_NODE(&node->rb_node);
185 static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr,
186 struct rb_node *node)
188 struct rb_node **p = &root->rb_node;
189 struct rb_node *parent = NULL;
190 struct tree_entry *entry;
194 entry = rb_entry(parent, struct tree_entry, rb_node);
196 if (bytenr < entry->bytenr)
198 else if (bytenr > entry->bytenr)
204 rb_link_node(node, parent, p);
205 rb_insert_color(node, root);
209 static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
211 struct rb_node *n = root->rb_node;
212 struct tree_entry *entry;
215 entry = rb_entry(n, struct tree_entry, rb_node);
217 if (bytenr < entry->bytenr)
219 else if (bytenr > entry->bytenr)
228 * walk up backref nodes until reach node presents tree root
230 static struct backref_node *walk_up_backref(struct backref_node *node,
231 struct backref_edge *edges[],
234 struct backref_edge *edge;
237 while (!list_empty(&node->upper)) {
238 edge = list_entry(node->upper.next,
239 struct backref_edge, list[LOWER]);
241 node = edge->node[UPPER];
248 * walk down backref nodes to find start of next reference path
250 static struct backref_node *walk_down_backref(struct backref_edge *edges[],
253 struct backref_edge *edge;
254 struct backref_node *lower;
258 edge = edges[idx - 1];
259 lower = edge->node[LOWER];
260 if (list_is_last(&edge->list[LOWER], &lower->upper)) {
264 edge = list_entry(edge->list[LOWER].next,
265 struct backref_edge, list[LOWER]);
266 edges[idx - 1] = edge;
268 return edge->node[UPPER];
274 static void drop_node_buffer(struct backref_node *node)
278 btrfs_tree_unlock(node->eb);
281 free_extent_buffer(node->eb);
286 static void drop_backref_node(struct backref_cache *tree,
287 struct backref_node *node)
289 BUG_ON(!node->lowest);
290 BUG_ON(!list_empty(&node->upper));
292 drop_node_buffer(node);
293 list_del(&node->lower);
295 rb_erase(&node->rb_node, &tree->rb_root);
300 * remove a backref node from the backref cache
302 static void remove_backref_node(struct backref_cache *cache,
303 struct backref_node *node)
305 struct backref_node *upper;
306 struct backref_edge *edge;
311 BUG_ON(!node->lowest);
312 while (!list_empty(&node->upper)) {
313 edge = list_entry(node->upper.next, struct backref_edge,
315 upper = edge->node[UPPER];
316 list_del(&edge->list[LOWER]);
317 list_del(&edge->list[UPPER]);
320 * add the node to pending list if no other
321 * child block cached.
323 if (list_empty(&upper->lower)) {
324 list_add_tail(&upper->lower,
325 &cache->pending[upper->level]);
329 drop_backref_node(cache, node);
333 * find reloc tree by address of tree root
335 static struct btrfs_root *find_reloc_root(struct reloc_control *rc,
338 struct rb_node *rb_node;
339 struct mapping_node *node;
340 struct btrfs_root *root = NULL;
342 spin_lock(&rc->reloc_root_tree.lock);
343 rb_node = tree_search(&rc->reloc_root_tree.rb_root, bytenr);
345 node = rb_entry(rb_node, struct mapping_node, rb_node);
346 root = (struct btrfs_root *)node->data;
348 spin_unlock(&rc->reloc_root_tree.lock);
352 static int is_cowonly_root(u64 root_objectid)
354 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
355 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
356 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
357 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
358 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
359 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
364 static struct btrfs_root *read_fs_root(struct btrfs_fs_info *fs_info,
367 struct btrfs_key key;
369 key.objectid = root_objectid;
370 key.type = BTRFS_ROOT_ITEM_KEY;
371 if (is_cowonly_root(root_objectid))
374 key.offset = (u64)-1;
376 return btrfs_read_fs_root_no_name(fs_info, &key);
379 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
380 static noinline_for_stack
381 struct btrfs_root *find_tree_root(struct reloc_control *rc,
382 struct extent_buffer *leaf,
383 struct btrfs_extent_ref_v0 *ref0)
385 struct btrfs_root *root;
386 u64 root_objectid = btrfs_ref_root_v0(leaf, ref0);
387 u64 generation = btrfs_ref_generation_v0(leaf, ref0);
389 BUG_ON(root_objectid == BTRFS_TREE_RELOC_OBJECTID);
391 root = read_fs_root(rc->extent_root->fs_info, root_objectid);
392 BUG_ON(IS_ERR(root));
394 if (root->ref_cows &&
395 generation != btrfs_root_generation(&root->root_item))
402 static noinline_for_stack
403 int find_inline_backref(struct extent_buffer *leaf, int slot,
404 unsigned long *ptr, unsigned long *end)
406 struct btrfs_extent_item *ei;
407 struct btrfs_tree_block_info *bi;
410 item_size = btrfs_item_size_nr(leaf, slot);
411 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
412 if (item_size < sizeof(*ei)) {
413 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
417 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
418 WARN_ON(!(btrfs_extent_flags(leaf, ei) &
419 BTRFS_EXTENT_FLAG_TREE_BLOCK));
421 if (item_size <= sizeof(*ei) + sizeof(*bi)) {
422 WARN_ON(item_size < sizeof(*ei) + sizeof(*bi));
426 bi = (struct btrfs_tree_block_info *)(ei + 1);
427 *ptr = (unsigned long)(bi + 1);
428 *end = (unsigned long)ei + item_size;
433 * build backref tree for a given tree block. root of the backref tree
434 * corresponds the tree block, leaves of the backref tree correspond
435 * roots of b-trees that reference the tree block.
437 * the basic idea of this function is check backrefs of a given block
438 * to find upper level blocks that refernece the block, and then check
439 * bakcrefs of these upper level blocks recursively. the recursion stop
440 * when tree root is reached or backrefs for the block is cached.
442 * NOTE: if we find backrefs for a block are cached, we know backrefs
443 * for all upper level blocks that directly/indirectly reference the
444 * block are also cached.
446 static struct backref_node *build_backref_tree(struct reloc_control *rc,
447 struct backref_cache *cache,
448 struct btrfs_key *node_key,
449 int level, u64 bytenr)
451 struct btrfs_path *path1;
452 struct btrfs_path *path2;
453 struct extent_buffer *eb;
454 struct btrfs_root *root;
455 struct backref_node *cur;
456 struct backref_node *upper;
457 struct backref_node *lower;
458 struct backref_node *node = NULL;
459 struct backref_node *exist = NULL;
460 struct backref_edge *edge;
461 struct rb_node *rb_node;
462 struct btrfs_key key;
469 path1 = btrfs_alloc_path();
470 path2 = btrfs_alloc_path();
471 if (!path1 || !path2) {
476 node = kmalloc(sizeof(*node), GFP_NOFS);
482 backref_node_init(node);
483 node->bytenr = bytenr;
491 key.objectid = cur->bytenr;
492 key.type = BTRFS_EXTENT_ITEM_KEY;
493 key.offset = (u64)-1;
495 path1->search_commit_root = 1;
496 path1->skip_locking = 1;
497 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path1,
503 BUG_ON(!ret || !path1->slots[0]);
507 WARN_ON(cur->checked);
508 if (!list_empty(&cur->upper)) {
510 * the backref was added previously when processsing
511 * backref of type BTRFS_TREE_BLOCK_REF_KEY
513 BUG_ON(!list_is_singular(&cur->upper));
514 edge = list_entry(cur->upper.next, struct backref_edge,
516 BUG_ON(!list_empty(&edge->list[UPPER]));
517 exist = edge->node[UPPER];
519 * add the upper level block to pending list if we need
523 list_add_tail(&edge->list[UPPER], &list);
530 eb = path1->nodes[0];
533 if (path1->slots[0] >= btrfs_header_nritems(eb)) {
534 ret = btrfs_next_leaf(rc->extent_root, path1);
541 eb = path1->nodes[0];
544 btrfs_item_key_to_cpu(eb, &key, path1->slots[0]);
545 if (key.objectid != cur->bytenr) {
550 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
551 ret = find_inline_backref(eb, path1->slots[0],
559 /* update key for inline back ref */
560 struct btrfs_extent_inline_ref *iref;
561 iref = (struct btrfs_extent_inline_ref *)ptr;
562 key.type = btrfs_extent_inline_ref_type(eb, iref);
563 key.offset = btrfs_extent_inline_ref_offset(eb, iref);
564 WARN_ON(key.type != BTRFS_TREE_BLOCK_REF_KEY &&
565 key.type != BTRFS_SHARED_BLOCK_REF_KEY);
569 ((key.type == BTRFS_TREE_BLOCK_REF_KEY &&
570 exist->owner == key.offset) ||
571 (key.type == BTRFS_SHARED_BLOCK_REF_KEY &&
572 exist->bytenr == key.offset))) {
577 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
578 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY ||
579 key.type == BTRFS_EXTENT_REF_V0_KEY) {
580 if (key.objectid == key.offset &&
581 key.type == BTRFS_EXTENT_REF_V0_KEY) {
582 struct btrfs_extent_ref_v0 *ref0;
583 ref0 = btrfs_item_ptr(eb, path1->slots[0],
584 struct btrfs_extent_ref_v0);
585 root = find_tree_root(rc, eb, ref0);
593 BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
594 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
596 if (key.objectid == key.offset) {
598 * only root blocks of reloc trees use
599 * backref of this type.
601 root = find_reloc_root(rc, cur->bytenr);
607 edge = kzalloc(sizeof(*edge), GFP_NOFS);
612 rb_node = tree_search(&cache->rb_root, key.offset);
614 upper = kmalloc(sizeof(*upper), GFP_NOFS);
620 backref_node_init(upper);
621 upper->bytenr = key.offset;
623 upper->level = cur->level + 1;
625 * backrefs for the upper level block isn't
626 * cached, add the block to pending list
628 list_add_tail(&edge->list[UPPER], &list);
630 upper = rb_entry(rb_node, struct backref_node,
632 INIT_LIST_HEAD(&edge->list[UPPER]);
634 list_add(&edge->list[LOWER], &cur->upper);
635 edge->node[UPPER] = upper;
636 edge->node[LOWER] = cur;
639 } else if (key.type != BTRFS_TREE_BLOCK_REF_KEY) {
643 /* key.type == BTRFS_TREE_BLOCK_REF_KEY */
644 root = read_fs_root(rc->extent_root->fs_info, key.offset);
650 if (btrfs_root_level(&root->root_item) == cur->level) {
652 BUG_ON(btrfs_root_bytenr(&root->root_item) !=
658 level = cur->level + 1;
661 * searching the tree to find upper level blocks
662 * reference the block.
664 path2->search_commit_root = 1;
665 path2->skip_locking = 1;
666 path2->lowest_level = level;
667 ret = btrfs_search_slot(NULL, root, node_key, path2, 0, 0);
668 path2->lowest_level = 0;
674 eb = path2->nodes[level];
675 WARN_ON(btrfs_node_blockptr(eb, path2->slots[level]) !=
679 for (; level < BTRFS_MAX_LEVEL; level++) {
680 if (!path2->nodes[level]) {
681 BUG_ON(btrfs_root_bytenr(&root->root_item) !=
687 edge = kzalloc(sizeof(*edge), GFP_NOFS);
693 eb = path2->nodes[level];
694 rb_node = tree_search(&cache->rb_root, eb->start);
696 upper = kmalloc(sizeof(*upper), GFP_NOFS);
702 backref_node_init(upper);
703 upper->bytenr = eb->start;
704 upper->owner = btrfs_header_owner(eb);
705 upper->level = lower->level + 1;
708 * if we know the block isn't shared
709 * we can void checking its backrefs.
711 if (btrfs_block_can_be_shared(root, eb))
717 * add the block to pending list if we
718 * need check its backrefs. only block
719 * at 'cur->level + 1' is added to the
720 * tail of pending list. this guarantees
721 * we check backrefs from lower level
722 * blocks to upper level blocks.
724 if (!upper->checked &&
725 level == cur->level + 1) {
726 list_add_tail(&edge->list[UPPER],
729 INIT_LIST_HEAD(&edge->list[UPPER]);
731 upper = rb_entry(rb_node, struct backref_node,
733 BUG_ON(!upper->checked);
734 INIT_LIST_HEAD(&edge->list[UPPER]);
736 list_add_tail(&edge->list[LOWER], &lower->upper);
737 edge->node[UPPER] = upper;
738 edge->node[LOWER] = lower;
745 btrfs_release_path(root, path2);
748 ptr += btrfs_extent_inline_ref_size(key.type);
758 btrfs_release_path(rc->extent_root, path1);
763 /* the pending list isn't empty, take the first block to process */
764 if (!list_empty(&list)) {
765 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
766 list_del_init(&edge->list[UPPER]);
767 cur = edge->node[UPPER];
772 * everything goes well, connect backref nodes and insert backref nodes
775 BUG_ON(!node->checked);
776 rb_node = tree_insert(&cache->rb_root, node->bytenr, &node->rb_node);
779 list_for_each_entry(edge, &node->upper, list[LOWER])
780 list_add_tail(&edge->list[UPPER], &list);
782 while (!list_empty(&list)) {
783 edge = list_entry(list.next, struct backref_edge, list[UPPER]);
784 list_del_init(&edge->list[UPPER]);
785 upper = edge->node[UPPER];
787 if (!RB_EMPTY_NODE(&upper->rb_node)) {
789 list_del_init(&upper->lower);
793 list_add_tail(&edge->list[UPPER], &upper->lower);
797 BUG_ON(!upper->checked);
798 rb_node = tree_insert(&cache->rb_root, upper->bytenr,
802 list_add_tail(&edge->list[UPPER], &upper->lower);
804 list_for_each_entry(edge, &upper->upper, list[LOWER])
805 list_add_tail(&edge->list[UPPER], &list);
808 btrfs_free_path(path1);
809 btrfs_free_path(path2);
811 INIT_LIST_HEAD(&list);
814 if (RB_EMPTY_NODE(&upper->rb_node)) {
815 list_splice_tail(&upper->upper, &list);
819 if (list_empty(&list))
822 edge = list_entry(list.next, struct backref_edge,
824 upper = edge->node[UPPER];
833 * helper to add 'address of tree root -> reloc tree' mapping
835 static int __add_reloc_root(struct btrfs_root *root)
837 struct rb_node *rb_node;
838 struct mapping_node *node;
839 struct reloc_control *rc = root->fs_info->reloc_ctl;
841 node = kmalloc(sizeof(*node), GFP_NOFS);
844 node->bytenr = root->node->start;
847 spin_lock(&rc->reloc_root_tree.lock);
848 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
849 node->bytenr, &node->rb_node);
850 spin_unlock(&rc->reloc_root_tree.lock);
853 list_add_tail(&root->root_list, &rc->reloc_roots);
858 * helper to update/delete the 'address of tree root -> reloc tree'
861 static int __update_reloc_root(struct btrfs_root *root, int del)
863 struct rb_node *rb_node;
864 struct mapping_node *node = NULL;
865 struct reloc_control *rc = root->fs_info->reloc_ctl;
867 spin_lock(&rc->reloc_root_tree.lock);
868 rb_node = tree_search(&rc->reloc_root_tree.rb_root,
869 root->commit_root->start);
871 node = rb_entry(rb_node, struct mapping_node, rb_node);
872 rb_erase(&node->rb_node, &rc->reloc_root_tree.rb_root);
874 spin_unlock(&rc->reloc_root_tree.lock);
876 BUG_ON((struct btrfs_root *)node->data != root);
879 spin_lock(&rc->reloc_root_tree.lock);
880 node->bytenr = root->node->start;
881 rb_node = tree_insert(&rc->reloc_root_tree.rb_root,
882 node->bytenr, &node->rb_node);
883 spin_unlock(&rc->reloc_root_tree.lock);
886 list_del_init(&root->root_list);
893 * create reloc tree for a given fs tree. reloc tree is just a
894 * snapshot of the fs tree with special root objectid.
896 int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
897 struct btrfs_root *root)
899 struct btrfs_root *reloc_root;
900 struct extent_buffer *eb;
901 struct btrfs_root_item *root_item;
902 struct btrfs_key root_key;
905 if (root->reloc_root) {
906 reloc_root = root->reloc_root;
907 reloc_root->last_trans = trans->transid;
911 if (!root->fs_info->reloc_ctl ||
912 !root->fs_info->reloc_ctl->create_reloc_root ||
913 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
916 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
919 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
920 root_key.type = BTRFS_ROOT_ITEM_KEY;
921 root_key.offset = root->root_key.objectid;
923 ret = btrfs_copy_root(trans, root, root->commit_root, &eb,
924 BTRFS_TREE_RELOC_OBJECTID);
927 btrfs_set_root_last_snapshot(&root->root_item, trans->transid - 1);
928 memcpy(root_item, &root->root_item, sizeof(*root_item));
929 btrfs_set_root_refs(root_item, 1);
930 btrfs_set_root_bytenr(root_item, eb->start);
931 btrfs_set_root_level(root_item, btrfs_header_level(eb));
932 btrfs_set_root_generation(root_item, trans->transid);
933 memset(&root_item->drop_progress, 0, sizeof(struct btrfs_disk_key));
934 root_item->drop_level = 0;
936 btrfs_tree_unlock(eb);
937 free_extent_buffer(eb);
939 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
940 &root_key, root_item);
944 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
946 BUG_ON(IS_ERR(reloc_root));
947 reloc_root->last_trans = trans->transid;
949 __add_reloc_root(reloc_root);
950 root->reloc_root = reloc_root;
955 * update root item of reloc tree
957 int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
958 struct btrfs_root *root)
960 struct btrfs_root *reloc_root;
961 struct btrfs_root_item *root_item;
965 if (!root->reloc_root)
968 reloc_root = root->reloc_root;
969 root_item = &reloc_root->root_item;
971 if (btrfs_root_refs(root_item) == 0) {
972 root->reloc_root = NULL;
976 __update_reloc_root(reloc_root, del);
978 if (reloc_root->commit_root != reloc_root->node) {
979 btrfs_set_root_node(root_item, reloc_root->node);
980 free_extent_buffer(reloc_root->commit_root);
981 reloc_root->commit_root = btrfs_root_node(reloc_root);
984 ret = btrfs_update_root(trans, root->fs_info->tree_root,
985 &reloc_root->root_key, root_item);
991 * helper to find first cached inode with inode number >= objectid
994 static struct inode *find_next_inode(struct btrfs_root *root, u64 objectid)
996 struct rb_node *node;
997 struct rb_node *prev;
998 struct btrfs_inode *entry;
1001 spin_lock(&root->inode_lock);
1003 node = root->inode_tree.rb_node;
1007 entry = rb_entry(node, struct btrfs_inode, rb_node);
1009 if (objectid < entry->vfs_inode.i_ino)
1010 node = node->rb_left;
1011 else if (objectid > entry->vfs_inode.i_ino)
1012 node = node->rb_right;
1018 entry = rb_entry(prev, struct btrfs_inode, rb_node);
1019 if (objectid <= entry->vfs_inode.i_ino) {
1023 prev = rb_next(prev);
1027 entry = rb_entry(node, struct btrfs_inode, rb_node);
1028 inode = igrab(&entry->vfs_inode);
1030 spin_unlock(&root->inode_lock);
1034 objectid = entry->vfs_inode.i_ino + 1;
1035 if (cond_resched_lock(&root->inode_lock))
1038 node = rb_next(node);
1040 spin_unlock(&root->inode_lock);
1044 static int in_block_group(u64 bytenr,
1045 struct btrfs_block_group_cache *block_group)
1047 if (bytenr >= block_group->key.objectid &&
1048 bytenr < block_group->key.objectid + block_group->key.offset)
1054 * get new location of data
1056 static int get_new_location(struct inode *reloc_inode, u64 *new_bytenr,
1057 u64 bytenr, u64 num_bytes)
1059 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
1060 struct btrfs_path *path;
1061 struct btrfs_file_extent_item *fi;
1062 struct extent_buffer *leaf;
1065 path = btrfs_alloc_path();
1069 bytenr -= BTRFS_I(reloc_inode)->index_cnt;
1070 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
1079 leaf = path->nodes[0];
1080 fi = btrfs_item_ptr(leaf, path->slots[0],
1081 struct btrfs_file_extent_item);
1083 BUG_ON(btrfs_file_extent_offset(leaf, fi) ||
1084 btrfs_file_extent_compression(leaf, fi) ||
1085 btrfs_file_extent_encryption(leaf, fi) ||
1086 btrfs_file_extent_other_encoding(leaf, fi));
1088 if (num_bytes != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
1094 *new_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1097 btrfs_free_path(path);
1102 * update file extent items in the tree leaf to point to
1103 * the new locations.
1105 static int replace_file_extents(struct btrfs_trans_handle *trans,
1106 struct reloc_control *rc,
1107 struct btrfs_root *root,
1108 struct extent_buffer *leaf,
1109 struct list_head *inode_list)
1111 struct btrfs_key key;
1112 struct btrfs_file_extent_item *fi;
1113 struct inode *inode = NULL;
1114 struct inodevec *ivec = NULL;
1126 if (rc->stage != UPDATE_DATA_PTRS)
1129 /* reloc trees always use full backref */
1130 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1131 parent = leaf->start;
1135 nritems = btrfs_header_nritems(leaf);
1136 for (i = 0; i < nritems; i++) {
1138 btrfs_item_key_to_cpu(leaf, &key, i);
1139 if (key.type != BTRFS_EXTENT_DATA_KEY)
1141 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1142 if (btrfs_file_extent_type(leaf, fi) ==
1143 BTRFS_FILE_EXTENT_INLINE)
1145 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1146 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
1149 if (!in_block_group(bytenr, rc->block_group))
1153 * if we are modifying block in fs tree, wait for readpage
1154 * to complete and drop the extent cache
1156 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1157 if (!ivec || ivec->nr == INODEVEC_SIZE) {
1158 ivec = kmalloc(sizeof(*ivec), GFP_NOFS);
1161 list_add_tail(&ivec->list, inode_list);
1164 inode = find_next_inode(root, key.objectid);
1166 ivec->inode[ivec->nr++] = inode;
1168 } else if (inode && inode->i_ino < key.objectid) {
1169 inode = find_next_inode(root, key.objectid);
1171 ivec->inode[ivec->nr++] = inode;
1173 if (inode && inode->i_ino == key.objectid) {
1175 btrfs_file_extent_num_bytes(leaf, fi);
1176 WARN_ON(!IS_ALIGNED(key.offset,
1178 WARN_ON(!IS_ALIGNED(end, root->sectorsize));
1180 ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
1186 btrfs_drop_extent_cache(inode, key.offset, end,
1188 unlock_extent(&BTRFS_I(inode)->io_tree,
1189 key.offset, end, GFP_NOFS);
1193 ret = get_new_location(rc->data_inode, &new_bytenr,
1199 btrfs_set_file_extent_disk_bytenr(leaf, fi, new_bytenr);
1202 key.offset -= btrfs_file_extent_offset(leaf, fi);
1203 ret = btrfs_inc_extent_ref(trans, root, new_bytenr,
1205 btrfs_header_owner(leaf),
1206 key.objectid, key.offset);
1209 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1210 parent, btrfs_header_owner(leaf),
1211 key.objectid, key.offset);
1215 btrfs_mark_buffer_dirty(leaf);
1219 static noinline_for_stack
1220 int memcmp_node_keys(struct extent_buffer *eb, int slot,
1221 struct btrfs_path *path, int level)
1223 struct btrfs_disk_key key1;
1224 struct btrfs_disk_key key2;
1225 btrfs_node_key(eb, &key1, slot);
1226 btrfs_node_key(path->nodes[level], &key2, path->slots[level]);
1227 return memcmp(&key1, &key2, sizeof(key1));
1231 * try to replace tree blocks in fs tree with the new blocks
1232 * in reloc tree. tree blocks haven't been modified since the
1233 * reloc tree was create can be replaced.
1235 * if a block was replaced, level of the block + 1 is returned.
1236 * if no block got replaced, 0 is returned. if there are other
1237 * errors, a negative error number is returned.
1239 static int replace_path(struct btrfs_trans_handle *trans,
1240 struct btrfs_root *dest, struct btrfs_root *src,
1241 struct btrfs_path *path, struct btrfs_key *next_key,
1242 struct extent_buffer **leaf,
1243 int lowest_level, int max_level)
1245 struct extent_buffer *eb;
1246 struct extent_buffer *parent;
1247 struct btrfs_key key;
1258 BUG_ON(src->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
1259 BUG_ON(dest->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID);
1260 BUG_ON(lowest_level > 1 && leaf);
1262 last_snapshot = btrfs_root_last_snapshot(&src->root_item);
1264 slot = path->slots[lowest_level];
1265 btrfs_node_key_to_cpu(path->nodes[lowest_level], &key, slot);
1267 eb = btrfs_lock_root_node(dest);
1268 btrfs_set_lock_blocking(eb);
1269 level = btrfs_header_level(eb);
1271 if (level < lowest_level) {
1272 btrfs_tree_unlock(eb);
1273 free_extent_buffer(eb);
1277 ret = btrfs_cow_block(trans, dest, eb, NULL, 0, &eb);
1279 btrfs_set_lock_blocking(eb);
1282 next_key->objectid = (u64)-1;
1283 next_key->type = (u8)-1;
1284 next_key->offset = (u64)-1;
1289 level = btrfs_header_level(parent);
1290 BUG_ON(level < lowest_level);
1292 ret = btrfs_bin_search(parent, &key, level, &slot);
1293 if (ret && slot > 0)
1296 if (next_key && slot + 1 < btrfs_header_nritems(parent))
1297 btrfs_node_key_to_cpu(parent, next_key, slot + 1);
1299 old_bytenr = btrfs_node_blockptr(parent, slot);
1300 blocksize = btrfs_level_size(dest, level - 1);
1301 old_ptr_gen = btrfs_node_ptr_generation(parent, slot);
1303 if (level <= max_level) {
1304 eb = path->nodes[level];
1305 new_bytenr = btrfs_node_blockptr(eb,
1306 path->slots[level]);
1307 new_ptr_gen = btrfs_node_ptr_generation(eb,
1308 path->slots[level]);
1314 if (new_bytenr > 0 && new_bytenr == old_bytenr) {
1320 if (new_bytenr == 0 || old_ptr_gen > last_snapshot ||
1321 memcmp_node_keys(parent, slot, path, level)) {
1322 if (level <= lowest_level && !leaf) {
1327 eb = read_tree_block(dest, old_bytenr, blocksize,
1329 btrfs_tree_lock(eb);
1330 ret = btrfs_cow_block(trans, dest, eb, parent,
1333 btrfs_set_lock_blocking(eb);
1335 if (level <= lowest_level) {
1341 btrfs_tree_unlock(parent);
1342 free_extent_buffer(parent);
1348 btrfs_node_key_to_cpu(path->nodes[level], &key,
1349 path->slots[level]);
1350 btrfs_release_path(src, path);
1352 path->lowest_level = level;
1353 ret = btrfs_search_slot(trans, src, &key, path, 0, 1);
1354 path->lowest_level = 0;
1358 * swap blocks in fs tree and reloc tree.
1360 btrfs_set_node_blockptr(parent, slot, new_bytenr);
1361 btrfs_set_node_ptr_generation(parent, slot, new_ptr_gen);
1362 btrfs_mark_buffer_dirty(parent);
1364 btrfs_set_node_blockptr(path->nodes[level],
1365 path->slots[level], old_bytenr);
1366 btrfs_set_node_ptr_generation(path->nodes[level],
1367 path->slots[level], old_ptr_gen);
1368 btrfs_mark_buffer_dirty(path->nodes[level]);
1370 ret = btrfs_inc_extent_ref(trans, src, old_bytenr, blocksize,
1371 path->nodes[level]->start,
1372 src->root_key.objectid, level - 1, 0);
1374 ret = btrfs_inc_extent_ref(trans, dest, new_bytenr, blocksize,
1375 0, dest->root_key.objectid, level - 1,
1379 ret = btrfs_free_extent(trans, src, new_bytenr, blocksize,
1380 path->nodes[level]->start,
1381 src->root_key.objectid, level - 1, 0);
1384 ret = btrfs_free_extent(trans, dest, old_bytenr, blocksize,
1385 0, dest->root_key.objectid, level - 1,
1389 btrfs_unlock_up_safe(path, 0);
1394 btrfs_tree_unlock(parent);
1395 free_extent_buffer(parent);
1400 * helper to find next relocated block in reloc tree
1402 static noinline_for_stack
1403 int walk_up_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1406 struct extent_buffer *eb;
1411 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1413 for (i = 0; i < *level; i++) {
1414 free_extent_buffer(path->nodes[i]);
1415 path->nodes[i] = NULL;
1418 for (i = *level; i < BTRFS_MAX_LEVEL && path->nodes[i]; i++) {
1419 eb = path->nodes[i];
1420 nritems = btrfs_header_nritems(eb);
1421 while (path->slots[i] + 1 < nritems) {
1423 if (btrfs_node_ptr_generation(eb, path->slots[i]) <=
1430 free_extent_buffer(path->nodes[i]);
1431 path->nodes[i] = NULL;
1437 * walk down reloc tree to find relocated block of lowest level
1439 static noinline_for_stack
1440 int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
1443 struct extent_buffer *eb = NULL;
1451 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1453 for (i = *level; i > 0; i--) {
1454 eb = path->nodes[i];
1455 nritems = btrfs_header_nritems(eb);
1456 while (path->slots[i] < nritems) {
1457 ptr_gen = btrfs_node_ptr_generation(eb, path->slots[i]);
1458 if (ptr_gen > last_snapshot)
1462 if (path->slots[i] >= nritems) {
1473 bytenr = btrfs_node_blockptr(eb, path->slots[i]);
1474 blocksize = btrfs_level_size(root, i - 1);
1475 eb = read_tree_block(root, bytenr, blocksize, ptr_gen);
1476 BUG_ON(btrfs_header_level(eb) != i - 1);
1477 path->nodes[i - 1] = eb;
1478 path->slots[i - 1] = 0;
1484 * invalidate extent cache for file extents whose key in range of
1485 * [min_key, max_key)
1487 static int invalidate_extent_cache(struct btrfs_root *root,
1488 struct btrfs_key *min_key,
1489 struct btrfs_key *max_key)
1491 struct inode *inode = NULL;
1495 objectid = min_key->objectid;
1500 if (objectid > max_key->objectid)
1503 inode = find_next_inode(root, objectid);
1507 if (inode->i_ino > max_key->objectid) {
1512 objectid = inode->i_ino + 1;
1513 if (!S_ISREG(inode->i_mode))
1516 if (unlikely(min_key->objectid == inode->i_ino)) {
1517 if (min_key->type > BTRFS_EXTENT_DATA_KEY)
1519 if (min_key->type < BTRFS_EXTENT_DATA_KEY)
1522 start = min_key->offset;
1523 WARN_ON(!IS_ALIGNED(start, root->sectorsize));
1529 if (unlikely(max_key->objectid == inode->i_ino)) {
1530 if (max_key->type < BTRFS_EXTENT_DATA_KEY)
1532 if (max_key->type > BTRFS_EXTENT_DATA_KEY) {
1535 if (max_key->offset == 0)
1537 end = max_key->offset;
1538 WARN_ON(!IS_ALIGNED(end, root->sectorsize));
1545 /* the lock_extent waits for readpage to complete */
1546 lock_extent(&BTRFS_I(inode)->io_tree, start, end, GFP_NOFS);
1547 btrfs_drop_extent_cache(inode, start, end, 1);
1548 unlock_extent(&BTRFS_I(inode)->io_tree, start, end, GFP_NOFS);
1553 static int find_next_key(struct btrfs_path *path, int level,
1554 struct btrfs_key *key)
1557 while (level < BTRFS_MAX_LEVEL) {
1558 if (!path->nodes[level])
1560 if (path->slots[level] + 1 <
1561 btrfs_header_nritems(path->nodes[level])) {
1562 btrfs_node_key_to_cpu(path->nodes[level], key,
1563 path->slots[level] + 1);
1572 * merge the relocated tree blocks in reloc tree with corresponding
1575 static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
1576 struct btrfs_root *root)
1578 LIST_HEAD(inode_list);
1579 struct btrfs_key key;
1580 struct btrfs_key next_key;
1581 struct btrfs_trans_handle *trans;
1582 struct btrfs_root *reloc_root;
1583 struct btrfs_root_item *root_item;
1584 struct btrfs_path *path;
1585 struct extent_buffer *leaf = NULL;
1593 path = btrfs_alloc_path();
1597 reloc_root = root->reloc_root;
1598 root_item = &reloc_root->root_item;
1600 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1601 level = btrfs_root_level(root_item);
1602 extent_buffer_get(reloc_root->node);
1603 path->nodes[level] = reloc_root->node;
1604 path->slots[level] = 0;
1606 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1608 level = root_item->drop_level;
1610 path->lowest_level = level;
1611 ret = btrfs_search_slot(NULL, reloc_root, &key, path, 0, 0);
1613 btrfs_free_path(path);
1617 btrfs_node_key_to_cpu(path->nodes[level], &next_key,
1618 path->slots[level]);
1619 WARN_ON(memcmp(&key, &next_key, sizeof(key)));
1621 btrfs_unlock_up_safe(path, 0);
1624 if (level == 0 && rc->stage == UPDATE_DATA_PTRS) {
1625 trans = btrfs_start_transaction(root, 1);
1627 leaf = path->nodes[0];
1628 btrfs_item_key_to_cpu(leaf, &key, 0);
1629 btrfs_release_path(reloc_root, path);
1631 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1637 leaf = path->nodes[0];
1638 btrfs_unlock_up_safe(path, 1);
1639 ret = replace_file_extents(trans, rc, root, leaf,
1646 memset(&next_key, 0, sizeof(next_key));
1651 trans = btrfs_start_transaction(root, 1);
1654 ret = walk_down_reloc_tree(reloc_root, path, &level);
1662 if (!find_next_key(path, level, &key) &&
1663 btrfs_comp_cpu_keys(&next_key, &key) >= 0) {
1665 } else if (level == 1 && rc->stage == UPDATE_DATA_PTRS) {
1666 ret = replace_path(trans, root, reloc_root,
1667 path, &next_key, &leaf,
1670 ret = replace_path(trans, root, reloc_root,
1671 path, &next_key, NULL,
1681 btrfs_node_key_to_cpu(path->nodes[level], &key,
1682 path->slots[level]);
1686 * no block got replaced, try replacing file extents
1688 btrfs_item_key_to_cpu(leaf, &key, 0);
1689 ret = replace_file_extents(trans, rc, root, leaf,
1691 btrfs_tree_unlock(leaf);
1692 free_extent_buffer(leaf);
1696 ret = walk_up_reloc_tree(reloc_root, path, &level);
1702 * save the merging progress in the drop_progress.
1703 * this is OK since root refs == 1 in this case.
1705 btrfs_node_key(path->nodes[level], &root_item->drop_progress,
1706 path->slots[level]);
1707 root_item->drop_level = level;
1709 nr = trans->blocks_used;
1710 btrfs_end_transaction(trans, root);
1712 btrfs_btree_balance_dirty(root, nr);
1714 if (replaced && rc->stage == UPDATE_DATA_PTRS)
1715 invalidate_extent_cache(root, &key, &next_key);
1719 * handle the case only one block in the fs tree need to be
1720 * relocated and the block is tree root.
1722 leaf = btrfs_lock_root_node(root);
1723 ret = btrfs_cow_block(trans, root, leaf, NULL, 0, &leaf);
1724 btrfs_tree_unlock(leaf);
1725 free_extent_buffer(leaf);
1729 btrfs_free_path(path);
1732 memset(&root_item->drop_progress, 0,
1733 sizeof(root_item->drop_progress));
1734 root_item->drop_level = 0;
1735 btrfs_set_root_refs(root_item, 0);
1738 nr = trans->blocks_used;
1739 btrfs_end_transaction(trans, root);
1741 btrfs_btree_balance_dirty(root, nr);
1744 * put inodes while we aren't holding the tree locks
1746 while (!list_empty(&inode_list)) {
1747 struct inodevec *ivec;
1748 ivec = list_entry(inode_list.next, struct inodevec, list);
1749 list_del(&ivec->list);
1750 while (ivec->nr > 0) {
1752 iput(ivec->inode[ivec->nr]);
1757 if (replaced && rc->stage == UPDATE_DATA_PTRS)
1758 invalidate_extent_cache(root, &key, &next_key);
1764 * callback for the work threads.
1765 * this function merges reloc tree with corresponding fs tree,
1766 * and then drops the reloc tree.
1768 static void merge_func(struct btrfs_work *work)
1770 struct btrfs_trans_handle *trans;
1771 struct btrfs_root *root;
1772 struct btrfs_root *reloc_root;
1773 struct async_merge *async;
1775 async = container_of(work, struct async_merge, work);
1776 reloc_root = async->root;
1778 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
1779 root = read_fs_root(reloc_root->fs_info,
1780 reloc_root->root_key.offset);
1781 BUG_ON(IS_ERR(root));
1782 BUG_ON(root->reloc_root != reloc_root);
1784 merge_reloc_root(async->rc, root);
1786 trans = btrfs_start_transaction(root, 1);
1787 btrfs_update_reloc_root(trans, root);
1788 btrfs_end_transaction(trans, root);
1791 btrfs_drop_dead_root(reloc_root);
1793 if (atomic_dec_and_test(async->num_pending))
1794 complete(async->done);
1799 static int merge_reloc_roots(struct reloc_control *rc)
1801 struct async_merge *async;
1802 struct btrfs_root *root;
1803 struct completion done;
1804 atomic_t num_pending;
1806 init_completion(&done);
1807 atomic_set(&num_pending, 1);
1809 while (!list_empty(&rc->reloc_roots)) {
1810 root = list_entry(rc->reloc_roots.next,
1811 struct btrfs_root, root_list);
1812 list_del_init(&root->root_list);
1814 async = kmalloc(sizeof(*async), GFP_NOFS);
1816 async->work.func = merge_func;
1817 async->work.flags = 0;
1820 async->done = &done;
1821 async->num_pending = &num_pending;
1822 atomic_inc(&num_pending);
1823 btrfs_queue_worker(&rc->workers, &async->work);
1826 if (!atomic_dec_and_test(&num_pending))
1827 wait_for_completion(&done);
1829 BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
1833 static void free_block_list(struct rb_root *blocks)
1835 struct tree_block *block;
1836 struct rb_node *rb_node;
1837 while ((rb_node = rb_first(blocks))) {
1838 block = rb_entry(rb_node, struct tree_block, rb_node);
1839 rb_erase(rb_node, blocks);
1844 static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans,
1845 struct btrfs_root *reloc_root)
1847 struct btrfs_root *root;
1849 if (reloc_root->last_trans == trans->transid)
1852 root = read_fs_root(reloc_root->fs_info, reloc_root->root_key.offset);
1853 BUG_ON(IS_ERR(root));
1854 BUG_ON(root->reloc_root != reloc_root);
1856 return btrfs_record_root_in_trans(trans, root);
1860 * select one tree from trees that references the block.
1861 * for blocks in refernce counted trees, we preper reloc tree.
1862 * if no reloc tree found and reloc_only is true, NULL is returned.
1864 static struct btrfs_root *__select_one_root(struct btrfs_trans_handle *trans,
1865 struct backref_node *node,
1866 struct backref_edge *edges[],
1867 int *nr, int reloc_only)
1869 struct backref_node *next;
1870 struct btrfs_root *root;
1878 next = walk_up_backref(next, edges, &index);
1881 BUG_ON(!node->old_root);
1885 /* no other choice for non-refernce counted tree */
1886 if (!root->ref_cows) {
1891 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
1892 record_reloc_root_in_trans(trans, root);
1897 btrfs_record_root_in_trans(trans, root);
1901 if (reloc_only || next != node) {
1902 if (!root->reloc_root)
1903 btrfs_record_root_in_trans(trans, root);
1904 root = root->reloc_root;
1906 * if the reloc tree was created in current
1907 * transation, there is no node in backref tree
1908 * corresponds to the root of the reloc tree.
1910 if (btrfs_root_last_snapshot(&root->root_item) ==
1916 next = walk_down_backref(edges, &index);
1917 if (!next || next->level <= node->level)
1921 if (!root && !loop && !reloc_only) {
1934 static noinline_for_stack
1935 struct btrfs_root *select_one_root(struct btrfs_trans_handle *trans,
1936 struct backref_node *node)
1938 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
1940 return __select_one_root(trans, node, edges, &nr, 0);
1943 static noinline_for_stack
1944 struct btrfs_root *select_reloc_root(struct btrfs_trans_handle *trans,
1945 struct backref_node *node,
1946 struct backref_edge *edges[], int *nr)
1948 return __select_one_root(trans, node, edges, nr, 1);
1951 static void grab_path_buffers(struct btrfs_path *path,
1952 struct backref_node *node,
1953 struct backref_edge *edges[], int nr)
1957 drop_node_buffer(node);
1958 node->eb = path->nodes[node->level];
1960 if (path->locks[node->level])
1962 path->nodes[node->level] = NULL;
1963 path->locks[node->level] = 0;
1968 edges[i]->blockptr = node->eb->start;
1969 node = edges[i]->node[UPPER];
1975 * relocate a block tree, and then update pointers in upper level
1976 * blocks that reference the block to point to the new location.
1978 * if called by link_to_upper, the block has already been relocated.
1979 * in that case this function just updates pointers.
1981 static int do_relocation(struct btrfs_trans_handle *trans,
1982 struct backref_node *node,
1983 struct btrfs_key *key,
1984 struct btrfs_path *path, int lowest)
1986 struct backref_node *upper;
1987 struct backref_edge *edge;
1988 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
1989 struct btrfs_root *root;
1990 struct extent_buffer *eb;
1999 BUG_ON(lowest && node->eb);
2001 path->lowest_level = node->level + 1;
2002 list_for_each_entry(edge, &node->upper, list[LOWER]) {
2004 if (node->eb && node->eb->start == edge->blockptr)
2007 upper = edge->node[UPPER];
2008 root = select_reloc_root(trans, upper, edges, &nr);
2012 if (upper->eb && !upper->locked)
2013 drop_node_buffer(upper);
2016 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2023 slot = path->slots[upper->level];
2025 btrfs_unlock_up_safe(path, upper->level + 1);
2026 grab_path_buffers(path, upper, edges, nr);
2028 btrfs_release_path(NULL, path);
2030 ret = btrfs_bin_search(upper->eb, key, upper->level,
2035 bytenr = btrfs_node_blockptr(upper->eb, slot);
2037 if (node->eb->start == bytenr) {
2038 btrfs_tree_unlock(upper->eb);
2043 BUG_ON(node->bytenr != bytenr);
2046 blocksize = btrfs_level_size(root, node->level);
2047 generation = btrfs_node_ptr_generation(upper->eb, slot);
2048 eb = read_tree_block(root, bytenr, blocksize, generation);
2049 btrfs_tree_lock(eb);
2050 btrfs_set_lock_blocking(eb);
2053 ret = btrfs_cow_block(trans, root, eb, upper->eb,
2059 btrfs_set_lock_blocking(eb);
2063 btrfs_set_node_blockptr(upper->eb, slot,
2065 btrfs_set_node_ptr_generation(upper->eb, slot,
2067 btrfs_mark_buffer_dirty(upper->eb);
2069 ret = btrfs_inc_extent_ref(trans, root,
2070 node->eb->start, blocksize,
2072 btrfs_header_owner(upper->eb),
2076 ret = btrfs_drop_subtree(trans, root, eb, upper->eb);
2079 btrfs_tree_unlock(eb);
2080 free_extent_buffer(eb);
2083 btrfs_tree_unlock(upper->eb);
2087 path->lowest_level = 0;
2091 static int link_to_upper(struct btrfs_trans_handle *trans,
2092 struct backref_node *node,
2093 struct btrfs_path *path)
2095 struct btrfs_key key;
2096 if (!node->eb || list_empty(&node->upper))
2099 btrfs_node_key_to_cpu(node->eb, &key, 0);
2100 return do_relocation(trans, node, &key, path, 0);
2103 static int finish_pending_nodes(struct btrfs_trans_handle *trans,
2104 struct backref_cache *cache,
2105 struct btrfs_path *path)
2107 struct backref_node *node;
2112 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2113 while (!list_empty(&cache->pending[level])) {
2114 node = list_entry(cache->pending[level].next,
2115 struct backref_node, lower);
2116 BUG_ON(node->level != level);
2118 ret = link_to_upper(trans, node, path);
2122 * this remove the node from the pending list and
2123 * may add some other nodes to the level + 1
2126 remove_backref_node(cache, node);
2129 BUG_ON(!RB_EMPTY_ROOT(&cache->rb_root));
2133 static void mark_block_processed(struct reloc_control *rc,
2134 struct backref_node *node)
2137 if (node->level == 0 ||
2138 in_block_group(node->bytenr, rc->block_group)) {
2139 blocksize = btrfs_level_size(rc->extent_root, node->level);
2140 set_extent_bits(&rc->processed_blocks, node->bytenr,
2141 node->bytenr + blocksize - 1, EXTENT_DIRTY,
2144 node->processed = 1;
2148 * mark a block and all blocks directly/indirectly reference the block
2151 static void update_processed_blocks(struct reloc_control *rc,
2152 struct backref_node *node)
2154 struct backref_node *next = node;
2155 struct backref_edge *edge;
2156 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
2162 if (next->processed)
2165 mark_block_processed(rc, next);
2167 if (list_empty(&next->upper))
2170 edge = list_entry(next->upper.next,
2171 struct backref_edge, list[LOWER]);
2172 edges[index++] = edge;
2173 next = edge->node[UPPER];
2175 next = walk_down_backref(edges, &index);
2179 static int tree_block_processed(u64 bytenr, u32 blocksize,
2180 struct reloc_control *rc)
2182 if (test_range_bit(&rc->processed_blocks, bytenr,
2183 bytenr + blocksize - 1, EXTENT_DIRTY, 1))
2189 * check if there are any file extent pointers in the leaf point to
2190 * data require processing
2192 static int check_file_extents(struct reloc_control *rc,
2193 u64 bytenr, u32 blocksize, u64 ptr_gen)
2195 struct btrfs_key found_key;
2196 struct btrfs_file_extent_item *fi;
2197 struct extent_buffer *leaf;
2202 leaf = read_tree_block(rc->extent_root, bytenr, blocksize, ptr_gen);
2204 nritems = btrfs_header_nritems(leaf);
2205 for (i = 0; i < nritems; i++) {
2207 btrfs_item_key_to_cpu(leaf, &found_key, i);
2208 if (found_key.type != BTRFS_EXTENT_DATA_KEY)
2210 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2211 if (btrfs_file_extent_type(leaf, fi) ==
2212 BTRFS_FILE_EXTENT_INLINE)
2214 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2217 if (in_block_group(bytenr, rc->block_group)) {
2222 free_extent_buffer(leaf);
2227 * scan child blocks of a given block to find blocks require processing
2229 static int add_child_blocks(struct btrfs_trans_handle *trans,
2230 struct reloc_control *rc,
2231 struct backref_node *node,
2232 struct rb_root *blocks)
2234 struct tree_block *block;
2235 struct rb_node *rb_node;
2243 nritems = btrfs_header_nritems(node->eb);
2244 blocksize = btrfs_level_size(rc->extent_root, node->level - 1);
2245 for (i = 0; i < nritems; i++) {
2247 bytenr = btrfs_node_blockptr(node->eb, i);
2248 ptr_gen = btrfs_node_ptr_generation(node->eb, i);
2249 if (ptr_gen == trans->transid)
2251 if (!in_block_group(bytenr, rc->block_group) &&
2252 (node->level > 1 || rc->stage == MOVE_DATA_EXTENTS))
2254 if (tree_block_processed(bytenr, blocksize, rc))
2257 readahead_tree_block(rc->extent_root,
2258 bytenr, blocksize, ptr_gen);
2261 for (i = 0; i < nritems; i++) {
2263 bytenr = btrfs_node_blockptr(node->eb, i);
2264 ptr_gen = btrfs_node_ptr_generation(node->eb, i);
2265 if (ptr_gen == trans->transid)
2267 if (!in_block_group(bytenr, rc->block_group) &&
2268 (node->level > 1 || rc->stage == MOVE_DATA_EXTENTS))
2270 if (tree_block_processed(bytenr, blocksize, rc))
2272 if (!in_block_group(bytenr, rc->block_group) &&
2273 !check_file_extents(rc, bytenr, blocksize, ptr_gen))
2276 block = kmalloc(sizeof(*block), GFP_NOFS);
2281 block->bytenr = bytenr;
2282 btrfs_node_key_to_cpu(node->eb, &block->key, i);
2283 block->level = node->level - 1;
2284 block->key_ready = 1;
2285 rb_node = tree_insert(blocks, block->bytenr, &block->rb_node);
2289 free_block_list(blocks);
2294 * find adjacent blocks require processing
2296 static noinline_for_stack
2297 int add_adjacent_blocks(struct btrfs_trans_handle *trans,
2298 struct reloc_control *rc,
2299 struct backref_cache *cache,
2300 struct rb_root *blocks, int level,
2301 struct backref_node **upper)
2303 struct backref_node *node;
2306 WARN_ON(!list_empty(&cache->pending[level]));
2308 if (list_empty(&cache->pending[level + 1]))
2311 node = list_entry(cache->pending[level + 1].next,
2312 struct backref_node, lower);
2314 ret = add_child_blocks(trans, rc, node, blocks);
2320 static int get_tree_block_key(struct reloc_control *rc,
2321 struct tree_block *block)
2323 struct extent_buffer *eb;
2325 BUG_ON(block->key_ready);
2326 eb = read_tree_block(rc->extent_root, block->bytenr,
2327 block->key.objectid, block->key.offset);
2328 WARN_ON(btrfs_header_level(eb) != block->level);
2329 if (block->level == 0)
2330 btrfs_item_key_to_cpu(eb, &block->key, 0);
2332 btrfs_node_key_to_cpu(eb, &block->key, 0);
2333 free_extent_buffer(eb);
2334 block->key_ready = 1;
2338 static int reada_tree_block(struct reloc_control *rc,
2339 struct tree_block *block)
2341 BUG_ON(block->key_ready);
2342 readahead_tree_block(rc->extent_root, block->bytenr,
2343 block->key.objectid, block->key.offset);
2348 * helper function to relocate a tree block
2350 static int relocate_tree_block(struct btrfs_trans_handle *trans,
2351 struct reloc_control *rc,
2352 struct backref_node *node,
2353 struct btrfs_key *key,
2354 struct btrfs_path *path)
2356 struct btrfs_root *root;
2359 root = select_one_root(trans, node);
2360 if (unlikely(!root)) {
2361 rc->found_old_snapshot = 1;
2362 update_processed_blocks(rc, node);
2366 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
2367 ret = do_relocation(trans, node, key, path, 1);
2370 if (node->level == 0 && rc->stage == UPDATE_DATA_PTRS) {
2371 ret = replace_file_extents(trans, rc, root,
2376 drop_node_buffer(node);
2377 } else if (!root->ref_cows) {
2378 path->lowest_level = node->level;
2379 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2380 btrfs_release_path(root, path);
2383 } else if (root != node->root) {
2384 WARN_ON(node->level > 0 || rc->stage != UPDATE_DATA_PTRS);
2387 update_processed_blocks(rc, node);
2390 drop_node_buffer(node);
2395 * relocate a list of blocks
2397 static noinline_for_stack
2398 int relocate_tree_blocks(struct btrfs_trans_handle *trans,
2399 struct reloc_control *rc, struct rb_root *blocks)
2401 struct backref_cache *cache;
2402 struct backref_node *node;
2403 struct btrfs_path *path;
2404 struct tree_block *block;
2405 struct rb_node *rb_node;
2410 path = btrfs_alloc_path();
2414 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2416 btrfs_free_path(path);
2420 backref_cache_init(cache);
2422 rb_node = rb_first(blocks);
2424 block = rb_entry(rb_node, struct tree_block, rb_node);
2426 level = block->level;
2428 BUG_ON(level != block->level);
2429 if (!block->key_ready)
2430 reada_tree_block(rc, block);
2431 rb_node = rb_next(rb_node);
2434 rb_node = rb_first(blocks);
2436 block = rb_entry(rb_node, struct tree_block, rb_node);
2437 if (!block->key_ready)
2438 get_tree_block_key(rc, block);
2439 rb_node = rb_next(rb_node);
2442 rb_node = rb_first(blocks);
2444 block = rb_entry(rb_node, struct tree_block, rb_node);
2446 node = build_backref_tree(rc, cache, &block->key,
2447 block->level, block->bytenr);
2449 err = PTR_ERR(node);
2453 ret = relocate_tree_block(trans, rc, node, &block->key,
2459 remove_backref_node(cache, node);
2460 rb_node = rb_next(rb_node);
2466 free_block_list(blocks);
2469 * now backrefs of some upper level tree blocks have been cached,
2470 * try relocating blocks referenced by these upper level blocks.
2473 struct backref_node *upper = NULL;
2474 if (trans->transaction->in_commit ||
2475 trans->transaction->delayed_refs.flushing)
2478 ret = add_adjacent_blocks(trans, rc, cache, blocks, level,
2485 rb_node = rb_first(blocks);
2487 block = rb_entry(rb_node, struct tree_block, rb_node);
2488 if (trans->transaction->in_commit ||
2489 trans->transaction->delayed_refs.flushing)
2491 BUG_ON(!block->key_ready);
2492 node = build_backref_tree(rc, cache, &block->key,
2493 level, block->bytenr);
2495 err = PTR_ERR(node);
2499 ret = relocate_tree_block(trans, rc, node,
2505 remove_backref_node(cache, node);
2506 rb_node = rb_next(rb_node);
2508 free_block_list(blocks);
2511 ret = link_to_upper(trans, upper, path);
2516 remove_backref_node(cache, upper);
2520 free_block_list(blocks);
2522 ret = finish_pending_nodes(trans, cache, path);
2527 btrfs_free_path(path);
2531 static noinline_for_stack
2532 int relocate_inode_pages(struct inode *inode, u64 start, u64 len)
2537 unsigned long first_index;
2538 unsigned long last_index;
2539 unsigned int total_read = 0;
2540 unsigned int total_dirty = 0;
2542 struct file_ra_state *ra;
2543 struct btrfs_ordered_extent *ordered;
2544 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2547 ra = kzalloc(sizeof(*ra), GFP_NOFS);
2551 mutex_lock(&inode->i_mutex);
2552 first_index = start >> PAGE_CACHE_SHIFT;
2553 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2555 /* make sure the dirty trick played by the caller work */
2556 ret = invalidate_inode_pages2_range(inode->i_mapping,
2557 first_index, last_index);
2561 file_ra_state_init(ra, inode->i_mapping);
2563 for (i = first_index ; i <= last_index; i++) {
2564 if (total_read % ra->ra_pages == 0) {
2565 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
2566 min(last_index, ra->ra_pages + i - 1));
2570 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
2572 page = grab_cache_page(inode->i_mapping, i);
2577 if (!PageUptodate(page)) {
2578 btrfs_readpage(NULL, page);
2580 if (!PageUptodate(page)) {
2582 page_cache_release(page);
2587 wait_on_page_writeback(page);
2589 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2590 page_end = page_start + PAGE_CACHE_SIZE - 1;
2591 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2593 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2595 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2597 page_cache_release(page);
2598 btrfs_start_ordered_extent(inode, ordered, 1);
2599 btrfs_put_ordered_extent(ordered);
2602 set_page_extent_mapped(page);
2604 if (i == first_index)
2605 set_extent_bits(io_tree, page_start, page_end,
2606 EXTENT_BOUNDARY, GFP_NOFS);
2607 btrfs_set_extent_delalloc(inode, page_start, page_end);
2609 set_page_dirty(page);
2612 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2614 page_cache_release(page);
2617 mutex_unlock(&inode->i_mutex);
2619 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
2623 static noinline_for_stack
2624 int relocate_data_extent(struct inode *inode, struct btrfs_key *extent_key)
2626 struct btrfs_root *root = BTRFS_I(inode)->root;
2627 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2628 struct extent_map *em;
2629 u64 start = extent_key->objectid - BTRFS_I(inode)->index_cnt;
2630 u64 end = start + extent_key->offset - 1;
2632 em = alloc_extent_map(GFP_NOFS);
2634 em->len = extent_key->offset;
2635 em->block_len = extent_key->offset;
2636 em->block_start = extent_key->objectid;
2637 em->bdev = root->fs_info->fs_devices->latest_bdev;
2638 set_bit(EXTENT_FLAG_PINNED, &em->flags);
2640 /* setup extent map to cheat btrfs_readpage */
2641 lock_extent(&BTRFS_I(inode)->io_tree, start, end, GFP_NOFS);
2644 spin_lock(&em_tree->lock);
2645 ret = add_extent_mapping(em_tree, em);
2646 spin_unlock(&em_tree->lock);
2647 if (ret != -EEXIST) {
2648 free_extent_map(em);
2651 btrfs_drop_extent_cache(inode, start, end, 0);
2653 unlock_extent(&BTRFS_I(inode)->io_tree, start, end, GFP_NOFS);
2655 return relocate_inode_pages(inode, start, extent_key->offset);
2658 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2659 static int get_ref_objectid_v0(struct reloc_control *rc,
2660 struct btrfs_path *path,
2661 struct btrfs_key *extent_key,
2662 u64 *ref_objectid, int *path_change)
2664 struct btrfs_key key;
2665 struct extent_buffer *leaf;
2666 struct btrfs_extent_ref_v0 *ref0;
2670 leaf = path->nodes[0];
2671 slot = path->slots[0];
2673 if (slot >= btrfs_header_nritems(leaf)) {
2674 ret = btrfs_next_leaf(rc->extent_root, path);
2678 leaf = path->nodes[0];
2679 slot = path->slots[0];
2683 btrfs_item_key_to_cpu(leaf, &key, slot);
2684 if (key.objectid != extent_key->objectid)
2687 if (key.type != BTRFS_EXTENT_REF_V0_KEY) {
2691 ref0 = btrfs_item_ptr(leaf, slot,
2692 struct btrfs_extent_ref_v0);
2693 *ref_objectid = btrfs_ref_objectid_v0(leaf, ref0);
2701 * helper to add a tree block to the list.
2702 * the major work is getting the generation and level of the block
2704 static int add_tree_block(struct reloc_control *rc,
2705 struct btrfs_key *extent_key,
2706 struct btrfs_path *path,
2707 struct rb_root *blocks)
2709 struct extent_buffer *eb;
2710 struct btrfs_extent_item *ei;
2711 struct btrfs_tree_block_info *bi;
2712 struct tree_block *block;
2713 struct rb_node *rb_node;
2718 eb = path->nodes[0];
2719 item_size = btrfs_item_size_nr(eb, path->slots[0]);
2721 if (item_size >= sizeof(*ei) + sizeof(*bi)) {
2722 ei = btrfs_item_ptr(eb, path->slots[0],
2723 struct btrfs_extent_item);
2724 bi = (struct btrfs_tree_block_info *)(ei + 1);
2725 generation = btrfs_extent_generation(eb, ei);
2726 level = btrfs_tree_block_level(eb, bi);
2728 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2732 BUG_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2733 ret = get_ref_objectid_v0(rc, path, extent_key,
2735 BUG_ON(ref_owner >= BTRFS_MAX_LEVEL);
2736 level = (int)ref_owner;
2737 /* FIXME: get real generation */
2744 btrfs_release_path(rc->extent_root, path);
2746 BUG_ON(level == -1);
2748 block = kmalloc(sizeof(*block), GFP_NOFS);
2752 block->bytenr = extent_key->objectid;
2753 block->key.objectid = extent_key->offset;
2754 block->key.offset = generation;
2755 block->level = level;
2756 block->key_ready = 0;
2758 rb_node = tree_insert(blocks, block->bytenr, &block->rb_node);
2765 * helper to add tree blocks for backref of type BTRFS_SHARED_DATA_REF_KEY
2767 static int __add_tree_block(struct reloc_control *rc,
2768 u64 bytenr, u32 blocksize,
2769 struct rb_root *blocks)
2771 struct btrfs_path *path;
2772 struct btrfs_key key;
2775 if (tree_block_processed(bytenr, blocksize, rc))
2778 if (tree_search(blocks, bytenr))
2781 path = btrfs_alloc_path();
2785 key.objectid = bytenr;
2786 key.type = BTRFS_EXTENT_ITEM_KEY;
2787 key.offset = blocksize;
2789 path->search_commit_root = 1;
2790 path->skip_locking = 1;
2791 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path, 0, 0);
2796 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2797 ret = add_tree_block(rc, &key, path, blocks);
2799 btrfs_free_path(path);
2804 * helper to check if the block use full backrefs for pointers in it
2806 static int block_use_full_backref(struct reloc_control *rc,
2807 struct extent_buffer *eb)
2809 struct btrfs_path *path;
2810 struct btrfs_extent_item *ei;
2811 struct btrfs_key key;
2815 if (btrfs_header_flag(eb, BTRFS_HEADER_FLAG_RELOC) ||
2816 btrfs_header_backref_rev(eb) < BTRFS_MIXED_BACKREF_REV)
2819 path = btrfs_alloc_path();
2822 key.objectid = eb->start;
2823 key.type = BTRFS_EXTENT_ITEM_KEY;
2824 key.offset = eb->len;
2826 path->search_commit_root = 1;
2827 path->skip_locking = 1;
2828 ret = btrfs_search_slot(NULL, rc->extent_root,
2832 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
2833 struct btrfs_extent_item);
2834 flags = btrfs_extent_flags(path->nodes[0], ei);
2835 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2836 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
2840 btrfs_free_path(path);
2845 * helper to add tree blocks for backref of type BTRFS_EXTENT_DATA_REF_KEY
2846 * this function scans fs tree to find blocks reference the data extent
2848 static int find_data_references(struct reloc_control *rc,
2849 struct btrfs_key *extent_key,
2850 struct extent_buffer *leaf,
2851 struct btrfs_extent_data_ref *ref,
2852 struct rb_root *blocks)
2854 struct btrfs_path *path;
2855 struct tree_block *block;
2856 struct btrfs_root *root;
2857 struct btrfs_file_extent_item *fi;
2858 struct rb_node *rb_node;
2859 struct btrfs_key key;
2870 path = btrfs_alloc_path();
2874 ref_root = btrfs_extent_data_ref_root(leaf, ref);
2875 ref_objectid = btrfs_extent_data_ref_objectid(leaf, ref);
2876 ref_offset = btrfs_extent_data_ref_offset(leaf, ref);
2877 ref_count = btrfs_extent_data_ref_count(leaf, ref);
2879 root = read_fs_root(rc->extent_root->fs_info, ref_root);
2881 err = PTR_ERR(root);
2885 key.objectid = ref_objectid;
2886 key.offset = ref_offset;
2887 key.type = BTRFS_EXTENT_DATA_KEY;
2889 path->search_commit_root = 1;
2890 path->skip_locking = 1;
2891 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2897 leaf = path->nodes[0];
2898 nritems = btrfs_header_nritems(leaf);
2900 * the references in tree blocks that use full backrefs
2901 * are not counted in
2903 if (block_use_full_backref(rc, leaf))
2907 rb_node = tree_search(blocks, leaf->start);
2912 path->slots[0] = nritems;
2915 while (ref_count > 0) {
2916 while (path->slots[0] >= nritems) {
2917 ret = btrfs_next_leaf(root, path);
2927 leaf = path->nodes[0];
2928 nritems = btrfs_header_nritems(leaf);
2931 if (block_use_full_backref(rc, leaf))
2935 rb_node = tree_search(blocks, leaf->start);
2940 path->slots[0] = nritems;
2944 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2945 if (key.objectid != ref_objectid ||
2946 key.type != BTRFS_EXTENT_DATA_KEY) {
2951 fi = btrfs_item_ptr(leaf, path->slots[0],
2952 struct btrfs_file_extent_item);
2954 if (btrfs_file_extent_type(leaf, fi) ==
2955 BTRFS_FILE_EXTENT_INLINE)
2958 if (btrfs_file_extent_disk_bytenr(leaf, fi) !=
2959 extent_key->objectid)
2962 key.offset -= btrfs_file_extent_offset(leaf, fi);
2963 if (key.offset != ref_offset)
2971 if (!tree_block_processed(leaf->start, leaf->len, rc)) {
2972 block = kmalloc(sizeof(*block), GFP_NOFS);
2977 block->bytenr = leaf->start;
2978 btrfs_item_key_to_cpu(leaf, &block->key, 0);
2980 block->key_ready = 1;
2981 rb_node = tree_insert(blocks, block->bytenr,
2988 path->slots[0] = nritems;
2994 btrfs_free_path(path);
2999 * hepler to find all tree blocks that reference a given data extent
3001 static noinline_for_stack
3002 int add_data_references(struct reloc_control *rc,
3003 struct btrfs_key *extent_key,
3004 struct btrfs_path *path,
3005 struct rb_root *blocks)
3007 struct btrfs_key key;
3008 struct extent_buffer *eb;
3009 struct btrfs_extent_data_ref *dref;
3010 struct btrfs_extent_inline_ref *iref;
3017 ret = get_new_location(rc->data_inode, NULL, extent_key->objectid,
3018 extent_key->offset);
3021 /* the relocated data is fragmented */
3022 rc->extents_skipped++;
3023 btrfs_release_path(rc->extent_root, path);
3027 blocksize = btrfs_level_size(rc->extent_root, 0);
3029 eb = path->nodes[0];
3030 ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
3031 end = ptr + btrfs_item_size_nr(eb, path->slots[0]);
3032 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3033 if (ptr + sizeof(struct btrfs_extent_item_v0) == end)
3037 ptr += sizeof(struct btrfs_extent_item);
3040 iref = (struct btrfs_extent_inline_ref *)ptr;
3041 key.type = btrfs_extent_inline_ref_type(eb, iref);
3042 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3043 key.offset = btrfs_extent_inline_ref_offset(eb, iref);
3044 ret = __add_tree_block(rc, key.offset, blocksize,
3046 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3047 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
3048 ret = find_data_references(rc, extent_key,
3053 ptr += btrfs_extent_inline_ref_size(key.type);
3059 eb = path->nodes[0];
3060 if (path->slots[0] >= btrfs_header_nritems(eb)) {
3061 ret = btrfs_next_leaf(rc->extent_root, path);
3068 eb = path->nodes[0];
3071 btrfs_item_key_to_cpu(eb, &key, path->slots[0]);
3072 if (key.objectid != extent_key->objectid)
3075 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3076 if (key.type == BTRFS_SHARED_DATA_REF_KEY ||
3077 key.type == BTRFS_EXTENT_REF_V0_KEY) {
3079 BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
3080 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3082 ret = __add_tree_block(rc, key.offset, blocksize,
3084 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3085 dref = btrfs_item_ptr(eb, path->slots[0],
3086 struct btrfs_extent_data_ref);
3087 ret = find_data_references(rc, extent_key,
3098 btrfs_release_path(rc->extent_root, path);
3100 free_block_list(blocks);
3105 * hepler to find next unprocessed extent
3107 static noinline_for_stack
3108 int find_next_extent(struct btrfs_trans_handle *trans,
3109 struct reloc_control *rc, struct btrfs_path *path)
3111 struct btrfs_key key;
3112 struct extent_buffer *leaf;
3113 u64 start, end, last;
3116 last = rc->block_group->key.objectid + rc->block_group->key.offset;
3119 if (rc->search_start >= last) {
3124 key.objectid = rc->search_start;
3125 key.type = BTRFS_EXTENT_ITEM_KEY;
3128 path->search_commit_root = 1;
3129 path->skip_locking = 1;
3130 ret = btrfs_search_slot(NULL, rc->extent_root, &key, path,
3135 leaf = path->nodes[0];
3136 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3137 ret = btrfs_next_leaf(rc->extent_root, path);
3140 leaf = path->nodes[0];
3143 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3144 if (key.objectid >= last) {
3149 if (key.type != BTRFS_EXTENT_ITEM_KEY ||
3150 key.objectid + key.offset <= rc->search_start) {
3155 ret = find_first_extent_bit(&rc->processed_blocks,
3156 key.objectid, &start, &end,
3159 if (ret == 0 && start <= key.objectid) {
3160 btrfs_release_path(rc->extent_root, path);
3161 rc->search_start = end + 1;
3163 rc->search_start = key.objectid + key.offset;
3167 btrfs_release_path(rc->extent_root, path);
3171 static void set_reloc_control(struct reloc_control *rc)
3173 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3174 mutex_lock(&fs_info->trans_mutex);
3175 fs_info->reloc_ctl = rc;
3176 mutex_unlock(&fs_info->trans_mutex);
3179 static void unset_reloc_control(struct reloc_control *rc)
3181 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3182 mutex_lock(&fs_info->trans_mutex);
3183 fs_info->reloc_ctl = NULL;
3184 mutex_unlock(&fs_info->trans_mutex);
3187 static int check_extent_flags(u64 flags)
3189 if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
3190 (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
3192 if (!(flags & BTRFS_EXTENT_FLAG_DATA) &&
3193 !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
3195 if ((flags & BTRFS_EXTENT_FLAG_DATA) &&
3196 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
3201 static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
3203 struct rb_root blocks = RB_ROOT;
3204 struct btrfs_key key;
3205 struct btrfs_trans_handle *trans = NULL;
3206 struct btrfs_path *path;
3207 struct btrfs_extent_item *ei;
3214 path = btrfs_alloc_path();
3218 rc->search_start = rc->block_group->key.objectid;
3219 clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY,
3222 rc->create_reloc_root = 1;
3223 set_reloc_control(rc);
3225 trans = btrfs_start_transaction(rc->extent_root, 1);
3226 btrfs_commit_transaction(trans, rc->extent_root);
3229 trans = btrfs_start_transaction(rc->extent_root, 1);
3231 ret = find_next_extent(trans, rc, path);
3237 rc->extents_found++;
3239 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
3240 struct btrfs_extent_item);
3241 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3242 item_size = btrfs_item_size_nr(path->nodes[0],
3244 if (item_size >= sizeof(*ei)) {
3245 flags = btrfs_extent_flags(path->nodes[0], ei);
3246 ret = check_extent_flags(flags);
3250 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3252 int path_change = 0;
3255 sizeof(struct btrfs_extent_item_v0));
3256 ret = get_ref_objectid_v0(rc, path, &key, &ref_owner,
3258 if (ref_owner < BTRFS_FIRST_FREE_OBJECTID)
3259 flags = BTRFS_EXTENT_FLAG_TREE_BLOCK;
3261 flags = BTRFS_EXTENT_FLAG_DATA;
3264 btrfs_release_path(rc->extent_root, path);
3266 path->search_commit_root = 1;
3267 path->skip_locking = 1;
3268 ret = btrfs_search_slot(NULL, rc->extent_root,
3281 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
3282 ret = add_tree_block(rc, &key, path, &blocks);
3283 } else if (rc->stage == UPDATE_DATA_PTRS &&
3284 (flags & BTRFS_EXTENT_FLAG_DATA)) {
3285 ret = add_data_references(rc, &key, path, &blocks);
3287 btrfs_release_path(rc->extent_root, path);
3295 if (!RB_EMPTY_ROOT(&blocks)) {
3296 ret = relocate_tree_blocks(trans, rc, &blocks);
3303 nr = trans->blocks_used;
3304 btrfs_end_transaction_throttle(trans, rc->extent_root);
3306 btrfs_btree_balance_dirty(rc->extent_root, nr);
3308 if (rc->stage == MOVE_DATA_EXTENTS &&
3309 (flags & BTRFS_EXTENT_FLAG_DATA)) {
3310 rc->found_file_extent = 1;
3311 ret = relocate_data_extent(rc->data_inode, &key);
3318 btrfs_free_path(path);
3321 nr = trans->blocks_used;
3322 btrfs_end_transaction(trans, rc->extent_root);
3323 btrfs_btree_balance_dirty(rc->extent_root, nr);
3326 rc->create_reloc_root = 0;
3329 if (rc->extents_found > 0) {
3330 trans = btrfs_start_transaction(rc->extent_root, 1);
3331 btrfs_commit_transaction(trans, rc->extent_root);
3334 merge_reloc_roots(rc);
3336 unset_reloc_control(rc);
3338 /* get rid of pinned extents */
3339 trans = btrfs_start_transaction(rc->extent_root, 1);
3340 btrfs_commit_transaction(trans, rc->extent_root);
3345 static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
3346 struct btrfs_root *root,
3347 u64 objectid, u64 size)
3349 struct btrfs_path *path;
3350 struct btrfs_inode_item *item;
3351 struct extent_buffer *leaf;
3354 path = btrfs_alloc_path();
3358 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
3362 leaf = path->nodes[0];
3363 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
3364 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
3365 btrfs_set_inode_generation(leaf, item, 1);
3366 btrfs_set_inode_size(leaf, item, size);
3367 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
3368 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
3369 btrfs_mark_buffer_dirty(leaf);
3370 btrfs_release_path(root, path);
3372 btrfs_free_path(path);
3377 * helper to create inode for data relocation.
3378 * the inode is in data relocation tree and its link count is 0
3380 static struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
3381 struct btrfs_block_group_cache *group)
3383 struct inode *inode = NULL;
3384 struct btrfs_trans_handle *trans;
3385 struct btrfs_root *root;
3386 struct btrfs_key key;
3388 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
3391 root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID);
3393 return ERR_CAST(root);
3395 trans = btrfs_start_transaction(root, 1);
3398 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
3402 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
3405 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
3406 group->key.offset, 0, group->key.offset,
3410 key.objectid = objectid;
3411 key.type = BTRFS_INODE_ITEM_KEY;
3413 inode = btrfs_iget(root->fs_info->sb, &key, root);
3414 BUG_ON(IS_ERR(inode) || is_bad_inode(inode));
3415 BTRFS_I(inode)->index_cnt = group->key.objectid;
3417 err = btrfs_orphan_add(trans, inode);
3419 nr = trans->blocks_used;
3420 btrfs_end_transaction(trans, root);
3422 btrfs_btree_balance_dirty(root, nr);
3426 inode = ERR_PTR(err);
3432 * function to relocate all extents in a block group.
3434 int btrfs_relocate_block_group(struct btrfs_root *extent_root, u64 group_start)
3436 struct btrfs_fs_info *fs_info = extent_root->fs_info;
3437 struct reloc_control *rc;
3441 rc = kzalloc(sizeof(*rc), GFP_NOFS);
3445 mapping_tree_init(&rc->reloc_root_tree);
3446 extent_io_tree_init(&rc->processed_blocks, NULL, GFP_NOFS);
3447 INIT_LIST_HEAD(&rc->reloc_roots);
3449 rc->block_group = btrfs_lookup_block_group(fs_info, group_start);
3450 BUG_ON(!rc->block_group);
3452 btrfs_init_workers(&rc->workers, "relocate",
3453 fs_info->thread_pool_size);
3455 rc->extent_root = extent_root;
3456 btrfs_prepare_block_group_relocation(extent_root, rc->block_group);
3458 rc->data_inode = create_reloc_inode(fs_info, rc->block_group);
3459 if (IS_ERR(rc->data_inode)) {
3460 err = PTR_ERR(rc->data_inode);
3461 rc->data_inode = NULL;
3465 printk(KERN_INFO "btrfs: relocating block group %llu flags %llu\n",
3466 (unsigned long long)rc->block_group->key.objectid,
3467 (unsigned long long)rc->block_group->flags);
3469 btrfs_start_delalloc_inodes(fs_info->tree_root);
3470 btrfs_wait_ordered_extents(fs_info->tree_root, 0);
3473 mutex_lock(&fs_info->cleaner_mutex);
3474 btrfs_clean_old_snapshots(fs_info->tree_root);
3475 mutex_unlock(&fs_info->cleaner_mutex);
3477 rc->extents_found = 0;
3478 rc->extents_skipped = 0;
3480 ret = relocate_block_group(rc);
3486 if (rc->extents_found == 0)
3489 printk(KERN_INFO "btrfs: found %llu extents\n",
3490 (unsigned long long)rc->extents_found);
3492 if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) {
3493 btrfs_wait_ordered_range(rc->data_inode, 0, (u64)-1);
3494 invalidate_mapping_pages(rc->data_inode->i_mapping,
3496 rc->stage = UPDATE_DATA_PTRS;
3497 } else if (rc->stage == UPDATE_DATA_PTRS &&
3498 rc->extents_skipped >= rc->extents_found) {
3499 iput(rc->data_inode);
3500 rc->data_inode = create_reloc_inode(fs_info,
3502 if (IS_ERR(rc->data_inode)) {
3503 err = PTR_ERR(rc->data_inode);
3504 rc->data_inode = NULL;
3507 rc->stage = MOVE_DATA_EXTENTS;
3508 rc->found_file_extent = 0;
3512 filemap_fdatawrite_range(fs_info->btree_inode->i_mapping,
3513 rc->block_group->key.objectid,
3514 rc->block_group->key.objectid +
3515 rc->block_group->key.offset - 1);
3517 WARN_ON(rc->block_group->pinned > 0);
3518 WARN_ON(rc->block_group->reserved > 0);
3519 WARN_ON(btrfs_block_group_used(&rc->block_group->item) > 0);
3521 iput(rc->data_inode);
3522 btrfs_stop_workers(&rc->workers);
3523 btrfs_put_block_group(rc->block_group);
3529 * recover relocation interrupted by system crash.
3531 * this function resumes merging reloc trees with corresponding fs trees.
3532 * this is important for keeping the sharing of tree blocks
3534 int btrfs_recover_relocation(struct btrfs_root *root)
3536 LIST_HEAD(reloc_roots);
3537 struct btrfs_key key;
3538 struct btrfs_root *fs_root;
3539 struct btrfs_root *reloc_root;
3540 struct btrfs_path *path;
3541 struct extent_buffer *leaf;
3542 struct reloc_control *rc = NULL;
3543 struct btrfs_trans_handle *trans;
3547 path = btrfs_alloc_path();
3551 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
3552 key.type = BTRFS_ROOT_ITEM_KEY;
3553 key.offset = (u64)-1;
3556 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key,
3563 if (path->slots[0] == 0)
3567 leaf = path->nodes[0];
3568 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3569 btrfs_release_path(root->fs_info->tree_root, path);
3571 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID ||
3572 key.type != BTRFS_ROOT_ITEM_KEY)
3575 reloc_root = btrfs_read_fs_root_no_radix(root, &key);
3576 if (IS_ERR(reloc_root)) {
3577 err = PTR_ERR(reloc_root);
3581 list_add(&reloc_root->root_list, &reloc_roots);
3583 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
3584 fs_root = read_fs_root(root->fs_info,
3585 reloc_root->root_key.offset);
3586 if (IS_ERR(fs_root)) {
3587 err = PTR_ERR(fs_root);
3592 if (key.offset == 0)
3597 btrfs_release_path(root->fs_info->tree_root, path);
3599 if (list_empty(&reloc_roots))
3602 rc = kzalloc(sizeof(*rc), GFP_NOFS);
3608 mapping_tree_init(&rc->reloc_root_tree);
3609 INIT_LIST_HEAD(&rc->reloc_roots);
3610 btrfs_init_workers(&rc->workers, "relocate",
3611 root->fs_info->thread_pool_size);
3612 rc->extent_root = root->fs_info->extent_root;
3614 set_reloc_control(rc);
3616 while (!list_empty(&reloc_roots)) {
3617 reloc_root = list_entry(reloc_roots.next,
3618 struct btrfs_root, root_list);
3619 list_del(&reloc_root->root_list);
3621 if (btrfs_root_refs(&reloc_root->root_item) == 0) {
3622 list_add_tail(&reloc_root->root_list,
3627 fs_root = read_fs_root(root->fs_info,
3628 reloc_root->root_key.offset);
3629 BUG_ON(IS_ERR(fs_root));
3631 __add_reloc_root(reloc_root);
3632 fs_root->reloc_root = reloc_root;
3635 trans = btrfs_start_transaction(rc->extent_root, 1);
3636 btrfs_commit_transaction(trans, rc->extent_root);
3638 merge_reloc_roots(rc);
3640 unset_reloc_control(rc);
3642 trans = btrfs_start_transaction(rc->extent_root, 1);
3643 btrfs_commit_transaction(trans, rc->extent_root);
3646 btrfs_stop_workers(&rc->workers);
3649 while (!list_empty(&reloc_roots)) {
3650 reloc_root = list_entry(reloc_roots.next,
3651 struct btrfs_root, root_list);
3652 list_del(&reloc_root->root_list);
3653 free_extent_buffer(reloc_root->node);
3654 free_extent_buffer(reloc_root->commit_root);
3657 btrfs_free_path(path);
3660 /* cleanup orphan inode in data relocation tree */
3661 fs_root = read_fs_root(root->fs_info,
3662 BTRFS_DATA_RELOC_TREE_OBJECTID);
3663 if (IS_ERR(fs_root))
3664 err = PTR_ERR(fs_root);
3670 * helper to add ordered checksum for data relocation.
3672 * cloning checksum properly handles the nodatasum extents.
3673 * it also saves CPU time to re-calculate the checksum.
3675 int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
3677 struct btrfs_ordered_sum *sums;
3678 struct btrfs_sector_sum *sector_sum;
3679 struct btrfs_ordered_extent *ordered;
3680 struct btrfs_root *root = BTRFS_I(inode)->root;
3686 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
3687 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
3689 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
3690 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
3691 disk_bytenr + len - 1, &list);
3693 while (!list_empty(&list)) {
3694 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
3695 list_del_init(&sums->list);
3697 sector_sum = sums->sums;
3698 sums->bytenr = ordered->start;
3701 while (offset < sums->len) {
3702 sector_sum->bytenr += ordered->start - disk_bytenr;
3704 offset += root->sectorsize;
3707 btrfs_add_ordered_sum(inode, ordered, sums);
3709 btrfs_put_ordered_extent(ordered);